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