1 // SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)
2 /* Copyright(c) 2014 - 2020 Intel Corporation */
3 #include <linux/slab.h>
4 #include <linux/delay.h>
5 #include <linux/pci_ids.h>
7 #include "adf_accel_devices.h"
8 #include "adf_common_drv.h"
9 #include "icp_qat_hal.h"
10 #include "icp_qat_uclo.h"
12 #define BAD_REGADDR 0xffff
13 #define MAX_RETRY_TIMES 10000
14 #define INIT_CTX_ARB_VALUE 0x0
15 #define INIT_CTX_ENABLE_VALUE 0x0
16 #define INIT_PC_VALUE 0x0
17 #define INIT_WAKEUP_EVENTS_VALUE 0x1
18 #define INIT_SIG_EVENTS_VALUE 0x1
19 #define INIT_CCENABLE_VALUE 0x2000
20 #define RST_CSR_QAT_LSB 20
21 #define RST_CSR_AE_LSB 0
22 #define MC_TIMESTAMP_ENABLE (0x1 << 7)
24 #define IGNORE_W1C_MASK ((~(1 << CE_BREAKPOINT_BITPOS)) & \
25 (~(1 << CE_CNTL_STORE_PARITY_ERROR_BITPOS)) & \
26 (~(1 << CE_REG_PAR_ERR_BITPOS)))
27 #define INSERT_IMMED_GPRA_CONST(inst, const_val) \
28 (inst = ((inst & 0xFFFF00C03FFull) | \
29 ((((const_val) << 12) & 0x0FF00000ull) | \
30 (((const_val) << 10) & 0x0003FC00ull))))
31 #define INSERT_IMMED_GPRB_CONST(inst, const_val) \
32 (inst = ((inst & 0xFFFF00FFF00ull) | \
33 ((((const_val) << 12) & 0x0FF00000ull) | \
34 (((const_val) << 0) & 0x000000FFull))))
36 #define AE(handle, ae) handle->hal_handle->aes[ae]
38 static const u64 inst_4b[] = {
39 0x0F0400C0000ull, 0x0F4400C0000ull, 0x0F040000300ull, 0x0F440000300ull,
40 0x0FC066C0000ull, 0x0F0000C0300ull, 0x0F0000C0300ull, 0x0F0000C0300ull,
44 static const u64 inst[] = {
45 0x0F0000C0000ull, 0x0F000000380ull, 0x0D805000011ull, 0x0FC082C0300ull,
46 0x0F0000C0300ull, 0x0F0000C0300ull, 0x0F0000C0300ull, 0x0F0000C0300ull,
47 0x0A0643C0000ull, 0x0BAC0000301ull, 0x0D802000101ull, 0x0F0000C0001ull,
48 0x0FC066C0001ull, 0x0F0000C0300ull, 0x0F0000C0300ull, 0x0F0000C0300ull,
49 0x0F000400300ull, 0x0A0610C0000ull, 0x0BAC0000301ull, 0x0D804400101ull,
50 0x0A0580C0000ull, 0x0A0581C0000ull, 0x0A0582C0000ull, 0x0A0583C0000ull,
51 0x0A0584C0000ull, 0x0A0585C0000ull, 0x0A0586C0000ull, 0x0A0587C0000ull,
52 0x0A0588C0000ull, 0x0A0589C0000ull, 0x0A058AC0000ull, 0x0A058BC0000ull,
53 0x0A058CC0000ull, 0x0A058DC0000ull, 0x0A058EC0000ull, 0x0A058FC0000ull,
54 0x0A05C0C0000ull, 0x0A05C1C0000ull, 0x0A05C2C0000ull, 0x0A05C3C0000ull,
55 0x0A05C4C0000ull, 0x0A05C5C0000ull, 0x0A05C6C0000ull, 0x0A05C7C0000ull,
56 0x0A05C8C0000ull, 0x0A05C9C0000ull, 0x0A05CAC0000ull, 0x0A05CBC0000ull,
57 0x0A05CCC0000ull, 0x0A05CDC0000ull, 0x0A05CEC0000ull, 0x0A05CFC0000ull,
58 0x0A0400C0000ull, 0x0B0400C0000ull, 0x0A0401C0000ull, 0x0B0401C0000ull,
59 0x0A0402C0000ull, 0x0B0402C0000ull, 0x0A0403C0000ull, 0x0B0403C0000ull,
60 0x0A0404C0000ull, 0x0B0404C0000ull, 0x0A0405C0000ull, 0x0B0405C0000ull,
61 0x0A0406C0000ull, 0x0B0406C0000ull, 0x0A0407C0000ull, 0x0B0407C0000ull,
62 0x0A0408C0000ull, 0x0B0408C0000ull, 0x0A0409C0000ull, 0x0B0409C0000ull,
63 0x0A040AC0000ull, 0x0B040AC0000ull, 0x0A040BC0000ull, 0x0B040BC0000ull,
64 0x0A040CC0000ull, 0x0B040CC0000ull, 0x0A040DC0000ull, 0x0B040DC0000ull,
65 0x0A040EC0000ull, 0x0B040EC0000ull, 0x0A040FC0000ull, 0x0B040FC0000ull,
66 0x0D81581C010ull, 0x0E000010000ull, 0x0E000010000ull,
69 void qat_hal_set_live_ctx(struct icp_qat_fw_loader_handle *handle,
70 unsigned char ae, unsigned int ctx_mask)
72 AE(handle, ae).live_ctx_mask = ctx_mask;
75 #define CSR_RETRY_TIMES 500
76 static int qat_hal_rd_ae_csr(struct icp_qat_fw_loader_handle *handle,
77 unsigned char ae, unsigned int csr)
79 unsigned int iterations = CSR_RETRY_TIMES;
83 value = GET_AE_CSR(handle, ae, csr);
84 if (!(GET_AE_CSR(handle, ae, LOCAL_CSR_STATUS) & LCS_STATUS))
86 } while (iterations--);
88 pr_err("QAT: Read CSR timeout\n");
92 static int qat_hal_wr_ae_csr(struct icp_qat_fw_loader_handle *handle,
93 unsigned char ae, unsigned int csr,
96 unsigned int iterations = CSR_RETRY_TIMES;
99 SET_AE_CSR(handle, ae, csr, value);
100 if (!(GET_AE_CSR(handle, ae, LOCAL_CSR_STATUS) & LCS_STATUS))
102 } while (iterations--);
104 pr_err("QAT: Write CSR Timeout\n");
108 static void qat_hal_get_wakeup_event(struct icp_qat_fw_loader_handle *handle,
109 unsigned char ae, unsigned char ctx,
110 unsigned int *events)
112 unsigned int cur_ctx;
114 cur_ctx = qat_hal_rd_ae_csr(handle, ae, CSR_CTX_POINTER);
115 qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, ctx);
116 *events = qat_hal_rd_ae_csr(handle, ae, CTX_WAKEUP_EVENTS_INDIRECT);
117 qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, cur_ctx);
120 static int qat_hal_wait_cycles(struct icp_qat_fw_loader_handle *handle,
121 unsigned char ae, unsigned int cycles,
124 unsigned int base_cnt = 0, cur_cnt = 0;
125 unsigned int csr = (1 << ACS_ABO_BITPOS);
126 int times = MAX_RETRY_TIMES;
127 int elapsed_cycles = 0;
129 base_cnt = qat_hal_rd_ae_csr(handle, ae, PROFILE_COUNT);
131 while ((int)cycles > elapsed_cycles && times--) {
133 csr = qat_hal_rd_ae_csr(handle, ae, ACTIVE_CTX_STATUS);
135 cur_cnt = qat_hal_rd_ae_csr(handle, ae, PROFILE_COUNT);
137 elapsed_cycles = cur_cnt - base_cnt;
139 if (elapsed_cycles < 0)
140 elapsed_cycles += 0x10000;
142 /* ensure at least 8 time cycles elapsed in wait_cycles */
143 if (elapsed_cycles >= 8 && !(csr & (1 << ACS_ABO_BITPOS)))
147 pr_err("QAT: wait_num_cycles time out\n");
153 #define CLR_BIT(wrd, bit) (wrd & ~(1 << bit))
154 #define SET_BIT(wrd, bit) (wrd | 1 << bit)
156 int qat_hal_set_ae_ctx_mode(struct icp_qat_fw_loader_handle *handle,
157 unsigned char ae, unsigned char mode)
159 unsigned int csr, new_csr;
161 if ((mode != 4) && (mode != 8)) {
162 pr_err("QAT: bad ctx mode=%d\n", mode);
166 /* Sets the accelaration engine context mode to either four or eight */
167 csr = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
168 csr = IGNORE_W1C_MASK & csr;
169 new_csr = (mode == 4) ?
170 SET_BIT(csr, CE_INUSE_CONTEXTS_BITPOS) :
171 CLR_BIT(csr, CE_INUSE_CONTEXTS_BITPOS);
172 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, new_csr);
176 int qat_hal_set_ae_nn_mode(struct icp_qat_fw_loader_handle *handle,
177 unsigned char ae, unsigned char mode)
179 unsigned int csr, new_csr;
181 csr = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
182 csr &= IGNORE_W1C_MASK;
185 SET_BIT(csr, CE_NN_MODE_BITPOS) :
186 CLR_BIT(csr, CE_NN_MODE_BITPOS);
189 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, new_csr);
194 int qat_hal_set_ae_lm_mode(struct icp_qat_fw_loader_handle *handle,
195 unsigned char ae, enum icp_qat_uof_regtype lm_type,
198 unsigned int csr, new_csr;
200 csr = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
201 csr &= IGNORE_W1C_MASK;
205 SET_BIT(csr, CE_LMADDR_0_GLOBAL_BITPOS) :
206 CLR_BIT(csr, CE_LMADDR_0_GLOBAL_BITPOS);
210 SET_BIT(csr, CE_LMADDR_1_GLOBAL_BITPOS) :
211 CLR_BIT(csr, CE_LMADDR_1_GLOBAL_BITPOS);
214 pr_err("QAT: lmType = 0x%x\n", lm_type);
219 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, new_csr);
223 static unsigned short qat_hal_get_reg_addr(unsigned int type,
224 unsigned short reg_num)
226 unsigned short reg_addr;
231 reg_addr = 0x80 | (reg_num & 0x7f);
235 reg_addr = reg_num & 0x1f;
240 reg_addr = 0x180 | (reg_num & 0x1f);
243 reg_addr = 0x140 | ((reg_num & 0x3) << 1);
248 reg_addr = 0x1c0 | (reg_num & 0x1f);
251 reg_addr = 0x100 | ((reg_num & 0x3) << 1);
254 reg_addr = 0x280 | (reg_num & 0x1f);
263 reg_addr = 0x300 | (reg_num & 0xff);
266 reg_addr = BAD_REGADDR;
272 void qat_hal_reset(struct icp_qat_fw_loader_handle *handle)
274 unsigned int ae_reset_csr;
276 ae_reset_csr = GET_GLB_CSR(handle, ICP_RESET);
277 ae_reset_csr |= handle->hal_handle->ae_mask << RST_CSR_AE_LSB;
278 ae_reset_csr |= handle->hal_handle->slice_mask << RST_CSR_QAT_LSB;
279 SET_GLB_CSR(handle, ICP_RESET, ae_reset_csr);
282 static void qat_hal_wr_indr_csr(struct icp_qat_fw_loader_handle *handle,
283 unsigned char ae, unsigned int ctx_mask,
284 unsigned int ae_csr, unsigned int csr_val)
286 unsigned int ctx, cur_ctx;
288 cur_ctx = qat_hal_rd_ae_csr(handle, ae, CSR_CTX_POINTER);
290 for (ctx = 0; ctx < ICP_QAT_UCLO_MAX_CTX; ctx++) {
291 if (!(ctx_mask & (1 << ctx)))
293 qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, ctx);
294 qat_hal_wr_ae_csr(handle, ae, ae_csr, csr_val);
297 qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, cur_ctx);
300 static unsigned int qat_hal_rd_indr_csr(struct icp_qat_fw_loader_handle *handle,
301 unsigned char ae, unsigned char ctx,
304 unsigned int cur_ctx, csr_val;
306 cur_ctx = qat_hal_rd_ae_csr(handle, ae, CSR_CTX_POINTER);
307 qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, ctx);
308 csr_val = qat_hal_rd_ae_csr(handle, ae, ae_csr);
309 qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, cur_ctx);
314 static void qat_hal_put_sig_event(struct icp_qat_fw_loader_handle *handle,
315 unsigned char ae, unsigned int ctx_mask,
318 unsigned int ctx, cur_ctx;
320 cur_ctx = qat_hal_rd_ae_csr(handle, ae, CSR_CTX_POINTER);
321 for (ctx = 0; ctx < ICP_QAT_UCLO_MAX_CTX; ctx++) {
322 if (!(ctx_mask & (1 << ctx)))
324 qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, ctx);
325 qat_hal_wr_ae_csr(handle, ae, CTX_SIG_EVENTS_INDIRECT, events);
327 qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, cur_ctx);
330 static void qat_hal_put_wakeup_event(struct icp_qat_fw_loader_handle *handle,
331 unsigned char ae, unsigned int ctx_mask,
334 unsigned int ctx, cur_ctx;
336 cur_ctx = qat_hal_rd_ae_csr(handle, ae, CSR_CTX_POINTER);
337 for (ctx = 0; ctx < ICP_QAT_UCLO_MAX_CTX; ctx++) {
338 if (!(ctx_mask & (1 << ctx)))
340 qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, ctx);
341 qat_hal_wr_ae_csr(handle, ae, CTX_WAKEUP_EVENTS_INDIRECT,
344 qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, cur_ctx);
347 static int qat_hal_check_ae_alive(struct icp_qat_fw_loader_handle *handle)
349 unsigned int base_cnt, cur_cnt;
351 int times = MAX_RETRY_TIMES;
353 for (ae = 0; ae < handle->hal_handle->ae_max_num; ae++) {
354 base_cnt = qat_hal_rd_ae_csr(handle, ae, PROFILE_COUNT);
358 cur_cnt = qat_hal_rd_ae_csr(handle, ae, PROFILE_COUNT);
360 } while (times-- && (cur_cnt == base_cnt));
363 pr_err("QAT: AE%d is inactive!!\n", ae);
371 int qat_hal_check_ae_active(struct icp_qat_fw_loader_handle *handle,
374 unsigned int enable = 0, active = 0;
376 enable = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
377 active = qat_hal_rd_ae_csr(handle, ae, ACTIVE_CTX_STATUS);
378 if ((enable & (0xff << CE_ENABLE_BITPOS)) ||
379 (active & (1 << ACS_ABO_BITPOS)))
385 static void qat_hal_reset_timestamp(struct icp_qat_fw_loader_handle *handle)
387 unsigned int misc_ctl;
390 /* stop the timestamp timers */
391 misc_ctl = GET_GLB_CSR(handle, MISC_CONTROL);
392 if (misc_ctl & MC_TIMESTAMP_ENABLE)
393 SET_GLB_CSR(handle, MISC_CONTROL, misc_ctl &
394 (~MC_TIMESTAMP_ENABLE));
396 for (ae = 0; ae < handle->hal_handle->ae_max_num; ae++) {
397 qat_hal_wr_ae_csr(handle, ae, TIMESTAMP_LOW, 0);
398 qat_hal_wr_ae_csr(handle, ae, TIMESTAMP_HIGH, 0);
400 /* start timestamp timers */
401 SET_GLB_CSR(handle, MISC_CONTROL, misc_ctl | MC_TIMESTAMP_ENABLE);
404 #define ESRAM_AUTO_TINIT BIT(2)
405 #define ESRAM_AUTO_TINIT_DONE BIT(3)
406 #define ESRAM_AUTO_INIT_USED_CYCLES (1640)
407 #define ESRAM_AUTO_INIT_CSR_OFFSET 0xC1C
408 static int qat_hal_init_esram(struct icp_qat_fw_loader_handle *handle)
410 void __iomem *csr_addr =
411 (void __iomem *)((uintptr_t)handle->hal_ep_csr_addr_v +
412 ESRAM_AUTO_INIT_CSR_OFFSET);
413 unsigned int csr_val;
416 if (handle->pci_dev->device != PCI_DEVICE_ID_INTEL_QAT_DH895XCC)
419 csr_val = ADF_CSR_RD(csr_addr, 0);
420 if ((csr_val & ESRAM_AUTO_TINIT) && (csr_val & ESRAM_AUTO_TINIT_DONE))
423 csr_val = ADF_CSR_RD(csr_addr, 0);
424 csr_val |= ESRAM_AUTO_TINIT;
425 ADF_CSR_WR(csr_addr, 0, csr_val);
428 qat_hal_wait_cycles(handle, 0, ESRAM_AUTO_INIT_USED_CYCLES, 0);
429 csr_val = ADF_CSR_RD(csr_addr, 0);
430 } while (!(csr_val & ESRAM_AUTO_TINIT_DONE) && times--);
432 pr_err("QAT: Fail to init eSram!\n");
438 #define SHRAM_INIT_CYCLES 2060
439 int qat_hal_clr_reset(struct icp_qat_fw_loader_handle *handle)
441 unsigned int ae_reset_csr;
443 unsigned int clk_csr;
444 unsigned int times = 100;
447 /* write to the reset csr */
448 ae_reset_csr = GET_GLB_CSR(handle, ICP_RESET);
449 ae_reset_csr &= ~(handle->hal_handle->ae_mask << RST_CSR_AE_LSB);
450 ae_reset_csr &= ~(handle->hal_handle->slice_mask << RST_CSR_QAT_LSB);
452 SET_GLB_CSR(handle, ICP_RESET, ae_reset_csr);
455 csr = GET_GLB_CSR(handle, ICP_RESET);
456 } while ((handle->hal_handle->ae_mask |
457 (handle->hal_handle->slice_mask << RST_CSR_QAT_LSB)) & csr);
459 clk_csr = GET_GLB_CSR(handle, ICP_GLOBAL_CLK_ENABLE);
460 clk_csr |= handle->hal_handle->ae_mask << 0;
461 clk_csr |= handle->hal_handle->slice_mask << 20;
462 SET_GLB_CSR(handle, ICP_GLOBAL_CLK_ENABLE, clk_csr);
463 if (qat_hal_check_ae_alive(handle))
466 /* Set undefined power-up/reset states to reasonable default values */
467 for (ae = 0; ae < handle->hal_handle->ae_max_num; ae++) {
468 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES,
469 INIT_CTX_ENABLE_VALUE);
470 qat_hal_wr_indr_csr(handle, ae, ICP_QAT_UCLO_AE_ALL_CTX,
472 handle->hal_handle->upc_mask &
474 qat_hal_wr_ae_csr(handle, ae, CTX_ARB_CNTL, INIT_CTX_ARB_VALUE);
475 qat_hal_wr_ae_csr(handle, ae, CC_ENABLE, INIT_CCENABLE_VALUE);
476 qat_hal_put_wakeup_event(handle, ae,
477 ICP_QAT_UCLO_AE_ALL_CTX,
478 INIT_WAKEUP_EVENTS_VALUE);
479 qat_hal_put_sig_event(handle, ae,
480 ICP_QAT_UCLO_AE_ALL_CTX,
481 INIT_SIG_EVENTS_VALUE);
483 if (qat_hal_init_esram(handle))
485 if (qat_hal_wait_cycles(handle, 0, SHRAM_INIT_CYCLES, 0))
487 qat_hal_reset_timestamp(handle);
491 pr_err("QAT: failed to get device out of reset\n");
495 static void qat_hal_disable_ctx(struct icp_qat_fw_loader_handle *handle,
496 unsigned char ae, unsigned int ctx_mask)
500 ctx = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
501 ctx &= IGNORE_W1C_MASK &
502 (~((ctx_mask & ICP_QAT_UCLO_AE_ALL_CTX) << CE_ENABLE_BITPOS));
503 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx);
506 static u64 qat_hal_parity_64bit(u64 word)
517 static u64 qat_hal_set_uword_ecc(u64 uword)
519 u64 bit0_mask = 0xff800007fffULL, bit1_mask = 0x1f801ff801fULL,
520 bit2_mask = 0xe387e0781e1ULL, bit3_mask = 0x7cb8e388e22ULL,
521 bit4_mask = 0xaf5b2c93244ULL, bit5_mask = 0xf56d5525488ULL,
522 bit6_mask = 0xdaf69a46910ULL;
524 /* clear the ecc bits */
525 uword &= ~(0x7fULL << 0x2C);
526 uword |= qat_hal_parity_64bit(bit0_mask & uword) << 0x2C;
527 uword |= qat_hal_parity_64bit(bit1_mask & uword) << 0x2D;
528 uword |= qat_hal_parity_64bit(bit2_mask & uword) << 0x2E;
529 uword |= qat_hal_parity_64bit(bit3_mask & uword) << 0x2F;
530 uword |= qat_hal_parity_64bit(bit4_mask & uword) << 0x30;
531 uword |= qat_hal_parity_64bit(bit5_mask & uword) << 0x31;
532 uword |= qat_hal_parity_64bit(bit6_mask & uword) << 0x32;
536 void qat_hal_wr_uwords(struct icp_qat_fw_loader_handle *handle,
537 unsigned char ae, unsigned int uaddr,
538 unsigned int words_num, u64 *uword)
540 unsigned int ustore_addr;
543 ustore_addr = qat_hal_rd_ae_csr(handle, ae, USTORE_ADDRESS);
545 qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, uaddr);
546 for (i = 0; i < words_num; i++) {
547 unsigned int uwrd_lo, uwrd_hi;
550 tmp = qat_hal_set_uword_ecc(uword[i]);
551 uwrd_lo = (unsigned int)(tmp & 0xffffffff);
552 uwrd_hi = (unsigned int)(tmp >> 0x20);
553 qat_hal_wr_ae_csr(handle, ae, USTORE_DATA_LOWER, uwrd_lo);
554 qat_hal_wr_ae_csr(handle, ae, USTORE_DATA_UPPER, uwrd_hi);
556 qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, ustore_addr);
559 static void qat_hal_enable_ctx(struct icp_qat_fw_loader_handle *handle,
560 unsigned char ae, unsigned int ctx_mask)
564 ctx = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
565 ctx &= IGNORE_W1C_MASK;
566 ctx_mask &= (ctx & CE_INUSE_CONTEXTS) ? 0x55 : 0xFF;
567 ctx |= (ctx_mask << CE_ENABLE_BITPOS);
568 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx);
571 static void qat_hal_clear_xfer(struct icp_qat_fw_loader_handle *handle)
576 for (ae = 0; ae < handle->hal_handle->ae_max_num; ae++) {
577 for (reg = 0; reg < ICP_QAT_UCLO_MAX_GPR_REG; reg++) {
578 qat_hal_init_rd_xfer(handle, ae, 0, ICP_SR_RD_ABS,
580 qat_hal_init_rd_xfer(handle, ae, 0, ICP_DR_RD_ABS,
586 static int qat_hal_clear_gpr(struct icp_qat_fw_loader_handle *handle)
589 unsigned int ctx_mask = ICP_QAT_UCLO_AE_ALL_CTX;
590 int times = MAX_RETRY_TIMES;
591 unsigned int csr_val = 0;
592 unsigned int savctx = 0;
595 for (ae = 0; ae < handle->hal_handle->ae_max_num; ae++) {
596 csr_val = qat_hal_rd_ae_csr(handle, ae, AE_MISC_CONTROL);
597 csr_val &= ~(1 << MMC_SHARE_CS_BITPOS);
598 qat_hal_wr_ae_csr(handle, ae, AE_MISC_CONTROL, csr_val);
599 csr_val = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
600 csr_val &= IGNORE_W1C_MASK;
601 csr_val |= CE_NN_MODE;
602 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, csr_val);
603 qat_hal_wr_uwords(handle, ae, 0, ARRAY_SIZE(inst),
605 qat_hal_wr_indr_csr(handle, ae, ctx_mask, CTX_STS_INDIRECT,
606 handle->hal_handle->upc_mask &
608 savctx = qat_hal_rd_ae_csr(handle, ae, ACTIVE_CTX_STATUS);
609 qat_hal_wr_ae_csr(handle, ae, ACTIVE_CTX_STATUS, 0);
610 qat_hal_put_wakeup_event(handle, ae, ctx_mask, XCWE_VOLUNTARY);
611 qat_hal_wr_indr_csr(handle, ae, ctx_mask,
612 CTX_SIG_EVENTS_INDIRECT, 0);
613 qat_hal_wr_ae_csr(handle, ae, CTX_SIG_EVENTS_ACTIVE, 0);
614 qat_hal_enable_ctx(handle, ae, ctx_mask);
616 for (ae = 0; ae < handle->hal_handle->ae_max_num; ae++) {
617 /* wait for AE to finish */
619 ret = qat_hal_wait_cycles(handle, ae, 20, 1);
620 } while (ret && times--);
623 pr_err("QAT: clear GPR of AE %d failed", ae);
626 qat_hal_disable_ctx(handle, ae, ctx_mask);
627 qat_hal_wr_ae_csr(handle, ae, ACTIVE_CTX_STATUS,
629 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES,
630 INIT_CTX_ENABLE_VALUE);
631 qat_hal_wr_indr_csr(handle, ae, ctx_mask, CTX_STS_INDIRECT,
632 handle->hal_handle->upc_mask &
634 qat_hal_wr_ae_csr(handle, ae, CTX_ARB_CNTL, INIT_CTX_ARB_VALUE);
635 qat_hal_wr_ae_csr(handle, ae, CC_ENABLE, INIT_CCENABLE_VALUE);
636 qat_hal_put_wakeup_event(handle, ae, ctx_mask,
637 INIT_WAKEUP_EVENTS_VALUE);
638 qat_hal_put_sig_event(handle, ae, ctx_mask,
639 INIT_SIG_EVENTS_VALUE);
644 #define ICP_QAT_AE_OFFSET 0x20000
645 #define ICP_QAT_CAP_OFFSET (ICP_QAT_AE_OFFSET + 0x10000)
646 #define LOCAL_TO_XFER_REG_OFFSET 0x800
647 #define ICP_QAT_EP_OFFSET 0x3a000
648 int qat_hal_init(struct adf_accel_dev *accel_dev)
651 unsigned int max_en_ae_id = 0;
652 struct icp_qat_fw_loader_handle *handle;
653 struct adf_accel_pci *pci_info = &accel_dev->accel_pci_dev;
654 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
655 struct adf_bar *misc_bar =
656 &pci_info->pci_bars[hw_data->get_misc_bar_id(hw_data)];
657 struct adf_bar *sram_bar;
659 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
663 handle->hal_cap_g_ctl_csr_addr_v =
664 (void __iomem *)((uintptr_t)misc_bar->virt_addr +
666 handle->hal_cap_ae_xfer_csr_addr_v =
667 (void __iomem *)((uintptr_t)misc_bar->virt_addr +
669 handle->hal_ep_csr_addr_v =
670 (void __iomem *)((uintptr_t)misc_bar->virt_addr +
672 handle->hal_cap_ae_local_csr_addr_v =
673 (void __iomem *)((uintptr_t)handle->hal_cap_ae_xfer_csr_addr_v +
674 LOCAL_TO_XFER_REG_OFFSET);
675 handle->pci_dev = pci_info->pci_dev;
676 if (handle->pci_dev->device == PCI_DEVICE_ID_INTEL_QAT_DH895XCC) {
678 &pci_info->pci_bars[hw_data->get_sram_bar_id(hw_data)];
679 handle->hal_sram_addr_v = sram_bar->virt_addr;
681 handle->fw_auth = (handle->pci_dev->device ==
682 PCI_DEVICE_ID_INTEL_QAT_DH895XCC) ? false : true;
683 handle->hal_handle = kzalloc(sizeof(*handle->hal_handle), GFP_KERNEL);
684 if (!handle->hal_handle)
686 handle->hal_handle->revision_id = accel_dev->accel_pci_dev.revid;
687 handle->hal_handle->ae_mask = hw_data->ae_mask;
688 handle->hal_handle->slice_mask = hw_data->accel_mask;
689 /* create AE objects */
690 handle->hal_handle->upc_mask = 0x1ffff;
691 handle->hal_handle->max_ustore = 0x4000;
692 for (ae = 0; ae < ICP_QAT_UCLO_MAX_AE; ae++) {
693 if (!(hw_data->ae_mask & (1 << ae)))
695 handle->hal_handle->aes[ae].free_addr = 0;
696 handle->hal_handle->aes[ae].free_size =
697 handle->hal_handle->max_ustore;
698 handle->hal_handle->aes[ae].ustore_size =
699 handle->hal_handle->max_ustore;
700 handle->hal_handle->aes[ae].live_ctx_mask =
701 ICP_QAT_UCLO_AE_ALL_CTX;
704 handle->hal_handle->ae_max_num = max_en_ae_id + 1;
705 /* take all AEs out of reset */
706 if (qat_hal_clr_reset(handle)) {
707 dev_err(&GET_DEV(accel_dev), "qat_hal_clr_reset error\n");
710 qat_hal_clear_xfer(handle);
711 if (!handle->fw_auth) {
712 if (qat_hal_clear_gpr(handle))
716 /* Set SIGNATURE_ENABLE[0] to 0x1 in order to enable ALU_OUT csr */
717 for (ae = 0; ae < handle->hal_handle->ae_max_num; ae++) {
718 unsigned int csr_val = 0;
720 csr_val = qat_hal_rd_ae_csr(handle, ae, SIGNATURE_ENABLE);
722 qat_hal_wr_ae_csr(handle, ae, SIGNATURE_ENABLE, csr_val);
724 accel_dev->fw_loader->fw_loader = handle;
728 kfree(handle->hal_handle);
734 void qat_hal_deinit(struct icp_qat_fw_loader_handle *handle)
738 kfree(handle->hal_handle);
742 void qat_hal_start(struct icp_qat_fw_loader_handle *handle, unsigned char ae,
743 unsigned int ctx_mask)
746 unsigned int fcu_sts = 0;
748 if (handle->fw_auth) {
749 SET_CAP_CSR(handle, FCU_CONTROL, FCU_CTRL_CMD_START);
751 msleep(FW_AUTH_WAIT_PERIOD);
752 fcu_sts = GET_CAP_CSR(handle, FCU_STATUS);
753 if (((fcu_sts >> FCU_STS_DONE_POS) & 0x1))
755 } while (retry++ < FW_AUTH_MAX_RETRY);
756 pr_err("QAT: start error (AE 0x%x FCU_STS = 0x%x)\n", ae,
759 qat_hal_put_wakeup_event(handle, ae, (~ctx_mask) &
760 ICP_QAT_UCLO_AE_ALL_CTX, 0x10000);
761 qat_hal_enable_ctx(handle, ae, ctx_mask);
765 void qat_hal_stop(struct icp_qat_fw_loader_handle *handle, unsigned char ae,
766 unsigned int ctx_mask)
768 if (!handle->fw_auth)
769 qat_hal_disable_ctx(handle, ae, ctx_mask);
772 void qat_hal_set_pc(struct icp_qat_fw_loader_handle *handle,
773 unsigned char ae, unsigned int ctx_mask, unsigned int upc)
775 qat_hal_wr_indr_csr(handle, ae, ctx_mask, CTX_STS_INDIRECT,
776 handle->hal_handle->upc_mask & upc);
779 static void qat_hal_get_uwords(struct icp_qat_fw_loader_handle *handle,
780 unsigned char ae, unsigned int uaddr,
781 unsigned int words_num, u64 *uword)
783 unsigned int i, uwrd_lo, uwrd_hi;
784 unsigned int ustore_addr, misc_control;
786 misc_control = qat_hal_rd_ae_csr(handle, ae, AE_MISC_CONTROL);
787 qat_hal_wr_ae_csr(handle, ae, AE_MISC_CONTROL,
788 misc_control & 0xfffffffb);
789 ustore_addr = qat_hal_rd_ae_csr(handle, ae, USTORE_ADDRESS);
791 for (i = 0; i < words_num; i++) {
792 qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, uaddr);
794 uwrd_lo = qat_hal_rd_ae_csr(handle, ae, USTORE_DATA_LOWER);
795 uwrd_hi = qat_hal_rd_ae_csr(handle, ae, USTORE_DATA_UPPER);
797 uword[i] = (uword[i] << 0x20) | uwrd_lo;
799 qat_hal_wr_ae_csr(handle, ae, AE_MISC_CONTROL, misc_control);
800 qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, ustore_addr);
803 void qat_hal_wr_umem(struct icp_qat_fw_loader_handle *handle,
804 unsigned char ae, unsigned int uaddr,
805 unsigned int words_num, unsigned int *data)
807 unsigned int i, ustore_addr;
809 ustore_addr = qat_hal_rd_ae_csr(handle, ae, USTORE_ADDRESS);
811 qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, uaddr);
812 for (i = 0; i < words_num; i++) {
813 unsigned int uwrd_lo, uwrd_hi, tmp;
815 uwrd_lo = ((data[i] & 0xfff0000) << 4) | (0x3 << 18) |
816 ((data[i] & 0xff00) << 2) |
817 (0x3 << 8) | (data[i] & 0xff);
818 uwrd_hi = (0xf << 4) | ((data[i] & 0xf0000000) >> 28);
819 uwrd_hi |= (hweight32(data[i] & 0xffff) & 0x1) << 8;
820 tmp = ((data[i] >> 0x10) & 0xffff);
821 uwrd_hi |= (hweight32(tmp) & 0x1) << 9;
822 qat_hal_wr_ae_csr(handle, ae, USTORE_DATA_LOWER, uwrd_lo);
823 qat_hal_wr_ae_csr(handle, ae, USTORE_DATA_UPPER, uwrd_hi);
825 qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, ustore_addr);
828 #define MAX_EXEC_INST 100
829 static int qat_hal_exec_micro_inst(struct icp_qat_fw_loader_handle *handle,
830 unsigned char ae, unsigned char ctx,
831 u64 *micro_inst, unsigned int inst_num,
832 int code_off, unsigned int max_cycle,
835 u64 savuwords[MAX_EXEC_INST];
836 unsigned int ind_lm_addr0, ind_lm_addr1;
837 unsigned int ind_lm_addr_byte0, ind_lm_addr_byte1;
838 unsigned int ind_cnt_sig;
839 unsigned int ind_sig, act_sig;
840 unsigned int csr_val = 0, newcsr_val;
842 unsigned int savcc, wakeup_events, savpc;
843 unsigned int ctxarb_ctl, ctx_enables;
845 if ((inst_num > handle->hal_handle->max_ustore) || !micro_inst) {
846 pr_err("QAT: invalid instruction num %d\n", inst_num);
849 /* save current context */
850 ind_lm_addr0 = qat_hal_rd_indr_csr(handle, ae, ctx, LM_ADDR_0_INDIRECT);
851 ind_lm_addr1 = qat_hal_rd_indr_csr(handle, ae, ctx, LM_ADDR_1_INDIRECT);
852 ind_lm_addr_byte0 = qat_hal_rd_indr_csr(handle, ae, ctx,
853 INDIRECT_LM_ADDR_0_BYTE_INDEX);
854 ind_lm_addr_byte1 = qat_hal_rd_indr_csr(handle, ae, ctx,
855 INDIRECT_LM_ADDR_1_BYTE_INDEX);
856 if (inst_num <= MAX_EXEC_INST)
857 qat_hal_get_uwords(handle, ae, 0, inst_num, savuwords);
858 qat_hal_get_wakeup_event(handle, ae, ctx, &wakeup_events);
859 savpc = qat_hal_rd_indr_csr(handle, ae, ctx, CTX_STS_INDIRECT);
860 savpc = (savpc & handle->hal_handle->upc_mask) >> 0;
861 ctx_enables = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
862 ctx_enables &= IGNORE_W1C_MASK;
863 savcc = qat_hal_rd_ae_csr(handle, ae, CC_ENABLE);
864 savctx = qat_hal_rd_ae_csr(handle, ae, ACTIVE_CTX_STATUS);
865 ctxarb_ctl = qat_hal_rd_ae_csr(handle, ae, CTX_ARB_CNTL);
866 ind_cnt_sig = qat_hal_rd_indr_csr(handle, ae, ctx,
867 FUTURE_COUNT_SIGNAL_INDIRECT);
868 ind_sig = qat_hal_rd_indr_csr(handle, ae, ctx,
869 CTX_SIG_EVENTS_INDIRECT);
870 act_sig = qat_hal_rd_ae_csr(handle, ae, CTX_SIG_EVENTS_ACTIVE);
871 /* execute micro codes */
872 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx_enables);
873 qat_hal_wr_uwords(handle, ae, 0, inst_num, micro_inst);
874 qat_hal_wr_indr_csr(handle, ae, (1 << ctx), CTX_STS_INDIRECT, 0);
875 qat_hal_wr_ae_csr(handle, ae, ACTIVE_CTX_STATUS, ctx & ACS_ACNO);
877 qat_hal_wr_ae_csr(handle, ae, CC_ENABLE, savcc & 0xffffdfff);
878 qat_hal_put_wakeup_event(handle, ae, (1 << ctx), XCWE_VOLUNTARY);
879 qat_hal_wr_indr_csr(handle, ae, (1 << ctx), CTX_SIG_EVENTS_INDIRECT, 0);
880 qat_hal_wr_ae_csr(handle, ae, CTX_SIG_EVENTS_ACTIVE, 0);
881 qat_hal_enable_ctx(handle, ae, (1 << ctx));
882 /* wait for micro codes to finish */
883 if (qat_hal_wait_cycles(handle, ae, max_cycle, 1) != 0)
886 unsigned int ctx_status;
888 ctx_status = qat_hal_rd_indr_csr(handle, ae, ctx,
890 *endpc = ctx_status & handle->hal_handle->upc_mask;
892 /* retore to saved context */
893 qat_hal_disable_ctx(handle, ae, (1 << ctx));
894 if (inst_num <= MAX_EXEC_INST)
895 qat_hal_wr_uwords(handle, ae, 0, inst_num, savuwords);
896 qat_hal_put_wakeup_event(handle, ae, (1 << ctx), wakeup_events);
897 qat_hal_wr_indr_csr(handle, ae, (1 << ctx), CTX_STS_INDIRECT,
898 handle->hal_handle->upc_mask & savpc);
899 csr_val = qat_hal_rd_ae_csr(handle, ae, AE_MISC_CONTROL);
900 newcsr_val = CLR_BIT(csr_val, MMC_SHARE_CS_BITPOS);
901 qat_hal_wr_ae_csr(handle, ae, AE_MISC_CONTROL, newcsr_val);
902 qat_hal_wr_ae_csr(handle, ae, CC_ENABLE, savcc);
903 qat_hal_wr_ae_csr(handle, ae, ACTIVE_CTX_STATUS, savctx & ACS_ACNO);
904 qat_hal_wr_ae_csr(handle, ae, CTX_ARB_CNTL, ctxarb_ctl);
905 qat_hal_wr_indr_csr(handle, ae, (1 << ctx),
906 LM_ADDR_0_INDIRECT, ind_lm_addr0);
907 qat_hal_wr_indr_csr(handle, ae, (1 << ctx),
908 LM_ADDR_1_INDIRECT, ind_lm_addr1);
909 qat_hal_wr_indr_csr(handle, ae, (1 << ctx),
910 INDIRECT_LM_ADDR_0_BYTE_INDEX, ind_lm_addr_byte0);
911 qat_hal_wr_indr_csr(handle, ae, (1 << ctx),
912 INDIRECT_LM_ADDR_1_BYTE_INDEX, ind_lm_addr_byte1);
913 qat_hal_wr_indr_csr(handle, ae, (1 << ctx),
914 FUTURE_COUNT_SIGNAL_INDIRECT, ind_cnt_sig);
915 qat_hal_wr_indr_csr(handle, ae, (1 << ctx),
916 CTX_SIG_EVENTS_INDIRECT, ind_sig);
917 qat_hal_wr_ae_csr(handle, ae, CTX_SIG_EVENTS_ACTIVE, act_sig);
918 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx_enables);
923 static int qat_hal_rd_rel_reg(struct icp_qat_fw_loader_handle *handle,
924 unsigned char ae, unsigned char ctx,
925 enum icp_qat_uof_regtype reg_type,
926 unsigned short reg_num, unsigned int *data)
928 unsigned int savctx, uaddr, uwrd_lo, uwrd_hi;
929 unsigned int ctxarb_cntl, ustore_addr, ctx_enables;
930 unsigned short reg_addr;
934 reg_addr = qat_hal_get_reg_addr(reg_type, reg_num);
935 if (reg_addr == BAD_REGADDR) {
936 pr_err("QAT: bad regaddr=0x%x\n", reg_addr);
941 insts = 0xA070000000ull | (reg_addr & 0x3ff);
944 insts = (u64)0xA030000000ull | ((reg_addr & 0x3ff) << 10);
947 savctx = qat_hal_rd_ae_csr(handle, ae, ACTIVE_CTX_STATUS);
948 ctxarb_cntl = qat_hal_rd_ae_csr(handle, ae, CTX_ARB_CNTL);
949 ctx_enables = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
950 ctx_enables &= IGNORE_W1C_MASK;
951 if (ctx != (savctx & ACS_ACNO))
952 qat_hal_wr_ae_csr(handle, ae, ACTIVE_CTX_STATUS,
954 qat_hal_get_uwords(handle, ae, 0, 1, &savuword);
955 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx_enables);
956 ustore_addr = qat_hal_rd_ae_csr(handle, ae, USTORE_ADDRESS);
958 qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, uaddr);
959 insts = qat_hal_set_uword_ecc(insts);
960 uwrd_lo = (unsigned int)(insts & 0xffffffff);
961 uwrd_hi = (unsigned int)(insts >> 0x20);
962 qat_hal_wr_ae_csr(handle, ae, USTORE_DATA_LOWER, uwrd_lo);
963 qat_hal_wr_ae_csr(handle, ae, USTORE_DATA_UPPER, uwrd_hi);
964 qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, uaddr);
965 /* delay for at least 8 cycles */
966 qat_hal_wait_cycles(handle, ae, 0x8, 0);
969 * the instruction should have been executed
970 * prior to clearing the ECS in putUwords
972 *data = qat_hal_rd_ae_csr(handle, ae, ALU_OUT);
973 qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, ustore_addr);
974 qat_hal_wr_uwords(handle, ae, 0, 1, &savuword);
975 if (ctx != (savctx & ACS_ACNO))
976 qat_hal_wr_ae_csr(handle, ae, ACTIVE_CTX_STATUS,
978 qat_hal_wr_ae_csr(handle, ae, CTX_ARB_CNTL, ctxarb_cntl);
979 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx_enables);
984 static int qat_hal_wr_rel_reg(struct icp_qat_fw_loader_handle *handle,
985 unsigned char ae, unsigned char ctx,
986 enum icp_qat_uof_regtype reg_type,
987 unsigned short reg_num, unsigned int data)
989 unsigned short src_hiaddr, src_lowaddr, dest_addr, data16hi, data16lo;
996 const int num_inst = ARRAY_SIZE(insts), code_off = 1;
997 const int imm_w1 = 0, imm_w0 = 1;
999 dest_addr = qat_hal_get_reg_addr(reg_type, reg_num);
1000 if (dest_addr == BAD_REGADDR) {
1001 pr_err("QAT: bad destAddr=0x%x\n", dest_addr);
1005 data16lo = 0xffff & data;
1006 data16hi = 0xffff & (data >> 0x10);
1007 src_hiaddr = qat_hal_get_reg_addr(ICP_NO_DEST, (unsigned short)
1009 src_lowaddr = qat_hal_get_reg_addr(ICP_NO_DEST, (unsigned short)
1013 insts[imm_w1] = insts[imm_w1] | ((data16hi >> 8) << 20) |
1014 ((src_hiaddr & 0x3ff) << 10) | (dest_addr & 0x3ff);
1015 insts[imm_w0] = insts[imm_w0] | ((data16lo >> 8) << 20) |
1016 ((src_lowaddr & 0x3ff) << 10) | (dest_addr & 0x3ff);
1019 insts[imm_w1] = insts[imm_w1] | ((data16hi >> 8) << 20) |
1020 ((dest_addr & 0x3ff) << 10) | (src_hiaddr & 0x3ff);
1022 insts[imm_w0] = insts[imm_w0] | ((data16lo >> 8) << 20) |
1023 ((dest_addr & 0x3ff) << 10) | (src_lowaddr & 0x3ff);
1027 return qat_hal_exec_micro_inst(handle, ae, ctx, insts, num_inst,
1028 code_off, num_inst * 0x5, NULL);
1031 int qat_hal_get_ins_num(void)
1033 return ARRAY_SIZE(inst_4b);
1036 static int qat_hal_concat_micro_code(u64 *micro_inst,
1037 unsigned int inst_num, unsigned int size,
1038 unsigned int addr, unsigned int *value)
1041 unsigned int cur_value;
1042 const u64 *inst_arr;
1047 orig_num = inst_num;
1048 cur_value = value[0];
1050 usize = ARRAY_SIZE(inst_4b);
1051 fixup_offset = inst_num;
1052 for (i = 0; i < usize; i++)
1053 micro_inst[inst_num++] = inst_arr[i];
1054 INSERT_IMMED_GPRA_CONST(micro_inst[fixup_offset], (addr));
1056 INSERT_IMMED_GPRA_CONST(micro_inst[fixup_offset], 0);
1058 INSERT_IMMED_GPRB_CONST(micro_inst[fixup_offset], (cur_value >> 0));
1060 INSERT_IMMED_GPRB_CONST(micro_inst[fixup_offset], (cur_value >> 0x10));
1062 return inst_num - orig_num;
1065 static int qat_hal_exec_micro_init_lm(struct icp_qat_fw_loader_handle *handle,
1066 unsigned char ae, unsigned char ctx,
1067 int *pfirst_exec, u64 *micro_inst,
1068 unsigned int inst_num)
1071 unsigned int gpra0 = 0, gpra1 = 0, gpra2 = 0;
1072 unsigned int gprb0 = 0, gprb1 = 0;
1075 qat_hal_rd_rel_reg(handle, ae, ctx, ICP_GPA_REL, 0, &gpra0);
1076 qat_hal_rd_rel_reg(handle, ae, ctx, ICP_GPA_REL, 0x1, &gpra1);
1077 qat_hal_rd_rel_reg(handle, ae, ctx, ICP_GPA_REL, 0x2, &gpra2);
1078 qat_hal_rd_rel_reg(handle, ae, ctx, ICP_GPB_REL, 0, &gprb0);
1079 qat_hal_rd_rel_reg(handle, ae, ctx, ICP_GPB_REL, 0x1, &gprb1);
1082 stat = qat_hal_exec_micro_inst(handle, ae, ctx, micro_inst, inst_num, 1,
1083 inst_num * 0x5, NULL);
1086 qat_hal_wr_rel_reg(handle, ae, ctx, ICP_GPA_REL, 0, gpra0);
1087 qat_hal_wr_rel_reg(handle, ae, ctx, ICP_GPA_REL, 0x1, gpra1);
1088 qat_hal_wr_rel_reg(handle, ae, ctx, ICP_GPA_REL, 0x2, gpra2);
1089 qat_hal_wr_rel_reg(handle, ae, ctx, ICP_GPB_REL, 0, gprb0);
1090 qat_hal_wr_rel_reg(handle, ae, ctx, ICP_GPB_REL, 0x1, gprb1);
1095 int qat_hal_batch_wr_lm(struct icp_qat_fw_loader_handle *handle,
1097 struct icp_qat_uof_batch_init *lm_init_header)
1099 struct icp_qat_uof_batch_init *plm_init;
1100 u64 *micro_inst_arry;
1102 int alloc_inst_size;
1106 plm_init = lm_init_header->next;
1107 alloc_inst_size = lm_init_header->size;
1108 if ((unsigned int)alloc_inst_size > handle->hal_handle->max_ustore)
1109 alloc_inst_size = handle->hal_handle->max_ustore;
1110 micro_inst_arry = kmalloc_array(alloc_inst_size, sizeof(u64),
1112 if (!micro_inst_arry)
1116 unsigned int addr, *value, size;
1119 addr = plm_init->addr;
1120 value = plm_init->value;
1121 size = plm_init->size;
1122 micro_inst_num += qat_hal_concat_micro_code(micro_inst_arry,
1125 plm_init = plm_init->next;
1127 /* exec micro codes */
1128 if (micro_inst_arry && (micro_inst_num > 0)) {
1129 micro_inst_arry[micro_inst_num++] = 0x0E000010000ull;
1130 stat = qat_hal_exec_micro_init_lm(handle, ae, 0, &first_exec,
1134 kfree(micro_inst_arry);
1138 static int qat_hal_put_rel_rd_xfer(struct icp_qat_fw_loader_handle *handle,
1139 unsigned char ae, unsigned char ctx,
1140 enum icp_qat_uof_regtype reg_type,
1141 unsigned short reg_num, unsigned int val)
1144 unsigned int reg_addr;
1145 unsigned int ctx_enables;
1146 unsigned short mask;
1147 unsigned short dr_offset = 0x10;
1149 ctx_enables = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
1150 if (CE_INUSE_CONTEXTS & ctx_enables) {
1152 pr_err("QAT: bad 4-ctx mode,ctx=0x%x\n", ctx);
1160 if (reg_num & ~mask)
1162 reg_addr = reg_num + (ctx << 0x5);
1166 SET_AE_XFER(handle, ae, reg_addr, val);
1170 SET_AE_XFER(handle, ae, (reg_addr + dr_offset), val);
1179 static int qat_hal_put_rel_wr_xfer(struct icp_qat_fw_loader_handle *handle,
1180 unsigned char ae, unsigned char ctx,
1181 enum icp_qat_uof_regtype reg_type,
1182 unsigned short reg_num, unsigned int data)
1184 unsigned int gprval, ctx_enables;
1185 unsigned short src_hiaddr, src_lowaddr, gpr_addr, xfr_addr, data16hi,
1187 unsigned short reg_mask;
1189 u64 micro_inst[] = {
1196 const int num_inst = ARRAY_SIZE(micro_inst), code_off = 1;
1197 const unsigned short gprnum = 0, dly = num_inst * 0x5;
1199 ctx_enables = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
1200 if (CE_INUSE_CONTEXTS & ctx_enables) {
1202 pr_err("QAT: 4-ctx mode,ctx=0x%x\n", ctx);
1205 reg_mask = (unsigned short)~0x1f;
1207 reg_mask = (unsigned short)~0xf;
1209 if (reg_num & reg_mask)
1211 xfr_addr = qat_hal_get_reg_addr(reg_type, reg_num);
1212 if (xfr_addr == BAD_REGADDR) {
1213 pr_err("QAT: bad xfrAddr=0x%x\n", xfr_addr);
1216 status = qat_hal_rd_rel_reg(handle, ae, ctx, ICP_GPB_REL, gprnum, &gprval);
1218 pr_err("QAT: failed to read register");
1221 gpr_addr = qat_hal_get_reg_addr(ICP_GPB_REL, gprnum);
1222 data16low = 0xffff & data;
1223 data16hi = 0xffff & (data >> 0x10);
1224 src_hiaddr = qat_hal_get_reg_addr(ICP_NO_DEST,
1225 (unsigned short)(0xff & data16hi));
1226 src_lowaddr = qat_hal_get_reg_addr(ICP_NO_DEST,
1227 (unsigned short)(0xff & data16low));
1228 micro_inst[0] = micro_inst[0x0] | ((data16hi >> 8) << 20) |
1229 ((gpr_addr & 0x3ff) << 10) | (src_hiaddr & 0x3ff);
1230 micro_inst[1] = micro_inst[0x1] | ((data16low >> 8) << 20) |
1231 ((gpr_addr & 0x3ff) << 10) | (src_lowaddr & 0x3ff);
1232 micro_inst[0x2] = micro_inst[0x2] |
1233 ((xfr_addr & 0x3ff) << 20) | ((gpr_addr & 0x3ff) << 10);
1234 status = qat_hal_exec_micro_inst(handle, ae, ctx, micro_inst, num_inst,
1235 code_off, dly, NULL);
1236 qat_hal_wr_rel_reg(handle, ae, ctx, ICP_GPB_REL, gprnum, gprval);
1240 static int qat_hal_put_rel_nn(struct icp_qat_fw_loader_handle *handle,
1241 unsigned char ae, unsigned char ctx,
1242 unsigned short nn, unsigned int val)
1244 unsigned int ctx_enables;
1247 ctx_enables = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
1248 ctx_enables &= IGNORE_W1C_MASK;
1249 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx_enables | CE_NN_MODE);
1251 stat = qat_hal_put_rel_wr_xfer(handle, ae, ctx, ICP_NEIGH_REL, nn, val);
1252 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx_enables);
1256 static int qat_hal_convert_abs_to_rel(struct icp_qat_fw_loader_handle
1257 *handle, unsigned char ae,
1258 unsigned short absreg_num,
1259 unsigned short *relreg,
1262 unsigned int ctx_enables;
1264 ctx_enables = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
1265 if (ctx_enables & CE_INUSE_CONTEXTS) {
1267 *relreg = absreg_num & 0x1F;
1268 *ctx = (absreg_num >> 0x4) & 0x6;
1271 *relreg = absreg_num & 0x0F;
1272 *ctx = (absreg_num >> 0x4) & 0x7;
1277 int qat_hal_init_gpr(struct icp_qat_fw_loader_handle *handle,
1278 unsigned char ae, unsigned char ctx_mask,
1279 enum icp_qat_uof_regtype reg_type,
1280 unsigned short reg_num, unsigned int regdata)
1284 unsigned char ctx = 0;
1285 enum icp_qat_uof_regtype type;
1287 if (reg_num >= ICP_QAT_UCLO_MAX_GPR_REG)
1291 if (ctx_mask == 0) {
1292 qat_hal_convert_abs_to_rel(handle, ae, reg_num, ®,
1294 type = reg_type - 1;
1298 if (!test_bit(ctx, (unsigned long *)&ctx_mask))
1301 stat = qat_hal_wr_rel_reg(handle, ae, ctx, type, reg, regdata);
1303 pr_err("QAT: write gpr fail\n");
1306 } while (ctx_mask && (ctx++ < ICP_QAT_UCLO_MAX_CTX));
1311 int qat_hal_init_wr_xfer(struct icp_qat_fw_loader_handle *handle,
1312 unsigned char ae, unsigned char ctx_mask,
1313 enum icp_qat_uof_regtype reg_type,
1314 unsigned short reg_num, unsigned int regdata)
1318 unsigned char ctx = 0;
1319 enum icp_qat_uof_regtype type;
1321 if (reg_num >= ICP_QAT_UCLO_MAX_XFER_REG)
1325 if (ctx_mask == 0) {
1326 qat_hal_convert_abs_to_rel(handle, ae, reg_num, ®,
1328 type = reg_type - 3;
1332 if (!test_bit(ctx, (unsigned long *)&ctx_mask))
1335 stat = qat_hal_put_rel_wr_xfer(handle, ae, ctx, type, reg,
1338 pr_err("QAT: write wr xfer fail\n");
1341 } while (ctx_mask && (ctx++ < ICP_QAT_UCLO_MAX_CTX));
1346 int qat_hal_init_rd_xfer(struct icp_qat_fw_loader_handle *handle,
1347 unsigned char ae, unsigned char ctx_mask,
1348 enum icp_qat_uof_regtype reg_type,
1349 unsigned short reg_num, unsigned int regdata)
1353 unsigned char ctx = 0;
1354 enum icp_qat_uof_regtype type;
1356 if (reg_num >= ICP_QAT_UCLO_MAX_XFER_REG)
1360 if (ctx_mask == 0) {
1361 qat_hal_convert_abs_to_rel(handle, ae, reg_num, ®,
1363 type = reg_type - 3;
1367 if (!test_bit(ctx, (unsigned long *)&ctx_mask))
1370 stat = qat_hal_put_rel_rd_xfer(handle, ae, ctx, type, reg,
1373 pr_err("QAT: write rd xfer fail\n");
1376 } while (ctx_mask && (ctx++ < ICP_QAT_UCLO_MAX_CTX));
1381 int qat_hal_init_nn(struct icp_qat_fw_loader_handle *handle,
1382 unsigned char ae, unsigned char ctx_mask,
1383 unsigned short reg_num, unsigned int regdata)
1391 for (ctx = 0; ctx < ICP_QAT_UCLO_MAX_CTX; ctx++) {
1392 if (!test_bit(ctx, (unsigned long *)&ctx_mask))
1394 stat = qat_hal_put_rel_nn(handle, ae, ctx, reg_num, regdata);
1396 pr_err("QAT: write neigh error\n");