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