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