1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 HiSilicon Limited. */
3 #include <linux/hisi_acc_qm.h>
6 #define QM_DFX_BASE 0x0100000
7 #define QM_DFX_STATE1 0x0104000
8 #define QM_DFX_STATE2 0x01040C8
9 #define QM_DFX_COMMON 0x0000
10 #define QM_DFX_BASE_LEN 0x5A
11 #define QM_DFX_STATE1_LEN 0x2E
12 #define QM_DFX_STATE2_LEN 0x11
13 #define QM_DFX_COMMON_LEN 0xC3
14 #define QM_DFX_REGS_LEN 4UL
15 #define QM_DBG_TMP_BUF_LEN 22
16 #define QM_XQC_ADDR_MASK GENMASK(31, 0)
17 #define CURRENT_FUN_MASK GENMASK(5, 0)
18 #define CURRENT_Q_MASK GENMASK(31, 16)
19 #define QM_SQE_ADDR_MASK GENMASK(7, 0)
21 #define QM_DFX_MB_CNT_VF 0x104010
22 #define QM_DFX_DB_CNT_VF 0x104020
23 #define QM_DFX_SQE_CNT_VF_SQN 0x104030
24 #define QM_DFX_CQE_CNT_VF_CQN 0x104040
25 #define QM_DFX_QN_SHIFT 16
26 #define QM_DFX_CNT_CLR_CE 0x100118
27 #define QM_DBG_WRITE_LEN 1024
28 #define QM_IN_IDLE_ST_REG 0x1040e4
29 #define QM_IN_IDLE_STATE 0x1
31 static const char * const qm_debug_file_name
[] = {
32 [CURRENT_QM
] = "current_qm",
33 [CURRENT_Q
] = "current_q",
34 [CLEAR_ENABLE
] = "clear_enable",
37 static const char * const qm_s
[] = {
46 struct qm_cmd_dump_item
{
49 int (*dump_fn
)(struct hisi_qm
*qm
, char *cmd
, char *info_name
);
52 static struct qm_dfx_item qm_dfx_files
[] = {
53 {"err_irq", offsetof(struct qm_dfx
, err_irq_cnt
)},
54 {"aeq_irq", offsetof(struct qm_dfx
, aeq_irq_cnt
)},
55 {"abnormal_irq", offsetof(struct qm_dfx
, abnormal_irq_cnt
)},
56 {"create_qp_err", offsetof(struct qm_dfx
, create_qp_err_cnt
)},
57 {"mb_err", offsetof(struct qm_dfx
, mb_err_cnt
)},
60 #define CNT_CYC_REGS_NUM 10
61 static const struct debugfs_reg32 qm_dfx_regs
[] = {
62 /* XXX_CNT are reading clear register */
63 {"QM_ECC_1BIT_CNT ", 0x104000},
64 {"QM_ECC_MBIT_CNT ", 0x104008},
65 {"QM_DFX_MB_CNT ", 0x104018},
66 {"QM_DFX_DB_CNT ", 0x104028},
67 {"QM_DFX_SQE_CNT ", 0x104038},
68 {"QM_DFX_CQE_CNT ", 0x104048},
69 {"QM_DFX_SEND_SQE_TO_ACC_CNT ", 0x104050},
70 {"QM_DFX_WB_SQE_FROM_ACC_CNT ", 0x104058},
71 {"QM_DFX_ACC_FINISH_CNT ", 0x104060},
72 {"QM_DFX_CQE_ERR_CNT ", 0x1040b4},
73 {"QM_DFX_FUNS_ACTIVE_ST ", 0x200},
74 {"QM_ECC_1BIT_INF ", 0x104004},
75 {"QM_ECC_MBIT_INF ", 0x10400c},
76 {"QM_DFX_ACC_RDY_VLD0 ", 0x1040a0},
77 {"QM_DFX_ACC_RDY_VLD1 ", 0x1040a4},
78 {"QM_DFX_AXI_RDY_VLD ", 0x1040a8},
79 {"QM_DFX_FF_ST0 ", 0x1040c8},
80 {"QM_DFX_FF_ST1 ", 0x1040cc},
81 {"QM_DFX_FF_ST2 ", 0x1040d0},
82 {"QM_DFX_FF_ST3 ", 0x1040d4},
83 {"QM_DFX_FF_ST4 ", 0x1040d8},
84 {"QM_DFX_FF_ST5 ", 0x1040dc},
85 {"QM_DFX_FF_ST6 ", 0x1040e0},
86 {"QM_IN_IDLE_ST ", 0x1040e4},
87 {"QM_CACHE_CTL ", 0x100050},
88 {"QM_TIMEOUT_CFG ", 0x100070},
89 {"QM_DB_TIMEOUT_CFG ", 0x100074},
90 {"QM_FLR_PENDING_TIME_CFG ", 0x100078},
91 {"QM_ARUSR_MCFG1 ", 0x100088},
92 {"QM_AWUSR_MCFG1 ", 0x100098},
93 {"QM_AXI_M_CFG_ENABLE ", 0x1000B0},
94 {"QM_RAS_CE_THRESHOLD ", 0x1000F8},
95 {"QM_AXI_TIMEOUT_CTRL ", 0x100120},
96 {"QM_AXI_TIMEOUT_STATUS ", 0x100124},
97 {"QM_CQE_AGGR_TIMEOUT_CTRL ", 0x100144},
98 {"ACC_RAS_MSI_INT_SEL ", 0x1040fc},
99 {"QM_CQE_OUT ", 0x104100},
100 {"QM_EQE_OUT ", 0x104104},
101 {"QM_AEQE_OUT ", 0x104108},
102 {"QM_DB_INFO0 ", 0x104180},
103 {"QM_DB_INFO1 ", 0x104184},
104 {"QM_AM_CTRL_GLOBAL ", 0x300000},
105 {"QM_AM_CURR_PORT_STS ", 0x300100},
106 {"QM_AM_CURR_TRANS_RETURN ", 0x300150},
107 {"QM_AM_CURR_RD_MAX_TXID ", 0x300154},
108 {"QM_AM_CURR_WR_MAX_TXID ", 0x300158},
109 {"QM_AM_ALARM_RRESP ", 0x300180},
110 {"QM_AM_ALARM_BRESP ", 0x300184},
113 static const struct debugfs_reg32 qm_vf_dfx_regs
[] = {
114 {"QM_DFX_FUNS_ACTIVE_ST ", 0x200},
117 /* define the QM's dfx regs region and region length */
118 static struct dfx_diff_registers qm_diff_regs
[] = {
120 .reg_offset
= QM_DFX_BASE
,
121 .reg_len
= QM_DFX_BASE_LEN
,
123 .reg_offset
= QM_DFX_STATE1
,
124 .reg_len
= QM_DFX_STATE1_LEN
,
126 .reg_offset
= QM_DFX_STATE2
,
127 .reg_len
= QM_DFX_STATE2_LEN
,
129 .reg_offset
= QM_DFX_COMMON
,
130 .reg_len
= QM_DFX_COMMON_LEN
,
134 static struct hisi_qm
*file_to_qm(struct debugfs_file
*file
)
136 struct qm_debug
*debug
= file
->debug
;
138 return container_of(debug
, struct hisi_qm
, debug
);
141 static ssize_t
qm_cmd_read(struct file
*filp
, char __user
*buffer
,
142 size_t count
, loff_t
*pos
)
144 char buf
[QM_DBG_READ_LEN
];
147 len
= scnprintf(buf
, QM_DBG_READ_LEN
, "%s\n",
148 "Please echo help to cmd to get help information");
150 return simple_read_from_buffer(buffer
, count
, pos
, buf
, len
);
153 static void dump_show(struct hisi_qm
*qm
, void *info
,
154 unsigned int info_size
, char *info_name
)
156 struct device
*dev
= &qm
->pdev
->dev
;
157 u8
*info_curr
= info
;
159 #define BYTE_PER_DW 4
161 dev_info(dev
, "%s DUMP\n", info_name
);
162 for (i
= 0; i
< info_size
; i
+= BYTE_PER_DW
, info_curr
+= BYTE_PER_DW
) {
163 pr_info("DW%u: %02X%02X %02X%02X\n", i
/ BYTE_PER_DW
,
164 *(info_curr
+ 3), *(info_curr
+ 2), *(info_curr
+ 1), *(info_curr
));
168 static int qm_sqc_dump(struct hisi_qm
*qm
, char *s
, char *name
)
170 struct device
*dev
= &qm
->pdev
->dev
;
178 ret
= kstrtou32(s
, 0, &qp_id
);
179 if (ret
|| qp_id
>= qm
->qp_num
) {
180 dev_err(dev
, "Please input qp num (0-%u)", qm
->qp_num
- 1);
184 ret
= qm_set_and_get_xqc(qm
, QM_MB_CMD_SQC
, &sqc
, qp_id
, 1);
186 sqc
.base_h
= cpu_to_le32(QM_XQC_ADDR_MASK
);
187 sqc
.base_l
= cpu_to_le32(QM_XQC_ADDR_MASK
);
188 dump_show(qm
, &sqc
, sizeof(struct qm_sqc
), name
);
193 down_read(&qm
->qps_lock
);
195 memcpy(&sqc
, qm
->sqc
+ qp_id
* sizeof(struct qm_sqc
), sizeof(struct qm_sqc
));
196 sqc
.base_h
= cpu_to_le32(QM_XQC_ADDR_MASK
);
197 sqc
.base_l
= cpu_to_le32(QM_XQC_ADDR_MASK
);
198 dump_show(qm
, &sqc
, sizeof(struct qm_sqc
), "SOFT SQC");
200 up_read(&qm
->qps_lock
);
205 static int qm_cqc_dump(struct hisi_qm
*qm
, char *s
, char *name
)
207 struct device
*dev
= &qm
->pdev
->dev
;
215 ret
= kstrtou32(s
, 0, &qp_id
);
216 if (ret
|| qp_id
>= qm
->qp_num
) {
217 dev_err(dev
, "Please input qp num (0-%u)", qm
->qp_num
- 1);
221 ret
= qm_set_and_get_xqc(qm
, QM_MB_CMD_CQC
, &cqc
, qp_id
, 1);
223 cqc
.base_h
= cpu_to_le32(QM_XQC_ADDR_MASK
);
224 cqc
.base_l
= cpu_to_le32(QM_XQC_ADDR_MASK
);
225 dump_show(qm
, &cqc
, sizeof(struct qm_cqc
), name
);
230 down_read(&qm
->qps_lock
);
232 memcpy(&cqc
, qm
->cqc
+ qp_id
* sizeof(struct qm_cqc
), sizeof(struct qm_cqc
));
233 cqc
.base_h
= cpu_to_le32(QM_XQC_ADDR_MASK
);
234 cqc
.base_l
= cpu_to_le32(QM_XQC_ADDR_MASK
);
235 dump_show(qm
, &cqc
, sizeof(struct qm_cqc
), "SOFT CQC");
237 up_read(&qm
->qps_lock
);
242 static int qm_eqc_aeqc_dump(struct hisi_qm
*qm
, char *s
, char *name
)
244 struct device
*dev
= &qm
->pdev
->dev
;
252 if (strsep(&s
, " ")) {
253 dev_err(dev
, "Please do not input extra characters!\n");
257 if (!strcmp(name
, "EQC")) {
259 size
= sizeof(struct qm_eqc
);
262 cmd
= QM_MB_CMD_AEQC
;
263 size
= sizeof(struct qm_aeqc
);
267 ret
= qm_set_and_get_xqc(qm
, cmd
, xeqc
, 0, 1);
271 aeqc
.base_h
= cpu_to_le32(QM_XQC_ADDR_MASK
);
272 aeqc
.base_l
= cpu_to_le32(QM_XQC_ADDR_MASK
);
273 eqc
.base_h
= cpu_to_le32(QM_XQC_ADDR_MASK
);
274 eqc
.base_l
= cpu_to_le32(QM_XQC_ADDR_MASK
);
275 dump_show(qm
, xeqc
, size
, name
);
280 static int q_dump_param_parse(struct hisi_qm
*qm
, char *s
,
281 u32
*e_id
, u32
*q_id
, u16 q_depth
)
283 struct device
*dev
= &qm
->pdev
->dev
;
284 unsigned int qp_num
= qm
->qp_num
;
288 presult
= strsep(&s
, " ");
290 dev_err(dev
, "Please input qp number!\n");
294 ret
= kstrtou32(presult
, 0, q_id
);
295 if (ret
|| *q_id
>= qp_num
) {
296 dev_err(dev
, "Please input qp num (0-%u)", qp_num
- 1);
300 presult
= strsep(&s
, " ");
302 dev_err(dev
, "Please input sqe number!\n");
306 ret
= kstrtou32(presult
, 0, e_id
);
307 if (ret
|| *e_id
>= q_depth
) {
308 dev_err(dev
, "Please input sqe num (0-%u)", q_depth
- 1);
312 if (strsep(&s
, " ")) {
313 dev_err(dev
, "Please do not input extra characters!\n");
320 static int qm_sq_dump(struct hisi_qm
*qm
, char *s
, char *name
)
322 u16 sq_depth
= qm
->qp_array
->sq_depth
;
328 ret
= q_dump_param_parse(qm
, s
, &sqe_id
, &qp_id
, sq_depth
);
332 sqe
= kzalloc(qm
->sqe_size
, GFP_KERNEL
);
336 qp
= &qm
->qp_array
[qp_id
];
337 memcpy(sqe
, qp
->sqe
+ sqe_id
* qm
->sqe_size
, qm
->sqe_size
);
338 memset(sqe
+ qm
->debug
.sqe_mask_offset
, QM_SQE_ADDR_MASK
,
339 qm
->debug
.sqe_mask_len
);
341 dump_show(qm
, sqe
, qm
->sqe_size
, name
);
348 static int qm_cq_dump(struct hisi_qm
*qm
, char *s
, char *name
)
350 struct qm_cqe
*cqe_curr
;
355 ret
= q_dump_param_parse(qm
, s
, &cqe_id
, &qp_id
, qm
->qp_array
->cq_depth
);
359 qp
= &qm
->qp_array
[qp_id
];
360 cqe_curr
= qp
->cqe
+ cqe_id
;
361 dump_show(qm
, cqe_curr
, sizeof(struct qm_cqe
), name
);
366 static int qm_eq_aeq_dump(struct hisi_qm
*qm
, char *s
, char *name
)
368 struct device
*dev
= &qm
->pdev
->dev
;
378 ret
= kstrtou32(s
, 0, &xeqe_id
);
382 if (!strcmp(name
, "EQE")) {
383 xeq_depth
= qm
->eq_depth
;
384 size
= sizeof(struct qm_eqe
);
386 xeq_depth
= qm
->aeq_depth
;
387 size
= sizeof(struct qm_aeqe
);
390 if (xeqe_id
>= xeq_depth
) {
391 dev_err(dev
, "Please input eqe or aeqe num (0-%u)", xeq_depth
- 1);
395 down_read(&qm
->qps_lock
);
397 if (qm
->eqe
&& !strcmp(name
, "EQE")) {
398 xeqe
= qm
->eqe
+ xeqe_id
;
399 } else if (qm
->aeqe
&& !strcmp(name
, "AEQE")) {
400 xeqe
= qm
->aeqe
+ xeqe_id
;
406 dump_show(qm
, xeqe
, size
, name
);
409 up_read(&qm
->qps_lock
);
413 static int qm_dbg_help(struct hisi_qm
*qm
, char *s
)
415 struct device
*dev
= &qm
->pdev
->dev
;
417 if (strsep(&s
, " ")) {
418 dev_err(dev
, "Please do not input extra characters!\n");
422 dev_info(dev
, "available commands:\n");
423 dev_info(dev
, "sqc <num>\n");
424 dev_info(dev
, "cqc <num>\n");
425 dev_info(dev
, "eqc\n");
426 dev_info(dev
, "aeqc\n");
427 dev_info(dev
, "sq <num> <e>\n");
428 dev_info(dev
, "cq <num> <e>\n");
429 dev_info(dev
, "eq <e>\n");
430 dev_info(dev
, "aeq <e>\n");
435 static const struct qm_cmd_dump_item qm_cmd_dump_table
[] = {
439 .dump_fn
= qm_sqc_dump
,
443 .dump_fn
= qm_cqc_dump
,
447 .dump_fn
= qm_eqc_aeqc_dump
,
451 .dump_fn
= qm_eqc_aeqc_dump
,
455 .dump_fn
= qm_sq_dump
,
459 .dump_fn
= qm_cq_dump
,
463 .dump_fn
= qm_eq_aeq_dump
,
467 .dump_fn
= qm_eq_aeq_dump
,
471 static int qm_cmd_write_dump(struct hisi_qm
*qm
, const char *cmd_buf
)
473 struct device
*dev
= &qm
->pdev
->dev
;
474 char *presult
, *s
, *s_tmp
;
475 int table_size
, i
, ret
;
477 s
= kstrdup(cmd_buf
, GFP_KERNEL
);
482 presult
= strsep(&s
, " ");
485 goto err_buffer_free
;
488 if (!strcmp(presult
, "help")) {
489 ret
= qm_dbg_help(qm
, s
);
490 goto err_buffer_free
;
493 table_size
= ARRAY_SIZE(qm_cmd_dump_table
);
494 for (i
= 0; i
< table_size
; i
++) {
495 if (!strcmp(presult
, qm_cmd_dump_table
[i
].cmd
)) {
496 ret
= qm_cmd_dump_table
[i
].dump_fn(qm
, s
,
497 qm_cmd_dump_table
[i
].info_name
);
502 if (i
== table_size
) {
503 dev_info(dev
, "Please echo help\n");
513 static ssize_t
qm_cmd_write(struct file
*filp
, const char __user
*buffer
,
514 size_t count
, loff_t
*pos
)
516 struct hisi_qm
*qm
= filp
->private_data
;
517 char *cmd_buf
, *cmd_buf_tmp
;
523 ret
= hisi_qm_get_dfx_access(qm
);
527 /* Judge if the instance is being reset. */
528 if (unlikely(atomic_read(&qm
->status
.flags
) == QM_STOP
)) {
533 if (count
> QM_DBG_WRITE_LEN
) {
538 cmd_buf
= memdup_user_nul(buffer
, count
);
539 if (IS_ERR(cmd_buf
)) {
540 ret
= PTR_ERR(cmd_buf
);
544 cmd_buf_tmp
= strchr(cmd_buf
, '\n');
547 count
= cmd_buf_tmp
- cmd_buf
+ 1;
550 ret
= qm_cmd_write_dump(qm
, cmd_buf
);
561 hisi_qm_put_dfx_access(qm
);
565 static const struct file_operations qm_cmd_fops
= {
566 .owner
= THIS_MODULE
,
569 .write
= qm_cmd_write
,
573 * hisi_qm_regs_dump() - Dump registers's value.
574 * @s: debugfs file handle.
575 * @regset: accelerator registers information.
577 * Dump accelerator registers.
579 void hisi_qm_regs_dump(struct seq_file
*s
, struct debugfs_regset32
*regset
)
581 struct pci_dev
*pdev
= to_pci_dev(regset
->dev
);
582 struct hisi_qm
*qm
= pci_get_drvdata(pdev
);
583 const struct debugfs_reg32
*regs
= regset
->regs
;
584 int regs_len
= regset
->nregs
;
588 ret
= hisi_qm_get_dfx_access(qm
);
592 for (i
= 0; i
< regs_len
; i
++) {
593 val
= readl(regset
->base
+ regs
[i
].offset
);
594 seq_printf(s
, "%s= 0x%08x\n", regs
[i
].name
, val
);
597 hisi_qm_put_dfx_access(qm
);
599 EXPORT_SYMBOL_GPL(hisi_qm_regs_dump
);
601 static int qm_regs_show(struct seq_file
*s
, void *unused
)
603 struct hisi_qm
*qm
= s
->private;
604 struct debugfs_regset32 regset
;
606 if (qm
->fun_type
== QM_HW_PF
) {
607 regset
.regs
= qm_dfx_regs
;
608 regset
.nregs
= ARRAY_SIZE(qm_dfx_regs
);
610 regset
.regs
= qm_vf_dfx_regs
;
611 regset
.nregs
= ARRAY_SIZE(qm_vf_dfx_regs
);
614 regset
.base
= qm
->io_base
;
615 regset
.dev
= &qm
->pdev
->dev
;
617 hisi_qm_regs_dump(s
, ®set
);
622 DEFINE_SHOW_ATTRIBUTE(qm_regs
);
624 static u32
current_q_read(struct hisi_qm
*qm
)
626 return readl(qm
->io_base
+ QM_DFX_SQE_CNT_VF_SQN
) >> QM_DFX_QN_SHIFT
;
629 static int current_q_write(struct hisi_qm
*qm
, u32 val
)
633 if (val
>= qm
->debug
.curr_qm_qp_num
)
636 tmp
= val
<< QM_DFX_QN_SHIFT
|
637 (readl(qm
->io_base
+ QM_DFX_SQE_CNT_VF_SQN
) & CURRENT_FUN_MASK
);
638 writel(tmp
, qm
->io_base
+ QM_DFX_SQE_CNT_VF_SQN
);
640 tmp
= val
<< QM_DFX_QN_SHIFT
|
641 (readl(qm
->io_base
+ QM_DFX_CQE_CNT_VF_CQN
) & CURRENT_FUN_MASK
);
642 writel(tmp
, qm
->io_base
+ QM_DFX_CQE_CNT_VF_CQN
);
647 static u32
clear_enable_read(struct hisi_qm
*qm
)
649 return readl(qm
->io_base
+ QM_DFX_CNT_CLR_CE
);
652 /* rd_clr_ctrl 1 enable read clear, otherwise 0 disable it */
653 static int clear_enable_write(struct hisi_qm
*qm
, u32 rd_clr_ctrl
)
658 writel(rd_clr_ctrl
, qm
->io_base
+ QM_DFX_CNT_CLR_CE
);
663 static u32
current_qm_read(struct hisi_qm
*qm
)
665 return readl(qm
->io_base
+ QM_DFX_MB_CNT_VF
);
668 static int qm_get_vf_qp_num(struct hisi_qm
*qm
, u32 fun_num
)
670 u32 remain_q_num
, vfq_num
;
671 u32 num_vfs
= qm
->vfs_num
;
673 vfq_num
= (qm
->ctrl_qp_num
- qm
->qp_num
) / num_vfs
;
674 if (vfq_num
>= qm
->max_qp_num
)
675 return qm
->max_qp_num
;
677 remain_q_num
= (qm
->ctrl_qp_num
- qm
->qp_num
) % num_vfs
;
678 if (vfq_num
+ remain_q_num
<= qm
->max_qp_num
)
679 return fun_num
== num_vfs
? vfq_num
+ remain_q_num
: vfq_num
;
682 * if vfq_num + remain_q_num > max_qp_num, the last VFs,
683 * each with one more queue.
685 return fun_num
+ remain_q_num
> num_vfs
? vfq_num
+ 1 : vfq_num
;
688 static int current_qm_write(struct hisi_qm
*qm
, u32 val
)
692 if (val
> qm
->vfs_num
)
695 /* According PF or VF Dev ID to calculation curr_qm_qp_num and store */
697 qm
->debug
.curr_qm_qp_num
= qm
->qp_num
;
699 qm
->debug
.curr_qm_qp_num
= qm_get_vf_qp_num(qm
, val
);
701 writel(val
, qm
->io_base
+ QM_DFX_MB_CNT_VF
);
702 writel(val
, qm
->io_base
+ QM_DFX_DB_CNT_VF
);
705 (readl(qm
->io_base
+ QM_DFX_SQE_CNT_VF_SQN
) & CURRENT_Q_MASK
);
706 writel(tmp
, qm
->io_base
+ QM_DFX_SQE_CNT_VF_SQN
);
709 (readl(qm
->io_base
+ QM_DFX_CQE_CNT_VF_CQN
) & CURRENT_Q_MASK
);
710 writel(tmp
, qm
->io_base
+ QM_DFX_CQE_CNT_VF_CQN
);
715 static ssize_t
qm_debug_read(struct file
*filp
, char __user
*buf
,
716 size_t count
, loff_t
*pos
)
718 struct debugfs_file
*file
= filp
->private_data
;
719 enum qm_debug_file index
= file
->index
;
720 struct hisi_qm
*qm
= file_to_qm(file
);
721 char tbuf
[QM_DBG_TMP_BUF_LEN
];
725 ret
= hisi_qm_get_dfx_access(qm
);
729 mutex_lock(&file
->lock
);
732 val
= current_qm_read(qm
);
735 val
= current_q_read(qm
);
738 val
= clear_enable_read(qm
);
743 mutex_unlock(&file
->lock
);
745 hisi_qm_put_dfx_access(qm
);
746 ret
= scnprintf(tbuf
, QM_DBG_TMP_BUF_LEN
, "%u\n", val
);
747 return simple_read_from_buffer(buf
, count
, pos
, tbuf
, ret
);
750 mutex_unlock(&file
->lock
);
751 hisi_qm_put_dfx_access(qm
);
755 static ssize_t
qm_debug_write(struct file
*filp
, const char __user
*buf
,
756 size_t count
, loff_t
*pos
)
758 struct debugfs_file
*file
= filp
->private_data
;
759 enum qm_debug_file index
= file
->index
;
760 struct hisi_qm
*qm
= file_to_qm(file
);
762 char tbuf
[QM_DBG_TMP_BUF_LEN
];
768 if (count
>= QM_DBG_TMP_BUF_LEN
)
771 len
= simple_write_to_buffer(tbuf
, QM_DBG_TMP_BUF_LEN
- 1, pos
, buf
,
777 if (kstrtoul(tbuf
, 0, &val
))
780 ret
= hisi_qm_get_dfx_access(qm
);
784 mutex_lock(&file
->lock
);
787 ret
= current_qm_write(qm
, val
);
790 ret
= current_q_write(qm
, val
);
793 ret
= clear_enable_write(qm
, val
);
798 mutex_unlock(&file
->lock
);
800 hisi_qm_put_dfx_access(qm
);
808 static const struct file_operations qm_debug_fops
= {
809 .owner
= THIS_MODULE
,
811 .read
= qm_debug_read
,
812 .write
= qm_debug_write
,
815 static void dfx_regs_uninit(struct hisi_qm
*qm
,
816 struct dfx_diff_registers
*dregs
, int reg_len
)
823 /* Setting the pointer is NULL to prevent double free */
824 for (i
= 0; i
< reg_len
; i
++) {
828 kfree(dregs
[i
].regs
);
829 dregs
[i
].regs
= NULL
;
834 static struct dfx_diff_registers
*dfx_regs_init(struct hisi_qm
*qm
,
835 const struct dfx_diff_registers
*cregs
, u32 reg_len
)
837 struct dfx_diff_registers
*diff_regs
;
841 diff_regs
= kcalloc(reg_len
, sizeof(*diff_regs
), GFP_KERNEL
);
843 return ERR_PTR(-ENOMEM
);
845 for (i
= 0; i
< reg_len
; i
++) {
846 if (!cregs
[i
].reg_len
)
849 diff_regs
[i
].reg_offset
= cregs
[i
].reg_offset
;
850 diff_regs
[i
].reg_len
= cregs
[i
].reg_len
;
851 diff_regs
[i
].regs
= kcalloc(QM_DFX_REGS_LEN
, cregs
[i
].reg_len
,
853 if (!diff_regs
[i
].regs
)
856 for (j
= 0; j
< diff_regs
[i
].reg_len
; j
++) {
857 base_offset
= diff_regs
[i
].reg_offset
+
859 diff_regs
[i
].regs
[j
] = readl(qm
->io_base
+ base_offset
);
868 kfree(diff_regs
[i
].regs
);
871 return ERR_PTR(-ENOMEM
);
874 static int qm_diff_regs_init(struct hisi_qm
*qm
,
875 struct dfx_diff_registers
*dregs
, u32 reg_len
)
879 qm
->debug
.qm_diff_regs
= dfx_regs_init(qm
, qm_diff_regs
, ARRAY_SIZE(qm_diff_regs
));
880 if (IS_ERR(qm
->debug
.qm_diff_regs
)) {
881 ret
= PTR_ERR(qm
->debug
.qm_diff_regs
);
882 qm
->debug
.qm_diff_regs
= NULL
;
886 qm
->debug
.acc_diff_regs
= dfx_regs_init(qm
, dregs
, reg_len
);
887 if (IS_ERR(qm
->debug
.acc_diff_regs
)) {
888 dfx_regs_uninit(qm
, qm
->debug
.qm_diff_regs
, ARRAY_SIZE(qm_diff_regs
));
889 ret
= PTR_ERR(qm
->debug
.acc_diff_regs
);
890 qm
->debug
.acc_diff_regs
= NULL
;
897 static void qm_last_regs_uninit(struct hisi_qm
*qm
)
899 struct qm_debug
*debug
= &qm
->debug
;
901 if (qm
->fun_type
== QM_HW_VF
|| !debug
->qm_last_words
)
904 kfree(debug
->qm_last_words
);
905 debug
->qm_last_words
= NULL
;
908 static int qm_last_regs_init(struct hisi_qm
*qm
)
910 int dfx_regs_num
= ARRAY_SIZE(qm_dfx_regs
);
911 struct qm_debug
*debug
= &qm
->debug
;
914 if (qm
->fun_type
== QM_HW_VF
)
917 debug
->qm_last_words
= kcalloc(dfx_regs_num
, sizeof(unsigned int), GFP_KERNEL
);
918 if (!debug
->qm_last_words
)
921 for (i
= 0; i
< dfx_regs_num
; i
++) {
922 debug
->qm_last_words
[i
] = readl_relaxed(qm
->io_base
+
923 qm_dfx_regs
[i
].offset
);
929 static void qm_diff_regs_uninit(struct hisi_qm
*qm
, u32 reg_len
)
931 dfx_regs_uninit(qm
, qm
->debug
.acc_diff_regs
, reg_len
);
932 qm
->debug
.acc_diff_regs
= NULL
;
933 dfx_regs_uninit(qm
, qm
->debug
.qm_diff_regs
, ARRAY_SIZE(qm_diff_regs
));
934 qm
->debug
.qm_diff_regs
= NULL
;
938 * hisi_qm_regs_debugfs_init() - Allocate memory for registers.
939 * @qm: device qm handle.
940 * @dregs: diff registers handle.
941 * @reg_len: diff registers region length.
943 int hisi_qm_regs_debugfs_init(struct hisi_qm
*qm
,
944 struct dfx_diff_registers
*dregs
, u32 reg_len
)
951 if (qm
->fun_type
!= QM_HW_PF
)
954 ret
= qm_last_regs_init(qm
);
956 dev_info(&qm
->pdev
->dev
, "failed to init qm words memory!\n");
960 ret
= qm_diff_regs_init(qm
, dregs
, reg_len
);
962 qm_last_regs_uninit(qm
);
968 EXPORT_SYMBOL_GPL(hisi_qm_regs_debugfs_init
);
971 * hisi_qm_regs_debugfs_uninit() - Free memory for registers.
972 * @qm: device qm handle.
973 * @reg_len: diff registers region length.
975 void hisi_qm_regs_debugfs_uninit(struct hisi_qm
*qm
, u32 reg_len
)
977 if (!qm
|| qm
->fun_type
!= QM_HW_PF
)
980 qm_diff_regs_uninit(qm
, reg_len
);
981 qm_last_regs_uninit(qm
);
983 EXPORT_SYMBOL_GPL(hisi_qm_regs_debugfs_uninit
);
986 * hisi_qm_acc_diff_regs_dump() - Dump registers's value.
987 * @qm: device qm handle.
988 * @s: Debugfs file handle.
989 * @dregs: diff registers handle.
990 * @regs_len: diff registers region length.
992 void hisi_qm_acc_diff_regs_dump(struct hisi_qm
*qm
, struct seq_file
*s
,
993 struct dfx_diff_registers
*dregs
, u32 regs_len
)
995 u32 j
, val
, base_offset
;
998 if (!qm
|| !s
|| !dregs
)
1001 ret
= hisi_qm_get_dfx_access(qm
);
1005 down_read(&qm
->qps_lock
);
1006 for (i
= 0; i
< regs_len
; i
++) {
1007 if (!dregs
[i
].reg_len
)
1010 for (j
= 0; j
< dregs
[i
].reg_len
; j
++) {
1011 base_offset
= dregs
[i
].reg_offset
+ j
* QM_DFX_REGS_LEN
;
1012 val
= readl(qm
->io_base
+ base_offset
);
1013 if (val
!= dregs
[i
].regs
[j
])
1014 seq_printf(s
, "0x%08x = 0x%08x ---> 0x%08x\n",
1015 base_offset
, dregs
[i
].regs
[j
], val
);
1018 up_read(&qm
->qps_lock
);
1020 hisi_qm_put_dfx_access(qm
);
1022 EXPORT_SYMBOL_GPL(hisi_qm_acc_diff_regs_dump
);
1024 void hisi_qm_show_last_dfx_regs(struct hisi_qm
*qm
)
1026 struct qm_debug
*debug
= &qm
->debug
;
1027 struct pci_dev
*pdev
= qm
->pdev
;
1031 if (qm
->fun_type
== QM_HW_VF
|| !debug
->qm_last_words
)
1034 for (i
= 0; i
< ARRAY_SIZE(qm_dfx_regs
); i
++) {
1035 val
= readl_relaxed(qm
->io_base
+ qm_dfx_regs
[i
].offset
);
1036 if (debug
->qm_last_words
[i
] != val
)
1037 pci_info(pdev
, "%s \t= 0x%08x => 0x%08x\n",
1038 qm_dfx_regs
[i
].name
, debug
->qm_last_words
[i
], val
);
1042 static int qm_diff_regs_show(struct seq_file
*s
, void *unused
)
1044 struct hisi_qm
*qm
= s
->private;
1046 hisi_qm_acc_diff_regs_dump(qm
, s
, qm
->debug
.qm_diff_regs
,
1047 ARRAY_SIZE(qm_diff_regs
));
1051 DEFINE_SHOW_ATTRIBUTE(qm_diff_regs
);
1053 static int qm_state_show(struct seq_file
*s
, void *unused
)
1055 struct hisi_qm
*qm
= s
->private;
1059 /* If device is in suspended, directly return the idle state. */
1060 ret
= hisi_qm_get_dfx_access(qm
);
1062 val
= readl(qm
->io_base
+ QM_IN_IDLE_ST_REG
);
1063 hisi_qm_put_dfx_access(qm
);
1064 } else if (ret
== -EAGAIN
) {
1065 val
= QM_IN_IDLE_STATE
;
1070 seq_printf(s
, "%u\n", val
);
1075 DEFINE_SHOW_ATTRIBUTE(qm_state
);
1077 static ssize_t
qm_status_read(struct file
*filp
, char __user
*buffer
,
1078 size_t count
, loff_t
*pos
)
1080 struct hisi_qm
*qm
= filp
->private_data
;
1081 char buf
[QM_DBG_READ_LEN
];
1084 val
= atomic_read(&qm
->status
.flags
);
1085 len
= scnprintf(buf
, QM_DBG_READ_LEN
, "%s\n", qm_s
[val
]);
1087 return simple_read_from_buffer(buffer
, count
, pos
, buf
, len
);
1090 static const struct file_operations qm_status_fops
= {
1091 .owner
= THIS_MODULE
,
1092 .open
= simple_open
,
1093 .read
= qm_status_read
,
1096 static void qm_create_debugfs_file(struct hisi_qm
*qm
, struct dentry
*dir
,
1097 enum qm_debug_file index
)
1099 struct debugfs_file
*file
= qm
->debug
.files
+ index
;
1101 file
->index
= index
;
1102 mutex_init(&file
->lock
);
1103 file
->debug
= &qm
->debug
;
1105 debugfs_create_file(qm_debug_file_name
[index
], 0600, dir
, file
,
1109 static int qm_debugfs_atomic64_set(void *data
, u64 val
)
1114 atomic64_set((atomic64_t
*)data
, 0);
1119 static int qm_debugfs_atomic64_get(void *data
, u64
*val
)
1121 *val
= atomic64_read((atomic64_t
*)data
);
1126 DEFINE_DEBUGFS_ATTRIBUTE(qm_atomic64_ops
, qm_debugfs_atomic64_get
,
1127 qm_debugfs_atomic64_set
, "%llu\n");
1130 * hisi_qm_debug_init() - Initialize qm related debugfs files.
1131 * @qm: The qm for which we want to add debugfs files.
1133 * Create qm related debugfs files.
1135 void hisi_qm_debug_init(struct hisi_qm
*qm
)
1137 struct dfx_diff_registers
*qm_regs
= qm
->debug
.qm_diff_regs
;
1138 struct qm_dev_dfx
*dev_dfx
= &qm
->debug
.dev_dfx
;
1139 struct qm_dfx
*dfx
= &qm
->debug
.dfx
;
1140 struct dentry
*qm_d
;
1144 qm_d
= debugfs_create_dir("qm", qm
->debug
.debug_root
);
1145 qm
->debug
.qm_d
= qm_d
;
1147 /* only show this in PF */
1148 if (qm
->fun_type
== QM_HW_PF
) {
1149 debugfs_create_file("qm_state", 0444, qm
->debug
.qm_d
,
1150 qm
, &qm_state_fops
);
1152 qm_create_debugfs_file(qm
, qm
->debug
.debug_root
, CURRENT_QM
);
1153 for (i
= CURRENT_Q
; i
< DEBUG_FILE_NUM
; i
++)
1154 qm_create_debugfs_file(qm
, qm
->debug
.qm_d
, i
);
1158 debugfs_create_file("diff_regs", 0444, qm
->debug
.qm_d
,
1159 qm
, &qm_diff_regs_fops
);
1161 debugfs_create_file("regs", 0444, qm
->debug
.qm_d
, qm
, &qm_regs_fops
);
1163 debugfs_create_file("cmd", 0600, qm
->debug
.qm_d
, qm
, &qm_cmd_fops
);
1165 debugfs_create_file("status", 0444, qm
->debug
.qm_d
, qm
,
1168 debugfs_create_u32("dev_state", 0444, qm
->debug
.qm_d
, &dev_dfx
->dev_state
);
1169 debugfs_create_u32("dev_timeout", 0644, qm
->debug
.qm_d
, &dev_dfx
->dev_timeout
);
1171 for (i
= 0; i
< ARRAY_SIZE(qm_dfx_files
); i
++) {
1172 data
= (atomic64_t
*)((uintptr_t)dfx
+ qm_dfx_files
[i
].offset
);
1173 debugfs_create_file(qm_dfx_files
[i
].name
,
1180 if (test_bit(QM_SUPPORT_FUNC_QOS
, &qm
->caps
))
1181 hisi_qm_set_algqos_init(qm
);
1183 EXPORT_SYMBOL_GPL(hisi_qm_debug_init
);
1186 * hisi_qm_debug_regs_clear() - clear qm debug related registers.
1187 * @qm: The qm for which we want to clear its debug registers.
1189 void hisi_qm_debug_regs_clear(struct hisi_qm
*qm
)
1191 const struct debugfs_reg32
*regs
;
1194 /* clear current_qm */
1195 writel(0x0, qm
->io_base
+ QM_DFX_MB_CNT_VF
);
1196 writel(0x0, qm
->io_base
+ QM_DFX_DB_CNT_VF
);
1198 /* clear current_q */
1199 writel(0x0, qm
->io_base
+ QM_DFX_SQE_CNT_VF_SQN
);
1200 writel(0x0, qm
->io_base
+ QM_DFX_CQE_CNT_VF_CQN
);
1203 * these registers are reading and clearing, so clear them after
1206 writel(0x1, qm
->io_base
+ QM_DFX_CNT_CLR_CE
);
1209 for (i
= 0; i
< CNT_CYC_REGS_NUM
; i
++) {
1210 readl(qm
->io_base
+ regs
->offset
);
1214 /* clear clear_enable */
1215 writel(0x0, qm
->io_base
+ QM_DFX_CNT_CLR_CE
);
1217 EXPORT_SYMBOL_GPL(hisi_qm_debug_regs_clear
);