MIPS: BCM1480: Remove checks for CONFIG_SIBYTE_BCM1480_PROF
[linux/fpc-iii.git] / sound / soc / intel / sst-firmware.c
blob928f228c38e754db3f41551208493045ce9ee561
1 /*
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>
27 #include <asm/page.h>
28 #include <asm/pgtable.h>
30 #include "sst-dsp.h"
31 #include "sst-dsp-priv.h"
33 static void sst_memcpy32(volatile void __iomem *dest, void *src, u32 bytes)
35 u32 i;
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;
47 int err;
49 if (!dsp->ops->parse_fw)
50 return NULL;
52 sst_fw = kzalloc(sizeof(*sst_fw), GFP_KERNEL);
53 if (sst_fw == NULL)
54 return NULL;
56 sst_fw->dsp = dsp;
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");
65 kfree(sst_fw);
66 return NULL;
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);
74 if (err < 0) {
75 dev_err(dsp->dev, "error: parse fw failed %d\n", err);
76 goto parse_err;
79 mutex_lock(&dsp->mutex);
80 list_add(&sst_fw->list, &dsp->fw_list);
81 mutex_unlock(&dsp->mutex);
83 return sst_fw;
85 parse_err:
86 dma_free_coherent(dsp->dev, sst_fw->size,
87 sst_fw->dma_buf,
88 sst_fw->dmable_fw_paddr);
89 kfree(sst_fw);
90 return NULL;
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);
105 kfree(sst_fw);
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);
120 kfree(sst_fw);
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)
135 return 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);
150 return sst_module;
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);
163 kfree(sst_module);
165 EXPORT_SYMBOL_GPL(sst_module_free);
167 static struct sst_mem_block *find_block(struct sst_dsp *dsp, int type,
168 u32 offset)
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)
174 return block;
177 return NULL;
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;
187 while (size > 0) {
188 block = find_block(dsp, data->type, offset);
189 if (!block) {
190 list_splice(&tmp, &dsp->free_block_list);
191 return -ENOMEM;
194 list_move_tail(&block->list, &tmp);
195 offset += block->size;
196 size -= 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);
203 return 0;
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;
212 int ret = 0;
214 if (data->size == 0)
215 return 0;
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)
222 continue;
224 if (data->size > block->size)
225 continue;
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);
234 return 0;
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)
242 continue;
244 /* do we span > 1 blocks */
245 if (data->size > block->size) {
246 ret = block_alloc_contiguous(module, data,
247 block->offset, data->size);
248 if (ret == 0)
249 return ret;
253 /* not enough free block space */
254 return -ENOMEM;
257 /* remove module from memory - callers hold locks */
258 static void block_module_remove(struct sst_module *module)
260 struct sst_mem_block *block, *tmp;
261 struct sst_dsp *dsp = module->dsp;
262 int err;
264 /* disable each block */
265 list_for_each_entry(block, &module->block_list, module_list) {
267 if (block->ops && block->ops->disable) {
268 err = block->ops->disable(block);
269 if (err < 0)
270 dev_err(dsp->dev,
271 "error: cant disable block %d:%d\n",
272 block->type, block->index);
276 /* mark each block as free */
277 list_for_each_entry_safe(block, tmp, &module->block_list, module_list) {
278 list_del(&block->module_list);
279 list_move(&block->list, &dsp->free_block_list);
283 /* prepare the memory block to receive data from host - callers hold locks */
284 static int block_module_prepare(struct sst_module *module)
286 struct sst_mem_block *block;
287 int ret = 0;
289 /* enable each block so that's it'e ready for module P/S data */
290 list_for_each_entry(block, &module->block_list, module_list) {
292 if (block->ops && block->ops->enable) {
293 ret = block->ops->enable(block);
294 if (ret < 0) {
295 dev_err(module->dsp->dev,
296 "error: cant disable block %d:%d\n",
297 block->type, block->index);
298 goto err;
302 return ret;
304 err:
305 list_for_each_entry(block, &module->block_list, module_list) {
306 if (block->ops && block->ops->disable)
307 block->ops->disable(block);
309 return ret;
312 /* allocate memory blocks for static module addresses - callers hold locks */
313 static int block_alloc_fixed(struct sst_module *module,
314 struct sst_module_data *data)
316 struct sst_dsp *dsp = module->dsp;
317 struct sst_mem_block *block, *tmp;
318 u32 end = data->offset + data->size, block_end;
319 int err;
321 /* only IRAM/DRAM blocks are managed */
322 if (data->type != SST_MEM_IRAM && data->type != SST_MEM_DRAM)
323 return 0;
325 /* are blocks already attached to this module */
326 list_for_each_entry_safe(block, tmp, &module->block_list, module_list) {
328 /* force compacting mem blocks of the same data_type */
329 if (block->data_type != data->data_type)
330 continue;
332 block_end = block->offset + block->size;
334 /* find block that holds section */
335 if (data->offset >= block->offset && end < block_end)
336 return 0;
338 /* does block span more than 1 section */
339 if (data->offset >= block->offset && data->offset < block_end) {
341 err = block_alloc_contiguous(module, data,
342 block->offset + block->size,
343 data->size - block->size);
344 if (err < 0)
345 return -ENOMEM;
347 /* module already owns blocks */
348 return 0;
352 /* find first free blocks that can hold section in free list */
353 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
354 block_end = block->offset + block->size;
356 /* find block that holds section */
357 if (data->offset >= block->offset && end < block_end) {
359 /* add block */
360 block->data_type = data->data_type;
361 list_move(&block->list, &dsp->used_block_list);
362 list_add(&block->module_list, &module->block_list);
363 return 0;
366 /* does block span more than 1 section */
367 if (data->offset >= block->offset && data->offset < block_end) {
369 err = block_alloc_contiguous(module, data,
370 block->offset, data->size);
371 if (err < 0)
372 return -ENOMEM;
374 return 0;
379 return -ENOMEM;
382 /* Load fixed module data into DSP memory blocks */
383 int sst_module_insert_fixed_block(struct sst_module *module,
384 struct sst_module_data *data)
386 struct sst_dsp *dsp = module->dsp;
387 int ret;
389 mutex_lock(&dsp->mutex);
391 /* alloc blocks that includes this section */
392 ret = block_alloc_fixed(module, data);
393 if (ret < 0) {
394 dev_err(dsp->dev,
395 "error: no free blocks for section at offset 0x%x size 0x%x\n",
396 data->offset, data->size);
397 mutex_unlock(&dsp->mutex);
398 return -ENOMEM;
401 /* prepare DSP blocks for module copy */
402 ret = block_module_prepare(module);
403 if (ret < 0) {
404 dev_err(dsp->dev, "error: fw module prepare failed\n");
405 goto err;
408 /* copy partial module data to blocks */
409 sst_memcpy32(dsp->addr.lpe + data->offset, data->data, data->size);
411 mutex_unlock(&dsp->mutex);
412 return ret;
414 err:
415 block_module_remove(module);
416 mutex_unlock(&dsp->mutex);
417 return ret;
419 EXPORT_SYMBOL_GPL(sst_module_insert_fixed_block);
421 /* Unload entire module from DSP memory */
422 int sst_block_module_remove(struct sst_module *module)
424 struct sst_dsp *dsp = module->dsp;
426 mutex_lock(&dsp->mutex);
427 block_module_remove(module);
428 mutex_unlock(&dsp->mutex);
429 return 0;
431 EXPORT_SYMBOL_GPL(sst_block_module_remove);
433 /* register a DSP memory block for use with FW based modules */
434 struct sst_mem_block *sst_mem_block_register(struct sst_dsp *dsp, u32 offset,
435 u32 size, enum sst_mem_type type, struct sst_block_ops *ops, u32 index,
436 void *private)
438 struct sst_mem_block *block;
440 block = kzalloc(sizeof(*block), GFP_KERNEL);
441 if (block == NULL)
442 return NULL;
444 block->offset = offset;
445 block->size = size;
446 block->index = index;
447 block->type = type;
448 block->dsp = dsp;
449 block->private = private;
450 block->ops = ops;
452 mutex_lock(&dsp->mutex);
453 list_add(&block->list, &dsp->free_block_list);
454 mutex_unlock(&dsp->mutex);
456 return block;
458 EXPORT_SYMBOL_GPL(sst_mem_block_register);
460 /* unregister all DSP memory blocks */
461 void sst_mem_block_unregister_all(struct sst_dsp *dsp)
463 struct sst_mem_block *block, *tmp;
465 mutex_lock(&dsp->mutex);
467 /* unregister used blocks */
468 list_for_each_entry_safe(block, tmp, &dsp->used_block_list, list) {
469 list_del(&block->list);
470 kfree(block);
473 /* unregister free blocks */
474 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
475 list_del(&block->list);
476 kfree(block);
479 mutex_unlock(&dsp->mutex);
481 EXPORT_SYMBOL_GPL(sst_mem_block_unregister_all);
483 /* allocate scratch buffer blocks */
484 struct sst_module *sst_mem_block_alloc_scratch(struct sst_dsp *dsp)
486 struct sst_module *sst_module, *scratch;
487 struct sst_mem_block *block, *tmp;
488 u32 block_size;
489 int ret = 0;
491 scratch = kzalloc(sizeof(struct sst_module), GFP_KERNEL);
492 if (scratch == NULL)
493 return NULL;
495 mutex_lock(&dsp->mutex);
497 /* calculate required scratch size */
498 list_for_each_entry(sst_module, &dsp->module_list, list) {
499 if (scratch->s.size > sst_module->s.size)
500 scratch->s.size = scratch->s.size;
501 else
502 scratch->s.size = sst_module->s.size;
505 dev_dbg(dsp->dev, "scratch buffer required is %d bytes\n",
506 scratch->s.size);
508 /* init scratch module */
509 scratch->dsp = dsp;
510 scratch->s.type = SST_MEM_DRAM;
511 scratch->s.data_type = SST_DATA_S;
512 INIT_LIST_HEAD(&scratch->block_list);
514 /* check free blocks before looking at used blocks for space */
515 if (!list_empty(&dsp->free_block_list))
516 block = list_first_entry(&dsp->free_block_list,
517 struct sst_mem_block, list);
518 else
519 block = list_first_entry(&dsp->used_block_list,
520 struct sst_mem_block, list);
521 block_size = block->size;
523 /* allocate blocks for module scratch buffers */
524 dev_dbg(dsp->dev, "allocating scratch blocks\n");
525 ret = block_alloc(scratch, &scratch->s);
526 if (ret < 0) {
527 dev_err(dsp->dev, "error: can't alloc scratch blocks\n");
528 goto err;
531 /* assign the same offset of scratch to each module */
532 list_for_each_entry(sst_module, &dsp->module_list, list)
533 sst_module->s.offset = scratch->s.offset;
535 mutex_unlock(&dsp->mutex);
536 return scratch;
538 err:
539 list_for_each_entry_safe(block, tmp, &scratch->block_list, module_list)
540 list_del(&block->module_list);
541 mutex_unlock(&dsp->mutex);
542 return NULL;
544 EXPORT_SYMBOL_GPL(sst_mem_block_alloc_scratch);
546 /* free all scratch blocks */
547 void sst_mem_block_free_scratch(struct sst_dsp *dsp,
548 struct sst_module *scratch)
550 struct sst_mem_block *block, *tmp;
552 mutex_lock(&dsp->mutex);
554 list_for_each_entry_safe(block, tmp, &scratch->block_list, module_list)
555 list_del(&block->module_list);
557 mutex_unlock(&dsp->mutex);
559 EXPORT_SYMBOL_GPL(sst_mem_block_free_scratch);
561 /* get a module from it's unique ID */
562 struct sst_module *sst_module_get_from_id(struct sst_dsp *dsp, u32 id)
564 struct sst_module *module;
566 mutex_lock(&dsp->mutex);
568 list_for_each_entry(module, &dsp->module_list, list) {
569 if (module->id == id) {
570 mutex_unlock(&dsp->mutex);
571 return module;
575 mutex_unlock(&dsp->mutex);
576 return NULL;
578 EXPORT_SYMBOL_GPL(sst_module_get_from_id);