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/file.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/seq_file.h>
23 #include <linux/slab.h>
24 #include <linux/uaccess.h>
25 #include <linux/anon_inodes.h>
29 static void sync_fence_signal_pt(struct sync_pt *pt);
30 static int _sync_pt_has_signaled(struct sync_pt *pt);
32 static LIST_HEAD(sync_timeline_list_head);
33 static DEFINE_SPINLOCK(sync_timeline_list_lock);
35 static LIST_HEAD(sync_fence_list_head);
36 static DEFINE_SPINLOCK(sync_fence_list_lock);
38 struct sync_timeline *sync_timeline_create(const struct sync_timeline_ops *ops,
39 int size, const char *name)
41 struct sync_timeline *obj;
44 if (size < sizeof(struct sync_timeline))
47 obj = kzalloc(size, GFP_KERNEL);
52 strlcpy(obj->name, name, sizeof(obj->name));
54 INIT_LIST_HEAD(&obj->child_list_head);
55 spin_lock_init(&obj->child_list_lock);
57 INIT_LIST_HEAD(&obj->active_list_head);
58 spin_lock_init(&obj->active_list_lock);
60 spin_lock_irqsave(&sync_timeline_list_lock, flags);
61 list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
62 spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
67 static void sync_timeline_free(struct sync_timeline *obj)
71 if (obj->ops->release_obj)
72 obj->ops->release_obj(obj);
74 spin_lock_irqsave(&sync_timeline_list_lock, flags);
75 list_del(&obj->sync_timeline_list);
76 spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
81 void sync_timeline_destroy(struct sync_timeline *obj)
86 spin_lock_irqsave(&obj->child_list_lock, flags);
87 obj->destroyed = true;
88 needs_freeing = list_empty(&obj->child_list_head);
89 spin_unlock_irqrestore(&obj->child_list_lock, flags);
92 sync_timeline_free(obj);
94 sync_timeline_signal(obj);
97 static void sync_timeline_add_pt(struct sync_timeline *obj, struct sync_pt *pt)
103 spin_lock_irqsave(&obj->child_list_lock, flags);
104 list_add_tail(&pt->child_list, &obj->child_list_head);
105 spin_unlock_irqrestore(&obj->child_list_lock, flags);
108 static void sync_timeline_remove_pt(struct sync_pt *pt)
110 struct sync_timeline *obj = pt->parent;
114 spin_lock_irqsave(&obj->active_list_lock, flags);
115 if (!list_empty(&pt->active_list))
116 list_del_init(&pt->active_list);
117 spin_unlock_irqrestore(&obj->active_list_lock, flags);
119 spin_lock_irqsave(&obj->child_list_lock, flags);
120 list_del(&pt->child_list);
121 needs_freeing = obj->destroyed && list_empty(&obj->child_list_head);
122 spin_unlock_irqrestore(&obj->child_list_lock, flags);
125 sync_timeline_free(obj);
128 void sync_timeline_signal(struct sync_timeline *obj)
131 LIST_HEAD(signaled_pts);
132 struct list_head *pos, *n;
134 spin_lock_irqsave(&obj->active_list_lock, flags);
136 list_for_each_safe(pos, n, &obj->active_list_head) {
138 container_of(pos, struct sync_pt, active_list);
140 if (_sync_pt_has_signaled(pt))
141 list_move(pos, &signaled_pts);
144 spin_unlock_irqrestore(&obj->active_list_lock, flags);
146 list_for_each_safe(pos, n, &signaled_pts) {
148 container_of(pos, struct sync_pt, active_list);
151 sync_fence_signal_pt(pt);
155 struct sync_pt *sync_pt_create(struct sync_timeline *parent, int size)
159 if (size < sizeof(struct sync_pt))
162 pt = kzalloc(size, GFP_KERNEL);
166 INIT_LIST_HEAD(&pt->active_list);
167 sync_timeline_add_pt(parent, pt);
172 void sync_pt_free(struct sync_pt *pt)
174 if (pt->parent->ops->free_pt)
175 pt->parent->ops->free_pt(pt);
177 sync_timeline_remove_pt(pt);
182 /* call with pt->parent->active_list_lock held */
183 static int _sync_pt_has_signaled(struct sync_pt *pt)
185 int old_status = pt->status;
188 pt->status = pt->parent->ops->has_signaled(pt);
190 if (!pt->status && pt->parent->destroyed)
191 pt->status = -ENOENT;
193 if (pt->status != old_status)
194 pt->timestamp = ktime_get();
199 static struct sync_pt *sync_pt_dup(struct sync_pt *pt)
201 return pt->parent->ops->dup(pt);
204 /* Adds a sync pt to the active queue. Called when added to a fence */
205 static void sync_pt_activate(struct sync_pt *pt)
207 struct sync_timeline *obj = pt->parent;
211 spin_lock_irqsave(&obj->active_list_lock, flags);
213 err = _sync_pt_has_signaled(pt);
217 list_add_tail(&pt->active_list, &obj->active_list_head);
220 spin_unlock_irqrestore(&obj->active_list_lock, flags);
223 static int sync_fence_release(struct inode *inode, struct file *file);
224 static long sync_fence_ioctl(struct file *file, unsigned int cmd,
228 static const struct file_operations sync_fence_fops = {
229 .release = sync_fence_release,
230 .unlocked_ioctl = sync_fence_ioctl,
233 static struct sync_fence *sync_fence_alloc(const char *name)
235 struct sync_fence *fence;
238 fence = kzalloc(sizeof(struct sync_fence), GFP_KERNEL);
242 fence->file = anon_inode_getfile("sync_fence", &sync_fence_fops,
244 if (fence->file == NULL)
247 strlcpy(fence->name, name, sizeof(fence->name));
249 INIT_LIST_HEAD(&fence->pt_list_head);
250 INIT_LIST_HEAD(&fence->waiter_list_head);
251 spin_lock_init(&fence->waiter_list_lock);
253 init_waitqueue_head(&fence->wq);
255 spin_lock_irqsave(&sync_fence_list_lock, flags);
256 list_add_tail(&fence->sync_fence_list, &sync_fence_list_head);
257 spin_unlock_irqrestore(&sync_fence_list_lock, flags);
266 /* TODO: implement a create which takes more that one sync_pt */
267 struct sync_fence *sync_fence_create(const char *name, struct sync_pt *pt)
269 struct sync_fence *fence;
274 fence = sync_fence_alloc(name);
279 list_add(&pt->pt_list, &fence->pt_list_head);
280 sync_pt_activate(pt);
285 static int sync_fence_copy_pts(struct sync_fence *dst, struct sync_fence *src)
287 struct list_head *pos;
289 list_for_each(pos, &src->pt_list_head) {
290 struct sync_pt *orig_pt =
291 container_of(pos, struct sync_pt, pt_list);
292 struct sync_pt *new_pt = sync_pt_dup(orig_pt);
298 list_add(&new_pt->pt_list, &dst->pt_list_head);
299 sync_pt_activate(new_pt);
305 static void sync_fence_free_pts(struct sync_fence *fence)
307 struct list_head *pos, *n;
309 list_for_each_safe(pos, n, &fence->pt_list_head) {
310 struct sync_pt *pt = container_of(pos, struct sync_pt, pt_list);
315 struct sync_fence *sync_fence_fdget(int fd)
317 struct file *file = fget(fd);
322 if (file->f_op != &sync_fence_fops)
325 return file->private_data;
332 void sync_fence_put(struct sync_fence *fence)
337 void sync_fence_install(struct sync_fence *fence, int fd)
339 fd_install(fd, fence->file);
342 static int sync_fence_get_status(struct sync_fence *fence)
344 struct list_head *pos;
347 list_for_each(pos, &fence->pt_list_head) {
348 struct sync_pt *pt = container_of(pos, struct sync_pt, pt_list);
349 int pt_status = pt->status;
354 } else if (status == 1) {
362 struct sync_fence *sync_fence_merge(const char *name,
363 struct sync_fence *a, struct sync_fence *b)
365 struct sync_fence *fence;
368 fence = sync_fence_alloc(name);
372 err = sync_fence_copy_pts(fence, a);
376 err = sync_fence_copy_pts(fence, b);
380 fence->status = sync_fence_get_status(fence);
384 sync_fence_free_pts(fence);
389 static void sync_fence_signal_pt(struct sync_pt *pt)
391 LIST_HEAD(signaled_waiters);
392 struct sync_fence *fence = pt->fence;
393 struct list_head *pos;
398 status = sync_fence_get_status(fence);
400 spin_lock_irqsave(&fence->waiter_list_lock, flags);
402 * this should protect against two threads racing on the signaled
403 * false -> true transition
405 if (status && !fence->status) {
406 list_for_each_safe(pos, n, &fence->waiter_list_head)
407 list_move(pos, &signaled_waiters);
409 fence->status = status;
413 spin_unlock_irqrestore(&fence->waiter_list_lock, flags);
416 list_for_each_safe(pos, n, &signaled_waiters) {
417 struct sync_fence_waiter *waiter =
418 container_of(pos, struct sync_fence_waiter,
421 waiter->callback(fence, waiter->callback_data);
429 int sync_fence_wait_async(struct sync_fence *fence,
430 void (*callback)(struct sync_fence *, void *data),
433 struct sync_fence_waiter *waiter;
437 waiter = kzalloc(sizeof(struct sync_fence_waiter), GFP_KERNEL);
441 waiter->callback = callback;
442 waiter->callback_data = callback_data;
444 spin_lock_irqsave(&fence->waiter_list_lock, flags);
452 list_add_tail(&waiter->waiter_list, &fence->waiter_list_head);
454 spin_unlock_irqrestore(&fence->waiter_list_lock, flags);
459 int sync_fence_wait(struct sync_fence *fence, long timeout)
464 timeout = msecs_to_jiffies(timeout);
465 err = wait_event_interruptible_timeout(fence->wq,
469 err = wait_event_interruptible(fence->wq, fence->status != 0);
475 if (fence->status < 0)
476 return fence->status;
478 if (fence->status == 0)
484 static int sync_fence_release(struct inode *inode, struct file *file)
486 struct sync_fence *fence = file->private_data;
489 sync_fence_free_pts(fence);
491 spin_lock_irqsave(&sync_fence_list_lock, flags);
492 list_del(&fence->sync_fence_list);
493 spin_unlock_irqrestore(&sync_fence_list_lock, flags);
500 static long sync_fence_ioctl_wait(struct sync_fence *fence, unsigned long arg)
504 if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
507 return sync_fence_wait(fence, value);
510 static long sync_fence_ioctl_merge(struct sync_fence *fence, unsigned long arg)
512 int fd = get_unused_fd();
514 struct sync_fence *fence2, *fence3;
515 struct sync_merge_data data;
517 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
520 fence2 = sync_fence_fdget(data.fd2);
521 if (fence2 == NULL) {
526 data.name[sizeof(data.name) - 1] = '\0';
527 fence3 = sync_fence_merge(data.name, fence, fence2);
528 if (fence3 == NULL) {
534 if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
539 sync_fence_install(fence3, fd);
540 sync_fence_put(fence2);
544 sync_fence_put(fence3);
547 sync_fence_put(fence2);
554 static int sync_fill_pt_info(struct sync_pt *pt, void *data, int size)
556 struct sync_pt_info *info = data;
559 if (size < sizeof(struct sync_pt_info))
562 info->len = sizeof(struct sync_pt_info);
564 if (pt->parent->ops->fill_driver_data) {
565 ret = pt->parent->ops->fill_driver_data(pt, info->driver_data,
566 size - sizeof(*info));
573 strlcpy(info->obj_name, pt->parent->name, sizeof(info->obj_name));
574 strlcpy(info->driver_name, pt->parent->ops->driver_name,
575 sizeof(info->driver_name));
576 info->status = pt->status;
577 info->timestamp_ns = ktime_to_ns(pt->timestamp);
582 static long sync_fence_ioctl_fence_info(struct sync_fence *fence,
585 struct sync_fence_info_data *data;
586 struct list_head *pos;
591 if (copy_from_user(&size, (void __user *)arg, sizeof(size)))
594 if (size < sizeof(struct sync_fence_info_data))
600 data = kzalloc(size, GFP_KERNEL);
604 strlcpy(data->name, fence->name, sizeof(data->name));
605 data->status = fence->status;
606 len = sizeof(struct sync_fence_info_data);
608 list_for_each(pos, &fence->pt_list_head) {
610 container_of(pos, struct sync_pt, pt_list);
612 ret = sync_fill_pt_info(pt, (u8 *)data + len, size - len);
622 if (copy_to_user((void __user *)arg, data, len))
633 static long sync_fence_ioctl(struct file *file, unsigned int cmd,
636 struct sync_fence *fence = file->private_data;
639 return sync_fence_ioctl_wait(fence, arg);
642 return sync_fence_ioctl_merge(fence, arg);
644 case SYNC_IOC_FENCE_INFO:
645 return sync_fence_ioctl_fence_info(fence, arg);
652 #ifdef CONFIG_DEBUG_FS
653 static const char *sync_status_str(int status)
657 else if (status == 0)
663 static void sync_print_pt(struct seq_file *s, struct sync_pt *pt, bool fence)
665 int status = pt->status;
666 seq_printf(s, " %s%spt %s",
667 fence ? pt->parent->name : "",
669 sync_status_str(status));
671 struct timeval tv = ktime_to_timeval(pt->timestamp);
672 seq_printf(s, "@%ld.%06ld", tv.tv_sec, tv.tv_usec);
675 if (pt->parent->ops->print_pt) {
677 pt->parent->ops->print_pt(s, pt);
683 static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
685 struct list_head *pos;
688 seq_printf(s, "%s %s", obj->name, obj->ops->driver_name);
690 if (obj->ops->print_obj) {
692 obj->ops->print_obj(s, obj);
697 spin_lock_irqsave(&obj->child_list_lock, flags);
698 list_for_each(pos, &obj->child_list_head) {
700 container_of(pos, struct sync_pt, child_list);
701 sync_print_pt(s, pt, false);
703 spin_unlock_irqrestore(&obj->child_list_lock, flags);
706 static void sync_print_fence(struct seq_file *s, struct sync_fence *fence)
708 struct list_head *pos;
711 seq_printf(s, "%s: %s\n", fence->name, sync_status_str(fence->status));
713 list_for_each(pos, &fence->pt_list_head) {
715 container_of(pos, struct sync_pt, pt_list);
716 sync_print_pt(s, pt, true);
719 spin_lock_irqsave(&fence->waiter_list_lock, flags);
720 list_for_each(pos, &fence->waiter_list_head) {
721 struct sync_fence_waiter *waiter =
722 container_of(pos, struct sync_fence_waiter,
725 seq_printf(s, "waiter %pF %p\n", waiter->callback,
726 waiter->callback_data);
728 spin_unlock_irqrestore(&fence->waiter_list_lock, flags);
731 static int sync_debugfs_show(struct seq_file *s, void *unused)
734 struct list_head *pos;
736 seq_printf(s, "objs:\n--------------\n");
738 spin_lock_irqsave(&sync_timeline_list_lock, flags);
739 list_for_each(pos, &sync_timeline_list_head) {
740 struct sync_timeline *obj =
741 container_of(pos, struct sync_timeline,
744 sync_print_obj(s, obj);
747 spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
749 seq_printf(s, "fences:\n--------------\n");
751 spin_lock_irqsave(&sync_fence_list_lock, flags);
752 list_for_each(pos, &sync_fence_list_head) {
753 struct sync_fence *fence =
754 container_of(pos, struct sync_fence, sync_fence_list);
756 sync_print_fence(s, fence);
759 spin_unlock_irqrestore(&sync_fence_list_lock, flags);
763 static int sync_debugfs_open(struct inode *inode, struct file *file)
765 return single_open(file, sync_debugfs_show, inode->i_private);
768 static const struct file_operations sync_debugfs_fops = {
769 .open = sync_debugfs_open,
772 .release = single_release,
775 static __init int sync_debugfs_init(void)
777 debugfs_create_file("sync", S_IRUGO, NULL, NULL, &sync_debugfs_fops);
781 late_initcall(sync_debugfs_init);