1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018-2019 HiSilicon Limited. */
3 #include <linux/acpi.h>
5 #include <linux/bitops.h>
6 #include <linux/debugfs.h>
7 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/topology.h>
15 #define HPRE_VF_NUM 63
16 #define HPRE_QUEUE_NUM_V2 1024
17 #define HPRE_QM_ABNML_INT_MASK 0x100004
18 #define HPRE_CTRL_CNT_CLR_CE_BIT BIT(0)
19 #define HPRE_COMM_CNT_CLR_CE 0x0
20 #define HPRE_CTRL_CNT_CLR_CE 0x301000
21 #define HPRE_FSM_MAX_CNT 0x301008
22 #define HPRE_VFG_AXQOS 0x30100c
23 #define HPRE_VFG_AXCACHE 0x301010
24 #define HPRE_RDCHN_INI_CFG 0x301014
25 #define HPRE_AWUSR_FP_CFG 0x301018
26 #define HPRE_BD_ENDIAN 0x301020
27 #define HPRE_ECC_BYPASS 0x301024
28 #define HPRE_RAS_WIDTH_CFG 0x301028
29 #define HPRE_POISON_BYPASS 0x30102c
30 #define HPRE_BD_ARUSR_CFG 0x301030
31 #define HPRE_BD_AWUSR_CFG 0x301034
32 #define HPRE_TYPES_ENB 0x301038
33 #define HPRE_DATA_RUSER_CFG 0x30103c
34 #define HPRE_DATA_WUSER_CFG 0x301040
35 #define HPRE_INT_MASK 0x301400
36 #define HPRE_INT_STATUS 0x301800
37 #define HPRE_CORE_INT_ENABLE 0
38 #define HPRE_CORE_INT_DISABLE 0x003fffff
39 #define HPRE_RAS_ECC_1BIT_TH 0x30140c
40 #define HPRE_RDCHN_INI_ST 0x301a00
41 #define HPRE_CLSTR_BASE 0x302000
42 #define HPRE_CORE_EN_OFFSET 0x04
43 #define HPRE_CORE_INI_CFG_OFFSET 0x20
44 #define HPRE_CORE_INI_STATUS_OFFSET 0x80
45 #define HPRE_CORE_HTBT_WARN_OFFSET 0x8c
46 #define HPRE_CORE_IS_SCHD_OFFSET 0x90
48 #define HPRE_RAS_CE_ENB 0x301410
49 #define HPRE_HAC_RAS_CE_ENABLE 0x3f
50 #define HPRE_RAS_NFE_ENB 0x301414
51 #define HPRE_HAC_RAS_NFE_ENABLE 0x3fffc0
52 #define HPRE_RAS_FE_ENB 0x301418
53 #define HPRE_HAC_RAS_FE_ENABLE 0
55 #define HPRE_CORE_ENB (HPRE_CLSTR_BASE + HPRE_CORE_EN_OFFSET)
56 #define HPRE_CORE_INI_CFG (HPRE_CLSTR_BASE + HPRE_CORE_INI_CFG_OFFSET)
57 #define HPRE_CORE_INI_STATUS (HPRE_CLSTR_BASE + HPRE_CORE_INI_STATUS_OFFSET)
58 #define HPRE_HAC_ECC1_CNT 0x301a04
59 #define HPRE_HAC_ECC2_CNT 0x301a08
60 #define HPRE_HAC_INT_STATUS 0x301800
61 #define HPRE_HAC_SOURCE_INT 0x301600
62 #define MASTER_GLOBAL_CTRL_SHUTDOWN 1
63 #define MASTER_TRANS_RETURN_RW 3
64 #define HPRE_MASTER_TRANS_RETURN 0x300150
65 #define HPRE_MASTER_GLOBAL_CTRL 0x300000
66 #define HPRE_CLSTR_ADDR_INTRVL 0x1000
67 #define HPRE_CLUSTER_INQURY 0x100
68 #define HPRE_CLSTR_ADDR_INQRY_RSLT 0x104
69 #define HPRE_TIMEOUT_ABNML_BIT 6
70 #define HPRE_PASID_EN_BIT 9
71 #define HPRE_REG_RD_INTVRL_US 10
72 #define HPRE_REG_RD_TMOUT_US 1000
73 #define HPRE_DBGFS_VAL_MAX_LEN 20
74 #define HPRE_PCI_DEVICE_ID 0xa258
75 #define HPRE_PCI_VF_DEVICE_ID 0xa259
76 #define HPRE_ADDR(qm, offset) (qm->io_base + (offset))
77 #define HPRE_QM_USR_CFG_MASK 0xfffffffe
78 #define HPRE_QM_AXI_CFG_MASK 0xffff
79 #define HPRE_QM_VFG_AX_MASK 0xff
80 #define HPRE_BD_USR_MASK 0x3
81 #define HPRE_CLUSTER_CORE_MASK 0xf
83 #define HPRE_VIA_MSI_DSM 1
85 static LIST_HEAD(hpre_list);
86 static DEFINE_MUTEX(hpre_list_lock);
87 static const char hpre_name[] = "hisi_hpre";
88 static struct dentry *hpre_debugfs_root;
89 static const struct pci_device_id hpre_dev_ids[] = {
90 { PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HPRE_PCI_DEVICE_ID) },
91 { PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HPRE_PCI_VF_DEVICE_ID) },
95 MODULE_DEVICE_TABLE(pci, hpre_dev_ids);
97 struct hpre_hw_error {
102 static const char * const hpre_debug_file_name[] = {
103 [HPRE_CURRENT_QM] = "current_qm",
104 [HPRE_CLEAR_ENABLE] = "rdclr_en",
105 [HPRE_CLUSTER_CTRL] = "cluster_ctrl",
108 static const struct hpre_hw_error hpre_hw_errors[] = {
109 { .int_msk = BIT(0), .msg = "hpre_ecc_1bitt_err" },
110 { .int_msk = BIT(1), .msg = "hpre_ecc_2bit_err" },
111 { .int_msk = BIT(2), .msg = "hpre_data_wr_err" },
112 { .int_msk = BIT(3), .msg = "hpre_data_rd_err" },
113 { .int_msk = BIT(4), .msg = "hpre_bd_rd_err" },
114 { .int_msk = BIT(5), .msg = "hpre_ooo_2bit_ecc_err" },
115 { .int_msk = BIT(6), .msg = "hpre_cltr1_htbt_tm_out_err" },
116 { .int_msk = BIT(7), .msg = "hpre_cltr2_htbt_tm_out_err" },
117 { .int_msk = BIT(8), .msg = "hpre_cltr3_htbt_tm_out_err" },
118 { .int_msk = BIT(9), .msg = "hpre_cltr4_htbt_tm_out_err" },
119 { .int_msk = GENMASK(15, 10), .msg = "hpre_ooo_rdrsp_err" },
120 { .int_msk = GENMASK(21, 16), .msg = "hpre_ooo_wrrsp_err" },
124 static const u64 hpre_cluster_offsets[] = {
126 HPRE_CLSTR_BASE + HPRE_CLUSTER0 * HPRE_CLSTR_ADDR_INTRVL,
128 HPRE_CLSTR_BASE + HPRE_CLUSTER1 * HPRE_CLSTR_ADDR_INTRVL,
130 HPRE_CLSTR_BASE + HPRE_CLUSTER2 * HPRE_CLSTR_ADDR_INTRVL,
132 HPRE_CLSTR_BASE + HPRE_CLUSTER3 * HPRE_CLSTR_ADDR_INTRVL,
135 static struct debugfs_reg32 hpre_cluster_dfx_regs[] = {
136 {"CORES_EN_STATUS ", HPRE_CORE_EN_OFFSET},
137 {"CORES_INI_CFG ", HPRE_CORE_INI_CFG_OFFSET},
138 {"CORES_INI_STATUS ", HPRE_CORE_INI_STATUS_OFFSET},
139 {"CORES_HTBT_WARN ", HPRE_CORE_HTBT_WARN_OFFSET},
140 {"CORES_IS_SCHD ", HPRE_CORE_IS_SCHD_OFFSET},
143 static struct debugfs_reg32 hpre_com_dfx_regs[] = {
144 {"READ_CLR_EN ", HPRE_CTRL_CNT_CLR_CE},
145 {"AXQOS ", HPRE_VFG_AXQOS},
146 {"AWUSR_CFG ", HPRE_AWUSR_FP_CFG},
147 {"QM_ARUSR_MCFG1 ", QM_ARUSER_M_CFG_1},
148 {"QM_AWUSR_MCFG1 ", QM_AWUSER_M_CFG_1},
149 {"BD_ENDIAN ", HPRE_BD_ENDIAN},
150 {"ECC_CHECK_CTRL ", HPRE_ECC_BYPASS},
151 {"RAS_INT_WIDTH ", HPRE_RAS_WIDTH_CFG},
152 {"POISON_BYPASS ", HPRE_POISON_BYPASS},
153 {"BD_ARUSER ", HPRE_BD_ARUSR_CFG},
154 {"BD_AWUSER ", HPRE_BD_AWUSR_CFG},
155 {"DATA_ARUSER ", HPRE_DATA_RUSER_CFG},
156 {"DATA_AWUSER ", HPRE_DATA_WUSER_CFG},
157 {"INT_STATUS ", HPRE_INT_STATUS},
160 static int hpre_pf_q_num_set(const char *val, const struct kernel_param *kp)
162 struct pci_dev *pdev;
170 pdev = pci_get_device(PCI_VENDOR_ID_HUAWEI, HPRE_PCI_DEVICE_ID, NULL);
172 q_num = HPRE_QUEUE_NUM_V2;
173 pr_info("No device found currently, suppose queue number is %d\n",
176 rev_id = pdev->revision;
177 if (rev_id != QM_HW_V2)
180 q_num = HPRE_QUEUE_NUM_V2;
183 ret = kstrtou32(val, 10, &n);
184 if (ret != 0 || n == 0 || n > q_num)
187 return param_set_int(val, kp);
190 static const struct kernel_param_ops hpre_pf_q_num_ops = {
191 .set = hpre_pf_q_num_set,
192 .get = param_get_int,
195 static u32 hpre_pf_q_num = HPRE_PF_DEF_Q_NUM;
196 module_param_cb(hpre_pf_q_num, &hpre_pf_q_num_ops, &hpre_pf_q_num, 0444);
197 MODULE_PARM_DESC(hpre_pf_q_num, "Number of queues in PF of CS(1-1024)");
199 static inline void hpre_add_to_list(struct hpre *hpre)
201 mutex_lock(&hpre_list_lock);
202 list_add_tail(&hpre->list, &hpre_list);
203 mutex_unlock(&hpre_list_lock);
206 static inline void hpre_remove_from_list(struct hpre *hpre)
208 mutex_lock(&hpre_list_lock);
209 list_del(&hpre->list);
210 mutex_unlock(&hpre_list_lock);
213 struct hpre *hpre_find_device(int node)
215 struct hpre *hpre, *ret = NULL;
216 int min_distance = INT_MAX;
220 mutex_lock(&hpre_list_lock);
221 list_for_each_entry(hpre, &hpre_list, list) {
222 dev = &hpre->qm.pdev->dev;
224 dev_node = dev->numa_node;
228 if (node_distance(dev_node, node) < min_distance) {
230 min_distance = node_distance(dev_node, node);
233 mutex_unlock(&hpre_list_lock);
238 static int hpre_cfg_by_dsm(struct hisi_qm *qm)
240 struct device *dev = &qm->pdev->dev;
241 union acpi_object *obj;
244 if (guid_parse("b06b81ab-0134-4a45-9b0c-483447b95fa7", &guid)) {
245 dev_err(dev, "Hpre GUID failed\n");
249 /* Switch over to MSI handling due to non-standard PCI implementation */
250 obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &guid,
251 0, HPRE_VIA_MSI_DSM, NULL);
253 dev_err(dev, "ACPI handle failed!\n");
262 static int hpre_set_user_domain_and_cache(struct hpre *hpre)
264 struct hisi_qm *qm = &hpre->qm;
265 struct device *dev = &qm->pdev->dev;
266 unsigned long offset;
270 writel(HPRE_QM_USR_CFG_MASK, HPRE_ADDR(qm, QM_ARUSER_M_CFG_ENABLE));
271 writel(HPRE_QM_USR_CFG_MASK, HPRE_ADDR(qm, QM_AWUSER_M_CFG_ENABLE));
272 writel_relaxed(HPRE_QM_AXI_CFG_MASK, HPRE_ADDR(qm, QM_AXI_M_CFG));
274 /* disable FLR triggered by BME(bus master enable) */
275 writel(PEH_AXUSER_CFG, HPRE_ADDR(qm, QM_PEH_AXUSER_CFG));
276 writel(PEH_AXUSER_CFG_ENABLE, HPRE_ADDR(qm, QM_PEH_AXUSER_CFG_ENABLE));
278 /* HPRE need more time, we close this interrupt */
279 val = readl_relaxed(HPRE_ADDR(qm, HPRE_QM_ABNML_INT_MASK));
280 val |= BIT(HPRE_TIMEOUT_ABNML_BIT);
281 writel_relaxed(val, HPRE_ADDR(qm, HPRE_QM_ABNML_INT_MASK));
283 writel(0x1, HPRE_ADDR(qm, HPRE_TYPES_ENB));
284 writel(HPRE_QM_VFG_AX_MASK, HPRE_ADDR(qm, HPRE_VFG_AXCACHE));
285 writel(0x0, HPRE_ADDR(qm, HPRE_BD_ENDIAN));
286 writel(0x0, HPRE_ADDR(qm, HPRE_INT_MASK));
287 writel(0x0, HPRE_ADDR(qm, HPRE_RAS_ECC_1BIT_TH));
288 writel(0x0, HPRE_ADDR(qm, HPRE_POISON_BYPASS));
289 writel(0x0, HPRE_ADDR(qm, HPRE_COMM_CNT_CLR_CE));
290 writel(0x0, HPRE_ADDR(qm, HPRE_ECC_BYPASS));
292 writel(HPRE_BD_USR_MASK, HPRE_ADDR(qm, HPRE_BD_ARUSR_CFG));
293 writel(HPRE_BD_USR_MASK, HPRE_ADDR(qm, HPRE_BD_AWUSR_CFG));
294 writel(0x1, HPRE_ADDR(qm, HPRE_RDCHN_INI_CFG));
295 ret = readl_relaxed_poll_timeout(HPRE_ADDR(qm, HPRE_RDCHN_INI_ST), val,
297 HPRE_REG_RD_INTVRL_US,
298 HPRE_REG_RD_TMOUT_US);
300 dev_err(dev, "read rd channel timeout fail!\n");
304 for (i = 0; i < HPRE_CLUSTERS_NUM; i++) {
305 offset = i * HPRE_CLSTR_ADDR_INTRVL;
307 /* clusters initiating */
308 writel(HPRE_CLUSTER_CORE_MASK,
309 HPRE_ADDR(qm, offset + HPRE_CORE_ENB));
310 writel(0x1, HPRE_ADDR(qm, offset + HPRE_CORE_INI_CFG));
311 ret = readl_relaxed_poll_timeout(HPRE_ADDR(qm, offset +
312 HPRE_CORE_INI_STATUS), val,
313 ((val & HPRE_CLUSTER_CORE_MASK) ==
314 HPRE_CLUSTER_CORE_MASK),
315 HPRE_REG_RD_INTVRL_US,
316 HPRE_REG_RD_TMOUT_US);
319 "cluster %d int st status timeout!\n", i);
324 ret = hpre_cfg_by_dsm(qm);
326 dev_err(dev, "acpi_evaluate_dsm err.\n");
331 static void hpre_cnt_regs_clear(struct hisi_qm *qm)
333 unsigned long offset;
336 /* clear current_qm */
337 writel(0x0, qm->io_base + QM_DFX_MB_CNT_VF);
338 writel(0x0, qm->io_base + QM_DFX_DB_CNT_VF);
340 /* clear clusterX/cluster_ctrl */
341 for (i = 0; i < HPRE_CLUSTERS_NUM; i++) {
342 offset = HPRE_CLSTR_BASE + i * HPRE_CLSTR_ADDR_INTRVL;
343 writel(0x0, qm->io_base + offset + HPRE_CLUSTER_INQURY);
347 writel(0x0, qm->io_base + HPRE_CTRL_CNT_CLR_CE);
349 hisi_qm_debug_regs_clear(qm);
352 static void hpre_hw_error_disable(struct hpre *hpre)
354 struct hisi_qm *qm = &hpre->qm;
356 /* disable hpre hw error interrupts */
357 writel(HPRE_CORE_INT_DISABLE, qm->io_base + HPRE_INT_MASK);
360 static void hpre_hw_error_enable(struct hpre *hpre)
362 struct hisi_qm *qm = &hpre->qm;
364 /* enable hpre hw error interrupts */
365 writel(HPRE_CORE_INT_ENABLE, qm->io_base + HPRE_INT_MASK);
366 writel(HPRE_HAC_RAS_CE_ENABLE, qm->io_base + HPRE_RAS_CE_ENB);
367 writel(HPRE_HAC_RAS_NFE_ENABLE, qm->io_base + HPRE_RAS_NFE_ENB);
368 writel(HPRE_HAC_RAS_FE_ENABLE, qm->io_base + HPRE_RAS_FE_ENB);
371 static inline struct hisi_qm *hpre_file_to_qm(struct hpre_debugfs_file *file)
373 struct hpre *hpre = container_of(file->debug, struct hpre, debug);
378 static u32 hpre_current_qm_read(struct hpre_debugfs_file *file)
380 struct hisi_qm *qm = hpre_file_to_qm(file);
382 return readl(qm->io_base + QM_DFX_MB_CNT_VF);
385 static int hpre_current_qm_write(struct hpre_debugfs_file *file, u32 val)
387 struct hisi_qm *qm = hpre_file_to_qm(file);
388 struct hpre_debug *debug = file->debug;
389 struct hpre *hpre = container_of(debug, struct hpre, debug);
390 u32 num_vfs = hpre->num_vfs;
397 /* According PF or VF Dev ID to calculation curr_qm_qp_num and store */
399 qm->debug.curr_qm_qp_num = qm->qp_num;
401 vfq_num = (qm->ctrl_qp_num - qm->qp_num) / num_vfs;
402 if (val == num_vfs) {
403 qm->debug.curr_qm_qp_num =
404 qm->ctrl_qp_num - qm->qp_num - (num_vfs - 1) * vfq_num;
406 qm->debug.curr_qm_qp_num = vfq_num;
410 writel(val, qm->io_base + QM_DFX_MB_CNT_VF);
411 writel(val, qm->io_base + QM_DFX_DB_CNT_VF);
414 (readl(qm->io_base + QM_DFX_SQE_CNT_VF_SQN) & CURRENT_Q_MASK);
415 writel(tmp, qm->io_base + QM_DFX_SQE_CNT_VF_SQN);
418 (readl(qm->io_base + QM_DFX_CQE_CNT_VF_CQN) & CURRENT_Q_MASK);
419 writel(tmp, qm->io_base + QM_DFX_CQE_CNT_VF_CQN);
424 static u32 hpre_clear_enable_read(struct hpre_debugfs_file *file)
426 struct hisi_qm *qm = hpre_file_to_qm(file);
428 return readl(qm->io_base + HPRE_CTRL_CNT_CLR_CE) &
429 HPRE_CTRL_CNT_CLR_CE_BIT;
432 static int hpre_clear_enable_write(struct hpre_debugfs_file *file, u32 val)
434 struct hisi_qm *qm = hpre_file_to_qm(file);
437 if (val != 1 && val != 0)
440 tmp = (readl(qm->io_base + HPRE_CTRL_CNT_CLR_CE) &
441 ~HPRE_CTRL_CNT_CLR_CE_BIT) | val;
442 writel(tmp, qm->io_base + HPRE_CTRL_CNT_CLR_CE);
447 static u32 hpre_cluster_inqry_read(struct hpre_debugfs_file *file)
449 struct hisi_qm *qm = hpre_file_to_qm(file);
450 int cluster_index = file->index - HPRE_CLUSTER_CTRL;
451 unsigned long offset = HPRE_CLSTR_BASE +
452 cluster_index * HPRE_CLSTR_ADDR_INTRVL;
454 return readl(qm->io_base + offset + HPRE_CLSTR_ADDR_INQRY_RSLT);
457 static int hpre_cluster_inqry_write(struct hpre_debugfs_file *file, u32 val)
459 struct hisi_qm *qm = hpre_file_to_qm(file);
460 int cluster_index = file->index - HPRE_CLUSTER_CTRL;
461 unsigned long offset = HPRE_CLSTR_BASE + cluster_index *
462 HPRE_CLSTR_ADDR_INTRVL;
464 writel(val, qm->io_base + offset + HPRE_CLUSTER_INQURY);
469 static ssize_t hpre_ctrl_debug_read(struct file *filp, char __user *buf,
470 size_t count, loff_t *pos)
472 struct hpre_debugfs_file *file = filp->private_data;
473 char tbuf[HPRE_DBGFS_VAL_MAX_LEN];
477 spin_lock_irq(&file->lock);
478 switch (file->type) {
479 case HPRE_CURRENT_QM:
480 val = hpre_current_qm_read(file);
482 case HPRE_CLEAR_ENABLE:
483 val = hpre_clear_enable_read(file);
485 case HPRE_CLUSTER_CTRL:
486 val = hpre_cluster_inqry_read(file);
489 spin_unlock_irq(&file->lock);
492 spin_unlock_irq(&file->lock);
493 ret = sprintf(tbuf, "%u\n", val);
494 return simple_read_from_buffer(buf, count, pos, tbuf, ret);
497 static ssize_t hpre_ctrl_debug_write(struct file *filp, const char __user *buf,
498 size_t count, loff_t *pos)
500 struct hpre_debugfs_file *file = filp->private_data;
501 char tbuf[HPRE_DBGFS_VAL_MAX_LEN];
508 if (count >= HPRE_DBGFS_VAL_MAX_LEN)
511 len = simple_write_to_buffer(tbuf, HPRE_DBGFS_VAL_MAX_LEN - 1,
517 if (kstrtoul(tbuf, 0, &val))
520 spin_lock_irq(&file->lock);
521 switch (file->type) {
522 case HPRE_CURRENT_QM:
523 ret = hpre_current_qm_write(file, val);
527 case HPRE_CLEAR_ENABLE:
528 ret = hpre_clear_enable_write(file, val);
532 case HPRE_CLUSTER_CTRL:
533 ret = hpre_cluster_inqry_write(file, val);
541 spin_unlock_irq(&file->lock);
546 spin_unlock_irq(&file->lock);
550 static const struct file_operations hpre_ctrl_debug_fops = {
551 .owner = THIS_MODULE,
553 .read = hpre_ctrl_debug_read,
554 .write = hpre_ctrl_debug_write,
557 static int hpre_create_debugfs_file(struct hpre_debug *dbg, struct dentry *dir,
558 enum hpre_ctrl_dbgfs_file type, int indx)
560 struct dentry *tmp, *file_dir;
565 file_dir = dbg->debug_root;
567 if (type >= HPRE_DEBUG_FILE_NUM)
570 spin_lock_init(&dbg->files[indx].lock);
571 dbg->files[indx].debug = dbg;
572 dbg->files[indx].type = type;
573 dbg->files[indx].index = indx;
574 tmp = debugfs_create_file(hpre_debug_file_name[type], 0600, file_dir,
575 dbg->files + indx, &hpre_ctrl_debug_fops);
582 static int hpre_pf_comm_regs_debugfs_init(struct hpre_debug *debug)
584 struct hpre *hpre = container_of(debug, struct hpre, debug);
585 struct hisi_qm *qm = &hpre->qm;
586 struct device *dev = &qm->pdev->dev;
587 struct debugfs_regset32 *regset;
590 regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
594 regset->regs = hpre_com_dfx_regs;
595 regset->nregs = ARRAY_SIZE(hpre_com_dfx_regs);
596 regset->base = qm->io_base;
598 tmp = debugfs_create_regset32("regs", 0444, debug->debug_root, regset);
605 static int hpre_cluster_debugfs_init(struct hpre_debug *debug)
607 struct hpre *hpre = container_of(debug, struct hpre, debug);
608 struct hisi_qm *qm = &hpre->qm;
609 struct device *dev = &qm->pdev->dev;
610 char buf[HPRE_DBGFS_VAL_MAX_LEN];
611 struct debugfs_regset32 *regset;
612 struct dentry *tmp_d, *tmp;
615 for (i = 0; i < HPRE_CLUSTERS_NUM; i++) {
616 sprintf(buf, "cluster%d", i);
618 tmp_d = debugfs_create_dir(buf, debug->debug_root);
622 regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
626 regset->regs = hpre_cluster_dfx_regs;
627 regset->nregs = ARRAY_SIZE(hpre_cluster_dfx_regs);
628 regset->base = qm->io_base + hpre_cluster_offsets[i];
630 tmp = debugfs_create_regset32("regs", 0444, tmp_d, regset);
633 ret = hpre_create_debugfs_file(debug, tmp_d, HPRE_CLUSTER_CTRL,
634 i + HPRE_CLUSTER_CTRL);
642 static int hpre_ctrl_debug_init(struct hpre_debug *debug)
646 ret = hpre_create_debugfs_file(debug, NULL, HPRE_CURRENT_QM,
651 ret = hpre_create_debugfs_file(debug, NULL, HPRE_CLEAR_ENABLE,
656 ret = hpre_pf_comm_regs_debugfs_init(debug);
660 return hpre_cluster_debugfs_init(debug);
663 static int hpre_debugfs_init(struct hpre *hpre)
665 struct hisi_qm *qm = &hpre->qm;
666 struct device *dev = &qm->pdev->dev;
670 dir = debugfs_create_dir(dev_name(dev), hpre_debugfs_root);
674 qm->debug.debug_root = dir;
676 ret = hisi_qm_debug_init(qm);
678 goto failed_to_create;
680 if (qm->pdev->device == HPRE_PCI_DEVICE_ID) {
681 hpre->debug.debug_root = dir;
682 ret = hpre_ctrl_debug_init(&hpre->debug);
684 goto failed_to_create;
689 debugfs_remove_recursive(qm->debug.debug_root);
693 static void hpre_debugfs_exit(struct hpre *hpre)
695 struct hisi_qm *qm = &hpre->qm;
697 debugfs_remove_recursive(qm->debug.debug_root);
700 static int hpre_qm_pre_init(struct hisi_qm *qm, struct pci_dev *pdev)
702 enum qm_hw_ver rev_id;
704 rev_id = hisi_qm_get_hw_version(pdev);
708 if (rev_id == QM_HW_V1) {
709 pci_warn(pdev, "HPRE version 1 is not supported!\n");
715 qm->sqe_size = HPRE_SQE_SIZE;
716 qm->dev_name = hpre_name;
717 qm->fun_type = (pdev->device == HPRE_PCI_DEVICE_ID) ?
719 if (pdev->is_physfn) {
720 qm->qp_base = HPRE_PF_DEF_Q_BASE;
721 qm->qp_num = hpre_pf_q_num;
723 qm->use_dma_api = true;
728 static void hpre_hw_err_init(struct hpre *hpre)
730 hisi_qm_hw_error_init(&hpre->qm, QM_BASE_CE, QM_BASE_NFE,
731 0, QM_DB_RANDOM_INVALID);
732 hpre_hw_error_enable(hpre);
735 static int hpre_pf_probe_init(struct hpre *hpre)
737 struct hisi_qm *qm = &hpre->qm;
740 qm->ctrl_qp_num = HPRE_QUEUE_NUM_V2;
742 ret = hpre_set_user_domain_and_cache(hpre);
746 hpre_hw_err_init(hpre);
751 static int hpre_probe(struct pci_dev *pdev, const struct pci_device_id *id)
757 hpre = devm_kzalloc(&pdev->dev, sizeof(*hpre), GFP_KERNEL);
761 pci_set_drvdata(pdev, hpre);
764 ret = hpre_qm_pre_init(qm, pdev);
768 ret = hisi_qm_init(qm);
772 if (pdev->is_physfn) {
773 ret = hpre_pf_probe_init(hpre);
775 goto err_with_qm_init;
776 } else if (qm->fun_type == QM_HW_VF && qm->ver == QM_HW_V2) {
777 /* v2 starts to support get vft by mailbox */
778 ret = hisi_qm_get_vft(qm, &qm->qp_base, &qm->qp_num);
780 goto err_with_qm_init;
783 ret = hisi_qm_start(qm);
785 goto err_with_err_init;
787 ret = hpre_debugfs_init(hpre);
789 dev_warn(&pdev->dev, "init debugfs fail!\n");
791 hpre_add_to_list(hpre);
793 ret = hpre_algs_register();
795 hpre_remove_from_list(hpre);
796 pci_err(pdev, "fail to register algs to crypto!\n");
797 goto err_with_qm_start;
806 hpre_hw_error_disable(hpre);
814 static int hpre_vf_q_assign(struct hpre *hpre, int num_vfs)
816 struct hisi_qm *qm = &hpre->qm;
817 u32 qp_num = qm->qp_num;
818 int q_num, remain_q_num, i;
825 remain_q_num = qm->ctrl_qp_num - qp_num;
827 /* If remaining queues are not enough, return error. */
828 if (remain_q_num < num_vfs)
831 q_num = remain_q_num / num_vfs;
832 for (i = 1; i <= num_vfs; i++) {
834 q_num += remain_q_num % num_vfs;
835 ret = hisi_qm_set_vft(qm, i, q_base, (u32)q_num);
844 static int hpre_clear_vft_config(struct hpre *hpre)
846 struct hisi_qm *qm = &hpre->qm;
847 u32 num_vfs = hpre->num_vfs;
851 for (i = 1; i <= num_vfs; i++) {
852 ret = hisi_qm_set_vft(qm, i, 0, 0);
861 static int hpre_sriov_enable(struct pci_dev *pdev, int max_vfs)
863 struct hpre *hpre = pci_get_drvdata(pdev);
864 int pre_existing_vfs, num_vfs, ret;
866 pre_existing_vfs = pci_num_vf(pdev);
867 if (pre_existing_vfs) {
869 "Can't enable VF. Please disable pre-enabled VFs!\n");
873 num_vfs = min_t(int, max_vfs, HPRE_VF_NUM);
874 ret = hpre_vf_q_assign(hpre, num_vfs);
876 pci_err(pdev, "Can't assign queues for VF!\n");
880 hpre->num_vfs = num_vfs;
882 ret = pci_enable_sriov(pdev, num_vfs);
884 pci_err(pdev, "Can't enable VF!\n");
885 hpre_clear_vft_config(hpre);
892 static int hpre_sriov_disable(struct pci_dev *pdev)
894 struct hpre *hpre = pci_get_drvdata(pdev);
896 if (pci_vfs_assigned(pdev)) {
897 pci_err(pdev, "Failed to disable VFs while VFs are assigned!\n");
901 /* remove in hpre_pci_driver will be called to free VF resources */
902 pci_disable_sriov(pdev);
904 return hpre_clear_vft_config(hpre);
907 static int hpre_sriov_configure(struct pci_dev *pdev, int num_vfs)
910 return hpre_sriov_enable(pdev, num_vfs);
912 return hpre_sriov_disable(pdev);
915 static void hpre_remove(struct pci_dev *pdev)
917 struct hpre *hpre = pci_get_drvdata(pdev);
918 struct hisi_qm *qm = &hpre->qm;
921 hpre_algs_unregister();
922 hpre_remove_from_list(hpre);
923 if (qm->fun_type == QM_HW_PF && hpre->num_vfs != 0) {
924 ret = hpre_sriov_disable(pdev);
926 pci_err(pdev, "Disable SRIOV fail!\n");
930 if (qm->fun_type == QM_HW_PF) {
931 hpre_cnt_regs_clear(qm);
932 qm->debug.curr_qm_qp_num = 0;
935 hpre_debugfs_exit(hpre);
937 if (qm->fun_type == QM_HW_PF)
938 hpre_hw_error_disable(hpre);
942 static void hpre_log_hw_error(struct hpre *hpre, u32 err_sts)
944 const struct hpre_hw_error *err = hpre_hw_errors;
945 struct device *dev = &hpre->qm.pdev->dev;
948 if (err->int_msk & err_sts)
949 dev_warn(dev, "%s [error status=0x%x] found\n",
950 err->msg, err->int_msk);
955 static pci_ers_result_t hpre_hw_error_handle(struct hpre *hpre)
960 err_sts = readl(hpre->qm.io_base + HPRE_HAC_INT_STATUS);
962 hpre_log_hw_error(hpre, err_sts);
964 /* clear error interrupts */
965 writel(err_sts, hpre->qm.io_base + HPRE_HAC_SOURCE_INT);
966 return PCI_ERS_RESULT_NEED_RESET;
969 return PCI_ERS_RESULT_RECOVERED;
972 static pci_ers_result_t hpre_process_hw_error(struct pci_dev *pdev)
974 struct hpre *hpre = pci_get_drvdata(pdev);
975 pci_ers_result_t qm_ret, hpre_ret;
978 qm_ret = hisi_qm_hw_error_handle(&hpre->qm);
981 hpre_ret = hpre_hw_error_handle(hpre);
983 return (qm_ret == PCI_ERS_RESULT_NEED_RESET ||
984 hpre_ret == PCI_ERS_RESULT_NEED_RESET) ?
985 PCI_ERS_RESULT_NEED_RESET : PCI_ERS_RESULT_RECOVERED;
988 static pci_ers_result_t hpre_error_detected(struct pci_dev *pdev,
989 pci_channel_state_t state)
991 pci_info(pdev, "PCI error detected, state(=%d)!!\n", state);
992 if (state == pci_channel_io_perm_failure)
993 return PCI_ERS_RESULT_DISCONNECT;
995 return hpre_process_hw_error(pdev);
998 static const struct pci_error_handlers hpre_err_handler = {
999 .error_detected = hpre_error_detected,
1002 static struct pci_driver hpre_pci_driver = {
1004 .id_table = hpre_dev_ids,
1005 .probe = hpre_probe,
1006 .remove = hpre_remove,
1007 .sriov_configure = hpre_sriov_configure,
1008 .err_handler = &hpre_err_handler,
1011 static void hpre_register_debugfs(void)
1013 if (!debugfs_initialized())
1016 hpre_debugfs_root = debugfs_create_dir(hpre_name, NULL);
1017 if (IS_ERR_OR_NULL(hpre_debugfs_root))
1018 hpre_debugfs_root = NULL;
1021 static void hpre_unregister_debugfs(void)
1023 debugfs_remove_recursive(hpre_debugfs_root);
1026 static int __init hpre_init(void)
1030 hpre_register_debugfs();
1032 ret = pci_register_driver(&hpre_pci_driver);
1034 hpre_unregister_debugfs();
1035 pr_err("hpre: can't register hisi hpre driver.\n");
1041 static void __exit hpre_exit(void)
1043 pci_unregister_driver(&hpre_pci_driver);
1044 hpre_unregister_debugfs();
1047 module_init(hpre_init);
1048 module_exit(hpre_exit);
1050 MODULE_LICENSE("GPL v2");
1051 MODULE_AUTHOR("Zaibo Xu <xuzaibo@huawei.com>");
1052 MODULE_DESCRIPTION("Driver for HiSilicon HPRE accelerator");