xref: /openbmc/linux/drivers/crypto/hisilicon/zip/zip_main.c (revision 1dc440355e472a60a98cb4ec9aa5ec56267a96fc)
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 #define HZIP_QUEUE_NUM_V2		1024
2262c455caSZhou Wang 
2362c455caSZhou Wang #define HZIP_CLOCK_GATE_CTRL		0x301004
2462c455caSZhou Wang #define COMP0_ENABLE			BIT(0)
2562c455caSZhou Wang #define COMP1_ENABLE			BIT(1)
2662c455caSZhou Wang #define DECOMP0_ENABLE			BIT(2)
2762c455caSZhou Wang #define DECOMP1_ENABLE			BIT(3)
2862c455caSZhou Wang #define DECOMP2_ENABLE			BIT(4)
2962c455caSZhou Wang #define DECOMP3_ENABLE			BIT(5)
3062c455caSZhou Wang #define DECOMP4_ENABLE			BIT(6)
3162c455caSZhou Wang #define DECOMP5_ENABLE			BIT(7)
3215b0694fSYang Shen #define HZIP_ALL_COMP_DECOMP_EN		(COMP0_ENABLE | COMP1_ENABLE | \
3362c455caSZhou Wang 					 DECOMP0_ENABLE | DECOMP1_ENABLE | \
3462c455caSZhou Wang 					 DECOMP2_ENABLE | DECOMP3_ENABLE | \
3562c455caSZhou Wang 					 DECOMP4_ENABLE | DECOMP5_ENABLE)
3615b0694fSYang Shen #define HZIP_DECOMP_CHECK_ENABLE	BIT(16)
3772c7a68dSZhou Wang #define HZIP_FSM_MAX_CNT		0x301008
3862c455caSZhou Wang 
3962c455caSZhou Wang #define HZIP_PORT_ARCA_CHE_0		0x301040
4062c455caSZhou Wang #define HZIP_PORT_ARCA_CHE_1		0x301044
4162c455caSZhou Wang #define HZIP_PORT_AWCA_CHE_0		0x301060
4262c455caSZhou Wang #define HZIP_PORT_AWCA_CHE_1		0x301064
4315b0694fSYang Shen #define HZIP_CACHE_ALL_EN		0xffffffff
4462c455caSZhou Wang 
4562c455caSZhou Wang #define HZIP_BD_RUSER_32_63		0x301110
4662c455caSZhou Wang #define HZIP_SGL_RUSER_32_63		0x30111c
4762c455caSZhou Wang #define HZIP_DATA_RUSER_32_63		0x301128
4862c455caSZhou Wang #define HZIP_DATA_WUSER_32_63		0x301134
4962c455caSZhou Wang #define HZIP_BD_WUSER_32_63		0x301140
5062c455caSZhou Wang 
5172c7a68dSZhou Wang #define HZIP_QM_IDEL_STATUS		0x3040e4
5262c455caSZhou Wang 
5372c7a68dSZhou Wang #define HZIP_CORE_DEBUG_COMP_0		0x302000
5472c7a68dSZhou Wang #define HZIP_CORE_DEBUG_COMP_1		0x303000
5572c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_0	0x304000
5672c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_1	0x305000
5772c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_2	0x306000
5872c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_3	0x307000
5972c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_4	0x308000
6072c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_5	0x309000
6162c455caSZhou Wang 
6262c455caSZhou Wang #define HZIP_CORE_INT_SOURCE		0x3010A0
63eaebf4c3SShukun Tan #define HZIP_CORE_INT_MASK_REG		0x3010A4
6484c9b780SShukun Tan #define HZIP_CORE_INT_SET		0x3010A8
6562c455caSZhou Wang #define HZIP_CORE_INT_STATUS		0x3010AC
6662c455caSZhou Wang #define HZIP_CORE_INT_STATUS_M_ECC	BIT(1)
6762c455caSZhou Wang #define HZIP_CORE_SRAM_ECC_ERR_INFO	0x301148
68de3daf4bSShukun Tan #define HZIP_CORE_INT_RAS_CE_ENB	0x301160
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_CURRENT_QM,
14172c7a68dSZhou Wang 	HZIP_CLEAR_ENABLE,
14272c7a68dSZhou Wang 	HZIP_DEBUG_FILE_NUM,
14372c7a68dSZhou Wang };
14472c7a68dSZhou Wang 
14572c7a68dSZhou Wang static const char * const ctrl_debug_file_name[] = {
14672c7a68dSZhou Wang 	[HZIP_CURRENT_QM]   = "current_qm",
14772c7a68dSZhou Wang 	[HZIP_CLEAR_ENABLE] = "clear_enable",
14872c7a68dSZhou Wang };
14972c7a68dSZhou Wang 
15072c7a68dSZhou Wang struct ctrl_debug_file {
15172c7a68dSZhou Wang 	enum ctrl_debug_file_index index;
15272c7a68dSZhou Wang 	spinlock_t lock;
15372c7a68dSZhou Wang 	struct hisi_zip_ctrl *ctrl;
15472c7a68dSZhou Wang };
15572c7a68dSZhou Wang 
15662c455caSZhou Wang /*
15762c455caSZhou Wang  * One ZIP controller has one PF and multiple VFs, some global configurations
15862c455caSZhou Wang  * which PF has need this structure.
15962c455caSZhou Wang  *
16062c455caSZhou Wang  * Just relevant for PF.
16162c455caSZhou Wang  */
16262c455caSZhou Wang struct hisi_zip_ctrl {
16362c455caSZhou Wang 	struct hisi_zip *hisi_zip;
16472c7a68dSZhou Wang 	struct ctrl_debug_file files[HZIP_DEBUG_FILE_NUM];
16572c7a68dSZhou Wang };
16672c7a68dSZhou Wang 
16772c7a68dSZhou Wang enum {
16872c7a68dSZhou Wang 	HZIP_COMP_CORE0,
16972c7a68dSZhou Wang 	HZIP_COMP_CORE1,
17072c7a68dSZhou Wang 	HZIP_DECOMP_CORE0,
17172c7a68dSZhou Wang 	HZIP_DECOMP_CORE1,
17272c7a68dSZhou Wang 	HZIP_DECOMP_CORE2,
17372c7a68dSZhou Wang 	HZIP_DECOMP_CORE3,
17472c7a68dSZhou Wang 	HZIP_DECOMP_CORE4,
17572c7a68dSZhou Wang 	HZIP_DECOMP_CORE5,
17672c7a68dSZhou Wang };
17772c7a68dSZhou Wang 
17872c7a68dSZhou Wang static const u64 core_offsets[] = {
17972c7a68dSZhou Wang 	[HZIP_COMP_CORE0]   = 0x302000,
18072c7a68dSZhou Wang 	[HZIP_COMP_CORE1]   = 0x303000,
18172c7a68dSZhou Wang 	[HZIP_DECOMP_CORE0] = 0x304000,
18272c7a68dSZhou Wang 	[HZIP_DECOMP_CORE1] = 0x305000,
18372c7a68dSZhou Wang 	[HZIP_DECOMP_CORE2] = 0x306000,
18472c7a68dSZhou Wang 	[HZIP_DECOMP_CORE3] = 0x307000,
18572c7a68dSZhou Wang 	[HZIP_DECOMP_CORE4] = 0x308000,
18672c7a68dSZhou Wang 	[HZIP_DECOMP_CORE5] = 0x309000,
18772c7a68dSZhou Wang };
18872c7a68dSZhou Wang 
1898f68659bSRikard Falkeborn static const struct debugfs_reg32 hzip_dfx_regs[] = {
19072c7a68dSZhou Wang 	{"HZIP_GET_BD_NUM                ",  0x00ull},
19172c7a68dSZhou Wang 	{"HZIP_GET_RIGHT_BD              ",  0x04ull},
19272c7a68dSZhou Wang 	{"HZIP_GET_ERROR_BD              ",  0x08ull},
19372c7a68dSZhou Wang 	{"HZIP_DONE_BD_NUM               ",  0x0cull},
19472c7a68dSZhou Wang 	{"HZIP_WORK_CYCLE                ",  0x10ull},
19572c7a68dSZhou Wang 	{"HZIP_IDLE_CYCLE                ",  0x18ull},
19672c7a68dSZhou Wang 	{"HZIP_MAX_DELAY                 ",  0x20ull},
19772c7a68dSZhou Wang 	{"HZIP_MIN_DELAY                 ",  0x24ull},
19872c7a68dSZhou Wang 	{"HZIP_AVG_DELAY                 ",  0x28ull},
19972c7a68dSZhou Wang 	{"HZIP_MEM_VISIBLE_DATA          ",  0x30ull},
20072c7a68dSZhou Wang 	{"HZIP_MEM_VISIBLE_ADDR          ",  0x34ull},
20172c7a68dSZhou Wang 	{"HZIP_COMSUMED_BYTE             ",  0x38ull},
20272c7a68dSZhou Wang 	{"HZIP_PRODUCED_BYTE             ",  0x40ull},
20372c7a68dSZhou Wang 	{"HZIP_COMP_INF                  ",  0x70ull},
20472c7a68dSZhou Wang 	{"HZIP_PRE_OUT                   ",  0x78ull},
20572c7a68dSZhou Wang 	{"HZIP_BD_RD                     ",  0x7cull},
20672c7a68dSZhou Wang 	{"HZIP_BD_WR                     ",  0x80ull},
20772c7a68dSZhou Wang 	{"HZIP_GET_BD_AXI_ERR_NUM        ",  0x84ull},
20872c7a68dSZhou Wang 	{"HZIP_GET_BD_PARSE_ERR_NUM      ",  0x88ull},
20972c7a68dSZhou Wang 	{"HZIP_ADD_BD_AXI_ERR_NUM        ",  0x8cull},
21072c7a68dSZhou Wang 	{"HZIP_DECOMP_STF_RELOAD_CURR_ST ",  0x94ull},
21172c7a68dSZhou Wang 	{"HZIP_DECOMP_LZ77_CURR_ST       ",  0x9cull},
21262c455caSZhou Wang };
21362c455caSZhou Wang 
21462c455caSZhou Wang static int pf_q_num_set(const char *val, const struct kernel_param *kp)
21562c455caSZhou Wang {
21620b291f5SShukun Tan 	return q_num_set(val, kp, PCI_DEVICE_ID_ZIP_PF);
21762c455caSZhou Wang }
21862c455caSZhou Wang 
21962c455caSZhou Wang static const struct kernel_param_ops pf_q_num_ops = {
22062c455caSZhou Wang 	.set = pf_q_num_set,
22162c455caSZhou Wang 	.get = param_get_int,
22262c455caSZhou Wang };
22362c455caSZhou Wang 
22462c455caSZhou Wang static u32 pf_q_num = HZIP_PF_DEF_Q_NUM;
22562c455caSZhou Wang module_param_cb(pf_q_num, &pf_q_num_ops, &pf_q_num, 0444);
2260542a941SLongfang Liu MODULE_PARM_DESC(pf_q_num, "Number of queues in PF(v1 2-4096, v2 2-1024)");
22762c455caSZhou Wang 
22835ee280fSHao Fang static const struct kernel_param_ops vfs_num_ops = {
22935ee280fSHao Fang 	.set = vfs_num_set,
23035ee280fSHao Fang 	.get = param_get_int,
23135ee280fSHao Fang };
23235ee280fSHao Fang 
23339977f4bSHao Fang static u32 vfs_num;
23435ee280fSHao Fang module_param_cb(vfs_num, &vfs_num_ops, &vfs_num, 0444);
23535ee280fSHao Fang MODULE_PARM_DESC(vfs_num, "Number of VFs to enable(1-63), 0(default)");
23639977f4bSHao Fang 
23762c455caSZhou Wang static const struct pci_device_id hisi_zip_dev_ids[] = {
23862c455caSZhou Wang 	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_ZIP_PF) },
23979e09f30SZhou Wang 	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_ZIP_VF) },
24062c455caSZhou Wang 	{ 0, }
24162c455caSZhou Wang };
24262c455caSZhou Wang MODULE_DEVICE_TABLE(pci, hisi_zip_dev_ids);
24362c455caSZhou Wang 
244813ec3f1SBarry Song int zip_create_qps(struct hisi_qp **qps, int qp_num, int node)
24562c455caSZhou Wang {
246813ec3f1SBarry Song 	if (node == NUMA_NO_NODE)
247813ec3f1SBarry Song 		node = cpu_to_node(smp_processor_id());
24862c455caSZhou Wang 
24918f1ab3fSShukun Tan 	return hisi_qm_alloc_qps_node(&zip_devices, qp_num, 0, node, qps);
25062c455caSZhou Wang }
25162c455caSZhou Wang 
25284c9b780SShukun Tan static int hisi_zip_set_user_domain_and_cache(struct hisi_qm *qm)
25362c455caSZhou Wang {
25484c9b780SShukun Tan 	void __iomem *base = qm->io_base;
25562c455caSZhou Wang 
25662c455caSZhou Wang 	/* qm user domain */
25762c455caSZhou Wang 	writel(AXUSER_BASE, base + QM_ARUSER_M_CFG_1);
25862c455caSZhou Wang 	writel(ARUSER_M_CFG_ENABLE, base + QM_ARUSER_M_CFG_ENABLE);
25962c455caSZhou Wang 	writel(AXUSER_BASE, base + QM_AWUSER_M_CFG_1);
26062c455caSZhou Wang 	writel(AWUSER_M_CFG_ENABLE, base + QM_AWUSER_M_CFG_ENABLE);
26162c455caSZhou Wang 	writel(WUSER_M_CFG_ENABLE, base + QM_WUSER_M_CFG_ENABLE);
26262c455caSZhou Wang 
26362c455caSZhou Wang 	/* qm cache */
26462c455caSZhou Wang 	writel(AXI_M_CFG, base + QM_AXI_M_CFG);
26562c455caSZhou Wang 	writel(AXI_M_CFG_ENABLE, base + QM_AXI_M_CFG_ENABLE);
2662ca73193SYang Shen 
26762c455caSZhou Wang 	/* disable FLR triggered by BME(bus master enable) */
26862c455caSZhou Wang 	writel(PEH_AXUSER_CFG, base + QM_PEH_AXUSER_CFG);
26962c455caSZhou Wang 	writel(PEH_AXUSER_CFG_ENABLE, base + QM_PEH_AXUSER_CFG_ENABLE);
27062c455caSZhou Wang 
27162c455caSZhou Wang 	/* cache */
27215b0694fSYang Shen 	writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_0);
27315b0694fSYang Shen 	writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_1);
27415b0694fSYang Shen 	writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_0);
27515b0694fSYang Shen 	writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_1);
27662c455caSZhou Wang 
27762c455caSZhou Wang 	/* user domain configurations */
27862c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_BD_RUSER_32_63);
27962c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_SGL_RUSER_32_63);
28062c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_BD_WUSER_32_63);
2819e00df71SZhangfei Gao 
28284c9b780SShukun Tan 	if (qm->use_sva) {
2839e00df71SZhangfei Gao 		writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_DATA_RUSER_32_63);
2849e00df71SZhangfei Gao 		writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_DATA_WUSER_32_63);
2859e00df71SZhangfei Gao 	} else {
28662c455caSZhou Wang 		writel(AXUSER_BASE, base + HZIP_DATA_RUSER_32_63);
28762c455caSZhou Wang 		writel(AXUSER_BASE, base + HZIP_DATA_WUSER_32_63);
2889e00df71SZhangfei Gao 	}
28962c455caSZhou Wang 
29062c455caSZhou Wang 	/* let's open all compression/decompression cores */
29115b0694fSYang Shen 	writel(HZIP_DECOMP_CHECK_ENABLE | HZIP_ALL_COMP_DECOMP_EN,
29262c455caSZhou Wang 	       base + HZIP_CLOCK_GATE_CTRL);
29362c455caSZhou Wang 
2942a928693SYang Shen 	/* enable sqc,cqc writeback */
29562c455caSZhou Wang 	writel(SQC_CACHE_ENABLE | CQC_CACHE_ENABLE | SQC_CACHE_WB_ENABLE |
29662c455caSZhou Wang 	       CQC_CACHE_WB_ENABLE | FIELD_PREP(SQC_CACHE_WB_THRD, 1) |
29762c455caSZhou Wang 	       FIELD_PREP(CQC_CACHE_WB_THRD, 1), base + QM_CACHE_CTL);
29884c9b780SShukun Tan 
29984c9b780SShukun Tan 	return 0;
30062c455caSZhou Wang }
30162c455caSZhou Wang 
302eaebf4c3SShukun Tan static void hisi_zip_hw_error_enable(struct hisi_qm *qm)
30362c455caSZhou Wang {
3047ce396faSShukun Tan 	u32 val;
3057ce396faSShukun Tan 
30662c455caSZhou Wang 	if (qm->ver == QM_HW_V1) {
307eaebf4c3SShukun Tan 		writel(HZIP_CORE_INT_MASK_ALL,
308eaebf4c3SShukun Tan 		       qm->io_base + HZIP_CORE_INT_MASK_REG);
309ee1788c6SZhou Wang 		dev_info(&qm->pdev->dev, "Does not support hw error handle\n");
31062c455caSZhou Wang 		return;
31162c455caSZhou Wang 	}
31262c455caSZhou Wang 
31362c455caSZhou Wang 	/* clear ZIP hw error source if having */
314eaebf4c3SShukun Tan 	writel(HZIP_CORE_INT_MASK_ALL, qm->io_base + HZIP_CORE_INT_SOURCE);
315eaebf4c3SShukun Tan 
316de3daf4bSShukun Tan 	/* configure error type */
317de3daf4bSShukun Tan 	writel(0x1, qm->io_base + HZIP_CORE_INT_RAS_CE_ENB);
318de3daf4bSShukun Tan 	writel(0x0, qm->io_base + HZIP_CORE_INT_RAS_FE_ENB);
319de3daf4bSShukun Tan 	writel(HZIP_CORE_INT_RAS_NFE_ENABLE,
320de3daf4bSShukun Tan 	       qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB);
321de3daf4bSShukun Tan 
32262c455caSZhou Wang 	/* enable ZIP hw error interrupts */
323eaebf4c3SShukun Tan 	writel(0, qm->io_base + HZIP_CORE_INT_MASK_REG);
3247ce396faSShukun Tan 
3257ce396faSShukun Tan 	/* enable ZIP block master OOO when m-bit error occur */
3267ce396faSShukun Tan 	val = readl(qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
3277ce396faSShukun Tan 	val = val | HZIP_AXI_SHUTDOWN_ENABLE;
3287ce396faSShukun Tan 	writel(val, qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
32962c455caSZhou Wang }
330eaebf4c3SShukun Tan 
331eaebf4c3SShukun Tan static void hisi_zip_hw_error_disable(struct hisi_qm *qm)
332eaebf4c3SShukun Tan {
3337ce396faSShukun Tan 	u32 val;
3347ce396faSShukun Tan 
335eaebf4c3SShukun Tan 	/* disable ZIP hw error interrupts */
336eaebf4c3SShukun Tan 	writel(HZIP_CORE_INT_MASK_ALL, qm->io_base + HZIP_CORE_INT_MASK_REG);
3377ce396faSShukun Tan 
3387ce396faSShukun Tan 	/* disable ZIP block master OOO when m-bit error occur */
3397ce396faSShukun Tan 	val = readl(qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
3407ce396faSShukun Tan 	val = val & ~HZIP_AXI_SHUTDOWN_ENABLE;
3417ce396faSShukun Tan 	writel(val, qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
34262c455caSZhou Wang }
34362c455caSZhou Wang 
34472c7a68dSZhou Wang static inline struct hisi_qm *file_to_qm(struct ctrl_debug_file *file)
34572c7a68dSZhou Wang {
34672c7a68dSZhou Wang 	struct hisi_zip *hisi_zip = file->ctrl->hisi_zip;
34772c7a68dSZhou Wang 
34872c7a68dSZhou Wang 	return &hisi_zip->qm;
34972c7a68dSZhou Wang }
35072c7a68dSZhou Wang 
35172c7a68dSZhou Wang static u32 current_qm_read(struct ctrl_debug_file *file)
35272c7a68dSZhou Wang {
35372c7a68dSZhou Wang 	struct hisi_qm *qm = file_to_qm(file);
35472c7a68dSZhou Wang 
35572c7a68dSZhou Wang 	return readl(qm->io_base + QM_DFX_MB_CNT_VF);
35672c7a68dSZhou Wang }
35772c7a68dSZhou Wang 
35872c7a68dSZhou Wang static int current_qm_write(struct ctrl_debug_file *file, u32 val)
35972c7a68dSZhou Wang {
36072c7a68dSZhou Wang 	struct hisi_qm *qm = file_to_qm(file);
36172c7a68dSZhou Wang 	u32 vfq_num;
36272c7a68dSZhou Wang 	u32 tmp;
36372c7a68dSZhou Wang 
364619e464aSShukun Tan 	if (val > qm->vfs_num)
36572c7a68dSZhou Wang 		return -EINVAL;
36672c7a68dSZhou Wang 
3672a928693SYang Shen 	/* According PF or VF Dev ID to calculation curr_qm_qp_num and store */
36872c7a68dSZhou Wang 	if (val == 0) {
36972c7a68dSZhou Wang 		qm->debug.curr_qm_qp_num = qm->qp_num;
37072c7a68dSZhou Wang 	} else {
371619e464aSShukun Tan 		vfq_num = (qm->ctrl_qp_num - qm->qp_num) / qm->vfs_num;
372619e464aSShukun Tan 		if (val == qm->vfs_num)
37372c7a68dSZhou Wang 			qm->debug.curr_qm_qp_num = qm->ctrl_qp_num -
374619e464aSShukun Tan 				qm->qp_num - (qm->vfs_num - 1) * vfq_num;
37572c7a68dSZhou Wang 		else
37672c7a68dSZhou Wang 			qm->debug.curr_qm_qp_num = vfq_num;
37772c7a68dSZhou Wang 	}
37872c7a68dSZhou Wang 
37972c7a68dSZhou Wang 	writel(val, qm->io_base + QM_DFX_MB_CNT_VF);
38072c7a68dSZhou Wang 	writel(val, qm->io_base + QM_DFX_DB_CNT_VF);
38172c7a68dSZhou Wang 
38272c7a68dSZhou Wang 	tmp = val |
38372c7a68dSZhou Wang 	      (readl(qm->io_base + QM_DFX_SQE_CNT_VF_SQN) & CURRENT_Q_MASK);
38472c7a68dSZhou Wang 	writel(tmp, qm->io_base + QM_DFX_SQE_CNT_VF_SQN);
38572c7a68dSZhou Wang 
38672c7a68dSZhou Wang 	tmp = val |
38772c7a68dSZhou Wang 	      (readl(qm->io_base + QM_DFX_CQE_CNT_VF_CQN) & CURRENT_Q_MASK);
38872c7a68dSZhou Wang 	writel(tmp, qm->io_base + QM_DFX_CQE_CNT_VF_CQN);
38972c7a68dSZhou Wang 
39072c7a68dSZhou Wang 	return  0;
39172c7a68dSZhou Wang }
39272c7a68dSZhou Wang 
39372c7a68dSZhou Wang static u32 clear_enable_read(struct ctrl_debug_file *file)
39472c7a68dSZhou Wang {
39572c7a68dSZhou Wang 	struct hisi_qm *qm = file_to_qm(file);
39672c7a68dSZhou Wang 
39772c7a68dSZhou Wang 	return readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) &
39815b0694fSYang Shen 		     HZIP_SOFT_CTRL_CNT_CLR_CE_BIT;
39972c7a68dSZhou Wang }
40072c7a68dSZhou Wang 
40172c7a68dSZhou Wang static int clear_enable_write(struct ctrl_debug_file *file, u32 val)
40272c7a68dSZhou Wang {
40372c7a68dSZhou Wang 	struct hisi_qm *qm = file_to_qm(file);
40472c7a68dSZhou Wang 	u32 tmp;
40572c7a68dSZhou Wang 
40672c7a68dSZhou Wang 	if (val != 1 && val != 0)
40772c7a68dSZhou Wang 		return -EINVAL;
40872c7a68dSZhou Wang 
40972c7a68dSZhou Wang 	tmp = (readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) &
41015b0694fSYang Shen 	       ~HZIP_SOFT_CTRL_CNT_CLR_CE_BIT) | val;
41172c7a68dSZhou Wang 	writel(tmp, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
41272c7a68dSZhou Wang 
41372c7a68dSZhou Wang 	return  0;
41472c7a68dSZhou Wang }
41572c7a68dSZhou Wang 
41615b0694fSYang Shen static ssize_t hisi_zip_ctrl_debug_read(struct file *filp, char __user *buf,
41772c7a68dSZhou Wang 					size_t count, loff_t *pos)
41872c7a68dSZhou Wang {
41972c7a68dSZhou Wang 	struct ctrl_debug_file *file = filp->private_data;
42072c7a68dSZhou Wang 	char tbuf[HZIP_BUF_SIZE];
42172c7a68dSZhou Wang 	u32 val;
42272c7a68dSZhou Wang 	int ret;
42372c7a68dSZhou Wang 
42472c7a68dSZhou Wang 	spin_lock_irq(&file->lock);
42572c7a68dSZhou Wang 	switch (file->index) {
42672c7a68dSZhou Wang 	case HZIP_CURRENT_QM:
42772c7a68dSZhou Wang 		val = current_qm_read(file);
42872c7a68dSZhou Wang 		break;
42972c7a68dSZhou Wang 	case HZIP_CLEAR_ENABLE:
43072c7a68dSZhou Wang 		val = clear_enable_read(file);
43172c7a68dSZhou Wang 		break;
43272c7a68dSZhou Wang 	default:
43372c7a68dSZhou Wang 		spin_unlock_irq(&file->lock);
43472c7a68dSZhou Wang 		return -EINVAL;
43572c7a68dSZhou Wang 	}
43672c7a68dSZhou Wang 	spin_unlock_irq(&file->lock);
437533b2079SYang Shen 	ret = scnprintf(tbuf, sizeof(tbuf), "%u\n", val);
43872c7a68dSZhou Wang 	return simple_read_from_buffer(buf, count, pos, tbuf, ret);
43972c7a68dSZhou Wang }
44072c7a68dSZhou Wang 
44115b0694fSYang Shen static ssize_t hisi_zip_ctrl_debug_write(struct file *filp,
44215b0694fSYang Shen 					 const char __user *buf,
44372c7a68dSZhou Wang 					 size_t count, loff_t *pos)
44472c7a68dSZhou Wang {
44572c7a68dSZhou Wang 	struct ctrl_debug_file *file = filp->private_data;
44672c7a68dSZhou Wang 	char tbuf[HZIP_BUF_SIZE];
44772c7a68dSZhou Wang 	unsigned long val;
44872c7a68dSZhou Wang 	int len, ret;
44972c7a68dSZhou Wang 
45072c7a68dSZhou Wang 	if (*pos != 0)
45172c7a68dSZhou Wang 		return 0;
45272c7a68dSZhou Wang 
45372c7a68dSZhou Wang 	if (count >= HZIP_BUF_SIZE)
45472c7a68dSZhou Wang 		return -ENOSPC;
45572c7a68dSZhou Wang 
45672c7a68dSZhou Wang 	len = simple_write_to_buffer(tbuf, HZIP_BUF_SIZE - 1, pos, buf, count);
45772c7a68dSZhou Wang 	if (len < 0)
45872c7a68dSZhou Wang 		return len;
45972c7a68dSZhou Wang 
46072c7a68dSZhou Wang 	tbuf[len] = '\0';
46172c7a68dSZhou Wang 	if (kstrtoul(tbuf, 0, &val))
46272c7a68dSZhou Wang 		return -EFAULT;
46372c7a68dSZhou Wang 
46472c7a68dSZhou Wang 	spin_lock_irq(&file->lock);
46572c7a68dSZhou Wang 	switch (file->index) {
46672c7a68dSZhou Wang 	case HZIP_CURRENT_QM:
46772c7a68dSZhou Wang 		ret = current_qm_write(file, val);
46872c7a68dSZhou Wang 		if (ret)
46972c7a68dSZhou Wang 			goto err_input;
47072c7a68dSZhou Wang 		break;
47172c7a68dSZhou Wang 	case HZIP_CLEAR_ENABLE:
47272c7a68dSZhou Wang 		ret = clear_enable_write(file, val);
47372c7a68dSZhou Wang 		if (ret)
47472c7a68dSZhou Wang 			goto err_input;
47572c7a68dSZhou Wang 		break;
47672c7a68dSZhou Wang 	default:
47772c7a68dSZhou Wang 		ret = -EINVAL;
47872c7a68dSZhou Wang 		goto err_input;
47972c7a68dSZhou Wang 	}
48072c7a68dSZhou Wang 	spin_unlock_irq(&file->lock);
48172c7a68dSZhou Wang 
48272c7a68dSZhou Wang 	return count;
48372c7a68dSZhou Wang 
48472c7a68dSZhou Wang err_input:
48572c7a68dSZhou Wang 	spin_unlock_irq(&file->lock);
48672c7a68dSZhou Wang 	return ret;
48772c7a68dSZhou Wang }
48872c7a68dSZhou Wang 
48972c7a68dSZhou Wang static const struct file_operations ctrl_debug_fops = {
49072c7a68dSZhou Wang 	.owner = THIS_MODULE,
49172c7a68dSZhou Wang 	.open = simple_open,
49215b0694fSYang Shen 	.read = hisi_zip_ctrl_debug_read,
49315b0694fSYang Shen 	.write = hisi_zip_ctrl_debug_write,
49472c7a68dSZhou Wang };
49572c7a68dSZhou Wang 
4966621e649SLongfang Liu static int zip_debugfs_atomic64_set(void *data, u64 val)
4976621e649SLongfang Liu {
4986621e649SLongfang Liu 	if (val)
4996621e649SLongfang Liu 		return -EINVAL;
5006621e649SLongfang Liu 
5016621e649SLongfang Liu 	atomic64_set((atomic64_t *)data, 0);
5026621e649SLongfang Liu 
5036621e649SLongfang Liu 	return 0;
5046621e649SLongfang Liu }
5056621e649SLongfang Liu 
5066621e649SLongfang Liu static int zip_debugfs_atomic64_get(void *data, u64 *val)
5076621e649SLongfang Liu {
5086621e649SLongfang Liu 	*val = atomic64_read((atomic64_t *)data);
5096621e649SLongfang Liu 
5106621e649SLongfang Liu 	return 0;
5116621e649SLongfang Liu }
5126621e649SLongfang Liu 
5136621e649SLongfang Liu DEFINE_DEBUGFS_ATTRIBUTE(zip_atomic64_ops, zip_debugfs_atomic64_get,
5146621e649SLongfang Liu 			 zip_debugfs_atomic64_set, "%llu\n");
5156621e649SLongfang Liu 
5164b33f057SShukun Tan static int hisi_zip_core_debug_init(struct hisi_qm *qm)
51772c7a68dSZhou Wang {
51872c7a68dSZhou Wang 	struct device *dev = &qm->pdev->dev;
51972c7a68dSZhou Wang 	struct debugfs_regset32 *regset;
5204a97bfc7SGreg Kroah-Hartman 	struct dentry *tmp_d;
52172c7a68dSZhou Wang 	char buf[HZIP_BUF_SIZE];
52272c7a68dSZhou Wang 	int i;
52372c7a68dSZhou Wang 
52472c7a68dSZhou Wang 	for (i = 0; i < HZIP_CORE_NUM; i++) {
52572c7a68dSZhou Wang 		if (i < HZIP_COMP_CORE_NUM)
526533b2079SYang Shen 			scnprintf(buf, sizeof(buf), "comp_core%d", i);
52772c7a68dSZhou Wang 		else
528533b2079SYang Shen 			scnprintf(buf, sizeof(buf), "decomp_core%d",
529533b2079SYang Shen 				  i - HZIP_COMP_CORE_NUM);
53072c7a68dSZhou Wang 
53172c7a68dSZhou Wang 		regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
53272c7a68dSZhou Wang 		if (!regset)
53372c7a68dSZhou Wang 			return -ENOENT;
53472c7a68dSZhou Wang 
53572c7a68dSZhou Wang 		regset->regs = hzip_dfx_regs;
53672c7a68dSZhou Wang 		regset->nregs = ARRAY_SIZE(hzip_dfx_regs);
53772c7a68dSZhou Wang 		regset->base = qm->io_base + core_offsets[i];
53872c7a68dSZhou Wang 
5394b33f057SShukun Tan 		tmp_d = debugfs_create_dir(buf, qm->debug.debug_root);
5404a97bfc7SGreg Kroah-Hartman 		debugfs_create_regset32("regs", 0444, tmp_d, regset);
54172c7a68dSZhou Wang 	}
54272c7a68dSZhou Wang 
54372c7a68dSZhou Wang 	return 0;
54472c7a68dSZhou Wang }
54572c7a68dSZhou Wang 
5466621e649SLongfang Liu static void hisi_zip_dfx_debug_init(struct hisi_qm *qm)
5476621e649SLongfang Liu {
5486621e649SLongfang Liu 	struct hisi_zip *zip = container_of(qm, struct hisi_zip, qm);
5496621e649SLongfang Liu 	struct hisi_zip_dfx *dfx = &zip->dfx;
5506621e649SLongfang Liu 	struct dentry *tmp_dir;
5516621e649SLongfang Liu 	void *data;
5526621e649SLongfang Liu 	int i;
5536621e649SLongfang Liu 
5546621e649SLongfang Liu 	tmp_dir = debugfs_create_dir("zip_dfx", qm->debug.debug_root);
5556621e649SLongfang Liu 	for (i = 0; i < ARRAY_SIZE(zip_dfx_files); i++) {
5566621e649SLongfang Liu 		data = (atomic64_t *)((uintptr_t)dfx + zip_dfx_files[i].offset);
5576621e649SLongfang Liu 		debugfs_create_file(zip_dfx_files[i].name,
5584b33f057SShukun Tan 				    0644, tmp_dir, data,
5596621e649SLongfang Liu 				    &zip_atomic64_ops);
5606621e649SLongfang Liu 	}
5616621e649SLongfang Liu }
5626621e649SLongfang Liu 
5634b33f057SShukun Tan static int hisi_zip_ctrl_debug_init(struct hisi_qm *qm)
56472c7a68dSZhou Wang {
5654b33f057SShukun Tan 	struct hisi_zip *zip = container_of(qm, struct hisi_zip, qm);
56672c7a68dSZhou Wang 	int i;
56772c7a68dSZhou Wang 
56872c7a68dSZhou Wang 	for (i = HZIP_CURRENT_QM; i < HZIP_DEBUG_FILE_NUM; i++) {
5694b33f057SShukun Tan 		spin_lock_init(&zip->ctrl->files[i].lock);
5704b33f057SShukun Tan 		zip->ctrl->files[i].ctrl = zip->ctrl;
5714b33f057SShukun Tan 		zip->ctrl->files[i].index = i;
57272c7a68dSZhou Wang 
5734a97bfc7SGreg Kroah-Hartman 		debugfs_create_file(ctrl_debug_file_name[i], 0600,
5744b33f057SShukun Tan 				    qm->debug.debug_root,
5754b33f057SShukun Tan 				    zip->ctrl->files + i,
57672c7a68dSZhou Wang 				    &ctrl_debug_fops);
57772c7a68dSZhou Wang 	}
57872c7a68dSZhou Wang 
5794b33f057SShukun Tan 	return hisi_zip_core_debug_init(qm);
58072c7a68dSZhou Wang }
58172c7a68dSZhou Wang 
5824b33f057SShukun Tan static int hisi_zip_debugfs_init(struct hisi_qm *qm)
58372c7a68dSZhou Wang {
58472c7a68dSZhou Wang 	struct device *dev = &qm->pdev->dev;
58572c7a68dSZhou Wang 	struct dentry *dev_d;
58672c7a68dSZhou Wang 	int ret;
58772c7a68dSZhou Wang 
58872c7a68dSZhou Wang 	dev_d = debugfs_create_dir(dev_name(dev), hzip_debugfs_root);
58972c7a68dSZhou Wang 
590c31dc9feSShukun Tan 	qm->debug.sqe_mask_offset = HZIP_SQE_MASK_OFFSET;
591c31dc9feSShukun Tan 	qm->debug.sqe_mask_len = HZIP_SQE_MASK_LEN;
59272c7a68dSZhou Wang 	qm->debug.debug_root = dev_d;
593a8ff38bdSWeili Qian 	hisi_qm_debug_init(qm);
59472c7a68dSZhou Wang 
59572c7a68dSZhou Wang 	if (qm->fun_type == QM_HW_PF) {
5964b33f057SShukun Tan 		ret = hisi_zip_ctrl_debug_init(qm);
59772c7a68dSZhou Wang 		if (ret)
59872c7a68dSZhou Wang 			goto failed_to_create;
59972c7a68dSZhou Wang 	}
60072c7a68dSZhou Wang 
6016621e649SLongfang Liu 	hisi_zip_dfx_debug_init(qm);
6026621e649SLongfang Liu 
60372c7a68dSZhou Wang 	return 0;
60472c7a68dSZhou Wang 
60572c7a68dSZhou Wang failed_to_create:
60672c7a68dSZhou Wang 	debugfs_remove_recursive(hzip_debugfs_root);
60772c7a68dSZhou Wang 	return ret;
60872c7a68dSZhou Wang }
60972c7a68dSZhou Wang 
610698f9523SHao Fang /* hisi_zip_debug_regs_clear() - clear the zip debug regs */
6114b33f057SShukun Tan static void hisi_zip_debug_regs_clear(struct hisi_qm *qm)
61272c7a68dSZhou Wang {
613698f9523SHao Fang 	int i, j;
614698f9523SHao Fang 
615698f9523SHao Fang 	/* clear current_qm */
61672c7a68dSZhou Wang 	writel(0x0, qm->io_base + QM_DFX_MB_CNT_VF);
61772c7a68dSZhou Wang 	writel(0x0, qm->io_base + QM_DFX_DB_CNT_VF);
618698f9523SHao Fang 
619698f9523SHao Fang 	/* enable register read_clear bit */
620698f9523SHao Fang 	writel(HZIP_RD_CNT_CLR_CE_EN, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
621698f9523SHao Fang 	for (i = 0; i < ARRAY_SIZE(core_offsets); i++)
622698f9523SHao Fang 		for (j = 0; j < ARRAY_SIZE(hzip_dfx_regs); j++)
623698f9523SHao Fang 			readl(qm->io_base + core_offsets[i] +
624698f9523SHao Fang 			      hzip_dfx_regs[j].offset);
625698f9523SHao Fang 
626698f9523SHao Fang 	/* disable register read_clear bit */
62772c7a68dSZhou Wang 	writel(0x0, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
62872c7a68dSZhou Wang 
62972c7a68dSZhou Wang 	hisi_qm_debug_regs_clear(qm);
63072c7a68dSZhou Wang }
63172c7a68dSZhou Wang 
6324b33f057SShukun Tan static void hisi_zip_debugfs_exit(struct hisi_qm *qm)
63372c7a68dSZhou Wang {
63472c7a68dSZhou Wang 	debugfs_remove_recursive(qm->debug.debug_root);
63572c7a68dSZhou Wang 
6364b33f057SShukun Tan 	if (qm->fun_type == QM_HW_PF) {
6374b33f057SShukun Tan 		hisi_zip_debug_regs_clear(qm);
6384b33f057SShukun Tan 		qm->debug.curr_qm_qp_num = 0;
6394b33f057SShukun Tan 	}
64072c7a68dSZhou Wang }
64172c7a68dSZhou Wang 
642f826e6efSShukun Tan static void hisi_zip_log_hw_error(struct hisi_qm *qm, u32 err_sts)
643f826e6efSShukun Tan {
644f826e6efSShukun Tan 	const struct hisi_zip_hw_error *err = zip_hw_error;
645f826e6efSShukun Tan 	struct device *dev = &qm->pdev->dev;
646f826e6efSShukun Tan 	u32 err_val;
647f826e6efSShukun Tan 
648f826e6efSShukun Tan 	while (err->msg) {
649f826e6efSShukun Tan 		if (err->int_msk & err_sts) {
650f826e6efSShukun Tan 			dev_err(dev, "%s [error status=0x%x] found\n",
651f826e6efSShukun Tan 				err->msg, err->int_msk);
652f826e6efSShukun Tan 
653f826e6efSShukun Tan 			if (err->int_msk & HZIP_CORE_INT_STATUS_M_ECC) {
654f826e6efSShukun Tan 				err_val = readl(qm->io_base +
655f826e6efSShukun Tan 						HZIP_CORE_SRAM_ECC_ERR_INFO);
656f826e6efSShukun Tan 				dev_err(dev, "hisi-zip multi ecc sram num=0x%x\n",
657f826e6efSShukun Tan 					((err_val >>
658f826e6efSShukun Tan 					HZIP_SRAM_ECC_ERR_NUM_SHIFT) & 0xFF));
659f826e6efSShukun Tan 			}
660f826e6efSShukun Tan 		}
661f826e6efSShukun Tan 		err++;
662f826e6efSShukun Tan 	}
663f826e6efSShukun Tan }
664f826e6efSShukun Tan 
665f826e6efSShukun Tan static u32 hisi_zip_get_hw_err_status(struct hisi_qm *qm)
666f826e6efSShukun Tan {
667f826e6efSShukun Tan 	return readl(qm->io_base + HZIP_CORE_INT_STATUS);
668f826e6efSShukun Tan }
669f826e6efSShukun Tan 
67084c9b780SShukun Tan static void hisi_zip_clear_hw_err_status(struct hisi_qm *qm, u32 err_sts)
67184c9b780SShukun Tan {
67284c9b780SShukun Tan 	writel(err_sts, qm->io_base + HZIP_CORE_INT_SOURCE);
67384c9b780SShukun Tan }
67484c9b780SShukun Tan 
67584c9b780SShukun Tan static void hisi_zip_open_axi_master_ooo(struct hisi_qm *qm)
67684c9b780SShukun Tan {
67784c9b780SShukun Tan 	u32 val;
67884c9b780SShukun Tan 
67984c9b780SShukun Tan 	val = readl(qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
68084c9b780SShukun Tan 
68184c9b780SShukun Tan 	writel(val & ~HZIP_AXI_SHUTDOWN_ENABLE,
68284c9b780SShukun Tan 	       qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
68384c9b780SShukun Tan 
68484c9b780SShukun Tan 	writel(val | HZIP_AXI_SHUTDOWN_ENABLE,
68584c9b780SShukun Tan 	       qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
68684c9b780SShukun Tan }
68784c9b780SShukun Tan 
68884c9b780SShukun Tan static void hisi_zip_close_axi_master_ooo(struct hisi_qm *qm)
68984c9b780SShukun Tan {
69084c9b780SShukun Tan 	u32 nfe_enb;
69184c9b780SShukun Tan 
69284c9b780SShukun Tan 	/* Disable ECC Mbit error report. */
69384c9b780SShukun Tan 	nfe_enb = readl(qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB);
69484c9b780SShukun Tan 	writel(nfe_enb & ~HZIP_CORE_INT_STATUS_M_ECC,
69584c9b780SShukun Tan 	       qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB);
69684c9b780SShukun Tan 
69784c9b780SShukun Tan 	/* Inject zip ECC Mbit error to block master ooo. */
69884c9b780SShukun Tan 	writel(HZIP_CORE_INT_STATUS_M_ECC,
69984c9b780SShukun Tan 	       qm->io_base + HZIP_CORE_INT_SET);
70084c9b780SShukun Tan }
70184c9b780SShukun Tan 
702eaebf4c3SShukun Tan static const struct hisi_qm_err_ini hisi_zip_err_ini = {
70384c9b780SShukun Tan 	.hw_init		= hisi_zip_set_user_domain_and_cache,
704eaebf4c3SShukun Tan 	.hw_err_enable		= hisi_zip_hw_error_enable,
705eaebf4c3SShukun Tan 	.hw_err_disable		= hisi_zip_hw_error_disable,
706f826e6efSShukun Tan 	.get_dev_hw_err_status	= hisi_zip_get_hw_err_status,
70784c9b780SShukun Tan 	.clear_dev_hw_err_status = hisi_zip_clear_hw_err_status,
708f826e6efSShukun Tan 	.log_dev_hw_err		= hisi_zip_log_hw_error,
70984c9b780SShukun Tan 	.open_axi_master_ooo	= hisi_zip_open_axi_master_ooo,
71084c9b780SShukun Tan 	.close_axi_master_ooo	= hisi_zip_close_axi_master_ooo,
711eaebf4c3SShukun Tan 	.err_info		= {
712eaebf4c3SShukun Tan 		.ce			= QM_BASE_CE,
713f826e6efSShukun Tan 		.nfe			= QM_BASE_NFE |
714f826e6efSShukun Tan 					  QM_ACC_WB_NOT_READY_TIMEOUT,
715eaebf4c3SShukun Tan 		.fe			= 0,
71684c9b780SShukun Tan 		.ecc_2bits_mask		= HZIP_CORE_INT_STATUS_M_ECC,
71784c9b780SShukun Tan 		.msi_wr_port		= HZIP_WR_PORT,
71884c9b780SShukun Tan 		.acpi_rst		= "ZRST",
71962c455caSZhou Wang 	}
720eaebf4c3SShukun Tan };
72162c455caSZhou Wang 
72262c455caSZhou Wang static int hisi_zip_pf_probe_init(struct hisi_zip *hisi_zip)
72362c455caSZhou Wang {
72462c455caSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
72562c455caSZhou Wang 	struct hisi_zip_ctrl *ctrl;
72662c455caSZhou Wang 
72762c455caSZhou Wang 	ctrl = devm_kzalloc(&qm->pdev->dev, sizeof(*ctrl), GFP_KERNEL);
72862c455caSZhou Wang 	if (!ctrl)
72962c455caSZhou Wang 		return -ENOMEM;
73062c455caSZhou Wang 
73162c455caSZhou Wang 	hisi_zip->ctrl = ctrl;
73262c455caSZhou Wang 	ctrl->hisi_zip = hisi_zip;
73362c455caSZhou Wang 
73458ca0060SWeili Qian 	if (qm->ver == QM_HW_V1)
73562c455caSZhou Wang 		qm->ctrl_qp_num = HZIP_QUEUE_NUM_V1;
73658ca0060SWeili Qian 	else
73762c455caSZhou Wang 		qm->ctrl_qp_num = HZIP_QUEUE_NUM_V2;
73862c455caSZhou Wang 
739eaebf4c3SShukun Tan 	qm->err_ini = &hisi_zip_err_ini;
740eaebf4c3SShukun Tan 
74184c9b780SShukun Tan 	hisi_zip_set_user_domain_and_cache(qm);
742eaebf4c3SShukun Tan 	hisi_qm_dev_err_init(qm);
7434b33f057SShukun Tan 	hisi_zip_debug_regs_clear(qm);
74462c455caSZhou Wang 
74562c455caSZhou Wang 	return 0;
74662c455caSZhou Wang }
74762c455caSZhou Wang 
748cfd66a66SLongfang Liu static int hisi_zip_qm_init(struct hisi_qm *qm, struct pci_dev *pdev)
74939977f4bSHao Fang {
750*1dc44035SYang Shen 	int ret;
751*1dc44035SYang Shen 
75239977f4bSHao Fang 	qm->pdev = pdev;
75358ca0060SWeili Qian 	qm->ver = pdev->revision;
7549e00df71SZhangfei Gao 	qm->algs = "zlib\ngzip";
75539977f4bSHao Fang 	qm->sqe_size = HZIP_SQE_SIZE;
75639977f4bSHao Fang 	qm->dev_name = hisi_zip_name;
757d9701f8dSWeili Qian 
758cfd66a66SLongfang Liu 	qm->fun_type = (pdev->device == PCI_DEVICE_ID_ZIP_PF) ?
759cfd66a66SLongfang Liu 			QM_HW_PF : QM_HW_VF;
760d9701f8dSWeili Qian 	if (qm->fun_type == QM_HW_PF) {
761d9701f8dSWeili Qian 		qm->qp_base = HZIP_PF_DEF_Q_BASE;
762d9701f8dSWeili Qian 		qm->qp_num = pf_q_num;
7632fcb4cc3SSihang Chen 		qm->debug.curr_qm_qp_num = pf_q_num;
764d9701f8dSWeili Qian 		qm->qm_list = &zip_devices;
765d9701f8dSWeili Qian 	} else if (qm->fun_type == QM_HW_VF && qm->ver == QM_HW_V1) {
766d9701f8dSWeili Qian 		/*
767d9701f8dSWeili Qian 		 * have no way to get qm configure in VM in v1 hardware,
768d9701f8dSWeili Qian 		 * so currently force PF to uses HZIP_PF_DEF_Q_NUM, and force
769d9701f8dSWeili Qian 		 * to trigger only one VF in v1 hardware.
770d9701f8dSWeili Qian 		 *
771d9701f8dSWeili Qian 		 * v2 hardware has no such problem.
772d9701f8dSWeili Qian 		 */
773d9701f8dSWeili Qian 		qm->qp_base = HZIP_PF_DEF_Q_NUM;
774d9701f8dSWeili Qian 		qm->qp_num = HZIP_QUEUE_NUM_V1 - HZIP_PF_DEF_Q_NUM;
775d9701f8dSWeili Qian 	}
776cfd66a66SLongfang Liu 
777*1dc44035SYang Shen 	qm->wq = alloc_workqueue("%s", WQ_HIGHPRI | WQ_MEM_RECLAIM |
778*1dc44035SYang Shen 				 WQ_UNBOUND, num_online_cpus(),
779*1dc44035SYang Shen 				 pci_name(qm->pdev));
780*1dc44035SYang Shen 	if (!qm->wq) {
781*1dc44035SYang Shen 		pci_err(qm->pdev, "fail to alloc workqueue\n");
782*1dc44035SYang Shen 		return -ENOMEM;
783*1dc44035SYang Shen 	}
784*1dc44035SYang Shen 
785*1dc44035SYang Shen 	ret = hisi_qm_init(qm);
786*1dc44035SYang Shen 	if (ret)
787*1dc44035SYang Shen 		destroy_workqueue(qm->wq);
788*1dc44035SYang Shen 
789*1dc44035SYang Shen 	return ret;
790*1dc44035SYang Shen }
791*1dc44035SYang Shen 
792*1dc44035SYang Shen static void hisi_zip_qm_uninit(struct hisi_qm *qm)
793*1dc44035SYang Shen {
794*1dc44035SYang Shen 	hisi_qm_uninit(qm);
795*1dc44035SYang Shen 	destroy_workqueue(qm->wq);
79639977f4bSHao Fang }
79739977f4bSHao Fang 
798cfd66a66SLongfang Liu static int hisi_zip_probe_init(struct hisi_zip *hisi_zip)
799cfd66a66SLongfang Liu {
800cfd66a66SLongfang Liu 	struct hisi_qm *qm = &hisi_zip->qm;
801cfd66a66SLongfang Liu 	int ret;
802cfd66a66SLongfang Liu 
80339977f4bSHao Fang 	if (qm->fun_type == QM_HW_PF) {
80439977f4bSHao Fang 		ret = hisi_zip_pf_probe_init(hisi_zip);
80539977f4bSHao Fang 		if (ret)
80639977f4bSHao Fang 			return ret;
807cfd66a66SLongfang Liu 	}
808cfd66a66SLongfang Liu 
809cfd66a66SLongfang Liu 	return 0;
810cfd66a66SLongfang Liu }
811cfd66a66SLongfang Liu 
812cfd66a66SLongfang Liu static int hisi_zip_probe(struct pci_dev *pdev, const struct pci_device_id *id)
813cfd66a66SLongfang Liu {
814cfd66a66SLongfang Liu 	struct hisi_zip *hisi_zip;
815cfd66a66SLongfang Liu 	struct hisi_qm *qm;
816cfd66a66SLongfang Liu 	int ret;
817cfd66a66SLongfang Liu 
818cfd66a66SLongfang Liu 	hisi_zip = devm_kzalloc(&pdev->dev, sizeof(*hisi_zip), GFP_KERNEL);
819cfd66a66SLongfang Liu 	if (!hisi_zip)
820cfd66a66SLongfang Liu 		return -ENOMEM;
821cfd66a66SLongfang Liu 
822cfd66a66SLongfang Liu 	qm = &hisi_zip->qm;
823cfd66a66SLongfang Liu 
824cfd66a66SLongfang Liu 	ret = hisi_zip_qm_init(qm, pdev);
825cfd66a66SLongfang Liu 	if (ret) {
826cfd66a66SLongfang Liu 		pci_err(pdev, "Failed to init ZIP QM (%d)!\n", ret);
827cfd66a66SLongfang Liu 		return ret;
828cfd66a66SLongfang Liu 	}
829cfd66a66SLongfang Liu 
830cfd66a66SLongfang Liu 	ret = hisi_zip_probe_init(hisi_zip);
831cfd66a66SLongfang Liu 	if (ret) {
832cfd66a66SLongfang Liu 		pci_err(pdev, "Failed to probe (%d)!\n", ret);
833cfd66a66SLongfang Liu 		goto err_qm_uninit;
83439977f4bSHao Fang 	}
83539977f4bSHao Fang 
83639977f4bSHao Fang 	ret = hisi_qm_start(qm);
83739977f4bSHao Fang 	if (ret)
8383d29e98dSYang Shen 		goto err_dev_err_uninit;
83939977f4bSHao Fang 
8404b33f057SShukun Tan 	ret = hisi_zip_debugfs_init(qm);
84139977f4bSHao Fang 	if (ret)
842b1a25820SYang Shen 		pci_err(pdev, "failed to init debugfs (%d)!\n", ret);
84339977f4bSHao Fang 
8443d29e98dSYang Shen 	ret = hisi_qm_alg_register(qm, &zip_devices);
8453d29e98dSYang Shen 	if (ret < 0) {
846b1a25820SYang Shen 		pci_err(pdev, "failed to register driver to crypto!\n");
8473d29e98dSYang Shen 		goto err_qm_stop;
8483d29e98dSYang Shen 	}
84939977f4bSHao Fang 
8509e00df71SZhangfei Gao 	if (qm->uacce) {
8519e00df71SZhangfei Gao 		ret = uacce_register(qm->uacce);
852b1a25820SYang Shen 		if (ret) {
853b1a25820SYang Shen 			pci_err(pdev, "failed to register uacce (%d)!\n", ret);
8543d29e98dSYang Shen 			goto err_qm_alg_unregister;
8559e00df71SZhangfei Gao 		}
856b1a25820SYang Shen 	}
8579e00df71SZhangfei Gao 
85839977f4bSHao Fang 	if (qm->fun_type == QM_HW_PF && vfs_num > 0) {
859cd1b7ae3SShukun Tan 		ret = hisi_qm_sriov_enable(pdev, vfs_num);
86039977f4bSHao Fang 		if (ret < 0)
8613d29e98dSYang Shen 			goto err_qm_alg_unregister;
86239977f4bSHao Fang 	}
86339977f4bSHao Fang 
86439977f4bSHao Fang 	return 0;
86539977f4bSHao Fang 
8663d29e98dSYang Shen err_qm_alg_unregister:
8673d29e98dSYang Shen 	hisi_qm_alg_unregister(qm, &zip_devices);
8683d29e98dSYang Shen 
8693d29e98dSYang Shen err_qm_stop:
8704b33f057SShukun Tan 	hisi_zip_debugfs_exit(qm);
871e88dd6e1SYang Shen 	hisi_qm_stop(qm, QM_NORMAL);
8723d29e98dSYang Shen 
8733d29e98dSYang Shen err_dev_err_uninit:
8743d29e98dSYang Shen 	hisi_qm_dev_err_uninit(qm);
8753d29e98dSYang Shen 
87639977f4bSHao Fang err_qm_uninit:
877*1dc44035SYang Shen 	hisi_zip_qm_uninit(qm);
878cfd66a66SLongfang Liu 
87939977f4bSHao Fang 	return ret;
88039977f4bSHao Fang }
88139977f4bSHao Fang 
88262c455caSZhou Wang static void hisi_zip_remove(struct pci_dev *pdev)
88362c455caSZhou Wang {
884d8140b87SYang Shen 	struct hisi_qm *qm = pci_get_drvdata(pdev);
88562c455caSZhou Wang 
886daa31783SWeili Qian 	hisi_qm_wait_task_finish(qm, &zip_devices);
8873d29e98dSYang Shen 	hisi_qm_alg_unregister(qm, &zip_devices);
8883d29e98dSYang Shen 
889619e464aSShukun Tan 	if (qm->fun_type == QM_HW_PF && qm->vfs_num)
890daa31783SWeili Qian 		hisi_qm_sriov_disable(pdev, qm->is_frozen);
89179e09f30SZhou Wang 
8924b33f057SShukun Tan 	hisi_zip_debugfs_exit(qm);
893e88dd6e1SYang Shen 	hisi_qm_stop(qm, QM_NORMAL);
894eaebf4c3SShukun Tan 	hisi_qm_dev_err_uninit(qm);
895*1dc44035SYang Shen 	hisi_zip_qm_uninit(qm);
89662c455caSZhou Wang }
89762c455caSZhou Wang 
89862c455caSZhou Wang static const struct pci_error_handlers hisi_zip_err_handler = {
899f826e6efSShukun Tan 	.error_detected	= hisi_qm_dev_err_detected,
90084c9b780SShukun Tan 	.slot_reset	= hisi_qm_dev_slot_reset,
9017ce396faSShukun Tan 	.reset_prepare	= hisi_qm_reset_prepare,
9027ce396faSShukun Tan 	.reset_done	= hisi_qm_reset_done,
90362c455caSZhou Wang };
90462c455caSZhou Wang 
90562c455caSZhou Wang static struct pci_driver hisi_zip_pci_driver = {
90662c455caSZhou Wang 	.name			= "hisi_zip",
90762c455caSZhou Wang 	.id_table		= hisi_zip_dev_ids,
90862c455caSZhou Wang 	.probe			= hisi_zip_probe,
90962c455caSZhou Wang 	.remove			= hisi_zip_remove,
910bf6a7a5aSArnd Bergmann 	.sriov_configure	= IS_ENABLED(CONFIG_PCI_IOV) ?
911cd1b7ae3SShukun Tan 					hisi_qm_sriov_configure : NULL,
91262c455caSZhou Wang 	.err_handler		= &hisi_zip_err_handler,
91364dfe495SYang Shen 	.shutdown		= hisi_qm_dev_shutdown,
91462c455caSZhou Wang };
91562c455caSZhou Wang 
91672c7a68dSZhou Wang static void hisi_zip_register_debugfs(void)
91772c7a68dSZhou Wang {
91872c7a68dSZhou Wang 	if (!debugfs_initialized())
91972c7a68dSZhou Wang 		return;
92072c7a68dSZhou Wang 
92172c7a68dSZhou Wang 	hzip_debugfs_root = debugfs_create_dir("hisi_zip", NULL);
92272c7a68dSZhou Wang }
92372c7a68dSZhou Wang 
92472c7a68dSZhou Wang static void hisi_zip_unregister_debugfs(void)
92572c7a68dSZhou Wang {
92672c7a68dSZhou Wang 	debugfs_remove_recursive(hzip_debugfs_root);
92772c7a68dSZhou Wang }
92872c7a68dSZhou Wang 
92962c455caSZhou Wang static int __init hisi_zip_init(void)
93062c455caSZhou Wang {
93162c455caSZhou Wang 	int ret;
93262c455caSZhou Wang 
93318f1ab3fSShukun Tan 	hisi_qm_init_list(&zip_devices);
93472c7a68dSZhou Wang 	hisi_zip_register_debugfs();
93572c7a68dSZhou Wang 
93662c455caSZhou Wang 	ret = pci_register_driver(&hisi_zip_pci_driver);
93762c455caSZhou Wang 	if (ret < 0) {
93872c7a68dSZhou Wang 		hisi_zip_unregister_debugfs();
9392ca73193SYang Shen 		pr_err("Failed to register pci driver.\n");
9402ca73193SYang Shen 	}
94172c7a68dSZhou Wang 
94262c455caSZhou Wang 	return ret;
94362c455caSZhou Wang }
94462c455caSZhou Wang 
94562c455caSZhou Wang static void __exit hisi_zip_exit(void)
94662c455caSZhou Wang {
94762c455caSZhou Wang 	pci_unregister_driver(&hisi_zip_pci_driver);
94872c7a68dSZhou Wang 	hisi_zip_unregister_debugfs();
94962c455caSZhou Wang }
95062c455caSZhou Wang 
95162c455caSZhou Wang module_init(hisi_zip_init);
95262c455caSZhou Wang module_exit(hisi_zip_exit);
95362c455caSZhou Wang 
95462c455caSZhou Wang MODULE_LICENSE("GPL v2");
95562c455caSZhou Wang MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>");
95662c455caSZhou Wang MODULE_DESCRIPTION("Driver for HiSilicon ZIP accelerator");
957