Commit | Line | Data |
---|---|---|
a4b12990 MB |
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> | |
e9600bc1 LG |
26 | #include <linux/acpi.h> |
27 | ||
28 | /* supported DMA engine drivers */ | |
29 | #include <linux/platform_data/dma-dw.h> | |
30 | #include <linux/dma/dw.h> | |
a4b12990 MB |
31 | |
32 | #include <asm/page.h> | |
33 | #include <asm/pgtable.h> | |
34 | ||
35 | #include "sst-dsp.h" | |
36 | #include "sst-dsp-priv.h" | |
37 | ||
e9600bc1 LG |
38 | #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 | ||
44 | struct 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 | }; | |
555f8a80 | 52 | |
7f266801 | 53 | static inline void sst_memcpy32(volatile void __iomem *dest, void *src, u32 bytes) |
a4b12990 | 54 | { |
7f266801 VK |
55 | /* __iowrite32_copy use 32bit size values so divide by 4 */ |
56 | __iowrite32_copy((void *)dest, src, bytes/4); | |
a4b12990 MB |
57 | } |
58 | ||
e9600bc1 LG |
59 | static 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 | ||
66 | static 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 */ | |
97 | int 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 | } | |
103 | EXPORT_SYMBOL_GPL(sst_dsp_dma_copyto); | |
104 | ||
105 | /* copy from DSP */ | |
106 | int 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 | } | |
112 | EXPORT_SYMBOL_GPL(sst_dsp_dma_copyfrom); | |
113 | ||
114 | /* remove module from memory - callers hold locks */ | |
115 | static 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 */ | |
143 | static 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 | ||
35c0a8c0 | 152 | if (block->ops && block->ops->enable && !block->users) { |
e9600bc1 LG |
153 | 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 | ||
164 | err: | |
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 | ||
137f6d54 | 172 | static struct dw_dma_platform_data dw_pdata = { |
e9600bc1 LG |
173 | .is_private = 1, |
174 | .chan_allocation_order = CHAN_ALLOCATION_ASCENDING, | |
175 | .chan_priority = CHAN_PRIORITY_ASCENDING, | |
176 | }; | |
177 | ||
178 | static 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 | ||
205 | static void dw_remove(struct dw_dma_chip *chip) | |
206 | { | |
207 | dw_dma_remove(chip); | |
208 | } | |
209 | ||
210 | static 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 | ||
217 | int 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 | } | |
252 | EXPORT_SYMBOL_GPL(sst_dsp_dma_get_channel); | |
253 | ||
254 | void 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 | } | |
264 | EXPORT_SYMBOL_GPL(sst_dsp_dma_put_channel); | |
265 | ||
266 | int 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 | ||
313 | err_dma_dev: | |
314 | devm_kfree(sst->dev, dma); | |
315 | return ret; | |
316 | } | |
317 | EXPORT_SYMBOL(sst_dma_new); | |
318 | ||
319 | void 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 | } | |
332 | EXPORT_SYMBOL(sst_dma_free); | |
333 | ||
a4b12990 MB |
334 | /* create new generic firmware object */ |
335 | struct 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 | ||
a4b12990 | 352 | /* allocate DMA buffer to store FW data */ |
10df3509 | 353 | sst_fw->dma_buf = dma_alloc_coherent(dsp->dma_dev, sst_fw->size, |
a4b12990 MB |
354 | &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 | ||
e9600bc1 LG |
364 | if (dsp->fw_use_dma) { |
365 | err = sst_dsp_dma_get_channel(dsp, 0); | |
366 | if (err < 0) | |
367 | goto chan_err; | |
368 | } | |
369 | ||
a4b12990 MB |
370 | /* 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 | ||
e9600bc1 LG |
377 | if (dsp->fw_use_dma) |
378 | sst_dsp_dma_put_channel(dsp); | |
379 | ||
a4b12990 MB |
380 | mutex_lock(&dsp->mutex); |
381 | list_add(&sst_fw->list, &dsp->fw_list); | |
382 | mutex_unlock(&dsp->mutex); | |
383 | ||
384 | return sst_fw; | |
385 | ||
386 | parse_err: | |
e9600bc1 LG |
387 | if (dsp->fw_use_dma) |
388 | sst_dsp_dma_put_channel(dsp); | |
389 | chan_err: | |
390 | dma_free_coherent(dsp->dma_dev, sst_fw->size, | |
a4b12990 MB |
391 | sst_fw->dma_buf, |
392 | sst_fw->dmable_fw_paddr); | |
e9600bc1 | 393 | sst_fw->dma_buf = NULL; |
a4b12990 MB |
394 | kfree(sst_fw); |
395 | return NULL; | |
396 | } | |
397 | EXPORT_SYMBOL_GPL(sst_fw_new); | |
398 | ||
555f8a80 LG |
399 | int 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 | } | |
413 | EXPORT_SYMBOL_GPL(sst_fw_reload); | |
414 | ||
415 | void sst_fw_unload(struct sst_fw *sst_fw) | |
416 | { | |
e9600bc1 LG |
417 | struct sst_dsp *dsp = sst_fw->dsp; |
418 | struct sst_module *module, *mtmp; | |
419 | struct sst_module_runtime *runtime, *rtmp; | |
420 | ||
421 | dev_dbg(dsp->dev, "unloading firmware\n"); | |
555f8a80 | 422 | |
e9600bc1 LG |
423 | mutex_lock(&dsp->mutex); |
424 | ||
425 | /* 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 | } | |
555f8a80 | 443 | |
e9600bc1 LG |
444 | /* remove all scratch blocks */ |
445 | block_list_remove(dsp, &dsp->scratch_block_list); | |
555f8a80 | 446 | |
e9600bc1 | 447 | mutex_unlock(&dsp->mutex); |
555f8a80 LG |
448 | } |
449 | EXPORT_SYMBOL_GPL(sst_fw_unload); | |
450 | ||
a4b12990 MB |
451 | /* free single firmware object */ |
452 | void 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 | ||
e9600bc1 LG |
460 | if (sst_fw->dma_buf) |
461 | dma_free_coherent(dsp->dma_dev, sst_fw->size, sst_fw->dma_buf, | |
a4b12990 MB |
462 | sst_fw->dmable_fw_paddr); |
463 | kfree(sst_fw); | |
464 | } | |
465 | EXPORT_SYMBOL_GPL(sst_fw_free); | |
466 | ||
467 | /* free all firmware objects */ | |
468 | void 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 | } | |
482 | EXPORT_SYMBOL_GPL(sst_fw_free_all); | |
483 | ||
484 | /* create a new SST generic module from FW template */ | |
485 | struct 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; | |
e9600bc1 LG |
498 | sst_module->scratch_size = template->scratch_size; |
499 | sst_module->persistent_size = template->persistent_size; | |
85b88a8d | 500 | sst_module->entry = template->entry; |
9449d39b | 501 | sst_module->state = SST_MODULE_STATE_UNLOADED; |
a4b12990 MB |
502 | |
503 | INIT_LIST_HEAD(&sst_module->block_list); | |
e9600bc1 | 504 | INIT_LIST_HEAD(&sst_module->runtime_list); |
a4b12990 MB |
505 | |
506 | mutex_lock(&dsp->mutex); | |
507 | list_add(&sst_module->list, &dsp->module_list); | |
508 | mutex_unlock(&dsp->mutex); | |
509 | ||
510 | return sst_module; | |
511 | } | |
512 | EXPORT_SYMBOL_GPL(sst_module_new); | |
513 | ||
514 | /* free firmware module and remove from available list */ | |
515 | void sst_module_free(struct sst_module *sst_module) | |
516 | { | |
517 | struct sst_dsp *dsp = sst_module->dsp; | |
518 | ||
519 | mutex_lock(&dsp->mutex); | |
520 | list_del(&sst_module->list); | |
521 | mutex_unlock(&dsp->mutex); | |
522 | ||
523 | kfree(sst_module); | |
524 | } | |
525 | EXPORT_SYMBOL_GPL(sst_module_free); | |
526 | ||
e9600bc1 LG |
527 | struct sst_module_runtime *sst_module_runtime_new(struct sst_module *module, |
528 | int id, void *private) | |
529 | { | |
530 | struct sst_dsp *dsp = module->dsp; | |
531 | struct sst_module_runtime *runtime; | |
532 | ||
533 | runtime = kzalloc(sizeof(*runtime), GFP_KERNEL); | |
534 | if (runtime == NULL) | |
535 | return NULL; | |
536 | ||
537 | runtime->id = id; | |
538 | runtime->dsp = dsp; | |
539 | runtime->module = module; | |
540 | INIT_LIST_HEAD(&runtime->block_list); | |
541 | ||
542 | mutex_lock(&dsp->mutex); | |
543 | list_add(&runtime->list, &module->runtime_list); | |
544 | mutex_unlock(&dsp->mutex); | |
545 | ||
546 | return runtime; | |
547 | } | |
548 | EXPORT_SYMBOL_GPL(sst_module_runtime_new); | |
549 | ||
550 | void sst_module_runtime_free(struct sst_module_runtime *runtime) | |
551 | { | |
552 | struct sst_dsp *dsp = runtime->dsp; | |
553 | ||
554 | mutex_lock(&dsp->mutex); | |
555 | list_del(&runtime->list); | |
556 | mutex_unlock(&dsp->mutex); | |
557 | ||
558 | kfree(runtime); | |
559 | } | |
560 | EXPORT_SYMBOL_GPL(sst_module_runtime_free); | |
561 | ||
562 | static struct sst_mem_block *find_block(struct sst_dsp *dsp, | |
563 | struct sst_block_allocator *ba) | |
a4b12990 MB |
564 | { |
565 | struct sst_mem_block *block; | |
566 | ||
567 | list_for_each_entry(block, &dsp->free_block_list, list) { | |
e9600bc1 | 568 | if (block->type == ba->type && block->offset == ba->offset) |
a4b12990 MB |
569 | return block; |
570 | } | |
571 | ||
572 | return NULL; | |
573 | } | |
574 | ||
e9600bc1 LG |
575 | /* Block allocator must be on block boundary */ |
576 | static int block_alloc_contiguous(struct sst_dsp *dsp, | |
577 | struct sst_block_allocator *ba, struct list_head *block_list) | |
a4b12990 MB |
578 | { |
579 | struct list_head tmp = LIST_HEAD_INIT(tmp); | |
a4b12990 | 580 | struct sst_mem_block *block; |
e9600bc1 LG |
581 | u32 block_start = SST_HSW_BLOCK_ANY; |
582 | int size = ba->size, offset = ba->offset; | |
a4b12990 | 583 | |
e9600bc1 LG |
584 | while (ba->size > 0) { |
585 | ||
586 | block = find_block(dsp, ba); | |
a4b12990 MB |
587 | if (!block) { |
588 | list_splice(&tmp, &dsp->free_block_list); | |
e9600bc1 LG |
589 | |
590 | ba->size = size; | |
591 | ba->offset = offset; | |
a4b12990 MB |
592 | return -ENOMEM; |
593 | } | |
594 | ||
595 | list_move_tail(&block->list, &tmp); | |
e9600bc1 LG |
596 | ba->offset += block->size; |
597 | ba->size -= block->size; | |
a4b12990 | 598 | } |
e9600bc1 LG |
599 | ba->size = size; |
600 | ba->offset = offset; | |
601 | ||
602 | list_for_each_entry(block, &tmp, list) { | |
603 | ||
604 | if (block->offset < block_start) | |
605 | block_start = block->offset; | |
606 | ||
607 | list_add(&block->module_list, block_list); | |
a4b12990 | 608 | |
e9600bc1 LG |
609 | dev_dbg(dsp->dev, "block allocated %d:%d at offset 0x%x\n", |
610 | block->type, block->index, block->offset); | |
611 | } | |
84fbdd58 | 612 | |
a4b12990 MB |
613 | list_splice(&tmp, &dsp->used_block_list); |
614 | return 0; | |
615 | } | |
616 | ||
e9600bc1 LG |
617 | /* allocate first free DSP blocks for data - callers hold locks */ |
618 | static int block_alloc(struct sst_dsp *dsp, struct sst_block_allocator *ba, | |
619 | struct list_head *block_list) | |
a4b12990 | 620 | { |
a4b12990 MB |
621 | struct sst_mem_block *block, *tmp; |
622 | int ret = 0; | |
623 | ||
e9600bc1 | 624 | if (ba->size == 0) |
a4b12990 MB |
625 | return 0; |
626 | ||
627 | /* find first free whole blocks that can hold module */ | |
628 | list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) { | |
629 | ||
630 | /* ignore blocks with wrong type */ | |
e9600bc1 | 631 | if (block->type != ba->type) |
a4b12990 MB |
632 | continue; |
633 | ||
e9600bc1 | 634 | if (ba->size > block->size) |
a4b12990 MB |
635 | continue; |
636 | ||
e9600bc1 LG |
637 | ba->offset = block->offset; |
638 | block->bytes_used = ba->size % block->size; | |
639 | list_add(&block->module_list, block_list); | |
a4b12990 | 640 | list_move(&block->list, &dsp->used_block_list); |
e9600bc1 LG |
641 | dev_dbg(dsp->dev, "block allocated %d:%d at offset 0x%x\n", |
642 | block->type, block->index, block->offset); | |
a4b12990 MB |
643 | return 0; |
644 | } | |
645 | ||
646 | /* then find free multiple blocks that can hold module */ | |
647 | list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) { | |
648 | ||
649 | /* ignore blocks with wrong type */ | |
e9600bc1 | 650 | if (block->type != ba->type) |
a4b12990 MB |
651 | continue; |
652 | ||
653 | /* do we span > 1 blocks */ | |
e9600bc1 LG |
654 | if (ba->size > block->size) { |
655 | ||
656 | /* align ba to block boundary */ | |
657 | ba->offset = block->offset; | |
658 | ||
659 | ret = block_alloc_contiguous(dsp, ba, block_list); | |
a4b12990 MB |
660 | if (ret == 0) |
661 | return ret; | |
e9600bc1 | 662 | |
a4b12990 MB |
663 | } |
664 | } | |
665 | ||
666 | /* not enough free block space */ | |
667 | return -ENOMEM; | |
668 | } | |
669 | ||
e9600bc1 LG |
670 | int sst_alloc_blocks(struct sst_dsp *dsp, struct sst_block_allocator *ba, |
671 | struct list_head *block_list) | |
a4b12990 | 672 | { |
e9600bc1 | 673 | int ret; |
a4b12990 | 674 | |
e9600bc1 LG |
675 | dev_dbg(dsp->dev, "block request 0x%x bytes at offset 0x%x type %d\n", |
676 | ba->size, ba->offset, ba->type); | |
a4b12990 | 677 | |
e9600bc1 | 678 | mutex_lock(&dsp->mutex); |
a4b12990 | 679 | |
e9600bc1 LG |
680 | ret = block_alloc(dsp, ba, block_list); |
681 | if (ret < 0) { | |
682 | dev_err(dsp->dev, "error: can't alloc blocks %d\n", ret); | |
683 | goto out; | |
a4b12990 | 684 | } |
a4b12990 | 685 | |
e9600bc1 LG |
686 | /* prepare DSP blocks for module usage */ |
687 | ret = block_list_prepare(dsp, block_list); | |
688 | if (ret < 0) | |
689 | dev_err(dsp->dev, "error: prepare failed\n"); | |
a4b12990 | 690 | |
e9600bc1 LG |
691 | out: |
692 | mutex_unlock(&dsp->mutex); | |
a4b12990 | 693 | return ret; |
e9600bc1 LG |
694 | } |
695 | EXPORT_SYMBOL_GPL(sst_alloc_blocks); | |
a4b12990 | 696 | |
e9600bc1 LG |
697 | int sst_free_blocks(struct sst_dsp *dsp, struct list_head *block_list) |
698 | { | |
699 | mutex_lock(&dsp->mutex); | |
700 | block_list_remove(dsp, block_list); | |
701 | mutex_unlock(&dsp->mutex); | |
702 | return 0; | |
a4b12990 | 703 | } |
e9600bc1 | 704 | EXPORT_SYMBOL_GPL(sst_free_blocks); |
a4b12990 MB |
705 | |
706 | /* allocate memory blocks for static module addresses - callers hold locks */ | |
e9600bc1 LG |
707 | static int block_alloc_fixed(struct sst_dsp *dsp, struct sst_block_allocator *ba, |
708 | struct list_head *block_list) | |
a4b12990 | 709 | { |
a4b12990 | 710 | struct sst_mem_block *block, *tmp; |
d83901e8 | 711 | struct sst_block_allocator ba_tmp = *ba; |
e9600bc1 | 712 | u32 end = ba->offset + ba->size, block_end; |
a4b12990 MB |
713 | int err; |
714 | ||
715 | /* only IRAM/DRAM blocks are managed */ | |
e9600bc1 | 716 | if (ba->type != SST_MEM_IRAM && ba->type != SST_MEM_DRAM) |
a4b12990 MB |
717 | return 0; |
718 | ||
719 | /* are blocks already attached to this module */ | |
e9600bc1 | 720 | list_for_each_entry_safe(block, tmp, block_list, module_list) { |
a4b12990 | 721 | |
e9600bc1 LG |
722 | /* ignore blocks with wrong type */ |
723 | if (block->type != ba->type) | |
a4b12990 MB |
724 | continue; |
725 | ||
726 | block_end = block->offset + block->size; | |
727 | ||
728 | /* find block that holds section */ | |
e9600bc1 | 729 | if (ba->offset >= block->offset && end <= block_end) |
a4b12990 MB |
730 | return 0; |
731 | ||
732 | /* does block span more than 1 section */ | |
e9600bc1 | 733 | if (ba->offset >= block->offset && ba->offset < block_end) { |
a4b12990 | 734 | |
e9600bc1 | 735 | /* align ba to block boundary */ |
d83901e8 JY |
736 | ba_tmp.size -= block_end - ba->offset; |
737 | ba_tmp.offset = block_end; | |
738 | err = block_alloc_contiguous(dsp, &ba_tmp, block_list); | |
a4b12990 MB |
739 | if (err < 0) |
740 | return -ENOMEM; | |
741 | ||
742 | /* module already owns blocks */ | |
743 | return 0; | |
744 | } | |
745 | } | |
746 | ||
747 | /* find first free blocks that can hold section in free list */ | |
748 | list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) { | |
749 | block_end = block->offset + block->size; | |
750 | ||
e9600bc1 LG |
751 | /* ignore blocks with wrong type */ |
752 | if (block->type != ba->type) | |
753 | continue; | |
754 | ||
a4b12990 | 755 | /* find block that holds section */ |
e9600bc1 | 756 | if (ba->offset >= block->offset && end <= block_end) { |
a4b12990 MB |
757 | |
758 | /* add block */ | |
a4b12990 | 759 | list_move(&block->list, &dsp->used_block_list); |
e9600bc1 LG |
760 | list_add(&block->module_list, block_list); |
761 | dev_dbg(dsp->dev, "block allocated %d:%d at offset 0x%x\n", | |
762 | block->type, block->index, block->offset); | |
a4b12990 MB |
763 | return 0; |
764 | } | |
765 | ||
766 | /* does block span more than 1 section */ | |
e9600bc1 | 767 | if (ba->offset >= block->offset && ba->offset < block_end) { |
a4b12990 | 768 | |
25f97549 JY |
769 | /* add block */ |
770 | list_move(&block->list, &dsp->used_block_list); | |
771 | list_add(&block->module_list, block_list); | |
e9600bc1 | 772 | /* align ba to block boundary */ |
d83901e8 JY |
773 | ba_tmp.size -= block_end - ba->offset; |
774 | ba_tmp.offset = block_end; | |
e9600bc1 | 775 | |
d83901e8 | 776 | err = block_alloc_contiguous(dsp, &ba_tmp, block_list); |
a4b12990 MB |
777 | if (err < 0) |
778 | return -ENOMEM; | |
779 | ||
a4b12990 MB |
780 | return 0; |
781 | } | |
a4b12990 MB |
782 | } |
783 | ||
784 | return -ENOMEM; | |
785 | } | |
786 | ||
787 | /* Load fixed module data into DSP memory blocks */ | |
e9600bc1 | 788 | int sst_module_alloc_blocks(struct sst_module *module) |
a4b12990 MB |
789 | { |
790 | struct sst_dsp *dsp = module->dsp; | |
e9600bc1 LG |
791 | struct sst_fw *sst_fw = module->sst_fw; |
792 | struct sst_block_allocator ba; | |
a4b12990 MB |
793 | int ret; |
794 | ||
c41cda1d | 795 | memset(&ba, 0, sizeof(ba)); |
e9600bc1 LG |
796 | ba.size = module->size; |
797 | ba.type = module->type; | |
798 | ba.offset = module->offset; | |
799 | ||
800 | dev_dbg(dsp->dev, "block request 0x%x bytes at offset 0x%x type %d\n", | |
801 | ba.size, ba.offset, ba.type); | |
802 | ||
a4b12990 MB |
803 | mutex_lock(&dsp->mutex); |
804 | ||
805 | /* alloc blocks that includes this section */ | |
e9600bc1 | 806 | ret = block_alloc_fixed(dsp, &ba, &module->block_list); |
a4b12990 MB |
807 | if (ret < 0) { |
808 | dev_err(dsp->dev, | |
809 | "error: no free blocks for section at offset 0x%x size 0x%x\n", | |
e9600bc1 | 810 | module->offset, module->size); |
a4b12990 MB |
811 | mutex_unlock(&dsp->mutex); |
812 | return -ENOMEM; | |
813 | } | |
814 | ||
815 | /* prepare DSP blocks for module copy */ | |
e9600bc1 | 816 | ret = block_list_prepare(dsp, &module->block_list); |
a4b12990 MB |
817 | if (ret < 0) { |
818 | dev_err(dsp->dev, "error: fw module prepare failed\n"); | |
819 | goto err; | |
820 | } | |
821 | ||
822 | /* copy partial module data to blocks */ | |
e9600bc1 LG |
823 | if (dsp->fw_use_dma) { |
824 | ret = sst_dsp_dma_copyto(dsp, | |
825 | dsp->addr.lpe_base + module->offset, | |
826 | sst_fw->dmable_fw_paddr + module->data_offset, | |
827 | module->size); | |
828 | if (ret < 0) { | |
829 | dev_err(dsp->dev, "error: module copy failed\n"); | |
830 | goto err; | |
831 | } | |
832 | } else | |
833 | sst_memcpy32(dsp->addr.lpe + module->offset, module->data, | |
834 | module->size); | |
a4b12990 MB |
835 | |
836 | mutex_unlock(&dsp->mutex); | |
837 | return ret; | |
838 | ||
839 | err: | |
e9600bc1 | 840 | block_list_remove(dsp, &module->block_list); |
a4b12990 MB |
841 | mutex_unlock(&dsp->mutex); |
842 | return ret; | |
843 | } | |
e9600bc1 | 844 | EXPORT_SYMBOL_GPL(sst_module_alloc_blocks); |
a4b12990 MB |
845 | |
846 | /* Unload entire module from DSP memory */ | |
e9600bc1 | 847 | int sst_module_free_blocks(struct sst_module *module) |
a4b12990 MB |
848 | { |
849 | struct sst_dsp *dsp = module->dsp; | |
850 | ||
851 | mutex_lock(&dsp->mutex); | |
e9600bc1 LG |
852 | block_list_remove(dsp, &module->block_list); |
853 | mutex_unlock(&dsp->mutex); | |
854 | return 0; | |
855 | } | |
856 | EXPORT_SYMBOL_GPL(sst_module_free_blocks); | |
857 | ||
858 | int sst_module_runtime_alloc_blocks(struct sst_module_runtime *runtime, | |
859 | int offset) | |
860 | { | |
861 | struct sst_dsp *dsp = runtime->dsp; | |
862 | struct sst_module *module = runtime->module; | |
863 | struct sst_block_allocator ba; | |
864 | int ret; | |
865 | ||
866 | if (module->persistent_size == 0) | |
867 | return 0; | |
868 | ||
c41cda1d | 869 | memset(&ba, 0, sizeof(ba)); |
e9600bc1 LG |
870 | ba.size = module->persistent_size; |
871 | ba.type = SST_MEM_DRAM; | |
872 | ||
873 | mutex_lock(&dsp->mutex); | |
874 | ||
875 | /* do we need to allocate at a fixed address ? */ | |
876 | if (offset != 0) { | |
877 | ||
878 | ba.offset = offset; | |
879 | ||
880 | dev_dbg(dsp->dev, "persistent fixed block request 0x%x bytes type %d offset 0x%x\n", | |
881 | ba.size, ba.type, ba.offset); | |
882 | ||
883 | /* alloc blocks that includes this section */ | |
884 | ret = block_alloc_fixed(dsp, &ba, &runtime->block_list); | |
885 | ||
886 | } else { | |
887 | dev_dbg(dsp->dev, "persistent block request 0x%x bytes type %d\n", | |
888 | ba.size, ba.type); | |
889 | ||
890 | /* alloc blocks that includes this section */ | |
891 | ret = block_alloc(dsp, &ba, &runtime->block_list); | |
892 | } | |
893 | if (ret < 0) { | |
894 | dev_err(dsp->dev, | |
895 | "error: no free blocks for runtime module size 0x%x\n", | |
896 | module->persistent_size); | |
897 | mutex_unlock(&dsp->mutex); | |
898 | return -ENOMEM; | |
899 | } | |
900 | runtime->persistent_offset = ba.offset; | |
901 | ||
902 | /* prepare DSP blocks for module copy */ | |
903 | ret = block_list_prepare(dsp, &runtime->block_list); | |
904 | if (ret < 0) { | |
905 | dev_err(dsp->dev, "error: runtime block prepare failed\n"); | |
906 | goto err; | |
907 | } | |
908 | ||
909 | mutex_unlock(&dsp->mutex); | |
910 | return ret; | |
911 | ||
912 | err: | |
913 | block_list_remove(dsp, &module->block_list); | |
914 | mutex_unlock(&dsp->mutex); | |
915 | return ret; | |
916 | } | |
917 | EXPORT_SYMBOL_GPL(sst_module_runtime_alloc_blocks); | |
918 | ||
919 | int sst_module_runtime_free_blocks(struct sst_module_runtime *runtime) | |
920 | { | |
921 | struct sst_dsp *dsp = runtime->dsp; | |
922 | ||
923 | mutex_lock(&dsp->mutex); | |
924 | block_list_remove(dsp, &runtime->block_list); | |
a4b12990 MB |
925 | mutex_unlock(&dsp->mutex); |
926 | return 0; | |
927 | } | |
e9600bc1 LG |
928 | EXPORT_SYMBOL_GPL(sst_module_runtime_free_blocks); |
929 | ||
930 | int sst_module_runtime_save(struct sst_module_runtime *runtime, | |
931 | struct sst_module_runtime_context *context) | |
932 | { | |
933 | struct sst_dsp *dsp = runtime->dsp; | |
934 | struct sst_module *module = runtime->module; | |
935 | int ret = 0; | |
936 | ||
937 | dev_dbg(dsp->dev, "saving runtime %d memory at 0x%x size 0x%x\n", | |
938 | runtime->id, runtime->persistent_offset, | |
939 | module->persistent_size); | |
940 | ||
941 | context->buffer = dma_alloc_coherent(dsp->dma_dev, | |
942 | module->persistent_size, | |
943 | &context->dma_buffer, GFP_DMA | GFP_KERNEL); | |
944 | if (!context->buffer) { | |
945 | dev_err(dsp->dev, "error: DMA context alloc failed\n"); | |
946 | return -ENOMEM; | |
947 | } | |
948 | ||
949 | mutex_lock(&dsp->mutex); | |
950 | ||
951 | if (dsp->fw_use_dma) { | |
952 | ||
953 | ret = sst_dsp_dma_get_channel(dsp, 0); | |
954 | if (ret < 0) | |
955 | goto err; | |
956 | ||
957 | ret = sst_dsp_dma_copyfrom(dsp, context->dma_buffer, | |
958 | dsp->addr.lpe_base + runtime->persistent_offset, | |
959 | module->persistent_size); | |
960 | sst_dsp_dma_put_channel(dsp); | |
961 | if (ret < 0) { | |
962 | dev_err(dsp->dev, "error: context copy failed\n"); | |
963 | goto err; | |
964 | } | |
965 | } else | |
966 | sst_memcpy32(context->buffer, dsp->addr.lpe + | |
967 | runtime->persistent_offset, | |
968 | module->persistent_size); | |
969 | ||
970 | err: | |
971 | mutex_unlock(&dsp->mutex); | |
972 | return ret; | |
973 | } | |
974 | EXPORT_SYMBOL_GPL(sst_module_runtime_save); | |
975 | ||
976 | int sst_module_runtime_restore(struct sst_module_runtime *runtime, | |
977 | struct sst_module_runtime_context *context) | |
978 | { | |
979 | struct sst_dsp *dsp = runtime->dsp; | |
980 | struct sst_module *module = runtime->module; | |
981 | int ret = 0; | |
982 | ||
983 | dev_dbg(dsp->dev, "restoring runtime %d memory at 0x%x size 0x%x\n", | |
984 | runtime->id, runtime->persistent_offset, | |
985 | module->persistent_size); | |
986 | ||
987 | mutex_lock(&dsp->mutex); | |
988 | ||
989 | if (!context->buffer) { | |
990 | dev_info(dsp->dev, "no context buffer need to restore!\n"); | |
991 | goto err; | |
992 | } | |
993 | ||
994 | if (dsp->fw_use_dma) { | |
995 | ||
996 | ret = sst_dsp_dma_get_channel(dsp, 0); | |
997 | if (ret < 0) | |
998 | goto err; | |
999 | ||
1000 | ret = sst_dsp_dma_copyto(dsp, | |
1001 | dsp->addr.lpe_base + runtime->persistent_offset, | |
1002 | context->dma_buffer, module->persistent_size); | |
1003 | sst_dsp_dma_put_channel(dsp); | |
1004 | if (ret < 0) { | |
1005 | dev_err(dsp->dev, "error: module copy failed\n"); | |
1006 | goto err; | |
1007 | } | |
1008 | } else | |
1009 | sst_memcpy32(dsp->addr.lpe + runtime->persistent_offset, | |
1010 | context->buffer, module->persistent_size); | |
1011 | ||
1012 | dma_free_coherent(dsp->dma_dev, module->persistent_size, | |
1013 | context->buffer, context->dma_buffer); | |
1014 | context->buffer = NULL; | |
1015 | ||
1016 | err: | |
1017 | mutex_unlock(&dsp->mutex); | |
1018 | return ret; | |
1019 | } | |
1020 | EXPORT_SYMBOL_GPL(sst_module_runtime_restore); | |
a4b12990 MB |
1021 | |
1022 | /* register a DSP memory block for use with FW based modules */ | |
1023 | struct sst_mem_block *sst_mem_block_register(struct sst_dsp *dsp, u32 offset, | |
1024 | u32 size, enum sst_mem_type type, struct sst_block_ops *ops, u32 index, | |
1025 | void *private) | |
1026 | { | |
1027 | struct sst_mem_block *block; | |
1028 | ||
1029 | block = kzalloc(sizeof(*block), GFP_KERNEL); | |
1030 | if (block == NULL) | |
1031 | return NULL; | |
1032 | ||
1033 | block->offset = offset; | |
1034 | block->size = size; | |
1035 | block->index = index; | |
1036 | block->type = type; | |
1037 | block->dsp = dsp; | |
1038 | block->private = private; | |
1039 | block->ops = ops; | |
1040 | ||
1041 | mutex_lock(&dsp->mutex); | |
1042 | list_add(&block->list, &dsp->free_block_list); | |
1043 | mutex_unlock(&dsp->mutex); | |
1044 | ||
1045 | return block; | |
1046 | } | |
1047 | EXPORT_SYMBOL_GPL(sst_mem_block_register); | |
1048 | ||
1049 | /* unregister all DSP memory blocks */ | |
1050 | void sst_mem_block_unregister_all(struct sst_dsp *dsp) | |
1051 | { | |
1052 | struct sst_mem_block *block, *tmp; | |
1053 | ||
1054 | mutex_lock(&dsp->mutex); | |
1055 | ||
1056 | /* unregister used blocks */ | |
1057 | list_for_each_entry_safe(block, tmp, &dsp->used_block_list, list) { | |
1058 | list_del(&block->list); | |
1059 | kfree(block); | |
1060 | } | |
1061 | ||
1062 | /* unregister free blocks */ | |
1063 | list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) { | |
1064 | list_del(&block->list); | |
1065 | kfree(block); | |
1066 | } | |
1067 | ||
1068 | mutex_unlock(&dsp->mutex); | |
1069 | } | |
1070 | EXPORT_SYMBOL_GPL(sst_mem_block_unregister_all); | |
1071 | ||
1072 | /* allocate scratch buffer blocks */ | |
e9600bc1 | 1073 | int sst_block_alloc_scratch(struct sst_dsp *dsp) |
a4b12990 | 1074 | { |
e9600bc1 LG |
1075 | struct sst_module *module; |
1076 | struct sst_block_allocator ba; | |
1077 | int ret; | |
a4b12990 MB |
1078 | |
1079 | mutex_lock(&dsp->mutex); | |
1080 | ||
1081 | /* calculate required scratch size */ | |
e9600bc1 LG |
1082 | dsp->scratch_size = 0; |
1083 | list_for_each_entry(module, &dsp->module_list, list) { | |
1084 | dev_dbg(dsp->dev, "module %d scratch req 0x%x bytes\n", | |
1085 | module->id, module->scratch_size); | |
1086 | if (dsp->scratch_size < module->scratch_size) | |
1087 | dsp->scratch_size = module->scratch_size; | |
a4b12990 MB |
1088 | } |
1089 | ||
e9600bc1 LG |
1090 | dev_dbg(dsp->dev, "scratch buffer required is 0x%x bytes\n", |
1091 | dsp->scratch_size); | |
a4b12990 | 1092 | |
e9600bc1 LG |
1093 | if (dsp->scratch_size == 0) { |
1094 | dev_info(dsp->dev, "no modules need scratch buffer\n"); | |
1095 | mutex_unlock(&dsp->mutex); | |
1096 | return 0; | |
1097 | } | |
a4b12990 MB |
1098 | |
1099 | /* allocate blocks for module scratch buffers */ | |
1100 | dev_dbg(dsp->dev, "allocating scratch blocks\n"); | |
e9600bc1 LG |
1101 | |
1102 | ba.size = dsp->scratch_size; | |
1103 | ba.type = SST_MEM_DRAM; | |
1104 | ||
1105 | /* do we need to allocate at fixed offset */ | |
1106 | if (dsp->scratch_offset != 0) { | |
1107 | ||
1108 | dev_dbg(dsp->dev, "block request 0x%x bytes type %d at 0x%x\n", | |
1109 | ba.size, ba.type, ba.offset); | |
1110 | ||
1111 | ba.offset = dsp->scratch_offset; | |
1112 | ||
1113 | /* alloc blocks that includes this section */ | |
1114 | ret = block_alloc_fixed(dsp, &ba, &dsp->scratch_block_list); | |
1115 | ||
1116 | } else { | |
1117 | dev_dbg(dsp->dev, "block request 0x%x bytes type %d\n", | |
1118 | ba.size, ba.type); | |
1119 | ||
1120 | ba.offset = 0; | |
1121 | ret = block_alloc(dsp, &ba, &dsp->scratch_block_list); | |
1122 | } | |
a4b12990 MB |
1123 | if (ret < 0) { |
1124 | dev_err(dsp->dev, "error: can't alloc scratch blocks\n"); | |
e9600bc1 LG |
1125 | mutex_unlock(&dsp->mutex); |
1126 | return ret; | |
a4b12990 MB |
1127 | } |
1128 | ||
e9600bc1 LG |
1129 | ret = block_list_prepare(dsp, &dsp->scratch_block_list); |
1130 | if (ret < 0) { | |
1131 | dev_err(dsp->dev, "error: scratch block prepare failed\n"); | |
22a236b4 | 1132 | mutex_unlock(&dsp->mutex); |
e9600bc1 LG |
1133 | return ret; |
1134 | } | |
a4b12990 | 1135 | |
e9600bc1 LG |
1136 | /* assign the same offset of scratch to each module */ |
1137 | dsp->scratch_offset = ba.offset; | |
a4b12990 | 1138 | mutex_unlock(&dsp->mutex); |
e9600bc1 | 1139 | return dsp->scratch_size; |
a4b12990 | 1140 | } |
e9600bc1 | 1141 | EXPORT_SYMBOL_GPL(sst_block_alloc_scratch); |
a4b12990 MB |
1142 | |
1143 | /* free all scratch blocks */ | |
e9600bc1 | 1144 | void sst_block_free_scratch(struct sst_dsp *dsp) |
a4b12990 | 1145 | { |
a4b12990 | 1146 | mutex_lock(&dsp->mutex); |
e9600bc1 | 1147 | block_list_remove(dsp, &dsp->scratch_block_list); |
a4b12990 MB |
1148 | mutex_unlock(&dsp->mutex); |
1149 | } | |
e9600bc1 | 1150 | EXPORT_SYMBOL_GPL(sst_block_free_scratch); |
a4b12990 MB |
1151 | |
1152 | /* get a module from it's unique ID */ | |
1153 | struct sst_module *sst_module_get_from_id(struct sst_dsp *dsp, u32 id) | |
1154 | { | |
1155 | struct sst_module *module; | |
1156 | ||
1157 | mutex_lock(&dsp->mutex); | |
1158 | ||
1159 | list_for_each_entry(module, &dsp->module_list, list) { | |
1160 | if (module->id == id) { | |
1161 | mutex_unlock(&dsp->mutex); | |
1162 | return module; | |
1163 | } | |
1164 | } | |
1165 | ||
1166 | mutex_unlock(&dsp->mutex); | |
1167 | return NULL; | |
1168 | } | |
1169 | EXPORT_SYMBOL_GPL(sst_module_get_from_id); | |
e9600bc1 LG |
1170 | |
1171 | struct sst_module_runtime *sst_module_runtime_get_from_id( | |
1172 | struct sst_module *module, u32 id) | |
1173 | { | |
1174 | struct sst_module_runtime *runtime; | |
1175 | struct sst_dsp *dsp = module->dsp; | |
1176 | ||
1177 | mutex_lock(&dsp->mutex); | |
1178 | ||
1179 | list_for_each_entry(runtime, &module->runtime_list, list) { | |
1180 | if (runtime->id == id) { | |
1181 | mutex_unlock(&dsp->mutex); | |
1182 | return runtime; | |
1183 | } | |
1184 | } | |
1185 | ||
1186 | mutex_unlock(&dsp->mutex); | |
1187 | return NULL; | |
1188 | } | |
1189 | EXPORT_SYMBOL_GPL(sst_module_runtime_get_from_id); | |
1190 | ||
1191 | /* returns block address in DSP address space */ | |
1192 | u32 sst_dsp_get_offset(struct sst_dsp *dsp, u32 offset, | |
1193 | enum sst_mem_type type) | |
1194 | { | |
1195 | switch (type) { | |
1196 | case SST_MEM_IRAM: | |
1197 | return offset - dsp->addr.iram_offset + | |
1198 | dsp->addr.dsp_iram_offset; | |
1199 | case SST_MEM_DRAM: | |
1200 | return offset - dsp->addr.dram_offset + | |
1201 | dsp->addr.dsp_dram_offset; | |
1202 | default: | |
1203 | return 0; | |
1204 | } | |
1205 | } | |
1206 | EXPORT_SYMBOL_GPL(sst_dsp_get_offset); |