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