Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 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> |
Erik Gilling | 6e91f71 | 2013-02-28 16:43:07 -0800 | [diff] [blame] | 18 | #include <linux/export.h> |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 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 | |
| 28 | static 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 | |
| 36 | struct 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 | } |
Erik Gilling | 6e91f71 | 2013-02-28 16:43:07 -0800 | [diff] [blame] | 47 | EXPORT_SYMBOL(sw_sync_pt_create); |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 48 | |
| 49 | static struct sync_pt *sw_sync_pt_dup(struct sync_pt *sync_pt) |
| 50 | { |
Purnendu Kapadia | 50d0a21 | 2014-09-15 13:06:36 +0100 | [diff] [blame] | 51 | struct sw_sync_pt *pt = (struct sw_sync_pt *)sync_pt; |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 52 | struct sw_sync_timeline *obj = |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 53 | (struct sw_sync_timeline *)sync_pt_parent(sync_pt); |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 54 | |
Purnendu Kapadia | 50d0a21 | 2014-09-15 13:06:36 +0100 | [diff] [blame] | 55 | return (struct sync_pt *)sw_sync_pt_create(obj, pt->value); |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static 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 = |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 62 | (struct sw_sync_timeline *)sync_pt_parent(sync_pt); |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 63 | |
| 64 | return sw_sync_cmp(obj->value, pt->value) >= 0; |
| 65 | } |
| 66 | |
| 67 | static 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 | |
Erik Gilling | b1489c2 | 2013-02-28 16:43:03 -0800 | [diff] [blame] | 75 | static 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 | |
Erik Gilling | 135114a | 2013-02-28 16:43:22 -0800 | [diff] [blame] | 88 | static 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 | |
| 96 | static void sw_sync_pt_value_str(struct sync_pt *sync_pt, |
Purnendu Kapadia | 50d0a21 | 2014-09-15 13:06:36 +0100 | [diff] [blame] | 97 | char *str, int size) |
Erik Gilling | 135114a | 2013-02-28 16:43:22 -0800 | [diff] [blame] | 98 | { |
| 99 | struct sw_sync_pt *pt = (struct sw_sync_pt *)sync_pt; |
Yee Chin, Chiam | a5b4e00 | 2014-08-06 17:47:34 -0400 | [diff] [blame] | 100 | |
Erik Gilling | 135114a | 2013-02-28 16:43:22 -0800 | [diff] [blame] | 101 | snprintf(str, size, "%d", pt->value); |
| 102 | } |
| 103 | |
Changlong Xie | 451fb76 | 2013-03-05 13:39:39 +0800 | [diff] [blame] | 104 | static struct sync_timeline_ops sw_sync_timeline_ops = { |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 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, |
Erik Gilling | b1489c2 | 2013-02-28 16:43:03 -0800 | [diff] [blame] | 109 | .fill_driver_data = sw_sync_fill_driver_data, |
Erik Gilling | 135114a | 2013-02-28 16:43:22 -0800 | [diff] [blame] | 110 | .timeline_value_str = sw_sync_timeline_value_str, |
| 111 | .pt_value_str = sw_sync_pt_value_str, |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 112 | }; |
| 113 | |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 114 | struct sw_sync_timeline *sw_sync_timeline_create(const char *name) |
| 115 | { |
| 116 | struct sw_sync_timeline *obj = (struct sw_sync_timeline *) |
| 117 | sync_timeline_create(&sw_sync_timeline_ops, |
| 118 | sizeof(struct sw_sync_timeline), |
| 119 | name); |
| 120 | |
| 121 | return obj; |
| 122 | } |
Erik Gilling | 6e91f71 | 2013-02-28 16:43:07 -0800 | [diff] [blame] | 123 | EXPORT_SYMBOL(sw_sync_timeline_create); |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 124 | |
| 125 | void sw_sync_timeline_inc(struct sw_sync_timeline *obj, u32 inc) |
| 126 | { |
| 127 | obj->value += inc; |
| 128 | |
| 129 | sync_timeline_signal(&obj->obj); |
| 130 | } |
Erik Gilling | 6e91f71 | 2013-02-28 16:43:07 -0800 | [diff] [blame] | 131 | EXPORT_SYMBOL(sw_sync_timeline_inc); |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 132 | |
| 133 | #ifdef CONFIG_SW_SYNC_USER |
| 134 | /* *WARNING* |
| 135 | * |
| 136 | * improper use of this can result in deadlocking kernel drivers from userspace. |
| 137 | */ |
| 138 | |
| 139 | /* opening sw_sync create a new sync obj */ |
Changlong Xie | 451fb76 | 2013-03-05 13:39:39 +0800 | [diff] [blame] | 140 | static int sw_sync_open(struct inode *inode, struct file *file) |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 141 | { |
| 142 | struct sw_sync_timeline *obj; |
| 143 | char task_comm[TASK_COMM_LEN]; |
| 144 | |
| 145 | get_task_comm(task_comm, current); |
| 146 | |
| 147 | obj = sw_sync_timeline_create(task_comm); |
| 148 | if (obj == NULL) |
| 149 | return -ENOMEM; |
| 150 | |
| 151 | file->private_data = obj; |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
Changlong Xie | 451fb76 | 2013-03-05 13:39:39 +0800 | [diff] [blame] | 156 | static int sw_sync_release(struct inode *inode, struct file *file) |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 157 | { |
| 158 | struct sw_sync_timeline *obj = file->private_data; |
Yee Chin, Chiam | a5b4e00 | 2014-08-06 17:47:34 -0400 | [diff] [blame] | 159 | |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 160 | sync_timeline_destroy(&obj->obj); |
| 161 | return 0; |
| 162 | } |
| 163 | |
Marlies Ruck | 0f14a20 | 2013-05-15 13:05:24 -0400 | [diff] [blame] | 164 | static long sw_sync_ioctl_create_fence(struct sw_sync_timeline *obj, |
| 165 | unsigned long arg) |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 166 | { |
Yann Droneaud | 45acea5 | 2013-08-15 15:10:53 +0200 | [diff] [blame] | 167 | int fd = get_unused_fd_flags(O_CLOEXEC); |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 168 | int err; |
| 169 | struct sync_pt *pt; |
| 170 | struct sync_fence *fence; |
| 171 | struct sw_sync_create_fence_data data; |
| 172 | |
Rebecca Schultz Zavin | 03e7a50 | 2013-02-28 16:43:13 -0800 | [diff] [blame] | 173 | if (fd < 0) |
| 174 | return fd; |
| 175 | |
| 176 | if (copy_from_user(&data, (void __user *)arg, sizeof(data))) { |
| 177 | err = -EFAULT; |
| 178 | goto err; |
| 179 | } |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 180 | |
| 181 | pt = sw_sync_pt_create(obj, data.value); |
| 182 | if (pt == NULL) { |
| 183 | err = -ENOMEM; |
| 184 | goto err; |
| 185 | } |
| 186 | |
| 187 | data.name[sizeof(data.name) - 1] = '\0'; |
| 188 | fence = sync_fence_create(data.name, pt); |
| 189 | if (fence == NULL) { |
| 190 | sync_pt_free(pt); |
| 191 | err = -ENOMEM; |
| 192 | goto err; |
| 193 | } |
| 194 | |
| 195 | data.fence = fd; |
| 196 | if (copy_to_user((void __user *)arg, &data, sizeof(data))) { |
| 197 | sync_fence_put(fence); |
| 198 | err = -EFAULT; |
| 199 | goto err; |
| 200 | } |
| 201 | |
| 202 | sync_fence_install(fence, fd); |
| 203 | |
| 204 | return 0; |
| 205 | |
| 206 | err: |
| 207 | put_unused_fd(fd); |
| 208 | return err; |
| 209 | } |
| 210 | |
Changlong Xie | 451fb76 | 2013-03-05 13:39:39 +0800 | [diff] [blame] | 211 | static long sw_sync_ioctl_inc(struct sw_sync_timeline *obj, unsigned long arg) |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 212 | { |
| 213 | u32 value; |
| 214 | |
| 215 | if (copy_from_user(&value, (void __user *)arg, sizeof(value))) |
| 216 | return -EFAULT; |
| 217 | |
| 218 | sw_sync_timeline_inc(obj, value); |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
Marlies Ruck | 0f14a20 | 2013-05-15 13:05:24 -0400 | [diff] [blame] | 223 | static long sw_sync_ioctl(struct file *file, unsigned int cmd, |
| 224 | unsigned long arg) |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 225 | { |
| 226 | struct sw_sync_timeline *obj = file->private_data; |
| 227 | |
| 228 | switch (cmd) { |
| 229 | case SW_SYNC_IOC_CREATE_FENCE: |
| 230 | return sw_sync_ioctl_create_fence(obj, arg); |
| 231 | |
| 232 | case SW_SYNC_IOC_INC: |
| 233 | return sw_sync_ioctl_inc(obj, arg); |
| 234 | |
| 235 | default: |
| 236 | return -ENOTTY; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | static const struct file_operations sw_sync_fops = { |
| 241 | .owner = THIS_MODULE, |
| 242 | .open = sw_sync_open, |
| 243 | .release = sw_sync_release, |
| 244 | .unlocked_ioctl = sw_sync_ioctl, |
Dmitry Pervushin | c37b95e | 2013-03-29 10:50:02 -0700 | [diff] [blame] | 245 | .compat_ioctl = sw_sync_ioctl, |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | static struct miscdevice sw_sync_dev = { |
| 249 | .minor = MISC_DYNAMIC_MINOR, |
| 250 | .name = "sw_sync", |
| 251 | .fops = &sw_sync_fops, |
| 252 | }; |
| 253 | |
Changlong Xie | 451fb76 | 2013-03-05 13:39:39 +0800 | [diff] [blame] | 254 | static int __init sw_sync_device_init(void) |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 255 | { |
| 256 | return misc_register(&sw_sync_dev); |
| 257 | } |
| 258 | |
Changlong Xie | 451fb76 | 2013-03-05 13:39:39 +0800 | [diff] [blame] | 259 | static void __exit sw_sync_device_remove(void) |
Erik Gilling | 9d1906e | 2013-02-28 16:42:58 -0800 | [diff] [blame] | 260 | { |
| 261 | misc_deregister(&sw_sync_dev); |
| 262 | } |
| 263 | |
| 264 | module_init(sw_sync_device_init); |
| 265 | module_exit(sw_sync_device_remove); |
| 266 | |
| 267 | #endif /* CONFIG_SW_SYNC_USER */ |