18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright (c) 2019 HiSilicon Limited. */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <linux/acpi.h>
58c2ecf20Sopenharmony_ci#include <linux/aer.h>
68c2ecf20Sopenharmony_ci#include <linux/bitops.h>
78c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
88c2ecf20Sopenharmony_ci#include <linux/init.h>
98c2ecf20Sopenharmony_ci#include <linux/io.h>
108c2ecf20Sopenharmony_ci#include <linux/iommu.h>
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/pci.h>
148c2ecf20Sopenharmony_ci#include <linux/seq_file.h>
158c2ecf20Sopenharmony_ci#include <linux/topology.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "sec.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define SEC_VF_NUM			63
208c2ecf20Sopenharmony_ci#define SEC_QUEUE_NUM_V1		4096
218c2ecf20Sopenharmony_ci#define SEC_QUEUE_NUM_V2		1024
228c2ecf20Sopenharmony_ci#define SEC_PF_PCI_DEVICE_ID		0xa255
238c2ecf20Sopenharmony_ci#define SEC_VF_PCI_DEVICE_ID		0xa256
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define SEC_BD_ERR_CHK_EN0		0xEFFFFFFF
268c2ecf20Sopenharmony_ci#define SEC_BD_ERR_CHK_EN1		0x7ffff7fd
278c2ecf20Sopenharmony_ci#define SEC_BD_ERR_CHK_EN3		0xffffbfff
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define SEC_SQE_SIZE			128
308c2ecf20Sopenharmony_ci#define SEC_SQ_SIZE			(SEC_SQE_SIZE * QM_Q_DEPTH)
318c2ecf20Sopenharmony_ci#define SEC_PF_DEF_Q_NUM		256
328c2ecf20Sopenharmony_ci#define SEC_PF_DEF_Q_BASE		0
338c2ecf20Sopenharmony_ci#define SEC_CTX_Q_NUM_DEF		2
348c2ecf20Sopenharmony_ci#define SEC_CTX_Q_NUM_MAX		32
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define SEC_CTRL_CNT_CLR_CE		0x301120
378c2ecf20Sopenharmony_ci#define SEC_CTRL_CNT_CLR_CE_BIT		BIT(0)
388c2ecf20Sopenharmony_ci#define SEC_ENGINE_PF_CFG_OFF		0x300000
398c2ecf20Sopenharmony_ci#define SEC_ACC_COMMON_REG_OFF		0x1000
408c2ecf20Sopenharmony_ci#define SEC_CORE_INT_SOURCE		0x301010
418c2ecf20Sopenharmony_ci#define SEC_CORE_INT_MASK		0x301000
428c2ecf20Sopenharmony_ci#define SEC_CORE_INT_STATUS		0x301008
438c2ecf20Sopenharmony_ci#define SEC_CORE_SRAM_ECC_ERR_INFO	0x301C14
448c2ecf20Sopenharmony_ci#define SEC_ECC_NUM(err)			(((err) >> 16) & 0xFF)
458c2ecf20Sopenharmony_ci#define SEC_ECC_ADDR(err)			((err) >> 0)
468c2ecf20Sopenharmony_ci#define SEC_CORE_INT_DISABLE		0x0
478c2ecf20Sopenharmony_ci#define SEC_CORE_INT_ENABLE		0x1ff
488c2ecf20Sopenharmony_ci#define SEC_CORE_INT_CLEAR		0x1ff
498c2ecf20Sopenharmony_ci#define SEC_SAA_ENABLE			0x17f
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#define SEC_RAS_CE_REG			0x301050
528c2ecf20Sopenharmony_ci#define SEC_RAS_FE_REG			0x301054
538c2ecf20Sopenharmony_ci#define SEC_RAS_NFE_REG			0x301058
548c2ecf20Sopenharmony_ci#define SEC_RAS_CE_ENB_MSK		0x88
558c2ecf20Sopenharmony_ci#define SEC_RAS_FE_ENB_MSK		0x0
568c2ecf20Sopenharmony_ci#define SEC_RAS_NFE_ENB_MSK		0x177
578c2ecf20Sopenharmony_ci#define SEC_RAS_DISABLE			0x0
588c2ecf20Sopenharmony_ci#define SEC_MEM_START_INIT_REG		0x0100
598c2ecf20Sopenharmony_ci#define SEC_MEM_INIT_DONE_REG		0x0104
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define SEC_CONTROL_REG			0x0200
628c2ecf20Sopenharmony_ci#define SEC_TRNG_EN_SHIFT		8
638c2ecf20Sopenharmony_ci#define SEC_CLK_GATE_ENABLE		BIT(3)
648c2ecf20Sopenharmony_ci#define SEC_CLK_GATE_DISABLE		(~BIT(3))
658c2ecf20Sopenharmony_ci#define SEC_AXI_SHUTDOWN_ENABLE	BIT(12)
668c2ecf20Sopenharmony_ci#define SEC_AXI_SHUTDOWN_DISABLE	0xFFFFEFFF
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define SEC_INTERFACE_USER_CTRL0_REG	0x0220
698c2ecf20Sopenharmony_ci#define SEC_INTERFACE_USER_CTRL1_REG	0x0224
708c2ecf20Sopenharmony_ci#define SEC_SAA_EN_REG					0x0270
718c2ecf20Sopenharmony_ci#define SEC_BD_ERR_CHK_EN_REG0		0x0380
728c2ecf20Sopenharmony_ci#define SEC_BD_ERR_CHK_EN_REG1		0x0384
738c2ecf20Sopenharmony_ci#define SEC_BD_ERR_CHK_EN_REG3		0x038c
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#define SEC_USER0_SMMU_NORMAL		(BIT(23) | BIT(15))
768c2ecf20Sopenharmony_ci#define SEC_USER1_SMMU_NORMAL		(BIT(31) | BIT(23) | BIT(15) | BIT(7))
778c2ecf20Sopenharmony_ci#define SEC_CORE_INT_STATUS_M_ECC	BIT(2)
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#define SEC_DELAY_10_US			10
808c2ecf20Sopenharmony_ci#define SEC_POLL_TIMEOUT_US		1000
818c2ecf20Sopenharmony_ci#define SEC_DBGFS_VAL_MAX_LEN		20
828c2ecf20Sopenharmony_ci#define SEC_SINGLE_PORT_MAX_TRANS	0x2060
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#define SEC_SQE_MASK_OFFSET		64
858c2ecf20Sopenharmony_ci#define SEC_SQE_MASK_LEN		48
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci#define SEC_ADDR(qm, offset) ((qm)->io_base + (offset) + \
888c2ecf20Sopenharmony_ci			     SEC_ENGINE_PF_CFG_OFF + SEC_ACC_COMMON_REG_OFF)
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistruct sec_hw_error {
918c2ecf20Sopenharmony_ci	u32 int_msk;
928c2ecf20Sopenharmony_ci	const char *msg;
938c2ecf20Sopenharmony_ci};
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistruct sec_dfx_item {
968c2ecf20Sopenharmony_ci	const char *name;
978c2ecf20Sopenharmony_ci	u32 offset;
988c2ecf20Sopenharmony_ci};
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic const char sec_name[] = "hisi_sec2";
1018c2ecf20Sopenharmony_cistatic struct dentry *sec_debugfs_root;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic struct hisi_qm_list sec_devices = {
1048c2ecf20Sopenharmony_ci	.register_to_crypto	= sec_register_to_crypto,
1058c2ecf20Sopenharmony_ci	.unregister_from_crypto	= sec_unregister_from_crypto,
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic const struct sec_hw_error sec_hw_errors[] = {
1098c2ecf20Sopenharmony_ci	{.int_msk = BIT(0), .msg = "sec_axi_rresp_err_rint"},
1108c2ecf20Sopenharmony_ci	{.int_msk = BIT(1), .msg = "sec_axi_bresp_err_rint"},
1118c2ecf20Sopenharmony_ci	{.int_msk = BIT(2), .msg = "sec_ecc_2bit_err_rint"},
1128c2ecf20Sopenharmony_ci	{.int_msk = BIT(3), .msg = "sec_ecc_1bit_err_rint"},
1138c2ecf20Sopenharmony_ci	{.int_msk = BIT(4), .msg = "sec_req_trng_timeout_rint"},
1148c2ecf20Sopenharmony_ci	{.int_msk = BIT(5), .msg = "sec_fsm_hbeat_rint"},
1158c2ecf20Sopenharmony_ci	{.int_msk = BIT(6), .msg = "sec_channel_req_rng_timeout_rint"},
1168c2ecf20Sopenharmony_ci	{.int_msk = BIT(7), .msg = "sec_bd_err_rint"},
1178c2ecf20Sopenharmony_ci	{.int_msk = BIT(8), .msg = "sec_chain_buff_err_rint"},
1188c2ecf20Sopenharmony_ci	{ /* sentinel */ }
1198c2ecf20Sopenharmony_ci};
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic const char * const sec_dbg_file_name[] = {
1228c2ecf20Sopenharmony_ci	[SEC_CURRENT_QM] = "current_qm",
1238c2ecf20Sopenharmony_ci	[SEC_CLEAR_ENABLE] = "clear_enable",
1248c2ecf20Sopenharmony_ci};
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic struct sec_dfx_item sec_dfx_labels[] = {
1278c2ecf20Sopenharmony_ci	{"send_cnt", offsetof(struct sec_dfx, send_cnt)},
1288c2ecf20Sopenharmony_ci	{"recv_cnt", offsetof(struct sec_dfx, recv_cnt)},
1298c2ecf20Sopenharmony_ci	{"send_busy_cnt", offsetof(struct sec_dfx, send_busy_cnt)},
1308c2ecf20Sopenharmony_ci	{"recv_busy_cnt", offsetof(struct sec_dfx, recv_busy_cnt)},
1318c2ecf20Sopenharmony_ci	{"err_bd_cnt", offsetof(struct sec_dfx, err_bd_cnt)},
1328c2ecf20Sopenharmony_ci	{"invalid_req_cnt", offsetof(struct sec_dfx, invalid_req_cnt)},
1338c2ecf20Sopenharmony_ci	{"done_flag_cnt", offsetof(struct sec_dfx, done_flag_cnt)},
1348c2ecf20Sopenharmony_ci};
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic const struct debugfs_reg32 sec_dfx_regs[] = {
1378c2ecf20Sopenharmony_ci	{"SEC_PF_ABNORMAL_INT_SOURCE    ",  0x301010},
1388c2ecf20Sopenharmony_ci	{"SEC_SAA_EN                    ",  0x301270},
1398c2ecf20Sopenharmony_ci	{"SEC_BD_LATENCY_MIN            ",  0x301600},
1408c2ecf20Sopenharmony_ci	{"SEC_BD_LATENCY_MAX            ",  0x301608},
1418c2ecf20Sopenharmony_ci	{"SEC_BD_LATENCY_AVG            ",  0x30160C},
1428c2ecf20Sopenharmony_ci	{"SEC_BD_NUM_IN_SAA0            ",  0x301670},
1438c2ecf20Sopenharmony_ci	{"SEC_BD_NUM_IN_SAA1            ",  0x301674},
1448c2ecf20Sopenharmony_ci	{"SEC_BD_NUM_IN_SEC             ",  0x301680},
1458c2ecf20Sopenharmony_ci	{"SEC_ECC_1BIT_CNT              ",  0x301C00},
1468c2ecf20Sopenharmony_ci	{"SEC_ECC_1BIT_INFO             ",  0x301C04},
1478c2ecf20Sopenharmony_ci	{"SEC_ECC_2BIT_CNT              ",  0x301C10},
1488c2ecf20Sopenharmony_ci	{"SEC_ECC_2BIT_INFO             ",  0x301C14},
1498c2ecf20Sopenharmony_ci	{"SEC_BD_SAA0                   ",  0x301C20},
1508c2ecf20Sopenharmony_ci	{"SEC_BD_SAA1                   ",  0x301C24},
1518c2ecf20Sopenharmony_ci	{"SEC_BD_SAA2                   ",  0x301C28},
1528c2ecf20Sopenharmony_ci	{"SEC_BD_SAA3                   ",  0x301C2C},
1538c2ecf20Sopenharmony_ci	{"SEC_BD_SAA4                   ",  0x301C30},
1548c2ecf20Sopenharmony_ci	{"SEC_BD_SAA5                   ",  0x301C34},
1558c2ecf20Sopenharmony_ci	{"SEC_BD_SAA6                   ",  0x301C38},
1568c2ecf20Sopenharmony_ci	{"SEC_BD_SAA7                   ",  0x301C3C},
1578c2ecf20Sopenharmony_ci	{"SEC_BD_SAA8                   ",  0x301C40},
1588c2ecf20Sopenharmony_ci};
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic int sec_pf_q_num_set(const char *val, const struct kernel_param *kp)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	return q_num_set(val, kp, SEC_PF_PCI_DEVICE_ID);
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic const struct kernel_param_ops sec_pf_q_num_ops = {
1668c2ecf20Sopenharmony_ci	.set = sec_pf_q_num_set,
1678c2ecf20Sopenharmony_ci	.get = param_get_int,
1688c2ecf20Sopenharmony_ci};
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic u32 pf_q_num = SEC_PF_DEF_Q_NUM;
1718c2ecf20Sopenharmony_cimodule_param_cb(pf_q_num, &sec_pf_q_num_ops, &pf_q_num, 0444);
1728c2ecf20Sopenharmony_ciMODULE_PARM_DESC(pf_q_num, "Number of queues in PF(v1 2-4096, v2 2-1024)");
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistatic int sec_ctx_q_num_set(const char *val, const struct kernel_param *kp)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	u32 ctx_q_num;
1778c2ecf20Sopenharmony_ci	int ret;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	if (!val)
1808c2ecf20Sopenharmony_ci		return -EINVAL;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	ret = kstrtou32(val, 10, &ctx_q_num);
1838c2ecf20Sopenharmony_ci	if (ret)
1848c2ecf20Sopenharmony_ci		return -EINVAL;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	if (!ctx_q_num || ctx_q_num > SEC_CTX_Q_NUM_MAX || ctx_q_num & 0x1) {
1878c2ecf20Sopenharmony_ci		pr_err("ctx queue num[%u] is invalid!\n", ctx_q_num);
1888c2ecf20Sopenharmony_ci		return -EINVAL;
1898c2ecf20Sopenharmony_ci	}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	return param_set_int(val, kp);
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistatic const struct kernel_param_ops sec_ctx_q_num_ops = {
1958c2ecf20Sopenharmony_ci	.set = sec_ctx_q_num_set,
1968c2ecf20Sopenharmony_ci	.get = param_get_int,
1978c2ecf20Sopenharmony_ci};
1988c2ecf20Sopenharmony_cistatic u32 ctx_q_num = SEC_CTX_Q_NUM_DEF;
1998c2ecf20Sopenharmony_cimodule_param_cb(ctx_q_num, &sec_ctx_q_num_ops, &ctx_q_num, 0444);
2008c2ecf20Sopenharmony_ciMODULE_PARM_DESC(ctx_q_num, "Queue num in ctx (2 default, 2, 4, ..., 32)");
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic const struct kernel_param_ops vfs_num_ops = {
2038c2ecf20Sopenharmony_ci	.set = vfs_num_set,
2048c2ecf20Sopenharmony_ci	.get = param_get_int,
2058c2ecf20Sopenharmony_ci};
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic u32 vfs_num;
2088c2ecf20Sopenharmony_cimodule_param_cb(vfs_num, &vfs_num_ops, &vfs_num, 0444);
2098c2ecf20Sopenharmony_ciMODULE_PARM_DESC(vfs_num, "Number of VFs to enable(1-63), 0(default)");
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_civoid sec_destroy_qps(struct hisi_qp **qps, int qp_num)
2128c2ecf20Sopenharmony_ci{
2138c2ecf20Sopenharmony_ci	hisi_qm_free_qps(qps, qp_num);
2148c2ecf20Sopenharmony_ci	kfree(qps);
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_cistruct hisi_qp **sec_create_qps(void)
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	int node = cpu_to_node(smp_processor_id());
2208c2ecf20Sopenharmony_ci	u32 ctx_num = ctx_q_num;
2218c2ecf20Sopenharmony_ci	struct hisi_qp **qps;
2228c2ecf20Sopenharmony_ci	int ret;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	qps = kcalloc(ctx_num, sizeof(struct hisi_qp *), GFP_KERNEL);
2258c2ecf20Sopenharmony_ci	if (!qps)
2268c2ecf20Sopenharmony_ci		return NULL;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	ret = hisi_qm_alloc_qps_node(&sec_devices, ctx_num, 0, node, qps);
2298c2ecf20Sopenharmony_ci	if (!ret)
2308c2ecf20Sopenharmony_ci		return qps;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	kfree(qps);
2338c2ecf20Sopenharmony_ci	return NULL;
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic const struct pci_device_id sec_dev_ids[] = {
2388c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, SEC_PF_PCI_DEVICE_ID) },
2398c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, SEC_VF_PCI_DEVICE_ID) },
2408c2ecf20Sopenharmony_ci	{ 0, }
2418c2ecf20Sopenharmony_ci};
2428c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, sec_dev_ids);
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic u8 sec_get_endian(struct hisi_qm *qm)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	u32 reg;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	/*
2498c2ecf20Sopenharmony_ci	 * As for VF, it is a wrong way to get endian setting by
2508c2ecf20Sopenharmony_ci	 * reading a register of the engine
2518c2ecf20Sopenharmony_ci	 */
2528c2ecf20Sopenharmony_ci	if (qm->pdev->is_virtfn) {
2538c2ecf20Sopenharmony_ci		dev_err_ratelimited(&qm->pdev->dev,
2548c2ecf20Sopenharmony_ci				    "cannot access a register in VF!\n");
2558c2ecf20Sopenharmony_ci		return SEC_LE;
2568c2ecf20Sopenharmony_ci	}
2578c2ecf20Sopenharmony_ci	reg = readl_relaxed(qm->io_base + SEC_ENGINE_PF_CFG_OFF +
2588c2ecf20Sopenharmony_ci			    SEC_ACC_COMMON_REG_OFF + SEC_CONTROL_REG);
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	/* BD little endian mode */
2618c2ecf20Sopenharmony_ci	if (!(reg & BIT(0)))
2628c2ecf20Sopenharmony_ci		return SEC_LE;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	/* BD 32-bits big endian mode */
2658c2ecf20Sopenharmony_ci	else if (!(reg & BIT(1)))
2668c2ecf20Sopenharmony_ci		return SEC_32BE;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	/* BD 64-bits big endian mode */
2698c2ecf20Sopenharmony_ci	else
2708c2ecf20Sopenharmony_ci		return SEC_64BE;
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_cistatic int sec_engine_init(struct hisi_qm *qm)
2748c2ecf20Sopenharmony_ci{
2758c2ecf20Sopenharmony_ci	int ret;
2768c2ecf20Sopenharmony_ci	u32 reg;
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	/* disable clock gate control */
2798c2ecf20Sopenharmony_ci	reg = readl_relaxed(SEC_ADDR(qm, SEC_CONTROL_REG));
2808c2ecf20Sopenharmony_ci	reg &= SEC_CLK_GATE_DISABLE;
2818c2ecf20Sopenharmony_ci	writel_relaxed(reg, SEC_ADDR(qm, SEC_CONTROL_REG));
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	writel_relaxed(0x1, SEC_ADDR(qm, SEC_MEM_START_INIT_REG));
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	ret = readl_relaxed_poll_timeout(SEC_ADDR(qm, SEC_MEM_INIT_DONE_REG),
2868c2ecf20Sopenharmony_ci					 reg, reg & 0x1, SEC_DELAY_10_US,
2878c2ecf20Sopenharmony_ci					 SEC_POLL_TIMEOUT_US);
2888c2ecf20Sopenharmony_ci	if (ret) {
2898c2ecf20Sopenharmony_ci		pci_err(qm->pdev, "fail to init sec mem\n");
2908c2ecf20Sopenharmony_ci		return ret;
2918c2ecf20Sopenharmony_ci	}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	reg = readl_relaxed(SEC_ADDR(qm, SEC_CONTROL_REG));
2948c2ecf20Sopenharmony_ci	reg |= (0x1 << SEC_TRNG_EN_SHIFT);
2958c2ecf20Sopenharmony_ci	writel_relaxed(reg, SEC_ADDR(qm, SEC_CONTROL_REG));
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	reg = readl_relaxed(SEC_ADDR(qm, SEC_INTERFACE_USER_CTRL0_REG));
2988c2ecf20Sopenharmony_ci	reg |= SEC_USER0_SMMU_NORMAL;
2998c2ecf20Sopenharmony_ci	writel_relaxed(reg, SEC_ADDR(qm, SEC_INTERFACE_USER_CTRL0_REG));
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	reg = readl_relaxed(SEC_ADDR(qm, SEC_INTERFACE_USER_CTRL1_REG));
3028c2ecf20Sopenharmony_ci	reg |= SEC_USER1_SMMU_NORMAL;
3038c2ecf20Sopenharmony_ci	writel_relaxed(reg, SEC_ADDR(qm, SEC_INTERFACE_USER_CTRL1_REG));
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	writel(SEC_SINGLE_PORT_MAX_TRANS,
3068c2ecf20Sopenharmony_ci	       qm->io_base + AM_CFG_SINGLE_PORT_MAX_TRANS);
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	writel(SEC_SAA_ENABLE, SEC_ADDR(qm, SEC_SAA_EN_REG));
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	/* Enable sm4 extra mode, as ctr/ecb */
3118c2ecf20Sopenharmony_ci	writel_relaxed(SEC_BD_ERR_CHK_EN0,
3128c2ecf20Sopenharmony_ci		       SEC_ADDR(qm, SEC_BD_ERR_CHK_EN_REG0));
3138c2ecf20Sopenharmony_ci	/* Enable sm4 xts mode multiple iv */
3148c2ecf20Sopenharmony_ci	writel_relaxed(SEC_BD_ERR_CHK_EN1,
3158c2ecf20Sopenharmony_ci		       SEC_ADDR(qm, SEC_BD_ERR_CHK_EN_REG1));
3168c2ecf20Sopenharmony_ci	writel_relaxed(SEC_BD_ERR_CHK_EN3,
3178c2ecf20Sopenharmony_ci		       SEC_ADDR(qm, SEC_BD_ERR_CHK_EN_REG3));
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	/* config endian */
3208c2ecf20Sopenharmony_ci	reg = readl_relaxed(SEC_ADDR(qm, SEC_CONTROL_REG));
3218c2ecf20Sopenharmony_ci	reg |= sec_get_endian(qm);
3228c2ecf20Sopenharmony_ci	writel_relaxed(reg, SEC_ADDR(qm, SEC_CONTROL_REG));
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	return 0;
3258c2ecf20Sopenharmony_ci}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_cistatic int sec_set_user_domain_and_cache(struct hisi_qm *qm)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	/* qm user domain */
3308c2ecf20Sopenharmony_ci	writel(AXUSER_BASE, qm->io_base + QM_ARUSER_M_CFG_1);
3318c2ecf20Sopenharmony_ci	writel(ARUSER_M_CFG_ENABLE, qm->io_base + QM_ARUSER_M_CFG_ENABLE);
3328c2ecf20Sopenharmony_ci	writel(AXUSER_BASE, qm->io_base + QM_AWUSER_M_CFG_1);
3338c2ecf20Sopenharmony_ci	writel(AWUSER_M_CFG_ENABLE, qm->io_base + QM_AWUSER_M_CFG_ENABLE);
3348c2ecf20Sopenharmony_ci	writel(WUSER_M_CFG_ENABLE, qm->io_base + QM_WUSER_M_CFG_ENABLE);
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	/* qm cache */
3378c2ecf20Sopenharmony_ci	writel(AXI_M_CFG, qm->io_base + QM_AXI_M_CFG);
3388c2ecf20Sopenharmony_ci	writel(AXI_M_CFG_ENABLE, qm->io_base + QM_AXI_M_CFG_ENABLE);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	/* disable FLR triggered by BME(bus master enable) */
3418c2ecf20Sopenharmony_ci	writel(PEH_AXUSER_CFG, qm->io_base + QM_PEH_AXUSER_CFG);
3428c2ecf20Sopenharmony_ci	writel(PEH_AXUSER_CFG_ENABLE, qm->io_base + QM_PEH_AXUSER_CFG_ENABLE);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	/* enable sqc,cqc writeback */
3458c2ecf20Sopenharmony_ci	writel(SQC_CACHE_ENABLE | CQC_CACHE_ENABLE | SQC_CACHE_WB_ENABLE |
3468c2ecf20Sopenharmony_ci	       CQC_CACHE_WB_ENABLE | FIELD_PREP(SQC_CACHE_WB_THRD, 1) |
3478c2ecf20Sopenharmony_ci	       FIELD_PREP(CQC_CACHE_WB_THRD, 1), qm->io_base + QM_CACHE_CTL);
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	return sec_engine_init(qm);
3508c2ecf20Sopenharmony_ci}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci/* sec_debug_regs_clear() - clear the sec debug regs */
3538c2ecf20Sopenharmony_cistatic void sec_debug_regs_clear(struct hisi_qm *qm)
3548c2ecf20Sopenharmony_ci{
3558c2ecf20Sopenharmony_ci	int i;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	/* clear current_qm */
3588c2ecf20Sopenharmony_ci	writel(0x0, qm->io_base + QM_DFX_MB_CNT_VF);
3598c2ecf20Sopenharmony_ci	writel(0x0, qm->io_base + QM_DFX_DB_CNT_VF);
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	/* clear sec dfx regs */
3628c2ecf20Sopenharmony_ci	writel(0x1, qm->io_base + SEC_CTRL_CNT_CLR_CE);
3638c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(sec_dfx_regs); i++)
3648c2ecf20Sopenharmony_ci		readl(qm->io_base + sec_dfx_regs[i].offset);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	/* clear rdclr_en */
3678c2ecf20Sopenharmony_ci	writel(0x0, qm->io_base + SEC_CTRL_CNT_CLR_CE);
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	hisi_qm_debug_regs_clear(qm);
3708c2ecf20Sopenharmony_ci}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_cistatic void sec_hw_error_enable(struct hisi_qm *qm)
3738c2ecf20Sopenharmony_ci{
3748c2ecf20Sopenharmony_ci	u32 val;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	if (qm->ver == QM_HW_V1) {
3778c2ecf20Sopenharmony_ci		writel(SEC_CORE_INT_DISABLE, qm->io_base + SEC_CORE_INT_MASK);
3788c2ecf20Sopenharmony_ci		pci_info(qm->pdev, "V1 not support hw error handle\n");
3798c2ecf20Sopenharmony_ci		return;
3808c2ecf20Sopenharmony_ci	}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	val = readl(SEC_ADDR(qm, SEC_CONTROL_REG));
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	/* clear SEC hw error source if having */
3858c2ecf20Sopenharmony_ci	writel(SEC_CORE_INT_CLEAR, qm->io_base + SEC_CORE_INT_SOURCE);
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	/* enable SEC hw error interrupts */
3888c2ecf20Sopenharmony_ci	writel(SEC_CORE_INT_ENABLE, qm->io_base + SEC_CORE_INT_MASK);
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	/* enable RAS int */
3918c2ecf20Sopenharmony_ci	writel(SEC_RAS_CE_ENB_MSK, qm->io_base + SEC_RAS_CE_REG);
3928c2ecf20Sopenharmony_ci	writel(SEC_RAS_FE_ENB_MSK, qm->io_base + SEC_RAS_FE_REG);
3938c2ecf20Sopenharmony_ci	writel(SEC_RAS_NFE_ENB_MSK, qm->io_base + SEC_RAS_NFE_REG);
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	/* enable SEC block master OOO when m-bit error occur */
3968c2ecf20Sopenharmony_ci	val = val | SEC_AXI_SHUTDOWN_ENABLE;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	writel(val, SEC_ADDR(qm, SEC_CONTROL_REG));
3998c2ecf20Sopenharmony_ci}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_cistatic void sec_hw_error_disable(struct hisi_qm *qm)
4028c2ecf20Sopenharmony_ci{
4038c2ecf20Sopenharmony_ci	u32 val;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	val = readl(SEC_ADDR(qm, SEC_CONTROL_REG));
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	/* disable RAS int */
4088c2ecf20Sopenharmony_ci	writel(SEC_RAS_DISABLE, qm->io_base + SEC_RAS_CE_REG);
4098c2ecf20Sopenharmony_ci	writel(SEC_RAS_DISABLE, qm->io_base + SEC_RAS_FE_REG);
4108c2ecf20Sopenharmony_ci	writel(SEC_RAS_DISABLE, qm->io_base + SEC_RAS_NFE_REG);
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	/* disable SEC hw error interrupts */
4138c2ecf20Sopenharmony_ci	writel(SEC_CORE_INT_DISABLE, qm->io_base + SEC_CORE_INT_MASK);
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	/* disable SEC block master OOO when m-bit error occur */
4168c2ecf20Sopenharmony_ci	val = val & SEC_AXI_SHUTDOWN_DISABLE;
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	writel(val, SEC_ADDR(qm, SEC_CONTROL_REG));
4198c2ecf20Sopenharmony_ci}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_cistatic u32 sec_current_qm_read(struct sec_debug_file *file)
4228c2ecf20Sopenharmony_ci{
4238c2ecf20Sopenharmony_ci	struct hisi_qm *qm = file->qm;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	return readl(qm->io_base + QM_DFX_MB_CNT_VF);
4268c2ecf20Sopenharmony_ci}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_cistatic int sec_current_qm_write(struct sec_debug_file *file, u32 val)
4298c2ecf20Sopenharmony_ci{
4308c2ecf20Sopenharmony_ci	struct hisi_qm *qm = file->qm;
4318c2ecf20Sopenharmony_ci	u32 vfq_num;
4328c2ecf20Sopenharmony_ci	u32 tmp;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	if (val > qm->vfs_num)
4358c2ecf20Sopenharmony_ci		return -EINVAL;
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	/* According PF or VF Dev ID to calculation curr_qm_qp_num and store */
4388c2ecf20Sopenharmony_ci	if (!val) {
4398c2ecf20Sopenharmony_ci		qm->debug.curr_qm_qp_num = qm->qp_num;
4408c2ecf20Sopenharmony_ci	} else {
4418c2ecf20Sopenharmony_ci		vfq_num = (qm->ctrl_qp_num - qm->qp_num) / qm->vfs_num;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci		if (val == qm->vfs_num)
4448c2ecf20Sopenharmony_ci			qm->debug.curr_qm_qp_num =
4458c2ecf20Sopenharmony_ci				qm->ctrl_qp_num - qm->qp_num -
4468c2ecf20Sopenharmony_ci				(qm->vfs_num - 1) * vfq_num;
4478c2ecf20Sopenharmony_ci		else
4488c2ecf20Sopenharmony_ci			qm->debug.curr_qm_qp_num = vfq_num;
4498c2ecf20Sopenharmony_ci	}
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	writel(val, qm->io_base + QM_DFX_MB_CNT_VF);
4528c2ecf20Sopenharmony_ci	writel(val, qm->io_base + QM_DFX_DB_CNT_VF);
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	tmp = val |
4558c2ecf20Sopenharmony_ci	      (readl(qm->io_base + QM_DFX_SQE_CNT_VF_SQN) & CURRENT_Q_MASK);
4568c2ecf20Sopenharmony_ci	writel(tmp, qm->io_base + QM_DFX_SQE_CNT_VF_SQN);
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	tmp = val |
4598c2ecf20Sopenharmony_ci	      (readl(qm->io_base + QM_DFX_CQE_CNT_VF_CQN) & CURRENT_Q_MASK);
4608c2ecf20Sopenharmony_ci	writel(tmp, qm->io_base + QM_DFX_CQE_CNT_VF_CQN);
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	return 0;
4638c2ecf20Sopenharmony_ci}
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_cistatic u32 sec_clear_enable_read(struct sec_debug_file *file)
4668c2ecf20Sopenharmony_ci{
4678c2ecf20Sopenharmony_ci	struct hisi_qm *qm = file->qm;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	return readl(qm->io_base + SEC_CTRL_CNT_CLR_CE) &
4708c2ecf20Sopenharmony_ci			SEC_CTRL_CNT_CLR_CE_BIT;
4718c2ecf20Sopenharmony_ci}
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_cistatic int sec_clear_enable_write(struct sec_debug_file *file, u32 val)
4748c2ecf20Sopenharmony_ci{
4758c2ecf20Sopenharmony_ci	struct hisi_qm *qm = file->qm;
4768c2ecf20Sopenharmony_ci	u32 tmp;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	if (val != 1 && val)
4798c2ecf20Sopenharmony_ci		return -EINVAL;
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	tmp = (readl(qm->io_base + SEC_CTRL_CNT_CLR_CE) &
4828c2ecf20Sopenharmony_ci	       ~SEC_CTRL_CNT_CLR_CE_BIT) | val;
4838c2ecf20Sopenharmony_ci	writel(tmp, qm->io_base + SEC_CTRL_CNT_CLR_CE);
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	return 0;
4868c2ecf20Sopenharmony_ci}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_cistatic ssize_t sec_debug_read(struct file *filp, char __user *buf,
4898c2ecf20Sopenharmony_ci			       size_t count, loff_t *pos)
4908c2ecf20Sopenharmony_ci{
4918c2ecf20Sopenharmony_ci	struct sec_debug_file *file = filp->private_data;
4928c2ecf20Sopenharmony_ci	char tbuf[SEC_DBGFS_VAL_MAX_LEN];
4938c2ecf20Sopenharmony_ci	u32 val;
4948c2ecf20Sopenharmony_ci	int ret;
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	spin_lock_irq(&file->lock);
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	switch (file->index) {
4998c2ecf20Sopenharmony_ci	case SEC_CURRENT_QM:
5008c2ecf20Sopenharmony_ci		val = sec_current_qm_read(file);
5018c2ecf20Sopenharmony_ci		break;
5028c2ecf20Sopenharmony_ci	case SEC_CLEAR_ENABLE:
5038c2ecf20Sopenharmony_ci		val = sec_clear_enable_read(file);
5048c2ecf20Sopenharmony_ci		break;
5058c2ecf20Sopenharmony_ci	default:
5068c2ecf20Sopenharmony_ci		spin_unlock_irq(&file->lock);
5078c2ecf20Sopenharmony_ci		return -EINVAL;
5088c2ecf20Sopenharmony_ci	}
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	spin_unlock_irq(&file->lock);
5118c2ecf20Sopenharmony_ci	ret = snprintf(tbuf, SEC_DBGFS_VAL_MAX_LEN, "%u\n", val);
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	return simple_read_from_buffer(buf, count, pos, tbuf, ret);
5148c2ecf20Sopenharmony_ci}
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_cistatic ssize_t sec_debug_write(struct file *filp, const char __user *buf,
5178c2ecf20Sopenharmony_ci			       size_t count, loff_t *pos)
5188c2ecf20Sopenharmony_ci{
5198c2ecf20Sopenharmony_ci	struct sec_debug_file *file = filp->private_data;
5208c2ecf20Sopenharmony_ci	char tbuf[SEC_DBGFS_VAL_MAX_LEN];
5218c2ecf20Sopenharmony_ci	unsigned long val;
5228c2ecf20Sopenharmony_ci	int len, ret;
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	if (*pos != 0)
5258c2ecf20Sopenharmony_ci		return 0;
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	if (count >= SEC_DBGFS_VAL_MAX_LEN)
5288c2ecf20Sopenharmony_ci		return -ENOSPC;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	len = simple_write_to_buffer(tbuf, SEC_DBGFS_VAL_MAX_LEN - 1,
5318c2ecf20Sopenharmony_ci				     pos, buf, count);
5328c2ecf20Sopenharmony_ci	if (len < 0)
5338c2ecf20Sopenharmony_ci		return len;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	tbuf[len] = '\0';
5368c2ecf20Sopenharmony_ci	if (kstrtoul(tbuf, 0, &val))
5378c2ecf20Sopenharmony_ci		return -EFAULT;
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	spin_lock_irq(&file->lock);
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	switch (file->index) {
5428c2ecf20Sopenharmony_ci	case SEC_CURRENT_QM:
5438c2ecf20Sopenharmony_ci		ret = sec_current_qm_write(file, val);
5448c2ecf20Sopenharmony_ci		if (ret)
5458c2ecf20Sopenharmony_ci			goto err_input;
5468c2ecf20Sopenharmony_ci		break;
5478c2ecf20Sopenharmony_ci	case SEC_CLEAR_ENABLE:
5488c2ecf20Sopenharmony_ci		ret = sec_clear_enable_write(file, val);
5498c2ecf20Sopenharmony_ci		if (ret)
5508c2ecf20Sopenharmony_ci			goto err_input;
5518c2ecf20Sopenharmony_ci		break;
5528c2ecf20Sopenharmony_ci	default:
5538c2ecf20Sopenharmony_ci		ret = -EINVAL;
5548c2ecf20Sopenharmony_ci		goto err_input;
5558c2ecf20Sopenharmony_ci	}
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	spin_unlock_irq(&file->lock);
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	return count;
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci err_input:
5628c2ecf20Sopenharmony_ci	spin_unlock_irq(&file->lock);
5638c2ecf20Sopenharmony_ci	return ret;
5648c2ecf20Sopenharmony_ci}
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_cistatic const struct file_operations sec_dbg_fops = {
5678c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
5688c2ecf20Sopenharmony_ci	.open = simple_open,
5698c2ecf20Sopenharmony_ci	.read = sec_debug_read,
5708c2ecf20Sopenharmony_ci	.write = sec_debug_write,
5718c2ecf20Sopenharmony_ci};
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_cistatic int sec_debugfs_atomic64_get(void *data, u64 *val)
5748c2ecf20Sopenharmony_ci{
5758c2ecf20Sopenharmony_ci	*val = atomic64_read((atomic64_t *)data);
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	return 0;
5788c2ecf20Sopenharmony_ci}
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_cistatic int sec_debugfs_atomic64_set(void *data, u64 val)
5818c2ecf20Sopenharmony_ci{
5828c2ecf20Sopenharmony_ci	if (val)
5838c2ecf20Sopenharmony_ci		return -EINVAL;
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	atomic64_set((atomic64_t *)data, 0);
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	return 0;
5888c2ecf20Sopenharmony_ci}
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(sec_atomic64_ops, sec_debugfs_atomic64_get,
5918c2ecf20Sopenharmony_ci			 sec_debugfs_atomic64_set, "%lld\n");
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_cistatic int sec_core_debug_init(struct hisi_qm *qm)
5948c2ecf20Sopenharmony_ci{
5958c2ecf20Sopenharmony_ci	struct sec_dev *sec = container_of(qm, struct sec_dev, qm);
5968c2ecf20Sopenharmony_ci	struct device *dev = &qm->pdev->dev;
5978c2ecf20Sopenharmony_ci	struct sec_dfx *dfx = &sec->debug.dfx;
5988c2ecf20Sopenharmony_ci	struct debugfs_regset32 *regset;
5998c2ecf20Sopenharmony_ci	struct dentry *tmp_d;
6008c2ecf20Sopenharmony_ci	int i;
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	tmp_d = debugfs_create_dir("sec_dfx", qm->debug.debug_root);
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
6058c2ecf20Sopenharmony_ci	if (!regset)
6068c2ecf20Sopenharmony_ci		return -ENOMEM;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	regset->regs = sec_dfx_regs;
6098c2ecf20Sopenharmony_ci	regset->nregs = ARRAY_SIZE(sec_dfx_regs);
6108c2ecf20Sopenharmony_ci	regset->base = qm->io_base;
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	if (qm->pdev->device == SEC_PF_PCI_DEVICE_ID)
6138c2ecf20Sopenharmony_ci		debugfs_create_regset32("regs", 0444, tmp_d, regset);
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(sec_dfx_labels); i++) {
6168c2ecf20Sopenharmony_ci		atomic64_t *data = (atomic64_t *)((uintptr_t)dfx +
6178c2ecf20Sopenharmony_ci					sec_dfx_labels[i].offset);
6188c2ecf20Sopenharmony_ci		debugfs_create_file(sec_dfx_labels[i].name, 0644,
6198c2ecf20Sopenharmony_ci				   tmp_d, data, &sec_atomic64_ops);
6208c2ecf20Sopenharmony_ci	}
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	return 0;
6238c2ecf20Sopenharmony_ci}
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_cistatic int sec_debug_init(struct hisi_qm *qm)
6268c2ecf20Sopenharmony_ci{
6278c2ecf20Sopenharmony_ci	struct sec_dev *sec = container_of(qm, struct sec_dev, qm);
6288c2ecf20Sopenharmony_ci	int i;
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	if (qm->pdev->device == SEC_PF_PCI_DEVICE_ID) {
6318c2ecf20Sopenharmony_ci		for (i = SEC_CURRENT_QM; i < SEC_DEBUG_FILE_NUM; i++) {
6328c2ecf20Sopenharmony_ci			spin_lock_init(&sec->debug.files[i].lock);
6338c2ecf20Sopenharmony_ci			sec->debug.files[i].index = i;
6348c2ecf20Sopenharmony_ci			sec->debug.files[i].qm = qm;
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci			debugfs_create_file(sec_dbg_file_name[i], 0600,
6378c2ecf20Sopenharmony_ci						  qm->debug.debug_root,
6388c2ecf20Sopenharmony_ci						  sec->debug.files + i,
6398c2ecf20Sopenharmony_ci						  &sec_dbg_fops);
6408c2ecf20Sopenharmony_ci		}
6418c2ecf20Sopenharmony_ci	}
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	return sec_core_debug_init(qm);
6448c2ecf20Sopenharmony_ci}
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_cistatic int sec_debugfs_init(struct hisi_qm *qm)
6478c2ecf20Sopenharmony_ci{
6488c2ecf20Sopenharmony_ci	struct device *dev = &qm->pdev->dev;
6498c2ecf20Sopenharmony_ci	int ret;
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	qm->debug.debug_root = debugfs_create_dir(dev_name(dev),
6528c2ecf20Sopenharmony_ci						  sec_debugfs_root);
6538c2ecf20Sopenharmony_ci	qm->debug.sqe_mask_offset = SEC_SQE_MASK_OFFSET;
6548c2ecf20Sopenharmony_ci	qm->debug.sqe_mask_len = SEC_SQE_MASK_LEN;
6558c2ecf20Sopenharmony_ci	ret = hisi_qm_debug_init(qm);
6568c2ecf20Sopenharmony_ci	if (ret)
6578c2ecf20Sopenharmony_ci		goto failed_to_create;
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	ret = sec_debug_init(qm);
6608c2ecf20Sopenharmony_ci	if (ret)
6618c2ecf20Sopenharmony_ci		goto failed_to_create;
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	return 0;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_cifailed_to_create:
6678c2ecf20Sopenharmony_ci	debugfs_remove_recursive(sec_debugfs_root);
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	return ret;
6708c2ecf20Sopenharmony_ci}
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_cistatic void sec_debugfs_exit(struct hisi_qm *qm)
6738c2ecf20Sopenharmony_ci{
6748c2ecf20Sopenharmony_ci	debugfs_remove_recursive(qm->debug.debug_root);
6758c2ecf20Sopenharmony_ci}
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_cistatic void sec_log_hw_error(struct hisi_qm *qm, u32 err_sts)
6788c2ecf20Sopenharmony_ci{
6798c2ecf20Sopenharmony_ci	const struct sec_hw_error *errs = sec_hw_errors;
6808c2ecf20Sopenharmony_ci	struct device *dev = &qm->pdev->dev;
6818c2ecf20Sopenharmony_ci	u32 err_val;
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	while (errs->msg) {
6848c2ecf20Sopenharmony_ci		if (errs->int_msk & err_sts) {
6858c2ecf20Sopenharmony_ci			dev_err(dev, "%s [error status=0x%x] found\n",
6868c2ecf20Sopenharmony_ci				errs->msg, errs->int_msk);
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci			if (SEC_CORE_INT_STATUS_M_ECC & errs->int_msk) {
6898c2ecf20Sopenharmony_ci				err_val = readl(qm->io_base +
6908c2ecf20Sopenharmony_ci						SEC_CORE_SRAM_ECC_ERR_INFO);
6918c2ecf20Sopenharmony_ci				dev_err(dev, "multi ecc sram num=0x%x\n",
6928c2ecf20Sopenharmony_ci					SEC_ECC_NUM(err_val));
6938c2ecf20Sopenharmony_ci			}
6948c2ecf20Sopenharmony_ci		}
6958c2ecf20Sopenharmony_ci		errs++;
6968c2ecf20Sopenharmony_ci	}
6978c2ecf20Sopenharmony_ci}
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_cistatic u32 sec_get_hw_err_status(struct hisi_qm *qm)
7008c2ecf20Sopenharmony_ci{
7018c2ecf20Sopenharmony_ci	return readl(qm->io_base + SEC_CORE_INT_STATUS);
7028c2ecf20Sopenharmony_ci}
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_cistatic void sec_clear_hw_err_status(struct hisi_qm *qm, u32 err_sts)
7058c2ecf20Sopenharmony_ci{
7068c2ecf20Sopenharmony_ci	writel(err_sts, qm->io_base + SEC_CORE_INT_SOURCE);
7078c2ecf20Sopenharmony_ci}
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_cistatic void sec_open_axi_master_ooo(struct hisi_qm *qm)
7108c2ecf20Sopenharmony_ci{
7118c2ecf20Sopenharmony_ci	u32 val;
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	val = readl(SEC_ADDR(qm, SEC_CONTROL_REG));
7148c2ecf20Sopenharmony_ci	writel(val & SEC_AXI_SHUTDOWN_DISABLE, SEC_ADDR(qm, SEC_CONTROL_REG));
7158c2ecf20Sopenharmony_ci	writel(val | SEC_AXI_SHUTDOWN_ENABLE, SEC_ADDR(qm, SEC_CONTROL_REG));
7168c2ecf20Sopenharmony_ci}
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_cistatic const struct hisi_qm_err_ini sec_err_ini = {
7198c2ecf20Sopenharmony_ci	.hw_init		= sec_set_user_domain_and_cache,
7208c2ecf20Sopenharmony_ci	.hw_err_enable		= sec_hw_error_enable,
7218c2ecf20Sopenharmony_ci	.hw_err_disable		= sec_hw_error_disable,
7228c2ecf20Sopenharmony_ci	.get_dev_hw_err_status	= sec_get_hw_err_status,
7238c2ecf20Sopenharmony_ci	.clear_dev_hw_err_status = sec_clear_hw_err_status,
7248c2ecf20Sopenharmony_ci	.log_dev_hw_err		= sec_log_hw_error,
7258c2ecf20Sopenharmony_ci	.open_axi_master_ooo	= sec_open_axi_master_ooo,
7268c2ecf20Sopenharmony_ci	.err_info		= {
7278c2ecf20Sopenharmony_ci		.ce			= QM_BASE_CE,
7288c2ecf20Sopenharmony_ci		.nfe			= QM_BASE_NFE | QM_ACC_DO_TASK_TIMEOUT |
7298c2ecf20Sopenharmony_ci					  QM_ACC_WB_NOT_READY_TIMEOUT,
7308c2ecf20Sopenharmony_ci		.fe			= 0,
7318c2ecf20Sopenharmony_ci		.ecc_2bits_mask		= SEC_CORE_INT_STATUS_M_ECC,
7328c2ecf20Sopenharmony_ci		.msi_wr_port		= BIT(0),
7338c2ecf20Sopenharmony_ci		.acpi_rst		= "SRST",
7348c2ecf20Sopenharmony_ci	}
7358c2ecf20Sopenharmony_ci};
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_cistatic int sec_pf_probe_init(struct sec_dev *sec)
7388c2ecf20Sopenharmony_ci{
7398c2ecf20Sopenharmony_ci	struct hisi_qm *qm = &sec->qm;
7408c2ecf20Sopenharmony_ci	int ret;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	if (qm->ver == QM_HW_V1)
7438c2ecf20Sopenharmony_ci		qm->ctrl_qp_num = SEC_QUEUE_NUM_V1;
7448c2ecf20Sopenharmony_ci	else
7458c2ecf20Sopenharmony_ci		qm->ctrl_qp_num = SEC_QUEUE_NUM_V2;
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	qm->err_ini = &sec_err_ini;
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci	ret = sec_set_user_domain_and_cache(qm);
7508c2ecf20Sopenharmony_ci	if (ret)
7518c2ecf20Sopenharmony_ci		return ret;
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci	hisi_qm_dev_err_init(qm);
7548c2ecf20Sopenharmony_ci	sec_debug_regs_clear(qm);
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	return 0;
7578c2ecf20Sopenharmony_ci}
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_cistatic int sec_qm_init(struct hisi_qm *qm, struct pci_dev *pdev)
7608c2ecf20Sopenharmony_ci{
7618c2ecf20Sopenharmony_ci	int ret;
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci	qm->pdev = pdev;
7648c2ecf20Sopenharmony_ci	qm->ver = pdev->revision;
7658c2ecf20Sopenharmony_ci	qm->sqe_size = SEC_SQE_SIZE;
7668c2ecf20Sopenharmony_ci	qm->dev_name = sec_name;
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	qm->fun_type = (pdev->device == SEC_PF_PCI_DEVICE_ID) ?
7698c2ecf20Sopenharmony_ci			QM_HW_PF : QM_HW_VF;
7708c2ecf20Sopenharmony_ci	if (qm->fun_type == QM_HW_PF) {
7718c2ecf20Sopenharmony_ci		qm->qp_base = SEC_PF_DEF_Q_BASE;
7728c2ecf20Sopenharmony_ci		qm->qp_num = pf_q_num;
7738c2ecf20Sopenharmony_ci		qm->debug.curr_qm_qp_num = pf_q_num;
7748c2ecf20Sopenharmony_ci		qm->qm_list = &sec_devices;
7758c2ecf20Sopenharmony_ci	} else if (qm->fun_type == QM_HW_VF && qm->ver == QM_HW_V1) {
7768c2ecf20Sopenharmony_ci		/*
7778c2ecf20Sopenharmony_ci		 * have no way to get qm configure in VM in v1 hardware,
7788c2ecf20Sopenharmony_ci		 * so currently force PF to uses SEC_PF_DEF_Q_NUM, and force
7798c2ecf20Sopenharmony_ci		 * to trigger only one VF in v1 hardware.
7808c2ecf20Sopenharmony_ci		 * v2 hardware has no such problem.
7818c2ecf20Sopenharmony_ci		 */
7828c2ecf20Sopenharmony_ci		qm->qp_base = SEC_PF_DEF_Q_NUM;
7838c2ecf20Sopenharmony_ci		qm->qp_num = SEC_QUEUE_NUM_V1 - SEC_PF_DEF_Q_NUM;
7848c2ecf20Sopenharmony_ci	}
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	/*
7878c2ecf20Sopenharmony_ci	 * WQ_HIGHPRI: SEC request must be low delayed,
7888c2ecf20Sopenharmony_ci	 * so need a high priority workqueue.
7898c2ecf20Sopenharmony_ci	 * WQ_UNBOUND: SEC task is likely with long
7908c2ecf20Sopenharmony_ci	 * running CPU intensive workloads.
7918c2ecf20Sopenharmony_ci	 */
7928c2ecf20Sopenharmony_ci	qm->wq = alloc_workqueue("%s", WQ_HIGHPRI | WQ_MEM_RECLAIM |
7938c2ecf20Sopenharmony_ci				 WQ_UNBOUND, num_online_cpus(),
7948c2ecf20Sopenharmony_ci				 pci_name(qm->pdev));
7958c2ecf20Sopenharmony_ci	if (!qm->wq) {
7968c2ecf20Sopenharmony_ci		pci_err(qm->pdev, "fail to alloc workqueue\n");
7978c2ecf20Sopenharmony_ci		return -ENOMEM;
7988c2ecf20Sopenharmony_ci	}
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	ret = hisi_qm_init(qm);
8018c2ecf20Sopenharmony_ci	if (ret)
8028c2ecf20Sopenharmony_ci		destroy_workqueue(qm->wq);
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	return ret;
8058c2ecf20Sopenharmony_ci}
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_cistatic void sec_qm_uninit(struct hisi_qm *qm)
8088c2ecf20Sopenharmony_ci{
8098c2ecf20Sopenharmony_ci	hisi_qm_uninit(qm);
8108c2ecf20Sopenharmony_ci}
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_cistatic int sec_probe_init(struct sec_dev *sec)
8138c2ecf20Sopenharmony_ci{
8148c2ecf20Sopenharmony_ci	struct hisi_qm *qm = &sec->qm;
8158c2ecf20Sopenharmony_ci	int ret;
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	if (qm->fun_type == QM_HW_PF) {
8188c2ecf20Sopenharmony_ci		ret = sec_pf_probe_init(sec);
8198c2ecf20Sopenharmony_ci		if (ret)
8208c2ecf20Sopenharmony_ci			return ret;
8218c2ecf20Sopenharmony_ci	}
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	return 0;
8248c2ecf20Sopenharmony_ci}
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_cistatic void sec_probe_uninit(struct hisi_qm *qm)
8278c2ecf20Sopenharmony_ci{
8288c2ecf20Sopenharmony_ci	hisi_qm_dev_err_uninit(qm);
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	destroy_workqueue(qm->wq);
8318c2ecf20Sopenharmony_ci}
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_cistatic void sec_iommu_used_check(struct sec_dev *sec)
8348c2ecf20Sopenharmony_ci{
8358c2ecf20Sopenharmony_ci	struct iommu_domain *domain;
8368c2ecf20Sopenharmony_ci	struct device *dev = &sec->qm.pdev->dev;
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	domain = iommu_get_domain_for_dev(dev);
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci	/* Check if iommu is used */
8418c2ecf20Sopenharmony_ci	sec->iommu_used = false;
8428c2ecf20Sopenharmony_ci	if (domain) {
8438c2ecf20Sopenharmony_ci		if (domain->type & __IOMMU_DOMAIN_PAGING)
8448c2ecf20Sopenharmony_ci			sec->iommu_used = true;
8458c2ecf20Sopenharmony_ci		dev_info(dev, "SMMU Opened, the iommu type = %u\n",
8468c2ecf20Sopenharmony_ci			domain->type);
8478c2ecf20Sopenharmony_ci	}
8488c2ecf20Sopenharmony_ci}
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_cistatic int sec_probe(struct pci_dev *pdev, const struct pci_device_id *id)
8518c2ecf20Sopenharmony_ci{
8528c2ecf20Sopenharmony_ci	struct sec_dev *sec;
8538c2ecf20Sopenharmony_ci	struct hisi_qm *qm;
8548c2ecf20Sopenharmony_ci	int ret;
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci	sec = devm_kzalloc(&pdev->dev, sizeof(*sec), GFP_KERNEL);
8578c2ecf20Sopenharmony_ci	if (!sec)
8588c2ecf20Sopenharmony_ci		return -ENOMEM;
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	qm = &sec->qm;
8618c2ecf20Sopenharmony_ci	ret = sec_qm_init(qm, pdev);
8628c2ecf20Sopenharmony_ci	if (ret) {
8638c2ecf20Sopenharmony_ci		pci_err(pdev, "Failed to init SEC QM (%d)!\n", ret);
8648c2ecf20Sopenharmony_ci		return ret;
8658c2ecf20Sopenharmony_ci	}
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci	sec->ctx_q_num = ctx_q_num;
8688c2ecf20Sopenharmony_ci	sec_iommu_used_check(sec);
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	ret = sec_probe_init(sec);
8718c2ecf20Sopenharmony_ci	if (ret) {
8728c2ecf20Sopenharmony_ci		pci_err(pdev, "Failed to probe!\n");
8738c2ecf20Sopenharmony_ci		goto err_qm_uninit;
8748c2ecf20Sopenharmony_ci	}
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci	ret = hisi_qm_start(qm);
8778c2ecf20Sopenharmony_ci	if (ret) {
8788c2ecf20Sopenharmony_ci		pci_err(pdev, "Failed to start sec qm!\n");
8798c2ecf20Sopenharmony_ci		goto err_probe_uninit;
8808c2ecf20Sopenharmony_ci	}
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	ret = sec_debugfs_init(qm);
8838c2ecf20Sopenharmony_ci	if (ret)
8848c2ecf20Sopenharmony_ci		pci_warn(pdev, "Failed to init debugfs!\n");
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	ret = hisi_qm_alg_register(qm, &sec_devices);
8878c2ecf20Sopenharmony_ci	if (ret < 0) {
8888c2ecf20Sopenharmony_ci		pr_err("Failed to register driver to crypto.\n");
8898c2ecf20Sopenharmony_ci		goto err_qm_stop;
8908c2ecf20Sopenharmony_ci	}
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	if (qm->fun_type == QM_HW_PF && vfs_num) {
8938c2ecf20Sopenharmony_ci		ret = hisi_qm_sriov_enable(pdev, vfs_num);
8948c2ecf20Sopenharmony_ci		if (ret < 0)
8958c2ecf20Sopenharmony_ci			goto err_alg_unregister;
8968c2ecf20Sopenharmony_ci	}
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	return 0;
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_cierr_alg_unregister:
9018c2ecf20Sopenharmony_ci	hisi_qm_alg_unregister(qm, &sec_devices);
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_cierr_qm_stop:
9048c2ecf20Sopenharmony_ci	sec_debugfs_exit(qm);
9058c2ecf20Sopenharmony_ci	hisi_qm_stop(qm, QM_NORMAL);
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_cierr_probe_uninit:
9088c2ecf20Sopenharmony_ci	sec_probe_uninit(qm);
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_cierr_qm_uninit:
9118c2ecf20Sopenharmony_ci	sec_qm_uninit(qm);
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci	return ret;
9148c2ecf20Sopenharmony_ci}
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_cistatic void sec_remove(struct pci_dev *pdev)
9178c2ecf20Sopenharmony_ci{
9188c2ecf20Sopenharmony_ci	struct hisi_qm *qm = pci_get_drvdata(pdev);
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci	hisi_qm_wait_task_finish(qm, &sec_devices);
9218c2ecf20Sopenharmony_ci	hisi_qm_alg_unregister(qm, &sec_devices);
9228c2ecf20Sopenharmony_ci	if (qm->fun_type == QM_HW_PF && qm->vfs_num)
9238c2ecf20Sopenharmony_ci		hisi_qm_sriov_disable(pdev, qm->is_frozen);
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci	sec_debugfs_exit(qm);
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ci	(void)hisi_qm_stop(qm, QM_NORMAL);
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	if (qm->fun_type == QM_HW_PF)
9308c2ecf20Sopenharmony_ci		sec_debug_regs_clear(qm);
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	sec_probe_uninit(qm);
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci	sec_qm_uninit(qm);
9358c2ecf20Sopenharmony_ci}
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_cistatic const struct pci_error_handlers sec_err_handler = {
9388c2ecf20Sopenharmony_ci	.error_detected = hisi_qm_dev_err_detected,
9398c2ecf20Sopenharmony_ci	.slot_reset =  hisi_qm_dev_slot_reset,
9408c2ecf20Sopenharmony_ci	.reset_prepare		= hisi_qm_reset_prepare,
9418c2ecf20Sopenharmony_ci	.reset_done		= hisi_qm_reset_done,
9428c2ecf20Sopenharmony_ci};
9438c2ecf20Sopenharmony_ci
9448c2ecf20Sopenharmony_cistatic struct pci_driver sec_pci_driver = {
9458c2ecf20Sopenharmony_ci	.name = "hisi_sec2",
9468c2ecf20Sopenharmony_ci	.id_table = sec_dev_ids,
9478c2ecf20Sopenharmony_ci	.probe = sec_probe,
9488c2ecf20Sopenharmony_ci	.remove = sec_remove,
9498c2ecf20Sopenharmony_ci	.err_handler = &sec_err_handler,
9508c2ecf20Sopenharmony_ci	.sriov_configure = hisi_qm_sriov_configure,
9518c2ecf20Sopenharmony_ci	.shutdown = hisi_qm_dev_shutdown,
9528c2ecf20Sopenharmony_ci};
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_cistatic void sec_register_debugfs(void)
9558c2ecf20Sopenharmony_ci{
9568c2ecf20Sopenharmony_ci	if (!debugfs_initialized())
9578c2ecf20Sopenharmony_ci		return;
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci	sec_debugfs_root = debugfs_create_dir("hisi_sec2", NULL);
9608c2ecf20Sopenharmony_ci}
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_cistatic void sec_unregister_debugfs(void)
9638c2ecf20Sopenharmony_ci{
9648c2ecf20Sopenharmony_ci	debugfs_remove_recursive(sec_debugfs_root);
9658c2ecf20Sopenharmony_ci}
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_cistatic int __init sec_init(void)
9688c2ecf20Sopenharmony_ci{
9698c2ecf20Sopenharmony_ci	int ret;
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci	hisi_qm_init_list(&sec_devices);
9728c2ecf20Sopenharmony_ci	sec_register_debugfs();
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci	ret = pci_register_driver(&sec_pci_driver);
9758c2ecf20Sopenharmony_ci	if (ret < 0) {
9768c2ecf20Sopenharmony_ci		sec_unregister_debugfs();
9778c2ecf20Sopenharmony_ci		pr_err("Failed to register pci driver.\n");
9788c2ecf20Sopenharmony_ci		return ret;
9798c2ecf20Sopenharmony_ci	}
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci	return 0;
9828c2ecf20Sopenharmony_ci}
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_cistatic void __exit sec_exit(void)
9858c2ecf20Sopenharmony_ci{
9868c2ecf20Sopenharmony_ci	pci_unregister_driver(&sec_pci_driver);
9878c2ecf20Sopenharmony_ci	sec_unregister_debugfs();
9888c2ecf20Sopenharmony_ci}
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_cimodule_init(sec_init);
9918c2ecf20Sopenharmony_cimodule_exit(sec_exit);
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
9948c2ecf20Sopenharmony_ciMODULE_AUTHOR("Zaibo Xu <xuzaibo@huawei.com>");
9958c2ecf20Sopenharmony_ciMODULE_AUTHOR("Longfang Liu <liulongfang@huawei.com>");
9968c2ecf20Sopenharmony_ciMODULE_AUTHOR("Kai Ye <yekai13@huawei.com>");
9978c2ecf20Sopenharmony_ciMODULE_AUTHOR("Wei Zhang <zhangwei375@huawei.com>");
9988c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Driver for HiSilicon SEC accelerator");
999