blob: 474d23c0c0cebaf7524b16631b7b64c92df50729 [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;
128 wait_queue_t *pos, *next;
129 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
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 Wilsonfc158402016-11-25 13:17:18 +0000164
165 debug_fence_assert(fence);
Chris Wilsone68a1392016-09-09 14:11:41 +0100166}
167
168static void __i915_sw_fence_complete(struct i915_sw_fence *fence,
169 struct list_head *continuation)
170{
Chris Wilsonfc158402016-11-25 13:17:18 +0000171 debug_fence_assert(fence);
172
Chris Wilsone68a1392016-09-09 14:11:41 +0100173 if (!atomic_dec_and_test(&fence->pending))
174 return;
175
Chris Wilsonfc158402016-11-25 13:17:18 +0000176 debug_fence_set_state(fence, DEBUG_FENCE_IDLE, DEBUG_FENCE_NOTIFY);
177
Chris Wilson9310cb72017-05-17 13:09:56 +0100178 if (__i915_sw_fence_notify(fence, FENCE_COMPLETE) != NOTIFY_DONE)
Chris Wilsone68a1392016-09-09 14:11:41 +0100179 return;
180
Chris Wilsonfc158402016-11-25 13:17:18 +0000181 debug_fence_set_state(fence, DEBUG_FENCE_NOTIFY, DEBUG_FENCE_IDLE);
182
Chris Wilsone68a1392016-09-09 14:11:41 +0100183 __i915_sw_fence_wake_up_all(fence, continuation);
Chris Wilson9310cb72017-05-17 13:09:56 +0100184
185 debug_fence_destroy(fence);
186 __i915_sw_fence_notify(fence, FENCE_FREE);
Chris Wilsone68a1392016-09-09 14:11:41 +0100187}
188
189static void i915_sw_fence_complete(struct i915_sw_fence *fence)
190{
Chris Wilsonfc158402016-11-25 13:17:18 +0000191 debug_fence_assert(fence);
192
Chris Wilsone68a1392016-09-09 14:11:41 +0100193 if (WARN_ON(i915_sw_fence_done(fence)))
194 return;
195
196 __i915_sw_fence_complete(fence, NULL);
197}
198
199static void i915_sw_fence_await(struct i915_sw_fence *fence)
200{
Chris Wilsonfc158402016-11-25 13:17:18 +0000201 debug_fence_assert(fence);
Chris Wilsone68a1392016-09-09 14:11:41 +0100202 WARN_ON(atomic_inc_return(&fence->pending) <= 1);
203}
204
Chris Wilson556b7482016-11-14 20:40:56 +0000205void __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 Wilsone68a1392016-09-09 14:11:41 +0100209{
Chris Wilson9310cb72017-05-17 13:09:56 +0100210 BUG_ON(!fn || (unsigned long)fn & ~I915_SW_FENCE_MASK);
Chris Wilsone68a1392016-09-09 14:11:41 +0100211
Chris Wilsonfc158402016-11-25 13:17:18 +0000212 debug_fence_init(fence);
213
Chris Wilson556b7482016-11-14 20:40:56 +0000214 __init_waitqueue_head(&fence->wait, name, key);
Chris Wilsone68a1392016-09-09 14:11:41 +0100215 atomic_set(&fence->pending, 1);
216 fence->flags = (unsigned long)fn;
217}
218
Chris Wilsonfc158402016-11-25 13:17:18 +0000219void i915_sw_fence_commit(struct i915_sw_fence *fence)
220{
221 debug_fence_activate(fence);
Chris Wilson9310cb72017-05-17 13:09:56 +0100222 i915_sw_fence_complete(fence);
Chris Wilsonfc158402016-11-25 13:17:18 +0000223}
224
Chris Wilsone68a1392016-09-09 14:11:41 +0100225static 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 Wilson9310cb72017-05-17 13:09:56 +0100229
Chris Wilson7e941862016-10-28 13:58:25 +0100230 if (wq->flags & I915_SW_FENCE_FLAG_ALLOC)
231 kfree(wq);
Chris Wilsone68a1392016-09-09 14:11:41 +0100232 return 0;
233}
234
235static 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
257static 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
272static 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 Wilson47624cc2017-05-17 13:09:57 +0100278 if (!IS_ENABLED(CONFIG_DRM_I915_SW_FENCE_CHECK_DAG))
Chris Wilsone68a1392016-09-09 14:11:41 +0100279 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 Wilson7e941862016-10-28 13:58:25 +0100289static 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 Wilsone68a1392016-09-09 14:11:41 +0100292{
293 unsigned long flags;
294 int pending;
295
Chris Wilsonfc158402016-11-25 13:17:18 +0000296 debug_fence_assert(fence);
297
Chris Wilsone68a1392016-09-09 14:11:41 +0100298 if (i915_sw_fence_done(signaler))
299 return 0;
300
Chris Wilsonfc158402016-11-25 13:17:18 +0000301 debug_fence_assert(signaler);
302
Chris Wilsone68a1392016-09-09 14:11:41 +0100303 /* The dependency graph must be acyclic. */
304 if (unlikely(i915_sw_fence_check_if_after(fence, signaler)))
305 return -EINVAL;
306
Chris Wilson7e941862016-10-28 13:58:25 +0100307 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 Wilsone68a1392016-09-09 14:11:41 +0100321 INIT_LIST_HEAD(&wq->task_list);
Chris Wilson7e941862016-10-28 13:58:25 +0100322 wq->flags = pending;
Chris Wilsone68a1392016-09-09 14:11:41 +0100323 wq->func = i915_sw_fence_wake;
Chris Wilson9310cb72017-05-17 13:09:56 +0100324 wq->private = fence;
Chris Wilsone68a1392016-09-09 14:11:41 +0100325
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 Wilson7e941862016-10-28 13:58:25 +0100341int 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
348int 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 Wilsonf54d1862016-10-25 13:00:45 +0100355struct i915_sw_dma_fence_cb {
356 struct dma_fence_cb base;
Chris Wilsone68a1392016-09-09 14:11:41 +0100357 struct i915_sw_fence *fence;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100358 struct dma_fence *dma;
Chris Wilsone68a1392016-09-09 14:11:41 +0100359 struct timer_list timer;
360};
361
362static void timer_i915_sw_fence_wake(unsigned long data)
363{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100364 struct i915_sw_dma_fence_cb *cb = (struct i915_sw_dma_fence_cb *)data;
Chris Wilsone68a1392016-09-09 14:11:41 +0100365
Joe Perches8dfe1622017-02-28 04:55:54 -0800366 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 Wilsonf54d1862016-10-25 13:00:45 +0100370 dma_fence_put(cb->dma);
Chris Wilsone68a1392016-09-09 14:11:41 +0100371 cb->dma = NULL;
372
Chris Wilson9310cb72017-05-17 13:09:56 +0100373 i915_sw_fence_complete(cb->fence);
Chris Wilsone68a1392016-09-09 14:11:41 +0100374 cb->timer.function = NULL;
375}
376
Chris Wilsonf54d1862016-10-25 13:00:45 +0100377static void dma_i915_sw_fence_wake(struct dma_fence *dma,
378 struct dma_fence_cb *data)
Chris Wilsone68a1392016-09-09 14:11:41 +0100379{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100380 struct i915_sw_dma_fence_cb *cb = container_of(data, typeof(*cb), base);
Chris Wilsone68a1392016-09-09 14:11:41 +0100381
382 del_timer_sync(&cb->timer);
383 if (cb->timer.function)
Chris Wilson9310cb72017-05-17 13:09:56 +0100384 i915_sw_fence_complete(cb->fence);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100385 dma_fence_put(cb->dma);
Chris Wilsone68a1392016-09-09 14:11:41 +0100386
387 kfree(cb);
388}
389
390int i915_sw_fence_await_dma_fence(struct i915_sw_fence *fence,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100391 struct dma_fence *dma,
Chris Wilsone68a1392016-09-09 14:11:41 +0100392 unsigned long timeout,
393 gfp_t gfp)
394{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100395 struct i915_sw_dma_fence_cb *cb;
Chris Wilsone68a1392016-09-09 14:11:41 +0100396 int ret;
397
Chris Wilsonfc158402016-11-25 13:17:18 +0000398 debug_fence_assert(fence);
399
Chris Wilsonf54d1862016-10-25 13:00:45 +0100400 if (dma_fence_is_signaled(dma))
Chris Wilsone68a1392016-09-09 14:11:41 +0100401 return 0;
402
403 cb = kmalloc(sizeof(*cb), gfp);
404 if (!cb) {
405 if (!gfpflags_allow_blocking(gfp))
406 return -ENOMEM;
407
Chris Wilsonf54d1862016-10-25 13:00:45 +0100408 return dma_fence_wait(dma, false);
Chris Wilsone68a1392016-09-09 14:11:41 +0100409 }
410
Chris Wilson9310cb72017-05-17 13:09:56 +0100411 cb->fence = fence;
Chris Wilsone68a1392016-09-09 14:11:41 +0100412 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 Wilsonf54d1862016-10-25 13:00:45 +0100419 cb->dma = dma_fence_get(dma);
Chris Wilsone68a1392016-09-09 14:11:41 +0100420 mod_timer(&cb->timer, round_jiffies_up(jiffies + timeout));
421 }
422
Chris Wilsonf54d1862016-10-25 13:00:45 +0100423 ret = dma_fence_add_callback(dma, &cb->base, dma_i915_sw_fence_wake);
Chris Wilsone68a1392016-09-09 14:11:41 +0100424 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
435int i915_sw_fence_await_reservation(struct i915_sw_fence *fence,
436 struct reservation_object *resv,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100437 const struct dma_fence_ops *exclude,
Chris Wilsone68a1392016-09-09 14:11:41 +0100438 bool write,
439 unsigned long timeout,
440 gfp_t gfp)
441{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100442 struct dma_fence *excl;
Chris Wilsone68a1392016-09-09 14:11:41 +0100443 int ret = 0, pending;
444
Chris Wilsonfc158402016-11-25 13:17:18 +0000445 debug_fence_assert(fence);
446
Chris Wilsone68a1392016-09-09 14:11:41 +0100447 if (write) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100448 struct dma_fence **shared;
Chris Wilsone68a1392016-09-09 14:11:41 +0100449 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 Wilsonf54d1862016-10-25 13:00:45 +0100473 dma_fence_put(shared[i]);
Chris Wilsone68a1392016-09-09 14:11:41 +0100474 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 Wilsonf54d1862016-10-25 13:00:45 +0100490 dma_fence_put(excl);
Chris Wilsone68a1392016-09-09 14:11:41 +0100491
492 return ret;
493}
Chris Wilson47624cc2017-05-17 13:09:57 +0100494
495#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
496#include "selftests/i915_sw_fence.c"
497#endif