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  */
sof_ipc4_probe_get_module_info(struct sof_client_dev * cdev)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  */
ipc4_probes_init(struct sof_client_dev * cdev,u32 stream_tag,size_t buffer_size)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 	msg.extension |= SOF_IPC4_MOD_EXT_PARAM_SIZE(sizeof(cfg) / sizeof(uint32_t));
129 
130 	msg.data_size = sizeof(cfg);
131 	msg.data_ptr = &cfg;
132 
133 	return sof_client_ipc_tx_message_no_reply(cdev, &msg);
134 }
135 
136 /**
137  * ipc4_probes_deinit - cleanup after data probing
138  * @cdev:		SOF client device
139  * @return:		0 on success, negative error code on error
140  *
141  * Host sends DEINIT request to free previously initialized probe
142  * on DSP side once it is no longer needed. DEINIT only when there
143  * are no probes connected and with all injectors detached.
144  */
ipc4_probes_deinit(struct sof_client_dev * cdev)145 static int ipc4_probes_deinit(struct sof_client_dev *cdev)
146 {
147 	struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev);
148 	struct sof_ipc4_msg msg;
149 
150 	if (!mentry)
151 		return -ENODEV;
152 
153 	msg.primary = mentry->id;
154 	msg.primary |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_DELETE_INSTANCE);
155 	msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
156 	msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
157 	msg.extension = SOF_IPC4_MOD_EXT_DST_MOD_INSTANCE(INVALID_PIPELINE_ID);
158 	msg.extension |= SOF_IPC4_MOD_EXT_CORE_ID(0);
159 
160 	msg.data_size = 0;
161 	msg.data_ptr = NULL;
162 
163 	return sof_client_ipc_tx_message_no_reply(cdev, &msg);
164 }
165 
166 /**
167  * ipc4_probes_points_info - retrieve list of active probe points
168  * @cdev:	SOF client device
169  * @desc:	Returned list of active probes
170  * @num_desc:	Returned count of active probes
171  * @return:	0 on success, negative error code on error
172  *
173  * Dummy implementation returning empty list of probes.
174  */
ipc4_probes_points_info(struct sof_client_dev * cdev,struct sof_probe_point_desc ** desc,size_t * num_desc)175 static int ipc4_probes_points_info(struct sof_client_dev *cdev,
176 				   struct sof_probe_point_desc **desc,
177 				   size_t *num_desc)
178 {
179 	/* TODO: Firmware side implementation needed first */
180 	*desc = NULL;
181 	*num_desc = 0;
182 	return 0;
183 }
184 
185 /**
186  * ipc4_probes_points_add - connect specified probes
187  * @cdev:	SOF client device
188  * @desc:	List of probe points to connect
189  * @num_desc:	Number of elements in @desc
190  * @return:	0 on success, negative error code on error
191  *
192  * Translates the generic probe point presentation to an IPC4
193  * message to dynamically connect the provided set of endpoints.
194  */
ipc4_probes_points_add(struct sof_client_dev * cdev,struct sof_probe_point_desc * desc,size_t num_desc)195 static int ipc4_probes_points_add(struct sof_client_dev *cdev,
196 				  struct sof_probe_point_desc *desc,
197 				  size_t num_desc)
198 {
199 	struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev);
200 	struct sof_ipc4_probe_point *points;
201 	struct sof_ipc4_msg msg;
202 	int i, ret;
203 
204 	if (!mentry)
205 		return -ENODEV;
206 
207 	/* The sof_probe_point_desc and sof_ipc4_probe_point structs
208 	 * are of same size and even the integers are the same in the
209 	 * same order, and similar meaning, but since there is no
210 	 * performance issue I wrote the conversion explicitly open for
211 	 * future development.
212 	 */
213 	points = kcalloc(num_desc, sizeof(*points), GFP_KERNEL);
214 	if (!points)
215 		return -ENOMEM;
216 
217 	for (i = 0; i < num_desc; i++) {
218 		points[i].point_id = desc[i].buffer_id;
219 		points[i].purpose = desc[i].purpose;
220 		points[i].stream_tag = desc[i].stream_tag;
221 	}
222 
223 	msg.primary = mentry->id;
224 	msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
225 	msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
226 
227 	msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_PROBE_POINTS);
228 
229 	msg.data_size = sizeof(*points) * num_desc;
230 	msg.data_ptr = points;
231 
232 	ret = sof_client_ipc_set_get_data(cdev, &msg, true);
233 
234 	kfree(points);
235 
236 	return ret;
237 }
238 
239 /**
240  * ipc4_probes_points_remove - disconnect specified probes
241  * @cdev:		SOF client device
242  * @buffer_id:		List of probe points to disconnect
243  * @num_buffer_id:	Number of elements in @desc
244  * @return:		0 on success, negative error code on error
245  *
246  * Converts the generic buffer_id to IPC4 probe_point_id and remove
247  * the probe points with an IPC4 for message.
248  */
ipc4_probes_points_remove(struct sof_client_dev * cdev,unsigned int * buffer_id,size_t num_buffer_id)249 static int ipc4_probes_points_remove(struct sof_client_dev *cdev,
250 				     unsigned int *buffer_id, size_t num_buffer_id)
251 {
252 	struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev);
253 	struct sof_ipc4_msg msg;
254 	u32 *probe_point_ids;
255 	int i, ret;
256 
257 	if (!mentry)
258 		return -ENODEV;
259 
260 	probe_point_ids = kcalloc(num_buffer_id, sizeof(*probe_point_ids),
261 				  GFP_KERNEL);
262 	if (!probe_point_ids)
263 		return -ENOMEM;
264 
265 	for (i = 0; i < num_buffer_id; i++)
266 		probe_point_ids[i] = buffer_id[i];
267 
268 	msg.primary = mentry->id;
269 	msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
270 	msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
271 
272 	msg.extension =
273 		SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_PROBE_POINTS_DISCONNECT);
274 
275 	msg.data_size = num_buffer_id * sizeof(*probe_point_ids);
276 	msg.data_ptr = probe_point_ids;
277 
278 	ret = sof_client_ipc_set_get_data(cdev, &msg, true);
279 
280 	kfree(probe_point_ids);
281 
282 	return ret;
283 }
284 
285 const struct sof_probes_ipc_ops ipc4_probe_ops =  {
286 	.init = ipc4_probes_init,
287 	.deinit = ipc4_probes_deinit,
288 	.points_info = ipc4_probes_points_info,
289 	.points_add = ipc4_probes_points_add,
290 	.points_remove = ipc4_probes_points_remove,
291 };
292