1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2019-2022 Intel Corporation. All rights reserved.
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 struct sof_ipc_reply reply;
69 int ret;
70
71 msg = kmalloc(size, GFP_KERNEL);
72 if (!msg)
73 return -ENOMEM;
74 msg->hdr.size = size;
75 msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_INIT;
76 msg->num_elems = 1;
77 msg->dma[0].stream_tag = stream_tag;
78 msg->dma[0].dma_buffer_size = buffer_size;
79
80 ret = sof_client_ipc_tx_message(cdev, msg, &reply, sizeof(reply));
81 kfree(msg);
82 return ret;
83 }
84
85 /**
86 * ipc3_probes_deinit - cleanup after data probing
87 * @cdev: SOF client device
88 *
89 * Host sends DEINIT request to free previously initialized probe
90 * on DSP side once it is no longer needed. DEINIT only when there
91 * are no probes connected and with all injectors detached.
92 */
ipc3_probes_deinit(struct sof_client_dev * cdev)93 static int ipc3_probes_deinit(struct sof_client_dev *cdev)
94 {
95 struct sof_ipc_cmd_hdr msg;
96 struct sof_ipc_reply reply;
97
98 msg.size = sizeof(msg);
99 msg.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_DEINIT;
100
101 return sof_client_ipc_tx_message(cdev, &msg, &reply, sizeof(reply));
102 }
103
ipc3_probes_info(struct sof_client_dev * cdev,unsigned int cmd,void ** params,size_t * num_params)104 static int ipc3_probes_info(struct sof_client_dev *cdev, unsigned int cmd,
105 void **params, size_t *num_params)
106 {
107 size_t max_msg_size = sof_client_get_ipc_max_payload_size(cdev);
108 struct sof_ipc_probe_info_params msg = {{{0}}};
109 struct sof_ipc_probe_info_params *reply;
110 size_t bytes;
111 int ret;
112
113 *params = NULL;
114 *num_params = 0;
115
116 reply = kzalloc(max_msg_size, GFP_KERNEL);
117 if (!reply)
118 return -ENOMEM;
119 msg.rhdr.hdr.size = sizeof(msg);
120 msg.rhdr.hdr.cmd = SOF_IPC_GLB_PROBE | cmd;
121
122 ret = sof_client_ipc_tx_message(cdev, &msg, reply, max_msg_size);
123 if (ret < 0 || reply->rhdr.error < 0)
124 goto exit;
125
126 if (!reply->num_elems)
127 goto exit;
128
129 if (cmd == SOF_IPC_PROBE_DMA_INFO)
130 bytes = sizeof(reply->dma[0]);
131 else
132 bytes = sizeof(reply->desc[0]);
133 bytes *= reply->num_elems;
134 *params = kmemdup(&reply->dma[0], bytes, GFP_KERNEL);
135 if (!*params) {
136 ret = -ENOMEM;
137 goto exit;
138 }
139 *num_params = reply->num_elems;
140
141 exit:
142 kfree(reply);
143 return ret;
144 }
145
146 /**
147 * ipc3_probes_points_info - retrieve list of active probe points
148 * @cdev: SOF client device
149 * @desc: Returned list of active probes
150 * @num_desc: Returned count of active probes
151 *
152 * Host sends PROBE_POINT_INFO request to obtain list of active probe
153 * points, valid for disconnection when given probe is no longer
154 * required.
155 */
ipc3_probes_points_info(struct sof_client_dev * cdev,struct sof_probe_point_desc ** desc,size_t * num_desc)156 static int ipc3_probes_points_info(struct sof_client_dev *cdev,
157 struct sof_probe_point_desc **desc,
158 size_t *num_desc)
159 {
160 return ipc3_probes_info(cdev, SOF_IPC_PROBE_POINT_INFO,
161 (void **)desc, num_desc);
162 }
163
164 /**
165 * ipc3_probes_points_add - connect specified probes
166 * @cdev: SOF client device
167 * @desc: List of probe points to connect
168 * @num_desc: Number of elements in @desc
169 *
170 * Dynamically connects to provided set of endpoints. Immediately
171 * after connection is established, host must be prepared to
172 * transfer data from or to target stream given the probing purpose.
173 *
174 * Each probe point should be removed using PROBE_POINT_REMOVE
175 * request when no longer needed.
176 */
ipc3_probes_points_add(struct sof_client_dev * cdev,struct sof_probe_point_desc * desc,size_t num_desc)177 static int ipc3_probes_points_add(struct sof_client_dev *cdev,
178 struct sof_probe_point_desc *desc,
179 size_t num_desc)
180 {
181 struct sof_ipc_probe_point_add_params *msg;
182 size_t size = struct_size(msg, desc, num_desc);
183 struct sof_ipc_reply reply;
184 int ret;
185
186 msg = kmalloc(size, GFP_KERNEL);
187 if (!msg)
188 return -ENOMEM;
189 msg->hdr.size = size;
190 msg->num_elems = num_desc;
191 msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_POINT_ADD;
192 memcpy(&msg->desc[0], desc, size - sizeof(*msg));
193
194 ret = sof_client_ipc_tx_message(cdev, msg, &reply, sizeof(reply));
195 kfree(msg);
196 return ret;
197 }
198
199 /**
200 * ipc3_probes_points_remove - disconnect specified probes
201 * @cdev: SOF client device
202 * @buffer_id: List of probe points to disconnect
203 * @num_buffer_id: Number of elements in @desc
204 *
205 * Removes previously connected probes from list of active probe
206 * points and frees all resources on DSP side.
207 */
ipc3_probes_points_remove(struct sof_client_dev * cdev,unsigned int * buffer_id,size_t num_buffer_id)208 static int ipc3_probes_points_remove(struct sof_client_dev *cdev,
209 unsigned int *buffer_id,
210 size_t num_buffer_id)
211 {
212 struct sof_ipc_probe_point_remove_params *msg;
213 size_t size = struct_size(msg, buffer_id, num_buffer_id);
214 struct sof_ipc_reply reply;
215 int ret;
216
217 msg = kmalloc(size, GFP_KERNEL);
218 if (!msg)
219 return -ENOMEM;
220 msg->hdr.size = size;
221 msg->num_elems = num_buffer_id;
222 msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_POINT_REMOVE;
223 memcpy(&msg->buffer_id[0], buffer_id, size - sizeof(*msg));
224
225 ret = sof_client_ipc_tx_message(cdev, msg, &reply, sizeof(reply));
226 kfree(msg);
227 return ret;
228 }
229
230 const struct sof_probes_ipc_ops ipc3_probe_ops = {
231 .init = ipc3_probes_init,
232 .deinit = ipc3_probes_deinit,
233 .points_info = ipc3_probes_points_info,
234 .points_add = ipc3_probes_points_add,
235 .points_remove = ipc3_probes_points_remove,
236 };
237