bxt-sst.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * bxt-sst.c - DSP library functions for BXT platform
  3. *
  4. * Copyright (C) 2015-16 Intel Corp
  5. * Author:Rafal Redzimski <rafal.f.redzimski@intel.com>
  6. * Jeeja KP <jeeja.kp@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/delay.h>
  19. #include <linux/firmware.h>
  20. #include <linux/device.h>
  21. #include "../common/sst-dsp.h"
  22. #include "../common/sst-dsp-priv.h"
  23. #include "skl-sst-ipc.h"
  24. #include "skl-tplg-interface.h"
  25. #define BXT_BASEFW_TIMEOUT 3000
  26. #define BXT_INIT_TIMEOUT 500
  27. #define BXT_IPC_PURGE_FW 0x01004000
  28. #define BXT_ROM_INIT 0x5
  29. #define BXT_ADSP_SRAM0_BASE 0x80000
  30. /* Firmware status window */
  31. #define BXT_ADSP_FW_STATUS BXT_ADSP_SRAM0_BASE
  32. #define BXT_ADSP_ERROR_CODE (BXT_ADSP_FW_STATUS + 0x4)
  33. #define BXT_ADSP_SRAM1_BASE 0xA0000
  34. #define BXT_INSTANCE_ID 0
  35. #define BXT_BASE_FW_MODULE_ID 0
  36. #define BXT_ADSP_FW_BIN_HDR_OFFSET 0x2000
  37. static unsigned int bxt_get_errorcode(struct sst_dsp *ctx)
  38. {
  39. return sst_dsp_shim_read(ctx, BXT_ADSP_ERROR_CODE);
  40. }
  41. static int
  42. bxt_load_library(struct sst_dsp *ctx, struct skl_dfw_manifest *minfo)
  43. {
  44. struct snd_dma_buffer dmab;
  45. struct skl_sst *skl = ctx->thread_context;
  46. const struct firmware *fw = NULL;
  47. struct firmware stripped_fw;
  48. int ret = 0, i, dma_id, stream_tag;
  49. /* library indices start from 1 to N. 0 represents base FW */
  50. for (i = 1; i < minfo->lib_count; i++) {
  51. ret = reject_firmware(&fw, minfo->lib[i].name, ctx->dev);
  52. if (ret < 0) {
  53. dev_err(ctx->dev, "Request lib %s failed:%d\n",
  54. minfo->lib[i].name, ret);
  55. return ret;
  56. }
  57. if (skl->is_first_boot) {
  58. ret = snd_skl_parse_uuids(ctx, fw,
  59. BXT_ADSP_FW_BIN_HDR_OFFSET, i);
  60. if (ret < 0)
  61. goto load_library_failed;
  62. }
  63. stripped_fw.data = fw->data;
  64. stripped_fw.size = fw->size;
  65. skl_dsp_strip_extended_manifest(&stripped_fw);
  66. stream_tag = ctx->dsp_ops.prepare(ctx->dev, 0x40,
  67. stripped_fw.size, &dmab);
  68. if (stream_tag <= 0) {
  69. dev_err(ctx->dev, "Lib prepare DMA err: %x\n",
  70. stream_tag);
  71. ret = stream_tag;
  72. goto load_library_failed;
  73. }
  74. dma_id = stream_tag - 1;
  75. memcpy(dmab.area, stripped_fw.data, stripped_fw.size);
  76. ctx->dsp_ops.trigger(ctx->dev, true, stream_tag);
  77. ret = skl_sst_ipc_load_library(&skl->ipc, dma_id, i);
  78. if (ret < 0)
  79. dev_err(ctx->dev, "IPC Load Lib for %s fail: %d\n",
  80. minfo->lib[i].name, ret);
  81. ctx->dsp_ops.trigger(ctx->dev, false, stream_tag);
  82. ctx->dsp_ops.cleanup(ctx->dev, &dmab, stream_tag);
  83. release_firmware(fw);
  84. fw = NULL;
  85. }
  86. return ret;
  87. load_library_failed:
  88. release_firmware(fw);
  89. return ret;
  90. }
  91. /*
  92. * First boot sequence has some extra steps. Core 0 waits for power
  93. * status on core 1, so power up core 1 also momentarily, keep it in
  94. * reset/stall and then turn it off
  95. */
  96. static int sst_bxt_prepare_fw(struct sst_dsp *ctx,
  97. const void *fwdata, u32 fwsize)
  98. {
  99. int stream_tag, ret, i;
  100. u32 reg;
  101. stream_tag = ctx->dsp_ops.prepare(ctx->dev, 0x40, fwsize, &ctx->dmab);
  102. if (stream_tag <= 0) {
  103. dev_err(ctx->dev, "Failed to prepare DMA FW loading err: %x\n",
  104. stream_tag);
  105. return stream_tag;
  106. }
  107. ctx->dsp_ops.stream_tag = stream_tag;
  108. memcpy(ctx->dmab.area, fwdata, fwsize);
  109. /* Step 1: Power up core 0 and core1 */
  110. ret = skl_dsp_core_power_up(ctx, SKL_DSP_CORE0_MASK |
  111. SKL_DSP_CORE_MASK(1));
  112. if (ret < 0) {
  113. dev_err(ctx->dev, "dsp core0/1 power up failed\n");
  114. goto base_fw_load_failed;
  115. }
  116. /* Step 2: Purge FW request */
  117. sst_dsp_shim_write(ctx, SKL_ADSP_REG_HIPCI, SKL_ADSP_REG_HIPCI_BUSY |
  118. (BXT_IPC_PURGE_FW | ((stream_tag - 1) << 9)));
  119. /* Step 3: Unset core0 reset state & unstall/run core0 */
  120. ret = skl_dsp_start_core(ctx, SKL_DSP_CORE0_MASK);
  121. if (ret < 0) {
  122. dev_err(ctx->dev, "Start dsp core failed ret: %d\n", ret);
  123. ret = -EIO;
  124. goto base_fw_load_failed;
  125. }
  126. /* Step 4: Wait for DONE Bit */
  127. for (i = BXT_INIT_TIMEOUT; i > 0; --i) {
  128. reg = sst_dsp_shim_read(ctx, SKL_ADSP_REG_HIPCIE);
  129. if (reg & SKL_ADSP_REG_HIPCIE_DONE) {
  130. sst_dsp_shim_update_bits_forced(ctx,
  131. SKL_ADSP_REG_HIPCIE,
  132. SKL_ADSP_REG_HIPCIE_DONE,
  133. SKL_ADSP_REG_HIPCIE_DONE);
  134. break;
  135. }
  136. mdelay(1);
  137. }
  138. if (!i) {
  139. dev_info(ctx->dev, "Waiting for HIPCIE done, reg: 0x%x\n", reg);
  140. sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_HIPCIE,
  141. SKL_ADSP_REG_HIPCIE_DONE,
  142. SKL_ADSP_REG_HIPCIE_DONE);
  143. }
  144. /* Step 5: power down core1 */
  145. ret = skl_dsp_core_power_down(ctx, SKL_DSP_CORE_MASK(1));
  146. if (ret < 0) {
  147. dev_err(ctx->dev, "dsp core1 power down failed\n");
  148. goto base_fw_load_failed;
  149. }
  150. /* Step 6: Enable Interrupt */
  151. skl_ipc_int_enable(ctx);
  152. skl_ipc_op_int_enable(ctx);
  153. /* Step 7: Wait for ROM init */
  154. for (i = BXT_INIT_TIMEOUT; i > 0; --i) {
  155. if (SKL_FW_INIT ==
  156. (sst_dsp_shim_read(ctx, BXT_ADSP_FW_STATUS) &
  157. SKL_FW_STS_MASK)) {
  158. dev_info(ctx->dev, "ROM loaded, continue FW loading\n");
  159. break;
  160. }
  161. mdelay(1);
  162. }
  163. if (!i) {
  164. dev_err(ctx->dev, "Timeout for ROM init, HIPCIE: 0x%x\n", reg);
  165. ret = -EIO;
  166. goto base_fw_load_failed;
  167. }
  168. return ret;
  169. base_fw_load_failed:
  170. ctx->dsp_ops.cleanup(ctx->dev, &ctx->dmab, stream_tag);
  171. skl_dsp_core_power_down(ctx, SKL_DSP_CORE_MASK(1));
  172. skl_dsp_disable_core(ctx, SKL_DSP_CORE0_MASK);
  173. return ret;
  174. }
  175. static int sst_transfer_fw_host_dma(struct sst_dsp *ctx)
  176. {
  177. int ret;
  178. ctx->dsp_ops.trigger(ctx->dev, true, ctx->dsp_ops.stream_tag);
  179. ret = sst_dsp_register_poll(ctx, BXT_ADSP_FW_STATUS, SKL_FW_STS_MASK,
  180. BXT_ROM_INIT, BXT_BASEFW_TIMEOUT, "Firmware boot");
  181. ctx->dsp_ops.trigger(ctx->dev, false, ctx->dsp_ops.stream_tag);
  182. ctx->dsp_ops.cleanup(ctx->dev, &ctx->dmab, ctx->dsp_ops.stream_tag);
  183. return ret;
  184. }
  185. static int bxt_load_base_firmware(struct sst_dsp *ctx)
  186. {
  187. struct firmware stripped_fw;
  188. struct skl_sst *skl = ctx->thread_context;
  189. int ret;
  190. ret = reject_firmware(&ctx->fw, ctx->fw_name, ctx->dev);
  191. if (ret < 0) {
  192. dev_err(ctx->dev, "Request firmware failed %d\n", ret);
  193. goto sst_load_base_firmware_failed;
  194. }
  195. /* check for extended manifest */
  196. if (ctx->fw == NULL)
  197. goto sst_load_base_firmware_failed;
  198. /* prase uuids on first boot */
  199. if (skl->is_first_boot) {
  200. ret = snd_skl_parse_uuids(ctx, ctx->fw, BXT_ADSP_FW_BIN_HDR_OFFSET, 0);
  201. if (ret < 0)
  202. goto sst_load_base_firmware_failed;
  203. }
  204. stripped_fw.data = ctx->fw->data;
  205. stripped_fw.size = ctx->fw->size;
  206. skl_dsp_strip_extended_manifest(&stripped_fw);
  207. ret = sst_bxt_prepare_fw(ctx, stripped_fw.data, stripped_fw.size);
  208. /* Retry Enabling core and ROM load. Retry seemed to help */
  209. if (ret < 0) {
  210. ret = sst_bxt_prepare_fw(ctx, stripped_fw.data, stripped_fw.size);
  211. if (ret < 0) {
  212. dev_err(ctx->dev, "Error code=0x%x: FW status=0x%x\n",
  213. sst_dsp_shim_read(ctx, BXT_ADSP_ERROR_CODE),
  214. sst_dsp_shim_read(ctx, BXT_ADSP_FW_STATUS));
  215. dev_err(ctx->dev, "Core En/ROM load fail:%d\n", ret);
  216. goto sst_load_base_firmware_failed;
  217. }
  218. }
  219. ret = sst_transfer_fw_host_dma(ctx);
  220. if (ret < 0) {
  221. dev_err(ctx->dev, "Transfer firmware failed %d\n", ret);
  222. dev_info(ctx->dev, "Error code=0x%x: FW status=0x%x\n",
  223. sst_dsp_shim_read(ctx, BXT_ADSP_ERROR_CODE),
  224. sst_dsp_shim_read(ctx, BXT_ADSP_FW_STATUS));
  225. skl_dsp_disable_core(ctx, SKL_DSP_CORE0_MASK);
  226. } else {
  227. dev_dbg(ctx->dev, "Firmware download successful\n");
  228. ret = wait_event_timeout(skl->boot_wait, skl->boot_complete,
  229. msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
  230. if (ret == 0) {
  231. dev_err(ctx->dev, "DSP boot fail, FW Ready timeout\n");
  232. skl_dsp_disable_core(ctx, SKL_DSP_CORE0_MASK);
  233. ret = -EIO;
  234. } else {
  235. ret = 0;
  236. skl->fw_loaded = true;
  237. }
  238. }
  239. sst_load_base_firmware_failed:
  240. release_firmware(ctx->fw);
  241. return ret;
  242. }
  243. static int bxt_set_dsp_D0(struct sst_dsp *ctx, unsigned int core_id)
  244. {
  245. struct skl_sst *skl = ctx->thread_context;
  246. int ret;
  247. struct skl_ipc_dxstate_info dx;
  248. unsigned int core_mask = SKL_DSP_CORE_MASK(core_id);
  249. struct skl_dfw_manifest *minfo = &skl->manifest;
  250. if (skl->fw_loaded == false) {
  251. skl->boot_complete = false;
  252. ret = bxt_load_base_firmware(ctx);
  253. if (ret < 0) {
  254. dev_err(ctx->dev, "reload fw failed: %d\n", ret);
  255. return ret;
  256. }
  257. if (minfo->lib_count > 1) {
  258. ret = bxt_load_library(ctx, minfo);
  259. if (ret < 0) {
  260. dev_err(ctx->dev, "reload libs failed: %d\n", ret);
  261. return ret;
  262. }
  263. }
  264. return ret;
  265. }
  266. /* If core 0 is being turned on, turn on core 1 as well */
  267. if (core_id == SKL_DSP_CORE0_ID)
  268. ret = skl_dsp_core_power_up(ctx, core_mask |
  269. SKL_DSP_CORE_MASK(1));
  270. else
  271. ret = skl_dsp_core_power_up(ctx, core_mask);
  272. if (ret < 0)
  273. goto err;
  274. if (core_id == SKL_DSP_CORE0_ID) {
  275. /*
  276. * Enable interrupt after SPA is set and before
  277. * DSP is unstalled
  278. */
  279. skl_ipc_int_enable(ctx);
  280. skl_ipc_op_int_enable(ctx);
  281. skl->boot_complete = false;
  282. }
  283. ret = skl_dsp_start_core(ctx, core_mask);
  284. if (ret < 0)
  285. goto err;
  286. if (core_id == SKL_DSP_CORE0_ID) {
  287. ret = wait_event_timeout(skl->boot_wait,
  288. skl->boot_complete,
  289. msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
  290. /* If core 1 was turned on for booting core 0, turn it off */
  291. skl_dsp_core_power_down(ctx, SKL_DSP_CORE_MASK(1));
  292. if (ret == 0) {
  293. dev_err(ctx->dev, "%s: DSP boot timeout\n", __func__);
  294. dev_err(ctx->dev, "Error code=0x%x: FW status=0x%x\n",
  295. sst_dsp_shim_read(ctx, BXT_ADSP_ERROR_CODE),
  296. sst_dsp_shim_read(ctx, BXT_ADSP_FW_STATUS));
  297. dev_err(ctx->dev, "Failed to set core0 to D0 state\n");
  298. ret = -EIO;
  299. goto err;
  300. }
  301. }
  302. /* Tell FW if additional core in now On */
  303. if (core_id != SKL_DSP_CORE0_ID) {
  304. dx.core_mask = core_mask;
  305. dx.dx_mask = core_mask;
  306. ret = skl_ipc_set_dx(&skl->ipc, BXT_INSTANCE_ID,
  307. BXT_BASE_FW_MODULE_ID, &dx);
  308. if (ret < 0) {
  309. dev_err(ctx->dev, "IPC set_dx for core %d fail: %d\n",
  310. core_id, ret);
  311. goto err;
  312. }
  313. }
  314. skl->cores.state[core_id] = SKL_DSP_RUNNING;
  315. return 0;
  316. err:
  317. if (core_id == SKL_DSP_CORE0_ID)
  318. core_mask |= SKL_DSP_CORE_MASK(1);
  319. skl_dsp_disable_core(ctx, core_mask);
  320. return ret;
  321. }
  322. static int bxt_set_dsp_D3(struct sst_dsp *ctx, unsigned int core_id)
  323. {
  324. int ret;
  325. struct skl_ipc_dxstate_info dx;
  326. struct skl_sst *skl = ctx->thread_context;
  327. unsigned int core_mask = SKL_DSP_CORE_MASK(core_id);
  328. dx.core_mask = core_mask;
  329. dx.dx_mask = SKL_IPC_D3_MASK;
  330. dev_dbg(ctx->dev, "core mask=%x dx_mask=%x\n",
  331. dx.core_mask, dx.dx_mask);
  332. ret = skl_ipc_set_dx(&skl->ipc, BXT_INSTANCE_ID,
  333. BXT_BASE_FW_MODULE_ID, &dx);
  334. if (ret < 0)
  335. dev_err(ctx->dev,
  336. "Failed to set DSP to D3:core id = %d;Continue reset\n",
  337. core_id);
  338. ret = skl_dsp_disable_core(ctx, core_mask);
  339. if (ret < 0) {
  340. dev_err(ctx->dev, "Failed to disable core %d\n", ret);
  341. return ret;
  342. }
  343. skl->cores.state[core_id] = SKL_DSP_RESET;
  344. return 0;
  345. }
  346. static struct skl_dsp_fw_ops bxt_fw_ops = {
  347. .set_state_D0 = bxt_set_dsp_D0,
  348. .set_state_D3 = bxt_set_dsp_D3,
  349. .load_fw = bxt_load_base_firmware,
  350. .get_fw_errcode = bxt_get_errorcode,
  351. .load_library = bxt_load_library,
  352. };
  353. static struct sst_ops skl_ops = {
  354. .irq_handler = skl_dsp_sst_interrupt,
  355. .write = sst_shim32_write,
  356. .read = sst_shim32_read,
  357. .ram_read = sst_memcpy_fromio_32,
  358. .ram_write = sst_memcpy_toio_32,
  359. .free = skl_dsp_free,
  360. };
  361. static struct sst_dsp_device skl_dev = {
  362. .thread = skl_dsp_irq_thread_handler,
  363. .ops = &skl_ops,
  364. };
  365. int bxt_sst_dsp_init(struct device *dev, void __iomem *mmio_base, int irq,
  366. const char *fw_name, struct skl_dsp_loader_ops dsp_ops,
  367. struct skl_sst **dsp)
  368. {
  369. struct skl_sst *skl;
  370. struct sst_dsp *sst;
  371. int ret;
  372. skl = devm_kzalloc(dev, sizeof(*skl), GFP_KERNEL);
  373. if (skl == NULL)
  374. return -ENOMEM;
  375. skl->dev = dev;
  376. skl_dev.thread_context = skl;
  377. INIT_LIST_HEAD(&skl->uuid_list);
  378. skl->dsp = skl_dsp_ctx_init(dev, &skl_dev, irq);
  379. if (!skl->dsp) {
  380. dev_err(skl->dev, "skl_dsp_ctx_init failed\n");
  381. return -ENODEV;
  382. }
  383. sst = skl->dsp;
  384. sst->fw_name = fw_name;
  385. sst->dsp_ops = dsp_ops;
  386. sst->fw_ops = bxt_fw_ops;
  387. sst->addr.lpe = mmio_base;
  388. sst->addr.shim = mmio_base;
  389. sst_dsp_mailbox_init(sst, (BXT_ADSP_SRAM0_BASE + SKL_ADSP_W0_STAT_SZ),
  390. SKL_ADSP_W0_UP_SZ, BXT_ADSP_SRAM1_BASE, SKL_ADSP_W1_SZ);
  391. INIT_LIST_HEAD(&sst->module_list);
  392. ret = skl_ipc_init(dev, skl);
  393. if (ret)
  394. return ret;
  395. skl->cores.count = 2;
  396. skl->boot_complete = false;
  397. init_waitqueue_head(&skl->boot_wait);
  398. skl->is_first_boot = true;
  399. if (dsp)
  400. *dsp = skl;
  401. return 0;
  402. }
  403. EXPORT_SYMBOL_GPL(bxt_sst_dsp_init);
  404. int bxt_sst_init_fw(struct device *dev, struct skl_sst *ctx)
  405. {
  406. int ret;
  407. struct sst_dsp *sst = ctx->dsp;
  408. ret = sst->fw_ops.load_fw(sst);
  409. if (ret < 0) {
  410. dev_err(dev, "Load base fw failed: %x\n", ret);
  411. return ret;
  412. }
  413. skl_dsp_init_core_state(sst);
  414. if (ctx->manifest.lib_count > 1) {
  415. ret = sst->fw_ops.load_library(sst, &ctx->manifest);
  416. if (ret < 0) {
  417. dev_err(dev, "Load Library failed : %x\n", ret);
  418. return ret;
  419. }
  420. }
  421. ctx->is_first_boot = false;
  422. return 0;
  423. }
  424. EXPORT_SYMBOL_GPL(bxt_sst_init_fw);
  425. void bxt_sst_dsp_cleanup(struct device *dev, struct skl_sst *ctx)
  426. {
  427. skl_freeup_uuid_list(ctx);
  428. skl_ipc_free(&ctx->ipc);
  429. ctx->dsp->cl_dev.ops.cl_cleanup_controller(ctx->dsp);
  430. if (ctx->dsp->addr.lpe)
  431. iounmap(ctx->dsp->addr.lpe);
  432. ctx->dsp->ops->free(ctx->dsp);
  433. }
  434. EXPORT_SYMBOL_GPL(bxt_sst_dsp_cleanup);
  435. MODULE_LICENSE("GPL v2");
  436. MODULE_DESCRIPTION("Intel Broxton IPC driver");