164d6c500SChris Wilson /*
264d6c500SChris Wilson * SPDX-License-Identifier: MIT
364d6c500SChris Wilson *
464d6c500SChris Wilson * Copyright © 2019 Intel Corporation
564d6c500SChris Wilson */
664d6c500SChris Wilson
75361db1aSChris Wilson #include <linux/debugobjects.h>
85361db1aSChris Wilson
9e6ba7648SChris Wilson #include "gt/intel_context.h"
10d13a3177SChris Wilson #include "gt/intel_engine_heartbeat.h"
117009db14SChris Wilson #include "gt/intel_engine_pm.h"
122871ea85SChris Wilson #include "gt/intel_ring.h"
137009db14SChris Wilson
1464d6c500SChris Wilson #include "i915_drv.h"
1564d6c500SChris Wilson #include "i915_active.h"
1664d6c500SChris Wilson
175f5c139dSChris Wilson /*
185f5c139dSChris Wilson * Active refs memory management
195f5c139dSChris Wilson *
205f5c139dSChris Wilson * To be more economical with memory, we reap all the i915_active trees as
215f5c139dSChris Wilson * they idle (when we know the active requests are inactive) and allocate the
225f5c139dSChris Wilson * nodes from a local slab cache to hopefully reduce the fragmentation.
235f5c139dSChris Wilson */
24512ba03eSDaniel Vetter static struct kmem_cache *slab_cache;
255f5c139dSChris Wilson
2664d6c500SChris Wilson struct active_node {
275d934137SChris Wilson struct rb_node node;
28b1e3177bSChris Wilson struct i915_active_fence base;
2964d6c500SChris Wilson struct i915_active *ref;
3064d6c500SChris Wilson u64 timeline;
3164d6c500SChris Wilson };
3264d6c500SChris Wilson
335d934137SChris Wilson #define fetch_node(x) rb_entry(READ_ONCE(x), typeof(struct active_node), node)
345d934137SChris Wilson
35d8af05ffSChris Wilson static inline struct active_node *
node_from_active(struct i915_active_fence * active)36b1e3177bSChris Wilson node_from_active(struct i915_active_fence *active)
37d8af05ffSChris Wilson {
38d8af05ffSChris Wilson return container_of(active, struct active_node, base);
39d8af05ffSChris Wilson }
40d8af05ffSChris Wilson
41d8af05ffSChris Wilson #define take_preallocated_barriers(x) llist_del_all(&(x)->preallocated_barriers)
42d8af05ffSChris Wilson
is_barrier(const struct i915_active_fence * active)43b1e3177bSChris Wilson static inline bool is_barrier(const struct i915_active_fence *active)
44d8af05ffSChris Wilson {
45b1e3177bSChris Wilson return IS_ERR(rcu_access_pointer(active->fence));
46d8af05ffSChris Wilson }
47d8af05ffSChris Wilson
barrier_to_ll(struct active_node * node)48d8af05ffSChris Wilson static inline struct llist_node *barrier_to_ll(struct active_node *node)
49d8af05ffSChris Wilson {
50d8af05ffSChris Wilson GEM_BUG_ON(!is_barrier(&node->base));
51b1e3177bSChris Wilson return (struct llist_node *)&node->base.cb.node;
52d8af05ffSChris Wilson }
53d8af05ffSChris Wilson
54d8af05ffSChris Wilson static inline struct intel_engine_cs *
__barrier_to_engine(struct active_node * node)55f130b712SChris Wilson __barrier_to_engine(struct active_node *node)
56f130b712SChris Wilson {
57b1e3177bSChris Wilson return (struct intel_engine_cs *)READ_ONCE(node->base.cb.node.prev);
58f130b712SChris Wilson }
59f130b712SChris Wilson
60f130b712SChris Wilson static inline struct intel_engine_cs *
barrier_to_engine(struct active_node * node)61d8af05ffSChris Wilson barrier_to_engine(struct active_node *node)
62d8af05ffSChris Wilson {
63d8af05ffSChris Wilson GEM_BUG_ON(!is_barrier(&node->base));
64f130b712SChris Wilson return __barrier_to_engine(node);
65d8af05ffSChris Wilson }
66d8af05ffSChris Wilson
barrier_from_ll(struct llist_node * x)67d8af05ffSChris Wilson static inline struct active_node *barrier_from_ll(struct llist_node *x)
68d8af05ffSChris Wilson {
69d8af05ffSChris Wilson return container_of((struct list_head *)x,
70b1e3177bSChris Wilson struct active_node, base.cb.node);
71d8af05ffSChris Wilson }
72d8af05ffSChris Wilson
735361db1aSChris Wilson #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM) && IS_ENABLED(CONFIG_DEBUG_OBJECTS)
745361db1aSChris Wilson
active_debug_hint(void * addr)755361db1aSChris Wilson static void *active_debug_hint(void *addr)
765361db1aSChris Wilson {
775361db1aSChris Wilson struct i915_active *ref = addr;
785361db1aSChris Wilson
7912c255b5SChris Wilson return (void *)ref->active ?: (void *)ref->retire ?: (void *)ref;
805361db1aSChris Wilson }
815361db1aSChris Wilson
82f9e62f31SStephen Boyd static const struct debug_obj_descr active_debug_desc = {
835361db1aSChris Wilson .name = "i915_active",
845361db1aSChris Wilson .debug_hint = active_debug_hint,
855361db1aSChris Wilson };
865361db1aSChris Wilson
debug_active_init(struct i915_active * ref)875361db1aSChris Wilson static void debug_active_init(struct i915_active *ref)
885361db1aSChris Wilson {
895361db1aSChris Wilson debug_object_init(ref, &active_debug_desc);
905361db1aSChris Wilson }
915361db1aSChris Wilson
debug_active_activate(struct i915_active * ref)925361db1aSChris Wilson static void debug_active_activate(struct i915_active *ref)
935361db1aSChris Wilson {
94bbca083dSChris Wilson lockdep_assert_held(&ref->tree_lock);
955361db1aSChris Wilson debug_object_activate(ref, &active_debug_desc);
965361db1aSChris Wilson }
975361db1aSChris Wilson
debug_active_deactivate(struct i915_active * ref)985361db1aSChris Wilson static void debug_active_deactivate(struct i915_active *ref)
995361db1aSChris Wilson {
100c9ad602fSChris Wilson lockdep_assert_held(&ref->tree_lock);
101f52c6d0dSChris Wilson if (!atomic_read(&ref->count)) /* after the last dec */
1025361db1aSChris Wilson debug_object_deactivate(ref, &active_debug_desc);
1035361db1aSChris Wilson }
1045361db1aSChris Wilson
debug_active_fini(struct i915_active * ref)1055361db1aSChris Wilson static void debug_active_fini(struct i915_active *ref)
1065361db1aSChris Wilson {
1075361db1aSChris Wilson debug_object_free(ref, &active_debug_desc);
1085361db1aSChris Wilson }
1095361db1aSChris Wilson
debug_active_assert(struct i915_active * ref)1105361db1aSChris Wilson static void debug_active_assert(struct i915_active *ref)
1115361db1aSChris Wilson {
1125361db1aSChris Wilson debug_object_assert_init(ref, &active_debug_desc);
1135361db1aSChris Wilson }
1145361db1aSChris Wilson
1155361db1aSChris Wilson #else
1165361db1aSChris Wilson
debug_active_init(struct i915_active * ref)1175361db1aSChris Wilson static inline void debug_active_init(struct i915_active *ref) { }
debug_active_activate(struct i915_active * ref)1185361db1aSChris Wilson static inline void debug_active_activate(struct i915_active *ref) { }
debug_active_deactivate(struct i915_active * ref)1195361db1aSChris Wilson static inline void debug_active_deactivate(struct i915_active *ref) { }
debug_active_fini(struct i915_active * ref)1205361db1aSChris Wilson static inline void debug_active_fini(struct i915_active *ref) { }
debug_active_assert(struct i915_active * ref)1215361db1aSChris Wilson static inline void debug_active_assert(struct i915_active *ref) { }
1225361db1aSChris Wilson
1235361db1aSChris Wilson #endif
1245361db1aSChris Wilson
12564d6c500SChris Wilson static void
__active_retire(struct i915_active * ref)12612c255b5SChris Wilson __active_retire(struct i915_active *ref)
127a42375afSChris Wilson {
12899a7f4daSChris Wilson struct rb_root root = RB_ROOT;
129a42375afSChris Wilson struct active_node *it, *n;
130c9ad602fSChris Wilson unsigned long flags;
131a42375afSChris Wilson
132274cbf20SChris Wilson GEM_BUG_ON(i915_active_is_idle(ref));
13312c255b5SChris Wilson
13412c255b5SChris Wilson /* return the unused nodes to our slabcache -- flushing the allocator */
135c9ad602fSChris Wilson if (!atomic_dec_and_lock_irqsave(&ref->count, &ref->tree_lock, flags))
13612c255b5SChris Wilson return;
13712c255b5SChris Wilson
138b1e3177bSChris Wilson GEM_BUG_ON(rcu_access_pointer(ref->excl.fence));
139c9ad602fSChris Wilson debug_active_deactivate(ref);
140c9ad602fSChris Wilson
14199a7f4daSChris Wilson /* Even if we have not used the cache, we may still have a barrier */
14299a7f4daSChris Wilson if (!ref->cache)
14399a7f4daSChris Wilson ref->cache = fetch_node(ref->tree.rb_node);
14499a7f4daSChris Wilson
14599a7f4daSChris Wilson /* Keep the MRU cached node for reuse */
14699a7f4daSChris Wilson if (ref->cache) {
14799a7f4daSChris Wilson /* Discard all other nodes in the tree */
14899a7f4daSChris Wilson rb_erase(&ref->cache->node, &ref->tree);
149c9ad602fSChris Wilson root = ref->tree;
15099a7f4daSChris Wilson
15199a7f4daSChris Wilson /* Rebuild the tree with only the cached node */
15299a7f4daSChris Wilson rb_link_node(&ref->cache->node, NULL, &ref->tree.rb_node);
15399a7f4daSChris Wilson rb_insert_color(&ref->cache->node, &ref->tree);
15499a7f4daSChris Wilson GEM_BUG_ON(ref->tree.rb_node != &ref->cache->node);
155e28860aeSChris Wilson
156e28860aeSChris Wilson /* Make the cached node available for reuse with any timeline */
157e28860aeSChris Wilson ref->cache->timeline = 0; /* needs cmpxchg(u64) */
15899a7f4daSChris Wilson }
159c9ad602fSChris Wilson
160c9ad602fSChris Wilson spin_unlock_irqrestore(&ref->tree_lock, flags);
161e1d7b66bSChris Wilson
162e1d7b66bSChris Wilson /* After the final retire, the entire struct may be freed */
163e1d7b66bSChris Wilson if (ref->retire)
164e1d7b66bSChris Wilson ref->retire(ref);
165b1e3177bSChris Wilson
166b1e3177bSChris Wilson /* ... except if you wait on it, you must manage your own references! */
167b1e3177bSChris Wilson wake_up_var(ref);
168c9ad602fSChris Wilson
16999a7f4daSChris Wilson /* Finally free the discarded timeline tree */
170c9ad602fSChris Wilson rbtree_postorder_for_each_entry_safe(it, n, &root, node) {
171c9ad602fSChris Wilson GEM_BUG_ON(i915_active_fence_isset(&it->base));
172512ba03eSDaniel Vetter kmem_cache_free(slab_cache, it);
173c9ad602fSChris Wilson }
174a42375afSChris Wilson }
175a42375afSChris Wilson
176a42375afSChris Wilson static void
active_work(struct work_struct * wrk)177274cbf20SChris Wilson active_work(struct work_struct *wrk)
178274cbf20SChris Wilson {
179274cbf20SChris Wilson struct i915_active *ref = container_of(wrk, typeof(*ref), work);
180274cbf20SChris Wilson
181274cbf20SChris Wilson GEM_BUG_ON(!atomic_read(&ref->count));
182274cbf20SChris Wilson if (atomic_add_unless(&ref->count, -1, 1))
183274cbf20SChris Wilson return;
184274cbf20SChris Wilson
185274cbf20SChris Wilson __active_retire(ref);
186274cbf20SChris Wilson }
187274cbf20SChris Wilson
188274cbf20SChris Wilson static void
active_retire(struct i915_active * ref)18912c255b5SChris Wilson active_retire(struct i915_active *ref)
19064d6c500SChris Wilson {
19112c255b5SChris Wilson GEM_BUG_ON(!atomic_read(&ref->count));
19212c255b5SChris Wilson if (atomic_add_unless(&ref->count, -1, 1))
193a42375afSChris Wilson return;
194a42375afSChris Wilson
195c9ad602fSChris Wilson if (ref->flags & I915_ACTIVE_RETIRE_SLEEPS) {
196274cbf20SChris Wilson queue_work(system_unbound_wq, &ref->work);
197274cbf20SChris Wilson return;
198274cbf20SChris Wilson }
199274cbf20SChris Wilson
20012c255b5SChris Wilson __active_retire(ref);
20164d6c500SChris Wilson }
20264d6c500SChris Wilson
203df9f85d8SChris Wilson static inline struct dma_fence **
__active_fence_slot(struct i915_active_fence * active)204df9f85d8SChris Wilson __active_fence_slot(struct i915_active_fence *active)
205df9f85d8SChris Wilson {
206df9f85d8SChris Wilson return (struct dma_fence ** __force)&active->fence;
207df9f85d8SChris Wilson }
208df9f85d8SChris Wilson
209df9f85d8SChris Wilson static inline bool
active_fence_cb(struct dma_fence * fence,struct dma_fence_cb * cb)210df9f85d8SChris Wilson active_fence_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
211df9f85d8SChris Wilson {
212df9f85d8SChris Wilson struct i915_active_fence *active =
213df9f85d8SChris Wilson container_of(cb, typeof(*active), cb);
214df9f85d8SChris Wilson
215df9f85d8SChris Wilson return cmpxchg(__active_fence_slot(active), fence, NULL) == fence;
216df9f85d8SChris Wilson }
217df9f85d8SChris Wilson
21864d6c500SChris Wilson static void
node_retire(struct dma_fence * fence,struct dma_fence_cb * cb)219b1e3177bSChris Wilson node_retire(struct dma_fence *fence, struct dma_fence_cb *cb)
22064d6c500SChris Wilson {
221df9f85d8SChris Wilson if (active_fence_cb(fence, cb))
222b1e3177bSChris Wilson active_retire(container_of(cb, struct active_node, base.cb)->ref);
22364d6c500SChris Wilson }
22464d6c500SChris Wilson
225b1e3177bSChris Wilson static void
excl_retire(struct dma_fence * fence,struct dma_fence_cb * cb)226b1e3177bSChris Wilson excl_retire(struct dma_fence *fence, struct dma_fence_cb *cb)
227b1e3177bSChris Wilson {
228df9f85d8SChris Wilson if (active_fence_cb(fence, cb))
229b1e3177bSChris Wilson active_retire(container_of(cb, struct i915_active, excl.cb));
230b1e3177bSChris Wilson }
231b1e3177bSChris Wilson
__active_lookup(struct i915_active * ref,u64 idx)2325d934137SChris Wilson static struct active_node *__active_lookup(struct i915_active *ref, u64 idx)
23364d6c500SChris Wilson {
2345d934137SChris Wilson struct active_node *it;
23564d6c500SChris Wilson
236e28860aeSChris Wilson GEM_BUG_ON(idx == 0); /* 0 is the unordered timeline, rsvd for cache */
23764d6c500SChris Wilson
23864d6c500SChris Wilson /*
23964d6c500SChris Wilson * We track the most recently used timeline to skip a rbtree search
24064d6c500SChris Wilson * for the common case, under typical loads we never need the rbtree
24164d6c500SChris Wilson * at all. We can reuse the last slot if it is empty, that is
24264d6c500SChris Wilson * after the previous activity has been retired, or if it matches the
24364d6c500SChris Wilson * current timeline.
24464d6c500SChris Wilson */
2455d934137SChris Wilson it = READ_ONCE(ref->cache);
246e28860aeSChris Wilson if (it) {
247e28860aeSChris Wilson u64 cached = READ_ONCE(it->timeline);
248e28860aeSChris Wilson
249e28860aeSChris Wilson /* Once claimed, this slot will only belong to this idx */
250e28860aeSChris Wilson if (cached == idx)
2515d934137SChris Wilson return it;
2525d934137SChris Wilson
253e28860aeSChris Wilson /*
254e28860aeSChris Wilson * An unclaimed cache [.timeline=0] can only be claimed once.
255e28860aeSChris Wilson *
256e28860aeSChris Wilson * If the value is already non-zero, some other thread has
257e28860aeSChris Wilson * claimed the cache and we know that is does not match our
258e28860aeSChris Wilson * idx. If, and only if, the timeline is currently zero is it
259e28860aeSChris Wilson * worth competing to claim it atomically for ourselves (for
260e28860aeSChris Wilson * only the winner of that race will cmpxchg return the old
261e28860aeSChris Wilson * value of 0).
262e28860aeSChris Wilson */
2635e963508SChris Wilson if (!cached && !cmpxchg64(&it->timeline, 0, idx))
264e28860aeSChris Wilson return it;
265e28860aeSChris Wilson }
266e28860aeSChris Wilson
2675d934137SChris Wilson BUILD_BUG_ON(offsetof(typeof(*it), node));
2685d934137SChris Wilson
2695d934137SChris Wilson /* While active, the tree can only be built; not destroyed */
2705d934137SChris Wilson GEM_BUG_ON(i915_active_is_idle(ref));
2715d934137SChris Wilson
2725d934137SChris Wilson it = fetch_node(ref->tree.rb_node);
2735d934137SChris Wilson while (it) {
2745d934137SChris Wilson if (it->timeline < idx) {
2755d934137SChris Wilson it = fetch_node(it->node.rb_right);
2765d934137SChris Wilson } else if (it->timeline > idx) {
2775d934137SChris Wilson it = fetch_node(it->node.rb_left);
2785d934137SChris Wilson } else {
2795d934137SChris Wilson WRITE_ONCE(ref->cache, it);
2805d934137SChris Wilson break;
2815d934137SChris Wilson }
2825d934137SChris Wilson }
2835d934137SChris Wilson
2845d934137SChris Wilson /* NB: If the tree rotated beneath us, we may miss our target. */
2855d934137SChris Wilson return it;
2865d934137SChris Wilson }
2875d934137SChris Wilson
2885d934137SChris Wilson static struct i915_active_fence *
active_instance(struct i915_active * ref,u64 idx)2895d934137SChris Wilson active_instance(struct i915_active *ref, u64 idx)
2905d934137SChris Wilson {
291bfaae47dSMaarten Lankhorst struct active_node *node;
2925d934137SChris Wilson struct rb_node **p, *parent;
2935d934137SChris Wilson
2945d934137SChris Wilson node = __active_lookup(ref, idx);
2955d934137SChris Wilson if (likely(node))
29612c255b5SChris Wilson return &node->base;
29764d6c500SChris Wilson
298c9ad602fSChris Wilson spin_lock_irq(&ref->tree_lock);
29912c255b5SChris Wilson GEM_BUG_ON(i915_active_is_idle(ref));
30064d6c500SChris Wilson
30164d6c500SChris Wilson parent = NULL;
30264d6c500SChris Wilson p = &ref->tree.rb_node;
30364d6c500SChris Wilson while (*p) {
30464d6c500SChris Wilson parent = *p;
30564d6c500SChris Wilson
30664d6c500SChris Wilson node = rb_entry(parent, struct active_node, node);
307bfaae47dSMaarten Lankhorst if (node->timeline == idx)
30812c255b5SChris Wilson goto out;
30964d6c500SChris Wilson
31064d6c500SChris Wilson if (node->timeline < idx)
31164d6c500SChris Wilson p = &parent->rb_right;
31264d6c500SChris Wilson else
31364d6c500SChris Wilson p = &parent->rb_left;
31464d6c500SChris Wilson }
31564d6c500SChris Wilson
316bfaae47dSMaarten Lankhorst /*
317bfaae47dSMaarten Lankhorst * XXX: We should preallocate this before i915_active_ref() is ever
318bfaae47dSMaarten Lankhorst * called, but we cannot call into fs_reclaim() anyway, so use GFP_ATOMIC.
319bfaae47dSMaarten Lankhorst */
320512ba03eSDaniel Vetter node = kmem_cache_alloc(slab_cache, GFP_ATOMIC);
321bfaae47dSMaarten Lankhorst if (!node)
322bfaae47dSMaarten Lankhorst goto out;
323bfaae47dSMaarten Lankhorst
324df9f85d8SChris Wilson __i915_active_fence_init(&node->base, NULL, node_retire);
32564d6c500SChris Wilson node->ref = ref;
32664d6c500SChris Wilson node->timeline = idx;
32764d6c500SChris Wilson
32864d6c500SChris Wilson rb_link_node(&node->node, parent, p);
32964d6c500SChris Wilson rb_insert_color(&node->node, &ref->tree);
33064d6c500SChris Wilson
33164d6c500SChris Wilson out:
3325d934137SChris Wilson WRITE_ONCE(ref->cache, node);
333c9ad602fSChris Wilson spin_unlock_irq(&ref->tree_lock);
33412c255b5SChris Wilson
33512c255b5SChris Wilson return &node->base;
33664d6c500SChris Wilson }
33764d6c500SChris Wilson
__i915_active_init(struct i915_active * ref,int (* active)(struct i915_active * ref),void (* retire)(struct i915_active * ref),unsigned long flags,struct lock_class_key * mkey,struct lock_class_key * wkey)338b1e3177bSChris Wilson void __i915_active_init(struct i915_active *ref,
33912c255b5SChris Wilson int (*active)(struct i915_active *ref),
34012c255b5SChris Wilson void (*retire)(struct i915_active *ref),
341c3b14760SMatthew Auld unsigned long flags,
342ae303004SChris Wilson struct lock_class_key *mkey,
343ae303004SChris Wilson struct lock_class_key *wkey)
34464d6c500SChris Wilson {
3455361db1aSChris Wilson debug_active_init(ref);
3465361db1aSChris Wilson
347c3b14760SMatthew Auld ref->flags = flags;
34812c255b5SChris Wilson ref->active = active;
349c3b14760SMatthew Auld ref->retire = retire;
3502850748eSChris Wilson
351c9ad602fSChris Wilson spin_lock_init(&ref->tree_lock);
35264d6c500SChris Wilson ref->tree = RB_ROOT;
35312c255b5SChris Wilson ref->cache = NULL;
354c9ad602fSChris Wilson
355d8af05ffSChris Wilson init_llist_head(&ref->preallocated_barriers);
35612c255b5SChris Wilson atomic_set(&ref->count, 0);
357ae303004SChris Wilson __mutex_init(&ref->mutex, "i915_active", mkey);
358df9f85d8SChris Wilson __i915_active_fence_init(&ref->excl, NULL, excl_retire);
359274cbf20SChris Wilson INIT_WORK(&ref->work, active_work);
360ae303004SChris Wilson #if IS_ENABLED(CONFIG_LOCKDEP)
361ae303004SChris Wilson lockdep_init_map(&ref->work.lockdep_map, "i915_active.work", wkey, 0);
362ae303004SChris Wilson #endif
36364d6c500SChris Wilson }
36464d6c500SChris Wilson
____active_del_barrier(struct i915_active * ref,struct active_node * node,struct intel_engine_cs * engine)365f130b712SChris Wilson static bool ____active_del_barrier(struct i915_active *ref,
366f130b712SChris Wilson struct active_node *node,
367f130b712SChris Wilson struct intel_engine_cs *engine)
368f130b712SChris Wilson
369d8af05ffSChris Wilson {
370d8af05ffSChris Wilson struct llist_node *head = NULL, *tail = NULL;
371d8af05ffSChris Wilson struct llist_node *pos, *next;
372d8af05ffSChris Wilson
37375d0a7f3SChris Wilson GEM_BUG_ON(node->timeline != engine->kernel_context->timeline->fence_context);
374d8af05ffSChris Wilson
375d8af05ffSChris Wilson /*
376d8af05ffSChris Wilson * Rebuild the llist excluding our node. We may perform this
377d8af05ffSChris Wilson * outside of the kernel_context timeline mutex and so someone
378d8af05ffSChris Wilson * else may be manipulating the engine->barrier_tasks, in
379d8af05ffSChris Wilson * which case either we or they will be upset :)
380d8af05ffSChris Wilson *
381d8af05ffSChris Wilson * A second __active_del_barrier() will report failure to claim
382d8af05ffSChris Wilson * the active_node and the caller will just shrug and know not to
383d8af05ffSChris Wilson * claim ownership of its node.
384d8af05ffSChris Wilson *
385d8af05ffSChris Wilson * A concurrent i915_request_add_active_barriers() will miss adding
386d8af05ffSChris Wilson * any of the tasks, but we will try again on the next -- and since
387d8af05ffSChris Wilson * we are actively using the barrier, we know that there will be
388d8af05ffSChris Wilson * at least another opportunity when we idle.
389d8af05ffSChris Wilson */
390d8af05ffSChris Wilson llist_for_each_safe(pos, next, llist_del_all(&engine->barrier_tasks)) {
391d8af05ffSChris Wilson if (node == barrier_from_ll(pos)) {
392d8af05ffSChris Wilson node = NULL;
393d8af05ffSChris Wilson continue;
394d8af05ffSChris Wilson }
395d8af05ffSChris Wilson
396d8af05ffSChris Wilson pos->next = head;
397d8af05ffSChris Wilson head = pos;
398d8af05ffSChris Wilson if (!tail)
399d8af05ffSChris Wilson tail = pos;
400d8af05ffSChris Wilson }
401d8af05ffSChris Wilson if (head)
402d8af05ffSChris Wilson llist_add_batch(head, tail, &engine->barrier_tasks);
403d8af05ffSChris Wilson
404d8af05ffSChris Wilson return !node;
405d8af05ffSChris Wilson }
406d8af05ffSChris Wilson
407f130b712SChris Wilson static bool
__active_del_barrier(struct i915_active * ref,struct active_node * node)408f130b712SChris Wilson __active_del_barrier(struct i915_active *ref, struct active_node *node)
409f130b712SChris Wilson {
410f130b712SChris Wilson return ____active_del_barrier(ref, node, barrier_to_engine(node));
411f130b712SChris Wilson }
412f130b712SChris Wilson
4135d934137SChris Wilson static bool
replace_barrier(struct i915_active * ref,struct i915_active_fence * active)4145d934137SChris Wilson replace_barrier(struct i915_active *ref, struct i915_active_fence *active)
41564d6c500SChris Wilson {
4165d934137SChris Wilson if (!is_barrier(active)) /* proto-node used by our idle barrier? */
4175d934137SChris Wilson return false;
418312c4ba1SChris Wilson
419d8af05ffSChris Wilson /*
420d8af05ffSChris Wilson * This request is on the kernel_context timeline, and so
421d8af05ffSChris Wilson * we can use it to substitute for the pending idle-barrer
422d8af05ffSChris Wilson * request that we want to emit on the kernel_context.
423d8af05ffSChris Wilson */
424e0e6b416SJanusz Krzysztofik return __active_del_barrier(ref, node_from_active(active));
4255d934137SChris Wilson }
4265d934137SChris Wilson
i915_active_add_request(struct i915_active * ref,struct i915_request * rq)427ad5c99e0SMaarten Lankhorst int i915_active_add_request(struct i915_active *ref, struct i915_request *rq)
4285d934137SChris Wilson {
429e0e6b416SJanusz Krzysztofik u64 idx = i915_request_timeline(rq)->fence_context;
430ad5c99e0SMaarten Lankhorst struct dma_fence *fence = &rq->fence;
4315d934137SChris Wilson struct i915_active_fence *active;
4325d934137SChris Wilson int err;
4335d934137SChris Wilson
4345d934137SChris Wilson /* Prevent reaping in case we malloc/wait while building the tree */
4355d934137SChris Wilson err = i915_active_acquire(ref);
4365d934137SChris Wilson if (err)
4375d934137SChris Wilson return err;
4385d934137SChris Wilson
439e0e6b416SJanusz Krzysztofik do {
440e0e6b416SJanusz Krzysztofik active = active_instance(ref, idx);
4415d934137SChris Wilson if (!active) {
4425d934137SChris Wilson err = -ENOMEM;
4435d934137SChris Wilson goto out;
4445d934137SChris Wilson }
4455d934137SChris Wilson
4465d934137SChris Wilson if (replace_barrier(ref, active)) {
447b1e3177bSChris Wilson RCU_INIT_POINTER(active->fence, NULL);
448b1e3177bSChris Wilson atomic_dec(&ref->count);
449d8af05ffSChris Wilson }
450e0e6b416SJanusz Krzysztofik } while (unlikely(is_barrier(active)));
451e0e6b416SJanusz Krzysztofik
452*a337b64fSJanusz Krzysztofik fence = __i915_active_fence_set(active, fence);
453*a337b64fSJanusz Krzysztofik if (!fence)
4545d934137SChris Wilson __i915_active_acquire(ref);
455*a337b64fSJanusz Krzysztofik else
456*a337b64fSJanusz Krzysztofik dma_fence_put(fence);
45764d6c500SChris Wilson
458312c4ba1SChris Wilson out:
459312c4ba1SChris Wilson i915_active_release(ref);
460312c4ba1SChris Wilson return err;
46164d6c500SChris Wilson }
46264d6c500SChris Wilson
4635d934137SChris Wilson static struct dma_fence *
__i915_active_set_fence(struct i915_active * ref,struct i915_active_fence * active,struct dma_fence * fence)4645d934137SChris Wilson __i915_active_set_fence(struct i915_active *ref,
4655d934137SChris Wilson struct i915_active_fence *active,
4665d934137SChris Wilson struct dma_fence *fence)
4672850748eSChris Wilson {
468e3793468SChris Wilson struct dma_fence *prev;
469e3793468SChris Wilson
4705d934137SChris Wilson if (replace_barrier(ref, active)) {
4715d934137SChris Wilson RCU_INIT_POINTER(active->fence, fence);
4725d934137SChris Wilson return NULL;
4735d934137SChris Wilson }
4742850748eSChris Wilson
4755d934137SChris Wilson prev = __i915_active_fence_set(active, fence);
476*a337b64fSJanusz Krzysztofik if (!prev)
4775d934137SChris Wilson __i915_active_acquire(ref);
478e3793468SChris Wilson
479e3793468SChris Wilson return prev;
4802850748eSChris Wilson }
481b1e3177bSChris Wilson
4825d934137SChris Wilson struct dma_fence *
i915_active_set_exclusive(struct i915_active * ref,struct dma_fence * f)4835d934137SChris Wilson i915_active_set_exclusive(struct i915_active *ref, struct dma_fence *f)
4845d934137SChris Wilson {
4855d934137SChris Wilson /* We expect the caller to manage the exclusive timeline ordering */
4865d934137SChris Wilson return __i915_active_set_fence(ref, &ref->excl, f);
4875d934137SChris Wilson }
4885d934137SChris Wilson
i915_active_acquire_if_busy(struct i915_active * ref)489b1e3177bSChris Wilson bool i915_active_acquire_if_busy(struct i915_active *ref)
490b1e3177bSChris Wilson {
491b1e3177bSChris Wilson debug_active_assert(ref);
492b1e3177bSChris Wilson return atomic_add_unless(&ref->count, 1, 0);
4932850748eSChris Wilson }
4942850748eSChris Wilson
__i915_active_activate(struct i915_active * ref)49504240e30SChris Wilson static void __i915_active_activate(struct i915_active *ref)
49604240e30SChris Wilson {
49704240e30SChris Wilson spin_lock_irq(&ref->tree_lock); /* __active_retire() */
49804240e30SChris Wilson if (!atomic_fetch_inc(&ref->count))
49904240e30SChris Wilson debug_active_activate(ref);
50004240e30SChris Wilson spin_unlock_irq(&ref->tree_lock);
50104240e30SChris Wilson }
50204240e30SChris Wilson
i915_active_acquire(struct i915_active * ref)50312c255b5SChris Wilson int i915_active_acquire(struct i915_active *ref)
50464d6c500SChris Wilson {
50512c255b5SChris Wilson int err;
50612c255b5SChris Wilson
507b1e3177bSChris Wilson if (i915_active_acquire_if_busy(ref))
50812c255b5SChris Wilson return 0;
5095361db1aSChris Wilson
51004240e30SChris Wilson if (!ref->active) {
51104240e30SChris Wilson __i915_active_activate(ref);
51204240e30SChris Wilson return 0;
51304240e30SChris Wilson }
51404240e30SChris Wilson
51512c255b5SChris Wilson err = mutex_lock_interruptible(&ref->mutex);
51612c255b5SChris Wilson if (err)
51712c255b5SChris Wilson return err;
5185361db1aSChris Wilson
519ac0e331aSChris Wilson if (likely(!i915_active_acquire_if_busy(ref))) {
52012c255b5SChris Wilson err = ref->active(ref);
52104240e30SChris Wilson if (!err)
52204240e30SChris Wilson __i915_active_activate(ref);
523ac0e331aSChris Wilson }
52412c255b5SChris Wilson
52512c255b5SChris Wilson mutex_unlock(&ref->mutex);
52612c255b5SChris Wilson
52712c255b5SChris Wilson return err;
52864d6c500SChris Wilson }
52964d6c500SChris Wilson
i915_active_acquire_for_context(struct i915_active * ref,u64 idx)5305d934137SChris Wilson int i915_active_acquire_for_context(struct i915_active *ref, u64 idx)
5315d934137SChris Wilson {
5325d934137SChris Wilson struct i915_active_fence *active;
5335d934137SChris Wilson int err;
5345d934137SChris Wilson
5355d934137SChris Wilson err = i915_active_acquire(ref);
5365d934137SChris Wilson if (err)
5375d934137SChris Wilson return err;
5385d934137SChris Wilson
5395d934137SChris Wilson active = active_instance(ref, idx);
5405d934137SChris Wilson if (!active) {
5415d934137SChris Wilson i915_active_release(ref);
5425d934137SChris Wilson return -ENOMEM;
5435d934137SChris Wilson }
5445d934137SChris Wilson
5455d934137SChris Wilson return 0; /* return with active ref */
5465d934137SChris Wilson }
5475d934137SChris Wilson
i915_active_release(struct i915_active * ref)54864d6c500SChris Wilson void i915_active_release(struct i915_active *ref)
54964d6c500SChris Wilson {
5505361db1aSChris Wilson debug_active_assert(ref);
55112c255b5SChris Wilson active_retire(ref);
55264d6c500SChris Wilson }
55364d6c500SChris Wilson
enable_signaling(struct i915_active_fence * active)554b1e3177bSChris Wilson static void enable_signaling(struct i915_active_fence *active)
55579c7a28eSChris Wilson {
556b1e3177bSChris Wilson struct dma_fence *fence;
55779c7a28eSChris Wilson
558c0e31018SChris Wilson if (unlikely(is_barrier(active)))
559c0e31018SChris Wilson return;
560c0e31018SChris Wilson
561b1e3177bSChris Wilson fence = i915_active_fence_get(active);
562b1e3177bSChris Wilson if (!fence)
563b1e3177bSChris Wilson return;
56479c7a28eSChris Wilson
565b1e3177bSChris Wilson dma_fence_enable_sw_signaling(fence);
566b1e3177bSChris Wilson dma_fence_put(fence);
5672850748eSChris Wilson }
5682850748eSChris Wilson
flush_barrier(struct active_node * it)569d13a3177SChris Wilson static int flush_barrier(struct active_node *it)
570d13a3177SChris Wilson {
571d13a3177SChris Wilson struct intel_engine_cs *engine;
572d13a3177SChris Wilson
573d13a3177SChris Wilson if (likely(!is_barrier(&it->base)))
574d13a3177SChris Wilson return 0;
575d13a3177SChris Wilson
576d13a3177SChris Wilson engine = __barrier_to_engine(it);
577d13a3177SChris Wilson smp_rmb(); /* serialise with add_active_barriers */
578d13a3177SChris Wilson if (!is_barrier(&it->base))
579d13a3177SChris Wilson return 0;
580d13a3177SChris Wilson
581d13a3177SChris Wilson return intel_engine_flush_barriers(engine);
582d13a3177SChris Wilson }
583d13a3177SChris Wilson
flush_lazy_signals(struct i915_active * ref)584d13a3177SChris Wilson static int flush_lazy_signals(struct i915_active *ref)
58564d6c500SChris Wilson {
58664d6c500SChris Wilson struct active_node *it, *n;
587b1e3177bSChris Wilson int err = 0;
58864d6c500SChris Wilson
589d13a3177SChris Wilson enable_signaling(&ref->excl);
590d13a3177SChris Wilson rbtree_postorder_for_each_entry_safe(it, n, &ref->tree, node) {
591d13a3177SChris Wilson err = flush_barrier(it); /* unconnected idle barrier? */
592d13a3177SChris Wilson if (err)
593d13a3177SChris Wilson break;
594d13a3177SChris Wilson
595d13a3177SChris Wilson enable_signaling(&it->base);
596d13a3177SChris Wilson }
597d13a3177SChris Wilson
598d13a3177SChris Wilson return err;
599d13a3177SChris Wilson }
600d13a3177SChris Wilson
__i915_active_wait(struct i915_active * ref,int state)601d75a92a8SChris Wilson int __i915_active_wait(struct i915_active *ref, int state)
602d13a3177SChris Wilson {
60312c255b5SChris Wilson might_sleep();
604d650d1f5SChris Wilson
605b1e3177bSChris Wilson /* Any fence added after the wait begins will not be auto-signaled */
606f6e98a18SChris Wilson if (i915_active_acquire_if_busy(ref)) {
607f6e98a18SChris Wilson int err;
608f6e98a18SChris Wilson
609d13a3177SChris Wilson err = flush_lazy_signals(ref);
610b1e3177bSChris Wilson i915_active_release(ref);
611afd1bcd4SChris Wilson if (err)
61212c255b5SChris Wilson return err;
613afd1bcd4SChris Wilson
614f6e98a18SChris Wilson if (___wait_var_event(ref, i915_active_is_idle(ref),
615d75a92a8SChris Wilson state, 0, 0, schedule()))
61679c7a28eSChris Wilson return -EINTR;
617f6e98a18SChris Wilson }
61879c7a28eSChris Wilson
619f6e98a18SChris Wilson /*
620f6e98a18SChris Wilson * After the wait is complete, the caller may free the active.
621f6e98a18SChris Wilson * We have to flush any concurrent retirement before returning.
622f6e98a18SChris Wilson */
623e1cda6a5SChris Wilson flush_work(&ref->work);
624afd1bcd4SChris Wilson return 0;
62564d6c500SChris Wilson }
62664d6c500SChris Wilson
__await_active(struct i915_active_fence * active,int (* fn)(void * arg,struct dma_fence * fence),void * arg)62729e6ecf3SChris Wilson static int __await_active(struct i915_active_fence *active,
62829e6ecf3SChris Wilson int (*fn)(void *arg, struct dma_fence *fence),
62929e6ecf3SChris Wilson void *arg)
63029e6ecf3SChris Wilson {
63129e6ecf3SChris Wilson struct dma_fence *fence;
63229e6ecf3SChris Wilson
63329e6ecf3SChris Wilson if (is_barrier(active)) /* XXX flush the barrier? */
63429e6ecf3SChris Wilson return 0;
63529e6ecf3SChris Wilson
63629e6ecf3SChris Wilson fence = i915_active_fence_get(active);
63729e6ecf3SChris Wilson if (fence) {
63829e6ecf3SChris Wilson int err;
63929e6ecf3SChris Wilson
64029e6ecf3SChris Wilson err = fn(arg, fence);
64129e6ecf3SChris Wilson dma_fence_put(fence);
64229e6ecf3SChris Wilson if (err < 0)
64329e6ecf3SChris Wilson return err;
64429e6ecf3SChris Wilson }
64529e6ecf3SChris Wilson
64629e6ecf3SChris Wilson return 0;
64729e6ecf3SChris Wilson }
64829e6ecf3SChris Wilson
6493b0a0579SChris Wilson struct wait_barrier {
6503b0a0579SChris Wilson struct wait_queue_entry base;
6513b0a0579SChris Wilson struct i915_active *ref;
6523b0a0579SChris Wilson };
6533b0a0579SChris Wilson
6543b0a0579SChris Wilson static int
barrier_wake(wait_queue_entry_t * wq,unsigned int mode,int flags,void * key)6553b0a0579SChris Wilson barrier_wake(wait_queue_entry_t *wq, unsigned int mode, int flags, void *key)
6563b0a0579SChris Wilson {
6573b0a0579SChris Wilson struct wait_barrier *wb = container_of(wq, typeof(*wb), base);
6583b0a0579SChris Wilson
6593b0a0579SChris Wilson if (i915_active_is_idle(wb->ref)) {
6603b0a0579SChris Wilson list_del(&wq->entry);
6613b0a0579SChris Wilson i915_sw_fence_complete(wq->private);
6623b0a0579SChris Wilson kfree(wq);
6633b0a0579SChris Wilson }
6643b0a0579SChris Wilson
6653b0a0579SChris Wilson return 0;
6663b0a0579SChris Wilson }
6673b0a0579SChris Wilson
__await_barrier(struct i915_active * ref,struct i915_sw_fence * fence)6683b0a0579SChris Wilson static int __await_barrier(struct i915_active *ref, struct i915_sw_fence *fence)
6693b0a0579SChris Wilson {
6703b0a0579SChris Wilson struct wait_barrier *wb;
6713b0a0579SChris Wilson
6723b0a0579SChris Wilson wb = kmalloc(sizeof(*wb), GFP_KERNEL);
6733b0a0579SChris Wilson if (unlikely(!wb))
6743b0a0579SChris Wilson return -ENOMEM;
6753b0a0579SChris Wilson
6763b0a0579SChris Wilson GEM_BUG_ON(i915_active_is_idle(ref));
6773b0a0579SChris Wilson if (!i915_sw_fence_await(fence)) {
6783b0a0579SChris Wilson kfree(wb);
6793b0a0579SChris Wilson return -EINVAL;
6803b0a0579SChris Wilson }
6813b0a0579SChris Wilson
6823b0a0579SChris Wilson wb->base.flags = 0;
6833b0a0579SChris Wilson wb->base.func = barrier_wake;
6843b0a0579SChris Wilson wb->base.private = fence;
6853b0a0579SChris Wilson wb->ref = ref;
6863b0a0579SChris Wilson
6873b0a0579SChris Wilson add_wait_queue(__var_waitqueue(ref), &wb->base);
6883b0a0579SChris Wilson return 0;
6893b0a0579SChris Wilson }
6903b0a0579SChris Wilson
await_active(struct i915_active * ref,unsigned int flags,int (* fn)(void * arg,struct dma_fence * fence),void * arg,struct i915_sw_fence * barrier)69129e6ecf3SChris Wilson static int await_active(struct i915_active *ref,
69229e6ecf3SChris Wilson unsigned int flags,
69329e6ecf3SChris Wilson int (*fn)(void *arg, struct dma_fence *fence),
6943b0a0579SChris Wilson void *arg, struct i915_sw_fence *barrier)
69564d6c500SChris Wilson {
6962850748eSChris Wilson int err = 0;
69712c255b5SChris Wilson
6983b0a0579SChris Wilson if (!i915_active_acquire_if_busy(ref))
6993b0a0579SChris Wilson return 0;
7003b0a0579SChris Wilson
701442dbc5cSChris Wilson if (flags & I915_ACTIVE_AWAIT_EXCL &&
702442dbc5cSChris Wilson rcu_access_pointer(ref->excl.fence)) {
70329e6ecf3SChris Wilson err = __await_active(&ref->excl, fn, arg);
70429e6ecf3SChris Wilson if (err)
7053b0a0579SChris Wilson goto out;
70664d6c500SChris Wilson }
70764d6c500SChris Wilson
7083b0a0579SChris Wilson if (flags & I915_ACTIVE_AWAIT_ACTIVE) {
70929e6ecf3SChris Wilson struct active_node *it, *n;
71029e6ecf3SChris Wilson
71129e6ecf3SChris Wilson rbtree_postorder_for_each_entry_safe(it, n, &ref->tree, node) {
71229e6ecf3SChris Wilson err = __await_active(&it->base, fn, arg);
71329e6ecf3SChris Wilson if (err)
7143b0a0579SChris Wilson goto out;
71529e6ecf3SChris Wilson }
71629e6ecf3SChris Wilson }
71729e6ecf3SChris Wilson
7183b0a0579SChris Wilson if (flags & I915_ACTIVE_AWAIT_BARRIER) {
7193b0a0579SChris Wilson err = flush_lazy_signals(ref);
7203b0a0579SChris Wilson if (err)
7213b0a0579SChris Wilson goto out;
7223b0a0579SChris Wilson
7233b0a0579SChris Wilson err = __await_barrier(ref, barrier);
7243b0a0579SChris Wilson if (err)
7253b0a0579SChris Wilson goto out;
7263b0a0579SChris Wilson }
7273b0a0579SChris Wilson
7283b0a0579SChris Wilson out:
7293b0a0579SChris Wilson i915_active_release(ref);
7303b0a0579SChris Wilson return err;
73129e6ecf3SChris Wilson }
73229e6ecf3SChris Wilson
rq_await_fence(void * arg,struct dma_fence * fence)73329e6ecf3SChris Wilson static int rq_await_fence(void *arg, struct dma_fence *fence)
73429e6ecf3SChris Wilson {
73529e6ecf3SChris Wilson return i915_request_await_dma_fence(arg, fence);
73629e6ecf3SChris Wilson }
73729e6ecf3SChris Wilson
i915_request_await_active(struct i915_request * rq,struct i915_active * ref,unsigned int flags)73829e6ecf3SChris Wilson int i915_request_await_active(struct i915_request *rq,
73929e6ecf3SChris Wilson struct i915_active *ref,
74029e6ecf3SChris Wilson unsigned int flags)
74129e6ecf3SChris Wilson {
7423b0a0579SChris Wilson return await_active(ref, flags, rq_await_fence, rq, &rq->submit);
74329e6ecf3SChris Wilson }
74429e6ecf3SChris Wilson
sw_await_fence(void * arg,struct dma_fence * fence)74529e6ecf3SChris Wilson static int sw_await_fence(void *arg, struct dma_fence *fence)
74629e6ecf3SChris Wilson {
74729e6ecf3SChris Wilson return i915_sw_fence_await_dma_fence(arg, fence, 0,
74829e6ecf3SChris Wilson GFP_NOWAIT | __GFP_NOWARN);
74929e6ecf3SChris Wilson }
75029e6ecf3SChris Wilson
i915_sw_fence_await_active(struct i915_sw_fence * fence,struct i915_active * ref,unsigned int flags)75129e6ecf3SChris Wilson int i915_sw_fence_await_active(struct i915_sw_fence *fence,
75229e6ecf3SChris Wilson struct i915_active *ref,
75329e6ecf3SChris Wilson unsigned int flags)
75429e6ecf3SChris Wilson {
7553b0a0579SChris Wilson return await_active(ref, flags, sw_await_fence, fence, fence);
75629e6ecf3SChris Wilson }
75729e6ecf3SChris Wilson
i915_active_fini(struct i915_active * ref)75864d6c500SChris Wilson void i915_active_fini(struct i915_active *ref)
75964d6c500SChris Wilson {
7605361db1aSChris Wilson debug_active_fini(ref);
76112c255b5SChris Wilson GEM_BUG_ON(atomic_read(&ref->count));
762274cbf20SChris Wilson GEM_BUG_ON(work_pending(&ref->work));
76312c255b5SChris Wilson mutex_destroy(&ref->mutex);
76499a7f4daSChris Wilson
76599a7f4daSChris Wilson if (ref->cache)
766512ba03eSDaniel Vetter kmem_cache_free(slab_cache, ref->cache);
76764d6c500SChris Wilson }
76864d6c500SChris Wilson
is_idle_barrier(struct active_node * node,u64 idx)769d8af05ffSChris Wilson static inline bool is_idle_barrier(struct active_node *node, u64 idx)
770d8af05ffSChris Wilson {
771b1e3177bSChris Wilson return node->timeline == idx && !i915_active_fence_isset(&node->base);
772d8af05ffSChris Wilson }
773d8af05ffSChris Wilson
reuse_idle_barrier(struct i915_active * ref,u64 idx)774d8af05ffSChris Wilson static struct active_node *reuse_idle_barrier(struct i915_active *ref, u64 idx)
775d8af05ffSChris Wilson {
776d8af05ffSChris Wilson struct rb_node *prev, *p;
777d8af05ffSChris Wilson
778d8af05ffSChris Wilson if (RB_EMPTY_ROOT(&ref->tree))
779d8af05ffSChris Wilson return NULL;
780d8af05ffSChris Wilson
781d8af05ffSChris Wilson GEM_BUG_ON(i915_active_is_idle(ref));
782d8af05ffSChris Wilson
783d8af05ffSChris Wilson /*
784d8af05ffSChris Wilson * Try to reuse any existing barrier nodes already allocated for this
785d8af05ffSChris Wilson * i915_active, due to overlapping active phases there is likely a
786d8af05ffSChris Wilson * node kept alive (as we reuse before parking). We prefer to reuse
787d8af05ffSChris Wilson * completely idle barriers (less hassle in manipulating the llists),
788d8af05ffSChris Wilson * but otherwise any will do.
789d8af05ffSChris Wilson */
790d8af05ffSChris Wilson if (ref->cache && is_idle_barrier(ref->cache, idx)) {
791d8af05ffSChris Wilson p = &ref->cache->node;
792d8af05ffSChris Wilson goto match;
793d8af05ffSChris Wilson }
794d8af05ffSChris Wilson
795d8af05ffSChris Wilson prev = NULL;
796d8af05ffSChris Wilson p = ref->tree.rb_node;
797d8af05ffSChris Wilson while (p) {
798d8af05ffSChris Wilson struct active_node *node =
799d8af05ffSChris Wilson rb_entry(p, struct active_node, node);
800d8af05ffSChris Wilson
801d8af05ffSChris Wilson if (is_idle_barrier(node, idx))
802d8af05ffSChris Wilson goto match;
803d8af05ffSChris Wilson
804d8af05ffSChris Wilson prev = p;
805d8af05ffSChris Wilson if (node->timeline < idx)
8069ff33bbcSChris Wilson p = READ_ONCE(p->rb_right);
807d8af05ffSChris Wilson else
8089ff33bbcSChris Wilson p = READ_ONCE(p->rb_left);
809d8af05ffSChris Wilson }
810d8af05ffSChris Wilson
811d8af05ffSChris Wilson /*
812d8af05ffSChris Wilson * No quick match, but we did find the leftmost rb_node for the
813d8af05ffSChris Wilson * kernel_context. Walk the rb_tree in-order to see if there were
814d8af05ffSChris Wilson * any idle-barriers on this timeline that we missed, or just use
815d8af05ffSChris Wilson * the first pending barrier.
816d8af05ffSChris Wilson */
817d8af05ffSChris Wilson for (p = prev; p; p = rb_next(p)) {
818d8af05ffSChris Wilson struct active_node *node =
819d8af05ffSChris Wilson rb_entry(p, struct active_node, node);
820f130b712SChris Wilson struct intel_engine_cs *engine;
821d8af05ffSChris Wilson
822d8af05ffSChris Wilson if (node->timeline > idx)
823d8af05ffSChris Wilson break;
824d8af05ffSChris Wilson
825d8af05ffSChris Wilson if (node->timeline < idx)
826d8af05ffSChris Wilson continue;
827d8af05ffSChris Wilson
828d8af05ffSChris Wilson if (is_idle_barrier(node, idx))
829d8af05ffSChris Wilson goto match;
830d8af05ffSChris Wilson
831d8af05ffSChris Wilson /*
832d8af05ffSChris Wilson * The list of pending barriers is protected by the
833d8af05ffSChris Wilson * kernel_context timeline, which notably we do not hold
834d8af05ffSChris Wilson * here. i915_request_add_active_barriers() may consume
835d8af05ffSChris Wilson * the barrier before we claim it, so we have to check
836d8af05ffSChris Wilson * for success.
837d8af05ffSChris Wilson */
838f130b712SChris Wilson engine = __barrier_to_engine(node);
839f130b712SChris Wilson smp_rmb(); /* serialise with add_active_barriers */
840f130b712SChris Wilson if (is_barrier(&node->base) &&
841f130b712SChris Wilson ____active_del_barrier(ref, node, engine))
842d8af05ffSChris Wilson goto match;
843d8af05ffSChris Wilson }
844d8af05ffSChris Wilson
845d8af05ffSChris Wilson return NULL;
846d8af05ffSChris Wilson
847d8af05ffSChris Wilson match:
8489ff33bbcSChris Wilson spin_lock_irq(&ref->tree_lock);
849d8af05ffSChris Wilson rb_erase(p, &ref->tree); /* Hide from waits and sibling allocations */
850d8af05ffSChris Wilson if (p == &ref->cache->node)
8515d934137SChris Wilson WRITE_ONCE(ref->cache, NULL);
852c9ad602fSChris Wilson spin_unlock_irq(&ref->tree_lock);
853d8af05ffSChris Wilson
854d8af05ffSChris Wilson return rb_entry(p, struct active_node, node);
855d8af05ffSChris Wilson }
856d8af05ffSChris Wilson
i915_active_acquire_preallocate_barrier(struct i915_active * ref,struct intel_engine_cs * engine)857ce476c80SChris Wilson int i915_active_acquire_preallocate_barrier(struct i915_active *ref,
858ce476c80SChris Wilson struct intel_engine_cs *engine)
859ce476c80SChris Wilson {
8603f99a614STvrtko Ursulin intel_engine_mask_t tmp, mask = engine->mask;
861d4c3c0b8SJosé Roberto de Souza struct llist_node *first = NULL, *last = NULL;
862a50134b1STvrtko Ursulin struct intel_gt *gt = engine->gt;
863ce476c80SChris Wilson
864b5e8e954SChris Wilson GEM_BUG_ON(i915_active_is_idle(ref));
86584135022SChris Wilson
86684135022SChris Wilson /* Wait until the previous preallocation is completed */
86784135022SChris Wilson while (!llist_empty(&ref->preallocated_barriers))
86884135022SChris Wilson cond_resched();
869d8af05ffSChris Wilson
870d8af05ffSChris Wilson /*
871d8af05ffSChris Wilson * Preallocate a node for each physical engine supporting the target
872d8af05ffSChris Wilson * engine (remember virtual engines have more than one sibling).
873d8af05ffSChris Wilson * We can then use the preallocated nodes in
874d8af05ffSChris Wilson * i915_active_acquire_barrier()
875d8af05ffSChris Wilson */
876416d3838SChris Wilson GEM_BUG_ON(!mask);
877a50134b1STvrtko Ursulin for_each_engine_masked(engine, gt, mask, tmp) {
87875d0a7f3SChris Wilson u64 idx = engine->kernel_context->timeline->fence_context;
879d4c3c0b8SJosé Roberto de Souza struct llist_node *prev = first;
880ce476c80SChris Wilson struct active_node *node;
881ce476c80SChris Wilson
8829ff33bbcSChris Wilson rcu_read_lock();
883d8af05ffSChris Wilson node = reuse_idle_barrier(ref, idx);
8849ff33bbcSChris Wilson rcu_read_unlock();
885d8af05ffSChris Wilson if (!node) {
886512ba03eSDaniel Vetter node = kmem_cache_alloc(slab_cache, GFP_KERNEL);
887e714977eSTianjia Zhang if (!node)
8887009db14SChris Wilson goto unwind;
889ce476c80SChris Wilson
890b1e3177bSChris Wilson RCU_INIT_POINTER(node->base.fence, NULL);
891b1e3177bSChris Wilson node->base.cb.func = node_retire;
892d8af05ffSChris Wilson node->timeline = idx;
893ce476c80SChris Wilson node->ref = ref;
894d8af05ffSChris Wilson }
895ce476c80SChris Wilson
896b1e3177bSChris Wilson if (!i915_active_fence_isset(&node->base)) {
897d8af05ffSChris Wilson /*
898d8af05ffSChris Wilson * Mark this as being *our* unconnected proto-node.
899d8af05ffSChris Wilson *
900d8af05ffSChris Wilson * Since this node is not in any list, and we have
901d8af05ffSChris Wilson * decoupled it from the rbtree, we can reuse the
902d8af05ffSChris Wilson * request to indicate this is an idle-barrier node
903d8af05ffSChris Wilson * and then we can use the rb_node and list pointers
904d8af05ffSChris Wilson * for our tracking of the pending barrier.
905d8af05ffSChris Wilson */
906b1e3177bSChris Wilson RCU_INIT_POINTER(node->base.fence, ERR_PTR(-EAGAIN));
907b1e3177bSChris Wilson node->base.cb.node.prev = (void *)engine;
9085d934137SChris Wilson __i915_active_acquire(ref);
909d8af05ffSChris Wilson }
910df9f85d8SChris Wilson GEM_BUG_ON(rcu_access_pointer(node->base.fence) != ERR_PTR(-EAGAIN));
911d8af05ffSChris Wilson
912d8af05ffSChris Wilson GEM_BUG_ON(barrier_to_engine(node) != engine);
913d4c3c0b8SJosé Roberto de Souza first = barrier_to_ll(node);
914d4c3c0b8SJosé Roberto de Souza first->next = prev;
915d4c3c0b8SJosé Roberto de Souza if (!last)
916d4c3c0b8SJosé Roberto de Souza last = first;
9177009db14SChris Wilson intel_engine_pm_get(engine);
918ce476c80SChris Wilson }
919ce476c80SChris Wilson
92084135022SChris Wilson GEM_BUG_ON(!llist_empty(&ref->preallocated_barriers));
921d4c3c0b8SJosé Roberto de Souza llist_add_batch(first, last, &ref->preallocated_barriers);
92284135022SChris Wilson
9237009db14SChris Wilson return 0;
9247009db14SChris Wilson
9257009db14SChris Wilson unwind:
926d4c3c0b8SJosé Roberto de Souza while (first) {
927d4c3c0b8SJosé Roberto de Souza struct active_node *node = barrier_from_ll(first);
9287009db14SChris Wilson
929d4c3c0b8SJosé Roberto de Souza first = first->next;
93084135022SChris Wilson
931d8af05ffSChris Wilson atomic_dec(&ref->count);
932d8af05ffSChris Wilson intel_engine_pm_put(barrier_to_engine(node));
9337009db14SChris Wilson
934512ba03eSDaniel Vetter kmem_cache_free(slab_cache, node);
9357009db14SChris Wilson }
936e714977eSTianjia Zhang return -ENOMEM;
937ce476c80SChris Wilson }
938ce476c80SChris Wilson
i915_active_acquire_barrier(struct i915_active * ref)939ce476c80SChris Wilson void i915_active_acquire_barrier(struct i915_active *ref)
940ce476c80SChris Wilson {
941ce476c80SChris Wilson struct llist_node *pos, *next;
942c9ad602fSChris Wilson unsigned long flags;
943ce476c80SChris Wilson
94412c255b5SChris Wilson GEM_BUG_ON(i915_active_is_idle(ref));
945ce476c80SChris Wilson
946d8af05ffSChris Wilson /*
947d8af05ffSChris Wilson * Transfer the list of preallocated barriers into the
948d8af05ffSChris Wilson * i915_active rbtree, but only as proto-nodes. They will be
949d8af05ffSChris Wilson * populated by i915_request_add_active_barriers() to point to the
950d8af05ffSChris Wilson * request that will eventually release them.
951d8af05ffSChris Wilson */
952d8af05ffSChris Wilson llist_for_each_safe(pos, next, take_preallocated_barriers(ref)) {
953d8af05ffSChris Wilson struct active_node *node = barrier_from_ll(pos);
954d8af05ffSChris Wilson struct intel_engine_cs *engine = barrier_to_engine(node);
955ce476c80SChris Wilson struct rb_node **p, *parent;
956ce476c80SChris Wilson
95707779a76SChris Wilson spin_lock_irqsave_nested(&ref->tree_lock, flags,
95807779a76SChris Wilson SINGLE_DEPTH_NESTING);
959ce476c80SChris Wilson parent = NULL;
960ce476c80SChris Wilson p = &ref->tree.rb_node;
961ce476c80SChris Wilson while (*p) {
962d8af05ffSChris Wilson struct active_node *it;
963d8af05ffSChris Wilson
964ce476c80SChris Wilson parent = *p;
965d8af05ffSChris Wilson
966d8af05ffSChris Wilson it = rb_entry(parent, struct active_node, node);
967d8af05ffSChris Wilson if (it->timeline < node->timeline)
968ce476c80SChris Wilson p = &parent->rb_right;
969ce476c80SChris Wilson else
970ce476c80SChris Wilson p = &parent->rb_left;
971ce476c80SChris Wilson }
972ce476c80SChris Wilson rb_link_node(&node->node, parent, p);
973ce476c80SChris Wilson rb_insert_color(&node->node, &ref->tree);
97407779a76SChris Wilson spin_unlock_irqrestore(&ref->tree_lock, flags);
975ce476c80SChris Wilson
976b7234840SChris Wilson GEM_BUG_ON(!intel_engine_pm_is_awake(engine));
977d8af05ffSChris Wilson llist_add(barrier_to_ll(node), &engine->barrier_tasks);
9781ea7fe77SChris Wilson intel_engine_pm_put_delay(engine, 2);
979ce476c80SChris Wilson }
980ce476c80SChris Wilson }
981ce476c80SChris Wilson
ll_to_fence_slot(struct llist_node * node)982df9f85d8SChris Wilson static struct dma_fence **ll_to_fence_slot(struct llist_node *node)
983df9f85d8SChris Wilson {
984df9f85d8SChris Wilson return __active_fence_slot(&barrier_from_ll(node)->base);
985df9f85d8SChris Wilson }
986df9f85d8SChris Wilson
i915_request_add_active_barriers(struct i915_request * rq)987d8af05ffSChris Wilson void i915_request_add_active_barriers(struct i915_request *rq)
988ce476c80SChris Wilson {
989ce476c80SChris Wilson struct intel_engine_cs *engine = rq->engine;
990ce476c80SChris Wilson struct llist_node *node, *next;
991b1e3177bSChris Wilson unsigned long flags;
992ce476c80SChris Wilson
993e6ba7648SChris Wilson GEM_BUG_ON(!intel_context_is_barrier(rq->context));
994d8af05ffSChris Wilson GEM_BUG_ON(intel_engine_is_virtual(engine));
995d19d71fcSChris Wilson GEM_BUG_ON(i915_request_timeline(rq) != engine->kernel_context->timeline);
996d8af05ffSChris Wilson
997b1e3177bSChris Wilson node = llist_del_all(&engine->barrier_tasks);
998b1e3177bSChris Wilson if (!node)
999b1e3177bSChris Wilson return;
1000d8af05ffSChris Wilson /*
1001d8af05ffSChris Wilson * Attach the list of proto-fences to the in-flight request such
1002d8af05ffSChris Wilson * that the parent i915_active will be released when this request
1003d8af05ffSChris Wilson * is retired.
1004d8af05ffSChris Wilson */
1005b1e3177bSChris Wilson spin_lock_irqsave(&rq->lock, flags);
1006b1e3177bSChris Wilson llist_for_each_safe(node, next, node) {
1007df9f85d8SChris Wilson /* serialise with reuse_idle_barrier */
1008df9f85d8SChris Wilson smp_store_mb(*ll_to_fence_slot(node), &rq->fence);
1009b1e3177bSChris Wilson list_add_tail((struct list_head *)node, &rq->fence.cb_list);
1010ce476c80SChris Wilson }
1011b1e3177bSChris Wilson spin_unlock_irqrestore(&rq->lock, flags);
1012d8af05ffSChris Wilson }
1013ce476c80SChris Wilson
1014b1e3177bSChris Wilson /*
1015b1e3177bSChris Wilson * __i915_active_fence_set: Update the last active fence along its timeline
1016b1e3177bSChris Wilson * @active: the active tracker
1017b1e3177bSChris Wilson * @fence: the new fence (under construction)
1018b1e3177bSChris Wilson *
1019b1e3177bSChris Wilson * Records the new @fence as the last active fence along its timeline in
1020b1e3177bSChris Wilson * this active tracker, moving the tracking callbacks from the previous
1021*a337b64fSJanusz Krzysztofik * fence onto this one. Gets and returns a reference to the previous fence
1022*a337b64fSJanusz Krzysztofik * (if not already completed), which the caller must put after making sure
1023*a337b64fSJanusz Krzysztofik * that it is executed before the new fence. To ensure that the order of
1024*a337b64fSJanusz Krzysztofik * fences within the timeline of the i915_active_fence is understood, it
1025*a337b64fSJanusz Krzysztofik * should be locked by the caller.
1026b1e3177bSChris Wilson */
1027b1e3177bSChris Wilson struct dma_fence *
__i915_active_fence_set(struct i915_active_fence * active,struct dma_fence * fence)1028b1e3177bSChris Wilson __i915_active_fence_set(struct i915_active_fence *active,
1029b1e3177bSChris Wilson struct dma_fence *fence)
1030b1e3177bSChris Wilson {
1031b1e3177bSChris Wilson struct dma_fence *prev;
1032b1e3177bSChris Wilson unsigned long flags;
1033b1e3177bSChris Wilson
1034*a337b64fSJanusz Krzysztofik /*
1035*a337b64fSJanusz Krzysztofik * In case of fences embedded in i915_requests, their memory is
1036*a337b64fSJanusz Krzysztofik * SLAB_FAILSAFE_BY_RCU, then it can be reused right after release
1037*a337b64fSJanusz Krzysztofik * by new requests. Then, there is a risk of passing back a pointer
1038*a337b64fSJanusz Krzysztofik * to a new, completely unrelated fence that reuses the same memory
1039*a337b64fSJanusz Krzysztofik * while tracked under a different active tracker. Combined with i915
1040*a337b64fSJanusz Krzysztofik * perf open/close operations that build await dependencies between
1041*a337b64fSJanusz Krzysztofik * engine kernel context requests and user requests from different
1042*a337b64fSJanusz Krzysztofik * timelines, this can lead to dependency loops and infinite waits.
1043*a337b64fSJanusz Krzysztofik *
1044*a337b64fSJanusz Krzysztofik * As a countermeasure, we try to get a reference to the active->fence
1045*a337b64fSJanusz Krzysztofik * first, so if we succeed and pass it back to our user then it is not
1046*a337b64fSJanusz Krzysztofik * released and potentially reused by an unrelated request before the
1047*a337b64fSJanusz Krzysztofik * user has a chance to set up an await dependency on it.
1048*a337b64fSJanusz Krzysztofik */
1049*a337b64fSJanusz Krzysztofik prev = i915_active_fence_get(active);
1050*a337b64fSJanusz Krzysztofik if (fence == prev)
1051df9f85d8SChris Wilson return fence;
1052df9f85d8SChris Wilson
1053b1e3177bSChris Wilson GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags));
1054b1e3177bSChris Wilson
1055df9f85d8SChris Wilson /*
1056df9f85d8SChris Wilson * Consider that we have two threads arriving (A and B), with
1057df9f85d8SChris Wilson * C already resident as the active->fence.
1058df9f85d8SChris Wilson *
1059*a337b64fSJanusz Krzysztofik * Both A and B have got a reference to C or NULL, depending on the
1060*a337b64fSJanusz Krzysztofik * timing of the interrupt handler. Let's assume that if A has got C
1061*a337b64fSJanusz Krzysztofik * then it has locked C first (before B).
1062df9f85d8SChris Wilson *
1063df9f85d8SChris Wilson * Note the strong ordering of the timeline also provides consistent
1064df9f85d8SChris Wilson * nesting rules for the fence->lock; the inner lock is always the
1065df9f85d8SChris Wilson * older lock.
1066df9f85d8SChris Wilson */
1067df9f85d8SChris Wilson spin_lock_irqsave(fence->lock, flags);
1068*a337b64fSJanusz Krzysztofik if (prev)
1069b1e3177bSChris Wilson spin_lock_nested(prev->lock, SINGLE_DEPTH_NESTING);
1070*a337b64fSJanusz Krzysztofik
1071*a337b64fSJanusz Krzysztofik /*
1072*a337b64fSJanusz Krzysztofik * A does the cmpxchg first, and so it sees C or NULL, as before, or
1073*a337b64fSJanusz Krzysztofik * something else, depending on the timing of other threads and/or
1074*a337b64fSJanusz Krzysztofik * interrupt handler. If not the same as before then A unlocks C if
1075*a337b64fSJanusz Krzysztofik * applicable and retries, starting from an attempt to get a new
1076*a337b64fSJanusz Krzysztofik * active->fence. Meanwhile, B follows the same path as A.
1077*a337b64fSJanusz Krzysztofik * Once A succeeds with cmpxch, B fails again, retires, gets A from
1078*a337b64fSJanusz Krzysztofik * active->fence, locks it as soon as A completes, and possibly
1079*a337b64fSJanusz Krzysztofik * succeeds with cmpxchg.
1080*a337b64fSJanusz Krzysztofik */
1081*a337b64fSJanusz Krzysztofik while (cmpxchg(__active_fence_slot(active), prev, fence) != prev) {
1082*a337b64fSJanusz Krzysztofik if (prev) {
1083*a337b64fSJanusz Krzysztofik spin_unlock(prev->lock);
1084*a337b64fSJanusz Krzysztofik dma_fence_put(prev);
1085*a337b64fSJanusz Krzysztofik }
1086*a337b64fSJanusz Krzysztofik spin_unlock_irqrestore(fence->lock, flags);
1087*a337b64fSJanusz Krzysztofik
1088*a337b64fSJanusz Krzysztofik prev = i915_active_fence_get(active);
1089*a337b64fSJanusz Krzysztofik GEM_BUG_ON(prev == fence);
1090*a337b64fSJanusz Krzysztofik
1091*a337b64fSJanusz Krzysztofik spin_lock_irqsave(fence->lock, flags);
1092*a337b64fSJanusz Krzysztofik if (prev)
1093*a337b64fSJanusz Krzysztofik spin_lock_nested(prev->lock, SINGLE_DEPTH_NESTING);
1094*a337b64fSJanusz Krzysztofik }
1095*a337b64fSJanusz Krzysztofik
1096*a337b64fSJanusz Krzysztofik /*
1097*a337b64fSJanusz Krzysztofik * If prev is NULL then the previous fence must have been signaled
1098*a337b64fSJanusz Krzysztofik * and we know that we are first on the timeline. If it is still
1099*a337b64fSJanusz Krzysztofik * present then, having the lock on that fence already acquired, we
1100*a337b64fSJanusz Krzysztofik * serialise with the interrupt handler, in the process of removing it
1101*a337b64fSJanusz Krzysztofik * from any future interrupt callback. A will then wait on C before
1102*a337b64fSJanusz Krzysztofik * executing (if present).
1103*a337b64fSJanusz Krzysztofik *
1104*a337b64fSJanusz Krzysztofik * As B is second, it sees A as the previous fence and so waits for
1105*a337b64fSJanusz Krzysztofik * it to complete its transition and takes over the occupancy for
1106*a337b64fSJanusz Krzysztofik * itself -- remembering that it needs to wait on A before executing.
1107*a337b64fSJanusz Krzysztofik */
1108*a337b64fSJanusz Krzysztofik if (prev) {
1109b1e3177bSChris Wilson __list_del_entry(&active->cb.node);
1110b1e3177bSChris Wilson spin_unlock(prev->lock); /* serialise with prev->cb_list */
1111b1e3177bSChris Wilson }
1112b1e3177bSChris Wilson list_add_tail(&active->cb.node, &fence->cb_list);
1113b1e3177bSChris Wilson spin_unlock_irqrestore(fence->lock, flags);
1114b1e3177bSChris Wilson
1115b1e3177bSChris Wilson return prev;
1116b1e3177bSChris Wilson }
1117b1e3177bSChris Wilson
i915_active_fence_set(struct i915_active_fence * active,struct i915_request * rq)1118b1e3177bSChris Wilson int i915_active_fence_set(struct i915_active_fence *active,
111921950ee7SChris Wilson struct i915_request *rq)
112021950ee7SChris Wilson {
1121b1e3177bSChris Wilson struct dma_fence *fence;
1122b1e3177bSChris Wilson int err = 0;
112321950ee7SChris Wilson
1124b1e3177bSChris Wilson /* Must maintain timeline ordering wrt previous active requests */
1125b1e3177bSChris Wilson fence = __i915_active_fence_set(active, &rq->fence);
1126b1e3177bSChris Wilson if (fence) {
1127b1e3177bSChris Wilson err = i915_request_await_dma_fence(rq, fence);
1128b1e3177bSChris Wilson dma_fence_put(fence);
112921950ee7SChris Wilson }
113021950ee7SChris Wilson
1131b1e3177bSChris Wilson return err;
1132b1e3177bSChris Wilson }
1133b1e3177bSChris Wilson
i915_active_noop(struct dma_fence * fence,struct dma_fence_cb * cb)1134b1e3177bSChris Wilson void i915_active_noop(struct dma_fence *fence, struct dma_fence_cb *cb)
113521950ee7SChris Wilson {
1136df9f85d8SChris Wilson active_fence_cb(fence, cb);
113721950ee7SChris Wilson }
113821950ee7SChris Wilson
1139229007e0SChris Wilson struct auto_active {
1140229007e0SChris Wilson struct i915_active base;
1141229007e0SChris Wilson struct kref ref;
1142229007e0SChris Wilson };
1143229007e0SChris Wilson
i915_active_get(struct i915_active * ref)1144229007e0SChris Wilson struct i915_active *i915_active_get(struct i915_active *ref)
1145229007e0SChris Wilson {
1146229007e0SChris Wilson struct auto_active *aa = container_of(ref, typeof(*aa), base);
1147229007e0SChris Wilson
1148229007e0SChris Wilson kref_get(&aa->ref);
1149229007e0SChris Wilson return &aa->base;
1150229007e0SChris Wilson }
1151229007e0SChris Wilson
auto_release(struct kref * ref)1152229007e0SChris Wilson static void auto_release(struct kref *ref)
1153229007e0SChris Wilson {
1154229007e0SChris Wilson struct auto_active *aa = container_of(ref, typeof(*aa), ref);
1155229007e0SChris Wilson
1156229007e0SChris Wilson i915_active_fini(&aa->base);
1157229007e0SChris Wilson kfree(aa);
1158229007e0SChris Wilson }
1159229007e0SChris Wilson
i915_active_put(struct i915_active * ref)1160229007e0SChris Wilson void i915_active_put(struct i915_active *ref)
1161229007e0SChris Wilson {
1162229007e0SChris Wilson struct auto_active *aa = container_of(ref, typeof(*aa), base);
1163229007e0SChris Wilson
1164229007e0SChris Wilson kref_put(&aa->ref, auto_release);
1165229007e0SChris Wilson }
1166229007e0SChris Wilson
auto_active(struct i915_active * ref)1167229007e0SChris Wilson static int auto_active(struct i915_active *ref)
1168229007e0SChris Wilson {
1169229007e0SChris Wilson i915_active_get(ref);
1170229007e0SChris Wilson return 0;
1171229007e0SChris Wilson }
1172229007e0SChris Wilson
auto_retire(struct i915_active * ref)1173c3b14760SMatthew Auld static void auto_retire(struct i915_active *ref)
1174229007e0SChris Wilson {
1175229007e0SChris Wilson i915_active_put(ref);
1176229007e0SChris Wilson }
1177229007e0SChris Wilson
i915_active_create(void)1178229007e0SChris Wilson struct i915_active *i915_active_create(void)
1179229007e0SChris Wilson {
1180229007e0SChris Wilson struct auto_active *aa;
1181229007e0SChris Wilson
1182229007e0SChris Wilson aa = kmalloc(sizeof(*aa), GFP_KERNEL);
1183229007e0SChris Wilson if (!aa)
1184229007e0SChris Wilson return NULL;
1185229007e0SChris Wilson
1186229007e0SChris Wilson kref_init(&aa->ref);
1187c3b14760SMatthew Auld i915_active_init(&aa->base, auto_active, auto_retire, 0);
1188229007e0SChris Wilson
1189229007e0SChris Wilson return &aa->base;
1190229007e0SChris Wilson }
1191229007e0SChris Wilson
119264d6c500SChris Wilson #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
119364d6c500SChris Wilson #include "selftests/i915_active.c"
119464d6c500SChris Wilson #endif
11955f5c139dSChris Wilson
i915_active_module_exit(void)1196512ba03eSDaniel Vetter void i915_active_module_exit(void)
1197103b76eeSChris Wilson {
1198512ba03eSDaniel Vetter kmem_cache_destroy(slab_cache);
1199103b76eeSChris Wilson }
1200103b76eeSChris Wilson
i915_active_module_init(void)1201512ba03eSDaniel Vetter int __init i915_active_module_init(void)
12025f5c139dSChris Wilson {
1203512ba03eSDaniel Vetter slab_cache = KMEM_CACHE(active_node, SLAB_HWCACHE_ALIGN);
1204512ba03eSDaniel Vetter if (!slab_cache)
12055f5c139dSChris Wilson return -ENOMEM;
12065f5c139dSChris Wilson
12075f5c139dSChris Wilson return 0;
12085f5c139dSChris Wilson }
1209