• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <linux/acpi.h>
27 
28 /* supported DMA engine drivers */
29 #include <linux/dma/dw.h>
30 
31 #include <asm/page.h>
32 #include <asm/pgtable.h>
33 
34 #include "sst-dsp.h"
35 #include "sst-dsp-priv.h"
36 
37 #define SST_DMA_RESOURCES	2
38 #define SST_DSP_DMA_MAX_BURST	0x3
39 #define SST_HSW_BLOCK_ANY	0xffffffff
40 
41 #define SST_HSW_MASK_DMA_ADDR_DSP 0xfff00000
42 
43 struct sst_dma {
44 	struct sst_dsp *sst;
45 
46 	struct dw_dma_chip *chip;
47 
48 	struct dma_async_tx_descriptor *desc;
49 	struct dma_chan *ch;
50 };
51 
sst_memcpy32(volatile void __iomem * dest,void * src,u32 bytes)52 static inline void sst_memcpy32(volatile void __iomem *dest, void *src, u32 bytes)
53 {
54 	/* __iowrite32_copy use 32bit size values so divide by 4 */
55 	__iowrite32_copy((void *)dest, src, bytes/4);
56 }
57 
sst_dma_transfer_complete(void * arg)58 static void sst_dma_transfer_complete(void *arg)
59 {
60 	struct sst_dsp *sst = (struct sst_dsp *)arg;
61 
62 	dev_dbg(sst->dev, "DMA: callback\n");
63 }
64 
sst_dsp_dma_copy(struct sst_dsp * sst,dma_addr_t dest_addr,dma_addr_t src_addr,size_t size)65 static int sst_dsp_dma_copy(struct sst_dsp *sst, dma_addr_t dest_addr,
66 	dma_addr_t src_addr, size_t size)
67 {
68 	struct dma_async_tx_descriptor *desc;
69 	struct sst_dma *dma = sst->dma;
70 
71 	if (dma->ch == NULL) {
72 		dev_err(sst->dev, "error: no DMA channel\n");
73 		return -ENODEV;
74 	}
75 
76 	dev_dbg(sst->dev, "DMA: src: 0x%lx dest 0x%lx size %zu\n",
77 		(unsigned long)src_addr, (unsigned long)dest_addr, size);
78 
79 	desc = dma->ch->device->device_prep_dma_memcpy(dma->ch, dest_addr,
80 		src_addr, size, DMA_CTRL_ACK);
81 	if (!desc){
82 		dev_err(sst->dev, "error: dma prep memcpy failed\n");
83 		return -EINVAL;
84 	}
85 
86 	desc->callback = sst_dma_transfer_complete;
87 	desc->callback_param = sst;
88 
89 	desc->tx_submit(desc);
90 	dma_wait_for_async_tx(desc);
91 
92 	return 0;
93 }
94 
95 /* copy to DSP */
sst_dsp_dma_copyto(struct sst_dsp * sst,dma_addr_t dest_addr,dma_addr_t src_addr,size_t size)96 int sst_dsp_dma_copyto(struct sst_dsp *sst, dma_addr_t dest_addr,
97 	dma_addr_t src_addr, size_t size)
98 {
99 	return sst_dsp_dma_copy(sst, dest_addr | SST_HSW_MASK_DMA_ADDR_DSP,
100 			src_addr, size);
101 }
102 EXPORT_SYMBOL_GPL(sst_dsp_dma_copyto);
103 
104 /* copy from DSP */
sst_dsp_dma_copyfrom(struct sst_dsp * sst,dma_addr_t dest_addr,dma_addr_t src_addr,size_t size)105 int sst_dsp_dma_copyfrom(struct sst_dsp *sst, dma_addr_t dest_addr,
106 	dma_addr_t src_addr, size_t size)
107 {
108 	return sst_dsp_dma_copy(sst, dest_addr,
109 		src_addr | SST_HSW_MASK_DMA_ADDR_DSP, size);
110 }
111 EXPORT_SYMBOL_GPL(sst_dsp_dma_copyfrom);
112 
113 /* remove module from memory - callers hold locks */
block_list_remove(struct sst_dsp * dsp,struct list_head * block_list)114 static void block_list_remove(struct sst_dsp *dsp,
115 	struct list_head *block_list)
116 {
117 	struct sst_mem_block *block, *tmp;
118 	int err;
119 
120 	/* disable each block  */
121 	list_for_each_entry(block, block_list, module_list) {
122 
123 		if (block->ops && block->ops->disable) {
124 			err = block->ops->disable(block);
125 			if (err < 0)
126 				dev_err(dsp->dev,
127 					"error: cant disable block %d:%d\n",
128 					block->type, block->index);
129 		}
130 	}
131 
132 	/* mark each block as free */
133 	list_for_each_entry_safe(block, tmp, block_list, module_list) {
134 		list_del(&block->module_list);
135 		list_move(&block->list, &dsp->free_block_list);
136 		dev_dbg(dsp->dev, "block freed %d:%d at offset 0x%x\n",
137 			block->type, block->index, block->offset);
138 	}
139 }
140 
141 /* prepare the memory block to receive data from host - callers hold locks */
block_list_prepare(struct sst_dsp * dsp,struct list_head * block_list)142 static int block_list_prepare(struct sst_dsp *dsp,
143 	struct list_head *block_list)
144 {
145 	struct sst_mem_block *block;
146 	int ret = 0;
147 
148 	/* enable each block so that's it'e ready for data */
149 	list_for_each_entry(block, block_list, module_list) {
150 
151 		if (block->ops && block->ops->enable && !block->users) {
152 			ret = block->ops->enable(block);
153 			if (ret < 0) {
154 				dev_err(dsp->dev,
155 					"error: cant disable block %d:%d\n",
156 					block->type, block->index);
157 				goto err;
158 			}
159 		}
160 	}
161 	return ret;
162 
163 err:
164 	list_for_each_entry(block, block_list, module_list) {
165 		if (block->ops && block->ops->disable)
166 			block->ops->disable(block);
167 	}
168 	return ret;
169 }
170 
dw_probe(struct device * dev,struct resource * mem,int irq)171 static struct dw_dma_chip *dw_probe(struct device *dev, struct resource *mem,
172 	int irq)
173 {
174 	struct dw_dma_chip *chip;
175 	int err;
176 
177 	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
178 	if (!chip)
179 		return ERR_PTR(-ENOMEM);
180 
181 	chip->irq = irq;
182 	chip->regs = devm_ioremap_resource(dev, mem);
183 	if (IS_ERR(chip->regs))
184 		return ERR_CAST(chip->regs);
185 
186 	err = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(31));
187 	if (err)
188 		return ERR_PTR(err);
189 
190 	chip->dev = dev;
191 
192 	err = dw_dma_probe(chip, NULL);
193 	if (err)
194 		return ERR_PTR(err);
195 
196 	return chip;
197 }
198 
dw_remove(struct dw_dma_chip * chip)199 static void dw_remove(struct dw_dma_chip *chip)
200 {
201 	dw_dma_remove(chip);
202 }
203 
dma_chan_filter(struct dma_chan * chan,void * param)204 static bool dma_chan_filter(struct dma_chan *chan, void *param)
205 {
206 	struct sst_dsp *dsp = (struct sst_dsp *)param;
207 
208 	return chan->device->dev == dsp->dma_dev;
209 }
210 
sst_dsp_dma_get_channel(struct sst_dsp * dsp,int chan_id)211 int sst_dsp_dma_get_channel(struct sst_dsp *dsp, int chan_id)
212 {
213 	struct sst_dma *dma = dsp->dma;
214 	struct dma_slave_config slave;
215 	dma_cap_mask_t mask;
216 	int ret;
217 
218 	dma_cap_zero(mask);
219 	dma_cap_set(DMA_SLAVE, mask);
220 	dma_cap_set(DMA_MEMCPY, mask);
221 
222 	dma->ch = dma_request_channel(mask, dma_chan_filter, dsp);
223 	if (dma->ch == NULL) {
224 		dev_err(dsp->dev, "error: DMA request channel failed\n");
225 		return -EIO;
226 	}
227 
228 	memset(&slave, 0, sizeof(slave));
229 	slave.direction = DMA_MEM_TO_DEV;
230 	slave.src_addr_width =
231 		slave.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
232 	slave.src_maxburst = slave.dst_maxburst = SST_DSP_DMA_MAX_BURST;
233 
234 	ret = dmaengine_slave_config(dma->ch, &slave);
235 	if (ret) {
236 		dev_err(dsp->dev, "error: unable to set DMA slave config %d\n",
237 			ret);
238 		dma_release_channel(dma->ch);
239 		dma->ch = NULL;
240 	}
241 
242 	return ret;
243 }
244 EXPORT_SYMBOL_GPL(sst_dsp_dma_get_channel);
245 
sst_dsp_dma_put_channel(struct sst_dsp * dsp)246 void sst_dsp_dma_put_channel(struct sst_dsp *dsp)
247 {
248 	struct sst_dma *dma = dsp->dma;
249 
250 	if (!dma->ch)
251 		return;
252 
253 	dma_release_channel(dma->ch);
254 	dma->ch = NULL;
255 }
256 EXPORT_SYMBOL_GPL(sst_dsp_dma_put_channel);
257 
sst_dma_new(struct sst_dsp * sst)258 int sst_dma_new(struct sst_dsp *sst)
259 {
260 	struct sst_pdata *sst_pdata = sst->pdata;
261 	struct sst_dma *dma;
262 	struct resource mem;
263 	int ret = 0;
264 
265 	if (sst->pdata->resindex_dma_base == -1)
266 		/* DMA is not used, return and squelsh error messages */
267 		return 0;
268 
269 	/* configure the correct platform data for whatever DMA engine
270 	* is attached to the ADSP IP. */
271 	switch (sst->pdata->dma_engine) {
272 	case SST_DMA_TYPE_DW:
273 		break;
274 	default:
275 		dev_err(sst->dev, "error: invalid DMA engine %d\n",
276 			sst->pdata->dma_engine);
277 		return -EINVAL;
278 	}
279 
280 	dma = devm_kzalloc(sst->dev, sizeof(struct sst_dma), GFP_KERNEL);
281 	if (!dma)
282 		return -ENOMEM;
283 
284 	dma->sst = sst;
285 
286 	memset(&mem, 0, sizeof(mem));
287 
288 	mem.start = sst->addr.lpe_base + sst_pdata->dma_base;
289 	mem.end   = sst->addr.lpe_base + sst_pdata->dma_base + sst_pdata->dma_size - 1;
290 	mem.flags = IORESOURCE_MEM;
291 
292 	/* now register DMA engine device */
293 	dma->chip = dw_probe(sst->dma_dev, &mem, sst_pdata->irq);
294 	if (IS_ERR(dma->chip)) {
295 		dev_err(sst->dev, "error: DMA device register failed\n");
296 		ret = PTR_ERR(dma->chip);
297 		goto err_dma_dev;
298 	}
299 
300 	sst->dma = dma;
301 	sst->fw_use_dma = true;
302 	return 0;
303 
304 err_dma_dev:
305 	devm_kfree(sst->dev, dma);
306 	return ret;
307 }
308 EXPORT_SYMBOL(sst_dma_new);
309 
sst_dma_free(struct sst_dma * dma)310 void sst_dma_free(struct sst_dma *dma)
311 {
312 
313 	if (dma == NULL)
314 		return;
315 
316 	if (dma->ch)
317 		dma_release_channel(dma->ch);
318 
319 	if (dma->chip)
320 		dw_remove(dma->chip);
321 
322 }
323 EXPORT_SYMBOL(sst_dma_free);
324 
325 /* create new generic firmware object */
sst_fw_new(struct sst_dsp * dsp,const struct firmware * fw,void * private)326 struct sst_fw *sst_fw_new(struct sst_dsp *dsp,
327 	const struct firmware *fw, void *private)
328 {
329 	struct sst_fw *sst_fw;
330 	int err;
331 
332 	if (!dsp->ops->parse_fw)
333 		return NULL;
334 
335 	sst_fw = kzalloc(sizeof(*sst_fw), GFP_KERNEL);
336 	if (sst_fw == NULL)
337 		return NULL;
338 
339 	sst_fw->dsp = dsp;
340 	sst_fw->private = private;
341 	sst_fw->size = fw->size;
342 
343 	/* allocate DMA buffer to store FW data */
344 	sst_fw->dma_buf = dma_alloc_coherent(dsp->dma_dev, sst_fw->size,
345 				&sst_fw->dmable_fw_paddr, GFP_DMA | GFP_KERNEL);
346 	if (!sst_fw->dma_buf) {
347 		dev_err(dsp->dev, "error: DMA alloc failed\n");
348 		kfree(sst_fw);
349 		return NULL;
350 	}
351 
352 	/* copy FW data to DMA-able memory */
353 	memcpy((void *)sst_fw->dma_buf, (void *)fw->data, fw->size);
354 
355 	if (dsp->fw_use_dma) {
356 		err = sst_dsp_dma_get_channel(dsp, 0);
357 		if (err < 0)
358 			goto chan_err;
359 	}
360 
361 	/* call core specific FW paser to load FW data into DSP */
362 	err = dsp->ops->parse_fw(sst_fw);
363 	if (err < 0) {
364 		dev_err(dsp->dev, "error: parse fw failed %d\n", err);
365 		goto parse_err;
366 	}
367 
368 	if (dsp->fw_use_dma)
369 		sst_dsp_dma_put_channel(dsp);
370 
371 	mutex_lock(&dsp->mutex);
372 	list_add(&sst_fw->list, &dsp->fw_list);
373 	mutex_unlock(&dsp->mutex);
374 
375 	return sst_fw;
376 
377 parse_err:
378 	if (dsp->fw_use_dma)
379 		sst_dsp_dma_put_channel(dsp);
380 chan_err:
381 	dma_free_coherent(dsp->dma_dev, sst_fw->size,
382 				sst_fw->dma_buf,
383 				sst_fw->dmable_fw_paddr);
384 	sst_fw->dma_buf = NULL;
385 	kfree(sst_fw);
386 	return NULL;
387 }
388 EXPORT_SYMBOL_GPL(sst_fw_new);
389 
sst_fw_reload(struct sst_fw * sst_fw)390 int sst_fw_reload(struct sst_fw *sst_fw)
391 {
392 	struct sst_dsp *dsp = sst_fw->dsp;
393 	int ret;
394 
395 	dev_dbg(dsp->dev, "reloading firmware\n");
396 
397 	/* call core specific FW paser to load FW data into DSP */
398 	ret = dsp->ops->parse_fw(sst_fw);
399 	if (ret < 0)
400 		dev_err(dsp->dev, "error: parse fw failed %d\n", ret);
401 
402 	return ret;
403 }
404 EXPORT_SYMBOL_GPL(sst_fw_reload);
405 
sst_fw_unload(struct sst_fw * sst_fw)406 void sst_fw_unload(struct sst_fw *sst_fw)
407 {
408 	struct sst_dsp *dsp = sst_fw->dsp;
409 	struct sst_module *module, *mtmp;
410 	struct sst_module_runtime *runtime, *rtmp;
411 
412 	dev_dbg(dsp->dev, "unloading firmware\n");
413 
414 	mutex_lock(&dsp->mutex);
415 
416 	/* check module by module */
417 	list_for_each_entry_safe(module, mtmp, &dsp->module_list, list) {
418 		if (module->sst_fw == sst_fw) {
419 
420 			/* remove runtime modules */
421 			list_for_each_entry_safe(runtime, rtmp, &module->runtime_list, list) {
422 
423 				block_list_remove(dsp, &runtime->block_list);
424 				list_del(&runtime->list);
425 				kfree(runtime);
426 			}
427 
428 			/* now remove the module */
429 			block_list_remove(dsp, &module->block_list);
430 			list_del(&module->list);
431 			kfree(module);
432 		}
433 	}
434 
435 	/* remove all scratch blocks */
436 	block_list_remove(dsp, &dsp->scratch_block_list);
437 
438 	mutex_unlock(&dsp->mutex);
439 }
440 EXPORT_SYMBOL_GPL(sst_fw_unload);
441 
442 /* free single firmware object */
sst_fw_free(struct sst_fw * sst_fw)443 void sst_fw_free(struct sst_fw *sst_fw)
444 {
445 	struct sst_dsp *dsp = sst_fw->dsp;
446 
447 	mutex_lock(&dsp->mutex);
448 	list_del(&sst_fw->list);
449 	mutex_unlock(&dsp->mutex);
450 
451 	if (sst_fw->dma_buf)
452 		dma_free_coherent(dsp->dma_dev, sst_fw->size, sst_fw->dma_buf,
453 			sst_fw->dmable_fw_paddr);
454 	kfree(sst_fw);
455 }
456 EXPORT_SYMBOL_GPL(sst_fw_free);
457 
458 /* free all firmware objects */
sst_fw_free_all(struct sst_dsp * dsp)459 void sst_fw_free_all(struct sst_dsp *dsp)
460 {
461 	struct sst_fw *sst_fw, *t;
462 
463 	mutex_lock(&dsp->mutex);
464 	list_for_each_entry_safe(sst_fw, t, &dsp->fw_list, list) {
465 
466 		list_del(&sst_fw->list);
467 		dma_free_coherent(dsp->dev, sst_fw->size, sst_fw->dma_buf,
468 			sst_fw->dmable_fw_paddr);
469 		kfree(sst_fw);
470 	}
471 	mutex_unlock(&dsp->mutex);
472 }
473 EXPORT_SYMBOL_GPL(sst_fw_free_all);
474 
475 /* create a new SST generic module from FW template */
sst_module_new(struct sst_fw * sst_fw,struct sst_module_template * template,void * private)476 struct sst_module *sst_module_new(struct sst_fw *sst_fw,
477 	struct sst_module_template *template, void *private)
478 {
479 	struct sst_dsp *dsp = sst_fw->dsp;
480 	struct sst_module *sst_module;
481 
482 	sst_module = kzalloc(sizeof(*sst_module), GFP_KERNEL);
483 	if (sst_module == NULL)
484 		return NULL;
485 
486 	sst_module->id = template->id;
487 	sst_module->dsp = dsp;
488 	sst_module->sst_fw = sst_fw;
489 	sst_module->scratch_size = template->scratch_size;
490 	sst_module->persistent_size = template->persistent_size;
491 	sst_module->entry = template->entry;
492 	sst_module->state = SST_MODULE_STATE_UNLOADED;
493 
494 	INIT_LIST_HEAD(&sst_module->block_list);
495 	INIT_LIST_HEAD(&sst_module->runtime_list);
496 
497 	mutex_lock(&dsp->mutex);
498 	list_add(&sst_module->list, &dsp->module_list);
499 	mutex_unlock(&dsp->mutex);
500 
501 	return sst_module;
502 }
503 EXPORT_SYMBOL_GPL(sst_module_new);
504 
505 /* free firmware module and remove from available list */
sst_module_free(struct sst_module * sst_module)506 void sst_module_free(struct sst_module *sst_module)
507 {
508 	struct sst_dsp *dsp = sst_module->dsp;
509 
510 	mutex_lock(&dsp->mutex);
511 	list_del(&sst_module->list);
512 	mutex_unlock(&dsp->mutex);
513 
514 	kfree(sst_module);
515 }
516 EXPORT_SYMBOL_GPL(sst_module_free);
517 
sst_module_runtime_new(struct sst_module * module,int id,void * private)518 struct sst_module_runtime *sst_module_runtime_new(struct sst_module *module,
519 	int id, void *private)
520 {
521 	struct sst_dsp *dsp = module->dsp;
522 	struct sst_module_runtime *runtime;
523 
524 	runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
525 	if (runtime == NULL)
526 		return NULL;
527 
528 	runtime->id = id;
529 	runtime->dsp = dsp;
530 	runtime->module = module;
531 	INIT_LIST_HEAD(&runtime->block_list);
532 
533 	mutex_lock(&dsp->mutex);
534 	list_add(&runtime->list, &module->runtime_list);
535 	mutex_unlock(&dsp->mutex);
536 
537 	return runtime;
538 }
539 EXPORT_SYMBOL_GPL(sst_module_runtime_new);
540 
sst_module_runtime_free(struct sst_module_runtime * runtime)541 void sst_module_runtime_free(struct sst_module_runtime *runtime)
542 {
543 	struct sst_dsp *dsp = runtime->dsp;
544 
545 	mutex_lock(&dsp->mutex);
546 	list_del(&runtime->list);
547 	mutex_unlock(&dsp->mutex);
548 
549 	kfree(runtime);
550 }
551 EXPORT_SYMBOL_GPL(sst_module_runtime_free);
552 
find_block(struct sst_dsp * dsp,struct sst_block_allocator * ba)553 static struct sst_mem_block *find_block(struct sst_dsp *dsp,
554 	struct sst_block_allocator *ba)
555 {
556 	struct sst_mem_block *block;
557 
558 	list_for_each_entry(block, &dsp->free_block_list, list) {
559 		if (block->type == ba->type && block->offset == ba->offset)
560 			return block;
561 	}
562 
563 	return NULL;
564 }
565 
566 /* Block allocator must be on block boundary */
block_alloc_contiguous(struct sst_dsp * dsp,struct sst_block_allocator * ba,struct list_head * block_list)567 static int block_alloc_contiguous(struct sst_dsp *dsp,
568 	struct sst_block_allocator *ba, struct list_head *block_list)
569 {
570 	struct list_head tmp = LIST_HEAD_INIT(tmp);
571 	struct sst_mem_block *block;
572 	u32 block_start = SST_HSW_BLOCK_ANY;
573 	int size = ba->size, offset = ba->offset;
574 
575 	while (ba->size > 0) {
576 
577 		block = find_block(dsp, ba);
578 		if (!block) {
579 			list_splice(&tmp, &dsp->free_block_list);
580 
581 			ba->size = size;
582 			ba->offset = offset;
583 			return -ENOMEM;
584 		}
585 
586 		list_move_tail(&block->list, &tmp);
587 		ba->offset += block->size;
588 		ba->size -= block->size;
589 	}
590 	ba->size = size;
591 	ba->offset = offset;
592 
593 	list_for_each_entry(block, &tmp, list) {
594 
595 		if (block->offset < block_start)
596 			block_start = block->offset;
597 
598 		list_add(&block->module_list, block_list);
599 
600 		dev_dbg(dsp->dev, "block allocated %d:%d at offset 0x%x\n",
601 			block->type, block->index, block->offset);
602 	}
603 
604 	list_splice(&tmp, &dsp->used_block_list);
605 	return 0;
606 }
607 
608 /* allocate first free DSP blocks for data - callers hold locks */
block_alloc(struct sst_dsp * dsp,struct sst_block_allocator * ba,struct list_head * block_list)609 static int block_alloc(struct sst_dsp *dsp, struct sst_block_allocator *ba,
610 	struct list_head *block_list)
611 {
612 	struct sst_mem_block *block, *tmp;
613 	int ret = 0;
614 
615 	if (ba->size == 0)
616 		return 0;
617 
618 	/* find first free whole blocks that can hold module */
619 	list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
620 
621 		/* ignore blocks with wrong type */
622 		if (block->type != ba->type)
623 			continue;
624 
625 		if (ba->size > block->size)
626 			continue;
627 
628 		ba->offset = block->offset;
629 		block->bytes_used = ba->size % block->size;
630 		list_add(&block->module_list, block_list);
631 		list_move(&block->list, &dsp->used_block_list);
632 		dev_dbg(dsp->dev, "block allocated %d:%d at offset 0x%x\n",
633 			block->type, block->index, block->offset);
634 		return 0;
635 	}
636 
637 	/* then find free multiple blocks that can hold module */
638 	list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
639 
640 		/* ignore blocks with wrong type */
641 		if (block->type != ba->type)
642 			continue;
643 
644 		/* do we span > 1 blocks */
645 		if (ba->size > block->size) {
646 
647 			/* align ba to block boundary */
648 			ba->offset = block->offset;
649 
650 			ret = block_alloc_contiguous(dsp, ba, block_list);
651 			if (ret == 0)
652 				return ret;
653 
654 		}
655 	}
656 
657 	/* not enough free block space */
658 	return -ENOMEM;
659 }
660 
sst_alloc_blocks(struct sst_dsp * dsp,struct sst_block_allocator * ba,struct list_head * block_list)661 int sst_alloc_blocks(struct sst_dsp *dsp, struct sst_block_allocator *ba,
662 	struct list_head *block_list)
663 {
664 	int ret;
665 
666 	dev_dbg(dsp->dev, "block request 0x%x bytes at offset 0x%x type %d\n",
667 		ba->size, ba->offset, ba->type);
668 
669 	mutex_lock(&dsp->mutex);
670 
671 	ret = block_alloc(dsp, ba, block_list);
672 	if (ret < 0) {
673 		dev_err(dsp->dev, "error: can't alloc blocks %d\n", ret);
674 		goto out;
675 	}
676 
677 	/* prepare DSP blocks for module usage */
678 	ret = block_list_prepare(dsp, block_list);
679 	if (ret < 0)
680 		dev_err(dsp->dev, "error: prepare failed\n");
681 
682 out:
683 	mutex_unlock(&dsp->mutex);
684 	return ret;
685 }
686 EXPORT_SYMBOL_GPL(sst_alloc_blocks);
687 
sst_free_blocks(struct sst_dsp * dsp,struct list_head * block_list)688 int sst_free_blocks(struct sst_dsp *dsp, struct list_head *block_list)
689 {
690 	mutex_lock(&dsp->mutex);
691 	block_list_remove(dsp, block_list);
692 	mutex_unlock(&dsp->mutex);
693 	return 0;
694 }
695 EXPORT_SYMBOL_GPL(sst_free_blocks);
696 
697 /* allocate memory blocks for static module addresses - callers hold locks */
block_alloc_fixed(struct sst_dsp * dsp,struct sst_block_allocator * ba,struct list_head * block_list)698 static int block_alloc_fixed(struct sst_dsp *dsp, struct sst_block_allocator *ba,
699 	struct list_head *block_list)
700 {
701 	struct sst_mem_block *block, *tmp;
702 	struct sst_block_allocator ba_tmp = *ba;
703 	u32 end = ba->offset + ba->size, block_end;
704 	int err;
705 
706 	/* only IRAM/DRAM blocks are managed */
707 	if (ba->type != SST_MEM_IRAM && ba->type != SST_MEM_DRAM)
708 		return 0;
709 
710 	/* are blocks already attached to this module */
711 	list_for_each_entry_safe(block, tmp, block_list, module_list) {
712 
713 		/* ignore blocks with wrong type */
714 		if (block->type != ba->type)
715 			continue;
716 
717 		block_end = block->offset + block->size;
718 
719 		/* find block that holds section */
720 		if (ba->offset >= block->offset && end <= block_end)
721 			return 0;
722 
723 		/* does block span more than 1 section */
724 		if (ba->offset >= block->offset && ba->offset < block_end) {
725 
726 			/* align ba to block boundary */
727 			ba_tmp.size -= block_end - ba->offset;
728 			ba_tmp.offset = block_end;
729 			err = block_alloc_contiguous(dsp, &ba_tmp, block_list);
730 			if (err < 0)
731 				return -ENOMEM;
732 
733 			/* module already owns blocks */
734 			return 0;
735 		}
736 	}
737 
738 	/* find first free blocks that can hold section in free list */
739 	list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
740 		block_end = block->offset + block->size;
741 
742 		/* ignore blocks with wrong type */
743 		if (block->type != ba->type)
744 			continue;
745 
746 		/* find block that holds section */
747 		if (ba->offset >= block->offset && end <= block_end) {
748 
749 			/* add block */
750 			list_move(&block->list, &dsp->used_block_list);
751 			list_add(&block->module_list, block_list);
752 			dev_dbg(dsp->dev, "block allocated %d:%d at offset 0x%x\n",
753 				block->type, block->index, block->offset);
754 			return 0;
755 		}
756 
757 		/* does block span more than 1 section */
758 		if (ba->offset >= block->offset && ba->offset < block_end) {
759 
760 			/* add block */
761 			list_move(&block->list, &dsp->used_block_list);
762 			list_add(&block->module_list, block_list);
763 			/* align ba to block boundary */
764 			ba_tmp.size -= block_end - ba->offset;
765 			ba_tmp.offset = block_end;
766 
767 			err = block_alloc_contiguous(dsp, &ba_tmp, block_list);
768 			if (err < 0)
769 				return -ENOMEM;
770 
771 			return 0;
772 		}
773 	}
774 
775 	return -ENOMEM;
776 }
777 
778 /* Load fixed module data into DSP memory blocks */
sst_module_alloc_blocks(struct sst_module * module)779 int sst_module_alloc_blocks(struct sst_module *module)
780 {
781 	struct sst_dsp *dsp = module->dsp;
782 	struct sst_fw *sst_fw = module->sst_fw;
783 	struct sst_block_allocator ba;
784 	int ret;
785 
786 	memset(&ba, 0, sizeof(ba));
787 	ba.size = module->size;
788 	ba.type = module->type;
789 	ba.offset = module->offset;
790 
791 	dev_dbg(dsp->dev, "block request 0x%x bytes at offset 0x%x type %d\n",
792 		ba.size, ba.offset, ba.type);
793 
794 	mutex_lock(&dsp->mutex);
795 
796 	/* alloc blocks that includes this section */
797 	ret = block_alloc_fixed(dsp, &ba, &module->block_list);
798 	if (ret < 0) {
799 		dev_err(dsp->dev,
800 			"error: no free blocks for section at offset 0x%x size 0x%x\n",
801 			module->offset, module->size);
802 		mutex_unlock(&dsp->mutex);
803 		return -ENOMEM;
804 	}
805 
806 	/* prepare DSP blocks for module copy */
807 	ret = block_list_prepare(dsp, &module->block_list);
808 	if (ret < 0) {
809 		dev_err(dsp->dev, "error: fw module prepare failed\n");
810 		goto err;
811 	}
812 
813 	/* copy partial module data to blocks */
814 	if (dsp->fw_use_dma) {
815 		ret = sst_dsp_dma_copyto(dsp,
816 			dsp->addr.lpe_base + module->offset,
817 			sst_fw->dmable_fw_paddr + module->data_offset,
818 			module->size);
819 		if (ret < 0) {
820 			dev_err(dsp->dev, "error: module copy failed\n");
821 			goto err;
822 		}
823 	} else
824 		sst_memcpy32(dsp->addr.lpe + module->offset, module->data,
825 			module->size);
826 
827 	mutex_unlock(&dsp->mutex);
828 	return ret;
829 
830 err:
831 	block_list_remove(dsp, &module->block_list);
832 	mutex_unlock(&dsp->mutex);
833 	return ret;
834 }
835 EXPORT_SYMBOL_GPL(sst_module_alloc_blocks);
836 
837 /* Unload entire module from DSP memory */
sst_module_free_blocks(struct sst_module * module)838 int sst_module_free_blocks(struct sst_module *module)
839 {
840 	struct sst_dsp *dsp = module->dsp;
841 
842 	mutex_lock(&dsp->mutex);
843 	block_list_remove(dsp, &module->block_list);
844 	mutex_unlock(&dsp->mutex);
845 	return 0;
846 }
847 EXPORT_SYMBOL_GPL(sst_module_free_blocks);
848 
sst_module_runtime_alloc_blocks(struct sst_module_runtime * runtime,int offset)849 int sst_module_runtime_alloc_blocks(struct sst_module_runtime *runtime,
850 	int offset)
851 {
852 	struct sst_dsp *dsp = runtime->dsp;
853 	struct sst_module *module = runtime->module;
854 	struct sst_block_allocator ba;
855 	int ret;
856 
857 	if (module->persistent_size == 0)
858 		return 0;
859 
860 	memset(&ba, 0, sizeof(ba));
861 	ba.size = module->persistent_size;
862 	ba.type = SST_MEM_DRAM;
863 
864 	mutex_lock(&dsp->mutex);
865 
866 	/* do we need to allocate at a fixed address ? */
867 	if (offset != 0) {
868 
869 		ba.offset = offset;
870 
871 		dev_dbg(dsp->dev, "persistent fixed block request 0x%x bytes type %d offset 0x%x\n",
872 			ba.size, ba.type, ba.offset);
873 
874 		/* alloc blocks that includes this section */
875 		ret = block_alloc_fixed(dsp, &ba, &runtime->block_list);
876 
877 	} else {
878 		dev_dbg(dsp->dev, "persistent block request 0x%x bytes type %d\n",
879 			ba.size, ba.type);
880 
881 		/* alloc blocks that includes this section */
882 		ret = block_alloc(dsp, &ba, &runtime->block_list);
883 	}
884 	if (ret < 0) {
885 		dev_err(dsp->dev,
886 		"error: no free blocks for runtime module size 0x%x\n",
887 			module->persistent_size);
888 		mutex_unlock(&dsp->mutex);
889 		return -ENOMEM;
890 	}
891 	runtime->persistent_offset = ba.offset;
892 
893 	/* prepare DSP blocks for module copy */
894 	ret = block_list_prepare(dsp, &runtime->block_list);
895 	if (ret < 0) {
896 		dev_err(dsp->dev, "error: runtime block prepare failed\n");
897 		goto err;
898 	}
899 
900 	mutex_unlock(&dsp->mutex);
901 	return ret;
902 
903 err:
904 	block_list_remove(dsp, &module->block_list);
905 	mutex_unlock(&dsp->mutex);
906 	return ret;
907 }
908 EXPORT_SYMBOL_GPL(sst_module_runtime_alloc_blocks);
909 
sst_module_runtime_free_blocks(struct sst_module_runtime * runtime)910 int sst_module_runtime_free_blocks(struct sst_module_runtime *runtime)
911 {
912 	struct sst_dsp *dsp = runtime->dsp;
913 
914 	mutex_lock(&dsp->mutex);
915 	block_list_remove(dsp, &runtime->block_list);
916 	mutex_unlock(&dsp->mutex);
917 	return 0;
918 }
919 EXPORT_SYMBOL_GPL(sst_module_runtime_free_blocks);
920 
sst_module_runtime_save(struct sst_module_runtime * runtime,struct sst_module_runtime_context * context)921 int sst_module_runtime_save(struct sst_module_runtime *runtime,
922 	struct sst_module_runtime_context *context)
923 {
924 	struct sst_dsp *dsp = runtime->dsp;
925 	struct sst_module *module = runtime->module;
926 	int ret = 0;
927 
928 	dev_dbg(dsp->dev, "saving runtime %d memory at 0x%x size 0x%x\n",
929 		runtime->id, runtime->persistent_offset,
930 		module->persistent_size);
931 
932 	context->buffer = dma_alloc_coherent(dsp->dma_dev,
933 		module->persistent_size,
934 		&context->dma_buffer, GFP_DMA | GFP_KERNEL);
935 	if (!context->buffer) {
936 		dev_err(dsp->dev, "error: DMA context alloc failed\n");
937 		return -ENOMEM;
938 	}
939 
940 	mutex_lock(&dsp->mutex);
941 
942 	if (dsp->fw_use_dma) {
943 
944 		ret = sst_dsp_dma_get_channel(dsp, 0);
945 		if (ret < 0)
946 			goto err;
947 
948 		ret = sst_dsp_dma_copyfrom(dsp, context->dma_buffer,
949 			dsp->addr.lpe_base + runtime->persistent_offset,
950 			module->persistent_size);
951 		sst_dsp_dma_put_channel(dsp);
952 		if (ret < 0) {
953 			dev_err(dsp->dev, "error: context copy failed\n");
954 			goto err;
955 		}
956 	} else
957 		sst_memcpy32(context->buffer, dsp->addr.lpe +
958 			runtime->persistent_offset,
959 			module->persistent_size);
960 
961 err:
962 	mutex_unlock(&dsp->mutex);
963 	return ret;
964 }
965 EXPORT_SYMBOL_GPL(sst_module_runtime_save);
966 
sst_module_runtime_restore(struct sst_module_runtime * runtime,struct sst_module_runtime_context * context)967 int sst_module_runtime_restore(struct sst_module_runtime *runtime,
968 	struct sst_module_runtime_context *context)
969 {
970 	struct sst_dsp *dsp = runtime->dsp;
971 	struct sst_module *module = runtime->module;
972 	int ret = 0;
973 
974 	dev_dbg(dsp->dev, "restoring runtime %d memory at 0x%x size 0x%x\n",
975 		runtime->id, runtime->persistent_offset,
976 		module->persistent_size);
977 
978 	mutex_lock(&dsp->mutex);
979 
980 	if (!context->buffer) {
981 		dev_info(dsp->dev, "no context buffer need to restore!\n");
982 		goto err;
983 	}
984 
985 	if (dsp->fw_use_dma) {
986 
987 		ret = sst_dsp_dma_get_channel(dsp, 0);
988 		if (ret < 0)
989 			goto err;
990 
991 		ret = sst_dsp_dma_copyto(dsp,
992 			dsp->addr.lpe_base + runtime->persistent_offset,
993 			context->dma_buffer, module->persistent_size);
994 		sst_dsp_dma_put_channel(dsp);
995 		if (ret < 0) {
996 			dev_err(dsp->dev, "error: module copy failed\n");
997 			goto err;
998 		}
999 	} else
1000 		sst_memcpy32(dsp->addr.lpe + runtime->persistent_offset,
1001 			context->buffer, module->persistent_size);
1002 
1003 	dma_free_coherent(dsp->dma_dev, module->persistent_size,
1004 				context->buffer, context->dma_buffer);
1005 	context->buffer = NULL;
1006 
1007 err:
1008 	mutex_unlock(&dsp->mutex);
1009 	return ret;
1010 }
1011 EXPORT_SYMBOL_GPL(sst_module_runtime_restore);
1012 
1013 /* register a DSP memory block for use with FW based modules */
sst_mem_block_register(struct sst_dsp * dsp,u32 offset,u32 size,enum sst_mem_type type,struct sst_block_ops * ops,u32 index,void * private)1014 struct sst_mem_block *sst_mem_block_register(struct sst_dsp *dsp, u32 offset,
1015 	u32 size, enum sst_mem_type type, struct sst_block_ops *ops, u32 index,
1016 	void *private)
1017 {
1018 	struct sst_mem_block *block;
1019 
1020 	block = kzalloc(sizeof(*block), GFP_KERNEL);
1021 	if (block == NULL)
1022 		return NULL;
1023 
1024 	block->offset = offset;
1025 	block->size = size;
1026 	block->index = index;
1027 	block->type = type;
1028 	block->dsp = dsp;
1029 	block->private = private;
1030 	block->ops = ops;
1031 
1032 	mutex_lock(&dsp->mutex);
1033 	list_add(&block->list, &dsp->free_block_list);
1034 	mutex_unlock(&dsp->mutex);
1035 
1036 	return block;
1037 }
1038 EXPORT_SYMBOL_GPL(sst_mem_block_register);
1039 
1040 /* unregister all DSP memory blocks */
sst_mem_block_unregister_all(struct sst_dsp * dsp)1041 void sst_mem_block_unregister_all(struct sst_dsp *dsp)
1042 {
1043 	struct sst_mem_block *block, *tmp;
1044 
1045 	mutex_lock(&dsp->mutex);
1046 
1047 	/* unregister used blocks */
1048 	list_for_each_entry_safe(block, tmp, &dsp->used_block_list, list) {
1049 		list_del(&block->list);
1050 		kfree(block);
1051 	}
1052 
1053 	/* unregister free blocks */
1054 	list_for_each_entry_safe(block, tmp, &dsp->free_block_list, list) {
1055 		list_del(&block->list);
1056 		kfree(block);
1057 	}
1058 
1059 	mutex_unlock(&dsp->mutex);
1060 }
1061 EXPORT_SYMBOL_GPL(sst_mem_block_unregister_all);
1062 
1063 /* allocate scratch buffer blocks */
sst_block_alloc_scratch(struct sst_dsp * dsp)1064 int sst_block_alloc_scratch(struct sst_dsp *dsp)
1065 {
1066 	struct sst_module *module;
1067 	struct sst_block_allocator ba;
1068 	int ret;
1069 
1070 	mutex_lock(&dsp->mutex);
1071 
1072 	/* calculate required scratch size */
1073 	dsp->scratch_size = 0;
1074 	list_for_each_entry(module, &dsp->module_list, list) {
1075 		dev_dbg(dsp->dev, "module %d scratch req 0x%x bytes\n",
1076 			module->id, module->scratch_size);
1077 		if (dsp->scratch_size < module->scratch_size)
1078 			dsp->scratch_size = module->scratch_size;
1079 	}
1080 
1081 	dev_dbg(dsp->dev, "scratch buffer required is 0x%x bytes\n",
1082 		dsp->scratch_size);
1083 
1084 	if (dsp->scratch_size == 0) {
1085 		dev_info(dsp->dev, "no modules need scratch buffer\n");
1086 		mutex_unlock(&dsp->mutex);
1087 		return 0;
1088 	}
1089 
1090 	/* allocate blocks for module scratch buffers */
1091 	dev_dbg(dsp->dev, "allocating scratch blocks\n");
1092 
1093 	ba.size = dsp->scratch_size;
1094 	ba.type = SST_MEM_DRAM;
1095 
1096 	/* do we need to allocate at fixed offset */
1097 	if (dsp->scratch_offset != 0) {
1098 
1099 		dev_dbg(dsp->dev, "block request 0x%x bytes type %d at 0x%x\n",
1100 			ba.size, ba.type, ba.offset);
1101 
1102 		ba.offset = dsp->scratch_offset;
1103 
1104 		/* alloc blocks that includes this section */
1105 		ret = block_alloc_fixed(dsp, &ba, &dsp->scratch_block_list);
1106 
1107 	} else {
1108 		dev_dbg(dsp->dev, "block request 0x%x bytes type %d\n",
1109 			ba.size, ba.type);
1110 
1111 		ba.offset = 0;
1112 		ret = block_alloc(dsp, &ba, &dsp->scratch_block_list);
1113 	}
1114 	if (ret < 0) {
1115 		dev_err(dsp->dev, "error: can't alloc scratch blocks\n");
1116 		mutex_unlock(&dsp->mutex);
1117 		return ret;
1118 	}
1119 
1120 	ret = block_list_prepare(dsp, &dsp->scratch_block_list);
1121 	if (ret < 0) {
1122 		dev_err(dsp->dev, "error: scratch block prepare failed\n");
1123 		mutex_unlock(&dsp->mutex);
1124 		return ret;
1125 	}
1126 
1127 	/* assign the same offset of scratch to each module */
1128 	dsp->scratch_offset = ba.offset;
1129 	mutex_unlock(&dsp->mutex);
1130 	return dsp->scratch_size;
1131 }
1132 EXPORT_SYMBOL_GPL(sst_block_alloc_scratch);
1133 
1134 /* free all scratch blocks */
sst_block_free_scratch(struct sst_dsp * dsp)1135 void sst_block_free_scratch(struct sst_dsp *dsp)
1136 {
1137 	mutex_lock(&dsp->mutex);
1138 	block_list_remove(dsp, &dsp->scratch_block_list);
1139 	mutex_unlock(&dsp->mutex);
1140 }
1141 EXPORT_SYMBOL_GPL(sst_block_free_scratch);
1142 
1143 /* get a module from it's unique ID */
sst_module_get_from_id(struct sst_dsp * dsp,u32 id)1144 struct sst_module *sst_module_get_from_id(struct sst_dsp *dsp, u32 id)
1145 {
1146 	struct sst_module *module;
1147 
1148 	mutex_lock(&dsp->mutex);
1149 
1150 	list_for_each_entry(module, &dsp->module_list, list) {
1151 		if (module->id == id) {
1152 			mutex_unlock(&dsp->mutex);
1153 			return module;
1154 		}
1155 	}
1156 
1157 	mutex_unlock(&dsp->mutex);
1158 	return NULL;
1159 }
1160 EXPORT_SYMBOL_GPL(sst_module_get_from_id);
1161 
sst_module_runtime_get_from_id(struct sst_module * module,u32 id)1162 struct sst_module_runtime *sst_module_runtime_get_from_id(
1163 	struct sst_module *module, u32 id)
1164 {
1165 	struct sst_module_runtime *runtime;
1166 	struct sst_dsp *dsp = module->dsp;
1167 
1168 	mutex_lock(&dsp->mutex);
1169 
1170 	list_for_each_entry(runtime, &module->runtime_list, list) {
1171 		if (runtime->id == id) {
1172 			mutex_unlock(&dsp->mutex);
1173 			return runtime;
1174 		}
1175 	}
1176 
1177 	mutex_unlock(&dsp->mutex);
1178 	return NULL;
1179 }
1180 EXPORT_SYMBOL_GPL(sst_module_runtime_get_from_id);
1181 
1182 /* returns block address in DSP address space */
sst_dsp_get_offset(struct sst_dsp * dsp,u32 offset,enum sst_mem_type type)1183 u32 sst_dsp_get_offset(struct sst_dsp *dsp, u32 offset,
1184 	enum sst_mem_type type)
1185 {
1186 	switch (type) {
1187 	case SST_MEM_IRAM:
1188 		return offset - dsp->addr.iram_offset +
1189 			dsp->addr.dsp_iram_offset;
1190 	case SST_MEM_DRAM:
1191 		return offset - dsp->addr.dram_offset +
1192 			dsp->addr.dsp_dram_offset;
1193 	default:
1194 		return 0;
1195 	}
1196 }
1197 EXPORT_SYMBOL_GPL(sst_dsp_get_offset);
1198