2 * Keystone Queue Manager subsystem driver
4 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com
5 * Authors: Sandeep Nair <sandeep_n@ti.com>
6 * Cyril Chemparathy <cyril@ti.com>
7 * Santosh Shilimkar <santosh.shilimkar@ti.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
19 #include <linux/debugfs.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/firmware.h>
22 #include <linux/interrupt.h>
24 #include <linux/module.h>
25 #include <linux/of_address.h>
26 #include <linux/of_device.h>
27 #include <linux/of_irq.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/slab.h>
30 #include <linux/soc/ti/knav_qmss.h>
32 #include "knav_qmss.h"
34 static struct knav_device
*kdev
;
35 static DEFINE_MUTEX(knav_dev_lock
);
37 /* Queue manager register indices in DTS */
38 #define KNAV_QUEUE_PEEK_REG_INDEX 0
39 #define KNAV_QUEUE_STATUS_REG_INDEX 1
40 #define KNAV_QUEUE_CONFIG_REG_INDEX 2
41 #define KNAV_QUEUE_REGION_REG_INDEX 3
42 #define KNAV_QUEUE_PUSH_REG_INDEX 4
43 #define KNAV_QUEUE_POP_REG_INDEX 5
45 /* Queue manager register indices in DTS for QMSS in K2G NAVSS.
46 * There are no status and vbusm push registers on this version
47 * of QMSS. Push registers are same as pop, So all indices above 1
48 * are to be re-defined
50 #define KNAV_L_QUEUE_CONFIG_REG_INDEX 1
51 #define KNAV_L_QUEUE_REGION_REG_INDEX 2
52 #define KNAV_L_QUEUE_PUSH_REG_INDEX 3
54 /* PDSP register indices in DTS */
55 #define KNAV_QUEUE_PDSP_IRAM_REG_INDEX 0
56 #define KNAV_QUEUE_PDSP_REGS_REG_INDEX 1
57 #define KNAV_QUEUE_PDSP_INTD_REG_INDEX 2
58 #define KNAV_QUEUE_PDSP_CMD_REG_INDEX 3
60 #define knav_queue_idx_to_inst(kdev, idx) \
61 (kdev->instances + (idx << kdev->inst_shift))
63 #define for_each_handle_rcu(qh, inst) \
64 list_for_each_entry_rcu(qh, &inst->handles, list)
66 #define for_each_instance(idx, inst, kdev) \
67 for (idx = 0, inst = kdev->instances; \
68 idx < (kdev)->num_queues_in_use; \
69 idx++, inst = knav_queue_idx_to_inst(kdev, idx))
71 /* All firmware file names end up here. List the firmware file names below.
72 * Newest followed by older ones. Search is done from start of the array
73 * until a firmware file is found.
75 const char *knav_acc_firmwares
[] = {"ks2_qmss_pdsp_acc48.bin"};
77 static bool device_ready
;
78 bool knav_qmss_device_ready(void)
82 EXPORT_SYMBOL_GPL(knav_qmss_device_ready
);
85 * knav_queue_notify: qmss queue notfier call
87 * @inst: qmss queue instance like accumulator
89 void knav_queue_notify(struct knav_queue_inst
*inst
)
91 struct knav_queue
*qh
;
97 for_each_handle_rcu(qh
, inst
) {
98 if (atomic_read(&qh
->notifier_enabled
) <= 0)
100 if (WARN_ON(!qh
->notifier_fn
))
102 this_cpu_inc(qh
->stats
->notifies
);
103 qh
->notifier_fn(qh
->notifier_fn_arg
);
107 EXPORT_SYMBOL_GPL(knav_queue_notify
);
109 static irqreturn_t
knav_queue_int_handler(int irq
, void *_instdata
)
111 struct knav_queue_inst
*inst
= _instdata
;
113 knav_queue_notify(inst
);
117 static int knav_queue_setup_irq(struct knav_range_info
*range
,
118 struct knav_queue_inst
*inst
)
120 unsigned queue
= inst
->id
- range
->queue_base
;
123 if (range
->flags
& RANGE_HAS_IRQ
) {
124 irq
= range
->irqs
[queue
].irq
;
125 ret
= request_irq(irq
, knav_queue_int_handler
, 0,
126 inst
->irq_name
, inst
);
130 if (range
->irqs
[queue
].cpu_mask
) {
131 ret
= irq_set_affinity_hint(irq
, range
->irqs
[queue
].cpu_mask
);
133 dev_warn(range
->kdev
->dev
,
134 "Failed to set IRQ affinity\n");
142 static void knav_queue_free_irq(struct knav_queue_inst
*inst
)
144 struct knav_range_info
*range
= inst
->range
;
145 unsigned queue
= inst
->id
- inst
->range
->queue_base
;
148 if (range
->flags
& RANGE_HAS_IRQ
) {
149 irq
= range
->irqs
[queue
].irq
;
150 irq_set_affinity_hint(irq
, NULL
);
155 static inline bool knav_queue_is_busy(struct knav_queue_inst
*inst
)
157 return !list_empty(&inst
->handles
);
160 static inline bool knav_queue_is_reserved(struct knav_queue_inst
*inst
)
162 return inst
->range
->flags
& RANGE_RESERVED
;
165 static inline bool knav_queue_is_shared(struct knav_queue_inst
*inst
)
167 struct knav_queue
*tmp
;
170 for_each_handle_rcu(tmp
, inst
) {
171 if (tmp
->flags
& KNAV_QUEUE_SHARED
) {
180 static inline bool knav_queue_match_type(struct knav_queue_inst
*inst
,
183 if ((type
== KNAV_QUEUE_QPEND
) &&
184 (inst
->range
->flags
& RANGE_HAS_IRQ
)) {
186 } else if ((type
== KNAV_QUEUE_ACC
) &&
187 (inst
->range
->flags
& RANGE_HAS_ACCUMULATOR
)) {
189 } else if ((type
== KNAV_QUEUE_GP
) &&
190 !(inst
->range
->flags
&
191 (RANGE_HAS_ACCUMULATOR
| RANGE_HAS_IRQ
))) {
197 static inline struct knav_queue_inst
*
198 knav_queue_match_id_to_inst(struct knav_device
*kdev
, unsigned id
)
200 struct knav_queue_inst
*inst
;
203 for_each_instance(idx
, inst
, kdev
) {
210 static inline struct knav_queue_inst
*knav_queue_find_by_id(int id
)
212 if (kdev
->base_id
<= id
&&
213 kdev
->base_id
+ kdev
->num_queues
> id
) {
215 return knav_queue_match_id_to_inst(kdev
, id
);
220 static struct knav_queue
*__knav_queue_open(struct knav_queue_inst
*inst
,
221 const char *name
, unsigned flags
)
223 struct knav_queue
*qh
;
227 qh
= devm_kzalloc(inst
->kdev
->dev
, sizeof(*qh
), GFP_KERNEL
);
229 return ERR_PTR(-ENOMEM
);
231 qh
->stats
= alloc_percpu(struct knav_queue_stats
);
239 id
= inst
->id
- inst
->qmgr
->start_queue
;
240 qh
->reg_push
= &inst
->qmgr
->reg_push
[id
];
241 qh
->reg_pop
= &inst
->qmgr
->reg_pop
[id
];
242 qh
->reg_peek
= &inst
->qmgr
->reg_peek
[id
];
245 if (!knav_queue_is_busy(inst
)) {
246 struct knav_range_info
*range
= inst
->range
;
248 inst
->name
= kstrndup(name
, KNAV_NAME_SIZE
- 1, GFP_KERNEL
);
249 if (range
->ops
&& range
->ops
->open_queue
)
250 ret
= range
->ops
->open_queue(range
, inst
, flags
);
255 list_add_tail_rcu(&qh
->list
, &inst
->handles
);
260 free_percpu(qh
->stats
);
261 devm_kfree(inst
->kdev
->dev
, qh
);
265 static struct knav_queue
*
266 knav_queue_open_by_id(const char *name
, unsigned id
, unsigned flags
)
268 struct knav_queue_inst
*inst
;
269 struct knav_queue
*qh
;
271 mutex_lock(&knav_dev_lock
);
273 qh
= ERR_PTR(-ENODEV
);
274 inst
= knav_queue_find_by_id(id
);
278 qh
= ERR_PTR(-EEXIST
);
279 if (!(flags
& KNAV_QUEUE_SHARED
) && knav_queue_is_busy(inst
))
282 qh
= ERR_PTR(-EBUSY
);
283 if ((flags
& KNAV_QUEUE_SHARED
) &&
284 (knav_queue_is_busy(inst
) && !knav_queue_is_shared(inst
)))
287 qh
= __knav_queue_open(inst
, name
, flags
);
290 mutex_unlock(&knav_dev_lock
);
295 static struct knav_queue
*knav_queue_open_by_type(const char *name
,
296 unsigned type
, unsigned flags
)
298 struct knav_queue_inst
*inst
;
299 struct knav_queue
*qh
= ERR_PTR(-EINVAL
);
302 mutex_lock(&knav_dev_lock
);
304 for_each_instance(idx
, inst
, kdev
) {
305 if (knav_queue_is_reserved(inst
))
307 if (!knav_queue_match_type(inst
, type
))
309 if (knav_queue_is_busy(inst
))
311 qh
= __knav_queue_open(inst
, name
, flags
);
316 mutex_unlock(&knav_dev_lock
);
320 static void knav_queue_set_notify(struct knav_queue_inst
*inst
, bool enabled
)
322 struct knav_range_info
*range
= inst
->range
;
324 if (range
->ops
&& range
->ops
->set_notify
)
325 range
->ops
->set_notify(range
, inst
, enabled
);
328 static int knav_queue_enable_notifier(struct knav_queue
*qh
)
330 struct knav_queue_inst
*inst
= qh
->inst
;
333 if (WARN_ON(!qh
->notifier_fn
))
336 /* Adjust the per handle notifier count */
337 first
= (atomic_inc_return(&qh
->notifier_enabled
) == 1);
339 return 0; /* nothing to do */
341 /* Now adjust the per instance notifier count */
342 first
= (atomic_inc_return(&inst
->num_notifiers
) == 1);
344 knav_queue_set_notify(inst
, true);
349 static int knav_queue_disable_notifier(struct knav_queue
*qh
)
351 struct knav_queue_inst
*inst
= qh
->inst
;
354 last
= (atomic_dec_return(&qh
->notifier_enabled
) == 0);
356 return 0; /* nothing to do */
358 last
= (atomic_dec_return(&inst
->num_notifiers
) == 0);
360 knav_queue_set_notify(inst
, false);
365 static int knav_queue_set_notifier(struct knav_queue
*qh
,
366 struct knav_queue_notify_config
*cfg
)
368 knav_queue_notify_fn old_fn
= qh
->notifier_fn
;
373 if (!(qh
->inst
->range
->flags
& (RANGE_HAS_ACCUMULATOR
| RANGE_HAS_IRQ
)))
376 if (!cfg
->fn
&& old_fn
)
377 knav_queue_disable_notifier(qh
);
379 qh
->notifier_fn
= cfg
->fn
;
380 qh
->notifier_fn_arg
= cfg
->fn_arg
;
382 if (cfg
->fn
&& !old_fn
)
383 knav_queue_enable_notifier(qh
);
388 static int knav_gp_set_notify(struct knav_range_info
*range
,
389 struct knav_queue_inst
*inst
,
394 if (range
->flags
& RANGE_HAS_IRQ
) {
395 queue
= inst
->id
- range
->queue_base
;
397 enable_irq(range
->irqs
[queue
].irq
);
399 disable_irq_nosync(range
->irqs
[queue
].irq
);
404 static int knav_gp_open_queue(struct knav_range_info
*range
,
405 struct knav_queue_inst
*inst
, unsigned flags
)
407 return knav_queue_setup_irq(range
, inst
);
410 static int knav_gp_close_queue(struct knav_range_info
*range
,
411 struct knav_queue_inst
*inst
)
413 knav_queue_free_irq(inst
);
417 struct knav_range_ops knav_gp_range_ops
= {
418 .set_notify
= knav_gp_set_notify
,
419 .open_queue
= knav_gp_open_queue
,
420 .close_queue
= knav_gp_close_queue
,
424 static int knav_queue_get_count(void *qhandle
)
426 struct knav_queue
*qh
= qhandle
;
427 struct knav_queue_inst
*inst
= qh
->inst
;
429 return readl_relaxed(&qh
->reg_peek
[0].entry_count
) +
430 atomic_read(&inst
->desc_count
);
433 static void knav_queue_debug_show_instance(struct seq_file
*s
,
434 struct knav_queue_inst
*inst
)
436 struct knav_device
*kdev
= inst
->kdev
;
437 struct knav_queue
*qh
;
445 if (!knav_queue_is_busy(inst
))
448 seq_printf(s
, "\tqueue id %d (%s)\n",
449 kdev
->base_id
+ inst
->id
, inst
->name
);
450 for_each_handle_rcu(qh
, inst
) {
451 for_each_possible_cpu(cpu
) {
452 pushes
+= per_cpu_ptr(qh
->stats
, cpu
)->pushes
;
453 pops
+= per_cpu_ptr(qh
->stats
, cpu
)->pops
;
454 push_errors
+= per_cpu_ptr(qh
->stats
, cpu
)->push_errors
;
455 pop_errors
+= per_cpu_ptr(qh
->stats
, cpu
)->pop_errors
;
456 notifies
+= per_cpu_ptr(qh
->stats
, cpu
)->notifies
;
459 seq_printf(s
, "\t\thandle %p: pushes %8d, pops %8d, count %8d, notifies %8d, push errors %8d, pop errors %8d\n",
463 knav_queue_get_count(qh
),
470 static int knav_queue_debug_show(struct seq_file
*s
, void *v
)
472 struct knav_queue_inst
*inst
;
475 mutex_lock(&knav_dev_lock
);
476 seq_printf(s
, "%s: %u-%u\n",
477 dev_name(kdev
->dev
), kdev
->base_id
,
478 kdev
->base_id
+ kdev
->num_queues
- 1);
479 for_each_instance(idx
, inst
, kdev
)
480 knav_queue_debug_show_instance(s
, inst
);
481 mutex_unlock(&knav_dev_lock
);
486 static int knav_queue_debug_open(struct inode
*inode
, struct file
*file
)
488 return single_open(file
, knav_queue_debug_show
, NULL
);
491 static const struct file_operations knav_queue_debug_ops
= {
492 .open
= knav_queue_debug_open
,
495 .release
= single_release
,
498 static inline int knav_queue_pdsp_wait(u32
* __iomem addr
, unsigned timeout
,
504 end
= jiffies
+ msecs_to_jiffies(timeout
);
505 while (time_after(end
, jiffies
)) {
506 val
= readl_relaxed(addr
);
513 return val
? -ETIMEDOUT
: 0;
517 static int knav_queue_flush(struct knav_queue
*qh
)
519 struct knav_queue_inst
*inst
= qh
->inst
;
520 unsigned id
= inst
->id
- inst
->qmgr
->start_queue
;
522 atomic_set(&inst
->desc_count
, 0);
523 writel_relaxed(0, &inst
->qmgr
->reg_push
[id
].ptr_size_thresh
);
528 * knav_queue_open() - open a hardware queue
529 * @name - name to give the queue handle
530 * @id - desired queue number if any or specifes the type
532 * @flags - the following flags are applicable to queues:
533 * KNAV_QUEUE_SHARED - allow the queue to be shared. Queues are
534 * exclusive by default.
535 * Subsequent attempts to open a shared queue should
536 * also have this flag.
538 * Returns a handle to the open hardware queue if successful. Use IS_ERR()
539 * to check the returned value for error codes.
541 void *knav_queue_open(const char *name
, unsigned id
,
544 struct knav_queue
*qh
= ERR_PTR(-EINVAL
);
547 case KNAV_QUEUE_QPEND
:
550 qh
= knav_queue_open_by_type(name
, id
, flags
);
554 qh
= knav_queue_open_by_id(name
, id
, flags
);
559 EXPORT_SYMBOL_GPL(knav_queue_open
);
562 * knav_queue_close() - close a hardware queue handle
563 * @qh - handle to close
565 void knav_queue_close(void *qhandle
)
567 struct knav_queue
*qh
= qhandle
;
568 struct knav_queue_inst
*inst
= qh
->inst
;
570 while (atomic_read(&qh
->notifier_enabled
) > 0)
571 knav_queue_disable_notifier(qh
);
573 mutex_lock(&knav_dev_lock
);
574 list_del_rcu(&qh
->list
);
575 mutex_unlock(&knav_dev_lock
);
577 if (!knav_queue_is_busy(inst
)) {
578 struct knav_range_info
*range
= inst
->range
;
580 if (range
->ops
&& range
->ops
->close_queue
)
581 range
->ops
->close_queue(range
, inst
);
583 free_percpu(qh
->stats
);
584 devm_kfree(inst
->kdev
->dev
, qh
);
586 EXPORT_SYMBOL_GPL(knav_queue_close
);
589 * knav_queue_device_control() - Perform control operations on a queue
591 * @cmd - control commands
592 * @arg - command argument
594 * Returns 0 on success, errno otherwise.
596 int knav_queue_device_control(void *qhandle
, enum knav_queue_ctrl_cmd cmd
,
599 struct knav_queue
*qh
= qhandle
;
600 struct knav_queue_notify_config
*cfg
;
604 case KNAV_QUEUE_GET_ID
:
605 ret
= qh
->inst
->kdev
->base_id
+ qh
->inst
->id
;
608 case KNAV_QUEUE_FLUSH
:
609 ret
= knav_queue_flush(qh
);
612 case KNAV_QUEUE_SET_NOTIFIER
:
614 ret
= knav_queue_set_notifier(qh
, cfg
);
617 case KNAV_QUEUE_ENABLE_NOTIFY
:
618 ret
= knav_queue_enable_notifier(qh
);
621 case KNAV_QUEUE_DISABLE_NOTIFY
:
622 ret
= knav_queue_disable_notifier(qh
);
625 case KNAV_QUEUE_GET_COUNT
:
626 ret
= knav_queue_get_count(qh
);
635 EXPORT_SYMBOL_GPL(knav_queue_device_control
);
640 * knav_queue_push() - push data (or descriptor) to the tail of a queue
641 * @qh - hardware queue handle
642 * @data - data to push
643 * @size - size of data to push
644 * @flags - can be used to pass additional information
646 * Returns 0 on success, errno otherwise.
648 int knav_queue_push(void *qhandle
, dma_addr_t dma
,
649 unsigned size
, unsigned flags
)
651 struct knav_queue
*qh
= qhandle
;
654 val
= (u32
)dma
| ((size
/ 16) - 1);
655 writel_relaxed(val
, &qh
->reg_push
[0].ptr_size_thresh
);
657 this_cpu_inc(qh
->stats
->pushes
);
660 EXPORT_SYMBOL_GPL(knav_queue_push
);
663 * knav_queue_pop() - pop data (or descriptor) from the head of a queue
664 * @qh - hardware queue handle
665 * @size - (optional) size of the data pop'ed.
667 * Returns a DMA address on success, 0 on failure.
669 dma_addr_t
knav_queue_pop(void *qhandle
, unsigned *size
)
671 struct knav_queue
*qh
= qhandle
;
672 struct knav_queue_inst
*inst
= qh
->inst
;
676 /* are we accumulated? */
678 if (unlikely(atomic_dec_return(&inst
->desc_count
) < 0)) {
679 atomic_inc(&inst
->desc_count
);
682 idx
= atomic_inc_return(&inst
->desc_head
);
683 idx
&= ACC_DESCS_MASK
;
684 val
= inst
->descs
[idx
];
686 val
= readl_relaxed(&qh
->reg_pop
[0].ptr_size_thresh
);
691 dma
= val
& DESC_PTR_MASK
;
693 *size
= ((val
& DESC_SIZE_MASK
) + 1) * 16;
695 this_cpu_inc(qh
->stats
->pops
);
698 EXPORT_SYMBOL_GPL(knav_queue_pop
);
700 /* carve out descriptors and push into queue */
701 static void kdesc_fill_pool(struct knav_pool
*pool
)
703 struct knav_region
*region
;
706 region
= pool
->region
;
707 pool
->desc_size
= region
->desc_size
;
708 for (i
= 0; i
< pool
->num_desc
; i
++) {
709 int index
= pool
->region_offset
+ i
;
712 dma_addr
= region
->dma_start
+ (region
->desc_size
* index
);
713 dma_size
= ALIGN(pool
->desc_size
, SMP_CACHE_BYTES
);
714 dma_sync_single_for_device(pool
->dev
, dma_addr
, dma_size
,
716 knav_queue_push(pool
->queue
, dma_addr
, dma_size
, 0);
720 /* pop out descriptors and close the queue */
721 static void kdesc_empty_pool(struct knav_pool
*pool
)
732 dma
= knav_queue_pop(pool
->queue
, &size
);
735 desc
= knav_pool_desc_dma_to_virt(pool
, dma
);
737 dev_dbg(pool
->kdev
->dev
,
738 "couldn't unmap desc, continuing\n");
742 WARN_ON(i
!= pool
->num_desc
);
743 knav_queue_close(pool
->queue
);
747 /* Get the DMA address of a descriptor */
748 dma_addr_t
knav_pool_desc_virt_to_dma(void *ph
, void *virt
)
750 struct knav_pool
*pool
= ph
;
751 return pool
->region
->dma_start
+ (virt
- pool
->region
->virt_start
);
753 EXPORT_SYMBOL_GPL(knav_pool_desc_virt_to_dma
);
755 void *knav_pool_desc_dma_to_virt(void *ph
, dma_addr_t dma
)
757 struct knav_pool
*pool
= ph
;
758 return pool
->region
->virt_start
+ (dma
- pool
->region
->dma_start
);
760 EXPORT_SYMBOL_GPL(knav_pool_desc_dma_to_virt
);
763 * knav_pool_create() - Create a pool of descriptors
764 * @name - name to give the pool handle
765 * @num_desc - numbers of descriptors in the pool
766 * @region_id - QMSS region id from which the descriptors are to be
769 * Returns a pool handle on success.
770 * Use IS_ERR_OR_NULL() to identify error values on return.
772 void *knav_pool_create(const char *name
,
773 int num_desc
, int region_id
)
775 struct knav_region
*reg_itr
, *region
= NULL
;
776 struct knav_pool
*pool
, *pi
;
777 struct list_head
*node
;
778 unsigned last_offset
;
783 return ERR_PTR(-EPROBE_DEFER
);
786 return ERR_PTR(-ENODEV
);
788 pool
= devm_kzalloc(kdev
->dev
, sizeof(*pool
), GFP_KERNEL
);
790 dev_err(kdev
->dev
, "out of memory allocating pool\n");
791 return ERR_PTR(-ENOMEM
);
794 for_each_region(kdev
, reg_itr
) {
795 if (reg_itr
->id
!= region_id
)
802 dev_err(kdev
->dev
, "region-id(%d) not found\n", region_id
);
807 pool
->queue
= knav_queue_open(name
, KNAV_QUEUE_GP
, 0);
808 if (IS_ERR_OR_NULL(pool
->queue
)) {
810 "failed to open queue for pool(%s), error %ld\n",
811 name
, PTR_ERR(pool
->queue
));
812 ret
= PTR_ERR(pool
->queue
);
816 pool
->name
= kstrndup(name
, KNAV_NAME_SIZE
- 1, GFP_KERNEL
);
818 pool
->dev
= kdev
->dev
;
820 mutex_lock(&knav_dev_lock
);
822 if (num_desc
> (region
->num_desc
- region
->used_desc
)) {
823 dev_err(kdev
->dev
, "out of descs in region(%d) for pool(%s)\n",
829 /* Region maintains a sorted (by region offset) list of pools
830 * use the first free slot which is large enough to accomodate
835 node
= ®ion
->pools
;
836 list_for_each_entry(pi
, ®ion
->pools
, region_inst
) {
837 if ((pi
->region_offset
- last_offset
) >= num_desc
) {
841 last_offset
= pi
->region_offset
+ pi
->num_desc
;
843 node
= &pi
->region_inst
;
846 pool
->region
= region
;
847 pool
->num_desc
= num_desc
;
848 pool
->region_offset
= last_offset
;
849 region
->used_desc
+= num_desc
;
850 list_add_tail(&pool
->list
, &kdev
->pools
);
851 list_add_tail(&pool
->region_inst
, node
);
853 dev_err(kdev
->dev
, "pool(%s) create failed: fragmented desc pool in region(%d)\n",
859 mutex_unlock(&knav_dev_lock
);
860 kdesc_fill_pool(pool
);
864 mutex_unlock(&knav_dev_lock
);
867 devm_kfree(kdev
->dev
, pool
);
870 EXPORT_SYMBOL_GPL(knav_pool_create
);
873 * knav_pool_destroy() - Free a pool of descriptors
874 * @pool - pool handle
876 void knav_pool_destroy(void *ph
)
878 struct knav_pool
*pool
= ph
;
886 kdesc_empty_pool(pool
);
887 mutex_lock(&knav_dev_lock
);
889 pool
->region
->used_desc
-= pool
->num_desc
;
890 list_del(&pool
->region_inst
);
891 list_del(&pool
->list
);
893 mutex_unlock(&knav_dev_lock
);
895 devm_kfree(kdev
->dev
, pool
);
897 EXPORT_SYMBOL_GPL(knav_pool_destroy
);
901 * knav_pool_desc_get() - Get a descriptor from the pool
902 * @pool - pool handle
904 * Returns descriptor from the pool.
906 void *knav_pool_desc_get(void *ph
)
908 struct knav_pool
*pool
= ph
;
913 dma
= knav_queue_pop(pool
->queue
, &size
);
915 return ERR_PTR(-ENOMEM
);
916 data
= knav_pool_desc_dma_to_virt(pool
, dma
);
919 EXPORT_SYMBOL_GPL(knav_pool_desc_get
);
922 * knav_pool_desc_put() - return a descriptor to the pool
923 * @pool - pool handle
925 void knav_pool_desc_put(void *ph
, void *desc
)
927 struct knav_pool
*pool
= ph
;
929 dma
= knav_pool_desc_virt_to_dma(pool
, desc
);
930 knav_queue_push(pool
->queue
, dma
, pool
->region
->desc_size
, 0);
932 EXPORT_SYMBOL_GPL(knav_pool_desc_put
);
935 * knav_pool_desc_map() - Map descriptor for DMA transfer
936 * @pool - pool handle
937 * @desc - address of descriptor to map
938 * @size - size of descriptor to map
939 * @dma - DMA address return pointer
940 * @dma_sz - adjusted return pointer
942 * Returns 0 on success, errno otherwise.
944 int knav_pool_desc_map(void *ph
, void *desc
, unsigned size
,
945 dma_addr_t
*dma
, unsigned *dma_sz
)
947 struct knav_pool
*pool
= ph
;
948 *dma
= knav_pool_desc_virt_to_dma(pool
, desc
);
949 size
= min(size
, pool
->region
->desc_size
);
950 size
= ALIGN(size
, SMP_CACHE_BYTES
);
952 dma_sync_single_for_device(pool
->dev
, *dma
, size
, DMA_TO_DEVICE
);
954 /* Ensure the descriptor reaches to the memory */
959 EXPORT_SYMBOL_GPL(knav_pool_desc_map
);
962 * knav_pool_desc_unmap() - Unmap descriptor after DMA transfer
963 * @pool - pool handle
964 * @dma - DMA address of descriptor to unmap
965 * @dma_sz - size of descriptor to unmap
967 * Returns descriptor address on success, Use IS_ERR_OR_NULL() to identify
968 * error values on return.
970 void *knav_pool_desc_unmap(void *ph
, dma_addr_t dma
, unsigned dma_sz
)
972 struct knav_pool
*pool
= ph
;
976 desc_sz
= min(dma_sz
, pool
->region
->desc_size
);
977 desc
= knav_pool_desc_dma_to_virt(pool
, dma
);
978 dma_sync_single_for_cpu(pool
->dev
, dma
, desc_sz
, DMA_FROM_DEVICE
);
982 EXPORT_SYMBOL_GPL(knav_pool_desc_unmap
);
985 * knav_pool_count() - Get the number of descriptors in pool.
986 * @pool - pool handle
987 * Returns number of elements in the pool.
989 int knav_pool_count(void *ph
)
991 struct knav_pool
*pool
= ph
;
992 return knav_queue_get_count(pool
->queue
);
994 EXPORT_SYMBOL_GPL(knav_pool_count
);
996 static void knav_queue_setup_region(struct knav_device
*kdev
,
997 struct knav_region
*region
)
999 unsigned hw_num_desc
, hw_desc_size
, size
;
1000 struct knav_reg_region __iomem
*regs
;
1001 struct knav_qmgr_info
*qmgr
;
1002 struct knav_pool
*pool
;
1003 int id
= region
->id
;
1006 /* unused region? */
1007 if (!region
->num_desc
) {
1008 dev_warn(kdev
->dev
, "unused region %s\n", region
->name
);
1012 /* get hardware descriptor value */
1013 hw_num_desc
= ilog2(region
->num_desc
- 1) + 1;
1015 /* did we force fit ourselves into nothingness? */
1016 if (region
->num_desc
< 32) {
1017 region
->num_desc
= 0;
1018 dev_warn(kdev
->dev
, "too few descriptors in region %s\n",
1023 size
= region
->num_desc
* region
->desc_size
;
1024 region
->virt_start
= alloc_pages_exact(size
, GFP_KERNEL
| GFP_DMA
|
1026 if (!region
->virt_start
) {
1027 region
->num_desc
= 0;
1028 dev_err(kdev
->dev
, "memory alloc failed for region %s\n",
1032 region
->virt_end
= region
->virt_start
+ size
;
1033 page
= virt_to_page(region
->virt_start
);
1035 region
->dma_start
= dma_map_page(kdev
->dev
, page
, 0, size
,
1037 if (dma_mapping_error(kdev
->dev
, region
->dma_start
)) {
1038 dev_err(kdev
->dev
, "dma map failed for region %s\n",
1042 region
->dma_end
= region
->dma_start
+ size
;
1044 pool
= devm_kzalloc(kdev
->dev
, sizeof(*pool
), GFP_KERNEL
);
1046 dev_err(kdev
->dev
, "out of memory allocating dummy pool\n");
1050 pool
->region_offset
= region
->num_desc
;
1051 list_add(&pool
->region_inst
, ®ion
->pools
);
1054 "region %s (%d): size:%d, link:%d@%d, dma:%pad-%pad, virt:%p-%p\n",
1055 region
->name
, id
, region
->desc_size
, region
->num_desc
,
1056 region
->link_index
, ®ion
->dma_start
, ®ion
->dma_end
,
1057 region
->virt_start
, region
->virt_end
);
1059 hw_desc_size
= (region
->desc_size
/ 16) - 1;
1062 for_each_qmgr(kdev
, qmgr
) {
1063 regs
= qmgr
->reg_region
+ id
;
1064 writel_relaxed((u32
)region
->dma_start
, ®s
->base
);
1065 writel_relaxed(region
->link_index
, ®s
->start_index
);
1066 writel_relaxed(hw_desc_size
<< 16 | hw_num_desc
,
1072 if (region
->dma_start
)
1073 dma_unmap_page(kdev
->dev
, region
->dma_start
, size
,
1075 if (region
->virt_start
)
1076 free_pages_exact(region
->virt_start
, size
);
1077 region
->num_desc
= 0;
1081 static const char *knav_queue_find_name(struct device_node
*node
)
1085 if (of_property_read_string(node
, "label", &name
) < 0)
1092 static int knav_queue_setup_regions(struct knav_device
*kdev
,
1093 struct device_node
*regions
)
1095 struct device
*dev
= kdev
->dev
;
1096 struct knav_region
*region
;
1097 struct device_node
*child
;
1101 for_each_child_of_node(regions
, child
) {
1102 region
= devm_kzalloc(dev
, sizeof(*region
), GFP_KERNEL
);
1104 dev_err(dev
, "out of memory allocating region\n");
1108 region
->name
= knav_queue_find_name(child
);
1109 of_property_read_u32(child
, "id", ®ion
->id
);
1110 ret
= of_property_read_u32_array(child
, "region-spec", temp
, 2);
1112 region
->num_desc
= temp
[0];
1113 region
->desc_size
= temp
[1];
1115 dev_err(dev
, "invalid region info %s\n", region
->name
);
1116 devm_kfree(dev
, region
);
1120 if (!of_get_property(child
, "link-index", NULL
)) {
1121 dev_err(dev
, "No link info for %s\n", region
->name
);
1122 devm_kfree(dev
, region
);
1125 ret
= of_property_read_u32(child
, "link-index",
1126 ®ion
->link_index
);
1128 dev_err(dev
, "link index not found for %s\n",
1130 devm_kfree(dev
, region
);
1134 INIT_LIST_HEAD(®ion
->pools
);
1135 list_add_tail(®ion
->list
, &kdev
->regions
);
1137 if (list_empty(&kdev
->regions
)) {
1138 dev_err(dev
, "no valid region information found\n");
1142 /* Next, we run through the regions and set things up */
1143 for_each_region(kdev
, region
)
1144 knav_queue_setup_region(kdev
, region
);
1149 static int knav_get_link_ram(struct knav_device
*kdev
,
1151 struct knav_link_ram_block
*block
)
1153 struct platform_device
*pdev
= to_platform_device(kdev
->dev
);
1154 struct device_node
*node
= pdev
->dev
.of_node
;
1158 * Note: link ram resources are specified in "entry" sized units. In
1159 * reality, although entries are ~40bits in hardware, we treat them as
1160 * 64-bit entities here.
1162 * For example, to specify the internal link ram for Keystone-I class
1163 * devices, we would set the linkram0 resource to 0x80000-0x83fff.
1165 * This gets a bit weird when other link rams are used. For example,
1166 * if the range specified is 0x0c000000-0x0c003fff (i.e., 16K entries
1167 * in MSMC SRAM), the actual memory used is 0x0c000000-0x0c020000,
1168 * which accounts for 64-bits per entry, for 16K entries.
1170 if (!of_property_read_u32_array(node
, name
, temp
, 2)) {
1173 * queue_base specified => using internal or onchip
1174 * link ram WARNING - we do not "reserve" this block
1176 block
->dma
= (dma_addr_t
)temp
[0];
1178 block
->size
= temp
[1];
1180 block
->size
= temp
[1];
1181 /* queue_base not specific => allocate requested size */
1182 block
->virt
= dmam_alloc_coherent(kdev
->dev
,
1183 8 * block
->size
, &block
->dma
,
1186 dev_err(kdev
->dev
, "failed to alloc linkram\n");
1196 static int knav_queue_setup_link_ram(struct knav_device
*kdev
)
1198 struct knav_link_ram_block
*block
;
1199 struct knav_qmgr_info
*qmgr
;
1201 for_each_qmgr(kdev
, qmgr
) {
1202 block
= &kdev
->link_rams
[0];
1203 dev_dbg(kdev
->dev
, "linkram0: dma:%pad, virt:%p, size:%x\n",
1204 &block
->dma
, block
->virt
, block
->size
);
1205 writel_relaxed((u32
)block
->dma
, &qmgr
->reg_config
->link_ram_base0
);
1206 if (kdev
->version
== QMSS_66AK2G
)
1207 writel_relaxed(block
->size
,
1208 &qmgr
->reg_config
->link_ram_size0
);
1210 writel_relaxed(block
->size
- 1,
1211 &qmgr
->reg_config
->link_ram_size0
);
1216 dev_dbg(kdev
->dev
, "linkram1: dma:%pad, virt:%p, size:%x\n",
1217 &block
->dma
, block
->virt
, block
->size
);
1218 writel_relaxed(block
->dma
, &qmgr
->reg_config
->link_ram_base1
);
1224 static int knav_setup_queue_range(struct knav_device
*kdev
,
1225 struct device_node
*node
)
1227 struct device
*dev
= kdev
->dev
;
1228 struct knav_range_info
*range
;
1229 struct knav_qmgr_info
*qmgr
;
1230 u32 temp
[2], start
, end
, id
, index
;
1233 range
= devm_kzalloc(dev
, sizeof(*range
), GFP_KERNEL
);
1235 dev_err(dev
, "out of memory allocating range\n");
1240 range
->name
= knav_queue_find_name(node
);
1241 ret
= of_property_read_u32_array(node
, "qrange", temp
, 2);
1243 range
->queue_base
= temp
[0] - kdev
->base_id
;
1244 range
->num_queues
= temp
[1];
1246 dev_err(dev
, "invalid queue range %s\n", range
->name
);
1247 devm_kfree(dev
, range
);
1251 for (i
= 0; i
< RANGE_MAX_IRQS
; i
++) {
1252 struct of_phandle_args oirq
;
1254 if (of_irq_parse_one(node
, i
, &oirq
))
1257 range
->irqs
[i
].irq
= irq_create_of_mapping(&oirq
);
1258 if (range
->irqs
[i
].irq
== IRQ_NONE
)
1263 if (IS_ENABLED(CONFIG_SMP
) && oirq
.args_count
== 3) {
1267 range
->irqs
[i
].cpu_mask
= devm_kzalloc(dev
,
1268 cpumask_size(), GFP_KERNEL
);
1269 if (!range
->irqs
[i
].cpu_mask
)
1272 mask
= (oirq
.args
[2] & 0x0000ff00) >> 8;
1273 for_each_set_bit(bit
, &mask
, BITS_PER_LONG
)
1274 cpumask_set_cpu(bit
, range
->irqs
[i
].cpu_mask
);
1278 range
->num_irqs
= min(range
->num_irqs
, range
->num_queues
);
1279 if (range
->num_irqs
)
1280 range
->flags
|= RANGE_HAS_IRQ
;
1282 if (of_get_property(node
, "qalloc-by-id", NULL
))
1283 range
->flags
|= RANGE_RESERVED
;
1285 if (of_get_property(node
, "accumulator", NULL
)) {
1286 ret
= knav_init_acc_range(kdev
, node
, range
);
1288 devm_kfree(dev
, range
);
1292 range
->ops
= &knav_gp_range_ops
;
1295 /* set threshold to 1, and flush out the queues */
1296 for_each_qmgr(kdev
, qmgr
) {
1297 start
= max(qmgr
->start_queue
, range
->queue_base
);
1298 end
= min(qmgr
->start_queue
+ qmgr
->num_queues
,
1299 range
->queue_base
+ range
->num_queues
);
1300 for (id
= start
; id
< end
; id
++) {
1301 index
= id
- qmgr
->start_queue
;
1302 writel_relaxed(THRESH_GTE
| 1,
1303 &qmgr
->reg_peek
[index
].ptr_size_thresh
);
1305 &qmgr
->reg_push
[index
].ptr_size_thresh
);
1309 list_add_tail(&range
->list
, &kdev
->queue_ranges
);
1310 dev_dbg(dev
, "added range %s: %d-%d, %d irqs%s%s%s\n",
1311 range
->name
, range
->queue_base
,
1312 range
->queue_base
+ range
->num_queues
- 1,
1314 (range
->flags
& RANGE_HAS_IRQ
) ? ", has irq" : "",
1315 (range
->flags
& RANGE_RESERVED
) ? ", reserved" : "",
1316 (range
->flags
& RANGE_HAS_ACCUMULATOR
) ? ", acc" : "");
1317 kdev
->num_queues_in_use
+= range
->num_queues
;
1321 static int knav_setup_queue_pools(struct knav_device
*kdev
,
1322 struct device_node
*queue_pools
)
1324 struct device_node
*type
, *range
;
1327 for_each_child_of_node(queue_pools
, type
) {
1328 for_each_child_of_node(type
, range
) {
1329 ret
= knav_setup_queue_range(kdev
, range
);
1330 /* return value ignored, we init the rest... */
1334 /* ... and barf if they all failed! */
1335 if (list_empty(&kdev
->queue_ranges
)) {
1336 dev_err(kdev
->dev
, "no valid queue range found\n");
1342 static void knav_free_queue_range(struct knav_device
*kdev
,
1343 struct knav_range_info
*range
)
1345 if (range
->ops
&& range
->ops
->free_range
)
1346 range
->ops
->free_range(range
);
1347 list_del(&range
->list
);
1348 devm_kfree(kdev
->dev
, range
);
1351 static void knav_free_queue_ranges(struct knav_device
*kdev
)
1353 struct knav_range_info
*range
;
1356 range
= first_queue_range(kdev
);
1359 knav_free_queue_range(kdev
, range
);
1363 static void knav_queue_free_regions(struct knav_device
*kdev
)
1365 struct knav_region
*region
;
1366 struct knav_pool
*pool
, *tmp
;
1370 region
= first_region(kdev
);
1373 list_for_each_entry_safe(pool
, tmp
, ®ion
->pools
, region_inst
)
1374 knav_pool_destroy(pool
);
1376 size
= region
->virt_end
- region
->virt_start
;
1378 free_pages_exact(region
->virt_start
, size
);
1379 list_del(®ion
->list
);
1380 devm_kfree(kdev
->dev
, region
);
1384 static void __iomem
*knav_queue_map_reg(struct knav_device
*kdev
,
1385 struct device_node
*node
, int index
)
1387 struct resource res
;
1391 ret
= of_address_to_resource(node
, index
, &res
);
1393 dev_err(kdev
->dev
, "Can't translate of node(%s) address for index(%d)\n",
1395 return ERR_PTR(ret
);
1398 regs
= devm_ioremap_resource(kdev
->dev
, &res
);
1400 dev_err(kdev
->dev
, "Failed to map register base for index(%d) node(%s)\n",
1405 static int knav_queue_init_qmgrs(struct knav_device
*kdev
,
1406 struct device_node
*qmgrs
)
1408 struct device
*dev
= kdev
->dev
;
1409 struct knav_qmgr_info
*qmgr
;
1410 struct device_node
*child
;
1414 for_each_child_of_node(qmgrs
, child
) {
1415 qmgr
= devm_kzalloc(dev
, sizeof(*qmgr
), GFP_KERNEL
);
1417 dev_err(dev
, "out of memory allocating qmgr\n");
1421 ret
= of_property_read_u32_array(child
, "managed-queues",
1424 qmgr
->start_queue
= temp
[0];
1425 qmgr
->num_queues
= temp
[1];
1427 dev_err(dev
, "invalid qmgr queue range\n");
1428 devm_kfree(dev
, qmgr
);
1432 dev_info(dev
, "qmgr start queue %d, number of queues %d\n",
1433 qmgr
->start_queue
, qmgr
->num_queues
);
1436 knav_queue_map_reg(kdev
, child
,
1437 KNAV_QUEUE_PEEK_REG_INDEX
);
1439 if (kdev
->version
== QMSS
) {
1441 knav_queue_map_reg(kdev
, child
,
1442 KNAV_QUEUE_STATUS_REG_INDEX
);
1446 knav_queue_map_reg(kdev
, child
,
1447 (kdev
->version
== QMSS_66AK2G
) ?
1448 KNAV_L_QUEUE_CONFIG_REG_INDEX
:
1449 KNAV_QUEUE_CONFIG_REG_INDEX
);
1451 knav_queue_map_reg(kdev
, child
,
1452 (kdev
->version
== QMSS_66AK2G
) ?
1453 KNAV_L_QUEUE_REGION_REG_INDEX
:
1454 KNAV_QUEUE_REGION_REG_INDEX
);
1457 knav_queue_map_reg(kdev
, child
,
1458 (kdev
->version
== QMSS_66AK2G
) ?
1459 KNAV_L_QUEUE_PUSH_REG_INDEX
:
1460 KNAV_QUEUE_PUSH_REG_INDEX
);
1462 if (kdev
->version
== QMSS
) {
1464 knav_queue_map_reg(kdev
, child
,
1465 KNAV_QUEUE_POP_REG_INDEX
);
1468 if (IS_ERR(qmgr
->reg_peek
) ||
1469 ((kdev
->version
== QMSS
) &&
1470 (IS_ERR(qmgr
->reg_status
) || IS_ERR(qmgr
->reg_pop
))) ||
1471 IS_ERR(qmgr
->reg_config
) || IS_ERR(qmgr
->reg_region
) ||
1472 IS_ERR(qmgr
->reg_push
)) {
1473 dev_err(dev
, "failed to map qmgr regs\n");
1474 if (kdev
->version
== QMSS
) {
1475 if (!IS_ERR(qmgr
->reg_status
))
1476 devm_iounmap(dev
, qmgr
->reg_status
);
1477 if (!IS_ERR(qmgr
->reg_pop
))
1478 devm_iounmap(dev
, qmgr
->reg_pop
);
1480 if (!IS_ERR(qmgr
->reg_peek
))
1481 devm_iounmap(dev
, qmgr
->reg_peek
);
1482 if (!IS_ERR(qmgr
->reg_config
))
1483 devm_iounmap(dev
, qmgr
->reg_config
);
1484 if (!IS_ERR(qmgr
->reg_region
))
1485 devm_iounmap(dev
, qmgr
->reg_region
);
1486 if (!IS_ERR(qmgr
->reg_push
))
1487 devm_iounmap(dev
, qmgr
->reg_push
);
1488 devm_kfree(dev
, qmgr
);
1492 /* Use same push register for pop as well */
1493 if (kdev
->version
== QMSS_66AK2G
)
1494 qmgr
->reg_pop
= qmgr
->reg_push
;
1496 list_add_tail(&qmgr
->list
, &kdev
->qmgrs
);
1497 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",
1498 qmgr
->start_queue
, qmgr
->num_queues
,
1499 qmgr
->reg_peek
, qmgr
->reg_status
,
1500 qmgr
->reg_config
, qmgr
->reg_region
,
1501 qmgr
->reg_push
, qmgr
->reg_pop
);
1506 static int knav_queue_init_pdsps(struct knav_device
*kdev
,
1507 struct device_node
*pdsps
)
1509 struct device
*dev
= kdev
->dev
;
1510 struct knav_pdsp_info
*pdsp
;
1511 struct device_node
*child
;
1513 for_each_child_of_node(pdsps
, child
) {
1514 pdsp
= devm_kzalloc(dev
, sizeof(*pdsp
), GFP_KERNEL
);
1516 dev_err(dev
, "out of memory allocating pdsp\n");
1519 pdsp
->name
= knav_queue_find_name(child
);
1521 knav_queue_map_reg(kdev
, child
,
1522 KNAV_QUEUE_PDSP_IRAM_REG_INDEX
);
1524 knav_queue_map_reg(kdev
, child
,
1525 KNAV_QUEUE_PDSP_REGS_REG_INDEX
);
1527 knav_queue_map_reg(kdev
, child
,
1528 KNAV_QUEUE_PDSP_INTD_REG_INDEX
);
1530 knav_queue_map_reg(kdev
, child
,
1531 KNAV_QUEUE_PDSP_CMD_REG_INDEX
);
1533 if (IS_ERR(pdsp
->command
) || IS_ERR(pdsp
->iram
) ||
1534 IS_ERR(pdsp
->regs
) || IS_ERR(pdsp
->intd
)) {
1535 dev_err(dev
, "failed to map pdsp %s regs\n",
1537 if (!IS_ERR(pdsp
->command
))
1538 devm_iounmap(dev
, pdsp
->command
);
1539 if (!IS_ERR(pdsp
->iram
))
1540 devm_iounmap(dev
, pdsp
->iram
);
1541 if (!IS_ERR(pdsp
->regs
))
1542 devm_iounmap(dev
, pdsp
->regs
);
1543 if (!IS_ERR(pdsp
->intd
))
1544 devm_iounmap(dev
, pdsp
->intd
);
1545 devm_kfree(dev
, pdsp
);
1548 of_property_read_u32(child
, "id", &pdsp
->id
);
1549 list_add_tail(&pdsp
->list
, &kdev
->pdsps
);
1550 dev_dbg(dev
, "added pdsp %s: command %p, iram %p, regs %p, intd %p\n",
1551 pdsp
->name
, pdsp
->command
, pdsp
->iram
, pdsp
->regs
,
1557 static int knav_queue_stop_pdsp(struct knav_device
*kdev
,
1558 struct knav_pdsp_info
*pdsp
)
1560 u32 val
, timeout
= 1000;
1563 val
= readl_relaxed(&pdsp
->regs
->control
) & ~PDSP_CTRL_ENABLE
;
1564 writel_relaxed(val
, &pdsp
->regs
->control
);
1565 ret
= knav_queue_pdsp_wait(&pdsp
->regs
->control
, timeout
,
1568 dev_err(kdev
->dev
, "timed out on pdsp %s stop\n", pdsp
->name
);
1571 pdsp
->loaded
= false;
1572 pdsp
->started
= false;
1576 static int knav_queue_load_pdsp(struct knav_device
*kdev
,
1577 struct knav_pdsp_info
*pdsp
)
1580 const struct firmware
*fw
;
1584 for (i
= 0; i
< ARRAY_SIZE(knav_acc_firmwares
); i
++) {
1585 if (knav_acc_firmwares
[i
]) {
1586 ret
= request_firmware_direct(&fw
,
1587 knav_acc_firmwares
[i
],
1597 dev_err(kdev
->dev
, "failed to get firmware for pdsp\n");
1601 dev_info(kdev
->dev
, "firmware file %s downloaded for PDSP\n",
1602 knav_acc_firmwares
[i
]);
1604 writel_relaxed(pdsp
->id
+ 1, pdsp
->command
+ 0x18);
1605 /* download the firmware */
1606 fwdata
= (u32
*)fw
->data
;
1607 fwlen
= (fw
->size
+ sizeof(u32
) - 1) / sizeof(u32
);
1608 for (i
= 0; i
< fwlen
; i
++)
1609 writel_relaxed(be32_to_cpu(fwdata
[i
]), pdsp
->iram
+ i
);
1611 release_firmware(fw
);
1615 static int knav_queue_start_pdsp(struct knav_device
*kdev
,
1616 struct knav_pdsp_info
*pdsp
)
1618 u32 val
, timeout
= 1000;
1621 /* write a command for sync */
1622 writel_relaxed(0xffffffff, pdsp
->command
);
1623 while (readl_relaxed(pdsp
->command
) != 0xffffffff)
1626 /* soft reset the PDSP */
1627 val
= readl_relaxed(&pdsp
->regs
->control
);
1628 val
&= ~(PDSP_CTRL_PC_MASK
| PDSP_CTRL_SOFT_RESET
);
1629 writel_relaxed(val
, &pdsp
->regs
->control
);
1632 val
= readl_relaxed(&pdsp
->regs
->control
) | PDSP_CTRL_ENABLE
;
1633 writel_relaxed(val
, &pdsp
->regs
->control
);
1635 /* wait for command register to clear */
1636 ret
= knav_queue_pdsp_wait(pdsp
->command
, timeout
, 0);
1639 "timed out on pdsp %s command register wait\n",
1646 static void knav_queue_stop_pdsps(struct knav_device
*kdev
)
1648 struct knav_pdsp_info
*pdsp
;
1650 /* disable all pdsps */
1651 for_each_pdsp(kdev
, pdsp
)
1652 knav_queue_stop_pdsp(kdev
, pdsp
);
1655 static int knav_queue_start_pdsps(struct knav_device
*kdev
)
1657 struct knav_pdsp_info
*pdsp
;
1660 knav_queue_stop_pdsps(kdev
);
1661 /* now load them all. We return success even if pdsp
1662 * is not loaded as acc channels are optional on having
1663 * firmware availability in the system. We set the loaded
1664 * and stated flag and when initialize the acc range, check
1665 * it and init the range only if pdsp is started.
1667 for_each_pdsp(kdev
, pdsp
) {
1668 ret
= knav_queue_load_pdsp(kdev
, pdsp
);
1670 pdsp
->loaded
= true;
1673 for_each_pdsp(kdev
, pdsp
) {
1675 ret
= knav_queue_start_pdsp(kdev
, pdsp
);
1677 pdsp
->started
= true;
1683 static inline struct knav_qmgr_info
*knav_find_qmgr(unsigned id
)
1685 struct knav_qmgr_info
*qmgr
;
1687 for_each_qmgr(kdev
, qmgr
) {
1688 if ((id
>= qmgr
->start_queue
) &&
1689 (id
< qmgr
->start_queue
+ qmgr
->num_queues
))
1695 static int knav_queue_init_queue(struct knav_device
*kdev
,
1696 struct knav_range_info
*range
,
1697 struct knav_queue_inst
*inst
,
1700 char irq_name
[KNAV_NAME_SIZE
];
1701 inst
->qmgr
= knav_find_qmgr(id
);
1705 INIT_LIST_HEAD(&inst
->handles
);
1707 inst
->range
= range
;
1710 scnprintf(irq_name
, sizeof(irq_name
), "hwqueue-%d", id
);
1711 inst
->irq_name
= kstrndup(irq_name
, sizeof(irq_name
), GFP_KERNEL
);
1713 if (range
->ops
&& range
->ops
->init_queue
)
1714 return range
->ops
->init_queue(range
, inst
);
1719 static int knav_queue_init_queues(struct knav_device
*kdev
)
1721 struct knav_range_info
*range
;
1722 int size
, id
, base_idx
;
1723 int idx
= 0, ret
= 0;
1725 /* how much do we need for instance data? */
1726 size
= sizeof(struct knav_queue_inst
);
1728 /* round this up to a power of 2, keep the index to instance
1731 kdev
->inst_shift
= order_base_2(size
);
1732 size
= (1 << kdev
->inst_shift
) * kdev
->num_queues_in_use
;
1733 kdev
->instances
= devm_kzalloc(kdev
->dev
, size
, GFP_KERNEL
);
1734 if (!kdev
->instances
)
1737 for_each_queue_range(kdev
, range
) {
1738 if (range
->ops
&& range
->ops
->init_range
)
1739 range
->ops
->init_range(range
);
1741 for (id
= range
->queue_base
;
1742 id
< range
->queue_base
+ range
->num_queues
; id
++, idx
++) {
1743 ret
= knav_queue_init_queue(kdev
, range
,
1744 knav_queue_idx_to_inst(kdev
, idx
), id
);
1748 range
->queue_base_inst
=
1749 knav_queue_idx_to_inst(kdev
, base_idx
);
1754 /* Match table for of_platform binding */
1755 static const struct of_device_id keystone_qmss_of_match
[] = {
1757 .compatible
= "ti,keystone-navigator-qmss",
1760 .compatible
= "ti,66ak2g-navss-qm",
1761 .data
= (void *)QMSS_66AK2G
,
1765 MODULE_DEVICE_TABLE(of
, keystone_qmss_of_match
);
1767 static int knav_queue_probe(struct platform_device
*pdev
)
1769 struct device_node
*node
= pdev
->dev
.of_node
;
1770 struct device_node
*qmgrs
, *queue_pools
, *regions
, *pdsps
;
1771 const struct of_device_id
*match
;
1772 struct device
*dev
= &pdev
->dev
;
1777 dev_err(dev
, "device tree info unavailable\n");
1781 kdev
= devm_kzalloc(dev
, sizeof(struct knav_device
), GFP_KERNEL
);
1783 dev_err(dev
, "memory allocation failed\n");
1787 match
= of_match_device(of_match_ptr(keystone_qmss_of_match
), dev
);
1788 if (match
&& match
->data
)
1789 kdev
->version
= QMSS_66AK2G
;
1791 platform_set_drvdata(pdev
, kdev
);
1793 INIT_LIST_HEAD(&kdev
->queue_ranges
);
1794 INIT_LIST_HEAD(&kdev
->qmgrs
);
1795 INIT_LIST_HEAD(&kdev
->pools
);
1796 INIT_LIST_HEAD(&kdev
->regions
);
1797 INIT_LIST_HEAD(&kdev
->pdsps
);
1799 pm_runtime_enable(&pdev
->dev
);
1800 ret
= pm_runtime_get_sync(&pdev
->dev
);
1802 dev_err(dev
, "Failed to enable QMSS\n");
1806 if (of_property_read_u32_array(node
, "queue-range", temp
, 2)) {
1807 dev_err(dev
, "queue-range not specified\n");
1811 kdev
->base_id
= temp
[0];
1812 kdev
->num_queues
= temp
[1];
1814 /* Initialize queue managers using device tree configuration */
1815 qmgrs
= of_get_child_by_name(node
, "qmgrs");
1817 dev_err(dev
, "queue manager info not specified\n");
1821 ret
= knav_queue_init_qmgrs(kdev
, qmgrs
);
1826 /* get pdsp configuration values from device tree */
1827 pdsps
= of_get_child_by_name(node
, "pdsps");
1829 ret
= knav_queue_init_pdsps(kdev
, pdsps
);
1833 ret
= knav_queue_start_pdsps(kdev
);
1839 /* get usable queue range values from device tree */
1840 queue_pools
= of_get_child_by_name(node
, "queue-pools");
1842 dev_err(dev
, "queue-pools not specified\n");
1846 ret
= knav_setup_queue_pools(kdev
, queue_pools
);
1847 of_node_put(queue_pools
);
1851 ret
= knav_get_link_ram(kdev
, "linkram0", &kdev
->link_rams
[0]);
1853 dev_err(kdev
->dev
, "could not setup linking ram\n");
1857 ret
= knav_get_link_ram(kdev
, "linkram1", &kdev
->link_rams
[1]);
1860 * nothing really, we have one linking ram already, so we just
1861 * live within our means
1865 ret
= knav_queue_setup_link_ram(kdev
);
1869 regions
= of_get_child_by_name(node
, "descriptor-regions");
1871 dev_err(dev
, "descriptor-regions not specified\n");
1874 ret
= knav_queue_setup_regions(kdev
, regions
);
1875 of_node_put(regions
);
1879 ret
= knav_queue_init_queues(kdev
);
1881 dev_err(dev
, "hwqueue initialization failed\n");
1885 debugfs_create_file("qmss", S_IFREG
| S_IRUGO
, NULL
, NULL
,
1886 &knav_queue_debug_ops
);
1887 device_ready
= true;
1891 knav_queue_stop_pdsps(kdev
);
1892 knav_queue_free_regions(kdev
);
1893 knav_free_queue_ranges(kdev
);
1894 pm_runtime_put_sync(&pdev
->dev
);
1895 pm_runtime_disable(&pdev
->dev
);
1899 static int knav_queue_remove(struct platform_device
*pdev
)
1901 /* TODO: Free resources */
1902 pm_runtime_put_sync(&pdev
->dev
);
1903 pm_runtime_disable(&pdev
->dev
);
1907 static struct platform_driver keystone_qmss_driver
= {
1908 .probe
= knav_queue_probe
,
1909 .remove
= knav_queue_remove
,
1911 .name
= "keystone-navigator-qmss",
1912 .of_match_table
= keystone_qmss_of_match
,
1915 module_platform_driver(keystone_qmss_driver
);
1917 MODULE_LICENSE("GPL v2");
1918 MODULE_DESCRIPTION("TI QMSS driver for Keystone SOCs");
1919 MODULE_AUTHOR("Sandeep Nair <sandeep_n@ti.com>");
1920 MODULE_AUTHOR("Santosh Shilimkar <santosh.shilimkar@ti.com>");