xref: /openbmc/linux/drivers/crypto/hisilicon/zip/zip_main.c (revision e88dd6e1d8370fb11af396747f3d5c16b436540f)
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_VF_NUM			63
2162c455caSZhou Wang #define HZIP_QUEUE_NUM_V1		4096
2262c455caSZhou Wang #define HZIP_QUEUE_NUM_V2		1024
2362c455caSZhou Wang 
2462c455caSZhou Wang #define HZIP_CLOCK_GATE_CTRL		0x301004
2562c455caSZhou Wang #define COMP0_ENABLE			BIT(0)
2662c455caSZhou Wang #define COMP1_ENABLE			BIT(1)
2762c455caSZhou Wang #define DECOMP0_ENABLE			BIT(2)
2862c455caSZhou Wang #define DECOMP1_ENABLE			BIT(3)
2962c455caSZhou Wang #define DECOMP2_ENABLE			BIT(4)
3062c455caSZhou Wang #define DECOMP3_ENABLE			BIT(5)
3162c455caSZhou Wang #define DECOMP4_ENABLE			BIT(6)
3262c455caSZhou Wang #define DECOMP5_ENABLE			BIT(7)
3362c455caSZhou Wang #define ALL_COMP_DECOMP_EN		(COMP0_ENABLE | COMP1_ENABLE |	\
3462c455caSZhou Wang 					 DECOMP0_ENABLE | DECOMP1_ENABLE | \
3562c455caSZhou Wang 					 DECOMP2_ENABLE | DECOMP3_ENABLE | \
3662c455caSZhou Wang 					 DECOMP4_ENABLE | DECOMP5_ENABLE)
3762c455caSZhou Wang #define DECOMP_CHECK_ENABLE		BIT(16)
3872c7a68dSZhou Wang #define HZIP_FSM_MAX_CNT		0x301008
3962c455caSZhou Wang 
4062c455caSZhou Wang #define HZIP_PORT_ARCA_CHE_0		0x301040
4162c455caSZhou Wang #define HZIP_PORT_ARCA_CHE_1		0x301044
4262c455caSZhou Wang #define HZIP_PORT_AWCA_CHE_0		0x301060
4362c455caSZhou Wang #define HZIP_PORT_AWCA_CHE_1		0x301064
4462c455caSZhou Wang #define CACHE_ALL_EN			0xffffffff
4562c455caSZhou Wang 
4662c455caSZhou Wang #define HZIP_BD_RUSER_32_63		0x301110
4762c455caSZhou Wang #define HZIP_SGL_RUSER_32_63		0x30111c
4862c455caSZhou Wang #define HZIP_DATA_RUSER_32_63		0x301128
4962c455caSZhou Wang #define HZIP_DATA_WUSER_32_63		0x301134
5062c455caSZhou Wang #define HZIP_BD_WUSER_32_63		0x301140
5162c455caSZhou Wang 
5272c7a68dSZhou Wang #define HZIP_QM_IDEL_STATUS		0x3040e4
5362c455caSZhou Wang 
5472c7a68dSZhou Wang #define HZIP_CORE_DEBUG_COMP_0		0x302000
5572c7a68dSZhou Wang #define HZIP_CORE_DEBUG_COMP_1		0x303000
5672c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_0	0x304000
5772c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_1	0x305000
5872c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_2	0x306000
5972c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_3	0x307000
6072c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_4	0x308000
6172c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_5	0x309000
6262c455caSZhou Wang 
6362c455caSZhou Wang #define HZIP_CORE_INT_SOURCE		0x3010A0
64eaebf4c3SShukun Tan #define HZIP_CORE_INT_MASK_REG		0x3010A4
6584c9b780SShukun Tan #define HZIP_CORE_INT_SET		0x3010A8
6662c455caSZhou Wang #define HZIP_CORE_INT_STATUS		0x3010AC
6762c455caSZhou Wang #define HZIP_CORE_INT_STATUS_M_ECC	BIT(1)
6862c455caSZhou Wang #define HZIP_CORE_SRAM_ECC_ERR_INFO	0x301148
69de3daf4bSShukun Tan #define HZIP_CORE_INT_RAS_CE_ENB	0x301160
70de3daf4bSShukun Tan #define HZIP_CORE_INT_RAS_NFE_ENB	0x301164
71de3daf4bSShukun Tan #define HZIP_CORE_INT_RAS_FE_ENB        0x301168
72de3daf4bSShukun Tan #define HZIP_CORE_INT_RAS_NFE_ENABLE	0x7FE
73f826e6efSShukun Tan #define HZIP_SRAM_ECC_ERR_NUM_SHIFT	16
74f826e6efSShukun Tan #define HZIP_SRAM_ECC_ERR_ADDR_SHIFT	24
75eaebf4c3SShukun Tan #define HZIP_CORE_INT_MASK_ALL		GENMASK(10, 0)
7672c7a68dSZhou Wang #define HZIP_COMP_CORE_NUM		2
7772c7a68dSZhou Wang #define HZIP_DECOMP_CORE_NUM		6
7872c7a68dSZhou Wang #define HZIP_CORE_NUM			(HZIP_COMP_CORE_NUM + \
7972c7a68dSZhou Wang 					 HZIP_DECOMP_CORE_NUM)
8062c455caSZhou Wang #define HZIP_SQE_SIZE			128
8172c7a68dSZhou Wang #define HZIP_SQ_SIZE			(HZIP_SQE_SIZE * QM_Q_DEPTH)
8262c455caSZhou Wang #define HZIP_PF_DEF_Q_NUM		64
8362c455caSZhou Wang #define HZIP_PF_DEF_Q_BASE		0
8462c455caSZhou Wang 
8572c7a68dSZhou Wang #define HZIP_SOFT_CTRL_CNT_CLR_CE	0x301000
8672c7a68dSZhou Wang #define SOFT_CTRL_CNT_CLR_CE_BIT	BIT(0)
8784c9b780SShukun Tan #define HZIP_SOFT_CTRL_ZIP_CONTROL	0x30100C
8884c9b780SShukun Tan #define HZIP_AXI_SHUTDOWN_ENABLE	BIT(14)
8984c9b780SShukun Tan #define HZIP_WR_PORT			BIT(11)
9062c455caSZhou Wang 
9172c7a68dSZhou Wang #define HZIP_BUF_SIZE			22
92c31dc9feSShukun Tan #define HZIP_SQE_MASK_OFFSET		64
93c31dc9feSShukun Tan #define HZIP_SQE_MASK_LEN		48
9462c455caSZhou Wang 
9562c455caSZhou Wang static const char hisi_zip_name[] = "hisi_zip";
9672c7a68dSZhou Wang static struct dentry *hzip_debugfs_root;
9718f1ab3fSShukun Tan static struct hisi_qm_list zip_devices;
9862c455caSZhou Wang 
9962c455caSZhou Wang struct hisi_zip_hw_error {
10062c455caSZhou Wang 	u32 int_msk;
10162c455caSZhou Wang 	const char *msg;
10262c455caSZhou Wang };
10362c455caSZhou Wang 
1046621e649SLongfang Liu struct zip_dfx_item {
1056621e649SLongfang Liu 	const char *name;
1066621e649SLongfang Liu 	u32 offset;
1076621e649SLongfang Liu };
1086621e649SLongfang Liu 
1096621e649SLongfang Liu static struct zip_dfx_item zip_dfx_files[] = {
1106621e649SLongfang Liu 	{"send_cnt", offsetof(struct hisi_zip_dfx, send_cnt)},
1116621e649SLongfang Liu 	{"recv_cnt", offsetof(struct hisi_zip_dfx, recv_cnt)},
1126621e649SLongfang Liu 	{"send_busy_cnt", offsetof(struct hisi_zip_dfx, send_busy_cnt)},
1136621e649SLongfang Liu 	{"err_bd_cnt", offsetof(struct hisi_zip_dfx, err_bd_cnt)},
1146621e649SLongfang Liu };
1156621e649SLongfang Liu 
11662c455caSZhou Wang static const struct hisi_zip_hw_error zip_hw_error[] = {
11762c455caSZhou Wang 	{ .int_msk = BIT(0), .msg = "zip_ecc_1bitt_err" },
11862c455caSZhou Wang 	{ .int_msk = BIT(1), .msg = "zip_ecc_2bit_err" },
11962c455caSZhou Wang 	{ .int_msk = BIT(2), .msg = "zip_axi_rresp_err" },
12062c455caSZhou Wang 	{ .int_msk = BIT(3), .msg = "zip_axi_bresp_err" },
12162c455caSZhou Wang 	{ .int_msk = BIT(4), .msg = "zip_src_addr_parse_err" },
12262c455caSZhou Wang 	{ .int_msk = BIT(5), .msg = "zip_dst_addr_parse_err" },
12362c455caSZhou Wang 	{ .int_msk = BIT(6), .msg = "zip_pre_in_addr_err" },
12462c455caSZhou Wang 	{ .int_msk = BIT(7), .msg = "zip_pre_in_data_err" },
12562c455caSZhou Wang 	{ .int_msk = BIT(8), .msg = "zip_com_inf_err" },
12662c455caSZhou Wang 	{ .int_msk = BIT(9), .msg = "zip_enc_inf_err" },
12762c455caSZhou Wang 	{ .int_msk = BIT(10), .msg = "zip_pre_out_err" },
12862c455caSZhou Wang 	{ /* sentinel */ }
12962c455caSZhou Wang };
13062c455caSZhou Wang 
13172c7a68dSZhou Wang enum ctrl_debug_file_index {
13272c7a68dSZhou Wang 	HZIP_CURRENT_QM,
13372c7a68dSZhou Wang 	HZIP_CLEAR_ENABLE,
13472c7a68dSZhou Wang 	HZIP_DEBUG_FILE_NUM,
13572c7a68dSZhou Wang };
13672c7a68dSZhou Wang 
13772c7a68dSZhou Wang static const char * const ctrl_debug_file_name[] = {
13872c7a68dSZhou Wang 	[HZIP_CURRENT_QM]   = "current_qm",
13972c7a68dSZhou Wang 	[HZIP_CLEAR_ENABLE] = "clear_enable",
14072c7a68dSZhou Wang };
14172c7a68dSZhou Wang 
14272c7a68dSZhou Wang struct ctrl_debug_file {
14372c7a68dSZhou Wang 	enum ctrl_debug_file_index index;
14472c7a68dSZhou Wang 	spinlock_t lock;
14572c7a68dSZhou Wang 	struct hisi_zip_ctrl *ctrl;
14672c7a68dSZhou Wang };
14772c7a68dSZhou Wang 
14862c455caSZhou Wang /*
14962c455caSZhou Wang  * One ZIP controller has one PF and multiple VFs, some global configurations
15062c455caSZhou Wang  * which PF has need this structure.
15162c455caSZhou Wang  *
15262c455caSZhou Wang  * Just relevant for PF.
15362c455caSZhou Wang  */
15462c455caSZhou Wang struct hisi_zip_ctrl {
15562c455caSZhou Wang 	struct hisi_zip *hisi_zip;
15672c7a68dSZhou Wang 	struct dentry *debug_root;
15772c7a68dSZhou Wang 	struct ctrl_debug_file files[HZIP_DEBUG_FILE_NUM];
15872c7a68dSZhou Wang };
15972c7a68dSZhou Wang 
16072c7a68dSZhou Wang enum {
16172c7a68dSZhou Wang 	HZIP_COMP_CORE0,
16272c7a68dSZhou Wang 	HZIP_COMP_CORE1,
16372c7a68dSZhou Wang 	HZIP_DECOMP_CORE0,
16472c7a68dSZhou Wang 	HZIP_DECOMP_CORE1,
16572c7a68dSZhou Wang 	HZIP_DECOMP_CORE2,
16672c7a68dSZhou Wang 	HZIP_DECOMP_CORE3,
16772c7a68dSZhou Wang 	HZIP_DECOMP_CORE4,
16872c7a68dSZhou Wang 	HZIP_DECOMP_CORE5,
16972c7a68dSZhou Wang };
17072c7a68dSZhou Wang 
17172c7a68dSZhou Wang static const u64 core_offsets[] = {
17272c7a68dSZhou Wang 	[HZIP_COMP_CORE0]   = 0x302000,
17372c7a68dSZhou Wang 	[HZIP_COMP_CORE1]   = 0x303000,
17472c7a68dSZhou Wang 	[HZIP_DECOMP_CORE0] = 0x304000,
17572c7a68dSZhou Wang 	[HZIP_DECOMP_CORE1] = 0x305000,
17672c7a68dSZhou Wang 	[HZIP_DECOMP_CORE2] = 0x306000,
17772c7a68dSZhou Wang 	[HZIP_DECOMP_CORE3] = 0x307000,
17872c7a68dSZhou Wang 	[HZIP_DECOMP_CORE4] = 0x308000,
17972c7a68dSZhou Wang 	[HZIP_DECOMP_CORE5] = 0x309000,
18072c7a68dSZhou Wang };
18172c7a68dSZhou Wang 
1828f68659bSRikard Falkeborn static const struct debugfs_reg32 hzip_dfx_regs[] = {
18372c7a68dSZhou Wang 	{"HZIP_GET_BD_NUM                ",  0x00ull},
18472c7a68dSZhou Wang 	{"HZIP_GET_RIGHT_BD              ",  0x04ull},
18572c7a68dSZhou Wang 	{"HZIP_GET_ERROR_BD              ",  0x08ull},
18672c7a68dSZhou Wang 	{"HZIP_DONE_BD_NUM               ",  0x0cull},
18772c7a68dSZhou Wang 	{"HZIP_WORK_CYCLE                ",  0x10ull},
18872c7a68dSZhou Wang 	{"HZIP_IDLE_CYCLE                ",  0x18ull},
18972c7a68dSZhou Wang 	{"HZIP_MAX_DELAY                 ",  0x20ull},
19072c7a68dSZhou Wang 	{"HZIP_MIN_DELAY                 ",  0x24ull},
19172c7a68dSZhou Wang 	{"HZIP_AVG_DELAY                 ",  0x28ull},
19272c7a68dSZhou Wang 	{"HZIP_MEM_VISIBLE_DATA          ",  0x30ull},
19372c7a68dSZhou Wang 	{"HZIP_MEM_VISIBLE_ADDR          ",  0x34ull},
19472c7a68dSZhou Wang 	{"HZIP_COMSUMED_BYTE             ",  0x38ull},
19572c7a68dSZhou Wang 	{"HZIP_PRODUCED_BYTE             ",  0x40ull},
19672c7a68dSZhou Wang 	{"HZIP_COMP_INF                  ",  0x70ull},
19772c7a68dSZhou Wang 	{"HZIP_PRE_OUT                   ",  0x78ull},
19872c7a68dSZhou Wang 	{"HZIP_BD_RD                     ",  0x7cull},
19972c7a68dSZhou Wang 	{"HZIP_BD_WR                     ",  0x80ull},
20072c7a68dSZhou Wang 	{"HZIP_GET_BD_AXI_ERR_NUM        ",  0x84ull},
20172c7a68dSZhou Wang 	{"HZIP_GET_BD_PARSE_ERR_NUM      ",  0x88ull},
20272c7a68dSZhou Wang 	{"HZIP_ADD_BD_AXI_ERR_NUM        ",  0x8cull},
20372c7a68dSZhou Wang 	{"HZIP_DECOMP_STF_RELOAD_CURR_ST ",  0x94ull},
20472c7a68dSZhou Wang 	{"HZIP_DECOMP_LZ77_CURR_ST       ",  0x9cull},
20562c455caSZhou Wang };
20662c455caSZhou Wang 
20762c455caSZhou Wang static int pf_q_num_set(const char *val, const struct kernel_param *kp)
20862c455caSZhou Wang {
20920b291f5SShukun Tan 	return q_num_set(val, kp, PCI_DEVICE_ID_ZIP_PF);
21062c455caSZhou Wang }
21162c455caSZhou Wang 
21262c455caSZhou Wang static const struct kernel_param_ops pf_q_num_ops = {
21362c455caSZhou Wang 	.set = pf_q_num_set,
21462c455caSZhou Wang 	.get = param_get_int,
21562c455caSZhou Wang };
21662c455caSZhou Wang 
21762c455caSZhou Wang static u32 pf_q_num = HZIP_PF_DEF_Q_NUM;
21862c455caSZhou Wang module_param_cb(pf_q_num, &pf_q_num_ops, &pf_q_num, 0444);
21962c455caSZhou Wang MODULE_PARM_DESC(pf_q_num, "Number of queues in PF(v1 1-4096, v2 1-1024)");
22062c455caSZhou Wang 
22135ee280fSHao Fang static const struct kernel_param_ops vfs_num_ops = {
22235ee280fSHao Fang 	.set = vfs_num_set,
22335ee280fSHao Fang 	.get = param_get_int,
22435ee280fSHao Fang };
22535ee280fSHao Fang 
22639977f4bSHao Fang static u32 vfs_num;
22735ee280fSHao Fang module_param_cb(vfs_num, &vfs_num_ops, &vfs_num, 0444);
22835ee280fSHao Fang MODULE_PARM_DESC(vfs_num, "Number of VFs to enable(1-63), 0(default)");
22939977f4bSHao Fang 
23062c455caSZhou Wang static const struct pci_device_id hisi_zip_dev_ids[] = {
23162c455caSZhou Wang 	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_ZIP_PF) },
23279e09f30SZhou Wang 	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_ZIP_VF) },
23362c455caSZhou Wang 	{ 0, }
23462c455caSZhou Wang };
23562c455caSZhou Wang MODULE_DEVICE_TABLE(pci, hisi_zip_dev_ids);
23662c455caSZhou Wang 
237813ec3f1SBarry Song int zip_create_qps(struct hisi_qp **qps, int qp_num, int node)
23862c455caSZhou Wang {
239813ec3f1SBarry Song 	if (node == NUMA_NO_NODE)
240813ec3f1SBarry Song 		node = cpu_to_node(smp_processor_id());
24162c455caSZhou Wang 
24218f1ab3fSShukun Tan 	return hisi_qm_alloc_qps_node(&zip_devices, qp_num, 0, node, qps);
24362c455caSZhou Wang }
24462c455caSZhou Wang 
24584c9b780SShukun Tan static int hisi_zip_set_user_domain_and_cache(struct hisi_qm *qm)
24662c455caSZhou Wang {
24784c9b780SShukun Tan 	void __iomem *base = qm->io_base;
24862c455caSZhou Wang 
24962c455caSZhou Wang 	/* qm user domain */
25062c455caSZhou Wang 	writel(AXUSER_BASE, base + QM_ARUSER_M_CFG_1);
25162c455caSZhou Wang 	writel(ARUSER_M_CFG_ENABLE, base + QM_ARUSER_M_CFG_ENABLE);
25262c455caSZhou Wang 	writel(AXUSER_BASE, base + QM_AWUSER_M_CFG_1);
25362c455caSZhou Wang 	writel(AWUSER_M_CFG_ENABLE, base + QM_AWUSER_M_CFG_ENABLE);
25462c455caSZhou Wang 	writel(WUSER_M_CFG_ENABLE, base + QM_WUSER_M_CFG_ENABLE);
25562c455caSZhou Wang 
25662c455caSZhou Wang 	/* qm cache */
25762c455caSZhou Wang 	writel(AXI_M_CFG, base + QM_AXI_M_CFG);
25862c455caSZhou Wang 	writel(AXI_M_CFG_ENABLE, base + QM_AXI_M_CFG_ENABLE);
25962c455caSZhou Wang 	/* disable FLR triggered by BME(bus master enable) */
26062c455caSZhou Wang 	writel(PEH_AXUSER_CFG, base + QM_PEH_AXUSER_CFG);
26162c455caSZhou Wang 	writel(PEH_AXUSER_CFG_ENABLE, base + QM_PEH_AXUSER_CFG_ENABLE);
26262c455caSZhou Wang 
26362c455caSZhou Wang 	/* cache */
26462c455caSZhou Wang 	writel(CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_0);
26562c455caSZhou Wang 	writel(CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_1);
26662c455caSZhou Wang 	writel(CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_0);
26762c455caSZhou Wang 	writel(CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_1);
26862c455caSZhou Wang 
26962c455caSZhou Wang 	/* user domain configurations */
27062c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_BD_RUSER_32_63);
27162c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_SGL_RUSER_32_63);
27262c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_BD_WUSER_32_63);
2739e00df71SZhangfei Gao 
27484c9b780SShukun Tan 	if (qm->use_sva) {
2759e00df71SZhangfei Gao 		writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_DATA_RUSER_32_63);
2769e00df71SZhangfei Gao 		writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_DATA_WUSER_32_63);
2779e00df71SZhangfei Gao 	} else {
27862c455caSZhou Wang 		writel(AXUSER_BASE, base + HZIP_DATA_RUSER_32_63);
27962c455caSZhou Wang 		writel(AXUSER_BASE, base + HZIP_DATA_WUSER_32_63);
2809e00df71SZhangfei Gao 	}
28162c455caSZhou Wang 
28262c455caSZhou Wang 	/* let's open all compression/decompression cores */
28362c455caSZhou Wang 	writel(DECOMP_CHECK_ENABLE | ALL_COMP_DECOMP_EN,
28462c455caSZhou Wang 	       base + HZIP_CLOCK_GATE_CTRL);
28562c455caSZhou Wang 
28662c455caSZhou Wang 	/* enable sqc writeback */
28762c455caSZhou Wang 	writel(SQC_CACHE_ENABLE | CQC_CACHE_ENABLE | SQC_CACHE_WB_ENABLE |
28862c455caSZhou Wang 	       CQC_CACHE_WB_ENABLE | FIELD_PREP(SQC_CACHE_WB_THRD, 1) |
28962c455caSZhou Wang 	       FIELD_PREP(CQC_CACHE_WB_THRD, 1), base + QM_CACHE_CTL);
29084c9b780SShukun Tan 
29184c9b780SShukun Tan 	return 0;
29262c455caSZhou Wang }
29362c455caSZhou Wang 
294eaebf4c3SShukun Tan static void hisi_zip_hw_error_enable(struct hisi_qm *qm)
29562c455caSZhou Wang {
2967ce396faSShukun Tan 	u32 val;
2977ce396faSShukun Tan 
29862c455caSZhou Wang 	if (qm->ver == QM_HW_V1) {
299eaebf4c3SShukun Tan 		writel(HZIP_CORE_INT_MASK_ALL,
300eaebf4c3SShukun Tan 		       qm->io_base + HZIP_CORE_INT_MASK_REG);
301ee1788c6SZhou Wang 		dev_info(&qm->pdev->dev, "Does not support hw error handle\n");
30262c455caSZhou Wang 		return;
30362c455caSZhou Wang 	}
30462c455caSZhou Wang 
30562c455caSZhou Wang 	/* clear ZIP hw error source if having */
306eaebf4c3SShukun Tan 	writel(HZIP_CORE_INT_MASK_ALL, qm->io_base + HZIP_CORE_INT_SOURCE);
307eaebf4c3SShukun Tan 
308de3daf4bSShukun Tan 	/* configure error type */
309de3daf4bSShukun Tan 	writel(0x1, qm->io_base + HZIP_CORE_INT_RAS_CE_ENB);
310de3daf4bSShukun Tan 	writel(0x0, qm->io_base + HZIP_CORE_INT_RAS_FE_ENB);
311de3daf4bSShukun Tan 	writel(HZIP_CORE_INT_RAS_NFE_ENABLE,
312de3daf4bSShukun Tan 		qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB);
313de3daf4bSShukun Tan 
31462c455caSZhou Wang 	/* enable ZIP hw error interrupts */
315eaebf4c3SShukun Tan 	writel(0, qm->io_base + HZIP_CORE_INT_MASK_REG);
3167ce396faSShukun Tan 
3177ce396faSShukun Tan 	/* enable ZIP block master OOO when m-bit error occur */
3187ce396faSShukun Tan 	val = readl(qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
3197ce396faSShukun Tan 	val = val | HZIP_AXI_SHUTDOWN_ENABLE;
3207ce396faSShukun Tan 	writel(val, qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
32162c455caSZhou Wang }
322eaebf4c3SShukun Tan 
323eaebf4c3SShukun Tan static void hisi_zip_hw_error_disable(struct hisi_qm *qm)
324eaebf4c3SShukun Tan {
3257ce396faSShukun Tan 	u32 val;
3267ce396faSShukun Tan 
327eaebf4c3SShukun Tan 	/* disable ZIP hw error interrupts */
328eaebf4c3SShukun Tan 	writel(HZIP_CORE_INT_MASK_ALL, qm->io_base + HZIP_CORE_INT_MASK_REG);
3297ce396faSShukun Tan 
3307ce396faSShukun Tan 	/* disable ZIP block master OOO when m-bit error occur */
3317ce396faSShukun Tan 	val = readl(qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
3327ce396faSShukun Tan 	val = val & ~HZIP_AXI_SHUTDOWN_ENABLE;
3337ce396faSShukun Tan 	writel(val, qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
33462c455caSZhou Wang }
33562c455caSZhou Wang 
33672c7a68dSZhou Wang static inline struct hisi_qm *file_to_qm(struct ctrl_debug_file *file)
33772c7a68dSZhou Wang {
33872c7a68dSZhou Wang 	struct hisi_zip *hisi_zip = file->ctrl->hisi_zip;
33972c7a68dSZhou Wang 
34072c7a68dSZhou Wang 	return &hisi_zip->qm;
34172c7a68dSZhou Wang }
34272c7a68dSZhou Wang 
34372c7a68dSZhou Wang static u32 current_qm_read(struct ctrl_debug_file *file)
34472c7a68dSZhou Wang {
34572c7a68dSZhou Wang 	struct hisi_qm *qm = file_to_qm(file);
34672c7a68dSZhou Wang 
34772c7a68dSZhou Wang 	return readl(qm->io_base + QM_DFX_MB_CNT_VF);
34872c7a68dSZhou Wang }
34972c7a68dSZhou Wang 
35072c7a68dSZhou Wang static int current_qm_write(struct ctrl_debug_file *file, u32 val)
35172c7a68dSZhou Wang {
35272c7a68dSZhou Wang 	struct hisi_qm *qm = file_to_qm(file);
35372c7a68dSZhou Wang 	u32 vfq_num;
35472c7a68dSZhou Wang 	u32 tmp;
35572c7a68dSZhou Wang 
356619e464aSShukun Tan 	if (val > qm->vfs_num)
35772c7a68dSZhou Wang 		return -EINVAL;
35872c7a68dSZhou Wang 
35972c7a68dSZhou Wang 	/* Calculate curr_qm_qp_num and store */
36072c7a68dSZhou Wang 	if (val == 0) {
36172c7a68dSZhou Wang 		qm->debug.curr_qm_qp_num = qm->qp_num;
36272c7a68dSZhou Wang 	} else {
363619e464aSShukun Tan 		vfq_num = (qm->ctrl_qp_num - qm->qp_num) / qm->vfs_num;
364619e464aSShukun Tan 		if (val == qm->vfs_num)
36572c7a68dSZhou Wang 			qm->debug.curr_qm_qp_num = qm->ctrl_qp_num -
366619e464aSShukun Tan 				qm->qp_num - (qm->vfs_num - 1) * vfq_num;
36772c7a68dSZhou Wang 		else
36872c7a68dSZhou Wang 			qm->debug.curr_qm_qp_num = vfq_num;
36972c7a68dSZhou Wang 	}
37072c7a68dSZhou Wang 
37172c7a68dSZhou Wang 	writel(val, qm->io_base + QM_DFX_MB_CNT_VF);
37272c7a68dSZhou Wang 	writel(val, qm->io_base + QM_DFX_DB_CNT_VF);
37372c7a68dSZhou Wang 
37472c7a68dSZhou Wang 	tmp = val |
37572c7a68dSZhou Wang 	      (readl(qm->io_base + QM_DFX_SQE_CNT_VF_SQN) & CURRENT_Q_MASK);
37672c7a68dSZhou Wang 	writel(tmp, qm->io_base + QM_DFX_SQE_CNT_VF_SQN);
37772c7a68dSZhou Wang 
37872c7a68dSZhou Wang 	tmp = val |
37972c7a68dSZhou Wang 	      (readl(qm->io_base + QM_DFX_CQE_CNT_VF_CQN) & CURRENT_Q_MASK);
38072c7a68dSZhou Wang 	writel(tmp, qm->io_base + QM_DFX_CQE_CNT_VF_CQN);
38172c7a68dSZhou Wang 
38272c7a68dSZhou Wang 	return  0;
38372c7a68dSZhou Wang }
38472c7a68dSZhou Wang 
38572c7a68dSZhou Wang static u32 clear_enable_read(struct ctrl_debug_file *file)
38672c7a68dSZhou Wang {
38772c7a68dSZhou Wang 	struct hisi_qm *qm = file_to_qm(file);
38872c7a68dSZhou Wang 
38972c7a68dSZhou Wang 	return readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) &
39072c7a68dSZhou Wang 	       SOFT_CTRL_CNT_CLR_CE_BIT;
39172c7a68dSZhou Wang }
39272c7a68dSZhou Wang 
39372c7a68dSZhou Wang static int clear_enable_write(struct ctrl_debug_file *file, u32 val)
39472c7a68dSZhou Wang {
39572c7a68dSZhou Wang 	struct hisi_qm *qm = file_to_qm(file);
39672c7a68dSZhou Wang 	u32 tmp;
39772c7a68dSZhou Wang 
39872c7a68dSZhou Wang 	if (val != 1 && val != 0)
39972c7a68dSZhou Wang 		return -EINVAL;
40072c7a68dSZhou Wang 
40172c7a68dSZhou Wang 	tmp = (readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) &
40272c7a68dSZhou Wang 	       ~SOFT_CTRL_CNT_CLR_CE_BIT) | val;
40372c7a68dSZhou Wang 	writel(tmp, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
40472c7a68dSZhou Wang 
40572c7a68dSZhou Wang 	return  0;
40672c7a68dSZhou Wang }
40772c7a68dSZhou Wang 
40872c7a68dSZhou Wang static ssize_t ctrl_debug_read(struct file *filp, char __user *buf,
40972c7a68dSZhou Wang 			       size_t count, loff_t *pos)
41072c7a68dSZhou Wang {
41172c7a68dSZhou Wang 	struct ctrl_debug_file *file = filp->private_data;
41272c7a68dSZhou Wang 	char tbuf[HZIP_BUF_SIZE];
41372c7a68dSZhou Wang 	u32 val;
41472c7a68dSZhou Wang 	int ret;
41572c7a68dSZhou Wang 
41672c7a68dSZhou Wang 	spin_lock_irq(&file->lock);
41772c7a68dSZhou Wang 	switch (file->index) {
41872c7a68dSZhou Wang 	case HZIP_CURRENT_QM:
41972c7a68dSZhou Wang 		val = current_qm_read(file);
42072c7a68dSZhou Wang 		break;
42172c7a68dSZhou Wang 	case HZIP_CLEAR_ENABLE:
42272c7a68dSZhou Wang 		val = clear_enable_read(file);
42372c7a68dSZhou Wang 		break;
42472c7a68dSZhou Wang 	default:
42572c7a68dSZhou Wang 		spin_unlock_irq(&file->lock);
42672c7a68dSZhou Wang 		return -EINVAL;
42772c7a68dSZhou Wang 	}
42872c7a68dSZhou Wang 	spin_unlock_irq(&file->lock);
42972c7a68dSZhou Wang 	ret = sprintf(tbuf, "%u\n", val);
43072c7a68dSZhou Wang 	return simple_read_from_buffer(buf, count, pos, tbuf, ret);
43172c7a68dSZhou Wang }
43272c7a68dSZhou Wang 
43372c7a68dSZhou Wang static ssize_t ctrl_debug_write(struct file *filp, const char __user *buf,
43472c7a68dSZhou Wang 				size_t count, loff_t *pos)
43572c7a68dSZhou Wang {
43672c7a68dSZhou Wang 	struct ctrl_debug_file *file = filp->private_data;
43772c7a68dSZhou Wang 	char tbuf[HZIP_BUF_SIZE];
43872c7a68dSZhou Wang 	unsigned long val;
43972c7a68dSZhou Wang 	int len, ret;
44072c7a68dSZhou Wang 
44172c7a68dSZhou Wang 	if (*pos != 0)
44272c7a68dSZhou Wang 		return 0;
44372c7a68dSZhou Wang 
44472c7a68dSZhou Wang 	if (count >= HZIP_BUF_SIZE)
44572c7a68dSZhou Wang 		return -ENOSPC;
44672c7a68dSZhou Wang 
44772c7a68dSZhou Wang 	len = simple_write_to_buffer(tbuf, HZIP_BUF_SIZE - 1, pos, buf, count);
44872c7a68dSZhou Wang 	if (len < 0)
44972c7a68dSZhou Wang 		return len;
45072c7a68dSZhou Wang 
45172c7a68dSZhou Wang 	tbuf[len] = '\0';
45272c7a68dSZhou Wang 	if (kstrtoul(tbuf, 0, &val))
45372c7a68dSZhou Wang 		return -EFAULT;
45472c7a68dSZhou Wang 
45572c7a68dSZhou Wang 	spin_lock_irq(&file->lock);
45672c7a68dSZhou Wang 	switch (file->index) {
45772c7a68dSZhou Wang 	case HZIP_CURRENT_QM:
45872c7a68dSZhou Wang 		ret = current_qm_write(file, val);
45972c7a68dSZhou Wang 		if (ret)
46072c7a68dSZhou Wang 			goto err_input;
46172c7a68dSZhou Wang 		break;
46272c7a68dSZhou Wang 	case HZIP_CLEAR_ENABLE:
46372c7a68dSZhou Wang 		ret = clear_enable_write(file, val);
46472c7a68dSZhou Wang 		if (ret)
46572c7a68dSZhou Wang 			goto err_input;
46672c7a68dSZhou Wang 		break;
46772c7a68dSZhou Wang 	default:
46872c7a68dSZhou Wang 		ret = -EINVAL;
46972c7a68dSZhou Wang 		goto err_input;
47072c7a68dSZhou Wang 	}
47172c7a68dSZhou Wang 	spin_unlock_irq(&file->lock);
47272c7a68dSZhou Wang 
47372c7a68dSZhou Wang 	return count;
47472c7a68dSZhou Wang 
47572c7a68dSZhou Wang err_input:
47672c7a68dSZhou Wang 	spin_unlock_irq(&file->lock);
47772c7a68dSZhou Wang 	return ret;
47872c7a68dSZhou Wang }
47972c7a68dSZhou Wang 
48072c7a68dSZhou Wang static const struct file_operations ctrl_debug_fops = {
48172c7a68dSZhou Wang 	.owner = THIS_MODULE,
48272c7a68dSZhou Wang 	.open = simple_open,
48372c7a68dSZhou Wang 	.read = ctrl_debug_read,
48472c7a68dSZhou Wang 	.write = ctrl_debug_write,
48572c7a68dSZhou Wang };
48672c7a68dSZhou Wang 
4876621e649SLongfang Liu 
4886621e649SLongfang Liu static int zip_debugfs_atomic64_set(void *data, u64 val)
4896621e649SLongfang Liu {
4906621e649SLongfang Liu 	if (val)
4916621e649SLongfang Liu 		return -EINVAL;
4926621e649SLongfang Liu 
4936621e649SLongfang Liu 	atomic64_set((atomic64_t *)data, 0);
4946621e649SLongfang Liu 
4956621e649SLongfang Liu 	return 0;
4966621e649SLongfang Liu }
4976621e649SLongfang Liu 
4986621e649SLongfang Liu static int zip_debugfs_atomic64_get(void *data, u64 *val)
4996621e649SLongfang Liu {
5006621e649SLongfang Liu 	*val = atomic64_read((atomic64_t *)data);
5016621e649SLongfang Liu 
5026621e649SLongfang Liu 	return 0;
5036621e649SLongfang Liu }
5046621e649SLongfang Liu 
5056621e649SLongfang Liu DEFINE_DEBUGFS_ATTRIBUTE(zip_atomic64_ops, zip_debugfs_atomic64_get,
5066621e649SLongfang Liu 			 zip_debugfs_atomic64_set, "%llu\n");
5076621e649SLongfang Liu 
50872c7a68dSZhou Wang static int hisi_zip_core_debug_init(struct hisi_zip_ctrl *ctrl)
50972c7a68dSZhou Wang {
51072c7a68dSZhou Wang 	struct hisi_zip *hisi_zip = ctrl->hisi_zip;
51172c7a68dSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
51272c7a68dSZhou Wang 	struct device *dev = &qm->pdev->dev;
51372c7a68dSZhou Wang 	struct debugfs_regset32 *regset;
5144a97bfc7SGreg Kroah-Hartman 	struct dentry *tmp_d;
51572c7a68dSZhou Wang 	char buf[HZIP_BUF_SIZE];
51672c7a68dSZhou Wang 	int i;
51772c7a68dSZhou Wang 
51872c7a68dSZhou Wang 	for (i = 0; i < HZIP_CORE_NUM; i++) {
51972c7a68dSZhou Wang 		if (i < HZIP_COMP_CORE_NUM)
52072c7a68dSZhou Wang 			sprintf(buf, "comp_core%d", i);
52172c7a68dSZhou Wang 		else
52272c7a68dSZhou Wang 			sprintf(buf, "decomp_core%d", i - HZIP_COMP_CORE_NUM);
52372c7a68dSZhou Wang 
52472c7a68dSZhou Wang 		regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
52572c7a68dSZhou Wang 		if (!regset)
52672c7a68dSZhou Wang 			return -ENOENT;
52772c7a68dSZhou Wang 
52872c7a68dSZhou Wang 		regset->regs = hzip_dfx_regs;
52972c7a68dSZhou Wang 		regset->nregs = ARRAY_SIZE(hzip_dfx_regs);
53072c7a68dSZhou Wang 		regset->base = qm->io_base + core_offsets[i];
53172c7a68dSZhou Wang 
5324a97bfc7SGreg Kroah-Hartman 		tmp_d = debugfs_create_dir(buf, ctrl->debug_root);
5334a97bfc7SGreg Kroah-Hartman 		debugfs_create_regset32("regs", 0444, tmp_d, regset);
53472c7a68dSZhou Wang 	}
53572c7a68dSZhou Wang 
53672c7a68dSZhou Wang 	return 0;
53772c7a68dSZhou Wang }
53872c7a68dSZhou Wang 
5396621e649SLongfang Liu static void hisi_zip_dfx_debug_init(struct hisi_qm *qm)
5406621e649SLongfang Liu {
5416621e649SLongfang Liu 	struct hisi_zip *zip = container_of(qm, struct hisi_zip, qm);
5426621e649SLongfang Liu 	struct hisi_zip_dfx *dfx = &zip->dfx;
5436621e649SLongfang Liu 	struct dentry *tmp_dir;
5446621e649SLongfang Liu 	void *data;
5456621e649SLongfang Liu 	int i;
5466621e649SLongfang Liu 
5476621e649SLongfang Liu 	tmp_dir = debugfs_create_dir("zip_dfx", qm->debug.debug_root);
5486621e649SLongfang Liu 	for (i = 0; i < ARRAY_SIZE(zip_dfx_files); i++) {
5496621e649SLongfang Liu 		data = (atomic64_t *)((uintptr_t)dfx + zip_dfx_files[i].offset);
5506621e649SLongfang Liu 		debugfs_create_file(zip_dfx_files[i].name,
5516621e649SLongfang Liu 			0644,
5526621e649SLongfang Liu 			tmp_dir,
5536621e649SLongfang Liu 			data,
5546621e649SLongfang Liu 			&zip_atomic64_ops);
5556621e649SLongfang Liu 	}
5566621e649SLongfang Liu }
5576621e649SLongfang Liu 
55872c7a68dSZhou Wang static int hisi_zip_ctrl_debug_init(struct hisi_zip_ctrl *ctrl)
55972c7a68dSZhou Wang {
56072c7a68dSZhou Wang 	int i;
56172c7a68dSZhou Wang 
56272c7a68dSZhou Wang 	for (i = HZIP_CURRENT_QM; i < HZIP_DEBUG_FILE_NUM; i++) {
56372c7a68dSZhou Wang 		spin_lock_init(&ctrl->files[i].lock);
56472c7a68dSZhou Wang 		ctrl->files[i].ctrl = ctrl;
56572c7a68dSZhou Wang 		ctrl->files[i].index = i;
56672c7a68dSZhou Wang 
5674a97bfc7SGreg Kroah-Hartman 		debugfs_create_file(ctrl_debug_file_name[i], 0600,
56872c7a68dSZhou Wang 				    ctrl->debug_root, ctrl->files + i,
56972c7a68dSZhou Wang 				    &ctrl_debug_fops);
57072c7a68dSZhou Wang 	}
57172c7a68dSZhou Wang 
57272c7a68dSZhou Wang 	return hisi_zip_core_debug_init(ctrl);
57372c7a68dSZhou Wang }
57472c7a68dSZhou Wang 
57572c7a68dSZhou Wang static int hisi_zip_debugfs_init(struct hisi_zip *hisi_zip)
57672c7a68dSZhou Wang {
57772c7a68dSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
57872c7a68dSZhou Wang 	struct device *dev = &qm->pdev->dev;
57972c7a68dSZhou Wang 	struct dentry *dev_d;
58072c7a68dSZhou Wang 	int ret;
58172c7a68dSZhou Wang 
58272c7a68dSZhou Wang 	dev_d = debugfs_create_dir(dev_name(dev), hzip_debugfs_root);
58372c7a68dSZhou Wang 
584c31dc9feSShukun Tan 	qm->debug.sqe_mask_offset = HZIP_SQE_MASK_OFFSET;
585c31dc9feSShukun Tan 	qm->debug.sqe_mask_len = HZIP_SQE_MASK_LEN;
58672c7a68dSZhou Wang 	qm->debug.debug_root = dev_d;
58772c7a68dSZhou Wang 	ret = hisi_qm_debug_init(qm);
58872c7a68dSZhou Wang 	if (ret)
58972c7a68dSZhou Wang 		goto failed_to_create;
59072c7a68dSZhou Wang 
59172c7a68dSZhou Wang 	if (qm->fun_type == QM_HW_PF) {
59272c7a68dSZhou Wang 		hisi_zip->ctrl->debug_root = dev_d;
59372c7a68dSZhou Wang 		ret = hisi_zip_ctrl_debug_init(hisi_zip->ctrl);
59472c7a68dSZhou Wang 		if (ret)
59572c7a68dSZhou Wang 			goto failed_to_create;
59672c7a68dSZhou Wang 	}
59772c7a68dSZhou Wang 
5986621e649SLongfang Liu 	hisi_zip_dfx_debug_init(qm);
5996621e649SLongfang Liu 
60072c7a68dSZhou Wang 	return 0;
60172c7a68dSZhou Wang 
60272c7a68dSZhou Wang failed_to_create:
60372c7a68dSZhou Wang 	debugfs_remove_recursive(hzip_debugfs_root);
60472c7a68dSZhou Wang 	return ret;
60572c7a68dSZhou Wang }
60672c7a68dSZhou Wang 
60772c7a68dSZhou Wang static void hisi_zip_debug_regs_clear(struct hisi_zip *hisi_zip)
60872c7a68dSZhou Wang {
60972c7a68dSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
61072c7a68dSZhou Wang 
61172c7a68dSZhou Wang 	writel(0x0, qm->io_base + QM_DFX_MB_CNT_VF);
61272c7a68dSZhou Wang 	writel(0x0, qm->io_base + QM_DFX_DB_CNT_VF);
61372c7a68dSZhou Wang 	writel(0x0, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
61472c7a68dSZhou Wang 
61572c7a68dSZhou Wang 	hisi_qm_debug_regs_clear(qm);
61672c7a68dSZhou Wang }
61772c7a68dSZhou Wang 
61872c7a68dSZhou Wang static void hisi_zip_debugfs_exit(struct hisi_zip *hisi_zip)
61972c7a68dSZhou Wang {
62072c7a68dSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
62172c7a68dSZhou Wang 
62272c7a68dSZhou Wang 	debugfs_remove_recursive(qm->debug.debug_root);
62372c7a68dSZhou Wang 
62472c7a68dSZhou Wang 	if (qm->fun_type == QM_HW_PF)
62572c7a68dSZhou Wang 		hisi_zip_debug_regs_clear(hisi_zip);
62672c7a68dSZhou Wang }
62772c7a68dSZhou Wang 
628f826e6efSShukun Tan static void hisi_zip_log_hw_error(struct hisi_qm *qm, u32 err_sts)
629f826e6efSShukun Tan {
630f826e6efSShukun Tan 	const struct hisi_zip_hw_error *err = zip_hw_error;
631f826e6efSShukun Tan 	struct device *dev = &qm->pdev->dev;
632f826e6efSShukun Tan 	u32 err_val;
633f826e6efSShukun Tan 
634f826e6efSShukun Tan 	while (err->msg) {
635f826e6efSShukun Tan 		if (err->int_msk & err_sts) {
636f826e6efSShukun Tan 			dev_err(dev, "%s [error status=0x%x] found\n",
637f826e6efSShukun Tan 				 err->msg, err->int_msk);
638f826e6efSShukun Tan 
639f826e6efSShukun Tan 			if (err->int_msk & HZIP_CORE_INT_STATUS_M_ECC) {
640f826e6efSShukun Tan 				err_val = readl(qm->io_base +
641f826e6efSShukun Tan 						HZIP_CORE_SRAM_ECC_ERR_INFO);
642f826e6efSShukun Tan 				dev_err(dev, "hisi-zip multi ecc sram num=0x%x\n",
643f826e6efSShukun Tan 					((err_val >>
644f826e6efSShukun Tan 					HZIP_SRAM_ECC_ERR_NUM_SHIFT) & 0xFF));
645f826e6efSShukun Tan 				dev_err(dev, "hisi-zip multi ecc sram addr=0x%x\n",
646f826e6efSShukun Tan 					(err_val >>
647f826e6efSShukun Tan 					HZIP_SRAM_ECC_ERR_ADDR_SHIFT));
648f826e6efSShukun Tan 			}
649f826e6efSShukun Tan 		}
650f826e6efSShukun Tan 		err++;
651f826e6efSShukun Tan 	}
652f826e6efSShukun Tan }
653f826e6efSShukun Tan 
654f826e6efSShukun Tan static u32 hisi_zip_get_hw_err_status(struct hisi_qm *qm)
655f826e6efSShukun Tan {
656f826e6efSShukun Tan 	return readl(qm->io_base + HZIP_CORE_INT_STATUS);
657f826e6efSShukun Tan }
658f826e6efSShukun Tan 
65984c9b780SShukun Tan static void hisi_zip_clear_hw_err_status(struct hisi_qm *qm, u32 err_sts)
66084c9b780SShukun Tan {
66184c9b780SShukun Tan 	writel(err_sts, qm->io_base + HZIP_CORE_INT_SOURCE);
66284c9b780SShukun Tan }
66384c9b780SShukun Tan 
66484c9b780SShukun Tan static void hisi_zip_open_axi_master_ooo(struct hisi_qm *qm)
66584c9b780SShukun Tan {
66684c9b780SShukun Tan 	u32 val;
66784c9b780SShukun Tan 
66884c9b780SShukun Tan 	val = readl(qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
66984c9b780SShukun Tan 
67084c9b780SShukun Tan 	writel(val & ~HZIP_AXI_SHUTDOWN_ENABLE,
67184c9b780SShukun Tan 	       qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
67284c9b780SShukun Tan 
67384c9b780SShukun Tan 	writel(val | HZIP_AXI_SHUTDOWN_ENABLE,
67484c9b780SShukun Tan 	       qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
67584c9b780SShukun Tan }
67684c9b780SShukun Tan 
67784c9b780SShukun Tan static void hisi_zip_close_axi_master_ooo(struct hisi_qm *qm)
67884c9b780SShukun Tan {
67984c9b780SShukun Tan 	u32 nfe_enb;
68084c9b780SShukun Tan 
68184c9b780SShukun Tan 	/* Disable ECC Mbit error report. */
68284c9b780SShukun Tan 	nfe_enb = readl(qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB);
68384c9b780SShukun Tan 	writel(nfe_enb & ~HZIP_CORE_INT_STATUS_M_ECC,
68484c9b780SShukun Tan 	       qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB);
68584c9b780SShukun Tan 
68684c9b780SShukun Tan 	/* Inject zip ECC Mbit error to block master ooo. */
68784c9b780SShukun Tan 	writel(HZIP_CORE_INT_STATUS_M_ECC,
68884c9b780SShukun Tan 	       qm->io_base + HZIP_CORE_INT_SET);
68984c9b780SShukun Tan }
69084c9b780SShukun Tan 
691eaebf4c3SShukun Tan static const struct hisi_qm_err_ini hisi_zip_err_ini = {
69284c9b780SShukun Tan 	.hw_init		= hisi_zip_set_user_domain_and_cache,
693eaebf4c3SShukun Tan 	.hw_err_enable		= hisi_zip_hw_error_enable,
694eaebf4c3SShukun Tan 	.hw_err_disable		= hisi_zip_hw_error_disable,
695f826e6efSShukun Tan 	.get_dev_hw_err_status	= hisi_zip_get_hw_err_status,
69684c9b780SShukun Tan 	.clear_dev_hw_err_status = hisi_zip_clear_hw_err_status,
697f826e6efSShukun Tan 	.log_dev_hw_err		= hisi_zip_log_hw_error,
69884c9b780SShukun Tan 	.open_axi_master_ooo	= hisi_zip_open_axi_master_ooo,
69984c9b780SShukun Tan 	.close_axi_master_ooo	= hisi_zip_close_axi_master_ooo,
700eaebf4c3SShukun Tan 	.err_info		= {
701eaebf4c3SShukun Tan 		.ce			= QM_BASE_CE,
702f826e6efSShukun Tan 		.nfe			= QM_BASE_NFE |
703f826e6efSShukun Tan 					  QM_ACC_WB_NOT_READY_TIMEOUT,
704eaebf4c3SShukun Tan 		.fe			= 0,
70584c9b780SShukun Tan 		.ecc_2bits_mask		= HZIP_CORE_INT_STATUS_M_ECC,
70684c9b780SShukun Tan 		.msi_wr_port		= HZIP_WR_PORT,
70784c9b780SShukun Tan 		.acpi_rst		= "ZRST",
70862c455caSZhou Wang 	}
709eaebf4c3SShukun Tan };
71062c455caSZhou Wang 
71162c455caSZhou Wang static int hisi_zip_pf_probe_init(struct hisi_zip *hisi_zip)
71262c455caSZhou Wang {
71362c455caSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
71462c455caSZhou Wang 	struct hisi_zip_ctrl *ctrl;
71562c455caSZhou Wang 
71662c455caSZhou Wang 	ctrl = devm_kzalloc(&qm->pdev->dev, sizeof(*ctrl), GFP_KERNEL);
71762c455caSZhou Wang 	if (!ctrl)
71862c455caSZhou Wang 		return -ENOMEM;
71962c455caSZhou Wang 
72062c455caSZhou Wang 	hisi_zip->ctrl = ctrl;
72162c455caSZhou Wang 	ctrl->hisi_zip = hisi_zip;
72262c455caSZhou Wang 
72358ca0060SWeili Qian 	if (qm->ver == QM_HW_V1)
72462c455caSZhou Wang 		qm->ctrl_qp_num = HZIP_QUEUE_NUM_V1;
72558ca0060SWeili Qian 	else
72662c455caSZhou Wang 		qm->ctrl_qp_num = HZIP_QUEUE_NUM_V2;
72762c455caSZhou Wang 
728eaebf4c3SShukun Tan 	qm->err_ini = &hisi_zip_err_ini;
729eaebf4c3SShukun Tan 
73084c9b780SShukun Tan 	hisi_zip_set_user_domain_and_cache(qm);
731eaebf4c3SShukun Tan 	hisi_qm_dev_err_init(qm);
73272c7a68dSZhou Wang 	hisi_zip_debug_regs_clear(hisi_zip);
73362c455caSZhou Wang 
73462c455caSZhou Wang 	return 0;
73562c455caSZhou Wang }
73662c455caSZhou Wang 
737cfd66a66SLongfang Liu static int hisi_zip_qm_init(struct hisi_qm *qm, struct pci_dev *pdev)
73839977f4bSHao Fang {
73939977f4bSHao Fang 	qm->pdev = pdev;
74058ca0060SWeili Qian 	qm->ver = pdev->revision;
7419e00df71SZhangfei Gao 	qm->algs = "zlib\ngzip";
74239977f4bSHao Fang 	qm->sqe_size = HZIP_SQE_SIZE;
74339977f4bSHao Fang 	qm->dev_name = hisi_zip_name;
744d9701f8dSWeili Qian 
745cfd66a66SLongfang Liu 	qm->fun_type = (pdev->device == PCI_DEVICE_ID_ZIP_PF) ?
746cfd66a66SLongfang Liu 			QM_HW_PF : QM_HW_VF;
747d9701f8dSWeili Qian 	if (qm->fun_type == QM_HW_PF) {
748d9701f8dSWeili Qian 		qm->qp_base = HZIP_PF_DEF_Q_BASE;
749d9701f8dSWeili Qian 		qm->qp_num = pf_q_num;
750d9701f8dSWeili Qian 		qm->qm_list = &zip_devices;
751d9701f8dSWeili Qian 	} else if (qm->fun_type == QM_HW_VF && qm->ver == QM_HW_V1) {
752d9701f8dSWeili Qian 		/*
753d9701f8dSWeili Qian 		 * have no way to get qm configure in VM in v1 hardware,
754d9701f8dSWeili Qian 		 * so currently force PF to uses HZIP_PF_DEF_Q_NUM, and force
755d9701f8dSWeili Qian 		 * to trigger only one VF in v1 hardware.
756d9701f8dSWeili Qian 		 *
757d9701f8dSWeili Qian 		 * v2 hardware has no such problem.
758d9701f8dSWeili Qian 		 */
759d9701f8dSWeili Qian 		qm->qp_base = HZIP_PF_DEF_Q_NUM;
760d9701f8dSWeili Qian 		qm->qp_num = HZIP_QUEUE_NUM_V1 - HZIP_PF_DEF_Q_NUM;
761d9701f8dSWeili Qian 	}
762cfd66a66SLongfang Liu 
763cfd66a66SLongfang Liu 	return hisi_qm_init(qm);
76439977f4bSHao Fang }
76539977f4bSHao Fang 
766cfd66a66SLongfang Liu static int hisi_zip_probe_init(struct hisi_zip *hisi_zip)
767cfd66a66SLongfang Liu {
768cfd66a66SLongfang Liu 	struct hisi_qm *qm = &hisi_zip->qm;
769cfd66a66SLongfang Liu 	int ret;
770cfd66a66SLongfang Liu 
77139977f4bSHao Fang 	if (qm->fun_type == QM_HW_PF) {
77239977f4bSHao Fang 		ret = hisi_zip_pf_probe_init(hisi_zip);
77339977f4bSHao Fang 		if (ret)
77439977f4bSHao Fang 			return ret;
775cfd66a66SLongfang Liu 	}
776cfd66a66SLongfang Liu 
777cfd66a66SLongfang Liu 	return 0;
778cfd66a66SLongfang Liu }
779cfd66a66SLongfang Liu 
780cfd66a66SLongfang Liu static int hisi_zip_probe(struct pci_dev *pdev, const struct pci_device_id *id)
781cfd66a66SLongfang Liu {
782cfd66a66SLongfang Liu 	struct hisi_zip *hisi_zip;
783cfd66a66SLongfang Liu 	struct hisi_qm *qm;
784cfd66a66SLongfang Liu 	int ret;
785cfd66a66SLongfang Liu 
786cfd66a66SLongfang Liu 	hisi_zip = devm_kzalloc(&pdev->dev, sizeof(*hisi_zip), GFP_KERNEL);
787cfd66a66SLongfang Liu 	if (!hisi_zip)
788cfd66a66SLongfang Liu 		return -ENOMEM;
789cfd66a66SLongfang Liu 
790cfd66a66SLongfang Liu 	qm = &hisi_zip->qm;
791cfd66a66SLongfang Liu 
792cfd66a66SLongfang Liu 	ret = hisi_zip_qm_init(qm, pdev);
793cfd66a66SLongfang Liu 	if (ret) {
794cfd66a66SLongfang Liu 		pci_err(pdev, "Failed to init ZIP QM (%d)!\n", ret);
795cfd66a66SLongfang Liu 		return ret;
796cfd66a66SLongfang Liu 	}
797cfd66a66SLongfang Liu 
798cfd66a66SLongfang Liu 	ret = hisi_zip_probe_init(hisi_zip);
799cfd66a66SLongfang Liu 	if (ret) {
800cfd66a66SLongfang Liu 		pci_err(pdev, "Failed to probe (%d)!\n", ret);
801cfd66a66SLongfang Liu 		goto err_qm_uninit;
80239977f4bSHao Fang 	}
80339977f4bSHao Fang 
80439977f4bSHao Fang 	ret = hisi_qm_start(qm);
80539977f4bSHao Fang 	if (ret)
80639977f4bSHao Fang 		goto err_qm_uninit;
80739977f4bSHao Fang 
80839977f4bSHao Fang 	ret = hisi_zip_debugfs_init(hisi_zip);
80939977f4bSHao Fang 	if (ret)
81039977f4bSHao Fang 		dev_err(&pdev->dev, "Failed to init debugfs (%d)!\n", ret);
81139977f4bSHao Fang 
81218f1ab3fSShukun Tan 	hisi_qm_add_to_list(qm, &zip_devices);
81339977f4bSHao Fang 
8149e00df71SZhangfei Gao 	if (qm->uacce) {
8159e00df71SZhangfei Gao 		ret = uacce_register(qm->uacce);
8169e00df71SZhangfei Gao 		if (ret)
8179e00df71SZhangfei Gao 			goto err_qm_uninit;
8189e00df71SZhangfei Gao 	}
8199e00df71SZhangfei Gao 
82039977f4bSHao Fang 	if (qm->fun_type == QM_HW_PF && vfs_num > 0) {
821cd1b7ae3SShukun Tan 		ret = hisi_qm_sriov_enable(pdev, vfs_num);
82239977f4bSHao Fang 		if (ret < 0)
82339977f4bSHao Fang 			goto err_remove_from_list;
82439977f4bSHao Fang 	}
82539977f4bSHao Fang 
82639977f4bSHao Fang 	return 0;
82739977f4bSHao Fang 
82839977f4bSHao Fang err_remove_from_list:
82918f1ab3fSShukun Tan 	hisi_qm_del_from_list(qm, &zip_devices);
83039977f4bSHao Fang 	hisi_zip_debugfs_exit(hisi_zip);
831*e88dd6e1SYang Shen 	hisi_qm_stop(qm, QM_NORMAL);
83239977f4bSHao Fang err_qm_uninit:
83339977f4bSHao Fang 	hisi_qm_uninit(qm);
834cfd66a66SLongfang Liu 
83539977f4bSHao Fang 	return ret;
83639977f4bSHao Fang }
83739977f4bSHao Fang 
83862c455caSZhou Wang static void hisi_zip_remove(struct pci_dev *pdev)
83962c455caSZhou Wang {
84062c455caSZhou Wang 	struct hisi_zip *hisi_zip = pci_get_drvdata(pdev);
84162c455caSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
84262c455caSZhou Wang 
843619e464aSShukun Tan 	if (qm->fun_type == QM_HW_PF && qm->vfs_num)
844cd1b7ae3SShukun Tan 		hisi_qm_sriov_disable(pdev);
84579e09f30SZhou Wang 
84672c7a68dSZhou Wang 	hisi_zip_debugfs_exit(hisi_zip);
847*e88dd6e1SYang Shen 	hisi_qm_stop(qm, QM_NORMAL);
84879e09f30SZhou Wang 
849eaebf4c3SShukun Tan 	hisi_qm_dev_err_uninit(qm);
85062c455caSZhou Wang 	hisi_qm_uninit(qm);
85118f1ab3fSShukun Tan 	hisi_qm_del_from_list(qm, &zip_devices);
85262c455caSZhou Wang }
85362c455caSZhou Wang 
85462c455caSZhou Wang static const struct pci_error_handlers hisi_zip_err_handler = {
855f826e6efSShukun Tan 	.error_detected	= hisi_qm_dev_err_detected,
85684c9b780SShukun Tan 	.slot_reset	= hisi_qm_dev_slot_reset,
8577ce396faSShukun Tan 	.reset_prepare	= hisi_qm_reset_prepare,
8587ce396faSShukun Tan 	.reset_done	= hisi_qm_reset_done,
85962c455caSZhou Wang };
86062c455caSZhou Wang 
86162c455caSZhou Wang static struct pci_driver hisi_zip_pci_driver = {
86262c455caSZhou Wang 	.name			= "hisi_zip",
86362c455caSZhou Wang 	.id_table		= hisi_zip_dev_ids,
86462c455caSZhou Wang 	.probe			= hisi_zip_probe,
86562c455caSZhou Wang 	.remove			= hisi_zip_remove,
866bf6a7a5aSArnd Bergmann 	.sriov_configure	= IS_ENABLED(CONFIG_PCI_IOV) ?
867cd1b7ae3SShukun Tan 					hisi_qm_sriov_configure : NULL,
86862c455caSZhou Wang 	.err_handler		= &hisi_zip_err_handler,
86962c455caSZhou Wang };
87062c455caSZhou Wang 
87172c7a68dSZhou Wang static void hisi_zip_register_debugfs(void)
87272c7a68dSZhou Wang {
87372c7a68dSZhou Wang 	if (!debugfs_initialized())
87472c7a68dSZhou Wang 		return;
87572c7a68dSZhou Wang 
87672c7a68dSZhou Wang 	hzip_debugfs_root = debugfs_create_dir("hisi_zip", NULL);
87772c7a68dSZhou Wang }
87872c7a68dSZhou Wang 
87972c7a68dSZhou Wang static void hisi_zip_unregister_debugfs(void)
88072c7a68dSZhou Wang {
88172c7a68dSZhou Wang 	debugfs_remove_recursive(hzip_debugfs_root);
88272c7a68dSZhou Wang }
88372c7a68dSZhou Wang 
88462c455caSZhou Wang static int __init hisi_zip_init(void)
88562c455caSZhou Wang {
88662c455caSZhou Wang 	int ret;
88762c455caSZhou Wang 
88818f1ab3fSShukun Tan 	hisi_qm_init_list(&zip_devices);
88972c7a68dSZhou Wang 	hisi_zip_register_debugfs();
89072c7a68dSZhou Wang 
89162c455caSZhou Wang 	ret = pci_register_driver(&hisi_zip_pci_driver);
89262c455caSZhou Wang 	if (ret < 0) {
89362c455caSZhou Wang 		pr_err("Failed to register pci driver.\n");
89472c7a68dSZhou Wang 		goto err_pci;
89562c455caSZhou Wang 	}
89662c455caSZhou Wang 
89762c455caSZhou Wang 	ret = hisi_zip_register_to_crypto();
89862c455caSZhou Wang 	if (ret < 0) {
89962c455caSZhou Wang 		pr_err("Failed to register driver to crypto.\n");
90062c455caSZhou Wang 		goto err_crypto;
90162c455caSZhou Wang 	}
90262c455caSZhou Wang 
90362c455caSZhou Wang 	return 0;
90462c455caSZhou Wang 
90562c455caSZhou Wang err_crypto:
90662c455caSZhou Wang 	pci_unregister_driver(&hisi_zip_pci_driver);
90772c7a68dSZhou Wang err_pci:
90872c7a68dSZhou Wang 	hisi_zip_unregister_debugfs();
90972c7a68dSZhou Wang 
91062c455caSZhou Wang 	return ret;
91162c455caSZhou Wang }
91262c455caSZhou Wang 
91362c455caSZhou Wang static void __exit hisi_zip_exit(void)
91462c455caSZhou Wang {
91562c455caSZhou Wang 	hisi_zip_unregister_from_crypto();
91662c455caSZhou Wang 	pci_unregister_driver(&hisi_zip_pci_driver);
91772c7a68dSZhou Wang 	hisi_zip_unregister_debugfs();
91862c455caSZhou Wang }
91962c455caSZhou Wang 
92062c455caSZhou Wang module_init(hisi_zip_init);
92162c455caSZhou Wang module_exit(hisi_zip_exit);
92262c455caSZhou Wang 
92362c455caSZhou Wang MODULE_LICENSE("GPL v2");
92462c455caSZhou Wang MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>");
92562c455caSZhou Wang MODULE_DESCRIPTION("Driver for HiSilicon ZIP accelerator");
926