2 * Copyright © 2015 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27 static void intel_breadcrumbs_fake_irq(unsigned long data)
29 struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
32 * The timer persists in case we cannot enable interrupts,
33 * or if we have previously seen seqno/interrupt incoherency
34 * ("missed interrupt" syndrome). Here the worker will wake up
35 * every jiffie in order to kick the oldest waiter to do the
36 * coherent seqno check.
39 if (intel_engine_wakeup(engine))
40 mod_timer(&engine->breadcrumbs.fake_irq, jiffies + 1);
44 static void irq_enable(struct intel_engine_cs *engine)
46 WARN_ON(!engine->irq_get(engine));
49 static void irq_disable(struct intel_engine_cs *engine)
51 engine->irq_put(engine);
54 static bool __intel_breadcrumbs_enable_irq(struct intel_breadcrumbs *b)
56 struct intel_engine_cs *engine =
57 container_of(b, struct intel_engine_cs, breadcrumbs);
58 struct drm_i915_private *i915 = engine->i915;
59 bool irq_posted = false;
61 assert_spin_locked(&b->lock);
65 /* Since we are waiting on a request, the GPU should be busy
66 * and should have its own rpm reference. For completeness,
67 * record an rpm reference for ourselves to cover the
68 * interrupt we unmask.
70 intel_runtime_pm_get_noresume(i915);
71 b->rpm_wakelock = true;
73 /* No interrupts? Kick the waiter every jiffie! */
74 if (intel_irqs_enabled(i915)) {
75 if (!test_bit(engine->id, &i915->gpu_error.test_irq_rings)) {
79 b->irq_enabled = true;
82 if (!b->irq_enabled ||
83 test_bit(engine->id, &i915->gpu_error.missed_irq_rings))
84 mod_timer(&b->fake_irq, jiffies + 1);
89 static void __intel_breadcrumbs_disable_irq(struct intel_breadcrumbs *b)
91 struct intel_engine_cs *engine =
92 container_of(b, struct intel_engine_cs, breadcrumbs);
94 assert_spin_locked(&b->lock);
100 b->irq_enabled = false;
103 intel_runtime_pm_put(engine->i915);
104 b->rpm_wakelock = false;
107 static inline struct intel_wait *to_wait(struct rb_node *node)
109 return container_of(node, struct intel_wait, node);
112 static inline void __intel_breadcrumbs_finish(struct intel_breadcrumbs *b,
113 struct intel_wait *wait)
115 assert_spin_locked(&b->lock);
117 /* This request is completed, so remove it from the tree, mark it as
118 * complete, and *then* wake up the associated task.
120 rb_erase(&wait->node, &b->waiters);
121 RB_CLEAR_NODE(&wait->node);
123 wake_up_process(wait->tsk); /* implicit smp_wmb() */
126 static bool __intel_engine_add_wait(struct intel_engine_cs *engine,
127 struct intel_wait *wait)
129 struct intel_breadcrumbs *b = &engine->breadcrumbs;
130 struct rb_node **p, *parent, *completed;
134 /* Insert the request into the retirement ordered list
135 * of waiters by walking the rbtree. If we are the oldest
136 * seqno in the tree (the first to be retired), then
137 * set ourselves as the bottom-half.
139 * As we descend the tree, prune completed branches since we hold the
140 * spinlock we know that the first_waiter must be delayed and can
141 * reduce some of the sequential wake up latency if we take action
142 * ourselves and wake up the completed tasks in parallel. Also, by
143 * removing stale elements in the tree, we may be able to reduce the
144 * ping-pong between the old bottom-half and ourselves as first-waiter.
149 seqno = engine->get_seqno(engine);
151 /* If the request completed before we managed to grab the spinlock,
152 * return now before adding ourselves to the rbtree. We let the
153 * current bottom-half handle any pending wakeups and instead
154 * try and get out of the way quickly.
156 if (i915_seqno_passed(seqno, wait->seqno)) {
157 RB_CLEAR_NODE(&wait->node);
161 p = &b->waiters.rb_node;
164 if (wait->seqno == to_wait(parent)->seqno) {
165 /* We have multiple waiters on the same seqno, select
166 * the highest priority task (that with the smallest
167 * task->prio) to serve as the bottom-half for this
170 if (wait->tsk->prio > to_wait(parent)->tsk->prio) {
171 p = &parent->rb_right;
174 p = &parent->rb_left;
176 } else if (i915_seqno_passed(wait->seqno,
177 to_wait(parent)->seqno)) {
178 p = &parent->rb_right;
179 if (i915_seqno_passed(seqno, to_wait(parent)->seqno))
184 p = &parent->rb_left;
187 rb_link_node(&wait->node, parent, p);
188 rb_insert_color(&wait->node, &b->waiters);
189 GEM_BUG_ON(!first && !b->tasklet);
192 struct rb_node *next = rb_next(completed);
194 GEM_BUG_ON(!next && !first);
195 if (next && next != &wait->node) {
197 b->first_wait = to_wait(next);
198 smp_store_mb(b->tasklet, b->first_wait->tsk);
199 /* As there is a delay between reading the current
200 * seqno, processing the completed tasks and selecting
201 * the next waiter, we may have missed the interrupt
202 * and so need for the next bottom-half to wakeup.
204 * Also as we enable the IRQ, we may miss the
205 * interrupt for that seqno, so we have to wake up
206 * the next bottom-half in order to do a coherent check
207 * in case the seqno passed.
209 __intel_breadcrumbs_enable_irq(b);
210 wake_up_process(to_wait(next)->tsk);
214 struct intel_wait *crumb = to_wait(completed);
215 completed = rb_prev(completed);
216 __intel_breadcrumbs_finish(b, crumb);
221 GEM_BUG_ON(rb_first(&b->waiters) != &wait->node);
222 b->first_wait = wait;
223 smp_store_mb(b->tasklet, wait->tsk);
224 first = __intel_breadcrumbs_enable_irq(b);
226 GEM_BUG_ON(!b->tasklet);
227 GEM_BUG_ON(!b->first_wait);
228 GEM_BUG_ON(rb_first(&b->waiters) != &b->first_wait->node);
233 bool intel_engine_add_wait(struct intel_engine_cs *engine,
234 struct intel_wait *wait)
236 struct intel_breadcrumbs *b = &engine->breadcrumbs;
240 first = __intel_engine_add_wait(engine, wait);
241 spin_unlock(&b->lock);
246 void intel_engine_enable_fake_irq(struct intel_engine_cs *engine)
248 mod_timer(&engine->breadcrumbs.fake_irq, jiffies + 1);
251 static inline bool chain_wakeup(struct rb_node *rb, int priority)
253 return rb && to_wait(rb)->tsk->prio <= priority;
256 void intel_engine_remove_wait(struct intel_engine_cs *engine,
257 struct intel_wait *wait)
259 struct intel_breadcrumbs *b = &engine->breadcrumbs;
261 /* Quick check to see if this waiter was already decoupled from
262 * the tree by the bottom-half to avoid contention on the spinlock
265 if (RB_EMPTY_NODE(&wait->node))
270 if (RB_EMPTY_NODE(&wait->node))
273 if (b->first_wait == wait) {
274 struct rb_node *next;
275 const int priority = wait->tsk->prio;
277 GEM_BUG_ON(b->tasklet != wait->tsk);
279 /* We are the current bottom-half. Find the next candidate,
280 * the first waiter in the queue on the remaining oldest
281 * request. As multiple seqnos may complete in the time it
282 * takes us to wake up and find the next waiter, we have to
283 * wake up that waiter for it to perform its own coherent
286 next = rb_next(&wait->node);
287 if (chain_wakeup(next, priority)) {
288 /* If the next waiter is already complete,
289 * wake it up and continue onto the next waiter. So
290 * if have a small herd, they will wake up in parallel
291 * rather than sequentially, which should reduce
292 * the overall latency in waking all the completed
295 * However, waking up a chain adds extra latency to
296 * the first_waiter. This is undesirable if that
297 * waiter is a high priority task.
299 u32 seqno = engine->get_seqno(engine);
301 while (i915_seqno_passed(seqno, to_wait(next)->seqno)) {
302 struct rb_node *n = rb_next(next);
304 __intel_breadcrumbs_finish(b, to_wait(next));
306 if (!chain_wakeup(next, priority))
312 /* In our haste, we may have completed the first waiter
313 * before we enabled the interrupt. Do so now as we
314 * have a second waiter for a future seqno. Afterwards,
315 * we have to wake up that waiter in case we missed
316 * the interrupt, or if we have to handle an
317 * exception rather than a seqno completion.
319 b->first_wait = to_wait(next);
320 smp_store_mb(b->tasklet, b->first_wait->tsk);
321 if (b->first_wait->seqno != wait->seqno)
322 __intel_breadcrumbs_enable_irq(b);
323 wake_up_process(b->tasklet);
325 b->first_wait = NULL;
326 WRITE_ONCE(b->tasklet, NULL);
327 __intel_breadcrumbs_disable_irq(b);
330 GEM_BUG_ON(rb_first(&b->waiters) == &wait->node);
333 GEM_BUG_ON(RB_EMPTY_NODE(&wait->node));
334 rb_erase(&wait->node, &b->waiters);
337 GEM_BUG_ON(b->first_wait == wait);
338 GEM_BUG_ON(rb_first(&b->waiters) !=
339 (b->first_wait ? &b->first_wait->node : NULL));
340 GEM_BUG_ON(!b->tasklet ^ RB_EMPTY_ROOT(&b->waiters));
341 spin_unlock(&b->lock);
344 int intel_engine_init_breadcrumbs(struct intel_engine_cs *engine)
346 struct intel_breadcrumbs *b = &engine->breadcrumbs;
348 spin_lock_init(&b->lock);
349 setup_timer(&b->fake_irq,
350 intel_breadcrumbs_fake_irq,
351 (unsigned long)engine);
356 void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine)
358 struct intel_breadcrumbs *b = &engine->breadcrumbs;
360 del_timer_sync(&b->fake_irq);
363 unsigned int intel_kick_waiters(struct drm_i915_private *i915)
365 struct intel_engine_cs *engine;
366 unsigned int mask = 0;
368 /* To avoid the task_struct disappearing beneath us as we wake up
369 * the process, we must first inspect the task_struct->state under the
370 * RCU lock, i.e. as we call wake_up_process() we must be holding the
374 for_each_engine(engine, i915)
375 if (unlikely(intel_engine_wakeup(engine)))
376 mask |= intel_engine_flag(engine);