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 | |
| 29 | static void intel_breadcrumbs_fake_irq(unsigned long data) |
| 30 | { |
| 31 | struct intel_engine_cs *engine = (struct intel_engine_cs *)data; |
| 32 | |
| 33 | /* |
| 34 | * The timer persists in case we cannot enable interrupts, |
| 35 | * or if we have previously seen seqno/interrupt incoherency |
| 36 | * ("missed interrupt" syndrome). Here the worker will wake up |
| 37 | * every jiffie in order to kick the oldest waiter to do the |
| 38 | * coherent seqno check. |
| 39 | */ |
| 40 | rcu_read_lock(); |
| 41 | if (intel_engine_wakeup(engine)) |
| 42 | mod_timer(&engine->breadcrumbs.fake_irq, jiffies + 1); |
| 43 | rcu_read_unlock(); |
| 44 | } |
| 45 | |
| 46 | static void irq_enable(struct intel_engine_cs *engine) |
| 47 | { |
Chris Wilson | 3d5564e | 2016-07-01 17:23:23 +0100 | [diff] [blame] | 48 | /* Enabling the IRQ may miss the generation of the interrupt, but |
| 49 | * we still need to force the barrier before reading the seqno, |
| 50 | * just in case. |
| 51 | */ |
| 52 | engine->irq_posted = true; |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 53 | WARN_ON(!engine->irq_get(engine)); |
| 54 | } |
| 55 | |
| 56 | static void irq_disable(struct intel_engine_cs *engine) |
| 57 | { |
| 58 | engine->irq_put(engine); |
Chris Wilson | 3d5564e | 2016-07-01 17:23:23 +0100 | [diff] [blame] | 59 | engine->irq_posted = false; |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static bool __intel_breadcrumbs_enable_irq(struct intel_breadcrumbs *b) |
| 63 | { |
| 64 | struct intel_engine_cs *engine = |
| 65 | container_of(b, struct intel_engine_cs, breadcrumbs); |
| 66 | struct drm_i915_private *i915 = engine->i915; |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 67 | |
| 68 | assert_spin_locked(&b->lock); |
| 69 | if (b->rpm_wakelock) |
| 70 | return false; |
| 71 | |
| 72 | /* Since we are waiting on a request, the GPU should be busy |
| 73 | * and should have its own rpm reference. For completeness, |
| 74 | * record an rpm reference for ourselves to cover the |
| 75 | * interrupt we unmask. |
| 76 | */ |
| 77 | intel_runtime_pm_get_noresume(i915); |
| 78 | b->rpm_wakelock = true; |
| 79 | |
| 80 | /* No interrupts? Kick the waiter every jiffie! */ |
| 81 | if (intel_irqs_enabled(i915)) { |
Chris Wilson | 3d5564e | 2016-07-01 17:23:23 +0100 | [diff] [blame] | 82 | if (!test_bit(engine->id, &i915->gpu_error.test_irq_rings)) |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 83 | irq_enable(engine); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 84 | b->irq_enabled = true; |
| 85 | } |
| 86 | |
| 87 | if (!b->irq_enabled || |
| 88 | test_bit(engine->id, &i915->gpu_error.missed_irq_rings)) |
| 89 | mod_timer(&b->fake_irq, jiffies + 1); |
| 90 | |
Chris Wilson | 3d5564e | 2016-07-01 17:23:23 +0100 | [diff] [blame] | 91 | return engine->irq_posted; |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static void __intel_breadcrumbs_disable_irq(struct intel_breadcrumbs *b) |
| 95 | { |
| 96 | struct intel_engine_cs *engine = |
| 97 | container_of(b, struct intel_engine_cs, breadcrumbs); |
| 98 | |
| 99 | assert_spin_locked(&b->lock); |
| 100 | if (!b->rpm_wakelock) |
| 101 | return; |
| 102 | |
| 103 | if (b->irq_enabled) { |
| 104 | irq_disable(engine); |
| 105 | b->irq_enabled = false; |
| 106 | } |
| 107 | |
| 108 | intel_runtime_pm_put(engine->i915); |
| 109 | b->rpm_wakelock = false; |
| 110 | } |
| 111 | |
| 112 | static inline struct intel_wait *to_wait(struct rb_node *node) |
| 113 | { |
| 114 | return container_of(node, struct intel_wait, node); |
| 115 | } |
| 116 | |
| 117 | static inline void __intel_breadcrumbs_finish(struct intel_breadcrumbs *b, |
| 118 | struct intel_wait *wait) |
| 119 | { |
| 120 | assert_spin_locked(&b->lock); |
| 121 | |
| 122 | /* This request is completed, so remove it from the tree, mark it as |
| 123 | * complete, and *then* wake up the associated task. |
| 124 | */ |
| 125 | rb_erase(&wait->node, &b->waiters); |
| 126 | RB_CLEAR_NODE(&wait->node); |
| 127 | |
| 128 | wake_up_process(wait->tsk); /* implicit smp_wmb() */ |
| 129 | } |
| 130 | |
| 131 | static bool __intel_engine_add_wait(struct intel_engine_cs *engine, |
| 132 | struct intel_wait *wait) |
| 133 | { |
| 134 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 135 | struct rb_node **p, *parent, *completed; |
| 136 | bool first; |
| 137 | u32 seqno; |
| 138 | |
| 139 | /* Insert the request into the retirement ordered list |
| 140 | * of waiters by walking the rbtree. If we are the oldest |
| 141 | * seqno in the tree (the first to be retired), then |
| 142 | * set ourselves as the bottom-half. |
| 143 | * |
| 144 | * As we descend the tree, prune completed branches since we hold the |
| 145 | * spinlock we know that the first_waiter must be delayed and can |
| 146 | * reduce some of the sequential wake up latency if we take action |
| 147 | * ourselves and wake up the completed tasks in parallel. Also, by |
| 148 | * removing stale elements in the tree, we may be able to reduce the |
| 149 | * ping-pong between the old bottom-half and ourselves as first-waiter. |
| 150 | */ |
| 151 | first = true; |
| 152 | parent = NULL; |
| 153 | completed = NULL; |
Chris Wilson | 1b7744e | 2016-07-01 17:23:17 +0100 | [diff] [blame] | 154 | seqno = intel_engine_get_seqno(engine); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 155 | |
| 156 | /* If the request completed before we managed to grab the spinlock, |
| 157 | * return now before adding ourselves to the rbtree. We let the |
| 158 | * current bottom-half handle any pending wakeups and instead |
| 159 | * try and get out of the way quickly. |
| 160 | */ |
| 161 | if (i915_seqno_passed(seqno, wait->seqno)) { |
| 162 | RB_CLEAR_NODE(&wait->node); |
| 163 | return first; |
| 164 | } |
| 165 | |
| 166 | p = &b->waiters.rb_node; |
| 167 | while (*p) { |
| 168 | parent = *p; |
| 169 | if (wait->seqno == to_wait(parent)->seqno) { |
| 170 | /* We have multiple waiters on the same seqno, select |
| 171 | * the highest priority task (that with the smallest |
| 172 | * task->prio) to serve as the bottom-half for this |
| 173 | * group. |
| 174 | */ |
| 175 | if (wait->tsk->prio > to_wait(parent)->tsk->prio) { |
| 176 | p = &parent->rb_right; |
| 177 | first = false; |
| 178 | } else { |
| 179 | p = &parent->rb_left; |
| 180 | } |
| 181 | } else if (i915_seqno_passed(wait->seqno, |
| 182 | to_wait(parent)->seqno)) { |
| 183 | p = &parent->rb_right; |
| 184 | if (i915_seqno_passed(seqno, to_wait(parent)->seqno)) |
| 185 | completed = parent; |
| 186 | else |
| 187 | first = false; |
| 188 | } else { |
| 189 | p = &parent->rb_left; |
| 190 | } |
| 191 | } |
| 192 | rb_link_node(&wait->node, parent, p); |
| 193 | rb_insert_color(&wait->node, &b->waiters); |
| 194 | GEM_BUG_ON(!first && !b->tasklet); |
| 195 | |
| 196 | if (completed) { |
| 197 | struct rb_node *next = rb_next(completed); |
| 198 | |
| 199 | GEM_BUG_ON(!next && !first); |
| 200 | if (next && next != &wait->node) { |
| 201 | GEM_BUG_ON(first); |
| 202 | b->first_wait = to_wait(next); |
| 203 | smp_store_mb(b->tasklet, b->first_wait->tsk); |
| 204 | /* As there is a delay between reading the current |
| 205 | * seqno, processing the completed tasks and selecting |
| 206 | * the next waiter, we may have missed the interrupt |
| 207 | * and so need for the next bottom-half to wakeup. |
| 208 | * |
| 209 | * Also as we enable the IRQ, we may miss the |
| 210 | * interrupt for that seqno, so we have to wake up |
| 211 | * the next bottom-half in order to do a coherent check |
| 212 | * in case the seqno passed. |
| 213 | */ |
| 214 | __intel_breadcrumbs_enable_irq(b); |
Chris Wilson | 3d5564e | 2016-07-01 17:23:23 +0100 | [diff] [blame] | 215 | if (READ_ONCE(engine->irq_posted)) |
| 216 | wake_up_process(to_wait(next)->tsk); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | do { |
| 220 | struct intel_wait *crumb = to_wait(completed); |
| 221 | completed = rb_prev(completed); |
| 222 | __intel_breadcrumbs_finish(b, crumb); |
| 223 | } while (completed); |
| 224 | } |
| 225 | |
| 226 | if (first) { |
| 227 | GEM_BUG_ON(rb_first(&b->waiters) != &wait->node); |
| 228 | b->first_wait = wait; |
| 229 | smp_store_mb(b->tasklet, wait->tsk); |
| 230 | first = __intel_breadcrumbs_enable_irq(b); |
| 231 | } |
| 232 | GEM_BUG_ON(!b->tasklet); |
| 233 | GEM_BUG_ON(!b->first_wait); |
| 234 | GEM_BUG_ON(rb_first(&b->waiters) != &b->first_wait->node); |
| 235 | |
| 236 | return first; |
| 237 | } |
| 238 | |
| 239 | bool intel_engine_add_wait(struct intel_engine_cs *engine, |
| 240 | struct intel_wait *wait) |
| 241 | { |
| 242 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 243 | bool first; |
| 244 | |
| 245 | spin_lock(&b->lock); |
| 246 | first = __intel_engine_add_wait(engine, wait); |
| 247 | spin_unlock(&b->lock); |
| 248 | |
| 249 | return first; |
| 250 | } |
| 251 | |
| 252 | void intel_engine_enable_fake_irq(struct intel_engine_cs *engine) |
| 253 | { |
| 254 | mod_timer(&engine->breadcrumbs.fake_irq, jiffies + 1); |
| 255 | } |
| 256 | |
| 257 | static inline bool chain_wakeup(struct rb_node *rb, int priority) |
| 258 | { |
| 259 | return rb && to_wait(rb)->tsk->prio <= priority; |
| 260 | } |
| 261 | |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 262 | static inline int wakeup_priority(struct intel_breadcrumbs *b, |
| 263 | struct task_struct *tsk) |
| 264 | { |
| 265 | if (tsk == b->signaler) |
| 266 | return INT_MIN; |
| 267 | else |
| 268 | return tsk->prio; |
| 269 | } |
| 270 | |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 271 | void intel_engine_remove_wait(struct intel_engine_cs *engine, |
| 272 | struct intel_wait *wait) |
| 273 | { |
| 274 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 275 | |
| 276 | /* Quick check to see if this waiter was already decoupled from |
| 277 | * the tree by the bottom-half to avoid contention on the spinlock |
| 278 | * by the herd. |
| 279 | */ |
| 280 | if (RB_EMPTY_NODE(&wait->node)) |
| 281 | return; |
| 282 | |
| 283 | spin_lock(&b->lock); |
| 284 | |
| 285 | if (RB_EMPTY_NODE(&wait->node)) |
| 286 | goto out_unlock; |
| 287 | |
| 288 | if (b->first_wait == wait) { |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 289 | const int priority = wakeup_priority(b, wait->tsk); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 290 | struct rb_node *next; |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 291 | |
| 292 | GEM_BUG_ON(b->tasklet != wait->tsk); |
| 293 | |
| 294 | /* We are the current bottom-half. Find the next candidate, |
| 295 | * the first waiter in the queue on the remaining oldest |
| 296 | * request. As multiple seqnos may complete in the time it |
| 297 | * takes us to wake up and find the next waiter, we have to |
| 298 | * wake up that waiter for it to perform its own coherent |
| 299 | * completion check. |
| 300 | */ |
| 301 | next = rb_next(&wait->node); |
| 302 | if (chain_wakeup(next, priority)) { |
| 303 | /* If the next waiter is already complete, |
| 304 | * wake it up and continue onto the next waiter. So |
| 305 | * if have a small herd, they will wake up in parallel |
| 306 | * rather than sequentially, which should reduce |
| 307 | * the overall latency in waking all the completed |
| 308 | * clients. |
| 309 | * |
| 310 | * However, waking up a chain adds extra latency to |
| 311 | * the first_waiter. This is undesirable if that |
| 312 | * waiter is a high priority task. |
| 313 | */ |
Chris Wilson | 1b7744e | 2016-07-01 17:23:17 +0100 | [diff] [blame] | 314 | u32 seqno = intel_engine_get_seqno(engine); |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 315 | |
| 316 | while (i915_seqno_passed(seqno, to_wait(next)->seqno)) { |
| 317 | struct rb_node *n = rb_next(next); |
| 318 | |
| 319 | __intel_breadcrumbs_finish(b, to_wait(next)); |
| 320 | next = n; |
| 321 | if (!chain_wakeup(next, priority)) |
| 322 | break; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | if (next) { |
| 327 | /* In our haste, we may have completed the first waiter |
| 328 | * before we enabled the interrupt. Do so now as we |
| 329 | * have a second waiter for a future seqno. Afterwards, |
| 330 | * we have to wake up that waiter in case we missed |
| 331 | * the interrupt, or if we have to handle an |
| 332 | * exception rather than a seqno completion. |
| 333 | */ |
| 334 | b->first_wait = to_wait(next); |
| 335 | smp_store_mb(b->tasklet, b->first_wait->tsk); |
| 336 | if (b->first_wait->seqno != wait->seqno) |
| 337 | __intel_breadcrumbs_enable_irq(b); |
| 338 | wake_up_process(b->tasklet); |
| 339 | } else { |
| 340 | b->first_wait = NULL; |
| 341 | WRITE_ONCE(b->tasklet, NULL); |
| 342 | __intel_breadcrumbs_disable_irq(b); |
| 343 | } |
| 344 | } else { |
| 345 | GEM_BUG_ON(rb_first(&b->waiters) == &wait->node); |
| 346 | } |
| 347 | |
| 348 | GEM_BUG_ON(RB_EMPTY_NODE(&wait->node)); |
| 349 | rb_erase(&wait->node, &b->waiters); |
| 350 | |
| 351 | out_unlock: |
| 352 | GEM_BUG_ON(b->first_wait == wait); |
| 353 | GEM_BUG_ON(rb_first(&b->waiters) != |
| 354 | (b->first_wait ? &b->first_wait->node : NULL)); |
| 355 | GEM_BUG_ON(!b->tasklet ^ RB_EMPTY_ROOT(&b->waiters)); |
| 356 | spin_unlock(&b->lock); |
| 357 | } |
| 358 | |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame^] | 359 | static bool signal_complete(struct drm_i915_gem_request *request) |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 360 | { |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame^] | 361 | if (!request) |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 362 | return false; |
| 363 | |
| 364 | /* If another process served as the bottom-half it may have already |
| 365 | * signalled that this wait is already completed. |
| 366 | */ |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame^] | 367 | if (intel_wait_complete(&request->signaling.wait)) |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 368 | return true; |
| 369 | |
| 370 | /* Carefully check if the request is complete, giving time for the |
| 371 | * seqno to be visible or if the GPU hung. |
| 372 | */ |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame^] | 373 | if (__i915_request_irq_complete(request)) |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 374 | return true; |
| 375 | |
| 376 | return false; |
| 377 | } |
| 378 | |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame^] | 379 | static struct drm_i915_gem_request *to_signaler(struct rb_node *rb) |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 380 | { |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame^] | 381 | return container_of(rb, struct drm_i915_gem_request, signaling.node); |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | static void signaler_set_rtpriority(void) |
| 385 | { |
| 386 | struct sched_param param = { .sched_priority = 1 }; |
| 387 | |
| 388 | sched_setscheduler_nocheck(current, SCHED_FIFO, ¶m); |
| 389 | } |
| 390 | |
| 391 | static int intel_breadcrumbs_signaler(void *arg) |
| 392 | { |
| 393 | struct intel_engine_cs *engine = arg; |
| 394 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame^] | 395 | struct drm_i915_gem_request *request; |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 396 | |
| 397 | /* Install ourselves with high priority to reduce signalling latency */ |
| 398 | signaler_set_rtpriority(); |
| 399 | |
| 400 | do { |
| 401 | set_current_state(TASK_INTERRUPTIBLE); |
| 402 | |
| 403 | /* We are either woken up by the interrupt bottom-half, |
| 404 | * or by a client adding a new signaller. In both cases, |
| 405 | * the GPU seqno may have advanced beyond our oldest signal. |
| 406 | * If it has, propagate the signal, remove the waiter and |
| 407 | * check again with the next oldest signal. Otherwise we |
| 408 | * need to wait for a new interrupt from the GPU or for |
| 409 | * a new client. |
| 410 | */ |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame^] | 411 | request = READ_ONCE(b->first_signal); |
| 412 | if (signal_complete(request)) { |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 413 | /* Wake up all other completed waiters and select the |
| 414 | * next bottom-half for the next user interrupt. |
| 415 | */ |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame^] | 416 | intel_engine_remove_wait(engine, |
| 417 | &request->signaling.wait); |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 418 | |
| 419 | /* Find the next oldest signal. Note that as we have |
| 420 | * not been holding the lock, another client may |
| 421 | * have installed an even older signal than the one |
| 422 | * we just completed - so double check we are still |
| 423 | * the oldest before picking the next one. |
| 424 | */ |
| 425 | spin_lock(&b->lock); |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame^] | 426 | if (request == b->first_signal) { |
| 427 | struct rb_node *rb = |
| 428 | rb_next(&request->signaling.node); |
| 429 | b->first_signal = rb ? to_signaler(rb) : NULL; |
| 430 | } |
| 431 | rb_erase(&request->signaling.node, &b->signals); |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 432 | spin_unlock(&b->lock); |
| 433 | |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame^] | 434 | i915_gem_request_unreference(request); |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 435 | } else { |
| 436 | if (kthread_should_stop()) |
| 437 | break; |
| 438 | |
| 439 | schedule(); |
| 440 | } |
| 441 | } while (1); |
| 442 | __set_current_state(TASK_RUNNING); |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame^] | 447 | void intel_engine_enable_signaling(struct drm_i915_gem_request *request) |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 448 | { |
| 449 | struct intel_engine_cs *engine = request->engine; |
| 450 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 451 | struct rb_node *parent, **p; |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 452 | bool first, wakeup; |
| 453 | |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame^] | 454 | if (unlikely(READ_ONCE(request->signaling.wait.tsk))) |
| 455 | return; |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 456 | |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame^] | 457 | spin_lock(&b->lock); |
| 458 | if (unlikely(request->signaling.wait.tsk)) { |
| 459 | wakeup = false; |
| 460 | goto unlock; |
| 461 | } |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 462 | |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame^] | 463 | request->signaling.wait.tsk = b->signaler; |
| 464 | request->signaling.wait.seqno = request->seqno; |
| 465 | i915_gem_request_reference(request); |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 466 | |
| 467 | /* First add ourselves into the list of waiters, but register our |
| 468 | * bottom-half as the signaller thread. As per usual, only the oldest |
| 469 | * waiter (not just signaller) is tasked as the bottom-half waking |
| 470 | * up all completed waiters after the user interrupt. |
| 471 | * |
| 472 | * If we are the oldest waiter, enable the irq (after which we |
| 473 | * must double check that the seqno did not complete). |
| 474 | */ |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame^] | 475 | wakeup = __intel_engine_add_wait(engine, &request->signaling.wait); |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 476 | |
| 477 | /* Now insert ourselves into the retirement ordered list of signals |
| 478 | * on this engine. We track the oldest seqno as that will be the |
| 479 | * first signal to complete. |
| 480 | */ |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 481 | parent = NULL; |
| 482 | first = true; |
| 483 | p = &b->signals.rb_node; |
| 484 | while (*p) { |
| 485 | parent = *p; |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame^] | 486 | if (i915_seqno_passed(request->seqno, |
| 487 | to_signaler(parent)->seqno)) { |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 488 | p = &parent->rb_right; |
| 489 | first = false; |
| 490 | } else { |
| 491 | p = &parent->rb_left; |
| 492 | } |
| 493 | } |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame^] | 494 | rb_link_node(&request->signaling.node, parent, p); |
| 495 | rb_insert_color(&request->signaling.node, &b->signals); |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 496 | if (first) |
Chris Wilson | b385085 | 2016-07-01 17:23:26 +0100 | [diff] [blame^] | 497 | smp_store_mb(b->first_signal, request); |
| 498 | |
| 499 | unlock: |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 500 | spin_unlock(&b->lock); |
| 501 | |
| 502 | if (wakeup) |
| 503 | wake_up_process(b->signaler); |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 504 | } |
| 505 | |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 506 | int intel_engine_init_breadcrumbs(struct intel_engine_cs *engine) |
| 507 | { |
| 508 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 509 | struct task_struct *tsk; |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 510 | |
| 511 | spin_lock_init(&b->lock); |
| 512 | setup_timer(&b->fake_irq, |
| 513 | intel_breadcrumbs_fake_irq, |
| 514 | (unsigned long)engine); |
| 515 | |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 516 | /* Spawn a thread to provide a common bottom-half for all signals. |
| 517 | * As this is an asynchronous interface we cannot steal the current |
| 518 | * task for handling the bottom-half to the user interrupt, therefore |
| 519 | * we create a thread to do the coherent seqno dance after the |
| 520 | * interrupt and then signal the waitqueue (via the dma-buf/fence). |
| 521 | */ |
| 522 | tsk = kthread_run(intel_breadcrumbs_signaler, engine, |
| 523 | "i915/signal:%d", engine->id); |
| 524 | if (IS_ERR(tsk)) |
| 525 | return PTR_ERR(tsk); |
| 526 | |
| 527 | b->signaler = tsk; |
| 528 | |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine) |
| 533 | { |
| 534 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 535 | |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 536 | if (!IS_ERR_OR_NULL(b->signaler)) |
| 537 | kthread_stop(b->signaler); |
| 538 | |
Chris Wilson | 688e6c7 | 2016-07-01 17:23:15 +0100 | [diff] [blame] | 539 | del_timer_sync(&b->fake_irq); |
| 540 | } |
| 541 | |
| 542 | unsigned int intel_kick_waiters(struct drm_i915_private *i915) |
| 543 | { |
| 544 | struct intel_engine_cs *engine; |
| 545 | unsigned int mask = 0; |
| 546 | |
| 547 | /* To avoid the task_struct disappearing beneath us as we wake up |
| 548 | * the process, we must first inspect the task_struct->state under the |
| 549 | * RCU lock, i.e. as we call wake_up_process() we must be holding the |
| 550 | * rcu_read_lock(). |
| 551 | */ |
| 552 | rcu_read_lock(); |
| 553 | for_each_engine(engine, i915) |
| 554 | if (unlikely(intel_engine_wakeup(engine))) |
| 555 | mask |= intel_engine_flag(engine); |
| 556 | rcu_read_unlock(); |
| 557 | |
| 558 | return mask; |
| 559 | } |
Chris Wilson | c81d461 | 2016-07-01 17:23:25 +0100 | [diff] [blame] | 560 | |
| 561 | unsigned int intel_kick_signalers(struct drm_i915_private *i915) |
| 562 | { |
| 563 | struct intel_engine_cs *engine; |
| 564 | unsigned int mask = 0; |
| 565 | |
| 566 | for_each_engine(engine, i915) { |
| 567 | if (unlikely(READ_ONCE(engine->breadcrumbs.first_signal))) { |
| 568 | wake_up_process(engine->breadcrumbs.signaler); |
| 569 | mask |= intel_engine_flag(engine); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | return mask; |
| 574 | } |