sst_loader.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * sst_dsp.c - Intel SST Driver for audio engine
  3. *
  4. * Copyright (C) 2008-14 Intel Corp
  5. * Authors: Vinod Koul <vinod.koul@intel.com>
  6. * Harsha Priya <priya.harsha@intel.com>
  7. * Dharageswari R <dharageswari.r@intel.com>
  8. * KP Jeeja <jeeja.kp@intel.com>
  9. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; version 2 of the License.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  21. *
  22. * This file contains all dsp controlling functions like firmware download,
  23. * setting/resetting dsp cores, etc
  24. */
  25. #include <linux/pci.h>
  26. #include <linux/delay.h>
  27. #include <linux/fs.h>
  28. #include <linux/sched.h>
  29. #include <linux/firmware.h>
  30. #include <linux/dmaengine.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/pm_qos.h>
  33. #include <sound/core.h>
  34. #include <sound/pcm.h>
  35. #include <sound/soc.h>
  36. #include <sound/compress_driver.h>
  37. #include <asm/platform_sst_audio.h>
  38. #include "../sst-mfld-platform.h"
  39. #include "sst.h"
  40. #include "../../common/sst-dsp.h"
  41. void memcpy32_toio(void __iomem *dst, const void *src, int count)
  42. {
  43. /* __iowrite32_copy uses 32-bit count values so divide by 4 for
  44. * right count in words
  45. */
  46. __iowrite32_copy(dst, src, count / 4);
  47. }
  48. void memcpy32_fromio(void *dst, const void __iomem *src, int count)
  49. {
  50. /* __ioread32_copy uses 32-bit count values so divide by 4 for
  51. * right count in words
  52. */
  53. __ioread32_copy(dst, src, count / 4);
  54. }
  55. /**
  56. * intel_sst_reset_dsp_mrfld - Resetting SST DSP
  57. *
  58. * This resets DSP in case of MRFLD platfroms
  59. */
  60. int intel_sst_reset_dsp_mrfld(struct intel_sst_drv *sst_drv_ctx)
  61. {
  62. union config_status_reg_mrfld csr;
  63. dev_dbg(sst_drv_ctx->dev, "sst: Resetting the DSP in mrfld\n");
  64. csr.full = sst_shim_read64(sst_drv_ctx->shim, SST_CSR);
  65. dev_dbg(sst_drv_ctx->dev, "value:0x%llx\n", csr.full);
  66. csr.full |= 0x7;
  67. sst_shim_write64(sst_drv_ctx->shim, SST_CSR, csr.full);
  68. csr.full = sst_shim_read64(sst_drv_ctx->shim, SST_CSR);
  69. dev_dbg(sst_drv_ctx->dev, "value:0x%llx\n", csr.full);
  70. csr.full &= ~(0x1);
  71. sst_shim_write64(sst_drv_ctx->shim, SST_CSR, csr.full);
  72. csr.full = sst_shim_read64(sst_drv_ctx->shim, SST_CSR);
  73. dev_dbg(sst_drv_ctx->dev, "value:0x%llx\n", csr.full);
  74. return 0;
  75. }
  76. /**
  77. * sst_start_merrifield - Start the SST DSP processor
  78. *
  79. * This starts the DSP in MERRIFIELD platfroms
  80. */
  81. int sst_start_mrfld(struct intel_sst_drv *sst_drv_ctx)
  82. {
  83. union config_status_reg_mrfld csr;
  84. dev_dbg(sst_drv_ctx->dev, "sst: Starting the DSP in mrfld LALALALA\n");
  85. csr.full = sst_shim_read64(sst_drv_ctx->shim, SST_CSR);
  86. dev_dbg(sst_drv_ctx->dev, "value:0x%llx\n", csr.full);
  87. csr.full |= 0x7;
  88. sst_shim_write64(sst_drv_ctx->shim, SST_CSR, csr.full);
  89. csr.full = sst_shim_read64(sst_drv_ctx->shim, SST_CSR);
  90. dev_dbg(sst_drv_ctx->dev, "value:0x%llx\n", csr.full);
  91. csr.part.xt_snoop = 1;
  92. csr.full &= ~(0x5);
  93. sst_shim_write64(sst_drv_ctx->shim, SST_CSR, csr.full);
  94. csr.full = sst_shim_read64(sst_drv_ctx->shim, SST_CSR);
  95. dev_dbg(sst_drv_ctx->dev, "sst: Starting the DSP_merrifield:%llx\n",
  96. csr.full);
  97. return 0;
  98. }
  99. static int sst_validate_fw_image(struct intel_sst_drv *ctx, unsigned long size,
  100. struct fw_module_header **module, u32 *num_modules)
  101. {
  102. struct sst_fw_header *header;
  103. const void *sst_fw_in_mem = ctx->fw_in_mem;
  104. dev_dbg(ctx->dev, "Enter\n");
  105. /* Read the header information from the data pointer */
  106. header = (struct sst_fw_header *)sst_fw_in_mem;
  107. dev_dbg(ctx->dev,
  108. "header sign=%s size=%x modules=%x fmt=%x size=%zx\n",
  109. header->signature, header->file_size, header->modules,
  110. header->file_format, sizeof(*header));
  111. /* verify FW */
  112. if ((strncmp(header->signature, SST_FW_SIGN, 4) != 0) ||
  113. (size != header->file_size + sizeof(*header))) {
  114. /* Invalid FW signature */
  115. dev_err(ctx->dev, "InvalidFW sign/filesize mismatch\n");
  116. return -EINVAL;
  117. }
  118. *num_modules = header->modules;
  119. *module = (void *)sst_fw_in_mem + sizeof(*header);
  120. return 0;
  121. }
  122. /*
  123. * sst_fill_memcpy_list - Fill the memcpy list
  124. *
  125. * @memcpy_list: List to be filled
  126. * @destn: Destination addr to be filled in the list
  127. * @src: Source addr to be filled in the list
  128. * @size: Size to be filled in the list
  129. *
  130. * Adds the node to the list after required fields
  131. * are populated in the node
  132. */
  133. static int sst_fill_memcpy_list(struct list_head *memcpy_list,
  134. void *destn, const void *src, u32 size, bool is_io)
  135. {
  136. struct sst_memcpy_list *listnode;
  137. listnode = kzalloc(sizeof(*listnode), GFP_KERNEL);
  138. if (listnode == NULL)
  139. return -ENOMEM;
  140. listnode->dstn = destn;
  141. listnode->src = src;
  142. listnode->size = size;
  143. listnode->is_io = is_io;
  144. list_add_tail(&listnode->memcpylist, memcpy_list);
  145. return 0;
  146. }
  147. /**
  148. * sst_parse_module_memcpy - Parse audio FW modules and populate the memcpy list
  149. *
  150. * @sst_drv_ctx : driver context
  151. * @module : FW module header
  152. * @memcpy_list : Pointer to the list to be populated
  153. * Create the memcpy list as the number of block to be copied
  154. * returns error or 0 if module sizes are proper
  155. */
  156. static int sst_parse_module_memcpy(struct intel_sst_drv *sst_drv_ctx,
  157. struct fw_module_header *module, struct list_head *memcpy_list)
  158. {
  159. struct fw_block_info *block;
  160. u32 count;
  161. int ret_val = 0;
  162. void __iomem *ram_iomem;
  163. dev_dbg(sst_drv_ctx->dev, "module sign %s size %x blocks %x type %x\n",
  164. module->signature, module->mod_size,
  165. module->blocks, module->type);
  166. dev_dbg(sst_drv_ctx->dev, "module entrypoint 0x%x\n", module->entry_point);
  167. block = (void *)module + sizeof(*module);
  168. for (count = 0; count < module->blocks; count++) {
  169. if (block->size <= 0) {
  170. dev_err(sst_drv_ctx->dev, "block size invalid\n");
  171. return -EINVAL;
  172. }
  173. switch (block->type) {
  174. case SST_IRAM:
  175. ram_iomem = sst_drv_ctx->iram;
  176. break;
  177. case SST_DRAM:
  178. ram_iomem = sst_drv_ctx->dram;
  179. break;
  180. case SST_DDR:
  181. ram_iomem = sst_drv_ctx->ddr;
  182. break;
  183. case SST_CUSTOM_INFO:
  184. block = (void *)block + sizeof(*block) + block->size;
  185. continue;
  186. default:
  187. dev_err(sst_drv_ctx->dev, "wrong ram type0x%x in block0x%x\n",
  188. block->type, count);
  189. return -EINVAL;
  190. }
  191. ret_val = sst_fill_memcpy_list(memcpy_list,
  192. ram_iomem + block->ram_offset,
  193. (void *)block + sizeof(*block), block->size, 1);
  194. if (ret_val)
  195. return ret_val;
  196. block = (void *)block + sizeof(*block) + block->size;
  197. }
  198. return 0;
  199. }
  200. /**
  201. * sst_parse_fw_memcpy - parse the firmware image & populate the list for memcpy
  202. *
  203. * @ctx : pointer to drv context
  204. * @size : size of the firmware
  205. * @fw_list : pointer to list_head to be populated
  206. * This function parses the FW image and saves the parsed image in the list
  207. * for memcpy
  208. */
  209. static int sst_parse_fw_memcpy(struct intel_sst_drv *ctx, unsigned long size,
  210. struct list_head *fw_list)
  211. {
  212. struct fw_module_header *module;
  213. u32 count, num_modules;
  214. int ret_val;
  215. ret_val = sst_validate_fw_image(ctx, size, &module, &num_modules);
  216. if (ret_val)
  217. return ret_val;
  218. for (count = 0; count < num_modules; count++) {
  219. ret_val = sst_parse_module_memcpy(ctx, module, fw_list);
  220. if (ret_val)
  221. return ret_val;
  222. module = (void *)module + sizeof(*module) + module->mod_size;
  223. }
  224. return 0;
  225. }
  226. /**
  227. * sst_do_memcpy - function initiates the memcpy
  228. *
  229. * @memcpy_list: Pter to memcpy list on which the memcpy needs to be initiated
  230. *
  231. * Triggers the memcpy
  232. */
  233. static void sst_do_memcpy(struct list_head *memcpy_list)
  234. {
  235. struct sst_memcpy_list *listnode;
  236. list_for_each_entry(listnode, memcpy_list, memcpylist) {
  237. if (listnode->is_io == true)
  238. memcpy32_toio((void __iomem *)listnode->dstn,
  239. listnode->src, listnode->size);
  240. else
  241. memcpy(listnode->dstn, listnode->src, listnode->size);
  242. }
  243. }
  244. void sst_memcpy_free_resources(struct intel_sst_drv *sst_drv_ctx)
  245. {
  246. struct sst_memcpy_list *listnode, *tmplistnode;
  247. /* Free the list */
  248. if (!list_empty(&sst_drv_ctx->memcpy_list)) {
  249. list_for_each_entry_safe(listnode, tmplistnode,
  250. &sst_drv_ctx->memcpy_list, memcpylist) {
  251. list_del(&listnode->memcpylist);
  252. kfree(listnode);
  253. }
  254. }
  255. }
  256. static int sst_cache_and_parse_fw(struct intel_sst_drv *sst,
  257. const struct firmware *fw)
  258. {
  259. int retval = 0;
  260. sst->fw_in_mem = kzalloc(fw->size, GFP_KERNEL);
  261. if (!sst->fw_in_mem) {
  262. retval = -ENOMEM;
  263. goto end_release;
  264. }
  265. dev_dbg(sst->dev, "copied fw to %p", sst->fw_in_mem);
  266. dev_dbg(sst->dev, "phys: %lx", (unsigned long)virt_to_phys(sst->fw_in_mem));
  267. memcpy(sst->fw_in_mem, fw->data, fw->size);
  268. retval = sst_parse_fw_memcpy(sst, fw->size, &sst->memcpy_list);
  269. if (retval) {
  270. dev_err(sst->dev, "Failed to parse fw\n");
  271. kfree(sst->fw_in_mem);
  272. sst->fw_in_mem = NULL;
  273. }
  274. end_release:
  275. release_firmware(fw);
  276. return retval;
  277. }
  278. void sst_firmware_load_cb(const struct firmware *fw, void *context)
  279. {
  280. struct intel_sst_drv *ctx = context;
  281. dev_dbg(ctx->dev, "Enter\n");
  282. if (fw == NULL) {
  283. dev_err(ctx->dev, "request fw failed\n");
  284. return;
  285. }
  286. mutex_lock(&ctx->sst_lock);
  287. if (ctx->sst_state != SST_RESET ||
  288. ctx->fw_in_mem != NULL) {
  289. release_firmware(fw);
  290. mutex_unlock(&ctx->sst_lock);
  291. return;
  292. }
  293. dev_dbg(ctx->dev, "Request Fw completed\n");
  294. sst_cache_and_parse_fw(ctx, fw);
  295. mutex_unlock(&ctx->sst_lock);
  296. }
  297. /*
  298. * sst_request_fw - requests audio fw from kernel and saves a copy
  299. *
  300. * This function requests the SST FW from the kernel, parses it and
  301. * saves a copy in the driver context
  302. */
  303. static int sst_request_fw(struct intel_sst_drv *sst)
  304. {
  305. int retval = 0;
  306. const struct firmware *fw;
  307. retval = request_firmware(&fw, sst->firmware_name, sst->dev);
  308. if (retval) {
  309. dev_err(sst->dev, "request fw failed %d\n", retval);
  310. return retval;
  311. }
  312. if (fw == NULL) {
  313. dev_err(sst->dev, "fw is returning as null\n");
  314. return -EINVAL;
  315. }
  316. mutex_lock(&sst->sst_lock);
  317. retval = sst_cache_and_parse_fw(sst, fw);
  318. mutex_unlock(&sst->sst_lock);
  319. return retval;
  320. }
  321. /*
  322. * Writing the DDR physical base to DCCM offset
  323. * so that FW can use it to setup TLB
  324. */
  325. static void sst_dccm_config_write(void __iomem *dram_base,
  326. unsigned int ddr_base)
  327. {
  328. void __iomem *addr;
  329. u32 bss_reset = 0;
  330. addr = (void __iomem *)(dram_base + MRFLD_FW_DDR_BASE_OFFSET);
  331. memcpy32_toio(addr, (void *)&ddr_base, sizeof(u32));
  332. bss_reset |= (1 << MRFLD_FW_BSS_RESET_BIT);
  333. addr = (void __iomem *)(dram_base + MRFLD_FW_FEATURE_BASE_OFFSET);
  334. memcpy32_toio(addr, &bss_reset, sizeof(u32));
  335. }
  336. void sst_post_download_mrfld(struct intel_sst_drv *ctx)
  337. {
  338. sst_dccm_config_write(ctx->dram, ctx->ddr_base);
  339. dev_dbg(ctx->dev, "config written to DCCM\n");
  340. }
  341. /**
  342. * sst_load_fw - function to load FW into DSP
  343. * Transfers the FW to DSP using dma/memcpy
  344. */
  345. int sst_load_fw(struct intel_sst_drv *sst_drv_ctx)
  346. {
  347. int ret_val = 0;
  348. struct sst_block *block;
  349. dev_dbg(sst_drv_ctx->dev, "sst_load_fw\n");
  350. if (sst_drv_ctx->sst_state != SST_RESET ||
  351. sst_drv_ctx->sst_state == SST_SHUTDOWN)
  352. return -EAGAIN;
  353. if (!sst_drv_ctx->fw_in_mem) {
  354. dev_dbg(sst_drv_ctx->dev, "sst: FW not in memory retry to download\n");
  355. ret_val = sst_request_fw(sst_drv_ctx);
  356. if (ret_val)
  357. return ret_val;
  358. }
  359. block = sst_create_block(sst_drv_ctx, 0, FW_DWNL_ID);
  360. if (block == NULL)
  361. return -ENOMEM;
  362. /* Prevent C-states beyond C6 */
  363. pm_qos_update_request(sst_drv_ctx->qos, 0);
  364. sst_drv_ctx->sst_state = SST_FW_LOADING;
  365. ret_val = sst_drv_ctx->ops->reset(sst_drv_ctx);
  366. if (ret_val)
  367. goto restore;
  368. sst_do_memcpy(&sst_drv_ctx->memcpy_list);
  369. /* Write the DRAM/DCCM config before enabling FW */
  370. if (sst_drv_ctx->ops->post_download)
  371. sst_drv_ctx->ops->post_download(sst_drv_ctx);
  372. /* bring sst out of reset */
  373. ret_val = sst_drv_ctx->ops->start(sst_drv_ctx);
  374. if (ret_val)
  375. goto restore;
  376. ret_val = sst_wait_timeout(sst_drv_ctx, block);
  377. if (ret_val) {
  378. dev_err(sst_drv_ctx->dev, "fw download failed %d\n" , ret_val);
  379. /* FW download failed due to timeout */
  380. ret_val = -EBUSY;
  381. }
  382. restore:
  383. /* Re-enable Deeper C-states beyond C6 */
  384. pm_qos_update_request(sst_drv_ctx->qos, PM_QOS_DEFAULT_VALUE);
  385. sst_free_block(sst_drv_ctx, block);
  386. dev_dbg(sst_drv_ctx->dev, "fw load successful!!!\n");
  387. if (sst_drv_ctx->ops->restore_dsp_context)
  388. sst_drv_ctx->ops->restore_dsp_context();
  389. sst_drv_ctx->sst_state = SST_FW_RUNNING;
  390. return ret_val;
  391. }