ipc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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
  7. //
  8. // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
  9. //
  10. // Generic IPC layer that can work over MMIO and SPI/I2C. PHY layer provided
  11. // by platform driver code.
  12. //
  13. #include <linux/mutex.h>
  14. #include <linux/types.h>
  15. #include "sof-priv.h"
  16. #include "sof-audio.h"
  17. #include "ops.h"
  18. /**
  19. * sof_ipc_send_msg - generic function to prepare and send one IPC message
  20. * @sdev: pointer to SOF core device struct
  21. * @msg_data: pointer to a message to send
  22. * @msg_bytes: number of bytes in the message
  23. * @reply_bytes: number of bytes available for the reply.
  24. * The buffer for the reply data is not passed to this
  25. * function, the available size is an information for the
  26. * reply handling functions.
  27. *
  28. * On success the function returns 0, otherwise negative error number.
  29. *
  30. * Note: higher level sdev->ipc->tx_mutex must be held to make sure that
  31. * transfers are synchronized.
  32. */
  33. int sof_ipc_send_msg(struct snd_sof_dev *sdev, void *msg_data, size_t msg_bytes,
  34. size_t reply_bytes)
  35. {
  36. struct snd_sof_ipc *ipc = sdev->ipc;
  37. struct snd_sof_ipc_msg *msg;
  38. int ret;
  39. if (ipc->disable_ipc_tx || sdev->fw_state != SOF_FW_BOOT_COMPLETE)
  40. return -ENODEV;
  41. /*
  42. * The spin-lock is needed to protect message objects against other
  43. * atomic contexts.
  44. */
  45. guard(spinlock_irq)(&sdev->ipc_lock);
  46. /* initialise the message */
  47. msg = &ipc->msg;
  48. /* attach message data */
  49. msg->msg_data = msg_data;
  50. msg->msg_size = msg_bytes;
  51. msg->reply_size = reply_bytes;
  52. msg->reply_error = 0;
  53. sdev->msg = msg;
  54. ret = snd_sof_dsp_send_msg(sdev, msg);
  55. /* Next reply that we receive will be related to this message */
  56. if (!ret)
  57. msg->ipc_complete = false;
  58. return ret;
  59. }
  60. /* send IPC message from host to DSP */
  61. int sof_ipc_tx_message(struct snd_sof_ipc *ipc, void *msg_data, size_t msg_bytes,
  62. void *reply_data, size_t reply_bytes)
  63. {
  64. if (msg_bytes > ipc->max_payload_size ||
  65. reply_bytes > ipc->max_payload_size)
  66. return -ENOBUFS;
  67. return ipc->ops->tx_msg(ipc->sdev, msg_data, msg_bytes, reply_data,
  68. reply_bytes, false);
  69. }
  70. EXPORT_SYMBOL(sof_ipc_tx_message);
  71. /* IPC set or get data from host to DSP */
  72. int sof_ipc_set_get_data(struct snd_sof_ipc *ipc, void *msg_data,
  73. size_t msg_bytes, bool set)
  74. {
  75. return ipc->ops->set_get_data(ipc->sdev, msg_data, msg_bytes, set);
  76. }
  77. EXPORT_SYMBOL(sof_ipc_set_get_data);
  78. /*
  79. * send IPC message from host to DSP without modifying the DSP state.
  80. * This will be used for IPC's that can be handled by the DSP
  81. * even in a low-power D0 substate.
  82. */
  83. int sof_ipc_tx_message_no_pm(struct snd_sof_ipc *ipc, void *msg_data, size_t msg_bytes,
  84. void *reply_data, size_t reply_bytes)
  85. {
  86. if (msg_bytes > ipc->max_payload_size ||
  87. reply_bytes > ipc->max_payload_size)
  88. return -ENOBUFS;
  89. return ipc->ops->tx_msg(ipc->sdev, msg_data, msg_bytes, reply_data,
  90. reply_bytes, true);
  91. }
  92. EXPORT_SYMBOL(sof_ipc_tx_message_no_pm);
  93. /* Generic helper function to retrieve the reply */
  94. void snd_sof_ipc_get_reply(struct snd_sof_dev *sdev)
  95. {
  96. /*
  97. * Sometimes, there is unexpected reply ipc arriving. The reply
  98. * ipc belongs to none of the ipcs sent from driver.
  99. * In this case, the driver must ignore the ipc.
  100. */
  101. if (!sdev->msg) {
  102. dev_warn(sdev->dev, "unexpected ipc interrupt raised!\n");
  103. return;
  104. }
  105. sdev->msg->reply_error = sdev->ipc->ops->get_reply(sdev);
  106. }
  107. EXPORT_SYMBOL(snd_sof_ipc_get_reply);
  108. /* handle reply message from DSP */
  109. void snd_sof_ipc_reply(struct snd_sof_dev *sdev, u32 msg_id)
  110. {
  111. struct snd_sof_ipc_msg *msg = &sdev->ipc->msg;
  112. if (msg->ipc_complete) {
  113. dev_dbg(sdev->dev,
  114. "no reply expected, received 0x%x, will be ignored",
  115. msg_id);
  116. return;
  117. }
  118. /* wake up and return the error if we have waiters on this message ? */
  119. msg->ipc_complete = true;
  120. wake_up(&msg->waitq);
  121. }
  122. EXPORT_SYMBOL(snd_sof_ipc_reply);
  123. struct snd_sof_ipc *snd_sof_ipc_init(struct snd_sof_dev *sdev)
  124. {
  125. struct snd_sof_ipc *ipc;
  126. struct snd_sof_ipc_msg *msg;
  127. const struct sof_ipc_ops *ops;
  128. ipc = devm_kzalloc(sdev->dev, sizeof(*ipc), GFP_KERNEL);
  129. if (!ipc)
  130. return NULL;
  131. mutex_init(&ipc->tx_mutex);
  132. ipc->sdev = sdev;
  133. msg = &ipc->msg;
  134. /* indicate that we aren't sending a message ATM */
  135. msg->ipc_complete = true;
  136. init_waitqueue_head(&msg->waitq);
  137. switch (sdev->pdata->ipc_type) {
  138. #if defined(CONFIG_SND_SOC_SOF_IPC3)
  139. case SOF_IPC_TYPE_3:
  140. ops = &ipc3_ops;
  141. break;
  142. #endif
  143. #if defined(CONFIG_SND_SOC_SOF_IPC4)
  144. case SOF_IPC_TYPE_4:
  145. ops = &ipc4_ops;
  146. break;
  147. #endif
  148. default:
  149. dev_err(sdev->dev, "Not supported IPC version: %d\n",
  150. sdev->pdata->ipc_type);
  151. return NULL;
  152. }
  153. /* check for mandatory ops */
  154. if (!ops->tx_msg || !ops->rx_msg || !ops->set_get_data || !ops->get_reply) {
  155. dev_err(sdev->dev, "Missing IPC message handling ops\n");
  156. return NULL;
  157. }
  158. if (!ops->fw_loader || !ops->fw_loader->validate ||
  159. !ops->fw_loader->parse_ext_manifest) {
  160. dev_err(sdev->dev, "Missing IPC firmware loading ops\n");
  161. return NULL;
  162. }
  163. if (!ops->pcm) {
  164. dev_err(sdev->dev, "Missing IPC PCM ops\n");
  165. return NULL;
  166. }
  167. if (!ops->tplg || !ops->tplg->widget || !ops->tplg->control) {
  168. dev_err(sdev->dev, "Missing IPC topology ops\n");
  169. return NULL;
  170. }
  171. if (ops->fw_tracing && (!ops->fw_tracing->init || !ops->fw_tracing->suspend ||
  172. !ops->fw_tracing->resume)) {
  173. dev_err(sdev->dev, "Missing firmware tracing ops\n");
  174. return NULL;
  175. }
  176. if (ops->init && ops->init(sdev))
  177. return NULL;
  178. ipc->ops = ops;
  179. return ipc;
  180. }
  181. EXPORT_SYMBOL(snd_sof_ipc_init);
  182. void snd_sof_ipc_free(struct snd_sof_dev *sdev)
  183. {
  184. struct snd_sof_ipc *ipc = sdev->ipc;
  185. if (!ipc)
  186. return;
  187. /* disable sending of ipc's */
  188. scoped_guard(mutex, &ipc->tx_mutex)
  189. ipc->disable_ipc_tx = true;
  190. if (ipc->ops->exit)
  191. ipc->ops->exit(sdev);
  192. }
  193. EXPORT_SYMBOL(snd_sof_ipc_free);