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