sst-firmware.c 28 KB

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