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