xref: /linux/sound/soc/sof/sof-client-probes-ipc3.c (revision 33e02dc69afbd8f1b85a51d74d72f139ba4ca623)
1ccf06b14SJyri Sarha // SPDX-License-Identifier: GPL-2.0-only
2ccf06b14SJyri Sarha //
3*293ad281SPierre-Louis Bossart // Copyright(c) 2019-2022 Intel Corporation
4ccf06b14SJyri Sarha //
5ccf06b14SJyri Sarha // Author: Cezary Rojewski <cezary.rojewski@intel.com>
6ccf06b14SJyri Sarha //
7ccf06b14SJyri Sarha // Code moved to this file by:
8ccf06b14SJyri Sarha //  Jyri Sarha <jyri.sarha@intel.com>
9ccf06b14SJyri Sarha //
10ccf06b14SJyri Sarha 
11ccf06b14SJyri Sarha #include <linux/stddef.h>
12ccf06b14SJyri Sarha #include <sound/soc.h>
13ccf06b14SJyri Sarha #include <sound/sof/header.h>
14ccf06b14SJyri Sarha #include "sof-client.h"
15ccf06b14SJyri Sarha #include "sof-client-probes.h"
16ccf06b14SJyri Sarha 
17ccf06b14SJyri Sarha struct sof_probe_dma {
18ccf06b14SJyri Sarha 	unsigned int stream_tag;
19ccf06b14SJyri Sarha 	unsigned int dma_buffer_size;
20ccf06b14SJyri Sarha } __packed;
21ccf06b14SJyri Sarha 
22ccf06b14SJyri Sarha struct sof_ipc_probe_dma_add_params {
23ccf06b14SJyri Sarha 	struct sof_ipc_cmd_hdr hdr;
24ccf06b14SJyri Sarha 	unsigned int num_elems;
25ccf06b14SJyri Sarha 	struct sof_probe_dma dma[];
26ccf06b14SJyri Sarha } __packed;
27ccf06b14SJyri Sarha 
28ccf06b14SJyri Sarha struct sof_ipc_probe_info_params {
29ccf06b14SJyri Sarha 	struct sof_ipc_reply rhdr;
30ccf06b14SJyri Sarha 	unsigned int num_elems;
31ccf06b14SJyri Sarha 	union {
32ccf06b14SJyri Sarha 		DECLARE_FLEX_ARRAY(struct sof_probe_dma, dma);
33ccf06b14SJyri Sarha 		DECLARE_FLEX_ARRAY(struct sof_probe_point_desc, desc);
34ccf06b14SJyri Sarha 	};
35ccf06b14SJyri Sarha } __packed;
36ccf06b14SJyri Sarha 
37ccf06b14SJyri Sarha struct sof_ipc_probe_point_add_params {
38ccf06b14SJyri Sarha 	struct sof_ipc_cmd_hdr hdr;
39ccf06b14SJyri Sarha 	unsigned int num_elems;
40ccf06b14SJyri Sarha 	struct sof_probe_point_desc desc[];
41ccf06b14SJyri Sarha } __packed;
42ccf06b14SJyri Sarha 
43ccf06b14SJyri Sarha struct sof_ipc_probe_point_remove_params {
44ccf06b14SJyri Sarha 	struct sof_ipc_cmd_hdr hdr;
45ccf06b14SJyri Sarha 	unsigned int num_elems;
46ccf06b14SJyri Sarha 	unsigned int buffer_id[];
47ccf06b14SJyri Sarha } __packed;
48ccf06b14SJyri Sarha 
49ccf06b14SJyri Sarha /**
50ccf06b14SJyri Sarha  * ipc3_probes_init - initialize data probing
51ccf06b14SJyri Sarha  * @cdev:		SOF client device
52ccf06b14SJyri Sarha  * @stream_tag:		Extractor stream tag
53ccf06b14SJyri Sarha  * @buffer_size:	DMA buffer size to set for extractor
54ccf06b14SJyri Sarha  *
55ccf06b14SJyri Sarha  * Host chooses whether extraction is supported or not by providing
56ccf06b14SJyri Sarha  * valid stream tag to DSP. Once specified, stream described by that
57ccf06b14SJyri Sarha  * tag will be tied to DSP for extraction for the entire lifetime of
58ccf06b14SJyri Sarha  * probe.
59ccf06b14SJyri Sarha  *
60ccf06b14SJyri Sarha  * Probing is initialized only once and each INIT request must be
61ccf06b14SJyri Sarha  * matched by DEINIT call.
62ccf06b14SJyri Sarha  */
ipc3_probes_init(struct sof_client_dev * cdev,u32 stream_tag,size_t buffer_size)63ccf06b14SJyri Sarha static int ipc3_probes_init(struct sof_client_dev *cdev, u32 stream_tag,
64ccf06b14SJyri Sarha 			    size_t buffer_size)
65ccf06b14SJyri Sarha {
66ccf06b14SJyri Sarha 	struct sof_ipc_probe_dma_add_params *msg;
67ccf06b14SJyri Sarha 	size_t size = struct_size(msg, dma, 1);
68ccf06b14SJyri Sarha 	int ret;
69ccf06b14SJyri Sarha 
70ccf06b14SJyri Sarha 	msg = kmalloc(size, GFP_KERNEL);
71ccf06b14SJyri Sarha 	if (!msg)
72ccf06b14SJyri Sarha 		return -ENOMEM;
73ccf06b14SJyri Sarha 	msg->hdr.size = size;
74ccf06b14SJyri Sarha 	msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_INIT;
75ccf06b14SJyri Sarha 	msg->num_elems = 1;
76ccf06b14SJyri Sarha 	msg->dma[0].stream_tag = stream_tag;
77ccf06b14SJyri Sarha 	msg->dma[0].dma_buffer_size = buffer_size;
78ccf06b14SJyri Sarha 
79367fd6ffSCurtis Malainey 	ret = sof_client_ipc_tx_message_no_reply(cdev, msg);
80ccf06b14SJyri Sarha 	kfree(msg);
81ccf06b14SJyri Sarha 	return ret;
82ccf06b14SJyri Sarha }
83ccf06b14SJyri Sarha 
84ccf06b14SJyri Sarha /**
85ccf06b14SJyri Sarha  * ipc3_probes_deinit - cleanup after data probing
86ccf06b14SJyri Sarha  * @cdev:		SOF client device
87ccf06b14SJyri Sarha  *
88ccf06b14SJyri Sarha  * Host sends DEINIT request to free previously initialized probe
89ccf06b14SJyri Sarha  * on DSP side once it is no longer needed. DEINIT only when there
90ccf06b14SJyri Sarha  * are no probes connected and with all injectors detached.
91ccf06b14SJyri Sarha  */
ipc3_probes_deinit(struct sof_client_dev * cdev)92ccf06b14SJyri Sarha static int ipc3_probes_deinit(struct sof_client_dev *cdev)
93ccf06b14SJyri Sarha {
94ccf06b14SJyri Sarha 	struct sof_ipc_cmd_hdr msg;
95ccf06b14SJyri Sarha 
96ccf06b14SJyri Sarha 	msg.size = sizeof(msg);
97ccf06b14SJyri Sarha 	msg.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_DEINIT;
98ccf06b14SJyri Sarha 
99367fd6ffSCurtis Malainey 	return sof_client_ipc_tx_message_no_reply(cdev, &msg);
100ccf06b14SJyri Sarha }
101ccf06b14SJyri Sarha 
ipc3_probes_info(struct sof_client_dev * cdev,unsigned int cmd,void ** params,size_t * num_params)102ccf06b14SJyri Sarha static int ipc3_probes_info(struct sof_client_dev *cdev, unsigned int cmd,
103ccf06b14SJyri Sarha 			    void **params, size_t *num_params)
104ccf06b14SJyri Sarha {
105ccf06b14SJyri Sarha 	size_t max_msg_size = sof_client_get_ipc_max_payload_size(cdev);
106ccf06b14SJyri Sarha 	struct sof_ipc_probe_info_params msg = {{{0}}};
107ccf06b14SJyri Sarha 	struct sof_ipc_probe_info_params *reply;
108ccf06b14SJyri Sarha 	size_t bytes;
109ccf06b14SJyri Sarha 	int ret;
110ccf06b14SJyri Sarha 
111ccf06b14SJyri Sarha 	*params = NULL;
112ccf06b14SJyri Sarha 	*num_params = 0;
113ccf06b14SJyri Sarha 
114ccf06b14SJyri Sarha 	reply = kzalloc(max_msg_size, GFP_KERNEL);
115ccf06b14SJyri Sarha 	if (!reply)
116ccf06b14SJyri Sarha 		return -ENOMEM;
117ccf06b14SJyri Sarha 	msg.rhdr.hdr.size = sizeof(msg);
118ccf06b14SJyri Sarha 	msg.rhdr.hdr.cmd = SOF_IPC_GLB_PROBE | cmd;
119ccf06b14SJyri Sarha 
120ccf06b14SJyri Sarha 	ret = sof_client_ipc_tx_message(cdev, &msg, reply, max_msg_size);
121ccf06b14SJyri Sarha 	if (ret < 0 || reply->rhdr.error < 0)
122ccf06b14SJyri Sarha 		goto exit;
123ccf06b14SJyri Sarha 
124ccf06b14SJyri Sarha 	if (!reply->num_elems)
125ccf06b14SJyri Sarha 		goto exit;
126ccf06b14SJyri Sarha 
127ccf06b14SJyri Sarha 	if (cmd == SOF_IPC_PROBE_DMA_INFO)
128ccf06b14SJyri Sarha 		bytes = sizeof(reply->dma[0]);
129ccf06b14SJyri Sarha 	else
130ccf06b14SJyri Sarha 		bytes = sizeof(reply->desc[0]);
131ccf06b14SJyri Sarha 	bytes *= reply->num_elems;
132ccf06b14SJyri Sarha 	*params = kmemdup(&reply->dma[0], bytes, GFP_KERNEL);
133ccf06b14SJyri Sarha 	if (!*params) {
134ccf06b14SJyri Sarha 		ret = -ENOMEM;
135ccf06b14SJyri Sarha 		goto exit;
136ccf06b14SJyri Sarha 	}
137ccf06b14SJyri Sarha 	*num_params = reply->num_elems;
138ccf06b14SJyri Sarha 
139ccf06b14SJyri Sarha exit:
140ccf06b14SJyri Sarha 	kfree(reply);
141ccf06b14SJyri Sarha 	return ret;
142ccf06b14SJyri Sarha }
143ccf06b14SJyri Sarha 
144ccf06b14SJyri Sarha /**
145ccf06b14SJyri Sarha  * ipc3_probes_points_info - retrieve list of active probe points
146ccf06b14SJyri Sarha  * @cdev:		SOF client device
147ccf06b14SJyri Sarha  * @desc:	Returned list of active probes
148ccf06b14SJyri Sarha  * @num_desc:	Returned count of active probes
149ccf06b14SJyri Sarha  *
150ccf06b14SJyri Sarha  * Host sends PROBE_POINT_INFO request to obtain list of active probe
151ccf06b14SJyri Sarha  * points, valid for disconnection when given probe is no longer
152ccf06b14SJyri Sarha  * required.
153ccf06b14SJyri Sarha  */
ipc3_probes_points_info(struct sof_client_dev * cdev,struct sof_probe_point_desc ** desc,size_t * num_desc)154ccf06b14SJyri Sarha static int ipc3_probes_points_info(struct sof_client_dev *cdev,
155ccf06b14SJyri Sarha 				   struct sof_probe_point_desc **desc,
156ccf06b14SJyri Sarha 				   size_t *num_desc)
157ccf06b14SJyri Sarha {
158ccf06b14SJyri Sarha 	return ipc3_probes_info(cdev, SOF_IPC_PROBE_POINT_INFO,
159ccf06b14SJyri Sarha 			       (void **)desc, num_desc);
160ccf06b14SJyri Sarha }
161ccf06b14SJyri Sarha 
162ccf06b14SJyri Sarha /**
163ccf06b14SJyri Sarha  * ipc3_probes_points_add - connect specified probes
164ccf06b14SJyri Sarha  * @cdev:		SOF client device
165ccf06b14SJyri Sarha  * @desc:	List of probe points to connect
166ccf06b14SJyri Sarha  * @num_desc:	Number of elements in @desc
167ccf06b14SJyri Sarha  *
168ccf06b14SJyri Sarha  * Dynamically connects to provided set of endpoints. Immediately
169ccf06b14SJyri Sarha  * after connection is established, host must be prepared to
170ccf06b14SJyri Sarha  * transfer data from or to target stream given the probing purpose.
171ccf06b14SJyri Sarha  *
172ccf06b14SJyri Sarha  * Each probe point should be removed using PROBE_POINT_REMOVE
173ccf06b14SJyri Sarha  * request when no longer needed.
174ccf06b14SJyri Sarha  */
ipc3_probes_points_add(struct sof_client_dev * cdev,struct sof_probe_point_desc * desc,size_t num_desc)175ccf06b14SJyri Sarha static int ipc3_probes_points_add(struct sof_client_dev *cdev,
176ccf06b14SJyri Sarha 				  struct sof_probe_point_desc *desc,
177ccf06b14SJyri Sarha 				  size_t num_desc)
178ccf06b14SJyri Sarha {
179ccf06b14SJyri Sarha 	struct sof_ipc_probe_point_add_params *msg;
180ccf06b14SJyri Sarha 	size_t size = struct_size(msg, desc, num_desc);
181ccf06b14SJyri Sarha 	int ret;
182ccf06b14SJyri Sarha 
183ccf06b14SJyri Sarha 	msg = kmalloc(size, GFP_KERNEL);
184ccf06b14SJyri Sarha 	if (!msg)
185ccf06b14SJyri Sarha 		return -ENOMEM;
186ccf06b14SJyri Sarha 	msg->hdr.size = size;
187ccf06b14SJyri Sarha 	msg->num_elems = num_desc;
188ccf06b14SJyri Sarha 	msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_POINT_ADD;
189ccf06b14SJyri Sarha 	memcpy(&msg->desc[0], desc, size - sizeof(*msg));
190ccf06b14SJyri Sarha 
191367fd6ffSCurtis Malainey 	ret = sof_client_ipc_tx_message_no_reply(cdev, msg);
192ccf06b14SJyri Sarha 	kfree(msg);
193ccf06b14SJyri Sarha 	return ret;
194ccf06b14SJyri Sarha }
195ccf06b14SJyri Sarha 
196ccf06b14SJyri Sarha /**
197ccf06b14SJyri Sarha  * ipc3_probes_points_remove - disconnect specified probes
198ccf06b14SJyri Sarha  * @cdev:		SOF client device
199ccf06b14SJyri Sarha  * @buffer_id:		List of probe points to disconnect
200ccf06b14SJyri Sarha  * @num_buffer_id:	Number of elements in @desc
201ccf06b14SJyri Sarha  *
202ccf06b14SJyri Sarha  * Removes previously connected probes from list of active probe
203ccf06b14SJyri Sarha  * points and frees all resources on DSP side.
204ccf06b14SJyri Sarha  */
ipc3_probes_points_remove(struct sof_client_dev * cdev,unsigned int * buffer_id,size_t num_buffer_id)205ccf06b14SJyri Sarha static int ipc3_probes_points_remove(struct sof_client_dev *cdev,
206ccf06b14SJyri Sarha 				     unsigned int *buffer_id,
207ccf06b14SJyri Sarha 				     size_t num_buffer_id)
208ccf06b14SJyri Sarha {
209ccf06b14SJyri Sarha 	struct sof_ipc_probe_point_remove_params *msg;
210ccf06b14SJyri Sarha 	size_t size = struct_size(msg, buffer_id, num_buffer_id);
211ccf06b14SJyri Sarha 	int ret;
212ccf06b14SJyri Sarha 
213ccf06b14SJyri Sarha 	msg = kmalloc(size, GFP_KERNEL);
214ccf06b14SJyri Sarha 	if (!msg)
215ccf06b14SJyri Sarha 		return -ENOMEM;
216ccf06b14SJyri Sarha 	msg->hdr.size = size;
217ccf06b14SJyri Sarha 	msg->num_elems = num_buffer_id;
218ccf06b14SJyri Sarha 	msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_POINT_REMOVE;
219ccf06b14SJyri Sarha 	memcpy(&msg->buffer_id[0], buffer_id, size - sizeof(*msg));
220ccf06b14SJyri Sarha 
221367fd6ffSCurtis Malainey 	ret = sof_client_ipc_tx_message_no_reply(cdev, msg);
222ccf06b14SJyri Sarha 	kfree(msg);
223ccf06b14SJyri Sarha 	return ret;
224ccf06b14SJyri Sarha }
225ccf06b14SJyri Sarha 
226ccf06b14SJyri Sarha const struct sof_probes_ipc_ops ipc3_probe_ops =  {
227ccf06b14SJyri Sarha 	.init = ipc3_probes_init,
228ccf06b14SJyri Sarha 	.deinit = ipc3_probes_deinit,
229ccf06b14SJyri Sarha 	.points_info = ipc3_probes_points_info,
230ccf06b14SJyri Sarha 	.points_add = ipc3_probes_points_add,
231ccf06b14SJyri Sarha 	.points_remove = ipc3_probes_points_remove,
232ccf06b14SJyri Sarha };
233