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