blob: 79b0046d9a577364dfc58a650323369073ebfb0c [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{
Chris Wilson73cb9702016-10-28 13:58:46 +010037 return to_request(fence)->timeline->common->name;
Chris Wilson04769652016-07-20 09:21:11 +010038}
39
Chris Wilsonf54d1862016-10-25 13:00:45 +010040static bool i915_fence_signaled(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010041{
42 return i915_gem_request_completed(to_request(fence));
43}
44
Chris Wilsonf54d1862016-10-25 13:00:45 +010045static bool i915_fence_enable_signaling(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010046{
47 if (i915_fence_signaled(fence))
48 return false;
49
50 intel_engine_enable_signaling(to_request(fence));
51 return true;
52}
53
Chris Wilsonf54d1862016-10-25 13:00:45 +010054static signed long i915_fence_wait(struct dma_fence *fence,
Chris Wilson04769652016-07-20 09:21:11 +010055 bool interruptible,
Chris Wilsone95433c2016-10-28 13:58:27 +010056 signed long timeout)
Chris Wilson04769652016-07-20 09:21:11 +010057{
Chris Wilsone95433c2016-10-28 13:58:27 +010058 return i915_wait_request(to_request(fence), interruptible, timeout);
Chris Wilson04769652016-07-20 09:21:11 +010059}
60
Chris Wilsonf54d1862016-10-25 13:00:45 +010061static void i915_fence_release(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010062{
63 struct drm_i915_gem_request *req = to_request(fence);
64
65 kmem_cache_free(req->i915->requests, req);
66}
67
Chris Wilsonf54d1862016-10-25 13:00:45 +010068const struct dma_fence_ops i915_fence_ops = {
Chris Wilson04769652016-07-20 09:21:11 +010069 .get_driver_name = i915_fence_get_driver_name,
70 .get_timeline_name = i915_fence_get_timeline_name,
71 .enable_signaling = i915_fence_enable_signaling,
72 .signaled = i915_fence_signaled,
73 .wait = i915_fence_wait,
74 .release = i915_fence_release,
Chris Wilson04769652016-07-20 09:21:11 +010075};
76
Chris Wilson05235c52016-07-20 09:21:08 +010077int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
78 struct drm_file *file)
79{
80 struct drm_i915_private *dev_private;
81 struct drm_i915_file_private *file_priv;
82
83 WARN_ON(!req || !file || req->file_priv);
84
85 if (!req || !file)
86 return -EINVAL;
87
88 if (req->file_priv)
89 return -EINVAL;
90
91 dev_private = req->i915;
92 file_priv = file->driver_priv;
93
94 spin_lock(&file_priv->mm.lock);
95 req->file_priv = file_priv;
96 list_add_tail(&req->client_list, &file_priv->mm.request_list);
97 spin_unlock(&file_priv->mm.lock);
98
Chris Wilson05235c52016-07-20 09:21:08 +010099 return 0;
100}
101
102static inline void
103i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
104{
105 struct drm_i915_file_private *file_priv = request->file_priv;
106
107 if (!file_priv)
108 return;
109
110 spin_lock(&file_priv->mm.lock);
111 list_del(&request->client_list);
112 request->file_priv = NULL;
113 spin_unlock(&file_priv->mm.lock);
Chris Wilson05235c52016-07-20 09:21:08 +0100114}
115
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100116void i915_gem_retire_noop(struct i915_gem_active *active,
117 struct drm_i915_gem_request *request)
118{
119 /* Space left intentionally blank */
120}
121
Chris Wilson05235c52016-07-20 09:21:08 +0100122static void i915_gem_request_retire(struct drm_i915_gem_request *request)
123{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100124 struct i915_gem_active *active, *next;
125
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100126 lockdep_assert_held(&request->i915->drm.struct_mutex);
127 GEM_BUG_ON(!i915_gem_request_completed(request));
128
Chris Wilson05235c52016-07-20 09:21:08 +0100129 trace_i915_gem_request_retire(request);
Chris Wilson80b204b2016-10-28 13:58:58 +0100130
131 spin_lock_irq(&request->engine->timeline->lock);
Chris Wilsone95433c2016-10-28 13:58:27 +0100132 list_del_init(&request->link);
Chris Wilson80b204b2016-10-28 13:58:58 +0100133 spin_unlock_irq(&request->engine->timeline->lock);
Chris Wilson05235c52016-07-20 09:21:08 +0100134
135 /* We know the GPU must have read the request to have
136 * sent us the seqno + interrupt, so use the position
137 * of tail of the request to update the last known position
138 * of the GPU head.
139 *
140 * Note this requires that we are always called in request
141 * completion order.
142 */
Chris Wilson675d9ad2016-08-04 07:52:36 +0100143 list_del(&request->ring_link);
Chris Wilson1dae2df2016-08-02 22:50:19 +0100144 request->ring->last_retired_head = request->postfix;
Chris Wilson28176ef2016-10-28 13:58:56 +0100145 request->i915->gt.active_requests--;
Chris Wilson05235c52016-07-20 09:21:08 +0100146
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100147 /* Walk through the active list, calling retire on each. This allows
148 * objects to track their GPU activity and mark themselves as idle
149 * when their *last* active request is completed (updating state
150 * tracking lists for eviction, active references for GEM, etc).
151 *
152 * As the ->retire() may free the node, we decouple it first and
153 * pass along the auxiliary information (to avoid dereferencing
154 * the node after the callback).
155 */
156 list_for_each_entry_safe(active, next, &request->active_list, link) {
157 /* In microbenchmarks or focusing upon time inside the kernel,
158 * we may spend an inordinate amount of time simply handling
159 * the retirement of requests and processing their callbacks.
160 * Of which, this loop itself is particularly hot due to the
161 * cache misses when jumping around the list of i915_gem_active.
162 * So we try to keep this loop as streamlined as possible and
163 * also prefetch the next i915_gem_active to try and hide
164 * the likely cache miss.
165 */
166 prefetchw(next);
167
168 INIT_LIST_HEAD(&active->link);
Chris Wilson0eafec62016-08-04 16:32:41 +0100169 RCU_INIT_POINTER(active->request, NULL);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100170
171 active->retire(active, request);
172 }
173
Chris Wilson05235c52016-07-20 09:21:08 +0100174 i915_gem_request_remove_from_client(request);
175
176 if (request->previous_context) {
177 if (i915.enable_execlists)
178 intel_lr_context_unpin(request->previous_context,
179 request->engine);
180 }
181
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100182 i915_gem_context_put(request->ctx);
Chris Wilsond07f0e52016-10-28 13:58:44 +0100183
184 dma_fence_signal(&request->fence);
Chris Wilsone8a261e2016-07-20 13:31:49 +0100185 i915_gem_request_put(request);
Chris Wilson05235c52016-07-20 09:21:08 +0100186}
187
188void i915_gem_request_retire_upto(struct drm_i915_gem_request *req)
189{
190 struct intel_engine_cs *engine = req->engine;
191 struct drm_i915_gem_request *tmp;
192
193 lockdep_assert_held(&req->i915->drm.struct_mutex);
Chris Wilsone95433c2016-10-28 13:58:27 +0100194 if (list_empty(&req->link))
195 return;
Chris Wilson05235c52016-07-20 09:21:08 +0100196
197 do {
Chris Wilson73cb9702016-10-28 13:58:46 +0100198 tmp = list_first_entry(&engine->timeline->requests,
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100199 typeof(*tmp), link);
Chris Wilson05235c52016-07-20 09:21:08 +0100200
201 i915_gem_request_retire(tmp);
202 } while (tmp != req);
Chris Wilson05235c52016-07-20 09:21:08 +0100203}
204
Chris Wilson8af29b02016-09-09 14:11:47 +0100205static int i915_gem_check_wedge(struct drm_i915_private *dev_priv)
Chris Wilson05235c52016-07-20 09:21:08 +0100206{
Chris Wilson8af29b02016-09-09 14:11:47 +0100207 struct i915_gpu_error *error = &dev_priv->gpu_error;
208
209 if (i915_terminally_wedged(error))
Chris Wilson05235c52016-07-20 09:21:08 +0100210 return -EIO;
211
Chris Wilson8af29b02016-09-09 14:11:47 +0100212 if (i915_reset_in_progress(error)) {
Chris Wilson05235c52016-07-20 09:21:08 +0100213 /* Non-interruptible callers can't handle -EAGAIN, hence return
214 * -EIO unconditionally for these.
215 */
Chris Wilson8af29b02016-09-09 14:11:47 +0100216 if (!dev_priv->mm.interruptible)
Chris Wilson05235c52016-07-20 09:21:08 +0100217 return -EIO;
218
219 return -EAGAIN;
220 }
221
222 return 0;
223}
224
Chris Wilson85e17f52016-10-28 13:58:53 +0100225static int i915_gem_init_global_seqno(struct drm_i915_private *i915, u32 seqno)
Chris Wilson05235c52016-07-20 09:21:08 +0100226{
Chris Wilson85e17f52016-10-28 13:58:53 +0100227 struct i915_gem_timeline *timeline = &i915->gt.global_timeline;
Chris Wilson05235c52016-07-20 09:21:08 +0100228 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +0530229 enum intel_engine_id id;
Chris Wilson05235c52016-07-20 09:21:08 +0100230 int ret;
231
232 /* Carefully retire all requests without writing to the rings */
Chris Wilson85e17f52016-10-28 13:58:53 +0100233 ret = i915_gem_wait_for_idle(i915,
Chris Wilson73cb9702016-10-28 13:58:46 +0100234 I915_WAIT_INTERRUPTIBLE |
235 I915_WAIT_LOCKED);
236 if (ret)
237 return ret;
238
Chris Wilson85e17f52016-10-28 13:58:53 +0100239 i915_gem_retire_requests(i915);
Chris Wilson28176ef2016-10-28 13:58:56 +0100240 GEM_BUG_ON(i915->gt.active_requests > 1);
Chris Wilson05235c52016-07-20 09:21:08 +0100241
242 /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
Chris Wilson28176ef2016-10-28 13:58:56 +0100243 if (!i915_seqno_passed(seqno, atomic_read(&timeline->next_seqno))) {
Chris Wilson85e17f52016-10-28 13:58:53 +0100244 while (intel_kick_waiters(i915) || intel_kick_signalers(i915))
Chris Wilson05235c52016-07-20 09:21:08 +0100245 yield();
Chris Wilson73cb9702016-10-28 13:58:46 +0100246 yield();
Chris Wilson05235c52016-07-20 09:21:08 +0100247 }
Chris Wilson28176ef2016-10-28 13:58:56 +0100248 atomic_set(&timeline->next_seqno, seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100249
250 /* Finally reset hw state */
Chris Wilson85e17f52016-10-28 13:58:53 +0100251 for_each_engine(engine, i915, id)
Chris Wilson73cb9702016-10-28 13:58:46 +0100252 intel_engine_init_global_seqno(engine, seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100253
Chris Wilson85e17f52016-10-28 13:58:53 +0100254 list_for_each_entry(timeline, &i915->gt.timelines, link) {
255 for_each_engine(engine, i915, id) {
256 struct intel_timeline *tl = &timeline->engine[id];
257
258 memset(tl->sync_seqno, 0, sizeof(tl->sync_seqno));
259 }
260 }
261
Chris Wilson05235c52016-07-20 09:21:08 +0100262 return 0;
263}
264
Chris Wilson73cb9702016-10-28 13:58:46 +0100265int i915_gem_set_global_seqno(struct drm_device *dev, u32 seqno)
Chris Wilson05235c52016-07-20 09:21:08 +0100266{
267 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson05235c52016-07-20 09:21:08 +0100268
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100269 lockdep_assert_held(&dev_priv->drm.struct_mutex);
270
Chris Wilson05235c52016-07-20 09:21:08 +0100271 if (seqno == 0)
272 return -EINVAL;
273
274 /* HWS page needs to be set less than what we
275 * will inject to ring
276 */
Chris Wilson28176ef2016-10-28 13:58:56 +0100277 return i915_gem_init_global_seqno(dev_priv, seqno - 1);
278}
Chris Wilson05235c52016-07-20 09:21:08 +0100279
Chris Wilson28176ef2016-10-28 13:58:56 +0100280static int reserve_global_seqno(struct drm_i915_private *i915)
281{
282 u32 active_requests = ++i915->gt.active_requests;
283 u32 next_seqno = atomic_read(&i915->gt.global_timeline.next_seqno);
284 int ret;
285
286 /* Reservation is fine until we need to wrap around */
287 if (likely(next_seqno + active_requests > next_seqno))
288 return 0;
289
290 ret = i915_gem_init_global_seqno(i915, 0);
291 if (ret) {
292 i915->gt.active_requests--;
293 return ret;
294 }
295
Chris Wilson05235c52016-07-20 09:21:08 +0100296 return 0;
297}
298
Chris Wilson80b204b2016-10-28 13:58:58 +0100299static u32 __timeline_get_seqno(struct i915_gem_timeline *tl)
300{
301 /* next_seqno only incremented under a mutex */
302 return ++tl->next_seqno.counter;
303}
304
Chris Wilson28176ef2016-10-28 13:58:56 +0100305static u32 timeline_get_seqno(struct i915_gem_timeline *tl)
Chris Wilson05235c52016-07-20 09:21:08 +0100306{
Chris Wilson28176ef2016-10-28 13:58:56 +0100307 return atomic_inc_return(&tl->next_seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100308}
309
Chris Wilson5590af32016-09-09 14:11:54 +0100310static int __i915_sw_fence_call
311submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
312{
313 struct drm_i915_gem_request *request =
314 container_of(fence, typeof(*request), submit);
Chris Wilson73cb9702016-10-28 13:58:46 +0100315 struct intel_engine_cs *engine = request->engine;
Chris Wilsonf2d13292016-10-28 13:58:57 +0100316 struct intel_timeline *timeline;
Chris Wilson80b204b2016-10-28 13:58:58 +0100317 unsigned long flags;
Chris Wilsonf2d13292016-10-28 13:58:57 +0100318 u32 seqno;
Chris Wilson5590af32016-09-09 14:11:54 +0100319
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100320 if (state != FENCE_COMPLETE)
321 return NOTIFY_DONE;
322
Chris Wilson80b204b2016-10-28 13:58:58 +0100323 /* Transfer from per-context onto the global per-engine timeline */
324 timeline = engine->timeline;
325 GEM_BUG_ON(timeline == request->timeline);
326
Chris Wilson5590af32016-09-09 14:11:54 +0100327 /* Will be called from irq-context when using foreign DMA fences */
Chris Wilson80b204b2016-10-28 13:58:58 +0100328 spin_lock_irqsave(&timeline->lock, flags);
Chris Wilson5590af32016-09-09 14:11:54 +0100329
Chris Wilson80b204b2016-10-28 13:58:58 +0100330 seqno = timeline_get_seqno(timeline->common);
Chris Wilsonf2d13292016-10-28 13:58:57 +0100331 GEM_BUG_ON(!seqno);
332 GEM_BUG_ON(i915_seqno_passed(intel_engine_get_seqno(engine), seqno));
333
334 GEM_BUG_ON(i915_seqno_passed(timeline->last_submitted_seqno, seqno));
335 request->previous_seqno = timeline->last_submitted_seqno;
336 timeline->last_submitted_seqno = seqno;
337
338 /* We may be recursing from the signal callback of another i915 fence */
339 spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
340 request->global_seqno = seqno;
341 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
342 intel_engine_enable_signaling(request);
343 spin_unlock(&request->lock);
344
345 GEM_BUG_ON(!request->global_seqno);
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100346 engine->emit_breadcrumb(request,
347 request->ring->vaddr + request->postfix);
348 engine->submit_request(request);
Chris Wilson5590af32016-09-09 14:11:54 +0100349
Chris Wilson80b204b2016-10-28 13:58:58 +0100350 spin_lock_nested(&request->timeline->lock, SINGLE_DEPTH_NESTING);
351 list_move_tail(&request->link, &timeline->requests);
352 spin_unlock(&request->timeline->lock);
353
354 spin_unlock_irqrestore(&timeline->lock, flags);
355
Chris Wilson5590af32016-09-09 14:11:54 +0100356 return NOTIFY_DONE;
357}
358
Chris Wilson8e637172016-08-02 22:50:26 +0100359/**
360 * i915_gem_request_alloc - allocate a request structure
361 *
362 * @engine: engine that we wish to issue the request on.
363 * @ctx: context that the request will be associated with.
364 * This can be NULL if the request is not directly related to
365 * any specific user context, in which case this function will
366 * choose an appropriate context to use.
367 *
368 * Returns a pointer to the allocated request if successful,
369 * or an error code if not.
370 */
371struct drm_i915_gem_request *
372i915_gem_request_alloc(struct intel_engine_cs *engine,
373 struct i915_gem_context *ctx)
Chris Wilson05235c52016-07-20 09:21:08 +0100374{
375 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson05235c52016-07-20 09:21:08 +0100376 struct drm_i915_gem_request *req;
377 int ret;
378
Chris Wilson28176ef2016-10-28 13:58:56 +0100379 lockdep_assert_held(&dev_priv->drm.struct_mutex);
380
Chris Wilson05235c52016-07-20 09:21:08 +0100381 /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
382 * EIO if the GPU is already wedged, or EAGAIN to drop the struct_mutex
383 * and restart.
384 */
Chris Wilson8af29b02016-09-09 14:11:47 +0100385 ret = i915_gem_check_wedge(dev_priv);
Chris Wilson05235c52016-07-20 09:21:08 +0100386 if (ret)
Chris Wilson8e637172016-08-02 22:50:26 +0100387 return ERR_PTR(ret);
Chris Wilson05235c52016-07-20 09:21:08 +0100388
Chris Wilson28176ef2016-10-28 13:58:56 +0100389 ret = reserve_global_seqno(dev_priv);
390 if (ret)
391 return ERR_PTR(ret);
392
Chris Wilson9b5f4e52016-07-20 09:21:09 +0100393 /* Move the oldest request to the slab-cache (if not in use!) */
Chris Wilson73cb9702016-10-28 13:58:46 +0100394 req = list_first_entry_or_null(&engine->timeline->requests,
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100395 typeof(*req), link);
Chris Wilson80b204b2016-10-28 13:58:58 +0100396 if (req && __i915_gem_request_completed(req))
Chris Wilson2a1d7752016-07-26 12:01:51 +0100397 i915_gem_request_retire(req);
Chris Wilson9b5f4e52016-07-20 09:21:09 +0100398
Chris Wilson5a198b82016-08-09 09:23:34 +0100399 /* Beware: Dragons be flying overhead.
400 *
401 * We use RCU to look up requests in flight. The lookups may
402 * race with the request being allocated from the slab freelist.
403 * That is the request we are writing to here, may be in the process
Chris Wilson1426f712016-08-09 17:03:22 +0100404 * of being read by __i915_gem_active_get_rcu(). As such,
Chris Wilson5a198b82016-08-09 09:23:34 +0100405 * we have to be very careful when overwriting the contents. During
406 * the RCU lookup, we change chase the request->engine pointer,
Chris Wilson65e47602016-10-28 13:58:49 +0100407 * read the request->global_seqno and increment the reference count.
Chris Wilson5a198b82016-08-09 09:23:34 +0100408 *
409 * The reference count is incremented atomically. If it is zero,
410 * the lookup knows the request is unallocated and complete. Otherwise,
411 * it is either still in use, or has been reallocated and reset
Chris Wilsonf54d1862016-10-25 13:00:45 +0100412 * with dma_fence_init(). This increment is safe for release as we
413 * check that the request we have a reference to and matches the active
Chris Wilson5a198b82016-08-09 09:23:34 +0100414 * request.
415 *
416 * Before we increment the refcount, we chase the request->engine
417 * pointer. We must not call kmem_cache_zalloc() or else we set
418 * that pointer to NULL and cause a crash during the lookup. If
419 * we see the request is completed (based on the value of the
420 * old engine and seqno), the lookup is complete and reports NULL.
421 * If we decide the request is not completed (new engine or seqno),
422 * then we grab a reference and double check that it is still the
423 * active request - which it won't be and restart the lookup.
424 *
425 * Do not use kmem_cache_zalloc() here!
426 */
427 req = kmem_cache_alloc(dev_priv->requests, GFP_KERNEL);
Chris Wilson28176ef2016-10-28 13:58:56 +0100428 if (!req) {
429 ret = -ENOMEM;
430 goto err_unreserve;
431 }
Chris Wilson05235c52016-07-20 09:21:08 +0100432
Chris Wilson80b204b2016-10-28 13:58:58 +0100433 req->timeline = i915_gem_context_lookup_timeline(ctx, engine);
434 GEM_BUG_ON(req->timeline == engine->timeline);
Chris Wilson73cb9702016-10-28 13:58:46 +0100435
Chris Wilson04769652016-07-20 09:21:11 +0100436 spin_lock_init(&req->lock);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100437 dma_fence_init(&req->fence,
438 &i915_fence_ops,
439 &req->lock,
Chris Wilson73cb9702016-10-28 13:58:46 +0100440 req->timeline->fence_context,
Chris Wilson80b204b2016-10-28 13:58:58 +0100441 __timeline_get_seqno(req->timeline->common));
Chris Wilson04769652016-07-20 09:21:11 +0100442
Chris Wilson5590af32016-09-09 14:11:54 +0100443 i915_sw_fence_init(&req->submit, submit_notify);
444
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100445 INIT_LIST_HEAD(&req->active_list);
Chris Wilson05235c52016-07-20 09:21:08 +0100446 req->i915 = dev_priv;
447 req->engine = engine;
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100448 req->ctx = i915_gem_context_get(ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100449
Chris Wilson5a198b82016-08-09 09:23:34 +0100450 /* No zalloc, must clear what we need by hand */
Chris Wilsonf2d13292016-10-28 13:58:57 +0100451 req->global_seqno = 0;
Chris Wilson5a198b82016-08-09 09:23:34 +0100452 req->previous_context = NULL;
453 req->file_priv = NULL;
Chris Wilson058d88c2016-08-15 10:49:06 +0100454 req->batch = NULL;
Chris Wilson5a198b82016-08-09 09:23:34 +0100455
Chris Wilson05235c52016-07-20 09:21:08 +0100456 /*
457 * Reserve space in the ring buffer for all the commands required to
458 * eventually emit this request. This is to guarantee that the
459 * i915_add_request() call can't fail. Note that the reserve may need
460 * to be redone if the request is not actually submitted straight
461 * away, e.g. because a GPU scheduler has deferred it.
462 */
463 req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
Chris Wilson98f29e82016-10-28 13:58:51 +0100464 GEM_BUG_ON(req->reserved_space < engine->emit_breadcrumb_sz);
Chris Wilson05235c52016-07-20 09:21:08 +0100465
466 if (i915.enable_execlists)
467 ret = intel_logical_ring_alloc_request_extras(req);
468 else
469 ret = intel_ring_alloc_request_extras(req);
470 if (ret)
471 goto err_ctx;
472
Chris Wilsond0454462016-08-15 10:48:40 +0100473 /* Record the position of the start of the request so that
474 * should we detect the updated seqno part-way through the
475 * GPU processing the request, we never over-estimate the
476 * position of the head.
477 */
478 req->head = req->ring->tail;
479
Chris Wilson8e637172016-08-02 22:50:26 +0100480 return req;
Chris Wilson05235c52016-07-20 09:21:08 +0100481
482err_ctx:
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100483 i915_gem_context_put(ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100484 kmem_cache_free(dev_priv->requests, req);
Chris Wilson28176ef2016-10-28 13:58:56 +0100485err_unreserve:
486 dev_priv->gt.active_requests--;
Chris Wilson8e637172016-08-02 22:50:26 +0100487 return ERR_PTR(ret);
Chris Wilson05235c52016-07-20 09:21:08 +0100488}
489
Chris Wilsona2bc4692016-09-09 14:11:56 +0100490static int
491i915_gem_request_await_request(struct drm_i915_gem_request *to,
492 struct drm_i915_gem_request *from)
493{
Chris Wilson85e17f52016-10-28 13:58:53 +0100494 int ret;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100495
496 GEM_BUG_ON(to == from);
497
Chris Wilson73cb9702016-10-28 13:58:46 +0100498 if (to->timeline == from->timeline)
Chris Wilsona2bc4692016-09-09 14:11:56 +0100499 return 0;
500
Chris Wilson73cb9702016-10-28 13:58:46 +0100501 if (to->engine == from->engine) {
502 ret = i915_sw_fence_await_sw_fence_gfp(&to->submit,
503 &from->submit,
504 GFP_KERNEL);
505 return ret < 0 ? ret : 0;
506 }
507
Chris Wilson65e47602016-10-28 13:58:49 +0100508 if (!from->global_seqno) {
509 ret = i915_sw_fence_await_dma_fence(&to->submit,
510 &from->fence, 0,
511 GFP_KERNEL);
512 return ret < 0 ? ret : 0;
513 }
514
Chris Wilson85e17f52016-10-28 13:58:53 +0100515 if (from->global_seqno <= to->timeline->sync_seqno[from->engine->id])
Chris Wilsona2bc4692016-09-09 14:11:56 +0100516 return 0;
517
518 trace_i915_gem_ring_sync_to(to, from);
519 if (!i915.semaphores) {
Chris Wilson0a046a02016-09-09 14:12:00 +0100520 if (!i915_spin_request(from, TASK_INTERRUPTIBLE, 2)) {
521 ret = i915_sw_fence_await_dma_fence(&to->submit,
522 &from->fence, 0,
523 GFP_KERNEL);
524 if (ret < 0)
525 return ret;
526 }
Chris Wilsona2bc4692016-09-09 14:11:56 +0100527 } else {
528 ret = to->engine->semaphore.sync_to(to, from);
529 if (ret)
530 return ret;
531 }
532
Chris Wilson85e17f52016-10-28 13:58:53 +0100533 to->timeline->sync_seqno[from->engine->id] = from->global_seqno;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100534 return 0;
535}
536
Chris Wilsonb52992c2016-10-28 13:58:24 +0100537int
538i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
539 struct dma_fence *fence)
540{
541 struct dma_fence_array *array;
542 int ret;
543 int i;
544
545 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
546 return 0;
547
548 if (dma_fence_is_i915(fence))
549 return i915_gem_request_await_request(req, to_request(fence));
550
551 if (!dma_fence_is_array(fence)) {
552 ret = i915_sw_fence_await_dma_fence(&req->submit,
553 fence, I915_FENCE_TIMEOUT,
554 GFP_KERNEL);
555 return ret < 0 ? ret : 0;
556 }
557
558 /* Note that if the fence-array was created in signal-on-any mode,
559 * we should *not* decompose it into its individual fences. However,
560 * we don't currently store which mode the fence-array is operating
561 * in. Fortunately, the only user of signal-on-any is private to
562 * amdgpu and we should not see any incoming fence-array from
563 * sync-file being in signal-on-any mode.
564 */
565
566 array = to_dma_fence_array(fence);
567 for (i = 0; i < array->num_fences; i++) {
568 struct dma_fence *child = array->fences[i];
569
570 if (dma_fence_is_i915(child))
571 ret = i915_gem_request_await_request(req,
572 to_request(child));
573 else
574 ret = i915_sw_fence_await_dma_fence(&req->submit,
575 child, I915_FENCE_TIMEOUT,
576 GFP_KERNEL);
577 if (ret < 0)
578 return ret;
579 }
580
581 return 0;
582}
583
Chris Wilsona2bc4692016-09-09 14:11:56 +0100584/**
585 * i915_gem_request_await_object - set this request to (async) wait upon a bo
586 *
587 * @to: request we are wishing to use
588 * @obj: object which may be in use on another ring.
589 *
590 * This code is meant to abstract object synchronization with the GPU.
591 * Conceptually we serialise writes between engines inside the GPU.
592 * We only allow one engine to write into a buffer at any time, but
593 * multiple readers. To ensure each has a coherent view of memory, we must:
594 *
595 * - If there is an outstanding write request to the object, the new
596 * request must wait for it to complete (either CPU or in hw, requests
597 * on the same ring will be naturally ordered).
598 *
599 * - If we are a write request (pending_write_domain is set), the new
600 * request must wait for outstanding read requests to complete.
601 *
602 * Returns 0 if successful, else propagates up the lower layer error.
603 */
604int
605i915_gem_request_await_object(struct drm_i915_gem_request *to,
606 struct drm_i915_gem_object *obj,
607 bool write)
608{
Chris Wilsond07f0e52016-10-28 13:58:44 +0100609 struct dma_fence *excl;
610 int ret = 0;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100611
612 if (write) {
Chris Wilsond07f0e52016-10-28 13:58:44 +0100613 struct dma_fence **shared;
614 unsigned int count, i;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100615
Chris Wilsond07f0e52016-10-28 13:58:44 +0100616 ret = reservation_object_get_fences_rcu(obj->resv,
617 &excl, &count, &shared);
Chris Wilsona2bc4692016-09-09 14:11:56 +0100618 if (ret)
619 return ret;
Chris Wilsond07f0e52016-10-28 13:58:44 +0100620
621 for (i = 0; i < count; i++) {
622 ret = i915_gem_request_await_dma_fence(to, shared[i]);
623 if (ret)
624 break;
625
626 dma_fence_put(shared[i]);
627 }
628
629 for (; i < count; i++)
630 dma_fence_put(shared[i]);
631 kfree(shared);
632 } else {
633 excl = reservation_object_get_excl_rcu(obj->resv);
Chris Wilsona2bc4692016-09-09 14:11:56 +0100634 }
635
Chris Wilsond07f0e52016-10-28 13:58:44 +0100636 if (excl) {
637 if (ret == 0)
638 ret = i915_gem_request_await_dma_fence(to, excl);
639
640 dma_fence_put(excl);
641 }
642
643 return ret;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100644}
645
Chris Wilson05235c52016-07-20 09:21:08 +0100646static void i915_gem_mark_busy(const struct intel_engine_cs *engine)
647{
648 struct drm_i915_private *dev_priv = engine->i915;
649
Chris Wilson05235c52016-07-20 09:21:08 +0100650 if (dev_priv->gt.awake)
651 return;
652
653 intel_runtime_pm_get_noresume(dev_priv);
654 dev_priv->gt.awake = true;
655
Chris Wilson54b4f682016-07-21 21:16:19 +0100656 intel_enable_gt_powersave(dev_priv);
Chris Wilson05235c52016-07-20 09:21:08 +0100657 i915_update_gfx_val(dev_priv);
658 if (INTEL_GEN(dev_priv) >= 6)
659 gen6_rps_busy(dev_priv);
660
661 queue_delayed_work(dev_priv->wq,
662 &dev_priv->gt.retire_work,
663 round_jiffies_up_relative(HZ));
664}
665
666/*
667 * NB: This function is not allowed to fail. Doing so would mean the the
668 * request is not being tracked for completion but the work itself is
669 * going to happen on the hardware. This would be a Bad Thing(tm).
670 */
Chris Wilson17f298cf2016-08-10 13:41:46 +0100671void __i915_add_request(struct drm_i915_gem_request *request, bool flush_caches)
Chris Wilson05235c52016-07-20 09:21:08 +0100672{
Chris Wilson95b2ab52016-08-15 10:48:46 +0100673 struct intel_engine_cs *engine = request->engine;
674 struct intel_ring *ring = request->ring;
Chris Wilson73cb9702016-10-28 13:58:46 +0100675 struct intel_timeline *timeline = request->timeline;
Chris Wilson0a046a02016-09-09 14:12:00 +0100676 struct drm_i915_gem_request *prev;
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100677 int err;
Chris Wilson05235c52016-07-20 09:21:08 +0100678
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100679 lockdep_assert_held(&request->i915->drm.struct_mutex);
Chris Wilson0f25dff2016-09-09 14:11:55 +0100680 trace_i915_gem_request_add(request);
681
Chris Wilson05235c52016-07-20 09:21:08 +0100682 /*
683 * To ensure that this call will not fail, space for its emissions
684 * should already have been reserved in the ring buffer. Let the ring
685 * know that it is time to use that space up.
686 */
Chris Wilson05235c52016-07-20 09:21:08 +0100687 request->reserved_space = 0;
688
689 /*
690 * Emit any outstanding flushes - execbuf can fail to emit the flush
691 * after having emitted the batchbuffer command. Hence we need to fix
692 * things up similar to emitting the lazy request. The difference here
693 * is that the flush _must_ happen before the next request, no matter
694 * what.
695 */
696 if (flush_caches) {
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100697 err = engine->emit_flush(request, EMIT_FLUSH);
Chris Wilsonc7fe7d22016-08-02 22:50:24 +0100698
Chris Wilson05235c52016-07-20 09:21:08 +0100699 /* Not allowed to fail! */
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100700 WARN(err, "engine->emit_flush() failed: %d!\n", err);
Chris Wilson05235c52016-07-20 09:21:08 +0100701 }
702
Chris Wilsond0454462016-08-15 10:48:40 +0100703 /* Record the position of the start of the breadcrumb so that
Chris Wilson05235c52016-07-20 09:21:08 +0100704 * should we detect the updated seqno part-way through the
705 * GPU processing the request, we never over-estimate the
Chris Wilsond0454462016-08-15 10:48:40 +0100706 * position of the ring's HEAD.
Chris Wilson05235c52016-07-20 09:21:08 +0100707 */
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100708 err = intel_ring_begin(request, engine->emit_breadcrumb_sz);
709 GEM_BUG_ON(err);
Chris Wilsonba76d912016-08-02 22:50:28 +0100710 request->postfix = ring->tail;
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100711 ring->tail += engine->emit_breadcrumb_sz * sizeof(u32);
Chris Wilson05235c52016-07-20 09:21:08 +0100712
Chris Wilson0f25dff2016-09-09 14:11:55 +0100713 /* Seal the request and mark it as pending execution. Note that
714 * we may inspect this state, without holding any locks, during
715 * hangcheck. Hence we apply the barrier to ensure that we do not
716 * see a more recent value in the hws than we are tracking.
717 */
Chris Wilson0a046a02016-09-09 14:12:00 +0100718
Chris Wilson73cb9702016-10-28 13:58:46 +0100719 prev = i915_gem_active_raw(&timeline->last_request,
Chris Wilson0a046a02016-09-09 14:12:00 +0100720 &request->i915->drm.struct_mutex);
721 if (prev)
722 i915_sw_fence_await_sw_fence(&request->submit, &prev->submit,
723 &request->submitq);
724
Chris Wilson80b204b2016-10-28 13:58:58 +0100725 spin_lock_irq(&timeline->lock);
Chris Wilsonf2d13292016-10-28 13:58:57 +0100726 list_add_tail(&request->link, &timeline->requests);
Chris Wilson80b204b2016-10-28 13:58:58 +0100727 spin_unlock_irq(&timeline->lock);
Chris Wilson28176ef2016-10-28 13:58:56 +0100728
Chris Wilson80b204b2016-10-28 13:58:58 +0100729 GEM_BUG_ON(i915_seqno_passed(timeline->last_submitted_seqno,
730 request->fence.seqno));
731
732 timeline->last_submitted_seqno = request->fence.seqno;
Chris Wilson73cb9702016-10-28 13:58:46 +0100733 i915_gem_active_set(&timeline->last_request, request);
Chris Wilsonf2d13292016-10-28 13:58:57 +0100734
Chris Wilson0f25dff2016-09-09 14:11:55 +0100735 list_add_tail(&request->ring_link, &ring->request_list);
Chris Wilsonf2d13292016-10-28 13:58:57 +0100736 request->emitted_jiffies = jiffies;
Chris Wilson0f25dff2016-09-09 14:11:55 +0100737
Chris Wilson05235c52016-07-20 09:21:08 +0100738 i915_gem_mark_busy(engine);
Chris Wilson5590af32016-09-09 14:11:54 +0100739
740 local_bh_disable();
741 i915_sw_fence_commit(&request->submit);
742 local_bh_enable(); /* Kick the execlists tasklet if just scheduled */
Chris Wilson05235c52016-07-20 09:21:08 +0100743}
744
Chris Wilson221fe792016-09-09 14:11:51 +0100745static void reset_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
746{
747 unsigned long flags;
748
749 spin_lock_irqsave(&q->lock, flags);
750 if (list_empty(&wait->task_list))
751 __add_wait_queue(q, wait);
752 spin_unlock_irqrestore(&q->lock, flags);
753}
754
Chris Wilson05235c52016-07-20 09:21:08 +0100755static unsigned long local_clock_us(unsigned int *cpu)
756{
757 unsigned long t;
758
759 /* Cheaply and approximately convert from nanoseconds to microseconds.
760 * The result and subsequent calculations are also defined in the same
761 * approximate microseconds units. The principal source of timing
762 * error here is from the simple truncation.
763 *
764 * Note that local_clock() is only defined wrt to the current CPU;
765 * the comparisons are no longer valid if we switch CPUs. Instead of
766 * blocking preemption for the entire busywait, we can detect the CPU
767 * switch and use that as indicator of system load and a reason to
768 * stop busywaiting, see busywait_stop().
769 */
770 *cpu = get_cpu();
771 t = local_clock() >> 10;
772 put_cpu();
773
774 return t;
775}
776
777static bool busywait_stop(unsigned long timeout, unsigned int cpu)
778{
779 unsigned int this_cpu;
780
781 if (time_after(local_clock_us(&this_cpu), timeout))
782 return true;
783
784 return this_cpu != cpu;
785}
786
787bool __i915_spin_request(const struct drm_i915_gem_request *req,
788 int state, unsigned long timeout_us)
789{
790 unsigned int cpu;
791
792 /* When waiting for high frequency requests, e.g. during synchronous
793 * rendering split between the CPU and GPU, the finite amount of time
794 * required to set up the irq and wait upon it limits the response
795 * rate. By busywaiting on the request completion for a short while we
796 * can service the high frequency waits as quick as possible. However,
797 * if it is a slow request, we want to sleep as quickly as possible.
798 * The tradeoff between waiting and sleeping is roughly the time it
799 * takes to sleep on a request, on the order of a microsecond.
800 */
801
802 timeout_us += local_clock_us(&cpu);
803 do {
Chris Wilson65e47602016-10-28 13:58:49 +0100804 if (__i915_gem_request_completed(req))
Chris Wilson05235c52016-07-20 09:21:08 +0100805 return true;
806
807 if (signal_pending_state(state, current))
808 break;
809
810 if (busywait_stop(timeout_us, cpu))
811 break;
812
813 cpu_relax_lowlatency();
814 } while (!need_resched());
815
816 return false;
817}
818
Chris Wilson4680816b2016-10-28 13:58:48 +0100819static long
820__i915_request_wait_for_submit(struct drm_i915_gem_request *request,
821 unsigned int flags,
822 long timeout)
823{
824 const int state = flags & I915_WAIT_INTERRUPTIBLE ?
825 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
826 wait_queue_head_t *q = &request->i915->gpu_error.wait_queue;
827 DEFINE_WAIT(reset);
828 DEFINE_WAIT(wait);
829
830 if (flags & I915_WAIT_LOCKED)
831 add_wait_queue(q, &reset);
832
833 do {
834 prepare_to_wait(&request->submit.wait, &wait, state);
835
836 if (i915_sw_fence_done(&request->submit))
837 break;
838
839 if (flags & I915_WAIT_LOCKED &&
840 i915_reset_in_progress(&request->i915->gpu_error)) {
841 __set_current_state(TASK_RUNNING);
842 i915_reset(request->i915);
843 reset_wait_queue(q, &reset);
844 continue;
845 }
846
847 if (signal_pending_state(state, current)) {
848 timeout = -ERESTARTSYS;
849 break;
850 }
851
852 timeout = io_schedule_timeout(timeout);
853 } while (timeout);
854 finish_wait(&request->submit.wait, &wait);
855
856 if (flags & I915_WAIT_LOCKED)
857 remove_wait_queue(q, &reset);
858
859 return timeout;
860}
861
Chris Wilson05235c52016-07-20 09:21:08 +0100862/**
Chris Wilson776f3232016-08-04 07:52:40 +0100863 * i915_wait_request - wait until execution of request has finished
Chris Wilsone95433c2016-10-28 13:58:27 +0100864 * @req: the request to wait upon
Chris Wilsonea746f32016-09-09 14:11:49 +0100865 * @flags: how to wait
Chris Wilsone95433c2016-10-28 13:58:27 +0100866 * @timeout: how long to wait in jiffies
Chris Wilson05235c52016-07-20 09:21:08 +0100867 *
Chris Wilsone95433c2016-10-28 13:58:27 +0100868 * i915_wait_request() waits for the request to be completed, for a
869 * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
870 * unbounded wait).
Chris Wilson05235c52016-07-20 09:21:08 +0100871 *
Chris Wilsone95433c2016-10-28 13:58:27 +0100872 * If the caller holds the struct_mutex, the caller must pass I915_WAIT_LOCKED
873 * in via the flags, and vice versa if the struct_mutex is not held, the caller
874 * must not specify that the wait is locked.
875 *
876 * Returns the remaining time (in jiffies) if the request completed, which may
877 * be zero or -ETIME if the request is unfinished after the timeout expires.
878 * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
879 * pending before the request completes.
Chris Wilson05235c52016-07-20 09:21:08 +0100880 */
Chris Wilsone95433c2016-10-28 13:58:27 +0100881long i915_wait_request(struct drm_i915_gem_request *req,
882 unsigned int flags,
883 long timeout)
Chris Wilson05235c52016-07-20 09:21:08 +0100884{
Chris Wilsonea746f32016-09-09 14:11:49 +0100885 const int state = flags & I915_WAIT_INTERRUPTIBLE ?
886 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
Chris Wilson05235c52016-07-20 09:21:08 +0100887 DEFINE_WAIT(reset);
888 struct intel_wait wait;
Chris Wilson05235c52016-07-20 09:21:08 +0100889
890 might_sleep();
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100891#if IS_ENABLED(CONFIG_LOCKDEP)
Chris Wilsone95433c2016-10-28 13:58:27 +0100892 GEM_BUG_ON(debug_locks &&
893 !!lockdep_is_held(&req->i915->drm.struct_mutex) !=
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100894 !!(flags & I915_WAIT_LOCKED));
895#endif
Chris Wilsone95433c2016-10-28 13:58:27 +0100896 GEM_BUG_ON(timeout < 0);
Chris Wilson05235c52016-07-20 09:21:08 +0100897
Chris Wilson05235c52016-07-20 09:21:08 +0100898 if (i915_gem_request_completed(req))
Chris Wilsone95433c2016-10-28 13:58:27 +0100899 return timeout;
Chris Wilson05235c52016-07-20 09:21:08 +0100900
Chris Wilsone95433c2016-10-28 13:58:27 +0100901 if (!timeout)
902 return -ETIME;
Chris Wilson05235c52016-07-20 09:21:08 +0100903
904 trace_i915_gem_request_wait_begin(req);
905
Chris Wilson4680816b2016-10-28 13:58:48 +0100906 if (!i915_sw_fence_done(&req->submit)) {
907 timeout = __i915_request_wait_for_submit(req, flags, timeout);
908 if (timeout < 0)
909 goto complete;
910
911 GEM_BUG_ON(!i915_sw_fence_done(&req->submit));
912 }
Chris Wilson65e47602016-10-28 13:58:49 +0100913 GEM_BUG_ON(!req->global_seqno);
Chris Wilson4680816b2016-10-28 13:58:48 +0100914
Daniel Vetter437c3082016-08-05 18:11:24 +0200915 /* Optimistic short spin before touching IRQs */
Chris Wilson05235c52016-07-20 09:21:08 +0100916 if (i915_spin_request(req, state, 5))
917 goto complete;
918
919 set_current_state(state);
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100920 if (flags & I915_WAIT_LOCKED)
921 add_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
Chris Wilson05235c52016-07-20 09:21:08 +0100922
Chris Wilson65e47602016-10-28 13:58:49 +0100923 intel_wait_init(&wait, req->global_seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100924 if (intel_engine_add_wait(req->engine, &wait))
925 /* In order to check that we haven't missed the interrupt
926 * as we enabled it, we need to kick ourselves to do a
927 * coherent check on the seqno before we sleep.
928 */
929 goto wakeup;
930
931 for (;;) {
932 if (signal_pending_state(state, current)) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100933 timeout = -ERESTARTSYS;
Chris Wilson05235c52016-07-20 09:21:08 +0100934 break;
935 }
936
Chris Wilsone95433c2016-10-28 13:58:27 +0100937 if (!timeout) {
938 timeout = -ETIME;
Chris Wilson05235c52016-07-20 09:21:08 +0100939 break;
940 }
941
Chris Wilsone95433c2016-10-28 13:58:27 +0100942 timeout = io_schedule_timeout(timeout);
943
Chris Wilson05235c52016-07-20 09:21:08 +0100944 if (intel_wait_complete(&wait))
945 break;
946
947 set_current_state(state);
948
949wakeup:
950 /* Carefully check if the request is complete, giving time
951 * for the seqno to be visible following the interrupt.
952 * We also have to check in case we are kicked by the GPU
953 * reset in order to drop the struct_mutex.
954 */
955 if (__i915_request_irq_complete(req))
956 break;
957
Chris Wilson221fe792016-09-09 14:11:51 +0100958 /* If the GPU is hung, and we hold the lock, reset the GPU
959 * and then check for completion. On a full reset, the engine's
960 * HW seqno will be advanced passed us and we are complete.
961 * If we do a partial reset, we have to wait for the GPU to
962 * resume and update the breadcrumb.
963 *
964 * If we don't hold the mutex, we can just wait for the worker
965 * to come along and update the breadcrumb (either directly
966 * itself, or indirectly by recovering the GPU).
967 */
968 if (flags & I915_WAIT_LOCKED &&
969 i915_reset_in_progress(&req->i915->gpu_error)) {
970 __set_current_state(TASK_RUNNING);
971 i915_reset(req->i915);
972 reset_wait_queue(&req->i915->gpu_error.wait_queue,
973 &reset);
974 continue;
975 }
976
Chris Wilson05235c52016-07-20 09:21:08 +0100977 /* Only spin if we know the GPU is processing this request */
978 if (i915_spin_request(req, state, 2))
979 break;
980 }
Chris Wilson05235c52016-07-20 09:21:08 +0100981
982 intel_engine_remove_wait(req->engine, &wait);
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100983 if (flags & I915_WAIT_LOCKED)
984 remove_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
Chris Wilson05235c52016-07-20 09:21:08 +0100985 __set_current_state(TASK_RUNNING);
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100986
Chris Wilson05235c52016-07-20 09:21:08 +0100987complete:
988 trace_i915_gem_request_wait_end(req);
989
Chris Wilsone95433c2016-10-28 13:58:27 +0100990 return timeout;
Chris Wilson05235c52016-07-20 09:21:08 +0100991}
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100992
Chris Wilson28176ef2016-10-28 13:58:56 +0100993static void engine_retire_requests(struct intel_engine_cs *engine)
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100994{
995 struct drm_i915_gem_request *request, *next;
996
Chris Wilson73cb9702016-10-28 13:58:46 +0100997 list_for_each_entry_safe(request, next,
998 &engine->timeline->requests, link) {
Chris Wilson80b204b2016-10-28 13:58:58 +0100999 if (!__i915_gem_request_completed(request))
Chris Wilson28176ef2016-10-28 13:58:56 +01001000 return;
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001001
1002 i915_gem_request_retire(request);
1003 }
1004}
1005
1006void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
1007{
1008 struct intel_engine_cs *engine;
Chris Wilson28176ef2016-10-28 13:58:56 +01001009 enum intel_engine_id id;
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001010
1011 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1012
Chris Wilson28176ef2016-10-28 13:58:56 +01001013 if (!dev_priv->gt.active_requests)
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001014 return;
1015
1016 GEM_BUG_ON(!dev_priv->gt.awake);
1017
Chris Wilson28176ef2016-10-28 13:58:56 +01001018 for_each_engine(engine, dev_priv, id)
1019 engine_retire_requests(engine);
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001020
Chris Wilson28176ef2016-10-28 13:58:56 +01001021 if (!dev_priv->gt.active_requests)
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001022 queue_delayed_work(dev_priv->wq,
1023 &dev_priv->gt.idle_work,
1024 msecs_to_jiffies(100));
1025}