cnl-sst.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * cnl-sst.c - DSP library functions for CNL platform
  3. *
  4. * Copyright (C) 2016-17, Intel Corporation.
  5. *
  6. * Author: Guneshwor Singh <guneshwor.o.singh@intel.com>
  7. *
  8. * Modified from:
  9. * HDA DSP library functions for SKL platform
  10. * Copyright (C) 2014-15, Intel Corporation.
  11. *
  12. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as version 2, as
  16. * published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful, but
  19. * WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * General Public License for more details.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #include <linux/module.h>
  26. #include <linux/delay.h>
  27. #include <linux/firmware.h>
  28. #include <linux/device.h>
  29. #include "../common/sst-dsp.h"
  30. #include "../common/sst-dsp-priv.h"
  31. #include "../common/sst-ipc.h"
  32. #include "cnl-sst-dsp.h"
  33. #include "skl-sst-dsp.h"
  34. #include "skl-sst-ipc.h"
  35. #define CNL_FW_ROM_INIT 0x1
  36. #define CNL_FW_INIT 0x5
  37. #define CNL_IPC_PURGE 0x01004000
  38. #define CNL_INIT_TIMEOUT 300
  39. #define CNL_BASEFW_TIMEOUT 3000
  40. #define CNL_ADSP_SRAM0_BASE 0x80000
  41. /* Firmware status window */
  42. #define CNL_ADSP_FW_STATUS CNL_ADSP_SRAM0_BASE
  43. #define CNL_ADSP_ERROR_CODE (CNL_ADSP_FW_STATUS + 0x4)
  44. #define CNL_INSTANCE_ID 0
  45. #define CNL_BASE_FW_MODULE_ID 0
  46. #define CNL_ADSP_FW_HDR_OFFSET 0x2000
  47. #define CNL_ROM_CTRL_DMA_ID 0x9
  48. static int cnl_prepare_fw(struct sst_dsp *ctx, const void *fwdata, u32 fwsize)
  49. {
  50. int ret, stream_tag;
  51. stream_tag = ctx->dsp_ops.prepare(ctx->dev, 0x40, fwsize, &ctx->dmab);
  52. if (stream_tag <= 0) {
  53. dev_err(ctx->dev, "dma prepare failed: 0%#x\n", stream_tag);
  54. return stream_tag;
  55. }
  56. ctx->dsp_ops.stream_tag = stream_tag;
  57. memcpy(ctx->dmab.area, fwdata, fwsize);
  58. /* purge FW request */
  59. sst_dsp_shim_write(ctx, CNL_ADSP_REG_HIPCIDR,
  60. CNL_ADSP_REG_HIPCIDR_BUSY | (CNL_IPC_PURGE |
  61. ((stream_tag - 1) << CNL_ROM_CTRL_DMA_ID)));
  62. ret = cnl_dsp_enable_core(ctx, SKL_DSP_CORE0_MASK);
  63. if (ret < 0) {
  64. dev_err(ctx->dev, "dsp boot core failed ret: %d\n", ret);
  65. ret = -EIO;
  66. goto base_fw_load_failed;
  67. }
  68. /* enable interrupt */
  69. cnl_ipc_int_enable(ctx);
  70. cnl_ipc_op_int_enable(ctx);
  71. ret = sst_dsp_register_poll(ctx, CNL_ADSP_FW_STATUS, CNL_FW_STS_MASK,
  72. CNL_FW_ROM_INIT, CNL_INIT_TIMEOUT,
  73. "rom load");
  74. if (ret < 0) {
  75. dev_err(ctx->dev, "rom init timeout, ret: %d\n", ret);
  76. goto base_fw_load_failed;
  77. }
  78. return 0;
  79. base_fw_load_failed:
  80. ctx->dsp_ops.cleanup(ctx->dev, &ctx->dmab, stream_tag);
  81. cnl_dsp_disable_core(ctx, SKL_DSP_CORE0_MASK);
  82. return ret;
  83. }
  84. static int sst_transfer_fw_host_dma(struct sst_dsp *ctx)
  85. {
  86. int ret;
  87. ctx->dsp_ops.trigger(ctx->dev, true, ctx->dsp_ops.stream_tag);
  88. ret = sst_dsp_register_poll(ctx, CNL_ADSP_FW_STATUS, CNL_FW_STS_MASK,
  89. CNL_FW_INIT, CNL_BASEFW_TIMEOUT,
  90. "firmware boot");
  91. ctx->dsp_ops.trigger(ctx->dev, false, ctx->dsp_ops.stream_tag);
  92. ctx->dsp_ops.cleanup(ctx->dev, &ctx->dmab, ctx->dsp_ops.stream_tag);
  93. return ret;
  94. }
  95. static int cnl_load_base_firmware(struct sst_dsp *ctx)
  96. {
  97. struct firmware stripped_fw;
  98. struct skl_sst *cnl = ctx->thread_context;
  99. int ret;
  100. if (!ctx->fw) {
  101. ret = request_firmware(&ctx->fw, ctx->fw_name, ctx->dev);
  102. if (ret < 0) {
  103. dev_err(ctx->dev, "request firmware failed: %d\n", ret);
  104. goto cnl_load_base_firmware_failed;
  105. }
  106. }
  107. /* parse uuids if first boot */
  108. if (cnl->is_first_boot) {
  109. ret = snd_skl_parse_uuids(ctx, ctx->fw,
  110. CNL_ADSP_FW_HDR_OFFSET, 0);
  111. if (ret < 0)
  112. goto cnl_load_base_firmware_failed;
  113. }
  114. stripped_fw.data = ctx->fw->data;
  115. stripped_fw.size = ctx->fw->size;
  116. skl_dsp_strip_extended_manifest(&stripped_fw);
  117. ret = cnl_prepare_fw(ctx, stripped_fw.data, stripped_fw.size);
  118. if (ret < 0) {
  119. dev_err(ctx->dev, "prepare firmware failed: %d\n", ret);
  120. goto cnl_load_base_firmware_failed;
  121. }
  122. ret = sst_transfer_fw_host_dma(ctx);
  123. if (ret < 0) {
  124. dev_err(ctx->dev, "transfer firmware failed: %d\n", ret);
  125. cnl_dsp_disable_core(ctx, SKL_DSP_CORE0_MASK);
  126. goto cnl_load_base_firmware_failed;
  127. }
  128. ret = wait_event_timeout(cnl->boot_wait, cnl->boot_complete,
  129. msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
  130. if (ret == 0) {
  131. dev_err(ctx->dev, "FW ready timed-out\n");
  132. cnl_dsp_disable_core(ctx, SKL_DSP_CORE0_MASK);
  133. ret = -EIO;
  134. goto cnl_load_base_firmware_failed;
  135. }
  136. cnl->fw_loaded = true;
  137. return 0;
  138. cnl_load_base_firmware_failed:
  139. release_firmware(ctx->fw);
  140. ctx->fw = NULL;
  141. return ret;
  142. }
  143. static int cnl_set_dsp_D0(struct sst_dsp *ctx, unsigned int core_id)
  144. {
  145. struct skl_sst *cnl = ctx->thread_context;
  146. unsigned int core_mask = SKL_DSP_CORE_MASK(core_id);
  147. struct skl_ipc_dxstate_info dx;
  148. int ret;
  149. if (!cnl->fw_loaded) {
  150. cnl->boot_complete = false;
  151. ret = cnl_load_base_firmware(ctx);
  152. if (ret < 0) {
  153. dev_err(ctx->dev, "fw reload failed: %d\n", ret);
  154. return ret;
  155. }
  156. cnl->cores.state[core_id] = SKL_DSP_RUNNING;
  157. return ret;
  158. }
  159. ret = cnl_dsp_enable_core(ctx, core_mask);
  160. if (ret < 0) {
  161. dev_err(ctx->dev, "enable dsp core %d failed: %d\n",
  162. core_id, ret);
  163. goto err;
  164. }
  165. if (core_id == SKL_DSP_CORE0_ID) {
  166. /* enable interrupt */
  167. cnl_ipc_int_enable(ctx);
  168. cnl_ipc_op_int_enable(ctx);
  169. cnl->boot_complete = false;
  170. ret = wait_event_timeout(cnl->boot_wait, cnl->boot_complete,
  171. msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
  172. if (ret == 0) {
  173. dev_err(ctx->dev,
  174. "dsp boot timeout, status=%#x error=%#x\n",
  175. sst_dsp_shim_read(ctx, CNL_ADSP_FW_STATUS),
  176. sst_dsp_shim_read(ctx, CNL_ADSP_ERROR_CODE));
  177. goto err;
  178. }
  179. } else {
  180. dx.core_mask = core_mask;
  181. dx.dx_mask = core_mask;
  182. ret = skl_ipc_set_dx(&cnl->ipc, CNL_INSTANCE_ID,
  183. CNL_BASE_FW_MODULE_ID, &dx);
  184. if (ret < 0) {
  185. dev_err(ctx->dev, "set_dx failed, core: %d ret: %d\n",
  186. core_id, ret);
  187. goto err;
  188. }
  189. }
  190. cnl->cores.state[core_id] = SKL_DSP_RUNNING;
  191. return 0;
  192. err:
  193. cnl_dsp_disable_core(ctx, core_mask);
  194. return ret;
  195. }
  196. static int cnl_set_dsp_D3(struct sst_dsp *ctx, unsigned int core_id)
  197. {
  198. struct skl_sst *cnl = ctx->thread_context;
  199. unsigned int core_mask = SKL_DSP_CORE_MASK(core_id);
  200. struct skl_ipc_dxstate_info dx;
  201. int ret;
  202. dx.core_mask = core_mask;
  203. dx.dx_mask = SKL_IPC_D3_MASK;
  204. ret = skl_ipc_set_dx(&cnl->ipc, CNL_INSTANCE_ID,
  205. CNL_BASE_FW_MODULE_ID, &dx);
  206. if (ret < 0) {
  207. dev_err(ctx->dev,
  208. "dsp core %d to d3 failed; continue reset\n",
  209. core_id);
  210. cnl->fw_loaded = false;
  211. }
  212. /* disable interrupts if core 0 */
  213. if (core_id == SKL_DSP_CORE0_ID) {
  214. skl_ipc_op_int_disable(ctx);
  215. skl_ipc_int_disable(ctx);
  216. }
  217. ret = cnl_dsp_disable_core(ctx, core_mask);
  218. if (ret < 0) {
  219. dev_err(ctx->dev, "disable dsp core %d failed: %d\n",
  220. core_id, ret);
  221. return ret;
  222. }
  223. cnl->cores.state[core_id] = SKL_DSP_RESET;
  224. return ret;
  225. }
  226. static unsigned int cnl_get_errno(struct sst_dsp *ctx)
  227. {
  228. return sst_dsp_shim_read(ctx, CNL_ADSP_ERROR_CODE);
  229. }
  230. static const struct skl_dsp_fw_ops cnl_fw_ops = {
  231. .set_state_D0 = cnl_set_dsp_D0,
  232. .set_state_D3 = cnl_set_dsp_D3,
  233. .load_fw = cnl_load_base_firmware,
  234. .get_fw_errcode = cnl_get_errno,
  235. };
  236. static struct sst_ops cnl_ops = {
  237. .irq_handler = cnl_dsp_sst_interrupt,
  238. .write = sst_shim32_write,
  239. .read = sst_shim32_read,
  240. .ram_read = sst_memcpy_fromio_32,
  241. .ram_write = sst_memcpy_toio_32,
  242. .free = cnl_dsp_free,
  243. };
  244. #define CNL_IPC_GLB_NOTIFY_RSP_SHIFT 29
  245. #define CNL_IPC_GLB_NOTIFY_RSP_MASK 0x1
  246. #define CNL_IPC_GLB_NOTIFY_RSP_TYPE(x) (((x) >> CNL_IPC_GLB_NOTIFY_RSP_SHIFT) \
  247. & CNL_IPC_GLB_NOTIFY_RSP_MASK)
  248. static irqreturn_t cnl_dsp_irq_thread_handler(int irq, void *context)
  249. {
  250. struct sst_dsp *dsp = context;
  251. struct skl_sst *cnl = sst_dsp_get_thread_context(dsp);
  252. struct sst_generic_ipc *ipc = &cnl->ipc;
  253. struct skl_ipc_header header = {0};
  254. u32 hipcida, hipctdr, hipctdd;
  255. int ipc_irq = 0;
  256. /* here we handle ipc interrupts only */
  257. if (!(dsp->intr_status & CNL_ADSPIS_IPC))
  258. return IRQ_NONE;
  259. hipcida = sst_dsp_shim_read_unlocked(dsp, CNL_ADSP_REG_HIPCIDA);
  260. hipctdr = sst_dsp_shim_read_unlocked(dsp, CNL_ADSP_REG_HIPCTDR);
  261. /* reply message from dsp */
  262. if (hipcida & CNL_ADSP_REG_HIPCIDA_DONE) {
  263. sst_dsp_shim_update_bits(dsp, CNL_ADSP_REG_HIPCCTL,
  264. CNL_ADSP_REG_HIPCCTL_DONE, 0);
  265. /* clear done bit - tell dsp operation is complete */
  266. sst_dsp_shim_update_bits_forced(dsp, CNL_ADSP_REG_HIPCIDA,
  267. CNL_ADSP_REG_HIPCIDA_DONE, CNL_ADSP_REG_HIPCIDA_DONE);
  268. ipc_irq = 1;
  269. /* unmask done interrupt */
  270. sst_dsp_shim_update_bits(dsp, CNL_ADSP_REG_HIPCCTL,
  271. CNL_ADSP_REG_HIPCCTL_DONE, CNL_ADSP_REG_HIPCCTL_DONE);
  272. }
  273. /* new message from dsp */
  274. if (hipctdr & CNL_ADSP_REG_HIPCTDR_BUSY) {
  275. hipctdd = sst_dsp_shim_read_unlocked(dsp, CNL_ADSP_REG_HIPCTDD);
  276. header.primary = hipctdr;
  277. header.extension = hipctdd;
  278. dev_dbg(dsp->dev, "IPC irq: Firmware respond primary:%x",
  279. header.primary);
  280. dev_dbg(dsp->dev, "IPC irq: Firmware respond extension:%x",
  281. header.extension);
  282. if (CNL_IPC_GLB_NOTIFY_RSP_TYPE(header.primary)) {
  283. /* Handle Immediate reply from DSP Core */
  284. skl_ipc_process_reply(ipc, header);
  285. } else {
  286. dev_dbg(dsp->dev, "IPC irq: Notification from firmware\n");
  287. skl_ipc_process_notification(ipc, header);
  288. }
  289. /* clear busy interrupt */
  290. sst_dsp_shim_update_bits_forced(dsp, CNL_ADSP_REG_HIPCTDR,
  291. CNL_ADSP_REG_HIPCTDR_BUSY, CNL_ADSP_REG_HIPCTDR_BUSY);
  292. /* set done bit to ack dsp */
  293. sst_dsp_shim_update_bits_forced(dsp, CNL_ADSP_REG_HIPCTDA,
  294. CNL_ADSP_REG_HIPCTDA_DONE, CNL_ADSP_REG_HIPCTDA_DONE);
  295. ipc_irq = 1;
  296. }
  297. if (ipc_irq == 0)
  298. return IRQ_NONE;
  299. cnl_ipc_int_enable(dsp);
  300. /* continue to send any remaining messages */
  301. schedule_work(&ipc->kwork);
  302. return IRQ_HANDLED;
  303. }
  304. static struct sst_dsp_device cnl_dev = {
  305. .thread = cnl_dsp_irq_thread_handler,
  306. .ops = &cnl_ops,
  307. };
  308. static void cnl_ipc_tx_msg(struct sst_generic_ipc *ipc, struct ipc_message *msg)
  309. {
  310. struct skl_ipc_header *header = (struct skl_ipc_header *)(&msg->header);
  311. if (msg->tx_size)
  312. sst_dsp_outbox_write(ipc->dsp, msg->tx_data, msg->tx_size);
  313. sst_dsp_shim_write_unlocked(ipc->dsp, CNL_ADSP_REG_HIPCIDD,
  314. header->extension);
  315. sst_dsp_shim_write_unlocked(ipc->dsp, CNL_ADSP_REG_HIPCIDR,
  316. header->primary | CNL_ADSP_REG_HIPCIDR_BUSY);
  317. }
  318. static bool cnl_ipc_is_dsp_busy(struct sst_dsp *dsp)
  319. {
  320. u32 hipcidr;
  321. hipcidr = sst_dsp_shim_read_unlocked(dsp, CNL_ADSP_REG_HIPCIDR);
  322. return (hipcidr & CNL_ADSP_REG_HIPCIDR_BUSY);
  323. }
  324. static int cnl_ipc_init(struct device *dev, struct skl_sst *cnl)
  325. {
  326. struct sst_generic_ipc *ipc;
  327. int err;
  328. ipc = &cnl->ipc;
  329. ipc->dsp = cnl->dsp;
  330. ipc->dev = dev;
  331. ipc->tx_data_max_size = CNL_ADSP_W1_SZ;
  332. ipc->rx_data_max_size = CNL_ADSP_W0_UP_SZ;
  333. err = sst_ipc_init(ipc);
  334. if (err)
  335. return err;
  336. /*
  337. * overriding tx_msg and is_dsp_busy since
  338. * ipc registers are different for cnl
  339. */
  340. ipc->ops.tx_msg = cnl_ipc_tx_msg;
  341. ipc->ops.tx_data_copy = skl_ipc_tx_data_copy;
  342. ipc->ops.is_dsp_busy = cnl_ipc_is_dsp_busy;
  343. return 0;
  344. }
  345. int cnl_sst_dsp_init(struct device *dev, void __iomem *mmio_base, int irq,
  346. const char *fw_name, struct skl_dsp_loader_ops dsp_ops,
  347. struct skl_sst **dsp)
  348. {
  349. struct skl_sst *cnl;
  350. struct sst_dsp *sst;
  351. int ret;
  352. ret = skl_sst_ctx_init(dev, irq, fw_name, dsp_ops, dsp, &cnl_dev);
  353. if (ret < 0) {
  354. dev_err(dev, "%s: no device\n", __func__);
  355. return ret;
  356. }
  357. cnl = *dsp;
  358. sst = cnl->dsp;
  359. sst->fw_ops = cnl_fw_ops;
  360. sst->addr.lpe = mmio_base;
  361. sst->addr.shim = mmio_base;
  362. sst->addr.sram0_base = CNL_ADSP_SRAM0_BASE;
  363. sst->addr.sram1_base = CNL_ADSP_SRAM1_BASE;
  364. sst->addr.w0_stat_sz = CNL_ADSP_W0_STAT_SZ;
  365. sst->addr.w0_up_sz = CNL_ADSP_W0_UP_SZ;
  366. sst_dsp_mailbox_init(sst, (CNL_ADSP_SRAM0_BASE + CNL_ADSP_W0_STAT_SZ),
  367. CNL_ADSP_W0_UP_SZ, CNL_ADSP_SRAM1_BASE,
  368. CNL_ADSP_W1_SZ);
  369. ret = cnl_ipc_init(dev, cnl);
  370. if (ret) {
  371. skl_dsp_free(sst);
  372. return ret;
  373. }
  374. cnl->boot_complete = false;
  375. init_waitqueue_head(&cnl->boot_wait);
  376. return skl_dsp_acquire_irq(sst);
  377. }
  378. EXPORT_SYMBOL_GPL(cnl_sst_dsp_init);
  379. int cnl_sst_init_fw(struct device *dev, struct skl_sst *ctx)
  380. {
  381. int ret;
  382. struct sst_dsp *sst = ctx->dsp;
  383. ret = ctx->dsp->fw_ops.load_fw(sst);
  384. if (ret < 0) {
  385. dev_err(dev, "load base fw failed: %d", ret);
  386. return ret;
  387. }
  388. skl_dsp_init_core_state(sst);
  389. ctx->is_first_boot = false;
  390. return 0;
  391. }
  392. EXPORT_SYMBOL_GPL(cnl_sst_init_fw);
  393. void cnl_sst_dsp_cleanup(struct device *dev, struct skl_sst *ctx)
  394. {
  395. if (ctx->dsp->fw)
  396. release_firmware(ctx->dsp->fw);
  397. skl_freeup_uuid_list(ctx);
  398. cnl_ipc_free(&ctx->ipc);
  399. ctx->dsp->ops->free(ctx->dsp);
  400. }
  401. EXPORT_SYMBOL_GPL(cnl_sst_dsp_cleanup);
  402. MODULE_LICENSE("GPL v2");
  403. MODULE_DESCRIPTION("Intel Cannonlake IPC driver");