xref: /openbmc/linux/drivers/crypto/hisilicon/zip/zip_main.c (revision ecc23d0a422a3118fcf6e4f0a46e17a6c2047b02)
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/bitops.h>
572c7a68dSZhou Wang #include <linux/debugfs.h>
662c455caSZhou Wang #include <linux/init.h>
762c455caSZhou Wang #include <linux/io.h>
862c455caSZhou Wang #include <linux/kernel.h>
962c455caSZhou Wang #include <linux/module.h>
1062c455caSZhou Wang #include <linux/pci.h>
11607c191bSWeili Qian #include <linux/pm_runtime.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 
17fae74feaSShameer Kolothum #define PCI_DEVICE_ID_HUAWEI_ZIP_PF	0xa250
1862c455caSZhou Wang 
1962c455caSZhou Wang #define HZIP_QUEUE_NUM_V1		4096
2062c455caSZhou Wang 
2162c455caSZhou Wang #define HZIP_CLOCK_GATE_CTRL		0x301004
2215b0694fSYang Shen #define HZIP_DECOMP_CHECK_ENABLE	BIT(16)
2372c7a68dSZhou Wang #define HZIP_FSM_MAX_CNT		0x301008
2462c455caSZhou Wang 
2562c455caSZhou Wang #define HZIP_PORT_ARCA_CHE_0		0x301040
2662c455caSZhou Wang #define HZIP_PORT_ARCA_CHE_1		0x301044
2762c455caSZhou Wang #define HZIP_PORT_AWCA_CHE_0		0x301060
2862c455caSZhou Wang #define HZIP_PORT_AWCA_CHE_1		0x301064
2915b0694fSYang Shen #define HZIP_CACHE_ALL_EN		0xffffffff
3062c455caSZhou Wang 
3162c455caSZhou Wang #define HZIP_BD_RUSER_32_63		0x301110
3262c455caSZhou Wang #define HZIP_SGL_RUSER_32_63		0x30111c
3362c455caSZhou Wang #define HZIP_DATA_RUSER_32_63		0x301128
3462c455caSZhou Wang #define HZIP_DATA_WUSER_32_63		0x301134
3562c455caSZhou Wang #define HZIP_BD_WUSER_32_63		0x301140
3662c455caSZhou Wang 
3772c7a68dSZhou Wang #define HZIP_QM_IDEL_STATUS		0x3040e4
3862c455caSZhou Wang 
399b0c97dfSKai Ye #define HZIP_CORE_DFX_BASE		0x301000
409b0c97dfSKai Ye #define HZIP_CLOCK_GATED_CONTL		0X301004
419b0c97dfSKai Ye #define HZIP_CORE_DFX_COMP_0		0x302000
429b0c97dfSKai Ye #define HZIP_CORE_DFX_COMP_1		0x303000
439b0c97dfSKai Ye #define HZIP_CORE_DFX_DECOMP_0		0x304000
449b0c97dfSKai Ye #define HZIP_CORE_DFX_DECOMP_1		0x305000
459b0c97dfSKai Ye #define HZIP_CORE_DFX_DECOMP_2		0x306000
469b0c97dfSKai Ye #define HZIP_CORE_DFX_DECOMP_3		0x307000
479b0c97dfSKai Ye #define HZIP_CORE_DFX_DECOMP_4		0x308000
489b0c97dfSKai Ye #define HZIP_CORE_DFX_DECOMP_5		0x309000
499b0c97dfSKai Ye #define HZIP_CORE_REGS_BASE_LEN		0xB0
509b0c97dfSKai Ye #define HZIP_CORE_REGS_DFX_LEN		0x28
5162c455caSZhou Wang 
5262c455caSZhou Wang #define HZIP_CORE_INT_SOURCE		0x3010A0
53eaebf4c3SShukun Tan #define HZIP_CORE_INT_MASK_REG		0x3010A4
5484c9b780SShukun Tan #define HZIP_CORE_INT_SET		0x3010A8
5562c455caSZhou Wang #define HZIP_CORE_INT_STATUS		0x3010AC
5662c455caSZhou Wang #define HZIP_CORE_INT_STATUS_M_ECC	BIT(1)
5762c455caSZhou Wang #define HZIP_CORE_SRAM_ECC_ERR_INFO	0x301148
58de3daf4bSShukun Tan #define HZIP_CORE_INT_RAS_CE_ENB	0x301160
59de3daf4bSShukun Tan #define HZIP_CORE_INT_RAS_NFE_ENB	0x301164
60de3daf4bSShukun Tan #define HZIP_CORE_INT_RAS_FE_ENB        0x301168
61d90fab0dSWeili Qian #define HZIP_CORE_INT_RAS_FE_ENB_MASK	0x0
62b7da13d0SWeili Qian #define HZIP_OOO_SHUTDOWN_SEL		0x30120C
63f826e6efSShukun Tan #define HZIP_SRAM_ECC_ERR_NUM_SHIFT	16
64f826e6efSShukun Tan #define HZIP_SRAM_ECC_ERR_ADDR_SHIFT	24
65b7220a74SWeili Qian #define HZIP_CORE_INT_MASK_ALL		GENMASK(12, 0)
6662c455caSZhou Wang #define HZIP_SQE_SIZE			128
6762c455caSZhou Wang #define HZIP_PF_DEF_Q_NUM		64
6862c455caSZhou Wang #define HZIP_PF_DEF_Q_BASE		0
6962c455caSZhou Wang 
7072c7a68dSZhou Wang #define HZIP_SOFT_CTRL_CNT_CLR_CE	0x301000
7115b0694fSYang Shen #define HZIP_SOFT_CTRL_CNT_CLR_CE_BIT	BIT(0)
7284c9b780SShukun Tan #define HZIP_SOFT_CTRL_ZIP_CONTROL	0x30100C
7384c9b780SShukun Tan #define HZIP_AXI_SHUTDOWN_ENABLE	BIT(14)
7484c9b780SShukun Tan #define HZIP_WR_PORT			BIT(11)
7562c455caSZhou Wang 
76d310dc25SZhiqi Song #define HZIP_ALG_ZLIB_BIT		GENMASK(1, 0)
77d310dc25SZhiqi Song #define HZIP_ALG_GZIP_BIT		GENMASK(3, 2)
78d310dc25SZhiqi Song #define HZIP_ALG_DEFLATE_BIT		GENMASK(5, 4)
79d310dc25SZhiqi Song #define HZIP_ALG_LZ77_BIT		GENMASK(7, 6)
80d310dc25SZhiqi Song 
8172c7a68dSZhou Wang #define HZIP_BUF_SIZE			22
82c31dc9feSShukun Tan #define HZIP_SQE_MASK_OFFSET		64
83c31dc9feSShukun Tan #define HZIP_SQE_MASK_LEN		48
8462c455caSZhou Wang 
85698f9523SHao Fang #define HZIP_CNT_CLR_CE_EN		BIT(0)
86698f9523SHao Fang #define HZIP_RO_CNT_CLR_CE_EN		BIT(2)
87698f9523SHao Fang #define HZIP_RD_CNT_CLR_CE_EN		(HZIP_CNT_CLR_CE_EN | \
88698f9523SHao Fang 					 HZIP_RO_CNT_CLR_CE_EN)
89698f9523SHao Fang 
90a5c164b1SLongfang Liu #define HZIP_PREFETCH_CFG		0x3011B0
91a5c164b1SLongfang Liu #define HZIP_SVA_TRANS			0x3011C4
92a5c164b1SLongfang Liu #define HZIP_PREFETCH_ENABLE		(~(BIT(26) | BIT(17) | BIT(0)))
93a5c164b1SLongfang Liu #define HZIP_SVA_PREFETCH_DISABLE	BIT(26)
94a5c164b1SLongfang Liu #define HZIP_SVA_DISABLE_READY		(BIT(26) | BIT(30))
95376a5c3cSKai Ye #define HZIP_SHAPER_RATE_COMPRESS	750
96376a5c3cSKai Ye #define HZIP_SHAPER_RATE_DECOMPRESS	140
97a5c164b1SLongfang Liu #define HZIP_DELAY_1_US		1
98a5c164b1SLongfang Liu #define HZIP_POLL_TIMEOUT_US	1000
99a5c164b1SLongfang Liu 
100ed5fa39fSWeili Qian /* clock gating */
101ed5fa39fSWeili Qian #define HZIP_PEH_CFG_AUTO_GATE		0x3011A8
102ed5fa39fSWeili Qian #define HZIP_PEH_CFG_AUTO_GATE_EN	BIT(0)
103ed5fa39fSWeili Qian #define HZIP_CORE_GATED_EN		GENMASK(15, 8)
104ed5fa39fSWeili Qian #define HZIP_CORE_GATED_OOO_EN		BIT(29)
105ed5fa39fSWeili Qian #define HZIP_CLOCK_GATED_EN		(HZIP_CORE_GATED_EN | \
106ed5fa39fSWeili Qian 					 HZIP_CORE_GATED_OOO_EN)
107ed5fa39fSWeili Qian 
108b7a03a0fSChenghai Huang /* zip comp high performance */
109b7a03a0fSChenghai Huang #define HZIP_HIGH_PERF_OFFSET		0x301208
110b7a03a0fSChenghai Huang 
111b7a03a0fSChenghai Huang enum {
112b7a03a0fSChenghai Huang 	HZIP_HIGH_COMP_RATE,
113b7a03a0fSChenghai Huang 	HZIP_HIGH_COMP_PERF,
114b7a03a0fSChenghai Huang };
115b7a03a0fSChenghai Huang 
11662c455caSZhou Wang static const char hisi_zip_name[] = "hisi_zip";
11772c7a68dSZhou Wang static struct dentry *hzip_debugfs_root;
11862c455caSZhou Wang 
11962c455caSZhou Wang struct hisi_zip_hw_error {
12062c455caSZhou Wang 	u32 int_msk;
12162c455caSZhou Wang 	const char *msg;
12262c455caSZhou Wang };
12362c455caSZhou Wang 
1246621e649SLongfang Liu struct zip_dfx_item {
1256621e649SLongfang Liu 	const char *name;
1266621e649SLongfang Liu 	u32 offset;
1276621e649SLongfang Liu };
1286621e649SLongfang Liu 
1291e8102e2SWenkai Lin static const struct qm_dev_alg zip_dev_algs[] = { {
130d310dc25SZhiqi Song 		.alg_msk = HZIP_ALG_ZLIB_BIT,
1311e8102e2SWenkai Lin 		.alg = "zlib\n",
132d310dc25SZhiqi Song 	}, {
133d310dc25SZhiqi Song 		.alg_msk = HZIP_ALG_GZIP_BIT,
1341e8102e2SWenkai Lin 		.alg = "gzip\n",
135d310dc25SZhiqi Song 	}, {
136d310dc25SZhiqi Song 		.alg_msk = HZIP_ALG_DEFLATE_BIT,
1371e8102e2SWenkai Lin 		.alg = "deflate\n",
138d310dc25SZhiqi Song 	}, {
139d310dc25SZhiqi Song 		.alg_msk = HZIP_ALG_LZ77_BIT,
1401e8102e2SWenkai Lin 		.alg = "lz77_zstd\n",
141d310dc25SZhiqi Song 	},
142d310dc25SZhiqi Song };
143d310dc25SZhiqi Song 
1443d29e98dSYang Shen static struct hisi_qm_list zip_devices = {
1453d29e98dSYang Shen 	.register_to_crypto	= hisi_zip_register_to_crypto,
1463d29e98dSYang Shen 	.unregister_from_crypto	= hisi_zip_unregister_from_crypto,
1473d29e98dSYang Shen };
1483d29e98dSYang Shen 
1496621e649SLongfang Liu static struct zip_dfx_item zip_dfx_files[] = {
1506621e649SLongfang Liu 	{"send_cnt", offsetof(struct hisi_zip_dfx, send_cnt)},
1516621e649SLongfang Liu 	{"recv_cnt", offsetof(struct hisi_zip_dfx, recv_cnt)},
1526621e649SLongfang Liu 	{"send_busy_cnt", offsetof(struct hisi_zip_dfx, send_busy_cnt)},
1536621e649SLongfang Liu 	{"err_bd_cnt", offsetof(struct hisi_zip_dfx, err_bd_cnt)},
1546621e649SLongfang Liu };
1556621e649SLongfang Liu 
15662c455caSZhou Wang static const struct hisi_zip_hw_error zip_hw_error[] = {
15762c455caSZhou Wang 	{ .int_msk = BIT(0), .msg = "zip_ecc_1bitt_err" },
15862c455caSZhou Wang 	{ .int_msk = BIT(1), .msg = "zip_ecc_2bit_err" },
15962c455caSZhou Wang 	{ .int_msk = BIT(2), .msg = "zip_axi_rresp_err" },
16062c455caSZhou Wang 	{ .int_msk = BIT(3), .msg = "zip_axi_bresp_err" },
16162c455caSZhou Wang 	{ .int_msk = BIT(4), .msg = "zip_src_addr_parse_err" },
16262c455caSZhou Wang 	{ .int_msk = BIT(5), .msg = "zip_dst_addr_parse_err" },
16362c455caSZhou Wang 	{ .int_msk = BIT(6), .msg = "zip_pre_in_addr_err" },
16462c455caSZhou Wang 	{ .int_msk = BIT(7), .msg = "zip_pre_in_data_err" },
16562c455caSZhou Wang 	{ .int_msk = BIT(8), .msg = "zip_com_inf_err" },
16662c455caSZhou Wang 	{ .int_msk = BIT(9), .msg = "zip_enc_inf_err" },
16762c455caSZhou Wang 	{ .int_msk = BIT(10), .msg = "zip_pre_out_err" },
168b7220a74SWeili Qian 	{ .int_msk = BIT(11), .msg = "zip_axi_poison_err" },
169b7220a74SWeili Qian 	{ .int_msk = BIT(12), .msg = "zip_sva_err" },
17062c455caSZhou Wang 	{ /* sentinel */ }
17162c455caSZhou Wang };
17262c455caSZhou Wang 
17372c7a68dSZhou Wang enum ctrl_debug_file_index {
17472c7a68dSZhou Wang 	HZIP_CLEAR_ENABLE,
17572c7a68dSZhou Wang 	HZIP_DEBUG_FILE_NUM,
17672c7a68dSZhou Wang };
17772c7a68dSZhou Wang 
17872c7a68dSZhou Wang static const char * const ctrl_debug_file_name[] = {
17972c7a68dSZhou Wang 	[HZIP_CLEAR_ENABLE] = "clear_enable",
18072c7a68dSZhou Wang };
18172c7a68dSZhou Wang 
18272c7a68dSZhou Wang struct ctrl_debug_file {
18372c7a68dSZhou Wang 	enum ctrl_debug_file_index index;
18472c7a68dSZhou Wang 	spinlock_t lock;
18572c7a68dSZhou Wang 	struct hisi_zip_ctrl *ctrl;
18672c7a68dSZhou Wang };
18772c7a68dSZhou Wang 
18862c455caSZhou Wang /*
18962c455caSZhou Wang  * One ZIP controller has one PF and multiple VFs, some global configurations
19062c455caSZhou Wang  * which PF has need this structure.
19162c455caSZhou Wang  *
19262c455caSZhou Wang  * Just relevant for PF.
19362c455caSZhou Wang  */
19462c455caSZhou Wang struct hisi_zip_ctrl {
19562c455caSZhou Wang 	struct hisi_zip *hisi_zip;
19672c7a68dSZhou Wang 	struct ctrl_debug_file files[HZIP_DEBUG_FILE_NUM];
19772c7a68dSZhou Wang };
19872c7a68dSZhou Wang 
199d90fab0dSWeili Qian enum zip_cap_type {
200d90fab0dSWeili Qian 	ZIP_QM_NFE_MASK_CAP = 0x0,
201d90fab0dSWeili Qian 	ZIP_QM_RESET_MASK_CAP,
202d90fab0dSWeili Qian 	ZIP_QM_OOO_SHUTDOWN_MASK_CAP,
203d90fab0dSWeili Qian 	ZIP_QM_CE_MASK_CAP,
204d90fab0dSWeili Qian 	ZIP_NFE_MASK_CAP,
205d90fab0dSWeili Qian 	ZIP_RESET_MASK_CAP,
206d90fab0dSWeili Qian 	ZIP_OOO_SHUTDOWN_MASK_CAP,
207d90fab0dSWeili Qian 	ZIP_CE_MASK_CAP,
208db700974SWeili Qian 	ZIP_CLUSTER_NUM_CAP,
209db700974SWeili Qian 	ZIP_CORE_TYPE_NUM_CAP,
210db700974SWeili Qian 	ZIP_CORE_NUM_CAP,
211db700974SWeili Qian 	ZIP_CLUSTER_COMP_NUM_CAP,
212db700974SWeili Qian 	ZIP_CLUSTER_DECOMP_NUM_CAP,
213db700974SWeili Qian 	ZIP_DECOMP_ENABLE_BITMAP,
214db700974SWeili Qian 	ZIP_COMP_ENABLE_BITMAP,
215db700974SWeili Qian 	ZIP_DRV_ALG_BITMAP,
216db700974SWeili Qian 	ZIP_DEV_ALG_BITMAP,
217db700974SWeili Qian 	ZIP_CORE1_ALG_BITMAP,
218db700974SWeili Qian 	ZIP_CORE2_ALG_BITMAP,
219db700974SWeili Qian 	ZIP_CORE3_ALG_BITMAP,
220db700974SWeili Qian 	ZIP_CORE4_ALG_BITMAP,
221db700974SWeili Qian 	ZIP_CORE5_ALG_BITMAP,
222db700974SWeili Qian 	ZIP_CAP_MAX
223d90fab0dSWeili Qian };
224d90fab0dSWeili Qian 
225d90fab0dSWeili Qian static struct hisi_qm_cap_info zip_basic_cap_info[] = {
226d90fab0dSWeili Qian 	{ZIP_QM_NFE_MASK_CAP, 0x3124, 0, GENMASK(31, 0), 0x0, 0x1C57, 0x7C77},
227d90fab0dSWeili Qian 	{ZIP_QM_RESET_MASK_CAP, 0x3128, 0, GENMASK(31, 0), 0x0, 0xC57, 0x6C77},
228d90fab0dSWeili Qian 	{ZIP_QM_OOO_SHUTDOWN_MASK_CAP, 0x3128, 0, GENMASK(31, 0), 0x0, 0x4, 0x6C77},
229d90fab0dSWeili Qian 	{ZIP_QM_CE_MASK_CAP, 0x312C, 0, GENMASK(31, 0), 0x0, 0x8, 0x8},
230d90fab0dSWeili Qian 	{ZIP_NFE_MASK_CAP, 0x3130, 0, GENMASK(31, 0), 0x0, 0x7FE, 0x1FFE},
231d90fab0dSWeili Qian 	{ZIP_RESET_MASK_CAP, 0x3134, 0, GENMASK(31, 0), 0x0, 0x7FE, 0x7FE},
232d90fab0dSWeili Qian 	{ZIP_OOO_SHUTDOWN_MASK_CAP, 0x3134, 0, GENMASK(31, 0), 0x0, 0x2, 0x7FE},
233d90fab0dSWeili Qian 	{ZIP_CE_MASK_CAP, 0x3138, 0, GENMASK(31, 0), 0x0, 0x1, 0x1},
234db700974SWeili Qian 	{ZIP_CLUSTER_NUM_CAP, 0x313C, 28, GENMASK(3, 0), 0x1, 0x1, 0x1},
235db700974SWeili Qian 	{ZIP_CORE_TYPE_NUM_CAP, 0x313C, 24, GENMASK(3, 0), 0x2, 0x2, 0x2},
236db700974SWeili Qian 	{ZIP_CORE_NUM_CAP, 0x313C, 16, GENMASK(7, 0), 0x8, 0x8, 0x5},
237db700974SWeili Qian 	{ZIP_CLUSTER_COMP_NUM_CAP, 0x313C, 8, GENMASK(7, 0), 0x2, 0x2, 0x2},
238db700974SWeili Qian 	{ZIP_CLUSTER_DECOMP_NUM_CAP, 0x313C, 0, GENMASK(7, 0), 0x6, 0x6, 0x3},
239db700974SWeili Qian 	{ZIP_DECOMP_ENABLE_BITMAP, 0x3140, 16, GENMASK(15, 0), 0xFC, 0xFC, 0x1C},
240db700974SWeili Qian 	{ZIP_COMP_ENABLE_BITMAP, 0x3140, 0, GENMASK(15, 0), 0x3, 0x3, 0x3},
241db700974SWeili Qian 	{ZIP_DRV_ALG_BITMAP, 0x3144, 0, GENMASK(31, 0), 0xF, 0xF, 0xF},
242db700974SWeili Qian 	{ZIP_DEV_ALG_BITMAP, 0x3148, 0, GENMASK(31, 0), 0xF, 0xF, 0xFF},
243db700974SWeili Qian 	{ZIP_CORE1_ALG_BITMAP, 0x314C, 0, GENMASK(31, 0), 0x5, 0x5, 0xD5},
244db700974SWeili Qian 	{ZIP_CORE2_ALG_BITMAP, 0x3150, 0, GENMASK(31, 0), 0x5, 0x5, 0xD5},
245db700974SWeili Qian 	{ZIP_CORE3_ALG_BITMAP, 0x3154, 0, GENMASK(31, 0), 0xA, 0xA, 0x2A},
246db700974SWeili Qian 	{ZIP_CORE4_ALG_BITMAP, 0x3158, 0, GENMASK(31, 0), 0xA, 0xA, 0x2A},
247db700974SWeili Qian 	{ZIP_CORE5_ALG_BITMAP, 0x315C, 0, GENMASK(31, 0), 0xA, 0xA, 0x2A},
248db700974SWeili Qian 	{ZIP_CAP_MAX, 0x317c, 0, GENMASK(0, 0), 0x0, 0x0, 0x0}
249d90fab0dSWeili Qian };
250d90fab0dSWeili Qian 
25152f0b4a3SZhiqi Song enum zip_pre_store_cap_idx {
25252f0b4a3SZhiqi Song 	ZIP_CORE_NUM_CAP_IDX = 0x0,
25352f0b4a3SZhiqi Song 	ZIP_CLUSTER_COMP_NUM_CAP_IDX,
25452f0b4a3SZhiqi Song 	ZIP_CLUSTER_DECOMP_NUM_CAP_IDX,
25552f0b4a3SZhiqi Song 	ZIP_DECOMP_ENABLE_BITMAP_IDX,
25652f0b4a3SZhiqi Song 	ZIP_COMP_ENABLE_BITMAP_IDX,
25752f0b4a3SZhiqi Song 	ZIP_DRV_ALG_BITMAP_IDX,
25852f0b4a3SZhiqi Song 	ZIP_DEV_ALG_BITMAP_IDX,
25952f0b4a3SZhiqi Song };
26052f0b4a3SZhiqi Song 
26152f0b4a3SZhiqi Song static const u32 zip_pre_store_caps[] = {
26252f0b4a3SZhiqi Song 	ZIP_CORE_NUM_CAP,
26352f0b4a3SZhiqi Song 	ZIP_CLUSTER_COMP_NUM_CAP,
26452f0b4a3SZhiqi Song 	ZIP_CLUSTER_DECOMP_NUM_CAP,
26552f0b4a3SZhiqi Song 	ZIP_DECOMP_ENABLE_BITMAP,
26652f0b4a3SZhiqi Song 	ZIP_COMP_ENABLE_BITMAP,
26752f0b4a3SZhiqi Song 	ZIP_DRV_ALG_BITMAP,
26852f0b4a3SZhiqi Song 	ZIP_DEV_ALG_BITMAP,
26952f0b4a3SZhiqi Song };
27052f0b4a3SZhiqi Song 
27172c7a68dSZhou Wang enum {
27272c7a68dSZhou Wang 	HZIP_COMP_CORE0,
27372c7a68dSZhou Wang 	HZIP_COMP_CORE1,
27472c7a68dSZhou Wang 	HZIP_DECOMP_CORE0,
27572c7a68dSZhou Wang 	HZIP_DECOMP_CORE1,
27672c7a68dSZhou Wang 	HZIP_DECOMP_CORE2,
27772c7a68dSZhou Wang 	HZIP_DECOMP_CORE3,
27872c7a68dSZhou Wang 	HZIP_DECOMP_CORE4,
27972c7a68dSZhou Wang 	HZIP_DECOMP_CORE5,
28072c7a68dSZhou Wang };
28172c7a68dSZhou Wang 
28272c7a68dSZhou Wang static const u64 core_offsets[] = {
28372c7a68dSZhou Wang 	[HZIP_COMP_CORE0]   = 0x302000,
28472c7a68dSZhou Wang 	[HZIP_COMP_CORE1]   = 0x303000,
28572c7a68dSZhou Wang 	[HZIP_DECOMP_CORE0] = 0x304000,
28672c7a68dSZhou Wang 	[HZIP_DECOMP_CORE1] = 0x305000,
28772c7a68dSZhou Wang 	[HZIP_DECOMP_CORE2] = 0x306000,
28872c7a68dSZhou Wang 	[HZIP_DECOMP_CORE3] = 0x307000,
28972c7a68dSZhou Wang 	[HZIP_DECOMP_CORE4] = 0x308000,
29072c7a68dSZhou Wang 	[HZIP_DECOMP_CORE5] = 0x309000,
29172c7a68dSZhou Wang };
29272c7a68dSZhou Wang 
2938f68659bSRikard Falkeborn static const struct debugfs_reg32 hzip_dfx_regs[] = {
29472c7a68dSZhou Wang 	{"HZIP_GET_BD_NUM                ",  0x00ull},
29572c7a68dSZhou Wang 	{"HZIP_GET_RIGHT_BD              ",  0x04ull},
29672c7a68dSZhou Wang 	{"HZIP_GET_ERROR_BD              ",  0x08ull},
29772c7a68dSZhou Wang 	{"HZIP_DONE_BD_NUM               ",  0x0cull},
29872c7a68dSZhou Wang 	{"HZIP_WORK_CYCLE                ",  0x10ull},
29972c7a68dSZhou Wang 	{"HZIP_IDLE_CYCLE                ",  0x18ull},
30072c7a68dSZhou Wang 	{"HZIP_MAX_DELAY                 ",  0x20ull},
30172c7a68dSZhou Wang 	{"HZIP_MIN_DELAY                 ",  0x24ull},
30272c7a68dSZhou Wang 	{"HZIP_AVG_DELAY                 ",  0x28ull},
30372c7a68dSZhou Wang 	{"HZIP_MEM_VISIBLE_DATA          ",  0x30ull},
30472c7a68dSZhou Wang 	{"HZIP_MEM_VISIBLE_ADDR          ",  0x34ull},
3056e96dbe7SColin Ian King 	{"HZIP_CONSUMED_BYTE             ",  0x38ull},
30672c7a68dSZhou Wang 	{"HZIP_PRODUCED_BYTE             ",  0x40ull},
30772c7a68dSZhou Wang 	{"HZIP_COMP_INF                  ",  0x70ull},
30872c7a68dSZhou Wang 	{"HZIP_PRE_OUT                   ",  0x78ull},
30972c7a68dSZhou Wang 	{"HZIP_BD_RD                     ",  0x7cull},
31072c7a68dSZhou Wang 	{"HZIP_BD_WR                     ",  0x80ull},
31172c7a68dSZhou Wang 	{"HZIP_GET_BD_AXI_ERR_NUM        ",  0x84ull},
31272c7a68dSZhou Wang 	{"HZIP_GET_BD_PARSE_ERR_NUM      ",  0x88ull},
31372c7a68dSZhou Wang 	{"HZIP_ADD_BD_AXI_ERR_NUM        ",  0x8cull},
31472c7a68dSZhou Wang 	{"HZIP_DECOMP_STF_RELOAD_CURR_ST ",  0x94ull},
31572c7a68dSZhou Wang 	{"HZIP_DECOMP_LZ77_CURR_ST       ",  0x9cull},
31662c455caSZhou Wang };
31762c455caSZhou Wang 
3185bfabd50SKai Ye static const struct debugfs_reg32 hzip_com_dfx_regs[] = {
3195bfabd50SKai Ye 	{"HZIP_CLOCK_GATE_CTRL           ",  0x301004},
3205bfabd50SKai Ye 	{"HZIP_CORE_INT_RAS_CE_ENB       ",  0x301160},
3215bfabd50SKai Ye 	{"HZIP_CORE_INT_RAS_NFE_ENB      ",  0x301164},
3225bfabd50SKai Ye 	{"HZIP_CORE_INT_RAS_FE_ENB       ",  0x301168},
3235bfabd50SKai Ye 	{"HZIP_UNCOM_ERR_RAS_CTRL        ",  0x30116C},
3245bfabd50SKai Ye };
3255bfabd50SKai Ye 
3265bfabd50SKai Ye static const struct debugfs_reg32 hzip_dump_dfx_regs[] = {
3275bfabd50SKai Ye 	{"HZIP_GET_BD_NUM                ",  0x00ull},
3285bfabd50SKai Ye 	{"HZIP_GET_RIGHT_BD              ",  0x04ull},
3295bfabd50SKai Ye 	{"HZIP_GET_ERROR_BD              ",  0x08ull},
3305bfabd50SKai Ye 	{"HZIP_DONE_BD_NUM               ",  0x0cull},
3315bfabd50SKai Ye 	{"HZIP_MAX_DELAY                 ",  0x20ull},
3325bfabd50SKai Ye };
3335bfabd50SKai Ye 
3349b0c97dfSKai Ye /* define the ZIP's dfx regs region and region length */
3359b0c97dfSKai Ye static struct dfx_diff_registers hzip_diff_regs[] = {
3369b0c97dfSKai Ye 	{
3379b0c97dfSKai Ye 		.reg_offset = HZIP_CORE_DFX_BASE,
3389b0c97dfSKai Ye 		.reg_len = HZIP_CORE_REGS_BASE_LEN,
3399b0c97dfSKai Ye 	}, {
3409b0c97dfSKai Ye 		.reg_offset = HZIP_CORE_DFX_COMP_0,
3419b0c97dfSKai Ye 		.reg_len = HZIP_CORE_REGS_DFX_LEN,
3429b0c97dfSKai Ye 	}, {
3439b0c97dfSKai Ye 		.reg_offset = HZIP_CORE_DFX_COMP_1,
3449b0c97dfSKai Ye 		.reg_len = HZIP_CORE_REGS_DFX_LEN,
3459b0c97dfSKai Ye 	}, {
3469b0c97dfSKai Ye 		.reg_offset = HZIP_CORE_DFX_DECOMP_0,
3479b0c97dfSKai Ye 		.reg_len = HZIP_CORE_REGS_DFX_LEN,
3489b0c97dfSKai Ye 	}, {
3499b0c97dfSKai Ye 		.reg_offset = HZIP_CORE_DFX_DECOMP_1,
3509b0c97dfSKai Ye 		.reg_len = HZIP_CORE_REGS_DFX_LEN,
3519b0c97dfSKai Ye 	}, {
3529b0c97dfSKai Ye 		.reg_offset = HZIP_CORE_DFX_DECOMP_2,
3539b0c97dfSKai Ye 		.reg_len = HZIP_CORE_REGS_DFX_LEN,
3549b0c97dfSKai Ye 	}, {
3559b0c97dfSKai Ye 		.reg_offset = HZIP_CORE_DFX_DECOMP_3,
3569b0c97dfSKai Ye 		.reg_len = HZIP_CORE_REGS_DFX_LEN,
3579b0c97dfSKai Ye 	}, {
3589b0c97dfSKai Ye 		.reg_offset = HZIP_CORE_DFX_DECOMP_4,
3599b0c97dfSKai Ye 		.reg_len = HZIP_CORE_REGS_DFX_LEN,
3609b0c97dfSKai Ye 	}, {
3619b0c97dfSKai Ye 		.reg_offset = HZIP_CORE_DFX_DECOMP_5,
3629b0c97dfSKai Ye 		.reg_len = HZIP_CORE_REGS_DFX_LEN,
3639b0c97dfSKai Ye 	},
3649b0c97dfSKai Ye };
3659b0c97dfSKai Ye 
hzip_diff_regs_show(struct seq_file * s,void * unused)3669b0c97dfSKai Ye static int hzip_diff_regs_show(struct seq_file *s, void *unused)
3679b0c97dfSKai Ye {
3689b0c97dfSKai Ye 	struct hisi_qm *qm = s->private;
3699b0c97dfSKai Ye 
3709b0c97dfSKai Ye 	hisi_qm_acc_diff_regs_dump(qm, s, qm->debug.acc_diff_regs,
3719b0c97dfSKai Ye 					ARRAY_SIZE(hzip_diff_regs));
3729b0c97dfSKai Ye 
3739b0c97dfSKai Ye 	return 0;
3749b0c97dfSKai Ye }
3759b0c97dfSKai Ye DEFINE_SHOW_ATTRIBUTE(hzip_diff_regs);
376b7a03a0fSChenghai Huang 
perf_mode_set(const char * val,const struct kernel_param * kp)377b7a03a0fSChenghai Huang static int perf_mode_set(const char *val, const struct kernel_param *kp)
378b7a03a0fSChenghai Huang {
379b7a03a0fSChenghai Huang 	int ret;
380b7a03a0fSChenghai Huang 	u32 n;
381b7a03a0fSChenghai Huang 
382b7a03a0fSChenghai Huang 	if (!val)
383b7a03a0fSChenghai Huang 		return -EINVAL;
384b7a03a0fSChenghai Huang 
385b7a03a0fSChenghai Huang 	ret = kstrtou32(val, 10, &n);
386b7a03a0fSChenghai Huang 	if (ret != 0 || (n != HZIP_HIGH_COMP_PERF &&
387b7a03a0fSChenghai Huang 			 n != HZIP_HIGH_COMP_RATE))
388b7a03a0fSChenghai Huang 		return -EINVAL;
389b7a03a0fSChenghai Huang 
390b7a03a0fSChenghai Huang 	return param_set_int(val, kp);
391b7a03a0fSChenghai Huang }
392b7a03a0fSChenghai Huang 
393b7a03a0fSChenghai Huang static const struct kernel_param_ops zip_com_perf_ops = {
394b7a03a0fSChenghai Huang 	.set = perf_mode_set,
395b7a03a0fSChenghai Huang 	.get = param_get_int,
396b7a03a0fSChenghai Huang };
397b7a03a0fSChenghai Huang 
398b7a03a0fSChenghai Huang /*
399b7a03a0fSChenghai Huang  * perf_mode = 0 means enable high compression rate mode,
400b7a03a0fSChenghai Huang  * perf_mode = 1 means enable high compression performance mode.
401b7a03a0fSChenghai Huang  * These two modes only apply to the compression direction.
402b7a03a0fSChenghai Huang  */
403b7a03a0fSChenghai Huang static u32 perf_mode = HZIP_HIGH_COMP_RATE;
404b7a03a0fSChenghai Huang module_param_cb(perf_mode, &zip_com_perf_ops, &perf_mode, 0444);
405b7a03a0fSChenghai Huang MODULE_PARM_DESC(perf_mode, "ZIP high perf mode 0(default), 1(enable)");
406b7a03a0fSChenghai Huang 
407f8408d2bSKai Ye static const struct kernel_param_ops zip_uacce_mode_ops = {
408f8408d2bSKai Ye 	.set = uacce_mode_set,
409f8408d2bSKai Ye 	.get = param_get_int,
410f8408d2bSKai Ye };
411f8408d2bSKai Ye 
412f8408d2bSKai Ye /*
413f8408d2bSKai Ye  * uacce_mode = 0 means zip only register to crypto,
414f8408d2bSKai Ye  * uacce_mode = 1 means zip both register to crypto and uacce.
415f8408d2bSKai Ye  */
416f8408d2bSKai Ye static u32 uacce_mode = UACCE_MODE_NOUACCE;
417f8408d2bSKai Ye module_param_cb(uacce_mode, &zip_uacce_mode_ops, &uacce_mode, 0444);
418f8408d2bSKai Ye MODULE_PARM_DESC(uacce_mode, UACCE_MODE_DESC);
419f8408d2bSKai Ye 
4204c79c7a4SLongfang Liu static bool pf_q_num_flag;
pf_q_num_set(const char * val,const struct kernel_param * kp)42162c455caSZhou Wang static int pf_q_num_set(const char *val, const struct kernel_param *kp)
42262c455caSZhou Wang {
4234c79c7a4SLongfang Liu 	pf_q_num_flag = true;
4244c79c7a4SLongfang Liu 
425fae74feaSShameer Kolothum 	return q_num_set(val, kp, PCI_DEVICE_ID_HUAWEI_ZIP_PF);
42662c455caSZhou Wang }
42762c455caSZhou Wang 
42862c455caSZhou Wang static const struct kernel_param_ops pf_q_num_ops = {
42962c455caSZhou Wang 	.set = pf_q_num_set,
43062c455caSZhou Wang 	.get = param_get_int,
43162c455caSZhou Wang };
43262c455caSZhou Wang 
43362c455caSZhou Wang static u32 pf_q_num = HZIP_PF_DEF_Q_NUM;
43462c455caSZhou Wang module_param_cb(pf_q_num, &pf_q_num_ops, &pf_q_num, 0444);
4350542a941SLongfang Liu MODULE_PARM_DESC(pf_q_num, "Number of queues in PF(v1 2-4096, v2 2-1024)");
43662c455caSZhou Wang 
43735ee280fSHao Fang static const struct kernel_param_ops vfs_num_ops = {
43835ee280fSHao Fang 	.set = vfs_num_set,
43935ee280fSHao Fang 	.get = param_get_int,
44035ee280fSHao Fang };
44135ee280fSHao Fang 
44239977f4bSHao Fang static u32 vfs_num;
44335ee280fSHao Fang module_param_cb(vfs_num, &vfs_num_ops, &vfs_num, 0444);
44435ee280fSHao Fang MODULE_PARM_DESC(vfs_num, "Number of VFs to enable(1-63), 0(default)");
44539977f4bSHao Fang 
44662c455caSZhou Wang static const struct pci_device_id hisi_zip_dev_ids[] = {
447fae74feaSShameer Kolothum 	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_HUAWEI_ZIP_PF) },
448fae74feaSShameer Kolothum 	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_HUAWEI_ZIP_VF) },
44962c455caSZhou Wang 	{ 0, }
45062c455caSZhou Wang };
45162c455caSZhou Wang MODULE_DEVICE_TABLE(pci, hisi_zip_dev_ids);
45262c455caSZhou Wang 
zip_create_qps(struct hisi_qp ** qps,int qp_num,int node)453813ec3f1SBarry Song int zip_create_qps(struct hisi_qp **qps, int qp_num, int node)
45462c455caSZhou Wang {
455813ec3f1SBarry Song 	if (node == NUMA_NO_NODE)
456813ec3f1SBarry Song 		node = cpu_to_node(smp_processor_id());
45762c455caSZhou Wang 
45818f1ab3fSShukun Tan 	return hisi_qm_alloc_qps_node(&zip_devices, qp_num, 0, node, qps);
45962c455caSZhou Wang }
46062c455caSZhou Wang 
hisi_zip_alg_support(struct hisi_qm * qm,u32 alg)461db700974SWeili Qian bool hisi_zip_alg_support(struct hisi_qm *qm, u32 alg)
462db700974SWeili Qian {
463db700974SWeili Qian 	u32 cap_val;
464db700974SWeili Qian 
46552f0b4a3SZhiqi Song 	cap_val = qm->cap_tables.dev_cap_table[ZIP_DRV_ALG_BITMAP_IDX].cap_val;
466db700974SWeili Qian 	if ((alg & cap_val) == alg)
467db700974SWeili Qian 		return true;
468db700974SWeili Qian 
469db700974SWeili Qian 	return false;
470db700974SWeili Qian }
471db700974SWeili Qian 
hisi_zip_set_high_perf(struct hisi_qm * qm)472b7a03a0fSChenghai Huang static int hisi_zip_set_high_perf(struct hisi_qm *qm)
473b7a03a0fSChenghai Huang {
474b7a03a0fSChenghai Huang 	u32 val;
475b7a03a0fSChenghai Huang 	int ret;
476b7a03a0fSChenghai Huang 
477b7a03a0fSChenghai Huang 	val = readl_relaxed(qm->io_base + HZIP_HIGH_PERF_OFFSET);
478b7a03a0fSChenghai Huang 	if (perf_mode == HZIP_HIGH_COMP_PERF)
479b7a03a0fSChenghai Huang 		val |= HZIP_HIGH_COMP_PERF;
480b7a03a0fSChenghai Huang 	else
481b7a03a0fSChenghai Huang 		val &= ~HZIP_HIGH_COMP_PERF;
482b7a03a0fSChenghai Huang 
483b7a03a0fSChenghai Huang 	/* Set perf mode */
484b7a03a0fSChenghai Huang 	writel(val, qm->io_base + HZIP_HIGH_PERF_OFFSET);
485b7a03a0fSChenghai Huang 	ret = readl_relaxed_poll_timeout(qm->io_base + HZIP_HIGH_PERF_OFFSET,
486b7a03a0fSChenghai Huang 					 val, val == perf_mode, HZIP_DELAY_1_US,
487b7a03a0fSChenghai Huang 					 HZIP_POLL_TIMEOUT_US);
488b7a03a0fSChenghai Huang 	if (ret)
489b7a03a0fSChenghai Huang 		pci_err(qm->pdev, "failed to set perf mode\n");
490b7a03a0fSChenghai Huang 
491b7a03a0fSChenghai Huang 	return ret;
492b7a03a0fSChenghai Huang }
493b7a03a0fSChenghai Huang 
hisi_zip_open_sva_prefetch(struct hisi_qm * qm)494a5c164b1SLongfang Liu static void hisi_zip_open_sva_prefetch(struct hisi_qm *qm)
495a5c164b1SLongfang Liu {
496a5c164b1SLongfang Liu 	u32 val;
497a5c164b1SLongfang Liu 	int ret;
498a5c164b1SLongfang Liu 
49982f00b24SWeili Qian 	if (!test_bit(QM_SUPPORT_SVA_PREFETCH, &qm->caps))
500a5c164b1SLongfang Liu 		return;
501a5c164b1SLongfang Liu 
502a5c164b1SLongfang Liu 	/* Enable prefetch */
503a5c164b1SLongfang Liu 	val = readl_relaxed(qm->io_base + HZIP_PREFETCH_CFG);
504a5c164b1SLongfang Liu 	val &= HZIP_PREFETCH_ENABLE;
505a5c164b1SLongfang Liu 	writel(val, qm->io_base + HZIP_PREFETCH_CFG);
506a5c164b1SLongfang Liu 
507a5c164b1SLongfang Liu 	ret = readl_relaxed_poll_timeout(qm->io_base + HZIP_PREFETCH_CFG,
508a5c164b1SLongfang Liu 					 val, !(val & HZIP_SVA_PREFETCH_DISABLE),
509a5c164b1SLongfang Liu 					 HZIP_DELAY_1_US, HZIP_POLL_TIMEOUT_US);
510a5c164b1SLongfang Liu 	if (ret)
511a5c164b1SLongfang Liu 		pci_err(qm->pdev, "failed to open sva prefetch\n");
512a5c164b1SLongfang Liu }
513a5c164b1SLongfang Liu 
hisi_zip_close_sva_prefetch(struct hisi_qm * qm)514a5c164b1SLongfang Liu static void hisi_zip_close_sva_prefetch(struct hisi_qm *qm)
515a5c164b1SLongfang Liu {
516a5c164b1SLongfang Liu 	u32 val;
517a5c164b1SLongfang Liu 	int ret;
518a5c164b1SLongfang Liu 
51982f00b24SWeili Qian 	if (!test_bit(QM_SUPPORT_SVA_PREFETCH, &qm->caps))
520a5c164b1SLongfang Liu 		return;
521a5c164b1SLongfang Liu 
522a5c164b1SLongfang Liu 	val = readl_relaxed(qm->io_base + HZIP_PREFETCH_CFG);
523a5c164b1SLongfang Liu 	val |= HZIP_SVA_PREFETCH_DISABLE;
524a5c164b1SLongfang Liu 	writel(val, qm->io_base + HZIP_PREFETCH_CFG);
525a5c164b1SLongfang Liu 
526a5c164b1SLongfang Liu 	ret = readl_relaxed_poll_timeout(qm->io_base + HZIP_SVA_TRANS,
527a5c164b1SLongfang Liu 					 val, !(val & HZIP_SVA_DISABLE_READY),
528a5c164b1SLongfang Liu 					 HZIP_DELAY_1_US, HZIP_POLL_TIMEOUT_US);
529a5c164b1SLongfang Liu 	if (ret)
530a5c164b1SLongfang Liu 		pci_err(qm->pdev, "failed to close sva prefetch\n");
531a5c164b1SLongfang Liu }
532a5c164b1SLongfang Liu 
hisi_zip_enable_clock_gate(struct hisi_qm * qm)533ed5fa39fSWeili Qian static void hisi_zip_enable_clock_gate(struct hisi_qm *qm)
534ed5fa39fSWeili Qian {
535ed5fa39fSWeili Qian 	u32 val;
536ed5fa39fSWeili Qian 
537ed5fa39fSWeili Qian 	if (qm->ver < QM_HW_V3)
538ed5fa39fSWeili Qian 		return;
539ed5fa39fSWeili Qian 
540ed5fa39fSWeili Qian 	val = readl(qm->io_base + HZIP_CLOCK_GATE_CTRL);
541ed5fa39fSWeili Qian 	val |= HZIP_CLOCK_GATED_EN;
542ed5fa39fSWeili Qian 	writel(val, qm->io_base + HZIP_CLOCK_GATE_CTRL);
543ed5fa39fSWeili Qian 
544ed5fa39fSWeili Qian 	val = readl(qm->io_base + HZIP_PEH_CFG_AUTO_GATE);
545ed5fa39fSWeili Qian 	val |= HZIP_PEH_CFG_AUTO_GATE_EN;
546ed5fa39fSWeili Qian 	writel(val, qm->io_base + HZIP_PEH_CFG_AUTO_GATE);
547ed5fa39fSWeili Qian }
548ed5fa39fSWeili Qian 
hisi_zip_set_user_domain_and_cache(struct hisi_qm * qm)54984c9b780SShukun Tan static int hisi_zip_set_user_domain_and_cache(struct hisi_qm *qm)
55062c455caSZhou Wang {
55184c9b780SShukun Tan 	void __iomem *base = qm->io_base;
552db700974SWeili Qian 	u32 dcomp_bm, comp_bm;
55362c455caSZhou Wang 
55462c455caSZhou Wang 	/* qm user domain */
55562c455caSZhou Wang 	writel(AXUSER_BASE, base + QM_ARUSER_M_CFG_1);
55662c455caSZhou Wang 	writel(ARUSER_M_CFG_ENABLE, base + QM_ARUSER_M_CFG_ENABLE);
55762c455caSZhou Wang 	writel(AXUSER_BASE, base + QM_AWUSER_M_CFG_1);
55862c455caSZhou Wang 	writel(AWUSER_M_CFG_ENABLE, base + QM_AWUSER_M_CFG_ENABLE);
55962c455caSZhou Wang 	writel(WUSER_M_CFG_ENABLE, base + QM_WUSER_M_CFG_ENABLE);
56062c455caSZhou Wang 
56162c455caSZhou Wang 	/* qm cache */
56262c455caSZhou Wang 	writel(AXI_M_CFG, base + QM_AXI_M_CFG);
56362c455caSZhou Wang 	writel(AXI_M_CFG_ENABLE, base + QM_AXI_M_CFG_ENABLE);
5642ca73193SYang Shen 
56562c455caSZhou Wang 	/* disable FLR triggered by BME(bus master enable) */
56662c455caSZhou Wang 	writel(PEH_AXUSER_CFG, base + QM_PEH_AXUSER_CFG);
56762c455caSZhou Wang 	writel(PEH_AXUSER_CFG_ENABLE, base + QM_PEH_AXUSER_CFG_ENABLE);
56862c455caSZhou Wang 
56962c455caSZhou Wang 	/* cache */
57015b0694fSYang Shen 	writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_0);
57115b0694fSYang Shen 	writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_1);
57215b0694fSYang Shen 	writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_0);
57315b0694fSYang Shen 	writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_1);
57462c455caSZhou Wang 
57562c455caSZhou Wang 	/* user domain configurations */
57662c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_BD_RUSER_32_63);
57762c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_BD_WUSER_32_63);
5789e00df71SZhangfei Gao 
579cc3292d1SWeili Qian 	if (qm->use_sva && qm->ver == QM_HW_V2) {
5809e00df71SZhangfei Gao 		writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_DATA_RUSER_32_63);
5819e00df71SZhangfei Gao 		writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_DATA_WUSER_32_63);
582808957baSYang Shen 		writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_SGL_RUSER_32_63);
5839e00df71SZhangfei Gao 	} else {
58462c455caSZhou Wang 		writel(AXUSER_BASE, base + HZIP_DATA_RUSER_32_63);
58562c455caSZhou Wang 		writel(AXUSER_BASE, base + HZIP_DATA_WUSER_32_63);
586808957baSYang Shen 		writel(AXUSER_BASE, base + HZIP_SGL_RUSER_32_63);
5879e00df71SZhangfei Gao 	}
58862c455caSZhou Wang 
58962c455caSZhou Wang 	/* let's open all compression/decompression cores */
59052f0b4a3SZhiqi Song 	dcomp_bm = qm->cap_tables.dev_cap_table[ZIP_DECOMP_ENABLE_BITMAP_IDX].cap_val;
59152f0b4a3SZhiqi Song 	comp_bm = qm->cap_tables.dev_cap_table[ZIP_COMP_ENABLE_BITMAP_IDX].cap_val;
592db700974SWeili Qian 	writel(HZIP_DECOMP_CHECK_ENABLE | dcomp_bm | comp_bm, base + HZIP_CLOCK_GATE_CTRL);
59362c455caSZhou Wang 
5942a928693SYang Shen 	/* enable sqc,cqc writeback */
59562c455caSZhou Wang 	writel(SQC_CACHE_ENABLE | CQC_CACHE_ENABLE | SQC_CACHE_WB_ENABLE |
59662c455caSZhou Wang 	       CQC_CACHE_WB_ENABLE | FIELD_PREP(SQC_CACHE_WB_THRD, 1) |
59762c455caSZhou Wang 	       FIELD_PREP(CQC_CACHE_WB_THRD, 1), base + QM_CACHE_CTL);
59884c9b780SShukun Tan 
599ed5fa39fSWeili Qian 	hisi_zip_enable_clock_gate(qm);
600ed5fa39fSWeili Qian 
60184c9b780SShukun Tan 	return 0;
60262c455caSZhou Wang }
60362c455caSZhou Wang 
hisi_zip_master_ooo_ctrl(struct hisi_qm * qm,bool enable)604b7da13d0SWeili Qian static void hisi_zip_master_ooo_ctrl(struct hisi_qm *qm, bool enable)
605b7da13d0SWeili Qian {
606b7da13d0SWeili Qian 	u32 val1, val2;
607b7da13d0SWeili Qian 
608b7da13d0SWeili Qian 	val1 = readl(qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
609b7da13d0SWeili Qian 	if (enable) {
610b7da13d0SWeili Qian 		val1 |= HZIP_AXI_SHUTDOWN_ENABLE;
611d90fab0dSWeili Qian 		val2 = hisi_qm_get_hw_info(qm, zip_basic_cap_info,
612d90fab0dSWeili Qian 				ZIP_OOO_SHUTDOWN_MASK_CAP, qm->cap_ver);
613b7da13d0SWeili Qian 	} else {
614b7da13d0SWeili Qian 		val1 &= ~HZIP_AXI_SHUTDOWN_ENABLE;
615b7da13d0SWeili Qian 		val2 = 0x0;
616b7da13d0SWeili Qian 	}
617b7da13d0SWeili Qian 
618b7da13d0SWeili Qian 	if (qm->ver > QM_HW_V2)
619b7da13d0SWeili Qian 		writel(val2, qm->io_base + HZIP_OOO_SHUTDOWN_SEL);
620b7da13d0SWeili Qian 
621b7da13d0SWeili Qian 	writel(val1, qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
622b7da13d0SWeili Qian }
623b7da13d0SWeili Qian 
hisi_zip_hw_error_enable(struct hisi_qm * qm)624eaebf4c3SShukun Tan static void hisi_zip_hw_error_enable(struct hisi_qm *qm)
62562c455caSZhou Wang {
626d90fab0dSWeili Qian 	u32 nfe, ce;
627d90fab0dSWeili Qian 
62862c455caSZhou Wang 	if (qm->ver == QM_HW_V1) {
629eaebf4c3SShukun Tan 		writel(HZIP_CORE_INT_MASK_ALL,
630eaebf4c3SShukun Tan 		       qm->io_base + HZIP_CORE_INT_MASK_REG);
631ee1788c6SZhou Wang 		dev_info(&qm->pdev->dev, "Does not support hw error handle\n");
63262c455caSZhou Wang 		return;
63362c455caSZhou Wang 	}
63462c455caSZhou Wang 
635d90fab0dSWeili Qian 	nfe = hisi_qm_get_hw_info(qm, zip_basic_cap_info, ZIP_NFE_MASK_CAP, qm->cap_ver);
636d90fab0dSWeili Qian 	ce = hisi_qm_get_hw_info(qm, zip_basic_cap_info, ZIP_CE_MASK_CAP, qm->cap_ver);
637d90fab0dSWeili Qian 
63862c455caSZhou Wang 	/* clear ZIP hw error source if having */
639d90fab0dSWeili Qian 	writel(ce | nfe | HZIP_CORE_INT_RAS_FE_ENB_MASK, qm->io_base + HZIP_CORE_INT_SOURCE);
640eaebf4c3SShukun Tan 
641de3daf4bSShukun Tan 	/* configure error type */
642d90fab0dSWeili Qian 	writel(ce, qm->io_base + HZIP_CORE_INT_RAS_CE_ENB);
643d90fab0dSWeili Qian 	writel(HZIP_CORE_INT_RAS_FE_ENB_MASK, qm->io_base + HZIP_CORE_INT_RAS_FE_ENB);
644d90fab0dSWeili Qian 	writel(nfe, qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB);
645de3daf4bSShukun Tan 
646b7da13d0SWeili Qian 	hisi_zip_master_ooo_ctrl(qm, true);
6473b9c24deSWeili Qian 
6483b9c24deSWeili Qian 	/* enable ZIP hw error interrupts */
6493b9c24deSWeili Qian 	writel(0, qm->io_base + HZIP_CORE_INT_MASK_REG);
65062c455caSZhou Wang }
651eaebf4c3SShukun Tan 
hisi_zip_hw_error_disable(struct hisi_qm * qm)652eaebf4c3SShukun Tan static void hisi_zip_hw_error_disable(struct hisi_qm *qm)
653eaebf4c3SShukun Tan {
654d90fab0dSWeili Qian 	u32 nfe, ce;
6557ce396faSShukun Tan 
656d90fab0dSWeili Qian 	/* disable ZIP hw error interrupts */
657d90fab0dSWeili Qian 	nfe = hisi_qm_get_hw_info(qm, zip_basic_cap_info, ZIP_NFE_MASK_CAP, qm->cap_ver);
658d90fab0dSWeili Qian 	ce = hisi_qm_get_hw_info(qm, zip_basic_cap_info, ZIP_CE_MASK_CAP, qm->cap_ver);
659d90fab0dSWeili Qian 	writel(ce | nfe | HZIP_CORE_INT_RAS_FE_ENB_MASK, qm->io_base + HZIP_CORE_INT_MASK_REG);
660d90fab0dSWeili Qian 
661b7da13d0SWeili Qian 	hisi_zip_master_ooo_ctrl(qm, false);
66262c455caSZhou Wang }
66362c455caSZhou Wang 
file_to_qm(struct ctrl_debug_file * file)66472c7a68dSZhou Wang static inline struct hisi_qm *file_to_qm(struct ctrl_debug_file *file)
66572c7a68dSZhou Wang {
66672c7a68dSZhou Wang 	struct hisi_zip *hisi_zip = file->ctrl->hisi_zip;
66772c7a68dSZhou Wang 
66872c7a68dSZhou Wang 	return &hisi_zip->qm;
66972c7a68dSZhou Wang }
67072c7a68dSZhou Wang 
clear_enable_read(struct hisi_qm * qm)67174f5edbfSWeili Qian static u32 clear_enable_read(struct hisi_qm *qm)
67272c7a68dSZhou Wang {
67372c7a68dSZhou Wang 	return readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) &
67415b0694fSYang Shen 		     HZIP_SOFT_CTRL_CNT_CLR_CE_BIT;
67572c7a68dSZhou Wang }
67672c7a68dSZhou Wang 
clear_enable_write(struct hisi_qm * qm,u32 val)67774f5edbfSWeili Qian static int clear_enable_write(struct hisi_qm *qm, u32 val)
67872c7a68dSZhou Wang {
67972c7a68dSZhou Wang 	u32 tmp;
68072c7a68dSZhou Wang 
68172c7a68dSZhou Wang 	if (val != 1 && val != 0)
68272c7a68dSZhou Wang 		return -EINVAL;
68372c7a68dSZhou Wang 
68472c7a68dSZhou Wang 	tmp = (readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) &
68515b0694fSYang Shen 	       ~HZIP_SOFT_CTRL_CNT_CLR_CE_BIT) | val;
68672c7a68dSZhou Wang 	writel(tmp, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
68772c7a68dSZhou Wang 
68872c7a68dSZhou Wang 	return  0;
68972c7a68dSZhou Wang }
69072c7a68dSZhou Wang 
hisi_zip_ctrl_debug_read(struct file * filp,char __user * buf,size_t count,loff_t * pos)69115b0694fSYang Shen static ssize_t hisi_zip_ctrl_debug_read(struct file *filp, char __user *buf,
69272c7a68dSZhou Wang 					size_t count, loff_t *pos)
69372c7a68dSZhou Wang {
69472c7a68dSZhou Wang 	struct ctrl_debug_file *file = filp->private_data;
695607c191bSWeili Qian 	struct hisi_qm *qm = file_to_qm(file);
69672c7a68dSZhou Wang 	char tbuf[HZIP_BUF_SIZE];
69772c7a68dSZhou Wang 	u32 val;
69872c7a68dSZhou Wang 	int ret;
69972c7a68dSZhou Wang 
700607c191bSWeili Qian 	ret = hisi_qm_get_dfx_access(qm);
701607c191bSWeili Qian 	if (ret)
702607c191bSWeili Qian 		return ret;
703607c191bSWeili Qian 
70472c7a68dSZhou Wang 	spin_lock_irq(&file->lock);
70572c7a68dSZhou Wang 	switch (file->index) {
70672c7a68dSZhou Wang 	case HZIP_CLEAR_ENABLE:
70774f5edbfSWeili Qian 		val = clear_enable_read(qm);
70872c7a68dSZhou Wang 		break;
70972c7a68dSZhou Wang 	default:
710607c191bSWeili Qian 		goto err_input;
71172c7a68dSZhou Wang 	}
71272c7a68dSZhou Wang 	spin_unlock_irq(&file->lock);
713607c191bSWeili Qian 
714607c191bSWeili Qian 	hisi_qm_put_dfx_access(qm);
715533b2079SYang Shen 	ret = scnprintf(tbuf, sizeof(tbuf), "%u\n", val);
71672c7a68dSZhou Wang 	return simple_read_from_buffer(buf, count, pos, tbuf, ret);
717607c191bSWeili Qian 
718607c191bSWeili Qian err_input:
719607c191bSWeili Qian 	spin_unlock_irq(&file->lock);
720607c191bSWeili Qian 	hisi_qm_put_dfx_access(qm);
721607c191bSWeili Qian 	return -EINVAL;
72272c7a68dSZhou Wang }
72372c7a68dSZhou Wang 
hisi_zip_ctrl_debug_write(struct file * filp,const char __user * buf,size_t count,loff_t * pos)72415b0694fSYang Shen static ssize_t hisi_zip_ctrl_debug_write(struct file *filp,
72515b0694fSYang Shen 					 const char __user *buf,
72672c7a68dSZhou Wang 					 size_t count, loff_t *pos)
72772c7a68dSZhou Wang {
72872c7a68dSZhou Wang 	struct ctrl_debug_file *file = filp->private_data;
729607c191bSWeili Qian 	struct hisi_qm *qm = file_to_qm(file);
73072c7a68dSZhou Wang 	char tbuf[HZIP_BUF_SIZE];
73172c7a68dSZhou Wang 	unsigned long val;
73272c7a68dSZhou Wang 	int len, ret;
73372c7a68dSZhou Wang 
73472c7a68dSZhou Wang 	if (*pos != 0)
73572c7a68dSZhou Wang 		return 0;
73672c7a68dSZhou Wang 
73772c7a68dSZhou Wang 	if (count >= HZIP_BUF_SIZE)
73872c7a68dSZhou Wang 		return -ENOSPC;
73972c7a68dSZhou Wang 
74072c7a68dSZhou Wang 	len = simple_write_to_buffer(tbuf, HZIP_BUF_SIZE - 1, pos, buf, count);
74172c7a68dSZhou Wang 	if (len < 0)
74272c7a68dSZhou Wang 		return len;
74372c7a68dSZhou Wang 
74472c7a68dSZhou Wang 	tbuf[len] = '\0';
7456d9a8995SYang Shen 	ret = kstrtoul(tbuf, 0, &val);
7466d9a8995SYang Shen 	if (ret)
7476d9a8995SYang Shen 		return ret;
74872c7a68dSZhou Wang 
749607c191bSWeili Qian 	ret = hisi_qm_get_dfx_access(qm);
750607c191bSWeili Qian 	if (ret)
751607c191bSWeili Qian 		return ret;
752607c191bSWeili Qian 
75372c7a68dSZhou Wang 	spin_lock_irq(&file->lock);
75472c7a68dSZhou Wang 	switch (file->index) {
75572c7a68dSZhou Wang 	case HZIP_CLEAR_ENABLE:
75674f5edbfSWeili Qian 		ret = clear_enable_write(qm, val);
75772c7a68dSZhou Wang 		if (ret)
75872c7a68dSZhou Wang 			goto err_input;
75972c7a68dSZhou Wang 		break;
76072c7a68dSZhou Wang 	default:
76172c7a68dSZhou Wang 		ret = -EINVAL;
76272c7a68dSZhou Wang 		goto err_input;
76372c7a68dSZhou Wang 	}
76472c7a68dSZhou Wang 
765607c191bSWeili Qian 	ret = count;
76672c7a68dSZhou Wang 
76772c7a68dSZhou Wang err_input:
76872c7a68dSZhou Wang 	spin_unlock_irq(&file->lock);
769607c191bSWeili Qian 	hisi_qm_put_dfx_access(qm);
77072c7a68dSZhou Wang 	return ret;
77172c7a68dSZhou Wang }
77272c7a68dSZhou Wang 
77372c7a68dSZhou Wang static const struct file_operations ctrl_debug_fops = {
77472c7a68dSZhou Wang 	.owner = THIS_MODULE,
77572c7a68dSZhou Wang 	.open = simple_open,
77615b0694fSYang Shen 	.read = hisi_zip_ctrl_debug_read,
77715b0694fSYang Shen 	.write = hisi_zip_ctrl_debug_write,
77872c7a68dSZhou Wang };
77972c7a68dSZhou Wang 
zip_debugfs_atomic64_set(void * data,u64 val)7806621e649SLongfang Liu static int zip_debugfs_atomic64_set(void *data, u64 val)
7816621e649SLongfang Liu {
7826621e649SLongfang Liu 	if (val)
7836621e649SLongfang Liu 		return -EINVAL;
7846621e649SLongfang Liu 
7856621e649SLongfang Liu 	atomic64_set((atomic64_t *)data, 0);
7866621e649SLongfang Liu 
7876621e649SLongfang Liu 	return 0;
7886621e649SLongfang Liu }
7896621e649SLongfang Liu 
zip_debugfs_atomic64_get(void * data,u64 * val)7906621e649SLongfang Liu static int zip_debugfs_atomic64_get(void *data, u64 *val)
7916621e649SLongfang Liu {
7926621e649SLongfang Liu 	*val = atomic64_read((atomic64_t *)data);
7936621e649SLongfang Liu 
7946621e649SLongfang Liu 	return 0;
7956621e649SLongfang Liu }
7966621e649SLongfang Liu 
7976621e649SLongfang Liu DEFINE_DEBUGFS_ATTRIBUTE(zip_atomic64_ops, zip_debugfs_atomic64_get,
7986621e649SLongfang Liu 			 zip_debugfs_atomic64_set, "%llu\n");
7996621e649SLongfang Liu 
hisi_zip_regs_show(struct seq_file * s,void * unused)8001295292dSWeili Qian static int hisi_zip_regs_show(struct seq_file *s, void *unused)
8011295292dSWeili Qian {
8021295292dSWeili Qian 	hisi_qm_regs_dump(s, s->private);
8031295292dSWeili Qian 
8041295292dSWeili Qian 	return 0;
8051295292dSWeili Qian }
8061295292dSWeili Qian 
8071295292dSWeili Qian DEFINE_SHOW_ATTRIBUTE(hisi_zip_regs);
8081295292dSWeili Qian 
hisi_zip_core_debug_init(struct hisi_qm * qm)8094b33f057SShukun Tan static int hisi_zip_core_debug_init(struct hisi_qm *qm)
81072c7a68dSZhou Wang {
811db700974SWeili Qian 	u32 zip_core_num, zip_comp_core_num;
81272c7a68dSZhou Wang 	struct device *dev = &qm->pdev->dev;
81372c7a68dSZhou Wang 	struct debugfs_regset32 *regset;
8144a97bfc7SGreg Kroah-Hartman 	struct dentry *tmp_d;
81572c7a68dSZhou Wang 	char buf[HZIP_BUF_SIZE];
81672c7a68dSZhou Wang 	int i;
81772c7a68dSZhou Wang 
81852f0b4a3SZhiqi Song 	zip_core_num = qm->cap_tables.dev_cap_table[ZIP_CORE_NUM_CAP_IDX].cap_val;
81952f0b4a3SZhiqi Song 	zip_comp_core_num = qm->cap_tables.dev_cap_table[ZIP_CLUSTER_COMP_NUM_CAP_IDX].cap_val;
820db700974SWeili Qian 
821db700974SWeili Qian 	for (i = 0; i < zip_core_num; i++) {
822db700974SWeili Qian 		if (i < zip_comp_core_num)
823533b2079SYang Shen 			scnprintf(buf, sizeof(buf), "comp_core%d", i);
82472c7a68dSZhou Wang 		else
825533b2079SYang Shen 			scnprintf(buf, sizeof(buf), "decomp_core%d",
826db700974SWeili Qian 				  i - zip_comp_core_num);
82772c7a68dSZhou Wang 
82872c7a68dSZhou Wang 		regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
82972c7a68dSZhou Wang 		if (!regset)
83072c7a68dSZhou Wang 			return -ENOENT;
83172c7a68dSZhou Wang 
83272c7a68dSZhou Wang 		regset->regs = hzip_dfx_regs;
83372c7a68dSZhou Wang 		regset->nregs = ARRAY_SIZE(hzip_dfx_regs);
83472c7a68dSZhou Wang 		regset->base = qm->io_base + core_offsets[i];
835607c191bSWeili Qian 		regset->dev = dev;
83672c7a68dSZhou Wang 
8374b33f057SShukun Tan 		tmp_d = debugfs_create_dir(buf, qm->debug.debug_root);
8381295292dSWeili Qian 		debugfs_create_file("regs", 0444, tmp_d, regset,
8391295292dSWeili Qian 				    &hisi_zip_regs_fops);
84072c7a68dSZhou Wang 	}
84172c7a68dSZhou Wang 
84272c7a68dSZhou Wang 	return 0;
84372c7a68dSZhou Wang }
84472c7a68dSZhou Wang 
hisi_zip_dfx_debug_init(struct hisi_qm * qm)8456621e649SLongfang Liu static void hisi_zip_dfx_debug_init(struct hisi_qm *qm)
8466621e649SLongfang Liu {
8479b0c97dfSKai Ye 	struct dfx_diff_registers *hzip_regs = qm->debug.acc_diff_regs;
8486621e649SLongfang Liu 	struct hisi_zip *zip = container_of(qm, struct hisi_zip, qm);
8496621e649SLongfang Liu 	struct hisi_zip_dfx *dfx = &zip->dfx;
8506621e649SLongfang Liu 	struct dentry *tmp_dir;
8516621e649SLongfang Liu 	void *data;
8526621e649SLongfang Liu 	int i;
8536621e649SLongfang Liu 
8546621e649SLongfang Liu 	tmp_dir = debugfs_create_dir("zip_dfx", qm->debug.debug_root);
8556621e649SLongfang Liu 	for (i = 0; i < ARRAY_SIZE(zip_dfx_files); i++) {
8566621e649SLongfang Liu 		data = (atomic64_t *)((uintptr_t)dfx + zip_dfx_files[i].offset);
8576621e649SLongfang Liu 		debugfs_create_file(zip_dfx_files[i].name,
8584b33f057SShukun Tan 				    0644, tmp_dir, data,
8596621e649SLongfang Liu 				    &zip_atomic64_ops);
8606621e649SLongfang Liu 	}
8619b0c97dfSKai Ye 
8629b0c97dfSKai Ye 	if (qm->fun_type == QM_HW_PF && hzip_regs)
8639b0c97dfSKai Ye 		debugfs_create_file("diff_regs", 0444, tmp_dir,
8649b0c97dfSKai Ye 				      qm, &hzip_diff_regs_fops);
8656621e649SLongfang Liu }
8666621e649SLongfang Liu 
hisi_zip_ctrl_debug_init(struct hisi_qm * qm)8674b33f057SShukun Tan static int hisi_zip_ctrl_debug_init(struct hisi_qm *qm)
86872c7a68dSZhou Wang {
8694b33f057SShukun Tan 	struct hisi_zip *zip = container_of(qm, struct hisi_zip, qm);
87072c7a68dSZhou Wang 	int i;
87172c7a68dSZhou Wang 
872c4392b46SWeili Qian 	for (i = HZIP_CLEAR_ENABLE; i < HZIP_DEBUG_FILE_NUM; i++) {
8734b33f057SShukun Tan 		spin_lock_init(&zip->ctrl->files[i].lock);
8744b33f057SShukun Tan 		zip->ctrl->files[i].ctrl = zip->ctrl;
8754b33f057SShukun Tan 		zip->ctrl->files[i].index = i;
87672c7a68dSZhou Wang 
8774a97bfc7SGreg Kroah-Hartman 		debugfs_create_file(ctrl_debug_file_name[i], 0600,
8784b33f057SShukun Tan 				    qm->debug.debug_root,
8794b33f057SShukun Tan 				    zip->ctrl->files + i,
88072c7a68dSZhou Wang 				    &ctrl_debug_fops);
88172c7a68dSZhou Wang 	}
88272c7a68dSZhou Wang 
8834b33f057SShukun Tan 	return hisi_zip_core_debug_init(qm);
88472c7a68dSZhou Wang }
88572c7a68dSZhou Wang 
hisi_zip_debugfs_init(struct hisi_qm * qm)8864b33f057SShukun Tan static int hisi_zip_debugfs_init(struct hisi_qm *qm)
88772c7a68dSZhou Wang {
88872c7a68dSZhou Wang 	struct device *dev = &qm->pdev->dev;
88972c7a68dSZhou Wang 	struct dentry *dev_d;
89072c7a68dSZhou Wang 	int ret;
89172c7a68dSZhou Wang 
89272c7a68dSZhou Wang 	dev_d = debugfs_create_dir(dev_name(dev), hzip_debugfs_root);
89372c7a68dSZhou Wang 
894c31dc9feSShukun Tan 	qm->debug.sqe_mask_offset = HZIP_SQE_MASK_OFFSET;
895c31dc9feSShukun Tan 	qm->debug.sqe_mask_len = HZIP_SQE_MASK_LEN;
89672c7a68dSZhou Wang 	qm->debug.debug_root = dev_d;
897b40b62edSKai Ye 	ret = hisi_qm_regs_debugfs_init(qm, hzip_diff_regs, ARRAY_SIZE(hzip_diff_regs));
8989b0c97dfSKai Ye 	if (ret) {
8999b0c97dfSKai Ye 		dev_warn(dev, "Failed to init ZIP diff regs!\n");
9009b0c97dfSKai Ye 		goto debugfs_remove;
9019b0c97dfSKai Ye 	}
9029b0c97dfSKai Ye 
903a8ff38bdSWeili Qian 	hisi_qm_debug_init(qm);
90472c7a68dSZhou Wang 
90572c7a68dSZhou Wang 	if (qm->fun_type == QM_HW_PF) {
9064b33f057SShukun Tan 		ret = hisi_zip_ctrl_debug_init(qm);
90772c7a68dSZhou Wang 		if (ret)
90872c7a68dSZhou Wang 			goto failed_to_create;
90972c7a68dSZhou Wang 	}
91072c7a68dSZhou Wang 
9116621e649SLongfang Liu 	hisi_zip_dfx_debug_init(qm);
9126621e649SLongfang Liu 
91372c7a68dSZhou Wang 	return 0;
91472c7a68dSZhou Wang 
91572c7a68dSZhou Wang failed_to_create:
916b40b62edSKai Ye 	hisi_qm_regs_debugfs_uninit(qm, ARRAY_SIZE(hzip_diff_regs));
9179b0c97dfSKai Ye debugfs_remove:
91872c7a68dSZhou Wang 	debugfs_remove_recursive(hzip_debugfs_root);
91972c7a68dSZhou Wang 	return ret;
92072c7a68dSZhou Wang }
92172c7a68dSZhou Wang 
922698f9523SHao Fang /* hisi_zip_debug_regs_clear() - clear the zip debug regs */
hisi_zip_debug_regs_clear(struct hisi_qm * qm)9234b33f057SShukun Tan static void hisi_zip_debug_regs_clear(struct hisi_qm *qm)
92472c7a68dSZhou Wang {
925698f9523SHao Fang 	int i, j;
926698f9523SHao Fang 
927698f9523SHao Fang 	/* enable register read_clear bit */
928698f9523SHao Fang 	writel(HZIP_RD_CNT_CLR_CE_EN, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
929698f9523SHao Fang 	for (i = 0; i < ARRAY_SIZE(core_offsets); i++)
930698f9523SHao Fang 		for (j = 0; j < ARRAY_SIZE(hzip_dfx_regs); j++)
931698f9523SHao Fang 			readl(qm->io_base + core_offsets[i] +
932698f9523SHao Fang 			      hzip_dfx_regs[j].offset);
933698f9523SHao Fang 
934698f9523SHao Fang 	/* disable register read_clear bit */
93572c7a68dSZhou Wang 	writel(0x0, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
93672c7a68dSZhou Wang 
93772c7a68dSZhou Wang 	hisi_qm_debug_regs_clear(qm);
93872c7a68dSZhou Wang }
93972c7a68dSZhou Wang 
hisi_zip_debugfs_exit(struct hisi_qm * qm)9404b33f057SShukun Tan static void hisi_zip_debugfs_exit(struct hisi_qm *qm)
94172c7a68dSZhou Wang {
942b40b62edSKai Ye 	hisi_qm_regs_debugfs_uninit(qm, ARRAY_SIZE(hzip_diff_regs));
9439b0c97dfSKai Ye 
94472c7a68dSZhou Wang 	debugfs_remove_recursive(qm->debug.debug_root);
94572c7a68dSZhou Wang 
9464b33f057SShukun Tan 	if (qm->fun_type == QM_HW_PF) {
9474b33f057SShukun Tan 		hisi_zip_debug_regs_clear(qm);
9484b33f057SShukun Tan 		qm->debug.curr_qm_qp_num = 0;
9494b33f057SShukun Tan 	}
95072c7a68dSZhou Wang }
95172c7a68dSZhou Wang 
hisi_zip_show_last_regs_init(struct hisi_qm * qm)9525bfabd50SKai Ye static int hisi_zip_show_last_regs_init(struct hisi_qm *qm)
9535bfabd50SKai Ye {
9545bfabd50SKai Ye 	int core_dfx_regs_num =  ARRAY_SIZE(hzip_dump_dfx_regs);
9555bfabd50SKai Ye 	int com_dfx_regs_num = ARRAY_SIZE(hzip_com_dfx_regs);
9565bfabd50SKai Ye 	struct qm_debug *debug = &qm->debug;
9575bfabd50SKai Ye 	void __iomem *io_base;
958db700974SWeili Qian 	u32 zip_core_num;
9595bfabd50SKai Ye 	int i, j, idx;
9605bfabd50SKai Ye 
96152f0b4a3SZhiqi Song 	zip_core_num = qm->cap_tables.dev_cap_table[ZIP_CORE_NUM_CAP_IDX].cap_val;
962db700974SWeili Qian 
963db700974SWeili Qian 	debug->last_words = kcalloc(core_dfx_regs_num * zip_core_num + com_dfx_regs_num,
964db700974SWeili Qian 				    sizeof(unsigned int), GFP_KERNEL);
9655bfabd50SKai Ye 	if (!debug->last_words)
9665bfabd50SKai Ye 		return -ENOMEM;
9675bfabd50SKai Ye 
9685bfabd50SKai Ye 	for (i = 0; i < com_dfx_regs_num; i++) {
9695bfabd50SKai Ye 		io_base = qm->io_base + hzip_com_dfx_regs[i].offset;
9705bfabd50SKai Ye 		debug->last_words[i] = readl_relaxed(io_base);
9715bfabd50SKai Ye 	}
9725bfabd50SKai Ye 
973db700974SWeili Qian 	for (i = 0; i < zip_core_num; i++) {
9745bfabd50SKai Ye 		io_base = qm->io_base + core_offsets[i];
9755bfabd50SKai Ye 		for (j = 0; j < core_dfx_regs_num; j++) {
9765bfabd50SKai Ye 			idx = com_dfx_regs_num + i * core_dfx_regs_num + j;
9775bfabd50SKai Ye 			debug->last_words[idx] = readl_relaxed(
9785bfabd50SKai Ye 				io_base + hzip_dump_dfx_regs[j].offset);
9795bfabd50SKai Ye 		}
9805bfabd50SKai Ye 	}
9815bfabd50SKai Ye 
9825bfabd50SKai Ye 	return 0;
9835bfabd50SKai Ye }
9845bfabd50SKai Ye 
hisi_zip_show_last_regs_uninit(struct hisi_qm * qm)9855bfabd50SKai Ye static void hisi_zip_show_last_regs_uninit(struct hisi_qm *qm)
9865bfabd50SKai Ye {
9875bfabd50SKai Ye 	struct qm_debug *debug = &qm->debug;
9885bfabd50SKai Ye 
9895bfabd50SKai Ye 	if (qm->fun_type == QM_HW_VF || !debug->last_words)
9905bfabd50SKai Ye 		return;
9915bfabd50SKai Ye 
9925bfabd50SKai Ye 	kfree(debug->last_words);
9935bfabd50SKai Ye 	debug->last_words = NULL;
9945bfabd50SKai Ye }
9955bfabd50SKai Ye 
hisi_zip_show_last_dfx_regs(struct hisi_qm * qm)9965bfabd50SKai Ye static void hisi_zip_show_last_dfx_regs(struct hisi_qm *qm)
9975bfabd50SKai Ye {
9985bfabd50SKai Ye 	int core_dfx_regs_num =  ARRAY_SIZE(hzip_dump_dfx_regs);
9995bfabd50SKai Ye 	int com_dfx_regs_num = ARRAY_SIZE(hzip_com_dfx_regs);
1000db700974SWeili Qian 	u32 zip_core_num, zip_comp_core_num;
10015bfabd50SKai Ye 	struct qm_debug *debug = &qm->debug;
10025bfabd50SKai Ye 	char buf[HZIP_BUF_SIZE];
10035bfabd50SKai Ye 	void __iomem *base;
10045bfabd50SKai Ye 	int i, j, idx;
10055bfabd50SKai Ye 	u32 val;
10065bfabd50SKai Ye 
10075bfabd50SKai Ye 	if (qm->fun_type == QM_HW_VF || !debug->last_words)
10085bfabd50SKai Ye 		return;
10095bfabd50SKai Ye 
10105bfabd50SKai Ye 	for (i = 0; i < com_dfx_regs_num; i++) {
10115bfabd50SKai Ye 		val = readl_relaxed(qm->io_base + hzip_com_dfx_regs[i].offset);
10125bfabd50SKai Ye 		if (debug->last_words[i] != val)
10135bfabd50SKai Ye 			pci_info(qm->pdev, "com_dfx: %s \t= 0x%08x => 0x%08x\n",
10145bfabd50SKai Ye 				 hzip_com_dfx_regs[i].name, debug->last_words[i], val);
10155bfabd50SKai Ye 	}
10165bfabd50SKai Ye 
101752f0b4a3SZhiqi Song 	zip_core_num = qm->cap_tables.dev_cap_table[ZIP_CORE_NUM_CAP_IDX].cap_val;
101852f0b4a3SZhiqi Song 	zip_comp_core_num = qm->cap_tables.dev_cap_table[ZIP_CLUSTER_COMP_NUM_CAP_IDX].cap_val;
101952f0b4a3SZhiqi Song 
1020db700974SWeili Qian 	for (i = 0; i < zip_core_num; i++) {
1021db700974SWeili Qian 		if (i < zip_comp_core_num)
10225bfabd50SKai Ye 			scnprintf(buf, sizeof(buf), "Comp_core-%d", i);
10235bfabd50SKai Ye 		else
10245bfabd50SKai Ye 			scnprintf(buf, sizeof(buf), "Decomp_core-%d",
1025db700974SWeili Qian 				  i - zip_comp_core_num);
10265bfabd50SKai Ye 		base = qm->io_base + core_offsets[i];
10275bfabd50SKai Ye 
10285bfabd50SKai Ye 		pci_info(qm->pdev, "==>%s:\n", buf);
10295bfabd50SKai Ye 		/* dump last word for dfx regs during control resetting */
10305bfabd50SKai Ye 		for (j = 0; j < core_dfx_regs_num; j++) {
10315bfabd50SKai Ye 			idx = com_dfx_regs_num + i * core_dfx_regs_num + j;
10325bfabd50SKai Ye 			val = readl_relaxed(base + hzip_dump_dfx_regs[j].offset);
10335bfabd50SKai Ye 			if (debug->last_words[idx] != val)
10345bfabd50SKai Ye 				pci_info(qm->pdev, "%s \t= 0x%08x => 0x%08x\n",
1035db700974SWeili Qian 					 hzip_dump_dfx_regs[j].name,
1036db700974SWeili Qian 					 debug->last_words[idx], val);
10375bfabd50SKai Ye 		}
10385bfabd50SKai Ye 	}
10395bfabd50SKai Ye }
10405bfabd50SKai Ye 
hisi_zip_log_hw_error(struct hisi_qm * qm,u32 err_sts)1041f826e6efSShukun Tan static void hisi_zip_log_hw_error(struct hisi_qm *qm, u32 err_sts)
1042f826e6efSShukun Tan {
1043f826e6efSShukun Tan 	const struct hisi_zip_hw_error *err = zip_hw_error;
1044f826e6efSShukun Tan 	struct device *dev = &qm->pdev->dev;
1045f826e6efSShukun Tan 	u32 err_val;
1046f826e6efSShukun Tan 
1047f826e6efSShukun Tan 	while (err->msg) {
1048f826e6efSShukun Tan 		if (err->int_msk & err_sts) {
1049f826e6efSShukun Tan 			dev_err(dev, "%s [error status=0x%x] found\n",
1050f826e6efSShukun Tan 				err->msg, err->int_msk);
1051f826e6efSShukun Tan 
1052f826e6efSShukun Tan 			if (err->int_msk & HZIP_CORE_INT_STATUS_M_ECC) {
1053f826e6efSShukun Tan 				err_val = readl(qm->io_base +
1054f826e6efSShukun Tan 						HZIP_CORE_SRAM_ECC_ERR_INFO);
1055f826e6efSShukun Tan 				dev_err(dev, "hisi-zip multi ecc sram num=0x%x\n",
1056f826e6efSShukun Tan 					((err_val >>
1057f826e6efSShukun Tan 					HZIP_SRAM_ECC_ERR_NUM_SHIFT) & 0xFF));
1058f826e6efSShukun Tan 			}
1059f826e6efSShukun Tan 		}
1060f826e6efSShukun Tan 		err++;
1061f826e6efSShukun Tan 	}
1062f826e6efSShukun Tan }
1063f826e6efSShukun Tan 
hisi_zip_get_hw_err_status(struct hisi_qm * qm)1064f826e6efSShukun Tan static u32 hisi_zip_get_hw_err_status(struct hisi_qm *qm)
1065f826e6efSShukun Tan {
1066f826e6efSShukun Tan 	return readl(qm->io_base + HZIP_CORE_INT_STATUS);
1067f826e6efSShukun Tan }
1068f826e6efSShukun Tan 
hisi_zip_clear_hw_err_status(struct hisi_qm * qm,u32 err_sts)106984c9b780SShukun Tan static void hisi_zip_clear_hw_err_status(struct hisi_qm *qm, u32 err_sts)
107084c9b780SShukun Tan {
107184c9b780SShukun Tan 	writel(err_sts, qm->io_base + HZIP_CORE_INT_SOURCE);
1072*6a975fbaSWeili Qian }
1073*6a975fbaSWeili Qian 
hisi_zip_disable_error_report(struct hisi_qm * qm,u32 err_type)1074*6a975fbaSWeili Qian static void hisi_zip_disable_error_report(struct hisi_qm *qm, u32 err_type)
1075*6a975fbaSWeili Qian {
1076*6a975fbaSWeili Qian 	u32 nfe_mask;
1077*6a975fbaSWeili Qian 
1078*6a975fbaSWeili Qian 	nfe_mask = hisi_qm_get_hw_info(qm, zip_basic_cap_info, ZIP_NFE_MASK_CAP, qm->cap_ver);
1079*6a975fbaSWeili Qian 	writel(nfe_mask & (~err_type), qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB);
108084c9b780SShukun Tan }
108184c9b780SShukun Tan 
hisi_zip_open_axi_master_ooo(struct hisi_qm * qm)108284c9b780SShukun Tan static void hisi_zip_open_axi_master_ooo(struct hisi_qm *qm)
108384c9b780SShukun Tan {
108484c9b780SShukun Tan 	u32 val;
108584c9b780SShukun Tan 
108684c9b780SShukun Tan 	val = readl(qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
108784c9b780SShukun Tan 
108884c9b780SShukun Tan 	writel(val & ~HZIP_AXI_SHUTDOWN_ENABLE,
108984c9b780SShukun Tan 	       qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
109084c9b780SShukun Tan 
109184c9b780SShukun Tan 	writel(val | HZIP_AXI_SHUTDOWN_ENABLE,
109284c9b780SShukun Tan 	       qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL);
109384c9b780SShukun Tan }
109484c9b780SShukun Tan 
hisi_zip_close_axi_master_ooo(struct hisi_qm * qm)109584c9b780SShukun Tan static void hisi_zip_close_axi_master_ooo(struct hisi_qm *qm)
109684c9b780SShukun Tan {
109784c9b780SShukun Tan 	u32 nfe_enb;
109884c9b780SShukun Tan 
109984c9b780SShukun Tan 	/* Disable ECC Mbit error report. */
110084c9b780SShukun Tan 	nfe_enb = readl(qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB);
110184c9b780SShukun Tan 	writel(nfe_enb & ~HZIP_CORE_INT_STATUS_M_ECC,
110284c9b780SShukun Tan 	       qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB);
110384c9b780SShukun Tan 
110484c9b780SShukun Tan 	/* Inject zip ECC Mbit error to block master ooo. */
110584c9b780SShukun Tan 	writel(HZIP_CORE_INT_STATUS_M_ECC,
110684c9b780SShukun Tan 	       qm->io_base + HZIP_CORE_INT_SET);
110784c9b780SShukun Tan }
110884c9b780SShukun Tan 
hisi_zip_get_err_result(struct hisi_qm * qm)1109*6a975fbaSWeili Qian static enum acc_err_result hisi_zip_get_err_result(struct hisi_qm *qm)
1110*6a975fbaSWeili Qian {
1111*6a975fbaSWeili Qian 	u32 err_status;
1112*6a975fbaSWeili Qian 
1113*6a975fbaSWeili Qian 	err_status = hisi_zip_get_hw_err_status(qm);
1114*6a975fbaSWeili Qian 	if (err_status) {
1115*6a975fbaSWeili Qian 		if (err_status & qm->err_info.ecc_2bits_mask)
1116*6a975fbaSWeili Qian 			qm->err_status.is_dev_ecc_mbit = true;
1117*6a975fbaSWeili Qian 		hisi_zip_log_hw_error(qm, err_status);
1118*6a975fbaSWeili Qian 
1119*6a975fbaSWeili Qian 		if (err_status & qm->err_info.dev_reset_mask) {
1120*6a975fbaSWeili Qian 			/* Disable the same error reporting until device is recovered. */
1121*6a975fbaSWeili Qian 			hisi_zip_disable_error_report(qm, err_status);
1122*6a975fbaSWeili Qian 			return ACC_ERR_NEED_RESET;
1123*6a975fbaSWeili Qian 		}
1124*6a975fbaSWeili Qian 		hisi_zip_clear_hw_err_status(qm, err_status);
1125*6a975fbaSWeili Qian 	}
1126*6a975fbaSWeili Qian 
1127*6a975fbaSWeili Qian 	return ACC_ERR_RECOVERED;
1128*6a975fbaSWeili Qian }
1129*6a975fbaSWeili Qian 
hisi_zip_err_info_init(struct hisi_qm * qm)1130d9e21600SWeili Qian static void hisi_zip_err_info_init(struct hisi_qm *qm)
1131d9e21600SWeili Qian {
1132d9e21600SWeili Qian 	struct hisi_qm_err_info *err_info = &qm->err_info;
1133d9e21600SWeili Qian 
1134d90fab0dSWeili Qian 	err_info->fe = HZIP_CORE_INT_RAS_FE_ENB_MASK;
1135d90fab0dSWeili Qian 	err_info->ce = hisi_qm_get_hw_info(qm, zip_basic_cap_info, ZIP_QM_CE_MASK_CAP, qm->cap_ver);
1136d90fab0dSWeili Qian 	err_info->nfe = hisi_qm_get_hw_info(qm, zip_basic_cap_info,
1137d90fab0dSWeili Qian 					    ZIP_QM_NFE_MASK_CAP, qm->cap_ver);
1138d9e21600SWeili Qian 	err_info->ecc_2bits_mask = HZIP_CORE_INT_STATUS_M_ECC;
1139d90fab0dSWeili Qian 	err_info->qm_shutdown_mask = hisi_qm_get_hw_info(qm, zip_basic_cap_info,
1140d90fab0dSWeili Qian 							 ZIP_QM_OOO_SHUTDOWN_MASK_CAP, qm->cap_ver);
1141d90fab0dSWeili Qian 	err_info->dev_shutdown_mask = hisi_qm_get_hw_info(qm, zip_basic_cap_info,
1142d90fab0dSWeili Qian 							  ZIP_OOO_SHUTDOWN_MASK_CAP, qm->cap_ver);
1143d90fab0dSWeili Qian 	err_info->qm_reset_mask = hisi_qm_get_hw_info(qm, zip_basic_cap_info,
1144d90fab0dSWeili Qian 						      ZIP_QM_RESET_MASK_CAP, qm->cap_ver);
1145d90fab0dSWeili Qian 	err_info->dev_reset_mask = hisi_qm_get_hw_info(qm, zip_basic_cap_info,
1146d90fab0dSWeili Qian 						       ZIP_RESET_MASK_CAP, qm->cap_ver);
1147d9e21600SWeili Qian 	err_info->msi_wr_port = HZIP_WR_PORT;
1148d9e21600SWeili Qian 	err_info->acpi_rst = "ZRST";
1149d9e21600SWeili Qian }
1150d9e21600SWeili Qian 
1151eaebf4c3SShukun Tan static const struct hisi_qm_err_ini hisi_zip_err_ini = {
115284c9b780SShukun Tan 	.hw_init		= hisi_zip_set_user_domain_and_cache,
1153eaebf4c3SShukun Tan 	.hw_err_enable		= hisi_zip_hw_error_enable,
1154eaebf4c3SShukun Tan 	.hw_err_disable		= hisi_zip_hw_error_disable,
1155f826e6efSShukun Tan 	.get_dev_hw_err_status	= hisi_zip_get_hw_err_status,
115684c9b780SShukun Tan 	.clear_dev_hw_err_status = hisi_zip_clear_hw_err_status,
115784c9b780SShukun Tan 	.open_axi_master_ooo	= hisi_zip_open_axi_master_ooo,
115884c9b780SShukun Tan 	.close_axi_master_ooo	= hisi_zip_close_axi_master_ooo,
1159a5c164b1SLongfang Liu 	.open_sva_prefetch	= hisi_zip_open_sva_prefetch,
1160a5c164b1SLongfang Liu 	.close_sva_prefetch	= hisi_zip_close_sva_prefetch,
11615bfabd50SKai Ye 	.show_last_dfx_regs	= hisi_zip_show_last_dfx_regs,
1162d9e21600SWeili Qian 	.err_info_init		= hisi_zip_err_info_init,
1163*6a975fbaSWeili Qian 	.get_err_result		= hisi_zip_get_err_result,
1164eaebf4c3SShukun Tan };
116562c455caSZhou Wang 
hisi_zip_pf_probe_init(struct hisi_zip * hisi_zip)116662c455caSZhou Wang static int hisi_zip_pf_probe_init(struct hisi_zip *hisi_zip)
116762c455caSZhou Wang {
116862c455caSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
116962c455caSZhou Wang 	struct hisi_zip_ctrl *ctrl;
11705bfabd50SKai Ye 	int ret;
117162c455caSZhou Wang 
117262c455caSZhou Wang 	ctrl = devm_kzalloc(&qm->pdev->dev, sizeof(*ctrl), GFP_KERNEL);
117362c455caSZhou Wang 	if (!ctrl)
117462c455caSZhou Wang 		return -ENOMEM;
117562c455caSZhou Wang 
117662c455caSZhou Wang 	hisi_zip->ctrl = ctrl;
117762c455caSZhou Wang 	ctrl->hisi_zip = hisi_zip;
1178eaebf4c3SShukun Tan 
11796d9a8995SYang Shen 	ret = hisi_zip_set_user_domain_and_cache(qm);
11806d9a8995SYang Shen 	if (ret)
11816d9a8995SYang Shen 		return ret;
11826d9a8995SYang Shen 
1183b7a03a0fSChenghai Huang 	ret = hisi_zip_set_high_perf(qm);
1184b7a03a0fSChenghai Huang 	if (ret)
1185b7a03a0fSChenghai Huang 		return ret;
1186b7a03a0fSChenghai Huang 
1187a5c164b1SLongfang Liu 	hisi_zip_open_sva_prefetch(qm);
1188eaebf4c3SShukun Tan 	hisi_qm_dev_err_init(qm);
11894b33f057SShukun Tan 	hisi_zip_debug_regs_clear(qm);
119062c455caSZhou Wang 
11915bfabd50SKai Ye 	ret = hisi_zip_show_last_regs_init(qm);
11925bfabd50SKai Ye 	if (ret)
11935bfabd50SKai Ye 		pci_err(qm->pdev, "Failed to init last word regs!\n");
11945bfabd50SKai Ye 
11955bfabd50SKai Ye 	return ret;
119662c455caSZhou Wang }
119762c455caSZhou Wang 
zip_pre_store_cap_reg(struct hisi_qm * qm)119852f0b4a3SZhiqi Song static int zip_pre_store_cap_reg(struct hisi_qm *qm)
119952f0b4a3SZhiqi Song {
120052f0b4a3SZhiqi Song 	struct hisi_qm_cap_record *zip_cap;
120152f0b4a3SZhiqi Song 	struct pci_dev *pdev = qm->pdev;
120252f0b4a3SZhiqi Song 	size_t i, size;
120352f0b4a3SZhiqi Song 
120452f0b4a3SZhiqi Song 	size = ARRAY_SIZE(zip_pre_store_caps);
120552f0b4a3SZhiqi Song 	zip_cap = devm_kzalloc(&pdev->dev, sizeof(*zip_cap) * size, GFP_KERNEL);
120652f0b4a3SZhiqi Song 	if (!zip_cap)
120752f0b4a3SZhiqi Song 		return -ENOMEM;
120852f0b4a3SZhiqi Song 
120952f0b4a3SZhiqi Song 	for (i = 0; i < size; i++) {
121052f0b4a3SZhiqi Song 		zip_cap[i].type = zip_pre_store_caps[i];
121152f0b4a3SZhiqi Song 		zip_cap[i].cap_val = hisi_qm_get_hw_info(qm, zip_basic_cap_info,
121252f0b4a3SZhiqi Song 				     zip_pre_store_caps[i], qm->cap_ver);
121352f0b4a3SZhiqi Song 	}
121452f0b4a3SZhiqi Song 
121552f0b4a3SZhiqi Song 	qm->cap_tables.dev_cap_table = zip_cap;
121652f0b4a3SZhiqi Song 
121752f0b4a3SZhiqi Song 	return 0;
121852f0b4a3SZhiqi Song }
121952f0b4a3SZhiqi Song 
hisi_zip_qm_init(struct hisi_qm * qm,struct pci_dev * pdev)1220cfd66a66SLongfang Liu static int hisi_zip_qm_init(struct hisi_qm *qm, struct pci_dev *pdev)
122139977f4bSHao Fang {
12221e8102e2SWenkai Lin 	u64 alg_msk;
1223d310dc25SZhiqi Song 	int ret;
1224d310dc25SZhiqi Song 
122539977f4bSHao Fang 	qm->pdev = pdev;
122658ca0060SWeili Qian 	qm->ver = pdev->revision;
1227f8408d2bSKai Ye 	qm->mode = uacce_mode;
122839977f4bSHao Fang 	qm->sqe_size = HZIP_SQE_SIZE;
122939977f4bSHao Fang 	qm->dev_name = hisi_zip_name;
1230d9701f8dSWeili Qian 
1231fae74feaSShameer Kolothum 	qm->fun_type = (pdev->device == PCI_DEVICE_ID_HUAWEI_ZIP_PF) ?
1232cfd66a66SLongfang Liu 			QM_HW_PF : QM_HW_VF;
1233d9701f8dSWeili Qian 	if (qm->fun_type == QM_HW_PF) {
1234d9701f8dSWeili Qian 		qm->qp_base = HZIP_PF_DEF_Q_BASE;
1235d9701f8dSWeili Qian 		qm->qp_num = pf_q_num;
12362fcb4cc3SSihang Chen 		qm->debug.curr_qm_qp_num = pf_q_num;
1237d9701f8dSWeili Qian 		qm->qm_list = &zip_devices;
12388b21a9b1SWeili Qian 		qm->err_ini = &hisi_zip_err_ini;
12394c79c7a4SLongfang Liu 		if (pf_q_num_flag)
12404c79c7a4SLongfang Liu 			set_bit(QM_MODULE_PARAM, &qm->misc_ctl);
1241d9701f8dSWeili Qian 	} else if (qm->fun_type == QM_HW_VF && qm->ver == QM_HW_V1) {
1242d9701f8dSWeili Qian 		/*
1243d9701f8dSWeili Qian 		 * have no way to get qm configure in VM in v1 hardware,
1244d9701f8dSWeili Qian 		 * so currently force PF to uses HZIP_PF_DEF_Q_NUM, and force
1245d9701f8dSWeili Qian 		 * to trigger only one VF in v1 hardware.
1246d9701f8dSWeili Qian 		 *
1247d9701f8dSWeili Qian 		 * v2 hardware has no such problem.
1248d9701f8dSWeili Qian 		 */
1249d9701f8dSWeili Qian 		qm->qp_base = HZIP_PF_DEF_Q_NUM;
1250d9701f8dSWeili Qian 		qm->qp_num = HZIP_QUEUE_NUM_V1 - HZIP_PF_DEF_Q_NUM;
1251d9701f8dSWeili Qian 	}
1252cfd66a66SLongfang Liu 
1253d310dc25SZhiqi Song 	ret = hisi_qm_init(qm);
1254d310dc25SZhiqi Song 	if (ret) {
1255d310dc25SZhiqi Song 		pci_err(qm->pdev, "Failed to init zip qm configures!\n");
1256d310dc25SZhiqi Song 		return ret;
1257d310dc25SZhiqi Song 	}
1258d310dc25SZhiqi Song 
125952f0b4a3SZhiqi Song 	/* Fetch and save the value of capability registers */
126052f0b4a3SZhiqi Song 	ret = zip_pre_store_cap_reg(qm);
126152f0b4a3SZhiqi Song 	if (ret) {
126252f0b4a3SZhiqi Song 		pci_err(qm->pdev, "Failed to pre-store capability registers!\n");
126352f0b4a3SZhiqi Song 		hisi_qm_uninit(qm);
126452f0b4a3SZhiqi Song 		return ret;
126552f0b4a3SZhiqi Song 	}
126652f0b4a3SZhiqi Song 
126752f0b4a3SZhiqi Song 	alg_msk = qm->cap_tables.dev_cap_table[ZIP_DEV_ALG_BITMAP_IDX].cap_val;
12681e8102e2SWenkai Lin 	ret = hisi_qm_set_algs(qm, alg_msk, zip_dev_algs, ARRAY_SIZE(zip_dev_algs));
1269d310dc25SZhiqi Song 	if (ret) {
1270d310dc25SZhiqi Song 		pci_err(qm->pdev, "Failed to set zip algs!\n");
1271d310dc25SZhiqi Song 		hisi_qm_uninit(qm);
1272d310dc25SZhiqi Song 	}
1273d310dc25SZhiqi Song 
1274d310dc25SZhiqi Song 	return ret;
12751dc44035SYang Shen }
12761dc44035SYang Shen 
hisi_zip_qm_uninit(struct hisi_qm * qm)12771dc44035SYang Shen static void hisi_zip_qm_uninit(struct hisi_qm *qm)
12781dc44035SYang Shen {
12791dc44035SYang Shen 	hisi_qm_uninit(qm);
128039977f4bSHao Fang }
128139977f4bSHao Fang 
hisi_zip_probe_init(struct hisi_zip * hisi_zip)1282cfd66a66SLongfang Liu static int hisi_zip_probe_init(struct hisi_zip *hisi_zip)
1283cfd66a66SLongfang Liu {
128438a9eb81SKai Ye 	u32 type_rate = HZIP_SHAPER_RATE_COMPRESS;
1285cfd66a66SLongfang Liu 	struct hisi_qm *qm = &hisi_zip->qm;
1286cfd66a66SLongfang Liu 	int ret;
1287cfd66a66SLongfang Liu 
128839977f4bSHao Fang 	if (qm->fun_type == QM_HW_PF) {
128939977f4bSHao Fang 		ret = hisi_zip_pf_probe_init(hisi_zip);
129039977f4bSHao Fang 		if (ret)
129139977f4bSHao Fang 			return ret;
129238a9eb81SKai Ye 		/* enable shaper type 0 */
129338a9eb81SKai Ye 		if (qm->ver >= QM_HW_V3) {
129438a9eb81SKai Ye 			type_rate |= QM_SHAPER_ENABLE;
129538a9eb81SKai Ye 
129638a9eb81SKai Ye 			/* ZIP need to enable shaper type 1 */
129738a9eb81SKai Ye 			type_rate |= HZIP_SHAPER_RATE_DECOMPRESS << QM_SHAPER_TYPE1_OFFSET;
129838a9eb81SKai Ye 			qm->type_rate = type_rate;
129938a9eb81SKai Ye 		}
1300cfd66a66SLongfang Liu 	}
1301cfd66a66SLongfang Liu 
1302cfd66a66SLongfang Liu 	return 0;
1303cfd66a66SLongfang Liu }
1304cfd66a66SLongfang Liu 
hisi_zip_probe_uninit(struct hisi_qm * qm)13058b21a9b1SWeili Qian static void hisi_zip_probe_uninit(struct hisi_qm *qm)
13068b21a9b1SWeili Qian {
13078b21a9b1SWeili Qian 	if (qm->fun_type == QM_HW_VF)
13088b21a9b1SWeili Qian 		return;
13098b21a9b1SWeili Qian 
13108b21a9b1SWeili Qian 	hisi_zip_show_last_regs_uninit(qm);
13118b21a9b1SWeili Qian 	hisi_zip_close_sva_prefetch(qm);
13128b21a9b1SWeili Qian 	hisi_qm_dev_err_uninit(qm);
13138b21a9b1SWeili Qian }
13148b21a9b1SWeili Qian 
hisi_zip_probe(struct pci_dev * pdev,const struct pci_device_id * id)1315cfd66a66SLongfang Liu static int hisi_zip_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1316cfd66a66SLongfang Liu {
1317cfd66a66SLongfang Liu 	struct hisi_zip *hisi_zip;
1318cfd66a66SLongfang Liu 	struct hisi_qm *qm;
1319cfd66a66SLongfang Liu 	int ret;
1320cfd66a66SLongfang Liu 
1321cfd66a66SLongfang Liu 	hisi_zip = devm_kzalloc(&pdev->dev, sizeof(*hisi_zip), GFP_KERNEL);
1322cfd66a66SLongfang Liu 	if (!hisi_zip)
1323cfd66a66SLongfang Liu 		return -ENOMEM;
1324cfd66a66SLongfang Liu 
1325cfd66a66SLongfang Liu 	qm = &hisi_zip->qm;
1326cfd66a66SLongfang Liu 
1327cfd66a66SLongfang Liu 	ret = hisi_zip_qm_init(qm, pdev);
1328cfd66a66SLongfang Liu 	if (ret) {
1329cfd66a66SLongfang Liu 		pci_err(pdev, "Failed to init ZIP QM (%d)!\n", ret);
1330cfd66a66SLongfang Liu 		return ret;
1331cfd66a66SLongfang Liu 	}
1332cfd66a66SLongfang Liu 
1333cfd66a66SLongfang Liu 	ret = hisi_zip_probe_init(hisi_zip);
1334cfd66a66SLongfang Liu 	if (ret) {
1335cfd66a66SLongfang Liu 		pci_err(pdev, "Failed to probe (%d)!\n", ret);
1336cfd66a66SLongfang Liu 		goto err_qm_uninit;
133739977f4bSHao Fang 	}
133839977f4bSHao Fang 
133939977f4bSHao Fang 	ret = hisi_qm_start(qm);
134039977f4bSHao Fang 	if (ret)
13418b21a9b1SWeili Qian 		goto err_probe_uninit;
134239977f4bSHao Fang 
13434b33f057SShukun Tan 	ret = hisi_zip_debugfs_init(qm);
134439977f4bSHao Fang 	if (ret)
1345b1a25820SYang Shen 		pci_err(pdev, "failed to init debugfs (%d)!\n", ret);
134639977f4bSHao Fang 
13473d29e98dSYang Shen 	ret = hisi_qm_alg_register(qm, &zip_devices);
13483d29e98dSYang Shen 	if (ret < 0) {
1349b1a25820SYang Shen 		pci_err(pdev, "failed to register driver to crypto!\n");
13503d29e98dSYang Shen 		goto err_qm_stop;
13513d29e98dSYang Shen 	}
135239977f4bSHao Fang 
13539e00df71SZhangfei Gao 	if (qm->uacce) {
13549e00df71SZhangfei Gao 		ret = uacce_register(qm->uacce);
1355b1a25820SYang Shen 		if (ret) {
1356b1a25820SYang Shen 			pci_err(pdev, "failed to register uacce (%d)!\n", ret);
13573d29e98dSYang Shen 			goto err_qm_alg_unregister;
13589e00df71SZhangfei Gao 		}
1359b1a25820SYang Shen 	}
13609e00df71SZhangfei Gao 
136139977f4bSHao Fang 	if (qm->fun_type == QM_HW_PF && vfs_num > 0) {
1362cd1b7ae3SShukun Tan 		ret = hisi_qm_sriov_enable(pdev, vfs_num);
136339977f4bSHao Fang 		if (ret < 0)
13643d29e98dSYang Shen 			goto err_qm_alg_unregister;
136539977f4bSHao Fang 	}
136639977f4bSHao Fang 
1367607c191bSWeili Qian 	hisi_qm_pm_init(qm);
1368607c191bSWeili Qian 
136939977f4bSHao Fang 	return 0;
137039977f4bSHao Fang 
13713d29e98dSYang Shen err_qm_alg_unregister:
13723d29e98dSYang Shen 	hisi_qm_alg_unregister(qm, &zip_devices);
13733d29e98dSYang Shen 
13743d29e98dSYang Shen err_qm_stop:
13754b33f057SShukun Tan 	hisi_zip_debugfs_exit(qm);
1376e88dd6e1SYang Shen 	hisi_qm_stop(qm, QM_NORMAL);
13773d29e98dSYang Shen 
13788b21a9b1SWeili Qian err_probe_uninit:
13798b21a9b1SWeili Qian 	hisi_zip_probe_uninit(qm);
13803d29e98dSYang Shen 
138139977f4bSHao Fang err_qm_uninit:
13821dc44035SYang Shen 	hisi_zip_qm_uninit(qm);
1383cfd66a66SLongfang Liu 
138439977f4bSHao Fang 	return ret;
138539977f4bSHao Fang }
138639977f4bSHao Fang 
hisi_zip_remove(struct pci_dev * pdev)138762c455caSZhou Wang static void hisi_zip_remove(struct pci_dev *pdev)
138862c455caSZhou Wang {
1389d8140b87SYang Shen 	struct hisi_qm *qm = pci_get_drvdata(pdev);
139062c455caSZhou Wang 
1391607c191bSWeili Qian 	hisi_qm_pm_uninit(qm);
1392daa31783SWeili Qian 	hisi_qm_wait_task_finish(qm, &zip_devices);
13933d29e98dSYang Shen 	hisi_qm_alg_unregister(qm, &zip_devices);
13943d29e98dSYang Shen 
1395619e464aSShukun Tan 	if (qm->fun_type == QM_HW_PF && qm->vfs_num)
13963e9954feSWeili Qian 		hisi_qm_sriov_disable(pdev, true);
139779e09f30SZhou Wang 
13984b33f057SShukun Tan 	hisi_zip_debugfs_exit(qm);
1399e88dd6e1SYang Shen 	hisi_qm_stop(qm, QM_NORMAL);
14008b21a9b1SWeili Qian 	hisi_zip_probe_uninit(qm);
14011dc44035SYang Shen 	hisi_zip_qm_uninit(qm);
140262c455caSZhou Wang }
140362c455caSZhou Wang 
1404607c191bSWeili Qian static const struct dev_pm_ops hisi_zip_pm_ops = {
1405607c191bSWeili Qian 	SET_RUNTIME_PM_OPS(hisi_qm_suspend, hisi_qm_resume, NULL)
1406607c191bSWeili Qian };
1407607c191bSWeili Qian 
140862c455caSZhou Wang static const struct pci_error_handlers hisi_zip_err_handler = {
1409f826e6efSShukun Tan 	.error_detected	= hisi_qm_dev_err_detected,
141084c9b780SShukun Tan 	.slot_reset	= hisi_qm_dev_slot_reset,
14117ce396faSShukun Tan 	.reset_prepare	= hisi_qm_reset_prepare,
14127ce396faSShukun Tan 	.reset_done	= hisi_qm_reset_done,
141362c455caSZhou Wang };
141462c455caSZhou Wang 
141562c455caSZhou Wang static struct pci_driver hisi_zip_pci_driver = {
141662c455caSZhou Wang 	.name			= "hisi_zip",
141762c455caSZhou Wang 	.id_table		= hisi_zip_dev_ids,
141862c455caSZhou Wang 	.probe			= hisi_zip_probe,
141962c455caSZhou Wang 	.remove			= hisi_zip_remove,
1420bf6a7a5aSArnd Bergmann 	.sriov_configure	= IS_ENABLED(CONFIG_PCI_IOV) ?
1421cd1b7ae3SShukun Tan 					hisi_qm_sriov_configure : NULL,
142262c455caSZhou Wang 	.err_handler		= &hisi_zip_err_handler,
142364dfe495SYang Shen 	.shutdown		= hisi_qm_dev_shutdown,
1424607c191bSWeili Qian 	.driver.pm		= &hisi_zip_pm_ops,
142562c455caSZhou Wang };
142662c455caSZhou Wang 
hisi_zip_get_pf_driver(void)1427442fbc09SShameer Kolothum struct pci_driver *hisi_zip_get_pf_driver(void)
1428442fbc09SShameer Kolothum {
1429442fbc09SShameer Kolothum 	return &hisi_zip_pci_driver;
1430442fbc09SShameer Kolothum }
1431442fbc09SShameer Kolothum EXPORT_SYMBOL_GPL(hisi_zip_get_pf_driver);
1432442fbc09SShameer Kolothum 
hisi_zip_register_debugfs(void)143372c7a68dSZhou Wang static void hisi_zip_register_debugfs(void)
143472c7a68dSZhou Wang {
143572c7a68dSZhou Wang 	if (!debugfs_initialized())
143672c7a68dSZhou Wang 		return;
143772c7a68dSZhou Wang 
143872c7a68dSZhou Wang 	hzip_debugfs_root = debugfs_create_dir("hisi_zip", NULL);
143972c7a68dSZhou Wang }
144072c7a68dSZhou Wang 
hisi_zip_unregister_debugfs(void)144172c7a68dSZhou Wang static void hisi_zip_unregister_debugfs(void)
144272c7a68dSZhou Wang {
144372c7a68dSZhou Wang 	debugfs_remove_recursive(hzip_debugfs_root);
144472c7a68dSZhou Wang }
144572c7a68dSZhou Wang 
hisi_zip_init(void)144662c455caSZhou Wang static int __init hisi_zip_init(void)
144762c455caSZhou Wang {
144862c455caSZhou Wang 	int ret;
144962c455caSZhou Wang 
145018f1ab3fSShukun Tan 	hisi_qm_init_list(&zip_devices);
145172c7a68dSZhou Wang 	hisi_zip_register_debugfs();
145272c7a68dSZhou Wang 
145362c455caSZhou Wang 	ret = pci_register_driver(&hisi_zip_pci_driver);
145462c455caSZhou Wang 	if (ret < 0) {
145572c7a68dSZhou Wang 		hisi_zip_unregister_debugfs();
14562ca73193SYang Shen 		pr_err("Failed to register pci driver.\n");
14572ca73193SYang Shen 	}
145872c7a68dSZhou Wang 
145962c455caSZhou Wang 	return ret;
146062c455caSZhou Wang }
146162c455caSZhou Wang 
hisi_zip_exit(void)146262c455caSZhou Wang static void __exit hisi_zip_exit(void)
146362c455caSZhou Wang {
146462c455caSZhou Wang 	pci_unregister_driver(&hisi_zip_pci_driver);
146572c7a68dSZhou Wang 	hisi_zip_unregister_debugfs();
146662c455caSZhou Wang }
146762c455caSZhou Wang 
146862c455caSZhou Wang module_init(hisi_zip_init);
146962c455caSZhou Wang module_exit(hisi_zip_exit);
147062c455caSZhou Wang 
147162c455caSZhou Wang MODULE_LICENSE("GPL v2");
147262c455caSZhou Wang MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>");
147362c455caSZhou Wang MODULE_DESCRIPTION("Driver for HiSilicon ZIP accelerator");
1474