sof-client-probes-ipc3.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // Copyright(c) 2019-2022 Intel Corporation
  4. //
  5. // Author: Cezary Rojewski <cezary.rojewski@intel.com>
  6. //
  7. // Code moved to this file by:
  8. // Jyri Sarha <jyri.sarha@intel.com>
  9. //
  10. #include <linux/stddef.h>
  11. #include <sound/soc.h>
  12. #include <sound/sof/header.h>
  13. #include "sof-client.h"
  14. #include "sof-client-probes.h"
  15. struct sof_probe_dma {
  16. unsigned int stream_tag;
  17. unsigned int dma_buffer_size;
  18. } __packed;
  19. struct sof_ipc_probe_dma_add_params {
  20. struct sof_ipc_cmd_hdr hdr;
  21. unsigned int num_elems;
  22. struct sof_probe_dma dma[];
  23. } __packed;
  24. struct sof_ipc_probe_info_params {
  25. struct sof_ipc_reply rhdr;
  26. unsigned int num_elems;
  27. union {
  28. DECLARE_FLEX_ARRAY(struct sof_probe_dma, dma);
  29. DECLARE_FLEX_ARRAY(struct sof_probe_point_desc, desc);
  30. };
  31. } __packed;
  32. struct sof_ipc_probe_point_add_params {
  33. struct sof_ipc_cmd_hdr hdr;
  34. unsigned int num_elems;
  35. struct sof_probe_point_desc desc[];
  36. } __packed;
  37. struct sof_ipc_probe_point_remove_params {
  38. struct sof_ipc_cmd_hdr hdr;
  39. unsigned int num_elems;
  40. unsigned int buffer_id[];
  41. } __packed;
  42. /**
  43. * ipc3_probes_init - initialize data probing
  44. * @cdev: SOF client device
  45. * @stream_tag: Extractor stream tag
  46. * @buffer_size: DMA buffer size to set for extractor
  47. *
  48. * Host chooses whether extraction is supported or not by providing
  49. * valid stream tag to DSP. Once specified, stream described by that
  50. * tag will be tied to DSP for extraction for the entire lifetime of
  51. * probe.
  52. *
  53. * Probing is initialized only once and each INIT request must be
  54. * matched by DEINIT call.
  55. */
  56. static int ipc3_probes_init(struct sof_client_dev *cdev, u32 stream_tag,
  57. size_t buffer_size)
  58. {
  59. struct sof_ipc_probe_dma_add_params *msg;
  60. size_t size = struct_size(msg, dma, 1);
  61. int ret;
  62. msg = kmalloc(size, GFP_KERNEL);
  63. if (!msg)
  64. return -ENOMEM;
  65. msg->hdr.size = size;
  66. msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_INIT;
  67. msg->num_elems = 1;
  68. msg->dma[0].stream_tag = stream_tag;
  69. msg->dma[0].dma_buffer_size = buffer_size;
  70. ret = sof_client_ipc_tx_message_no_reply(cdev, msg);
  71. kfree(msg);
  72. return ret;
  73. }
  74. /**
  75. * ipc3_probes_deinit - cleanup after data probing
  76. * @cdev: SOF client device
  77. *
  78. * Host sends DEINIT request to free previously initialized probe
  79. * on DSP side once it is no longer needed. DEINIT only when there
  80. * are no probes connected and with all injectors detached.
  81. */
  82. static int ipc3_probes_deinit(struct sof_client_dev *cdev)
  83. {
  84. struct sof_ipc_cmd_hdr msg;
  85. msg.size = sizeof(msg);
  86. msg.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_DEINIT;
  87. return sof_client_ipc_tx_message_no_reply(cdev, &msg);
  88. }
  89. static int ipc3_probes_info(struct sof_client_dev *cdev, unsigned int cmd,
  90. void **params, size_t *num_params)
  91. {
  92. size_t max_msg_size = sof_client_get_ipc_max_payload_size(cdev);
  93. struct sof_ipc_probe_info_params msg = {{{0}}};
  94. struct sof_ipc_probe_info_params *reply;
  95. size_t bytes;
  96. int ret;
  97. *params = NULL;
  98. *num_params = 0;
  99. reply = kzalloc(max_msg_size, GFP_KERNEL);
  100. if (!reply)
  101. return -ENOMEM;
  102. msg.rhdr.hdr.size = sizeof(msg);
  103. msg.rhdr.hdr.cmd = SOF_IPC_GLB_PROBE | cmd;
  104. ret = sof_client_ipc_tx_message(cdev, &msg, reply, max_msg_size);
  105. if (ret < 0 || reply->rhdr.error < 0)
  106. goto exit;
  107. if (!reply->num_elems)
  108. goto exit;
  109. if (cmd == SOF_IPC_PROBE_DMA_INFO)
  110. bytes = sizeof(reply->dma[0]);
  111. else
  112. bytes = sizeof(reply->desc[0]);
  113. bytes *= reply->num_elems;
  114. *params = kmemdup(&reply->dma[0], bytes, GFP_KERNEL);
  115. if (!*params) {
  116. ret = -ENOMEM;
  117. goto exit;
  118. }
  119. *num_params = reply->num_elems;
  120. exit:
  121. kfree(reply);
  122. return ret;
  123. }
  124. /**
  125. * ipc3_probes_points_info - retrieve list of active probe points
  126. * @cdev: SOF client device
  127. * @desc: Returned list of active probes
  128. * @num_desc: Returned count of active probes
  129. *
  130. * Host sends PROBE_POINT_INFO request to obtain list of active probe
  131. * points, valid for disconnection when given probe is no longer
  132. * required.
  133. */
  134. static int ipc3_probes_points_info(struct sof_client_dev *cdev,
  135. struct sof_probe_point_desc **desc,
  136. size_t *num_desc)
  137. {
  138. return ipc3_probes_info(cdev, SOF_IPC_PROBE_POINT_INFO,
  139. (void **)desc, num_desc);
  140. }
  141. /**
  142. * ipc3_probes_points_add - connect specified probes
  143. * @cdev: SOF client device
  144. * @desc: List of probe points to connect
  145. * @num_desc: Number of elements in @desc
  146. *
  147. * Dynamically connects to provided set of endpoints. Immediately
  148. * after connection is established, host must be prepared to
  149. * transfer data from or to target stream given the probing purpose.
  150. *
  151. * Each probe point should be removed using PROBE_POINT_REMOVE
  152. * request when no longer needed.
  153. */
  154. static int ipc3_probes_points_add(struct sof_client_dev *cdev,
  155. struct sof_probe_point_desc *desc,
  156. size_t num_desc)
  157. {
  158. struct sof_ipc_probe_point_add_params *msg;
  159. size_t size = struct_size(msg, desc, num_desc);
  160. int ret;
  161. msg = kmalloc(size, GFP_KERNEL);
  162. if (!msg)
  163. return -ENOMEM;
  164. msg->hdr.size = size;
  165. msg->num_elems = num_desc;
  166. msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_POINT_ADD;
  167. memcpy(&msg->desc[0], desc, size - sizeof(*msg));
  168. ret = sof_client_ipc_tx_message_no_reply(cdev, msg);
  169. kfree(msg);
  170. return ret;
  171. }
  172. /**
  173. * ipc3_probes_points_remove - disconnect specified probes
  174. * @cdev: SOF client device
  175. * @buffer_id: List of probe points to disconnect
  176. * @num_buffer_id: Number of elements in @desc
  177. *
  178. * Removes previously connected probes from list of active probe
  179. * points and frees all resources on DSP side.
  180. */
  181. static int ipc3_probes_points_remove(struct sof_client_dev *cdev,
  182. unsigned int *buffer_id,
  183. size_t num_buffer_id)
  184. {
  185. struct sof_ipc_probe_point_remove_params *msg;
  186. size_t size = struct_size(msg, buffer_id, num_buffer_id);
  187. int ret;
  188. msg = kmalloc(size, GFP_KERNEL);
  189. if (!msg)
  190. return -ENOMEM;
  191. msg->hdr.size = size;
  192. msg->num_elems = num_buffer_id;
  193. msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_POINT_REMOVE;
  194. memcpy(&msg->buffer_id[0], buffer_id, size - sizeof(*msg));
  195. ret = sof_client_ipc_tx_message_no_reply(cdev, msg);
  196. kfree(msg);
  197. return ret;
  198. }
  199. const struct sof_probes_ipc_ops ipc3_probe_ops = {
  200. .init = ipc3_probes_init,
  201. .deinit = ipc3_probes_deinit,
  202. .points_info = ipc3_probes_points_info,
  203. .points_add = ipc3_probes_points_add,
  204. .points_remove = ipc3_probes_points_remove,
  205. };