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