1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 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/pm_runtime.h>
13 #include <linux/seq_file.h>
14 #include <linux/topology.h>
15 #include <linux/uacce.h>
18 #define PCI_DEVICE_ID_ZIP_PF 0xa250
19 #define PCI_DEVICE_ID_ZIP_VF 0xa251
21 #define HZIP_QUEUE_NUM_V1 4096
23 #define HZIP_CLOCK_GATE_CTRL 0x301004
24 #define COMP0_ENABLE BIT(0)
25 #define COMP1_ENABLE BIT(1)
26 #define DECOMP0_ENABLE BIT(2)
27 #define DECOMP1_ENABLE BIT(3)
28 #define DECOMP2_ENABLE BIT(4)
29 #define DECOMP3_ENABLE BIT(5)
30 #define DECOMP4_ENABLE BIT(6)
31 #define DECOMP5_ENABLE BIT(7)
32 #define HZIP_ALL_COMP_DECOMP_EN (COMP0_ENABLE | COMP1_ENABLE | \
33 DECOMP0_ENABLE | DECOMP1_ENABLE | \
34 DECOMP2_ENABLE | DECOMP3_ENABLE | \
35 DECOMP4_ENABLE | DECOMP5_ENABLE)
36 #define HZIP_DECOMP_CHECK_ENABLE BIT(16)
37 #define HZIP_FSM_MAX_CNT 0x301008
39 #define HZIP_PORT_ARCA_CHE_0 0x301040
40 #define HZIP_PORT_ARCA_CHE_1 0x301044
41 #define HZIP_PORT_AWCA_CHE_0 0x301060
42 #define HZIP_PORT_AWCA_CHE_1 0x301064
43 #define HZIP_CACHE_ALL_EN 0xffffffff
45 #define HZIP_BD_RUSER_32_63 0x301110
46 #define HZIP_SGL_RUSER_32_63 0x30111c
47 #define HZIP_DATA_RUSER_32_63 0x301128
48 #define HZIP_DATA_WUSER_32_63 0x301134
49 #define HZIP_BD_WUSER_32_63 0x301140
51 #define HZIP_QM_IDEL_STATUS 0x3040e4
53 #define HZIP_CORE_DEBUG_COMP_0 0x302000
54 #define HZIP_CORE_DEBUG_COMP_1 0x303000
55 #define HZIP_CORE_DEBUG_DECOMP_0 0x304000
56 #define HZIP_CORE_DEBUG_DECOMP_1 0x305000
57 #define HZIP_CORE_DEBUG_DECOMP_2 0x306000
58 #define HZIP_CORE_DEBUG_DECOMP_3 0x307000
59 #define HZIP_CORE_DEBUG_DECOMP_4 0x308000
60 #define HZIP_CORE_DEBUG_DECOMP_5 0x309000
62 #define HZIP_CORE_INT_SOURCE 0x3010A0
63 #define HZIP_CORE_INT_MASK_REG 0x3010A4
64 #define HZIP_CORE_INT_SET 0x3010A8
65 #define HZIP_CORE_INT_STATUS 0x3010AC
66 #define HZIP_CORE_INT_STATUS_M_ECC BIT(1)
67 #define HZIP_CORE_SRAM_ECC_ERR_INFO 0x301148
68 #define HZIP_CORE_INT_RAS_CE_ENB 0x301160
69 #define HZIP_CORE_INT_RAS_CE_ENABLE 0x1
70 #define HZIP_CORE_INT_RAS_NFE_ENB 0x301164
71 #define HZIP_CORE_INT_RAS_FE_ENB 0x301168
72 #define HZIP_OOO_SHUTDOWN_SEL 0x30120C
73 #define HZIP_CORE_INT_RAS_NFE_ENABLE 0x1FFE
74 #define HZIP_SRAM_ECC_ERR_NUM_SHIFT 16
75 #define HZIP_SRAM_ECC_ERR_ADDR_SHIFT 24
76 #define HZIP_CORE_INT_MASK_ALL GENMASK(12, 0)
77 #define HZIP_COMP_CORE_NUM 2
78 #define HZIP_DECOMP_CORE_NUM 6
79 #define HZIP_CORE_NUM (HZIP_COMP_CORE_NUM + \
81 #define HZIP_SQE_SIZE 128
82 #define HZIP_SQ_SIZE (HZIP_SQE_SIZE * QM_Q_DEPTH)
83 #define HZIP_PF_DEF_Q_NUM 64
84 #define HZIP_PF_DEF_Q_BASE 0
86 #define HZIP_SOFT_CTRL_CNT_CLR_CE 0x301000
87 #define HZIP_SOFT_CTRL_CNT_CLR_CE_BIT BIT(0)
88 #define HZIP_SOFT_CTRL_ZIP_CONTROL 0x30100C
89 #define HZIP_AXI_SHUTDOWN_ENABLE BIT(14)
90 #define HZIP_WR_PORT BIT(11)
92 #define HZIP_BUF_SIZE 22
93 #define HZIP_SQE_MASK_OFFSET 64
94 #define HZIP_SQE_MASK_LEN 48
96 #define HZIP_CNT_CLR_CE_EN BIT(0)
97 #define HZIP_RO_CNT_CLR_CE_EN BIT(2)
98 #define HZIP_RD_CNT_CLR_CE_EN (HZIP_CNT_CLR_CE_EN | \
99 HZIP_RO_CNT_CLR_CE_EN)
101 #define HZIP_PREFETCH_CFG 0x3011B0
102 #define HZIP_SVA_TRANS 0x3011C4
103 #define HZIP_PREFETCH_ENABLE (~(BIT(26) | BIT(17) | BIT(0)))
104 #define HZIP_SVA_PREFETCH_DISABLE BIT(26)
105 #define HZIP_SVA_DISABLE_READY (BIT(26) | BIT(30))
106 #define HZIP_SHAPER_RATE_COMPRESS 252
107 #define HZIP_SHAPER_RATE_DECOMPRESS 229
108 #define HZIP_DELAY_1_US 1
109 #define HZIP_POLL_TIMEOUT_US 1000
112 #define HZIP_PEH_CFG_AUTO_GATE 0x3011A8
113 #define HZIP_PEH_CFG_AUTO_GATE_EN BIT(0)
114 #define HZIP_CORE_GATED_EN GENMASK(15, 8)
115 #define HZIP_CORE_GATED_OOO_EN BIT(29)
116 #define HZIP_CLOCK_GATED_EN (HZIP_CORE_GATED_EN | \
117 HZIP_CORE_GATED_OOO_EN)
119 static const char hisi_zip_name[] = "hisi_zip";
120 static struct dentry *hzip_debugfs_root;
122 struct hisi_zip_hw_error {
127 struct zip_dfx_item {
132 static struct hisi_qm_list zip_devices = {
133 .register_to_crypto = hisi_zip_register_to_crypto,
134 .unregister_from_crypto = hisi_zip_unregister_from_crypto,
137 static struct zip_dfx_item zip_dfx_files[] = {
138 {"send_cnt", offsetof(struct hisi_zip_dfx, send_cnt)},
139 {"recv_cnt", offsetof(struct hisi_zip_dfx, recv_cnt)},
140 {"send_busy_cnt", offsetof(struct hisi_zip_dfx, send_busy_cnt)},
141 {"err_bd_cnt", offsetof(struct hisi_zip_dfx, err_bd_cnt)},
144 static const struct hisi_zip_hw_error zip_hw_error[] = {
145 { .int_msk = BIT(0), .msg = "zip_ecc_1bitt_err" },
146 { .int_msk = BIT(1), .msg = "zip_ecc_2bit_err" },
147 { .int_msk = BIT(2), .msg = "zip_axi_rresp_err" },
148 { .int_msk = BIT(3), .msg = "zip_axi_bresp_err" },
149 { .int_msk = BIT(4), .msg = "zip_src_addr_parse_err" },
150 { .int_msk = BIT(5), .msg = "zip_dst_addr_parse_err" },
151 { .int_msk = BIT(6), .msg = "zip_pre_in_addr_err" },
152 { .int_msk = BIT(7), .msg = "zip_pre_in_data_err" },
153 { .int_msk = BIT(8), .msg = "zip_com_inf_err" },
154 { .int_msk = BIT(9), .msg = "zip_enc_inf_err" },
155 { .int_msk = BIT(10), .msg = "zip_pre_out_err" },
156 { .int_msk = BIT(11), .msg = "zip_axi_poison_err" },
157 { .int_msk = BIT(12), .msg = "zip_sva_err" },
161 enum ctrl_debug_file_index {
166 static const char * const ctrl_debug_file_name[] = {
167 [HZIP_CLEAR_ENABLE] = "clear_enable",
170 struct ctrl_debug_file {
171 enum ctrl_debug_file_index index;
173 struct hisi_zip_ctrl *ctrl;
177 * One ZIP controller has one PF and multiple VFs, some global configurations
178 * which PF has need this structure.
180 * Just relevant for PF.
182 struct hisi_zip_ctrl {
183 struct hisi_zip *hisi_zip;
184 struct ctrl_debug_file files[HZIP_DEBUG_FILE_NUM];
198 static const u64 core_offsets[] = {
199 [HZIP_COMP_CORE0] = 0x302000,
200 [HZIP_COMP_CORE1] = 0x303000,
201 [HZIP_DECOMP_CORE0] = 0x304000,
202 [HZIP_DECOMP_CORE1] = 0x305000,
203 [HZIP_DECOMP_CORE2] = 0x306000,
204 [HZIP_DECOMP_CORE3] = 0x307000,
205 [HZIP_DECOMP_CORE4] = 0x308000,
206 [HZIP_DECOMP_CORE5] = 0x309000,
209 static const struct debugfs_reg32 hzip_dfx_regs[] = {
210 {"HZIP_GET_BD_NUM ", 0x00ull},
211 {"HZIP_GET_RIGHT_BD ", 0x04ull},
212 {"HZIP_GET_ERROR_BD ", 0x08ull},
213 {"HZIP_DONE_BD_NUM ", 0x0cull},
214 {"HZIP_WORK_CYCLE ", 0x10ull},
215 {"HZIP_IDLE_CYCLE ", 0x18ull},
216 {"HZIP_MAX_DELAY ", 0x20ull},
217 {"HZIP_MIN_DELAY ", 0x24ull},
218 {"HZIP_AVG_DELAY ", 0x28ull},
219 {"HZIP_MEM_VISIBLE_DATA ", 0x30ull},
220 {"HZIP_MEM_VISIBLE_ADDR ", 0x34ull},
221 {"HZIP_COMSUMED_BYTE ", 0x38ull},
222 {"HZIP_PRODUCED_BYTE ", 0x40ull},
223 {"HZIP_COMP_INF ", 0x70ull},
224 {"HZIP_PRE_OUT ", 0x78ull},
225 {"HZIP_BD_RD ", 0x7cull},
226 {"HZIP_BD_WR ", 0x80ull},
227 {"HZIP_GET_BD_AXI_ERR_NUM ", 0x84ull},
228 {"HZIP_GET_BD_PARSE_ERR_NUM ", 0x88ull},
229 {"HZIP_ADD_BD_AXI_ERR_NUM ", 0x8cull},
230 {"HZIP_DECOMP_STF_RELOAD_CURR_ST ", 0x94ull},
231 {"HZIP_DECOMP_LZ77_CURR_ST ", 0x9cull},
234 static const struct kernel_param_ops zip_uacce_mode_ops = {
235 .set = uacce_mode_set,
236 .get = param_get_int,
240 * uacce_mode = 0 means zip only register to crypto,
241 * uacce_mode = 1 means zip both register to crypto and uacce.
243 static u32 uacce_mode = UACCE_MODE_NOUACCE;
244 module_param_cb(uacce_mode, &zip_uacce_mode_ops, &uacce_mode, 0444);
245 MODULE_PARM_DESC(uacce_mode, UACCE_MODE_DESC);
247 static int pf_q_num_set(const char *val, const struct kernel_param *kp)
249 return q_num_set(val, kp, PCI_DEVICE_ID_ZIP_PF);
252 static const struct kernel_param_ops pf_q_num_ops = {
254 .get = param_get_int,
257 static u32 pf_q_num = HZIP_PF_DEF_Q_NUM;
258 module_param_cb(pf_q_num, &pf_q_num_ops, &pf_q_num, 0444);
259 MODULE_PARM_DESC(pf_q_num, "Number of queues in PF(v1 2-4096, v2 2-1024)");
261 static const struct kernel_param_ops vfs_num_ops = {
263 .get = param_get_int,
267 module_param_cb(vfs_num, &vfs_num_ops, &vfs_num, 0444);
268 MODULE_PARM_DESC(vfs_num, "Number of VFs to enable(1-63), 0(default)");
270 static const struct pci_device_id hisi_zip_dev_ids[] = {
271 { PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_ZIP_PF) },
272 { PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_ZIP_VF) },
275 MODULE_DEVICE_TABLE(pci, hisi_zip_dev_ids);
277 int zip_create_qps(struct hisi_qp **qps, int qp_num, int node)
279 if (node == NUMA_NO_NODE)
280 node = cpu_to_node(smp_processor_id());
282 return hisi_qm_alloc_qps_node(&zip_devices, qp_num, 0, node, qps);
285 static void hisi_zip_open_sva_prefetch(struct hisi_qm *qm)
290 if (qm->ver < QM_HW_V3)
293 /* Enable prefetch */
294 val = readl_relaxed(qm->io_base + HZIP_PREFETCH_CFG);
295 val &= HZIP_PREFETCH_ENABLE;
296 writel(val, qm->io_base + HZIP_PREFETCH_CFG);
298 ret = readl_relaxed_poll_timeout(qm->io_base + HZIP_PREFETCH_CFG,
299 val, !(val & HZIP_SVA_PREFETCH_DISABLE),
300 HZIP_DELAY_1_US, HZIP_POLL_TIMEOUT_US);
302 pci_err(qm->pdev, "failed to open sva prefetch\n");
305 static void hisi_zip_close_sva_prefetch(struct hisi_qm *qm)
310 if (qm->ver < QM_HW_V3)
313 val = readl_relaxed(qm->io_base + HZIP_PREFETCH_CFG);
314 val |= HZIP_SVA_PREFETCH_DISABLE;
315 writel(val, qm->io_base + HZIP_PREFETCH_CFG);
317 ret = readl_relaxed_poll_timeout(qm->io_base + HZIP_SVA_TRANS,
318 val, !(val & HZIP_SVA_DISABLE_READY),
319 HZIP_DELAY_1_US, HZIP_POLL_TIMEOUT_US);
321 pci_err(qm->pdev, "failed to close sva prefetch\n");
324 static void hisi_zip_enable_clock_gate(struct hisi_qm *qm)
328 if (qm->ver < QM_HW_V3)
331 val = readl(qm->io_base + HZIP_CLOCK_GATE_CTRL);
332 val |= HZIP_CLOCK_GATED_EN;
333 writel(val, qm->io_base + HZIP_CLOCK_GATE_CTRL);
335 val = readl(qm->io_base + HZIP_PEH_CFG_AUTO_GATE);
336 val |= HZIP_PEH_CFG_AUTO_GATE_EN;
337 writel(val, qm->io_base + HZIP_PEH_CFG_AUTO_GATE);
340 static int hisi_zip_set_user_domain_and_cache(struct hisi_qm *qm)
342 void __iomem *base = qm->io_base;
345 writel(AXUSER_BASE, base + QM_ARUSER_M_CFG_1);
346 writel(ARUSER_M_CFG_ENABLE, base + QM_ARUSER_M_CFG_ENABLE);
347 writel(AXUSER_BASE, base + QM_AWUSER_M_CFG_1);
348 writel(AWUSER_M_CFG_ENABLE, base + QM_AWUSER_M_CFG_ENABLE);
349 writel(WUSER_M_CFG_ENABLE, base + QM_WUSER_M_CFG_ENABLE);
352 writel(AXI_M_CFG, base + QM_AXI_M_CFG);
353 writel(AXI_M_CFG_ENABLE, base + QM_AXI_M_CFG_ENABLE);
355 /* disable FLR triggered by BME(bus master enable) */
356 writel(PEH_AXUSER_CFG, base + QM_PEH_AXUSER_CFG);
357 writel(PEH_AXUSER_CFG_ENABLE, base + QM_PEH_AXUSER_CFG_ENABLE);
360 writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_0);
361 writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_1);
362 writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_0);
363 writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_1);
365 /* user domain configurations */
366 writel(AXUSER_BASE, base + HZIP_BD_RUSER_32_63);
367 writel(AXUSER_BASE, base + HZIP_SGL_RUSER_32_63);
368 writel(AXUSER_BASE, base + HZIP_BD_WUSER_32_63);
370 if (qm->use_sva && qm->ver == QM_HW_V2) {
371 writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_DATA_RUSER_32_63);
372 writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_DATA_WUSER_32_63);
374 writel(AXUSER_BASE, base + HZIP_DATA_RUSER_32_63);
375 writel(AXUSER_BASE, base + HZIP_DATA_WUSER_32_63);
378 /* let's open all compression/decompression cores */
379 writel(HZIP_DECOMP_CHECK_ENABLE | HZIP_ALL_COMP_DECOMP_EN,
380 base + HZIP_CLOCK_GATE_CTRL);
382 /* enable sqc,cqc writeback */
383 writel(SQC_CACHE_ENABLE | CQC_CACHE_ENABLE | SQC_CACHE_WB_ENABLE |
384 CQC_CACHE_WB_ENABLE | FIELD_PREP(SQC_CACHE_WB_THRD, 1) |
385 FIELD_PREP(CQC_CACHE_WB_THRD, 1), base + QM_CACHE_CTL);
387 hisi_zip_enable_clock_gate(qm);
392 static void hisi_zip_master_ooo_ctrl(struct hisi_qm *qm, bool enable)
396 val1 = readl(qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
398 val1 |= HZIP_AXI_SHUTDOWN_ENABLE;
399 val2 = HZIP_CORE_INT_RAS_NFE_ENABLE;
401 val1 &= ~HZIP_AXI_SHUTDOWN_ENABLE;
405 if (qm->ver > QM_HW_V2)
406 writel(val2, qm->io_base + HZIP_OOO_SHUTDOWN_SEL);
408 writel(val1, qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
411 static void hisi_zip_hw_error_enable(struct hisi_qm *qm)
413 if (qm->ver == QM_HW_V1) {
414 writel(HZIP_CORE_INT_MASK_ALL,
415 qm->io_base + HZIP_CORE_INT_MASK_REG);
416 dev_info(&qm->pdev->dev, "Does not support hw error handle\n");
420 /* clear ZIP hw error source if having */
421 writel(HZIP_CORE_INT_MASK_ALL, qm->io_base + HZIP_CORE_INT_SOURCE);
423 /* configure error type */
424 writel(HZIP_CORE_INT_RAS_CE_ENABLE,
425 qm->io_base + HZIP_CORE_INT_RAS_CE_ENB);
426 writel(0x0, qm->io_base + HZIP_CORE_INT_RAS_FE_ENB);
427 writel(HZIP_CORE_INT_RAS_NFE_ENABLE,
428 qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB);
430 /* enable ZIP block master OOO when nfe occurs on Kunpeng930 */
431 hisi_zip_master_ooo_ctrl(qm, true);
433 /* enable ZIP hw error interrupts */
434 writel(0, qm->io_base + HZIP_CORE_INT_MASK_REG);
437 static void hisi_zip_hw_error_disable(struct hisi_qm *qm)
439 /* disable ZIP hw error interrupts */
440 writel(HZIP_CORE_INT_MASK_ALL, qm->io_base + HZIP_CORE_INT_MASK_REG);
442 /* disable ZIP block master OOO when nfe occurs on Kunpeng930 */
443 hisi_zip_master_ooo_ctrl(qm, false);
446 static inline struct hisi_qm *file_to_qm(struct ctrl_debug_file *file)
448 struct hisi_zip *hisi_zip = file->ctrl->hisi_zip;
450 return &hisi_zip->qm;
453 static u32 clear_enable_read(struct hisi_qm *qm)
455 return readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) &
456 HZIP_SOFT_CTRL_CNT_CLR_CE_BIT;
459 static int clear_enable_write(struct hisi_qm *qm, u32 val)
463 if (val != 1 && val != 0)
466 tmp = (readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) &
467 ~HZIP_SOFT_CTRL_CNT_CLR_CE_BIT) | val;
468 writel(tmp, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
473 static ssize_t hisi_zip_ctrl_debug_read(struct file *filp, char __user *buf,
474 size_t count, loff_t *pos)
476 struct ctrl_debug_file *file = filp->private_data;
477 struct hisi_qm *qm = file_to_qm(file);
478 char tbuf[HZIP_BUF_SIZE];
482 ret = hisi_qm_get_dfx_access(qm);
486 spin_lock_irq(&file->lock);
487 switch (file->index) {
488 case HZIP_CLEAR_ENABLE:
489 val = clear_enable_read(qm);
494 spin_unlock_irq(&file->lock);
496 hisi_qm_put_dfx_access(qm);
497 ret = scnprintf(tbuf, sizeof(tbuf), "%u\n", val);
498 return simple_read_from_buffer(buf, count, pos, tbuf, ret);
501 spin_unlock_irq(&file->lock);
502 hisi_qm_put_dfx_access(qm);
506 static ssize_t hisi_zip_ctrl_debug_write(struct file *filp,
507 const char __user *buf,
508 size_t count, loff_t *pos)
510 struct ctrl_debug_file *file = filp->private_data;
511 struct hisi_qm *qm = file_to_qm(file);
512 char tbuf[HZIP_BUF_SIZE];
519 if (count >= HZIP_BUF_SIZE)
522 len = simple_write_to_buffer(tbuf, HZIP_BUF_SIZE - 1, pos, buf, count);
527 if (kstrtoul(tbuf, 0, &val))
530 ret = hisi_qm_get_dfx_access(qm);
534 spin_lock_irq(&file->lock);
535 switch (file->index) {
536 case HZIP_CLEAR_ENABLE:
537 ret = clear_enable_write(qm, val);
549 spin_unlock_irq(&file->lock);
550 hisi_qm_put_dfx_access(qm);
554 static const struct file_operations ctrl_debug_fops = {
555 .owner = THIS_MODULE,
557 .read = hisi_zip_ctrl_debug_read,
558 .write = hisi_zip_ctrl_debug_write,
561 static int zip_debugfs_atomic64_set(void *data, u64 val)
566 atomic64_set((atomic64_t *)data, 0);
571 static int zip_debugfs_atomic64_get(void *data, u64 *val)
573 *val = atomic64_read((atomic64_t *)data);
578 DEFINE_DEBUGFS_ATTRIBUTE(zip_atomic64_ops, zip_debugfs_atomic64_get,
579 zip_debugfs_atomic64_set, "%llu\n");
581 static int hisi_zip_regs_show(struct seq_file *s, void *unused)
583 hisi_qm_regs_dump(s, s->private);
588 DEFINE_SHOW_ATTRIBUTE(hisi_zip_regs);
590 static int hisi_zip_core_debug_init(struct hisi_qm *qm)
592 struct device *dev = &qm->pdev->dev;
593 struct debugfs_regset32 *regset;
594 struct dentry *tmp_d;
595 char buf[HZIP_BUF_SIZE];
598 for (i = 0; i < HZIP_CORE_NUM; i++) {
599 if (i < HZIP_COMP_CORE_NUM)
600 scnprintf(buf, sizeof(buf), "comp_core%d", i);
602 scnprintf(buf, sizeof(buf), "decomp_core%d",
603 i - HZIP_COMP_CORE_NUM);
605 regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
609 regset->regs = hzip_dfx_regs;
610 regset->nregs = ARRAY_SIZE(hzip_dfx_regs);
611 regset->base = qm->io_base + core_offsets[i];
614 tmp_d = debugfs_create_dir(buf, qm->debug.debug_root);
615 debugfs_create_file("regs", 0444, tmp_d, regset,
616 &hisi_zip_regs_fops);
622 static void hisi_zip_dfx_debug_init(struct hisi_qm *qm)
624 struct hisi_zip *zip = container_of(qm, struct hisi_zip, qm);
625 struct hisi_zip_dfx *dfx = &zip->dfx;
626 struct dentry *tmp_dir;
630 tmp_dir = debugfs_create_dir("zip_dfx", qm->debug.debug_root);
631 for (i = 0; i < ARRAY_SIZE(zip_dfx_files); i++) {
632 data = (atomic64_t *)((uintptr_t)dfx + zip_dfx_files[i].offset);
633 debugfs_create_file(zip_dfx_files[i].name,
639 static int hisi_zip_ctrl_debug_init(struct hisi_qm *qm)
641 struct hisi_zip *zip = container_of(qm, struct hisi_zip, qm);
644 for (i = HZIP_CLEAR_ENABLE; i < HZIP_DEBUG_FILE_NUM; i++) {
645 spin_lock_init(&zip->ctrl->files[i].lock);
646 zip->ctrl->files[i].ctrl = zip->ctrl;
647 zip->ctrl->files[i].index = i;
649 debugfs_create_file(ctrl_debug_file_name[i], 0600,
650 qm->debug.debug_root,
651 zip->ctrl->files + i,
655 return hisi_zip_core_debug_init(qm);
658 static int hisi_zip_debugfs_init(struct hisi_qm *qm)
660 struct device *dev = &qm->pdev->dev;
661 struct dentry *dev_d;
664 dev_d = debugfs_create_dir(dev_name(dev), hzip_debugfs_root);
666 qm->debug.sqe_mask_offset = HZIP_SQE_MASK_OFFSET;
667 qm->debug.sqe_mask_len = HZIP_SQE_MASK_LEN;
668 qm->debug.debug_root = dev_d;
669 hisi_qm_debug_init(qm);
671 if (qm->fun_type == QM_HW_PF) {
672 ret = hisi_zip_ctrl_debug_init(qm);
674 goto failed_to_create;
677 hisi_zip_dfx_debug_init(qm);
682 debugfs_remove_recursive(hzip_debugfs_root);
686 /* hisi_zip_debug_regs_clear() - clear the zip debug regs */
687 static void hisi_zip_debug_regs_clear(struct hisi_qm *qm)
691 /* enable register read_clear bit */
692 writel(HZIP_RD_CNT_CLR_CE_EN, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
693 for (i = 0; i < ARRAY_SIZE(core_offsets); i++)
694 for (j = 0; j < ARRAY_SIZE(hzip_dfx_regs); j++)
695 readl(qm->io_base + core_offsets[i] +
696 hzip_dfx_regs[j].offset);
698 /* disable register read_clear bit */
699 writel(0x0, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
701 hisi_qm_debug_regs_clear(qm);
704 static void hisi_zip_debugfs_exit(struct hisi_qm *qm)
706 debugfs_remove_recursive(qm->debug.debug_root);
708 if (qm->fun_type == QM_HW_PF) {
709 hisi_zip_debug_regs_clear(qm);
710 qm->debug.curr_qm_qp_num = 0;
714 static void hisi_zip_log_hw_error(struct hisi_qm *qm, u32 err_sts)
716 const struct hisi_zip_hw_error *err = zip_hw_error;
717 struct device *dev = &qm->pdev->dev;
721 if (err->int_msk & err_sts) {
722 dev_err(dev, "%s [error status=0x%x] found\n",
723 err->msg, err->int_msk);
725 if (err->int_msk & HZIP_CORE_INT_STATUS_M_ECC) {
726 err_val = readl(qm->io_base +
727 HZIP_CORE_SRAM_ECC_ERR_INFO);
728 dev_err(dev, "hisi-zip multi ecc sram num=0x%x\n",
730 HZIP_SRAM_ECC_ERR_NUM_SHIFT) & 0xFF));
737 static u32 hisi_zip_get_hw_err_status(struct hisi_qm *qm)
739 return readl(qm->io_base + HZIP_CORE_INT_STATUS);
742 static void hisi_zip_clear_hw_err_status(struct hisi_qm *qm, u32 err_sts)
744 writel(err_sts, qm->io_base + HZIP_CORE_INT_SOURCE);
747 static void hisi_zip_open_axi_master_ooo(struct hisi_qm *qm)
751 val = readl(qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
753 writel(val & ~HZIP_AXI_SHUTDOWN_ENABLE,
754 qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
756 writel(val | HZIP_AXI_SHUTDOWN_ENABLE,
757 qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
760 static void hisi_zip_close_axi_master_ooo(struct hisi_qm *qm)
764 /* Disable ECC Mbit error report. */
765 nfe_enb = readl(qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB);
766 writel(nfe_enb & ~HZIP_CORE_INT_STATUS_M_ECC,
767 qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB);
769 /* Inject zip ECC Mbit error to block master ooo. */
770 writel(HZIP_CORE_INT_STATUS_M_ECC,
771 qm->io_base + HZIP_CORE_INT_SET);
774 static void hisi_zip_err_info_init(struct hisi_qm *qm)
776 struct hisi_qm_err_info *err_info = &qm->err_info;
778 err_info->ce = QM_BASE_CE;
780 err_info->ecc_2bits_mask = HZIP_CORE_INT_STATUS_M_ECC;
781 err_info->dev_ce_mask = HZIP_CORE_INT_RAS_CE_ENABLE;
782 err_info->msi_wr_port = HZIP_WR_PORT;
783 err_info->acpi_rst = "ZRST";
784 err_info->nfe = QM_BASE_NFE | QM_ACC_WB_NOT_READY_TIMEOUT;
786 if (qm->ver >= QM_HW_V3)
787 err_info->nfe |= QM_ACC_DO_TASK_TIMEOUT;
790 static const struct hisi_qm_err_ini hisi_zip_err_ini = {
791 .hw_init = hisi_zip_set_user_domain_and_cache,
792 .hw_err_enable = hisi_zip_hw_error_enable,
793 .hw_err_disable = hisi_zip_hw_error_disable,
794 .get_dev_hw_err_status = hisi_zip_get_hw_err_status,
795 .clear_dev_hw_err_status = hisi_zip_clear_hw_err_status,
796 .log_dev_hw_err = hisi_zip_log_hw_error,
797 .open_axi_master_ooo = hisi_zip_open_axi_master_ooo,
798 .close_axi_master_ooo = hisi_zip_close_axi_master_ooo,
799 .open_sva_prefetch = hisi_zip_open_sva_prefetch,
800 .close_sva_prefetch = hisi_zip_close_sva_prefetch,
801 .err_info_init = hisi_zip_err_info_init,
804 static int hisi_zip_pf_probe_init(struct hisi_zip *hisi_zip)
806 struct hisi_qm *qm = &hisi_zip->qm;
807 struct hisi_zip_ctrl *ctrl;
809 ctrl = devm_kzalloc(&qm->pdev->dev, sizeof(*ctrl), GFP_KERNEL);
813 hisi_zip->ctrl = ctrl;
814 ctrl->hisi_zip = hisi_zip;
815 qm->err_ini = &hisi_zip_err_ini;
816 qm->err_ini->err_info_init(qm);
818 hisi_zip_set_user_domain_and_cache(qm);
819 hisi_zip_open_sva_prefetch(qm);
820 hisi_qm_dev_err_init(qm);
821 hisi_zip_debug_regs_clear(qm);
826 static int hisi_zip_qm_init(struct hisi_qm *qm, struct pci_dev *pdev)
831 qm->ver = pdev->revision;
832 qm->algs = "zlib\ngzip";
833 qm->mode = uacce_mode;
834 qm->sqe_size = HZIP_SQE_SIZE;
835 qm->dev_name = hisi_zip_name;
837 qm->fun_type = (pdev->device == PCI_DEVICE_ID_ZIP_PF) ?
839 if (qm->fun_type == QM_HW_PF) {
840 qm->qp_base = HZIP_PF_DEF_Q_BASE;
841 qm->qp_num = pf_q_num;
842 qm->debug.curr_qm_qp_num = pf_q_num;
843 qm->qm_list = &zip_devices;
844 } else if (qm->fun_type == QM_HW_VF && qm->ver == QM_HW_V1) {
846 * have no way to get qm configure in VM in v1 hardware,
847 * so currently force PF to uses HZIP_PF_DEF_Q_NUM, and force
848 * to trigger only one VF in v1 hardware.
850 * v2 hardware has no such problem.
852 qm->qp_base = HZIP_PF_DEF_Q_NUM;
853 qm->qp_num = HZIP_QUEUE_NUM_V1 - HZIP_PF_DEF_Q_NUM;
856 qm->wq = alloc_workqueue("%s", WQ_HIGHPRI | WQ_MEM_RECLAIM |
857 WQ_UNBOUND, num_online_cpus(),
860 pci_err(qm->pdev, "fail to alloc workqueue\n");
864 ret = hisi_qm_init(qm);
866 destroy_workqueue(qm->wq);
871 static void hisi_zip_qm_uninit(struct hisi_qm *qm)
874 destroy_workqueue(qm->wq);
877 static int hisi_zip_probe_init(struct hisi_zip *hisi_zip)
879 u32 type_rate = HZIP_SHAPER_RATE_COMPRESS;
880 struct hisi_qm *qm = &hisi_zip->qm;
883 if (qm->fun_type == QM_HW_PF) {
884 ret = hisi_zip_pf_probe_init(hisi_zip);
887 /* enable shaper type 0 */
888 if (qm->ver >= QM_HW_V3) {
889 type_rate |= QM_SHAPER_ENABLE;
891 /* ZIP need to enable shaper type 1 */
892 type_rate |= HZIP_SHAPER_RATE_DECOMPRESS << QM_SHAPER_TYPE1_OFFSET;
893 qm->type_rate = type_rate;
900 static int hisi_zip_probe(struct pci_dev *pdev, const struct pci_device_id *id)
902 struct hisi_zip *hisi_zip;
906 hisi_zip = devm_kzalloc(&pdev->dev, sizeof(*hisi_zip), GFP_KERNEL);
912 ret = hisi_zip_qm_init(qm, pdev);
914 pci_err(pdev, "Failed to init ZIP QM (%d)!\n", ret);
918 ret = hisi_zip_probe_init(hisi_zip);
920 pci_err(pdev, "Failed to probe (%d)!\n", ret);
924 ret = hisi_qm_start(qm);
926 goto err_dev_err_uninit;
928 ret = hisi_zip_debugfs_init(qm);
930 pci_err(pdev, "failed to init debugfs (%d)!\n", ret);
932 ret = hisi_qm_alg_register(qm, &zip_devices);
934 pci_err(pdev, "failed to register driver to crypto!\n");
939 ret = uacce_register(qm->uacce);
941 pci_err(pdev, "failed to register uacce (%d)!\n", ret);
942 goto err_qm_alg_unregister;
946 if (qm->fun_type == QM_HW_PF && vfs_num > 0) {
947 ret = hisi_qm_sriov_enable(pdev, vfs_num);
949 goto err_qm_alg_unregister;
956 err_qm_alg_unregister:
957 hisi_qm_alg_unregister(qm, &zip_devices);
960 hisi_zip_debugfs_exit(qm);
961 hisi_qm_stop(qm, QM_NORMAL);
964 hisi_qm_dev_err_uninit(qm);
967 hisi_zip_qm_uninit(qm);
972 static void hisi_zip_remove(struct pci_dev *pdev)
974 struct hisi_qm *qm = pci_get_drvdata(pdev);
976 hisi_qm_pm_uninit(qm);
977 hisi_qm_wait_task_finish(qm, &zip_devices);
978 hisi_qm_alg_unregister(qm, &zip_devices);
980 if (qm->fun_type == QM_HW_PF && qm->vfs_num)
981 hisi_qm_sriov_disable(pdev, true);
983 hisi_zip_debugfs_exit(qm);
984 hisi_qm_stop(qm, QM_NORMAL);
985 hisi_qm_dev_err_uninit(qm);
986 hisi_zip_qm_uninit(qm);
989 static const struct dev_pm_ops hisi_zip_pm_ops = {
990 SET_RUNTIME_PM_OPS(hisi_qm_suspend, hisi_qm_resume, NULL)
993 static const struct pci_error_handlers hisi_zip_err_handler = {
994 .error_detected = hisi_qm_dev_err_detected,
995 .slot_reset = hisi_qm_dev_slot_reset,
996 .reset_prepare = hisi_qm_reset_prepare,
997 .reset_done = hisi_qm_reset_done,
1000 static struct pci_driver hisi_zip_pci_driver = {
1002 .id_table = hisi_zip_dev_ids,
1003 .probe = hisi_zip_probe,
1004 .remove = hisi_zip_remove,
1005 .sriov_configure = IS_ENABLED(CONFIG_PCI_IOV) ?
1006 hisi_qm_sriov_configure : NULL,
1007 .err_handler = &hisi_zip_err_handler,
1008 .shutdown = hisi_qm_dev_shutdown,
1009 .driver.pm = &hisi_zip_pm_ops,
1012 static void hisi_zip_register_debugfs(void)
1014 if (!debugfs_initialized())
1017 hzip_debugfs_root = debugfs_create_dir("hisi_zip", NULL);
1020 static void hisi_zip_unregister_debugfs(void)
1022 debugfs_remove_recursive(hzip_debugfs_root);
1025 static int __init hisi_zip_init(void)
1029 hisi_qm_init_list(&zip_devices);
1030 hisi_zip_register_debugfs();
1032 ret = pci_register_driver(&hisi_zip_pci_driver);
1034 hisi_zip_unregister_debugfs();
1035 pr_err("Failed to register pci driver.\n");
1041 static void __exit hisi_zip_exit(void)
1043 pci_unregister_driver(&hisi_zip_pci_driver);
1044 hisi_zip_unregister_debugfs();
1047 module_init(hisi_zip_init);
1048 module_exit(hisi_zip_exit);
1050 MODULE_LICENSE("GPL v2");
1051 MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>");
1052 MODULE_DESCRIPTION("Driver for HiSilicon ZIP accelerator");