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