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);
215 SET_BIT(csr, CE_LMADDR_2_GLOBAL_BITPOS) :
216 CLR_BIT(csr, CE_LMADDR_2_GLOBAL_BITPOS);
220 SET_BIT(csr, CE_LMADDR_3_GLOBAL_BITPOS) :
221 CLR_BIT(csr, CE_LMADDR_3_GLOBAL_BITPOS);
224 pr_err("QAT: lmType = 0x%x\n", lm_type);
229 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, new_csr);
233 void qat_hal_set_ae_tindex_mode(struct icp_qat_fw_loader_handle *handle,
234 unsigned char ae, unsigned char mode)
236 unsigned int csr, new_csr;
238 csr = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
239 csr &= IGNORE_W1C_MASK;
241 SET_BIT(csr, CE_T_INDEX_GLOBAL_BITPOS) :
242 CLR_BIT(csr, CE_T_INDEX_GLOBAL_BITPOS);
244 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, new_csr);
247 static unsigned short qat_hal_get_reg_addr(unsigned int type,
248 unsigned short reg_num)
250 unsigned short reg_addr;
255 reg_addr = 0x80 | (reg_num & 0x7f);
259 reg_addr = reg_num & 0x1f;
264 reg_addr = 0x180 | (reg_num & 0x1f);
267 reg_addr = 0x140 | ((reg_num & 0x3) << 1);
272 reg_addr = 0x1c0 | (reg_num & 0x1f);
275 reg_addr = 0x100 | ((reg_num & 0x3) << 1);
278 reg_addr = 0x280 | (reg_num & 0x1f);
293 reg_addr = 0x300 | (reg_num & 0xff);
296 reg_addr = BAD_REGADDR;
302 void qat_hal_reset(struct icp_qat_fw_loader_handle *handle)
304 unsigned int reset_mask = handle->chip_info->icp_rst_mask;
305 unsigned int reset_csr = handle->chip_info->icp_rst_csr;
306 unsigned int csr_val;
308 csr_val = GET_CAP_CSR(handle, reset_csr);
309 csr_val |= reset_mask;
310 SET_CAP_CSR(handle, reset_csr, csr_val);
313 static void qat_hal_wr_indr_csr(struct icp_qat_fw_loader_handle *handle,
314 unsigned char ae, unsigned int ctx_mask,
315 unsigned int ae_csr, unsigned int csr_val)
317 unsigned int ctx, cur_ctx;
319 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, ae_csr, csr_val);
328 qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, cur_ctx);
331 static unsigned int qat_hal_rd_indr_csr(struct icp_qat_fw_loader_handle *handle,
332 unsigned char ae, unsigned char ctx,
335 unsigned int cur_ctx, csr_val;
337 cur_ctx = qat_hal_rd_ae_csr(handle, ae, CSR_CTX_POINTER);
338 qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, ctx);
339 csr_val = qat_hal_rd_ae_csr(handle, ae, ae_csr);
340 qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, cur_ctx);
345 static void qat_hal_put_sig_event(struct icp_qat_fw_loader_handle *handle,
346 unsigned char ae, unsigned int ctx_mask,
349 unsigned int ctx, cur_ctx;
351 cur_ctx = qat_hal_rd_ae_csr(handle, ae, CSR_CTX_POINTER);
352 for (ctx = 0; ctx < ICP_QAT_UCLO_MAX_CTX; ctx++) {
353 if (!(ctx_mask & (1 << ctx)))
355 qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, ctx);
356 qat_hal_wr_ae_csr(handle, ae, CTX_SIG_EVENTS_INDIRECT, events);
358 qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, cur_ctx);
361 static void qat_hal_put_wakeup_event(struct icp_qat_fw_loader_handle *handle,
362 unsigned char ae, unsigned int ctx_mask,
365 unsigned int ctx, cur_ctx;
367 cur_ctx = qat_hal_rd_ae_csr(handle, ae, CSR_CTX_POINTER);
368 for (ctx = 0; ctx < ICP_QAT_UCLO_MAX_CTX; ctx++) {
369 if (!(ctx_mask & (1 << ctx)))
371 qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, ctx);
372 qat_hal_wr_ae_csr(handle, ae, CTX_WAKEUP_EVENTS_INDIRECT,
375 qat_hal_wr_ae_csr(handle, ae, CSR_CTX_POINTER, cur_ctx);
378 static int qat_hal_check_ae_alive(struct icp_qat_fw_loader_handle *handle)
380 unsigned long ae_mask = handle->hal_handle->ae_mask;
381 unsigned int base_cnt, cur_cnt;
383 int times = MAX_RETRY_TIMES;
385 for_each_set_bit(ae, &ae_mask, handle->hal_handle->ae_max_num) {
386 base_cnt = qat_hal_rd_ae_csr(handle, ae, PROFILE_COUNT);
390 cur_cnt = qat_hal_rd_ae_csr(handle, ae, PROFILE_COUNT);
392 } while (times-- && (cur_cnt == base_cnt));
395 pr_err("QAT: AE%d is inactive!!\n", ae);
403 int qat_hal_check_ae_active(struct icp_qat_fw_loader_handle *handle,
406 unsigned int enable = 0, active = 0;
408 enable = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
409 active = qat_hal_rd_ae_csr(handle, ae, ACTIVE_CTX_STATUS);
410 if ((enable & (0xff << CE_ENABLE_BITPOS)) ||
411 (active & (1 << ACS_ABO_BITPOS)))
417 static void qat_hal_reset_timestamp(struct icp_qat_fw_loader_handle *handle)
419 unsigned long ae_mask = handle->hal_handle->ae_mask;
420 unsigned int misc_ctl_csr, misc_ctl;
423 misc_ctl_csr = handle->chip_info->misc_ctl_csr;
424 /* stop the timestamp timers */
425 misc_ctl = GET_CAP_CSR(handle, misc_ctl_csr);
426 if (misc_ctl & MC_TIMESTAMP_ENABLE)
427 SET_CAP_CSR(handle, misc_ctl_csr, misc_ctl &
428 (~MC_TIMESTAMP_ENABLE));
430 for_each_set_bit(ae, &ae_mask, handle->hal_handle->ae_max_num) {
431 qat_hal_wr_ae_csr(handle, ae, TIMESTAMP_LOW, 0);
432 qat_hal_wr_ae_csr(handle, ae, TIMESTAMP_HIGH, 0);
434 /* start timestamp timers */
435 SET_CAP_CSR(handle, misc_ctl_csr, misc_ctl | MC_TIMESTAMP_ENABLE);
438 #define ESRAM_AUTO_TINIT BIT(2)
439 #define ESRAM_AUTO_TINIT_DONE BIT(3)
440 #define ESRAM_AUTO_INIT_USED_CYCLES (1640)
441 #define ESRAM_AUTO_INIT_CSR_OFFSET 0xC1C
442 static int qat_hal_init_esram(struct icp_qat_fw_loader_handle *handle)
444 void __iomem *csr_addr =
445 (void __iomem *)((uintptr_t)handle->hal_ep_csr_addr_v +
446 ESRAM_AUTO_INIT_CSR_OFFSET);
447 unsigned int csr_val;
450 if (handle->pci_dev->device != PCI_DEVICE_ID_INTEL_QAT_DH895XCC)
453 csr_val = ADF_CSR_RD(csr_addr, 0);
454 if ((csr_val & ESRAM_AUTO_TINIT) && (csr_val & ESRAM_AUTO_TINIT_DONE))
457 csr_val = ADF_CSR_RD(csr_addr, 0);
458 csr_val |= ESRAM_AUTO_TINIT;
459 ADF_CSR_WR(csr_addr, 0, csr_val);
462 qat_hal_wait_cycles(handle, 0, ESRAM_AUTO_INIT_USED_CYCLES, 0);
463 csr_val = ADF_CSR_RD(csr_addr, 0);
464 } while (!(csr_val & ESRAM_AUTO_TINIT_DONE) && times--);
466 pr_err("QAT: Fail to init eSram!\n");
472 #define SHRAM_INIT_CYCLES 2060
473 int qat_hal_clr_reset(struct icp_qat_fw_loader_handle *handle)
475 unsigned int clk_csr = handle->chip_info->glb_clk_enable_csr;
476 unsigned int reset_mask = handle->chip_info->icp_rst_mask;
477 unsigned int reset_csr = handle->chip_info->icp_rst_csr;
478 unsigned long ae_mask = handle->hal_handle->ae_mask;
479 unsigned char ae = 0;
480 unsigned int times = 100;
481 unsigned int csr_val;
483 /* write to the reset csr */
484 csr_val = GET_CAP_CSR(handle, reset_csr);
485 csr_val &= ~reset_mask;
487 SET_CAP_CSR(handle, reset_csr, csr_val);
490 csr_val = GET_CAP_CSR(handle, reset_csr);
491 csr_val &= reset_mask;
494 csr_val = GET_CAP_CSR(handle, clk_csr);
495 csr_val |= reset_mask;
496 SET_CAP_CSR(handle, clk_csr, csr_val);
497 if (qat_hal_check_ae_alive(handle))
500 /* Set undefined power-up/reset states to reasonable default values */
501 for_each_set_bit(ae, &ae_mask, handle->hal_handle->ae_max_num) {
502 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES,
503 INIT_CTX_ENABLE_VALUE);
504 qat_hal_wr_indr_csr(handle, ae, ICP_QAT_UCLO_AE_ALL_CTX,
506 handle->hal_handle->upc_mask &
508 qat_hal_wr_ae_csr(handle, ae, CTX_ARB_CNTL, INIT_CTX_ARB_VALUE);
509 qat_hal_wr_ae_csr(handle, ae, CC_ENABLE, INIT_CCENABLE_VALUE);
510 qat_hal_put_wakeup_event(handle, ae,
511 ICP_QAT_UCLO_AE_ALL_CTX,
512 INIT_WAKEUP_EVENTS_VALUE);
513 qat_hal_put_sig_event(handle, ae,
514 ICP_QAT_UCLO_AE_ALL_CTX,
515 INIT_SIG_EVENTS_VALUE);
517 if (qat_hal_init_esram(handle))
519 if (qat_hal_wait_cycles(handle, 0, SHRAM_INIT_CYCLES, 0))
521 qat_hal_reset_timestamp(handle);
525 pr_err("QAT: failed to get device out of reset\n");
529 static void qat_hal_disable_ctx(struct icp_qat_fw_loader_handle *handle,
530 unsigned char ae, unsigned int ctx_mask)
534 ctx = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
535 ctx &= IGNORE_W1C_MASK &
536 (~((ctx_mask & ICP_QAT_UCLO_AE_ALL_CTX) << CE_ENABLE_BITPOS));
537 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx);
540 static u64 qat_hal_parity_64bit(u64 word)
551 static u64 qat_hal_set_uword_ecc(u64 uword)
553 u64 bit0_mask = 0xff800007fffULL, bit1_mask = 0x1f801ff801fULL,
554 bit2_mask = 0xe387e0781e1ULL, bit3_mask = 0x7cb8e388e22ULL,
555 bit4_mask = 0xaf5b2c93244ULL, bit5_mask = 0xf56d5525488ULL,
556 bit6_mask = 0xdaf69a46910ULL;
558 /* clear the ecc bits */
559 uword &= ~(0x7fULL << 0x2C);
560 uword |= qat_hal_parity_64bit(bit0_mask & uword) << 0x2C;
561 uword |= qat_hal_parity_64bit(bit1_mask & uword) << 0x2D;
562 uword |= qat_hal_parity_64bit(bit2_mask & uword) << 0x2E;
563 uword |= qat_hal_parity_64bit(bit3_mask & uword) << 0x2F;
564 uword |= qat_hal_parity_64bit(bit4_mask & uword) << 0x30;
565 uword |= qat_hal_parity_64bit(bit5_mask & uword) << 0x31;
566 uword |= qat_hal_parity_64bit(bit6_mask & uword) << 0x32;
570 void qat_hal_wr_uwords(struct icp_qat_fw_loader_handle *handle,
571 unsigned char ae, unsigned int uaddr,
572 unsigned int words_num, u64 *uword)
574 unsigned int ustore_addr;
577 ustore_addr = qat_hal_rd_ae_csr(handle, ae, USTORE_ADDRESS);
579 qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, uaddr);
580 for (i = 0; i < words_num; i++) {
581 unsigned int uwrd_lo, uwrd_hi;
584 tmp = qat_hal_set_uword_ecc(uword[i]);
585 uwrd_lo = (unsigned int)(tmp & 0xffffffff);
586 uwrd_hi = (unsigned int)(tmp >> 0x20);
587 qat_hal_wr_ae_csr(handle, ae, USTORE_DATA_LOWER, uwrd_lo);
588 qat_hal_wr_ae_csr(handle, ae, USTORE_DATA_UPPER, uwrd_hi);
590 qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, ustore_addr);
593 static void qat_hal_enable_ctx(struct icp_qat_fw_loader_handle *handle,
594 unsigned char ae, unsigned int ctx_mask)
598 ctx = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
599 ctx &= IGNORE_W1C_MASK;
600 ctx_mask &= (ctx & CE_INUSE_CONTEXTS) ? 0x55 : 0xFF;
601 ctx |= (ctx_mask << CE_ENABLE_BITPOS);
602 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx);
605 static void qat_hal_clear_xfer(struct icp_qat_fw_loader_handle *handle)
607 unsigned long ae_mask = handle->hal_handle->ae_mask;
611 for_each_set_bit(ae, &ae_mask, handle->hal_handle->ae_max_num) {
612 for (reg = 0; reg < ICP_QAT_UCLO_MAX_GPR_REG; reg++) {
613 qat_hal_init_rd_xfer(handle, ae, 0, ICP_SR_RD_ABS,
615 qat_hal_init_rd_xfer(handle, ae, 0, ICP_DR_RD_ABS,
621 static int qat_hal_clear_gpr(struct icp_qat_fw_loader_handle *handle)
623 unsigned long ae_mask = handle->hal_handle->ae_mask;
625 unsigned int ctx_mask = ICP_QAT_UCLO_AE_ALL_CTX;
626 int times = MAX_RETRY_TIMES;
627 unsigned int csr_val = 0;
628 unsigned int savctx = 0;
631 for_each_set_bit(ae, &ae_mask, handle->hal_handle->ae_max_num) {
632 csr_val = qat_hal_rd_ae_csr(handle, ae, AE_MISC_CONTROL);
633 csr_val &= ~(1 << MMC_SHARE_CS_BITPOS);
634 qat_hal_wr_ae_csr(handle, ae, AE_MISC_CONTROL, csr_val);
635 csr_val = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
636 csr_val &= IGNORE_W1C_MASK;
637 if (handle->chip_info->nn)
638 csr_val |= CE_NN_MODE;
640 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, csr_val);
641 qat_hal_wr_uwords(handle, ae, 0, ARRAY_SIZE(inst),
643 qat_hal_wr_indr_csr(handle, ae, ctx_mask, CTX_STS_INDIRECT,
644 handle->hal_handle->upc_mask &
646 savctx = qat_hal_rd_ae_csr(handle, ae, ACTIVE_CTX_STATUS);
647 qat_hal_wr_ae_csr(handle, ae, ACTIVE_CTX_STATUS, 0);
648 qat_hal_put_wakeup_event(handle, ae, ctx_mask, XCWE_VOLUNTARY);
649 qat_hal_wr_indr_csr(handle, ae, ctx_mask,
650 CTX_SIG_EVENTS_INDIRECT, 0);
651 qat_hal_wr_ae_csr(handle, ae, CTX_SIG_EVENTS_ACTIVE, 0);
652 qat_hal_enable_ctx(handle, ae, ctx_mask);
654 for_each_set_bit(ae, &ae_mask, handle->hal_handle->ae_max_num) {
655 /* wait for AE to finish */
657 ret = qat_hal_wait_cycles(handle, ae, 20, 1);
658 } while (ret && times--);
661 pr_err("QAT: clear GPR of AE %d failed", ae);
664 qat_hal_disable_ctx(handle, ae, ctx_mask);
665 qat_hal_wr_ae_csr(handle, ae, ACTIVE_CTX_STATUS,
667 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES,
668 INIT_CTX_ENABLE_VALUE);
669 qat_hal_wr_indr_csr(handle, ae, ctx_mask, CTX_STS_INDIRECT,
670 handle->hal_handle->upc_mask &
672 qat_hal_wr_ae_csr(handle, ae, CTX_ARB_CNTL, INIT_CTX_ARB_VALUE);
673 qat_hal_wr_ae_csr(handle, ae, CC_ENABLE, INIT_CCENABLE_VALUE);
674 qat_hal_put_wakeup_event(handle, ae, ctx_mask,
675 INIT_WAKEUP_EVENTS_VALUE);
676 qat_hal_put_sig_event(handle, ae, ctx_mask,
677 INIT_SIG_EVENTS_VALUE);
682 static int qat_hal_chip_init(struct icp_qat_fw_loader_handle *handle,
683 struct adf_accel_dev *accel_dev)
685 struct adf_accel_pci *pci_info = &accel_dev->accel_pci_dev;
686 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
687 struct adf_bar *misc_bar =
688 &pci_info->pci_bars[hw_data->get_misc_bar_id(hw_data)];
689 unsigned int max_en_ae_id = 0;
690 struct adf_bar *sram_bar;
691 unsigned int csr_val = 0;
692 unsigned long ae_mask;
693 unsigned char ae = 0;
696 handle->pci_dev = pci_info->pci_dev;
697 switch (handle->pci_dev->device) {
698 case ADF_4XXX_PCI_DEVICE_ID:
699 handle->chip_info->mmp_sram_size = 0;
700 handle->chip_info->nn = false;
701 handle->chip_info->lm2lm3 = true;
702 handle->chip_info->lm_size = ICP_QAT_UCLO_MAX_LMEM_REG_2X;
703 handle->chip_info->icp_rst_csr = ICP_RESET_CPP0;
704 handle->chip_info->icp_rst_mask = 0x100015;
705 handle->chip_info->glb_clk_enable_csr = ICP_GLOBAL_CLK_ENABLE_CPP0;
706 handle->chip_info->misc_ctl_csr = MISC_CONTROL_C4XXX;
707 handle->chip_info->wakeup_event_val = 0x80000000;
708 handle->chip_info->fw_auth = true;
709 handle->chip_info->css_3k = true;
710 handle->chip_info->tgroup_share_ustore = true;
711 handle->chip_info->fcu_ctl_csr = FCU_CONTROL_4XXX;
712 handle->chip_info->fcu_sts_csr = FCU_STATUS_4XXX;
713 handle->chip_info->fcu_dram_addr_hi = FCU_DRAM_ADDR_HI_4XXX;
714 handle->chip_info->fcu_dram_addr_lo = FCU_DRAM_ADDR_LO_4XXX;
715 handle->chip_info->fcu_loaded_ae_csr = FCU_AE_LOADED_4XXX;
716 handle->chip_info->fcu_loaded_ae_pos = 0;
718 handle->hal_cap_g_ctl_csr_addr_v =
719 (void __iomem *)((uintptr_t)misc_bar->virt_addr +
720 ICP_QAT_CAP_OFFSET_4XXX);
721 handle->hal_cap_ae_xfer_csr_addr_v =
722 (void __iomem *)((uintptr_t)misc_bar->virt_addr +
723 ICP_QAT_AE_OFFSET_4XXX);
724 handle->hal_ep_csr_addr_v =
725 (void __iomem *)((uintptr_t)misc_bar->virt_addr +
726 ICP_QAT_EP_OFFSET_4XXX);
727 handle->hal_cap_ae_local_csr_addr_v =
728 (void __iomem *)((uintptr_t)handle->hal_cap_ae_xfer_csr_addr_v
729 + LOCAL_TO_XFER_REG_OFFSET);
731 case PCI_DEVICE_ID_INTEL_QAT_C62X:
732 case PCI_DEVICE_ID_INTEL_QAT_C3XXX:
733 handle->chip_info->mmp_sram_size = 0;
734 handle->chip_info->nn = true;
735 handle->chip_info->lm2lm3 = false;
736 handle->chip_info->lm_size = ICP_QAT_UCLO_MAX_LMEM_REG;
737 handle->chip_info->icp_rst_csr = ICP_RESET;
738 handle->chip_info->icp_rst_mask = (hw_data->ae_mask << RST_CSR_AE_LSB) |
739 (hw_data->accel_mask << RST_CSR_QAT_LSB);
740 handle->chip_info->glb_clk_enable_csr = ICP_GLOBAL_CLK_ENABLE;
741 handle->chip_info->misc_ctl_csr = MISC_CONTROL;
742 handle->chip_info->wakeup_event_val = WAKEUP_EVENT;
743 handle->chip_info->fw_auth = true;
744 handle->chip_info->css_3k = false;
745 handle->chip_info->tgroup_share_ustore = false;
746 handle->chip_info->fcu_ctl_csr = FCU_CONTROL;
747 handle->chip_info->fcu_sts_csr = FCU_STATUS;
748 handle->chip_info->fcu_dram_addr_hi = FCU_DRAM_ADDR_HI;
749 handle->chip_info->fcu_dram_addr_lo = FCU_DRAM_ADDR_LO;
750 handle->chip_info->fcu_loaded_ae_csr = FCU_STATUS;
751 handle->chip_info->fcu_loaded_ae_pos = FCU_LOADED_AE_POS;
752 handle->hal_cap_g_ctl_csr_addr_v =
753 (void __iomem *)((uintptr_t)misc_bar->virt_addr +
755 handle->hal_cap_ae_xfer_csr_addr_v =
756 (void __iomem *)((uintptr_t)misc_bar->virt_addr +
758 handle->hal_ep_csr_addr_v =
759 (void __iomem *)((uintptr_t)misc_bar->virt_addr +
761 handle->hal_cap_ae_local_csr_addr_v =
762 (void __iomem *)((uintptr_t)handle->hal_cap_ae_xfer_csr_addr_v
763 + LOCAL_TO_XFER_REG_OFFSET);
765 case PCI_DEVICE_ID_INTEL_QAT_DH895XCC:
766 handle->chip_info->mmp_sram_size = 0x40000;
767 handle->chip_info->nn = true;
768 handle->chip_info->lm2lm3 = false;
769 handle->chip_info->lm_size = ICP_QAT_UCLO_MAX_LMEM_REG;
770 handle->chip_info->icp_rst_csr = ICP_RESET;
771 handle->chip_info->icp_rst_mask = (hw_data->ae_mask << RST_CSR_AE_LSB) |
772 (hw_data->accel_mask << RST_CSR_QAT_LSB);
773 handle->chip_info->glb_clk_enable_csr = ICP_GLOBAL_CLK_ENABLE;
774 handle->chip_info->misc_ctl_csr = MISC_CONTROL;
775 handle->chip_info->wakeup_event_val = WAKEUP_EVENT;
776 handle->chip_info->fw_auth = false;
777 handle->chip_info->css_3k = false;
778 handle->chip_info->tgroup_share_ustore = false;
779 handle->chip_info->fcu_ctl_csr = 0;
780 handle->chip_info->fcu_sts_csr = 0;
781 handle->chip_info->fcu_dram_addr_hi = 0;
782 handle->chip_info->fcu_dram_addr_lo = 0;
783 handle->chip_info->fcu_loaded_ae_csr = 0;
784 handle->chip_info->fcu_loaded_ae_pos = 0;
785 handle->hal_cap_g_ctl_csr_addr_v =
786 (void __iomem *)((uintptr_t)misc_bar->virt_addr +
788 handle->hal_cap_ae_xfer_csr_addr_v =
789 (void __iomem *)((uintptr_t)misc_bar->virt_addr +
791 handle->hal_ep_csr_addr_v =
792 (void __iomem *)((uintptr_t)misc_bar->virt_addr +
794 handle->hal_cap_ae_local_csr_addr_v =
795 (void __iomem *)((uintptr_t)handle->hal_cap_ae_xfer_csr_addr_v
796 + LOCAL_TO_XFER_REG_OFFSET);
803 if (handle->chip_info->mmp_sram_size > 0) {
805 &pci_info->pci_bars[hw_data->get_sram_bar_id(hw_data)];
806 handle->hal_sram_addr_v = sram_bar->virt_addr;
808 handle->hal_handle->revision_id = accel_dev->accel_pci_dev.revid;
809 handle->hal_handle->ae_mask = hw_data->ae_mask;
810 handle->hal_handle->admin_ae_mask = hw_data->admin_ae_mask;
811 handle->hal_handle->slice_mask = hw_data->accel_mask;
812 handle->cfg_ae_mask = ALL_AE_MASK;
813 /* create AE objects */
814 handle->hal_handle->upc_mask = 0x1ffff;
815 handle->hal_handle->max_ustore = 0x4000;
817 ae_mask = handle->hal_handle->ae_mask;
818 for_each_set_bit(ae, &ae_mask, ICP_QAT_UCLO_MAX_AE) {
819 handle->hal_handle->aes[ae].free_addr = 0;
820 handle->hal_handle->aes[ae].free_size =
821 handle->hal_handle->max_ustore;
822 handle->hal_handle->aes[ae].ustore_size =
823 handle->hal_handle->max_ustore;
824 handle->hal_handle->aes[ae].live_ctx_mask =
825 ICP_QAT_UCLO_AE_ALL_CTX;
828 handle->hal_handle->ae_max_num = max_en_ae_id + 1;
830 /* Set SIGNATURE_ENABLE[0] to 0x1 in order to enable ALU_OUT csr */
831 for_each_set_bit(ae, &ae_mask, handle->hal_handle->ae_max_num) {
832 csr_val = qat_hal_rd_ae_csr(handle, ae, SIGNATURE_ENABLE);
834 qat_hal_wr_ae_csr(handle, ae, SIGNATURE_ENABLE, csr_val);
840 int qat_hal_init(struct adf_accel_dev *accel_dev)
842 struct icp_qat_fw_loader_handle *handle;
845 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
849 handle->hal_handle = kzalloc(sizeof(*handle->hal_handle), GFP_KERNEL);
850 if (!handle->hal_handle) {
855 handle->chip_info = kzalloc(sizeof(*handle->chip_info), GFP_KERNEL);
856 if (!handle->chip_info) {
861 ret = qat_hal_chip_init(handle, accel_dev);
863 dev_err(&GET_DEV(accel_dev), "qat_hal_chip_init error\n");
867 /* take all AEs out of reset */
868 ret = qat_hal_clr_reset(handle);
870 dev_err(&GET_DEV(accel_dev), "qat_hal_clr_reset error\n");
874 qat_hal_clear_xfer(handle);
875 if (!handle->chip_info->fw_auth) {
876 ret = qat_hal_clear_gpr(handle);
881 accel_dev->fw_loader->fw_loader = handle;
885 kfree(handle->chip_info);
887 kfree(handle->hal_handle);
893 void qat_hal_deinit(struct icp_qat_fw_loader_handle *handle)
897 kfree(handle->chip_info);
898 kfree(handle->hal_handle);
902 int qat_hal_start(struct icp_qat_fw_loader_handle *handle)
904 unsigned long ae_mask = handle->hal_handle->ae_mask;
905 u32 wakeup_val = handle->chip_info->wakeup_event_val;
906 u32 fcu_ctl_csr, fcu_sts_csr;
907 unsigned int fcu_sts;
912 if (handle->chip_info->fw_auth) {
913 fcu_ctl_csr = handle->chip_info->fcu_ctl_csr;
914 fcu_sts_csr = handle->chip_info->fcu_sts_csr;
915 ae_ctr = hweight32(ae_mask);
916 SET_CAP_CSR(handle, fcu_ctl_csr, FCU_CTRL_CMD_START);
918 msleep(FW_AUTH_WAIT_PERIOD);
919 fcu_sts = GET_CAP_CSR(handle, fcu_sts_csr);
920 if (((fcu_sts >> FCU_STS_DONE_POS) & 0x1))
922 } while (retry++ < FW_AUTH_MAX_RETRY);
923 pr_err("QAT: start error (FCU_STS = 0x%x)\n", fcu_sts);
926 for_each_set_bit(ae, &ae_mask, handle->hal_handle->ae_max_num) {
927 qat_hal_put_wakeup_event(handle, ae, 0, wakeup_val);
928 qat_hal_enable_ctx(handle, ae, ICP_QAT_UCLO_AE_ALL_CTX);
935 void qat_hal_stop(struct icp_qat_fw_loader_handle *handle, unsigned char ae,
936 unsigned int ctx_mask)
938 if (!handle->chip_info->fw_auth)
939 qat_hal_disable_ctx(handle, ae, ctx_mask);
942 void qat_hal_set_pc(struct icp_qat_fw_loader_handle *handle,
943 unsigned char ae, unsigned int ctx_mask, unsigned int upc)
945 qat_hal_wr_indr_csr(handle, ae, ctx_mask, CTX_STS_INDIRECT,
946 handle->hal_handle->upc_mask & upc);
949 static void qat_hal_get_uwords(struct icp_qat_fw_loader_handle *handle,
950 unsigned char ae, unsigned int uaddr,
951 unsigned int words_num, u64 *uword)
953 unsigned int i, uwrd_lo, uwrd_hi;
954 unsigned int ustore_addr, misc_control;
956 misc_control = qat_hal_rd_ae_csr(handle, ae, AE_MISC_CONTROL);
957 qat_hal_wr_ae_csr(handle, ae, AE_MISC_CONTROL,
958 misc_control & 0xfffffffb);
959 ustore_addr = qat_hal_rd_ae_csr(handle, ae, USTORE_ADDRESS);
961 for (i = 0; i < words_num; i++) {
962 qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, uaddr);
964 uwrd_lo = qat_hal_rd_ae_csr(handle, ae, USTORE_DATA_LOWER);
965 uwrd_hi = qat_hal_rd_ae_csr(handle, ae, USTORE_DATA_UPPER);
967 uword[i] = (uword[i] << 0x20) | uwrd_lo;
969 qat_hal_wr_ae_csr(handle, ae, AE_MISC_CONTROL, misc_control);
970 qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, ustore_addr);
973 void qat_hal_wr_umem(struct icp_qat_fw_loader_handle *handle,
974 unsigned char ae, unsigned int uaddr,
975 unsigned int words_num, unsigned int *data)
977 unsigned int i, ustore_addr;
979 ustore_addr = qat_hal_rd_ae_csr(handle, ae, USTORE_ADDRESS);
981 qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, uaddr);
982 for (i = 0; i < words_num; i++) {
983 unsigned int uwrd_lo, uwrd_hi, tmp;
985 uwrd_lo = ((data[i] & 0xfff0000) << 4) | (0x3 << 18) |
986 ((data[i] & 0xff00) << 2) |
987 (0x3 << 8) | (data[i] & 0xff);
988 uwrd_hi = (0xf << 4) | ((data[i] & 0xf0000000) >> 28);
989 uwrd_hi |= (hweight32(data[i] & 0xffff) & 0x1) << 8;
990 tmp = ((data[i] >> 0x10) & 0xffff);
991 uwrd_hi |= (hweight32(tmp) & 0x1) << 9;
992 qat_hal_wr_ae_csr(handle, ae, USTORE_DATA_LOWER, uwrd_lo);
993 qat_hal_wr_ae_csr(handle, ae, USTORE_DATA_UPPER, uwrd_hi);
995 qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, ustore_addr);
998 #define MAX_EXEC_INST 100
999 static int qat_hal_exec_micro_inst(struct icp_qat_fw_loader_handle *handle,
1000 unsigned char ae, unsigned char ctx,
1001 u64 *micro_inst, unsigned int inst_num,
1002 int code_off, unsigned int max_cycle,
1003 unsigned int *endpc)
1005 unsigned int ind_lm_addr_byte0 = 0, ind_lm_addr_byte1 = 0;
1006 unsigned int ind_lm_addr_byte2 = 0, ind_lm_addr_byte3 = 0;
1007 unsigned int ind_t_index = 0, ind_t_index_byte = 0;
1008 unsigned int ind_lm_addr0 = 0, ind_lm_addr1 = 0;
1009 unsigned int ind_lm_addr2 = 0, ind_lm_addr3 = 0;
1010 u64 savuwords[MAX_EXEC_INST];
1011 unsigned int ind_cnt_sig;
1012 unsigned int ind_sig, act_sig;
1013 unsigned int csr_val = 0, newcsr_val;
1014 unsigned int savctx;
1015 unsigned int savcc, wakeup_events, savpc;
1016 unsigned int ctxarb_ctl, ctx_enables;
1018 if ((inst_num > handle->hal_handle->max_ustore) || !micro_inst) {
1019 pr_err("QAT: invalid instruction num %d\n", inst_num);
1022 /* save current context */
1023 ind_lm_addr0 = qat_hal_rd_indr_csr(handle, ae, ctx, LM_ADDR_0_INDIRECT);
1024 ind_lm_addr1 = qat_hal_rd_indr_csr(handle, ae, ctx, LM_ADDR_1_INDIRECT);
1025 ind_lm_addr_byte0 = qat_hal_rd_indr_csr(handle, ae, ctx,
1026 INDIRECT_LM_ADDR_0_BYTE_INDEX);
1027 ind_lm_addr_byte1 = qat_hal_rd_indr_csr(handle, ae, ctx,
1028 INDIRECT_LM_ADDR_1_BYTE_INDEX);
1029 if (handle->chip_info->lm2lm3) {
1030 ind_lm_addr2 = qat_hal_rd_indr_csr(handle, ae, ctx,
1031 LM_ADDR_2_INDIRECT);
1032 ind_lm_addr3 = qat_hal_rd_indr_csr(handle, ae, ctx,
1033 LM_ADDR_3_INDIRECT);
1034 ind_lm_addr_byte2 = qat_hal_rd_indr_csr(handle, ae, ctx,
1035 INDIRECT_LM_ADDR_2_BYTE_INDEX);
1036 ind_lm_addr_byte3 = qat_hal_rd_indr_csr(handle, ae, ctx,
1037 INDIRECT_LM_ADDR_3_BYTE_INDEX);
1038 ind_t_index = qat_hal_rd_indr_csr(handle, ae, ctx,
1040 ind_t_index_byte = qat_hal_rd_indr_csr(handle, ae, ctx,
1041 INDIRECT_T_INDEX_BYTE_INDEX);
1043 if (inst_num <= MAX_EXEC_INST)
1044 qat_hal_get_uwords(handle, ae, 0, inst_num, savuwords);
1045 qat_hal_get_wakeup_event(handle, ae, ctx, &wakeup_events);
1046 savpc = qat_hal_rd_indr_csr(handle, ae, ctx, CTX_STS_INDIRECT);
1047 savpc = (savpc & handle->hal_handle->upc_mask) >> 0;
1048 ctx_enables = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
1049 ctx_enables &= IGNORE_W1C_MASK;
1050 savcc = qat_hal_rd_ae_csr(handle, ae, CC_ENABLE);
1051 savctx = qat_hal_rd_ae_csr(handle, ae, ACTIVE_CTX_STATUS);
1052 ctxarb_ctl = qat_hal_rd_ae_csr(handle, ae, CTX_ARB_CNTL);
1053 ind_cnt_sig = qat_hal_rd_indr_csr(handle, ae, ctx,
1054 FUTURE_COUNT_SIGNAL_INDIRECT);
1055 ind_sig = qat_hal_rd_indr_csr(handle, ae, ctx,
1056 CTX_SIG_EVENTS_INDIRECT);
1057 act_sig = qat_hal_rd_ae_csr(handle, ae, CTX_SIG_EVENTS_ACTIVE);
1058 /* execute micro codes */
1059 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx_enables);
1060 qat_hal_wr_uwords(handle, ae, 0, inst_num, micro_inst);
1061 qat_hal_wr_indr_csr(handle, ae, (1 << ctx), CTX_STS_INDIRECT, 0);
1062 qat_hal_wr_ae_csr(handle, ae, ACTIVE_CTX_STATUS, ctx & ACS_ACNO);
1064 qat_hal_wr_ae_csr(handle, ae, CC_ENABLE, savcc & 0xffffdfff);
1065 qat_hal_put_wakeup_event(handle, ae, (1 << ctx), XCWE_VOLUNTARY);
1066 qat_hal_wr_indr_csr(handle, ae, (1 << ctx), CTX_SIG_EVENTS_INDIRECT, 0);
1067 qat_hal_wr_ae_csr(handle, ae, CTX_SIG_EVENTS_ACTIVE, 0);
1068 qat_hal_enable_ctx(handle, ae, (1 << ctx));
1069 /* wait for micro codes to finish */
1070 if (qat_hal_wait_cycles(handle, ae, max_cycle, 1) != 0)
1073 unsigned int ctx_status;
1075 ctx_status = qat_hal_rd_indr_csr(handle, ae, ctx,
1077 *endpc = ctx_status & handle->hal_handle->upc_mask;
1079 /* retore to saved context */
1080 qat_hal_disable_ctx(handle, ae, (1 << ctx));
1081 if (inst_num <= MAX_EXEC_INST)
1082 qat_hal_wr_uwords(handle, ae, 0, inst_num, savuwords);
1083 qat_hal_put_wakeup_event(handle, ae, (1 << ctx), wakeup_events);
1084 qat_hal_wr_indr_csr(handle, ae, (1 << ctx), CTX_STS_INDIRECT,
1085 handle->hal_handle->upc_mask & savpc);
1086 csr_val = qat_hal_rd_ae_csr(handle, ae, AE_MISC_CONTROL);
1087 newcsr_val = CLR_BIT(csr_val, MMC_SHARE_CS_BITPOS);
1088 qat_hal_wr_ae_csr(handle, ae, AE_MISC_CONTROL, newcsr_val);
1089 qat_hal_wr_ae_csr(handle, ae, CC_ENABLE, savcc);
1090 qat_hal_wr_ae_csr(handle, ae, ACTIVE_CTX_STATUS, savctx & ACS_ACNO);
1091 qat_hal_wr_ae_csr(handle, ae, CTX_ARB_CNTL, ctxarb_ctl);
1092 qat_hal_wr_indr_csr(handle, ae, (1 << ctx),
1093 LM_ADDR_0_INDIRECT, ind_lm_addr0);
1094 qat_hal_wr_indr_csr(handle, ae, (1 << ctx),
1095 LM_ADDR_1_INDIRECT, ind_lm_addr1);
1096 qat_hal_wr_indr_csr(handle, ae, (1 << ctx),
1097 INDIRECT_LM_ADDR_0_BYTE_INDEX, ind_lm_addr_byte0);
1098 qat_hal_wr_indr_csr(handle, ae, (1 << ctx),
1099 INDIRECT_LM_ADDR_1_BYTE_INDEX, ind_lm_addr_byte1);
1100 if (handle->chip_info->lm2lm3) {
1101 qat_hal_wr_indr_csr(handle, ae, BIT(ctx), LM_ADDR_2_INDIRECT,
1103 qat_hal_wr_indr_csr(handle, ae, BIT(ctx), LM_ADDR_3_INDIRECT,
1105 qat_hal_wr_indr_csr(handle, ae, BIT(ctx),
1106 INDIRECT_LM_ADDR_2_BYTE_INDEX,
1108 qat_hal_wr_indr_csr(handle, ae, BIT(ctx),
1109 INDIRECT_LM_ADDR_3_BYTE_INDEX,
1111 qat_hal_wr_indr_csr(handle, ae, BIT(ctx),
1112 INDIRECT_T_INDEX, ind_t_index);
1113 qat_hal_wr_indr_csr(handle, ae, BIT(ctx),
1114 INDIRECT_T_INDEX_BYTE_INDEX,
1117 qat_hal_wr_indr_csr(handle, ae, (1 << ctx),
1118 FUTURE_COUNT_SIGNAL_INDIRECT, ind_cnt_sig);
1119 qat_hal_wr_indr_csr(handle, ae, (1 << ctx),
1120 CTX_SIG_EVENTS_INDIRECT, ind_sig);
1121 qat_hal_wr_ae_csr(handle, ae, CTX_SIG_EVENTS_ACTIVE, act_sig);
1122 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx_enables);
1127 static int qat_hal_rd_rel_reg(struct icp_qat_fw_loader_handle *handle,
1128 unsigned char ae, unsigned char ctx,
1129 enum icp_qat_uof_regtype reg_type,
1130 unsigned short reg_num, unsigned int *data)
1132 unsigned int savctx, uaddr, uwrd_lo, uwrd_hi;
1133 unsigned int ctxarb_cntl, ustore_addr, ctx_enables;
1134 unsigned short reg_addr;
1136 u64 insts, savuword;
1138 reg_addr = qat_hal_get_reg_addr(reg_type, reg_num);
1139 if (reg_addr == BAD_REGADDR) {
1140 pr_err("QAT: bad regaddr=0x%x\n", reg_addr);
1145 insts = 0xA070000000ull | (reg_addr & 0x3ff);
1148 insts = (u64)0xA030000000ull | ((reg_addr & 0x3ff) << 10);
1151 savctx = qat_hal_rd_ae_csr(handle, ae, ACTIVE_CTX_STATUS);
1152 ctxarb_cntl = qat_hal_rd_ae_csr(handle, ae, CTX_ARB_CNTL);
1153 ctx_enables = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
1154 ctx_enables &= IGNORE_W1C_MASK;
1155 if (ctx != (savctx & ACS_ACNO))
1156 qat_hal_wr_ae_csr(handle, ae, ACTIVE_CTX_STATUS,
1158 qat_hal_get_uwords(handle, ae, 0, 1, &savuword);
1159 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx_enables);
1160 ustore_addr = qat_hal_rd_ae_csr(handle, ae, USTORE_ADDRESS);
1162 qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, uaddr);
1163 insts = qat_hal_set_uword_ecc(insts);
1164 uwrd_lo = (unsigned int)(insts & 0xffffffff);
1165 uwrd_hi = (unsigned int)(insts >> 0x20);
1166 qat_hal_wr_ae_csr(handle, ae, USTORE_DATA_LOWER, uwrd_lo);
1167 qat_hal_wr_ae_csr(handle, ae, USTORE_DATA_UPPER, uwrd_hi);
1168 qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, uaddr);
1169 /* delay for at least 8 cycles */
1170 qat_hal_wait_cycles(handle, ae, 0x8, 0);
1173 * the instruction should have been executed
1174 * prior to clearing the ECS in putUwords
1176 *data = qat_hal_rd_ae_csr(handle, ae, ALU_OUT);
1177 qat_hal_wr_ae_csr(handle, ae, USTORE_ADDRESS, ustore_addr);
1178 qat_hal_wr_uwords(handle, ae, 0, 1, &savuword);
1179 if (ctx != (savctx & ACS_ACNO))
1180 qat_hal_wr_ae_csr(handle, ae, ACTIVE_CTX_STATUS,
1182 qat_hal_wr_ae_csr(handle, ae, CTX_ARB_CNTL, ctxarb_cntl);
1183 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx_enables);
1188 static int qat_hal_wr_rel_reg(struct icp_qat_fw_loader_handle *handle,
1189 unsigned char ae, unsigned char ctx,
1190 enum icp_qat_uof_regtype reg_type,
1191 unsigned short reg_num, unsigned int data)
1193 unsigned short src_hiaddr, src_lowaddr, dest_addr, data16hi, data16lo;
1200 const int num_inst = ARRAY_SIZE(insts), code_off = 1;
1201 const int imm_w1 = 0, imm_w0 = 1;
1203 dest_addr = qat_hal_get_reg_addr(reg_type, reg_num);
1204 if (dest_addr == BAD_REGADDR) {
1205 pr_err("QAT: bad destAddr=0x%x\n", dest_addr);
1209 data16lo = 0xffff & data;
1210 data16hi = 0xffff & (data >> 0x10);
1211 src_hiaddr = qat_hal_get_reg_addr(ICP_NO_DEST, (unsigned short)
1213 src_lowaddr = qat_hal_get_reg_addr(ICP_NO_DEST, (unsigned short)
1217 insts[imm_w1] = insts[imm_w1] | ((data16hi >> 8) << 20) |
1218 ((src_hiaddr & 0x3ff) << 10) | (dest_addr & 0x3ff);
1219 insts[imm_w0] = insts[imm_w0] | ((data16lo >> 8) << 20) |
1220 ((src_lowaddr & 0x3ff) << 10) | (dest_addr & 0x3ff);
1223 insts[imm_w1] = insts[imm_w1] | ((data16hi >> 8) << 20) |
1224 ((dest_addr & 0x3ff) << 10) | (src_hiaddr & 0x3ff);
1226 insts[imm_w0] = insts[imm_w0] | ((data16lo >> 8) << 20) |
1227 ((dest_addr & 0x3ff) << 10) | (src_lowaddr & 0x3ff);
1231 return qat_hal_exec_micro_inst(handle, ae, ctx, insts, num_inst,
1232 code_off, num_inst * 0x5, NULL);
1235 int qat_hal_get_ins_num(void)
1237 return ARRAY_SIZE(inst_4b);
1240 static int qat_hal_concat_micro_code(u64 *micro_inst,
1241 unsigned int inst_num, unsigned int size,
1242 unsigned int addr, unsigned int *value)
1245 unsigned int cur_value;
1246 const u64 *inst_arr;
1251 orig_num = inst_num;
1252 cur_value = value[0];
1254 usize = ARRAY_SIZE(inst_4b);
1255 fixup_offset = inst_num;
1256 for (i = 0; i < usize; i++)
1257 micro_inst[inst_num++] = inst_arr[i];
1258 INSERT_IMMED_GPRA_CONST(micro_inst[fixup_offset], (addr));
1260 INSERT_IMMED_GPRA_CONST(micro_inst[fixup_offset], 0);
1262 INSERT_IMMED_GPRB_CONST(micro_inst[fixup_offset], (cur_value >> 0));
1264 INSERT_IMMED_GPRB_CONST(micro_inst[fixup_offset], (cur_value >> 0x10));
1266 return inst_num - orig_num;
1269 static int qat_hal_exec_micro_init_lm(struct icp_qat_fw_loader_handle *handle,
1270 unsigned char ae, unsigned char ctx,
1271 int *pfirst_exec, u64 *micro_inst,
1272 unsigned int inst_num)
1275 unsigned int gpra0 = 0, gpra1 = 0, gpra2 = 0;
1276 unsigned int gprb0 = 0, gprb1 = 0;
1279 qat_hal_rd_rel_reg(handle, ae, ctx, ICP_GPA_REL, 0, &gpra0);
1280 qat_hal_rd_rel_reg(handle, ae, ctx, ICP_GPA_REL, 0x1, &gpra1);
1281 qat_hal_rd_rel_reg(handle, ae, ctx, ICP_GPA_REL, 0x2, &gpra2);
1282 qat_hal_rd_rel_reg(handle, ae, ctx, ICP_GPB_REL, 0, &gprb0);
1283 qat_hal_rd_rel_reg(handle, ae, ctx, ICP_GPB_REL, 0x1, &gprb1);
1286 stat = qat_hal_exec_micro_inst(handle, ae, ctx, micro_inst, inst_num, 1,
1287 inst_num * 0x5, NULL);
1290 qat_hal_wr_rel_reg(handle, ae, ctx, ICP_GPA_REL, 0, gpra0);
1291 qat_hal_wr_rel_reg(handle, ae, ctx, ICP_GPA_REL, 0x1, gpra1);
1292 qat_hal_wr_rel_reg(handle, ae, ctx, ICP_GPA_REL, 0x2, gpra2);
1293 qat_hal_wr_rel_reg(handle, ae, ctx, ICP_GPB_REL, 0, gprb0);
1294 qat_hal_wr_rel_reg(handle, ae, ctx, ICP_GPB_REL, 0x1, gprb1);
1299 int qat_hal_batch_wr_lm(struct icp_qat_fw_loader_handle *handle,
1301 struct icp_qat_uof_batch_init *lm_init_header)
1303 struct icp_qat_uof_batch_init *plm_init;
1304 u64 *micro_inst_arry;
1306 int alloc_inst_size;
1310 plm_init = lm_init_header->next;
1311 alloc_inst_size = lm_init_header->size;
1312 if ((unsigned int)alloc_inst_size > handle->hal_handle->max_ustore)
1313 alloc_inst_size = handle->hal_handle->max_ustore;
1314 micro_inst_arry = kmalloc_array(alloc_inst_size, sizeof(u64),
1316 if (!micro_inst_arry)
1320 unsigned int addr, *value, size;
1323 addr = plm_init->addr;
1324 value = plm_init->value;
1325 size = plm_init->size;
1326 micro_inst_num += qat_hal_concat_micro_code(micro_inst_arry,
1329 plm_init = plm_init->next;
1331 /* exec micro codes */
1332 if (micro_inst_arry && micro_inst_num > 0) {
1333 micro_inst_arry[micro_inst_num++] = 0x0E000010000ull;
1334 stat = qat_hal_exec_micro_init_lm(handle, ae, 0, &first_exec,
1338 kfree(micro_inst_arry);
1342 static int qat_hal_put_rel_rd_xfer(struct icp_qat_fw_loader_handle *handle,
1343 unsigned char ae, unsigned char ctx,
1344 enum icp_qat_uof_regtype reg_type,
1345 unsigned short reg_num, unsigned int val)
1348 unsigned int reg_addr;
1349 unsigned int ctx_enables;
1350 unsigned short mask;
1351 unsigned short dr_offset = 0x10;
1353 ctx_enables = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
1354 if (CE_INUSE_CONTEXTS & ctx_enables) {
1356 pr_err("QAT: bad 4-ctx mode,ctx=0x%x\n", ctx);
1364 if (reg_num & ~mask)
1366 reg_addr = reg_num + (ctx << 0x5);
1370 SET_AE_XFER(handle, ae, reg_addr, val);
1374 SET_AE_XFER(handle, ae, (reg_addr + dr_offset), val);
1383 static int qat_hal_put_rel_wr_xfer(struct icp_qat_fw_loader_handle *handle,
1384 unsigned char ae, unsigned char ctx,
1385 enum icp_qat_uof_regtype reg_type,
1386 unsigned short reg_num, unsigned int data)
1388 unsigned int gprval, ctx_enables;
1389 unsigned short src_hiaddr, src_lowaddr, gpr_addr, xfr_addr, data16hi,
1391 unsigned short reg_mask;
1393 u64 micro_inst[] = {
1400 const int num_inst = ARRAY_SIZE(micro_inst), code_off = 1;
1401 const unsigned short gprnum = 0, dly = num_inst * 0x5;
1403 ctx_enables = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
1404 if (CE_INUSE_CONTEXTS & ctx_enables) {
1406 pr_err("QAT: 4-ctx mode,ctx=0x%x\n", ctx);
1409 reg_mask = (unsigned short)~0x1f;
1411 reg_mask = (unsigned short)~0xf;
1413 if (reg_num & reg_mask)
1415 xfr_addr = qat_hal_get_reg_addr(reg_type, reg_num);
1416 if (xfr_addr == BAD_REGADDR) {
1417 pr_err("QAT: bad xfrAddr=0x%x\n", xfr_addr);
1420 status = qat_hal_rd_rel_reg(handle, ae, ctx, ICP_GPB_REL, gprnum, &gprval);
1422 pr_err("QAT: failed to read register");
1425 gpr_addr = qat_hal_get_reg_addr(ICP_GPB_REL, gprnum);
1426 data16low = 0xffff & data;
1427 data16hi = 0xffff & (data >> 0x10);
1428 src_hiaddr = qat_hal_get_reg_addr(ICP_NO_DEST,
1429 (unsigned short)(0xff & data16hi));
1430 src_lowaddr = qat_hal_get_reg_addr(ICP_NO_DEST,
1431 (unsigned short)(0xff & data16low));
1432 micro_inst[0] = micro_inst[0x0] | ((data16hi >> 8) << 20) |
1433 ((gpr_addr & 0x3ff) << 10) | (src_hiaddr & 0x3ff);
1434 micro_inst[1] = micro_inst[0x1] | ((data16low >> 8) << 20) |
1435 ((gpr_addr & 0x3ff) << 10) | (src_lowaddr & 0x3ff);
1436 micro_inst[0x2] = micro_inst[0x2] |
1437 ((xfr_addr & 0x3ff) << 20) | ((gpr_addr & 0x3ff) << 10);
1438 status = qat_hal_exec_micro_inst(handle, ae, ctx, micro_inst, num_inst,
1439 code_off, dly, NULL);
1440 qat_hal_wr_rel_reg(handle, ae, ctx, ICP_GPB_REL, gprnum, gprval);
1444 static int qat_hal_put_rel_nn(struct icp_qat_fw_loader_handle *handle,
1445 unsigned char ae, unsigned char ctx,
1446 unsigned short nn, unsigned int val)
1448 unsigned int ctx_enables;
1451 ctx_enables = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
1452 ctx_enables &= IGNORE_W1C_MASK;
1453 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx_enables | CE_NN_MODE);
1455 stat = qat_hal_put_rel_wr_xfer(handle, ae, ctx, ICP_NEIGH_REL, nn, val);
1456 qat_hal_wr_ae_csr(handle, ae, CTX_ENABLES, ctx_enables);
1460 static int qat_hal_convert_abs_to_rel(struct icp_qat_fw_loader_handle
1461 *handle, unsigned char ae,
1462 unsigned short absreg_num,
1463 unsigned short *relreg,
1466 unsigned int ctx_enables;
1468 ctx_enables = qat_hal_rd_ae_csr(handle, ae, CTX_ENABLES);
1469 if (ctx_enables & CE_INUSE_CONTEXTS) {
1471 *relreg = absreg_num & 0x1F;
1472 *ctx = (absreg_num >> 0x4) & 0x6;
1475 *relreg = absreg_num & 0x0F;
1476 *ctx = (absreg_num >> 0x4) & 0x7;
1481 int qat_hal_init_gpr(struct icp_qat_fw_loader_handle *handle,
1482 unsigned char ae, unsigned long ctx_mask,
1483 enum icp_qat_uof_regtype reg_type,
1484 unsigned short reg_num, unsigned int regdata)
1488 unsigned char ctx = 0;
1489 enum icp_qat_uof_regtype type;
1491 if (reg_num >= ICP_QAT_UCLO_MAX_GPR_REG)
1495 if (ctx_mask == 0) {
1496 qat_hal_convert_abs_to_rel(handle, ae, reg_num, ®,
1498 type = reg_type - 1;
1502 if (!test_bit(ctx, &ctx_mask))
1505 stat = qat_hal_wr_rel_reg(handle, ae, ctx, type, reg, regdata);
1507 pr_err("QAT: write gpr fail\n");
1510 } while (ctx_mask && (ctx++ < ICP_QAT_UCLO_MAX_CTX));
1515 int qat_hal_init_wr_xfer(struct icp_qat_fw_loader_handle *handle,
1516 unsigned char ae, unsigned long ctx_mask,
1517 enum icp_qat_uof_regtype reg_type,
1518 unsigned short reg_num, unsigned int regdata)
1522 unsigned char ctx = 0;
1523 enum icp_qat_uof_regtype type;
1525 if (reg_num >= ICP_QAT_UCLO_MAX_XFER_REG)
1529 if (ctx_mask == 0) {
1530 qat_hal_convert_abs_to_rel(handle, ae, reg_num, ®,
1532 type = reg_type - 3;
1536 if (!test_bit(ctx, &ctx_mask))
1539 stat = qat_hal_put_rel_wr_xfer(handle, ae, ctx, type, reg,
1542 pr_err("QAT: write wr xfer fail\n");
1545 } while (ctx_mask && (ctx++ < ICP_QAT_UCLO_MAX_CTX));
1550 int qat_hal_init_rd_xfer(struct icp_qat_fw_loader_handle *handle,
1551 unsigned char ae, unsigned long ctx_mask,
1552 enum icp_qat_uof_regtype reg_type,
1553 unsigned short reg_num, unsigned int regdata)
1557 unsigned char ctx = 0;
1558 enum icp_qat_uof_regtype type;
1560 if (reg_num >= ICP_QAT_UCLO_MAX_XFER_REG)
1564 if (ctx_mask == 0) {
1565 qat_hal_convert_abs_to_rel(handle, ae, reg_num, ®,
1567 type = reg_type - 3;
1571 if (!test_bit(ctx, &ctx_mask))
1574 stat = qat_hal_put_rel_rd_xfer(handle, ae, ctx, type, reg,
1577 pr_err("QAT: write rd xfer fail\n");
1580 } while (ctx_mask && (ctx++ < ICP_QAT_UCLO_MAX_CTX));
1585 int qat_hal_init_nn(struct icp_qat_fw_loader_handle *handle,
1586 unsigned char ae, unsigned long ctx_mask,
1587 unsigned short reg_num, unsigned int regdata)
1591 if (!handle->chip_info->nn) {
1592 dev_err(&handle->pci_dev->dev, "QAT: No next neigh in 0x%x\n",
1593 handle->pci_dev->device);
1600 for (ctx = 0; ctx < ICP_QAT_UCLO_MAX_CTX; ctx++) {
1601 if (!test_bit(ctx, &ctx_mask))
1603 stat = qat_hal_put_rel_nn(handle, ae, ctx, reg_num, regdata);
1605 pr_err("QAT: write neigh error\n");