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 if (sst->pdata->resindex_dma_base == -1)
275 /* DMA is not used, return and squelsh error messages */
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";
284 case SST_DMA_TYPE_MID:
285 dma_dev_name = "Intel MID DMA";
288 dev_err(sst->dev, "error: invalid DMA engine %d\n",
289 sst->pdata->dma_engine);
293 dma = devm_kzalloc(sst->dev, sizeof(struct sst_dma), GFP_KERNEL);
299 memset(&mem, 0, sizeof(mem));
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;
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);
314 sst->fw_use_dma = true;
318 devm_kfree(sst->dev, dma);
321 EXPORT_SYMBOL(sst_dma_new);
323 void sst_dma_free(struct sst_dma *dma)
330 dma_release_channel(dma->ch);
333 dw_remove(dma->chip);
336 EXPORT_SYMBOL(sst_dma_free);
338 /* create new generic firmware object */
339 struct sst_fw *sst_fw_new(struct sst_dsp *dsp,
340 const struct firmware *fw, void *private)
342 struct sst_fw *sst_fw;
345 if (!dsp->ops->parse_fw)
348 sst_fw = kzalloc(sizeof(*sst_fw), GFP_KERNEL);
353 sst_fw->private = private;
354 sst_fw->size = fw->size;
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");
365 /* copy FW data to DMA-able memory */
366 memcpy((void *)sst_fw->dma_buf, (void *)fw->data, fw->size);
368 if (dsp->fw_use_dma) {
369 err = sst_dsp_dma_get_channel(dsp, 0);
374 /* call core specific FW paser to load FW data into DSP */
375 err = dsp->ops->parse_fw(sst_fw);
377 dev_err(dsp->dev, "error: parse fw failed %d\n", err);
382 sst_dsp_dma_put_channel(dsp);
384 mutex_lock(&dsp->mutex);
385 list_add(&sst_fw->list, &dsp->fw_list);
386 mutex_unlock(&dsp->mutex);
392 sst_dsp_dma_put_channel(dsp);
394 dma_free_coherent(dsp->dma_dev, sst_fw->size,
396 sst_fw->dmable_fw_paddr);
397 sst_fw->dma_buf = NULL;
401 EXPORT_SYMBOL_GPL(sst_fw_new);
403 int sst_fw_reload(struct sst_fw *sst_fw)
405 struct sst_dsp *dsp = sst_fw->dsp;
408 dev_dbg(dsp->dev, "reloading firmware\n");
410 /* call core specific FW paser to load FW data into DSP */
411 ret = dsp->ops->parse_fw(sst_fw);
413 dev_err(dsp->dev, "error: parse fw failed %d\n", ret);
417 EXPORT_SYMBOL_GPL(sst_fw_reload);
419 void sst_fw_unload(struct sst_fw *sst_fw)
421 struct sst_dsp *dsp = sst_fw->dsp;
422 struct sst_module *module, *mtmp;
423 struct sst_module_runtime *runtime, *rtmp;
425 dev_dbg(dsp->dev, "unloading firmware\n");
427 mutex_lock(&dsp->mutex);
429 /* check module by module */
430 list_for_each_entry_safe(module, mtmp, &dsp->module_list, list) {
431 if (module->sst_fw == sst_fw) {
433 /* remove runtime modules */
434 list_for_each_entry_safe(runtime, rtmp, &module->runtime_list, list) {
436 block_list_remove(dsp, &runtime->block_list);
437 list_del(&runtime->list);
441 /* now remove the module */
442 block_list_remove(dsp, &module->block_list);
443 list_del(&module->list);
448 /* remove all scratch blocks */
449 block_list_remove(dsp, &dsp->scratch_block_list);
451 mutex_unlock(&dsp->mutex);
453 EXPORT_SYMBOL_GPL(sst_fw_unload);
455 /* free single firmware object */
456 void sst_fw_free(struct sst_fw *sst_fw)
458 struct sst_dsp *dsp = sst_fw->dsp;
460 mutex_lock(&dsp->mutex);
461 list_del(&sst_fw->list);
462 mutex_unlock(&dsp->mutex);
465 dma_free_coherent(dsp->dma_dev, sst_fw->size, sst_fw->dma_buf,
466 sst_fw->dmable_fw_paddr);
469 EXPORT_SYMBOL_GPL(sst_fw_free);
471 /* free all firmware objects */
472 void sst_fw_free_all(struct sst_dsp *dsp)
474 struct sst_fw *sst_fw, *t;
476 mutex_lock(&dsp->mutex);
477 list_for_each_entry_safe(sst_fw, t, &dsp->fw_list, list) {
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);
484 mutex_unlock(&dsp->mutex);
486 EXPORT_SYMBOL_GPL(sst_fw_free_all);
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)
492 struct sst_dsp *dsp = sst_fw->dsp;
493 struct sst_module *sst_module;
495 sst_module = kzalloc(sizeof(*sst_module), GFP_KERNEL);
496 if (sst_module == NULL)
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;
505 INIT_LIST_HEAD(&sst_module->block_list);
506 INIT_LIST_HEAD(&sst_module->runtime_list);
508 mutex_lock(&dsp->mutex);
509 list_add(&sst_module->list, &dsp->module_list);
510 mutex_unlock(&dsp->mutex);
514 EXPORT_SYMBOL_GPL(sst_module_new);
516 /* free firmware module and remove from available list */
517 void sst_module_free(struct sst_module *sst_module)
519 struct sst_dsp *dsp = sst_module->dsp;
521 mutex_lock(&dsp->mutex);
522 list_del(&sst_module->list);
523 mutex_unlock(&dsp->mutex);
527 EXPORT_SYMBOL_GPL(sst_module_free);
529 struct sst_module_runtime *sst_module_runtime_new(struct sst_module *module,
530 int id, void *private)
532 struct sst_dsp *dsp = module->dsp;
533 struct sst_module_runtime *runtime;
535 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
541 runtime->module = module;
542 INIT_LIST_HEAD(&runtime->block_list);
544 mutex_lock(&dsp->mutex);
545 list_add(&runtime->list, &module->runtime_list);
546 mutex_unlock(&dsp->mutex);
550 EXPORT_SYMBOL_GPL(sst_module_runtime_new);
552 void sst_module_runtime_free(struct sst_module_runtime *runtime)
554 struct sst_dsp *dsp = runtime->dsp;
556 mutex_lock(&dsp->mutex);
557 list_del(&runtime->list);
558 mutex_unlock(&dsp->mutex);
562 EXPORT_SYMBOL_GPL(sst_module_runtime_free);
564 static struct sst_mem_block *find_block(struct sst_dsp *dsp,
565 struct sst_block_allocator *ba)
567 struct sst_mem_block *block;
569 list_for_each_entry(block, &dsp->free_block_list, list) {
570 if (block->type == ba->type && block->offset == ba->offset)
577 /* Block allocator must be on block boundary */
578 static int block_alloc_contiguous(struct sst_dsp *dsp,
579 struct sst_block_allocator *ba, struct list_head *block_list)
581 struct list_head tmp = LIST_HEAD_INIT(tmp);
582 struct sst_mem_block *block;
583 u32 block_start = SST_HSW_BLOCK_ANY;
584 int size = ba->size, offset = ba->offset;
586 while (ba->size > 0) {
588 block = find_block(dsp, ba);
590 list_splice(&tmp, &dsp->free_block_list);
597 list_move_tail(&block->list, &tmp);
598 ba->offset += block->size;
599 ba->size -= block->size;
604 list_for_each_entry(block, &tmp, list) {
606 if (block->offset < block_start)
607 block_start = block->offset;
609 list_add(&block->module_list, block_list);
611 dev_dbg(dsp->dev, "block allocated %d:%d at offset 0x%x\n",
612 block->type, block->index, block->offset);
615 list_splice(&tmp, &dsp->used_block_list);
619 /* allocate first free DSP blocks for data - callers hold locks */
620 static int block_alloc(struct sst_dsp *dsp, struct sst_block_allocator *ba,
621 struct list_head *block_list)
623 struct sst_mem_block *block, *tmp;
629 /* find first free whole blocks that can hold module */
630 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
632 /* ignore blocks with wrong type */
633 if (block->type != ba->type)
636 if (ba->size > block->size)
639 ba->offset = block->offset;
640 block->bytes_used = ba->size % block->size;
641 list_add(&block->module_list, block_list);
642 list_move(&block->list, &dsp->used_block_list);
643 dev_dbg(dsp->dev, "block allocated %d:%d at offset 0x%x\n",
644 block->type, block->index, block->offset);
648 /* then find free multiple blocks that can hold module */
649 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
651 /* ignore blocks with wrong type */
652 if (block->type != ba->type)
655 /* do we span > 1 blocks */
656 if (ba->size > block->size) {
658 /* align ba to block boundary */
659 ba->offset = block->offset;
661 ret = block_alloc_contiguous(dsp, ba, block_list);
668 /* not enough free block space */
672 int sst_alloc_blocks(struct sst_dsp *dsp, struct sst_block_allocator *ba,
673 struct list_head *block_list)
677 dev_dbg(dsp->dev, "block request 0x%x bytes at offset 0x%x type %d\n",
678 ba->size, ba->offset, ba->type);
680 mutex_lock(&dsp->mutex);
682 ret = block_alloc(dsp, ba, block_list);
684 dev_err(dsp->dev, "error: can't alloc blocks %d\n", ret);
688 /* prepare DSP blocks for module usage */
689 ret = block_list_prepare(dsp, block_list);
691 dev_err(dsp->dev, "error: prepare failed\n");
694 mutex_unlock(&dsp->mutex);
697 EXPORT_SYMBOL_GPL(sst_alloc_blocks);
699 int sst_free_blocks(struct sst_dsp *dsp, struct list_head *block_list)
701 mutex_lock(&dsp->mutex);
702 block_list_remove(dsp, block_list);
703 mutex_unlock(&dsp->mutex);
706 EXPORT_SYMBOL_GPL(sst_free_blocks);
708 /* allocate memory blocks for static module addresses - callers hold locks */
709 static int block_alloc_fixed(struct sst_dsp *dsp, struct sst_block_allocator *ba,
710 struct list_head *block_list)
712 struct sst_mem_block *block, *tmp;
713 struct sst_block_allocator ba_tmp = *ba;
714 u32 end = ba->offset + ba->size, block_end;
717 /* only IRAM/DRAM blocks are managed */
718 if (ba->type != SST_MEM_IRAM && ba->type != SST_MEM_DRAM)
721 /* are blocks already attached to this module */
722 list_for_each_entry_safe(block, tmp, block_list, module_list) {
724 /* ignore blocks with wrong type */
725 if (block->type != ba->type)
728 block_end = block->offset + block->size;
730 /* find block that holds section */
731 if (ba->offset >= block->offset && end <= block_end)
734 /* does block span more than 1 section */
735 if (ba->offset >= block->offset && ba->offset < block_end) {
737 /* align ba to block boundary */
738 ba_tmp.size -= block_end - ba->offset;
739 ba_tmp.offset = block_end;
740 err = block_alloc_contiguous(dsp, &ba_tmp, block_list);
744 /* module already owns blocks */
749 /* find first free blocks that can hold section in free list */
750 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
751 block_end = block->offset + block->size;
753 /* ignore blocks with wrong type */
754 if (block->type != ba->type)
757 /* find block that holds section */
758 if (ba->offset >= block->offset && end <= block_end) {
761 list_move(&block->list, &dsp->used_block_list);
762 list_add(&block->module_list, block_list);
763 dev_dbg(dsp->dev, "block allocated %d:%d at offset 0x%x\n",
764 block->type, block->index, block->offset);
768 /* does block span more than 1 section */
769 if (ba->offset >= block->offset && ba->offset < block_end) {
772 list_move(&block->list, &dsp->used_block_list);
773 list_add(&block->module_list, block_list);
774 /* align ba to block boundary */
775 ba_tmp.size -= block_end - ba->offset;
776 ba_tmp.offset = block_end;
778 err = block_alloc_contiguous(dsp, &ba_tmp, block_list);
789 /* Load fixed module data into DSP memory blocks */
790 int sst_module_alloc_blocks(struct sst_module *module)
792 struct sst_dsp *dsp = module->dsp;
793 struct sst_fw *sst_fw = module->sst_fw;
794 struct sst_block_allocator ba;
797 ba.size = module->size;
798 ba.type = module->type;
799 ba.offset = module->offset;
801 dev_dbg(dsp->dev, "block request 0x%x bytes at offset 0x%x type %d\n",
802 ba.size, ba.offset, ba.type);
804 mutex_lock(&dsp->mutex);
806 /* alloc blocks that includes this section */
807 ret = block_alloc_fixed(dsp, &ba, &module->block_list);
810 "error: no free blocks for section at offset 0x%x size 0x%x\n",
811 module->offset, module->size);
812 mutex_unlock(&dsp->mutex);
816 /* prepare DSP blocks for module copy */
817 ret = block_list_prepare(dsp, &module->block_list);
819 dev_err(dsp->dev, "error: fw module prepare failed\n");
823 /* copy partial module data to blocks */
824 if (dsp->fw_use_dma) {
825 ret = sst_dsp_dma_copyto(dsp,
826 dsp->addr.lpe_base + module->offset,
827 sst_fw->dmable_fw_paddr + module->data_offset,
830 dev_err(dsp->dev, "error: module copy failed\n");
834 sst_memcpy32(dsp->addr.lpe + module->offset, module->data,
837 mutex_unlock(&dsp->mutex);
841 block_list_remove(dsp, &module->block_list);
842 mutex_unlock(&dsp->mutex);
845 EXPORT_SYMBOL_GPL(sst_module_alloc_blocks);
847 /* Unload entire module from DSP memory */
848 int sst_module_free_blocks(struct sst_module *module)
850 struct sst_dsp *dsp = module->dsp;
852 mutex_lock(&dsp->mutex);
853 block_list_remove(dsp, &module->block_list);
854 mutex_unlock(&dsp->mutex);
857 EXPORT_SYMBOL_GPL(sst_module_free_blocks);
859 int sst_module_runtime_alloc_blocks(struct sst_module_runtime *runtime,
862 struct sst_dsp *dsp = runtime->dsp;
863 struct sst_module *module = runtime->module;
864 struct sst_block_allocator ba;
867 if (module->persistent_size == 0)
870 ba.size = module->persistent_size;
871 ba.type = SST_MEM_DRAM;
873 mutex_lock(&dsp->mutex);
875 /* do we need to allocate at a fixed address ? */
880 dev_dbg(dsp->dev, "persistent fixed block request 0x%x bytes type %d offset 0x%x\n",
881 ba.size, ba.type, ba.offset);
883 /* alloc blocks that includes this section */
884 ret = block_alloc_fixed(dsp, &ba, &runtime->block_list);
887 dev_dbg(dsp->dev, "persistent block request 0x%x bytes type %d\n",
890 /* alloc blocks that includes this section */
891 ret = block_alloc(dsp, &ba, &runtime->block_list);
895 "error: no free blocks for runtime module size 0x%x\n",
896 module->persistent_size);
897 mutex_unlock(&dsp->mutex);
900 runtime->persistent_offset = ba.offset;
902 /* prepare DSP blocks for module copy */
903 ret = block_list_prepare(dsp, &runtime->block_list);
905 dev_err(dsp->dev, "error: runtime block prepare failed\n");
909 mutex_unlock(&dsp->mutex);
913 block_list_remove(dsp, &module->block_list);
914 mutex_unlock(&dsp->mutex);
917 EXPORT_SYMBOL_GPL(sst_module_runtime_alloc_blocks);
919 int sst_module_runtime_free_blocks(struct sst_module_runtime *runtime)
921 struct sst_dsp *dsp = runtime->dsp;
923 mutex_lock(&dsp->mutex);
924 block_list_remove(dsp, &runtime->block_list);
925 mutex_unlock(&dsp->mutex);
928 EXPORT_SYMBOL_GPL(sst_module_runtime_free_blocks);
930 int sst_module_runtime_save(struct sst_module_runtime *runtime,
931 struct sst_module_runtime_context *context)
933 struct sst_dsp *dsp = runtime->dsp;
934 struct sst_module *module = runtime->module;
937 dev_dbg(dsp->dev, "saving runtime %d memory at 0x%x size 0x%x\n",
938 runtime->id, runtime->persistent_offset,
939 module->persistent_size);
941 context->buffer = dma_alloc_coherent(dsp->dma_dev,
942 module->persistent_size,
943 &context->dma_buffer, GFP_DMA | GFP_KERNEL);
944 if (!context->buffer) {
945 dev_err(dsp->dev, "error: DMA context alloc failed\n");
949 mutex_lock(&dsp->mutex);
951 if (dsp->fw_use_dma) {
953 ret = sst_dsp_dma_get_channel(dsp, 0);
957 ret = sst_dsp_dma_copyfrom(dsp, context->dma_buffer,
958 dsp->addr.lpe_base + runtime->persistent_offset,
959 module->persistent_size);
960 sst_dsp_dma_put_channel(dsp);
962 dev_err(dsp->dev, "error: context copy failed\n");
966 sst_memcpy32(context->buffer, dsp->addr.lpe +
967 runtime->persistent_offset,
968 module->persistent_size);
971 mutex_unlock(&dsp->mutex);
974 EXPORT_SYMBOL_GPL(sst_module_runtime_save);
976 int sst_module_runtime_restore(struct sst_module_runtime *runtime,
977 struct sst_module_runtime_context *context)
979 struct sst_dsp *dsp = runtime->dsp;
980 struct sst_module *module = runtime->module;
983 dev_dbg(dsp->dev, "restoring runtime %d memory at 0x%x size 0x%x\n",
984 runtime->id, runtime->persistent_offset,
985 module->persistent_size);
987 mutex_lock(&dsp->mutex);
989 if (!context->buffer) {
990 dev_info(dsp->dev, "no context buffer need to restore!\n");
994 if (dsp->fw_use_dma) {
996 ret = sst_dsp_dma_get_channel(dsp, 0);
1000 ret = sst_dsp_dma_copyto(dsp,
1001 dsp->addr.lpe_base + runtime->persistent_offset,
1002 context->dma_buffer, module->persistent_size);
1003 sst_dsp_dma_put_channel(dsp);
1005 dev_err(dsp->dev, "error: module copy failed\n");
1009 sst_memcpy32(dsp->addr.lpe + runtime->persistent_offset,
1010 context->buffer, module->persistent_size);
1012 dma_free_coherent(dsp->dma_dev, module->persistent_size,
1013 context->buffer, context->dma_buffer);
1014 context->buffer = NULL;
1017 mutex_unlock(&dsp->mutex);
1020 EXPORT_SYMBOL_GPL(sst_module_runtime_restore);
1022 /* register a DSP memory block for use with FW based modules */
1023 struct sst_mem_block *sst_mem_block_register(struct sst_dsp *dsp, u32 offset,
1024 u32 size, enum sst_mem_type type, struct sst_block_ops *ops, u32 index,
1027 struct sst_mem_block *block;
1029 block = kzalloc(sizeof(*block), GFP_KERNEL);
1033 block->offset = offset;
1035 block->index = index;
1038 block->private = private;
1041 mutex_lock(&dsp->mutex);
1042 list_add(&block->list, &dsp->free_block_list);
1043 mutex_unlock(&dsp->mutex);
1047 EXPORT_SYMBOL_GPL(sst_mem_block_register);
1049 /* unregister all DSP memory blocks */
1050 void sst_mem_block_unregister_all(struct sst_dsp *dsp)
1052 struct sst_mem_block *block, *tmp;
1054 mutex_lock(&dsp->mutex);
1056 /* unregister used blocks */
1057 list_for_each_entry_safe(block, tmp, &dsp->used_block_list, list) {
1058 list_del(&block->list);
1062 /* unregister free blocks */
1063 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
1064 list_del(&block->list);
1068 mutex_unlock(&dsp->mutex);
1070 EXPORT_SYMBOL_GPL(sst_mem_block_unregister_all);
1072 /* allocate scratch buffer blocks */
1073 int sst_block_alloc_scratch(struct sst_dsp *dsp)
1075 struct sst_module *module;
1076 struct sst_block_allocator ba;
1079 mutex_lock(&dsp->mutex);
1081 /* calculate required scratch size */
1082 dsp->scratch_size = 0;
1083 list_for_each_entry(module, &dsp->module_list, list) {
1084 dev_dbg(dsp->dev, "module %d scratch req 0x%x bytes\n",
1085 module->id, module->scratch_size);
1086 if (dsp->scratch_size < module->scratch_size)
1087 dsp->scratch_size = module->scratch_size;
1090 dev_dbg(dsp->dev, "scratch buffer required is 0x%x bytes\n",
1093 if (dsp->scratch_size == 0) {
1094 dev_info(dsp->dev, "no modules need scratch buffer\n");
1095 mutex_unlock(&dsp->mutex);
1099 /* allocate blocks for module scratch buffers */
1100 dev_dbg(dsp->dev, "allocating scratch blocks\n");
1102 ba.size = dsp->scratch_size;
1103 ba.type = SST_MEM_DRAM;
1105 /* do we need to allocate at fixed offset */
1106 if (dsp->scratch_offset != 0) {
1108 dev_dbg(dsp->dev, "block request 0x%x bytes type %d at 0x%x\n",
1109 ba.size, ba.type, ba.offset);
1111 ba.offset = dsp->scratch_offset;
1113 /* alloc blocks that includes this section */
1114 ret = block_alloc_fixed(dsp, &ba, &dsp->scratch_block_list);
1117 dev_dbg(dsp->dev, "block request 0x%x bytes type %d\n",
1121 ret = block_alloc(dsp, &ba, &dsp->scratch_block_list);
1124 dev_err(dsp->dev, "error: can't alloc scratch blocks\n");
1125 mutex_unlock(&dsp->mutex);
1129 ret = block_list_prepare(dsp, &dsp->scratch_block_list);
1131 dev_err(dsp->dev, "error: scratch block prepare failed\n");
1132 mutex_unlock(&dsp->mutex);
1136 /* assign the same offset of scratch to each module */
1137 dsp->scratch_offset = ba.offset;
1138 mutex_unlock(&dsp->mutex);
1139 return dsp->scratch_size;
1141 EXPORT_SYMBOL_GPL(sst_block_alloc_scratch);
1143 /* free all scratch blocks */
1144 void sst_block_free_scratch(struct sst_dsp *dsp)
1146 mutex_lock(&dsp->mutex);
1147 block_list_remove(dsp, &dsp->scratch_block_list);
1148 mutex_unlock(&dsp->mutex);
1150 EXPORT_SYMBOL_GPL(sst_block_free_scratch);
1152 /* get a module from it's unique ID */
1153 struct sst_module *sst_module_get_from_id(struct sst_dsp *dsp, u32 id)
1155 struct sst_module *module;
1157 mutex_lock(&dsp->mutex);
1159 list_for_each_entry(module, &dsp->module_list, list) {
1160 if (module->id == id) {
1161 mutex_unlock(&dsp->mutex);
1166 mutex_unlock(&dsp->mutex);
1169 EXPORT_SYMBOL_GPL(sst_module_get_from_id);
1171 struct sst_module_runtime *sst_module_runtime_get_from_id(
1172 struct sst_module *module, u32 id)
1174 struct sst_module_runtime *runtime;
1175 struct sst_dsp *dsp = module->dsp;
1177 mutex_lock(&dsp->mutex);
1179 list_for_each_entry(runtime, &module->runtime_list, list) {
1180 if (runtime->id == id) {
1181 mutex_unlock(&dsp->mutex);
1186 mutex_unlock(&dsp->mutex);
1189 EXPORT_SYMBOL_GPL(sst_module_runtime_get_from_id);
1191 /* returns block address in DSP address space */
1192 u32 sst_dsp_get_offset(struct sst_dsp *dsp, u32 offset,
1193 enum sst_mem_type type)
1197 return offset - dsp->addr.iram_offset +
1198 dsp->addr.dsp_iram_offset;
1200 return offset - dsp->addr.dram_offset +
1201 dsp->addr.dsp_dram_offset;
1206 EXPORT_SYMBOL_GPL(sst_dsp_get_offset);