xref: /linux/drivers/crypto/hisilicon/zip/zip_main.c (revision 0a94608f0f7de9b1135ffea3546afe68eafef57f)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 HiSilicon Limited. */
3 #include <linux/acpi.h>
4 #include <linux/aer.h>
5 #include <linux/bitops.h>
6 #include <linux/debugfs.h>
7 #include <linux/init.h>
8 #include <linux/io.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>
16 #include "zip.h"
17 
18 #define PCI_DEVICE_ID_HUAWEI_ZIP_PF	0xa250
19 
20 #define HZIP_QUEUE_NUM_V1		4096
21 
22 #define HZIP_CLOCK_GATE_CTRL		0x301004
23 #define COMP0_ENABLE			BIT(0)
24 #define COMP1_ENABLE			BIT(1)
25 #define DECOMP0_ENABLE			BIT(2)
26 #define DECOMP1_ENABLE			BIT(3)
27 #define DECOMP2_ENABLE			BIT(4)
28 #define DECOMP3_ENABLE			BIT(5)
29 #define DECOMP4_ENABLE			BIT(6)
30 #define DECOMP5_ENABLE			BIT(7)
31 #define HZIP_ALL_COMP_DECOMP_EN		(COMP0_ENABLE | COMP1_ENABLE | \
32 					 DECOMP0_ENABLE | DECOMP1_ENABLE | \
33 					 DECOMP2_ENABLE | DECOMP3_ENABLE | \
34 					 DECOMP4_ENABLE | DECOMP5_ENABLE)
35 #define HZIP_DECOMP_CHECK_ENABLE	BIT(16)
36 #define HZIP_FSM_MAX_CNT		0x301008
37 
38 #define HZIP_PORT_ARCA_CHE_0		0x301040
39 #define HZIP_PORT_ARCA_CHE_1		0x301044
40 #define HZIP_PORT_AWCA_CHE_0		0x301060
41 #define HZIP_PORT_AWCA_CHE_1		0x301064
42 #define HZIP_CACHE_ALL_EN		0xffffffff
43 
44 #define HZIP_BD_RUSER_32_63		0x301110
45 #define HZIP_SGL_RUSER_32_63		0x30111c
46 #define HZIP_DATA_RUSER_32_63		0x301128
47 #define HZIP_DATA_WUSER_32_63		0x301134
48 #define HZIP_BD_WUSER_32_63		0x301140
49 
50 #define HZIP_QM_IDEL_STATUS		0x3040e4
51 
52 #define HZIP_CORE_DEBUG_COMP_0		0x302000
53 #define HZIP_CORE_DEBUG_COMP_1		0x303000
54 #define HZIP_CORE_DEBUG_DECOMP_0	0x304000
55 #define HZIP_CORE_DEBUG_DECOMP_1	0x305000
56 #define HZIP_CORE_DEBUG_DECOMP_2	0x306000
57 #define HZIP_CORE_DEBUG_DECOMP_3	0x307000
58 #define HZIP_CORE_DEBUG_DECOMP_4	0x308000
59 #define HZIP_CORE_DEBUG_DECOMP_5	0x309000
60 
61 #define HZIP_CORE_INT_SOURCE		0x3010A0
62 #define HZIP_CORE_INT_MASK_REG		0x3010A4
63 #define HZIP_CORE_INT_SET		0x3010A8
64 #define HZIP_CORE_INT_STATUS		0x3010AC
65 #define HZIP_CORE_INT_STATUS_M_ECC	BIT(1)
66 #define HZIP_CORE_SRAM_ECC_ERR_INFO	0x301148
67 #define HZIP_CORE_INT_RAS_CE_ENB	0x301160
68 #define HZIP_CORE_INT_RAS_CE_ENABLE	0x1
69 #define HZIP_CORE_INT_RAS_NFE_ENB	0x301164
70 #define HZIP_CORE_INT_RAS_FE_ENB        0x301168
71 #define HZIP_OOO_SHUTDOWN_SEL		0x30120C
72 #define HZIP_CORE_INT_RAS_NFE_ENABLE	0x1FFE
73 #define HZIP_SRAM_ECC_ERR_NUM_SHIFT	16
74 #define HZIP_SRAM_ECC_ERR_ADDR_SHIFT	24
75 #define HZIP_CORE_INT_MASK_ALL		GENMASK(12, 0)
76 #define HZIP_COMP_CORE_NUM		2
77 #define HZIP_DECOMP_CORE_NUM		6
78 #define HZIP_CORE_NUM			(HZIP_COMP_CORE_NUM + \
79 					 HZIP_DECOMP_CORE_NUM)
80 #define HZIP_SQE_SIZE			128
81 #define HZIP_SQ_SIZE			(HZIP_SQE_SIZE * QM_Q_DEPTH)
82 #define HZIP_PF_DEF_Q_NUM		64
83 #define HZIP_PF_DEF_Q_BASE		0
84 
85 #define HZIP_SOFT_CTRL_CNT_CLR_CE	0x301000
86 #define HZIP_SOFT_CTRL_CNT_CLR_CE_BIT	BIT(0)
87 #define HZIP_SOFT_CTRL_ZIP_CONTROL	0x30100C
88 #define HZIP_AXI_SHUTDOWN_ENABLE	BIT(14)
89 #define HZIP_WR_PORT			BIT(11)
90 
91 #define HZIP_BUF_SIZE			22
92 #define HZIP_SQE_MASK_OFFSET		64
93 #define HZIP_SQE_MASK_LEN		48
94 
95 #define HZIP_CNT_CLR_CE_EN		BIT(0)
96 #define HZIP_RO_CNT_CLR_CE_EN		BIT(2)
97 #define HZIP_RD_CNT_CLR_CE_EN		(HZIP_CNT_CLR_CE_EN | \
98 					 HZIP_RO_CNT_CLR_CE_EN)
99 
100 #define HZIP_PREFETCH_CFG		0x3011B0
101 #define HZIP_SVA_TRANS			0x3011C4
102 #define HZIP_PREFETCH_ENABLE		(~(BIT(26) | BIT(17) | BIT(0)))
103 #define HZIP_SVA_PREFETCH_DISABLE	BIT(26)
104 #define HZIP_SVA_DISABLE_READY		(BIT(26) | BIT(30))
105 #define HZIP_SHAPER_RATE_COMPRESS	750
106 #define HZIP_SHAPER_RATE_DECOMPRESS	140
107 #define HZIP_DELAY_1_US		1
108 #define HZIP_POLL_TIMEOUT_US	1000
109 
110 /* clock gating */
111 #define HZIP_PEH_CFG_AUTO_GATE		0x3011A8
112 #define HZIP_PEH_CFG_AUTO_GATE_EN	BIT(0)
113 #define HZIP_CORE_GATED_EN		GENMASK(15, 8)
114 #define HZIP_CORE_GATED_OOO_EN		BIT(29)
115 #define HZIP_CLOCK_GATED_EN		(HZIP_CORE_GATED_EN | \
116 					 HZIP_CORE_GATED_OOO_EN)
117 
118 static const char hisi_zip_name[] = "hisi_zip";
119 static struct dentry *hzip_debugfs_root;
120 
121 struct hisi_zip_hw_error {
122 	u32 int_msk;
123 	const char *msg;
124 };
125 
126 struct zip_dfx_item {
127 	const char *name;
128 	u32 offset;
129 };
130 
131 static struct hisi_qm_list zip_devices = {
132 	.register_to_crypto	= hisi_zip_register_to_crypto,
133 	.unregister_from_crypto	= hisi_zip_unregister_from_crypto,
134 };
135 
136 static struct zip_dfx_item zip_dfx_files[] = {
137 	{"send_cnt", offsetof(struct hisi_zip_dfx, send_cnt)},
138 	{"recv_cnt", offsetof(struct hisi_zip_dfx, recv_cnt)},
139 	{"send_busy_cnt", offsetof(struct hisi_zip_dfx, send_busy_cnt)},
140 	{"err_bd_cnt", offsetof(struct hisi_zip_dfx, err_bd_cnt)},
141 };
142 
143 static const struct hisi_zip_hw_error zip_hw_error[] = {
144 	{ .int_msk = BIT(0), .msg = "zip_ecc_1bitt_err" },
145 	{ .int_msk = BIT(1), .msg = "zip_ecc_2bit_err" },
146 	{ .int_msk = BIT(2), .msg = "zip_axi_rresp_err" },
147 	{ .int_msk = BIT(3), .msg = "zip_axi_bresp_err" },
148 	{ .int_msk = BIT(4), .msg = "zip_src_addr_parse_err" },
149 	{ .int_msk = BIT(5), .msg = "zip_dst_addr_parse_err" },
150 	{ .int_msk = BIT(6), .msg = "zip_pre_in_addr_err" },
151 	{ .int_msk = BIT(7), .msg = "zip_pre_in_data_err" },
152 	{ .int_msk = BIT(8), .msg = "zip_com_inf_err" },
153 	{ .int_msk = BIT(9), .msg = "zip_enc_inf_err" },
154 	{ .int_msk = BIT(10), .msg = "zip_pre_out_err" },
155 	{ .int_msk = BIT(11), .msg = "zip_axi_poison_err" },
156 	{ .int_msk = BIT(12), .msg = "zip_sva_err" },
157 	{ /* sentinel */ }
158 };
159 
160 enum ctrl_debug_file_index {
161 	HZIP_CLEAR_ENABLE,
162 	HZIP_DEBUG_FILE_NUM,
163 };
164 
165 static const char * const ctrl_debug_file_name[] = {
166 	[HZIP_CLEAR_ENABLE] = "clear_enable",
167 };
168 
169 struct ctrl_debug_file {
170 	enum ctrl_debug_file_index index;
171 	spinlock_t lock;
172 	struct hisi_zip_ctrl *ctrl;
173 };
174 
175 /*
176  * One ZIP controller has one PF and multiple VFs, some global configurations
177  * which PF has need this structure.
178  *
179  * Just relevant for PF.
180  */
181 struct hisi_zip_ctrl {
182 	struct hisi_zip *hisi_zip;
183 	struct ctrl_debug_file files[HZIP_DEBUG_FILE_NUM];
184 };
185 
186 enum {
187 	HZIP_COMP_CORE0,
188 	HZIP_COMP_CORE1,
189 	HZIP_DECOMP_CORE0,
190 	HZIP_DECOMP_CORE1,
191 	HZIP_DECOMP_CORE2,
192 	HZIP_DECOMP_CORE3,
193 	HZIP_DECOMP_CORE4,
194 	HZIP_DECOMP_CORE5,
195 };
196 
197 static const u64 core_offsets[] = {
198 	[HZIP_COMP_CORE0]   = 0x302000,
199 	[HZIP_COMP_CORE1]   = 0x303000,
200 	[HZIP_DECOMP_CORE0] = 0x304000,
201 	[HZIP_DECOMP_CORE1] = 0x305000,
202 	[HZIP_DECOMP_CORE2] = 0x306000,
203 	[HZIP_DECOMP_CORE3] = 0x307000,
204 	[HZIP_DECOMP_CORE4] = 0x308000,
205 	[HZIP_DECOMP_CORE5] = 0x309000,
206 };
207 
208 static const struct debugfs_reg32 hzip_dfx_regs[] = {
209 	{"HZIP_GET_BD_NUM                ",  0x00ull},
210 	{"HZIP_GET_RIGHT_BD              ",  0x04ull},
211 	{"HZIP_GET_ERROR_BD              ",  0x08ull},
212 	{"HZIP_DONE_BD_NUM               ",  0x0cull},
213 	{"HZIP_WORK_CYCLE                ",  0x10ull},
214 	{"HZIP_IDLE_CYCLE                ",  0x18ull},
215 	{"HZIP_MAX_DELAY                 ",  0x20ull},
216 	{"HZIP_MIN_DELAY                 ",  0x24ull},
217 	{"HZIP_AVG_DELAY                 ",  0x28ull},
218 	{"HZIP_MEM_VISIBLE_DATA          ",  0x30ull},
219 	{"HZIP_MEM_VISIBLE_ADDR          ",  0x34ull},
220 	{"HZIP_CONSUMED_BYTE             ",  0x38ull},
221 	{"HZIP_PRODUCED_BYTE             ",  0x40ull},
222 	{"HZIP_COMP_INF                  ",  0x70ull},
223 	{"HZIP_PRE_OUT                   ",  0x78ull},
224 	{"HZIP_BD_RD                     ",  0x7cull},
225 	{"HZIP_BD_WR                     ",  0x80ull},
226 	{"HZIP_GET_BD_AXI_ERR_NUM        ",  0x84ull},
227 	{"HZIP_GET_BD_PARSE_ERR_NUM      ",  0x88ull},
228 	{"HZIP_ADD_BD_AXI_ERR_NUM        ",  0x8cull},
229 	{"HZIP_DECOMP_STF_RELOAD_CURR_ST ",  0x94ull},
230 	{"HZIP_DECOMP_LZ77_CURR_ST       ",  0x9cull},
231 };
232 
233 static const struct kernel_param_ops zip_uacce_mode_ops = {
234 	.set = uacce_mode_set,
235 	.get = param_get_int,
236 };
237 
238 /*
239  * uacce_mode = 0 means zip only register to crypto,
240  * uacce_mode = 1 means zip both register to crypto and uacce.
241  */
242 static u32 uacce_mode = UACCE_MODE_NOUACCE;
243 module_param_cb(uacce_mode, &zip_uacce_mode_ops, &uacce_mode, 0444);
244 MODULE_PARM_DESC(uacce_mode, UACCE_MODE_DESC);
245 
246 static int pf_q_num_set(const char *val, const struct kernel_param *kp)
247 {
248 	return q_num_set(val, kp, PCI_DEVICE_ID_HUAWEI_ZIP_PF);
249 }
250 
251 static const struct kernel_param_ops pf_q_num_ops = {
252 	.set = pf_q_num_set,
253 	.get = param_get_int,
254 };
255 
256 static u32 pf_q_num = HZIP_PF_DEF_Q_NUM;
257 module_param_cb(pf_q_num, &pf_q_num_ops, &pf_q_num, 0444);
258 MODULE_PARM_DESC(pf_q_num, "Number of queues in PF(v1 2-4096, v2 2-1024)");
259 
260 static const struct kernel_param_ops vfs_num_ops = {
261 	.set = vfs_num_set,
262 	.get = param_get_int,
263 };
264 
265 static u32 vfs_num;
266 module_param_cb(vfs_num, &vfs_num_ops, &vfs_num, 0444);
267 MODULE_PARM_DESC(vfs_num, "Number of VFs to enable(1-63), 0(default)");
268 
269 static const struct pci_device_id hisi_zip_dev_ids[] = {
270 	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_HUAWEI_ZIP_PF) },
271 	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_HUAWEI_ZIP_VF) },
272 	{ 0, }
273 };
274 MODULE_DEVICE_TABLE(pci, hisi_zip_dev_ids);
275 
276 int zip_create_qps(struct hisi_qp **qps, int qp_num, int node)
277 {
278 	if (node == NUMA_NO_NODE)
279 		node = cpu_to_node(smp_processor_id());
280 
281 	return hisi_qm_alloc_qps_node(&zip_devices, qp_num, 0, node, qps);
282 }
283 
284 static void hisi_zip_open_sva_prefetch(struct hisi_qm *qm)
285 {
286 	u32 val;
287 	int ret;
288 
289 	if (qm->ver < QM_HW_V3)
290 		return;
291 
292 	/* Enable prefetch */
293 	val = readl_relaxed(qm->io_base + HZIP_PREFETCH_CFG);
294 	val &= HZIP_PREFETCH_ENABLE;
295 	writel(val, qm->io_base + HZIP_PREFETCH_CFG);
296 
297 	ret = readl_relaxed_poll_timeout(qm->io_base + HZIP_PREFETCH_CFG,
298 					 val, !(val & HZIP_SVA_PREFETCH_DISABLE),
299 					 HZIP_DELAY_1_US, HZIP_POLL_TIMEOUT_US);
300 	if (ret)
301 		pci_err(qm->pdev, "failed to open sva prefetch\n");
302 }
303 
304 static void hisi_zip_close_sva_prefetch(struct hisi_qm *qm)
305 {
306 	u32 val;
307 	int ret;
308 
309 	if (qm->ver < QM_HW_V3)
310 		return;
311 
312 	val = readl_relaxed(qm->io_base + HZIP_PREFETCH_CFG);
313 	val |= HZIP_SVA_PREFETCH_DISABLE;
314 	writel(val, qm->io_base + HZIP_PREFETCH_CFG);
315 
316 	ret = readl_relaxed_poll_timeout(qm->io_base + HZIP_SVA_TRANS,
317 					 val, !(val & HZIP_SVA_DISABLE_READY),
318 					 HZIP_DELAY_1_US, HZIP_POLL_TIMEOUT_US);
319 	if (ret)
320 		pci_err(qm->pdev, "failed to close sva prefetch\n");
321 }
322 
323 static void hisi_zip_enable_clock_gate(struct hisi_qm *qm)
324 {
325 	u32 val;
326 
327 	if (qm->ver < QM_HW_V3)
328 		return;
329 
330 	val = readl(qm->io_base + HZIP_CLOCK_GATE_CTRL);
331 	val |= HZIP_CLOCK_GATED_EN;
332 	writel(val, qm->io_base + HZIP_CLOCK_GATE_CTRL);
333 
334 	val = readl(qm->io_base + HZIP_PEH_CFG_AUTO_GATE);
335 	val |= HZIP_PEH_CFG_AUTO_GATE_EN;
336 	writel(val, qm->io_base + HZIP_PEH_CFG_AUTO_GATE);
337 }
338 
339 static int hisi_zip_set_user_domain_and_cache(struct hisi_qm *qm)
340 {
341 	void __iomem *base = qm->io_base;
342 
343 	/* qm user domain */
344 	writel(AXUSER_BASE, base + QM_ARUSER_M_CFG_1);
345 	writel(ARUSER_M_CFG_ENABLE, base + QM_ARUSER_M_CFG_ENABLE);
346 	writel(AXUSER_BASE, base + QM_AWUSER_M_CFG_1);
347 	writel(AWUSER_M_CFG_ENABLE, base + QM_AWUSER_M_CFG_ENABLE);
348 	writel(WUSER_M_CFG_ENABLE, base + QM_WUSER_M_CFG_ENABLE);
349 
350 	/* qm cache */
351 	writel(AXI_M_CFG, base + QM_AXI_M_CFG);
352 	writel(AXI_M_CFG_ENABLE, base + QM_AXI_M_CFG_ENABLE);
353 
354 	/* disable FLR triggered by BME(bus master enable) */
355 	writel(PEH_AXUSER_CFG, base + QM_PEH_AXUSER_CFG);
356 	writel(PEH_AXUSER_CFG_ENABLE, base + QM_PEH_AXUSER_CFG_ENABLE);
357 
358 	/* cache */
359 	writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_0);
360 	writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_1);
361 	writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_0);
362 	writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_1);
363 
364 	/* user domain configurations */
365 	writel(AXUSER_BASE, base + HZIP_BD_RUSER_32_63);
366 	writel(AXUSER_BASE, base + HZIP_BD_WUSER_32_63);
367 
368 	if (qm->use_sva && qm->ver == QM_HW_V2) {
369 		writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_DATA_RUSER_32_63);
370 		writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_DATA_WUSER_32_63);
371 		writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_SGL_RUSER_32_63);
372 	} else {
373 		writel(AXUSER_BASE, base + HZIP_DATA_RUSER_32_63);
374 		writel(AXUSER_BASE, base + HZIP_DATA_WUSER_32_63);
375 		writel(AXUSER_BASE, base + HZIP_SGL_RUSER_32_63);
376 	}
377 
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);
381 
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);
386 
387 	hisi_zip_enable_clock_gate(qm);
388 
389 	return 0;
390 }
391 
392 static void hisi_zip_master_ooo_ctrl(struct hisi_qm *qm, bool enable)
393 {
394 	u32 val1, val2;
395 
396 	val1 = readl(qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
397 	if (enable) {
398 		val1 |= HZIP_AXI_SHUTDOWN_ENABLE;
399 		val2 = HZIP_CORE_INT_RAS_NFE_ENABLE;
400 	} else {
401 		val1 &= ~HZIP_AXI_SHUTDOWN_ENABLE;
402 		val2 = 0x0;
403 	}
404 
405 	if (qm->ver > QM_HW_V2)
406 		writel(val2, qm->io_base + HZIP_OOO_SHUTDOWN_SEL);
407 
408 	writel(val1, qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
409 }
410 
411 static void hisi_zip_hw_error_enable(struct hisi_qm *qm)
412 {
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");
417 		return;
418 	}
419 
420 	/* clear ZIP hw error source if having */
421 	writel(HZIP_CORE_INT_MASK_ALL, qm->io_base + HZIP_CORE_INT_SOURCE);
422 
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);
429 
430 	/* enable ZIP block master OOO when nfe occurs on Kunpeng930 */
431 	hisi_zip_master_ooo_ctrl(qm, true);
432 
433 	/* enable ZIP hw error interrupts */
434 	writel(0, qm->io_base + HZIP_CORE_INT_MASK_REG);
435 }
436 
437 static void hisi_zip_hw_error_disable(struct hisi_qm *qm)
438 {
439 	/* disable ZIP hw error interrupts */
440 	writel(HZIP_CORE_INT_MASK_ALL, qm->io_base + HZIP_CORE_INT_MASK_REG);
441 
442 	/* disable ZIP block master OOO when nfe occurs on Kunpeng930 */
443 	hisi_zip_master_ooo_ctrl(qm, false);
444 }
445 
446 static inline struct hisi_qm *file_to_qm(struct ctrl_debug_file *file)
447 {
448 	struct hisi_zip *hisi_zip = file->ctrl->hisi_zip;
449 
450 	return &hisi_zip->qm;
451 }
452 
453 static u32 clear_enable_read(struct hisi_qm *qm)
454 {
455 	return readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) &
456 		     HZIP_SOFT_CTRL_CNT_CLR_CE_BIT;
457 }
458 
459 static int clear_enable_write(struct hisi_qm *qm, u32 val)
460 {
461 	u32 tmp;
462 
463 	if (val != 1 && val != 0)
464 		return -EINVAL;
465 
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);
469 
470 	return  0;
471 }
472 
473 static ssize_t hisi_zip_ctrl_debug_read(struct file *filp, char __user *buf,
474 					size_t count, loff_t *pos)
475 {
476 	struct ctrl_debug_file *file = filp->private_data;
477 	struct hisi_qm *qm = file_to_qm(file);
478 	char tbuf[HZIP_BUF_SIZE];
479 	u32 val;
480 	int ret;
481 
482 	ret = hisi_qm_get_dfx_access(qm);
483 	if (ret)
484 		return ret;
485 
486 	spin_lock_irq(&file->lock);
487 	switch (file->index) {
488 	case HZIP_CLEAR_ENABLE:
489 		val = clear_enable_read(qm);
490 		break;
491 	default:
492 		goto err_input;
493 	}
494 	spin_unlock_irq(&file->lock);
495 
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);
499 
500 err_input:
501 	spin_unlock_irq(&file->lock);
502 	hisi_qm_put_dfx_access(qm);
503 	return -EINVAL;
504 }
505 
506 static ssize_t hisi_zip_ctrl_debug_write(struct file *filp,
507 					 const char __user *buf,
508 					 size_t count, loff_t *pos)
509 {
510 	struct ctrl_debug_file *file = filp->private_data;
511 	struct hisi_qm *qm = file_to_qm(file);
512 	char tbuf[HZIP_BUF_SIZE];
513 	unsigned long val;
514 	int len, ret;
515 
516 	if (*pos != 0)
517 		return 0;
518 
519 	if (count >= HZIP_BUF_SIZE)
520 		return -ENOSPC;
521 
522 	len = simple_write_to_buffer(tbuf, HZIP_BUF_SIZE - 1, pos, buf, count);
523 	if (len < 0)
524 		return len;
525 
526 	tbuf[len] = '\0';
527 	if (kstrtoul(tbuf, 0, &val))
528 		return -EFAULT;
529 
530 	ret = hisi_qm_get_dfx_access(qm);
531 	if (ret)
532 		return ret;
533 
534 	spin_lock_irq(&file->lock);
535 	switch (file->index) {
536 	case HZIP_CLEAR_ENABLE:
537 		ret = clear_enable_write(qm, val);
538 		if (ret)
539 			goto err_input;
540 		break;
541 	default:
542 		ret = -EINVAL;
543 		goto err_input;
544 	}
545 
546 	ret = count;
547 
548 err_input:
549 	spin_unlock_irq(&file->lock);
550 	hisi_qm_put_dfx_access(qm);
551 	return ret;
552 }
553 
554 static const struct file_operations ctrl_debug_fops = {
555 	.owner = THIS_MODULE,
556 	.open = simple_open,
557 	.read = hisi_zip_ctrl_debug_read,
558 	.write = hisi_zip_ctrl_debug_write,
559 };
560 
561 static int zip_debugfs_atomic64_set(void *data, u64 val)
562 {
563 	if (val)
564 		return -EINVAL;
565 
566 	atomic64_set((atomic64_t *)data, 0);
567 
568 	return 0;
569 }
570 
571 static int zip_debugfs_atomic64_get(void *data, u64 *val)
572 {
573 	*val = atomic64_read((atomic64_t *)data);
574 
575 	return 0;
576 }
577 
578 DEFINE_DEBUGFS_ATTRIBUTE(zip_atomic64_ops, zip_debugfs_atomic64_get,
579 			 zip_debugfs_atomic64_set, "%llu\n");
580 
581 static int hisi_zip_regs_show(struct seq_file *s, void *unused)
582 {
583 	hisi_qm_regs_dump(s, s->private);
584 
585 	return 0;
586 }
587 
588 DEFINE_SHOW_ATTRIBUTE(hisi_zip_regs);
589 
590 static int hisi_zip_core_debug_init(struct hisi_qm *qm)
591 {
592 	struct device *dev = &qm->pdev->dev;
593 	struct debugfs_regset32 *regset;
594 	struct dentry *tmp_d;
595 	char buf[HZIP_BUF_SIZE];
596 	int i;
597 
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);
601 		else
602 			scnprintf(buf, sizeof(buf), "decomp_core%d",
603 				  i - HZIP_COMP_CORE_NUM);
604 
605 		regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
606 		if (!regset)
607 			return -ENOENT;
608 
609 		regset->regs = hzip_dfx_regs;
610 		regset->nregs = ARRAY_SIZE(hzip_dfx_regs);
611 		regset->base = qm->io_base + core_offsets[i];
612 		regset->dev = dev;
613 
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);
617 	}
618 
619 	return 0;
620 }
621 
622 static void hisi_zip_dfx_debug_init(struct hisi_qm *qm)
623 {
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;
627 	void *data;
628 	int i;
629 
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,
634 				    0644, tmp_dir, data,
635 				    &zip_atomic64_ops);
636 	}
637 }
638 
639 static int hisi_zip_ctrl_debug_init(struct hisi_qm *qm)
640 {
641 	struct hisi_zip *zip = container_of(qm, struct hisi_zip, qm);
642 	int i;
643 
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;
648 
649 		debugfs_create_file(ctrl_debug_file_name[i], 0600,
650 				    qm->debug.debug_root,
651 				    zip->ctrl->files + i,
652 				    &ctrl_debug_fops);
653 	}
654 
655 	return hisi_zip_core_debug_init(qm);
656 }
657 
658 static int hisi_zip_debugfs_init(struct hisi_qm *qm)
659 {
660 	struct device *dev = &qm->pdev->dev;
661 	struct dentry *dev_d;
662 	int ret;
663 
664 	dev_d = debugfs_create_dir(dev_name(dev), hzip_debugfs_root);
665 
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);
670 
671 	if (qm->fun_type == QM_HW_PF) {
672 		ret = hisi_zip_ctrl_debug_init(qm);
673 		if (ret)
674 			goto failed_to_create;
675 	}
676 
677 	hisi_zip_dfx_debug_init(qm);
678 
679 	return 0;
680 
681 failed_to_create:
682 	debugfs_remove_recursive(hzip_debugfs_root);
683 	return ret;
684 }
685 
686 /* hisi_zip_debug_regs_clear() - clear the zip debug regs */
687 static void hisi_zip_debug_regs_clear(struct hisi_qm *qm)
688 {
689 	int i, j;
690 
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);
697 
698 	/* disable register read_clear bit */
699 	writel(0x0, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
700 
701 	hisi_qm_debug_regs_clear(qm);
702 }
703 
704 static void hisi_zip_debugfs_exit(struct hisi_qm *qm)
705 {
706 	debugfs_remove_recursive(qm->debug.debug_root);
707 
708 	if (qm->fun_type == QM_HW_PF) {
709 		hisi_zip_debug_regs_clear(qm);
710 		qm->debug.curr_qm_qp_num = 0;
711 	}
712 }
713 
714 static void hisi_zip_log_hw_error(struct hisi_qm *qm, u32 err_sts)
715 {
716 	const struct hisi_zip_hw_error *err = zip_hw_error;
717 	struct device *dev = &qm->pdev->dev;
718 	u32 err_val;
719 
720 	while (err->msg) {
721 		if (err->int_msk & err_sts) {
722 			dev_err(dev, "%s [error status=0x%x] found\n",
723 				err->msg, err->int_msk);
724 
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",
729 					((err_val >>
730 					HZIP_SRAM_ECC_ERR_NUM_SHIFT) & 0xFF));
731 			}
732 		}
733 		err++;
734 	}
735 }
736 
737 static u32 hisi_zip_get_hw_err_status(struct hisi_qm *qm)
738 {
739 	return readl(qm->io_base + HZIP_CORE_INT_STATUS);
740 }
741 
742 static void hisi_zip_clear_hw_err_status(struct hisi_qm *qm, u32 err_sts)
743 {
744 	writel(err_sts, qm->io_base + HZIP_CORE_INT_SOURCE);
745 }
746 
747 static void hisi_zip_open_axi_master_ooo(struct hisi_qm *qm)
748 {
749 	u32 val;
750 
751 	val = readl(qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
752 
753 	writel(val & ~HZIP_AXI_SHUTDOWN_ENABLE,
754 	       qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
755 
756 	writel(val | HZIP_AXI_SHUTDOWN_ENABLE,
757 	       qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
758 }
759 
760 static void hisi_zip_close_axi_master_ooo(struct hisi_qm *qm)
761 {
762 	u32 nfe_enb;
763 
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);
768 
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);
772 }
773 
774 static void hisi_zip_err_info_init(struct hisi_qm *qm)
775 {
776 	struct hisi_qm_err_info *err_info = &qm->err_info;
777 
778 	err_info->ce = QM_BASE_CE;
779 	err_info->fe = 0;
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;
785 
786 	if (qm->ver >= QM_HW_V3)
787 		err_info->nfe |= QM_ACC_DO_TASK_TIMEOUT;
788 }
789 
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,
802 };
803 
804 static int hisi_zip_pf_probe_init(struct hisi_zip *hisi_zip)
805 {
806 	struct hisi_qm *qm = &hisi_zip->qm;
807 	struct hisi_zip_ctrl *ctrl;
808 
809 	ctrl = devm_kzalloc(&qm->pdev->dev, sizeof(*ctrl), GFP_KERNEL);
810 	if (!ctrl)
811 		return -ENOMEM;
812 
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);
817 
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);
822 
823 	return 0;
824 }
825 
826 static int hisi_zip_qm_init(struct hisi_qm *qm, struct pci_dev *pdev)
827 {
828 	int ret;
829 
830 	qm->pdev = pdev;
831 	qm->ver = pdev->revision;
832 	if (pdev->revision >= QM_HW_V3)
833 		qm->algs = "zlib\ngzip\ndeflate\nlz77_zstd";
834 	else
835 		qm->algs = "zlib\ngzip";
836 	qm->mode = uacce_mode;
837 	qm->sqe_size = HZIP_SQE_SIZE;
838 	qm->dev_name = hisi_zip_name;
839 
840 	qm->fun_type = (pdev->device == PCI_DEVICE_ID_HUAWEI_ZIP_PF) ?
841 			QM_HW_PF : QM_HW_VF;
842 	if (qm->fun_type == QM_HW_PF) {
843 		qm->qp_base = HZIP_PF_DEF_Q_BASE;
844 		qm->qp_num = pf_q_num;
845 		qm->debug.curr_qm_qp_num = pf_q_num;
846 		qm->qm_list = &zip_devices;
847 	} else if (qm->fun_type == QM_HW_VF && qm->ver == QM_HW_V1) {
848 		/*
849 		 * have no way to get qm configure in VM in v1 hardware,
850 		 * so currently force PF to uses HZIP_PF_DEF_Q_NUM, and force
851 		 * to trigger only one VF in v1 hardware.
852 		 *
853 		 * v2 hardware has no such problem.
854 		 */
855 		qm->qp_base = HZIP_PF_DEF_Q_NUM;
856 		qm->qp_num = HZIP_QUEUE_NUM_V1 - HZIP_PF_DEF_Q_NUM;
857 	}
858 
859 	qm->wq = alloc_workqueue("%s", WQ_HIGHPRI | WQ_MEM_RECLAIM |
860 				 WQ_UNBOUND, num_online_cpus(),
861 				 pci_name(qm->pdev));
862 	if (!qm->wq) {
863 		pci_err(qm->pdev, "fail to alloc workqueue\n");
864 		return -ENOMEM;
865 	}
866 
867 	ret = hisi_qm_init(qm);
868 	if (ret)
869 		destroy_workqueue(qm->wq);
870 
871 	return ret;
872 }
873 
874 static void hisi_zip_qm_uninit(struct hisi_qm *qm)
875 {
876 	hisi_qm_uninit(qm);
877 	destroy_workqueue(qm->wq);
878 }
879 
880 static int hisi_zip_probe_init(struct hisi_zip *hisi_zip)
881 {
882 	u32 type_rate = HZIP_SHAPER_RATE_COMPRESS;
883 	struct hisi_qm *qm = &hisi_zip->qm;
884 	int ret;
885 
886 	if (qm->fun_type == QM_HW_PF) {
887 		ret = hisi_zip_pf_probe_init(hisi_zip);
888 		if (ret)
889 			return ret;
890 		/* enable shaper type 0 */
891 		if (qm->ver >= QM_HW_V3) {
892 			type_rate |= QM_SHAPER_ENABLE;
893 
894 			/* ZIP need to enable shaper type 1 */
895 			type_rate |= HZIP_SHAPER_RATE_DECOMPRESS << QM_SHAPER_TYPE1_OFFSET;
896 			qm->type_rate = type_rate;
897 		}
898 	}
899 
900 	return 0;
901 }
902 
903 static int hisi_zip_probe(struct pci_dev *pdev, const struct pci_device_id *id)
904 {
905 	struct hisi_zip *hisi_zip;
906 	struct hisi_qm *qm;
907 	int ret;
908 
909 	hisi_zip = devm_kzalloc(&pdev->dev, sizeof(*hisi_zip), GFP_KERNEL);
910 	if (!hisi_zip)
911 		return -ENOMEM;
912 
913 	qm = &hisi_zip->qm;
914 
915 	ret = hisi_zip_qm_init(qm, pdev);
916 	if (ret) {
917 		pci_err(pdev, "Failed to init ZIP QM (%d)!\n", ret);
918 		return ret;
919 	}
920 
921 	ret = hisi_zip_probe_init(hisi_zip);
922 	if (ret) {
923 		pci_err(pdev, "Failed to probe (%d)!\n", ret);
924 		goto err_qm_uninit;
925 	}
926 
927 	ret = hisi_qm_start(qm);
928 	if (ret)
929 		goto err_dev_err_uninit;
930 
931 	ret = hisi_zip_debugfs_init(qm);
932 	if (ret)
933 		pci_err(pdev, "failed to init debugfs (%d)!\n", ret);
934 
935 	ret = hisi_qm_alg_register(qm, &zip_devices);
936 	if (ret < 0) {
937 		pci_err(pdev, "failed to register driver to crypto!\n");
938 		goto err_qm_stop;
939 	}
940 
941 	if (qm->uacce) {
942 		ret = uacce_register(qm->uacce);
943 		if (ret) {
944 			pci_err(pdev, "failed to register uacce (%d)!\n", ret);
945 			goto err_qm_alg_unregister;
946 		}
947 	}
948 
949 	if (qm->fun_type == QM_HW_PF && vfs_num > 0) {
950 		ret = hisi_qm_sriov_enable(pdev, vfs_num);
951 		if (ret < 0)
952 			goto err_qm_alg_unregister;
953 	}
954 
955 	hisi_qm_pm_init(qm);
956 
957 	return 0;
958 
959 err_qm_alg_unregister:
960 	hisi_qm_alg_unregister(qm, &zip_devices);
961 
962 err_qm_stop:
963 	hisi_zip_debugfs_exit(qm);
964 	hisi_qm_stop(qm, QM_NORMAL);
965 
966 err_dev_err_uninit:
967 	hisi_qm_dev_err_uninit(qm);
968 
969 err_qm_uninit:
970 	hisi_zip_qm_uninit(qm);
971 
972 	return ret;
973 }
974 
975 static void hisi_zip_remove(struct pci_dev *pdev)
976 {
977 	struct hisi_qm *qm = pci_get_drvdata(pdev);
978 
979 	hisi_qm_pm_uninit(qm);
980 	hisi_qm_wait_task_finish(qm, &zip_devices);
981 	hisi_qm_alg_unregister(qm, &zip_devices);
982 
983 	if (qm->fun_type == QM_HW_PF && qm->vfs_num)
984 		hisi_qm_sriov_disable(pdev, true);
985 
986 	hisi_zip_debugfs_exit(qm);
987 	hisi_qm_stop(qm, QM_NORMAL);
988 	hisi_qm_dev_err_uninit(qm);
989 	hisi_zip_qm_uninit(qm);
990 }
991 
992 static const struct dev_pm_ops hisi_zip_pm_ops = {
993 	SET_RUNTIME_PM_OPS(hisi_qm_suspend, hisi_qm_resume, NULL)
994 };
995 
996 static const struct pci_error_handlers hisi_zip_err_handler = {
997 	.error_detected	= hisi_qm_dev_err_detected,
998 	.slot_reset	= hisi_qm_dev_slot_reset,
999 	.reset_prepare	= hisi_qm_reset_prepare,
1000 	.reset_done	= hisi_qm_reset_done,
1001 };
1002 
1003 static struct pci_driver hisi_zip_pci_driver = {
1004 	.name			= "hisi_zip",
1005 	.id_table		= hisi_zip_dev_ids,
1006 	.probe			= hisi_zip_probe,
1007 	.remove			= hisi_zip_remove,
1008 	.sriov_configure	= IS_ENABLED(CONFIG_PCI_IOV) ?
1009 					hisi_qm_sriov_configure : NULL,
1010 	.err_handler		= &hisi_zip_err_handler,
1011 	.shutdown		= hisi_qm_dev_shutdown,
1012 	.driver.pm		= &hisi_zip_pm_ops,
1013 };
1014 
1015 struct pci_driver *hisi_zip_get_pf_driver(void)
1016 {
1017 	return &hisi_zip_pci_driver;
1018 }
1019 EXPORT_SYMBOL_GPL(hisi_zip_get_pf_driver);
1020 
1021 static void hisi_zip_register_debugfs(void)
1022 {
1023 	if (!debugfs_initialized())
1024 		return;
1025 
1026 	hzip_debugfs_root = debugfs_create_dir("hisi_zip", NULL);
1027 }
1028 
1029 static void hisi_zip_unregister_debugfs(void)
1030 {
1031 	debugfs_remove_recursive(hzip_debugfs_root);
1032 }
1033 
1034 static int __init hisi_zip_init(void)
1035 {
1036 	int ret;
1037 
1038 	hisi_qm_init_list(&zip_devices);
1039 	hisi_zip_register_debugfs();
1040 
1041 	ret = pci_register_driver(&hisi_zip_pci_driver);
1042 	if (ret < 0) {
1043 		hisi_zip_unregister_debugfs();
1044 		pr_err("Failed to register pci driver.\n");
1045 	}
1046 
1047 	return ret;
1048 }
1049 
1050 static void __exit hisi_zip_exit(void)
1051 {
1052 	pci_unregister_driver(&hisi_zip_pci_driver);
1053 	hisi_zip_unregister_debugfs();
1054 }
1055 
1056 module_init(hisi_zip_init);
1057 module_exit(hisi_zip_exit);
1058 
1059 MODULE_LICENSE("GPL v2");
1060 MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>");
1061 MODULE_DESCRIPTION("Driver for HiSilicon ZIP accelerator");
1062