2 * Keystone accumulator queue manager
4 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com
5 * Author: 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/kernel.h>
20 #include <linux/module.h>
21 #include <linux/device.h>
23 #include <linux/interrupt.h>
24 #include <linux/bitops.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock.h>
27 #include <linux/soc/ti/knav_qmss.h>
28 #include <linux/platform_device.h>
29 #include <linux/dma-mapping.h>
31 #include <linux/of_device.h>
32 #include <linux/of_address.h>
33 #include <linux/firmware.h>
35 #include "knav_qmss.h"
37 #define knav_range_offset_to_inst(kdev, range, q) \
38 (range->queue_base_inst + (q << kdev->inst_shift))
40 static void __knav_acc_notify(struct knav_range_info
*range
,
41 struct knav_acc_channel
*acc
)
43 struct knav_device
*kdev
= range
->kdev
;
44 struct knav_queue_inst
*inst
;
45 int range_base
, queue
;
47 range_base
= kdev
->base_id
+ range
->queue_base
;
49 if (range
->flags
& RANGE_MULTI_QUEUE
) {
50 for (queue
= 0; queue
< range
->num_queues
; queue
++) {
51 inst
= knav_range_offset_to_inst(kdev
, range
,
53 if (inst
->notify_needed
) {
54 inst
->notify_needed
= 0;
55 dev_dbg(kdev
->dev
, "acc-irq: notifying %d\n",
57 knav_queue_notify(inst
);
61 queue
= acc
->channel
- range
->acc_info
.start_channel
;
62 inst
= knav_range_offset_to_inst(kdev
, range
, queue
);
63 dev_dbg(kdev
->dev
, "acc-irq: notifying %d\n",
65 knav_queue_notify(inst
);
69 static int knav_acc_set_notify(struct knav_range_info
*range
,
70 struct knav_queue_inst
*kq
,
73 struct knav_pdsp_info
*pdsp
= range
->acc_info
.pdsp
;
74 struct knav_device
*kdev
= range
->kdev
;
78 * when enabling, we need to re-trigger an interrupt if we
79 * have descriptors pending
81 if (!enabled
|| atomic_read(&kq
->desc_count
) <= 0)
84 kq
->notify_needed
= 1;
85 atomic_inc(&kq
->acc
->retrigger_count
);
86 mask
= BIT(kq
->acc
->channel
% 32);
87 offset
= ACC_INTD_OFFSET_STATUS(kq
->acc
->channel
);
88 dev_dbg(kdev
->dev
, "setup-notify: re-triggering irq for %s\n",
90 writel_relaxed(mask
, pdsp
->intd
+ offset
);
94 static irqreturn_t
knav_acc_int_handler(int irq
, void *_instdata
)
96 struct knav_acc_channel
*acc
;
97 struct knav_queue_inst
*kq
= NULL
;
98 struct knav_range_info
*range
;
99 struct knav_pdsp_info
*pdsp
;
100 struct knav_acc_info
*info
;
101 struct knav_device
*kdev
;
103 u32
*list
, *list_cpu
, val
, idx
, notifies
;
104 int range_base
, channel
, queue
= 0;
108 info
= &range
->acc_info
;
110 pdsp
= range
->acc_info
.pdsp
;
113 range_base
= kdev
->base_id
+ range
->queue_base
;
114 if ((range
->flags
& RANGE_MULTI_QUEUE
) == 0) {
115 for (queue
= 0; queue
< range
->num_irqs
; queue
++)
116 if (range
->irqs
[queue
].irq
== irq
)
118 kq
= knav_range_offset_to_inst(kdev
, range
, queue
);
122 channel
= acc
->channel
;
123 list_dma
= acc
->list_dma
[acc
->list_index
];
124 list_cpu
= acc
->list_cpu
[acc
->list_index
];
125 dev_dbg(kdev
->dev
, "acc-irq: channel %d, list %d, virt %p, dma %pad\n",
126 channel
, acc
->list_index
, list_cpu
, &list_dma
);
127 if (atomic_read(&acc
->retrigger_count
)) {
128 atomic_dec(&acc
->retrigger_count
);
129 __knav_acc_notify(range
, acc
);
130 writel_relaxed(1, pdsp
->intd
+ ACC_INTD_OFFSET_COUNT(channel
));
131 /* ack the interrupt */
132 writel_relaxed(ACC_CHANNEL_INT_BASE
+ channel
,
133 pdsp
->intd
+ ACC_INTD_OFFSET_EOI
);
138 notifies
= readl_relaxed(pdsp
->intd
+ ACC_INTD_OFFSET_COUNT(channel
));
140 dma_sync_single_for_cpu(kdev
->dev
, list_dma
, info
->list_size
,
143 for (list
= list_cpu
; list
< list_cpu
+ (info
->list_size
/ sizeof(u32
));
144 list
+= ACC_LIST_ENTRY_WORDS
) {
145 if (ACC_LIST_ENTRY_WORDS
== 1) {
147 "acc-irq: list %d, entry @%p, %08x\n",
148 acc
->list_index
, list
, list
[0]);
149 } else if (ACC_LIST_ENTRY_WORDS
== 2) {
151 "acc-irq: list %d, entry @%p, %08x %08x\n",
152 acc
->list_index
, list
, list
[0], list
[1]);
153 } else if (ACC_LIST_ENTRY_WORDS
== 4) {
155 "acc-irq: list %d, entry @%p, %08x %08x %08x %08x\n",
156 acc
->list_index
, list
, list
[0], list
[1],
160 val
= list
[ACC_LIST_ENTRY_DESC_IDX
];
164 if (range
->flags
& RANGE_MULTI_QUEUE
) {
165 queue
= list
[ACC_LIST_ENTRY_QUEUE_IDX
] >> 16;
166 if (queue
< range_base
||
167 queue
>= range_base
+ range
->num_queues
) {
169 "bad queue %d, expecting %d-%d\n",
171 range_base
+ range
->num_queues
);
175 kq
= knav_range_offset_to_inst(kdev
, range
,
179 if (atomic_inc_return(&kq
->desc_count
) >= ACC_DESCS_MAX
) {
180 atomic_dec(&kq
->desc_count
);
182 "acc-irq: queue %d full, entry dropped\n",
187 idx
= atomic_inc_return(&kq
->desc_tail
) & ACC_DESCS_MASK
;
188 kq
->descs
[idx
] = val
;
189 kq
->notify_needed
= 1;
190 dev_dbg(kdev
->dev
, "acc-irq: enqueue %08x at %d, queue %d\n",
191 val
, idx
, queue
+ range_base
);
194 __knav_acc_notify(range
, acc
);
195 memset(list_cpu
, 0, info
->list_size
);
196 dma_sync_single_for_device(kdev
->dev
, list_dma
, info
->list_size
,
199 /* flip to the other list */
200 acc
->list_index
^= 1;
202 /* reset the interrupt counter */
203 writel_relaxed(1, pdsp
->intd
+ ACC_INTD_OFFSET_COUNT(channel
));
205 /* ack the interrupt */
206 writel_relaxed(ACC_CHANNEL_INT_BASE
+ channel
,
207 pdsp
->intd
+ ACC_INTD_OFFSET_EOI
);
212 static int knav_range_setup_acc_irq(struct knav_range_info
*range
,
213 int queue
, bool enabled
)
215 struct knav_device
*kdev
= range
->kdev
;
216 struct knav_acc_channel
*acc
;
217 unsigned long cpu_map
;
221 if (range
->flags
& RANGE_MULTI_QUEUE
) {
223 irq
= range
->irqs
[0].irq
;
224 cpu_map
= range
->irqs
[0].cpu_map
;
226 acc
= range
->acc
+ queue
;
227 irq
= range
->irqs
[queue
].irq
;
228 cpu_map
= range
->irqs
[queue
].cpu_map
;
231 old
= acc
->open_mask
;
233 new = old
| BIT(queue
);
235 new = old
& ~BIT(queue
);
236 acc
->open_mask
= new;
239 "setup-acc-irq: open mask old %08x, new %08x, channel %s\n",
240 old
, new, acc
->name
);
242 if (likely(new == old
))
247 "setup-acc-irq: requesting %s for channel %s\n",
248 acc
->name
, acc
->name
);
249 ret
= request_irq(irq
, knav_acc_int_handler
, 0, acc
->name
,
251 if (!ret
&& cpu_map
) {
252 ret
= irq_set_affinity_hint(irq
, to_cpumask(&cpu_map
));
254 dev_warn(range
->kdev
->dev
,
255 "Failed to set IRQ affinity\n");
262 dev_dbg(kdev
->dev
, "setup-acc-irq: freeing %s for channel %s\n",
263 acc
->name
, acc
->name
);
264 ret
= irq_set_affinity_hint(irq
, NULL
);
266 dev_warn(range
->kdev
->dev
,
267 "Failed to set IRQ affinity\n");
268 free_irq(irq
, range
);
274 static const char *knav_acc_result_str(enum knav_acc_result result
)
276 static const char * const result_str
[] = {
277 [ACC_RET_IDLE
] = "idle",
278 [ACC_RET_SUCCESS
] = "success",
279 [ACC_RET_INVALID_COMMAND
] = "invalid command",
280 [ACC_RET_INVALID_CHANNEL
] = "invalid channel",
281 [ACC_RET_INACTIVE_CHANNEL
] = "inactive channel",
282 [ACC_RET_ACTIVE_CHANNEL
] = "active channel",
283 [ACC_RET_INVALID_QUEUE
] = "invalid queue",
284 [ACC_RET_INVALID_RET
] = "invalid return code",
287 if (result
>= ARRAY_SIZE(result_str
))
288 return result_str
[ACC_RET_INVALID_RET
];
290 return result_str
[result
];
293 static enum knav_acc_result
294 knav_acc_write(struct knav_device
*kdev
, struct knav_pdsp_info
*pdsp
,
295 struct knav_reg_acc_command
*cmd
)
299 dev_dbg(kdev
->dev
, "acc command %08x %08x %08x %08x %08x\n",
300 cmd
->command
, cmd
->queue_mask
, cmd
->list_dma
,
301 cmd
->queue_num
, cmd
->timer_config
);
303 writel_relaxed(cmd
->timer_config
, &pdsp
->acc_command
->timer_config
);
304 writel_relaxed(cmd
->queue_num
, &pdsp
->acc_command
->queue_num
);
305 writel_relaxed(cmd
->list_dma
, &pdsp
->acc_command
->list_dma
);
306 writel_relaxed(cmd
->queue_mask
, &pdsp
->acc_command
->queue_mask
);
307 writel_relaxed(cmd
->command
, &pdsp
->acc_command
->command
);
309 /* wait for the command to clear */
311 result
= readl_relaxed(&pdsp
->acc_command
->command
);
312 } while ((result
>> 8) & 0xff);
314 return (result
>> 24) & 0xff;
317 static void knav_acc_setup_cmd(struct knav_device
*kdev
,
318 struct knav_range_info
*range
,
319 struct knav_reg_acc_command
*cmd
,
322 struct knav_acc_info
*info
= &range
->acc_info
;
323 struct knav_acc_channel
*acc
;
327 if (range
->flags
& RANGE_MULTI_QUEUE
) {
329 queue_base
= range
->queue_base
;
330 queue_mask
= BIT(range
->num_queues
) - 1;
332 acc
= range
->acc
+ queue
;
333 queue_base
= range
->queue_base
+ queue
;
337 memset(cmd
, 0, sizeof(*cmd
));
338 cmd
->command
= acc
->channel
;
339 cmd
->queue_mask
= queue_mask
;
340 cmd
->list_dma
= (u32
)acc
->list_dma
[0];
341 cmd
->queue_num
= info
->list_entries
<< 16;
342 cmd
->queue_num
|= queue_base
;
344 cmd
->timer_config
= ACC_LIST_ENTRY_TYPE
<< 18;
345 if (range
->flags
& RANGE_MULTI_QUEUE
)
346 cmd
->timer_config
|= ACC_CFG_MULTI_QUEUE
;
347 cmd
->timer_config
|= info
->pacing_mode
<< 16;
348 cmd
->timer_config
|= info
->timer_count
;
351 static void knav_acc_stop(struct knav_device
*kdev
,
352 struct knav_range_info
*range
,
355 struct knav_reg_acc_command cmd
;
356 struct knav_acc_channel
*acc
;
357 enum knav_acc_result result
;
359 acc
= range
->acc
+ queue
;
361 knav_acc_setup_cmd(kdev
, range
, &cmd
, queue
);
362 cmd
.command
|= ACC_CMD_DISABLE_CHANNEL
<< 8;
363 result
= knav_acc_write(kdev
, range
->acc_info
.pdsp
, &cmd
);
365 dev_dbg(kdev
->dev
, "stopped acc channel %s, result %s\n",
366 acc
->name
, knav_acc_result_str(result
));
369 static enum knav_acc_result
knav_acc_start(struct knav_device
*kdev
,
370 struct knav_range_info
*range
,
373 struct knav_reg_acc_command cmd
;
374 struct knav_acc_channel
*acc
;
375 enum knav_acc_result result
;
377 acc
= range
->acc
+ queue
;
379 knav_acc_setup_cmd(kdev
, range
, &cmd
, queue
);
380 cmd
.command
|= ACC_CMD_ENABLE_CHANNEL
<< 8;
381 result
= knav_acc_write(kdev
, range
->acc_info
.pdsp
, &cmd
);
383 dev_dbg(kdev
->dev
, "started acc channel %s, result %s\n",
384 acc
->name
, knav_acc_result_str(result
));
389 static int knav_acc_init_range(struct knav_range_info
*range
)
391 struct knav_device
*kdev
= range
->kdev
;
392 struct knav_acc_channel
*acc
;
393 enum knav_acc_result result
;
396 for (queue
= 0; queue
< range
->num_queues
; queue
++) {
397 acc
= range
->acc
+ queue
;
399 knav_acc_stop(kdev
, range
, queue
);
401 result
= knav_acc_start(kdev
, range
, queue
);
403 if (result
!= ACC_RET_SUCCESS
)
406 if (range
->flags
& RANGE_MULTI_QUEUE
)
412 static int knav_acc_init_queue(struct knav_range_info
*range
,
413 struct knav_queue_inst
*kq
)
415 unsigned id
= kq
->id
- range
->queue_base
;
417 kq
->descs
= devm_kzalloc(range
->kdev
->dev
,
418 ACC_DESCS_MAX
* sizeof(u32
), GFP_KERNEL
);
422 kq
->acc
= range
->acc
;
423 if ((range
->flags
& RANGE_MULTI_QUEUE
) == 0)
428 static int knav_acc_open_queue(struct knav_range_info
*range
,
429 struct knav_queue_inst
*inst
, unsigned flags
)
431 unsigned id
= inst
->id
- range
->queue_base
;
433 return knav_range_setup_acc_irq(range
, id
, true);
436 static int knav_acc_close_queue(struct knav_range_info
*range
,
437 struct knav_queue_inst
*inst
)
439 unsigned id
= inst
->id
- range
->queue_base
;
441 return knav_range_setup_acc_irq(range
, id
, false);
444 static int knav_acc_free_range(struct knav_range_info
*range
)
446 struct knav_device
*kdev
= range
->kdev
;
447 struct knav_acc_channel
*acc
;
448 struct knav_acc_info
*info
;
449 int channel
, channels
;
451 info
= &range
->acc_info
;
453 if (range
->flags
& RANGE_MULTI_QUEUE
)
456 channels
= range
->num_queues
;
458 for (channel
= 0; channel
< channels
; channel
++) {
459 acc
= range
->acc
+ channel
;
460 if (!acc
->list_cpu
[0])
462 dma_unmap_single(kdev
->dev
, acc
->list_dma
[0],
463 info
->mem_size
, DMA_BIDIRECTIONAL
);
464 free_pages_exact(acc
->list_cpu
[0], info
->mem_size
);
466 devm_kfree(range
->kdev
->dev
, range
->acc
);
470 struct knav_range_ops knav_acc_range_ops
= {
471 .set_notify
= knav_acc_set_notify
,
472 .init_queue
= knav_acc_init_queue
,
473 .open_queue
= knav_acc_open_queue
,
474 .close_queue
= knav_acc_close_queue
,
475 .init_range
= knav_acc_init_range
,
476 .free_range
= knav_acc_free_range
,
480 * knav_init_acc_range: Initialise accumulator ranges
484 * @range: qmms range information
486 * Return 0 on success or error
488 int knav_init_acc_range(struct knav_device
*kdev
,
489 struct device_node
*node
,
490 struct knav_range_info
*range
)
492 struct knav_acc_channel
*acc
;
493 struct knav_pdsp_info
*pdsp
;
494 struct knav_acc_info
*info
;
495 int ret
, channel
, channels
;
496 int list_size
, mem_size
;
501 range
->flags
|= RANGE_HAS_ACCUMULATOR
;
502 info
= &range
->acc_info
;
504 ret
= of_property_read_u32_array(node
, "accumulator", config
, 5);
508 info
->pdsp_id
= config
[0];
509 info
->start_channel
= config
[1];
510 info
->list_entries
= config
[2];
511 info
->pacing_mode
= config
[3];
512 info
->timer_count
= config
[4] / ACC_DEFAULT_PERIOD
;
514 if (info
->start_channel
> ACC_MAX_CHANNEL
) {
515 dev_err(kdev
->dev
, "channel %d invalid for range %s\n",
516 info
->start_channel
, range
->name
);
520 if (info
->pacing_mode
> 3) {
521 dev_err(kdev
->dev
, "pacing mode %d invalid for range %s\n",
522 info
->pacing_mode
, range
->name
);
526 pdsp
= knav_find_pdsp(kdev
, info
->pdsp_id
);
528 dev_err(kdev
->dev
, "pdsp id %d not found for range %s\n",
529 info
->pdsp_id
, range
->name
);
533 if (!pdsp
->started
) {
534 dev_err(kdev
->dev
, "pdsp id %d not started for range %s\n",
535 info
->pdsp_id
, range
->name
);
540 channels
= range
->num_queues
;
541 if (of_get_property(node
, "multi-queue", NULL
)) {
542 range
->flags
|= RANGE_MULTI_QUEUE
;
544 if (range
->queue_base
& (32 - 1)) {
546 "misaligned multi-queue accumulator range %s\n",
550 if (range
->num_queues
> 32) {
552 "too many queues in accumulator range %s\n",
558 /* figure out list size */
559 list_size
= info
->list_entries
;
560 list_size
*= ACC_LIST_ENTRY_WORDS
* sizeof(u32
);
561 info
->list_size
= list_size
;
562 mem_size
= PAGE_ALIGN(list_size
* 2);
563 info
->mem_size
= mem_size
;
564 range
->acc
= devm_kzalloc(kdev
->dev
, channels
* sizeof(*range
->acc
),
569 for (channel
= 0; channel
< channels
; channel
++) {
570 acc
= range
->acc
+ channel
;
571 acc
->channel
= info
->start_channel
+ channel
;
573 /* allocate memory for the two lists */
574 list_mem
= alloc_pages_exact(mem_size
, GFP_KERNEL
| GFP_DMA
);
578 list_dma
= dma_map_single(kdev
->dev
, list_mem
, mem_size
,
580 if (dma_mapping_error(kdev
->dev
, list_dma
)) {
581 free_pages_exact(list_mem
, mem_size
);
585 memset(list_mem
, 0, mem_size
);
586 dma_sync_single_for_device(kdev
->dev
, list_dma
, mem_size
,
588 scnprintf(acc
->name
, sizeof(acc
->name
), "hwqueue-acc-%d",
590 acc
->list_cpu
[0] = list_mem
;
591 acc
->list_cpu
[1] = list_mem
+ list_size
;
592 acc
->list_dma
[0] = list_dma
;
593 acc
->list_dma
[1] = list_dma
+ list_size
;
594 dev_dbg(kdev
->dev
, "%s: channel %d, dma %pad, virt %8p\n",
595 acc
->name
, acc
->channel
, &list_dma
, list_mem
);
598 range
->ops
= &knav_acc_range_ops
;
601 EXPORT_SYMBOL_GPL(knav_init_acc_range
);