xref: /OK3568_Linux_fs/kernel/sound/soc/sof/probe.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // This file is provided under a dual BSD/GPLv2 license.  When using or
4*4882a593Smuzhiyun // redistributing this file, you may do so under either license.
5*4882a593Smuzhiyun //
6*4882a593Smuzhiyun // Copyright(c) 2019-2020 Intel Corporation. All rights reserved.
7*4882a593Smuzhiyun //
8*4882a593Smuzhiyun // Author: Cezary Rojewski <cezary.rojewski@intel.com>
9*4882a593Smuzhiyun //
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include "sof-priv.h"
12*4882a593Smuzhiyun #include "probe.h"
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun /**
15*4882a593Smuzhiyun  * sof_ipc_probe_init - initialize data probing
16*4882a593Smuzhiyun  * @sdev:		SOF sound device
17*4882a593Smuzhiyun  * @stream_tag:		Extractor stream tag
18*4882a593Smuzhiyun  * @buffer_size:	DMA buffer size to set for extractor
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * Host chooses whether extraction is supported or not by providing
21*4882a593Smuzhiyun  * valid stream tag to DSP. Once specified, stream described by that
22*4882a593Smuzhiyun  * tag will be tied to DSP for extraction for the entire lifetime of
23*4882a593Smuzhiyun  * probe.
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * Probing is initialized only once and each INIT request must be
26*4882a593Smuzhiyun  * matched by DEINIT call.
27*4882a593Smuzhiyun  */
sof_ipc_probe_init(struct snd_sof_dev * sdev,u32 stream_tag,size_t buffer_size)28*4882a593Smuzhiyun int sof_ipc_probe_init(struct snd_sof_dev *sdev,
29*4882a593Smuzhiyun 		u32 stream_tag, size_t buffer_size)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun 	struct sof_ipc_probe_dma_add_params *msg;
32*4882a593Smuzhiyun 	struct sof_ipc_reply reply;
33*4882a593Smuzhiyun 	size_t size = struct_size(msg, dma, 1);
34*4882a593Smuzhiyun 	int ret;
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun 	msg = kmalloc(size, GFP_KERNEL);
37*4882a593Smuzhiyun 	if (!msg)
38*4882a593Smuzhiyun 		return -ENOMEM;
39*4882a593Smuzhiyun 	msg->hdr.size = size;
40*4882a593Smuzhiyun 	msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_INIT;
41*4882a593Smuzhiyun 	msg->num_elems = 1;
42*4882a593Smuzhiyun 	msg->dma[0].stream_tag = stream_tag;
43*4882a593Smuzhiyun 	msg->dma[0].dma_buffer_size = buffer_size;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	ret = sof_ipc_tx_message(sdev->ipc, msg->hdr.cmd, msg, msg->hdr.size,
46*4882a593Smuzhiyun 			&reply, sizeof(reply));
47*4882a593Smuzhiyun 	kfree(msg);
48*4882a593Smuzhiyun 	return ret;
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun EXPORT_SYMBOL(sof_ipc_probe_init);
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /**
53*4882a593Smuzhiyun  * sof_ipc_probe_deinit - cleanup after data probing
54*4882a593Smuzhiyun  * @sdev:	SOF sound device
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  * Host sends DEINIT request to free previously initialized probe
57*4882a593Smuzhiyun  * on DSP side once it is no longer needed. DEINIT only when there
58*4882a593Smuzhiyun  * are no probes connected and with all injectors detached.
59*4882a593Smuzhiyun  */
sof_ipc_probe_deinit(struct snd_sof_dev * sdev)60*4882a593Smuzhiyun int sof_ipc_probe_deinit(struct snd_sof_dev *sdev)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun 	struct sof_ipc_cmd_hdr msg;
63*4882a593Smuzhiyun 	struct sof_ipc_reply reply;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	msg.size = sizeof(msg);
66*4882a593Smuzhiyun 	msg.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_DEINIT;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	return sof_ipc_tx_message(sdev->ipc, msg.cmd, &msg, msg.size,
69*4882a593Smuzhiyun 			&reply, sizeof(reply));
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun EXPORT_SYMBOL(sof_ipc_probe_deinit);
72*4882a593Smuzhiyun 
sof_ipc_probe_info(struct snd_sof_dev * sdev,unsigned int cmd,void ** params,size_t * num_params)73*4882a593Smuzhiyun static int sof_ipc_probe_info(struct snd_sof_dev *sdev, unsigned int cmd,
74*4882a593Smuzhiyun 		void **params, size_t *num_params)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	struct sof_ipc_probe_info_params msg = {{{0}}};
77*4882a593Smuzhiyun 	struct sof_ipc_probe_info_params *reply;
78*4882a593Smuzhiyun 	size_t bytes;
79*4882a593Smuzhiyun 	int ret;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	*params = NULL;
82*4882a593Smuzhiyun 	*num_params = 0;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	reply = kzalloc(SOF_IPC_MSG_MAX_SIZE, GFP_KERNEL);
85*4882a593Smuzhiyun 	if (!reply)
86*4882a593Smuzhiyun 		return -ENOMEM;
87*4882a593Smuzhiyun 	msg.rhdr.hdr.size = sizeof(msg);
88*4882a593Smuzhiyun 	msg.rhdr.hdr.cmd = SOF_IPC_GLB_PROBE | cmd;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	ret = sof_ipc_tx_message(sdev->ipc, msg.rhdr.hdr.cmd, &msg,
91*4882a593Smuzhiyun 			msg.rhdr.hdr.size, reply, SOF_IPC_MSG_MAX_SIZE);
92*4882a593Smuzhiyun 	if (ret < 0 || reply->rhdr.error < 0)
93*4882a593Smuzhiyun 		goto exit;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	if (!reply->num_elems)
96*4882a593Smuzhiyun 		goto exit;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	if (cmd == SOF_IPC_PROBE_DMA_INFO)
99*4882a593Smuzhiyun 		bytes = sizeof(reply->dma[0]);
100*4882a593Smuzhiyun 	else
101*4882a593Smuzhiyun 		bytes = sizeof(reply->desc[0]);
102*4882a593Smuzhiyun 	bytes *= reply->num_elems;
103*4882a593Smuzhiyun 	*params = kmemdup(&reply->dma[0], bytes, GFP_KERNEL);
104*4882a593Smuzhiyun 	if (!*params) {
105*4882a593Smuzhiyun 		ret = -ENOMEM;
106*4882a593Smuzhiyun 		goto exit;
107*4882a593Smuzhiyun 	}
108*4882a593Smuzhiyun 	*num_params = reply->num_elems;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun exit:
111*4882a593Smuzhiyun 	kfree(reply);
112*4882a593Smuzhiyun 	return ret;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun /**
116*4882a593Smuzhiyun  * sof_ipc_probe_dma_info - retrieve list of active injection dmas
117*4882a593Smuzhiyun  * @sdev:	SOF sound device
118*4882a593Smuzhiyun  * @dma:	Returned list of active dmas
119*4882a593Smuzhiyun  * @num_dma:	Returned count of active dmas
120*4882a593Smuzhiyun  *
121*4882a593Smuzhiyun  * Host sends DMA_INFO request to obtain list of injection dmas it
122*4882a593Smuzhiyun  * can use to transfer data over with.
123*4882a593Smuzhiyun  *
124*4882a593Smuzhiyun  * Note that list contains only injection dmas as there is only one
125*4882a593Smuzhiyun  * extractor (dma) and it is always assigned on probing init.
126*4882a593Smuzhiyun  * DSP knows exactly where data from extraction probes is going to,
127*4882a593Smuzhiyun  * which is not the case for injection where multiple streams
128*4882a593Smuzhiyun  * could be engaged.
129*4882a593Smuzhiyun  */
sof_ipc_probe_dma_info(struct snd_sof_dev * sdev,struct sof_probe_dma ** dma,size_t * num_dma)130*4882a593Smuzhiyun int sof_ipc_probe_dma_info(struct snd_sof_dev *sdev,
131*4882a593Smuzhiyun 		struct sof_probe_dma **dma, size_t *num_dma)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	return sof_ipc_probe_info(sdev, SOF_IPC_PROBE_DMA_INFO,
134*4882a593Smuzhiyun 			(void **)dma, num_dma);
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun EXPORT_SYMBOL(sof_ipc_probe_dma_info);
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun /**
139*4882a593Smuzhiyun  * sof_ipc_probe_dma_add - attach to specified dmas
140*4882a593Smuzhiyun  * @sdev:	SOF sound device
141*4882a593Smuzhiyun  * @dma:	List of streams (dmas) to attach to
142*4882a593Smuzhiyun  * @num_dma:	Number of elements in @dma
143*4882a593Smuzhiyun  *
144*4882a593Smuzhiyun  * Contrary to extraction, injection streams are never assigned
145*4882a593Smuzhiyun  * on init. Before attempting any data injection, host is responsible
146*4882a593Smuzhiyun  * for specifying streams which will be later used to transfer data
147*4882a593Smuzhiyun  * to connected probe points.
148*4882a593Smuzhiyun  */
sof_ipc_probe_dma_add(struct snd_sof_dev * sdev,struct sof_probe_dma * dma,size_t num_dma)149*4882a593Smuzhiyun int sof_ipc_probe_dma_add(struct snd_sof_dev *sdev,
150*4882a593Smuzhiyun 		struct sof_probe_dma *dma, size_t num_dma)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	struct sof_ipc_probe_dma_add_params *msg;
153*4882a593Smuzhiyun 	struct sof_ipc_reply reply;
154*4882a593Smuzhiyun 	size_t size = struct_size(msg, dma, num_dma);
155*4882a593Smuzhiyun 	int ret;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	msg = kmalloc(size, GFP_KERNEL);
158*4882a593Smuzhiyun 	if (!msg)
159*4882a593Smuzhiyun 		return -ENOMEM;
160*4882a593Smuzhiyun 	msg->hdr.size = size;
161*4882a593Smuzhiyun 	msg->num_elems = num_dma;
162*4882a593Smuzhiyun 	msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_DMA_ADD;
163*4882a593Smuzhiyun 	memcpy(&msg->dma[0], dma, size - sizeof(*msg));
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	ret = sof_ipc_tx_message(sdev->ipc, msg->hdr.cmd, msg, msg->hdr.size,
166*4882a593Smuzhiyun 			&reply, sizeof(reply));
167*4882a593Smuzhiyun 	kfree(msg);
168*4882a593Smuzhiyun 	return ret;
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun EXPORT_SYMBOL(sof_ipc_probe_dma_add);
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun /**
173*4882a593Smuzhiyun  * sof_ipc_probe_dma_remove - detach from specified dmas
174*4882a593Smuzhiyun  * @sdev:		SOF sound device
175*4882a593Smuzhiyun  * @stream_tag:		List of stream tags to detach from
176*4882a593Smuzhiyun  * @num_stream_tag:	Number of elements in @stream_tag
177*4882a593Smuzhiyun  *
178*4882a593Smuzhiyun  * Host sends DMA_REMOVE request to free previously attached stream
179*4882a593Smuzhiyun  * from being occupied for injection. Each detach operation should
180*4882a593Smuzhiyun  * match equivalent DMA_ADD. Detach only when all probes tied to
181*4882a593Smuzhiyun  * given stream have been disconnected.
182*4882a593Smuzhiyun  */
sof_ipc_probe_dma_remove(struct snd_sof_dev * sdev,unsigned int * stream_tag,size_t num_stream_tag)183*4882a593Smuzhiyun int sof_ipc_probe_dma_remove(struct snd_sof_dev *sdev,
184*4882a593Smuzhiyun 		unsigned int *stream_tag, size_t num_stream_tag)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun 	struct sof_ipc_probe_dma_remove_params *msg;
187*4882a593Smuzhiyun 	struct sof_ipc_reply reply;
188*4882a593Smuzhiyun 	size_t size = struct_size(msg, stream_tag, num_stream_tag);
189*4882a593Smuzhiyun 	int ret;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	msg = kmalloc(size, GFP_KERNEL);
192*4882a593Smuzhiyun 	if (!msg)
193*4882a593Smuzhiyun 		return -ENOMEM;
194*4882a593Smuzhiyun 	msg->hdr.size = size;
195*4882a593Smuzhiyun 	msg->num_elems = num_stream_tag;
196*4882a593Smuzhiyun 	msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_DMA_REMOVE;
197*4882a593Smuzhiyun 	memcpy(&msg->stream_tag[0], stream_tag, size - sizeof(*msg));
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	ret = sof_ipc_tx_message(sdev->ipc, msg->hdr.cmd, msg, msg->hdr.size,
200*4882a593Smuzhiyun 			&reply, sizeof(reply));
201*4882a593Smuzhiyun 	kfree(msg);
202*4882a593Smuzhiyun 	return ret;
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun EXPORT_SYMBOL(sof_ipc_probe_dma_remove);
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun /**
207*4882a593Smuzhiyun  * sof_ipc_probe_points_info - retrieve list of active probe points
208*4882a593Smuzhiyun  * @sdev:	SOF sound device
209*4882a593Smuzhiyun  * @desc:	Returned list of active probes
210*4882a593Smuzhiyun  * @num_desc:	Returned count of active probes
211*4882a593Smuzhiyun  *
212*4882a593Smuzhiyun  * Host sends PROBE_POINT_INFO request to obtain list of active probe
213*4882a593Smuzhiyun  * points, valid for disconnection when given probe is no longer
214*4882a593Smuzhiyun  * required.
215*4882a593Smuzhiyun  */
sof_ipc_probe_points_info(struct snd_sof_dev * sdev,struct sof_probe_point_desc ** desc,size_t * num_desc)216*4882a593Smuzhiyun int sof_ipc_probe_points_info(struct snd_sof_dev *sdev,
217*4882a593Smuzhiyun 		struct sof_probe_point_desc **desc, size_t *num_desc)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun 	return sof_ipc_probe_info(sdev, SOF_IPC_PROBE_POINT_INFO,
220*4882a593Smuzhiyun 				 (void **)desc, num_desc);
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun EXPORT_SYMBOL(sof_ipc_probe_points_info);
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun /**
225*4882a593Smuzhiyun  * sof_ipc_probe_points_add - connect specified probes
226*4882a593Smuzhiyun  * @sdev:	SOF sound device
227*4882a593Smuzhiyun  * @desc:	List of probe points to connect
228*4882a593Smuzhiyun  * @num_desc:	Number of elements in @desc
229*4882a593Smuzhiyun  *
230*4882a593Smuzhiyun  * Dynamically connects to provided set of endpoints. Immediately
231*4882a593Smuzhiyun  * after connection is established, host must be prepared to
232*4882a593Smuzhiyun  * transfer data from or to target stream given the probing purpose.
233*4882a593Smuzhiyun  *
234*4882a593Smuzhiyun  * Each probe point should be removed using PROBE_POINT_REMOVE
235*4882a593Smuzhiyun  * request when no longer needed.
236*4882a593Smuzhiyun  */
sof_ipc_probe_points_add(struct snd_sof_dev * sdev,struct sof_probe_point_desc * desc,size_t num_desc)237*4882a593Smuzhiyun int sof_ipc_probe_points_add(struct snd_sof_dev *sdev,
238*4882a593Smuzhiyun 		struct sof_probe_point_desc *desc, size_t num_desc)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun 	struct sof_ipc_probe_point_add_params *msg;
241*4882a593Smuzhiyun 	struct sof_ipc_reply reply;
242*4882a593Smuzhiyun 	size_t size = struct_size(msg, desc, num_desc);
243*4882a593Smuzhiyun 	int ret;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	msg = kmalloc(size, GFP_KERNEL);
246*4882a593Smuzhiyun 	if (!msg)
247*4882a593Smuzhiyun 		return -ENOMEM;
248*4882a593Smuzhiyun 	msg->hdr.size = size;
249*4882a593Smuzhiyun 	msg->num_elems = num_desc;
250*4882a593Smuzhiyun 	msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_POINT_ADD;
251*4882a593Smuzhiyun 	memcpy(&msg->desc[0], desc, size - sizeof(*msg));
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	ret = sof_ipc_tx_message(sdev->ipc, msg->hdr.cmd, msg, msg->hdr.size,
254*4882a593Smuzhiyun 			&reply, sizeof(reply));
255*4882a593Smuzhiyun 	kfree(msg);
256*4882a593Smuzhiyun 	return ret;
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun EXPORT_SYMBOL(sof_ipc_probe_points_add);
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun /**
261*4882a593Smuzhiyun  * sof_ipc_probe_points_remove - disconnect specified probes
262*4882a593Smuzhiyun  * @sdev:		SOF sound device
263*4882a593Smuzhiyun  * @buffer_id:		List of probe points to disconnect
264*4882a593Smuzhiyun  * @num_buffer_id:	Number of elements in @desc
265*4882a593Smuzhiyun  *
266*4882a593Smuzhiyun  * Removes previously connected probes from list of active probe
267*4882a593Smuzhiyun  * points and frees all resources on DSP side.
268*4882a593Smuzhiyun  */
sof_ipc_probe_points_remove(struct snd_sof_dev * sdev,unsigned int * buffer_id,size_t num_buffer_id)269*4882a593Smuzhiyun int sof_ipc_probe_points_remove(struct snd_sof_dev *sdev,
270*4882a593Smuzhiyun 		unsigned int *buffer_id, size_t num_buffer_id)
271*4882a593Smuzhiyun {
272*4882a593Smuzhiyun 	struct sof_ipc_probe_point_remove_params *msg;
273*4882a593Smuzhiyun 	struct sof_ipc_reply reply;
274*4882a593Smuzhiyun 	size_t size = struct_size(msg, buffer_id, num_buffer_id);
275*4882a593Smuzhiyun 	int ret;
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	msg = kmalloc(size, GFP_KERNEL);
278*4882a593Smuzhiyun 	if (!msg)
279*4882a593Smuzhiyun 		return -ENOMEM;
280*4882a593Smuzhiyun 	msg->hdr.size = size;
281*4882a593Smuzhiyun 	msg->num_elems = num_buffer_id;
282*4882a593Smuzhiyun 	msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_POINT_REMOVE;
283*4882a593Smuzhiyun 	memcpy(&msg->buffer_id[0], buffer_id, size - sizeof(*msg));
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	ret = sof_ipc_tx_message(sdev->ipc, msg->hdr.cmd, msg, msg->hdr.size,
286*4882a593Smuzhiyun 			&reply, sizeof(reply));
287*4882a593Smuzhiyun 	kfree(msg);
288*4882a593Smuzhiyun 	return ret;
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun EXPORT_SYMBOL(sof_ipc_probe_points_remove);
291