18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)
28c2ecf20Sopenharmony_ci/* Copyright(c) 2014 - 2020 Intel Corporation */
38c2ecf20Sopenharmony_ci#include <linux/slab.h>
48c2ecf20Sopenharmony_ci#include <linux/delay.h>
58c2ecf20Sopenharmony_ci#include <linux/pci_ids.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include "adf_accel_devices.h"
88c2ecf20Sopenharmony_ci#include "adf_common_drv.h"
98c2ecf20Sopenharmony_ci#include "icp_qat_hal.h"
108c2ecf20Sopenharmony_ci#include "icp_qat_uclo.h"
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#define BAD_REGADDR	       0xffff
138c2ecf20Sopenharmony_ci#define MAX_RETRY_TIMES	   10000
148c2ecf20Sopenharmony_ci#define INIT_CTX_ARB_VALUE	0x0
158c2ecf20Sopenharmony_ci#define INIT_CTX_ENABLE_VALUE     0x0
168c2ecf20Sopenharmony_ci#define INIT_PC_VALUE	     0x0
178c2ecf20Sopenharmony_ci#define INIT_WAKEUP_EVENTS_VALUE  0x1
188c2ecf20Sopenharmony_ci#define INIT_SIG_EVENTS_VALUE     0x1
198c2ecf20Sopenharmony_ci#define INIT_CCENABLE_VALUE       0x2000
208c2ecf20Sopenharmony_ci#define RST_CSR_QAT_LSB	   20
218c2ecf20Sopenharmony_ci#define RST_CSR_AE_LSB		  0
228c2ecf20Sopenharmony_ci#define MC_TIMESTAMP_ENABLE       (0x1 << 7)
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define IGNORE_W1C_MASK ((~(1 << CE_BREAKPOINT_BITPOS)) & \
258c2ecf20Sopenharmony_ci	(~(1 << CE_CNTL_STORE_PARITY_ERROR_BITPOS)) & \
268c2ecf20Sopenharmony_ci	(~(1 << CE_REG_PAR_ERR_BITPOS)))
278c2ecf20Sopenharmony_ci#define INSERT_IMMED_GPRA_CONST(inst, const_val) \
288c2ecf20Sopenharmony_ci	(inst = ((inst & 0xFFFF00C03FFull) | \
298c2ecf20Sopenharmony_ci		((((const_val) << 12) & 0x0FF00000ull) | \
308c2ecf20Sopenharmony_ci		(((const_val) << 10) & 0x0003FC00ull))))
318c2ecf20Sopenharmony_ci#define INSERT_IMMED_GPRB_CONST(inst, const_val) \
328c2ecf20Sopenharmony_ci	(inst = ((inst & 0xFFFF00FFF00ull) | \
338c2ecf20Sopenharmony_ci		((((const_val) << 12) & 0x0FF00000ull) | \
348c2ecf20Sopenharmony_ci		(((const_val) <<  0) & 0x000000FFull))))
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define AE(handle, ae) handle->hal_handle->aes[ae]
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic const u64 inst_4b[] = {
398c2ecf20Sopenharmony_ci	0x0F0400C0000ull, 0x0F4400C0000ull, 0x0F040000300ull, 0x0F440000300ull,
408c2ecf20Sopenharmony_ci	0x0FC066C0000ull, 0x0F0000C0300ull, 0x0F0000C0300ull, 0x0F0000C0300ull,
418c2ecf20Sopenharmony_ci	0x0A021000000ull
428c2ecf20Sopenharmony_ci};
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic const u64 inst[] = {
458c2ecf20Sopenharmony_ci	0x0F0000C0000ull, 0x0F000000380ull, 0x0D805000011ull, 0x0FC082C0300ull,
468c2ecf20Sopenharmony_ci	0x0F0000C0300ull, 0x0F0000C0300ull, 0x0F0000C0300ull, 0x0F0000C0300ull,
478c2ecf20Sopenharmony_ci	0x0A0643C0000ull, 0x0BAC0000301ull, 0x0D802000101ull, 0x0F0000C0001ull,
488c2ecf20Sopenharmony_ci	0x0FC066C0001ull, 0x0F0000C0300ull, 0x0F0000C0300ull, 0x0F0000C0300ull,
498c2ecf20Sopenharmony_ci	0x0F000400300ull, 0x0A0610C0000ull, 0x0BAC0000301ull, 0x0D804400101ull,
508c2ecf20Sopenharmony_ci	0x0A0580C0000ull, 0x0A0581C0000ull, 0x0A0582C0000ull, 0x0A0583C0000ull,
518c2ecf20Sopenharmony_ci	0x0A0584C0000ull, 0x0A0585C0000ull, 0x0A0586C0000ull, 0x0A0587C0000ull,
528c2ecf20Sopenharmony_ci	0x0A0588C0000ull, 0x0A0589C0000ull, 0x0A058AC0000ull, 0x0A058BC0000ull,
538c2ecf20Sopenharmony_ci	0x0A058CC0000ull, 0x0A058DC0000ull, 0x0A058EC0000ull, 0x0A058FC0000ull,
548c2ecf20Sopenharmony_ci	0x0A05C0C0000ull, 0x0A05C1C0000ull, 0x0A05C2C0000ull, 0x0A05C3C0000ull,
558c2ecf20Sopenharmony_ci	0x0A05C4C0000ull, 0x0A05C5C0000ull, 0x0A05C6C0000ull, 0x0A05C7C0000ull,
568c2ecf20Sopenharmony_ci	0x0A05C8C0000ull, 0x0A05C9C0000ull, 0x0A05CAC0000ull, 0x0A05CBC0000ull,
578c2ecf20Sopenharmony_ci	0x0A05CCC0000ull, 0x0A05CDC0000ull, 0x0A05CEC0000ull, 0x0A05CFC0000ull,
588c2ecf20Sopenharmony_ci	0x0A0400C0000ull, 0x0B0400C0000ull, 0x0A0401C0000ull, 0x0B0401C0000ull,
598c2ecf20Sopenharmony_ci	0x0A0402C0000ull, 0x0B0402C0000ull, 0x0A0403C0000ull, 0x0B0403C0000ull,
608c2ecf20Sopenharmony_ci	0x0A0404C0000ull, 0x0B0404C0000ull, 0x0A0405C0000ull, 0x0B0405C0000ull,
618c2ecf20Sopenharmony_ci	0x0A0406C0000ull, 0x0B0406C0000ull, 0x0A0407C0000ull, 0x0B0407C0000ull,
628c2ecf20Sopenharmony_ci	0x0A0408C0000ull, 0x0B0408C0000ull, 0x0A0409C0000ull, 0x0B0409C0000ull,
638c2ecf20Sopenharmony_ci	0x0A040AC0000ull, 0x0B040AC0000ull, 0x0A040BC0000ull, 0x0B040BC0000ull,
648c2ecf20Sopenharmony_ci	0x0A040CC0000ull, 0x0B040CC0000ull, 0x0A040DC0000ull, 0x0B040DC0000ull,
658c2ecf20Sopenharmony_ci	0x0A040EC0000ull, 0x0B040EC0000ull, 0x0A040FC0000ull, 0x0B040FC0000ull,
668c2ecf20Sopenharmony_ci	0x0D81581C010ull, 0x0E000010000ull, 0x0E000010000ull,
678c2ecf20Sopenharmony_ci};
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_civoid qat_hal_set_live_ctx(struct icp_qat_fw_loader_handle *handle,
708c2ecf20Sopenharmony_ci			  unsigned char ae, unsigned int ctx_mask)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	AE(handle, ae).live_ctx_mask = ctx_mask;
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#define CSR_RETRY_TIMES 500
768c2ecf20Sopenharmony_cistatic int qat_hal_rd_ae_csr(struct icp_qat_fw_loader_handle *handle,
778c2ecf20Sopenharmony_ci			     unsigned char ae, unsigned int csr)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	unsigned int iterations = CSR_RETRY_TIMES;
808c2ecf20Sopenharmony_ci	int value;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	do {
838c2ecf20Sopenharmony_ci		value = GET_AE_CSR(handle, ae, csr);
848c2ecf20Sopenharmony_ci		if (!(GET_AE_CSR(handle, ae, LOCAL_CSR_STATUS) & LCS_STATUS))
858c2ecf20Sopenharmony_ci			return value;
868c2ecf20Sopenharmony_ci	} while (iterations--);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	pr_err("QAT: Read CSR timeout\n");
898c2ecf20Sopenharmony_ci	return 0;
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic int qat_hal_wr_ae_csr(struct icp_qat_fw_loader_handle *handle,
938c2ecf20Sopenharmony_ci			     unsigned char ae, unsigned int csr,
948c2ecf20Sopenharmony_ci			     unsigned int value)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	unsigned int iterations = CSR_RETRY_TIMES;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	do {
998c2ecf20Sopenharmony_ci		SET_AE_CSR(handle, ae, csr, value);
1008c2ecf20Sopenharmony_ci		if (!(GET_AE_CSR(handle, ae, LOCAL_CSR_STATUS) & LCS_STATUS))
1018c2ecf20Sopenharmony_ci			return 0;
1028c2ecf20Sopenharmony_ci	} while (iterations--);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	pr_err("QAT: Write CSR Timeout\n");
1058c2ecf20Sopenharmony_ci	return -EFAULT;
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic void qat_hal_get_wakeup_event(struct icp_qat_fw_loader_handle *handle,
1098c2ecf20Sopenharmony_ci				     unsigned char ae, unsigned char ctx,
1108c2ecf20Sopenharmony_ci				     unsigned int *events)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	unsigned int cur_ctx;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	cur_ctx = qat_hal_rd_ae_csr(handle, ae, CSR_CTX_POINTER);
1158c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, ctx);
1168c2ecf20Sopenharmony_ci	*events = qat_hal_rd_ae_csr(handle, ae, CTX_WAKEUP_EVENTS_INDIRECT);
1178c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, cur_ctx);
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic int qat_hal_wait_cycles(struct icp_qat_fw_loader_handle *handle,
1218c2ecf20Sopenharmony_ci			       unsigned char ae, unsigned int cycles,
1228c2ecf20Sopenharmony_ci			       int chk_inactive)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	unsigned int base_cnt = 0, cur_cnt = 0;
1258c2ecf20Sopenharmony_ci	unsigned int csr = (1 << ACS_ABO_BITPOS);
1268c2ecf20Sopenharmony_ci	int times = MAX_RETRY_TIMES;
1278c2ecf20Sopenharmony_ci	int elapsed_cycles = 0;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	base_cnt = qat_hal_rd_ae_csr(handle, ae, PROFILE_COUNT);
1308c2ecf20Sopenharmony_ci	base_cnt &= 0xffff;
1318c2ecf20Sopenharmony_ci	while ((int)cycles > elapsed_cycles && times--) {
1328c2ecf20Sopenharmony_ci		if (chk_inactive)
1338c2ecf20Sopenharmony_ci			csr = qat_hal_rd_ae_csr(handle, ae, ACTIVE_CTX_STATUS);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci		cur_cnt = qat_hal_rd_ae_csr(handle, ae, PROFILE_COUNT);
1368c2ecf20Sopenharmony_ci		cur_cnt &= 0xffff;
1378c2ecf20Sopenharmony_ci		elapsed_cycles = cur_cnt - base_cnt;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci		if (elapsed_cycles < 0)
1408c2ecf20Sopenharmony_ci			elapsed_cycles += 0x10000;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci		/* ensure at least 8 time cycles elapsed in wait_cycles */
1438c2ecf20Sopenharmony_ci		if (elapsed_cycles >= 8 && !(csr & (1 << ACS_ABO_BITPOS)))
1448c2ecf20Sopenharmony_ci			return 0;
1458c2ecf20Sopenharmony_ci	}
1468c2ecf20Sopenharmony_ci	if (times < 0) {
1478c2ecf20Sopenharmony_ci		pr_err("QAT: wait_num_cycles time out\n");
1488c2ecf20Sopenharmony_ci		return -EFAULT;
1498c2ecf20Sopenharmony_ci	}
1508c2ecf20Sopenharmony_ci	return 0;
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci#define CLR_BIT(wrd, bit) (wrd & ~(1 << bit))
1548c2ecf20Sopenharmony_ci#define SET_BIT(wrd, bit) (wrd | 1 << bit)
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ciint qat_hal_set_ae_ctx_mode(struct icp_qat_fw_loader_handle *handle,
1578c2ecf20Sopenharmony_ci			    unsigned char ae, unsigned char mode)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	unsigned int csr, new_csr;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	if ((mode != 4) && (mode != 8)) {
1628c2ecf20Sopenharmony_ci		pr_err("QAT: bad ctx mode=%d\n", mode);
1638c2ecf20Sopenharmony_ci		return -EINVAL;
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	/* Sets the accelaration engine context mode to either four or eight */
1678c2ecf20Sopenharmony_ci	csr = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
1688c2ecf20Sopenharmony_ci	csr = IGNORE_W1C_MASK & csr;
1698c2ecf20Sopenharmony_ci	new_csr = (mode == 4) ?
1708c2ecf20Sopenharmony_ci		SET_BIT(csr, CE_INUSE_CONTEXTS_BITPOS) :
1718c2ecf20Sopenharmony_ci		CLR_BIT(csr, CE_INUSE_CONTEXTS_BITPOS);
1728c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, new_csr);
1738c2ecf20Sopenharmony_ci	return 0;
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ciint qat_hal_set_ae_nn_mode(struct icp_qat_fw_loader_handle *handle,
1778c2ecf20Sopenharmony_ci			   unsigned char ae, unsigned char mode)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	unsigned int csr, new_csr;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	csr = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
1828c2ecf20Sopenharmony_ci	csr &= IGNORE_W1C_MASK;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	new_csr = (mode) ?
1858c2ecf20Sopenharmony_ci		SET_BIT(csr, CE_NN_MODE_BITPOS) :
1868c2ecf20Sopenharmony_ci		CLR_BIT(csr, CE_NN_MODE_BITPOS);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	if (new_csr != csr)
1898c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, new_csr);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	return 0;
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ciint qat_hal_set_ae_lm_mode(struct icp_qat_fw_loader_handle *handle,
1958c2ecf20Sopenharmony_ci			   unsigned char ae, enum icp_qat_uof_regtype lm_type,
1968c2ecf20Sopenharmony_ci			   unsigned char mode)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	unsigned int csr, new_csr;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	csr = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
2018c2ecf20Sopenharmony_ci	csr &= IGNORE_W1C_MASK;
2028c2ecf20Sopenharmony_ci	switch (lm_type) {
2038c2ecf20Sopenharmony_ci	case ICP_LMEM0:
2048c2ecf20Sopenharmony_ci		new_csr = (mode) ?
2058c2ecf20Sopenharmony_ci			SET_BIT(csr, CE_LMADDR_0_GLOBAL_BITPOS) :
2068c2ecf20Sopenharmony_ci			CLR_BIT(csr, CE_LMADDR_0_GLOBAL_BITPOS);
2078c2ecf20Sopenharmony_ci		break;
2088c2ecf20Sopenharmony_ci	case ICP_LMEM1:
2098c2ecf20Sopenharmony_ci		new_csr = (mode) ?
2108c2ecf20Sopenharmony_ci			SET_BIT(csr, CE_LMADDR_1_GLOBAL_BITPOS) :
2118c2ecf20Sopenharmony_ci			CLR_BIT(csr, CE_LMADDR_1_GLOBAL_BITPOS);
2128c2ecf20Sopenharmony_ci		break;
2138c2ecf20Sopenharmony_ci	default:
2148c2ecf20Sopenharmony_ci		pr_err("QAT: lmType = 0x%x\n", lm_type);
2158c2ecf20Sopenharmony_ci		return -EINVAL;
2168c2ecf20Sopenharmony_ci	}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	if (new_csr != csr)
2198c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, new_csr);
2208c2ecf20Sopenharmony_ci	return 0;
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistatic unsigned short qat_hal_get_reg_addr(unsigned int type,
2248c2ecf20Sopenharmony_ci					   unsigned short reg_num)
2258c2ecf20Sopenharmony_ci{
2268c2ecf20Sopenharmony_ci	unsigned short reg_addr;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	switch (type) {
2298c2ecf20Sopenharmony_ci	case ICP_GPA_ABS:
2308c2ecf20Sopenharmony_ci	case ICP_GPB_ABS:
2318c2ecf20Sopenharmony_ci		reg_addr = 0x80 | (reg_num & 0x7f);
2328c2ecf20Sopenharmony_ci		break;
2338c2ecf20Sopenharmony_ci	case ICP_GPA_REL:
2348c2ecf20Sopenharmony_ci	case ICP_GPB_REL:
2358c2ecf20Sopenharmony_ci		reg_addr = reg_num & 0x1f;
2368c2ecf20Sopenharmony_ci		break;
2378c2ecf20Sopenharmony_ci	case ICP_SR_RD_REL:
2388c2ecf20Sopenharmony_ci	case ICP_SR_WR_REL:
2398c2ecf20Sopenharmony_ci	case ICP_SR_REL:
2408c2ecf20Sopenharmony_ci		reg_addr = 0x180 | (reg_num & 0x1f);
2418c2ecf20Sopenharmony_ci		break;
2428c2ecf20Sopenharmony_ci	case ICP_SR_ABS:
2438c2ecf20Sopenharmony_ci		reg_addr = 0x140 | ((reg_num & 0x3) << 1);
2448c2ecf20Sopenharmony_ci		break;
2458c2ecf20Sopenharmony_ci	case ICP_DR_RD_REL:
2468c2ecf20Sopenharmony_ci	case ICP_DR_WR_REL:
2478c2ecf20Sopenharmony_ci	case ICP_DR_REL:
2488c2ecf20Sopenharmony_ci		reg_addr = 0x1c0 | (reg_num & 0x1f);
2498c2ecf20Sopenharmony_ci		break;
2508c2ecf20Sopenharmony_ci	case ICP_DR_ABS:
2518c2ecf20Sopenharmony_ci		reg_addr = 0x100 | ((reg_num & 0x3) << 1);
2528c2ecf20Sopenharmony_ci		break;
2538c2ecf20Sopenharmony_ci	case ICP_NEIGH_REL:
2548c2ecf20Sopenharmony_ci		reg_addr = 0x280 | (reg_num & 0x1f);
2558c2ecf20Sopenharmony_ci		break;
2568c2ecf20Sopenharmony_ci	case ICP_LMEM0:
2578c2ecf20Sopenharmony_ci		reg_addr = 0x200;
2588c2ecf20Sopenharmony_ci		break;
2598c2ecf20Sopenharmony_ci	case ICP_LMEM1:
2608c2ecf20Sopenharmony_ci		reg_addr = 0x220;
2618c2ecf20Sopenharmony_ci		break;
2628c2ecf20Sopenharmony_ci	case ICP_NO_DEST:
2638c2ecf20Sopenharmony_ci		reg_addr = 0x300 | (reg_num & 0xff);
2648c2ecf20Sopenharmony_ci		break;
2658c2ecf20Sopenharmony_ci	default:
2668c2ecf20Sopenharmony_ci		reg_addr = BAD_REGADDR;
2678c2ecf20Sopenharmony_ci		break;
2688c2ecf20Sopenharmony_ci	}
2698c2ecf20Sopenharmony_ci	return reg_addr;
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_civoid qat_hal_reset(struct icp_qat_fw_loader_handle *handle)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	unsigned int ae_reset_csr;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	ae_reset_csr = GET_GLB_CSR(handle, ICP_RESET);
2778c2ecf20Sopenharmony_ci	ae_reset_csr |= handle->hal_handle->ae_mask << RST_CSR_AE_LSB;
2788c2ecf20Sopenharmony_ci	ae_reset_csr |= handle->hal_handle->slice_mask << RST_CSR_QAT_LSB;
2798c2ecf20Sopenharmony_ci	SET_GLB_CSR(handle, ICP_RESET, ae_reset_csr);
2808c2ecf20Sopenharmony_ci}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_cistatic void qat_hal_wr_indr_csr(struct icp_qat_fw_loader_handle *handle,
2838c2ecf20Sopenharmony_ci				unsigned char ae, unsigned int ctx_mask,
2848c2ecf20Sopenharmony_ci				unsigned int ae_csr, unsigned int csr_val)
2858c2ecf20Sopenharmony_ci{
2868c2ecf20Sopenharmony_ci	unsigned int ctx, cur_ctx;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	cur_ctx = qat_hal_rd_ae_csr(handle, ae, CSR_CTX_POINTER);
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	for (ctx = 0; ctx < ICP_QAT_UCLO_MAX_CTX; ctx++) {
2918c2ecf20Sopenharmony_ci		if (!(ctx_mask & (1 << ctx)))
2928c2ecf20Sopenharmony_ci			continue;
2938c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, ctx);
2948c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, ae_csr, csr_val);
2958c2ecf20Sopenharmony_ci	}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, cur_ctx);
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_cistatic unsigned int qat_hal_rd_indr_csr(struct icp_qat_fw_loader_handle *handle,
3018c2ecf20Sopenharmony_ci				unsigned char ae, unsigned char ctx,
3028c2ecf20Sopenharmony_ci				unsigned int ae_csr)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	unsigned int cur_ctx, csr_val;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	cur_ctx = qat_hal_rd_ae_csr(handle, ae, CSR_CTX_POINTER);
3078c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, ctx);
3088c2ecf20Sopenharmony_ci	csr_val = qat_hal_rd_ae_csr(handle, ae, ae_csr);
3098c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, cur_ctx);
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	return csr_val;
3128c2ecf20Sopenharmony_ci}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cistatic void qat_hal_put_sig_event(struct icp_qat_fw_loader_handle *handle,
3158c2ecf20Sopenharmony_ci				  unsigned char ae, unsigned int ctx_mask,
3168c2ecf20Sopenharmony_ci				  unsigned int events)
3178c2ecf20Sopenharmony_ci{
3188c2ecf20Sopenharmony_ci	unsigned int ctx, cur_ctx;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	cur_ctx = qat_hal_rd_ae_csr(handle, ae, CSR_CTX_POINTER);
3218c2ecf20Sopenharmony_ci	for (ctx = 0; ctx < ICP_QAT_UCLO_MAX_CTX; ctx++) {
3228c2ecf20Sopenharmony_ci		if (!(ctx_mask & (1 << ctx)))
3238c2ecf20Sopenharmony_ci			continue;
3248c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, ctx);
3258c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, CTX_SIG_EVENTS_INDIRECT, events);
3268c2ecf20Sopenharmony_ci	}
3278c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, cur_ctx);
3288c2ecf20Sopenharmony_ci}
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_cistatic void qat_hal_put_wakeup_event(struct icp_qat_fw_loader_handle *handle,
3318c2ecf20Sopenharmony_ci				     unsigned char ae, unsigned int ctx_mask,
3328c2ecf20Sopenharmony_ci				     unsigned int events)
3338c2ecf20Sopenharmony_ci{
3348c2ecf20Sopenharmony_ci	unsigned int ctx, cur_ctx;
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	cur_ctx = qat_hal_rd_ae_csr(handle, ae, CSR_CTX_POINTER);
3378c2ecf20Sopenharmony_ci	for (ctx = 0; ctx < ICP_QAT_UCLO_MAX_CTX; ctx++) {
3388c2ecf20Sopenharmony_ci		if (!(ctx_mask & (1 << ctx)))
3398c2ecf20Sopenharmony_ci			continue;
3408c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, ctx);
3418c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, CTX_WAKEUP_EVENTS_INDIRECT,
3428c2ecf20Sopenharmony_ci				  events);
3438c2ecf20Sopenharmony_ci	}
3448c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, cur_ctx);
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_cistatic int qat_hal_check_ae_alive(struct icp_qat_fw_loader_handle *handle)
3488c2ecf20Sopenharmony_ci{
3498c2ecf20Sopenharmony_ci	unsigned long ae_mask = handle->hal_handle->ae_mask;
3508c2ecf20Sopenharmony_ci	unsigned int base_cnt, cur_cnt;
3518c2ecf20Sopenharmony_ci	unsigned char ae;
3528c2ecf20Sopenharmony_ci	int times = MAX_RETRY_TIMES;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	for_each_set_bit(ae, &ae_mask, handle->hal_handle->ae_max_num) {
3558c2ecf20Sopenharmony_ci		base_cnt = qat_hal_rd_ae_csr(handle, ae, PROFILE_COUNT);
3568c2ecf20Sopenharmony_ci		base_cnt &= 0xffff;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci		do {
3598c2ecf20Sopenharmony_ci			cur_cnt = qat_hal_rd_ae_csr(handle, ae, PROFILE_COUNT);
3608c2ecf20Sopenharmony_ci			cur_cnt &= 0xffff;
3618c2ecf20Sopenharmony_ci		} while (times-- && (cur_cnt == base_cnt));
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci		if (times < 0) {
3648c2ecf20Sopenharmony_ci			pr_err("QAT: AE%d is inactive!!\n", ae);
3658c2ecf20Sopenharmony_ci			return -EFAULT;
3668c2ecf20Sopenharmony_ci		}
3678c2ecf20Sopenharmony_ci	}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	return 0;
3708c2ecf20Sopenharmony_ci}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ciint qat_hal_check_ae_active(struct icp_qat_fw_loader_handle *handle,
3738c2ecf20Sopenharmony_ci			    unsigned int ae)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	unsigned int enable = 0, active = 0;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	enable = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
3788c2ecf20Sopenharmony_ci	active = qat_hal_rd_ae_csr(handle, ae, ACTIVE_CTX_STATUS);
3798c2ecf20Sopenharmony_ci	if ((enable & (0xff << CE_ENABLE_BITPOS)) ||
3808c2ecf20Sopenharmony_ci	    (active & (1 << ACS_ABO_BITPOS)))
3818c2ecf20Sopenharmony_ci		return 1;
3828c2ecf20Sopenharmony_ci	else
3838c2ecf20Sopenharmony_ci		return 0;
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_cistatic void qat_hal_reset_timestamp(struct icp_qat_fw_loader_handle *handle)
3878c2ecf20Sopenharmony_ci{
3888c2ecf20Sopenharmony_ci	unsigned long ae_mask = handle->hal_handle->ae_mask;
3898c2ecf20Sopenharmony_ci	unsigned int misc_ctl;
3908c2ecf20Sopenharmony_ci	unsigned char ae;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	/* stop the timestamp timers */
3938c2ecf20Sopenharmony_ci	misc_ctl = GET_GLB_CSR(handle, MISC_CONTROL);
3948c2ecf20Sopenharmony_ci	if (misc_ctl & MC_TIMESTAMP_ENABLE)
3958c2ecf20Sopenharmony_ci		SET_GLB_CSR(handle, MISC_CONTROL, misc_ctl &
3968c2ecf20Sopenharmony_ci			    (~MC_TIMESTAMP_ENABLE));
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	for_each_set_bit(ae, &ae_mask, handle->hal_handle->ae_max_num) {
3998c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, TIMESTAMP_LOW, 0);
4008c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, TIMESTAMP_HIGH, 0);
4018c2ecf20Sopenharmony_ci	}
4028c2ecf20Sopenharmony_ci	/* start timestamp timers */
4038c2ecf20Sopenharmony_ci	SET_GLB_CSR(handle, MISC_CONTROL, misc_ctl | MC_TIMESTAMP_ENABLE);
4048c2ecf20Sopenharmony_ci}
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci#define ESRAM_AUTO_TINIT	BIT(2)
4078c2ecf20Sopenharmony_ci#define ESRAM_AUTO_TINIT_DONE	BIT(3)
4088c2ecf20Sopenharmony_ci#define ESRAM_AUTO_INIT_USED_CYCLES (1640)
4098c2ecf20Sopenharmony_ci#define ESRAM_AUTO_INIT_CSR_OFFSET 0xC1C
4108c2ecf20Sopenharmony_cistatic int qat_hal_init_esram(struct icp_qat_fw_loader_handle *handle)
4118c2ecf20Sopenharmony_ci{
4128c2ecf20Sopenharmony_ci	void __iomem *csr_addr =
4138c2ecf20Sopenharmony_ci			(void __iomem *)((uintptr_t)handle->hal_ep_csr_addr_v +
4148c2ecf20Sopenharmony_ci			ESRAM_AUTO_INIT_CSR_OFFSET);
4158c2ecf20Sopenharmony_ci	unsigned int csr_val;
4168c2ecf20Sopenharmony_ci	int times = 30;
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	if (handle->pci_dev->device != PCI_DEVICE_ID_INTEL_QAT_DH895XCC)
4198c2ecf20Sopenharmony_ci		return 0;
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	csr_val = ADF_CSR_RD(csr_addr, 0);
4228c2ecf20Sopenharmony_ci	if ((csr_val & ESRAM_AUTO_TINIT) && (csr_val & ESRAM_AUTO_TINIT_DONE))
4238c2ecf20Sopenharmony_ci		return 0;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	csr_val = ADF_CSR_RD(csr_addr, 0);
4268c2ecf20Sopenharmony_ci	csr_val |= ESRAM_AUTO_TINIT;
4278c2ecf20Sopenharmony_ci	ADF_CSR_WR(csr_addr, 0, csr_val);
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	do {
4308c2ecf20Sopenharmony_ci		qat_hal_wait_cycles(handle, 0, ESRAM_AUTO_INIT_USED_CYCLES, 0);
4318c2ecf20Sopenharmony_ci		csr_val = ADF_CSR_RD(csr_addr, 0);
4328c2ecf20Sopenharmony_ci	} while (!(csr_val & ESRAM_AUTO_TINIT_DONE) && times--);
4338c2ecf20Sopenharmony_ci	if ((times < 0)) {
4348c2ecf20Sopenharmony_ci		pr_err("QAT: Fail to init eSram!\n");
4358c2ecf20Sopenharmony_ci		return -EFAULT;
4368c2ecf20Sopenharmony_ci	}
4378c2ecf20Sopenharmony_ci	return 0;
4388c2ecf20Sopenharmony_ci}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci#define SHRAM_INIT_CYCLES 2060
4418c2ecf20Sopenharmony_ciint qat_hal_clr_reset(struct icp_qat_fw_loader_handle *handle)
4428c2ecf20Sopenharmony_ci{
4438c2ecf20Sopenharmony_ci	unsigned long ae_mask = handle->hal_handle->ae_mask;
4448c2ecf20Sopenharmony_ci	unsigned int ae_reset_csr;
4458c2ecf20Sopenharmony_ci	unsigned char ae;
4468c2ecf20Sopenharmony_ci	unsigned int clk_csr;
4478c2ecf20Sopenharmony_ci	unsigned int times = 100;
4488c2ecf20Sopenharmony_ci	unsigned int csr;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	/* write to the reset csr */
4518c2ecf20Sopenharmony_ci	ae_reset_csr = GET_GLB_CSR(handle, ICP_RESET);
4528c2ecf20Sopenharmony_ci	ae_reset_csr &= ~(handle->hal_handle->ae_mask << RST_CSR_AE_LSB);
4538c2ecf20Sopenharmony_ci	ae_reset_csr &= ~(handle->hal_handle->slice_mask << RST_CSR_QAT_LSB);
4548c2ecf20Sopenharmony_ci	do {
4558c2ecf20Sopenharmony_ci		SET_GLB_CSR(handle, ICP_RESET, ae_reset_csr);
4568c2ecf20Sopenharmony_ci		if (!(times--))
4578c2ecf20Sopenharmony_ci			goto out_err;
4588c2ecf20Sopenharmony_ci		csr = GET_GLB_CSR(handle, ICP_RESET);
4598c2ecf20Sopenharmony_ci	} while ((handle->hal_handle->ae_mask |
4608c2ecf20Sopenharmony_ci		 (handle->hal_handle->slice_mask << RST_CSR_QAT_LSB)) & csr);
4618c2ecf20Sopenharmony_ci	/* enable clock */
4628c2ecf20Sopenharmony_ci	clk_csr = GET_GLB_CSR(handle, ICP_GLOBAL_CLK_ENABLE);
4638c2ecf20Sopenharmony_ci	clk_csr |= handle->hal_handle->ae_mask << 0;
4648c2ecf20Sopenharmony_ci	clk_csr |= handle->hal_handle->slice_mask << 20;
4658c2ecf20Sopenharmony_ci	SET_GLB_CSR(handle, ICP_GLOBAL_CLK_ENABLE, clk_csr);
4668c2ecf20Sopenharmony_ci	if (qat_hal_check_ae_alive(handle))
4678c2ecf20Sopenharmony_ci		goto out_err;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	/* Set undefined power-up/reset states to reasonable default values */
4708c2ecf20Sopenharmony_ci	for_each_set_bit(ae, &ae_mask, handle->hal_handle->ae_max_num) {
4718c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES,
4728c2ecf20Sopenharmony_ci				  INIT_CTX_ENABLE_VALUE);
4738c2ecf20Sopenharmony_ci		qat_hal_wr_indr_csr(handle, ae, ICP_QAT_UCLO_AE_ALL_CTX,
4748c2ecf20Sopenharmony_ci				    CTX_STS_INDIRECT,
4758c2ecf20Sopenharmony_ci				    handle->hal_handle->upc_mask &
4768c2ecf20Sopenharmony_ci				    INIT_PC_VALUE);
4778c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, CTX_ARB_CNTL, INIT_CTX_ARB_VALUE);
4788c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, CC_ENABLE, INIT_CCENABLE_VALUE);
4798c2ecf20Sopenharmony_ci		qat_hal_put_wakeup_event(handle, ae,
4808c2ecf20Sopenharmony_ci					 ICP_QAT_UCLO_AE_ALL_CTX,
4818c2ecf20Sopenharmony_ci					 INIT_WAKEUP_EVENTS_VALUE);
4828c2ecf20Sopenharmony_ci		qat_hal_put_sig_event(handle, ae,
4838c2ecf20Sopenharmony_ci				      ICP_QAT_UCLO_AE_ALL_CTX,
4848c2ecf20Sopenharmony_ci				      INIT_SIG_EVENTS_VALUE);
4858c2ecf20Sopenharmony_ci	}
4868c2ecf20Sopenharmony_ci	if (qat_hal_init_esram(handle))
4878c2ecf20Sopenharmony_ci		goto out_err;
4888c2ecf20Sopenharmony_ci	if (qat_hal_wait_cycles(handle, 0, SHRAM_INIT_CYCLES, 0))
4898c2ecf20Sopenharmony_ci		goto out_err;
4908c2ecf20Sopenharmony_ci	qat_hal_reset_timestamp(handle);
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	return 0;
4938c2ecf20Sopenharmony_ciout_err:
4948c2ecf20Sopenharmony_ci	pr_err("QAT: failed to get device out of reset\n");
4958c2ecf20Sopenharmony_ci	return -EFAULT;
4968c2ecf20Sopenharmony_ci}
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_cistatic void qat_hal_disable_ctx(struct icp_qat_fw_loader_handle *handle,
4998c2ecf20Sopenharmony_ci				unsigned char ae, unsigned int ctx_mask)
5008c2ecf20Sopenharmony_ci{
5018c2ecf20Sopenharmony_ci	unsigned int ctx;
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	ctx = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
5048c2ecf20Sopenharmony_ci	ctx &= IGNORE_W1C_MASK &
5058c2ecf20Sopenharmony_ci		(~((ctx_mask & ICP_QAT_UCLO_AE_ALL_CTX) << CE_ENABLE_BITPOS));
5068c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx);
5078c2ecf20Sopenharmony_ci}
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_cistatic u64 qat_hal_parity_64bit(u64 word)
5108c2ecf20Sopenharmony_ci{
5118c2ecf20Sopenharmony_ci	word ^= word >> 1;
5128c2ecf20Sopenharmony_ci	word ^= word >> 2;
5138c2ecf20Sopenharmony_ci	word ^= word >> 4;
5148c2ecf20Sopenharmony_ci	word ^= word >> 8;
5158c2ecf20Sopenharmony_ci	word ^= word >> 16;
5168c2ecf20Sopenharmony_ci	word ^= word >> 32;
5178c2ecf20Sopenharmony_ci	return word & 1;
5188c2ecf20Sopenharmony_ci}
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_cistatic u64 qat_hal_set_uword_ecc(u64 uword)
5218c2ecf20Sopenharmony_ci{
5228c2ecf20Sopenharmony_ci	u64 bit0_mask = 0xff800007fffULL, bit1_mask = 0x1f801ff801fULL,
5238c2ecf20Sopenharmony_ci		bit2_mask = 0xe387e0781e1ULL, bit3_mask = 0x7cb8e388e22ULL,
5248c2ecf20Sopenharmony_ci		bit4_mask = 0xaf5b2c93244ULL, bit5_mask = 0xf56d5525488ULL,
5258c2ecf20Sopenharmony_ci		bit6_mask = 0xdaf69a46910ULL;
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	/* clear the ecc bits */
5288c2ecf20Sopenharmony_ci	uword &= ~(0x7fULL << 0x2C);
5298c2ecf20Sopenharmony_ci	uword |= qat_hal_parity_64bit(bit0_mask & uword) << 0x2C;
5308c2ecf20Sopenharmony_ci	uword |= qat_hal_parity_64bit(bit1_mask & uword) << 0x2D;
5318c2ecf20Sopenharmony_ci	uword |= qat_hal_parity_64bit(bit2_mask & uword) << 0x2E;
5328c2ecf20Sopenharmony_ci	uword |= qat_hal_parity_64bit(bit3_mask & uword) << 0x2F;
5338c2ecf20Sopenharmony_ci	uword |= qat_hal_parity_64bit(bit4_mask & uword) << 0x30;
5348c2ecf20Sopenharmony_ci	uword |= qat_hal_parity_64bit(bit5_mask & uword) << 0x31;
5358c2ecf20Sopenharmony_ci	uword |= qat_hal_parity_64bit(bit6_mask & uword) << 0x32;
5368c2ecf20Sopenharmony_ci	return uword;
5378c2ecf20Sopenharmony_ci}
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_civoid qat_hal_wr_uwords(struct icp_qat_fw_loader_handle *handle,
5408c2ecf20Sopenharmony_ci		       unsigned char ae, unsigned int uaddr,
5418c2ecf20Sopenharmony_ci		       unsigned int words_num, u64 *uword)
5428c2ecf20Sopenharmony_ci{
5438c2ecf20Sopenharmony_ci	unsigned int ustore_addr;
5448c2ecf20Sopenharmony_ci	unsigned int i;
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	ustore_addr = qat_hal_rd_ae_csr(handle, ae, USTORE_ADDRESS);
5478c2ecf20Sopenharmony_ci	uaddr |= UA_ECS;
5488c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, uaddr);
5498c2ecf20Sopenharmony_ci	for (i = 0; i < words_num; i++) {
5508c2ecf20Sopenharmony_ci		unsigned int uwrd_lo, uwrd_hi;
5518c2ecf20Sopenharmony_ci		u64 tmp;
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci		tmp = qat_hal_set_uword_ecc(uword[i]);
5548c2ecf20Sopenharmony_ci		uwrd_lo = (unsigned int)(tmp & 0xffffffff);
5558c2ecf20Sopenharmony_ci		uwrd_hi = (unsigned int)(tmp >> 0x20);
5568c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, USTORE_DATA_LOWER, uwrd_lo);
5578c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, USTORE_DATA_UPPER, uwrd_hi);
5588c2ecf20Sopenharmony_ci	}
5598c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, ustore_addr);
5608c2ecf20Sopenharmony_ci}
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_cistatic void qat_hal_enable_ctx(struct icp_qat_fw_loader_handle *handle,
5638c2ecf20Sopenharmony_ci			       unsigned char ae, unsigned int ctx_mask)
5648c2ecf20Sopenharmony_ci{
5658c2ecf20Sopenharmony_ci	unsigned int ctx;
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	ctx = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
5688c2ecf20Sopenharmony_ci	ctx &= IGNORE_W1C_MASK;
5698c2ecf20Sopenharmony_ci	ctx_mask &= (ctx & CE_INUSE_CONTEXTS) ? 0x55 : 0xFF;
5708c2ecf20Sopenharmony_ci	ctx |= (ctx_mask << CE_ENABLE_BITPOS);
5718c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx);
5728c2ecf20Sopenharmony_ci}
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_cistatic void qat_hal_clear_xfer(struct icp_qat_fw_loader_handle *handle)
5758c2ecf20Sopenharmony_ci{
5768c2ecf20Sopenharmony_ci	unsigned long ae_mask = handle->hal_handle->ae_mask;
5778c2ecf20Sopenharmony_ci	unsigned char ae;
5788c2ecf20Sopenharmony_ci	unsigned short reg;
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	for_each_set_bit(ae, &ae_mask, handle->hal_handle->ae_max_num) {
5818c2ecf20Sopenharmony_ci		for (reg = 0; reg < ICP_QAT_UCLO_MAX_GPR_REG; reg++) {
5828c2ecf20Sopenharmony_ci			qat_hal_init_rd_xfer(handle, ae, 0, ICP_SR_RD_ABS,
5838c2ecf20Sopenharmony_ci					     reg, 0);
5848c2ecf20Sopenharmony_ci			qat_hal_init_rd_xfer(handle, ae, 0, ICP_DR_RD_ABS,
5858c2ecf20Sopenharmony_ci					     reg, 0);
5868c2ecf20Sopenharmony_ci		}
5878c2ecf20Sopenharmony_ci	}
5888c2ecf20Sopenharmony_ci}
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_cistatic int qat_hal_clear_gpr(struct icp_qat_fw_loader_handle *handle)
5918c2ecf20Sopenharmony_ci{
5928c2ecf20Sopenharmony_ci	unsigned long ae_mask = handle->hal_handle->ae_mask;
5938c2ecf20Sopenharmony_ci	unsigned char ae;
5948c2ecf20Sopenharmony_ci	unsigned int ctx_mask = ICP_QAT_UCLO_AE_ALL_CTX;
5958c2ecf20Sopenharmony_ci	int times = MAX_RETRY_TIMES;
5968c2ecf20Sopenharmony_ci	unsigned int csr_val = 0;
5978c2ecf20Sopenharmony_ci	unsigned int savctx = 0;
5988c2ecf20Sopenharmony_ci	int ret = 0;
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	for_each_set_bit(ae, &ae_mask, handle->hal_handle->ae_max_num) {
6018c2ecf20Sopenharmony_ci		csr_val = qat_hal_rd_ae_csr(handle, ae, AE_MISC_CONTROL);
6028c2ecf20Sopenharmony_ci		csr_val &= ~(1 << MMC_SHARE_CS_BITPOS);
6038c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, AE_MISC_CONTROL, csr_val);
6048c2ecf20Sopenharmony_ci		csr_val = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
6058c2ecf20Sopenharmony_ci		csr_val &= IGNORE_W1C_MASK;
6068c2ecf20Sopenharmony_ci		csr_val |= CE_NN_MODE;
6078c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, csr_val);
6088c2ecf20Sopenharmony_ci		qat_hal_wr_uwords(handle, ae, 0, ARRAY_SIZE(inst),
6098c2ecf20Sopenharmony_ci				  (u64 *)inst);
6108c2ecf20Sopenharmony_ci		qat_hal_wr_indr_csr(handle, ae, ctx_mask, CTX_STS_INDIRECT,
6118c2ecf20Sopenharmony_ci				    handle->hal_handle->upc_mask &
6128c2ecf20Sopenharmony_ci				    INIT_PC_VALUE);
6138c2ecf20Sopenharmony_ci		savctx = qat_hal_rd_ae_csr(handle, ae, ACTIVE_CTX_STATUS);
6148c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, ACTIVE_CTX_STATUS, 0);
6158c2ecf20Sopenharmony_ci		qat_hal_put_wakeup_event(handle, ae, ctx_mask, XCWE_VOLUNTARY);
6168c2ecf20Sopenharmony_ci		qat_hal_wr_indr_csr(handle, ae, ctx_mask,
6178c2ecf20Sopenharmony_ci				    CTX_SIG_EVENTS_INDIRECT, 0);
6188c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, CTX_SIG_EVENTS_ACTIVE, 0);
6198c2ecf20Sopenharmony_ci		qat_hal_enable_ctx(handle, ae, ctx_mask);
6208c2ecf20Sopenharmony_ci	}
6218c2ecf20Sopenharmony_ci	for_each_set_bit(ae, &ae_mask, handle->hal_handle->ae_max_num) {
6228c2ecf20Sopenharmony_ci		/* wait for AE to finish */
6238c2ecf20Sopenharmony_ci		do {
6248c2ecf20Sopenharmony_ci			ret = qat_hal_wait_cycles(handle, ae, 20, 1);
6258c2ecf20Sopenharmony_ci		} while (ret && times--);
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci		if (times < 0) {
6288c2ecf20Sopenharmony_ci			pr_err("QAT: clear GPR of AE %d failed", ae);
6298c2ecf20Sopenharmony_ci			return -EINVAL;
6308c2ecf20Sopenharmony_ci		}
6318c2ecf20Sopenharmony_ci		qat_hal_disable_ctx(handle, ae, ctx_mask);
6328c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, ACTIVE_CTX_STATUS,
6338c2ecf20Sopenharmony_ci				  savctx & ACS_ACNO);
6348c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES,
6358c2ecf20Sopenharmony_ci				  INIT_CTX_ENABLE_VALUE);
6368c2ecf20Sopenharmony_ci		qat_hal_wr_indr_csr(handle, ae, ctx_mask, CTX_STS_INDIRECT,
6378c2ecf20Sopenharmony_ci				    handle->hal_handle->upc_mask &
6388c2ecf20Sopenharmony_ci				    INIT_PC_VALUE);
6398c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, CTX_ARB_CNTL, INIT_CTX_ARB_VALUE);
6408c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, CC_ENABLE, INIT_CCENABLE_VALUE);
6418c2ecf20Sopenharmony_ci		qat_hal_put_wakeup_event(handle, ae, ctx_mask,
6428c2ecf20Sopenharmony_ci					 INIT_WAKEUP_EVENTS_VALUE);
6438c2ecf20Sopenharmony_ci		qat_hal_put_sig_event(handle, ae, ctx_mask,
6448c2ecf20Sopenharmony_ci				      INIT_SIG_EVENTS_VALUE);
6458c2ecf20Sopenharmony_ci	}
6468c2ecf20Sopenharmony_ci	return 0;
6478c2ecf20Sopenharmony_ci}
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci#define ICP_QAT_AE_OFFSET	0x20000
6508c2ecf20Sopenharmony_ci#define ICP_QAT_CAP_OFFSET       (ICP_QAT_AE_OFFSET + 0x10000)
6518c2ecf20Sopenharmony_ci#define LOCAL_TO_XFER_REG_OFFSET    0x800
6528c2ecf20Sopenharmony_ci#define ICP_QAT_EP_OFFSET	0x3a000
6538c2ecf20Sopenharmony_ciint qat_hal_init(struct adf_accel_dev *accel_dev)
6548c2ecf20Sopenharmony_ci{
6558c2ecf20Sopenharmony_ci	unsigned char ae;
6568c2ecf20Sopenharmony_ci	unsigned int max_en_ae_id = 0;
6578c2ecf20Sopenharmony_ci	struct icp_qat_fw_loader_handle *handle;
6588c2ecf20Sopenharmony_ci	struct adf_accel_pci *pci_info = &accel_dev->accel_pci_dev;
6598c2ecf20Sopenharmony_ci	struct adf_hw_device_data *hw_data = accel_dev->hw_device;
6608c2ecf20Sopenharmony_ci	struct adf_bar *misc_bar =
6618c2ecf20Sopenharmony_ci			&pci_info->pci_bars[hw_data->get_misc_bar_id(hw_data)];
6628c2ecf20Sopenharmony_ci	unsigned long ae_mask = hw_data->ae_mask;
6638c2ecf20Sopenharmony_ci	unsigned int csr_val = 0;
6648c2ecf20Sopenharmony_ci	struct adf_bar *sram_bar;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	handle = kzalloc(sizeof(*handle), GFP_KERNEL);
6678c2ecf20Sopenharmony_ci	if (!handle)
6688c2ecf20Sopenharmony_ci		return -ENOMEM;
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	handle->hal_cap_g_ctl_csr_addr_v =
6718c2ecf20Sopenharmony_ci		(void __iomem *)((uintptr_t)misc_bar->virt_addr +
6728c2ecf20Sopenharmony_ci				 ICP_QAT_CAP_OFFSET);
6738c2ecf20Sopenharmony_ci	handle->hal_cap_ae_xfer_csr_addr_v =
6748c2ecf20Sopenharmony_ci		(void __iomem *)((uintptr_t)misc_bar->virt_addr +
6758c2ecf20Sopenharmony_ci				 ICP_QAT_AE_OFFSET);
6768c2ecf20Sopenharmony_ci	handle->hal_ep_csr_addr_v =
6778c2ecf20Sopenharmony_ci		(void __iomem *)((uintptr_t)misc_bar->virt_addr +
6788c2ecf20Sopenharmony_ci				 ICP_QAT_EP_OFFSET);
6798c2ecf20Sopenharmony_ci	handle->hal_cap_ae_local_csr_addr_v =
6808c2ecf20Sopenharmony_ci		(void __iomem *)((uintptr_t)handle->hal_cap_ae_xfer_csr_addr_v +
6818c2ecf20Sopenharmony_ci				 LOCAL_TO_XFER_REG_OFFSET);
6828c2ecf20Sopenharmony_ci	handle->pci_dev = pci_info->pci_dev;
6838c2ecf20Sopenharmony_ci	if (handle->pci_dev->device == PCI_DEVICE_ID_INTEL_QAT_DH895XCC) {
6848c2ecf20Sopenharmony_ci		sram_bar =
6858c2ecf20Sopenharmony_ci			&pci_info->pci_bars[hw_data->get_sram_bar_id(hw_data)];
6868c2ecf20Sopenharmony_ci		handle->hal_sram_addr_v = sram_bar->virt_addr;
6878c2ecf20Sopenharmony_ci	}
6888c2ecf20Sopenharmony_ci	handle->fw_auth = (handle->pci_dev->device ==
6898c2ecf20Sopenharmony_ci			   PCI_DEVICE_ID_INTEL_QAT_DH895XCC) ? false : true;
6908c2ecf20Sopenharmony_ci	handle->hal_handle = kzalloc(sizeof(*handle->hal_handle), GFP_KERNEL);
6918c2ecf20Sopenharmony_ci	if (!handle->hal_handle)
6928c2ecf20Sopenharmony_ci		goto out_hal_handle;
6938c2ecf20Sopenharmony_ci	handle->hal_handle->revision_id = accel_dev->accel_pci_dev.revid;
6948c2ecf20Sopenharmony_ci	handle->hal_handle->ae_mask = hw_data->ae_mask;
6958c2ecf20Sopenharmony_ci	handle->hal_handle->slice_mask = hw_data->accel_mask;
6968c2ecf20Sopenharmony_ci	/* create AE objects */
6978c2ecf20Sopenharmony_ci	handle->hal_handle->upc_mask = 0x1ffff;
6988c2ecf20Sopenharmony_ci	handle->hal_handle->max_ustore = 0x4000;
6998c2ecf20Sopenharmony_ci	for_each_set_bit(ae, &ae_mask, ICP_QAT_UCLO_MAX_AE) {
7008c2ecf20Sopenharmony_ci		handle->hal_handle->aes[ae].free_addr = 0;
7018c2ecf20Sopenharmony_ci		handle->hal_handle->aes[ae].free_size =
7028c2ecf20Sopenharmony_ci		    handle->hal_handle->max_ustore;
7038c2ecf20Sopenharmony_ci		handle->hal_handle->aes[ae].ustore_size =
7048c2ecf20Sopenharmony_ci		    handle->hal_handle->max_ustore;
7058c2ecf20Sopenharmony_ci		handle->hal_handle->aes[ae].live_ctx_mask =
7068c2ecf20Sopenharmony_ci						ICP_QAT_UCLO_AE_ALL_CTX;
7078c2ecf20Sopenharmony_ci		max_en_ae_id = ae;
7088c2ecf20Sopenharmony_ci	}
7098c2ecf20Sopenharmony_ci	handle->hal_handle->ae_max_num = max_en_ae_id + 1;
7108c2ecf20Sopenharmony_ci	/* take all AEs out of reset */
7118c2ecf20Sopenharmony_ci	if (qat_hal_clr_reset(handle)) {
7128c2ecf20Sopenharmony_ci		dev_err(&GET_DEV(accel_dev), "qat_hal_clr_reset error\n");
7138c2ecf20Sopenharmony_ci		goto out_err;
7148c2ecf20Sopenharmony_ci	}
7158c2ecf20Sopenharmony_ci	qat_hal_clear_xfer(handle);
7168c2ecf20Sopenharmony_ci	if (!handle->fw_auth) {
7178c2ecf20Sopenharmony_ci		if (qat_hal_clear_gpr(handle))
7188c2ecf20Sopenharmony_ci			goto out_err;
7198c2ecf20Sopenharmony_ci	}
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	/* Set SIGNATURE_ENABLE[0] to 0x1 in order to enable ALU_OUT csr */
7228c2ecf20Sopenharmony_ci	for_each_set_bit(ae, &ae_mask, handle->hal_handle->ae_max_num) {
7238c2ecf20Sopenharmony_ci		csr_val = qat_hal_rd_ae_csr(handle, ae, SIGNATURE_ENABLE);
7248c2ecf20Sopenharmony_ci		csr_val |= 0x1;
7258c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, SIGNATURE_ENABLE, csr_val);
7268c2ecf20Sopenharmony_ci	}
7278c2ecf20Sopenharmony_ci	accel_dev->fw_loader->fw_loader = handle;
7288c2ecf20Sopenharmony_ci	return 0;
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ciout_err:
7318c2ecf20Sopenharmony_ci	kfree(handle->hal_handle);
7328c2ecf20Sopenharmony_ciout_hal_handle:
7338c2ecf20Sopenharmony_ci	kfree(handle);
7348c2ecf20Sopenharmony_ci	return -EFAULT;
7358c2ecf20Sopenharmony_ci}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_civoid qat_hal_deinit(struct icp_qat_fw_loader_handle *handle)
7388c2ecf20Sopenharmony_ci{
7398c2ecf20Sopenharmony_ci	if (!handle)
7408c2ecf20Sopenharmony_ci		return;
7418c2ecf20Sopenharmony_ci	kfree(handle->hal_handle);
7428c2ecf20Sopenharmony_ci	kfree(handle);
7438c2ecf20Sopenharmony_ci}
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_civoid qat_hal_start(struct icp_qat_fw_loader_handle *handle, unsigned char ae,
7468c2ecf20Sopenharmony_ci		   unsigned int ctx_mask)
7478c2ecf20Sopenharmony_ci{
7488c2ecf20Sopenharmony_ci	int retry = 0;
7498c2ecf20Sopenharmony_ci	unsigned int fcu_sts = 0;
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	if (handle->fw_auth) {
7528c2ecf20Sopenharmony_ci		SET_CAP_CSR(handle, FCU_CONTROL, FCU_CTRL_CMD_START);
7538c2ecf20Sopenharmony_ci		do {
7548c2ecf20Sopenharmony_ci			msleep(FW_AUTH_WAIT_PERIOD);
7558c2ecf20Sopenharmony_ci			fcu_sts = GET_CAP_CSR(handle, FCU_STATUS);
7568c2ecf20Sopenharmony_ci			if (((fcu_sts >> FCU_STS_DONE_POS) & 0x1))
7578c2ecf20Sopenharmony_ci				return;
7588c2ecf20Sopenharmony_ci		} while (retry++ < FW_AUTH_MAX_RETRY);
7598c2ecf20Sopenharmony_ci		pr_err("QAT: start error (AE 0x%x FCU_STS = 0x%x)\n", ae,
7608c2ecf20Sopenharmony_ci		       fcu_sts);
7618c2ecf20Sopenharmony_ci	} else {
7628c2ecf20Sopenharmony_ci		qat_hal_put_wakeup_event(handle, ae, (~ctx_mask) &
7638c2ecf20Sopenharmony_ci				 ICP_QAT_UCLO_AE_ALL_CTX, 0x10000);
7648c2ecf20Sopenharmony_ci		qat_hal_enable_ctx(handle, ae, ctx_mask);
7658c2ecf20Sopenharmony_ci	}
7668c2ecf20Sopenharmony_ci}
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_civoid qat_hal_stop(struct icp_qat_fw_loader_handle *handle, unsigned char ae,
7698c2ecf20Sopenharmony_ci		  unsigned int ctx_mask)
7708c2ecf20Sopenharmony_ci{
7718c2ecf20Sopenharmony_ci	if (!handle->fw_auth)
7728c2ecf20Sopenharmony_ci		qat_hal_disable_ctx(handle, ae, ctx_mask);
7738c2ecf20Sopenharmony_ci}
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_civoid qat_hal_set_pc(struct icp_qat_fw_loader_handle *handle,
7768c2ecf20Sopenharmony_ci		    unsigned char ae, unsigned int ctx_mask, unsigned int upc)
7778c2ecf20Sopenharmony_ci{
7788c2ecf20Sopenharmony_ci	qat_hal_wr_indr_csr(handle, ae, ctx_mask, CTX_STS_INDIRECT,
7798c2ecf20Sopenharmony_ci			    handle->hal_handle->upc_mask & upc);
7808c2ecf20Sopenharmony_ci}
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_cistatic void qat_hal_get_uwords(struct icp_qat_fw_loader_handle *handle,
7838c2ecf20Sopenharmony_ci			       unsigned char ae, unsigned int uaddr,
7848c2ecf20Sopenharmony_ci			       unsigned int words_num, u64 *uword)
7858c2ecf20Sopenharmony_ci{
7868c2ecf20Sopenharmony_ci	unsigned int i, uwrd_lo, uwrd_hi;
7878c2ecf20Sopenharmony_ci	unsigned int ustore_addr, misc_control;
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	misc_control = qat_hal_rd_ae_csr(handle, ae, AE_MISC_CONTROL);
7908c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, AE_MISC_CONTROL,
7918c2ecf20Sopenharmony_ci			  misc_control & 0xfffffffb);
7928c2ecf20Sopenharmony_ci	ustore_addr = qat_hal_rd_ae_csr(handle, ae, USTORE_ADDRESS);
7938c2ecf20Sopenharmony_ci	uaddr |= UA_ECS;
7948c2ecf20Sopenharmony_ci	for (i = 0; i < words_num; i++) {
7958c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, uaddr);
7968c2ecf20Sopenharmony_ci		uaddr++;
7978c2ecf20Sopenharmony_ci		uwrd_lo = qat_hal_rd_ae_csr(handle, ae, USTORE_DATA_LOWER);
7988c2ecf20Sopenharmony_ci		uwrd_hi = qat_hal_rd_ae_csr(handle, ae, USTORE_DATA_UPPER);
7998c2ecf20Sopenharmony_ci		uword[i] = uwrd_hi;
8008c2ecf20Sopenharmony_ci		uword[i] = (uword[i] << 0x20) | uwrd_lo;
8018c2ecf20Sopenharmony_ci	}
8028c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, AE_MISC_CONTROL, misc_control);
8038c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, ustore_addr);
8048c2ecf20Sopenharmony_ci}
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_civoid qat_hal_wr_umem(struct icp_qat_fw_loader_handle *handle,
8078c2ecf20Sopenharmony_ci		     unsigned char ae, unsigned int uaddr,
8088c2ecf20Sopenharmony_ci		     unsigned int words_num, unsigned int *data)
8098c2ecf20Sopenharmony_ci{
8108c2ecf20Sopenharmony_ci	unsigned int i, ustore_addr;
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	ustore_addr = qat_hal_rd_ae_csr(handle, ae, USTORE_ADDRESS);
8138c2ecf20Sopenharmony_ci	uaddr |= UA_ECS;
8148c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, uaddr);
8158c2ecf20Sopenharmony_ci	for (i = 0; i < words_num; i++) {
8168c2ecf20Sopenharmony_ci		unsigned int uwrd_lo, uwrd_hi, tmp;
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci		uwrd_lo = ((data[i] & 0xfff0000) << 4) | (0x3 << 18) |
8198c2ecf20Sopenharmony_ci			  ((data[i] & 0xff00) << 2) |
8208c2ecf20Sopenharmony_ci			  (0x3 << 8) | (data[i] & 0xff);
8218c2ecf20Sopenharmony_ci		uwrd_hi = (0xf << 4) | ((data[i] & 0xf0000000) >> 28);
8228c2ecf20Sopenharmony_ci		uwrd_hi |= (hweight32(data[i] & 0xffff) & 0x1) << 8;
8238c2ecf20Sopenharmony_ci		tmp = ((data[i] >> 0x10) & 0xffff);
8248c2ecf20Sopenharmony_ci		uwrd_hi |= (hweight32(tmp) & 0x1) << 9;
8258c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, USTORE_DATA_LOWER, uwrd_lo);
8268c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, USTORE_DATA_UPPER, uwrd_hi);
8278c2ecf20Sopenharmony_ci	}
8288c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, ustore_addr);
8298c2ecf20Sopenharmony_ci}
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci#define MAX_EXEC_INST 100
8328c2ecf20Sopenharmony_cistatic int qat_hal_exec_micro_inst(struct icp_qat_fw_loader_handle *handle,
8338c2ecf20Sopenharmony_ci				   unsigned char ae, unsigned char ctx,
8348c2ecf20Sopenharmony_ci				   u64 *micro_inst, unsigned int inst_num,
8358c2ecf20Sopenharmony_ci				   int code_off, unsigned int max_cycle,
8368c2ecf20Sopenharmony_ci				   unsigned int *endpc)
8378c2ecf20Sopenharmony_ci{
8388c2ecf20Sopenharmony_ci	u64 savuwords[MAX_EXEC_INST];
8398c2ecf20Sopenharmony_ci	unsigned int ind_lm_addr0, ind_lm_addr1;
8408c2ecf20Sopenharmony_ci	unsigned int ind_lm_addr_byte0, ind_lm_addr_byte1;
8418c2ecf20Sopenharmony_ci	unsigned int ind_cnt_sig;
8428c2ecf20Sopenharmony_ci	unsigned int ind_sig, act_sig;
8438c2ecf20Sopenharmony_ci	unsigned int csr_val = 0, newcsr_val;
8448c2ecf20Sopenharmony_ci	unsigned int savctx;
8458c2ecf20Sopenharmony_ci	unsigned int savcc, wakeup_events, savpc;
8468c2ecf20Sopenharmony_ci	unsigned int ctxarb_ctl, ctx_enables;
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	if ((inst_num > handle->hal_handle->max_ustore) || !micro_inst) {
8498c2ecf20Sopenharmony_ci		pr_err("QAT: invalid instruction num %d\n", inst_num);
8508c2ecf20Sopenharmony_ci		return -EINVAL;
8518c2ecf20Sopenharmony_ci	}
8528c2ecf20Sopenharmony_ci	/* save current context */
8538c2ecf20Sopenharmony_ci	ind_lm_addr0 = qat_hal_rd_indr_csr(handle, ae, ctx, LM_ADDR_0_INDIRECT);
8548c2ecf20Sopenharmony_ci	ind_lm_addr1 = qat_hal_rd_indr_csr(handle, ae, ctx, LM_ADDR_1_INDIRECT);
8558c2ecf20Sopenharmony_ci	ind_lm_addr_byte0 = qat_hal_rd_indr_csr(handle, ae, ctx,
8568c2ecf20Sopenharmony_ci						INDIRECT_LM_ADDR_0_BYTE_INDEX);
8578c2ecf20Sopenharmony_ci	ind_lm_addr_byte1 = qat_hal_rd_indr_csr(handle, ae, ctx,
8588c2ecf20Sopenharmony_ci						INDIRECT_LM_ADDR_1_BYTE_INDEX);
8598c2ecf20Sopenharmony_ci	if (inst_num <= MAX_EXEC_INST)
8608c2ecf20Sopenharmony_ci		qat_hal_get_uwords(handle, ae, 0, inst_num, savuwords);
8618c2ecf20Sopenharmony_ci	qat_hal_get_wakeup_event(handle, ae, ctx, &wakeup_events);
8628c2ecf20Sopenharmony_ci	savpc = qat_hal_rd_indr_csr(handle, ae, ctx, CTX_STS_INDIRECT);
8638c2ecf20Sopenharmony_ci	savpc = (savpc & handle->hal_handle->upc_mask) >> 0;
8648c2ecf20Sopenharmony_ci	ctx_enables = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
8658c2ecf20Sopenharmony_ci	ctx_enables &= IGNORE_W1C_MASK;
8668c2ecf20Sopenharmony_ci	savcc = qat_hal_rd_ae_csr(handle, ae, CC_ENABLE);
8678c2ecf20Sopenharmony_ci	savctx = qat_hal_rd_ae_csr(handle, ae, ACTIVE_CTX_STATUS);
8688c2ecf20Sopenharmony_ci	ctxarb_ctl = qat_hal_rd_ae_csr(handle, ae, CTX_ARB_CNTL);
8698c2ecf20Sopenharmony_ci	ind_cnt_sig = qat_hal_rd_indr_csr(handle, ae, ctx,
8708c2ecf20Sopenharmony_ci					  FUTURE_COUNT_SIGNAL_INDIRECT);
8718c2ecf20Sopenharmony_ci	ind_sig = qat_hal_rd_indr_csr(handle, ae, ctx,
8728c2ecf20Sopenharmony_ci				      CTX_SIG_EVENTS_INDIRECT);
8738c2ecf20Sopenharmony_ci	act_sig = qat_hal_rd_ae_csr(handle, ae, CTX_SIG_EVENTS_ACTIVE);
8748c2ecf20Sopenharmony_ci	/* execute micro codes */
8758c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx_enables);
8768c2ecf20Sopenharmony_ci	qat_hal_wr_uwords(handle, ae, 0, inst_num, micro_inst);
8778c2ecf20Sopenharmony_ci	qat_hal_wr_indr_csr(handle, ae, (1 << ctx), CTX_STS_INDIRECT, 0);
8788c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, ACTIVE_CTX_STATUS, ctx & ACS_ACNO);
8798c2ecf20Sopenharmony_ci	if (code_off)
8808c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, CC_ENABLE, savcc & 0xffffdfff);
8818c2ecf20Sopenharmony_ci	qat_hal_put_wakeup_event(handle, ae, (1 << ctx), XCWE_VOLUNTARY);
8828c2ecf20Sopenharmony_ci	qat_hal_wr_indr_csr(handle, ae, (1 << ctx), CTX_SIG_EVENTS_INDIRECT, 0);
8838c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CTX_SIG_EVENTS_ACTIVE, 0);
8848c2ecf20Sopenharmony_ci	qat_hal_enable_ctx(handle, ae, (1 << ctx));
8858c2ecf20Sopenharmony_ci	/* wait for micro codes to finish */
8868c2ecf20Sopenharmony_ci	if (qat_hal_wait_cycles(handle, ae, max_cycle, 1) != 0)
8878c2ecf20Sopenharmony_ci		return -EFAULT;
8888c2ecf20Sopenharmony_ci	if (endpc) {
8898c2ecf20Sopenharmony_ci		unsigned int ctx_status;
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci		ctx_status = qat_hal_rd_indr_csr(handle, ae, ctx,
8928c2ecf20Sopenharmony_ci						 CTX_STS_INDIRECT);
8938c2ecf20Sopenharmony_ci		*endpc = ctx_status & handle->hal_handle->upc_mask;
8948c2ecf20Sopenharmony_ci	}
8958c2ecf20Sopenharmony_ci	/* retore to saved context */
8968c2ecf20Sopenharmony_ci	qat_hal_disable_ctx(handle, ae, (1 << ctx));
8978c2ecf20Sopenharmony_ci	if (inst_num <= MAX_EXEC_INST)
8988c2ecf20Sopenharmony_ci		qat_hal_wr_uwords(handle, ae, 0, inst_num, savuwords);
8998c2ecf20Sopenharmony_ci	qat_hal_put_wakeup_event(handle, ae, (1 << ctx), wakeup_events);
9008c2ecf20Sopenharmony_ci	qat_hal_wr_indr_csr(handle, ae, (1 << ctx), CTX_STS_INDIRECT,
9018c2ecf20Sopenharmony_ci			    handle->hal_handle->upc_mask & savpc);
9028c2ecf20Sopenharmony_ci	csr_val = qat_hal_rd_ae_csr(handle, ae, AE_MISC_CONTROL);
9038c2ecf20Sopenharmony_ci	newcsr_val = CLR_BIT(csr_val, MMC_SHARE_CS_BITPOS);
9048c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, AE_MISC_CONTROL, newcsr_val);
9058c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CC_ENABLE, savcc);
9068c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, ACTIVE_CTX_STATUS, savctx & ACS_ACNO);
9078c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CTX_ARB_CNTL, ctxarb_ctl);
9088c2ecf20Sopenharmony_ci	qat_hal_wr_indr_csr(handle, ae, (1 << ctx),
9098c2ecf20Sopenharmony_ci			    LM_ADDR_0_INDIRECT, ind_lm_addr0);
9108c2ecf20Sopenharmony_ci	qat_hal_wr_indr_csr(handle, ae, (1 << ctx),
9118c2ecf20Sopenharmony_ci			    LM_ADDR_1_INDIRECT, ind_lm_addr1);
9128c2ecf20Sopenharmony_ci	qat_hal_wr_indr_csr(handle, ae, (1 << ctx),
9138c2ecf20Sopenharmony_ci			    INDIRECT_LM_ADDR_0_BYTE_INDEX, ind_lm_addr_byte0);
9148c2ecf20Sopenharmony_ci	qat_hal_wr_indr_csr(handle, ae, (1 << ctx),
9158c2ecf20Sopenharmony_ci			    INDIRECT_LM_ADDR_1_BYTE_INDEX, ind_lm_addr_byte1);
9168c2ecf20Sopenharmony_ci	qat_hal_wr_indr_csr(handle, ae, (1 << ctx),
9178c2ecf20Sopenharmony_ci			    FUTURE_COUNT_SIGNAL_INDIRECT, ind_cnt_sig);
9188c2ecf20Sopenharmony_ci	qat_hal_wr_indr_csr(handle, ae, (1 << ctx),
9198c2ecf20Sopenharmony_ci			    CTX_SIG_EVENTS_INDIRECT, ind_sig);
9208c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CTX_SIG_EVENTS_ACTIVE, act_sig);
9218c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx_enables);
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	return 0;
9248c2ecf20Sopenharmony_ci}
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_cistatic int qat_hal_rd_rel_reg(struct icp_qat_fw_loader_handle *handle,
9278c2ecf20Sopenharmony_ci			      unsigned char ae, unsigned char ctx,
9288c2ecf20Sopenharmony_ci			      enum icp_qat_uof_regtype reg_type,
9298c2ecf20Sopenharmony_ci			      unsigned short reg_num, unsigned int *data)
9308c2ecf20Sopenharmony_ci{
9318c2ecf20Sopenharmony_ci	unsigned int savctx, uaddr, uwrd_lo, uwrd_hi;
9328c2ecf20Sopenharmony_ci	unsigned int ctxarb_cntl, ustore_addr, ctx_enables;
9338c2ecf20Sopenharmony_ci	unsigned short reg_addr;
9348c2ecf20Sopenharmony_ci	int status = 0;
9358c2ecf20Sopenharmony_ci	u64 insts, savuword;
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	reg_addr = qat_hal_get_reg_addr(reg_type, reg_num);
9388c2ecf20Sopenharmony_ci	if (reg_addr == BAD_REGADDR) {
9398c2ecf20Sopenharmony_ci		pr_err("QAT: bad regaddr=0x%x\n", reg_addr);
9408c2ecf20Sopenharmony_ci		return -EINVAL;
9418c2ecf20Sopenharmony_ci	}
9428c2ecf20Sopenharmony_ci	switch (reg_type) {
9438c2ecf20Sopenharmony_ci	case ICP_GPA_REL:
9448c2ecf20Sopenharmony_ci		insts = 0xA070000000ull | (reg_addr & 0x3ff);
9458c2ecf20Sopenharmony_ci		break;
9468c2ecf20Sopenharmony_ci	default:
9478c2ecf20Sopenharmony_ci		insts = (u64)0xA030000000ull | ((reg_addr & 0x3ff) << 10);
9488c2ecf20Sopenharmony_ci		break;
9498c2ecf20Sopenharmony_ci	}
9508c2ecf20Sopenharmony_ci	savctx = qat_hal_rd_ae_csr(handle, ae, ACTIVE_CTX_STATUS);
9518c2ecf20Sopenharmony_ci	ctxarb_cntl = qat_hal_rd_ae_csr(handle, ae, CTX_ARB_CNTL);
9528c2ecf20Sopenharmony_ci	ctx_enables = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
9538c2ecf20Sopenharmony_ci	ctx_enables &= IGNORE_W1C_MASK;
9548c2ecf20Sopenharmony_ci	if (ctx != (savctx & ACS_ACNO))
9558c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, ACTIVE_CTX_STATUS,
9568c2ecf20Sopenharmony_ci				  ctx & ACS_ACNO);
9578c2ecf20Sopenharmony_ci	qat_hal_get_uwords(handle, ae, 0, 1, &savuword);
9588c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx_enables);
9598c2ecf20Sopenharmony_ci	ustore_addr = qat_hal_rd_ae_csr(handle, ae, USTORE_ADDRESS);
9608c2ecf20Sopenharmony_ci	uaddr = UA_ECS;
9618c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, uaddr);
9628c2ecf20Sopenharmony_ci	insts = qat_hal_set_uword_ecc(insts);
9638c2ecf20Sopenharmony_ci	uwrd_lo = (unsigned int)(insts & 0xffffffff);
9648c2ecf20Sopenharmony_ci	uwrd_hi = (unsigned int)(insts >> 0x20);
9658c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, USTORE_DATA_LOWER, uwrd_lo);
9668c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, USTORE_DATA_UPPER, uwrd_hi);
9678c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, uaddr);
9688c2ecf20Sopenharmony_ci	/* delay for at least 8 cycles */
9698c2ecf20Sopenharmony_ci	qat_hal_wait_cycles(handle, ae, 0x8, 0);
9708c2ecf20Sopenharmony_ci	/*
9718c2ecf20Sopenharmony_ci	 * read ALU output
9728c2ecf20Sopenharmony_ci	 * the instruction should have been executed
9738c2ecf20Sopenharmony_ci	 * prior to clearing the ECS in putUwords
9748c2ecf20Sopenharmony_ci	 */
9758c2ecf20Sopenharmony_ci	*data = qat_hal_rd_ae_csr(handle, ae, ALU_OUT);
9768c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, ustore_addr);
9778c2ecf20Sopenharmony_ci	qat_hal_wr_uwords(handle, ae, 0, 1, &savuword);
9788c2ecf20Sopenharmony_ci	if (ctx != (savctx & ACS_ACNO))
9798c2ecf20Sopenharmony_ci		qat_hal_wr_ae_csr(handle, ae, ACTIVE_CTX_STATUS,
9808c2ecf20Sopenharmony_ci				  savctx & ACS_ACNO);
9818c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CTX_ARB_CNTL, ctxarb_cntl);
9828c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx_enables);
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_ci	return status;
9858c2ecf20Sopenharmony_ci}
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_cistatic int qat_hal_wr_rel_reg(struct icp_qat_fw_loader_handle *handle,
9888c2ecf20Sopenharmony_ci			      unsigned char ae, unsigned char ctx,
9898c2ecf20Sopenharmony_ci			      enum icp_qat_uof_regtype reg_type,
9908c2ecf20Sopenharmony_ci			      unsigned short reg_num, unsigned int data)
9918c2ecf20Sopenharmony_ci{
9928c2ecf20Sopenharmony_ci	unsigned short src_hiaddr, src_lowaddr, dest_addr, data16hi, data16lo;
9938c2ecf20Sopenharmony_ci	u64 insts[] = {
9948c2ecf20Sopenharmony_ci		0x0F440000000ull,
9958c2ecf20Sopenharmony_ci		0x0F040000000ull,
9968c2ecf20Sopenharmony_ci		0x0F0000C0300ull,
9978c2ecf20Sopenharmony_ci		0x0E000010000ull
9988c2ecf20Sopenharmony_ci	};
9998c2ecf20Sopenharmony_ci	const int num_inst = ARRAY_SIZE(insts), code_off = 1;
10008c2ecf20Sopenharmony_ci	const int imm_w1 = 0, imm_w0 = 1;
10018c2ecf20Sopenharmony_ci
10028c2ecf20Sopenharmony_ci	dest_addr = qat_hal_get_reg_addr(reg_type, reg_num);
10038c2ecf20Sopenharmony_ci	if (dest_addr == BAD_REGADDR) {
10048c2ecf20Sopenharmony_ci		pr_err("QAT: bad destAddr=0x%x\n", dest_addr);
10058c2ecf20Sopenharmony_ci		return -EINVAL;
10068c2ecf20Sopenharmony_ci	}
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	data16lo = 0xffff & data;
10098c2ecf20Sopenharmony_ci	data16hi = 0xffff & (data >> 0x10);
10108c2ecf20Sopenharmony_ci	src_hiaddr = qat_hal_get_reg_addr(ICP_NO_DEST, (unsigned short)
10118c2ecf20Sopenharmony_ci					  (0xff & data16hi));
10128c2ecf20Sopenharmony_ci	src_lowaddr = qat_hal_get_reg_addr(ICP_NO_DEST, (unsigned short)
10138c2ecf20Sopenharmony_ci					   (0xff & data16lo));
10148c2ecf20Sopenharmony_ci	switch (reg_type) {
10158c2ecf20Sopenharmony_ci	case ICP_GPA_REL:
10168c2ecf20Sopenharmony_ci		insts[imm_w1] = insts[imm_w1] | ((data16hi >> 8) << 20) |
10178c2ecf20Sopenharmony_ci		    ((src_hiaddr & 0x3ff) << 10) | (dest_addr & 0x3ff);
10188c2ecf20Sopenharmony_ci		insts[imm_w0] = insts[imm_w0] | ((data16lo >> 8) << 20) |
10198c2ecf20Sopenharmony_ci		    ((src_lowaddr & 0x3ff) << 10) | (dest_addr & 0x3ff);
10208c2ecf20Sopenharmony_ci		break;
10218c2ecf20Sopenharmony_ci	default:
10228c2ecf20Sopenharmony_ci		insts[imm_w1] = insts[imm_w1] | ((data16hi >> 8) << 20) |
10238c2ecf20Sopenharmony_ci		    ((dest_addr & 0x3ff) << 10) | (src_hiaddr & 0x3ff);
10248c2ecf20Sopenharmony_ci
10258c2ecf20Sopenharmony_ci		insts[imm_w0] = insts[imm_w0] | ((data16lo >> 8) << 20) |
10268c2ecf20Sopenharmony_ci		    ((dest_addr & 0x3ff) << 10) | (src_lowaddr & 0x3ff);
10278c2ecf20Sopenharmony_ci		break;
10288c2ecf20Sopenharmony_ci	}
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_ci	return qat_hal_exec_micro_inst(handle, ae, ctx, insts, num_inst,
10318c2ecf20Sopenharmony_ci				       code_off, num_inst * 0x5, NULL);
10328c2ecf20Sopenharmony_ci}
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_ciint qat_hal_get_ins_num(void)
10358c2ecf20Sopenharmony_ci{
10368c2ecf20Sopenharmony_ci	return ARRAY_SIZE(inst_4b);
10378c2ecf20Sopenharmony_ci}
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_cistatic int qat_hal_concat_micro_code(u64 *micro_inst,
10408c2ecf20Sopenharmony_ci				     unsigned int inst_num, unsigned int size,
10418c2ecf20Sopenharmony_ci				     unsigned int addr, unsigned int *value)
10428c2ecf20Sopenharmony_ci{
10438c2ecf20Sopenharmony_ci	int i;
10448c2ecf20Sopenharmony_ci	unsigned int cur_value;
10458c2ecf20Sopenharmony_ci	const u64 *inst_arr;
10468c2ecf20Sopenharmony_ci	int fixup_offset;
10478c2ecf20Sopenharmony_ci	int usize = 0;
10488c2ecf20Sopenharmony_ci	int orig_num;
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci	orig_num = inst_num;
10518c2ecf20Sopenharmony_ci	cur_value = value[0];
10528c2ecf20Sopenharmony_ci	inst_arr = inst_4b;
10538c2ecf20Sopenharmony_ci	usize = ARRAY_SIZE(inst_4b);
10548c2ecf20Sopenharmony_ci	fixup_offset = inst_num;
10558c2ecf20Sopenharmony_ci	for (i = 0; i < usize; i++)
10568c2ecf20Sopenharmony_ci		micro_inst[inst_num++] = inst_arr[i];
10578c2ecf20Sopenharmony_ci	INSERT_IMMED_GPRA_CONST(micro_inst[fixup_offset], (addr));
10588c2ecf20Sopenharmony_ci	fixup_offset++;
10598c2ecf20Sopenharmony_ci	INSERT_IMMED_GPRA_CONST(micro_inst[fixup_offset], 0);
10608c2ecf20Sopenharmony_ci	fixup_offset++;
10618c2ecf20Sopenharmony_ci	INSERT_IMMED_GPRB_CONST(micro_inst[fixup_offset], (cur_value >> 0));
10628c2ecf20Sopenharmony_ci	fixup_offset++;
10638c2ecf20Sopenharmony_ci	INSERT_IMMED_GPRB_CONST(micro_inst[fixup_offset], (cur_value >> 0x10));
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_ci	return inst_num - orig_num;
10668c2ecf20Sopenharmony_ci}
10678c2ecf20Sopenharmony_ci
10688c2ecf20Sopenharmony_cistatic int qat_hal_exec_micro_init_lm(struct icp_qat_fw_loader_handle *handle,
10698c2ecf20Sopenharmony_ci				      unsigned char ae, unsigned char ctx,
10708c2ecf20Sopenharmony_ci				      int *pfirst_exec, u64 *micro_inst,
10718c2ecf20Sopenharmony_ci				      unsigned int inst_num)
10728c2ecf20Sopenharmony_ci{
10738c2ecf20Sopenharmony_ci	int stat = 0;
10748c2ecf20Sopenharmony_ci	unsigned int gpra0 = 0, gpra1 = 0, gpra2 = 0;
10758c2ecf20Sopenharmony_ci	unsigned int gprb0 = 0, gprb1 = 0;
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_ci	if (*pfirst_exec) {
10788c2ecf20Sopenharmony_ci		qat_hal_rd_rel_reg(handle, ae, ctx, ICP_GPA_REL, 0, &gpra0);
10798c2ecf20Sopenharmony_ci		qat_hal_rd_rel_reg(handle, ae, ctx, ICP_GPA_REL, 0x1, &gpra1);
10808c2ecf20Sopenharmony_ci		qat_hal_rd_rel_reg(handle, ae, ctx, ICP_GPA_REL, 0x2, &gpra2);
10818c2ecf20Sopenharmony_ci		qat_hal_rd_rel_reg(handle, ae, ctx, ICP_GPB_REL, 0, &gprb0);
10828c2ecf20Sopenharmony_ci		qat_hal_rd_rel_reg(handle, ae, ctx, ICP_GPB_REL, 0x1, &gprb1);
10838c2ecf20Sopenharmony_ci		*pfirst_exec = 0;
10848c2ecf20Sopenharmony_ci	}
10858c2ecf20Sopenharmony_ci	stat = qat_hal_exec_micro_inst(handle, ae, ctx, micro_inst, inst_num, 1,
10868c2ecf20Sopenharmony_ci				       inst_num * 0x5, NULL);
10878c2ecf20Sopenharmony_ci	if (stat != 0)
10888c2ecf20Sopenharmony_ci		return -EFAULT;
10898c2ecf20Sopenharmony_ci	qat_hal_wr_rel_reg(handle, ae, ctx, ICP_GPA_REL, 0, gpra0);
10908c2ecf20Sopenharmony_ci	qat_hal_wr_rel_reg(handle, ae, ctx, ICP_GPA_REL, 0x1, gpra1);
10918c2ecf20Sopenharmony_ci	qat_hal_wr_rel_reg(handle, ae, ctx, ICP_GPA_REL, 0x2, gpra2);
10928c2ecf20Sopenharmony_ci	qat_hal_wr_rel_reg(handle, ae, ctx, ICP_GPB_REL, 0, gprb0);
10938c2ecf20Sopenharmony_ci	qat_hal_wr_rel_reg(handle, ae, ctx, ICP_GPB_REL, 0x1, gprb1);
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_ci	return 0;
10968c2ecf20Sopenharmony_ci}
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_ciint qat_hal_batch_wr_lm(struct icp_qat_fw_loader_handle *handle,
10998c2ecf20Sopenharmony_ci			unsigned char ae,
11008c2ecf20Sopenharmony_ci			struct icp_qat_uof_batch_init *lm_init_header)
11018c2ecf20Sopenharmony_ci{
11028c2ecf20Sopenharmony_ci	struct icp_qat_uof_batch_init *plm_init;
11038c2ecf20Sopenharmony_ci	u64 *micro_inst_arry;
11048c2ecf20Sopenharmony_ci	int micro_inst_num;
11058c2ecf20Sopenharmony_ci	int alloc_inst_size;
11068c2ecf20Sopenharmony_ci	int first_exec = 1;
11078c2ecf20Sopenharmony_ci	int stat = 0;
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci	plm_init = lm_init_header->next;
11108c2ecf20Sopenharmony_ci	alloc_inst_size = lm_init_header->size;
11118c2ecf20Sopenharmony_ci	if ((unsigned int)alloc_inst_size > handle->hal_handle->max_ustore)
11128c2ecf20Sopenharmony_ci		alloc_inst_size = handle->hal_handle->max_ustore;
11138c2ecf20Sopenharmony_ci	micro_inst_arry = kmalloc_array(alloc_inst_size, sizeof(u64),
11148c2ecf20Sopenharmony_ci					GFP_KERNEL);
11158c2ecf20Sopenharmony_ci	if (!micro_inst_arry)
11168c2ecf20Sopenharmony_ci		return -ENOMEM;
11178c2ecf20Sopenharmony_ci	micro_inst_num = 0;
11188c2ecf20Sopenharmony_ci	while (plm_init) {
11198c2ecf20Sopenharmony_ci		unsigned int addr, *value, size;
11208c2ecf20Sopenharmony_ci
11218c2ecf20Sopenharmony_ci		ae = plm_init->ae;
11228c2ecf20Sopenharmony_ci		addr = plm_init->addr;
11238c2ecf20Sopenharmony_ci		value = plm_init->value;
11248c2ecf20Sopenharmony_ci		size = plm_init->size;
11258c2ecf20Sopenharmony_ci		micro_inst_num += qat_hal_concat_micro_code(micro_inst_arry,
11268c2ecf20Sopenharmony_ci							    micro_inst_num,
11278c2ecf20Sopenharmony_ci							    size, addr, value);
11288c2ecf20Sopenharmony_ci		plm_init = plm_init->next;
11298c2ecf20Sopenharmony_ci	}
11308c2ecf20Sopenharmony_ci	/* exec micro codes */
11318c2ecf20Sopenharmony_ci	if (micro_inst_arry && (micro_inst_num > 0)) {
11328c2ecf20Sopenharmony_ci		micro_inst_arry[micro_inst_num++] = 0x0E000010000ull;
11338c2ecf20Sopenharmony_ci		stat = qat_hal_exec_micro_init_lm(handle, ae, 0, &first_exec,
11348c2ecf20Sopenharmony_ci						  micro_inst_arry,
11358c2ecf20Sopenharmony_ci						  micro_inst_num);
11368c2ecf20Sopenharmony_ci	}
11378c2ecf20Sopenharmony_ci	kfree(micro_inst_arry);
11388c2ecf20Sopenharmony_ci	return stat;
11398c2ecf20Sopenharmony_ci}
11408c2ecf20Sopenharmony_ci
11418c2ecf20Sopenharmony_cistatic int qat_hal_put_rel_rd_xfer(struct icp_qat_fw_loader_handle *handle,
11428c2ecf20Sopenharmony_ci				   unsigned char ae, unsigned char ctx,
11438c2ecf20Sopenharmony_ci				   enum icp_qat_uof_regtype reg_type,
11448c2ecf20Sopenharmony_ci				   unsigned short reg_num, unsigned int val)
11458c2ecf20Sopenharmony_ci{
11468c2ecf20Sopenharmony_ci	int status = 0;
11478c2ecf20Sopenharmony_ci	unsigned int reg_addr;
11488c2ecf20Sopenharmony_ci	unsigned int ctx_enables;
11498c2ecf20Sopenharmony_ci	unsigned short mask;
11508c2ecf20Sopenharmony_ci	unsigned short dr_offset = 0x10;
11518c2ecf20Sopenharmony_ci
11528c2ecf20Sopenharmony_ci	ctx_enables = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
11538c2ecf20Sopenharmony_ci	if (CE_INUSE_CONTEXTS & ctx_enables) {
11548c2ecf20Sopenharmony_ci		if (ctx & 0x1) {
11558c2ecf20Sopenharmony_ci			pr_err("QAT: bad 4-ctx mode,ctx=0x%x\n", ctx);
11568c2ecf20Sopenharmony_ci			return -EINVAL;
11578c2ecf20Sopenharmony_ci		}
11588c2ecf20Sopenharmony_ci		mask = 0x1f;
11598c2ecf20Sopenharmony_ci		dr_offset = 0x20;
11608c2ecf20Sopenharmony_ci	} else {
11618c2ecf20Sopenharmony_ci		mask = 0x0f;
11628c2ecf20Sopenharmony_ci	}
11638c2ecf20Sopenharmony_ci	if (reg_num & ~mask)
11648c2ecf20Sopenharmony_ci		return -EINVAL;
11658c2ecf20Sopenharmony_ci	reg_addr = reg_num + (ctx << 0x5);
11668c2ecf20Sopenharmony_ci	switch (reg_type) {
11678c2ecf20Sopenharmony_ci	case ICP_SR_RD_REL:
11688c2ecf20Sopenharmony_ci	case ICP_SR_REL:
11698c2ecf20Sopenharmony_ci		SET_AE_XFER(handle, ae, reg_addr, val);
11708c2ecf20Sopenharmony_ci		break;
11718c2ecf20Sopenharmony_ci	case ICP_DR_RD_REL:
11728c2ecf20Sopenharmony_ci	case ICP_DR_REL:
11738c2ecf20Sopenharmony_ci		SET_AE_XFER(handle, ae, (reg_addr + dr_offset), val);
11748c2ecf20Sopenharmony_ci		break;
11758c2ecf20Sopenharmony_ci	default:
11768c2ecf20Sopenharmony_ci		status = -EINVAL;
11778c2ecf20Sopenharmony_ci		break;
11788c2ecf20Sopenharmony_ci	}
11798c2ecf20Sopenharmony_ci	return status;
11808c2ecf20Sopenharmony_ci}
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_cistatic int qat_hal_put_rel_wr_xfer(struct icp_qat_fw_loader_handle *handle,
11838c2ecf20Sopenharmony_ci				   unsigned char ae, unsigned char ctx,
11848c2ecf20Sopenharmony_ci				   enum icp_qat_uof_regtype reg_type,
11858c2ecf20Sopenharmony_ci				   unsigned short reg_num, unsigned int data)
11868c2ecf20Sopenharmony_ci{
11878c2ecf20Sopenharmony_ci	unsigned int gprval, ctx_enables;
11888c2ecf20Sopenharmony_ci	unsigned short src_hiaddr, src_lowaddr, gpr_addr, xfr_addr, data16hi,
11898c2ecf20Sopenharmony_ci	    data16low;
11908c2ecf20Sopenharmony_ci	unsigned short reg_mask;
11918c2ecf20Sopenharmony_ci	int status = 0;
11928c2ecf20Sopenharmony_ci	u64 micro_inst[] = {
11938c2ecf20Sopenharmony_ci		0x0F440000000ull,
11948c2ecf20Sopenharmony_ci		0x0F040000000ull,
11958c2ecf20Sopenharmony_ci		0x0A000000000ull,
11968c2ecf20Sopenharmony_ci		0x0F0000C0300ull,
11978c2ecf20Sopenharmony_ci		0x0E000010000ull
11988c2ecf20Sopenharmony_ci	};
11998c2ecf20Sopenharmony_ci	const int num_inst = ARRAY_SIZE(micro_inst), code_off = 1;
12008c2ecf20Sopenharmony_ci	const unsigned short gprnum = 0, dly = num_inst * 0x5;
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci	ctx_enables = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
12038c2ecf20Sopenharmony_ci	if (CE_INUSE_CONTEXTS & ctx_enables) {
12048c2ecf20Sopenharmony_ci		if (ctx & 0x1) {
12058c2ecf20Sopenharmony_ci			pr_err("QAT: 4-ctx mode,ctx=0x%x\n", ctx);
12068c2ecf20Sopenharmony_ci			return -EINVAL;
12078c2ecf20Sopenharmony_ci		}
12088c2ecf20Sopenharmony_ci		reg_mask = (unsigned short)~0x1f;
12098c2ecf20Sopenharmony_ci	} else {
12108c2ecf20Sopenharmony_ci		reg_mask = (unsigned short)~0xf;
12118c2ecf20Sopenharmony_ci	}
12128c2ecf20Sopenharmony_ci	if (reg_num & reg_mask)
12138c2ecf20Sopenharmony_ci		return -EINVAL;
12148c2ecf20Sopenharmony_ci	xfr_addr = qat_hal_get_reg_addr(reg_type, reg_num);
12158c2ecf20Sopenharmony_ci	if (xfr_addr == BAD_REGADDR) {
12168c2ecf20Sopenharmony_ci		pr_err("QAT: bad xfrAddr=0x%x\n", xfr_addr);
12178c2ecf20Sopenharmony_ci		return -EINVAL;
12188c2ecf20Sopenharmony_ci	}
12198c2ecf20Sopenharmony_ci	status = qat_hal_rd_rel_reg(handle, ae, ctx, ICP_GPB_REL, gprnum, &gprval);
12208c2ecf20Sopenharmony_ci	if (status) {
12218c2ecf20Sopenharmony_ci		pr_err("QAT: failed to read register");
12228c2ecf20Sopenharmony_ci		return status;
12238c2ecf20Sopenharmony_ci	}
12248c2ecf20Sopenharmony_ci	gpr_addr = qat_hal_get_reg_addr(ICP_GPB_REL, gprnum);
12258c2ecf20Sopenharmony_ci	data16low = 0xffff & data;
12268c2ecf20Sopenharmony_ci	data16hi = 0xffff & (data >> 0x10);
12278c2ecf20Sopenharmony_ci	src_hiaddr = qat_hal_get_reg_addr(ICP_NO_DEST,
12288c2ecf20Sopenharmony_ci					  (unsigned short)(0xff & data16hi));
12298c2ecf20Sopenharmony_ci	src_lowaddr = qat_hal_get_reg_addr(ICP_NO_DEST,
12308c2ecf20Sopenharmony_ci					   (unsigned short)(0xff & data16low));
12318c2ecf20Sopenharmony_ci	micro_inst[0] = micro_inst[0x0] | ((data16hi >> 8) << 20) |
12328c2ecf20Sopenharmony_ci	    ((gpr_addr & 0x3ff) << 10) | (src_hiaddr & 0x3ff);
12338c2ecf20Sopenharmony_ci	micro_inst[1] = micro_inst[0x1] | ((data16low >> 8) << 20) |
12348c2ecf20Sopenharmony_ci	    ((gpr_addr & 0x3ff) << 10) | (src_lowaddr & 0x3ff);
12358c2ecf20Sopenharmony_ci	micro_inst[0x2] = micro_inst[0x2] |
12368c2ecf20Sopenharmony_ci	    ((xfr_addr & 0x3ff) << 20) | ((gpr_addr & 0x3ff) << 10);
12378c2ecf20Sopenharmony_ci	status = qat_hal_exec_micro_inst(handle, ae, ctx, micro_inst, num_inst,
12388c2ecf20Sopenharmony_ci					 code_off, dly, NULL);
12398c2ecf20Sopenharmony_ci	qat_hal_wr_rel_reg(handle, ae, ctx, ICP_GPB_REL, gprnum, gprval);
12408c2ecf20Sopenharmony_ci	return status;
12418c2ecf20Sopenharmony_ci}
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_cistatic int qat_hal_put_rel_nn(struct icp_qat_fw_loader_handle *handle,
12448c2ecf20Sopenharmony_ci			      unsigned char ae, unsigned char ctx,
12458c2ecf20Sopenharmony_ci			      unsigned short nn, unsigned int val)
12468c2ecf20Sopenharmony_ci{
12478c2ecf20Sopenharmony_ci	unsigned int ctx_enables;
12488c2ecf20Sopenharmony_ci	int stat = 0;
12498c2ecf20Sopenharmony_ci
12508c2ecf20Sopenharmony_ci	ctx_enables = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
12518c2ecf20Sopenharmony_ci	ctx_enables &= IGNORE_W1C_MASK;
12528c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx_enables | CE_NN_MODE);
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_ci	stat = qat_hal_put_rel_wr_xfer(handle, ae, ctx, ICP_NEIGH_REL, nn, val);
12558c2ecf20Sopenharmony_ci	qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx_enables);
12568c2ecf20Sopenharmony_ci	return stat;
12578c2ecf20Sopenharmony_ci}
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_cistatic int qat_hal_convert_abs_to_rel(struct icp_qat_fw_loader_handle
12608c2ecf20Sopenharmony_ci				      *handle, unsigned char ae,
12618c2ecf20Sopenharmony_ci				      unsigned short absreg_num,
12628c2ecf20Sopenharmony_ci				      unsigned short *relreg,
12638c2ecf20Sopenharmony_ci				      unsigned char *ctx)
12648c2ecf20Sopenharmony_ci{
12658c2ecf20Sopenharmony_ci	unsigned int ctx_enables;
12668c2ecf20Sopenharmony_ci
12678c2ecf20Sopenharmony_ci	ctx_enables = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
12688c2ecf20Sopenharmony_ci	if (ctx_enables & CE_INUSE_CONTEXTS) {
12698c2ecf20Sopenharmony_ci		/* 4-ctx mode */
12708c2ecf20Sopenharmony_ci		*relreg = absreg_num & 0x1F;
12718c2ecf20Sopenharmony_ci		*ctx = (absreg_num >> 0x4) & 0x6;
12728c2ecf20Sopenharmony_ci	} else {
12738c2ecf20Sopenharmony_ci		/* 8-ctx mode */
12748c2ecf20Sopenharmony_ci		*relreg = absreg_num & 0x0F;
12758c2ecf20Sopenharmony_ci		*ctx = (absreg_num >> 0x4) & 0x7;
12768c2ecf20Sopenharmony_ci	}
12778c2ecf20Sopenharmony_ci	return 0;
12788c2ecf20Sopenharmony_ci}
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ciint qat_hal_init_gpr(struct icp_qat_fw_loader_handle *handle,
12818c2ecf20Sopenharmony_ci		     unsigned char ae, unsigned char ctx_mask,
12828c2ecf20Sopenharmony_ci		     enum icp_qat_uof_regtype reg_type,
12838c2ecf20Sopenharmony_ci		     unsigned short reg_num, unsigned int regdata)
12848c2ecf20Sopenharmony_ci{
12858c2ecf20Sopenharmony_ci	int stat = 0;
12868c2ecf20Sopenharmony_ci	unsigned short reg;
12878c2ecf20Sopenharmony_ci	unsigned char ctx = 0;
12888c2ecf20Sopenharmony_ci	enum icp_qat_uof_regtype type;
12898c2ecf20Sopenharmony_ci
12908c2ecf20Sopenharmony_ci	if (reg_num >= ICP_QAT_UCLO_MAX_GPR_REG)
12918c2ecf20Sopenharmony_ci		return -EINVAL;
12928c2ecf20Sopenharmony_ci
12938c2ecf20Sopenharmony_ci	do {
12948c2ecf20Sopenharmony_ci		if (ctx_mask == 0) {
12958c2ecf20Sopenharmony_ci			qat_hal_convert_abs_to_rel(handle, ae, reg_num, &reg,
12968c2ecf20Sopenharmony_ci						   &ctx);
12978c2ecf20Sopenharmony_ci			type = reg_type - 1;
12988c2ecf20Sopenharmony_ci		} else {
12998c2ecf20Sopenharmony_ci			reg = reg_num;
13008c2ecf20Sopenharmony_ci			type = reg_type;
13018c2ecf20Sopenharmony_ci			if (!test_bit(ctx, (unsigned long *)&ctx_mask))
13028c2ecf20Sopenharmony_ci				continue;
13038c2ecf20Sopenharmony_ci		}
13048c2ecf20Sopenharmony_ci		stat = qat_hal_wr_rel_reg(handle, ae, ctx, type, reg, regdata);
13058c2ecf20Sopenharmony_ci		if (stat) {
13068c2ecf20Sopenharmony_ci			pr_err("QAT: write gpr fail\n");
13078c2ecf20Sopenharmony_ci			return -EINVAL;
13088c2ecf20Sopenharmony_ci		}
13098c2ecf20Sopenharmony_ci	} while (ctx_mask && (ctx++ < ICP_QAT_UCLO_MAX_CTX));
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_ci	return 0;
13128c2ecf20Sopenharmony_ci}
13138c2ecf20Sopenharmony_ci
13148c2ecf20Sopenharmony_ciint qat_hal_init_wr_xfer(struct icp_qat_fw_loader_handle *handle,
13158c2ecf20Sopenharmony_ci			 unsigned char ae, unsigned char ctx_mask,
13168c2ecf20Sopenharmony_ci			 enum icp_qat_uof_regtype reg_type,
13178c2ecf20Sopenharmony_ci			 unsigned short reg_num, unsigned int regdata)
13188c2ecf20Sopenharmony_ci{
13198c2ecf20Sopenharmony_ci	int stat = 0;
13208c2ecf20Sopenharmony_ci	unsigned short reg;
13218c2ecf20Sopenharmony_ci	unsigned char ctx = 0;
13228c2ecf20Sopenharmony_ci	enum icp_qat_uof_regtype type;
13238c2ecf20Sopenharmony_ci
13248c2ecf20Sopenharmony_ci	if (reg_num >= ICP_QAT_UCLO_MAX_XFER_REG)
13258c2ecf20Sopenharmony_ci		return -EINVAL;
13268c2ecf20Sopenharmony_ci
13278c2ecf20Sopenharmony_ci	do {
13288c2ecf20Sopenharmony_ci		if (ctx_mask == 0) {
13298c2ecf20Sopenharmony_ci			qat_hal_convert_abs_to_rel(handle, ae, reg_num, &reg,
13308c2ecf20Sopenharmony_ci						   &ctx);
13318c2ecf20Sopenharmony_ci			type = reg_type - 3;
13328c2ecf20Sopenharmony_ci		} else {
13338c2ecf20Sopenharmony_ci			reg = reg_num;
13348c2ecf20Sopenharmony_ci			type = reg_type;
13358c2ecf20Sopenharmony_ci			if (!test_bit(ctx, (unsigned long *)&ctx_mask))
13368c2ecf20Sopenharmony_ci				continue;
13378c2ecf20Sopenharmony_ci		}
13388c2ecf20Sopenharmony_ci		stat = qat_hal_put_rel_wr_xfer(handle, ae, ctx, type, reg,
13398c2ecf20Sopenharmony_ci					       regdata);
13408c2ecf20Sopenharmony_ci		if (stat) {
13418c2ecf20Sopenharmony_ci			pr_err("QAT: write wr xfer fail\n");
13428c2ecf20Sopenharmony_ci			return -EINVAL;
13438c2ecf20Sopenharmony_ci		}
13448c2ecf20Sopenharmony_ci	} while (ctx_mask && (ctx++ < ICP_QAT_UCLO_MAX_CTX));
13458c2ecf20Sopenharmony_ci
13468c2ecf20Sopenharmony_ci	return 0;
13478c2ecf20Sopenharmony_ci}
13488c2ecf20Sopenharmony_ci
13498c2ecf20Sopenharmony_ciint qat_hal_init_rd_xfer(struct icp_qat_fw_loader_handle *handle,
13508c2ecf20Sopenharmony_ci			 unsigned char ae, unsigned char ctx_mask,
13518c2ecf20Sopenharmony_ci			 enum icp_qat_uof_regtype reg_type,
13528c2ecf20Sopenharmony_ci			 unsigned short reg_num, unsigned int regdata)
13538c2ecf20Sopenharmony_ci{
13548c2ecf20Sopenharmony_ci	int stat = 0;
13558c2ecf20Sopenharmony_ci	unsigned short reg;
13568c2ecf20Sopenharmony_ci	unsigned char ctx = 0;
13578c2ecf20Sopenharmony_ci	enum icp_qat_uof_regtype type;
13588c2ecf20Sopenharmony_ci
13598c2ecf20Sopenharmony_ci	if (reg_num >= ICP_QAT_UCLO_MAX_XFER_REG)
13608c2ecf20Sopenharmony_ci		return -EINVAL;
13618c2ecf20Sopenharmony_ci
13628c2ecf20Sopenharmony_ci	do {
13638c2ecf20Sopenharmony_ci		if (ctx_mask == 0) {
13648c2ecf20Sopenharmony_ci			qat_hal_convert_abs_to_rel(handle, ae, reg_num, &reg,
13658c2ecf20Sopenharmony_ci						   &ctx);
13668c2ecf20Sopenharmony_ci			type = reg_type - 3;
13678c2ecf20Sopenharmony_ci		} else {
13688c2ecf20Sopenharmony_ci			reg = reg_num;
13698c2ecf20Sopenharmony_ci			type = reg_type;
13708c2ecf20Sopenharmony_ci			if (!test_bit(ctx, (unsigned long *)&ctx_mask))
13718c2ecf20Sopenharmony_ci				continue;
13728c2ecf20Sopenharmony_ci		}
13738c2ecf20Sopenharmony_ci		stat = qat_hal_put_rel_rd_xfer(handle, ae, ctx, type, reg,
13748c2ecf20Sopenharmony_ci					       regdata);
13758c2ecf20Sopenharmony_ci		if (stat) {
13768c2ecf20Sopenharmony_ci			pr_err("QAT: write rd xfer fail\n");
13778c2ecf20Sopenharmony_ci			return -EINVAL;
13788c2ecf20Sopenharmony_ci		}
13798c2ecf20Sopenharmony_ci	} while (ctx_mask && (ctx++ < ICP_QAT_UCLO_MAX_CTX));
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_ci	return 0;
13828c2ecf20Sopenharmony_ci}
13838c2ecf20Sopenharmony_ci
13848c2ecf20Sopenharmony_ciint qat_hal_init_nn(struct icp_qat_fw_loader_handle *handle,
13858c2ecf20Sopenharmony_ci		    unsigned char ae, unsigned char ctx_mask,
13868c2ecf20Sopenharmony_ci		    unsigned short reg_num, unsigned int regdata)
13878c2ecf20Sopenharmony_ci{
13888c2ecf20Sopenharmony_ci	int stat = 0;
13898c2ecf20Sopenharmony_ci	unsigned char ctx;
13908c2ecf20Sopenharmony_ci
13918c2ecf20Sopenharmony_ci	if (ctx_mask == 0)
13928c2ecf20Sopenharmony_ci		return -EINVAL;
13938c2ecf20Sopenharmony_ci
13948c2ecf20Sopenharmony_ci	for (ctx = 0; ctx < ICP_QAT_UCLO_MAX_CTX; ctx++) {
13958c2ecf20Sopenharmony_ci		if (!test_bit(ctx, (unsigned long *)&ctx_mask))
13968c2ecf20Sopenharmony_ci			continue;
13978c2ecf20Sopenharmony_ci		stat = qat_hal_put_rel_nn(handle, ae, ctx, reg_num, regdata);
13988c2ecf20Sopenharmony_ci		if (stat) {
13998c2ecf20Sopenharmony_ci			pr_err("QAT: write neigh error\n");
14008c2ecf20Sopenharmony_ci			return -EINVAL;
14018c2ecf20Sopenharmony_ci		}
14028c2ecf20Sopenharmony_ci	}
14038c2ecf20Sopenharmony_ci
14048c2ecf20Sopenharmony_ci	return 0;
14058c2ecf20Sopenharmony_ci}
1406