sw_sync.c revision cebed3b1d7eaee7fb79e2c510a0da4296db043c8
1/*
2 * drivers/base/sw_sync.c
3 *
4 * Copyright (C) 2012 Google, Inc.
5 *
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.
9 *
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.
14 *
15 */
16
17#include <linux/kernel.h>
18#include <linux/file.h>
19#include <linux/fs.h>
20#include <linux/miscdevice.h>
21#include <linux/module.h>
22#include <linux/syscalls.h>
23#include <linux/uaccess.h>
24
25#include "sw_sync.h"
26
27static int sw_sync_cmp(u32 a, u32 b)
28{
29	if (a == b)
30		return 0;
31
32	return ((s32)a - (s32)b) < 0 ? -1 : 1;
33}
34
35struct sync_pt *sw_sync_pt_create(struct sw_sync_timeline *obj, u32 value)
36{
37	struct sw_sync_pt *pt;
38
39	pt = (struct sw_sync_pt *)
40		sync_pt_create(&obj->obj, sizeof(struct sw_sync_pt));
41
42	pt->value = value;
43
44	return (struct sync_pt *)pt;
45}
46
47static struct sync_pt *sw_sync_pt_dup(struct sync_pt *sync_pt)
48{
49	struct sw_sync_pt *pt = (struct sw_sync_pt *) sync_pt;
50	struct sw_sync_timeline *obj =
51		(struct sw_sync_timeline *)sync_pt->parent;
52
53	return (struct sync_pt *) sw_sync_pt_create(obj, pt->value);
54}
55
56static int sw_sync_pt_has_signaled(struct sync_pt *sync_pt)
57{
58	struct sw_sync_pt *pt = (struct sw_sync_pt *)sync_pt;
59	struct sw_sync_timeline *obj =
60		(struct sw_sync_timeline *)sync_pt->parent;
61
62	return sw_sync_cmp(obj->value, pt->value) >= 0;
63}
64
65static int sw_sync_pt_compare(struct sync_pt *a, struct sync_pt *b)
66{
67	struct sw_sync_pt *pt_a = (struct sw_sync_pt *)a;
68	struct sw_sync_pt *pt_b = (struct sw_sync_pt *)b;
69
70	return sw_sync_cmp(pt_a->value, pt_b->value);
71}
72
73static void sw_sync_print_obj(struct seq_file *s,
74			      struct sync_timeline *sync_timeline)
75{
76	struct sw_sync_timeline *obj = (struct sw_sync_timeline *)sync_timeline;
77
78	seq_printf(s, "%d", obj->value);
79}
80
81static void sw_sync_print_pt(struct seq_file *s, struct sync_pt *sync_pt)
82{
83	struct sw_sync_pt *pt = (struct sw_sync_pt *)sync_pt;
84	struct sw_sync_timeline *obj =
85		(struct sw_sync_timeline *)sync_pt->parent;
86
87	seq_printf(s, "%d / %d", pt->value, obj->value);
88}
89
90struct sync_timeline_ops sw_sync_timeline_ops = {
91	.driver_name = "sw_sync",
92	.dup = sw_sync_pt_dup,
93	.has_signaled = sw_sync_pt_has_signaled,
94	.compare = sw_sync_pt_compare,
95	.print_obj = sw_sync_print_obj,
96	.print_pt = sw_sync_print_pt,
97};
98
99
100struct sw_sync_timeline *sw_sync_timeline_create(const char *name)
101{
102	struct sw_sync_timeline *obj = (struct sw_sync_timeline *)
103		sync_timeline_create(&sw_sync_timeline_ops,
104				     sizeof(struct sw_sync_timeline),
105				     name);
106
107	return obj;
108}
109
110void sw_sync_timeline_inc(struct sw_sync_timeline *obj, u32 inc)
111{
112	obj->value += inc;
113
114	sync_timeline_signal(&obj->obj);
115}
116
117
118#ifdef CONFIG_SW_SYNC_USER
119/* *WARNING*
120 *
121 * improper use of this can result in deadlocking kernel drivers from userspace.
122 */
123
124/* opening sw_sync create a new sync obj */
125int sw_sync_open(struct inode *inode, struct file *file)
126{
127	struct sw_sync_timeline *obj;
128	char task_comm[TASK_COMM_LEN];
129
130	get_task_comm(task_comm, current);
131
132	obj = sw_sync_timeline_create(task_comm);
133	if (obj == NULL)
134		return -ENOMEM;
135
136	file->private_data = obj;
137
138	return 0;
139}
140
141int sw_sync_release(struct inode *inode, struct file *file)
142{
143	struct sw_sync_timeline *obj = file->private_data;
144	sync_timeline_destroy(&obj->obj);
145	return 0;
146}
147
148long sw_sync_ioctl_create_fence(struct sw_sync_timeline *obj, unsigned long arg)
149{
150	int fd = get_unused_fd();
151	int err;
152	struct sync_pt *pt;
153	struct sync_fence *fence;
154	struct sw_sync_create_fence_data data;
155
156	if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
157		return -EFAULT;
158
159	pt = sw_sync_pt_create(obj, data.value);
160	if (pt == NULL) {
161		err = -ENOMEM;
162		goto err;
163	}
164
165	data.name[sizeof(data.name) - 1] = '\0';
166	fence = sync_fence_create(data.name, pt);
167	if (fence == NULL) {
168		sync_pt_free(pt);
169		err = -ENOMEM;
170		goto err;
171	}
172
173	data.fence = fd;
174	if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
175		sync_fence_put(fence);
176		err = -EFAULT;
177		goto err;
178	}
179
180	sync_fence_install(fence, fd);
181
182	return 0;
183
184err:
185	put_unused_fd(fd);
186	return err;
187}
188
189long sw_sync_ioctl_inc(struct sw_sync_timeline *obj, unsigned long arg)
190{
191	u32 value;
192
193	if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
194		return -EFAULT;
195
196	sw_sync_timeline_inc(obj, value);
197
198	return 0;
199}
200
201long sw_sync_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
202{
203	struct sw_sync_timeline *obj = file->private_data;
204
205	switch (cmd) {
206	case SW_SYNC_IOC_CREATE_FENCE:
207		return sw_sync_ioctl_create_fence(obj, arg);
208
209	case SW_SYNC_IOC_INC:
210		return sw_sync_ioctl_inc(obj, arg);
211
212	default:
213		return -ENOTTY;
214	}
215}
216
217static const struct file_operations sw_sync_fops = {
218	.owner = THIS_MODULE,
219	.open = sw_sync_open,
220	.release = sw_sync_release,
221	.unlocked_ioctl = sw_sync_ioctl,
222};
223
224static struct miscdevice sw_sync_dev = {
225	.minor	= MISC_DYNAMIC_MINOR,
226	.name	= "sw_sync",
227	.fops	= &sw_sync_fops,
228};
229
230int __init sw_sync_device_init(void)
231{
232	return misc_register(&sw_sync_dev);
233}
234
235void __exit sw_sync_device_remove(void)
236{
237	misc_deregister(&sw_sync_dev);
238}
239
240module_init(sw_sync_device_init);
241module_exit(sw_sync_device_remove);
242
243#endif /* CONFIG_SW_SYNC_USER */
244