blob: 311cf3fac2e09ae8696dfb40690920028b9686ae [file] [log] [blame]
Chris Wilson05235c52016-07-20 09:21:08 +01001/*
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
Chris Wilsonfa545cb2016-08-04 07:52:35 +010025#include <linux/prefetch.h>
Chris Wilsonb52992c2016-10-28 13:58:24 +010026#include <linux/dma-fence-array.h>
Chris Wilsonfa545cb2016-08-04 07:52:35 +010027
Chris Wilson05235c52016-07-20 09:21:08 +010028#include "i915_drv.h"
29
Chris Wilsonf54d1862016-10-25 13:00:45 +010030static const char *i915_fence_get_driver_name(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010031{
32 return "i915";
33}
34
Chris Wilsonf54d1862016-10-25 13:00:45 +010035static const char *i915_fence_get_timeline_name(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010036{
37 /* Timelines are bound by eviction to a VM. However, since
38 * we only have a global seqno at the moment, we only have
39 * a single timeline. Note that each timeline will have
40 * multiple execution contexts (fence contexts) as we allow
41 * engines within a single timeline to execute in parallel.
42 */
Chris Wilson73cb9702016-10-28 13:58:46 +010043 return to_request(fence)->timeline->common->name;
Chris Wilson04769652016-07-20 09:21:11 +010044}
45
Chris Wilsonf54d1862016-10-25 13:00:45 +010046static bool i915_fence_signaled(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010047{
48 return i915_gem_request_completed(to_request(fence));
49}
50
Chris Wilsonf54d1862016-10-25 13:00:45 +010051static bool i915_fence_enable_signaling(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010052{
53 if (i915_fence_signaled(fence))
54 return false;
55
56 intel_engine_enable_signaling(to_request(fence));
57 return true;
58}
59
Chris Wilsonf54d1862016-10-25 13:00:45 +010060static signed long i915_fence_wait(struct dma_fence *fence,
Chris Wilson04769652016-07-20 09:21:11 +010061 bool interruptible,
Chris Wilsone95433c2016-10-28 13:58:27 +010062 signed long timeout)
Chris Wilson04769652016-07-20 09:21:11 +010063{
Chris Wilsone95433c2016-10-28 13:58:27 +010064 return i915_wait_request(to_request(fence), interruptible, timeout);
Chris Wilson04769652016-07-20 09:21:11 +010065}
66
Chris Wilsonf54d1862016-10-25 13:00:45 +010067static void i915_fence_value_str(struct dma_fence *fence, char *str, int size)
Chris Wilson04769652016-07-20 09:21:11 +010068{
69 snprintf(str, size, "%u", fence->seqno);
70}
71
Chris Wilsonf54d1862016-10-25 13:00:45 +010072static void i915_fence_timeline_value_str(struct dma_fence *fence, char *str,
Chris Wilson04769652016-07-20 09:21:11 +010073 int size)
74{
75 snprintf(str, size, "%u",
76 intel_engine_get_seqno(to_request(fence)->engine));
77}
78
Chris Wilsonf54d1862016-10-25 13:00:45 +010079static void i915_fence_release(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010080{
81 struct drm_i915_gem_request *req = to_request(fence);
82
83 kmem_cache_free(req->i915->requests, req);
84}
85
Chris Wilsonf54d1862016-10-25 13:00:45 +010086const struct dma_fence_ops i915_fence_ops = {
Chris Wilson04769652016-07-20 09:21:11 +010087 .get_driver_name = i915_fence_get_driver_name,
88 .get_timeline_name = i915_fence_get_timeline_name,
89 .enable_signaling = i915_fence_enable_signaling,
90 .signaled = i915_fence_signaled,
91 .wait = i915_fence_wait,
92 .release = i915_fence_release,
93 .fence_value_str = i915_fence_value_str,
94 .timeline_value_str = i915_fence_timeline_value_str,
95};
96
Chris Wilson05235c52016-07-20 09:21:08 +010097int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
98 struct drm_file *file)
99{
100 struct drm_i915_private *dev_private;
101 struct drm_i915_file_private *file_priv;
102
103 WARN_ON(!req || !file || req->file_priv);
104
105 if (!req || !file)
106 return -EINVAL;
107
108 if (req->file_priv)
109 return -EINVAL;
110
111 dev_private = req->i915;
112 file_priv = file->driver_priv;
113
114 spin_lock(&file_priv->mm.lock);
115 req->file_priv = file_priv;
116 list_add_tail(&req->client_list, &file_priv->mm.request_list);
117 spin_unlock(&file_priv->mm.lock);
118
Chris Wilson05235c52016-07-20 09:21:08 +0100119 return 0;
120}
121
122static inline void
123i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
124{
125 struct drm_i915_file_private *file_priv = request->file_priv;
126
127 if (!file_priv)
128 return;
129
130 spin_lock(&file_priv->mm.lock);
131 list_del(&request->client_list);
132 request->file_priv = NULL;
133 spin_unlock(&file_priv->mm.lock);
Chris Wilson05235c52016-07-20 09:21:08 +0100134}
135
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100136void i915_gem_retire_noop(struct i915_gem_active *active,
137 struct drm_i915_gem_request *request)
138{
139 /* Space left intentionally blank */
140}
141
Chris Wilson05235c52016-07-20 09:21:08 +0100142static void i915_gem_request_retire(struct drm_i915_gem_request *request)
143{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100144 struct i915_gem_active *active, *next;
145
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100146 lockdep_assert_held(&request->i915->drm.struct_mutex);
147 GEM_BUG_ON(!i915_gem_request_completed(request));
148
Chris Wilson05235c52016-07-20 09:21:08 +0100149 trace_i915_gem_request_retire(request);
Chris Wilsone95433c2016-10-28 13:58:27 +0100150 list_del_init(&request->link);
Chris Wilson05235c52016-07-20 09:21:08 +0100151
152 /* We know the GPU must have read the request to have
153 * sent us the seqno + interrupt, so use the position
154 * of tail of the request to update the last known position
155 * of the GPU head.
156 *
157 * Note this requires that we are always called in request
158 * completion order.
159 */
Chris Wilson675d9ad2016-08-04 07:52:36 +0100160 list_del(&request->ring_link);
Chris Wilson1dae2df2016-08-02 22:50:19 +0100161 request->ring->last_retired_head = request->postfix;
Chris Wilson05235c52016-07-20 09:21:08 +0100162
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100163 /* Walk through the active list, calling retire on each. This allows
164 * objects to track their GPU activity and mark themselves as idle
165 * when their *last* active request is completed (updating state
166 * tracking lists for eviction, active references for GEM, etc).
167 *
168 * As the ->retire() may free the node, we decouple it first and
169 * pass along the auxiliary information (to avoid dereferencing
170 * the node after the callback).
171 */
172 list_for_each_entry_safe(active, next, &request->active_list, link) {
173 /* In microbenchmarks or focusing upon time inside the kernel,
174 * we may spend an inordinate amount of time simply handling
175 * the retirement of requests and processing their callbacks.
176 * Of which, this loop itself is particularly hot due to the
177 * cache misses when jumping around the list of i915_gem_active.
178 * So we try to keep this loop as streamlined as possible and
179 * also prefetch the next i915_gem_active to try and hide
180 * the likely cache miss.
181 */
182 prefetchw(next);
183
184 INIT_LIST_HEAD(&active->link);
Chris Wilson0eafec62016-08-04 16:32:41 +0100185 RCU_INIT_POINTER(active->request, NULL);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100186
187 active->retire(active, request);
188 }
189
Chris Wilson05235c52016-07-20 09:21:08 +0100190 i915_gem_request_remove_from_client(request);
191
192 if (request->previous_context) {
193 if (i915.enable_execlists)
194 intel_lr_context_unpin(request->previous_context,
195 request->engine);
196 }
197
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100198 i915_gem_context_put(request->ctx);
Chris Wilsond07f0e52016-10-28 13:58:44 +0100199
200 dma_fence_signal(&request->fence);
Chris Wilsone8a261e2016-07-20 13:31:49 +0100201 i915_gem_request_put(request);
Chris Wilson05235c52016-07-20 09:21:08 +0100202}
203
204void i915_gem_request_retire_upto(struct drm_i915_gem_request *req)
205{
206 struct intel_engine_cs *engine = req->engine;
207 struct drm_i915_gem_request *tmp;
208
209 lockdep_assert_held(&req->i915->drm.struct_mutex);
Chris Wilsone95433c2016-10-28 13:58:27 +0100210 if (list_empty(&req->link))
211 return;
Chris Wilson05235c52016-07-20 09:21:08 +0100212
213 do {
Chris Wilson73cb9702016-10-28 13:58:46 +0100214 tmp = list_first_entry(&engine->timeline->requests,
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100215 typeof(*tmp), link);
Chris Wilson05235c52016-07-20 09:21:08 +0100216
217 i915_gem_request_retire(tmp);
218 } while (tmp != req);
Chris Wilson05235c52016-07-20 09:21:08 +0100219}
220
Chris Wilson8af29b02016-09-09 14:11:47 +0100221static int i915_gem_check_wedge(struct drm_i915_private *dev_priv)
Chris Wilson05235c52016-07-20 09:21:08 +0100222{
Chris Wilson8af29b02016-09-09 14:11:47 +0100223 struct i915_gpu_error *error = &dev_priv->gpu_error;
224
225 if (i915_terminally_wedged(error))
Chris Wilson05235c52016-07-20 09:21:08 +0100226 return -EIO;
227
Chris Wilson8af29b02016-09-09 14:11:47 +0100228 if (i915_reset_in_progress(error)) {
Chris Wilson05235c52016-07-20 09:21:08 +0100229 /* Non-interruptible callers can't handle -EAGAIN, hence return
230 * -EIO unconditionally for these.
231 */
Chris Wilson8af29b02016-09-09 14:11:47 +0100232 if (!dev_priv->mm.interruptible)
Chris Wilson05235c52016-07-20 09:21:08 +0100233 return -EIO;
234
235 return -EAGAIN;
236 }
237
238 return 0;
239}
240
Chris Wilson73cb9702016-10-28 13:58:46 +0100241static int i915_gem_init_global_seqno(struct drm_i915_private *dev_priv,
242 u32 seqno)
Chris Wilson05235c52016-07-20 09:21:08 +0100243{
Chris Wilson73cb9702016-10-28 13:58:46 +0100244 struct i915_gem_timeline *timeline = &dev_priv->gt.global_timeline;
Chris Wilson05235c52016-07-20 09:21:08 +0100245 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +0530246 enum intel_engine_id id;
Chris Wilson05235c52016-07-20 09:21:08 +0100247 int ret;
248
249 /* Carefully retire all requests without writing to the rings */
Chris Wilson73cb9702016-10-28 13:58:46 +0100250 ret = i915_gem_wait_for_idle(dev_priv,
251 I915_WAIT_INTERRUPTIBLE |
252 I915_WAIT_LOCKED);
253 if (ret)
254 return ret;
255
Chris Wilson05235c52016-07-20 09:21:08 +0100256 i915_gem_retire_requests(dev_priv);
257
258 /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
Chris Wilson73cb9702016-10-28 13:58:46 +0100259 if (!i915_seqno_passed(seqno, timeline->next_seqno)) {
Chris Wilson05235c52016-07-20 09:21:08 +0100260 while (intel_kick_waiters(dev_priv) ||
261 intel_kick_signalers(dev_priv))
262 yield();
Chris Wilson73cb9702016-10-28 13:58:46 +0100263 yield();
Chris Wilson05235c52016-07-20 09:21:08 +0100264 }
265
266 /* Finally reset hw state */
Akash Goel3b3f1652016-10-13 22:44:48 +0530267 for_each_engine(engine, dev_priv, id)
Chris Wilson73cb9702016-10-28 13:58:46 +0100268 intel_engine_init_global_seqno(engine, seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100269
270 return 0;
271}
272
Chris Wilson73cb9702016-10-28 13:58:46 +0100273int i915_gem_set_global_seqno(struct drm_device *dev, u32 seqno)
Chris Wilson05235c52016-07-20 09:21:08 +0100274{
275 struct drm_i915_private *dev_priv = to_i915(dev);
276 int ret;
277
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100278 lockdep_assert_held(&dev_priv->drm.struct_mutex);
279
Chris Wilson05235c52016-07-20 09:21:08 +0100280 if (seqno == 0)
281 return -EINVAL;
282
283 /* HWS page needs to be set less than what we
284 * will inject to ring
285 */
Chris Wilson73cb9702016-10-28 13:58:46 +0100286 ret = i915_gem_init_global_seqno(dev_priv, seqno - 1);
Chris Wilson05235c52016-07-20 09:21:08 +0100287 if (ret)
288 return ret;
289
Chris Wilson73cb9702016-10-28 13:58:46 +0100290 dev_priv->gt.global_timeline.next_seqno = seqno;
Chris Wilson05235c52016-07-20 09:21:08 +0100291 return 0;
292}
293
Chris Wilson73cb9702016-10-28 13:58:46 +0100294static int i915_gem_get_global_seqno(struct drm_i915_private *dev_priv,
295 u32 *seqno)
Chris Wilson05235c52016-07-20 09:21:08 +0100296{
Chris Wilson73cb9702016-10-28 13:58:46 +0100297 struct i915_gem_timeline *tl = &dev_priv->gt.global_timeline;
298
Chris Wilson05235c52016-07-20 09:21:08 +0100299 /* reserve 0 for non-seqno */
Chris Wilson73cb9702016-10-28 13:58:46 +0100300 if (unlikely(tl->next_seqno == 0)) {
Chris Wilson05235c52016-07-20 09:21:08 +0100301 int ret;
302
Chris Wilson73cb9702016-10-28 13:58:46 +0100303 ret = i915_gem_init_global_seqno(dev_priv, 0);
Chris Wilson05235c52016-07-20 09:21:08 +0100304 if (ret)
305 return ret;
306
Chris Wilson73cb9702016-10-28 13:58:46 +0100307 tl->next_seqno = 1;
Chris Wilson05235c52016-07-20 09:21:08 +0100308 }
309
Chris Wilson73cb9702016-10-28 13:58:46 +0100310 *seqno = tl->next_seqno++;
Chris Wilson05235c52016-07-20 09:21:08 +0100311 return 0;
312}
313
Chris Wilson5590af32016-09-09 14:11:54 +0100314static int __i915_sw_fence_call
315submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
316{
317 struct drm_i915_gem_request *request =
318 container_of(fence, typeof(*request), submit);
Chris Wilson73cb9702016-10-28 13:58:46 +0100319 struct intel_engine_cs *engine = request->engine;
Chris Wilson5590af32016-09-09 14:11:54 +0100320
321 /* Will be called from irq-context when using foreign DMA fences */
322
323 switch (state) {
324 case FENCE_COMPLETE:
Chris Wilson73cb9702016-10-28 13:58:46 +0100325 engine->timeline->last_submitted_seqno = request->fence.seqno;
326 engine->submit_request(request);
Chris Wilson5590af32016-09-09 14:11:54 +0100327 break;
328
329 case FENCE_FREE:
330 break;
331 }
332
333 return NOTIFY_DONE;
334}
335
Chris Wilson8e637172016-08-02 22:50:26 +0100336/**
337 * i915_gem_request_alloc - allocate a request structure
338 *
339 * @engine: engine that we wish to issue the request on.
340 * @ctx: context that the request will be associated with.
341 * This can be NULL if the request is not directly related to
342 * any specific user context, in which case this function will
343 * choose an appropriate context to use.
344 *
345 * Returns a pointer to the allocated request if successful,
346 * or an error code if not.
347 */
348struct drm_i915_gem_request *
349i915_gem_request_alloc(struct intel_engine_cs *engine,
350 struct i915_gem_context *ctx)
Chris Wilson05235c52016-07-20 09:21:08 +0100351{
352 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson05235c52016-07-20 09:21:08 +0100353 struct drm_i915_gem_request *req;
Chris Wilson04769652016-07-20 09:21:11 +0100354 u32 seqno;
Chris Wilson05235c52016-07-20 09:21:08 +0100355 int ret;
356
Chris Wilson05235c52016-07-20 09:21:08 +0100357 /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
358 * EIO if the GPU is already wedged, or EAGAIN to drop the struct_mutex
359 * and restart.
360 */
Chris Wilson8af29b02016-09-09 14:11:47 +0100361 ret = i915_gem_check_wedge(dev_priv);
Chris Wilson05235c52016-07-20 09:21:08 +0100362 if (ret)
Chris Wilson8e637172016-08-02 22:50:26 +0100363 return ERR_PTR(ret);
Chris Wilson05235c52016-07-20 09:21:08 +0100364
Chris Wilson9b5f4e52016-07-20 09:21:09 +0100365 /* Move the oldest request to the slab-cache (if not in use!) */
Chris Wilson73cb9702016-10-28 13:58:46 +0100366 req = list_first_entry_or_null(&engine->timeline->requests,
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100367 typeof(*req), link);
Chris Wilson2a1d7752016-07-26 12:01:51 +0100368 if (req && i915_gem_request_completed(req))
369 i915_gem_request_retire(req);
Chris Wilson9b5f4e52016-07-20 09:21:09 +0100370
Chris Wilson5a198b82016-08-09 09:23:34 +0100371 /* Beware: Dragons be flying overhead.
372 *
373 * We use RCU to look up requests in flight. The lookups may
374 * race with the request being allocated from the slab freelist.
375 * That is the request we are writing to here, may be in the process
Chris Wilson1426f712016-08-09 17:03:22 +0100376 * of being read by __i915_gem_active_get_rcu(). As such,
Chris Wilson5a198b82016-08-09 09:23:34 +0100377 * we have to be very careful when overwriting the contents. During
378 * the RCU lookup, we change chase the request->engine pointer,
Chris Wilson65e47602016-10-28 13:58:49 +0100379 * read the request->global_seqno and increment the reference count.
Chris Wilson5a198b82016-08-09 09:23:34 +0100380 *
381 * The reference count is incremented atomically. If it is zero,
382 * the lookup knows the request is unallocated and complete. Otherwise,
383 * it is either still in use, or has been reallocated and reset
Chris Wilsonf54d1862016-10-25 13:00:45 +0100384 * with dma_fence_init(). This increment is safe for release as we
385 * check that the request we have a reference to and matches the active
Chris Wilson5a198b82016-08-09 09:23:34 +0100386 * request.
387 *
388 * Before we increment the refcount, we chase the request->engine
389 * pointer. We must not call kmem_cache_zalloc() or else we set
390 * that pointer to NULL and cause a crash during the lookup. If
391 * we see the request is completed (based on the value of the
392 * old engine and seqno), the lookup is complete and reports NULL.
393 * If we decide the request is not completed (new engine or seqno),
394 * then we grab a reference and double check that it is still the
395 * active request - which it won't be and restart the lookup.
396 *
397 * Do not use kmem_cache_zalloc() here!
398 */
399 req = kmem_cache_alloc(dev_priv->requests, GFP_KERNEL);
Chris Wilson05235c52016-07-20 09:21:08 +0100400 if (!req)
Chris Wilson8e637172016-08-02 22:50:26 +0100401 return ERR_PTR(-ENOMEM);
Chris Wilson05235c52016-07-20 09:21:08 +0100402
Chris Wilson73cb9702016-10-28 13:58:46 +0100403 ret = i915_gem_get_global_seqno(dev_priv, &seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100404 if (ret)
405 goto err;
406
Chris Wilson73cb9702016-10-28 13:58:46 +0100407 req->timeline = engine->timeline;
408
Chris Wilson04769652016-07-20 09:21:11 +0100409 spin_lock_init(&req->lock);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100410 dma_fence_init(&req->fence,
411 &i915_fence_ops,
412 &req->lock,
Chris Wilson73cb9702016-10-28 13:58:46 +0100413 req->timeline->fence_context,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100414 seqno);
Chris Wilson04769652016-07-20 09:21:11 +0100415
Chris Wilson5590af32016-09-09 14:11:54 +0100416 i915_sw_fence_init(&req->submit, submit_notify);
417
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100418 INIT_LIST_HEAD(&req->active_list);
Chris Wilson05235c52016-07-20 09:21:08 +0100419 req->i915 = dev_priv;
420 req->engine = engine;
Chris Wilson65e47602016-10-28 13:58:49 +0100421 req->global_seqno = seqno;
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100422 req->ctx = i915_gem_context_get(ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100423
Chris Wilson5a198b82016-08-09 09:23:34 +0100424 /* No zalloc, must clear what we need by hand */
425 req->previous_context = NULL;
426 req->file_priv = NULL;
Chris Wilson058d88c2016-08-15 10:49:06 +0100427 req->batch = NULL;
Chris Wilson5a198b82016-08-09 09:23:34 +0100428
Chris Wilson05235c52016-07-20 09:21:08 +0100429 /*
430 * Reserve space in the ring buffer for all the commands required to
431 * eventually emit this request. This is to guarantee that the
432 * i915_add_request() call can't fail. Note that the reserve may need
433 * to be redone if the request is not actually submitted straight
434 * away, e.g. because a GPU scheduler has deferred it.
435 */
436 req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
437
438 if (i915.enable_execlists)
439 ret = intel_logical_ring_alloc_request_extras(req);
440 else
441 ret = intel_ring_alloc_request_extras(req);
442 if (ret)
443 goto err_ctx;
444
Chris Wilsond0454462016-08-15 10:48:40 +0100445 /* Record the position of the start of the request so that
446 * should we detect the updated seqno part-way through the
447 * GPU processing the request, we never over-estimate the
448 * position of the head.
449 */
450 req->head = req->ring->tail;
451
Chris Wilson8e637172016-08-02 22:50:26 +0100452 return req;
Chris Wilson05235c52016-07-20 09:21:08 +0100453
454err_ctx:
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100455 i915_gem_context_put(ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100456err:
457 kmem_cache_free(dev_priv->requests, req);
Chris Wilson8e637172016-08-02 22:50:26 +0100458 return ERR_PTR(ret);
Chris Wilson05235c52016-07-20 09:21:08 +0100459}
460
Chris Wilsona2bc4692016-09-09 14:11:56 +0100461static int
462i915_gem_request_await_request(struct drm_i915_gem_request *to,
463 struct drm_i915_gem_request *from)
464{
465 int idx, ret;
466
467 GEM_BUG_ON(to == from);
468
Chris Wilson73cb9702016-10-28 13:58:46 +0100469 if (to->timeline == from->timeline)
Chris Wilsona2bc4692016-09-09 14:11:56 +0100470 return 0;
471
Chris Wilson73cb9702016-10-28 13:58:46 +0100472 if (to->engine == from->engine) {
473 ret = i915_sw_fence_await_sw_fence_gfp(&to->submit,
474 &from->submit,
475 GFP_KERNEL);
476 return ret < 0 ? ret : 0;
477 }
478
Chris Wilson65e47602016-10-28 13:58:49 +0100479 if (!from->global_seqno) {
480 ret = i915_sw_fence_await_dma_fence(&to->submit,
481 &from->fence, 0,
482 GFP_KERNEL);
483 return ret < 0 ? ret : 0;
484 }
485
Chris Wilsona2bc4692016-09-09 14:11:56 +0100486 idx = intel_engine_sync_index(from->engine, to->engine);
Chris Wilson65e47602016-10-28 13:58:49 +0100487 if (from->global_seqno <= from->engine->semaphore.sync_seqno[idx])
Chris Wilsona2bc4692016-09-09 14:11:56 +0100488 return 0;
489
490 trace_i915_gem_ring_sync_to(to, from);
491 if (!i915.semaphores) {
Chris Wilson0a046a02016-09-09 14:12:00 +0100492 if (!i915_spin_request(from, TASK_INTERRUPTIBLE, 2)) {
493 ret = i915_sw_fence_await_dma_fence(&to->submit,
494 &from->fence, 0,
495 GFP_KERNEL);
496 if (ret < 0)
497 return ret;
498 }
Chris Wilsona2bc4692016-09-09 14:11:56 +0100499 } else {
500 ret = to->engine->semaphore.sync_to(to, from);
501 if (ret)
502 return ret;
503 }
504
Chris Wilson65e47602016-10-28 13:58:49 +0100505 from->engine->semaphore.sync_seqno[idx] = from->global_seqno;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100506 return 0;
507}
508
Chris Wilsonb52992c2016-10-28 13:58:24 +0100509int
510i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
511 struct dma_fence *fence)
512{
513 struct dma_fence_array *array;
514 int ret;
515 int i;
516
517 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
518 return 0;
519
520 if (dma_fence_is_i915(fence))
521 return i915_gem_request_await_request(req, to_request(fence));
522
523 if (!dma_fence_is_array(fence)) {
524 ret = i915_sw_fence_await_dma_fence(&req->submit,
525 fence, I915_FENCE_TIMEOUT,
526 GFP_KERNEL);
527 return ret < 0 ? ret : 0;
528 }
529
530 /* Note that if the fence-array was created in signal-on-any mode,
531 * we should *not* decompose it into its individual fences. However,
532 * we don't currently store which mode the fence-array is operating
533 * in. Fortunately, the only user of signal-on-any is private to
534 * amdgpu and we should not see any incoming fence-array from
535 * sync-file being in signal-on-any mode.
536 */
537
538 array = to_dma_fence_array(fence);
539 for (i = 0; i < array->num_fences; i++) {
540 struct dma_fence *child = array->fences[i];
541
542 if (dma_fence_is_i915(child))
543 ret = i915_gem_request_await_request(req,
544 to_request(child));
545 else
546 ret = i915_sw_fence_await_dma_fence(&req->submit,
547 child, I915_FENCE_TIMEOUT,
548 GFP_KERNEL);
549 if (ret < 0)
550 return ret;
551 }
552
553 return 0;
554}
555
Chris Wilsona2bc4692016-09-09 14:11:56 +0100556/**
557 * i915_gem_request_await_object - set this request to (async) wait upon a bo
558 *
559 * @to: request we are wishing to use
560 * @obj: object which may be in use on another ring.
561 *
562 * This code is meant to abstract object synchronization with the GPU.
563 * Conceptually we serialise writes between engines inside the GPU.
564 * We only allow one engine to write into a buffer at any time, but
565 * multiple readers. To ensure each has a coherent view of memory, we must:
566 *
567 * - If there is an outstanding write request to the object, the new
568 * request must wait for it to complete (either CPU or in hw, requests
569 * on the same ring will be naturally ordered).
570 *
571 * - If we are a write request (pending_write_domain is set), the new
572 * request must wait for outstanding read requests to complete.
573 *
574 * Returns 0 if successful, else propagates up the lower layer error.
575 */
576int
577i915_gem_request_await_object(struct drm_i915_gem_request *to,
578 struct drm_i915_gem_object *obj,
579 bool write)
580{
Chris Wilsond07f0e52016-10-28 13:58:44 +0100581 struct dma_fence *excl;
582 int ret = 0;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100583
584 if (write) {
Chris Wilsond07f0e52016-10-28 13:58:44 +0100585 struct dma_fence **shared;
586 unsigned int count, i;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100587
Chris Wilsond07f0e52016-10-28 13:58:44 +0100588 ret = reservation_object_get_fences_rcu(obj->resv,
589 &excl, &count, &shared);
Chris Wilsona2bc4692016-09-09 14:11:56 +0100590 if (ret)
591 return ret;
Chris Wilsond07f0e52016-10-28 13:58:44 +0100592
593 for (i = 0; i < count; i++) {
594 ret = i915_gem_request_await_dma_fence(to, shared[i]);
595 if (ret)
596 break;
597
598 dma_fence_put(shared[i]);
599 }
600
601 for (; i < count; i++)
602 dma_fence_put(shared[i]);
603 kfree(shared);
604 } else {
605 excl = reservation_object_get_excl_rcu(obj->resv);
Chris Wilsona2bc4692016-09-09 14:11:56 +0100606 }
607
Chris Wilsond07f0e52016-10-28 13:58:44 +0100608 if (excl) {
609 if (ret == 0)
610 ret = i915_gem_request_await_dma_fence(to, excl);
611
612 dma_fence_put(excl);
613 }
614
615 return ret;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100616}
617
Chris Wilson05235c52016-07-20 09:21:08 +0100618static void i915_gem_mark_busy(const struct intel_engine_cs *engine)
619{
620 struct drm_i915_private *dev_priv = engine->i915;
621
622 dev_priv->gt.active_engines |= intel_engine_flag(engine);
623 if (dev_priv->gt.awake)
624 return;
625
626 intel_runtime_pm_get_noresume(dev_priv);
627 dev_priv->gt.awake = true;
628
Chris Wilson54b4f682016-07-21 21:16:19 +0100629 intel_enable_gt_powersave(dev_priv);
Chris Wilson05235c52016-07-20 09:21:08 +0100630 i915_update_gfx_val(dev_priv);
631 if (INTEL_GEN(dev_priv) >= 6)
632 gen6_rps_busy(dev_priv);
633
634 queue_delayed_work(dev_priv->wq,
635 &dev_priv->gt.retire_work,
636 round_jiffies_up_relative(HZ));
637}
638
639/*
640 * NB: This function is not allowed to fail. Doing so would mean the the
641 * request is not being tracked for completion but the work itself is
642 * going to happen on the hardware. This would be a Bad Thing(tm).
643 */
Chris Wilson17f298cf2016-08-10 13:41:46 +0100644void __i915_add_request(struct drm_i915_gem_request *request, bool flush_caches)
Chris Wilson05235c52016-07-20 09:21:08 +0100645{
Chris Wilson95b2ab52016-08-15 10:48:46 +0100646 struct intel_engine_cs *engine = request->engine;
647 struct intel_ring *ring = request->ring;
Chris Wilson73cb9702016-10-28 13:58:46 +0100648 struct intel_timeline *timeline = request->timeline;
Chris Wilson0a046a02016-09-09 14:12:00 +0100649 struct drm_i915_gem_request *prev;
Chris Wilson05235c52016-07-20 09:21:08 +0100650 u32 request_start;
651 u32 reserved_tail;
652 int ret;
653
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100654 lockdep_assert_held(&request->i915->drm.struct_mutex);
Chris Wilson0f25dff2016-09-09 14:11:55 +0100655 trace_i915_gem_request_add(request);
656
Chris Wilson05235c52016-07-20 09:21:08 +0100657 /*
658 * To ensure that this call will not fail, space for its emissions
659 * should already have been reserved in the ring buffer. Let the ring
660 * know that it is time to use that space up.
661 */
Chris Wilsonba76d912016-08-02 22:50:28 +0100662 request_start = ring->tail;
Chris Wilson05235c52016-07-20 09:21:08 +0100663 reserved_tail = request->reserved_space;
664 request->reserved_space = 0;
665
666 /*
667 * Emit any outstanding flushes - execbuf can fail to emit the flush
668 * after having emitted the batchbuffer command. Hence we need to fix
669 * things up similar to emitting the lazy request. The difference here
670 * is that the flush _must_ happen before the next request, no matter
671 * what.
672 */
673 if (flush_caches) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100674 ret = engine->emit_flush(request, EMIT_FLUSH);
Chris Wilsonc7fe7d22016-08-02 22:50:24 +0100675
Chris Wilson05235c52016-07-20 09:21:08 +0100676 /* Not allowed to fail! */
Chris Wilsonc7fe7d22016-08-02 22:50:24 +0100677 WARN(ret, "engine->emit_flush() failed: %d!\n", ret);
Chris Wilson05235c52016-07-20 09:21:08 +0100678 }
679
Chris Wilsond0454462016-08-15 10:48:40 +0100680 /* Record the position of the start of the breadcrumb so that
Chris Wilson05235c52016-07-20 09:21:08 +0100681 * should we detect the updated seqno part-way through the
682 * GPU processing the request, we never over-estimate the
Chris Wilsond0454462016-08-15 10:48:40 +0100683 * position of the ring's HEAD.
Chris Wilson05235c52016-07-20 09:21:08 +0100684 */
Chris Wilsonba76d912016-08-02 22:50:28 +0100685 request->postfix = ring->tail;
Chris Wilson05235c52016-07-20 09:21:08 +0100686
Chris Wilson05235c52016-07-20 09:21:08 +0100687 /* Not allowed to fail! */
Chris Wilsonddd66c52016-08-02 22:50:31 +0100688 ret = engine->emit_request(request);
689 WARN(ret, "(%s)->emit_request failed: %d!\n", engine->name, ret);
Chris Wilsonc5efa1a2016-08-02 22:50:29 +0100690
Chris Wilson05235c52016-07-20 09:21:08 +0100691 /* Sanity check that the reserved size was large enough. */
Chris Wilsonba76d912016-08-02 22:50:28 +0100692 ret = ring->tail - request_start;
Chris Wilson05235c52016-07-20 09:21:08 +0100693 if (ret < 0)
Chris Wilson1dae2df2016-08-02 22:50:19 +0100694 ret += ring->size;
Chris Wilson05235c52016-07-20 09:21:08 +0100695 WARN_ONCE(ret > reserved_tail,
696 "Not enough space reserved (%d bytes) "
697 "for adding the request (%d bytes)\n",
698 reserved_tail, ret);
699
Chris Wilson0f25dff2016-09-09 14:11:55 +0100700 /* Seal the request and mark it as pending execution. Note that
701 * we may inspect this state, without holding any locks, during
702 * hangcheck. Hence we apply the barrier to ensure that we do not
703 * see a more recent value in the hws than we are tracking.
704 */
Chris Wilson0a046a02016-09-09 14:12:00 +0100705
Chris Wilson73cb9702016-10-28 13:58:46 +0100706 prev = i915_gem_active_raw(&timeline->last_request,
Chris Wilson0a046a02016-09-09 14:12:00 +0100707 &request->i915->drm.struct_mutex);
708 if (prev)
709 i915_sw_fence_await_sw_fence(&request->submit, &prev->submit,
710 &request->submitq);
711
Chris Wilson0f25dff2016-09-09 14:11:55 +0100712 request->emitted_jiffies = jiffies;
Chris Wilson73cb9702016-10-28 13:58:46 +0100713 request->previous_seqno = timeline->last_pending_seqno;
714 timeline->last_pending_seqno = request->fence.seqno;
715 i915_gem_active_set(&timeline->last_request, request);
716 list_add_tail(&request->link, &timeline->requests);
Chris Wilson0f25dff2016-09-09 14:11:55 +0100717 list_add_tail(&request->ring_link, &ring->request_list);
718
Chris Wilson05235c52016-07-20 09:21:08 +0100719 i915_gem_mark_busy(engine);
Chris Wilson5590af32016-09-09 14:11:54 +0100720
721 local_bh_disable();
722 i915_sw_fence_commit(&request->submit);
723 local_bh_enable(); /* Kick the execlists tasklet if just scheduled */
Chris Wilson05235c52016-07-20 09:21:08 +0100724}
725
Chris Wilson221fe792016-09-09 14:11:51 +0100726static void reset_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
727{
728 unsigned long flags;
729
730 spin_lock_irqsave(&q->lock, flags);
731 if (list_empty(&wait->task_list))
732 __add_wait_queue(q, wait);
733 spin_unlock_irqrestore(&q->lock, flags);
734}
735
Chris Wilson05235c52016-07-20 09:21:08 +0100736static unsigned long local_clock_us(unsigned int *cpu)
737{
738 unsigned long t;
739
740 /* Cheaply and approximately convert from nanoseconds to microseconds.
741 * The result and subsequent calculations are also defined in the same
742 * approximate microseconds units. The principal source of timing
743 * error here is from the simple truncation.
744 *
745 * Note that local_clock() is only defined wrt to the current CPU;
746 * the comparisons are no longer valid if we switch CPUs. Instead of
747 * blocking preemption for the entire busywait, we can detect the CPU
748 * switch and use that as indicator of system load and a reason to
749 * stop busywaiting, see busywait_stop().
750 */
751 *cpu = get_cpu();
752 t = local_clock() >> 10;
753 put_cpu();
754
755 return t;
756}
757
758static bool busywait_stop(unsigned long timeout, unsigned int cpu)
759{
760 unsigned int this_cpu;
761
762 if (time_after(local_clock_us(&this_cpu), timeout))
763 return true;
764
765 return this_cpu != cpu;
766}
767
768bool __i915_spin_request(const struct drm_i915_gem_request *req,
769 int state, unsigned long timeout_us)
770{
771 unsigned int cpu;
772
773 /* When waiting for high frequency requests, e.g. during synchronous
774 * rendering split between the CPU and GPU, the finite amount of time
775 * required to set up the irq and wait upon it limits the response
776 * rate. By busywaiting on the request completion for a short while we
777 * can service the high frequency waits as quick as possible. However,
778 * if it is a slow request, we want to sleep as quickly as possible.
779 * The tradeoff between waiting and sleeping is roughly the time it
780 * takes to sleep on a request, on the order of a microsecond.
781 */
782
783 timeout_us += local_clock_us(&cpu);
784 do {
Chris Wilson65e47602016-10-28 13:58:49 +0100785 if (__i915_gem_request_completed(req))
Chris Wilson05235c52016-07-20 09:21:08 +0100786 return true;
787
788 if (signal_pending_state(state, current))
789 break;
790
791 if (busywait_stop(timeout_us, cpu))
792 break;
793
794 cpu_relax_lowlatency();
795 } while (!need_resched());
796
797 return false;
798}
799
Chris Wilson4680816b2016-10-28 13:58:48 +0100800static long
801__i915_request_wait_for_submit(struct drm_i915_gem_request *request,
802 unsigned int flags,
803 long timeout)
804{
805 const int state = flags & I915_WAIT_INTERRUPTIBLE ?
806 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
807 wait_queue_head_t *q = &request->i915->gpu_error.wait_queue;
808 DEFINE_WAIT(reset);
809 DEFINE_WAIT(wait);
810
811 if (flags & I915_WAIT_LOCKED)
812 add_wait_queue(q, &reset);
813
814 do {
815 prepare_to_wait(&request->submit.wait, &wait, state);
816
817 if (i915_sw_fence_done(&request->submit))
818 break;
819
820 if (flags & I915_WAIT_LOCKED &&
821 i915_reset_in_progress(&request->i915->gpu_error)) {
822 __set_current_state(TASK_RUNNING);
823 i915_reset(request->i915);
824 reset_wait_queue(q, &reset);
825 continue;
826 }
827
828 if (signal_pending_state(state, current)) {
829 timeout = -ERESTARTSYS;
830 break;
831 }
832
833 timeout = io_schedule_timeout(timeout);
834 } while (timeout);
835 finish_wait(&request->submit.wait, &wait);
836
837 if (flags & I915_WAIT_LOCKED)
838 remove_wait_queue(q, &reset);
839
840 return timeout;
841}
842
Chris Wilson05235c52016-07-20 09:21:08 +0100843/**
Chris Wilson776f3232016-08-04 07:52:40 +0100844 * i915_wait_request - wait until execution of request has finished
Chris Wilsone95433c2016-10-28 13:58:27 +0100845 * @req: the request to wait upon
Chris Wilsonea746f32016-09-09 14:11:49 +0100846 * @flags: how to wait
Chris Wilsone95433c2016-10-28 13:58:27 +0100847 * @timeout: how long to wait in jiffies
Chris Wilson05235c52016-07-20 09:21:08 +0100848 *
Chris Wilsone95433c2016-10-28 13:58:27 +0100849 * i915_wait_request() waits for the request to be completed, for a
850 * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
851 * unbounded wait).
Chris Wilson05235c52016-07-20 09:21:08 +0100852 *
Chris Wilsone95433c2016-10-28 13:58:27 +0100853 * If the caller holds the struct_mutex, the caller must pass I915_WAIT_LOCKED
854 * in via the flags, and vice versa if the struct_mutex is not held, the caller
855 * must not specify that the wait is locked.
856 *
857 * Returns the remaining time (in jiffies) if the request completed, which may
858 * be zero or -ETIME if the request is unfinished after the timeout expires.
859 * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
860 * pending before the request completes.
Chris Wilson05235c52016-07-20 09:21:08 +0100861 */
Chris Wilsone95433c2016-10-28 13:58:27 +0100862long i915_wait_request(struct drm_i915_gem_request *req,
863 unsigned int flags,
864 long timeout)
Chris Wilson05235c52016-07-20 09:21:08 +0100865{
Chris Wilsonea746f32016-09-09 14:11:49 +0100866 const int state = flags & I915_WAIT_INTERRUPTIBLE ?
867 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
Chris Wilson05235c52016-07-20 09:21:08 +0100868 DEFINE_WAIT(reset);
869 struct intel_wait wait;
Chris Wilson05235c52016-07-20 09:21:08 +0100870
871 might_sleep();
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100872#if IS_ENABLED(CONFIG_LOCKDEP)
Chris Wilsone95433c2016-10-28 13:58:27 +0100873 GEM_BUG_ON(debug_locks &&
874 !!lockdep_is_held(&req->i915->drm.struct_mutex) !=
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100875 !!(flags & I915_WAIT_LOCKED));
876#endif
Chris Wilsone95433c2016-10-28 13:58:27 +0100877 GEM_BUG_ON(timeout < 0);
Chris Wilson05235c52016-07-20 09:21:08 +0100878
Chris Wilson05235c52016-07-20 09:21:08 +0100879 if (i915_gem_request_completed(req))
Chris Wilsone95433c2016-10-28 13:58:27 +0100880 return timeout;
Chris Wilson05235c52016-07-20 09:21:08 +0100881
Chris Wilsone95433c2016-10-28 13:58:27 +0100882 if (!timeout)
883 return -ETIME;
Chris Wilson05235c52016-07-20 09:21:08 +0100884
885 trace_i915_gem_request_wait_begin(req);
886
Chris Wilson4680816b2016-10-28 13:58:48 +0100887 if (!i915_sw_fence_done(&req->submit)) {
888 timeout = __i915_request_wait_for_submit(req, flags, timeout);
889 if (timeout < 0)
890 goto complete;
891
892 GEM_BUG_ON(!i915_sw_fence_done(&req->submit));
893 }
Chris Wilson65e47602016-10-28 13:58:49 +0100894 GEM_BUG_ON(!req->global_seqno);
Chris Wilson4680816b2016-10-28 13:58:48 +0100895
Daniel Vetter437c3082016-08-05 18:11:24 +0200896 /* Optimistic short spin before touching IRQs */
Chris Wilson05235c52016-07-20 09:21:08 +0100897 if (i915_spin_request(req, state, 5))
898 goto complete;
899
900 set_current_state(state);
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100901 if (flags & I915_WAIT_LOCKED)
902 add_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
Chris Wilson05235c52016-07-20 09:21:08 +0100903
Chris Wilson65e47602016-10-28 13:58:49 +0100904 intel_wait_init(&wait, req->global_seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100905 if (intel_engine_add_wait(req->engine, &wait))
906 /* In order to check that we haven't missed the interrupt
907 * as we enabled it, we need to kick ourselves to do a
908 * coherent check on the seqno before we sleep.
909 */
910 goto wakeup;
911
912 for (;;) {
913 if (signal_pending_state(state, current)) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100914 timeout = -ERESTARTSYS;
Chris Wilson05235c52016-07-20 09:21:08 +0100915 break;
916 }
917
Chris Wilsone95433c2016-10-28 13:58:27 +0100918 if (!timeout) {
919 timeout = -ETIME;
Chris Wilson05235c52016-07-20 09:21:08 +0100920 break;
921 }
922
Chris Wilsone95433c2016-10-28 13:58:27 +0100923 timeout = io_schedule_timeout(timeout);
924
Chris Wilson05235c52016-07-20 09:21:08 +0100925 if (intel_wait_complete(&wait))
926 break;
927
928 set_current_state(state);
929
930wakeup:
931 /* Carefully check if the request is complete, giving time
932 * for the seqno to be visible following the interrupt.
933 * We also have to check in case we are kicked by the GPU
934 * reset in order to drop the struct_mutex.
935 */
936 if (__i915_request_irq_complete(req))
937 break;
938
Chris Wilson221fe792016-09-09 14:11:51 +0100939 /* If the GPU is hung, and we hold the lock, reset the GPU
940 * and then check for completion. On a full reset, the engine's
941 * HW seqno will be advanced passed us and we are complete.
942 * If we do a partial reset, we have to wait for the GPU to
943 * resume and update the breadcrumb.
944 *
945 * If we don't hold the mutex, we can just wait for the worker
946 * to come along and update the breadcrumb (either directly
947 * itself, or indirectly by recovering the GPU).
948 */
949 if (flags & I915_WAIT_LOCKED &&
950 i915_reset_in_progress(&req->i915->gpu_error)) {
951 __set_current_state(TASK_RUNNING);
952 i915_reset(req->i915);
953 reset_wait_queue(&req->i915->gpu_error.wait_queue,
954 &reset);
955 continue;
956 }
957
Chris Wilson05235c52016-07-20 09:21:08 +0100958 /* Only spin if we know the GPU is processing this request */
959 if (i915_spin_request(req, state, 2))
960 break;
961 }
Chris Wilson05235c52016-07-20 09:21:08 +0100962
963 intel_engine_remove_wait(req->engine, &wait);
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100964 if (flags & I915_WAIT_LOCKED)
965 remove_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
Chris Wilson05235c52016-07-20 09:21:08 +0100966 __set_current_state(TASK_RUNNING);
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100967
Chris Wilson05235c52016-07-20 09:21:08 +0100968complete:
969 trace_i915_gem_request_wait_end(req);
970
Chris Wilsone95433c2016-10-28 13:58:27 +0100971 return timeout;
Chris Wilson05235c52016-07-20 09:21:08 +0100972}
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100973
Chris Wilsonf6407192016-08-27 08:54:00 +0100974static bool engine_retire_requests(struct intel_engine_cs *engine)
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100975{
976 struct drm_i915_gem_request *request, *next;
977
Chris Wilson73cb9702016-10-28 13:58:46 +0100978 list_for_each_entry_safe(request, next,
979 &engine->timeline->requests, link) {
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100980 if (!i915_gem_request_completed(request))
Chris Wilsonf6407192016-08-27 08:54:00 +0100981 return false;
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100982
983 i915_gem_request_retire(request);
984 }
Chris Wilsonf6407192016-08-27 08:54:00 +0100985
986 return true;
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100987}
988
989void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
990{
991 struct intel_engine_cs *engine;
Chris Wilsonbafb0fc2016-08-27 08:54:01 +0100992 unsigned int tmp;
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100993
994 lockdep_assert_held(&dev_priv->drm.struct_mutex);
995
996 if (dev_priv->gt.active_engines == 0)
997 return;
998
999 GEM_BUG_ON(!dev_priv->gt.awake);
1000
Chris Wilsonbafb0fc2016-08-27 08:54:01 +01001001 for_each_engine_masked(engine, dev_priv, dev_priv->gt.active_engines, tmp)
Chris Wilsonf6407192016-08-27 08:54:00 +01001002 if (engine_retire_requests(engine))
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001003 dev_priv->gt.active_engines &= ~intel_engine_flag(engine);
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001004
1005 if (dev_priv->gt.active_engines == 0)
1006 queue_delayed_work(dev_priv->wq,
1007 &dev_priv->gt.idle_work,
1008 msecs_to_jiffies(100));
1009}