2 * Intel SST Firmware Loader
4 * Copyright (C) 2013, Intel Corporation. All rights reserved.
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.
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.
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>
28 /* supported DMA engine drivers */
29 #include <linux/platform_data/dma-dw.h>
30 #include <linux/dma/dw.h>
33 #include <asm/pgtable.h>
36 #include "sst-dsp-priv.h"
38 #define SST_DMA_RESOURCES 2
39 #define SST_DSP_DMA_MAX_BURST 0x3
40 #define SST_HSW_BLOCK_ANY 0xffffffff
42 #define SST_HSW_MASK_DMA_ADDR_DSP 0xfff00000
47 struct dw_dma_chip
*chip
;
49 struct dma_async_tx_descriptor
*desc
;
53 static inline void sst_memcpy32(volatile void __iomem
*dest
, void *src
, u32 bytes
)
55 /* __iowrite32_copy use 32bit size values so divide by 4 */
56 __iowrite32_copy((void *)dest
, src
, bytes
/4);
59 static void sst_dma_transfer_complete(void *arg
)
61 struct sst_dsp
*sst
= (struct sst_dsp
*)arg
;
63 dev_dbg(sst
->dev
, "DMA: callback\n");
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
)
69 struct dma_async_tx_descriptor
*desc
;
70 struct sst_dma
*dma
= sst
->dma
;
72 if (dma
->ch
== NULL
) {
73 dev_err(sst
->dev
, "error: no DMA channel\n");
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
);
80 desc
= dma
->ch
->device
->device_prep_dma_memcpy(dma
->ch
, dest_addr
,
81 src_addr
, size
, DMA_CTRL_ACK
);
83 dev_err(sst
->dev
, "error: dma prep memcpy failed\n");
87 desc
->callback
= sst_dma_transfer_complete
;
88 desc
->callback_param
= sst
;
90 desc
->tx_submit(desc
);
91 dma_wait_for_async_tx(desc
);
97 int sst_dsp_dma_copyto(struct sst_dsp
*sst
, dma_addr_t dest_addr
,
98 dma_addr_t src_addr
, size_t size
)
100 return sst_dsp_dma_copy(sst
, dest_addr
| SST_HSW_MASK_DMA_ADDR_DSP
,
103 EXPORT_SYMBOL_GPL(sst_dsp_dma_copyto
);
106 int sst_dsp_dma_copyfrom(struct sst_dsp
*sst
, dma_addr_t dest_addr
,
107 dma_addr_t src_addr
, size_t size
)
109 return sst_dsp_dma_copy(sst
, dest_addr
,
110 src_addr
| SST_HSW_MASK_DMA_ADDR_DSP
, size
);
112 EXPORT_SYMBOL_GPL(sst_dsp_dma_copyfrom
);
114 /* remove module from memory - callers hold locks */
115 static void block_list_remove(struct sst_dsp
*dsp
,
116 struct list_head
*block_list
)
118 struct sst_mem_block
*block
, *tmp
;
121 /* disable each block */
122 list_for_each_entry(block
, block_list
, module_list
) {
124 if (block
->ops
&& block
->ops
->disable
) {
125 err
= block
->ops
->disable(block
);
128 "error: cant disable block %d:%d\n",
129 block
->type
, block
->index
);
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
);
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
)
146 struct sst_mem_block
*block
;
149 /* enable each block so that's it'e ready for data */
150 list_for_each_entry(block
, block_list
, module_list
) {
152 if (block
->ops
&& block
->ops
->enable
&& !block
->users
) {
153 ret
= block
->ops
->enable(block
);
156 "error: cant disable block %d:%d\n",
157 block
->type
, block
->index
);
165 list_for_each_entry(block
, block_list
, module_list
) {
166 if (block
->ops
&& block
->ops
->disable
)
167 block
->ops
->disable(block
);
172 static struct dw_dma_platform_data dw_pdata
= {
174 .chan_allocation_order
= CHAN_ALLOCATION_ASCENDING
,
175 .chan_priority
= CHAN_PRIORITY_ASCENDING
,
178 static struct dw_dma_chip
*dw_probe(struct device
*dev
, struct resource
*mem
,
181 struct dw_dma_chip
*chip
;
184 chip
= devm_kzalloc(dev
, sizeof(*chip
), GFP_KERNEL
);
186 return ERR_PTR(-ENOMEM
);
189 chip
->regs
= devm_ioremap_resource(dev
, mem
);
190 if (IS_ERR(chip
->regs
))
191 return ERR_CAST(chip
->regs
);
193 err
= dma_coerce_mask_and_coherent(dev
, DMA_BIT_MASK(31));
198 err
= dw_dma_probe(chip
, &dw_pdata
);
205 static void dw_remove(struct dw_dma_chip
*chip
)
210 static bool dma_chan_filter(struct dma_chan
*chan
, void *param
)
212 struct sst_dsp
*dsp
= (struct sst_dsp
*)param
;
214 return chan
->device
->dev
== dsp
->dma_dev
;
217 int sst_dsp_dma_get_channel(struct sst_dsp
*dsp
, int chan_id
)
219 struct sst_dma
*dma
= dsp
->dma
;
220 struct dma_slave_config slave
;
224 /* The Intel MID DMA engine driver needs the slave config set but
225 * Synopsis DMA engine driver safely ignores the slave config */
227 dma_cap_set(DMA_SLAVE
, mask
);
228 dma_cap_set(DMA_MEMCPY
, mask
);
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");
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
;
242 ret
= dmaengine_slave_config(dma
->ch
, &slave
);
244 dev_err(dsp
->dev
, "error: unable to set DMA slave config %d\n",
246 dma_release_channel(dma
->ch
);
252 EXPORT_SYMBOL_GPL(sst_dsp_dma_get_channel
);
254 void sst_dsp_dma_put_channel(struct sst_dsp
*dsp
)
256 struct sst_dma
*dma
= dsp
->dma
;
261 dma_release_channel(dma
->ch
);
264 EXPORT_SYMBOL_GPL(sst_dsp_dma_put_channel
);
266 int sst_dma_new(struct sst_dsp
*sst
)
268 struct sst_pdata
*sst_pdata
= sst
->pdata
;
271 const char *dma_dev_name
;
274 /* configure the correct platform data for whatever DMA engine
275 * is attached to the ADSP IP. */
276 switch (sst
->pdata
->dma_engine
) {
277 case SST_DMA_TYPE_DW
:
278 dma_dev_name
= "dw_dmac";
280 case SST_DMA_TYPE_MID
:
281 dma_dev_name
= "Intel MID DMA";
284 dev_err(sst
->dev
, "error: invalid DMA engine %d\n",
285 sst
->pdata
->dma_engine
);
289 dma
= devm_kzalloc(sst
->dev
, sizeof(struct sst_dma
), GFP_KERNEL
);
295 memset(&mem
, 0, sizeof(mem
));
297 mem
.start
= sst
->addr
.lpe_base
+ sst_pdata
->dma_base
;
298 mem
.end
= sst
->addr
.lpe_base
+ sst_pdata
->dma_base
+ sst_pdata
->dma_size
- 1;
299 mem
.flags
= IORESOURCE_MEM
;
301 /* now register DMA engine device */
302 dma
->chip
= dw_probe(sst
->dma_dev
, &mem
, sst_pdata
->irq
);
303 if (IS_ERR(dma
->chip
)) {
304 dev_err(sst
->dev
, "error: DMA device register failed\n");
305 ret
= PTR_ERR(dma
->chip
);
310 sst
->fw_use_dma
= true;
314 devm_kfree(sst
->dev
, dma
);
317 EXPORT_SYMBOL(sst_dma_new
);
319 void sst_dma_free(struct sst_dma
*dma
)
326 dma_release_channel(dma
->ch
);
329 dw_remove(dma
->chip
);
332 EXPORT_SYMBOL(sst_dma_free
);
334 /* create new generic firmware object */
335 struct sst_fw
*sst_fw_new(struct sst_dsp
*dsp
,
336 const struct firmware
*fw
, void *private)
338 struct sst_fw
*sst_fw
;
341 if (!dsp
->ops
->parse_fw
)
344 sst_fw
= kzalloc(sizeof(*sst_fw
), GFP_KERNEL
);
349 sst_fw
->private = private;
350 sst_fw
->size
= fw
->size
;
352 /* allocate DMA buffer to store FW data */
353 sst_fw
->dma_buf
= dma_alloc_coherent(dsp
->dma_dev
, sst_fw
->size
,
354 &sst_fw
->dmable_fw_paddr
, GFP_DMA
| GFP_KERNEL
);
355 if (!sst_fw
->dma_buf
) {
356 dev_err(dsp
->dev
, "error: DMA alloc failed\n");
361 /* copy FW data to DMA-able memory */
362 memcpy((void *)sst_fw
->dma_buf
, (void *)fw
->data
, fw
->size
);
364 if (dsp
->fw_use_dma
) {
365 err
= sst_dsp_dma_get_channel(dsp
, 0);
370 /* call core specific FW paser to load FW data into DSP */
371 err
= dsp
->ops
->parse_fw(sst_fw
);
373 dev_err(dsp
->dev
, "error: parse fw failed %d\n", err
);
378 sst_dsp_dma_put_channel(dsp
);
380 mutex_lock(&dsp
->mutex
);
381 list_add(&sst_fw
->list
, &dsp
->fw_list
);
382 mutex_unlock(&dsp
->mutex
);
388 sst_dsp_dma_put_channel(dsp
);
390 dma_free_coherent(dsp
->dma_dev
, sst_fw
->size
,
392 sst_fw
->dmable_fw_paddr
);
393 sst_fw
->dma_buf
= NULL
;
397 EXPORT_SYMBOL_GPL(sst_fw_new
);
399 int sst_fw_reload(struct sst_fw
*sst_fw
)
401 struct sst_dsp
*dsp
= sst_fw
->dsp
;
404 dev_dbg(dsp
->dev
, "reloading firmware\n");
406 /* call core specific FW paser to load FW data into DSP */
407 ret
= dsp
->ops
->parse_fw(sst_fw
);
409 dev_err(dsp
->dev
, "error: parse fw failed %d\n", ret
);
413 EXPORT_SYMBOL_GPL(sst_fw_reload
);
415 void sst_fw_unload(struct sst_fw
*sst_fw
)
417 struct sst_dsp
*dsp
= sst_fw
->dsp
;
418 struct sst_module
*module
, *mtmp
;
419 struct sst_module_runtime
*runtime
, *rtmp
;
421 dev_dbg(dsp
->dev
, "unloading firmware\n");
423 mutex_lock(&dsp
->mutex
);
425 /* check module by module */
426 list_for_each_entry_safe(module
, mtmp
, &dsp
->module_list
, list
) {
427 if (module
->sst_fw
== sst_fw
) {
429 /* remove runtime modules */
430 list_for_each_entry_safe(runtime
, rtmp
, &module
->runtime_list
, list
) {
432 block_list_remove(dsp
, &runtime
->block_list
);
433 list_del(&runtime
->list
);
437 /* now remove the module */
438 block_list_remove(dsp
, &module
->block_list
);
439 list_del(&module
->list
);
444 /* remove all scratch blocks */
445 block_list_remove(dsp
, &dsp
->scratch_block_list
);
447 mutex_unlock(&dsp
->mutex
);
449 EXPORT_SYMBOL_GPL(sst_fw_unload
);
451 /* free single firmware object */
452 void sst_fw_free(struct sst_fw
*sst_fw
)
454 struct sst_dsp
*dsp
= sst_fw
->dsp
;
456 mutex_lock(&dsp
->mutex
);
457 list_del(&sst_fw
->list
);
458 mutex_unlock(&dsp
->mutex
);
461 dma_free_coherent(dsp
->dma_dev
, sst_fw
->size
, sst_fw
->dma_buf
,
462 sst_fw
->dmable_fw_paddr
);
465 EXPORT_SYMBOL_GPL(sst_fw_free
);
467 /* free all firmware objects */
468 void sst_fw_free_all(struct sst_dsp
*dsp
)
470 struct sst_fw
*sst_fw
, *t
;
472 mutex_lock(&dsp
->mutex
);
473 list_for_each_entry_safe(sst_fw
, t
, &dsp
->fw_list
, list
) {
475 list_del(&sst_fw
->list
);
476 dma_free_coherent(dsp
->dev
, sst_fw
->size
, sst_fw
->dma_buf
,
477 sst_fw
->dmable_fw_paddr
);
480 mutex_unlock(&dsp
->mutex
);
482 EXPORT_SYMBOL_GPL(sst_fw_free_all
);
484 /* create a new SST generic module from FW template */
485 struct sst_module
*sst_module_new(struct sst_fw
*sst_fw
,
486 struct sst_module_template
*template, void *private)
488 struct sst_dsp
*dsp
= sst_fw
->dsp
;
489 struct sst_module
*sst_module
;
491 sst_module
= kzalloc(sizeof(*sst_module
), GFP_KERNEL
);
492 if (sst_module
== NULL
)
495 sst_module
->id
= template->id
;
496 sst_module
->dsp
= dsp
;
497 sst_module
->sst_fw
= sst_fw
;
498 sst_module
->scratch_size
= template->scratch_size
;
499 sst_module
->persistent_size
= template->persistent_size
;
501 INIT_LIST_HEAD(&sst_module
->block_list
);
502 INIT_LIST_HEAD(&sst_module
->runtime_list
);
504 mutex_lock(&dsp
->mutex
);
505 list_add(&sst_module
->list
, &dsp
->module_list
);
506 mutex_unlock(&dsp
->mutex
);
510 EXPORT_SYMBOL_GPL(sst_module_new
);
512 /* free firmware module and remove from available list */
513 void sst_module_free(struct sst_module
*sst_module
)
515 struct sst_dsp
*dsp
= sst_module
->dsp
;
517 mutex_lock(&dsp
->mutex
);
518 list_del(&sst_module
->list
);
519 mutex_unlock(&dsp
->mutex
);
523 EXPORT_SYMBOL_GPL(sst_module_free
);
525 struct sst_module_runtime
*sst_module_runtime_new(struct sst_module
*module
,
526 int id
, void *private)
528 struct sst_dsp
*dsp
= module
->dsp
;
529 struct sst_module_runtime
*runtime
;
531 runtime
= kzalloc(sizeof(*runtime
), GFP_KERNEL
);
537 runtime
->module
= module
;
538 INIT_LIST_HEAD(&runtime
->block_list
);
540 mutex_lock(&dsp
->mutex
);
541 list_add(&runtime
->list
, &module
->runtime_list
);
542 mutex_unlock(&dsp
->mutex
);
546 EXPORT_SYMBOL_GPL(sst_module_runtime_new
);
548 void sst_module_runtime_free(struct sst_module_runtime
*runtime
)
550 struct sst_dsp
*dsp
= runtime
->dsp
;
552 mutex_lock(&dsp
->mutex
);
553 list_del(&runtime
->list
);
554 mutex_unlock(&dsp
->mutex
);
558 EXPORT_SYMBOL_GPL(sst_module_runtime_free
);
560 static struct sst_mem_block
*find_block(struct sst_dsp
*dsp
,
561 struct sst_block_allocator
*ba
)
563 struct sst_mem_block
*block
;
565 list_for_each_entry(block
, &dsp
->free_block_list
, list
) {
566 if (block
->type
== ba
->type
&& block
->offset
== ba
->offset
)
573 /* Block allocator must be on block boundary */
574 static int block_alloc_contiguous(struct sst_dsp
*dsp
,
575 struct sst_block_allocator
*ba
, struct list_head
*block_list
)
577 struct list_head tmp
= LIST_HEAD_INIT(tmp
);
578 struct sst_mem_block
*block
;
579 u32 block_start
= SST_HSW_BLOCK_ANY
;
580 int size
= ba
->size
, offset
= ba
->offset
;
582 while (ba
->size
> 0) {
584 block
= find_block(dsp
, ba
);
586 list_splice(&tmp
, &dsp
->free_block_list
);
593 list_move_tail(&block
->list
, &tmp
);
594 ba
->offset
+= block
->size
;
595 ba
->size
-= block
->size
;
600 list_for_each_entry(block
, &tmp
, list
) {
602 if (block
->offset
< block_start
)
603 block_start
= block
->offset
;
605 list_add(&block
->module_list
, block_list
);
607 dev_dbg(dsp
->dev
, "block allocated %d:%d at offset 0x%x\n",
608 block
->type
, block
->index
, block
->offset
);
611 list_splice(&tmp
, &dsp
->used_block_list
);
615 /* allocate first free DSP blocks for data - callers hold locks */
616 static int block_alloc(struct sst_dsp
*dsp
, struct sst_block_allocator
*ba
,
617 struct list_head
*block_list
)
619 struct sst_mem_block
*block
, *tmp
;
625 /* find first free whole blocks that can hold module */
626 list_for_each_entry_safe(block
, tmp
, &dsp
->free_block_list
, list
) {
628 /* ignore blocks with wrong type */
629 if (block
->type
!= ba
->type
)
632 if (ba
->size
> block
->size
)
635 ba
->offset
= block
->offset
;
636 block
->bytes_used
= ba
->size
% block
->size
;
637 list_add(&block
->module_list
, block_list
);
638 list_move(&block
->list
, &dsp
->used_block_list
);
639 dev_dbg(dsp
->dev
, "block allocated %d:%d at offset 0x%x\n",
640 block
->type
, block
->index
, block
->offset
);
644 /* then find free multiple blocks that can hold module */
645 list_for_each_entry_safe(block
, tmp
, &dsp
->free_block_list
, list
) {
647 /* ignore blocks with wrong type */
648 if (block
->type
!= ba
->type
)
651 /* do we span > 1 blocks */
652 if (ba
->size
> block
->size
) {
654 /* align ba to block boundary */
655 ba
->offset
= block
->offset
;
657 ret
= block_alloc_contiguous(dsp
, ba
, block_list
);
664 /* not enough free block space */
668 int sst_alloc_blocks(struct sst_dsp
*dsp
, struct sst_block_allocator
*ba
,
669 struct list_head
*block_list
)
673 dev_dbg(dsp
->dev
, "block request 0x%x bytes at offset 0x%x type %d\n",
674 ba
->size
, ba
->offset
, ba
->type
);
676 mutex_lock(&dsp
->mutex
);
678 ret
= block_alloc(dsp
, ba
, block_list
);
680 dev_err(dsp
->dev
, "error: can't alloc blocks %d\n", ret
);
684 /* prepare DSP blocks for module usage */
685 ret
= block_list_prepare(dsp
, block_list
);
687 dev_err(dsp
->dev
, "error: prepare failed\n");
690 mutex_unlock(&dsp
->mutex
);
693 EXPORT_SYMBOL_GPL(sst_alloc_blocks
);
695 int sst_free_blocks(struct sst_dsp
*dsp
, struct list_head
*block_list
)
697 mutex_lock(&dsp
->mutex
);
698 block_list_remove(dsp
, block_list
);
699 mutex_unlock(&dsp
->mutex
);
702 EXPORT_SYMBOL_GPL(sst_free_blocks
);
704 /* allocate memory blocks for static module addresses - callers hold locks */
705 static int block_alloc_fixed(struct sst_dsp
*dsp
, struct sst_block_allocator
*ba
,
706 struct list_head
*block_list
)
708 struct sst_mem_block
*block
, *tmp
;
709 u32 end
= ba
->offset
+ ba
->size
, block_end
;
712 /* only IRAM/DRAM blocks are managed */
713 if (ba
->type
!= SST_MEM_IRAM
&& ba
->type
!= SST_MEM_DRAM
)
716 /* are blocks already attached to this module */
717 list_for_each_entry_safe(block
, tmp
, block_list
, module_list
) {
719 /* ignore blocks with wrong type */
720 if (block
->type
!= ba
->type
)
723 block_end
= block
->offset
+ block
->size
;
725 /* find block that holds section */
726 if (ba
->offset
>= block
->offset
&& end
<= block_end
)
729 /* does block span more than 1 section */
730 if (ba
->offset
>= block
->offset
&& ba
->offset
< block_end
) {
732 /* align ba to block boundary */
733 ba
->size
-= block_end
- ba
->offset
;
734 ba
->offset
= block_end
;
735 err
= block_alloc_contiguous(dsp
, ba
, block_list
);
739 /* module already owns blocks */
744 /* find first free blocks that can hold section in free list */
745 list_for_each_entry_safe(block
, tmp
, &dsp
->free_block_list
, list
) {
746 block_end
= block
->offset
+ block
->size
;
748 /* ignore blocks with wrong type */
749 if (block
->type
!= ba
->type
)
752 /* find block that holds section */
753 if (ba
->offset
>= block
->offset
&& end
<= block_end
) {
756 list_move(&block
->list
, &dsp
->used_block_list
);
757 list_add(&block
->module_list
, block_list
);
758 dev_dbg(dsp
->dev
, "block allocated %d:%d at offset 0x%x\n",
759 block
->type
, block
->index
, block
->offset
);
763 /* does block span more than 1 section */
764 if (ba
->offset
>= block
->offset
&& ba
->offset
< block_end
) {
767 list_move(&block
->list
, &dsp
->used_block_list
);
768 list_add(&block
->module_list
, block_list
);
769 /* align ba to block boundary */
770 ba
->size
-= block_end
- ba
->offset
;
771 ba
->offset
= block_end
;
773 err
= block_alloc_contiguous(dsp
, ba
, block_list
);
784 /* Load fixed module data into DSP memory blocks */
785 int sst_module_alloc_blocks(struct sst_module
*module
)
787 struct sst_dsp
*dsp
= module
->dsp
;
788 struct sst_fw
*sst_fw
= module
->sst_fw
;
789 struct sst_block_allocator ba
;
792 ba
.size
= module
->size
;
793 ba
.type
= module
->type
;
794 ba
.offset
= module
->offset
;
796 dev_dbg(dsp
->dev
, "block request 0x%x bytes at offset 0x%x type %d\n",
797 ba
.size
, ba
.offset
, ba
.type
);
799 mutex_lock(&dsp
->mutex
);
801 /* alloc blocks that includes this section */
802 ret
= block_alloc_fixed(dsp
, &ba
, &module
->block_list
);
805 "error: no free blocks for section at offset 0x%x size 0x%x\n",
806 module
->offset
, module
->size
);
807 mutex_unlock(&dsp
->mutex
);
811 /* prepare DSP blocks for module copy */
812 ret
= block_list_prepare(dsp
, &module
->block_list
);
814 dev_err(dsp
->dev
, "error: fw module prepare failed\n");
818 /* copy partial module data to blocks */
819 if (dsp
->fw_use_dma
) {
820 ret
= sst_dsp_dma_copyto(dsp
,
821 dsp
->addr
.lpe_base
+ module
->offset
,
822 sst_fw
->dmable_fw_paddr
+ module
->data_offset
,
825 dev_err(dsp
->dev
, "error: module copy failed\n");
829 sst_memcpy32(dsp
->addr
.lpe
+ module
->offset
, module
->data
,
832 mutex_unlock(&dsp
->mutex
);
836 block_list_remove(dsp
, &module
->block_list
);
837 mutex_unlock(&dsp
->mutex
);
840 EXPORT_SYMBOL_GPL(sst_module_alloc_blocks
);
842 /* Unload entire module from DSP memory */
843 int sst_module_free_blocks(struct sst_module
*module
)
845 struct sst_dsp
*dsp
= module
->dsp
;
847 mutex_lock(&dsp
->mutex
);
848 block_list_remove(dsp
, &module
->block_list
);
849 mutex_unlock(&dsp
->mutex
);
852 EXPORT_SYMBOL_GPL(sst_module_free_blocks
);
854 int sst_module_runtime_alloc_blocks(struct sst_module_runtime
*runtime
,
857 struct sst_dsp
*dsp
= runtime
->dsp
;
858 struct sst_module
*module
= runtime
->module
;
859 struct sst_block_allocator ba
;
862 if (module
->persistent_size
== 0)
865 ba
.size
= module
->persistent_size
;
866 ba
.type
= SST_MEM_DRAM
;
868 mutex_lock(&dsp
->mutex
);
870 /* do we need to allocate at a fixed address ? */
875 dev_dbg(dsp
->dev
, "persistent fixed block request 0x%x bytes type %d offset 0x%x\n",
876 ba
.size
, ba
.type
, ba
.offset
);
878 /* alloc blocks that includes this section */
879 ret
= block_alloc_fixed(dsp
, &ba
, &runtime
->block_list
);
882 dev_dbg(dsp
->dev
, "persistent block request 0x%x bytes type %d\n",
885 /* alloc blocks that includes this section */
886 ret
= block_alloc(dsp
, &ba
, &runtime
->block_list
);
890 "error: no free blocks for runtime module size 0x%x\n",
891 module
->persistent_size
);
892 mutex_unlock(&dsp
->mutex
);
895 runtime
->persistent_offset
= ba
.offset
;
897 /* prepare DSP blocks for module copy */
898 ret
= block_list_prepare(dsp
, &runtime
->block_list
);
900 dev_err(dsp
->dev
, "error: runtime block prepare failed\n");
904 mutex_unlock(&dsp
->mutex
);
908 block_list_remove(dsp
, &module
->block_list
);
909 mutex_unlock(&dsp
->mutex
);
912 EXPORT_SYMBOL_GPL(sst_module_runtime_alloc_blocks
);
914 int sst_module_runtime_free_blocks(struct sst_module_runtime
*runtime
)
916 struct sst_dsp
*dsp
= runtime
->dsp
;
918 mutex_lock(&dsp
->mutex
);
919 block_list_remove(dsp
, &runtime
->block_list
);
920 mutex_unlock(&dsp
->mutex
);
923 EXPORT_SYMBOL_GPL(sst_module_runtime_free_blocks
);
925 int sst_module_runtime_save(struct sst_module_runtime
*runtime
,
926 struct sst_module_runtime_context
*context
)
928 struct sst_dsp
*dsp
= runtime
->dsp
;
929 struct sst_module
*module
= runtime
->module
;
932 dev_dbg(dsp
->dev
, "saving runtime %d memory at 0x%x size 0x%x\n",
933 runtime
->id
, runtime
->persistent_offset
,
934 module
->persistent_size
);
936 context
->buffer
= dma_alloc_coherent(dsp
->dma_dev
,
937 module
->persistent_size
,
938 &context
->dma_buffer
, GFP_DMA
| GFP_KERNEL
);
939 if (!context
->buffer
) {
940 dev_err(dsp
->dev
, "error: DMA context alloc failed\n");
944 mutex_lock(&dsp
->mutex
);
946 if (dsp
->fw_use_dma
) {
948 ret
= sst_dsp_dma_get_channel(dsp
, 0);
952 ret
= sst_dsp_dma_copyfrom(dsp
, context
->dma_buffer
,
953 dsp
->addr
.lpe_base
+ runtime
->persistent_offset
,
954 module
->persistent_size
);
955 sst_dsp_dma_put_channel(dsp
);
957 dev_err(dsp
->dev
, "error: context copy failed\n");
961 sst_memcpy32(context
->buffer
, dsp
->addr
.lpe
+
962 runtime
->persistent_offset
,
963 module
->persistent_size
);
966 mutex_unlock(&dsp
->mutex
);
969 EXPORT_SYMBOL_GPL(sst_module_runtime_save
);
971 int sst_module_runtime_restore(struct sst_module_runtime
*runtime
,
972 struct sst_module_runtime_context
*context
)
974 struct sst_dsp
*dsp
= runtime
->dsp
;
975 struct sst_module
*module
= runtime
->module
;
978 dev_dbg(dsp
->dev
, "restoring runtime %d memory at 0x%x size 0x%x\n",
979 runtime
->id
, runtime
->persistent_offset
,
980 module
->persistent_size
);
982 mutex_lock(&dsp
->mutex
);
984 if (!context
->buffer
) {
985 dev_info(dsp
->dev
, "no context buffer need to restore!\n");
989 if (dsp
->fw_use_dma
) {
991 ret
= sst_dsp_dma_get_channel(dsp
, 0);
995 ret
= sst_dsp_dma_copyto(dsp
,
996 dsp
->addr
.lpe_base
+ runtime
->persistent_offset
,
997 context
->dma_buffer
, module
->persistent_size
);
998 sst_dsp_dma_put_channel(dsp
);
1000 dev_err(dsp
->dev
, "error: module copy failed\n");
1004 sst_memcpy32(dsp
->addr
.lpe
+ runtime
->persistent_offset
,
1005 context
->buffer
, module
->persistent_size
);
1007 dma_free_coherent(dsp
->dma_dev
, module
->persistent_size
,
1008 context
->buffer
, context
->dma_buffer
);
1009 context
->buffer
= NULL
;
1012 mutex_unlock(&dsp
->mutex
);
1015 EXPORT_SYMBOL_GPL(sst_module_runtime_restore
);
1017 /* register a DSP memory block for use with FW based modules */
1018 struct sst_mem_block
*sst_mem_block_register(struct sst_dsp
*dsp
, u32 offset
,
1019 u32 size
, enum sst_mem_type type
, struct sst_block_ops
*ops
, u32 index
,
1022 struct sst_mem_block
*block
;
1024 block
= kzalloc(sizeof(*block
), GFP_KERNEL
);
1028 block
->offset
= offset
;
1030 block
->index
= index
;
1033 block
->private = private;
1036 mutex_lock(&dsp
->mutex
);
1037 list_add(&block
->list
, &dsp
->free_block_list
);
1038 mutex_unlock(&dsp
->mutex
);
1042 EXPORT_SYMBOL_GPL(sst_mem_block_register
);
1044 /* unregister all DSP memory blocks */
1045 void sst_mem_block_unregister_all(struct sst_dsp
*dsp
)
1047 struct sst_mem_block
*block
, *tmp
;
1049 mutex_lock(&dsp
->mutex
);
1051 /* unregister used blocks */
1052 list_for_each_entry_safe(block
, tmp
, &dsp
->used_block_list
, list
) {
1053 list_del(&block
->list
);
1057 /* unregister free blocks */
1058 list_for_each_entry_safe(block
, tmp
, &dsp
->free_block_list
, list
) {
1059 list_del(&block
->list
);
1063 mutex_unlock(&dsp
->mutex
);
1065 EXPORT_SYMBOL_GPL(sst_mem_block_unregister_all
);
1067 /* allocate scratch buffer blocks */
1068 int sst_block_alloc_scratch(struct sst_dsp
*dsp
)
1070 struct sst_module
*module
;
1071 struct sst_block_allocator ba
;
1074 mutex_lock(&dsp
->mutex
);
1076 /* calculate required scratch size */
1077 dsp
->scratch_size
= 0;
1078 list_for_each_entry(module
, &dsp
->module_list
, list
) {
1079 dev_dbg(dsp
->dev
, "module %d scratch req 0x%x bytes\n",
1080 module
->id
, module
->scratch_size
);
1081 if (dsp
->scratch_size
< module
->scratch_size
)
1082 dsp
->scratch_size
= module
->scratch_size
;
1085 dev_dbg(dsp
->dev
, "scratch buffer required is 0x%x bytes\n",
1088 if (dsp
->scratch_size
== 0) {
1089 dev_info(dsp
->dev
, "no modules need scratch buffer\n");
1090 mutex_unlock(&dsp
->mutex
);
1094 /* allocate blocks for module scratch buffers */
1095 dev_dbg(dsp
->dev
, "allocating scratch blocks\n");
1097 ba
.size
= dsp
->scratch_size
;
1098 ba
.type
= SST_MEM_DRAM
;
1100 /* do we need to allocate at fixed offset */
1101 if (dsp
->scratch_offset
!= 0) {
1103 dev_dbg(dsp
->dev
, "block request 0x%x bytes type %d at 0x%x\n",
1104 ba
.size
, ba
.type
, ba
.offset
);
1106 ba
.offset
= dsp
->scratch_offset
;
1108 /* alloc blocks that includes this section */
1109 ret
= block_alloc_fixed(dsp
, &ba
, &dsp
->scratch_block_list
);
1112 dev_dbg(dsp
->dev
, "block request 0x%x bytes type %d\n",
1116 ret
= block_alloc(dsp
, &ba
, &dsp
->scratch_block_list
);
1119 dev_err(dsp
->dev
, "error: can't alloc scratch blocks\n");
1120 mutex_unlock(&dsp
->mutex
);
1124 ret
= block_list_prepare(dsp
, &dsp
->scratch_block_list
);
1126 dev_err(dsp
->dev
, "error: scratch block prepare failed\n");
1127 mutex_unlock(&dsp
->mutex
);
1131 /* assign the same offset of scratch to each module */
1132 dsp
->scratch_offset
= ba
.offset
;
1133 mutex_unlock(&dsp
->mutex
);
1134 return dsp
->scratch_size
;
1136 EXPORT_SYMBOL_GPL(sst_block_alloc_scratch
);
1138 /* free all scratch blocks */
1139 void sst_block_free_scratch(struct sst_dsp
*dsp
)
1141 mutex_lock(&dsp
->mutex
);
1142 block_list_remove(dsp
, &dsp
->scratch_block_list
);
1143 mutex_unlock(&dsp
->mutex
);
1145 EXPORT_SYMBOL_GPL(sst_block_free_scratch
);
1147 /* get a module from it's unique ID */
1148 struct sst_module
*sst_module_get_from_id(struct sst_dsp
*dsp
, u32 id
)
1150 struct sst_module
*module
;
1152 mutex_lock(&dsp
->mutex
);
1154 list_for_each_entry(module
, &dsp
->module_list
, list
) {
1155 if (module
->id
== id
) {
1156 mutex_unlock(&dsp
->mutex
);
1161 mutex_unlock(&dsp
->mutex
);
1164 EXPORT_SYMBOL_GPL(sst_module_get_from_id
);
1166 struct sst_module_runtime
*sst_module_runtime_get_from_id(
1167 struct sst_module
*module
, u32 id
)
1169 struct sst_module_runtime
*runtime
;
1170 struct sst_dsp
*dsp
= module
->dsp
;
1172 mutex_lock(&dsp
->mutex
);
1174 list_for_each_entry(runtime
, &module
->runtime_list
, list
) {
1175 if (runtime
->id
== id
) {
1176 mutex_unlock(&dsp
->mutex
);
1181 mutex_unlock(&dsp
->mutex
);
1184 EXPORT_SYMBOL_GPL(sst_module_runtime_get_from_id
);
1186 /* returns block address in DSP address space */
1187 u32
sst_dsp_get_offset(struct sst_dsp
*dsp
, u32 offset
,
1188 enum sst_mem_type type
)
1192 return offset
- dsp
->addr
.iram_offset
+
1193 dsp
->addr
.dsp_iram_offset
;
1195 return offset
- dsp
->addr
.dram_offset
+
1196 dsp
->addr
.dsp_dram_offset
;
1201 EXPORT_SYMBOL_GPL(sst_dsp_get_offset
);