1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // Copyright(c) 2019-2022 Intel Corporation 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-audio.h" 12 #include "ipc4-priv.h" 13 #include "sof-client.h" 14 #include "sof-client-probes.h" 15 16 enum 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 26 enum 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 SOF_IPC4_PROBE_POINTS_AVAILABLE, 32 }; 33 34 struct sof_ipc4_probe_gtw_cfg { 35 u32 node_id; 36 u32 dma_buffer_size; 37 } __packed __aligned(4); 38 39 #define SOF_IPC4_PROBE_NODE_ID_INDEX(x) ((x) & GENMASK(7, 0)) 40 #define SOF_IPC4_PROBE_NODE_ID_TYPE(x) (((x) << 8) & GENMASK(12, 8)) 41 42 struct sof_ipc4_probe_cfg { 43 struct sof_ipc4_base_module_cfg base; 44 struct sof_ipc4_probe_gtw_cfg gtw_cfg; 45 } __packed __aligned(4); 46 47 enum sof_ipc4_probe_type { 48 SOF_IPC4_PROBE_TYPE_INPUT = 0, 49 SOF_IPC4_PROBE_TYPE_OUTPUT, 50 SOF_IPC4_PROBE_TYPE_INTERNAL 51 }; 52 53 #define SOF_IPC4_PROBE_TYPE_SHIFT 24 54 #define SOF_IPC4_PROBE_TYPE_MASK GENMASK(25, 24) 55 #define SOF_IPC4_PROBE_TYPE_GET(x) (((x) & SOF_IPC4_PROBE_TYPE_MASK) \ 56 >> SOF_IPC4_PROBE_TYPE_SHIFT) 57 #define SOF_IPC4_PROBE_IDX_SHIFT 26 58 #define SOF_IPC4_PROBE_IDX_MASK GENMASK(31, 26) 59 #define SOF_IPC4_PROBE_IDX_GET(x) (((x) & SOF_IPC4_PROBE_IDX_MASK) \ 60 >> SOF_IPC4_PROBE_IDX_SHIFT) 61 62 struct sof_ipc4_probe_point { 63 u32 point_id; 64 u32 purpose; 65 u32 stream_tag; 66 } __packed __aligned(4); 67 68 struct sof_ipc4_probe_info { 69 unsigned int num_elems; 70 DECLARE_FLEX_ARRAY(struct sof_ipc4_probe_point, points); 71 } __packed; 72 73 #define INVALID_PIPELINE_ID 0xFF 74 75 static const char *sof_probe_ipc4_type_string(u32 type) 76 { 77 switch (type) { 78 case SOF_IPC4_PROBE_TYPE_INPUT: 79 return "input"; 80 case SOF_IPC4_PROBE_TYPE_OUTPUT: 81 return "output"; 82 case SOF_IPC4_PROBE_TYPE_INTERNAL: 83 return "internal"; 84 default: 85 return "UNKNOWN"; 86 } 87 } 88 89 /** 90 * sof_ipc4_probe_get_module_info - Get IPC4 module info for probe module 91 * @cdev: SOF client device 92 * @return: Pointer to IPC4 probe module info 93 * 94 * Look up the IPC4 probe module info based on the hard coded uuid and 95 * store the value for the future calls. 96 */ 97 static struct sof_man4_module *sof_ipc4_probe_get_module_info(struct sof_client_dev *cdev) 98 { 99 struct sof_probes_priv *priv = cdev->data; 100 struct device *dev = &cdev->auxdev.dev; 101 static const guid_t probe_uuid = 102 GUID_INIT(0x7CAD0808, 0xAB10, 0xCD23, 103 0xEF, 0x45, 0x12, 0xAB, 0x34, 0xCD, 0x56, 0xEF); 104 105 if (!priv->ipc_priv) { 106 struct sof_ipc4_fw_module *fw_module = 107 sof_client_ipc4_find_module(cdev, &probe_uuid); 108 109 if (!fw_module) { 110 dev_err(dev, "%s: no matching uuid found", __func__); 111 return NULL; 112 } 113 114 priv->ipc_priv = &fw_module->man4_module_entry; 115 } 116 117 return (struct sof_man4_module *)priv->ipc_priv; 118 } 119 120 /** 121 * ipc4_probes_init - initialize data probing 122 * @cdev: SOF client device 123 * @stream_tag: Extractor stream tag 124 * @buffer_size: DMA buffer size to set for extractor 125 * @return: 0 on success, negative error code on error 126 * 127 * Host chooses whether extraction is supported or not by providing 128 * valid stream tag to DSP. Once specified, stream described by that 129 * tag will be tied to DSP for extraction for the entire lifetime of 130 * probe. 131 * 132 * Probing is initialized only once and each INIT request must be 133 * matched by DEINIT call. 134 */ 135 static int ipc4_probes_init(struct sof_client_dev *cdev, u32 stream_tag, 136 size_t buffer_size) 137 { 138 struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev); 139 struct sof_ipc4_msg msg; 140 struct sof_ipc4_probe_cfg cfg; 141 142 if (!mentry) 143 return -ENODEV; 144 145 memset(&cfg, '\0', sizeof(cfg)); 146 cfg.gtw_cfg.node_id = SOF_IPC4_PROBE_NODE_ID_INDEX(stream_tag - 1) | 147 SOF_IPC4_PROBE_NODE_ID_TYPE(SOF_IPC4_DMA_HDA_HOST_INPUT); 148 149 cfg.gtw_cfg.dma_buffer_size = buffer_size; 150 151 msg.primary = mentry->id; 152 msg.primary |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_INIT_INSTANCE); 153 msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); 154 msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); 155 msg.extension = SOF_IPC4_MOD_EXT_DST_MOD_INSTANCE(INVALID_PIPELINE_ID); 156 msg.extension |= SOF_IPC4_MOD_EXT_CORE_ID(0); 157 msg.extension |= SOF_IPC4_MOD_EXT_PARAM_SIZE(sizeof(cfg) / sizeof(uint32_t)); 158 159 msg.data_size = sizeof(cfg); 160 msg.data_ptr = &cfg; 161 162 return sof_client_ipc_tx_message_no_reply(cdev, &msg); 163 } 164 165 /** 166 * ipc4_probes_deinit - cleanup after data probing 167 * @cdev: SOF client device 168 * @return: 0 on success, negative error code on error 169 * 170 * Host sends DEINIT request to free previously initialized probe 171 * on DSP side once it is no longer needed. DEINIT only when there 172 * are no probes connected and with all injectors detached. 173 */ 174 static int ipc4_probes_deinit(struct sof_client_dev *cdev) 175 { 176 struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev); 177 struct sof_ipc4_msg msg; 178 179 if (!mentry) 180 return -ENODEV; 181 182 msg.primary = mentry->id; 183 msg.primary |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_DELETE_INSTANCE); 184 msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); 185 msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); 186 msg.extension = SOF_IPC4_MOD_EXT_DST_MOD_INSTANCE(INVALID_PIPELINE_ID); 187 msg.extension |= SOF_IPC4_MOD_EXT_CORE_ID(0); 188 189 msg.data_size = 0; 190 msg.data_ptr = NULL; 191 192 return sof_client_ipc_tx_message_no_reply(cdev, &msg); 193 } 194 195 /** 196 * ipc4_probes_points_info - retrieve list of probe points 197 * @cdev: SOF client device 198 * @desc: Returned list of active probes 199 * @num_desc: Returned count of active probes 200 * @type: Either PROBES_INFO_ACTIVE_PROBES or PROBES_INFO_AVAILABE_PROBES 201 * @return: 0 on success, negative error code on error 202 * 203 * Returns list if active probe points if type is 204 * PROBES_INFO_ACTIVE_PROBES, or list of all available probe points if 205 * type is PROBES_INFO_AVAILABE_PROBES. 206 */ 207 static int ipc4_probes_points_info(struct sof_client_dev *cdev, 208 struct sof_probe_point_desc **desc, 209 size_t *num_desc, 210 enum sof_probe_info_type type) 211 { 212 struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev); 213 struct device *dev = &cdev->auxdev.dev; 214 struct sof_ipc4_probe_info *info; 215 struct sof_ipc4_msg msg; 216 u32 param_id; 217 int i, ret; 218 219 if (!mentry) 220 return -ENODEV; 221 222 switch (type) { 223 case PROBES_INFO_ACTIVE_PROBES: 224 param_id = SOF_IPC4_PROBE_POINTS; 225 break; 226 case PROBES_INFO_AVAILABE_PROBES: 227 param_id = SOF_IPC4_PROBE_POINTS_AVAILABLE; 228 break; 229 default: 230 dev_err(dev, "%s: info type %u not supported", __func__, type); 231 return -EOPNOTSUPP; 232 } 233 234 msg.primary = mentry->id; 235 msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); 236 msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); 237 238 msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(param_id); 239 240 msg.data_size = sof_client_get_ipc_max_payload_size(cdev); 241 msg.data_ptr = kzalloc(msg.data_size, GFP_KERNEL); 242 if (!msg.data_ptr) 243 return -ENOMEM; 244 245 ret = sof_client_ipc_set_get_data(cdev, &msg, false); 246 if (ret) { 247 kfree(msg.data_ptr); 248 return ret; 249 } 250 info = msg.data_ptr; 251 *num_desc = info->num_elems; 252 dev_dbg(dev, "%s: got %zu probe points", __func__, *num_desc); 253 254 *desc = kzalloc(*num_desc * sizeof(**desc), GFP_KERNEL); 255 if (!*desc) { 256 kfree(msg.data_ptr); 257 return -ENOMEM; 258 } 259 260 for (i = 0; i < *num_desc; i++) { 261 (*desc)[i].buffer_id = info->points[i].point_id; 262 (*desc)[i].purpose = info->points[i].purpose; 263 (*desc)[i].stream_tag = info->points[i].stream_tag; 264 } 265 kfree(msg.data_ptr); 266 267 return 0; 268 } 269 270 /** 271 * ipc4_probes_point_print - Human readable print of probe point descriptor 272 * @cdev: SOF client device 273 * @buf: Buffer to print to 274 * @size: Available bytes in buffer 275 * @desc: Describes the probe point to print 276 * @return: Number of bytes printed or an error code (snprintf return value) 277 */ 278 static int ipc4_probes_point_print(struct sof_client_dev *cdev, char *buf, size_t size, 279 struct sof_probe_point_desc *desc) 280 { 281 struct device *dev = &cdev->auxdev.dev; 282 struct snd_sof_widget *swidget; 283 int ret; 284 285 swidget = sof_client_ipc4_find_swidget_by_id(cdev, SOF_IPC4_MOD_ID_GET(desc->buffer_id), 286 SOF_IPC4_MOD_INSTANCE_GET(desc->buffer_id)); 287 if (!swidget) 288 dev_err(dev, "%s: Failed to find widget for module %lu.%lu\n", 289 __func__, SOF_IPC4_MOD_ID_GET(desc->buffer_id), 290 SOF_IPC4_MOD_INSTANCE_GET(desc->buffer_id)); 291 292 ret = snprintf(buf, size, "%#x,%#x,%#x\t%s %s buf idx %lu %s\n", 293 desc->buffer_id, desc->purpose, desc->stream_tag, 294 swidget ? swidget->widget->name : "<unknown>", 295 sof_probe_ipc4_type_string(SOF_IPC4_PROBE_TYPE_GET(desc->buffer_id)), 296 SOF_IPC4_PROBE_IDX_GET(desc->buffer_id), 297 desc->stream_tag ? "(connected)" : ""); 298 299 return ret; 300 } 301 302 /** 303 * ipc4_probes_points_add - connect specified probes 304 * @cdev: SOF client device 305 * @desc: List of probe points to connect 306 * @num_desc: Number of elements in @desc 307 * @return: 0 on success, negative error code on error 308 * 309 * Translates the generic probe point presentation to an IPC4 310 * message to dynamically connect the provided set of endpoints. 311 */ 312 static int ipc4_probes_points_add(struct sof_client_dev *cdev, 313 struct sof_probe_point_desc *desc, 314 size_t num_desc) 315 { 316 struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev); 317 struct sof_ipc4_probe_point *points; 318 struct sof_ipc4_msg msg; 319 int i, ret; 320 321 if (!mentry) 322 return -EOPNOTSUPP; 323 324 /* The sof_probe_point_desc and sof_ipc4_probe_point structs 325 * are of same size and even the integers are the same in the 326 * same order, and similar meaning, but since there is no 327 * performance issue I wrote the conversion explicitly open for 328 * future development. 329 */ 330 points = kcalloc(num_desc, sizeof(*points), GFP_KERNEL); 331 if (!points) 332 return -ENOMEM; 333 334 for (i = 0; i < num_desc; i++) { 335 points[i].point_id = desc[i].buffer_id; 336 points[i].purpose = desc[i].purpose; 337 points[i].stream_tag = desc[i].stream_tag; 338 } 339 340 msg.primary = mentry->id; 341 msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); 342 msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); 343 344 msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_PROBE_POINTS); 345 346 msg.data_size = sizeof(*points) * num_desc; 347 msg.data_ptr = points; 348 349 ret = sof_client_ipc_set_get_data(cdev, &msg, true); 350 351 kfree(points); 352 353 return ret; 354 } 355 356 /** 357 * ipc4_probes_points_remove - disconnect specified probes 358 * @cdev: SOF client device 359 * @buffer_id: List of probe points to disconnect 360 * @num_buffer_id: Number of elements in @desc 361 * @return: 0 on success, negative error code on error 362 * 363 * Converts the generic buffer_id to IPC4 probe_point_id and remove 364 * the probe points with an IPC4 for message. 365 */ 366 static int ipc4_probes_points_remove(struct sof_client_dev *cdev, 367 unsigned int *buffer_id, size_t num_buffer_id) 368 { 369 struct sof_man4_module *mentry = sof_ipc4_probe_get_module_info(cdev); 370 struct sof_ipc4_msg msg; 371 u32 *probe_point_ids; 372 int i, ret; 373 374 if (!mentry) 375 return -ENODEV; 376 377 probe_point_ids = kcalloc(num_buffer_id, sizeof(*probe_point_ids), 378 GFP_KERNEL); 379 if (!probe_point_ids) 380 return -ENOMEM; 381 382 for (i = 0; i < num_buffer_id; i++) 383 probe_point_ids[i] = buffer_id[i]; 384 385 msg.primary = mentry->id; 386 msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); 387 msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); 388 389 msg.extension = 390 SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_PROBE_POINTS_DISCONNECT); 391 392 msg.data_size = num_buffer_id * sizeof(*probe_point_ids); 393 msg.data_ptr = probe_point_ids; 394 395 ret = sof_client_ipc_set_get_data(cdev, &msg, true); 396 397 kfree(probe_point_ids); 398 399 return ret; 400 } 401 402 const struct sof_probes_ipc_ops ipc4_probe_ops = { 403 .init = ipc4_probes_init, 404 .deinit = ipc4_probes_deinit, 405 .points_info = ipc4_probes_points_info, 406 .point_print = ipc4_probes_point_print, 407 .points_add = ipc4_probes_points_add, 408 .points_remove = ipc4_probes_points_remove, 409 }; 410