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,enum sof_probe_info_type type)102 static int ipc3_probes_info(struct sof_client_dev *cdev, unsigned int cmd,
103 void **params, size_t *num_params,
104 enum sof_probe_info_type type)
105 {
106 size_t max_msg_size = sof_client_get_ipc_max_payload_size(cdev);
107 struct device *dev = &cdev->auxdev.dev;
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 if (type != PROBES_INFO_ACTIVE_PROBES) {
117 dev_err(dev, "%s: info type %u not supported", __func__, type);
118 return -EOPNOTSUPP;
119 }
120
121 reply = kzalloc(max_msg_size, GFP_KERNEL);
122 if (!reply)
123 return -ENOMEM;
124 msg.rhdr.hdr.size = sizeof(msg);
125 msg.rhdr.hdr.cmd = SOF_IPC_GLB_PROBE | cmd;
126
127 ret = sof_client_ipc_tx_message(cdev, &msg, reply, max_msg_size);
128 if (ret < 0 || reply->rhdr.error < 0)
129 goto exit;
130
131 if (!reply->num_elems)
132 goto exit;
133
134 if (cmd == SOF_IPC_PROBE_DMA_INFO)
135 bytes = sizeof(reply->dma[0]);
136 else
137 bytes = sizeof(reply->desc[0]);
138 bytes *= reply->num_elems;
139 *params = kmemdup(&reply->dma[0], bytes, GFP_KERNEL);
140 if (!*params) {
141 ret = -ENOMEM;
142 goto exit;
143 }
144 *num_params = reply->num_elems;
145
146 exit:
147 kfree(reply);
148 return ret;
149 }
150
151 /**
152 * ipc3_probes_points_info - retrieve list of probe points
153 * @cdev: SOF client device
154 * @desc: Returned list of active probes
155 * @num_desc: Returned count of active probes
156 * @type: Either PROBES_INFO_ACTIVE_PROBES or PROBES_INFO_AVAILABE_PROBES
157 *
158 * If type is PROBES_INFO_ACTIVE_PROBES, host sends PROBE_POINT_INFO
159 * request to obtain list of active probe points, valid for
160 * disconnection when given probe is no longer required.
161 *
162 * Type PROBES_INFO_AVAILABE_PROBES is not yet supported.
163 */
ipc3_probes_points_info(struct sof_client_dev * cdev,struct sof_probe_point_desc ** desc,size_t * num_desc,enum sof_probe_info_type type)164 static int ipc3_probes_points_info(struct sof_client_dev *cdev,
165 struct sof_probe_point_desc **desc,
166 size_t *num_desc,
167 enum sof_probe_info_type type)
168 {
169 return ipc3_probes_info(cdev, SOF_IPC_PROBE_POINT_INFO,
170 (void **)desc, num_desc, type);
171 }
172
173 /**
174 * ipc3_probes_points_add - connect specified probes
175 * @cdev: SOF client device
176 * @desc: List of probe points to connect
177 * @num_desc: Number of elements in @desc
178 *
179 * Dynamically connects to provided set of endpoints. Immediately
180 * after connection is established, host must be prepared to
181 * transfer data from or to target stream given the probing purpose.
182 *
183 * Each probe point should be removed using PROBE_POINT_REMOVE
184 * request when no longer needed.
185 */
ipc3_probes_points_add(struct sof_client_dev * cdev,struct sof_probe_point_desc * desc,size_t num_desc)186 static int ipc3_probes_points_add(struct sof_client_dev *cdev,
187 struct sof_probe_point_desc *desc,
188 size_t num_desc)
189 {
190 struct sof_ipc_probe_point_add_params *msg;
191 size_t size = struct_size(msg, desc, num_desc);
192 int ret;
193
194 msg = kmalloc(size, GFP_KERNEL);
195 if (!msg)
196 return -ENOMEM;
197 msg->hdr.size = size;
198 msg->num_elems = num_desc;
199 msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_POINT_ADD;
200 memcpy(&msg->desc[0], desc, size - sizeof(*msg));
201
202 ret = sof_client_ipc_tx_message_no_reply(cdev, msg);
203 kfree(msg);
204 return ret;
205 }
206
207 /**
208 * ipc3_probes_points_remove - disconnect specified probes
209 * @cdev: SOF client device
210 * @buffer_id: List of probe points to disconnect
211 * @num_buffer_id: Number of elements in @desc
212 *
213 * Removes previously connected probes from list of active probe
214 * points and frees all resources on DSP side.
215 */
ipc3_probes_points_remove(struct sof_client_dev * cdev,unsigned int * buffer_id,size_t num_buffer_id)216 static int ipc3_probes_points_remove(struct sof_client_dev *cdev,
217 unsigned int *buffer_id,
218 size_t num_buffer_id)
219 {
220 struct sof_ipc_probe_point_remove_params *msg;
221 size_t size = struct_size(msg, buffer_id, num_buffer_id);
222 int ret;
223
224 msg = kmalloc(size, GFP_KERNEL);
225 if (!msg)
226 return -ENOMEM;
227 msg->hdr.size = size;
228 msg->num_elems = num_buffer_id;
229 msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_POINT_REMOVE;
230 memcpy(&msg->buffer_id[0], buffer_id, size - sizeof(*msg));
231
232 ret = sof_client_ipc_tx_message_no_reply(cdev, msg);
233 kfree(msg);
234 return ret;
235 }
236
237 const struct sof_probes_ipc_ops ipc3_probe_ops = {
238 .init = ipc3_probes_init,
239 .deinit = ipc3_probes_deinit,
240 .points_info = ipc3_probes_points_info,
241 .points_add = ipc3_probes_points_add,
242 .points_remove = ipc3_probes_points_remove,
243 };
244