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
, data
->size
);
253 /* not enough free block space */
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
;
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
);
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
;
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
);
295 dev_err(module
->dsp
->dev
,
296 "error: cant disable block %d:%d\n",
297 block
->type
, block
->index
);
305 list_for_each_entry(block
, &module
->block_list
, module_list
) {
306 if (block
->ops
&& block
->ops
->disable
)
307 block
->ops
->disable(block
);
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
;
321 /* only IRAM/DRAM blocks are managed */
322 if (data
->type
!= SST_MEM_IRAM
&& data
->type
!= SST_MEM_DRAM
)
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
)
332 block_end
= block
->offset
+ block
->size
;
334 /* find block that holds section */
335 if (data
->offset
>= block
->offset
&& end
< block_end
)
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
);
347 /* module already owns blocks */
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
) {
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
);
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
);
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
;
389 mutex_lock(&dsp
->mutex
);
391 /* alloc blocks that includes this section */
392 ret
= block_alloc_fixed(module
, data
);
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
);
401 /* prepare DSP blocks for module copy */
402 ret
= block_module_prepare(module
);
404 dev_err(dsp
->dev
, "error: fw module prepare failed\n");
408 /* copy partial module data to blocks */
409 sst_memcpy32(dsp
->addr
.lpe
+ data
->offset
, data
->data
, data
->size
);
411 mutex_unlock(&dsp
->mutex
);
415 block_module_remove(module
);
416 mutex_unlock(&dsp
->mutex
);
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
);
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
,
438 struct sst_mem_block
*block
;
440 block
= kzalloc(sizeof(*block
), GFP_KERNEL
);
444 block
->offset
= offset
;
446 block
->index
= index
;
449 block
->private = private;
452 mutex_lock(&dsp
->mutex
);
453 list_add(&block
->list
, &dsp
->free_block_list
);
454 mutex_unlock(&dsp
->mutex
);
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
);
473 /* unregister free blocks */
474 list_for_each_entry_safe(block
, tmp
, &dsp
->free_block_list
, list
) {
475 list_del(&block
->list
);
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
;
491 scratch
= kzalloc(sizeof(struct sst_module
), GFP_KERNEL
);
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
;
502 scratch
->s
.size
= sst_module
->s
.size
;
505 dev_dbg(dsp
->dev
, "scratch buffer required is %d bytes\n",
508 /* init scratch module */
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
);
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
);
527 dev_err(dsp
->dev
, "error: can't alloc scratch blocks\n");
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
);
539 list_for_each_entry_safe(block
, tmp
, &scratch
->block_list
, module_list
)
540 list_del(&block
->module_list
);
541 mutex_unlock(&dsp
->mutex
);
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
);
575 mutex_unlock(&dsp
->mutex
);
578 EXPORT_SYMBOL_GPL(sst_module_get_from_id
);