1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 HiSilicon Limited. */
4 #include <linux/acpi.h>
6 #include <linux/bitops.h>
7 #include <linux/debugfs.h>
8 #include <linux/init.h>
10 #include <linux/iommu.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/seq_file.h>
15 #include <linux/topology.h>
20 #define SEC_QUEUE_NUM_V1 4096
21 #define SEC_QUEUE_NUM_V2 1024
22 #define SEC_PF_PCI_DEVICE_ID 0xa255
23 #define SEC_VF_PCI_DEVICE_ID 0xa256
25 #define SEC_XTS_MIV_ENABLE_REG 0x301384
26 #define SEC_XTS_MIV_ENABLE_MSK 0x7FFFFFFF
27 #define SEC_XTS_MIV_DISABLE_MSK 0xFFFFFFFF
28 #define SEC_BD_ERR_CHK_EN1 0xfffff7fd
29 #define SEC_BD_ERR_CHK_EN2 0xffffbfff
31 #define SEC_SQE_SIZE 128
32 #define SEC_SQ_SIZE (SEC_SQE_SIZE * QM_Q_DEPTH)
33 #define SEC_PF_DEF_Q_NUM 64
34 #define SEC_PF_DEF_Q_BASE 0
35 #define SEC_CTX_Q_NUM_DEF 24
36 #define SEC_CTX_Q_NUM_MAX 32
38 #define SEC_CTRL_CNT_CLR_CE 0x301120
39 #define SEC_CTRL_CNT_CLR_CE_BIT BIT(0)
40 #define SEC_ENGINE_PF_CFG_OFF 0x300000
41 #define SEC_ACC_COMMON_REG_OFF 0x1000
42 #define SEC_CORE_INT_SOURCE 0x301010
43 #define SEC_CORE_INT_MASK 0x301000
44 #define SEC_CORE_INT_STATUS 0x301008
45 #define SEC_CORE_SRAM_ECC_ERR_INFO 0x301C14
46 #define SEC_ECC_NUM(err) (((err) >> 16) & 0xFF)
47 #define SEC_ECC_ADDR(err) ((err) >> 0)
48 #define SEC_CORE_INT_DISABLE 0x0
49 #define SEC_CORE_INT_ENABLE 0x1ff
51 #define SEC_RAS_CE_REG 0x50
52 #define SEC_RAS_FE_REG 0x54
53 #define SEC_RAS_NFE_REG 0x58
54 #define SEC_RAS_CE_ENB_MSK 0x88
55 #define SEC_RAS_FE_ENB_MSK 0x0
56 #define SEC_RAS_NFE_ENB_MSK 0x177
57 #define SEC_RAS_DISABLE 0x0
58 #define SEC_MEM_START_INIT_REG 0x0100
59 #define SEC_MEM_INIT_DONE_REG 0x0104
60 #define SEC_QM_ABNORMAL_INT_MASK 0x100004
62 #define SEC_CONTROL_REG 0x0200
63 #define SEC_TRNG_EN_SHIFT 8
64 #define SEC_CLK_GATE_ENABLE BIT(3)
65 #define SEC_CLK_GATE_DISABLE (~BIT(3))
66 #define SEC_AXI_SHUTDOWN_ENABLE BIT(12)
67 #define SEC_AXI_SHUTDOWN_DISABLE 0xFFFFEFFF
69 #define SEC_INTERFACE_USER_CTRL0_REG 0x0220
70 #define SEC_INTERFACE_USER_CTRL1_REG 0x0224
71 #define SEC_BD_ERR_CHK_EN_REG1 0x0384
72 #define SEC_BD_ERR_CHK_EN_REG2 0x038c
74 #define SEC_USER0_SMMU_NORMAL (BIT(23) | BIT(15))
75 #define SEC_USER1_SMMU_NORMAL (BIT(31) | BIT(23) | BIT(15) | BIT(7))
76 #define SEC_CORE_INT_STATUS_M_ECC BIT(2)
78 #define SEC_DELAY_10_US 10
79 #define SEC_POLL_TIMEOUT_US 1000
80 #define SEC_VF_CNT_MASK 0xffffffc0
81 #define SEC_DBGFS_VAL_MAX_LEN 20
83 #define SEC_ADDR(qm, offset) ((qm)->io_base + (offset) + \
84 SEC_ENGINE_PF_CFG_OFF + SEC_ACC_COMMON_REG_OFF)
91 static const char sec_name
[] = "hisi_sec2";
92 static struct dentry
*sec_debugfs_root
;
93 static struct hisi_qm_list sec_devices
;
95 static const struct sec_hw_error sec_hw_errors
[] = {
96 {.int_msk
= BIT(0), .msg
= "sec_axi_rresp_err_rint"},
97 {.int_msk
= BIT(1), .msg
= "sec_axi_bresp_err_rint"},
98 {.int_msk
= BIT(2), .msg
= "sec_ecc_2bit_err_rint"},
99 {.int_msk
= BIT(3), .msg
= "sec_ecc_1bit_err_rint"},
100 {.int_msk
= BIT(4), .msg
= "sec_req_trng_timeout_rint"},
101 {.int_msk
= BIT(5), .msg
= "sec_fsm_hbeat_rint"},
102 {.int_msk
= BIT(6), .msg
= "sec_channel_req_rng_timeout_rint"},
103 {.int_msk
= BIT(7), .msg
= "sec_bd_err_rint"},
104 {.int_msk
= BIT(8), .msg
= "sec_chain_buff_err_rint"},
108 static const char * const sec_dbg_file_name
[] = {
109 [SEC_CURRENT_QM
] = "current_qm",
110 [SEC_CLEAR_ENABLE
] = "clear_enable",
113 static struct debugfs_reg32 sec_dfx_regs
[] = {
114 {"SEC_PF_ABNORMAL_INT_SOURCE ", 0x301010},
115 {"SEC_SAA_EN ", 0x301270},
116 {"SEC_BD_LATENCY_MIN ", 0x301600},
117 {"SEC_BD_LATENCY_MAX ", 0x301608},
118 {"SEC_BD_LATENCY_AVG ", 0x30160C},
119 {"SEC_BD_NUM_IN_SAA0 ", 0x301670},
120 {"SEC_BD_NUM_IN_SAA1 ", 0x301674},
121 {"SEC_BD_NUM_IN_SEC ", 0x301680},
122 {"SEC_ECC_1BIT_CNT ", 0x301C00},
123 {"SEC_ECC_1BIT_INFO ", 0x301C04},
124 {"SEC_ECC_2BIT_CNT ", 0x301C10},
125 {"SEC_ECC_2BIT_INFO ", 0x301C14},
126 {"SEC_BD_SAA0 ", 0x301C20},
127 {"SEC_BD_SAA1 ", 0x301C24},
128 {"SEC_BD_SAA2 ", 0x301C28},
129 {"SEC_BD_SAA3 ", 0x301C2C},
130 {"SEC_BD_SAA4 ", 0x301C30},
131 {"SEC_BD_SAA5 ", 0x301C34},
132 {"SEC_BD_SAA6 ", 0x301C38},
133 {"SEC_BD_SAA7 ", 0x301C3C},
134 {"SEC_BD_SAA8 ", 0x301C40},
137 static int sec_pf_q_num_set(const char *val
, const struct kernel_param
*kp
)
139 struct pci_dev
*pdev
;
147 pdev
= pci_get_device(PCI_VENDOR_ID_HUAWEI
,
148 SEC_PF_PCI_DEVICE_ID
, NULL
);
150 q_num
= min_t(u32
, SEC_QUEUE_NUM_V1
, SEC_QUEUE_NUM_V2
);
151 pr_info("No device, suppose queue number is %d!\n", q_num
);
153 rev_id
= pdev
->revision
;
157 q_num
= SEC_QUEUE_NUM_V1
;
160 q_num
= SEC_QUEUE_NUM_V2
;
167 ret
= kstrtou32(val
, 10, &n
);
168 if (ret
|| !n
|| n
> q_num
)
171 return param_set_int(val
, kp
);
174 static const struct kernel_param_ops sec_pf_q_num_ops
= {
175 .set
= sec_pf_q_num_set
,
176 .get
= param_get_int
,
178 static u32 pf_q_num
= SEC_PF_DEF_Q_NUM
;
179 module_param_cb(pf_q_num
, &sec_pf_q_num_ops
, &pf_q_num
, 0444);
180 MODULE_PARM_DESC(pf_q_num
, "Number of queues in PF(v1 0-4096, v2 0-1024)");
182 static int sec_ctx_q_num_set(const char *val
, const struct kernel_param
*kp
)
190 ret
= kstrtou32(val
, 10, &ctx_q_num
);
194 if (!ctx_q_num
|| ctx_q_num
> SEC_CTX_Q_NUM_MAX
|| ctx_q_num
& 0x1) {
195 pr_err("ctx queue num[%u] is invalid!\n", ctx_q_num
);
199 return param_set_int(val
, kp
);
202 static const struct kernel_param_ops sec_ctx_q_num_ops
= {
203 .set
= sec_ctx_q_num_set
,
204 .get
= param_get_int
,
206 static u32 ctx_q_num
= SEC_CTX_Q_NUM_DEF
;
207 module_param_cb(ctx_q_num
, &sec_ctx_q_num_ops
, &ctx_q_num
, 0444);
208 MODULE_PARM_DESC(ctx_q_num
, "Queue num in ctx (24 default, 2, 4, ..., 32)");
210 void sec_destroy_qps(struct hisi_qp
**qps
, int qp_num
)
212 hisi_qm_free_qps(qps
, qp_num
);
216 struct hisi_qp
**sec_create_qps(void)
218 int node
= cpu_to_node(smp_processor_id());
219 u32 ctx_num
= ctx_q_num
;
220 struct hisi_qp
**qps
;
223 qps
= kcalloc(ctx_num
, sizeof(struct hisi_qp
*), GFP_KERNEL
);
227 ret
= hisi_qm_alloc_qps_node(&sec_devices
, ctx_num
, 0, node
, qps
);
236 static const struct pci_device_id sec_dev_ids
[] = {
237 { PCI_DEVICE(PCI_VENDOR_ID_HUAWEI
, SEC_PF_PCI_DEVICE_ID
) },
238 { PCI_DEVICE(PCI_VENDOR_ID_HUAWEI
, SEC_VF_PCI_DEVICE_ID
) },
241 MODULE_DEVICE_TABLE(pci
, sec_dev_ids
);
243 static u8
sec_get_endian(struct sec_dev
*sec
)
245 struct hisi_qm
*qm
= &sec
->qm
;
249 * As for VF, it is a wrong way to get endian setting by
250 * reading a register of the engine
252 if (qm
->pdev
->is_virtfn
) {
253 dev_err_ratelimited(&qm
->pdev
->dev
,
254 "cannot access a register in VF!\n");
257 reg
= readl_relaxed(qm
->io_base
+ SEC_ENGINE_PF_CFG_OFF
+
258 SEC_ACC_COMMON_REG_OFF
+ SEC_CONTROL_REG
);
260 /* BD little endian mode */
264 /* BD 32-bits big endian mode */
265 else if (!(reg
& BIT(1)))
268 /* BD 64-bits big endian mode */
273 static int sec_engine_init(struct sec_dev
*sec
)
275 struct hisi_qm
*qm
= &sec
->qm
;
279 /* disable clock gate control */
280 reg
= readl_relaxed(SEC_ADDR(qm
, SEC_CONTROL_REG
));
281 reg
&= SEC_CLK_GATE_DISABLE
;
282 writel_relaxed(reg
, SEC_ADDR(qm
, SEC_CONTROL_REG
));
284 writel_relaxed(0x1, SEC_ADDR(qm
, SEC_MEM_START_INIT_REG
));
286 ret
= readl_relaxed_poll_timeout(SEC_ADDR(qm
, SEC_MEM_INIT_DONE_REG
),
287 reg
, reg
& 0x1, SEC_DELAY_10_US
,
288 SEC_POLL_TIMEOUT_US
);
290 dev_err(&qm
->pdev
->dev
, "fail to init sec mem\n");
294 reg
= readl_relaxed(SEC_ADDR(qm
, SEC_CONTROL_REG
));
295 reg
|= (0x1 << SEC_TRNG_EN_SHIFT
);
296 writel_relaxed(reg
, SEC_ADDR(qm
, SEC_CONTROL_REG
));
298 reg
= readl_relaxed(SEC_ADDR(qm
, SEC_INTERFACE_USER_CTRL0_REG
));
299 reg
|= SEC_USER0_SMMU_NORMAL
;
300 writel_relaxed(reg
, SEC_ADDR(qm
, SEC_INTERFACE_USER_CTRL0_REG
));
302 reg
= readl_relaxed(SEC_ADDR(qm
, SEC_INTERFACE_USER_CTRL1_REG
));
303 reg
|= SEC_USER1_SMMU_NORMAL
;
304 writel_relaxed(reg
, SEC_ADDR(qm
, SEC_INTERFACE_USER_CTRL1_REG
));
306 writel_relaxed(SEC_BD_ERR_CHK_EN1
,
307 SEC_ADDR(qm
, SEC_BD_ERR_CHK_EN_REG1
));
308 writel_relaxed(SEC_BD_ERR_CHK_EN2
,
309 SEC_ADDR(qm
, SEC_BD_ERR_CHK_EN_REG2
));
311 /* enable clock gate control */
312 reg
= readl_relaxed(SEC_ADDR(qm
, SEC_CONTROL_REG
));
313 reg
|= SEC_CLK_GATE_ENABLE
;
314 writel_relaxed(reg
, SEC_ADDR(qm
, SEC_CONTROL_REG
));
317 reg
= readl_relaxed(SEC_ADDR(qm
, SEC_CONTROL_REG
));
318 reg
|= sec_get_endian(sec
);
319 writel_relaxed(reg
, SEC_ADDR(qm
, SEC_CONTROL_REG
));
321 /* Enable sm4 xts mode multiple iv */
322 writel_relaxed(SEC_XTS_MIV_ENABLE_MSK
,
323 qm
->io_base
+ SEC_XTS_MIV_ENABLE_REG
);
328 static int sec_set_user_domain_and_cache(struct sec_dev
*sec
)
330 struct hisi_qm
*qm
= &sec
->qm
;
333 writel(AXUSER_BASE
, qm
->io_base
+ QM_ARUSER_M_CFG_1
);
334 writel(ARUSER_M_CFG_ENABLE
, qm
->io_base
+ QM_ARUSER_M_CFG_ENABLE
);
335 writel(AXUSER_BASE
, qm
->io_base
+ QM_AWUSER_M_CFG_1
);
336 writel(AWUSER_M_CFG_ENABLE
, qm
->io_base
+ QM_AWUSER_M_CFG_ENABLE
);
337 writel(WUSER_M_CFG_ENABLE
, qm
->io_base
+ QM_WUSER_M_CFG_ENABLE
);
340 writel(AXI_M_CFG
, qm
->io_base
+ QM_AXI_M_CFG
);
341 writel(AXI_M_CFG_ENABLE
, qm
->io_base
+ QM_AXI_M_CFG_ENABLE
);
343 /* disable FLR triggered by BME(bus master enable) */
344 writel(PEH_AXUSER_CFG
, qm
->io_base
+ QM_PEH_AXUSER_CFG
);
345 writel(PEH_AXUSER_CFG_ENABLE
, qm
->io_base
+ QM_PEH_AXUSER_CFG_ENABLE
);
347 /* enable sqc,cqc writeback */
348 writel(SQC_CACHE_ENABLE
| CQC_CACHE_ENABLE
| SQC_CACHE_WB_ENABLE
|
349 CQC_CACHE_WB_ENABLE
| FIELD_PREP(SQC_CACHE_WB_THRD
, 1) |
350 FIELD_PREP(CQC_CACHE_WB_THRD
, 1), qm
->io_base
+ QM_CACHE_CTL
);
352 return sec_engine_init(sec
);
355 /* sec_debug_regs_clear() - clear the sec debug regs */
356 static void sec_debug_regs_clear(struct hisi_qm
*qm
)
358 /* clear current_qm */
359 writel(0x0, qm
->io_base
+ QM_DFX_MB_CNT_VF
);
360 writel(0x0, qm
->io_base
+ QM_DFX_DB_CNT_VF
);
363 writel(0x0, qm
->io_base
+ SEC_CTRL_CNT_CLR_CE
);
365 hisi_qm_debug_regs_clear(qm
);
368 static void sec_hw_error_enable(struct hisi_qm
*qm
)
372 if (qm
->ver
== QM_HW_V1
) {
373 writel(SEC_CORE_INT_DISABLE
, qm
->io_base
+ SEC_CORE_INT_MASK
);
374 dev_info(&qm
->pdev
->dev
, "V1 not support hw error handle\n");
378 val
= readl(qm
->io_base
+ SEC_CONTROL_REG
);
380 /* clear SEC hw error source if having */
381 writel(SEC_CORE_INT_DISABLE
, qm
->io_base
+ SEC_CORE_INT_SOURCE
);
383 /* enable SEC hw error interrupts */
384 writel(SEC_CORE_INT_ENABLE
, qm
->io_base
+ SEC_CORE_INT_MASK
);
387 writel(SEC_RAS_CE_ENB_MSK
, qm
->io_base
+ SEC_RAS_CE_REG
);
388 writel(SEC_RAS_FE_ENB_MSK
, qm
->io_base
+ SEC_RAS_FE_REG
);
389 writel(SEC_RAS_NFE_ENB_MSK
, qm
->io_base
+ SEC_RAS_NFE_REG
);
391 /* enable SEC block master OOO when m-bit error occur */
392 val
= val
| SEC_AXI_SHUTDOWN_ENABLE
;
394 writel(val
, qm
->io_base
+ SEC_CONTROL_REG
);
397 static void sec_hw_error_disable(struct hisi_qm
*qm
)
401 val
= readl(qm
->io_base
+ SEC_CONTROL_REG
);
403 /* disable RAS int */
404 writel(SEC_RAS_DISABLE
, qm
->io_base
+ SEC_RAS_CE_REG
);
405 writel(SEC_RAS_DISABLE
, qm
->io_base
+ SEC_RAS_FE_REG
);
406 writel(SEC_RAS_DISABLE
, qm
->io_base
+ SEC_RAS_NFE_REG
);
408 /* disable SEC hw error interrupts */
409 writel(SEC_CORE_INT_DISABLE
, qm
->io_base
+ SEC_CORE_INT_MASK
);
411 /* disable SEC block master OOO when m-bit error occur */
412 val
= val
& SEC_AXI_SHUTDOWN_DISABLE
;
414 writel(val
, qm
->io_base
+ SEC_CONTROL_REG
);
417 static u32
sec_current_qm_read(struct sec_debug_file
*file
)
419 struct hisi_qm
*qm
= file
->qm
;
421 return readl(qm
->io_base
+ QM_DFX_MB_CNT_VF
);
424 static int sec_current_qm_write(struct sec_debug_file
*file
, u32 val
)
426 struct hisi_qm
*qm
= file
->qm
;
427 struct sec_dev
*sec
= container_of(qm
, struct sec_dev
, qm
);
431 if (val
> sec
->num_vfs
)
434 /* According PF or VF Dev ID to calculation curr_qm_qp_num and store */
436 qm
->debug
.curr_qm_qp_num
= qm
->qp_num
;
438 vfq_num
= (qm
->ctrl_qp_num
- qm
->qp_num
) / sec
->num_vfs
;
440 if (val
== sec
->num_vfs
)
441 qm
->debug
.curr_qm_qp_num
=
442 qm
->ctrl_qp_num
- qm
->qp_num
-
443 (sec
->num_vfs
- 1) * vfq_num
;
445 qm
->debug
.curr_qm_qp_num
= vfq_num
;
448 writel(val
, qm
->io_base
+ QM_DFX_MB_CNT_VF
);
449 writel(val
, qm
->io_base
+ QM_DFX_DB_CNT_VF
);
452 (readl(qm
->io_base
+ QM_DFX_SQE_CNT_VF_SQN
) & CURRENT_Q_MASK
);
453 writel(tmp
, qm
->io_base
+ QM_DFX_SQE_CNT_VF_SQN
);
456 (readl(qm
->io_base
+ QM_DFX_CQE_CNT_VF_CQN
) & CURRENT_Q_MASK
);
457 writel(tmp
, qm
->io_base
+ QM_DFX_CQE_CNT_VF_CQN
);
462 static u32
sec_clear_enable_read(struct sec_debug_file
*file
)
464 struct hisi_qm
*qm
= file
->qm
;
466 return readl(qm
->io_base
+ SEC_CTRL_CNT_CLR_CE
) &
467 SEC_CTRL_CNT_CLR_CE_BIT
;
470 static int sec_clear_enable_write(struct sec_debug_file
*file
, u32 val
)
472 struct hisi_qm
*qm
= file
->qm
;
478 tmp
= (readl(qm
->io_base
+ SEC_CTRL_CNT_CLR_CE
) &
479 ~SEC_CTRL_CNT_CLR_CE_BIT
) | val
;
480 writel(tmp
, qm
->io_base
+ SEC_CTRL_CNT_CLR_CE
);
485 static ssize_t
sec_debug_read(struct file
*filp
, char __user
*buf
,
486 size_t count
, loff_t
*pos
)
488 struct sec_debug_file
*file
= filp
->private_data
;
489 char tbuf
[SEC_DBGFS_VAL_MAX_LEN
];
493 spin_lock_irq(&file
->lock
);
495 switch (file
->index
) {
497 val
= sec_current_qm_read(file
);
499 case SEC_CLEAR_ENABLE
:
500 val
= sec_clear_enable_read(file
);
503 spin_unlock_irq(&file
->lock
);
507 spin_unlock_irq(&file
->lock
);
508 ret
= snprintf(tbuf
, SEC_DBGFS_VAL_MAX_LEN
, "%u\n", val
);
510 return simple_read_from_buffer(buf
, count
, pos
, tbuf
, ret
);
513 static ssize_t
sec_debug_write(struct file
*filp
, const char __user
*buf
,
514 size_t count
, loff_t
*pos
)
516 struct sec_debug_file
*file
= filp
->private_data
;
517 char tbuf
[SEC_DBGFS_VAL_MAX_LEN
];
524 if (count
>= SEC_DBGFS_VAL_MAX_LEN
)
527 len
= simple_write_to_buffer(tbuf
, SEC_DBGFS_VAL_MAX_LEN
- 1,
533 if (kstrtoul(tbuf
, 0, &val
))
536 spin_lock_irq(&file
->lock
);
538 switch (file
->index
) {
540 ret
= sec_current_qm_write(file
, val
);
544 case SEC_CLEAR_ENABLE
:
545 ret
= sec_clear_enable_write(file
, val
);
554 spin_unlock_irq(&file
->lock
);
559 spin_unlock_irq(&file
->lock
);
563 static const struct file_operations sec_dbg_fops
= {
564 .owner
= THIS_MODULE
,
566 .read
= sec_debug_read
,
567 .write
= sec_debug_write
,
570 static int sec_debugfs_atomic64_get(void *data
, u64
*val
)
572 *val
= atomic64_read((atomic64_t
*)data
);
575 DEFINE_DEBUGFS_ATTRIBUTE(sec_atomic64_ops
, sec_debugfs_atomic64_get
,
578 static int sec_core_debug_init(struct sec_dev
*sec
)
580 struct hisi_qm
*qm
= &sec
->qm
;
581 struct device
*dev
= &qm
->pdev
->dev
;
582 struct sec_dfx
*dfx
= &sec
->debug
.dfx
;
583 struct debugfs_regset32
*regset
;
584 struct dentry
*tmp_d
;
586 tmp_d
= debugfs_create_dir("sec_dfx", sec
->qm
.debug
.debug_root
);
588 regset
= devm_kzalloc(dev
, sizeof(*regset
), GFP_KERNEL
);
592 regset
->regs
= sec_dfx_regs
;
593 regset
->nregs
= ARRAY_SIZE(sec_dfx_regs
);
594 regset
->base
= qm
->io_base
;
596 debugfs_create_regset32("regs", 0444, tmp_d
, regset
);
598 debugfs_create_file("send_cnt", 0444, tmp_d
,
599 &dfx
->send_cnt
, &sec_atomic64_ops
);
601 debugfs_create_file("recv_cnt", 0444, tmp_d
,
602 &dfx
->recv_cnt
, &sec_atomic64_ops
);
607 static int sec_debug_init(struct sec_dev
*sec
)
611 for (i
= SEC_CURRENT_QM
; i
< SEC_DEBUG_FILE_NUM
; i
++) {
612 spin_lock_init(&sec
->debug
.files
[i
].lock
);
613 sec
->debug
.files
[i
].index
= i
;
614 sec
->debug
.files
[i
].qm
= &sec
->qm
;
616 debugfs_create_file(sec_dbg_file_name
[i
], 0600,
617 sec
->qm
.debug
.debug_root
,
618 sec
->debug
.files
+ i
,
622 return sec_core_debug_init(sec
);
625 static int sec_debugfs_init(struct sec_dev
*sec
)
627 struct hisi_qm
*qm
= &sec
->qm
;
628 struct device
*dev
= &qm
->pdev
->dev
;
631 qm
->debug
.debug_root
= debugfs_create_dir(dev_name(dev
),
633 ret
= hisi_qm_debug_init(qm
);
635 goto failed_to_create
;
637 if (qm
->pdev
->device
== SEC_PF_PCI_DEVICE_ID
) {
638 ret
= sec_debug_init(sec
);
640 goto failed_to_create
;
646 debugfs_remove_recursive(sec_debugfs_root
);
651 static void sec_debugfs_exit(struct sec_dev
*sec
)
653 debugfs_remove_recursive(sec
->qm
.debug
.debug_root
);
656 static void sec_log_hw_error(struct hisi_qm
*qm
, u32 err_sts
)
658 const struct sec_hw_error
*errs
= sec_hw_errors
;
659 struct device
*dev
= &qm
->pdev
->dev
;
663 if (errs
->int_msk
& err_sts
) {
664 dev_err(dev
, "%s [error status=0x%x] found\n",
665 errs
->msg
, errs
->int_msk
);
667 if (SEC_CORE_INT_STATUS_M_ECC
& errs
->int_msk
) {
668 err_val
= readl(qm
->io_base
+
669 SEC_CORE_SRAM_ECC_ERR_INFO
);
670 dev_err(dev
, "multi ecc sram num=0x%x\n",
671 SEC_ECC_NUM(err_val
));
672 dev_err(dev
, "multi ecc sram addr=0x%x\n",
673 SEC_ECC_ADDR(err_val
));
679 writel(err_sts
, qm
->io_base
+ SEC_CORE_INT_SOURCE
);
682 static u32
sec_get_hw_err_status(struct hisi_qm
*qm
)
684 return readl(qm
->io_base
+ SEC_CORE_INT_STATUS
);
687 static const struct hisi_qm_err_ini sec_err_ini
= {
688 .hw_err_enable
= sec_hw_error_enable
,
689 .hw_err_disable
= sec_hw_error_disable
,
690 .get_dev_hw_err_status
= sec_get_hw_err_status
,
691 .log_dev_hw_err
= sec_log_hw_error
,
694 .nfe
= QM_BASE_NFE
| QM_ACC_DO_TASK_TIMEOUT
|
695 QM_ACC_WB_NOT_READY_TIMEOUT
,
697 .msi
= QM_DB_RANDOM_INVALID
,
701 static int sec_pf_probe_init(struct sec_dev
*sec
)
703 struct hisi_qm
*qm
= &sec
->qm
;
708 qm
->ctrl_qp_num
= SEC_QUEUE_NUM_V1
;
712 qm
->ctrl_qp_num
= SEC_QUEUE_NUM_V2
;
719 qm
->err_ini
= &sec_err_ini
;
721 ret
= sec_set_user_domain_and_cache(sec
);
725 hisi_qm_dev_err_init(qm
);
726 sec_debug_regs_clear(qm
);
731 static int sec_qm_init(struct hisi_qm
*qm
, struct pci_dev
*pdev
)
733 enum qm_hw_ver rev_id
;
735 rev_id
= hisi_qm_get_hw_version(pdev
);
736 if (rev_id
== QM_HW_UNKNOWN
)
742 qm
->sqe_size
= SEC_SQE_SIZE
;
743 qm
->dev_name
= sec_name
;
744 qm
->fun_type
= (pdev
->device
== SEC_PF_PCI_DEVICE_ID
) ?
746 qm
->use_dma_api
= true;
748 return hisi_qm_init(qm
);
751 static void sec_qm_uninit(struct hisi_qm
*qm
)
756 static int sec_probe_init(struct hisi_qm
*qm
, struct sec_dev
*sec
)
761 * WQ_HIGHPRI: SEC request must be low delayed,
762 * so need a high priority workqueue.
763 * WQ_UNBOUND: SEC task is likely with long
764 * running CPU intensive workloads.
766 qm
->wq
= alloc_workqueue("%s", WQ_HIGHPRI
|
767 WQ_MEM_RECLAIM
| WQ_UNBOUND
, num_online_cpus(),
770 pci_err(qm
->pdev
, "fail to alloc workqueue\n");
774 if (qm
->fun_type
== QM_HW_PF
) {
775 qm
->qp_base
= SEC_PF_DEF_Q_BASE
;
776 qm
->qp_num
= pf_q_num
;
777 qm
->debug
.curr_qm_qp_num
= pf_q_num
;
779 ret
= sec_pf_probe_init(sec
);
781 goto err_probe_uninit
;
782 } else if (qm
->fun_type
== QM_HW_VF
) {
784 * have no way to get qm configure in VM in v1 hardware,
785 * so currently force PF to uses SEC_PF_DEF_Q_NUM, and force
786 * to trigger only one VF in v1 hardware.
787 * v2 hardware has no such problem.
789 if (qm
->ver
== QM_HW_V1
) {
790 qm
->qp_base
= SEC_PF_DEF_Q_NUM
;
791 qm
->qp_num
= SEC_QUEUE_NUM_V1
- SEC_PF_DEF_Q_NUM
;
792 } else if (qm
->ver
== QM_HW_V2
) {
793 /* v2 starts to support get vft by mailbox */
794 ret
= hisi_qm_get_vft(qm
, &qm
->qp_base
, &qm
->qp_num
);
796 goto err_probe_uninit
;
800 goto err_probe_uninit
;
805 destroy_workqueue(qm
->wq
);
809 static void sec_probe_uninit(struct hisi_qm
*qm
)
811 hisi_qm_dev_err_uninit(qm
);
813 destroy_workqueue(qm
->wq
);
816 static void sec_iommu_used_check(struct sec_dev
*sec
)
818 struct iommu_domain
*domain
;
819 struct device
*dev
= &sec
->qm
.pdev
->dev
;
821 domain
= iommu_get_domain_for_dev(dev
);
823 /* Check if iommu is used */
824 sec
->iommu_used
= false;
826 if (domain
->type
& __IOMMU_DOMAIN_PAGING
)
827 sec
->iommu_used
= true;
828 dev_info(dev
, "SMMU Opened, the iommu type = %u\n",
833 static int sec_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
839 sec
= devm_kzalloc(&pdev
->dev
, sizeof(*sec
), GFP_KERNEL
);
843 pci_set_drvdata(pdev
, sec
);
845 sec
->ctx_q_num
= ctx_q_num
;
846 sec_iommu_used_check(sec
);
850 ret
= sec_qm_init(qm
, pdev
);
852 pci_err(pdev
, "Failed to pre init qm!\n");
856 ret
= sec_probe_init(qm
, sec
);
858 pci_err(pdev
, "Failed to probe!\n");
862 ret
= hisi_qm_start(qm
);
864 pci_err(pdev
, "Failed to start sec qm!\n");
865 goto err_probe_uninit
;
868 ret
= sec_debugfs_init(sec
);
870 pci_warn(pdev
, "Failed to init debugfs!\n");
872 hisi_qm_add_to_list(qm
, &sec_devices
);
874 ret
= sec_register_to_crypto();
876 pr_err("Failed to register driver to crypto.\n");
877 goto err_remove_from_list
;
882 err_remove_from_list
:
883 hisi_qm_del_from_list(qm
, &sec_devices
);
884 sec_debugfs_exit(sec
);
888 sec_probe_uninit(qm
);
896 /* now we only support equal assignment */
897 static int sec_vf_q_assign(struct sec_dev
*sec
, u32 num_vfs
)
899 struct hisi_qm
*qm
= &sec
->qm
;
900 u32 qp_num
= qm
->qp_num
;
902 u32 q_num
, remain_q_num
;
908 remain_q_num
= qm
->ctrl_qp_num
- qp_num
;
909 q_num
= remain_q_num
/ num_vfs
;
911 for (i
= 1; i
<= num_vfs
; i
++) {
913 q_num
+= remain_q_num
% num_vfs
;
914 ret
= hisi_qm_set_vft(qm
, i
, q_base
, q_num
);
916 for (j
= i
; j
> 0; j
--)
917 hisi_qm_set_vft(qm
, j
, 0, 0);
926 static int sec_clear_vft_config(struct sec_dev
*sec
)
928 struct hisi_qm
*qm
= &sec
->qm
;
929 u32 num_vfs
= sec
->num_vfs
;
933 for (i
= 1; i
<= num_vfs
; i
++) {
934 ret
= hisi_qm_set_vft(qm
, i
, 0, 0);
944 static int sec_sriov_enable(struct pci_dev
*pdev
, int max_vfs
)
946 struct sec_dev
*sec
= pci_get_drvdata(pdev
);
947 int pre_existing_vfs
, ret
;
950 pre_existing_vfs
= pci_num_vf(pdev
);
952 if (pre_existing_vfs
) {
953 pci_err(pdev
, "Can't enable VF. Please disable at first!\n");
957 num_vfs
= min_t(u32
, max_vfs
, SEC_VF_NUM
);
959 ret
= sec_vf_q_assign(sec
, num_vfs
);
961 pci_err(pdev
, "Can't assign queues for VF!\n");
965 sec
->num_vfs
= num_vfs
;
967 ret
= pci_enable_sriov(pdev
, num_vfs
);
969 pci_err(pdev
, "Can't enable VF!\n");
970 sec_clear_vft_config(sec
);
977 static int sec_sriov_disable(struct pci_dev
*pdev
)
979 struct sec_dev
*sec
= pci_get_drvdata(pdev
);
981 if (pci_vfs_assigned(pdev
)) {
982 pci_err(pdev
, "Can't disable VFs while VFs are assigned!\n");
986 /* remove in sec_pci_driver will be called to free VF resources */
987 pci_disable_sriov(pdev
);
989 return sec_clear_vft_config(sec
);
992 static int sec_sriov_configure(struct pci_dev
*pdev
, int num_vfs
)
995 return sec_sriov_enable(pdev
, num_vfs
);
997 return sec_sriov_disable(pdev
);
1000 static void sec_remove(struct pci_dev
*pdev
)
1002 struct sec_dev
*sec
= pci_get_drvdata(pdev
);
1003 struct hisi_qm
*qm
= &sec
->qm
;
1005 sec_unregister_from_crypto();
1007 hisi_qm_del_from_list(qm
, &sec_devices
);
1009 if (qm
->fun_type
== QM_HW_PF
&& sec
->num_vfs
)
1010 (void)sec_sriov_disable(pdev
);
1012 sec_debugfs_exit(sec
);
1014 (void)hisi_qm_stop(qm
);
1016 if (qm
->fun_type
== QM_HW_PF
)
1017 sec_debug_regs_clear(qm
);
1019 sec_probe_uninit(qm
);
1024 static const struct pci_error_handlers sec_err_handler
= {
1025 .error_detected
= hisi_qm_dev_err_detected
,
1028 static struct pci_driver sec_pci_driver
= {
1029 .name
= "hisi_sec2",
1030 .id_table
= sec_dev_ids
,
1032 .remove
= sec_remove
,
1033 .err_handler
= &sec_err_handler
,
1034 .sriov_configure
= sec_sriov_configure
,
1037 static void sec_register_debugfs(void)
1039 if (!debugfs_initialized())
1042 sec_debugfs_root
= debugfs_create_dir("hisi_sec2", NULL
);
1045 static void sec_unregister_debugfs(void)
1047 debugfs_remove_recursive(sec_debugfs_root
);
1050 static int __init
sec_init(void)
1054 hisi_qm_init_list(&sec_devices
);
1055 sec_register_debugfs();
1057 ret
= pci_register_driver(&sec_pci_driver
);
1059 sec_unregister_debugfs();
1060 pr_err("Failed to register pci driver.\n");
1067 static void __exit
sec_exit(void)
1069 pci_unregister_driver(&sec_pci_driver
);
1070 sec_unregister_debugfs();
1073 module_init(sec_init
);
1074 module_exit(sec_exit
);
1076 MODULE_LICENSE("GPL v2");
1077 MODULE_AUTHOR("Zaibo Xu <xuzaibo@huawei.com>");
1078 MODULE_AUTHOR("Longfang Liu <liulongfang@huawei.com>");
1079 MODULE_AUTHOR("Wei Zhang <zhangwei375@huawei.com>");
1080 MODULE_DESCRIPTION("Driver for HiSilicon SEC accelerator");