dpio-service.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * Copyright 2014-2016 Freescale Semiconductor Inc.
  4. * Copyright 2016 NXP
  5. *
  6. */
  7. #include <linux/types.h>
  8. #include <linux/fsl/mc.h>
  9. #include <soc/fsl/dpaa2-io.h>
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/slab.h>
  16. #include "dpio.h"
  17. #include "qbman-portal.h"
  18. struct dpaa2_io {
  19. struct dpaa2_io_desc dpio_desc;
  20. struct qbman_swp_desc swp_desc;
  21. struct qbman_swp *swp;
  22. struct list_head node;
  23. /* protect against multiple management commands */
  24. spinlock_t lock_mgmt_cmd;
  25. /* protect notifications list */
  26. spinlock_t lock_notifications;
  27. struct list_head notifications;
  28. };
  29. struct dpaa2_io_store {
  30. unsigned int max;
  31. dma_addr_t paddr;
  32. struct dpaa2_dq *vaddr;
  33. void *alloced_addr; /* unaligned value from kmalloc() */
  34. unsigned int idx; /* position of the next-to-be-returned entry */
  35. struct qbman_swp *swp; /* portal used to issue VDQCR */
  36. struct device *dev; /* device used for DMA mapping */
  37. };
  38. /* keep a per cpu array of DPIOs for fast access */
  39. static struct dpaa2_io *dpio_by_cpu[NR_CPUS];
  40. static struct list_head dpio_list = LIST_HEAD_INIT(dpio_list);
  41. static DEFINE_SPINLOCK(dpio_list_lock);
  42. static inline struct dpaa2_io *service_select_by_cpu(struct dpaa2_io *d,
  43. int cpu)
  44. {
  45. if (d)
  46. return d;
  47. if (cpu != DPAA2_IO_ANY_CPU && cpu >= num_possible_cpus())
  48. return NULL;
  49. /*
  50. * If cpu == -1, choose the current cpu, with no guarantees about
  51. * potentially being migrated away.
  52. */
  53. if (unlikely(cpu < 0))
  54. cpu = smp_processor_id();
  55. /* If a specific cpu was requested, pick it up immediately */
  56. return dpio_by_cpu[cpu];
  57. }
  58. static inline struct dpaa2_io *service_select(struct dpaa2_io *d)
  59. {
  60. if (d)
  61. return d;
  62. spin_lock(&dpio_list_lock);
  63. d = list_entry(dpio_list.next, struct dpaa2_io, node);
  64. list_del(&d->node);
  65. list_add_tail(&d->node, &dpio_list);
  66. spin_unlock(&dpio_list_lock);
  67. return d;
  68. }
  69. /**
  70. * dpaa2_io_service_select() - return a dpaa2_io service affined to this cpu
  71. * @cpu: the cpu id
  72. *
  73. * Return the affine dpaa2_io service, or NULL if there is no service affined
  74. * to the specified cpu. If DPAA2_IO_ANY_CPU is used, return the next available
  75. * service.
  76. */
  77. struct dpaa2_io *dpaa2_io_service_select(int cpu)
  78. {
  79. if (cpu == DPAA2_IO_ANY_CPU)
  80. return service_select(NULL);
  81. return service_select_by_cpu(NULL, cpu);
  82. }
  83. EXPORT_SYMBOL_GPL(dpaa2_io_service_select);
  84. /**
  85. * dpaa2_io_create() - create a dpaa2_io object.
  86. * @desc: the dpaa2_io descriptor
  87. *
  88. * Activates a "struct dpaa2_io" corresponding to the given config of an actual
  89. * DPIO object.
  90. *
  91. * Return a valid dpaa2_io object for success, or NULL for failure.
  92. */
  93. struct dpaa2_io *dpaa2_io_create(const struct dpaa2_io_desc *desc)
  94. {
  95. struct dpaa2_io *obj = kmalloc(sizeof(*obj), GFP_KERNEL);
  96. if (!obj)
  97. return NULL;
  98. /* check if CPU is out of range (-1 means any cpu) */
  99. if (desc->cpu != DPAA2_IO_ANY_CPU && desc->cpu >= num_possible_cpus()) {
  100. kfree(obj);
  101. return NULL;
  102. }
  103. obj->dpio_desc = *desc;
  104. obj->swp_desc.cena_bar = obj->dpio_desc.regs_cena;
  105. obj->swp_desc.cinh_bar = obj->dpio_desc.regs_cinh;
  106. obj->swp_desc.qman_version = obj->dpio_desc.qman_version;
  107. obj->swp = qbman_swp_init(&obj->swp_desc);
  108. if (!obj->swp) {
  109. kfree(obj);
  110. return NULL;
  111. }
  112. INIT_LIST_HEAD(&obj->node);
  113. spin_lock_init(&obj->lock_mgmt_cmd);
  114. spin_lock_init(&obj->lock_notifications);
  115. INIT_LIST_HEAD(&obj->notifications);
  116. /* For now only enable DQRR interrupts */
  117. qbman_swp_interrupt_set_trigger(obj->swp,
  118. QBMAN_SWP_INTERRUPT_DQRI);
  119. qbman_swp_interrupt_clear_status(obj->swp, 0xffffffff);
  120. if (obj->dpio_desc.receives_notifications)
  121. qbman_swp_push_set(obj->swp, 0, 1);
  122. spin_lock(&dpio_list_lock);
  123. list_add_tail(&obj->node, &dpio_list);
  124. if (desc->cpu >= 0 && !dpio_by_cpu[desc->cpu])
  125. dpio_by_cpu[desc->cpu] = obj;
  126. spin_unlock(&dpio_list_lock);
  127. return obj;
  128. }
  129. /**
  130. * dpaa2_io_down() - release the dpaa2_io object.
  131. * @d: the dpaa2_io object to be released.
  132. *
  133. * The "struct dpaa2_io" type can represent an individual DPIO object (as
  134. * described by "struct dpaa2_io_desc") or an instance of a "DPIO service",
  135. * which can be used to group/encapsulate multiple DPIO objects. In all cases,
  136. * each handle obtained should be released using this function.
  137. */
  138. void dpaa2_io_down(struct dpaa2_io *d)
  139. {
  140. kfree(d);
  141. }
  142. #define DPAA_POLL_MAX 32
  143. /**
  144. * dpaa2_io_irq() - ISR for DPIO interrupts
  145. *
  146. * @obj: the given DPIO object.
  147. *
  148. * Return IRQ_HANDLED for success or IRQ_NONE if there
  149. * were no pending interrupts.
  150. */
  151. irqreturn_t dpaa2_io_irq(struct dpaa2_io *obj)
  152. {
  153. const struct dpaa2_dq *dq;
  154. int max = 0;
  155. struct qbman_swp *swp;
  156. u32 status;
  157. swp = obj->swp;
  158. status = qbman_swp_interrupt_read_status(swp);
  159. if (!status)
  160. return IRQ_NONE;
  161. dq = qbman_swp_dqrr_next(swp);
  162. while (dq) {
  163. if (qbman_result_is_SCN(dq)) {
  164. struct dpaa2_io_notification_ctx *ctx;
  165. u64 q64;
  166. q64 = qbman_result_SCN_ctx(dq);
  167. ctx = (void *)(uintptr_t)q64;
  168. ctx->cb(ctx);
  169. } else {
  170. pr_crit("fsl-mc-dpio: Unrecognised/ignored DQRR entry\n");
  171. }
  172. qbman_swp_dqrr_consume(swp, dq);
  173. ++max;
  174. if (max > DPAA_POLL_MAX)
  175. goto done;
  176. dq = qbman_swp_dqrr_next(swp);
  177. }
  178. done:
  179. qbman_swp_interrupt_clear_status(swp, status);
  180. qbman_swp_interrupt_set_inhibit(swp, 0);
  181. return IRQ_HANDLED;
  182. }
  183. /**
  184. * dpaa2_io_service_register() - Prepare for servicing of FQDAN or CDAN
  185. * notifications on the given DPIO service.
  186. * @d: the given DPIO service.
  187. * @ctx: the notification context.
  188. *
  189. * The caller should make the MC command to attach a DPAA2 object to
  190. * a DPIO after this function completes successfully. In that way:
  191. * (a) The DPIO service is "ready" to handle a notification arrival
  192. * (which might happen before the "attach" command to MC has
  193. * returned control of execution back to the caller)
  194. * (b) The DPIO service can provide back to the caller the 'dpio_id' and
  195. * 'qman64' parameters that it should pass along in the MC command
  196. * in order for the object to be configured to produce the right
  197. * notification fields to the DPIO service.
  198. *
  199. * Return 0 for success, or -ENODEV for failure.
  200. */
  201. int dpaa2_io_service_register(struct dpaa2_io *d,
  202. struct dpaa2_io_notification_ctx *ctx)
  203. {
  204. unsigned long irqflags;
  205. d = service_select_by_cpu(d, ctx->desired_cpu);
  206. if (!d)
  207. return -ENODEV;
  208. ctx->dpio_id = d->dpio_desc.dpio_id;
  209. ctx->qman64 = (u64)(uintptr_t)ctx;
  210. ctx->dpio_private = d;
  211. spin_lock_irqsave(&d->lock_notifications, irqflags);
  212. list_add(&ctx->node, &d->notifications);
  213. spin_unlock_irqrestore(&d->lock_notifications, irqflags);
  214. /* Enable the generation of CDAN notifications */
  215. if (ctx->is_cdan)
  216. return qbman_swp_CDAN_set_context_enable(d->swp,
  217. (u16)ctx->id,
  218. ctx->qman64);
  219. return 0;
  220. }
  221. EXPORT_SYMBOL_GPL(dpaa2_io_service_register);
  222. /**
  223. * dpaa2_io_service_deregister - The opposite of 'register'.
  224. * @service: the given DPIO service.
  225. * @ctx: the notification context.
  226. *
  227. * This function should be called only after sending the MC command to
  228. * to detach the notification-producing device from the DPIO.
  229. */
  230. void dpaa2_io_service_deregister(struct dpaa2_io *service,
  231. struct dpaa2_io_notification_ctx *ctx)
  232. {
  233. struct dpaa2_io *d = ctx->dpio_private;
  234. unsigned long irqflags;
  235. if (ctx->is_cdan)
  236. qbman_swp_CDAN_disable(d->swp, (u16)ctx->id);
  237. spin_lock_irqsave(&d->lock_notifications, irqflags);
  238. list_del(&ctx->node);
  239. spin_unlock_irqrestore(&d->lock_notifications, irqflags);
  240. }
  241. EXPORT_SYMBOL_GPL(dpaa2_io_service_deregister);
  242. /**
  243. * dpaa2_io_service_rearm() - Rearm the notification for the given DPIO service.
  244. * @d: the given DPIO service.
  245. * @ctx: the notification context.
  246. *
  247. * Once a FQDAN/CDAN has been produced, the corresponding FQ/channel is
  248. * considered "disarmed". Ie. the user can issue pull dequeue operations on that
  249. * traffic source for as long as it likes. Eventually it may wish to "rearm"
  250. * that source to allow it to produce another FQDAN/CDAN, that's what this
  251. * function achieves.
  252. *
  253. * Return 0 for success.
  254. */
  255. int dpaa2_io_service_rearm(struct dpaa2_io *d,
  256. struct dpaa2_io_notification_ctx *ctx)
  257. {
  258. unsigned long irqflags;
  259. int err;
  260. d = service_select_by_cpu(d, ctx->desired_cpu);
  261. if (!unlikely(d))
  262. return -ENODEV;
  263. spin_lock_irqsave(&d->lock_mgmt_cmd, irqflags);
  264. if (ctx->is_cdan)
  265. err = qbman_swp_CDAN_enable(d->swp, (u16)ctx->id);
  266. else
  267. err = qbman_swp_fq_schedule(d->swp, ctx->id);
  268. spin_unlock_irqrestore(&d->lock_mgmt_cmd, irqflags);
  269. return err;
  270. }
  271. EXPORT_SYMBOL_GPL(dpaa2_io_service_rearm);
  272. /**
  273. * dpaa2_io_service_pull_channel() - pull dequeue functions from a channel.
  274. * @d: the given DPIO service.
  275. * @channelid: the given channel id.
  276. * @s: the dpaa2_io_store object for the result.
  277. *
  278. * Return 0 for success, or error code for failure.
  279. */
  280. int dpaa2_io_service_pull_channel(struct dpaa2_io *d, u32 channelid,
  281. struct dpaa2_io_store *s)
  282. {
  283. struct qbman_pull_desc pd;
  284. int err;
  285. qbman_pull_desc_clear(&pd);
  286. qbman_pull_desc_set_storage(&pd, s->vaddr, s->paddr, 1);
  287. qbman_pull_desc_set_numframes(&pd, (u8)s->max);
  288. qbman_pull_desc_set_channel(&pd, channelid, qbman_pull_type_prio);
  289. d = service_select(d);
  290. if (!d)
  291. return -ENODEV;
  292. s->swp = d->swp;
  293. err = qbman_swp_pull(d->swp, &pd);
  294. if (err)
  295. s->swp = NULL;
  296. return err;
  297. }
  298. EXPORT_SYMBOL_GPL(dpaa2_io_service_pull_channel);
  299. /**
  300. * dpaa2_io_service_enqueue_qd() - Enqueue a frame to a QD.
  301. * @d: the given DPIO service.
  302. * @qdid: the given queuing destination id.
  303. * @prio: the given queuing priority.
  304. * @qdbin: the given queuing destination bin.
  305. * @fd: the frame descriptor which is enqueued.
  306. *
  307. * Return 0 for successful enqueue, or -EBUSY if the enqueue ring is not ready,
  308. * or -ENODEV if there is no dpio service.
  309. */
  310. int dpaa2_io_service_enqueue_qd(struct dpaa2_io *d,
  311. u32 qdid, u8 prio, u16 qdbin,
  312. const struct dpaa2_fd *fd)
  313. {
  314. struct qbman_eq_desc ed;
  315. d = service_select(d);
  316. if (!d)
  317. return -ENODEV;
  318. qbman_eq_desc_clear(&ed);
  319. qbman_eq_desc_set_no_orp(&ed, 0);
  320. qbman_eq_desc_set_qd(&ed, qdid, qdbin, prio);
  321. return qbman_swp_enqueue(d->swp, &ed, fd);
  322. }
  323. EXPORT_SYMBOL_GPL(dpaa2_io_service_enqueue_qd);
  324. /**
  325. * dpaa2_io_service_release() - Release buffers to a buffer pool.
  326. * @d: the given DPIO object.
  327. * @bpid: the buffer pool id.
  328. * @buffers: the buffers to be released.
  329. * @num_buffers: the number of the buffers to be released.
  330. *
  331. * Return 0 for success, and negative error code for failure.
  332. */
  333. int dpaa2_io_service_release(struct dpaa2_io *d,
  334. u32 bpid,
  335. const u64 *buffers,
  336. unsigned int num_buffers)
  337. {
  338. struct qbman_release_desc rd;
  339. d = service_select(d);
  340. if (!d)
  341. return -ENODEV;
  342. qbman_release_desc_clear(&rd);
  343. qbman_release_desc_set_bpid(&rd, bpid);
  344. return qbman_swp_release(d->swp, &rd, buffers, num_buffers);
  345. }
  346. EXPORT_SYMBOL_GPL(dpaa2_io_service_release);
  347. /**
  348. * dpaa2_io_service_acquire() - Acquire buffers from a buffer pool.
  349. * @d: the given DPIO object.
  350. * @bpid: the buffer pool id.
  351. * @buffers: the buffer addresses for acquired buffers.
  352. * @num_buffers: the expected number of the buffers to acquire.
  353. *
  354. * Return a negative error code if the command failed, otherwise it returns
  355. * the number of buffers acquired, which may be less than the number requested.
  356. * Eg. if the buffer pool is empty, this will return zero.
  357. */
  358. int dpaa2_io_service_acquire(struct dpaa2_io *d,
  359. u32 bpid,
  360. u64 *buffers,
  361. unsigned int num_buffers)
  362. {
  363. unsigned long irqflags;
  364. int err;
  365. d = service_select(d);
  366. if (!d)
  367. return -ENODEV;
  368. spin_lock_irqsave(&d->lock_mgmt_cmd, irqflags);
  369. err = qbman_swp_acquire(d->swp, bpid, buffers, num_buffers);
  370. spin_unlock_irqrestore(&d->lock_mgmt_cmd, irqflags);
  371. return err;
  372. }
  373. EXPORT_SYMBOL_GPL(dpaa2_io_service_acquire);
  374. /*
  375. * 'Stores' are reusable memory blocks for holding dequeue results, and to
  376. * assist with parsing those results.
  377. */
  378. /**
  379. * dpaa2_io_store_create() - Create the dma memory storage for dequeue result.
  380. * @max_frames: the maximum number of dequeued result for frames, must be <= 16.
  381. * @dev: the device to allow mapping/unmapping the DMAable region.
  382. *
  383. * The size of the storage is "max_frames*sizeof(struct dpaa2_dq)".
  384. * The 'dpaa2_io_store' returned is a DPIO service managed object.
  385. *
  386. * Return pointer to dpaa2_io_store struct for successfully created storage
  387. * memory, or NULL on error.
  388. */
  389. struct dpaa2_io_store *dpaa2_io_store_create(unsigned int max_frames,
  390. struct device *dev)
  391. {
  392. struct dpaa2_io_store *ret;
  393. size_t size;
  394. if (!max_frames || (max_frames > 16))
  395. return NULL;
  396. ret = kmalloc(sizeof(*ret), GFP_KERNEL);
  397. if (!ret)
  398. return NULL;
  399. ret->max = max_frames;
  400. size = max_frames * sizeof(struct dpaa2_dq) + 64;
  401. ret->alloced_addr = kzalloc(size, GFP_KERNEL);
  402. if (!ret->alloced_addr) {
  403. kfree(ret);
  404. return NULL;
  405. }
  406. ret->vaddr = PTR_ALIGN(ret->alloced_addr, 64);
  407. ret->paddr = dma_map_single(dev, ret->vaddr,
  408. sizeof(struct dpaa2_dq) * max_frames,
  409. DMA_FROM_DEVICE);
  410. if (dma_mapping_error(dev, ret->paddr)) {
  411. kfree(ret->alloced_addr);
  412. kfree(ret);
  413. return NULL;
  414. }
  415. ret->idx = 0;
  416. ret->dev = dev;
  417. return ret;
  418. }
  419. EXPORT_SYMBOL_GPL(dpaa2_io_store_create);
  420. /**
  421. * dpaa2_io_store_destroy() - Frees the dma memory storage for dequeue
  422. * result.
  423. * @s: the storage memory to be destroyed.
  424. */
  425. void dpaa2_io_store_destroy(struct dpaa2_io_store *s)
  426. {
  427. dma_unmap_single(s->dev, s->paddr, sizeof(struct dpaa2_dq) * s->max,
  428. DMA_FROM_DEVICE);
  429. kfree(s->alloced_addr);
  430. kfree(s);
  431. }
  432. EXPORT_SYMBOL_GPL(dpaa2_io_store_destroy);
  433. /**
  434. * dpaa2_io_store_next() - Determine when the next dequeue result is available.
  435. * @s: the dpaa2_io_store object.
  436. * @is_last: indicate whether this is the last frame in the pull command.
  437. *
  438. * When an object driver performs dequeues to a dpaa2_io_store, this function
  439. * can be used to determine when the next frame result is available. Once
  440. * this function returns non-NULL, a subsequent call to it will try to find
  441. * the next dequeue result.
  442. *
  443. * Note that if a pull-dequeue has a NULL result because the target FQ/channel
  444. * was empty, then this function will also return NULL (rather than expecting
  445. * the caller to always check for this. As such, "is_last" can be used to
  446. * differentiate between "end-of-empty-dequeue" and "still-waiting".
  447. *
  448. * Return dequeue result for a valid dequeue result, or NULL for empty dequeue.
  449. */
  450. struct dpaa2_dq *dpaa2_io_store_next(struct dpaa2_io_store *s, int *is_last)
  451. {
  452. int match;
  453. struct dpaa2_dq *ret = &s->vaddr[s->idx];
  454. match = qbman_result_has_new_result(s->swp, ret);
  455. if (!match) {
  456. *is_last = 0;
  457. return NULL;
  458. }
  459. s->idx++;
  460. if (dpaa2_dq_is_pull_complete(ret)) {
  461. *is_last = 1;
  462. s->idx = 0;
  463. /*
  464. * If we get an empty dequeue result to terminate a zero-results
  465. * vdqcr, return NULL to the caller rather than expecting him to
  466. * check non-NULL results every time.
  467. */
  468. if (!(dpaa2_dq_flags(ret) & DPAA2_DQ_STAT_VALIDFRAME))
  469. ret = NULL;
  470. } else {
  471. *is_last = 0;
  472. }
  473. return ret;
  474. }
  475. EXPORT_SYMBOL_GPL(dpaa2_io_store_next);