qbman-portal.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * Copyright (C) 2014-2016 Freescale Semiconductor, Inc.
  4. * Copyright 2016 NXP
  5. *
  6. */
  7. #include <asm/cacheflush.h>
  8. #include <linux/io.h>
  9. #include <linux/slab.h>
  10. #include <soc/fsl/dpaa2-global.h>
  11. #include "qbman-portal.h"
  12. #define QMAN_REV_4000 0x04000000
  13. #define QMAN_REV_4100 0x04010000
  14. #define QMAN_REV_4101 0x04010001
  15. #define QMAN_REV_MASK 0xffff0000
  16. /* All QBMan command and result structures use this "valid bit" encoding */
  17. #define QB_VALID_BIT ((u32)0x80)
  18. /* QBMan portal management command codes */
  19. #define QBMAN_MC_ACQUIRE 0x30
  20. #define QBMAN_WQCHAN_CONFIGURE 0x46
  21. /* CINH register offsets */
  22. #define QBMAN_CINH_SWP_EQAR 0x8c0
  23. #define QBMAN_CINH_SWP_DQPI 0xa00
  24. #define QBMAN_CINH_SWP_DCAP 0xac0
  25. #define QBMAN_CINH_SWP_SDQCR 0xb00
  26. #define QBMAN_CINH_SWP_RAR 0xcc0
  27. #define QBMAN_CINH_SWP_ISR 0xe00
  28. #define QBMAN_CINH_SWP_IER 0xe40
  29. #define QBMAN_CINH_SWP_ISDR 0xe80
  30. #define QBMAN_CINH_SWP_IIR 0xec0
  31. /* CENA register offsets */
  32. #define QBMAN_CENA_SWP_EQCR(n) (0x000 + ((u32)(n) << 6))
  33. #define QBMAN_CENA_SWP_DQRR(n) (0x200 + ((u32)(n) << 6))
  34. #define QBMAN_CENA_SWP_RCR(n) (0x400 + ((u32)(n) << 6))
  35. #define QBMAN_CENA_SWP_CR 0x600
  36. #define QBMAN_CENA_SWP_RR(vb) (0x700 + ((u32)(vb) >> 1))
  37. #define QBMAN_CENA_SWP_VDQCR 0x780
  38. /* Reverse mapping of QBMAN_CENA_SWP_DQRR() */
  39. #define QBMAN_IDX_FROM_DQRR(p) (((unsigned long)(p) & 0x1ff) >> 6)
  40. /* Define token used to determine if response written to memory is valid */
  41. #define QMAN_DQ_TOKEN_VALID 1
  42. /* SDQCR attribute codes */
  43. #define QB_SDQCR_FC_SHIFT 29
  44. #define QB_SDQCR_FC_MASK 0x1
  45. #define QB_SDQCR_DCT_SHIFT 24
  46. #define QB_SDQCR_DCT_MASK 0x3
  47. #define QB_SDQCR_TOK_SHIFT 16
  48. #define QB_SDQCR_TOK_MASK 0xff
  49. #define QB_SDQCR_SRC_SHIFT 0
  50. #define QB_SDQCR_SRC_MASK 0xffff
  51. /* opaque token for static dequeues */
  52. #define QMAN_SDQCR_TOKEN 0xbb
  53. enum qbman_sdqcr_dct {
  54. qbman_sdqcr_dct_null = 0,
  55. qbman_sdqcr_dct_prio_ics,
  56. qbman_sdqcr_dct_active_ics,
  57. qbman_sdqcr_dct_active
  58. };
  59. enum qbman_sdqcr_fc {
  60. qbman_sdqcr_fc_one = 0,
  61. qbman_sdqcr_fc_up_to_3 = 1
  62. };
  63. /* Portal Access */
  64. static inline u32 qbman_read_register(struct qbman_swp *p, u32 offset)
  65. {
  66. return readl_relaxed(p->addr_cinh + offset);
  67. }
  68. static inline void qbman_write_register(struct qbman_swp *p, u32 offset,
  69. u32 value)
  70. {
  71. writel_relaxed(value, p->addr_cinh + offset);
  72. }
  73. static inline void *qbman_get_cmd(struct qbman_swp *p, u32 offset)
  74. {
  75. return p->addr_cena + offset;
  76. }
  77. #define QBMAN_CINH_SWP_CFG 0xd00
  78. #define SWP_CFG_DQRR_MF_SHIFT 20
  79. #define SWP_CFG_EST_SHIFT 16
  80. #define SWP_CFG_WN_SHIFT 14
  81. #define SWP_CFG_RPM_SHIFT 12
  82. #define SWP_CFG_DCM_SHIFT 10
  83. #define SWP_CFG_EPM_SHIFT 8
  84. #define SWP_CFG_SD_SHIFT 5
  85. #define SWP_CFG_SP_SHIFT 4
  86. #define SWP_CFG_SE_SHIFT 3
  87. #define SWP_CFG_DP_SHIFT 2
  88. #define SWP_CFG_DE_SHIFT 1
  89. #define SWP_CFG_EP_SHIFT 0
  90. static inline u32 qbman_set_swp_cfg(u8 max_fill, u8 wn, u8 est, u8 rpm, u8 dcm,
  91. u8 epm, int sd, int sp, int se,
  92. int dp, int de, int ep)
  93. {
  94. return (max_fill << SWP_CFG_DQRR_MF_SHIFT |
  95. est << SWP_CFG_EST_SHIFT |
  96. wn << SWP_CFG_WN_SHIFT |
  97. rpm << SWP_CFG_RPM_SHIFT |
  98. dcm << SWP_CFG_DCM_SHIFT |
  99. epm << SWP_CFG_EPM_SHIFT |
  100. sd << SWP_CFG_SD_SHIFT |
  101. sp << SWP_CFG_SP_SHIFT |
  102. se << SWP_CFG_SE_SHIFT |
  103. dp << SWP_CFG_DP_SHIFT |
  104. de << SWP_CFG_DE_SHIFT |
  105. ep << SWP_CFG_EP_SHIFT);
  106. }
  107. /**
  108. * qbman_swp_init() - Create a functional object representing the given
  109. * QBMan portal descriptor.
  110. * @d: the given qbman swp descriptor
  111. *
  112. * Return qbman_swp portal for success, NULL if the object cannot
  113. * be created.
  114. */
  115. struct qbman_swp *qbman_swp_init(const struct qbman_swp_desc *d)
  116. {
  117. struct qbman_swp *p = kmalloc(sizeof(*p), GFP_KERNEL);
  118. u32 reg;
  119. if (!p)
  120. return NULL;
  121. p->desc = d;
  122. p->mc.valid_bit = QB_VALID_BIT;
  123. p->sdq = 0;
  124. p->sdq |= qbman_sdqcr_dct_prio_ics << QB_SDQCR_DCT_SHIFT;
  125. p->sdq |= qbman_sdqcr_fc_up_to_3 << QB_SDQCR_FC_SHIFT;
  126. p->sdq |= QMAN_SDQCR_TOKEN << QB_SDQCR_TOK_SHIFT;
  127. atomic_set(&p->vdq.available, 1);
  128. p->vdq.valid_bit = QB_VALID_BIT;
  129. p->dqrr.next_idx = 0;
  130. p->dqrr.valid_bit = QB_VALID_BIT;
  131. if ((p->desc->qman_version & QMAN_REV_MASK) < QMAN_REV_4100) {
  132. p->dqrr.dqrr_size = 4;
  133. p->dqrr.reset_bug = 1;
  134. } else {
  135. p->dqrr.dqrr_size = 8;
  136. p->dqrr.reset_bug = 0;
  137. }
  138. p->addr_cena = d->cena_bar;
  139. p->addr_cinh = d->cinh_bar;
  140. reg = qbman_set_swp_cfg(p->dqrr.dqrr_size,
  141. 1, /* Writes Non-cacheable */
  142. 0, /* EQCR_CI stashing threshold */
  143. 3, /* RPM: Valid bit mode, RCR in array mode */
  144. 2, /* DCM: Discrete consumption ack mode */
  145. 3, /* EPM: Valid bit mode, EQCR in array mode */
  146. 0, /* mem stashing drop enable == FALSE */
  147. 1, /* mem stashing priority == TRUE */
  148. 0, /* mem stashing enable == FALSE */
  149. 1, /* dequeue stashing priority == TRUE */
  150. 0, /* dequeue stashing enable == FALSE */
  151. 0); /* EQCR_CI stashing priority == FALSE */
  152. qbman_write_register(p, QBMAN_CINH_SWP_CFG, reg);
  153. reg = qbman_read_register(p, QBMAN_CINH_SWP_CFG);
  154. if (!reg) {
  155. pr_err("qbman: the portal is not enabled!\n");
  156. return NULL;
  157. }
  158. /*
  159. * SDQCR needs to be initialized to 0 when no channels are
  160. * being dequeued from or else the QMan HW will indicate an
  161. * error. The values that were calculated above will be
  162. * applied when dequeues from a specific channel are enabled.
  163. */
  164. qbman_write_register(p, QBMAN_CINH_SWP_SDQCR, 0);
  165. return p;
  166. }
  167. /**
  168. * qbman_swp_finish() - Create and destroy a functional object representing
  169. * the given QBMan portal descriptor.
  170. * @p: the qbman_swp object to be destroyed
  171. */
  172. void qbman_swp_finish(struct qbman_swp *p)
  173. {
  174. kfree(p);
  175. }
  176. /**
  177. * qbman_swp_interrupt_read_status()
  178. * @p: the given software portal
  179. *
  180. * Return the value in the SWP_ISR register.
  181. */
  182. u32 qbman_swp_interrupt_read_status(struct qbman_swp *p)
  183. {
  184. return qbman_read_register(p, QBMAN_CINH_SWP_ISR);
  185. }
  186. /**
  187. * qbman_swp_interrupt_clear_status()
  188. * @p: the given software portal
  189. * @mask: The mask to clear in SWP_ISR register
  190. */
  191. void qbman_swp_interrupt_clear_status(struct qbman_swp *p, u32 mask)
  192. {
  193. qbman_write_register(p, QBMAN_CINH_SWP_ISR, mask);
  194. }
  195. /**
  196. * qbman_swp_interrupt_get_trigger() - read interrupt enable register
  197. * @p: the given software portal
  198. *
  199. * Return the value in the SWP_IER register.
  200. */
  201. u32 qbman_swp_interrupt_get_trigger(struct qbman_swp *p)
  202. {
  203. return qbman_read_register(p, QBMAN_CINH_SWP_IER);
  204. }
  205. /**
  206. * qbman_swp_interrupt_set_trigger() - enable interrupts for a swp
  207. * @p: the given software portal
  208. * @mask: The mask of bits to enable in SWP_IER
  209. */
  210. void qbman_swp_interrupt_set_trigger(struct qbman_swp *p, u32 mask)
  211. {
  212. qbman_write_register(p, QBMAN_CINH_SWP_IER, mask);
  213. }
  214. /**
  215. * qbman_swp_interrupt_get_inhibit() - read interrupt mask register
  216. * @p: the given software portal object
  217. *
  218. * Return the value in the SWP_IIR register.
  219. */
  220. int qbman_swp_interrupt_get_inhibit(struct qbman_swp *p)
  221. {
  222. return qbman_read_register(p, QBMAN_CINH_SWP_IIR);
  223. }
  224. /**
  225. * qbman_swp_interrupt_set_inhibit() - write interrupt mask register
  226. * @p: the given software portal object
  227. * @mask: The mask to set in SWP_IIR register
  228. */
  229. void qbman_swp_interrupt_set_inhibit(struct qbman_swp *p, int inhibit)
  230. {
  231. qbman_write_register(p, QBMAN_CINH_SWP_IIR, inhibit ? 0xffffffff : 0);
  232. }
  233. /*
  234. * Different management commands all use this common base layer of code to issue
  235. * commands and poll for results.
  236. */
  237. /*
  238. * Returns a pointer to where the caller should fill in their management command
  239. * (caller should ignore the verb byte)
  240. */
  241. void *qbman_swp_mc_start(struct qbman_swp *p)
  242. {
  243. return qbman_get_cmd(p, QBMAN_CENA_SWP_CR);
  244. }
  245. /*
  246. * Commits merges in the caller-supplied command verb (which should not include
  247. * the valid-bit) and submits the command to hardware
  248. */
  249. void qbman_swp_mc_submit(struct qbman_swp *p, void *cmd, u8 cmd_verb)
  250. {
  251. u8 *v = cmd;
  252. dma_wmb();
  253. *v = cmd_verb | p->mc.valid_bit;
  254. }
  255. /*
  256. * Checks for a completed response (returns non-NULL if only if the response
  257. * is complete).
  258. */
  259. void *qbman_swp_mc_result(struct qbman_swp *p)
  260. {
  261. u32 *ret, verb;
  262. ret = qbman_get_cmd(p, QBMAN_CENA_SWP_RR(p->mc.valid_bit));
  263. /* Remove the valid-bit - command completed if the rest is non-zero */
  264. verb = ret[0] & ~QB_VALID_BIT;
  265. if (!verb)
  266. return NULL;
  267. p->mc.valid_bit ^= QB_VALID_BIT;
  268. return ret;
  269. }
  270. #define QB_ENQUEUE_CMD_OPTIONS_SHIFT 0
  271. enum qb_enqueue_commands {
  272. enqueue_empty = 0,
  273. enqueue_response_always = 1,
  274. enqueue_rejects_to_fq = 2
  275. };
  276. #define QB_ENQUEUE_CMD_ORP_ENABLE_SHIFT 2
  277. #define QB_ENQUEUE_CMD_IRQ_ON_DISPATCH_SHIFT 3
  278. #define QB_ENQUEUE_CMD_TARGET_TYPE_SHIFT 4
  279. /**
  280. * qbman_eq_desc_clear() - Clear the contents of a descriptor to
  281. * default/starting state.
  282. */
  283. void qbman_eq_desc_clear(struct qbman_eq_desc *d)
  284. {
  285. memset(d, 0, sizeof(*d));
  286. }
  287. /**
  288. * qbman_eq_desc_set_no_orp() - Set enqueue descriptor without orp
  289. * @d: the enqueue descriptor.
  290. * @response_success: 1 = enqueue with response always; 0 = enqueue with
  291. * rejections returned on a FQ.
  292. */
  293. void qbman_eq_desc_set_no_orp(struct qbman_eq_desc *d, int respond_success)
  294. {
  295. d->verb &= ~(1 << QB_ENQUEUE_CMD_ORP_ENABLE_SHIFT);
  296. if (respond_success)
  297. d->verb |= enqueue_response_always;
  298. else
  299. d->verb |= enqueue_rejects_to_fq;
  300. }
  301. /*
  302. * Exactly one of the following descriptor "targets" should be set. (Calling any
  303. * one of these will replace the effect of any prior call to one of these.)
  304. * -enqueue to a frame queue
  305. * -enqueue to a queuing destination
  306. */
  307. /**
  308. * qbman_eq_desc_set_fq() - set the FQ for the enqueue command
  309. * @d: the enqueue descriptor
  310. * @fqid: the id of the frame queue to be enqueued
  311. */
  312. void qbman_eq_desc_set_fq(struct qbman_eq_desc *d, u32 fqid)
  313. {
  314. d->verb &= ~(1 << QB_ENQUEUE_CMD_TARGET_TYPE_SHIFT);
  315. d->tgtid = cpu_to_le32(fqid);
  316. }
  317. /**
  318. * qbman_eq_desc_set_qd() - Set Queuing Destination for the enqueue command
  319. * @d: the enqueue descriptor
  320. * @qdid: the id of the queuing destination to be enqueued
  321. * @qd_bin: the queuing destination bin
  322. * @qd_prio: the queuing destination priority
  323. */
  324. void qbman_eq_desc_set_qd(struct qbman_eq_desc *d, u32 qdid,
  325. u32 qd_bin, u32 qd_prio)
  326. {
  327. d->verb |= 1 << QB_ENQUEUE_CMD_TARGET_TYPE_SHIFT;
  328. d->tgtid = cpu_to_le32(qdid);
  329. d->qdbin = cpu_to_le16(qd_bin);
  330. d->qpri = qd_prio;
  331. }
  332. #define EQAR_IDX(eqar) ((eqar) & 0x7)
  333. #define EQAR_VB(eqar) ((eqar) & 0x80)
  334. #define EQAR_SUCCESS(eqar) ((eqar) & 0x100)
  335. /**
  336. * qbman_swp_enqueue() - Issue an enqueue command
  337. * @s: the software portal used for enqueue
  338. * @d: the enqueue descriptor
  339. * @fd: the frame descriptor to be enqueued
  340. *
  341. * Please note that 'fd' should only be NULL if the "action" of the
  342. * descriptor is "orp_hole" or "orp_nesn".
  343. *
  344. * Return 0 for successful enqueue, -EBUSY if the EQCR is not ready.
  345. */
  346. int qbman_swp_enqueue(struct qbman_swp *s, const struct qbman_eq_desc *d,
  347. const struct dpaa2_fd *fd)
  348. {
  349. struct qbman_eq_desc *p;
  350. u32 eqar = qbman_read_register(s, QBMAN_CINH_SWP_EQAR);
  351. if (!EQAR_SUCCESS(eqar))
  352. return -EBUSY;
  353. p = qbman_get_cmd(s, QBMAN_CENA_SWP_EQCR(EQAR_IDX(eqar)));
  354. memcpy(&p->dca, &d->dca, 31);
  355. memcpy(&p->fd, fd, sizeof(*fd));
  356. /* Set the verb byte, have to substitute in the valid-bit */
  357. dma_wmb();
  358. p->verb = d->verb | EQAR_VB(eqar);
  359. return 0;
  360. }
  361. /* Static (push) dequeue */
  362. /**
  363. * qbman_swp_push_get() - Get the push dequeue setup
  364. * @p: the software portal object
  365. * @channel_idx: the channel index to query
  366. * @enabled: returned boolean to show whether the push dequeue is enabled
  367. * for the given channel
  368. */
  369. void qbman_swp_push_get(struct qbman_swp *s, u8 channel_idx, int *enabled)
  370. {
  371. u16 src = (s->sdq >> QB_SDQCR_SRC_SHIFT) & QB_SDQCR_SRC_MASK;
  372. WARN_ON(channel_idx > 15);
  373. *enabled = src | (1 << channel_idx);
  374. }
  375. /**
  376. * qbman_swp_push_set() - Enable or disable push dequeue
  377. * @p: the software portal object
  378. * @channel_idx: the channel index (0 to 15)
  379. * @enable: enable or disable push dequeue
  380. */
  381. void qbman_swp_push_set(struct qbman_swp *s, u8 channel_idx, int enable)
  382. {
  383. u16 dqsrc;
  384. WARN_ON(channel_idx > 15);
  385. if (enable)
  386. s->sdq |= 1 << channel_idx;
  387. else
  388. s->sdq &= ~(1 << channel_idx);
  389. /* Read make the complete src map. If no channels are enabled
  390. * the SDQCR must be 0 or else QMan will assert errors
  391. */
  392. dqsrc = (s->sdq >> QB_SDQCR_SRC_SHIFT) & QB_SDQCR_SRC_MASK;
  393. if (dqsrc != 0)
  394. qbman_write_register(s, QBMAN_CINH_SWP_SDQCR, s->sdq);
  395. else
  396. qbman_write_register(s, QBMAN_CINH_SWP_SDQCR, 0);
  397. }
  398. #define QB_VDQCR_VERB_DCT_SHIFT 0
  399. #define QB_VDQCR_VERB_DT_SHIFT 2
  400. #define QB_VDQCR_VERB_RLS_SHIFT 4
  401. #define QB_VDQCR_VERB_WAE_SHIFT 5
  402. enum qb_pull_dt_e {
  403. qb_pull_dt_channel,
  404. qb_pull_dt_workqueue,
  405. qb_pull_dt_framequeue
  406. };
  407. /**
  408. * qbman_pull_desc_clear() - Clear the contents of a descriptor to
  409. * default/starting state
  410. * @d: the pull dequeue descriptor to be cleared
  411. */
  412. void qbman_pull_desc_clear(struct qbman_pull_desc *d)
  413. {
  414. memset(d, 0, sizeof(*d));
  415. }
  416. /**
  417. * qbman_pull_desc_set_storage()- Set the pull dequeue storage
  418. * @d: the pull dequeue descriptor to be set
  419. * @storage: the pointer of the memory to store the dequeue result
  420. * @storage_phys: the physical address of the storage memory
  421. * @stash: to indicate whether write allocate is enabled
  422. *
  423. * If not called, or if called with 'storage' as NULL, the result pull dequeues
  424. * will produce results to DQRR. If 'storage' is non-NULL, then results are
  425. * produced to the given memory location (using the DMA address which
  426. * the caller provides in 'storage_phys'), and 'stash' controls whether or not
  427. * those writes to main-memory express a cache-warming attribute.
  428. */
  429. void qbman_pull_desc_set_storage(struct qbman_pull_desc *d,
  430. struct dpaa2_dq *storage,
  431. dma_addr_t storage_phys,
  432. int stash)
  433. {
  434. /* save the virtual address */
  435. d->rsp_addr_virt = (u64)(uintptr_t)storage;
  436. if (!storage) {
  437. d->verb &= ~(1 << QB_VDQCR_VERB_RLS_SHIFT);
  438. return;
  439. }
  440. d->verb |= 1 << QB_VDQCR_VERB_RLS_SHIFT;
  441. if (stash)
  442. d->verb |= 1 << QB_VDQCR_VERB_WAE_SHIFT;
  443. else
  444. d->verb &= ~(1 << QB_VDQCR_VERB_WAE_SHIFT);
  445. d->rsp_addr = cpu_to_le64(storage_phys);
  446. }
  447. /**
  448. * qbman_pull_desc_set_numframes() - Set the number of frames to be dequeued
  449. * @d: the pull dequeue descriptor to be set
  450. * @numframes: number of frames to be set, must be between 1 and 16, inclusive
  451. */
  452. void qbman_pull_desc_set_numframes(struct qbman_pull_desc *d, u8 numframes)
  453. {
  454. d->numf = numframes - 1;
  455. }
  456. /*
  457. * Exactly one of the following descriptor "actions" should be set. (Calling any
  458. * one of these will replace the effect of any prior call to one of these.)
  459. * - pull dequeue from the given frame queue (FQ)
  460. * - pull dequeue from any FQ in the given work queue (WQ)
  461. * - pull dequeue from any FQ in any WQ in the given channel
  462. */
  463. /**
  464. * qbman_pull_desc_set_fq() - Set fqid from which the dequeue command dequeues
  465. * @fqid: the frame queue index of the given FQ
  466. */
  467. void qbman_pull_desc_set_fq(struct qbman_pull_desc *d, u32 fqid)
  468. {
  469. d->verb |= 1 << QB_VDQCR_VERB_DCT_SHIFT;
  470. d->verb |= qb_pull_dt_framequeue << QB_VDQCR_VERB_DT_SHIFT;
  471. d->dq_src = cpu_to_le32(fqid);
  472. }
  473. /**
  474. * qbman_pull_desc_set_wq() - Set wqid from which the dequeue command dequeues
  475. * @wqid: composed of channel id and wqid within the channel
  476. * @dct: the dequeue command type
  477. */
  478. void qbman_pull_desc_set_wq(struct qbman_pull_desc *d, u32 wqid,
  479. enum qbman_pull_type_e dct)
  480. {
  481. d->verb |= dct << QB_VDQCR_VERB_DCT_SHIFT;
  482. d->verb |= qb_pull_dt_workqueue << QB_VDQCR_VERB_DT_SHIFT;
  483. d->dq_src = cpu_to_le32(wqid);
  484. }
  485. /**
  486. * qbman_pull_desc_set_channel() - Set channelid from which the dequeue command
  487. * dequeues
  488. * @chid: the channel id to be dequeued
  489. * @dct: the dequeue command type
  490. */
  491. void qbman_pull_desc_set_channel(struct qbman_pull_desc *d, u32 chid,
  492. enum qbman_pull_type_e dct)
  493. {
  494. d->verb |= dct << QB_VDQCR_VERB_DCT_SHIFT;
  495. d->verb |= qb_pull_dt_channel << QB_VDQCR_VERB_DT_SHIFT;
  496. d->dq_src = cpu_to_le32(chid);
  497. }
  498. /**
  499. * qbman_swp_pull() - Issue the pull dequeue command
  500. * @s: the software portal object
  501. * @d: the software portal descriptor which has been configured with
  502. * the set of qbman_pull_desc_set_*() calls
  503. *
  504. * Return 0 for success, and -EBUSY if the software portal is not ready
  505. * to do pull dequeue.
  506. */
  507. int qbman_swp_pull(struct qbman_swp *s, struct qbman_pull_desc *d)
  508. {
  509. struct qbman_pull_desc *p;
  510. if (!atomic_dec_and_test(&s->vdq.available)) {
  511. atomic_inc(&s->vdq.available);
  512. return -EBUSY;
  513. }
  514. s->vdq.storage = (void *)(uintptr_t)d->rsp_addr_virt;
  515. p = qbman_get_cmd(s, QBMAN_CENA_SWP_VDQCR);
  516. p->numf = d->numf;
  517. p->tok = QMAN_DQ_TOKEN_VALID;
  518. p->dq_src = d->dq_src;
  519. p->rsp_addr = d->rsp_addr;
  520. p->rsp_addr_virt = d->rsp_addr_virt;
  521. dma_wmb();
  522. /* Set the verb byte, have to substitute in the valid-bit */
  523. p->verb = d->verb | s->vdq.valid_bit;
  524. s->vdq.valid_bit ^= QB_VALID_BIT;
  525. return 0;
  526. }
  527. #define QMAN_DQRR_PI_MASK 0xf
  528. /**
  529. * qbman_swp_dqrr_next() - Get an valid DQRR entry
  530. * @s: the software portal object
  531. *
  532. * Return NULL if there are no unconsumed DQRR entries. Return a DQRR entry
  533. * only once, so repeated calls can return a sequence of DQRR entries, without
  534. * requiring they be consumed immediately or in any particular order.
  535. */
  536. const struct dpaa2_dq *qbman_swp_dqrr_next(struct qbman_swp *s)
  537. {
  538. u32 verb;
  539. u32 response_verb;
  540. u32 flags;
  541. struct dpaa2_dq *p;
  542. /* Before using valid-bit to detect if something is there, we have to
  543. * handle the case of the DQRR reset bug...
  544. */
  545. if (unlikely(s->dqrr.reset_bug)) {
  546. /*
  547. * We pick up new entries by cache-inhibited producer index,
  548. * which means that a non-coherent mapping would require us to
  549. * invalidate and read *only* once that PI has indicated that
  550. * there's an entry here. The first trip around the DQRR ring
  551. * will be much less efficient than all subsequent trips around
  552. * it...
  553. */
  554. u8 pi = qbman_read_register(s, QBMAN_CINH_SWP_DQPI) &
  555. QMAN_DQRR_PI_MASK;
  556. /* there are new entries if pi != next_idx */
  557. if (pi == s->dqrr.next_idx)
  558. return NULL;
  559. /*
  560. * if next_idx is/was the last ring index, and 'pi' is
  561. * different, we can disable the workaround as all the ring
  562. * entries have now been DMA'd to so valid-bit checking is
  563. * repaired. Note: this logic needs to be based on next_idx
  564. * (which increments one at a time), rather than on pi (which
  565. * can burst and wrap-around between our snapshots of it).
  566. */
  567. if (s->dqrr.next_idx == (s->dqrr.dqrr_size - 1)) {
  568. pr_debug("next_idx=%d, pi=%d, clear reset bug\n",
  569. s->dqrr.next_idx, pi);
  570. s->dqrr.reset_bug = 0;
  571. }
  572. prefetch(qbman_get_cmd(s,
  573. QBMAN_CENA_SWP_DQRR(s->dqrr.next_idx)));
  574. }
  575. p = qbman_get_cmd(s, QBMAN_CENA_SWP_DQRR(s->dqrr.next_idx));
  576. verb = p->dq.verb;
  577. /*
  578. * If the valid-bit isn't of the expected polarity, nothing there. Note,
  579. * in the DQRR reset bug workaround, we shouldn't need to skip these
  580. * check, because we've already determined that a new entry is available
  581. * and we've invalidated the cacheline before reading it, so the
  582. * valid-bit behaviour is repaired and should tell us what we already
  583. * knew from reading PI.
  584. */
  585. if ((verb & QB_VALID_BIT) != s->dqrr.valid_bit) {
  586. prefetch(qbman_get_cmd(s,
  587. QBMAN_CENA_SWP_DQRR(s->dqrr.next_idx)));
  588. return NULL;
  589. }
  590. /*
  591. * There's something there. Move "next_idx" attention to the next ring
  592. * entry (and prefetch it) before returning what we found.
  593. */
  594. s->dqrr.next_idx++;
  595. s->dqrr.next_idx &= s->dqrr.dqrr_size - 1; /* Wrap around */
  596. if (!s->dqrr.next_idx)
  597. s->dqrr.valid_bit ^= QB_VALID_BIT;
  598. /*
  599. * If this is the final response to a volatile dequeue command
  600. * indicate that the vdq is available
  601. */
  602. flags = p->dq.stat;
  603. response_verb = verb & QBMAN_RESULT_MASK;
  604. if ((response_verb == QBMAN_RESULT_DQ) &&
  605. (flags & DPAA2_DQ_STAT_VOLATILE) &&
  606. (flags & DPAA2_DQ_STAT_EXPIRED))
  607. atomic_inc(&s->vdq.available);
  608. prefetch(qbman_get_cmd(s, QBMAN_CENA_SWP_DQRR(s->dqrr.next_idx)));
  609. return p;
  610. }
  611. /**
  612. * qbman_swp_dqrr_consume() - Consume DQRR entries previously returned from
  613. * qbman_swp_dqrr_next().
  614. * @s: the software portal object
  615. * @dq: the DQRR entry to be consumed
  616. */
  617. void qbman_swp_dqrr_consume(struct qbman_swp *s, const struct dpaa2_dq *dq)
  618. {
  619. qbman_write_register(s, QBMAN_CINH_SWP_DCAP, QBMAN_IDX_FROM_DQRR(dq));
  620. }
  621. /**
  622. * qbman_result_has_new_result() - Check and get the dequeue response from the
  623. * dq storage memory set in pull dequeue command
  624. * @s: the software portal object
  625. * @dq: the dequeue result read from the memory
  626. *
  627. * Return 1 for getting a valid dequeue result, or 0 for not getting a valid
  628. * dequeue result.
  629. *
  630. * Only used for user-provided storage of dequeue results, not DQRR. For
  631. * efficiency purposes, the driver will perform any required endianness
  632. * conversion to ensure that the user's dequeue result storage is in host-endian
  633. * format. As such, once the user has called qbman_result_has_new_result() and
  634. * been returned a valid dequeue result, they should not call it again on
  635. * the same memory location (except of course if another dequeue command has
  636. * been executed to produce a new result to that location).
  637. */
  638. int qbman_result_has_new_result(struct qbman_swp *s, const struct dpaa2_dq *dq)
  639. {
  640. if (dq->dq.tok != QMAN_DQ_TOKEN_VALID)
  641. return 0;
  642. /*
  643. * Set token to be 0 so we will detect change back to 1
  644. * next time the looping is traversed. Const is cast away here
  645. * as we want users to treat the dequeue responses as read only.
  646. */
  647. ((struct dpaa2_dq *)dq)->dq.tok = 0;
  648. /*
  649. * Determine whether VDQCR is available based on whether the
  650. * current result is sitting in the first storage location of
  651. * the busy command.
  652. */
  653. if (s->vdq.storage == dq) {
  654. s->vdq.storage = NULL;
  655. atomic_inc(&s->vdq.available);
  656. }
  657. return 1;
  658. }
  659. /**
  660. * qbman_release_desc_clear() - Clear the contents of a descriptor to
  661. * default/starting state.
  662. */
  663. void qbman_release_desc_clear(struct qbman_release_desc *d)
  664. {
  665. memset(d, 0, sizeof(*d));
  666. d->verb = 1 << 5; /* Release Command Valid */
  667. }
  668. /**
  669. * qbman_release_desc_set_bpid() - Set the ID of the buffer pool to release to
  670. */
  671. void qbman_release_desc_set_bpid(struct qbman_release_desc *d, u16 bpid)
  672. {
  673. d->bpid = cpu_to_le16(bpid);
  674. }
  675. /**
  676. * qbman_release_desc_set_rcdi() - Determines whether or not the portal's RCDI
  677. * interrupt source should be asserted after the release command is completed.
  678. */
  679. void qbman_release_desc_set_rcdi(struct qbman_release_desc *d, int enable)
  680. {
  681. if (enable)
  682. d->verb |= 1 << 6;
  683. else
  684. d->verb &= ~(1 << 6);
  685. }
  686. #define RAR_IDX(rar) ((rar) & 0x7)
  687. #define RAR_VB(rar) ((rar) & 0x80)
  688. #define RAR_SUCCESS(rar) ((rar) & 0x100)
  689. /**
  690. * qbman_swp_release() - Issue a buffer release command
  691. * @s: the software portal object
  692. * @d: the release descriptor
  693. * @buffers: a pointer pointing to the buffer address to be released
  694. * @num_buffers: number of buffers to be released, must be less than 8
  695. *
  696. * Return 0 for success, -EBUSY if the release command ring is not ready.
  697. */
  698. int qbman_swp_release(struct qbman_swp *s, const struct qbman_release_desc *d,
  699. const u64 *buffers, unsigned int num_buffers)
  700. {
  701. int i;
  702. struct qbman_release_desc *p;
  703. u32 rar;
  704. if (!num_buffers || (num_buffers > 7))
  705. return -EINVAL;
  706. rar = qbman_read_register(s, QBMAN_CINH_SWP_RAR);
  707. if (!RAR_SUCCESS(rar))
  708. return -EBUSY;
  709. /* Start the release command */
  710. p = qbman_get_cmd(s, QBMAN_CENA_SWP_RCR(RAR_IDX(rar)));
  711. /* Copy the caller's buffer pointers to the command */
  712. for (i = 0; i < num_buffers; i++)
  713. p->buf[i] = cpu_to_le64(buffers[i]);
  714. p->bpid = d->bpid;
  715. /*
  716. * Set the verb byte, have to substitute in the valid-bit and the number
  717. * of buffers.
  718. */
  719. dma_wmb();
  720. p->verb = d->verb | RAR_VB(rar) | num_buffers;
  721. return 0;
  722. }
  723. struct qbman_acquire_desc {
  724. u8 verb;
  725. u8 reserved;
  726. __le16 bpid;
  727. u8 num;
  728. u8 reserved2[59];
  729. };
  730. struct qbman_acquire_rslt {
  731. u8 verb;
  732. u8 rslt;
  733. __le16 reserved;
  734. u8 num;
  735. u8 reserved2[3];
  736. __le64 buf[7];
  737. };
  738. /**
  739. * qbman_swp_acquire() - Issue a buffer acquire command
  740. * @s: the software portal object
  741. * @bpid: the buffer pool index
  742. * @buffers: a pointer pointing to the acquired buffer addresses
  743. * @num_buffers: number of buffers to be acquired, must be less than 8
  744. *
  745. * Return 0 for success, or negative error code if the acquire command
  746. * fails.
  747. */
  748. int qbman_swp_acquire(struct qbman_swp *s, u16 bpid, u64 *buffers,
  749. unsigned int num_buffers)
  750. {
  751. struct qbman_acquire_desc *p;
  752. struct qbman_acquire_rslt *r;
  753. int i;
  754. if (!num_buffers || (num_buffers > 7))
  755. return -EINVAL;
  756. /* Start the management command */
  757. p = qbman_swp_mc_start(s);
  758. if (!p)
  759. return -EBUSY;
  760. /* Encode the caller-provided attributes */
  761. p->bpid = cpu_to_le16(bpid);
  762. p->num = num_buffers;
  763. /* Complete the management command */
  764. r = qbman_swp_mc_complete(s, p, QBMAN_MC_ACQUIRE);
  765. if (unlikely(!r)) {
  766. pr_err("qbman: acquire from BPID %d failed, no response\n",
  767. bpid);
  768. return -EIO;
  769. }
  770. /* Decode the outcome */
  771. WARN_ON((r->verb & 0x7f) != QBMAN_MC_ACQUIRE);
  772. /* Determine success or failure */
  773. if (unlikely(r->rslt != QBMAN_MC_RSLT_OK)) {
  774. pr_err("qbman: acquire from BPID 0x%x failed, code=0x%02x\n",
  775. bpid, r->rslt);
  776. return -EIO;
  777. }
  778. WARN_ON(r->num > num_buffers);
  779. /* Copy the acquired buffers to the caller's array */
  780. for (i = 0; i < r->num; i++)
  781. buffers[i] = le64_to_cpu(r->buf[i]);
  782. return (int)r->num;
  783. }
  784. struct qbman_alt_fq_state_desc {
  785. u8 verb;
  786. u8 reserved[3];
  787. __le32 fqid;
  788. u8 reserved2[56];
  789. };
  790. struct qbman_alt_fq_state_rslt {
  791. u8 verb;
  792. u8 rslt;
  793. u8 reserved[62];
  794. };
  795. #define ALT_FQ_FQID_MASK 0x00FFFFFF
  796. int qbman_swp_alt_fq_state(struct qbman_swp *s, u32 fqid,
  797. u8 alt_fq_verb)
  798. {
  799. struct qbman_alt_fq_state_desc *p;
  800. struct qbman_alt_fq_state_rslt *r;
  801. /* Start the management command */
  802. p = qbman_swp_mc_start(s);
  803. if (!p)
  804. return -EBUSY;
  805. p->fqid = cpu_to_le32(fqid & ALT_FQ_FQID_MASK);
  806. /* Complete the management command */
  807. r = qbman_swp_mc_complete(s, p, alt_fq_verb);
  808. if (unlikely(!r)) {
  809. pr_err("qbman: mgmt cmd failed, no response (verb=0x%x)\n",
  810. alt_fq_verb);
  811. return -EIO;
  812. }
  813. /* Decode the outcome */
  814. WARN_ON((r->verb & QBMAN_RESULT_MASK) != alt_fq_verb);
  815. /* Determine success or failure */
  816. if (unlikely(r->rslt != QBMAN_MC_RSLT_OK)) {
  817. pr_err("qbman: ALT FQID %d failed: verb = 0x%08x code = 0x%02x\n",
  818. fqid, r->verb, r->rslt);
  819. return -EIO;
  820. }
  821. return 0;
  822. }
  823. struct qbman_cdan_ctrl_desc {
  824. u8 verb;
  825. u8 reserved;
  826. __le16 ch;
  827. u8 we;
  828. u8 ctrl;
  829. __le16 reserved2;
  830. __le64 cdan_ctx;
  831. u8 reserved3[48];
  832. };
  833. struct qbman_cdan_ctrl_rslt {
  834. u8 verb;
  835. u8 rslt;
  836. __le16 ch;
  837. u8 reserved[60];
  838. };
  839. int qbman_swp_CDAN_set(struct qbman_swp *s, u16 channelid,
  840. u8 we_mask, u8 cdan_en,
  841. u64 ctx)
  842. {
  843. struct qbman_cdan_ctrl_desc *p = NULL;
  844. struct qbman_cdan_ctrl_rslt *r = NULL;
  845. /* Start the management command */
  846. p = qbman_swp_mc_start(s);
  847. if (!p)
  848. return -EBUSY;
  849. /* Encode the caller-provided attributes */
  850. p->ch = cpu_to_le16(channelid);
  851. p->we = we_mask;
  852. if (cdan_en)
  853. p->ctrl = 1;
  854. else
  855. p->ctrl = 0;
  856. p->cdan_ctx = cpu_to_le64(ctx);
  857. /* Complete the management command */
  858. r = qbman_swp_mc_complete(s, p, QBMAN_WQCHAN_CONFIGURE);
  859. if (unlikely(!r)) {
  860. pr_err("qbman: wqchan config failed, no response\n");
  861. return -EIO;
  862. }
  863. WARN_ON((r->verb & 0x7f) != QBMAN_WQCHAN_CONFIGURE);
  864. /* Determine success or failure */
  865. if (unlikely(r->rslt != QBMAN_MC_RSLT_OK)) {
  866. pr_err("qbman: CDAN cQID %d failed: code = 0x%02x\n",
  867. channelid, r->rslt);
  868. return -EIO;
  869. }
  870. return 0;
  871. }