blob: b3f9489794a6acad776fc2cf7670b6e12ab06bf6 [file] [log] [blame]
Mark Browna4b12992014-03-12 23:04:35 +00001/*
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>
Liam Girdwoode9600bc2014-10-28 17:37:12 +000026#include <linux/acpi.h>
27
28/* supported DMA engine drivers */
29#include <linux/platform_data/dma-dw.h>
30#include <linux/dma/dw.h>
Mark Browna4b12992014-03-12 23:04:35 +000031
32#include <asm/page.h>
33#include <asm/pgtable.h>
34
35#include "sst-dsp.h"
36#include "sst-dsp-priv.h"
37
Liam Girdwoode9600bc2014-10-28 17:37:12 +000038#define SST_DMA_RESOURCES 2
39#define SST_DSP_DMA_MAX_BURST 0x3
40#define SST_HSW_BLOCK_ANY 0xffffffff
41
42#define SST_HSW_MASK_DMA_ADDR_DSP 0xfff00000
43
44struct sst_dma {
45 struct sst_dsp *sst;
46
47 struct dw_dma_chip *chip;
48
49 struct dma_async_tx_descriptor *desc;
50 struct dma_chan *ch;
51};
Liam Girdwood555f8a802014-05-05 17:31:37 +010052
Vinod Koul7f266802014-10-20 20:54:34 +053053static inline void sst_memcpy32(volatile void __iomem *dest, void *src, u32 bytes)
Mark Browna4b12992014-03-12 23:04:35 +000054{
Vinod Koul7f266802014-10-20 20:54:34 +053055 /* __iowrite32_copy use 32bit size values so divide by 4 */
56 __iowrite32_copy((void *)dest, src, bytes/4);
Mark Browna4b12992014-03-12 23:04:35 +000057}
58
Liam Girdwoode9600bc2014-10-28 17:37:12 +000059static void sst_dma_transfer_complete(void *arg)
60{
61 struct sst_dsp *sst = (struct sst_dsp *)arg;
62
63 dev_dbg(sst->dev, "DMA: callback\n");
64}
65
66static int sst_dsp_dma_copy(struct sst_dsp *sst, dma_addr_t dest_addr,
67 dma_addr_t src_addr, size_t size)
68{
69 struct dma_async_tx_descriptor *desc;
70 struct sst_dma *dma = sst->dma;
71
72 if (dma->ch == NULL) {
73 dev_err(sst->dev, "error: no DMA channel\n");
74 return -ENODEV;
75 }
76
77 dev_dbg(sst->dev, "DMA: src: 0x%lx dest 0x%lx size %zu\n",
78 (unsigned long)src_addr, (unsigned long)dest_addr, size);
79
80 desc = dma->ch->device->device_prep_dma_memcpy(dma->ch, dest_addr,
81 src_addr, size, DMA_CTRL_ACK);
82 if (!desc){
83 dev_err(sst->dev, "error: dma prep memcpy failed\n");
84 return -EINVAL;
85 }
86
87 desc->callback = sst_dma_transfer_complete;
88 desc->callback_param = sst;
89
90 desc->tx_submit(desc);
91 dma_wait_for_async_tx(desc);
92
93 return 0;
94}
95
96/* copy to DSP */
97int sst_dsp_dma_copyto(struct sst_dsp *sst, dma_addr_t dest_addr,
98 dma_addr_t src_addr, size_t size)
99{
100 return sst_dsp_dma_copy(sst, dest_addr | SST_HSW_MASK_DMA_ADDR_DSP,
101 src_addr, size);
102}
103EXPORT_SYMBOL_GPL(sst_dsp_dma_copyto);
104
105/* copy from DSP */
106int sst_dsp_dma_copyfrom(struct sst_dsp *sst, dma_addr_t dest_addr,
107 dma_addr_t src_addr, size_t size)
108{
109 return sst_dsp_dma_copy(sst, dest_addr,
110 src_addr | SST_HSW_MASK_DMA_ADDR_DSP, size);
111}
112EXPORT_SYMBOL_GPL(sst_dsp_dma_copyfrom);
113
114/* remove module from memory - callers hold locks */
115static void block_list_remove(struct sst_dsp *dsp,
116 struct list_head *block_list)
117{
118 struct sst_mem_block *block, *tmp;
119 int err;
120
121 /* disable each block */
122 list_for_each_entry(block, block_list, module_list) {
123
124 if (block->ops && block->ops->disable) {
125 err = block->ops->disable(block);
126 if (err < 0)
127 dev_err(dsp->dev,
128 "error: cant disable block %d:%d\n",
129 block->type, block->index);
130 }
131 }
132
133 /* mark each block as free */
134 list_for_each_entry_safe(block, tmp, block_list, module_list) {
135 list_del(&block->module_list);
136 list_move(&block->list, &dsp->free_block_list);
137 dev_dbg(dsp->dev, "block freed %d:%d at offset 0x%x\n",
138 block->type, block->index, block->offset);
139 }
140}
141
142/* prepare the memory block to receive data from host - callers hold locks */
143static int block_list_prepare(struct sst_dsp *dsp,
144 struct list_head *block_list)
145{
146 struct sst_mem_block *block;
147 int ret = 0;
148
149 /* enable each block so that's it'e ready for data */
150 list_for_each_entry(block, block_list, module_list) {
151
Jie Yang35c0a8c2014-10-30 21:21:52 +0800152 if (block->ops && block->ops->enable && !block->users) {
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000153 ret = block->ops->enable(block);
154 if (ret < 0) {
155 dev_err(dsp->dev,
156 "error: cant disable block %d:%d\n",
157 block->type, block->index);
158 goto err;
159 }
160 }
161 }
162 return ret;
163
164err:
165 list_for_each_entry(block, block_list, module_list) {
166 if (block->ops && block->ops->disable)
167 block->ops->disable(block);
168 }
169 return ret;
170}
171
kbuild test robot137f6d52014-10-29 15:40:27 +0000172static struct dw_dma_platform_data dw_pdata = {
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000173 .is_private = 1,
174 .chan_allocation_order = CHAN_ALLOCATION_ASCENDING,
175 .chan_priority = CHAN_PRIORITY_ASCENDING,
176};
177
178static struct dw_dma_chip *dw_probe(struct device *dev, struct resource *mem,
179 int irq)
180{
181 struct dw_dma_chip *chip;
182 int err;
183
184 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
185 if (!chip)
186 return ERR_PTR(-ENOMEM);
187
188 chip->irq = irq;
189 chip->regs = devm_ioremap_resource(dev, mem);
190 if (IS_ERR(chip->regs))
191 return ERR_CAST(chip->regs);
192
193 err = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(31));
194 if (err)
195 return ERR_PTR(err);
196
197 chip->dev = dev;
198 err = dw_dma_probe(chip, &dw_pdata);
199 if (err)
200 return ERR_PTR(err);
201
202 return chip;
203}
204
205static void dw_remove(struct dw_dma_chip *chip)
206{
207 dw_dma_remove(chip);
208}
209
210static bool dma_chan_filter(struct dma_chan *chan, void *param)
211{
212 struct sst_dsp *dsp = (struct sst_dsp *)param;
213
214 return chan->device->dev == dsp->dma_dev;
215}
216
217int sst_dsp_dma_get_channel(struct sst_dsp *dsp, int chan_id)
218{
219 struct sst_dma *dma = dsp->dma;
220 struct dma_slave_config slave;
221 dma_cap_mask_t mask;
222 int ret;
223
224 /* The Intel MID DMA engine driver needs the slave config set but
225 * Synopsis DMA engine driver safely ignores the slave config */
226 dma_cap_zero(mask);
227 dma_cap_set(DMA_SLAVE, mask);
228 dma_cap_set(DMA_MEMCPY, mask);
229
230 dma->ch = dma_request_channel(mask, dma_chan_filter, dsp);
231 if (dma->ch == NULL) {
232 dev_err(dsp->dev, "error: DMA request channel failed\n");
233 return -EIO;
234 }
235
236 memset(&slave, 0, sizeof(slave));
237 slave.direction = DMA_MEM_TO_DEV;
238 slave.src_addr_width =
239 slave.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
240 slave.src_maxburst = slave.dst_maxburst = SST_DSP_DMA_MAX_BURST;
241
242 ret = dmaengine_slave_config(dma->ch, &slave);
243 if (ret) {
244 dev_err(dsp->dev, "error: unable to set DMA slave config %d\n",
245 ret);
246 dma_release_channel(dma->ch);
247 dma->ch = NULL;
248 }
249
250 return ret;
251}
252EXPORT_SYMBOL_GPL(sst_dsp_dma_get_channel);
253
254void sst_dsp_dma_put_channel(struct sst_dsp *dsp)
255{
256 struct sst_dma *dma = dsp->dma;
257
258 if (!dma->ch)
259 return;
260
261 dma_release_channel(dma->ch);
262 dma->ch = NULL;
263}
264EXPORT_SYMBOL_GPL(sst_dsp_dma_put_channel);
265
266int sst_dma_new(struct sst_dsp *sst)
267{
268 struct sst_pdata *sst_pdata = sst->pdata;
269 struct sst_dma *dma;
270 struct resource mem;
271 const char *dma_dev_name;
272 int ret = 0;
273
274 /* configure the correct platform data for whatever DMA engine
275 * is attached to the ADSP IP. */
276 switch (sst->pdata->dma_engine) {
277 case SST_DMA_TYPE_DW:
278 dma_dev_name = "dw_dmac";
279 break;
280 case SST_DMA_TYPE_MID:
281 dma_dev_name = "Intel MID DMA";
282 break;
283 default:
284 dev_err(sst->dev, "error: invalid DMA engine %d\n",
285 sst->pdata->dma_engine);
286 return -EINVAL;
287 }
288
289 dma = devm_kzalloc(sst->dev, sizeof(struct sst_dma), GFP_KERNEL);
290 if (!dma)
291 return -ENOMEM;
292
293 dma->sst = sst;
294
295 memset(&mem, 0, sizeof(mem));
296
297 mem.start = sst->addr.lpe_base + sst_pdata->dma_base;
298 mem.end = sst->addr.lpe_base + sst_pdata->dma_base + sst_pdata->dma_size - 1;
299 mem.flags = IORESOURCE_MEM;
300
301 /* now register DMA engine device */
302 dma->chip = dw_probe(sst->dma_dev, &mem, sst_pdata->irq);
303 if (IS_ERR(dma->chip)) {
304 dev_err(sst->dev, "error: DMA device register failed\n");
305 ret = PTR_ERR(dma->chip);
306 goto err_dma_dev;
307 }
308
309 sst->dma = dma;
310 sst->fw_use_dma = true;
311 return 0;
312
313err_dma_dev:
314 devm_kfree(sst->dev, dma);
315 return ret;
316}
317EXPORT_SYMBOL(sst_dma_new);
318
319void sst_dma_free(struct sst_dma *dma)
320{
321
322 if (dma == NULL)
323 return;
324
325 if (dma->ch)
326 dma_release_channel(dma->ch);
327
328 if (dma->chip)
329 dw_remove(dma->chip);
330
331}
332EXPORT_SYMBOL(sst_dma_free);
333
Mark Browna4b12992014-03-12 23:04:35 +0000334/* create new generic firmware object */
335struct sst_fw *sst_fw_new(struct sst_dsp *dsp,
336 const struct firmware *fw, void *private)
337{
338 struct sst_fw *sst_fw;
339 int err;
340
341 if (!dsp->ops->parse_fw)
342 return NULL;
343
344 sst_fw = kzalloc(sizeof(*sst_fw), GFP_KERNEL);
345 if (sst_fw == NULL)
346 return NULL;
347
348 sst_fw->dsp = dsp;
349 sst_fw->private = private;
350 sst_fw->size = fw->size;
351
Mark Browna4b12992014-03-12 23:04:35 +0000352 /* allocate DMA buffer to store FW data */
Liam Girdwood10df3502014-05-02 16:56:31 +0100353 sst_fw->dma_buf = dma_alloc_coherent(dsp->dma_dev, sst_fw->size,
Mark Browna4b12992014-03-12 23:04:35 +0000354 &sst_fw->dmable_fw_paddr, GFP_DMA | GFP_KERNEL);
355 if (!sst_fw->dma_buf) {
356 dev_err(dsp->dev, "error: DMA alloc failed\n");
357 kfree(sst_fw);
358 return NULL;
359 }
360
361 /* copy FW data to DMA-able memory */
362 memcpy((void *)sst_fw->dma_buf, (void *)fw->data, fw->size);
363
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000364 if (dsp->fw_use_dma) {
365 err = sst_dsp_dma_get_channel(dsp, 0);
366 if (err < 0)
367 goto chan_err;
368 }
369
Mark Browna4b12992014-03-12 23:04:35 +0000370 /* call core specific FW paser to load FW data into DSP */
371 err = dsp->ops->parse_fw(sst_fw);
372 if (err < 0) {
373 dev_err(dsp->dev, "error: parse fw failed %d\n", err);
374 goto parse_err;
375 }
376
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000377 if (dsp->fw_use_dma)
378 sst_dsp_dma_put_channel(dsp);
379
Mark Browna4b12992014-03-12 23:04:35 +0000380 mutex_lock(&dsp->mutex);
381 list_add(&sst_fw->list, &dsp->fw_list);
382 mutex_unlock(&dsp->mutex);
383
384 return sst_fw;
385
386parse_err:
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000387 if (dsp->fw_use_dma)
388 sst_dsp_dma_put_channel(dsp);
389chan_err:
390 dma_free_coherent(dsp->dma_dev, sst_fw->size,
Mark Browna4b12992014-03-12 23:04:35 +0000391 sst_fw->dma_buf,
392 sst_fw->dmable_fw_paddr);
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000393 sst_fw->dma_buf = NULL;
Mark Browna4b12992014-03-12 23:04:35 +0000394 kfree(sst_fw);
395 return NULL;
396}
397EXPORT_SYMBOL_GPL(sst_fw_new);
398
Liam Girdwood555f8a802014-05-05 17:31:37 +0100399int sst_fw_reload(struct sst_fw *sst_fw)
400{
401 struct sst_dsp *dsp = sst_fw->dsp;
402 int ret;
403
404 dev_dbg(dsp->dev, "reloading firmware\n");
405
406 /* call core specific FW paser to load FW data into DSP */
407 ret = dsp->ops->parse_fw(sst_fw);
408 if (ret < 0)
409 dev_err(dsp->dev, "error: parse fw failed %d\n", ret);
410
411 return ret;
412}
413EXPORT_SYMBOL_GPL(sst_fw_reload);
414
415void sst_fw_unload(struct sst_fw *sst_fw)
416{
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000417 struct sst_dsp *dsp = sst_fw->dsp;
418 struct sst_module *module, *mtmp;
419 struct sst_module_runtime *runtime, *rtmp;
Liam Girdwood555f8a802014-05-05 17:31:37 +0100420
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000421 dev_dbg(dsp->dev, "unloading firmware\n");
Liam Girdwood555f8a802014-05-05 17:31:37 +0100422
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000423 mutex_lock(&dsp->mutex);
Liam Girdwood555f8a802014-05-05 17:31:37 +0100424
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000425 /* check module by module */
426 list_for_each_entry_safe(module, mtmp, &dsp->module_list, list) {
427 if (module->sst_fw == sst_fw) {
428
429 /* remove runtime modules */
430 list_for_each_entry_safe(runtime, rtmp, &module->runtime_list, list) {
431
432 block_list_remove(dsp, &runtime->block_list);
433 list_del(&runtime->list);
434 kfree(runtime);
435 }
436
437 /* now remove the module */
438 block_list_remove(dsp, &module->block_list);
439 list_del(&module->list);
440 kfree(module);
441 }
442 }
443
444 /* remove all scratch blocks */
445 block_list_remove(dsp, &dsp->scratch_block_list);
446
447 mutex_unlock(&dsp->mutex);
Liam Girdwood555f8a802014-05-05 17:31:37 +0100448}
449EXPORT_SYMBOL_GPL(sst_fw_unload);
450
Mark Browna4b12992014-03-12 23:04:35 +0000451/* free single firmware object */
452void sst_fw_free(struct sst_fw *sst_fw)
453{
454 struct sst_dsp *dsp = sst_fw->dsp;
455
456 mutex_lock(&dsp->mutex);
457 list_del(&sst_fw->list);
458 mutex_unlock(&dsp->mutex);
459
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000460 if (sst_fw->dma_buf)
461 dma_free_coherent(dsp->dma_dev, sst_fw->size, sst_fw->dma_buf,
Mark Browna4b12992014-03-12 23:04:35 +0000462 sst_fw->dmable_fw_paddr);
463 kfree(sst_fw);
464}
465EXPORT_SYMBOL_GPL(sst_fw_free);
466
467/* free all firmware objects */
468void sst_fw_free_all(struct sst_dsp *dsp)
469{
470 struct sst_fw *sst_fw, *t;
471
472 mutex_lock(&dsp->mutex);
473 list_for_each_entry_safe(sst_fw, t, &dsp->fw_list, list) {
474
475 list_del(&sst_fw->list);
476 dma_free_coherent(dsp->dev, sst_fw->size, sst_fw->dma_buf,
477 sst_fw->dmable_fw_paddr);
478 kfree(sst_fw);
479 }
480 mutex_unlock(&dsp->mutex);
481}
482EXPORT_SYMBOL_GPL(sst_fw_free_all);
483
484/* create a new SST generic module from FW template */
485struct sst_module *sst_module_new(struct sst_fw *sst_fw,
486 struct sst_module_template *template, void *private)
487{
488 struct sst_dsp *dsp = sst_fw->dsp;
489 struct sst_module *sst_module;
490
491 sst_module = kzalloc(sizeof(*sst_module), GFP_KERNEL);
492 if (sst_module == NULL)
493 return NULL;
494
495 sst_module->id = template->id;
496 sst_module->dsp = dsp;
497 sst_module->sst_fw = sst_fw;
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000498 sst_module->scratch_size = template->scratch_size;
499 sst_module->persistent_size = template->persistent_size;
Mark Browna4b12992014-03-12 23:04:35 +0000500
501 INIT_LIST_HEAD(&sst_module->block_list);
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000502 INIT_LIST_HEAD(&sst_module->runtime_list);
Mark Browna4b12992014-03-12 23:04:35 +0000503
504 mutex_lock(&dsp->mutex);
505 list_add(&sst_module->list, &dsp->module_list);
506 mutex_unlock(&dsp->mutex);
507
508 return sst_module;
509}
510EXPORT_SYMBOL_GPL(sst_module_new);
511
512/* free firmware module and remove from available list */
513void sst_module_free(struct sst_module *sst_module)
514{
515 struct sst_dsp *dsp = sst_module->dsp;
516
517 mutex_lock(&dsp->mutex);
518 list_del(&sst_module->list);
519 mutex_unlock(&dsp->mutex);
520
521 kfree(sst_module);
522}
523EXPORT_SYMBOL_GPL(sst_module_free);
524
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000525struct sst_module_runtime *sst_module_runtime_new(struct sst_module *module,
526 int id, void *private)
527{
528 struct sst_dsp *dsp = module->dsp;
529 struct sst_module_runtime *runtime;
530
531 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
532 if (runtime == NULL)
533 return NULL;
534
535 runtime->id = id;
536 runtime->dsp = dsp;
537 runtime->module = module;
538 INIT_LIST_HEAD(&runtime->block_list);
539
540 mutex_lock(&dsp->mutex);
541 list_add(&runtime->list, &module->runtime_list);
542 mutex_unlock(&dsp->mutex);
543
544 return runtime;
545}
546EXPORT_SYMBOL_GPL(sst_module_runtime_new);
547
548void sst_module_runtime_free(struct sst_module_runtime *runtime)
549{
550 struct sst_dsp *dsp = runtime->dsp;
551
552 mutex_lock(&dsp->mutex);
553 list_del(&runtime->list);
554 mutex_unlock(&dsp->mutex);
555
556 kfree(runtime);
557}
558EXPORT_SYMBOL_GPL(sst_module_runtime_free);
559
560static struct sst_mem_block *find_block(struct sst_dsp *dsp,
561 struct sst_block_allocator *ba)
Mark Browna4b12992014-03-12 23:04:35 +0000562{
563 struct sst_mem_block *block;
564
565 list_for_each_entry(block, &dsp->free_block_list, list) {
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000566 if (block->type == ba->type && block->offset == ba->offset)
Mark Browna4b12992014-03-12 23:04:35 +0000567 return block;
568 }
569
570 return NULL;
571}
572
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000573/* Block allocator must be on block boundary */
574static int block_alloc_contiguous(struct sst_dsp *dsp,
575 struct sst_block_allocator *ba, struct list_head *block_list)
Mark Browna4b12992014-03-12 23:04:35 +0000576{
577 struct list_head tmp = LIST_HEAD_INIT(tmp);
Mark Browna4b12992014-03-12 23:04:35 +0000578 struct sst_mem_block *block;
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000579 u32 block_start = SST_HSW_BLOCK_ANY;
580 int size = ba->size, offset = ba->offset;
Mark Browna4b12992014-03-12 23:04:35 +0000581
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000582 while (ba->size > 0) {
583
584 block = find_block(dsp, ba);
Mark Browna4b12992014-03-12 23:04:35 +0000585 if (!block) {
586 list_splice(&tmp, &dsp->free_block_list);
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000587
588 ba->size = size;
589 ba->offset = offset;
Mark Browna4b12992014-03-12 23:04:35 +0000590 return -ENOMEM;
591 }
592
593 list_move_tail(&block->list, &tmp);
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000594 ba->offset += block->size;
595 ba->size -= block->size;
Mark Browna4b12992014-03-12 23:04:35 +0000596 }
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000597 ba->size = size;
598 ba->offset = offset;
Mark Browna4b12992014-03-12 23:04:35 +0000599
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000600 list_for_each_entry(block, &tmp, list) {
601
602 if (block->offset < block_start)
603 block_start = block->offset;
604
605 list_add(&block->module_list, block_list);
606
607 dev_dbg(dsp->dev, "block allocated %d:%d at offset 0x%x\n",
608 block->type, block->index, block->offset);
609 }
Liam Girdwood84fbdd52014-05-02 16:56:29 +0100610
Mark Browna4b12992014-03-12 23:04:35 +0000611 list_splice(&tmp, &dsp->used_block_list);
612 return 0;
613}
614
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000615/* allocate first free DSP blocks for data - callers hold locks */
616static int block_alloc(struct sst_dsp *dsp, struct sst_block_allocator *ba,
617 struct list_head *block_list)
Mark Browna4b12992014-03-12 23:04:35 +0000618{
Mark Browna4b12992014-03-12 23:04:35 +0000619 struct sst_mem_block *block, *tmp;
620 int ret = 0;
621
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000622 if (ba->size == 0)
Mark Browna4b12992014-03-12 23:04:35 +0000623 return 0;
624
625 /* find first free whole blocks that can hold module */
626 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
627
628 /* ignore blocks with wrong type */
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000629 if (block->type != ba->type)
Mark Browna4b12992014-03-12 23:04:35 +0000630 continue;
631
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000632 if (ba->size > block->size)
Mark Browna4b12992014-03-12 23:04:35 +0000633 continue;
634
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000635 ba->offset = block->offset;
636 block->bytes_used = ba->size % block->size;
637 list_add(&block->module_list, block_list);
Mark Browna4b12992014-03-12 23:04:35 +0000638 list_move(&block->list, &dsp->used_block_list);
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000639 dev_dbg(dsp->dev, "block allocated %d:%d at offset 0x%x\n",
640 block->type, block->index, block->offset);
Mark Browna4b12992014-03-12 23:04:35 +0000641 return 0;
642 }
643
644 /* then find free multiple blocks that can hold module */
645 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
646
647 /* ignore blocks with wrong type */
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000648 if (block->type != ba->type)
Mark Browna4b12992014-03-12 23:04:35 +0000649 continue;
650
651 /* do we span > 1 blocks */
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000652 if (ba->size > block->size) {
653
654 /* align ba to block boundary */
655 ba->offset = block->offset;
656
657 ret = block_alloc_contiguous(dsp, ba, block_list);
Mark Browna4b12992014-03-12 23:04:35 +0000658 if (ret == 0)
659 return ret;
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000660
Mark Browna4b12992014-03-12 23:04:35 +0000661 }
662 }
663
664 /* not enough free block space */
665 return -ENOMEM;
666}
667
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000668int sst_alloc_blocks(struct sst_dsp *dsp, struct sst_block_allocator *ba,
669 struct list_head *block_list)
Mark Browna4b12992014-03-12 23:04:35 +0000670{
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000671 int ret;
Mark Browna4b12992014-03-12 23:04:35 +0000672
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000673 dev_dbg(dsp->dev, "block request 0x%x bytes at offset 0x%x type %d\n",
674 ba->size, ba->offset, ba->type);
Mark Browna4b12992014-03-12 23:04:35 +0000675
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000676 mutex_lock(&dsp->mutex);
677
678 ret = block_alloc(dsp, ba, block_list);
679 if (ret < 0) {
680 dev_err(dsp->dev, "error: can't alloc blocks %d\n", ret);
681 goto out;
Mark Browna4b12992014-03-12 23:04:35 +0000682 }
683
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000684 /* prepare DSP blocks for module usage */
685 ret = block_list_prepare(dsp, block_list);
686 if (ret < 0)
687 dev_err(dsp->dev, "error: prepare failed\n");
Mark Browna4b12992014-03-12 23:04:35 +0000688
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000689out:
690 mutex_unlock(&dsp->mutex);
Mark Browna4b12992014-03-12 23:04:35 +0000691 return ret;
692}
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000693EXPORT_SYMBOL_GPL(sst_alloc_blocks);
694
695int sst_free_blocks(struct sst_dsp *dsp, struct list_head *block_list)
696{
697 mutex_lock(&dsp->mutex);
698 block_list_remove(dsp, block_list);
699 mutex_unlock(&dsp->mutex);
700 return 0;
701}
702EXPORT_SYMBOL_GPL(sst_free_blocks);
Mark Browna4b12992014-03-12 23:04:35 +0000703
704/* allocate memory blocks for static module addresses - callers hold locks */
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000705static int block_alloc_fixed(struct sst_dsp *dsp, struct sst_block_allocator *ba,
706 struct list_head *block_list)
Mark Browna4b12992014-03-12 23:04:35 +0000707{
Mark Browna4b12992014-03-12 23:04:35 +0000708 struct sst_mem_block *block, *tmp;
Jie Yangd83901e2015-01-04 09:15:04 +0800709 struct sst_block_allocator ba_tmp = *ba;
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000710 u32 end = ba->offset + ba->size, block_end;
Mark Browna4b12992014-03-12 23:04:35 +0000711 int err;
712
713 /* only IRAM/DRAM blocks are managed */
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000714 if (ba->type != SST_MEM_IRAM && ba->type != SST_MEM_DRAM)
Mark Browna4b12992014-03-12 23:04:35 +0000715 return 0;
716
717 /* are blocks already attached to this module */
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000718 list_for_each_entry_safe(block, tmp, block_list, module_list) {
Mark Browna4b12992014-03-12 23:04:35 +0000719
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000720 /* ignore blocks with wrong type */
721 if (block->type != ba->type)
Mark Browna4b12992014-03-12 23:04:35 +0000722 continue;
723
724 block_end = block->offset + block->size;
725
726 /* find block that holds section */
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000727 if (ba->offset >= block->offset && end <= block_end)
Mark Browna4b12992014-03-12 23:04:35 +0000728 return 0;
729
730 /* does block span more than 1 section */
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000731 if (ba->offset >= block->offset && ba->offset < block_end) {
Mark Browna4b12992014-03-12 23:04:35 +0000732
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000733 /* align ba to block boundary */
Jie Yangd83901e2015-01-04 09:15:04 +0800734 ba_tmp.size -= block_end - ba->offset;
735 ba_tmp.offset = block_end;
736 err = block_alloc_contiguous(dsp, &ba_tmp, block_list);
Mark Browna4b12992014-03-12 23:04:35 +0000737 if (err < 0)
738 return -ENOMEM;
739
740 /* module already owns blocks */
741 return 0;
742 }
743 }
744
745 /* find first free blocks that can hold section in free list */
746 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
747 block_end = block->offset + block->size;
748
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000749 /* ignore blocks with wrong type */
750 if (block->type != ba->type)
751 continue;
752
Mark Browna4b12992014-03-12 23:04:35 +0000753 /* find block that holds section */
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000754 if (ba->offset >= block->offset && end <= block_end) {
Mark Browna4b12992014-03-12 23:04:35 +0000755
756 /* add block */
Mark Browna4b12992014-03-12 23:04:35 +0000757 list_move(&block->list, &dsp->used_block_list);
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000758 list_add(&block->module_list, block_list);
759 dev_dbg(dsp->dev, "block allocated %d:%d at offset 0x%x\n",
760 block->type, block->index, block->offset);
Mark Browna4b12992014-03-12 23:04:35 +0000761 return 0;
762 }
763
764 /* does block span more than 1 section */
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000765 if (ba->offset >= block->offset && ba->offset < block_end) {
Mark Browna4b12992014-03-12 23:04:35 +0000766
Jie Yang25f97542014-12-23 09:12:45 +0800767 /* add block */
768 list_move(&block->list, &dsp->used_block_list);
769 list_add(&block->module_list, block_list);
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000770 /* align ba to block boundary */
Jie Yangd83901e2015-01-04 09:15:04 +0800771 ba_tmp.size -= block_end - ba->offset;
772 ba_tmp.offset = block_end;
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000773
Jie Yangd83901e2015-01-04 09:15:04 +0800774 err = block_alloc_contiguous(dsp, &ba_tmp, block_list);
Mark Browna4b12992014-03-12 23:04:35 +0000775 if (err < 0)
776 return -ENOMEM;
777
Mark Browna4b12992014-03-12 23:04:35 +0000778 return 0;
779 }
Mark Browna4b12992014-03-12 23:04:35 +0000780 }
781
782 return -ENOMEM;
783}
784
785/* Load fixed module data into DSP memory blocks */
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000786int sst_module_alloc_blocks(struct sst_module *module)
Mark Browna4b12992014-03-12 23:04:35 +0000787{
788 struct sst_dsp *dsp = module->dsp;
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000789 struct sst_fw *sst_fw = module->sst_fw;
790 struct sst_block_allocator ba;
Mark Browna4b12992014-03-12 23:04:35 +0000791 int ret;
792
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000793 ba.size = module->size;
794 ba.type = module->type;
795 ba.offset = module->offset;
796
797 dev_dbg(dsp->dev, "block request 0x%x bytes at offset 0x%x type %d\n",
798 ba.size, ba.offset, ba.type);
799
Mark Browna4b12992014-03-12 23:04:35 +0000800 mutex_lock(&dsp->mutex);
801
802 /* alloc blocks that includes this section */
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000803 ret = block_alloc_fixed(dsp, &ba, &module->block_list);
Mark Browna4b12992014-03-12 23:04:35 +0000804 if (ret < 0) {
805 dev_err(dsp->dev,
806 "error: no free blocks for section at offset 0x%x size 0x%x\n",
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000807 module->offset, module->size);
Mark Browna4b12992014-03-12 23:04:35 +0000808 mutex_unlock(&dsp->mutex);
809 return -ENOMEM;
810 }
811
812 /* prepare DSP blocks for module copy */
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000813 ret = block_list_prepare(dsp, &module->block_list);
Mark Browna4b12992014-03-12 23:04:35 +0000814 if (ret < 0) {
815 dev_err(dsp->dev, "error: fw module prepare failed\n");
816 goto err;
817 }
818
819 /* copy partial module data to blocks */
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000820 if (dsp->fw_use_dma) {
821 ret = sst_dsp_dma_copyto(dsp,
822 dsp->addr.lpe_base + module->offset,
823 sst_fw->dmable_fw_paddr + module->data_offset,
824 module->size);
825 if (ret < 0) {
826 dev_err(dsp->dev, "error: module copy failed\n");
827 goto err;
828 }
829 } else
830 sst_memcpy32(dsp->addr.lpe + module->offset, module->data,
831 module->size);
Mark Browna4b12992014-03-12 23:04:35 +0000832
833 mutex_unlock(&dsp->mutex);
834 return ret;
835
836err:
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000837 block_list_remove(dsp, &module->block_list);
Mark Browna4b12992014-03-12 23:04:35 +0000838 mutex_unlock(&dsp->mutex);
839 return ret;
840}
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000841EXPORT_SYMBOL_GPL(sst_module_alloc_blocks);
Mark Browna4b12992014-03-12 23:04:35 +0000842
843/* Unload entire module from DSP memory */
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000844int sst_module_free_blocks(struct sst_module *module)
Mark Browna4b12992014-03-12 23:04:35 +0000845{
846 struct sst_dsp *dsp = module->dsp;
847
848 mutex_lock(&dsp->mutex);
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000849 block_list_remove(dsp, &module->block_list);
Mark Browna4b12992014-03-12 23:04:35 +0000850 mutex_unlock(&dsp->mutex);
851 return 0;
852}
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000853EXPORT_SYMBOL_GPL(sst_module_free_blocks);
854
855int sst_module_runtime_alloc_blocks(struct sst_module_runtime *runtime,
856 int offset)
857{
858 struct sst_dsp *dsp = runtime->dsp;
859 struct sst_module *module = runtime->module;
860 struct sst_block_allocator ba;
861 int ret;
862
863 if (module->persistent_size == 0)
864 return 0;
865
866 ba.size = module->persistent_size;
867 ba.type = SST_MEM_DRAM;
868
869 mutex_lock(&dsp->mutex);
870
871 /* do we need to allocate at a fixed address ? */
872 if (offset != 0) {
873
874 ba.offset = offset;
875
876 dev_dbg(dsp->dev, "persistent fixed block request 0x%x bytes type %d offset 0x%x\n",
877 ba.size, ba.type, ba.offset);
878
879 /* alloc blocks that includes this section */
880 ret = block_alloc_fixed(dsp, &ba, &runtime->block_list);
881
882 } else {
883 dev_dbg(dsp->dev, "persistent block request 0x%x bytes type %d\n",
884 ba.size, ba.type);
885
886 /* alloc blocks that includes this section */
887 ret = block_alloc(dsp, &ba, &runtime->block_list);
888 }
889 if (ret < 0) {
890 dev_err(dsp->dev,
891 "error: no free blocks for runtime module size 0x%x\n",
892 module->persistent_size);
893 mutex_unlock(&dsp->mutex);
894 return -ENOMEM;
895 }
896 runtime->persistent_offset = ba.offset;
897
898 /* prepare DSP blocks for module copy */
899 ret = block_list_prepare(dsp, &runtime->block_list);
900 if (ret < 0) {
901 dev_err(dsp->dev, "error: runtime block prepare failed\n");
902 goto err;
903 }
904
905 mutex_unlock(&dsp->mutex);
906 return ret;
907
908err:
909 block_list_remove(dsp, &module->block_list);
910 mutex_unlock(&dsp->mutex);
911 return ret;
912}
913EXPORT_SYMBOL_GPL(sst_module_runtime_alloc_blocks);
914
915int sst_module_runtime_free_blocks(struct sst_module_runtime *runtime)
916{
917 struct sst_dsp *dsp = runtime->dsp;
918
919 mutex_lock(&dsp->mutex);
920 block_list_remove(dsp, &runtime->block_list);
921 mutex_unlock(&dsp->mutex);
922 return 0;
923}
924EXPORT_SYMBOL_GPL(sst_module_runtime_free_blocks);
925
926int sst_module_runtime_save(struct sst_module_runtime *runtime,
927 struct sst_module_runtime_context *context)
928{
929 struct sst_dsp *dsp = runtime->dsp;
930 struct sst_module *module = runtime->module;
931 int ret = 0;
932
933 dev_dbg(dsp->dev, "saving runtime %d memory at 0x%x size 0x%x\n",
934 runtime->id, runtime->persistent_offset,
935 module->persistent_size);
936
937 context->buffer = dma_alloc_coherent(dsp->dma_dev,
938 module->persistent_size,
939 &context->dma_buffer, GFP_DMA | GFP_KERNEL);
940 if (!context->buffer) {
941 dev_err(dsp->dev, "error: DMA context alloc failed\n");
942 return -ENOMEM;
943 }
944
945 mutex_lock(&dsp->mutex);
946
947 if (dsp->fw_use_dma) {
948
949 ret = sst_dsp_dma_get_channel(dsp, 0);
950 if (ret < 0)
951 goto err;
952
953 ret = sst_dsp_dma_copyfrom(dsp, context->dma_buffer,
954 dsp->addr.lpe_base + runtime->persistent_offset,
955 module->persistent_size);
956 sst_dsp_dma_put_channel(dsp);
957 if (ret < 0) {
958 dev_err(dsp->dev, "error: context copy failed\n");
959 goto err;
960 }
961 } else
962 sst_memcpy32(context->buffer, dsp->addr.lpe +
963 runtime->persistent_offset,
964 module->persistent_size);
965
966err:
967 mutex_unlock(&dsp->mutex);
968 return ret;
969}
970EXPORT_SYMBOL_GPL(sst_module_runtime_save);
971
972int sst_module_runtime_restore(struct sst_module_runtime *runtime,
973 struct sst_module_runtime_context *context)
974{
975 struct sst_dsp *dsp = runtime->dsp;
976 struct sst_module *module = runtime->module;
977 int ret = 0;
978
979 dev_dbg(dsp->dev, "restoring runtime %d memory at 0x%x size 0x%x\n",
980 runtime->id, runtime->persistent_offset,
981 module->persistent_size);
982
983 mutex_lock(&dsp->mutex);
984
985 if (!context->buffer) {
986 dev_info(dsp->dev, "no context buffer need to restore!\n");
987 goto err;
988 }
989
990 if (dsp->fw_use_dma) {
991
992 ret = sst_dsp_dma_get_channel(dsp, 0);
993 if (ret < 0)
994 goto err;
995
996 ret = sst_dsp_dma_copyto(dsp,
997 dsp->addr.lpe_base + runtime->persistent_offset,
998 context->dma_buffer, module->persistent_size);
999 sst_dsp_dma_put_channel(dsp);
1000 if (ret < 0) {
1001 dev_err(dsp->dev, "error: module copy failed\n");
1002 goto err;
1003 }
1004 } else
1005 sst_memcpy32(dsp->addr.lpe + runtime->persistent_offset,
1006 context->buffer, module->persistent_size);
1007
1008 dma_free_coherent(dsp->dma_dev, module->persistent_size,
1009 context->buffer, context->dma_buffer);
1010 context->buffer = NULL;
1011
1012err:
1013 mutex_unlock(&dsp->mutex);
1014 return ret;
1015}
1016EXPORT_SYMBOL_GPL(sst_module_runtime_restore);
Mark Browna4b12992014-03-12 23:04:35 +00001017
1018/* register a DSP memory block for use with FW based modules */
1019struct sst_mem_block *sst_mem_block_register(struct sst_dsp *dsp, u32 offset,
1020 u32 size, enum sst_mem_type type, struct sst_block_ops *ops, u32 index,
1021 void *private)
1022{
1023 struct sst_mem_block *block;
1024
1025 block = kzalloc(sizeof(*block), GFP_KERNEL);
1026 if (block == NULL)
1027 return NULL;
1028
1029 block->offset = offset;
1030 block->size = size;
1031 block->index = index;
1032 block->type = type;
1033 block->dsp = dsp;
1034 block->private = private;
1035 block->ops = ops;
1036
1037 mutex_lock(&dsp->mutex);
1038 list_add(&block->list, &dsp->free_block_list);
1039 mutex_unlock(&dsp->mutex);
1040
1041 return block;
1042}
1043EXPORT_SYMBOL_GPL(sst_mem_block_register);
1044
1045/* unregister all DSP memory blocks */
1046void sst_mem_block_unregister_all(struct sst_dsp *dsp)
1047{
1048 struct sst_mem_block *block, *tmp;
1049
1050 mutex_lock(&dsp->mutex);
1051
1052 /* unregister used blocks */
1053 list_for_each_entry_safe(block, tmp, &dsp->used_block_list, list) {
1054 list_del(&block->list);
1055 kfree(block);
1056 }
1057
1058 /* unregister free blocks */
1059 list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
1060 list_del(&block->list);
1061 kfree(block);
1062 }
1063
1064 mutex_unlock(&dsp->mutex);
1065}
1066EXPORT_SYMBOL_GPL(sst_mem_block_unregister_all);
1067
1068/* allocate scratch buffer blocks */
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001069int sst_block_alloc_scratch(struct sst_dsp *dsp)
Mark Browna4b12992014-03-12 23:04:35 +00001070{
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001071 struct sst_module *module;
1072 struct sst_block_allocator ba;
1073 int ret;
Mark Browna4b12992014-03-12 23:04:35 +00001074
1075 mutex_lock(&dsp->mutex);
1076
1077 /* calculate required scratch size */
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001078 dsp->scratch_size = 0;
1079 list_for_each_entry(module, &dsp->module_list, list) {
1080 dev_dbg(dsp->dev, "module %d scratch req 0x%x bytes\n",
1081 module->id, module->scratch_size);
1082 if (dsp->scratch_size < module->scratch_size)
1083 dsp->scratch_size = module->scratch_size;
Mark Browna4b12992014-03-12 23:04:35 +00001084 }
1085
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001086 dev_dbg(dsp->dev, "scratch buffer required is 0x%x bytes\n",
1087 dsp->scratch_size);
Mark Browna4b12992014-03-12 23:04:35 +00001088
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001089 if (dsp->scratch_size == 0) {
1090 dev_info(dsp->dev, "no modules need scratch buffer\n");
1091 mutex_unlock(&dsp->mutex);
1092 return 0;
1093 }
Mark Browna4b12992014-03-12 23:04:35 +00001094
1095 /* allocate blocks for module scratch buffers */
1096 dev_dbg(dsp->dev, "allocating scratch blocks\n");
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001097
1098 ba.size = dsp->scratch_size;
1099 ba.type = SST_MEM_DRAM;
1100
1101 /* do we need to allocate at fixed offset */
1102 if (dsp->scratch_offset != 0) {
1103
1104 dev_dbg(dsp->dev, "block request 0x%x bytes type %d at 0x%x\n",
1105 ba.size, ba.type, ba.offset);
1106
1107 ba.offset = dsp->scratch_offset;
1108
1109 /* alloc blocks that includes this section */
1110 ret = block_alloc_fixed(dsp, &ba, &dsp->scratch_block_list);
1111
1112 } else {
1113 dev_dbg(dsp->dev, "block request 0x%x bytes type %d\n",
1114 ba.size, ba.type);
1115
1116 ba.offset = 0;
1117 ret = block_alloc(dsp, &ba, &dsp->scratch_block_list);
1118 }
Mark Browna4b12992014-03-12 23:04:35 +00001119 if (ret < 0) {
1120 dev_err(dsp->dev, "error: can't alloc scratch blocks\n");
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001121 mutex_unlock(&dsp->mutex);
1122 return ret;
1123 }
1124
1125 ret = block_list_prepare(dsp, &dsp->scratch_block_list);
1126 if (ret < 0) {
1127 dev_err(dsp->dev, "error: scratch block prepare failed\n");
Sudip Mukherjee22a236b2014-11-02 12:04:41 +05301128 mutex_unlock(&dsp->mutex);
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001129 return ret;
Mark Browna4b12992014-03-12 23:04:35 +00001130 }
1131
1132 /* assign the same offset of scratch to each module */
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001133 dsp->scratch_offset = ba.offset;
Mark Browna4b12992014-03-12 23:04:35 +00001134 mutex_unlock(&dsp->mutex);
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001135 return dsp->scratch_size;
Mark Browna4b12992014-03-12 23:04:35 +00001136}
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001137EXPORT_SYMBOL_GPL(sst_block_alloc_scratch);
Mark Browna4b12992014-03-12 23:04:35 +00001138
1139/* free all scratch blocks */
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001140void sst_block_free_scratch(struct sst_dsp *dsp)
Mark Browna4b12992014-03-12 23:04:35 +00001141{
Mark Browna4b12992014-03-12 23:04:35 +00001142 mutex_lock(&dsp->mutex);
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001143 block_list_remove(dsp, &dsp->scratch_block_list);
Mark Browna4b12992014-03-12 23:04:35 +00001144 mutex_unlock(&dsp->mutex);
1145}
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001146EXPORT_SYMBOL_GPL(sst_block_free_scratch);
Mark Browna4b12992014-03-12 23:04:35 +00001147
1148/* get a module from it's unique ID */
1149struct sst_module *sst_module_get_from_id(struct sst_dsp *dsp, u32 id)
1150{
1151 struct sst_module *module;
1152
1153 mutex_lock(&dsp->mutex);
1154
1155 list_for_each_entry(module, &dsp->module_list, list) {
1156 if (module->id == id) {
1157 mutex_unlock(&dsp->mutex);
1158 return module;
1159 }
1160 }
1161
1162 mutex_unlock(&dsp->mutex);
1163 return NULL;
1164}
1165EXPORT_SYMBOL_GPL(sst_module_get_from_id);
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001166
1167struct sst_module_runtime *sst_module_runtime_get_from_id(
1168 struct sst_module *module, u32 id)
1169{
1170 struct sst_module_runtime *runtime;
1171 struct sst_dsp *dsp = module->dsp;
1172
1173 mutex_lock(&dsp->mutex);
1174
1175 list_for_each_entry(runtime, &module->runtime_list, list) {
1176 if (runtime->id == id) {
1177 mutex_unlock(&dsp->mutex);
1178 return runtime;
1179 }
1180 }
1181
1182 mutex_unlock(&dsp->mutex);
1183 return NULL;
1184}
1185EXPORT_SYMBOL_GPL(sst_module_runtime_get_from_id);
1186
1187/* returns block address in DSP address space */
1188u32 sst_dsp_get_offset(struct sst_dsp *dsp, u32 offset,
1189 enum sst_mem_type type)
1190{
1191 switch (type) {
1192 case SST_MEM_IRAM:
1193 return offset - dsp->addr.iram_offset +
1194 dsp->addr.dsp_iram_offset;
1195 case SST_MEM_DRAM:
1196 return offset - dsp->addr.dram_offset +
1197 dsp->addr.dsp_dram_offset;
1198 default:
1199 return 0;
1200 }
1201}
1202EXPORT_SYMBOL_GPL(sst_dsp_get_offset);