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/dma-mapping.h>
21 #include <linux/interrupt.h>
22 #include <linux/module.h>
23 #include <linux/of_address.h>
24 #include <linux/soc/ti/knav_qmss.h>
26 #include "knav_qmss.h"
28 #define knav_range_offset_to_inst(kdev, range, q) \
29 (range->queue_base_inst + (q << kdev->inst_shift))
31 static void __knav_acc_notify(struct knav_range_info
*range
,
32 struct knav_acc_channel
*acc
)
34 struct knav_device
*kdev
= range
->kdev
;
35 struct knav_queue_inst
*inst
;
36 int range_base
, queue
;
38 range_base
= kdev
->base_id
+ range
->queue_base
;
40 if (range
->flags
& RANGE_MULTI_QUEUE
) {
41 for (queue
= 0; queue
< range
->num_queues
; queue
++) {
42 inst
= knav_range_offset_to_inst(kdev
, range
,
44 if (inst
->notify_needed
) {
45 inst
->notify_needed
= 0;
46 dev_dbg(kdev
->dev
, "acc-irq: notifying %d\n",
48 knav_queue_notify(inst
);
52 queue
= acc
->channel
- range
->acc_info
.start_channel
;
53 inst
= knav_range_offset_to_inst(kdev
, range
, queue
);
54 dev_dbg(kdev
->dev
, "acc-irq: notifying %d\n",
56 knav_queue_notify(inst
);
60 static int knav_acc_set_notify(struct knav_range_info
*range
,
61 struct knav_queue_inst
*kq
,
64 struct knav_pdsp_info
*pdsp
= range
->acc_info
.pdsp
;
65 struct knav_device
*kdev
= range
->kdev
;
69 * when enabling, we need to re-trigger an interrupt if we
70 * have descriptors pending
72 if (!enabled
|| atomic_read(&kq
->desc_count
) <= 0)
75 kq
->notify_needed
= 1;
76 atomic_inc(&kq
->acc
->retrigger_count
);
77 mask
= BIT(kq
->acc
->channel
% 32);
78 offset
= ACC_INTD_OFFSET_STATUS(kq
->acc
->channel
);
79 dev_dbg(kdev
->dev
, "setup-notify: re-triggering irq for %s\n",
81 writel_relaxed(mask
, pdsp
->intd
+ offset
);
85 static irqreturn_t
knav_acc_int_handler(int irq
, void *_instdata
)
87 struct knav_acc_channel
*acc
;
88 struct knav_queue_inst
*kq
= NULL
;
89 struct knav_range_info
*range
;
90 struct knav_pdsp_info
*pdsp
;
91 struct knav_acc_info
*info
;
92 struct knav_device
*kdev
;
94 u32
*list
, *list_cpu
, val
, idx
, notifies
;
95 int range_base
, channel
, queue
= 0;
99 info
= &range
->acc_info
;
101 pdsp
= range
->acc_info
.pdsp
;
104 range_base
= kdev
->base_id
+ range
->queue_base
;
105 if ((range
->flags
& RANGE_MULTI_QUEUE
) == 0) {
106 for (queue
= 0; queue
< range
->num_irqs
; queue
++)
107 if (range
->irqs
[queue
].irq
== irq
)
109 kq
= knav_range_offset_to_inst(kdev
, range
, queue
);
113 channel
= acc
->channel
;
114 list_dma
= acc
->list_dma
[acc
->list_index
];
115 list_cpu
= acc
->list_cpu
[acc
->list_index
];
116 dev_dbg(kdev
->dev
, "acc-irq: channel %d, list %d, virt %p, dma %pad\n",
117 channel
, acc
->list_index
, list_cpu
, &list_dma
);
118 if (atomic_read(&acc
->retrigger_count
)) {
119 atomic_dec(&acc
->retrigger_count
);
120 __knav_acc_notify(range
, acc
);
121 writel_relaxed(1, pdsp
->intd
+ ACC_INTD_OFFSET_COUNT(channel
));
122 /* ack the interrupt */
123 writel_relaxed(ACC_CHANNEL_INT_BASE
+ channel
,
124 pdsp
->intd
+ ACC_INTD_OFFSET_EOI
);
129 notifies
= readl_relaxed(pdsp
->intd
+ ACC_INTD_OFFSET_COUNT(channel
));
131 dma_sync_single_for_cpu(kdev
->dev
, list_dma
, info
->list_size
,
134 for (list
= list_cpu
; list
< list_cpu
+ (info
->list_size
/ sizeof(u32
));
135 list
+= ACC_LIST_ENTRY_WORDS
) {
136 if (ACC_LIST_ENTRY_WORDS
== 1) {
138 "acc-irq: list %d, entry @%p, %08x\n",
139 acc
->list_index
, list
, list
[0]);
140 } else if (ACC_LIST_ENTRY_WORDS
== 2) {
142 "acc-irq: list %d, entry @%p, %08x %08x\n",
143 acc
->list_index
, list
, list
[0], list
[1]);
144 } else if (ACC_LIST_ENTRY_WORDS
== 4) {
146 "acc-irq: list %d, entry @%p, %08x %08x %08x %08x\n",
147 acc
->list_index
, list
, list
[0], list
[1],
151 val
= list
[ACC_LIST_ENTRY_DESC_IDX
];
155 if (range
->flags
& RANGE_MULTI_QUEUE
) {
156 queue
= list
[ACC_LIST_ENTRY_QUEUE_IDX
] >> 16;
157 if (queue
< range_base
||
158 queue
>= range_base
+ range
->num_queues
) {
160 "bad queue %d, expecting %d-%d\n",
162 range_base
+ range
->num_queues
);
166 kq
= knav_range_offset_to_inst(kdev
, range
,
170 if (atomic_inc_return(&kq
->desc_count
) >= ACC_DESCS_MAX
) {
171 atomic_dec(&kq
->desc_count
);
173 "acc-irq: queue %d full, entry dropped\n",
178 idx
= atomic_inc_return(&kq
->desc_tail
) & ACC_DESCS_MASK
;
179 kq
->descs
[idx
] = val
;
180 kq
->notify_needed
= 1;
181 dev_dbg(kdev
->dev
, "acc-irq: enqueue %08x at %d, queue %d\n",
182 val
, idx
, queue
+ range_base
);
185 __knav_acc_notify(range
, acc
);
186 memset(list_cpu
, 0, info
->list_size
);
187 dma_sync_single_for_device(kdev
->dev
, list_dma
, info
->list_size
,
190 /* flip to the other list */
191 acc
->list_index
^= 1;
193 /* reset the interrupt counter */
194 writel_relaxed(1, pdsp
->intd
+ ACC_INTD_OFFSET_COUNT(channel
));
196 /* ack the interrupt */
197 writel_relaxed(ACC_CHANNEL_INT_BASE
+ channel
,
198 pdsp
->intd
+ ACC_INTD_OFFSET_EOI
);
203 static int knav_range_setup_acc_irq(struct knav_range_info
*range
,
204 int queue
, bool enabled
)
206 struct knav_device
*kdev
= range
->kdev
;
207 struct knav_acc_channel
*acc
;
208 unsigned long cpu_map
;
212 if (range
->flags
& RANGE_MULTI_QUEUE
) {
214 irq
= range
->irqs
[0].irq
;
215 cpu_map
= range
->irqs
[0].cpu_map
;
217 acc
= range
->acc
+ queue
;
218 irq
= range
->irqs
[queue
].irq
;
219 cpu_map
= range
->irqs
[queue
].cpu_map
;
222 old
= acc
->open_mask
;
224 new = old
| BIT(queue
);
226 new = old
& ~BIT(queue
);
227 acc
->open_mask
= new;
230 "setup-acc-irq: open mask old %08x, new %08x, channel %s\n",
231 old
, new, acc
->name
);
233 if (likely(new == old
))
238 "setup-acc-irq: requesting %s for channel %s\n",
239 acc
->name
, acc
->name
);
240 ret
= request_irq(irq
, knav_acc_int_handler
, 0, acc
->name
,
242 if (!ret
&& cpu_map
) {
243 ret
= irq_set_affinity_hint(irq
, to_cpumask(&cpu_map
));
245 dev_warn(range
->kdev
->dev
,
246 "Failed to set IRQ affinity\n");
253 dev_dbg(kdev
->dev
, "setup-acc-irq: freeing %s for channel %s\n",
254 acc
->name
, acc
->name
);
255 ret
= irq_set_affinity_hint(irq
, NULL
);
257 dev_warn(range
->kdev
->dev
,
258 "Failed to set IRQ affinity\n");
259 free_irq(irq
, range
);
265 static const char *knav_acc_result_str(enum knav_acc_result result
)
267 static const char * const result_str
[] = {
268 [ACC_RET_IDLE
] = "idle",
269 [ACC_RET_SUCCESS
] = "success",
270 [ACC_RET_INVALID_COMMAND
] = "invalid command",
271 [ACC_RET_INVALID_CHANNEL
] = "invalid channel",
272 [ACC_RET_INACTIVE_CHANNEL
] = "inactive channel",
273 [ACC_RET_ACTIVE_CHANNEL
] = "active channel",
274 [ACC_RET_INVALID_QUEUE
] = "invalid queue",
275 [ACC_RET_INVALID_RET
] = "invalid return code",
278 if (result
>= ARRAY_SIZE(result_str
))
279 return result_str
[ACC_RET_INVALID_RET
];
281 return result_str
[result
];
284 static enum knav_acc_result
285 knav_acc_write(struct knav_device
*kdev
, struct knav_pdsp_info
*pdsp
,
286 struct knav_reg_acc_command
*cmd
)
290 dev_dbg(kdev
->dev
, "acc command %08x %08x %08x %08x %08x\n",
291 cmd
->command
, cmd
->queue_mask
, cmd
->list_dma
,
292 cmd
->queue_num
, cmd
->timer_config
);
294 writel_relaxed(cmd
->timer_config
, &pdsp
->acc_command
->timer_config
);
295 writel_relaxed(cmd
->queue_num
, &pdsp
->acc_command
->queue_num
);
296 writel_relaxed(cmd
->list_dma
, &pdsp
->acc_command
->list_dma
);
297 writel_relaxed(cmd
->queue_mask
, &pdsp
->acc_command
->queue_mask
);
298 writel_relaxed(cmd
->command
, &pdsp
->acc_command
->command
);
300 /* wait for the command to clear */
302 result
= readl_relaxed(&pdsp
->acc_command
->command
);
303 } while ((result
>> 8) & 0xff);
305 return (result
>> 24) & 0xff;
308 static void knav_acc_setup_cmd(struct knav_device
*kdev
,
309 struct knav_range_info
*range
,
310 struct knav_reg_acc_command
*cmd
,
313 struct knav_acc_info
*info
= &range
->acc_info
;
314 struct knav_acc_channel
*acc
;
318 if (range
->flags
& RANGE_MULTI_QUEUE
) {
320 queue_base
= range
->queue_base
;
321 queue_mask
= BIT(range
->num_queues
) - 1;
323 acc
= range
->acc
+ queue
;
324 queue_base
= range
->queue_base
+ queue
;
328 memset(cmd
, 0, sizeof(*cmd
));
329 cmd
->command
= acc
->channel
;
330 cmd
->queue_mask
= queue_mask
;
331 cmd
->list_dma
= (u32
)acc
->list_dma
[0];
332 cmd
->queue_num
= info
->list_entries
<< 16;
333 cmd
->queue_num
|= queue_base
;
335 cmd
->timer_config
= ACC_LIST_ENTRY_TYPE
<< 18;
336 if (range
->flags
& RANGE_MULTI_QUEUE
)
337 cmd
->timer_config
|= ACC_CFG_MULTI_QUEUE
;
338 cmd
->timer_config
|= info
->pacing_mode
<< 16;
339 cmd
->timer_config
|= info
->timer_count
;
342 static void knav_acc_stop(struct knav_device
*kdev
,
343 struct knav_range_info
*range
,
346 struct knav_reg_acc_command cmd
;
347 struct knav_acc_channel
*acc
;
348 enum knav_acc_result result
;
350 acc
= range
->acc
+ queue
;
352 knav_acc_setup_cmd(kdev
, range
, &cmd
, queue
);
353 cmd
.command
|= ACC_CMD_DISABLE_CHANNEL
<< 8;
354 result
= knav_acc_write(kdev
, range
->acc_info
.pdsp
, &cmd
);
356 dev_dbg(kdev
->dev
, "stopped acc channel %s, result %s\n",
357 acc
->name
, knav_acc_result_str(result
));
360 static enum knav_acc_result
knav_acc_start(struct knav_device
*kdev
,
361 struct knav_range_info
*range
,
364 struct knav_reg_acc_command cmd
;
365 struct knav_acc_channel
*acc
;
366 enum knav_acc_result result
;
368 acc
= range
->acc
+ queue
;
370 knav_acc_setup_cmd(kdev
, range
, &cmd
, queue
);
371 cmd
.command
|= ACC_CMD_ENABLE_CHANNEL
<< 8;
372 result
= knav_acc_write(kdev
, range
->acc_info
.pdsp
, &cmd
);
374 dev_dbg(kdev
->dev
, "started acc channel %s, result %s\n",
375 acc
->name
, knav_acc_result_str(result
));
380 static int knav_acc_init_range(struct knav_range_info
*range
)
382 struct knav_device
*kdev
= range
->kdev
;
383 struct knav_acc_channel
*acc
;
384 enum knav_acc_result result
;
387 for (queue
= 0; queue
< range
->num_queues
; queue
++) {
388 acc
= range
->acc
+ queue
;
390 knav_acc_stop(kdev
, range
, queue
);
392 result
= knav_acc_start(kdev
, range
, queue
);
394 if (result
!= ACC_RET_SUCCESS
)
397 if (range
->flags
& RANGE_MULTI_QUEUE
)
403 static int knav_acc_init_queue(struct knav_range_info
*range
,
404 struct knav_queue_inst
*kq
)
406 unsigned id
= kq
->id
- range
->queue_base
;
408 kq
->descs
= devm_kzalloc(range
->kdev
->dev
,
409 ACC_DESCS_MAX
* sizeof(u32
), GFP_KERNEL
);
413 kq
->acc
= range
->acc
;
414 if ((range
->flags
& RANGE_MULTI_QUEUE
) == 0)
419 static int knav_acc_open_queue(struct knav_range_info
*range
,
420 struct knav_queue_inst
*inst
, unsigned flags
)
422 unsigned id
= inst
->id
- range
->queue_base
;
424 return knav_range_setup_acc_irq(range
, id
, true);
427 static int knav_acc_close_queue(struct knav_range_info
*range
,
428 struct knav_queue_inst
*inst
)
430 unsigned id
= inst
->id
- range
->queue_base
;
432 return knav_range_setup_acc_irq(range
, id
, false);
435 static int knav_acc_free_range(struct knav_range_info
*range
)
437 struct knav_device
*kdev
= range
->kdev
;
438 struct knav_acc_channel
*acc
;
439 struct knav_acc_info
*info
;
440 int channel
, channels
;
442 info
= &range
->acc_info
;
444 if (range
->flags
& RANGE_MULTI_QUEUE
)
447 channels
= range
->num_queues
;
449 for (channel
= 0; channel
< channels
; channel
++) {
450 acc
= range
->acc
+ channel
;
451 if (!acc
->list_cpu
[0])
453 dma_unmap_single(kdev
->dev
, acc
->list_dma
[0],
454 info
->mem_size
, DMA_BIDIRECTIONAL
);
455 free_pages_exact(acc
->list_cpu
[0], info
->mem_size
);
457 devm_kfree(range
->kdev
->dev
, range
->acc
);
461 struct knav_range_ops knav_acc_range_ops
= {
462 .set_notify
= knav_acc_set_notify
,
463 .init_queue
= knav_acc_init_queue
,
464 .open_queue
= knav_acc_open_queue
,
465 .close_queue
= knav_acc_close_queue
,
466 .init_range
= knav_acc_init_range
,
467 .free_range
= knav_acc_free_range
,
471 * knav_init_acc_range: Initialise accumulator ranges
475 * @range: qmms range information
477 * Return 0 on success or error
479 int knav_init_acc_range(struct knav_device
*kdev
,
480 struct device_node
*node
,
481 struct knav_range_info
*range
)
483 struct knav_acc_channel
*acc
;
484 struct knav_pdsp_info
*pdsp
;
485 struct knav_acc_info
*info
;
486 int ret
, channel
, channels
;
487 int list_size
, mem_size
;
492 range
->flags
|= RANGE_HAS_ACCUMULATOR
;
493 info
= &range
->acc_info
;
495 ret
= of_property_read_u32_array(node
, "accumulator", config
, 5);
499 info
->pdsp_id
= config
[0];
500 info
->start_channel
= config
[1];
501 info
->list_entries
= config
[2];
502 info
->pacing_mode
= config
[3];
503 info
->timer_count
= config
[4] / ACC_DEFAULT_PERIOD
;
505 if (info
->start_channel
> ACC_MAX_CHANNEL
) {
506 dev_err(kdev
->dev
, "channel %d invalid for range %s\n",
507 info
->start_channel
, range
->name
);
511 if (info
->pacing_mode
> 3) {
512 dev_err(kdev
->dev
, "pacing mode %d invalid for range %s\n",
513 info
->pacing_mode
, range
->name
);
517 pdsp
= knav_find_pdsp(kdev
, info
->pdsp_id
);
519 dev_err(kdev
->dev
, "pdsp id %d not found for range %s\n",
520 info
->pdsp_id
, range
->name
);
524 if (!pdsp
->started
) {
525 dev_err(kdev
->dev
, "pdsp id %d not started for range %s\n",
526 info
->pdsp_id
, range
->name
);
531 channels
= range
->num_queues
;
532 if (of_get_property(node
, "multi-queue", NULL
)) {
533 range
->flags
|= RANGE_MULTI_QUEUE
;
535 if (range
->queue_base
& (32 - 1)) {
537 "misaligned multi-queue accumulator range %s\n",
541 if (range
->num_queues
> 32) {
543 "too many queues in accumulator range %s\n",
549 /* figure out list size */
550 list_size
= info
->list_entries
;
551 list_size
*= ACC_LIST_ENTRY_WORDS
* sizeof(u32
);
552 info
->list_size
= list_size
;
553 mem_size
= PAGE_ALIGN(list_size
* 2);
554 info
->mem_size
= mem_size
;
555 range
->acc
= devm_kzalloc(kdev
->dev
, channels
* sizeof(*range
->acc
),
560 for (channel
= 0; channel
< channels
; channel
++) {
561 acc
= range
->acc
+ channel
;
562 acc
->channel
= info
->start_channel
+ channel
;
564 /* allocate memory for the two lists */
565 list_mem
= alloc_pages_exact(mem_size
, GFP_KERNEL
| GFP_DMA
);
569 list_dma
= dma_map_single(kdev
->dev
, list_mem
, mem_size
,
571 if (dma_mapping_error(kdev
->dev
, list_dma
)) {
572 free_pages_exact(list_mem
, mem_size
);
576 memset(list_mem
, 0, mem_size
);
577 dma_sync_single_for_device(kdev
->dev
, list_dma
, mem_size
,
579 scnprintf(acc
->name
, sizeof(acc
->name
), "hwqueue-acc-%d",
581 acc
->list_cpu
[0] = list_mem
;
582 acc
->list_cpu
[1] = list_mem
+ list_size
;
583 acc
->list_dma
[0] = list_dma
;
584 acc
->list_dma
[1] = list_dma
+ list_size
;
585 dev_dbg(kdev
->dev
, "%s: channel %d, dma %pad, virt %8p\n",
586 acc
->name
, acc
->channel
, &list_dma
, list_mem
);
589 range
->ops
= &knav_acc_range_ops
;
592 EXPORT_SYMBOL_GPL(knav_init_acc_range
);