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 block_module_remove(struct sst_module *module);
35 static void sst_memcpy32(volatile void __iomem *dest, void *src, u32 bytes)
39 /* copy one 32 bit word at a time as 64 bit access is not supported */
40 for (i = 0; i < bytes; i += 4)
41 memcpy_toio(dest + i, src + i, 4);
44 /* create new generic firmware object */
45 struct sst_fw *sst_fw_new(struct sst_dsp *dsp,
46 const struct firmware *fw, void *private)
48 struct sst_fw *sst_fw;
51 if (!dsp->ops->parse_fw)
54 sst_fw = kzalloc(sizeof(*sst_fw), GFP_KERNEL);
59 sst_fw->private = private;
60 sst_fw->size = fw->size;
62 err = dma_coerce_mask_and_coherent(dsp->dev, DMA_BIT_MASK(32));
68 /* allocate DMA buffer to store FW data */
69 sst_fw->dma_buf = dma_alloc_coherent(dsp->dev, sst_fw->size,
70 &sst_fw->dmable_fw_paddr, GFP_DMA | GFP_KERNEL);
71 if (!sst_fw->dma_buf) {
72 dev_err(dsp->dev, "error: DMA alloc failed\n");
77 /* copy FW data to DMA-able memory */
78 memcpy((void *)sst_fw->dma_buf, (void *)fw->data, fw->size);
80 /* call core specific FW paser to load FW data into DSP */
81 err = dsp->ops->parse_fw(sst_fw);
83 dev_err(dsp->dev, "error: parse fw failed %d\n", err);
87 mutex_lock(&dsp->mutex);
88 list_add(&sst_fw->list, &dsp->fw_list);
89 mutex_unlock(&dsp->mutex);
94 dma_free_coherent(dsp->dev, sst_fw->size,
96 sst_fw->dmable_fw_paddr);
100 EXPORT_SYMBOL_GPL(sst_fw_new);
102 int sst_fw_reload(struct sst_fw *sst_fw)
104 struct sst_dsp *dsp = sst_fw->dsp;
107 dev_dbg(dsp->dev, "reloading firmware\n");
109 /* call core specific FW paser to load FW data into DSP */
110 ret = dsp->ops->parse_fw(sst_fw);
112 dev_err(dsp->dev, "error: parse fw failed %d\n", ret);
116 EXPORT_SYMBOL_GPL(sst_fw_reload);
118 void sst_fw_unload(struct sst_fw *sst_fw)
120 struct sst_dsp *dsp = sst_fw->dsp;
121 struct sst_module *module, *tmp;
123 dev_dbg(dsp->dev, "unloading firmware\n");
125 mutex_lock(&dsp->mutex);
126 list_for_each_entry_safe(module, tmp, &dsp->module_list, list) {
127 if (module->sst_fw == sst_fw) {
128 block_module_remove(module);
129 list_del(&module->list);
134 mutex_unlock(&dsp->mutex);
136 EXPORT_SYMBOL_GPL(sst_fw_unload);
138 /* free single firmware object */
139 void sst_fw_free(struct sst_fw *sst_fw)
141 struct sst_dsp *dsp = sst_fw->dsp;
143 mutex_lock(&dsp->mutex);
144 list_del(&sst_fw->list);
145 mutex_unlock(&dsp->mutex);
147 dma_free_coherent(dsp->dev, sst_fw->size, sst_fw->dma_buf,
148 sst_fw->dmable_fw_paddr);
151 EXPORT_SYMBOL_GPL(sst_fw_free);
153 /* free all firmware objects */
154 void sst_fw_free_all(struct sst_dsp *dsp)
156 struct sst_fw *sst_fw, *t;
158 mutex_lock(&dsp->mutex);
159 list_for_each_entry_safe(sst_fw, t, &dsp->fw_list, list) {
161 list_del(&sst_fw->list);
162 dma_free_coherent(dsp->dev, sst_fw->size, sst_fw->dma_buf,
163 sst_fw->dmable_fw_paddr);
166 mutex_unlock(&dsp->mutex);
168 EXPORT_SYMBOL_GPL(sst_fw_free_all);
170 /* create a new SST generic module from FW template */
171 struct sst_module *sst_module_new(struct sst_fw *sst_fw,
172 struct sst_module_template *template, void *private)
174 struct sst_dsp *dsp = sst_fw->dsp;
175 struct sst_module *sst_module;
177 sst_module = kzalloc(sizeof(*sst_module), GFP_KERNEL);
178 if (sst_module == NULL)
181 sst_module->id = template->id;
182 sst_module->dsp = dsp;
183 sst_module->sst_fw = sst_fw;
185 memcpy(&sst_module->s, &template->s, sizeof(struct sst_module_data));
186 memcpy(&sst_module->p, &template->p, sizeof(struct sst_module_data));
188 INIT_LIST_HEAD(&sst_module->block_list);
190 mutex_lock(&dsp->mutex);
191 list_add(&sst_module->list, &dsp->module_list);
192 mutex_unlock(&dsp->mutex);
196 EXPORT_SYMBOL_GPL(sst_module_new);
198 /* free firmware module and remove from available list */
199 void sst_module_free(struct sst_module *sst_module)
201 struct sst_dsp *dsp = sst_module->dsp;
203 mutex_lock(&dsp->mutex);
204 list_del(&sst_module->list);
205 mutex_unlock(&dsp->mutex);
209 EXPORT_SYMBOL_GPL(sst_module_free);
211 static struct sst_mem_block *find_block(struct sst_dsp *dsp, int type,
214 struct sst_mem_block *block;
216 list_for_each_entry(block, &dsp->free_block_list, list) {
217 if (block->type == type && block->offset == offset)
224 static int block_alloc_contiguous(struct sst_module *module,
225 struct sst_module_data *data, u32 offset, int size)
227 struct list_head tmp = LIST_HEAD_INIT(tmp);
228 struct sst_dsp *dsp = module->dsp;
229 struct sst_mem_block *block;
232 block = find_block(dsp, data->type, offset);
234 list_splice(&tmp, &dsp->free_block_list);
238 list_move_tail(&block->list, &tmp);
239 offset += block->size;
243 list_splice(&tmp, &dsp->used_block_list);
247 /* allocate free DSP blocks for module data - callers hold locks */
248 static int block_alloc(struct sst_module *module,
249 struct sst_module_data *data)
251 struct sst_dsp *dsp = module->dsp;
252 struct sst_mem_block *block, *tmp;
258 /* find first free whole blocks that can hold module */
259 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
261 /* ignore blocks with wrong type */
262 if (block->type != data->type)
265 if (data->size > block->size)
268 data->offset = block->offset;
269 block->data_type = data->data_type;
270 block->bytes_used = data->size % block->size;
271 list_add(&block->module_list, &module->block_list);
272 list_move(&block->list, &dsp->used_block_list);
273 dev_dbg(dsp->dev, " *module %d added block %d:%d\n",
274 module->id, block->type, block->index);
278 /* then find free multiple blocks that can hold module */
279 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
281 /* ignore blocks with wrong type */
282 if (block->type != data->type)
285 /* do we span > 1 blocks */
286 if (data->size > block->size) {
287 ret = block_alloc_contiguous(module, data,
288 block->offset + block->size,
289 data->size - block->size);
295 /* not enough free block space */
299 /* remove module from memory - callers hold locks */
300 static void block_module_remove(struct sst_module *module)
302 struct sst_mem_block *block, *tmp;
303 struct sst_dsp *dsp = module->dsp;
306 /* disable each block */
307 list_for_each_entry(block, &module->block_list, module_list) {
309 if (block->ops && block->ops->disable) {
310 err = block->ops->disable(block);
313 "error: cant disable block %d:%d\n",
314 block->type, block->index);
318 /* mark each block as free */
319 list_for_each_entry_safe(block, tmp, &module->block_list, module_list) {
320 list_del(&block->module_list);
321 list_move(&block->list, &dsp->free_block_list);
325 /* prepare the memory block to receive data from host - callers hold locks */
326 static int block_module_prepare(struct sst_module *module)
328 struct sst_mem_block *block;
331 /* enable each block so that's it'e ready for module P/S data */
332 list_for_each_entry(block, &module->block_list, module_list) {
334 if (block->ops && block->ops->enable) {
335 ret = block->ops->enable(block);
337 dev_err(module->dsp->dev,
338 "error: cant disable block %d:%d\n",
339 block->type, block->index);
347 list_for_each_entry(block, &module->block_list, module_list) {
348 if (block->ops && block->ops->disable)
349 block->ops->disable(block);
354 /* allocate memory blocks for static module addresses - callers hold locks */
355 static int block_alloc_fixed(struct sst_module *module,
356 struct sst_module_data *data)
358 struct sst_dsp *dsp = module->dsp;
359 struct sst_mem_block *block, *tmp;
360 u32 end = data->offset + data->size, block_end;
363 /* only IRAM/DRAM blocks are managed */
364 if (data->type != SST_MEM_IRAM && data->type != SST_MEM_DRAM)
367 /* are blocks already attached to this module */
368 list_for_each_entry_safe(block, tmp, &module->block_list, module_list) {
370 /* force compacting mem blocks of the same data_type */
371 if (block->data_type != data->data_type)
374 block_end = block->offset + block->size;
376 /* find block that holds section */
377 if (data->offset >= block->offset && end < block_end)
380 /* does block span more than 1 section */
381 if (data->offset >= block->offset && data->offset < block_end) {
383 err = block_alloc_contiguous(module, data,
384 block->offset + block->size,
385 data->size - block->size + data->offset - block->offset);
389 /* module already owns blocks */
394 /* find first free blocks that can hold section in free list */
395 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
396 block_end = block->offset + block->size;
398 /* find block that holds section */
399 if (data->offset >= block->offset && end < block_end) {
402 block->data_type = data->data_type;
403 list_move(&block->list, &dsp->used_block_list);
404 list_add(&block->module_list, &module->block_list);
408 /* does block span more than 1 section */
409 if (data->offset >= block->offset && data->offset < block_end) {
411 err = block_alloc_contiguous(module, data,
412 block->offset + block->size,
413 data->size - block->size);
418 block->data_type = data->data_type;
419 list_move(&block->list, &dsp->used_block_list);
420 list_add(&block->module_list, &module->block_list);
429 /* Load fixed module data into DSP memory blocks */
430 int sst_module_insert_fixed_block(struct sst_module *module,
431 struct sst_module_data *data)
433 struct sst_dsp *dsp = module->dsp;
436 mutex_lock(&dsp->mutex);
438 /* alloc blocks that includes this section */
439 ret = block_alloc_fixed(module, data);
442 "error: no free blocks for section at offset 0x%x size 0x%x\n",
443 data->offset, data->size);
444 mutex_unlock(&dsp->mutex);
448 /* prepare DSP blocks for module copy */
449 ret = block_module_prepare(module);
451 dev_err(dsp->dev, "error: fw module prepare failed\n");
455 /* copy partial module data to blocks */
456 sst_memcpy32(dsp->addr.lpe + data->offset, data->data, data->size);
458 mutex_unlock(&dsp->mutex);
462 block_module_remove(module);
463 mutex_unlock(&dsp->mutex);
466 EXPORT_SYMBOL_GPL(sst_module_insert_fixed_block);
468 /* Unload entire module from DSP memory */
469 int sst_block_module_remove(struct sst_module *module)
471 struct sst_dsp *dsp = module->dsp;
473 mutex_lock(&dsp->mutex);
474 block_module_remove(module);
475 mutex_unlock(&dsp->mutex);
478 EXPORT_SYMBOL_GPL(sst_block_module_remove);
480 /* register a DSP memory block for use with FW based modules */
481 struct sst_mem_block *sst_mem_block_register(struct sst_dsp *dsp, u32 offset,
482 u32 size, enum sst_mem_type type, struct sst_block_ops *ops, u32 index,
485 struct sst_mem_block *block;
487 block = kzalloc(sizeof(*block), GFP_KERNEL);
491 block->offset = offset;
493 block->index = index;
496 block->private = private;
499 mutex_lock(&dsp->mutex);
500 list_add(&block->list, &dsp->free_block_list);
501 mutex_unlock(&dsp->mutex);
505 EXPORT_SYMBOL_GPL(sst_mem_block_register);
507 /* unregister all DSP memory blocks */
508 void sst_mem_block_unregister_all(struct sst_dsp *dsp)
510 struct sst_mem_block *block, *tmp;
512 mutex_lock(&dsp->mutex);
514 /* unregister used blocks */
515 list_for_each_entry_safe(block, tmp, &dsp->used_block_list, list) {
516 list_del(&block->list);
520 /* unregister free blocks */
521 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
522 list_del(&block->list);
526 mutex_unlock(&dsp->mutex);
528 EXPORT_SYMBOL_GPL(sst_mem_block_unregister_all);
530 /* allocate scratch buffer blocks */
531 struct sst_module *sst_mem_block_alloc_scratch(struct sst_dsp *dsp)
533 struct sst_module *sst_module, *scratch;
534 struct sst_mem_block *block, *tmp;
538 scratch = kzalloc(sizeof(struct sst_module), GFP_KERNEL);
542 mutex_lock(&dsp->mutex);
544 /* calculate required scratch size */
545 list_for_each_entry(sst_module, &dsp->module_list, list) {
546 if (scratch->s.size < sst_module->s.size)
547 scratch->s.size = sst_module->s.size;
550 dev_dbg(dsp->dev, "scratch buffer required is %d bytes\n",
553 /* init scratch module */
555 scratch->s.type = SST_MEM_DRAM;
556 scratch->s.data_type = SST_DATA_S;
557 INIT_LIST_HEAD(&scratch->block_list);
559 /* check free blocks before looking at used blocks for space */
560 if (!list_empty(&dsp->free_block_list))
561 block = list_first_entry(&dsp->free_block_list,
562 struct sst_mem_block, list);
564 block = list_first_entry(&dsp->used_block_list,
565 struct sst_mem_block, list);
566 block_size = block->size;
568 /* allocate blocks for module scratch buffers */
569 dev_dbg(dsp->dev, "allocating scratch blocks\n");
570 ret = block_alloc(scratch, &scratch->s);
572 dev_err(dsp->dev, "error: can't alloc scratch blocks\n");
576 /* assign the same offset of scratch to each module */
577 list_for_each_entry(sst_module, &dsp->module_list, list)
578 sst_module->s.offset = scratch->s.offset;
580 mutex_unlock(&dsp->mutex);
584 list_for_each_entry_safe(block, tmp, &scratch->block_list, module_list)
585 list_del(&block->module_list);
586 mutex_unlock(&dsp->mutex);
589 EXPORT_SYMBOL_GPL(sst_mem_block_alloc_scratch);
591 /* free all scratch blocks */
592 void sst_mem_block_free_scratch(struct sst_dsp *dsp,
593 struct sst_module *scratch)
595 struct sst_mem_block *block, *tmp;
597 mutex_lock(&dsp->mutex);
599 list_for_each_entry_safe(block, tmp, &scratch->block_list, module_list)
600 list_del(&block->module_list);
602 mutex_unlock(&dsp->mutex);
604 EXPORT_SYMBOL_GPL(sst_mem_block_free_scratch);
606 /* get a module from it's unique ID */
607 struct sst_module *sst_module_get_from_id(struct sst_dsp *dsp, u32 id)
609 struct sst_module *module;
611 mutex_lock(&dsp->mutex);
613 list_for_each_entry(module, &dsp->module_list, list) {
614 if (module->id == id) {
615 mutex_unlock(&dsp->mutex);
620 mutex_unlock(&dsp->mutex);
623 EXPORT_SYMBOL_GPL(sst_module_get_from_id);