ipc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // Copyright(c) 2020 Intel Corporation
  4. //
  5. // Author: Cezary Rojewski <cezary.rojewski@intel.com>
  6. //
  7. #include <linux/irqreturn.h>
  8. #include "core.h"
  9. #include "messages.h"
  10. #include "registers.h"
  11. #include "trace.h"
  12. #define CATPT_IPC_TIMEOUT_MS 300
  13. void catpt_ipc_init(struct catpt_ipc *ipc, struct device *dev)
  14. {
  15. ipc->dev = dev;
  16. ipc->ready = false;
  17. ipc->default_timeout = CATPT_IPC_TIMEOUT_MS;
  18. init_completion(&ipc->done_completion);
  19. init_completion(&ipc->busy_completion);
  20. spin_lock_init(&ipc->lock);
  21. mutex_init(&ipc->mutex);
  22. }
  23. static int catpt_ipc_arm(struct catpt_ipc *ipc, struct catpt_fw_ready *config)
  24. {
  25. /*
  26. * Both tx and rx are put into and received from outbox. Inbox is
  27. * only used for notifications where payload size is known upfront,
  28. * thus no separate buffer is allocated for it.
  29. */
  30. ipc->rx.data = devm_kzalloc(ipc->dev, config->outbox_size, GFP_KERNEL);
  31. if (!ipc->rx.data)
  32. return -ENOMEM;
  33. memcpy(&ipc->config, config, sizeof(*config));
  34. ipc->ready = true;
  35. return 0;
  36. }
  37. static void catpt_ipc_msg_init(struct catpt_ipc *ipc,
  38. struct catpt_ipc_msg *reply)
  39. {
  40. lockdep_assert_held(&ipc->lock);
  41. ipc->rx.header = 0;
  42. ipc->rx.size = reply ? reply->size : 0;
  43. reinit_completion(&ipc->done_completion);
  44. reinit_completion(&ipc->busy_completion);
  45. }
  46. static void catpt_dsp_send_tx(struct catpt_dev *cdev,
  47. const struct catpt_ipc_msg *tx)
  48. {
  49. u32 header = tx->header | CATPT_IPCC_BUSY;
  50. trace_catpt_ipc_request(header);
  51. trace_catpt_ipc_payload(tx->data, tx->size);
  52. memcpy_toio(catpt_outbox_addr(cdev), tx->data, tx->size);
  53. catpt_writel_shim(cdev, IPCC, header);
  54. }
  55. static int catpt_wait_msg_completion(struct catpt_dev *cdev, int timeout)
  56. {
  57. struct catpt_ipc *ipc = &cdev->ipc;
  58. int ret;
  59. ret = wait_for_completion_timeout(&ipc->done_completion,
  60. msecs_to_jiffies(timeout));
  61. if (!ret)
  62. return -ETIMEDOUT;
  63. if (ipc->rx.rsp.status != CATPT_REPLY_PENDING)
  64. return 0;
  65. /* wait for delayed reply */
  66. ret = wait_for_completion_timeout(&ipc->busy_completion,
  67. msecs_to_jiffies(timeout));
  68. return ret ? 0 : -ETIMEDOUT;
  69. }
  70. static int catpt_dsp_do_send_msg(struct catpt_dev *cdev,
  71. struct catpt_ipc_msg request,
  72. struct catpt_ipc_msg *reply, int timeout)
  73. {
  74. struct catpt_ipc *ipc = &cdev->ipc;
  75. unsigned long flags;
  76. int ret;
  77. if (!ipc->ready)
  78. return -EPERM;
  79. if (request.size > ipc->config.outbox_size ||
  80. (reply && reply->size > ipc->config.outbox_size))
  81. return -EINVAL;
  82. spin_lock_irqsave(&ipc->lock, flags);
  83. catpt_ipc_msg_init(ipc, reply);
  84. catpt_dsp_send_tx(cdev, &request);
  85. spin_unlock_irqrestore(&ipc->lock, flags);
  86. ret = catpt_wait_msg_completion(cdev, timeout);
  87. if (ret) {
  88. dev_crit(cdev->dev, "communication severed: %d, rebooting dsp..\n",
  89. ret);
  90. ipc->ready = false;
  91. /* TODO: attempt recovery */
  92. return ret;
  93. }
  94. ret = ipc->rx.rsp.status;
  95. if (reply) {
  96. reply->header = ipc->rx.header;
  97. if (!ret && reply->data)
  98. memcpy(reply->data, ipc->rx.data, reply->size);
  99. }
  100. return ret;
  101. }
  102. int catpt_dsp_send_msg_timeout(struct catpt_dev *cdev,
  103. struct catpt_ipc_msg request,
  104. struct catpt_ipc_msg *reply, int timeout)
  105. {
  106. struct catpt_ipc *ipc = &cdev->ipc;
  107. int ret;
  108. mutex_lock(&ipc->mutex);
  109. ret = catpt_dsp_do_send_msg(cdev, request, reply, timeout);
  110. mutex_unlock(&ipc->mutex);
  111. return ret;
  112. }
  113. int catpt_dsp_send_msg(struct catpt_dev *cdev, struct catpt_ipc_msg request,
  114. struct catpt_ipc_msg *reply)
  115. {
  116. return catpt_dsp_send_msg_timeout(cdev, request, reply,
  117. cdev->ipc.default_timeout);
  118. }
  119. static void
  120. catpt_dsp_notify_stream(struct catpt_dev *cdev, union catpt_notify_msg msg)
  121. {
  122. struct catpt_stream_runtime *stream;
  123. struct catpt_notify_position pos;
  124. struct catpt_notify_glitch glitch;
  125. stream = catpt_stream_find(cdev, msg.stream_hw_id);
  126. if (!stream) {
  127. dev_warn(cdev->dev, "notify %d for non-existent stream %d\n",
  128. msg.notify_reason, msg.stream_hw_id);
  129. return;
  130. }
  131. switch (msg.notify_reason) {
  132. case CATPT_NOTIFY_POSITION_CHANGED:
  133. memcpy_fromio(&pos, catpt_inbox_addr(cdev), sizeof(pos));
  134. trace_catpt_ipc_payload((u8 *)&pos, sizeof(pos));
  135. catpt_stream_update_position(cdev, stream, &pos);
  136. break;
  137. case CATPT_NOTIFY_GLITCH_OCCURRED:
  138. memcpy_fromio(&glitch, catpt_inbox_addr(cdev), sizeof(glitch));
  139. trace_catpt_ipc_payload((u8 *)&glitch, sizeof(glitch));
  140. dev_warn(cdev->dev, "glitch %d at pos: 0x%08llx, wp: 0x%08x\n",
  141. glitch.type, glitch.presentation_pos,
  142. glitch.write_pos);
  143. break;
  144. default:
  145. dev_warn(cdev->dev, "unknown notification: %d received\n",
  146. msg.notify_reason);
  147. break;
  148. }
  149. }
  150. static void catpt_dsp_copy_rx(struct catpt_dev *cdev, u32 header)
  151. {
  152. struct catpt_ipc *ipc = &cdev->ipc;
  153. ipc->rx.header = header;
  154. if (ipc->rx.rsp.status != CATPT_REPLY_SUCCESS)
  155. return;
  156. memcpy_fromio(ipc->rx.data, catpt_outbox_addr(cdev), ipc->rx.size);
  157. trace_catpt_ipc_payload(ipc->rx.data, ipc->rx.size);
  158. }
  159. static void catpt_dsp_process_response(struct catpt_dev *cdev, u32 header)
  160. {
  161. union catpt_notify_msg msg = CATPT_MSG(header);
  162. struct catpt_ipc *ipc = &cdev->ipc;
  163. if (msg.fw_ready) {
  164. struct catpt_fw_ready config;
  165. /* to fit 32b header original address is shifted right by 3 */
  166. u32 off = msg.mailbox_address << 3;
  167. memcpy_fromio(&config, cdev->lpe_ba + off, sizeof(config));
  168. trace_catpt_ipc_payload((u8 *)&config, sizeof(config));
  169. catpt_ipc_arm(ipc, &config);
  170. complete(&cdev->fw_ready);
  171. return;
  172. }
  173. switch (msg.global_msg_type) {
  174. case CATPT_GLB_REQUEST_CORE_DUMP:
  175. dev_err(cdev->dev, "ADSP device coredump received\n");
  176. ipc->ready = false;
  177. catpt_coredump(cdev);
  178. /* TODO: attempt recovery */
  179. break;
  180. case CATPT_GLB_STREAM_MESSAGE:
  181. switch (msg.stream_msg_type) {
  182. case CATPT_STRM_NOTIFICATION:
  183. catpt_dsp_notify_stream(cdev, msg);
  184. break;
  185. default:
  186. catpt_dsp_copy_rx(cdev, header);
  187. /* signal completion of delayed reply */
  188. complete(&ipc->busy_completion);
  189. break;
  190. }
  191. break;
  192. default:
  193. dev_warn(cdev->dev, "unknown response: %d received\n",
  194. msg.global_msg_type);
  195. break;
  196. }
  197. }
  198. irqreturn_t catpt_dsp_irq_thread(int irq, void *dev_id)
  199. {
  200. struct catpt_dev *cdev = dev_id;
  201. u32 ipcd;
  202. ipcd = catpt_readl_shim(cdev, IPCD);
  203. trace_catpt_ipc_notify(ipcd);
  204. /* ensure there is delayed reply or notification to process */
  205. if (!(ipcd & CATPT_IPCD_BUSY))
  206. return IRQ_NONE;
  207. catpt_dsp_process_response(cdev, ipcd);
  208. /* tell DSP processing is completed */
  209. catpt_updatel_shim(cdev, IPCD, CATPT_IPCD_BUSY | CATPT_IPCD_DONE,
  210. CATPT_IPCD_DONE);
  211. /* unmask dsp BUSY interrupt */
  212. catpt_updatel_shim(cdev, IMC, CATPT_IMC_IPCDB, 0);
  213. return IRQ_HANDLED;
  214. }
  215. irqreturn_t catpt_dsp_irq_handler(int irq, void *dev_id)
  216. {
  217. struct catpt_dev *cdev = dev_id;
  218. irqreturn_t ret = IRQ_NONE;
  219. u32 isc, ipcc;
  220. isc = catpt_readl_shim(cdev, ISC);
  221. trace_catpt_irq(isc);
  222. /* immediate reply */
  223. if (isc & CATPT_ISC_IPCCD) {
  224. /* mask host DONE interrupt */
  225. catpt_updatel_shim(cdev, IMC, CATPT_IMC_IPCCD, CATPT_IMC_IPCCD);
  226. ipcc = catpt_readl_shim(cdev, IPCC);
  227. trace_catpt_ipc_reply(ipcc);
  228. catpt_dsp_copy_rx(cdev, ipcc);
  229. complete(&cdev->ipc.done_completion);
  230. /* tell DSP processing is completed */
  231. catpt_updatel_shim(cdev, IPCC, CATPT_IPCC_DONE, 0);
  232. /* unmask host DONE interrupt */
  233. catpt_updatel_shim(cdev, IMC, CATPT_IMC_IPCCD, 0);
  234. ret = IRQ_HANDLED;
  235. }
  236. /* delayed reply or notification */
  237. if (isc & CATPT_ISC_IPCDB) {
  238. /* mask dsp BUSY interrupt */
  239. catpt_updatel_shim(cdev, IMC, CATPT_IMC_IPCDB, CATPT_IMC_IPCDB);
  240. ret = IRQ_WAKE_THREAD;
  241. }
  242. return ret;
  243. }