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