xref: /openbmc/linux/drivers/crypto/hisilicon/zip/zip_main.c (revision 700f7d0d29c795c36517dcd3541e4432a76c2efc)
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>
1462c455caSZhou Wang #include "zip.h"
1562c455caSZhou Wang 
1662c455caSZhou Wang #define PCI_DEVICE_ID_ZIP_PF		0xa250
1779e09f30SZhou Wang #define PCI_DEVICE_ID_ZIP_VF		0xa251
1862c455caSZhou Wang 
1962c455caSZhou Wang #define HZIP_VF_NUM			63
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)
3262c455caSZhou Wang #define 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)
3662c455caSZhou Wang #define 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
4362c455caSZhou Wang #define 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
6362c455caSZhou Wang #define HZIP_CORE_INT_MASK		0x3010A4
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
6762c455caSZhou Wang #define SRAM_ECC_ERR_NUM_SHIFT		16
6862c455caSZhou Wang #define SRAM_ECC_ERR_ADDR_SHIFT		24
6962c455caSZhou Wang #define HZIP_CORE_INT_DISABLE		0x000007FF
7072c7a68dSZhou Wang #define HZIP_COMP_CORE_NUM		2
7172c7a68dSZhou Wang #define HZIP_DECOMP_CORE_NUM		6
7272c7a68dSZhou Wang #define HZIP_CORE_NUM			(HZIP_COMP_CORE_NUM + \
7372c7a68dSZhou Wang 					 HZIP_DECOMP_CORE_NUM)
7462c455caSZhou Wang #define HZIP_SQE_SIZE			128
7572c7a68dSZhou Wang #define HZIP_SQ_SIZE			(HZIP_SQE_SIZE * QM_Q_DEPTH)
7662c455caSZhou Wang #define HZIP_PF_DEF_Q_NUM		64
7762c455caSZhou Wang #define HZIP_PF_DEF_Q_BASE		0
7862c455caSZhou Wang 
7972c7a68dSZhou Wang #define HZIP_SOFT_CTRL_CNT_CLR_CE	0x301000
8072c7a68dSZhou Wang #define SOFT_CTRL_CNT_CLR_CE_BIT	BIT(0)
8162c455caSZhou Wang 
8272c7a68dSZhou Wang #define HZIP_BUF_SIZE			22
8362c455caSZhou Wang 
8462c455caSZhou Wang static const char hisi_zip_name[] = "hisi_zip";
8572c7a68dSZhou Wang static struct dentry *hzip_debugfs_root;
86719181f3SShukun Tan static LIST_HEAD(hisi_zip_list);
87719181f3SShukun Tan static DEFINE_MUTEX(hisi_zip_list_lock);
8862c455caSZhou Wang 
89*700f7d0dSZhou Wang struct hisi_zip_resource {
90*700f7d0dSZhou Wang 	struct hisi_zip *hzip;
91*700f7d0dSZhou Wang 	int distance;
92*700f7d0dSZhou Wang 	struct list_head list;
93*700f7d0dSZhou Wang };
94*700f7d0dSZhou Wang 
95*700f7d0dSZhou Wang static void free_list(struct list_head *head)
9662c455caSZhou Wang {
97*700f7d0dSZhou Wang 	struct hisi_zip_resource *res, *tmp;
9862c455caSZhou Wang 
99*700f7d0dSZhou Wang 	list_for_each_entry_safe(res, tmp, head, list) {
100*700f7d0dSZhou Wang 		list_del(&res->list);
101*700f7d0dSZhou Wang 		kfree(res);
10262c455caSZhou Wang 	}
10362c455caSZhou Wang }
10462c455caSZhou Wang 
10562c455caSZhou Wang struct hisi_zip *find_zip_device(int node)
10662c455caSZhou Wang {
107*700f7d0dSZhou Wang 	struct hisi_zip *ret = NULL;
108*700f7d0dSZhou Wang #ifdef CONFIG_NUMA
109*700f7d0dSZhou Wang 	struct hisi_zip_resource *res, *tmp;
110*700f7d0dSZhou Wang 	struct hisi_zip *hisi_zip;
111*700f7d0dSZhou Wang 	struct list_head *n;
112*700f7d0dSZhou Wang 	struct device *dev;
113*700f7d0dSZhou Wang 	LIST_HEAD(head);
11462c455caSZhou Wang 
11562c455caSZhou Wang 	mutex_lock(&hisi_zip_list_lock);
116*700f7d0dSZhou Wang 
117*700f7d0dSZhou Wang 	list_for_each_entry(hisi_zip, &hisi_zip_list, list) {
118*700f7d0dSZhou Wang 		res = kzalloc(sizeof(*res), GFP_KERNEL);
119*700f7d0dSZhou Wang 		if (!res)
120*700f7d0dSZhou Wang 			goto err;
121*700f7d0dSZhou Wang 
122*700f7d0dSZhou Wang 		dev = &hisi_zip->qm.pdev->dev;
123*700f7d0dSZhou Wang 		res->hzip = hisi_zip;
124*700f7d0dSZhou Wang 		res->distance = node_distance(dev->numa_node, node);
125*700f7d0dSZhou Wang 
126*700f7d0dSZhou Wang 		n = &head;
127*700f7d0dSZhou Wang 		list_for_each_entry(tmp, &head, list) {
128*700f7d0dSZhou Wang 			if (res->distance < tmp->distance) {
129*700f7d0dSZhou Wang 				n = &tmp->list;
130*700f7d0dSZhou Wang 				break;
131*700f7d0dSZhou Wang 			}
132*700f7d0dSZhou Wang 		}
133*700f7d0dSZhou Wang 		list_add_tail(&res->list, n);
134*700f7d0dSZhou Wang 	}
135*700f7d0dSZhou Wang 
136*700f7d0dSZhou Wang 	list_for_each_entry(tmp, &head, list) {
137*700f7d0dSZhou Wang 		if (hisi_qm_get_free_qp_num(&tmp->hzip->qm)) {
138*700f7d0dSZhou Wang 			ret = tmp->hzip;
139*700f7d0dSZhou Wang 			break;
140*700f7d0dSZhou Wang 		}
141*700f7d0dSZhou Wang 	}
142*700f7d0dSZhou Wang 
143*700f7d0dSZhou Wang 	free_list(&head);
14462c455caSZhou Wang #else
145*700f7d0dSZhou Wang 	mutex_lock(&hisi_zip_list_lock);
146*700f7d0dSZhou Wang 
147*700f7d0dSZhou Wang 	ret = list_first_entry(&hisi_zip_list, struct hisi_zip, list);
14862c455caSZhou Wang #endif
14962c455caSZhou Wang 	mutex_unlock(&hisi_zip_list_lock);
15062c455caSZhou Wang 
151*700f7d0dSZhou Wang 	return ret;
152*700f7d0dSZhou Wang 
153*700f7d0dSZhou Wang err:
154*700f7d0dSZhou Wang 	free_list(&head);
155*700f7d0dSZhou Wang 	mutex_unlock(&hisi_zip_list_lock);
156*700f7d0dSZhou Wang 	return NULL;
15762c455caSZhou Wang }
15862c455caSZhou Wang 
15962c455caSZhou Wang struct hisi_zip_hw_error {
16062c455caSZhou Wang 	u32 int_msk;
16162c455caSZhou Wang 	const char *msg;
16262c455caSZhou Wang };
16362c455caSZhou Wang 
16462c455caSZhou Wang static const struct hisi_zip_hw_error zip_hw_error[] = {
16562c455caSZhou Wang 	{ .int_msk = BIT(0), .msg = "zip_ecc_1bitt_err" },
16662c455caSZhou Wang 	{ .int_msk = BIT(1), .msg = "zip_ecc_2bit_err" },
16762c455caSZhou Wang 	{ .int_msk = BIT(2), .msg = "zip_axi_rresp_err" },
16862c455caSZhou Wang 	{ .int_msk = BIT(3), .msg = "zip_axi_bresp_err" },
16962c455caSZhou Wang 	{ .int_msk = BIT(4), .msg = "zip_src_addr_parse_err" },
17062c455caSZhou Wang 	{ .int_msk = BIT(5), .msg = "zip_dst_addr_parse_err" },
17162c455caSZhou Wang 	{ .int_msk = BIT(6), .msg = "zip_pre_in_addr_err" },
17262c455caSZhou Wang 	{ .int_msk = BIT(7), .msg = "zip_pre_in_data_err" },
17362c455caSZhou Wang 	{ .int_msk = BIT(8), .msg = "zip_com_inf_err" },
17462c455caSZhou Wang 	{ .int_msk = BIT(9), .msg = "zip_enc_inf_err" },
17562c455caSZhou Wang 	{ .int_msk = BIT(10), .msg = "zip_pre_out_err" },
17662c455caSZhou Wang 	{ /* sentinel */ }
17762c455caSZhou Wang };
17862c455caSZhou Wang 
17972c7a68dSZhou Wang enum ctrl_debug_file_index {
18072c7a68dSZhou Wang 	HZIP_CURRENT_QM,
18172c7a68dSZhou Wang 	HZIP_CLEAR_ENABLE,
18272c7a68dSZhou Wang 	HZIP_DEBUG_FILE_NUM,
18372c7a68dSZhou Wang };
18472c7a68dSZhou Wang 
18572c7a68dSZhou Wang static const char * const ctrl_debug_file_name[] = {
18672c7a68dSZhou Wang 	[HZIP_CURRENT_QM]   = "current_qm",
18772c7a68dSZhou Wang 	[HZIP_CLEAR_ENABLE] = "clear_enable",
18872c7a68dSZhou Wang };
18972c7a68dSZhou Wang 
19072c7a68dSZhou Wang struct ctrl_debug_file {
19172c7a68dSZhou Wang 	enum ctrl_debug_file_index index;
19272c7a68dSZhou Wang 	spinlock_t lock;
19372c7a68dSZhou Wang 	struct hisi_zip_ctrl *ctrl;
19472c7a68dSZhou Wang };
19572c7a68dSZhou Wang 
19662c455caSZhou Wang /*
19762c455caSZhou Wang  * One ZIP controller has one PF and multiple VFs, some global configurations
19862c455caSZhou Wang  * which PF has need this structure.
19962c455caSZhou Wang  *
20062c455caSZhou Wang  * Just relevant for PF.
20162c455caSZhou Wang  */
20262c455caSZhou Wang struct hisi_zip_ctrl {
20379e09f30SZhou Wang 	u32 num_vfs;
20462c455caSZhou Wang 	struct hisi_zip *hisi_zip;
20572c7a68dSZhou Wang 	struct dentry *debug_root;
20672c7a68dSZhou Wang 	struct ctrl_debug_file files[HZIP_DEBUG_FILE_NUM];
20772c7a68dSZhou Wang };
20872c7a68dSZhou Wang 
20972c7a68dSZhou Wang enum {
21072c7a68dSZhou Wang 	HZIP_COMP_CORE0,
21172c7a68dSZhou Wang 	HZIP_COMP_CORE1,
21272c7a68dSZhou Wang 	HZIP_DECOMP_CORE0,
21372c7a68dSZhou Wang 	HZIP_DECOMP_CORE1,
21472c7a68dSZhou Wang 	HZIP_DECOMP_CORE2,
21572c7a68dSZhou Wang 	HZIP_DECOMP_CORE3,
21672c7a68dSZhou Wang 	HZIP_DECOMP_CORE4,
21772c7a68dSZhou Wang 	HZIP_DECOMP_CORE5,
21872c7a68dSZhou Wang };
21972c7a68dSZhou Wang 
22072c7a68dSZhou Wang static const u64 core_offsets[] = {
22172c7a68dSZhou Wang 	[HZIP_COMP_CORE0]   = 0x302000,
22272c7a68dSZhou Wang 	[HZIP_COMP_CORE1]   = 0x303000,
22372c7a68dSZhou Wang 	[HZIP_DECOMP_CORE0] = 0x304000,
22472c7a68dSZhou Wang 	[HZIP_DECOMP_CORE1] = 0x305000,
22572c7a68dSZhou Wang 	[HZIP_DECOMP_CORE2] = 0x306000,
22672c7a68dSZhou Wang 	[HZIP_DECOMP_CORE3] = 0x307000,
22772c7a68dSZhou Wang 	[HZIP_DECOMP_CORE4] = 0x308000,
22872c7a68dSZhou Wang 	[HZIP_DECOMP_CORE5] = 0x309000,
22972c7a68dSZhou Wang };
23072c7a68dSZhou Wang 
23172c7a68dSZhou Wang static struct debugfs_reg32 hzip_dfx_regs[] = {
23272c7a68dSZhou Wang 	{"HZIP_GET_BD_NUM                ",  0x00ull},
23372c7a68dSZhou Wang 	{"HZIP_GET_RIGHT_BD              ",  0x04ull},
23472c7a68dSZhou Wang 	{"HZIP_GET_ERROR_BD              ",  0x08ull},
23572c7a68dSZhou Wang 	{"HZIP_DONE_BD_NUM               ",  0x0cull},
23672c7a68dSZhou Wang 	{"HZIP_WORK_CYCLE                ",  0x10ull},
23772c7a68dSZhou Wang 	{"HZIP_IDLE_CYCLE                ",  0x18ull},
23872c7a68dSZhou Wang 	{"HZIP_MAX_DELAY                 ",  0x20ull},
23972c7a68dSZhou Wang 	{"HZIP_MIN_DELAY                 ",  0x24ull},
24072c7a68dSZhou Wang 	{"HZIP_AVG_DELAY                 ",  0x28ull},
24172c7a68dSZhou Wang 	{"HZIP_MEM_VISIBLE_DATA          ",  0x30ull},
24272c7a68dSZhou Wang 	{"HZIP_MEM_VISIBLE_ADDR          ",  0x34ull},
24372c7a68dSZhou Wang 	{"HZIP_COMSUMED_BYTE             ",  0x38ull},
24472c7a68dSZhou Wang 	{"HZIP_PRODUCED_BYTE             ",  0x40ull},
24572c7a68dSZhou Wang 	{"HZIP_COMP_INF                  ",  0x70ull},
24672c7a68dSZhou Wang 	{"HZIP_PRE_OUT                   ",  0x78ull},
24772c7a68dSZhou Wang 	{"HZIP_BD_RD                     ",  0x7cull},
24872c7a68dSZhou Wang 	{"HZIP_BD_WR                     ",  0x80ull},
24972c7a68dSZhou Wang 	{"HZIP_GET_BD_AXI_ERR_NUM        ",  0x84ull},
25072c7a68dSZhou Wang 	{"HZIP_GET_BD_PARSE_ERR_NUM      ",  0x88ull},
25172c7a68dSZhou Wang 	{"HZIP_ADD_BD_AXI_ERR_NUM        ",  0x8cull},
25272c7a68dSZhou Wang 	{"HZIP_DECOMP_STF_RELOAD_CURR_ST ",  0x94ull},
25372c7a68dSZhou Wang 	{"HZIP_DECOMP_LZ77_CURR_ST       ",  0x9cull},
25462c455caSZhou Wang };
25562c455caSZhou Wang 
25662c455caSZhou Wang static int pf_q_num_set(const char *val, const struct kernel_param *kp)
25762c455caSZhou Wang {
25862c455caSZhou Wang 	struct pci_dev *pdev = pci_get_device(PCI_VENDOR_ID_HUAWEI,
25962c455caSZhou Wang 					      PCI_DEVICE_ID_ZIP_PF, NULL);
26062c455caSZhou Wang 	u32 n, q_num;
26162c455caSZhou Wang 	u8 rev_id;
26262c455caSZhou Wang 	int ret;
26362c455caSZhou Wang 
26462c455caSZhou Wang 	if (!val)
26562c455caSZhou Wang 		return -EINVAL;
26662c455caSZhou Wang 
26762c455caSZhou Wang 	if (!pdev) {
26862c455caSZhou Wang 		q_num = min_t(u32, HZIP_QUEUE_NUM_V1, HZIP_QUEUE_NUM_V2);
26962c455caSZhou Wang 		pr_info("No device found currently, suppose queue number is %d\n",
27062c455caSZhou Wang 			q_num);
27162c455caSZhou Wang 	} else {
27262c455caSZhou Wang 		rev_id = pdev->revision;
27362c455caSZhou Wang 		switch (rev_id) {
27462c455caSZhou Wang 		case QM_HW_V1:
27562c455caSZhou Wang 			q_num = HZIP_QUEUE_NUM_V1;
27662c455caSZhou Wang 			break;
27762c455caSZhou Wang 		case QM_HW_V2:
27862c455caSZhou Wang 			q_num = HZIP_QUEUE_NUM_V2;
27962c455caSZhou Wang 			break;
28062c455caSZhou Wang 		default:
28162c455caSZhou Wang 			return -EINVAL;
28262c455caSZhou Wang 		}
28362c455caSZhou Wang 	}
28462c455caSZhou Wang 
28562c455caSZhou Wang 	ret = kstrtou32(val, 10, &n);
28662c455caSZhou Wang 	if (ret != 0 || n > q_num || n == 0)
28762c455caSZhou Wang 		return -EINVAL;
28862c455caSZhou Wang 
28962c455caSZhou Wang 	return param_set_int(val, kp);
29062c455caSZhou Wang }
29162c455caSZhou Wang 
29262c455caSZhou Wang static const struct kernel_param_ops pf_q_num_ops = {
29362c455caSZhou Wang 	.set = pf_q_num_set,
29462c455caSZhou Wang 	.get = param_get_int,
29562c455caSZhou Wang };
29662c455caSZhou Wang 
29762c455caSZhou Wang static u32 pf_q_num = HZIP_PF_DEF_Q_NUM;
29862c455caSZhou Wang module_param_cb(pf_q_num, &pf_q_num_ops, &pf_q_num, 0444);
29962c455caSZhou Wang MODULE_PARM_DESC(pf_q_num, "Number of queues in PF(v1 1-4096, v2 1-1024)");
30062c455caSZhou Wang 
30162c455caSZhou Wang static int uacce_mode;
30262c455caSZhou Wang module_param(uacce_mode, int, 0);
30362c455caSZhou Wang 
30462c455caSZhou Wang static const struct pci_device_id hisi_zip_dev_ids[] = {
30562c455caSZhou Wang 	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_ZIP_PF) },
30679e09f30SZhou Wang 	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_ZIP_VF) },
30762c455caSZhou Wang 	{ 0, }
30862c455caSZhou Wang };
30962c455caSZhou Wang MODULE_DEVICE_TABLE(pci, hisi_zip_dev_ids);
31062c455caSZhou Wang 
31162c455caSZhou Wang static inline void hisi_zip_add_to_list(struct hisi_zip *hisi_zip)
31262c455caSZhou Wang {
31362c455caSZhou Wang 	mutex_lock(&hisi_zip_list_lock);
31462c455caSZhou Wang 	list_add_tail(&hisi_zip->list, &hisi_zip_list);
31562c455caSZhou Wang 	mutex_unlock(&hisi_zip_list_lock);
31662c455caSZhou Wang }
31762c455caSZhou Wang 
31862c455caSZhou Wang static inline void hisi_zip_remove_from_list(struct hisi_zip *hisi_zip)
31962c455caSZhou Wang {
32062c455caSZhou Wang 	mutex_lock(&hisi_zip_list_lock);
32162c455caSZhou Wang 	list_del(&hisi_zip->list);
32262c455caSZhou Wang 	mutex_unlock(&hisi_zip_list_lock);
32362c455caSZhou Wang }
32462c455caSZhou Wang 
32562c455caSZhou Wang static void hisi_zip_set_user_domain_and_cache(struct hisi_zip *hisi_zip)
32662c455caSZhou Wang {
32762c455caSZhou Wang 	void __iomem *base = hisi_zip->qm.io_base;
32862c455caSZhou Wang 
32962c455caSZhou Wang 	/* qm user domain */
33062c455caSZhou Wang 	writel(AXUSER_BASE, base + QM_ARUSER_M_CFG_1);
33162c455caSZhou Wang 	writel(ARUSER_M_CFG_ENABLE, base + QM_ARUSER_M_CFG_ENABLE);
33262c455caSZhou Wang 	writel(AXUSER_BASE, base + QM_AWUSER_M_CFG_1);
33362c455caSZhou Wang 	writel(AWUSER_M_CFG_ENABLE, base + QM_AWUSER_M_CFG_ENABLE);
33462c455caSZhou Wang 	writel(WUSER_M_CFG_ENABLE, base + QM_WUSER_M_CFG_ENABLE);
33562c455caSZhou Wang 
33662c455caSZhou Wang 	/* qm cache */
33762c455caSZhou Wang 	writel(AXI_M_CFG, base + QM_AXI_M_CFG);
33862c455caSZhou Wang 	writel(AXI_M_CFG_ENABLE, base + QM_AXI_M_CFG_ENABLE);
33962c455caSZhou Wang 	/* disable FLR triggered by BME(bus master enable) */
34062c455caSZhou Wang 	writel(PEH_AXUSER_CFG, base + QM_PEH_AXUSER_CFG);
34162c455caSZhou Wang 	writel(PEH_AXUSER_CFG_ENABLE, base + QM_PEH_AXUSER_CFG_ENABLE);
34262c455caSZhou Wang 
34362c455caSZhou Wang 	/* cache */
34462c455caSZhou Wang 	writel(CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_0);
34562c455caSZhou Wang 	writel(CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_1);
34662c455caSZhou Wang 	writel(CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_0);
34762c455caSZhou Wang 	writel(CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_1);
34862c455caSZhou Wang 
34962c455caSZhou Wang 	/* user domain configurations */
35062c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_BD_RUSER_32_63);
35162c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_SGL_RUSER_32_63);
35262c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_BD_WUSER_32_63);
35362c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_DATA_RUSER_32_63);
35462c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_DATA_WUSER_32_63);
35562c455caSZhou Wang 
35662c455caSZhou Wang 	/* let's open all compression/decompression cores */
35762c455caSZhou Wang 	writel(DECOMP_CHECK_ENABLE | ALL_COMP_DECOMP_EN,
35862c455caSZhou Wang 	       base + HZIP_CLOCK_GATE_CTRL);
35962c455caSZhou Wang 
36062c455caSZhou Wang 	/* enable sqc writeback */
36162c455caSZhou Wang 	writel(SQC_CACHE_ENABLE | CQC_CACHE_ENABLE | SQC_CACHE_WB_ENABLE |
36262c455caSZhou Wang 	       CQC_CACHE_WB_ENABLE | FIELD_PREP(SQC_CACHE_WB_THRD, 1) |
36362c455caSZhou Wang 	       FIELD_PREP(CQC_CACHE_WB_THRD, 1), base + QM_CACHE_CTL);
36462c455caSZhou Wang }
36562c455caSZhou Wang 
36662c455caSZhou Wang static void hisi_zip_hw_error_set_state(struct hisi_zip *hisi_zip, bool state)
36762c455caSZhou Wang {
36862c455caSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
36962c455caSZhou Wang 
37062c455caSZhou Wang 	if (qm->ver == QM_HW_V1) {
37162c455caSZhou Wang 		writel(HZIP_CORE_INT_DISABLE, qm->io_base + HZIP_CORE_INT_MASK);
372ee1788c6SZhou Wang 		dev_info(&qm->pdev->dev, "Does not support hw error handle\n");
37362c455caSZhou Wang 		return;
37462c455caSZhou Wang 	}
37562c455caSZhou Wang 
37662c455caSZhou Wang 	if (state) {
37762c455caSZhou Wang 		/* clear ZIP hw error source if having */
37862c455caSZhou Wang 		writel(HZIP_CORE_INT_DISABLE, hisi_zip->qm.io_base +
37962c455caSZhou Wang 					      HZIP_CORE_INT_SOURCE);
38062c455caSZhou Wang 		/* enable ZIP hw error interrupts */
38162c455caSZhou Wang 		writel(0, hisi_zip->qm.io_base + HZIP_CORE_INT_MASK);
38262c455caSZhou Wang 	} else {
38362c455caSZhou Wang 		/* disable ZIP hw error interrupts */
38462c455caSZhou Wang 		writel(HZIP_CORE_INT_DISABLE,
38562c455caSZhou Wang 		       hisi_zip->qm.io_base + HZIP_CORE_INT_MASK);
38662c455caSZhou Wang 	}
38762c455caSZhou Wang }
38862c455caSZhou Wang 
38972c7a68dSZhou Wang static inline struct hisi_qm *file_to_qm(struct ctrl_debug_file *file)
39072c7a68dSZhou Wang {
39172c7a68dSZhou Wang 	struct hisi_zip *hisi_zip = file->ctrl->hisi_zip;
39272c7a68dSZhou Wang 
39372c7a68dSZhou Wang 	return &hisi_zip->qm;
39472c7a68dSZhou Wang }
39572c7a68dSZhou Wang 
39672c7a68dSZhou Wang static u32 current_qm_read(struct ctrl_debug_file *file)
39772c7a68dSZhou Wang {
39872c7a68dSZhou Wang 	struct hisi_qm *qm = file_to_qm(file);
39972c7a68dSZhou Wang 
40072c7a68dSZhou Wang 	return readl(qm->io_base + QM_DFX_MB_CNT_VF);
40172c7a68dSZhou Wang }
40272c7a68dSZhou Wang 
40372c7a68dSZhou Wang static int current_qm_write(struct ctrl_debug_file *file, u32 val)
40472c7a68dSZhou Wang {
40572c7a68dSZhou Wang 	struct hisi_qm *qm = file_to_qm(file);
40672c7a68dSZhou Wang 	struct hisi_zip_ctrl *ctrl = file->ctrl;
40772c7a68dSZhou Wang 	u32 vfq_num;
40872c7a68dSZhou Wang 	u32 tmp;
40972c7a68dSZhou Wang 
41072c7a68dSZhou Wang 	if (val > ctrl->num_vfs)
41172c7a68dSZhou Wang 		return -EINVAL;
41272c7a68dSZhou Wang 
41372c7a68dSZhou Wang 	/* Calculate curr_qm_qp_num and store */
41472c7a68dSZhou Wang 	if (val == 0) {
41572c7a68dSZhou Wang 		qm->debug.curr_qm_qp_num = qm->qp_num;
41672c7a68dSZhou Wang 	} else {
41772c7a68dSZhou Wang 		vfq_num = (qm->ctrl_qp_num - qm->qp_num) / ctrl->num_vfs;
41872c7a68dSZhou Wang 		if (val == ctrl->num_vfs)
41972c7a68dSZhou Wang 			qm->debug.curr_qm_qp_num = qm->ctrl_qp_num -
42072c7a68dSZhou Wang 				qm->qp_num - (ctrl->num_vfs - 1) * vfq_num;
42172c7a68dSZhou Wang 		else
42272c7a68dSZhou Wang 			qm->debug.curr_qm_qp_num = vfq_num;
42372c7a68dSZhou Wang 	}
42472c7a68dSZhou Wang 
42572c7a68dSZhou Wang 	writel(val, qm->io_base + QM_DFX_MB_CNT_VF);
42672c7a68dSZhou Wang 	writel(val, qm->io_base + QM_DFX_DB_CNT_VF);
42772c7a68dSZhou Wang 
42872c7a68dSZhou Wang 	tmp = val |
42972c7a68dSZhou Wang 	      (readl(qm->io_base + QM_DFX_SQE_CNT_VF_SQN) & CURRENT_Q_MASK);
43072c7a68dSZhou Wang 	writel(tmp, qm->io_base + QM_DFX_SQE_CNT_VF_SQN);
43172c7a68dSZhou Wang 
43272c7a68dSZhou Wang 	tmp = val |
43372c7a68dSZhou Wang 	      (readl(qm->io_base + QM_DFX_CQE_CNT_VF_CQN) & CURRENT_Q_MASK);
43472c7a68dSZhou Wang 	writel(tmp, qm->io_base + QM_DFX_CQE_CNT_VF_CQN);
43572c7a68dSZhou Wang 
43672c7a68dSZhou Wang 	return  0;
43772c7a68dSZhou Wang }
43872c7a68dSZhou Wang 
43972c7a68dSZhou Wang static u32 clear_enable_read(struct ctrl_debug_file *file)
44072c7a68dSZhou Wang {
44172c7a68dSZhou Wang 	struct hisi_qm *qm = file_to_qm(file);
44272c7a68dSZhou Wang 
44372c7a68dSZhou Wang 	return readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) &
44472c7a68dSZhou Wang 	       SOFT_CTRL_CNT_CLR_CE_BIT;
44572c7a68dSZhou Wang }
44672c7a68dSZhou Wang 
44772c7a68dSZhou Wang static int clear_enable_write(struct ctrl_debug_file *file, u32 val)
44872c7a68dSZhou Wang {
44972c7a68dSZhou Wang 	struct hisi_qm *qm = file_to_qm(file);
45072c7a68dSZhou Wang 	u32 tmp;
45172c7a68dSZhou Wang 
45272c7a68dSZhou Wang 	if (val != 1 && val != 0)
45372c7a68dSZhou Wang 		return -EINVAL;
45472c7a68dSZhou Wang 
45572c7a68dSZhou Wang 	tmp = (readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) &
45672c7a68dSZhou Wang 	       ~SOFT_CTRL_CNT_CLR_CE_BIT) | val;
45772c7a68dSZhou Wang 	writel(tmp, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
45872c7a68dSZhou Wang 
45972c7a68dSZhou Wang 	return  0;
46072c7a68dSZhou Wang }
46172c7a68dSZhou Wang 
46272c7a68dSZhou Wang static ssize_t ctrl_debug_read(struct file *filp, char __user *buf,
46372c7a68dSZhou Wang 			       size_t count, loff_t *pos)
46472c7a68dSZhou Wang {
46572c7a68dSZhou Wang 	struct ctrl_debug_file *file = filp->private_data;
46672c7a68dSZhou Wang 	char tbuf[HZIP_BUF_SIZE];
46772c7a68dSZhou Wang 	u32 val;
46872c7a68dSZhou Wang 	int ret;
46972c7a68dSZhou Wang 
47072c7a68dSZhou Wang 	spin_lock_irq(&file->lock);
47172c7a68dSZhou Wang 	switch (file->index) {
47272c7a68dSZhou Wang 	case HZIP_CURRENT_QM:
47372c7a68dSZhou Wang 		val = current_qm_read(file);
47472c7a68dSZhou Wang 		break;
47572c7a68dSZhou Wang 	case HZIP_CLEAR_ENABLE:
47672c7a68dSZhou Wang 		val = clear_enable_read(file);
47772c7a68dSZhou Wang 		break;
47872c7a68dSZhou Wang 	default:
47972c7a68dSZhou Wang 		spin_unlock_irq(&file->lock);
48072c7a68dSZhou Wang 		return -EINVAL;
48172c7a68dSZhou Wang 	}
48272c7a68dSZhou Wang 	spin_unlock_irq(&file->lock);
48372c7a68dSZhou Wang 	ret = sprintf(tbuf, "%u\n", val);
48472c7a68dSZhou Wang 	return simple_read_from_buffer(buf, count, pos, tbuf, ret);
48572c7a68dSZhou Wang }
48672c7a68dSZhou Wang 
48772c7a68dSZhou Wang static ssize_t ctrl_debug_write(struct file *filp, const char __user *buf,
48872c7a68dSZhou Wang 				size_t count, loff_t *pos)
48972c7a68dSZhou Wang {
49072c7a68dSZhou Wang 	struct ctrl_debug_file *file = filp->private_data;
49172c7a68dSZhou Wang 	char tbuf[HZIP_BUF_SIZE];
49272c7a68dSZhou Wang 	unsigned long val;
49372c7a68dSZhou Wang 	int len, ret;
49472c7a68dSZhou Wang 
49572c7a68dSZhou Wang 	if (*pos != 0)
49672c7a68dSZhou Wang 		return 0;
49772c7a68dSZhou Wang 
49872c7a68dSZhou Wang 	if (count >= HZIP_BUF_SIZE)
49972c7a68dSZhou Wang 		return -ENOSPC;
50072c7a68dSZhou Wang 
50172c7a68dSZhou Wang 	len = simple_write_to_buffer(tbuf, HZIP_BUF_SIZE - 1, pos, buf, count);
50272c7a68dSZhou Wang 	if (len < 0)
50372c7a68dSZhou Wang 		return len;
50472c7a68dSZhou Wang 
50572c7a68dSZhou Wang 	tbuf[len] = '\0';
50672c7a68dSZhou Wang 	if (kstrtoul(tbuf, 0, &val))
50772c7a68dSZhou Wang 		return -EFAULT;
50872c7a68dSZhou Wang 
50972c7a68dSZhou Wang 	spin_lock_irq(&file->lock);
51072c7a68dSZhou Wang 	switch (file->index) {
51172c7a68dSZhou Wang 	case HZIP_CURRENT_QM:
51272c7a68dSZhou Wang 		ret = current_qm_write(file, val);
51372c7a68dSZhou Wang 		if (ret)
51472c7a68dSZhou Wang 			goto err_input;
51572c7a68dSZhou Wang 		break;
51672c7a68dSZhou Wang 	case HZIP_CLEAR_ENABLE:
51772c7a68dSZhou Wang 		ret = clear_enable_write(file, val);
51872c7a68dSZhou Wang 		if (ret)
51972c7a68dSZhou Wang 			goto err_input;
52072c7a68dSZhou Wang 		break;
52172c7a68dSZhou Wang 	default:
52272c7a68dSZhou Wang 		ret = -EINVAL;
52372c7a68dSZhou Wang 		goto err_input;
52472c7a68dSZhou Wang 	}
52572c7a68dSZhou Wang 	spin_unlock_irq(&file->lock);
52672c7a68dSZhou Wang 
52772c7a68dSZhou Wang 	return count;
52872c7a68dSZhou Wang 
52972c7a68dSZhou Wang err_input:
53072c7a68dSZhou Wang 	spin_unlock_irq(&file->lock);
53172c7a68dSZhou Wang 	return ret;
53272c7a68dSZhou Wang }
53372c7a68dSZhou Wang 
53472c7a68dSZhou Wang static const struct file_operations ctrl_debug_fops = {
53572c7a68dSZhou Wang 	.owner = THIS_MODULE,
53672c7a68dSZhou Wang 	.open = simple_open,
53772c7a68dSZhou Wang 	.read = ctrl_debug_read,
53872c7a68dSZhou Wang 	.write = ctrl_debug_write,
53972c7a68dSZhou Wang };
54072c7a68dSZhou Wang 
54172c7a68dSZhou Wang static int hisi_zip_core_debug_init(struct hisi_zip_ctrl *ctrl)
54272c7a68dSZhou Wang {
54372c7a68dSZhou Wang 	struct hisi_zip *hisi_zip = ctrl->hisi_zip;
54472c7a68dSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
54572c7a68dSZhou Wang 	struct device *dev = &qm->pdev->dev;
54672c7a68dSZhou Wang 	struct debugfs_regset32 *regset;
54772c7a68dSZhou Wang 	struct dentry *tmp_d, *tmp;
54872c7a68dSZhou Wang 	char buf[HZIP_BUF_SIZE];
54972c7a68dSZhou Wang 	int i;
55072c7a68dSZhou Wang 
55172c7a68dSZhou Wang 	for (i = 0; i < HZIP_CORE_NUM; i++) {
55272c7a68dSZhou Wang 		if (i < HZIP_COMP_CORE_NUM)
55372c7a68dSZhou Wang 			sprintf(buf, "comp_core%d", i);
55472c7a68dSZhou Wang 		else
55572c7a68dSZhou Wang 			sprintf(buf, "decomp_core%d", i - HZIP_COMP_CORE_NUM);
55672c7a68dSZhou Wang 
55772c7a68dSZhou Wang 		tmp_d = debugfs_create_dir(buf, ctrl->debug_root);
55872c7a68dSZhou Wang 		if (!tmp_d)
55972c7a68dSZhou Wang 			return -ENOENT;
56072c7a68dSZhou Wang 
56172c7a68dSZhou Wang 		regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
56272c7a68dSZhou Wang 		if (!regset)
56372c7a68dSZhou Wang 			return -ENOENT;
56472c7a68dSZhou Wang 
56572c7a68dSZhou Wang 		regset->regs = hzip_dfx_regs;
56672c7a68dSZhou Wang 		regset->nregs = ARRAY_SIZE(hzip_dfx_regs);
56772c7a68dSZhou Wang 		regset->base = qm->io_base + core_offsets[i];
56872c7a68dSZhou Wang 
56972c7a68dSZhou Wang 		tmp = debugfs_create_regset32("regs", 0444, tmp_d, regset);
57072c7a68dSZhou Wang 		if (!tmp)
57172c7a68dSZhou Wang 			return -ENOENT;
57272c7a68dSZhou Wang 	}
57372c7a68dSZhou Wang 
57472c7a68dSZhou Wang 	return 0;
57572c7a68dSZhou Wang }
57672c7a68dSZhou Wang 
57772c7a68dSZhou Wang static int hisi_zip_ctrl_debug_init(struct hisi_zip_ctrl *ctrl)
57872c7a68dSZhou Wang {
57972c7a68dSZhou Wang 	struct dentry *tmp;
58072c7a68dSZhou Wang 	int i;
58172c7a68dSZhou Wang 
58272c7a68dSZhou Wang 	for (i = HZIP_CURRENT_QM; i < HZIP_DEBUG_FILE_NUM; i++) {
58372c7a68dSZhou Wang 		spin_lock_init(&ctrl->files[i].lock);
58472c7a68dSZhou Wang 		ctrl->files[i].ctrl = ctrl;
58572c7a68dSZhou Wang 		ctrl->files[i].index = i;
58672c7a68dSZhou Wang 
58772c7a68dSZhou Wang 		tmp = debugfs_create_file(ctrl_debug_file_name[i], 0600,
58872c7a68dSZhou Wang 					  ctrl->debug_root, ctrl->files + i,
58972c7a68dSZhou Wang 					  &ctrl_debug_fops);
59072c7a68dSZhou Wang 		if (!tmp)
59172c7a68dSZhou Wang 			return -ENOENT;
59272c7a68dSZhou Wang 	}
59372c7a68dSZhou Wang 
59472c7a68dSZhou Wang 	return hisi_zip_core_debug_init(ctrl);
59572c7a68dSZhou Wang }
59672c7a68dSZhou Wang 
59772c7a68dSZhou Wang static int hisi_zip_debugfs_init(struct hisi_zip *hisi_zip)
59872c7a68dSZhou Wang {
59972c7a68dSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
60072c7a68dSZhou Wang 	struct device *dev = &qm->pdev->dev;
60172c7a68dSZhou Wang 	struct dentry *dev_d;
60272c7a68dSZhou Wang 	int ret;
60372c7a68dSZhou Wang 
60472c7a68dSZhou Wang 	dev_d = debugfs_create_dir(dev_name(dev), hzip_debugfs_root);
60572c7a68dSZhou Wang 	if (!dev_d)
60672c7a68dSZhou Wang 		return -ENOENT;
60772c7a68dSZhou Wang 
60872c7a68dSZhou Wang 	qm->debug.debug_root = dev_d;
60972c7a68dSZhou Wang 	ret = hisi_qm_debug_init(qm);
61072c7a68dSZhou Wang 	if (ret)
61172c7a68dSZhou Wang 		goto failed_to_create;
61272c7a68dSZhou Wang 
61372c7a68dSZhou Wang 	if (qm->fun_type == QM_HW_PF) {
61472c7a68dSZhou Wang 		hisi_zip->ctrl->debug_root = dev_d;
61572c7a68dSZhou Wang 		ret = hisi_zip_ctrl_debug_init(hisi_zip->ctrl);
61672c7a68dSZhou Wang 		if (ret)
61772c7a68dSZhou Wang 			goto failed_to_create;
61872c7a68dSZhou Wang 	}
61972c7a68dSZhou Wang 
62072c7a68dSZhou Wang 	return 0;
62172c7a68dSZhou Wang 
62272c7a68dSZhou Wang failed_to_create:
62372c7a68dSZhou Wang 	debugfs_remove_recursive(hzip_debugfs_root);
62472c7a68dSZhou Wang 	return ret;
62572c7a68dSZhou Wang }
62672c7a68dSZhou Wang 
62772c7a68dSZhou Wang static void hisi_zip_debug_regs_clear(struct hisi_zip *hisi_zip)
62872c7a68dSZhou Wang {
62972c7a68dSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
63072c7a68dSZhou Wang 
63172c7a68dSZhou Wang 	writel(0x0, qm->io_base + QM_DFX_MB_CNT_VF);
63272c7a68dSZhou Wang 	writel(0x0, qm->io_base + QM_DFX_DB_CNT_VF);
63372c7a68dSZhou Wang 	writel(0x0, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
63472c7a68dSZhou Wang 
63572c7a68dSZhou Wang 	hisi_qm_debug_regs_clear(qm);
63672c7a68dSZhou Wang }
63772c7a68dSZhou Wang 
63872c7a68dSZhou Wang static void hisi_zip_debugfs_exit(struct hisi_zip *hisi_zip)
63972c7a68dSZhou Wang {
64072c7a68dSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
64172c7a68dSZhou Wang 
64272c7a68dSZhou Wang 	debugfs_remove_recursive(qm->debug.debug_root);
64372c7a68dSZhou Wang 
64472c7a68dSZhou Wang 	if (qm->fun_type == QM_HW_PF)
64572c7a68dSZhou Wang 		hisi_zip_debug_regs_clear(hisi_zip);
64672c7a68dSZhou Wang }
64772c7a68dSZhou Wang 
64862c455caSZhou Wang static void hisi_zip_hw_error_init(struct hisi_zip *hisi_zip)
64962c455caSZhou Wang {
65062c455caSZhou Wang 	hisi_qm_hw_error_init(&hisi_zip->qm, QM_BASE_CE,
65162c455caSZhou Wang 			      QM_BASE_NFE | QM_ACC_WB_NOT_READY_TIMEOUT, 0,
65262c455caSZhou Wang 			      QM_DB_RANDOM_INVALID);
65362c455caSZhou Wang 	hisi_zip_hw_error_set_state(hisi_zip, true);
65462c455caSZhou Wang }
65562c455caSZhou Wang 
65662c455caSZhou Wang static int hisi_zip_pf_probe_init(struct hisi_zip *hisi_zip)
65762c455caSZhou Wang {
65862c455caSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
65962c455caSZhou Wang 	struct hisi_zip_ctrl *ctrl;
66062c455caSZhou Wang 
66162c455caSZhou Wang 	ctrl = devm_kzalloc(&qm->pdev->dev, sizeof(*ctrl), GFP_KERNEL);
66262c455caSZhou Wang 	if (!ctrl)
66362c455caSZhou Wang 		return -ENOMEM;
66462c455caSZhou Wang 
66562c455caSZhou Wang 	hisi_zip->ctrl = ctrl;
66662c455caSZhou Wang 	ctrl->hisi_zip = hisi_zip;
66762c455caSZhou Wang 
66862c455caSZhou Wang 	switch (qm->ver) {
66962c455caSZhou Wang 	case QM_HW_V1:
67062c455caSZhou Wang 		qm->ctrl_qp_num = HZIP_QUEUE_NUM_V1;
67162c455caSZhou Wang 		break;
67262c455caSZhou Wang 
67362c455caSZhou Wang 	case QM_HW_V2:
67462c455caSZhou Wang 		qm->ctrl_qp_num = HZIP_QUEUE_NUM_V2;
67562c455caSZhou Wang 		break;
67662c455caSZhou Wang 
67762c455caSZhou Wang 	default:
67862c455caSZhou Wang 		return -EINVAL;
67962c455caSZhou Wang 	}
68062c455caSZhou Wang 
68162c455caSZhou Wang 	hisi_zip_set_user_domain_and_cache(hisi_zip);
68262c455caSZhou Wang 	hisi_zip_hw_error_init(hisi_zip);
68372c7a68dSZhou Wang 	hisi_zip_debug_regs_clear(hisi_zip);
68462c455caSZhou Wang 
68562c455caSZhou Wang 	return 0;
68662c455caSZhou Wang }
68762c455caSZhou Wang 
68862c455caSZhou Wang static int hisi_zip_probe(struct pci_dev *pdev, const struct pci_device_id *id)
68962c455caSZhou Wang {
69062c455caSZhou Wang 	struct hisi_zip *hisi_zip;
69162c455caSZhou Wang 	enum qm_hw_ver rev_id;
69262c455caSZhou Wang 	struct hisi_qm *qm;
69362c455caSZhou Wang 	int ret;
69462c455caSZhou Wang 
69562c455caSZhou Wang 	rev_id = hisi_qm_get_hw_version(pdev);
69662c455caSZhou Wang 	if (rev_id == QM_HW_UNKNOWN)
69762c455caSZhou Wang 		return -EINVAL;
69862c455caSZhou Wang 
69962c455caSZhou Wang 	hisi_zip = devm_kzalloc(&pdev->dev, sizeof(*hisi_zip), GFP_KERNEL);
70062c455caSZhou Wang 	if (!hisi_zip)
70162c455caSZhou Wang 		return -ENOMEM;
70262c455caSZhou Wang 	pci_set_drvdata(pdev, hisi_zip);
70362c455caSZhou Wang 
70462c455caSZhou Wang 	qm = &hisi_zip->qm;
70562c455caSZhou Wang 	qm->pdev = pdev;
70662c455caSZhou Wang 	qm->ver = rev_id;
70762c455caSZhou Wang 
70862c455caSZhou Wang 	qm->sqe_size = HZIP_SQE_SIZE;
70962c455caSZhou Wang 	qm->dev_name = hisi_zip_name;
71079e09f30SZhou Wang 	qm->fun_type = (pdev->device == PCI_DEVICE_ID_ZIP_PF) ? QM_HW_PF :
71179e09f30SZhou Wang 								QM_HW_VF;
71262c455caSZhou Wang 	switch (uacce_mode) {
71362c455caSZhou Wang 	case 0:
71462c455caSZhou Wang 		qm->use_dma_api = true;
71562c455caSZhou Wang 		break;
71662c455caSZhou Wang 	case 1:
71762c455caSZhou Wang 		qm->use_dma_api = false;
71862c455caSZhou Wang 		break;
71962c455caSZhou Wang 	case 2:
72062c455caSZhou Wang 		qm->use_dma_api = true;
72162c455caSZhou Wang 		break;
72262c455caSZhou Wang 	default:
72362c455caSZhou Wang 		return -EINVAL;
72462c455caSZhou Wang 	}
72562c455caSZhou Wang 
72662c455caSZhou Wang 	ret = hisi_qm_init(qm);
72762c455caSZhou Wang 	if (ret) {
72862c455caSZhou Wang 		dev_err(&pdev->dev, "Failed to init qm!\n");
72962c455caSZhou Wang 		return ret;
73062c455caSZhou Wang 	}
73162c455caSZhou Wang 
73279e09f30SZhou Wang 	if (qm->fun_type == QM_HW_PF) {
73362c455caSZhou Wang 		ret = hisi_zip_pf_probe_init(hisi_zip);
73462c455caSZhou Wang 		if (ret)
73579e09f30SZhou Wang 			return ret;
73662c455caSZhou Wang 
73762c455caSZhou Wang 		qm->qp_base = HZIP_PF_DEF_Q_BASE;
73862c455caSZhou Wang 		qm->qp_num = pf_q_num;
73979e09f30SZhou Wang 	} else if (qm->fun_type == QM_HW_VF) {
74079e09f30SZhou Wang 		/*
74179e09f30SZhou Wang 		 * have no way to get qm configure in VM in v1 hardware,
74279e09f30SZhou Wang 		 * so currently force PF to uses HZIP_PF_DEF_Q_NUM, and force
74379e09f30SZhou Wang 		 * to trigger only one VF in v1 hardware.
74479e09f30SZhou Wang 		 *
74579e09f30SZhou Wang 		 * v2 hardware has no such problem.
74679e09f30SZhou Wang 		 */
74779e09f30SZhou Wang 		if (qm->ver == QM_HW_V1) {
74879e09f30SZhou Wang 			qm->qp_base = HZIP_PF_DEF_Q_NUM;
74979e09f30SZhou Wang 			qm->qp_num = HZIP_QUEUE_NUM_V1 - HZIP_PF_DEF_Q_NUM;
75079e09f30SZhou Wang 		} else if (qm->ver == QM_HW_V2)
75179e09f30SZhou Wang 			/* v2 starts to support get vft by mailbox */
75279e09f30SZhou Wang 			hisi_qm_get_vft(qm, &qm->qp_base, &qm->qp_num);
75379e09f30SZhou Wang 	}
75462c455caSZhou Wang 
75562c455caSZhou Wang 	ret = hisi_qm_start(qm);
75662c455caSZhou Wang 	if (ret)
75762c455caSZhou Wang 		goto err_qm_uninit;
75862c455caSZhou Wang 
75972c7a68dSZhou Wang 	ret = hisi_zip_debugfs_init(hisi_zip);
76072c7a68dSZhou Wang 	if (ret)
76172c7a68dSZhou Wang 		dev_err(&pdev->dev, "Failed to init debugfs (%d)!\n", ret);
76272c7a68dSZhou Wang 
76362c455caSZhou Wang 	hisi_zip_add_to_list(hisi_zip);
76462c455caSZhou Wang 
76562c455caSZhou Wang 	return 0;
76662c455caSZhou Wang 
76762c455caSZhou Wang err_qm_uninit:
76862c455caSZhou Wang 	hisi_qm_uninit(qm);
76962c455caSZhou Wang 	return ret;
77062c455caSZhou Wang }
77162c455caSZhou Wang 
77279e09f30SZhou Wang /* Currently we only support equal assignment */
77379e09f30SZhou Wang static int hisi_zip_vf_q_assign(struct hisi_zip *hisi_zip, int num_vfs)
77479e09f30SZhou Wang {
77579e09f30SZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
77679e09f30SZhou Wang 	u32 qp_num = qm->qp_num;
77779e09f30SZhou Wang 	u32 q_base = qp_num;
77879e09f30SZhou Wang 	u32 q_num, remain_q_num, i;
77979e09f30SZhou Wang 	int ret;
78079e09f30SZhou Wang 
78179e09f30SZhou Wang 	if (!num_vfs)
78279e09f30SZhou Wang 		return -EINVAL;
78379e09f30SZhou Wang 
78479e09f30SZhou Wang 	remain_q_num = qm->ctrl_qp_num - qp_num;
78579e09f30SZhou Wang 	if (remain_q_num < num_vfs)
78679e09f30SZhou Wang 		return -EINVAL;
78779e09f30SZhou Wang 
78879e09f30SZhou Wang 	q_num = remain_q_num / num_vfs;
78979e09f30SZhou Wang 	for (i = 1; i <= num_vfs; i++) {
79079e09f30SZhou Wang 		if (i == num_vfs)
79179e09f30SZhou Wang 			q_num += remain_q_num % num_vfs;
79279e09f30SZhou Wang 		ret = hisi_qm_set_vft(qm, i, q_base, q_num);
79379e09f30SZhou Wang 		if (ret)
79479e09f30SZhou Wang 			return ret;
79579e09f30SZhou Wang 		q_base += q_num;
79679e09f30SZhou Wang 	}
79779e09f30SZhou Wang 
79879e09f30SZhou Wang 	return 0;
79979e09f30SZhou Wang }
80079e09f30SZhou Wang 
80179e09f30SZhou Wang static int hisi_zip_clear_vft_config(struct hisi_zip *hisi_zip)
80279e09f30SZhou Wang {
80379e09f30SZhou Wang 	struct hisi_zip_ctrl *ctrl = hisi_zip->ctrl;
80479e09f30SZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
80579e09f30SZhou Wang 	u32 i, num_vfs = ctrl->num_vfs;
80679e09f30SZhou Wang 	int ret;
80779e09f30SZhou Wang 
80879e09f30SZhou Wang 	for (i = 1; i <= num_vfs; i++) {
80979e09f30SZhou Wang 		ret = hisi_qm_set_vft(qm, i, 0, 0);
81079e09f30SZhou Wang 		if (ret)
81179e09f30SZhou Wang 			return ret;
81279e09f30SZhou Wang 	}
81379e09f30SZhou Wang 
81479e09f30SZhou Wang 	ctrl->num_vfs = 0;
81579e09f30SZhou Wang 
81679e09f30SZhou Wang 	return 0;
81779e09f30SZhou Wang }
81879e09f30SZhou Wang 
81979e09f30SZhou Wang static int hisi_zip_sriov_enable(struct pci_dev *pdev, int max_vfs)
82079e09f30SZhou Wang {
82179e09f30SZhou Wang 	struct hisi_zip *hisi_zip = pci_get_drvdata(pdev);
82279e09f30SZhou Wang 	int pre_existing_vfs, num_vfs, ret;
82379e09f30SZhou Wang 
82479e09f30SZhou Wang 	pre_existing_vfs = pci_num_vf(pdev);
82579e09f30SZhou Wang 
82679e09f30SZhou Wang 	if (pre_existing_vfs) {
82779e09f30SZhou Wang 		dev_err(&pdev->dev,
82879e09f30SZhou Wang 			"Can't enable VF. Please disable pre-enabled VFs!\n");
82979e09f30SZhou Wang 		return 0;
83079e09f30SZhou Wang 	}
83179e09f30SZhou Wang 
83279e09f30SZhou Wang 	num_vfs = min_t(int, max_vfs, HZIP_VF_NUM);
83379e09f30SZhou Wang 
83479e09f30SZhou Wang 	ret = hisi_zip_vf_q_assign(hisi_zip, num_vfs);
83579e09f30SZhou Wang 	if (ret) {
83679e09f30SZhou Wang 		dev_err(&pdev->dev, "Can't assign queues for VF!\n");
83779e09f30SZhou Wang 		return ret;
83879e09f30SZhou Wang 	}
83979e09f30SZhou Wang 
84079e09f30SZhou Wang 	hisi_zip->ctrl->num_vfs = num_vfs;
84179e09f30SZhou Wang 
84279e09f30SZhou Wang 	ret = pci_enable_sriov(pdev, num_vfs);
84379e09f30SZhou Wang 	if (ret) {
84479e09f30SZhou Wang 		dev_err(&pdev->dev, "Can't enable VF!\n");
84579e09f30SZhou Wang 		hisi_zip_clear_vft_config(hisi_zip);
84679e09f30SZhou Wang 		return ret;
84779e09f30SZhou Wang 	}
84879e09f30SZhou Wang 
84979e09f30SZhou Wang 	return num_vfs;
85079e09f30SZhou Wang }
85179e09f30SZhou Wang 
85279e09f30SZhou Wang static int hisi_zip_sriov_disable(struct pci_dev *pdev)
85379e09f30SZhou Wang {
85479e09f30SZhou Wang 	struct hisi_zip *hisi_zip = pci_get_drvdata(pdev);
85579e09f30SZhou Wang 
85679e09f30SZhou Wang 	if (pci_vfs_assigned(pdev)) {
85779e09f30SZhou Wang 		dev_err(&pdev->dev,
85879e09f30SZhou Wang 			"Can't disable VFs while VFs are assigned!\n");
85979e09f30SZhou Wang 		return -EPERM;
86079e09f30SZhou Wang 	}
86179e09f30SZhou Wang 
86279e09f30SZhou Wang 	/* remove in hisi_zip_pci_driver will be called to free VF resources */
86379e09f30SZhou Wang 	pci_disable_sriov(pdev);
86479e09f30SZhou Wang 
86579e09f30SZhou Wang 	return hisi_zip_clear_vft_config(hisi_zip);
86679e09f30SZhou Wang }
86779e09f30SZhou Wang 
86879e09f30SZhou Wang static int hisi_zip_sriov_configure(struct pci_dev *pdev, int num_vfs)
86979e09f30SZhou Wang {
87079e09f30SZhou Wang 	if (num_vfs == 0)
87179e09f30SZhou Wang 		return hisi_zip_sriov_disable(pdev);
87279e09f30SZhou Wang 	else
87379e09f30SZhou Wang 		return hisi_zip_sriov_enable(pdev, num_vfs);
87479e09f30SZhou Wang }
87579e09f30SZhou Wang 
87662c455caSZhou Wang static void hisi_zip_remove(struct pci_dev *pdev)
87762c455caSZhou Wang {
87862c455caSZhou Wang 	struct hisi_zip *hisi_zip = pci_get_drvdata(pdev);
87962c455caSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
88062c455caSZhou Wang 
88179e09f30SZhou Wang 	if (qm->fun_type == QM_HW_PF && hisi_zip->ctrl->num_vfs != 0)
88279e09f30SZhou Wang 		hisi_zip_sriov_disable(pdev);
88379e09f30SZhou Wang 
88472c7a68dSZhou Wang 	hisi_zip_debugfs_exit(hisi_zip);
88562c455caSZhou Wang 	hisi_qm_stop(qm);
88679e09f30SZhou Wang 
88779e09f30SZhou Wang 	if (qm->fun_type == QM_HW_PF)
88862c455caSZhou Wang 		hisi_zip_hw_error_set_state(hisi_zip, false);
88979e09f30SZhou Wang 
89062c455caSZhou Wang 	hisi_qm_uninit(qm);
89162c455caSZhou Wang 	hisi_zip_remove_from_list(hisi_zip);
89262c455caSZhou Wang }
89362c455caSZhou Wang 
89462c455caSZhou Wang static void hisi_zip_log_hw_error(struct hisi_zip *hisi_zip, u32 err_sts)
89562c455caSZhou Wang {
89662c455caSZhou Wang 	const struct hisi_zip_hw_error *err = zip_hw_error;
89762c455caSZhou Wang 	struct device *dev = &hisi_zip->qm.pdev->dev;
89862c455caSZhou Wang 	u32 err_val;
89962c455caSZhou Wang 
90062c455caSZhou Wang 	while (err->msg) {
90162c455caSZhou Wang 		if (err->int_msk & err_sts) {
90262c455caSZhou Wang 			dev_warn(dev, "%s [error status=0x%x] found\n",
90362c455caSZhou Wang 				 err->msg, err->int_msk);
90462c455caSZhou Wang 
90562c455caSZhou Wang 			if (HZIP_CORE_INT_STATUS_M_ECC & err->int_msk) {
90662c455caSZhou Wang 				err_val = readl(hisi_zip->qm.io_base +
90762c455caSZhou Wang 						HZIP_CORE_SRAM_ECC_ERR_INFO);
90862c455caSZhou Wang 				dev_warn(dev, "hisi-zip multi ecc sram num=0x%x\n",
90962c455caSZhou Wang 					 ((err_val >> SRAM_ECC_ERR_NUM_SHIFT) &
91062c455caSZhou Wang 					  0xFF));
91162c455caSZhou Wang 				dev_warn(dev, "hisi-zip multi ecc sram addr=0x%x\n",
91262c455caSZhou Wang 					 (err_val >> SRAM_ECC_ERR_ADDR_SHIFT));
91362c455caSZhou Wang 			}
91462c455caSZhou Wang 		}
91562c455caSZhou Wang 		err++;
91662c455caSZhou Wang 	}
91762c455caSZhou Wang }
91862c455caSZhou Wang 
91962c455caSZhou Wang static pci_ers_result_t hisi_zip_hw_error_handle(struct hisi_zip *hisi_zip)
92062c455caSZhou Wang {
92162c455caSZhou Wang 	u32 err_sts;
92262c455caSZhou Wang 
92362c455caSZhou Wang 	/* read err sts */
92462c455caSZhou Wang 	err_sts = readl(hisi_zip->qm.io_base + HZIP_CORE_INT_STATUS);
92562c455caSZhou Wang 
92662c455caSZhou Wang 	if (err_sts) {
92762c455caSZhou Wang 		hisi_zip_log_hw_error(hisi_zip, err_sts);
92862c455caSZhou Wang 		/* clear error interrupts */
92962c455caSZhou Wang 		writel(err_sts, hisi_zip->qm.io_base + HZIP_CORE_INT_SOURCE);
93062c455caSZhou Wang 
93162c455caSZhou Wang 		return PCI_ERS_RESULT_NEED_RESET;
93262c455caSZhou Wang 	}
93362c455caSZhou Wang 
93462c455caSZhou Wang 	return PCI_ERS_RESULT_RECOVERED;
93562c455caSZhou Wang }
93662c455caSZhou Wang 
93762c455caSZhou Wang static pci_ers_result_t hisi_zip_process_hw_error(struct pci_dev *pdev)
93862c455caSZhou Wang {
93962c455caSZhou Wang 	struct hisi_zip *hisi_zip = pci_get_drvdata(pdev);
94062c455caSZhou Wang 	struct device *dev = &pdev->dev;
94162c455caSZhou Wang 	pci_ers_result_t qm_ret, zip_ret;
94262c455caSZhou Wang 
94362c455caSZhou Wang 	if (!hisi_zip) {
94462c455caSZhou Wang 		dev_err(dev,
94562c455caSZhou Wang 			"Can't recover ZIP-error occurred during device init\n");
94662c455caSZhou Wang 		return PCI_ERS_RESULT_NONE;
94762c455caSZhou Wang 	}
94862c455caSZhou Wang 
94962c455caSZhou Wang 	qm_ret = hisi_qm_hw_error_handle(&hisi_zip->qm);
95062c455caSZhou Wang 
95162c455caSZhou Wang 	zip_ret = hisi_zip_hw_error_handle(hisi_zip);
95262c455caSZhou Wang 
95362c455caSZhou Wang 	return (qm_ret == PCI_ERS_RESULT_NEED_RESET ||
95462c455caSZhou Wang 		zip_ret == PCI_ERS_RESULT_NEED_RESET) ?
95562c455caSZhou Wang 	       PCI_ERS_RESULT_NEED_RESET : PCI_ERS_RESULT_RECOVERED;
95662c455caSZhou Wang }
95762c455caSZhou Wang 
95862c455caSZhou Wang static pci_ers_result_t hisi_zip_error_detected(struct pci_dev *pdev,
95962c455caSZhou Wang 						pci_channel_state_t state)
96062c455caSZhou Wang {
96162c455caSZhou Wang 	if (pdev->is_virtfn)
96262c455caSZhou Wang 		return PCI_ERS_RESULT_NONE;
96362c455caSZhou Wang 
96462c455caSZhou Wang 	dev_info(&pdev->dev, "PCI error detected, state(=%d)!!\n", state);
96562c455caSZhou Wang 	if (state == pci_channel_io_perm_failure)
96662c455caSZhou Wang 		return PCI_ERS_RESULT_DISCONNECT;
96762c455caSZhou Wang 
96862c455caSZhou Wang 	return hisi_zip_process_hw_error(pdev);
96962c455caSZhou Wang }
97062c455caSZhou Wang 
97162c455caSZhou Wang static const struct pci_error_handlers hisi_zip_err_handler = {
97262c455caSZhou Wang 	.error_detected	= hisi_zip_error_detected,
97362c455caSZhou Wang };
97462c455caSZhou Wang 
97562c455caSZhou Wang static struct pci_driver hisi_zip_pci_driver = {
97662c455caSZhou Wang 	.name			= "hisi_zip",
97762c455caSZhou Wang 	.id_table		= hisi_zip_dev_ids,
97862c455caSZhou Wang 	.probe			= hisi_zip_probe,
97962c455caSZhou Wang 	.remove			= hisi_zip_remove,
980bf6a7a5aSArnd Bergmann 	.sriov_configure	= IS_ENABLED(CONFIG_PCI_IOV) ?
981719181f3SShukun Tan 					hisi_zip_sriov_configure : NULL,
98262c455caSZhou Wang 	.err_handler		= &hisi_zip_err_handler,
98362c455caSZhou Wang };
98462c455caSZhou Wang 
98572c7a68dSZhou Wang static void hisi_zip_register_debugfs(void)
98672c7a68dSZhou Wang {
98772c7a68dSZhou Wang 	if (!debugfs_initialized())
98872c7a68dSZhou Wang 		return;
98972c7a68dSZhou Wang 
99072c7a68dSZhou Wang 	hzip_debugfs_root = debugfs_create_dir("hisi_zip", NULL);
99172c7a68dSZhou Wang 	if (IS_ERR_OR_NULL(hzip_debugfs_root))
99272c7a68dSZhou Wang 		hzip_debugfs_root = NULL;
99372c7a68dSZhou Wang }
99472c7a68dSZhou Wang 
99572c7a68dSZhou Wang static void hisi_zip_unregister_debugfs(void)
99672c7a68dSZhou Wang {
99772c7a68dSZhou Wang 	debugfs_remove_recursive(hzip_debugfs_root);
99872c7a68dSZhou Wang }
99972c7a68dSZhou Wang 
100062c455caSZhou Wang static int __init hisi_zip_init(void)
100162c455caSZhou Wang {
100262c455caSZhou Wang 	int ret;
100362c455caSZhou Wang 
100472c7a68dSZhou Wang 	hisi_zip_register_debugfs();
100572c7a68dSZhou Wang 
100662c455caSZhou Wang 	ret = pci_register_driver(&hisi_zip_pci_driver);
100762c455caSZhou Wang 	if (ret < 0) {
100862c455caSZhou Wang 		pr_err("Failed to register pci driver.\n");
100972c7a68dSZhou Wang 		goto err_pci;
101062c455caSZhou Wang 	}
101162c455caSZhou Wang 
101262c455caSZhou Wang 	if (uacce_mode == 0 || uacce_mode == 2) {
101362c455caSZhou Wang 		ret = hisi_zip_register_to_crypto();
101462c455caSZhou Wang 		if (ret < 0) {
101562c455caSZhou Wang 			pr_err("Failed to register driver to crypto.\n");
101662c455caSZhou Wang 			goto err_crypto;
101762c455caSZhou Wang 		}
101862c455caSZhou Wang 	}
101962c455caSZhou Wang 
102062c455caSZhou Wang 	return 0;
102162c455caSZhou Wang 
102262c455caSZhou Wang err_crypto:
102362c455caSZhou Wang 	pci_unregister_driver(&hisi_zip_pci_driver);
102472c7a68dSZhou Wang err_pci:
102572c7a68dSZhou Wang 	hisi_zip_unregister_debugfs();
102672c7a68dSZhou Wang 
102762c455caSZhou Wang 	return ret;
102862c455caSZhou Wang }
102962c455caSZhou Wang 
103062c455caSZhou Wang static void __exit hisi_zip_exit(void)
103162c455caSZhou Wang {
103262c455caSZhou Wang 	if (uacce_mode == 0 || uacce_mode == 2)
103362c455caSZhou Wang 		hisi_zip_unregister_from_crypto();
103462c455caSZhou Wang 	pci_unregister_driver(&hisi_zip_pci_driver);
103572c7a68dSZhou Wang 	hisi_zip_unregister_debugfs();
103662c455caSZhou Wang }
103762c455caSZhou Wang 
103862c455caSZhou Wang module_init(hisi_zip_init);
103962c455caSZhou Wang module_exit(hisi_zip_exit);
104062c455caSZhou Wang 
104162c455caSZhou Wang MODULE_LICENSE("GPL v2");
104262c455caSZhou Wang MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>");
104362c455caSZhou Wang MODULE_DESCRIPTION("Driver for HiSilicon ZIP accelerator");
1044