18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright (c) 2018-2019 HiSilicon Limited. */
38c2ecf20Sopenharmony_ci#include <linux/acpi.h>
48c2ecf20Sopenharmony_ci#include <linux/aer.h>
58c2ecf20Sopenharmony_ci#include <linux/bitops.h>
68c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
78c2ecf20Sopenharmony_ci#include <linux/init.h>
88c2ecf20Sopenharmony_ci#include <linux/io.h>
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/pci.h>
128c2ecf20Sopenharmony_ci#include <linux/topology.h>
138c2ecf20Sopenharmony_ci#include "hpre.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define HPRE_QUEUE_NUM_V2		1024
168c2ecf20Sopenharmony_ci#define HPRE_QM_ABNML_INT_MASK		0x100004
178c2ecf20Sopenharmony_ci#define HPRE_CTRL_CNT_CLR_CE_BIT	BIT(0)
188c2ecf20Sopenharmony_ci#define HPRE_COMM_CNT_CLR_CE		0x0
198c2ecf20Sopenharmony_ci#define HPRE_CTRL_CNT_CLR_CE		0x301000
208c2ecf20Sopenharmony_ci#define HPRE_FSM_MAX_CNT		0x301008
218c2ecf20Sopenharmony_ci#define HPRE_VFG_AXQOS			0x30100c
228c2ecf20Sopenharmony_ci#define HPRE_VFG_AXCACHE		0x301010
238c2ecf20Sopenharmony_ci#define HPRE_RDCHN_INI_CFG		0x301014
248c2ecf20Sopenharmony_ci#define HPRE_AWUSR_FP_CFG		0x301018
258c2ecf20Sopenharmony_ci#define HPRE_BD_ENDIAN			0x301020
268c2ecf20Sopenharmony_ci#define HPRE_ECC_BYPASS			0x301024
278c2ecf20Sopenharmony_ci#define HPRE_RAS_WIDTH_CFG		0x301028
288c2ecf20Sopenharmony_ci#define HPRE_POISON_BYPASS		0x30102c
298c2ecf20Sopenharmony_ci#define HPRE_BD_ARUSR_CFG		0x301030
308c2ecf20Sopenharmony_ci#define HPRE_BD_AWUSR_CFG		0x301034
318c2ecf20Sopenharmony_ci#define HPRE_TYPES_ENB			0x301038
328c2ecf20Sopenharmony_ci#define HPRE_DATA_RUSER_CFG		0x30103c
338c2ecf20Sopenharmony_ci#define HPRE_DATA_WUSER_CFG		0x301040
348c2ecf20Sopenharmony_ci#define HPRE_INT_MASK			0x301400
358c2ecf20Sopenharmony_ci#define HPRE_INT_STATUS			0x301800
368c2ecf20Sopenharmony_ci#define HPRE_CORE_INT_ENABLE		0
378c2ecf20Sopenharmony_ci#define HPRE_CORE_INT_DISABLE		0x003fffff
388c2ecf20Sopenharmony_ci#define HPRE_RAS_ECC_1BIT_TH		0x30140c
398c2ecf20Sopenharmony_ci#define HPRE_RDCHN_INI_ST		0x301a00
408c2ecf20Sopenharmony_ci#define HPRE_CLSTR_BASE			0x302000
418c2ecf20Sopenharmony_ci#define HPRE_CORE_EN_OFFSET		0x04
428c2ecf20Sopenharmony_ci#define HPRE_CORE_INI_CFG_OFFSET	0x20
438c2ecf20Sopenharmony_ci#define HPRE_CORE_INI_STATUS_OFFSET	0x80
448c2ecf20Sopenharmony_ci#define HPRE_CORE_HTBT_WARN_OFFSET	0x8c
458c2ecf20Sopenharmony_ci#define HPRE_CORE_IS_SCHD_OFFSET	0x90
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define HPRE_RAS_CE_ENB			0x301410
488c2ecf20Sopenharmony_ci#define HPRE_HAC_RAS_CE_ENABLE		0x1
498c2ecf20Sopenharmony_ci#define HPRE_RAS_NFE_ENB		0x301414
508c2ecf20Sopenharmony_ci#define HPRE_HAC_RAS_NFE_ENABLE		0x3ffffe
518c2ecf20Sopenharmony_ci#define HPRE_RAS_FE_ENB			0x301418
528c2ecf20Sopenharmony_ci#define HPRE_HAC_RAS_FE_ENABLE		0
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define HPRE_CORE_ENB		(HPRE_CLSTR_BASE + HPRE_CORE_EN_OFFSET)
558c2ecf20Sopenharmony_ci#define HPRE_CORE_INI_CFG	(HPRE_CLSTR_BASE + HPRE_CORE_INI_CFG_OFFSET)
568c2ecf20Sopenharmony_ci#define HPRE_CORE_INI_STATUS (HPRE_CLSTR_BASE + HPRE_CORE_INI_STATUS_OFFSET)
578c2ecf20Sopenharmony_ci#define HPRE_HAC_ECC1_CNT		0x301a04
588c2ecf20Sopenharmony_ci#define HPRE_HAC_ECC2_CNT		0x301a08
598c2ecf20Sopenharmony_ci#define HPRE_HAC_INT_STATUS		0x301800
608c2ecf20Sopenharmony_ci#define HPRE_HAC_SOURCE_INT		0x301600
618c2ecf20Sopenharmony_ci#define HPRE_CLSTR_ADDR_INTRVL		0x1000
628c2ecf20Sopenharmony_ci#define HPRE_CLUSTER_INQURY		0x100
638c2ecf20Sopenharmony_ci#define HPRE_CLSTR_ADDR_INQRY_RSLT	0x104
648c2ecf20Sopenharmony_ci#define HPRE_TIMEOUT_ABNML_BIT		6
658c2ecf20Sopenharmony_ci#define HPRE_PASID_EN_BIT		9
668c2ecf20Sopenharmony_ci#define HPRE_REG_RD_INTVRL_US		10
678c2ecf20Sopenharmony_ci#define HPRE_REG_RD_TMOUT_US		1000
688c2ecf20Sopenharmony_ci#define HPRE_DBGFS_VAL_MAX_LEN		20
698c2ecf20Sopenharmony_ci#define HPRE_PCI_DEVICE_ID		0xa258
708c2ecf20Sopenharmony_ci#define HPRE_PCI_VF_DEVICE_ID		0xa259
718c2ecf20Sopenharmony_ci#define HPRE_ADDR(qm, offset)		((qm)->io_base + (offset))
728c2ecf20Sopenharmony_ci#define HPRE_QM_USR_CFG_MASK		0xfffffffe
738c2ecf20Sopenharmony_ci#define HPRE_QM_AXI_CFG_MASK		0xffff
748c2ecf20Sopenharmony_ci#define HPRE_QM_VFG_AX_MASK		0xff
758c2ecf20Sopenharmony_ci#define HPRE_BD_USR_MASK		0x3
768c2ecf20Sopenharmony_ci#define HPRE_CLUSTER_CORE_MASK		0xf
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci#define HPRE_AM_OOO_SHUTDOWN_ENB	0x301044
798c2ecf20Sopenharmony_ci#define HPRE_AM_OOO_SHUTDOWN_ENABLE	BIT(0)
808c2ecf20Sopenharmony_ci#define HPRE_WR_MSI_PORT		BIT(2)
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define HPRE_CORE_ECC_2BIT_ERR		BIT(1)
838c2ecf20Sopenharmony_ci#define HPRE_OOO_ECC_2BIT_ERR		BIT(5)
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#define HPRE_QM_BME_FLR			BIT(7)
868c2ecf20Sopenharmony_ci#define HPRE_QM_PM_FLR			BIT(11)
878c2ecf20Sopenharmony_ci#define HPRE_QM_SRIOV_FLR		BIT(12)
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#define HPRE_VIA_MSI_DSM		1
908c2ecf20Sopenharmony_ci#define HPRE_SQE_MASK_OFFSET		8
918c2ecf20Sopenharmony_ci#define HPRE_SQE_MASK_LEN		24
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic const char hpre_name[] = "hisi_hpre";
948c2ecf20Sopenharmony_cistatic struct dentry *hpre_debugfs_root;
958c2ecf20Sopenharmony_cistatic const struct pci_device_id hpre_dev_ids[] = {
968c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HPRE_PCI_DEVICE_ID) },
978c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HPRE_PCI_VF_DEVICE_ID) },
988c2ecf20Sopenharmony_ci	{ 0, }
998c2ecf20Sopenharmony_ci};
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, hpre_dev_ids);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistruct hpre_hw_error {
1048c2ecf20Sopenharmony_ci	u32 int_msk;
1058c2ecf20Sopenharmony_ci	const char *msg;
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic struct hisi_qm_list hpre_devices = {
1098c2ecf20Sopenharmony_ci	.register_to_crypto	= hpre_algs_register,
1108c2ecf20Sopenharmony_ci	.unregister_from_crypto	= hpre_algs_unregister,
1118c2ecf20Sopenharmony_ci};
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic const char * const hpre_debug_file_name[] = {
1148c2ecf20Sopenharmony_ci	[HPRE_CURRENT_QM]   = "current_qm",
1158c2ecf20Sopenharmony_ci	[HPRE_CLEAR_ENABLE] = "rdclr_en",
1168c2ecf20Sopenharmony_ci	[HPRE_CLUSTER_CTRL] = "cluster_ctrl",
1178c2ecf20Sopenharmony_ci};
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic const struct hpre_hw_error hpre_hw_errors[] = {
1208c2ecf20Sopenharmony_ci	{ .int_msk = BIT(0), .msg = "core_ecc_1bit_err_int_set" },
1218c2ecf20Sopenharmony_ci	{ .int_msk = BIT(1), .msg = "core_ecc_2bit_err_int_set" },
1228c2ecf20Sopenharmony_ci	{ .int_msk = BIT(2), .msg = "dat_wb_poison_int_set" },
1238c2ecf20Sopenharmony_ci	{ .int_msk = BIT(3), .msg = "dat_rd_poison_int_set" },
1248c2ecf20Sopenharmony_ci	{ .int_msk = BIT(4), .msg = "bd_rd_poison_int_set" },
1258c2ecf20Sopenharmony_ci	{ .int_msk = BIT(5), .msg = "ooo_ecc_2bit_err_int_set" },
1268c2ecf20Sopenharmony_ci	{ .int_msk = BIT(6), .msg = "cluster1_shb_timeout_int_set" },
1278c2ecf20Sopenharmony_ci	{ .int_msk = BIT(7), .msg = "cluster2_shb_timeout_int_set" },
1288c2ecf20Sopenharmony_ci	{ .int_msk = BIT(8), .msg = "cluster3_shb_timeout_int_set" },
1298c2ecf20Sopenharmony_ci	{ .int_msk = BIT(9), .msg = "cluster4_shb_timeout_int_set" },
1308c2ecf20Sopenharmony_ci	{ .int_msk = GENMASK(15, 10), .msg = "ooo_rdrsp_err_int_set" },
1318c2ecf20Sopenharmony_ci	{ .int_msk = GENMASK(21, 16), .msg = "ooo_wrrsp_err_int_set" },
1328c2ecf20Sopenharmony_ci	{ /* sentinel */ }
1338c2ecf20Sopenharmony_ci};
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic const u64 hpre_cluster_offsets[] = {
1368c2ecf20Sopenharmony_ci	[HPRE_CLUSTER0] =
1378c2ecf20Sopenharmony_ci		HPRE_CLSTR_BASE + HPRE_CLUSTER0 * HPRE_CLSTR_ADDR_INTRVL,
1388c2ecf20Sopenharmony_ci	[HPRE_CLUSTER1] =
1398c2ecf20Sopenharmony_ci		HPRE_CLSTR_BASE + HPRE_CLUSTER1 * HPRE_CLSTR_ADDR_INTRVL,
1408c2ecf20Sopenharmony_ci	[HPRE_CLUSTER2] =
1418c2ecf20Sopenharmony_ci		HPRE_CLSTR_BASE + HPRE_CLUSTER2 * HPRE_CLSTR_ADDR_INTRVL,
1428c2ecf20Sopenharmony_ci	[HPRE_CLUSTER3] =
1438c2ecf20Sopenharmony_ci		HPRE_CLSTR_BASE + HPRE_CLUSTER3 * HPRE_CLSTR_ADDR_INTRVL,
1448c2ecf20Sopenharmony_ci};
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic const struct debugfs_reg32 hpre_cluster_dfx_regs[] = {
1478c2ecf20Sopenharmony_ci	{"CORES_EN_STATUS          ",  HPRE_CORE_EN_OFFSET},
1488c2ecf20Sopenharmony_ci	{"CORES_INI_CFG              ",  HPRE_CORE_INI_CFG_OFFSET},
1498c2ecf20Sopenharmony_ci	{"CORES_INI_STATUS         ",  HPRE_CORE_INI_STATUS_OFFSET},
1508c2ecf20Sopenharmony_ci	{"CORES_HTBT_WARN         ",  HPRE_CORE_HTBT_WARN_OFFSET},
1518c2ecf20Sopenharmony_ci	{"CORES_IS_SCHD               ",  HPRE_CORE_IS_SCHD_OFFSET},
1528c2ecf20Sopenharmony_ci};
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic const struct debugfs_reg32 hpre_com_dfx_regs[] = {
1558c2ecf20Sopenharmony_ci	{"READ_CLR_EN          ",  HPRE_CTRL_CNT_CLR_CE},
1568c2ecf20Sopenharmony_ci	{"AXQOS                   ",  HPRE_VFG_AXQOS},
1578c2ecf20Sopenharmony_ci	{"AWUSR_CFG              ",  HPRE_AWUSR_FP_CFG},
1588c2ecf20Sopenharmony_ci	{"QM_ARUSR_MCFG1           ",  QM_ARUSER_M_CFG_1},
1598c2ecf20Sopenharmony_ci	{"QM_AWUSR_MCFG1           ",  QM_AWUSER_M_CFG_1},
1608c2ecf20Sopenharmony_ci	{"BD_ENDIAN               ",  HPRE_BD_ENDIAN},
1618c2ecf20Sopenharmony_ci	{"ECC_CHECK_CTRL       ",  HPRE_ECC_BYPASS},
1628c2ecf20Sopenharmony_ci	{"RAS_INT_WIDTH       ",  HPRE_RAS_WIDTH_CFG},
1638c2ecf20Sopenharmony_ci	{"POISON_BYPASS       ",  HPRE_POISON_BYPASS},
1648c2ecf20Sopenharmony_ci	{"BD_ARUSER               ",  HPRE_BD_ARUSR_CFG},
1658c2ecf20Sopenharmony_ci	{"BD_AWUSER               ",  HPRE_BD_AWUSR_CFG},
1668c2ecf20Sopenharmony_ci	{"DATA_ARUSER            ",  HPRE_DATA_RUSER_CFG},
1678c2ecf20Sopenharmony_ci	{"DATA_AWUSER           ",  HPRE_DATA_WUSER_CFG},
1688c2ecf20Sopenharmony_ci	{"INT_STATUS               ",  HPRE_INT_STATUS},
1698c2ecf20Sopenharmony_ci};
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic const char *hpre_dfx_files[HPRE_DFX_FILE_NUM] = {
1728c2ecf20Sopenharmony_ci	"send_cnt",
1738c2ecf20Sopenharmony_ci	"recv_cnt",
1748c2ecf20Sopenharmony_ci	"send_fail_cnt",
1758c2ecf20Sopenharmony_ci	"send_busy_cnt",
1768c2ecf20Sopenharmony_ci	"over_thrhld_cnt",
1778c2ecf20Sopenharmony_ci	"overtime_thrhld",
1788c2ecf20Sopenharmony_ci	"invalid_req_cnt"
1798c2ecf20Sopenharmony_ci};
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic int pf_q_num_set(const char *val, const struct kernel_param *kp)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	return q_num_set(val, kp, HPRE_PCI_DEVICE_ID);
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic const struct kernel_param_ops hpre_pf_q_num_ops = {
1878c2ecf20Sopenharmony_ci	.set = pf_q_num_set,
1888c2ecf20Sopenharmony_ci	.get = param_get_int,
1898c2ecf20Sopenharmony_ci};
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic u32 pf_q_num = HPRE_PF_DEF_Q_NUM;
1928c2ecf20Sopenharmony_cimodule_param_cb(pf_q_num, &hpre_pf_q_num_ops, &pf_q_num, 0444);
1938c2ecf20Sopenharmony_ciMODULE_PARM_DESC(pf_q_num, "Number of queues in PF of CS(2-1024)");
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_cistatic const struct kernel_param_ops vfs_num_ops = {
1968c2ecf20Sopenharmony_ci	.set = vfs_num_set,
1978c2ecf20Sopenharmony_ci	.get = param_get_int,
1988c2ecf20Sopenharmony_ci};
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic u32 vfs_num;
2018c2ecf20Sopenharmony_cimodule_param_cb(vfs_num, &vfs_num_ops, &vfs_num, 0444);
2028c2ecf20Sopenharmony_ciMODULE_PARM_DESC(vfs_num, "Number of VFs to enable(1-63), 0(default)");
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistruct hisi_qp *hpre_create_qp(void)
2058c2ecf20Sopenharmony_ci{
2068c2ecf20Sopenharmony_ci	int node = cpu_to_node(smp_processor_id());
2078c2ecf20Sopenharmony_ci	struct hisi_qp *qp = NULL;
2088c2ecf20Sopenharmony_ci	int ret;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	ret = hisi_qm_alloc_qps_node(&hpre_devices, 1, 0, node, &qp);
2118c2ecf20Sopenharmony_ci	if (!ret)
2128c2ecf20Sopenharmony_ci		return qp;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	return NULL;
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_cistatic int hpre_cfg_by_dsm(struct hisi_qm *qm)
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	struct device *dev = &qm->pdev->dev;
2208c2ecf20Sopenharmony_ci	union acpi_object *obj;
2218c2ecf20Sopenharmony_ci	guid_t guid;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	if (guid_parse("b06b81ab-0134-4a45-9b0c-483447b95fa7", &guid)) {
2248c2ecf20Sopenharmony_ci		dev_err(dev, "Hpre GUID failed\n");
2258c2ecf20Sopenharmony_ci		return -EINVAL;
2268c2ecf20Sopenharmony_ci	}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	/* Switch over to MSI handling due to non-standard PCI implementation */
2298c2ecf20Sopenharmony_ci	obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &guid,
2308c2ecf20Sopenharmony_ci				0, HPRE_VIA_MSI_DSM, NULL);
2318c2ecf20Sopenharmony_ci	if (!obj) {
2328c2ecf20Sopenharmony_ci		dev_err(dev, "ACPI handle failed!\n");
2338c2ecf20Sopenharmony_ci		return -EIO;
2348c2ecf20Sopenharmony_ci	}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	ACPI_FREE(obj);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	return 0;
2398c2ecf20Sopenharmony_ci}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci/*
2428c2ecf20Sopenharmony_ci * For Hi1620, we shoul disable FLR triggered by hardware (BME/PM/SRIOV).
2438c2ecf20Sopenharmony_ci * Or it may stay in D3 state when we bind and unbind hpre quickly,
2448c2ecf20Sopenharmony_ci * as it does FLR triggered by hardware.
2458c2ecf20Sopenharmony_ci */
2468c2ecf20Sopenharmony_cistatic void disable_flr_of_bme(struct hisi_qm *qm)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	u32 val;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	val = readl(HPRE_ADDR(qm, QM_PEH_AXUSER_CFG));
2518c2ecf20Sopenharmony_ci	val &= ~(HPRE_QM_BME_FLR | HPRE_QM_SRIOV_FLR);
2528c2ecf20Sopenharmony_ci	val |= HPRE_QM_PM_FLR;
2538c2ecf20Sopenharmony_ci	writel(val, HPRE_ADDR(qm, QM_PEH_AXUSER_CFG));
2548c2ecf20Sopenharmony_ci	writel(PEH_AXUSER_CFG_ENABLE, HPRE_ADDR(qm, QM_PEH_AXUSER_CFG_ENABLE));
2558c2ecf20Sopenharmony_ci}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistatic int hpre_set_user_domain_and_cache(struct hisi_qm *qm)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	struct device *dev = &qm->pdev->dev;
2608c2ecf20Sopenharmony_ci	unsigned long offset;
2618c2ecf20Sopenharmony_ci	int ret, i;
2628c2ecf20Sopenharmony_ci	u32 val;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	writel(HPRE_QM_USR_CFG_MASK, HPRE_ADDR(qm, QM_ARUSER_M_CFG_ENABLE));
2658c2ecf20Sopenharmony_ci	writel(HPRE_QM_USR_CFG_MASK, HPRE_ADDR(qm, QM_AWUSER_M_CFG_ENABLE));
2668c2ecf20Sopenharmony_ci	writel_relaxed(HPRE_QM_AXI_CFG_MASK, HPRE_ADDR(qm, QM_AXI_M_CFG));
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	/* HPRE need more time, we close this interrupt */
2698c2ecf20Sopenharmony_ci	val = readl_relaxed(HPRE_ADDR(qm, HPRE_QM_ABNML_INT_MASK));
2708c2ecf20Sopenharmony_ci	val |= BIT(HPRE_TIMEOUT_ABNML_BIT);
2718c2ecf20Sopenharmony_ci	writel_relaxed(val, HPRE_ADDR(qm, HPRE_QM_ABNML_INT_MASK));
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	writel(0x1, HPRE_ADDR(qm, HPRE_TYPES_ENB));
2748c2ecf20Sopenharmony_ci	writel(HPRE_QM_VFG_AX_MASK, HPRE_ADDR(qm, HPRE_VFG_AXCACHE));
2758c2ecf20Sopenharmony_ci	writel(0x0, HPRE_ADDR(qm, HPRE_BD_ENDIAN));
2768c2ecf20Sopenharmony_ci	writel(0x0, HPRE_ADDR(qm, HPRE_INT_MASK));
2778c2ecf20Sopenharmony_ci	writel(0x0, HPRE_ADDR(qm, HPRE_RAS_ECC_1BIT_TH));
2788c2ecf20Sopenharmony_ci	writel(0x0, HPRE_ADDR(qm, HPRE_POISON_BYPASS));
2798c2ecf20Sopenharmony_ci	writel(0x0, HPRE_ADDR(qm, HPRE_COMM_CNT_CLR_CE));
2808c2ecf20Sopenharmony_ci	writel(0x0, HPRE_ADDR(qm, HPRE_ECC_BYPASS));
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	writel(HPRE_BD_USR_MASK, HPRE_ADDR(qm, HPRE_BD_ARUSR_CFG));
2838c2ecf20Sopenharmony_ci	writel(HPRE_BD_USR_MASK, HPRE_ADDR(qm, HPRE_BD_AWUSR_CFG));
2848c2ecf20Sopenharmony_ci	writel(0x1, HPRE_ADDR(qm, HPRE_RDCHN_INI_CFG));
2858c2ecf20Sopenharmony_ci	ret = readl_relaxed_poll_timeout(HPRE_ADDR(qm, HPRE_RDCHN_INI_ST), val,
2868c2ecf20Sopenharmony_ci					 val & BIT(0),
2878c2ecf20Sopenharmony_ci			HPRE_REG_RD_INTVRL_US,
2888c2ecf20Sopenharmony_ci			HPRE_REG_RD_TMOUT_US);
2898c2ecf20Sopenharmony_ci	if (ret) {
2908c2ecf20Sopenharmony_ci		dev_err(dev, "read rd channel timeout fail!\n");
2918c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
2928c2ecf20Sopenharmony_ci	}
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	for (i = 0; i < HPRE_CLUSTERS_NUM; i++) {
2958c2ecf20Sopenharmony_ci		offset = i * HPRE_CLSTR_ADDR_INTRVL;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci		/* clusters initiating */
2988c2ecf20Sopenharmony_ci		writel(HPRE_CLUSTER_CORE_MASK,
2998c2ecf20Sopenharmony_ci		       HPRE_ADDR(qm, offset + HPRE_CORE_ENB));
3008c2ecf20Sopenharmony_ci		writel(0x1, HPRE_ADDR(qm, offset + HPRE_CORE_INI_CFG));
3018c2ecf20Sopenharmony_ci		ret = readl_relaxed_poll_timeout(HPRE_ADDR(qm, offset +
3028c2ecf20Sopenharmony_ci					HPRE_CORE_INI_STATUS), val,
3038c2ecf20Sopenharmony_ci					((val & HPRE_CLUSTER_CORE_MASK) ==
3048c2ecf20Sopenharmony_ci					HPRE_CLUSTER_CORE_MASK),
3058c2ecf20Sopenharmony_ci					HPRE_REG_RD_INTVRL_US,
3068c2ecf20Sopenharmony_ci					HPRE_REG_RD_TMOUT_US);
3078c2ecf20Sopenharmony_ci		if (ret) {
3088c2ecf20Sopenharmony_ci			dev_err(dev,
3098c2ecf20Sopenharmony_ci				"cluster %d int st status timeout!\n", i);
3108c2ecf20Sopenharmony_ci			return -ETIMEDOUT;
3118c2ecf20Sopenharmony_ci		}
3128c2ecf20Sopenharmony_ci	}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	ret = hpre_cfg_by_dsm(qm);
3158c2ecf20Sopenharmony_ci	if (ret)
3168c2ecf20Sopenharmony_ci		dev_err(dev, "acpi_evaluate_dsm err.\n");
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	disable_flr_of_bme(qm);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	return ret;
3218c2ecf20Sopenharmony_ci}
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_cistatic void hpre_cnt_regs_clear(struct hisi_qm *qm)
3248c2ecf20Sopenharmony_ci{
3258c2ecf20Sopenharmony_ci	unsigned long offset;
3268c2ecf20Sopenharmony_ci	int i;
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	/* clear current_qm */
3298c2ecf20Sopenharmony_ci	writel(0x0, qm->io_base + QM_DFX_MB_CNT_VF);
3308c2ecf20Sopenharmony_ci	writel(0x0, qm->io_base + QM_DFX_DB_CNT_VF);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	/* clear clusterX/cluster_ctrl */
3338c2ecf20Sopenharmony_ci	for (i = 0; i < HPRE_CLUSTERS_NUM; i++) {
3348c2ecf20Sopenharmony_ci		offset = HPRE_CLSTR_BASE + i * HPRE_CLSTR_ADDR_INTRVL;
3358c2ecf20Sopenharmony_ci		writel(0x0, qm->io_base + offset + HPRE_CLUSTER_INQURY);
3368c2ecf20Sopenharmony_ci	}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	/* clear rdclr_en */
3398c2ecf20Sopenharmony_ci	writel(0x0, qm->io_base + HPRE_CTRL_CNT_CLR_CE);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	hisi_qm_debug_regs_clear(qm);
3428c2ecf20Sopenharmony_ci}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_cistatic void hpre_hw_error_disable(struct hisi_qm *qm)
3458c2ecf20Sopenharmony_ci{
3468c2ecf20Sopenharmony_ci	u32 val;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	/* disable hpre hw error interrupts */
3498c2ecf20Sopenharmony_ci	writel(HPRE_CORE_INT_DISABLE, qm->io_base + HPRE_INT_MASK);
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	/* disable HPRE block master OOO when m-bit error occur */
3528c2ecf20Sopenharmony_ci	val = readl(qm->io_base + HPRE_AM_OOO_SHUTDOWN_ENB);
3538c2ecf20Sopenharmony_ci	val &= ~HPRE_AM_OOO_SHUTDOWN_ENABLE;
3548c2ecf20Sopenharmony_ci	writel(val, qm->io_base + HPRE_AM_OOO_SHUTDOWN_ENB);
3558c2ecf20Sopenharmony_ci}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_cistatic void hpre_hw_error_enable(struct hisi_qm *qm)
3588c2ecf20Sopenharmony_ci{
3598c2ecf20Sopenharmony_ci	u32 val;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	/* clear HPRE hw error source if having */
3628c2ecf20Sopenharmony_ci	writel(HPRE_CORE_INT_DISABLE, qm->io_base + HPRE_HAC_SOURCE_INT);
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	/* enable hpre hw error interrupts */
3658c2ecf20Sopenharmony_ci	writel(HPRE_CORE_INT_ENABLE, qm->io_base + HPRE_INT_MASK);
3668c2ecf20Sopenharmony_ci	writel(HPRE_HAC_RAS_CE_ENABLE, qm->io_base + HPRE_RAS_CE_ENB);
3678c2ecf20Sopenharmony_ci	writel(HPRE_HAC_RAS_NFE_ENABLE, qm->io_base + HPRE_RAS_NFE_ENB);
3688c2ecf20Sopenharmony_ci	writel(HPRE_HAC_RAS_FE_ENABLE, qm->io_base + HPRE_RAS_FE_ENB);
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	/* enable HPRE block master OOO when m-bit error occur */
3718c2ecf20Sopenharmony_ci	val = readl(qm->io_base + HPRE_AM_OOO_SHUTDOWN_ENB);
3728c2ecf20Sopenharmony_ci	val |= HPRE_AM_OOO_SHUTDOWN_ENABLE;
3738c2ecf20Sopenharmony_ci	writel(val, qm->io_base + HPRE_AM_OOO_SHUTDOWN_ENB);
3748c2ecf20Sopenharmony_ci}
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_cistatic inline struct hisi_qm *hpre_file_to_qm(struct hpre_debugfs_file *file)
3778c2ecf20Sopenharmony_ci{
3788c2ecf20Sopenharmony_ci	struct hpre *hpre = container_of(file->debug, struct hpre, debug);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	return &hpre->qm;
3818c2ecf20Sopenharmony_ci}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_cistatic u32 hpre_current_qm_read(struct hpre_debugfs_file *file)
3848c2ecf20Sopenharmony_ci{
3858c2ecf20Sopenharmony_ci	struct hisi_qm *qm = hpre_file_to_qm(file);
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	return readl(qm->io_base + QM_DFX_MB_CNT_VF);
3888c2ecf20Sopenharmony_ci}
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_cistatic int hpre_current_qm_write(struct hpre_debugfs_file *file, u32 val)
3918c2ecf20Sopenharmony_ci{
3928c2ecf20Sopenharmony_ci	struct hisi_qm *qm = hpre_file_to_qm(file);
3938c2ecf20Sopenharmony_ci	u32 num_vfs = qm->vfs_num;
3948c2ecf20Sopenharmony_ci	u32 vfq_num, tmp;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	if (val > num_vfs)
3978c2ecf20Sopenharmony_ci		return -EINVAL;
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	/* According PF or VF Dev ID to calculation curr_qm_qp_num and store */
4008c2ecf20Sopenharmony_ci	if (val == 0) {
4018c2ecf20Sopenharmony_ci		qm->debug.curr_qm_qp_num = qm->qp_num;
4028c2ecf20Sopenharmony_ci	} else {
4038c2ecf20Sopenharmony_ci		vfq_num = (qm->ctrl_qp_num - qm->qp_num) / num_vfs;
4048c2ecf20Sopenharmony_ci		if (val == num_vfs) {
4058c2ecf20Sopenharmony_ci			qm->debug.curr_qm_qp_num =
4068c2ecf20Sopenharmony_ci			qm->ctrl_qp_num - qm->qp_num - (num_vfs - 1) * vfq_num;
4078c2ecf20Sopenharmony_ci		} else {
4088c2ecf20Sopenharmony_ci			qm->debug.curr_qm_qp_num = vfq_num;
4098c2ecf20Sopenharmony_ci		}
4108c2ecf20Sopenharmony_ci	}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	writel(val, qm->io_base + QM_DFX_MB_CNT_VF);
4138c2ecf20Sopenharmony_ci	writel(val, qm->io_base + QM_DFX_DB_CNT_VF);
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	tmp = val |
4168c2ecf20Sopenharmony_ci	      (readl(qm->io_base + QM_DFX_SQE_CNT_VF_SQN) & CURRENT_Q_MASK);
4178c2ecf20Sopenharmony_ci	writel(tmp, qm->io_base + QM_DFX_SQE_CNT_VF_SQN);
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	tmp = val |
4208c2ecf20Sopenharmony_ci	      (readl(qm->io_base + QM_DFX_CQE_CNT_VF_CQN) & CURRENT_Q_MASK);
4218c2ecf20Sopenharmony_ci	writel(tmp, qm->io_base + QM_DFX_CQE_CNT_VF_CQN);
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	return  0;
4248c2ecf20Sopenharmony_ci}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_cistatic u32 hpre_clear_enable_read(struct hpre_debugfs_file *file)
4278c2ecf20Sopenharmony_ci{
4288c2ecf20Sopenharmony_ci	struct hisi_qm *qm = hpre_file_to_qm(file);
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	return readl(qm->io_base + HPRE_CTRL_CNT_CLR_CE) &
4318c2ecf20Sopenharmony_ci	       HPRE_CTRL_CNT_CLR_CE_BIT;
4328c2ecf20Sopenharmony_ci}
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_cistatic int hpre_clear_enable_write(struct hpre_debugfs_file *file, u32 val)
4358c2ecf20Sopenharmony_ci{
4368c2ecf20Sopenharmony_ci	struct hisi_qm *qm = hpre_file_to_qm(file);
4378c2ecf20Sopenharmony_ci	u32 tmp;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	if (val != 1 && val != 0)
4408c2ecf20Sopenharmony_ci		return -EINVAL;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	tmp = (readl(qm->io_base + HPRE_CTRL_CNT_CLR_CE) &
4438c2ecf20Sopenharmony_ci	       ~HPRE_CTRL_CNT_CLR_CE_BIT) | val;
4448c2ecf20Sopenharmony_ci	writel(tmp, qm->io_base + HPRE_CTRL_CNT_CLR_CE);
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	return  0;
4478c2ecf20Sopenharmony_ci}
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_cistatic u32 hpre_cluster_inqry_read(struct hpre_debugfs_file *file)
4508c2ecf20Sopenharmony_ci{
4518c2ecf20Sopenharmony_ci	struct hisi_qm *qm = hpre_file_to_qm(file);
4528c2ecf20Sopenharmony_ci	int cluster_index = file->index - HPRE_CLUSTER_CTRL;
4538c2ecf20Sopenharmony_ci	unsigned long offset = HPRE_CLSTR_BASE +
4548c2ecf20Sopenharmony_ci			       cluster_index * HPRE_CLSTR_ADDR_INTRVL;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	return readl(qm->io_base + offset + HPRE_CLSTR_ADDR_INQRY_RSLT);
4578c2ecf20Sopenharmony_ci}
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_cistatic int hpre_cluster_inqry_write(struct hpre_debugfs_file *file, u32 val)
4608c2ecf20Sopenharmony_ci{
4618c2ecf20Sopenharmony_ci	struct hisi_qm *qm = hpre_file_to_qm(file);
4628c2ecf20Sopenharmony_ci	int cluster_index = file->index - HPRE_CLUSTER_CTRL;
4638c2ecf20Sopenharmony_ci	unsigned long offset = HPRE_CLSTR_BASE + cluster_index *
4648c2ecf20Sopenharmony_ci			       HPRE_CLSTR_ADDR_INTRVL;
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	writel(val, qm->io_base + offset + HPRE_CLUSTER_INQURY);
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	return  0;
4698c2ecf20Sopenharmony_ci}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_cistatic ssize_t hpre_ctrl_debug_read(struct file *filp, char __user *buf,
4728c2ecf20Sopenharmony_ci				    size_t count, loff_t *pos)
4738c2ecf20Sopenharmony_ci{
4748c2ecf20Sopenharmony_ci	struct hpre_debugfs_file *file = filp->private_data;
4758c2ecf20Sopenharmony_ci	char tbuf[HPRE_DBGFS_VAL_MAX_LEN];
4768c2ecf20Sopenharmony_ci	u32 val;
4778c2ecf20Sopenharmony_ci	int ret;
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	spin_lock_irq(&file->lock);
4808c2ecf20Sopenharmony_ci	switch (file->type) {
4818c2ecf20Sopenharmony_ci	case HPRE_CURRENT_QM:
4828c2ecf20Sopenharmony_ci		val = hpre_current_qm_read(file);
4838c2ecf20Sopenharmony_ci		break;
4848c2ecf20Sopenharmony_ci	case HPRE_CLEAR_ENABLE:
4858c2ecf20Sopenharmony_ci		val = hpre_clear_enable_read(file);
4868c2ecf20Sopenharmony_ci		break;
4878c2ecf20Sopenharmony_ci	case HPRE_CLUSTER_CTRL:
4888c2ecf20Sopenharmony_ci		val = hpre_cluster_inqry_read(file);
4898c2ecf20Sopenharmony_ci		break;
4908c2ecf20Sopenharmony_ci	default:
4918c2ecf20Sopenharmony_ci		spin_unlock_irq(&file->lock);
4928c2ecf20Sopenharmony_ci		return -EINVAL;
4938c2ecf20Sopenharmony_ci	}
4948c2ecf20Sopenharmony_ci	spin_unlock_irq(&file->lock);
4958c2ecf20Sopenharmony_ci	ret = snprintf(tbuf, HPRE_DBGFS_VAL_MAX_LEN, "%u\n", val);
4968c2ecf20Sopenharmony_ci	return simple_read_from_buffer(buf, count, pos, tbuf, ret);
4978c2ecf20Sopenharmony_ci}
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_cistatic ssize_t hpre_ctrl_debug_write(struct file *filp, const char __user *buf,
5008c2ecf20Sopenharmony_ci				     size_t count, loff_t *pos)
5018c2ecf20Sopenharmony_ci{
5028c2ecf20Sopenharmony_ci	struct hpre_debugfs_file *file = filp->private_data;
5038c2ecf20Sopenharmony_ci	char tbuf[HPRE_DBGFS_VAL_MAX_LEN];
5048c2ecf20Sopenharmony_ci	unsigned long val;
5058c2ecf20Sopenharmony_ci	int len, ret;
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	if (*pos != 0)
5088c2ecf20Sopenharmony_ci		return 0;
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	if (count >= HPRE_DBGFS_VAL_MAX_LEN)
5118c2ecf20Sopenharmony_ci		return -ENOSPC;
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	len = simple_write_to_buffer(tbuf, HPRE_DBGFS_VAL_MAX_LEN - 1,
5148c2ecf20Sopenharmony_ci				     pos, buf, count);
5158c2ecf20Sopenharmony_ci	if (len < 0)
5168c2ecf20Sopenharmony_ci		return len;
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	tbuf[len] = '\0';
5198c2ecf20Sopenharmony_ci	if (kstrtoul(tbuf, 0, &val))
5208c2ecf20Sopenharmony_ci		return -EFAULT;
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	spin_lock_irq(&file->lock);
5238c2ecf20Sopenharmony_ci	switch (file->type) {
5248c2ecf20Sopenharmony_ci	case HPRE_CURRENT_QM:
5258c2ecf20Sopenharmony_ci		ret = hpre_current_qm_write(file, val);
5268c2ecf20Sopenharmony_ci		if (ret)
5278c2ecf20Sopenharmony_ci			goto err_input;
5288c2ecf20Sopenharmony_ci		break;
5298c2ecf20Sopenharmony_ci	case HPRE_CLEAR_ENABLE:
5308c2ecf20Sopenharmony_ci		ret = hpre_clear_enable_write(file, val);
5318c2ecf20Sopenharmony_ci		if (ret)
5328c2ecf20Sopenharmony_ci			goto err_input;
5338c2ecf20Sopenharmony_ci		break;
5348c2ecf20Sopenharmony_ci	case HPRE_CLUSTER_CTRL:
5358c2ecf20Sopenharmony_ci		ret = hpre_cluster_inqry_write(file, val);
5368c2ecf20Sopenharmony_ci		if (ret)
5378c2ecf20Sopenharmony_ci			goto err_input;
5388c2ecf20Sopenharmony_ci		break;
5398c2ecf20Sopenharmony_ci	default:
5408c2ecf20Sopenharmony_ci		ret = -EINVAL;
5418c2ecf20Sopenharmony_ci		goto err_input;
5428c2ecf20Sopenharmony_ci	}
5438c2ecf20Sopenharmony_ci	spin_unlock_irq(&file->lock);
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	return count;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_cierr_input:
5488c2ecf20Sopenharmony_ci	spin_unlock_irq(&file->lock);
5498c2ecf20Sopenharmony_ci	return ret;
5508c2ecf20Sopenharmony_ci}
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_cistatic const struct file_operations hpre_ctrl_debug_fops = {
5538c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
5548c2ecf20Sopenharmony_ci	.open = simple_open,
5558c2ecf20Sopenharmony_ci	.read = hpre_ctrl_debug_read,
5568c2ecf20Sopenharmony_ci	.write = hpre_ctrl_debug_write,
5578c2ecf20Sopenharmony_ci};
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_cistatic int hpre_debugfs_atomic64_get(void *data, u64 *val)
5608c2ecf20Sopenharmony_ci{
5618c2ecf20Sopenharmony_ci	struct hpre_dfx *dfx_item = data;
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	*val = atomic64_read(&dfx_item->value);
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	return 0;
5668c2ecf20Sopenharmony_ci}
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_cistatic int hpre_debugfs_atomic64_set(void *data, u64 val)
5698c2ecf20Sopenharmony_ci{
5708c2ecf20Sopenharmony_ci	struct hpre_dfx *dfx_item = data;
5718c2ecf20Sopenharmony_ci	struct hpre_dfx *hpre_dfx = NULL;
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	if (dfx_item->type == HPRE_OVERTIME_THRHLD) {
5748c2ecf20Sopenharmony_ci		hpre_dfx = dfx_item - HPRE_OVERTIME_THRHLD;
5758c2ecf20Sopenharmony_ci		atomic64_set(&hpre_dfx[HPRE_OVER_THRHLD_CNT].value, 0);
5768c2ecf20Sopenharmony_ci	} else if (val) {
5778c2ecf20Sopenharmony_ci		return -EINVAL;
5788c2ecf20Sopenharmony_ci	}
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	atomic64_set(&dfx_item->value, val);
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	return 0;
5838c2ecf20Sopenharmony_ci}
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(hpre_atomic64_ops, hpre_debugfs_atomic64_get,
5868c2ecf20Sopenharmony_ci			 hpre_debugfs_atomic64_set, "%llu\n");
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_cistatic int hpre_create_debugfs_file(struct hisi_qm *qm, struct dentry *dir,
5898c2ecf20Sopenharmony_ci				    enum hpre_ctrl_dbgfs_file type, int indx)
5908c2ecf20Sopenharmony_ci{
5918c2ecf20Sopenharmony_ci	struct hpre *hpre = container_of(qm, struct hpre, qm);
5928c2ecf20Sopenharmony_ci	struct hpre_debug *dbg = &hpre->debug;
5938c2ecf20Sopenharmony_ci	struct dentry *file_dir;
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	if (dir)
5968c2ecf20Sopenharmony_ci		file_dir = dir;
5978c2ecf20Sopenharmony_ci	else
5988c2ecf20Sopenharmony_ci		file_dir = qm->debug.debug_root;
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	if (type >= HPRE_DEBUG_FILE_NUM)
6018c2ecf20Sopenharmony_ci		return -EINVAL;
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci	spin_lock_init(&dbg->files[indx].lock);
6048c2ecf20Sopenharmony_ci	dbg->files[indx].debug = dbg;
6058c2ecf20Sopenharmony_ci	dbg->files[indx].type = type;
6068c2ecf20Sopenharmony_ci	dbg->files[indx].index = indx;
6078c2ecf20Sopenharmony_ci	debugfs_create_file(hpre_debug_file_name[type], 0600, file_dir,
6088c2ecf20Sopenharmony_ci			    dbg->files + indx, &hpre_ctrl_debug_fops);
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	return 0;
6118c2ecf20Sopenharmony_ci}
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_cistatic int hpre_pf_comm_regs_debugfs_init(struct hisi_qm *qm)
6148c2ecf20Sopenharmony_ci{
6158c2ecf20Sopenharmony_ci	struct device *dev = &qm->pdev->dev;
6168c2ecf20Sopenharmony_ci	struct debugfs_regset32 *regset;
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
6198c2ecf20Sopenharmony_ci	if (!regset)
6208c2ecf20Sopenharmony_ci		return -ENOMEM;
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	regset->regs = hpre_com_dfx_regs;
6238c2ecf20Sopenharmony_ci	regset->nregs = ARRAY_SIZE(hpre_com_dfx_regs);
6248c2ecf20Sopenharmony_ci	regset->base = qm->io_base;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	debugfs_create_regset32("regs", 0444,  qm->debug.debug_root, regset);
6278c2ecf20Sopenharmony_ci	return 0;
6288c2ecf20Sopenharmony_ci}
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_cistatic int hpre_cluster_debugfs_init(struct hisi_qm *qm)
6318c2ecf20Sopenharmony_ci{
6328c2ecf20Sopenharmony_ci	struct device *dev = &qm->pdev->dev;
6338c2ecf20Sopenharmony_ci	char buf[HPRE_DBGFS_VAL_MAX_LEN];
6348c2ecf20Sopenharmony_ci	struct debugfs_regset32 *regset;
6358c2ecf20Sopenharmony_ci	struct dentry *tmp_d;
6368c2ecf20Sopenharmony_ci	int i, ret;
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	for (i = 0; i < HPRE_CLUSTERS_NUM; i++) {
6398c2ecf20Sopenharmony_ci		ret = snprintf(buf, HPRE_DBGFS_VAL_MAX_LEN, "cluster%d", i);
6408c2ecf20Sopenharmony_ci		if (ret >= HPRE_DBGFS_VAL_MAX_LEN)
6418c2ecf20Sopenharmony_ci			return -EINVAL;
6428c2ecf20Sopenharmony_ci		tmp_d = debugfs_create_dir(buf, qm->debug.debug_root);
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci		regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
6458c2ecf20Sopenharmony_ci		if (!regset)
6468c2ecf20Sopenharmony_ci			return -ENOMEM;
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci		regset->regs = hpre_cluster_dfx_regs;
6498c2ecf20Sopenharmony_ci		regset->nregs = ARRAY_SIZE(hpre_cluster_dfx_regs);
6508c2ecf20Sopenharmony_ci		regset->base = qm->io_base + hpre_cluster_offsets[i];
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci		debugfs_create_regset32("regs", 0444, tmp_d, regset);
6538c2ecf20Sopenharmony_ci		ret = hpre_create_debugfs_file(qm, tmp_d, HPRE_CLUSTER_CTRL,
6548c2ecf20Sopenharmony_ci					       i + HPRE_CLUSTER_CTRL);
6558c2ecf20Sopenharmony_ci		if (ret)
6568c2ecf20Sopenharmony_ci			return ret;
6578c2ecf20Sopenharmony_ci	}
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	return 0;
6608c2ecf20Sopenharmony_ci}
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_cistatic int hpre_ctrl_debug_init(struct hisi_qm *qm)
6638c2ecf20Sopenharmony_ci{
6648c2ecf20Sopenharmony_ci	int ret;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	ret = hpre_create_debugfs_file(qm, NULL, HPRE_CURRENT_QM,
6678c2ecf20Sopenharmony_ci				       HPRE_CURRENT_QM);
6688c2ecf20Sopenharmony_ci	if (ret)
6698c2ecf20Sopenharmony_ci		return ret;
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	ret = hpre_create_debugfs_file(qm, NULL, HPRE_CLEAR_ENABLE,
6728c2ecf20Sopenharmony_ci				       HPRE_CLEAR_ENABLE);
6738c2ecf20Sopenharmony_ci	if (ret)
6748c2ecf20Sopenharmony_ci		return ret;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	ret = hpre_pf_comm_regs_debugfs_init(qm);
6778c2ecf20Sopenharmony_ci	if (ret)
6788c2ecf20Sopenharmony_ci		return ret;
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	return hpre_cluster_debugfs_init(qm);
6818c2ecf20Sopenharmony_ci}
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_cistatic void hpre_dfx_debug_init(struct hisi_qm *qm)
6848c2ecf20Sopenharmony_ci{
6858c2ecf20Sopenharmony_ci	struct hpre *hpre = container_of(qm, struct hpre, qm);
6868c2ecf20Sopenharmony_ci	struct hpre_dfx *dfx = hpre->debug.dfx;
6878c2ecf20Sopenharmony_ci	struct dentry *parent;
6888c2ecf20Sopenharmony_ci	int i;
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	parent = debugfs_create_dir("hpre_dfx", qm->debug.debug_root);
6918c2ecf20Sopenharmony_ci	for (i = 0; i < HPRE_DFX_FILE_NUM; i++) {
6928c2ecf20Sopenharmony_ci		dfx[i].type = i;
6938c2ecf20Sopenharmony_ci		debugfs_create_file(hpre_dfx_files[i], 0644, parent, &dfx[i],
6948c2ecf20Sopenharmony_ci				    &hpre_atomic64_ops);
6958c2ecf20Sopenharmony_ci	}
6968c2ecf20Sopenharmony_ci}
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_cistatic int hpre_debugfs_init(struct hisi_qm *qm)
6998c2ecf20Sopenharmony_ci{
7008c2ecf20Sopenharmony_ci	struct device *dev = &qm->pdev->dev;
7018c2ecf20Sopenharmony_ci	int ret;
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	qm->debug.debug_root = debugfs_create_dir(dev_name(dev),
7048c2ecf20Sopenharmony_ci						  hpre_debugfs_root);
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	qm->debug.sqe_mask_offset = HPRE_SQE_MASK_OFFSET;
7078c2ecf20Sopenharmony_ci	qm->debug.sqe_mask_len = HPRE_SQE_MASK_LEN;
7088c2ecf20Sopenharmony_ci	ret = hisi_qm_debug_init(qm);
7098c2ecf20Sopenharmony_ci	if (ret)
7108c2ecf20Sopenharmony_ci		goto failed_to_create;
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	if (qm->pdev->device == HPRE_PCI_DEVICE_ID) {
7138c2ecf20Sopenharmony_ci		ret = hpre_ctrl_debug_init(qm);
7148c2ecf20Sopenharmony_ci		if (ret)
7158c2ecf20Sopenharmony_ci			goto failed_to_create;
7168c2ecf20Sopenharmony_ci	}
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	hpre_dfx_debug_init(qm);
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	return 0;
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_cifailed_to_create:
7238c2ecf20Sopenharmony_ci	debugfs_remove_recursive(qm->debug.debug_root);
7248c2ecf20Sopenharmony_ci	return ret;
7258c2ecf20Sopenharmony_ci}
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_cistatic void hpre_debugfs_exit(struct hisi_qm *qm)
7288c2ecf20Sopenharmony_ci{
7298c2ecf20Sopenharmony_ci	debugfs_remove_recursive(qm->debug.debug_root);
7308c2ecf20Sopenharmony_ci}
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_cistatic int hpre_qm_init(struct hisi_qm *qm, struct pci_dev *pdev)
7338c2ecf20Sopenharmony_ci{
7348c2ecf20Sopenharmony_ci	if (pdev->revision == QM_HW_V1) {
7358c2ecf20Sopenharmony_ci		pci_warn(pdev, "HPRE version 1 is not supported!\n");
7368c2ecf20Sopenharmony_ci		return -EINVAL;
7378c2ecf20Sopenharmony_ci	}
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	qm->pdev = pdev;
7408c2ecf20Sopenharmony_ci	qm->ver = pdev->revision;
7418c2ecf20Sopenharmony_ci	qm->sqe_size = HPRE_SQE_SIZE;
7428c2ecf20Sopenharmony_ci	qm->dev_name = hpre_name;
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	qm->fun_type = (pdev->device == HPRE_PCI_DEVICE_ID) ?
7458c2ecf20Sopenharmony_ci			QM_HW_PF : QM_HW_VF;
7468c2ecf20Sopenharmony_ci	if (qm->fun_type == QM_HW_PF) {
7478c2ecf20Sopenharmony_ci		qm->qp_base = HPRE_PF_DEF_Q_BASE;
7488c2ecf20Sopenharmony_ci		qm->qp_num = pf_q_num;
7498c2ecf20Sopenharmony_ci		qm->debug.curr_qm_qp_num = pf_q_num;
7508c2ecf20Sopenharmony_ci		qm->qm_list = &hpre_devices;
7518c2ecf20Sopenharmony_ci	}
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci	return hisi_qm_init(qm);
7548c2ecf20Sopenharmony_ci}
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_cistatic void hpre_log_hw_error(struct hisi_qm *qm, u32 err_sts)
7578c2ecf20Sopenharmony_ci{
7588c2ecf20Sopenharmony_ci	const struct hpre_hw_error *err = hpre_hw_errors;
7598c2ecf20Sopenharmony_ci	struct device *dev = &qm->pdev->dev;
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	while (err->msg) {
7628c2ecf20Sopenharmony_ci		if (err->int_msk & err_sts)
7638c2ecf20Sopenharmony_ci			dev_warn(dev, "%s [error status=0x%x] found\n",
7648c2ecf20Sopenharmony_ci				 err->msg, err->int_msk);
7658c2ecf20Sopenharmony_ci		err++;
7668c2ecf20Sopenharmony_ci	}
7678c2ecf20Sopenharmony_ci}
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_cistatic u32 hpre_get_hw_err_status(struct hisi_qm *qm)
7708c2ecf20Sopenharmony_ci{
7718c2ecf20Sopenharmony_ci	return readl(qm->io_base + HPRE_HAC_INT_STATUS);
7728c2ecf20Sopenharmony_ci}
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_cistatic void hpre_clear_hw_err_status(struct hisi_qm *qm, u32 err_sts)
7758c2ecf20Sopenharmony_ci{
7768c2ecf20Sopenharmony_ci	writel(err_sts, qm->io_base + HPRE_HAC_SOURCE_INT);
7778c2ecf20Sopenharmony_ci}
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_cistatic void hpre_open_axi_master_ooo(struct hisi_qm *qm)
7808c2ecf20Sopenharmony_ci{
7818c2ecf20Sopenharmony_ci	u32 value;
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	value = readl(qm->io_base + HPRE_AM_OOO_SHUTDOWN_ENB);
7848c2ecf20Sopenharmony_ci	writel(value & ~HPRE_AM_OOO_SHUTDOWN_ENABLE,
7858c2ecf20Sopenharmony_ci	       HPRE_ADDR(qm, HPRE_AM_OOO_SHUTDOWN_ENB));
7868c2ecf20Sopenharmony_ci	writel(value | HPRE_AM_OOO_SHUTDOWN_ENABLE,
7878c2ecf20Sopenharmony_ci	       HPRE_ADDR(qm, HPRE_AM_OOO_SHUTDOWN_ENB));
7888c2ecf20Sopenharmony_ci}
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_cistatic const struct hisi_qm_err_ini hpre_err_ini = {
7918c2ecf20Sopenharmony_ci	.hw_init		= hpre_set_user_domain_and_cache,
7928c2ecf20Sopenharmony_ci	.hw_err_enable		= hpre_hw_error_enable,
7938c2ecf20Sopenharmony_ci	.hw_err_disable		= hpre_hw_error_disable,
7948c2ecf20Sopenharmony_ci	.get_dev_hw_err_status	= hpre_get_hw_err_status,
7958c2ecf20Sopenharmony_ci	.clear_dev_hw_err_status = hpre_clear_hw_err_status,
7968c2ecf20Sopenharmony_ci	.log_dev_hw_err		= hpre_log_hw_error,
7978c2ecf20Sopenharmony_ci	.open_axi_master_ooo	= hpre_open_axi_master_ooo,
7988c2ecf20Sopenharmony_ci	.err_info		= {
7998c2ecf20Sopenharmony_ci		.ce			= QM_BASE_CE,
8008c2ecf20Sopenharmony_ci		.nfe			= QM_BASE_NFE | QM_ACC_DO_TASK_TIMEOUT,
8018c2ecf20Sopenharmony_ci		.fe			= 0,
8028c2ecf20Sopenharmony_ci		.ecc_2bits_mask		= HPRE_CORE_ECC_2BIT_ERR |
8038c2ecf20Sopenharmony_ci					  HPRE_OOO_ECC_2BIT_ERR,
8048c2ecf20Sopenharmony_ci		.msi_wr_port		= HPRE_WR_MSI_PORT,
8058c2ecf20Sopenharmony_ci		.acpi_rst		= "HRST",
8068c2ecf20Sopenharmony_ci	}
8078c2ecf20Sopenharmony_ci};
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_cistatic int hpre_pf_probe_init(struct hpre *hpre)
8108c2ecf20Sopenharmony_ci{
8118c2ecf20Sopenharmony_ci	struct hisi_qm *qm = &hpre->qm;
8128c2ecf20Sopenharmony_ci	int ret;
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	qm->ctrl_qp_num = HPRE_QUEUE_NUM_V2;
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	ret = hpre_set_user_domain_and_cache(qm);
8178c2ecf20Sopenharmony_ci	if (ret)
8188c2ecf20Sopenharmony_ci		return ret;
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_ci	qm->err_ini = &hpre_err_ini;
8218c2ecf20Sopenharmony_ci	hisi_qm_dev_err_init(qm);
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	return 0;
8248c2ecf20Sopenharmony_ci}
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_cistatic int hpre_probe_init(struct hpre *hpre)
8278c2ecf20Sopenharmony_ci{
8288c2ecf20Sopenharmony_ci	struct hisi_qm *qm = &hpre->qm;
8298c2ecf20Sopenharmony_ci	int ret;
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	if (qm->fun_type == QM_HW_PF) {
8328c2ecf20Sopenharmony_ci		ret = hpre_pf_probe_init(hpre);
8338c2ecf20Sopenharmony_ci		if (ret)
8348c2ecf20Sopenharmony_ci			return ret;
8358c2ecf20Sopenharmony_ci	}
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	return 0;
8388c2ecf20Sopenharmony_ci}
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_cistatic int hpre_probe(struct pci_dev *pdev, const struct pci_device_id *id)
8418c2ecf20Sopenharmony_ci{
8428c2ecf20Sopenharmony_ci	struct hisi_qm *qm;
8438c2ecf20Sopenharmony_ci	struct hpre *hpre;
8448c2ecf20Sopenharmony_ci	int ret;
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	hpre = devm_kzalloc(&pdev->dev, sizeof(*hpre), GFP_KERNEL);
8478c2ecf20Sopenharmony_ci	if (!hpre)
8488c2ecf20Sopenharmony_ci		return -ENOMEM;
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	qm = &hpre->qm;
8518c2ecf20Sopenharmony_ci	ret = hpre_qm_init(qm, pdev);
8528c2ecf20Sopenharmony_ci	if (ret) {
8538c2ecf20Sopenharmony_ci		pci_err(pdev, "Failed to init HPRE QM (%d)!\n", ret);
8548c2ecf20Sopenharmony_ci		return ret;
8558c2ecf20Sopenharmony_ci	}
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	ret = hpre_probe_init(hpre);
8588c2ecf20Sopenharmony_ci	if (ret) {
8598c2ecf20Sopenharmony_ci		pci_err(pdev, "Failed to probe (%d)!\n", ret);
8608c2ecf20Sopenharmony_ci		goto err_with_qm_init;
8618c2ecf20Sopenharmony_ci	}
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	ret = hisi_qm_start(qm);
8648c2ecf20Sopenharmony_ci	if (ret)
8658c2ecf20Sopenharmony_ci		goto err_with_err_init;
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci	ret = hpre_debugfs_init(qm);
8688c2ecf20Sopenharmony_ci	if (ret)
8698c2ecf20Sopenharmony_ci		dev_warn(&pdev->dev, "init debugfs fail!\n");
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	ret = hisi_qm_alg_register(qm, &hpre_devices);
8728c2ecf20Sopenharmony_ci	if (ret < 0) {
8738c2ecf20Sopenharmony_ci		pci_err(pdev, "fail to register algs to crypto!\n");
8748c2ecf20Sopenharmony_ci		goto err_with_qm_start;
8758c2ecf20Sopenharmony_ci	}
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci	if (qm->fun_type == QM_HW_PF && vfs_num) {
8788c2ecf20Sopenharmony_ci		ret = hisi_qm_sriov_enable(pdev, vfs_num);
8798c2ecf20Sopenharmony_ci		if (ret < 0)
8808c2ecf20Sopenharmony_ci			goto err_with_alg_register;
8818c2ecf20Sopenharmony_ci	}
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_ci	return 0;
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_cierr_with_alg_register:
8868c2ecf20Sopenharmony_ci	hisi_qm_alg_unregister(qm, &hpre_devices);
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_cierr_with_qm_start:
8898c2ecf20Sopenharmony_ci	hpre_debugfs_exit(qm);
8908c2ecf20Sopenharmony_ci	hisi_qm_stop(qm, QM_NORMAL);
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_cierr_with_err_init:
8938c2ecf20Sopenharmony_ci	hisi_qm_dev_err_uninit(qm);
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_cierr_with_qm_init:
8968c2ecf20Sopenharmony_ci	hisi_qm_uninit(qm);
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	return ret;
8998c2ecf20Sopenharmony_ci}
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_cistatic void hpre_remove(struct pci_dev *pdev)
9028c2ecf20Sopenharmony_ci{
9038c2ecf20Sopenharmony_ci	struct hisi_qm *qm = pci_get_drvdata(pdev);
9048c2ecf20Sopenharmony_ci	int ret;
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	hisi_qm_wait_task_finish(qm, &hpre_devices);
9078c2ecf20Sopenharmony_ci	hisi_qm_alg_unregister(qm, &hpre_devices);
9088c2ecf20Sopenharmony_ci	if (qm->fun_type == QM_HW_PF && qm->vfs_num) {
9098c2ecf20Sopenharmony_ci		ret = hisi_qm_sriov_disable(pdev, qm->is_frozen);
9108c2ecf20Sopenharmony_ci		if (ret) {
9118c2ecf20Sopenharmony_ci			pci_err(pdev, "Disable SRIOV fail!\n");
9128c2ecf20Sopenharmony_ci			return;
9138c2ecf20Sopenharmony_ci		}
9148c2ecf20Sopenharmony_ci	}
9158c2ecf20Sopenharmony_ci	if (qm->fun_type == QM_HW_PF) {
9168c2ecf20Sopenharmony_ci		hpre_cnt_regs_clear(qm);
9178c2ecf20Sopenharmony_ci		qm->debug.curr_qm_qp_num = 0;
9188c2ecf20Sopenharmony_ci	}
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci	hpre_debugfs_exit(qm);
9218c2ecf20Sopenharmony_ci	hisi_qm_stop(qm, QM_NORMAL);
9228c2ecf20Sopenharmony_ci	hisi_qm_dev_err_uninit(qm);
9238c2ecf20Sopenharmony_ci	hisi_qm_uninit(qm);
9248c2ecf20Sopenharmony_ci}
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_cistatic const struct pci_error_handlers hpre_err_handler = {
9288c2ecf20Sopenharmony_ci	.error_detected		= hisi_qm_dev_err_detected,
9298c2ecf20Sopenharmony_ci	.slot_reset		= hisi_qm_dev_slot_reset,
9308c2ecf20Sopenharmony_ci	.reset_prepare		= hisi_qm_reset_prepare,
9318c2ecf20Sopenharmony_ci	.reset_done		= hisi_qm_reset_done,
9328c2ecf20Sopenharmony_ci};
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_cistatic struct pci_driver hpre_pci_driver = {
9358c2ecf20Sopenharmony_ci	.name			= hpre_name,
9368c2ecf20Sopenharmony_ci	.id_table		= hpre_dev_ids,
9378c2ecf20Sopenharmony_ci	.probe			= hpre_probe,
9388c2ecf20Sopenharmony_ci	.remove			= hpre_remove,
9398c2ecf20Sopenharmony_ci	.sriov_configure	= IS_ENABLED(CONFIG_PCI_IOV) ?
9408c2ecf20Sopenharmony_ci				  hisi_qm_sriov_configure : NULL,
9418c2ecf20Sopenharmony_ci	.err_handler		= &hpre_err_handler,
9428c2ecf20Sopenharmony_ci	.shutdown		= hisi_qm_dev_shutdown,
9438c2ecf20Sopenharmony_ci};
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_cistatic void hpre_register_debugfs(void)
9468c2ecf20Sopenharmony_ci{
9478c2ecf20Sopenharmony_ci	if (!debugfs_initialized())
9488c2ecf20Sopenharmony_ci		return;
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci	hpre_debugfs_root = debugfs_create_dir(hpre_name, NULL);
9518c2ecf20Sopenharmony_ci}
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_cistatic void hpre_unregister_debugfs(void)
9548c2ecf20Sopenharmony_ci{
9558c2ecf20Sopenharmony_ci	debugfs_remove_recursive(hpre_debugfs_root);
9568c2ecf20Sopenharmony_ci}
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_cistatic int __init hpre_init(void)
9598c2ecf20Sopenharmony_ci{
9608c2ecf20Sopenharmony_ci	int ret;
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_ci	hisi_qm_init_list(&hpre_devices);
9638c2ecf20Sopenharmony_ci	hpre_register_debugfs();
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci	ret = pci_register_driver(&hpre_pci_driver);
9668c2ecf20Sopenharmony_ci	if (ret) {
9678c2ecf20Sopenharmony_ci		hpre_unregister_debugfs();
9688c2ecf20Sopenharmony_ci		pr_err("hpre: can't register hisi hpre driver.\n");
9698c2ecf20Sopenharmony_ci	}
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci	return ret;
9728c2ecf20Sopenharmony_ci}
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_cistatic void __exit hpre_exit(void)
9758c2ecf20Sopenharmony_ci{
9768c2ecf20Sopenharmony_ci	pci_unregister_driver(&hpre_pci_driver);
9778c2ecf20Sopenharmony_ci	hpre_unregister_debugfs();
9788c2ecf20Sopenharmony_ci}
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_cimodule_init(hpre_init);
9818c2ecf20Sopenharmony_cimodule_exit(hpre_exit);
9828c2ecf20Sopenharmony_ci
9838c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
9848c2ecf20Sopenharmony_ciMODULE_AUTHOR("Zaibo Xu <xuzaibo@huawei.com>");
9858c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Driver for HiSilicon HPRE accelerator");
986