1 // SPDX-License-Identifier: GPL-2.0-only
3 * Keystone Queue Manager subsystem driver
5 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com
6 * Authors: Sandeep Nair <sandeep_n@ti.com>
7 * Cyril Chemparathy <cyril@ti.com>
8 * Santosh Shilimkar <santosh.shilimkar@ti.com>
11 #include <linux/debugfs.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/firmware.h>
14 #include <linux/interrupt.h>
16 #include <linux/module.h>
17 #include <linux/of_address.h>
18 #include <linux/of_device.h>
19 #include <linux/of_irq.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/slab.h>
22 #include <linux/soc/ti/knav_qmss.h>
24 #include "knav_qmss.h"
26 static struct knav_device
*kdev
;
27 static DEFINE_MUTEX(knav_dev_lock
);
28 #define knav_dev_lock_held() \
29 lockdep_is_held(&knav_dev_lock)
31 /* Queue manager register indices in DTS */
32 #define KNAV_QUEUE_PEEK_REG_INDEX 0
33 #define KNAV_QUEUE_STATUS_REG_INDEX 1
34 #define KNAV_QUEUE_CONFIG_REG_INDEX 2
35 #define KNAV_QUEUE_REGION_REG_INDEX 3
36 #define KNAV_QUEUE_PUSH_REG_INDEX 4
37 #define KNAV_QUEUE_POP_REG_INDEX 5
39 /* Queue manager register indices in DTS for QMSS in K2G NAVSS.
40 * There are no status and vbusm push registers on this version
41 * of QMSS. Push registers are same as pop, So all indices above 1
42 * are to be re-defined
44 #define KNAV_L_QUEUE_CONFIG_REG_INDEX 1
45 #define KNAV_L_QUEUE_REGION_REG_INDEX 2
46 #define KNAV_L_QUEUE_PUSH_REG_INDEX 3
48 /* PDSP register indices in DTS */
49 #define KNAV_QUEUE_PDSP_IRAM_REG_INDEX 0
50 #define KNAV_QUEUE_PDSP_REGS_REG_INDEX 1
51 #define KNAV_QUEUE_PDSP_INTD_REG_INDEX 2
52 #define KNAV_QUEUE_PDSP_CMD_REG_INDEX 3
54 #define knav_queue_idx_to_inst(kdev, idx) \
55 (kdev->instances + (idx << kdev->inst_shift))
57 #define for_each_handle_rcu(qh, inst) \
58 list_for_each_entry_rcu(qh, &inst->handles, list, \
61 #define for_each_instance(idx, inst, kdev) \
62 for (idx = 0, inst = kdev->instances; \
63 idx < (kdev)->num_queues_in_use; \
64 idx++, inst = knav_queue_idx_to_inst(kdev, idx))
66 /* All firmware file names end up here. List the firmware file names below.
67 * Newest followed by older ones. Search is done from start of the array
68 * until a firmware file is found.
70 const char *knav_acc_firmwares
[] = {"ks2_qmss_pdsp_acc48.bin"};
72 static bool device_ready
;
73 bool knav_qmss_device_ready(void)
77 EXPORT_SYMBOL_GPL(knav_qmss_device_ready
);
80 * knav_queue_notify: qmss queue notfier call
82 * @inst: - qmss queue instance like accumulator
84 void knav_queue_notify(struct knav_queue_inst
*inst
)
86 struct knav_queue
*qh
;
92 for_each_handle_rcu(qh
, inst
) {
93 if (atomic_read(&qh
->notifier_enabled
) <= 0)
95 if (WARN_ON(!qh
->notifier_fn
))
97 this_cpu_inc(qh
->stats
->notifies
);
98 qh
->notifier_fn(qh
->notifier_fn_arg
);
102 EXPORT_SYMBOL_GPL(knav_queue_notify
);
104 static irqreturn_t
knav_queue_int_handler(int irq
, void *_instdata
)
106 struct knav_queue_inst
*inst
= _instdata
;
108 knav_queue_notify(inst
);
112 static int knav_queue_setup_irq(struct knav_range_info
*range
,
113 struct knav_queue_inst
*inst
)
115 unsigned queue
= inst
->id
- range
->queue_base
;
118 if (range
->flags
& RANGE_HAS_IRQ
) {
119 irq
= range
->irqs
[queue
].irq
;
120 ret
= request_irq(irq
, knav_queue_int_handler
, 0,
121 inst
->irq_name
, inst
);
125 if (range
->irqs
[queue
].cpu_mask
) {
126 ret
= irq_set_affinity_hint(irq
, range
->irqs
[queue
].cpu_mask
);
128 dev_warn(range
->kdev
->dev
,
129 "Failed to set IRQ affinity\n");
137 static void knav_queue_free_irq(struct knav_queue_inst
*inst
)
139 struct knav_range_info
*range
= inst
->range
;
140 unsigned queue
= inst
->id
- inst
->range
->queue_base
;
143 if (range
->flags
& RANGE_HAS_IRQ
) {
144 irq
= range
->irqs
[queue
].irq
;
145 irq_set_affinity_hint(irq
, NULL
);
150 static inline bool knav_queue_is_busy(struct knav_queue_inst
*inst
)
152 return !list_empty(&inst
->handles
);
155 static inline bool knav_queue_is_reserved(struct knav_queue_inst
*inst
)
157 return inst
->range
->flags
& RANGE_RESERVED
;
160 static inline bool knav_queue_is_shared(struct knav_queue_inst
*inst
)
162 struct knav_queue
*tmp
;
165 for_each_handle_rcu(tmp
, inst
) {
166 if (tmp
->flags
& KNAV_QUEUE_SHARED
) {
175 static inline bool knav_queue_match_type(struct knav_queue_inst
*inst
,
178 if ((type
== KNAV_QUEUE_QPEND
) &&
179 (inst
->range
->flags
& RANGE_HAS_IRQ
)) {
181 } else if ((type
== KNAV_QUEUE_ACC
) &&
182 (inst
->range
->flags
& RANGE_HAS_ACCUMULATOR
)) {
184 } else if ((type
== KNAV_QUEUE_GP
) &&
185 !(inst
->range
->flags
&
186 (RANGE_HAS_ACCUMULATOR
| RANGE_HAS_IRQ
))) {
192 static inline struct knav_queue_inst
*
193 knav_queue_match_id_to_inst(struct knav_device
*kdev
, unsigned id
)
195 struct knav_queue_inst
*inst
;
198 for_each_instance(idx
, inst
, kdev
) {
205 static inline struct knav_queue_inst
*knav_queue_find_by_id(int id
)
207 if (kdev
->base_id
<= id
&&
208 kdev
->base_id
+ kdev
->num_queues
> id
) {
210 return knav_queue_match_id_to_inst(kdev
, id
);
215 static struct knav_queue
*__knav_queue_open(struct knav_queue_inst
*inst
,
216 const char *name
, unsigned flags
)
218 struct knav_queue
*qh
;
222 qh
= devm_kzalloc(inst
->kdev
->dev
, sizeof(*qh
), GFP_KERNEL
);
224 return ERR_PTR(-ENOMEM
);
226 qh
->stats
= alloc_percpu(struct knav_queue_stats
);
234 id
= inst
->id
- inst
->qmgr
->start_queue
;
235 qh
->reg_push
= &inst
->qmgr
->reg_push
[id
];
236 qh
->reg_pop
= &inst
->qmgr
->reg_pop
[id
];
237 qh
->reg_peek
= &inst
->qmgr
->reg_peek
[id
];
240 if (!knav_queue_is_busy(inst
)) {
241 struct knav_range_info
*range
= inst
->range
;
243 inst
->name
= kstrndup(name
, KNAV_NAME_SIZE
- 1, GFP_KERNEL
);
244 if (range
->ops
&& range
->ops
->open_queue
)
245 ret
= range
->ops
->open_queue(range
, inst
, flags
);
250 list_add_tail_rcu(&qh
->list
, &inst
->handles
);
255 free_percpu(qh
->stats
);
256 devm_kfree(inst
->kdev
->dev
, qh
);
260 static struct knav_queue
*
261 knav_queue_open_by_id(const char *name
, unsigned id
, unsigned flags
)
263 struct knav_queue_inst
*inst
;
264 struct knav_queue
*qh
;
266 mutex_lock(&knav_dev_lock
);
268 qh
= ERR_PTR(-ENODEV
);
269 inst
= knav_queue_find_by_id(id
);
273 qh
= ERR_PTR(-EEXIST
);
274 if (!(flags
& KNAV_QUEUE_SHARED
) && knav_queue_is_busy(inst
))
277 qh
= ERR_PTR(-EBUSY
);
278 if ((flags
& KNAV_QUEUE_SHARED
) &&
279 (knav_queue_is_busy(inst
) && !knav_queue_is_shared(inst
)))
282 qh
= __knav_queue_open(inst
, name
, flags
);
285 mutex_unlock(&knav_dev_lock
);
290 static struct knav_queue
*knav_queue_open_by_type(const char *name
,
291 unsigned type
, unsigned flags
)
293 struct knav_queue_inst
*inst
;
294 struct knav_queue
*qh
= ERR_PTR(-EINVAL
);
297 mutex_lock(&knav_dev_lock
);
299 for_each_instance(idx
, inst
, kdev
) {
300 if (knav_queue_is_reserved(inst
))
302 if (!knav_queue_match_type(inst
, type
))
304 if (knav_queue_is_busy(inst
))
306 qh
= __knav_queue_open(inst
, name
, flags
);
311 mutex_unlock(&knav_dev_lock
);
315 static void knav_queue_set_notify(struct knav_queue_inst
*inst
, bool enabled
)
317 struct knav_range_info
*range
= inst
->range
;
319 if (range
->ops
&& range
->ops
->set_notify
)
320 range
->ops
->set_notify(range
, inst
, enabled
);
323 static int knav_queue_enable_notifier(struct knav_queue
*qh
)
325 struct knav_queue_inst
*inst
= qh
->inst
;
328 if (WARN_ON(!qh
->notifier_fn
))
331 /* Adjust the per handle notifier count */
332 first
= (atomic_inc_return(&qh
->notifier_enabled
) == 1);
334 return 0; /* nothing to do */
336 /* Now adjust the per instance notifier count */
337 first
= (atomic_inc_return(&inst
->num_notifiers
) == 1);
339 knav_queue_set_notify(inst
, true);
344 static int knav_queue_disable_notifier(struct knav_queue
*qh
)
346 struct knav_queue_inst
*inst
= qh
->inst
;
349 last
= (atomic_dec_return(&qh
->notifier_enabled
) == 0);
351 return 0; /* nothing to do */
353 last
= (atomic_dec_return(&inst
->num_notifiers
) == 0);
355 knav_queue_set_notify(inst
, false);
360 static int knav_queue_set_notifier(struct knav_queue
*qh
,
361 struct knav_queue_notify_config
*cfg
)
363 knav_queue_notify_fn old_fn
= qh
->notifier_fn
;
368 if (!(qh
->inst
->range
->flags
& (RANGE_HAS_ACCUMULATOR
| RANGE_HAS_IRQ
)))
371 if (!cfg
->fn
&& old_fn
)
372 knav_queue_disable_notifier(qh
);
374 qh
->notifier_fn
= cfg
->fn
;
375 qh
->notifier_fn_arg
= cfg
->fn_arg
;
377 if (cfg
->fn
&& !old_fn
)
378 knav_queue_enable_notifier(qh
);
383 static int knav_gp_set_notify(struct knav_range_info
*range
,
384 struct knav_queue_inst
*inst
,
389 if (range
->flags
& RANGE_HAS_IRQ
) {
390 queue
= inst
->id
- range
->queue_base
;
392 enable_irq(range
->irqs
[queue
].irq
);
394 disable_irq_nosync(range
->irqs
[queue
].irq
);
399 static int knav_gp_open_queue(struct knav_range_info
*range
,
400 struct knav_queue_inst
*inst
, unsigned flags
)
402 return knav_queue_setup_irq(range
, inst
);
405 static int knav_gp_close_queue(struct knav_range_info
*range
,
406 struct knav_queue_inst
*inst
)
408 knav_queue_free_irq(inst
);
412 static struct knav_range_ops knav_gp_range_ops
= {
413 .set_notify
= knav_gp_set_notify
,
414 .open_queue
= knav_gp_open_queue
,
415 .close_queue
= knav_gp_close_queue
,
419 static int knav_queue_get_count(void *qhandle
)
421 struct knav_queue
*qh
= qhandle
;
422 struct knav_queue_inst
*inst
= qh
->inst
;
424 return readl_relaxed(&qh
->reg_peek
[0].entry_count
) +
425 atomic_read(&inst
->desc_count
);
428 static void knav_queue_debug_show_instance(struct seq_file
*s
,
429 struct knav_queue_inst
*inst
)
431 struct knav_device
*kdev
= inst
->kdev
;
432 struct knav_queue
*qh
;
440 if (!knav_queue_is_busy(inst
))
443 seq_printf(s
, "\tqueue id %d (%s)\n",
444 kdev
->base_id
+ inst
->id
, inst
->name
);
445 for_each_handle_rcu(qh
, inst
) {
446 for_each_possible_cpu(cpu
) {
447 pushes
+= per_cpu_ptr(qh
->stats
, cpu
)->pushes
;
448 pops
+= per_cpu_ptr(qh
->stats
, cpu
)->pops
;
449 push_errors
+= per_cpu_ptr(qh
->stats
, cpu
)->push_errors
;
450 pop_errors
+= per_cpu_ptr(qh
->stats
, cpu
)->pop_errors
;
451 notifies
+= per_cpu_ptr(qh
->stats
, cpu
)->notifies
;
454 seq_printf(s
, "\t\thandle %p: pushes %8d, pops %8d, count %8d, notifies %8d, push errors %8d, pop errors %8d\n",
458 knav_queue_get_count(qh
),
465 static int knav_queue_debug_show(struct seq_file
*s
, void *v
)
467 struct knav_queue_inst
*inst
;
470 mutex_lock(&knav_dev_lock
);
471 seq_printf(s
, "%s: %u-%u\n",
472 dev_name(kdev
->dev
), kdev
->base_id
,
473 kdev
->base_id
+ kdev
->num_queues
- 1);
474 for_each_instance(idx
, inst
, kdev
)
475 knav_queue_debug_show_instance(s
, inst
);
476 mutex_unlock(&knav_dev_lock
);
481 DEFINE_SHOW_ATTRIBUTE(knav_queue_debug
);
483 static inline int knav_queue_pdsp_wait(u32
* __iomem addr
, unsigned timeout
,
489 end
= jiffies
+ msecs_to_jiffies(timeout
);
490 while (time_after(end
, jiffies
)) {
491 val
= readl_relaxed(addr
);
498 return val
? -ETIMEDOUT
: 0;
502 static int knav_queue_flush(struct knav_queue
*qh
)
504 struct knav_queue_inst
*inst
= qh
->inst
;
505 unsigned id
= inst
->id
- inst
->qmgr
->start_queue
;
507 atomic_set(&inst
->desc_count
, 0);
508 writel_relaxed(0, &inst
->qmgr
->reg_push
[id
].ptr_size_thresh
);
513 * knav_queue_open() - open a hardware queue
514 * @name: - name to give the queue handle
515 * @id: - desired queue number if any or specifes the type
517 * @flags: - the following flags are applicable to queues:
518 * KNAV_QUEUE_SHARED - allow the queue to be shared. Queues are
519 * exclusive by default.
520 * Subsequent attempts to open a shared queue should
521 * also have this flag.
523 * Returns a handle to the open hardware queue if successful. Use IS_ERR()
524 * to check the returned value for error codes.
526 void *knav_queue_open(const char *name
, unsigned id
,
529 struct knav_queue
*qh
= ERR_PTR(-EINVAL
);
532 case KNAV_QUEUE_QPEND
:
535 qh
= knav_queue_open_by_type(name
, id
, flags
);
539 qh
= knav_queue_open_by_id(name
, id
, flags
);
544 EXPORT_SYMBOL_GPL(knav_queue_open
);
547 * knav_queue_close() - close a hardware queue handle
548 * @qhandle: - handle to close
550 void knav_queue_close(void *qhandle
)
552 struct knav_queue
*qh
= qhandle
;
553 struct knav_queue_inst
*inst
= qh
->inst
;
555 while (atomic_read(&qh
->notifier_enabled
) > 0)
556 knav_queue_disable_notifier(qh
);
558 mutex_lock(&knav_dev_lock
);
559 list_del_rcu(&qh
->list
);
560 mutex_unlock(&knav_dev_lock
);
562 if (!knav_queue_is_busy(inst
)) {
563 struct knav_range_info
*range
= inst
->range
;
565 if (range
->ops
&& range
->ops
->close_queue
)
566 range
->ops
->close_queue(range
, inst
);
568 free_percpu(qh
->stats
);
569 devm_kfree(inst
->kdev
->dev
, qh
);
571 EXPORT_SYMBOL_GPL(knav_queue_close
);
574 * knav_queue_device_control() - Perform control operations on a queue
575 * @qhandle: - queue handle
576 * @cmd: - control commands
577 * @arg: - command argument
579 * Returns 0 on success, errno otherwise.
581 int knav_queue_device_control(void *qhandle
, enum knav_queue_ctrl_cmd cmd
,
584 struct knav_queue
*qh
= qhandle
;
585 struct knav_queue_notify_config
*cfg
;
589 case KNAV_QUEUE_GET_ID
:
590 ret
= qh
->inst
->kdev
->base_id
+ qh
->inst
->id
;
593 case KNAV_QUEUE_FLUSH
:
594 ret
= knav_queue_flush(qh
);
597 case KNAV_QUEUE_SET_NOTIFIER
:
599 ret
= knav_queue_set_notifier(qh
, cfg
);
602 case KNAV_QUEUE_ENABLE_NOTIFY
:
603 ret
= knav_queue_enable_notifier(qh
);
606 case KNAV_QUEUE_DISABLE_NOTIFY
:
607 ret
= knav_queue_disable_notifier(qh
);
610 case KNAV_QUEUE_GET_COUNT
:
611 ret
= knav_queue_get_count(qh
);
620 EXPORT_SYMBOL_GPL(knav_queue_device_control
);
625 * knav_queue_push() - push data (or descriptor) to the tail of a queue
626 * @qhandle: - hardware queue handle
627 * @dma: - DMA data to push
628 * @size: - size of data to push
629 * @flags: - can be used to pass additional information
631 * Returns 0 on success, errno otherwise.
633 int knav_queue_push(void *qhandle
, dma_addr_t dma
,
634 unsigned size
, unsigned flags
)
636 struct knav_queue
*qh
= qhandle
;
639 val
= (u32
)dma
| ((size
/ 16) - 1);
640 writel_relaxed(val
, &qh
->reg_push
[0].ptr_size_thresh
);
642 this_cpu_inc(qh
->stats
->pushes
);
645 EXPORT_SYMBOL_GPL(knav_queue_push
);
648 * knav_queue_pop() - pop data (or descriptor) from the head of a queue
649 * @qhandle: - hardware queue handle
650 * @size: - (optional) size of the data pop'ed.
652 * Returns a DMA address on success, 0 on failure.
654 dma_addr_t
knav_queue_pop(void *qhandle
, unsigned *size
)
656 struct knav_queue
*qh
= qhandle
;
657 struct knav_queue_inst
*inst
= qh
->inst
;
661 /* are we accumulated? */
663 if (unlikely(atomic_dec_return(&inst
->desc_count
) < 0)) {
664 atomic_inc(&inst
->desc_count
);
667 idx
= atomic_inc_return(&inst
->desc_head
);
668 idx
&= ACC_DESCS_MASK
;
669 val
= inst
->descs
[idx
];
671 val
= readl_relaxed(&qh
->reg_pop
[0].ptr_size_thresh
);
676 dma
= val
& DESC_PTR_MASK
;
678 *size
= ((val
& DESC_SIZE_MASK
) + 1) * 16;
680 this_cpu_inc(qh
->stats
->pops
);
683 EXPORT_SYMBOL_GPL(knav_queue_pop
);
685 /* carve out descriptors and push into queue */
686 static void kdesc_fill_pool(struct knav_pool
*pool
)
688 struct knav_region
*region
;
691 region
= pool
->region
;
692 pool
->desc_size
= region
->desc_size
;
693 for (i
= 0; i
< pool
->num_desc
; i
++) {
694 int index
= pool
->region_offset
+ i
;
697 dma_addr
= region
->dma_start
+ (region
->desc_size
* index
);
698 dma_size
= ALIGN(pool
->desc_size
, SMP_CACHE_BYTES
);
699 dma_sync_single_for_device(pool
->dev
, dma_addr
, dma_size
,
701 knav_queue_push(pool
->queue
, dma_addr
, dma_size
, 0);
705 /* pop out descriptors and close the queue */
706 static void kdesc_empty_pool(struct knav_pool
*pool
)
717 dma
= knav_queue_pop(pool
->queue
, &size
);
720 desc
= knav_pool_desc_dma_to_virt(pool
, dma
);
722 dev_dbg(pool
->kdev
->dev
,
723 "couldn't unmap desc, continuing\n");
727 WARN_ON(i
!= pool
->num_desc
);
728 knav_queue_close(pool
->queue
);
732 /* Get the DMA address of a descriptor */
733 dma_addr_t
knav_pool_desc_virt_to_dma(void *ph
, void *virt
)
735 struct knav_pool
*pool
= ph
;
736 return pool
->region
->dma_start
+ (virt
- pool
->region
->virt_start
);
738 EXPORT_SYMBOL_GPL(knav_pool_desc_virt_to_dma
);
740 void *knav_pool_desc_dma_to_virt(void *ph
, dma_addr_t dma
)
742 struct knav_pool
*pool
= ph
;
743 return pool
->region
->virt_start
+ (dma
- pool
->region
->dma_start
);
745 EXPORT_SYMBOL_GPL(knav_pool_desc_dma_to_virt
);
748 * knav_pool_create() - Create a pool of descriptors
749 * @name: - name to give the pool handle
750 * @num_desc: - numbers of descriptors in the pool
751 * @region_id: - QMSS region id from which the descriptors are to be
754 * Returns a pool handle on success.
755 * Use IS_ERR_OR_NULL() to identify error values on return.
757 void *knav_pool_create(const char *name
,
758 int num_desc
, int region_id
)
760 struct knav_region
*reg_itr
, *region
= NULL
;
761 struct knav_pool
*pool
, *pi
;
762 struct list_head
*node
;
763 unsigned last_offset
;
768 return ERR_PTR(-EPROBE_DEFER
);
771 return ERR_PTR(-ENODEV
);
773 pool
= devm_kzalloc(kdev
->dev
, sizeof(*pool
), GFP_KERNEL
);
775 dev_err(kdev
->dev
, "out of memory allocating pool\n");
776 return ERR_PTR(-ENOMEM
);
779 for_each_region(kdev
, reg_itr
) {
780 if (reg_itr
->id
!= region_id
)
787 dev_err(kdev
->dev
, "region-id(%d) not found\n", region_id
);
792 pool
->queue
= knav_queue_open(name
, KNAV_QUEUE_GP
, 0);
793 if (IS_ERR_OR_NULL(pool
->queue
)) {
795 "failed to open queue for pool(%s), error %ld\n",
796 name
, PTR_ERR(pool
->queue
));
797 ret
= PTR_ERR(pool
->queue
);
801 pool
->name
= kstrndup(name
, KNAV_NAME_SIZE
- 1, GFP_KERNEL
);
803 pool
->dev
= kdev
->dev
;
805 mutex_lock(&knav_dev_lock
);
807 if (num_desc
> (region
->num_desc
- region
->used_desc
)) {
808 dev_err(kdev
->dev
, "out of descs in region(%d) for pool(%s)\n",
814 /* Region maintains a sorted (by region offset) list of pools
815 * use the first free slot which is large enough to accomodate
820 node
= ®ion
->pools
;
821 list_for_each_entry(pi
, ®ion
->pools
, region_inst
) {
822 if ((pi
->region_offset
- last_offset
) >= num_desc
) {
826 last_offset
= pi
->region_offset
+ pi
->num_desc
;
828 node
= &pi
->region_inst
;
831 pool
->region
= region
;
832 pool
->num_desc
= num_desc
;
833 pool
->region_offset
= last_offset
;
834 region
->used_desc
+= num_desc
;
835 list_add_tail(&pool
->list
, &kdev
->pools
);
836 list_add_tail(&pool
->region_inst
, node
);
838 dev_err(kdev
->dev
, "pool(%s) create failed: fragmented desc pool in region(%d)\n",
844 mutex_unlock(&knav_dev_lock
);
845 kdesc_fill_pool(pool
);
849 mutex_unlock(&knav_dev_lock
);
852 devm_kfree(kdev
->dev
, pool
);
855 EXPORT_SYMBOL_GPL(knav_pool_create
);
858 * knav_pool_destroy() - Free a pool of descriptors
861 void knav_pool_destroy(void *ph
)
863 struct knav_pool
*pool
= ph
;
871 kdesc_empty_pool(pool
);
872 mutex_lock(&knav_dev_lock
);
874 pool
->region
->used_desc
-= pool
->num_desc
;
875 list_del(&pool
->region_inst
);
876 list_del(&pool
->list
);
878 mutex_unlock(&knav_dev_lock
);
880 devm_kfree(kdev
->dev
, pool
);
882 EXPORT_SYMBOL_GPL(knav_pool_destroy
);
886 * knav_pool_desc_get() - Get a descriptor from the pool
889 * Returns descriptor from the pool.
891 void *knav_pool_desc_get(void *ph
)
893 struct knav_pool
*pool
= ph
;
898 dma
= knav_queue_pop(pool
->queue
, &size
);
900 return ERR_PTR(-ENOMEM
);
901 data
= knav_pool_desc_dma_to_virt(pool
, dma
);
904 EXPORT_SYMBOL_GPL(knav_pool_desc_get
);
907 * knav_pool_desc_put() - return a descriptor to the pool
909 * @desc: - virtual address
911 void knav_pool_desc_put(void *ph
, void *desc
)
913 struct knav_pool
*pool
= ph
;
915 dma
= knav_pool_desc_virt_to_dma(pool
, desc
);
916 knav_queue_push(pool
->queue
, dma
, pool
->region
->desc_size
, 0);
918 EXPORT_SYMBOL_GPL(knav_pool_desc_put
);
921 * knav_pool_desc_map() - Map descriptor for DMA transfer
923 * @desc: - address of descriptor to map
924 * @size: - size of descriptor to map
925 * @dma: - DMA address return pointer
926 * @dma_sz: - adjusted return pointer
928 * Returns 0 on success, errno otherwise.
930 int knav_pool_desc_map(void *ph
, void *desc
, unsigned size
,
931 dma_addr_t
*dma
, unsigned *dma_sz
)
933 struct knav_pool
*pool
= ph
;
934 *dma
= knav_pool_desc_virt_to_dma(pool
, desc
);
935 size
= min(size
, pool
->region
->desc_size
);
936 size
= ALIGN(size
, SMP_CACHE_BYTES
);
938 dma_sync_single_for_device(pool
->dev
, *dma
, size
, DMA_TO_DEVICE
);
940 /* Ensure the descriptor reaches to the memory */
945 EXPORT_SYMBOL_GPL(knav_pool_desc_map
);
948 * knav_pool_desc_unmap() - Unmap descriptor after DMA transfer
950 * @dma: - DMA address of descriptor to unmap
951 * @dma_sz: - size of descriptor to unmap
953 * Returns descriptor address on success, Use IS_ERR_OR_NULL() to identify
954 * error values on return.
956 void *knav_pool_desc_unmap(void *ph
, dma_addr_t dma
, unsigned dma_sz
)
958 struct knav_pool
*pool
= ph
;
962 desc_sz
= min(dma_sz
, pool
->region
->desc_size
);
963 desc
= knav_pool_desc_dma_to_virt(pool
, dma
);
964 dma_sync_single_for_cpu(pool
->dev
, dma
, desc_sz
, DMA_FROM_DEVICE
);
968 EXPORT_SYMBOL_GPL(knav_pool_desc_unmap
);
971 * knav_pool_count() - Get the number of descriptors in pool.
973 * Returns number of elements in the pool.
975 int knav_pool_count(void *ph
)
977 struct knav_pool
*pool
= ph
;
978 return knav_queue_get_count(pool
->queue
);
980 EXPORT_SYMBOL_GPL(knav_pool_count
);
982 static void knav_queue_setup_region(struct knav_device
*kdev
,
983 struct knav_region
*region
)
985 unsigned hw_num_desc
, hw_desc_size
, size
;
986 struct knav_reg_region __iomem
*regs
;
987 struct knav_qmgr_info
*qmgr
;
988 struct knav_pool
*pool
;
993 if (!region
->num_desc
) {
994 dev_warn(kdev
->dev
, "unused region %s\n", region
->name
);
998 /* get hardware descriptor value */
999 hw_num_desc
= ilog2(region
->num_desc
- 1) + 1;
1001 /* did we force fit ourselves into nothingness? */
1002 if (region
->num_desc
< 32) {
1003 region
->num_desc
= 0;
1004 dev_warn(kdev
->dev
, "too few descriptors in region %s\n",
1009 size
= region
->num_desc
* region
->desc_size
;
1010 region
->virt_start
= alloc_pages_exact(size
, GFP_KERNEL
| GFP_DMA
|
1012 if (!region
->virt_start
) {
1013 region
->num_desc
= 0;
1014 dev_err(kdev
->dev
, "memory alloc failed for region %s\n",
1018 region
->virt_end
= region
->virt_start
+ size
;
1019 page
= virt_to_page(region
->virt_start
);
1021 region
->dma_start
= dma_map_page(kdev
->dev
, page
, 0, size
,
1023 if (dma_mapping_error(kdev
->dev
, region
->dma_start
)) {
1024 dev_err(kdev
->dev
, "dma map failed for region %s\n",
1028 region
->dma_end
= region
->dma_start
+ size
;
1030 pool
= devm_kzalloc(kdev
->dev
, sizeof(*pool
), GFP_KERNEL
);
1032 dev_err(kdev
->dev
, "out of memory allocating dummy pool\n");
1036 pool
->region_offset
= region
->num_desc
;
1037 list_add(&pool
->region_inst
, ®ion
->pools
);
1040 "region %s (%d): size:%d, link:%d@%d, dma:%pad-%pad, virt:%p-%p\n",
1041 region
->name
, id
, region
->desc_size
, region
->num_desc
,
1042 region
->link_index
, ®ion
->dma_start
, ®ion
->dma_end
,
1043 region
->virt_start
, region
->virt_end
);
1045 hw_desc_size
= (region
->desc_size
/ 16) - 1;
1048 for_each_qmgr(kdev
, qmgr
) {
1049 regs
= qmgr
->reg_region
+ id
;
1050 writel_relaxed((u32
)region
->dma_start
, ®s
->base
);
1051 writel_relaxed(region
->link_index
, ®s
->start_index
);
1052 writel_relaxed(hw_desc_size
<< 16 | hw_num_desc
,
1058 if (region
->dma_start
)
1059 dma_unmap_page(kdev
->dev
, region
->dma_start
, size
,
1061 if (region
->virt_start
)
1062 free_pages_exact(region
->virt_start
, size
);
1063 region
->num_desc
= 0;
1067 static const char *knav_queue_find_name(struct device_node
*node
)
1071 if (of_property_read_string(node
, "label", &name
) < 0)
1078 static int knav_queue_setup_regions(struct knav_device
*kdev
,
1079 struct device_node
*regions
)
1081 struct device
*dev
= kdev
->dev
;
1082 struct knav_region
*region
;
1083 struct device_node
*child
;
1087 for_each_child_of_node(regions
, child
) {
1088 region
= devm_kzalloc(dev
, sizeof(*region
), GFP_KERNEL
);
1090 dev_err(dev
, "out of memory allocating region\n");
1094 region
->name
= knav_queue_find_name(child
);
1095 of_property_read_u32(child
, "id", ®ion
->id
);
1096 ret
= of_property_read_u32_array(child
, "region-spec", temp
, 2);
1098 region
->num_desc
= temp
[0];
1099 region
->desc_size
= temp
[1];
1101 dev_err(dev
, "invalid region info %s\n", region
->name
);
1102 devm_kfree(dev
, region
);
1106 if (!of_get_property(child
, "link-index", NULL
)) {
1107 dev_err(dev
, "No link info for %s\n", region
->name
);
1108 devm_kfree(dev
, region
);
1111 ret
= of_property_read_u32(child
, "link-index",
1112 ®ion
->link_index
);
1114 dev_err(dev
, "link index not found for %s\n",
1116 devm_kfree(dev
, region
);
1120 INIT_LIST_HEAD(®ion
->pools
);
1121 list_add_tail(®ion
->list
, &kdev
->regions
);
1123 if (list_empty(&kdev
->regions
)) {
1124 dev_err(dev
, "no valid region information found\n");
1128 /* Next, we run through the regions and set things up */
1129 for_each_region(kdev
, region
)
1130 knav_queue_setup_region(kdev
, region
);
1135 static int knav_get_link_ram(struct knav_device
*kdev
,
1137 struct knav_link_ram_block
*block
)
1139 struct platform_device
*pdev
= to_platform_device(kdev
->dev
);
1140 struct device_node
*node
= pdev
->dev
.of_node
;
1144 * Note: link ram resources are specified in "entry" sized units. In
1145 * reality, although entries are ~40bits in hardware, we treat them as
1146 * 64-bit entities here.
1148 * For example, to specify the internal link ram for Keystone-I class
1149 * devices, we would set the linkram0 resource to 0x80000-0x83fff.
1151 * This gets a bit weird when other link rams are used. For example,
1152 * if the range specified is 0x0c000000-0x0c003fff (i.e., 16K entries
1153 * in MSMC SRAM), the actual memory used is 0x0c000000-0x0c020000,
1154 * which accounts for 64-bits per entry, for 16K entries.
1156 if (!of_property_read_u32_array(node
, name
, temp
, 2)) {
1159 * queue_base specified => using internal or onchip
1160 * link ram WARNING - we do not "reserve" this block
1162 block
->dma
= (dma_addr_t
)temp
[0];
1164 block
->size
= temp
[1];
1166 block
->size
= temp
[1];
1167 /* queue_base not specific => allocate requested size */
1168 block
->virt
= dmam_alloc_coherent(kdev
->dev
,
1169 8 * block
->size
, &block
->dma
,
1172 dev_err(kdev
->dev
, "failed to alloc linkram\n");
1182 static int knav_queue_setup_link_ram(struct knav_device
*kdev
)
1184 struct knav_link_ram_block
*block
;
1185 struct knav_qmgr_info
*qmgr
;
1187 for_each_qmgr(kdev
, qmgr
) {
1188 block
= &kdev
->link_rams
[0];
1189 dev_dbg(kdev
->dev
, "linkram0: dma:%pad, virt:%p, size:%x\n",
1190 &block
->dma
, block
->virt
, block
->size
);
1191 writel_relaxed((u32
)block
->dma
, &qmgr
->reg_config
->link_ram_base0
);
1192 if (kdev
->version
== QMSS_66AK2G
)
1193 writel_relaxed(block
->size
,
1194 &qmgr
->reg_config
->link_ram_size0
);
1196 writel_relaxed(block
->size
- 1,
1197 &qmgr
->reg_config
->link_ram_size0
);
1202 dev_dbg(kdev
->dev
, "linkram1: dma:%pad, virt:%p, size:%x\n",
1203 &block
->dma
, block
->virt
, block
->size
);
1204 writel_relaxed(block
->dma
, &qmgr
->reg_config
->link_ram_base1
);
1210 static int knav_setup_queue_range(struct knav_device
*kdev
,
1211 struct device_node
*node
)
1213 struct device
*dev
= kdev
->dev
;
1214 struct knav_range_info
*range
;
1215 struct knav_qmgr_info
*qmgr
;
1216 u32 temp
[2], start
, end
, id
, index
;
1219 range
= devm_kzalloc(dev
, sizeof(*range
), GFP_KERNEL
);
1221 dev_err(dev
, "out of memory allocating range\n");
1226 range
->name
= knav_queue_find_name(node
);
1227 ret
= of_property_read_u32_array(node
, "qrange", temp
, 2);
1229 range
->queue_base
= temp
[0] - kdev
->base_id
;
1230 range
->num_queues
= temp
[1];
1232 dev_err(dev
, "invalid queue range %s\n", range
->name
);
1233 devm_kfree(dev
, range
);
1237 for (i
= 0; i
< RANGE_MAX_IRQS
; i
++) {
1238 struct of_phandle_args oirq
;
1240 if (of_irq_parse_one(node
, i
, &oirq
))
1243 range
->irqs
[i
].irq
= irq_create_of_mapping(&oirq
);
1244 if (range
->irqs
[i
].irq
== IRQ_NONE
)
1249 if (IS_ENABLED(CONFIG_SMP
) && oirq
.args_count
== 3) {
1253 range
->irqs
[i
].cpu_mask
= devm_kzalloc(dev
,
1254 cpumask_size(), GFP_KERNEL
);
1255 if (!range
->irqs
[i
].cpu_mask
)
1258 mask
= (oirq
.args
[2] & 0x0000ff00) >> 8;
1259 for_each_set_bit(bit
, &mask
, BITS_PER_LONG
)
1260 cpumask_set_cpu(bit
, range
->irqs
[i
].cpu_mask
);
1264 range
->num_irqs
= min(range
->num_irqs
, range
->num_queues
);
1265 if (range
->num_irqs
)
1266 range
->flags
|= RANGE_HAS_IRQ
;
1268 if (of_get_property(node
, "qalloc-by-id", NULL
))
1269 range
->flags
|= RANGE_RESERVED
;
1271 if (of_get_property(node
, "accumulator", NULL
)) {
1272 ret
= knav_init_acc_range(kdev
, node
, range
);
1274 devm_kfree(dev
, range
);
1278 range
->ops
= &knav_gp_range_ops
;
1281 /* set threshold to 1, and flush out the queues */
1282 for_each_qmgr(kdev
, qmgr
) {
1283 start
= max(qmgr
->start_queue
, range
->queue_base
);
1284 end
= min(qmgr
->start_queue
+ qmgr
->num_queues
,
1285 range
->queue_base
+ range
->num_queues
);
1286 for (id
= start
; id
< end
; id
++) {
1287 index
= id
- qmgr
->start_queue
;
1288 writel_relaxed(THRESH_GTE
| 1,
1289 &qmgr
->reg_peek
[index
].ptr_size_thresh
);
1291 &qmgr
->reg_push
[index
].ptr_size_thresh
);
1295 list_add_tail(&range
->list
, &kdev
->queue_ranges
);
1296 dev_dbg(dev
, "added range %s: %d-%d, %d irqs%s%s%s\n",
1297 range
->name
, range
->queue_base
,
1298 range
->queue_base
+ range
->num_queues
- 1,
1300 (range
->flags
& RANGE_HAS_IRQ
) ? ", has irq" : "",
1301 (range
->flags
& RANGE_RESERVED
) ? ", reserved" : "",
1302 (range
->flags
& RANGE_HAS_ACCUMULATOR
) ? ", acc" : "");
1303 kdev
->num_queues_in_use
+= range
->num_queues
;
1307 static int knav_setup_queue_pools(struct knav_device
*kdev
,
1308 struct device_node
*queue_pools
)
1310 struct device_node
*type
, *range
;
1312 for_each_child_of_node(queue_pools
, type
) {
1313 for_each_child_of_node(type
, range
) {
1314 /* return value ignored, we init the rest... */
1315 knav_setup_queue_range(kdev
, range
);
1319 /* ... and barf if they all failed! */
1320 if (list_empty(&kdev
->queue_ranges
)) {
1321 dev_err(kdev
->dev
, "no valid queue range found\n");
1327 static void knav_free_queue_range(struct knav_device
*kdev
,
1328 struct knav_range_info
*range
)
1330 if (range
->ops
&& range
->ops
->free_range
)
1331 range
->ops
->free_range(range
);
1332 list_del(&range
->list
);
1333 devm_kfree(kdev
->dev
, range
);
1336 static void knav_free_queue_ranges(struct knav_device
*kdev
)
1338 struct knav_range_info
*range
;
1341 range
= first_queue_range(kdev
);
1344 knav_free_queue_range(kdev
, range
);
1348 static void knav_queue_free_regions(struct knav_device
*kdev
)
1350 struct knav_region
*region
;
1351 struct knav_pool
*pool
, *tmp
;
1355 region
= first_region(kdev
);
1358 list_for_each_entry_safe(pool
, tmp
, ®ion
->pools
, region_inst
)
1359 knav_pool_destroy(pool
);
1361 size
= region
->virt_end
- region
->virt_start
;
1363 free_pages_exact(region
->virt_start
, size
);
1364 list_del(®ion
->list
);
1365 devm_kfree(kdev
->dev
, region
);
1369 static void __iomem
*knav_queue_map_reg(struct knav_device
*kdev
,
1370 struct device_node
*node
, int index
)
1372 struct resource res
;
1376 ret
= of_address_to_resource(node
, index
, &res
);
1378 dev_err(kdev
->dev
, "Can't translate of node(%pOFn) address for index(%d)\n",
1380 return ERR_PTR(ret
);
1383 regs
= devm_ioremap_resource(kdev
->dev
, &res
);
1385 dev_err(kdev
->dev
, "Failed to map register base for index(%d) node(%pOFn)\n",
1390 static int knav_queue_init_qmgrs(struct knav_device
*kdev
,
1391 struct device_node
*qmgrs
)
1393 struct device
*dev
= kdev
->dev
;
1394 struct knav_qmgr_info
*qmgr
;
1395 struct device_node
*child
;
1399 for_each_child_of_node(qmgrs
, child
) {
1400 qmgr
= devm_kzalloc(dev
, sizeof(*qmgr
), GFP_KERNEL
);
1402 dev_err(dev
, "out of memory allocating qmgr\n");
1406 ret
= of_property_read_u32_array(child
, "managed-queues",
1409 qmgr
->start_queue
= temp
[0];
1410 qmgr
->num_queues
= temp
[1];
1412 dev_err(dev
, "invalid qmgr queue range\n");
1413 devm_kfree(dev
, qmgr
);
1417 dev_info(dev
, "qmgr start queue %d, number of queues %d\n",
1418 qmgr
->start_queue
, qmgr
->num_queues
);
1421 knav_queue_map_reg(kdev
, child
,
1422 KNAV_QUEUE_PEEK_REG_INDEX
);
1424 if (kdev
->version
== QMSS
) {
1426 knav_queue_map_reg(kdev
, child
,
1427 KNAV_QUEUE_STATUS_REG_INDEX
);
1431 knav_queue_map_reg(kdev
, child
,
1432 (kdev
->version
== QMSS_66AK2G
) ?
1433 KNAV_L_QUEUE_CONFIG_REG_INDEX
:
1434 KNAV_QUEUE_CONFIG_REG_INDEX
);
1436 knav_queue_map_reg(kdev
, child
,
1437 (kdev
->version
== QMSS_66AK2G
) ?
1438 KNAV_L_QUEUE_REGION_REG_INDEX
:
1439 KNAV_QUEUE_REGION_REG_INDEX
);
1442 knav_queue_map_reg(kdev
, child
,
1443 (kdev
->version
== QMSS_66AK2G
) ?
1444 KNAV_L_QUEUE_PUSH_REG_INDEX
:
1445 KNAV_QUEUE_PUSH_REG_INDEX
);
1447 if (kdev
->version
== QMSS
) {
1449 knav_queue_map_reg(kdev
, child
,
1450 KNAV_QUEUE_POP_REG_INDEX
);
1453 if (IS_ERR(qmgr
->reg_peek
) ||
1454 ((kdev
->version
== QMSS
) &&
1455 (IS_ERR(qmgr
->reg_status
) || IS_ERR(qmgr
->reg_pop
))) ||
1456 IS_ERR(qmgr
->reg_config
) || IS_ERR(qmgr
->reg_region
) ||
1457 IS_ERR(qmgr
->reg_push
)) {
1458 dev_err(dev
, "failed to map qmgr regs\n");
1459 if (kdev
->version
== QMSS
) {
1460 if (!IS_ERR(qmgr
->reg_status
))
1461 devm_iounmap(dev
, qmgr
->reg_status
);
1462 if (!IS_ERR(qmgr
->reg_pop
))
1463 devm_iounmap(dev
, qmgr
->reg_pop
);
1465 if (!IS_ERR(qmgr
->reg_peek
))
1466 devm_iounmap(dev
, qmgr
->reg_peek
);
1467 if (!IS_ERR(qmgr
->reg_config
))
1468 devm_iounmap(dev
, qmgr
->reg_config
);
1469 if (!IS_ERR(qmgr
->reg_region
))
1470 devm_iounmap(dev
, qmgr
->reg_region
);
1471 if (!IS_ERR(qmgr
->reg_push
))
1472 devm_iounmap(dev
, qmgr
->reg_push
);
1473 devm_kfree(dev
, qmgr
);
1477 /* Use same push register for pop as well */
1478 if (kdev
->version
== QMSS_66AK2G
)
1479 qmgr
->reg_pop
= qmgr
->reg_push
;
1481 list_add_tail(&qmgr
->list
, &kdev
->qmgrs
);
1482 dev_info(dev
, "added qmgr start queue %d, num of queues %d, reg_peek %p, reg_status %p, reg_config %p, reg_region %p, reg_push %p, reg_pop %p\n",
1483 qmgr
->start_queue
, qmgr
->num_queues
,
1484 qmgr
->reg_peek
, qmgr
->reg_status
,
1485 qmgr
->reg_config
, qmgr
->reg_region
,
1486 qmgr
->reg_push
, qmgr
->reg_pop
);
1491 static int knav_queue_init_pdsps(struct knav_device
*kdev
,
1492 struct device_node
*pdsps
)
1494 struct device
*dev
= kdev
->dev
;
1495 struct knav_pdsp_info
*pdsp
;
1496 struct device_node
*child
;
1498 for_each_child_of_node(pdsps
, child
) {
1499 pdsp
= devm_kzalloc(dev
, sizeof(*pdsp
), GFP_KERNEL
);
1501 dev_err(dev
, "out of memory allocating pdsp\n");
1504 pdsp
->name
= knav_queue_find_name(child
);
1506 knav_queue_map_reg(kdev
, child
,
1507 KNAV_QUEUE_PDSP_IRAM_REG_INDEX
);
1509 knav_queue_map_reg(kdev
, child
,
1510 KNAV_QUEUE_PDSP_REGS_REG_INDEX
);
1512 knav_queue_map_reg(kdev
, child
,
1513 KNAV_QUEUE_PDSP_INTD_REG_INDEX
);
1515 knav_queue_map_reg(kdev
, child
,
1516 KNAV_QUEUE_PDSP_CMD_REG_INDEX
);
1518 if (IS_ERR(pdsp
->command
) || IS_ERR(pdsp
->iram
) ||
1519 IS_ERR(pdsp
->regs
) || IS_ERR(pdsp
->intd
)) {
1520 dev_err(dev
, "failed to map pdsp %s regs\n",
1522 if (!IS_ERR(pdsp
->command
))
1523 devm_iounmap(dev
, pdsp
->command
);
1524 if (!IS_ERR(pdsp
->iram
))
1525 devm_iounmap(dev
, pdsp
->iram
);
1526 if (!IS_ERR(pdsp
->regs
))
1527 devm_iounmap(dev
, pdsp
->regs
);
1528 if (!IS_ERR(pdsp
->intd
))
1529 devm_iounmap(dev
, pdsp
->intd
);
1530 devm_kfree(dev
, pdsp
);
1533 of_property_read_u32(child
, "id", &pdsp
->id
);
1534 list_add_tail(&pdsp
->list
, &kdev
->pdsps
);
1535 dev_dbg(dev
, "added pdsp %s: command %p, iram %p, regs %p, intd %p\n",
1536 pdsp
->name
, pdsp
->command
, pdsp
->iram
, pdsp
->regs
,
1542 static int knav_queue_stop_pdsp(struct knav_device
*kdev
,
1543 struct knav_pdsp_info
*pdsp
)
1545 u32 val
, timeout
= 1000;
1548 val
= readl_relaxed(&pdsp
->regs
->control
) & ~PDSP_CTRL_ENABLE
;
1549 writel_relaxed(val
, &pdsp
->regs
->control
);
1550 ret
= knav_queue_pdsp_wait(&pdsp
->regs
->control
, timeout
,
1553 dev_err(kdev
->dev
, "timed out on pdsp %s stop\n", pdsp
->name
);
1556 pdsp
->loaded
= false;
1557 pdsp
->started
= false;
1561 static int knav_queue_load_pdsp(struct knav_device
*kdev
,
1562 struct knav_pdsp_info
*pdsp
)
1565 const struct firmware
*fw
;
1569 for (i
= 0; i
< ARRAY_SIZE(knav_acc_firmwares
); i
++) {
1570 if (knav_acc_firmwares
[i
]) {
1571 ret
= request_firmware_direct(&fw
,
1572 knav_acc_firmwares
[i
],
1582 dev_err(kdev
->dev
, "failed to get firmware for pdsp\n");
1586 dev_info(kdev
->dev
, "firmware file %s downloaded for PDSP\n",
1587 knav_acc_firmwares
[i
]);
1589 writel_relaxed(pdsp
->id
+ 1, pdsp
->command
+ 0x18);
1590 /* download the firmware */
1591 fwdata
= (u32
*)fw
->data
;
1592 fwlen
= (fw
->size
+ sizeof(u32
) - 1) / sizeof(u32
);
1593 for (i
= 0; i
< fwlen
; i
++)
1594 writel_relaxed(be32_to_cpu(fwdata
[i
]), pdsp
->iram
+ i
);
1596 release_firmware(fw
);
1600 static int knav_queue_start_pdsp(struct knav_device
*kdev
,
1601 struct knav_pdsp_info
*pdsp
)
1603 u32 val
, timeout
= 1000;
1606 /* write a command for sync */
1607 writel_relaxed(0xffffffff, pdsp
->command
);
1608 while (readl_relaxed(pdsp
->command
) != 0xffffffff)
1611 /* soft reset the PDSP */
1612 val
= readl_relaxed(&pdsp
->regs
->control
);
1613 val
&= ~(PDSP_CTRL_PC_MASK
| PDSP_CTRL_SOFT_RESET
);
1614 writel_relaxed(val
, &pdsp
->regs
->control
);
1617 val
= readl_relaxed(&pdsp
->regs
->control
) | PDSP_CTRL_ENABLE
;
1618 writel_relaxed(val
, &pdsp
->regs
->control
);
1620 /* wait for command register to clear */
1621 ret
= knav_queue_pdsp_wait(pdsp
->command
, timeout
, 0);
1624 "timed out on pdsp %s command register wait\n",
1631 static void knav_queue_stop_pdsps(struct knav_device
*kdev
)
1633 struct knav_pdsp_info
*pdsp
;
1635 /* disable all pdsps */
1636 for_each_pdsp(kdev
, pdsp
)
1637 knav_queue_stop_pdsp(kdev
, pdsp
);
1640 static int knav_queue_start_pdsps(struct knav_device
*kdev
)
1642 struct knav_pdsp_info
*pdsp
;
1645 knav_queue_stop_pdsps(kdev
);
1646 /* now load them all. We return success even if pdsp
1647 * is not loaded as acc channels are optional on having
1648 * firmware availability in the system. We set the loaded
1649 * and stated flag and when initialize the acc range, check
1650 * it and init the range only if pdsp is started.
1652 for_each_pdsp(kdev
, pdsp
) {
1653 ret
= knav_queue_load_pdsp(kdev
, pdsp
);
1655 pdsp
->loaded
= true;
1658 for_each_pdsp(kdev
, pdsp
) {
1660 ret
= knav_queue_start_pdsp(kdev
, pdsp
);
1662 pdsp
->started
= true;
1668 static inline struct knav_qmgr_info
*knav_find_qmgr(unsigned id
)
1670 struct knav_qmgr_info
*qmgr
;
1672 for_each_qmgr(kdev
, qmgr
) {
1673 if ((id
>= qmgr
->start_queue
) &&
1674 (id
< qmgr
->start_queue
+ qmgr
->num_queues
))
1680 static int knav_queue_init_queue(struct knav_device
*kdev
,
1681 struct knav_range_info
*range
,
1682 struct knav_queue_inst
*inst
,
1685 char irq_name
[KNAV_NAME_SIZE
];
1686 inst
->qmgr
= knav_find_qmgr(id
);
1690 INIT_LIST_HEAD(&inst
->handles
);
1692 inst
->range
= range
;
1695 scnprintf(irq_name
, sizeof(irq_name
), "hwqueue-%d", id
);
1696 inst
->irq_name
= kstrndup(irq_name
, sizeof(irq_name
), GFP_KERNEL
);
1698 if (range
->ops
&& range
->ops
->init_queue
)
1699 return range
->ops
->init_queue(range
, inst
);
1704 static int knav_queue_init_queues(struct knav_device
*kdev
)
1706 struct knav_range_info
*range
;
1707 int size
, id
, base_idx
;
1708 int idx
= 0, ret
= 0;
1710 /* how much do we need for instance data? */
1711 size
= sizeof(struct knav_queue_inst
);
1713 /* round this up to a power of 2, keep the index to instance
1716 kdev
->inst_shift
= order_base_2(size
);
1717 size
= (1 << kdev
->inst_shift
) * kdev
->num_queues_in_use
;
1718 kdev
->instances
= devm_kzalloc(kdev
->dev
, size
, GFP_KERNEL
);
1719 if (!kdev
->instances
)
1722 for_each_queue_range(kdev
, range
) {
1723 if (range
->ops
&& range
->ops
->init_range
)
1724 range
->ops
->init_range(range
);
1726 for (id
= range
->queue_base
;
1727 id
< range
->queue_base
+ range
->num_queues
; id
++, idx
++) {
1728 ret
= knav_queue_init_queue(kdev
, range
,
1729 knav_queue_idx_to_inst(kdev
, idx
), id
);
1733 range
->queue_base_inst
=
1734 knav_queue_idx_to_inst(kdev
, base_idx
);
1739 /* Match table for of_platform binding */
1740 static const struct of_device_id keystone_qmss_of_match
[] = {
1742 .compatible
= "ti,keystone-navigator-qmss",
1745 .compatible
= "ti,66ak2g-navss-qm",
1746 .data
= (void *)QMSS_66AK2G
,
1750 MODULE_DEVICE_TABLE(of
, keystone_qmss_of_match
);
1752 static int knav_queue_probe(struct platform_device
*pdev
)
1754 struct device_node
*node
= pdev
->dev
.of_node
;
1755 struct device_node
*qmgrs
, *queue_pools
, *regions
, *pdsps
;
1756 const struct of_device_id
*match
;
1757 struct device
*dev
= &pdev
->dev
;
1762 dev_err(dev
, "device tree info unavailable\n");
1766 kdev
= devm_kzalloc(dev
, sizeof(struct knav_device
), GFP_KERNEL
);
1768 dev_err(dev
, "memory allocation failed\n");
1772 match
= of_match_device(of_match_ptr(keystone_qmss_of_match
), dev
);
1773 if (match
&& match
->data
)
1774 kdev
->version
= QMSS_66AK2G
;
1776 platform_set_drvdata(pdev
, kdev
);
1778 INIT_LIST_HEAD(&kdev
->queue_ranges
);
1779 INIT_LIST_HEAD(&kdev
->qmgrs
);
1780 INIT_LIST_HEAD(&kdev
->pools
);
1781 INIT_LIST_HEAD(&kdev
->regions
);
1782 INIT_LIST_HEAD(&kdev
->pdsps
);
1784 pm_runtime_enable(&pdev
->dev
);
1785 ret
= pm_runtime_get_sync(&pdev
->dev
);
1787 pm_runtime_put_noidle(&pdev
->dev
);
1788 dev_err(dev
, "Failed to enable QMSS\n");
1792 if (of_property_read_u32_array(node
, "queue-range", temp
, 2)) {
1793 dev_err(dev
, "queue-range not specified\n");
1797 kdev
->base_id
= temp
[0];
1798 kdev
->num_queues
= temp
[1];
1800 /* Initialize queue managers using device tree configuration */
1801 qmgrs
= of_get_child_by_name(node
, "qmgrs");
1803 dev_err(dev
, "queue manager info not specified\n");
1807 ret
= knav_queue_init_qmgrs(kdev
, qmgrs
);
1812 /* get pdsp configuration values from device tree */
1813 pdsps
= of_get_child_by_name(node
, "pdsps");
1815 ret
= knav_queue_init_pdsps(kdev
, pdsps
);
1819 ret
= knav_queue_start_pdsps(kdev
);
1825 /* get usable queue range values from device tree */
1826 queue_pools
= of_get_child_by_name(node
, "queue-pools");
1828 dev_err(dev
, "queue-pools not specified\n");
1832 ret
= knav_setup_queue_pools(kdev
, queue_pools
);
1833 of_node_put(queue_pools
);
1837 ret
= knav_get_link_ram(kdev
, "linkram0", &kdev
->link_rams
[0]);
1839 dev_err(kdev
->dev
, "could not setup linking ram\n");
1843 ret
= knav_get_link_ram(kdev
, "linkram1", &kdev
->link_rams
[1]);
1846 * nothing really, we have one linking ram already, so we just
1847 * live within our means
1851 ret
= knav_queue_setup_link_ram(kdev
);
1855 regions
= of_get_child_by_name(node
, "descriptor-regions");
1857 dev_err(dev
, "descriptor-regions not specified\n");
1861 ret
= knav_queue_setup_regions(kdev
, regions
);
1862 of_node_put(regions
);
1866 ret
= knav_queue_init_queues(kdev
);
1868 dev_err(dev
, "hwqueue initialization failed\n");
1872 debugfs_create_file("qmss", S_IFREG
| S_IRUGO
, NULL
, NULL
,
1873 &knav_queue_debug_fops
);
1874 device_ready
= true;
1878 knav_queue_stop_pdsps(kdev
);
1879 knav_queue_free_regions(kdev
);
1880 knav_free_queue_ranges(kdev
);
1881 pm_runtime_put_sync(&pdev
->dev
);
1882 pm_runtime_disable(&pdev
->dev
);
1886 static int knav_queue_remove(struct platform_device
*pdev
)
1888 /* TODO: Free resources */
1889 pm_runtime_put_sync(&pdev
->dev
);
1890 pm_runtime_disable(&pdev
->dev
);
1894 static struct platform_driver keystone_qmss_driver
= {
1895 .probe
= knav_queue_probe
,
1896 .remove
= knav_queue_remove
,
1898 .name
= "keystone-navigator-qmss",
1899 .of_match_table
= keystone_qmss_of_match
,
1902 module_platform_driver(keystone_qmss_driver
);
1904 MODULE_LICENSE("GPL v2");
1905 MODULE_DESCRIPTION("TI QMSS driver for Keystone SOCs");
1906 MODULE_AUTHOR("Sandeep Nair <sandeep_n@ti.com>");
1907 MODULE_AUTHOR("Santosh Shilimkar <santosh.shilimkar@ti.com>");