Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2015 Intel Corporation |
| 3 | * |
| 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: |
| 10 | * |
| 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 |
| 13 | * Software. |
| 14 | * |
| 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 |
| 21 | * IN THE SOFTWARE. |
| 22 | * |
| 23 | */ |
| 24 | |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 25 | #include <linux/kthread.h> |
| 26 | |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 27 | #include "i915_drv.h" |
| 28 | |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 29 | static unsigned int __intel_breadcrumbs_wakeup(struct intel_breadcrumbs *b) |
Chris Wilson | 8d769ea | 2017-02-27 20:58:47 +0000 | [diff] [blame] | 30 | { |
Chris Wilson | 56299fb | 2017-02-27 20:58:48 +0000 | [diff] [blame] | 31 | struct intel_wait *wait; |
Chris Wilson | 8d769ea | 2017-02-27 20:58:47 +0000 | [diff] [blame] | 32 | unsigned int result = 0; |
| 33 | |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 34 | wait = b->first_wait; |
Chris Wilson | 56299fb | 2017-02-27 20:58:48 +0000 | [diff] [blame] | 35 | if (wait) { |
Chris Wilson | 8d769ea | 2017-02-27 20:58:47 +0000 | [diff] [blame] | 36 | result = ENGINE_WAKEUP_WAITER; |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 37 | if (wake_up_process(wait->tsk)) |
| 38 | result |= ENGINE_WAKEUP_ASLEEP; |
Chris Wilson | 8d769ea | 2017-02-27 20:58:47 +0000 | [diff] [blame] | 39 | } |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 40 | |
| 41 | return result; |
| 42 | } |
| 43 | |
| 44 | unsigned int intel_engine_wakeup(struct intel_engine_cs *engine) |
| 45 | { |
| 46 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 47 | unsigned long flags; |
| 48 | unsigned int result; |
| 49 | |
| 50 | spin_lock_irqsave(&b->lock, flags); |
| 51 | result = __intel_breadcrumbs_wakeup(b); |
| 52 | spin_unlock_irqrestore(&b->lock, flags); |
Chris Wilson | 8d769ea | 2017-02-27 20:58:47 +0000 | [diff] [blame] | 53 | |
| 54 | return result; |
| 55 | } |
| 56 | |
Chris Wilson | 2246bea | 2017-02-17 15:13:00 +0000 | [diff] [blame] | 57 | static unsigned long wait_timeout(void) |
| 58 | { |
| 59 | return round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES); |
| 60 | } |
| 61 | |
Chris Wilson | 80166e40 | 2017-02-28 08:50:18 +0000 | [diff] [blame] | 62 | static noinline void missed_breadcrumb(struct intel_engine_cs *engine) |
| 63 | { |
| 64 | DRM_DEBUG_DRIVER("%s missed breadcrumb at %pF, irq posted? %s\n", |
| 65 | engine->name, __builtin_return_address(0), |
| 66 | yesno(test_bit(ENGINE_IRQ_BREADCRUMB, |
| 67 | &engine->irq_posted))); |
| 68 | |
| 69 | set_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings); |
| 70 | } |
| 71 | |
Chris Wilson | 83348ba | 2016-08-09 17:47:51 +0100 | [diff] [blame] | 72 | static void intel_breadcrumbs_hangcheck(unsigned long data) |
| 73 | { |
| 74 | struct intel_engine_cs *engine = (struct intel_engine_cs *)data; |
| 75 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 76 | |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 77 | if (!b->irq_armed) |
Chris Wilson | 83348ba | 2016-08-09 17:47:51 +0100 | [diff] [blame] | 78 | return; |
| 79 | |
Chris Wilson | 2246bea | 2017-02-17 15:13:00 +0000 | [diff] [blame] | 80 | if (b->hangcheck_interrupts != atomic_read(&engine->irq_count)) { |
| 81 | b->hangcheck_interrupts = atomic_read(&engine->irq_count); |
| 82 | mod_timer(&b->hangcheck, wait_timeout()); |
Chris Wilson | 83348ba | 2016-08-09 17:47:51 +0100 | [diff] [blame] | 83 | return; |
| 84 | } |
| 85 | |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 86 | /* We keep the hangcheck time alive until we disarm the irq, even |
| 87 | * if there are no waiters at present. |
| 88 | * |
| 89 | * If the waiter was currently running, assume it hasn't had a chance |
Chris Wilson | 8998567 | 2017-02-17 15:13:02 +0000 | [diff] [blame] | 90 | * to process the pending interrupt (e.g, low priority task on a loaded |
| 91 | * system) and wait until it sleeps before declaring a missed interrupt. |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 92 | * |
| 93 | * If the waiter was asleep (and not even pending a wakeup), then we |
| 94 | * must have missed an interrupt as the GPU has stopped advancing |
| 95 | * but we still have a waiter. Assuming all batches complete within |
| 96 | * DRM_I915_HANGCHECK_JIFFIES [1.5s]! |
Chris Wilson | 8998567 | 2017-02-17 15:13:02 +0000 | [diff] [blame] | 97 | */ |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 98 | if (intel_engine_wakeup(engine) & ENGINE_WAKEUP_ASLEEP) { |
Chris Wilson | 80166e40 | 2017-02-28 08:50:18 +0000 | [diff] [blame] | 99 | missed_breadcrumb(engine); |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 100 | mod_timer(&engine->breadcrumbs.fake_irq, jiffies + 1); |
| 101 | } else { |
Chris Wilson | 8998567 | 2017-02-17 15:13:02 +0000 | [diff] [blame] | 102 | mod_timer(&b->hangcheck, wait_timeout()); |
Chris Wilson | 8998567 | 2017-02-17 15:13:02 +0000 | [diff] [blame] | 103 | } |
Chris Wilson | 83348ba | 2016-08-09 17:47:51 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 106 | static void intel_breadcrumbs_fake_irq(unsigned long data) |
| 107 | { |
| 108 | struct intel_engine_cs *engine = (struct intel_engine_cs *)data; |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 109 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 110 | unsigned long flags; |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * The timer persists in case we cannot enable interrupts, |
| 114 | * or if we have previously seen seqno/interrupt incoherency |
| 115 | * ("missed interrupt" syndrome). Here the worker will wake up |
| 116 | * every jiffie in order to kick the oldest waiter to do the |
| 117 | * coherent seqno check. |
| 118 | */ |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 119 | |
| 120 | spin_lock_irqsave(&b->lock, flags); |
| 121 | if (!__intel_breadcrumbs_wakeup(b)) |
| 122 | __intel_engine_disarm_breadcrumbs(engine); |
| 123 | spin_unlock_irqrestore(&b->lock, flags); |
| 124 | if (!b->irq_armed) |
Chris Wilson | 19d0a57 | 2017-02-27 20:58:49 +0000 | [diff] [blame] | 125 | return; |
| 126 | |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 127 | mod_timer(&b->fake_irq, jiffies + 1); |
Chris Wilson | 19d0a57 | 2017-02-27 20:58:49 +0000 | [diff] [blame] | 128 | |
| 129 | /* Ensure that even if the GPU hangs, we get woken up. |
| 130 | * |
| 131 | * However, note that if no one is waiting, we never notice |
| 132 | * a gpu hang. Eventually, we will have to wait for a resource |
| 133 | * held by the GPU and so trigger a hangcheck. In the most |
| 134 | * pathological case, this will be upon memory starvation! To |
| 135 | * prevent this, we also queue the hangcheck from the retire |
| 136 | * worker. |
| 137 | */ |
| 138 | i915_queue_hangcheck(engine->i915); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static void irq_enable(struct intel_engine_cs *engine) |
| 142 | { |
Chris Wilson | 3d5564e | 2016-07-01 17:23:23 +0100 | [diff] [blame] | 143 | /* Enabling the IRQ may miss the generation of the interrupt, but |
| 144 | * we still need to force the barrier before reading the seqno, |
| 145 | * just in case. |
| 146 | */ |
Chris Wilson | 538b257 | 2017-01-24 15:18:05 +0000 | [diff] [blame] | 147 | set_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted); |
Chris Wilson | 31bb59c | 2016-07-01 17:23:27 +0100 | [diff] [blame] | 148 | |
Chris Wilson | f6168e3 | 2016-10-28 13:58:55 +0100 | [diff] [blame] | 149 | /* Caller disables interrupts */ |
| 150 | spin_lock(&engine->i915->irq_lock); |
Chris Wilson | 31bb59c | 2016-07-01 17:23:27 +0100 | [diff] [blame] | 151 | engine->irq_enable(engine); |
Chris Wilson | f6168e3 | 2016-10-28 13:58:55 +0100 | [diff] [blame] | 152 | spin_unlock(&engine->i915->irq_lock); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static void irq_disable(struct intel_engine_cs *engine) |
| 156 | { |
Chris Wilson | f6168e3 | 2016-10-28 13:58:55 +0100 | [diff] [blame] | 157 | /* Caller disables interrupts */ |
| 158 | spin_lock(&engine->i915->irq_lock); |
Chris Wilson | 31bb59c | 2016-07-01 17:23:27 +0100 | [diff] [blame] | 159 | engine->irq_disable(engine); |
Chris Wilson | f6168e3 | 2016-10-28 13:58:55 +0100 | [diff] [blame] | 160 | spin_unlock(&engine->i915->irq_lock); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 163 | void __intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine) |
| 164 | { |
| 165 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 166 | |
Chris Wilson | 6752041 | 2017-03-02 13:28:01 +0000 | [diff] [blame] | 167 | lockdep_assert_held(&b->lock); |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 168 | |
| 169 | if (b->irq_enabled) { |
| 170 | irq_disable(engine); |
| 171 | b->irq_enabled = false; |
| 172 | } |
| 173 | |
| 174 | b->irq_armed = false; |
| 175 | } |
| 176 | |
| 177 | void intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine) |
| 178 | { |
| 179 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 180 | unsigned long flags; |
| 181 | |
| 182 | if (!b->irq_armed) |
| 183 | return; |
| 184 | |
| 185 | spin_lock_irqsave(&b->lock, flags); |
| 186 | |
| 187 | /* We only disarm the irq when we are idle (all requests completed), |
| 188 | * so if there remains a sleeping waiter, it missed the request |
| 189 | * completion. |
| 190 | */ |
| 191 | if (__intel_breadcrumbs_wakeup(b) & ENGINE_WAKEUP_ASLEEP) |
Chris Wilson | 80166e40 | 2017-02-28 08:50:18 +0000 | [diff] [blame] | 192 | missed_breadcrumb(engine); |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 193 | |
| 194 | __intel_engine_disarm_breadcrumbs(engine); |
| 195 | |
| 196 | spin_unlock_irqrestore(&b->lock, flags); |
| 197 | } |
| 198 | |
Chris Wilson | 6ef98ea | 2017-02-17 15:13:03 +0000 | [diff] [blame] | 199 | static bool use_fake_irq(const struct intel_breadcrumbs *b) |
| 200 | { |
| 201 | const struct intel_engine_cs *engine = |
| 202 | container_of(b, struct intel_engine_cs, breadcrumbs); |
| 203 | |
| 204 | if (!test_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings)) |
| 205 | return false; |
| 206 | |
| 207 | /* Only start with the heavy weight fake irq timer if we have not |
| 208 | * seen any interrupts since enabling it the first time. If the |
| 209 | * interrupts are still arriving, it means we made a mistake in our |
| 210 | * engine->seqno_barrier(), a timing error that should be transient |
| 211 | * and unlikely to reoccur. |
| 212 | */ |
| 213 | return atomic_read(&engine->irq_count) == b->hangcheck_interrupts; |
| 214 | } |
| 215 | |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 216 | static void enable_fake_irq(struct intel_breadcrumbs *b) |
| 217 | { |
| 218 | /* Ensure we never sleep indefinitely */ |
| 219 | if (!b->irq_enabled || use_fake_irq(b)) |
| 220 | mod_timer(&b->fake_irq, jiffies + 1); |
| 221 | else |
| 222 | mod_timer(&b->hangcheck, wait_timeout()); |
| 223 | } |
| 224 | |
Chris Wilson | 0417131 | 2016-07-06 12:39:00 +0100 | [diff] [blame] | 225 | static void __intel_breadcrumbs_enable_irq(struct intel_breadcrumbs *b) |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 226 | { |
| 227 | struct intel_engine_cs *engine = |
| 228 | container_of(b, struct intel_engine_cs, breadcrumbs); |
| 229 | struct drm_i915_private *i915 = engine->i915; |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 230 | |
Chris Wilson | 6752041 | 2017-03-02 13:28:01 +0000 | [diff] [blame] | 231 | lockdep_assert_held(&b->lock); |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 232 | if (b->irq_armed) |
Chris Wilson | 0417131 | 2016-07-06 12:39:00 +0100 | [diff] [blame] | 233 | return; |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 234 | |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 235 | /* The breadcrumb irq will be disarmed on the interrupt after the |
| 236 | * waiters are signaled. This gives us a single interrupt window in |
| 237 | * which we can add a new waiter and avoid the cost of re-enabling |
| 238 | * the irq. |
| 239 | */ |
| 240 | b->irq_armed = true; |
| 241 | GEM_BUG_ON(b->irq_enabled); |
| 242 | |
Chris Wilson | f97fbf9 | 2017-02-13 17:15:14 +0000 | [diff] [blame] | 243 | if (I915_SELFTEST_ONLY(b->mock)) { |
| 244 | /* For our mock objects we want to avoid interaction |
| 245 | * with the real hardware (which is not set up). So |
| 246 | * we simply pretend we have enabled the powerwell |
| 247 | * and the irq, and leave it up to the mock |
| 248 | * implementation to call intel_engine_wakeup() |
| 249 | * itself when it wants to simulate a user interrupt, |
| 250 | */ |
Chris Wilson | f97fbf9 | 2017-02-13 17:15:14 +0000 | [diff] [blame] | 251 | return; |
| 252 | } |
| 253 | |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 254 | /* Since we are waiting on a request, the GPU should be busy |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 255 | * and should have its own rpm reference. This is tracked |
| 256 | * by i915->gt.awake, we can forgo holding our own wakref |
| 257 | * for the interrupt as before i915->gt.awake is released (when |
| 258 | * the driver is idle) we disarm the breadcrumbs. |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 259 | */ |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 260 | |
| 261 | /* No interrupts? Kick the waiter every jiffie! */ |
| 262 | if (intel_irqs_enabled(i915)) { |
Chris Wilson | 3d5564e | 2016-07-01 17:23:23 +0100 | [diff] [blame] | 263 | if (!test_bit(engine->id, &i915->gpu_error.test_irq_rings)) |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 264 | irq_enable(engine); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 265 | b->irq_enabled = true; |
| 266 | } |
| 267 | |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 268 | enable_fake_irq(b); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static inline struct intel_wait *to_wait(struct rb_node *node) |
| 272 | { |
Chris Wilson | d856786 | 2016-12-20 10:40:03 +0000 | [diff] [blame] | 273 | return rb_entry(node, struct intel_wait, node); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static inline void __intel_breadcrumbs_finish(struct intel_breadcrumbs *b, |
| 277 | struct intel_wait *wait) |
| 278 | { |
Chris Wilson | 6752041 | 2017-03-02 13:28:01 +0000 | [diff] [blame] | 279 | lockdep_assert_held(&b->lock); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 280 | |
| 281 | /* This request is completed, so remove it from the tree, mark it as |
| 282 | * complete, and *then* wake up the associated task. |
| 283 | */ |
| 284 | rb_erase(&wait->node, &b->waiters); |
| 285 | RB_CLEAR_NODE(&wait->node); |
| 286 | |
| 287 | wake_up_process(wait->tsk); /* implicit smp_wmb() */ |
| 288 | } |
| 289 | |
Chris Wilson | b66255f | 2017-03-03 17:14:22 +0000 | [diff] [blame^] | 290 | static inline void __intel_breadcrumbs_next(struct intel_engine_cs *engine, |
| 291 | struct rb_node *next) |
| 292 | { |
| 293 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 294 | |
| 295 | GEM_BUG_ON(!b->irq_armed); |
| 296 | b->first_wait = to_wait(next); |
| 297 | |
| 298 | /* We always wake up the next waiter that takes over as the bottom-half |
| 299 | * as we may delegate not only the irq-seqno barrier to the next waiter |
| 300 | * but also the task of waking up concurrent waiters. |
| 301 | */ |
| 302 | if (next) |
| 303 | wake_up_process(to_wait(next)->tsk); |
| 304 | } |
| 305 | |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 306 | static bool __intel_engine_add_wait(struct intel_engine_cs *engine, |
| 307 | struct intel_wait *wait) |
| 308 | { |
| 309 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 310 | struct rb_node **p, *parent, *completed; |
| 311 | bool first; |
| 312 | u32 seqno; |
| 313 | |
| 314 | /* Insert the request into the retirement ordered list |
| 315 | * of waiters by walking the rbtree. If we are the oldest |
| 316 | * seqno in the tree (the first to be retired), then |
| 317 | * set ourselves as the bottom-half. |
| 318 | * |
| 319 | * As we descend the tree, prune completed branches since we hold the |
| 320 | * spinlock we know that the first_waiter must be delayed and can |
| 321 | * reduce some of the sequential wake up latency if we take action |
| 322 | * ourselves and wake up the completed tasks in parallel. Also, by |
| 323 | * removing stale elements in the tree, we may be able to reduce the |
| 324 | * ping-pong between the old bottom-half and ourselves as first-waiter. |
| 325 | */ |
| 326 | first = true; |
| 327 | parent = NULL; |
| 328 | completed = NULL; |
Chris Wilson | 1b7744e | 2016-07-01 17:23:17 +0100 | [diff] [blame] | 329 | seqno = intel_engine_get_seqno(engine); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 330 | |
| 331 | /* If the request completed before we managed to grab the spinlock, |
| 332 | * return now before adding ourselves to the rbtree. We let the |
| 333 | * current bottom-half handle any pending wakeups and instead |
| 334 | * try and get out of the way quickly. |
| 335 | */ |
| 336 | if (i915_seqno_passed(seqno, wait->seqno)) { |
| 337 | RB_CLEAR_NODE(&wait->node); |
| 338 | return first; |
| 339 | } |
| 340 | |
| 341 | p = &b->waiters.rb_node; |
| 342 | while (*p) { |
| 343 | parent = *p; |
| 344 | if (wait->seqno == to_wait(parent)->seqno) { |
| 345 | /* We have multiple waiters on the same seqno, select |
| 346 | * the highest priority task (that with the smallest |
| 347 | * task->prio) to serve as the bottom-half for this |
| 348 | * group. |
| 349 | */ |
| 350 | if (wait->tsk->prio > to_wait(parent)->tsk->prio) { |
| 351 | p = &parent->rb_right; |
| 352 | first = false; |
| 353 | } else { |
| 354 | p = &parent->rb_left; |
| 355 | } |
| 356 | } else if (i915_seqno_passed(wait->seqno, |
| 357 | to_wait(parent)->seqno)) { |
| 358 | p = &parent->rb_right; |
| 359 | if (i915_seqno_passed(seqno, to_wait(parent)->seqno)) |
| 360 | completed = parent; |
| 361 | else |
| 362 | first = false; |
| 363 | } else { |
| 364 | p = &parent->rb_left; |
| 365 | } |
| 366 | } |
| 367 | rb_link_node(&wait->node, parent, p); |
| 368 | rb_insert_color(&wait->node, &b->waiters); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 369 | |
| 370 | if (completed) { |
| 371 | struct rb_node *next = rb_next(completed); |
| 372 | |
| 373 | GEM_BUG_ON(!next && !first); |
| 374 | if (next && next != &wait->node) { |
| 375 | GEM_BUG_ON(first); |
Chris Wilson | b66255f | 2017-03-03 17:14:22 +0000 | [diff] [blame^] | 376 | __intel_breadcrumbs_next(engine, next); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | do { |
| 380 | struct intel_wait *crumb = to_wait(completed); |
| 381 | completed = rb_prev(completed); |
| 382 | __intel_breadcrumbs_finish(b, crumb); |
| 383 | } while (completed); |
| 384 | } |
| 385 | |
| 386 | if (first) { |
| 387 | GEM_BUG_ON(rb_first(&b->waiters) != &wait->node); |
| 388 | b->first_wait = wait; |
Chris Wilson | 0417131 | 2016-07-06 12:39:00 +0100 | [diff] [blame] | 389 | /* After assigning ourselves as the new bottom-half, we must |
| 390 | * perform a cursory check to prevent a missed interrupt. |
| 391 | * Either we miss the interrupt whilst programming the hardware, |
| 392 | * or if there was a previous waiter (for a later seqno) they |
| 393 | * may be woken instead of us (due to the inherent race |
Chris Wilson | aca34b6 | 2016-07-06 12:39:02 +0100 | [diff] [blame] | 394 | * in the unlocked read of b->irq_seqno_bh in the irq handler) |
| 395 | * and so we miss the wake up. |
Chris Wilson | 0417131 | 2016-07-06 12:39:00 +0100 | [diff] [blame] | 396 | */ |
| 397 | __intel_breadcrumbs_enable_irq(b); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 398 | } |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 399 | GEM_BUG_ON(!b->first_wait); |
| 400 | GEM_BUG_ON(rb_first(&b->waiters) != &b->first_wait->node); |
| 401 | |
| 402 | return first; |
| 403 | } |
| 404 | |
| 405 | bool intel_engine_add_wait(struct intel_engine_cs *engine, |
| 406 | struct intel_wait *wait) |
| 407 | { |
| 408 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 409 | bool first; |
| 410 | |
Chris Wilson | f6168e3 | 2016-10-28 13:58:55 +0100 | [diff] [blame] | 411 | spin_lock_irq(&b->lock); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 412 | first = __intel_engine_add_wait(engine, wait); |
Chris Wilson | f6168e3 | 2016-10-28 13:58:55 +0100 | [diff] [blame] | 413 | spin_unlock_irq(&b->lock); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 414 | |
| 415 | return first; |
| 416 | } |
| 417 | |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 418 | static inline bool chain_wakeup(struct rb_node *rb, int priority) |
| 419 | { |
| 420 | return rb && to_wait(rb)->tsk->prio <= priority; |
| 421 | } |
| 422 | |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 423 | static inline int wakeup_priority(struct intel_breadcrumbs *b, |
| 424 | struct task_struct *tsk) |
| 425 | { |
| 426 | if (tsk == b->signaler) |
| 427 | return INT_MIN; |
| 428 | else |
| 429 | return tsk->prio; |
| 430 | } |
| 431 | |
Chris Wilson | 9eb143b | 2017-02-23 07:44:16 +0000 | [diff] [blame] | 432 | static void __intel_engine_remove_wait(struct intel_engine_cs *engine, |
| 433 | struct intel_wait *wait) |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 434 | { |
| 435 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 436 | |
Chris Wilson | 6752041 | 2017-03-02 13:28:01 +0000 | [diff] [blame] | 437 | lockdep_assert_held(&b->lock); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 438 | |
| 439 | if (RB_EMPTY_NODE(&wait->node)) |
Chris Wilson | 9eb143b | 2017-02-23 07:44:16 +0000 | [diff] [blame] | 440 | goto out; |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 441 | |
| 442 | if (b->first_wait == wait) { |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 443 | const int priority = wakeup_priority(b, wait->tsk); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 444 | struct rb_node *next; |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 445 | |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 446 | /* We are the current bottom-half. Find the next candidate, |
| 447 | * the first waiter in the queue on the remaining oldest |
| 448 | * request. As multiple seqnos may complete in the time it |
| 449 | * takes us to wake up and find the next waiter, we have to |
| 450 | * wake up that waiter for it to perform its own coherent |
| 451 | * completion check. |
| 452 | */ |
| 453 | next = rb_next(&wait->node); |
| 454 | if (chain_wakeup(next, priority)) { |
| 455 | /* If the next waiter is already complete, |
| 456 | * wake it up and continue onto the next waiter. So |
| 457 | * if have a small herd, they will wake up in parallel |
| 458 | * rather than sequentially, which should reduce |
| 459 | * the overall latency in waking all the completed |
| 460 | * clients. |
| 461 | * |
| 462 | * However, waking up a chain adds extra latency to |
| 463 | * the first_waiter. This is undesirable if that |
| 464 | * waiter is a high priority task. |
| 465 | */ |
Chris Wilson | 1b7744e | 2016-07-01 17:23:17 +0100 | [diff] [blame] | 466 | u32 seqno = intel_engine_get_seqno(engine); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 467 | |
| 468 | while (i915_seqno_passed(seqno, to_wait(next)->seqno)) { |
| 469 | struct rb_node *n = rb_next(next); |
| 470 | |
| 471 | __intel_breadcrumbs_finish(b, to_wait(next)); |
| 472 | next = n; |
| 473 | if (!chain_wakeup(next, priority)) |
| 474 | break; |
| 475 | } |
| 476 | } |
| 477 | |
Chris Wilson | b66255f | 2017-03-03 17:14:22 +0000 | [diff] [blame^] | 478 | __intel_breadcrumbs_next(engine, next); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 479 | } else { |
| 480 | GEM_BUG_ON(rb_first(&b->waiters) == &wait->node); |
| 481 | } |
| 482 | |
| 483 | GEM_BUG_ON(RB_EMPTY_NODE(&wait->node)); |
| 484 | rb_erase(&wait->node, &b->waiters); |
| 485 | |
Chris Wilson | 9eb143b | 2017-02-23 07:44:16 +0000 | [diff] [blame] | 486 | out: |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 487 | GEM_BUG_ON(b->first_wait == wait); |
| 488 | GEM_BUG_ON(rb_first(&b->waiters) != |
| 489 | (b->first_wait ? &b->first_wait->node : NULL)); |
Chris Wilson | 9eb143b | 2017-02-23 07:44:16 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | void intel_engine_remove_wait(struct intel_engine_cs *engine, |
| 493 | struct intel_wait *wait) |
| 494 | { |
| 495 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 496 | |
| 497 | /* Quick check to see if this waiter was already decoupled from |
| 498 | * the tree by the bottom-half to avoid contention on the spinlock |
| 499 | * by the herd. |
| 500 | */ |
| 501 | if (RB_EMPTY_NODE(&wait->node)) |
| 502 | return; |
| 503 | |
| 504 | spin_lock_irq(&b->lock); |
| 505 | __intel_engine_remove_wait(engine, wait); |
Chris Wilson | f6168e3 | 2016-10-28 13:58:55 +0100 | [diff] [blame] | 506 | spin_unlock_irq(&b->lock); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 507 | } |
| 508 | |
Chris Wilson | d6a2289 | 2017-02-23 07:44:17 +0000 | [diff] [blame] | 509 | static bool signal_valid(const struct drm_i915_gem_request *request) |
| 510 | { |
| 511 | return intel_wait_check_request(&request->signaling.wait, request); |
| 512 | } |
| 513 | |
| 514 | static bool signal_complete(const struct drm_i915_gem_request *request) |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 515 | { |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame] | 516 | if (!request) |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 517 | return false; |
| 518 | |
| 519 | /* If another process served as the bottom-half it may have already |
| 520 | * signalled that this wait is already completed. |
| 521 | */ |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame] | 522 | if (intel_wait_complete(&request->signaling.wait)) |
Chris Wilson | d6a2289 | 2017-02-23 07:44:17 +0000 | [diff] [blame] | 523 | return signal_valid(request); |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 524 | |
| 525 | /* Carefully check if the request is complete, giving time for the |
| 526 | * seqno to be visible or if the GPU hung. |
| 527 | */ |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame] | 528 | if (__i915_request_irq_complete(request)) |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 529 | return true; |
| 530 | |
| 531 | return false; |
| 532 | } |
| 533 | |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame] | 534 | static struct drm_i915_gem_request *to_signaler(struct rb_node *rb) |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 535 | { |
Chris Wilson | d856786 | 2016-12-20 10:40:03 +0000 | [diff] [blame] | 536 | return rb_entry(rb, struct drm_i915_gem_request, signaling.node); |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static void signaler_set_rtpriority(void) |
| 540 | { |
| 541 | struct sched_param param = { .sched_priority = 1 }; |
| 542 | |
| 543 | sched_setscheduler_nocheck(current, SCHED_FIFO, ¶m); |
| 544 | } |
| 545 | |
| 546 | static int intel_breadcrumbs_signaler(void *arg) |
| 547 | { |
| 548 | struct intel_engine_cs *engine = arg; |
| 549 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame] | 550 | struct drm_i915_gem_request *request; |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 551 | |
| 552 | /* Install ourselves with high priority to reduce signalling latency */ |
| 553 | signaler_set_rtpriority(); |
| 554 | |
| 555 | do { |
| 556 | set_current_state(TASK_INTERRUPTIBLE); |
| 557 | |
| 558 | /* We are either woken up by the interrupt bottom-half, |
| 559 | * or by a client adding a new signaller. In both cases, |
| 560 | * the GPU seqno may have advanced beyond our oldest signal. |
| 561 | * If it has, propagate the signal, remove the waiter and |
| 562 | * check again with the next oldest signal. Otherwise we |
| 563 | * need to wait for a new interrupt from the GPU or for |
| 564 | * a new client. |
| 565 | */ |
Chris Wilson | cced5e2 | 2017-02-23 07:44:15 +0000 | [diff] [blame] | 566 | rcu_read_lock(); |
| 567 | request = rcu_dereference(b->first_signal); |
| 568 | if (request) |
| 569 | request = i915_gem_request_get_rcu(request); |
| 570 | rcu_read_unlock(); |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame] | 571 | if (signal_complete(request)) { |
Chris Wilson | 7c9e934 | 2017-01-24 11:00:09 +0000 | [diff] [blame] | 572 | local_bh_disable(); |
| 573 | dma_fence_signal(&request->fence); |
| 574 | local_bh_enable(); /* kick start the tasklets */ |
| 575 | |
Chris Wilson | 9eb143b | 2017-02-23 07:44:16 +0000 | [diff] [blame] | 576 | spin_lock_irq(&b->lock); |
| 577 | |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 578 | /* Wake up all other completed waiters and select the |
| 579 | * next bottom-half for the next user interrupt. |
| 580 | */ |
Chris Wilson | 9eb143b | 2017-02-23 07:44:16 +0000 | [diff] [blame] | 581 | __intel_engine_remove_wait(engine, |
| 582 | &request->signaling.wait); |
Chris Wilson | 5590af3 | 2016-09-09 14:11:54 +0100 | [diff] [blame] | 583 | |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 584 | /* Find the next oldest signal. Note that as we have |
| 585 | * not been holding the lock, another client may |
| 586 | * have installed an even older signal than the one |
| 587 | * we just completed - so double check we are still |
| 588 | * the oldest before picking the next one. |
| 589 | */ |
Chris Wilson | cced5e2 | 2017-02-23 07:44:15 +0000 | [diff] [blame] | 590 | if (request == rcu_access_pointer(b->first_signal)) { |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame] | 591 | struct rb_node *rb = |
| 592 | rb_next(&request->signaling.node); |
Chris Wilson | cced5e2 | 2017-02-23 07:44:15 +0000 | [diff] [blame] | 593 | rcu_assign_pointer(b->first_signal, |
| 594 | rb ? to_signaler(rb) : NULL); |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame] | 595 | } |
| 596 | rb_erase(&request->signaling.node, &b->signals); |
Chris Wilson | 9eb143b | 2017-02-23 07:44:16 +0000 | [diff] [blame] | 597 | RB_CLEAR_NODE(&request->signaling.node); |
| 598 | |
Chris Wilson | f6168e3 | 2016-10-28 13:58:55 +0100 | [diff] [blame] | 599 | spin_unlock_irq(&b->lock); |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 600 | |
Chris Wilson | e8a261e | 2016-07-20 13:31:49 +0100 | [diff] [blame] | 601 | i915_gem_request_put(request); |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 602 | } else { |
Chris Wilson | d6a2289 | 2017-02-23 07:44:17 +0000 | [diff] [blame] | 603 | DEFINE_WAIT(exec); |
| 604 | |
Chris Wilson | cced5e2 | 2017-02-23 07:44:15 +0000 | [diff] [blame] | 605 | if (kthread_should_stop()) { |
| 606 | GEM_BUG_ON(request); |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 607 | break; |
Chris Wilson | cced5e2 | 2017-02-23 07:44:15 +0000 | [diff] [blame] | 608 | } |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 609 | |
Chris Wilson | d6a2289 | 2017-02-23 07:44:17 +0000 | [diff] [blame] | 610 | if (request) |
| 611 | add_wait_queue(&request->execute, &exec); |
| 612 | |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 613 | schedule(); |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 614 | |
Chris Wilson | d6a2289 | 2017-02-23 07:44:17 +0000 | [diff] [blame] | 615 | if (request) |
| 616 | remove_wait_queue(&request->execute, &exec); |
| 617 | |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 618 | if (kthread_should_park()) |
| 619 | kthread_parkme(); |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 620 | } |
Chris Wilson | cced5e2 | 2017-02-23 07:44:15 +0000 | [diff] [blame] | 621 | i915_gem_request_put(request); |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 622 | } while (1); |
| 623 | __set_current_state(TASK_RUNNING); |
| 624 | |
| 625 | return 0; |
| 626 | } |
| 627 | |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame] | 628 | void intel_engine_enable_signaling(struct drm_i915_gem_request *request) |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 629 | { |
| 630 | struct intel_engine_cs *engine = request->engine; |
| 631 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 632 | struct rb_node *parent, **p; |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 633 | bool first, wakeup; |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 634 | u32 seqno; |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 635 | |
Chris Wilson | f6168e3 | 2016-10-28 13:58:55 +0100 | [diff] [blame] | 636 | /* Note that we may be called from an interrupt handler on another |
| 637 | * device (e.g. nouveau signaling a fence completion causing us |
| 638 | * to submit a request, and so enable signaling). As such, |
| 639 | * we need to make sure that all other users of b->lock protect |
| 640 | * against interrupts, i.e. use spin_lock_irqsave. |
| 641 | */ |
| 642 | |
| 643 | /* locked by dma_fence_enable_sw_signaling() (irqsafe fence->lock) */ |
Chris Wilson | e60a870 | 2017-03-02 11:51:30 +0000 | [diff] [blame] | 644 | GEM_BUG_ON(!irqs_disabled()); |
Chris Wilson | 6752041 | 2017-03-02 13:28:01 +0000 | [diff] [blame] | 645 | lockdep_assert_held(&request->lock); |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 646 | |
| 647 | seqno = i915_gem_request_global_seqno(request); |
| 648 | if (!seqno) |
Chris Wilson | 65e4760 | 2016-10-28 13:58:49 +0100 | [diff] [blame] | 649 | return; |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 650 | |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame] | 651 | request->signaling.wait.tsk = b->signaler; |
Chris Wilson | 56299fb | 2017-02-27 20:58:48 +0000 | [diff] [blame] | 652 | request->signaling.wait.request = request; |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 653 | request->signaling.wait.seqno = seqno; |
Chris Wilson | e8a261e | 2016-07-20 13:31:49 +0100 | [diff] [blame] | 654 | i915_gem_request_get(request); |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 655 | |
Chris Wilson | 4a50d20 | 2016-07-26 12:01:50 +0100 | [diff] [blame] | 656 | spin_lock(&b->lock); |
| 657 | |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 658 | /* First add ourselves into the list of waiters, but register our |
| 659 | * bottom-half as the signaller thread. As per usual, only the oldest |
| 660 | * waiter (not just signaller) is tasked as the bottom-half waking |
| 661 | * up all completed waiters after the user interrupt. |
| 662 | * |
| 663 | * If we are the oldest waiter, enable the irq (after which we |
| 664 | * must double check that the seqno did not complete). |
| 665 | */ |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame] | 666 | wakeup = __intel_engine_add_wait(engine, &request->signaling.wait); |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 667 | |
| 668 | /* Now insert ourselves into the retirement ordered list of signals |
| 669 | * on this engine. We track the oldest seqno as that will be the |
| 670 | * first signal to complete. |
| 671 | */ |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 672 | parent = NULL; |
| 673 | first = true; |
| 674 | p = &b->signals.rb_node; |
| 675 | while (*p) { |
| 676 | parent = *p; |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 677 | if (i915_seqno_passed(seqno, |
| 678 | to_signaler(parent)->signaling.wait.seqno)) { |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 679 | p = &parent->rb_right; |
| 680 | first = false; |
| 681 | } else { |
| 682 | p = &parent->rb_left; |
| 683 | } |
| 684 | } |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame] | 685 | rb_link_node(&request->signaling.node, parent, p); |
| 686 | rb_insert_color(&request->signaling.node, &b->signals); |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 687 | if (first) |
Chris Wilson | cced5e2 | 2017-02-23 07:44:15 +0000 | [diff] [blame] | 688 | rcu_assign_pointer(b->first_signal, request); |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame] | 689 | |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 690 | spin_unlock(&b->lock); |
| 691 | |
| 692 | if (wakeup) |
| 693 | wake_up_process(b->signaler); |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 694 | } |
| 695 | |
Chris Wilson | 9eb143b | 2017-02-23 07:44:16 +0000 | [diff] [blame] | 696 | void intel_engine_cancel_signaling(struct drm_i915_gem_request *request) |
| 697 | { |
| 698 | struct intel_engine_cs *engine = request->engine; |
| 699 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 700 | |
Chris Wilson | e60a870 | 2017-03-02 11:51:30 +0000 | [diff] [blame] | 701 | GEM_BUG_ON(!irqs_disabled()); |
Chris Wilson | 6752041 | 2017-03-02 13:28:01 +0000 | [diff] [blame] | 702 | lockdep_assert_held(&request->lock); |
Chris Wilson | 9eb143b | 2017-02-23 07:44:16 +0000 | [diff] [blame] | 703 | GEM_BUG_ON(!request->signaling.wait.seqno); |
| 704 | |
| 705 | spin_lock(&b->lock); |
| 706 | |
| 707 | if (!RB_EMPTY_NODE(&request->signaling.node)) { |
| 708 | if (request == rcu_access_pointer(b->first_signal)) { |
| 709 | struct rb_node *rb = |
| 710 | rb_next(&request->signaling.node); |
| 711 | rcu_assign_pointer(b->first_signal, |
| 712 | rb ? to_signaler(rb) : NULL); |
| 713 | } |
| 714 | rb_erase(&request->signaling.node, &b->signals); |
| 715 | RB_CLEAR_NODE(&request->signaling.node); |
| 716 | i915_gem_request_put(request); |
| 717 | } |
| 718 | |
| 719 | __intel_engine_remove_wait(engine, &request->signaling.wait); |
| 720 | |
| 721 | spin_unlock(&b->lock); |
| 722 | |
| 723 | request->signaling.wait.seqno = 0; |
| 724 | } |
| 725 | |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 726 | int intel_engine_init_breadcrumbs(struct intel_engine_cs *engine) |
| 727 | { |
| 728 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 729 | struct task_struct *tsk; |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 730 | |
| 731 | spin_lock_init(&b->lock); |
| 732 | setup_timer(&b->fake_irq, |
| 733 | intel_breadcrumbs_fake_irq, |
| 734 | (unsigned long)engine); |
Chris Wilson | 83348ba | 2016-08-09 17:47:51 +0100 | [diff] [blame] | 735 | setup_timer(&b->hangcheck, |
| 736 | intel_breadcrumbs_hangcheck, |
| 737 | (unsigned long)engine); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 738 | |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 739 | /* Spawn a thread to provide a common bottom-half for all signals. |
| 740 | * As this is an asynchronous interface we cannot steal the current |
| 741 | * task for handling the bottom-half to the user interrupt, therefore |
| 742 | * we create a thread to do the coherent seqno dance after the |
| 743 | * interrupt and then signal the waitqueue (via the dma-buf/fence). |
| 744 | */ |
| 745 | tsk = kthread_run(intel_breadcrumbs_signaler, engine, |
| 746 | "i915/signal:%d", engine->id); |
| 747 | if (IS_ERR(tsk)) |
| 748 | return PTR_ERR(tsk); |
| 749 | |
| 750 | b->signaler = tsk; |
| 751 | |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 752 | return 0; |
| 753 | } |
| 754 | |
Chris Wilson | ad07dfc | 2016-10-07 07:53:26 +0100 | [diff] [blame] | 755 | static void cancel_fake_irq(struct intel_engine_cs *engine) |
| 756 | { |
| 757 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 758 | |
| 759 | del_timer_sync(&b->hangcheck); |
| 760 | del_timer_sync(&b->fake_irq); |
| 761 | clear_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings); |
| 762 | } |
| 763 | |
| 764 | void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine) |
| 765 | { |
| 766 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 767 | |
| 768 | cancel_fake_irq(engine); |
Chris Wilson | f6168e3 | 2016-10-28 13:58:55 +0100 | [diff] [blame] | 769 | spin_lock_irq(&b->lock); |
Chris Wilson | ad07dfc | 2016-10-07 07:53:26 +0100 | [diff] [blame] | 770 | |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 771 | if (b->irq_enabled) |
| 772 | irq_enable(engine); |
| 773 | else |
Chris Wilson | ad07dfc | 2016-10-07 07:53:26 +0100 | [diff] [blame] | 774 | irq_disable(engine); |
Chris Wilson | 67b807a8 | 2017-02-27 20:58:50 +0000 | [diff] [blame] | 775 | |
| 776 | /* We set the IRQ_BREADCRUMB bit when we enable the irq presuming the |
| 777 | * GPU is active and may have already executed the MI_USER_INTERRUPT |
| 778 | * before the CPU is ready to receive. However, the engine is currently |
| 779 | * idle (we haven't started it yet), there is no possibility for a |
| 780 | * missed interrupt as we enabled the irq and so we can clear the |
| 781 | * immediate wakeup (until a real interrupt arrives for the waiter). |
| 782 | */ |
| 783 | clear_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted); |
| 784 | |
| 785 | if (b->irq_armed) |
| 786 | enable_fake_irq(b); |
Chris Wilson | ad07dfc | 2016-10-07 07:53:26 +0100 | [diff] [blame] | 787 | |
Chris Wilson | f6168e3 | 2016-10-28 13:58:55 +0100 | [diff] [blame] | 788 | spin_unlock_irq(&b->lock); |
Chris Wilson | ad07dfc | 2016-10-07 07:53:26 +0100 | [diff] [blame] | 789 | } |
| 790 | |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 791 | void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine) |
| 792 | { |
| 793 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 794 | |
Chris Wilson | 381744f | 2016-11-21 11:07:59 +0000 | [diff] [blame] | 795 | /* The engines should be idle and all requests accounted for! */ |
| 796 | WARN_ON(READ_ONCE(b->first_wait)); |
| 797 | WARN_ON(!RB_EMPTY_ROOT(&b->waiters)); |
Chris Wilson | cced5e2 | 2017-02-23 07:44:15 +0000 | [diff] [blame] | 798 | WARN_ON(rcu_access_pointer(b->first_signal)); |
Chris Wilson | 381744f | 2016-11-21 11:07:59 +0000 | [diff] [blame] | 799 | WARN_ON(!RB_EMPTY_ROOT(&b->signals)); |
| 800 | |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 801 | if (!IS_ERR_OR_NULL(b->signaler)) |
| 802 | kthread_stop(b->signaler); |
| 803 | |
Chris Wilson | ad07dfc | 2016-10-07 07:53:26 +0100 | [diff] [blame] | 804 | cancel_fake_irq(engine); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 805 | } |
| 806 | |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame] | 807 | bool intel_breadcrumbs_busy(struct intel_engine_cs *engine) |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 808 | { |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame] | 809 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 810 | bool busy = false; |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 811 | |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame] | 812 | spin_lock_irq(&b->lock); |
Chris Wilson | 6a5d1db | 2016-11-08 14:37:19 +0000 | [diff] [blame] | 813 | |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame] | 814 | if (b->first_wait) { |
| 815 | wake_up_process(b->first_wait->tsk); |
| 816 | busy |= intel_engine_flag(engine); |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 817 | } |
| 818 | |
Chris Wilson | cced5e2 | 2017-02-23 07:44:15 +0000 | [diff] [blame] | 819 | if (rcu_access_pointer(b->first_signal)) { |
Chris Wilson | 9b6586a | 2017-02-23 07:44:08 +0000 | [diff] [blame] | 820 | wake_up_process(b->signaler); |
| 821 | busy |= intel_engine_flag(engine); |
| 822 | } |
| 823 | |
| 824 | spin_unlock_irq(&b->lock); |
| 825 | |
| 826 | return busy; |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 827 | } |
Chris Wilson | f97fbf9 | 2017-02-13 17:15:14 +0000 | [diff] [blame] | 828 | |
| 829 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 830 | #include "selftests/intel_breadcrumbs.c" |
| 831 | #endif |