Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2016 Intel Corporation |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; version 2 |
| 7 | * of the License. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/slab.h> |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 11 | #include <linux/dma-fence.h> |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 12 | #include <linux/reservation.h> |
| 13 | |
| 14 | #include "i915_sw_fence.h" |
Chris Wilson | 47624cc | 2017-05-17 13:09:57 +0100 | [diff] [blame^] | 15 | #include "i915_selftest.h" |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 16 | |
Chris Wilson | 7e94186 | 2016-10-28 13:58:25 +0100 | [diff] [blame] | 17 | #define I915_SW_FENCE_FLAG_ALLOC BIT(3) /* after WQ_FLAG_* for safety */ |
| 18 | |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 19 | static DEFINE_SPINLOCK(i915_sw_fence_lock); |
| 20 | |
Chris Wilson | fc15840 | 2016-11-25 13:17:18 +0000 | [diff] [blame] | 21 | enum { |
| 22 | DEBUG_FENCE_IDLE = 0, |
| 23 | DEBUG_FENCE_NOTIFY, |
| 24 | }; |
| 25 | |
| 26 | #ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS |
| 27 | |
| 28 | static void *i915_sw_fence_debug_hint(void *addr) |
| 29 | { |
| 30 | return (void *)(((struct i915_sw_fence *)addr)->flags & I915_SW_FENCE_MASK); |
| 31 | } |
| 32 | |
| 33 | static struct debug_obj_descr i915_sw_fence_debug_descr = { |
| 34 | .name = "i915_sw_fence", |
| 35 | .debug_hint = i915_sw_fence_debug_hint, |
| 36 | }; |
| 37 | |
| 38 | static inline void debug_fence_init(struct i915_sw_fence *fence) |
| 39 | { |
| 40 | debug_object_init(fence, &i915_sw_fence_debug_descr); |
| 41 | } |
| 42 | |
| 43 | static inline void debug_fence_activate(struct i915_sw_fence *fence) |
| 44 | { |
| 45 | debug_object_activate(fence, &i915_sw_fence_debug_descr); |
| 46 | } |
| 47 | |
| 48 | static inline void debug_fence_set_state(struct i915_sw_fence *fence, |
| 49 | int old, int new) |
| 50 | { |
| 51 | debug_object_active_state(fence, &i915_sw_fence_debug_descr, old, new); |
| 52 | } |
| 53 | |
| 54 | static inline void debug_fence_deactivate(struct i915_sw_fence *fence) |
| 55 | { |
| 56 | debug_object_deactivate(fence, &i915_sw_fence_debug_descr); |
| 57 | } |
| 58 | |
| 59 | static inline void debug_fence_destroy(struct i915_sw_fence *fence) |
| 60 | { |
| 61 | debug_object_destroy(fence, &i915_sw_fence_debug_descr); |
| 62 | } |
| 63 | |
| 64 | static inline void debug_fence_free(struct i915_sw_fence *fence) |
| 65 | { |
| 66 | debug_object_free(fence, &i915_sw_fence_debug_descr); |
Chris Wilson | 6f13f29 | 2017-01-13 21:43:35 +0000 | [diff] [blame] | 67 | smp_wmb(); /* flush the change in state before reallocation */ |
Chris Wilson | fc15840 | 2016-11-25 13:17:18 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static inline void debug_fence_assert(struct i915_sw_fence *fence) |
| 71 | { |
| 72 | debug_object_assert_init(fence, &i915_sw_fence_debug_descr); |
| 73 | } |
| 74 | |
| 75 | #else |
| 76 | |
| 77 | static inline void debug_fence_init(struct i915_sw_fence *fence) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | static inline void debug_fence_activate(struct i915_sw_fence *fence) |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | static inline void debug_fence_set_state(struct i915_sw_fence *fence, |
| 86 | int old, int new) |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | static inline void debug_fence_deactivate(struct i915_sw_fence *fence) |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | static inline void debug_fence_destroy(struct i915_sw_fence *fence) |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | static inline void debug_fence_free(struct i915_sw_fence *fence) |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | static inline void debug_fence_assert(struct i915_sw_fence *fence) |
| 103 | { |
| 104 | } |
| 105 | |
| 106 | #endif |
| 107 | |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 108 | static int __i915_sw_fence_notify(struct i915_sw_fence *fence, |
| 109 | enum i915_sw_fence_notify state) |
| 110 | { |
| 111 | i915_sw_fence_notify_t fn; |
| 112 | |
| 113 | fn = (i915_sw_fence_notify_t)(fence->flags & I915_SW_FENCE_MASK); |
| 114 | return fn(fence, state); |
| 115 | } |
| 116 | |
Chris Wilson | fc15840 | 2016-11-25 13:17:18 +0000 | [diff] [blame] | 117 | #ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS |
| 118 | void i915_sw_fence_fini(struct i915_sw_fence *fence) |
| 119 | { |
| 120 | debug_fence_free(fence); |
| 121 | } |
| 122 | #endif |
| 123 | |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 124 | static void __i915_sw_fence_wake_up_all(struct i915_sw_fence *fence, |
| 125 | struct list_head *continuation) |
| 126 | { |
| 127 | wait_queue_head_t *x = &fence->wait; |
| 128 | wait_queue_t *pos, *next; |
| 129 | unsigned long flags; |
| 130 | |
Chris Wilson | fc15840 | 2016-11-25 13:17:18 +0000 | [diff] [blame] | 131 | debug_fence_deactivate(fence); |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 132 | atomic_set_release(&fence->pending, -1); /* 0 -> -1 [done] */ |
| 133 | |
| 134 | /* |
| 135 | * To prevent unbounded recursion as we traverse the graph of |
| 136 | * i915_sw_fences, we move the task_list from this, the next ready |
| 137 | * fence, to the tail of the original fence's task_list |
| 138 | * (and so added to the list to be woken). |
| 139 | */ |
| 140 | |
| 141 | spin_lock_irqsave_nested(&x->lock, flags, 1 + !!continuation); |
| 142 | if (continuation) { |
| 143 | list_for_each_entry_safe(pos, next, &x->task_list, task_list) { |
| 144 | if (pos->func == autoremove_wake_function) |
| 145 | pos->func(pos, TASK_NORMAL, 0, continuation); |
| 146 | else |
| 147 | list_move_tail(&pos->task_list, continuation); |
| 148 | } |
| 149 | } else { |
| 150 | LIST_HEAD(extra); |
| 151 | |
| 152 | do { |
| 153 | list_for_each_entry_safe(pos, next, |
| 154 | &x->task_list, task_list) |
| 155 | pos->func(pos, TASK_NORMAL, 0, &extra); |
| 156 | |
| 157 | if (list_empty(&extra)) |
| 158 | break; |
| 159 | |
| 160 | list_splice_tail_init(&extra, &x->task_list); |
| 161 | } while (1); |
| 162 | } |
| 163 | spin_unlock_irqrestore(&x->lock, flags); |
Chris Wilson | fc15840 | 2016-11-25 13:17:18 +0000 | [diff] [blame] | 164 | |
| 165 | debug_fence_assert(fence); |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static void __i915_sw_fence_complete(struct i915_sw_fence *fence, |
| 169 | struct list_head *continuation) |
| 170 | { |
Chris Wilson | fc15840 | 2016-11-25 13:17:18 +0000 | [diff] [blame] | 171 | debug_fence_assert(fence); |
| 172 | |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 173 | if (!atomic_dec_and_test(&fence->pending)) |
| 174 | return; |
| 175 | |
Chris Wilson | fc15840 | 2016-11-25 13:17:18 +0000 | [diff] [blame] | 176 | debug_fence_set_state(fence, DEBUG_FENCE_IDLE, DEBUG_FENCE_NOTIFY); |
| 177 | |
Chris Wilson | 9310cb7 | 2017-05-17 13:09:56 +0100 | [diff] [blame] | 178 | if (__i915_sw_fence_notify(fence, FENCE_COMPLETE) != NOTIFY_DONE) |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 179 | return; |
| 180 | |
Chris Wilson | fc15840 | 2016-11-25 13:17:18 +0000 | [diff] [blame] | 181 | debug_fence_set_state(fence, DEBUG_FENCE_NOTIFY, DEBUG_FENCE_IDLE); |
| 182 | |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 183 | __i915_sw_fence_wake_up_all(fence, continuation); |
Chris Wilson | 9310cb7 | 2017-05-17 13:09:56 +0100 | [diff] [blame] | 184 | |
| 185 | debug_fence_destroy(fence); |
| 186 | __i915_sw_fence_notify(fence, FENCE_FREE); |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static void i915_sw_fence_complete(struct i915_sw_fence *fence) |
| 190 | { |
Chris Wilson | fc15840 | 2016-11-25 13:17:18 +0000 | [diff] [blame] | 191 | debug_fence_assert(fence); |
| 192 | |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 193 | if (WARN_ON(i915_sw_fence_done(fence))) |
| 194 | return; |
| 195 | |
| 196 | __i915_sw_fence_complete(fence, NULL); |
| 197 | } |
| 198 | |
| 199 | static void i915_sw_fence_await(struct i915_sw_fence *fence) |
| 200 | { |
Chris Wilson | fc15840 | 2016-11-25 13:17:18 +0000 | [diff] [blame] | 201 | debug_fence_assert(fence); |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 202 | WARN_ON(atomic_inc_return(&fence->pending) <= 1); |
| 203 | } |
| 204 | |
Chris Wilson | 556b748 | 2016-11-14 20:40:56 +0000 | [diff] [blame] | 205 | void __i915_sw_fence_init(struct i915_sw_fence *fence, |
| 206 | i915_sw_fence_notify_t fn, |
| 207 | const char *name, |
| 208 | struct lock_class_key *key) |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 209 | { |
Chris Wilson | 9310cb7 | 2017-05-17 13:09:56 +0100 | [diff] [blame] | 210 | BUG_ON(!fn || (unsigned long)fn & ~I915_SW_FENCE_MASK); |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 211 | |
Chris Wilson | fc15840 | 2016-11-25 13:17:18 +0000 | [diff] [blame] | 212 | debug_fence_init(fence); |
| 213 | |
Chris Wilson | 556b748 | 2016-11-14 20:40:56 +0000 | [diff] [blame] | 214 | __init_waitqueue_head(&fence->wait, name, key); |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 215 | atomic_set(&fence->pending, 1); |
| 216 | fence->flags = (unsigned long)fn; |
| 217 | } |
| 218 | |
Chris Wilson | fc15840 | 2016-11-25 13:17:18 +0000 | [diff] [blame] | 219 | void i915_sw_fence_commit(struct i915_sw_fence *fence) |
| 220 | { |
| 221 | debug_fence_activate(fence); |
Chris Wilson | 9310cb7 | 2017-05-17 13:09:56 +0100 | [diff] [blame] | 222 | i915_sw_fence_complete(fence); |
Chris Wilson | fc15840 | 2016-11-25 13:17:18 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 225 | static int i915_sw_fence_wake(wait_queue_t *wq, unsigned mode, int flags, void *key) |
| 226 | { |
| 227 | list_del(&wq->task_list); |
| 228 | __i915_sw_fence_complete(wq->private, key); |
Chris Wilson | 9310cb7 | 2017-05-17 13:09:56 +0100 | [diff] [blame] | 229 | |
Chris Wilson | 7e94186 | 2016-10-28 13:58:25 +0100 | [diff] [blame] | 230 | if (wq->flags & I915_SW_FENCE_FLAG_ALLOC) |
| 231 | kfree(wq); |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static bool __i915_sw_fence_check_if_after(struct i915_sw_fence *fence, |
| 236 | const struct i915_sw_fence * const signaler) |
| 237 | { |
| 238 | wait_queue_t *wq; |
| 239 | |
| 240 | if (__test_and_set_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags)) |
| 241 | return false; |
| 242 | |
| 243 | if (fence == signaler) |
| 244 | return true; |
| 245 | |
| 246 | list_for_each_entry(wq, &fence->wait.task_list, task_list) { |
| 247 | if (wq->func != i915_sw_fence_wake) |
| 248 | continue; |
| 249 | |
| 250 | if (__i915_sw_fence_check_if_after(wq->private, signaler)) |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | static void __i915_sw_fence_clear_checked_bit(struct i915_sw_fence *fence) |
| 258 | { |
| 259 | wait_queue_t *wq; |
| 260 | |
| 261 | if (!__test_and_clear_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags)) |
| 262 | return; |
| 263 | |
| 264 | list_for_each_entry(wq, &fence->wait.task_list, task_list) { |
| 265 | if (wq->func != i915_sw_fence_wake) |
| 266 | continue; |
| 267 | |
| 268 | __i915_sw_fence_clear_checked_bit(wq->private); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | static bool i915_sw_fence_check_if_after(struct i915_sw_fence *fence, |
| 273 | const struct i915_sw_fence * const signaler) |
| 274 | { |
| 275 | unsigned long flags; |
| 276 | bool err; |
| 277 | |
Chris Wilson | 47624cc | 2017-05-17 13:09:57 +0100 | [diff] [blame^] | 278 | if (!IS_ENABLED(CONFIG_DRM_I915_SW_FENCE_CHECK_DAG)) |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 279 | return false; |
| 280 | |
| 281 | spin_lock_irqsave(&i915_sw_fence_lock, flags); |
| 282 | err = __i915_sw_fence_check_if_after(fence, signaler); |
| 283 | __i915_sw_fence_clear_checked_bit(fence); |
| 284 | spin_unlock_irqrestore(&i915_sw_fence_lock, flags); |
| 285 | |
| 286 | return err; |
| 287 | } |
| 288 | |
Chris Wilson | 7e94186 | 2016-10-28 13:58:25 +0100 | [diff] [blame] | 289 | static int __i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence, |
| 290 | struct i915_sw_fence *signaler, |
| 291 | wait_queue_t *wq, gfp_t gfp) |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 292 | { |
| 293 | unsigned long flags; |
| 294 | int pending; |
| 295 | |
Chris Wilson | fc15840 | 2016-11-25 13:17:18 +0000 | [diff] [blame] | 296 | debug_fence_assert(fence); |
| 297 | |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 298 | if (i915_sw_fence_done(signaler)) |
| 299 | return 0; |
| 300 | |
Chris Wilson | fc15840 | 2016-11-25 13:17:18 +0000 | [diff] [blame] | 301 | debug_fence_assert(signaler); |
| 302 | |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 303 | /* The dependency graph must be acyclic. */ |
| 304 | if (unlikely(i915_sw_fence_check_if_after(fence, signaler))) |
| 305 | return -EINVAL; |
| 306 | |
Chris Wilson | 7e94186 | 2016-10-28 13:58:25 +0100 | [diff] [blame] | 307 | pending = 0; |
| 308 | if (!wq) { |
| 309 | wq = kmalloc(sizeof(*wq), gfp); |
| 310 | if (!wq) { |
| 311 | if (!gfpflags_allow_blocking(gfp)) |
| 312 | return -ENOMEM; |
| 313 | |
| 314 | i915_sw_fence_wait(signaler); |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | pending |= I915_SW_FENCE_FLAG_ALLOC; |
| 319 | } |
| 320 | |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 321 | INIT_LIST_HEAD(&wq->task_list); |
Chris Wilson | 7e94186 | 2016-10-28 13:58:25 +0100 | [diff] [blame] | 322 | wq->flags = pending; |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 323 | wq->func = i915_sw_fence_wake; |
Chris Wilson | 9310cb7 | 2017-05-17 13:09:56 +0100 | [diff] [blame] | 324 | wq->private = fence; |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 325 | |
| 326 | i915_sw_fence_await(fence); |
| 327 | |
| 328 | spin_lock_irqsave(&signaler->wait.lock, flags); |
| 329 | if (likely(!i915_sw_fence_done(signaler))) { |
| 330 | __add_wait_queue_tail(&signaler->wait, wq); |
| 331 | pending = 1; |
| 332 | } else { |
| 333 | i915_sw_fence_wake(wq, 0, 0, NULL); |
| 334 | pending = 0; |
| 335 | } |
| 336 | spin_unlock_irqrestore(&signaler->wait.lock, flags); |
| 337 | |
| 338 | return pending; |
| 339 | } |
| 340 | |
Chris Wilson | 7e94186 | 2016-10-28 13:58:25 +0100 | [diff] [blame] | 341 | int i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence, |
| 342 | struct i915_sw_fence *signaler, |
| 343 | wait_queue_t *wq) |
| 344 | { |
| 345 | return __i915_sw_fence_await_sw_fence(fence, signaler, wq, 0); |
| 346 | } |
| 347 | |
| 348 | int i915_sw_fence_await_sw_fence_gfp(struct i915_sw_fence *fence, |
| 349 | struct i915_sw_fence *signaler, |
| 350 | gfp_t gfp) |
| 351 | { |
| 352 | return __i915_sw_fence_await_sw_fence(fence, signaler, NULL, gfp); |
| 353 | } |
| 354 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 355 | struct i915_sw_dma_fence_cb { |
| 356 | struct dma_fence_cb base; |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 357 | struct i915_sw_fence *fence; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 358 | struct dma_fence *dma; |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 359 | struct timer_list timer; |
| 360 | }; |
| 361 | |
| 362 | static void timer_i915_sw_fence_wake(unsigned long data) |
| 363 | { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 364 | struct i915_sw_dma_fence_cb *cb = (struct i915_sw_dma_fence_cb *)data; |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 365 | |
Joe Perches | 8dfe162 | 2017-02-28 04:55:54 -0800 | [diff] [blame] | 366 | pr_warn("asynchronous wait on fence %s:%s:%x timed out\n", |
| 367 | cb->dma->ops->get_driver_name(cb->dma), |
| 368 | cb->dma->ops->get_timeline_name(cb->dma), |
| 369 | cb->dma->seqno); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 370 | dma_fence_put(cb->dma); |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 371 | cb->dma = NULL; |
| 372 | |
Chris Wilson | 9310cb7 | 2017-05-17 13:09:56 +0100 | [diff] [blame] | 373 | i915_sw_fence_complete(cb->fence); |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 374 | cb->timer.function = NULL; |
| 375 | } |
| 376 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 377 | static void dma_i915_sw_fence_wake(struct dma_fence *dma, |
| 378 | struct dma_fence_cb *data) |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 379 | { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 380 | struct i915_sw_dma_fence_cb *cb = container_of(data, typeof(*cb), base); |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 381 | |
| 382 | del_timer_sync(&cb->timer); |
| 383 | if (cb->timer.function) |
Chris Wilson | 9310cb7 | 2017-05-17 13:09:56 +0100 | [diff] [blame] | 384 | i915_sw_fence_complete(cb->fence); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 385 | dma_fence_put(cb->dma); |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 386 | |
| 387 | kfree(cb); |
| 388 | } |
| 389 | |
| 390 | int i915_sw_fence_await_dma_fence(struct i915_sw_fence *fence, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 391 | struct dma_fence *dma, |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 392 | unsigned long timeout, |
| 393 | gfp_t gfp) |
| 394 | { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 395 | struct i915_sw_dma_fence_cb *cb; |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 396 | int ret; |
| 397 | |
Chris Wilson | fc15840 | 2016-11-25 13:17:18 +0000 | [diff] [blame] | 398 | debug_fence_assert(fence); |
| 399 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 400 | if (dma_fence_is_signaled(dma)) |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 401 | return 0; |
| 402 | |
| 403 | cb = kmalloc(sizeof(*cb), gfp); |
| 404 | if (!cb) { |
| 405 | if (!gfpflags_allow_blocking(gfp)) |
| 406 | return -ENOMEM; |
| 407 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 408 | return dma_fence_wait(dma, false); |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 409 | } |
| 410 | |
Chris Wilson | 9310cb7 | 2017-05-17 13:09:56 +0100 | [diff] [blame] | 411 | cb->fence = fence; |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 412 | i915_sw_fence_await(fence); |
| 413 | |
| 414 | cb->dma = NULL; |
| 415 | __setup_timer(&cb->timer, |
| 416 | timer_i915_sw_fence_wake, (unsigned long)cb, |
| 417 | TIMER_IRQSAFE); |
| 418 | if (timeout) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 419 | cb->dma = dma_fence_get(dma); |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 420 | mod_timer(&cb->timer, round_jiffies_up(jiffies + timeout)); |
| 421 | } |
| 422 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 423 | ret = dma_fence_add_callback(dma, &cb->base, dma_i915_sw_fence_wake); |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 424 | if (ret == 0) { |
| 425 | ret = 1; |
| 426 | } else { |
| 427 | dma_i915_sw_fence_wake(dma, &cb->base); |
| 428 | if (ret == -ENOENT) /* fence already signaled */ |
| 429 | ret = 0; |
| 430 | } |
| 431 | |
| 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | int i915_sw_fence_await_reservation(struct i915_sw_fence *fence, |
| 436 | struct reservation_object *resv, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 437 | const struct dma_fence_ops *exclude, |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 438 | bool write, |
| 439 | unsigned long timeout, |
| 440 | gfp_t gfp) |
| 441 | { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 442 | struct dma_fence *excl; |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 443 | int ret = 0, pending; |
| 444 | |
Chris Wilson | fc15840 | 2016-11-25 13:17:18 +0000 | [diff] [blame] | 445 | debug_fence_assert(fence); |
| 446 | |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 447 | if (write) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 448 | struct dma_fence **shared; |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 449 | unsigned int count, i; |
| 450 | |
| 451 | ret = reservation_object_get_fences_rcu(resv, |
| 452 | &excl, &count, &shared); |
| 453 | if (ret) |
| 454 | return ret; |
| 455 | |
| 456 | for (i = 0; i < count; i++) { |
| 457 | if (shared[i]->ops == exclude) |
| 458 | continue; |
| 459 | |
| 460 | pending = i915_sw_fence_await_dma_fence(fence, |
| 461 | shared[i], |
| 462 | timeout, |
| 463 | gfp); |
| 464 | if (pending < 0) { |
| 465 | ret = pending; |
| 466 | break; |
| 467 | } |
| 468 | |
| 469 | ret |= pending; |
| 470 | } |
| 471 | |
| 472 | for (i = 0; i < count; i++) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 473 | dma_fence_put(shared[i]); |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 474 | kfree(shared); |
| 475 | } else { |
| 476 | excl = reservation_object_get_excl_rcu(resv); |
| 477 | } |
| 478 | |
| 479 | if (ret >= 0 && excl && excl->ops != exclude) { |
| 480 | pending = i915_sw_fence_await_dma_fence(fence, |
| 481 | excl, |
| 482 | timeout, |
| 483 | gfp); |
| 484 | if (pending < 0) |
| 485 | ret = pending; |
| 486 | else |
| 487 | ret |= pending; |
| 488 | } |
| 489 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 490 | dma_fence_put(excl); |
Chris Wilson | e68a139 | 2016-09-09 14:11:41 +0100 | [diff] [blame] | 491 | |
| 492 | return ret; |
| 493 | } |
Chris Wilson | 47624cc | 2017-05-17 13:09:57 +0100 | [diff] [blame^] | 494 | |
| 495 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 496 | #include "selftests/i915_sw_fence.c" |
| 497 | #endif |