drm/i915: set "ret" correctly on error paths
[linux-2.6-block.git] / drivers / gpu / drm / i915 / i915_gem_request.c
CommitLineData
05235c53
CW
1/*
2 * Copyright © 2008-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
fa545cbf 25#include <linux/prefetch.h>
b52992c0 26#include <linux/dma-fence-array.h>
e6017571
IM
27#include <linux/sched.h>
28#include <linux/sched/clock.h>
f361bf4a 29#include <linux/sched/signal.h>
fa545cbf 30
05235c53
CW
31#include "i915_drv.h"
32
f54d1867 33static const char *i915_fence_get_driver_name(struct dma_fence *fence)
04769652
CW
34{
35 return "i915";
36}
37
f54d1867 38static const char *i915_fence_get_timeline_name(struct dma_fence *fence)
04769652 39{
05506b5b
CW
40 /* The timeline struct (as part of the ppgtt underneath a context)
41 * may be freed when the request is no longer in use by the GPU.
42 * We could extend the life of a context to beyond that of all
43 * fences, possibly keeping the hw resource around indefinitely,
44 * or we just give them a false name. Since
45 * dma_fence_ops.get_timeline_name is a debug feature, the occasional
46 * lie seems justifiable.
47 */
48 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
49 return "signaled";
50
73cb9701 51 return to_request(fence)->timeline->common->name;
04769652
CW
52}
53
f54d1867 54static bool i915_fence_signaled(struct dma_fence *fence)
04769652
CW
55{
56 return i915_gem_request_completed(to_request(fence));
57}
58
f54d1867 59static bool i915_fence_enable_signaling(struct dma_fence *fence)
04769652
CW
60{
61 if (i915_fence_signaled(fence))
62 return false;
63
64 intel_engine_enable_signaling(to_request(fence));
65 return true;
66}
67
f54d1867 68static signed long i915_fence_wait(struct dma_fence *fence,
04769652 69 bool interruptible,
e95433c7 70 signed long timeout)
04769652 71{
e95433c7 72 return i915_wait_request(to_request(fence), interruptible, timeout);
04769652
CW
73}
74
f54d1867 75static void i915_fence_release(struct dma_fence *fence)
04769652
CW
76{
77 struct drm_i915_gem_request *req = to_request(fence);
78
fc158405
CW
79 /* The request is put onto a RCU freelist (i.e. the address
80 * is immediately reused), mark the fences as being freed now.
81 * Otherwise the debugobjects for the fences are only marked as
82 * freed when the slab cache itself is freed, and so we would get
83 * caught trying to reuse dead objects.
84 */
85 i915_sw_fence_fini(&req->submit);
fc158405 86
04769652
CW
87 kmem_cache_free(req->i915->requests, req);
88}
89
f54d1867 90const struct dma_fence_ops i915_fence_ops = {
04769652
CW
91 .get_driver_name = i915_fence_get_driver_name,
92 .get_timeline_name = i915_fence_get_timeline_name,
93 .enable_signaling = i915_fence_enable_signaling,
94 .signaled = i915_fence_signaled,
95 .wait = i915_fence_wait,
96 .release = i915_fence_release,
04769652
CW
97};
98
05235c53
CW
99static inline void
100i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
101{
c8659efa 102 struct drm_i915_file_private *file_priv;
05235c53 103
c8659efa 104 file_priv = request->file_priv;
05235c53
CW
105 if (!file_priv)
106 return;
107
108 spin_lock(&file_priv->mm.lock);
c8659efa
CW
109 if (request->file_priv) {
110 list_del(&request->client_link);
111 request->file_priv = NULL;
112 }
05235c53 113 spin_unlock(&file_priv->mm.lock);
05235c53
CW
114}
115
52e54209
CW
116static struct i915_dependency *
117i915_dependency_alloc(struct drm_i915_private *i915)
118{
119 return kmem_cache_alloc(i915->dependencies, GFP_KERNEL);
120}
121
122static void
123i915_dependency_free(struct drm_i915_private *i915,
124 struct i915_dependency *dep)
125{
126 kmem_cache_free(i915->dependencies, dep);
127}
128
129static void
130__i915_priotree_add_dependency(struct i915_priotree *pt,
131 struct i915_priotree *signal,
132 struct i915_dependency *dep,
133 unsigned long flags)
134{
20311bd3 135 INIT_LIST_HEAD(&dep->dfs_link);
52e54209
CW
136 list_add(&dep->wait_link, &signal->waiters_list);
137 list_add(&dep->signal_link, &pt->signalers_list);
138 dep->signaler = signal;
139 dep->flags = flags;
140}
141
142static int
143i915_priotree_add_dependency(struct drm_i915_private *i915,
144 struct i915_priotree *pt,
145 struct i915_priotree *signal)
146{
147 struct i915_dependency *dep;
148
149 dep = i915_dependency_alloc(i915);
150 if (!dep)
151 return -ENOMEM;
152
153 __i915_priotree_add_dependency(pt, signal, dep, I915_DEPENDENCY_ALLOC);
154 return 0;
155}
156
157static void
158i915_priotree_fini(struct drm_i915_private *i915, struct i915_priotree *pt)
159{
160 struct i915_dependency *dep, *next;
161
20311bd3
CW
162 GEM_BUG_ON(!RB_EMPTY_NODE(&pt->node));
163
52e54209
CW
164 /* Everyone we depended upon (the fences we wait to be signaled)
165 * should retire before us and remove themselves from our list.
166 * However, retirement is run independently on each timeline and
167 * so we may be called out-of-order.
168 */
169 list_for_each_entry_safe(dep, next, &pt->signalers_list, signal_link) {
170 list_del(&dep->wait_link);
171 if (dep->flags & I915_DEPENDENCY_ALLOC)
172 i915_dependency_free(i915, dep);
173 }
174
175 /* Remove ourselves from everyone who depends upon us */
176 list_for_each_entry_safe(dep, next, &pt->waiters_list, wait_link) {
177 list_del(&dep->signal_link);
178 if (dep->flags & I915_DEPENDENCY_ALLOC)
179 i915_dependency_free(i915, dep);
180 }
181}
182
183static void
184i915_priotree_init(struct i915_priotree *pt)
185{
186 INIT_LIST_HEAD(&pt->signalers_list);
187 INIT_LIST_HEAD(&pt->waiters_list);
20311bd3
CW
188 RB_CLEAR_NODE(&pt->node);
189 pt->priority = INT_MIN;
52e54209
CW
190}
191
12d3173b
CW
192static int reset_all_global_seqno(struct drm_i915_private *i915, u32 seqno)
193{
12d3173b
CW
194 struct intel_engine_cs *engine;
195 enum intel_engine_id id;
196 int ret;
197
198 /* Carefully retire all requests without writing to the rings */
199 ret = i915_gem_wait_for_idle(i915,
200 I915_WAIT_INTERRUPTIBLE |
201 I915_WAIT_LOCKED);
202 if (ret)
203 return ret;
204
12d3173b
CW
205 /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
206 for_each_engine(engine, i915, id) {
ae351beb
CW
207 struct i915_gem_timeline *timeline;
208 struct intel_timeline *tl = engine->timeline;
12d3173b
CW
209
210 if (!i915_seqno_passed(seqno, tl->seqno)) {
211 /* spin until threads are complete */
212 while (intel_breadcrumbs_busy(engine))
213 cond_resched();
214 }
215
216 /* Finally reset hw state */
12d3173b 217 intel_engine_init_global_seqno(engine, seqno);
2ca9faa5 218 tl->seqno = seqno;
12d3173b 219
ae351beb
CW
220 list_for_each_entry(timeline, &i915->gt.timelines, link)
221 memset(timeline->engine[id].sync_seqno, 0,
222 sizeof(timeline->engine[id].sync_seqno));
12d3173b
CW
223 }
224
225 return 0;
226}
227
228int i915_gem_set_global_seqno(struct drm_device *dev, u32 seqno)
229{
230 struct drm_i915_private *dev_priv = to_i915(dev);
231
232 lockdep_assert_held(&dev_priv->drm.struct_mutex);
233
234 if (seqno == 0)
235 return -EINVAL;
236
237 /* HWS page needs to be set less than what we
238 * will inject to ring
239 */
240 return reset_all_global_seqno(dev_priv, seqno - 1);
241}
242
243static int reserve_seqno(struct intel_engine_cs *engine)
244{
245 u32 active = ++engine->timeline->inflight_seqnos;
246 u32 seqno = engine->timeline->seqno;
247 int ret;
248
249 /* Reservation is fine until we need to wrap around */
250 if (likely(!add_overflows(seqno, active)))
251 return 0;
252
253 ret = reset_all_global_seqno(engine->i915, 0);
254 if (ret) {
255 engine->timeline->inflight_seqnos--;
256 return ret;
257 }
258
259 return 0;
260}
261
9b6586ae
CW
262static void unreserve_seqno(struct intel_engine_cs *engine)
263{
264 GEM_BUG_ON(!engine->timeline->inflight_seqnos);
265 engine->timeline->inflight_seqnos--;
266}
267
fa545cbf
CW
268void i915_gem_retire_noop(struct i915_gem_active *active,
269 struct drm_i915_gem_request *request)
270{
271 /* Space left intentionally blank */
272}
273
cbb60b4b
CW
274static void advance_ring(struct drm_i915_gem_request *request)
275{
276 unsigned int tail;
277
278 /* We know the GPU must have read the request to have
279 * sent us the seqno + interrupt, so use the position
280 * of tail of the request to update the last known position
281 * of the GPU head.
282 *
283 * Note this requires that we are always called in request
284 * completion order.
285 */
286 if (list_is_last(&request->ring_link, &request->ring->request_list))
287 tail = request->ring->tail;
288 else
289 tail = request->postfix;
290 list_del(&request->ring_link);
291
292 request->ring->head = tail;
293}
294
05235c53
CW
295static void i915_gem_request_retire(struct drm_i915_gem_request *request)
296{
e8a9c58f 297 struct intel_engine_cs *engine = request->engine;
fa545cbf
CW
298 struct i915_gem_active *active, *next;
299
4c7d62c6 300 lockdep_assert_held(&request->i915->drm.struct_mutex);
48bc2a4a 301 GEM_BUG_ON(!i915_sw_fence_signaled(&request->submit));
4c7d62c6 302 GEM_BUG_ON(!i915_gem_request_completed(request));
4302055b 303 GEM_BUG_ON(!request->i915->gt.active_requests);
4c7d62c6 304
05235c53 305 trace_i915_gem_request_retire(request);
80b204bc 306
e8a9c58f 307 spin_lock_irq(&engine->timeline->lock);
e95433c7 308 list_del_init(&request->link);
e8a9c58f 309 spin_unlock_irq(&engine->timeline->lock);
05235c53 310
4302055b
CW
311 if (!--request->i915->gt.active_requests) {
312 GEM_BUG_ON(!request->i915->gt.awake);
313 mod_delayed_work(request->i915->wq,
314 &request->i915->gt.idle_work,
315 msecs_to_jiffies(100));
316 }
9b6586ae 317 unreserve_seqno(request->engine);
cbb60b4b 318 advance_ring(request);
05235c53 319
fa545cbf
CW
320 /* Walk through the active list, calling retire on each. This allows
321 * objects to track their GPU activity and mark themselves as idle
322 * when their *last* active request is completed (updating state
323 * tracking lists for eviction, active references for GEM, etc).
324 *
325 * As the ->retire() may free the node, we decouple it first and
326 * pass along the auxiliary information (to avoid dereferencing
327 * the node after the callback).
328 */
329 list_for_each_entry_safe(active, next, &request->active_list, link) {
330 /* In microbenchmarks or focusing upon time inside the kernel,
331 * we may spend an inordinate amount of time simply handling
332 * the retirement of requests and processing their callbacks.
333 * Of which, this loop itself is particularly hot due to the
334 * cache misses when jumping around the list of i915_gem_active.
335 * So we try to keep this loop as streamlined as possible and
336 * also prefetch the next i915_gem_active to try and hide
337 * the likely cache miss.
338 */
339 prefetchw(next);
340
341 INIT_LIST_HEAD(&active->link);
0eafec6d 342 RCU_INIT_POINTER(active->request, NULL);
fa545cbf
CW
343
344 active->retire(active, request);
345 }
346
05235c53
CW
347 i915_gem_request_remove_from_client(request);
348
e5e1fc47 349 /* Retirement decays the ban score as it is a sign of ctx progress */
bc1d53c6
MK
350 if (request->ctx->ban_score > 0)
351 request->ctx->ban_score--;
e5e1fc47 352
e8a9c58f
CW
353 /* The backing object for the context is done after switching to the
354 * *next* context. Therefore we cannot retire the previous context until
355 * the next context has already started running. However, since we
356 * cannot take the required locks at i915_gem_request_submit() we
357 * defer the unpinning of the active context to now, retirement of
358 * the subsequent request.
359 */
360 if (engine->last_retired_context)
361 engine->context_unpin(engine, engine->last_retired_context);
362 engine->last_retired_context = request->ctx;
d07f0e59
CW
363
364 dma_fence_signal(&request->fence);
52e54209
CW
365
366 i915_priotree_fini(request->i915, &request->priotree);
e8a261ea 367 i915_gem_request_put(request);
05235c53
CW
368}
369
370void i915_gem_request_retire_upto(struct drm_i915_gem_request *req)
371{
372 struct intel_engine_cs *engine = req->engine;
373 struct drm_i915_gem_request *tmp;
374
375 lockdep_assert_held(&req->i915->drm.struct_mutex);
4ffd6e0c
CW
376 GEM_BUG_ON(!i915_gem_request_completed(req));
377
e95433c7
CW
378 if (list_empty(&req->link))
379 return;
05235c53
CW
380
381 do {
73cb9701 382 tmp = list_first_entry(&engine->timeline->requests,
efdf7c06 383 typeof(*tmp), link);
05235c53
CW
384
385 i915_gem_request_retire(tmp);
386 } while (tmp != req);
05235c53
CW
387}
388
9b6586ae 389static u32 timeline_get_seqno(struct intel_timeline *tl)
05235c53 390{
9b6586ae 391 return ++tl->seqno;
28176ef4
CW
392}
393
d55ac5bf 394void __i915_gem_request_submit(struct drm_i915_gem_request *request)
5590af3e 395{
73cb9701 396 struct intel_engine_cs *engine = request->engine;
f2d13290
CW
397 struct intel_timeline *timeline;
398 u32 seqno;
5590af3e 399
e60a870d 400 GEM_BUG_ON(!irqs_disabled());
67520415 401 lockdep_assert_held(&engine->timeline->lock);
e60a870d 402
fe49789f
CW
403 trace_i915_gem_request_execute(request);
404
80b204bc
CW
405 /* Transfer from per-context onto the global per-engine timeline */
406 timeline = engine->timeline;
407 GEM_BUG_ON(timeline == request->timeline);
5590af3e 408
9b6586ae 409 seqno = timeline_get_seqno(timeline);
f2d13290
CW
410 GEM_BUG_ON(!seqno);
411 GEM_BUG_ON(i915_seqno_passed(intel_engine_get_seqno(engine), seqno));
412
f2d13290
CW
413 /* We may be recursing from the signal callback of another i915 fence */
414 spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
415 request->global_seqno = seqno;
416 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
417 intel_engine_enable_signaling(request);
418 spin_unlock(&request->lock);
419
caddfe71
CW
420 engine->emit_breadcrumb(request,
421 request->ring->vaddr + request->postfix);
5590af3e 422
bb89485e 423 spin_lock(&request->timeline->lock);
80b204bc
CW
424 list_move_tail(&request->link, &timeline->requests);
425 spin_unlock(&request->timeline->lock);
426
fe49789f 427 wake_up_all(&request->execute);
d55ac5bf
CW
428}
429
430void i915_gem_request_submit(struct drm_i915_gem_request *request)
431{
432 struct intel_engine_cs *engine = request->engine;
433 unsigned long flags;
23902e49 434
d55ac5bf
CW
435 /* Will be called from irq-context when using foreign fences. */
436 spin_lock_irqsave(&engine->timeline->lock, flags);
437
438 __i915_gem_request_submit(request);
439
440 spin_unlock_irqrestore(&engine->timeline->lock, flags);
441}
442
d6a2289d 443void __i915_gem_request_unsubmit(struct drm_i915_gem_request *request)
d55ac5bf 444{
d6a2289d
CW
445 struct intel_engine_cs *engine = request->engine;
446 struct intel_timeline *timeline;
d55ac5bf 447
e60a870d 448 GEM_BUG_ON(!irqs_disabled());
67520415 449 lockdep_assert_held(&engine->timeline->lock);
48bc2a4a 450
d6a2289d
CW
451 /* Only unwind in reverse order, required so that the per-context list
452 * is kept in seqno/ring order.
453 */
454 GEM_BUG_ON(request->global_seqno != engine->timeline->seqno);
455 engine->timeline->seqno--;
80b204bc 456
d6a2289d
CW
457 /* We may be recursing from the signal callback of another i915 fence */
458 spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
459 request->global_seqno = 0;
460 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
461 intel_engine_cancel_signaling(request);
462 spin_unlock(&request->lock);
463
464 /* Transfer back from the global per-engine timeline to per-context */
465 timeline = request->timeline;
466 GEM_BUG_ON(timeline == engine->timeline);
467
468 spin_lock(&timeline->lock);
469 list_move(&request->link, &timeline->requests);
470 spin_unlock(&timeline->lock);
471
472 /* We don't need to wake_up any waiters on request->execute, they
473 * will get woken by any other event or us re-adding this request
474 * to the engine timeline (__i915_gem_request_submit()). The waiters
475 * should be quite adapt at finding that the request now has a new
476 * global_seqno to the one they went to sleep on.
477 */
478}
479
480void i915_gem_request_unsubmit(struct drm_i915_gem_request *request)
481{
482 struct intel_engine_cs *engine = request->engine;
483 unsigned long flags;
484
485 /* Will be called from irq-context when using foreign fences. */
486 spin_lock_irqsave(&engine->timeline->lock, flags);
487
488 __i915_gem_request_unsubmit(request);
489
490 spin_unlock_irqrestore(&engine->timeline->lock, flags);
5590af3e
CW
491}
492
23902e49 493static int __i915_sw_fence_call
d55ac5bf 494submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
23902e49 495{
48bc2a4a 496 struct drm_i915_gem_request *request =
48bc2a4a 497 container_of(fence, typeof(*request), submit);
48bc2a4a
CW
498
499 switch (state) {
500 case FENCE_COMPLETE:
354d036f 501 trace_i915_gem_request_submit(request);
d55ac5bf 502 request->engine->submit_request(request);
48bc2a4a
CW
503 break;
504
505 case FENCE_FREE:
506 i915_gem_request_put(request);
507 break;
508 }
509
23902e49
CW
510 return NOTIFY_DONE;
511}
512
8e637178
CW
513/**
514 * i915_gem_request_alloc - allocate a request structure
515 *
516 * @engine: engine that we wish to issue the request on.
517 * @ctx: context that the request will be associated with.
518 * This can be NULL if the request is not directly related to
519 * any specific user context, in which case this function will
520 * choose an appropriate context to use.
521 *
522 * Returns a pointer to the allocated request if successful,
523 * or an error code if not.
524 */
525struct drm_i915_gem_request *
526i915_gem_request_alloc(struct intel_engine_cs *engine,
527 struct i915_gem_context *ctx)
05235c53
CW
528{
529 struct drm_i915_private *dev_priv = engine->i915;
05235c53
CW
530 struct drm_i915_gem_request *req;
531 int ret;
532
28176ef4
CW
533 lockdep_assert_held(&dev_priv->drm.struct_mutex);
534
05235c53 535 /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
6ffb7d07 536 * EIO if the GPU is already wedged.
05235c53 537 */
6ffb7d07
CW
538 if (i915_terminally_wedged(&dev_priv->gpu_error))
539 return ERR_PTR(-EIO);
05235c53 540
e8a9c58f
CW
541 /* Pinning the contexts may generate requests in order to acquire
542 * GGTT space, so do this first before we reserve a seqno for
543 * ourselves.
544 */
545 ret = engine->context_pin(engine, ctx);
28176ef4
CW
546 if (ret)
547 return ERR_PTR(ret);
548
9b6586ae 549 ret = reserve_seqno(engine);
e8a9c58f
CW
550 if (ret)
551 goto err_unpin;
552
9b5f4e5e 553 /* Move the oldest request to the slab-cache (if not in use!) */
73cb9701 554 req = list_first_entry_or_null(&engine->timeline->requests,
efdf7c06 555 typeof(*req), link);
754c9fd5 556 if (req && i915_gem_request_completed(req))
2a1d7752 557 i915_gem_request_retire(req);
9b5f4e5e 558
5a198b8c
CW
559 /* Beware: Dragons be flying overhead.
560 *
561 * We use RCU to look up requests in flight. The lookups may
562 * race with the request being allocated from the slab freelist.
563 * That is the request we are writing to here, may be in the process
1426f715 564 * of being read by __i915_gem_active_get_rcu(). As such,
5a198b8c
CW
565 * we have to be very careful when overwriting the contents. During
566 * the RCU lookup, we change chase the request->engine pointer,
65e4760e 567 * read the request->global_seqno and increment the reference count.
5a198b8c
CW
568 *
569 * The reference count is incremented atomically. If it is zero,
570 * the lookup knows the request is unallocated and complete. Otherwise,
571 * it is either still in use, or has been reallocated and reset
f54d1867
CW
572 * with dma_fence_init(). This increment is safe for release as we
573 * check that the request we have a reference to and matches the active
5a198b8c
CW
574 * request.
575 *
576 * Before we increment the refcount, we chase the request->engine
577 * pointer. We must not call kmem_cache_zalloc() or else we set
578 * that pointer to NULL and cause a crash during the lookup. If
579 * we see the request is completed (based on the value of the
580 * old engine and seqno), the lookup is complete and reports NULL.
581 * If we decide the request is not completed (new engine or seqno),
582 * then we grab a reference and double check that it is still the
583 * active request - which it won't be and restart the lookup.
584 *
585 * Do not use kmem_cache_zalloc() here!
586 */
587 req = kmem_cache_alloc(dev_priv->requests, GFP_KERNEL);
28176ef4
CW
588 if (!req) {
589 ret = -ENOMEM;
590 goto err_unreserve;
591 }
05235c53 592
80b204bc
CW
593 req->timeline = i915_gem_context_lookup_timeline(ctx, engine);
594 GEM_BUG_ON(req->timeline == engine->timeline);
73cb9701 595
04769652 596 spin_lock_init(&req->lock);
f54d1867
CW
597 dma_fence_init(&req->fence,
598 &i915_fence_ops,
599 &req->lock,
73cb9701 600 req->timeline->fence_context,
9b6586ae 601 timeline_get_seqno(req->timeline));
04769652 602
48bc2a4a
CW
603 /* We bump the ref for the fence chain */
604 i915_sw_fence_init(&i915_gem_request_get(req)->submit, submit_notify);
fe49789f 605 init_waitqueue_head(&req->execute);
5590af3e 606
52e54209
CW
607 i915_priotree_init(&req->priotree);
608
fa545cbf 609 INIT_LIST_HEAD(&req->active_list);
05235c53
CW
610 req->i915 = dev_priv;
611 req->engine = engine;
e8a9c58f 612 req->ctx = ctx;
05235c53 613
5a198b8c 614 /* No zalloc, must clear what we need by hand */
f2d13290 615 req->global_seqno = 0;
5a198b8c 616 req->file_priv = NULL;
058d88c4 617 req->batch = NULL;
5a198b8c 618
05235c53
CW
619 /*
620 * Reserve space in the ring buffer for all the commands required to
621 * eventually emit this request. This is to guarantee that the
622 * i915_add_request() call can't fail. Note that the reserve may need
623 * to be redone if the request is not actually submitted straight
624 * away, e.g. because a GPU scheduler has deferred it.
625 */
626 req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
98f29e8d 627 GEM_BUG_ON(req->reserved_space < engine->emit_breadcrumb_sz);
05235c53 628
f73e7399 629 ret = engine->request_alloc(req);
05235c53
CW
630 if (ret)
631 goto err_ctx;
632
d045446d
CW
633 /* Record the position of the start of the request so that
634 * should we detect the updated seqno part-way through the
635 * GPU processing the request, we never over-estimate the
636 * position of the head.
637 */
638 req->head = req->ring->tail;
639
9b6586ae
CW
640 /* Check that we didn't interrupt ourselves with a new request */
641 GEM_BUG_ON(req->timeline->seqno != req->fence.seqno);
8e637178 642 return req;
05235c53
CW
643
644err_ctx:
1618bdb8
CW
645 /* Make sure we didn't add ourselves to external state before freeing */
646 GEM_BUG_ON(!list_empty(&req->active_list));
647 GEM_BUG_ON(!list_empty(&req->priotree.signalers_list));
648 GEM_BUG_ON(!list_empty(&req->priotree.waiters_list));
649
05235c53 650 kmem_cache_free(dev_priv->requests, req);
28176ef4 651err_unreserve:
9b6586ae 652 unreserve_seqno(engine);
e8a9c58f
CW
653err_unpin:
654 engine->context_unpin(engine, ctx);
8e637178 655 return ERR_PTR(ret);
05235c53
CW
656}
657
a2bc4695
CW
658static int
659i915_gem_request_await_request(struct drm_i915_gem_request *to,
660 struct drm_i915_gem_request *from)
661{
754c9fd5 662 u32 seqno;
85e17f59 663 int ret;
a2bc4695
CW
664
665 GEM_BUG_ON(to == from);
666
52e54209
CW
667 if (to->engine->schedule) {
668 ret = i915_priotree_add_dependency(to->i915,
669 &to->priotree,
670 &from->priotree);
671 if (ret < 0)
672 return ret;
673 }
674
73cb9701 675 if (to->timeline == from->timeline)
a2bc4695
CW
676 return 0;
677
73cb9701
CW
678 if (to->engine == from->engine) {
679 ret = i915_sw_fence_await_sw_fence_gfp(&to->submit,
680 &from->submit,
681 GFP_KERNEL);
682 return ret < 0 ? ret : 0;
683 }
684
754c9fd5
CW
685 seqno = i915_gem_request_global_seqno(from);
686 if (!seqno) {
65e4760e
CW
687 ret = i915_sw_fence_await_dma_fence(&to->submit,
688 &from->fence, 0,
689 GFP_KERNEL);
690 return ret < 0 ? ret : 0;
691 }
692
754c9fd5 693 if (seqno <= to->timeline->sync_seqno[from->engine->id])
a2bc4695
CW
694 return 0;
695
696 trace_i915_gem_ring_sync_to(to, from);
697 if (!i915.semaphores) {
0a046a0e
CW
698 if (!i915_spin_request(from, TASK_INTERRUPTIBLE, 2)) {
699 ret = i915_sw_fence_await_dma_fence(&to->submit,
700 &from->fence, 0,
701 GFP_KERNEL);
702 if (ret < 0)
703 return ret;
704 }
a2bc4695
CW
705 } else {
706 ret = to->engine->semaphore.sync_to(to, from);
707 if (ret)
708 return ret;
709 }
710
754c9fd5 711 to->timeline->sync_seqno[from->engine->id] = seqno;
a2bc4695
CW
712 return 0;
713}
714
b52992c0
CW
715int
716i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
717 struct dma_fence *fence)
718{
719 struct dma_fence_array *array;
720 int ret;
721 int i;
722
723 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
724 return 0;
725
726 if (dma_fence_is_i915(fence))
727 return i915_gem_request_await_request(req, to_request(fence));
728
729 if (!dma_fence_is_array(fence)) {
730 ret = i915_sw_fence_await_dma_fence(&req->submit,
731 fence, I915_FENCE_TIMEOUT,
732 GFP_KERNEL);
733 return ret < 0 ? ret : 0;
734 }
735
736 /* Note that if the fence-array was created in signal-on-any mode,
737 * we should *not* decompose it into its individual fences. However,
738 * we don't currently store which mode the fence-array is operating
739 * in. Fortunately, the only user of signal-on-any is private to
740 * amdgpu and we should not see any incoming fence-array from
741 * sync-file being in signal-on-any mode.
742 */
743
744 array = to_dma_fence_array(fence);
745 for (i = 0; i < array->num_fences; i++) {
746 struct dma_fence *child = array->fences[i];
747
748 if (dma_fence_is_i915(child))
749 ret = i915_gem_request_await_request(req,
750 to_request(child));
751 else
752 ret = i915_sw_fence_await_dma_fence(&req->submit,
753 child, I915_FENCE_TIMEOUT,
754 GFP_KERNEL);
755 if (ret < 0)
756 return ret;
757 }
758
759 return 0;
760}
761
a2bc4695
CW
762/**
763 * i915_gem_request_await_object - set this request to (async) wait upon a bo
764 *
765 * @to: request we are wishing to use
766 * @obj: object which may be in use on another ring.
767 *
768 * This code is meant to abstract object synchronization with the GPU.
769 * Conceptually we serialise writes between engines inside the GPU.
770 * We only allow one engine to write into a buffer at any time, but
771 * multiple readers. To ensure each has a coherent view of memory, we must:
772 *
773 * - If there is an outstanding write request to the object, the new
774 * request must wait for it to complete (either CPU or in hw, requests
775 * on the same ring will be naturally ordered).
776 *
777 * - If we are a write request (pending_write_domain is set), the new
778 * request must wait for outstanding read requests to complete.
779 *
780 * Returns 0 if successful, else propagates up the lower layer error.
781 */
782int
783i915_gem_request_await_object(struct drm_i915_gem_request *to,
784 struct drm_i915_gem_object *obj,
785 bool write)
786{
d07f0e59
CW
787 struct dma_fence *excl;
788 int ret = 0;
a2bc4695
CW
789
790 if (write) {
d07f0e59
CW
791 struct dma_fence **shared;
792 unsigned int count, i;
793
794 ret = reservation_object_get_fences_rcu(obj->resv,
795 &excl, &count, &shared);
796 if (ret)
797 return ret;
798
799 for (i = 0; i < count; i++) {
800 ret = i915_gem_request_await_dma_fence(to, shared[i]);
801 if (ret)
802 break;
803
804 dma_fence_put(shared[i]);
805 }
806
807 for (; i < count; i++)
808 dma_fence_put(shared[i]);
809 kfree(shared);
a2bc4695 810 } else {
d07f0e59 811 excl = reservation_object_get_excl_rcu(obj->resv);
a2bc4695
CW
812 }
813
d07f0e59
CW
814 if (excl) {
815 if (ret == 0)
816 ret = i915_gem_request_await_dma_fence(to, excl);
a2bc4695 817
d07f0e59 818 dma_fence_put(excl);
a2bc4695
CW
819 }
820
d07f0e59 821 return ret;
a2bc4695
CW
822}
823
05235c53
CW
824static void i915_gem_mark_busy(const struct intel_engine_cs *engine)
825{
826 struct drm_i915_private *dev_priv = engine->i915;
827
05235c53
CW
828 if (dev_priv->gt.awake)
829 return;
830
4302055b
CW
831 GEM_BUG_ON(!dev_priv->gt.active_requests);
832
05235c53
CW
833 intel_runtime_pm_get_noresume(dev_priv);
834 dev_priv->gt.awake = true;
835
54b4f68f 836 intel_enable_gt_powersave(dev_priv);
05235c53
CW
837 i915_update_gfx_val(dev_priv);
838 if (INTEL_GEN(dev_priv) >= 6)
839 gen6_rps_busy(dev_priv);
840
841 queue_delayed_work(dev_priv->wq,
842 &dev_priv->gt.retire_work,
843 round_jiffies_up_relative(HZ));
844}
845
846/*
847 * NB: This function is not allowed to fail. Doing so would mean the the
848 * request is not being tracked for completion but the work itself is
849 * going to happen on the hardware. This would be a Bad Thing(tm).
850 */
17f298cf 851void __i915_add_request(struct drm_i915_gem_request *request, bool flush_caches)
05235c53 852{
95b2ab56
CW
853 struct intel_engine_cs *engine = request->engine;
854 struct intel_ring *ring = request->ring;
73cb9701 855 struct intel_timeline *timeline = request->timeline;
0a046a0e 856 struct drm_i915_gem_request *prev;
73dec95e 857 u32 *cs;
caddfe71 858 int err;
05235c53 859
4c7d62c6 860 lockdep_assert_held(&request->i915->drm.struct_mutex);
0f25dff6
CW
861 trace_i915_gem_request_add(request);
862
c781c978
CW
863 /* Make sure that no request gazumped us - if it was allocated after
864 * our i915_gem_request_alloc() and called __i915_add_request() before
865 * us, the timeline will hold its seqno which is later than ours.
866 */
9b6586ae 867 GEM_BUG_ON(timeline->seqno != request->fence.seqno);
c781c978 868
05235c53
CW
869 /*
870 * To ensure that this call will not fail, space for its emissions
871 * should already have been reserved in the ring buffer. Let the ring
872 * know that it is time to use that space up.
873 */
05235c53
CW
874 request->reserved_space = 0;
875
876 /*
877 * Emit any outstanding flushes - execbuf can fail to emit the flush
878 * after having emitted the batchbuffer command. Hence we need to fix
879 * things up similar to emitting the lazy request. The difference here
880 * is that the flush _must_ happen before the next request, no matter
881 * what.
882 */
883 if (flush_caches) {
caddfe71 884 err = engine->emit_flush(request, EMIT_FLUSH);
c7fe7d25 885
05235c53 886 /* Not allowed to fail! */
caddfe71 887 WARN(err, "engine->emit_flush() failed: %d!\n", err);
05235c53
CW
888 }
889
d045446d 890 /* Record the position of the start of the breadcrumb so that
05235c53
CW
891 * should we detect the updated seqno part-way through the
892 * GPU processing the request, we never over-estimate the
d045446d 893 * position of the ring's HEAD.
05235c53 894 */
73dec95e
TU
895 cs = intel_ring_begin(request, engine->emit_breadcrumb_sz);
896 GEM_BUG_ON(IS_ERR(cs));
897 request->postfix = intel_ring_offset(request, cs);
05235c53 898
0f25dff6
CW
899 /* Seal the request and mark it as pending execution. Note that
900 * we may inspect this state, without holding any locks, during
901 * hangcheck. Hence we apply the barrier to ensure that we do not
902 * see a more recent value in the hws than we are tracking.
903 */
0a046a0e 904
73cb9701 905 prev = i915_gem_active_raw(&timeline->last_request,
0a046a0e 906 &request->i915->drm.struct_mutex);
52e54209 907 if (prev) {
0a046a0e
CW
908 i915_sw_fence_await_sw_fence(&request->submit, &prev->submit,
909 &request->submitq);
52e54209
CW
910 if (engine->schedule)
911 __i915_priotree_add_dependency(&request->priotree,
912 &prev->priotree,
913 &request->dep,
914 0);
915 }
0a046a0e 916
80b204bc 917 spin_lock_irq(&timeline->lock);
f2d13290 918 list_add_tail(&request->link, &timeline->requests);
80b204bc
CW
919 spin_unlock_irq(&timeline->lock);
920
9b6586ae 921 GEM_BUG_ON(timeline->seqno != request->fence.seqno);
73cb9701 922 i915_gem_active_set(&timeline->last_request, request);
f2d13290 923
0f25dff6 924 list_add_tail(&request->ring_link, &ring->request_list);
f2d13290 925 request->emitted_jiffies = jiffies;
0f25dff6 926
9b6586ae
CW
927 if (!request->i915->gt.active_requests++)
928 i915_gem_mark_busy(engine);
5590af3e 929
0de9136d
CW
930 /* Let the backend know a new request has arrived that may need
931 * to adjust the existing execution schedule due to a high priority
932 * request - i.e. we may want to preempt the current request in order
933 * to run a high priority dependency chain *before* we can execute this
934 * request.
935 *
936 * This is called before the request is ready to run so that we can
937 * decide whether to preempt the entire chain so that it is ready to
938 * run at the earliest possible convenience.
939 */
940 if (engine->schedule)
9f792eba 941 engine->schedule(request, request->ctx->priority);
0de9136d 942
5590af3e
CW
943 local_bh_disable();
944 i915_sw_fence_commit(&request->submit);
945 local_bh_enable(); /* Kick the execlists tasklet if just scheduled */
05235c53
CW
946}
947
948static unsigned long local_clock_us(unsigned int *cpu)
949{
950 unsigned long t;
951
952 /* Cheaply and approximately convert from nanoseconds to microseconds.
953 * The result and subsequent calculations are also defined in the same
954 * approximate microseconds units. The principal source of timing
955 * error here is from the simple truncation.
956 *
957 * Note that local_clock() is only defined wrt to the current CPU;
958 * the comparisons are no longer valid if we switch CPUs. Instead of
959 * blocking preemption for the entire busywait, we can detect the CPU
960 * switch and use that as indicator of system load and a reason to
961 * stop busywaiting, see busywait_stop().
962 */
963 *cpu = get_cpu();
964 t = local_clock() >> 10;
965 put_cpu();
966
967 return t;
968}
969
970static bool busywait_stop(unsigned long timeout, unsigned int cpu)
971{
972 unsigned int this_cpu;
973
974 if (time_after(local_clock_us(&this_cpu), timeout))
975 return true;
976
977 return this_cpu != cpu;
978}
979
980bool __i915_spin_request(const struct drm_i915_gem_request *req,
754c9fd5 981 u32 seqno, int state, unsigned long timeout_us)
05235c53 982{
c33ed067
CW
983 struct intel_engine_cs *engine = req->engine;
984 unsigned int irq, cpu;
05235c53
CW
985
986 /* When waiting for high frequency requests, e.g. during synchronous
987 * rendering split between the CPU and GPU, the finite amount of time
988 * required to set up the irq and wait upon it limits the response
989 * rate. By busywaiting on the request completion for a short while we
990 * can service the high frequency waits as quick as possible. However,
991 * if it is a slow request, we want to sleep as quickly as possible.
992 * The tradeoff between waiting and sleeping is roughly the time it
993 * takes to sleep on a request, on the order of a microsecond.
994 */
995
c33ed067 996 irq = atomic_read(&engine->irq_count);
05235c53
CW
997 timeout_us += local_clock_us(&cpu);
998 do {
754c9fd5
CW
999 if (seqno != i915_gem_request_global_seqno(req))
1000 break;
1001
1002 if (i915_seqno_passed(intel_engine_get_seqno(req->engine),
1003 seqno))
05235c53
CW
1004 return true;
1005
c33ed067
CW
1006 /* Seqno are meant to be ordered *before* the interrupt. If
1007 * we see an interrupt without a corresponding seqno advance,
1008 * assume we won't see one in the near future but require
1009 * the engine->seqno_barrier() to fixup coherency.
1010 */
1011 if (atomic_read(&engine->irq_count) != irq)
1012 break;
1013
05235c53
CW
1014 if (signal_pending_state(state, current))
1015 break;
1016
1017 if (busywait_stop(timeout_us, cpu))
1018 break;
1019
f2f09a4c 1020 cpu_relax();
05235c53
CW
1021 } while (!need_resched());
1022
1023 return false;
1024}
1025
e0705114 1026static bool __i915_wait_request_check_and_reset(struct drm_i915_gem_request *request)
4680816b 1027{
8c185eca 1028 if (likely(!i915_reset_handoff(&request->i915->gpu_error)))
e0705114 1029 return false;
4680816b 1030
e0705114
CW
1031 __set_current_state(TASK_RUNNING);
1032 i915_reset(request->i915);
1033 return true;
4680816b
CW
1034}
1035
05235c53 1036/**
776f3236 1037 * i915_wait_request - wait until execution of request has finished
e95433c7 1038 * @req: the request to wait upon
ea746f36 1039 * @flags: how to wait
e95433c7
CW
1040 * @timeout: how long to wait in jiffies
1041 *
1042 * i915_wait_request() waits for the request to be completed, for a
1043 * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
1044 * unbounded wait).
05235c53 1045 *
e95433c7
CW
1046 * If the caller holds the struct_mutex, the caller must pass I915_WAIT_LOCKED
1047 * in via the flags, and vice versa if the struct_mutex is not held, the caller
1048 * must not specify that the wait is locked.
05235c53 1049 *
e95433c7
CW
1050 * Returns the remaining time (in jiffies) if the request completed, which may
1051 * be zero or -ETIME if the request is unfinished after the timeout expires.
1052 * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
1053 * pending before the request completes.
05235c53 1054 */
e95433c7
CW
1055long i915_wait_request(struct drm_i915_gem_request *req,
1056 unsigned int flags,
1057 long timeout)
05235c53 1058{
ea746f36
CW
1059 const int state = flags & I915_WAIT_INTERRUPTIBLE ?
1060 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
4b36b2e5 1061 wait_queue_head_t *errq = &req->i915->gpu_error.wait_queue;
a49625f9
CW
1062 DEFINE_WAIT_FUNC(reset, default_wake_function);
1063 DEFINE_WAIT_FUNC(exec, default_wake_function);
05235c53 1064 struct intel_wait wait;
05235c53
CW
1065
1066 might_sleep();
22dd3bb9 1067#if IS_ENABLED(CONFIG_LOCKDEP)
e95433c7
CW
1068 GEM_BUG_ON(debug_locks &&
1069 !!lockdep_is_held(&req->i915->drm.struct_mutex) !=
22dd3bb9
CW
1070 !!(flags & I915_WAIT_LOCKED));
1071#endif
e95433c7 1072 GEM_BUG_ON(timeout < 0);
05235c53 1073
05235c53 1074 if (i915_gem_request_completed(req))
e95433c7 1075 return timeout;
05235c53 1076
e95433c7
CW
1077 if (!timeout)
1078 return -ETIME;
05235c53 1079
93692502 1080 trace_i915_gem_request_wait_begin(req, flags);
05235c53 1081
a49625f9 1082 add_wait_queue(&req->execute, &exec);
7de53bf7
CW
1083 if (flags & I915_WAIT_LOCKED)
1084 add_wait_queue(errq, &reset);
1085
56299fb7 1086 intel_wait_init(&wait, req);
754c9fd5 1087
d6a2289d 1088restart:
0f2f61d4
CW
1089 do {
1090 set_current_state(state);
1091 if (intel_wait_update_request(&wait, req))
1092 break;
541ca6ed 1093
0f2f61d4
CW
1094 if (flags & I915_WAIT_LOCKED &&
1095 __i915_wait_request_check_and_reset(req))
1096 continue;
05235c53 1097
0f2f61d4
CW
1098 if (signal_pending_state(state, current)) {
1099 timeout = -ERESTARTSYS;
4680816b 1100 goto complete;
0f2f61d4 1101 }
4680816b 1102
0f2f61d4
CW
1103 if (!timeout) {
1104 timeout = -ETIME;
1105 goto complete;
1106 }
541ca6ed 1107
0f2f61d4
CW
1108 timeout = io_schedule_timeout(timeout);
1109 } while (1);
4680816b 1110
0f2f61d4 1111 GEM_BUG_ON(!intel_wait_has_seqno(&wait));
fe49789f 1112 GEM_BUG_ON(!i915_sw_fence_signaled(&req->submit));
4680816b 1113
437c3087 1114 /* Optimistic short spin before touching IRQs */
05235c53
CW
1115 if (i915_spin_request(req, state, 5))
1116 goto complete;
1117
1118 set_current_state(state);
05235c53
CW
1119 if (intel_engine_add_wait(req->engine, &wait))
1120 /* In order to check that we haven't missed the interrupt
1121 * as we enabled it, we need to kick ourselves to do a
1122 * coherent check on the seqno before we sleep.
1123 */
1124 goto wakeup;
1125
24f417ec
CW
1126 if (flags & I915_WAIT_LOCKED)
1127 __i915_wait_request_check_and_reset(req);
1128
05235c53
CW
1129 for (;;) {
1130 if (signal_pending_state(state, current)) {
e95433c7 1131 timeout = -ERESTARTSYS;
05235c53
CW
1132 break;
1133 }
1134
e95433c7
CW
1135 if (!timeout) {
1136 timeout = -ETIME;
05235c53
CW
1137 break;
1138 }
1139
e95433c7
CW
1140 timeout = io_schedule_timeout(timeout);
1141
754c9fd5
CW
1142 if (intel_wait_complete(&wait) &&
1143 intel_wait_check_request(&wait, req))
05235c53
CW
1144 break;
1145
1146 set_current_state(state);
1147
1148wakeup:
1149 /* Carefully check if the request is complete, giving time
1150 * for the seqno to be visible following the interrupt.
1151 * We also have to check in case we are kicked by the GPU
1152 * reset in order to drop the struct_mutex.
1153 */
1154 if (__i915_request_irq_complete(req))
1155 break;
1156
221fe799
CW
1157 /* If the GPU is hung, and we hold the lock, reset the GPU
1158 * and then check for completion. On a full reset, the engine's
1159 * HW seqno will be advanced passed us and we are complete.
1160 * If we do a partial reset, we have to wait for the GPU to
1161 * resume and update the breadcrumb.
1162 *
1163 * If we don't hold the mutex, we can just wait for the worker
1164 * to come along and update the breadcrumb (either directly
1165 * itself, or indirectly by recovering the GPU).
1166 */
1167 if (flags & I915_WAIT_LOCKED &&
e0705114 1168 __i915_wait_request_check_and_reset(req))
221fe799 1169 continue;
221fe799 1170
05235c53
CW
1171 /* Only spin if we know the GPU is processing this request */
1172 if (i915_spin_request(req, state, 2))
1173 break;
d6a2289d
CW
1174
1175 if (!intel_wait_check_request(&wait, req)) {
1176 intel_engine_remove_wait(req->engine, &wait);
1177 goto restart;
1178 }
05235c53 1179 }
05235c53
CW
1180
1181 intel_engine_remove_wait(req->engine, &wait);
05235c53 1182complete:
a49625f9 1183 __set_current_state(TASK_RUNNING);
7de53bf7
CW
1184 if (flags & I915_WAIT_LOCKED)
1185 remove_wait_queue(errq, &reset);
a49625f9 1186 remove_wait_queue(&req->execute, &exec);
05235c53
CW
1187 trace_i915_gem_request_wait_end(req);
1188
e95433c7 1189 return timeout;
05235c53 1190}
4b8de8e6 1191
28176ef4 1192static void engine_retire_requests(struct intel_engine_cs *engine)
4b8de8e6
CW
1193{
1194 struct drm_i915_gem_request *request, *next;
754c9fd5
CW
1195 u32 seqno = intel_engine_get_seqno(engine);
1196 LIST_HEAD(retire);
4b8de8e6 1197
754c9fd5 1198 spin_lock_irq(&engine->timeline->lock);
73cb9701
CW
1199 list_for_each_entry_safe(request, next,
1200 &engine->timeline->requests, link) {
754c9fd5
CW
1201 if (!i915_seqno_passed(seqno, request->global_seqno))
1202 break;
4b8de8e6 1203
754c9fd5 1204 list_move_tail(&request->link, &retire);
4b8de8e6 1205 }
754c9fd5
CW
1206 spin_unlock_irq(&engine->timeline->lock);
1207
1208 list_for_each_entry_safe(request, next, &retire, link)
1209 i915_gem_request_retire(request);
4b8de8e6
CW
1210}
1211
1212void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
1213{
1214 struct intel_engine_cs *engine;
28176ef4 1215 enum intel_engine_id id;
4b8de8e6
CW
1216
1217 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1218
28176ef4 1219 if (!dev_priv->gt.active_requests)
4b8de8e6
CW
1220 return;
1221
28176ef4
CW
1222 for_each_engine(engine, dev_priv, id)
1223 engine_retire_requests(engine);
4b8de8e6 1224}
c835c550
CW
1225
1226#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1227#include "selftests/mock_request.c"
1228#include "selftests/i915_gem_request.c"
1229#endif