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