otx2_cptlf.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /* SPDX-License-Identifier: GPL-2.0-only
  2. * Copyright (C) 2020 Marvell.
  3. */
  4. #ifndef __OTX2_CPTLF_H
  5. #define __OTX2_CPTLF_H
  6. #include <linux/soc/marvell/octeontx2/asm.h>
  7. #include <mbox.h>
  8. #include <rvu.h>
  9. #include "otx2_cpt_common.h"
  10. #include "otx2_cpt_reqmgr.h"
  11. /*
  12. * CPT instruction and pending queues user requested length in CPT_INST_S msgs
  13. */
  14. #define OTX2_CPT_USER_REQUESTED_QLEN_MSGS 8200
  15. /*
  16. * CPT instruction queue size passed to HW is in units of 40*CPT_INST_S
  17. * messages.
  18. */
  19. #define OTX2_CPT_SIZE_DIV40 (OTX2_CPT_USER_REQUESTED_QLEN_MSGS/40)
  20. /*
  21. * CPT instruction and pending queues length in CPT_INST_S messages
  22. */
  23. #define OTX2_CPT_INST_QLEN_MSGS ((OTX2_CPT_SIZE_DIV40 - 1) * 40)
  24. /*
  25. * LDWB is getting incorrectly used when IQB_LDWB = 1 and CPT instruction
  26. * queue has less than 320 free entries. So, increase HW instruction queue
  27. * size by 320 and give 320 entries less for SW/NIX RX as a workaround.
  28. */
  29. #define OTX2_CPT_INST_QLEN_EXTRA_BYTES (320 * OTX2_CPT_INST_SIZE)
  30. #define OTX2_CPT_EXTRA_SIZE_DIV40 (320/40)
  31. /* CPT instruction queue length in bytes */
  32. #define OTX2_CPT_INST_QLEN_BYTES \
  33. ((OTX2_CPT_SIZE_DIV40 * 40 * OTX2_CPT_INST_SIZE) + \
  34. OTX2_CPT_INST_QLEN_EXTRA_BYTES)
  35. /* CPT instruction group queue length in bytes */
  36. #define OTX2_CPT_INST_GRP_QLEN_BYTES \
  37. ((OTX2_CPT_SIZE_DIV40 + OTX2_CPT_EXTRA_SIZE_DIV40) * 16)
  38. /* CPT FC length in bytes */
  39. #define OTX2_CPT_Q_FC_LEN 128
  40. /* CPT instruction queue alignment */
  41. #define OTX2_CPT_INST_Q_ALIGNMENT 128
  42. /* Mask which selects all engine groups */
  43. #define OTX2_CPT_ALL_ENG_GRPS_MASK 0xFF
  44. /* Maximum LFs supported in OcteonTX2 for CPT */
  45. #define OTX2_CPT_MAX_LFS_NUM 64
  46. /* Queue priority */
  47. #define OTX2_CPT_QUEUE_HI_PRIO 0x1
  48. #define OTX2_CPT_QUEUE_LOW_PRIO 0x0
  49. enum otx2_cptlf_state {
  50. OTX2_CPTLF_IN_RESET,
  51. OTX2_CPTLF_STARTED,
  52. };
  53. struct otx2_cpt_inst_queue {
  54. u8 *vaddr;
  55. u8 *real_vaddr;
  56. dma_addr_t dma_addr;
  57. dma_addr_t real_dma_addr;
  58. u32 size;
  59. };
  60. struct otx2_cptlfs_info;
  61. struct otx2_cptlf_wqe {
  62. struct tasklet_struct work;
  63. struct otx2_cptlfs_info *lfs;
  64. u8 lf_num;
  65. };
  66. struct otx2_cptlf_info {
  67. struct otx2_cptlfs_info *lfs; /* Ptr to cptlfs_info struct */
  68. void __iomem *lmtline; /* Address of LMTLINE */
  69. void __iomem *ioreg; /* LMTLINE send register */
  70. int msix_offset; /* MSI-X interrupts offset */
  71. cpumask_var_t affinity_mask; /* IRQs affinity mask */
  72. u8 irq_name[OTX2_CPT_LF_MSIX_VECTORS][32];/* Interrupts name */
  73. u8 is_irq_reg[OTX2_CPT_LF_MSIX_VECTORS]; /* Is interrupt registered */
  74. u8 slot; /* Slot number of this LF */
  75. struct otx2_cpt_inst_queue iqueue;/* Instruction queue */
  76. struct otx2_cpt_pending_queue pqueue; /* Pending queue */
  77. struct otx2_cptlf_wqe *wqe; /* Tasklet work info */
  78. };
  79. struct cpt_hw_ops {
  80. void (*send_cmd)(union otx2_cpt_inst_s *cptinst, u32 insts_num,
  81. struct otx2_cptlf_info *lf);
  82. u8 (*cpt_get_compcode)(union otx2_cpt_res_s *result);
  83. u8 (*cpt_get_uc_compcode)(union otx2_cpt_res_s *result);
  84. };
  85. struct otx2_cptlfs_info {
  86. /* Registers start address of VF/PF LFs are attached to */
  87. void __iomem *reg_base;
  88. #define LMTLINE_SIZE 128
  89. void __iomem *lmt_base;
  90. struct pci_dev *pdev; /* Device LFs are attached to */
  91. struct otx2_cptlf_info lf[OTX2_CPT_MAX_LFS_NUM];
  92. struct otx2_mbox *mbox;
  93. struct cpt_hw_ops *ops;
  94. u8 are_lfs_attached; /* Whether CPT LFs are attached */
  95. u8 lfs_num; /* Number of CPT LFs */
  96. u8 kcrypto_eng_grp_num; /* Kernel crypto engine group number */
  97. u8 kvf_limits; /* Kernel crypto limits */
  98. atomic_t state; /* LF's state. started/reset */
  99. int blkaddr; /* CPT blkaddr: BLKADDR_CPT0/BLKADDR_CPT1 */
  100. };
  101. static inline void otx2_cpt_free_instruction_queues(
  102. struct otx2_cptlfs_info *lfs)
  103. {
  104. struct otx2_cpt_inst_queue *iq;
  105. int i;
  106. for (i = 0; i < lfs->lfs_num; i++) {
  107. iq = &lfs->lf[i].iqueue;
  108. if (iq->real_vaddr)
  109. dma_free_coherent(&lfs->pdev->dev,
  110. iq->size,
  111. iq->real_vaddr,
  112. iq->real_dma_addr);
  113. iq->real_vaddr = NULL;
  114. iq->vaddr = NULL;
  115. }
  116. }
  117. static inline int otx2_cpt_alloc_instruction_queues(
  118. struct otx2_cptlfs_info *lfs)
  119. {
  120. struct otx2_cpt_inst_queue *iq;
  121. int ret = 0, i;
  122. if (!lfs->lfs_num)
  123. return -EINVAL;
  124. for (i = 0; i < lfs->lfs_num; i++) {
  125. iq = &lfs->lf[i].iqueue;
  126. iq->size = OTX2_CPT_INST_QLEN_BYTES +
  127. OTX2_CPT_Q_FC_LEN +
  128. OTX2_CPT_INST_GRP_QLEN_BYTES +
  129. OTX2_CPT_INST_Q_ALIGNMENT;
  130. iq->real_vaddr = dma_alloc_coherent(&lfs->pdev->dev, iq->size,
  131. &iq->real_dma_addr, GFP_KERNEL);
  132. if (!iq->real_vaddr) {
  133. ret = -ENOMEM;
  134. goto error;
  135. }
  136. iq->vaddr = iq->real_vaddr + OTX2_CPT_INST_GRP_QLEN_BYTES;
  137. iq->dma_addr = iq->real_dma_addr + OTX2_CPT_INST_GRP_QLEN_BYTES;
  138. /* Align pointers */
  139. iq->vaddr = PTR_ALIGN(iq->vaddr, OTX2_CPT_INST_Q_ALIGNMENT);
  140. iq->dma_addr = PTR_ALIGN(iq->dma_addr,
  141. OTX2_CPT_INST_Q_ALIGNMENT);
  142. }
  143. return 0;
  144. error:
  145. otx2_cpt_free_instruction_queues(lfs);
  146. return ret;
  147. }
  148. static inline void otx2_cptlf_set_iqueues_base_addr(
  149. struct otx2_cptlfs_info *lfs)
  150. {
  151. union otx2_cptx_lf_q_base lf_q_base;
  152. int slot;
  153. for (slot = 0; slot < lfs->lfs_num; slot++) {
  154. lf_q_base.u = lfs->lf[slot].iqueue.dma_addr;
  155. otx2_cpt_write64(lfs->reg_base, BLKADDR_CPT0, slot,
  156. OTX2_CPT_LF_Q_BASE, lf_q_base.u);
  157. }
  158. }
  159. static inline void otx2_cptlf_do_set_iqueue_size(struct otx2_cptlf_info *lf)
  160. {
  161. union otx2_cptx_lf_q_size lf_q_size = { .u = 0x0 };
  162. lf_q_size.s.size_div40 = OTX2_CPT_SIZE_DIV40 +
  163. OTX2_CPT_EXTRA_SIZE_DIV40;
  164. otx2_cpt_write64(lf->lfs->reg_base, BLKADDR_CPT0, lf->slot,
  165. OTX2_CPT_LF_Q_SIZE, lf_q_size.u);
  166. }
  167. static inline void otx2_cptlf_set_iqueues_size(struct otx2_cptlfs_info *lfs)
  168. {
  169. int slot;
  170. for (slot = 0; slot < lfs->lfs_num; slot++)
  171. otx2_cptlf_do_set_iqueue_size(&lfs->lf[slot]);
  172. }
  173. static inline void otx2_cptlf_do_disable_iqueue(struct otx2_cptlf_info *lf)
  174. {
  175. union otx2_cptx_lf_ctl lf_ctl = { .u = 0x0 };
  176. union otx2_cptx_lf_inprog lf_inprog;
  177. int timeout = 20;
  178. /* Disable instructions enqueuing */
  179. otx2_cpt_write64(lf->lfs->reg_base, BLKADDR_CPT0, lf->slot,
  180. OTX2_CPT_LF_CTL, lf_ctl.u);
  181. /* Wait for instruction queue to become empty */
  182. do {
  183. lf_inprog.u = otx2_cpt_read64(lf->lfs->reg_base, BLKADDR_CPT0,
  184. lf->slot, OTX2_CPT_LF_INPROG);
  185. if (!lf_inprog.s.inflight)
  186. break;
  187. usleep_range(10000, 20000);
  188. if (timeout-- < 0) {
  189. dev_err(&lf->lfs->pdev->dev,
  190. "Error LF %d is still busy.\n", lf->slot);
  191. break;
  192. }
  193. } while (1);
  194. /*
  195. * Disable executions in the LF's queue,
  196. * the queue should be empty at this point
  197. */
  198. lf_inprog.s.eena = 0x0;
  199. otx2_cpt_write64(lf->lfs->reg_base, BLKADDR_CPT0, lf->slot,
  200. OTX2_CPT_LF_INPROG, lf_inprog.u);
  201. }
  202. static inline void otx2_cptlf_disable_iqueues(struct otx2_cptlfs_info *lfs)
  203. {
  204. int slot;
  205. for (slot = 0; slot < lfs->lfs_num; slot++)
  206. otx2_cptlf_do_disable_iqueue(&lfs->lf[slot]);
  207. }
  208. static inline void otx2_cptlf_set_iqueue_enq(struct otx2_cptlf_info *lf,
  209. bool enable)
  210. {
  211. union otx2_cptx_lf_ctl lf_ctl;
  212. lf_ctl.u = otx2_cpt_read64(lf->lfs->reg_base, BLKADDR_CPT0, lf->slot,
  213. OTX2_CPT_LF_CTL);
  214. /* Set iqueue's enqueuing */
  215. lf_ctl.s.ena = enable ? 0x1 : 0x0;
  216. otx2_cpt_write64(lf->lfs->reg_base, BLKADDR_CPT0, lf->slot,
  217. OTX2_CPT_LF_CTL, lf_ctl.u);
  218. }
  219. static inline void otx2_cptlf_enable_iqueue_enq(struct otx2_cptlf_info *lf)
  220. {
  221. otx2_cptlf_set_iqueue_enq(lf, true);
  222. }
  223. static inline void otx2_cptlf_set_iqueue_exec(struct otx2_cptlf_info *lf,
  224. bool enable)
  225. {
  226. union otx2_cptx_lf_inprog lf_inprog;
  227. lf_inprog.u = otx2_cpt_read64(lf->lfs->reg_base, BLKADDR_CPT0, lf->slot,
  228. OTX2_CPT_LF_INPROG);
  229. /* Set iqueue's execution */
  230. lf_inprog.s.eena = enable ? 0x1 : 0x0;
  231. otx2_cpt_write64(lf->lfs->reg_base, BLKADDR_CPT0, lf->slot,
  232. OTX2_CPT_LF_INPROG, lf_inprog.u);
  233. }
  234. static inline void otx2_cptlf_enable_iqueue_exec(struct otx2_cptlf_info *lf)
  235. {
  236. otx2_cptlf_set_iqueue_exec(lf, true);
  237. }
  238. static inline void otx2_cptlf_disable_iqueue_exec(struct otx2_cptlf_info *lf)
  239. {
  240. otx2_cptlf_set_iqueue_exec(lf, false);
  241. }
  242. static inline void otx2_cptlf_enable_iqueues(struct otx2_cptlfs_info *lfs)
  243. {
  244. int slot;
  245. for (slot = 0; slot < lfs->lfs_num; slot++) {
  246. otx2_cptlf_enable_iqueue_exec(&lfs->lf[slot]);
  247. otx2_cptlf_enable_iqueue_enq(&lfs->lf[slot]);
  248. }
  249. }
  250. static inline void otx2_cpt_fill_inst(union otx2_cpt_inst_s *cptinst,
  251. struct otx2_cpt_iq_command *iq_cmd,
  252. u64 comp_baddr)
  253. {
  254. cptinst->u[0] = 0x0;
  255. cptinst->s.doneint = true;
  256. cptinst->s.res_addr = comp_baddr;
  257. cptinst->u[2] = 0x0;
  258. cptinst->u[3] = 0x0;
  259. cptinst->s.ei0 = iq_cmd->cmd.u;
  260. cptinst->s.ei1 = iq_cmd->dptr;
  261. cptinst->s.ei2 = iq_cmd->rptr;
  262. cptinst->s.ei3 = iq_cmd->cptr.u;
  263. }
  264. /*
  265. * On OcteonTX2 platform the parameter insts_num is used as a count of
  266. * instructions to be enqueued. The valid values for insts_num are:
  267. * 1 - 1 CPT instruction will be enqueued during LMTST operation
  268. * 2 - 2 CPT instructions will be enqueued during LMTST operation
  269. */
  270. static inline void otx2_cpt_send_cmd(union otx2_cpt_inst_s *cptinst,
  271. u32 insts_num, struct otx2_cptlf_info *lf)
  272. {
  273. void __iomem *lmtline = lf->lmtline;
  274. long ret;
  275. /*
  276. * Make sure memory areas pointed in CPT_INST_S
  277. * are flushed before the instruction is sent to CPT
  278. */
  279. dma_wmb();
  280. do {
  281. /* Copy CPT command to LMTLINE */
  282. memcpy_toio(lmtline, cptinst, insts_num * OTX2_CPT_INST_SIZE);
  283. /*
  284. * LDEOR initiates atomic transfer to I/O device
  285. * The following will cause the LMTST to fail (the LDEOR
  286. * returns zero):
  287. * - No stores have been performed to the LMTLINE since it was
  288. * last invalidated.
  289. * - The bytes which have been stored to LMTLINE since it was
  290. * last invalidated form a pattern that is non-contiguous, does
  291. * not start at byte 0, or does not end on a 8-byte boundary.
  292. * (i.e.comprises a formation of other than 1–16 8-byte
  293. * words.)
  294. *
  295. * These rules are designed such that an operating system
  296. * context switch or hypervisor guest switch need have no
  297. * knowledge of the LMTST operations; the switch code does not
  298. * need to store to LMTCANCEL. Also note as LMTLINE data cannot
  299. * be read, there is no information leakage between processes.
  300. */
  301. ret = otx2_lmt_flush(lf->ioreg);
  302. } while (!ret);
  303. }
  304. static inline bool otx2_cptlf_started(struct otx2_cptlfs_info *lfs)
  305. {
  306. return atomic_read(&lfs->state) == OTX2_CPTLF_STARTED;
  307. }
  308. int otx2_cptlf_init(struct otx2_cptlfs_info *lfs, u8 eng_grp_msk, int pri,
  309. int lfs_num);
  310. void otx2_cptlf_shutdown(struct otx2_cptlfs_info *lfs);
  311. int otx2_cptlf_register_interrupts(struct otx2_cptlfs_info *lfs);
  312. void otx2_cptlf_unregister_interrupts(struct otx2_cptlfs_info *lfs);
  313. void otx2_cptlf_free_irqs_affinity(struct otx2_cptlfs_info *lfs);
  314. int otx2_cptlf_set_irqs_affinity(struct otx2_cptlfs_info *lfs);
  315. #endif /* __OTX2_CPTLF_H */