xref: /openbmc/linux/drivers/crypto/hisilicon/zip/zip_main.c (revision 9b0c97dfc215b87208a699870881a69e762301ca)
162c455caSZhou Wang // SPDX-License-Identifier: GPL-2.0
262c455caSZhou Wang /* Copyright (c) 2019 HiSilicon Limited. */
362c455caSZhou Wang #include <linux/acpi.h>
462c455caSZhou Wang #include <linux/aer.h>
562c455caSZhou Wang #include <linux/bitops.h>
672c7a68dSZhou Wang #include <linux/debugfs.h>
762c455caSZhou Wang #include <linux/init.h>
862c455caSZhou Wang #include <linux/io.h>
962c455caSZhou Wang #include <linux/kernel.h>
1062c455caSZhou Wang #include <linux/module.h>
1162c455caSZhou Wang #include <linux/pci.h>
12607c191bSWeili Qian #include <linux/pm_runtime.h>
1372c7a68dSZhou Wang #include <linux/seq_file.h>
1462c455caSZhou Wang #include <linux/topology.h>
159e00df71SZhangfei Gao #include <linux/uacce.h>
1662c455caSZhou Wang #include "zip.h"
1762c455caSZhou Wang 
18fae74feaSShameer Kolothum #define PCI_DEVICE_ID_HUAWEI_ZIP_PF	0xa250
1962c455caSZhou Wang 
2062c455caSZhou Wang #define HZIP_QUEUE_NUM_V1		4096
2162c455caSZhou Wang 
2262c455caSZhou Wang #define HZIP_CLOCK_GATE_CTRL		0x301004
2362c455caSZhou Wang #define COMP0_ENABLE			BIT(0)
2462c455caSZhou Wang #define COMP1_ENABLE			BIT(1)
2562c455caSZhou Wang #define DECOMP0_ENABLE			BIT(2)
2662c455caSZhou Wang #define DECOMP1_ENABLE			BIT(3)
2762c455caSZhou Wang #define DECOMP2_ENABLE			BIT(4)
2862c455caSZhou Wang #define DECOMP3_ENABLE			BIT(5)
2962c455caSZhou Wang #define DECOMP4_ENABLE			BIT(6)
3062c455caSZhou Wang #define DECOMP5_ENABLE			BIT(7)
3115b0694fSYang Shen #define HZIP_ALL_COMP_DECOMP_EN		(COMP0_ENABLE | COMP1_ENABLE | \
3262c455caSZhou Wang 					 DECOMP0_ENABLE | DECOMP1_ENABLE | \
3362c455caSZhou Wang 					 DECOMP2_ENABLE | DECOMP3_ENABLE | \
3462c455caSZhou Wang 					 DECOMP4_ENABLE | DECOMP5_ENABLE)
3515b0694fSYang Shen #define HZIP_DECOMP_CHECK_ENABLE	BIT(16)
3672c7a68dSZhou Wang #define HZIP_FSM_MAX_CNT		0x301008
3762c455caSZhou Wang 
3862c455caSZhou Wang #define HZIP_PORT_ARCA_CHE_0		0x301040
3962c455caSZhou Wang #define HZIP_PORT_ARCA_CHE_1		0x301044
4062c455caSZhou Wang #define HZIP_PORT_AWCA_CHE_0		0x301060
4162c455caSZhou Wang #define HZIP_PORT_AWCA_CHE_1		0x301064
4215b0694fSYang Shen #define HZIP_CACHE_ALL_EN		0xffffffff
4362c455caSZhou Wang 
4462c455caSZhou Wang #define HZIP_BD_RUSER_32_63		0x301110
4562c455caSZhou Wang #define HZIP_SGL_RUSER_32_63		0x30111c
4662c455caSZhou Wang #define HZIP_DATA_RUSER_32_63		0x301128
4762c455caSZhou Wang #define HZIP_DATA_WUSER_32_63		0x301134
4862c455caSZhou Wang #define HZIP_BD_WUSER_32_63		0x301140
4962c455caSZhou Wang 
5072c7a68dSZhou Wang #define HZIP_QM_IDEL_STATUS		0x3040e4
5162c455caSZhou Wang 
52*9b0c97dfSKai Ye #define HZIP_CORE_DFX_BASE		0x301000
53*9b0c97dfSKai Ye #define HZIP_CLOCK_GATED_CONTL		0X301004
54*9b0c97dfSKai Ye #define HZIP_CORE_DFX_COMP_0		0x302000
55*9b0c97dfSKai Ye #define HZIP_CORE_DFX_COMP_1		0x303000
56*9b0c97dfSKai Ye #define HZIP_CORE_DFX_DECOMP_0		0x304000
57*9b0c97dfSKai Ye #define HZIP_CORE_DFX_DECOMP_1		0x305000
58*9b0c97dfSKai Ye #define HZIP_CORE_DFX_DECOMP_2		0x306000
59*9b0c97dfSKai Ye #define HZIP_CORE_DFX_DECOMP_3		0x307000
60*9b0c97dfSKai Ye #define HZIP_CORE_DFX_DECOMP_4		0x308000
61*9b0c97dfSKai Ye #define HZIP_CORE_DFX_DECOMP_5		0x309000
62*9b0c97dfSKai Ye #define HZIP_CORE_REGS_BASE_LEN		0xB0
63*9b0c97dfSKai Ye #define HZIP_CORE_REGS_DFX_LEN		0x28
6462c455caSZhou Wang 
6562c455caSZhou Wang #define HZIP_CORE_INT_SOURCE		0x3010A0
66eaebf4c3SShukun Tan #define HZIP_CORE_INT_MASK_REG		0x3010A4
6784c9b780SShukun Tan #define HZIP_CORE_INT_SET		0x3010A8
6862c455caSZhou Wang #define HZIP_CORE_INT_STATUS		0x3010AC
6962c455caSZhou Wang #define HZIP_CORE_INT_STATUS_M_ECC	BIT(1)
7062c455caSZhou Wang #define HZIP_CORE_SRAM_ECC_ERR_INFO	0x301148
71de3daf4bSShukun Tan #define HZIP_CORE_INT_RAS_CE_ENB	0x301160
721db0016eSWeili Qian #define HZIP_CORE_INT_RAS_CE_ENABLE	0x1
73de3daf4bSShukun Tan #define HZIP_CORE_INT_RAS_NFE_ENB	0x301164
74de3daf4bSShukun Tan #define HZIP_CORE_INT_RAS_FE_ENB        0x301168
75b7da13d0SWeili Qian #define HZIP_OOO_SHUTDOWN_SEL		0x30120C
76b7220a74SWeili Qian #define HZIP_CORE_INT_RAS_NFE_ENABLE	0x1FFE
77f826e6efSShukun Tan #define HZIP_SRAM_ECC_ERR_NUM_SHIFT	16
78f826e6efSShukun Tan #define HZIP_SRAM_ECC_ERR_ADDR_SHIFT	24
79b7220a74SWeili Qian #define HZIP_CORE_INT_MASK_ALL		GENMASK(12, 0)
8072c7a68dSZhou Wang #define HZIP_COMP_CORE_NUM		2
8172c7a68dSZhou Wang #define HZIP_DECOMP_CORE_NUM		6
8272c7a68dSZhou Wang #define HZIP_CORE_NUM			(HZIP_COMP_CORE_NUM + \
8372c7a68dSZhou Wang 					 HZIP_DECOMP_CORE_NUM)
8462c455caSZhou Wang #define HZIP_SQE_SIZE			128
8572c7a68dSZhou Wang #define HZIP_SQ_SIZE			(HZIP_SQE_SIZE * QM_Q_DEPTH)
8662c455caSZhou Wang #define HZIP_PF_DEF_Q_NUM		64
8762c455caSZhou Wang #define HZIP_PF_DEF_Q_BASE		0
8862c455caSZhou Wang 
8972c7a68dSZhou Wang #define HZIP_SOFT_CTRL_CNT_CLR_CE	0x301000
9015b0694fSYang Shen #define HZIP_SOFT_CTRL_CNT_CLR_CE_BIT	BIT(0)
9184c9b780SShukun Tan #define HZIP_SOFT_CTRL_ZIP_CONTROL	0x30100C
9284c9b780SShukun Tan #define HZIP_AXI_SHUTDOWN_ENABLE	BIT(14)
9384c9b780SShukun Tan #define HZIP_WR_PORT			BIT(11)
9462c455caSZhou Wang 
9572c7a68dSZhou Wang #define HZIP_BUF_SIZE			22
96c31dc9feSShukun Tan #define HZIP_SQE_MASK_OFFSET		64
97c31dc9feSShukun Tan #define HZIP_SQE_MASK_LEN		48
9862c455caSZhou Wang 
99698f9523SHao Fang #define HZIP_CNT_CLR_CE_EN		BIT(0)
100698f9523SHao Fang #define HZIP_RO_CNT_CLR_CE_EN		BIT(2)
101698f9523SHao Fang #define HZIP_RD_CNT_CLR_CE_EN		(HZIP_CNT_CLR_CE_EN | \
102698f9523SHao Fang 					 HZIP_RO_CNT_CLR_CE_EN)
103698f9523SHao Fang 
104a5c164b1SLongfang Liu #define HZIP_PREFETCH_CFG		0x3011B0
105a5c164b1SLongfang Liu #define HZIP_SVA_TRANS			0x3011C4
106a5c164b1SLongfang Liu #define HZIP_PREFETCH_ENABLE		(~(BIT(26) | BIT(17) | BIT(0)))
107a5c164b1SLongfang Liu #define HZIP_SVA_PREFETCH_DISABLE	BIT(26)
108a5c164b1SLongfang Liu #define HZIP_SVA_DISABLE_READY		(BIT(26) | BIT(30))
109376a5c3cSKai Ye #define HZIP_SHAPER_RATE_COMPRESS	750
110376a5c3cSKai Ye #define HZIP_SHAPER_RATE_DECOMPRESS	140
111a5c164b1SLongfang Liu #define HZIP_DELAY_1_US		1
112a5c164b1SLongfang Liu #define HZIP_POLL_TIMEOUT_US	1000
113a5c164b1SLongfang Liu 
114ed5fa39fSWeili Qian /* clock gating */
115ed5fa39fSWeili Qian #define HZIP_PEH_CFG_AUTO_GATE		0x3011A8
116ed5fa39fSWeili Qian #define HZIP_PEH_CFG_AUTO_GATE_EN	BIT(0)
117ed5fa39fSWeili Qian #define HZIP_CORE_GATED_EN		GENMASK(15, 8)
118ed5fa39fSWeili Qian #define HZIP_CORE_GATED_OOO_EN		BIT(29)
119ed5fa39fSWeili Qian #define HZIP_CLOCK_GATED_EN		(HZIP_CORE_GATED_EN | \
120ed5fa39fSWeili Qian 					 HZIP_CORE_GATED_OOO_EN)
121ed5fa39fSWeili Qian 
12262c455caSZhou Wang static const char hisi_zip_name[] = "hisi_zip";
12372c7a68dSZhou Wang static struct dentry *hzip_debugfs_root;
12462c455caSZhou Wang 
12562c455caSZhou Wang struct hisi_zip_hw_error {
12662c455caSZhou Wang 	u32 int_msk;
12762c455caSZhou Wang 	const char *msg;
12862c455caSZhou Wang };
12962c455caSZhou Wang 
1306621e649SLongfang Liu struct zip_dfx_item {
1316621e649SLongfang Liu 	const char *name;
1326621e649SLongfang Liu 	u32 offset;
1336621e649SLongfang Liu };
1346621e649SLongfang Liu 
1353d29e98dSYang Shen static struct hisi_qm_list zip_devices = {
1363d29e98dSYang Shen 	.register_to_crypto	= hisi_zip_register_to_crypto,
1373d29e98dSYang Shen 	.unregister_from_crypto	= hisi_zip_unregister_from_crypto,
1383d29e98dSYang Shen };
1393d29e98dSYang Shen 
1406621e649SLongfang Liu static struct zip_dfx_item zip_dfx_files[] = {
1416621e649SLongfang Liu 	{"send_cnt", offsetof(struct hisi_zip_dfx, send_cnt)},
1426621e649SLongfang Liu 	{"recv_cnt", offsetof(struct hisi_zip_dfx, recv_cnt)},
1436621e649SLongfang Liu 	{"send_busy_cnt", offsetof(struct hisi_zip_dfx, send_busy_cnt)},
1446621e649SLongfang Liu 	{"err_bd_cnt", offsetof(struct hisi_zip_dfx, err_bd_cnt)},
1456621e649SLongfang Liu };
1466621e649SLongfang Liu 
14762c455caSZhou Wang static const struct hisi_zip_hw_error zip_hw_error[] = {
14862c455caSZhou Wang 	{ .int_msk = BIT(0), .msg = "zip_ecc_1bitt_err" },
14962c455caSZhou Wang 	{ .int_msk = BIT(1), .msg = "zip_ecc_2bit_err" },
15062c455caSZhou Wang 	{ .int_msk = BIT(2), .msg = "zip_axi_rresp_err" },
15162c455caSZhou Wang 	{ .int_msk = BIT(3), .msg = "zip_axi_bresp_err" },
15262c455caSZhou Wang 	{ .int_msk = BIT(4), .msg = "zip_src_addr_parse_err" },
15362c455caSZhou Wang 	{ .int_msk = BIT(5), .msg = "zip_dst_addr_parse_err" },
15462c455caSZhou Wang 	{ .int_msk = BIT(6), .msg = "zip_pre_in_addr_err" },
15562c455caSZhou Wang 	{ .int_msk = BIT(7), .msg = "zip_pre_in_data_err" },
15662c455caSZhou Wang 	{ .int_msk = BIT(8), .msg = "zip_com_inf_err" },
15762c455caSZhou Wang 	{ .int_msk = BIT(9), .msg = "zip_enc_inf_err" },
15862c455caSZhou Wang 	{ .int_msk = BIT(10), .msg = "zip_pre_out_err" },
159b7220a74SWeili Qian 	{ .int_msk = BIT(11), .msg = "zip_axi_poison_err" },
160b7220a74SWeili Qian 	{ .int_msk = BIT(12), .msg = "zip_sva_err" },
16162c455caSZhou Wang 	{ /* sentinel */ }
16262c455caSZhou Wang };
16362c455caSZhou Wang 
16472c7a68dSZhou Wang enum ctrl_debug_file_index {
16572c7a68dSZhou Wang 	HZIP_CLEAR_ENABLE,
16672c7a68dSZhou Wang 	HZIP_DEBUG_FILE_NUM,
16772c7a68dSZhou Wang };
16872c7a68dSZhou Wang 
16972c7a68dSZhou Wang static const char * const ctrl_debug_file_name[] = {
17072c7a68dSZhou Wang 	[HZIP_CLEAR_ENABLE] = "clear_enable",
17172c7a68dSZhou Wang };
17272c7a68dSZhou Wang 
17372c7a68dSZhou Wang struct ctrl_debug_file {
17472c7a68dSZhou Wang 	enum ctrl_debug_file_index index;
17572c7a68dSZhou Wang 	spinlock_t lock;
17672c7a68dSZhou Wang 	struct hisi_zip_ctrl *ctrl;
17772c7a68dSZhou Wang };
17872c7a68dSZhou Wang 
17962c455caSZhou Wang /*
18062c455caSZhou Wang  * One ZIP controller has one PF and multiple VFs, some global configurations
18162c455caSZhou Wang  * which PF has need this structure.
18262c455caSZhou Wang  *
18362c455caSZhou Wang  * Just relevant for PF.
18462c455caSZhou Wang  */
18562c455caSZhou Wang struct hisi_zip_ctrl {
18662c455caSZhou Wang 	struct hisi_zip *hisi_zip;
18772c7a68dSZhou Wang 	struct ctrl_debug_file files[HZIP_DEBUG_FILE_NUM];
18872c7a68dSZhou Wang };
18972c7a68dSZhou Wang 
19072c7a68dSZhou Wang enum {
19172c7a68dSZhou Wang 	HZIP_COMP_CORE0,
19272c7a68dSZhou Wang 	HZIP_COMP_CORE1,
19372c7a68dSZhou Wang 	HZIP_DECOMP_CORE0,
19472c7a68dSZhou Wang 	HZIP_DECOMP_CORE1,
19572c7a68dSZhou Wang 	HZIP_DECOMP_CORE2,
19672c7a68dSZhou Wang 	HZIP_DECOMP_CORE3,
19772c7a68dSZhou Wang 	HZIP_DECOMP_CORE4,
19872c7a68dSZhou Wang 	HZIP_DECOMP_CORE5,
19972c7a68dSZhou Wang };
20072c7a68dSZhou Wang 
20172c7a68dSZhou Wang static const u64 core_offsets[] = {
20272c7a68dSZhou Wang 	[HZIP_COMP_CORE0]   = 0x302000,
20372c7a68dSZhou Wang 	[HZIP_COMP_CORE1]   = 0x303000,
20472c7a68dSZhou Wang 	[HZIP_DECOMP_CORE0] = 0x304000,
20572c7a68dSZhou Wang 	[HZIP_DECOMP_CORE1] = 0x305000,
20672c7a68dSZhou Wang 	[HZIP_DECOMP_CORE2] = 0x306000,
20772c7a68dSZhou Wang 	[HZIP_DECOMP_CORE3] = 0x307000,
20872c7a68dSZhou Wang 	[HZIP_DECOMP_CORE4] = 0x308000,
20972c7a68dSZhou Wang 	[HZIP_DECOMP_CORE5] = 0x309000,
21072c7a68dSZhou Wang };
21172c7a68dSZhou Wang 
2128f68659bSRikard Falkeborn static const struct debugfs_reg32 hzip_dfx_regs[] = {
21372c7a68dSZhou Wang 	{"HZIP_GET_BD_NUM                ",  0x00ull},
21472c7a68dSZhou Wang 	{"HZIP_GET_RIGHT_BD              ",  0x04ull},
21572c7a68dSZhou Wang 	{"HZIP_GET_ERROR_BD              ",  0x08ull},
21672c7a68dSZhou Wang 	{"HZIP_DONE_BD_NUM               ",  0x0cull},
21772c7a68dSZhou Wang 	{"HZIP_WORK_CYCLE                ",  0x10ull},
21872c7a68dSZhou Wang 	{"HZIP_IDLE_CYCLE                ",  0x18ull},
21972c7a68dSZhou Wang 	{"HZIP_MAX_DELAY                 ",  0x20ull},
22072c7a68dSZhou Wang 	{"HZIP_MIN_DELAY                 ",  0x24ull},
22172c7a68dSZhou Wang 	{"HZIP_AVG_DELAY                 ",  0x28ull},
22272c7a68dSZhou Wang 	{"HZIP_MEM_VISIBLE_DATA          ",  0x30ull},
22372c7a68dSZhou Wang 	{"HZIP_MEM_VISIBLE_ADDR          ",  0x34ull},
2246e96dbe7SColin Ian King 	{"HZIP_CONSUMED_BYTE             ",  0x38ull},
22572c7a68dSZhou Wang 	{"HZIP_PRODUCED_BYTE             ",  0x40ull},
22672c7a68dSZhou Wang 	{"HZIP_COMP_INF                  ",  0x70ull},
22772c7a68dSZhou Wang 	{"HZIP_PRE_OUT                   ",  0x78ull},
22872c7a68dSZhou Wang 	{"HZIP_BD_RD                     ",  0x7cull},
22972c7a68dSZhou Wang 	{"HZIP_BD_WR                     ",  0x80ull},
23072c7a68dSZhou Wang 	{"HZIP_GET_BD_AXI_ERR_NUM        ",  0x84ull},
23172c7a68dSZhou Wang 	{"HZIP_GET_BD_PARSE_ERR_NUM      ",  0x88ull},
23272c7a68dSZhou Wang 	{"HZIP_ADD_BD_AXI_ERR_NUM        ",  0x8cull},
23372c7a68dSZhou Wang 	{"HZIP_DECOMP_STF_RELOAD_CURR_ST ",  0x94ull},
23472c7a68dSZhou Wang 	{"HZIP_DECOMP_LZ77_CURR_ST       ",  0x9cull},
23562c455caSZhou Wang };
23662c455caSZhou Wang 
237*9b0c97dfSKai Ye /* define the ZIP's dfx regs region and region length */
238*9b0c97dfSKai Ye static struct dfx_diff_registers hzip_diff_regs[] = {
239*9b0c97dfSKai Ye 	{
240*9b0c97dfSKai Ye 		.reg_offset = HZIP_CORE_DFX_BASE,
241*9b0c97dfSKai Ye 		.reg_len = HZIP_CORE_REGS_BASE_LEN,
242*9b0c97dfSKai Ye 	}, {
243*9b0c97dfSKai Ye 		.reg_offset = HZIP_CORE_DFX_COMP_0,
244*9b0c97dfSKai Ye 		.reg_len = HZIP_CORE_REGS_DFX_LEN,
245*9b0c97dfSKai Ye 	}, {
246*9b0c97dfSKai Ye 		.reg_offset = HZIP_CORE_DFX_COMP_1,
247*9b0c97dfSKai Ye 		.reg_len = HZIP_CORE_REGS_DFX_LEN,
248*9b0c97dfSKai Ye 	}, {
249*9b0c97dfSKai Ye 		.reg_offset = HZIP_CORE_DFX_DECOMP_0,
250*9b0c97dfSKai Ye 		.reg_len = HZIP_CORE_REGS_DFX_LEN,
251*9b0c97dfSKai Ye 	}, {
252*9b0c97dfSKai Ye 		.reg_offset = HZIP_CORE_DFX_DECOMP_1,
253*9b0c97dfSKai Ye 		.reg_len = HZIP_CORE_REGS_DFX_LEN,
254*9b0c97dfSKai Ye 	}, {
255*9b0c97dfSKai Ye 		.reg_offset = HZIP_CORE_DFX_DECOMP_2,
256*9b0c97dfSKai Ye 		.reg_len = HZIP_CORE_REGS_DFX_LEN,
257*9b0c97dfSKai Ye 	}, {
258*9b0c97dfSKai Ye 		.reg_offset = HZIP_CORE_DFX_DECOMP_3,
259*9b0c97dfSKai Ye 		.reg_len = HZIP_CORE_REGS_DFX_LEN,
260*9b0c97dfSKai Ye 	}, {
261*9b0c97dfSKai Ye 		.reg_offset = HZIP_CORE_DFX_DECOMP_4,
262*9b0c97dfSKai Ye 		.reg_len = HZIP_CORE_REGS_DFX_LEN,
263*9b0c97dfSKai Ye 	}, {
264*9b0c97dfSKai Ye 		.reg_offset = HZIP_CORE_DFX_DECOMP_5,
265*9b0c97dfSKai Ye 		.reg_len = HZIP_CORE_REGS_DFX_LEN,
266*9b0c97dfSKai Ye 	},
267*9b0c97dfSKai Ye };
268*9b0c97dfSKai Ye 
269*9b0c97dfSKai Ye static int hzip_diff_regs_show(struct seq_file *s, void *unused)
270*9b0c97dfSKai Ye {
271*9b0c97dfSKai Ye 	struct hisi_qm *qm = s->private;
272*9b0c97dfSKai Ye 
273*9b0c97dfSKai Ye 	hisi_qm_acc_diff_regs_dump(qm, s, qm->debug.acc_diff_regs,
274*9b0c97dfSKai Ye 					ARRAY_SIZE(hzip_diff_regs));
275*9b0c97dfSKai Ye 
276*9b0c97dfSKai Ye 	return 0;
277*9b0c97dfSKai Ye }
278*9b0c97dfSKai Ye DEFINE_SHOW_ATTRIBUTE(hzip_diff_regs);
279f8408d2bSKai Ye static const struct kernel_param_ops zip_uacce_mode_ops = {
280f8408d2bSKai Ye 	.set = uacce_mode_set,
281f8408d2bSKai Ye 	.get = param_get_int,
282f8408d2bSKai Ye };
283f8408d2bSKai Ye 
284f8408d2bSKai Ye /*
285f8408d2bSKai Ye  * uacce_mode = 0 means zip only register to crypto,
286f8408d2bSKai Ye  * uacce_mode = 1 means zip both register to crypto and uacce.
287f8408d2bSKai Ye  */
288f8408d2bSKai Ye static u32 uacce_mode = UACCE_MODE_NOUACCE;
289f8408d2bSKai Ye module_param_cb(uacce_mode, &zip_uacce_mode_ops, &uacce_mode, 0444);
290f8408d2bSKai Ye MODULE_PARM_DESC(uacce_mode, UACCE_MODE_DESC);
291f8408d2bSKai Ye 
29262c455caSZhou Wang static int pf_q_num_set(const char *val, const struct kernel_param *kp)
29362c455caSZhou Wang {
294fae74feaSShameer Kolothum 	return q_num_set(val, kp, PCI_DEVICE_ID_HUAWEI_ZIP_PF);
29562c455caSZhou Wang }
29662c455caSZhou Wang 
29762c455caSZhou Wang static const struct kernel_param_ops pf_q_num_ops = {
29862c455caSZhou Wang 	.set = pf_q_num_set,
29962c455caSZhou Wang 	.get = param_get_int,
30062c455caSZhou Wang };
30162c455caSZhou Wang 
30262c455caSZhou Wang static u32 pf_q_num = HZIP_PF_DEF_Q_NUM;
30362c455caSZhou Wang module_param_cb(pf_q_num, &pf_q_num_ops, &pf_q_num, 0444);
3040542a941SLongfang Liu MODULE_PARM_DESC(pf_q_num, "Number of queues in PF(v1 2-4096, v2 2-1024)");
30562c455caSZhou Wang 
30635ee280fSHao Fang static const struct kernel_param_ops vfs_num_ops = {
30735ee280fSHao Fang 	.set = vfs_num_set,
30835ee280fSHao Fang 	.get = param_get_int,
30935ee280fSHao Fang };
31035ee280fSHao Fang 
31139977f4bSHao Fang static u32 vfs_num;
31235ee280fSHao Fang module_param_cb(vfs_num, &vfs_num_ops, &vfs_num, 0444);
31335ee280fSHao Fang MODULE_PARM_DESC(vfs_num, "Number of VFs to enable(1-63), 0(default)");
31439977f4bSHao Fang 
31562c455caSZhou Wang static const struct pci_device_id hisi_zip_dev_ids[] = {
316fae74feaSShameer Kolothum 	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_HUAWEI_ZIP_PF) },
317fae74feaSShameer Kolothum 	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_HUAWEI_ZIP_VF) },
31862c455caSZhou Wang 	{ 0, }
31962c455caSZhou Wang };
32062c455caSZhou Wang MODULE_DEVICE_TABLE(pci, hisi_zip_dev_ids);
32162c455caSZhou Wang 
322813ec3f1SBarry Song int zip_create_qps(struct hisi_qp **qps, int qp_num, int node)
32362c455caSZhou Wang {
324813ec3f1SBarry Song 	if (node == NUMA_NO_NODE)
325813ec3f1SBarry Song 		node = cpu_to_node(smp_processor_id());
32662c455caSZhou Wang 
32718f1ab3fSShukun Tan 	return hisi_qm_alloc_qps_node(&zip_devices, qp_num, 0, node, qps);
32862c455caSZhou Wang }
32962c455caSZhou Wang 
330a5c164b1SLongfang Liu static void hisi_zip_open_sva_prefetch(struct hisi_qm *qm)
331a5c164b1SLongfang Liu {
332a5c164b1SLongfang Liu 	u32 val;
333a5c164b1SLongfang Liu 	int ret;
334a5c164b1SLongfang Liu 
335a5c164b1SLongfang Liu 	if (qm->ver < QM_HW_V3)
336a5c164b1SLongfang Liu 		return;
337a5c164b1SLongfang Liu 
338a5c164b1SLongfang Liu 	/* Enable prefetch */
339a5c164b1SLongfang Liu 	val = readl_relaxed(qm->io_base + HZIP_PREFETCH_CFG);
340a5c164b1SLongfang Liu 	val &= HZIP_PREFETCH_ENABLE;
341a5c164b1SLongfang Liu 	writel(val, qm->io_base + HZIP_PREFETCH_CFG);
342a5c164b1SLongfang Liu 
343a5c164b1SLongfang Liu 	ret = readl_relaxed_poll_timeout(qm->io_base + HZIP_PREFETCH_CFG,
344a5c164b1SLongfang Liu 					 val, !(val & HZIP_SVA_PREFETCH_DISABLE),
345a5c164b1SLongfang Liu 					 HZIP_DELAY_1_US, HZIP_POLL_TIMEOUT_US);
346a5c164b1SLongfang Liu 	if (ret)
347a5c164b1SLongfang Liu 		pci_err(qm->pdev, "failed to open sva prefetch\n");
348a5c164b1SLongfang Liu }
349a5c164b1SLongfang Liu 
350a5c164b1SLongfang Liu static void hisi_zip_close_sva_prefetch(struct hisi_qm *qm)
351a5c164b1SLongfang Liu {
352a5c164b1SLongfang Liu 	u32 val;
353a5c164b1SLongfang Liu 	int ret;
354a5c164b1SLongfang Liu 
355a5c164b1SLongfang Liu 	if (qm->ver < QM_HW_V3)
356a5c164b1SLongfang Liu 		return;
357a5c164b1SLongfang Liu 
358a5c164b1SLongfang Liu 	val = readl_relaxed(qm->io_base + HZIP_PREFETCH_CFG);
359a5c164b1SLongfang Liu 	val |= HZIP_SVA_PREFETCH_DISABLE;
360a5c164b1SLongfang Liu 	writel(val, qm->io_base + HZIP_PREFETCH_CFG);
361a5c164b1SLongfang Liu 
362a5c164b1SLongfang Liu 	ret = readl_relaxed_poll_timeout(qm->io_base + HZIP_SVA_TRANS,
363a5c164b1SLongfang Liu 					 val, !(val & HZIP_SVA_DISABLE_READY),
364a5c164b1SLongfang Liu 					 HZIP_DELAY_1_US, HZIP_POLL_TIMEOUT_US);
365a5c164b1SLongfang Liu 	if (ret)
366a5c164b1SLongfang Liu 		pci_err(qm->pdev, "failed to close sva prefetch\n");
367a5c164b1SLongfang Liu }
368a5c164b1SLongfang Liu 
369ed5fa39fSWeili Qian static void hisi_zip_enable_clock_gate(struct hisi_qm *qm)
370ed5fa39fSWeili Qian {
371ed5fa39fSWeili Qian 	u32 val;
372ed5fa39fSWeili Qian 
373ed5fa39fSWeili Qian 	if (qm->ver < QM_HW_V3)
374ed5fa39fSWeili Qian 		return;
375ed5fa39fSWeili Qian 
376ed5fa39fSWeili Qian 	val = readl(qm->io_base + HZIP_CLOCK_GATE_CTRL);
377ed5fa39fSWeili Qian 	val |= HZIP_CLOCK_GATED_EN;
378ed5fa39fSWeili Qian 	writel(val, qm->io_base + HZIP_CLOCK_GATE_CTRL);
379ed5fa39fSWeili Qian 
380ed5fa39fSWeili Qian 	val = readl(qm->io_base + HZIP_PEH_CFG_AUTO_GATE);
381ed5fa39fSWeili Qian 	val |= HZIP_PEH_CFG_AUTO_GATE_EN;
382ed5fa39fSWeili Qian 	writel(val, qm->io_base + HZIP_PEH_CFG_AUTO_GATE);
383ed5fa39fSWeili Qian }
384ed5fa39fSWeili Qian 
38584c9b780SShukun Tan static int hisi_zip_set_user_domain_and_cache(struct hisi_qm *qm)
38662c455caSZhou Wang {
38784c9b780SShukun Tan 	void __iomem *base = qm->io_base;
38862c455caSZhou Wang 
38962c455caSZhou Wang 	/* qm user domain */
39062c455caSZhou Wang 	writel(AXUSER_BASE, base + QM_ARUSER_M_CFG_1);
39162c455caSZhou Wang 	writel(ARUSER_M_CFG_ENABLE, base + QM_ARUSER_M_CFG_ENABLE);
39262c455caSZhou Wang 	writel(AXUSER_BASE, base + QM_AWUSER_M_CFG_1);
39362c455caSZhou Wang 	writel(AWUSER_M_CFG_ENABLE, base + QM_AWUSER_M_CFG_ENABLE);
39462c455caSZhou Wang 	writel(WUSER_M_CFG_ENABLE, base + QM_WUSER_M_CFG_ENABLE);
39562c455caSZhou Wang 
39662c455caSZhou Wang 	/* qm cache */
39762c455caSZhou Wang 	writel(AXI_M_CFG, base + QM_AXI_M_CFG);
39862c455caSZhou Wang 	writel(AXI_M_CFG_ENABLE, base + QM_AXI_M_CFG_ENABLE);
3992ca73193SYang Shen 
40062c455caSZhou Wang 	/* disable FLR triggered by BME(bus master enable) */
40162c455caSZhou Wang 	writel(PEH_AXUSER_CFG, base + QM_PEH_AXUSER_CFG);
40262c455caSZhou Wang 	writel(PEH_AXUSER_CFG_ENABLE, base + QM_PEH_AXUSER_CFG_ENABLE);
40362c455caSZhou Wang 
40462c455caSZhou Wang 	/* cache */
40515b0694fSYang Shen 	writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_0);
40615b0694fSYang Shen 	writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_1);
40715b0694fSYang Shen 	writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_0);
40815b0694fSYang Shen 	writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_1);
40962c455caSZhou Wang 
41062c455caSZhou Wang 	/* user domain configurations */
41162c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_BD_RUSER_32_63);
41262c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_BD_WUSER_32_63);
4139e00df71SZhangfei Gao 
414cc3292d1SWeili Qian 	if (qm->use_sva && qm->ver == QM_HW_V2) {
4159e00df71SZhangfei Gao 		writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_DATA_RUSER_32_63);
4169e00df71SZhangfei Gao 		writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_DATA_WUSER_32_63);
417808957baSYang Shen 		writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_SGL_RUSER_32_63);
4189e00df71SZhangfei Gao 	} else {
41962c455caSZhou Wang 		writel(AXUSER_BASE, base + HZIP_DATA_RUSER_32_63);
42062c455caSZhou Wang 		writel(AXUSER_BASE, base + HZIP_DATA_WUSER_32_63);
421808957baSYang Shen 		writel(AXUSER_BASE, base + HZIP_SGL_RUSER_32_63);
4229e00df71SZhangfei Gao 	}
42362c455caSZhou Wang 
42462c455caSZhou Wang 	/* let's open all compression/decompression cores */
42515b0694fSYang Shen 	writel(HZIP_DECOMP_CHECK_ENABLE | HZIP_ALL_COMP_DECOMP_EN,
42662c455caSZhou Wang 	       base + HZIP_CLOCK_GATE_CTRL);
42762c455caSZhou Wang 
4282a928693SYang Shen 	/* enable sqc,cqc writeback */
42962c455caSZhou Wang 	writel(SQC_CACHE_ENABLE | CQC_CACHE_ENABLE | SQC_CACHE_WB_ENABLE |
43062c455caSZhou Wang 	       CQC_CACHE_WB_ENABLE | FIELD_PREP(SQC_CACHE_WB_THRD, 1) |
43162c455caSZhou Wang 	       FIELD_PREP(CQC_CACHE_WB_THRD, 1), base + QM_CACHE_CTL);
43284c9b780SShukun Tan 
433ed5fa39fSWeili Qian 	hisi_zip_enable_clock_gate(qm);
434ed5fa39fSWeili Qian 
43584c9b780SShukun Tan 	return 0;
43662c455caSZhou Wang }
43762c455caSZhou Wang 
438b7da13d0SWeili Qian static void hisi_zip_master_ooo_ctrl(struct hisi_qm *qm, bool enable)
439b7da13d0SWeili Qian {
440b7da13d0SWeili Qian 	u32 val1, val2;
441b7da13d0SWeili Qian 
442b7da13d0SWeili Qian 	val1 = readl(qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
443b7da13d0SWeili Qian 	if (enable) {
444b7da13d0SWeili Qian 		val1 |= HZIP_AXI_SHUTDOWN_ENABLE;
445b7da13d0SWeili Qian 		val2 = HZIP_CORE_INT_RAS_NFE_ENABLE;
446b7da13d0SWeili Qian 	} else {
447b7da13d0SWeili Qian 		val1 &= ~HZIP_AXI_SHUTDOWN_ENABLE;
448b7da13d0SWeili Qian 		val2 = 0x0;
449b7da13d0SWeili Qian 	}
450b7da13d0SWeili Qian 
451b7da13d0SWeili Qian 	if (qm->ver > QM_HW_V2)
452b7da13d0SWeili Qian 		writel(val2, qm->io_base + HZIP_OOO_SHUTDOWN_SEL);
453b7da13d0SWeili Qian 
454b7da13d0SWeili Qian 	writel(val1, qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
455b7da13d0SWeili Qian }
456b7da13d0SWeili Qian 
457eaebf4c3SShukun Tan static void hisi_zip_hw_error_enable(struct hisi_qm *qm)
45862c455caSZhou Wang {
45962c455caSZhou Wang 	if (qm->ver == QM_HW_V1) {
460eaebf4c3SShukun Tan 		writel(HZIP_CORE_INT_MASK_ALL,
461eaebf4c3SShukun Tan 		       qm->io_base + HZIP_CORE_INT_MASK_REG);
462ee1788c6SZhou Wang 		dev_info(&qm->pdev->dev, "Does not support hw error handle\n");
46362c455caSZhou Wang 		return;
46462c455caSZhou Wang 	}
46562c455caSZhou Wang 
46662c455caSZhou Wang 	/* clear ZIP hw error source if having */
467eaebf4c3SShukun Tan 	writel(HZIP_CORE_INT_MASK_ALL, qm->io_base + HZIP_CORE_INT_SOURCE);
468eaebf4c3SShukun Tan 
469de3daf4bSShukun Tan 	/* configure error type */
4701db0016eSWeili Qian 	writel(HZIP_CORE_INT_RAS_CE_ENABLE,
4711db0016eSWeili Qian 	       qm->io_base + HZIP_CORE_INT_RAS_CE_ENB);
472de3daf4bSShukun Tan 	writel(0x0, qm->io_base + HZIP_CORE_INT_RAS_FE_ENB);
473de3daf4bSShukun Tan 	writel(HZIP_CORE_INT_RAS_NFE_ENABLE,
474de3daf4bSShukun Tan 	       qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB);
475de3daf4bSShukun Tan 
476b7da13d0SWeili Qian 	/* enable ZIP block master OOO when nfe occurs on Kunpeng930 */
477b7da13d0SWeili Qian 	hisi_zip_master_ooo_ctrl(qm, true);
4783b9c24deSWeili Qian 
4793b9c24deSWeili Qian 	/* enable ZIP hw error interrupts */
4803b9c24deSWeili Qian 	writel(0, qm->io_base + HZIP_CORE_INT_MASK_REG);
48162c455caSZhou Wang }
482eaebf4c3SShukun Tan 
483eaebf4c3SShukun Tan static void hisi_zip_hw_error_disable(struct hisi_qm *qm)
484eaebf4c3SShukun Tan {
485eaebf4c3SShukun Tan 	/* disable ZIP hw error interrupts */
486eaebf4c3SShukun Tan 	writel(HZIP_CORE_INT_MASK_ALL, qm->io_base + HZIP_CORE_INT_MASK_REG);
4877ce396faSShukun Tan 
488b7da13d0SWeili Qian 	/* disable ZIP block master OOO when nfe occurs on Kunpeng930 */
489b7da13d0SWeili Qian 	hisi_zip_master_ooo_ctrl(qm, false);
49062c455caSZhou Wang }
49162c455caSZhou Wang 
49272c7a68dSZhou Wang static inline struct hisi_qm *file_to_qm(struct ctrl_debug_file *file)
49372c7a68dSZhou Wang {
49472c7a68dSZhou Wang 	struct hisi_zip *hisi_zip = file->ctrl->hisi_zip;
49572c7a68dSZhou Wang 
49672c7a68dSZhou Wang 	return &hisi_zip->qm;
49772c7a68dSZhou Wang }
49872c7a68dSZhou Wang 
49974f5edbfSWeili Qian static u32 clear_enable_read(struct hisi_qm *qm)
50072c7a68dSZhou Wang {
50172c7a68dSZhou Wang 	return readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) &
50215b0694fSYang Shen 		     HZIP_SOFT_CTRL_CNT_CLR_CE_BIT;
50372c7a68dSZhou Wang }
50472c7a68dSZhou Wang 
50574f5edbfSWeili Qian static int clear_enable_write(struct hisi_qm *qm, u32 val)
50672c7a68dSZhou Wang {
50772c7a68dSZhou Wang 	u32 tmp;
50872c7a68dSZhou Wang 
50972c7a68dSZhou Wang 	if (val != 1 && val != 0)
51072c7a68dSZhou Wang 		return -EINVAL;
51172c7a68dSZhou Wang 
51272c7a68dSZhou Wang 	tmp = (readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) &
51315b0694fSYang Shen 	       ~HZIP_SOFT_CTRL_CNT_CLR_CE_BIT) | val;
51472c7a68dSZhou Wang 	writel(tmp, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
51572c7a68dSZhou Wang 
51672c7a68dSZhou Wang 	return  0;
51772c7a68dSZhou Wang }
51872c7a68dSZhou Wang 
51915b0694fSYang Shen static ssize_t hisi_zip_ctrl_debug_read(struct file *filp, char __user *buf,
52072c7a68dSZhou Wang 					size_t count, loff_t *pos)
52172c7a68dSZhou Wang {
52272c7a68dSZhou Wang 	struct ctrl_debug_file *file = filp->private_data;
523607c191bSWeili Qian 	struct hisi_qm *qm = file_to_qm(file);
52472c7a68dSZhou Wang 	char tbuf[HZIP_BUF_SIZE];
52572c7a68dSZhou Wang 	u32 val;
52672c7a68dSZhou Wang 	int ret;
52772c7a68dSZhou Wang 
528607c191bSWeili Qian 	ret = hisi_qm_get_dfx_access(qm);
529607c191bSWeili Qian 	if (ret)
530607c191bSWeili Qian 		return ret;
531607c191bSWeili Qian 
53272c7a68dSZhou Wang 	spin_lock_irq(&file->lock);
53372c7a68dSZhou Wang 	switch (file->index) {
53472c7a68dSZhou Wang 	case HZIP_CLEAR_ENABLE:
53574f5edbfSWeili Qian 		val = clear_enable_read(qm);
53672c7a68dSZhou Wang 		break;
53772c7a68dSZhou Wang 	default:
538607c191bSWeili Qian 		goto err_input;
53972c7a68dSZhou Wang 	}
54072c7a68dSZhou Wang 	spin_unlock_irq(&file->lock);
541607c191bSWeili Qian 
542607c191bSWeili Qian 	hisi_qm_put_dfx_access(qm);
543533b2079SYang Shen 	ret = scnprintf(tbuf, sizeof(tbuf), "%u\n", val);
54472c7a68dSZhou Wang 	return simple_read_from_buffer(buf, count, pos, tbuf, ret);
545607c191bSWeili Qian 
546607c191bSWeili Qian err_input:
547607c191bSWeili Qian 	spin_unlock_irq(&file->lock);
548607c191bSWeili Qian 	hisi_qm_put_dfx_access(qm);
549607c191bSWeili Qian 	return -EINVAL;
55072c7a68dSZhou Wang }
55172c7a68dSZhou Wang 
55215b0694fSYang Shen static ssize_t hisi_zip_ctrl_debug_write(struct file *filp,
55315b0694fSYang Shen 					 const char __user *buf,
55472c7a68dSZhou Wang 					 size_t count, loff_t *pos)
55572c7a68dSZhou Wang {
55672c7a68dSZhou Wang 	struct ctrl_debug_file *file = filp->private_data;
557607c191bSWeili Qian 	struct hisi_qm *qm = file_to_qm(file);
55872c7a68dSZhou Wang 	char tbuf[HZIP_BUF_SIZE];
55972c7a68dSZhou Wang 	unsigned long val;
56072c7a68dSZhou Wang 	int len, ret;
56172c7a68dSZhou Wang 
56272c7a68dSZhou Wang 	if (*pos != 0)
56372c7a68dSZhou Wang 		return 0;
56472c7a68dSZhou Wang 
56572c7a68dSZhou Wang 	if (count >= HZIP_BUF_SIZE)
56672c7a68dSZhou Wang 		return -ENOSPC;
56772c7a68dSZhou Wang 
56872c7a68dSZhou Wang 	len = simple_write_to_buffer(tbuf, HZIP_BUF_SIZE - 1, pos, buf, count);
56972c7a68dSZhou Wang 	if (len < 0)
57072c7a68dSZhou Wang 		return len;
57172c7a68dSZhou Wang 
57272c7a68dSZhou Wang 	tbuf[len] = '\0';
57372c7a68dSZhou Wang 	if (kstrtoul(tbuf, 0, &val))
57472c7a68dSZhou Wang 		return -EFAULT;
57572c7a68dSZhou Wang 
576607c191bSWeili Qian 	ret = hisi_qm_get_dfx_access(qm);
577607c191bSWeili Qian 	if (ret)
578607c191bSWeili Qian 		return ret;
579607c191bSWeili Qian 
58072c7a68dSZhou Wang 	spin_lock_irq(&file->lock);
58172c7a68dSZhou Wang 	switch (file->index) {
58272c7a68dSZhou Wang 	case HZIP_CLEAR_ENABLE:
58374f5edbfSWeili Qian 		ret = clear_enable_write(qm, val);
58472c7a68dSZhou Wang 		if (ret)
58572c7a68dSZhou Wang 			goto err_input;
58672c7a68dSZhou Wang 		break;
58772c7a68dSZhou Wang 	default:
58872c7a68dSZhou Wang 		ret = -EINVAL;
58972c7a68dSZhou Wang 		goto err_input;
59072c7a68dSZhou Wang 	}
59172c7a68dSZhou Wang 
592607c191bSWeili Qian 	ret = count;
59372c7a68dSZhou Wang 
59472c7a68dSZhou Wang err_input:
59572c7a68dSZhou Wang 	spin_unlock_irq(&file->lock);
596607c191bSWeili Qian 	hisi_qm_put_dfx_access(qm);
59772c7a68dSZhou Wang 	return ret;
59872c7a68dSZhou Wang }
59972c7a68dSZhou Wang 
60072c7a68dSZhou Wang static const struct file_operations ctrl_debug_fops = {
60172c7a68dSZhou Wang 	.owner = THIS_MODULE,
60272c7a68dSZhou Wang 	.open = simple_open,
60315b0694fSYang Shen 	.read = hisi_zip_ctrl_debug_read,
60415b0694fSYang Shen 	.write = hisi_zip_ctrl_debug_write,
60572c7a68dSZhou Wang };
60672c7a68dSZhou Wang 
6076621e649SLongfang Liu static int zip_debugfs_atomic64_set(void *data, u64 val)
6086621e649SLongfang Liu {
6096621e649SLongfang Liu 	if (val)
6106621e649SLongfang Liu 		return -EINVAL;
6116621e649SLongfang Liu 
6126621e649SLongfang Liu 	atomic64_set((atomic64_t *)data, 0);
6136621e649SLongfang Liu 
6146621e649SLongfang Liu 	return 0;
6156621e649SLongfang Liu }
6166621e649SLongfang Liu 
6176621e649SLongfang Liu static int zip_debugfs_atomic64_get(void *data, u64 *val)
6186621e649SLongfang Liu {
6196621e649SLongfang Liu 	*val = atomic64_read((atomic64_t *)data);
6206621e649SLongfang Liu 
6216621e649SLongfang Liu 	return 0;
6226621e649SLongfang Liu }
6236621e649SLongfang Liu 
6246621e649SLongfang Liu DEFINE_DEBUGFS_ATTRIBUTE(zip_atomic64_ops, zip_debugfs_atomic64_get,
6256621e649SLongfang Liu 			 zip_debugfs_atomic64_set, "%llu\n");
6266621e649SLongfang Liu 
6271295292dSWeili Qian static int hisi_zip_regs_show(struct seq_file *s, void *unused)
6281295292dSWeili Qian {
6291295292dSWeili Qian 	hisi_qm_regs_dump(s, s->private);
6301295292dSWeili Qian 
6311295292dSWeili Qian 	return 0;
6321295292dSWeili Qian }
6331295292dSWeili Qian 
6341295292dSWeili Qian DEFINE_SHOW_ATTRIBUTE(hisi_zip_regs);
6351295292dSWeili Qian 
6364b33f057SShukun Tan static int hisi_zip_core_debug_init(struct hisi_qm *qm)
63772c7a68dSZhou Wang {
63872c7a68dSZhou Wang 	struct device *dev = &qm->pdev->dev;
63972c7a68dSZhou Wang 	struct debugfs_regset32 *regset;
6404a97bfc7SGreg Kroah-Hartman 	struct dentry *tmp_d;
64172c7a68dSZhou Wang 	char buf[HZIP_BUF_SIZE];
64272c7a68dSZhou Wang 	int i;
64372c7a68dSZhou Wang 
64472c7a68dSZhou Wang 	for (i = 0; i < HZIP_CORE_NUM; i++) {
64572c7a68dSZhou Wang 		if (i < HZIP_COMP_CORE_NUM)
646533b2079SYang Shen 			scnprintf(buf, sizeof(buf), "comp_core%d", i);
64772c7a68dSZhou Wang 		else
648533b2079SYang Shen 			scnprintf(buf, sizeof(buf), "decomp_core%d",
649533b2079SYang Shen 				  i - HZIP_COMP_CORE_NUM);
65072c7a68dSZhou Wang 
65172c7a68dSZhou Wang 		regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
65272c7a68dSZhou Wang 		if (!regset)
65372c7a68dSZhou Wang 			return -ENOENT;
65472c7a68dSZhou Wang 
65572c7a68dSZhou Wang 		regset->regs = hzip_dfx_regs;
65672c7a68dSZhou Wang 		regset->nregs = ARRAY_SIZE(hzip_dfx_regs);
65772c7a68dSZhou Wang 		regset->base = qm->io_base + core_offsets[i];
658607c191bSWeili Qian 		regset->dev = dev;
65972c7a68dSZhou Wang 
6604b33f057SShukun Tan 		tmp_d = debugfs_create_dir(buf, qm->debug.debug_root);
6611295292dSWeili Qian 		debugfs_create_file("regs", 0444, tmp_d, regset,
6621295292dSWeili Qian 				     &hisi_zip_regs_fops);
66372c7a68dSZhou Wang 	}
66472c7a68dSZhou Wang 
66572c7a68dSZhou Wang 	return 0;
66672c7a68dSZhou Wang }
66772c7a68dSZhou Wang 
6686621e649SLongfang Liu static void hisi_zip_dfx_debug_init(struct hisi_qm *qm)
6696621e649SLongfang Liu {
670*9b0c97dfSKai Ye 	struct dfx_diff_registers *hzip_regs = qm->debug.acc_diff_regs;
6716621e649SLongfang Liu 	struct hisi_zip *zip = container_of(qm, struct hisi_zip, qm);
6726621e649SLongfang Liu 	struct hisi_zip_dfx *dfx = &zip->dfx;
6736621e649SLongfang Liu 	struct dentry *tmp_dir;
6746621e649SLongfang Liu 	void *data;
6756621e649SLongfang Liu 	int i;
6766621e649SLongfang Liu 
6776621e649SLongfang Liu 	tmp_dir = debugfs_create_dir("zip_dfx", qm->debug.debug_root);
6786621e649SLongfang Liu 	for (i = 0; i < ARRAY_SIZE(zip_dfx_files); i++) {
6796621e649SLongfang Liu 		data = (atomic64_t *)((uintptr_t)dfx + zip_dfx_files[i].offset);
6806621e649SLongfang Liu 		debugfs_create_file(zip_dfx_files[i].name,
6814b33f057SShukun Tan 				    0644, tmp_dir, data,
6826621e649SLongfang Liu 				    &zip_atomic64_ops);
6836621e649SLongfang Liu 	}
684*9b0c97dfSKai Ye 
685*9b0c97dfSKai Ye 	if (qm->fun_type == QM_HW_PF && hzip_regs)
686*9b0c97dfSKai Ye 		debugfs_create_file("diff_regs", 0444, tmp_dir,
687*9b0c97dfSKai Ye 				      qm, &hzip_diff_regs_fops);
6886621e649SLongfang Liu }
6896621e649SLongfang Liu 
6904b33f057SShukun Tan static int hisi_zip_ctrl_debug_init(struct hisi_qm *qm)
69172c7a68dSZhou Wang {
6924b33f057SShukun Tan 	struct hisi_zip *zip = container_of(qm, struct hisi_zip, qm);
69372c7a68dSZhou Wang 	int i;
69472c7a68dSZhou Wang 
695c4392b46SWeili Qian 	for (i = HZIP_CLEAR_ENABLE; i < HZIP_DEBUG_FILE_NUM; i++) {
6964b33f057SShukun Tan 		spin_lock_init(&zip->ctrl->files[i].lock);
6974b33f057SShukun Tan 		zip->ctrl->files[i].ctrl = zip->ctrl;
6984b33f057SShukun Tan 		zip->ctrl->files[i].index = i;
69972c7a68dSZhou Wang 
7004a97bfc7SGreg Kroah-Hartman 		debugfs_create_file(ctrl_debug_file_name[i], 0600,
7014b33f057SShukun Tan 				    qm->debug.debug_root,
7024b33f057SShukun Tan 				    zip->ctrl->files + i,
70372c7a68dSZhou Wang 				    &ctrl_debug_fops);
70472c7a68dSZhou Wang 	}
70572c7a68dSZhou Wang 
7064b33f057SShukun Tan 	return hisi_zip_core_debug_init(qm);
70772c7a68dSZhou Wang }
70872c7a68dSZhou Wang 
7094b33f057SShukun Tan static int hisi_zip_debugfs_init(struct hisi_qm *qm)
71072c7a68dSZhou Wang {
71172c7a68dSZhou Wang 	struct device *dev = &qm->pdev->dev;
71272c7a68dSZhou Wang 	struct dentry *dev_d;
71372c7a68dSZhou Wang 	int ret;
71472c7a68dSZhou Wang 
71572c7a68dSZhou Wang 	dev_d = debugfs_create_dir(dev_name(dev), hzip_debugfs_root);
71672c7a68dSZhou Wang 
717c31dc9feSShukun Tan 	qm->debug.sqe_mask_offset = HZIP_SQE_MASK_OFFSET;
718c31dc9feSShukun Tan 	qm->debug.sqe_mask_len = HZIP_SQE_MASK_LEN;
71972c7a68dSZhou Wang 	qm->debug.debug_root = dev_d;
720*9b0c97dfSKai Ye 	ret = hisi_qm_diff_regs_init(qm, hzip_diff_regs,
721*9b0c97dfSKai Ye 				ARRAY_SIZE(hzip_diff_regs));
722*9b0c97dfSKai Ye 	if (ret) {
723*9b0c97dfSKai Ye 		dev_warn(dev, "Failed to init ZIP diff regs!\n");
724*9b0c97dfSKai Ye 		goto debugfs_remove;
725*9b0c97dfSKai Ye 	}
726*9b0c97dfSKai Ye 
727a8ff38bdSWeili Qian 	hisi_qm_debug_init(qm);
72872c7a68dSZhou Wang 
72972c7a68dSZhou Wang 	if (qm->fun_type == QM_HW_PF) {
7304b33f057SShukun Tan 		ret = hisi_zip_ctrl_debug_init(qm);
73172c7a68dSZhou Wang 		if (ret)
73272c7a68dSZhou Wang 			goto failed_to_create;
73372c7a68dSZhou Wang 	}
73472c7a68dSZhou Wang 
7356621e649SLongfang Liu 	hisi_zip_dfx_debug_init(qm);
7366621e649SLongfang Liu 
73772c7a68dSZhou Wang 	return 0;
73872c7a68dSZhou Wang 
73972c7a68dSZhou Wang failed_to_create:
740*9b0c97dfSKai Ye 	hisi_qm_diff_regs_uninit(qm, ARRAY_SIZE(hzip_diff_regs));
741*9b0c97dfSKai Ye debugfs_remove:
74272c7a68dSZhou Wang 	debugfs_remove_recursive(hzip_debugfs_root);
74372c7a68dSZhou Wang 	return ret;
74472c7a68dSZhou Wang }
74572c7a68dSZhou Wang 
746698f9523SHao Fang /* hisi_zip_debug_regs_clear() - clear the zip debug regs */
7474b33f057SShukun Tan static void hisi_zip_debug_regs_clear(struct hisi_qm *qm)
74872c7a68dSZhou Wang {
749698f9523SHao Fang 	int i, j;
750698f9523SHao Fang 
751698f9523SHao Fang 	/* enable register read_clear bit */
752698f9523SHao Fang 	writel(HZIP_RD_CNT_CLR_CE_EN, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
753698f9523SHao Fang 	for (i = 0; i < ARRAY_SIZE(core_offsets); i++)
754698f9523SHao Fang 		for (j = 0; j < ARRAY_SIZE(hzip_dfx_regs); j++)
755698f9523SHao Fang 			readl(qm->io_base + core_offsets[i] +
756698f9523SHao Fang 			      hzip_dfx_regs[j].offset);
757698f9523SHao Fang 
758698f9523SHao Fang 	/* disable register read_clear bit */
75972c7a68dSZhou Wang 	writel(0x0, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
76072c7a68dSZhou Wang 
76172c7a68dSZhou Wang 	hisi_qm_debug_regs_clear(qm);
76272c7a68dSZhou Wang }
76372c7a68dSZhou Wang 
7644b33f057SShukun Tan static void hisi_zip_debugfs_exit(struct hisi_qm *qm)
76572c7a68dSZhou Wang {
766*9b0c97dfSKai Ye 	hisi_qm_diff_regs_uninit(qm, ARRAY_SIZE(hzip_diff_regs));
767*9b0c97dfSKai Ye 
76872c7a68dSZhou Wang 	debugfs_remove_recursive(qm->debug.debug_root);
76972c7a68dSZhou Wang 
7704b33f057SShukun Tan 	if (qm->fun_type == QM_HW_PF) {
7714b33f057SShukun Tan 		hisi_zip_debug_regs_clear(qm);
7724b33f057SShukun Tan 		qm->debug.curr_qm_qp_num = 0;
7734b33f057SShukun Tan 	}
77472c7a68dSZhou Wang }
77572c7a68dSZhou Wang 
776f826e6efSShukun Tan static void hisi_zip_log_hw_error(struct hisi_qm *qm, u32 err_sts)
777f826e6efSShukun Tan {
778f826e6efSShukun Tan 	const struct hisi_zip_hw_error *err = zip_hw_error;
779f826e6efSShukun Tan 	struct device *dev = &qm->pdev->dev;
780f826e6efSShukun Tan 	u32 err_val;
781f826e6efSShukun Tan 
782f826e6efSShukun Tan 	while (err->msg) {
783f826e6efSShukun Tan 		if (err->int_msk & err_sts) {
784f826e6efSShukun Tan 			dev_err(dev, "%s [error status=0x%x] found\n",
785f826e6efSShukun Tan 				err->msg, err->int_msk);
786f826e6efSShukun Tan 
787f826e6efSShukun Tan 			if (err->int_msk & HZIP_CORE_INT_STATUS_M_ECC) {
788f826e6efSShukun Tan 				err_val = readl(qm->io_base +
789f826e6efSShukun Tan 						HZIP_CORE_SRAM_ECC_ERR_INFO);
790f826e6efSShukun Tan 				dev_err(dev, "hisi-zip multi ecc sram num=0x%x\n",
791f826e6efSShukun Tan 					((err_val >>
792f826e6efSShukun Tan 					HZIP_SRAM_ECC_ERR_NUM_SHIFT) & 0xFF));
793f826e6efSShukun Tan 			}
794f826e6efSShukun Tan 		}
795f826e6efSShukun Tan 		err++;
796f826e6efSShukun Tan 	}
797f826e6efSShukun Tan }
798f826e6efSShukun Tan 
799f826e6efSShukun Tan static u32 hisi_zip_get_hw_err_status(struct hisi_qm *qm)
800f826e6efSShukun Tan {
801f826e6efSShukun Tan 	return readl(qm->io_base + HZIP_CORE_INT_STATUS);
802f826e6efSShukun Tan }
803f826e6efSShukun Tan 
80484c9b780SShukun Tan static void hisi_zip_clear_hw_err_status(struct hisi_qm *qm, u32 err_sts)
80584c9b780SShukun Tan {
80684c9b780SShukun Tan 	writel(err_sts, qm->io_base + HZIP_CORE_INT_SOURCE);
80784c9b780SShukun Tan }
80884c9b780SShukun Tan 
80984c9b780SShukun Tan static void hisi_zip_open_axi_master_ooo(struct hisi_qm *qm)
81084c9b780SShukun Tan {
81184c9b780SShukun Tan 	u32 val;
81284c9b780SShukun Tan 
81384c9b780SShukun Tan 	val = readl(qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
81484c9b780SShukun Tan 
81584c9b780SShukun Tan 	writel(val & ~HZIP_AXI_SHUTDOWN_ENABLE,
81684c9b780SShukun Tan 	       qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
81784c9b780SShukun Tan 
81884c9b780SShukun Tan 	writel(val | HZIP_AXI_SHUTDOWN_ENABLE,
81984c9b780SShukun Tan 	       qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
82084c9b780SShukun Tan }
82184c9b780SShukun Tan 
82284c9b780SShukun Tan static void hisi_zip_close_axi_master_ooo(struct hisi_qm *qm)
82384c9b780SShukun Tan {
82484c9b780SShukun Tan 	u32 nfe_enb;
82584c9b780SShukun Tan 
82684c9b780SShukun Tan 	/* Disable ECC Mbit error report. */
82784c9b780SShukun Tan 	nfe_enb = readl(qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB);
82884c9b780SShukun Tan 	writel(nfe_enb & ~HZIP_CORE_INT_STATUS_M_ECC,
82984c9b780SShukun Tan 	       qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB);
83084c9b780SShukun Tan 
83184c9b780SShukun Tan 	/* Inject zip ECC Mbit error to block master ooo. */
83284c9b780SShukun Tan 	writel(HZIP_CORE_INT_STATUS_M_ECC,
83384c9b780SShukun Tan 	       qm->io_base + HZIP_CORE_INT_SET);
83484c9b780SShukun Tan }
83584c9b780SShukun Tan 
836d9e21600SWeili Qian static void hisi_zip_err_info_init(struct hisi_qm *qm)
837d9e21600SWeili Qian {
838d9e21600SWeili Qian 	struct hisi_qm_err_info *err_info = &qm->err_info;
839d9e21600SWeili Qian 
840d9e21600SWeili Qian 	err_info->ce = QM_BASE_CE;
841d9e21600SWeili Qian 	err_info->fe = 0;
842d9e21600SWeili Qian 	err_info->ecc_2bits_mask = HZIP_CORE_INT_STATUS_M_ECC;
843d9e21600SWeili Qian 	err_info->dev_ce_mask = HZIP_CORE_INT_RAS_CE_ENABLE;
844d9e21600SWeili Qian 	err_info->msi_wr_port = HZIP_WR_PORT;
845d9e21600SWeili Qian 	err_info->acpi_rst = "ZRST";
846d9e21600SWeili Qian 	err_info->nfe = QM_BASE_NFE | QM_ACC_WB_NOT_READY_TIMEOUT;
847b7220a74SWeili Qian 
848b7220a74SWeili Qian 	if (qm->ver >= QM_HW_V3)
849b7220a74SWeili Qian 		err_info->nfe |= QM_ACC_DO_TASK_TIMEOUT;
850d9e21600SWeili Qian }
851d9e21600SWeili Qian 
852eaebf4c3SShukun Tan static const struct hisi_qm_err_ini hisi_zip_err_ini = {
85384c9b780SShukun Tan 	.hw_init		= hisi_zip_set_user_domain_and_cache,
854eaebf4c3SShukun Tan 	.hw_err_enable		= hisi_zip_hw_error_enable,
855eaebf4c3SShukun Tan 	.hw_err_disable		= hisi_zip_hw_error_disable,
856f826e6efSShukun Tan 	.get_dev_hw_err_status	= hisi_zip_get_hw_err_status,
85784c9b780SShukun Tan 	.clear_dev_hw_err_status = hisi_zip_clear_hw_err_status,
858f826e6efSShukun Tan 	.log_dev_hw_err		= hisi_zip_log_hw_error,
85984c9b780SShukun Tan 	.open_axi_master_ooo	= hisi_zip_open_axi_master_ooo,
86084c9b780SShukun Tan 	.close_axi_master_ooo	= hisi_zip_close_axi_master_ooo,
861a5c164b1SLongfang Liu 	.open_sva_prefetch	= hisi_zip_open_sva_prefetch,
862a5c164b1SLongfang Liu 	.close_sva_prefetch	= hisi_zip_close_sva_prefetch,
863d9e21600SWeili Qian 	.err_info_init		= hisi_zip_err_info_init,
864eaebf4c3SShukun Tan };
86562c455caSZhou Wang 
86662c455caSZhou Wang static int hisi_zip_pf_probe_init(struct hisi_zip *hisi_zip)
86762c455caSZhou Wang {
86862c455caSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
86962c455caSZhou Wang 	struct hisi_zip_ctrl *ctrl;
87062c455caSZhou Wang 
87162c455caSZhou Wang 	ctrl = devm_kzalloc(&qm->pdev->dev, sizeof(*ctrl), GFP_KERNEL);
87262c455caSZhou Wang 	if (!ctrl)
87362c455caSZhou Wang 		return -ENOMEM;
87462c455caSZhou Wang 
87562c455caSZhou Wang 	hisi_zip->ctrl = ctrl;
87662c455caSZhou Wang 	ctrl->hisi_zip = hisi_zip;
877eaebf4c3SShukun Tan 	qm->err_ini = &hisi_zip_err_ini;
878d9e21600SWeili Qian 	qm->err_ini->err_info_init(qm);
879eaebf4c3SShukun Tan 
88084c9b780SShukun Tan 	hisi_zip_set_user_domain_and_cache(qm);
881a5c164b1SLongfang Liu 	hisi_zip_open_sva_prefetch(qm);
882eaebf4c3SShukun Tan 	hisi_qm_dev_err_init(qm);
8834b33f057SShukun Tan 	hisi_zip_debug_regs_clear(qm);
88462c455caSZhou Wang 
88562c455caSZhou Wang 	return 0;
88662c455caSZhou Wang }
88762c455caSZhou Wang 
888cfd66a66SLongfang Liu static int hisi_zip_qm_init(struct hisi_qm *qm, struct pci_dev *pdev)
88939977f4bSHao Fang {
8901dc44035SYang Shen 	int ret;
8911dc44035SYang Shen 
89239977f4bSHao Fang 	qm->pdev = pdev;
89358ca0060SWeili Qian 	qm->ver = pdev->revision;
894223a41f5SYang Shen 	if (pdev->revision >= QM_HW_V3)
895223a41f5SYang Shen 		qm->algs = "zlib\ngzip\ndeflate\nlz77_zstd";
896223a41f5SYang Shen 	else
8979e00df71SZhangfei Gao 		qm->algs = "zlib\ngzip";
898f8408d2bSKai Ye 	qm->mode = uacce_mode;
89939977f4bSHao Fang 	qm->sqe_size = HZIP_SQE_SIZE;
90039977f4bSHao Fang 	qm->dev_name = hisi_zip_name;
901d9701f8dSWeili Qian 
902fae74feaSShameer Kolothum 	qm->fun_type = (pdev->device == PCI_DEVICE_ID_HUAWEI_ZIP_PF) ?
903cfd66a66SLongfang Liu 			QM_HW_PF : QM_HW_VF;
904d9701f8dSWeili Qian 	if (qm->fun_type == QM_HW_PF) {
905d9701f8dSWeili Qian 		qm->qp_base = HZIP_PF_DEF_Q_BASE;
906d9701f8dSWeili Qian 		qm->qp_num = pf_q_num;
9072fcb4cc3SSihang Chen 		qm->debug.curr_qm_qp_num = pf_q_num;
908d9701f8dSWeili Qian 		qm->qm_list = &zip_devices;
909d9701f8dSWeili Qian 	} else if (qm->fun_type == QM_HW_VF && qm->ver == QM_HW_V1) {
910d9701f8dSWeili Qian 		/*
911d9701f8dSWeili Qian 		 * have no way to get qm configure in VM in v1 hardware,
912d9701f8dSWeili Qian 		 * so currently force PF to uses HZIP_PF_DEF_Q_NUM, and force
913d9701f8dSWeili Qian 		 * to trigger only one VF in v1 hardware.
914d9701f8dSWeili Qian 		 *
915d9701f8dSWeili Qian 		 * v2 hardware has no such problem.
916d9701f8dSWeili Qian 		 */
917d9701f8dSWeili Qian 		qm->qp_base = HZIP_PF_DEF_Q_NUM;
918d9701f8dSWeili Qian 		qm->qp_num = HZIP_QUEUE_NUM_V1 - HZIP_PF_DEF_Q_NUM;
919d9701f8dSWeili Qian 	}
920cfd66a66SLongfang Liu 
9211dc44035SYang Shen 	qm->wq = alloc_workqueue("%s", WQ_HIGHPRI | WQ_MEM_RECLAIM |
9221dc44035SYang Shen 				 WQ_UNBOUND, num_online_cpus(),
9231dc44035SYang Shen 				 pci_name(qm->pdev));
9241dc44035SYang Shen 	if (!qm->wq) {
9251dc44035SYang Shen 		pci_err(qm->pdev, "fail to alloc workqueue\n");
9261dc44035SYang Shen 		return -ENOMEM;
9271dc44035SYang Shen 	}
9281dc44035SYang Shen 
9291dc44035SYang Shen 	ret = hisi_qm_init(qm);
9301dc44035SYang Shen 	if (ret)
9311dc44035SYang Shen 		destroy_workqueue(qm->wq);
9321dc44035SYang Shen 
9331dc44035SYang Shen 	return ret;
9341dc44035SYang Shen }
9351dc44035SYang Shen 
9361dc44035SYang Shen static void hisi_zip_qm_uninit(struct hisi_qm *qm)
9371dc44035SYang Shen {
9381dc44035SYang Shen 	hisi_qm_uninit(qm);
9391dc44035SYang Shen 	destroy_workqueue(qm->wq);
94039977f4bSHao Fang }
94139977f4bSHao Fang 
942cfd66a66SLongfang Liu static int hisi_zip_probe_init(struct hisi_zip *hisi_zip)
943cfd66a66SLongfang Liu {
94438a9eb81SKai Ye 	u32 type_rate = HZIP_SHAPER_RATE_COMPRESS;
945cfd66a66SLongfang Liu 	struct hisi_qm *qm = &hisi_zip->qm;
946cfd66a66SLongfang Liu 	int ret;
947cfd66a66SLongfang Liu 
94839977f4bSHao Fang 	if (qm->fun_type == QM_HW_PF) {
94939977f4bSHao Fang 		ret = hisi_zip_pf_probe_init(hisi_zip);
95039977f4bSHao Fang 		if (ret)
95139977f4bSHao Fang 			return ret;
95238a9eb81SKai Ye 		/* enable shaper type 0 */
95338a9eb81SKai Ye 		if (qm->ver >= QM_HW_V3) {
95438a9eb81SKai Ye 			type_rate |= QM_SHAPER_ENABLE;
95538a9eb81SKai Ye 
95638a9eb81SKai Ye 			/* ZIP need to enable shaper type 1 */
95738a9eb81SKai Ye 			type_rate |= HZIP_SHAPER_RATE_DECOMPRESS << QM_SHAPER_TYPE1_OFFSET;
95838a9eb81SKai Ye 			qm->type_rate = type_rate;
95938a9eb81SKai Ye 		}
960cfd66a66SLongfang Liu 	}
961cfd66a66SLongfang Liu 
962cfd66a66SLongfang Liu 	return 0;
963cfd66a66SLongfang Liu }
964cfd66a66SLongfang Liu 
965cfd66a66SLongfang Liu static int hisi_zip_probe(struct pci_dev *pdev, const struct pci_device_id *id)
966cfd66a66SLongfang Liu {
967cfd66a66SLongfang Liu 	struct hisi_zip *hisi_zip;
968cfd66a66SLongfang Liu 	struct hisi_qm *qm;
969cfd66a66SLongfang Liu 	int ret;
970cfd66a66SLongfang Liu 
971cfd66a66SLongfang Liu 	hisi_zip = devm_kzalloc(&pdev->dev, sizeof(*hisi_zip), GFP_KERNEL);
972cfd66a66SLongfang Liu 	if (!hisi_zip)
973cfd66a66SLongfang Liu 		return -ENOMEM;
974cfd66a66SLongfang Liu 
975cfd66a66SLongfang Liu 	qm = &hisi_zip->qm;
976cfd66a66SLongfang Liu 
977cfd66a66SLongfang Liu 	ret = hisi_zip_qm_init(qm, pdev);
978cfd66a66SLongfang Liu 	if (ret) {
979cfd66a66SLongfang Liu 		pci_err(pdev, "Failed to init ZIP QM (%d)!\n", ret);
980cfd66a66SLongfang Liu 		return ret;
981cfd66a66SLongfang Liu 	}
982cfd66a66SLongfang Liu 
983cfd66a66SLongfang Liu 	ret = hisi_zip_probe_init(hisi_zip);
984cfd66a66SLongfang Liu 	if (ret) {
985cfd66a66SLongfang Liu 		pci_err(pdev, "Failed to probe (%d)!\n", ret);
986cfd66a66SLongfang Liu 		goto err_qm_uninit;
98739977f4bSHao Fang 	}
98839977f4bSHao Fang 
98939977f4bSHao Fang 	ret = hisi_qm_start(qm);
99039977f4bSHao Fang 	if (ret)
9913d29e98dSYang Shen 		goto err_dev_err_uninit;
99239977f4bSHao Fang 
9934b33f057SShukun Tan 	ret = hisi_zip_debugfs_init(qm);
99439977f4bSHao Fang 	if (ret)
995b1a25820SYang Shen 		pci_err(pdev, "failed to init debugfs (%d)!\n", ret);
99639977f4bSHao Fang 
9973d29e98dSYang Shen 	ret = hisi_qm_alg_register(qm, &zip_devices);
9983d29e98dSYang Shen 	if (ret < 0) {
999b1a25820SYang Shen 		pci_err(pdev, "failed to register driver to crypto!\n");
10003d29e98dSYang Shen 		goto err_qm_stop;
10013d29e98dSYang Shen 	}
100239977f4bSHao Fang 
10039e00df71SZhangfei Gao 	if (qm->uacce) {
10049e00df71SZhangfei Gao 		ret = uacce_register(qm->uacce);
1005b1a25820SYang Shen 		if (ret) {
1006b1a25820SYang Shen 			pci_err(pdev, "failed to register uacce (%d)!\n", ret);
10073d29e98dSYang Shen 			goto err_qm_alg_unregister;
10089e00df71SZhangfei Gao 		}
1009b1a25820SYang Shen 	}
10109e00df71SZhangfei Gao 
101139977f4bSHao Fang 	if (qm->fun_type == QM_HW_PF && vfs_num > 0) {
1012cd1b7ae3SShukun Tan 		ret = hisi_qm_sriov_enable(pdev, vfs_num);
101339977f4bSHao Fang 		if (ret < 0)
10143d29e98dSYang Shen 			goto err_qm_alg_unregister;
101539977f4bSHao Fang 	}
101639977f4bSHao Fang 
1017607c191bSWeili Qian 	hisi_qm_pm_init(qm);
1018607c191bSWeili Qian 
101939977f4bSHao Fang 	return 0;
102039977f4bSHao Fang 
10213d29e98dSYang Shen err_qm_alg_unregister:
10223d29e98dSYang Shen 	hisi_qm_alg_unregister(qm, &zip_devices);
10233d29e98dSYang Shen 
10243d29e98dSYang Shen err_qm_stop:
10254b33f057SShukun Tan 	hisi_zip_debugfs_exit(qm);
1026e88dd6e1SYang Shen 	hisi_qm_stop(qm, QM_NORMAL);
10273d29e98dSYang Shen 
10283d29e98dSYang Shen err_dev_err_uninit:
10293d29e98dSYang Shen 	hisi_qm_dev_err_uninit(qm);
10303d29e98dSYang Shen 
103139977f4bSHao Fang err_qm_uninit:
10321dc44035SYang Shen 	hisi_zip_qm_uninit(qm);
1033cfd66a66SLongfang Liu 
103439977f4bSHao Fang 	return ret;
103539977f4bSHao Fang }
103639977f4bSHao Fang 
103762c455caSZhou Wang static void hisi_zip_remove(struct pci_dev *pdev)
103862c455caSZhou Wang {
1039d8140b87SYang Shen 	struct hisi_qm *qm = pci_get_drvdata(pdev);
104062c455caSZhou Wang 
1041607c191bSWeili Qian 	hisi_qm_pm_uninit(qm);
1042daa31783SWeili Qian 	hisi_qm_wait_task_finish(qm, &zip_devices);
10433d29e98dSYang Shen 	hisi_qm_alg_unregister(qm, &zip_devices);
10443d29e98dSYang Shen 
1045619e464aSShukun Tan 	if (qm->fun_type == QM_HW_PF && qm->vfs_num)
10463e9954feSWeili Qian 		hisi_qm_sriov_disable(pdev, true);
104779e09f30SZhou Wang 
10484b33f057SShukun Tan 	hisi_zip_debugfs_exit(qm);
1049e88dd6e1SYang Shen 	hisi_qm_stop(qm, QM_NORMAL);
1050eaebf4c3SShukun Tan 	hisi_qm_dev_err_uninit(qm);
10511dc44035SYang Shen 	hisi_zip_qm_uninit(qm);
105262c455caSZhou Wang }
105362c455caSZhou Wang 
1054607c191bSWeili Qian static const struct dev_pm_ops hisi_zip_pm_ops = {
1055607c191bSWeili Qian 	SET_RUNTIME_PM_OPS(hisi_qm_suspend, hisi_qm_resume, NULL)
1056607c191bSWeili Qian };
1057607c191bSWeili Qian 
105862c455caSZhou Wang static const struct pci_error_handlers hisi_zip_err_handler = {
1059f826e6efSShukun Tan 	.error_detected	= hisi_qm_dev_err_detected,
106084c9b780SShukun Tan 	.slot_reset	= hisi_qm_dev_slot_reset,
10617ce396faSShukun Tan 	.reset_prepare	= hisi_qm_reset_prepare,
10627ce396faSShukun Tan 	.reset_done	= hisi_qm_reset_done,
106362c455caSZhou Wang };
106462c455caSZhou Wang 
106562c455caSZhou Wang static struct pci_driver hisi_zip_pci_driver = {
106662c455caSZhou Wang 	.name			= "hisi_zip",
106762c455caSZhou Wang 	.id_table		= hisi_zip_dev_ids,
106862c455caSZhou Wang 	.probe			= hisi_zip_probe,
106962c455caSZhou Wang 	.remove			= hisi_zip_remove,
1070bf6a7a5aSArnd Bergmann 	.sriov_configure	= IS_ENABLED(CONFIG_PCI_IOV) ?
1071cd1b7ae3SShukun Tan 					hisi_qm_sriov_configure : NULL,
107262c455caSZhou Wang 	.err_handler		= &hisi_zip_err_handler,
107364dfe495SYang Shen 	.shutdown		= hisi_qm_dev_shutdown,
1074607c191bSWeili Qian 	.driver.pm		= &hisi_zip_pm_ops,
107562c455caSZhou Wang };
107662c455caSZhou Wang 
1077442fbc09SShameer Kolothum struct pci_driver *hisi_zip_get_pf_driver(void)
1078442fbc09SShameer Kolothum {
1079442fbc09SShameer Kolothum 	return &hisi_zip_pci_driver;
1080442fbc09SShameer Kolothum }
1081442fbc09SShameer Kolothum EXPORT_SYMBOL_GPL(hisi_zip_get_pf_driver);
1082442fbc09SShameer Kolothum 
108372c7a68dSZhou Wang static void hisi_zip_register_debugfs(void)
108472c7a68dSZhou Wang {
108572c7a68dSZhou Wang 	if (!debugfs_initialized())
108672c7a68dSZhou Wang 		return;
108772c7a68dSZhou Wang 
108872c7a68dSZhou Wang 	hzip_debugfs_root = debugfs_create_dir("hisi_zip", NULL);
108972c7a68dSZhou Wang }
109072c7a68dSZhou Wang 
109172c7a68dSZhou Wang static void hisi_zip_unregister_debugfs(void)
109272c7a68dSZhou Wang {
109372c7a68dSZhou Wang 	debugfs_remove_recursive(hzip_debugfs_root);
109472c7a68dSZhou Wang }
109572c7a68dSZhou Wang 
109662c455caSZhou Wang static int __init hisi_zip_init(void)
109762c455caSZhou Wang {
109862c455caSZhou Wang 	int ret;
109962c455caSZhou Wang 
110018f1ab3fSShukun Tan 	hisi_qm_init_list(&zip_devices);
110172c7a68dSZhou Wang 	hisi_zip_register_debugfs();
110272c7a68dSZhou Wang 
110362c455caSZhou Wang 	ret = pci_register_driver(&hisi_zip_pci_driver);
110462c455caSZhou Wang 	if (ret < 0) {
110572c7a68dSZhou Wang 		hisi_zip_unregister_debugfs();
11062ca73193SYang Shen 		pr_err("Failed to register pci driver.\n");
11072ca73193SYang Shen 	}
110872c7a68dSZhou Wang 
110962c455caSZhou Wang 	return ret;
111062c455caSZhou Wang }
111162c455caSZhou Wang 
111262c455caSZhou Wang static void __exit hisi_zip_exit(void)
111362c455caSZhou Wang {
111462c455caSZhou Wang 	pci_unregister_driver(&hisi_zip_pci_driver);
111572c7a68dSZhou Wang 	hisi_zip_unregister_debugfs();
111662c455caSZhou Wang }
111762c455caSZhou Wang 
111862c455caSZhou Wang module_init(hisi_zip_init);
111962c455caSZhou Wang module_exit(hisi_zip_exit);
112062c455caSZhou Wang 
112162c455caSZhou Wang MODULE_LICENSE("GPL v2");
112262c455caSZhou Wang MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>");
112362c455caSZhou Wang MODULE_DESCRIPTION("Driver for HiSilicon ZIP accelerator");
1124