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