1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 HiSilicon Limited. */
4 #include <linux/acpi.h>
6 #include <linux/bitmap.h>
7 #include <linux/debugfs.h>
8 #include <linux/dma-mapping.h>
11 #include <linux/irqreturn.h>
12 #include <linux/log2.h>
13 #include <linux/seq_file.h>
14 #include <linux/slab.h>
15 #include <linux/uacce.h>
16 #include <linux/uaccess.h>
17 #include <uapi/misc/uacce/hisi_qm.h>
20 /* eq/aeq irq enable */
21 #define QM_VF_AEQ_INT_SOURCE 0x0
22 #define QM_VF_AEQ_INT_MASK 0x4
23 #define QM_VF_EQ_INT_SOURCE 0x8
24 #define QM_VF_EQ_INT_MASK 0xc
25 #define QM_IRQ_NUM_V1 1
26 #define QM_IRQ_NUM_PF_V2 4
27 #define QM_IRQ_NUM_VF_V2 2
29 #define QM_EQ_EVENT_IRQ_VECTOR 0
30 #define QM_AEQ_EVENT_IRQ_VECTOR 1
31 #define QM_ABNORMAL_EVENT_IRQ_VECTOR 3
34 #define QM_MB_CMD_SQC 0x0
35 #define QM_MB_CMD_CQC 0x1
36 #define QM_MB_CMD_EQC 0x2
37 #define QM_MB_CMD_AEQC 0x3
38 #define QM_MB_CMD_SQC_BT 0x4
39 #define QM_MB_CMD_CQC_BT 0x5
40 #define QM_MB_CMD_SQC_VFT_V2 0x6
42 #define QM_MB_CMD_SEND_BASE 0x300
43 #define QM_MB_EVENT_SHIFT 8
44 #define QM_MB_BUSY_SHIFT 13
45 #define QM_MB_OP_SHIFT 14
46 #define QM_MB_CMD_DATA_ADDR_L 0x304
47 #define QM_MB_CMD_DATA_ADDR_H 0x308
50 #define QM_SQ_HOP_NUM_SHIFT 0
51 #define QM_SQ_PAGE_SIZE_SHIFT 4
52 #define QM_SQ_BUF_SIZE_SHIFT 8
53 #define QM_SQ_SQE_SIZE_SHIFT 12
54 #define QM_SQ_PRIORITY_SHIFT 0
55 #define QM_SQ_ORDERS_SHIFT 4
56 #define QM_SQ_TYPE_SHIFT 8
58 #define QM_SQ_TYPE_MASK GENMASK(3, 0)
59 #define QM_SQ_TAIL_IDX(sqc) ((le16_to_cpu((sqc)->w11) >> 6) & 0x1)
62 #define QM_CQ_HOP_NUM_SHIFT 0
63 #define QM_CQ_PAGE_SIZE_SHIFT 4
64 #define QM_CQ_BUF_SIZE_SHIFT 8
65 #define QM_CQ_CQE_SIZE_SHIFT 12
66 #define QM_CQ_PHASE_SHIFT 0
67 #define QM_CQ_FLAG_SHIFT 1
69 #define QM_CQE_PHASE(cqe) (le16_to_cpu((cqe)->w7) & 0x1)
70 #define QM_QC_CQE_SIZE 4
71 #define QM_CQ_TAIL_IDX(cqc) ((le16_to_cpu((cqc)->w11) >> 6) & 0x1)
74 #define QM_EQE_AEQE_SIZE (2UL << 12)
75 #define QM_EQC_PHASE_SHIFT 16
77 #define QM_EQE_PHASE(eqe) ((le32_to_cpu((eqe)->dw0) >> 16) & 0x1)
78 #define QM_EQE_CQN_MASK GENMASK(15, 0)
80 #define QM_AEQE_PHASE(aeqe) ((le32_to_cpu((aeqe)->dw0) >> 16) & 0x1)
81 #define QM_AEQE_TYPE_SHIFT 17
83 #define QM_DOORBELL_CMD_SQ 0
84 #define QM_DOORBELL_CMD_CQ 1
85 #define QM_DOORBELL_CMD_EQ 2
86 #define QM_DOORBELL_CMD_AEQ 3
88 #define QM_DOORBELL_BASE_V1 0x340
89 #define QM_DB_CMD_SHIFT_V1 16
90 #define QM_DB_INDEX_SHIFT_V1 32
91 #define QM_DB_PRIORITY_SHIFT_V1 48
92 #define QM_DOORBELL_SQ_CQ_BASE_V2 0x1000
93 #define QM_DOORBELL_EQ_AEQ_BASE_V2 0x2000
94 #define QM_DB_CMD_SHIFT_V2 12
95 #define QM_DB_RAND_SHIFT_V2 16
96 #define QM_DB_INDEX_SHIFT_V2 32
97 #define QM_DB_PRIORITY_SHIFT_V2 48
99 #define QM_MEM_START_INIT 0x100040
100 #define QM_MEM_INIT_DONE 0x100044
101 #define QM_VFT_CFG_RDY 0x10006c
102 #define QM_VFT_CFG_OP_WR 0x100058
103 #define QM_VFT_CFG_TYPE 0x10005c
104 #define QM_SQC_VFT 0x0
105 #define QM_CQC_VFT 0x1
106 #define QM_VFT_CFG 0x100060
107 #define QM_VFT_CFG_OP_ENABLE 0x100054
109 #define QM_VFT_CFG_DATA_L 0x100064
110 #define QM_VFT_CFG_DATA_H 0x100068
111 #define QM_SQC_VFT_BUF_SIZE (7ULL << 8)
112 #define QM_SQC_VFT_SQC_SIZE (5ULL << 12)
113 #define QM_SQC_VFT_INDEX_NUMBER (1ULL << 16)
114 #define QM_SQC_VFT_START_SQN_SHIFT 28
115 #define QM_SQC_VFT_VALID (1ULL << 44)
116 #define QM_SQC_VFT_SQN_SHIFT 45
117 #define QM_CQC_VFT_BUF_SIZE (7ULL << 8)
118 #define QM_CQC_VFT_SQC_SIZE (5ULL << 12)
119 #define QM_CQC_VFT_INDEX_NUMBER (1ULL << 16)
120 #define QM_CQC_VFT_VALID (1ULL << 28)
122 #define QM_SQC_VFT_BASE_SHIFT_V2 28
123 #define QM_SQC_VFT_BASE_MASK_V2 GENMASK(5, 0)
124 #define QM_SQC_VFT_NUM_SHIFT_V2 45
125 #define QM_SQC_VFT_NUM_MASK_v2 GENMASK(9, 0)
127 #define QM_DFX_CNT_CLR_CE 0x100118
129 #define QM_ABNORMAL_INT_SOURCE 0x100000
130 #define QM_ABNORMAL_INT_SOURCE_CLR GENMASK(12, 0)
131 #define QM_ABNORMAL_INT_MASK 0x100004
132 #define QM_ABNORMAL_INT_MASK_VALUE 0x1fff
133 #define QM_ABNORMAL_INT_STATUS 0x100008
134 #define QM_ABNORMAL_INT_SET 0x10000c
135 #define QM_ABNORMAL_INF00 0x100010
136 #define QM_FIFO_OVERFLOW_TYPE 0xc0
137 #define QM_FIFO_OVERFLOW_TYPE_SHIFT 6
138 #define QM_FIFO_OVERFLOW_VF 0x3f
139 #define QM_ABNORMAL_INF01 0x100014
140 #define QM_DB_TIMEOUT_TYPE 0xc0
141 #define QM_DB_TIMEOUT_TYPE_SHIFT 6
142 #define QM_DB_TIMEOUT_VF 0x3f
143 #define QM_RAS_CE_ENABLE 0x1000ec
144 #define QM_RAS_FE_ENABLE 0x1000f0
145 #define QM_RAS_NFE_ENABLE 0x1000f4
146 #define QM_RAS_CE_THRESHOLD 0x1000f8
147 #define QM_RAS_CE_TIMES_PER_IRQ 1
148 #define QM_RAS_MSI_INT_SEL 0x1040f4
150 #define QM_DEV_RESET_FLAG 0
151 #define QM_RESET_WAIT_TIMEOUT 400
152 #define QM_PEH_VENDOR_ID 0x1000d8
153 #define ACC_VENDOR_ID_VALUE 0x5a5a
154 #define QM_PEH_DFX_INFO0 0x1000fc
155 #define ACC_PEH_SRIOV_CTRL_VF_MSE_SHIFT 3
156 #define ACC_PEH_MSI_DISABLE GENMASK(31, 0)
157 #define ACC_MASTER_GLOBAL_CTRL_SHUTDOWN 0x1
158 #define ACC_MASTER_TRANS_RETURN_RW 3
159 #define ACC_MASTER_TRANS_RETURN 0x300150
160 #define ACC_MASTER_GLOBAL_CTRL 0x300000
161 #define ACC_AM_CFG_PORT_WR_EN 0x30001c
162 #define QM_RAS_NFE_MBIT_DISABLE ~QM_ECC_MBIT
163 #define ACC_AM_ROB_ECC_INT_STS 0x300104
164 #define ACC_ROB_ECC_ERR_MULTPL BIT(1)
166 #define POLL_PERIOD 10
167 #define POLL_TIMEOUT 1000
168 #define WAIT_PERIOD_US_MAX 200
169 #define WAIT_PERIOD_US_MIN 100
170 #define MAX_WAIT_COUNTS 1000
171 #define QM_CACHE_WB_START 0x204
172 #define QM_CACHE_WB_DONE 0x208
175 #define QM_SQE_DATA_ALIGN_MASK GENMASK(6, 0)
176 #define QMC_ALIGN(sz) ALIGN(sz, 32)
178 #define QM_DBG_READ_LEN 256
179 #define QM_DBG_WRITE_LEN 1024
180 #define QM_DBG_TMP_BUF_LEN 22
181 #define QM_PCI_COMMAND_INVALID ~0
183 #define WAIT_PERIOD 20
184 #define REMOVE_WAIT_DELAY 10
185 #define QM_SQE_ADDR_MASK GENMASK(7, 0)
186 #define QM_EQ_DEPTH (1024 * 2)
188 #define QM_MK_CQC_DW3_V1(hop_num, pg_sz, buf_sz, cqe_sz) \
189 (((hop_num) << QM_CQ_HOP_NUM_SHIFT) | \
190 ((pg_sz) << QM_CQ_PAGE_SIZE_SHIFT) | \
191 ((buf_sz) << QM_CQ_BUF_SIZE_SHIFT) | \
192 ((cqe_sz) << QM_CQ_CQE_SIZE_SHIFT))
194 #define QM_MK_CQC_DW3_V2(cqe_sz) \
195 ((QM_Q_DEPTH - 1) | ((cqe_sz) << QM_CQ_CQE_SIZE_SHIFT))
197 #define QM_MK_SQC_W13(priority, orders, alg_type) \
198 (((priority) << QM_SQ_PRIORITY_SHIFT) | \
199 ((orders) << QM_SQ_ORDERS_SHIFT) | \
200 (((alg_type) & QM_SQ_TYPE_MASK) << QM_SQ_TYPE_SHIFT))
202 #define QM_MK_SQC_DW3_V1(hop_num, pg_sz, buf_sz, sqe_sz) \
203 (((hop_num) << QM_SQ_HOP_NUM_SHIFT) | \
204 ((pg_sz) << QM_SQ_PAGE_SIZE_SHIFT) | \
205 ((buf_sz) << QM_SQ_BUF_SIZE_SHIFT) | \
206 ((u32)ilog2(sqe_sz) << QM_SQ_SQE_SIZE_SHIFT))
208 #define QM_MK_SQC_DW3_V2(sqe_sz) \
209 ((QM_Q_DEPTH - 1) | ((u32)ilog2(sqe_sz) << QM_SQ_SQE_SIZE_SHIFT))
211 #define INIT_QC_COMMON(qc, base, pasid) do { \
214 (qc)->base_l = cpu_to_le32(lower_32_bits(base)); \
215 (qc)->base_h = cpu_to_le32(upper_32_bits(base)); \
219 (qc)->pasid = cpu_to_le16(pasid); \
229 enum acc_err_result
{
317 struct hisi_qm_resource
{
320 struct list_head list
;
323 struct hisi_qm_hw_ops
{
324 int (*get_vft
)(struct hisi_qm
*qm
, u32
*base
, u32
*number
);
325 void (*qm_db
)(struct hisi_qm
*qm
, u16 qn
,
326 u8 cmd
, u16 index
, u8 priority
);
327 u32 (*get_irq_num
)(struct hisi_qm
*qm
);
328 int (*debug_init
)(struct hisi_qm
*qm
);
329 void (*hw_error_init
)(struct hisi_qm
*qm
, u32 ce
, u32 nfe
, u32 fe
);
330 void (*hw_error_uninit
)(struct hisi_qm
*qm
);
331 enum acc_err_result (*hw_error_handle
)(struct hisi_qm
*qm
);
339 static struct qm_dfx_item qm_dfx_files
[] = {
340 {"err_irq", offsetof(struct qm_dfx
, err_irq_cnt
)},
341 {"aeq_irq", offsetof(struct qm_dfx
, aeq_irq_cnt
)},
342 {"abnormal_irq", offsetof(struct qm_dfx
, abnormal_irq_cnt
)},
343 {"create_qp_err", offsetof(struct qm_dfx
, create_qp_err_cnt
)},
344 {"mb_err", offsetof(struct qm_dfx
, mb_err_cnt
)},
347 static const char * const qm_debug_file_name
[] = {
348 [CURRENT_Q
] = "current_q",
349 [CLEAR_ENABLE
] = "clear_enable",
352 struct hisi_qm_hw_error
{
357 static const struct hisi_qm_hw_error qm_hw_error
[] = {
358 { .int_msk
= BIT(0), .msg
= "qm_axi_rresp" },
359 { .int_msk
= BIT(1), .msg
= "qm_axi_bresp" },
360 { .int_msk
= BIT(2), .msg
= "qm_ecc_mbit" },
361 { .int_msk
= BIT(3), .msg
= "qm_ecc_1bit" },
362 { .int_msk
= BIT(4), .msg
= "qm_acc_get_task_timeout" },
363 { .int_msk
= BIT(5), .msg
= "qm_acc_do_task_timeout" },
364 { .int_msk
= BIT(6), .msg
= "qm_acc_wb_not_ready_timeout" },
365 { .int_msk
= BIT(7), .msg
= "qm_sq_cq_vf_invalid" },
366 { .int_msk
= BIT(8), .msg
= "qm_cq_vf_invalid" },
367 { .int_msk
= BIT(9), .msg
= "qm_sq_vf_invalid" },
368 { .int_msk
= BIT(10), .msg
= "qm_db_timeout" },
369 { .int_msk
= BIT(11), .msg
= "qm_of_fifo_of" },
370 { .int_msk
= BIT(12), .msg
= "qm_db_random_invalid" },
374 static const char * const qm_db_timeout
[] = {
375 "sq", "cq", "eq", "aeq",
378 static const char * const qm_fifo_overflow
[] = {
382 static const char * const qm_s
[] = {
383 "init", "start", "close", "stop",
386 static const char * const qp_s
[] = {
387 "none", "init", "start", "stop", "close",
390 static bool qm_avail_state(struct hisi_qm
*qm
, enum qm_state
new)
392 enum qm_state curr
= atomic_read(&qm
->status
.flags
);
397 if (new == QM_START
|| new == QM_CLOSE
)
405 if (new == QM_CLOSE
|| new == QM_START
)
412 dev_dbg(&qm
->pdev
->dev
, "change qm state from %s to %s\n",
413 qm_s
[curr
], qm_s
[new]);
416 dev_warn(&qm
->pdev
->dev
, "Can not change qm state from %s to %s\n",
417 qm_s
[curr
], qm_s
[new]);
422 static bool qm_qp_avail_state(struct hisi_qm
*qm
, struct hisi_qp
*qp
,
425 enum qm_state qm_curr
= atomic_read(&qm
->status
.flags
);
426 enum qp_state qp_curr
= 0;
430 qp_curr
= atomic_read(&qp
->qp_status
.flags
);
434 if (qm_curr
== QM_START
|| qm_curr
== QM_INIT
)
438 if ((qm_curr
== QM_START
&& qp_curr
== QP_INIT
) ||
439 (qm_curr
== QM_START
&& qp_curr
== QP_STOP
))
443 if ((qm_curr
== QM_START
&& qp_curr
== QP_START
) ||
444 (qp_curr
== QP_INIT
))
448 if ((qm_curr
== QM_START
&& qp_curr
== QP_INIT
) ||
449 (qm_curr
== QM_START
&& qp_curr
== QP_STOP
) ||
450 (qm_curr
== QM_STOP
&& qp_curr
== QP_STOP
) ||
451 (qm_curr
== QM_STOP
&& qp_curr
== QP_INIT
))
458 dev_dbg(&qm
->pdev
->dev
, "change qp state from %s to %s in QM %s\n",
459 qp_s
[qp_curr
], qp_s
[new], qm_s
[qm_curr
]);
462 dev_warn(&qm
->pdev
->dev
,
463 "Can not change qp state from %s to %s in QM %s\n",
464 qp_s
[qp_curr
], qp_s
[new], qm_s
[qm_curr
]);
469 /* return 0 mailbox ready, -ETIMEDOUT hardware timeout */
470 static int qm_wait_mb_ready(struct hisi_qm
*qm
)
474 return readl_relaxed_poll_timeout(qm
->io_base
+ QM_MB_CMD_SEND_BASE
,
475 val
, !((val
>> QM_MB_BUSY_SHIFT
) &
476 0x1), POLL_PERIOD
, POLL_TIMEOUT
);
479 /* 128 bit should be written to hardware at one time to trigger a mailbox */
480 static void qm_mb_write(struct hisi_qm
*qm
, const void *src
)
482 void __iomem
*fun_base
= qm
->io_base
+ QM_MB_CMD_SEND_BASE
;
483 unsigned long tmp0
= 0, tmp1
= 0;
485 if (!IS_ENABLED(CONFIG_ARM64
)) {
486 memcpy_toio(fun_base
, src
, 16);
491 asm volatile("ldp %0, %1, %3\n"
496 "+Q" (*((char __iomem
*)fun_base
))
497 : "Q" (*((char *)src
))
501 static int qm_mb(struct hisi_qm
*qm
, u8 cmd
, dma_addr_t dma_addr
, u16 queue
,
504 struct qm_mailbox mailbox
;
507 dev_dbg(&qm
->pdev
->dev
, "QM mailbox request to q%u: %u-%llx\n",
508 queue
, cmd
, (unsigned long long)dma_addr
);
510 mailbox
.w0
= cpu_to_le16(cmd
|
511 (op
? 0x1 << QM_MB_OP_SHIFT
: 0) |
512 (0x1 << QM_MB_BUSY_SHIFT
));
513 mailbox
.queue_num
= cpu_to_le16(queue
);
514 mailbox
.base_l
= cpu_to_le32(lower_32_bits(dma_addr
));
515 mailbox
.base_h
= cpu_to_le32(upper_32_bits(dma_addr
));
518 mutex_lock(&qm
->mailbox_lock
);
520 if (unlikely(qm_wait_mb_ready(qm
))) {
522 dev_err(&qm
->pdev
->dev
, "QM mailbox is busy to start!\n");
526 qm_mb_write(qm
, &mailbox
);
528 if (unlikely(qm_wait_mb_ready(qm
))) {
530 dev_err(&qm
->pdev
->dev
, "QM mailbox operation timeout!\n");
535 mutex_unlock(&qm
->mailbox_lock
);
538 atomic64_inc(&qm
->debug
.dfx
.mb_err_cnt
);
542 static void qm_db_v1(struct hisi_qm
*qm
, u16 qn
, u8 cmd
, u16 index
, u8 priority
)
546 doorbell
= qn
| ((u64
)cmd
<< QM_DB_CMD_SHIFT_V1
) |
547 ((u64
)index
<< QM_DB_INDEX_SHIFT_V1
) |
548 ((u64
)priority
<< QM_DB_PRIORITY_SHIFT_V1
);
550 writeq(doorbell
, qm
->io_base
+ QM_DOORBELL_BASE_V1
);
553 static void qm_db_v2(struct hisi_qm
*qm
, u16 qn
, u8 cmd
, u16 index
, u8 priority
)
559 if (cmd
== QM_DOORBELL_CMD_SQ
|| cmd
== QM_DOORBELL_CMD_CQ
)
560 dbase
= QM_DOORBELL_SQ_CQ_BASE_V2
;
562 dbase
= QM_DOORBELL_EQ_AEQ_BASE_V2
;
564 doorbell
= qn
| ((u64
)cmd
<< QM_DB_CMD_SHIFT_V2
) |
565 ((u64
)randata
<< QM_DB_RAND_SHIFT_V2
) |
566 ((u64
)index
<< QM_DB_INDEX_SHIFT_V2
) |
567 ((u64
)priority
<< QM_DB_PRIORITY_SHIFT_V2
);
569 writeq(doorbell
, qm
->io_base
+ dbase
);
572 static void qm_db(struct hisi_qm
*qm
, u16 qn
, u8 cmd
, u16 index
, u8 priority
)
574 dev_dbg(&qm
->pdev
->dev
, "QM doorbell request: qn=%u, cmd=%u, index=%u\n",
577 qm
->ops
->qm_db(qm
, qn
, cmd
, index
, priority
);
580 static int qm_dev_mem_reset(struct hisi_qm
*qm
)
584 writel(0x1, qm
->io_base
+ QM_MEM_START_INIT
);
585 return readl_relaxed_poll_timeout(qm
->io_base
+ QM_MEM_INIT_DONE
, val
,
586 val
& BIT(0), POLL_PERIOD
,
590 static u32
qm_get_irq_num_v1(struct hisi_qm
*qm
)
592 return QM_IRQ_NUM_V1
;
595 static u32
qm_get_irq_num_v2(struct hisi_qm
*qm
)
597 if (qm
->fun_type
== QM_HW_PF
)
598 return QM_IRQ_NUM_PF_V2
;
600 return QM_IRQ_NUM_VF_V2
;
603 static struct hisi_qp
*qm_to_hisi_qp(struct hisi_qm
*qm
, struct qm_eqe
*eqe
)
605 u16 cqn
= le32_to_cpu(eqe
->dw0
) & QM_EQE_CQN_MASK
;
607 return &qm
->qp_array
[cqn
];
610 static void qm_cq_head_update(struct hisi_qp
*qp
)
612 if (qp
->qp_status
.cq_head
== QM_Q_DEPTH
- 1) {
613 qp
->qp_status
.cqc_phase
= !qp
->qp_status
.cqc_phase
;
614 qp
->qp_status
.cq_head
= 0;
616 qp
->qp_status
.cq_head
++;
620 static void qm_poll_qp(struct hisi_qp
*qp
, struct hisi_qm
*qm
)
628 struct qm_cqe
*cqe
= qp
->cqe
+ qp
->qp_status
.cq_head
;
630 while (QM_CQE_PHASE(cqe
) == qp
->qp_status
.cqc_phase
) {
632 qp
->req_cb(qp
, qp
->sqe
+ qm
->sqe_size
*
633 le16_to_cpu(cqe
->sq_head
));
634 qm_cq_head_update(qp
);
635 cqe
= qp
->cqe
+ qp
->qp_status
.cq_head
;
636 qm_db(qm
, qp
->qp_id
, QM_DOORBELL_CMD_CQ
,
637 qp
->qp_status
.cq_head
, 0);
638 atomic_dec(&qp
->qp_status
.used
);
642 qm_db(qm
, qp
->qp_id
, QM_DOORBELL_CMD_CQ
,
643 qp
->qp_status
.cq_head
, 1);
647 static void qm_work_process(struct work_struct
*work
)
649 struct hisi_qm
*qm
= container_of(work
, struct hisi_qm
, work
);
650 struct qm_eqe
*eqe
= qm
->eqe
+ qm
->status
.eq_head
;
654 while (QM_EQE_PHASE(eqe
) == qm
->status
.eqc_phase
) {
656 qp
= qm_to_hisi_qp(qm
, eqe
);
659 if (qm
->status
.eq_head
== QM_EQ_DEPTH
- 1) {
660 qm
->status
.eqc_phase
= !qm
->status
.eqc_phase
;
662 qm
->status
.eq_head
= 0;
665 qm
->status
.eq_head
++;
668 if (eqe_num
== QM_EQ_DEPTH
/ 2 - 1) {
670 qm_db(qm
, 0, QM_DOORBELL_CMD_EQ
, qm
->status
.eq_head
, 0);
674 qm_db(qm
, 0, QM_DOORBELL_CMD_EQ
, qm
->status
.eq_head
, 0);
677 static irqreturn_t
do_qm_irq(int irq
, void *data
)
679 struct hisi_qm
*qm
= (struct hisi_qm
*)data
;
681 /* the workqueue created by device driver of QM */
683 queue_work(qm
->wq
, &qm
->work
);
685 schedule_work(&qm
->work
);
690 static irqreturn_t
qm_irq(int irq
, void *data
)
692 struct hisi_qm
*qm
= data
;
694 if (readl(qm
->io_base
+ QM_VF_EQ_INT_SOURCE
))
695 return do_qm_irq(irq
, data
);
697 atomic64_inc(&qm
->debug
.dfx
.err_irq_cnt
);
698 dev_err(&qm
->pdev
->dev
, "invalid int source\n");
699 qm_db(qm
, 0, QM_DOORBELL_CMD_EQ
, qm
->status
.eq_head
, 0);
704 static irqreturn_t
qm_aeq_irq(int irq
, void *data
)
706 struct hisi_qm
*qm
= data
;
707 struct qm_aeqe
*aeqe
= qm
->aeqe
+ qm
->status
.aeq_head
;
710 atomic64_inc(&qm
->debug
.dfx
.aeq_irq_cnt
);
711 if (!readl(qm
->io_base
+ QM_VF_AEQ_INT_SOURCE
))
714 while (QM_AEQE_PHASE(aeqe
) == qm
->status
.aeqc_phase
) {
715 type
= le32_to_cpu(aeqe
->dw0
) >> QM_AEQE_TYPE_SHIFT
;
716 if (type
< ARRAY_SIZE(qm_fifo_overflow
))
717 dev_err(&qm
->pdev
->dev
, "%s overflow\n",
718 qm_fifo_overflow
[type
]);
720 dev_err(&qm
->pdev
->dev
, "unknown error type %d\n",
723 if (qm
->status
.aeq_head
== QM_Q_DEPTH
- 1) {
724 qm
->status
.aeqc_phase
= !qm
->status
.aeqc_phase
;
726 qm
->status
.aeq_head
= 0;
729 qm
->status
.aeq_head
++;
732 qm_db(qm
, 0, QM_DOORBELL_CMD_AEQ
, qm
->status
.aeq_head
, 0);
738 static void qm_irq_unregister(struct hisi_qm
*qm
)
740 struct pci_dev
*pdev
= qm
->pdev
;
742 free_irq(pci_irq_vector(pdev
, QM_EQ_EVENT_IRQ_VECTOR
), qm
);
744 if (qm
->ver
== QM_HW_V1
)
747 free_irq(pci_irq_vector(pdev
, QM_AEQ_EVENT_IRQ_VECTOR
), qm
);
749 if (qm
->fun_type
== QM_HW_PF
)
750 free_irq(pci_irq_vector(pdev
,
751 QM_ABNORMAL_EVENT_IRQ_VECTOR
), qm
);
754 static void qm_init_qp_status(struct hisi_qp
*qp
)
756 struct hisi_qp_status
*qp_status
= &qp
->qp_status
;
758 qp_status
->sq_tail
= 0;
759 qp_status
->cq_head
= 0;
760 qp_status
->cqc_phase
= true;
761 atomic_set(&qp_status
->used
, 0);
764 static void qm_vft_data_cfg(struct hisi_qm
*qm
, enum vft_type type
, u32 base
,
772 if (qm
->ver
== QM_HW_V1
) {
773 tmp
= QM_SQC_VFT_BUF_SIZE
|
774 QM_SQC_VFT_SQC_SIZE
|
775 QM_SQC_VFT_INDEX_NUMBER
|
777 (u64
)base
<< QM_SQC_VFT_START_SQN_SHIFT
;
779 tmp
= (u64
)base
<< QM_SQC_VFT_START_SQN_SHIFT
|
781 (u64
)(number
- 1) << QM_SQC_VFT_SQN_SHIFT
;
785 if (qm
->ver
== QM_HW_V1
) {
786 tmp
= QM_CQC_VFT_BUF_SIZE
|
787 QM_CQC_VFT_SQC_SIZE
|
788 QM_CQC_VFT_INDEX_NUMBER
|
791 tmp
= QM_CQC_VFT_VALID
;
797 writel(lower_32_bits(tmp
), qm
->io_base
+ QM_VFT_CFG_DATA_L
);
798 writel(upper_32_bits(tmp
), qm
->io_base
+ QM_VFT_CFG_DATA_H
);
801 static int qm_set_vft_common(struct hisi_qm
*qm
, enum vft_type type
,
802 u32 fun_num
, u32 base
, u32 number
)
807 ret
= readl_relaxed_poll_timeout(qm
->io_base
+ QM_VFT_CFG_RDY
, val
,
808 val
& BIT(0), POLL_PERIOD
,
813 writel(0x0, qm
->io_base
+ QM_VFT_CFG_OP_WR
);
814 writel(type
, qm
->io_base
+ QM_VFT_CFG_TYPE
);
815 writel(fun_num
, qm
->io_base
+ QM_VFT_CFG
);
817 qm_vft_data_cfg(qm
, type
, base
, number
);
819 writel(0x0, qm
->io_base
+ QM_VFT_CFG_RDY
);
820 writel(0x1, qm
->io_base
+ QM_VFT_CFG_OP_ENABLE
);
822 return readl_relaxed_poll_timeout(qm
->io_base
+ QM_VFT_CFG_RDY
, val
,
823 val
& BIT(0), POLL_PERIOD
,
827 /* The config should be conducted after qm_dev_mem_reset() */
828 static int qm_set_sqc_cqc_vft(struct hisi_qm
*qm
, u32 fun_num
, u32 base
,
833 for (i
= SQC_VFT
; i
<= CQC_VFT
; i
++) {
834 ret
= qm_set_vft_common(qm
, i
, fun_num
, base
, number
);
842 static int qm_get_vft_v2(struct hisi_qm
*qm
, u32
*base
, u32
*number
)
847 ret
= qm_mb(qm
, QM_MB_CMD_SQC_VFT_V2
, 0, 0, 1);
851 sqc_vft
= readl(qm
->io_base
+ QM_MB_CMD_DATA_ADDR_L
) |
852 ((u64
)readl(qm
->io_base
+ QM_MB_CMD_DATA_ADDR_H
) << 32);
853 *base
= QM_SQC_VFT_BASE_MASK_V2
& (sqc_vft
>> QM_SQC_VFT_BASE_SHIFT_V2
);
854 *number
= (QM_SQC_VFT_NUM_MASK_v2
&
855 (sqc_vft
>> QM_SQC_VFT_NUM_SHIFT_V2
)) + 1;
860 static struct hisi_qm
*file_to_qm(struct debugfs_file
*file
)
862 struct qm_debug
*debug
= file
->debug
;
864 return container_of(debug
, struct hisi_qm
, debug
);
867 static u32
current_q_read(struct debugfs_file
*file
)
869 struct hisi_qm
*qm
= file_to_qm(file
);
871 return readl(qm
->io_base
+ QM_DFX_SQE_CNT_VF_SQN
) >> QM_DFX_QN_SHIFT
;
874 static int current_q_write(struct debugfs_file
*file
, u32 val
)
876 struct hisi_qm
*qm
= file_to_qm(file
);
879 if (val
>= qm
->debug
.curr_qm_qp_num
)
882 tmp
= val
<< QM_DFX_QN_SHIFT
|
883 (readl(qm
->io_base
+ QM_DFX_SQE_CNT_VF_SQN
) & CURRENT_FUN_MASK
);
884 writel(tmp
, qm
->io_base
+ QM_DFX_SQE_CNT_VF_SQN
);
886 tmp
= val
<< QM_DFX_QN_SHIFT
|
887 (readl(qm
->io_base
+ QM_DFX_CQE_CNT_VF_CQN
) & CURRENT_FUN_MASK
);
888 writel(tmp
, qm
->io_base
+ QM_DFX_CQE_CNT_VF_CQN
);
893 static u32
clear_enable_read(struct debugfs_file
*file
)
895 struct hisi_qm
*qm
= file_to_qm(file
);
897 return readl(qm
->io_base
+ QM_DFX_CNT_CLR_CE
);
900 /* rd_clr_ctrl 1 enable read clear, otherwise 0 disable it */
901 static int clear_enable_write(struct debugfs_file
*file
, u32 rd_clr_ctrl
)
903 struct hisi_qm
*qm
= file_to_qm(file
);
908 writel(rd_clr_ctrl
, qm
->io_base
+ QM_DFX_CNT_CLR_CE
);
913 static ssize_t
qm_debug_read(struct file
*filp
, char __user
*buf
,
914 size_t count
, loff_t
*pos
)
916 struct debugfs_file
*file
= filp
->private_data
;
917 enum qm_debug_file index
= file
->index
;
918 char tbuf
[QM_DBG_TMP_BUF_LEN
];
922 mutex_lock(&file
->lock
);
925 val
= current_q_read(file
);
928 val
= clear_enable_read(file
);
931 mutex_unlock(&file
->lock
);
934 mutex_unlock(&file
->lock
);
936 ret
= scnprintf(tbuf
, QM_DBG_TMP_BUF_LEN
, "%u\n", val
);
937 return simple_read_from_buffer(buf
, count
, pos
, tbuf
, ret
);
940 static ssize_t
qm_debug_write(struct file
*filp
, const char __user
*buf
,
941 size_t count
, loff_t
*pos
)
943 struct debugfs_file
*file
= filp
->private_data
;
944 enum qm_debug_file index
= file
->index
;
946 char tbuf
[QM_DBG_TMP_BUF_LEN
];
952 if (count
>= QM_DBG_TMP_BUF_LEN
)
955 len
= simple_write_to_buffer(tbuf
, QM_DBG_TMP_BUF_LEN
- 1, pos
, buf
,
961 if (kstrtoul(tbuf
, 0, &val
))
964 mutex_lock(&file
->lock
);
967 ret
= current_q_write(file
, val
);
972 ret
= clear_enable_write(file
, val
);
980 mutex_unlock(&file
->lock
);
985 mutex_unlock(&file
->lock
);
989 static const struct file_operations qm_debug_fops
= {
990 .owner
= THIS_MODULE
,
992 .read
= qm_debug_read
,
993 .write
= qm_debug_write
,
996 struct qm_dfx_registers
{
1001 #define CNT_CYC_REGS_NUM 10
1002 static struct qm_dfx_registers qm_dfx_regs
[] = {
1003 /* XXX_CNT are reading clear register */
1004 {"QM_ECC_1BIT_CNT ", 0x104000ull
},
1005 {"QM_ECC_MBIT_CNT ", 0x104008ull
},
1006 {"QM_DFX_MB_CNT ", 0x104018ull
},
1007 {"QM_DFX_DB_CNT ", 0x104028ull
},
1008 {"QM_DFX_SQE_CNT ", 0x104038ull
},
1009 {"QM_DFX_CQE_CNT ", 0x104048ull
},
1010 {"QM_DFX_SEND_SQE_TO_ACC_CNT ", 0x104050ull
},
1011 {"QM_DFX_WB_SQE_FROM_ACC_CNT ", 0x104058ull
},
1012 {"QM_DFX_ACC_FINISH_CNT ", 0x104060ull
},
1013 {"QM_DFX_CQE_ERR_CNT ", 0x1040b4ull
},
1014 {"QM_DFX_FUNS_ACTIVE_ST ", 0x200ull
},
1015 {"QM_ECC_1BIT_INF ", 0x104004ull
},
1016 {"QM_ECC_MBIT_INF ", 0x10400cull
},
1017 {"QM_DFX_ACC_RDY_VLD0 ", 0x1040a0ull
},
1018 {"QM_DFX_ACC_RDY_VLD1 ", 0x1040a4ull
},
1019 {"QM_DFX_AXI_RDY_VLD ", 0x1040a8ull
},
1020 {"QM_DFX_FF_ST0 ", 0x1040c8ull
},
1021 {"QM_DFX_FF_ST1 ", 0x1040ccull
},
1022 {"QM_DFX_FF_ST2 ", 0x1040d0ull
},
1023 {"QM_DFX_FF_ST3 ", 0x1040d4ull
},
1024 {"QM_DFX_FF_ST4 ", 0x1040d8ull
},
1025 {"QM_DFX_FF_ST5 ", 0x1040dcull
},
1026 {"QM_DFX_FF_ST6 ", 0x1040e0ull
},
1027 {"QM_IN_IDLE_ST ", 0x1040e4ull
},
1031 static struct qm_dfx_registers qm_vf_dfx_regs
[] = {
1032 {"QM_DFX_FUNS_ACTIVE_ST ", 0x200ull
},
1036 static int qm_regs_show(struct seq_file
*s
, void *unused
)
1038 struct hisi_qm
*qm
= s
->private;
1039 struct qm_dfx_registers
*regs
;
1042 if (qm
->fun_type
== QM_HW_PF
)
1045 regs
= qm_vf_dfx_regs
;
1047 while (regs
->reg_name
) {
1048 val
= readl(qm
->io_base
+ regs
->reg_offset
);
1049 seq_printf(s
, "%s= 0x%08x\n", regs
->reg_name
, val
);
1056 DEFINE_SHOW_ATTRIBUTE(qm_regs
);
1058 static ssize_t
qm_cmd_read(struct file
*filp
, char __user
*buffer
,
1059 size_t count
, loff_t
*pos
)
1061 char buf
[QM_DBG_READ_LEN
];
1064 len
= scnprintf(buf
, QM_DBG_READ_LEN
, "%s\n",
1065 "Please echo help to cmd to get help information");
1067 return simple_read_from_buffer(buffer
, count
, pos
, buf
, len
);
1070 static void *qm_ctx_alloc(struct hisi_qm
*qm
, size_t ctx_size
,
1071 dma_addr_t
*dma_addr
)
1073 struct device
*dev
= &qm
->pdev
->dev
;
1076 ctx_addr
= kzalloc(ctx_size
, GFP_KERNEL
);
1078 return ERR_PTR(-ENOMEM
);
1080 *dma_addr
= dma_map_single(dev
, ctx_addr
, ctx_size
, DMA_FROM_DEVICE
);
1081 if (dma_mapping_error(dev
, *dma_addr
)) {
1082 dev_err(dev
, "DMA mapping error!\n");
1084 return ERR_PTR(-ENOMEM
);
1090 static void qm_ctx_free(struct hisi_qm
*qm
, size_t ctx_size
,
1091 const void *ctx_addr
, dma_addr_t
*dma_addr
)
1093 struct device
*dev
= &qm
->pdev
->dev
;
1095 dma_unmap_single(dev
, *dma_addr
, ctx_size
, DMA_FROM_DEVICE
);
1099 static int dump_show(struct hisi_qm
*qm
, void *info
,
1100 unsigned int info_size
, char *info_name
)
1102 struct device
*dev
= &qm
->pdev
->dev
;
1103 u8
*info_buf
, *info_curr
= info
;
1105 #define BYTE_PER_DW 4
1107 info_buf
= kzalloc(info_size
, GFP_KERNEL
);
1111 for (i
= 0; i
< info_size
; i
++, info_curr
++) {
1112 if (i
% BYTE_PER_DW
== 0)
1113 info_buf
[i
+ 3UL] = *info_curr
;
1114 else if (i
% BYTE_PER_DW
== 1)
1115 info_buf
[i
+ 1UL] = *info_curr
;
1116 else if (i
% BYTE_PER_DW
== 2)
1117 info_buf
[i
- 1] = *info_curr
;
1118 else if (i
% BYTE_PER_DW
== 3)
1119 info_buf
[i
- 3] = *info_curr
;
1122 dev_info(dev
, "%s DUMP\n", info_name
);
1123 for (i
= 0; i
< info_size
; i
+= BYTE_PER_DW
) {
1124 pr_info("DW%d: %02X%02X %02X%02X\n", i
/ BYTE_PER_DW
,
1125 info_buf
[i
], info_buf
[i
+ 1UL],
1126 info_buf
[i
+ 2UL], info_buf
[i
+ 3UL]);
1134 static int qm_dump_sqc_raw(struct hisi_qm
*qm
, dma_addr_t dma_addr
, u16 qp_id
)
1136 return qm_mb(qm
, QM_MB_CMD_SQC
, dma_addr
, qp_id
, 1);
1139 static int qm_dump_cqc_raw(struct hisi_qm
*qm
, dma_addr_t dma_addr
, u16 qp_id
)
1141 return qm_mb(qm
, QM_MB_CMD_CQC
, dma_addr
, qp_id
, 1);
1144 static int qm_sqc_dump(struct hisi_qm
*qm
, const char *s
)
1146 struct device
*dev
= &qm
->pdev
->dev
;
1147 struct qm_sqc
*sqc
, *sqc_curr
;
1155 ret
= kstrtou32(s
, 0, &qp_id
);
1156 if (ret
|| qp_id
>= qm
->qp_num
) {
1157 dev_err(dev
, "Please input qp num (0-%d)", qm
->qp_num
- 1);
1161 sqc
= qm_ctx_alloc(qm
, sizeof(*sqc
), &sqc_dma
);
1163 return PTR_ERR(sqc
);
1165 ret
= qm_dump_sqc_raw(qm
, sqc_dma
, qp_id
);
1167 down_read(&qm
->qps_lock
);
1169 sqc_curr
= qm
->sqc
+ qp_id
;
1171 ret
= dump_show(qm
, sqc_curr
, sizeof(*sqc
),
1174 dev_info(dev
, "Show soft sqc failed!\n");
1176 up_read(&qm
->qps_lock
);
1181 ret
= dump_show(qm
, sqc
, sizeof(*sqc
), "SQC");
1183 dev_info(dev
, "Show hw sqc failed!\n");
1186 qm_ctx_free(qm
, sizeof(*sqc
), sqc
, &sqc_dma
);
1190 static int qm_cqc_dump(struct hisi_qm
*qm
, const char *s
)
1192 struct device
*dev
= &qm
->pdev
->dev
;
1193 struct qm_cqc
*cqc
, *cqc_curr
;
1201 ret
= kstrtou32(s
, 0, &qp_id
);
1202 if (ret
|| qp_id
>= qm
->qp_num
) {
1203 dev_err(dev
, "Please input qp num (0-%d)", qm
->qp_num
- 1);
1207 cqc
= qm_ctx_alloc(qm
, sizeof(*cqc
), &cqc_dma
);
1209 return PTR_ERR(cqc
);
1211 ret
= qm_dump_cqc_raw(qm
, cqc_dma
, qp_id
);
1213 down_read(&qm
->qps_lock
);
1215 cqc_curr
= qm
->cqc
+ qp_id
;
1217 ret
= dump_show(qm
, cqc_curr
, sizeof(*cqc
),
1220 dev_info(dev
, "Show soft cqc failed!\n");
1222 up_read(&qm
->qps_lock
);
1227 ret
= dump_show(qm
, cqc
, sizeof(*cqc
), "CQC");
1229 dev_info(dev
, "Show hw cqc failed!\n");
1232 qm_ctx_free(qm
, sizeof(*cqc
), cqc
, &cqc_dma
);
1236 static int qm_eqc_aeqc_dump(struct hisi_qm
*qm
, char *s
, size_t size
,
1237 int cmd
, char *name
)
1239 struct device
*dev
= &qm
->pdev
->dev
;
1240 dma_addr_t xeqc_dma
;
1244 if (strsep(&s
, " ")) {
1245 dev_err(dev
, "Please do not input extra characters!\n");
1249 xeqc
= qm_ctx_alloc(qm
, size
, &xeqc_dma
);
1251 return PTR_ERR(xeqc
);
1253 ret
= qm_mb(qm
, cmd
, xeqc_dma
, 0, 1);
1257 ret
= dump_show(qm
, xeqc
, size
, name
);
1259 dev_info(dev
, "Show hw %s failed!\n", name
);
1262 qm_ctx_free(qm
, size
, xeqc
, &xeqc_dma
);
1266 static int q_dump_param_parse(struct hisi_qm
*qm
, char *s
,
1267 u32
*e_id
, u32
*q_id
)
1269 struct device
*dev
= &qm
->pdev
->dev
;
1270 unsigned int qp_num
= qm
->qp_num
;
1274 presult
= strsep(&s
, " ");
1276 dev_err(dev
, "Please input qp number!\n");
1280 ret
= kstrtou32(presult
, 0, q_id
);
1281 if (ret
|| *q_id
>= qp_num
) {
1282 dev_err(dev
, "Please input qp num (0-%d)", qp_num
- 1);
1286 presult
= strsep(&s
, " ");
1288 dev_err(dev
, "Please input sqe number!\n");
1292 ret
= kstrtou32(presult
, 0, e_id
);
1293 if (ret
|| *e_id
>= QM_Q_DEPTH
) {
1294 dev_err(dev
, "Please input sqe num (0-%d)", QM_Q_DEPTH
- 1);
1298 if (strsep(&s
, " ")) {
1299 dev_err(dev
, "Please do not input extra characters!\n");
1306 static int qm_sq_dump(struct hisi_qm
*qm
, char *s
)
1308 struct device
*dev
= &qm
->pdev
->dev
;
1309 void *sqe
, *sqe_curr
;
1314 ret
= q_dump_param_parse(qm
, s
, &sqe_id
, &qp_id
);
1318 sqe
= kzalloc(qm
->sqe_size
* QM_Q_DEPTH
, GFP_KERNEL
);
1322 qp
= &qm
->qp_array
[qp_id
];
1323 memcpy(sqe
, qp
->sqe
, qm
->sqe_size
* QM_Q_DEPTH
);
1324 sqe_curr
= sqe
+ (u32
)(sqe_id
* qm
->sqe_size
);
1325 memset(sqe_curr
+ qm
->debug
.sqe_mask_offset
, QM_SQE_ADDR_MASK
,
1326 qm
->debug
.sqe_mask_len
);
1328 ret
= dump_show(qm
, sqe_curr
, qm
->sqe_size
, "SQE");
1330 dev_info(dev
, "Show sqe failed!\n");
1337 static int qm_cq_dump(struct hisi_qm
*qm
, char *s
)
1339 struct device
*dev
= &qm
->pdev
->dev
;
1340 struct qm_cqe
*cqe_curr
;
1345 ret
= q_dump_param_parse(qm
, s
, &cqe_id
, &qp_id
);
1349 qp
= &qm
->qp_array
[qp_id
];
1350 cqe_curr
= qp
->cqe
+ cqe_id
;
1351 ret
= dump_show(qm
, cqe_curr
, sizeof(struct qm_cqe
), "CQE");
1353 dev_info(dev
, "Show cqe failed!\n");
1358 static int qm_eq_aeq_dump(struct hisi_qm
*qm
, const char *s
,
1359 size_t size
, char *name
)
1361 struct device
*dev
= &qm
->pdev
->dev
;
1369 ret
= kstrtou32(s
, 0, &xeqe_id
);
1373 if (!strcmp(name
, "EQE") && xeqe_id
>= QM_EQ_DEPTH
) {
1374 dev_err(dev
, "Please input eqe num (0-%d)", QM_EQ_DEPTH
- 1);
1376 } else if (!strcmp(name
, "AEQE") && xeqe_id
>= QM_Q_DEPTH
) {
1377 dev_err(dev
, "Please input aeqe num (0-%d)", QM_Q_DEPTH
- 1);
1381 down_read(&qm
->qps_lock
);
1383 if (qm
->eqe
&& !strcmp(name
, "EQE")) {
1384 xeqe
= qm
->eqe
+ xeqe_id
;
1385 } else if (qm
->aeqe
&& !strcmp(name
, "AEQE")) {
1386 xeqe
= qm
->aeqe
+ xeqe_id
;
1392 ret
= dump_show(qm
, xeqe
, size
, name
);
1394 dev_info(dev
, "Show %s failed!\n", name
);
1397 up_read(&qm
->qps_lock
);
1401 static int qm_dbg_help(struct hisi_qm
*qm
, char *s
)
1403 struct device
*dev
= &qm
->pdev
->dev
;
1405 if (strsep(&s
, " ")) {
1406 dev_err(dev
, "Please do not input extra characters!\n");
1410 dev_info(dev
, "available commands:\n");
1411 dev_info(dev
, "sqc <num>\n");
1412 dev_info(dev
, "cqc <num>\n");
1413 dev_info(dev
, "eqc\n");
1414 dev_info(dev
, "aeqc\n");
1415 dev_info(dev
, "sq <num> <e>\n");
1416 dev_info(dev
, "cq <num> <e>\n");
1417 dev_info(dev
, "eq <e>\n");
1418 dev_info(dev
, "aeq <e>\n");
1423 static int qm_cmd_write_dump(struct hisi_qm
*qm
, const char *cmd_buf
)
1425 struct device
*dev
= &qm
->pdev
->dev
;
1426 char *presult
, *s
, *s_tmp
;
1429 s
= kstrdup(cmd_buf
, GFP_KERNEL
);
1434 presult
= strsep(&s
, " ");
1437 goto err_buffer_free
;
1440 if (!strcmp(presult
, "sqc"))
1441 ret
= qm_sqc_dump(qm
, s
);
1442 else if (!strcmp(presult
, "cqc"))
1443 ret
= qm_cqc_dump(qm
, s
);
1444 else if (!strcmp(presult
, "eqc"))
1445 ret
= qm_eqc_aeqc_dump(qm
, s
, sizeof(struct qm_eqc
),
1446 QM_MB_CMD_EQC
, "EQC");
1447 else if (!strcmp(presult
, "aeqc"))
1448 ret
= qm_eqc_aeqc_dump(qm
, s
, sizeof(struct qm_aeqc
),
1449 QM_MB_CMD_AEQC
, "AEQC");
1450 else if (!strcmp(presult
, "sq"))
1451 ret
= qm_sq_dump(qm
, s
);
1452 else if (!strcmp(presult
, "cq"))
1453 ret
= qm_cq_dump(qm
, s
);
1454 else if (!strcmp(presult
, "eq"))
1455 ret
= qm_eq_aeq_dump(qm
, s
, sizeof(struct qm_eqe
), "EQE");
1456 else if (!strcmp(presult
, "aeq"))
1457 ret
= qm_eq_aeq_dump(qm
, s
, sizeof(struct qm_aeqe
), "AEQE");
1458 else if (!strcmp(presult
, "help"))
1459 ret
= qm_dbg_help(qm
, s
);
1464 dev_info(dev
, "Please echo help\n");
1472 static ssize_t
qm_cmd_write(struct file
*filp
, const char __user
*buffer
,
1473 size_t count
, loff_t
*pos
)
1475 struct hisi_qm
*qm
= filp
->private_data
;
1476 char *cmd_buf
, *cmd_buf_tmp
;
1482 /* Judge if the instance is being reset. */
1483 if (unlikely(atomic_read(&qm
->status
.flags
) == QM_STOP
))
1486 if (count
> QM_DBG_WRITE_LEN
)
1489 cmd_buf
= kzalloc(count
+ 1, GFP_KERNEL
);
1493 if (copy_from_user(cmd_buf
, buffer
, count
)) {
1498 cmd_buf
[count
] = '\0';
1500 cmd_buf_tmp
= strchr(cmd_buf
, '\n');
1502 *cmd_buf_tmp
= '\0';
1503 count
= cmd_buf_tmp
- cmd_buf
+ 1;
1506 ret
= qm_cmd_write_dump(qm
, cmd_buf
);
1517 static const struct file_operations qm_cmd_fops
= {
1518 .owner
= THIS_MODULE
,
1519 .open
= simple_open
,
1520 .read
= qm_cmd_read
,
1521 .write
= qm_cmd_write
,
1524 static void qm_create_debugfs_file(struct hisi_qm
*qm
, enum qm_debug_file index
)
1526 struct dentry
*qm_d
= qm
->debug
.qm_d
;
1527 struct debugfs_file
*file
= qm
->debug
.files
+ index
;
1529 debugfs_create_file(qm_debug_file_name
[index
], 0600, qm_d
, file
,
1532 file
->index
= index
;
1533 mutex_init(&file
->lock
);
1534 file
->debug
= &qm
->debug
;
1537 static void qm_hw_error_init_v1(struct hisi_qm
*qm
, u32 ce
, u32 nfe
, u32 fe
)
1539 writel(QM_ABNORMAL_INT_MASK_VALUE
, qm
->io_base
+ QM_ABNORMAL_INT_MASK
);
1542 static void qm_hw_error_init_v2(struct hisi_qm
*qm
, u32 ce
, u32 nfe
, u32 fe
)
1544 u32 irq_enable
= ce
| nfe
| fe
;
1545 u32 irq_unmask
= ~irq_enable
;
1547 qm
->error_mask
= ce
| nfe
| fe
;
1549 /* clear QM hw residual error source */
1550 writel(QM_ABNORMAL_INT_SOURCE_CLR
,
1551 qm
->io_base
+ QM_ABNORMAL_INT_SOURCE
);
1553 /* configure error type */
1554 writel(ce
, qm
->io_base
+ QM_RAS_CE_ENABLE
);
1555 writel(QM_RAS_CE_TIMES_PER_IRQ
, qm
->io_base
+ QM_RAS_CE_THRESHOLD
);
1556 writel(nfe
, qm
->io_base
+ QM_RAS_NFE_ENABLE
);
1557 writel(fe
, qm
->io_base
+ QM_RAS_FE_ENABLE
);
1559 irq_unmask
&= readl(qm
->io_base
+ QM_ABNORMAL_INT_MASK
);
1560 writel(irq_unmask
, qm
->io_base
+ QM_ABNORMAL_INT_MASK
);
1563 static void qm_hw_error_uninit_v2(struct hisi_qm
*qm
)
1565 writel(QM_ABNORMAL_INT_MASK_VALUE
, qm
->io_base
+ QM_ABNORMAL_INT_MASK
);
1568 static void qm_log_hw_error(struct hisi_qm
*qm
, u32 error_status
)
1570 const struct hisi_qm_hw_error
*err
;
1571 struct device
*dev
= &qm
->pdev
->dev
;
1572 u32 reg_val
, type
, vf_num
;
1575 for (i
= 0; i
< ARRAY_SIZE(qm_hw_error
); i
++) {
1576 err
= &qm_hw_error
[i
];
1577 if (!(err
->int_msk
& error_status
))
1580 dev_err(dev
, "%s [error status=0x%x] found\n",
1581 err
->msg
, err
->int_msk
);
1583 if (err
->int_msk
& QM_DB_TIMEOUT
) {
1584 reg_val
= readl(qm
->io_base
+ QM_ABNORMAL_INF01
);
1585 type
= (reg_val
& QM_DB_TIMEOUT_TYPE
) >>
1586 QM_DB_TIMEOUT_TYPE_SHIFT
;
1587 vf_num
= reg_val
& QM_DB_TIMEOUT_VF
;
1588 dev_err(dev
, "qm %s doorbell timeout in function %u\n",
1589 qm_db_timeout
[type
], vf_num
);
1590 } else if (err
->int_msk
& QM_OF_FIFO_OF
) {
1591 reg_val
= readl(qm
->io_base
+ QM_ABNORMAL_INF00
);
1592 type
= (reg_val
& QM_FIFO_OVERFLOW_TYPE
) >>
1593 QM_FIFO_OVERFLOW_TYPE_SHIFT
;
1594 vf_num
= reg_val
& QM_FIFO_OVERFLOW_VF
;
1596 if (type
< ARRAY_SIZE(qm_fifo_overflow
))
1597 dev_err(dev
, "qm %s fifo overflow in function %u\n",
1598 qm_fifo_overflow
[type
], vf_num
);
1600 dev_err(dev
, "unknown error type\n");
1605 static enum acc_err_result
qm_hw_error_handle_v2(struct hisi_qm
*qm
)
1607 u32 error_status
, tmp
;
1610 tmp
= readl(qm
->io_base
+ QM_ABNORMAL_INT_STATUS
);
1611 error_status
= qm
->error_mask
& tmp
;
1614 if (error_status
& QM_ECC_MBIT
)
1615 qm
->err_status
.is_qm_ecc_mbit
= true;
1617 qm_log_hw_error(qm
, error_status
);
1618 if (error_status
== QM_DB_RANDOM_INVALID
) {
1619 writel(error_status
, qm
->io_base
+
1620 QM_ABNORMAL_INT_SOURCE
);
1621 return ACC_ERR_RECOVERED
;
1624 return ACC_ERR_NEED_RESET
;
1627 return ACC_ERR_RECOVERED
;
1630 static const struct hisi_qm_hw_ops qm_hw_ops_v1
= {
1632 .get_irq_num
= qm_get_irq_num_v1
,
1633 .hw_error_init
= qm_hw_error_init_v1
,
1636 static const struct hisi_qm_hw_ops qm_hw_ops_v2
= {
1637 .get_vft
= qm_get_vft_v2
,
1639 .get_irq_num
= qm_get_irq_num_v2
,
1640 .hw_error_init
= qm_hw_error_init_v2
,
1641 .hw_error_uninit
= qm_hw_error_uninit_v2
,
1642 .hw_error_handle
= qm_hw_error_handle_v2
,
1645 static void *qm_get_avail_sqe(struct hisi_qp
*qp
)
1647 struct hisi_qp_status
*qp_status
= &qp
->qp_status
;
1648 u16 sq_tail
= qp_status
->sq_tail
;
1650 if (unlikely(atomic_read(&qp
->qp_status
.used
) == QM_Q_DEPTH
- 1))
1653 return qp
->sqe
+ sq_tail
* qp
->qm
->sqe_size
;
1656 static struct hisi_qp
*qm_create_qp_nolock(struct hisi_qm
*qm
, u8 alg_type
)
1658 struct device
*dev
= &qm
->pdev
->dev
;
1662 if (!qm_qp_avail_state(qm
, NULL
, QP_INIT
))
1663 return ERR_PTR(-EPERM
);
1665 if (qm
->qp_in_used
== qm
->qp_num
) {
1666 dev_info_ratelimited(dev
, "All %u queues of QM are busy!\n",
1668 atomic64_inc(&qm
->debug
.dfx
.create_qp_err_cnt
);
1669 return ERR_PTR(-EBUSY
);
1672 qp_id
= idr_alloc_cyclic(&qm
->qp_idr
, NULL
, 0, qm
->qp_num
, GFP_ATOMIC
);
1674 dev_info_ratelimited(dev
, "All %u queues of QM are busy!\n",
1676 atomic64_inc(&qm
->debug
.dfx
.create_qp_err_cnt
);
1677 return ERR_PTR(-EBUSY
);
1680 qp
= &qm
->qp_array
[qp_id
];
1682 memset(qp
->cqe
, 0, sizeof(struct qm_cqe
) * QM_Q_DEPTH
);
1684 qp
->event_cb
= NULL
;
1687 qp
->alg_type
= alg_type
;
1689 atomic_set(&qp
->qp_status
.flags
, QP_INIT
);
1695 * hisi_qm_create_qp() - Create a queue pair from qm.
1696 * @qm: The qm we create a qp from.
1697 * @alg_type: Accelerator specific algorithm type in sqc.
1699 * return created qp, -EBUSY if all qps in qm allocated, -ENOMEM if allocating
1702 struct hisi_qp
*hisi_qm_create_qp(struct hisi_qm
*qm
, u8 alg_type
)
1706 down_write(&qm
->qps_lock
);
1707 qp
= qm_create_qp_nolock(qm
, alg_type
);
1708 up_write(&qm
->qps_lock
);
1712 EXPORT_SYMBOL_GPL(hisi_qm_create_qp
);
1715 * hisi_qm_release_qp() - Release a qp back to its qm.
1716 * @qp: The qp we want to release.
1718 * This function releases the resource of a qp.
1720 void hisi_qm_release_qp(struct hisi_qp
*qp
)
1722 struct hisi_qm
*qm
= qp
->qm
;
1724 down_write(&qm
->qps_lock
);
1726 if (!qm_qp_avail_state(qm
, qp
, QP_CLOSE
)) {
1727 up_write(&qm
->qps_lock
);
1732 idr_remove(&qm
->qp_idr
, qp
->qp_id
);
1734 up_write(&qm
->qps_lock
);
1736 EXPORT_SYMBOL_GPL(hisi_qm_release_qp
);
1738 static int qm_sq_ctx_cfg(struct hisi_qp
*qp
, int qp_id
, u32 pasid
)
1740 struct hisi_qm
*qm
= qp
->qm
;
1741 struct device
*dev
= &qm
->pdev
->dev
;
1742 enum qm_hw_ver ver
= qm
->ver
;
1747 sqc
= kzalloc(sizeof(struct qm_sqc
), GFP_KERNEL
);
1750 sqc_dma
= dma_map_single(dev
, sqc
, sizeof(struct qm_sqc
),
1752 if (dma_mapping_error(dev
, sqc_dma
)) {
1757 INIT_QC_COMMON(sqc
, qp
->sqe_dma
, pasid
);
1758 if (ver
== QM_HW_V1
) {
1759 sqc
->dw3
= cpu_to_le32(QM_MK_SQC_DW3_V1(0, 0, 0, qm
->sqe_size
));
1760 sqc
->w8
= cpu_to_le16(QM_Q_DEPTH
- 1);
1762 sqc
->dw3
= cpu_to_le32(QM_MK_SQC_DW3_V2(qm
->sqe_size
));
1763 sqc
->w8
= 0; /* rand_qc */
1765 sqc
->cq_num
= cpu_to_le16(qp_id
);
1766 sqc
->w13
= cpu_to_le16(QM_MK_SQC_W13(0, 1, qp
->alg_type
));
1768 ret
= qm_mb(qm
, QM_MB_CMD_SQC
, sqc_dma
, qp_id
, 0);
1769 dma_unmap_single(dev
, sqc_dma
, sizeof(struct qm_sqc
), DMA_TO_DEVICE
);
1775 static int qm_cq_ctx_cfg(struct hisi_qp
*qp
, int qp_id
, u32 pasid
)
1777 struct hisi_qm
*qm
= qp
->qm
;
1778 struct device
*dev
= &qm
->pdev
->dev
;
1779 enum qm_hw_ver ver
= qm
->ver
;
1784 cqc
= kzalloc(sizeof(struct qm_cqc
), GFP_KERNEL
);
1787 cqc_dma
= dma_map_single(dev
, cqc
, sizeof(struct qm_cqc
),
1789 if (dma_mapping_error(dev
, cqc_dma
)) {
1794 INIT_QC_COMMON(cqc
, qp
->cqe_dma
, pasid
);
1795 if (ver
== QM_HW_V1
) {
1796 cqc
->dw3
= cpu_to_le32(QM_MK_CQC_DW3_V1(0, 0, 0,
1798 cqc
->w8
= cpu_to_le16(QM_Q_DEPTH
- 1);
1800 cqc
->dw3
= cpu_to_le32(QM_MK_CQC_DW3_V2(QM_QC_CQE_SIZE
));
1801 cqc
->w8
= 0; /* rand_qc */
1803 cqc
->dw6
= cpu_to_le32(1 << QM_CQ_PHASE_SHIFT
| 1 << QM_CQ_FLAG_SHIFT
);
1805 ret
= qm_mb(qm
, QM_MB_CMD_CQC
, cqc_dma
, qp_id
, 0);
1806 dma_unmap_single(dev
, cqc_dma
, sizeof(struct qm_cqc
), DMA_TO_DEVICE
);
1812 static int qm_qp_ctx_cfg(struct hisi_qp
*qp
, int qp_id
, u32 pasid
)
1816 qm_init_qp_status(qp
);
1818 ret
= qm_sq_ctx_cfg(qp
, qp_id
, pasid
);
1822 return qm_cq_ctx_cfg(qp
, qp_id
, pasid
);
1825 static int qm_start_qp_nolock(struct hisi_qp
*qp
, unsigned long arg
)
1827 struct hisi_qm
*qm
= qp
->qm
;
1828 struct device
*dev
= &qm
->pdev
->dev
;
1829 int qp_id
= qp
->qp_id
;
1833 if (!qm_qp_avail_state(qm
, qp
, QP_START
))
1836 ret
= qm_qp_ctx_cfg(qp
, qp_id
, pasid
);
1840 atomic_set(&qp
->qp_status
.flags
, QP_START
);
1841 dev_dbg(dev
, "queue %d started\n", qp_id
);
1847 * hisi_qm_start_qp() - Start a qp into running.
1848 * @qp: The qp we want to start to run.
1849 * @arg: Accelerator specific argument.
1851 * After this function, qp can receive request from user. Return 0 if
1852 * successful, Return -EBUSY if failed.
1854 int hisi_qm_start_qp(struct hisi_qp
*qp
, unsigned long arg
)
1856 struct hisi_qm
*qm
= qp
->qm
;
1859 down_write(&qm
->qps_lock
);
1860 ret
= qm_start_qp_nolock(qp
, arg
);
1861 up_write(&qm
->qps_lock
);
1865 EXPORT_SYMBOL_GPL(hisi_qm_start_qp
);
1868 * qm_drain_qp() - Drain a qp.
1869 * @qp: The qp we want to drain.
1871 * Determine whether the queue is cleared by judging the tail pointers of
1874 static int qm_drain_qp(struct hisi_qp
*qp
)
1876 size_t size
= sizeof(struct qm_sqc
) + sizeof(struct qm_cqc
);
1877 struct hisi_qm
*qm
= qp
->qm
;
1878 struct device
*dev
= &qm
->pdev
->dev
;
1881 dma_addr_t dma_addr
;
1886 * No need to judge if ECC multi-bit error occurs because the
1887 * master OOO will be blocked.
1889 if (qm
->err_status
.is_qm_ecc_mbit
|| qm
->err_status
.is_dev_ecc_mbit
)
1892 addr
= qm_ctx_alloc(qm
, size
, &dma_addr
);
1894 dev_err(dev
, "Failed to alloc ctx for sqc and cqc!\n");
1899 ret
= qm_dump_sqc_raw(qm
, dma_addr
, qp
->qp_id
);
1901 dev_err_ratelimited(dev
, "Failed to dump sqc!\n");
1906 ret
= qm_dump_cqc_raw(qm
, (dma_addr
+ sizeof(struct qm_sqc
)),
1909 dev_err_ratelimited(dev
, "Failed to dump cqc!\n");
1912 cqc
= addr
+ sizeof(struct qm_sqc
);
1914 if ((sqc
->tail
== cqc
->tail
) &&
1915 (QM_SQ_TAIL_IDX(sqc
) == QM_CQ_TAIL_IDX(cqc
)))
1918 if (i
== MAX_WAIT_COUNTS
) {
1919 dev_err(dev
, "Fail to empty queue %u!\n", qp
->qp_id
);
1924 usleep_range(WAIT_PERIOD_US_MIN
, WAIT_PERIOD_US_MAX
);
1927 qm_ctx_free(qm
, size
, addr
, &dma_addr
);
1932 static int qm_stop_qp_nolock(struct hisi_qp
*qp
)
1934 struct device
*dev
= &qp
->qm
->pdev
->dev
;
1938 * It is allowed to stop and release qp when reset, If the qp is
1939 * stopped when reset but still want to be released then, the
1940 * is_resetting flag should be set negative so that this qp will not
1941 * be restarted after reset.
1943 if (atomic_read(&qp
->qp_status
.flags
) == QP_STOP
) {
1944 qp
->is_resetting
= false;
1948 if (!qm_qp_avail_state(qp
->qm
, qp
, QP_STOP
))
1951 atomic_set(&qp
->qp_status
.flags
, QP_STOP
);
1953 ret
= qm_drain_qp(qp
);
1955 dev_err(dev
, "Failed to drain out data for stopping!\n");
1958 flush_workqueue(qp
->qm
->wq
);
1960 flush_work(&qp
->qm
->work
);
1962 dev_dbg(dev
, "stop queue %u!", qp
->qp_id
);
1968 * hisi_qm_stop_qp() - Stop a qp in qm.
1969 * @qp: The qp we want to stop.
1971 * This function is reverse of hisi_qm_start_qp. Return 0 if successful.
1973 int hisi_qm_stop_qp(struct hisi_qp
*qp
)
1977 down_write(&qp
->qm
->qps_lock
);
1978 ret
= qm_stop_qp_nolock(qp
);
1979 up_write(&qp
->qm
->qps_lock
);
1983 EXPORT_SYMBOL_GPL(hisi_qm_stop_qp
);
1986 * hisi_qp_send() - Queue up a task in the hardware queue.
1987 * @qp: The qp in which to put the message.
1988 * @msg: The message.
1990 * This function will return -EBUSY if qp is currently full, and -EAGAIN
1991 * if qp related qm is resetting.
1993 * Note: This function may run with qm_irq_thread and ACC reset at same time.
1994 * It has no race with qm_irq_thread. However, during hisi_qp_send, ACC
1995 * reset may happen, we have no lock here considering performance. This
1996 * causes current qm_db sending fail or can not receive sended sqe. QM
1997 * sync/async receive function should handle the error sqe. ACC reset
1998 * done function should clear used sqe to 0.
2000 int hisi_qp_send(struct hisi_qp
*qp
, const void *msg
)
2002 struct hisi_qp_status
*qp_status
= &qp
->qp_status
;
2003 u16 sq_tail
= qp_status
->sq_tail
;
2004 u16 sq_tail_next
= (sq_tail
+ 1) % QM_Q_DEPTH
;
2005 void *sqe
= qm_get_avail_sqe(qp
);
2007 if (unlikely(atomic_read(&qp
->qp_status
.flags
) == QP_STOP
||
2008 atomic_read(&qp
->qm
->status
.flags
) == QM_STOP
||
2009 qp
->is_resetting
)) {
2010 dev_info_ratelimited(&qp
->qm
->pdev
->dev
, "QP is stopped or resetting\n");
2017 memcpy(sqe
, msg
, qp
->qm
->sqe_size
);
2019 qm_db(qp
->qm
, qp
->qp_id
, QM_DOORBELL_CMD_SQ
, sq_tail_next
, 0);
2020 atomic_inc(&qp
->qp_status
.used
);
2021 qp_status
->sq_tail
= sq_tail_next
;
2025 EXPORT_SYMBOL_GPL(hisi_qp_send
);
2027 static void hisi_qm_cache_wb(struct hisi_qm
*qm
)
2031 if (qm
->ver
== QM_HW_V1
)
2034 writel(0x1, qm
->io_base
+ QM_CACHE_WB_START
);
2035 if (readl_relaxed_poll_timeout(qm
->io_base
+ QM_CACHE_WB_DONE
,
2036 val
, val
& BIT(0), POLL_PERIOD
,
2038 dev_err(&qm
->pdev
->dev
, "QM writeback sqc cache fail!\n");
2041 static void qm_qp_event_notifier(struct hisi_qp
*qp
)
2043 wake_up_interruptible(&qp
->uacce_q
->wait
);
2046 static int hisi_qm_get_available_instances(struct uacce_device
*uacce
)
2048 return hisi_qm_get_free_qp_num(uacce
->priv
);
2051 static int hisi_qm_uacce_get_queue(struct uacce_device
*uacce
,
2053 struct uacce_queue
*q
)
2055 struct hisi_qm
*qm
= uacce
->priv
;
2059 qp
= hisi_qm_create_qp(qm
, alg_type
);
2066 qp
->event_cb
= qm_qp_event_notifier
;
2072 static void hisi_qm_uacce_put_queue(struct uacce_queue
*q
)
2074 struct hisi_qp
*qp
= q
->priv
;
2076 hisi_qm_cache_wb(qp
->qm
);
2077 hisi_qm_release_qp(qp
);
2080 /* map sq/cq/doorbell to user space */
2081 static int hisi_qm_uacce_mmap(struct uacce_queue
*q
,
2082 struct vm_area_struct
*vma
,
2083 struct uacce_qfile_region
*qfr
)
2085 struct hisi_qp
*qp
= q
->priv
;
2086 struct hisi_qm
*qm
= qp
->qm
;
2087 size_t sz
= vma
->vm_end
- vma
->vm_start
;
2088 struct pci_dev
*pdev
= qm
->pdev
;
2089 struct device
*dev
= &pdev
->dev
;
2090 unsigned long vm_pgoff
;
2093 switch (qfr
->type
) {
2094 case UACCE_QFRT_MMIO
:
2095 if (qm
->ver
== QM_HW_V1
) {
2096 if (sz
> PAGE_SIZE
* QM_DOORBELL_PAGE_NR
)
2099 if (sz
> PAGE_SIZE
* (QM_DOORBELL_PAGE_NR
+
2100 QM_DOORBELL_SQ_CQ_BASE_V2
/ PAGE_SIZE
))
2104 vma
->vm_flags
|= VM_IO
;
2106 return remap_pfn_range(vma
, vma
->vm_start
,
2107 qm
->phys_base
>> PAGE_SHIFT
,
2108 sz
, pgprot_noncached(vma
->vm_page_prot
));
2109 case UACCE_QFRT_DUS
:
2110 if (sz
!= qp
->qdma
.size
)
2114 * dma_mmap_coherent() requires vm_pgoff as 0
2115 * restore vm_pfoff to initial value for mmap()
2117 vm_pgoff
= vma
->vm_pgoff
;
2119 ret
= dma_mmap_coherent(dev
, vma
, qp
->qdma
.va
,
2121 vma
->vm_pgoff
= vm_pgoff
;
2129 static int hisi_qm_uacce_start_queue(struct uacce_queue
*q
)
2131 struct hisi_qp
*qp
= q
->priv
;
2133 return hisi_qm_start_qp(qp
, qp
->pasid
);
2136 static void hisi_qm_uacce_stop_queue(struct uacce_queue
*q
)
2138 hisi_qm_stop_qp(q
->priv
);
2141 static void qm_set_sqctype(struct uacce_queue
*q
, u16 type
)
2143 struct hisi_qm
*qm
= q
->uacce
->priv
;
2144 struct hisi_qp
*qp
= q
->priv
;
2146 down_write(&qm
->qps_lock
);
2147 qp
->alg_type
= type
;
2148 up_write(&qm
->qps_lock
);
2151 static long hisi_qm_uacce_ioctl(struct uacce_queue
*q
, unsigned int cmd
,
2154 struct hisi_qp
*qp
= q
->priv
;
2155 struct hisi_qp_ctx qp_ctx
;
2157 if (cmd
== UACCE_CMD_QM_SET_QP_CTX
) {
2158 if (copy_from_user(&qp_ctx
, (void __user
*)arg
,
2159 sizeof(struct hisi_qp_ctx
)))
2162 if (qp_ctx
.qc_type
!= 0 && qp_ctx
.qc_type
!= 1)
2165 qm_set_sqctype(q
, qp_ctx
.qc_type
);
2166 qp_ctx
.id
= qp
->qp_id
;
2168 if (copy_to_user((void __user
*)arg
, &qp_ctx
,
2169 sizeof(struct hisi_qp_ctx
)))
2178 static const struct uacce_ops uacce_qm_ops
= {
2179 .get_available_instances
= hisi_qm_get_available_instances
,
2180 .get_queue
= hisi_qm_uacce_get_queue
,
2181 .put_queue
= hisi_qm_uacce_put_queue
,
2182 .start_queue
= hisi_qm_uacce_start_queue
,
2183 .stop_queue
= hisi_qm_uacce_stop_queue
,
2184 .mmap
= hisi_qm_uacce_mmap
,
2185 .ioctl
= hisi_qm_uacce_ioctl
,
2188 static int qm_alloc_uacce(struct hisi_qm
*qm
)
2190 struct pci_dev
*pdev
= qm
->pdev
;
2191 struct uacce_device
*uacce
;
2192 unsigned long mmio_page_nr
;
2193 unsigned long dus_page_nr
;
2194 struct uacce_interface interface
= {
2195 .flags
= UACCE_DEV_SVA
,
2196 .ops
= &uacce_qm_ops
,
2200 ret
= strscpy(interface
.name
, pdev
->driver
->name
,
2201 sizeof(interface
.name
));
2203 return -ENAMETOOLONG
;
2205 uacce
= uacce_alloc(&pdev
->dev
, &interface
);
2207 return PTR_ERR(uacce
);
2209 if (uacce
->flags
& UACCE_DEV_SVA
) {
2212 /* only consider sva case */
2213 uacce_remove(uacce
);
2218 uacce
->is_vf
= pdev
->is_virtfn
;
2220 uacce
->algs
= qm
->algs
;
2222 if (qm
->ver
== QM_HW_V1
) {
2223 mmio_page_nr
= QM_DOORBELL_PAGE_NR
;
2224 uacce
->api_ver
= HISI_QM_API_VER_BASE
;
2226 mmio_page_nr
= QM_DOORBELL_PAGE_NR
+
2227 QM_DOORBELL_SQ_CQ_BASE_V2
/ PAGE_SIZE
;
2228 uacce
->api_ver
= HISI_QM_API_VER2_BASE
;
2231 dus_page_nr
= (PAGE_SIZE
- 1 + qm
->sqe_size
* QM_Q_DEPTH
+
2232 sizeof(struct qm_cqe
) * QM_Q_DEPTH
) >> PAGE_SHIFT
;
2234 uacce
->qf_pg_num
[UACCE_QFRT_MMIO
] = mmio_page_nr
;
2235 uacce
->qf_pg_num
[UACCE_QFRT_DUS
] = dus_page_nr
;
2243 * qm_frozen() - Try to froze QM to cut continuous queue request. If
2244 * there is user on the QM, return failure without doing anything.
2245 * @qm: The qm needed to be fronzen.
2247 * This function frozes QM, then we can do SRIOV disabling.
2249 static int qm_frozen(struct hisi_qm
*qm
)
2251 down_write(&qm
->qps_lock
);
2253 if (qm
->is_frozen
) {
2254 up_write(&qm
->qps_lock
);
2258 if (!qm
->qp_in_used
) {
2259 qm
->qp_in_used
= qm
->qp_num
;
2260 qm
->is_frozen
= true;
2261 up_write(&qm
->qps_lock
);
2265 up_write(&qm
->qps_lock
);
2270 static int qm_try_frozen_vfs(struct pci_dev
*pdev
,
2271 struct hisi_qm_list
*qm_list
)
2273 struct hisi_qm
*qm
, *vf_qm
;
2274 struct pci_dev
*dev
;
2277 if (!qm_list
|| !pdev
)
2280 /* Try to frozen all the VFs as disable SRIOV */
2281 mutex_lock(&qm_list
->lock
);
2282 list_for_each_entry(qm
, &qm_list
->list
, list
) {
2286 if (pci_physfn(dev
) == pdev
) {
2287 vf_qm
= pci_get_drvdata(dev
);
2288 ret
= qm_frozen(vf_qm
);
2295 mutex_unlock(&qm_list
->lock
);
2301 * hisi_qm_wait_task_finish() - Wait until the task is finished
2302 * when removing the driver.
2303 * @qm: The qm needed to wait for the task to finish.
2304 * @qm_list: The list of all available devices.
2306 void hisi_qm_wait_task_finish(struct hisi_qm
*qm
, struct hisi_qm_list
*qm_list
)
2308 while (qm_frozen(qm
) ||
2309 ((qm
->fun_type
== QM_HW_PF
) &&
2310 qm_try_frozen_vfs(qm
->pdev
, qm_list
))) {
2311 msleep(WAIT_PERIOD
);
2314 udelay(REMOVE_WAIT_DELAY
);
2316 EXPORT_SYMBOL_GPL(hisi_qm_wait_task_finish
);
2319 * hisi_qm_get_free_qp_num() - Get free number of qp in qm.
2320 * @qm: The qm which want to get free qp.
2322 * This function return free number of qp in qm.
2324 int hisi_qm_get_free_qp_num(struct hisi_qm
*qm
)
2328 down_read(&qm
->qps_lock
);
2329 ret
= qm
->qp_num
- qm
->qp_in_used
;
2330 up_read(&qm
->qps_lock
);
2334 EXPORT_SYMBOL_GPL(hisi_qm_get_free_qp_num
);
2336 static void hisi_qp_memory_uninit(struct hisi_qm
*qm
, int num
)
2338 struct device
*dev
= &qm
->pdev
->dev
;
2339 struct qm_dma
*qdma
;
2342 for (i
= num
- 1; i
>= 0; i
--) {
2343 qdma
= &qm
->qp_array
[i
].qdma
;
2344 dma_free_coherent(dev
, qdma
->size
, qdma
->va
, qdma
->dma
);
2347 kfree(qm
->qp_array
);
2350 static int hisi_qp_memory_init(struct hisi_qm
*qm
, size_t dma_size
, int id
)
2352 struct device
*dev
= &qm
->pdev
->dev
;
2353 size_t off
= qm
->sqe_size
* QM_Q_DEPTH
;
2356 qp
= &qm
->qp_array
[id
];
2357 qp
->qdma
.va
= dma_alloc_coherent(dev
, dma_size
, &qp
->qdma
.dma
,
2362 qp
->sqe
= qp
->qdma
.va
;
2363 qp
->sqe_dma
= qp
->qdma
.dma
;
2364 qp
->cqe
= qp
->qdma
.va
+ off
;
2365 qp
->cqe_dma
= qp
->qdma
.dma
+ off
;
2366 qp
->qdma
.size
= dma_size
;
2373 static int hisi_qm_memory_init(struct hisi_qm
*qm
)
2375 struct device
*dev
= &qm
->pdev
->dev
;
2376 size_t qp_dma_size
, off
= 0;
2379 #define QM_INIT_BUF(qm, type, num) do { \
2380 (qm)->type = ((qm)->qdma.va + (off)); \
2381 (qm)->type##_dma = (qm)->qdma.dma + (off); \
2382 off += QMC_ALIGN(sizeof(struct qm_##type) * (num)); \
2385 idr_init(&qm
->qp_idr
);
2386 qm
->qdma
.size
= QMC_ALIGN(sizeof(struct qm_eqe
) * QM_EQ_DEPTH
) +
2387 QMC_ALIGN(sizeof(struct qm_aeqe
) * QM_Q_DEPTH
) +
2388 QMC_ALIGN(sizeof(struct qm_sqc
) * qm
->qp_num
) +
2389 QMC_ALIGN(sizeof(struct qm_cqc
) * qm
->qp_num
);
2390 qm
->qdma
.va
= dma_alloc_coherent(dev
, qm
->qdma
.size
, &qm
->qdma
.dma
,
2392 dev_dbg(dev
, "allocate qm dma buf size=%zx)\n", qm
->qdma
.size
);
2396 QM_INIT_BUF(qm
, eqe
, QM_EQ_DEPTH
);
2397 QM_INIT_BUF(qm
, aeqe
, QM_Q_DEPTH
);
2398 QM_INIT_BUF(qm
, sqc
, qm
->qp_num
);
2399 QM_INIT_BUF(qm
, cqc
, qm
->qp_num
);
2401 qm
->qp_array
= kcalloc(qm
->qp_num
, sizeof(struct hisi_qp
), GFP_KERNEL
);
2402 if (!qm
->qp_array
) {
2404 goto err_alloc_qp_array
;
2407 /* one more page for device or qp statuses */
2408 qp_dma_size
= qm
->sqe_size
* QM_Q_DEPTH
+
2409 sizeof(struct qm_cqe
) * QM_Q_DEPTH
;
2410 qp_dma_size
= PAGE_ALIGN(qp_dma_size
);
2411 for (i
= 0; i
< qm
->qp_num
; i
++) {
2412 ret
= hisi_qp_memory_init(qm
, qp_dma_size
, i
);
2414 goto err_init_qp_mem
;
2416 dev_dbg(dev
, "allocate qp dma buf size=%zx)\n", qp_dma_size
);
2422 hisi_qp_memory_uninit(qm
, i
);
2424 dma_free_coherent(dev
, qm
->qdma
.size
, qm
->qdma
.va
, qm
->qdma
.dma
);
2429 static void hisi_qm_pre_init(struct hisi_qm
*qm
)
2431 struct pci_dev
*pdev
= qm
->pdev
;
2433 if (qm
->ver
== QM_HW_V1
)
2434 qm
->ops
= &qm_hw_ops_v1
;
2436 qm
->ops
= &qm_hw_ops_v2
;
2438 pci_set_drvdata(pdev
, qm
);
2439 mutex_init(&qm
->mailbox_lock
);
2440 init_rwsem(&qm
->qps_lock
);
2442 qm
->is_frozen
= false;
2445 static void hisi_qm_pci_uninit(struct hisi_qm
*qm
)
2447 struct pci_dev
*pdev
= qm
->pdev
;
2449 pci_free_irq_vectors(pdev
);
2450 iounmap(qm
->io_base
);
2451 pci_release_mem_regions(pdev
);
2452 pci_disable_device(pdev
);
2456 * hisi_qm_uninit() - Uninitialize qm.
2457 * @qm: The qm needed uninit.
2459 * This function uninits qm related device resources.
2461 void hisi_qm_uninit(struct hisi_qm
*qm
)
2463 struct pci_dev
*pdev
= qm
->pdev
;
2464 struct device
*dev
= &pdev
->dev
;
2466 down_write(&qm
->qps_lock
);
2468 if (!qm_avail_state(qm
, QM_CLOSE
)) {
2469 up_write(&qm
->qps_lock
);
2473 hisi_qp_memory_uninit(qm
, qm
->qp_num
);
2474 idr_destroy(&qm
->qp_idr
);
2477 hisi_qm_cache_wb(qm
);
2478 dma_free_coherent(dev
, qm
->qdma
.size
,
2479 qm
->qdma
.va
, qm
->qdma
.dma
);
2480 memset(&qm
->qdma
, 0, sizeof(qm
->qdma
));
2483 qm_irq_unregister(qm
);
2484 hisi_qm_pci_uninit(qm
);
2485 uacce_remove(qm
->uacce
);
2488 up_write(&qm
->qps_lock
);
2490 EXPORT_SYMBOL_GPL(hisi_qm_uninit
);
2493 * hisi_qm_get_vft() - Get vft from a qm.
2494 * @qm: The qm we want to get its vft.
2495 * @base: The base number of queue in vft.
2496 * @number: The number of queues in vft.
2498 * We can allocate multiple queues to a qm by configuring virtual function
2499 * table. We get related configures by this function. Normally, we call this
2500 * function in VF driver to get the queue information.
2502 * qm hw v1 does not support this interface.
2504 int hisi_qm_get_vft(struct hisi_qm
*qm
, u32
*base
, u32
*number
)
2506 if (!base
|| !number
)
2509 if (!qm
->ops
->get_vft
) {
2510 dev_err(&qm
->pdev
->dev
, "Don't support vft read!\n");
2514 return qm
->ops
->get_vft(qm
, base
, number
);
2516 EXPORT_SYMBOL_GPL(hisi_qm_get_vft
);
2519 * hisi_qm_set_vft() - Set vft to a qm.
2520 * @qm: The qm we want to set its vft.
2521 * @fun_num: The function number.
2522 * @base: The base number of queue in vft.
2523 * @number: The number of queues in vft.
2525 * This function is alway called in PF driver, it is used to assign queues
2528 * Assign queues A~B to PF: hisi_qm_set_vft(qm, 0, A, B - A + 1)
2529 * Assign queues A~B to VF: hisi_qm_set_vft(qm, 2, A, B - A + 1)
2530 * (VF function number 0x2)
2532 static int hisi_qm_set_vft(struct hisi_qm
*qm
, u32 fun_num
, u32 base
,
2535 u32 max_q_num
= qm
->ctrl_qp_num
;
2537 if (base
>= max_q_num
|| number
> max_q_num
||
2538 (base
+ number
) > max_q_num
)
2541 return qm_set_sqc_cqc_vft(qm
, fun_num
, base
, number
);
2544 static void qm_init_eq_aeq_status(struct hisi_qm
*qm
)
2546 struct hisi_qm_status
*status
= &qm
->status
;
2548 status
->eq_head
= 0;
2549 status
->aeq_head
= 0;
2550 status
->eqc_phase
= true;
2551 status
->aeqc_phase
= true;
2554 static int qm_eq_ctx_cfg(struct hisi_qm
*qm
)
2556 struct device
*dev
= &qm
->pdev
->dev
;
2561 eqc
= kzalloc(sizeof(struct qm_eqc
), GFP_KERNEL
); //todo
2564 eqc_dma
= dma_map_single(dev
, eqc
, sizeof(struct qm_eqc
),
2566 if (dma_mapping_error(dev
, eqc_dma
)) {
2571 eqc
->base_l
= cpu_to_le32(lower_32_bits(qm
->eqe_dma
));
2572 eqc
->base_h
= cpu_to_le32(upper_32_bits(qm
->eqe_dma
));
2573 if (qm
->ver
== QM_HW_V1
)
2574 eqc
->dw3
= cpu_to_le32(QM_EQE_AEQE_SIZE
);
2575 eqc
->dw6
= cpu_to_le32((QM_EQ_DEPTH
- 1) | (1 << QM_EQC_PHASE_SHIFT
));
2577 ret
= qm_mb(qm
, QM_MB_CMD_EQC
, eqc_dma
, 0, 0);
2578 dma_unmap_single(dev
, eqc_dma
, sizeof(struct qm_eqc
), DMA_TO_DEVICE
);
2584 static int qm_aeq_ctx_cfg(struct hisi_qm
*qm
)
2586 struct device
*dev
= &qm
->pdev
->dev
;
2587 struct qm_aeqc
*aeqc
;
2588 dma_addr_t aeqc_dma
;
2591 aeqc
= kzalloc(sizeof(struct qm_aeqc
), GFP_KERNEL
);
2594 aeqc_dma
= dma_map_single(dev
, aeqc
, sizeof(struct qm_aeqc
),
2596 if (dma_mapping_error(dev
, aeqc_dma
)) {
2601 aeqc
->base_l
= cpu_to_le32(lower_32_bits(qm
->aeqe_dma
));
2602 aeqc
->base_h
= cpu_to_le32(upper_32_bits(qm
->aeqe_dma
));
2603 aeqc
->dw6
= cpu_to_le32((QM_Q_DEPTH
- 1) | (1 << QM_EQC_PHASE_SHIFT
));
2605 ret
= qm_mb(qm
, QM_MB_CMD_AEQC
, aeqc_dma
, 0, 0);
2606 dma_unmap_single(dev
, aeqc_dma
, sizeof(struct qm_aeqc
), DMA_TO_DEVICE
);
2612 static int qm_eq_aeq_ctx_cfg(struct hisi_qm
*qm
)
2614 struct device
*dev
= &qm
->pdev
->dev
;
2617 qm_init_eq_aeq_status(qm
);
2619 ret
= qm_eq_ctx_cfg(qm
);
2621 dev_err(dev
, "Set eqc failed!\n");
2625 return qm_aeq_ctx_cfg(qm
);
2628 static int __hisi_qm_start(struct hisi_qm
*qm
)
2632 WARN_ON(!qm
->qdma
.dma
);
2634 if (qm
->fun_type
== QM_HW_PF
) {
2635 ret
= qm_dev_mem_reset(qm
);
2639 ret
= hisi_qm_set_vft(qm
, 0, qm
->qp_base
, qm
->qp_num
);
2644 ret
= qm_eq_aeq_ctx_cfg(qm
);
2648 ret
= qm_mb(qm
, QM_MB_CMD_SQC_BT
, qm
->sqc_dma
, 0, 0);
2652 ret
= qm_mb(qm
, QM_MB_CMD_CQC_BT
, qm
->cqc_dma
, 0, 0);
2656 writel(0x0, qm
->io_base
+ QM_VF_EQ_INT_MASK
);
2657 writel(0x0, qm
->io_base
+ QM_VF_AEQ_INT_MASK
);
2663 * hisi_qm_start() - start qm
2664 * @qm: The qm to be started.
2666 * This function starts a qm, then we can allocate qp from this qm.
2668 int hisi_qm_start(struct hisi_qm
*qm
)
2670 struct device
*dev
= &qm
->pdev
->dev
;
2673 down_write(&qm
->qps_lock
);
2675 if (!qm_avail_state(qm
, QM_START
)) {
2676 up_write(&qm
->qps_lock
);
2680 dev_dbg(dev
, "qm start with %d queue pairs\n", qm
->qp_num
);
2683 dev_err(dev
, "qp_num should not be 0\n");
2688 ret
= __hisi_qm_start(qm
);
2690 atomic_set(&qm
->status
.flags
, QM_START
);
2693 up_write(&qm
->qps_lock
);
2696 EXPORT_SYMBOL_GPL(hisi_qm_start
);
2698 static int qm_restart(struct hisi_qm
*qm
)
2700 struct device
*dev
= &qm
->pdev
->dev
;
2704 ret
= hisi_qm_start(qm
);
2708 down_write(&qm
->qps_lock
);
2709 for (i
= 0; i
< qm
->qp_num
; i
++) {
2710 qp
= &qm
->qp_array
[i
];
2711 if (atomic_read(&qp
->qp_status
.flags
) == QP_STOP
&&
2712 qp
->is_resetting
== true) {
2713 ret
= qm_start_qp_nolock(qp
, 0);
2715 dev_err(dev
, "Failed to start qp%d!\n", i
);
2717 up_write(&qm
->qps_lock
);
2720 qp
->is_resetting
= false;
2723 up_write(&qm
->qps_lock
);
2728 /* Stop started qps in reset flow */
2729 static int qm_stop_started_qp(struct hisi_qm
*qm
)
2731 struct device
*dev
= &qm
->pdev
->dev
;
2735 for (i
= 0; i
< qm
->qp_num
; i
++) {
2736 qp
= &qm
->qp_array
[i
];
2737 if (qp
&& atomic_read(&qp
->qp_status
.flags
) == QP_START
) {
2738 qp
->is_resetting
= true;
2739 ret
= qm_stop_qp_nolock(qp
);
2741 dev_err(dev
, "Failed to stop qp%d!\n", i
);
2752 * qm_clear_queues() - Clear all queues memory in a qm.
2753 * @qm: The qm in which the queues will be cleared.
2755 * This function clears all queues memory in a qm. Reset of accelerator can
2756 * use this to clear queues.
2758 static void qm_clear_queues(struct hisi_qm
*qm
)
2763 for (i
= 0; i
< qm
->qp_num
; i
++) {
2764 qp
= &qm
->qp_array
[i
];
2765 if (qp
->is_resetting
)
2766 memset(qp
->qdma
.va
, 0, qp
->qdma
.size
);
2769 memset(qm
->qdma
.va
, 0, qm
->qdma
.size
);
2773 * hisi_qm_stop() - Stop a qm.
2774 * @qm: The qm which will be stopped.
2775 * @r: The reason to stop qm.
2777 * This function stops qm and its qps, then qm can not accept request.
2778 * Related resources are not released at this state, we can use hisi_qm_start
2779 * to let qm start again.
2781 int hisi_qm_stop(struct hisi_qm
*qm
, enum qm_stop_reason r
)
2783 struct device
*dev
= &qm
->pdev
->dev
;
2786 down_write(&qm
->qps_lock
);
2788 qm
->status
.stop_reason
= r
;
2789 if (!qm_avail_state(qm
, QM_STOP
)) {
2794 if (qm
->status
.stop_reason
== QM_SOFT_RESET
||
2795 qm
->status
.stop_reason
== QM_FLR
) {
2796 ret
= qm_stop_started_qp(qm
);
2798 dev_err(dev
, "Failed to stop started qp!\n");
2803 /* Mask eq and aeq irq */
2804 writel(0x1, qm
->io_base
+ QM_VF_EQ_INT_MASK
);
2805 writel(0x1, qm
->io_base
+ QM_VF_AEQ_INT_MASK
);
2807 if (qm
->fun_type
== QM_HW_PF
) {
2808 ret
= hisi_qm_set_vft(qm
, 0, 0, 0);
2810 dev_err(dev
, "Failed to set vft!\n");
2816 qm_clear_queues(qm
);
2817 atomic_set(&qm
->status
.flags
, QM_STOP
);
2820 up_write(&qm
->qps_lock
);
2823 EXPORT_SYMBOL_GPL(hisi_qm_stop
);
2825 static ssize_t
qm_status_read(struct file
*filp
, char __user
*buffer
,
2826 size_t count
, loff_t
*pos
)
2828 struct hisi_qm
*qm
= filp
->private_data
;
2829 char buf
[QM_DBG_READ_LEN
];
2832 val
= atomic_read(&qm
->status
.flags
);
2833 len
= scnprintf(buf
, QM_DBG_READ_LEN
, "%s\n", qm_s
[val
]);
2835 return simple_read_from_buffer(buffer
, count
, pos
, buf
, len
);
2838 static const struct file_operations qm_status_fops
= {
2839 .owner
= THIS_MODULE
,
2840 .open
= simple_open
,
2841 .read
= qm_status_read
,
2844 static int qm_debugfs_atomic64_set(void *data
, u64 val
)
2849 atomic64_set((atomic64_t
*)data
, 0);
2854 static int qm_debugfs_atomic64_get(void *data
, u64
*val
)
2856 *val
= atomic64_read((atomic64_t
*)data
);
2861 DEFINE_DEBUGFS_ATTRIBUTE(qm_atomic64_ops
, qm_debugfs_atomic64_get
,
2862 qm_debugfs_atomic64_set
, "%llu\n");
2865 * hisi_qm_debug_init() - Initialize qm related debugfs files.
2866 * @qm: The qm for which we want to add debugfs files.
2868 * Create qm related debugfs files.
2870 void hisi_qm_debug_init(struct hisi_qm
*qm
)
2872 struct qm_dfx
*dfx
= &qm
->debug
.dfx
;
2873 struct dentry
*qm_d
;
2877 qm_d
= debugfs_create_dir("qm", qm
->debug
.debug_root
);
2878 qm
->debug
.qm_d
= qm_d
;
2880 /* only show this in PF */
2881 if (qm
->fun_type
== QM_HW_PF
)
2882 for (i
= CURRENT_Q
; i
< DEBUG_FILE_NUM
; i
++)
2883 qm_create_debugfs_file(qm
, i
);
2885 debugfs_create_file("regs", 0444, qm
->debug
.qm_d
, qm
, &qm_regs_fops
);
2887 debugfs_create_file("cmd", 0444, qm
->debug
.qm_d
, qm
, &qm_cmd_fops
);
2889 debugfs_create_file("status", 0444, qm
->debug
.qm_d
, qm
,
2891 for (i
= 0; i
< ARRAY_SIZE(qm_dfx_files
); i
++) {
2892 data
= (atomic64_t
*)((uintptr_t)dfx
+ qm_dfx_files
[i
].offset
);
2893 debugfs_create_file(qm_dfx_files
[i
].name
,
2900 EXPORT_SYMBOL_GPL(hisi_qm_debug_init
);
2903 * hisi_qm_debug_regs_clear() - clear qm debug related registers.
2904 * @qm: The qm for which we want to clear its debug registers.
2906 void hisi_qm_debug_regs_clear(struct hisi_qm
*qm
)
2908 struct qm_dfx_registers
*regs
;
2911 /* clear current_q */
2912 writel(0x0, qm
->io_base
+ QM_DFX_SQE_CNT_VF_SQN
);
2913 writel(0x0, qm
->io_base
+ QM_DFX_CQE_CNT_VF_CQN
);
2916 * these registers are reading and clearing, so clear them after
2919 writel(0x1, qm
->io_base
+ QM_DFX_CNT_CLR_CE
);
2922 for (i
= 0; i
< CNT_CYC_REGS_NUM
; i
++) {
2923 readl(qm
->io_base
+ regs
->reg_offset
);
2927 writel(0x0, qm
->io_base
+ QM_DFX_CNT_CLR_CE
);
2929 EXPORT_SYMBOL_GPL(hisi_qm_debug_regs_clear
);
2931 static void qm_hw_error_init(struct hisi_qm
*qm
)
2933 const struct hisi_qm_err_info
*err_info
= &qm
->err_ini
->err_info
;
2935 if (!qm
->ops
->hw_error_init
) {
2936 dev_err(&qm
->pdev
->dev
, "QM doesn't support hw error handling!\n");
2940 qm
->ops
->hw_error_init(qm
, err_info
->ce
, err_info
->nfe
, err_info
->fe
);
2943 static void qm_hw_error_uninit(struct hisi_qm
*qm
)
2945 if (!qm
->ops
->hw_error_uninit
) {
2946 dev_err(&qm
->pdev
->dev
, "Unexpected QM hw error uninit!\n");
2950 qm
->ops
->hw_error_uninit(qm
);
2953 static enum acc_err_result
qm_hw_error_handle(struct hisi_qm
*qm
)
2955 if (!qm
->ops
->hw_error_handle
) {
2956 dev_err(&qm
->pdev
->dev
, "QM doesn't support hw error report!\n");
2957 return ACC_ERR_NONE
;
2960 return qm
->ops
->hw_error_handle(qm
);
2964 * hisi_qm_dev_err_init() - Initialize device error configuration.
2965 * @qm: The qm for which we want to do error initialization.
2967 * Initialize QM and device error related configuration.
2969 void hisi_qm_dev_err_init(struct hisi_qm
*qm
)
2971 if (qm
->fun_type
== QM_HW_VF
)
2974 qm_hw_error_init(qm
);
2976 if (!qm
->err_ini
->hw_err_enable
) {
2977 dev_err(&qm
->pdev
->dev
, "Device doesn't support hw error init!\n");
2980 qm
->err_ini
->hw_err_enable(qm
);
2982 EXPORT_SYMBOL_GPL(hisi_qm_dev_err_init
);
2985 * hisi_qm_dev_err_uninit() - Uninitialize device error configuration.
2986 * @qm: The qm for which we want to do error uninitialization.
2988 * Uninitialize QM and device error related configuration.
2990 void hisi_qm_dev_err_uninit(struct hisi_qm
*qm
)
2992 if (qm
->fun_type
== QM_HW_VF
)
2995 qm_hw_error_uninit(qm
);
2997 if (!qm
->err_ini
->hw_err_disable
) {
2998 dev_err(&qm
->pdev
->dev
, "Unexpected device hw error uninit!\n");
3001 qm
->err_ini
->hw_err_disable(qm
);
3003 EXPORT_SYMBOL_GPL(hisi_qm_dev_err_uninit
);
3006 * hisi_qm_free_qps() - free multiple queue pairs.
3007 * @qps: The queue pairs need to be freed.
3008 * @qp_num: The num of queue pairs.
3010 void hisi_qm_free_qps(struct hisi_qp
**qps
, int qp_num
)
3014 if (!qps
|| qp_num
<= 0)
3017 for (i
= qp_num
- 1; i
>= 0; i
--)
3018 hisi_qm_release_qp(qps
[i
]);
3020 EXPORT_SYMBOL_GPL(hisi_qm_free_qps
);
3022 static void free_list(struct list_head
*head
)
3024 struct hisi_qm_resource
*res
, *tmp
;
3026 list_for_each_entry_safe(res
, tmp
, head
, list
) {
3027 list_del(&res
->list
);
3032 static int hisi_qm_sort_devices(int node
, struct list_head
*head
,
3033 struct hisi_qm_list
*qm_list
)
3035 struct hisi_qm_resource
*res
, *tmp
;
3037 struct list_head
*n
;
3041 list_for_each_entry(qm
, &qm_list
->list
, list
) {
3042 dev
= &qm
->pdev
->dev
;
3044 if (IS_ENABLED(CONFIG_NUMA
)) {
3045 dev_node
= dev_to_node(dev
);
3050 res
= kzalloc(sizeof(*res
), GFP_KERNEL
);
3055 res
->distance
= node_distance(dev_node
, node
);
3057 list_for_each_entry(tmp
, head
, list
) {
3058 if (res
->distance
< tmp
->distance
) {
3063 list_add_tail(&res
->list
, n
);
3070 * hisi_qm_alloc_qps_node() - Create multiple queue pairs.
3071 * @qm_list: The list of all available devices.
3072 * @qp_num: The number of queue pairs need created.
3073 * @alg_type: The algorithm type.
3074 * @node: The numa node.
3075 * @qps: The queue pairs need created.
3077 * This function will sort all available device according to numa distance.
3078 * Then try to create all queue pairs from one device, if all devices do
3079 * not meet the requirements will return error.
3081 int hisi_qm_alloc_qps_node(struct hisi_qm_list
*qm_list
, int qp_num
,
3082 u8 alg_type
, int node
, struct hisi_qp
**qps
)
3084 struct hisi_qm_resource
*tmp
;
3089 if (!qps
|| !qm_list
|| qp_num
<= 0)
3092 mutex_lock(&qm_list
->lock
);
3093 if (hisi_qm_sort_devices(node
, &head
, qm_list
)) {
3094 mutex_unlock(&qm_list
->lock
);
3098 list_for_each_entry(tmp
, &head
, list
) {
3099 for (i
= 0; i
< qp_num
; i
++) {
3100 qps
[i
] = hisi_qm_create_qp(tmp
->qm
, alg_type
);
3101 if (IS_ERR(qps
[i
])) {
3102 hisi_qm_free_qps(qps
, i
);
3113 mutex_unlock(&qm_list
->lock
);
3115 pr_info("Failed to create qps, node[%d], alg[%d], qp[%d]!\n",
3116 node
, alg_type
, qp_num
);
3122 EXPORT_SYMBOL_GPL(hisi_qm_alloc_qps_node
);
3124 static int qm_vf_q_assign(struct hisi_qm
*qm
, u32 num_vfs
)
3126 u32 remain_q_num
, q_num
, i
, j
;
3127 u32 q_base
= qm
->qp_num
;
3133 remain_q_num
= qm
->ctrl_qp_num
- qm
->qp_num
;
3135 /* If remain queues not enough, return error. */
3136 if (qm
->ctrl_qp_num
< qm
->qp_num
|| remain_q_num
< num_vfs
)
3139 q_num
= remain_q_num
/ num_vfs
;
3140 for (i
= 1; i
<= num_vfs
; i
++) {
3142 q_num
+= remain_q_num
% num_vfs
;
3143 ret
= hisi_qm_set_vft(qm
, i
, q_base
, q_num
);
3145 for (j
= i
; j
> 0; j
--)
3146 hisi_qm_set_vft(qm
, j
, 0, 0);
3155 static int qm_clear_vft_config(struct hisi_qm
*qm
)
3160 for (i
= 1; i
<= qm
->vfs_num
; i
++) {
3161 ret
= hisi_qm_set_vft(qm
, i
, 0, 0);
3171 * hisi_qm_sriov_enable() - enable virtual functions
3172 * @pdev: the PCIe device
3173 * @max_vfs: the number of virtual functions to enable
3175 * Returns the number of enabled VFs. If there are VFs enabled already or
3176 * max_vfs is more than the total number of device can be enabled, returns
3179 int hisi_qm_sriov_enable(struct pci_dev
*pdev
, int max_vfs
)
3181 struct hisi_qm
*qm
= pci_get_drvdata(pdev
);
3182 int pre_existing_vfs
, num_vfs
, total_vfs
, ret
;
3184 total_vfs
= pci_sriov_get_totalvfs(pdev
);
3185 pre_existing_vfs
= pci_num_vf(pdev
);
3186 if (pre_existing_vfs
) {
3187 pci_err(pdev
, "%d VFs already enabled. Please disable pre-enabled VFs!\n",
3192 num_vfs
= min_t(int, max_vfs
, total_vfs
);
3193 ret
= qm_vf_q_assign(qm
, num_vfs
);
3195 pci_err(pdev
, "Can't assign queues for VF!\n");
3199 qm
->vfs_num
= num_vfs
;
3201 ret
= pci_enable_sriov(pdev
, num_vfs
);
3203 pci_err(pdev
, "Can't enable VF!\n");
3204 qm_clear_vft_config(qm
);
3208 pci_info(pdev
, "VF enabled, vfs_num(=%d)!\n", num_vfs
);
3212 EXPORT_SYMBOL_GPL(hisi_qm_sriov_enable
);
3215 * hisi_qm_sriov_disable - disable virtual functions
3216 * @pdev: the PCI device.
3217 * @is_frozen: true when all the VFs are frozen.
3219 * Return failure if there are VFs assigned already or VF is in used.
3221 int hisi_qm_sriov_disable(struct pci_dev
*pdev
, bool is_frozen
)
3223 struct hisi_qm
*qm
= pci_get_drvdata(pdev
);
3225 if (pci_vfs_assigned(pdev
)) {
3226 pci_err(pdev
, "Failed to disable VFs as VFs are assigned!\n");
3230 /* While VF is in used, SRIOV cannot be disabled. */
3231 if (!is_frozen
&& qm_try_frozen_vfs(pdev
, qm
->qm_list
)) {
3232 pci_err(pdev
, "Task is using its VF!\n");
3236 pci_disable_sriov(pdev
);
3237 return qm_clear_vft_config(qm
);
3239 EXPORT_SYMBOL_GPL(hisi_qm_sriov_disable
);
3242 * hisi_qm_sriov_configure - configure the number of VFs
3243 * @pdev: The PCI device
3244 * @num_vfs: The number of VFs need enabled
3246 * Enable SR-IOV according to num_vfs, 0 means disable.
3248 int hisi_qm_sriov_configure(struct pci_dev
*pdev
, int num_vfs
)
3251 return hisi_qm_sriov_disable(pdev
, 0);
3253 return hisi_qm_sriov_enable(pdev
, num_vfs
);
3255 EXPORT_SYMBOL_GPL(hisi_qm_sriov_configure
);
3257 static enum acc_err_result
qm_dev_err_handle(struct hisi_qm
*qm
)
3261 if (!qm
->err_ini
->get_dev_hw_err_status
) {
3262 dev_err(&qm
->pdev
->dev
, "Device doesn't support get hw error status!\n");
3263 return ACC_ERR_NONE
;
3266 /* get device hardware error status */
3267 err_sts
= qm
->err_ini
->get_dev_hw_err_status(qm
);
3269 if (err_sts
& qm
->err_ini
->err_info
.ecc_2bits_mask
)
3270 qm
->err_status
.is_dev_ecc_mbit
= true;
3272 if (!qm
->err_ini
->log_dev_hw_err
) {
3273 dev_err(&qm
->pdev
->dev
, "Device doesn't support log hw error!\n");
3274 return ACC_ERR_NEED_RESET
;
3277 qm
->err_ini
->log_dev_hw_err(qm
, err_sts
);
3278 return ACC_ERR_NEED_RESET
;
3281 return ACC_ERR_RECOVERED
;
3284 static enum acc_err_result
qm_process_dev_error(struct hisi_qm
*qm
)
3286 enum acc_err_result qm_ret
, dev_ret
;
3289 qm_ret
= qm_hw_error_handle(qm
);
3291 /* log device error */
3292 dev_ret
= qm_dev_err_handle(qm
);
3294 return (qm_ret
== ACC_ERR_NEED_RESET
||
3295 dev_ret
== ACC_ERR_NEED_RESET
) ?
3296 ACC_ERR_NEED_RESET
: ACC_ERR_RECOVERED
;
3300 * hisi_qm_dev_err_detected() - Get device and qm error status then log it.
3301 * @pdev: The PCI device which need report error.
3302 * @state: The connectivity between CPU and device.
3304 * We register this function into PCIe AER handlers, It will report device or
3305 * qm hardware error status when error occur.
3307 pci_ers_result_t
hisi_qm_dev_err_detected(struct pci_dev
*pdev
,
3308 pci_channel_state_t state
)
3310 struct hisi_qm
*qm
= pci_get_drvdata(pdev
);
3311 enum acc_err_result ret
;
3313 if (pdev
->is_virtfn
)
3314 return PCI_ERS_RESULT_NONE
;
3316 pci_info(pdev
, "PCI error detected, state(=%d)!!\n", state
);
3317 if (state
== pci_channel_io_perm_failure
)
3318 return PCI_ERS_RESULT_DISCONNECT
;
3320 ret
= qm_process_dev_error(qm
);
3321 if (ret
== ACC_ERR_NEED_RESET
)
3322 return PCI_ERS_RESULT_NEED_RESET
;
3324 return PCI_ERS_RESULT_RECOVERED
;
3326 EXPORT_SYMBOL_GPL(hisi_qm_dev_err_detected
);
3328 static u32
qm_get_hw_error_status(struct hisi_qm
*qm
)
3330 return readl(qm
->io_base
+ QM_ABNORMAL_INT_STATUS
);
3333 static int qm_check_req_recv(struct hisi_qm
*qm
)
3335 struct pci_dev
*pdev
= qm
->pdev
;
3339 writel(ACC_VENDOR_ID_VALUE
, qm
->io_base
+ QM_PEH_VENDOR_ID
);
3340 ret
= readl_relaxed_poll_timeout(qm
->io_base
+ QM_PEH_VENDOR_ID
, val
,
3341 (val
== ACC_VENDOR_ID_VALUE
),
3342 POLL_PERIOD
, POLL_TIMEOUT
);
3344 dev_err(&pdev
->dev
, "Fails to read QM reg!\n");
3348 writel(PCI_VENDOR_ID_HUAWEI
, qm
->io_base
+ QM_PEH_VENDOR_ID
);
3349 ret
= readl_relaxed_poll_timeout(qm
->io_base
+ QM_PEH_VENDOR_ID
, val
,
3350 (val
== PCI_VENDOR_ID_HUAWEI
),
3351 POLL_PERIOD
, POLL_TIMEOUT
);
3353 dev_err(&pdev
->dev
, "Fails to read QM reg in the second time!\n");
3358 static int qm_set_pf_mse(struct hisi_qm
*qm
, bool set
)
3360 struct pci_dev
*pdev
= qm
->pdev
;
3364 pci_read_config_word(pdev
, PCI_COMMAND
, &cmd
);
3366 cmd
|= PCI_COMMAND_MEMORY
;
3368 cmd
&= ~PCI_COMMAND_MEMORY
;
3370 pci_write_config_word(pdev
, PCI_COMMAND
, cmd
);
3371 for (i
= 0; i
< MAX_WAIT_COUNTS
; i
++) {
3372 pci_read_config_word(pdev
, PCI_COMMAND
, &cmd
);
3373 if (set
== ((cmd
& PCI_COMMAND_MEMORY
) >> 1))
3382 static int qm_set_vf_mse(struct hisi_qm
*qm
, bool set
)
3384 struct pci_dev
*pdev
= qm
->pdev
;
3389 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_SRIOV
);
3390 pci_read_config_word(pdev
, pos
+ PCI_SRIOV_CTRL
, &sriov_ctrl
);
3392 sriov_ctrl
|= PCI_SRIOV_CTRL_MSE
;
3394 sriov_ctrl
&= ~PCI_SRIOV_CTRL_MSE
;
3395 pci_write_config_word(pdev
, pos
+ PCI_SRIOV_CTRL
, sriov_ctrl
);
3397 for (i
= 0; i
< MAX_WAIT_COUNTS
; i
++) {
3398 pci_read_config_word(pdev
, pos
+ PCI_SRIOV_CTRL
, &sriov_ctrl
);
3399 if (set
== (sriov_ctrl
& PCI_SRIOV_CTRL_MSE
) >>
3400 ACC_PEH_SRIOV_CTRL_VF_MSE_SHIFT
)
3409 static int qm_set_msi(struct hisi_qm
*qm
, bool set
)
3411 struct pci_dev
*pdev
= qm
->pdev
;
3414 pci_write_config_dword(pdev
, pdev
->msi_cap
+ PCI_MSI_MASK_64
,
3417 pci_write_config_dword(pdev
, pdev
->msi_cap
+ PCI_MSI_MASK_64
,
3418 ACC_PEH_MSI_DISABLE
);
3419 if (qm
->err_status
.is_qm_ecc_mbit
||
3420 qm
->err_status
.is_dev_ecc_mbit
)
3424 if (readl(qm
->io_base
+ QM_PEH_DFX_INFO0
))
3431 static int qm_vf_reset_prepare(struct hisi_qm
*qm
,
3432 enum qm_stop_reason stop_reason
)
3434 struct hisi_qm_list
*qm_list
= qm
->qm_list
;
3435 struct pci_dev
*pdev
= qm
->pdev
;
3436 struct pci_dev
*virtfn
;
3437 struct hisi_qm
*vf_qm
;
3440 mutex_lock(&qm_list
->lock
);
3441 list_for_each_entry(vf_qm
, &qm_list
->list
, list
) {
3442 virtfn
= vf_qm
->pdev
;
3446 if (pci_physfn(virtfn
) == pdev
) {
3447 /* save VFs PCIE BAR configuration */
3448 pci_save_state(virtfn
);
3450 ret
= hisi_qm_stop(vf_qm
, stop_reason
);
3457 mutex_unlock(&qm_list
->lock
);
3461 static int qm_reset_prepare_ready(struct hisi_qm
*qm
)
3463 struct pci_dev
*pdev
= qm
->pdev
;
3464 struct hisi_qm
*pf_qm
= pci_get_drvdata(pci_physfn(pdev
));
3467 /* All reset requests need to be queued for processing */
3468 while (test_and_set_bit(QM_DEV_RESET_FLAG
, &pf_qm
->reset_flag
)) {
3470 if (delay
> QM_RESET_WAIT_TIMEOUT
)
3477 static int qm_controller_reset_prepare(struct hisi_qm
*qm
)
3479 struct pci_dev
*pdev
= qm
->pdev
;
3482 ret
= qm_reset_prepare_ready(qm
);
3484 pci_err(pdev
, "Controller reset not ready!\n");
3489 ret
= qm_vf_reset_prepare(qm
, QM_SOFT_RESET
);
3491 pci_err(pdev
, "Fails to stop VFs!\n");
3496 ret
= hisi_qm_stop(qm
, QM_SOFT_RESET
);
3498 pci_err(pdev
, "Fails to stop QM!\n");
3505 static void qm_dev_ecc_mbit_handle(struct hisi_qm
*qm
)
3509 if (!qm
->err_status
.is_dev_ecc_mbit
&&
3510 qm
->err_status
.is_qm_ecc_mbit
&&
3511 qm
->err_ini
->close_axi_master_ooo
) {
3513 qm
->err_ini
->close_axi_master_ooo(qm
);
3515 } else if (qm
->err_status
.is_dev_ecc_mbit
&&
3516 !qm
->err_status
.is_qm_ecc_mbit
&&
3517 !qm
->err_ini
->close_axi_master_ooo
) {
3519 nfe_enb
= readl(qm
->io_base
+ QM_RAS_NFE_ENABLE
);
3520 writel(nfe_enb
& QM_RAS_NFE_MBIT_DISABLE
,
3521 qm
->io_base
+ QM_RAS_NFE_ENABLE
);
3522 writel(QM_ECC_MBIT
, qm
->io_base
+ QM_ABNORMAL_INT_SET
);
3526 static int qm_soft_reset(struct hisi_qm
*qm
)
3528 struct pci_dev
*pdev
= qm
->pdev
;
3532 /* Ensure all doorbells and mailboxes received by QM */
3533 ret
= qm_check_req_recv(qm
);
3538 ret
= qm_set_vf_mse(qm
, false);
3540 pci_err(pdev
, "Fails to disable vf MSE bit.\n");
3545 ret
= qm_set_msi(qm
, false);
3547 pci_err(pdev
, "Fails to disable PEH MSI bit.\n");
3551 qm_dev_ecc_mbit_handle(qm
);
3553 /* OOO register set and check */
3554 writel(ACC_MASTER_GLOBAL_CTRL_SHUTDOWN
,
3555 qm
->io_base
+ ACC_MASTER_GLOBAL_CTRL
);
3557 /* If bus lock, reset chip */
3558 ret
= readl_relaxed_poll_timeout(qm
->io_base
+ ACC_MASTER_TRANS_RETURN
,
3560 (val
== ACC_MASTER_TRANS_RETURN_RW
),
3561 POLL_PERIOD
, POLL_TIMEOUT
);
3563 pci_emerg(pdev
, "Bus lock! Please reset system.\n");
3567 ret
= qm_set_pf_mse(qm
, false);
3569 pci_err(pdev
, "Fails to disable pf MSE bit.\n");
3573 /* The reset related sub-control registers are not in PCI BAR */
3574 if (ACPI_HANDLE(&pdev
->dev
)) {
3575 unsigned long long value
= 0;
3578 s
= acpi_evaluate_integer(ACPI_HANDLE(&pdev
->dev
),
3579 qm
->err_ini
->err_info
.acpi_rst
,
3581 if (ACPI_FAILURE(s
)) {
3582 pci_err(pdev
, "NO controller reset method!\n");
3587 pci_err(pdev
, "Reset step %llu failed!\n", value
);
3591 pci_err(pdev
, "No reset method!\n");
3598 static int qm_vf_reset_done(struct hisi_qm
*qm
)
3600 struct hisi_qm_list
*qm_list
= qm
->qm_list
;
3601 struct pci_dev
*pdev
= qm
->pdev
;
3602 struct pci_dev
*virtfn
;
3603 struct hisi_qm
*vf_qm
;
3606 mutex_lock(&qm_list
->lock
);
3607 list_for_each_entry(vf_qm
, &qm_list
->list
, list
) {
3608 virtfn
= vf_qm
->pdev
;
3612 if (pci_physfn(virtfn
) == pdev
) {
3613 /* enable VFs PCIE BAR configuration */
3614 pci_restore_state(virtfn
);
3616 ret
= qm_restart(vf_qm
);
3623 mutex_unlock(&qm_list
->lock
);
3627 static u32
qm_get_dev_err_status(struct hisi_qm
*qm
)
3629 return qm
->err_ini
->get_dev_hw_err_status(qm
);
3632 static int qm_dev_hw_init(struct hisi_qm
*qm
)
3634 return qm
->err_ini
->hw_init(qm
);
3637 static void qm_restart_prepare(struct hisi_qm
*qm
)
3641 if (!qm
->err_status
.is_qm_ecc_mbit
&&
3642 !qm
->err_status
.is_dev_ecc_mbit
)
3645 /* temporarily close the OOO port used for PEH to write out MSI */
3646 value
= readl(qm
->io_base
+ ACC_AM_CFG_PORT_WR_EN
);
3647 writel(value
& ~qm
->err_ini
->err_info
.msi_wr_port
,
3648 qm
->io_base
+ ACC_AM_CFG_PORT_WR_EN
);
3650 /* clear dev ecc 2bit error source if having */
3651 value
= qm_get_dev_err_status(qm
) &
3652 qm
->err_ini
->err_info
.ecc_2bits_mask
;
3653 if (value
&& qm
->err_ini
->clear_dev_hw_err_status
)
3654 qm
->err_ini
->clear_dev_hw_err_status(qm
, value
);
3656 /* clear QM ecc mbit error source */
3657 writel(QM_ECC_MBIT
, qm
->io_base
+ QM_ABNORMAL_INT_SOURCE
);
3659 /* clear AM Reorder Buffer ecc mbit source */
3660 writel(ACC_ROB_ECC_ERR_MULTPL
, qm
->io_base
+ ACC_AM_ROB_ECC_INT_STS
);
3662 if (qm
->err_ini
->open_axi_master_ooo
)
3663 qm
->err_ini
->open_axi_master_ooo(qm
);
3666 static void qm_restart_done(struct hisi_qm
*qm
)
3670 if (!qm
->err_status
.is_qm_ecc_mbit
&&
3671 !qm
->err_status
.is_dev_ecc_mbit
)
3674 /* open the OOO port for PEH to write out MSI */
3675 value
= readl(qm
->io_base
+ ACC_AM_CFG_PORT_WR_EN
);
3676 value
|= qm
->err_ini
->err_info
.msi_wr_port
;
3677 writel(value
, qm
->io_base
+ ACC_AM_CFG_PORT_WR_EN
);
3679 qm
->err_status
.is_qm_ecc_mbit
= false;
3680 qm
->err_status
.is_dev_ecc_mbit
= false;
3683 static int qm_controller_reset_done(struct hisi_qm
*qm
)
3685 struct pci_dev
*pdev
= qm
->pdev
;
3688 ret
= qm_set_msi(qm
, true);
3690 pci_err(pdev
, "Fails to enable PEH MSI bit!\n");
3694 ret
= qm_set_pf_mse(qm
, true);
3696 pci_err(pdev
, "Fails to enable pf MSE bit!\n");
3701 ret
= qm_set_vf_mse(qm
, true);
3703 pci_err(pdev
, "Fails to enable vf MSE bit!\n");
3708 ret
= qm_dev_hw_init(qm
);
3710 pci_err(pdev
, "Failed to init device\n");
3714 qm_restart_prepare(qm
);
3716 ret
= qm_restart(qm
);
3718 pci_err(pdev
, "Failed to start QM!\n");
3723 ret
= qm_vf_q_assign(qm
, qm
->vfs_num
);
3725 pci_err(pdev
, "Failed to assign queue!\n");
3730 ret
= qm_vf_reset_done(qm
);
3732 pci_err(pdev
, "Failed to start VFs!\n");
3736 hisi_qm_dev_err_init(qm
);
3737 qm_restart_done(qm
);
3739 clear_bit(QM_DEV_RESET_FLAG
, &qm
->reset_flag
);
3744 static int qm_controller_reset(struct hisi_qm
*qm
)
3746 struct pci_dev
*pdev
= qm
->pdev
;
3749 pci_info(pdev
, "Controller resetting...\n");
3751 ret
= qm_controller_reset_prepare(qm
);
3755 ret
= qm_soft_reset(qm
);
3757 pci_err(pdev
, "Controller reset failed (%d)\n", ret
);
3761 ret
= qm_controller_reset_done(qm
);
3765 pci_info(pdev
, "Controller reset complete\n");
3771 * hisi_qm_dev_slot_reset() - slot reset
3772 * @pdev: the PCIe device
3774 * This function offers QM relate PCIe device reset interface. Drivers which
3775 * use QM can use this function as slot_reset in its struct pci_error_handlers.
3777 pci_ers_result_t
hisi_qm_dev_slot_reset(struct pci_dev
*pdev
)
3779 struct hisi_qm
*qm
= pci_get_drvdata(pdev
);
3782 if (pdev
->is_virtfn
)
3783 return PCI_ERS_RESULT_RECOVERED
;
3785 pci_aer_clear_nonfatal_status(pdev
);
3787 /* reset pcie device controller */
3788 ret
= qm_controller_reset(qm
);
3790 pci_err(pdev
, "Controller reset failed (%d)\n", ret
);
3791 return PCI_ERS_RESULT_DISCONNECT
;
3794 return PCI_ERS_RESULT_RECOVERED
;
3796 EXPORT_SYMBOL_GPL(hisi_qm_dev_slot_reset
);
3798 /* check the interrupt is ecc-mbit error or not */
3799 static int qm_check_dev_error(struct hisi_qm
*qm
)
3803 if (qm
->fun_type
== QM_HW_VF
)
3806 ret
= qm_get_hw_error_status(qm
) & QM_ECC_MBIT
;
3810 return (qm_get_dev_err_status(qm
) &
3811 qm
->err_ini
->err_info
.ecc_2bits_mask
);
3814 void hisi_qm_reset_prepare(struct pci_dev
*pdev
)
3816 struct hisi_qm
*pf_qm
= pci_get_drvdata(pci_physfn(pdev
));
3817 struct hisi_qm
*qm
= pci_get_drvdata(pdev
);
3821 hisi_qm_dev_err_uninit(pf_qm
);
3824 * Check whether there is an ECC mbit error, If it occurs, need to
3825 * wait for soft reset to fix it.
3827 while (qm_check_dev_error(pf_qm
)) {
3829 if (delay
> QM_RESET_WAIT_TIMEOUT
)
3833 ret
= qm_reset_prepare_ready(qm
);
3835 pci_err(pdev
, "FLR not ready!\n");
3840 ret
= qm_vf_reset_prepare(qm
, QM_FLR
);
3842 pci_err(pdev
, "Failed to prepare reset, ret = %d.\n",
3848 ret
= hisi_qm_stop(qm
, QM_FLR
);
3850 pci_err(pdev
, "Failed to stop QM, ret = %d.\n", ret
);
3854 pci_info(pdev
, "FLR resetting...\n");
3856 EXPORT_SYMBOL_GPL(hisi_qm_reset_prepare
);
3858 static bool qm_flr_reset_complete(struct pci_dev
*pdev
)
3860 struct pci_dev
*pf_pdev
= pci_physfn(pdev
);
3861 struct hisi_qm
*qm
= pci_get_drvdata(pf_pdev
);
3864 pci_read_config_dword(qm
->pdev
, PCI_COMMAND
, &id
);
3865 if (id
== QM_PCI_COMMAND_INVALID
) {
3866 pci_err(pdev
, "Device can not be used!\n");
3870 clear_bit(QM_DEV_RESET_FLAG
, &qm
->reset_flag
);
3875 void hisi_qm_reset_done(struct pci_dev
*pdev
)
3877 struct hisi_qm
*pf_qm
= pci_get_drvdata(pci_physfn(pdev
));
3878 struct hisi_qm
*qm
= pci_get_drvdata(pdev
);
3881 hisi_qm_dev_err_init(pf_qm
);
3883 ret
= qm_restart(qm
);
3885 pci_err(pdev
, "Failed to start QM, ret = %d.\n", ret
);
3889 if (qm
->fun_type
== QM_HW_PF
) {
3890 ret
= qm_dev_hw_init(qm
);
3892 pci_err(pdev
, "Failed to init PF, ret = %d.\n", ret
);
3899 ret
= qm_vf_q_assign(qm
, qm
->vfs_num
);
3901 pci_err(pdev
, "Failed to assign VFs, ret = %d.\n", ret
);
3905 ret
= qm_vf_reset_done(qm
);
3907 pci_err(pdev
, "Failed to start VFs, ret = %d.\n", ret
);
3913 if (qm_flr_reset_complete(pdev
))
3914 pci_info(pdev
, "FLR reset complete\n");
3916 EXPORT_SYMBOL_GPL(hisi_qm_reset_done
);
3918 static irqreturn_t
qm_abnormal_irq(int irq
, void *data
)
3920 struct hisi_qm
*qm
= data
;
3921 enum acc_err_result ret
;
3923 atomic64_inc(&qm
->debug
.dfx
.abnormal_irq_cnt
);
3924 ret
= qm_process_dev_error(qm
);
3925 if (ret
== ACC_ERR_NEED_RESET
)
3926 schedule_work(&qm
->rst_work
);
3931 static int qm_irq_register(struct hisi_qm
*qm
)
3933 struct pci_dev
*pdev
= qm
->pdev
;
3936 ret
= request_irq(pci_irq_vector(pdev
, QM_EQ_EVENT_IRQ_VECTOR
),
3937 qm_irq
, IRQF_SHARED
, qm
->dev_name
, qm
);
3941 if (qm
->ver
!= QM_HW_V1
) {
3942 ret
= request_irq(pci_irq_vector(pdev
, QM_AEQ_EVENT_IRQ_VECTOR
),
3943 qm_aeq_irq
, IRQF_SHARED
, qm
->dev_name
, qm
);
3947 if (qm
->fun_type
== QM_HW_PF
) {
3948 ret
= request_irq(pci_irq_vector(pdev
,
3949 QM_ABNORMAL_EVENT_IRQ_VECTOR
),
3950 qm_abnormal_irq
, IRQF_SHARED
,
3953 goto err_abonormal_irq
;
3960 free_irq(pci_irq_vector(pdev
, QM_AEQ_EVENT_IRQ_VECTOR
), qm
);
3962 free_irq(pci_irq_vector(pdev
, QM_EQ_EVENT_IRQ_VECTOR
), qm
);
3967 * hisi_qm_dev_shutdown() - Shutdown device.
3968 * @pdev: The device will be shutdown.
3970 * This function will stop qm when OS shutdown or rebooting.
3972 void hisi_qm_dev_shutdown(struct pci_dev
*pdev
)
3974 struct hisi_qm
*qm
= pci_get_drvdata(pdev
);
3977 ret
= hisi_qm_stop(qm
, QM_NORMAL
);
3979 dev_err(&pdev
->dev
, "Fail to stop qm in shutdown!\n");
3981 EXPORT_SYMBOL_GPL(hisi_qm_dev_shutdown
);
3983 static void hisi_qm_controller_reset(struct work_struct
*rst_work
)
3985 struct hisi_qm
*qm
= container_of(rst_work
, struct hisi_qm
, rst_work
);
3988 /* reset pcie device controller */
3989 ret
= qm_controller_reset(qm
);
3991 dev_err(&qm
->pdev
->dev
, "controller reset failed (%d)\n", ret
);
3996 * hisi_qm_alg_register() - Register alg to crypto and add qm to qm_list.
3997 * @qm: The qm needs add.
3998 * @qm_list: The qm list.
4000 * This function adds qm to qm list, and will register algorithm to
4001 * crypto when the qm list is empty.
4003 int hisi_qm_alg_register(struct hisi_qm
*qm
, struct hisi_qm_list
*qm_list
)
4008 mutex_lock(&qm_list
->lock
);
4009 if (list_empty(&qm_list
->list
))
4011 list_add_tail(&qm
->list
, &qm_list
->list
);
4012 mutex_unlock(&qm_list
->lock
);
4015 ret
= qm_list
->register_to_crypto();
4017 mutex_lock(&qm_list
->lock
);
4018 list_del(&qm
->list
);
4019 mutex_unlock(&qm_list
->lock
);
4025 EXPORT_SYMBOL_GPL(hisi_qm_alg_register
);
4028 * hisi_qm_alg_unregister() - Unregister alg from crypto and delete qm from
4030 * @qm: The qm needs delete.
4031 * @qm_list: The qm list.
4033 * This function deletes qm from qm list, and will unregister algorithm
4034 * from crypto when the qm list is empty.
4036 void hisi_qm_alg_unregister(struct hisi_qm
*qm
, struct hisi_qm_list
*qm_list
)
4038 mutex_lock(&qm_list
->lock
);
4039 list_del(&qm
->list
);
4040 mutex_unlock(&qm_list
->lock
);
4042 if (list_empty(&qm_list
->list
))
4043 qm_list
->unregister_from_crypto();
4045 EXPORT_SYMBOL_GPL(hisi_qm_alg_unregister
);
4047 static int hisi_qm_pci_init(struct hisi_qm
*qm
)
4049 struct pci_dev
*pdev
= qm
->pdev
;
4050 struct device
*dev
= &pdev
->dev
;
4051 unsigned int num_vec
;
4054 ret
= pci_enable_device_mem(pdev
);
4056 dev_err(dev
, "Failed to enable device mem!\n");
4060 ret
= pci_request_mem_regions(pdev
, qm
->dev_name
);
4062 dev_err(dev
, "Failed to request mem regions!\n");
4063 goto err_disable_pcidev
;
4066 qm
->phys_base
= pci_resource_start(pdev
, PCI_BAR_2
);
4067 qm
->phys_size
= pci_resource_len(qm
->pdev
, PCI_BAR_2
);
4068 qm
->io_base
= ioremap(qm
->phys_base
, qm
->phys_size
);
4071 goto err_release_mem_regions
;
4074 ret
= dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(64));
4077 pci_set_master(pdev
);
4079 if (!qm
->ops
->get_irq_num
) {
4083 num_vec
= qm
->ops
->get_irq_num(qm
);
4084 ret
= pci_alloc_irq_vectors(pdev
, num_vec
, num_vec
, PCI_IRQ_MSI
);
4086 dev_err(dev
, "Failed to enable MSI vectors!\n");
4093 iounmap(qm
->io_base
);
4094 err_release_mem_regions
:
4095 pci_release_mem_regions(pdev
);
4097 pci_disable_device(pdev
);
4102 * hisi_qm_init() - Initialize configures about qm.
4103 * @qm: The qm needing init.
4105 * This function init qm, then we can call hisi_qm_start to put qm into work.
4107 int hisi_qm_init(struct hisi_qm
*qm
)
4109 struct pci_dev
*pdev
= qm
->pdev
;
4110 struct device
*dev
= &pdev
->dev
;
4113 hisi_qm_pre_init(qm
);
4115 ret
= qm_alloc_uacce(qm
);
4117 dev_warn(dev
, "fail to alloc uacce (%d)\n", ret
);
4119 ret
= hisi_qm_pci_init(qm
);
4121 goto err_remove_uacce
;
4123 ret
= qm_irq_register(qm
);
4125 goto err_pci_uninit
;
4127 if (qm
->fun_type
== QM_HW_VF
&& qm
->ver
!= QM_HW_V1
) {
4128 /* v2 starts to support get vft by mailbox */
4129 ret
= hisi_qm_get_vft(qm
, &qm
->qp_base
, &qm
->qp_num
);
4131 goto err_irq_unregister
;
4134 ret
= hisi_qm_memory_init(qm
);
4136 goto err_irq_unregister
;
4138 INIT_WORK(&qm
->work
, qm_work_process
);
4139 if (qm
->fun_type
== QM_HW_PF
)
4140 INIT_WORK(&qm
->rst_work
, hisi_qm_controller_reset
);
4142 atomic_set(&qm
->status
.flags
, QM_INIT
);
4147 qm_irq_unregister(qm
);
4149 hisi_qm_pci_uninit(qm
);
4151 uacce_remove(qm
->uacce
);
4155 EXPORT_SYMBOL_GPL(hisi_qm_init
);
4157 MODULE_LICENSE("GPL v2");
4158 MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>");
4159 MODULE_DESCRIPTION("HiSilicon Accelerator queue manager driver");