hda-ipc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. //
  3. // This file is provided under a dual BSD/GPLv2 license. When using or
  4. // redistributing this file, you may do so under either license.
  5. //
  6. // Copyright(c) 2018 Intel Corporation. All rights reserved.
  7. //
  8. // Authors: Liam Girdwood <[email protected]>
  9. // Ranjani Sridharan <[email protected]>
  10. // Rander Wang <[email protected]>
  11. // Keyon Jie <[email protected]>
  12. //
  13. /*
  14. * Hardware interface for generic Intel audio DSP HDA IP
  15. */
  16. #include <sound/sof/ipc4/header.h>
  17. #include <trace/events/sof_intel.h>
  18. #include "../ops.h"
  19. #include "hda.h"
  20. static void hda_dsp_ipc_host_done(struct snd_sof_dev *sdev)
  21. {
  22. /*
  23. * tell DSP cmd is done - clear busy
  24. * interrupt and send reply msg to dsp
  25. */
  26. snd_sof_dsp_update_bits_forced(sdev, HDA_DSP_BAR,
  27. HDA_DSP_REG_HIPCT,
  28. HDA_DSP_REG_HIPCT_BUSY,
  29. HDA_DSP_REG_HIPCT_BUSY);
  30. /* unmask BUSY interrupt */
  31. snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR,
  32. HDA_DSP_REG_HIPCCTL,
  33. HDA_DSP_REG_HIPCCTL_BUSY,
  34. HDA_DSP_REG_HIPCCTL_BUSY);
  35. }
  36. static void hda_dsp_ipc_dsp_done(struct snd_sof_dev *sdev)
  37. {
  38. /*
  39. * set DONE bit - tell DSP we have received the reply msg
  40. * from DSP, and processed it, don't send more reply to host
  41. */
  42. snd_sof_dsp_update_bits_forced(sdev, HDA_DSP_BAR,
  43. HDA_DSP_REG_HIPCIE,
  44. HDA_DSP_REG_HIPCIE_DONE,
  45. HDA_DSP_REG_HIPCIE_DONE);
  46. /* unmask Done interrupt */
  47. snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR,
  48. HDA_DSP_REG_HIPCCTL,
  49. HDA_DSP_REG_HIPCCTL_DONE,
  50. HDA_DSP_REG_HIPCCTL_DONE);
  51. }
  52. int hda_dsp_ipc_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)
  53. {
  54. /* send IPC message to DSP */
  55. sof_mailbox_write(sdev, sdev->host_box.offset, msg->msg_data,
  56. msg->msg_size);
  57. snd_sof_dsp_write(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCI,
  58. HDA_DSP_REG_HIPCI_BUSY);
  59. return 0;
  60. }
  61. int hda_dsp_ipc4_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)
  62. {
  63. struct sof_ipc4_msg *msg_data = msg->msg_data;
  64. /* send the message via mailbox */
  65. if (msg_data->data_size)
  66. sof_mailbox_write(sdev, sdev->host_box.offset, msg_data->data_ptr,
  67. msg_data->data_size);
  68. snd_sof_dsp_write(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCIE, msg_data->extension);
  69. snd_sof_dsp_write(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCI,
  70. msg_data->primary | HDA_DSP_REG_HIPCI_BUSY);
  71. return 0;
  72. }
  73. void hda_dsp_ipc_get_reply(struct snd_sof_dev *sdev)
  74. {
  75. struct snd_sof_ipc_msg *msg = sdev->msg;
  76. struct sof_ipc_reply reply;
  77. struct sof_ipc_cmd_hdr *hdr;
  78. /*
  79. * Sometimes, there is unexpected reply ipc arriving. The reply
  80. * ipc belongs to none of the ipcs sent from driver.
  81. * In this case, the driver must ignore the ipc.
  82. */
  83. if (!msg) {
  84. dev_warn(sdev->dev, "unexpected ipc interrupt raised!\n");
  85. return;
  86. }
  87. hdr = msg->msg_data;
  88. if (hdr->cmd == (SOF_IPC_GLB_PM_MSG | SOF_IPC_PM_CTX_SAVE) ||
  89. hdr->cmd == (SOF_IPC_GLB_PM_MSG | SOF_IPC_PM_GATE)) {
  90. /*
  91. * memory windows are powered off before sending IPC reply,
  92. * so we can't read the mailbox for CTX_SAVE and PM_GATE
  93. * replies.
  94. */
  95. reply.error = 0;
  96. reply.hdr.cmd = SOF_IPC_GLB_REPLY;
  97. reply.hdr.size = sizeof(reply);
  98. memcpy(msg->reply_data, &reply, sizeof(reply));
  99. msg->reply_error = 0;
  100. } else {
  101. snd_sof_ipc_get_reply(sdev);
  102. }
  103. }
  104. irqreturn_t hda_dsp_ipc4_irq_thread(int irq, void *context)
  105. {
  106. struct sof_ipc4_msg notification_data = {{ 0 }};
  107. struct snd_sof_dev *sdev = context;
  108. bool ipc_irq = false;
  109. u32 hipcie, hipct;
  110. hipcie = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCIE);
  111. if (hipcie & HDA_DSP_REG_HIPCIE_DONE) {
  112. /* DSP received the message */
  113. snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCCTL,
  114. HDA_DSP_REG_HIPCCTL_DONE, 0);
  115. hda_dsp_ipc_dsp_done(sdev);
  116. ipc_irq = true;
  117. }
  118. hipct = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCT);
  119. if (hipct & HDA_DSP_REG_HIPCT_BUSY) {
  120. /* Message from DSP (reply or notification) */
  121. u32 hipcte = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
  122. HDA_DSP_REG_HIPCTE);
  123. u32 primary = hipct & HDA_DSP_REG_HIPCT_MSG_MASK;
  124. u32 extension = hipcte & HDA_DSP_REG_HIPCTE_MSG_MASK;
  125. /* mask BUSY interrupt */
  126. snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCCTL,
  127. HDA_DSP_REG_HIPCCTL_BUSY, 0);
  128. if (primary & SOF_IPC4_MSG_DIR_MASK) {
  129. /* Reply received */
  130. if (likely(sdev->fw_state == SOF_FW_BOOT_COMPLETE)) {
  131. struct sof_ipc4_msg *data = sdev->ipc->msg.reply_data;
  132. data->primary = primary;
  133. data->extension = extension;
  134. spin_lock_irq(&sdev->ipc_lock);
  135. snd_sof_ipc_get_reply(sdev);
  136. snd_sof_ipc_reply(sdev, data->primary);
  137. spin_unlock_irq(&sdev->ipc_lock);
  138. } else {
  139. dev_dbg_ratelimited(sdev->dev,
  140. "IPC reply before FW_READY: %#x|%#x\n",
  141. primary, extension);
  142. }
  143. } else {
  144. /* Notification received */
  145. notification_data.primary = primary;
  146. notification_data.extension = extension;
  147. sdev->ipc->msg.rx_data = &notification_data;
  148. snd_sof_ipc_msgs_rx(sdev);
  149. sdev->ipc->msg.rx_data = NULL;
  150. }
  151. /* Let DSP know that we have finished processing the message */
  152. hda_dsp_ipc_host_done(sdev);
  153. ipc_irq = true;
  154. }
  155. if (!ipc_irq)
  156. /* This interrupt is not shared so no need to return IRQ_NONE. */
  157. dev_dbg_ratelimited(sdev->dev, "nothing to do in IPC IRQ thread\n");
  158. return IRQ_HANDLED;
  159. }
  160. /* IPC handler thread */
  161. irqreturn_t hda_dsp_ipc_irq_thread(int irq, void *context)
  162. {
  163. struct snd_sof_dev *sdev = context;
  164. u32 hipci;
  165. u32 hipcie;
  166. u32 hipct;
  167. u32 hipcte;
  168. u32 msg;
  169. u32 msg_ext;
  170. bool ipc_irq = false;
  171. /* read IPC status */
  172. hipcie = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
  173. HDA_DSP_REG_HIPCIE);
  174. hipct = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCT);
  175. hipci = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCI);
  176. hipcte = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCTE);
  177. /* is this a reply message from the DSP */
  178. if (hipcie & HDA_DSP_REG_HIPCIE_DONE) {
  179. msg = hipci & HDA_DSP_REG_HIPCI_MSG_MASK;
  180. msg_ext = hipcie & HDA_DSP_REG_HIPCIE_MSG_MASK;
  181. trace_sof_intel_ipc_firmware_response(sdev, msg, msg_ext);
  182. /* mask Done interrupt */
  183. snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR,
  184. HDA_DSP_REG_HIPCCTL,
  185. HDA_DSP_REG_HIPCCTL_DONE, 0);
  186. /*
  187. * Make sure the interrupt thread cannot be preempted between
  188. * waking up the sender and re-enabling the interrupt. Also
  189. * protect against a theoretical race with sof_ipc_tx_message():
  190. * if the DSP is fast enough to receive an IPC message, reply to
  191. * it, and the host interrupt processing calls this function on
  192. * a different core from the one, where the sending is taking
  193. * place, the message might not yet be marked as expecting a
  194. * reply.
  195. */
  196. if (likely(sdev->fw_state == SOF_FW_BOOT_COMPLETE)) {
  197. spin_lock_irq(&sdev->ipc_lock);
  198. /* handle immediate reply from DSP core */
  199. hda_dsp_ipc_get_reply(sdev);
  200. snd_sof_ipc_reply(sdev, msg);
  201. /* set the done bit */
  202. hda_dsp_ipc_dsp_done(sdev);
  203. spin_unlock_irq(&sdev->ipc_lock);
  204. } else {
  205. dev_dbg_ratelimited(sdev->dev, "IPC reply before FW_READY: %#x\n",
  206. msg);
  207. }
  208. ipc_irq = true;
  209. }
  210. /* is this a new message from DSP */
  211. if (hipct & HDA_DSP_REG_HIPCT_BUSY) {
  212. msg = hipct & HDA_DSP_REG_HIPCT_MSG_MASK;
  213. msg_ext = hipcte & HDA_DSP_REG_HIPCTE_MSG_MASK;
  214. trace_sof_intel_ipc_firmware_initiated(sdev, msg, msg_ext);
  215. /* mask BUSY interrupt */
  216. snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR,
  217. HDA_DSP_REG_HIPCCTL,
  218. HDA_DSP_REG_HIPCCTL_BUSY, 0);
  219. /* handle messages from DSP */
  220. if ((hipct & SOF_IPC_PANIC_MAGIC_MASK) == SOF_IPC_PANIC_MAGIC) {
  221. struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
  222. bool non_recoverable = true;
  223. /*
  224. * This is a PANIC message!
  225. *
  226. * If it is arriving during firmware boot and it is not
  227. * the last boot attempt then change the non_recoverable
  228. * to false as the DSP might be able to boot in the next
  229. * iteration(s)
  230. */
  231. if (sdev->fw_state == SOF_FW_BOOT_IN_PROGRESS &&
  232. hda->boot_iteration < HDA_FW_BOOT_ATTEMPTS)
  233. non_recoverable = false;
  234. snd_sof_dsp_panic(sdev, HDA_DSP_PANIC_OFFSET(msg_ext),
  235. non_recoverable);
  236. } else {
  237. /* normal message - process normally */
  238. snd_sof_ipc_msgs_rx(sdev);
  239. }
  240. hda_dsp_ipc_host_done(sdev);
  241. ipc_irq = true;
  242. }
  243. if (!ipc_irq) {
  244. /*
  245. * This interrupt is not shared so no need to return IRQ_NONE.
  246. */
  247. dev_dbg_ratelimited(sdev->dev,
  248. "nothing to do in IPC IRQ thread\n");
  249. }
  250. return IRQ_HANDLED;
  251. }
  252. /* Check if an IPC IRQ occurred */
  253. bool hda_dsp_check_ipc_irq(struct snd_sof_dev *sdev)
  254. {
  255. struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
  256. bool ret = false;
  257. u32 irq_status;
  258. /* store status */
  259. irq_status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS);
  260. trace_sof_intel_hda_irq_ipc_check(sdev, irq_status);
  261. /* invalid message ? */
  262. if (irq_status == 0xffffffff)
  263. goto out;
  264. /* IPC message ? */
  265. if (irq_status & HDA_DSP_ADSPIS_IPC)
  266. ret = true;
  267. /* CLDMA message ? */
  268. if (irq_status & HDA_DSP_ADSPIS_CL_DMA) {
  269. hda->code_loading = 0;
  270. wake_up(&hda->waitq);
  271. ret = false;
  272. }
  273. out:
  274. return ret;
  275. }
  276. int hda_dsp_ipc_get_mailbox_offset(struct snd_sof_dev *sdev)
  277. {
  278. return HDA_DSP_MBOX_UPLINK_OFFSET;
  279. }
  280. int hda_dsp_ipc_get_window_offset(struct snd_sof_dev *sdev, u32 id)
  281. {
  282. return SRAM_WINDOW_OFFSET(id);
  283. }
  284. int hda_ipc_msg_data(struct snd_sof_dev *sdev,
  285. struct snd_pcm_substream *substream,
  286. void *p, size_t sz)
  287. {
  288. if (!substream || !sdev->stream_box.size) {
  289. sof_mailbox_read(sdev, sdev->dsp_box.offset, p, sz);
  290. } else {
  291. struct hdac_stream *hstream = substream->runtime->private_data;
  292. struct sof_intel_hda_stream *hda_stream;
  293. hda_stream = container_of(hstream,
  294. struct sof_intel_hda_stream,
  295. hext_stream.hstream);
  296. /* The stream might already be closed */
  297. if (!hstream)
  298. return -ESTRPIPE;
  299. sof_mailbox_read(sdev, hda_stream->sof_intel_stream.posn_offset, p, sz);
  300. }
  301. return 0;
  302. }
  303. int hda_set_stream_data_offset(struct snd_sof_dev *sdev,
  304. struct snd_pcm_substream *substream,
  305. size_t posn_offset)
  306. {
  307. struct hdac_stream *hstream = substream->runtime->private_data;
  308. struct sof_intel_hda_stream *hda_stream;
  309. hda_stream = container_of(hstream, struct sof_intel_hda_stream,
  310. hext_stream.hstream);
  311. /* check for unaligned offset or overflow */
  312. if (posn_offset > sdev->stream_box.size ||
  313. posn_offset % sizeof(struct sof_ipc_stream_posn) != 0)
  314. return -EINVAL;
  315. hda_stream->sof_intel_stream.posn_offset = sdev->stream_box.offset + posn_offset;
  316. dev_dbg(sdev->dev, "pcm: stream dir %d, posn mailbox offset is %zu",
  317. substream->stream, hda_stream->sof_intel_stream.posn_offset);
  318. return 0;
  319. }