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