2 * SPDX-License-Identifier: MIT
4 * Copyright © 2018 Intel Corporation
7 #include <linux/mutex.h>
10 #include "i915_globals.h"
11 #include "i915_request.h"
12 #include "i915_scheduler.h"
14 static struct i915_global_scheduler {
15 struct i915_global base;
16 struct kmem_cache *slab_dependencies;
17 struct kmem_cache *slab_priorities;
20 static DEFINE_SPINLOCK(schedule_lock);
22 static const struct i915_request *
23 node_to_request(const struct i915_sched_node *node)
25 return container_of(node, const struct i915_request, sched);
28 static inline bool node_started(const struct i915_sched_node *node)
30 return i915_request_started(node_to_request(node));
33 static inline bool node_signaled(const struct i915_sched_node *node)
35 return i915_request_completed(node_to_request(node));
38 void i915_sched_node_init(struct i915_sched_node *node)
40 INIT_LIST_HEAD(&node->signalers_list);
41 INIT_LIST_HEAD(&node->waiters_list);
42 INIT_LIST_HEAD(&node->link);
43 node->attr.priority = I915_PRIORITY_INVALID;
48 static struct i915_dependency *
49 i915_dependency_alloc(void)
51 return kmem_cache_alloc(global.slab_dependencies, GFP_KERNEL);
55 i915_dependency_free(struct i915_dependency *dep)
57 kmem_cache_free(global.slab_dependencies, dep);
60 bool __i915_sched_node_add_dependency(struct i915_sched_node *node,
61 struct i915_sched_node *signal,
62 struct i915_dependency *dep,
67 spin_lock_irq(&schedule_lock);
69 if (!node_signaled(signal)) {
70 INIT_LIST_HEAD(&dep->dfs_link);
71 list_add(&dep->wait_link, &signal->waiters_list);
72 list_add(&dep->signal_link, &node->signalers_list);
73 dep->signaler = signal;
76 /* Keep track of whether anyone on this chain has a semaphore */
77 if (signal->flags & I915_SCHED_HAS_SEMAPHORE_CHAIN &&
78 !node_started(signal))
79 node->flags |= I915_SCHED_HAS_SEMAPHORE_CHAIN;
84 spin_unlock_irq(&schedule_lock);
89 int i915_sched_node_add_dependency(struct i915_sched_node *node,
90 struct i915_sched_node *signal)
92 struct i915_dependency *dep;
94 dep = i915_dependency_alloc();
98 if (!__i915_sched_node_add_dependency(node, signal, dep,
99 I915_DEPENDENCY_ALLOC))
100 i915_dependency_free(dep);
105 void i915_sched_node_fini(struct i915_sched_node *node)
107 struct i915_dependency *dep, *tmp;
109 GEM_BUG_ON(!list_empty(&node->link));
111 spin_lock_irq(&schedule_lock);
114 * Everyone we depended upon (the fences we wait to be signaled)
115 * should retire before us and remove themselves from our list.
116 * However, retirement is run independently on each timeline and
117 * so we may be called out-of-order.
119 list_for_each_entry_safe(dep, tmp, &node->signalers_list, signal_link) {
120 GEM_BUG_ON(!node_signaled(dep->signaler));
121 GEM_BUG_ON(!list_empty(&dep->dfs_link));
123 list_del(&dep->wait_link);
124 if (dep->flags & I915_DEPENDENCY_ALLOC)
125 i915_dependency_free(dep);
128 /* Remove ourselves from everyone who depends upon us */
129 list_for_each_entry_safe(dep, tmp, &node->waiters_list, wait_link) {
130 GEM_BUG_ON(dep->signaler != node);
131 GEM_BUG_ON(!list_empty(&dep->dfs_link));
133 list_del(&dep->signal_link);
134 if (dep->flags & I915_DEPENDENCY_ALLOC)
135 i915_dependency_free(dep);
138 spin_unlock_irq(&schedule_lock);
141 static inline struct i915_priolist *to_priolist(struct rb_node *rb)
143 return rb_entry(rb, struct i915_priolist, node);
146 static void assert_priolists(struct intel_engine_execlists * const execlists)
151 if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
154 GEM_BUG_ON(rb_first_cached(&execlists->queue) !=
155 rb_first(&execlists->queue.rb_root));
157 last_prio = (INT_MAX >> I915_USER_PRIORITY_SHIFT) + 1;
158 for (rb = rb_first_cached(&execlists->queue); rb; rb = rb_next(rb)) {
159 const struct i915_priolist *p = to_priolist(rb);
161 GEM_BUG_ON(p->priority >= last_prio);
162 last_prio = p->priority;
164 GEM_BUG_ON(!p->used);
165 for (i = 0; i < ARRAY_SIZE(p->requests); i++) {
166 if (list_empty(&p->requests[i]))
169 GEM_BUG_ON(!(p->used & BIT(i)));
175 i915_sched_lookup_priolist(struct intel_engine_cs *engine, int prio)
177 struct intel_engine_execlists * const execlists = &engine->execlists;
178 struct i915_priolist *p;
179 struct rb_node **parent, *rb;
183 lockdep_assert_held(&engine->timeline.lock);
184 assert_priolists(execlists);
186 /* buckets sorted from highest [in slot 0] to lowest priority */
187 idx = I915_PRIORITY_COUNT - (prio & I915_PRIORITY_MASK) - 1;
188 prio >>= I915_USER_PRIORITY_SHIFT;
189 if (unlikely(execlists->no_priolist))
190 prio = I915_PRIORITY_NORMAL;
193 /* most positive priority is scheduled first, equal priorities fifo */
195 parent = &execlists->queue.rb_root.rb_node;
199 if (prio > p->priority) {
200 parent = &rb->rb_left;
201 } else if (prio < p->priority) {
202 parent = &rb->rb_right;
209 if (prio == I915_PRIORITY_NORMAL) {
210 p = &execlists->default_priolist;
212 p = kmem_cache_alloc(global.slab_priorities, GFP_ATOMIC);
213 /* Convert an allocation failure to a priority bump */
215 prio = I915_PRIORITY_NORMAL; /* recurses just once */
217 /* To maintain ordering with all rendering, after an
218 * allocation failure we have to disable all scheduling.
219 * Requests will then be executed in fifo, and schedule
220 * will ensure that dependencies are emitted in fifo.
221 * There will be still some reordering with existing
222 * requests, so if userspace lied about their
223 * dependencies that reordering may be visible.
225 execlists->no_priolist = true;
231 for (i = 0; i < ARRAY_SIZE(p->requests); i++)
232 INIT_LIST_HEAD(&p->requests[i]);
233 rb_link_node(&p->node, rb, parent);
234 rb_insert_color_cached(&p->node, &execlists->queue, first);
239 return &p->requests[idx];
243 struct list_head *priolist;
246 static struct intel_engine_cs *
247 sched_lock_engine(const struct i915_sched_node *node,
248 struct intel_engine_cs *locked,
249 struct sched_cache *cache)
251 struct intel_engine_cs *engine = node_to_request(node)->engine;
255 if (engine != locked) {
256 spin_unlock(&locked->timeline.lock);
257 memset(cache, 0, sizeof(*cache));
258 spin_lock(&engine->timeline.lock);
264 static bool inflight(const struct i915_request *rq,
265 const struct intel_engine_cs *engine)
267 const struct i915_request *active;
269 if (!i915_request_is_active(rq))
272 active = port_request(engine->execlists.port);
273 return active->hw_context == rq->hw_context;
276 static void __i915_schedule(struct i915_request *rq,
277 const struct i915_sched_attr *attr)
279 struct intel_engine_cs *engine;
280 struct i915_dependency *dep, *p;
281 struct i915_dependency stack;
282 const int prio = attr->priority;
283 struct sched_cache cache;
286 /* Needed in order to use the temporary link inside i915_dependency */
287 lockdep_assert_held(&schedule_lock);
288 GEM_BUG_ON(prio == I915_PRIORITY_INVALID);
290 if (i915_request_completed(rq))
293 if (prio <= READ_ONCE(rq->sched.attr.priority))
296 stack.signaler = &rq->sched;
297 list_add(&stack.dfs_link, &dfs);
300 * Recursively bump all dependent priorities to match the new request.
302 * A naive approach would be to use recursion:
303 * static void update_priorities(struct i915_sched_node *node, prio) {
304 * list_for_each_entry(dep, &node->signalers_list, signal_link)
305 * update_priorities(dep->signal, prio)
306 * queue_request(node);
308 * but that may have unlimited recursion depth and so runs a very
309 * real risk of overunning the kernel stack. Instead, we build
310 * a flat list of all dependencies starting with the current request.
311 * As we walk the list of dependencies, we add all of its dependencies
312 * to the end of the list (this may include an already visited
313 * request) and continue to walk onwards onto the new dependencies. The
314 * end result is a topological list of requests in reverse order, the
315 * last element in the list is the request we must execute first.
317 list_for_each_entry(dep, &dfs, dfs_link) {
318 struct i915_sched_node *node = dep->signaler;
320 /* If we are already flying, we know we have no signalers */
321 if (node_started(node))
325 * Within an engine, there can be no cycle, but we may
326 * refer to the same dependency chain multiple times
327 * (redundant dependencies are not eliminated) and across
330 list_for_each_entry(p, &node->signalers_list, signal_link) {
331 GEM_BUG_ON(p == dep); /* no cycles! */
333 if (node_signaled(p->signaler))
336 if (prio > READ_ONCE(p->signaler->attr.priority))
337 list_move_tail(&p->dfs_link, &dfs);
342 * If we didn't need to bump any existing priorities, and we haven't
343 * yet submitted this request (i.e. there is no potential race with
344 * execlists_submit_request()), we can set our own priority and skip
345 * acquiring the engine locks.
347 if (rq->sched.attr.priority == I915_PRIORITY_INVALID) {
348 GEM_BUG_ON(!list_empty(&rq->sched.link));
349 rq->sched.attr = *attr;
351 if (stack.dfs_link.next == stack.dfs_link.prev)
354 __list_del_entry(&stack.dfs_link);
357 memset(&cache, 0, sizeof(cache));
359 spin_lock(&engine->timeline.lock);
361 /* Fifo and depth-first replacement ensure our deps execute before us */
362 list_for_each_entry_safe_reverse(dep, p, &dfs, dfs_link) {
363 struct i915_sched_node *node = dep->signaler;
365 INIT_LIST_HEAD(&dep->dfs_link);
367 engine = sched_lock_engine(node, engine, &cache);
368 lockdep_assert_held(&engine->timeline.lock);
370 /* Recheck after acquiring the engine->timeline.lock */
371 if (prio <= node->attr.priority || node_signaled(node))
374 node->attr.priority = prio;
375 if (!list_empty(&node->link)) {
378 i915_sched_lookup_priolist(engine,
380 list_move_tail(&node->link, cache.priolist);
383 * If the request is not in the priolist queue because
384 * it is not yet runnable, then it doesn't contribute
385 * to our preemption decisions. On the other hand,
386 * if the request is on the HW, it too is not in the
387 * queue; but in that case we may still need to reorder
388 * the inflight requests.
390 if (!i915_sw_fence_done(&node_to_request(node)->submit))
394 if (prio <= engine->execlists.queue_priority_hint)
397 engine->execlists.queue_priority_hint = prio;
400 * If we are already the currently executing context, don't
401 * bother evaluating if we should preempt ourselves.
403 if (inflight(node_to_request(node), engine))
406 /* Defer (tasklet) submission until after all of our updates. */
407 tasklet_hi_schedule(&engine->execlists.tasklet);
410 spin_unlock(&engine->timeline.lock);
413 void i915_schedule(struct i915_request *rq, const struct i915_sched_attr *attr)
415 spin_lock_irq(&schedule_lock);
416 __i915_schedule(rq, attr);
417 spin_unlock_irq(&schedule_lock);
420 void i915_schedule_bump_priority(struct i915_request *rq, unsigned int bump)
422 struct i915_sched_attr attr;
425 GEM_BUG_ON(bump & ~I915_PRIORITY_MASK);
427 if (READ_ONCE(rq->sched.attr.priority) == I915_PRIORITY_INVALID)
430 spin_lock_irqsave(&schedule_lock, flags);
432 attr = rq->sched.attr;
433 attr.priority |= bump;
434 __i915_schedule(rq, &attr);
436 spin_unlock_irqrestore(&schedule_lock, flags);
439 void __i915_priolist_free(struct i915_priolist *p)
441 kmem_cache_free(global.slab_priorities, p);
444 static void i915_global_scheduler_shrink(void)
446 kmem_cache_shrink(global.slab_dependencies);
447 kmem_cache_shrink(global.slab_priorities);
450 static void i915_global_scheduler_exit(void)
452 kmem_cache_destroy(global.slab_dependencies);
453 kmem_cache_destroy(global.slab_priorities);
456 static struct i915_global_scheduler global = { {
457 .shrink = i915_global_scheduler_shrink,
458 .exit = i915_global_scheduler_exit,
461 int __init i915_global_scheduler_init(void)
463 global.slab_dependencies = KMEM_CACHE(i915_dependency,
465 if (!global.slab_dependencies)
468 global.slab_priorities = KMEM_CACHE(i915_priolist,
470 if (!global.slab_priorities)
473 i915_global_register(&global.base);
477 kmem_cache_destroy(global.slab_priorities);