1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved. 4 // 5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com> 6 // Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com> 7 // 8 9 #include <linux/debugfs.h> 10 #include <linux/kfifo.h> 11 #include <linux/wait.h> 12 #include <linux/sched/signal.h> 13 #include <sound/soc.h> 14 #include "avs.h" 15 #include "messages.h" 16 17 static unsigned int __kfifo_fromio(struct kfifo *fifo, const void __iomem *src, unsigned int len) 18 { 19 struct __kfifo *__fifo = &fifo->kfifo; 20 unsigned int l, off; 21 22 len = min(len, kfifo_avail(fifo)); 23 off = __fifo->in & __fifo->mask; 24 l = min(len, kfifo_size(fifo) - off); 25 26 memcpy_fromio(__fifo->data + off, src, l); 27 memcpy_fromio(__fifo->data, src + l, len - l); 28 /* Make sure data copied from SRAM is visible to all CPUs. */ 29 smp_mb(); 30 __fifo->in += len; 31 32 return len; 33 } 34 35 bool avs_logging_fw(struct avs_dev *adev) 36 { 37 return kfifo_initialized(&adev->trace_fifo); 38 } 39 40 void avs_dump_fw_log(struct avs_dev *adev, const void __iomem *src, unsigned int len) 41 { 42 __kfifo_fromio(&adev->trace_fifo, src, len); 43 } 44 45 void avs_dump_fw_log_wakeup(struct avs_dev *adev, const void __iomem *src, unsigned int len) 46 { 47 avs_dump_fw_log(adev, src, len); 48 wake_up(&adev->trace_waitq); 49 } 50 51 static ssize_t fw_regs_read(struct file *file, char __user *to, size_t count, loff_t *ppos) 52 { 53 struct avs_dev *adev = file->private_data; 54 char *buf; 55 int ret; 56 57 buf = kzalloc(AVS_FW_REGS_SIZE, GFP_KERNEL); 58 if (!buf) 59 return -ENOMEM; 60 61 memcpy_fromio(buf, avs_sram_addr(adev, AVS_FW_REGS_WINDOW), AVS_FW_REGS_SIZE); 62 63 ret = simple_read_from_buffer(to, count, ppos, buf, AVS_FW_REGS_SIZE); 64 kfree(buf); 65 return ret; 66 } 67 68 static const struct file_operations fw_regs_fops = { 69 .open = simple_open, 70 .read = fw_regs_read, 71 .llseek = no_llseek, 72 }; 73 74 static ssize_t debug_window_read(struct file *file, char __user *to, size_t count, loff_t *ppos) 75 { 76 struct avs_dev *adev = file->private_data; 77 size_t size; 78 char *buf; 79 int ret; 80 81 size = adev->hw_cfg.dsp_cores * AVS_WINDOW_CHUNK_SIZE; 82 buf = kzalloc(size, GFP_KERNEL); 83 if (!buf) 84 return -ENOMEM; 85 86 memcpy_fromio(buf, avs_sram_addr(adev, AVS_DEBUG_WINDOW), size); 87 88 ret = simple_read_from_buffer(to, count, ppos, buf, size); 89 kfree(buf); 90 return ret; 91 } 92 93 static const struct file_operations debug_window_fops = { 94 .open = simple_open, 95 .read = debug_window_read, 96 .llseek = no_llseek, 97 }; 98 99 static ssize_t probe_points_read(struct file *file, char __user *to, size_t count, loff_t *ppos) 100 { 101 struct avs_dev *adev = file->private_data; 102 struct avs_probe_point_desc *desc; 103 size_t num_desc, len = 0; 104 char *buf; 105 int i, ret; 106 107 /* Prevent chaining, send and dump IPC value just once. */ 108 if (*ppos) 109 return 0; 110 111 buf = kzalloc(PAGE_SIZE, GFP_KERNEL); 112 if (!buf) 113 return -ENOMEM; 114 115 ret = avs_ipc_probe_get_points(adev, &desc, &num_desc); 116 if (ret) { 117 ret = AVS_IPC_RET(ret); 118 goto exit; 119 } 120 121 for (i = 0; i < num_desc; i++) { 122 ret = snprintf(buf + len, PAGE_SIZE - len, 123 "Id: %#010x Purpose: %d Node id: %#x\n", 124 desc[i].id.value, desc[i].purpose, desc[i].node_id.val); 125 if (ret < 0) 126 goto free_desc; 127 len += ret; 128 } 129 130 ret = simple_read_from_buffer(to, count, ppos, buf, len); 131 free_desc: 132 kfree(desc); 133 exit: 134 kfree(buf); 135 return ret; 136 } 137 138 static ssize_t probe_points_write(struct file *file, const char __user *from, size_t count, 139 loff_t *ppos) 140 { 141 struct avs_dev *adev = file->private_data; 142 struct avs_probe_point_desc *desc; 143 u32 *array, num_elems; 144 size_t bytes; 145 int ret; 146 147 ret = parse_int_array_user(from, count, (int **)&array); 148 if (ret < 0) 149 return ret; 150 151 num_elems = *array; 152 bytes = sizeof(*array) * num_elems; 153 if (bytes % sizeof(*desc)) { 154 ret = -EINVAL; 155 goto exit; 156 } 157 158 desc = (struct avs_probe_point_desc *)&array[1]; 159 ret = avs_ipc_probe_connect_points(adev, desc, bytes / sizeof(*desc)); 160 if (ret) 161 ret = AVS_IPC_RET(ret); 162 else 163 ret = count; 164 exit: 165 kfree(array); 166 return ret; 167 } 168 169 static const struct file_operations probe_points_fops = { 170 .open = simple_open, 171 .read = probe_points_read, 172 .write = probe_points_write, 173 .llseek = no_llseek, 174 }; 175 176 static ssize_t probe_points_disconnect_write(struct file *file, const char __user *from, 177 size_t count, loff_t *ppos) 178 { 179 struct avs_dev *adev = file->private_data; 180 union avs_probe_point_id *id; 181 u32 *array, num_elems; 182 size_t bytes; 183 int ret; 184 185 ret = parse_int_array_user(from, count, (int **)&array); 186 if (ret < 0) 187 return ret; 188 189 num_elems = *array; 190 bytes = sizeof(*array) * num_elems; 191 if (bytes % sizeof(*id)) { 192 ret = -EINVAL; 193 goto exit; 194 } 195 196 id = (union avs_probe_point_id *)&array[1]; 197 ret = avs_ipc_probe_disconnect_points(adev, id, bytes / sizeof(*id)); 198 if (ret) 199 ret = AVS_IPC_RET(ret); 200 else 201 ret = count; 202 exit: 203 kfree(array); 204 return ret; 205 } 206 207 static const struct file_operations probe_points_disconnect_fops = { 208 .open = simple_open, 209 .write = probe_points_disconnect_write, 210 .llseek = default_llseek, 211 }; 212 213 static ssize_t strace_read(struct file *file, char __user *to, size_t count, loff_t *ppos) 214 { 215 struct avs_dev *adev = file->private_data; 216 struct kfifo *fifo = &adev->trace_fifo; 217 unsigned int copied; 218 219 if (kfifo_is_empty(fifo)) { 220 DEFINE_WAIT(wait); 221 222 prepare_to_wait(&adev->trace_waitq, &wait, TASK_INTERRUPTIBLE); 223 if (!signal_pending(current)) 224 schedule(); 225 finish_wait(&adev->trace_waitq, &wait); 226 } 227 228 if (kfifo_to_user(fifo, to, count, &copied)) 229 return -EFAULT; 230 *ppos += copied; 231 return copied; 232 } 233 234 static int strace_open(struct inode *inode, struct file *file) 235 { 236 struct avs_dev *adev = inode->i_private; 237 int ret; 238 239 if (kfifo_initialized(&adev->trace_fifo)) 240 return -EBUSY; 241 242 ret = kfifo_alloc(&adev->trace_fifo, PAGE_SIZE, GFP_KERNEL); 243 if (ret < 0) 244 return ret; 245 246 file->private_data = adev; 247 return 0; 248 } 249 250 static int strace_release(struct inode *inode, struct file *file) 251 { 252 union avs_notify_msg msg = AVS_NOTIFICATION(LOG_BUFFER_STATUS); 253 struct avs_dev *adev = file->private_data; 254 unsigned long resource_mask; 255 unsigned long flags, i; 256 u32 num_cores; 257 258 resource_mask = adev->logged_resources; 259 num_cores = adev->hw_cfg.dsp_cores; 260 261 spin_lock_irqsave(&adev->trace_lock, flags); 262 263 /* Gather any remaining logs. */ 264 for_each_set_bit(i, &resource_mask, num_cores) { 265 msg.log.core = i; 266 avs_dsp_op(adev, log_buffer_status, &msg); 267 } 268 269 kfifo_free(&adev->trace_fifo); 270 271 spin_unlock_irqrestore(&adev->trace_lock, flags); 272 273 return 0; 274 } 275 276 static const struct file_operations strace_fops = { 277 .llseek = default_llseek, 278 .read = strace_read, 279 .open = strace_open, 280 .release = strace_release, 281 }; 282 283 #define DISABLE_TIMERS UINT_MAX 284 285 static int enable_logs(struct avs_dev *adev, u32 resource_mask, u32 *priorities) 286 { 287 int ret; 288 289 /* Logging demands D0i0 state from DSP. */ 290 if (!adev->logged_resources) { 291 pm_runtime_get_sync(adev->dev); 292 293 ret = avs_dsp_disable_d0ix(adev); 294 if (ret) 295 goto err_d0ix; 296 } 297 298 ret = avs_ipc_set_system_time(adev); 299 if (ret && ret != AVS_IPC_NOT_SUPPORTED) { 300 ret = AVS_IPC_RET(ret); 301 goto err_ipc; 302 } 303 304 ret = avs_dsp_op(adev, enable_logs, AVS_LOG_ENABLE, adev->aging_timer_period, 305 adev->fifo_full_timer_period, resource_mask, priorities); 306 if (ret) 307 goto err_ipc; 308 309 adev->logged_resources |= resource_mask; 310 return 0; 311 312 err_ipc: 313 if (!adev->logged_resources) { 314 avs_dsp_enable_d0ix(adev); 315 err_d0ix: 316 pm_runtime_mark_last_busy(adev->dev); 317 pm_runtime_put_autosuspend(adev->dev); 318 } 319 320 return ret; 321 } 322 323 static int disable_logs(struct avs_dev *adev, u32 resource_mask) 324 { 325 int ret; 326 327 /* Check if there's anything to do. */ 328 if (!adev->logged_resources) 329 return 0; 330 331 ret = avs_dsp_op(adev, enable_logs, AVS_LOG_DISABLE, DISABLE_TIMERS, DISABLE_TIMERS, 332 resource_mask, NULL); 333 334 /* 335 * If IPC fails causing recovery, logged_resources is already zero 336 * so unsetting bits is still safe. 337 */ 338 adev->logged_resources &= ~resource_mask; 339 340 /* If that's the last resource, allow for D3. */ 341 if (!adev->logged_resources) { 342 avs_dsp_enable_d0ix(adev); 343 pm_runtime_mark_last_busy(adev->dev); 344 pm_runtime_put_autosuspend(adev->dev); 345 } 346 347 return ret; 348 } 349 350 static ssize_t trace_control_read(struct file *file, char __user *to, size_t count, loff_t *ppos) 351 { 352 struct avs_dev *adev = file->private_data; 353 char buf[64]; 354 int len; 355 356 len = snprintf(buf, sizeof(buf), "0x%08x\n", adev->logged_resources); 357 358 return simple_read_from_buffer(to, count, ppos, buf, len); 359 } 360 361 static ssize_t trace_control_write(struct file *file, const char __user *from, size_t count, 362 loff_t *ppos) 363 { 364 struct avs_dev *adev = file->private_data; 365 u32 *array, num_elems; 366 u32 resource_mask; 367 int ret; 368 369 ret = parse_int_array_user(from, count, (int **)&array); 370 if (ret < 0) 371 return ret; 372 373 num_elems = *array; 374 resource_mask = array[1]; 375 376 /* 377 * Disable if just resource mask is provided - no log priority flags. 378 * 379 * Enable input format: mask, prio1, .., prioN 380 * Where 'N' equals number of bits set in the 'mask'. 381 */ 382 if (num_elems == 1) { 383 ret = disable_logs(adev, resource_mask); 384 } else { 385 if (num_elems != (hweight_long(resource_mask) + 1)) { 386 ret = -EINVAL; 387 goto free_array; 388 } 389 390 ret = enable_logs(adev, resource_mask, &array[2]); 391 } 392 393 if (!ret) 394 ret = count; 395 free_array: 396 kfree(array); 397 return ret; 398 } 399 400 static const struct file_operations trace_control_fops = { 401 .llseek = default_llseek, 402 .read = trace_control_read, 403 .write = trace_control_write, 404 .open = simple_open, 405 }; 406 407 void avs_debugfs_init(struct avs_dev *adev) 408 { 409 init_waitqueue_head(&adev->trace_waitq); 410 spin_lock_init(&adev->trace_lock); 411 412 adev->debugfs_root = debugfs_create_dir("avs", snd_soc_debugfs_root); 413 414 /* Initialize timer periods with recommended defaults. */ 415 adev->aging_timer_period = 10; 416 adev->fifo_full_timer_period = 10; 417 418 debugfs_create_file("strace", 0444, adev->debugfs_root, adev, &strace_fops); 419 debugfs_create_file("trace_control", 0644, adev->debugfs_root, adev, &trace_control_fops); 420 debugfs_create_file("fw_regs", 0444, adev->debugfs_root, adev, &fw_regs_fops); 421 debugfs_create_file("debug_window", 0444, adev->debugfs_root, adev, &debug_window_fops); 422 423 debugfs_create_u32("trace_aging_period", 0644, adev->debugfs_root, 424 &adev->aging_timer_period); 425 debugfs_create_u32("trace_fifo_full_period", 0644, adev->debugfs_root, 426 &adev->fifo_full_timer_period); 427 428 debugfs_create_file("probe_points", 0644, adev->debugfs_root, adev, &probe_points_fops); 429 debugfs_create_file("probe_points_disconnect", 0200, adev->debugfs_root, adev, 430 &probe_points_disconnect_fops); 431 } 432 433 void avs_debugfs_exit(struct avs_dev *adev) 434 { 435 debugfs_remove_recursive(adev->debugfs_root); 436 } 437