]> git.karo-electronics.de Git - karo-tx-linux.git/blob - sound/soc/intel/sst-firmware.c
Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs
[karo-tx-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         err = dma_coerce_mask_and_coherent(dsp->dev, DMA_BIT_MASK(32));
61         if (err < 0) {
62                 kfree(sst_fw);
63                 return NULL;
64         }
65
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");
71                 kfree(sst_fw);
72                 return NULL;
73         }
74
75         /* copy FW data to DMA-able memory */
76         memcpy((void *)sst_fw->dma_buf, (void *)fw->data, fw->size);
77
78         /* call core specific FW paser to load FW data into DSP */
79         err = dsp->ops->parse_fw(sst_fw);
80         if (err < 0) {
81                 dev_err(dsp->dev, "error: parse fw failed %d\n", err);
82                 goto parse_err;
83         }
84
85         mutex_lock(&dsp->mutex);
86         list_add(&sst_fw->list, &dsp->fw_list);
87         mutex_unlock(&dsp->mutex);
88
89         return sst_fw;
90
91 parse_err:
92         dma_free_coherent(dsp->dev, sst_fw->size,
93                                 sst_fw->dma_buf,
94                                 sst_fw->dmable_fw_paddr);
95         kfree(sst_fw);
96         return NULL;
97 }
98 EXPORT_SYMBOL_GPL(sst_fw_new);
99
100 /* free single firmware object */
101 void sst_fw_free(struct sst_fw *sst_fw)
102 {
103         struct sst_dsp *dsp = sst_fw->dsp;
104
105         mutex_lock(&dsp->mutex);
106         list_del(&sst_fw->list);
107         mutex_unlock(&dsp->mutex);
108
109         dma_free_coherent(dsp->dev, sst_fw->size, sst_fw->dma_buf,
110                         sst_fw->dmable_fw_paddr);
111         kfree(sst_fw);
112 }
113 EXPORT_SYMBOL_GPL(sst_fw_free);
114
115 /* free all firmware objects */
116 void sst_fw_free_all(struct sst_dsp *dsp)
117 {
118         struct sst_fw *sst_fw, *t;
119
120         mutex_lock(&dsp->mutex);
121         list_for_each_entry_safe(sst_fw, t, &dsp->fw_list, list) {
122
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);
126                 kfree(sst_fw);
127         }
128         mutex_unlock(&dsp->mutex);
129 }
130 EXPORT_SYMBOL_GPL(sst_fw_free_all);
131
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)
135 {
136         struct sst_dsp *dsp = sst_fw->dsp;
137         struct sst_module *sst_module;
138
139         sst_module = kzalloc(sizeof(*sst_module), GFP_KERNEL);
140         if (sst_module == NULL)
141                 return NULL;
142
143         sst_module->id = template->id;
144         sst_module->dsp = dsp;
145         sst_module->sst_fw = sst_fw;
146
147         memcpy(&sst_module->s, &template->s, sizeof(struct sst_module_data));
148         memcpy(&sst_module->p, &template->p, sizeof(struct sst_module_data));
149
150         INIT_LIST_HEAD(&sst_module->block_list);
151
152         mutex_lock(&dsp->mutex);
153         list_add(&sst_module->list, &dsp->module_list);
154         mutex_unlock(&dsp->mutex);
155
156         return sst_module;
157 }
158 EXPORT_SYMBOL_GPL(sst_module_new);
159
160 /* free firmware module and remove from available list */
161 void sst_module_free(struct sst_module *sst_module)
162 {
163         struct sst_dsp *dsp = sst_module->dsp;
164
165         mutex_lock(&dsp->mutex);
166         list_del(&sst_module->list);
167         mutex_unlock(&dsp->mutex);
168
169         kfree(sst_module);
170 }
171 EXPORT_SYMBOL_GPL(sst_module_free);
172
173 static struct sst_mem_block *find_block(struct sst_dsp *dsp, int type,
174         u32 offset)
175 {
176         struct sst_mem_block *block;
177
178         list_for_each_entry(block, &dsp->free_block_list, list) {
179                 if (block->type == type && block->offset == offset)
180                         return block;
181         }
182
183         return NULL;
184 }
185
186 static int block_alloc_contiguous(struct sst_module *module,
187         struct sst_module_data *data, u32 offset, int size)
188 {
189         struct list_head tmp = LIST_HEAD_INIT(tmp);
190         struct sst_dsp *dsp = module->dsp;
191         struct sst_mem_block *block;
192
193         while (size > 0) {
194                 block = find_block(dsp, data->type, offset);
195                 if (!block) {
196                         list_splice(&tmp, &dsp->free_block_list);
197                         return -ENOMEM;
198                 }
199
200                 list_move_tail(&block->list, &tmp);
201                 offset += block->size;
202                 size -= block->size;
203         }
204
205         list_splice(&tmp, &dsp->used_block_list);
206         return 0;
207 }
208
209 /* allocate free DSP blocks for module data - callers hold locks */
210 static int block_alloc(struct sst_module *module,
211         struct sst_module_data *data)
212 {
213         struct sst_dsp *dsp = module->dsp;
214         struct sst_mem_block *block, *tmp;
215         int ret = 0;
216
217         if (data->size == 0)
218                 return 0;
219
220         /* find first free whole blocks that can hold module */
221         list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
222
223                 /* ignore blocks with wrong type */
224                 if (block->type != data->type)
225                         continue;
226
227                 if (data->size > block->size)
228                         continue;
229
230                 data->offset = block->offset;
231                 block->data_type = data->data_type;
232                 block->bytes_used = data->size % block->size;
233                 list_add(&block->module_list, &module->block_list);
234                 list_move(&block->list, &dsp->used_block_list);
235                 dev_dbg(dsp->dev, " *module %d added block %d:%d\n",
236                         module->id, block->type, block->index);
237                 return 0;
238         }
239
240         /* then find free multiple blocks that can hold module */
241         list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
242
243                 /* ignore blocks with wrong type */
244                 if (block->type != data->type)
245                         continue;
246
247                 /* do we span > 1 blocks */
248                 if (data->size > block->size) {
249                         ret = block_alloc_contiguous(module, data,
250                                 block->offset + block->size,
251                                 data->size - block->size);
252                         if (ret == 0)
253                                 return ret;
254                 }
255         }
256
257         /* not enough free block space */
258         return -ENOMEM;
259 }
260
261 /* remove module from memory - callers hold locks */
262 static void block_module_remove(struct sst_module *module)
263 {
264         struct sst_mem_block *block, *tmp;
265         struct sst_dsp *dsp = module->dsp;
266         int err;
267
268         /* disable each block  */
269         list_for_each_entry(block, &module->block_list, module_list) {
270
271                 if (block->ops && block->ops->disable) {
272                         err = block->ops->disable(block);
273                         if (err < 0)
274                                 dev_err(dsp->dev,
275                                         "error: cant disable block %d:%d\n",
276                                         block->type, block->index);
277                 }
278         }
279
280         /* mark each block as free */
281         list_for_each_entry_safe(block, tmp, &module->block_list, module_list) {
282                 list_del(&block->module_list);
283                 list_move(&block->list, &dsp->free_block_list);
284         }
285 }
286
287 /* prepare the memory block to receive data from host - callers hold locks */
288 static int block_module_prepare(struct sst_module *module)
289 {
290         struct sst_mem_block *block;
291         int ret = 0;
292
293         /* enable each block so that's it'e ready for module P/S data */
294         list_for_each_entry(block, &module->block_list, module_list) {
295
296                 if (block->ops && block->ops->enable) {
297                         ret = block->ops->enable(block);
298                         if (ret < 0) {
299                                 dev_err(module->dsp->dev,
300                                         "error: cant disable block %d:%d\n",
301                                         block->type, block->index);
302                                 goto err;
303                         }
304                 }
305         }
306         return ret;
307
308 err:
309         list_for_each_entry(block, &module->block_list, module_list) {
310                 if (block->ops && block->ops->disable)
311                         block->ops->disable(block);
312         }
313         return ret;
314 }
315
316 /* allocate memory blocks for static module addresses - callers hold locks */
317 static int block_alloc_fixed(struct sst_module *module,
318         struct sst_module_data *data)
319 {
320         struct sst_dsp *dsp = module->dsp;
321         struct sst_mem_block *block, *tmp;
322         u32 end = data->offset + data->size, block_end;
323         int err;
324
325         /* only IRAM/DRAM blocks are managed */
326         if (data->type != SST_MEM_IRAM && data->type != SST_MEM_DRAM)
327                 return 0;
328
329         /* are blocks already attached to this module */
330         list_for_each_entry_safe(block, tmp, &module->block_list, module_list) {
331
332                 /* force compacting mem blocks of the same data_type */
333                 if (block->data_type != data->data_type)
334                         continue;
335
336                 block_end = block->offset + block->size;
337
338                 /* find block that holds section */
339                 if (data->offset >= block->offset && end < block_end)
340                         return 0;
341
342                 /* does block span more than 1 section */
343                 if (data->offset >= block->offset && data->offset < block_end) {
344
345                         err = block_alloc_contiguous(module, data,
346                                 block->offset + block->size,
347                                 data->size - block->size + data->offset - block->offset);
348                         if (err < 0)
349                                 return -ENOMEM;
350
351                         /* module already owns blocks */
352                         return 0;
353                 }
354         }
355
356         /* find first free blocks that can hold section in free list */
357         list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
358                 block_end = block->offset + block->size;
359
360                 /* find block that holds section */
361                 if (data->offset >= block->offset && end < block_end) {
362
363                         /* add block */
364                         block->data_type = data->data_type;
365                         list_move(&block->list, &dsp->used_block_list);
366                         list_add(&block->module_list, &module->block_list);
367                         return 0;
368                 }
369
370                 /* does block span more than 1 section */
371                 if (data->offset >= block->offset && data->offset < block_end) {
372
373                         err = block_alloc_contiguous(module, data,
374                                 block->offset + block->size,
375                                 data->size - block->size);
376                         if (err < 0)
377                                 return -ENOMEM;
378
379                         /* add block */
380                         block->data_type = data->data_type;
381                         list_move(&block->list, &dsp->used_block_list);
382                         list_add(&block->module_list, &module->block_list);
383                         return 0;
384                 }
385
386         }
387
388         return -ENOMEM;
389 }
390
391 /* Load fixed module data into DSP memory blocks */
392 int sst_module_insert_fixed_block(struct sst_module *module,
393         struct sst_module_data *data)
394 {
395         struct sst_dsp *dsp = module->dsp;
396         int ret;
397
398         mutex_lock(&dsp->mutex);
399
400         /* alloc blocks that includes this section */
401         ret = block_alloc_fixed(module, data);
402         if (ret < 0) {
403                 dev_err(dsp->dev,
404                         "error: no free blocks for section at offset 0x%x size 0x%x\n",
405                         data->offset, data->size);
406                 mutex_unlock(&dsp->mutex);
407                 return -ENOMEM;
408         }
409
410         /* prepare DSP blocks for module copy */
411         ret = block_module_prepare(module);
412         if (ret < 0) {
413                 dev_err(dsp->dev, "error: fw module prepare failed\n");
414                 goto err;
415         }
416
417         /* copy partial module data to blocks */
418         sst_memcpy32(dsp->addr.lpe + data->offset, data->data, data->size);
419
420         mutex_unlock(&dsp->mutex);
421         return ret;
422
423 err:
424         block_module_remove(module);
425         mutex_unlock(&dsp->mutex);
426         return ret;
427 }
428 EXPORT_SYMBOL_GPL(sst_module_insert_fixed_block);
429
430 /* Unload entire module from DSP memory */
431 int sst_block_module_remove(struct sst_module *module)
432 {
433         struct sst_dsp *dsp = module->dsp;
434
435         mutex_lock(&dsp->mutex);
436         block_module_remove(module);
437         mutex_unlock(&dsp->mutex);
438         return 0;
439 }
440 EXPORT_SYMBOL_GPL(sst_block_module_remove);
441
442 /* register a DSP memory block for use with FW based modules */
443 struct sst_mem_block *sst_mem_block_register(struct sst_dsp *dsp, u32 offset,
444         u32 size, enum sst_mem_type type, struct sst_block_ops *ops, u32 index,
445         void *private)
446 {
447         struct sst_mem_block *block;
448
449         block = kzalloc(sizeof(*block), GFP_KERNEL);
450         if (block == NULL)
451                 return NULL;
452
453         block->offset = offset;
454         block->size = size;
455         block->index = index;
456         block->type = type;
457         block->dsp = dsp;
458         block->private = private;
459         block->ops = ops;
460
461         mutex_lock(&dsp->mutex);
462         list_add(&block->list, &dsp->free_block_list);
463         mutex_unlock(&dsp->mutex);
464
465         return block;
466 }
467 EXPORT_SYMBOL_GPL(sst_mem_block_register);
468
469 /* unregister all DSP memory blocks */
470 void sst_mem_block_unregister_all(struct sst_dsp *dsp)
471 {
472         struct sst_mem_block *block, *tmp;
473
474         mutex_lock(&dsp->mutex);
475
476         /* unregister used blocks */
477         list_for_each_entry_safe(block, tmp, &dsp->used_block_list, list) {
478                 list_del(&block->list);
479                 kfree(block);
480         }
481
482         /* unregister free blocks */
483         list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
484                 list_del(&block->list);
485                 kfree(block);
486         }
487
488         mutex_unlock(&dsp->mutex);
489 }
490 EXPORT_SYMBOL_GPL(sst_mem_block_unregister_all);
491
492 /* allocate scratch buffer blocks */
493 struct sst_module *sst_mem_block_alloc_scratch(struct sst_dsp *dsp)
494 {
495         struct sst_module *sst_module, *scratch;
496         struct sst_mem_block *block, *tmp;
497         u32 block_size;
498         int ret = 0;
499
500         scratch = kzalloc(sizeof(struct sst_module), GFP_KERNEL);
501         if (scratch == NULL)
502                 return NULL;
503
504         mutex_lock(&dsp->mutex);
505
506         /* calculate required scratch size */
507         list_for_each_entry(sst_module, &dsp->module_list, list) {
508                 if (scratch->s.size > sst_module->s.size)
509                         scratch->s.size = scratch->s.size;
510                 else
511                         scratch->s.size = sst_module->s.size;
512         }
513
514         dev_dbg(dsp->dev, "scratch buffer required is %d bytes\n",
515                 scratch->s.size);
516
517         /* init scratch module */
518         scratch->dsp = dsp;
519         scratch->s.type = SST_MEM_DRAM;
520         scratch->s.data_type = SST_DATA_S;
521         INIT_LIST_HEAD(&scratch->block_list);
522
523         /* check free blocks before looking at used blocks for space */
524         if (!list_empty(&dsp->free_block_list))
525                 block = list_first_entry(&dsp->free_block_list,
526                         struct sst_mem_block, list);
527         else
528                 block = list_first_entry(&dsp->used_block_list,
529                         struct sst_mem_block, list);
530         block_size = block->size;
531
532         /* allocate blocks for module scratch buffers */
533         dev_dbg(dsp->dev, "allocating scratch blocks\n");
534         ret = block_alloc(scratch, &scratch->s);
535         if (ret < 0) {
536                 dev_err(dsp->dev, "error: can't alloc scratch blocks\n");
537                 goto err;
538         }
539
540         /* assign the same offset of scratch to each module */
541         list_for_each_entry(sst_module, &dsp->module_list, list)
542                 sst_module->s.offset = scratch->s.offset;
543
544         mutex_unlock(&dsp->mutex);
545         return scratch;
546
547 err:
548         list_for_each_entry_safe(block, tmp, &scratch->block_list, module_list)
549                 list_del(&block->module_list);
550         mutex_unlock(&dsp->mutex);
551         return NULL;
552 }
553 EXPORT_SYMBOL_GPL(sst_mem_block_alloc_scratch);
554
555 /* free all scratch blocks */
556 void sst_mem_block_free_scratch(struct sst_dsp *dsp,
557         struct sst_module *scratch)
558 {
559         struct sst_mem_block *block, *tmp;
560
561         mutex_lock(&dsp->mutex);
562
563         list_for_each_entry_safe(block, tmp, &scratch->block_list, module_list)
564                 list_del(&block->module_list);
565
566         mutex_unlock(&dsp->mutex);
567 }
568 EXPORT_SYMBOL_GPL(sst_mem_block_free_scratch);
569
570 /* get a module from it's unique ID */
571 struct sst_module *sst_module_get_from_id(struct sst_dsp *dsp, u32 id)
572 {
573         struct sst_module *module;
574
575         mutex_lock(&dsp->mutex);
576
577         list_for_each_entry(module, &dsp->module_list, list) {
578                 if (module->id == id) {
579                         mutex_unlock(&dsp->mutex);
580                         return module;
581                 }
582         }
583
584         mutex_unlock(&dsp->mutex);
585         return NULL;
586 }
587 EXPORT_SYMBOL_GPL(sst_module_get_from_id);