xref: /linux/sound/soc/sof/sof-client-probes-ipc4.c (revision 782d4613171e271b1e28ee1db9616beb8e6ad8a1)
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 "ipc4-priv.h"
12 #include "sof-client.h"
13 #include "sof-client-probes.h"
14 
15 enum sof_ipc4_dma_type {
16 	SOF_IPC4_DMA_HDA_HOST_OUTPUT = 0,
17 	SOF_IPC4_DMA_HDA_HOST_INPUT = 1,
18 	SOF_IPC4_DMA_HDA_LINK_OUTPUT = 8,
19 	SOF_IPC4_DMA_HDA_LINK_INPUT = 9,
20 	SOF_IPC4_DMA_DMIC_LINK_INPUT = 11,
21 	SOF_IPC4_DMA_I2S_LINK_OUTPUT = 12,
22 	SOF_IPC4_DMA_I2S_LINK_INPUT = 13,
23 };
24 
25 enum sof_ipc4_probe_runtime_param {
26 	SOF_IPC4_PROBE_INJECTION_DMA = 1,
27 	SOF_IPC4_PROBE_INJECTION_DMA_DETACH,
28 	SOF_IPC4_PROBE_POINTS,
29 	SOF_IPC4_PROBE_POINTS_DISCONNECT,
30 };
31 
32 struct sof_ipc4_probe_gtw_cfg {
33 	u32 node_id;
34 	u32 dma_buffer_size;
35 } __packed __aligned(4);
36 
37 #define SOF_IPC4_PROBE_NODE_ID_INDEX(x)		((x) & GENMASK(7, 0))
38 #define SOF_IPC4_PROBE_NODE_ID_TYPE(x)		(((x) << 8) & GENMASK(12, 8))
39 
40 struct sof_ipc4_probe_cfg {
41 	struct sof_ipc4_base_module_cfg base;
42 	struct sof_ipc4_probe_gtw_cfg gtw_cfg;
43 } __packed __aligned(4);
44 
45 enum sof_ipc4_probe_type {
46 	SOF_IPC4_PROBE_TYPE_INPUT = 0,
47 	SOF_IPC4_PROBE_TYPE_OUTPUT,
48 	SOF_IPC4_PROBE_TYPE_INTERNAL
49 };
50 
51 struct sof_ipc4_probe_point {
52 	u32 point_id;
53 	u32 purpose;
54 	u32 stream_tag;
55 } __packed __aligned(4);
56 
57 struct sof_ipc4_probe_info {
58 	unsigned int num_elems;
59 	DECLARE_FLEX_ARRAY(struct sof_ipc4_probe_point, points);
60 } __packed;
61 
62 #define INVALID_PIPELINE_ID      0xFF
63 
64 /**
65  * sof_ipc4_probe_get_module_info - Get IPC4 module info for probe module
66  * @cdev:		SOF client device
67  * @return:		Pointer to IPC4 probe module info
68  *
69  * Look up the IPC4 probe module info based on the hard coded uuid and
70  * store the value for the future calls.
71  */
72 static struct sof_man4_module *sof_ipc4_probe_get_module_info(struct sof_client_dev *cdev)
73 {
74 	struct sof_probes_priv *priv = cdev->data;
75 	struct device *dev = &cdev->auxdev.dev;
76 	static const guid_t probe_uuid =
77 		GUID_INIT(0x7CAD0808, 0xAB10, 0xCD23,
78 			  0xEF, 0x45, 0x12, 0xAB, 0x34, 0xCD, 0x56, 0xEF);
79 
80 	if (!priv->ipc_priv) {
81 		struct sof_ipc4_fw_module *fw_module =
82 			sof_client_ipc4_find_module(cdev, &probe_uuid);
83 
84 		if (!fw_module) {
85 			dev_err(dev, "%s: no matching uuid found", __func__);
86 			return NULL;
87 		}
88 
89 		priv->ipc_priv = &fw_module->man4_module_entry;
90 	}
91 
92 	return (struct sof_man4_module *)priv->ipc_priv;
93 }
94 
95 /**
96  * ipc4_probes_init - initialize data probing
97  * @cdev:		SOF client device
98  * @stream_tag:		Extractor stream tag
99  * @buffer_size:	DMA buffer size to set for extractor
100  * @return:		0 on success, negative error code on error
101  *
102  * Host chooses whether extraction is supported or not by providing
103  * valid stream tag to DSP. Once specified, stream described by that
104  * tag will be tied to DSP for extraction for the entire lifetime of
105  * probe.
106  *
107  * Probing is initialized only once and each INIT request must be
108  * matched by DEINIT call.
109  */
110 static int ipc4_probes_init(struct sof_client_dev *cdev, u32 stream_tag,
111 			    size_t buffer_size)
112 {
113 	struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev);
114 	struct sof_ipc4_msg msg;
115 	struct sof_ipc4_probe_cfg cfg;
116 
117 	if (!mentry)
118 		return -ENODEV;
119 
120 	memset(&cfg, '\0', sizeof(cfg));
121 	cfg.gtw_cfg.node_id = SOF_IPC4_PROBE_NODE_ID_INDEX(stream_tag - 1) |
122 		SOF_IPC4_PROBE_NODE_ID_TYPE(SOF_IPC4_DMA_HDA_HOST_INPUT);
123 
124 	cfg.gtw_cfg.dma_buffer_size = buffer_size;
125 
126 	msg.primary = mentry->id;
127 	msg.primary |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_INIT_INSTANCE);
128 	msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
129 	msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
130 	msg.extension = SOF_IPC4_MOD_EXT_DST_MOD_INSTANCE(INVALID_PIPELINE_ID);
131 	msg.extension |= SOF_IPC4_MOD_EXT_CORE_ID(0);
132 	msg.extension |= SOF_IPC4_MOD_EXT_PARAM_SIZE(sizeof(cfg) / sizeof(uint32_t));
133 
134 	msg.data_size = sizeof(cfg);
135 	msg.data_ptr = &cfg;
136 
137 	return sof_client_ipc_tx_message_no_reply(cdev, &msg);
138 }
139 
140 /**
141  * ipc4_probes_deinit - cleanup after data probing
142  * @cdev:		SOF client device
143  * @return:		0 on success, negative error code on error
144  *
145  * Host sends DEINIT request to free previously initialized probe
146  * on DSP side once it is no longer needed. DEINIT only when there
147  * are no probes connected and with all injectors detached.
148  */
149 static int ipc4_probes_deinit(struct sof_client_dev *cdev)
150 {
151 	struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev);
152 	struct sof_ipc4_msg msg;
153 
154 	if (!mentry)
155 		return -ENODEV;
156 
157 	msg.primary = mentry->id;
158 	msg.primary |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_DELETE_INSTANCE);
159 	msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
160 	msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
161 	msg.extension = SOF_IPC4_MOD_EXT_DST_MOD_INSTANCE(INVALID_PIPELINE_ID);
162 	msg.extension |= SOF_IPC4_MOD_EXT_CORE_ID(0);
163 
164 	msg.data_size = 0;
165 	msg.data_ptr = NULL;
166 
167 	return sof_client_ipc_tx_message_no_reply(cdev, &msg);
168 }
169 
170 /**
171  * ipc4_probes_points_info - retrieve list of active probe points
172  * @cdev:	SOF client device
173  * @desc:	Returned list of active probes
174  * @num_desc:	Returned count of active probes
175  * @return:	0 on success, negative error code on error
176  */
177 static int ipc4_probes_points_info(struct sof_client_dev *cdev,
178 				   struct sof_probe_point_desc **desc,
179 				   size_t *num_desc)
180 {
181 	struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev);
182 	struct device *dev = &cdev->auxdev.dev;
183 	struct sof_ipc4_probe_info *info;
184 	struct sof_ipc4_msg msg;
185 	int i, ret;
186 
187 	if (!mentry)
188 		return -ENODEV;
189 
190 	msg.primary = mentry->id;
191 	msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
192 	msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
193 
194 	msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_PROBE_POINTS);
195 
196 	msg.data_size = sof_client_get_ipc_max_payload_size(cdev);
197 	msg.data_ptr = kzalloc(msg.data_size, GFP_KERNEL);
198 	if (!msg.data_ptr)
199 		return -ENOMEM;
200 
201 	ret = sof_client_ipc_set_get_data(cdev, &msg, false);
202 	if (ret) {
203 		kfree(msg.data_ptr);
204 		return ret;
205 	}
206 	info = msg.data_ptr;
207 	*num_desc = info->num_elems;
208 	dev_dbg(dev, "%s: got %zu probe points", __func__, *num_desc);
209 
210 	*desc = kzalloc(*num_desc * sizeof(**desc), GFP_KERNEL);
211 	if (!*desc) {
212 		kfree(msg.data_ptr);
213 		return -ENOMEM;
214 	}
215 
216 	for (i = 0; i < *num_desc; i++) {
217 		(*desc)[i].buffer_id = info->points[i].point_id;
218 		(*desc)[i].purpose = info->points[i].purpose;
219 		(*desc)[i].stream_tag = info->points[i].stream_tag;
220 	}
221 	kfree(msg.data_ptr);
222 
223 	return 0;
224 }
225 
226 /**
227  * ipc4_probes_points_add - connect specified probes
228  * @cdev:	SOF client device
229  * @desc:	List of probe points to connect
230  * @num_desc:	Number of elements in @desc
231  * @return:	0 on success, negative error code on error
232  *
233  * Translates the generic probe point presentation to an IPC4
234  * message to dynamically connect the provided set of endpoints.
235  */
236 static int ipc4_probes_points_add(struct sof_client_dev *cdev,
237 				  struct sof_probe_point_desc *desc,
238 				  size_t num_desc)
239 {
240 	struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev);
241 	struct sof_ipc4_probe_point *points;
242 	struct sof_ipc4_msg msg;
243 	int i, ret;
244 
245 	if (!mentry)
246 		return -ENODEV;
247 
248 	/* The sof_probe_point_desc and sof_ipc4_probe_point structs
249 	 * are of same size and even the integers are the same in the
250 	 * same order, and similar meaning, but since there is no
251 	 * performance issue I wrote the conversion explicitly open for
252 	 * future development.
253 	 */
254 	points = kcalloc(num_desc, sizeof(*points), GFP_KERNEL);
255 	if (!points)
256 		return -ENOMEM;
257 
258 	for (i = 0; i < num_desc; i++) {
259 		points[i].point_id = desc[i].buffer_id;
260 		points[i].purpose = desc[i].purpose;
261 		points[i].stream_tag = desc[i].stream_tag;
262 	}
263 
264 	msg.primary = mentry->id;
265 	msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
266 	msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
267 
268 	msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_PROBE_POINTS);
269 
270 	msg.data_size = sizeof(*points) * num_desc;
271 	msg.data_ptr = points;
272 
273 	ret = sof_client_ipc_set_get_data(cdev, &msg, true);
274 
275 	kfree(points);
276 
277 	return ret;
278 }
279 
280 /**
281  * ipc4_probes_points_remove - disconnect specified probes
282  * @cdev:		SOF client device
283  * @buffer_id:		List of probe points to disconnect
284  * @num_buffer_id:	Number of elements in @desc
285  * @return:		0 on success, negative error code on error
286  *
287  * Converts the generic buffer_id to IPC4 probe_point_id and remove
288  * the probe points with an IPC4 for message.
289  */
290 static int ipc4_probes_points_remove(struct sof_client_dev *cdev,
291 				     unsigned int *buffer_id, size_t num_buffer_id)
292 {
293 	struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev);
294 	struct sof_ipc4_msg msg;
295 	u32 *probe_point_ids;
296 	int i, ret;
297 
298 	if (!mentry)
299 		return -ENODEV;
300 
301 	probe_point_ids = kcalloc(num_buffer_id, sizeof(*probe_point_ids),
302 				  GFP_KERNEL);
303 	if (!probe_point_ids)
304 		return -ENOMEM;
305 
306 	for (i = 0; i < num_buffer_id; i++)
307 		probe_point_ids[i] = buffer_id[i];
308 
309 	msg.primary = mentry->id;
310 	msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
311 	msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
312 
313 	msg.extension =
314 		SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_PROBE_POINTS_DISCONNECT);
315 
316 	msg.data_size = num_buffer_id * sizeof(*probe_point_ids);
317 	msg.data_ptr = probe_point_ids;
318 
319 	ret = sof_client_ipc_set_get_data(cdev, &msg, true);
320 
321 	kfree(probe_point_ids);
322 
323 	return ret;
324 }
325 
326 const struct sof_probes_ipc_ops ipc4_probe_ops =  {
327 	.init = ipc4_probes_init,
328 	.deinit = ipc4_probes_deinit,
329 	.points_info = ipc4_probes_points_info,
330 	.points_add = ipc4_probes_points_add,
331 	.points_remove = ipc4_probes_points_remove,
332 };
333