1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 HiSilicon Limited. */
4 #include <linux/bitmap.h>
5 #include <linux/debugfs.h>
6 #include <linux/dma-mapping.h>
8 #include <linux/irqreturn.h>
9 #include <linux/log2.h>
10 #include <linux/seq_file.h>
11 #include <linux/slab.h>
14 /* eq/aeq irq enable */
15 #define QM_VF_AEQ_INT_SOURCE 0x0
16 #define QM_VF_AEQ_INT_MASK 0x4
17 #define QM_VF_EQ_INT_SOURCE 0x8
18 #define QM_VF_EQ_INT_MASK 0xc
19 #define QM_IRQ_NUM_V1 1
20 #define QM_IRQ_NUM_PF_V2 4
21 #define QM_IRQ_NUM_VF_V2 2
23 #define QM_EQ_EVENT_IRQ_VECTOR 0
24 #define QM_AEQ_EVENT_IRQ_VECTOR 1
25 #define QM_ABNORMAL_EVENT_IRQ_VECTOR 3
28 #define QM_MB_CMD_SQC 0x0
29 #define QM_MB_CMD_CQC 0x1
30 #define QM_MB_CMD_EQC 0x2
31 #define QM_MB_CMD_AEQC 0x3
32 #define QM_MB_CMD_SQC_BT 0x4
33 #define QM_MB_CMD_CQC_BT 0x5
34 #define QM_MB_CMD_SQC_VFT_V2 0x6
36 #define QM_MB_CMD_SEND_BASE 0x300
37 #define QM_MB_EVENT_SHIFT 8
38 #define QM_MB_BUSY_SHIFT 13
39 #define QM_MB_OP_SHIFT 14
40 #define QM_MB_CMD_DATA_ADDR_L 0x304
41 #define QM_MB_CMD_DATA_ADDR_H 0x308
44 #define QM_SQ_HOP_NUM_SHIFT 0
45 #define QM_SQ_PAGE_SIZE_SHIFT 4
46 #define QM_SQ_BUF_SIZE_SHIFT 8
47 #define QM_SQ_SQE_SIZE_SHIFT 12
48 #define QM_SQ_PRIORITY_SHIFT 0
49 #define QM_SQ_ORDERS_SHIFT 4
50 #define QM_SQ_TYPE_SHIFT 8
52 #define QM_SQ_TYPE_MASK GENMASK(3, 0)
55 #define QM_CQ_HOP_NUM_SHIFT 0
56 #define QM_CQ_PAGE_SIZE_SHIFT 4
57 #define QM_CQ_BUF_SIZE_SHIFT 8
58 #define QM_CQ_CQE_SIZE_SHIFT 12
59 #define QM_CQ_PHASE_SHIFT 0
60 #define QM_CQ_FLAG_SHIFT 1
62 #define QM_CQE_PHASE(cqe) (le16_to_cpu((cqe)->w7) & 0x1)
63 #define QM_QC_CQE_SIZE 4
66 #define QM_EQE_AEQE_SIZE (2UL << 12)
67 #define QM_EQC_PHASE_SHIFT 16
69 #define QM_EQE_PHASE(eqe) ((le32_to_cpu((eqe)->dw0) >> 16) & 0x1)
70 #define QM_EQE_CQN_MASK GENMASK(15, 0)
72 #define QM_AEQE_PHASE(aeqe) ((le32_to_cpu((aeqe)->dw0) >> 16) & 0x1)
73 #define QM_AEQE_TYPE_SHIFT 17
75 #define QM_DOORBELL_CMD_SQ 0
76 #define QM_DOORBELL_CMD_CQ 1
77 #define QM_DOORBELL_CMD_EQ 2
78 #define QM_DOORBELL_CMD_AEQ 3
80 #define QM_DOORBELL_BASE_V1 0x340
81 #define QM_DB_CMD_SHIFT_V1 16
82 #define QM_DB_INDEX_SHIFT_V1 32
83 #define QM_DB_PRIORITY_SHIFT_V1 48
84 #define QM_DOORBELL_SQ_CQ_BASE_V2 0x1000
85 #define QM_DOORBELL_EQ_AEQ_BASE_V2 0x2000
86 #define QM_DB_CMD_SHIFT_V2 12
87 #define QM_DB_RAND_SHIFT_V2 16
88 #define QM_DB_INDEX_SHIFT_V2 32
89 #define QM_DB_PRIORITY_SHIFT_V2 48
91 #define QM_MEM_START_INIT 0x100040
92 #define QM_MEM_INIT_DONE 0x100044
93 #define QM_VFT_CFG_RDY 0x10006c
94 #define QM_VFT_CFG_OP_WR 0x100058
95 #define QM_VFT_CFG_TYPE 0x10005c
96 #define QM_SQC_VFT 0x0
97 #define QM_CQC_VFT 0x1
98 #define QM_VFT_CFG 0x100060
99 #define QM_VFT_CFG_OP_ENABLE 0x100054
101 #define QM_VFT_CFG_DATA_L 0x100064
102 #define QM_VFT_CFG_DATA_H 0x100068
103 #define QM_SQC_VFT_BUF_SIZE (7ULL << 8)
104 #define QM_SQC_VFT_SQC_SIZE (5ULL << 12)
105 #define QM_SQC_VFT_INDEX_NUMBER (1ULL << 16)
106 #define QM_SQC_VFT_START_SQN_SHIFT 28
107 #define QM_SQC_VFT_VALID (1ULL << 44)
108 #define QM_SQC_VFT_SQN_SHIFT 45
109 #define QM_CQC_VFT_BUF_SIZE (7ULL << 8)
110 #define QM_CQC_VFT_SQC_SIZE (5ULL << 12)
111 #define QM_CQC_VFT_INDEX_NUMBER (1ULL << 16)
112 #define QM_CQC_VFT_VALID (1ULL << 28)
114 #define QM_SQC_VFT_BASE_SHIFT_V2 28
115 #define QM_SQC_VFT_BASE_MASK_V2 GENMASK(5, 0)
116 #define QM_SQC_VFT_NUM_SHIFT_V2 45
117 #define QM_SQC_VFT_NUM_MASK_v2 GENMASK(9, 0)
119 #define QM_DFX_CNT_CLR_CE 0x100118
121 #define QM_ABNORMAL_INT_SOURCE 0x100000
122 #define QM_ABNORMAL_INT_MASK 0x100004
123 #define QM_ABNORMAL_INT_MASK_VALUE 0x1fff
124 #define QM_ABNORMAL_INT_STATUS 0x100008
125 #define QM_ABNORMAL_INF00 0x100010
126 #define QM_FIFO_OVERFLOW_TYPE 0xc0
127 #define QM_FIFO_OVERFLOW_TYPE_SHIFT 6
128 #define QM_FIFO_OVERFLOW_VF 0x3f
129 #define QM_ABNORMAL_INF01 0x100014
130 #define QM_DB_TIMEOUT_TYPE 0xc0
131 #define QM_DB_TIMEOUT_TYPE_SHIFT 6
132 #define QM_DB_TIMEOUT_VF 0x3f
133 #define QM_RAS_CE_ENABLE 0x1000ec
134 #define QM_RAS_FE_ENABLE 0x1000f0
135 #define QM_RAS_NFE_ENABLE 0x1000f4
136 #define QM_RAS_CE_THRESHOLD 0x1000f8
137 #define QM_RAS_CE_TIMES_PER_IRQ 1
138 #define QM_RAS_MSI_INT_SEL 0x1040f4
140 #define QM_CACHE_WB_START 0x204
141 #define QM_CACHE_WB_DONE 0x208
144 #define QM_SQE_DATA_ALIGN_MASK GENMASK(6, 0)
145 #define QMC_ALIGN(sz) ALIGN(sz, 32)
147 #define QM_DBG_TMP_BUF_LEN 22
149 #define QM_MK_CQC_DW3_V1(hop_num, pg_sz, buf_sz, cqe_sz) \
150 (((hop_num) << QM_CQ_HOP_NUM_SHIFT) | \
151 ((pg_sz) << QM_CQ_PAGE_SIZE_SHIFT) | \
152 ((buf_sz) << QM_CQ_BUF_SIZE_SHIFT) | \
153 ((cqe_sz) << QM_CQ_CQE_SIZE_SHIFT))
155 #define QM_MK_CQC_DW3_V2(cqe_sz) \
156 ((QM_Q_DEPTH - 1) | ((cqe_sz) << QM_CQ_CQE_SIZE_SHIFT))
158 #define QM_MK_SQC_W13(priority, orders, alg_type) \
159 (((priority) << QM_SQ_PRIORITY_SHIFT) | \
160 ((orders) << QM_SQ_ORDERS_SHIFT) | \
161 (((alg_type) & QM_SQ_TYPE_MASK) << QM_SQ_TYPE_SHIFT))
163 #define QM_MK_SQC_DW3_V1(hop_num, pg_sz, buf_sz, sqe_sz) \
164 (((hop_num) << QM_SQ_HOP_NUM_SHIFT) | \
165 ((pg_sz) << QM_SQ_PAGE_SIZE_SHIFT) | \
166 ((buf_sz) << QM_SQ_BUF_SIZE_SHIFT) | \
167 ((u32)ilog2(sqe_sz) << QM_SQ_SQE_SIZE_SHIFT))
169 #define QM_MK_SQC_DW3_V2(sqe_sz) \
170 ((QM_Q_DEPTH - 1) | ((u32)ilog2(sqe_sz) << QM_SQ_SQE_SIZE_SHIFT))
172 #define INIT_QC_COMMON(qc, base, pasid) do { \
175 (qc)->base_l = cpu_to_le32(lower_32_bits(base)); \
176 (qc)->base_h = cpu_to_le32(upper_32_bits(base)); \
180 (qc)->pasid = cpu_to_le16(pasid); \
272 struct hisi_qm_hw_ops
{
273 int (*get_vft
)(struct hisi_qm
*qm
, u32
*base
, u32
*number
);
274 void (*qm_db
)(struct hisi_qm
*qm
, u16 qn
,
275 u8 cmd
, u16 index
, u8 priority
);
276 u32 (*get_irq_num
)(struct hisi_qm
*qm
);
277 int (*debug_init
)(struct hisi_qm
*qm
);
278 void (*hw_error_init
)(struct hisi_qm
*qm
, u32 ce
, u32 nfe
, u32 fe
,
280 pci_ers_result_t (*hw_error_handle
)(struct hisi_qm
*qm
);
283 static const char * const qm_debug_file_name
[] = {
284 [CURRENT_Q
] = "current_q",
285 [CLEAR_ENABLE
] = "clear_enable",
288 struct hisi_qm_hw_error
{
293 static const struct hisi_qm_hw_error qm_hw_error
[] = {
294 { .int_msk
= BIT(0), .msg
= "qm_axi_rresp" },
295 { .int_msk
= BIT(1), .msg
= "qm_axi_bresp" },
296 { .int_msk
= BIT(2), .msg
= "qm_ecc_mbit" },
297 { .int_msk
= BIT(3), .msg
= "qm_ecc_1bit" },
298 { .int_msk
= BIT(4), .msg
= "qm_acc_get_task_timeout" },
299 { .int_msk
= BIT(5), .msg
= "qm_acc_do_task_timeout" },
300 { .int_msk
= BIT(6), .msg
= "qm_acc_wb_not_ready_timeout" },
301 { .int_msk
= BIT(7), .msg
= "qm_sq_cq_vf_invalid" },
302 { .int_msk
= BIT(8), .msg
= "qm_cq_vf_invalid" },
303 { .int_msk
= BIT(9), .msg
= "qm_sq_vf_invalid" },
304 { .int_msk
= BIT(10), .msg
= "qm_db_timeout" },
305 { .int_msk
= BIT(11), .msg
= "qm_of_fifo_of" },
306 { .int_msk
= BIT(12), .msg
= "qm_db_random_invalid" },
310 static const char * const qm_db_timeout
[] = {
311 "sq", "cq", "eq", "aeq",
314 static const char * const qm_fifo_overflow
[] = {
318 /* return 0 mailbox ready, -ETIMEDOUT hardware timeout */
319 static int qm_wait_mb_ready(struct hisi_qm
*qm
)
323 return readl_relaxed_poll_timeout(qm
->io_base
+ QM_MB_CMD_SEND_BASE
,
324 val
, !((val
>> QM_MB_BUSY_SHIFT
) &
328 /* 128 bit should be written to hardware at one time to trigger a mailbox */
329 static void qm_mb_write(struct hisi_qm
*qm
, const void *src
)
331 void __iomem
*fun_base
= qm
->io_base
+ QM_MB_CMD_SEND_BASE
;
332 unsigned long tmp0
= 0, tmp1
= 0;
334 if (!IS_ENABLED(CONFIG_ARM64
)) {
335 memcpy_toio(fun_base
, src
, 16);
340 asm volatile("ldp %0, %1, %3\n"
345 "+Q" (*((char __iomem
*)fun_base
))
346 : "Q" (*((char *)src
))
350 static int qm_mb(struct hisi_qm
*qm
, u8 cmd
, dma_addr_t dma_addr
, u16 queue
,
353 struct qm_mailbox mailbox
;
356 dev_dbg(&qm
->pdev
->dev
, "QM mailbox request to q%u: %u-%llx\n",
357 queue
, cmd
, (unsigned long long)dma_addr
);
359 mailbox
.w0
= cpu_to_le16(cmd
|
360 (op
? 0x1 << QM_MB_OP_SHIFT
: 0) |
361 (0x1 << QM_MB_BUSY_SHIFT
));
362 mailbox
.queue_num
= cpu_to_le16(queue
);
363 mailbox
.base_l
= cpu_to_le32(lower_32_bits(dma_addr
));
364 mailbox
.base_h
= cpu_to_le32(upper_32_bits(dma_addr
));
367 mutex_lock(&qm
->mailbox_lock
);
369 if (unlikely(qm_wait_mb_ready(qm
))) {
371 dev_err(&qm
->pdev
->dev
, "QM mailbox is busy to start!\n");
375 qm_mb_write(qm
, &mailbox
);
377 if (unlikely(qm_wait_mb_ready(qm
))) {
379 dev_err(&qm
->pdev
->dev
, "QM mailbox operation timeout!\n");
384 mutex_unlock(&qm
->mailbox_lock
);
389 static void qm_db_v1(struct hisi_qm
*qm
, u16 qn
, u8 cmd
, u16 index
, u8 priority
)
393 doorbell
= qn
| ((u64
)cmd
<< QM_DB_CMD_SHIFT_V1
) |
394 ((u64
)index
<< QM_DB_INDEX_SHIFT_V1
) |
395 ((u64
)priority
<< QM_DB_PRIORITY_SHIFT_V1
);
397 writeq(doorbell
, qm
->io_base
+ QM_DOORBELL_BASE_V1
);
400 static void qm_db_v2(struct hisi_qm
*qm
, u16 qn
, u8 cmd
, u16 index
, u8 priority
)
406 if (cmd
== QM_DOORBELL_CMD_SQ
|| cmd
== QM_DOORBELL_CMD_CQ
)
407 dbase
= QM_DOORBELL_SQ_CQ_BASE_V2
;
409 dbase
= QM_DOORBELL_EQ_AEQ_BASE_V2
;
411 doorbell
= qn
| ((u64
)cmd
<< QM_DB_CMD_SHIFT_V2
) |
412 ((u64
)randata
<< QM_DB_RAND_SHIFT_V2
) |
413 ((u64
)index
<< QM_DB_INDEX_SHIFT_V2
) |
414 ((u64
)priority
<< QM_DB_PRIORITY_SHIFT_V2
);
416 writeq(doorbell
, qm
->io_base
+ dbase
);
419 static void qm_db(struct hisi_qm
*qm
, u16 qn
, u8 cmd
, u16 index
, u8 priority
)
421 dev_dbg(&qm
->pdev
->dev
, "QM doorbell request: qn=%u, cmd=%u, index=%u\n",
424 qm
->ops
->qm_db(qm
, qn
, cmd
, index
, priority
);
427 static int qm_dev_mem_reset(struct hisi_qm
*qm
)
431 writel(0x1, qm
->io_base
+ QM_MEM_START_INIT
);
432 return readl_relaxed_poll_timeout(qm
->io_base
+ QM_MEM_INIT_DONE
, val
,
433 val
& BIT(0), 10, 1000);
436 static u32
qm_get_irq_num_v1(struct hisi_qm
*qm
)
438 return QM_IRQ_NUM_V1
;
441 static u32
qm_get_irq_num_v2(struct hisi_qm
*qm
)
443 if (qm
->fun_type
== QM_HW_PF
)
444 return QM_IRQ_NUM_PF_V2
;
446 return QM_IRQ_NUM_VF_V2
;
449 static struct hisi_qp
*qm_to_hisi_qp(struct hisi_qm
*qm
, struct qm_eqe
*eqe
)
451 u16 cqn
= le32_to_cpu(eqe
->dw0
) & QM_EQE_CQN_MASK
;
453 return qm
->qp_array
[cqn
];
456 static void qm_cq_head_update(struct hisi_qp
*qp
)
458 if (qp
->qp_status
.cq_head
== QM_Q_DEPTH
- 1) {
459 qp
->qp_status
.cqc_phase
= !qp
->qp_status
.cqc_phase
;
460 qp
->qp_status
.cq_head
= 0;
462 qp
->qp_status
.cq_head
++;
466 static void qm_poll_qp(struct hisi_qp
*qp
, struct hisi_qm
*qm
)
468 struct qm_cqe
*cqe
= qp
->cqe
+ qp
->qp_status
.cq_head
;
471 while (QM_CQE_PHASE(cqe
) == qp
->qp_status
.cqc_phase
) {
473 qp
->req_cb(qp
, qp
->sqe
+ qm
->sqe_size
*
474 le16_to_cpu(cqe
->sq_head
));
475 qm_cq_head_update(qp
);
476 cqe
= qp
->cqe
+ qp
->qp_status
.cq_head
;
477 qm_db(qm
, qp
->qp_id
, QM_DOORBELL_CMD_CQ
,
478 qp
->qp_status
.cq_head
, 0);
479 atomic_dec(&qp
->qp_status
.used
);
483 qm_db(qm
, qp
->qp_id
, QM_DOORBELL_CMD_CQ
,
484 qp
->qp_status
.cq_head
, 1);
488 static void qm_qp_work_func(struct work_struct
*work
)
492 qp
= container_of(work
, struct hisi_qp
, work
);
493 qm_poll_qp(qp
, qp
->qm
);
496 static irqreturn_t
qm_irq_handler(int irq
, void *data
)
498 struct hisi_qm
*qm
= data
;
499 struct qm_eqe
*eqe
= qm
->eqe
+ qm
->status
.eq_head
;
503 while (QM_EQE_PHASE(eqe
) == qm
->status
.eqc_phase
) {
505 qp
= qm_to_hisi_qp(qm
, eqe
);
507 queue_work(qp
->wq
, &qp
->work
);
509 if (qm
->status
.eq_head
== QM_Q_DEPTH
- 1) {
510 qm
->status
.eqc_phase
= !qm
->status
.eqc_phase
;
512 qm
->status
.eq_head
= 0;
515 qm
->status
.eq_head
++;
518 if (eqe_num
== QM_Q_DEPTH
/ 2 - 1) {
520 qm_db(qm
, 0, QM_DOORBELL_CMD_EQ
, qm
->status
.eq_head
, 0);
524 qm_db(qm
, 0, QM_DOORBELL_CMD_EQ
, qm
->status
.eq_head
, 0);
529 static irqreturn_t
qm_irq(int irq
, void *data
)
531 struct hisi_qm
*qm
= data
;
533 if (readl(qm
->io_base
+ QM_VF_EQ_INT_SOURCE
))
534 return qm_irq_handler(irq
, data
);
536 dev_err(&qm
->pdev
->dev
, "invalid int source\n");
537 qm_db(qm
, 0, QM_DOORBELL_CMD_EQ
, qm
->status
.eq_head
, 0);
542 static irqreturn_t
qm_aeq_irq(int irq
, void *data
)
544 struct hisi_qm
*qm
= data
;
545 struct qm_aeqe
*aeqe
= qm
->aeqe
+ qm
->status
.aeq_head
;
548 if (!readl(qm
->io_base
+ QM_VF_AEQ_INT_SOURCE
))
551 while (QM_AEQE_PHASE(aeqe
) == qm
->status
.aeqc_phase
) {
552 type
= le32_to_cpu(aeqe
->dw0
) >> QM_AEQE_TYPE_SHIFT
;
553 if (type
< ARRAY_SIZE(qm_fifo_overflow
))
554 dev_err(&qm
->pdev
->dev
, "%s overflow\n",
555 qm_fifo_overflow
[type
]);
557 dev_err(&qm
->pdev
->dev
, "unknown error type %d\n",
560 if (qm
->status
.aeq_head
== QM_Q_DEPTH
- 1) {
561 qm
->status
.aeqc_phase
= !qm
->status
.aeqc_phase
;
563 qm
->status
.aeq_head
= 0;
566 qm
->status
.aeq_head
++;
569 qm_db(qm
, 0, QM_DOORBELL_CMD_AEQ
, qm
->status
.aeq_head
, 0);
575 static irqreturn_t
qm_abnormal_irq(int irq
, void *data
)
577 const struct hisi_qm_hw_error
*err
= qm_hw_error
;
578 struct hisi_qm
*qm
= data
;
579 struct device
*dev
= &qm
->pdev
->dev
;
580 u32 error_status
, tmp
;
583 tmp
= readl(qm
->io_base
+ QM_ABNORMAL_INT_STATUS
);
584 error_status
= qm
->msi_mask
& tmp
;
587 if (err
->int_msk
& error_status
)
588 dev_err(dev
, "%s [error status=0x%x] found\n",
589 err
->msg
, err
->int_msk
);
595 writel(error_status
, qm
->io_base
+ QM_ABNORMAL_INT_SOURCE
);
600 static int qm_irq_register(struct hisi_qm
*qm
)
602 struct pci_dev
*pdev
= qm
->pdev
;
605 ret
= request_irq(pci_irq_vector(pdev
, QM_EQ_EVENT_IRQ_VECTOR
),
606 qm_irq
, IRQF_SHARED
, qm
->dev_name
, qm
);
610 if (qm
->ver
== QM_HW_V2
) {
611 ret
= request_irq(pci_irq_vector(pdev
, QM_AEQ_EVENT_IRQ_VECTOR
),
612 qm_aeq_irq
, IRQF_SHARED
, qm
->dev_name
, qm
);
616 if (qm
->fun_type
== QM_HW_PF
) {
617 ret
= request_irq(pci_irq_vector(pdev
,
618 QM_ABNORMAL_EVENT_IRQ_VECTOR
),
619 qm_abnormal_irq
, IRQF_SHARED
,
622 goto err_abonormal_irq
;
629 free_irq(pci_irq_vector(pdev
, QM_AEQ_EVENT_IRQ_VECTOR
), qm
);
631 free_irq(pci_irq_vector(pdev
, QM_EQ_EVENT_IRQ_VECTOR
), qm
);
635 static void qm_irq_unregister(struct hisi_qm
*qm
)
637 struct pci_dev
*pdev
= qm
->pdev
;
639 free_irq(pci_irq_vector(pdev
, QM_EQ_EVENT_IRQ_VECTOR
), qm
);
641 if (qm
->ver
== QM_HW_V2
) {
642 free_irq(pci_irq_vector(pdev
, QM_AEQ_EVENT_IRQ_VECTOR
), qm
);
644 if (qm
->fun_type
== QM_HW_PF
)
645 free_irq(pci_irq_vector(pdev
,
646 QM_ABNORMAL_EVENT_IRQ_VECTOR
), qm
);
650 static void qm_init_qp_status(struct hisi_qp
*qp
)
652 struct hisi_qp_status
*qp_status
= &qp
->qp_status
;
654 qp_status
->sq_tail
= 0;
655 qp_status
->cq_head
= 0;
656 qp_status
->cqc_phase
= true;
657 qp_status
->flags
= 0;
660 static void qm_vft_data_cfg(struct hisi_qm
*qm
, enum vft_type type
, u32 base
,
670 tmp
= QM_SQC_VFT_BUF_SIZE
|
671 QM_SQC_VFT_SQC_SIZE
|
672 QM_SQC_VFT_INDEX_NUMBER
|
674 (u64
)base
<< QM_SQC_VFT_START_SQN_SHIFT
;
677 tmp
= (u64
)base
<< QM_SQC_VFT_START_SQN_SHIFT
|
679 (u64
)(number
- 1) << QM_SQC_VFT_SQN_SHIFT
;
688 tmp
= QM_CQC_VFT_BUF_SIZE
|
689 QM_CQC_VFT_SQC_SIZE
|
690 QM_CQC_VFT_INDEX_NUMBER
|
694 tmp
= QM_CQC_VFT_VALID
;
703 writel(lower_32_bits(tmp
), qm
->io_base
+ QM_VFT_CFG_DATA_L
);
704 writel(upper_32_bits(tmp
), qm
->io_base
+ QM_VFT_CFG_DATA_H
);
707 static int qm_set_vft_common(struct hisi_qm
*qm
, enum vft_type type
,
708 u32 fun_num
, u32 base
, u32 number
)
713 ret
= readl_relaxed_poll_timeout(qm
->io_base
+ QM_VFT_CFG_RDY
, val
,
714 val
& BIT(0), 10, 1000);
718 writel(0x0, qm
->io_base
+ QM_VFT_CFG_OP_WR
);
719 writel(type
, qm
->io_base
+ QM_VFT_CFG_TYPE
);
720 writel(fun_num
, qm
->io_base
+ QM_VFT_CFG
);
722 qm_vft_data_cfg(qm
, type
, base
, number
);
724 writel(0x0, qm
->io_base
+ QM_VFT_CFG_RDY
);
725 writel(0x1, qm
->io_base
+ QM_VFT_CFG_OP_ENABLE
);
727 return readl_relaxed_poll_timeout(qm
->io_base
+ QM_VFT_CFG_RDY
, val
,
728 val
& BIT(0), 10, 1000);
731 /* The config should be conducted after qm_dev_mem_reset() */
732 static int qm_set_sqc_cqc_vft(struct hisi_qm
*qm
, u32 fun_num
, u32 base
,
737 for (i
= SQC_VFT
; i
<= CQC_VFT
; i
++) {
738 ret
= qm_set_vft_common(qm
, i
, fun_num
, base
, number
);
746 static int qm_get_vft_v2(struct hisi_qm
*qm
, u32
*base
, u32
*number
)
751 ret
= qm_mb(qm
, QM_MB_CMD_SQC_VFT_V2
, 0, 0, 1);
755 sqc_vft
= readl(qm
->io_base
+ QM_MB_CMD_DATA_ADDR_L
) |
756 ((u64
)readl(qm
->io_base
+ QM_MB_CMD_DATA_ADDR_H
) << 32);
757 *base
= QM_SQC_VFT_BASE_MASK_V2
& (sqc_vft
>> QM_SQC_VFT_BASE_SHIFT_V2
);
758 *number
= (QM_SQC_VFT_NUM_MASK_v2
&
759 (sqc_vft
>> QM_SQC_VFT_NUM_SHIFT_V2
)) + 1;
764 static struct hisi_qm
*file_to_qm(struct debugfs_file
*file
)
766 struct qm_debug
*debug
= file
->debug
;
768 return container_of(debug
, struct hisi_qm
, debug
);
771 static u32
current_q_read(struct debugfs_file
*file
)
773 struct hisi_qm
*qm
= file_to_qm(file
);
775 return readl(qm
->io_base
+ QM_DFX_SQE_CNT_VF_SQN
) >> QM_DFX_QN_SHIFT
;
778 static int current_q_write(struct debugfs_file
*file
, u32 val
)
780 struct hisi_qm
*qm
= file_to_qm(file
);
783 if (val
>= qm
->debug
.curr_qm_qp_num
)
786 tmp
= val
<< QM_DFX_QN_SHIFT
|
787 (readl(qm
->io_base
+ QM_DFX_SQE_CNT_VF_SQN
) & CURRENT_FUN_MASK
);
788 writel(tmp
, qm
->io_base
+ QM_DFX_SQE_CNT_VF_SQN
);
790 tmp
= val
<< QM_DFX_QN_SHIFT
|
791 (readl(qm
->io_base
+ QM_DFX_CQE_CNT_VF_CQN
) & CURRENT_FUN_MASK
);
792 writel(tmp
, qm
->io_base
+ QM_DFX_CQE_CNT_VF_CQN
);
797 static u32
clear_enable_read(struct debugfs_file
*file
)
799 struct hisi_qm
*qm
= file_to_qm(file
);
801 return readl(qm
->io_base
+ QM_DFX_CNT_CLR_CE
);
804 /* rd_clr_ctrl 1 enable read clear, otherwise 0 disable it */
805 static int clear_enable_write(struct debugfs_file
*file
, u32 rd_clr_ctrl
)
807 struct hisi_qm
*qm
= file_to_qm(file
);
812 writel(rd_clr_ctrl
, qm
->io_base
+ QM_DFX_CNT_CLR_CE
);
817 static ssize_t
qm_debug_read(struct file
*filp
, char __user
*buf
,
818 size_t count
, loff_t
*pos
)
820 struct debugfs_file
*file
= filp
->private_data
;
821 enum qm_debug_file index
= file
->index
;
822 char tbuf
[QM_DBG_TMP_BUF_LEN
];
826 mutex_lock(&file
->lock
);
829 val
= current_q_read(file
);
832 val
= clear_enable_read(file
);
835 mutex_unlock(&file
->lock
);
838 mutex_unlock(&file
->lock
);
839 ret
= sprintf(tbuf
, "%u\n", val
);
840 return simple_read_from_buffer(buf
, count
, pos
, tbuf
, ret
);
843 static ssize_t
qm_debug_write(struct file
*filp
, const char __user
*buf
,
844 size_t count
, loff_t
*pos
)
846 struct debugfs_file
*file
= filp
->private_data
;
847 enum qm_debug_file index
= file
->index
;
849 char tbuf
[QM_DBG_TMP_BUF_LEN
];
855 if (count
>= QM_DBG_TMP_BUF_LEN
)
858 len
= simple_write_to_buffer(tbuf
, QM_DBG_TMP_BUF_LEN
- 1, pos
, buf
,
864 if (kstrtoul(tbuf
, 0, &val
))
867 mutex_lock(&file
->lock
);
870 ret
= current_q_write(file
, val
);
875 ret
= clear_enable_write(file
, val
);
883 mutex_unlock(&file
->lock
);
888 mutex_unlock(&file
->lock
);
892 static const struct file_operations qm_debug_fops
= {
893 .owner
= THIS_MODULE
,
895 .read
= qm_debug_read
,
896 .write
= qm_debug_write
,
899 struct qm_dfx_registers
{
904 #define CNT_CYC_REGS_NUM 10
905 static struct qm_dfx_registers qm_dfx_regs
[] = {
906 /* XXX_CNT are reading clear register */
907 {"QM_ECC_1BIT_CNT ", 0x104000ull
},
908 {"QM_ECC_MBIT_CNT ", 0x104008ull
},
909 {"QM_DFX_MB_CNT ", 0x104018ull
},
910 {"QM_DFX_DB_CNT ", 0x104028ull
},
911 {"QM_DFX_SQE_CNT ", 0x104038ull
},
912 {"QM_DFX_CQE_CNT ", 0x104048ull
},
913 {"QM_DFX_SEND_SQE_TO_ACC_CNT ", 0x104050ull
},
914 {"QM_DFX_WB_SQE_FROM_ACC_CNT ", 0x104058ull
},
915 {"QM_DFX_ACC_FINISH_CNT ", 0x104060ull
},
916 {"QM_DFX_CQE_ERR_CNT ", 0x1040b4ull
},
917 {"QM_DFX_FUNS_ACTIVE_ST ", 0x200ull
},
918 {"QM_ECC_1BIT_INF ", 0x104004ull
},
919 {"QM_ECC_MBIT_INF ", 0x10400cull
},
920 {"QM_DFX_ACC_RDY_VLD0 ", 0x1040a0ull
},
921 {"QM_DFX_ACC_RDY_VLD1 ", 0x1040a4ull
},
922 {"QM_DFX_AXI_RDY_VLD ", 0x1040a8ull
},
923 {"QM_DFX_FF_ST0 ", 0x1040c8ull
},
924 {"QM_DFX_FF_ST1 ", 0x1040ccull
},
925 {"QM_DFX_FF_ST2 ", 0x1040d0ull
},
926 {"QM_DFX_FF_ST3 ", 0x1040d4ull
},
927 {"QM_DFX_FF_ST4 ", 0x1040d8ull
},
928 {"QM_DFX_FF_ST5 ", 0x1040dcull
},
929 {"QM_DFX_FF_ST6 ", 0x1040e0ull
},
930 {"QM_IN_IDLE_ST ", 0x1040e4ull
},
934 static struct qm_dfx_registers qm_vf_dfx_regs
[] = {
935 {"QM_DFX_FUNS_ACTIVE_ST ", 0x200ull
},
939 static int qm_regs_show(struct seq_file
*s
, void *unused
)
941 struct hisi_qm
*qm
= s
->private;
942 struct qm_dfx_registers
*regs
;
945 if (qm
->fun_type
== QM_HW_PF
)
948 regs
= qm_vf_dfx_regs
;
950 while (regs
->reg_name
) {
951 val
= readl(qm
->io_base
+ regs
->reg_offset
);
952 seq_printf(s
, "%s= 0x%08x\n", regs
->reg_name
, val
);
959 static int qm_regs_open(struct inode
*inode
, struct file
*file
)
961 return single_open(file
, qm_regs_show
, inode
->i_private
);
964 static const struct file_operations qm_regs_fops
= {
965 .owner
= THIS_MODULE
,
966 .open
= qm_regs_open
,
968 .release
= single_release
,
971 static int qm_create_debugfs_file(struct hisi_qm
*qm
, enum qm_debug_file index
)
973 struct dentry
*qm_d
= qm
->debug
.qm_d
;
974 struct debugfs_file
*file
= qm
->debug
.files
+ index
;
976 debugfs_create_file(qm_debug_file_name
[index
], 0600, qm_d
, file
,
980 mutex_init(&file
->lock
);
981 file
->debug
= &qm
->debug
;
986 static void qm_hw_error_init_v1(struct hisi_qm
*qm
, u32 ce
, u32 nfe
, u32 fe
,
989 writel(QM_ABNORMAL_INT_MASK_VALUE
, qm
->io_base
+ QM_ABNORMAL_INT_MASK
);
992 static void qm_hw_error_init_v2(struct hisi_qm
*qm
, u32 ce
, u32 nfe
, u32 fe
,
995 u32 irq_enable
= ce
| nfe
| fe
| msi
;
996 u32 irq_unmask
= ~irq_enable
;
998 qm
->error_mask
= ce
| nfe
| fe
;
1001 /* configure error type */
1002 writel(ce
, qm
->io_base
+ QM_RAS_CE_ENABLE
);
1003 writel(QM_RAS_CE_TIMES_PER_IRQ
, qm
->io_base
+ QM_RAS_CE_THRESHOLD
);
1004 writel(nfe
, qm
->io_base
+ QM_RAS_NFE_ENABLE
);
1005 writel(fe
, qm
->io_base
+ QM_RAS_FE_ENABLE
);
1007 /* use RAS irq default, so only set QM_RAS_MSI_INT_SEL for MSI */
1008 writel(msi
, qm
->io_base
+ QM_RAS_MSI_INT_SEL
);
1010 irq_unmask
&= readl(qm
->io_base
+ QM_ABNORMAL_INT_MASK
);
1011 writel(irq_unmask
, qm
->io_base
+ QM_ABNORMAL_INT_MASK
);
1014 static void qm_log_hw_error(struct hisi_qm
*qm
, u32 error_status
)
1016 const struct hisi_qm_hw_error
*err
= qm_hw_error
;
1017 struct device
*dev
= &qm
->pdev
->dev
;
1018 u32 reg_val
, type
, vf_num
;
1021 if (err
->int_msk
& error_status
) {
1022 dev_err(dev
, "%s [error status=0x%x] found\n",
1023 err
->msg
, err
->int_msk
);
1025 if (error_status
& QM_DB_TIMEOUT
) {
1026 reg_val
= readl(qm
->io_base
+
1028 type
= (reg_val
& QM_DB_TIMEOUT_TYPE
) >>
1029 QM_DB_TIMEOUT_TYPE_SHIFT
;
1030 vf_num
= reg_val
& QM_DB_TIMEOUT_VF
;
1031 dev_err(dev
, "qm %s doorbell timeout in function %u\n",
1032 qm_db_timeout
[type
], vf_num
);
1035 if (error_status
& QM_OF_FIFO_OF
) {
1036 reg_val
= readl(qm
->io_base
+
1038 type
= (reg_val
& QM_FIFO_OVERFLOW_TYPE
) >>
1039 QM_FIFO_OVERFLOW_TYPE_SHIFT
;
1040 vf_num
= reg_val
& QM_FIFO_OVERFLOW_VF
;
1042 if (type
< ARRAY_SIZE(qm_fifo_overflow
))
1043 dev_err(dev
, "qm %s fifo overflow in function %u\n",
1044 qm_fifo_overflow
[type
],
1047 dev_err(dev
, "unknown error type\n");
1054 static pci_ers_result_t
qm_hw_error_handle_v2(struct hisi_qm
*qm
)
1056 u32 error_status
, tmp
;
1059 tmp
= readl(qm
->io_base
+ QM_ABNORMAL_INT_STATUS
);
1060 error_status
= qm
->error_mask
& tmp
;
1063 qm_log_hw_error(qm
, error_status
);
1066 writel(error_status
, qm
->io_base
+ QM_ABNORMAL_INT_SOURCE
);
1068 return PCI_ERS_RESULT_NEED_RESET
;
1071 return PCI_ERS_RESULT_RECOVERED
;
1074 static const struct hisi_qm_hw_ops qm_hw_ops_v1
= {
1076 .get_irq_num
= qm_get_irq_num_v1
,
1077 .hw_error_init
= qm_hw_error_init_v1
,
1080 static const struct hisi_qm_hw_ops qm_hw_ops_v2
= {
1081 .get_vft
= qm_get_vft_v2
,
1083 .get_irq_num
= qm_get_irq_num_v2
,
1084 .hw_error_init
= qm_hw_error_init_v2
,
1085 .hw_error_handle
= qm_hw_error_handle_v2
,
1088 static void *qm_get_avail_sqe(struct hisi_qp
*qp
)
1090 struct hisi_qp_status
*qp_status
= &qp
->qp_status
;
1091 u16 sq_tail
= qp_status
->sq_tail
;
1093 if (unlikely(atomic_read(&qp
->qp_status
.used
) == QM_Q_DEPTH
))
1096 return qp
->sqe
+ sq_tail
* qp
->qm
->sqe_size
;
1100 * hisi_qm_create_qp() - Create a queue pair from qm.
1101 * @qm: The qm we create a qp from.
1102 * @alg_type: Accelerator specific algorithm type in sqc.
1104 * return created qp, -EBUSY if all qps in qm allocated, -ENOMEM if allocating
1107 struct hisi_qp
*hisi_qm_create_qp(struct hisi_qm
*qm
, u8 alg_type
)
1109 struct device
*dev
= &qm
->pdev
->dev
;
1113 qp
= kzalloc(sizeof(*qp
), GFP_KERNEL
);
1115 return ERR_PTR(-ENOMEM
);
1117 write_lock(&qm
->qps_lock
);
1119 qp_id
= find_first_zero_bit(qm
->qp_bitmap
, qm
->qp_num
);
1120 if (qp_id
>= qm
->qp_num
) {
1121 write_unlock(&qm
->qps_lock
);
1122 dev_info(&qm
->pdev
->dev
, "QM all queues are busy!\n");
1126 set_bit(qp_id
, qm
->qp_bitmap
);
1127 qm
->qp_array
[qp_id
] = qp
;
1130 write_unlock(&qm
->qps_lock
);
1134 if (qm
->use_dma_api
) {
1135 qp
->qdma
.size
= qm
->sqe_size
* QM_Q_DEPTH
+
1136 sizeof(struct qm_cqe
) * QM_Q_DEPTH
;
1137 qp
->qdma
.va
= dma_alloc_coherent(dev
, qp
->qdma
.size
,
1138 &qp
->qdma
.dma
, GFP_KERNEL
);
1144 dev_dbg(dev
, "allocate qp dma buf(va=%pK, dma=%pad, size=%zx)\n",
1145 qp
->qdma
.va
, &qp
->qdma
.dma
, qp
->qdma
.size
);
1149 qp
->alg_type
= alg_type
;
1150 INIT_WORK(&qp
->work
, qm_qp_work_func
);
1151 qp
->wq
= alloc_workqueue("hisi_qm", WQ_UNBOUND
| WQ_HIGHPRI
|
1152 WQ_CPU_INTENSIVE
| WQ_MEM_RECLAIM
, 0);
1155 goto err_free_qp_mem
;
1161 if (qm
->use_dma_api
)
1162 dma_free_coherent(dev
, qp
->qdma
.size
, qp
->qdma
.va
,
1165 write_lock(&qm
->qps_lock
);
1166 qm
->qp_array
[qp_id
] = NULL
;
1167 clear_bit(qp_id
, qm
->qp_bitmap
);
1168 write_unlock(&qm
->qps_lock
);
1171 return ERR_PTR(ret
);
1173 EXPORT_SYMBOL_GPL(hisi_qm_create_qp
);
1176 * hisi_qm_release_qp() - Release a qp back to its qm.
1177 * @qp: The qp we want to release.
1179 * This function releases the resource of a qp.
1181 void hisi_qm_release_qp(struct hisi_qp
*qp
)
1183 struct hisi_qm
*qm
= qp
->qm
;
1184 struct qm_dma
*qdma
= &qp
->qdma
;
1185 struct device
*dev
= &qm
->pdev
->dev
;
1187 if (qm
->use_dma_api
&& qdma
->va
)
1188 dma_free_coherent(dev
, qdma
->size
, qdma
->va
, qdma
->dma
);
1190 write_lock(&qm
->qps_lock
);
1191 qm
->qp_array
[qp
->qp_id
] = NULL
;
1192 clear_bit(qp
->qp_id
, qm
->qp_bitmap
);
1194 write_unlock(&qm
->qps_lock
);
1198 EXPORT_SYMBOL_GPL(hisi_qm_release_qp
);
1200 static int qm_qp_ctx_cfg(struct hisi_qp
*qp
, int qp_id
, int pasid
)
1202 struct hisi_qm
*qm
= qp
->qm
;
1203 struct device
*dev
= &qm
->pdev
->dev
;
1204 enum qm_hw_ver ver
= qm
->ver
;
1211 qm_init_qp_status(qp
);
1213 sqc
= kzalloc(sizeof(struct qm_sqc
), GFP_KERNEL
);
1216 sqc_dma
= dma_map_single(dev
, sqc
, sizeof(struct qm_sqc
),
1218 if (dma_mapping_error(dev
, sqc_dma
)) {
1223 INIT_QC_COMMON(sqc
, qp
->sqe_dma
, pasid
);
1224 if (ver
== QM_HW_V1
) {
1225 sqc
->dw3
= cpu_to_le32(QM_MK_SQC_DW3_V1(0, 0, 0, qm
->sqe_size
));
1226 sqc
->w8
= cpu_to_le16(QM_Q_DEPTH
- 1);
1227 } else if (ver
== QM_HW_V2
) {
1228 sqc
->dw3
= cpu_to_le32(QM_MK_SQC_DW3_V2(qm
->sqe_size
));
1229 sqc
->w8
= 0; /* rand_qc */
1231 sqc
->cq_num
= cpu_to_le16(qp_id
);
1232 sqc
->w13
= cpu_to_le16(QM_MK_SQC_W13(0, 1, qp
->alg_type
));
1234 ret
= qm_mb(qm
, QM_MB_CMD_SQC
, sqc_dma
, qp_id
, 0);
1235 dma_unmap_single(dev
, sqc_dma
, sizeof(struct qm_sqc
), DMA_TO_DEVICE
);
1240 cqc
= kzalloc(sizeof(struct qm_cqc
), GFP_KERNEL
);
1243 cqc_dma
= dma_map_single(dev
, cqc
, sizeof(struct qm_cqc
),
1245 if (dma_mapping_error(dev
, cqc_dma
)) {
1250 INIT_QC_COMMON(cqc
, qp
->cqe_dma
, pasid
);
1251 if (ver
== QM_HW_V1
) {
1252 cqc
->dw3
= cpu_to_le32(QM_MK_CQC_DW3_V1(0, 0, 0, 4));
1253 cqc
->w8
= cpu_to_le16(QM_Q_DEPTH
- 1);
1254 } else if (ver
== QM_HW_V2
) {
1255 cqc
->dw3
= cpu_to_le32(QM_MK_CQC_DW3_V2(4));
1258 cqc
->dw6
= cpu_to_le32(1 << QM_CQ_PHASE_SHIFT
| 1 << QM_CQ_FLAG_SHIFT
);
1260 ret
= qm_mb(qm
, QM_MB_CMD_CQC
, cqc_dma
, qp_id
, 0);
1261 dma_unmap_single(dev
, cqc_dma
, sizeof(struct qm_cqc
), DMA_TO_DEVICE
);
1268 * hisi_qm_start_qp() - Start a qp into running.
1269 * @qp: The qp we want to start to run.
1270 * @arg: Accelerator specific argument.
1272 * After this function, qp can receive request from user. Return qp_id if
1273 * successful, Return -EBUSY if failed.
1275 int hisi_qm_start_qp(struct hisi_qp
*qp
, unsigned long arg
)
1277 struct hisi_qm
*qm
= qp
->qm
;
1278 struct device
*dev
= &qm
->pdev
->dev
;
1279 enum qm_hw_ver ver
= qm
->ver
;
1280 int qp_id
= qp
->qp_id
;
1285 #define QP_INIT_BUF(qp, type, size) do { \
1286 (qp)->type = ((qp)->qdma.va + (off)); \
1287 (qp)->type##_dma = (qp)->qdma.dma + (off); \
1291 if (!qp
->qdma
.dma
) {
1292 dev_err(dev
, "cannot get qm dma buffer\n");
1296 /* sq need 128 bytes alignment */
1297 if (qp
->qdma
.dma
& QM_SQE_DATA_ALIGN_MASK
) {
1298 dev_err(dev
, "qm sq is not aligned to 128 byte\n");
1302 QP_INIT_BUF(qp
, sqe
, qm
->sqe_size
* QM_Q_DEPTH
);
1303 QP_INIT_BUF(qp
, cqe
, sizeof(struct qm_cqe
) * QM_Q_DEPTH
);
1305 dev_dbg(dev
, "init qp buffer(v%d):\n"
1307 " cqe (%pK, %lx)\n",
1308 ver
, qp
->sqe
, (unsigned long)qp
->sqe_dma
,
1309 qp
->cqe
, (unsigned long)qp
->cqe_dma
);
1311 ret
= qm_qp_ctx_cfg(qp
, qp_id
, pasid
);
1315 dev_dbg(dev
, "queue %d started\n", qp_id
);
1319 EXPORT_SYMBOL_GPL(hisi_qm_start_qp
);
1322 * hisi_qm_stop_qp() - Stop a qp in qm.
1323 * @qp: The qp we want to stop.
1325 * This function is reverse of hisi_qm_start_qp. Return 0 if successful.
1327 int hisi_qm_stop_qp(struct hisi_qp
*qp
)
1329 struct device
*dev
= &qp
->qm
->pdev
->dev
;
1333 if (test_bit(QP_STOP
, &qp
->qp_status
.flags
))
1336 while (atomic_read(&qp
->qp_status
.used
)) {
1340 dev_err(dev
, "Cannot drain out data for stopping, Force to stop!\n");
1345 set_bit(QP_STOP
, &qp
->qp_status
.flags
);
1347 dev_dbg(dev
, "stop queue %u!", qp
->qp_id
);
1351 EXPORT_SYMBOL_GPL(hisi_qm_stop_qp
);
1354 * hisi_qp_send() - Queue up a task in the hardware queue.
1355 * @qp: The qp in which to put the message.
1356 * @msg: The message.
1358 * This function will return -EBUSY if qp is currently full, and -EAGAIN
1359 * if qp related qm is resetting.
1361 int hisi_qp_send(struct hisi_qp
*qp
, const void *msg
)
1363 struct hisi_qp_status
*qp_status
= &qp
->qp_status
;
1364 u16 sq_tail
= qp_status
->sq_tail
;
1365 u16 sq_tail_next
= (sq_tail
+ 1) % QM_Q_DEPTH
;
1366 void *sqe
= qm_get_avail_sqe(qp
);
1368 if (unlikely(test_bit(QP_STOP
, &qp
->qp_status
.flags
))) {
1369 dev_info(&qp
->qm
->pdev
->dev
, "QP is stopped or resetting\n");
1376 memcpy(sqe
, msg
, qp
->qm
->sqe_size
);
1378 qm_db(qp
->qm
, qp
->qp_id
, QM_DOORBELL_CMD_SQ
, sq_tail_next
, 0);
1379 atomic_inc(&qp
->qp_status
.used
);
1380 qp_status
->sq_tail
= sq_tail_next
;
1384 EXPORT_SYMBOL_GPL(hisi_qp_send
);
1386 static void hisi_qm_cache_wb(struct hisi_qm
*qm
)
1390 if (qm
->ver
== QM_HW_V2
) {
1391 writel(0x1, qm
->io_base
+ QM_CACHE_WB_START
);
1392 if (readl_relaxed_poll_timeout(qm
->io_base
+ QM_CACHE_WB_DONE
,
1393 val
, val
& BIT(0), 10, 1000))
1394 dev_err(&qm
->pdev
->dev
, "QM writeback sqc cache fail!\n");
1399 * hisi_qm_get_free_qp_num() - Get free number of qp in qm.
1400 * @qm: The qm which want to get free qp.
1402 * This function return free number of qp in qm.
1404 int hisi_qm_get_free_qp_num(struct hisi_qm
*qm
)
1408 read_lock(&qm
->qps_lock
);
1409 ret
= qm
->qp_num
- qm
->qp_in_used
;
1410 read_unlock(&qm
->qps_lock
);
1414 EXPORT_SYMBOL_GPL(hisi_qm_get_free_qp_num
);
1417 * hisi_qm_init() - Initialize configures about qm.
1418 * @qm: The qm needing init.
1420 * This function init qm, then we can call hisi_qm_start to put qm into work.
1422 int hisi_qm_init(struct hisi_qm
*qm
)
1424 struct pci_dev
*pdev
= qm
->pdev
;
1425 struct device
*dev
= &pdev
->dev
;
1426 unsigned int num_vec
;
1431 qm
->ops
= &qm_hw_ops_v1
;
1434 qm
->ops
= &qm_hw_ops_v2
;
1440 ret
= pci_enable_device_mem(pdev
);
1442 dev_err(&pdev
->dev
, "Failed to enable device mem!\n");
1446 ret
= pci_request_mem_regions(pdev
, qm
->dev_name
);
1448 dev_err(&pdev
->dev
, "Failed to request mem regions!\n");
1449 goto err_disable_pcidev
;
1452 qm
->io_base
= ioremap(pci_resource_start(pdev
, PCI_BAR_2
),
1453 pci_resource_len(qm
->pdev
, PCI_BAR_2
));
1456 goto err_release_mem_regions
;
1459 ret
= dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(64));
1462 pci_set_master(pdev
);
1464 if (!qm
->ops
->get_irq_num
) {
1468 num_vec
= qm
->ops
->get_irq_num(qm
);
1469 ret
= pci_alloc_irq_vectors(pdev
, num_vec
, num_vec
, PCI_IRQ_MSI
);
1471 dev_err(dev
, "Failed to enable MSI vectors!\n");
1475 ret
= qm_irq_register(qm
);
1477 goto err_free_irq_vectors
;
1480 mutex_init(&qm
->mailbox_lock
);
1481 rwlock_init(&qm
->qps_lock
);
1483 dev_dbg(dev
, "init qm %s with %s\n", pdev
->is_physfn
? "pf" : "vf",
1484 qm
->use_dma_api
? "dma api" : "iommu api");
1488 err_free_irq_vectors
:
1489 pci_free_irq_vectors(pdev
);
1491 iounmap(qm
->io_base
);
1492 err_release_mem_regions
:
1493 pci_release_mem_regions(pdev
);
1495 pci_disable_device(pdev
);
1499 EXPORT_SYMBOL_GPL(hisi_qm_init
);
1502 * hisi_qm_uninit() - Uninitialize qm.
1503 * @qm: The qm needed uninit.
1505 * This function uninits qm related device resources.
1507 void hisi_qm_uninit(struct hisi_qm
*qm
)
1509 struct pci_dev
*pdev
= qm
->pdev
;
1510 struct device
*dev
= &pdev
->dev
;
1512 if (qm
->use_dma_api
&& qm
->qdma
.va
) {
1513 hisi_qm_cache_wb(qm
);
1514 dma_free_coherent(dev
, qm
->qdma
.size
,
1515 qm
->qdma
.va
, qm
->qdma
.dma
);
1516 memset(&qm
->qdma
, 0, sizeof(qm
->qdma
));
1519 qm_irq_unregister(qm
);
1520 pci_free_irq_vectors(pdev
);
1521 iounmap(qm
->io_base
);
1522 pci_release_mem_regions(pdev
);
1523 pci_disable_device(pdev
);
1525 EXPORT_SYMBOL_GPL(hisi_qm_uninit
);
1528 * hisi_qm_get_vft() - Get vft from a qm.
1529 * @qm: The qm we want to get its vft.
1530 * @base: The base number of queue in vft.
1531 * @number: The number of queues in vft.
1533 * We can allocate multiple queues to a qm by configuring virtual function
1534 * table. We get related configures by this function. Normally, we call this
1535 * function in VF driver to get the queue information.
1537 * qm hw v1 does not support this interface.
1539 int hisi_qm_get_vft(struct hisi_qm
*qm
, u32
*base
, u32
*number
)
1541 if (!base
|| !number
)
1544 if (!qm
->ops
->get_vft
) {
1545 dev_err(&qm
->pdev
->dev
, "Don't support vft read!\n");
1549 return qm
->ops
->get_vft(qm
, base
, number
);
1551 EXPORT_SYMBOL_GPL(hisi_qm_get_vft
);
1554 * hisi_qm_set_vft() - Set "virtual function table" for a qm.
1555 * @fun_num: Number of operated function.
1556 * @qm: The qm in which to set vft, alway in a PF.
1557 * @base: The base number of queue in vft.
1558 * @number: The number of queues in vft. 0 means invalid vft.
1560 * This function is alway called in PF driver, it is used to assign queues
1563 * Assign queues A~B to PF: hisi_qm_set_vft(qm, 0, A, B - A + 1)
1564 * Assign queues A~B to VF: hisi_qm_set_vft(qm, 2, A, B - A + 1)
1565 * (VF function number 0x2)
1567 int hisi_qm_set_vft(struct hisi_qm
*qm
, u32 fun_num
, u32 base
,
1570 u32 max_q_num
= qm
->ctrl_qp_num
;
1572 if (base
>= max_q_num
|| number
> max_q_num
||
1573 (base
+ number
) > max_q_num
)
1576 return qm_set_sqc_cqc_vft(qm
, fun_num
, base
, number
);
1578 EXPORT_SYMBOL_GPL(hisi_qm_set_vft
);
1580 static void qm_init_eq_aeq_status(struct hisi_qm
*qm
)
1582 struct hisi_qm_status
*status
= &qm
->status
;
1584 status
->eq_head
= 0;
1585 status
->aeq_head
= 0;
1586 status
->eqc_phase
= true;
1587 status
->aeqc_phase
= true;
1590 static int qm_eq_ctx_cfg(struct hisi_qm
*qm
)
1592 struct device
*dev
= &qm
->pdev
->dev
;
1594 struct qm_aeqc
*aeqc
;
1596 dma_addr_t aeqc_dma
;
1599 qm_init_eq_aeq_status(qm
);
1601 eqc
= kzalloc(sizeof(struct qm_eqc
), GFP_KERNEL
);
1604 eqc_dma
= dma_map_single(dev
, eqc
, sizeof(struct qm_eqc
),
1606 if (dma_mapping_error(dev
, eqc_dma
)) {
1611 eqc
->base_l
= cpu_to_le32(lower_32_bits(qm
->eqe_dma
));
1612 eqc
->base_h
= cpu_to_le32(upper_32_bits(qm
->eqe_dma
));
1613 if (qm
->ver
== QM_HW_V1
)
1614 eqc
->dw3
= cpu_to_le32(QM_EQE_AEQE_SIZE
);
1615 eqc
->dw6
= cpu_to_le32((QM_Q_DEPTH
- 1) | (1 << QM_EQC_PHASE_SHIFT
));
1616 ret
= qm_mb(qm
, QM_MB_CMD_EQC
, eqc_dma
, 0, 0);
1617 dma_unmap_single(dev
, eqc_dma
, sizeof(struct qm_eqc
), DMA_TO_DEVICE
);
1622 aeqc
= kzalloc(sizeof(struct qm_aeqc
), GFP_KERNEL
);
1625 aeqc_dma
= dma_map_single(dev
, aeqc
, sizeof(struct qm_aeqc
),
1627 if (dma_mapping_error(dev
, aeqc_dma
)) {
1632 aeqc
->base_l
= cpu_to_le32(lower_32_bits(qm
->aeqe_dma
));
1633 aeqc
->base_h
= cpu_to_le32(upper_32_bits(qm
->aeqe_dma
));
1634 aeqc
->dw6
= cpu_to_le32((QM_Q_DEPTH
- 1) | (1 << QM_EQC_PHASE_SHIFT
));
1636 ret
= qm_mb(qm
, QM_MB_CMD_AEQC
, aeqc_dma
, 0, 0);
1637 dma_unmap_single(dev
, aeqc_dma
, sizeof(struct qm_aeqc
), DMA_TO_DEVICE
);
1643 static int __hisi_qm_start(struct hisi_qm
*qm
)
1645 struct pci_dev
*pdev
= qm
->pdev
;
1646 struct device
*dev
= &pdev
->dev
;
1650 #define QM_INIT_BUF(qm, type, num) do { \
1651 (qm)->type = ((qm)->qdma.va + (off)); \
1652 (qm)->type##_dma = (qm)->qdma.dma + (off); \
1653 off += QMC_ALIGN(sizeof(struct qm_##type) * (num)); \
1656 WARN_ON(!qm
->qdma
.dma
);
1658 if (qm
->qp_num
== 0)
1661 if (qm
->fun_type
== QM_HW_PF
) {
1662 ret
= qm_dev_mem_reset(qm
);
1666 ret
= hisi_qm_set_vft(qm
, 0, qm
->qp_base
, qm
->qp_num
);
1671 QM_INIT_BUF(qm
, eqe
, QM_Q_DEPTH
);
1672 QM_INIT_BUF(qm
, aeqe
, QM_Q_DEPTH
);
1673 QM_INIT_BUF(qm
, sqc
, qm
->qp_num
);
1674 QM_INIT_BUF(qm
, cqc
, qm
->qp_num
);
1676 dev_dbg(dev
, "init qm buffer:\n"
1678 " aeqe (%pK, %lx)\n"
1680 " cqc (%pK, %lx)\n",
1681 qm
->eqe
, (unsigned long)qm
->eqe_dma
,
1682 qm
->aeqe
, (unsigned long)qm
->aeqe_dma
,
1683 qm
->sqc
, (unsigned long)qm
->sqc_dma
,
1684 qm
->cqc
, (unsigned long)qm
->cqc_dma
);
1686 ret
= qm_eq_ctx_cfg(qm
);
1690 ret
= qm_mb(qm
, QM_MB_CMD_SQC_BT
, qm
->sqc_dma
, 0, 0);
1694 ret
= qm_mb(qm
, QM_MB_CMD_CQC_BT
, qm
->cqc_dma
, 0, 0);
1698 writel(0x0, qm
->io_base
+ QM_VF_EQ_INT_MASK
);
1699 writel(0x0, qm
->io_base
+ QM_VF_AEQ_INT_MASK
);
1705 * hisi_qm_start() - start qm
1706 * @qm: The qm to be started.
1708 * This function starts a qm, then we can allocate qp from this qm.
1710 int hisi_qm_start(struct hisi_qm
*qm
)
1712 struct device
*dev
= &qm
->pdev
->dev
;
1714 dev_dbg(dev
, "qm start with %d queue pairs\n", qm
->qp_num
);
1717 dev_err(dev
, "qp_num should not be 0\n");
1721 if (!qm
->qp_bitmap
) {
1722 qm
->qp_bitmap
= devm_kcalloc(dev
, BITS_TO_LONGS(qm
->qp_num
),
1723 sizeof(long), GFP_KERNEL
);
1724 qm
->qp_array
= devm_kcalloc(dev
, qm
->qp_num
,
1725 sizeof(struct hisi_qp
*),
1727 if (!qm
->qp_bitmap
|| !qm
->qp_array
)
1731 if (!qm
->use_dma_api
) {
1732 dev_dbg(&qm
->pdev
->dev
, "qm delay start\n");
1734 } else if (!qm
->qdma
.va
) {
1735 qm
->qdma
.size
= QMC_ALIGN(sizeof(struct qm_eqe
) * QM_Q_DEPTH
) +
1736 QMC_ALIGN(sizeof(struct qm_aeqe
) * QM_Q_DEPTH
) +
1737 QMC_ALIGN(sizeof(struct qm_sqc
) * qm
->qp_num
) +
1738 QMC_ALIGN(sizeof(struct qm_cqc
) * qm
->qp_num
);
1739 qm
->qdma
.va
= dma_alloc_coherent(dev
, qm
->qdma
.size
,
1740 &qm
->qdma
.dma
, GFP_KERNEL
);
1741 dev_dbg(dev
, "allocate qm dma buf(va=%pK, dma=%pad, size=%zx)\n",
1742 qm
->qdma
.va
, &qm
->qdma
.dma
, qm
->qdma
.size
);
1747 return __hisi_qm_start(qm
);
1749 EXPORT_SYMBOL_GPL(hisi_qm_start
);
1752 * hisi_qm_stop() - Stop a qm.
1753 * @qm: The qm which will be stopped.
1755 * This function stops qm and its qps, then qm can not accept request.
1756 * Related resources are not released at this state, we can use hisi_qm_start
1757 * to let qm start again.
1759 int hisi_qm_stop(struct hisi_qm
*qm
)
1765 if (!qm
|| !qm
->pdev
) {
1770 dev
= &qm
->pdev
->dev
;
1772 /* Mask eq and aeq irq */
1773 writel(0x1, qm
->io_base
+ QM_VF_EQ_INT_MASK
);
1774 writel(0x1, qm
->io_base
+ QM_VF_AEQ_INT_MASK
);
1776 /* Stop all qps belong to this qm */
1777 for (i
= 0; i
< qm
->qp_num
; i
++) {
1778 qp
= qm
->qp_array
[i
];
1780 ret
= hisi_qm_stop_qp(qp
);
1782 dev_err(dev
, "Failed to stop qp%d!\n", i
);
1788 if (qm
->fun_type
== QM_HW_PF
) {
1789 ret
= hisi_qm_set_vft(qm
, 0, 0, 0);
1791 dev_err(dev
, "Failed to set vft!\n");
1796 EXPORT_SYMBOL_GPL(hisi_qm_stop
);
1799 * hisi_qm_debug_init() - Initialize qm related debugfs files.
1800 * @qm: The qm for which we want to add debugfs files.
1802 * Create qm related debugfs files.
1804 int hisi_qm_debug_init(struct hisi_qm
*qm
)
1806 struct dentry
*qm_d
;
1809 qm_d
= debugfs_create_dir("qm", qm
->debug
.debug_root
);
1810 qm
->debug
.qm_d
= qm_d
;
1812 /* only show this in PF */
1813 if (qm
->fun_type
== QM_HW_PF
)
1814 for (i
= CURRENT_Q
; i
< DEBUG_FILE_NUM
; i
++)
1815 if (qm_create_debugfs_file(qm
, i
)) {
1817 goto failed_to_create
;
1820 debugfs_create_file("qm_regs", 0444, qm
->debug
.qm_d
, qm
, &qm_regs_fops
);
1825 debugfs_remove_recursive(qm_d
);
1828 EXPORT_SYMBOL_GPL(hisi_qm_debug_init
);
1831 * hisi_qm_debug_regs_clear() - clear qm debug related registers.
1832 * @qm: The qm for which we want to clear its debug registers.
1834 void hisi_qm_debug_regs_clear(struct hisi_qm
*qm
)
1836 struct qm_dfx_registers
*regs
;
1839 /* clear current_q */
1840 writel(0x0, qm
->io_base
+ QM_DFX_SQE_CNT_VF_SQN
);
1841 writel(0x0, qm
->io_base
+ QM_DFX_CQE_CNT_VF_CQN
);
1844 * these registers are reading and clearing, so clear them after
1847 writel(0x1, qm
->io_base
+ QM_DFX_CNT_CLR_CE
);
1850 for (i
= 0; i
< CNT_CYC_REGS_NUM
; i
++) {
1851 readl(qm
->io_base
+ regs
->reg_offset
);
1855 writel(0x0, qm
->io_base
+ QM_DFX_CNT_CLR_CE
);
1857 EXPORT_SYMBOL_GPL(hisi_qm_debug_regs_clear
);
1860 * hisi_qm_hw_error_init() - Configure qm hardware error report method.
1861 * @qm: The qm which we want to configure.
1862 * @ce: Bit mask of correctable error configure.
1863 * @nfe: Bit mask of non-fatal error configure.
1864 * @fe: Bit mask of fatal error configure.
1865 * @msi: Bit mask of error reported by message signal interrupt.
1867 * Hardware errors of qm can be reported either by RAS interrupts which will
1868 * be handled by UEFI and then PCIe AER or by device MSI. User can configure
1869 * each error to use either of above two methods. For RAS interrupts, we can
1870 * configure an error as one of correctable error, non-fatal error or
1873 * Bits indicating errors can be configured to ce, nfe, fe and msi to enable
1874 * related report methods. Error report will be masked if related error bit
1875 * does not configure.
1877 void hisi_qm_hw_error_init(struct hisi_qm
*qm
, u32 ce
, u32 nfe
, u32 fe
,
1880 if (!qm
->ops
->hw_error_init
) {
1881 dev_err(&qm
->pdev
->dev
, "QM doesn't support hw error handling!\n");
1885 qm
->ops
->hw_error_init(qm
, ce
, nfe
, fe
, msi
);
1887 EXPORT_SYMBOL_GPL(hisi_qm_hw_error_init
);
1890 * hisi_qm_hw_error_handle() - Handle qm non-fatal hardware errors.
1891 * @qm: The qm which has non-fatal hardware errors.
1893 * Accelerators use this function to handle qm non-fatal hardware errors.
1895 pci_ers_result_t
hisi_qm_hw_error_handle(struct hisi_qm
*qm
)
1897 if (!qm
->ops
->hw_error_handle
) {
1898 dev_err(&qm
->pdev
->dev
, "QM doesn't support hw error report!\n");
1899 return PCI_ERS_RESULT_NONE
;
1902 return qm
->ops
->hw_error_handle(qm
);
1904 EXPORT_SYMBOL_GPL(hisi_qm_hw_error_handle
);
1907 * hisi_qm_get_hw_version() - Get hardware version of a qm.
1908 * @pdev: The device which hardware version we want to get.
1910 * This function gets the hardware version of a qm. Return QM_HW_UNKNOWN
1911 * if the hardware version is not supported.
1913 enum qm_hw_ver
hisi_qm_get_hw_version(struct pci_dev
*pdev
)
1915 switch (pdev
->revision
) {
1918 return pdev
->revision
;
1920 return QM_HW_UNKNOWN
;
1923 EXPORT_SYMBOL_GPL(hisi_qm_get_hw_version
);
1925 MODULE_LICENSE("GPL v2");
1926 MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>");
1927 MODULE_DESCRIPTION("HiSilicon Accelerator queue manager driver");