1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
3 * Copyright 2014-2016 Freescale Semiconductor Inc.
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>
18 #include "qbman-portal.h"
21 struct dpaa2_io_desc dpio_desc;
22 struct qbman_swp_desc swp_desc;
23 struct qbman_swp *swp;
24 struct list_head node;
25 /* protect against multiple management commands */
26 spinlock_t lock_mgmt_cmd;
27 /* protect notifications list */
28 spinlock_t lock_notifications;
29 struct list_head notifications;
32 struct dpaa2_io_store {
35 struct dpaa2_dq *vaddr;
36 void *alloced_addr; /* unaligned value from kmalloc() */
37 unsigned int idx; /* position of the next-to-be-returned entry */
38 struct qbman_swp *swp; /* portal used to issue VDQCR */
39 struct device *dev; /* device used for DMA mapping */
42 /* keep a per cpu array of DPIOs for fast access */
43 static struct dpaa2_io *dpio_by_cpu[NR_CPUS];
44 static struct list_head dpio_list = LIST_HEAD_INIT(dpio_list);
45 static DEFINE_SPINLOCK(dpio_list_lock);
47 static inline struct dpaa2_io *service_select_by_cpu(struct dpaa2_io *d,
53 if (cpu != DPAA2_IO_ANY_CPU && cpu >= num_possible_cpus())
57 * If cpu == -1, choose the current cpu, with no guarantees about
58 * potentially being migrated away.
60 if (unlikely(cpu < 0))
61 cpu = smp_processor_id();
63 /* If a specific cpu was requested, pick it up immediately */
64 return dpio_by_cpu[cpu];
67 static inline struct dpaa2_io *service_select(struct dpaa2_io *d)
72 spin_lock(&dpio_list_lock);
73 d = list_entry(dpio_list.next, struct dpaa2_io, node);
75 list_add_tail(&d->node, &dpio_list);
76 spin_unlock(&dpio_list_lock);
82 * dpaa2_io_service_select() - return a dpaa2_io service affined to this cpu
85 * Return the affine dpaa2_io service, or NULL if there is no service affined
86 * to the specified cpu. If DPAA2_IO_ANY_CPU is used, return the next available
89 struct dpaa2_io *dpaa2_io_service_select(int cpu)
91 if (cpu == DPAA2_IO_ANY_CPU)
92 return service_select(NULL);
94 return service_select_by_cpu(NULL, cpu);
96 EXPORT_SYMBOL_GPL(dpaa2_io_service_select);
99 * dpaa2_io_create() - create a dpaa2_io object.
100 * @desc: the dpaa2_io descriptor
102 * Activates a "struct dpaa2_io" corresponding to the given config of an actual
105 * Return a valid dpaa2_io object for success, or NULL for failure.
107 struct dpaa2_io *dpaa2_io_create(const struct dpaa2_io_desc *desc)
109 struct dpaa2_io *obj = kmalloc(sizeof(*obj), GFP_KERNEL);
114 /* check if CPU is out of range (-1 means any cpu) */
115 if (desc->cpu != DPAA2_IO_ANY_CPU && desc->cpu >= num_possible_cpus()) {
120 obj->dpio_desc = *desc;
121 obj->swp_desc.cena_bar = obj->dpio_desc.regs_cena;
122 obj->swp_desc.cinh_bar = obj->dpio_desc.regs_cinh;
123 obj->swp_desc.qman_version = obj->dpio_desc.qman_version;
124 obj->swp = qbman_swp_init(&obj->swp_desc);
131 INIT_LIST_HEAD(&obj->node);
132 spin_lock_init(&obj->lock_mgmt_cmd);
133 spin_lock_init(&obj->lock_notifications);
134 INIT_LIST_HEAD(&obj->notifications);
136 /* For now only enable DQRR interrupts */
137 qbman_swp_interrupt_set_trigger(obj->swp,
138 QBMAN_SWP_INTERRUPT_DQRI);
139 qbman_swp_interrupt_clear_status(obj->swp, 0xffffffff);
140 if (obj->dpio_desc.receives_notifications)
141 qbman_swp_push_set(obj->swp, 0, 1);
143 spin_lock(&dpio_list_lock);
144 list_add_tail(&obj->node, &dpio_list);
145 if (desc->cpu >= 0 && !dpio_by_cpu[desc->cpu])
146 dpio_by_cpu[desc->cpu] = obj;
147 spin_unlock(&dpio_list_lock);
153 * dpaa2_io_down() - release the dpaa2_io object.
154 * @d: the dpaa2_io object to be released.
156 * The "struct dpaa2_io" type can represent an individual DPIO object (as
157 * described by "struct dpaa2_io_desc") or an instance of a "DPIO service",
158 * which can be used to group/encapsulate multiple DPIO objects. In all cases,
159 * each handle obtained should be released using this function.
161 void dpaa2_io_down(struct dpaa2_io *d)
166 #define DPAA_POLL_MAX 32
169 * dpaa2_io_irq() - ISR for DPIO interrupts
171 * @obj: the given DPIO object.
173 * Return IRQ_HANDLED for success or IRQ_NONE if there
174 * were no pending interrupts.
176 irqreturn_t dpaa2_io_irq(struct dpaa2_io *obj)
178 const struct dpaa2_dq *dq;
180 struct qbman_swp *swp;
184 status = qbman_swp_interrupt_read_status(swp);
188 dq = qbman_swp_dqrr_next(swp);
190 if (qbman_result_is_SCN(dq)) {
191 struct dpaa2_io_notification_ctx *ctx;
194 q64 = qbman_result_SCN_ctx(dq);
195 ctx = (void *)(uintptr_t)q64;
198 pr_crit("fsl-mc-dpio: Unrecognised/ignored DQRR entry\n");
200 qbman_swp_dqrr_consume(swp, dq);
202 if (max > DPAA_POLL_MAX)
204 dq = qbman_swp_dqrr_next(swp);
207 qbman_swp_interrupt_clear_status(swp, status);
208 qbman_swp_interrupt_set_inhibit(swp, 0);
213 * dpaa2_io_service_register() - Prepare for servicing of FQDAN or CDAN
214 * notifications on the given DPIO service.
215 * @d: the given DPIO service.
216 * @ctx: the notification context.
218 * The caller should make the MC command to attach a DPAA2 object to
219 * a DPIO after this function completes successfully. In that way:
220 * (a) The DPIO service is "ready" to handle a notification arrival
221 * (which might happen before the "attach" command to MC has
222 * returned control of execution back to the caller)
223 * (b) The DPIO service can provide back to the caller the 'dpio_id' and
224 * 'qman64' parameters that it should pass along in the MC command
225 * in order for the object to be configured to produce the right
226 * notification fields to the DPIO service.
228 * Return 0 for success, or -ENODEV for failure.
230 int dpaa2_io_service_register(struct dpaa2_io *d,
231 struct dpaa2_io_notification_ctx *ctx)
233 unsigned long irqflags;
235 d = service_select_by_cpu(d, ctx->desired_cpu);
239 ctx->dpio_id = d->dpio_desc.dpio_id;
240 ctx->qman64 = (u64)(uintptr_t)ctx;
241 ctx->dpio_private = d;
242 spin_lock_irqsave(&d->lock_notifications, irqflags);
243 list_add(&ctx->node, &d->notifications);
244 spin_unlock_irqrestore(&d->lock_notifications, irqflags);
246 /* Enable the generation of CDAN notifications */
248 return qbman_swp_CDAN_set_context_enable(d->swp,
253 EXPORT_SYMBOL_GPL(dpaa2_io_service_register);
256 * dpaa2_io_service_deregister - The opposite of 'register'.
257 * @service: the given DPIO service.
258 * @ctx: the notification context.
260 * This function should be called only after sending the MC command to
261 * to detach the notification-producing device from the DPIO.
263 void dpaa2_io_service_deregister(struct dpaa2_io *service,
264 struct dpaa2_io_notification_ctx *ctx)
266 struct dpaa2_io *d = ctx->dpio_private;
267 unsigned long irqflags;
270 qbman_swp_CDAN_disable(d->swp, (u16)ctx->id);
272 spin_lock_irqsave(&d->lock_notifications, irqflags);
273 list_del(&ctx->node);
274 spin_unlock_irqrestore(&d->lock_notifications, irqflags);
276 EXPORT_SYMBOL_GPL(dpaa2_io_service_deregister);
279 * dpaa2_io_service_rearm() - Rearm the notification for the given DPIO service.
280 * @d: the given DPIO service.
281 * @ctx: the notification context.
283 * Once a FQDAN/CDAN has been produced, the corresponding FQ/channel is
284 * considered "disarmed". Ie. the user can issue pull dequeue operations on that
285 * traffic source for as long as it likes. Eventually it may wish to "rearm"
286 * that source to allow it to produce another FQDAN/CDAN, that's what this
289 * Return 0 for success.
291 int dpaa2_io_service_rearm(struct dpaa2_io *d,
292 struct dpaa2_io_notification_ctx *ctx)
294 unsigned long irqflags;
297 d = service_select_by_cpu(d, ctx->desired_cpu);
301 spin_lock_irqsave(&d->lock_mgmt_cmd, irqflags);
303 err = qbman_swp_CDAN_enable(d->swp, (u16)ctx->id);
305 err = qbman_swp_fq_schedule(d->swp, ctx->id);
306 spin_unlock_irqrestore(&d->lock_mgmt_cmd, irqflags);
310 EXPORT_SYMBOL_GPL(dpaa2_io_service_rearm);
313 * dpaa2_io_service_pull_fq() - pull dequeue functions from a fq.
314 * @d: the given DPIO service.
315 * @fqid: the given frame queue id.
316 * @s: the dpaa2_io_store object for the result.
318 * Return 0 for success, or error code for failure.
320 int dpaa2_io_service_pull_fq(struct dpaa2_io *d, u32 fqid,
321 struct dpaa2_io_store *s)
323 struct qbman_pull_desc pd;
326 qbman_pull_desc_clear(&pd);
327 qbman_pull_desc_set_storage(&pd, s->vaddr, s->paddr, 1);
328 qbman_pull_desc_set_numframes(&pd, (u8)s->max);
329 qbman_pull_desc_set_fq(&pd, fqid);
331 d = service_select(d);
335 err = qbman_swp_pull(d->swp, &pd);
341 EXPORT_SYMBOL(dpaa2_io_service_pull_fq);
344 * dpaa2_io_service_pull_channel() - pull dequeue functions from a channel.
345 * @d: the given DPIO service.
346 * @channelid: the given channel id.
347 * @s: the dpaa2_io_store object for the result.
349 * Return 0 for success, or error code for failure.
351 int dpaa2_io_service_pull_channel(struct dpaa2_io *d, u32 channelid,
352 struct dpaa2_io_store *s)
354 struct qbman_pull_desc pd;
357 qbman_pull_desc_clear(&pd);
358 qbman_pull_desc_set_storage(&pd, s->vaddr, s->paddr, 1);
359 qbman_pull_desc_set_numframes(&pd, (u8)s->max);
360 qbman_pull_desc_set_channel(&pd, channelid, qbman_pull_type_prio);
362 d = service_select(d);
367 err = qbman_swp_pull(d->swp, &pd);
373 EXPORT_SYMBOL_GPL(dpaa2_io_service_pull_channel);
376 * dpaa2_io_service_enqueue_fq() - Enqueue a frame to a frame queue.
377 * @d: the given DPIO service.
378 * @fqid: the given frame queue id.
379 * @fd: the frame descriptor which is enqueued.
381 * Return 0 for successful enqueue, -EBUSY if the enqueue ring is not ready,
382 * or -ENODEV if there is no dpio service.
384 int dpaa2_io_service_enqueue_fq(struct dpaa2_io *d,
386 const struct dpaa2_fd *fd)
388 struct qbman_eq_desc ed;
390 d = service_select(d);
394 qbman_eq_desc_clear(&ed);
395 qbman_eq_desc_set_no_orp(&ed, 0);
396 qbman_eq_desc_set_fq(&ed, fqid);
398 return qbman_swp_enqueue(d->swp, &ed, fd);
400 EXPORT_SYMBOL(dpaa2_io_service_enqueue_fq);
403 * dpaa2_io_service_enqueue_qd() - Enqueue a frame to a QD.
404 * @d: the given DPIO service.
405 * @qdid: the given queuing destination id.
406 * @prio: the given queuing priority.
407 * @qdbin: the given queuing destination bin.
408 * @fd: the frame descriptor which is enqueued.
410 * Return 0 for successful enqueue, or -EBUSY if the enqueue ring is not ready,
411 * or -ENODEV if there is no dpio service.
413 int dpaa2_io_service_enqueue_qd(struct dpaa2_io *d,
414 u32 qdid, u8 prio, u16 qdbin,
415 const struct dpaa2_fd *fd)
417 struct qbman_eq_desc ed;
419 d = service_select(d);
423 qbman_eq_desc_clear(&ed);
424 qbman_eq_desc_set_no_orp(&ed, 0);
425 qbman_eq_desc_set_qd(&ed, qdid, qdbin, prio);
427 return qbman_swp_enqueue(d->swp, &ed, fd);
429 EXPORT_SYMBOL_GPL(dpaa2_io_service_enqueue_qd);
432 * dpaa2_io_service_release() - Release buffers to a buffer pool.
433 * @d: the given DPIO object.
434 * @bpid: the buffer pool id.
435 * @buffers: the buffers to be released.
436 * @num_buffers: the number of the buffers to be released.
438 * Return 0 for success, and negative error code for failure.
440 int dpaa2_io_service_release(struct dpaa2_io *d,
443 unsigned int num_buffers)
445 struct qbman_release_desc rd;
447 d = service_select(d);
451 qbman_release_desc_clear(&rd);
452 qbman_release_desc_set_bpid(&rd, bpid);
454 return qbman_swp_release(d->swp, &rd, buffers, num_buffers);
456 EXPORT_SYMBOL_GPL(dpaa2_io_service_release);
459 * dpaa2_io_service_acquire() - Acquire buffers from a buffer pool.
460 * @d: the given DPIO object.
461 * @bpid: the buffer pool id.
462 * @buffers: the buffer addresses for acquired buffers.
463 * @num_buffers: the expected number of the buffers to acquire.
465 * Return a negative error code if the command failed, otherwise it returns
466 * the number of buffers acquired, which may be less than the number requested.
467 * Eg. if the buffer pool is empty, this will return zero.
469 int dpaa2_io_service_acquire(struct dpaa2_io *d,
472 unsigned int num_buffers)
474 unsigned long irqflags;
477 d = service_select(d);
481 spin_lock_irqsave(&d->lock_mgmt_cmd, irqflags);
482 err = qbman_swp_acquire(d->swp, bpid, buffers, num_buffers);
483 spin_unlock_irqrestore(&d->lock_mgmt_cmd, irqflags);
487 EXPORT_SYMBOL_GPL(dpaa2_io_service_acquire);
490 * 'Stores' are reusable memory blocks for holding dequeue results, and to
491 * assist with parsing those results.
495 * dpaa2_io_store_create() - Create the dma memory storage for dequeue result.
496 * @max_frames: the maximum number of dequeued result for frames, must be <= 16.
497 * @dev: the device to allow mapping/unmapping the DMAable region.
499 * The size of the storage is "max_frames*sizeof(struct dpaa2_dq)".
500 * The 'dpaa2_io_store' returned is a DPIO service managed object.
502 * Return pointer to dpaa2_io_store struct for successfully created storage
503 * memory, or NULL on error.
505 struct dpaa2_io_store *dpaa2_io_store_create(unsigned int max_frames,
508 struct dpaa2_io_store *ret;
511 if (!max_frames || (max_frames > 16))
514 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
518 ret->max = max_frames;
519 size = max_frames * sizeof(struct dpaa2_dq) + 64;
520 ret->alloced_addr = kzalloc(size, GFP_KERNEL);
521 if (!ret->alloced_addr) {
526 ret->vaddr = PTR_ALIGN(ret->alloced_addr, 64);
527 ret->paddr = dma_map_single(dev, ret->vaddr,
528 sizeof(struct dpaa2_dq) * max_frames,
530 if (dma_mapping_error(dev, ret->paddr)) {
531 kfree(ret->alloced_addr);
541 EXPORT_SYMBOL_GPL(dpaa2_io_store_create);
544 * dpaa2_io_store_destroy() - Frees the dma memory storage for dequeue
546 * @s: the storage memory to be destroyed.
548 void dpaa2_io_store_destroy(struct dpaa2_io_store *s)
550 dma_unmap_single(s->dev, s->paddr, sizeof(struct dpaa2_dq) * s->max,
552 kfree(s->alloced_addr);
555 EXPORT_SYMBOL_GPL(dpaa2_io_store_destroy);
558 * dpaa2_io_store_next() - Determine when the next dequeue result is available.
559 * @s: the dpaa2_io_store object.
560 * @is_last: indicate whether this is the last frame in the pull command.
562 * When an object driver performs dequeues to a dpaa2_io_store, this function
563 * can be used to determine when the next frame result is available. Once
564 * this function returns non-NULL, a subsequent call to it will try to find
565 * the next dequeue result.
567 * Note that if a pull-dequeue has a NULL result because the target FQ/channel
568 * was empty, then this function will also return NULL (rather than expecting
569 * the caller to always check for this. As such, "is_last" can be used to
570 * differentiate between "end-of-empty-dequeue" and "still-waiting".
572 * Return dequeue result for a valid dequeue result, or NULL for empty dequeue.
574 struct dpaa2_dq *dpaa2_io_store_next(struct dpaa2_io_store *s, int *is_last)
577 struct dpaa2_dq *ret = &s->vaddr[s->idx];
579 match = qbman_result_has_new_result(s->swp, ret);
587 if (dpaa2_dq_is_pull_complete(ret)) {
591 * If we get an empty dequeue result to terminate a zero-results
592 * vdqcr, return NULL to the caller rather than expecting him to
593 * check non-NULL results every time.
595 if (!(dpaa2_dq_flags(ret) & DPAA2_DQ_STAT_VALIDFRAME))
603 EXPORT_SYMBOL_GPL(dpaa2_io_store_next);
606 * dpaa2_io_query_fq_count() - Get the frame and byte count for a given fq.
607 * @d: the given DPIO object.
608 * @fqid: the id of frame queue to be queried.
609 * @fcnt: the queried frame count.
610 * @bcnt: the queried byte count.
612 * Knowing the FQ count at run-time can be useful in debugging situations.
613 * The instantaneous frame- and byte-count are hereby returned.
615 * Return 0 for a successful query, and negative error code if query fails.
617 int dpaa2_io_query_fq_count(struct dpaa2_io *d, u32 fqid,
618 u32 *fcnt, u32 *bcnt)
620 struct qbman_fq_query_np_rslt state;
621 struct qbman_swp *swp;
622 unsigned long irqflags;
625 d = service_select(d);
630 spin_lock_irqsave(&d->lock_mgmt_cmd, irqflags);
631 ret = qbman_fq_query_state(swp, fqid, &state);
632 spin_unlock_irqrestore(&d->lock_mgmt_cmd, irqflags);
635 *fcnt = qbman_fq_state_frame_count(&state);
636 *bcnt = qbman_fq_state_byte_count(&state);
640 EXPORT_SYMBOL_GPL(dpaa2_io_query_fq_count);
643 * dpaa2_io_query_bp_count() - Query the number of buffers currently in a
645 * @d: the given DPIO object.
646 * @bpid: the index of buffer pool to be queried.
647 * @num: the queried number of buffers in the buffer pool.
649 * Return 0 for a successful query, and negative error code if query fails.
651 int dpaa2_io_query_bp_count(struct dpaa2_io *d, u16 bpid, u32 *num)
653 struct qbman_bp_query_rslt state;
654 struct qbman_swp *swp;
655 unsigned long irqflags;
658 d = service_select(d);
663 spin_lock_irqsave(&d->lock_mgmt_cmd, irqflags);
664 ret = qbman_bp_query(swp, bpid, &state);
665 spin_unlock_irqrestore(&d->lock_mgmt_cmd, irqflags);
668 *num = qbman_bp_info_num_free_bufs(&state);
671 EXPORT_SYMBOL_GPL(dpaa2_io_query_bp_count);