Home | History | Annotate | Line # | Download | only in i915
i915_active.c revision 1.1
      1 /*	$NetBSD: i915_active.c,v 1.1 2021/12/18 20:15:24 riastradh Exp $	*/
      2 
      3 /*
      4  * SPDX-License-Identifier: MIT
      5  *
      6  * Copyright  2019 Intel Corporation
      7  */
      8 
      9 #include <sys/cdefs.h>
     10 __KERNEL_RCSID(0, "$NetBSD: i915_active.c,v 1.1 2021/12/18 20:15:24 riastradh Exp $");
     11 
     12 #include <linux/debugobjects.h>
     13 
     14 #include "gt/intel_context.h"
     15 #include "gt/intel_engine_pm.h"
     16 #include "gt/intel_ring.h"
     17 
     18 #include "i915_drv.h"
     19 #include "i915_active.h"
     20 #include "i915_globals.h"
     21 
     22 /*
     23  * Active refs memory management
     24  *
     25  * To be more economical with memory, we reap all the i915_active trees as
     26  * they idle (when we know the active requests are inactive) and allocate the
     27  * nodes from a local slab cache to hopefully reduce the fragmentation.
     28  */
     29 static struct i915_global_active {
     30 	struct i915_global base;
     31 	struct kmem_cache *slab_cache;
     32 } global;
     33 
     34 struct active_node {
     35 	struct i915_active_fence base;
     36 	struct i915_active *ref;
     37 	struct rb_node node;
     38 	u64 timeline;
     39 };
     40 
     41 static inline struct active_node *
     42 node_from_active(struct i915_active_fence *active)
     43 {
     44 	return container_of(active, struct active_node, base);
     45 }
     46 
     47 #define take_preallocated_barriers(x) llist_del_all(&(x)->preallocated_barriers)
     48 
     49 static inline bool is_barrier(const struct i915_active_fence *active)
     50 {
     51 	return IS_ERR(rcu_access_pointer(active->fence));
     52 }
     53 
     54 static inline struct llist_node *barrier_to_ll(struct active_node *node)
     55 {
     56 	GEM_BUG_ON(!is_barrier(&node->base));
     57 	return (struct llist_node *)&node->base.cb.node;
     58 }
     59 
     60 static inline struct intel_engine_cs *
     61 __barrier_to_engine(struct active_node *node)
     62 {
     63 	return (struct intel_engine_cs *)READ_ONCE(node->base.cb.node.prev);
     64 }
     65 
     66 static inline struct intel_engine_cs *
     67 barrier_to_engine(struct active_node *node)
     68 {
     69 	GEM_BUG_ON(!is_barrier(&node->base));
     70 	return __barrier_to_engine(node);
     71 }
     72 
     73 static inline struct active_node *barrier_from_ll(struct llist_node *x)
     74 {
     75 	return container_of((struct list_head *)x,
     76 			    struct active_node, base.cb.node);
     77 }
     78 
     79 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM) && IS_ENABLED(CONFIG_DEBUG_OBJECTS)
     80 
     81 static void *active_debug_hint(void *addr)
     82 {
     83 	struct i915_active *ref = addr;
     84 
     85 	return (void *)ref->active ?: (void *)ref->retire ?: (void *)ref;
     86 }
     87 
     88 static struct debug_obj_descr active_debug_desc = {
     89 	.name = "i915_active",
     90 	.debug_hint = active_debug_hint,
     91 };
     92 
     93 static void debug_active_init(struct i915_active *ref)
     94 {
     95 	debug_object_init(ref, &active_debug_desc);
     96 }
     97 
     98 static void debug_active_activate(struct i915_active *ref)
     99 {
    100 	lockdep_assert_held(&ref->tree_lock);
    101 	if (!atomic_read(&ref->count)) /* before the first inc */
    102 		debug_object_activate(ref, &active_debug_desc);
    103 }
    104 
    105 static void debug_active_deactivate(struct i915_active *ref)
    106 {
    107 	lockdep_assert_held(&ref->tree_lock);
    108 	if (!atomic_read(&ref->count)) /* after the last dec */
    109 		debug_object_deactivate(ref, &active_debug_desc);
    110 }
    111 
    112 static void debug_active_fini(struct i915_active *ref)
    113 {
    114 	debug_object_free(ref, &active_debug_desc);
    115 }
    116 
    117 static void debug_active_assert(struct i915_active *ref)
    118 {
    119 	debug_object_assert_init(ref, &active_debug_desc);
    120 }
    121 
    122 #else
    123 
    124 static inline void debug_active_init(struct i915_active *ref) { }
    125 static inline void debug_active_activate(struct i915_active *ref) { }
    126 static inline void debug_active_deactivate(struct i915_active *ref) { }
    127 static inline void debug_active_fini(struct i915_active *ref) { }
    128 static inline void debug_active_assert(struct i915_active *ref) { }
    129 
    130 #endif
    131 
    132 static void
    133 __active_retire(struct i915_active *ref)
    134 {
    135 	struct active_node *it, *n;
    136 	struct rb_root root;
    137 	unsigned long flags;
    138 
    139 	GEM_BUG_ON(i915_active_is_idle(ref));
    140 
    141 	/* return the unused nodes to our slabcache -- flushing the allocator */
    142 	if (!atomic_dec_and_lock_irqsave(&ref->count, &ref->tree_lock, flags))
    143 		return;
    144 
    145 	GEM_BUG_ON(rcu_access_pointer(ref->excl.fence));
    146 	debug_active_deactivate(ref);
    147 
    148 	root = ref->tree;
    149 	ref->tree = RB_ROOT;
    150 	ref->cache = NULL;
    151 
    152 	spin_unlock_irqrestore(&ref->tree_lock, flags);
    153 
    154 	/* After the final retire, the entire struct may be freed */
    155 	if (ref->retire)
    156 		ref->retire(ref);
    157 
    158 	/* ... except if you wait on it, you must manage your own references! */
    159 	wake_up_var(ref);
    160 
    161 	rbtree_postorder_for_each_entry_safe(it, n, &root, node) {
    162 		GEM_BUG_ON(i915_active_fence_isset(&it->base));
    163 		kmem_cache_free(global.slab_cache, it);
    164 	}
    165 }
    166 
    167 static void
    168 active_work(struct work_struct *wrk)
    169 {
    170 	struct i915_active *ref = container_of(wrk, typeof(*ref), work);
    171 
    172 	GEM_BUG_ON(!atomic_read(&ref->count));
    173 	if (atomic_add_unless(&ref->count, -1, 1))
    174 		return;
    175 
    176 	__active_retire(ref);
    177 }
    178 
    179 static void
    180 active_retire(struct i915_active *ref)
    181 {
    182 	GEM_BUG_ON(!atomic_read(&ref->count));
    183 	if (atomic_add_unless(&ref->count, -1, 1))
    184 		return;
    185 
    186 	if (ref->flags & I915_ACTIVE_RETIRE_SLEEPS) {
    187 		queue_work(system_unbound_wq, &ref->work);
    188 		return;
    189 	}
    190 
    191 	__active_retire(ref);
    192 }
    193 
    194 static inline struct dma_fence **
    195 __active_fence_slot(struct i915_active_fence *active)
    196 {
    197 	return (struct dma_fence ** __force)&active->fence;
    198 }
    199 
    200 static inline bool
    201 active_fence_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
    202 {
    203 	struct i915_active_fence *active =
    204 		container_of(cb, typeof(*active), cb);
    205 
    206 	return cmpxchg(__active_fence_slot(active), fence, NULL) == fence;
    207 }
    208 
    209 static void
    210 node_retire(struct dma_fence *fence, struct dma_fence_cb *cb)
    211 {
    212 	if (active_fence_cb(fence, cb))
    213 		active_retire(container_of(cb, struct active_node, base.cb)->ref);
    214 }
    215 
    216 static void
    217 excl_retire(struct dma_fence *fence, struct dma_fence_cb *cb)
    218 {
    219 	if (active_fence_cb(fence, cb))
    220 		active_retire(container_of(cb, struct i915_active, excl.cb));
    221 }
    222 
    223 static struct i915_active_fence *
    224 active_instance(struct i915_active *ref, struct intel_timeline *tl)
    225 {
    226 	struct active_node *node, *prealloc;
    227 	struct rb_node **p, *parent;
    228 	u64 idx = tl->fence_context;
    229 
    230 	/*
    231 	 * We track the most recently used timeline to skip a rbtree search
    232 	 * for the common case, under typical loads we never need the rbtree
    233 	 * at all. We can reuse the last slot if it is empty, that is
    234 	 * after the previous activity has been retired, or if it matches the
    235 	 * current timeline.
    236 	 */
    237 	node = READ_ONCE(ref->cache);
    238 	if (node && node->timeline == idx)
    239 		return &node->base;
    240 
    241 	/* Preallocate a replacement, just in case */
    242 	prealloc = kmem_cache_alloc(global.slab_cache, GFP_KERNEL);
    243 	if (!prealloc)
    244 		return NULL;
    245 
    246 	spin_lock_irq(&ref->tree_lock);
    247 	GEM_BUG_ON(i915_active_is_idle(ref));
    248 
    249 	parent = NULL;
    250 	p = &ref->tree.rb_node;
    251 	while (*p) {
    252 		parent = *p;
    253 
    254 		node = rb_entry(parent, struct active_node, node);
    255 		if (node->timeline == idx) {
    256 			kmem_cache_free(global.slab_cache, prealloc);
    257 			goto out;
    258 		}
    259 
    260 		if (node->timeline < idx)
    261 			p = &parent->rb_right;
    262 		else
    263 			p = &parent->rb_left;
    264 	}
    265 
    266 	node = prealloc;
    267 	__i915_active_fence_init(&node->base, NULL, node_retire);
    268 	node->ref = ref;
    269 	node->timeline = idx;
    270 
    271 	rb_link_node(&node->node, parent, p);
    272 	rb_insert_color(&node->node, &ref->tree);
    273 
    274 out:
    275 	ref->cache = node;
    276 	spin_unlock_irq(&ref->tree_lock);
    277 
    278 	BUILD_BUG_ON(offsetof(typeof(*node), base));
    279 	return &node->base;
    280 }
    281 
    282 void __i915_active_init(struct i915_active *ref,
    283 			int (*active)(struct i915_active *ref),
    284 			void (*retire)(struct i915_active *ref),
    285 			struct lock_class_key *mkey,
    286 			struct lock_class_key *wkey)
    287 {
    288 	unsigned long bits;
    289 
    290 	debug_active_init(ref);
    291 
    292 	ref->flags = 0;
    293 	ref->active = active;
    294 	ref->retire = ptr_unpack_bits(retire, &bits, 2);
    295 	if (bits & I915_ACTIVE_MAY_SLEEP)
    296 		ref->flags |= I915_ACTIVE_RETIRE_SLEEPS;
    297 
    298 	spin_lock_init(&ref->tree_lock);
    299 	ref->tree = RB_ROOT;
    300 	ref->cache = NULL;
    301 
    302 	init_llist_head(&ref->preallocated_barriers);
    303 	atomic_set(&ref->count, 0);
    304 	__mutex_init(&ref->mutex, "i915_active", mkey);
    305 	__i915_active_fence_init(&ref->excl, NULL, excl_retire);
    306 	INIT_WORK(&ref->work, active_work);
    307 #if IS_ENABLED(CONFIG_LOCKDEP)
    308 	lockdep_init_map(&ref->work.lockdep_map, "i915_active.work", wkey, 0);
    309 #endif
    310 }
    311 
    312 static bool ____active_del_barrier(struct i915_active *ref,
    313 				   struct active_node *node,
    314 				   struct intel_engine_cs *engine)
    315 
    316 {
    317 	struct llist_node *head = NULL, *tail = NULL;
    318 	struct llist_node *pos, *next;
    319 
    320 	GEM_BUG_ON(node->timeline != engine->kernel_context->timeline->fence_context);
    321 
    322 	/*
    323 	 * Rebuild the llist excluding our node. We may perform this
    324 	 * outside of the kernel_context timeline mutex and so someone
    325 	 * else may be manipulating the engine->barrier_tasks, in
    326 	 * which case either we or they will be upset :)
    327 	 *
    328 	 * A second __active_del_barrier() will report failure to claim
    329 	 * the active_node and the caller will just shrug and know not to
    330 	 * claim ownership of its node.
    331 	 *
    332 	 * A concurrent i915_request_add_active_barriers() will miss adding
    333 	 * any of the tasks, but we will try again on the next -- and since
    334 	 * we are actively using the barrier, we know that there will be
    335 	 * at least another opportunity when we idle.
    336 	 */
    337 	llist_for_each_safe(pos, next, llist_del_all(&engine->barrier_tasks)) {
    338 		if (node == barrier_from_ll(pos)) {
    339 			node = NULL;
    340 			continue;
    341 		}
    342 
    343 		pos->next = head;
    344 		head = pos;
    345 		if (!tail)
    346 			tail = pos;
    347 	}
    348 	if (head)
    349 		llist_add_batch(head, tail, &engine->barrier_tasks);
    350 
    351 	return !node;
    352 }
    353 
    354 static bool
    355 __active_del_barrier(struct i915_active *ref, struct active_node *node)
    356 {
    357 	return ____active_del_barrier(ref, node, barrier_to_engine(node));
    358 }
    359 
    360 int i915_active_ref(struct i915_active *ref,
    361 		    struct intel_timeline *tl,
    362 		    struct dma_fence *fence)
    363 {
    364 	struct i915_active_fence *active;
    365 	int err;
    366 
    367 	lockdep_assert_held(&tl->mutex);
    368 
    369 	/* Prevent reaping in case we malloc/wait while building the tree */
    370 	err = i915_active_acquire(ref);
    371 	if (err)
    372 		return err;
    373 
    374 	active = active_instance(ref, tl);
    375 	if (!active) {
    376 		err = -ENOMEM;
    377 		goto out;
    378 	}
    379 
    380 	if (is_barrier(active)) { /* proto-node used by our idle barrier */
    381 		/*
    382 		 * This request is on the kernel_context timeline, and so
    383 		 * we can use it to substitute for the pending idle-barrer
    384 		 * request that we want to emit on the kernel_context.
    385 		 */
    386 		__active_del_barrier(ref, node_from_active(active));
    387 		RCU_INIT_POINTER(active->fence, NULL);
    388 		atomic_dec(&ref->count);
    389 	}
    390 	if (!__i915_active_fence_set(active, fence))
    391 		atomic_inc(&ref->count);
    392 
    393 out:
    394 	i915_active_release(ref);
    395 	return err;
    396 }
    397 
    398 void i915_active_set_exclusive(struct i915_active *ref, struct dma_fence *f)
    399 {
    400 	/* We expect the caller to manage the exclusive timeline ordering */
    401 	GEM_BUG_ON(i915_active_is_idle(ref));
    402 
    403 	if (!__i915_active_fence_set(&ref->excl, f))
    404 		atomic_inc(&ref->count);
    405 }
    406 
    407 bool i915_active_acquire_if_busy(struct i915_active *ref)
    408 {
    409 	debug_active_assert(ref);
    410 	return atomic_add_unless(&ref->count, 1, 0);
    411 }
    412 
    413 int i915_active_acquire(struct i915_active *ref)
    414 {
    415 	int err;
    416 
    417 	if (i915_active_acquire_if_busy(ref))
    418 		return 0;
    419 
    420 	err = mutex_lock_interruptible(&ref->mutex);
    421 	if (err)
    422 		return err;
    423 
    424 	if (likely(!i915_active_acquire_if_busy(ref))) {
    425 		if (ref->active)
    426 			err = ref->active(ref);
    427 		if (!err) {
    428 			spin_lock_irq(&ref->tree_lock); /* __active_retire() */
    429 			debug_active_activate(ref);
    430 			atomic_inc(&ref->count);
    431 			spin_unlock_irq(&ref->tree_lock);
    432 		}
    433 	}
    434 
    435 	mutex_unlock(&ref->mutex);
    436 
    437 	return err;
    438 }
    439 
    440 void i915_active_release(struct i915_active *ref)
    441 {
    442 	debug_active_assert(ref);
    443 	active_retire(ref);
    444 }
    445 
    446 static void enable_signaling(struct i915_active_fence *active)
    447 {
    448 	struct dma_fence *fence;
    449 
    450 	fence = i915_active_fence_get(active);
    451 	if (!fence)
    452 		return;
    453 
    454 	dma_fence_enable_sw_signaling(fence);
    455 	dma_fence_put(fence);
    456 }
    457 
    458 int i915_active_wait(struct i915_active *ref)
    459 {
    460 	struct active_node *it, *n;
    461 	int err = 0;
    462 
    463 	might_sleep();
    464 
    465 	if (!i915_active_acquire_if_busy(ref))
    466 		return 0;
    467 
    468 	/* Flush lazy signals */
    469 	enable_signaling(&ref->excl);
    470 	rbtree_postorder_for_each_entry_safe(it, n, &ref->tree, node) {
    471 		if (is_barrier(&it->base)) /* unconnected idle barrier */
    472 			continue;
    473 
    474 		enable_signaling(&it->base);
    475 	}
    476 	/* Any fence added after the wait begins will not be auto-signaled */
    477 
    478 	i915_active_release(ref);
    479 	if (err)
    480 		return err;
    481 
    482 	if (wait_var_event_interruptible(ref, i915_active_is_idle(ref)))
    483 		return -EINTR;
    484 
    485 	flush_work(&ref->work);
    486 	return 0;
    487 }
    488 
    489 int i915_request_await_active(struct i915_request *rq, struct i915_active *ref)
    490 {
    491 	int err = 0;
    492 
    493 	if (rcu_access_pointer(ref->excl.fence)) {
    494 		struct dma_fence *fence;
    495 
    496 		rcu_read_lock();
    497 		fence = dma_fence_get_rcu_safe(&ref->excl.fence);
    498 		rcu_read_unlock();
    499 		if (fence) {
    500 			err = i915_request_await_dma_fence(rq, fence);
    501 			dma_fence_put(fence);
    502 		}
    503 	}
    504 
    505 	/* In the future we may choose to await on all fences */
    506 
    507 	return err;
    508 }
    509 
    510 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)
    511 void i915_active_fini(struct i915_active *ref)
    512 {
    513 	debug_active_fini(ref);
    514 	GEM_BUG_ON(atomic_read(&ref->count));
    515 	GEM_BUG_ON(work_pending(&ref->work));
    516 	GEM_BUG_ON(!RB_EMPTY_ROOT(&ref->tree));
    517 	mutex_destroy(&ref->mutex);
    518 }
    519 #endif
    520 
    521 static inline bool is_idle_barrier(struct active_node *node, u64 idx)
    522 {
    523 	return node->timeline == idx && !i915_active_fence_isset(&node->base);
    524 }
    525 
    526 static struct active_node *reuse_idle_barrier(struct i915_active *ref, u64 idx)
    527 {
    528 	struct rb_node *prev, *p;
    529 
    530 	if (RB_EMPTY_ROOT(&ref->tree))
    531 		return NULL;
    532 
    533 	spin_lock_irq(&ref->tree_lock);
    534 	GEM_BUG_ON(i915_active_is_idle(ref));
    535 
    536 	/*
    537 	 * Try to reuse any existing barrier nodes already allocated for this
    538 	 * i915_active, due to overlapping active phases there is likely a
    539 	 * node kept alive (as we reuse before parking). We prefer to reuse
    540 	 * completely idle barriers (less hassle in manipulating the llists),
    541 	 * but otherwise any will do.
    542 	 */
    543 	if (ref->cache && is_idle_barrier(ref->cache, idx)) {
    544 		p = &ref->cache->node;
    545 		goto match;
    546 	}
    547 
    548 	prev = NULL;
    549 	p = ref->tree.rb_node;
    550 	while (p) {
    551 		struct active_node *node =
    552 			rb_entry(p, struct active_node, node);
    553 
    554 		if (is_idle_barrier(node, idx))
    555 			goto match;
    556 
    557 		prev = p;
    558 		if (node->timeline < idx)
    559 			p = p->rb_right;
    560 		else
    561 			p = p->rb_left;
    562 	}
    563 
    564 	/*
    565 	 * No quick match, but we did find the leftmost rb_node for the
    566 	 * kernel_context. Walk the rb_tree in-order to see if there were
    567 	 * any idle-barriers on this timeline that we missed, or just use
    568 	 * the first pending barrier.
    569 	 */
    570 	for (p = prev; p; p = rb_next(p)) {
    571 		struct active_node *node =
    572 			rb_entry(p, struct active_node, node);
    573 		struct intel_engine_cs *engine;
    574 
    575 		if (node->timeline > idx)
    576 			break;
    577 
    578 		if (node->timeline < idx)
    579 			continue;
    580 
    581 		if (is_idle_barrier(node, idx))
    582 			goto match;
    583 
    584 		/*
    585 		 * The list of pending barriers is protected by the
    586 		 * kernel_context timeline, which notably we do not hold
    587 		 * here. i915_request_add_active_barriers() may consume
    588 		 * the barrier before we claim it, so we have to check
    589 		 * for success.
    590 		 */
    591 		engine = __barrier_to_engine(node);
    592 		smp_rmb(); /* serialise with add_active_barriers */
    593 		if (is_barrier(&node->base) &&
    594 		    ____active_del_barrier(ref, node, engine))
    595 			goto match;
    596 	}
    597 
    598 	spin_unlock_irq(&ref->tree_lock);
    599 
    600 	return NULL;
    601 
    602 match:
    603 	rb_erase(p, &ref->tree); /* Hide from waits and sibling allocations */
    604 	if (p == &ref->cache->node)
    605 		ref->cache = NULL;
    606 	spin_unlock_irq(&ref->tree_lock);
    607 
    608 	return rb_entry(p, struct active_node, node);
    609 }
    610 
    611 int i915_active_acquire_preallocate_barrier(struct i915_active *ref,
    612 					    struct intel_engine_cs *engine)
    613 {
    614 	intel_engine_mask_t tmp, mask = engine->mask;
    615 	struct llist_node *first = NULL, *last = NULL;
    616 	struct intel_gt *gt = engine->gt;
    617 	int err;
    618 
    619 	GEM_BUG_ON(i915_active_is_idle(ref));
    620 
    621 	/* Wait until the previous preallocation is completed */
    622 	while (!llist_empty(&ref->preallocated_barriers))
    623 		cond_resched();
    624 
    625 	/*
    626 	 * Preallocate a node for each physical engine supporting the target
    627 	 * engine (remember virtual engines have more than one sibling).
    628 	 * We can then use the preallocated nodes in
    629 	 * i915_active_acquire_barrier()
    630 	 */
    631 	for_each_engine_masked(engine, gt, mask, tmp) {
    632 		u64 idx = engine->kernel_context->timeline->fence_context;
    633 		struct llist_node *prev = first;
    634 		struct active_node *node;
    635 
    636 		node = reuse_idle_barrier(ref, idx);
    637 		if (!node) {
    638 			node = kmem_cache_alloc(global.slab_cache, GFP_KERNEL);
    639 			if (!node) {
    640 				err = ENOMEM;
    641 				goto unwind;
    642 			}
    643 
    644 			RCU_INIT_POINTER(node->base.fence, NULL);
    645 			node->base.cb.func = node_retire;
    646 			node->timeline = idx;
    647 			node->ref = ref;
    648 		}
    649 
    650 		if (!i915_active_fence_isset(&node->base)) {
    651 			/*
    652 			 * Mark this as being *our* unconnected proto-node.
    653 			 *
    654 			 * Since this node is not in any list, and we have
    655 			 * decoupled it from the rbtree, we can reuse the
    656 			 * request to indicate this is an idle-barrier node
    657 			 * and then we can use the rb_node and list pointers
    658 			 * for our tracking of the pending barrier.
    659 			 */
    660 			RCU_INIT_POINTER(node->base.fence, ERR_PTR(-EAGAIN));
    661 			node->base.cb.node.prev = (void *)engine;
    662 			atomic_inc(&ref->count);
    663 		}
    664 		GEM_BUG_ON(rcu_access_pointer(node->base.fence) != ERR_PTR(-EAGAIN));
    665 
    666 		GEM_BUG_ON(barrier_to_engine(node) != engine);
    667 		first = barrier_to_ll(node);
    668 		first->next = prev;
    669 		if (!last)
    670 			last = first;
    671 		intel_engine_pm_get(engine);
    672 	}
    673 
    674 	GEM_BUG_ON(!llist_empty(&ref->preallocated_barriers));
    675 	llist_add_batch(first, last, &ref->preallocated_barriers);
    676 
    677 	return 0;
    678 
    679 unwind:
    680 	while (first) {
    681 		struct active_node *node = barrier_from_ll(first);
    682 
    683 		first = first->next;
    684 
    685 		atomic_dec(&ref->count);
    686 		intel_engine_pm_put(barrier_to_engine(node));
    687 
    688 		kmem_cache_free(global.slab_cache, node);
    689 	}
    690 	return err;
    691 }
    692 
    693 void i915_active_acquire_barrier(struct i915_active *ref)
    694 {
    695 	struct llist_node *pos, *next;
    696 	unsigned long flags;
    697 
    698 	GEM_BUG_ON(i915_active_is_idle(ref));
    699 
    700 	/*
    701 	 * Transfer the list of preallocated barriers into the
    702 	 * i915_active rbtree, but only as proto-nodes. They will be
    703 	 * populated by i915_request_add_active_barriers() to point to the
    704 	 * request that will eventually release them.
    705 	 */
    706 	llist_for_each_safe(pos, next, take_preallocated_barriers(ref)) {
    707 		struct active_node *node = barrier_from_ll(pos);
    708 		struct intel_engine_cs *engine = barrier_to_engine(node);
    709 		struct rb_node **p, *parent;
    710 
    711 		spin_lock_irqsave_nested(&ref->tree_lock, flags,
    712 					 SINGLE_DEPTH_NESTING);
    713 		parent = NULL;
    714 		p = &ref->tree.rb_node;
    715 		while (*p) {
    716 			struct active_node *it;
    717 
    718 			parent = *p;
    719 
    720 			it = rb_entry(parent, struct active_node, node);
    721 			if (it->timeline < node->timeline)
    722 				p = &parent->rb_right;
    723 			else
    724 				p = &parent->rb_left;
    725 		}
    726 		rb_link_node(&node->node, parent, p);
    727 		rb_insert_color(&node->node, &ref->tree);
    728 		spin_unlock_irqrestore(&ref->tree_lock, flags);
    729 
    730 		GEM_BUG_ON(!intel_engine_pm_is_awake(engine));
    731 		llist_add(barrier_to_ll(node), &engine->barrier_tasks);
    732 		intel_engine_pm_put(engine);
    733 	}
    734 }
    735 
    736 static struct dma_fence **ll_to_fence_slot(struct llist_node *node)
    737 {
    738 	return __active_fence_slot(&barrier_from_ll(node)->base);
    739 }
    740 
    741 void i915_request_add_active_barriers(struct i915_request *rq)
    742 {
    743 	struct intel_engine_cs *engine = rq->engine;
    744 	struct llist_node *node, *next;
    745 	unsigned long flags;
    746 
    747 	GEM_BUG_ON(!intel_context_is_barrier(rq->context));
    748 	GEM_BUG_ON(intel_engine_is_virtual(engine));
    749 	GEM_BUG_ON(i915_request_timeline(rq) != engine->kernel_context->timeline);
    750 
    751 	node = llist_del_all(&engine->barrier_tasks);
    752 	if (!node)
    753 		return;
    754 	/*
    755 	 * Attach the list of proto-fences to the in-flight request such
    756 	 * that the parent i915_active will be released when this request
    757 	 * is retired.
    758 	 */
    759 	spin_lock_irqsave(&rq->lock, flags);
    760 	llist_for_each_safe(node, next, node) {
    761 		/* serialise with reuse_idle_barrier */
    762 		smp_store_mb(*ll_to_fence_slot(node), &rq->fence);
    763 		list_add_tail((struct list_head *)node, &rq->fence.cb_list);
    764 	}
    765 	spin_unlock_irqrestore(&rq->lock, flags);
    766 }
    767 
    768 /*
    769  * __i915_active_fence_set: Update the last active fence along its timeline
    770  * @active: the active tracker
    771  * @fence: the new fence (under construction)
    772  *
    773  * Records the new @fence as the last active fence along its timeline in
    774  * this active tracker, moving the tracking callbacks from the previous
    775  * fence onto this one. Returns the previous fence (if not already completed),
    776  * which the caller must ensure is executed before the new fence. To ensure
    777  * that the order of fences within the timeline of the i915_active_fence is
    778  * understood, it should be locked by the caller.
    779  */
    780 struct dma_fence *
    781 __i915_active_fence_set(struct i915_active_fence *active,
    782 			struct dma_fence *fence)
    783 {
    784 	struct dma_fence *prev;
    785 	unsigned long flags;
    786 
    787 	if (fence == rcu_access_pointer(active->fence))
    788 		return fence;
    789 
    790 	GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags));
    791 
    792 	/*
    793 	 * Consider that we have two threads arriving (A and B), with
    794 	 * C already resident as the active->fence.
    795 	 *
    796 	 * A does the xchg first, and so it sees C or NULL depending
    797 	 * on the timing of the interrupt handler. If it is NULL, the
    798 	 * previous fence must have been signaled and we know that
    799 	 * we are first on the timeline. If it is still present,
    800 	 * we acquire the lock on that fence and serialise with the interrupt
    801 	 * handler, in the process removing it from any future interrupt
    802 	 * callback. A will then wait on C before executing (if present).
    803 	 *
    804 	 * As B is second, it sees A as the previous fence and so waits for
    805 	 * it to complete its transition and takes over the occupancy for
    806 	 * itself -- remembering that it needs to wait on A before executing.
    807 	 *
    808 	 * Note the strong ordering of the timeline also provides consistent
    809 	 * nesting rules for the fence->lock; the inner lock is always the
    810 	 * older lock.
    811 	 */
    812 	spin_lock_irqsave(fence->lock, flags);
    813 	prev = xchg(__active_fence_slot(active), fence);
    814 	if (prev) {
    815 		GEM_BUG_ON(prev == fence);
    816 		spin_lock_nested(prev->lock, SINGLE_DEPTH_NESTING);
    817 		__list_del_entry(&active->cb.node);
    818 		spin_unlock(prev->lock); /* serialise with prev->cb_list */
    819 	}
    820 	GEM_BUG_ON(rcu_access_pointer(active->fence) != fence);
    821 	list_add_tail(&active->cb.node, &fence->cb_list);
    822 	spin_unlock_irqrestore(fence->lock, flags);
    823 
    824 	return prev;
    825 }
    826 
    827 int i915_active_fence_set(struct i915_active_fence *active,
    828 			  struct i915_request *rq)
    829 {
    830 	struct dma_fence *fence;
    831 	int err = 0;
    832 
    833 	/* Must maintain timeline ordering wrt previous active requests */
    834 	rcu_read_lock();
    835 	fence = __i915_active_fence_set(active, &rq->fence);
    836 	if (fence) /* but the previous fence may not belong to that timeline! */
    837 		fence = dma_fence_get_rcu(fence);
    838 	rcu_read_unlock();
    839 	if (fence) {
    840 		err = i915_request_await_dma_fence(rq, fence);
    841 		dma_fence_put(fence);
    842 	}
    843 
    844 	return err;
    845 }
    846 
    847 void i915_active_noop(struct dma_fence *fence, struct dma_fence_cb *cb)
    848 {
    849 	active_fence_cb(fence, cb);
    850 }
    851 
    852 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
    853 #include "selftests/i915_active.c"
    854 #endif
    855 
    856 static void i915_global_active_shrink(void)
    857 {
    858 	kmem_cache_shrink(global.slab_cache);
    859 }
    860 
    861 static void i915_global_active_exit(void)
    862 {
    863 	kmem_cache_destroy(global.slab_cache);
    864 }
    865 
    866 static struct i915_global_active global = { {
    867 	.shrink = i915_global_active_shrink,
    868 	.exit = i915_global_active_exit,
    869 } };
    870 
    871 int __init i915_global_active_init(void)
    872 {
    873 	global.slab_cache = KMEM_CACHE(active_node, SLAB_HWCACHE_ALIGN);
    874 	if (!global.slab_cache)
    875 		return -ENOMEM;
    876 
    877 	i915_global_register(&global.base);
    878 	return 0;
    879 }
    880