Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / gpu / drm / i915 / i915_sw_fence.c
blob3669f5eeb91efa709ea2d6f9f83525f98230f7c5
1 /*
2 * (C) Copyright 2016 Intel Corporation
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 */
10 #include <linux/slab.h>
11 #include <linux/dma-fence.h>
12 #include <linux/irq_work.h>
13 #include <linux/reservation.h>
15 #include "i915_sw_fence.h"
16 #include "i915_selftest.h"
18 #define I915_SW_FENCE_FLAG_ALLOC BIT(3) /* after WQ_FLAG_* for safety */
20 static DEFINE_SPINLOCK(i915_sw_fence_lock);
22 enum {
23 DEBUG_FENCE_IDLE = 0,
24 DEBUG_FENCE_NOTIFY,
27 #ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS
29 static void *i915_sw_fence_debug_hint(void *addr)
31 return (void *)(((struct i915_sw_fence *)addr)->flags & I915_SW_FENCE_MASK);
34 static struct debug_obj_descr i915_sw_fence_debug_descr = {
35 .name = "i915_sw_fence",
36 .debug_hint = i915_sw_fence_debug_hint,
39 static inline void debug_fence_init(struct i915_sw_fence *fence)
41 debug_object_init(fence, &i915_sw_fence_debug_descr);
44 static inline void debug_fence_init_onstack(struct i915_sw_fence *fence)
46 debug_object_init_on_stack(fence, &i915_sw_fence_debug_descr);
49 static inline void debug_fence_activate(struct i915_sw_fence *fence)
51 debug_object_activate(fence, &i915_sw_fence_debug_descr);
54 static inline void debug_fence_set_state(struct i915_sw_fence *fence,
55 int old, int new)
57 debug_object_active_state(fence, &i915_sw_fence_debug_descr, old, new);
60 static inline void debug_fence_deactivate(struct i915_sw_fence *fence)
62 debug_object_deactivate(fence, &i915_sw_fence_debug_descr);
65 static inline void debug_fence_destroy(struct i915_sw_fence *fence)
67 debug_object_destroy(fence, &i915_sw_fence_debug_descr);
70 static inline void debug_fence_free(struct i915_sw_fence *fence)
72 debug_object_free(fence, &i915_sw_fence_debug_descr);
73 smp_wmb(); /* flush the change in state before reallocation */
76 static inline void debug_fence_assert(struct i915_sw_fence *fence)
78 debug_object_assert_init(fence, &i915_sw_fence_debug_descr);
81 #else
83 static inline void debug_fence_init(struct i915_sw_fence *fence)
87 static inline void debug_fence_init_onstack(struct i915_sw_fence *fence)
91 static inline void debug_fence_activate(struct i915_sw_fence *fence)
95 static inline void debug_fence_set_state(struct i915_sw_fence *fence,
96 int old, int new)
100 static inline void debug_fence_deactivate(struct i915_sw_fence *fence)
104 static inline void debug_fence_destroy(struct i915_sw_fence *fence)
108 static inline void debug_fence_free(struct i915_sw_fence *fence)
112 static inline void debug_fence_assert(struct i915_sw_fence *fence)
116 #endif
118 static int __i915_sw_fence_notify(struct i915_sw_fence *fence,
119 enum i915_sw_fence_notify state)
121 i915_sw_fence_notify_t fn;
123 fn = (i915_sw_fence_notify_t)(fence->flags & I915_SW_FENCE_MASK);
124 return fn(fence, state);
127 #ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS
128 void i915_sw_fence_fini(struct i915_sw_fence *fence)
130 debug_fence_free(fence);
132 #endif
134 static void __i915_sw_fence_wake_up_all(struct i915_sw_fence *fence,
135 struct list_head *continuation)
137 wait_queue_head_t *x = &fence->wait;
138 wait_queue_entry_t *pos, *next;
139 unsigned long flags;
141 debug_fence_deactivate(fence);
142 atomic_set_release(&fence->pending, -1); /* 0 -> -1 [done] */
145 * To prevent unbounded recursion as we traverse the graph of
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
148 * (and so added to the list to be woken).
151 spin_lock_irqsave_nested(&x->lock, flags, 1 + !!continuation);
152 if (continuation) {
153 list_for_each_entry_safe(pos, next, &x->head, entry) {
154 if (pos->func == autoremove_wake_function)
155 pos->func(pos, TASK_NORMAL, 0, continuation);
156 else
157 list_move_tail(&pos->entry, continuation);
159 } else {
160 LIST_HEAD(extra);
162 do {
163 list_for_each_entry_safe(pos, next, &x->head, entry)
164 pos->func(pos, TASK_NORMAL, 0, &extra);
166 if (list_empty(&extra))
167 break;
169 list_splice_tail_init(&extra, &x->head);
170 } while (1);
172 spin_unlock_irqrestore(&x->lock, flags);
174 debug_fence_assert(fence);
177 static void __i915_sw_fence_complete(struct i915_sw_fence *fence,
178 struct list_head *continuation)
180 debug_fence_assert(fence);
182 if (!atomic_dec_and_test(&fence->pending))
183 return;
185 debug_fence_set_state(fence, DEBUG_FENCE_IDLE, DEBUG_FENCE_NOTIFY);
187 if (__i915_sw_fence_notify(fence, FENCE_COMPLETE) != NOTIFY_DONE)
188 return;
190 debug_fence_set_state(fence, DEBUG_FENCE_NOTIFY, DEBUG_FENCE_IDLE);
192 __i915_sw_fence_wake_up_all(fence, continuation);
194 debug_fence_destroy(fence);
195 __i915_sw_fence_notify(fence, FENCE_FREE);
198 static void i915_sw_fence_complete(struct i915_sw_fence *fence)
200 debug_fence_assert(fence);
202 if (WARN_ON(i915_sw_fence_done(fence)))
203 return;
205 __i915_sw_fence_complete(fence, NULL);
208 static void i915_sw_fence_await(struct i915_sw_fence *fence)
210 debug_fence_assert(fence);
211 WARN_ON(atomic_inc_return(&fence->pending) <= 1);
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)
219 BUG_ON(!fn || (unsigned long)fn & ~I915_SW_FENCE_MASK);
221 debug_fence_init(fence);
223 __init_waitqueue_head(&fence->wait, name, key);
224 atomic_set(&fence->pending, 1);
225 fence->flags = (unsigned long)fn;
228 void i915_sw_fence_commit(struct i915_sw_fence *fence)
230 debug_fence_activate(fence);
231 i915_sw_fence_complete(fence);
234 static int i915_sw_fence_wake(wait_queue_entry_t *wq, unsigned mode, int flags, void *key)
236 list_del(&wq->entry);
237 __i915_sw_fence_complete(wq->private, key);
239 if (wq->flags & I915_SW_FENCE_FLAG_ALLOC)
240 kfree(wq);
241 return 0;
244 static bool __i915_sw_fence_check_if_after(struct i915_sw_fence *fence,
245 const struct i915_sw_fence * const signaler)
247 wait_queue_entry_t *wq;
249 if (__test_and_set_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags))
250 return false;
252 if (fence == signaler)
253 return true;
255 list_for_each_entry(wq, &fence->wait.head, entry) {
256 if (wq->func != i915_sw_fence_wake)
257 continue;
259 if (__i915_sw_fence_check_if_after(wq->private, signaler))
260 return true;
263 return false;
266 static void __i915_sw_fence_clear_checked_bit(struct i915_sw_fence *fence)
268 wait_queue_entry_t *wq;
270 if (!__test_and_clear_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags))
271 return;
273 list_for_each_entry(wq, &fence->wait.head, entry) {
274 if (wq->func != i915_sw_fence_wake)
275 continue;
277 __i915_sw_fence_clear_checked_bit(wq->private);
281 static bool i915_sw_fence_check_if_after(struct i915_sw_fence *fence,
282 const struct i915_sw_fence * const signaler)
284 unsigned long flags;
285 bool err;
287 if (!IS_ENABLED(CONFIG_DRM_I915_SW_FENCE_CHECK_DAG))
288 return false;
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);
295 return err;
298 static int __i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence,
299 struct i915_sw_fence *signaler,
300 wait_queue_entry_t *wq, gfp_t gfp)
302 unsigned long flags;
303 int pending;
305 debug_fence_assert(fence);
306 might_sleep_if(gfpflags_allow_blocking(gfp));
308 if (i915_sw_fence_done(signaler))
309 return 0;
311 debug_fence_assert(signaler);
313 /* The dependency graph must be acyclic. */
314 if (unlikely(i915_sw_fence_check_if_after(fence, signaler)))
315 return -EINVAL;
317 pending = 0;
318 if (!wq) {
319 wq = kmalloc(sizeof(*wq), gfp);
320 if (!wq) {
321 if (!gfpflags_allow_blocking(gfp))
322 return -ENOMEM;
324 i915_sw_fence_wait(signaler);
325 return 0;
328 pending |= I915_SW_FENCE_FLAG_ALLOC;
331 INIT_LIST_HEAD(&wq->entry);
332 wq->flags = pending;
333 wq->func = i915_sw_fence_wake;
334 wq->private = fence;
336 i915_sw_fence_await(fence);
338 spin_lock_irqsave(&signaler->wait.lock, flags);
339 if (likely(!i915_sw_fence_done(signaler))) {
340 __add_wait_queue_entry_tail(&signaler->wait, wq);
341 pending = 1;
342 } else {
343 i915_sw_fence_wake(wq, 0, 0, NULL);
344 pending = 0;
346 spin_unlock_irqrestore(&signaler->wait.lock, flags);
348 return pending;
351 int i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence,
352 struct i915_sw_fence *signaler,
353 wait_queue_entry_t *wq)
355 return __i915_sw_fence_await_sw_fence(fence, signaler, wq, 0);
358 int i915_sw_fence_await_sw_fence_gfp(struct i915_sw_fence *fence,
359 struct i915_sw_fence *signaler,
360 gfp_t gfp)
362 return __i915_sw_fence_await_sw_fence(fence, signaler, NULL, gfp);
365 struct i915_sw_dma_fence_cb {
366 struct dma_fence_cb base;
367 struct i915_sw_fence *fence;
368 struct dma_fence *dma;
369 struct timer_list timer;
370 struct irq_work work;
371 struct rcu_head rcu;
374 static void timer_i915_sw_fence_wake(struct timer_list *t)
376 struct i915_sw_dma_fence_cb *cb = from_timer(cb, t, timer);
377 struct i915_sw_fence *fence;
379 fence = xchg(&cb->fence, NULL);
380 if (!fence)
381 return;
383 pr_warn("asynchronous wait on fence %s:%s:%x timed out\n",
384 cb->dma->ops->get_driver_name(cb->dma),
385 cb->dma->ops->get_timeline_name(cb->dma),
386 cb->dma->seqno);
388 i915_sw_fence_complete(fence);
391 static void dma_i915_sw_fence_wake(struct dma_fence *dma,
392 struct dma_fence_cb *data)
394 struct i915_sw_dma_fence_cb *cb = container_of(data, typeof(*cb), base);
395 struct i915_sw_fence *fence;
397 fence = xchg(&cb->fence, NULL);
398 if (fence)
399 i915_sw_fence_complete(fence);
401 irq_work_queue(&cb->work);
404 static void irq_i915_sw_fence_work(struct irq_work *wrk)
406 struct i915_sw_dma_fence_cb *cb = container_of(wrk, typeof(*cb), work);
408 del_timer_sync(&cb->timer);
409 dma_fence_put(cb->dma);
411 kfree_rcu(cb, rcu);
414 int i915_sw_fence_await_dma_fence(struct i915_sw_fence *fence,
415 struct dma_fence *dma,
416 unsigned long timeout,
417 gfp_t gfp)
419 struct i915_sw_dma_fence_cb *cb;
420 int ret;
422 debug_fence_assert(fence);
423 might_sleep_if(gfpflags_allow_blocking(gfp));
425 if (dma_fence_is_signaled(dma))
426 return 0;
428 cb = kmalloc(sizeof(*cb), gfp);
429 if (!cb) {
430 if (!gfpflags_allow_blocking(gfp))
431 return -ENOMEM;
433 return dma_fence_wait(dma, false);
436 cb->fence = fence;
437 i915_sw_fence_await(fence);
439 cb->dma = NULL;
440 timer_setup(&cb->timer, timer_i915_sw_fence_wake, TIMER_IRQSAFE);
441 init_irq_work(&cb->work, irq_i915_sw_fence_work);
442 if (timeout) {
443 cb->dma = dma_fence_get(dma);
444 mod_timer(&cb->timer, round_jiffies_up(jiffies + timeout));
447 ret = dma_fence_add_callback(dma, &cb->base, dma_i915_sw_fence_wake);
448 if (ret == 0) {
449 ret = 1;
450 } else {
451 dma_i915_sw_fence_wake(dma, &cb->base);
452 if (ret == -ENOENT) /* fence already signaled */
453 ret = 0;
456 return ret;
459 int i915_sw_fence_await_reservation(struct i915_sw_fence *fence,
460 struct reservation_object *resv,
461 const struct dma_fence_ops *exclude,
462 bool write,
463 unsigned long timeout,
464 gfp_t gfp)
466 struct dma_fence *excl;
467 int ret = 0, pending;
469 debug_fence_assert(fence);
470 might_sleep_if(gfpflags_allow_blocking(gfp));
472 if (write) {
473 struct dma_fence **shared;
474 unsigned int count, i;
476 ret = reservation_object_get_fences_rcu(resv,
477 &excl, &count, &shared);
478 if (ret)
479 return ret;
481 for (i = 0; i < count; i++) {
482 if (shared[i]->ops == exclude)
483 continue;
485 pending = i915_sw_fence_await_dma_fence(fence,
486 shared[i],
487 timeout,
488 gfp);
489 if (pending < 0) {
490 ret = pending;
491 break;
494 ret |= pending;
497 for (i = 0; i < count; i++)
498 dma_fence_put(shared[i]);
499 kfree(shared);
500 } else {
501 excl = reservation_object_get_excl_rcu(resv);
504 if (ret >= 0 && excl && excl->ops != exclude) {
505 pending = i915_sw_fence_await_dma_fence(fence,
506 excl,
507 timeout,
508 gfp);
509 if (pending < 0)
510 ret = pending;
511 else
512 ret |= pending;
515 dma_fence_put(excl);
517 return ret;
520 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
521 #include "selftests/lib_sw_fence.c"
522 #include "selftests/i915_sw_fence.c"
523 #endif