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/dma-mapping.h>
10 #include <linux/irqreturn.h>
11 #include <linux/log2.h>
12 #include <linux/pm_runtime.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
28 #define QM_IRQ_NUM_VF_V3 3
30 #define QM_EQ_EVENT_IRQ_VECTOR 0
31 #define QM_AEQ_EVENT_IRQ_VECTOR 1
32 #define QM_CMD_EVENT_IRQ_VECTOR 2
33 #define QM_ABNORMAL_EVENT_IRQ_VECTOR 3
36 #define QM_MB_CMD_SQC 0x0
37 #define QM_MB_CMD_CQC 0x1
38 #define QM_MB_CMD_EQC 0x2
39 #define QM_MB_CMD_AEQC 0x3
40 #define QM_MB_CMD_SQC_BT 0x4
41 #define QM_MB_CMD_CQC_BT 0x5
42 #define QM_MB_CMD_SQC_VFT_V2 0x6
43 #define QM_MB_CMD_STOP_QP 0x8
44 #define QM_MB_CMD_SRC 0xc
45 #define QM_MB_CMD_DST 0xd
47 #define QM_MB_CMD_SEND_BASE 0x300
48 #define QM_MB_EVENT_SHIFT 8
49 #define QM_MB_BUSY_SHIFT 13
50 #define QM_MB_OP_SHIFT 14
51 #define QM_MB_CMD_DATA_ADDR_L 0x304
52 #define QM_MB_CMD_DATA_ADDR_H 0x308
53 #define QM_MB_PING_ALL_VFS 0xffff
54 #define QM_MB_CMD_DATA_SHIFT 32
55 #define QM_MB_CMD_DATA_MASK GENMASK(31, 0)
58 #define QM_SQ_HOP_NUM_SHIFT 0
59 #define QM_SQ_PAGE_SIZE_SHIFT 4
60 #define QM_SQ_BUF_SIZE_SHIFT 8
61 #define QM_SQ_SQE_SIZE_SHIFT 12
62 #define QM_SQ_PRIORITY_SHIFT 0
63 #define QM_SQ_ORDERS_SHIFT 4
64 #define QM_SQ_TYPE_SHIFT 8
65 #define QM_QC_PASID_ENABLE 0x1
66 #define QM_QC_PASID_ENABLE_SHIFT 7
68 #define QM_SQ_TYPE_MASK GENMASK(3, 0)
69 #define QM_SQ_TAIL_IDX(sqc) ((le16_to_cpu((sqc)->w11) >> 6) & 0x1)
72 #define QM_CQ_HOP_NUM_SHIFT 0
73 #define QM_CQ_PAGE_SIZE_SHIFT 4
74 #define QM_CQ_BUF_SIZE_SHIFT 8
75 #define QM_CQ_CQE_SIZE_SHIFT 12
76 #define QM_CQ_PHASE_SHIFT 0
77 #define QM_CQ_FLAG_SHIFT 1
79 #define QM_CQE_PHASE(cqe) (le16_to_cpu((cqe)->w7) & 0x1)
80 #define QM_QC_CQE_SIZE 4
81 #define QM_CQ_TAIL_IDX(cqc) ((le16_to_cpu((cqc)->w11) >> 6) & 0x1)
84 #define QM_EQE_AEQE_SIZE (2UL << 12)
85 #define QM_EQC_PHASE_SHIFT 16
87 #define QM_EQE_PHASE(eqe) ((le32_to_cpu((eqe)->dw0) >> 16) & 0x1)
88 #define QM_EQE_CQN_MASK GENMASK(15, 0)
90 #define QM_AEQE_PHASE(aeqe) ((le32_to_cpu((aeqe)->dw0) >> 16) & 0x1)
91 #define QM_AEQE_TYPE_SHIFT 17
93 #define QM_DOORBELL_CMD_SQ 0
94 #define QM_DOORBELL_CMD_CQ 1
95 #define QM_DOORBELL_CMD_EQ 2
96 #define QM_DOORBELL_CMD_AEQ 3
98 #define QM_DOORBELL_BASE_V1 0x340
99 #define QM_DB_CMD_SHIFT_V1 16
100 #define QM_DB_INDEX_SHIFT_V1 32
101 #define QM_DB_PRIORITY_SHIFT_V1 48
102 #define QM_DOORBELL_SQ_CQ_BASE_V2 0x1000
103 #define QM_DOORBELL_EQ_AEQ_BASE_V2 0x2000
104 #define QM_QUE_ISO_CFG_V 0x0030
105 #define QM_PAGE_SIZE 0x0034
106 #define QM_QUE_ISO_EN 0x100154
107 #define QM_CAPBILITY 0x100158
108 #define QM_QP_NUN_MASK GENMASK(10, 0)
109 #define QM_QP_DB_INTERVAL 0x10000
110 #define QM_QP_MAX_NUM_SHIFT 11
111 #define QM_DB_CMD_SHIFT_V2 12
112 #define QM_DB_RAND_SHIFT_V2 16
113 #define QM_DB_INDEX_SHIFT_V2 32
114 #define QM_DB_PRIORITY_SHIFT_V2 48
116 #define QM_MEM_START_INIT 0x100040
117 #define QM_MEM_INIT_DONE 0x100044
118 #define QM_VFT_CFG_RDY 0x10006c
119 #define QM_VFT_CFG_OP_WR 0x100058
120 #define QM_VFT_CFG_TYPE 0x10005c
121 #define QM_SQC_VFT 0x0
122 #define QM_CQC_VFT 0x1
123 #define QM_VFT_CFG 0x100060
124 #define QM_VFT_CFG_OP_ENABLE 0x100054
126 #define QM_VFT_CFG_DATA_L 0x100064
127 #define QM_VFT_CFG_DATA_H 0x100068
128 #define QM_SQC_VFT_BUF_SIZE (7ULL << 8)
129 #define QM_SQC_VFT_SQC_SIZE (5ULL << 12)
130 #define QM_SQC_VFT_INDEX_NUMBER (1ULL << 16)
131 #define QM_SQC_VFT_START_SQN_SHIFT 28
132 #define QM_SQC_VFT_VALID (1ULL << 44)
133 #define QM_SQC_VFT_SQN_SHIFT 45
134 #define QM_CQC_VFT_BUF_SIZE (7ULL << 8)
135 #define QM_CQC_VFT_SQC_SIZE (5ULL << 12)
136 #define QM_CQC_VFT_INDEX_NUMBER (1ULL << 16)
137 #define QM_CQC_VFT_VALID (1ULL << 28)
139 #define QM_SQC_VFT_BASE_SHIFT_V2 28
140 #define QM_SQC_VFT_BASE_MASK_V2 GENMASK(15, 0)
141 #define QM_SQC_VFT_NUM_SHIFT_V2 45
142 #define QM_SQC_VFT_NUM_MASK_v2 GENMASK(9, 0)
144 #define QM_DFX_CNT_CLR_CE 0x100118
146 #define QM_ABNORMAL_INT_SOURCE 0x100000
147 #define QM_ABNORMAL_INT_SOURCE_CLR GENMASK(14, 0)
148 #define QM_ABNORMAL_INT_MASK 0x100004
149 #define QM_ABNORMAL_INT_MASK_VALUE 0x7fff
150 #define QM_ABNORMAL_INT_STATUS 0x100008
151 #define QM_ABNORMAL_INT_SET 0x10000c
152 #define QM_ABNORMAL_INF00 0x100010
153 #define QM_FIFO_OVERFLOW_TYPE 0xc0
154 #define QM_FIFO_OVERFLOW_TYPE_SHIFT 6
155 #define QM_FIFO_OVERFLOW_VF 0x3f
156 #define QM_ABNORMAL_INF01 0x100014
157 #define QM_DB_TIMEOUT_TYPE 0xc0
158 #define QM_DB_TIMEOUT_TYPE_SHIFT 6
159 #define QM_DB_TIMEOUT_VF 0x3f
160 #define QM_RAS_CE_ENABLE 0x1000ec
161 #define QM_RAS_FE_ENABLE 0x1000f0
162 #define QM_RAS_NFE_ENABLE 0x1000f4
163 #define QM_RAS_CE_THRESHOLD 0x1000f8
164 #define QM_RAS_CE_TIMES_PER_IRQ 1
165 #define QM_RAS_MSI_INT_SEL 0x1040f4
166 #define QM_OOO_SHUTDOWN_SEL 0x1040f8
168 #define QM_RESET_WAIT_TIMEOUT 400
169 #define QM_PEH_VENDOR_ID 0x1000d8
170 #define ACC_VENDOR_ID_VALUE 0x5a5a
171 #define QM_PEH_DFX_INFO0 0x1000fc
172 #define QM_PEH_DFX_INFO1 0x100100
173 #define QM_PEH_DFX_MASK (BIT(0) | BIT(2))
174 #define QM_PEH_MSI_FINISH_MASK GENMASK(19, 16)
175 #define ACC_PEH_SRIOV_CTRL_VF_MSE_SHIFT 3
176 #define ACC_PEH_MSI_DISABLE GENMASK(31, 0)
177 #define ACC_MASTER_GLOBAL_CTRL_SHUTDOWN 0x1
178 #define ACC_MASTER_TRANS_RETURN_RW 3
179 #define ACC_MASTER_TRANS_RETURN 0x300150
180 #define ACC_MASTER_GLOBAL_CTRL 0x300000
181 #define ACC_AM_CFG_PORT_WR_EN 0x30001c
182 #define QM_RAS_NFE_MBIT_DISABLE ~QM_ECC_MBIT
183 #define ACC_AM_ROB_ECC_INT_STS 0x300104
184 #define ACC_ROB_ECC_ERR_MULTPL BIT(1)
185 #define QM_MSI_CAP_ENABLE BIT(16)
187 /* interfunction communication */
188 #define QM_IFC_READY_STATUS 0x100128
189 #define QM_IFC_C_STS_M 0x10012C
190 #define QM_IFC_INT_SET_P 0x100130
191 #define QM_IFC_INT_CFG 0x100134
192 #define QM_IFC_INT_SOURCE_P 0x100138
193 #define QM_IFC_INT_SOURCE_V 0x0020
194 #define QM_IFC_INT_MASK 0x0024
195 #define QM_IFC_INT_STATUS 0x0028
196 #define QM_IFC_INT_SET_V 0x002C
197 #define QM_IFC_SEND_ALL_VFS GENMASK(6, 0)
198 #define QM_IFC_INT_SOURCE_CLR GENMASK(63, 0)
199 #define QM_IFC_INT_SOURCE_MASK BIT(0)
200 #define QM_IFC_INT_DISABLE BIT(0)
201 #define QM_IFC_INT_STATUS_MASK BIT(0)
202 #define QM_IFC_INT_SET_MASK BIT(0)
203 #define QM_WAIT_DST_ACK 10
204 #define QM_MAX_PF_WAIT_COUNT 10
205 #define QM_MAX_VF_WAIT_COUNT 40
206 #define QM_VF_RESET_WAIT_US 20000
207 #define QM_VF_RESET_WAIT_CNT 3000
208 #define QM_VF_RESET_WAIT_TIMEOUT_US \
209 (QM_VF_RESET_WAIT_US * QM_VF_RESET_WAIT_CNT)
211 #define QM_DFX_MB_CNT_VF 0x104010
212 #define QM_DFX_DB_CNT_VF 0x104020
213 #define QM_DFX_SQE_CNT_VF_SQN 0x104030
214 #define QM_DFX_CQE_CNT_VF_CQN 0x104040
215 #define QM_DFX_QN_SHIFT 16
216 #define CURRENT_FUN_MASK GENMASK(5, 0)
217 #define CURRENT_Q_MASK GENMASK(31, 16)
219 #define POLL_PERIOD 10
220 #define POLL_TIMEOUT 1000
221 #define WAIT_PERIOD_US_MAX 200
222 #define WAIT_PERIOD_US_MIN 100
223 #define MAX_WAIT_COUNTS 1000
224 #define QM_CACHE_WB_START 0x204
225 #define QM_CACHE_WB_DONE 0x208
229 #define QM_SQE_DATA_ALIGN_MASK GENMASK(6, 0)
230 #define QMC_ALIGN(sz) ALIGN(sz, 32)
232 #define QM_DBG_READ_LEN 256
233 #define QM_DBG_WRITE_LEN 1024
234 #define QM_DBG_TMP_BUF_LEN 22
235 #define QM_PCI_COMMAND_INVALID ~0
236 #define QM_RESET_STOP_TX_OFFSET 1
237 #define QM_RESET_STOP_RX_OFFSET 2
239 #define WAIT_PERIOD 20
240 #define REMOVE_WAIT_DELAY 10
241 #define QM_SQE_ADDR_MASK GENMASK(7, 0)
242 #define QM_EQ_DEPTH (1024 * 2)
244 #define QM_DRIVER_REMOVING 0
245 #define QM_RST_SCHED 1
246 #define QM_RESETTING 2
247 #define QM_QOS_PARAM_NUM 2
248 #define QM_QOS_VAL_NUM 1
249 #define QM_QOS_BDF_PARAM_NUM 4
250 #define QM_QOS_MAX_VAL 1000
251 #define QM_QOS_RATE 100
252 #define QM_QOS_EXPAND_RATE 1000
253 #define QM_SHAPER_CIR_B_MASK GENMASK(7, 0)
254 #define QM_SHAPER_CIR_U_MASK GENMASK(10, 8)
255 #define QM_SHAPER_CIR_S_MASK GENMASK(14, 11)
256 #define QM_SHAPER_FACTOR_CIR_U_SHIFT 8
257 #define QM_SHAPER_FACTOR_CIR_S_SHIFT 11
258 #define QM_SHAPER_FACTOR_CBS_B_SHIFT 15
259 #define QM_SHAPER_FACTOR_CBS_S_SHIFT 19
260 #define QM_SHAPER_CBS_B 1
261 #define QM_SHAPER_CBS_S 16
262 #define QM_SHAPER_VFT_OFFSET 6
263 #define WAIT_FOR_QOS_VF 100
264 #define QM_QOS_MIN_ERROR_RATE 5
265 #define QM_QOS_TYPICAL_NUM 8
266 #define QM_SHAPER_MIN_CBS_S 8
267 #define QM_QOS_TICK 0x300U
268 #define QM_QOS_DIVISOR_CLK 0x1f40U
269 #define QM_QOS_MAX_CIR_B 200
270 #define QM_QOS_MIN_CIR_B 100
271 #define QM_QOS_MAX_CIR_U 6
272 #define QM_QOS_MAX_CIR_S 11
273 #define QM_QOS_VAL_MAX_LEN 32
275 #define QM_AUTOSUSPEND_DELAY 3000
277 #define QM_MK_CQC_DW3_V1(hop_num, pg_sz, buf_sz, cqe_sz) \
278 (((hop_num) << QM_CQ_HOP_NUM_SHIFT) | \
279 ((pg_sz) << QM_CQ_PAGE_SIZE_SHIFT) | \
280 ((buf_sz) << QM_CQ_BUF_SIZE_SHIFT) | \
281 ((cqe_sz) << QM_CQ_CQE_SIZE_SHIFT))
283 #define QM_MK_CQC_DW3_V2(cqe_sz) \
284 ((QM_Q_DEPTH - 1) | ((cqe_sz) << QM_CQ_CQE_SIZE_SHIFT))
286 #define QM_MK_SQC_W13(priority, orders, alg_type) \
287 (((priority) << QM_SQ_PRIORITY_SHIFT) | \
288 ((orders) << QM_SQ_ORDERS_SHIFT) | \
289 (((alg_type) & QM_SQ_TYPE_MASK) << QM_SQ_TYPE_SHIFT))
291 #define QM_MK_SQC_DW3_V1(hop_num, pg_sz, buf_sz, sqe_sz) \
292 (((hop_num) << QM_SQ_HOP_NUM_SHIFT) | \
293 ((pg_sz) << QM_SQ_PAGE_SIZE_SHIFT) | \
294 ((buf_sz) << QM_SQ_BUF_SIZE_SHIFT) | \
295 ((u32)ilog2(sqe_sz) << QM_SQ_SQE_SIZE_SHIFT))
297 #define QM_MK_SQC_DW3_V2(sqe_sz) \
298 ((QM_Q_DEPTH - 1) | ((u32)ilog2(sqe_sz) << QM_SQ_SQE_SIZE_SHIFT))
300 #define INIT_QC_COMMON(qc, base, pasid) do { \
303 (qc)->base_l = cpu_to_le32(lower_32_bits(base)); \
304 (qc)->base_h = cpu_to_le32(upper_32_bits(base)); \
308 (qc)->pasid = cpu_to_le16(pasid); \
319 enum acc_err_result {
331 QM_PF_FLR_PREPARE = 0x01,
424 struct hisi_qm_resource {
427 struct list_head list;
430 struct hisi_qm_hw_ops {
431 int (*get_vft)(struct hisi_qm *qm, u32 *base, u32 *number);
432 void (*qm_db)(struct hisi_qm *qm, u16 qn,
433 u8 cmd, u16 index, u8 priority);
434 u32 (*get_irq_num)(struct hisi_qm *qm);
435 int (*debug_init)(struct hisi_qm *qm);
436 void (*hw_error_init)(struct hisi_qm *qm, u32 ce, u32 nfe, u32 fe);
437 void (*hw_error_uninit)(struct hisi_qm *qm);
438 enum acc_err_result (*hw_error_handle)(struct hisi_qm *qm);
439 int (*stop_qp)(struct hisi_qp *qp);
440 int (*set_msi)(struct hisi_qm *qm, bool set);
441 int (*ping_all_vfs)(struct hisi_qm *qm, u64 cmd);
442 int (*ping_pf)(struct hisi_qm *qm, u64 cmd);
450 static struct qm_dfx_item qm_dfx_files[] = {
451 {"err_irq", offsetof(struct qm_dfx, err_irq_cnt)},
452 {"aeq_irq", offsetof(struct qm_dfx, aeq_irq_cnt)},
453 {"abnormal_irq", offsetof(struct qm_dfx, abnormal_irq_cnt)},
454 {"create_qp_err", offsetof(struct qm_dfx, create_qp_err_cnt)},
455 {"mb_err", offsetof(struct qm_dfx, mb_err_cnt)},
458 static const char * const qm_debug_file_name[] = {
459 [CURRENT_QM] = "current_qm",
460 [CURRENT_Q] = "current_q",
461 [CLEAR_ENABLE] = "clear_enable",
464 struct hisi_qm_hw_error {
469 static const struct hisi_qm_hw_error qm_hw_error[] = {
470 { .int_msk = BIT(0), .msg = "qm_axi_rresp" },
471 { .int_msk = BIT(1), .msg = "qm_axi_bresp" },
472 { .int_msk = BIT(2), .msg = "qm_ecc_mbit" },
473 { .int_msk = BIT(3), .msg = "qm_ecc_1bit" },
474 { .int_msk = BIT(4), .msg = "qm_acc_get_task_timeout" },
475 { .int_msk = BIT(5), .msg = "qm_acc_do_task_timeout" },
476 { .int_msk = BIT(6), .msg = "qm_acc_wb_not_ready_timeout" },
477 { .int_msk = BIT(7), .msg = "qm_sq_cq_vf_invalid" },
478 { .int_msk = BIT(8), .msg = "qm_cq_vf_invalid" },
479 { .int_msk = BIT(9), .msg = "qm_sq_vf_invalid" },
480 { .int_msk = BIT(10), .msg = "qm_db_timeout" },
481 { .int_msk = BIT(11), .msg = "qm_of_fifo_of" },
482 { .int_msk = BIT(12), .msg = "qm_db_random_invalid" },
483 { .int_msk = BIT(13), .msg = "qm_mailbox_timeout" },
484 { .int_msk = BIT(14), .msg = "qm_flr_timeout" },
488 static const char * const qm_db_timeout[] = {
489 "sq", "cq", "eq", "aeq",
492 static const char * const qm_fifo_overflow[] = {
496 static const char * const qm_s[] = {
497 "init", "start", "close", "stop",
500 static const char * const qp_s[] = {
501 "none", "init", "start", "stop", "close",
504 static const u32 typical_qos_val[QM_QOS_TYPICAL_NUM] = {100, 250, 500, 1000,
505 10000, 25000, 50000, 100000};
506 static const u32 typical_qos_cbs_s[QM_QOS_TYPICAL_NUM] = {9, 10, 11, 12, 16,
509 static bool qm_avail_state(struct hisi_qm *qm, enum qm_state new)
511 enum qm_state curr = atomic_read(&qm->status.flags);
516 if (new == QM_START || new == QM_CLOSE)
524 if (new == QM_CLOSE || new == QM_START)
531 dev_dbg(&qm->pdev->dev, "change qm state from %s to %s\n",
532 qm_s[curr], qm_s[new]);
535 dev_warn(&qm->pdev->dev, "Can not change qm state from %s to %s\n",
536 qm_s[curr], qm_s[new]);
541 static bool qm_qp_avail_state(struct hisi_qm *qm, struct hisi_qp *qp,
544 enum qm_state qm_curr = atomic_read(&qm->status.flags);
545 enum qp_state qp_curr = 0;
549 qp_curr = atomic_read(&qp->qp_status.flags);
553 if (qm_curr == QM_START || qm_curr == QM_INIT)
557 if ((qm_curr == QM_START && qp_curr == QP_INIT) ||
558 (qm_curr == QM_START && qp_curr == QP_STOP))
562 if ((qm_curr == QM_START && qp_curr == QP_START) ||
563 (qp_curr == QP_INIT))
567 if ((qm_curr == QM_START && qp_curr == QP_INIT) ||
568 (qm_curr == QM_START && qp_curr == QP_STOP) ||
569 (qm_curr == QM_STOP && qp_curr == QP_STOP) ||
570 (qm_curr == QM_STOP && qp_curr == QP_INIT))
577 dev_dbg(&qm->pdev->dev, "change qp state from %s to %s in QM %s\n",
578 qp_s[qp_curr], qp_s[new], qm_s[qm_curr]);
581 dev_warn(&qm->pdev->dev,
582 "Can not change qp state from %s to %s in QM %s\n",
583 qp_s[qp_curr], qp_s[new], qm_s[qm_curr]);
588 static void qm_mb_pre_init(struct qm_mailbox *mailbox, u8 cmd,
589 u64 base, u16 queue, bool op)
591 mailbox->w0 = cpu_to_le16((cmd) |
592 ((op) ? 0x1 << QM_MB_OP_SHIFT : 0) |
593 (0x1 << QM_MB_BUSY_SHIFT));
594 mailbox->queue_num = cpu_to_le16(queue);
595 mailbox->base_l = cpu_to_le32(lower_32_bits(base));
596 mailbox->base_h = cpu_to_le32(upper_32_bits(base));
600 /* return 0 mailbox ready, -ETIMEDOUT hardware timeout */
601 static int qm_wait_mb_ready(struct hisi_qm *qm)
605 return readl_relaxed_poll_timeout(qm->io_base + QM_MB_CMD_SEND_BASE,
606 val, !((val >> QM_MB_BUSY_SHIFT) &
607 0x1), POLL_PERIOD, POLL_TIMEOUT);
610 /* 128 bit should be written to hardware at one time to trigger a mailbox */
611 static void qm_mb_write(struct hisi_qm *qm, const void *src)
613 void __iomem *fun_base = qm->io_base + QM_MB_CMD_SEND_BASE;
614 unsigned long tmp0 = 0, tmp1 = 0;
616 if (!IS_ENABLED(CONFIG_ARM64)) {
617 memcpy_toio(fun_base, src, 16);
622 asm volatile("ldp %0, %1, %3\n"
627 "+Q" (*((char __iomem *)fun_base))
628 : "Q" (*((char *)src))
632 static int qm_mb_nolock(struct hisi_qm *qm, struct qm_mailbox *mailbox)
634 if (unlikely(qm_wait_mb_ready(qm))) {
635 dev_err(&qm->pdev->dev, "QM mailbox is busy to start!\n");
639 qm_mb_write(qm, mailbox);
641 if (unlikely(qm_wait_mb_ready(qm))) {
642 dev_err(&qm->pdev->dev, "QM mailbox operation timeout!\n");
649 atomic64_inc(&qm->debug.dfx.mb_err_cnt);
653 static int qm_mb(struct hisi_qm *qm, u8 cmd, dma_addr_t dma_addr, u16 queue,
656 struct qm_mailbox mailbox;
659 dev_dbg(&qm->pdev->dev, "QM mailbox request to q%u: %u-%llx\n",
660 queue, cmd, (unsigned long long)dma_addr);
662 qm_mb_pre_init(&mailbox, cmd, dma_addr, queue, op);
664 mutex_lock(&qm->mailbox_lock);
665 ret = qm_mb_nolock(qm, &mailbox);
666 mutex_unlock(&qm->mailbox_lock);
671 static void qm_db_v1(struct hisi_qm *qm, u16 qn, u8 cmd, u16 index, u8 priority)
675 doorbell = qn | ((u64)cmd << QM_DB_CMD_SHIFT_V1) |
676 ((u64)index << QM_DB_INDEX_SHIFT_V1) |
677 ((u64)priority << QM_DB_PRIORITY_SHIFT_V1);
679 writeq(doorbell, qm->io_base + QM_DOORBELL_BASE_V1);
682 static void qm_db_v2(struct hisi_qm *qm, u16 qn, u8 cmd, u16 index, u8 priority)
684 void __iomem *io_base = qm->io_base;
688 if (cmd == QM_DOORBELL_CMD_SQ || cmd == QM_DOORBELL_CMD_CQ)
689 io_base = qm->db_io_base + (u64)qn * qm->db_interval +
690 QM_DOORBELL_SQ_CQ_BASE_V2;
692 io_base += QM_DOORBELL_EQ_AEQ_BASE_V2;
694 doorbell = qn | ((u64)cmd << QM_DB_CMD_SHIFT_V2) |
695 ((u64)randata << QM_DB_RAND_SHIFT_V2) |
696 ((u64)index << QM_DB_INDEX_SHIFT_V2) |
697 ((u64)priority << QM_DB_PRIORITY_SHIFT_V2);
699 writeq(doorbell, io_base);
702 static void qm_db(struct hisi_qm *qm, u16 qn, u8 cmd, u16 index, u8 priority)
704 dev_dbg(&qm->pdev->dev, "QM doorbell request: qn=%u, cmd=%u, index=%u\n",
707 qm->ops->qm_db(qm, qn, cmd, index, priority);
710 static int qm_dev_mem_reset(struct hisi_qm *qm)
714 writel(0x1, qm->io_base + QM_MEM_START_INIT);
715 return readl_relaxed_poll_timeout(qm->io_base + QM_MEM_INIT_DONE, val,
716 val & BIT(0), POLL_PERIOD,
720 static u32 qm_get_irq_num_v1(struct hisi_qm *qm)
722 return QM_IRQ_NUM_V1;
725 static u32 qm_get_irq_num_v2(struct hisi_qm *qm)
727 if (qm->fun_type == QM_HW_PF)
728 return QM_IRQ_NUM_PF_V2;
730 return QM_IRQ_NUM_VF_V2;
733 static u32 qm_get_irq_num_v3(struct hisi_qm *qm)
735 if (qm->fun_type == QM_HW_PF)
736 return QM_IRQ_NUM_PF_V2;
738 return QM_IRQ_NUM_VF_V3;
741 static int qm_pm_get_sync(struct hisi_qm *qm)
743 struct device *dev = &qm->pdev->dev;
746 if (qm->fun_type == QM_HW_VF || qm->ver < QM_HW_V3)
749 ret = pm_runtime_resume_and_get(dev);
751 dev_err(dev, "failed to get_sync(%d).\n", ret);
758 static void qm_pm_put_sync(struct hisi_qm *qm)
760 struct device *dev = &qm->pdev->dev;
762 if (qm->fun_type == QM_HW_VF || qm->ver < QM_HW_V3)
765 pm_runtime_mark_last_busy(dev);
766 pm_runtime_put_autosuspend(dev);
769 static struct hisi_qp *qm_to_hisi_qp(struct hisi_qm *qm, struct qm_eqe *eqe)
771 u16 cqn = le32_to_cpu(eqe->dw0) & QM_EQE_CQN_MASK;
773 return &qm->qp_array[cqn];
776 static void qm_cq_head_update(struct hisi_qp *qp)
778 if (qp->qp_status.cq_head == QM_Q_DEPTH - 1) {
779 qp->qp_status.cqc_phase = !qp->qp_status.cqc_phase;
780 qp->qp_status.cq_head = 0;
782 qp->qp_status.cq_head++;
786 static void qm_poll_qp(struct hisi_qp *qp, struct hisi_qm *qm)
788 if (unlikely(atomic_read(&qp->qp_status.flags) == QP_STOP))
797 struct qm_cqe *cqe = qp->cqe + qp->qp_status.cq_head;
799 while (QM_CQE_PHASE(cqe) == qp->qp_status.cqc_phase) {
801 qp->req_cb(qp, qp->sqe + qm->sqe_size *
802 le16_to_cpu(cqe->sq_head));
803 qm_cq_head_update(qp);
804 cqe = qp->cqe + qp->qp_status.cq_head;
805 qm_db(qm, qp->qp_id, QM_DOORBELL_CMD_CQ,
806 qp->qp_status.cq_head, 0);
807 atomic_dec(&qp->qp_status.used);
811 qm_db(qm, qp->qp_id, QM_DOORBELL_CMD_CQ,
812 qp->qp_status.cq_head, 1);
816 static void qm_work_process(struct work_struct *work)
818 struct hisi_qm *qm = container_of(work, struct hisi_qm, work);
819 struct qm_eqe *eqe = qm->eqe + qm->status.eq_head;
823 while (QM_EQE_PHASE(eqe) == qm->status.eqc_phase) {
825 qp = qm_to_hisi_qp(qm, eqe);
828 if (qm->status.eq_head == QM_EQ_DEPTH - 1) {
829 qm->status.eqc_phase = !qm->status.eqc_phase;
831 qm->status.eq_head = 0;
834 qm->status.eq_head++;
837 if (eqe_num == QM_EQ_DEPTH / 2 - 1) {
839 qm_db(qm, 0, QM_DOORBELL_CMD_EQ, qm->status.eq_head, 0);
843 qm_db(qm, 0, QM_DOORBELL_CMD_EQ, qm->status.eq_head, 0);
846 static irqreturn_t do_qm_irq(int irq, void *data)
848 struct hisi_qm *qm = (struct hisi_qm *)data;
850 /* the workqueue created by device driver of QM */
852 queue_work(qm->wq, &qm->work);
854 schedule_work(&qm->work);
859 static irqreturn_t qm_irq(int irq, void *data)
861 struct hisi_qm *qm = data;
863 if (readl(qm->io_base + QM_VF_EQ_INT_SOURCE))
864 return do_qm_irq(irq, data);
866 atomic64_inc(&qm->debug.dfx.err_irq_cnt);
867 dev_err(&qm->pdev->dev, "invalid int source\n");
868 qm_db(qm, 0, QM_DOORBELL_CMD_EQ, qm->status.eq_head, 0);
873 static irqreturn_t qm_mb_cmd_irq(int irq, void *data)
875 struct hisi_qm *qm = data;
878 val = readl(qm->io_base + QM_IFC_INT_STATUS);
879 val &= QM_IFC_INT_STATUS_MASK;
883 schedule_work(&qm->cmd_process);
888 static void qm_set_qp_disable(struct hisi_qp *qp, int offset)
892 if (qp->is_in_kernel)
895 addr = (u32 *)(qp->qdma.va + qp->qdma.size) - offset;
898 /* make sure setup is completed */
902 static irqreturn_t qm_aeq_irq(int irq, void *data)
904 struct hisi_qm *qm = data;
905 struct qm_aeqe *aeqe = qm->aeqe + qm->status.aeq_head;
908 atomic64_inc(&qm->debug.dfx.aeq_irq_cnt);
909 if (!readl(qm->io_base + QM_VF_AEQ_INT_SOURCE))
912 while (QM_AEQE_PHASE(aeqe) == qm->status.aeqc_phase) {
913 type = le32_to_cpu(aeqe->dw0) >> QM_AEQE_TYPE_SHIFT;
914 if (type < ARRAY_SIZE(qm_fifo_overflow))
915 dev_err(&qm->pdev->dev, "%s overflow\n",
916 qm_fifo_overflow[type]);
918 dev_err(&qm->pdev->dev, "unknown error type %u\n",
921 if (qm->status.aeq_head == QM_Q_DEPTH - 1) {
922 qm->status.aeqc_phase = !qm->status.aeqc_phase;
924 qm->status.aeq_head = 0;
927 qm->status.aeq_head++;
930 qm_db(qm, 0, QM_DOORBELL_CMD_AEQ, qm->status.aeq_head, 0);
936 static void qm_irq_unregister(struct hisi_qm *qm)
938 struct pci_dev *pdev = qm->pdev;
940 free_irq(pci_irq_vector(pdev, QM_EQ_EVENT_IRQ_VECTOR), qm);
942 if (qm->ver > QM_HW_V1) {
943 free_irq(pci_irq_vector(pdev, QM_AEQ_EVENT_IRQ_VECTOR), qm);
945 if (qm->fun_type == QM_HW_PF)
946 free_irq(pci_irq_vector(pdev,
947 QM_ABNORMAL_EVENT_IRQ_VECTOR), qm);
950 if (qm->ver > QM_HW_V2)
951 free_irq(pci_irq_vector(pdev, QM_CMD_EVENT_IRQ_VECTOR), qm);
954 static void qm_init_qp_status(struct hisi_qp *qp)
956 struct hisi_qp_status *qp_status = &qp->qp_status;
958 qp_status->sq_tail = 0;
959 qp_status->cq_head = 0;
960 qp_status->cqc_phase = true;
961 atomic_set(&qp_status->used, 0);
964 static void qm_init_prefetch(struct hisi_qm *qm)
966 struct device *dev = &qm->pdev->dev;
969 if (qm->ver < QM_HW_V3)
983 dev_err(dev, "system page size is not support: %lu, default set to 4KB",
987 writel(page_type, qm->io_base + QM_PAGE_SIZE);
992 * IR = X Mbps if ir = 1 means IR = 100 Mbps, if ir = 10000 means = 10Gbps
994 * IR_b * (2 ^ IR_u) * 8
995 * IR(Mbps) * 10 ^ -3 = -------------------------
998 static u32 acc_shaper_para_calc(u64 cir_b, u64 cir_u, u64 cir_s)
1000 return ((cir_b * QM_QOS_DIVISOR_CLK) * (1 << cir_u)) /
1001 (QM_QOS_TICK * (1 << cir_s));
1004 static u32 acc_shaper_calc_cbs_s(u32 ir)
1008 if (ir < typical_qos_val[0])
1009 return QM_SHAPER_MIN_CBS_S;
1011 for (i = 1; i < QM_QOS_TYPICAL_NUM; i++) {
1012 if (ir >= typical_qos_val[i - 1] && ir < typical_qos_val[i])
1013 return typical_qos_cbs_s[i - 1];
1016 return typical_qos_cbs_s[QM_QOS_TYPICAL_NUM - 1];
1019 static int qm_get_shaper_para(u32 ir, struct qm_shaper_factor *factor)
1021 u32 cir_b, cir_u, cir_s, ir_calc;
1024 factor->cbs_s = acc_shaper_calc_cbs_s(ir);
1026 for (cir_b = QM_QOS_MIN_CIR_B; cir_b <= QM_QOS_MAX_CIR_B; cir_b++) {
1027 for (cir_u = 0; cir_u <= QM_QOS_MAX_CIR_U; cir_u++) {
1028 for (cir_s = 0; cir_s <= QM_QOS_MAX_CIR_S; cir_s++) {
1029 /** the formula is changed to:
1030 * IR_b * (2 ^ IR_u) * DIVISOR_CLK
1031 * IR(Mbps) = -------------------------
1034 ir_calc = acc_shaper_para_calc(cir_b, cir_u,
1036 error_rate = QM_QOS_EXPAND_RATE * (u32)abs(ir_calc - ir) / ir;
1037 if (error_rate <= QM_QOS_MIN_ERROR_RATE) {
1038 factor->cir_b = cir_b;
1039 factor->cir_u = cir_u;
1040 factor->cir_s = cir_s;
1051 static void qm_vft_data_cfg(struct hisi_qm *qm, enum vft_type type, u32 base,
1052 u32 number, struct qm_shaper_factor *factor)
1059 if (qm->ver == QM_HW_V1) {
1060 tmp = QM_SQC_VFT_BUF_SIZE |
1061 QM_SQC_VFT_SQC_SIZE |
1062 QM_SQC_VFT_INDEX_NUMBER |
1064 (u64)base << QM_SQC_VFT_START_SQN_SHIFT;
1066 tmp = (u64)base << QM_SQC_VFT_START_SQN_SHIFT |
1068 (u64)(number - 1) << QM_SQC_VFT_SQN_SHIFT;
1072 if (qm->ver == QM_HW_V1) {
1073 tmp = QM_CQC_VFT_BUF_SIZE |
1074 QM_CQC_VFT_SQC_SIZE |
1075 QM_CQC_VFT_INDEX_NUMBER |
1078 tmp = QM_CQC_VFT_VALID;
1082 if (qm->ver >= QM_HW_V3) {
1083 tmp = factor->cir_b |
1084 (factor->cir_u << QM_SHAPER_FACTOR_CIR_U_SHIFT) |
1085 (factor->cir_s << QM_SHAPER_FACTOR_CIR_S_SHIFT) |
1086 (QM_SHAPER_CBS_B << QM_SHAPER_FACTOR_CBS_B_SHIFT) |
1087 (factor->cbs_s << QM_SHAPER_FACTOR_CBS_S_SHIFT);
1093 writel(lower_32_bits(tmp), qm->io_base + QM_VFT_CFG_DATA_L);
1094 writel(upper_32_bits(tmp), qm->io_base + QM_VFT_CFG_DATA_H);
1097 static int qm_set_vft_common(struct hisi_qm *qm, enum vft_type type,
1098 u32 fun_num, u32 base, u32 number)
1100 struct qm_shaper_factor *factor = &qm->factor[fun_num];
1104 ret = readl_relaxed_poll_timeout(qm->io_base + QM_VFT_CFG_RDY, val,
1105 val & BIT(0), POLL_PERIOD,
1110 writel(0x0, qm->io_base + QM_VFT_CFG_OP_WR);
1111 writel(type, qm->io_base + QM_VFT_CFG_TYPE);
1112 if (type == SHAPER_VFT)
1113 fun_num |= base << QM_SHAPER_VFT_OFFSET;
1115 writel(fun_num, qm->io_base + QM_VFT_CFG);
1117 qm_vft_data_cfg(qm, type, base, number, factor);
1119 writel(0x0, qm->io_base + QM_VFT_CFG_RDY);
1120 writel(0x1, qm->io_base + QM_VFT_CFG_OP_ENABLE);
1122 return readl_relaxed_poll_timeout(qm->io_base + QM_VFT_CFG_RDY, val,
1123 val & BIT(0), POLL_PERIOD,
1127 static int qm_shaper_init_vft(struct hisi_qm *qm, u32 fun_num)
1131 qm->factor[fun_num].func_qos = QM_QOS_MAX_VAL;
1132 ret = qm_get_shaper_para(QM_QOS_MAX_VAL * QM_QOS_RATE, &qm->factor[fun_num]);
1134 dev_err(&qm->pdev->dev, "failed to calculate shaper parameter!\n");
1137 writel(qm->type_rate, qm->io_base + QM_SHAPER_CFG);
1138 for (i = ALG_TYPE_0; i <= ALG_TYPE_1; i++) {
1139 /* The base number of queue reuse for different alg type */
1140 ret = qm_set_vft_common(qm, SHAPER_VFT, fun_num, i, 1);
1148 /* The config should be conducted after qm_dev_mem_reset() */
1149 static int qm_set_sqc_cqc_vft(struct hisi_qm *qm, u32 fun_num, u32 base,
1154 for (i = SQC_VFT; i <= CQC_VFT; i++) {
1155 ret = qm_set_vft_common(qm, i, fun_num, base, number);
1160 /* init default shaper qos val */
1161 if (qm->ver >= QM_HW_V3) {
1162 ret = qm_shaper_init_vft(qm, fun_num);
1169 for (i = SQC_VFT; i <= CQC_VFT; i++) {
1170 ret = qm_set_vft_common(qm, i, fun_num, 0, 0);
1177 static int qm_get_vft_v2(struct hisi_qm *qm, u32 *base, u32 *number)
1182 ret = qm_mb(qm, QM_MB_CMD_SQC_VFT_V2, 0, 0, 1);
1186 sqc_vft = readl(qm->io_base + QM_MB_CMD_DATA_ADDR_L) |
1187 ((u64)readl(qm->io_base + QM_MB_CMD_DATA_ADDR_H) << 32);
1188 *base = QM_SQC_VFT_BASE_MASK_V2 & (sqc_vft >> QM_SQC_VFT_BASE_SHIFT_V2);
1189 *number = (QM_SQC_VFT_NUM_MASK_v2 &
1190 (sqc_vft >> QM_SQC_VFT_NUM_SHIFT_V2)) + 1;
1195 static int qm_get_vf_qp_num(struct hisi_qm *qm, u32 fun_num)
1197 u32 remain_q_num, vfq_num;
1198 u32 num_vfs = qm->vfs_num;
1200 vfq_num = (qm->ctrl_qp_num - qm->qp_num) / num_vfs;
1201 if (vfq_num >= qm->max_qp_num)
1202 return qm->max_qp_num;
1204 remain_q_num = (qm->ctrl_qp_num - qm->qp_num) % num_vfs;
1205 if (vfq_num + remain_q_num <= qm->max_qp_num)
1206 return fun_num == num_vfs ? vfq_num + remain_q_num : vfq_num;
1209 * if vfq_num + remain_q_num > max_qp_num, the last VFs,
1210 * each with one more queue.
1212 return fun_num + remain_q_num > num_vfs ? vfq_num + 1 : vfq_num;
1215 static struct hisi_qm *file_to_qm(struct debugfs_file *file)
1217 struct qm_debug *debug = file->debug;
1219 return container_of(debug, struct hisi_qm, debug);
1222 static u32 current_q_read(struct hisi_qm *qm)
1224 return readl(qm->io_base + QM_DFX_SQE_CNT_VF_SQN) >> QM_DFX_QN_SHIFT;
1227 static int current_q_write(struct hisi_qm *qm, u32 val)
1231 if (val >= qm->debug.curr_qm_qp_num)
1234 tmp = val << QM_DFX_QN_SHIFT |
1235 (readl(qm->io_base + QM_DFX_SQE_CNT_VF_SQN) & CURRENT_FUN_MASK);
1236 writel(tmp, qm->io_base + QM_DFX_SQE_CNT_VF_SQN);
1238 tmp = val << QM_DFX_QN_SHIFT |
1239 (readl(qm->io_base + QM_DFX_CQE_CNT_VF_CQN) & CURRENT_FUN_MASK);
1240 writel(tmp, qm->io_base + QM_DFX_CQE_CNT_VF_CQN);
1245 static u32 clear_enable_read(struct hisi_qm *qm)
1247 return readl(qm->io_base + QM_DFX_CNT_CLR_CE);
1250 /* rd_clr_ctrl 1 enable read clear, otherwise 0 disable it */
1251 static int clear_enable_write(struct hisi_qm *qm, u32 rd_clr_ctrl)
1253 if (rd_clr_ctrl > 1)
1256 writel(rd_clr_ctrl, qm->io_base + QM_DFX_CNT_CLR_CE);
1261 static u32 current_qm_read(struct hisi_qm *qm)
1263 return readl(qm->io_base + QM_DFX_MB_CNT_VF);
1266 static int current_qm_write(struct hisi_qm *qm, u32 val)
1270 if (val > qm->vfs_num)
1273 /* According PF or VF Dev ID to calculation curr_qm_qp_num and store */
1275 qm->debug.curr_qm_qp_num = qm->qp_num;
1277 qm->debug.curr_qm_qp_num = qm_get_vf_qp_num(qm, val);
1279 writel(val, qm->io_base + QM_DFX_MB_CNT_VF);
1280 writel(val, qm->io_base + QM_DFX_DB_CNT_VF);
1283 (readl(qm->io_base + QM_DFX_SQE_CNT_VF_SQN) & CURRENT_Q_MASK);
1284 writel(tmp, qm->io_base + QM_DFX_SQE_CNT_VF_SQN);
1287 (readl(qm->io_base + QM_DFX_CQE_CNT_VF_CQN) & CURRENT_Q_MASK);
1288 writel(tmp, qm->io_base + QM_DFX_CQE_CNT_VF_CQN);
1293 static ssize_t qm_debug_read(struct file *filp, char __user *buf,
1294 size_t count, loff_t *pos)
1296 struct debugfs_file *file = filp->private_data;
1297 enum qm_debug_file index = file->index;
1298 struct hisi_qm *qm = file_to_qm(file);
1299 char tbuf[QM_DBG_TMP_BUF_LEN];
1303 ret = hisi_qm_get_dfx_access(qm);
1307 mutex_lock(&file->lock);
1310 val = current_qm_read(qm);
1313 val = current_q_read(qm);
1316 val = clear_enable_read(qm);
1321 mutex_unlock(&file->lock);
1323 hisi_qm_put_dfx_access(qm);
1324 ret = scnprintf(tbuf, QM_DBG_TMP_BUF_LEN, "%u\n", val);
1325 return simple_read_from_buffer(buf, count, pos, tbuf, ret);
1328 mutex_unlock(&file->lock);
1329 hisi_qm_put_dfx_access(qm);
1333 static ssize_t qm_debug_write(struct file *filp, const char __user *buf,
1334 size_t count, loff_t *pos)
1336 struct debugfs_file *file = filp->private_data;
1337 enum qm_debug_file index = file->index;
1338 struct hisi_qm *qm = file_to_qm(file);
1340 char tbuf[QM_DBG_TMP_BUF_LEN];
1346 if (count >= QM_DBG_TMP_BUF_LEN)
1349 len = simple_write_to_buffer(tbuf, QM_DBG_TMP_BUF_LEN - 1, pos, buf,
1355 if (kstrtoul(tbuf, 0, &val))
1358 ret = hisi_qm_get_dfx_access(qm);
1362 mutex_lock(&file->lock);
1365 ret = current_qm_write(qm, val);
1368 ret = current_q_write(qm, val);
1371 ret = clear_enable_write(qm, val);
1376 mutex_unlock(&file->lock);
1378 hisi_qm_put_dfx_access(qm);
1386 static const struct file_operations qm_debug_fops = {
1387 .owner = THIS_MODULE,
1388 .open = simple_open,
1389 .read = qm_debug_read,
1390 .write = qm_debug_write,
1393 #define CNT_CYC_REGS_NUM 10
1394 static const struct debugfs_reg32 qm_dfx_regs[] = {
1395 /* XXX_CNT are reading clear register */
1396 {"QM_ECC_1BIT_CNT ", 0x104000ull},
1397 {"QM_ECC_MBIT_CNT ", 0x104008ull},
1398 {"QM_DFX_MB_CNT ", 0x104018ull},
1399 {"QM_DFX_DB_CNT ", 0x104028ull},
1400 {"QM_DFX_SQE_CNT ", 0x104038ull},
1401 {"QM_DFX_CQE_CNT ", 0x104048ull},
1402 {"QM_DFX_SEND_SQE_TO_ACC_CNT ", 0x104050ull},
1403 {"QM_DFX_WB_SQE_FROM_ACC_CNT ", 0x104058ull},
1404 {"QM_DFX_ACC_FINISH_CNT ", 0x104060ull},
1405 {"QM_DFX_CQE_ERR_CNT ", 0x1040b4ull},
1406 {"QM_DFX_FUNS_ACTIVE_ST ", 0x200ull},
1407 {"QM_ECC_1BIT_INF ", 0x104004ull},
1408 {"QM_ECC_MBIT_INF ", 0x10400cull},
1409 {"QM_DFX_ACC_RDY_VLD0 ", 0x1040a0ull},
1410 {"QM_DFX_ACC_RDY_VLD1 ", 0x1040a4ull},
1411 {"QM_DFX_AXI_RDY_VLD ", 0x1040a8ull},
1412 {"QM_DFX_FF_ST0 ", 0x1040c8ull},
1413 {"QM_DFX_FF_ST1 ", 0x1040ccull},
1414 {"QM_DFX_FF_ST2 ", 0x1040d0ull},
1415 {"QM_DFX_FF_ST3 ", 0x1040d4ull},
1416 {"QM_DFX_FF_ST4 ", 0x1040d8ull},
1417 {"QM_DFX_FF_ST5 ", 0x1040dcull},
1418 {"QM_DFX_FF_ST6 ", 0x1040e0ull},
1419 {"QM_IN_IDLE_ST ", 0x1040e4ull},
1422 static const struct debugfs_reg32 qm_vf_dfx_regs[] = {
1423 {"QM_DFX_FUNS_ACTIVE_ST ", 0x200ull},
1427 * hisi_qm_regs_dump() - Dump registers's value.
1428 * @s: debugfs file handle.
1429 * @regset: accelerator registers information.
1431 * Dump accelerator registers.
1433 void hisi_qm_regs_dump(struct seq_file *s, struct debugfs_regset32 *regset)
1435 struct pci_dev *pdev = to_pci_dev(regset->dev);
1436 struct hisi_qm *qm = pci_get_drvdata(pdev);
1437 const struct debugfs_reg32 *regs = regset->regs;
1438 int regs_len = regset->nregs;
1442 ret = hisi_qm_get_dfx_access(qm);
1446 for (i = 0; i < regs_len; i++) {
1447 val = readl(regset->base + regs[i].offset);
1448 seq_printf(s, "%s= 0x%08x\n", regs[i].name, val);
1451 hisi_qm_put_dfx_access(qm);
1453 EXPORT_SYMBOL_GPL(hisi_qm_regs_dump);
1455 static int qm_regs_show(struct seq_file *s, void *unused)
1457 struct hisi_qm *qm = s->private;
1458 struct debugfs_regset32 regset;
1460 if (qm->fun_type == QM_HW_PF) {
1461 regset.regs = qm_dfx_regs;
1462 regset.nregs = ARRAY_SIZE(qm_dfx_regs);
1464 regset.regs = qm_vf_dfx_regs;
1465 regset.nregs = ARRAY_SIZE(qm_vf_dfx_regs);
1468 regset.base = qm->io_base;
1469 regset.dev = &qm->pdev->dev;
1471 hisi_qm_regs_dump(s, ®set);
1476 DEFINE_SHOW_ATTRIBUTE(qm_regs);
1478 static ssize_t qm_cmd_read(struct file *filp, char __user *buffer,
1479 size_t count, loff_t *pos)
1481 char buf[QM_DBG_READ_LEN];
1484 len = scnprintf(buf, QM_DBG_READ_LEN, "%s\n",
1485 "Please echo help to cmd to get help information");
1487 return simple_read_from_buffer(buffer, count, pos, buf, len);
1490 static void *qm_ctx_alloc(struct hisi_qm *qm, size_t ctx_size,
1491 dma_addr_t *dma_addr)
1493 struct device *dev = &qm->pdev->dev;
1496 ctx_addr = kzalloc(ctx_size, GFP_KERNEL);
1498 return ERR_PTR(-ENOMEM);
1500 *dma_addr = dma_map_single(dev, ctx_addr, ctx_size, DMA_FROM_DEVICE);
1501 if (dma_mapping_error(dev, *dma_addr)) {
1502 dev_err(dev, "DMA mapping error!\n");
1504 return ERR_PTR(-ENOMEM);
1510 static void qm_ctx_free(struct hisi_qm *qm, size_t ctx_size,
1511 const void *ctx_addr, dma_addr_t *dma_addr)
1513 struct device *dev = &qm->pdev->dev;
1515 dma_unmap_single(dev, *dma_addr, ctx_size, DMA_FROM_DEVICE);
1519 static int dump_show(struct hisi_qm *qm, void *info,
1520 unsigned int info_size, char *info_name)
1522 struct device *dev = &qm->pdev->dev;
1523 u8 *info_buf, *info_curr = info;
1525 #define BYTE_PER_DW 4
1527 info_buf = kzalloc(info_size, GFP_KERNEL);
1531 for (i = 0; i < info_size; i++, info_curr++) {
1532 if (i % BYTE_PER_DW == 0)
1533 info_buf[i + 3UL] = *info_curr;
1534 else if (i % BYTE_PER_DW == 1)
1535 info_buf[i + 1UL] = *info_curr;
1536 else if (i % BYTE_PER_DW == 2)
1537 info_buf[i - 1] = *info_curr;
1538 else if (i % BYTE_PER_DW == 3)
1539 info_buf[i - 3] = *info_curr;
1542 dev_info(dev, "%s DUMP\n", info_name);
1543 for (i = 0; i < info_size; i += BYTE_PER_DW) {
1544 pr_info("DW%u: %02X%02X %02X%02X\n", i / BYTE_PER_DW,
1545 info_buf[i], info_buf[i + 1UL],
1546 info_buf[i + 2UL], info_buf[i + 3UL]);
1554 static int qm_dump_sqc_raw(struct hisi_qm *qm, dma_addr_t dma_addr, u16 qp_id)
1556 return qm_mb(qm, QM_MB_CMD_SQC, dma_addr, qp_id, 1);
1559 static int qm_dump_cqc_raw(struct hisi_qm *qm, dma_addr_t dma_addr, u16 qp_id)
1561 return qm_mb(qm, QM_MB_CMD_CQC, dma_addr, qp_id, 1);
1564 static int qm_sqc_dump(struct hisi_qm *qm, const char *s)
1566 struct device *dev = &qm->pdev->dev;
1567 struct qm_sqc *sqc, *sqc_curr;
1575 ret = kstrtou32(s, 0, &qp_id);
1576 if (ret || qp_id >= qm->qp_num) {
1577 dev_err(dev, "Please input qp num (0-%u)", qm->qp_num - 1);
1581 sqc = qm_ctx_alloc(qm, sizeof(*sqc), &sqc_dma);
1583 return PTR_ERR(sqc);
1585 ret = qm_dump_sqc_raw(qm, sqc_dma, qp_id);
1587 down_read(&qm->qps_lock);
1589 sqc_curr = qm->sqc + qp_id;
1591 ret = dump_show(qm, sqc_curr, sizeof(*sqc),
1594 dev_info(dev, "Show soft sqc failed!\n");
1596 up_read(&qm->qps_lock);
1601 ret = dump_show(qm, sqc, sizeof(*sqc), "SQC");
1603 dev_info(dev, "Show hw sqc failed!\n");
1606 qm_ctx_free(qm, sizeof(*sqc), sqc, &sqc_dma);
1610 static int qm_cqc_dump(struct hisi_qm *qm, const char *s)
1612 struct device *dev = &qm->pdev->dev;
1613 struct qm_cqc *cqc, *cqc_curr;
1621 ret = kstrtou32(s, 0, &qp_id);
1622 if (ret || qp_id >= qm->qp_num) {
1623 dev_err(dev, "Please input qp num (0-%u)", qm->qp_num - 1);
1627 cqc = qm_ctx_alloc(qm, sizeof(*cqc), &cqc_dma);
1629 return PTR_ERR(cqc);
1631 ret = qm_dump_cqc_raw(qm, cqc_dma, qp_id);
1633 down_read(&qm->qps_lock);
1635 cqc_curr = qm->cqc + qp_id;
1637 ret = dump_show(qm, cqc_curr, sizeof(*cqc),
1640 dev_info(dev, "Show soft cqc failed!\n");
1642 up_read(&qm->qps_lock);
1647 ret = dump_show(qm, cqc, sizeof(*cqc), "CQC");
1649 dev_info(dev, "Show hw cqc failed!\n");
1652 qm_ctx_free(qm, sizeof(*cqc), cqc, &cqc_dma);
1656 static int qm_eqc_aeqc_dump(struct hisi_qm *qm, char *s, size_t size,
1657 int cmd, char *name)
1659 struct device *dev = &qm->pdev->dev;
1660 dma_addr_t xeqc_dma;
1664 if (strsep(&s, " ")) {
1665 dev_err(dev, "Please do not input extra characters!\n");
1669 xeqc = qm_ctx_alloc(qm, size, &xeqc_dma);
1671 return PTR_ERR(xeqc);
1673 ret = qm_mb(qm, cmd, xeqc_dma, 0, 1);
1677 ret = dump_show(qm, xeqc, size, name);
1679 dev_info(dev, "Show hw %s failed!\n", name);
1682 qm_ctx_free(qm, size, xeqc, &xeqc_dma);
1686 static int q_dump_param_parse(struct hisi_qm *qm, char *s,
1687 u32 *e_id, u32 *q_id)
1689 struct device *dev = &qm->pdev->dev;
1690 unsigned int qp_num = qm->qp_num;
1694 presult = strsep(&s, " ");
1696 dev_err(dev, "Please input qp number!\n");
1700 ret = kstrtou32(presult, 0, q_id);
1701 if (ret || *q_id >= qp_num) {
1702 dev_err(dev, "Please input qp num (0-%u)", qp_num - 1);
1706 presult = strsep(&s, " ");
1708 dev_err(dev, "Please input sqe number!\n");
1712 ret = kstrtou32(presult, 0, e_id);
1713 if (ret || *e_id >= QM_Q_DEPTH) {
1714 dev_err(dev, "Please input sqe num (0-%d)", QM_Q_DEPTH - 1);
1718 if (strsep(&s, " ")) {
1719 dev_err(dev, "Please do not input extra characters!\n");
1726 static int qm_sq_dump(struct hisi_qm *qm, char *s)
1728 struct device *dev = &qm->pdev->dev;
1729 void *sqe, *sqe_curr;
1734 ret = q_dump_param_parse(qm, s, &sqe_id, &qp_id);
1738 sqe = kzalloc(qm->sqe_size * QM_Q_DEPTH, GFP_KERNEL);
1742 qp = &qm->qp_array[qp_id];
1743 memcpy(sqe, qp->sqe, qm->sqe_size * QM_Q_DEPTH);
1744 sqe_curr = sqe + (u32)(sqe_id * qm->sqe_size);
1745 memset(sqe_curr + qm->debug.sqe_mask_offset, QM_SQE_ADDR_MASK,
1746 qm->debug.sqe_mask_len);
1748 ret = dump_show(qm, sqe_curr, qm->sqe_size, "SQE");
1750 dev_info(dev, "Show sqe failed!\n");
1757 static int qm_cq_dump(struct hisi_qm *qm, char *s)
1759 struct device *dev = &qm->pdev->dev;
1760 struct qm_cqe *cqe_curr;
1765 ret = q_dump_param_parse(qm, s, &cqe_id, &qp_id);
1769 qp = &qm->qp_array[qp_id];
1770 cqe_curr = qp->cqe + cqe_id;
1771 ret = dump_show(qm, cqe_curr, sizeof(struct qm_cqe), "CQE");
1773 dev_info(dev, "Show cqe failed!\n");
1778 static int qm_eq_aeq_dump(struct hisi_qm *qm, const char *s,
1779 size_t size, char *name)
1781 struct device *dev = &qm->pdev->dev;
1789 ret = kstrtou32(s, 0, &xeqe_id);
1793 if (!strcmp(name, "EQE") && xeqe_id >= QM_EQ_DEPTH) {
1794 dev_err(dev, "Please input eqe num (0-%d)", QM_EQ_DEPTH - 1);
1796 } else if (!strcmp(name, "AEQE") && xeqe_id >= QM_Q_DEPTH) {
1797 dev_err(dev, "Please input aeqe num (0-%d)", QM_Q_DEPTH - 1);
1801 down_read(&qm->qps_lock);
1803 if (qm->eqe && !strcmp(name, "EQE")) {
1804 xeqe = qm->eqe + xeqe_id;
1805 } else if (qm->aeqe && !strcmp(name, "AEQE")) {
1806 xeqe = qm->aeqe + xeqe_id;
1812 ret = dump_show(qm, xeqe, size, name);
1814 dev_info(dev, "Show %s failed!\n", name);
1817 up_read(&qm->qps_lock);
1821 static int qm_dbg_help(struct hisi_qm *qm, char *s)
1823 struct device *dev = &qm->pdev->dev;
1825 if (strsep(&s, " ")) {
1826 dev_err(dev, "Please do not input extra characters!\n");
1830 dev_info(dev, "available commands:\n");
1831 dev_info(dev, "sqc <num>\n");
1832 dev_info(dev, "cqc <num>\n");
1833 dev_info(dev, "eqc\n");
1834 dev_info(dev, "aeqc\n");
1835 dev_info(dev, "sq <num> <e>\n");
1836 dev_info(dev, "cq <num> <e>\n");
1837 dev_info(dev, "eq <e>\n");
1838 dev_info(dev, "aeq <e>\n");
1843 static int qm_cmd_write_dump(struct hisi_qm *qm, const char *cmd_buf)
1845 struct device *dev = &qm->pdev->dev;
1846 char *presult, *s, *s_tmp;
1849 s = kstrdup(cmd_buf, GFP_KERNEL);
1854 presult = strsep(&s, " ");
1857 goto err_buffer_free;
1860 if (!strcmp(presult, "sqc"))
1861 ret = qm_sqc_dump(qm, s);
1862 else if (!strcmp(presult, "cqc"))
1863 ret = qm_cqc_dump(qm, s);
1864 else if (!strcmp(presult, "eqc"))
1865 ret = qm_eqc_aeqc_dump(qm, s, sizeof(struct qm_eqc),
1866 QM_MB_CMD_EQC, "EQC");
1867 else if (!strcmp(presult, "aeqc"))
1868 ret = qm_eqc_aeqc_dump(qm, s, sizeof(struct qm_aeqc),
1869 QM_MB_CMD_AEQC, "AEQC");
1870 else if (!strcmp(presult, "sq"))
1871 ret = qm_sq_dump(qm, s);
1872 else if (!strcmp(presult, "cq"))
1873 ret = qm_cq_dump(qm, s);
1874 else if (!strcmp(presult, "eq"))
1875 ret = qm_eq_aeq_dump(qm, s, sizeof(struct qm_eqe), "EQE");
1876 else if (!strcmp(presult, "aeq"))
1877 ret = qm_eq_aeq_dump(qm, s, sizeof(struct qm_aeqe), "AEQE");
1878 else if (!strcmp(presult, "help"))
1879 ret = qm_dbg_help(qm, s);
1884 dev_info(dev, "Please echo help\n");
1892 static ssize_t qm_cmd_write(struct file *filp, const char __user *buffer,
1893 size_t count, loff_t *pos)
1895 struct hisi_qm *qm = filp->private_data;
1896 char *cmd_buf, *cmd_buf_tmp;
1902 ret = hisi_qm_get_dfx_access(qm);
1906 /* Judge if the instance is being reset. */
1907 if (unlikely(atomic_read(&qm->status.flags) == QM_STOP))
1910 if (count > QM_DBG_WRITE_LEN) {
1912 goto put_dfx_access;
1915 cmd_buf = memdup_user_nul(buffer, count);
1916 if (IS_ERR(cmd_buf)) {
1917 ret = PTR_ERR(cmd_buf);
1918 goto put_dfx_access;
1921 cmd_buf_tmp = strchr(cmd_buf, '\n');
1923 *cmd_buf_tmp = '\0';
1924 count = cmd_buf_tmp - cmd_buf + 1;
1927 ret = qm_cmd_write_dump(qm, cmd_buf);
1930 goto put_dfx_access;
1938 hisi_qm_put_dfx_access(qm);
1942 static const struct file_operations qm_cmd_fops = {
1943 .owner = THIS_MODULE,
1944 .open = simple_open,
1945 .read = qm_cmd_read,
1946 .write = qm_cmd_write,
1949 static void qm_create_debugfs_file(struct hisi_qm *qm, struct dentry *dir,
1950 enum qm_debug_file index)
1952 struct debugfs_file *file = qm->debug.files + index;
1954 debugfs_create_file(qm_debug_file_name[index], 0600, dir, file,
1957 file->index = index;
1958 mutex_init(&file->lock);
1959 file->debug = &qm->debug;
1962 static void qm_hw_error_init_v1(struct hisi_qm *qm, u32 ce, u32 nfe, u32 fe)
1964 writel(QM_ABNORMAL_INT_MASK_VALUE, qm->io_base + QM_ABNORMAL_INT_MASK);
1967 static void qm_hw_error_cfg(struct hisi_qm *qm, u32 ce, u32 nfe, u32 fe)
1969 qm->error_mask = ce | nfe | fe;
1970 /* clear QM hw residual error source */
1971 writel(QM_ABNORMAL_INT_SOURCE_CLR,
1972 qm->io_base + QM_ABNORMAL_INT_SOURCE);
1974 /* configure error type */
1975 writel(ce, qm->io_base + QM_RAS_CE_ENABLE);
1976 writel(QM_RAS_CE_TIMES_PER_IRQ, qm->io_base + QM_RAS_CE_THRESHOLD);
1977 writel(nfe, qm->io_base + QM_RAS_NFE_ENABLE);
1978 writel(fe, qm->io_base + QM_RAS_FE_ENABLE);
1981 static void qm_hw_error_init_v2(struct hisi_qm *qm, u32 ce, u32 nfe, u32 fe)
1983 u32 irq_enable = ce | nfe | fe;
1984 u32 irq_unmask = ~irq_enable;
1986 qm_hw_error_cfg(qm, ce, nfe, fe);
1988 irq_unmask &= readl(qm->io_base + QM_ABNORMAL_INT_MASK);
1989 writel(irq_unmask, qm->io_base + QM_ABNORMAL_INT_MASK);
1992 static void qm_hw_error_uninit_v2(struct hisi_qm *qm)
1994 writel(QM_ABNORMAL_INT_MASK_VALUE, qm->io_base + QM_ABNORMAL_INT_MASK);
1997 static void qm_hw_error_init_v3(struct hisi_qm *qm, u32 ce, u32 nfe, u32 fe)
1999 u32 irq_enable = ce | nfe | fe;
2000 u32 irq_unmask = ~irq_enable;
2002 qm_hw_error_cfg(qm, ce, nfe, fe);
2004 /* enable close master ooo when hardware error happened */
2005 writel(nfe & (~QM_DB_RANDOM_INVALID), qm->io_base + QM_OOO_SHUTDOWN_SEL);
2007 irq_unmask &= readl(qm->io_base + QM_ABNORMAL_INT_MASK);
2008 writel(irq_unmask, qm->io_base + QM_ABNORMAL_INT_MASK);
2011 static void qm_hw_error_uninit_v3(struct hisi_qm *qm)
2013 writel(QM_ABNORMAL_INT_MASK_VALUE, qm->io_base + QM_ABNORMAL_INT_MASK);
2015 /* disable close master ooo when hardware error happened */
2016 writel(0x0, qm->io_base + QM_OOO_SHUTDOWN_SEL);
2019 static void qm_log_hw_error(struct hisi_qm *qm, u32 error_status)
2021 const struct hisi_qm_hw_error *err;
2022 struct device *dev = &qm->pdev->dev;
2023 u32 reg_val, type, vf_num;
2026 for (i = 0; i < ARRAY_SIZE(qm_hw_error); i++) {
2027 err = &qm_hw_error[i];
2028 if (!(err->int_msk & error_status))
2031 dev_err(dev, "%s [error status=0x%x] found\n",
2032 err->msg, err->int_msk);
2034 if (err->int_msk & QM_DB_TIMEOUT) {
2035 reg_val = readl(qm->io_base + QM_ABNORMAL_INF01);
2036 type = (reg_val & QM_DB_TIMEOUT_TYPE) >>
2037 QM_DB_TIMEOUT_TYPE_SHIFT;
2038 vf_num = reg_val & QM_DB_TIMEOUT_VF;
2039 dev_err(dev, "qm %s doorbell timeout in function %u\n",
2040 qm_db_timeout[type], vf_num);
2041 } else if (err->int_msk & QM_OF_FIFO_OF) {
2042 reg_val = readl(qm->io_base + QM_ABNORMAL_INF00);
2043 type = (reg_val & QM_FIFO_OVERFLOW_TYPE) >>
2044 QM_FIFO_OVERFLOW_TYPE_SHIFT;
2045 vf_num = reg_val & QM_FIFO_OVERFLOW_VF;
2047 if (type < ARRAY_SIZE(qm_fifo_overflow))
2048 dev_err(dev, "qm %s fifo overflow in function %u\n",
2049 qm_fifo_overflow[type], vf_num);
2051 dev_err(dev, "unknown error type\n");
2056 static enum acc_err_result qm_hw_error_handle_v2(struct hisi_qm *qm)
2058 u32 error_status, tmp, val;
2061 tmp = readl(qm->io_base + QM_ABNORMAL_INT_STATUS);
2062 error_status = qm->error_mask & tmp;
2065 if (error_status & QM_ECC_MBIT)
2066 qm->err_status.is_qm_ecc_mbit = true;
2068 qm_log_hw_error(qm, error_status);
2069 val = error_status | QM_DB_RANDOM_INVALID | QM_BASE_CE;
2070 /* ce error does not need to be reset */
2071 if (val == (QM_DB_RANDOM_INVALID | QM_BASE_CE)) {
2072 writel(error_status, qm->io_base +
2073 QM_ABNORMAL_INT_SOURCE);
2074 writel(qm->err_info.nfe,
2075 qm->io_base + QM_RAS_NFE_ENABLE);
2076 return ACC_ERR_RECOVERED;
2079 return ACC_ERR_NEED_RESET;
2082 return ACC_ERR_RECOVERED;
2085 static u32 qm_get_hw_error_status(struct hisi_qm *qm)
2087 return readl(qm->io_base + QM_ABNORMAL_INT_STATUS);
2090 static u32 qm_get_dev_err_status(struct hisi_qm *qm)
2092 return qm->err_ini->get_dev_hw_err_status(qm);
2095 /* Check if the error causes the master ooo block */
2096 static int qm_check_dev_error(struct hisi_qm *qm)
2100 if (qm->fun_type == QM_HW_VF)
2103 val = qm_get_hw_error_status(qm);
2104 dev_val = qm_get_dev_err_status(qm);
2106 if (qm->ver < QM_HW_V3)
2107 return (val & QM_ECC_MBIT) ||
2108 (dev_val & qm->err_info.ecc_2bits_mask);
2110 return (val & readl(qm->io_base + QM_OOO_SHUTDOWN_SEL)) ||
2111 (dev_val & (~qm->err_info.dev_ce_mask));
2114 static int qm_get_mb_cmd(struct hisi_qm *qm, u64 *msg, u16 fun_num)
2116 struct qm_mailbox mailbox;
2119 qm_mb_pre_init(&mailbox, QM_MB_CMD_DST, 0, fun_num, 0);
2120 mutex_lock(&qm->mailbox_lock);
2121 ret = qm_mb_nolock(qm, &mailbox);
2125 *msg = readl(qm->io_base + QM_MB_CMD_DATA_ADDR_L) |
2126 ((u64)readl(qm->io_base + QM_MB_CMD_DATA_ADDR_H) << 32);
2129 mutex_unlock(&qm->mailbox_lock);
2133 static void qm_clear_cmd_interrupt(struct hisi_qm *qm, u64 vf_mask)
2137 if (qm->fun_type == QM_HW_PF)
2138 writeq(vf_mask, qm->io_base + QM_IFC_INT_SOURCE_P);
2140 val = readl(qm->io_base + QM_IFC_INT_SOURCE_V);
2141 val |= QM_IFC_INT_SOURCE_MASK;
2142 writel(val, qm->io_base + QM_IFC_INT_SOURCE_V);
2145 static void qm_handle_vf_msg(struct hisi_qm *qm, u32 vf_id)
2147 struct device *dev = &qm->pdev->dev;
2152 ret = qm_get_mb_cmd(qm, &msg, vf_id);
2154 dev_err(dev, "failed to get msg from VF(%u)!\n", vf_id);
2158 cmd = msg & QM_MB_CMD_DATA_MASK;
2160 case QM_VF_PREPARE_FAIL:
2161 dev_err(dev, "failed to stop VF(%u)!\n", vf_id);
2163 case QM_VF_START_FAIL:
2164 dev_err(dev, "failed to start VF(%u)!\n", vf_id);
2166 case QM_VF_PREPARE_DONE:
2167 case QM_VF_START_DONE:
2170 dev_err(dev, "unsupported cmd %u sent by VF(%u)!\n", cmd, vf_id);
2175 static int qm_wait_vf_prepare_finish(struct hisi_qm *qm)
2177 struct device *dev = &qm->pdev->dev;
2178 u32 vfs_num = qm->vfs_num;
2184 if (!qm->vfs_num || qm->ver < QM_HW_V3)
2188 val = readq(qm->io_base + QM_IFC_INT_SOURCE_P);
2189 /* All VFs send command to PF, break */
2190 if ((val & GENMASK(vfs_num, 1)) == GENMASK(vfs_num, 1))
2193 if (++cnt > QM_MAX_PF_WAIT_COUNT) {
2198 msleep(QM_WAIT_DST_ACK);
2201 /* PF check VFs msg */
2202 for (i = 1; i <= vfs_num; i++) {
2204 qm_handle_vf_msg(qm, i);
2206 dev_err(dev, "VF(%u) not ping PF!\n", i);
2209 /* PF clear interrupt to ack VFs */
2210 qm_clear_cmd_interrupt(qm, val);
2215 static void qm_trigger_vf_interrupt(struct hisi_qm *qm, u32 fun_num)
2219 val = readl(qm->io_base + QM_IFC_INT_CFG);
2220 val &= ~QM_IFC_SEND_ALL_VFS;
2222 writel(val, qm->io_base + QM_IFC_INT_CFG);
2224 val = readl(qm->io_base + QM_IFC_INT_SET_P);
2225 val |= QM_IFC_INT_SET_MASK;
2226 writel(val, qm->io_base + QM_IFC_INT_SET_P);
2229 static void qm_trigger_pf_interrupt(struct hisi_qm *qm)
2233 val = readl(qm->io_base + QM_IFC_INT_SET_V);
2234 val |= QM_IFC_INT_SET_MASK;
2235 writel(val, qm->io_base + QM_IFC_INT_SET_V);
2238 static int qm_ping_single_vf(struct hisi_qm *qm, u64 cmd, u32 fun_num)
2240 struct device *dev = &qm->pdev->dev;
2241 struct qm_mailbox mailbox;
2246 qm_mb_pre_init(&mailbox, QM_MB_CMD_SRC, cmd, fun_num, 0);
2247 mutex_lock(&qm->mailbox_lock);
2248 ret = qm_mb_nolock(qm, &mailbox);
2250 dev_err(dev, "failed to send command to vf(%u)!\n", fun_num);
2254 qm_trigger_vf_interrupt(qm, fun_num);
2256 msleep(QM_WAIT_DST_ACK);
2257 val = readq(qm->io_base + QM_IFC_READY_STATUS);
2258 /* if VF respond, PF notifies VF successfully. */
2259 if (!(val & BIT(fun_num)))
2262 if (++cnt > QM_MAX_PF_WAIT_COUNT) {
2263 dev_err(dev, "failed to get response from VF(%u)!\n", fun_num);
2270 mutex_unlock(&qm->mailbox_lock);
2274 static int qm_ping_all_vfs(struct hisi_qm *qm, u64 cmd)
2276 struct device *dev = &qm->pdev->dev;
2277 u32 vfs_num = qm->vfs_num;
2278 struct qm_mailbox mailbox;
2284 qm_mb_pre_init(&mailbox, QM_MB_CMD_SRC, cmd, QM_MB_PING_ALL_VFS, 0);
2285 mutex_lock(&qm->mailbox_lock);
2286 /* PF sends command to all VFs by mailbox */
2287 ret = qm_mb_nolock(qm, &mailbox);
2289 dev_err(dev, "failed to send command to VFs!\n");
2290 mutex_unlock(&qm->mailbox_lock);
2294 qm_trigger_vf_interrupt(qm, QM_IFC_SEND_ALL_VFS);
2296 msleep(QM_WAIT_DST_ACK);
2297 val = readq(qm->io_base + QM_IFC_READY_STATUS);
2298 /* If all VFs acked, PF notifies VFs successfully. */
2299 if (!(val & GENMASK(vfs_num, 1))) {
2300 mutex_unlock(&qm->mailbox_lock);
2304 if (++cnt > QM_MAX_PF_WAIT_COUNT)
2308 mutex_unlock(&qm->mailbox_lock);
2310 /* Check which vf respond timeout. */
2311 for (i = 1; i <= vfs_num; i++) {
2313 dev_err(dev, "failed to get response from VF(%u)!\n", i);
2319 static int qm_ping_pf(struct hisi_qm *qm, u64 cmd)
2321 struct qm_mailbox mailbox;
2326 qm_mb_pre_init(&mailbox, QM_MB_CMD_SRC, cmd, 0, 0);
2327 mutex_lock(&qm->mailbox_lock);
2328 ret = qm_mb_nolock(qm, &mailbox);
2330 dev_err(&qm->pdev->dev, "failed to send command to PF!\n");
2334 qm_trigger_pf_interrupt(qm);
2335 /* Waiting for PF response */
2337 msleep(QM_WAIT_DST_ACK);
2338 val = readl(qm->io_base + QM_IFC_INT_SET_V);
2339 if (!(val & QM_IFC_INT_STATUS_MASK))
2342 if (++cnt > QM_MAX_VF_WAIT_COUNT) {
2349 mutex_unlock(&qm->mailbox_lock);
2353 static int qm_stop_qp(struct hisi_qp *qp)
2355 return qm_mb(qp->qm, QM_MB_CMD_STOP_QP, 0, qp->qp_id, 0);
2358 static int qm_set_msi(struct hisi_qm *qm, bool set)
2360 struct pci_dev *pdev = qm->pdev;
2363 pci_write_config_dword(pdev, pdev->msi_cap + PCI_MSI_MASK_64,
2366 pci_write_config_dword(pdev, pdev->msi_cap + PCI_MSI_MASK_64,
2367 ACC_PEH_MSI_DISABLE);
2368 if (qm->err_status.is_qm_ecc_mbit ||
2369 qm->err_status.is_dev_ecc_mbit)
2373 if (readl(qm->io_base + QM_PEH_DFX_INFO0))
2380 static void qm_wait_msi_finish(struct hisi_qm *qm)
2382 struct pci_dev *pdev = qm->pdev;
2389 pci_read_config_dword(pdev, pdev->msi_cap +
2390 PCI_MSI_PENDING_64, &cmd);
2394 if (++cnt > MAX_WAIT_COUNTS) {
2395 pci_warn(pdev, "failed to empty MSI PENDING!\n");
2402 ret = readl_relaxed_poll_timeout(qm->io_base + QM_PEH_DFX_INFO0,
2403 val, !(val & QM_PEH_DFX_MASK),
2404 POLL_PERIOD, POLL_TIMEOUT);
2406 pci_warn(pdev, "failed to empty PEH MSI!\n");
2408 ret = readl_relaxed_poll_timeout(qm->io_base + QM_PEH_DFX_INFO1,
2409 val, !(val & QM_PEH_MSI_FINISH_MASK),
2410 POLL_PERIOD, POLL_TIMEOUT);
2412 pci_warn(pdev, "failed to finish MSI operation!\n");
2415 static int qm_set_msi_v3(struct hisi_qm *qm, bool set)
2417 struct pci_dev *pdev = qm->pdev;
2418 int ret = -ETIMEDOUT;
2421 pci_read_config_dword(pdev, pdev->msi_cap, &cmd);
2423 cmd |= QM_MSI_CAP_ENABLE;
2425 cmd &= ~QM_MSI_CAP_ENABLE;
2427 pci_write_config_dword(pdev, pdev->msi_cap, cmd);
2429 for (i = 0; i < MAX_WAIT_COUNTS; i++) {
2430 pci_read_config_dword(pdev, pdev->msi_cap, &cmd);
2431 if (cmd & QM_MSI_CAP_ENABLE)
2437 udelay(WAIT_PERIOD_US_MIN);
2438 qm_wait_msi_finish(qm);
2445 static const struct hisi_qm_hw_ops qm_hw_ops_v1 = {
2447 .get_irq_num = qm_get_irq_num_v1,
2448 .hw_error_init = qm_hw_error_init_v1,
2449 .set_msi = qm_set_msi,
2452 static const struct hisi_qm_hw_ops qm_hw_ops_v2 = {
2453 .get_vft = qm_get_vft_v2,
2455 .get_irq_num = qm_get_irq_num_v2,
2456 .hw_error_init = qm_hw_error_init_v2,
2457 .hw_error_uninit = qm_hw_error_uninit_v2,
2458 .hw_error_handle = qm_hw_error_handle_v2,
2459 .set_msi = qm_set_msi,
2462 static const struct hisi_qm_hw_ops qm_hw_ops_v3 = {
2463 .get_vft = qm_get_vft_v2,
2465 .get_irq_num = qm_get_irq_num_v3,
2466 .hw_error_init = qm_hw_error_init_v3,
2467 .hw_error_uninit = qm_hw_error_uninit_v3,
2468 .hw_error_handle = qm_hw_error_handle_v2,
2469 .stop_qp = qm_stop_qp,
2470 .set_msi = qm_set_msi_v3,
2471 .ping_all_vfs = qm_ping_all_vfs,
2472 .ping_pf = qm_ping_pf,
2475 static void *qm_get_avail_sqe(struct hisi_qp *qp)
2477 struct hisi_qp_status *qp_status = &qp->qp_status;
2478 u16 sq_tail = qp_status->sq_tail;
2480 if (unlikely(atomic_read(&qp->qp_status.used) == QM_Q_DEPTH - 1))
2483 return qp->sqe + sq_tail * qp->qm->sqe_size;
2486 static void hisi_qm_unset_hw_reset(struct hisi_qp *qp)
2490 /* Use last 64 bits of DUS to reset status. */
2491 addr = (u64 *)(qp->qdma.va + qp->qdma.size) - QM_RESET_STOP_TX_OFFSET;
2495 static struct hisi_qp *qm_create_qp_nolock(struct hisi_qm *qm, u8 alg_type)
2497 struct device *dev = &qm->pdev->dev;
2501 if (!qm_qp_avail_state(qm, NULL, QP_INIT))
2502 return ERR_PTR(-EPERM);
2504 if (qm->qp_in_used == qm->qp_num) {
2505 dev_info_ratelimited(dev, "All %u queues of QM are busy!\n",
2507 atomic64_inc(&qm->debug.dfx.create_qp_err_cnt);
2508 return ERR_PTR(-EBUSY);
2511 qp_id = idr_alloc_cyclic(&qm->qp_idr, NULL, 0, qm->qp_num, GFP_ATOMIC);
2513 dev_info_ratelimited(dev, "All %u queues of QM are busy!\n",
2515 atomic64_inc(&qm->debug.dfx.create_qp_err_cnt);
2516 return ERR_PTR(-EBUSY);
2519 qp = &qm->qp_array[qp_id];
2520 hisi_qm_unset_hw_reset(qp);
2521 memset(qp->cqe, 0, sizeof(struct qm_cqe) * QM_Q_DEPTH);
2523 qp->event_cb = NULL;
2526 qp->alg_type = alg_type;
2527 qp->is_in_kernel = true;
2529 atomic_set(&qp->qp_status.flags, QP_INIT);
2535 * hisi_qm_create_qp() - Create a queue pair from qm.
2536 * @qm: The qm we create a qp from.
2537 * @alg_type: Accelerator specific algorithm type in sqc.
2539 * return created qp, -EBUSY if all qps in qm allocated, -ENOMEM if allocating
2542 struct hisi_qp *hisi_qm_create_qp(struct hisi_qm *qm, u8 alg_type)
2547 ret = qm_pm_get_sync(qm);
2549 return ERR_PTR(ret);
2551 down_write(&qm->qps_lock);
2552 qp = qm_create_qp_nolock(qm, alg_type);
2553 up_write(&qm->qps_lock);
2560 EXPORT_SYMBOL_GPL(hisi_qm_create_qp);
2563 * hisi_qm_release_qp() - Release a qp back to its qm.
2564 * @qp: The qp we want to release.
2566 * This function releases the resource of a qp.
2568 void hisi_qm_release_qp(struct hisi_qp *qp)
2570 struct hisi_qm *qm = qp->qm;
2572 down_write(&qm->qps_lock);
2574 if (!qm_qp_avail_state(qm, qp, QP_CLOSE)) {
2575 up_write(&qm->qps_lock);
2580 idr_remove(&qm->qp_idr, qp->qp_id);
2582 up_write(&qm->qps_lock);
2586 EXPORT_SYMBOL_GPL(hisi_qm_release_qp);
2588 static int qm_sq_ctx_cfg(struct hisi_qp *qp, int qp_id, u32 pasid)
2590 struct hisi_qm *qm = qp->qm;
2591 struct device *dev = &qm->pdev->dev;
2592 enum qm_hw_ver ver = qm->ver;
2597 sqc = kzalloc(sizeof(struct qm_sqc), GFP_KERNEL);
2601 INIT_QC_COMMON(sqc, qp->sqe_dma, pasid);
2602 if (ver == QM_HW_V1) {
2603 sqc->dw3 = cpu_to_le32(QM_MK_SQC_DW3_V1(0, 0, 0, qm->sqe_size));
2604 sqc->w8 = cpu_to_le16(QM_Q_DEPTH - 1);
2606 sqc->dw3 = cpu_to_le32(QM_MK_SQC_DW3_V2(qm->sqe_size));
2607 sqc->w8 = 0; /* rand_qc */
2609 sqc->cq_num = cpu_to_le16(qp_id);
2610 sqc->w13 = cpu_to_le16(QM_MK_SQC_W13(0, 1, qp->alg_type));
2612 if (ver >= QM_HW_V3 && qm->use_sva && !qp->is_in_kernel)
2613 sqc->w11 = cpu_to_le16(QM_QC_PASID_ENABLE <<
2614 QM_QC_PASID_ENABLE_SHIFT);
2616 sqc_dma = dma_map_single(dev, sqc, sizeof(struct qm_sqc),
2618 if (dma_mapping_error(dev, sqc_dma)) {
2623 ret = qm_mb(qm, QM_MB_CMD_SQC, sqc_dma, qp_id, 0);
2624 dma_unmap_single(dev, sqc_dma, sizeof(struct qm_sqc), DMA_TO_DEVICE);
2630 static int qm_cq_ctx_cfg(struct hisi_qp *qp, int qp_id, u32 pasid)
2632 struct hisi_qm *qm = qp->qm;
2633 struct device *dev = &qm->pdev->dev;
2634 enum qm_hw_ver ver = qm->ver;
2639 cqc = kzalloc(sizeof(struct qm_cqc), GFP_KERNEL);
2643 INIT_QC_COMMON(cqc, qp->cqe_dma, pasid);
2644 if (ver == QM_HW_V1) {
2645 cqc->dw3 = cpu_to_le32(QM_MK_CQC_DW3_V1(0, 0, 0,
2647 cqc->w8 = cpu_to_le16(QM_Q_DEPTH - 1);
2649 cqc->dw3 = cpu_to_le32(QM_MK_CQC_DW3_V2(QM_QC_CQE_SIZE));
2650 cqc->w8 = 0; /* rand_qc */
2652 cqc->dw6 = cpu_to_le32(1 << QM_CQ_PHASE_SHIFT | 1 << QM_CQ_FLAG_SHIFT);
2654 if (ver >= QM_HW_V3 && qm->use_sva && !qp->is_in_kernel)
2655 cqc->w11 = cpu_to_le16(QM_QC_PASID_ENABLE);
2657 cqc_dma = dma_map_single(dev, cqc, sizeof(struct qm_cqc),
2659 if (dma_mapping_error(dev, cqc_dma)) {
2664 ret = qm_mb(qm, QM_MB_CMD_CQC, cqc_dma, qp_id, 0);
2665 dma_unmap_single(dev, cqc_dma, sizeof(struct qm_cqc), DMA_TO_DEVICE);
2671 static int qm_qp_ctx_cfg(struct hisi_qp *qp, int qp_id, u32 pasid)
2675 qm_init_qp_status(qp);
2677 ret = qm_sq_ctx_cfg(qp, qp_id, pasid);
2681 return qm_cq_ctx_cfg(qp, qp_id, pasid);
2684 static int qm_start_qp_nolock(struct hisi_qp *qp, unsigned long arg)
2686 struct hisi_qm *qm = qp->qm;
2687 struct device *dev = &qm->pdev->dev;
2688 int qp_id = qp->qp_id;
2692 if (!qm_qp_avail_state(qm, qp, QP_START))
2695 ret = qm_qp_ctx_cfg(qp, qp_id, pasid);
2699 atomic_set(&qp->qp_status.flags, QP_START);
2700 dev_dbg(dev, "queue %d started\n", qp_id);
2706 * hisi_qm_start_qp() - Start a qp into running.
2707 * @qp: The qp we want to start to run.
2708 * @arg: Accelerator specific argument.
2710 * After this function, qp can receive request from user. Return 0 if
2711 * successful, Return -EBUSY if failed.
2713 int hisi_qm_start_qp(struct hisi_qp *qp, unsigned long arg)
2715 struct hisi_qm *qm = qp->qm;
2718 down_write(&qm->qps_lock);
2719 ret = qm_start_qp_nolock(qp, arg);
2720 up_write(&qm->qps_lock);
2724 EXPORT_SYMBOL_GPL(hisi_qm_start_qp);
2727 * qp_stop_fail_cb() - call request cb.
2728 * @qp: stopped failed qp.
2730 * Callback function should be called whether task completed or not.
2732 static void qp_stop_fail_cb(struct hisi_qp *qp)
2734 int qp_used = atomic_read(&qp->qp_status.used);
2735 u16 cur_tail = qp->qp_status.sq_tail;
2736 u16 cur_head = (cur_tail + QM_Q_DEPTH - qp_used) % QM_Q_DEPTH;
2737 struct hisi_qm *qm = qp->qm;
2741 for (i = 0; i < qp_used; i++) {
2742 pos = (i + cur_head) % QM_Q_DEPTH;
2743 qp->req_cb(qp, qp->sqe + (u32)(qm->sqe_size * pos));
2744 atomic_dec(&qp->qp_status.used);
2749 * qm_drain_qp() - Drain a qp.
2750 * @qp: The qp we want to drain.
2752 * Determine whether the queue is cleared by judging the tail pointers of
2755 static int qm_drain_qp(struct hisi_qp *qp)
2757 size_t size = sizeof(struct qm_sqc) + sizeof(struct qm_cqc);
2758 struct hisi_qm *qm = qp->qm;
2759 struct device *dev = &qm->pdev->dev;
2762 dma_addr_t dma_addr;
2766 /* No need to judge if master OOO is blocked. */
2767 if (qm_check_dev_error(qm))
2770 /* Kunpeng930 supports drain qp by device */
2771 if (qm->ops->stop_qp) {
2772 ret = qm->ops->stop_qp(qp);
2774 dev_err(dev, "Failed to stop qp(%u)!\n", qp->qp_id);
2778 addr = qm_ctx_alloc(qm, size, &dma_addr);
2780 dev_err(dev, "Failed to alloc ctx for sqc and cqc!\n");
2785 ret = qm_dump_sqc_raw(qm, dma_addr, qp->qp_id);
2787 dev_err_ratelimited(dev, "Failed to dump sqc!\n");
2792 ret = qm_dump_cqc_raw(qm, (dma_addr + sizeof(struct qm_sqc)),
2795 dev_err_ratelimited(dev, "Failed to dump cqc!\n");
2798 cqc = addr + sizeof(struct qm_sqc);
2800 if ((sqc->tail == cqc->tail) &&
2801 (QM_SQ_TAIL_IDX(sqc) == QM_CQ_TAIL_IDX(cqc)))
2804 if (i == MAX_WAIT_COUNTS) {
2805 dev_err(dev, "Fail to empty queue %u!\n", qp->qp_id);
2810 usleep_range(WAIT_PERIOD_US_MIN, WAIT_PERIOD_US_MAX);
2813 qm_ctx_free(qm, size, addr, &dma_addr);
2818 static int qm_stop_qp_nolock(struct hisi_qp *qp)
2820 struct device *dev = &qp->qm->pdev->dev;
2824 * It is allowed to stop and release qp when reset, If the qp is
2825 * stopped when reset but still want to be released then, the
2826 * is_resetting flag should be set negative so that this qp will not
2827 * be restarted after reset.
2829 if (atomic_read(&qp->qp_status.flags) == QP_STOP) {
2830 qp->is_resetting = false;
2834 if (!qm_qp_avail_state(qp->qm, qp, QP_STOP))
2837 atomic_set(&qp->qp_status.flags, QP_STOP);
2839 ret = qm_drain_qp(qp);
2841 dev_err(dev, "Failed to drain out data for stopping!\n");
2844 flush_workqueue(qp->qm->wq);
2846 flush_work(&qp->qm->work);
2848 if (unlikely(qp->is_resetting && atomic_read(&qp->qp_status.used)))
2849 qp_stop_fail_cb(qp);
2851 dev_dbg(dev, "stop queue %u!", qp->qp_id);
2857 * hisi_qm_stop_qp() - Stop a qp in qm.
2858 * @qp: The qp we want to stop.
2860 * This function is reverse of hisi_qm_start_qp. Return 0 if successful.
2862 int hisi_qm_stop_qp(struct hisi_qp *qp)
2866 down_write(&qp->qm->qps_lock);
2867 ret = qm_stop_qp_nolock(qp);
2868 up_write(&qp->qm->qps_lock);
2872 EXPORT_SYMBOL_GPL(hisi_qm_stop_qp);
2875 * hisi_qp_send() - Queue up a task in the hardware queue.
2876 * @qp: The qp in which to put the message.
2877 * @msg: The message.
2879 * This function will return -EBUSY if qp is currently full, and -EAGAIN
2880 * if qp related qm is resetting.
2882 * Note: This function may run with qm_irq_thread and ACC reset at same time.
2883 * It has no race with qm_irq_thread. However, during hisi_qp_send, ACC
2884 * reset may happen, we have no lock here considering performance. This
2885 * causes current qm_db sending fail or can not receive sended sqe. QM
2886 * sync/async receive function should handle the error sqe. ACC reset
2887 * done function should clear used sqe to 0.
2889 int hisi_qp_send(struct hisi_qp *qp, const void *msg)
2891 struct hisi_qp_status *qp_status = &qp->qp_status;
2892 u16 sq_tail = qp_status->sq_tail;
2893 u16 sq_tail_next = (sq_tail + 1) % QM_Q_DEPTH;
2894 void *sqe = qm_get_avail_sqe(qp);
2896 if (unlikely(atomic_read(&qp->qp_status.flags) == QP_STOP ||
2897 atomic_read(&qp->qm->status.flags) == QM_STOP ||
2898 qp->is_resetting)) {
2899 dev_info_ratelimited(&qp->qm->pdev->dev, "QP is stopped or resetting\n");
2906 memcpy(sqe, msg, qp->qm->sqe_size);
2908 qm_db(qp->qm, qp->qp_id, QM_DOORBELL_CMD_SQ, sq_tail_next, 0);
2909 atomic_inc(&qp->qp_status.used);
2910 qp_status->sq_tail = sq_tail_next;
2914 EXPORT_SYMBOL_GPL(hisi_qp_send);
2916 static void hisi_qm_cache_wb(struct hisi_qm *qm)
2920 if (qm->ver == QM_HW_V1)
2923 writel(0x1, qm->io_base + QM_CACHE_WB_START);
2924 if (readl_relaxed_poll_timeout(qm->io_base + QM_CACHE_WB_DONE,
2925 val, val & BIT(0), POLL_PERIOD,
2927 dev_err(&qm->pdev->dev, "QM writeback sqc cache fail!\n");
2930 static void qm_qp_event_notifier(struct hisi_qp *qp)
2932 wake_up_interruptible(&qp->uacce_q->wait);
2935 static int hisi_qm_get_available_instances(struct uacce_device *uacce)
2937 return hisi_qm_get_free_qp_num(uacce->priv);
2940 static void hisi_qm_set_hw_reset(struct hisi_qm *qm, int offset)
2944 for (i = 0; i < qm->qp_num; i++)
2945 qm_set_qp_disable(&qm->qp_array[i], offset);
2948 static int hisi_qm_uacce_get_queue(struct uacce_device *uacce,
2950 struct uacce_queue *q)
2952 struct hisi_qm *qm = uacce->priv;
2956 qp = hisi_qm_create_qp(qm, alg_type);
2963 qp->event_cb = qm_qp_event_notifier;
2965 qp->is_in_kernel = false;
2970 static void hisi_qm_uacce_put_queue(struct uacce_queue *q)
2972 struct hisi_qp *qp = q->priv;
2974 hisi_qm_cache_wb(qp->qm);
2975 hisi_qm_release_qp(qp);
2978 /* map sq/cq/doorbell to user space */
2979 static int hisi_qm_uacce_mmap(struct uacce_queue *q,
2980 struct vm_area_struct *vma,
2981 struct uacce_qfile_region *qfr)
2983 struct hisi_qp *qp = q->priv;
2984 struct hisi_qm *qm = qp->qm;
2985 resource_size_t phys_base = qm->db_phys_base +
2986 qp->qp_id * qm->db_interval;
2987 size_t sz = vma->vm_end - vma->vm_start;
2988 struct pci_dev *pdev = qm->pdev;
2989 struct device *dev = &pdev->dev;
2990 unsigned long vm_pgoff;
2993 switch (qfr->type) {
2994 case UACCE_QFRT_MMIO:
2995 if (qm->ver == QM_HW_V1) {
2996 if (sz > PAGE_SIZE * QM_DOORBELL_PAGE_NR)
2998 } else if (qm->ver == QM_HW_V2 || !qm->use_db_isolation) {
2999 if (sz > PAGE_SIZE * (QM_DOORBELL_PAGE_NR +
3000 QM_DOORBELL_SQ_CQ_BASE_V2 / PAGE_SIZE))
3003 if (sz > qm->db_interval)
3007 vma->vm_flags |= VM_IO;
3009 return remap_pfn_range(vma, vma->vm_start,
3010 phys_base >> PAGE_SHIFT,
3011 sz, pgprot_noncached(vma->vm_page_prot));
3012 case UACCE_QFRT_DUS:
3013 if (sz != qp->qdma.size)
3017 * dma_mmap_coherent() requires vm_pgoff as 0
3018 * restore vm_pfoff to initial value for mmap()
3020 vm_pgoff = vma->vm_pgoff;
3022 ret = dma_mmap_coherent(dev, vma, qp->qdma.va,
3024 vma->vm_pgoff = vm_pgoff;
3032 static int hisi_qm_uacce_start_queue(struct uacce_queue *q)
3034 struct hisi_qp *qp = q->priv;
3036 return hisi_qm_start_qp(qp, qp->pasid);
3039 static void hisi_qm_uacce_stop_queue(struct uacce_queue *q)
3041 hisi_qm_stop_qp(q->priv);
3044 static int hisi_qm_is_q_updated(struct uacce_queue *q)
3046 struct hisi_qp *qp = q->priv;
3047 struct qm_cqe *cqe = qp->cqe + qp->qp_status.cq_head;
3050 while (QM_CQE_PHASE(cqe) == qp->qp_status.cqc_phase) {
3051 /* make sure to read data from memory */
3053 qm_cq_head_update(qp);
3054 cqe = qp->cqe + qp->qp_status.cq_head;
3061 static void qm_set_sqctype(struct uacce_queue *q, u16 type)
3063 struct hisi_qm *qm = q->uacce->priv;
3064 struct hisi_qp *qp = q->priv;
3066 down_write(&qm->qps_lock);
3067 qp->alg_type = type;
3068 up_write(&qm->qps_lock);
3071 static long hisi_qm_uacce_ioctl(struct uacce_queue *q, unsigned int cmd,
3074 struct hisi_qp *qp = q->priv;
3075 struct hisi_qp_ctx qp_ctx;
3077 if (cmd == UACCE_CMD_QM_SET_QP_CTX) {
3078 if (copy_from_user(&qp_ctx, (void __user *)arg,
3079 sizeof(struct hisi_qp_ctx)))
3082 if (qp_ctx.qc_type != 0 && qp_ctx.qc_type != 1)
3085 qm_set_sqctype(q, qp_ctx.qc_type);
3086 qp_ctx.id = qp->qp_id;
3088 if (copy_to_user((void __user *)arg, &qp_ctx,
3089 sizeof(struct hisi_qp_ctx)))
3098 static const struct uacce_ops uacce_qm_ops = {
3099 .get_available_instances = hisi_qm_get_available_instances,
3100 .get_queue = hisi_qm_uacce_get_queue,
3101 .put_queue = hisi_qm_uacce_put_queue,
3102 .start_queue = hisi_qm_uacce_start_queue,
3103 .stop_queue = hisi_qm_uacce_stop_queue,
3104 .mmap = hisi_qm_uacce_mmap,
3105 .ioctl = hisi_qm_uacce_ioctl,
3106 .is_q_updated = hisi_qm_is_q_updated,
3109 static int qm_alloc_uacce(struct hisi_qm *qm)
3111 struct pci_dev *pdev = qm->pdev;
3112 struct uacce_device *uacce;
3113 unsigned long mmio_page_nr;
3114 unsigned long dus_page_nr;
3115 struct uacce_interface interface = {
3116 .flags = UACCE_DEV_SVA,
3117 .ops = &uacce_qm_ops,
3121 ret = strscpy(interface.name, dev_driver_string(&pdev->dev),
3122 sizeof(interface.name));
3124 return -ENAMETOOLONG;
3126 uacce = uacce_alloc(&pdev->dev, &interface);
3128 return PTR_ERR(uacce);
3130 if (uacce->flags & UACCE_DEV_SVA) {
3133 /* only consider sva case */
3134 uacce_remove(uacce);
3139 uacce->is_vf = pdev->is_virtfn;
3141 uacce->algs = qm->algs;
3143 if (qm->ver == QM_HW_V1)
3144 uacce->api_ver = HISI_QM_API_VER_BASE;
3145 else if (qm->ver == QM_HW_V2)
3146 uacce->api_ver = HISI_QM_API_VER2_BASE;
3148 uacce->api_ver = HISI_QM_API_VER3_BASE;
3150 if (qm->ver == QM_HW_V1)
3151 mmio_page_nr = QM_DOORBELL_PAGE_NR;
3152 else if (qm->ver == QM_HW_V2 || !qm->use_db_isolation)
3153 mmio_page_nr = QM_DOORBELL_PAGE_NR +
3154 QM_DOORBELL_SQ_CQ_BASE_V2 / PAGE_SIZE;
3156 mmio_page_nr = qm->db_interval / PAGE_SIZE;
3158 /* Add one more page for device or qp status */
3159 dus_page_nr = (PAGE_SIZE - 1 + qm->sqe_size * QM_Q_DEPTH +
3160 sizeof(struct qm_cqe) * QM_Q_DEPTH + PAGE_SIZE) >>
3163 uacce->qf_pg_num[UACCE_QFRT_MMIO] = mmio_page_nr;
3164 uacce->qf_pg_num[UACCE_QFRT_DUS] = dus_page_nr;
3172 * qm_frozen() - Try to froze QM to cut continuous queue request. If
3173 * there is user on the QM, return failure without doing anything.
3174 * @qm: The qm needed to be fronzen.
3176 * This function frozes QM, then we can do SRIOV disabling.
3178 static int qm_frozen(struct hisi_qm *qm)
3180 if (test_bit(QM_DRIVER_REMOVING, &qm->misc_ctl))
3183 down_write(&qm->qps_lock);
3185 if (!qm->qp_in_used) {
3186 qm->qp_in_used = qm->qp_num;
3187 up_write(&qm->qps_lock);
3188 set_bit(QM_DRIVER_REMOVING, &qm->misc_ctl);
3192 up_write(&qm->qps_lock);
3197 static int qm_try_frozen_vfs(struct pci_dev *pdev,
3198 struct hisi_qm_list *qm_list)
3200 struct hisi_qm *qm, *vf_qm;
3201 struct pci_dev *dev;
3204 if (!qm_list || !pdev)
3207 /* Try to frozen all the VFs as disable SRIOV */
3208 mutex_lock(&qm_list->lock);
3209 list_for_each_entry(qm, &qm_list->list, list) {
3213 if (pci_physfn(dev) == pdev) {
3214 vf_qm = pci_get_drvdata(dev);
3215 ret = qm_frozen(vf_qm);
3222 mutex_unlock(&qm_list->lock);
3228 * hisi_qm_wait_task_finish() - Wait until the task is finished
3229 * when removing the driver.
3230 * @qm: The qm needed to wait for the task to finish.
3231 * @qm_list: The list of all available devices.
3233 void hisi_qm_wait_task_finish(struct hisi_qm *qm, struct hisi_qm_list *qm_list)
3235 while (qm_frozen(qm) ||
3236 ((qm->fun_type == QM_HW_PF) &&
3237 qm_try_frozen_vfs(qm->pdev, qm_list))) {
3238 msleep(WAIT_PERIOD);
3241 while (test_bit(QM_RST_SCHED, &qm->misc_ctl) ||
3242 test_bit(QM_RESETTING, &qm->misc_ctl))
3243 msleep(WAIT_PERIOD);
3245 udelay(REMOVE_WAIT_DELAY);
3247 EXPORT_SYMBOL_GPL(hisi_qm_wait_task_finish);
3250 * hisi_qm_get_free_qp_num() - Get free number of qp in qm.
3251 * @qm: The qm which want to get free qp.
3253 * This function return free number of qp in qm.
3255 int hisi_qm_get_free_qp_num(struct hisi_qm *qm)
3259 down_read(&qm->qps_lock);
3260 ret = qm->qp_num - qm->qp_in_used;
3261 up_read(&qm->qps_lock);
3265 EXPORT_SYMBOL_GPL(hisi_qm_get_free_qp_num);
3267 static void hisi_qp_memory_uninit(struct hisi_qm *qm, int num)
3269 struct device *dev = &qm->pdev->dev;
3270 struct qm_dma *qdma;
3273 for (i = num - 1; i >= 0; i--) {
3274 qdma = &qm->qp_array[i].qdma;
3275 dma_free_coherent(dev, qdma->size, qdma->va, qdma->dma);
3278 kfree(qm->qp_array);
3281 static int hisi_qp_memory_init(struct hisi_qm *qm, size_t dma_size, int id)
3283 struct device *dev = &qm->pdev->dev;
3284 size_t off = qm->sqe_size * QM_Q_DEPTH;
3287 qp = &qm->qp_array[id];
3288 qp->qdma.va = dma_alloc_coherent(dev, dma_size, &qp->qdma.dma,
3293 qp->sqe = qp->qdma.va;
3294 qp->sqe_dma = qp->qdma.dma;
3295 qp->cqe = qp->qdma.va + off;
3296 qp->cqe_dma = qp->qdma.dma + off;
3297 qp->qdma.size = dma_size;
3304 static void hisi_qm_pre_init(struct hisi_qm *qm)
3306 struct pci_dev *pdev = qm->pdev;
3308 if (qm->ver == QM_HW_V1)
3309 qm->ops = &qm_hw_ops_v1;
3310 else if (qm->ver == QM_HW_V2)
3311 qm->ops = &qm_hw_ops_v2;
3313 qm->ops = &qm_hw_ops_v3;
3315 pci_set_drvdata(pdev, qm);
3316 mutex_init(&qm->mailbox_lock);
3317 init_rwsem(&qm->qps_lock);
3319 qm->misc_ctl = false;
3320 if (qm->fun_type == QM_HW_PF && qm->ver > QM_HW_V2) {
3321 if (!acpi_device_power_manageable(ACPI_COMPANION(&pdev->dev)))
3322 dev_info(&pdev->dev, "_PS0 and _PR0 are not defined");
3326 static void qm_cmd_uninit(struct hisi_qm *qm)
3330 if (qm->ver < QM_HW_V3)
3333 val = readl(qm->io_base + QM_IFC_INT_MASK);
3334 val |= QM_IFC_INT_DISABLE;
3335 writel(val, qm->io_base + QM_IFC_INT_MASK);
3338 static void qm_cmd_init(struct hisi_qm *qm)
3342 if (qm->ver < QM_HW_V3)
3345 /* Clear communication interrupt source */
3346 qm_clear_cmd_interrupt(qm, QM_IFC_INT_SOURCE_CLR);
3348 /* Enable pf to vf communication reg. */
3349 val = readl(qm->io_base + QM_IFC_INT_MASK);
3350 val &= ~QM_IFC_INT_DISABLE;
3351 writel(val, qm->io_base + QM_IFC_INT_MASK);
3354 static void qm_put_pci_res(struct hisi_qm *qm)
3356 struct pci_dev *pdev = qm->pdev;
3358 if (qm->use_db_isolation)
3359 iounmap(qm->db_io_base);
3361 iounmap(qm->io_base);
3362 pci_release_mem_regions(pdev);
3365 static void hisi_qm_pci_uninit(struct hisi_qm *qm)
3367 struct pci_dev *pdev = qm->pdev;
3369 pci_free_irq_vectors(pdev);
3371 pci_disable_device(pdev);
3375 * hisi_qm_uninit() - Uninitialize qm.
3376 * @qm: The qm needed uninit.
3378 * This function uninits qm related device resources.
3380 void hisi_qm_uninit(struct hisi_qm *qm)
3382 struct pci_dev *pdev = qm->pdev;
3383 struct device *dev = &pdev->dev;
3387 down_write(&qm->qps_lock);
3389 if (!qm_avail_state(qm, QM_CLOSE)) {
3390 up_write(&qm->qps_lock);
3394 hisi_qp_memory_uninit(qm, qm->qp_num);
3395 idr_destroy(&qm->qp_idr);
3398 hisi_qm_cache_wb(qm);
3399 dma_free_coherent(dev, qm->qdma.size,
3400 qm->qdma.va, qm->qdma.dma);
3403 qm_irq_unregister(qm);
3404 hisi_qm_pci_uninit(qm);
3406 uacce_remove(qm->uacce);
3410 up_write(&qm->qps_lock);
3412 EXPORT_SYMBOL_GPL(hisi_qm_uninit);
3415 * hisi_qm_get_vft() - Get vft from a qm.
3416 * @qm: The qm we want to get its vft.
3417 * @base: The base number of queue in vft.
3418 * @number: The number of queues in vft.
3420 * We can allocate multiple queues to a qm by configuring virtual function
3421 * table. We get related configures by this function. Normally, we call this
3422 * function in VF driver to get the queue information.
3424 * qm hw v1 does not support this interface.
3426 int hisi_qm_get_vft(struct hisi_qm *qm, u32 *base, u32 *number)
3428 if (!base || !number)
3431 if (!qm->ops->get_vft) {
3432 dev_err(&qm->pdev->dev, "Don't support vft read!\n");
3436 return qm->ops->get_vft(qm, base, number);
3438 EXPORT_SYMBOL_GPL(hisi_qm_get_vft);
3441 * hisi_qm_set_vft() - Set vft to a qm.
3442 * @qm: The qm we want to set its vft.
3443 * @fun_num: The function number.
3444 * @base: The base number of queue in vft.
3445 * @number: The number of queues in vft.
3447 * This function is alway called in PF driver, it is used to assign queues
3450 * Assign queues A~B to PF: hisi_qm_set_vft(qm, 0, A, B - A + 1)
3451 * Assign queues A~B to VF: hisi_qm_set_vft(qm, 2, A, B - A + 1)
3452 * (VF function number 0x2)
3454 static int hisi_qm_set_vft(struct hisi_qm *qm, u32 fun_num, u32 base,
3457 u32 max_q_num = qm->ctrl_qp_num;
3459 if (base >= max_q_num || number > max_q_num ||
3460 (base + number) > max_q_num)
3463 return qm_set_sqc_cqc_vft(qm, fun_num, base, number);
3466 static void qm_init_eq_aeq_status(struct hisi_qm *qm)
3468 struct hisi_qm_status *status = &qm->status;
3470 status->eq_head = 0;
3471 status->aeq_head = 0;
3472 status->eqc_phase = true;
3473 status->aeqc_phase = true;
3476 static int qm_eq_ctx_cfg(struct hisi_qm *qm)
3478 struct device *dev = &qm->pdev->dev;
3483 eqc = kzalloc(sizeof(struct qm_eqc), GFP_KERNEL);
3487 eqc->base_l = cpu_to_le32(lower_32_bits(qm->eqe_dma));
3488 eqc->base_h = cpu_to_le32(upper_32_bits(qm->eqe_dma));
3489 if (qm->ver == QM_HW_V1)
3490 eqc->dw3 = cpu_to_le32(QM_EQE_AEQE_SIZE);
3491 eqc->dw6 = cpu_to_le32((QM_EQ_DEPTH - 1) | (1 << QM_EQC_PHASE_SHIFT));
3493 eqc_dma = dma_map_single(dev, eqc, sizeof(struct qm_eqc),
3495 if (dma_mapping_error(dev, eqc_dma)) {
3500 ret = qm_mb(qm, QM_MB_CMD_EQC, eqc_dma, 0, 0);
3501 dma_unmap_single(dev, eqc_dma, sizeof(struct qm_eqc), DMA_TO_DEVICE);
3507 static int qm_aeq_ctx_cfg(struct hisi_qm *qm)
3509 struct device *dev = &qm->pdev->dev;
3510 struct qm_aeqc *aeqc;
3511 dma_addr_t aeqc_dma;
3514 aeqc = kzalloc(sizeof(struct qm_aeqc), GFP_KERNEL);
3518 aeqc->base_l = cpu_to_le32(lower_32_bits(qm->aeqe_dma));
3519 aeqc->base_h = cpu_to_le32(upper_32_bits(qm->aeqe_dma));
3520 aeqc->dw6 = cpu_to_le32((QM_Q_DEPTH - 1) | (1 << QM_EQC_PHASE_SHIFT));
3522 aeqc_dma = dma_map_single(dev, aeqc, sizeof(struct qm_aeqc),
3524 if (dma_mapping_error(dev, aeqc_dma)) {
3529 ret = qm_mb(qm, QM_MB_CMD_AEQC, aeqc_dma, 0, 0);
3530 dma_unmap_single(dev, aeqc_dma, sizeof(struct qm_aeqc), DMA_TO_DEVICE);
3536 static int qm_eq_aeq_ctx_cfg(struct hisi_qm *qm)
3538 struct device *dev = &qm->pdev->dev;
3541 qm_init_eq_aeq_status(qm);
3543 ret = qm_eq_ctx_cfg(qm);
3545 dev_err(dev, "Set eqc failed!\n");
3549 return qm_aeq_ctx_cfg(qm);
3552 static int __hisi_qm_start(struct hisi_qm *qm)
3556 WARN_ON(!qm->qdma.va);
3558 if (qm->fun_type == QM_HW_PF) {
3559 ret = qm_dev_mem_reset(qm);
3563 ret = hisi_qm_set_vft(qm, 0, qm->qp_base, qm->qp_num);
3568 ret = qm_eq_aeq_ctx_cfg(qm);
3572 ret = qm_mb(qm, QM_MB_CMD_SQC_BT, qm->sqc_dma, 0, 0);
3576 ret = qm_mb(qm, QM_MB_CMD_CQC_BT, qm->cqc_dma, 0, 0);
3580 qm_init_prefetch(qm);
3582 writel(0x0, qm->io_base + QM_VF_EQ_INT_MASK);
3583 writel(0x0, qm->io_base + QM_VF_AEQ_INT_MASK);
3589 * hisi_qm_start() - start qm
3590 * @qm: The qm to be started.
3592 * This function starts a qm, then we can allocate qp from this qm.
3594 int hisi_qm_start(struct hisi_qm *qm)
3596 struct device *dev = &qm->pdev->dev;
3599 down_write(&qm->qps_lock);
3601 if (!qm_avail_state(qm, QM_START)) {
3602 up_write(&qm->qps_lock);
3606 dev_dbg(dev, "qm start with %u queue pairs\n", qm->qp_num);
3609 dev_err(dev, "qp_num should not be 0\n");
3614 ret = __hisi_qm_start(qm);
3616 atomic_set(&qm->status.flags, QM_START);
3619 up_write(&qm->qps_lock);
3622 EXPORT_SYMBOL_GPL(hisi_qm_start);
3624 static int qm_restart(struct hisi_qm *qm)
3626 struct device *dev = &qm->pdev->dev;
3630 ret = hisi_qm_start(qm);
3634 down_write(&qm->qps_lock);
3635 for (i = 0; i < qm->qp_num; i++) {
3636 qp = &qm->qp_array[i];
3637 if (atomic_read(&qp->qp_status.flags) == QP_STOP &&
3638 qp->is_resetting == true) {
3639 ret = qm_start_qp_nolock(qp, 0);
3641 dev_err(dev, "Failed to start qp%d!\n", i);
3643 up_write(&qm->qps_lock);
3646 qp->is_resetting = false;
3649 up_write(&qm->qps_lock);
3654 /* Stop started qps in reset flow */
3655 static int qm_stop_started_qp(struct hisi_qm *qm)
3657 struct device *dev = &qm->pdev->dev;
3661 for (i = 0; i < qm->qp_num; i++) {
3662 qp = &qm->qp_array[i];
3663 if (qp && atomic_read(&qp->qp_status.flags) == QP_START) {
3664 qp->is_resetting = true;
3665 ret = qm_stop_qp_nolock(qp);
3667 dev_err(dev, "Failed to stop qp%d!\n", i);
3678 * qm_clear_queues() - Clear all queues memory in a qm.
3679 * @qm: The qm in which the queues will be cleared.
3681 * This function clears all queues memory in a qm. Reset of accelerator can
3682 * use this to clear queues.
3684 static void qm_clear_queues(struct hisi_qm *qm)
3689 for (i = 0; i < qm->qp_num; i++) {
3690 qp = &qm->qp_array[i];
3691 if (qp->is_resetting)
3692 memset(qp->qdma.va, 0, qp->qdma.size);
3695 memset(qm->qdma.va, 0, qm->qdma.size);
3699 * hisi_qm_stop() - Stop a qm.
3700 * @qm: The qm which will be stopped.
3701 * @r: The reason to stop qm.
3703 * This function stops qm and its qps, then qm can not accept request.
3704 * Related resources are not released at this state, we can use hisi_qm_start
3705 * to let qm start again.
3707 int hisi_qm_stop(struct hisi_qm *qm, enum qm_stop_reason r)
3709 struct device *dev = &qm->pdev->dev;
3712 down_write(&qm->qps_lock);
3714 qm->status.stop_reason = r;
3715 if (!qm_avail_state(qm, QM_STOP)) {
3720 if (qm->status.stop_reason == QM_SOFT_RESET ||
3721 qm->status.stop_reason == QM_FLR) {
3722 hisi_qm_set_hw_reset(qm, QM_RESET_STOP_TX_OFFSET);
3723 ret = qm_stop_started_qp(qm);
3725 dev_err(dev, "Failed to stop started qp!\n");
3728 hisi_qm_set_hw_reset(qm, QM_RESET_STOP_RX_OFFSET);
3731 /* Mask eq and aeq irq */
3732 writel(0x1, qm->io_base + QM_VF_EQ_INT_MASK);
3733 writel(0x1, qm->io_base + QM_VF_AEQ_INT_MASK);
3735 if (qm->fun_type == QM_HW_PF) {
3736 ret = hisi_qm_set_vft(qm, 0, 0, 0);
3738 dev_err(dev, "Failed to set vft!\n");
3744 qm_clear_queues(qm);
3745 atomic_set(&qm->status.flags, QM_STOP);
3748 up_write(&qm->qps_lock);
3751 EXPORT_SYMBOL_GPL(hisi_qm_stop);
3753 static ssize_t qm_status_read(struct file *filp, char __user *buffer,
3754 size_t count, loff_t *pos)
3756 struct hisi_qm *qm = filp->private_data;
3757 char buf[QM_DBG_READ_LEN];
3760 val = atomic_read(&qm->status.flags);
3761 len = scnprintf(buf, QM_DBG_READ_LEN, "%s\n", qm_s[val]);
3763 return simple_read_from_buffer(buffer, count, pos, buf, len);
3766 static const struct file_operations qm_status_fops = {
3767 .owner = THIS_MODULE,
3768 .open = simple_open,
3769 .read = qm_status_read,
3772 static int qm_debugfs_atomic64_set(void *data, u64 val)
3777 atomic64_set((atomic64_t *)data, 0);
3782 static int qm_debugfs_atomic64_get(void *data, u64 *val)
3784 *val = atomic64_read((atomic64_t *)data);
3789 DEFINE_DEBUGFS_ATTRIBUTE(qm_atomic64_ops, qm_debugfs_atomic64_get,
3790 qm_debugfs_atomic64_set, "%llu\n");
3792 static void qm_hw_error_init(struct hisi_qm *qm)
3794 struct hisi_qm_err_info *err_info = &qm->err_info;
3796 if (!qm->ops->hw_error_init) {
3797 dev_err(&qm->pdev->dev, "QM doesn't support hw error handling!\n");
3801 qm->ops->hw_error_init(qm, err_info->ce, err_info->nfe, err_info->fe);
3804 static void qm_hw_error_uninit(struct hisi_qm *qm)
3806 if (!qm->ops->hw_error_uninit) {
3807 dev_err(&qm->pdev->dev, "Unexpected QM hw error uninit!\n");
3811 qm->ops->hw_error_uninit(qm);
3814 static enum acc_err_result qm_hw_error_handle(struct hisi_qm *qm)
3816 if (!qm->ops->hw_error_handle) {
3817 dev_err(&qm->pdev->dev, "QM doesn't support hw error report!\n");
3818 return ACC_ERR_NONE;
3821 return qm->ops->hw_error_handle(qm);
3825 * hisi_qm_dev_err_init() - Initialize device error configuration.
3826 * @qm: The qm for which we want to do error initialization.
3828 * Initialize QM and device error related configuration.
3830 void hisi_qm_dev_err_init(struct hisi_qm *qm)
3832 if (qm->fun_type == QM_HW_VF)
3835 qm_hw_error_init(qm);
3837 if (!qm->err_ini->hw_err_enable) {
3838 dev_err(&qm->pdev->dev, "Device doesn't support hw error init!\n");
3841 qm->err_ini->hw_err_enable(qm);
3843 EXPORT_SYMBOL_GPL(hisi_qm_dev_err_init);
3846 * hisi_qm_dev_err_uninit() - Uninitialize device error configuration.
3847 * @qm: The qm for which we want to do error uninitialization.
3849 * Uninitialize QM and device error related configuration.
3851 void hisi_qm_dev_err_uninit(struct hisi_qm *qm)
3853 if (qm->fun_type == QM_HW_VF)
3856 qm_hw_error_uninit(qm);
3858 if (!qm->err_ini->hw_err_disable) {
3859 dev_err(&qm->pdev->dev, "Unexpected device hw error uninit!\n");
3862 qm->err_ini->hw_err_disable(qm);
3864 EXPORT_SYMBOL_GPL(hisi_qm_dev_err_uninit);
3867 * hisi_qm_free_qps() - free multiple queue pairs.
3868 * @qps: The queue pairs need to be freed.
3869 * @qp_num: The num of queue pairs.
3871 void hisi_qm_free_qps(struct hisi_qp **qps, int qp_num)
3875 if (!qps || qp_num <= 0)
3878 for (i = qp_num - 1; i >= 0; i--)
3879 hisi_qm_release_qp(qps[i]);
3881 EXPORT_SYMBOL_GPL(hisi_qm_free_qps);
3883 static void free_list(struct list_head *head)
3885 struct hisi_qm_resource *res, *tmp;
3887 list_for_each_entry_safe(res, tmp, head, list) {
3888 list_del(&res->list);
3893 static int hisi_qm_sort_devices(int node, struct list_head *head,
3894 struct hisi_qm_list *qm_list)
3896 struct hisi_qm_resource *res, *tmp;
3898 struct list_head *n;
3902 list_for_each_entry(qm, &qm_list->list, list) {
3903 dev = &qm->pdev->dev;
3905 if (IS_ENABLED(CONFIG_NUMA)) {
3906 dev_node = dev_to_node(dev);
3911 res = kzalloc(sizeof(*res), GFP_KERNEL);
3916 res->distance = node_distance(dev_node, node);
3918 list_for_each_entry(tmp, head, list) {
3919 if (res->distance < tmp->distance) {
3924 list_add_tail(&res->list, n);
3931 * hisi_qm_alloc_qps_node() - Create multiple queue pairs.
3932 * @qm_list: The list of all available devices.
3933 * @qp_num: The number of queue pairs need created.
3934 * @alg_type: The algorithm type.
3935 * @node: The numa node.
3936 * @qps: The queue pairs need created.
3938 * This function will sort all available device according to numa distance.
3939 * Then try to create all queue pairs from one device, if all devices do
3940 * not meet the requirements will return error.
3942 int hisi_qm_alloc_qps_node(struct hisi_qm_list *qm_list, int qp_num,
3943 u8 alg_type, int node, struct hisi_qp **qps)
3945 struct hisi_qm_resource *tmp;
3950 if (!qps || !qm_list || qp_num <= 0)
3953 mutex_lock(&qm_list->lock);
3954 if (hisi_qm_sort_devices(node, &head, qm_list)) {
3955 mutex_unlock(&qm_list->lock);
3959 list_for_each_entry(tmp, &head, list) {
3960 for (i = 0; i < qp_num; i++) {
3961 qps[i] = hisi_qm_create_qp(tmp->qm, alg_type);
3962 if (IS_ERR(qps[i])) {
3963 hisi_qm_free_qps(qps, i);
3974 mutex_unlock(&qm_list->lock);
3976 pr_info("Failed to create qps, node[%d], alg[%u], qp[%d]!\n",
3977 node, alg_type, qp_num);
3983 EXPORT_SYMBOL_GPL(hisi_qm_alloc_qps_node);
3985 static int qm_vf_q_assign(struct hisi_qm *qm, u32 num_vfs)
3987 u32 remain_q_num, vfs_q_num, act_q_num, q_num, i, j;
3988 u32 max_qp_num = qm->max_qp_num;
3989 u32 q_base = qm->qp_num;
3995 vfs_q_num = qm->ctrl_qp_num - qm->qp_num;
3997 /* If vfs_q_num is less than num_vfs, return error. */
3998 if (vfs_q_num < num_vfs)
4001 q_num = vfs_q_num / num_vfs;
4002 remain_q_num = vfs_q_num % num_vfs;
4004 for (i = num_vfs; i > 0; i--) {
4006 * if q_num + remain_q_num > max_qp_num in last vf, divide the
4007 * remaining queues equally.
4009 if (i == num_vfs && q_num + remain_q_num <= max_qp_num) {
4010 act_q_num = q_num + remain_q_num;
4012 } else if (remain_q_num > 0) {
4013 act_q_num = q_num + 1;
4019 act_q_num = min_t(int, act_q_num, max_qp_num);
4020 ret = hisi_qm_set_vft(qm, i, q_base, act_q_num);
4022 for (j = num_vfs; j > i; j--)
4023 hisi_qm_set_vft(qm, j, 0, 0);
4026 q_base += act_q_num;
4032 static int qm_clear_vft_config(struct hisi_qm *qm)
4037 for (i = 1; i <= qm->vfs_num; i++) {
4038 ret = hisi_qm_set_vft(qm, i, 0, 0);
4047 static int qm_func_shaper_enable(struct hisi_qm *qm, u32 fun_index, u32 qos)
4049 struct device *dev = &qm->pdev->dev;
4050 u32 ir = qos * QM_QOS_RATE;
4051 int ret, total_vfs, i;
4053 total_vfs = pci_sriov_get_totalvfs(qm->pdev);
4054 if (fun_index > total_vfs)
4057 qm->factor[fun_index].func_qos = qos;
4059 ret = qm_get_shaper_para(ir, &qm->factor[fun_index]);
4061 dev_err(dev, "failed to calculate shaper parameter!\n");
4065 for (i = ALG_TYPE_0; i <= ALG_TYPE_1; i++) {
4066 /* The base number of queue reuse for different alg type */
4067 ret = qm_set_vft_common(qm, SHAPER_VFT, fun_index, i, 1);
4069 dev_err(dev, "type: %d, failed to set shaper vft!\n", i);
4077 static u32 qm_get_shaper_vft_qos(struct hisi_qm *qm, u32 fun_index)
4079 u64 cir_u = 0, cir_b = 0, cir_s = 0;
4080 u64 shaper_vft, ir_calc, ir;
4085 ret = readl_relaxed_poll_timeout(qm->io_base + QM_VFT_CFG_RDY, val,
4086 val & BIT(0), POLL_PERIOD,
4091 writel(0x1, qm->io_base + QM_VFT_CFG_OP_WR);
4092 writel(SHAPER_VFT, qm->io_base + QM_VFT_CFG_TYPE);
4093 writel(fun_index, qm->io_base + QM_VFT_CFG);
4095 writel(0x0, qm->io_base + QM_VFT_CFG_RDY);
4096 writel(0x1, qm->io_base + QM_VFT_CFG_OP_ENABLE);
4098 ret = readl_relaxed_poll_timeout(qm->io_base + QM_VFT_CFG_RDY, val,
4099 val & BIT(0), POLL_PERIOD,
4104 shaper_vft = readl(qm->io_base + QM_VFT_CFG_DATA_L) |
4105 ((u64)readl(qm->io_base + QM_VFT_CFG_DATA_H) << 32);
4107 cir_b = shaper_vft & QM_SHAPER_CIR_B_MASK;
4108 cir_u = shaper_vft & QM_SHAPER_CIR_U_MASK;
4109 cir_u = cir_u >> QM_SHAPER_FACTOR_CIR_U_SHIFT;
4111 cir_s = shaper_vft & QM_SHAPER_CIR_S_MASK;
4112 cir_s = cir_s >> QM_SHAPER_FACTOR_CIR_S_SHIFT;
4114 ir_calc = acc_shaper_para_calc(cir_b, cir_u, cir_s);
4116 ir = qm->factor[fun_index].func_qos * QM_QOS_RATE;
4118 error_rate = QM_QOS_EXPAND_RATE * (u32)abs(ir_calc - ir) / ir;
4119 if (error_rate > QM_QOS_MIN_ERROR_RATE) {
4120 pci_err(qm->pdev, "error_rate: %u, get function qos is error!\n", error_rate);
4127 static void qm_vf_get_qos(struct hisi_qm *qm, u32 fun_num)
4129 struct device *dev = &qm->pdev->dev;
4134 qos = qm_get_shaper_vft_qos(qm, fun_num);
4136 dev_err(dev, "function(%u) failed to get qos by PF!\n", fun_num);
4140 mb_cmd = QM_PF_SET_QOS | (u64)qos << QM_MB_CMD_DATA_SHIFT;
4141 ret = qm_ping_single_vf(qm, mb_cmd, fun_num);
4143 dev_err(dev, "failed to send cmd to VF(%u)!\n", fun_num);
4146 static int qm_vf_read_qos(struct hisi_qm *qm)
4151 /* reset mailbox qos val */
4154 /* vf ping pf to get function qos */
4155 if (qm->ops->ping_pf) {
4156 ret = qm->ops->ping_pf(qm, QM_VF_GET_QOS);
4158 pci_err(qm->pdev, "failed to send cmd to PF to get qos!\n");
4164 msleep(QM_WAIT_DST_ACK);
4168 if (++cnt > QM_MAX_VF_WAIT_COUNT) {
4169 pci_err(qm->pdev, "PF ping VF timeout!\n");
4177 static ssize_t qm_algqos_read(struct file *filp, char __user *buf,
4178 size_t count, loff_t *pos)
4180 struct hisi_qm *qm = filp->private_data;
4181 char tbuf[QM_DBG_READ_LEN];
4185 ret = hisi_qm_get_dfx_access(qm);
4189 /* Mailbox and reset cannot be operated at the same time */
4190 if (test_and_set_bit(QM_RESETTING, &qm->misc_ctl)) {
4191 pci_err(qm->pdev, "dev resetting, read alg qos failed!\n");
4193 goto err_put_dfx_access;
4196 if (qm->fun_type == QM_HW_PF) {
4197 ir = qm_get_shaper_vft_qos(qm, 0);
4199 ret = qm_vf_read_qos(qm);
4201 goto err_get_status;
4205 qos_val = ir / QM_QOS_RATE;
4206 ret = scnprintf(tbuf, QM_DBG_READ_LEN, "%u\n", qos_val);
4208 ret = simple_read_from_buffer(buf, count, pos, tbuf, ret);
4211 clear_bit(QM_RESETTING, &qm->misc_ctl);
4213 hisi_qm_put_dfx_access(qm);
4217 static ssize_t qm_qos_value_init(const char *buf, unsigned long *val)
4219 int buflen = strlen(buf);
4222 for (i = 0; i < buflen; i++) {
4223 if (!isdigit(buf[i]))
4227 ret = sscanf(buf, "%lu", val);
4228 if (ret != QM_QOS_VAL_NUM)
4234 static ssize_t qm_algqos_write(struct file *filp, const char __user *buf,
4235 size_t count, loff_t *pos)
4237 struct hisi_qm *qm = filp->private_data;
4238 char tbuf[QM_DBG_READ_LEN];
4239 int tmp1, bus, device, function;
4240 char tbuf_bdf[QM_DBG_READ_LEN] = {0};
4241 char val_buf[QM_QOS_VAL_MAX_LEN] = {0};
4242 unsigned int fun_index;
4243 unsigned long val = 0;
4246 if (qm->fun_type == QM_HW_VF)
4249 /* Mailbox and reset cannot be operated at the same time */
4250 if (test_and_set_bit(QM_RESETTING, &qm->misc_ctl)) {
4251 pci_err(qm->pdev, "dev resetting, write alg qos failed!\n");
4257 goto err_get_status;
4260 if (count >= QM_DBG_READ_LEN) {
4262 goto err_get_status;
4265 len = simple_write_to_buffer(tbuf, QM_DBG_READ_LEN - 1, pos, buf, count);
4268 goto err_get_status;
4272 ret = sscanf(tbuf, "%s %s", tbuf_bdf, val_buf);
4273 if (ret != QM_QOS_PARAM_NUM) {
4275 goto err_get_status;
4278 ret = qm_qos_value_init(val_buf, &val);
4279 if (val == 0 || val > QM_QOS_MAX_VAL || ret) {
4280 pci_err(qm->pdev, "input qos value is error, please set 1~1000!\n");
4282 goto err_get_status;
4285 ret = sscanf(tbuf_bdf, "%d:%x:%d.%d", &tmp1, &bus, &device, &function);
4286 if (ret != QM_QOS_BDF_PARAM_NUM) {
4287 pci_err(qm->pdev, "input pci bdf value is error!\n");
4289 goto err_get_status;
4292 fun_index = device * 8 + function;
4294 ret = qm_pm_get_sync(qm);
4297 goto err_get_status;
4300 ret = qm_func_shaper_enable(qm, fun_index, val);
4302 pci_err(qm->pdev, "failed to enable function shaper!\n");
4312 clear_bit(QM_RESETTING, &qm->misc_ctl);
4316 static const struct file_operations qm_algqos_fops = {
4317 .owner = THIS_MODULE,
4318 .open = simple_open,
4319 .read = qm_algqos_read,
4320 .write = qm_algqos_write,
4324 * hisi_qm_set_algqos_init() - Initialize function qos debugfs files.
4325 * @qm: The qm for which we want to add debugfs files.
4327 * Create function qos debugfs files.
4329 static void hisi_qm_set_algqos_init(struct hisi_qm *qm)
4331 if (qm->fun_type == QM_HW_PF)
4332 debugfs_create_file("alg_qos", 0644, qm->debug.debug_root,
4333 qm, &qm_algqos_fops);
4335 debugfs_create_file("alg_qos", 0444, qm->debug.debug_root,
4336 qm, &qm_algqos_fops);
4340 * hisi_qm_debug_init() - Initialize qm related debugfs files.
4341 * @qm: The qm for which we want to add debugfs files.
4343 * Create qm related debugfs files.
4345 void hisi_qm_debug_init(struct hisi_qm *qm)
4347 struct qm_dfx *dfx = &qm->debug.dfx;
4348 struct dentry *qm_d;
4352 qm_d = debugfs_create_dir("qm", qm->debug.debug_root);
4353 qm->debug.qm_d = qm_d;
4355 /* only show this in PF */
4356 if (qm->fun_type == QM_HW_PF) {
4357 qm_create_debugfs_file(qm, qm->debug.debug_root, CURRENT_QM);
4358 for (i = CURRENT_Q; i < DEBUG_FILE_NUM; i++)
4359 qm_create_debugfs_file(qm, qm->debug.qm_d, i);
4362 debugfs_create_file("regs", 0444, qm->debug.qm_d, qm, &qm_regs_fops);
4364 debugfs_create_file("cmd", 0600, qm->debug.qm_d, qm, &qm_cmd_fops);
4366 debugfs_create_file("status", 0444, qm->debug.qm_d, qm,
4368 for (i = 0; i < ARRAY_SIZE(qm_dfx_files); i++) {
4369 data = (atomic64_t *)((uintptr_t)dfx + qm_dfx_files[i].offset);
4370 debugfs_create_file(qm_dfx_files[i].name,
4377 if (qm->ver >= QM_HW_V3)
4378 hisi_qm_set_algqos_init(qm);
4380 EXPORT_SYMBOL_GPL(hisi_qm_debug_init);
4383 * hisi_qm_debug_regs_clear() - clear qm debug related registers.
4384 * @qm: The qm for which we want to clear its debug registers.
4386 void hisi_qm_debug_regs_clear(struct hisi_qm *qm)
4388 const struct debugfs_reg32 *regs;
4391 /* clear current_qm */
4392 writel(0x0, qm->io_base + QM_DFX_MB_CNT_VF);
4393 writel(0x0, qm->io_base + QM_DFX_DB_CNT_VF);
4395 /* clear current_q */
4396 writel(0x0, qm->io_base + QM_DFX_SQE_CNT_VF_SQN);
4397 writel(0x0, qm->io_base + QM_DFX_CQE_CNT_VF_CQN);
4400 * these registers are reading and clearing, so clear them after
4403 writel(0x1, qm->io_base + QM_DFX_CNT_CLR_CE);
4406 for (i = 0; i < CNT_CYC_REGS_NUM; i++) {
4407 readl(qm->io_base + regs->offset);
4411 /* clear clear_enable */
4412 writel(0x0, qm->io_base + QM_DFX_CNT_CLR_CE);
4414 EXPORT_SYMBOL_GPL(hisi_qm_debug_regs_clear);
4417 * hisi_qm_sriov_enable() - enable virtual functions
4418 * @pdev: the PCIe device
4419 * @max_vfs: the number of virtual functions to enable
4421 * Returns the number of enabled VFs. If there are VFs enabled already or
4422 * max_vfs is more than the total number of device can be enabled, returns
4425 int hisi_qm_sriov_enable(struct pci_dev *pdev, int max_vfs)
4427 struct hisi_qm *qm = pci_get_drvdata(pdev);
4428 int pre_existing_vfs, num_vfs, total_vfs, ret;
4430 ret = qm_pm_get_sync(qm);
4434 total_vfs = pci_sriov_get_totalvfs(pdev);
4435 pre_existing_vfs = pci_num_vf(pdev);
4436 if (pre_existing_vfs) {
4437 pci_err(pdev, "%d VFs already enabled. Please disable pre-enabled VFs!\n",
4442 num_vfs = min_t(int, max_vfs, total_vfs);
4443 ret = qm_vf_q_assign(qm, num_vfs);
4445 pci_err(pdev, "Can't assign queues for VF!\n");
4449 qm->vfs_num = num_vfs;
4451 ret = pci_enable_sriov(pdev, num_vfs);
4453 pci_err(pdev, "Can't enable VF!\n");
4454 qm_clear_vft_config(qm);
4458 pci_info(pdev, "VF enabled, vfs_num(=%d)!\n", num_vfs);
4466 EXPORT_SYMBOL_GPL(hisi_qm_sriov_enable);
4469 * hisi_qm_sriov_disable - disable virtual functions
4470 * @pdev: the PCI device.
4471 * @is_frozen: true when all the VFs are frozen.
4473 * Return failure if there are VFs assigned already or VF is in used.
4475 int hisi_qm_sriov_disable(struct pci_dev *pdev, bool is_frozen)
4477 struct hisi_qm *qm = pci_get_drvdata(pdev);
4478 int total_vfs = pci_sriov_get_totalvfs(qm->pdev);
4481 if (pci_vfs_assigned(pdev)) {
4482 pci_err(pdev, "Failed to disable VFs as VFs are assigned!\n");
4486 /* While VF is in used, SRIOV cannot be disabled. */
4487 if (!is_frozen && qm_try_frozen_vfs(pdev, qm->qm_list)) {
4488 pci_err(pdev, "Task is using its VF!\n");
4492 pci_disable_sriov(pdev);
4493 /* clear vf function shaper configure array */
4494 memset(qm->factor + 1, 0, sizeof(struct qm_shaper_factor) * total_vfs);
4495 ret = qm_clear_vft_config(qm);
4503 EXPORT_SYMBOL_GPL(hisi_qm_sriov_disable);
4506 * hisi_qm_sriov_configure - configure the number of VFs
4507 * @pdev: The PCI device
4508 * @num_vfs: The number of VFs need enabled
4510 * Enable SR-IOV according to num_vfs, 0 means disable.
4512 int hisi_qm_sriov_configure(struct pci_dev *pdev, int num_vfs)
4515 return hisi_qm_sriov_disable(pdev, false);
4517 return hisi_qm_sriov_enable(pdev, num_vfs);
4519 EXPORT_SYMBOL_GPL(hisi_qm_sriov_configure);
4521 static enum acc_err_result qm_dev_err_handle(struct hisi_qm *qm)
4525 if (!qm->err_ini->get_dev_hw_err_status) {
4526 dev_err(&qm->pdev->dev, "Device doesn't support get hw error status!\n");
4527 return ACC_ERR_NONE;
4530 /* get device hardware error status */
4531 err_sts = qm->err_ini->get_dev_hw_err_status(qm);
4533 if (err_sts & qm->err_info.ecc_2bits_mask)
4534 qm->err_status.is_dev_ecc_mbit = true;
4536 if (qm->err_ini->log_dev_hw_err)
4537 qm->err_ini->log_dev_hw_err(qm, err_sts);
4539 /* ce error does not need to be reset */
4540 if ((err_sts | qm->err_info.dev_ce_mask) ==
4541 qm->err_info.dev_ce_mask) {
4542 if (qm->err_ini->clear_dev_hw_err_status)
4543 qm->err_ini->clear_dev_hw_err_status(qm,
4546 return ACC_ERR_RECOVERED;
4549 return ACC_ERR_NEED_RESET;
4552 return ACC_ERR_RECOVERED;
4555 static enum acc_err_result qm_process_dev_error(struct hisi_qm *qm)
4557 enum acc_err_result qm_ret, dev_ret;
4560 qm_ret = qm_hw_error_handle(qm);
4562 /* log device error */
4563 dev_ret = qm_dev_err_handle(qm);
4565 return (qm_ret == ACC_ERR_NEED_RESET ||
4566 dev_ret == ACC_ERR_NEED_RESET) ?
4567 ACC_ERR_NEED_RESET : ACC_ERR_RECOVERED;
4571 * hisi_qm_dev_err_detected() - Get device and qm error status then log it.
4572 * @pdev: The PCI device which need report error.
4573 * @state: The connectivity between CPU and device.
4575 * We register this function into PCIe AER handlers, It will report device or
4576 * qm hardware error status when error occur.
4578 pci_ers_result_t hisi_qm_dev_err_detected(struct pci_dev *pdev,
4579 pci_channel_state_t state)
4581 struct hisi_qm *qm = pci_get_drvdata(pdev);
4582 enum acc_err_result ret;
4584 if (pdev->is_virtfn)
4585 return PCI_ERS_RESULT_NONE;
4587 pci_info(pdev, "PCI error detected, state(=%u)!!\n", state);
4588 if (state == pci_channel_io_perm_failure)
4589 return PCI_ERS_RESULT_DISCONNECT;
4591 ret = qm_process_dev_error(qm);
4592 if (ret == ACC_ERR_NEED_RESET)
4593 return PCI_ERS_RESULT_NEED_RESET;
4595 return PCI_ERS_RESULT_RECOVERED;
4597 EXPORT_SYMBOL_GPL(hisi_qm_dev_err_detected);
4599 static int qm_check_req_recv(struct hisi_qm *qm)
4601 struct pci_dev *pdev = qm->pdev;
4605 if (qm->ver >= QM_HW_V3)
4608 writel(ACC_VENDOR_ID_VALUE, qm->io_base + QM_PEH_VENDOR_ID);
4609 ret = readl_relaxed_poll_timeout(qm->io_base + QM_PEH_VENDOR_ID, val,
4610 (val == ACC_VENDOR_ID_VALUE),
4611 POLL_PERIOD, POLL_TIMEOUT);
4613 dev_err(&pdev->dev, "Fails to read QM reg!\n");
4617 writel(PCI_VENDOR_ID_HUAWEI, qm->io_base + QM_PEH_VENDOR_ID);
4618 ret = readl_relaxed_poll_timeout(qm->io_base + QM_PEH_VENDOR_ID, val,
4619 (val == PCI_VENDOR_ID_HUAWEI),
4620 POLL_PERIOD, POLL_TIMEOUT);
4622 dev_err(&pdev->dev, "Fails to read QM reg in the second time!\n");
4627 static int qm_set_pf_mse(struct hisi_qm *qm, bool set)
4629 struct pci_dev *pdev = qm->pdev;
4633 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
4635 cmd |= PCI_COMMAND_MEMORY;
4637 cmd &= ~PCI_COMMAND_MEMORY;
4639 pci_write_config_word(pdev, PCI_COMMAND, cmd);
4640 for (i = 0; i < MAX_WAIT_COUNTS; i++) {
4641 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
4642 if (set == ((cmd & PCI_COMMAND_MEMORY) >> 1))
4651 static int qm_set_vf_mse(struct hisi_qm *qm, bool set)
4653 struct pci_dev *pdev = qm->pdev;
4658 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4659 pci_read_config_word(pdev, pos + PCI_SRIOV_CTRL, &sriov_ctrl);
4661 sriov_ctrl |= PCI_SRIOV_CTRL_MSE;
4663 sriov_ctrl &= ~PCI_SRIOV_CTRL_MSE;
4664 pci_write_config_word(pdev, pos + PCI_SRIOV_CTRL, sriov_ctrl);
4666 for (i = 0; i < MAX_WAIT_COUNTS; i++) {
4667 pci_read_config_word(pdev, pos + PCI_SRIOV_CTRL, &sriov_ctrl);
4668 if (set == (sriov_ctrl & PCI_SRIOV_CTRL_MSE) >>
4669 ACC_PEH_SRIOV_CTRL_VF_MSE_SHIFT)
4678 static int qm_vf_reset_prepare(struct hisi_qm *qm,
4679 enum qm_stop_reason stop_reason)
4681 struct hisi_qm_list *qm_list = qm->qm_list;
4682 struct pci_dev *pdev = qm->pdev;
4683 struct pci_dev *virtfn;
4684 struct hisi_qm *vf_qm;
4687 mutex_lock(&qm_list->lock);
4688 list_for_each_entry(vf_qm, &qm_list->list, list) {
4689 virtfn = vf_qm->pdev;
4693 if (pci_physfn(virtfn) == pdev) {
4694 /* save VFs PCIE BAR configuration */
4695 pci_save_state(virtfn);
4697 ret = hisi_qm_stop(vf_qm, stop_reason);
4704 mutex_unlock(&qm_list->lock);
4708 static int qm_try_stop_vfs(struct hisi_qm *qm, u64 cmd,
4709 enum qm_stop_reason stop_reason)
4711 struct pci_dev *pdev = qm->pdev;
4717 /* Kunpeng930 supports to notify VFs to stop before PF reset */
4718 if (qm->ops->ping_all_vfs) {
4719 ret = qm->ops->ping_all_vfs(qm, cmd);
4721 pci_err(pdev, "failed to send cmd to all VFs before PF reset!\n");
4723 ret = qm_vf_reset_prepare(qm, stop_reason);
4725 pci_err(pdev, "failed to prepare reset, ret = %d.\n", ret);
4731 static int qm_wait_reset_finish(struct hisi_qm *qm)
4735 /* All reset requests need to be queued for processing */
4736 while (test_and_set_bit(QM_RESETTING, &qm->misc_ctl)) {
4738 if (delay > QM_RESET_WAIT_TIMEOUT)
4745 static int qm_reset_prepare_ready(struct hisi_qm *qm)
4747 struct pci_dev *pdev = qm->pdev;
4748 struct hisi_qm *pf_qm = pci_get_drvdata(pci_physfn(pdev));
4751 * PF and VF on host doesnot support resetting at the
4752 * same time on Kunpeng920.
4754 if (qm->ver < QM_HW_V3)
4755 return qm_wait_reset_finish(pf_qm);
4757 return qm_wait_reset_finish(qm);
4760 static void qm_reset_bit_clear(struct hisi_qm *qm)
4762 struct pci_dev *pdev = qm->pdev;
4763 struct hisi_qm *pf_qm = pci_get_drvdata(pci_physfn(pdev));
4765 if (qm->ver < QM_HW_V3)
4766 clear_bit(QM_RESETTING, &pf_qm->misc_ctl);
4768 clear_bit(QM_RESETTING, &qm->misc_ctl);
4771 static int qm_controller_reset_prepare(struct hisi_qm *qm)
4773 struct pci_dev *pdev = qm->pdev;
4776 ret = qm_reset_prepare_ready(qm);
4778 pci_err(pdev, "Controller reset not ready!\n");
4782 /* PF obtains the information of VF by querying the register. */
4785 /* Whether VFs stop successfully, soft reset will continue. */
4786 ret = qm_try_stop_vfs(qm, QM_PF_SRST_PREPARE, QM_SOFT_RESET);
4788 pci_err(pdev, "failed to stop vfs by pf in soft reset.\n");
4790 ret = hisi_qm_stop(qm, QM_SOFT_RESET);
4792 pci_err(pdev, "Fails to stop QM!\n");
4793 qm_reset_bit_clear(qm);
4797 ret = qm_wait_vf_prepare_finish(qm);
4799 pci_err(pdev, "failed to stop by vfs in soft reset!\n");
4801 clear_bit(QM_RST_SCHED, &qm->misc_ctl);
4806 static void qm_dev_ecc_mbit_handle(struct hisi_qm *qm)
4810 /* Kunpeng930 hardware automatically close master ooo when NFE occurs */
4811 if (qm->ver >= QM_HW_V3)
4814 if (!qm->err_status.is_dev_ecc_mbit &&
4815 qm->err_status.is_qm_ecc_mbit &&
4816 qm->err_ini->close_axi_master_ooo) {
4818 qm->err_ini->close_axi_master_ooo(qm);
4820 } else if (qm->err_status.is_dev_ecc_mbit &&
4821 !qm->err_status.is_qm_ecc_mbit &&
4822 !qm->err_ini->close_axi_master_ooo) {
4824 nfe_enb = readl(qm->io_base + QM_RAS_NFE_ENABLE);
4825 writel(nfe_enb & QM_RAS_NFE_MBIT_DISABLE,
4826 qm->io_base + QM_RAS_NFE_ENABLE);
4827 writel(QM_ECC_MBIT, qm->io_base + QM_ABNORMAL_INT_SET);
4831 static int qm_soft_reset(struct hisi_qm *qm)
4833 struct pci_dev *pdev = qm->pdev;
4837 /* Ensure all doorbells and mailboxes received by QM */
4838 ret = qm_check_req_recv(qm);
4843 ret = qm_set_vf_mse(qm, false);
4845 pci_err(pdev, "Fails to disable vf MSE bit.\n");
4850 ret = qm->ops->set_msi(qm, false);
4852 pci_err(pdev, "Fails to disable PEH MSI bit.\n");
4856 qm_dev_ecc_mbit_handle(qm);
4858 /* OOO register set and check */
4859 writel(ACC_MASTER_GLOBAL_CTRL_SHUTDOWN,
4860 qm->io_base + ACC_MASTER_GLOBAL_CTRL);
4862 /* If bus lock, reset chip */
4863 ret = readl_relaxed_poll_timeout(qm->io_base + ACC_MASTER_TRANS_RETURN,
4865 (val == ACC_MASTER_TRANS_RETURN_RW),
4866 POLL_PERIOD, POLL_TIMEOUT);
4868 pci_emerg(pdev, "Bus lock! Please reset system.\n");
4872 if (qm->err_ini->close_sva_prefetch)
4873 qm->err_ini->close_sva_prefetch(qm);
4875 ret = qm_set_pf_mse(qm, false);
4877 pci_err(pdev, "Fails to disable pf MSE bit.\n");
4881 /* The reset related sub-control registers are not in PCI BAR */
4882 if (ACPI_HANDLE(&pdev->dev)) {
4883 unsigned long long value = 0;
4886 s = acpi_evaluate_integer(ACPI_HANDLE(&pdev->dev),
4887 qm->err_info.acpi_rst,
4889 if (ACPI_FAILURE(s)) {
4890 pci_err(pdev, "NO controller reset method!\n");
4895 pci_err(pdev, "Reset step %llu failed!\n", value);
4899 pci_err(pdev, "No reset method!\n");
4906 static int qm_vf_reset_done(struct hisi_qm *qm)
4908 struct hisi_qm_list *qm_list = qm->qm_list;
4909 struct pci_dev *pdev = qm->pdev;
4910 struct pci_dev *virtfn;
4911 struct hisi_qm *vf_qm;
4914 mutex_lock(&qm_list->lock);
4915 list_for_each_entry(vf_qm, &qm_list->list, list) {
4916 virtfn = vf_qm->pdev;
4920 if (pci_physfn(virtfn) == pdev) {
4921 /* enable VFs PCIE BAR configuration */
4922 pci_restore_state(virtfn);
4924 ret = qm_restart(vf_qm);
4931 mutex_unlock(&qm_list->lock);
4935 static int qm_try_start_vfs(struct hisi_qm *qm, enum qm_mb_cmd cmd)
4937 struct pci_dev *pdev = qm->pdev;
4943 ret = qm_vf_q_assign(qm, qm->vfs_num);
4945 pci_err(pdev, "failed to assign VFs, ret = %d.\n", ret);
4949 /* Kunpeng930 supports to notify VFs to start after PF reset. */
4950 if (qm->ops->ping_all_vfs) {
4951 ret = qm->ops->ping_all_vfs(qm, cmd);
4953 pci_warn(pdev, "failed to send cmd to all VFs after PF reset!\n");
4955 ret = qm_vf_reset_done(qm);
4957 pci_warn(pdev, "failed to start vfs, ret = %d.\n", ret);
4963 static int qm_dev_hw_init(struct hisi_qm *qm)
4965 return qm->err_ini->hw_init(qm);
4968 static void qm_restart_prepare(struct hisi_qm *qm)
4972 if (qm->err_ini->open_sva_prefetch)
4973 qm->err_ini->open_sva_prefetch(qm);
4975 if (qm->ver >= QM_HW_V3)
4978 if (!qm->err_status.is_qm_ecc_mbit &&
4979 !qm->err_status.is_dev_ecc_mbit)
4982 /* temporarily close the OOO port used for PEH to write out MSI */
4983 value = readl(qm->io_base + ACC_AM_CFG_PORT_WR_EN);
4984 writel(value & ~qm->err_info.msi_wr_port,
4985 qm->io_base + ACC_AM_CFG_PORT_WR_EN);
4987 /* clear dev ecc 2bit error source if having */
4988 value = qm_get_dev_err_status(qm) & qm->err_info.ecc_2bits_mask;
4989 if (value && qm->err_ini->clear_dev_hw_err_status)
4990 qm->err_ini->clear_dev_hw_err_status(qm, value);
4992 /* clear QM ecc mbit error source */
4993 writel(QM_ECC_MBIT, qm->io_base + QM_ABNORMAL_INT_SOURCE);
4995 /* clear AM Reorder Buffer ecc mbit source */
4996 writel(ACC_ROB_ECC_ERR_MULTPL, qm->io_base + ACC_AM_ROB_ECC_INT_STS);
4999 static void qm_restart_done(struct hisi_qm *qm)
5003 if (qm->ver >= QM_HW_V3)
5006 if (!qm->err_status.is_qm_ecc_mbit &&
5007 !qm->err_status.is_dev_ecc_mbit)
5010 /* open the OOO port for PEH to write out MSI */
5011 value = readl(qm->io_base + ACC_AM_CFG_PORT_WR_EN);
5012 value |= qm->err_info.msi_wr_port;
5013 writel(value, qm->io_base + ACC_AM_CFG_PORT_WR_EN);
5016 qm->err_status.is_qm_ecc_mbit = false;
5017 qm->err_status.is_dev_ecc_mbit = false;
5020 static int qm_controller_reset_done(struct hisi_qm *qm)
5022 struct pci_dev *pdev = qm->pdev;
5025 ret = qm->ops->set_msi(qm, true);
5027 pci_err(pdev, "Fails to enable PEH MSI bit!\n");
5031 ret = qm_set_pf_mse(qm, true);
5033 pci_err(pdev, "Fails to enable pf MSE bit!\n");
5038 ret = qm_set_vf_mse(qm, true);
5040 pci_err(pdev, "Fails to enable vf MSE bit!\n");
5045 ret = qm_dev_hw_init(qm);
5047 pci_err(pdev, "Failed to init device\n");
5051 qm_restart_prepare(qm);
5052 hisi_qm_dev_err_init(qm);
5053 if (qm->err_ini->open_axi_master_ooo)
5054 qm->err_ini->open_axi_master_ooo(qm);
5056 ret = qm_restart(qm);
5058 pci_err(pdev, "Failed to start QM!\n");
5062 ret = qm_try_start_vfs(qm, QM_PF_RESET_DONE);
5064 pci_err(pdev, "failed to start vfs by pf in soft reset.\n");
5066 ret = qm_wait_vf_prepare_finish(qm);
5068 pci_err(pdev, "failed to start by vfs in soft reset!\n");
5071 qm_restart_done(qm);
5073 qm_reset_bit_clear(qm);
5078 static int qm_controller_reset(struct hisi_qm *qm)
5080 struct pci_dev *pdev = qm->pdev;
5083 pci_info(pdev, "Controller resetting...\n");
5085 ret = qm_controller_reset_prepare(qm);
5087 hisi_qm_set_hw_reset(qm, QM_RESET_STOP_TX_OFFSET);
5088 hisi_qm_set_hw_reset(qm, QM_RESET_STOP_RX_OFFSET);
5089 clear_bit(QM_RST_SCHED, &qm->misc_ctl);
5093 ret = qm_soft_reset(qm);
5095 pci_err(pdev, "Controller reset failed (%d)\n", ret);
5096 qm_reset_bit_clear(qm);
5100 ret = qm_controller_reset_done(qm);
5102 qm_reset_bit_clear(qm);
5106 pci_info(pdev, "Controller reset complete\n");
5112 * hisi_qm_dev_slot_reset() - slot reset
5113 * @pdev: the PCIe device
5115 * This function offers QM relate PCIe device reset interface. Drivers which
5116 * use QM can use this function as slot_reset in its struct pci_error_handlers.
5118 pci_ers_result_t hisi_qm_dev_slot_reset(struct pci_dev *pdev)
5120 struct hisi_qm *qm = pci_get_drvdata(pdev);
5123 if (pdev->is_virtfn)
5124 return PCI_ERS_RESULT_RECOVERED;
5126 pci_aer_clear_nonfatal_status(pdev);
5128 /* reset pcie device controller */
5129 ret = qm_controller_reset(qm);
5131 pci_err(pdev, "Controller reset failed (%d)\n", ret);
5132 return PCI_ERS_RESULT_DISCONNECT;
5135 return PCI_ERS_RESULT_RECOVERED;
5137 EXPORT_SYMBOL_GPL(hisi_qm_dev_slot_reset);
5139 void hisi_qm_reset_prepare(struct pci_dev *pdev)
5141 struct hisi_qm *pf_qm = pci_get_drvdata(pci_physfn(pdev));
5142 struct hisi_qm *qm = pci_get_drvdata(pdev);
5146 hisi_qm_dev_err_uninit(pf_qm);
5149 * Check whether there is an ECC mbit error, If it occurs, need to
5150 * wait for soft reset to fix it.
5152 while (qm_check_dev_error(pf_qm)) {
5154 if (delay > QM_RESET_WAIT_TIMEOUT)
5158 ret = qm_reset_prepare_ready(qm);
5160 pci_err(pdev, "FLR not ready!\n");
5164 /* PF obtains the information of VF by querying the register. */
5165 if (qm->fun_type == QM_HW_PF)
5168 ret = qm_try_stop_vfs(qm, QM_PF_FLR_PREPARE, QM_FLR);
5170 pci_err(pdev, "failed to stop vfs by pf in FLR.\n");
5172 ret = hisi_qm_stop(qm, QM_FLR);
5174 pci_err(pdev, "Failed to stop QM, ret = %d.\n", ret);
5175 hisi_qm_set_hw_reset(qm, QM_RESET_STOP_TX_OFFSET);
5176 hisi_qm_set_hw_reset(qm, QM_RESET_STOP_RX_OFFSET);
5180 ret = qm_wait_vf_prepare_finish(qm);
5182 pci_err(pdev, "failed to stop by vfs in FLR!\n");
5184 pci_info(pdev, "FLR resetting...\n");
5186 EXPORT_SYMBOL_GPL(hisi_qm_reset_prepare);
5188 static bool qm_flr_reset_complete(struct pci_dev *pdev)
5190 struct pci_dev *pf_pdev = pci_physfn(pdev);
5191 struct hisi_qm *qm = pci_get_drvdata(pf_pdev);
5194 pci_read_config_dword(qm->pdev, PCI_COMMAND, &id);
5195 if (id == QM_PCI_COMMAND_INVALID) {
5196 pci_err(pdev, "Device can not be used!\n");
5203 void hisi_qm_reset_done(struct pci_dev *pdev)
5205 struct hisi_qm *pf_qm = pci_get_drvdata(pci_physfn(pdev));
5206 struct hisi_qm *qm = pci_get_drvdata(pdev);
5209 if (qm->fun_type == QM_HW_PF) {
5210 ret = qm_dev_hw_init(qm);
5212 pci_err(pdev, "Failed to init PF, ret = %d.\n", ret);
5217 hisi_qm_dev_err_init(pf_qm);
5219 ret = qm_restart(qm);
5221 pci_err(pdev, "Failed to start QM, ret = %d.\n", ret);
5225 ret = qm_try_start_vfs(qm, QM_PF_RESET_DONE);
5227 pci_err(pdev, "failed to start vfs by pf in FLR.\n");
5229 ret = qm_wait_vf_prepare_finish(qm);
5231 pci_err(pdev, "failed to start by vfs in FLR!\n");
5234 if (qm->fun_type == QM_HW_PF)
5237 if (qm_flr_reset_complete(pdev))
5238 pci_info(pdev, "FLR reset complete\n");
5240 qm_reset_bit_clear(qm);
5242 EXPORT_SYMBOL_GPL(hisi_qm_reset_done);
5244 static irqreturn_t qm_abnormal_irq(int irq, void *data)
5246 struct hisi_qm *qm = data;
5247 enum acc_err_result ret;
5249 atomic64_inc(&qm->debug.dfx.abnormal_irq_cnt);
5250 ret = qm_process_dev_error(qm);
5251 if (ret == ACC_ERR_NEED_RESET &&
5252 !test_bit(QM_DRIVER_REMOVING, &qm->misc_ctl) &&
5253 !test_and_set_bit(QM_RST_SCHED, &qm->misc_ctl))
5254 schedule_work(&qm->rst_work);
5259 static int qm_irq_register(struct hisi_qm *qm)
5261 struct pci_dev *pdev = qm->pdev;
5264 ret = request_irq(pci_irq_vector(pdev, QM_EQ_EVENT_IRQ_VECTOR),
5265 qm_irq, 0, qm->dev_name, qm);
5269 if (qm->ver > QM_HW_V1) {
5270 ret = request_irq(pci_irq_vector(pdev, QM_AEQ_EVENT_IRQ_VECTOR),
5271 qm_aeq_irq, 0, qm->dev_name, qm);
5275 if (qm->fun_type == QM_HW_PF) {
5276 ret = request_irq(pci_irq_vector(pdev,
5277 QM_ABNORMAL_EVENT_IRQ_VECTOR),
5278 qm_abnormal_irq, 0, qm->dev_name, qm);
5280 goto err_abonormal_irq;
5284 if (qm->ver > QM_HW_V2) {
5285 ret = request_irq(pci_irq_vector(pdev, QM_CMD_EVENT_IRQ_VECTOR),
5286 qm_mb_cmd_irq, 0, qm->dev_name, qm);
5288 goto err_mb_cmd_irq;
5294 if (qm->fun_type == QM_HW_PF)
5295 free_irq(pci_irq_vector(pdev, QM_ABNORMAL_EVENT_IRQ_VECTOR), qm);
5297 free_irq(pci_irq_vector(pdev, QM_AEQ_EVENT_IRQ_VECTOR), qm);
5299 free_irq(pci_irq_vector(pdev, QM_EQ_EVENT_IRQ_VECTOR), qm);
5304 * hisi_qm_dev_shutdown() - Shutdown device.
5305 * @pdev: The device will be shutdown.
5307 * This function will stop qm when OS shutdown or rebooting.
5309 void hisi_qm_dev_shutdown(struct pci_dev *pdev)
5311 struct hisi_qm *qm = pci_get_drvdata(pdev);
5314 ret = hisi_qm_stop(qm, QM_NORMAL);
5316 dev_err(&pdev->dev, "Fail to stop qm in shutdown!\n");
5318 EXPORT_SYMBOL_GPL(hisi_qm_dev_shutdown);
5320 static void hisi_qm_controller_reset(struct work_struct *rst_work)
5322 struct hisi_qm *qm = container_of(rst_work, struct hisi_qm, rst_work);
5325 ret = qm_pm_get_sync(qm);
5327 clear_bit(QM_RST_SCHED, &qm->misc_ctl);
5331 /* reset pcie device controller */
5332 ret = qm_controller_reset(qm);
5334 dev_err(&qm->pdev->dev, "controller reset failed (%d)\n", ret);
5339 static void qm_pf_reset_vf_prepare(struct hisi_qm *qm,
5340 enum qm_stop_reason stop_reason)
5342 enum qm_mb_cmd cmd = QM_VF_PREPARE_DONE;
5343 struct pci_dev *pdev = qm->pdev;
5346 ret = qm_reset_prepare_ready(qm);
5348 dev_err(&pdev->dev, "reset prepare not ready!\n");
5349 atomic_set(&qm->status.flags, QM_STOP);
5350 cmd = QM_VF_PREPARE_FAIL;
5354 ret = hisi_qm_stop(qm, stop_reason);
5356 dev_err(&pdev->dev, "failed to stop QM, ret = %d.\n", ret);
5357 atomic_set(&qm->status.flags, QM_STOP);
5358 cmd = QM_VF_PREPARE_FAIL;
5365 hisi_qm_set_hw_reset(qm, QM_RESET_STOP_TX_OFFSET);
5366 hisi_qm_set_hw_reset(qm, QM_RESET_STOP_RX_OFFSET);
5368 pci_save_state(pdev);
5369 ret = qm->ops->ping_pf(qm, cmd);
5371 dev_warn(&pdev->dev, "PF responds timeout in reset prepare!\n");
5374 static void qm_pf_reset_vf_done(struct hisi_qm *qm)
5376 enum qm_mb_cmd cmd = QM_VF_START_DONE;
5377 struct pci_dev *pdev = qm->pdev;
5380 pci_restore_state(pdev);
5381 ret = hisi_qm_start(qm);
5383 dev_err(&pdev->dev, "failed to start QM, ret = %d.\n", ret);
5384 cmd = QM_VF_START_FAIL;
5387 ret = qm->ops->ping_pf(qm, cmd);
5389 dev_warn(&pdev->dev, "PF responds timeout in reset done!\n");
5391 qm_reset_bit_clear(qm);
5394 static int qm_wait_pf_reset_finish(struct hisi_qm *qm)
5396 struct device *dev = &qm->pdev->dev;
5401 /* Wait for reset to finish */
5402 ret = readl_relaxed_poll_timeout(qm->io_base + QM_IFC_INT_SOURCE_V, val,
5403 val == BIT(0), QM_VF_RESET_WAIT_US,
5404 QM_VF_RESET_WAIT_TIMEOUT_US);
5405 /* hardware completion status should be available by this time */
5407 dev_err(dev, "couldn't get reset done status from PF, timeout!\n");
5412 * Whether message is got successfully,
5413 * VF needs to ack PF by clearing the interrupt.
5415 ret = qm_get_mb_cmd(qm, &msg, 0);
5416 qm_clear_cmd_interrupt(qm, 0);
5418 dev_err(dev, "failed to get msg from PF in reset done!\n");
5422 cmd = msg & QM_MB_CMD_DATA_MASK;
5423 if (cmd != QM_PF_RESET_DONE) {
5424 dev_err(dev, "the cmd(%u) is not reset done!\n", cmd);
5431 static void qm_pf_reset_vf_process(struct hisi_qm *qm,
5432 enum qm_stop_reason stop_reason)
5434 struct device *dev = &qm->pdev->dev;
5437 dev_info(dev, "device reset start...\n");
5439 /* The message is obtained by querying the register during resetting */
5441 qm_pf_reset_vf_prepare(qm, stop_reason);
5443 ret = qm_wait_pf_reset_finish(qm);
5445 goto err_get_status;
5447 qm_pf_reset_vf_done(qm);
5450 dev_info(dev, "device reset done.\n");
5456 qm_reset_bit_clear(qm);
5459 static void qm_handle_cmd_msg(struct hisi_qm *qm, u32 fun_num)
5461 struct device *dev = &qm->pdev->dev;
5467 * Get the msg from source by sending mailbox. Whether message is got
5468 * successfully, destination needs to ack source by clearing the interrupt.
5470 ret = qm_get_mb_cmd(qm, &msg, fun_num);
5471 qm_clear_cmd_interrupt(qm, BIT(fun_num));
5473 dev_err(dev, "failed to get msg from source!\n");
5477 cmd = msg & QM_MB_CMD_DATA_MASK;
5479 case QM_PF_FLR_PREPARE:
5480 qm_pf_reset_vf_process(qm, QM_FLR);
5482 case QM_PF_SRST_PREPARE:
5483 qm_pf_reset_vf_process(qm, QM_SOFT_RESET);
5486 qm_vf_get_qos(qm, fun_num);
5489 qm->mb_qos = msg >> QM_MB_CMD_DATA_SHIFT;
5492 dev_err(dev, "unsupported cmd %u sent by function(%u)!\n", cmd, fun_num);
5497 static void qm_cmd_process(struct work_struct *cmd_process)
5499 struct hisi_qm *qm = container_of(cmd_process,
5500 struct hisi_qm, cmd_process);
5501 u32 vfs_num = qm->vfs_num;
5505 if (qm->fun_type == QM_HW_PF) {
5506 val = readq(qm->io_base + QM_IFC_INT_SOURCE_P);
5510 for (i = 1; i <= vfs_num; i++) {
5512 qm_handle_cmd_msg(qm, i);
5518 qm_handle_cmd_msg(qm, 0);
5522 * hisi_qm_alg_register() - Register alg to crypto and add qm to qm_list.
5523 * @qm: The qm needs add.
5524 * @qm_list: The qm list.
5526 * This function adds qm to qm list, and will register algorithm to
5527 * crypto when the qm list is empty.
5529 int hisi_qm_alg_register(struct hisi_qm *qm, struct hisi_qm_list *qm_list)
5531 struct device *dev = &qm->pdev->dev;
5535 mutex_lock(&qm_list->lock);
5536 if (list_empty(&qm_list->list))
5538 list_add_tail(&qm->list, &qm_list->list);
5539 mutex_unlock(&qm_list->lock);
5541 if (qm->ver <= QM_HW_V2 && qm->use_sva) {
5542 dev_info(dev, "HW V2 not both use uacce sva mode and hardware crypto algs.\n");
5547 ret = qm_list->register_to_crypto(qm);
5549 mutex_lock(&qm_list->lock);
5550 list_del(&qm->list);
5551 mutex_unlock(&qm_list->lock);
5557 EXPORT_SYMBOL_GPL(hisi_qm_alg_register);
5560 * hisi_qm_alg_unregister() - Unregister alg from crypto and delete qm from
5562 * @qm: The qm needs delete.
5563 * @qm_list: The qm list.
5565 * This function deletes qm from qm list, and will unregister algorithm
5566 * from crypto when the qm list is empty.
5568 void hisi_qm_alg_unregister(struct hisi_qm *qm, struct hisi_qm_list *qm_list)
5570 mutex_lock(&qm_list->lock);
5571 list_del(&qm->list);
5572 mutex_unlock(&qm_list->lock);
5574 if (qm->ver <= QM_HW_V2 && qm->use_sva)
5577 if (list_empty(&qm_list->list))
5578 qm_list->unregister_from_crypto(qm);
5580 EXPORT_SYMBOL_GPL(hisi_qm_alg_unregister);
5582 static int qm_get_qp_num(struct hisi_qm *qm)
5584 if (qm->ver == QM_HW_V1)
5585 qm->ctrl_qp_num = QM_QNUM_V1;
5586 else if (qm->ver == QM_HW_V2)
5587 qm->ctrl_qp_num = QM_QNUM_V2;
5589 qm->ctrl_qp_num = readl(qm->io_base + QM_CAPBILITY) &
5592 if (qm->use_db_isolation)
5593 qm->max_qp_num = (readl(qm->io_base + QM_CAPBILITY) >>
5594 QM_QP_MAX_NUM_SHIFT) & QM_QP_NUN_MASK;
5596 qm->max_qp_num = qm->ctrl_qp_num;
5598 /* check if qp number is valid */
5599 if (qm->qp_num > qm->max_qp_num) {
5600 dev_err(&qm->pdev->dev, "qp num(%u) is more than max qp num(%u)!\n",
5601 qm->qp_num, qm->max_qp_num);
5608 static int qm_get_pci_res(struct hisi_qm *qm)
5610 struct pci_dev *pdev = qm->pdev;
5611 struct device *dev = &pdev->dev;
5614 ret = pci_request_mem_regions(pdev, qm->dev_name);
5616 dev_err(dev, "Failed to request mem regions!\n");
5620 qm->phys_base = pci_resource_start(pdev, PCI_BAR_2);
5621 qm->io_base = ioremap(qm->phys_base, pci_resource_len(pdev, PCI_BAR_2));
5624 goto err_request_mem_regions;
5627 if (qm->ver > QM_HW_V2) {
5628 if (qm->fun_type == QM_HW_PF)
5629 qm->use_db_isolation = readl(qm->io_base +
5630 QM_QUE_ISO_EN) & BIT(0);
5632 qm->use_db_isolation = readl(qm->io_base +
5633 QM_QUE_ISO_CFG_V) & BIT(0);
5636 if (qm->use_db_isolation) {
5637 qm->db_interval = QM_QP_DB_INTERVAL;
5638 qm->db_phys_base = pci_resource_start(pdev, PCI_BAR_4);
5639 qm->db_io_base = ioremap(qm->db_phys_base,
5640 pci_resource_len(pdev, PCI_BAR_4));
5641 if (!qm->db_io_base) {
5646 qm->db_phys_base = qm->phys_base;
5647 qm->db_io_base = qm->io_base;
5648 qm->db_interval = 0;
5651 if (qm->fun_type == QM_HW_PF) {
5652 ret = qm_get_qp_num(qm);
5654 goto err_db_ioremap;
5660 if (qm->use_db_isolation)
5661 iounmap(qm->db_io_base);
5663 iounmap(qm->io_base);
5664 err_request_mem_regions:
5665 pci_release_mem_regions(pdev);
5669 static int hisi_qm_pci_init(struct hisi_qm *qm)
5671 struct pci_dev *pdev = qm->pdev;
5672 struct device *dev = &pdev->dev;
5673 unsigned int num_vec;
5676 ret = pci_enable_device_mem(pdev);
5678 dev_err(dev, "Failed to enable device mem!\n");
5682 ret = qm_get_pci_res(qm);
5684 goto err_disable_pcidev;
5686 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
5688 goto err_get_pci_res;
5689 pci_set_master(pdev);
5691 if (!qm->ops->get_irq_num) {
5693 goto err_get_pci_res;
5695 num_vec = qm->ops->get_irq_num(qm);
5696 ret = pci_alloc_irq_vectors(pdev, num_vec, num_vec, PCI_IRQ_MSI);
5698 dev_err(dev, "Failed to enable MSI vectors!\n");
5699 goto err_get_pci_res;
5707 pci_disable_device(pdev);
5711 static void hisi_qm_init_work(struct hisi_qm *qm)
5713 INIT_WORK(&qm->work, qm_work_process);
5714 if (qm->fun_type == QM_HW_PF)
5715 INIT_WORK(&qm->rst_work, hisi_qm_controller_reset);
5717 if (qm->ver > QM_HW_V2)
5718 INIT_WORK(&qm->cmd_process, qm_cmd_process);
5721 static int hisi_qp_alloc_memory(struct hisi_qm *qm)
5723 struct device *dev = &qm->pdev->dev;
5727 qm->qp_array = kcalloc(qm->qp_num, sizeof(struct hisi_qp), GFP_KERNEL);
5731 /* one more page for device or qp statuses */
5732 qp_dma_size = qm->sqe_size * QM_Q_DEPTH +
5733 sizeof(struct qm_cqe) * QM_Q_DEPTH;
5734 qp_dma_size = PAGE_ALIGN(qp_dma_size) + PAGE_SIZE;
5735 for (i = 0; i < qm->qp_num; i++) {
5736 ret = hisi_qp_memory_init(qm, qp_dma_size, i);
5738 goto err_init_qp_mem;
5740 dev_dbg(dev, "allocate qp dma buf size=%zx)\n", qp_dma_size);
5745 hisi_qp_memory_uninit(qm, i);
5750 static int hisi_qm_memory_init(struct hisi_qm *qm)
5752 struct device *dev = &qm->pdev->dev;
5756 total_vfs = pci_sriov_get_totalvfs(qm->pdev);
5757 qm->factor = kcalloc(total_vfs + 1, sizeof(struct qm_shaper_factor), GFP_KERNEL);
5761 #define QM_INIT_BUF(qm, type, num) do { \
5762 (qm)->type = ((qm)->qdma.va + (off)); \
5763 (qm)->type##_dma = (qm)->qdma.dma + (off); \
5764 off += QMC_ALIGN(sizeof(struct qm_##type) * (num)); \
5767 idr_init(&qm->qp_idr);
5768 qm->qdma.size = QMC_ALIGN(sizeof(struct qm_eqe) * QM_EQ_DEPTH) +
5769 QMC_ALIGN(sizeof(struct qm_aeqe) * QM_Q_DEPTH) +
5770 QMC_ALIGN(sizeof(struct qm_sqc) * qm->qp_num) +
5771 QMC_ALIGN(sizeof(struct qm_cqc) * qm->qp_num);
5772 qm->qdma.va = dma_alloc_coherent(dev, qm->qdma.size, &qm->qdma.dma,
5774 dev_dbg(dev, "allocate qm dma buf size=%zx)\n", qm->qdma.size);
5777 goto err_alloc_qdma;
5780 QM_INIT_BUF(qm, eqe, QM_EQ_DEPTH);
5781 QM_INIT_BUF(qm, aeqe, QM_Q_DEPTH);
5782 QM_INIT_BUF(qm, sqc, qm->qp_num);
5783 QM_INIT_BUF(qm, cqc, qm->qp_num);
5785 ret = hisi_qp_alloc_memory(qm);
5787 goto err_alloc_qp_array;
5792 dma_free_coherent(dev, qm->qdma.size, qm->qdma.va, qm->qdma.dma);
5800 * hisi_qm_init() - Initialize configures about qm.
5801 * @qm: The qm needing init.
5803 * This function init qm, then we can call hisi_qm_start to put qm into work.
5805 int hisi_qm_init(struct hisi_qm *qm)
5807 struct pci_dev *pdev = qm->pdev;
5808 struct device *dev = &pdev->dev;
5811 hisi_qm_pre_init(qm);
5813 ret = hisi_qm_pci_init(qm);
5817 ret = qm_irq_register(qm);
5821 if (qm->fun_type == QM_HW_VF && qm->ver != QM_HW_V1) {
5822 /* v2 starts to support get vft by mailbox */
5823 ret = hisi_qm_get_vft(qm, &qm->qp_base, &qm->qp_num);
5825 goto err_irq_register;
5828 if (qm->mode == UACCE_MODE_SVA) {
5829 ret = qm_alloc_uacce(qm);
5831 dev_warn(dev, "fail to alloc uacce (%d)\n", ret);
5834 ret = hisi_qm_memory_init(qm);
5836 goto err_alloc_uacce;
5838 hisi_qm_init_work(qm);
5840 atomic_set(&qm->status.flags, QM_INIT);
5846 uacce_remove(qm->uacce);
5850 qm_irq_unregister(qm);
5852 hisi_qm_pci_uninit(qm);
5855 EXPORT_SYMBOL_GPL(hisi_qm_init);
5858 * hisi_qm_get_dfx_access() - Try to get dfx access.
5859 * @qm: pointer to accelerator device.
5861 * Try to get dfx access, then user can get message.
5863 * If device is in suspended, return failure, otherwise
5864 * bump up the runtime PM usage counter.
5866 int hisi_qm_get_dfx_access(struct hisi_qm *qm)
5868 struct device *dev = &qm->pdev->dev;
5870 if (pm_runtime_suspended(dev)) {
5871 dev_info(dev, "can not read/write - device in suspended.\n");
5875 return qm_pm_get_sync(qm);
5877 EXPORT_SYMBOL_GPL(hisi_qm_get_dfx_access);
5880 * hisi_qm_put_dfx_access() - Put dfx access.
5881 * @qm: pointer to accelerator device.
5883 * Put dfx access, drop runtime PM usage counter.
5885 void hisi_qm_put_dfx_access(struct hisi_qm *qm)
5889 EXPORT_SYMBOL_GPL(hisi_qm_put_dfx_access);
5892 * hisi_qm_pm_init() - Initialize qm runtime PM.
5893 * @qm: pointer to accelerator device.
5895 * Function that initialize qm runtime PM.
5897 void hisi_qm_pm_init(struct hisi_qm *qm)
5899 struct device *dev = &qm->pdev->dev;
5901 if (qm->fun_type == QM_HW_VF || qm->ver < QM_HW_V3)
5904 pm_runtime_set_autosuspend_delay(dev, QM_AUTOSUSPEND_DELAY);
5905 pm_runtime_use_autosuspend(dev);
5906 pm_runtime_put_noidle(dev);
5908 EXPORT_SYMBOL_GPL(hisi_qm_pm_init);
5911 * hisi_qm_pm_uninit() - Uninitialize qm runtime PM.
5912 * @qm: pointer to accelerator device.
5914 * Function that uninitialize qm runtime PM.
5916 void hisi_qm_pm_uninit(struct hisi_qm *qm)
5918 struct device *dev = &qm->pdev->dev;
5920 if (qm->fun_type == QM_HW_VF || qm->ver < QM_HW_V3)
5923 pm_runtime_get_noresume(dev);
5924 pm_runtime_dont_use_autosuspend(dev);
5926 EXPORT_SYMBOL_GPL(hisi_qm_pm_uninit);
5928 static int qm_prepare_for_suspend(struct hisi_qm *qm)
5930 struct pci_dev *pdev = qm->pdev;
5934 ret = qm->ops->set_msi(qm, false);
5936 pci_err(pdev, "failed to disable MSI before suspending!\n");
5940 /* shutdown OOO register */
5941 writel(ACC_MASTER_GLOBAL_CTRL_SHUTDOWN,
5942 qm->io_base + ACC_MASTER_GLOBAL_CTRL);
5944 ret = readl_relaxed_poll_timeout(qm->io_base + ACC_MASTER_TRANS_RETURN,
5946 (val == ACC_MASTER_TRANS_RETURN_RW),
5947 POLL_PERIOD, POLL_TIMEOUT);
5949 pci_emerg(pdev, "Bus lock! Please reset system.\n");
5953 ret = qm_set_pf_mse(qm, false);
5955 pci_err(pdev, "failed to disable MSE before suspending!\n");
5960 static int qm_rebuild_for_resume(struct hisi_qm *qm)
5962 struct pci_dev *pdev = qm->pdev;
5965 ret = qm_set_pf_mse(qm, true);
5967 pci_err(pdev, "failed to enable MSE after resuming!\n");
5971 ret = qm->ops->set_msi(qm, true);
5973 pci_err(pdev, "failed to enable MSI after resuming!\n");
5977 ret = qm_dev_hw_init(qm);
5979 pci_err(pdev, "failed to init device after resuming\n");
5984 hisi_qm_dev_err_init(qm);
5990 * hisi_qm_suspend() - Runtime suspend of given device.
5991 * @dev: device to suspend.
5993 * Function that suspend the device.
5995 int hisi_qm_suspend(struct device *dev)
5997 struct pci_dev *pdev = to_pci_dev(dev);
5998 struct hisi_qm *qm = pci_get_drvdata(pdev);
6001 pci_info(pdev, "entering suspended state\n");
6003 ret = hisi_qm_stop(qm, QM_NORMAL);
6005 pci_err(pdev, "failed to stop qm(%d)\n", ret);
6009 ret = qm_prepare_for_suspend(qm);
6011 pci_err(pdev, "failed to prepare suspended(%d)\n", ret);
6015 EXPORT_SYMBOL_GPL(hisi_qm_suspend);
6018 * hisi_qm_resume() - Runtime resume of given device.
6019 * @dev: device to resume.
6021 * Function that resume the device.
6023 int hisi_qm_resume(struct device *dev)
6025 struct pci_dev *pdev = to_pci_dev(dev);
6026 struct hisi_qm *qm = pci_get_drvdata(pdev);
6029 pci_info(pdev, "resuming from suspend state\n");
6031 ret = qm_rebuild_for_resume(qm);
6033 pci_err(pdev, "failed to rebuild resume(%d)\n", ret);
6037 ret = hisi_qm_start(qm);
6039 pci_err(pdev, "failed to start qm(%d)\n", ret);
6043 EXPORT_SYMBOL_GPL(hisi_qm_resume);
6045 MODULE_LICENSE("GPL v2");
6046 MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>");
6047 MODULE_DESCRIPTION("HiSilicon Accelerator queue manager driver");