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 err = dma_coerce_mask_and_coherent(dsp->dev, DMA_BIT_MASK(32));
66 /* allocate DMA buffer to store FW data */
67 sst_fw->dma_buf = dma_alloc_coherent(dsp->dev, sst_fw->size,
68 &sst_fw->dmable_fw_paddr, GFP_DMA | GFP_KERNEL);
69 if (!sst_fw->dma_buf) {
70 dev_err(dsp->dev, "error: DMA alloc failed\n");
75 /* copy FW data to DMA-able memory */
76 memcpy((void *)sst_fw->dma_buf, (void *)fw->data, fw->size);
78 /* call core specific FW paser to load FW data into DSP */
79 err = dsp->ops->parse_fw(sst_fw);
81 dev_err(dsp->dev, "error: parse fw failed %d\n", err);
85 mutex_lock(&dsp->mutex);
86 list_add(&sst_fw->list, &dsp->fw_list);
87 mutex_unlock(&dsp->mutex);
92 dma_free_coherent(dsp->dev, sst_fw->size,
94 sst_fw->dmable_fw_paddr);
98 EXPORT_SYMBOL_GPL(sst_fw_new);
100 /* free single firmware object */
101 void sst_fw_free(struct sst_fw *sst_fw)
103 struct sst_dsp *dsp = sst_fw->dsp;
105 mutex_lock(&dsp->mutex);
106 list_del(&sst_fw->list);
107 mutex_unlock(&dsp->mutex);
109 dma_free_coherent(dsp->dev, sst_fw->size, sst_fw->dma_buf,
110 sst_fw->dmable_fw_paddr);
113 EXPORT_SYMBOL_GPL(sst_fw_free);
115 /* free all firmware objects */
116 void sst_fw_free_all(struct sst_dsp *dsp)
118 struct sst_fw *sst_fw, *t;
120 mutex_lock(&dsp->mutex);
121 list_for_each_entry_safe(sst_fw, t, &dsp->fw_list, list) {
123 list_del(&sst_fw->list);
124 dma_free_coherent(dsp->dev, sst_fw->size, sst_fw->dma_buf,
125 sst_fw->dmable_fw_paddr);
128 mutex_unlock(&dsp->mutex);
130 EXPORT_SYMBOL_GPL(sst_fw_free_all);
132 /* create a new SST generic module from FW template */
133 struct sst_module *sst_module_new(struct sst_fw *sst_fw,
134 struct sst_module_template *template, void *private)
136 struct sst_dsp *dsp = sst_fw->dsp;
137 struct sst_module *sst_module;
139 sst_module = kzalloc(sizeof(*sst_module), GFP_KERNEL);
140 if (sst_module == NULL)
143 sst_module->id = template->id;
144 sst_module->dsp = dsp;
145 sst_module->sst_fw = sst_fw;
147 memcpy(&sst_module->s, &template->s, sizeof(struct sst_module_data));
148 memcpy(&sst_module->p, &template->p, sizeof(struct sst_module_data));
150 INIT_LIST_HEAD(&sst_module->block_list);
152 mutex_lock(&dsp->mutex);
153 list_add(&sst_module->list, &dsp->module_list);
154 mutex_unlock(&dsp->mutex);
158 EXPORT_SYMBOL_GPL(sst_module_new);
160 /* free firmware module and remove from available list */
161 void sst_module_free(struct sst_module *sst_module)
163 struct sst_dsp *dsp = sst_module->dsp;
165 mutex_lock(&dsp->mutex);
166 list_del(&sst_module->list);
167 mutex_unlock(&dsp->mutex);
171 EXPORT_SYMBOL_GPL(sst_module_free);
173 static struct sst_mem_block *find_block(struct sst_dsp *dsp, int type,
176 struct sst_mem_block *block;
178 list_for_each_entry(block, &dsp->free_block_list, list) {
179 if (block->type == type && block->offset == offset)
186 static int block_alloc_contiguous(struct sst_module *module,
187 struct sst_module_data *data, u32 offset, int size)
189 struct list_head tmp = LIST_HEAD_INIT(tmp);
190 struct sst_dsp *dsp = module->dsp;
191 struct sst_mem_block *block;
194 block = find_block(dsp, data->type, offset);
196 list_splice(&tmp, &dsp->free_block_list);
200 list_move_tail(&block->list, &tmp);
201 offset += block->size;
205 list_for_each_entry(block, &tmp, list)
206 list_add(&block->module_list, &module->block_list);
208 list_splice(&tmp, &dsp->used_block_list);
212 /* allocate free DSP blocks for module data - callers hold locks */
213 static int block_alloc(struct sst_module *module,
214 struct sst_module_data *data)
216 struct sst_dsp *dsp = module->dsp;
217 struct sst_mem_block *block, *tmp;
223 /* find first free whole blocks that can hold module */
224 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
226 /* ignore blocks with wrong type */
227 if (block->type != data->type)
230 if (data->size > block->size)
233 data->offset = block->offset;
234 block->data_type = data->data_type;
235 block->bytes_used = data->size % block->size;
236 list_add(&block->module_list, &module->block_list);
237 list_move(&block->list, &dsp->used_block_list);
238 dev_dbg(dsp->dev, " *module %d added block %d:%d\n",
239 module->id, block->type, block->index);
243 /* then find free multiple blocks that can hold module */
244 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
246 /* ignore blocks with wrong type */
247 if (block->type != data->type)
250 /* do we span > 1 blocks */
251 if (data->size > block->size) {
252 ret = block_alloc_contiguous(module, data,
253 block->offset + block->size,
254 data->size - block->size);
260 /* not enough free block space */
264 /* remove module from memory - callers hold locks */
265 static void block_module_remove(struct sst_module *module)
267 struct sst_mem_block *block, *tmp;
268 struct sst_dsp *dsp = module->dsp;
271 /* disable each block */
272 list_for_each_entry(block, &module->block_list, module_list) {
274 if (block->ops && block->ops->disable) {
275 err = block->ops->disable(block);
278 "error: cant disable block %d:%d\n",
279 block->type, block->index);
283 /* mark each block as free */
284 list_for_each_entry_safe(block, tmp, &module->block_list, module_list) {
285 list_del(&block->module_list);
286 list_move(&block->list, &dsp->free_block_list);
290 /* prepare the memory block to receive data from host - callers hold locks */
291 static int block_module_prepare(struct sst_module *module)
293 struct sst_mem_block *block;
296 /* enable each block so that's it'e ready for module P/S data */
297 list_for_each_entry(block, &module->block_list, module_list) {
299 if (block->ops && block->ops->enable) {
300 ret = block->ops->enable(block);
302 dev_err(module->dsp->dev,
303 "error: cant disable block %d:%d\n",
304 block->type, block->index);
312 list_for_each_entry(block, &module->block_list, module_list) {
313 if (block->ops && block->ops->disable)
314 block->ops->disable(block);
319 /* allocate memory blocks for static module addresses - callers hold locks */
320 static int block_alloc_fixed(struct sst_module *module,
321 struct sst_module_data *data)
323 struct sst_dsp *dsp = module->dsp;
324 struct sst_mem_block *block, *tmp;
325 u32 end = data->offset + data->size, block_end;
328 /* only IRAM/DRAM blocks are managed */
329 if (data->type != SST_MEM_IRAM && data->type != SST_MEM_DRAM)
332 /* are blocks already attached to this module */
333 list_for_each_entry_safe(block, tmp, &module->block_list, module_list) {
335 /* force compacting mem blocks of the same data_type */
336 if (block->data_type != data->data_type)
339 block_end = block->offset + block->size;
341 /* find block that holds section */
342 if (data->offset >= block->offset && end < block_end)
345 /* does block span more than 1 section */
346 if (data->offset >= block->offset && data->offset < block_end) {
348 err = block_alloc_contiguous(module, data,
349 block->offset + block->size,
350 data->size - block->size + data->offset - block->offset);
354 /* module already owns blocks */
359 /* find first free blocks that can hold section in free list */
360 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
361 block_end = block->offset + block->size;
363 /* find block that holds section */
364 if (data->offset >= block->offset && end < block_end) {
367 block->data_type = data->data_type;
368 list_move(&block->list, &dsp->used_block_list);
369 list_add(&block->module_list, &module->block_list);
373 /* does block span more than 1 section */
374 if (data->offset >= block->offset && data->offset < block_end) {
376 err = block_alloc_contiguous(module, data,
377 block->offset + block->size,
378 data->size - block->size);
390 /* Load fixed module data into DSP memory blocks */
391 int sst_module_insert_fixed_block(struct sst_module *module,
392 struct sst_module_data *data)
394 struct sst_dsp *dsp = module->dsp;
397 mutex_lock(&dsp->mutex);
399 /* alloc blocks that includes this section */
400 ret = block_alloc_fixed(module, data);
403 "error: no free blocks for section at offset 0x%x size 0x%x\n",
404 data->offset, data->size);
405 mutex_unlock(&dsp->mutex);
409 /* prepare DSP blocks for module copy */
410 ret = block_module_prepare(module);
412 dev_err(dsp->dev, "error: fw module prepare failed\n");
416 /* copy partial module data to blocks */
417 sst_memcpy32(dsp->addr.lpe + data->offset, data->data, data->size);
419 mutex_unlock(&dsp->mutex);
423 block_module_remove(module);
424 mutex_unlock(&dsp->mutex);
427 EXPORT_SYMBOL_GPL(sst_module_insert_fixed_block);
429 /* Unload entire module from DSP memory */
430 int sst_block_module_remove(struct sst_module *module)
432 struct sst_dsp *dsp = module->dsp;
434 mutex_lock(&dsp->mutex);
435 block_module_remove(module);
436 mutex_unlock(&dsp->mutex);
439 EXPORT_SYMBOL_GPL(sst_block_module_remove);
441 /* register a DSP memory block for use with FW based modules */
442 struct sst_mem_block *sst_mem_block_register(struct sst_dsp *dsp, u32 offset,
443 u32 size, enum sst_mem_type type, struct sst_block_ops *ops, u32 index,
446 struct sst_mem_block *block;
448 block = kzalloc(sizeof(*block), GFP_KERNEL);
452 block->offset = offset;
454 block->index = index;
457 block->private = private;
460 mutex_lock(&dsp->mutex);
461 list_add(&block->list, &dsp->free_block_list);
462 mutex_unlock(&dsp->mutex);
466 EXPORT_SYMBOL_GPL(sst_mem_block_register);
468 /* unregister all DSP memory blocks */
469 void sst_mem_block_unregister_all(struct sst_dsp *dsp)
471 struct sst_mem_block *block, *tmp;
473 mutex_lock(&dsp->mutex);
475 /* unregister used blocks */
476 list_for_each_entry_safe(block, tmp, &dsp->used_block_list, list) {
477 list_del(&block->list);
481 /* unregister free blocks */
482 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
483 list_del(&block->list);
487 mutex_unlock(&dsp->mutex);
489 EXPORT_SYMBOL_GPL(sst_mem_block_unregister_all);
491 /* allocate scratch buffer blocks */
492 struct sst_module *sst_mem_block_alloc_scratch(struct sst_dsp *dsp)
494 struct sst_module *sst_module, *scratch;
495 struct sst_mem_block *block, *tmp;
499 scratch = kzalloc(sizeof(struct sst_module), GFP_KERNEL);
503 mutex_lock(&dsp->mutex);
505 /* calculate required scratch size */
506 list_for_each_entry(sst_module, &dsp->module_list, list) {
507 if (scratch->s.size > sst_module->s.size)
508 scratch->s.size = scratch->s.size;
510 scratch->s.size = sst_module->s.size;
513 dev_dbg(dsp->dev, "scratch buffer required is %d bytes\n",
516 /* init scratch module */
518 scratch->s.type = SST_MEM_DRAM;
519 scratch->s.data_type = SST_DATA_S;
520 INIT_LIST_HEAD(&scratch->block_list);
522 /* check free blocks before looking at used blocks for space */
523 if (!list_empty(&dsp->free_block_list))
524 block = list_first_entry(&dsp->free_block_list,
525 struct sst_mem_block, list);
527 block = list_first_entry(&dsp->used_block_list,
528 struct sst_mem_block, list);
529 block_size = block->size;
531 /* allocate blocks for module scratch buffers */
532 dev_dbg(dsp->dev, "allocating scratch blocks\n");
533 ret = block_alloc(scratch, &scratch->s);
535 dev_err(dsp->dev, "error: can't alloc scratch blocks\n");
539 /* assign the same offset of scratch to each module */
540 list_for_each_entry(sst_module, &dsp->module_list, list)
541 sst_module->s.offset = scratch->s.offset;
543 mutex_unlock(&dsp->mutex);
547 list_for_each_entry_safe(block, tmp, &scratch->block_list, module_list)
548 list_del(&block->module_list);
549 mutex_unlock(&dsp->mutex);
552 EXPORT_SYMBOL_GPL(sst_mem_block_alloc_scratch);
554 /* free all scratch blocks */
555 void sst_mem_block_free_scratch(struct sst_dsp *dsp,
556 struct sst_module *scratch)
558 struct sst_mem_block *block, *tmp;
560 mutex_lock(&dsp->mutex);
562 list_for_each_entry_safe(block, tmp, &scratch->block_list, module_list)
563 list_del(&block->module_list);
565 mutex_unlock(&dsp->mutex);
567 EXPORT_SYMBOL_GPL(sst_mem_block_free_scratch);
569 /* get a module from it's unique ID */
570 struct sst_module *sst_module_get_from_id(struct sst_dsp *dsp, u32 id)
572 struct sst_module *module;
574 mutex_lock(&dsp->mutex);
576 list_for_each_entry(module, &dsp->module_list, list) {
577 if (module->id == id) {
578 mutex_unlock(&dsp->mutex);
583 mutex_unlock(&dsp->mutex);
586 EXPORT_SYMBOL_GPL(sst_module_get_from_id);