4 * Copyright (C) 2012 Google, Inc.
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <linux/debugfs.h>
18 #include <linux/export.h>
19 #include <linux/file.h>
21 #include <linux/kernel.h>
22 #include <linux/poll.h>
23 #include <linux/sched.h>
24 #include <linux/seq_file.h>
25 #include <linux/slab.h>
26 #include <linux/uaccess.h>
27 #include <linux/anon_inodes.h>
28 #include <linux/time64.h>
31 #ifdef CONFIG_DEBUG_FS
33 static LIST_HEAD(sync_timeline_list_head);
34 static DEFINE_SPINLOCK(sync_timeline_list_lock);
35 static LIST_HEAD(sync_fence_list_head);
36 static DEFINE_SPINLOCK(sync_fence_list_lock);
38 void sync_timeline_debug_add(struct sync_timeline *obj)
42 spin_lock_irqsave(&sync_timeline_list_lock, flags);
43 list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
44 spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
47 void sync_timeline_debug_remove(struct sync_timeline *obj)
51 spin_lock_irqsave(&sync_timeline_list_lock, flags);
52 list_del(&obj->sync_timeline_list);
53 spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
56 void sync_fence_debug_add(struct sync_fence *fence)
60 spin_lock_irqsave(&sync_fence_list_lock, flags);
61 list_add_tail(&fence->sync_fence_list, &sync_fence_list_head);
62 spin_unlock_irqrestore(&sync_fence_list_lock, flags);
65 void sync_fence_debug_remove(struct sync_fence *fence)
69 spin_lock_irqsave(&sync_fence_list_lock, flags);
70 list_del(&fence->sync_fence_list);
71 spin_unlock_irqrestore(&sync_fence_list_lock, flags);
74 static const char *sync_status_str(int status)
85 static void sync_print_pt(struct seq_file *s, struct sync_pt *pt, bool fence)
88 struct sync_timeline *parent = sync_pt_parent(pt);
90 if (fence_is_signaled_locked(&pt->base))
91 status = pt->base.status;
93 seq_printf(s, " %s%spt %s",
94 fence ? parent->name : "",
96 sync_status_str(status));
99 struct timespec64 ts64 =
100 ktime_to_timespec64(pt->base.timestamp);
102 seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
105 if (parent->ops->timeline_value_str &&
106 parent->ops->pt_value_str) {
109 parent->ops->pt_value_str(pt, value, sizeof(value));
110 seq_printf(s, ": %s", value);
112 parent->ops->timeline_value_str(parent, value,
114 seq_printf(s, " / %s", value);
121 static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
123 struct list_head *pos;
126 seq_printf(s, "%s %s", obj->name, obj->ops->driver_name);
128 if (obj->ops->timeline_value_str) {
131 obj->ops->timeline_value_str(obj, value, sizeof(value));
132 seq_printf(s, ": %s", value);
137 spin_lock_irqsave(&obj->child_list_lock, flags);
138 list_for_each(pos, &obj->child_list_head) {
140 container_of(pos, struct sync_pt, child_list);
141 sync_print_pt(s, pt, false);
143 spin_unlock_irqrestore(&obj->child_list_lock, flags);
146 static void sync_print_fence(struct seq_file *s, struct sync_fence *fence)
152 seq_printf(s, "[%p] %s: %s\n", fence, fence->name,
153 sync_status_str(atomic_read(&fence->status)));
155 for (i = 0; i < fence->num_fences; ++i) {
157 container_of(fence->cbs[i].sync_pt,
158 struct sync_pt, base);
160 sync_print_pt(s, pt, true);
163 spin_lock_irqsave(&fence->wq.lock, flags);
164 list_for_each_entry(pos, &fence->wq.task_list, task_list) {
165 struct sync_fence_waiter *waiter;
167 if (pos->func != &sync_fence_wake_up_wq)
170 waiter = container_of(pos, struct sync_fence_waiter, work);
172 seq_printf(s, "waiter %pF\n", waiter->callback);
174 spin_unlock_irqrestore(&fence->wq.lock, flags);
177 static int sync_debugfs_show(struct seq_file *s, void *unused)
180 struct list_head *pos;
182 seq_puts(s, "objs:\n--------------\n");
184 spin_lock_irqsave(&sync_timeline_list_lock, flags);
185 list_for_each(pos, &sync_timeline_list_head) {
186 struct sync_timeline *obj =
187 container_of(pos, struct sync_timeline,
190 sync_print_obj(s, obj);
193 spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
195 seq_puts(s, "fences:\n--------------\n");
197 spin_lock_irqsave(&sync_fence_list_lock, flags);
198 list_for_each(pos, &sync_fence_list_head) {
199 struct sync_fence *fence =
200 container_of(pos, struct sync_fence, sync_fence_list);
202 sync_print_fence(s, fence);
205 spin_unlock_irqrestore(&sync_fence_list_lock, flags);
209 static int sync_debugfs_open(struct inode *inode, struct file *file)
211 return single_open(file, sync_debugfs_show, inode->i_private);
214 static const struct file_operations sync_debugfs_fops = {
215 .open = sync_debugfs_open,
218 .release = single_release,
221 static __init int sync_debugfs_init(void)
223 debugfs_create_file("sync", S_IRUGO, NULL, NULL, &sync_debugfs_fops);
226 late_initcall(sync_debugfs_init);
228 #define DUMP_CHUNK 256
229 static char sync_dump_buf[64 * 1024];
232 struct seq_file s = {
233 .buf = sync_dump_buf,
234 .size = sizeof(sync_dump_buf) - 1,
238 sync_debugfs_show(&s, NULL);
240 for (i = 0; i < s.count; i += DUMP_CHUNK) {
241 if ((s.count - i) > DUMP_CHUNK) {
242 char c = s.buf[i + DUMP_CHUNK];
244 s.buf[i + DUMP_CHUNK] = 0;
245 pr_cont("%s", s.buf + i);
246 s.buf[i + DUMP_CHUNK] = c;
249 pr_cont("%s", s.buf + i);