xref: /openbmc/linux/drivers/crypto/hisilicon/zip/zip_main.c (revision c4392b46ee95be9815e682a1c8cb0aa2f92f07e2)
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>
1272c7a68dSZhou Wang #include <linux/seq_file.h>
1362c455caSZhou Wang #include <linux/topology.h>
149e00df71SZhangfei Gao #include <linux/uacce.h>
1562c455caSZhou Wang #include "zip.h"
1662c455caSZhou Wang 
1762c455caSZhou Wang #define PCI_DEVICE_ID_ZIP_PF		0xa250
1879e09f30SZhou Wang #define PCI_DEVICE_ID_ZIP_VF		0xa251
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 
5272c7a68dSZhou Wang #define HZIP_CORE_DEBUG_COMP_0		0x302000
5372c7a68dSZhou Wang #define HZIP_CORE_DEBUG_COMP_1		0x303000
5472c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_0	0x304000
5572c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_1	0x305000
5672c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_2	0x306000
5772c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_3	0x307000
5872c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_4	0x308000
5972c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_5	0x309000
6062c455caSZhou Wang 
6162c455caSZhou Wang #define HZIP_CORE_INT_SOURCE		0x3010A0
62eaebf4c3SShukun Tan #define HZIP_CORE_INT_MASK_REG		0x3010A4
6384c9b780SShukun Tan #define HZIP_CORE_INT_SET		0x3010A8
6462c455caSZhou Wang #define HZIP_CORE_INT_STATUS		0x3010AC
6562c455caSZhou Wang #define HZIP_CORE_INT_STATUS_M_ECC	BIT(1)
6662c455caSZhou Wang #define HZIP_CORE_SRAM_ECC_ERR_INFO	0x301148
67de3daf4bSShukun Tan #define HZIP_CORE_INT_RAS_CE_ENB	0x301160
681db0016eSWeili Qian #define HZIP_CORE_INT_RAS_CE_ENABLE	0x1
69de3daf4bSShukun Tan #define HZIP_CORE_INT_RAS_NFE_ENB	0x301164
70de3daf4bSShukun Tan #define HZIP_CORE_INT_RAS_FE_ENB        0x301168
71de3daf4bSShukun Tan #define HZIP_CORE_INT_RAS_NFE_ENABLE	0x7FE
72f826e6efSShukun Tan #define HZIP_SRAM_ECC_ERR_NUM_SHIFT	16
73f826e6efSShukun Tan #define HZIP_SRAM_ECC_ERR_ADDR_SHIFT	24
74eaebf4c3SShukun Tan #define HZIP_CORE_INT_MASK_ALL		GENMASK(10, 0)
7572c7a68dSZhou Wang #define HZIP_COMP_CORE_NUM		2
7672c7a68dSZhou Wang #define HZIP_DECOMP_CORE_NUM		6
7772c7a68dSZhou Wang #define HZIP_CORE_NUM			(HZIP_COMP_CORE_NUM + \
7872c7a68dSZhou Wang 					 HZIP_DECOMP_CORE_NUM)
7962c455caSZhou Wang #define HZIP_SQE_SIZE			128
8072c7a68dSZhou Wang #define HZIP_SQ_SIZE			(HZIP_SQE_SIZE * QM_Q_DEPTH)
8162c455caSZhou Wang #define HZIP_PF_DEF_Q_NUM		64
8262c455caSZhou Wang #define HZIP_PF_DEF_Q_BASE		0
8362c455caSZhou Wang 
8472c7a68dSZhou Wang #define HZIP_SOFT_CTRL_CNT_CLR_CE	0x301000
8515b0694fSYang Shen #define HZIP_SOFT_CTRL_CNT_CLR_CE_BIT	BIT(0)
8684c9b780SShukun Tan #define HZIP_SOFT_CTRL_ZIP_CONTROL	0x30100C
8784c9b780SShukun Tan #define HZIP_AXI_SHUTDOWN_ENABLE	BIT(14)
8884c9b780SShukun Tan #define HZIP_WR_PORT			BIT(11)
8962c455caSZhou Wang 
9072c7a68dSZhou Wang #define HZIP_BUF_SIZE			22
91c31dc9feSShukun Tan #define HZIP_SQE_MASK_OFFSET		64
92c31dc9feSShukun Tan #define HZIP_SQE_MASK_LEN		48
9362c455caSZhou Wang 
94698f9523SHao Fang #define HZIP_CNT_CLR_CE_EN		BIT(0)
95698f9523SHao Fang #define HZIP_RO_CNT_CLR_CE_EN		BIT(2)
96698f9523SHao Fang #define HZIP_RD_CNT_CLR_CE_EN		(HZIP_CNT_CLR_CE_EN | \
97698f9523SHao Fang 					 HZIP_RO_CNT_CLR_CE_EN)
98698f9523SHao Fang 
9962c455caSZhou Wang static const char hisi_zip_name[] = "hisi_zip";
10072c7a68dSZhou Wang static struct dentry *hzip_debugfs_root;
10162c455caSZhou Wang 
10262c455caSZhou Wang struct hisi_zip_hw_error {
10362c455caSZhou Wang 	u32 int_msk;
10462c455caSZhou Wang 	const char *msg;
10562c455caSZhou Wang };
10662c455caSZhou Wang 
1076621e649SLongfang Liu struct zip_dfx_item {
1086621e649SLongfang Liu 	const char *name;
1096621e649SLongfang Liu 	u32 offset;
1106621e649SLongfang Liu };
1116621e649SLongfang Liu 
1123d29e98dSYang Shen static struct hisi_qm_list zip_devices = {
1133d29e98dSYang Shen 	.register_to_crypto	= hisi_zip_register_to_crypto,
1143d29e98dSYang Shen 	.unregister_from_crypto	= hisi_zip_unregister_from_crypto,
1153d29e98dSYang Shen };
1163d29e98dSYang Shen 
1176621e649SLongfang Liu static struct zip_dfx_item zip_dfx_files[] = {
1186621e649SLongfang Liu 	{"send_cnt", offsetof(struct hisi_zip_dfx, send_cnt)},
1196621e649SLongfang Liu 	{"recv_cnt", offsetof(struct hisi_zip_dfx, recv_cnt)},
1206621e649SLongfang Liu 	{"send_busy_cnt", offsetof(struct hisi_zip_dfx, send_busy_cnt)},
1216621e649SLongfang Liu 	{"err_bd_cnt", offsetof(struct hisi_zip_dfx, err_bd_cnt)},
1226621e649SLongfang Liu };
1236621e649SLongfang Liu 
12462c455caSZhou Wang static const struct hisi_zip_hw_error zip_hw_error[] = {
12562c455caSZhou Wang 	{ .int_msk = BIT(0), .msg = "zip_ecc_1bitt_err" },
12662c455caSZhou Wang 	{ .int_msk = BIT(1), .msg = "zip_ecc_2bit_err" },
12762c455caSZhou Wang 	{ .int_msk = BIT(2), .msg = "zip_axi_rresp_err" },
12862c455caSZhou Wang 	{ .int_msk = BIT(3), .msg = "zip_axi_bresp_err" },
12962c455caSZhou Wang 	{ .int_msk = BIT(4), .msg = "zip_src_addr_parse_err" },
13062c455caSZhou Wang 	{ .int_msk = BIT(5), .msg = "zip_dst_addr_parse_err" },
13162c455caSZhou Wang 	{ .int_msk = BIT(6), .msg = "zip_pre_in_addr_err" },
13262c455caSZhou Wang 	{ .int_msk = BIT(7), .msg = "zip_pre_in_data_err" },
13362c455caSZhou Wang 	{ .int_msk = BIT(8), .msg = "zip_com_inf_err" },
13462c455caSZhou Wang 	{ .int_msk = BIT(9), .msg = "zip_enc_inf_err" },
13562c455caSZhou Wang 	{ .int_msk = BIT(10), .msg = "zip_pre_out_err" },
13662c455caSZhou Wang 	{ /* sentinel */ }
13762c455caSZhou Wang };
13862c455caSZhou Wang 
13972c7a68dSZhou Wang enum ctrl_debug_file_index {
14072c7a68dSZhou Wang 	HZIP_CLEAR_ENABLE,
14172c7a68dSZhou Wang 	HZIP_DEBUG_FILE_NUM,
14272c7a68dSZhou Wang };
14372c7a68dSZhou Wang 
14472c7a68dSZhou Wang static const char * const ctrl_debug_file_name[] = {
14572c7a68dSZhou Wang 	[HZIP_CLEAR_ENABLE] = "clear_enable",
14672c7a68dSZhou Wang };
14772c7a68dSZhou Wang 
14872c7a68dSZhou Wang struct ctrl_debug_file {
14972c7a68dSZhou Wang 	enum ctrl_debug_file_index index;
15072c7a68dSZhou Wang 	spinlock_t lock;
15172c7a68dSZhou Wang 	struct hisi_zip_ctrl *ctrl;
15272c7a68dSZhou Wang };
15372c7a68dSZhou Wang 
15462c455caSZhou Wang /*
15562c455caSZhou Wang  * One ZIP controller has one PF and multiple VFs, some global configurations
15662c455caSZhou Wang  * which PF has need this structure.
15762c455caSZhou Wang  *
15862c455caSZhou Wang  * Just relevant for PF.
15962c455caSZhou Wang  */
16062c455caSZhou Wang struct hisi_zip_ctrl {
16162c455caSZhou Wang 	struct hisi_zip *hisi_zip;
16272c7a68dSZhou Wang 	struct ctrl_debug_file files[HZIP_DEBUG_FILE_NUM];
16372c7a68dSZhou Wang };
16472c7a68dSZhou Wang 
16572c7a68dSZhou Wang enum {
16672c7a68dSZhou Wang 	HZIP_COMP_CORE0,
16772c7a68dSZhou Wang 	HZIP_COMP_CORE1,
16872c7a68dSZhou Wang 	HZIP_DECOMP_CORE0,
16972c7a68dSZhou Wang 	HZIP_DECOMP_CORE1,
17072c7a68dSZhou Wang 	HZIP_DECOMP_CORE2,
17172c7a68dSZhou Wang 	HZIP_DECOMP_CORE3,
17272c7a68dSZhou Wang 	HZIP_DECOMP_CORE4,
17372c7a68dSZhou Wang 	HZIP_DECOMP_CORE5,
17472c7a68dSZhou Wang };
17572c7a68dSZhou Wang 
17672c7a68dSZhou Wang static const u64 core_offsets[] = {
17772c7a68dSZhou Wang 	[HZIP_COMP_CORE0]   = 0x302000,
17872c7a68dSZhou Wang 	[HZIP_COMP_CORE1]   = 0x303000,
17972c7a68dSZhou Wang 	[HZIP_DECOMP_CORE0] = 0x304000,
18072c7a68dSZhou Wang 	[HZIP_DECOMP_CORE1] = 0x305000,
18172c7a68dSZhou Wang 	[HZIP_DECOMP_CORE2] = 0x306000,
18272c7a68dSZhou Wang 	[HZIP_DECOMP_CORE3] = 0x307000,
18372c7a68dSZhou Wang 	[HZIP_DECOMP_CORE4] = 0x308000,
18472c7a68dSZhou Wang 	[HZIP_DECOMP_CORE5] = 0x309000,
18572c7a68dSZhou Wang };
18672c7a68dSZhou Wang 
1878f68659bSRikard Falkeborn static const struct debugfs_reg32 hzip_dfx_regs[] = {
18872c7a68dSZhou Wang 	{"HZIP_GET_BD_NUM                ",  0x00ull},
18972c7a68dSZhou Wang 	{"HZIP_GET_RIGHT_BD              ",  0x04ull},
19072c7a68dSZhou Wang 	{"HZIP_GET_ERROR_BD              ",  0x08ull},
19172c7a68dSZhou Wang 	{"HZIP_DONE_BD_NUM               ",  0x0cull},
19272c7a68dSZhou Wang 	{"HZIP_WORK_CYCLE                ",  0x10ull},
19372c7a68dSZhou Wang 	{"HZIP_IDLE_CYCLE                ",  0x18ull},
19472c7a68dSZhou Wang 	{"HZIP_MAX_DELAY                 ",  0x20ull},
19572c7a68dSZhou Wang 	{"HZIP_MIN_DELAY                 ",  0x24ull},
19672c7a68dSZhou Wang 	{"HZIP_AVG_DELAY                 ",  0x28ull},
19772c7a68dSZhou Wang 	{"HZIP_MEM_VISIBLE_DATA          ",  0x30ull},
19872c7a68dSZhou Wang 	{"HZIP_MEM_VISIBLE_ADDR          ",  0x34ull},
19972c7a68dSZhou Wang 	{"HZIP_COMSUMED_BYTE             ",  0x38ull},
20072c7a68dSZhou Wang 	{"HZIP_PRODUCED_BYTE             ",  0x40ull},
20172c7a68dSZhou Wang 	{"HZIP_COMP_INF                  ",  0x70ull},
20272c7a68dSZhou Wang 	{"HZIP_PRE_OUT                   ",  0x78ull},
20372c7a68dSZhou Wang 	{"HZIP_BD_RD                     ",  0x7cull},
20472c7a68dSZhou Wang 	{"HZIP_BD_WR                     ",  0x80ull},
20572c7a68dSZhou Wang 	{"HZIP_GET_BD_AXI_ERR_NUM        ",  0x84ull},
20672c7a68dSZhou Wang 	{"HZIP_GET_BD_PARSE_ERR_NUM      ",  0x88ull},
20772c7a68dSZhou Wang 	{"HZIP_ADD_BD_AXI_ERR_NUM        ",  0x8cull},
20872c7a68dSZhou Wang 	{"HZIP_DECOMP_STF_RELOAD_CURR_ST ",  0x94ull},
20972c7a68dSZhou Wang 	{"HZIP_DECOMP_LZ77_CURR_ST       ",  0x9cull},
21062c455caSZhou Wang };
21162c455caSZhou Wang 
212f8408d2bSKai Ye static const struct kernel_param_ops zip_uacce_mode_ops = {
213f8408d2bSKai Ye 	.set = uacce_mode_set,
214f8408d2bSKai Ye 	.get = param_get_int,
215f8408d2bSKai Ye };
216f8408d2bSKai Ye 
217f8408d2bSKai Ye /*
218f8408d2bSKai Ye  * uacce_mode = 0 means zip only register to crypto,
219f8408d2bSKai Ye  * uacce_mode = 1 means zip both register to crypto and uacce.
220f8408d2bSKai Ye  */
221f8408d2bSKai Ye static u32 uacce_mode = UACCE_MODE_NOUACCE;
222f8408d2bSKai Ye module_param_cb(uacce_mode, &zip_uacce_mode_ops, &uacce_mode, 0444);
223f8408d2bSKai Ye MODULE_PARM_DESC(uacce_mode, UACCE_MODE_DESC);
224f8408d2bSKai Ye 
22562c455caSZhou Wang static int pf_q_num_set(const char *val, const struct kernel_param *kp)
22662c455caSZhou Wang {
22720b291f5SShukun Tan 	return q_num_set(val, kp, PCI_DEVICE_ID_ZIP_PF);
22862c455caSZhou Wang }
22962c455caSZhou Wang 
23062c455caSZhou Wang static const struct kernel_param_ops pf_q_num_ops = {
23162c455caSZhou Wang 	.set = pf_q_num_set,
23262c455caSZhou Wang 	.get = param_get_int,
23362c455caSZhou Wang };
23462c455caSZhou Wang 
23562c455caSZhou Wang static u32 pf_q_num = HZIP_PF_DEF_Q_NUM;
23662c455caSZhou Wang module_param_cb(pf_q_num, &pf_q_num_ops, &pf_q_num, 0444);
2370542a941SLongfang Liu MODULE_PARM_DESC(pf_q_num, "Number of queues in PF(v1 2-4096, v2 2-1024)");
23862c455caSZhou Wang 
23935ee280fSHao Fang static const struct kernel_param_ops vfs_num_ops = {
24035ee280fSHao Fang 	.set = vfs_num_set,
24135ee280fSHao Fang 	.get = param_get_int,
24235ee280fSHao Fang };
24335ee280fSHao Fang 
24439977f4bSHao Fang static u32 vfs_num;
24535ee280fSHao Fang module_param_cb(vfs_num, &vfs_num_ops, &vfs_num, 0444);
24635ee280fSHao Fang MODULE_PARM_DESC(vfs_num, "Number of VFs to enable(1-63), 0(default)");
24739977f4bSHao Fang 
24862c455caSZhou Wang static const struct pci_device_id hisi_zip_dev_ids[] = {
24962c455caSZhou Wang 	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_ZIP_PF) },
25079e09f30SZhou Wang 	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_ZIP_VF) },
25162c455caSZhou Wang 	{ 0, }
25262c455caSZhou Wang };
25362c455caSZhou Wang MODULE_DEVICE_TABLE(pci, hisi_zip_dev_ids);
25462c455caSZhou Wang 
255813ec3f1SBarry Song int zip_create_qps(struct hisi_qp **qps, int qp_num, int node)
25662c455caSZhou Wang {
257813ec3f1SBarry Song 	if (node == NUMA_NO_NODE)
258813ec3f1SBarry Song 		node = cpu_to_node(smp_processor_id());
25962c455caSZhou Wang 
26018f1ab3fSShukun Tan 	return hisi_qm_alloc_qps_node(&zip_devices, qp_num, 0, node, qps);
26162c455caSZhou Wang }
26262c455caSZhou Wang 
26384c9b780SShukun Tan static int hisi_zip_set_user_domain_and_cache(struct hisi_qm *qm)
26462c455caSZhou Wang {
26584c9b780SShukun Tan 	void __iomem *base = qm->io_base;
26662c455caSZhou Wang 
26762c455caSZhou Wang 	/* qm user domain */
26862c455caSZhou Wang 	writel(AXUSER_BASE, base + QM_ARUSER_M_CFG_1);
26962c455caSZhou Wang 	writel(ARUSER_M_CFG_ENABLE, base + QM_ARUSER_M_CFG_ENABLE);
27062c455caSZhou Wang 	writel(AXUSER_BASE, base + QM_AWUSER_M_CFG_1);
27162c455caSZhou Wang 	writel(AWUSER_M_CFG_ENABLE, base + QM_AWUSER_M_CFG_ENABLE);
27262c455caSZhou Wang 	writel(WUSER_M_CFG_ENABLE, base + QM_WUSER_M_CFG_ENABLE);
27362c455caSZhou Wang 
27462c455caSZhou Wang 	/* qm cache */
27562c455caSZhou Wang 	writel(AXI_M_CFG, base + QM_AXI_M_CFG);
27662c455caSZhou Wang 	writel(AXI_M_CFG_ENABLE, base + QM_AXI_M_CFG_ENABLE);
2772ca73193SYang Shen 
27862c455caSZhou Wang 	/* disable FLR triggered by BME(bus master enable) */
27962c455caSZhou Wang 	writel(PEH_AXUSER_CFG, base + QM_PEH_AXUSER_CFG);
28062c455caSZhou Wang 	writel(PEH_AXUSER_CFG_ENABLE, base + QM_PEH_AXUSER_CFG_ENABLE);
28162c455caSZhou Wang 
28262c455caSZhou Wang 	/* cache */
28315b0694fSYang Shen 	writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_0);
28415b0694fSYang Shen 	writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_1);
28515b0694fSYang Shen 	writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_0);
28615b0694fSYang Shen 	writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_1);
28762c455caSZhou Wang 
28862c455caSZhou Wang 	/* user domain configurations */
28962c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_BD_RUSER_32_63);
29062c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_SGL_RUSER_32_63);
29162c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_BD_WUSER_32_63);
2929e00df71SZhangfei Gao 
293cc3292d1SWeili Qian 	if (qm->use_sva && qm->ver == QM_HW_V2) {
2949e00df71SZhangfei Gao 		writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_DATA_RUSER_32_63);
2959e00df71SZhangfei Gao 		writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_DATA_WUSER_32_63);
2969e00df71SZhangfei Gao 	} else {
29762c455caSZhou Wang 		writel(AXUSER_BASE, base + HZIP_DATA_RUSER_32_63);
29862c455caSZhou Wang 		writel(AXUSER_BASE, base + HZIP_DATA_WUSER_32_63);
2999e00df71SZhangfei Gao 	}
30062c455caSZhou Wang 
30162c455caSZhou Wang 	/* let's open all compression/decompression cores */
30215b0694fSYang Shen 	writel(HZIP_DECOMP_CHECK_ENABLE | HZIP_ALL_COMP_DECOMP_EN,
30362c455caSZhou Wang 	       base + HZIP_CLOCK_GATE_CTRL);
30462c455caSZhou Wang 
3052a928693SYang Shen 	/* enable sqc,cqc writeback */
30662c455caSZhou Wang 	writel(SQC_CACHE_ENABLE | CQC_CACHE_ENABLE | SQC_CACHE_WB_ENABLE |
30762c455caSZhou Wang 	       CQC_CACHE_WB_ENABLE | FIELD_PREP(SQC_CACHE_WB_THRD, 1) |
30862c455caSZhou Wang 	       FIELD_PREP(CQC_CACHE_WB_THRD, 1), base + QM_CACHE_CTL);
30984c9b780SShukun Tan 
31084c9b780SShukun Tan 	return 0;
31162c455caSZhou Wang }
31262c455caSZhou Wang 
313eaebf4c3SShukun Tan static void hisi_zip_hw_error_enable(struct hisi_qm *qm)
31462c455caSZhou Wang {
3157ce396faSShukun Tan 	u32 val;
3167ce396faSShukun Tan 
31762c455caSZhou Wang 	if (qm->ver == QM_HW_V1) {
318eaebf4c3SShukun Tan 		writel(HZIP_CORE_INT_MASK_ALL,
319eaebf4c3SShukun Tan 		       qm->io_base + HZIP_CORE_INT_MASK_REG);
320ee1788c6SZhou Wang 		dev_info(&qm->pdev->dev, "Does not support hw error handle\n");
32162c455caSZhou Wang 		return;
32262c455caSZhou Wang 	}
32362c455caSZhou Wang 
32462c455caSZhou Wang 	/* clear ZIP hw error source if having */
325eaebf4c3SShukun Tan 	writel(HZIP_CORE_INT_MASK_ALL, qm->io_base + HZIP_CORE_INT_SOURCE);
326eaebf4c3SShukun Tan 
327de3daf4bSShukun Tan 	/* configure error type */
3281db0016eSWeili Qian 	writel(HZIP_CORE_INT_RAS_CE_ENABLE,
3291db0016eSWeili Qian 	       qm->io_base + HZIP_CORE_INT_RAS_CE_ENB);
330de3daf4bSShukun Tan 	writel(0x0, qm->io_base + HZIP_CORE_INT_RAS_FE_ENB);
331de3daf4bSShukun Tan 	writel(HZIP_CORE_INT_RAS_NFE_ENABLE,
332de3daf4bSShukun Tan 	       qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB);
333de3daf4bSShukun Tan 
33462c455caSZhou Wang 	/* enable ZIP hw error interrupts */
335eaebf4c3SShukun Tan 	writel(0, qm->io_base + HZIP_CORE_INT_MASK_REG);
3367ce396faSShukun Tan 
3377ce396faSShukun Tan 	/* enable ZIP block master OOO when m-bit error occur */
3387ce396faSShukun Tan 	val = readl(qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
3397ce396faSShukun Tan 	val = val | HZIP_AXI_SHUTDOWN_ENABLE;
3407ce396faSShukun Tan 	writel(val, qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
34162c455caSZhou Wang }
342eaebf4c3SShukun Tan 
343eaebf4c3SShukun Tan static void hisi_zip_hw_error_disable(struct hisi_qm *qm)
344eaebf4c3SShukun Tan {
3457ce396faSShukun Tan 	u32 val;
3467ce396faSShukun Tan 
347eaebf4c3SShukun Tan 	/* disable ZIP hw error interrupts */
348eaebf4c3SShukun Tan 	writel(HZIP_CORE_INT_MASK_ALL, qm->io_base + HZIP_CORE_INT_MASK_REG);
3497ce396faSShukun Tan 
3507ce396faSShukun Tan 	/* disable ZIP block master OOO when m-bit error occur */
3517ce396faSShukun Tan 	val = readl(qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
3527ce396faSShukun Tan 	val = val & ~HZIP_AXI_SHUTDOWN_ENABLE;
3537ce396faSShukun Tan 	writel(val, qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
35462c455caSZhou Wang }
35562c455caSZhou Wang 
35672c7a68dSZhou Wang static inline struct hisi_qm *file_to_qm(struct ctrl_debug_file *file)
35772c7a68dSZhou Wang {
35872c7a68dSZhou Wang 	struct hisi_zip *hisi_zip = file->ctrl->hisi_zip;
35972c7a68dSZhou Wang 
36072c7a68dSZhou Wang 	return &hisi_zip->qm;
36172c7a68dSZhou Wang }
36272c7a68dSZhou Wang 
36372c7a68dSZhou Wang static u32 clear_enable_read(struct ctrl_debug_file *file)
36472c7a68dSZhou Wang {
36572c7a68dSZhou Wang 	struct hisi_qm *qm = file_to_qm(file);
36672c7a68dSZhou Wang 
36772c7a68dSZhou Wang 	return readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) &
36815b0694fSYang Shen 		     HZIP_SOFT_CTRL_CNT_CLR_CE_BIT;
36972c7a68dSZhou Wang }
37072c7a68dSZhou Wang 
37172c7a68dSZhou Wang static int clear_enable_write(struct ctrl_debug_file *file, u32 val)
37272c7a68dSZhou Wang {
37372c7a68dSZhou Wang 	struct hisi_qm *qm = file_to_qm(file);
37472c7a68dSZhou Wang 	u32 tmp;
37572c7a68dSZhou Wang 
37672c7a68dSZhou Wang 	if (val != 1 && val != 0)
37772c7a68dSZhou Wang 		return -EINVAL;
37872c7a68dSZhou Wang 
37972c7a68dSZhou Wang 	tmp = (readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) &
38015b0694fSYang Shen 	       ~HZIP_SOFT_CTRL_CNT_CLR_CE_BIT) | val;
38172c7a68dSZhou Wang 	writel(tmp, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
38272c7a68dSZhou Wang 
38372c7a68dSZhou Wang 	return  0;
38472c7a68dSZhou Wang }
38572c7a68dSZhou Wang 
38615b0694fSYang Shen static ssize_t hisi_zip_ctrl_debug_read(struct file *filp, char __user *buf,
38772c7a68dSZhou Wang 					size_t count, loff_t *pos)
38872c7a68dSZhou Wang {
38972c7a68dSZhou Wang 	struct ctrl_debug_file *file = filp->private_data;
39072c7a68dSZhou Wang 	char tbuf[HZIP_BUF_SIZE];
39172c7a68dSZhou Wang 	u32 val;
39272c7a68dSZhou Wang 	int ret;
39372c7a68dSZhou Wang 
39472c7a68dSZhou Wang 	spin_lock_irq(&file->lock);
39572c7a68dSZhou Wang 	switch (file->index) {
39672c7a68dSZhou Wang 	case HZIP_CLEAR_ENABLE:
39772c7a68dSZhou Wang 		val = clear_enable_read(file);
39872c7a68dSZhou Wang 		break;
39972c7a68dSZhou Wang 	default:
40072c7a68dSZhou Wang 		spin_unlock_irq(&file->lock);
40172c7a68dSZhou Wang 		return -EINVAL;
40272c7a68dSZhou Wang 	}
40372c7a68dSZhou Wang 	spin_unlock_irq(&file->lock);
404533b2079SYang Shen 	ret = scnprintf(tbuf, sizeof(tbuf), "%u\n", val);
40572c7a68dSZhou Wang 	return simple_read_from_buffer(buf, count, pos, tbuf, ret);
40672c7a68dSZhou Wang }
40772c7a68dSZhou Wang 
40815b0694fSYang Shen static ssize_t hisi_zip_ctrl_debug_write(struct file *filp,
40915b0694fSYang Shen 					 const char __user *buf,
41072c7a68dSZhou Wang 					 size_t count, loff_t *pos)
41172c7a68dSZhou Wang {
41272c7a68dSZhou Wang 	struct ctrl_debug_file *file = filp->private_data;
41372c7a68dSZhou Wang 	char tbuf[HZIP_BUF_SIZE];
41472c7a68dSZhou Wang 	unsigned long val;
41572c7a68dSZhou Wang 	int len, ret;
41672c7a68dSZhou Wang 
41772c7a68dSZhou Wang 	if (*pos != 0)
41872c7a68dSZhou Wang 		return 0;
41972c7a68dSZhou Wang 
42072c7a68dSZhou Wang 	if (count >= HZIP_BUF_SIZE)
42172c7a68dSZhou Wang 		return -ENOSPC;
42272c7a68dSZhou Wang 
42372c7a68dSZhou Wang 	len = simple_write_to_buffer(tbuf, HZIP_BUF_SIZE - 1, pos, buf, count);
42472c7a68dSZhou Wang 	if (len < 0)
42572c7a68dSZhou Wang 		return len;
42672c7a68dSZhou Wang 
42772c7a68dSZhou Wang 	tbuf[len] = '\0';
42872c7a68dSZhou Wang 	if (kstrtoul(tbuf, 0, &val))
42972c7a68dSZhou Wang 		return -EFAULT;
43072c7a68dSZhou Wang 
43172c7a68dSZhou Wang 	spin_lock_irq(&file->lock);
43272c7a68dSZhou Wang 	switch (file->index) {
43372c7a68dSZhou Wang 	case HZIP_CLEAR_ENABLE:
43472c7a68dSZhou Wang 		ret = clear_enable_write(file, val);
43572c7a68dSZhou Wang 		if (ret)
43672c7a68dSZhou Wang 			goto err_input;
43772c7a68dSZhou Wang 		break;
43872c7a68dSZhou Wang 	default:
43972c7a68dSZhou Wang 		ret = -EINVAL;
44072c7a68dSZhou Wang 		goto err_input;
44172c7a68dSZhou Wang 	}
44272c7a68dSZhou Wang 	spin_unlock_irq(&file->lock);
44372c7a68dSZhou Wang 
44472c7a68dSZhou Wang 	return count;
44572c7a68dSZhou Wang 
44672c7a68dSZhou Wang err_input:
44772c7a68dSZhou Wang 	spin_unlock_irq(&file->lock);
44872c7a68dSZhou Wang 	return ret;
44972c7a68dSZhou Wang }
45072c7a68dSZhou Wang 
45172c7a68dSZhou Wang static const struct file_operations ctrl_debug_fops = {
45272c7a68dSZhou Wang 	.owner = THIS_MODULE,
45372c7a68dSZhou Wang 	.open = simple_open,
45415b0694fSYang Shen 	.read = hisi_zip_ctrl_debug_read,
45515b0694fSYang Shen 	.write = hisi_zip_ctrl_debug_write,
45672c7a68dSZhou Wang };
45772c7a68dSZhou Wang 
4586621e649SLongfang Liu static int zip_debugfs_atomic64_set(void *data, u64 val)
4596621e649SLongfang Liu {
4606621e649SLongfang Liu 	if (val)
4616621e649SLongfang Liu 		return -EINVAL;
4626621e649SLongfang Liu 
4636621e649SLongfang Liu 	atomic64_set((atomic64_t *)data, 0);
4646621e649SLongfang Liu 
4656621e649SLongfang Liu 	return 0;
4666621e649SLongfang Liu }
4676621e649SLongfang Liu 
4686621e649SLongfang Liu static int zip_debugfs_atomic64_get(void *data, u64 *val)
4696621e649SLongfang Liu {
4706621e649SLongfang Liu 	*val = atomic64_read((atomic64_t *)data);
4716621e649SLongfang Liu 
4726621e649SLongfang Liu 	return 0;
4736621e649SLongfang Liu }
4746621e649SLongfang Liu 
4756621e649SLongfang Liu DEFINE_DEBUGFS_ATTRIBUTE(zip_atomic64_ops, zip_debugfs_atomic64_get,
4766621e649SLongfang Liu 			 zip_debugfs_atomic64_set, "%llu\n");
4776621e649SLongfang Liu 
4784b33f057SShukun Tan static int hisi_zip_core_debug_init(struct hisi_qm *qm)
47972c7a68dSZhou Wang {
48072c7a68dSZhou Wang 	struct device *dev = &qm->pdev->dev;
48172c7a68dSZhou Wang 	struct debugfs_regset32 *regset;
4824a97bfc7SGreg Kroah-Hartman 	struct dentry *tmp_d;
48372c7a68dSZhou Wang 	char buf[HZIP_BUF_SIZE];
48472c7a68dSZhou Wang 	int i;
48572c7a68dSZhou Wang 
48672c7a68dSZhou Wang 	for (i = 0; i < HZIP_CORE_NUM; i++) {
48772c7a68dSZhou Wang 		if (i < HZIP_COMP_CORE_NUM)
488533b2079SYang Shen 			scnprintf(buf, sizeof(buf), "comp_core%d", i);
48972c7a68dSZhou Wang 		else
490533b2079SYang Shen 			scnprintf(buf, sizeof(buf), "decomp_core%d",
491533b2079SYang Shen 				  i - HZIP_COMP_CORE_NUM);
49272c7a68dSZhou Wang 
49372c7a68dSZhou Wang 		regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
49472c7a68dSZhou Wang 		if (!regset)
49572c7a68dSZhou Wang 			return -ENOENT;
49672c7a68dSZhou Wang 
49772c7a68dSZhou Wang 		regset->regs = hzip_dfx_regs;
49872c7a68dSZhou Wang 		regset->nregs = ARRAY_SIZE(hzip_dfx_regs);
49972c7a68dSZhou Wang 		regset->base = qm->io_base + core_offsets[i];
50072c7a68dSZhou Wang 
5014b33f057SShukun Tan 		tmp_d = debugfs_create_dir(buf, qm->debug.debug_root);
5024a97bfc7SGreg Kroah-Hartman 		debugfs_create_regset32("regs", 0444, tmp_d, regset);
50372c7a68dSZhou Wang 	}
50472c7a68dSZhou Wang 
50572c7a68dSZhou Wang 	return 0;
50672c7a68dSZhou Wang }
50772c7a68dSZhou Wang 
5086621e649SLongfang Liu static void hisi_zip_dfx_debug_init(struct hisi_qm *qm)
5096621e649SLongfang Liu {
5106621e649SLongfang Liu 	struct hisi_zip *zip = container_of(qm, struct hisi_zip, qm);
5116621e649SLongfang Liu 	struct hisi_zip_dfx *dfx = &zip->dfx;
5126621e649SLongfang Liu 	struct dentry *tmp_dir;
5136621e649SLongfang Liu 	void *data;
5146621e649SLongfang Liu 	int i;
5156621e649SLongfang Liu 
5166621e649SLongfang Liu 	tmp_dir = debugfs_create_dir("zip_dfx", qm->debug.debug_root);
5176621e649SLongfang Liu 	for (i = 0; i < ARRAY_SIZE(zip_dfx_files); i++) {
5186621e649SLongfang Liu 		data = (atomic64_t *)((uintptr_t)dfx + zip_dfx_files[i].offset);
5196621e649SLongfang Liu 		debugfs_create_file(zip_dfx_files[i].name,
5204b33f057SShukun Tan 				    0644, tmp_dir, data,
5216621e649SLongfang Liu 				    &zip_atomic64_ops);
5226621e649SLongfang Liu 	}
5236621e649SLongfang Liu }
5246621e649SLongfang Liu 
5254b33f057SShukun Tan static int hisi_zip_ctrl_debug_init(struct hisi_qm *qm)
52672c7a68dSZhou Wang {
5274b33f057SShukun Tan 	struct hisi_zip *zip = container_of(qm, struct hisi_zip, qm);
52872c7a68dSZhou Wang 	int i;
52972c7a68dSZhou Wang 
530*c4392b46SWeili Qian 	for (i = HZIP_CLEAR_ENABLE; i < HZIP_DEBUG_FILE_NUM; i++) {
5314b33f057SShukun Tan 		spin_lock_init(&zip->ctrl->files[i].lock);
5324b33f057SShukun Tan 		zip->ctrl->files[i].ctrl = zip->ctrl;
5334b33f057SShukun Tan 		zip->ctrl->files[i].index = i;
53472c7a68dSZhou Wang 
5354a97bfc7SGreg Kroah-Hartman 		debugfs_create_file(ctrl_debug_file_name[i], 0600,
5364b33f057SShukun Tan 				    qm->debug.debug_root,
5374b33f057SShukun Tan 				    zip->ctrl->files + i,
53872c7a68dSZhou Wang 				    &ctrl_debug_fops);
53972c7a68dSZhou Wang 	}
54072c7a68dSZhou Wang 
5414b33f057SShukun Tan 	return hisi_zip_core_debug_init(qm);
54272c7a68dSZhou Wang }
54372c7a68dSZhou Wang 
5444b33f057SShukun Tan static int hisi_zip_debugfs_init(struct hisi_qm *qm)
54572c7a68dSZhou Wang {
54672c7a68dSZhou Wang 	struct device *dev = &qm->pdev->dev;
54772c7a68dSZhou Wang 	struct dentry *dev_d;
54872c7a68dSZhou Wang 	int ret;
54972c7a68dSZhou Wang 
55072c7a68dSZhou Wang 	dev_d = debugfs_create_dir(dev_name(dev), hzip_debugfs_root);
55172c7a68dSZhou Wang 
552c31dc9feSShukun Tan 	qm->debug.sqe_mask_offset = HZIP_SQE_MASK_OFFSET;
553c31dc9feSShukun Tan 	qm->debug.sqe_mask_len = HZIP_SQE_MASK_LEN;
55472c7a68dSZhou Wang 	qm->debug.debug_root = dev_d;
555a8ff38bdSWeili Qian 	hisi_qm_debug_init(qm);
55672c7a68dSZhou Wang 
55772c7a68dSZhou Wang 	if (qm->fun_type == QM_HW_PF) {
5584b33f057SShukun Tan 		ret = hisi_zip_ctrl_debug_init(qm);
55972c7a68dSZhou Wang 		if (ret)
56072c7a68dSZhou Wang 			goto failed_to_create;
56172c7a68dSZhou Wang 	}
56272c7a68dSZhou Wang 
5636621e649SLongfang Liu 	hisi_zip_dfx_debug_init(qm);
5646621e649SLongfang Liu 
56572c7a68dSZhou Wang 	return 0;
56672c7a68dSZhou Wang 
56772c7a68dSZhou Wang failed_to_create:
56872c7a68dSZhou Wang 	debugfs_remove_recursive(hzip_debugfs_root);
56972c7a68dSZhou Wang 	return ret;
57072c7a68dSZhou Wang }
57172c7a68dSZhou Wang 
572698f9523SHao Fang /* hisi_zip_debug_regs_clear() - clear the zip debug regs */
5734b33f057SShukun Tan static void hisi_zip_debug_regs_clear(struct hisi_qm *qm)
57472c7a68dSZhou Wang {
575698f9523SHao Fang 	int i, j;
576698f9523SHao Fang 
577698f9523SHao Fang 	/* enable register read_clear bit */
578698f9523SHao Fang 	writel(HZIP_RD_CNT_CLR_CE_EN, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
579698f9523SHao Fang 	for (i = 0; i < ARRAY_SIZE(core_offsets); i++)
580698f9523SHao Fang 		for (j = 0; j < ARRAY_SIZE(hzip_dfx_regs); j++)
581698f9523SHao Fang 			readl(qm->io_base + core_offsets[i] +
582698f9523SHao Fang 			      hzip_dfx_regs[j].offset);
583698f9523SHao Fang 
584698f9523SHao Fang 	/* disable register read_clear bit */
58572c7a68dSZhou Wang 	writel(0x0, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
58672c7a68dSZhou Wang 
58772c7a68dSZhou Wang 	hisi_qm_debug_regs_clear(qm);
58872c7a68dSZhou Wang }
58972c7a68dSZhou Wang 
5904b33f057SShukun Tan static void hisi_zip_debugfs_exit(struct hisi_qm *qm)
59172c7a68dSZhou Wang {
59272c7a68dSZhou Wang 	debugfs_remove_recursive(qm->debug.debug_root);
59372c7a68dSZhou Wang 
5944b33f057SShukun Tan 	if (qm->fun_type == QM_HW_PF) {
5954b33f057SShukun Tan 		hisi_zip_debug_regs_clear(qm);
5964b33f057SShukun Tan 		qm->debug.curr_qm_qp_num = 0;
5974b33f057SShukun Tan 	}
59872c7a68dSZhou Wang }
59972c7a68dSZhou Wang 
600f826e6efSShukun Tan static void hisi_zip_log_hw_error(struct hisi_qm *qm, u32 err_sts)
601f826e6efSShukun Tan {
602f826e6efSShukun Tan 	const struct hisi_zip_hw_error *err = zip_hw_error;
603f826e6efSShukun Tan 	struct device *dev = &qm->pdev->dev;
604f826e6efSShukun Tan 	u32 err_val;
605f826e6efSShukun Tan 
606f826e6efSShukun Tan 	while (err->msg) {
607f826e6efSShukun Tan 		if (err->int_msk & err_sts) {
608f826e6efSShukun Tan 			dev_err(dev, "%s [error status=0x%x] found\n",
609f826e6efSShukun Tan 				err->msg, err->int_msk);
610f826e6efSShukun Tan 
611f826e6efSShukun Tan 			if (err->int_msk & HZIP_CORE_INT_STATUS_M_ECC) {
612f826e6efSShukun Tan 				err_val = readl(qm->io_base +
613f826e6efSShukun Tan 						HZIP_CORE_SRAM_ECC_ERR_INFO);
614f826e6efSShukun Tan 				dev_err(dev, "hisi-zip multi ecc sram num=0x%x\n",
615f826e6efSShukun Tan 					((err_val >>
616f826e6efSShukun Tan 					HZIP_SRAM_ECC_ERR_NUM_SHIFT) & 0xFF));
617f826e6efSShukun Tan 			}
618f826e6efSShukun Tan 		}
619f826e6efSShukun Tan 		err++;
620f826e6efSShukun Tan 	}
621f826e6efSShukun Tan }
622f826e6efSShukun Tan 
623f826e6efSShukun Tan static u32 hisi_zip_get_hw_err_status(struct hisi_qm *qm)
624f826e6efSShukun Tan {
625f826e6efSShukun Tan 	return readl(qm->io_base + HZIP_CORE_INT_STATUS);
626f826e6efSShukun Tan }
627f826e6efSShukun Tan 
62884c9b780SShukun Tan static void hisi_zip_clear_hw_err_status(struct hisi_qm *qm, u32 err_sts)
62984c9b780SShukun Tan {
63084c9b780SShukun Tan 	writel(err_sts, qm->io_base + HZIP_CORE_INT_SOURCE);
63184c9b780SShukun Tan }
63284c9b780SShukun Tan 
63384c9b780SShukun Tan static void hisi_zip_open_axi_master_ooo(struct hisi_qm *qm)
63484c9b780SShukun Tan {
63584c9b780SShukun Tan 	u32 val;
63684c9b780SShukun Tan 
63784c9b780SShukun Tan 	val = readl(qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
63884c9b780SShukun Tan 
63984c9b780SShukun Tan 	writel(val & ~HZIP_AXI_SHUTDOWN_ENABLE,
64084c9b780SShukun Tan 	       qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
64184c9b780SShukun Tan 
64284c9b780SShukun Tan 	writel(val | HZIP_AXI_SHUTDOWN_ENABLE,
64384c9b780SShukun Tan 	       qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
64484c9b780SShukun Tan }
64584c9b780SShukun Tan 
64684c9b780SShukun Tan static void hisi_zip_close_axi_master_ooo(struct hisi_qm *qm)
64784c9b780SShukun Tan {
64884c9b780SShukun Tan 	u32 nfe_enb;
64984c9b780SShukun Tan 
65084c9b780SShukun Tan 	/* Disable ECC Mbit error report. */
65184c9b780SShukun Tan 	nfe_enb = readl(qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB);
65284c9b780SShukun Tan 	writel(nfe_enb & ~HZIP_CORE_INT_STATUS_M_ECC,
65384c9b780SShukun Tan 	       qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB);
65484c9b780SShukun Tan 
65584c9b780SShukun Tan 	/* Inject zip ECC Mbit error to block master ooo. */
65684c9b780SShukun Tan 	writel(HZIP_CORE_INT_STATUS_M_ECC,
65784c9b780SShukun Tan 	       qm->io_base + HZIP_CORE_INT_SET);
65884c9b780SShukun Tan }
65984c9b780SShukun Tan 
660eaebf4c3SShukun Tan static const struct hisi_qm_err_ini hisi_zip_err_ini = {
66184c9b780SShukun Tan 	.hw_init		= hisi_zip_set_user_domain_and_cache,
662eaebf4c3SShukun Tan 	.hw_err_enable		= hisi_zip_hw_error_enable,
663eaebf4c3SShukun Tan 	.hw_err_disable		= hisi_zip_hw_error_disable,
664f826e6efSShukun Tan 	.get_dev_hw_err_status	= hisi_zip_get_hw_err_status,
66584c9b780SShukun Tan 	.clear_dev_hw_err_status = hisi_zip_clear_hw_err_status,
666f826e6efSShukun Tan 	.log_dev_hw_err		= hisi_zip_log_hw_error,
66784c9b780SShukun Tan 	.open_axi_master_ooo	= hisi_zip_open_axi_master_ooo,
66884c9b780SShukun Tan 	.close_axi_master_ooo	= hisi_zip_close_axi_master_ooo,
669eaebf4c3SShukun Tan 	.err_info		= {
670eaebf4c3SShukun Tan 		.ce			= QM_BASE_CE,
671f826e6efSShukun Tan 		.nfe			= QM_BASE_NFE |
672f826e6efSShukun Tan 					  QM_ACC_WB_NOT_READY_TIMEOUT,
673eaebf4c3SShukun Tan 		.fe			= 0,
67484c9b780SShukun Tan 		.ecc_2bits_mask		= HZIP_CORE_INT_STATUS_M_ECC,
6751db0016eSWeili Qian 		.dev_ce_mask		= HZIP_CORE_INT_RAS_CE_ENABLE,
67684c9b780SShukun Tan 		.msi_wr_port		= HZIP_WR_PORT,
67784c9b780SShukun Tan 		.acpi_rst		= "ZRST",
67862c455caSZhou Wang 	}
679eaebf4c3SShukun Tan };
68062c455caSZhou Wang 
68162c455caSZhou Wang static int hisi_zip_pf_probe_init(struct hisi_zip *hisi_zip)
68262c455caSZhou Wang {
68362c455caSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
68462c455caSZhou Wang 	struct hisi_zip_ctrl *ctrl;
68562c455caSZhou Wang 
68662c455caSZhou Wang 	ctrl = devm_kzalloc(&qm->pdev->dev, sizeof(*ctrl), GFP_KERNEL);
68762c455caSZhou Wang 	if (!ctrl)
68862c455caSZhou Wang 		return -ENOMEM;
68962c455caSZhou Wang 
69062c455caSZhou Wang 	hisi_zip->ctrl = ctrl;
69162c455caSZhou Wang 	ctrl->hisi_zip = hisi_zip;
692eaebf4c3SShukun Tan 	qm->err_ini = &hisi_zip_err_ini;
693eaebf4c3SShukun Tan 
69484c9b780SShukun Tan 	hisi_zip_set_user_domain_and_cache(qm);
695eaebf4c3SShukun Tan 	hisi_qm_dev_err_init(qm);
6964b33f057SShukun Tan 	hisi_zip_debug_regs_clear(qm);
69762c455caSZhou Wang 
69862c455caSZhou Wang 	return 0;
69962c455caSZhou Wang }
70062c455caSZhou Wang 
701cfd66a66SLongfang Liu static int hisi_zip_qm_init(struct hisi_qm *qm, struct pci_dev *pdev)
70239977f4bSHao Fang {
7031dc44035SYang Shen 	int ret;
7041dc44035SYang Shen 
70539977f4bSHao Fang 	qm->pdev = pdev;
70658ca0060SWeili Qian 	qm->ver = pdev->revision;
7079e00df71SZhangfei Gao 	qm->algs = "zlib\ngzip";
708f8408d2bSKai Ye 	qm->mode = uacce_mode;
70939977f4bSHao Fang 	qm->sqe_size = HZIP_SQE_SIZE;
71039977f4bSHao Fang 	qm->dev_name = hisi_zip_name;
711d9701f8dSWeili Qian 
712cfd66a66SLongfang Liu 	qm->fun_type = (pdev->device == PCI_DEVICE_ID_ZIP_PF) ?
713cfd66a66SLongfang Liu 			QM_HW_PF : QM_HW_VF;
714d9701f8dSWeili Qian 	if (qm->fun_type == QM_HW_PF) {
715d9701f8dSWeili Qian 		qm->qp_base = HZIP_PF_DEF_Q_BASE;
716d9701f8dSWeili Qian 		qm->qp_num = pf_q_num;
7172fcb4cc3SSihang Chen 		qm->debug.curr_qm_qp_num = pf_q_num;
718d9701f8dSWeili Qian 		qm->qm_list = &zip_devices;
719d9701f8dSWeili Qian 	} else if (qm->fun_type == QM_HW_VF && qm->ver == QM_HW_V1) {
720d9701f8dSWeili Qian 		/*
721d9701f8dSWeili Qian 		 * have no way to get qm configure in VM in v1 hardware,
722d9701f8dSWeili Qian 		 * so currently force PF to uses HZIP_PF_DEF_Q_NUM, and force
723d9701f8dSWeili Qian 		 * to trigger only one VF in v1 hardware.
724d9701f8dSWeili Qian 		 *
725d9701f8dSWeili Qian 		 * v2 hardware has no such problem.
726d9701f8dSWeili Qian 		 */
727d9701f8dSWeili Qian 		qm->qp_base = HZIP_PF_DEF_Q_NUM;
728d9701f8dSWeili Qian 		qm->qp_num = HZIP_QUEUE_NUM_V1 - HZIP_PF_DEF_Q_NUM;
729d9701f8dSWeili Qian 	}
730cfd66a66SLongfang Liu 
7311dc44035SYang Shen 	qm->wq = alloc_workqueue("%s", WQ_HIGHPRI | WQ_MEM_RECLAIM |
7321dc44035SYang Shen 				 WQ_UNBOUND, num_online_cpus(),
7331dc44035SYang Shen 				 pci_name(qm->pdev));
7341dc44035SYang Shen 	if (!qm->wq) {
7351dc44035SYang Shen 		pci_err(qm->pdev, "fail to alloc workqueue\n");
7361dc44035SYang Shen 		return -ENOMEM;
7371dc44035SYang Shen 	}
7381dc44035SYang Shen 
7391dc44035SYang Shen 	ret = hisi_qm_init(qm);
7401dc44035SYang Shen 	if (ret)
7411dc44035SYang Shen 		destroy_workqueue(qm->wq);
7421dc44035SYang Shen 
7431dc44035SYang Shen 	return ret;
7441dc44035SYang Shen }
7451dc44035SYang Shen 
7461dc44035SYang Shen static void hisi_zip_qm_uninit(struct hisi_qm *qm)
7471dc44035SYang Shen {
7481dc44035SYang Shen 	hisi_qm_uninit(qm);
7491dc44035SYang Shen 	destroy_workqueue(qm->wq);
75039977f4bSHao Fang }
75139977f4bSHao Fang 
752cfd66a66SLongfang Liu static int hisi_zip_probe_init(struct hisi_zip *hisi_zip)
753cfd66a66SLongfang Liu {
754cfd66a66SLongfang Liu 	struct hisi_qm *qm = &hisi_zip->qm;
755cfd66a66SLongfang Liu 	int ret;
756cfd66a66SLongfang Liu 
75739977f4bSHao Fang 	if (qm->fun_type == QM_HW_PF) {
75839977f4bSHao Fang 		ret = hisi_zip_pf_probe_init(hisi_zip);
75939977f4bSHao Fang 		if (ret)
76039977f4bSHao Fang 			return ret;
761cfd66a66SLongfang Liu 	}
762cfd66a66SLongfang Liu 
763cfd66a66SLongfang Liu 	return 0;
764cfd66a66SLongfang Liu }
765cfd66a66SLongfang Liu 
766cfd66a66SLongfang Liu static int hisi_zip_probe(struct pci_dev *pdev, const struct pci_device_id *id)
767cfd66a66SLongfang Liu {
768cfd66a66SLongfang Liu 	struct hisi_zip *hisi_zip;
769cfd66a66SLongfang Liu 	struct hisi_qm *qm;
770cfd66a66SLongfang Liu 	int ret;
771cfd66a66SLongfang Liu 
772cfd66a66SLongfang Liu 	hisi_zip = devm_kzalloc(&pdev->dev, sizeof(*hisi_zip), GFP_KERNEL);
773cfd66a66SLongfang Liu 	if (!hisi_zip)
774cfd66a66SLongfang Liu 		return -ENOMEM;
775cfd66a66SLongfang Liu 
776cfd66a66SLongfang Liu 	qm = &hisi_zip->qm;
777cfd66a66SLongfang Liu 
778cfd66a66SLongfang Liu 	ret = hisi_zip_qm_init(qm, pdev);
779cfd66a66SLongfang Liu 	if (ret) {
780cfd66a66SLongfang Liu 		pci_err(pdev, "Failed to init ZIP QM (%d)!\n", ret);
781cfd66a66SLongfang Liu 		return ret;
782cfd66a66SLongfang Liu 	}
783cfd66a66SLongfang Liu 
784cfd66a66SLongfang Liu 	ret = hisi_zip_probe_init(hisi_zip);
785cfd66a66SLongfang Liu 	if (ret) {
786cfd66a66SLongfang Liu 		pci_err(pdev, "Failed to probe (%d)!\n", ret);
787cfd66a66SLongfang Liu 		goto err_qm_uninit;
78839977f4bSHao Fang 	}
78939977f4bSHao Fang 
79039977f4bSHao Fang 	ret = hisi_qm_start(qm);
79139977f4bSHao Fang 	if (ret)
7923d29e98dSYang Shen 		goto err_dev_err_uninit;
79339977f4bSHao Fang 
7944b33f057SShukun Tan 	ret = hisi_zip_debugfs_init(qm);
79539977f4bSHao Fang 	if (ret)
796b1a25820SYang Shen 		pci_err(pdev, "failed to init debugfs (%d)!\n", ret);
79739977f4bSHao Fang 
7983d29e98dSYang Shen 	ret = hisi_qm_alg_register(qm, &zip_devices);
7993d29e98dSYang Shen 	if (ret < 0) {
800b1a25820SYang Shen 		pci_err(pdev, "failed to register driver to crypto!\n");
8013d29e98dSYang Shen 		goto err_qm_stop;
8023d29e98dSYang Shen 	}
80339977f4bSHao Fang 
8049e00df71SZhangfei Gao 	if (qm->uacce) {
8059e00df71SZhangfei Gao 		ret = uacce_register(qm->uacce);
806b1a25820SYang Shen 		if (ret) {
807b1a25820SYang Shen 			pci_err(pdev, "failed to register uacce (%d)!\n", ret);
8083d29e98dSYang Shen 			goto err_qm_alg_unregister;
8099e00df71SZhangfei Gao 		}
810b1a25820SYang Shen 	}
8119e00df71SZhangfei Gao 
81239977f4bSHao Fang 	if (qm->fun_type == QM_HW_PF && vfs_num > 0) {
813cd1b7ae3SShukun Tan 		ret = hisi_qm_sriov_enable(pdev, vfs_num);
81439977f4bSHao Fang 		if (ret < 0)
8153d29e98dSYang Shen 			goto err_qm_alg_unregister;
81639977f4bSHao Fang 	}
81739977f4bSHao Fang 
81839977f4bSHao Fang 	return 0;
81939977f4bSHao Fang 
8203d29e98dSYang Shen err_qm_alg_unregister:
8213d29e98dSYang Shen 	hisi_qm_alg_unregister(qm, &zip_devices);
8223d29e98dSYang Shen 
8233d29e98dSYang Shen err_qm_stop:
8244b33f057SShukun Tan 	hisi_zip_debugfs_exit(qm);
825e88dd6e1SYang Shen 	hisi_qm_stop(qm, QM_NORMAL);
8263d29e98dSYang Shen 
8273d29e98dSYang Shen err_dev_err_uninit:
8283d29e98dSYang Shen 	hisi_qm_dev_err_uninit(qm);
8293d29e98dSYang Shen 
83039977f4bSHao Fang err_qm_uninit:
8311dc44035SYang Shen 	hisi_zip_qm_uninit(qm);
832cfd66a66SLongfang Liu 
83339977f4bSHao Fang 	return ret;
83439977f4bSHao Fang }
83539977f4bSHao Fang 
83662c455caSZhou Wang static void hisi_zip_remove(struct pci_dev *pdev)
83762c455caSZhou Wang {
838d8140b87SYang Shen 	struct hisi_qm *qm = pci_get_drvdata(pdev);
83962c455caSZhou Wang 
840daa31783SWeili Qian 	hisi_qm_wait_task_finish(qm, &zip_devices);
8413d29e98dSYang Shen 	hisi_qm_alg_unregister(qm, &zip_devices);
8423d29e98dSYang Shen 
843619e464aSShukun Tan 	if (qm->fun_type == QM_HW_PF && qm->vfs_num)
8443e9954feSWeili Qian 		hisi_qm_sriov_disable(pdev, true);
84579e09f30SZhou Wang 
8464b33f057SShukun Tan 	hisi_zip_debugfs_exit(qm);
847e88dd6e1SYang Shen 	hisi_qm_stop(qm, QM_NORMAL);
848eaebf4c3SShukun Tan 	hisi_qm_dev_err_uninit(qm);
8491dc44035SYang Shen 	hisi_zip_qm_uninit(qm);
85062c455caSZhou Wang }
85162c455caSZhou Wang 
85262c455caSZhou Wang static const struct pci_error_handlers hisi_zip_err_handler = {
853f826e6efSShukun Tan 	.error_detected	= hisi_qm_dev_err_detected,
85484c9b780SShukun Tan 	.slot_reset	= hisi_qm_dev_slot_reset,
8557ce396faSShukun Tan 	.reset_prepare	= hisi_qm_reset_prepare,
8567ce396faSShukun Tan 	.reset_done	= hisi_qm_reset_done,
85762c455caSZhou Wang };
85862c455caSZhou Wang 
85962c455caSZhou Wang static struct pci_driver hisi_zip_pci_driver = {
86062c455caSZhou Wang 	.name			= "hisi_zip",
86162c455caSZhou Wang 	.id_table		= hisi_zip_dev_ids,
86262c455caSZhou Wang 	.probe			= hisi_zip_probe,
86362c455caSZhou Wang 	.remove			= hisi_zip_remove,
864bf6a7a5aSArnd Bergmann 	.sriov_configure	= IS_ENABLED(CONFIG_PCI_IOV) ?
865cd1b7ae3SShukun Tan 					hisi_qm_sriov_configure : NULL,
86662c455caSZhou Wang 	.err_handler		= &hisi_zip_err_handler,
86764dfe495SYang Shen 	.shutdown		= hisi_qm_dev_shutdown,
86862c455caSZhou Wang };
86962c455caSZhou Wang 
87072c7a68dSZhou Wang static void hisi_zip_register_debugfs(void)
87172c7a68dSZhou Wang {
87272c7a68dSZhou Wang 	if (!debugfs_initialized())
87372c7a68dSZhou Wang 		return;
87472c7a68dSZhou Wang 
87572c7a68dSZhou Wang 	hzip_debugfs_root = debugfs_create_dir("hisi_zip", NULL);
87672c7a68dSZhou Wang }
87772c7a68dSZhou Wang 
87872c7a68dSZhou Wang static void hisi_zip_unregister_debugfs(void)
87972c7a68dSZhou Wang {
88072c7a68dSZhou Wang 	debugfs_remove_recursive(hzip_debugfs_root);
88172c7a68dSZhou Wang }
88272c7a68dSZhou Wang 
88362c455caSZhou Wang static int __init hisi_zip_init(void)
88462c455caSZhou Wang {
88562c455caSZhou Wang 	int ret;
88662c455caSZhou Wang 
88718f1ab3fSShukun Tan 	hisi_qm_init_list(&zip_devices);
88872c7a68dSZhou Wang 	hisi_zip_register_debugfs();
88972c7a68dSZhou Wang 
89062c455caSZhou Wang 	ret = pci_register_driver(&hisi_zip_pci_driver);
89162c455caSZhou Wang 	if (ret < 0) {
89272c7a68dSZhou Wang 		hisi_zip_unregister_debugfs();
8932ca73193SYang Shen 		pr_err("Failed to register pci driver.\n");
8942ca73193SYang Shen 	}
89572c7a68dSZhou Wang 
89662c455caSZhou Wang 	return ret;
89762c455caSZhou Wang }
89862c455caSZhou Wang 
89962c455caSZhou Wang static void __exit hisi_zip_exit(void)
90062c455caSZhou Wang {
90162c455caSZhou Wang 	pci_unregister_driver(&hisi_zip_pci_driver);
90272c7a68dSZhou Wang 	hisi_zip_unregister_debugfs();
90362c455caSZhou Wang }
90462c455caSZhou Wang 
90562c455caSZhou Wang module_init(hisi_zip_init);
90662c455caSZhou Wang module_exit(hisi_zip_exit);
90762c455caSZhou Wang 
90862c455caSZhou Wang MODULE_LICENSE("GPL v2");
90962c455caSZhou Wang MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>");
91062c455caSZhou Wang MODULE_DESCRIPTION("Driver for HiSilicon ZIP accelerator");
911