ipc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. // Author: Liam Girdwood <[email protected]>
  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. spin_lock_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. spin_unlock_irq(&sdev->ipc_lock);
  59. return ret;
  60. }
  61. /* send IPC message from host to DSP */
  62. int sof_ipc_tx_message(struct snd_sof_ipc *ipc, void *msg_data, size_t msg_bytes,
  63. void *reply_data, size_t reply_bytes)
  64. {
  65. if (msg_bytes > ipc->max_payload_size ||
  66. reply_bytes > ipc->max_payload_size)
  67. return -ENOBUFS;
  68. return ipc->ops->tx_msg(ipc->sdev, msg_data, msg_bytes, reply_data,
  69. reply_bytes, false);
  70. }
  71. EXPORT_SYMBOL(sof_ipc_tx_message);
  72. /*
  73. * send IPC message from host to DSP without modifying the DSP state.
  74. * This will be used for IPC's that can be handled by the DSP
  75. * even in a low-power D0 substate.
  76. */
  77. int sof_ipc_tx_message_no_pm(struct snd_sof_ipc *ipc, void *msg_data, size_t msg_bytes,
  78. void *reply_data, size_t reply_bytes)
  79. {
  80. if (msg_bytes > ipc->max_payload_size ||
  81. reply_bytes > ipc->max_payload_size)
  82. return -ENOBUFS;
  83. return ipc->ops->tx_msg(ipc->sdev, msg_data, msg_bytes, reply_data,
  84. reply_bytes, true);
  85. }
  86. EXPORT_SYMBOL(sof_ipc_tx_message_no_pm);
  87. /* Generic helper function to retrieve the reply */
  88. void snd_sof_ipc_get_reply(struct snd_sof_dev *sdev)
  89. {
  90. /*
  91. * Sometimes, there is unexpected reply ipc arriving. The reply
  92. * ipc belongs to none of the ipcs sent from driver.
  93. * In this case, the driver must ignore the ipc.
  94. */
  95. if (!sdev->msg) {
  96. dev_warn(sdev->dev, "unexpected ipc interrupt raised!\n");
  97. return;
  98. }
  99. sdev->msg->reply_error = sdev->ipc->ops->get_reply(sdev);
  100. }
  101. EXPORT_SYMBOL(snd_sof_ipc_get_reply);
  102. /* handle reply message from DSP */
  103. void snd_sof_ipc_reply(struct snd_sof_dev *sdev, u32 msg_id)
  104. {
  105. struct snd_sof_ipc_msg *msg = &sdev->ipc->msg;
  106. if (msg->ipc_complete) {
  107. dev_dbg(sdev->dev,
  108. "no reply expected, received 0x%x, will be ignored",
  109. msg_id);
  110. return;
  111. }
  112. /* wake up and return the error if we have waiters on this message ? */
  113. msg->ipc_complete = true;
  114. wake_up(&msg->waitq);
  115. }
  116. EXPORT_SYMBOL(snd_sof_ipc_reply);
  117. struct snd_sof_ipc *snd_sof_ipc_init(struct snd_sof_dev *sdev)
  118. {
  119. struct snd_sof_ipc *ipc;
  120. struct snd_sof_ipc_msg *msg;
  121. const struct sof_ipc_ops *ops;
  122. ipc = devm_kzalloc(sdev->dev, sizeof(*ipc), GFP_KERNEL);
  123. if (!ipc)
  124. return NULL;
  125. mutex_init(&ipc->tx_mutex);
  126. ipc->sdev = sdev;
  127. msg = &ipc->msg;
  128. /* indicate that we aren't sending a message ATM */
  129. msg->ipc_complete = true;
  130. init_waitqueue_head(&msg->waitq);
  131. switch (sdev->pdata->ipc_type) {
  132. #if defined(CONFIG_SND_SOC_SOF_IPC3)
  133. case SOF_IPC:
  134. ops = &ipc3_ops;
  135. break;
  136. #endif
  137. #if defined(CONFIG_SND_SOC_SOF_INTEL_IPC4)
  138. case SOF_INTEL_IPC4:
  139. ops = &ipc4_ops;
  140. break;
  141. #endif
  142. default:
  143. dev_err(sdev->dev, "Not supported IPC version: %d\n",
  144. sdev->pdata->ipc_type);
  145. return NULL;
  146. }
  147. /* check for mandatory ops */
  148. if (!ops->tx_msg || !ops->rx_msg || !ops->set_get_data || !ops->get_reply) {
  149. dev_err(sdev->dev, "Missing IPC message handling ops\n");
  150. return NULL;
  151. }
  152. if (!ops->fw_loader || !ops->fw_loader->validate ||
  153. !ops->fw_loader->parse_ext_manifest) {
  154. dev_err(sdev->dev, "Missing IPC firmware loading ops\n");
  155. return NULL;
  156. }
  157. if (!ops->pcm) {
  158. dev_err(sdev->dev, "Missing IPC PCM ops\n");
  159. return NULL;
  160. }
  161. if (!ops->tplg || !ops->tplg->widget || !ops->tplg->control) {
  162. dev_err(sdev->dev, "Missing IPC topology ops\n");
  163. return NULL;
  164. }
  165. if (ops->fw_tracing && (!ops->fw_tracing->init || !ops->fw_tracing->suspend ||
  166. !ops->fw_tracing->resume)) {
  167. dev_err(sdev->dev, "Missing firmware tracing ops\n");
  168. return NULL;
  169. }
  170. ipc->ops = ops;
  171. return ipc;
  172. }
  173. EXPORT_SYMBOL(snd_sof_ipc_init);
  174. void snd_sof_ipc_free(struct snd_sof_dev *sdev)
  175. {
  176. struct snd_sof_ipc *ipc = sdev->ipc;
  177. if (!ipc)
  178. return;
  179. /* disable sending of ipc's */
  180. mutex_lock(&ipc->tx_mutex);
  181. ipc->disable_ipc_tx = true;
  182. mutex_unlock(&ipc->tx_mutex);
  183. }
  184. EXPORT_SYMBOL(snd_sof_ipc_free);