cnl-sst.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. ret = -ETIMEDOUT;
  178. goto err;
  179. }
  180. } else {
  181. dx.core_mask = core_mask;
  182. dx.dx_mask = core_mask;
  183. ret = skl_ipc_set_dx(&cnl->ipc, CNL_INSTANCE_ID,
  184. CNL_BASE_FW_MODULE_ID, &dx);
  185. if (ret < 0) {
  186. dev_err(ctx->dev, "set_dx failed, core: %d ret: %d\n",
  187. core_id, ret);
  188. goto err;
  189. }
  190. }
  191. cnl->cores.state[core_id] = SKL_DSP_RUNNING;
  192. return 0;
  193. err:
  194. cnl_dsp_disable_core(ctx, core_mask);
  195. return ret;
  196. }
  197. static int cnl_set_dsp_D3(struct sst_dsp *ctx, unsigned int core_id)
  198. {
  199. struct skl_sst *cnl = ctx->thread_context;
  200. unsigned int core_mask = SKL_DSP_CORE_MASK(core_id);
  201. struct skl_ipc_dxstate_info dx;
  202. int ret;
  203. dx.core_mask = core_mask;
  204. dx.dx_mask = SKL_IPC_D3_MASK;
  205. ret = skl_ipc_set_dx(&cnl->ipc, CNL_INSTANCE_ID,
  206. CNL_BASE_FW_MODULE_ID, &dx);
  207. if (ret < 0) {
  208. dev_err(ctx->dev,
  209. "dsp core %d to d3 failed; continue reset\n",
  210. core_id);
  211. cnl->fw_loaded = false;
  212. }
  213. /* disable interrupts if core 0 */
  214. if (core_id == SKL_DSP_CORE0_ID) {
  215. skl_ipc_op_int_disable(ctx);
  216. skl_ipc_int_disable(ctx);
  217. }
  218. ret = cnl_dsp_disable_core(ctx, core_mask);
  219. if (ret < 0) {
  220. dev_err(ctx->dev, "disable dsp core %d failed: %d\n",
  221. core_id, ret);
  222. return ret;
  223. }
  224. cnl->cores.state[core_id] = SKL_DSP_RESET;
  225. return ret;
  226. }
  227. static unsigned int cnl_get_errno(struct sst_dsp *ctx)
  228. {
  229. return sst_dsp_shim_read(ctx, CNL_ADSP_ERROR_CODE);
  230. }
  231. static const struct skl_dsp_fw_ops cnl_fw_ops = {
  232. .set_state_D0 = cnl_set_dsp_D0,
  233. .set_state_D3 = cnl_set_dsp_D3,
  234. .load_fw = cnl_load_base_firmware,
  235. .get_fw_errcode = cnl_get_errno,
  236. };
  237. static struct sst_ops cnl_ops = {
  238. .irq_handler = cnl_dsp_sst_interrupt,
  239. .write = sst_shim32_write,
  240. .read = sst_shim32_read,
  241. .ram_read = sst_memcpy_fromio_32,
  242. .ram_write = sst_memcpy_toio_32,
  243. .free = cnl_dsp_free,
  244. };
  245. #define CNL_IPC_GLB_NOTIFY_RSP_SHIFT 29
  246. #define CNL_IPC_GLB_NOTIFY_RSP_MASK 0x1
  247. #define CNL_IPC_GLB_NOTIFY_RSP_TYPE(x) (((x) >> CNL_IPC_GLB_NOTIFY_RSP_SHIFT) \
  248. & CNL_IPC_GLB_NOTIFY_RSP_MASK)
  249. static irqreturn_t cnl_dsp_irq_thread_handler(int irq, void *context)
  250. {
  251. struct sst_dsp *dsp = context;
  252. struct skl_sst *cnl = sst_dsp_get_thread_context(dsp);
  253. struct sst_generic_ipc *ipc = &cnl->ipc;
  254. struct skl_ipc_header header = {0};
  255. u32 hipcida, hipctdr, hipctdd;
  256. int ipc_irq = 0;
  257. /* here we handle ipc interrupts only */
  258. if (!(dsp->intr_status & CNL_ADSPIS_IPC))
  259. return IRQ_NONE;
  260. hipcida = sst_dsp_shim_read_unlocked(dsp, CNL_ADSP_REG_HIPCIDA);
  261. hipctdr = sst_dsp_shim_read_unlocked(dsp, CNL_ADSP_REG_HIPCTDR);
  262. /* reply message from dsp */
  263. if (hipcida & CNL_ADSP_REG_HIPCIDA_DONE) {
  264. sst_dsp_shim_update_bits(dsp, CNL_ADSP_REG_HIPCCTL,
  265. CNL_ADSP_REG_HIPCCTL_DONE, 0);
  266. /* clear done bit - tell dsp operation is complete */
  267. sst_dsp_shim_update_bits_forced(dsp, CNL_ADSP_REG_HIPCIDA,
  268. CNL_ADSP_REG_HIPCIDA_DONE, CNL_ADSP_REG_HIPCIDA_DONE);
  269. ipc_irq = 1;
  270. /* unmask done interrupt */
  271. sst_dsp_shim_update_bits(dsp, CNL_ADSP_REG_HIPCCTL,
  272. CNL_ADSP_REG_HIPCCTL_DONE, CNL_ADSP_REG_HIPCCTL_DONE);
  273. }
  274. /* new message from dsp */
  275. if (hipctdr & CNL_ADSP_REG_HIPCTDR_BUSY) {
  276. hipctdd = sst_dsp_shim_read_unlocked(dsp, CNL_ADSP_REG_HIPCTDD);
  277. header.primary = hipctdr;
  278. header.extension = hipctdd;
  279. dev_dbg(dsp->dev, "IPC irq: Firmware respond primary:%x",
  280. header.primary);
  281. dev_dbg(dsp->dev, "IPC irq: Firmware respond extension:%x",
  282. header.extension);
  283. if (CNL_IPC_GLB_NOTIFY_RSP_TYPE(header.primary)) {
  284. /* Handle Immediate reply from DSP Core */
  285. skl_ipc_process_reply(ipc, header);
  286. } else {
  287. dev_dbg(dsp->dev, "IPC irq: Notification from firmware\n");
  288. skl_ipc_process_notification(ipc, header);
  289. }
  290. /* clear busy interrupt */
  291. sst_dsp_shim_update_bits_forced(dsp, CNL_ADSP_REG_HIPCTDR,
  292. CNL_ADSP_REG_HIPCTDR_BUSY, CNL_ADSP_REG_HIPCTDR_BUSY);
  293. /* set done bit to ack dsp */
  294. sst_dsp_shim_update_bits_forced(dsp, CNL_ADSP_REG_HIPCTDA,
  295. CNL_ADSP_REG_HIPCTDA_DONE, CNL_ADSP_REG_HIPCTDA_DONE);
  296. ipc_irq = 1;
  297. }
  298. if (ipc_irq == 0)
  299. return IRQ_NONE;
  300. cnl_ipc_int_enable(dsp);
  301. /* continue to send any remaining messages */
  302. schedule_work(&ipc->kwork);
  303. return IRQ_HANDLED;
  304. }
  305. static struct sst_dsp_device cnl_dev = {
  306. .thread = cnl_dsp_irq_thread_handler,
  307. .ops = &cnl_ops,
  308. };
  309. static void cnl_ipc_tx_msg(struct sst_generic_ipc *ipc, struct ipc_message *msg)
  310. {
  311. struct skl_ipc_header *header = (struct skl_ipc_header *)(&msg->header);
  312. if (msg->tx_size)
  313. sst_dsp_outbox_write(ipc->dsp, msg->tx_data, msg->tx_size);
  314. sst_dsp_shim_write_unlocked(ipc->dsp, CNL_ADSP_REG_HIPCIDD,
  315. header->extension);
  316. sst_dsp_shim_write_unlocked(ipc->dsp, CNL_ADSP_REG_HIPCIDR,
  317. header->primary | CNL_ADSP_REG_HIPCIDR_BUSY);
  318. }
  319. static bool cnl_ipc_is_dsp_busy(struct sst_dsp *dsp)
  320. {
  321. u32 hipcidr;
  322. hipcidr = sst_dsp_shim_read_unlocked(dsp, CNL_ADSP_REG_HIPCIDR);
  323. return (hipcidr & CNL_ADSP_REG_HIPCIDR_BUSY);
  324. }
  325. static int cnl_ipc_init(struct device *dev, struct skl_sst *cnl)
  326. {
  327. struct sst_generic_ipc *ipc;
  328. int err;
  329. ipc = &cnl->ipc;
  330. ipc->dsp = cnl->dsp;
  331. ipc->dev = dev;
  332. ipc->tx_data_max_size = CNL_ADSP_W1_SZ;
  333. ipc->rx_data_max_size = CNL_ADSP_W0_UP_SZ;
  334. err = sst_ipc_init(ipc);
  335. if (err)
  336. return err;
  337. /*
  338. * overriding tx_msg and is_dsp_busy since
  339. * ipc registers are different for cnl
  340. */
  341. ipc->ops.tx_msg = cnl_ipc_tx_msg;
  342. ipc->ops.tx_data_copy = skl_ipc_tx_data_copy;
  343. ipc->ops.is_dsp_busy = cnl_ipc_is_dsp_busy;
  344. return 0;
  345. }
  346. int cnl_sst_dsp_init(struct device *dev, void __iomem *mmio_base, int irq,
  347. const char *fw_name, struct skl_dsp_loader_ops dsp_ops,
  348. struct skl_sst **dsp)
  349. {
  350. struct skl_sst *cnl;
  351. struct sst_dsp *sst;
  352. int ret;
  353. ret = skl_sst_ctx_init(dev, irq, fw_name, dsp_ops, dsp, &cnl_dev);
  354. if (ret < 0) {
  355. dev_err(dev, "%s: no device\n", __func__);
  356. return ret;
  357. }
  358. cnl = *dsp;
  359. sst = cnl->dsp;
  360. sst->fw_ops = cnl_fw_ops;
  361. sst->addr.lpe = mmio_base;
  362. sst->addr.shim = mmio_base;
  363. sst->addr.sram0_base = CNL_ADSP_SRAM0_BASE;
  364. sst->addr.sram1_base = CNL_ADSP_SRAM1_BASE;
  365. sst->addr.w0_stat_sz = CNL_ADSP_W0_STAT_SZ;
  366. sst->addr.w0_up_sz = CNL_ADSP_W0_UP_SZ;
  367. sst_dsp_mailbox_init(sst, (CNL_ADSP_SRAM0_BASE + CNL_ADSP_W0_STAT_SZ),
  368. CNL_ADSP_W0_UP_SZ, CNL_ADSP_SRAM1_BASE,
  369. CNL_ADSP_W1_SZ);
  370. ret = cnl_ipc_init(dev, cnl);
  371. if (ret) {
  372. skl_dsp_free(sst);
  373. return ret;
  374. }
  375. cnl->boot_complete = false;
  376. init_waitqueue_head(&cnl->boot_wait);
  377. return skl_dsp_acquire_irq(sst);
  378. }
  379. EXPORT_SYMBOL_GPL(cnl_sst_dsp_init);
  380. int cnl_sst_init_fw(struct device *dev, struct skl_sst *ctx)
  381. {
  382. int ret;
  383. struct sst_dsp *sst = ctx->dsp;
  384. ret = ctx->dsp->fw_ops.load_fw(sst);
  385. if (ret < 0) {
  386. dev_err(dev, "load base fw failed: %d", ret);
  387. return ret;
  388. }
  389. skl_dsp_init_core_state(sst);
  390. ctx->is_first_boot = false;
  391. return 0;
  392. }
  393. EXPORT_SYMBOL_GPL(cnl_sst_init_fw);
  394. void cnl_sst_dsp_cleanup(struct device *dev, struct skl_sst *ctx)
  395. {
  396. if (ctx->dsp->fw)
  397. release_firmware(ctx->dsp->fw);
  398. skl_freeup_uuid_list(ctx);
  399. cnl_ipc_free(&ctx->ipc);
  400. ctx->dsp->ops->free(ctx->dsp);
  401. }
  402. EXPORT_SYMBOL_GPL(cnl_sst_dsp_cleanup);
  403. MODULE_LICENSE("GPL v2");
  404. MODULE_DESCRIPTION("Intel Cannonlake IPC driver");