ARM: cpu topology: Add debugfs interface for cpu_power
[cmplus.git] / drivers / base / sync.c
blobd6913f8e194e8e1d6e613ea04d72c81ac56e27ae
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/file.h>
19 #include <linux/fs.h>
20 #include <linux/kernel.h>
21 #include <linux/poll.h>
22 #include <linux/sched.h>
23 #include <linux/seq_file.h>
24 #include <linux/slab.h>
25 #include <linux/sync.h>
26 #include <linux/uaccess.h>
28 #include <linux/anon_inodes.h>
30 static void sync_fence_signal_pt(struct sync_pt *pt);
31 static int _sync_pt_has_signaled(struct sync_pt *pt);
33 static LIST_HEAD(sync_timeline_list_head);
34 static DEFINE_SPINLOCK(sync_timeline_list_lock);
36 static LIST_HEAD(sync_fence_list_head);
37 static DEFINE_SPINLOCK(sync_fence_list_lock);
39 struct sync_timeline *sync_timeline_create(const struct sync_timeline_ops *ops,
40 int size, const char *name)
42 struct sync_timeline *obj;
43 unsigned long flags;
45 if (size < sizeof(struct sync_timeline))
46 return NULL;
48 obj = kzalloc(size, GFP_KERNEL);
49 if (obj == NULL)
50 return NULL;
52 obj->ops = ops;
53 strlcpy(obj->name, name, sizeof(obj->name));
55 INIT_LIST_HEAD(&obj->child_list_head);
56 spin_lock_init(&obj->child_list_lock);
58 INIT_LIST_HEAD(&obj->active_list_head);
59 spin_lock_init(&obj->active_list_lock);
61 spin_lock_irqsave(&sync_timeline_list_lock, flags);
62 list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
63 spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
65 return obj;
68 static void sync_timeline_free(struct sync_timeline *obj)
70 unsigned long flags;
72 if (obj->ops->release_obj)
73 obj->ops->release_obj(obj);
75 spin_lock_irqsave(&sync_timeline_list_lock, flags);
76 list_del(&obj->sync_timeline_list);
77 spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
79 kfree(obj);
82 void sync_timeline_destroy(struct sync_timeline *obj)
84 unsigned long flags;
85 bool needs_freeing;
87 spin_lock_irqsave(&obj->child_list_lock, flags);
88 obj->destroyed = true;
89 needs_freeing = list_empty(&obj->child_list_head);
90 spin_unlock_irqrestore(&obj->child_list_lock, flags);
92 if (needs_freeing)
93 sync_timeline_free(obj);
94 else
95 sync_timeline_signal(obj);
98 static void sync_timeline_add_pt(struct sync_timeline *obj, struct sync_pt *pt)
100 unsigned long flags;
102 pt->parent = obj;
104 spin_lock_irqsave(&obj->child_list_lock, flags);
105 list_add_tail(&pt->child_list, &obj->child_list_head);
106 spin_unlock_irqrestore(&obj->child_list_lock, flags);
109 static void sync_timeline_remove_pt(struct sync_pt *pt)
111 struct sync_timeline *obj = pt->parent;
112 unsigned long flags;
113 bool needs_freeing;
115 spin_lock_irqsave(&obj->active_list_lock, flags);
116 if (!list_empty(&pt->active_list))
117 list_del_init(&pt->active_list);
118 spin_unlock_irqrestore(&obj->active_list_lock, flags);
120 spin_lock_irqsave(&obj->child_list_lock, flags);
121 list_del(&pt->child_list);
122 needs_freeing = obj->destroyed && list_empty(&obj->child_list_head);
123 spin_unlock_irqrestore(&obj->child_list_lock, flags);
125 if (needs_freeing)
126 sync_timeline_free(obj);
129 void sync_timeline_signal(struct sync_timeline *obj)
131 unsigned long flags;
132 LIST_HEAD(signaled_pts);
133 struct list_head *pos, *n;
135 spin_lock_irqsave(&obj->active_list_lock, flags);
137 list_for_each_safe(pos, n, &obj->active_list_head) {
138 struct sync_pt *pt =
139 container_of(pos, struct sync_pt, active_list);
141 if (_sync_pt_has_signaled(pt))
142 list_move(pos, &signaled_pts);
145 spin_unlock_irqrestore(&obj->active_list_lock, flags);
147 list_for_each_safe(pos, n, &signaled_pts) {
148 struct sync_pt *pt =
149 container_of(pos, struct sync_pt, active_list);
151 list_del_init(pos);
152 sync_fence_signal_pt(pt);
156 struct sync_pt *sync_pt_create(struct sync_timeline *parent, int size)
158 struct sync_pt *pt;
160 if (size < sizeof(struct sync_pt))
161 return NULL;
163 pt = kzalloc(size, GFP_KERNEL);
164 if (pt == NULL)
165 return NULL;
167 INIT_LIST_HEAD(&pt->active_list);
168 sync_timeline_add_pt(parent, pt);
170 return pt;
173 void sync_pt_free(struct sync_pt *pt)
175 if (pt->parent->ops->free_pt)
176 pt->parent->ops->free_pt(pt);
178 sync_timeline_remove_pt(pt);
180 kfree(pt);
183 /* call with pt->parent->active_list_lock held */
184 static int _sync_pt_has_signaled(struct sync_pt *pt)
186 int old_status = pt->status;
188 if (!pt->status)
189 pt->status = pt->parent->ops->has_signaled(pt);
191 if (!pt->status && pt->parent->destroyed)
192 pt->status = -ENOENT;
194 if (pt->status != old_status)
195 pt->timestamp = ktime_get();
197 return pt->status;
200 static struct sync_pt *sync_pt_dup(struct sync_pt *pt)
202 return pt->parent->ops->dup(pt);
205 /* Adds a sync pt to the active queue. Called when added to a fence */
206 static void sync_pt_activate(struct sync_pt *pt)
208 struct sync_timeline *obj = pt->parent;
209 unsigned long flags;
210 int err;
212 spin_lock_irqsave(&obj->active_list_lock, flags);
214 err = _sync_pt_has_signaled(pt);
215 if (err != 0)
216 goto out;
218 list_add_tail(&pt->active_list, &obj->active_list_head);
220 out:
221 spin_unlock_irqrestore(&obj->active_list_lock, flags);
224 static int sync_fence_release(struct inode *inode, struct file *file);
225 static unsigned int sync_fence_poll(struct file *file, poll_table *wait);
226 static long sync_fence_ioctl(struct file *file, unsigned int cmd,
227 unsigned long arg);
230 static const struct file_operations sync_fence_fops = {
231 .release = sync_fence_release,
232 .poll = sync_fence_poll,
233 .unlocked_ioctl = sync_fence_ioctl,
236 static struct sync_fence *sync_fence_alloc(const char *name)
238 struct sync_fence *fence;
239 unsigned long flags;
241 fence = kzalloc(sizeof(struct sync_fence), GFP_KERNEL);
242 if (fence == NULL)
243 return NULL;
245 fence->file = anon_inode_getfile("sync_fence", &sync_fence_fops,
246 fence, 0);
247 if (fence->file == NULL)
248 goto err;
250 strlcpy(fence->name, name, sizeof(fence->name));
252 INIT_LIST_HEAD(&fence->pt_list_head);
253 INIT_LIST_HEAD(&fence->waiter_list_head);
254 spin_lock_init(&fence->waiter_list_lock);
256 init_waitqueue_head(&fence->wq);
258 spin_lock_irqsave(&sync_fence_list_lock, flags);
259 list_add_tail(&fence->sync_fence_list, &sync_fence_list_head);
260 spin_unlock_irqrestore(&sync_fence_list_lock, flags);
262 return fence;
264 err:
265 kfree(fence);
266 return NULL;
269 /* TODO: implement a create which takes more that one sync_pt */
270 struct sync_fence *sync_fence_create(const char *name, struct sync_pt *pt)
272 struct sync_fence *fence;
274 if (pt->fence)
275 return NULL;
277 fence = sync_fence_alloc(name);
278 if (fence == NULL)
279 return NULL;
281 pt->fence = fence;
282 list_add(&pt->pt_list, &fence->pt_list_head);
283 sync_pt_activate(pt);
285 return fence;
288 static int sync_fence_copy_pts(struct sync_fence *dst, struct sync_fence *src)
290 struct list_head *pos;
292 list_for_each(pos, &src->pt_list_head) {
293 struct sync_pt *orig_pt =
294 container_of(pos, struct sync_pt, pt_list);
295 struct sync_pt *new_pt = sync_pt_dup(orig_pt);
297 if (new_pt == NULL)
298 return -ENOMEM;
300 new_pt->fence = dst;
301 list_add(&new_pt->pt_list, &dst->pt_list_head);
302 sync_pt_activate(new_pt);
305 return 0;
308 static void sync_fence_free_pts(struct sync_fence *fence)
310 struct list_head *pos, *n;
312 list_for_each_safe(pos, n, &fence->pt_list_head) {
313 struct sync_pt *pt = container_of(pos, struct sync_pt, pt_list);
314 sync_pt_free(pt);
318 struct sync_fence *sync_fence_fdget(int fd)
320 struct file *file = fget(fd);
322 if (file == NULL)
323 return NULL;
325 if (file->f_op != &sync_fence_fops)
326 goto err;
328 return file->private_data;
330 err:
331 fput(file);
332 return NULL;
335 void sync_fence_put(struct sync_fence *fence)
337 fput(fence->file);
340 void sync_fence_install(struct sync_fence *fence, int fd)
342 fd_install(fd, fence->file);
345 static int sync_fence_get_status(struct sync_fence *fence)
347 struct list_head *pos;
348 int status = 1;
350 list_for_each(pos, &fence->pt_list_head) {
351 struct sync_pt *pt = container_of(pos, struct sync_pt, pt_list);
352 int pt_status = pt->status;
354 if (pt_status < 0) {
355 status = pt_status;
356 break;
357 } else if (status == 1) {
358 status = pt_status;
362 return status;
365 struct sync_fence *sync_fence_merge(const char *name,
366 struct sync_fence *a, struct sync_fence *b)
368 struct sync_fence *fence;
369 int err;
371 fence = sync_fence_alloc(name);
372 if (fence == NULL)
373 return NULL;
375 err = sync_fence_copy_pts(fence, a);
376 if (err < 0)
377 goto err;
379 err = sync_fence_copy_pts(fence, b);
380 if (err < 0)
381 goto err;
383 fence->status = sync_fence_get_status(fence);
385 return fence;
386 err:
387 sync_fence_free_pts(fence);
388 kfree(fence);
389 return NULL;
392 static void sync_fence_signal_pt(struct sync_pt *pt)
394 LIST_HEAD(signaled_waiters);
395 struct sync_fence *fence = pt->fence;
396 struct list_head *pos;
397 struct list_head *n;
398 unsigned long flags;
399 int status;
401 status = sync_fence_get_status(fence);
403 spin_lock_irqsave(&fence->waiter_list_lock, flags);
405 * this should protect against two threads racing on the signaled
406 * false -> true transition
408 if (status && !fence->status) {
409 list_for_each_safe(pos, n, &fence->waiter_list_head)
410 list_move(pos, &signaled_waiters);
412 fence->status = status;
413 } else {
414 status = 0;
416 spin_unlock_irqrestore(&fence->waiter_list_lock, flags);
418 if (status) {
419 list_for_each_safe(pos, n, &signaled_waiters) {
420 struct sync_fence_waiter *waiter =
421 container_of(pos, struct sync_fence_waiter,
422 waiter_list);
424 waiter->callback(fence, waiter->callback_data);
425 list_del(pos);
426 kfree(waiter);
428 wake_up(&fence->wq);
432 int sync_fence_wait_async(struct sync_fence *fence,
433 void (*callback)(struct sync_fence *, void *data),
434 void *callback_data)
436 struct sync_fence_waiter *waiter;
437 unsigned long flags;
438 int err = 0;
440 waiter = kzalloc(sizeof(struct sync_fence_waiter), GFP_KERNEL);
441 if (waiter == NULL)
442 return -ENOMEM;
444 waiter->callback = callback;
445 waiter->callback_data = callback_data;
447 spin_lock_irqsave(&fence->waiter_list_lock, flags);
449 if (fence->status) {
450 kfree(waiter);
451 err = fence->status;
452 goto out;
455 list_add_tail(&waiter->waiter_list, &fence->waiter_list_head);
456 out:
457 spin_unlock_irqrestore(&fence->waiter_list_lock, flags);
459 return err;
462 int sync_fence_wait(struct sync_fence *fence, long timeout)
464 int err;
466 if (timeout) {
467 timeout = msecs_to_jiffies(timeout);
468 err = wait_event_interruptible_timeout(fence->wq,
469 fence->status != 0,
470 timeout);
471 } else {
472 err = wait_event_interruptible(fence->wq, fence->status != 0);
475 if (err < 0)
476 return err;
478 if (fence->status < 0)
479 return fence->status;
481 if (fence->status == 0)
482 return -ETIME;
484 return 0;
487 static int sync_fence_release(struct inode *inode, struct file *file)
489 struct sync_fence *fence = file->private_data;
490 unsigned long flags;
492 sync_fence_free_pts(fence);
494 spin_lock_irqsave(&sync_fence_list_lock, flags);
495 list_del(&fence->sync_fence_list);
496 spin_unlock_irqrestore(&sync_fence_list_lock, flags);
498 kfree(fence);
500 return 0;
503 static unsigned int sync_fence_poll(struct file *file, poll_table *wait)
505 struct sync_fence *fence = file->private_data;
507 poll_wait(file, &fence->wq, wait);
509 if (fence->status == 1)
510 return POLLIN;
511 else if (fence->status < 0)
512 return POLLERR;
513 else
514 return 0;
517 static long sync_fence_ioctl_wait(struct sync_fence *fence, unsigned long arg)
519 __u32 value;
521 if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
522 return -EFAULT;
524 return sync_fence_wait(fence, value);
527 static long sync_fence_ioctl_merge(struct sync_fence *fence, unsigned long arg)
529 int fd = get_unused_fd();
530 int err;
531 struct sync_fence *fence2, *fence3;
532 struct sync_merge_data data;
534 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
535 return -EFAULT;
537 fence2 = sync_fence_fdget(data.fd2);
538 if (fence2 == NULL) {
539 err = -ENOENT;
540 goto err_put_fd;
543 data.name[sizeof(data.name) - 1] = '\0';
544 fence3 = sync_fence_merge(data.name, fence, fence2);
545 if (fence3 == NULL) {
546 err = -ENOMEM;
547 goto err_put_fence2;
550 data.fence = fd;
551 if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
552 err = -EFAULT;
553 goto err_put_fence3;
556 sync_fence_install(fence3, fd);
557 sync_fence_put(fence2);
558 return 0;
560 err_put_fence3:
561 sync_fence_put(fence3);
563 err_put_fence2:
564 sync_fence_put(fence2);
566 err_put_fd:
567 put_unused_fd(fd);
568 return err;
571 int sync_fill_pt_info(struct sync_pt *pt, void *data, int size)
573 struct sync_pt_info *info = data;
574 int ret;
576 if (size < sizeof(struct sync_pt_info))
577 return -ENOMEM;
579 info->len = sizeof(struct sync_pt_info);
581 if (pt->parent->ops->fill_driver_data) {
582 ret = pt->parent->ops->fill_driver_data(pt, info->driver_data,
583 size - sizeof(*info));
584 if (ret < 0)
585 return ret;
587 info->len += ret;
590 strlcpy(info->obj_name, pt->parent->name, sizeof(info->obj_name));
591 strlcpy(info->driver_name, pt->parent->ops->driver_name,
592 sizeof(info->driver_name));
593 info->status = pt->status;
594 info->timestamp_ns = ktime_to_ns(pt->timestamp);
596 return info->len;
600 static long sync_fence_ioctl_fence_info(struct sync_fence *fence,
601 unsigned long arg)
603 struct sync_fence_info_data *data;
604 struct list_head *pos;
605 __u32 size;
606 __u32 len = 0;
607 int ret;
609 if (copy_from_user(&size, (void __user *)arg, sizeof(size)))
610 return -EFAULT;
612 if (size < sizeof(struct sync_fence_info_data))
613 return -EINVAL;
615 if (size > 4096)
616 size = 4096;
618 data = kzalloc(size, GFP_KERNEL);
619 if (data == NULL)
620 return -ENOMEM;
622 strlcpy(data->name, fence->name, sizeof(data->name));
623 data->status = fence->status;
624 len = sizeof(struct sync_fence_info_data);
626 list_for_each(pos, &fence->pt_list_head) {
627 struct sync_pt *pt =
628 container_of(pos, struct sync_pt, pt_list);
630 ret = sync_fill_pt_info(pt, (u8 *)data + len, size - len);
632 if (ret < 0)
633 goto out;
635 len += ret;
638 data->len = len;
640 if (copy_to_user((void __user *)arg, data, len))
641 ret = -EFAULT;
642 else
643 ret = 0;
645 out:
646 kfree(data);
648 return ret;
651 static long sync_fence_ioctl(struct file *file, unsigned int cmd,
652 unsigned long arg)
654 struct sync_fence *fence = file->private_data;
655 switch (cmd) {
656 case SYNC_IOC_WAIT:
657 return sync_fence_ioctl_wait(fence, arg);
659 case SYNC_IOC_MERGE:
660 return sync_fence_ioctl_merge(fence, arg);
662 case SYNC_IOC_FENCE_INFO:
663 return sync_fence_ioctl_fence_info(fence, arg);
665 default:
666 return -ENOTTY;
670 #ifdef CONFIG_DEBUG_FS
671 static const char *sync_status_str(int status)
673 if (status > 0)
674 return "signaled";
675 else if (status == 0)
676 return "active";
677 else
678 return "error";
681 static void sync_print_pt(struct seq_file *s, struct sync_pt *pt, bool fence)
683 int status = pt->status;
684 seq_printf(s, " %s%spt %s",
685 fence ? pt->parent->name : "",
686 fence ? "_" : "",
687 sync_status_str(status));
688 if (pt->status) {
689 struct timeval tv = ktime_to_timeval(pt->timestamp);
690 seq_printf(s, "@%ld.%06ld", tv.tv_sec, tv.tv_usec);
693 if (pt->parent->ops->print_pt) {
694 seq_printf(s, ": ");
695 pt->parent->ops->print_pt(s, pt);
698 seq_printf(s, "\n");
701 static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
703 struct list_head *pos;
704 unsigned long flags;
706 seq_printf(s, "%s %s", obj->name, obj->ops->driver_name);
708 if (obj->ops->print_obj) {
709 seq_printf(s, ": ");
710 obj->ops->print_obj(s, obj);
713 seq_printf(s, "\n");
715 spin_lock_irqsave(&obj->child_list_lock, flags);
716 list_for_each(pos, &obj->child_list_head) {
717 struct sync_pt *pt =
718 container_of(pos, struct sync_pt, child_list);
719 sync_print_pt(s, pt, false);
721 spin_unlock_irqrestore(&obj->child_list_lock, flags);
724 static void sync_print_fence(struct seq_file *s, struct sync_fence *fence)
726 struct list_head *pos;
727 unsigned long flags;
729 seq_printf(s, "%s: %s\n", fence->name, sync_status_str(fence->status));
731 list_for_each(pos, &fence->pt_list_head) {
732 struct sync_pt *pt =
733 container_of(pos, struct sync_pt, pt_list);
734 sync_print_pt(s, pt, true);
737 spin_lock_irqsave(&fence->waiter_list_lock, flags);
738 list_for_each(pos, &fence->waiter_list_head) {
739 struct sync_fence_waiter *waiter =
740 container_of(pos, struct sync_fence_waiter,
741 waiter_list);
743 seq_printf(s, "waiter %pF %p\n", waiter->callback,
744 waiter->callback_data);
746 spin_unlock_irqrestore(&fence->waiter_list_lock, flags);
749 static int sync_debugfs_show(struct seq_file *s, void *unused)
751 unsigned long flags;
752 struct list_head *pos;
754 seq_printf(s, "objs:\n--------------\n");
756 spin_lock_irqsave(&sync_timeline_list_lock, flags);
757 list_for_each(pos, &sync_timeline_list_head) {
758 struct sync_timeline *obj =
759 container_of(pos, struct sync_timeline,
760 sync_timeline_list);
762 sync_print_obj(s, obj);
763 seq_printf(s, "\n");
765 spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
767 seq_printf(s, "fences:\n--------------\n");
769 spin_lock_irqsave(&sync_fence_list_lock, flags);
770 list_for_each(pos, &sync_fence_list_head) {
771 struct sync_fence *fence =
772 container_of(pos, struct sync_fence, sync_fence_list);
774 sync_print_fence(s, fence);
775 seq_printf(s, "\n");
777 spin_unlock_irqrestore(&sync_fence_list_lock, flags);
778 return 0;
781 static int sync_debugfs_open(struct inode *inode, struct file *file)
783 return single_open(file, sync_debugfs_show, inode->i_private);
786 static const struct file_operations sync_debugfs_fops = {
787 .open = sync_debugfs_open,
788 .read = seq_read,
789 .llseek = seq_lseek,
790 .release = single_release,
793 static __init int sync_debugfs_init(void)
795 debugfs_create_file("sync", S_IRUGO, NULL, NULL, &sync_debugfs_fops);
796 return 0;
799 late_initcall(sync_debugfs_init);
801 #endif