From fde1f62e767c37f6a6c717df3d03154d081da47e Mon Sep 17 00:00:00 2001
From: wangzijian <wangzijian22@huawei.com>
Date: Tue, 25 Feb 2025 07:11:27 +0800
Subject: [PATCH 1/2] Hikptool add support dump SDMA register information
 according to the usage environment.

1. support dump SDMA channel status reg e.g. hikptool sdma_dump -s -c <chip_id> -d <die_id>
2. support dump SDMA pc reg e.g. hikptool sdma_dump -p -c <chip_id> -d <die_id> -n <chn_id>
3. support dump SDMA vc reg e.g. hikptool sdma_dump -v -c <chip_id> -d <die_id> -n <chn_id>

Signed-off-by: wangzijian <wangzijian22@huawei.com>
---
 CMakeLists.txt                      |   1 +
 libhikptdev/include/hikptdev_plug.h |   3 +-
 sdma/sdma_func/sdma_common.h        |  31 +++++
 sdma/sdma_func/sdma_dump_reg.c      | 133 ++++++++++++++++++++++
 sdma/sdma_func/sdma_dump_reg.h      |  28 +++++
 sdma/user_cmd/sdma_cmd_dump.c       | 171 ++++++++++++++++++++++++++++
 sdma/user_cmd/sdma_tools_include.h  |  24 ++++
 7 files changed, 390 insertions(+), 1 deletion(-)
 create mode 100644 sdma/sdma_func/sdma_common.h
 create mode 100644 sdma/sdma_func/sdma_dump_reg.c
 create mode 100644 sdma/sdma_func/sdma_dump_reg.h
 create mode 100644 sdma/user_cmd/sdma_cmd_dump.c
 create mode 100644 sdma/user_cmd/sdma_tools_include.h

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 72f2dab..4f4eb03 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -46,6 +46,7 @@ file(GLOB_RECURSE HIKPTOOL_SRC
 	${CMAKE_CURRENT_SOURCE_DIR}/serdes/*.c
 	${CMAKE_CURRENT_SOURCE_DIR}/socip/*.c
 	${CMAKE_CURRENT_SOURCE_DIR}/hccs/*.c
+	${CMAKE_CURRENT_SOURCE_DIR}/sdma/*.c
 	${CMAKE_CURRENT_SOURCE_DIR}/tool_lib/*.c
 	${CMAKE_CURRENT_SOURCE_DIR}/info_collect/*.c
 	${CMAKE_CURRENT_SOURCE_DIR}/hikp_init_main.c
diff --git a/libhikptdev/include/hikptdev_plug.h b/libhikptdev/include/hikptdev_plug.h
index d45a654..375fb89 100644
--- a/libhikptdev/include/hikptdev_plug.h
+++ b/libhikptdev/include/hikptdev_plug.h
@@ -44,7 +44,8 @@ enum cmd_module_type {
 	DPDK_MOD = 9,
 	CXL_MOD = 10,
 	UB_MOD = 11,
-	HCCS_MOD = 16
+	HCCS_MOD = 16,
+	SDMA_MOD = 17
 };
 
 void hikp_unlock(void);
diff --git a/sdma/sdma_func/sdma_common.h b/sdma/sdma_func/sdma_common.h
new file mode 100644
index 0000000..40969b8
--- /dev/null
+++ b/sdma/sdma_func/sdma_common.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2025 Hisilicon Technologies Co., Ltd.
+ * Hikptool is licensed under Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ *          http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
+ * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
+ * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
+ *
+ * See the Mulan PSL v2 for more details.
+ */
+
+#ifndef SDMA_COMMON_H
+#define SDMA_COMMON_H
+
+#define RESP_MAX_NUM 160
+
+/* SDMA command code */
+enum sdma_cmd_type {
+	SDMA_DUMP = 0,
+};
+
+enum sdma_dump_cmd_type {
+	DUMP_UNKNOWN = 0,
+	DUMP_CHN_STATUS,
+	DUMP_CHN_PC,
+	DUMP_CHN_VC,
+};
+
+#endif /* SDMA_COMMON_H */
diff --git a/sdma/sdma_func/sdma_dump_reg.c b/sdma/sdma_func/sdma_dump_reg.c
new file mode 100644
index 0000000..7440fb6
--- /dev/null
+++ b/sdma/sdma_func/sdma_dump_reg.c
@@ -0,0 +1,133 @@
+/*
+ * Copyright (c) 2025 Hisilicon Technologies Co., Ltd.
+ * Hikptool is licensed under Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ *          http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
+ * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
+ * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
+ *
+ * See the Mulan PSL v2 for more details.
+ */
+#include <unistd.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <dirent.h>
+#include <stdio.h>
+#include <errno.h>
+#include "hikptdev_plug.h"
+#include "sdma_common.h"
+#include "sdma_dump_reg.h"
+
+#define TARGET_DIR "/sys/devices/platform/"
+#define PREFIX "HISI0431"
+#define PREFIX_LEN 8
+
+int sdma_dev_check(void)
+{
+	struct dirent *entry;
+	DIR *dir;
+
+	dir = opendir(TARGET_DIR);
+	if (dir == NULL) {
+		perror("opendir");
+		return -errno;
+	}
+
+	while ((entry = readdir(dir)) != NULL) {
+		if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
+			continue;
+
+		if (strlen(entry->d_name) >= PREFIX_LEN) {
+			if (strncmp(entry->d_name, PREFIX, PREFIX_LEN) == 0) {
+				closedir(dir);
+				return 0;
+			}
+		}
+	}
+
+	closedir(dir);
+	return -ENODEV;
+}
+
+static int sdma_rsp_normal_check(const struct hikp_cmd_ret *cmd_ret)
+{
+	if (cmd_ret == NULL)
+		return -ENOSPC;
+
+	if (cmd_ret->status != 0)
+		return -EINVAL;
+
+	if (cmd_ret->rsp_data_num > RESP_MAX_NUM)
+		return -E2BIG;
+
+	return 0;
+}
+
+static int sdma_get_reg(const struct tool_sdma_cmd *cmd, uint32_t *reg_save, uint32_t *reg_num)
+{
+	struct sdma_dump_req_para req_data = { 0 };
+	struct hikp_cmd_header req_header = { 0 };
+	struct hikp_cmd_ret *cmd_ret;
+	uint32_t i;
+	int ret;
+
+	req_data.chip_id = cmd->chip_id;
+	req_data.die_id = cmd->die_id;
+	req_data.chn_id = cmd->chn_id;
+
+	hikp_cmd_init(&req_header, SDMA_MOD, SDMA_DUMP, cmd->sdma_cmd_type);
+	cmd_ret = hikp_cmd_alloc(&req_header, &req_data, sizeof(req_data));
+	ret = sdma_rsp_normal_check(cmd_ret);
+	if (ret) {
+		printf("check cmd ret failed, ret: %d.\n", ret);
+		hikp_cmd_free(&cmd_ret);
+		return ret;
+	}
+	*reg_num = cmd_ret->rsp_data_num;
+	for (i = 0; i < *reg_num; i++)
+		reg_save[i] = cmd_ret->rsp_data[i];
+
+	hikp_cmd_free(&cmd_ret);
+
+	return 0;
+}
+
+static void sdma_print_reg(const uint32_t *reg_save, uint32_t reg_num)
+{
+	uint32_t i;
+
+	if (reg_num == 0) {
+		printf("SDMA dump is failed\n");
+		return;
+	}
+	printf("  sdma reg dump list:\n");
+	for (i = 0; i < reg_num; i++)
+		printf("    0x%08x\n", reg_save[i]);
+}
+
+int sdma_reg_dump(struct tool_sdma_cmd *cmd)
+{
+	uint32_t sdma_reg_save[RESP_MAX_NUM] = { 0 };
+	uint32_t sdma_reg_num = 0;
+	int ret;
+
+	if (cmd == NULL)
+		return -EINVAL;
+
+	ret = sdma_dev_check();
+	if (ret) {
+		printf("The current environment not support this feature!\n");
+		return ret;
+	}
+
+	ret = sdma_get_reg(cmd, sdma_reg_save, &sdma_reg_num);
+	if (ret)
+		return ret;
+
+	sdma_print_reg(sdma_reg_save, sdma_reg_num);
+
+	return 0;
+}
diff --git a/sdma/sdma_func/sdma_dump_reg.h b/sdma/sdma_func/sdma_dump_reg.h
new file mode 100644
index 0000000..51c4e66
--- /dev/null
+++ b/sdma/sdma_func/sdma_dump_reg.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2025 Hisilicon Technologies Co., Ltd.
+ * Hikptool is licensed under Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ *          http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
+ * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
+ * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
+ *
+ * See the Mulan PSL v2 for more details.
+ */
+
+#ifndef SDMA_DUMP_REG_H
+#define SDMA_DUMP_REG_H
+
+#include "sdma_tools_include.h"
+
+struct sdma_dump_req_para {
+	uint32_t chip_id;
+	uint32_t die_id;
+	uint32_t chn_id;
+};
+
+int sdma_dev_check(void);
+int sdma_reg_dump(struct tool_sdma_cmd *cmd);
+
+#endif /* SDMA_DUMP_REG_H */
diff --git a/sdma/user_cmd/sdma_cmd_dump.c b/sdma/user_cmd/sdma_cmd_dump.c
new file mode 100644
index 0000000..47b095d
--- /dev/null
+++ b/sdma/user_cmd/sdma_cmd_dump.c
@@ -0,0 +1,171 @@
+/*
+ * Copyright (c) 2025 Hisilicon Technologies Co., Ltd.
+ * Hikptool is licensed under Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ *          http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
+ * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
+ * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
+ *
+ * See the Mulan PSL v2 for more details.
+ */
+
+#include <stdint.h>
+#include "hikptdev_plug.h"
+#include "tool_lib.h"
+#include "tool_cmd.h"
+#include "sdma_tools_include.h"
+#include "sdma_common.h"
+#include "sdma_dump_reg.h"
+
+struct tool_sdma_cmd g_sdma_dump_cmd = {
+	.sdma_cmd_type = DUMP_UNKNOWN,
+	.chip_id = (uint32_t)(-1),
+	.die_id = (uint32_t)(-1),
+	.chn_id = (uint32_t)(-1),
+};
+
+static int sdma_dump_help(struct major_cmd_ctrl *self, const char *argv)
+{
+	int ret;
+
+	ret = sdma_dev_check();
+	if (ret) {
+		printf("The current environment not support this feature!\n");
+		return ret;
+	}
+	HIKP_SET_USED(argv);
+
+	printf("\n  Usage: %s\n", self->cmd_ptr->name);
+	printf("\n         %s\n", self->cmd_ptr->help_info);
+	printf("    %s, %-25s %s\n", "-c", "--chipid", "please input chip id[x]  first\n");
+	printf("    %s, %-25s %s\n", "-d", "--dieid", "please input die id[x]  first\n");
+	printf("    %s, %-25s %s\n", "-n", "--chnid", "please input chn id[x]  first\n");
+	printf("\n  Options:\n\n");
+	printf("    %s, %-25s %s\n", "-h", "--help", "display this help and exit\n");
+	printf("    %s, %-25s %s\n", "-s", "--chnstatus", "dump sdma channel status dfx reg\n");
+	printf("\tParameter Limitation: -c --chipid and -d --dieid is necessary,");
+	printf(" -n --chnid is invalid\n");
+	printf("\tUsage: -s -c [chipid] -d [dieid], e.g. -s -c 0 -d 0\n\n");
+	printf("    %s, %-25s %s\n", "-p", "--pc", "dump sdma pc channel dfx reg\n");
+	printf("\tParameter Limitation: All three parameters are necessary,");
+	printf(" the -n --chnid range is limited to 0-31\n");
+	printf("\tUsage: -p -c [chipid] -d [dieid] -n [chnid], e.g. -p -c 0 -d 0 -n 31\n\n");
+	printf("    %s, %-25s %s\n", "-v", "--vc", "dump sdma vc channel dfx reg\n");
+	printf("\tParameter Limitation: All three parameters are necessary,");
+	printf(" the -n --chnid range is limited to 0-159\n");
+	printf("\tUsage: -v -c [chipid] -d [dieid] -n [chnid], e.g. -v -c 0 -d 0 -n 159\n\n");
+	printf("\n");
+
+	return 0;
+}
+
+static int sdma_set_id(struct major_cmd_ctrl *self, const char *argv, uint32_t *id)
+{
+	uint32_t val = 0;
+	int ret;
+
+	ret = string_toui(argv, &val);
+	if (ret) {
+		snprintf(self->err_str, sizeof(self->err_str), "Invalid id.");
+		self->err_no = ret;
+		return ret;
+	}
+	*id = val;
+	return ret;
+}
+
+static int sdma_set_chip_id(struct major_cmd_ctrl *self, const char *argv)
+{
+	return sdma_set_id(self, argv, &g_sdma_dump_cmd.chip_id);
+}
+
+static int sdma_set_die_id(struct major_cmd_ctrl *self, const char *argv)
+{
+	return sdma_set_id(self, argv, &g_sdma_dump_cmd.die_id);
+}
+
+static int sdma_set_chn_id(struct major_cmd_ctrl *self, const char *argv)
+{
+	return sdma_set_id(self, argv, &g_sdma_dump_cmd.chn_id);
+}
+
+static int sdma_dump_chn_status(struct major_cmd_ctrl *self, const char *argv)
+{
+	HIKP_SET_USED(self);
+	HIKP_SET_USED(argv);
+
+	g_sdma_dump_cmd.sdma_cmd_type = DUMP_CHN_STATUS;
+	return 0;
+}
+
+static int sdma_dump_chn_pc(struct major_cmd_ctrl *self, const char *argv)
+{
+	HIKP_SET_USED(self);
+	HIKP_SET_USED(argv);
+
+	g_sdma_dump_cmd.sdma_cmd_type = DUMP_CHN_PC;
+	return 0;
+}
+
+static int sdma_dump_chn_vc(struct major_cmd_ctrl *self, const char *argv)
+{
+	HIKP_SET_USED(self);
+	HIKP_SET_USED(argv);
+
+	g_sdma_dump_cmd.sdma_cmd_type = DUMP_CHN_VC;
+	return 0;
+}
+
+static int sdma_dump_excute_function_call(uint32_t cmd_type)
+{
+	if (cmd_type != DUMP_UNKNOWN)
+		return sdma_reg_dump(&g_sdma_dump_cmd);
+
+	return -EINVAL;
+}
+
+static void sdma_dump_execute(struct major_cmd_ctrl *self)
+{
+	int ret;
+	const char *suc_msg[] = {
+		"",
+		"sdma_dump_chn_status success.",
+		"sdma_dump_dfx_pc success.",
+		"sdma_dump_dfx_vc success."
+	};
+	const char *err_msg[] = {
+		"sdma_dump failed, unknown cmd type",
+		"sdma_dump_chn_status error.",
+		"sdma_dump_dfx_pc error.",
+		"sdma_dump_dfx_vc error."
+	};
+
+	ret = sdma_dump_excute_function_call(g_sdma_dump_cmd.sdma_cmd_type);
+	if (ret == 0)
+		printf("%s\n", suc_msg[g_sdma_dump_cmd.sdma_cmd_type]);
+	else {
+		snprintf(self->err_str, sizeof(self->err_str), "%s\n",
+			 err_msg[g_sdma_dump_cmd.sdma_cmd_type]);
+		self->err_no = ret;
+	}
+}
+
+static void cmd_sdma_dump_init(void)
+{
+	struct major_cmd_ctrl *major_cmd = get_major_cmd();
+
+	major_cmd->option_count = 0;
+	major_cmd->execute = sdma_dump_execute;
+
+	cmd_option_register("-c", "--chipid", true, sdma_set_chip_id);
+	cmd_option_register("-d", "--dieid", true, sdma_set_die_id);
+	cmd_option_register("-n", "--chnid", true, sdma_set_chn_id);
+	cmd_option_register("-h", "--help", false, sdma_dump_help);
+	cmd_option_register("-s", "--chnstatus", false, sdma_dump_chn_status);
+	cmd_option_register("-p", "--pc", false, sdma_dump_chn_pc);
+	cmd_option_register("-v", "--vc", false, sdma_dump_chn_vc);
+}
+
+HIKP_CMD_DECLARE("sdma_dump", "sdma reg dump", cmd_sdma_dump_init);
diff --git a/sdma/user_cmd/sdma_tools_include.h b/sdma/user_cmd/sdma_tools_include.h
new file mode 100644
index 0000000..01b24b5
--- /dev/null
+++ b/sdma/user_cmd/sdma_tools_include.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2025 Hisilicon Technologies Co., Ltd.
+ * Hikptool is licensed under Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ *          http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
+ * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
+ * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
+ *
+ * See the Mulan PSL v2 for more details.
+ */
+
+#ifndef SDMA_TOOLS_INCLUDE_H
+#define SDMA_TOOLS_INCLUDE_H
+
+struct tool_sdma_cmd {
+	uint32_t sdma_cmd_type;
+	uint32_t chip_id;
+	uint32_t die_id;
+	uint32_t chn_id;
+};
+
+#endif /* SDMA_TOOLS_INCLUDE_H */
-- 
2.45.0.windows.1

