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/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/dmaengine.h>
26 #include <linux/pci.h>
27 #include <linux/acpi.h>
29 /* supported DMA engine drivers */
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)
57 const u8 *src_byte = src;
62 /* __iowrite32_copy use 32bit size values so divide by 4 */
63 __iowrite32_copy((void *)dest, src, m);
66 for (i = 0; i < n; i++)
67 tmp |= (u32)*(src_byte + m * 4 + i) << (i * 8);
68 __iowrite32_copy((void *)(dest + m * 4), &tmp, 1);
73 static void sst_dma_transfer_complete(void *arg)
75 struct sst_dsp *sst = (struct sst_dsp *)arg;
77 dev_dbg(sst->dev, "DMA: callback\n");
80 static int sst_dsp_dma_copy(struct sst_dsp *sst, dma_addr_t dest_addr,
81 dma_addr_t src_addr, size_t size)
83 struct dma_async_tx_descriptor *desc;
84 struct sst_dma *dma = sst->dma;
86 if (dma->ch == NULL) {
87 dev_err(sst->dev, "error: no DMA channel\n");
91 dev_dbg(sst->dev, "DMA: src: 0x%lx dest 0x%lx size %zu\n",
92 (unsigned long)src_addr, (unsigned long)dest_addr, size);
94 desc = dma->ch->device->device_prep_dma_memcpy(dma->ch, dest_addr,
95 src_addr, size, DMA_CTRL_ACK);
97 dev_err(sst->dev, "error: dma prep memcpy failed\n");
101 desc->callback = sst_dma_transfer_complete;
102 desc->callback_param = sst;
104 desc->tx_submit(desc);
105 dma_wait_for_async_tx(desc);
111 int sst_dsp_dma_copyto(struct sst_dsp *sst, dma_addr_t dest_addr,
112 dma_addr_t src_addr, size_t size)
114 return sst_dsp_dma_copy(sst, dest_addr | SST_HSW_MASK_DMA_ADDR_DSP,
117 EXPORT_SYMBOL_GPL(sst_dsp_dma_copyto);
120 int sst_dsp_dma_copyfrom(struct sst_dsp *sst, dma_addr_t dest_addr,
121 dma_addr_t src_addr, size_t size)
123 return sst_dsp_dma_copy(sst, dest_addr,
124 src_addr | SST_HSW_MASK_DMA_ADDR_DSP, size);
126 EXPORT_SYMBOL_GPL(sst_dsp_dma_copyfrom);
128 /* remove module from memory - callers hold locks */
129 static void block_list_remove(struct sst_dsp *dsp,
130 struct list_head *block_list)
132 struct sst_mem_block *block, *tmp;
135 /* disable each block */
136 list_for_each_entry(block, block_list, module_list) {
138 if (block->ops && block->ops->disable) {
139 err = block->ops->disable(block);
142 "error: cant disable block %d:%d\n",
143 block->type, block->index);
147 /* mark each block as free */
148 list_for_each_entry_safe(block, tmp, block_list, module_list) {
149 list_del(&block->module_list);
150 list_move(&block->list, &dsp->free_block_list);
151 dev_dbg(dsp->dev, "block freed %d:%d at offset 0x%x\n",
152 block->type, block->index, block->offset);
156 /* prepare the memory block to receive data from host - callers hold locks */
157 static int block_list_prepare(struct sst_dsp *dsp,
158 struct list_head *block_list)
160 struct sst_mem_block *block;
163 /* enable each block so that's it'e ready for data */
164 list_for_each_entry(block, block_list, module_list) {
166 if (block->ops && block->ops->enable && !block->users) {
167 ret = block->ops->enable(block);
170 "error: cant disable block %d:%d\n",
171 block->type, block->index);
179 list_for_each_entry(block, block_list, module_list) {
180 if (block->ops && block->ops->disable)
181 block->ops->disable(block);
186 static struct dw_dma_chip *dw_probe(struct device *dev, struct resource *mem,
189 struct dw_dma_chip *chip;
192 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
194 return ERR_PTR(-ENOMEM);
197 chip->regs = devm_ioremap_resource(dev, mem);
198 if (IS_ERR(chip->regs))
199 return ERR_CAST(chip->regs);
201 err = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(31));
207 err = dw_dma_probe(chip);
214 static void dw_remove(struct dw_dma_chip *chip)
219 static bool dma_chan_filter(struct dma_chan *chan, void *param)
221 struct sst_dsp *dsp = (struct sst_dsp *)param;
223 return chan->device->dev == dsp->dma_dev;
226 int sst_dsp_dma_get_channel(struct sst_dsp *dsp, int chan_id)
228 struct sst_dma *dma = dsp->dma;
229 struct dma_slave_config slave;
234 dma_cap_set(DMA_SLAVE, mask);
235 dma_cap_set(DMA_MEMCPY, mask);
237 dma->ch = dma_request_channel(mask, dma_chan_filter, dsp);
238 if (dma->ch == NULL) {
239 dev_err(dsp->dev, "error: DMA request channel failed\n");
243 memset(&slave, 0, sizeof(slave));
244 slave.direction = DMA_MEM_TO_DEV;
245 slave.src_addr_width =
246 slave.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
247 slave.src_maxburst = slave.dst_maxburst = SST_DSP_DMA_MAX_BURST;
249 ret = dmaengine_slave_config(dma->ch, &slave);
251 dev_err(dsp->dev, "error: unable to set DMA slave config %d\n",
253 dma_release_channel(dma->ch);
259 EXPORT_SYMBOL_GPL(sst_dsp_dma_get_channel);
261 void sst_dsp_dma_put_channel(struct sst_dsp *dsp)
263 struct sst_dma *dma = dsp->dma;
268 dma_release_channel(dma->ch);
271 EXPORT_SYMBOL_GPL(sst_dsp_dma_put_channel);
273 int sst_dma_new(struct sst_dsp *sst)
275 struct sst_pdata *sst_pdata = sst->pdata;
280 if (sst->pdata->resindex_dma_base == -1)
281 /* DMA is not used, return and squelsh error messages */
284 /* configure the correct platform data for whatever DMA engine
285 * is attached to the ADSP IP. */
286 switch (sst->pdata->dma_engine) {
287 case SST_DMA_TYPE_DW:
290 dev_err(sst->dev, "error: invalid DMA engine %d\n",
291 sst->pdata->dma_engine);
295 dma = devm_kzalloc(sst->dev, sizeof(struct sst_dma), GFP_KERNEL);
301 memset(&mem, 0, sizeof(mem));
303 mem.start = sst->addr.lpe_base + sst_pdata->dma_base;
304 mem.end = sst->addr.lpe_base + sst_pdata->dma_base + sst_pdata->dma_size - 1;
305 mem.flags = IORESOURCE_MEM;
307 /* now register DMA engine device */
308 dma->chip = dw_probe(sst->dma_dev, &mem, sst_pdata->irq);
309 if (IS_ERR(dma->chip)) {
310 dev_err(sst->dev, "error: DMA device register failed\n");
311 ret = PTR_ERR(dma->chip);
316 sst->fw_use_dma = true;
320 devm_kfree(sst->dev, dma);
323 EXPORT_SYMBOL(sst_dma_new);
325 void sst_dma_free(struct sst_dma *dma)
332 dma_release_channel(dma->ch);
335 dw_remove(dma->chip);
338 EXPORT_SYMBOL(sst_dma_free);
340 /* create new generic firmware object */
341 struct sst_fw *sst_fw_new(struct sst_dsp *dsp,
342 const struct firmware *fw, void *private)
344 struct sst_fw *sst_fw;
347 if (!dsp->ops->parse_fw)
350 sst_fw = kzalloc(sizeof(*sst_fw), GFP_KERNEL);
355 sst_fw->private = private;
356 sst_fw->size = fw->size;
358 /* allocate DMA buffer to store FW data */
359 sst_fw->dma_buf = dma_alloc_coherent(dsp->dma_dev, sst_fw->size,
360 &sst_fw->dmable_fw_paddr, GFP_DMA | GFP_KERNEL);
361 if (!sst_fw->dma_buf) {
362 dev_err(dsp->dev, "error: DMA alloc failed\n");
367 /* copy FW data to DMA-able memory */
368 memcpy((void *)sst_fw->dma_buf, (void *)fw->data, fw->size);
370 if (dsp->fw_use_dma) {
371 err = sst_dsp_dma_get_channel(dsp, 0);
376 /* call core specific FW paser to load FW data into DSP */
377 err = dsp->ops->parse_fw(sst_fw);
379 dev_err(dsp->dev, "error: parse fw failed %d\n", err);
384 sst_dsp_dma_put_channel(dsp);
386 mutex_lock(&dsp->mutex);
387 list_add(&sst_fw->list, &dsp->fw_list);
388 mutex_unlock(&dsp->mutex);
394 sst_dsp_dma_put_channel(dsp);
396 dma_free_coherent(dsp->dma_dev, sst_fw->size,
398 sst_fw->dmable_fw_paddr);
399 sst_fw->dma_buf = NULL;
403 EXPORT_SYMBOL_GPL(sst_fw_new);
405 int sst_fw_reload(struct sst_fw *sst_fw)
407 struct sst_dsp *dsp = sst_fw->dsp;
410 dev_dbg(dsp->dev, "reloading firmware\n");
412 /* call core specific FW paser to load FW data into DSP */
413 ret = dsp->ops->parse_fw(sst_fw);
415 dev_err(dsp->dev, "error: parse fw failed %d\n", ret);
419 EXPORT_SYMBOL_GPL(sst_fw_reload);
421 void sst_fw_unload(struct sst_fw *sst_fw)
423 struct sst_dsp *dsp = sst_fw->dsp;
424 struct sst_module *module, *mtmp;
425 struct sst_module_runtime *runtime, *rtmp;
427 dev_dbg(dsp->dev, "unloading firmware\n");
429 mutex_lock(&dsp->mutex);
431 /* check module by module */
432 list_for_each_entry_safe(module, mtmp, &dsp->module_list, list) {
433 if (module->sst_fw == sst_fw) {
435 /* remove runtime modules */
436 list_for_each_entry_safe(runtime, rtmp, &module->runtime_list, list) {
438 block_list_remove(dsp, &runtime->block_list);
439 list_del(&runtime->list);
443 /* now remove the module */
444 block_list_remove(dsp, &module->block_list);
445 list_del(&module->list);
450 /* remove all scratch blocks */
451 block_list_remove(dsp, &dsp->scratch_block_list);
453 mutex_unlock(&dsp->mutex);
455 EXPORT_SYMBOL_GPL(sst_fw_unload);
457 /* free single firmware object */
458 void sst_fw_free(struct sst_fw *sst_fw)
460 struct sst_dsp *dsp = sst_fw->dsp;
462 mutex_lock(&dsp->mutex);
463 list_del(&sst_fw->list);
464 mutex_unlock(&dsp->mutex);
467 dma_free_coherent(dsp->dma_dev, sst_fw->size, sst_fw->dma_buf,
468 sst_fw->dmable_fw_paddr);
471 EXPORT_SYMBOL_GPL(sst_fw_free);
473 /* free all firmware objects */
474 void sst_fw_free_all(struct sst_dsp *dsp)
476 struct sst_fw *sst_fw, *t;
478 mutex_lock(&dsp->mutex);
479 list_for_each_entry_safe(sst_fw, t, &dsp->fw_list, list) {
481 list_del(&sst_fw->list);
482 dma_free_coherent(dsp->dev, sst_fw->size, sst_fw->dma_buf,
483 sst_fw->dmable_fw_paddr);
486 mutex_unlock(&dsp->mutex);
488 EXPORT_SYMBOL_GPL(sst_fw_free_all);
490 /* create a new SST generic module from FW template */
491 struct sst_module *sst_module_new(struct sst_fw *sst_fw,
492 struct sst_module_template *template, void *private)
494 struct sst_dsp *dsp = sst_fw->dsp;
495 struct sst_module *sst_module;
497 sst_module = kzalloc(sizeof(*sst_module), GFP_KERNEL);
498 if (sst_module == NULL)
501 sst_module->id = template->id;
502 sst_module->dsp = dsp;
503 sst_module->sst_fw = sst_fw;
504 sst_module->scratch_size = template->scratch_size;
505 sst_module->persistent_size = template->persistent_size;
506 sst_module->entry = template->entry;
507 sst_module->state = SST_MODULE_STATE_UNLOADED;
509 INIT_LIST_HEAD(&sst_module->block_list);
510 INIT_LIST_HEAD(&sst_module->runtime_list);
512 mutex_lock(&dsp->mutex);
513 list_add(&sst_module->list, &dsp->module_list);
514 mutex_unlock(&dsp->mutex);
518 EXPORT_SYMBOL_GPL(sst_module_new);
520 /* free firmware module and remove from available list */
521 void sst_module_free(struct sst_module *sst_module)
523 struct sst_dsp *dsp = sst_module->dsp;
525 mutex_lock(&dsp->mutex);
526 list_del(&sst_module->list);
527 mutex_unlock(&dsp->mutex);
531 EXPORT_SYMBOL_GPL(sst_module_free);
533 struct sst_module_runtime *sst_module_runtime_new(struct sst_module *module,
534 int id, void *private)
536 struct sst_dsp *dsp = module->dsp;
537 struct sst_module_runtime *runtime;
539 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
545 runtime->module = module;
546 INIT_LIST_HEAD(&runtime->block_list);
548 mutex_lock(&dsp->mutex);
549 list_add(&runtime->list, &module->runtime_list);
550 mutex_unlock(&dsp->mutex);
554 EXPORT_SYMBOL_GPL(sst_module_runtime_new);
556 void sst_module_runtime_free(struct sst_module_runtime *runtime)
558 struct sst_dsp *dsp = runtime->dsp;
560 mutex_lock(&dsp->mutex);
561 list_del(&runtime->list);
562 mutex_unlock(&dsp->mutex);
566 EXPORT_SYMBOL_GPL(sst_module_runtime_free);
568 static struct sst_mem_block *find_block(struct sst_dsp *dsp,
569 struct sst_block_allocator *ba)
571 struct sst_mem_block *block;
573 list_for_each_entry(block, &dsp->free_block_list, list) {
574 if (block->type == ba->type && block->offset == ba->offset)
581 /* Block allocator must be on block boundary */
582 static int block_alloc_contiguous(struct sst_dsp *dsp,
583 struct sst_block_allocator *ba, struct list_head *block_list)
585 struct list_head tmp = LIST_HEAD_INIT(tmp);
586 struct sst_mem_block *block;
587 u32 block_start = SST_HSW_BLOCK_ANY;
588 int size = ba->size, offset = ba->offset;
590 while (ba->size > 0) {
592 block = find_block(dsp, ba);
594 list_splice(&tmp, &dsp->free_block_list);
601 list_move_tail(&block->list, &tmp);
602 ba->offset += block->size;
603 ba->size -= block->size;
608 list_for_each_entry(block, &tmp, list) {
610 if (block->offset < block_start)
611 block_start = block->offset;
613 list_add(&block->module_list, block_list);
615 dev_dbg(dsp->dev, "block allocated %d:%d at offset 0x%x\n",
616 block->type, block->index, block->offset);
619 list_splice(&tmp, &dsp->used_block_list);
623 /* allocate first free DSP blocks for data - callers hold locks */
624 static int block_alloc(struct sst_dsp *dsp, struct sst_block_allocator *ba,
625 struct list_head *block_list)
627 struct sst_mem_block *block, *tmp;
633 /* find first free whole blocks that can hold module */
634 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
636 /* ignore blocks with wrong type */
637 if (block->type != ba->type)
640 if (ba->size > block->size)
643 ba->offset = block->offset;
644 block->bytes_used = ba->size % block->size;
645 list_add(&block->module_list, block_list);
646 list_move(&block->list, &dsp->used_block_list);
647 dev_dbg(dsp->dev, "block allocated %d:%d at offset 0x%x\n",
648 block->type, block->index, block->offset);
652 /* then find free multiple blocks that can hold module */
653 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
655 /* ignore blocks with wrong type */
656 if (block->type != ba->type)
659 /* do we span > 1 blocks */
660 if (ba->size > block->size) {
662 /* align ba to block boundary */
663 ba->offset = block->offset;
665 ret = block_alloc_contiguous(dsp, ba, block_list);
672 /* not enough free block space */
676 int sst_alloc_blocks(struct sst_dsp *dsp, struct sst_block_allocator *ba,
677 struct list_head *block_list)
681 dev_dbg(dsp->dev, "block request 0x%x bytes at offset 0x%x type %d\n",
682 ba->size, ba->offset, ba->type);
684 mutex_lock(&dsp->mutex);
686 ret = block_alloc(dsp, ba, block_list);
688 dev_err(dsp->dev, "error: can't alloc blocks %d\n", ret);
692 /* prepare DSP blocks for module usage */
693 ret = block_list_prepare(dsp, block_list);
695 dev_err(dsp->dev, "error: prepare failed\n");
698 mutex_unlock(&dsp->mutex);
701 EXPORT_SYMBOL_GPL(sst_alloc_blocks);
703 int sst_free_blocks(struct sst_dsp *dsp, struct list_head *block_list)
705 mutex_lock(&dsp->mutex);
706 block_list_remove(dsp, block_list);
707 mutex_unlock(&dsp->mutex);
710 EXPORT_SYMBOL_GPL(sst_free_blocks);
712 /* allocate memory blocks for static module addresses - callers hold locks */
713 static int block_alloc_fixed(struct sst_dsp *dsp, struct sst_block_allocator *ba,
714 struct list_head *block_list)
716 struct sst_mem_block *block, *tmp;
717 struct sst_block_allocator ba_tmp = *ba;
718 u32 end = ba->offset + ba->size, block_end;
721 /* only IRAM/DRAM blocks are managed */
722 if (ba->type != SST_MEM_IRAM && ba->type != SST_MEM_DRAM)
725 /* are blocks already attached to this module */
726 list_for_each_entry_safe(block, tmp, block_list, module_list) {
728 /* ignore blocks with wrong type */
729 if (block->type != ba->type)
732 block_end = block->offset + block->size;
734 /* find block that holds section */
735 if (ba->offset >= block->offset && end <= block_end)
738 /* does block span more than 1 section */
739 if (ba->offset >= block->offset && ba->offset < block_end) {
741 /* align ba to block boundary */
742 ba_tmp.size -= block_end - ba->offset;
743 ba_tmp.offset = block_end;
744 err = block_alloc_contiguous(dsp, &ba_tmp, block_list);
748 /* module already owns blocks */
753 /* find first free blocks that can hold section in free list */
754 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
755 block_end = block->offset + block->size;
757 /* ignore blocks with wrong type */
758 if (block->type != ba->type)
761 /* find block that holds section */
762 if (ba->offset >= block->offset && end <= block_end) {
765 list_move(&block->list, &dsp->used_block_list);
766 list_add(&block->module_list, block_list);
767 dev_dbg(dsp->dev, "block allocated %d:%d at offset 0x%x\n",
768 block->type, block->index, block->offset);
772 /* does block span more than 1 section */
773 if (ba->offset >= block->offset && ba->offset < block_end) {
776 list_move(&block->list, &dsp->used_block_list);
777 list_add(&block->module_list, block_list);
778 /* align ba to block boundary */
779 ba_tmp.size -= block_end - ba->offset;
780 ba_tmp.offset = block_end;
782 err = block_alloc_contiguous(dsp, &ba_tmp, block_list);
793 /* Load fixed module data into DSP memory blocks */
794 int sst_module_alloc_blocks(struct sst_module *module)
796 struct sst_dsp *dsp = module->dsp;
797 struct sst_fw *sst_fw = module->sst_fw;
798 struct sst_block_allocator ba;
801 memset(&ba, 0, sizeof(ba));
802 ba.size = module->size;
803 ba.type = module->type;
804 ba.offset = module->offset;
806 dev_dbg(dsp->dev, "block request 0x%x bytes at offset 0x%x type %d\n",
807 ba.size, ba.offset, ba.type);
809 mutex_lock(&dsp->mutex);
811 /* alloc blocks that includes this section */
812 ret = block_alloc_fixed(dsp, &ba, &module->block_list);
815 "error: no free blocks for section at offset 0x%x size 0x%x\n",
816 module->offset, module->size);
817 mutex_unlock(&dsp->mutex);
821 /* prepare DSP blocks for module copy */
822 ret = block_list_prepare(dsp, &module->block_list);
824 dev_err(dsp->dev, "error: fw module prepare failed\n");
828 /* copy partial module data to blocks */
829 if (dsp->fw_use_dma) {
830 ret = sst_dsp_dma_copyto(dsp,
831 dsp->addr.lpe_base + module->offset,
832 sst_fw->dmable_fw_paddr + module->data_offset,
835 dev_err(dsp->dev, "error: module copy failed\n");
839 sst_memcpy32(dsp->addr.lpe + module->offset, module->data,
842 mutex_unlock(&dsp->mutex);
846 block_list_remove(dsp, &module->block_list);
847 mutex_unlock(&dsp->mutex);
850 EXPORT_SYMBOL_GPL(sst_module_alloc_blocks);
852 /* Unload entire module from DSP memory */
853 int sst_module_free_blocks(struct sst_module *module)
855 struct sst_dsp *dsp = module->dsp;
857 mutex_lock(&dsp->mutex);
858 block_list_remove(dsp, &module->block_list);
859 mutex_unlock(&dsp->mutex);
862 EXPORT_SYMBOL_GPL(sst_module_free_blocks);
864 int sst_module_runtime_alloc_blocks(struct sst_module_runtime *runtime,
867 struct sst_dsp *dsp = runtime->dsp;
868 struct sst_module *module = runtime->module;
869 struct sst_block_allocator ba;
872 if (module->persistent_size == 0)
875 memset(&ba, 0, sizeof(ba));
876 ba.size = module->persistent_size;
877 ba.type = SST_MEM_DRAM;
879 mutex_lock(&dsp->mutex);
881 /* do we need to allocate at a fixed address ? */
886 dev_dbg(dsp->dev, "persistent fixed block request 0x%x bytes type %d offset 0x%x\n",
887 ba.size, ba.type, ba.offset);
889 /* alloc blocks that includes this section */
890 ret = block_alloc_fixed(dsp, &ba, &runtime->block_list);
893 dev_dbg(dsp->dev, "persistent block request 0x%x bytes type %d\n",
896 /* alloc blocks that includes this section */
897 ret = block_alloc(dsp, &ba, &runtime->block_list);
901 "error: no free blocks for runtime module size 0x%x\n",
902 module->persistent_size);
903 mutex_unlock(&dsp->mutex);
906 runtime->persistent_offset = ba.offset;
908 /* prepare DSP blocks for module copy */
909 ret = block_list_prepare(dsp, &runtime->block_list);
911 dev_err(dsp->dev, "error: runtime block prepare failed\n");
915 mutex_unlock(&dsp->mutex);
919 block_list_remove(dsp, &module->block_list);
920 mutex_unlock(&dsp->mutex);
923 EXPORT_SYMBOL_GPL(sst_module_runtime_alloc_blocks);
925 int sst_module_runtime_free_blocks(struct sst_module_runtime *runtime)
927 struct sst_dsp *dsp = runtime->dsp;
929 mutex_lock(&dsp->mutex);
930 block_list_remove(dsp, &runtime->block_list);
931 mutex_unlock(&dsp->mutex);
934 EXPORT_SYMBOL_GPL(sst_module_runtime_free_blocks);
936 int sst_module_runtime_save(struct sst_module_runtime *runtime,
937 struct sst_module_runtime_context *context)
939 struct sst_dsp *dsp = runtime->dsp;
940 struct sst_module *module = runtime->module;
943 dev_dbg(dsp->dev, "saving runtime %d memory at 0x%x size 0x%x\n",
944 runtime->id, runtime->persistent_offset,
945 module->persistent_size);
947 context->buffer = dma_alloc_coherent(dsp->dma_dev,
948 module->persistent_size,
949 &context->dma_buffer, GFP_DMA | GFP_KERNEL);
950 if (!context->buffer) {
951 dev_err(dsp->dev, "error: DMA context alloc failed\n");
955 mutex_lock(&dsp->mutex);
957 if (dsp->fw_use_dma) {
959 ret = sst_dsp_dma_get_channel(dsp, 0);
963 ret = sst_dsp_dma_copyfrom(dsp, context->dma_buffer,
964 dsp->addr.lpe_base + runtime->persistent_offset,
965 module->persistent_size);
966 sst_dsp_dma_put_channel(dsp);
968 dev_err(dsp->dev, "error: context copy failed\n");
972 sst_memcpy32(context->buffer, dsp->addr.lpe +
973 runtime->persistent_offset,
974 module->persistent_size);
977 mutex_unlock(&dsp->mutex);
980 EXPORT_SYMBOL_GPL(sst_module_runtime_save);
982 int sst_module_runtime_restore(struct sst_module_runtime *runtime,
983 struct sst_module_runtime_context *context)
985 struct sst_dsp *dsp = runtime->dsp;
986 struct sst_module *module = runtime->module;
989 dev_dbg(dsp->dev, "restoring runtime %d memory at 0x%x size 0x%x\n",
990 runtime->id, runtime->persistent_offset,
991 module->persistent_size);
993 mutex_lock(&dsp->mutex);
995 if (!context->buffer) {
996 dev_info(dsp->dev, "no context buffer need to restore!\n");
1000 if (dsp->fw_use_dma) {
1002 ret = sst_dsp_dma_get_channel(dsp, 0);
1006 ret = sst_dsp_dma_copyto(dsp,
1007 dsp->addr.lpe_base + runtime->persistent_offset,
1008 context->dma_buffer, module->persistent_size);
1009 sst_dsp_dma_put_channel(dsp);
1011 dev_err(dsp->dev, "error: module copy failed\n");
1015 sst_memcpy32(dsp->addr.lpe + runtime->persistent_offset,
1016 context->buffer, module->persistent_size);
1018 dma_free_coherent(dsp->dma_dev, module->persistent_size,
1019 context->buffer, context->dma_buffer);
1020 context->buffer = NULL;
1023 mutex_unlock(&dsp->mutex);
1026 EXPORT_SYMBOL_GPL(sst_module_runtime_restore);
1028 /* register a DSP memory block for use with FW based modules */
1029 struct sst_mem_block *sst_mem_block_register(struct sst_dsp *dsp, u32 offset,
1030 u32 size, enum sst_mem_type type, const struct sst_block_ops *ops,
1031 u32 index, void *private)
1033 struct sst_mem_block *block;
1035 block = kzalloc(sizeof(*block), GFP_KERNEL);
1039 block->offset = offset;
1041 block->index = index;
1044 block->private = private;
1047 mutex_lock(&dsp->mutex);
1048 list_add(&block->list, &dsp->free_block_list);
1049 mutex_unlock(&dsp->mutex);
1053 EXPORT_SYMBOL_GPL(sst_mem_block_register);
1055 /* unregister all DSP memory blocks */
1056 void sst_mem_block_unregister_all(struct sst_dsp *dsp)
1058 struct sst_mem_block *block, *tmp;
1060 mutex_lock(&dsp->mutex);
1062 /* unregister used blocks */
1063 list_for_each_entry_safe(block, tmp, &dsp->used_block_list, list) {
1064 list_del(&block->list);
1068 /* unregister free blocks */
1069 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
1070 list_del(&block->list);
1074 mutex_unlock(&dsp->mutex);
1076 EXPORT_SYMBOL_GPL(sst_mem_block_unregister_all);
1078 /* allocate scratch buffer blocks */
1079 int sst_block_alloc_scratch(struct sst_dsp *dsp)
1081 struct sst_module *module;
1082 struct sst_block_allocator ba;
1085 mutex_lock(&dsp->mutex);
1087 /* calculate required scratch size */
1088 dsp->scratch_size = 0;
1089 list_for_each_entry(module, &dsp->module_list, list) {
1090 dev_dbg(dsp->dev, "module %d scratch req 0x%x bytes\n",
1091 module->id, module->scratch_size);
1092 if (dsp->scratch_size < module->scratch_size)
1093 dsp->scratch_size = module->scratch_size;
1096 dev_dbg(dsp->dev, "scratch buffer required is 0x%x bytes\n",
1099 if (dsp->scratch_size == 0) {
1100 dev_info(dsp->dev, "no modules need scratch buffer\n");
1101 mutex_unlock(&dsp->mutex);
1105 /* allocate blocks for module scratch buffers */
1106 dev_dbg(dsp->dev, "allocating scratch blocks\n");
1108 ba.size = dsp->scratch_size;
1109 ba.type = SST_MEM_DRAM;
1111 /* do we need to allocate at fixed offset */
1112 if (dsp->scratch_offset != 0) {
1114 dev_dbg(dsp->dev, "block request 0x%x bytes type %d at 0x%x\n",
1115 ba.size, ba.type, ba.offset);
1117 ba.offset = dsp->scratch_offset;
1119 /* alloc blocks that includes this section */
1120 ret = block_alloc_fixed(dsp, &ba, &dsp->scratch_block_list);
1123 dev_dbg(dsp->dev, "block request 0x%x bytes type %d\n",
1127 ret = block_alloc(dsp, &ba, &dsp->scratch_block_list);
1130 dev_err(dsp->dev, "error: can't alloc scratch blocks\n");
1131 mutex_unlock(&dsp->mutex);
1135 ret = block_list_prepare(dsp, &dsp->scratch_block_list);
1137 dev_err(dsp->dev, "error: scratch block prepare failed\n");
1138 mutex_unlock(&dsp->mutex);
1142 /* assign the same offset of scratch to each module */
1143 dsp->scratch_offset = ba.offset;
1144 mutex_unlock(&dsp->mutex);
1145 return dsp->scratch_size;
1147 EXPORT_SYMBOL_GPL(sst_block_alloc_scratch);
1149 /* free all scratch blocks */
1150 void sst_block_free_scratch(struct sst_dsp *dsp)
1152 mutex_lock(&dsp->mutex);
1153 block_list_remove(dsp, &dsp->scratch_block_list);
1154 mutex_unlock(&dsp->mutex);
1156 EXPORT_SYMBOL_GPL(sst_block_free_scratch);
1158 /* get a module from it's unique ID */
1159 struct sst_module *sst_module_get_from_id(struct sst_dsp *dsp, u32 id)
1161 struct sst_module *module;
1163 mutex_lock(&dsp->mutex);
1165 list_for_each_entry(module, &dsp->module_list, list) {
1166 if (module->id == id) {
1167 mutex_unlock(&dsp->mutex);
1172 mutex_unlock(&dsp->mutex);
1175 EXPORT_SYMBOL_GPL(sst_module_get_from_id);
1177 struct sst_module_runtime *sst_module_runtime_get_from_id(
1178 struct sst_module *module, u32 id)
1180 struct sst_module_runtime *runtime;
1181 struct sst_dsp *dsp = module->dsp;
1183 mutex_lock(&dsp->mutex);
1185 list_for_each_entry(runtime, &module->runtime_list, list) {
1186 if (runtime->id == id) {
1187 mutex_unlock(&dsp->mutex);
1192 mutex_unlock(&dsp->mutex);
1195 EXPORT_SYMBOL_GPL(sst_module_runtime_get_from_id);
1197 /* returns block address in DSP address space */
1198 u32 sst_dsp_get_offset(struct sst_dsp *dsp, u32 offset,
1199 enum sst_mem_type type)
1203 return offset - dsp->addr.iram_offset +
1204 dsp->addr.dsp_iram_offset;
1206 return offset - dsp->addr.dram_offset +
1207 dsp->addr.dsp_dram_offset;
1212 EXPORT_SYMBOL_GPL(sst_dsp_get_offset);
1214 struct sst_dsp *sst_dsp_new(struct device *dev,
1215 struct sst_dsp_device *sst_dev, struct sst_pdata *pdata)
1217 struct sst_dsp *sst;
1220 dev_dbg(dev, "initialising audio DSP id 0x%x\n", pdata->id);
1222 sst = devm_kzalloc(dev, sizeof(*sst), GFP_KERNEL);
1226 spin_lock_init(&sst->spinlock);
1227 mutex_init(&sst->mutex);
1229 sst->dma_dev = pdata->dma_dev;
1230 sst->thread_context = sst_dev->thread_context;
1231 sst->sst_dev = sst_dev;
1232 sst->id = pdata->id;
1233 sst->irq = pdata->irq;
1234 sst->ops = sst_dev->ops;
1236 INIT_LIST_HEAD(&sst->used_block_list);
1237 INIT_LIST_HEAD(&sst->free_block_list);
1238 INIT_LIST_HEAD(&sst->module_list);
1239 INIT_LIST_HEAD(&sst->fw_list);
1240 INIT_LIST_HEAD(&sst->scratch_block_list);
1242 /* Initialise SST Audio DSP */
1243 if (sst->ops->init) {
1244 err = sst->ops->init(sst, pdata);
1249 /* Register the ISR */
1250 err = request_threaded_irq(sst->irq, sst->ops->irq_handler,
1251 sst_dev->thread, IRQF_SHARED, "AudioDSP", sst);
1255 err = sst_dma_new(sst);
1257 dev_warn(dev, "sst_dma_new failed %d\n", err);
1263 sst->ops->free(sst);
1267 EXPORT_SYMBOL_GPL(sst_dsp_new);
1269 void sst_dsp_free(struct sst_dsp *sst)
1271 free_irq(sst->irq, sst);
1273 sst->ops->free(sst);
1275 sst_dma_free(sst->dma);
1277 EXPORT_SYMBOL_GPL(sst_dsp_free);
1279 MODULE_DESCRIPTION("Intel SST Firmware Loader");
1280 MODULE_LICENSE("GPL v2");