staging/android: bring struct sync_pt back
[linux-2.6/btrfs-unstable.git] / drivers / staging / android / sync_debug.c
blob703f198abe5aa0d0527039a3e79fde666840369c
1 /*
2 * drivers/base/sync.c
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>
20 #include <linux/fs.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>
29 #include <linux/sync_file.h>
30 #include <linux/types.h>
31 #include <linux/kconfig.h>
33 #include "uapi/sw_sync.h"
34 #include "sync.h"
36 #ifdef CONFIG_DEBUG_FS
38 static struct dentry *dbgfs;
40 static LIST_HEAD(sync_timeline_list_head);
41 static DEFINE_SPINLOCK(sync_timeline_list_lock);
42 static LIST_HEAD(sync_file_list_head);
43 static DEFINE_SPINLOCK(sync_file_list_lock);
45 void sync_timeline_debug_add(struct sync_timeline *obj)
47 unsigned long flags;
49 spin_lock_irqsave(&sync_timeline_list_lock, flags);
50 list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
51 spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
54 void sync_timeline_debug_remove(struct sync_timeline *obj)
56 unsigned long flags;
58 spin_lock_irqsave(&sync_timeline_list_lock, flags);
59 list_del(&obj->sync_timeline_list);
60 spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
63 void sync_file_debug_add(struct sync_file *sync_file)
65 unsigned long flags;
67 spin_lock_irqsave(&sync_file_list_lock, flags);
68 list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
69 spin_unlock_irqrestore(&sync_file_list_lock, flags);
72 void sync_file_debug_remove(struct sync_file *sync_file)
74 unsigned long flags;
76 spin_lock_irqsave(&sync_file_list_lock, flags);
77 list_del(&sync_file->sync_file_list);
78 spin_unlock_irqrestore(&sync_file_list_lock, flags);
81 static const char *sync_status_str(int status)
83 if (status == 0)
84 return "signaled";
86 if (status > 0)
87 return "active";
89 return "error";
92 static void sync_print_fence(struct seq_file *s, struct fence *fence, bool show)
94 int status = 1;
95 struct sync_timeline *parent = fence_parent(fence);
97 if (fence_is_signaled_locked(fence))
98 status = fence->status;
100 seq_printf(s, " %s%sfence %s",
101 show ? parent->name : "",
102 show ? "_" : "",
103 sync_status_str(status));
105 if (status <= 0) {
106 struct timespec64 ts64 =
107 ktime_to_timespec64(fence->timestamp);
109 seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
112 if (fence->ops->timeline_value_str &&
113 fence->ops->fence_value_str) {
114 char value[64];
115 bool success;
117 fence->ops->fence_value_str(fence, value, sizeof(value));
118 success = strlen(value);
120 if (success) {
121 seq_printf(s, ": %s", value);
123 fence->ops->timeline_value_str(fence, value,
124 sizeof(value));
126 if (strlen(value))
127 seq_printf(s, " / %s", value);
131 seq_puts(s, "\n");
134 static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
136 struct list_head *pos;
137 unsigned long flags;
139 seq_printf(s, "%s %s: %d\n", obj->name, obj->drv_name, obj->value);
141 spin_lock_irqsave(&obj->child_list_lock, flags);
142 list_for_each(pos, &obj->child_list_head) {
143 struct sync_pt *pt =
144 container_of(pos, struct sync_pt, child_list);
145 sync_print_fence(s, &pt->base, false);
147 spin_unlock_irqrestore(&obj->child_list_lock, flags);
150 static void sync_print_sync_file(struct seq_file *s,
151 struct sync_file *sync_file)
153 int i;
155 seq_printf(s, "[%p] %s: %s\n", sync_file, sync_file->name,
156 sync_status_str(atomic_read(&sync_file->status)));
158 for (i = 0; i < sync_file->num_fences; ++i)
159 sync_print_fence(s, sync_file->cbs[i].fence, true);
162 static int sync_debugfs_show(struct seq_file *s, void *unused)
164 unsigned long flags;
165 struct list_head *pos;
167 seq_puts(s, "objs:\n--------------\n");
169 spin_lock_irqsave(&sync_timeline_list_lock, flags);
170 list_for_each(pos, &sync_timeline_list_head) {
171 struct sync_timeline *obj =
172 container_of(pos, struct sync_timeline,
173 sync_timeline_list);
175 sync_print_obj(s, obj);
176 seq_puts(s, "\n");
178 spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
180 seq_puts(s, "fences:\n--------------\n");
182 spin_lock_irqsave(&sync_file_list_lock, flags);
183 list_for_each(pos, &sync_file_list_head) {
184 struct sync_file *sync_file =
185 container_of(pos, struct sync_file, sync_file_list);
187 sync_print_sync_file(s, sync_file);
188 seq_puts(s, "\n");
190 spin_unlock_irqrestore(&sync_file_list_lock, flags);
191 return 0;
194 static int sync_info_debugfs_open(struct inode *inode, struct file *file)
196 return single_open(file, sync_debugfs_show, inode->i_private);
199 static const struct file_operations sync_info_debugfs_fops = {
200 .open = sync_info_debugfs_open,
201 .read = seq_read,
202 .llseek = seq_lseek,
203 .release = single_release,
206 #if IS_ENABLED(CONFIG_SW_SYNC)
208 * *WARNING*
210 * improper use of this can result in deadlocking kernel drivers from userspace.
213 /* opening sw_sync create a new sync obj */
214 static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
216 struct sync_timeline *obj;
217 char task_comm[TASK_COMM_LEN];
219 get_task_comm(task_comm, current);
221 obj = sync_timeline_create("sw_sync", task_comm);
222 if (!obj)
223 return -ENOMEM;
225 file->private_data = obj;
227 return 0;
230 static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
232 struct sync_timeline *obj = file->private_data;
234 sync_timeline_destroy(obj);
235 return 0;
238 static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
239 unsigned long arg)
241 int fd = get_unused_fd_flags(O_CLOEXEC);
242 int err;
243 struct sync_pt *pt;
244 struct sync_file *sync_file;
245 struct sw_sync_create_fence_data data;
247 if (fd < 0)
248 return fd;
250 if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
251 err = -EFAULT;
252 goto err;
255 pt = sync_pt_create(obj, sizeof(*pt), data.value);
256 if (!pt) {
257 err = -ENOMEM;
258 goto err;
261 sync_file = sync_file_create(&pt->base);
262 if (!sync_file) {
263 fence_put(&pt->base);
264 err = -ENOMEM;
265 goto err;
268 data.fence = fd;
269 if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
270 fput(sync_file->file);
271 err = -EFAULT;
272 goto err;
275 fd_install(fd, sync_file->file);
277 return 0;
279 err:
280 put_unused_fd(fd);
281 return err;
284 static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
286 u32 value;
288 if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
289 return -EFAULT;
291 sync_timeline_signal(obj, value);
293 return 0;
296 static long sw_sync_ioctl(struct file *file, unsigned int cmd,
297 unsigned long arg)
299 struct sync_timeline *obj = file->private_data;
301 switch (cmd) {
302 case SW_SYNC_IOC_CREATE_FENCE:
303 return sw_sync_ioctl_create_fence(obj, arg);
305 case SW_SYNC_IOC_INC:
306 return sw_sync_ioctl_inc(obj, arg);
308 default:
309 return -ENOTTY;
313 static const struct file_operations sw_sync_debugfs_fops = {
314 .open = sw_sync_debugfs_open,
315 .release = sw_sync_debugfs_release,
316 .unlocked_ioctl = sw_sync_ioctl,
317 .compat_ioctl = sw_sync_ioctl,
319 #endif
321 static __init int sync_debugfs_init(void)
323 dbgfs = debugfs_create_dir("sync", NULL);
325 debugfs_create_file("info", 0444, dbgfs, NULL, &sync_info_debugfs_fops);
327 #if IS_ENABLED(CONFIG_SW_SYNC)
328 debugfs_create_file("sw_sync", 0644, dbgfs, NULL,
329 &sw_sync_debugfs_fops);
330 #endif
332 return 0;
334 late_initcall(sync_debugfs_init);
336 #define DUMP_CHUNK 256
337 static char sync_dump_buf[64 * 1024];
338 void sync_dump(void)
340 struct seq_file s = {
341 .buf = sync_dump_buf,
342 .size = sizeof(sync_dump_buf) - 1,
344 int i;
346 sync_debugfs_show(&s, NULL);
348 for (i = 0; i < s.count; i += DUMP_CHUNK) {
349 if ((s.count - i) > DUMP_CHUNK) {
350 char c = s.buf[i + DUMP_CHUNK];
352 s.buf[i + DUMP_CHUNK] = 0;
353 pr_cont("%s", s.buf + i);
354 s.buf[i + DUMP_CHUNK] = c;
355 } else {
356 s.buf[s.count] = 0;
357 pr_cont("%s", s.buf + i);
362 #endif