blob: a0a690d6627ebd69c2fbf31734c9644e1404770e [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"
15
Chris Wilson7e941862016-10-28 13:58:25 +010016#define I915_SW_FENCE_FLAG_ALLOC BIT(3) /* after WQ_FLAG_* for safety */
17
Chris Wilsone68a1392016-09-09 14:11:41 +010018static DEFINE_SPINLOCK(i915_sw_fence_lock);
19
Chris Wilsonfc158402016-11-25 13:17:18 +000020enum {
21 DEBUG_FENCE_IDLE = 0,
22 DEBUG_FENCE_NOTIFY,
23};
24
25#ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS
26
27static void *i915_sw_fence_debug_hint(void *addr)
28{
29 return (void *)(((struct i915_sw_fence *)addr)->flags & I915_SW_FENCE_MASK);
30}
31
32static struct debug_obj_descr i915_sw_fence_debug_descr = {
33 .name = "i915_sw_fence",
34 .debug_hint = i915_sw_fence_debug_hint,
35};
36
37static inline void debug_fence_init(struct i915_sw_fence *fence)
38{
39 debug_object_init(fence, &i915_sw_fence_debug_descr);
40}
41
42static inline void debug_fence_activate(struct i915_sw_fence *fence)
43{
44 debug_object_activate(fence, &i915_sw_fence_debug_descr);
45}
46
47static inline void debug_fence_set_state(struct i915_sw_fence *fence,
48 int old, int new)
49{
50 debug_object_active_state(fence, &i915_sw_fence_debug_descr, old, new);
51}
52
53static inline void debug_fence_deactivate(struct i915_sw_fence *fence)
54{
55 debug_object_deactivate(fence, &i915_sw_fence_debug_descr);
56}
57
58static inline void debug_fence_destroy(struct i915_sw_fence *fence)
59{
60 debug_object_destroy(fence, &i915_sw_fence_debug_descr);
61}
62
63static inline void debug_fence_free(struct i915_sw_fence *fence)
64{
65 debug_object_free(fence, &i915_sw_fence_debug_descr);
Chris Wilson6f13f292017-01-13 21:43:35 +000066 smp_wmb(); /* flush the change in state before reallocation */
Chris Wilsonfc158402016-11-25 13:17:18 +000067}
68
69static inline void debug_fence_assert(struct i915_sw_fence *fence)
70{
71 debug_object_assert_init(fence, &i915_sw_fence_debug_descr);
72}
73
74#else
75
76static inline void debug_fence_init(struct i915_sw_fence *fence)
77{
78}
79
80static inline void debug_fence_activate(struct i915_sw_fence *fence)
81{
82}
83
84static inline void debug_fence_set_state(struct i915_sw_fence *fence,
85 int old, int new)
86{
87}
88
89static inline void debug_fence_deactivate(struct i915_sw_fence *fence)
90{
91}
92
93static inline void debug_fence_destroy(struct i915_sw_fence *fence)
94{
95}
96
97static inline void debug_fence_free(struct i915_sw_fence *fence)
98{
99}
100
101static inline void debug_fence_assert(struct i915_sw_fence *fence)
102{
103}
104
105#endif
106
Chris Wilsone68a1392016-09-09 14:11:41 +0100107static int __i915_sw_fence_notify(struct i915_sw_fence *fence,
108 enum i915_sw_fence_notify state)
109{
110 i915_sw_fence_notify_t fn;
111
112 fn = (i915_sw_fence_notify_t)(fence->flags & I915_SW_FENCE_MASK);
113 return fn(fence, state);
114}
115
Chris Wilsonfc158402016-11-25 13:17:18 +0000116#ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS
117void i915_sw_fence_fini(struct i915_sw_fence *fence)
118{
119 debug_fence_free(fence);
120}
121#endif
122
Chris Wilsone68a1392016-09-09 14:11:41 +0100123static void __i915_sw_fence_wake_up_all(struct i915_sw_fence *fence,
124 struct list_head *continuation)
125{
126 wait_queue_head_t *x = &fence->wait;
127 wait_queue_t *pos, *next;
128 unsigned long flags;
129
Chris Wilsonfc158402016-11-25 13:17:18 +0000130 debug_fence_deactivate(fence);
Chris Wilsone68a1392016-09-09 14:11:41 +0100131 atomic_set_release(&fence->pending, -1); /* 0 -> -1 [done] */
132
133 /*
134 * To prevent unbounded recursion as we traverse the graph of
135 * i915_sw_fences, we move the task_list from this, the next ready
136 * fence, to the tail of the original fence's task_list
137 * (and so added to the list to be woken).
138 */
139
140 spin_lock_irqsave_nested(&x->lock, flags, 1 + !!continuation);
141 if (continuation) {
142 list_for_each_entry_safe(pos, next, &x->task_list, task_list) {
143 if (pos->func == autoremove_wake_function)
144 pos->func(pos, TASK_NORMAL, 0, continuation);
145 else
146 list_move_tail(&pos->task_list, continuation);
147 }
148 } else {
149 LIST_HEAD(extra);
150
151 do {
152 list_for_each_entry_safe(pos, next,
153 &x->task_list, task_list)
154 pos->func(pos, TASK_NORMAL, 0, &extra);
155
156 if (list_empty(&extra))
157 break;
158
159 list_splice_tail_init(&extra, &x->task_list);
160 } 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
Chris Wilsone68a1392016-09-09 14:11:41 +0100224static int i915_sw_fence_wake(wait_queue_t *wq, unsigned mode, int flags, void *key)
225{
226 list_del(&wq->task_list);
227 __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{
237 wait_queue_t *wq;
238
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
245 list_for_each_entry(wq, &fence->wait.task_list, task_list) {
246 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{
258 wait_queue_t *wq;
259
260 if (!__test_and_clear_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags))
261 return;
262
263 list_for_each_entry(wq, &fence->wait.task_list, task_list) {
264 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
277 if (!IS_ENABLED(CONFIG_I915_SW_FENCE_CHECK_DAG))
278 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,
290 wait_queue_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
Chris Wilsone68a1392016-09-09 14:11:41 +0100320 INIT_LIST_HEAD(&wq->task_list);
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))) {
329 __add_wait_queue_tail(&signaler->wait, wq);
330 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,
342 wait_queue_t *wq)
343{
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}