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>
28 #include <asm/pgtable.h>
31 #include "sst-dsp-priv.h"
33 static void sst_memcpy32(volatile void __iomem *dest, void *src, u32 bytes)
37 /* copy one 32 bit word at a time as 64 bit access is not supported */
38 for (i = 0; i < bytes; i += 4)
39 memcpy_toio(dest + i, src + i, 4);
42 /* create new generic firmware object */
43 struct sst_fw *sst_fw_new(struct sst_dsp *dsp,
44 const struct firmware *fw, void *private)
46 struct sst_fw *sst_fw;
49 if (!dsp->ops->parse_fw)
52 sst_fw = kzalloc(sizeof(*sst_fw), GFP_KERNEL);
57 sst_fw->private = private;
58 sst_fw->size = fw->size;
60 /* allocate DMA buffer to store FW data */
61 sst_fw->dma_buf = dma_alloc_coherent(dsp->dma_dev, sst_fw->size,
62 &sst_fw->dmable_fw_paddr, GFP_DMA | GFP_KERNEL);
63 if (!sst_fw->dma_buf) {
64 dev_err(dsp->dev, "error: DMA alloc failed\n");
69 /* copy FW data to DMA-able memory */
70 memcpy((void *)sst_fw->dma_buf, (void *)fw->data, fw->size);
72 /* call core specific FW paser to load FW data into DSP */
73 err = dsp->ops->parse_fw(sst_fw);
75 dev_err(dsp->dev, "error: parse fw failed %d\n", err);
79 mutex_lock(&dsp->mutex);
80 list_add(&sst_fw->list, &dsp->fw_list);
81 mutex_unlock(&dsp->mutex);
86 dma_free_coherent(dsp->dev, sst_fw->size,
88 sst_fw->dmable_fw_paddr);
92 EXPORT_SYMBOL_GPL(sst_fw_new);
94 /* free single firmware object */
95 void sst_fw_free(struct sst_fw *sst_fw)
97 struct sst_dsp *dsp = sst_fw->dsp;
99 mutex_lock(&dsp->mutex);
100 list_del(&sst_fw->list);
101 mutex_unlock(&dsp->mutex);
103 dma_free_coherent(dsp->dma_dev, sst_fw->size, sst_fw->dma_buf,
104 sst_fw->dmable_fw_paddr);
107 EXPORT_SYMBOL_GPL(sst_fw_free);
109 /* free all firmware objects */
110 void sst_fw_free_all(struct sst_dsp *dsp)
112 struct sst_fw *sst_fw, *t;
114 mutex_lock(&dsp->mutex);
115 list_for_each_entry_safe(sst_fw, t, &dsp->fw_list, list) {
117 list_del(&sst_fw->list);
118 dma_free_coherent(dsp->dev, sst_fw->size, sst_fw->dma_buf,
119 sst_fw->dmable_fw_paddr);
122 mutex_unlock(&dsp->mutex);
124 EXPORT_SYMBOL_GPL(sst_fw_free_all);
126 /* create a new SST generic module from FW template */
127 struct sst_module *sst_module_new(struct sst_fw *sst_fw,
128 struct sst_module_template *template, void *private)
130 struct sst_dsp *dsp = sst_fw->dsp;
131 struct sst_module *sst_module;
133 sst_module = kzalloc(sizeof(*sst_module), GFP_KERNEL);
134 if (sst_module == NULL)
137 sst_module->id = template->id;
138 sst_module->dsp = dsp;
139 sst_module->sst_fw = sst_fw;
141 memcpy(&sst_module->s, &template->s, sizeof(struct sst_module_data));
142 memcpy(&sst_module->p, &template->p, sizeof(struct sst_module_data));
144 INIT_LIST_HEAD(&sst_module->block_list);
146 mutex_lock(&dsp->mutex);
147 list_add(&sst_module->list, &dsp->module_list);
148 mutex_unlock(&dsp->mutex);
152 EXPORT_SYMBOL_GPL(sst_module_new);
154 /* free firmware module and remove from available list */
155 void sst_module_free(struct sst_module *sst_module)
157 struct sst_dsp *dsp = sst_module->dsp;
159 mutex_lock(&dsp->mutex);
160 list_del(&sst_module->list);
161 mutex_unlock(&dsp->mutex);
165 EXPORT_SYMBOL_GPL(sst_module_free);
167 static struct sst_mem_block *find_block(struct sst_dsp *dsp, int type,
170 struct sst_mem_block *block;
172 list_for_each_entry(block, &dsp->free_block_list, list) {
173 if (block->type == type && block->offset == offset)
180 static int block_alloc_contiguous(struct sst_module *module,
181 struct sst_module_data *data, u32 offset, int size)
183 struct list_head tmp = LIST_HEAD_INIT(tmp);
184 struct sst_dsp *dsp = module->dsp;
185 struct sst_mem_block *block;
188 block = find_block(dsp, data->type, offset);
190 list_splice(&tmp, &dsp->free_block_list);
194 list_move_tail(&block->list, &tmp);
195 offset += block->size;
199 list_for_each_entry(block, &tmp, list)
200 list_add(&block->module_list, &module->block_list);
202 list_splice(&tmp, &dsp->used_block_list);
206 /* allocate free DSP blocks for module data - callers hold locks */
207 static int block_alloc(struct sst_module *module,
208 struct sst_module_data *data)
210 struct sst_dsp *dsp = module->dsp;
211 struct sst_mem_block *block, *tmp;
217 /* find first free whole blocks that can hold module */
218 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
220 /* ignore blocks with wrong type */
221 if (block->type != data->type)
224 if (data->size > block->size)
227 data->offset = block->offset;
228 block->data_type = data->data_type;
229 block->bytes_used = data->size % block->size;
230 list_add(&block->module_list, &module->block_list);
231 list_move(&block->list, &dsp->used_block_list);
232 dev_dbg(dsp->dev, " *module %d added block %d:%d\n",
233 module->id, block->type, block->index);
237 /* then find free multiple blocks that can hold module */
238 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
240 /* ignore blocks with wrong type */
241 if (block->type != data->type)
244 /* do we span > 1 blocks */
245 if (data->size > block->size) {
246 ret = block_alloc_contiguous(module, data,
247 block->offset + block->size,
248 data->size - block->size);
254 /* not enough free block space */
258 /* remove module from memory - callers hold locks */
259 static void block_module_remove(struct sst_module *module)
261 struct sst_mem_block *block, *tmp;
262 struct sst_dsp *dsp = module->dsp;
265 /* disable each block */
266 list_for_each_entry(block, &module->block_list, module_list) {
268 if (block->ops && block->ops->disable) {
269 err = block->ops->disable(block);
272 "error: cant disable block %d:%d\n",
273 block->type, block->index);
277 /* mark each block as free */
278 list_for_each_entry_safe(block, tmp, &module->block_list, module_list) {
279 list_del(&block->module_list);
280 list_move(&block->list, &dsp->free_block_list);
284 /* prepare the memory block to receive data from host - callers hold locks */
285 static int block_module_prepare(struct sst_module *module)
287 struct sst_mem_block *block;
290 /* enable each block so that's it'e ready for module P/S data */
291 list_for_each_entry(block, &module->block_list, module_list) {
293 if (block->ops && block->ops->enable) {
294 ret = block->ops->enable(block);
296 dev_err(module->dsp->dev,
297 "error: cant disable block %d:%d\n",
298 block->type, block->index);
306 list_for_each_entry(block, &module->block_list, module_list) {
307 if (block->ops && block->ops->disable)
308 block->ops->disable(block);
313 /* allocate memory blocks for static module addresses - callers hold locks */
314 static int block_alloc_fixed(struct sst_module *module,
315 struct sst_module_data *data)
317 struct sst_dsp *dsp = module->dsp;
318 struct sst_mem_block *block, *tmp;
319 u32 end = data->offset + data->size, block_end;
322 /* only IRAM/DRAM blocks are managed */
323 if (data->type != SST_MEM_IRAM && data->type != SST_MEM_DRAM)
326 /* are blocks already attached to this module */
327 list_for_each_entry_safe(block, tmp, &module->block_list, module_list) {
329 /* force compacting mem blocks of the same data_type */
330 if (block->data_type != data->data_type)
333 block_end = block->offset + block->size;
335 /* find block that holds section */
336 if (data->offset >= block->offset && end < block_end)
339 /* does block span more than 1 section */
340 if (data->offset >= block->offset && data->offset < block_end) {
342 err = block_alloc_contiguous(module, data,
343 block->offset + block->size,
344 data->size - block->size + data->offset - block->offset);
348 /* module already owns blocks */
353 /* find first free blocks that can hold section in free list */
354 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
355 block_end = block->offset + block->size;
357 /* find block that holds section */
358 if (data->offset >= block->offset && end < block_end) {
361 block->data_type = data->data_type;
362 list_move(&block->list, &dsp->used_block_list);
363 list_add(&block->module_list, &module->block_list);
367 /* does block span more than 1 section */
368 if (data->offset >= block->offset && data->offset < block_end) {
370 err = block_alloc_contiguous(module, data,
371 block->offset + block->size,
372 data->size - block->size);
384 /* Load fixed module data into DSP memory blocks */
385 int sst_module_insert_fixed_block(struct sst_module *module,
386 struct sst_module_data *data)
388 struct sst_dsp *dsp = module->dsp;
391 mutex_lock(&dsp->mutex);
393 /* alloc blocks that includes this section */
394 ret = block_alloc_fixed(module, data);
397 "error: no free blocks for section at offset 0x%x size 0x%x\n",
398 data->offset, data->size);
399 mutex_unlock(&dsp->mutex);
403 /* prepare DSP blocks for module copy */
404 ret = block_module_prepare(module);
406 dev_err(dsp->dev, "error: fw module prepare failed\n");
410 /* copy partial module data to blocks */
411 sst_memcpy32(dsp->addr.lpe + data->offset, data->data, data->size);
413 mutex_unlock(&dsp->mutex);
417 block_module_remove(module);
418 mutex_unlock(&dsp->mutex);
421 EXPORT_SYMBOL_GPL(sst_module_insert_fixed_block);
423 /* Unload entire module from DSP memory */
424 int sst_block_module_remove(struct sst_module *module)
426 struct sst_dsp *dsp = module->dsp;
428 mutex_lock(&dsp->mutex);
429 block_module_remove(module);
430 mutex_unlock(&dsp->mutex);
433 EXPORT_SYMBOL_GPL(sst_block_module_remove);
435 /* register a DSP memory block for use with FW based modules */
436 struct sst_mem_block *sst_mem_block_register(struct sst_dsp *dsp, u32 offset,
437 u32 size, enum sst_mem_type type, struct sst_block_ops *ops, u32 index,
440 struct sst_mem_block *block;
442 block = kzalloc(sizeof(*block), GFP_KERNEL);
446 block->offset = offset;
448 block->index = index;
451 block->private = private;
454 mutex_lock(&dsp->mutex);
455 list_add(&block->list, &dsp->free_block_list);
456 mutex_unlock(&dsp->mutex);
460 EXPORT_SYMBOL_GPL(sst_mem_block_register);
462 /* unregister all DSP memory blocks */
463 void sst_mem_block_unregister_all(struct sst_dsp *dsp)
465 struct sst_mem_block *block, *tmp;
467 mutex_lock(&dsp->mutex);
469 /* unregister used blocks */
470 list_for_each_entry_safe(block, tmp, &dsp->used_block_list, list) {
471 list_del(&block->list);
475 /* unregister free blocks */
476 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
477 list_del(&block->list);
481 mutex_unlock(&dsp->mutex);
483 EXPORT_SYMBOL_GPL(sst_mem_block_unregister_all);
485 /* allocate scratch buffer blocks */
486 struct sst_module *sst_mem_block_alloc_scratch(struct sst_dsp *dsp)
488 struct sst_module *sst_module, *scratch;
489 struct sst_mem_block *block, *tmp;
493 scratch = kzalloc(sizeof(struct sst_module), GFP_KERNEL);
497 mutex_lock(&dsp->mutex);
499 /* calculate required scratch size */
500 list_for_each_entry(sst_module, &dsp->module_list, list) {
501 if (scratch->s.size > sst_module->s.size)
502 scratch->s.size = scratch->s.size;
504 scratch->s.size = sst_module->s.size;
507 dev_dbg(dsp->dev, "scratch buffer required is %d bytes\n",
510 /* init scratch module */
512 scratch->s.type = SST_MEM_DRAM;
513 scratch->s.data_type = SST_DATA_S;
514 INIT_LIST_HEAD(&scratch->block_list);
516 /* check free blocks before looking at used blocks for space */
517 if (!list_empty(&dsp->free_block_list))
518 block = list_first_entry(&dsp->free_block_list,
519 struct sst_mem_block, list);
521 block = list_first_entry(&dsp->used_block_list,
522 struct sst_mem_block, list);
523 block_size = block->size;
525 /* allocate blocks for module scratch buffers */
526 dev_dbg(dsp->dev, "allocating scratch blocks\n");
527 ret = block_alloc(scratch, &scratch->s);
529 dev_err(dsp->dev, "error: can't alloc scratch blocks\n");
533 /* assign the same offset of scratch to each module */
534 list_for_each_entry(sst_module, &dsp->module_list, list)
535 sst_module->s.offset = scratch->s.offset;
537 mutex_unlock(&dsp->mutex);
541 list_for_each_entry_safe(block, tmp, &scratch->block_list, module_list)
542 list_del(&block->module_list);
543 mutex_unlock(&dsp->mutex);
546 EXPORT_SYMBOL_GPL(sst_mem_block_alloc_scratch);
548 /* free all scratch blocks */
549 void sst_mem_block_free_scratch(struct sst_dsp *dsp,
550 struct sst_module *scratch)
552 struct sst_mem_block *block, *tmp;
554 mutex_lock(&dsp->mutex);
556 list_for_each_entry_safe(block, tmp, &scratch->block_list, module_list)
557 list_del(&block->module_list);
559 mutex_unlock(&dsp->mutex);
561 EXPORT_SYMBOL_GPL(sst_mem_block_free_scratch);
563 /* get a module from it's unique ID */
564 struct sst_module *sst_module_get_from_id(struct sst_dsp *dsp, u32 id)
566 struct sst_module *module;
568 mutex_lock(&dsp->mutex);
570 list_for_each_entry(module, &dsp->module_list, list) {
571 if (module->id == id) {
572 mutex_unlock(&dsp->mutex);
577 mutex_unlock(&dsp->mutex);
580 EXPORT_SYMBOL_GPL(sst_module_get_from_id);