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
;
225 dma_cap_set(DMA_SLAVE
, mask
);
226 dma_cap_set(DMA_MEMCPY
, mask
);
228 dma
->ch
= dma_request_channel(mask
, dma_chan_filter
, dsp
);
229 if (dma
->ch
== NULL
) {
230 dev_err(dsp
->dev
, "error: DMA request channel failed\n");
234 memset(&slave
, 0, sizeof(slave
));
235 slave
.direction
= DMA_MEM_TO_DEV
;
236 slave
.src_addr_width
=
237 slave
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
238 slave
.src_maxburst
= slave
.dst_maxburst
= SST_DSP_DMA_MAX_BURST
;
240 ret
= dmaengine_slave_config(dma
->ch
, &slave
);
242 dev_err(dsp
->dev
, "error: unable to set DMA slave config %d\n",
244 dma_release_channel(dma
->ch
);
250 EXPORT_SYMBOL_GPL(sst_dsp_dma_get_channel
);
252 void sst_dsp_dma_put_channel(struct sst_dsp
*dsp
)
254 struct sst_dma
*dma
= dsp
->dma
;
259 dma_release_channel(dma
->ch
);
262 EXPORT_SYMBOL_GPL(sst_dsp_dma_put_channel
);
264 int sst_dma_new(struct sst_dsp
*sst
)
266 struct sst_pdata
*sst_pdata
= sst
->pdata
;
269 const char *dma_dev_name
;
272 if (sst
->pdata
->resindex_dma_base
== -1)
273 /* DMA is not used, return and squelsh error messages */
276 /* configure the correct platform data for whatever DMA engine
277 * is attached to the ADSP IP. */
278 switch (sst
->pdata
->dma_engine
) {
279 case SST_DMA_TYPE_DW
:
280 dma_dev_name
= "dw_dmac";
283 dev_err(sst
->dev
, "error: invalid DMA engine %d\n",
284 sst
->pdata
->dma_engine
);
288 dma
= devm_kzalloc(sst
->dev
, sizeof(struct sst_dma
), GFP_KERNEL
);
294 memset(&mem
, 0, sizeof(mem
));
296 mem
.start
= sst
->addr
.lpe_base
+ sst_pdata
->dma_base
;
297 mem
.end
= sst
->addr
.lpe_base
+ sst_pdata
->dma_base
+ sst_pdata
->dma_size
- 1;
298 mem
.flags
= IORESOURCE_MEM
;
300 /* now register DMA engine device */
301 dma
->chip
= dw_probe(sst
->dma_dev
, &mem
, sst_pdata
->irq
);
302 if (IS_ERR(dma
->chip
)) {
303 dev_err(sst
->dev
, "error: DMA device register failed\n");
304 ret
= PTR_ERR(dma
->chip
);
309 sst
->fw_use_dma
= true;
313 devm_kfree(sst
->dev
, dma
);
316 EXPORT_SYMBOL(sst_dma_new
);
318 void sst_dma_free(struct sst_dma
*dma
)
325 dma_release_channel(dma
->ch
);
328 dw_remove(dma
->chip
);
331 EXPORT_SYMBOL(sst_dma_free
);
333 /* create new generic firmware object */
334 struct sst_fw
*sst_fw_new(struct sst_dsp
*dsp
,
335 const struct firmware
*fw
, void *private)
337 struct sst_fw
*sst_fw
;
340 if (!dsp
->ops
->parse_fw
)
343 sst_fw
= kzalloc(sizeof(*sst_fw
), GFP_KERNEL
);
348 sst_fw
->private = private;
349 sst_fw
->size
= fw
->size
;
351 /* allocate DMA buffer to store FW data */
352 sst_fw
->dma_buf
= dma_alloc_coherent(dsp
->dma_dev
, sst_fw
->size
,
353 &sst_fw
->dmable_fw_paddr
, GFP_DMA
| GFP_KERNEL
);
354 if (!sst_fw
->dma_buf
) {
355 dev_err(dsp
->dev
, "error: DMA alloc failed\n");
360 /* copy FW data to DMA-able memory */
361 memcpy((void *)sst_fw
->dma_buf
, (void *)fw
->data
, fw
->size
);
363 if (dsp
->fw_use_dma
) {
364 err
= sst_dsp_dma_get_channel(dsp
, 0);
369 /* call core specific FW paser to load FW data into DSP */
370 err
= dsp
->ops
->parse_fw(sst_fw
);
372 dev_err(dsp
->dev
, "error: parse fw failed %d\n", err
);
377 sst_dsp_dma_put_channel(dsp
);
379 mutex_lock(&dsp
->mutex
);
380 list_add(&sst_fw
->list
, &dsp
->fw_list
);
381 mutex_unlock(&dsp
->mutex
);
387 sst_dsp_dma_put_channel(dsp
);
389 dma_free_coherent(dsp
->dma_dev
, sst_fw
->size
,
391 sst_fw
->dmable_fw_paddr
);
392 sst_fw
->dma_buf
= NULL
;
396 EXPORT_SYMBOL_GPL(sst_fw_new
);
398 int sst_fw_reload(struct sst_fw
*sst_fw
)
400 struct sst_dsp
*dsp
= sst_fw
->dsp
;
403 dev_dbg(dsp
->dev
, "reloading firmware\n");
405 /* call core specific FW paser to load FW data into DSP */
406 ret
= dsp
->ops
->parse_fw(sst_fw
);
408 dev_err(dsp
->dev
, "error: parse fw failed %d\n", ret
);
412 EXPORT_SYMBOL_GPL(sst_fw_reload
);
414 void sst_fw_unload(struct sst_fw
*sst_fw
)
416 struct sst_dsp
*dsp
= sst_fw
->dsp
;
417 struct sst_module
*module
, *mtmp
;
418 struct sst_module_runtime
*runtime
, *rtmp
;
420 dev_dbg(dsp
->dev
, "unloading firmware\n");
422 mutex_lock(&dsp
->mutex
);
424 /* check module by module */
425 list_for_each_entry_safe(module
, mtmp
, &dsp
->module_list
, list
) {
426 if (module
->sst_fw
== sst_fw
) {
428 /* remove runtime modules */
429 list_for_each_entry_safe(runtime
, rtmp
, &module
->runtime_list
, list
) {
431 block_list_remove(dsp
, &runtime
->block_list
);
432 list_del(&runtime
->list
);
436 /* now remove the module */
437 block_list_remove(dsp
, &module
->block_list
);
438 list_del(&module
->list
);
443 /* remove all scratch blocks */
444 block_list_remove(dsp
, &dsp
->scratch_block_list
);
446 mutex_unlock(&dsp
->mutex
);
448 EXPORT_SYMBOL_GPL(sst_fw_unload
);
450 /* free single firmware object */
451 void sst_fw_free(struct sst_fw
*sst_fw
)
453 struct sst_dsp
*dsp
= sst_fw
->dsp
;
455 mutex_lock(&dsp
->mutex
);
456 list_del(&sst_fw
->list
);
457 mutex_unlock(&dsp
->mutex
);
460 dma_free_coherent(dsp
->dma_dev
, sst_fw
->size
, sst_fw
->dma_buf
,
461 sst_fw
->dmable_fw_paddr
);
464 EXPORT_SYMBOL_GPL(sst_fw_free
);
466 /* free all firmware objects */
467 void sst_fw_free_all(struct sst_dsp
*dsp
)
469 struct sst_fw
*sst_fw
, *t
;
471 mutex_lock(&dsp
->mutex
);
472 list_for_each_entry_safe(sst_fw
, t
, &dsp
->fw_list
, list
) {
474 list_del(&sst_fw
->list
);
475 dma_free_coherent(dsp
->dev
, sst_fw
->size
, sst_fw
->dma_buf
,
476 sst_fw
->dmable_fw_paddr
);
479 mutex_unlock(&dsp
->mutex
);
481 EXPORT_SYMBOL_GPL(sst_fw_free_all
);
483 /* create a new SST generic module from FW template */
484 struct sst_module
*sst_module_new(struct sst_fw
*sst_fw
,
485 struct sst_module_template
*template, void *private)
487 struct sst_dsp
*dsp
= sst_fw
->dsp
;
488 struct sst_module
*sst_module
;
490 sst_module
= kzalloc(sizeof(*sst_module
), GFP_KERNEL
);
491 if (sst_module
== NULL
)
494 sst_module
->id
= template->id
;
495 sst_module
->dsp
= dsp
;
496 sst_module
->sst_fw
= sst_fw
;
497 sst_module
->scratch_size
= template->scratch_size
;
498 sst_module
->persistent_size
= template->persistent_size
;
499 sst_module
->entry
= template->entry
;
500 sst_module
->state
= SST_MODULE_STATE_UNLOADED
;
502 INIT_LIST_HEAD(&sst_module
->block_list
);
503 INIT_LIST_HEAD(&sst_module
->runtime_list
);
505 mutex_lock(&dsp
->mutex
);
506 list_add(&sst_module
->list
, &dsp
->module_list
);
507 mutex_unlock(&dsp
->mutex
);
511 EXPORT_SYMBOL_GPL(sst_module_new
);
513 /* free firmware module and remove from available list */
514 void sst_module_free(struct sst_module
*sst_module
)
516 struct sst_dsp
*dsp
= sst_module
->dsp
;
518 mutex_lock(&dsp
->mutex
);
519 list_del(&sst_module
->list
);
520 mutex_unlock(&dsp
->mutex
);
524 EXPORT_SYMBOL_GPL(sst_module_free
);
526 struct sst_module_runtime
*sst_module_runtime_new(struct sst_module
*module
,
527 int id
, void *private)
529 struct sst_dsp
*dsp
= module
->dsp
;
530 struct sst_module_runtime
*runtime
;
532 runtime
= kzalloc(sizeof(*runtime
), GFP_KERNEL
);
538 runtime
->module
= module
;
539 INIT_LIST_HEAD(&runtime
->block_list
);
541 mutex_lock(&dsp
->mutex
);
542 list_add(&runtime
->list
, &module
->runtime_list
);
543 mutex_unlock(&dsp
->mutex
);
547 EXPORT_SYMBOL_GPL(sst_module_runtime_new
);
549 void sst_module_runtime_free(struct sst_module_runtime
*runtime
)
551 struct sst_dsp
*dsp
= runtime
->dsp
;
553 mutex_lock(&dsp
->mutex
);
554 list_del(&runtime
->list
);
555 mutex_unlock(&dsp
->mutex
);
559 EXPORT_SYMBOL_GPL(sst_module_runtime_free
);
561 static struct sst_mem_block
*find_block(struct sst_dsp
*dsp
,
562 struct sst_block_allocator
*ba
)
564 struct sst_mem_block
*block
;
566 list_for_each_entry(block
, &dsp
->free_block_list
, list
) {
567 if (block
->type
== ba
->type
&& block
->offset
== ba
->offset
)
574 /* Block allocator must be on block boundary */
575 static int block_alloc_contiguous(struct sst_dsp
*dsp
,
576 struct sst_block_allocator
*ba
, struct list_head
*block_list
)
578 struct list_head tmp
= LIST_HEAD_INIT(tmp
);
579 struct sst_mem_block
*block
;
580 u32 block_start
= SST_HSW_BLOCK_ANY
;
581 int size
= ba
->size
, offset
= ba
->offset
;
583 while (ba
->size
> 0) {
585 block
= find_block(dsp
, ba
);
587 list_splice(&tmp
, &dsp
->free_block_list
);
594 list_move_tail(&block
->list
, &tmp
);
595 ba
->offset
+= block
->size
;
596 ba
->size
-= block
->size
;
601 list_for_each_entry(block
, &tmp
, list
) {
603 if (block
->offset
< block_start
)
604 block_start
= block
->offset
;
606 list_add(&block
->module_list
, block_list
);
608 dev_dbg(dsp
->dev
, "block allocated %d:%d at offset 0x%x\n",
609 block
->type
, block
->index
, block
->offset
);
612 list_splice(&tmp
, &dsp
->used_block_list
);
616 /* allocate first free DSP blocks for data - callers hold locks */
617 static int block_alloc(struct sst_dsp
*dsp
, struct sst_block_allocator
*ba
,
618 struct list_head
*block_list
)
620 struct sst_mem_block
*block
, *tmp
;
626 /* find first free whole blocks that can hold module */
627 list_for_each_entry_safe(block
, tmp
, &dsp
->free_block_list
, list
) {
629 /* ignore blocks with wrong type */
630 if (block
->type
!= ba
->type
)
633 if (ba
->size
> block
->size
)
636 ba
->offset
= block
->offset
;
637 block
->bytes_used
= ba
->size
% block
->size
;
638 list_add(&block
->module_list
, block_list
);
639 list_move(&block
->list
, &dsp
->used_block_list
);
640 dev_dbg(dsp
->dev
, "block allocated %d:%d at offset 0x%x\n",
641 block
->type
, block
->index
, block
->offset
);
645 /* then find free multiple blocks that can hold module */
646 list_for_each_entry_safe(block
, tmp
, &dsp
->free_block_list
, list
) {
648 /* ignore blocks with wrong type */
649 if (block
->type
!= ba
->type
)
652 /* do we span > 1 blocks */
653 if (ba
->size
> block
->size
) {
655 /* align ba to block boundary */
656 ba
->offset
= block
->offset
;
658 ret
= block_alloc_contiguous(dsp
, ba
, block_list
);
665 /* not enough free block space */
669 int sst_alloc_blocks(struct sst_dsp
*dsp
, struct sst_block_allocator
*ba
,
670 struct list_head
*block_list
)
674 dev_dbg(dsp
->dev
, "block request 0x%x bytes at offset 0x%x type %d\n",
675 ba
->size
, ba
->offset
, ba
->type
);
677 mutex_lock(&dsp
->mutex
);
679 ret
= block_alloc(dsp
, ba
, block_list
);
681 dev_err(dsp
->dev
, "error: can't alloc blocks %d\n", ret
);
685 /* prepare DSP blocks for module usage */
686 ret
= block_list_prepare(dsp
, block_list
);
688 dev_err(dsp
->dev
, "error: prepare failed\n");
691 mutex_unlock(&dsp
->mutex
);
694 EXPORT_SYMBOL_GPL(sst_alloc_blocks
);
696 int sst_free_blocks(struct sst_dsp
*dsp
, struct list_head
*block_list
)
698 mutex_lock(&dsp
->mutex
);
699 block_list_remove(dsp
, block_list
);
700 mutex_unlock(&dsp
->mutex
);
703 EXPORT_SYMBOL_GPL(sst_free_blocks
);
705 /* allocate memory blocks for static module addresses - callers hold locks */
706 static int block_alloc_fixed(struct sst_dsp
*dsp
, struct sst_block_allocator
*ba
,
707 struct list_head
*block_list
)
709 struct sst_mem_block
*block
, *tmp
;
710 struct sst_block_allocator ba_tmp
= *ba
;
711 u32 end
= ba
->offset
+ ba
->size
, block_end
;
714 /* only IRAM/DRAM blocks are managed */
715 if (ba
->type
!= SST_MEM_IRAM
&& ba
->type
!= SST_MEM_DRAM
)
718 /* are blocks already attached to this module */
719 list_for_each_entry_safe(block
, tmp
, block_list
, module_list
) {
721 /* ignore blocks with wrong type */
722 if (block
->type
!= ba
->type
)
725 block_end
= block
->offset
+ block
->size
;
727 /* find block that holds section */
728 if (ba
->offset
>= block
->offset
&& end
<= block_end
)
731 /* does block span more than 1 section */
732 if (ba
->offset
>= block
->offset
&& ba
->offset
< block_end
) {
734 /* align ba to block boundary */
735 ba_tmp
.size
-= block_end
- ba
->offset
;
736 ba_tmp
.offset
= block_end
;
737 err
= block_alloc_contiguous(dsp
, &ba_tmp
, block_list
);
741 /* module already owns blocks */
746 /* find first free blocks that can hold section in free list */
747 list_for_each_entry_safe(block
, tmp
, &dsp
->free_block_list
, list
) {
748 block_end
= block
->offset
+ block
->size
;
750 /* ignore blocks with wrong type */
751 if (block
->type
!= ba
->type
)
754 /* find block that holds section */
755 if (ba
->offset
>= block
->offset
&& end
<= block_end
) {
758 list_move(&block
->list
, &dsp
->used_block_list
);
759 list_add(&block
->module_list
, block_list
);
760 dev_dbg(dsp
->dev
, "block allocated %d:%d at offset 0x%x\n",
761 block
->type
, block
->index
, block
->offset
);
765 /* does block span more than 1 section */
766 if (ba
->offset
>= block
->offset
&& ba
->offset
< block_end
) {
769 list_move(&block
->list
, &dsp
->used_block_list
);
770 list_add(&block
->module_list
, block_list
);
771 /* align ba to block boundary */
772 ba_tmp
.size
-= block_end
- ba
->offset
;
773 ba_tmp
.offset
= block_end
;
775 err
= block_alloc_contiguous(dsp
, &ba_tmp
, block_list
);
786 /* Load fixed module data into DSP memory blocks */
787 int sst_module_alloc_blocks(struct sst_module
*module
)
789 struct sst_dsp
*dsp
= module
->dsp
;
790 struct sst_fw
*sst_fw
= module
->sst_fw
;
791 struct sst_block_allocator ba
;
794 memset(&ba
, 0, sizeof(ba
));
795 ba
.size
= module
->size
;
796 ba
.type
= module
->type
;
797 ba
.offset
= module
->offset
;
799 dev_dbg(dsp
->dev
, "block request 0x%x bytes at offset 0x%x type %d\n",
800 ba
.size
, ba
.offset
, ba
.type
);
802 mutex_lock(&dsp
->mutex
);
804 /* alloc blocks that includes this section */
805 ret
= block_alloc_fixed(dsp
, &ba
, &module
->block_list
);
808 "error: no free blocks for section at offset 0x%x size 0x%x\n",
809 module
->offset
, module
->size
);
810 mutex_unlock(&dsp
->mutex
);
814 /* prepare DSP blocks for module copy */
815 ret
= block_list_prepare(dsp
, &module
->block_list
);
817 dev_err(dsp
->dev
, "error: fw module prepare failed\n");
821 /* copy partial module data to blocks */
822 if (dsp
->fw_use_dma
) {
823 ret
= sst_dsp_dma_copyto(dsp
,
824 dsp
->addr
.lpe_base
+ module
->offset
,
825 sst_fw
->dmable_fw_paddr
+ module
->data_offset
,
828 dev_err(dsp
->dev
, "error: module copy failed\n");
832 sst_memcpy32(dsp
->addr
.lpe
+ module
->offset
, module
->data
,
835 mutex_unlock(&dsp
->mutex
);
839 block_list_remove(dsp
, &module
->block_list
);
840 mutex_unlock(&dsp
->mutex
);
843 EXPORT_SYMBOL_GPL(sst_module_alloc_blocks
);
845 /* Unload entire module from DSP memory */
846 int sst_module_free_blocks(struct sst_module
*module
)
848 struct sst_dsp
*dsp
= module
->dsp
;
850 mutex_lock(&dsp
->mutex
);
851 block_list_remove(dsp
, &module
->block_list
);
852 mutex_unlock(&dsp
->mutex
);
855 EXPORT_SYMBOL_GPL(sst_module_free_blocks
);
857 int sst_module_runtime_alloc_blocks(struct sst_module_runtime
*runtime
,
860 struct sst_dsp
*dsp
= runtime
->dsp
;
861 struct sst_module
*module
= runtime
->module
;
862 struct sst_block_allocator ba
;
865 if (module
->persistent_size
== 0)
868 memset(&ba
, 0, sizeof(ba
));
869 ba
.size
= module
->persistent_size
;
870 ba
.type
= SST_MEM_DRAM
;
872 mutex_lock(&dsp
->mutex
);
874 /* do we need to allocate at a fixed address ? */
879 dev_dbg(dsp
->dev
, "persistent fixed block request 0x%x bytes type %d offset 0x%x\n",
880 ba
.size
, ba
.type
, ba
.offset
);
882 /* alloc blocks that includes this section */
883 ret
= block_alloc_fixed(dsp
, &ba
, &runtime
->block_list
);
886 dev_dbg(dsp
->dev
, "persistent block request 0x%x bytes type %d\n",
889 /* alloc blocks that includes this section */
890 ret
= block_alloc(dsp
, &ba
, &runtime
->block_list
);
894 "error: no free blocks for runtime module size 0x%x\n",
895 module
->persistent_size
);
896 mutex_unlock(&dsp
->mutex
);
899 runtime
->persistent_offset
= ba
.offset
;
901 /* prepare DSP blocks for module copy */
902 ret
= block_list_prepare(dsp
, &runtime
->block_list
);
904 dev_err(dsp
->dev
, "error: runtime block prepare failed\n");
908 mutex_unlock(&dsp
->mutex
);
912 block_list_remove(dsp
, &module
->block_list
);
913 mutex_unlock(&dsp
->mutex
);
916 EXPORT_SYMBOL_GPL(sst_module_runtime_alloc_blocks
);
918 int sst_module_runtime_free_blocks(struct sst_module_runtime
*runtime
)
920 struct sst_dsp
*dsp
= runtime
->dsp
;
922 mutex_lock(&dsp
->mutex
);
923 block_list_remove(dsp
, &runtime
->block_list
);
924 mutex_unlock(&dsp
->mutex
);
927 EXPORT_SYMBOL_GPL(sst_module_runtime_free_blocks
);
929 int sst_module_runtime_save(struct sst_module_runtime
*runtime
,
930 struct sst_module_runtime_context
*context
)
932 struct sst_dsp
*dsp
= runtime
->dsp
;
933 struct sst_module
*module
= runtime
->module
;
936 dev_dbg(dsp
->dev
, "saving runtime %d memory at 0x%x size 0x%x\n",
937 runtime
->id
, runtime
->persistent_offset
,
938 module
->persistent_size
);
940 context
->buffer
= dma_alloc_coherent(dsp
->dma_dev
,
941 module
->persistent_size
,
942 &context
->dma_buffer
, GFP_DMA
| GFP_KERNEL
);
943 if (!context
->buffer
) {
944 dev_err(dsp
->dev
, "error: DMA context alloc failed\n");
948 mutex_lock(&dsp
->mutex
);
950 if (dsp
->fw_use_dma
) {
952 ret
= sst_dsp_dma_get_channel(dsp
, 0);
956 ret
= sst_dsp_dma_copyfrom(dsp
, context
->dma_buffer
,
957 dsp
->addr
.lpe_base
+ runtime
->persistent_offset
,
958 module
->persistent_size
);
959 sst_dsp_dma_put_channel(dsp
);
961 dev_err(dsp
->dev
, "error: context copy failed\n");
965 sst_memcpy32(context
->buffer
, dsp
->addr
.lpe
+
966 runtime
->persistent_offset
,
967 module
->persistent_size
);
970 mutex_unlock(&dsp
->mutex
);
973 EXPORT_SYMBOL_GPL(sst_module_runtime_save
);
975 int sst_module_runtime_restore(struct sst_module_runtime
*runtime
,
976 struct sst_module_runtime_context
*context
)
978 struct sst_dsp
*dsp
= runtime
->dsp
;
979 struct sst_module
*module
= runtime
->module
;
982 dev_dbg(dsp
->dev
, "restoring runtime %d memory at 0x%x size 0x%x\n",
983 runtime
->id
, runtime
->persistent_offset
,
984 module
->persistent_size
);
986 mutex_lock(&dsp
->mutex
);
988 if (!context
->buffer
) {
989 dev_info(dsp
->dev
, "no context buffer need to restore!\n");
993 if (dsp
->fw_use_dma
) {
995 ret
= sst_dsp_dma_get_channel(dsp
, 0);
999 ret
= sst_dsp_dma_copyto(dsp
,
1000 dsp
->addr
.lpe_base
+ runtime
->persistent_offset
,
1001 context
->dma_buffer
, module
->persistent_size
);
1002 sst_dsp_dma_put_channel(dsp
);
1004 dev_err(dsp
->dev
, "error: module copy failed\n");
1008 sst_memcpy32(dsp
->addr
.lpe
+ runtime
->persistent_offset
,
1009 context
->buffer
, module
->persistent_size
);
1011 dma_free_coherent(dsp
->dma_dev
, module
->persistent_size
,
1012 context
->buffer
, context
->dma_buffer
);
1013 context
->buffer
= NULL
;
1016 mutex_unlock(&dsp
->mutex
);
1019 EXPORT_SYMBOL_GPL(sst_module_runtime_restore
);
1021 /* register a DSP memory block for use with FW based modules */
1022 struct sst_mem_block
*sst_mem_block_register(struct sst_dsp
*dsp
, u32 offset
,
1023 u32 size
, enum sst_mem_type type
, struct sst_block_ops
*ops
, u32 index
,
1026 struct sst_mem_block
*block
;
1028 block
= kzalloc(sizeof(*block
), GFP_KERNEL
);
1032 block
->offset
= offset
;
1034 block
->index
= index
;
1037 block
->private = private;
1040 mutex_lock(&dsp
->mutex
);
1041 list_add(&block
->list
, &dsp
->free_block_list
);
1042 mutex_unlock(&dsp
->mutex
);
1046 EXPORT_SYMBOL_GPL(sst_mem_block_register
);
1048 /* unregister all DSP memory blocks */
1049 void sst_mem_block_unregister_all(struct sst_dsp
*dsp
)
1051 struct sst_mem_block
*block
, *tmp
;
1053 mutex_lock(&dsp
->mutex
);
1055 /* unregister used blocks */
1056 list_for_each_entry_safe(block
, tmp
, &dsp
->used_block_list
, list
) {
1057 list_del(&block
->list
);
1061 /* unregister free blocks */
1062 list_for_each_entry_safe(block
, tmp
, &dsp
->free_block_list
, list
) {
1063 list_del(&block
->list
);
1067 mutex_unlock(&dsp
->mutex
);
1069 EXPORT_SYMBOL_GPL(sst_mem_block_unregister_all
);
1071 /* allocate scratch buffer blocks */
1072 int sst_block_alloc_scratch(struct sst_dsp
*dsp
)
1074 struct sst_module
*module
;
1075 struct sst_block_allocator ba
;
1078 mutex_lock(&dsp
->mutex
);
1080 /* calculate required scratch size */
1081 dsp
->scratch_size
= 0;
1082 list_for_each_entry(module
, &dsp
->module_list
, list
) {
1083 dev_dbg(dsp
->dev
, "module %d scratch req 0x%x bytes\n",
1084 module
->id
, module
->scratch_size
);
1085 if (dsp
->scratch_size
< module
->scratch_size
)
1086 dsp
->scratch_size
= module
->scratch_size
;
1089 dev_dbg(dsp
->dev
, "scratch buffer required is 0x%x bytes\n",
1092 if (dsp
->scratch_size
== 0) {
1093 dev_info(dsp
->dev
, "no modules need scratch buffer\n");
1094 mutex_unlock(&dsp
->mutex
);
1098 /* allocate blocks for module scratch buffers */
1099 dev_dbg(dsp
->dev
, "allocating scratch blocks\n");
1101 ba
.size
= dsp
->scratch_size
;
1102 ba
.type
= SST_MEM_DRAM
;
1104 /* do we need to allocate at fixed offset */
1105 if (dsp
->scratch_offset
!= 0) {
1107 dev_dbg(dsp
->dev
, "block request 0x%x bytes type %d at 0x%x\n",
1108 ba
.size
, ba
.type
, ba
.offset
);
1110 ba
.offset
= dsp
->scratch_offset
;
1112 /* alloc blocks that includes this section */
1113 ret
= block_alloc_fixed(dsp
, &ba
, &dsp
->scratch_block_list
);
1116 dev_dbg(dsp
->dev
, "block request 0x%x bytes type %d\n",
1120 ret
= block_alloc(dsp
, &ba
, &dsp
->scratch_block_list
);
1123 dev_err(dsp
->dev
, "error: can't alloc scratch blocks\n");
1124 mutex_unlock(&dsp
->mutex
);
1128 ret
= block_list_prepare(dsp
, &dsp
->scratch_block_list
);
1130 dev_err(dsp
->dev
, "error: scratch block prepare failed\n");
1131 mutex_unlock(&dsp
->mutex
);
1135 /* assign the same offset of scratch to each module */
1136 dsp
->scratch_offset
= ba
.offset
;
1137 mutex_unlock(&dsp
->mutex
);
1138 return dsp
->scratch_size
;
1140 EXPORT_SYMBOL_GPL(sst_block_alloc_scratch
);
1142 /* free all scratch blocks */
1143 void sst_block_free_scratch(struct sst_dsp
*dsp
)
1145 mutex_lock(&dsp
->mutex
);
1146 block_list_remove(dsp
, &dsp
->scratch_block_list
);
1147 mutex_unlock(&dsp
->mutex
);
1149 EXPORT_SYMBOL_GPL(sst_block_free_scratch
);
1151 /* get a module from it's unique ID */
1152 struct sst_module
*sst_module_get_from_id(struct sst_dsp
*dsp
, u32 id
)
1154 struct sst_module
*module
;
1156 mutex_lock(&dsp
->mutex
);
1158 list_for_each_entry(module
, &dsp
->module_list
, list
) {
1159 if (module
->id
== id
) {
1160 mutex_unlock(&dsp
->mutex
);
1165 mutex_unlock(&dsp
->mutex
);
1168 EXPORT_SYMBOL_GPL(sst_module_get_from_id
);
1170 struct sst_module_runtime
*sst_module_runtime_get_from_id(
1171 struct sst_module
*module
, u32 id
)
1173 struct sst_module_runtime
*runtime
;
1174 struct sst_dsp
*dsp
= module
->dsp
;
1176 mutex_lock(&dsp
->mutex
);
1178 list_for_each_entry(runtime
, &module
->runtime_list
, list
) {
1179 if (runtime
->id
== id
) {
1180 mutex_unlock(&dsp
->mutex
);
1185 mutex_unlock(&dsp
->mutex
);
1188 EXPORT_SYMBOL_GPL(sst_module_runtime_get_from_id
);
1190 /* returns block address in DSP address space */
1191 u32
sst_dsp_get_offset(struct sst_dsp
*dsp
, u32 offset
,
1192 enum sst_mem_type type
)
1196 return offset
- dsp
->addr
.iram_offset
+
1197 dsp
->addr
.dsp_iram_offset
;
1199 return offset
- dsp
->addr
.dram_offset
+
1200 dsp
->addr
.dsp_dram_offset
;
1205 EXPORT_SYMBOL_GPL(sst_dsp_get_offset
);