| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/base/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 | |
| Erik Gilling | 981c8a9 | 2012-03-14 19:49:15 -0700 | [diff] [blame] | 17 | #include <linux/debugfs.h> |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 18 | #include <linux/file.h> |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/kernel.h> |
| Erik Gilling | a1eeaca | 2012-03-19 17:28:32 -0700 | [diff] [blame] | 21 | #include <linux/poll.h> |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 22 | #include <linux/sched.h> |
| Erik Gilling | 981c8a9 | 2012-03-14 19:49:15 -0700 | [diff] [blame] | 23 | #include <linux/seq_file.h> |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 24 | #include <linux/slab.h> |
| 25 | #include <linux/sync.h> |
| 26 | #include <linux/uaccess.h> |
| 27 | |
| 28 | #include <linux/anon_inodes.h> |
| 29 | |
| 30 | static void sync_fence_signal_pt(struct sync_pt *pt); |
| 31 | static int _sync_pt_has_signaled(struct sync_pt *pt); |
| 32 | |
| Erik Gilling | 981c8a9 | 2012-03-14 19:49:15 -0700 | [diff] [blame] | 33 | static LIST_HEAD(sync_timeline_list_head); |
| 34 | static DEFINE_SPINLOCK(sync_timeline_list_lock); |
| 35 | |
| 36 | static LIST_HEAD(sync_fence_list_head); |
| 37 | static DEFINE_SPINLOCK(sync_fence_list_lock); |
| 38 | |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 39 | struct sync_timeline *sync_timeline_create(const struct sync_timeline_ops *ops, |
| 40 | int size, const char *name) |
| 41 | { |
| 42 | struct sync_timeline *obj; |
| Erik Gilling | 981c8a9 | 2012-03-14 19:49:15 -0700 | [diff] [blame] | 43 | unsigned long flags; |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 44 | |
| 45 | if (size < sizeof(struct sync_timeline)) |
| 46 | return NULL; |
| 47 | |
| 48 | obj = kzalloc(size, GFP_KERNEL); |
| 49 | if (obj == NULL) |
| 50 | return NULL; |
| 51 | |
| 52 | obj->ops = ops; |
| 53 | strlcpy(obj->name, name, sizeof(obj->name)); |
| 54 | |
| 55 | INIT_LIST_HEAD(&obj->child_list_head); |
| 56 | spin_lock_init(&obj->child_list_lock); |
| 57 | |
| 58 | INIT_LIST_HEAD(&obj->active_list_head); |
| 59 | spin_lock_init(&obj->active_list_lock); |
| 60 | |
| Erik Gilling | 981c8a9 | 2012-03-14 19:49:15 -0700 | [diff] [blame] | 61 | spin_lock_irqsave(&sync_timeline_list_lock, flags); |
| 62 | list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head); |
| 63 | spin_unlock_irqrestore(&sync_timeline_list_lock, flags); |
| 64 | |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 65 | return obj; |
| 66 | } |
| 67 | |
| Erik Gilling | 981c8a9 | 2012-03-14 19:49:15 -0700 | [diff] [blame] | 68 | static void sync_timeline_free(struct sync_timeline *obj) |
| 69 | { |
| 70 | unsigned long flags; |
| 71 | |
| 72 | if (obj->ops->release_obj) |
| 73 | obj->ops->release_obj(obj); |
| 74 | |
| 75 | spin_lock_irqsave(&sync_timeline_list_lock, flags); |
| 76 | list_del(&obj->sync_timeline_list); |
| 77 | spin_unlock_irqrestore(&sync_timeline_list_lock, flags); |
| 78 | |
| 79 | kfree(obj); |
| 80 | } |
| 81 | |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 82 | void sync_timeline_destroy(struct sync_timeline *obj) |
| 83 | { |
| 84 | unsigned long flags; |
| 85 | bool needs_freeing; |
| 86 | |
| 87 | spin_lock_irqsave(&obj->child_list_lock, flags); |
| 88 | obj->destroyed = true; |
| 89 | needs_freeing = list_empty(&obj->child_list_head); |
| 90 | spin_unlock_irqrestore(&obj->child_list_lock, flags); |
| 91 | |
| 92 | if (needs_freeing) |
| Erik Gilling | 981c8a9 | 2012-03-14 19:49:15 -0700 | [diff] [blame] | 93 | sync_timeline_free(obj); |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 94 | else |
| 95 | sync_timeline_signal(obj); |
| 96 | } |
| 97 | |
| 98 | static void sync_timeline_add_pt(struct sync_timeline *obj, struct sync_pt *pt) |
| 99 | { |
| 100 | unsigned long flags; |
| 101 | |
| 102 | pt->parent = obj; |
| 103 | |
| 104 | spin_lock_irqsave(&obj->child_list_lock, flags); |
| 105 | list_add_tail(&pt->child_list, &obj->child_list_head); |
| 106 | spin_unlock_irqrestore(&obj->child_list_lock, flags); |
| 107 | } |
| 108 | |
| 109 | static void sync_timeline_remove_pt(struct sync_pt *pt) |
| 110 | { |
| 111 | struct sync_timeline *obj = pt->parent; |
| 112 | unsigned long flags; |
| 113 | bool needs_freeing; |
| 114 | |
| 115 | spin_lock_irqsave(&obj->active_list_lock, flags); |
| 116 | if (!list_empty(&pt->active_list)) |
| 117 | list_del_init(&pt->active_list); |
| 118 | spin_unlock_irqrestore(&obj->active_list_lock, flags); |
| 119 | |
| 120 | spin_lock_irqsave(&obj->child_list_lock, flags); |
| 121 | list_del(&pt->child_list); |
| 122 | needs_freeing = obj->destroyed && list_empty(&obj->child_list_head); |
| 123 | spin_unlock_irqrestore(&obj->child_list_lock, flags); |
| 124 | |
| 125 | if (needs_freeing) |
| Erik Gilling | 981c8a9 | 2012-03-14 19:49:15 -0700 | [diff] [blame] | 126 | sync_timeline_free(obj); |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void sync_timeline_signal(struct sync_timeline *obj) |
| 130 | { |
| 131 | unsigned long flags; |
| 132 | LIST_HEAD(signaled_pts); |
| 133 | struct list_head *pos, *n; |
| 134 | |
| 135 | spin_lock_irqsave(&obj->active_list_lock, flags); |
| 136 | |
| 137 | list_for_each_safe(pos, n, &obj->active_list_head) { |
| 138 | struct sync_pt *pt = |
| 139 | container_of(pos, struct sync_pt, active_list); |
| 140 | |
| 141 | if (_sync_pt_has_signaled(pt)) |
| 142 | list_move(pos, &signaled_pts); |
| 143 | } |
| 144 | |
| 145 | spin_unlock_irqrestore(&obj->active_list_lock, flags); |
| 146 | |
| 147 | list_for_each_safe(pos, n, &signaled_pts) { |
| 148 | struct sync_pt *pt = |
| 149 | container_of(pos, struct sync_pt, active_list); |
| 150 | |
| 151 | list_del_init(pos); |
| 152 | sync_fence_signal_pt(pt); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | struct sync_pt *sync_pt_create(struct sync_timeline *parent, int size) |
| 157 | { |
| 158 | struct sync_pt *pt; |
| 159 | |
| 160 | if (size < sizeof(struct sync_pt)) |
| 161 | return NULL; |
| 162 | |
| 163 | pt = kzalloc(size, GFP_KERNEL); |
| 164 | if (pt == NULL) |
| 165 | return NULL; |
| 166 | |
| 167 | INIT_LIST_HEAD(&pt->active_list); |
| 168 | sync_timeline_add_pt(parent, pt); |
| 169 | |
| 170 | return pt; |
| 171 | } |
| 172 | |
| 173 | void sync_pt_free(struct sync_pt *pt) |
| 174 | { |
| 175 | if (pt->parent->ops->free_pt) |
| 176 | pt->parent->ops->free_pt(pt); |
| 177 | |
| 178 | sync_timeline_remove_pt(pt); |
| 179 | |
| 180 | kfree(pt); |
| 181 | } |
| 182 | |
| 183 | /* call with pt->parent->active_list_lock held */ |
| 184 | static int _sync_pt_has_signaled(struct sync_pt *pt) |
| 185 | { |
| Erik Gilling | ad433ba | 2012-03-15 14:59:33 -0700 | [diff] [blame] | 186 | int old_status = pt->status; |
| 187 | |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 188 | if (!pt->status) |
| 189 | pt->status = pt->parent->ops->has_signaled(pt); |
| 190 | |
| 191 | if (!pt->status && pt->parent->destroyed) |
| 192 | pt->status = -ENOENT; |
| 193 | |
| Erik Gilling | ad433ba | 2012-03-15 14:59:33 -0700 | [diff] [blame] | 194 | if (pt->status != old_status) |
| 195 | pt->timestamp = ktime_get(); |
| 196 | |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 197 | return pt->status; |
| 198 | } |
| 199 | |
| 200 | static struct sync_pt *sync_pt_dup(struct sync_pt *pt) |
| 201 | { |
| 202 | return pt->parent->ops->dup(pt); |
| 203 | } |
| 204 | |
| 205 | /* Adds a sync pt to the active queue. Called when added to a fence */ |
| 206 | static void sync_pt_activate(struct sync_pt *pt) |
| 207 | { |
| 208 | struct sync_timeline *obj = pt->parent; |
| 209 | unsigned long flags; |
| 210 | int err; |
| 211 | |
| 212 | spin_lock_irqsave(&obj->active_list_lock, flags); |
| 213 | |
| 214 | err = _sync_pt_has_signaled(pt); |
| 215 | if (err != 0) |
| 216 | goto out; |
| 217 | |
| 218 | list_add_tail(&pt->active_list, &obj->active_list_head); |
| 219 | |
| 220 | out: |
| 221 | spin_unlock_irqrestore(&obj->active_list_lock, flags); |
| 222 | } |
| 223 | |
| 224 | static int sync_fence_release(struct inode *inode, struct file *file); |
| Erik Gilling | a1eeaca | 2012-03-19 17:28:32 -0700 | [diff] [blame] | 225 | static unsigned int sync_fence_poll(struct file *file, poll_table *wait); |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 226 | static long sync_fence_ioctl(struct file *file, unsigned int cmd, |
| 227 | unsigned long arg); |
| 228 | |
| 229 | |
| 230 | static const struct file_operations sync_fence_fops = { |
| 231 | .release = sync_fence_release, |
| Erik Gilling | a1eeaca | 2012-03-19 17:28:32 -0700 | [diff] [blame] | 232 | .poll = sync_fence_poll, |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 233 | .unlocked_ioctl = sync_fence_ioctl, |
| 234 | }; |
| 235 | |
| 236 | static struct sync_fence *sync_fence_alloc(const char *name) |
| 237 | { |
| 238 | struct sync_fence *fence; |
| Erik Gilling | 981c8a9 | 2012-03-14 19:49:15 -0700 | [diff] [blame] | 239 | unsigned long flags; |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 240 | |
| 241 | fence = kzalloc(sizeof(struct sync_fence), GFP_KERNEL); |
| 242 | if (fence == NULL) |
| 243 | return NULL; |
| 244 | |
| 245 | fence->file = anon_inode_getfile("sync_fence", &sync_fence_fops, |
| 246 | fence, 0); |
| 247 | if (fence->file == NULL) |
| 248 | goto err; |
| 249 | |
| 250 | strlcpy(fence->name, name, sizeof(fence->name)); |
| 251 | |
| 252 | INIT_LIST_HEAD(&fence->pt_list_head); |
| 253 | INIT_LIST_HEAD(&fence->waiter_list_head); |
| 254 | spin_lock_init(&fence->waiter_list_lock); |
| 255 | |
| 256 | init_waitqueue_head(&fence->wq); |
| Erik Gilling | 981c8a9 | 2012-03-14 19:49:15 -0700 | [diff] [blame] | 257 | |
| 258 | spin_lock_irqsave(&sync_fence_list_lock, flags); |
| 259 | list_add_tail(&fence->sync_fence_list, &sync_fence_list_head); |
| 260 | spin_unlock_irqrestore(&sync_fence_list_lock, flags); |
| 261 | |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 262 | return fence; |
| 263 | |
| 264 | err: |
| 265 | kfree(fence); |
| 266 | return NULL; |
| 267 | } |
| 268 | |
| 269 | /* TODO: implement a create which takes more that one sync_pt */ |
| 270 | struct sync_fence *sync_fence_create(const char *name, struct sync_pt *pt) |
| 271 | { |
| 272 | struct sync_fence *fence; |
| 273 | |
| 274 | if (pt->fence) |
| 275 | return NULL; |
| 276 | |
| 277 | fence = sync_fence_alloc(name); |
| 278 | if (fence == NULL) |
| 279 | return NULL; |
| 280 | |
| 281 | pt->fence = fence; |
| 282 | list_add(&pt->pt_list, &fence->pt_list_head); |
| 283 | sync_pt_activate(pt); |
| 284 | |
| 285 | return fence; |
| 286 | } |
| 287 | |
| 288 | static int sync_fence_copy_pts(struct sync_fence *dst, struct sync_fence *src) |
| 289 | { |
| 290 | struct list_head *pos; |
| 291 | |
| 292 | list_for_each(pos, &src->pt_list_head) { |
| 293 | struct sync_pt *orig_pt = |
| 294 | container_of(pos, struct sync_pt, pt_list); |
| 295 | struct sync_pt *new_pt = sync_pt_dup(orig_pt); |
| 296 | |
| 297 | if (new_pt == NULL) |
| 298 | return -ENOMEM; |
| 299 | |
| 300 | new_pt->fence = dst; |
| 301 | list_add(&new_pt->pt_list, &dst->pt_list_head); |
| 302 | sync_pt_activate(new_pt); |
| 303 | } |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static void sync_fence_free_pts(struct sync_fence *fence) |
| 309 | { |
| 310 | struct list_head *pos, *n; |
| 311 | |
| 312 | list_for_each_safe(pos, n, &fence->pt_list_head) { |
| 313 | struct sync_pt *pt = container_of(pos, struct sync_pt, pt_list); |
| 314 | sync_pt_free(pt); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | struct sync_fence *sync_fence_fdget(int fd) |
| 319 | { |
| 320 | struct file *file = fget(fd); |
| 321 | |
| 322 | if (file == NULL) |
| 323 | return NULL; |
| 324 | |
| 325 | if (file->f_op != &sync_fence_fops) |
| 326 | goto err; |
| 327 | |
| 328 | return file->private_data; |
| 329 | |
| 330 | err: |
| 331 | fput(file); |
| 332 | return NULL; |
| 333 | } |
| 334 | |
| 335 | void sync_fence_put(struct sync_fence *fence) |
| 336 | { |
| 337 | fput(fence->file); |
| 338 | } |
| 339 | |
| 340 | void sync_fence_install(struct sync_fence *fence, int fd) |
| 341 | { |
| 342 | fd_install(fd, fence->file); |
| 343 | } |
| 344 | |
| 345 | static int sync_fence_get_status(struct sync_fence *fence) |
| 346 | { |
| 347 | struct list_head *pos; |
| 348 | int status = 1; |
| 349 | |
| 350 | list_for_each(pos, &fence->pt_list_head) { |
| 351 | struct sync_pt *pt = container_of(pos, struct sync_pt, pt_list); |
| 352 | int pt_status = pt->status; |
| 353 | |
| 354 | if (pt_status < 0) { |
| 355 | status = pt_status; |
| 356 | break; |
| 357 | } else if (status == 1) { |
| 358 | status = pt_status; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | return status; |
| 363 | } |
| 364 | |
| 365 | struct sync_fence *sync_fence_merge(const char *name, |
| 366 | struct sync_fence *a, struct sync_fence *b) |
| 367 | { |
| 368 | struct sync_fence *fence; |
| 369 | int err; |
| 370 | |
| 371 | fence = sync_fence_alloc(name); |
| 372 | if (fence == NULL) |
| 373 | return NULL; |
| 374 | |
| 375 | err = sync_fence_copy_pts(fence, a); |
| 376 | if (err < 0) |
| 377 | goto err; |
| 378 | |
| 379 | err = sync_fence_copy_pts(fence, b); |
| 380 | if (err < 0) |
| 381 | goto err; |
| 382 | |
| 383 | fence->status = sync_fence_get_status(fence); |
| 384 | |
| 385 | return fence; |
| 386 | err: |
| 387 | sync_fence_free_pts(fence); |
| 388 | kfree(fence); |
| 389 | return NULL; |
| 390 | } |
| 391 | |
| 392 | static void sync_fence_signal_pt(struct sync_pt *pt) |
| 393 | { |
| 394 | LIST_HEAD(signaled_waiters); |
| 395 | struct sync_fence *fence = pt->fence; |
| 396 | struct list_head *pos; |
| 397 | struct list_head *n; |
| 398 | unsigned long flags; |
| 399 | int status; |
| 400 | |
| 401 | status = sync_fence_get_status(fence); |
| 402 | |
| 403 | spin_lock_irqsave(&fence->waiter_list_lock, flags); |
| 404 | /* |
| 405 | * this should protect against two threads racing on the signaled |
| 406 | * false -> true transition |
| 407 | */ |
| 408 | if (status && !fence->status) { |
| 409 | list_for_each_safe(pos, n, &fence->waiter_list_head) |
| 410 | list_move(pos, &signaled_waiters); |
| 411 | |
| 412 | fence->status = status; |
| 413 | } else { |
| 414 | status = 0; |
| 415 | } |
| 416 | spin_unlock_irqrestore(&fence->waiter_list_lock, flags); |
| 417 | |
| 418 | if (status) { |
| 419 | list_for_each_safe(pos, n, &signaled_waiters) { |
| 420 | struct sync_fence_waiter *waiter = |
| 421 | container_of(pos, struct sync_fence_waiter, |
| 422 | waiter_list); |
| 423 | |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 424 | list_del(pos); |
| Erik Gilling | c80114f | 2012-05-15 16:23:26 -0700 | [diff] [blame^] | 425 | waiter->callback(fence, waiter); |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 426 | } |
| 427 | wake_up(&fence->wq); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | int sync_fence_wait_async(struct sync_fence *fence, |
| Erik Gilling | c80114f | 2012-05-15 16:23:26 -0700 | [diff] [blame^] | 432 | struct sync_fence_waiter *waiter) |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 433 | { |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 434 | unsigned long flags; |
| 435 | int err = 0; |
| 436 | |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 437 | spin_lock_irqsave(&fence->waiter_list_lock, flags); |
| 438 | |
| 439 | if (fence->status) { |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 440 | err = fence->status; |
| 441 | goto out; |
| 442 | } |
| 443 | |
| 444 | list_add_tail(&waiter->waiter_list, &fence->waiter_list_head); |
| 445 | out: |
| 446 | spin_unlock_irqrestore(&fence->waiter_list_lock, flags); |
| 447 | |
| 448 | return err; |
| 449 | } |
| 450 | |
| Erik Gilling | c80114f | 2012-05-15 16:23:26 -0700 | [diff] [blame^] | 451 | int sync_fence_cancel_async(struct sync_fence *fence, |
| 452 | struct sync_fence_waiter *waiter) |
| 453 | { |
| 454 | struct list_head *pos; |
| 455 | struct list_head *n; |
| 456 | unsigned long flags; |
| 457 | int ret = -ENOENT; |
| 458 | |
| 459 | spin_lock_irqsave(&fence->waiter_list_lock, flags); |
| 460 | /* |
| 461 | * Make sure waiter is still in waiter_list because it is possible for |
| 462 | * the waiter to be removed from the list while the callback is still |
| 463 | * pending. |
| 464 | */ |
| 465 | list_for_each_safe(pos, n, &fence->waiter_list_head) { |
| 466 | struct sync_fence_waiter *list_waiter = |
| 467 | container_of(pos, struct sync_fence_waiter, |
| 468 | waiter_list); |
| 469 | if (list_waiter == waiter) { |
| 470 | list_del(pos); |
| 471 | ret = 0; |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 | spin_unlock_irqrestore(&fence->waiter_list_lock, flags); |
| 476 | return ret; |
| 477 | } |
| 478 | |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 479 | int sync_fence_wait(struct sync_fence *fence, long timeout) |
| 480 | { |
| 481 | int err; |
| 482 | |
| 483 | if (timeout) { |
| 484 | timeout = msecs_to_jiffies(timeout); |
| 485 | err = wait_event_interruptible_timeout(fence->wq, |
| 486 | fence->status != 0, |
| 487 | timeout); |
| 488 | } else { |
| 489 | err = wait_event_interruptible(fence->wq, fence->status != 0); |
| 490 | } |
| 491 | |
| 492 | if (err < 0) |
| 493 | return err; |
| 494 | |
| 495 | if (fence->status < 0) |
| 496 | return fence->status; |
| 497 | |
| 498 | if (fence->status == 0) |
| 499 | return -ETIME; |
| 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | static int sync_fence_release(struct inode *inode, struct file *file) |
| 505 | { |
| 506 | struct sync_fence *fence = file->private_data; |
| Erik Gilling | 981c8a9 | 2012-03-14 19:49:15 -0700 | [diff] [blame] | 507 | unsigned long flags; |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 508 | |
| 509 | sync_fence_free_pts(fence); |
| Erik Gilling | 981c8a9 | 2012-03-14 19:49:15 -0700 | [diff] [blame] | 510 | |
| 511 | spin_lock_irqsave(&sync_fence_list_lock, flags); |
| 512 | list_del(&fence->sync_fence_list); |
| 513 | spin_unlock_irqrestore(&sync_fence_list_lock, flags); |
| 514 | |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 515 | kfree(fence); |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
| Erik Gilling | a1eeaca | 2012-03-19 17:28:32 -0700 | [diff] [blame] | 520 | static unsigned int sync_fence_poll(struct file *file, poll_table *wait) |
| 521 | { |
| 522 | struct sync_fence *fence = file->private_data; |
| 523 | |
| 524 | poll_wait(file, &fence->wq, wait); |
| 525 | |
| 526 | if (fence->status == 1) |
| 527 | return POLLIN; |
| 528 | else if (fence->status < 0) |
| 529 | return POLLERR; |
| 530 | else |
| 531 | return 0; |
| 532 | } |
| 533 | |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 534 | static long sync_fence_ioctl_wait(struct sync_fence *fence, unsigned long arg) |
| 535 | { |
| 536 | __u32 value; |
| 537 | |
| 538 | if (copy_from_user(&value, (void __user *)arg, sizeof(value))) |
| 539 | return -EFAULT; |
| 540 | |
| 541 | return sync_fence_wait(fence, value); |
| 542 | } |
| 543 | |
| 544 | static long sync_fence_ioctl_merge(struct sync_fence *fence, unsigned long arg) |
| 545 | { |
| 546 | int fd = get_unused_fd(); |
| 547 | int err; |
| 548 | struct sync_fence *fence2, *fence3; |
| 549 | struct sync_merge_data data; |
| 550 | |
| 551 | if (copy_from_user(&data, (void __user *)arg, sizeof(data))) |
| 552 | return -EFAULT; |
| 553 | |
| 554 | fence2 = sync_fence_fdget(data.fd2); |
| 555 | if (fence2 == NULL) { |
| 556 | err = -ENOENT; |
| 557 | goto err_put_fd; |
| 558 | } |
| 559 | |
| 560 | data.name[sizeof(data.name) - 1] = '\0'; |
| 561 | fence3 = sync_fence_merge(data.name, fence, fence2); |
| 562 | if (fence3 == NULL) { |
| 563 | err = -ENOMEM; |
| 564 | goto err_put_fence2; |
| 565 | } |
| 566 | |
| 567 | data.fence = fd; |
| 568 | if (copy_to_user((void __user *)arg, &data, sizeof(data))) { |
| 569 | err = -EFAULT; |
| 570 | goto err_put_fence3; |
| 571 | } |
| 572 | |
| 573 | sync_fence_install(fence3, fd); |
| 574 | sync_fence_put(fence2); |
| 575 | return 0; |
| 576 | |
| 577 | err_put_fence3: |
| 578 | sync_fence_put(fence3); |
| 579 | |
| 580 | err_put_fence2: |
| 581 | sync_fence_put(fence2); |
| 582 | |
| 583 | err_put_fd: |
| 584 | put_unused_fd(fd); |
| 585 | return err; |
| 586 | } |
| 587 | |
| Erik Gilling | 3913bff | 2012-03-15 17:45:50 -0700 | [diff] [blame] | 588 | static int sync_fill_pt_info(struct sync_pt *pt, void *data, int size) |
| 589 | { |
| 590 | struct sync_pt_info *info = data; |
| 591 | int ret; |
| 592 | |
| 593 | if (size < sizeof(struct sync_pt_info)) |
| 594 | return -ENOMEM; |
| 595 | |
| 596 | info->len = sizeof(struct sync_pt_info); |
| 597 | |
| 598 | if (pt->parent->ops->fill_driver_data) { |
| 599 | ret = pt->parent->ops->fill_driver_data(pt, info->driver_data, |
| 600 | size - sizeof(*info)); |
| 601 | if (ret < 0) |
| 602 | return ret; |
| 603 | |
| 604 | info->len += ret; |
| 605 | } |
| 606 | |
| 607 | strlcpy(info->obj_name, pt->parent->name, sizeof(info->obj_name)); |
| 608 | strlcpy(info->driver_name, pt->parent->ops->driver_name, |
| 609 | sizeof(info->driver_name)); |
| 610 | info->status = pt->status; |
| 611 | info->timestamp_ns = ktime_to_ns(pt->timestamp); |
| 612 | |
| 613 | return info->len; |
| 614 | } |
| 615 | |
| 616 | static long sync_fence_ioctl_fence_info(struct sync_fence *fence, |
| 617 | unsigned long arg) |
| 618 | { |
| 619 | struct sync_fence_info_data *data; |
| 620 | struct list_head *pos; |
| 621 | __u32 size; |
| 622 | __u32 len = 0; |
| 623 | int ret; |
| 624 | |
| 625 | if (copy_from_user(&size, (void __user *)arg, sizeof(size))) |
| 626 | return -EFAULT; |
| 627 | |
| 628 | if (size < sizeof(struct sync_fence_info_data)) |
| 629 | return -EINVAL; |
| 630 | |
| 631 | if (size > 4096) |
| 632 | size = 4096; |
| 633 | |
| 634 | data = kzalloc(size, GFP_KERNEL); |
| 635 | if (data == NULL) |
| 636 | return -ENOMEM; |
| 637 | |
| 638 | strlcpy(data->name, fence->name, sizeof(data->name)); |
| 639 | data->status = fence->status; |
| 640 | len = sizeof(struct sync_fence_info_data); |
| 641 | |
| 642 | list_for_each(pos, &fence->pt_list_head) { |
| 643 | struct sync_pt *pt = |
| 644 | container_of(pos, struct sync_pt, pt_list); |
| 645 | |
| 646 | ret = sync_fill_pt_info(pt, (u8 *)data + len, size - len); |
| 647 | |
| 648 | if (ret < 0) |
| 649 | goto out; |
| 650 | |
| 651 | len += ret; |
| 652 | } |
| 653 | |
| 654 | data->len = len; |
| 655 | |
| 656 | if (copy_to_user((void __user *)arg, data, len)) |
| 657 | ret = -EFAULT; |
| 658 | else |
| 659 | ret = 0; |
| 660 | |
| 661 | out: |
| 662 | kfree(data); |
| 663 | |
| 664 | return ret; |
| 665 | } |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 666 | |
| 667 | static long sync_fence_ioctl(struct file *file, unsigned int cmd, |
| 668 | unsigned long arg) |
| 669 | { |
| 670 | struct sync_fence *fence = file->private_data; |
| 671 | switch (cmd) { |
| 672 | case SYNC_IOC_WAIT: |
| 673 | return sync_fence_ioctl_wait(fence, arg); |
| 674 | |
| 675 | case SYNC_IOC_MERGE: |
| 676 | return sync_fence_ioctl_merge(fence, arg); |
| Erik Gilling | 981c8a9 | 2012-03-14 19:49:15 -0700 | [diff] [blame] | 677 | |
| Erik Gilling | 3913bff | 2012-03-15 17:45:50 -0700 | [diff] [blame] | 678 | case SYNC_IOC_FENCE_INFO: |
| 679 | return sync_fence_ioctl_fence_info(fence, arg); |
| 680 | |
| Erik Gilling | 010accf | 2012-03-13 15:34:34 -0700 | [diff] [blame] | 681 | default: |
| 682 | return -ENOTTY; |
| 683 | } |
| 684 | } |
| 685 | |
| Erik Gilling | 981c8a9 | 2012-03-14 19:49:15 -0700 | [diff] [blame] | 686 | #ifdef CONFIG_DEBUG_FS |
| 687 | static const char *sync_status_str(int status) |
| 688 | { |
| 689 | if (status > 0) |
| 690 | return "signaled"; |
| 691 | else if (status == 0) |
| 692 | return "active"; |
| 693 | else |
| 694 | return "error"; |
| 695 | } |
| 696 | |
| 697 | static void sync_print_pt(struct seq_file *s, struct sync_pt *pt, bool fence) |
| 698 | { |
| 699 | int status = pt->status; |
| 700 | seq_printf(s, " %s%spt %s", |
| 701 | fence ? pt->parent->name : "", |
| 702 | fence ? "_" : "", |
| 703 | sync_status_str(status)); |
| 704 | if (pt->status) { |
| 705 | struct timeval tv = ktime_to_timeval(pt->timestamp); |
| 706 | seq_printf(s, "@%ld.%06ld", tv.tv_sec, tv.tv_usec); |
| 707 | } |
| 708 | |
| 709 | if (pt->parent->ops->print_pt) { |
| 710 | seq_printf(s, ": "); |
| 711 | pt->parent->ops->print_pt(s, pt); |
| 712 | } |
| 713 | |
| 714 | seq_printf(s, "\n"); |
| 715 | } |
| 716 | |
| 717 | static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj) |
| 718 | { |
| 719 | struct list_head *pos; |
| 720 | unsigned long flags; |
| 721 | |
| 722 | seq_printf(s, "%s %s", obj->name, obj->ops->driver_name); |
| 723 | |
| 724 | if (obj->ops->print_obj) { |
| 725 | seq_printf(s, ": "); |
| 726 | obj->ops->print_obj(s, obj); |
| 727 | } |
| 728 | |
| 729 | seq_printf(s, "\n"); |
| 730 | |
| 731 | spin_lock_irqsave(&obj->child_list_lock, flags); |
| 732 | list_for_each(pos, &obj->child_list_head) { |
| 733 | struct sync_pt *pt = |
| 734 | container_of(pos, struct sync_pt, child_list); |
| 735 | sync_print_pt(s, pt, false); |
| 736 | } |
| 737 | spin_unlock_irqrestore(&obj->child_list_lock, flags); |
| 738 | } |
| 739 | |
| 740 | static void sync_print_fence(struct seq_file *s, struct sync_fence *fence) |
| 741 | { |
| 742 | struct list_head *pos; |
| 743 | unsigned long flags; |
| 744 | |
| 745 | seq_printf(s, "%s: %s\n", fence->name, sync_status_str(fence->status)); |
| 746 | |
| 747 | list_for_each(pos, &fence->pt_list_head) { |
| 748 | struct sync_pt *pt = |
| 749 | container_of(pos, struct sync_pt, pt_list); |
| 750 | sync_print_pt(s, pt, true); |
| 751 | } |
| 752 | |
| 753 | spin_lock_irqsave(&fence->waiter_list_lock, flags); |
| 754 | list_for_each(pos, &fence->waiter_list_head) { |
| 755 | struct sync_fence_waiter *waiter = |
| 756 | container_of(pos, struct sync_fence_waiter, |
| 757 | waiter_list); |
| 758 | |
| Erik Gilling | c80114f | 2012-05-15 16:23:26 -0700 | [diff] [blame^] | 759 | seq_printf(s, "waiter %pF\n", waiter->callback); |
| Erik Gilling | 981c8a9 | 2012-03-14 19:49:15 -0700 | [diff] [blame] | 760 | } |
| 761 | spin_unlock_irqrestore(&fence->waiter_list_lock, flags); |
| 762 | } |
| 763 | |
| 764 | static int sync_debugfs_show(struct seq_file *s, void *unused) |
| 765 | { |
| 766 | unsigned long flags; |
| 767 | struct list_head *pos; |
| 768 | |
| 769 | seq_printf(s, "objs:\n--------------\n"); |
| 770 | |
| 771 | spin_lock_irqsave(&sync_timeline_list_lock, flags); |
| 772 | list_for_each(pos, &sync_timeline_list_head) { |
| 773 | struct sync_timeline *obj = |
| 774 | container_of(pos, struct sync_timeline, |
| 775 | sync_timeline_list); |
| 776 | |
| 777 | sync_print_obj(s, obj); |
| 778 | seq_printf(s, "\n"); |
| 779 | } |
| 780 | spin_unlock_irqrestore(&sync_timeline_list_lock, flags); |
| 781 | |
| 782 | seq_printf(s, "fences:\n--------------\n"); |
| 783 | |
| 784 | spin_lock_irqsave(&sync_fence_list_lock, flags); |
| 785 | list_for_each(pos, &sync_fence_list_head) { |
| 786 | struct sync_fence *fence = |
| 787 | container_of(pos, struct sync_fence, sync_fence_list); |
| 788 | |
| 789 | sync_print_fence(s, fence); |
| 790 | seq_printf(s, "\n"); |
| 791 | } |
| 792 | spin_unlock_irqrestore(&sync_fence_list_lock, flags); |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | static int sync_debugfs_open(struct inode *inode, struct file *file) |
| 797 | { |
| 798 | return single_open(file, sync_debugfs_show, inode->i_private); |
| 799 | } |
| 800 | |
| 801 | static const struct file_operations sync_debugfs_fops = { |
| 802 | .open = sync_debugfs_open, |
| 803 | .read = seq_read, |
| 804 | .llseek = seq_lseek, |
| 805 | .release = single_release, |
| 806 | }; |
| 807 | |
| 808 | static __init int sync_debugfs_init(void) |
| 809 | { |
| 810 | debugfs_create_file("sync", S_IRUGO, NULL, NULL, &sync_debugfs_fops); |
| 811 | return 0; |
| 812 | } |
| 813 | |
| 814 | late_initcall(sync_debugfs_init); |
| 815 | |
| 816 | #endif |