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