blob: f29540f922af196542c8fb8a2b5b576385c29217 [file] [log] [blame]
Chris Wilsone68a1392016-09-09 14:11:41 +01001/*
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 Wilsonf54d1862016-10-25 13:00:45 +010011#include <linux/dma-fence.h>
Chris Wilsone68a1392016-09-09 14:11:41 +010012#include <linux/reservation.h>
13
14#include "i915_sw_fence.h"
Chris Wilson47624cc2017-05-17 13:09:57 +010015#include "i915_selftest.h"
Chris Wilsone68a1392016-09-09 14:11:41 +010016
Chris Wilson7e941862016-10-28 13:58:25 +010017#define I915_SW_FENCE_FLAG_ALLOC BIT(3) /* after WQ_FLAG_* for safety */
18
Chris Wilsone68a1392016-09-09 14:11:41 +010019static DEFINE_SPINLOCK(i915_sw_fence_lock);
20
Chris Wilsonfc158402016-11-25 13:17:18 +000021enum {
22 DEBUG_FENCE_IDLE = 0,
23 DEBUG_FENCE_NOTIFY,
24};
25
26#ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS
27
28static void *i915_sw_fence_debug_hint(void *addr)
29{
30 return (void *)(((struct i915_sw_fence *)addr)->flags & I915_SW_FENCE_MASK);
31}
32
33static struct debug_obj_descr i915_sw_fence_debug_descr = {
34 .name = "i915_sw_fence",
35 .debug_hint = i915_sw_fence_debug_hint,
36};
37
38static inline void debug_fence_init(struct i915_sw_fence *fence)
39{
40 debug_object_init(fence, &i915_sw_fence_debug_descr);
41}
42
43static inline void debug_fence_activate(struct i915_sw_fence *fence)
44{
45 debug_object_activate(fence, &i915_sw_fence_debug_descr);
46}
47
48static 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
54static inline void debug_fence_deactivate(struct i915_sw_fence *fence)
55{
56 debug_object_deactivate(fence, &i915_sw_fence_debug_descr);
57}
58
59static inline void debug_fence_destroy(struct i915_sw_fence *fence)
60{
61 debug_object_destroy(fence, &i915_sw_fence_debug_descr);
62}
63
64static inline void debug_fence_free(struct i915_sw_fence *fence)
65{
66 debug_object_free(fence, &i915_sw_fence_debug_descr);
Chris Wilson6f13f292017-01-13 21:43:35 +000067 smp_wmb(); /* flush the change in state before reallocation */
Chris Wilsonfc158402016-11-25 13:17:18 +000068}
69
70static 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
77static inline void debug_fence_init(struct i915_sw_fence *fence)
78{
79}
80
81static inline void debug_fence_activate(struct i915_sw_fence *fence)
82{
83}
84
85static inline void debug_fence_set_state(struct i915_sw_fence *fence,
86 int old, int new)
87{
88}
89
90static inline void debug_fence_deactivate(struct i915_sw_fence *fence)
91{
92}
93
94static inline void debug_fence_destroy(struct i915_sw_fence *fence)
95{
96}
97
98static inline void debug_fence_free(struct i915_sw_fence *fence)
99{
100}
101
102static inline void debug_fence_assert(struct i915_sw_fence *fence)
103{
104}
105
106#endif
107
Chris Wilsone68a1392016-09-09 14:11:41 +0100108static 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 Wilsonfc158402016-11-25 13:17:18 +0000117#ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS
118void i915_sw_fence_fini(struct i915_sw_fence *fence)
119{
120 debug_fence_free(fence);
121}
122#endif
123
Chris Wilsone68a1392016-09-09 14:11:41 +0100124static 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;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200128 wait_queue_entry_t *pos, *next;
Chris Wilsone68a1392016-09-09 14:11:41 +0100129 unsigned long flags;
130
Chris Wilsonfc158402016-11-25 13:17:18 +0000131 debug_fence_deactivate(fence);
Chris Wilsone68a1392016-09-09 14:11:41 +0100132 atomic_set_release(&fence->pending, -1); /* 0 -> -1 [done] */
133
134 /*
135 * To prevent unbounded recursion as we traverse the graph of
Ingo Molnar2055da92017-06-20 12:06:46 +0200136 * i915_sw_fences, we move the entry list from this, the next ready
137 * fence, to the tail of the original fence's entry list
Chris Wilsone68a1392016-09-09 14:11:41 +0100138 * (and so added to the list to be woken).
139 */
140
141 spin_lock_irqsave_nested(&x->lock, flags, 1 + !!continuation);
142 if (continuation) {
Ingo Molnar2055da92017-06-20 12:06:46 +0200143 list_for_each_entry_safe(pos, next, &x->head, entry) {
Chris Wilsone68a1392016-09-09 14:11:41 +0100144 if (pos->func == autoremove_wake_function)
145 pos->func(pos, TASK_NORMAL, 0, continuation);
146 else
Ingo Molnar2055da92017-06-20 12:06:46 +0200147 list_move_tail(&pos->entry, continuation);
Chris Wilsone68a1392016-09-09 14:11:41 +0100148 }
149 } else {
150 LIST_HEAD(extra);
151
152 do {
Ingo Molnar2055da92017-06-20 12:06:46 +0200153 list_for_each_entry_safe(pos, next, &x->head, entry)
Chris Wilsone68a1392016-09-09 14:11:41 +0100154 pos->func(pos, TASK_NORMAL, 0, &extra);
155
156 if (list_empty(&extra))
157 break;
158
Ingo Molnar2055da92017-06-20 12:06:46 +0200159 list_splice_tail_init(&extra, &x->head);
Chris Wilsone68a1392016-09-09 14:11:41 +0100160 } while (1);
161 }
162 spin_unlock_irqrestore(&x->lock, flags);
Chris Wilsonfc158402016-11-25 13:17:18 +0000163
164 debug_fence_assert(fence);
Chris Wilsone68a1392016-09-09 14:11:41 +0100165}
166
167static void __i915_sw_fence_complete(struct i915_sw_fence *fence,
168 struct list_head *continuation)
169{
Chris Wilsonfc158402016-11-25 13:17:18 +0000170 debug_fence_assert(fence);
171
Chris Wilsone68a1392016-09-09 14:11:41 +0100172 if (!atomic_dec_and_test(&fence->pending))
173 return;
174
Chris Wilsonfc158402016-11-25 13:17:18 +0000175 debug_fence_set_state(fence, DEBUG_FENCE_IDLE, DEBUG_FENCE_NOTIFY);
176
Chris Wilson9310cb72017-05-17 13:09:56 +0100177 if (__i915_sw_fence_notify(fence, FENCE_COMPLETE) != NOTIFY_DONE)
Chris Wilsone68a1392016-09-09 14:11:41 +0100178 return;
179
Chris Wilsonfc158402016-11-25 13:17:18 +0000180 debug_fence_set_state(fence, DEBUG_FENCE_NOTIFY, DEBUG_FENCE_IDLE);
181
Chris Wilsone68a1392016-09-09 14:11:41 +0100182 __i915_sw_fence_wake_up_all(fence, continuation);
Chris Wilson9310cb72017-05-17 13:09:56 +0100183
184 debug_fence_destroy(fence);
185 __i915_sw_fence_notify(fence, FENCE_FREE);
Chris Wilsone68a1392016-09-09 14:11:41 +0100186}
187
188static void i915_sw_fence_complete(struct i915_sw_fence *fence)
189{
Chris Wilsonfc158402016-11-25 13:17:18 +0000190 debug_fence_assert(fence);
191
Chris Wilsone68a1392016-09-09 14:11:41 +0100192 if (WARN_ON(i915_sw_fence_done(fence)))
193 return;
194
195 __i915_sw_fence_complete(fence, NULL);
196}
197
198static void i915_sw_fence_await(struct i915_sw_fence *fence)
199{
Chris Wilsonfc158402016-11-25 13:17:18 +0000200 debug_fence_assert(fence);
Chris Wilsone68a1392016-09-09 14:11:41 +0100201 WARN_ON(atomic_inc_return(&fence->pending) <= 1);
202}
203
Chris Wilson556b7482016-11-14 20:40:56 +0000204void __i915_sw_fence_init(struct i915_sw_fence *fence,
205 i915_sw_fence_notify_t fn,
206 const char *name,
207 struct lock_class_key *key)
Chris Wilsone68a1392016-09-09 14:11:41 +0100208{
Chris Wilson9310cb72017-05-17 13:09:56 +0100209 BUG_ON(!fn || (unsigned long)fn & ~I915_SW_FENCE_MASK);
Chris Wilsone68a1392016-09-09 14:11:41 +0100210
Chris Wilsonfc158402016-11-25 13:17:18 +0000211 debug_fence_init(fence);
212
Chris Wilson556b7482016-11-14 20:40:56 +0000213 __init_waitqueue_head(&fence->wait, name, key);
Chris Wilsone68a1392016-09-09 14:11:41 +0100214 atomic_set(&fence->pending, 1);
215 fence->flags = (unsigned long)fn;
216}
217
Chris Wilsonfc158402016-11-25 13:17:18 +0000218void i915_sw_fence_commit(struct i915_sw_fence *fence)
219{
220 debug_fence_activate(fence);
Chris Wilson9310cb72017-05-17 13:09:56 +0100221 i915_sw_fence_complete(fence);
Chris Wilsonfc158402016-11-25 13:17:18 +0000222}
223
Ingo Molnarac6424b2017-06-20 12:06:13 +0200224static int i915_sw_fence_wake(wait_queue_entry_t *wq, unsigned mode, int flags, void *key)
Chris Wilsone68a1392016-09-09 14:11:41 +0100225{
Ingo Molnar2055da92017-06-20 12:06:46 +0200226 list_del(&wq->entry);
Chris Wilsone68a1392016-09-09 14:11:41 +0100227 __i915_sw_fence_complete(wq->private, key);
Chris Wilson9310cb72017-05-17 13:09:56 +0100228
Chris Wilson7e941862016-10-28 13:58:25 +0100229 if (wq->flags & I915_SW_FENCE_FLAG_ALLOC)
230 kfree(wq);
Chris Wilsone68a1392016-09-09 14:11:41 +0100231 return 0;
232}
233
234static bool __i915_sw_fence_check_if_after(struct i915_sw_fence *fence,
235 const struct i915_sw_fence * const signaler)
236{
Ingo Molnarac6424b2017-06-20 12:06:13 +0200237 wait_queue_entry_t *wq;
Chris Wilsone68a1392016-09-09 14:11:41 +0100238
239 if (__test_and_set_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags))
240 return false;
241
242 if (fence == signaler)
243 return true;
244
Ingo Molnar2055da92017-06-20 12:06:46 +0200245 list_for_each_entry(wq, &fence->wait.head, entry) {
Chris Wilsone68a1392016-09-09 14:11:41 +0100246 if (wq->func != i915_sw_fence_wake)
247 continue;
248
249 if (__i915_sw_fence_check_if_after(wq->private, signaler))
250 return true;
251 }
252
253 return false;
254}
255
256static void __i915_sw_fence_clear_checked_bit(struct i915_sw_fence *fence)
257{
Ingo Molnarac6424b2017-06-20 12:06:13 +0200258 wait_queue_entry_t *wq;
Chris Wilsone68a1392016-09-09 14:11:41 +0100259
260 if (!__test_and_clear_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags))
261 return;
262
Ingo Molnar2055da92017-06-20 12:06:46 +0200263 list_for_each_entry(wq, &fence->wait.head, entry) {
Chris Wilsone68a1392016-09-09 14:11:41 +0100264 if (wq->func != i915_sw_fence_wake)
265 continue;
266
267 __i915_sw_fence_clear_checked_bit(wq->private);
268 }
269}
270
271static bool i915_sw_fence_check_if_after(struct i915_sw_fence *fence,
272 const struct i915_sw_fence * const signaler)
273{
274 unsigned long flags;
275 bool err;
276
Chris Wilson47624cc2017-05-17 13:09:57 +0100277 if (!IS_ENABLED(CONFIG_DRM_I915_SW_FENCE_CHECK_DAG))
Chris Wilsone68a1392016-09-09 14:11:41 +0100278 return false;
279
280 spin_lock_irqsave(&i915_sw_fence_lock, flags);
281 err = __i915_sw_fence_check_if_after(fence, signaler);
282 __i915_sw_fence_clear_checked_bit(fence);
283 spin_unlock_irqrestore(&i915_sw_fence_lock, flags);
284
285 return err;
286}
287
Chris Wilson7e941862016-10-28 13:58:25 +0100288static int __i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence,
289 struct i915_sw_fence *signaler,
Ingo Molnarac6424b2017-06-20 12:06:13 +0200290 wait_queue_entry_t *wq, gfp_t gfp)
Chris Wilsone68a1392016-09-09 14:11:41 +0100291{
292 unsigned long flags;
293 int pending;
294
Chris Wilsonfc158402016-11-25 13:17:18 +0000295 debug_fence_assert(fence);
296
Chris Wilsone68a1392016-09-09 14:11:41 +0100297 if (i915_sw_fence_done(signaler))
298 return 0;
299
Chris Wilsonfc158402016-11-25 13:17:18 +0000300 debug_fence_assert(signaler);
301
Chris Wilsone68a1392016-09-09 14:11:41 +0100302 /* The dependency graph must be acyclic. */
303 if (unlikely(i915_sw_fence_check_if_after(fence, signaler)))
304 return -EINVAL;
305
Chris Wilson7e941862016-10-28 13:58:25 +0100306 pending = 0;
307 if (!wq) {
308 wq = kmalloc(sizeof(*wq), gfp);
309 if (!wq) {
310 if (!gfpflags_allow_blocking(gfp))
311 return -ENOMEM;
312
313 i915_sw_fence_wait(signaler);
314 return 0;
315 }
316
317 pending |= I915_SW_FENCE_FLAG_ALLOC;
318 }
319
Ingo Molnar2055da92017-06-20 12:06:46 +0200320 INIT_LIST_HEAD(&wq->entry);
Chris Wilson7e941862016-10-28 13:58:25 +0100321 wq->flags = pending;
Chris Wilsone68a1392016-09-09 14:11:41 +0100322 wq->func = i915_sw_fence_wake;
Chris Wilson9310cb72017-05-17 13:09:56 +0100323 wq->private = fence;
Chris Wilsone68a1392016-09-09 14:11:41 +0100324
325 i915_sw_fence_await(fence);
326
327 spin_lock_irqsave(&signaler->wait.lock, flags);
328 if (likely(!i915_sw_fence_done(signaler))) {
Ingo Molnarac6424b2017-06-20 12:06:13 +0200329 __add_wait_queue_entry_tail(&signaler->wait, wq);
Chris Wilsone68a1392016-09-09 14:11:41 +0100330 pending = 1;
331 } else {
332 i915_sw_fence_wake(wq, 0, 0, NULL);
333 pending = 0;
334 }
335 spin_unlock_irqrestore(&signaler->wait.lock, flags);
336
337 return pending;
338}
339
Chris Wilson7e941862016-10-28 13:58:25 +0100340int i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence,
341 struct i915_sw_fence *signaler,
Ingo Molnarac6424b2017-06-20 12:06:13 +0200342 wait_queue_entry_t *wq)
Chris Wilson7e941862016-10-28 13:58:25 +0100343{
344 return __i915_sw_fence_await_sw_fence(fence, signaler, wq, 0);
345}
346
347int i915_sw_fence_await_sw_fence_gfp(struct i915_sw_fence *fence,
348 struct i915_sw_fence *signaler,
349 gfp_t gfp)
350{
351 return __i915_sw_fence_await_sw_fence(fence, signaler, NULL, gfp);
352}
353
Chris Wilsonf54d1862016-10-25 13:00:45 +0100354struct i915_sw_dma_fence_cb {
355 struct dma_fence_cb base;
Chris Wilsone68a1392016-09-09 14:11:41 +0100356 struct i915_sw_fence *fence;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100357 struct dma_fence *dma;
Chris Wilsone68a1392016-09-09 14:11:41 +0100358 struct timer_list timer;
359};
360
361static void timer_i915_sw_fence_wake(unsigned long data)
362{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100363 struct i915_sw_dma_fence_cb *cb = (struct i915_sw_dma_fence_cb *)data;
Chris Wilsone68a1392016-09-09 14:11:41 +0100364
Joe Perches8dfe1622017-02-28 04:55:54 -0800365 pr_warn("asynchronous wait on fence %s:%s:%x timed out\n",
366 cb->dma->ops->get_driver_name(cb->dma),
367 cb->dma->ops->get_timeline_name(cb->dma),
368 cb->dma->seqno);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100369 dma_fence_put(cb->dma);
Chris Wilsone68a1392016-09-09 14:11:41 +0100370 cb->dma = NULL;
371
Chris Wilson9310cb72017-05-17 13:09:56 +0100372 i915_sw_fence_complete(cb->fence);
Chris Wilsone68a1392016-09-09 14:11:41 +0100373 cb->timer.function = NULL;
374}
375
Chris Wilsonf54d1862016-10-25 13:00:45 +0100376static void dma_i915_sw_fence_wake(struct dma_fence *dma,
377 struct dma_fence_cb *data)
Chris Wilsone68a1392016-09-09 14:11:41 +0100378{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100379 struct i915_sw_dma_fence_cb *cb = container_of(data, typeof(*cb), base);
Chris Wilsone68a1392016-09-09 14:11:41 +0100380
381 del_timer_sync(&cb->timer);
382 if (cb->timer.function)
Chris Wilson9310cb72017-05-17 13:09:56 +0100383 i915_sw_fence_complete(cb->fence);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100384 dma_fence_put(cb->dma);
Chris Wilsone68a1392016-09-09 14:11:41 +0100385
386 kfree(cb);
387}
388
389int i915_sw_fence_await_dma_fence(struct i915_sw_fence *fence,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100390 struct dma_fence *dma,
Chris Wilsone68a1392016-09-09 14:11:41 +0100391 unsigned long timeout,
392 gfp_t gfp)
393{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100394 struct i915_sw_dma_fence_cb *cb;
Chris Wilsone68a1392016-09-09 14:11:41 +0100395 int ret;
396
Chris Wilsonfc158402016-11-25 13:17:18 +0000397 debug_fence_assert(fence);
398
Chris Wilsonf54d1862016-10-25 13:00:45 +0100399 if (dma_fence_is_signaled(dma))
Chris Wilsone68a1392016-09-09 14:11:41 +0100400 return 0;
401
402 cb = kmalloc(sizeof(*cb), gfp);
403 if (!cb) {
404 if (!gfpflags_allow_blocking(gfp))
405 return -ENOMEM;
406
Chris Wilsonf54d1862016-10-25 13:00:45 +0100407 return dma_fence_wait(dma, false);
Chris Wilsone68a1392016-09-09 14:11:41 +0100408 }
409
Chris Wilson9310cb72017-05-17 13:09:56 +0100410 cb->fence = fence;
Chris Wilsone68a1392016-09-09 14:11:41 +0100411 i915_sw_fence_await(fence);
412
413 cb->dma = NULL;
414 __setup_timer(&cb->timer,
415 timer_i915_sw_fence_wake, (unsigned long)cb,
416 TIMER_IRQSAFE);
417 if (timeout) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100418 cb->dma = dma_fence_get(dma);
Chris Wilsone68a1392016-09-09 14:11:41 +0100419 mod_timer(&cb->timer, round_jiffies_up(jiffies + timeout));
420 }
421
Chris Wilsonf54d1862016-10-25 13:00:45 +0100422 ret = dma_fence_add_callback(dma, &cb->base, dma_i915_sw_fence_wake);
Chris Wilsone68a1392016-09-09 14:11:41 +0100423 if (ret == 0) {
424 ret = 1;
425 } else {
426 dma_i915_sw_fence_wake(dma, &cb->base);
427 if (ret == -ENOENT) /* fence already signaled */
428 ret = 0;
429 }
430
431 return ret;
432}
433
434int i915_sw_fence_await_reservation(struct i915_sw_fence *fence,
435 struct reservation_object *resv,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100436 const struct dma_fence_ops *exclude,
Chris Wilsone68a1392016-09-09 14:11:41 +0100437 bool write,
438 unsigned long timeout,
439 gfp_t gfp)
440{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100441 struct dma_fence *excl;
Chris Wilsone68a1392016-09-09 14:11:41 +0100442 int ret = 0, pending;
443
Chris Wilsonfc158402016-11-25 13:17:18 +0000444 debug_fence_assert(fence);
445
Chris Wilsone68a1392016-09-09 14:11:41 +0100446 if (write) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100447 struct dma_fence **shared;
Chris Wilsone68a1392016-09-09 14:11:41 +0100448 unsigned int count, i;
449
450 ret = reservation_object_get_fences_rcu(resv,
451 &excl, &count, &shared);
452 if (ret)
453 return ret;
454
455 for (i = 0; i < count; i++) {
456 if (shared[i]->ops == exclude)
457 continue;
458
459 pending = i915_sw_fence_await_dma_fence(fence,
460 shared[i],
461 timeout,
462 gfp);
463 if (pending < 0) {
464 ret = pending;
465 break;
466 }
467
468 ret |= pending;
469 }
470
471 for (i = 0; i < count; i++)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100472 dma_fence_put(shared[i]);
Chris Wilsone68a1392016-09-09 14:11:41 +0100473 kfree(shared);
474 } else {
475 excl = reservation_object_get_excl_rcu(resv);
476 }
477
478 if (ret >= 0 && excl && excl->ops != exclude) {
479 pending = i915_sw_fence_await_dma_fence(fence,
480 excl,
481 timeout,
482 gfp);
483 if (pending < 0)
484 ret = pending;
485 else
486 ret |= pending;
487 }
488
Chris Wilsonf54d1862016-10-25 13:00:45 +0100489 dma_fence_put(excl);
Chris Wilsone68a1392016-09-09 14:11:41 +0100490
491 return ret;
492}
Chris Wilson47624cc2017-05-17 13:09:57 +0100493
494#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
495#include "selftests/i915_sw_fence.c"
496#endif