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
10 #include <linux/slab.h>
11 #include <linux/dma-fence.h>
12 #include <linux/reservation.h>
14 #include "i915_sw_fence.h"
16 #define I915_SW_FENCE_FLAG_ALLOC BIT(3) /* after WQ_FLAG_* for safety */
18 static DEFINE_SPINLOCK(i915_sw_fence_lock);
25 #ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS
27 static void *i915_sw_fence_debug_hint(void *addr)
29 return (void *)(((struct i915_sw_fence *)addr)->flags & I915_SW_FENCE_MASK);
32 static struct debug_obj_descr i915_sw_fence_debug_descr = {
33 .name = "i915_sw_fence",
34 .debug_hint = i915_sw_fence_debug_hint,
37 static inline void debug_fence_init(struct i915_sw_fence *fence)
39 debug_object_init(fence, &i915_sw_fence_debug_descr);
42 static inline void debug_fence_activate(struct i915_sw_fence *fence)
44 debug_object_activate(fence, &i915_sw_fence_debug_descr);
47 static inline void debug_fence_set_state(struct i915_sw_fence *fence,
50 debug_object_active_state(fence, &i915_sw_fence_debug_descr, old, new);
53 static inline void debug_fence_deactivate(struct i915_sw_fence *fence)
55 debug_object_deactivate(fence, &i915_sw_fence_debug_descr);
58 static inline void debug_fence_destroy(struct i915_sw_fence *fence)
60 debug_object_destroy(fence, &i915_sw_fence_debug_descr);
63 static inline void debug_fence_free(struct i915_sw_fence *fence)
65 debug_object_free(fence, &i915_sw_fence_debug_descr);
68 static inline void debug_fence_assert(struct i915_sw_fence *fence)
70 debug_object_assert_init(fence, &i915_sw_fence_debug_descr);
75 static inline void debug_fence_init(struct i915_sw_fence *fence)
79 static inline void debug_fence_activate(struct i915_sw_fence *fence)
83 static inline void debug_fence_set_state(struct i915_sw_fence *fence,
88 static inline void debug_fence_deactivate(struct i915_sw_fence *fence)
92 static inline void debug_fence_destroy(struct i915_sw_fence *fence)
96 static inline void debug_fence_free(struct i915_sw_fence *fence)
100 static inline void debug_fence_assert(struct i915_sw_fence *fence)
106 static int __i915_sw_fence_notify(struct i915_sw_fence *fence,
107 enum i915_sw_fence_notify state)
109 i915_sw_fence_notify_t fn;
111 fn = (i915_sw_fence_notify_t)(fence->flags & I915_SW_FENCE_MASK);
112 return fn(fence, state);
115 #ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS
116 void i915_sw_fence_fini(struct i915_sw_fence *fence)
118 debug_fence_free(fence);
122 static void i915_sw_fence_release(struct kref *kref)
124 struct i915_sw_fence *fence = container_of(kref, typeof(*fence), kref);
126 WARN_ON(atomic_read(&fence->pending) > 0);
127 debug_fence_destroy(fence);
129 if (fence->flags & I915_SW_FENCE_MASK) {
130 __i915_sw_fence_notify(fence, FENCE_FREE);
132 i915_sw_fence_fini(fence);
137 static void i915_sw_fence_put(struct i915_sw_fence *fence)
139 debug_fence_assert(fence);
140 kref_put(&fence->kref, i915_sw_fence_release);
143 static struct i915_sw_fence *i915_sw_fence_get(struct i915_sw_fence *fence)
145 debug_fence_assert(fence);
146 kref_get(&fence->kref);
150 static void __i915_sw_fence_wake_up_all(struct i915_sw_fence *fence,
151 struct list_head *continuation)
153 wait_queue_head_t *x = &fence->wait;
154 wait_queue_t *pos, *next;
157 debug_fence_deactivate(fence);
158 atomic_set_release(&fence->pending, -1); /* 0 -> -1 [done] */
161 * To prevent unbounded recursion as we traverse the graph of
162 * i915_sw_fences, we move the task_list from this, the next ready
163 * fence, to the tail of the original fence's task_list
164 * (and so added to the list to be woken).
167 spin_lock_irqsave_nested(&x->lock, flags, 1 + !!continuation);
169 list_for_each_entry_safe(pos, next, &x->task_list, task_list) {
170 if (pos->func == autoremove_wake_function)
171 pos->func(pos, TASK_NORMAL, 0, continuation);
173 list_move_tail(&pos->task_list, continuation);
179 list_for_each_entry_safe(pos, next,
180 &x->task_list, task_list)
181 pos->func(pos, TASK_NORMAL, 0, &extra);
183 if (list_empty(&extra))
186 list_splice_tail_init(&extra, &x->task_list);
189 spin_unlock_irqrestore(&x->lock, flags);
191 debug_fence_assert(fence);
194 static void __i915_sw_fence_complete(struct i915_sw_fence *fence,
195 struct list_head *continuation)
197 debug_fence_assert(fence);
199 if (!atomic_dec_and_test(&fence->pending))
202 debug_fence_set_state(fence, DEBUG_FENCE_IDLE, DEBUG_FENCE_NOTIFY);
204 if (fence->flags & I915_SW_FENCE_MASK &&
205 __i915_sw_fence_notify(fence, FENCE_COMPLETE) != NOTIFY_DONE)
208 debug_fence_set_state(fence, DEBUG_FENCE_NOTIFY, DEBUG_FENCE_IDLE);
210 __i915_sw_fence_wake_up_all(fence, continuation);
213 static void i915_sw_fence_complete(struct i915_sw_fence *fence)
215 debug_fence_assert(fence);
217 if (WARN_ON(i915_sw_fence_done(fence)))
220 __i915_sw_fence_complete(fence, NULL);
223 static void i915_sw_fence_await(struct i915_sw_fence *fence)
225 debug_fence_assert(fence);
226 WARN_ON(atomic_inc_return(&fence->pending) <= 1);
229 void __i915_sw_fence_init(struct i915_sw_fence *fence,
230 i915_sw_fence_notify_t fn,
232 struct lock_class_key *key)
234 BUG_ON((unsigned long)fn & ~I915_SW_FENCE_MASK);
236 debug_fence_init(fence);
238 __init_waitqueue_head(&fence->wait, name, key);
239 kref_init(&fence->kref);
240 atomic_set(&fence->pending, 1);
241 fence->flags = (unsigned long)fn;
244 static void __i915_sw_fence_commit(struct i915_sw_fence *fence)
246 i915_sw_fence_complete(fence);
247 i915_sw_fence_put(fence);
250 void i915_sw_fence_commit(struct i915_sw_fence *fence)
252 debug_fence_activate(fence);
253 __i915_sw_fence_commit(fence);
256 static int i915_sw_fence_wake(wait_queue_t *wq, unsigned mode, int flags, void *key)
258 list_del(&wq->task_list);
259 __i915_sw_fence_complete(wq->private, key);
260 i915_sw_fence_put(wq->private);
261 if (wq->flags & I915_SW_FENCE_FLAG_ALLOC)
266 static bool __i915_sw_fence_check_if_after(struct i915_sw_fence *fence,
267 const struct i915_sw_fence * const signaler)
271 if (__test_and_set_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags))
274 if (fence == signaler)
277 list_for_each_entry(wq, &fence->wait.task_list, task_list) {
278 if (wq->func != i915_sw_fence_wake)
281 if (__i915_sw_fence_check_if_after(wq->private, signaler))
288 static void __i915_sw_fence_clear_checked_bit(struct i915_sw_fence *fence)
292 if (!__test_and_clear_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags))
295 list_for_each_entry(wq, &fence->wait.task_list, task_list) {
296 if (wq->func != i915_sw_fence_wake)
299 __i915_sw_fence_clear_checked_bit(wq->private);
303 static bool i915_sw_fence_check_if_after(struct i915_sw_fence *fence,
304 const struct i915_sw_fence * const signaler)
309 if (!IS_ENABLED(CONFIG_I915_SW_FENCE_CHECK_DAG))
312 spin_lock_irqsave(&i915_sw_fence_lock, flags);
313 err = __i915_sw_fence_check_if_after(fence, signaler);
314 __i915_sw_fence_clear_checked_bit(fence);
315 spin_unlock_irqrestore(&i915_sw_fence_lock, flags);
320 static int __i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence,
321 struct i915_sw_fence *signaler,
322 wait_queue_t *wq, gfp_t gfp)
327 debug_fence_assert(fence);
329 if (i915_sw_fence_done(signaler))
332 debug_fence_assert(signaler);
334 /* The dependency graph must be acyclic. */
335 if (unlikely(i915_sw_fence_check_if_after(fence, signaler)))
340 wq = kmalloc(sizeof(*wq), gfp);
342 if (!gfpflags_allow_blocking(gfp))
345 i915_sw_fence_wait(signaler);
349 pending |= I915_SW_FENCE_FLAG_ALLOC;
352 INIT_LIST_HEAD(&wq->task_list);
354 wq->func = i915_sw_fence_wake;
355 wq->private = i915_sw_fence_get(fence);
357 i915_sw_fence_await(fence);
359 spin_lock_irqsave(&signaler->wait.lock, flags);
360 if (likely(!i915_sw_fence_done(signaler))) {
361 __add_wait_queue_tail(&signaler->wait, wq);
364 i915_sw_fence_wake(wq, 0, 0, NULL);
367 spin_unlock_irqrestore(&signaler->wait.lock, flags);
372 int i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence,
373 struct i915_sw_fence *signaler,
376 return __i915_sw_fence_await_sw_fence(fence, signaler, wq, 0);
379 int i915_sw_fence_await_sw_fence_gfp(struct i915_sw_fence *fence,
380 struct i915_sw_fence *signaler,
383 return __i915_sw_fence_await_sw_fence(fence, signaler, NULL, gfp);
386 struct i915_sw_dma_fence_cb {
387 struct dma_fence_cb base;
388 struct i915_sw_fence *fence;
389 struct dma_fence *dma;
390 struct timer_list timer;
393 static void timer_i915_sw_fence_wake(unsigned long data)
395 struct i915_sw_dma_fence_cb *cb = (struct i915_sw_dma_fence_cb *)data;
397 printk(KERN_WARNING "asynchronous wait on fence %s:%s:%x timed out\n",
398 cb->dma->ops->get_driver_name(cb->dma),
399 cb->dma->ops->get_timeline_name(cb->dma),
401 dma_fence_put(cb->dma);
404 __i915_sw_fence_commit(cb->fence);
405 cb->timer.function = NULL;
408 static void dma_i915_sw_fence_wake(struct dma_fence *dma,
409 struct dma_fence_cb *data)
411 struct i915_sw_dma_fence_cb *cb = container_of(data, typeof(*cb), base);
413 del_timer_sync(&cb->timer);
414 if (cb->timer.function)
415 __i915_sw_fence_commit(cb->fence);
416 dma_fence_put(cb->dma);
421 int i915_sw_fence_await_dma_fence(struct i915_sw_fence *fence,
422 struct dma_fence *dma,
423 unsigned long timeout,
426 struct i915_sw_dma_fence_cb *cb;
429 debug_fence_assert(fence);
431 if (dma_fence_is_signaled(dma))
434 cb = kmalloc(sizeof(*cb), gfp);
436 if (!gfpflags_allow_blocking(gfp))
439 return dma_fence_wait(dma, false);
442 cb->fence = i915_sw_fence_get(fence);
443 i915_sw_fence_await(fence);
446 __setup_timer(&cb->timer,
447 timer_i915_sw_fence_wake, (unsigned long)cb,
450 cb->dma = dma_fence_get(dma);
451 mod_timer(&cb->timer, round_jiffies_up(jiffies + timeout));
454 ret = dma_fence_add_callback(dma, &cb->base, dma_i915_sw_fence_wake);
458 dma_i915_sw_fence_wake(dma, &cb->base);
459 if (ret == -ENOENT) /* fence already signaled */
466 int i915_sw_fence_await_reservation(struct i915_sw_fence *fence,
467 struct reservation_object *resv,
468 const struct dma_fence_ops *exclude,
470 unsigned long timeout,
473 struct dma_fence *excl;
474 int ret = 0, pending;
476 debug_fence_assert(fence);
479 struct dma_fence **shared;
480 unsigned int count, i;
482 ret = reservation_object_get_fences_rcu(resv,
483 &excl, &count, &shared);
487 for (i = 0; i < count; i++) {
488 if (shared[i]->ops == exclude)
491 pending = i915_sw_fence_await_dma_fence(fence,
503 for (i = 0; i < count; i++)
504 dma_fence_put(shared[i]);
507 excl = reservation_object_get_excl_rcu(resv);
510 if (ret >= 0 && excl && excl->ops != exclude) {
511 pending = i915_sw_fence_await_dma_fence(fence,