1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 HiSilicon Limited. */
4 #include <linux/acpi.h>
5 #include <linux/bitops.h>
6 #include <linux/debugfs.h>
7 #include <linux/init.h>
9 #include <linux/iommu.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/seq_file.h>
15 #include <linux/topology.h>
16 #include <linux/uacce.h>
19 #define CAP_FILE_PERMISSION 0444
21 #define SEC_QUEUE_NUM_V1 4096
22 #define PCI_DEVICE_ID_HUAWEI_SEC_PF 0xa255
24 #define SEC_BD_ERR_CHK_EN0 0xEFFFFFFF
25 #define SEC_BD_ERR_CHK_EN1 0x7ffff7fd
26 #define SEC_BD_ERR_CHK_EN3 0xffffbfff
28 #define SEC_SQE_SIZE 128
29 #define SEC_PF_DEF_Q_NUM 256
30 #define SEC_PF_DEF_Q_BASE 0
31 #define SEC_CTX_Q_NUM_DEF 2
32 #define SEC_CTX_Q_NUM_MAX 32
34 #define SEC_CTRL_CNT_CLR_CE 0x301120
35 #define SEC_CTRL_CNT_CLR_CE_BIT BIT(0)
36 #define SEC_CORE_INT_SOURCE 0x301010
37 #define SEC_CORE_INT_MASK 0x301000
38 #define SEC_CORE_INT_STATUS 0x301008
39 #define SEC_CORE_SRAM_ECC_ERR_INFO 0x301C14
40 #define SEC_ECC_NUM 16
41 #define SEC_ECC_MASH 0xFF
42 #define SEC_CORE_INT_DISABLE 0x0
44 #define SEC_RAS_CE_REG 0x301050
45 #define SEC_RAS_FE_REG 0x301054
46 #define SEC_RAS_NFE_REG 0x301058
47 #define SEC_RAS_FE_ENB_MSK 0x0
48 #define SEC_OOO_SHUTDOWN_SEL 0x301014
49 #define SEC_RAS_DISABLE 0x0
50 #define SEC_MEM_START_INIT_REG 0x301100
51 #define SEC_MEM_INIT_DONE_REG 0x301104
54 #define SEC_CONTROL_REG 0x301200
55 #define SEC_DYNAMIC_GATE_REG 0x30121c
56 #define SEC_CORE_AUTO_GATE 0x30212c
57 #define SEC_DYNAMIC_GATE_EN 0x7fff
58 #define SEC_CORE_AUTO_GATE_EN GENMASK(3, 0)
59 #define SEC_CLK_GATE_ENABLE BIT(3)
60 #define SEC_CLK_GATE_DISABLE (~BIT(3))
62 #define SEC_TRNG_EN_SHIFT 8
63 #define SEC_AXI_SHUTDOWN_ENABLE BIT(12)
64 #define SEC_AXI_SHUTDOWN_DISABLE 0xFFFFEFFF
66 #define SEC_INTERFACE_USER_CTRL0_REG 0x301220
67 #define SEC_INTERFACE_USER_CTRL1_REG 0x301224
68 #define SEC_SAA_EN_REG 0x301270
69 #define SEC_BD_ERR_CHK_EN_REG0 0x301380
70 #define SEC_BD_ERR_CHK_EN_REG1 0x301384
71 #define SEC_BD_ERR_CHK_EN_REG3 0x30138c
73 #define SEC_USER0_SMMU_NORMAL (BIT(23) | BIT(15))
74 #define SEC_USER1_SMMU_NORMAL (BIT(31) | BIT(23) | BIT(15) | BIT(7))
75 #define SEC_USER1_ENABLE_CONTEXT_SSV BIT(24)
76 #define SEC_USER1_ENABLE_DATA_SSV BIT(16)
77 #define SEC_USER1_WB_CONTEXT_SSV BIT(8)
78 #define SEC_USER1_WB_DATA_SSV BIT(0)
79 #define SEC_USER1_SVA_SET (SEC_USER1_ENABLE_CONTEXT_SSV | \
80 SEC_USER1_ENABLE_DATA_SSV | \
81 SEC_USER1_WB_CONTEXT_SSV | \
82 SEC_USER1_WB_DATA_SSV)
83 #define SEC_USER1_SMMU_SVA (SEC_USER1_SMMU_NORMAL | SEC_USER1_SVA_SET)
84 #define SEC_USER1_SMMU_MASK (~SEC_USER1_SVA_SET)
85 #define SEC_INTERFACE_USER_CTRL0_REG_V3 0x302220
86 #define SEC_INTERFACE_USER_CTRL1_REG_V3 0x302224
87 #define SEC_USER1_SMMU_NORMAL_V3 (BIT(23) | BIT(17) | BIT(11) | BIT(5))
88 #define SEC_USER1_SMMU_MASK_V3 0xFF79E79E
89 #define SEC_CORE_INT_STATUS_M_ECC BIT(2)
91 #define SEC_PREFETCH_CFG 0x301130
92 #define SEC_SVA_TRANS 0x301EC4
93 #define SEC_PREFETCH_ENABLE (~(BIT(0) | BIT(1) | BIT(11)))
94 #define SEC_PREFETCH_DISABLE BIT(1)
95 #define SEC_SVA_DISABLE_READY (BIT(7) | BIT(11))
97 #define SEC_DELAY_10_US 10
98 #define SEC_POLL_TIMEOUT_US 1000
99 #define SEC_DBGFS_VAL_MAX_LEN 20
100 #define SEC_SINGLE_PORT_MAX_TRANS 0x2060
102 #define SEC_SQE_MASK_OFFSET 16
103 #define SEC_SQE_MASK_LEN 108
104 #define SEC_SHAPER_TYPE_RATE 400
106 #define SEC_DFX_BASE 0x301000
107 #define SEC_DFX_CORE 0x302100
108 #define SEC_DFX_COMMON1 0x301600
109 #define SEC_DFX_COMMON2 0x301C00
110 #define SEC_DFX_BASE_LEN 0x9D
111 #define SEC_DFX_CORE_LEN 0x32B
112 #define SEC_DFX_COMMON1_LEN 0x45
113 #define SEC_DFX_COMMON2_LEN 0xBA
115 #define SEC_ALG_BITMAP_SHIFT 32
117 #define SEC_CIPHER_BITMAP (GENMASK_ULL(5, 0) | GENMASK_ULL(16, 12) | \
119 #define SEC_DIGEST_BITMAP (GENMASK_ULL(11, 8) | GENMASK_ULL(20, 19) | \
121 #define SEC_AEAD_BITMAP (GENMASK_ULL(7, 6) | GENMASK_ULL(18, 17) | \
124 struct sec_hw_error
{
129 struct sec_dfx_item
{
134 static const char sec_name
[] = "hisi_sec2";
135 static struct dentry
*sec_debugfs_root
;
137 static struct hisi_qm_list sec_devices
= {
138 .register_to_crypto
= sec_register_to_crypto
,
139 .unregister_from_crypto
= sec_unregister_from_crypto
,
142 static const struct hisi_qm_cap_info sec_basic_info
[] = {
143 {SEC_QM_NFE_MASK_CAP
, 0x3124, 0, GENMASK(31, 0), 0x0, 0x1C77, 0x7C77},
144 {SEC_QM_RESET_MASK_CAP
, 0x3128, 0, GENMASK(31, 0), 0x0, 0xC77, 0x6C77},
145 {SEC_QM_OOO_SHUTDOWN_MASK_CAP
, 0x3128, 0, GENMASK(31, 0), 0x0, 0x4, 0x6C77},
146 {SEC_QM_CE_MASK_CAP
, 0x312C, 0, GENMASK(31, 0), 0x0, 0x8, 0x8},
147 {SEC_NFE_MASK_CAP
, 0x3130, 0, GENMASK(31, 0), 0x0, 0x177, 0x60177},
148 {SEC_RESET_MASK_CAP
, 0x3134, 0, GENMASK(31, 0), 0x0, 0x177, 0x177},
149 {SEC_OOO_SHUTDOWN_MASK_CAP
, 0x3134, 0, GENMASK(31, 0), 0x0, 0x4, 0x177},
150 {SEC_CE_MASK_CAP
, 0x3138, 0, GENMASK(31, 0), 0x0, 0x88, 0xC088},
151 {SEC_CLUSTER_NUM_CAP
, 0x313c, 20, GENMASK(3, 0), 0x1, 0x1, 0x1},
152 {SEC_CORE_TYPE_NUM_CAP
, 0x313c, 16, GENMASK(3, 0), 0x1, 0x1, 0x1},
153 {SEC_CORE_NUM_CAP
, 0x313c, 8, GENMASK(7, 0), 0x4, 0x4, 0x4},
154 {SEC_CORES_PER_CLUSTER_NUM_CAP
, 0x313c, 0, GENMASK(7, 0), 0x4, 0x4, 0x4},
155 {SEC_CORE_ENABLE_BITMAP
, 0x3140, 0, GENMASK(31, 0), 0x17F, 0x17F, 0xF},
156 {SEC_DRV_ALG_BITMAP_LOW
, 0x3144, 0, GENMASK(31, 0), 0x18050CB, 0x18050CB, 0x18670CF},
157 {SEC_DRV_ALG_BITMAP_HIGH
, 0x3148, 0, GENMASK(31, 0), 0x395C, 0x395C, 0x395C},
158 {SEC_DEV_ALG_BITMAP_LOW
, 0x314c, 0, GENMASK(31, 0), 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
159 {SEC_DEV_ALG_BITMAP_HIGH
, 0x3150, 0, GENMASK(31, 0), 0x3FFF, 0x3FFF, 0x3FFF},
160 {SEC_CORE1_ALG_BITMAP_LOW
, 0x3154, 0, GENMASK(31, 0), 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
161 {SEC_CORE1_ALG_BITMAP_HIGH
, 0x3158, 0, GENMASK(31, 0), 0x3FFF, 0x3FFF, 0x3FFF},
162 {SEC_CORE2_ALG_BITMAP_LOW
, 0x315c, 0, GENMASK(31, 0), 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
163 {SEC_CORE2_ALG_BITMAP_HIGH
, 0x3160, 0, GENMASK(31, 0), 0x3FFF, 0x3FFF, 0x3FFF},
164 {SEC_CORE3_ALG_BITMAP_LOW
, 0x3164, 0, GENMASK(31, 0), 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
165 {SEC_CORE3_ALG_BITMAP_HIGH
, 0x3168, 0, GENMASK(31, 0), 0x3FFF, 0x3FFF, 0x3FFF},
166 {SEC_CORE4_ALG_BITMAP_LOW
, 0x316c, 0, GENMASK(31, 0), 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
167 {SEC_CORE4_ALG_BITMAP_HIGH
, 0x3170, 0, GENMASK(31, 0), 0x3FFF, 0x3FFF, 0x3FFF},
170 static const struct hisi_qm_cap_query_info sec_cap_query_info
[] = {
171 {QM_RAS_NFE_TYPE
, "QM_RAS_NFE_TYPE ", 0x3124, 0x0, 0x1C77, 0x7C77},
172 {QM_RAS_NFE_RESET
, "QM_RAS_NFE_RESET ", 0x3128, 0x0, 0xC77, 0x6C77},
173 {QM_RAS_CE_TYPE
, "QM_RAS_CE_TYPE ", 0x312C, 0x0, 0x8, 0x8},
174 {SEC_RAS_NFE_TYPE
, "SEC_RAS_NFE_TYPE ", 0x3130, 0x0, 0x177, 0x60177},
175 {SEC_RAS_NFE_RESET
, "SEC_RAS_NFE_RESET ", 0x3134, 0x0, 0x177, 0x177},
176 {SEC_RAS_CE_TYPE
, "SEC_RAS_CE_TYPE ", 0x3138, 0x0, 0x88, 0xC088},
177 {SEC_CORE_INFO
, "SEC_CORE_INFO ", 0x313c, 0x110404, 0x110404, 0x110404},
178 {SEC_CORE_EN
, "SEC_CORE_EN ", 0x3140, 0x17F, 0x17F, 0xF},
179 {SEC_DRV_ALG_BITMAP_LOW_TB
, "SEC_DRV_ALG_BITMAP_LOW ",
180 0x3144, 0x18050CB, 0x18050CB, 0x18670CF},
181 {SEC_DRV_ALG_BITMAP_HIGH_TB
, "SEC_DRV_ALG_BITMAP_HIGH ",
182 0x3148, 0x395C, 0x395C, 0x395C},
183 {SEC_ALG_BITMAP_LOW
, "SEC_ALG_BITMAP_LOW ",
184 0x314c, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
185 {SEC_ALG_BITMAP_HIGH
, "SEC_ALG_BITMAP_HIGH ", 0x3150, 0x3FFF, 0x3FFF, 0x3FFF},
186 {SEC_CORE1_BITMAP_LOW
, "SEC_CORE1_BITMAP_LOW ",
187 0x3154, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
188 {SEC_CORE1_BITMAP_HIGH
, "SEC_CORE1_BITMAP_HIGH ", 0x3158, 0x3FFF, 0x3FFF, 0x3FFF},
189 {SEC_CORE2_BITMAP_LOW
, "SEC_CORE2_BITMAP_LOW ",
190 0x315c, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
191 {SEC_CORE2_BITMAP_HIGH
, "SEC_CORE2_BITMAP_HIGH ", 0x3160, 0x3FFF, 0x3FFF, 0x3FFF},
192 {SEC_CORE3_BITMAP_LOW
, "SEC_CORE3_BITMAP_LOW ",
193 0x3164, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
194 {SEC_CORE3_BITMAP_HIGH
, "SEC_CORE3_BITMAP_HIGH ", 0x3168, 0x3FFF, 0x3FFF, 0x3FFF},
195 {SEC_CORE4_BITMAP_LOW
, "SEC_CORE4_BITMAP_LOW ",
196 0x316c, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
197 {SEC_CORE4_BITMAP_HIGH
, "SEC_CORE4_BITMAP_HIGH ", 0x3170, 0x3FFF, 0x3FFF, 0x3FFF},
200 static const struct qm_dev_alg sec_dev_algs
[] = { {
201 .alg_msk
= SEC_CIPHER_BITMAP
,
204 .alg_msk
= SEC_DIGEST_BITMAP
,
207 .alg_msk
= SEC_AEAD_BITMAP
,
212 static const struct sec_hw_error sec_hw_errors
[] = {
215 .msg
= "sec_axi_rresp_err_rint"
219 .msg
= "sec_axi_bresp_err_rint"
223 .msg
= "sec_ecc_2bit_err_rint"
227 .msg
= "sec_ecc_1bit_err_rint"
231 .msg
= "sec_req_trng_timeout_rint"
235 .msg
= "sec_fsm_hbeat_rint"
239 .msg
= "sec_channel_req_rng_timeout_rint"
243 .msg
= "sec_bd_err_rint"
247 .msg
= "sec_chain_buff_err_rint"
251 .msg
= "sec_no_secure_access"
255 .msg
= "sec_wrapping_key_auth_err"
259 .msg
= "sec_km_key_crc_fail"
263 .msg
= "sec_axi_poison_err"
272 static const char * const sec_dbg_file_name
[] = {
273 [SEC_CLEAR_ENABLE
] = "clear_enable",
276 static struct sec_dfx_item sec_dfx_labels
[] = {
277 {"send_cnt", offsetof(struct sec_dfx
, send_cnt
)},
278 {"recv_cnt", offsetof(struct sec_dfx
, recv_cnt
)},
279 {"send_busy_cnt", offsetof(struct sec_dfx
, send_busy_cnt
)},
280 {"recv_busy_cnt", offsetof(struct sec_dfx
, recv_busy_cnt
)},
281 {"err_bd_cnt", offsetof(struct sec_dfx
, err_bd_cnt
)},
282 {"invalid_req_cnt", offsetof(struct sec_dfx
, invalid_req_cnt
)},
283 {"done_flag_cnt", offsetof(struct sec_dfx
, done_flag_cnt
)},
286 static const struct debugfs_reg32 sec_dfx_regs
[] = {
287 {"SEC_PF_ABNORMAL_INT_SOURCE ", 0x301010},
288 {"SEC_SAA_EN ", 0x301270},
289 {"SEC_BD_LATENCY_MIN ", 0x301600},
290 {"SEC_BD_LATENCY_MAX ", 0x301608},
291 {"SEC_BD_LATENCY_AVG ", 0x30160C},
292 {"SEC_BD_NUM_IN_SAA0 ", 0x301670},
293 {"SEC_BD_NUM_IN_SAA1 ", 0x301674},
294 {"SEC_BD_NUM_IN_SEC ", 0x301680},
295 {"SEC_ECC_1BIT_CNT ", 0x301C00},
296 {"SEC_ECC_1BIT_INFO ", 0x301C04},
297 {"SEC_ECC_2BIT_CNT ", 0x301C10},
298 {"SEC_ECC_2BIT_INFO ", 0x301C14},
299 {"SEC_BD_SAA0 ", 0x301C20},
300 {"SEC_BD_SAA1 ", 0x301C24},
301 {"SEC_BD_SAA2 ", 0x301C28},
302 {"SEC_BD_SAA3 ", 0x301C2C},
303 {"SEC_BD_SAA4 ", 0x301C30},
304 {"SEC_BD_SAA5 ", 0x301C34},
305 {"SEC_BD_SAA6 ", 0x301C38},
306 {"SEC_BD_SAA7 ", 0x301C3C},
307 {"SEC_BD_SAA8 ", 0x301C40},
308 {"SEC_RAS_CE_ENABLE ", 0x301050},
309 {"SEC_RAS_FE_ENABLE ", 0x301054},
310 {"SEC_RAS_NFE_ENABLE ", 0x301058},
311 {"SEC_REQ_TRNG_TIME_TH ", 0x30112C},
312 {"SEC_CHANNEL_RNG_REQ_THLD ", 0x302110},
315 /* define the SEC's dfx regs region and region length */
316 static struct dfx_diff_registers sec_diff_regs
[] = {
318 .reg_offset
= SEC_DFX_BASE
,
319 .reg_len
= SEC_DFX_BASE_LEN
,
321 .reg_offset
= SEC_DFX_COMMON1
,
322 .reg_len
= SEC_DFX_COMMON1_LEN
,
324 .reg_offset
= SEC_DFX_COMMON2
,
325 .reg_len
= SEC_DFX_COMMON2_LEN
,
327 .reg_offset
= SEC_DFX_CORE
,
328 .reg_len
= SEC_DFX_CORE_LEN
,
332 static int sec_diff_regs_show(struct seq_file
*s
, void *unused
)
334 struct hisi_qm
*qm
= s
->private;
336 hisi_qm_acc_diff_regs_dump(qm
, s
, qm
->debug
.acc_diff_regs
,
337 ARRAY_SIZE(sec_diff_regs
));
341 DEFINE_SHOW_ATTRIBUTE(sec_diff_regs
);
343 static bool pf_q_num_flag
;
344 static int sec_pf_q_num_set(const char *val
, const struct kernel_param
*kp
)
346 pf_q_num_flag
= true;
348 return hisi_qm_q_num_set(val
, kp
, PCI_DEVICE_ID_HUAWEI_SEC_PF
);
351 static const struct kernel_param_ops sec_pf_q_num_ops
= {
352 .set
= sec_pf_q_num_set
,
353 .get
= param_get_int
,
356 static u32 pf_q_num
= SEC_PF_DEF_Q_NUM
;
357 module_param_cb(pf_q_num
, &sec_pf_q_num_ops
, &pf_q_num
, 0444);
358 MODULE_PARM_DESC(pf_q_num
, "Number of queues in PF(v1 2-4096, v2 2-1024)");
360 static int sec_ctx_q_num_set(const char *val
, const struct kernel_param
*kp
)
368 ret
= kstrtou32(val
, 10, &ctx_q_num
);
372 if (!ctx_q_num
|| ctx_q_num
> SEC_CTX_Q_NUM_MAX
|| ctx_q_num
& 0x1) {
373 pr_err("ctx queue num[%u] is invalid!\n", ctx_q_num
);
377 return param_set_int(val
, kp
);
380 static const struct kernel_param_ops sec_ctx_q_num_ops
= {
381 .set
= sec_ctx_q_num_set
,
382 .get
= param_get_int
,
384 static u32 ctx_q_num
= SEC_CTX_Q_NUM_DEF
;
385 module_param_cb(ctx_q_num
, &sec_ctx_q_num_ops
, &ctx_q_num
, 0444);
386 MODULE_PARM_DESC(ctx_q_num
, "Queue num in ctx (2 default, 2, 4, ..., 32)");
388 static const struct kernel_param_ops vfs_num_ops
= {
390 .get
= param_get_int
,
394 module_param_cb(vfs_num
, &vfs_num_ops
, &vfs_num
, 0444);
395 MODULE_PARM_DESC(vfs_num
, "Number of VFs to enable(1-63), 0(default)");
397 void sec_destroy_qps(struct hisi_qp
**qps
, int qp_num
)
399 hisi_qm_free_qps(qps
, qp_num
);
403 struct hisi_qp
**sec_create_qps(void)
405 int node
= cpu_to_node(raw_smp_processor_id());
406 u32 ctx_num
= ctx_q_num
;
407 struct hisi_qp
**qps
;
410 qps
= kcalloc(ctx_num
, sizeof(struct hisi_qp
*), GFP_KERNEL
);
414 ret
= hisi_qm_alloc_qps_node(&sec_devices
, ctx_num
, 0, node
, qps
);
422 u64
sec_get_alg_bitmap(struct hisi_qm
*qm
, u32 high
, u32 low
)
424 u32 cap_val_h
, cap_val_l
;
426 cap_val_h
= qm
->cap_tables
.dev_cap_table
[high
].cap_val
;
427 cap_val_l
= qm
->cap_tables
.dev_cap_table
[low
].cap_val
;
429 return ((u64
)cap_val_h
<< SEC_ALG_BITMAP_SHIFT
) | (u64
)cap_val_l
;
432 static const struct kernel_param_ops sec_uacce_mode_ops
= {
433 .set
= uacce_mode_set
,
434 .get
= param_get_int
,
438 * uacce_mode = 0 means sec only register to crypto,
439 * uacce_mode = 1 means sec both register to crypto and uacce.
441 static u32 uacce_mode
= UACCE_MODE_NOUACCE
;
442 module_param_cb(uacce_mode
, &sec_uacce_mode_ops
, &uacce_mode
, 0444);
443 MODULE_PARM_DESC(uacce_mode
, UACCE_MODE_DESC
);
445 static const struct pci_device_id sec_dev_ids
[] = {
446 { PCI_DEVICE(PCI_VENDOR_ID_HUAWEI
, PCI_DEVICE_ID_HUAWEI_SEC_PF
) },
447 { PCI_DEVICE(PCI_VENDOR_ID_HUAWEI
, PCI_DEVICE_ID_HUAWEI_SEC_VF
) },
450 MODULE_DEVICE_TABLE(pci
, sec_dev_ids
);
452 static void sec_set_endian(struct hisi_qm
*qm
)
456 reg
= readl_relaxed(qm
->io_base
+ SEC_CONTROL_REG
);
457 reg
&= ~(BIT(1) | BIT(0));
458 if (!IS_ENABLED(CONFIG_64BIT
))
461 if (!IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN
))
464 writel_relaxed(reg
, qm
->io_base
+ SEC_CONTROL_REG
);
467 static void sec_engine_sva_config(struct hisi_qm
*qm
)
471 if (qm
->ver
> QM_HW_V2
) {
472 reg
= readl_relaxed(qm
->io_base
+
473 SEC_INTERFACE_USER_CTRL0_REG_V3
);
474 reg
|= SEC_USER0_SMMU_NORMAL
;
475 writel_relaxed(reg
, qm
->io_base
+
476 SEC_INTERFACE_USER_CTRL0_REG_V3
);
478 reg
= readl_relaxed(qm
->io_base
+
479 SEC_INTERFACE_USER_CTRL1_REG_V3
);
480 reg
&= SEC_USER1_SMMU_MASK_V3
;
481 reg
|= SEC_USER1_SMMU_NORMAL_V3
;
482 writel_relaxed(reg
, qm
->io_base
+
483 SEC_INTERFACE_USER_CTRL1_REG_V3
);
485 reg
= readl_relaxed(qm
->io_base
+
486 SEC_INTERFACE_USER_CTRL0_REG
);
487 reg
|= SEC_USER0_SMMU_NORMAL
;
488 writel_relaxed(reg
, qm
->io_base
+
489 SEC_INTERFACE_USER_CTRL0_REG
);
490 reg
= readl_relaxed(qm
->io_base
+
491 SEC_INTERFACE_USER_CTRL1_REG
);
492 reg
&= SEC_USER1_SMMU_MASK
;
494 reg
|= SEC_USER1_SMMU_SVA
;
496 reg
|= SEC_USER1_SMMU_NORMAL
;
497 writel_relaxed(reg
, qm
->io_base
+
498 SEC_INTERFACE_USER_CTRL1_REG
);
502 static void sec_open_sva_prefetch(struct hisi_qm
*qm
)
507 if (!test_bit(QM_SUPPORT_SVA_PREFETCH
, &qm
->caps
))
510 /* Enable prefetch */
511 val
= readl_relaxed(qm
->io_base
+ SEC_PREFETCH_CFG
);
512 val
&= SEC_PREFETCH_ENABLE
;
513 writel(val
, qm
->io_base
+ SEC_PREFETCH_CFG
);
515 ret
= readl_relaxed_poll_timeout(qm
->io_base
+ SEC_PREFETCH_CFG
,
516 val
, !(val
& SEC_PREFETCH_DISABLE
),
517 SEC_DELAY_10_US
, SEC_POLL_TIMEOUT_US
);
519 pci_err(qm
->pdev
, "failed to open sva prefetch\n");
522 static void sec_close_sva_prefetch(struct hisi_qm
*qm
)
527 if (!test_bit(QM_SUPPORT_SVA_PREFETCH
, &qm
->caps
))
530 val
= readl_relaxed(qm
->io_base
+ SEC_PREFETCH_CFG
);
531 val
|= SEC_PREFETCH_DISABLE
;
532 writel(val
, qm
->io_base
+ SEC_PREFETCH_CFG
);
534 ret
= readl_relaxed_poll_timeout(qm
->io_base
+ SEC_SVA_TRANS
,
535 val
, !(val
& SEC_SVA_DISABLE_READY
),
536 SEC_DELAY_10_US
, SEC_POLL_TIMEOUT_US
);
538 pci_err(qm
->pdev
, "failed to close sva prefetch\n");
541 static void sec_enable_clock_gate(struct hisi_qm
*qm
)
545 if (qm
->ver
< QM_HW_V3
)
548 val
= readl_relaxed(qm
->io_base
+ SEC_CONTROL_REG
);
549 val
|= SEC_CLK_GATE_ENABLE
;
550 writel_relaxed(val
, qm
->io_base
+ SEC_CONTROL_REG
);
552 val
= readl(qm
->io_base
+ SEC_DYNAMIC_GATE_REG
);
553 val
|= SEC_DYNAMIC_GATE_EN
;
554 writel(val
, qm
->io_base
+ SEC_DYNAMIC_GATE_REG
);
556 val
= readl(qm
->io_base
+ SEC_CORE_AUTO_GATE
);
557 val
|= SEC_CORE_AUTO_GATE_EN
;
558 writel(val
, qm
->io_base
+ SEC_CORE_AUTO_GATE
);
561 static void sec_disable_clock_gate(struct hisi_qm
*qm
)
565 /* Kunpeng920 needs to close clock gating */
566 val
= readl_relaxed(qm
->io_base
+ SEC_CONTROL_REG
);
567 val
&= SEC_CLK_GATE_DISABLE
;
568 writel_relaxed(val
, qm
->io_base
+ SEC_CONTROL_REG
);
571 static int sec_engine_init(struct hisi_qm
*qm
)
576 /* disable clock gate control before mem init */
577 sec_disable_clock_gate(qm
);
579 writel_relaxed(0x1, qm
->io_base
+ SEC_MEM_START_INIT_REG
);
581 ret
= readl_relaxed_poll_timeout(qm
->io_base
+ SEC_MEM_INIT_DONE_REG
,
582 reg
, reg
& 0x1, SEC_DELAY_10_US
,
583 SEC_POLL_TIMEOUT_US
);
585 pci_err(qm
->pdev
, "fail to init sec mem\n");
589 reg
= readl_relaxed(qm
->io_base
+ SEC_CONTROL_REG
);
590 reg
|= (0x1 << SEC_TRNG_EN_SHIFT
);
591 writel_relaxed(reg
, qm
->io_base
+ SEC_CONTROL_REG
);
593 sec_engine_sva_config(qm
);
595 writel(SEC_SINGLE_PORT_MAX_TRANS
,
596 qm
->io_base
+ AM_CFG_SINGLE_PORT_MAX_TRANS
);
598 reg
= hisi_qm_get_hw_info(qm
, sec_basic_info
, SEC_CORE_ENABLE_BITMAP
, qm
->cap_ver
);
599 writel(reg
, qm
->io_base
+ SEC_SAA_EN_REG
);
601 if (qm
->ver
< QM_HW_V3
) {
602 /* HW V2 enable sm4 extra mode, as ctr/ecb */
603 writel_relaxed(SEC_BD_ERR_CHK_EN0
,
604 qm
->io_base
+ SEC_BD_ERR_CHK_EN_REG0
);
606 /* HW V2 enable sm4 xts mode multiple iv */
607 writel_relaxed(SEC_BD_ERR_CHK_EN1
,
608 qm
->io_base
+ SEC_BD_ERR_CHK_EN_REG1
);
609 writel_relaxed(SEC_BD_ERR_CHK_EN3
,
610 qm
->io_base
+ SEC_BD_ERR_CHK_EN_REG3
);
616 sec_enable_clock_gate(qm
);
621 static int sec_set_user_domain_and_cache(struct hisi_qm
*qm
)
624 writel(AXUSER_BASE
, qm
->io_base
+ QM_ARUSER_M_CFG_1
);
625 writel(ARUSER_M_CFG_ENABLE
, qm
->io_base
+ QM_ARUSER_M_CFG_ENABLE
);
626 writel(AXUSER_BASE
, qm
->io_base
+ QM_AWUSER_M_CFG_1
);
627 writel(AWUSER_M_CFG_ENABLE
, qm
->io_base
+ QM_AWUSER_M_CFG_ENABLE
);
628 writel(WUSER_M_CFG_ENABLE
, qm
->io_base
+ QM_WUSER_M_CFG_ENABLE
);
631 writel(AXI_M_CFG
, qm
->io_base
+ QM_AXI_M_CFG
);
632 writel(AXI_M_CFG_ENABLE
, qm
->io_base
+ QM_AXI_M_CFG_ENABLE
);
634 /* disable FLR triggered by BME(bus master enable) */
635 writel(PEH_AXUSER_CFG
, qm
->io_base
+ QM_PEH_AXUSER_CFG
);
636 writel(PEH_AXUSER_CFG_ENABLE
, qm
->io_base
+ QM_PEH_AXUSER_CFG_ENABLE
);
638 /* enable sqc,cqc writeback */
639 writel(SQC_CACHE_ENABLE
| CQC_CACHE_ENABLE
| SQC_CACHE_WB_ENABLE
|
640 CQC_CACHE_WB_ENABLE
| FIELD_PREP(SQC_CACHE_WB_THRD
, 1) |
641 FIELD_PREP(CQC_CACHE_WB_THRD
, 1), qm
->io_base
+ QM_CACHE_CTL
);
643 return sec_engine_init(qm
);
646 /* sec_debug_regs_clear() - clear the sec debug regs */
647 static void sec_debug_regs_clear(struct hisi_qm
*qm
)
651 /* clear sec dfx regs */
652 writel(0x1, qm
->io_base
+ SEC_CTRL_CNT_CLR_CE
);
653 for (i
= 0; i
< ARRAY_SIZE(sec_dfx_regs
); i
++)
654 readl(qm
->io_base
+ sec_dfx_regs
[i
].offset
);
657 writel(0x0, qm
->io_base
+ SEC_CTRL_CNT_CLR_CE
);
659 hisi_qm_debug_regs_clear(qm
);
662 static void sec_master_ooo_ctrl(struct hisi_qm
*qm
, bool enable
)
666 val1
= readl(qm
->io_base
+ SEC_CONTROL_REG
);
668 val1
|= SEC_AXI_SHUTDOWN_ENABLE
;
669 val2
= hisi_qm_get_hw_info(qm
, sec_basic_info
,
670 SEC_OOO_SHUTDOWN_MASK_CAP
, qm
->cap_ver
);
672 val1
&= SEC_AXI_SHUTDOWN_DISABLE
;
676 if (qm
->ver
> QM_HW_V2
)
677 writel(val2
, qm
->io_base
+ SEC_OOO_SHUTDOWN_SEL
);
679 writel(val1
, qm
->io_base
+ SEC_CONTROL_REG
);
682 static void sec_hw_error_enable(struct hisi_qm
*qm
)
686 if (qm
->ver
== QM_HW_V1
) {
687 writel(SEC_CORE_INT_DISABLE
, qm
->io_base
+ SEC_CORE_INT_MASK
);
688 pci_info(qm
->pdev
, "V1 not support hw error handle\n");
692 ce
= hisi_qm_get_hw_info(qm
, sec_basic_info
, SEC_CE_MASK_CAP
, qm
->cap_ver
);
693 nfe
= hisi_qm_get_hw_info(qm
, sec_basic_info
, SEC_NFE_MASK_CAP
, qm
->cap_ver
);
695 /* clear SEC hw error source if having */
696 writel(ce
| nfe
| SEC_RAS_FE_ENB_MSK
, qm
->io_base
+ SEC_CORE_INT_SOURCE
);
699 writel(ce
, qm
->io_base
+ SEC_RAS_CE_REG
);
700 writel(SEC_RAS_FE_ENB_MSK
, qm
->io_base
+ SEC_RAS_FE_REG
);
701 writel(nfe
, qm
->io_base
+ SEC_RAS_NFE_REG
);
703 /* enable SEC block master OOO when nfe occurs on Kunpeng930 */
704 sec_master_ooo_ctrl(qm
, true);
706 /* enable SEC hw error interrupts */
707 writel(ce
| nfe
| SEC_RAS_FE_ENB_MSK
, qm
->io_base
+ SEC_CORE_INT_MASK
);
710 static void sec_hw_error_disable(struct hisi_qm
*qm
)
712 /* disable SEC hw error interrupts */
713 writel(SEC_CORE_INT_DISABLE
, qm
->io_base
+ SEC_CORE_INT_MASK
);
715 /* disable SEC block master OOO when nfe occurs on Kunpeng930 */
716 sec_master_ooo_ctrl(qm
, false);
718 /* disable RAS int */
719 writel(SEC_RAS_DISABLE
, qm
->io_base
+ SEC_RAS_CE_REG
);
720 writel(SEC_RAS_DISABLE
, qm
->io_base
+ SEC_RAS_FE_REG
);
721 writel(SEC_RAS_DISABLE
, qm
->io_base
+ SEC_RAS_NFE_REG
);
724 static u32
sec_clear_enable_read(struct hisi_qm
*qm
)
726 return readl(qm
->io_base
+ SEC_CTRL_CNT_CLR_CE
) &
727 SEC_CTRL_CNT_CLR_CE_BIT
;
730 static int sec_clear_enable_write(struct hisi_qm
*qm
, u32 val
)
737 tmp
= (readl(qm
->io_base
+ SEC_CTRL_CNT_CLR_CE
) &
738 ~SEC_CTRL_CNT_CLR_CE_BIT
) | val
;
739 writel(tmp
, qm
->io_base
+ SEC_CTRL_CNT_CLR_CE
);
744 static ssize_t
sec_debug_read(struct file
*filp
, char __user
*buf
,
745 size_t count
, loff_t
*pos
)
747 struct sec_debug_file
*file
= filp
->private_data
;
748 char tbuf
[SEC_DBGFS_VAL_MAX_LEN
];
749 struct hisi_qm
*qm
= file
->qm
;
753 ret
= hisi_qm_get_dfx_access(qm
);
757 spin_lock_irq(&file
->lock
);
759 switch (file
->index
) {
760 case SEC_CLEAR_ENABLE
:
761 val
= sec_clear_enable_read(qm
);
767 spin_unlock_irq(&file
->lock
);
769 hisi_qm_put_dfx_access(qm
);
770 ret
= snprintf(tbuf
, SEC_DBGFS_VAL_MAX_LEN
, "%u\n", val
);
771 return simple_read_from_buffer(buf
, count
, pos
, tbuf
, ret
);
774 spin_unlock_irq(&file
->lock
);
775 hisi_qm_put_dfx_access(qm
);
779 static ssize_t
sec_debug_write(struct file
*filp
, const char __user
*buf
,
780 size_t count
, loff_t
*pos
)
782 struct sec_debug_file
*file
= filp
->private_data
;
783 char tbuf
[SEC_DBGFS_VAL_MAX_LEN
];
784 struct hisi_qm
*qm
= file
->qm
;
791 if (count
>= SEC_DBGFS_VAL_MAX_LEN
)
794 len
= simple_write_to_buffer(tbuf
, SEC_DBGFS_VAL_MAX_LEN
- 1,
800 if (kstrtoul(tbuf
, 0, &val
))
803 ret
= hisi_qm_get_dfx_access(qm
);
807 spin_lock_irq(&file
->lock
);
809 switch (file
->index
) {
810 case SEC_CLEAR_ENABLE
:
811 ret
= sec_clear_enable_write(qm
, val
);
823 spin_unlock_irq(&file
->lock
);
824 hisi_qm_put_dfx_access(qm
);
828 static const struct file_operations sec_dbg_fops
= {
829 .owner
= THIS_MODULE
,
831 .read
= sec_debug_read
,
832 .write
= sec_debug_write
,
835 static int sec_debugfs_atomic64_get(void *data
, u64
*val
)
837 *val
= atomic64_read((atomic64_t
*)data
);
842 static int sec_debugfs_atomic64_set(void *data
, u64 val
)
847 atomic64_set((atomic64_t
*)data
, 0);
852 DEFINE_DEBUGFS_ATTRIBUTE(sec_atomic64_ops
, sec_debugfs_atomic64_get
,
853 sec_debugfs_atomic64_set
, "%lld\n");
855 static int sec_regs_show(struct seq_file
*s
, void *unused
)
857 hisi_qm_regs_dump(s
, s
->private);
862 DEFINE_SHOW_ATTRIBUTE(sec_regs
);
864 static int sec_cap_regs_show(struct seq_file
*s
, void *unused
)
866 struct hisi_qm
*qm
= s
->private;
869 size
= qm
->cap_tables
.qm_cap_size
;
870 for (i
= 0; i
< size
; i
++)
871 seq_printf(s
, "%s= 0x%08x\n", qm
->cap_tables
.qm_cap_table
[i
].name
,
872 qm
->cap_tables
.qm_cap_table
[i
].cap_val
);
874 size
= qm
->cap_tables
.dev_cap_size
;
875 for (i
= 0; i
< size
; i
++)
876 seq_printf(s
, "%s= 0x%08x\n", qm
->cap_tables
.dev_cap_table
[i
].name
,
877 qm
->cap_tables
.dev_cap_table
[i
].cap_val
);
882 DEFINE_SHOW_ATTRIBUTE(sec_cap_regs
);
884 static int sec_core_debug_init(struct hisi_qm
*qm
)
886 struct dfx_diff_registers
*sec_regs
= qm
->debug
.acc_diff_regs
;
887 struct sec_dev
*sec
= container_of(qm
, struct sec_dev
, qm
);
888 struct device
*dev
= &qm
->pdev
->dev
;
889 struct sec_dfx
*dfx
= &sec
->debug
.dfx
;
890 struct debugfs_regset32
*regset
;
891 struct dentry
*tmp_d
;
894 tmp_d
= debugfs_create_dir("sec_dfx", qm
->debug
.debug_root
);
896 regset
= devm_kzalloc(dev
, sizeof(*regset
), GFP_KERNEL
);
900 regset
->regs
= sec_dfx_regs
;
901 regset
->nregs
= ARRAY_SIZE(sec_dfx_regs
);
902 regset
->base
= qm
->io_base
;
905 if (qm
->pdev
->device
== PCI_DEVICE_ID_HUAWEI_SEC_PF
)
906 debugfs_create_file("regs", 0444, tmp_d
, regset
, &sec_regs_fops
);
907 if (qm
->fun_type
== QM_HW_PF
&& sec_regs
)
908 debugfs_create_file("diff_regs", 0444, tmp_d
,
909 qm
, &sec_diff_regs_fops
);
911 for (i
= 0; i
< ARRAY_SIZE(sec_dfx_labels
); i
++) {
912 atomic64_t
*data
= (atomic64_t
*)((uintptr_t)dfx
+
913 sec_dfx_labels
[i
].offset
);
914 debugfs_create_file(sec_dfx_labels
[i
].name
, 0644,
915 tmp_d
, data
, &sec_atomic64_ops
);
918 debugfs_create_file("cap_regs", CAP_FILE_PERMISSION
,
919 qm
->debug
.debug_root
, qm
, &sec_cap_regs_fops
);
924 static int sec_debug_init(struct hisi_qm
*qm
)
926 struct sec_dev
*sec
= container_of(qm
, struct sec_dev
, qm
);
929 if (qm
->pdev
->device
== PCI_DEVICE_ID_HUAWEI_SEC_PF
) {
930 for (i
= SEC_CLEAR_ENABLE
; i
< SEC_DEBUG_FILE_NUM
; i
++) {
931 spin_lock_init(&sec
->debug
.files
[i
].lock
);
932 sec
->debug
.files
[i
].index
= i
;
933 sec
->debug
.files
[i
].qm
= qm
;
935 debugfs_create_file(sec_dbg_file_name
[i
], 0600,
936 qm
->debug
.debug_root
,
937 sec
->debug
.files
+ i
,
942 return sec_core_debug_init(qm
);
945 static int sec_debugfs_init(struct hisi_qm
*qm
)
947 struct device
*dev
= &qm
->pdev
->dev
;
950 ret
= hisi_qm_regs_debugfs_init(qm
, sec_diff_regs
, ARRAY_SIZE(sec_diff_regs
));
952 dev_warn(dev
, "Failed to init SEC diff regs!\n");
956 qm
->debug
.debug_root
= debugfs_create_dir(dev_name(dev
),
958 qm
->debug
.sqe_mask_offset
= SEC_SQE_MASK_OFFSET
;
959 qm
->debug
.sqe_mask_len
= SEC_SQE_MASK_LEN
;
961 hisi_qm_debug_init(qm
);
963 ret
= sec_debug_init(qm
);
970 debugfs_remove_recursive(qm
->debug
.debug_root
);
971 hisi_qm_regs_debugfs_uninit(qm
, ARRAY_SIZE(sec_diff_regs
));
975 static void sec_debugfs_exit(struct hisi_qm
*qm
)
977 debugfs_remove_recursive(qm
->debug
.debug_root
);
979 hisi_qm_regs_debugfs_uninit(qm
, ARRAY_SIZE(sec_diff_regs
));
982 static int sec_show_last_regs_init(struct hisi_qm
*qm
)
984 struct qm_debug
*debug
= &qm
->debug
;
987 debug
->last_words
= kcalloc(ARRAY_SIZE(sec_dfx_regs
),
988 sizeof(unsigned int), GFP_KERNEL
);
989 if (!debug
->last_words
)
992 for (i
= 0; i
< ARRAY_SIZE(sec_dfx_regs
); i
++)
993 debug
->last_words
[i
] = readl_relaxed(qm
->io_base
+
994 sec_dfx_regs
[i
].offset
);
999 static void sec_show_last_regs_uninit(struct hisi_qm
*qm
)
1001 struct qm_debug
*debug
= &qm
->debug
;
1003 if (qm
->fun_type
== QM_HW_VF
|| !debug
->last_words
)
1006 kfree(debug
->last_words
);
1007 debug
->last_words
= NULL
;
1010 static void sec_show_last_dfx_regs(struct hisi_qm
*qm
)
1012 struct qm_debug
*debug
= &qm
->debug
;
1013 struct pci_dev
*pdev
= qm
->pdev
;
1017 if (qm
->fun_type
== QM_HW_VF
|| !debug
->last_words
)
1020 /* dumps last word of the debugging registers during controller reset */
1021 for (i
= 0; i
< ARRAY_SIZE(sec_dfx_regs
); i
++) {
1022 val
= readl_relaxed(qm
->io_base
+ sec_dfx_regs
[i
].offset
);
1023 if (val
!= debug
->last_words
[i
])
1024 pci_info(pdev
, "%s \t= 0x%08x => 0x%08x\n",
1025 sec_dfx_regs
[i
].name
, debug
->last_words
[i
], val
);
1029 static void sec_log_hw_error(struct hisi_qm
*qm
, u32 err_sts
)
1031 const struct sec_hw_error
*errs
= sec_hw_errors
;
1032 struct device
*dev
= &qm
->pdev
->dev
;
1036 if (errs
->int_msk
& err_sts
) {
1037 dev_err(dev
, "%s [error status=0x%x] found\n",
1038 errs
->msg
, errs
->int_msk
);
1040 if (SEC_CORE_INT_STATUS_M_ECC
& errs
->int_msk
) {
1041 err_val
= readl(qm
->io_base
+
1042 SEC_CORE_SRAM_ECC_ERR_INFO
);
1043 dev_err(dev
, "multi ecc sram num=0x%x\n",
1044 ((err_val
) >> SEC_ECC_NUM
) &
1052 static u32
sec_get_hw_err_status(struct hisi_qm
*qm
)
1054 return readl(qm
->io_base
+ SEC_CORE_INT_STATUS
);
1057 static void sec_clear_hw_err_status(struct hisi_qm
*qm
, u32 err_sts
)
1059 writel(err_sts
, qm
->io_base
+ SEC_CORE_INT_SOURCE
);
1062 static void sec_disable_error_report(struct hisi_qm
*qm
, u32 err_type
)
1066 nfe_mask
= hisi_qm_get_hw_info(qm
, sec_basic_info
, SEC_NFE_MASK_CAP
, qm
->cap_ver
);
1067 writel(nfe_mask
& (~err_type
), qm
->io_base
+ SEC_RAS_NFE_REG
);
1070 static void sec_open_axi_master_ooo(struct hisi_qm
*qm
)
1074 val
= readl(qm
->io_base
+ SEC_CONTROL_REG
);
1075 writel(val
& SEC_AXI_SHUTDOWN_DISABLE
, qm
->io_base
+ SEC_CONTROL_REG
);
1076 writel(val
| SEC_AXI_SHUTDOWN_ENABLE
, qm
->io_base
+ SEC_CONTROL_REG
);
1079 static enum acc_err_result
sec_get_err_result(struct hisi_qm
*qm
)
1083 err_status
= sec_get_hw_err_status(qm
);
1085 if (err_status
& qm
->err_info
.ecc_2bits_mask
)
1086 qm
->err_status
.is_dev_ecc_mbit
= true;
1087 sec_log_hw_error(qm
, err_status
);
1089 if (err_status
& qm
->err_info
.dev_reset_mask
) {
1090 /* Disable the same error reporting until device is recovered. */
1091 sec_disable_error_report(qm
, err_status
);
1092 return ACC_ERR_NEED_RESET
;
1094 sec_clear_hw_err_status(qm
, err_status
);
1097 return ACC_ERR_RECOVERED
;
1100 static void sec_err_info_init(struct hisi_qm
*qm
)
1102 struct hisi_qm_err_info
*err_info
= &qm
->err_info
;
1104 err_info
->fe
= SEC_RAS_FE_ENB_MSK
;
1105 err_info
->ce
= hisi_qm_get_hw_info(qm
, sec_basic_info
, SEC_QM_CE_MASK_CAP
, qm
->cap_ver
);
1106 err_info
->nfe
= hisi_qm_get_hw_info(qm
, sec_basic_info
, SEC_QM_NFE_MASK_CAP
, qm
->cap_ver
);
1107 err_info
->ecc_2bits_mask
= SEC_CORE_INT_STATUS_M_ECC
;
1108 err_info
->qm_shutdown_mask
= hisi_qm_get_hw_info(qm
, sec_basic_info
,
1109 SEC_QM_OOO_SHUTDOWN_MASK_CAP
, qm
->cap_ver
);
1110 err_info
->dev_shutdown_mask
= hisi_qm_get_hw_info(qm
, sec_basic_info
,
1111 SEC_OOO_SHUTDOWN_MASK_CAP
, qm
->cap_ver
);
1112 err_info
->qm_reset_mask
= hisi_qm_get_hw_info(qm
, sec_basic_info
,
1113 SEC_QM_RESET_MASK_CAP
, qm
->cap_ver
);
1114 err_info
->dev_reset_mask
= hisi_qm_get_hw_info(qm
, sec_basic_info
,
1115 SEC_RESET_MASK_CAP
, qm
->cap_ver
);
1116 err_info
->msi_wr_port
= BIT(0);
1117 err_info
->acpi_rst
= "SRST";
1120 static const struct hisi_qm_err_ini sec_err_ini
= {
1121 .hw_init
= sec_set_user_domain_and_cache
,
1122 .hw_err_enable
= sec_hw_error_enable
,
1123 .hw_err_disable
= sec_hw_error_disable
,
1124 .get_dev_hw_err_status
= sec_get_hw_err_status
,
1125 .clear_dev_hw_err_status
= sec_clear_hw_err_status
,
1126 .open_axi_master_ooo
= sec_open_axi_master_ooo
,
1127 .open_sva_prefetch
= sec_open_sva_prefetch
,
1128 .close_sva_prefetch
= sec_close_sva_prefetch
,
1129 .show_last_dfx_regs
= sec_show_last_dfx_regs
,
1130 .err_info_init
= sec_err_info_init
,
1131 .get_err_result
= sec_get_err_result
,
1134 static int sec_pf_probe_init(struct sec_dev
*sec
)
1136 struct hisi_qm
*qm
= &sec
->qm
;
1139 ret
= sec_set_user_domain_and_cache(qm
);
1143 sec_open_sva_prefetch(qm
);
1144 hisi_qm_dev_err_init(qm
);
1145 sec_debug_regs_clear(qm
);
1146 ret
= sec_show_last_regs_init(qm
);
1148 pci_err(qm
->pdev
, "Failed to init last word regs!\n");
1153 static int sec_pre_store_cap_reg(struct hisi_qm
*qm
)
1155 struct hisi_qm_cap_record
*sec_cap
;
1156 struct pci_dev
*pdev
= qm
->pdev
;
1159 size
= ARRAY_SIZE(sec_cap_query_info
);
1160 sec_cap
= devm_kzalloc(&pdev
->dev
, sizeof(*sec_cap
) * size
, GFP_KERNEL
);
1164 for (i
= 0; i
< size
; i
++) {
1165 sec_cap
[i
].type
= sec_cap_query_info
[i
].type
;
1166 sec_cap
[i
].name
= sec_cap_query_info
[i
].name
;
1167 sec_cap
[i
].cap_val
= hisi_qm_get_cap_value(qm
, sec_cap_query_info
,
1171 qm
->cap_tables
.dev_cap_table
= sec_cap
;
1172 qm
->cap_tables
.dev_cap_size
= size
;
1177 static int sec_qm_init(struct hisi_qm
*qm
, struct pci_dev
*pdev
)
1183 qm
->ver
= pdev
->revision
;
1184 qm
->mode
= uacce_mode
;
1185 qm
->sqe_size
= SEC_SQE_SIZE
;
1186 qm
->dev_name
= sec_name
;
1188 qm
->fun_type
= (pdev
->device
== PCI_DEVICE_ID_HUAWEI_SEC_PF
) ?
1189 QM_HW_PF
: QM_HW_VF
;
1190 if (qm
->fun_type
== QM_HW_PF
) {
1191 qm
->qp_base
= SEC_PF_DEF_Q_BASE
;
1192 qm
->qp_num
= pf_q_num
;
1193 qm
->debug
.curr_qm_qp_num
= pf_q_num
;
1194 qm
->qm_list
= &sec_devices
;
1195 qm
->err_ini
= &sec_err_ini
;
1197 set_bit(QM_MODULE_PARAM
, &qm
->misc_ctl
);
1198 } else if (qm
->fun_type
== QM_HW_VF
&& qm
->ver
== QM_HW_V1
) {
1200 * have no way to get qm configure in VM in v1 hardware,
1201 * so currently force PF to uses SEC_PF_DEF_Q_NUM, and force
1202 * to trigger only one VF in v1 hardware.
1203 * v2 hardware has no such problem.
1205 qm
->qp_base
= SEC_PF_DEF_Q_NUM
;
1206 qm
->qp_num
= SEC_QUEUE_NUM_V1
- SEC_PF_DEF_Q_NUM
;
1209 ret
= hisi_qm_init(qm
);
1211 pci_err(qm
->pdev
, "Failed to init sec qm configures!\n");
1215 /* Fetch and save the value of capability registers */
1216 ret
= sec_pre_store_cap_reg(qm
);
1218 pci_err(qm
->pdev
, "Failed to pre-store capability registers!\n");
1222 alg_msk
= sec_get_alg_bitmap(qm
, SEC_ALG_BITMAP_HIGH
, SEC_ALG_BITMAP_LOW
);
1223 ret
= hisi_qm_set_algs(qm
, alg_msk
, sec_dev_algs
, ARRAY_SIZE(sec_dev_algs
));
1225 pci_err(qm
->pdev
, "Failed to set sec algs!\n");
1232 static void sec_qm_uninit(struct hisi_qm
*qm
)
1237 static int sec_probe_init(struct sec_dev
*sec
)
1239 u32 type_rate
= SEC_SHAPER_TYPE_RATE
;
1240 struct hisi_qm
*qm
= &sec
->qm
;
1243 if (qm
->fun_type
== QM_HW_PF
) {
1244 ret
= sec_pf_probe_init(sec
);
1247 /* enable shaper type 0 */
1248 if (qm
->ver
>= QM_HW_V3
) {
1249 type_rate
|= QM_SHAPER_ENABLE
;
1250 qm
->type_rate
= type_rate
;
1257 static void sec_probe_uninit(struct hisi_qm
*qm
)
1259 if (qm
->fun_type
== QM_HW_VF
)
1262 sec_debug_regs_clear(qm
);
1263 sec_show_last_regs_uninit(qm
);
1264 sec_close_sva_prefetch(qm
);
1265 hisi_qm_dev_err_uninit(qm
);
1268 static void sec_iommu_used_check(struct sec_dev
*sec
)
1270 struct iommu_domain
*domain
;
1271 struct device
*dev
= &sec
->qm
.pdev
->dev
;
1273 domain
= iommu_get_domain_for_dev(dev
);
1275 /* Check if iommu is used */
1276 sec
->iommu_used
= false;
1278 if (domain
->type
& __IOMMU_DOMAIN_PAGING
)
1279 sec
->iommu_used
= true;
1280 dev_info(dev
, "SMMU Opened, the iommu type = %u\n",
1285 static int sec_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
1287 struct sec_dev
*sec
;
1291 sec
= devm_kzalloc(&pdev
->dev
, sizeof(*sec
), GFP_KERNEL
);
1296 ret
= sec_qm_init(qm
, pdev
);
1298 pci_err(pdev
, "Failed to init SEC QM (%d)!\n", ret
);
1302 sec
->ctx_q_num
= ctx_q_num
;
1303 sec_iommu_used_check(sec
);
1305 ret
= sec_probe_init(sec
);
1307 pci_err(pdev
, "Failed to probe!\n");
1311 ret
= hisi_qm_start(qm
);
1313 pci_err(pdev
, "Failed to start sec qm!\n");
1314 goto err_probe_uninit
;
1317 ret
= sec_debugfs_init(qm
);
1319 pci_warn(pdev
, "Failed to init debugfs!\n");
1321 hisi_qm_add_list(qm
, &sec_devices
);
1322 ret
= hisi_qm_alg_register(qm
, &sec_devices
, ctx_q_num
);
1324 pr_err("Failed to register driver to crypto.\n");
1325 goto err_qm_del_list
;
1329 ret
= uacce_register(qm
->uacce
);
1331 pci_err(pdev
, "failed to register uacce (%d)!\n", ret
);
1332 goto err_alg_unregister
;
1336 if (qm
->fun_type
== QM_HW_PF
&& vfs_num
) {
1337 ret
= hisi_qm_sriov_enable(pdev
, vfs_num
);
1339 goto err_alg_unregister
;
1342 hisi_qm_pm_init(qm
);
1347 hisi_qm_alg_unregister(qm
, &sec_devices
, ctx_q_num
);
1349 hisi_qm_del_list(qm
, &sec_devices
);
1350 sec_debugfs_exit(qm
);
1351 hisi_qm_stop(qm
, QM_NORMAL
);
1353 sec_probe_uninit(qm
);
1359 static void sec_remove(struct pci_dev
*pdev
)
1361 struct hisi_qm
*qm
= pci_get_drvdata(pdev
);
1363 hisi_qm_pm_uninit(qm
);
1364 hisi_qm_wait_task_finish(qm
, &sec_devices
);
1365 hisi_qm_alg_unregister(qm
, &sec_devices
, ctx_q_num
);
1366 hisi_qm_del_list(qm
, &sec_devices
);
1368 if (qm
->fun_type
== QM_HW_PF
&& qm
->vfs_num
)
1369 hisi_qm_sriov_disable(pdev
, true);
1371 sec_debugfs_exit(qm
);
1373 (void)hisi_qm_stop(qm
, QM_NORMAL
);
1374 sec_probe_uninit(qm
);
1379 static const struct dev_pm_ops sec_pm_ops
= {
1380 SET_RUNTIME_PM_OPS(hisi_qm_suspend
, hisi_qm_resume
, NULL
)
1383 static const struct pci_error_handlers sec_err_handler
= {
1384 .error_detected
= hisi_qm_dev_err_detected
,
1385 .slot_reset
= hisi_qm_dev_slot_reset
,
1386 .reset_prepare
= hisi_qm_reset_prepare
,
1387 .reset_done
= hisi_qm_reset_done
,
1390 static struct pci_driver sec_pci_driver
= {
1391 .name
= "hisi_sec2",
1392 .id_table
= sec_dev_ids
,
1394 .remove
= sec_remove
,
1395 .err_handler
= &sec_err_handler
,
1396 .sriov_configure
= IS_ENABLED(CONFIG_PCI_IOV
) ?
1397 hisi_qm_sriov_configure
: NULL
,
1398 .shutdown
= hisi_qm_dev_shutdown
,
1399 .driver
.pm
= &sec_pm_ops
,
1402 struct pci_driver
*hisi_sec_get_pf_driver(void)
1404 return &sec_pci_driver
;
1406 EXPORT_SYMBOL_GPL(hisi_sec_get_pf_driver
);
1408 static void sec_register_debugfs(void)
1410 if (!debugfs_initialized())
1413 sec_debugfs_root
= debugfs_create_dir("hisi_sec2", NULL
);
1416 static void sec_unregister_debugfs(void)
1418 debugfs_remove_recursive(sec_debugfs_root
);
1421 static int __init
sec_init(void)
1425 hisi_qm_init_list(&sec_devices
);
1426 sec_register_debugfs();
1428 ret
= pci_register_driver(&sec_pci_driver
);
1430 sec_unregister_debugfs();
1431 pr_err("Failed to register pci driver.\n");
1438 static void __exit
sec_exit(void)
1440 pci_unregister_driver(&sec_pci_driver
);
1441 sec_unregister_debugfs();
1444 module_init(sec_init
);
1445 module_exit(sec_exit
);
1447 MODULE_LICENSE("GPL v2");
1448 MODULE_AUTHOR("Zaibo Xu <xuzaibo@huawei.com>");
1449 MODULE_AUTHOR("Longfang Liu <liulongfang@huawei.com>");
1450 MODULE_AUTHOR("Kai Ye <yekai13@huawei.com>");
1451 MODULE_AUTHOR("Wei Zhang <zhangwei375@huawei.com>");
1452 MODULE_DESCRIPTION("Driver for HiSilicon SEC accelerator");