blob: 21ddf4ffd58944714d8a84b25ae19d4611163ce8 [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
Erik Gilling2c959d72012-03-15 14:23:23 -070072static void sw_sync_print_obj(struct seq_file *s,
73 struct sync_timeline *sync_timeline)
74{
75 struct sw_sync_timeline *obj = (struct sw_sync_timeline *)sync_timeline;
76
77 seq_printf(s, "%d", obj->value);
78}
79
80static void sw_sync_print_pt(struct seq_file *s, struct sync_pt *sync_pt)
81{
82 struct sw_sync_pt *pt = (struct sw_sync_pt *)sync_pt;
83 struct sw_sync_timeline *obj =
84 (struct sw_sync_timeline *)sync_pt->parent;
85
86 seq_printf(s, "%d / %d", pt->value, obj->value);
87}
88
Erik Gilling484101e2012-03-15 17:46:07 -070089static int sw_sync_fill_driver_data(struct sync_pt *sync_pt,
90 void *data, int size)
91{
92 struct sw_sync_pt *pt = (struct sw_sync_pt *)sync_pt;
93
94 if (size < sizeof(pt->value))
95 return -ENOMEM;
96
97 memcpy(data, &pt->value, sizeof(pt->value));
98
99 return sizeof(pt->value);
100}
101
Erik Gilling560b5462012-04-18 13:43:22 -0700102struct sync_timeline_ops sw_sync_timeline_ops = {
103 .driver_name = "sw_sync",
104 .dup = sw_sync_pt_dup,
105 .has_signaled = sw_sync_pt_has_signaled,
106 .compare = sw_sync_pt_compare,
Erik Gilling2c959d72012-03-15 14:23:23 -0700107 .print_obj = sw_sync_print_obj,
108 .print_pt = sw_sync_print_pt,
Erik Gilling484101e2012-03-15 17:46:07 -0700109 .fill_driver_data = sw_sync_fill_driver_data,
Erik Gilling560b5462012-04-18 13:43:22 -0700110};
111
112
113struct sw_sync_timeline *sw_sync_timeline_create(const char *name)
114{
115 struct sw_sync_timeline *obj = (struct sw_sync_timeline *)
116 sync_timeline_create(&sw_sync_timeline_ops,
117 sizeof(struct sw_sync_timeline),
118 name);
119
120 return obj;
121}
122
123void sw_sync_timeline_inc(struct sw_sync_timeline *obj, u32 inc)
124{
125 obj->value += inc;
126
127 sync_timeline_signal(&obj->obj);
128}
129
130
131#ifdef CONFIG_SW_SYNC_USER
132/* *WARNING*
133 *
134 * improper use of this can result in deadlocking kernel drivers from userspace.
135 */
136
137/* opening sw_sync create a new sync obj */
138int sw_sync_open(struct inode *inode, struct file *file)
139{
140 struct sw_sync_timeline *obj;
141 char task_comm[TASK_COMM_LEN];
142
143 get_task_comm(task_comm, current);
144
145 obj = sw_sync_timeline_create(task_comm);
146 if (obj == NULL)
147 return -ENOMEM;
148
149 file->private_data = obj;
150
151 return 0;
152}
153
154int sw_sync_release(struct inode *inode, struct file *file)
155{
156 struct sw_sync_timeline *obj = file->private_data;
157 sync_timeline_destroy(&obj->obj);
158 return 0;
159}
160
161long sw_sync_ioctl_create_fence(struct sw_sync_timeline *obj, unsigned long arg)
162{
163 int fd = get_unused_fd();
164 int err;
165 struct sync_pt *pt;
166 struct sync_fence *fence;
167 struct sw_sync_create_fence_data data;
168
169 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
170 return -EFAULT;
171
172 pt = sw_sync_pt_create(obj, data.value);
173 if (pt == NULL) {
174 err = -ENOMEM;
175 goto err;
176 }
177
178 data.name[sizeof(data.name) - 1] = '\0';
179 fence = sync_fence_create(data.name, pt);
180 if (fence == NULL) {
181 sync_pt_free(pt);
182 err = -ENOMEM;
183 goto err;
184 }
185
186 data.fence = fd;
187 if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
188 sync_fence_put(fence);
189 err = -EFAULT;
190 goto err;
191 }
192
193 sync_fence_install(fence, fd);
194
195 return 0;
196
197err:
198 put_unused_fd(fd);
199 return err;
200}
201
202long sw_sync_ioctl_inc(struct sw_sync_timeline *obj, unsigned long arg)
203{
204 u32 value;
205
206 if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
207 return -EFAULT;
208
209 sw_sync_timeline_inc(obj, value);
210
211 return 0;
212}
213
214long sw_sync_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
215{
216 struct sw_sync_timeline *obj = file->private_data;
217
218 switch (cmd) {
219 case SW_SYNC_IOC_CREATE_FENCE:
220 return sw_sync_ioctl_create_fence(obj, arg);
221
222 case SW_SYNC_IOC_INC:
223 return sw_sync_ioctl_inc(obj, arg);
224
225 default:
226 return -ENOTTY;
227 }
228}
229
230static const struct file_operations sw_sync_fops = {
231 .owner = THIS_MODULE,
232 .open = sw_sync_open,
233 .release = sw_sync_release,
234 .unlocked_ioctl = sw_sync_ioctl,
235};
236
237static struct miscdevice sw_sync_dev = {
238 .minor = MISC_DYNAMIC_MINOR,
239 .name = "sw_sync",
240 .fops = &sw_sync_fops,
241};
242
243int __init sw_sync_device_init(void)
244{
245 return misc_register(&sw_sync_dev);
246}
247
248void __exit sw_sync_device_remove(void)
249{
250 misc_deregister(&sw_sync_dev);
251}
252
253module_init(sw_sync_device_init);
254module_exit(sw_sync_device_remove);
255
256#endif /* CONFIG_SW_SYNC_USER */