blob: be9e23b32e4a3b1fd91abf8ed98715c6d8ac0d13 [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;
Chris Wilson98f29e82016-10-28 13:58:51 +0100437 GEM_BUG_ON(req->reserved_space < engine->emit_breadcrumb_sz);
Chris Wilson05235c52016-07-20 09:21:08 +0100438
439 if (i915.enable_execlists)
440 ret = intel_logical_ring_alloc_request_extras(req);
441 else
442 ret = intel_ring_alloc_request_extras(req);
443 if (ret)
444 goto err_ctx;
445
Chris Wilsond0454462016-08-15 10:48:40 +0100446 /* Record the position of the start of the request so that
447 * should we detect the updated seqno part-way through the
448 * GPU processing the request, we never over-estimate the
449 * position of the head.
450 */
451 req->head = req->ring->tail;
452
Chris Wilson8e637172016-08-02 22:50:26 +0100453 return req;
Chris Wilson05235c52016-07-20 09:21:08 +0100454
455err_ctx:
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100456 i915_gem_context_put(ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100457err:
458 kmem_cache_free(dev_priv->requests, req);
Chris Wilson8e637172016-08-02 22:50:26 +0100459 return ERR_PTR(ret);
Chris Wilson05235c52016-07-20 09:21:08 +0100460}
461
Chris Wilsona2bc4692016-09-09 14:11:56 +0100462static int
463i915_gem_request_await_request(struct drm_i915_gem_request *to,
464 struct drm_i915_gem_request *from)
465{
466 int idx, ret;
467
468 GEM_BUG_ON(to == from);
469
Chris Wilson73cb9702016-10-28 13:58:46 +0100470 if (to->timeline == from->timeline)
Chris Wilsona2bc4692016-09-09 14:11:56 +0100471 return 0;
472
Chris Wilson73cb9702016-10-28 13:58:46 +0100473 if (to->engine == from->engine) {
474 ret = i915_sw_fence_await_sw_fence_gfp(&to->submit,
475 &from->submit,
476 GFP_KERNEL);
477 return ret < 0 ? ret : 0;
478 }
479
Chris Wilson65e47602016-10-28 13:58:49 +0100480 if (!from->global_seqno) {
481 ret = i915_sw_fence_await_dma_fence(&to->submit,
482 &from->fence, 0,
483 GFP_KERNEL);
484 return ret < 0 ? ret : 0;
485 }
486
Chris Wilsona2bc4692016-09-09 14:11:56 +0100487 idx = intel_engine_sync_index(from->engine, to->engine);
Chris Wilson65e47602016-10-28 13:58:49 +0100488 if (from->global_seqno <= from->engine->semaphore.sync_seqno[idx])
Chris Wilsona2bc4692016-09-09 14:11:56 +0100489 return 0;
490
491 trace_i915_gem_ring_sync_to(to, from);
492 if (!i915.semaphores) {
Chris Wilson0a046a02016-09-09 14:12:00 +0100493 if (!i915_spin_request(from, TASK_INTERRUPTIBLE, 2)) {
494 ret = i915_sw_fence_await_dma_fence(&to->submit,
495 &from->fence, 0,
496 GFP_KERNEL);
497 if (ret < 0)
498 return ret;
499 }
Chris Wilsona2bc4692016-09-09 14:11:56 +0100500 } else {
501 ret = to->engine->semaphore.sync_to(to, from);
502 if (ret)
503 return ret;
504 }
505
Chris Wilson65e47602016-10-28 13:58:49 +0100506 from->engine->semaphore.sync_seqno[idx] = from->global_seqno;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100507 return 0;
508}
509
Chris Wilsonb52992c2016-10-28 13:58:24 +0100510int
511i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
512 struct dma_fence *fence)
513{
514 struct dma_fence_array *array;
515 int ret;
516 int i;
517
518 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
519 return 0;
520
521 if (dma_fence_is_i915(fence))
522 return i915_gem_request_await_request(req, to_request(fence));
523
524 if (!dma_fence_is_array(fence)) {
525 ret = i915_sw_fence_await_dma_fence(&req->submit,
526 fence, I915_FENCE_TIMEOUT,
527 GFP_KERNEL);
528 return ret < 0 ? ret : 0;
529 }
530
531 /* Note that if the fence-array was created in signal-on-any mode,
532 * we should *not* decompose it into its individual fences. However,
533 * we don't currently store which mode the fence-array is operating
534 * in. Fortunately, the only user of signal-on-any is private to
535 * amdgpu and we should not see any incoming fence-array from
536 * sync-file being in signal-on-any mode.
537 */
538
539 array = to_dma_fence_array(fence);
540 for (i = 0; i < array->num_fences; i++) {
541 struct dma_fence *child = array->fences[i];
542
543 if (dma_fence_is_i915(child))
544 ret = i915_gem_request_await_request(req,
545 to_request(child));
546 else
547 ret = i915_sw_fence_await_dma_fence(&req->submit,
548 child, I915_FENCE_TIMEOUT,
549 GFP_KERNEL);
550 if (ret < 0)
551 return ret;
552 }
553
554 return 0;
555}
556
Chris Wilsona2bc4692016-09-09 14:11:56 +0100557/**
558 * i915_gem_request_await_object - set this request to (async) wait upon a bo
559 *
560 * @to: request we are wishing to use
561 * @obj: object which may be in use on another ring.
562 *
563 * This code is meant to abstract object synchronization with the GPU.
564 * Conceptually we serialise writes between engines inside the GPU.
565 * We only allow one engine to write into a buffer at any time, but
566 * multiple readers. To ensure each has a coherent view of memory, we must:
567 *
568 * - If there is an outstanding write request to the object, the new
569 * request must wait for it to complete (either CPU or in hw, requests
570 * on the same ring will be naturally ordered).
571 *
572 * - If we are a write request (pending_write_domain is set), the new
573 * request must wait for outstanding read requests to complete.
574 *
575 * Returns 0 if successful, else propagates up the lower layer error.
576 */
577int
578i915_gem_request_await_object(struct drm_i915_gem_request *to,
579 struct drm_i915_gem_object *obj,
580 bool write)
581{
Chris Wilsond07f0e52016-10-28 13:58:44 +0100582 struct dma_fence *excl;
583 int ret = 0;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100584
585 if (write) {
Chris Wilsond07f0e52016-10-28 13:58:44 +0100586 struct dma_fence **shared;
587 unsigned int count, i;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100588
Chris Wilsond07f0e52016-10-28 13:58:44 +0100589 ret = reservation_object_get_fences_rcu(obj->resv,
590 &excl, &count, &shared);
Chris Wilsona2bc4692016-09-09 14:11:56 +0100591 if (ret)
592 return ret;
Chris Wilsond07f0e52016-10-28 13:58:44 +0100593
594 for (i = 0; i < count; i++) {
595 ret = i915_gem_request_await_dma_fence(to, shared[i]);
596 if (ret)
597 break;
598
599 dma_fence_put(shared[i]);
600 }
601
602 for (; i < count; i++)
603 dma_fence_put(shared[i]);
604 kfree(shared);
605 } else {
606 excl = reservation_object_get_excl_rcu(obj->resv);
Chris Wilsona2bc4692016-09-09 14:11:56 +0100607 }
608
Chris Wilsond07f0e52016-10-28 13:58:44 +0100609 if (excl) {
610 if (ret == 0)
611 ret = i915_gem_request_await_dma_fence(to, excl);
612
613 dma_fence_put(excl);
614 }
615
616 return ret;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100617}
618
Chris Wilson05235c52016-07-20 09:21:08 +0100619static void i915_gem_mark_busy(const struct intel_engine_cs *engine)
620{
621 struct drm_i915_private *dev_priv = engine->i915;
622
623 dev_priv->gt.active_engines |= intel_engine_flag(engine);
624 if (dev_priv->gt.awake)
625 return;
626
627 intel_runtime_pm_get_noresume(dev_priv);
628 dev_priv->gt.awake = true;
629
Chris Wilson54b4f682016-07-21 21:16:19 +0100630 intel_enable_gt_powersave(dev_priv);
Chris Wilson05235c52016-07-20 09:21:08 +0100631 i915_update_gfx_val(dev_priv);
632 if (INTEL_GEN(dev_priv) >= 6)
633 gen6_rps_busy(dev_priv);
634
635 queue_delayed_work(dev_priv->wq,
636 &dev_priv->gt.retire_work,
637 round_jiffies_up_relative(HZ));
638}
639
640/*
641 * NB: This function is not allowed to fail. Doing so would mean the the
642 * request is not being tracked for completion but the work itself is
643 * going to happen on the hardware. This would be a Bad Thing(tm).
644 */
Chris Wilson17f298cf2016-08-10 13:41:46 +0100645void __i915_add_request(struct drm_i915_gem_request *request, bool flush_caches)
Chris Wilson05235c52016-07-20 09:21:08 +0100646{
Chris Wilson95b2ab52016-08-15 10:48:46 +0100647 struct intel_engine_cs *engine = request->engine;
648 struct intel_ring *ring = request->ring;
Chris Wilson73cb9702016-10-28 13:58:46 +0100649 struct intel_timeline *timeline = request->timeline;
Chris Wilson0a046a02016-09-09 14:12:00 +0100650 struct drm_i915_gem_request *prev;
Chris Wilson05235c52016-07-20 09:21:08 +0100651 u32 request_start;
652 u32 reserved_tail;
653 int ret;
654
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100655 lockdep_assert_held(&request->i915->drm.struct_mutex);
Chris Wilson0f25dff2016-09-09 14:11:55 +0100656 trace_i915_gem_request_add(request);
657
Chris Wilson05235c52016-07-20 09:21:08 +0100658 /*
659 * To ensure that this call will not fail, space for its emissions
660 * should already have been reserved in the ring buffer. Let the ring
661 * know that it is time to use that space up.
662 */
Chris Wilsonba76d912016-08-02 22:50:28 +0100663 request_start = ring->tail;
Chris Wilson05235c52016-07-20 09:21:08 +0100664 reserved_tail = request->reserved_space;
665 request->reserved_space = 0;
666
667 /*
668 * Emit any outstanding flushes - execbuf can fail to emit the flush
669 * after having emitted the batchbuffer command. Hence we need to fix
670 * things up similar to emitting the lazy request. The difference here
671 * is that the flush _must_ happen before the next request, no matter
672 * what.
673 */
674 if (flush_caches) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100675 ret = engine->emit_flush(request, EMIT_FLUSH);
Chris Wilsonc7fe7d22016-08-02 22:50:24 +0100676
Chris Wilson05235c52016-07-20 09:21:08 +0100677 /* Not allowed to fail! */
Chris Wilsonc7fe7d22016-08-02 22:50:24 +0100678 WARN(ret, "engine->emit_flush() failed: %d!\n", ret);
Chris Wilson05235c52016-07-20 09:21:08 +0100679 }
680
Chris Wilsond0454462016-08-15 10:48:40 +0100681 /* Record the position of the start of the breadcrumb so that
Chris Wilson05235c52016-07-20 09:21:08 +0100682 * should we detect the updated seqno part-way through the
683 * GPU processing the request, we never over-estimate the
Chris Wilsond0454462016-08-15 10:48:40 +0100684 * position of the ring's HEAD.
Chris Wilson05235c52016-07-20 09:21:08 +0100685 */
Chris Wilsonba76d912016-08-02 22:50:28 +0100686 request->postfix = ring->tail;
Chris Wilson05235c52016-07-20 09:21:08 +0100687
Chris Wilson05235c52016-07-20 09:21:08 +0100688 /* Not allowed to fail! */
Chris Wilson9b81d552016-10-28 13:58:50 +0100689 ret = engine->emit_breadcrumb(request);
690 WARN(ret, "(%s)->emit_breadcrumb failed: %d!\n", engine->name, ret);
Chris Wilsonc5efa1a2016-08-02 22:50:29 +0100691
Chris Wilson05235c52016-07-20 09:21:08 +0100692 /* Sanity check that the reserved size was large enough. */
Chris Wilsonba76d912016-08-02 22:50:28 +0100693 ret = ring->tail - request_start;
Chris Wilson05235c52016-07-20 09:21:08 +0100694 if (ret < 0)
Chris Wilson1dae2df2016-08-02 22:50:19 +0100695 ret += ring->size;
Chris Wilson05235c52016-07-20 09:21:08 +0100696 WARN_ONCE(ret > reserved_tail,
697 "Not enough space reserved (%d bytes) "
698 "for adding the request (%d bytes)\n",
699 reserved_tail, ret);
700
Chris Wilson0f25dff2016-09-09 14:11:55 +0100701 /* Seal the request and mark it as pending execution. Note that
702 * we may inspect this state, without holding any locks, during
703 * hangcheck. Hence we apply the barrier to ensure that we do not
704 * see a more recent value in the hws than we are tracking.
705 */
Chris Wilson0a046a02016-09-09 14:12:00 +0100706
Chris Wilson73cb9702016-10-28 13:58:46 +0100707 prev = i915_gem_active_raw(&timeline->last_request,
Chris Wilson0a046a02016-09-09 14:12:00 +0100708 &request->i915->drm.struct_mutex);
709 if (prev)
710 i915_sw_fence_await_sw_fence(&request->submit, &prev->submit,
711 &request->submitq);
712
Chris Wilson0f25dff2016-09-09 14:11:55 +0100713 request->emitted_jiffies = jiffies;
Chris Wilson73cb9702016-10-28 13:58:46 +0100714 request->previous_seqno = timeline->last_pending_seqno;
715 timeline->last_pending_seqno = request->fence.seqno;
716 i915_gem_active_set(&timeline->last_request, request);
717 list_add_tail(&request->link, &timeline->requests);
Chris Wilson0f25dff2016-09-09 14:11:55 +0100718 list_add_tail(&request->ring_link, &ring->request_list);
719
Chris Wilson05235c52016-07-20 09:21:08 +0100720 i915_gem_mark_busy(engine);
Chris Wilson5590af32016-09-09 14:11:54 +0100721
722 local_bh_disable();
723 i915_sw_fence_commit(&request->submit);
724 local_bh_enable(); /* Kick the execlists tasklet if just scheduled */
Chris Wilson05235c52016-07-20 09:21:08 +0100725}
726
Chris Wilson221fe792016-09-09 14:11:51 +0100727static void reset_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
728{
729 unsigned long flags;
730
731 spin_lock_irqsave(&q->lock, flags);
732 if (list_empty(&wait->task_list))
733 __add_wait_queue(q, wait);
734 spin_unlock_irqrestore(&q->lock, flags);
735}
736
Chris Wilson05235c52016-07-20 09:21:08 +0100737static unsigned long local_clock_us(unsigned int *cpu)
738{
739 unsigned long t;
740
741 /* Cheaply and approximately convert from nanoseconds to microseconds.
742 * The result and subsequent calculations are also defined in the same
743 * approximate microseconds units. The principal source of timing
744 * error here is from the simple truncation.
745 *
746 * Note that local_clock() is only defined wrt to the current CPU;
747 * the comparisons are no longer valid if we switch CPUs. Instead of
748 * blocking preemption for the entire busywait, we can detect the CPU
749 * switch and use that as indicator of system load and a reason to
750 * stop busywaiting, see busywait_stop().
751 */
752 *cpu = get_cpu();
753 t = local_clock() >> 10;
754 put_cpu();
755
756 return t;
757}
758
759static bool busywait_stop(unsigned long timeout, unsigned int cpu)
760{
761 unsigned int this_cpu;
762
763 if (time_after(local_clock_us(&this_cpu), timeout))
764 return true;
765
766 return this_cpu != cpu;
767}
768
769bool __i915_spin_request(const struct drm_i915_gem_request *req,
770 int state, unsigned long timeout_us)
771{
772 unsigned int cpu;
773
774 /* When waiting for high frequency requests, e.g. during synchronous
775 * rendering split between the CPU and GPU, the finite amount of time
776 * required to set up the irq and wait upon it limits the response
777 * rate. By busywaiting on the request completion for a short while we
778 * can service the high frequency waits as quick as possible. However,
779 * if it is a slow request, we want to sleep as quickly as possible.
780 * The tradeoff between waiting and sleeping is roughly the time it
781 * takes to sleep on a request, on the order of a microsecond.
782 */
783
784 timeout_us += local_clock_us(&cpu);
785 do {
Chris Wilson65e47602016-10-28 13:58:49 +0100786 if (__i915_gem_request_completed(req))
Chris Wilson05235c52016-07-20 09:21:08 +0100787 return true;
788
789 if (signal_pending_state(state, current))
790 break;
791
792 if (busywait_stop(timeout_us, cpu))
793 break;
794
795 cpu_relax_lowlatency();
796 } while (!need_resched());
797
798 return false;
799}
800
Chris Wilson4680816b2016-10-28 13:58:48 +0100801static long
802__i915_request_wait_for_submit(struct drm_i915_gem_request *request,
803 unsigned int flags,
804 long timeout)
805{
806 const int state = flags & I915_WAIT_INTERRUPTIBLE ?
807 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
808 wait_queue_head_t *q = &request->i915->gpu_error.wait_queue;
809 DEFINE_WAIT(reset);
810 DEFINE_WAIT(wait);
811
812 if (flags & I915_WAIT_LOCKED)
813 add_wait_queue(q, &reset);
814
815 do {
816 prepare_to_wait(&request->submit.wait, &wait, state);
817
818 if (i915_sw_fence_done(&request->submit))
819 break;
820
821 if (flags & I915_WAIT_LOCKED &&
822 i915_reset_in_progress(&request->i915->gpu_error)) {
823 __set_current_state(TASK_RUNNING);
824 i915_reset(request->i915);
825 reset_wait_queue(q, &reset);
826 continue;
827 }
828
829 if (signal_pending_state(state, current)) {
830 timeout = -ERESTARTSYS;
831 break;
832 }
833
834 timeout = io_schedule_timeout(timeout);
835 } while (timeout);
836 finish_wait(&request->submit.wait, &wait);
837
838 if (flags & I915_WAIT_LOCKED)
839 remove_wait_queue(q, &reset);
840
841 return timeout;
842}
843
Chris Wilson05235c52016-07-20 09:21:08 +0100844/**
Chris Wilson776f3232016-08-04 07:52:40 +0100845 * i915_wait_request - wait until execution of request has finished
Chris Wilsone95433c2016-10-28 13:58:27 +0100846 * @req: the request to wait upon
Chris Wilsonea746f32016-09-09 14:11:49 +0100847 * @flags: how to wait
Chris Wilsone95433c2016-10-28 13:58:27 +0100848 * @timeout: how long to wait in jiffies
Chris Wilson05235c52016-07-20 09:21:08 +0100849 *
Chris Wilsone95433c2016-10-28 13:58:27 +0100850 * i915_wait_request() waits for the request to be completed, for a
851 * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
852 * unbounded wait).
Chris Wilson05235c52016-07-20 09:21:08 +0100853 *
Chris Wilsone95433c2016-10-28 13:58:27 +0100854 * If the caller holds the struct_mutex, the caller must pass I915_WAIT_LOCKED
855 * in via the flags, and vice versa if the struct_mutex is not held, the caller
856 * must not specify that the wait is locked.
857 *
858 * Returns the remaining time (in jiffies) if the request completed, which may
859 * be zero or -ETIME if the request is unfinished after the timeout expires.
860 * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
861 * pending before the request completes.
Chris Wilson05235c52016-07-20 09:21:08 +0100862 */
Chris Wilsone95433c2016-10-28 13:58:27 +0100863long i915_wait_request(struct drm_i915_gem_request *req,
864 unsigned int flags,
865 long timeout)
Chris Wilson05235c52016-07-20 09:21:08 +0100866{
Chris Wilsonea746f32016-09-09 14:11:49 +0100867 const int state = flags & I915_WAIT_INTERRUPTIBLE ?
868 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
Chris Wilson05235c52016-07-20 09:21:08 +0100869 DEFINE_WAIT(reset);
870 struct intel_wait wait;
Chris Wilson05235c52016-07-20 09:21:08 +0100871
872 might_sleep();
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100873#if IS_ENABLED(CONFIG_LOCKDEP)
Chris Wilsone95433c2016-10-28 13:58:27 +0100874 GEM_BUG_ON(debug_locks &&
875 !!lockdep_is_held(&req->i915->drm.struct_mutex) !=
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100876 !!(flags & I915_WAIT_LOCKED));
877#endif
Chris Wilsone95433c2016-10-28 13:58:27 +0100878 GEM_BUG_ON(timeout < 0);
Chris Wilson05235c52016-07-20 09:21:08 +0100879
Chris Wilson05235c52016-07-20 09:21:08 +0100880 if (i915_gem_request_completed(req))
Chris Wilsone95433c2016-10-28 13:58:27 +0100881 return timeout;
Chris Wilson05235c52016-07-20 09:21:08 +0100882
Chris Wilsone95433c2016-10-28 13:58:27 +0100883 if (!timeout)
884 return -ETIME;
Chris Wilson05235c52016-07-20 09:21:08 +0100885
886 trace_i915_gem_request_wait_begin(req);
887
Chris Wilson4680816b2016-10-28 13:58:48 +0100888 if (!i915_sw_fence_done(&req->submit)) {
889 timeout = __i915_request_wait_for_submit(req, flags, timeout);
890 if (timeout < 0)
891 goto complete;
892
893 GEM_BUG_ON(!i915_sw_fence_done(&req->submit));
894 }
Chris Wilson65e47602016-10-28 13:58:49 +0100895 GEM_BUG_ON(!req->global_seqno);
Chris Wilson4680816b2016-10-28 13:58:48 +0100896
Daniel Vetter437c3082016-08-05 18:11:24 +0200897 /* Optimistic short spin before touching IRQs */
Chris Wilson05235c52016-07-20 09:21:08 +0100898 if (i915_spin_request(req, state, 5))
899 goto complete;
900
901 set_current_state(state);
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100902 if (flags & I915_WAIT_LOCKED)
903 add_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
Chris Wilson05235c52016-07-20 09:21:08 +0100904
Chris Wilson65e47602016-10-28 13:58:49 +0100905 intel_wait_init(&wait, req->global_seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100906 if (intel_engine_add_wait(req->engine, &wait))
907 /* In order to check that we haven't missed the interrupt
908 * as we enabled it, we need to kick ourselves to do a
909 * coherent check on the seqno before we sleep.
910 */
911 goto wakeup;
912
913 for (;;) {
914 if (signal_pending_state(state, current)) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100915 timeout = -ERESTARTSYS;
Chris Wilson05235c52016-07-20 09:21:08 +0100916 break;
917 }
918
Chris Wilsone95433c2016-10-28 13:58:27 +0100919 if (!timeout) {
920 timeout = -ETIME;
Chris Wilson05235c52016-07-20 09:21:08 +0100921 break;
922 }
923
Chris Wilsone95433c2016-10-28 13:58:27 +0100924 timeout = io_schedule_timeout(timeout);
925
Chris Wilson05235c52016-07-20 09:21:08 +0100926 if (intel_wait_complete(&wait))
927 break;
928
929 set_current_state(state);
930
931wakeup:
932 /* Carefully check if the request is complete, giving time
933 * for the seqno to be visible following the interrupt.
934 * We also have to check in case we are kicked by the GPU
935 * reset in order to drop the struct_mutex.
936 */
937 if (__i915_request_irq_complete(req))
938 break;
939
Chris Wilson221fe792016-09-09 14:11:51 +0100940 /* If the GPU is hung, and we hold the lock, reset the GPU
941 * and then check for completion. On a full reset, the engine's
942 * HW seqno will be advanced passed us and we are complete.
943 * If we do a partial reset, we have to wait for the GPU to
944 * resume and update the breadcrumb.
945 *
946 * If we don't hold the mutex, we can just wait for the worker
947 * to come along and update the breadcrumb (either directly
948 * itself, or indirectly by recovering the GPU).
949 */
950 if (flags & I915_WAIT_LOCKED &&
951 i915_reset_in_progress(&req->i915->gpu_error)) {
952 __set_current_state(TASK_RUNNING);
953 i915_reset(req->i915);
954 reset_wait_queue(&req->i915->gpu_error.wait_queue,
955 &reset);
956 continue;
957 }
958
Chris Wilson05235c52016-07-20 09:21:08 +0100959 /* Only spin if we know the GPU is processing this request */
960 if (i915_spin_request(req, state, 2))
961 break;
962 }
Chris Wilson05235c52016-07-20 09:21:08 +0100963
964 intel_engine_remove_wait(req->engine, &wait);
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100965 if (flags & I915_WAIT_LOCKED)
966 remove_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
Chris Wilson05235c52016-07-20 09:21:08 +0100967 __set_current_state(TASK_RUNNING);
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100968
Chris Wilson05235c52016-07-20 09:21:08 +0100969complete:
970 trace_i915_gem_request_wait_end(req);
971
Chris Wilsone95433c2016-10-28 13:58:27 +0100972 return timeout;
Chris Wilson05235c52016-07-20 09:21:08 +0100973}
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100974
Chris Wilsonf6407192016-08-27 08:54:00 +0100975static bool engine_retire_requests(struct intel_engine_cs *engine)
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100976{
977 struct drm_i915_gem_request *request, *next;
978
Chris Wilson73cb9702016-10-28 13:58:46 +0100979 list_for_each_entry_safe(request, next,
980 &engine->timeline->requests, link) {
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100981 if (!i915_gem_request_completed(request))
Chris Wilsonf6407192016-08-27 08:54:00 +0100982 return false;
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100983
984 i915_gem_request_retire(request);
985 }
Chris Wilsonf6407192016-08-27 08:54:00 +0100986
987 return true;
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100988}
989
990void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
991{
992 struct intel_engine_cs *engine;
Chris Wilsonbafb0fc2016-08-27 08:54:01 +0100993 unsigned int tmp;
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100994
995 lockdep_assert_held(&dev_priv->drm.struct_mutex);
996
997 if (dev_priv->gt.active_engines == 0)
998 return;
999
1000 GEM_BUG_ON(!dev_priv->gt.awake);
1001
Chris Wilsonbafb0fc2016-08-27 08:54:01 +01001002 for_each_engine_masked(engine, dev_priv, dev_priv->gt.active_engines, tmp)
Chris Wilsonf6407192016-08-27 08:54:00 +01001003 if (engine_retire_requests(engine))
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001004 dev_priv->gt.active_engines &= ~intel_engine_flag(engine);
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001005
1006 if (dev_priv->gt.active_engines == 0)
1007 queue_delayed_work(dev_priv->wq,
1008 &dev_priv->gt.idle_work,
1009 msecs_to_jiffies(100));
1010}