1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // Copyright(c) 2022 Intel Corporation 4 // 5 // Authors: Ranjani Sridharan <ranjani.sridharan@linux.intel.com> 6 // Peter Ujfalusi <peter.ujfalusi@linux.intel.com> 7 // 8 9 #include <linux/auxiliary_bus.h> 10 #include <linux/completion.h> 11 #include <linux/debugfs.h> 12 #include <linux/ktime.h> 13 #include <linux/mod_devicetable.h> 14 #include <linux/module.h> 15 #include <linux/pm_runtime.h> 16 #include <linux/slab.h> 17 #include <linux/uaccess.h> 18 #include <sound/sof/header.h> 19 20 #include "sof-client.h" 21 22 #define MAX_IPC_FLOOD_DURATION_MS 1000 23 #define MAX_IPC_FLOOD_COUNT 10000 24 #define IPC_FLOOD_TEST_RESULT_LEN 512 25 #define SOF_IPC_CLIENT_SUSPEND_DELAY_MS 3000 26 27 #define DEBUGFS_IPC_FLOOD_COUNT "ipc_flood_count" 28 #define DEBUGFS_IPC_FLOOD_DURATION "ipc_flood_duration_ms" 29 30 struct sof_ipc_flood_priv { 31 struct dentry *dfs_root; 32 struct dentry *dfs_link[2]; 33 char *buf; 34 }; 35 36 static int sof_ipc_flood_dfs_open(struct inode *inode, struct file *file) 37 { 38 struct sof_client_dev *cdev = inode->i_private; 39 int ret; 40 41 if (sof_client_get_fw_state(cdev) == SOF_FW_CRASHED) 42 return -ENODEV; 43 44 ret = debugfs_file_get(file->f_path.dentry); 45 if (unlikely(ret)) 46 return ret; 47 48 ret = simple_open(inode, file); 49 if (ret) 50 debugfs_file_put(file->f_path.dentry); 51 52 return ret; 53 } 54 55 /* 56 * helper function to perform the flood test. Only one of the two params, ipc_duration_ms 57 * or ipc_count, will be non-zero and will determine the type of test 58 */ 59 static int sof_debug_ipc_flood_test(struct sof_client_dev *cdev, 60 bool flood_duration_test, 61 unsigned long ipc_duration_ms, 62 unsigned long ipc_count) 63 { 64 struct sof_ipc_flood_priv *priv = cdev->data; 65 struct device *dev = &cdev->auxdev.dev; 66 struct sof_ipc_cmd_hdr hdr; 67 u64 min_response_time = U64_MAX; 68 ktime_t start, end, test_end; 69 u64 avg_response_time = 0; 70 u64 max_response_time = 0; 71 u64 ipc_response_time; 72 int i = 0; 73 int ret; 74 75 /* configure test IPC */ 76 hdr.cmd = SOF_IPC_GLB_TEST_MSG | SOF_IPC_TEST_IPC_FLOOD; 77 hdr.size = sizeof(hdr); 78 79 /* set test end time for duration flood test */ 80 if (flood_duration_test) 81 test_end = ktime_get_ns() + ipc_duration_ms * NSEC_PER_MSEC; 82 83 /* send test IPC's */ 84 while (1) { 85 start = ktime_get(); 86 ret = sof_client_ipc_tx_message_no_reply(cdev, &hdr); 87 end = ktime_get(); 88 89 if (ret < 0) 90 break; 91 92 /* compute min and max response times */ 93 ipc_response_time = ktime_to_ns(ktime_sub(end, start)); 94 min_response_time = min(min_response_time, ipc_response_time); 95 max_response_time = max(max_response_time, ipc_response_time); 96 97 /* sum up response times */ 98 avg_response_time += ipc_response_time; 99 i++; 100 101 /* test complete? */ 102 if (flood_duration_test) { 103 if (ktime_to_ns(end) >= test_end) 104 break; 105 } else { 106 if (i == ipc_count) 107 break; 108 } 109 } 110 111 if (ret < 0) 112 dev_err(dev, "ipc flood test failed at %d iterations\n", i); 113 114 /* return if the first IPC fails */ 115 if (!i) 116 return ret; 117 118 /* compute average response time */ 119 do_div(avg_response_time, i); 120 121 /* clear previous test output */ 122 memset(priv->buf, 0, IPC_FLOOD_TEST_RESULT_LEN); 123 124 if (!ipc_count) { 125 dev_dbg(dev, "IPC Flood test duration: %lums\n", ipc_duration_ms); 126 snprintf(priv->buf, IPC_FLOOD_TEST_RESULT_LEN, 127 "IPC Flood test duration: %lums\n", ipc_duration_ms); 128 } 129 130 dev_dbg(dev, "IPC Flood count: %d, Avg response time: %lluns\n", 131 i, avg_response_time); 132 dev_dbg(dev, "Max response time: %lluns\n", max_response_time); 133 dev_dbg(dev, "Min response time: %lluns\n", min_response_time); 134 135 /* format output string and save test results */ 136 snprintf(priv->buf + strlen(priv->buf), 137 IPC_FLOOD_TEST_RESULT_LEN - strlen(priv->buf), 138 "IPC Flood count: %d\nAvg response time: %lluns\n", 139 i, avg_response_time); 140 141 snprintf(priv->buf + strlen(priv->buf), 142 IPC_FLOOD_TEST_RESULT_LEN - strlen(priv->buf), 143 "Max response time: %lluns\nMin response time: %lluns\n", 144 max_response_time, min_response_time); 145 146 return ret; 147 } 148 149 /* 150 * Writing to the debugfs entry initiates the IPC flood test based on 151 * the IPC count or the duration specified by the user. 152 */ 153 static ssize_t sof_ipc_flood_dfs_write(struct file *file, const char __user *buffer, 154 size_t count, loff_t *ppos) 155 { 156 struct sof_client_dev *cdev = file->private_data; 157 struct device *dev = &cdev->auxdev.dev; 158 unsigned long ipc_duration_ms = 0; 159 bool flood_duration_test = false; 160 unsigned long ipc_count = 0; 161 int err; 162 char *string; 163 int ret; 164 165 if (*ppos != 0) 166 return -EINVAL; 167 168 string = kzalloc(count + 1, GFP_KERNEL); 169 if (!string) 170 return -ENOMEM; 171 172 if (copy_from_user(string, buffer, count)) { 173 ret = -EFAULT; 174 goto out; 175 } 176 177 /* 178 * write op is only supported for ipc_flood_count or 179 * ipc_flood_duration_ms debugfs entries atm. 180 * ipc_flood_count floods the DSP with the number of IPC's specified. 181 * ipc_duration_ms test floods the DSP for the time specified 182 * in the debugfs entry. 183 */ 184 if (debugfs_get_aux_num(file)) 185 flood_duration_test = true; 186 187 /* test completion criterion */ 188 if (flood_duration_test) 189 ret = kstrtoul(string, 0, &ipc_duration_ms); 190 else 191 ret = kstrtoul(string, 0, &ipc_count); 192 if (ret < 0) 193 goto out; 194 195 /* limit max duration/ipc count for flood test */ 196 if (flood_duration_test) { 197 if (!ipc_duration_ms) { 198 ret = count; 199 goto out; 200 } 201 202 /* find the minimum. min() is not used to avoid warnings */ 203 if (ipc_duration_ms > MAX_IPC_FLOOD_DURATION_MS) 204 ipc_duration_ms = MAX_IPC_FLOOD_DURATION_MS; 205 } else { 206 if (!ipc_count) { 207 ret = count; 208 goto out; 209 } 210 211 /* find the minimum. min() is not used to avoid warnings */ 212 if (ipc_count > MAX_IPC_FLOOD_COUNT) 213 ipc_count = MAX_IPC_FLOOD_COUNT; 214 } 215 216 ret = pm_runtime_resume_and_get(dev); 217 if (ret < 0 && ret != -EACCES) { 218 dev_err_ratelimited(dev, "debugfs write failed to resume %d\n", ret); 219 goto out; 220 } 221 222 /* flood test */ 223 ret = sof_debug_ipc_flood_test(cdev, flood_duration_test, 224 ipc_duration_ms, ipc_count); 225 226 pm_runtime_mark_last_busy(dev); 227 err = pm_runtime_put_autosuspend(dev); 228 if (err < 0) 229 dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", err); 230 231 /* return count if test is successful */ 232 if (ret >= 0) 233 ret = count; 234 out: 235 kfree(string); 236 return ret; 237 } 238 239 /* return the result of the last IPC flood test */ 240 static ssize_t sof_ipc_flood_dfs_read(struct file *file, char __user *buffer, 241 size_t count, loff_t *ppos) 242 { 243 struct sof_client_dev *cdev = file->private_data; 244 struct sof_ipc_flood_priv *priv = cdev->data; 245 size_t size_ret; 246 247 if (*ppos) 248 return 0; 249 250 count = min_t(size_t, count, strlen(priv->buf)); 251 size_ret = copy_to_user(buffer, priv->buf, count); 252 if (size_ret) 253 return -EFAULT; 254 255 *ppos += count; 256 return count; 257 } 258 259 static int sof_ipc_flood_dfs_release(struct inode *inode, struct file *file) 260 { 261 debugfs_file_put(file->f_path.dentry); 262 263 return 0; 264 } 265 266 static const struct file_operations sof_ipc_flood_fops = { 267 .open = sof_ipc_flood_dfs_open, 268 .read = sof_ipc_flood_dfs_read, 269 .llseek = default_llseek, 270 .write = sof_ipc_flood_dfs_write, 271 .release = sof_ipc_flood_dfs_release, 272 273 .owner = THIS_MODULE, 274 }; 275 276 /* 277 * The IPC test client creates a couple of debugfs entries that will be used 278 * flood tests. Users can write to these entries to execute the IPC flood test 279 * by specifying either the number of IPCs to flood the DSP with or the duration 280 * (in ms) for which the DSP should be flooded with test IPCs. At the 281 * end of each test, the average, min and max response times are reported back. 282 * The results of the last flood test can be accessed by reading the debugfs 283 * entries. 284 */ 285 static int sof_ipc_flood_probe(struct auxiliary_device *auxdev, 286 const struct auxiliary_device_id *id) 287 { 288 struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev); 289 struct dentry *debugfs_root = sof_client_get_debugfs_root(cdev); 290 struct device *dev = &auxdev->dev; 291 struct sof_ipc_flood_priv *priv; 292 293 /* allocate memory for client data */ 294 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 295 if (!priv) 296 return -ENOMEM; 297 298 priv->buf = devm_kmalloc(dev, IPC_FLOOD_TEST_RESULT_LEN, GFP_KERNEL); 299 if (!priv->buf) 300 return -ENOMEM; 301 302 cdev->data = priv; 303 304 /* create debugfs root folder with device name under parent SOF dir */ 305 priv->dfs_root = debugfs_create_dir(dev_name(dev), debugfs_root); 306 if (!IS_ERR_OR_NULL(priv->dfs_root)) { 307 /* create read-write ipc_flood_count debugfs entry */ 308 debugfs_create_file_aux_num(DEBUGFS_IPC_FLOOD_COUNT, 0644, 309 priv->dfs_root, cdev, 0, &sof_ipc_flood_fops); 310 311 /* create read-write ipc_flood_duration_ms debugfs entry */ 312 debugfs_create_file_aux_num(DEBUGFS_IPC_FLOOD_DURATION, 0644, 313 priv->dfs_root, cdev, 1, &sof_ipc_flood_fops); 314 315 if (auxdev->id == 0) { 316 /* 317 * Create symlinks for backwards compatibility to the 318 * first IPC flood test instance 319 */ 320 char target[100]; 321 322 snprintf(target, 100, "%s/" DEBUGFS_IPC_FLOOD_COUNT, 323 dev_name(dev)); 324 priv->dfs_link[0] = 325 debugfs_create_symlink(DEBUGFS_IPC_FLOOD_COUNT, 326 debugfs_root, target); 327 328 snprintf(target, 100, "%s/" DEBUGFS_IPC_FLOOD_DURATION, 329 dev_name(dev)); 330 priv->dfs_link[1] = 331 debugfs_create_symlink(DEBUGFS_IPC_FLOOD_DURATION, 332 debugfs_root, target); 333 } 334 } 335 336 /* enable runtime PM */ 337 pm_runtime_set_autosuspend_delay(dev, SOF_IPC_CLIENT_SUSPEND_DELAY_MS); 338 pm_runtime_use_autosuspend(dev); 339 pm_runtime_enable(dev); 340 pm_runtime_mark_last_busy(dev); 341 pm_runtime_idle(dev); 342 343 return 0; 344 } 345 346 static void sof_ipc_flood_remove(struct auxiliary_device *auxdev) 347 { 348 struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev); 349 struct sof_ipc_flood_priv *priv = cdev->data; 350 351 pm_runtime_disable(&auxdev->dev); 352 353 if (auxdev->id == 0) { 354 debugfs_remove(priv->dfs_link[0]); 355 debugfs_remove(priv->dfs_link[1]); 356 } 357 358 debugfs_remove_recursive(priv->dfs_root); 359 } 360 361 static const struct auxiliary_device_id sof_ipc_flood_client_id_table[] = { 362 { .name = "snd_sof.ipc_flood" }, 363 {}, 364 }; 365 MODULE_DEVICE_TABLE(auxiliary, sof_ipc_flood_client_id_table); 366 367 /* 368 * No need for driver pm_ops as the generic pm callbacks in the auxiliary bus 369 * type are enough to ensure that the parent SOF device resumes to bring the DSP 370 * back to D0. 371 * Driver name will be set based on KBUILD_MODNAME. 372 */ 373 static struct auxiliary_driver sof_ipc_flood_client_drv = { 374 .probe = sof_ipc_flood_probe, 375 .remove = sof_ipc_flood_remove, 376 377 .id_table = sof_ipc_flood_client_id_table, 378 }; 379 380 module_auxiliary_driver(sof_ipc_flood_client_drv); 381 382 MODULE_LICENSE("GPL"); 383 MODULE_DESCRIPTION("SOF IPC Flood Test Client Driver"); 384 MODULE_IMPORT_NS("SND_SOC_SOF_CLIENT"); 385