ASoC: Intel: Fix Audio DSP usage when IOMMU is enabled.
[deliverable/linux.git] / sound / soc / intel / sst-firmware.c
1 /*
2 * Intel SST Firmware Loader
3 *
4 * Copyright (C) 2013, Intel Corporation. All rights reserved.
5 *
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.
9 *
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.
14 *
15 */
16
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>
26
27 #include <asm/page.h>
28 #include <asm/pgtable.h>
29
30 #include "sst-dsp.h"
31 #include "sst-dsp-priv.h"
32
33 static void sst_memcpy32(volatile void __iomem *dest, void *src, u32 bytes)
34 {
35 u32 i;
36
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);
40 }
41
42 /* create new generic firmware object */
43 struct sst_fw *sst_fw_new(struct sst_dsp *dsp,
44 const struct firmware *fw, void *private)
45 {
46 struct sst_fw *sst_fw;
47 int err;
48
49 if (!dsp->ops->parse_fw)
50 return NULL;
51
52 sst_fw = kzalloc(sizeof(*sst_fw), GFP_KERNEL);
53 if (sst_fw == NULL)
54 return NULL;
55
56 sst_fw->dsp = dsp;
57 sst_fw->private = private;
58 sst_fw->size = fw->size;
59
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;
67 }
68
69 /* copy FW data to DMA-able memory */
70 memcpy((void *)sst_fw->dma_buf, (void *)fw->data, fw->size);
71
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;
77 }
78
79 mutex_lock(&dsp->mutex);
80 list_add(&sst_fw->list, &dsp->fw_list);
81 mutex_unlock(&dsp->mutex);
82
83 return sst_fw;
84
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;
91 }
92 EXPORT_SYMBOL_GPL(sst_fw_new);
93
94 /* free single firmware object */
95 void sst_fw_free(struct sst_fw *sst_fw)
96 {
97 struct sst_dsp *dsp = sst_fw->dsp;
98
99 mutex_lock(&dsp->mutex);
100 list_del(&sst_fw->list);
101 mutex_unlock(&dsp->mutex);
102
103 dma_free_coherent(dsp->dma_dev, sst_fw->size, sst_fw->dma_buf,
104 sst_fw->dmable_fw_paddr);
105 kfree(sst_fw);
106 }
107 EXPORT_SYMBOL_GPL(sst_fw_free);
108
109 /* free all firmware objects */
110 void sst_fw_free_all(struct sst_dsp *dsp)
111 {
112 struct sst_fw *sst_fw, *t;
113
114 mutex_lock(&dsp->mutex);
115 list_for_each_entry_safe(sst_fw, t, &dsp->fw_list, list) {
116
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);
121 }
122 mutex_unlock(&dsp->mutex);
123 }
124 EXPORT_SYMBOL_GPL(sst_fw_free_all);
125
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)
129 {
130 struct sst_dsp *dsp = sst_fw->dsp;
131 struct sst_module *sst_module;
132
133 sst_module = kzalloc(sizeof(*sst_module), GFP_KERNEL);
134 if (sst_module == NULL)
135 return NULL;
136
137 sst_module->id = template->id;
138 sst_module->dsp = dsp;
139 sst_module->sst_fw = sst_fw;
140
141 memcpy(&sst_module->s, &template->s, sizeof(struct sst_module_data));
142 memcpy(&sst_module->p, &template->p, sizeof(struct sst_module_data));
143
144 INIT_LIST_HEAD(&sst_module->block_list);
145
146 mutex_lock(&dsp->mutex);
147 list_add(&sst_module->list, &dsp->module_list);
148 mutex_unlock(&dsp->mutex);
149
150 return sst_module;
151 }
152 EXPORT_SYMBOL_GPL(sst_module_new);
153
154 /* free firmware module and remove from available list */
155 void sst_module_free(struct sst_module *sst_module)
156 {
157 struct sst_dsp *dsp = sst_module->dsp;
158
159 mutex_lock(&dsp->mutex);
160 list_del(&sst_module->list);
161 mutex_unlock(&dsp->mutex);
162
163 kfree(sst_module);
164 }
165 EXPORT_SYMBOL_GPL(sst_module_free);
166
167 static struct sst_mem_block *find_block(struct sst_dsp *dsp, int type,
168 u32 offset)
169 {
170 struct sst_mem_block *block;
171
172 list_for_each_entry(block, &dsp->free_block_list, list) {
173 if (block->type == type && block->offset == offset)
174 return block;
175 }
176
177 return NULL;
178 }
179
180 static int block_alloc_contiguous(struct sst_module *module,
181 struct sst_module_data *data, u32 offset, int size)
182 {
183 struct list_head tmp = LIST_HEAD_INIT(tmp);
184 struct sst_dsp *dsp = module->dsp;
185 struct sst_mem_block *block;
186
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;
192 }
193
194 list_move_tail(&block->list, &tmp);
195 offset += block->size;
196 size -= block->size;
197 }
198
199 list_for_each_entry(block, &tmp, list)
200 list_add(&block->module_list, &module->block_list);
201
202 list_splice(&tmp, &dsp->used_block_list);
203 return 0;
204 }
205
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)
209 {
210 struct sst_dsp *dsp = module->dsp;
211 struct sst_mem_block *block, *tmp;
212 int ret = 0;
213
214 if (data->size == 0)
215 return 0;
216
217 /* find first free whole blocks that can hold module */
218 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
219
220 /* ignore blocks with wrong type */
221 if (block->type != data->type)
222 continue;
223
224 if (data->size > block->size)
225 continue;
226
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;
235 }
236
237 /* then find free multiple blocks that can hold module */
238 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
239
240 /* ignore blocks with wrong type */
241 if (block->type != data->type)
242 continue;
243
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);
249 if (ret == 0)
250 return ret;
251 }
252 }
253
254 /* not enough free block space */
255 return -ENOMEM;
256 }
257
258 /* remove module from memory - callers hold locks */
259 static void block_module_remove(struct sst_module *module)
260 {
261 struct sst_mem_block *block, *tmp;
262 struct sst_dsp *dsp = module->dsp;
263 int err;
264
265 /* disable each block */
266 list_for_each_entry(block, &module->block_list, module_list) {
267
268 if (block->ops && block->ops->disable) {
269 err = block->ops->disable(block);
270 if (err < 0)
271 dev_err(dsp->dev,
272 "error: cant disable block %d:%d\n",
273 block->type, block->index);
274 }
275 }
276
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);
281 }
282 }
283
284 /* prepare the memory block to receive data from host - callers hold locks */
285 static int block_module_prepare(struct sst_module *module)
286 {
287 struct sst_mem_block *block;
288 int ret = 0;
289
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) {
292
293 if (block->ops && block->ops->enable) {
294 ret = block->ops->enable(block);
295 if (ret < 0) {
296 dev_err(module->dsp->dev,
297 "error: cant disable block %d:%d\n",
298 block->type, block->index);
299 goto err;
300 }
301 }
302 }
303 return ret;
304
305 err:
306 list_for_each_entry(block, &module->block_list, module_list) {
307 if (block->ops && block->ops->disable)
308 block->ops->disable(block);
309 }
310 return ret;
311 }
312
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)
316 {
317 struct sst_dsp *dsp = module->dsp;
318 struct sst_mem_block *block, *tmp;
319 u32 end = data->offset + data->size, block_end;
320 int err;
321
322 /* only IRAM/DRAM blocks are managed */
323 if (data->type != SST_MEM_IRAM && data->type != SST_MEM_DRAM)
324 return 0;
325
326 /* are blocks already attached to this module */
327 list_for_each_entry_safe(block, tmp, &module->block_list, module_list) {
328
329 /* force compacting mem blocks of the same data_type */
330 if (block->data_type != data->data_type)
331 continue;
332
333 block_end = block->offset + block->size;
334
335 /* find block that holds section */
336 if (data->offset >= block->offset && end < block_end)
337 return 0;
338
339 /* does block span more than 1 section */
340 if (data->offset >= block->offset && data->offset < block_end) {
341
342 err = block_alloc_contiguous(module, data,
343 block->offset + block->size,
344 data->size - block->size + data->offset - block->offset);
345 if (err < 0)
346 return -ENOMEM;
347
348 /* module already owns blocks */
349 return 0;
350 }
351 }
352
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;
356
357 /* find block that holds section */
358 if (data->offset >= block->offset && end < block_end) {
359
360 /* add block */
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);
364 return 0;
365 }
366
367 /* does block span more than 1 section */
368 if (data->offset >= block->offset && data->offset < block_end) {
369
370 err = block_alloc_contiguous(module, data,
371 block->offset + block->size,
372 data->size - block->size);
373 if (err < 0)
374 return -ENOMEM;
375
376 return 0;
377 }
378
379 }
380
381 return -ENOMEM;
382 }
383
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)
387 {
388 struct sst_dsp *dsp = module->dsp;
389 int ret;
390
391 mutex_lock(&dsp->mutex);
392
393 /* alloc blocks that includes this section */
394 ret = block_alloc_fixed(module, data);
395 if (ret < 0) {
396 dev_err(dsp->dev,
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);
400 return -ENOMEM;
401 }
402
403 /* prepare DSP blocks for module copy */
404 ret = block_module_prepare(module);
405 if (ret < 0) {
406 dev_err(dsp->dev, "error: fw module prepare failed\n");
407 goto err;
408 }
409
410 /* copy partial module data to blocks */
411 sst_memcpy32(dsp->addr.lpe + data->offset, data->data, data->size);
412
413 mutex_unlock(&dsp->mutex);
414 return ret;
415
416 err:
417 block_module_remove(module);
418 mutex_unlock(&dsp->mutex);
419 return ret;
420 }
421 EXPORT_SYMBOL_GPL(sst_module_insert_fixed_block);
422
423 /* Unload entire module from DSP memory */
424 int sst_block_module_remove(struct sst_module *module)
425 {
426 struct sst_dsp *dsp = module->dsp;
427
428 mutex_lock(&dsp->mutex);
429 block_module_remove(module);
430 mutex_unlock(&dsp->mutex);
431 return 0;
432 }
433 EXPORT_SYMBOL_GPL(sst_block_module_remove);
434
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,
438 void *private)
439 {
440 struct sst_mem_block *block;
441
442 block = kzalloc(sizeof(*block), GFP_KERNEL);
443 if (block == NULL)
444 return NULL;
445
446 block->offset = offset;
447 block->size = size;
448 block->index = index;
449 block->type = type;
450 block->dsp = dsp;
451 block->private = private;
452 block->ops = ops;
453
454 mutex_lock(&dsp->mutex);
455 list_add(&block->list, &dsp->free_block_list);
456 mutex_unlock(&dsp->mutex);
457
458 return block;
459 }
460 EXPORT_SYMBOL_GPL(sst_mem_block_register);
461
462 /* unregister all DSP memory blocks */
463 void sst_mem_block_unregister_all(struct sst_dsp *dsp)
464 {
465 struct sst_mem_block *block, *tmp;
466
467 mutex_lock(&dsp->mutex);
468
469 /* unregister used blocks */
470 list_for_each_entry_safe(block, tmp, &dsp->used_block_list, list) {
471 list_del(&block->list);
472 kfree(block);
473 }
474
475 /* unregister free blocks */
476 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
477 list_del(&block->list);
478 kfree(block);
479 }
480
481 mutex_unlock(&dsp->mutex);
482 }
483 EXPORT_SYMBOL_GPL(sst_mem_block_unregister_all);
484
485 /* allocate scratch buffer blocks */
486 struct sst_module *sst_mem_block_alloc_scratch(struct sst_dsp *dsp)
487 {
488 struct sst_module *sst_module, *scratch;
489 struct sst_mem_block *block, *tmp;
490 u32 block_size;
491 int ret = 0;
492
493 scratch = kzalloc(sizeof(struct sst_module), GFP_KERNEL);
494 if (scratch == NULL)
495 return NULL;
496
497 mutex_lock(&dsp->mutex);
498
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;
503 else
504 scratch->s.size = sst_module->s.size;
505 }
506
507 dev_dbg(dsp->dev, "scratch buffer required is %d bytes\n",
508 scratch->s.size);
509
510 /* init scratch module */
511 scratch->dsp = dsp;
512 scratch->s.type = SST_MEM_DRAM;
513 scratch->s.data_type = SST_DATA_S;
514 INIT_LIST_HEAD(&scratch->block_list);
515
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);
520 else
521 block = list_first_entry(&dsp->used_block_list,
522 struct sst_mem_block, list);
523 block_size = block->size;
524
525 /* allocate blocks for module scratch buffers */
526 dev_dbg(dsp->dev, "allocating scratch blocks\n");
527 ret = block_alloc(scratch, &scratch->s);
528 if (ret < 0) {
529 dev_err(dsp->dev, "error: can't alloc scratch blocks\n");
530 goto err;
531 }
532
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;
536
537 mutex_unlock(&dsp->mutex);
538 return scratch;
539
540 err:
541 list_for_each_entry_safe(block, tmp, &scratch->block_list, module_list)
542 list_del(&block->module_list);
543 mutex_unlock(&dsp->mutex);
544 return NULL;
545 }
546 EXPORT_SYMBOL_GPL(sst_mem_block_alloc_scratch);
547
548 /* free all scratch blocks */
549 void sst_mem_block_free_scratch(struct sst_dsp *dsp,
550 struct sst_module *scratch)
551 {
552 struct sst_mem_block *block, *tmp;
553
554 mutex_lock(&dsp->mutex);
555
556 list_for_each_entry_safe(block, tmp, &scratch->block_list, module_list)
557 list_del(&block->module_list);
558
559 mutex_unlock(&dsp->mutex);
560 }
561 EXPORT_SYMBOL_GPL(sst_mem_block_free_scratch);
562
563 /* get a module from it's unique ID */
564 struct sst_module *sst_module_get_from_id(struct sst_dsp *dsp, u32 id)
565 {
566 struct sst_module *module;
567
568 mutex_lock(&dsp->mutex);
569
570 list_for_each_entry(module, &dsp->module_list, list) {
571 if (module->id == id) {
572 mutex_unlock(&dsp->mutex);
573 return module;
574 }
575 }
576
577 mutex_unlock(&dsp->mutex);
578 return NULL;
579 }
580 EXPORT_SYMBOL_GPL(sst_module_get_from_id);
This page took 0.042484 seconds and 5 git commands to generate.