1// SPDX-License-Identifier: GPL-2.0-only
2//
3// Copyright(c) 2019-2022 Intel Corporation. All rights reserved.
4//
5// Author: Jyri Sarha <jyri.sarha@intel.com>
6//
7
8#include <sound/soc.h>
9#include <sound/sof/ipc4/header.h>
10#include <uapi/sound/sof/header.h>
11#include "sof-priv.h"
12#include "ipc4-priv.h"
13#include "sof-client.h"
14#include "sof-client-probes.h"
15
16enum sof_ipc4_dma_type {
17 SOF_IPC4_DMA_HDA_HOST_OUTPUT = 0,
18 SOF_IPC4_DMA_HDA_HOST_INPUT = 1,
19 SOF_IPC4_DMA_HDA_LINK_OUTPUT = 8,
20 SOF_IPC4_DMA_HDA_LINK_INPUT = 9,
21 SOF_IPC4_DMA_DMIC_LINK_INPUT = 11,
22 SOF_IPC4_DMA_I2S_LINK_OUTPUT = 12,
23 SOF_IPC4_DMA_I2S_LINK_INPUT = 13,
24};
25
26enum sof_ipc4_probe_runtime_param {
27 SOF_IPC4_PROBE_INJECTION_DMA = 1,
28 SOF_IPC4_PROBE_INJECTION_DMA_DETACH,
29 SOF_IPC4_PROBE_POINTS,
30 SOF_IPC4_PROBE_POINTS_DISCONNECT,
31};
32
33struct sof_ipc4_probe_gtw_cfg {
34 u32 node_id;
35 u32 dma_buffer_size;
36} __packed __aligned(4);
37
38#define SOF_IPC4_PROBE_NODE_ID_INDEX(x) ((x) & GENMASK(7, 0))
39#define SOF_IPC4_PROBE_NODE_ID_TYPE(x) (((x) << 8) & GENMASK(12, 8))
40
41struct sof_ipc4_probe_cfg {
42 struct sof_ipc4_base_module_cfg base;
43 struct sof_ipc4_probe_gtw_cfg gtw_cfg;
44} __packed __aligned(4);
45
46enum sof_ipc4_probe_type {
47 SOF_IPC4_PROBE_TYPE_INPUT = 0,
48 SOF_IPC4_PROBE_TYPE_OUTPUT,
49 SOF_IPC4_PROBE_TYPE_INTERNAL
50};
51
52struct sof_ipc4_probe_point {
53 u32 point_id;
54 u32 purpose;
55 u32 stream_tag;
56} __packed __aligned(4);
57
58#define INVALID_PIPELINE_ID 0xFF
59
60/**
61 * sof_ipc4_probe_get_module_info - Get IPC4 module info for probe module
62 * @cdev: SOF client device
63 * @return: Pointer to IPC4 probe module info
64 *
65 * Look up the IPC4 probe module info based on the hard coded uuid and
66 * store the value for the future calls.
67 */
68static struct sof_man4_module *sof_ipc4_probe_get_module_info(struct sof_client_dev *cdev)
69{
70 struct sof_probes_priv *priv = cdev->data;
71 struct device *dev = &cdev->auxdev.dev;
72 static const guid_t probe_uuid =
73 GUID_INIT(0x7CAD0808, 0xAB10, 0xCD23,
74 0xEF, 0x45, 0x12, 0xAB, 0x34, 0xCD, 0x56, 0xEF);
75
76 if (!priv->ipc_priv) {
77 struct sof_ipc4_fw_module *fw_module =
78 sof_client_ipc4_find_module(c: cdev, u: &probe_uuid);
79
80 if (!fw_module) {
81 dev_err(dev, "%s: no matching uuid found", __func__);
82 return NULL;
83 }
84
85 priv->ipc_priv = &fw_module->man4_module_entry;
86 }
87
88 return (struct sof_man4_module *)priv->ipc_priv;
89}
90
91/**
92 * ipc4_probes_init - initialize data probing
93 * @cdev: SOF client device
94 * @stream_tag: Extractor stream tag
95 * @buffer_size: DMA buffer size to set for extractor
96 * @return: 0 on success, negative error code on error
97 *
98 * Host chooses whether extraction is supported or not by providing
99 * valid stream tag to DSP. Once specified, stream described by that
100 * tag will be tied to DSP for extraction for the entire lifetime of
101 * probe.
102 *
103 * Probing is initialized only once and each INIT request must be
104 * matched by DEINIT call.
105 */
106static int ipc4_probes_init(struct sof_client_dev *cdev, u32 stream_tag,
107 size_t buffer_size)
108{
109 struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev);
110 struct sof_ipc4_msg msg;
111 struct sof_ipc4_probe_cfg cfg;
112
113 if (!mentry)
114 return -ENODEV;
115
116 memset(&cfg, '\0', sizeof(cfg));
117 cfg.gtw_cfg.node_id = SOF_IPC4_PROBE_NODE_ID_INDEX(stream_tag - 1) |
118 SOF_IPC4_PROBE_NODE_ID_TYPE(SOF_IPC4_DMA_HDA_HOST_INPUT);
119
120 cfg.gtw_cfg.dma_buffer_size = buffer_size;
121
122 msg.primary = mentry->id;
123 msg.primary |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_INIT_INSTANCE);
124 msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
125 msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
126 msg.extension = SOF_IPC4_MOD_EXT_DST_MOD_INSTANCE(INVALID_PIPELINE_ID);
127 msg.extension |= SOF_IPC4_MOD_EXT_CORE_ID(0);
128
129 msg.data_size = sizeof(cfg);
130 msg.data_ptr = &cfg;
131
132 return sof_client_ipc_tx_message_no_reply(cdev, ipc_msg: &msg);
133}
134
135/**
136 * ipc4_probes_deinit - cleanup after data probing
137 * @cdev: SOF client device
138 * @return: 0 on success, negative error code on error
139 *
140 * Host sends DEINIT request to free previously initialized probe
141 * on DSP side once it is no longer needed. DEINIT only when there
142 * are no probes connected and with all injectors detached.
143 */
144static int ipc4_probes_deinit(struct sof_client_dev *cdev)
145{
146 struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev);
147 struct sof_ipc4_msg msg;
148
149 if (!mentry)
150 return -ENODEV;
151
152 msg.primary = mentry->id;
153 msg.primary |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_DELETE_INSTANCE);
154 msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
155 msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
156 msg.extension = SOF_IPC4_MOD_EXT_DST_MOD_INSTANCE(INVALID_PIPELINE_ID);
157 msg.extension |= SOF_IPC4_MOD_EXT_CORE_ID(0);
158
159 msg.data_size = 0;
160 msg.data_ptr = NULL;
161
162 return sof_client_ipc_tx_message_no_reply(cdev, ipc_msg: &msg);
163}
164
165/**
166 * ipc4_probes_points_info - retrieve list of active probe points
167 * @cdev: SOF client device
168 * @desc: Returned list of active probes
169 * @num_desc: Returned count of active probes
170 * @return: 0 on success, negative error code on error
171 *
172 * Dummy implementation returning empty list of probes.
173 */
174static int ipc4_probes_points_info(struct sof_client_dev *cdev,
175 struct sof_probe_point_desc **desc,
176 size_t *num_desc)
177{
178 /* TODO: Firmware side implementation needed first */
179 *desc = NULL;
180 *num_desc = 0;
181 return 0;
182}
183
184/**
185 * ipc4_probes_points_add - connect specified probes
186 * @cdev: SOF client device
187 * @desc: List of probe points to connect
188 * @num_desc: Number of elements in @desc
189 * @return: 0 on success, negative error code on error
190 *
191 * Translates the generic probe point presentation to an IPC4
192 * message to dynamically connect the provided set of endpoints.
193 */
194static int ipc4_probes_points_add(struct sof_client_dev *cdev,
195 struct sof_probe_point_desc *desc,
196 size_t num_desc)
197{
198 struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev);
199 struct sof_ipc4_probe_point *points;
200 struct sof_ipc4_msg msg;
201 int i, ret;
202
203 if (!mentry)
204 return -ENODEV;
205
206 /* The sof_probe_point_desc and sof_ipc4_probe_point structs
207 * are of same size and even the integers are the same in the
208 * same order, and similar meaning, but since there is no
209 * performance issue I wrote the conversion explicitly open for
210 * future development.
211 */
212 points = kcalloc(n: num_desc, size: sizeof(*points), GFP_KERNEL);
213 if (!points)
214 return -ENOMEM;
215
216 for (i = 0; i < num_desc; i++) {
217 points[i].point_id = desc[i].buffer_id;
218 points[i].purpose = desc[i].purpose;
219 points[i].stream_tag = desc[i].stream_tag;
220 }
221
222 msg.primary = mentry->id;
223 msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
224 msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
225
226 msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_PROBE_POINTS);
227
228 msg.data_size = sizeof(*points) * num_desc;
229 msg.data_ptr = points;
230
231 ret = sof_client_ipc_set_get_data(cdev, ipc_msg: &msg, set: true);
232
233 kfree(objp: points);
234
235 return ret;
236}
237
238/**
239 * ipc4_probes_points_remove - disconnect specified probes
240 * @cdev: SOF client device
241 * @buffer_id: List of probe points to disconnect
242 * @num_buffer_id: Number of elements in @desc
243 * @return: 0 on success, negative error code on error
244 *
245 * Converts the generic buffer_id to IPC4 probe_point_id and remove
246 * the probe points with an IPC4 for message.
247 */
248static int ipc4_probes_points_remove(struct sof_client_dev *cdev,
249 unsigned int *buffer_id, size_t num_buffer_id)
250{
251 struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev);
252 struct sof_ipc4_msg msg;
253 u32 *probe_point_ids;
254 int i, ret;
255
256 if (!mentry)
257 return -ENODEV;
258
259 probe_point_ids = kcalloc(n: num_buffer_id, size: sizeof(*probe_point_ids),
260 GFP_KERNEL);
261 if (!probe_point_ids)
262 return -ENOMEM;
263
264 for (i = 0; i < num_buffer_id; i++)
265 probe_point_ids[i] = buffer_id[i];
266
267 msg.primary = mentry->id;
268 msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
269 msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
270
271 msg.extension =
272 SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_PROBE_POINTS_DISCONNECT);
273
274 msg.data_size = num_buffer_id * sizeof(*probe_point_ids);
275 msg.data_ptr = probe_point_ids;
276
277 ret = sof_client_ipc_set_get_data(cdev, ipc_msg: &msg, set: true);
278
279 kfree(objp: probe_point_ids);
280
281 return ret;
282}
283
284const struct sof_probes_ipc_ops ipc4_probe_ops = {
285 .init = ipc4_probes_init,
286 .deinit = ipc4_probes_deinit,
287 .points_info = ipc4_probes_points_info,
288 .points_add = ipc4_probes_points_add,
289 .points_remove = ipc4_probes_points_remove,
290};
291

source code of linux/sound/soc/sof/sof-client-probes-ipc4.c