]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - sound/soc/intel/sst-firmware.c
Merge remote-tracking branch 'asoc/fix/intel' into asoc-linus
[mirror_ubuntu-zesty-kernel.git] / sound / soc / intel / sst-firmware.c
1 /*
2 * Intel SST Firmware Loader
3 *
4 * Copyright (C) 2013, Intel Corporation. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/sched.h>
20 #include <linux/firmware.h>
21 #include <linux/export.h>
22 #include <linux/platform_device.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/dmaengine.h>
25 #include <linux/pci.h>
26 #include <linux/acpi.h>
27
28 /* supported DMA engine drivers */
29 #include <linux/platform_data/dma-dw.h>
30 #include <linux/dma/dw.h>
31
32 #include <asm/page.h>
33 #include <asm/pgtable.h>
34
35 #include "sst-dsp.h"
36 #include "sst-dsp-priv.h"
37
38 #define SST_DMA_RESOURCES 2
39 #define SST_DSP_DMA_MAX_BURST 0x3
40 #define SST_HSW_BLOCK_ANY 0xffffffff
41
42 #define SST_HSW_MASK_DMA_ADDR_DSP 0xfff00000
43
44 struct sst_dma {
45 struct sst_dsp *sst;
46
47 struct dw_dma_chip *chip;
48
49 struct dma_async_tx_descriptor *desc;
50 struct dma_chan *ch;
51 };
52
53 static inline void sst_memcpy32(volatile void __iomem *dest, void *src, u32 bytes)
54 {
55 /* __iowrite32_copy use 32bit size values so divide by 4 */
56 __iowrite32_copy((void *)dest, src, bytes/4);
57 }
58
59 static void sst_dma_transfer_complete(void *arg)
60 {
61 struct sst_dsp *sst = (struct sst_dsp *)arg;
62
63 dev_dbg(sst->dev, "DMA: callback\n");
64 }
65
66 static int sst_dsp_dma_copy(struct sst_dsp *sst, dma_addr_t dest_addr,
67 dma_addr_t src_addr, size_t size)
68 {
69 struct dma_async_tx_descriptor *desc;
70 struct sst_dma *dma = sst->dma;
71
72 if (dma->ch == NULL) {
73 dev_err(sst->dev, "error: no DMA channel\n");
74 return -ENODEV;
75 }
76
77 dev_dbg(sst->dev, "DMA: src: 0x%lx dest 0x%lx size %zu\n",
78 (unsigned long)src_addr, (unsigned long)dest_addr, size);
79
80 desc = dma->ch->device->device_prep_dma_memcpy(dma->ch, dest_addr,
81 src_addr, size, DMA_CTRL_ACK);
82 if (!desc){
83 dev_err(sst->dev, "error: dma prep memcpy failed\n");
84 return -EINVAL;
85 }
86
87 desc->callback = sst_dma_transfer_complete;
88 desc->callback_param = sst;
89
90 desc->tx_submit(desc);
91 dma_wait_for_async_tx(desc);
92
93 return 0;
94 }
95
96 /* copy to DSP */
97 int sst_dsp_dma_copyto(struct sst_dsp *sst, dma_addr_t dest_addr,
98 dma_addr_t src_addr, size_t size)
99 {
100 return sst_dsp_dma_copy(sst, dest_addr | SST_HSW_MASK_DMA_ADDR_DSP,
101 src_addr, size);
102 }
103 EXPORT_SYMBOL_GPL(sst_dsp_dma_copyto);
104
105 /* copy from DSP */
106 int sst_dsp_dma_copyfrom(struct sst_dsp *sst, dma_addr_t dest_addr,
107 dma_addr_t src_addr, size_t size)
108 {
109 return sst_dsp_dma_copy(sst, dest_addr,
110 src_addr | SST_HSW_MASK_DMA_ADDR_DSP, size);
111 }
112 EXPORT_SYMBOL_GPL(sst_dsp_dma_copyfrom);
113
114 /* remove module from memory - callers hold locks */
115 static void block_list_remove(struct sst_dsp *dsp,
116 struct list_head *block_list)
117 {
118 struct sst_mem_block *block, *tmp;
119 int err;
120
121 /* disable each block */
122 list_for_each_entry(block, block_list, module_list) {
123
124 if (block->ops && block->ops->disable) {
125 err = block->ops->disable(block);
126 if (err < 0)
127 dev_err(dsp->dev,
128 "error: cant disable block %d:%d\n",
129 block->type, block->index);
130 }
131 }
132
133 /* mark each block as free */
134 list_for_each_entry_safe(block, tmp, block_list, module_list) {
135 list_del(&block->module_list);
136 list_move(&block->list, &dsp->free_block_list);
137 dev_dbg(dsp->dev, "block freed %d:%d at offset 0x%x\n",
138 block->type, block->index, block->offset);
139 }
140 }
141
142 /* prepare the memory block to receive data from host - callers hold locks */
143 static int block_list_prepare(struct sst_dsp *dsp,
144 struct list_head *block_list)
145 {
146 struct sst_mem_block *block;
147 int ret = 0;
148
149 /* enable each block so that's it'e ready for data */
150 list_for_each_entry(block, block_list, module_list) {
151
152 if (block->ops && block->ops->enable && !block->users) {
153 ret = block->ops->enable(block);
154 if (ret < 0) {
155 dev_err(dsp->dev,
156 "error: cant disable block %d:%d\n",
157 block->type, block->index);
158 goto err;
159 }
160 }
161 }
162 return ret;
163
164 err:
165 list_for_each_entry(block, block_list, module_list) {
166 if (block->ops && block->ops->disable)
167 block->ops->disable(block);
168 }
169 return ret;
170 }
171
172 static struct dw_dma_platform_data dw_pdata = {
173 .is_private = 1,
174 .chan_allocation_order = CHAN_ALLOCATION_ASCENDING,
175 .chan_priority = CHAN_PRIORITY_ASCENDING,
176 };
177
178 static struct dw_dma_chip *dw_probe(struct device *dev, struct resource *mem,
179 int irq)
180 {
181 struct dw_dma_chip *chip;
182 int err;
183
184 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
185 if (!chip)
186 return ERR_PTR(-ENOMEM);
187
188 chip->irq = irq;
189 chip->regs = devm_ioremap_resource(dev, mem);
190 if (IS_ERR(chip->regs))
191 return ERR_CAST(chip->regs);
192
193 err = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(31));
194 if (err)
195 return ERR_PTR(err);
196
197 chip->dev = dev;
198 err = dw_dma_probe(chip, &dw_pdata);
199 if (err)
200 return ERR_PTR(err);
201
202 return chip;
203 }
204
205 static void dw_remove(struct dw_dma_chip *chip)
206 {
207 dw_dma_remove(chip);
208 }
209
210 static bool dma_chan_filter(struct dma_chan *chan, void *param)
211 {
212 struct sst_dsp *dsp = (struct sst_dsp *)param;
213
214 return chan->device->dev == dsp->dma_dev;
215 }
216
217 int sst_dsp_dma_get_channel(struct sst_dsp *dsp, int chan_id)
218 {
219 struct sst_dma *dma = dsp->dma;
220 struct dma_slave_config slave;
221 dma_cap_mask_t mask;
222 int ret;
223
224 /* The Intel MID DMA engine driver needs the slave config set but
225 * Synopsis DMA engine driver safely ignores the slave config */
226 dma_cap_zero(mask);
227 dma_cap_set(DMA_SLAVE, mask);
228 dma_cap_set(DMA_MEMCPY, mask);
229
230 dma->ch = dma_request_channel(mask, dma_chan_filter, dsp);
231 if (dma->ch == NULL) {
232 dev_err(dsp->dev, "error: DMA request channel failed\n");
233 return -EIO;
234 }
235
236 memset(&slave, 0, sizeof(slave));
237 slave.direction = DMA_MEM_TO_DEV;
238 slave.src_addr_width =
239 slave.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
240 slave.src_maxburst = slave.dst_maxburst = SST_DSP_DMA_MAX_BURST;
241
242 ret = dmaengine_slave_config(dma->ch, &slave);
243 if (ret) {
244 dev_err(dsp->dev, "error: unable to set DMA slave config %d\n",
245 ret);
246 dma_release_channel(dma->ch);
247 dma->ch = NULL;
248 }
249
250 return ret;
251 }
252 EXPORT_SYMBOL_GPL(sst_dsp_dma_get_channel);
253
254 void sst_dsp_dma_put_channel(struct sst_dsp *dsp)
255 {
256 struct sst_dma *dma = dsp->dma;
257
258 if (!dma->ch)
259 return;
260
261 dma_release_channel(dma->ch);
262 dma->ch = NULL;
263 }
264 EXPORT_SYMBOL_GPL(sst_dsp_dma_put_channel);
265
266 int sst_dma_new(struct sst_dsp *sst)
267 {
268 struct sst_pdata *sst_pdata = sst->pdata;
269 struct sst_dma *dma;
270 struct resource mem;
271 const char *dma_dev_name;
272 int ret = 0;
273
274 if (sst->pdata->resindex_dma_base == -1)
275 /* DMA is not used, return and squelsh error messages */
276 return 0;
277
278 /* configure the correct platform data for whatever DMA engine
279 * is attached to the ADSP IP. */
280 switch (sst->pdata->dma_engine) {
281 case SST_DMA_TYPE_DW:
282 dma_dev_name = "dw_dmac";
283 break;
284 case SST_DMA_TYPE_MID:
285 dma_dev_name = "Intel MID DMA";
286 break;
287 default:
288 dev_err(sst->dev, "error: invalid DMA engine %d\n",
289 sst->pdata->dma_engine);
290 return -EINVAL;
291 }
292
293 dma = devm_kzalloc(sst->dev, sizeof(struct sst_dma), GFP_KERNEL);
294 if (!dma)
295 return -ENOMEM;
296
297 dma->sst = sst;
298
299 memset(&mem, 0, sizeof(mem));
300
301 mem.start = sst->addr.lpe_base + sst_pdata->dma_base;
302 mem.end = sst->addr.lpe_base + sst_pdata->dma_base + sst_pdata->dma_size - 1;
303 mem.flags = IORESOURCE_MEM;
304
305 /* now register DMA engine device */
306 dma->chip = dw_probe(sst->dma_dev, &mem, sst_pdata->irq);
307 if (IS_ERR(dma->chip)) {
308 dev_err(sst->dev, "error: DMA device register failed\n");
309 ret = PTR_ERR(dma->chip);
310 goto err_dma_dev;
311 }
312
313 sst->dma = dma;
314 sst->fw_use_dma = true;
315 return 0;
316
317 err_dma_dev:
318 devm_kfree(sst->dev, dma);
319 return ret;
320 }
321 EXPORT_SYMBOL(sst_dma_new);
322
323 void sst_dma_free(struct sst_dma *dma)
324 {
325
326 if (dma == NULL)
327 return;
328
329 if (dma->ch)
330 dma_release_channel(dma->ch);
331
332 if (dma->chip)
333 dw_remove(dma->chip);
334
335 }
336 EXPORT_SYMBOL(sst_dma_free);
337
338 /* create new generic firmware object */
339 struct sst_fw *sst_fw_new(struct sst_dsp *dsp,
340 const struct firmware *fw, void *private)
341 {
342 struct sst_fw *sst_fw;
343 int err;
344
345 if (!dsp->ops->parse_fw)
346 return NULL;
347
348 sst_fw = kzalloc(sizeof(*sst_fw), GFP_KERNEL);
349 if (sst_fw == NULL)
350 return NULL;
351
352 sst_fw->dsp = dsp;
353 sst_fw->private = private;
354 sst_fw->size = fw->size;
355
356 /* allocate DMA buffer to store FW data */
357 sst_fw->dma_buf = dma_alloc_coherent(dsp->dma_dev, sst_fw->size,
358 &sst_fw->dmable_fw_paddr, GFP_DMA | GFP_KERNEL);
359 if (!sst_fw->dma_buf) {
360 dev_err(dsp->dev, "error: DMA alloc failed\n");
361 kfree(sst_fw);
362 return NULL;
363 }
364
365 /* copy FW data to DMA-able memory */
366 memcpy((void *)sst_fw->dma_buf, (void *)fw->data, fw->size);
367
368 if (dsp->fw_use_dma) {
369 err = sst_dsp_dma_get_channel(dsp, 0);
370 if (err < 0)
371 goto chan_err;
372 }
373
374 /* call core specific FW paser to load FW data into DSP */
375 err = dsp->ops->parse_fw(sst_fw);
376 if (err < 0) {
377 dev_err(dsp->dev, "error: parse fw failed %d\n", err);
378 goto parse_err;
379 }
380
381 if (dsp->fw_use_dma)
382 sst_dsp_dma_put_channel(dsp);
383
384 mutex_lock(&dsp->mutex);
385 list_add(&sst_fw->list, &dsp->fw_list);
386 mutex_unlock(&dsp->mutex);
387
388 return sst_fw;
389
390 parse_err:
391 if (dsp->fw_use_dma)
392 sst_dsp_dma_put_channel(dsp);
393 chan_err:
394 dma_free_coherent(dsp->dma_dev, sst_fw->size,
395 sst_fw->dma_buf,
396 sst_fw->dmable_fw_paddr);
397 sst_fw->dma_buf = NULL;
398 kfree(sst_fw);
399 return NULL;
400 }
401 EXPORT_SYMBOL_GPL(sst_fw_new);
402
403 int sst_fw_reload(struct sst_fw *sst_fw)
404 {
405 struct sst_dsp *dsp = sst_fw->dsp;
406 int ret;
407
408 dev_dbg(dsp->dev, "reloading firmware\n");
409
410 /* call core specific FW paser to load FW data into DSP */
411 ret = dsp->ops->parse_fw(sst_fw);
412 if (ret < 0)
413 dev_err(dsp->dev, "error: parse fw failed %d\n", ret);
414
415 return ret;
416 }
417 EXPORT_SYMBOL_GPL(sst_fw_reload);
418
419 void sst_fw_unload(struct sst_fw *sst_fw)
420 {
421 struct sst_dsp *dsp = sst_fw->dsp;
422 struct sst_module *module, *mtmp;
423 struct sst_module_runtime *runtime, *rtmp;
424
425 dev_dbg(dsp->dev, "unloading firmware\n");
426
427 mutex_lock(&dsp->mutex);
428
429 /* check module by module */
430 list_for_each_entry_safe(module, mtmp, &dsp->module_list, list) {
431 if (module->sst_fw == sst_fw) {
432
433 /* remove runtime modules */
434 list_for_each_entry_safe(runtime, rtmp, &module->runtime_list, list) {
435
436 block_list_remove(dsp, &runtime->block_list);
437 list_del(&runtime->list);
438 kfree(runtime);
439 }
440
441 /* now remove the module */
442 block_list_remove(dsp, &module->block_list);
443 list_del(&module->list);
444 kfree(module);
445 }
446 }
447
448 /* remove all scratch blocks */
449 block_list_remove(dsp, &dsp->scratch_block_list);
450
451 mutex_unlock(&dsp->mutex);
452 }
453 EXPORT_SYMBOL_GPL(sst_fw_unload);
454
455 /* free single firmware object */
456 void sst_fw_free(struct sst_fw *sst_fw)
457 {
458 struct sst_dsp *dsp = sst_fw->dsp;
459
460 mutex_lock(&dsp->mutex);
461 list_del(&sst_fw->list);
462 mutex_unlock(&dsp->mutex);
463
464 if (sst_fw->dma_buf)
465 dma_free_coherent(dsp->dma_dev, sst_fw->size, sst_fw->dma_buf,
466 sst_fw->dmable_fw_paddr);
467 kfree(sst_fw);
468 }
469 EXPORT_SYMBOL_GPL(sst_fw_free);
470
471 /* free all firmware objects */
472 void sst_fw_free_all(struct sst_dsp *dsp)
473 {
474 struct sst_fw *sst_fw, *t;
475
476 mutex_lock(&dsp->mutex);
477 list_for_each_entry_safe(sst_fw, t, &dsp->fw_list, list) {
478
479 list_del(&sst_fw->list);
480 dma_free_coherent(dsp->dev, sst_fw->size, sst_fw->dma_buf,
481 sst_fw->dmable_fw_paddr);
482 kfree(sst_fw);
483 }
484 mutex_unlock(&dsp->mutex);
485 }
486 EXPORT_SYMBOL_GPL(sst_fw_free_all);
487
488 /* create a new SST generic module from FW template */
489 struct sst_module *sst_module_new(struct sst_fw *sst_fw,
490 struct sst_module_template *template, void *private)
491 {
492 struct sst_dsp *dsp = sst_fw->dsp;
493 struct sst_module *sst_module;
494
495 sst_module = kzalloc(sizeof(*sst_module), GFP_KERNEL);
496 if (sst_module == NULL)
497 return NULL;
498
499 sst_module->id = template->id;
500 sst_module->dsp = dsp;
501 sst_module->sst_fw = sst_fw;
502 sst_module->scratch_size = template->scratch_size;
503 sst_module->persistent_size = template->persistent_size;
504 sst_module->entry = template->entry;
505
506 INIT_LIST_HEAD(&sst_module->block_list);
507 INIT_LIST_HEAD(&sst_module->runtime_list);
508
509 mutex_lock(&dsp->mutex);
510 list_add(&sst_module->list, &dsp->module_list);
511 mutex_unlock(&dsp->mutex);
512
513 return sst_module;
514 }
515 EXPORT_SYMBOL_GPL(sst_module_new);
516
517 /* free firmware module and remove from available list */
518 void sst_module_free(struct sst_module *sst_module)
519 {
520 struct sst_dsp *dsp = sst_module->dsp;
521
522 mutex_lock(&dsp->mutex);
523 list_del(&sst_module->list);
524 mutex_unlock(&dsp->mutex);
525
526 kfree(sst_module);
527 }
528 EXPORT_SYMBOL_GPL(sst_module_free);
529
530 struct sst_module_runtime *sst_module_runtime_new(struct sst_module *module,
531 int id, void *private)
532 {
533 struct sst_dsp *dsp = module->dsp;
534 struct sst_module_runtime *runtime;
535
536 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
537 if (runtime == NULL)
538 return NULL;
539
540 runtime->id = id;
541 runtime->dsp = dsp;
542 runtime->module = module;
543 INIT_LIST_HEAD(&runtime->block_list);
544
545 mutex_lock(&dsp->mutex);
546 list_add(&runtime->list, &module->runtime_list);
547 mutex_unlock(&dsp->mutex);
548
549 return runtime;
550 }
551 EXPORT_SYMBOL_GPL(sst_module_runtime_new);
552
553 void sst_module_runtime_free(struct sst_module_runtime *runtime)
554 {
555 struct sst_dsp *dsp = runtime->dsp;
556
557 mutex_lock(&dsp->mutex);
558 list_del(&runtime->list);
559 mutex_unlock(&dsp->mutex);
560
561 kfree(runtime);
562 }
563 EXPORT_SYMBOL_GPL(sst_module_runtime_free);
564
565 static struct sst_mem_block *find_block(struct sst_dsp *dsp,
566 struct sst_block_allocator *ba)
567 {
568 struct sst_mem_block *block;
569
570 list_for_each_entry(block, &dsp->free_block_list, list) {
571 if (block->type == ba->type && block->offset == ba->offset)
572 return block;
573 }
574
575 return NULL;
576 }
577
578 /* Block allocator must be on block boundary */
579 static int block_alloc_contiguous(struct sst_dsp *dsp,
580 struct sst_block_allocator *ba, struct list_head *block_list)
581 {
582 struct list_head tmp = LIST_HEAD_INIT(tmp);
583 struct sst_mem_block *block;
584 u32 block_start = SST_HSW_BLOCK_ANY;
585 int size = ba->size, offset = ba->offset;
586
587 while (ba->size > 0) {
588
589 block = find_block(dsp, ba);
590 if (!block) {
591 list_splice(&tmp, &dsp->free_block_list);
592
593 ba->size = size;
594 ba->offset = offset;
595 return -ENOMEM;
596 }
597
598 list_move_tail(&block->list, &tmp);
599 ba->offset += block->size;
600 ba->size -= block->size;
601 }
602 ba->size = size;
603 ba->offset = offset;
604
605 list_for_each_entry(block, &tmp, list) {
606
607 if (block->offset < block_start)
608 block_start = block->offset;
609
610 list_add(&block->module_list, block_list);
611
612 dev_dbg(dsp->dev, "block allocated %d:%d at offset 0x%x\n",
613 block->type, block->index, block->offset);
614 }
615
616 list_splice(&tmp, &dsp->used_block_list);
617 return 0;
618 }
619
620 /* allocate first free DSP blocks for data - callers hold locks */
621 static int block_alloc(struct sst_dsp *dsp, struct sst_block_allocator *ba,
622 struct list_head *block_list)
623 {
624 struct sst_mem_block *block, *tmp;
625 int ret = 0;
626
627 if (ba->size == 0)
628 return 0;
629
630 /* find first free whole blocks that can hold module */
631 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
632
633 /* ignore blocks with wrong type */
634 if (block->type != ba->type)
635 continue;
636
637 if (ba->size > block->size)
638 continue;
639
640 ba->offset = block->offset;
641 block->bytes_used = ba->size % block->size;
642 list_add(&block->module_list, block_list);
643 list_move(&block->list, &dsp->used_block_list);
644 dev_dbg(dsp->dev, "block allocated %d:%d at offset 0x%x\n",
645 block->type, block->index, block->offset);
646 return 0;
647 }
648
649 /* then find free multiple blocks that can hold module */
650 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
651
652 /* ignore blocks with wrong type */
653 if (block->type != ba->type)
654 continue;
655
656 /* do we span > 1 blocks */
657 if (ba->size > block->size) {
658
659 /* align ba to block boundary */
660 ba->offset = block->offset;
661
662 ret = block_alloc_contiguous(dsp, ba, block_list);
663 if (ret == 0)
664 return ret;
665
666 }
667 }
668
669 /* not enough free block space */
670 return -ENOMEM;
671 }
672
673 int sst_alloc_blocks(struct sst_dsp *dsp, struct sst_block_allocator *ba,
674 struct list_head *block_list)
675 {
676 int ret;
677
678 dev_dbg(dsp->dev, "block request 0x%x bytes at offset 0x%x type %d\n",
679 ba->size, ba->offset, ba->type);
680
681 mutex_lock(&dsp->mutex);
682
683 ret = block_alloc(dsp, ba, block_list);
684 if (ret < 0) {
685 dev_err(dsp->dev, "error: can't alloc blocks %d\n", ret);
686 goto out;
687 }
688
689 /* prepare DSP blocks for module usage */
690 ret = block_list_prepare(dsp, block_list);
691 if (ret < 0)
692 dev_err(dsp->dev, "error: prepare failed\n");
693
694 out:
695 mutex_unlock(&dsp->mutex);
696 return ret;
697 }
698 EXPORT_SYMBOL_GPL(sst_alloc_blocks);
699
700 int sst_free_blocks(struct sst_dsp *dsp, struct list_head *block_list)
701 {
702 mutex_lock(&dsp->mutex);
703 block_list_remove(dsp, block_list);
704 mutex_unlock(&dsp->mutex);
705 return 0;
706 }
707 EXPORT_SYMBOL_GPL(sst_free_blocks);
708
709 /* allocate memory blocks for static module addresses - callers hold locks */
710 static int block_alloc_fixed(struct sst_dsp *dsp, struct sst_block_allocator *ba,
711 struct list_head *block_list)
712 {
713 struct sst_mem_block *block, *tmp;
714 struct sst_block_allocator ba_tmp = *ba;
715 u32 end = ba->offset + ba->size, block_end;
716 int err;
717
718 /* only IRAM/DRAM blocks are managed */
719 if (ba->type != SST_MEM_IRAM && ba->type != SST_MEM_DRAM)
720 return 0;
721
722 /* are blocks already attached to this module */
723 list_for_each_entry_safe(block, tmp, block_list, module_list) {
724
725 /* ignore blocks with wrong type */
726 if (block->type != ba->type)
727 continue;
728
729 block_end = block->offset + block->size;
730
731 /* find block that holds section */
732 if (ba->offset >= block->offset && end <= block_end)
733 return 0;
734
735 /* does block span more than 1 section */
736 if (ba->offset >= block->offset && ba->offset < block_end) {
737
738 /* align ba to block boundary */
739 ba_tmp.size -= block_end - ba->offset;
740 ba_tmp.offset = block_end;
741 err = block_alloc_contiguous(dsp, &ba_tmp, block_list);
742 if (err < 0)
743 return -ENOMEM;
744
745 /* module already owns blocks */
746 return 0;
747 }
748 }
749
750 /* find first free blocks that can hold section in free list */
751 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
752 block_end = block->offset + block->size;
753
754 /* ignore blocks with wrong type */
755 if (block->type != ba->type)
756 continue;
757
758 /* find block that holds section */
759 if (ba->offset >= block->offset && end <= block_end) {
760
761 /* add block */
762 list_move(&block->list, &dsp->used_block_list);
763 list_add(&block->module_list, block_list);
764 dev_dbg(dsp->dev, "block allocated %d:%d at offset 0x%x\n",
765 block->type, block->index, block->offset);
766 return 0;
767 }
768
769 /* does block span more than 1 section */
770 if (ba->offset >= block->offset && ba->offset < block_end) {
771
772 /* add block */
773 list_move(&block->list, &dsp->used_block_list);
774 list_add(&block->module_list, block_list);
775 /* align ba to block boundary */
776 ba_tmp.size -= block_end - ba->offset;
777 ba_tmp.offset = block_end;
778
779 err = block_alloc_contiguous(dsp, &ba_tmp, block_list);
780 if (err < 0)
781 return -ENOMEM;
782
783 return 0;
784 }
785 }
786
787 return -ENOMEM;
788 }
789
790 /* Load fixed module data into DSP memory blocks */
791 int sst_module_alloc_blocks(struct sst_module *module)
792 {
793 struct sst_dsp *dsp = module->dsp;
794 struct sst_fw *sst_fw = module->sst_fw;
795 struct sst_block_allocator ba;
796 int ret;
797
798 memset(&ba, 0, sizeof(ba));
799 ba.size = module->size;
800 ba.type = module->type;
801 ba.offset = module->offset;
802
803 dev_dbg(dsp->dev, "block request 0x%x bytes at offset 0x%x type %d\n",
804 ba.size, ba.offset, ba.type);
805
806 mutex_lock(&dsp->mutex);
807
808 /* alloc blocks that includes this section */
809 ret = block_alloc_fixed(dsp, &ba, &module->block_list);
810 if (ret < 0) {
811 dev_err(dsp->dev,
812 "error: no free blocks for section at offset 0x%x size 0x%x\n",
813 module->offset, module->size);
814 mutex_unlock(&dsp->mutex);
815 return -ENOMEM;
816 }
817
818 /* prepare DSP blocks for module copy */
819 ret = block_list_prepare(dsp, &module->block_list);
820 if (ret < 0) {
821 dev_err(dsp->dev, "error: fw module prepare failed\n");
822 goto err;
823 }
824
825 /* copy partial module data to blocks */
826 if (dsp->fw_use_dma) {
827 ret = sst_dsp_dma_copyto(dsp,
828 dsp->addr.lpe_base + module->offset,
829 sst_fw->dmable_fw_paddr + module->data_offset,
830 module->size);
831 if (ret < 0) {
832 dev_err(dsp->dev, "error: module copy failed\n");
833 goto err;
834 }
835 } else
836 sst_memcpy32(dsp->addr.lpe + module->offset, module->data,
837 module->size);
838
839 mutex_unlock(&dsp->mutex);
840 return ret;
841
842 err:
843 block_list_remove(dsp, &module->block_list);
844 mutex_unlock(&dsp->mutex);
845 return ret;
846 }
847 EXPORT_SYMBOL_GPL(sst_module_alloc_blocks);
848
849 /* Unload entire module from DSP memory */
850 int sst_module_free_blocks(struct sst_module *module)
851 {
852 struct sst_dsp *dsp = module->dsp;
853
854 mutex_lock(&dsp->mutex);
855 block_list_remove(dsp, &module->block_list);
856 mutex_unlock(&dsp->mutex);
857 return 0;
858 }
859 EXPORT_SYMBOL_GPL(sst_module_free_blocks);
860
861 int sst_module_runtime_alloc_blocks(struct sst_module_runtime *runtime,
862 int offset)
863 {
864 struct sst_dsp *dsp = runtime->dsp;
865 struct sst_module *module = runtime->module;
866 struct sst_block_allocator ba;
867 int ret;
868
869 if (module->persistent_size == 0)
870 return 0;
871
872 memset(&ba, 0, sizeof(ba));
873 ba.size = module->persistent_size;
874 ba.type = SST_MEM_DRAM;
875
876 mutex_lock(&dsp->mutex);
877
878 /* do we need to allocate at a fixed address ? */
879 if (offset != 0) {
880
881 ba.offset = offset;
882
883 dev_dbg(dsp->dev, "persistent fixed block request 0x%x bytes type %d offset 0x%x\n",
884 ba.size, ba.type, ba.offset);
885
886 /* alloc blocks that includes this section */
887 ret = block_alloc_fixed(dsp, &ba, &runtime->block_list);
888
889 } else {
890 dev_dbg(dsp->dev, "persistent block request 0x%x bytes type %d\n",
891 ba.size, ba.type);
892
893 /* alloc blocks that includes this section */
894 ret = block_alloc(dsp, &ba, &runtime->block_list);
895 }
896 if (ret < 0) {
897 dev_err(dsp->dev,
898 "error: no free blocks for runtime module size 0x%x\n",
899 module->persistent_size);
900 mutex_unlock(&dsp->mutex);
901 return -ENOMEM;
902 }
903 runtime->persistent_offset = ba.offset;
904
905 /* prepare DSP blocks for module copy */
906 ret = block_list_prepare(dsp, &runtime->block_list);
907 if (ret < 0) {
908 dev_err(dsp->dev, "error: runtime block prepare failed\n");
909 goto err;
910 }
911
912 mutex_unlock(&dsp->mutex);
913 return ret;
914
915 err:
916 block_list_remove(dsp, &module->block_list);
917 mutex_unlock(&dsp->mutex);
918 return ret;
919 }
920 EXPORT_SYMBOL_GPL(sst_module_runtime_alloc_blocks);
921
922 int sst_module_runtime_free_blocks(struct sst_module_runtime *runtime)
923 {
924 struct sst_dsp *dsp = runtime->dsp;
925
926 mutex_lock(&dsp->mutex);
927 block_list_remove(dsp, &runtime->block_list);
928 mutex_unlock(&dsp->mutex);
929 return 0;
930 }
931 EXPORT_SYMBOL_GPL(sst_module_runtime_free_blocks);
932
933 int sst_module_runtime_save(struct sst_module_runtime *runtime,
934 struct sst_module_runtime_context *context)
935 {
936 struct sst_dsp *dsp = runtime->dsp;
937 struct sst_module *module = runtime->module;
938 int ret = 0;
939
940 dev_dbg(dsp->dev, "saving runtime %d memory at 0x%x size 0x%x\n",
941 runtime->id, runtime->persistent_offset,
942 module->persistent_size);
943
944 context->buffer = dma_alloc_coherent(dsp->dma_dev,
945 module->persistent_size,
946 &context->dma_buffer, GFP_DMA | GFP_KERNEL);
947 if (!context->buffer) {
948 dev_err(dsp->dev, "error: DMA context alloc failed\n");
949 return -ENOMEM;
950 }
951
952 mutex_lock(&dsp->mutex);
953
954 if (dsp->fw_use_dma) {
955
956 ret = sst_dsp_dma_get_channel(dsp, 0);
957 if (ret < 0)
958 goto err;
959
960 ret = sst_dsp_dma_copyfrom(dsp, context->dma_buffer,
961 dsp->addr.lpe_base + runtime->persistent_offset,
962 module->persistent_size);
963 sst_dsp_dma_put_channel(dsp);
964 if (ret < 0) {
965 dev_err(dsp->dev, "error: context copy failed\n");
966 goto err;
967 }
968 } else
969 sst_memcpy32(context->buffer, dsp->addr.lpe +
970 runtime->persistent_offset,
971 module->persistent_size);
972
973 err:
974 mutex_unlock(&dsp->mutex);
975 return ret;
976 }
977 EXPORT_SYMBOL_GPL(sst_module_runtime_save);
978
979 int sst_module_runtime_restore(struct sst_module_runtime *runtime,
980 struct sst_module_runtime_context *context)
981 {
982 struct sst_dsp *dsp = runtime->dsp;
983 struct sst_module *module = runtime->module;
984 int ret = 0;
985
986 dev_dbg(dsp->dev, "restoring runtime %d memory at 0x%x size 0x%x\n",
987 runtime->id, runtime->persistent_offset,
988 module->persistent_size);
989
990 mutex_lock(&dsp->mutex);
991
992 if (!context->buffer) {
993 dev_info(dsp->dev, "no context buffer need to restore!\n");
994 goto err;
995 }
996
997 if (dsp->fw_use_dma) {
998
999 ret = sst_dsp_dma_get_channel(dsp, 0);
1000 if (ret < 0)
1001 goto err;
1002
1003 ret = sst_dsp_dma_copyto(dsp,
1004 dsp->addr.lpe_base + runtime->persistent_offset,
1005 context->dma_buffer, module->persistent_size);
1006 sst_dsp_dma_put_channel(dsp);
1007 if (ret < 0) {
1008 dev_err(dsp->dev, "error: module copy failed\n");
1009 goto err;
1010 }
1011 } else
1012 sst_memcpy32(dsp->addr.lpe + runtime->persistent_offset,
1013 context->buffer, module->persistent_size);
1014
1015 dma_free_coherent(dsp->dma_dev, module->persistent_size,
1016 context->buffer, context->dma_buffer);
1017 context->buffer = NULL;
1018
1019 err:
1020 mutex_unlock(&dsp->mutex);
1021 return ret;
1022 }
1023 EXPORT_SYMBOL_GPL(sst_module_runtime_restore);
1024
1025 /* register a DSP memory block for use with FW based modules */
1026 struct sst_mem_block *sst_mem_block_register(struct sst_dsp *dsp, u32 offset,
1027 u32 size, enum sst_mem_type type, struct sst_block_ops *ops, u32 index,
1028 void *private)
1029 {
1030 struct sst_mem_block *block;
1031
1032 block = kzalloc(sizeof(*block), GFP_KERNEL);
1033 if (block == NULL)
1034 return NULL;
1035
1036 block->offset = offset;
1037 block->size = size;
1038 block->index = index;
1039 block->type = type;
1040 block->dsp = dsp;
1041 block->private = private;
1042 block->ops = ops;
1043
1044 mutex_lock(&dsp->mutex);
1045 list_add(&block->list, &dsp->free_block_list);
1046 mutex_unlock(&dsp->mutex);
1047
1048 return block;
1049 }
1050 EXPORT_SYMBOL_GPL(sst_mem_block_register);
1051
1052 /* unregister all DSP memory blocks */
1053 void sst_mem_block_unregister_all(struct sst_dsp *dsp)
1054 {
1055 struct sst_mem_block *block, *tmp;
1056
1057 mutex_lock(&dsp->mutex);
1058
1059 /* unregister used blocks */
1060 list_for_each_entry_safe(block, tmp, &dsp->used_block_list, list) {
1061 list_del(&block->list);
1062 kfree(block);
1063 }
1064
1065 /* unregister free blocks */
1066 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
1067 list_del(&block->list);
1068 kfree(block);
1069 }
1070
1071 mutex_unlock(&dsp->mutex);
1072 }
1073 EXPORT_SYMBOL_GPL(sst_mem_block_unregister_all);
1074
1075 /* allocate scratch buffer blocks */
1076 int sst_block_alloc_scratch(struct sst_dsp *dsp)
1077 {
1078 struct sst_module *module;
1079 struct sst_block_allocator ba;
1080 int ret;
1081
1082 mutex_lock(&dsp->mutex);
1083
1084 /* calculate required scratch size */
1085 dsp->scratch_size = 0;
1086 list_for_each_entry(module, &dsp->module_list, list) {
1087 dev_dbg(dsp->dev, "module %d scratch req 0x%x bytes\n",
1088 module->id, module->scratch_size);
1089 if (dsp->scratch_size < module->scratch_size)
1090 dsp->scratch_size = module->scratch_size;
1091 }
1092
1093 dev_dbg(dsp->dev, "scratch buffer required is 0x%x bytes\n",
1094 dsp->scratch_size);
1095
1096 if (dsp->scratch_size == 0) {
1097 dev_info(dsp->dev, "no modules need scratch buffer\n");
1098 mutex_unlock(&dsp->mutex);
1099 return 0;
1100 }
1101
1102 /* allocate blocks for module scratch buffers */
1103 dev_dbg(dsp->dev, "allocating scratch blocks\n");
1104
1105 ba.size = dsp->scratch_size;
1106 ba.type = SST_MEM_DRAM;
1107
1108 /* do we need to allocate at fixed offset */
1109 if (dsp->scratch_offset != 0) {
1110
1111 dev_dbg(dsp->dev, "block request 0x%x bytes type %d at 0x%x\n",
1112 ba.size, ba.type, ba.offset);
1113
1114 ba.offset = dsp->scratch_offset;
1115
1116 /* alloc blocks that includes this section */
1117 ret = block_alloc_fixed(dsp, &ba, &dsp->scratch_block_list);
1118
1119 } else {
1120 dev_dbg(dsp->dev, "block request 0x%x bytes type %d\n",
1121 ba.size, ba.type);
1122
1123 ba.offset = 0;
1124 ret = block_alloc(dsp, &ba, &dsp->scratch_block_list);
1125 }
1126 if (ret < 0) {
1127 dev_err(dsp->dev, "error: can't alloc scratch blocks\n");
1128 mutex_unlock(&dsp->mutex);
1129 return ret;
1130 }
1131
1132 ret = block_list_prepare(dsp, &dsp->scratch_block_list);
1133 if (ret < 0) {
1134 dev_err(dsp->dev, "error: scratch block prepare failed\n");
1135 mutex_unlock(&dsp->mutex);
1136 return ret;
1137 }
1138
1139 /* assign the same offset of scratch to each module */
1140 dsp->scratch_offset = ba.offset;
1141 mutex_unlock(&dsp->mutex);
1142 return dsp->scratch_size;
1143 }
1144 EXPORT_SYMBOL_GPL(sst_block_alloc_scratch);
1145
1146 /* free all scratch blocks */
1147 void sst_block_free_scratch(struct sst_dsp *dsp)
1148 {
1149 mutex_lock(&dsp->mutex);
1150 block_list_remove(dsp, &dsp->scratch_block_list);
1151 mutex_unlock(&dsp->mutex);
1152 }
1153 EXPORT_SYMBOL_GPL(sst_block_free_scratch);
1154
1155 /* get a module from it's unique ID */
1156 struct sst_module *sst_module_get_from_id(struct sst_dsp *dsp, u32 id)
1157 {
1158 struct sst_module *module;
1159
1160 mutex_lock(&dsp->mutex);
1161
1162 list_for_each_entry(module, &dsp->module_list, list) {
1163 if (module->id == id) {
1164 mutex_unlock(&dsp->mutex);
1165 return module;
1166 }
1167 }
1168
1169 mutex_unlock(&dsp->mutex);
1170 return NULL;
1171 }
1172 EXPORT_SYMBOL_GPL(sst_module_get_from_id);
1173
1174 struct sst_module_runtime *sst_module_runtime_get_from_id(
1175 struct sst_module *module, u32 id)
1176 {
1177 struct sst_module_runtime *runtime;
1178 struct sst_dsp *dsp = module->dsp;
1179
1180 mutex_lock(&dsp->mutex);
1181
1182 list_for_each_entry(runtime, &module->runtime_list, list) {
1183 if (runtime->id == id) {
1184 mutex_unlock(&dsp->mutex);
1185 return runtime;
1186 }
1187 }
1188
1189 mutex_unlock(&dsp->mutex);
1190 return NULL;
1191 }
1192 EXPORT_SYMBOL_GPL(sst_module_runtime_get_from_id);
1193
1194 /* returns block address in DSP address space */
1195 u32 sst_dsp_get_offset(struct sst_dsp *dsp, u32 offset,
1196 enum sst_mem_type type)
1197 {
1198 switch (type) {
1199 case SST_MEM_IRAM:
1200 return offset - dsp->addr.iram_offset +
1201 dsp->addr.dsp_iram_offset;
1202 case SST_MEM_DRAM:
1203 return offset - dsp->addr.dram_offset +
1204 dsp->addr.dsp_dram_offset;
1205 default:
1206 return 0;
1207 }
1208 }
1209 EXPORT_SYMBOL_GPL(sst_dsp_get_offset);