1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright(c) 2019-2022 Intel Corporation. All rights reserved.
5 // Author: Cezary Rojewski <cezary.rojewski@intel.com>
7 // Code moved to this file by:
8 // Jyri Sarha <jyri.sarha@intel.com>
11 #include <linux/stddef.h>
12 #include <sound/soc.h>
13 #include <sound/sof/header.h>
14 #include "sof-client.h"
15 #include "sof-client-probes.h"
17 struct sof_probe_dma {
18 unsigned int stream_tag;
19 unsigned int dma_buffer_size;
22 struct sof_ipc_probe_dma_add_params {
23 struct sof_ipc_cmd_hdr hdr;
24 unsigned int num_elems;
25 struct sof_probe_dma dma[];
28 struct sof_ipc_probe_info_params {
29 struct sof_ipc_reply rhdr;
30 unsigned int num_elems;
32 DECLARE_FLEX_ARRAY(struct sof_probe_dma, dma);
33 DECLARE_FLEX_ARRAY(struct sof_probe_point_desc, desc);
37 struct sof_ipc_probe_point_add_params {
38 struct sof_ipc_cmd_hdr hdr;
39 unsigned int num_elems;
40 struct sof_probe_point_desc desc[];
43 struct sof_ipc_probe_point_remove_params {
44 struct sof_ipc_cmd_hdr hdr;
45 unsigned int num_elems;
46 unsigned int buffer_id[];
50 * ipc3_probes_init - initialize data probing
51 * @cdev: SOF client device
52 * @stream_tag: Extractor stream tag
53 * @buffer_size: DMA buffer size to set for extractor
55 * Host chooses whether extraction is supported or not by providing
56 * valid stream tag to DSP. Once specified, stream described by that
57 * tag will be tied to DSP for extraction for the entire lifetime of
60 * Probing is initialized only once and each INIT request must be
61 * matched by DEINIT call.
63 static int ipc3_probes_init(struct sof_client_dev *cdev, u32 stream_tag,
66 struct sof_ipc_probe_dma_add_params *msg;
67 size_t size = struct_size(msg, dma, 1);
70 msg = kmalloc(size, GFP_KERNEL);
74 msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_INIT;
76 msg->dma[0].stream_tag = stream_tag;
77 msg->dma[0].dma_buffer_size = buffer_size;
79 ret = sof_client_ipc_tx_message_no_reply(cdev, msg);
85 * ipc3_probes_deinit - cleanup after data probing
86 * @cdev: SOF client device
88 * Host sends DEINIT request to free previously initialized probe
89 * on DSP side once it is no longer needed. DEINIT only when there
90 * are no probes connected and with all injectors detached.
92 static int ipc3_probes_deinit(struct sof_client_dev *cdev)
94 struct sof_ipc_cmd_hdr msg;
96 msg.size = sizeof(msg);
97 msg.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_DEINIT;
99 return sof_client_ipc_tx_message_no_reply(cdev, &msg);
102 static int ipc3_probes_info(struct sof_client_dev *cdev, unsigned int cmd,
103 void **params, size_t *num_params)
105 size_t max_msg_size = sof_client_get_ipc_max_payload_size(cdev);
106 struct sof_ipc_probe_info_params msg = {{{0}}};
107 struct sof_ipc_probe_info_params *reply;
114 reply = kzalloc(max_msg_size, GFP_KERNEL);
117 msg.rhdr.hdr.size = sizeof(msg);
118 msg.rhdr.hdr.cmd = SOF_IPC_GLB_PROBE | cmd;
120 ret = sof_client_ipc_tx_message(cdev, &msg, reply, max_msg_size);
121 if (ret < 0 || reply->rhdr.error < 0)
124 if (!reply->num_elems)
127 if (cmd == SOF_IPC_PROBE_DMA_INFO)
128 bytes = sizeof(reply->dma[0]);
130 bytes = sizeof(reply->desc[0]);
131 bytes *= reply->num_elems;
132 *params = kmemdup(&reply->dma[0], bytes, GFP_KERNEL);
137 *num_params = reply->num_elems;
145 * ipc3_probes_points_info - retrieve list of active probe points
146 * @cdev: SOF client device
147 * @desc: Returned list of active probes
148 * @num_desc: Returned count of active probes
150 * Host sends PROBE_POINT_INFO request to obtain list of active probe
151 * points, valid for disconnection when given probe is no longer
154 static int ipc3_probes_points_info(struct sof_client_dev *cdev,
155 struct sof_probe_point_desc **desc,
158 return ipc3_probes_info(cdev, SOF_IPC_PROBE_POINT_INFO,
159 (void **)desc, num_desc);
163 * ipc3_probes_points_add - connect specified probes
164 * @cdev: SOF client device
165 * @desc: List of probe points to connect
166 * @num_desc: Number of elements in @desc
168 * Dynamically connects to provided set of endpoints. Immediately
169 * after connection is established, host must be prepared to
170 * transfer data from or to target stream given the probing purpose.
172 * Each probe point should be removed using PROBE_POINT_REMOVE
173 * request when no longer needed.
175 static int ipc3_probes_points_add(struct sof_client_dev *cdev,
176 struct sof_probe_point_desc *desc,
179 struct sof_ipc_probe_point_add_params *msg;
180 size_t size = struct_size(msg, desc, num_desc);
183 msg = kmalloc(size, GFP_KERNEL);
186 msg->hdr.size = size;
187 msg->num_elems = num_desc;
188 msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_POINT_ADD;
189 memcpy(&msg->desc[0], desc, size - sizeof(*msg));
191 ret = sof_client_ipc_tx_message_no_reply(cdev, msg);
197 * ipc3_probes_points_remove - disconnect specified probes
198 * @cdev: SOF client device
199 * @buffer_id: List of probe points to disconnect
200 * @num_buffer_id: Number of elements in @desc
202 * Removes previously connected probes from list of active probe
203 * points and frees all resources on DSP side.
205 static int ipc3_probes_points_remove(struct sof_client_dev *cdev,
206 unsigned int *buffer_id,
207 size_t num_buffer_id)
209 struct sof_ipc_probe_point_remove_params *msg;
210 size_t size = struct_size(msg, buffer_id, num_buffer_id);
213 msg = kmalloc(size, GFP_KERNEL);
216 msg->hdr.size = size;
217 msg->num_elems = num_buffer_id;
218 msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_POINT_REMOVE;
219 memcpy(&msg->buffer_id[0], buffer_id, size - sizeof(*msg));
221 ret = sof_client_ipc_tx_message_no_reply(cdev, msg);
226 const struct sof_probes_ipc_ops ipc3_probe_ops = {
227 .init = ipc3_probes_init,
228 .deinit = ipc3_probes_deinit,
229 .points_info = ipc3_probes_points_info,
230 .points_add = ipc3_probes_points_add,
231 .points_remove = ipc3_probes_points_remove,