1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2019-2022 Intel Corporation
4 //
5 // Author: Jyri Sarha <jyri.sarha@intel.com>
6 //
7
8 #include <sound/soc.h>
9 #include <sound/sof/ipc4/header.h>
10 #include <uapi/sound/sof/header.h>
11 #include "sof-audio.h"
12 #include "ipc4-priv.h"
13 #include "sof-client.h"
14 #include "sof-client-probes.h"
15
16 enum sof_ipc4_dma_type {
17 SOF_IPC4_DMA_HDA_HOST_OUTPUT = 0,
18 SOF_IPC4_DMA_HDA_HOST_INPUT = 1,
19 SOF_IPC4_DMA_HDA_LINK_OUTPUT = 8,
20 SOF_IPC4_DMA_HDA_LINK_INPUT = 9,
21 SOF_IPC4_DMA_DMIC_LINK_INPUT = 11,
22 SOF_IPC4_DMA_I2S_LINK_OUTPUT = 12,
23 SOF_IPC4_DMA_I2S_LINK_INPUT = 13,
24 };
25
26 enum sof_ipc4_probe_runtime_param {
27 SOF_IPC4_PROBE_INJECTION_DMA = 1,
28 SOF_IPC4_PROBE_INJECTION_DMA_DETACH,
29 SOF_IPC4_PROBE_POINTS,
30 SOF_IPC4_PROBE_POINTS_DISCONNECT,
31 SOF_IPC4_PROBE_POINTS_AVAILABLE,
32 };
33
34 struct sof_ipc4_probe_gtw_cfg {
35 u32 node_id;
36 u32 dma_buffer_size;
37 } __packed __aligned(4);
38
39 #define SOF_IPC4_PROBE_NODE_ID_INDEX(x) ((x) & GENMASK(7, 0))
40 #define SOF_IPC4_PROBE_NODE_ID_TYPE(x) (((x) << 8) & GENMASK(12, 8))
41
42 struct sof_ipc4_probe_cfg {
43 struct sof_ipc4_base_module_cfg base;
44 struct sof_ipc4_probe_gtw_cfg gtw_cfg;
45 } __packed __aligned(4);
46
47 enum sof_ipc4_probe_type {
48 SOF_IPC4_PROBE_TYPE_INPUT = 0,
49 SOF_IPC4_PROBE_TYPE_OUTPUT,
50 SOF_IPC4_PROBE_TYPE_INTERNAL
51 };
52
53 #define SOF_IPC4_PROBE_TYPE_SHIFT 24
54 #define SOF_IPC4_PROBE_TYPE_MASK GENMASK(25, 24)
55 #define SOF_IPC4_PROBE_TYPE_GET(x) (((x) & SOF_IPC4_PROBE_TYPE_MASK) \
56 >> SOF_IPC4_PROBE_TYPE_SHIFT)
57 #define SOF_IPC4_PROBE_IDX_SHIFT 26
58 #define SOF_IPC4_PROBE_IDX_MASK GENMASK(31, 26)
59 #define SOF_IPC4_PROBE_IDX_GET(x) (((x) & SOF_IPC4_PROBE_IDX_MASK) \
60 >> SOF_IPC4_PROBE_IDX_SHIFT)
61
62 struct sof_ipc4_probe_point {
63 u32 point_id;
64 u32 purpose;
65 u32 stream_tag;
66 } __packed __aligned(4);
67
68 struct sof_ipc4_probe_info {
69 unsigned int num_elems;
70 DECLARE_FLEX_ARRAY(struct sof_ipc4_probe_point, points);
71 } __packed;
72
73 #define INVALID_PIPELINE_ID 0xFF
74
sof_probe_ipc4_type_string(u32 type)75 static const char *sof_probe_ipc4_type_string(u32 type)
76 {
77 switch (type) {
78 case SOF_IPC4_PROBE_TYPE_INPUT:
79 return "input";
80 case SOF_IPC4_PROBE_TYPE_OUTPUT:
81 return "output";
82 case SOF_IPC4_PROBE_TYPE_INTERNAL:
83 return "internal";
84 default:
85 return "UNKNOWN";
86 }
87 }
88
89 /**
90 * sof_ipc4_probe_get_module_info - Get IPC4 module info for probe module
91 * @cdev: SOF client device
92 * @return: Pointer to IPC4 probe module info
93 *
94 * Look up the IPC4 probe module info based on the hard coded uuid and
95 * store the value for the future calls.
96 */
sof_ipc4_probe_get_module_info(struct sof_client_dev * cdev)97 static struct sof_man4_module *sof_ipc4_probe_get_module_info(struct sof_client_dev *cdev)
98 {
99 struct sof_probes_priv *priv = cdev->data;
100 struct device *dev = &cdev->auxdev.dev;
101 static const guid_t probe_uuid =
102 GUID_INIT(0x7CAD0808, 0xAB10, 0xCD23,
103 0xEF, 0x45, 0x12, 0xAB, 0x34, 0xCD, 0x56, 0xEF);
104
105 if (!priv->ipc_priv) {
106 struct sof_ipc4_fw_module *fw_module =
107 sof_client_ipc4_find_module(cdev, &probe_uuid);
108
109 if (!fw_module) {
110 dev_err(dev, "%s: no matching uuid found", __func__);
111 return NULL;
112 }
113
114 priv->ipc_priv = &fw_module->man4_module_entry;
115 }
116
117 return (struct sof_man4_module *)priv->ipc_priv;
118 }
119
120 /**
121 * ipc4_probes_init - initialize data probing
122 * @cdev: SOF client device
123 * @stream_tag: Extractor stream tag
124 * @buffer_size: DMA buffer size to set for extractor
125 * @return: 0 on success, negative error code on error
126 *
127 * Host chooses whether extraction is supported or not by providing
128 * valid stream tag to DSP. Once specified, stream described by that
129 * tag will be tied to DSP for extraction for the entire lifetime of
130 * probe.
131 *
132 * Probing is initialized only once and each INIT request must be
133 * matched by DEINIT call.
134 */
ipc4_probes_init(struct sof_client_dev * cdev,u32 stream_tag,size_t buffer_size)135 static int ipc4_probes_init(struct sof_client_dev *cdev, u32 stream_tag,
136 size_t buffer_size)
137 {
138 struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev);
139 struct sof_ipc4_msg msg;
140 struct sof_ipc4_probe_cfg cfg;
141
142 if (!mentry)
143 return -ENODEV;
144
145 memset(&cfg, '\0', sizeof(cfg));
146 cfg.gtw_cfg.node_id = SOF_IPC4_PROBE_NODE_ID_INDEX(stream_tag - 1) |
147 SOF_IPC4_PROBE_NODE_ID_TYPE(SOF_IPC4_DMA_HDA_HOST_INPUT);
148
149 cfg.gtw_cfg.dma_buffer_size = buffer_size;
150
151 msg.primary = mentry->id;
152 msg.primary |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_INIT_INSTANCE);
153 msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
154 msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
155 msg.extension = SOF_IPC4_MOD_EXT_DST_MOD_INSTANCE(INVALID_PIPELINE_ID);
156 msg.extension |= SOF_IPC4_MOD_EXT_CORE_ID(0);
157 msg.extension |= SOF_IPC4_MOD_EXT_PARAM_SIZE(sizeof(cfg) / sizeof(uint32_t));
158
159 msg.data_size = sizeof(cfg);
160 msg.data_ptr = &cfg;
161
162 return sof_client_ipc_tx_message_no_reply(cdev, &msg);
163 }
164
165 /**
166 * ipc4_probes_deinit - cleanup after data probing
167 * @cdev: SOF client device
168 * @return: 0 on success, negative error code on error
169 *
170 * Host sends DEINIT request to free previously initialized probe
171 * on DSP side once it is no longer needed. DEINIT only when there
172 * are no probes connected and with all injectors detached.
173 */
ipc4_probes_deinit(struct sof_client_dev * cdev)174 static int ipc4_probes_deinit(struct sof_client_dev *cdev)
175 {
176 struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev);
177 struct sof_ipc4_msg msg;
178
179 if (!mentry)
180 return -ENODEV;
181
182 msg.primary = mentry->id;
183 msg.primary |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_DELETE_INSTANCE);
184 msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
185 msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
186 msg.extension = SOF_IPC4_MOD_EXT_DST_MOD_INSTANCE(INVALID_PIPELINE_ID);
187 msg.extension |= SOF_IPC4_MOD_EXT_CORE_ID(0);
188
189 msg.data_size = 0;
190 msg.data_ptr = NULL;
191
192 return sof_client_ipc_tx_message_no_reply(cdev, &msg);
193 }
194
195 /**
196 * ipc4_probes_points_info - retrieve list of probe points
197 * @cdev: SOF client device
198 * @desc: Returned list of active probes
199 * @num_desc: Returned count of active probes
200 * @type: Either PROBES_INFO_ACTIVE_PROBES or PROBES_INFO_AVAILABE_PROBES
201 * @return: 0 on success, negative error code on error
202 *
203 * Returns list if active probe points if type is
204 * PROBES_INFO_ACTIVE_PROBES, or list of all available probe points if
205 * type is PROBES_INFO_AVAILABE_PROBES.
206 */
ipc4_probes_points_info(struct sof_client_dev * cdev,struct sof_probe_point_desc ** desc,size_t * num_desc,enum sof_probe_info_type type)207 static int ipc4_probes_points_info(struct sof_client_dev *cdev,
208 struct sof_probe_point_desc **desc,
209 size_t *num_desc,
210 enum sof_probe_info_type type)
211 {
212 struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev);
213 struct device *dev = &cdev->auxdev.dev;
214 struct sof_ipc4_probe_info *info;
215 struct sof_ipc4_msg msg;
216 u32 param_id;
217 int i, ret;
218
219 if (!mentry)
220 return -ENODEV;
221
222 switch (type) {
223 case PROBES_INFO_ACTIVE_PROBES:
224 param_id = SOF_IPC4_PROBE_POINTS;
225 break;
226 case PROBES_INFO_AVAILABE_PROBES:
227 param_id = SOF_IPC4_PROBE_POINTS_AVAILABLE;
228 break;
229 default:
230 dev_err(dev, "%s: info type %u not supported", __func__, type);
231 return -EOPNOTSUPP;
232 }
233
234 msg.primary = mentry->id;
235 msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
236 msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
237
238 msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(param_id);
239
240 msg.data_size = sof_client_get_ipc_max_payload_size(cdev);
241 msg.data_ptr = kzalloc(msg.data_size, GFP_KERNEL);
242 if (!msg.data_ptr)
243 return -ENOMEM;
244
245 ret = sof_client_ipc_set_get_data(cdev, &msg, false);
246 if (ret) {
247 kfree(msg.data_ptr);
248 return ret;
249 }
250 info = msg.data_ptr;
251 if (msg.data_size < sizeof(*info) ||
252 info->num_elems > (msg.data_size - sizeof(*info)) /
253 sizeof(info->points[0])) {
254 dev_err(dev, "%s: invalid probe info element count %u\n",
255 __func__, info->num_elems);
256 kfree(msg.data_ptr);
257 return -EINVAL;
258 }
259
260 *num_desc = info->num_elems;
261 dev_dbg(dev, "%s: got %zu probe points", __func__, *num_desc);
262
263 *desc = kcalloc(*num_desc, sizeof(**desc), GFP_KERNEL);
264 if (!*desc) {
265 kfree(msg.data_ptr);
266 return -ENOMEM;
267 }
268
269 for (i = 0; i < *num_desc; i++) {
270 (*desc)[i].buffer_id = info->points[i].point_id;
271 (*desc)[i].purpose = info->points[i].purpose;
272 (*desc)[i].stream_tag = info->points[i].stream_tag;
273 }
274 kfree(msg.data_ptr);
275
276 return 0;
277 }
278
279 /**
280 * ipc4_probes_point_print - Human readable print of probe point descriptor
281 * @cdev: SOF client device
282 * @buf: Buffer to print to
283 * @size: Available bytes in buffer
284 * @desc: Describes the probe point to print
285 * @return: Number of bytes printed or an error code (snprintf return value)
286 */
ipc4_probes_point_print(struct sof_client_dev * cdev,char * buf,size_t size,struct sof_probe_point_desc * desc)287 static int ipc4_probes_point_print(struct sof_client_dev *cdev, char *buf, size_t size,
288 struct sof_probe_point_desc *desc)
289 {
290 struct device *dev = &cdev->auxdev.dev;
291 struct snd_sof_widget *swidget;
292 int ret;
293
294 swidget = sof_client_ipc4_find_swidget_by_id(cdev, SOF_IPC4_MOD_ID_GET(desc->buffer_id),
295 SOF_IPC4_MOD_INSTANCE_GET(desc->buffer_id));
296 if (!swidget)
297 dev_err(dev, "%s: Failed to find widget for module %lu.%lu\n",
298 __func__, SOF_IPC4_MOD_ID_GET(desc->buffer_id),
299 SOF_IPC4_MOD_INSTANCE_GET(desc->buffer_id));
300
301 ret = scnprintf(buf, size, "%#x,%#x,%#x\t%s %s buf idx %lu %s\n",
302 desc->buffer_id, desc->purpose, desc->stream_tag,
303 swidget ? swidget->widget->name : "<unknown>",
304 sof_probe_ipc4_type_string(SOF_IPC4_PROBE_TYPE_GET(desc->buffer_id)),
305 SOF_IPC4_PROBE_IDX_GET(desc->buffer_id),
306 desc->stream_tag ? "(connected)" : "");
307
308 return ret;
309 }
310
311 /**
312 * ipc4_probes_points_add - connect specified probes
313 * @cdev: SOF client device
314 * @desc: List of probe points to connect
315 * @num_desc: Number of elements in @desc
316 * @return: 0 on success, negative error code on error
317 *
318 * Translates the generic probe point presentation to an IPC4
319 * message to dynamically connect the provided set of endpoints.
320 */
ipc4_probes_points_add(struct sof_client_dev * cdev,struct sof_probe_point_desc * desc,size_t num_desc)321 static int ipc4_probes_points_add(struct sof_client_dev *cdev,
322 struct sof_probe_point_desc *desc,
323 size_t num_desc)
324 {
325 struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev);
326 struct sof_ipc4_probe_point *points;
327 struct sof_ipc4_msg msg;
328 int i, ret;
329
330 if (!mentry)
331 return -EOPNOTSUPP;
332
333 /* The sof_probe_point_desc and sof_ipc4_probe_point structs
334 * are of same size and even the integers are the same in the
335 * same order, and similar meaning, but since there is no
336 * performance issue I wrote the conversion explicitly open for
337 * future development.
338 */
339 points = kzalloc_objs(*points, num_desc);
340 if (!points)
341 return -ENOMEM;
342
343 for (i = 0; i < num_desc; i++) {
344 points[i].point_id = desc[i].buffer_id;
345 points[i].purpose = desc[i].purpose;
346 points[i].stream_tag = desc[i].stream_tag;
347 }
348
349 msg.primary = mentry->id;
350 msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
351 msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
352
353 msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_PROBE_POINTS);
354
355 msg.data_size = sizeof(*points) * num_desc;
356 msg.data_ptr = points;
357
358 ret = sof_client_ipc_set_get_data(cdev, &msg, true);
359
360 kfree(points);
361
362 return ret;
363 }
364
365 /**
366 * ipc4_probes_points_remove - disconnect specified probes
367 * @cdev: SOF client device
368 * @buffer_id: List of probe points to disconnect
369 * @num_buffer_id: Number of elements in @desc
370 * @return: 0 on success, negative error code on error
371 *
372 * Converts the generic buffer_id to IPC4 probe_point_id and remove
373 * the probe points with an IPC4 for message.
374 */
ipc4_probes_points_remove(struct sof_client_dev * cdev,unsigned int * buffer_id,size_t num_buffer_id)375 static int ipc4_probes_points_remove(struct sof_client_dev *cdev,
376 unsigned int *buffer_id, size_t num_buffer_id)
377 {
378 struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev);
379 struct sof_ipc4_msg msg;
380 u32 *probe_point_ids;
381 int i, ret;
382
383 if (!mentry)
384 return -ENODEV;
385
386 probe_point_ids = kcalloc(num_buffer_id, sizeof(*probe_point_ids),
387 GFP_KERNEL);
388 if (!probe_point_ids)
389 return -ENOMEM;
390
391 for (i = 0; i < num_buffer_id; i++)
392 probe_point_ids[i] = buffer_id[i];
393
394 msg.primary = mentry->id;
395 msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
396 msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
397
398 msg.extension =
399 SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_PROBE_POINTS_DISCONNECT);
400
401 msg.data_size = num_buffer_id * sizeof(*probe_point_ids);
402 msg.data_ptr = probe_point_ids;
403
404 ret = sof_client_ipc_set_get_data(cdev, &msg, true);
405
406 kfree(probe_point_ids);
407
408 return ret;
409 }
410
411 const struct sof_probes_ipc_ops ipc4_probe_ops = {
412 .init = ipc4_probes_init,
413 .deinit = ipc4_probes_deinit,
414 .points_info = ipc4_probes_points_info,
415 .point_print = ipc4_probes_point_print,
416 .points_add = ipc4_probes_points_add,
417 .points_remove = ipc4_probes_points_remove,
418 };
419