1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright(c) 2021-2022 Intel Corporation
9 #include <linux/debugfs.h>
10 #include <linux/kfifo.h>
11 #include <linux/wait.h>
12 #include <linux/sched/signal.h>
13 #include <linux/string_helpers.h>
14 #include <sound/soc.h>
18 static unsigned int __kfifo_fromio(struct kfifo *fifo, const void __iomem *src, unsigned int len)
20 struct __kfifo *__fifo = &fifo->kfifo;
23 len = min(len, kfifo_avail(fifo));
24 off = __fifo->in & __fifo->mask;
25 l = min(len, kfifo_size(fifo) - off);
27 memcpy_fromio(__fifo->data + off, src, l);
28 memcpy_fromio(__fifo->data, src + l, len - l);
29 /* Make sure data copied from SRAM is visible to all CPUs. */
36 bool avs_logging_fw(struct avs_dev *adev)
38 return kfifo_initialized(&adev->trace_fifo);
41 void avs_dump_fw_log(struct avs_dev *adev, const void __iomem *src, unsigned int len)
43 __kfifo_fromio(&adev->trace_fifo, src, len);
46 void avs_dump_fw_log_wakeup(struct avs_dev *adev, const void __iomem *src, unsigned int len)
48 avs_dump_fw_log(adev, src, len);
49 wake_up(&adev->trace_waitq);
52 static ssize_t fw_regs_read(struct file *file, char __user *to, size_t count, loff_t *ppos)
54 struct avs_dev *adev = file->private_data;
58 buf = kzalloc(AVS_FW_REGS_SIZE, GFP_KERNEL);
62 memcpy_fromio(buf, avs_sram_addr(adev, AVS_FW_REGS_WINDOW), AVS_FW_REGS_SIZE);
64 ret = simple_read_from_buffer(to, count, ppos, buf, AVS_FW_REGS_SIZE);
69 static const struct file_operations fw_regs_fops = {
74 static ssize_t debug_window_read(struct file *file, char __user *to, size_t count, loff_t *ppos)
76 struct avs_dev *adev = file->private_data;
81 size = adev->hw_cfg.dsp_cores * AVS_WINDOW_CHUNK_SIZE;
82 buf = kzalloc(size, GFP_KERNEL);
86 memcpy_fromio(buf, avs_sram_addr(adev, AVS_DEBUG_WINDOW), size);
88 ret = simple_read_from_buffer(to, count, ppos, buf, size);
93 static const struct file_operations debug_window_fops = {
95 .read = debug_window_read,
98 static ssize_t probe_points_read(struct file *file, char __user *to, size_t count, loff_t *ppos)
100 struct avs_dev *adev = file->private_data;
101 struct avs_probe_point_desc *desc;
102 size_t num_desc, len = 0;
106 /* Prevent chaining, send and dump IPC value just once. */
110 buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
114 ret = avs_ipc_probe_get_points(adev, &desc, &num_desc);
116 ret = AVS_IPC_RET(ret);
120 for (i = 0; i < num_desc; i++) {
121 ret = snprintf(buf + len, PAGE_SIZE - len,
122 "Id: %#010x Purpose: %d Node id: %#x\n",
123 desc[i].id.value, desc[i].purpose, desc[i].node_id.val);
129 ret = simple_read_from_buffer(to, count, ppos, buf, len);
137 static ssize_t probe_points_write(struct file *file, const char __user *from, size_t count,
140 struct avs_dev *adev = file->private_data;
141 struct avs_probe_point_desc *desc;
142 u32 *array, num_elems;
146 ret = parse_int_array_user(from, count, (int **)&array);
151 bytes = sizeof(*array) * num_elems;
152 if (bytes % sizeof(*desc)) {
157 desc = (struct avs_probe_point_desc *)&array[1];
158 ret = avs_ipc_probe_connect_points(adev, desc, bytes / sizeof(*desc));
160 ret = AVS_IPC_RET(ret);
168 static const struct file_operations probe_points_fops = {
170 .read = probe_points_read,
171 .write = probe_points_write,
174 static ssize_t probe_points_disconnect_write(struct file *file, const char __user *from,
175 size_t count, loff_t *ppos)
177 struct avs_dev *adev = file->private_data;
178 union avs_probe_point_id *id;
179 u32 *array, num_elems;
183 ret = parse_int_array_user(from, count, (int **)&array);
188 bytes = sizeof(*array) * num_elems;
189 if (bytes % sizeof(*id)) {
194 id = (union avs_probe_point_id *)&array[1];
195 ret = avs_ipc_probe_disconnect_points(adev, id, bytes / sizeof(*id));
197 ret = AVS_IPC_RET(ret);
205 static const struct file_operations probe_points_disconnect_fops = {
207 .write = probe_points_disconnect_write,
208 .llseek = default_llseek,
211 static ssize_t strace_read(struct file *file, char __user *to, size_t count, loff_t *ppos)
213 struct avs_dev *adev = file->private_data;
214 struct kfifo *fifo = &adev->trace_fifo;
217 if (kfifo_is_empty(fifo)) {
220 prepare_to_wait(&adev->trace_waitq, &wait, TASK_INTERRUPTIBLE);
221 if (!signal_pending(current))
223 finish_wait(&adev->trace_waitq, &wait);
226 if (kfifo_to_user(fifo, to, count, &copied))
232 static int strace_open(struct inode *inode, struct file *file)
234 struct avs_dev *adev = inode->i_private;
237 if (!try_module_get(adev->dev->driver->owner))
240 if (kfifo_initialized(&adev->trace_fifo))
243 ret = kfifo_alloc(&adev->trace_fifo, PAGE_SIZE, GFP_KERNEL);
247 file->private_data = adev;
251 static int strace_release(struct inode *inode, struct file *file)
253 union avs_notify_msg msg = AVS_NOTIFICATION(LOG_BUFFER_STATUS);
254 struct avs_dev *adev = file->private_data;
255 unsigned long resource_mask;
256 unsigned long flags, i;
259 resource_mask = adev->logged_resources;
260 num_cores = adev->hw_cfg.dsp_cores;
262 spin_lock_irqsave(&adev->trace_lock, flags);
264 /* Gather any remaining logs. */
265 for_each_set_bit(i, &resource_mask, num_cores) {
267 avs_dsp_op(adev, log_buffer_status, &msg);
270 kfifo_free(&adev->trace_fifo);
272 spin_unlock_irqrestore(&adev->trace_lock, flags);
274 module_put(adev->dev->driver->owner);
278 static const struct file_operations strace_fops = {
279 .llseek = default_llseek,
282 .release = strace_release,
285 #define DISABLE_TIMERS UINT_MAX
287 static int enable_logs(struct avs_dev *adev, u32 resource_mask, u32 *priorities)
291 /* Logging demands D0i0 state from DSP. */
292 if (!adev->logged_resources) {
293 pm_runtime_get_sync(adev->dev);
295 ret = avs_dsp_disable_d0ix(adev);
300 ret = avs_ipc_set_system_time(adev);
301 if (ret && ret != AVS_IPC_NOT_SUPPORTED) {
302 ret = AVS_IPC_RET(ret);
306 ret = avs_dsp_op(adev, enable_logs, AVS_LOG_ENABLE, adev->aging_timer_period,
307 adev->fifo_full_timer_period, resource_mask, priorities);
311 adev->logged_resources |= resource_mask;
315 if (!adev->logged_resources) {
316 avs_dsp_enable_d0ix(adev);
318 pm_runtime_mark_last_busy(adev->dev);
319 pm_runtime_put_autosuspend(adev->dev);
325 static int disable_logs(struct avs_dev *adev, u32 resource_mask)
329 /* Check if there's anything to do. */
330 if (!adev->logged_resources)
333 ret = avs_dsp_op(adev, enable_logs, AVS_LOG_DISABLE, DISABLE_TIMERS, DISABLE_TIMERS,
334 resource_mask, NULL);
337 * If IPC fails causing recovery, logged_resources is already zero
338 * so unsetting bits is still safe.
340 adev->logged_resources &= ~resource_mask;
342 /* If that's the last resource, allow for D3. */
343 if (!adev->logged_resources) {
344 avs_dsp_enable_d0ix(adev);
345 pm_runtime_mark_last_busy(adev->dev);
346 pm_runtime_put_autosuspend(adev->dev);
352 static ssize_t trace_control_read(struct file *file, char __user *to, size_t count, loff_t *ppos)
354 struct avs_dev *adev = file->private_data;
358 len = snprintf(buf, sizeof(buf), "0x%08x\n", adev->logged_resources);
360 return simple_read_from_buffer(to, count, ppos, buf, len);
363 static ssize_t trace_control_write(struct file *file, const char __user *from, size_t count,
366 struct avs_dev *adev = file->private_data;
367 u32 *array, num_elems;
371 ret = parse_int_array_user(from, count, (int **)&array);
376 resource_mask = array[1];
379 * Disable if just resource mask is provided - no log priority flags.
381 * Enable input format: mask, prio1, .., prioN
382 * Where 'N' equals number of bits set in the 'mask'.
384 if (num_elems == 1) {
385 ret = disable_logs(adev, resource_mask);
387 if (num_elems != (hweight_long(resource_mask) + 1)) {
392 ret = enable_logs(adev, resource_mask, &array[2]);
402 static const struct file_operations trace_control_fops = {
403 .llseek = default_llseek,
404 .read = trace_control_read,
405 .write = trace_control_write,
409 void avs_debugfs_init(struct avs_dev *adev)
411 init_waitqueue_head(&adev->trace_waitq);
412 spin_lock_init(&adev->trace_lock);
414 adev->debugfs_root = debugfs_create_dir("avs", snd_soc_debugfs_root);
416 /* Initialize timer periods with recommended defaults. */
417 adev->aging_timer_period = 10;
418 adev->fifo_full_timer_period = 10;
420 debugfs_create_file("strace", 0444, adev->debugfs_root, adev, &strace_fops);
421 debugfs_create_file("trace_control", 0644, adev->debugfs_root, adev, &trace_control_fops);
422 debugfs_create_file("fw_regs", 0444, adev->debugfs_root, adev, &fw_regs_fops);
423 debugfs_create_file("debug_window", 0444, adev->debugfs_root, adev, &debug_window_fops);
425 debugfs_create_u32("trace_aging_period", 0644, adev->debugfs_root,
426 &adev->aging_timer_period);
427 debugfs_create_u32("trace_fifo_full_period", 0644, adev->debugfs_root,
428 &adev->fifo_full_timer_period);
430 debugfs_create_file("probe_points", 0644, adev->debugfs_root, adev, &probe_points_fops);
431 debugfs_create_file("probe_points_disconnect", 0200, adev->debugfs_root, adev,
432 &probe_points_disconnect_fops);
435 void avs_debugfs_exit(struct avs_dev *adev)
437 debugfs_remove_recursive(adev->debugfs_root);