blob: 78c87d94d20556562b97a42918ce4062c3f441c2 [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 Wilson52e54202016-11-14 20:41:02 +0000116static struct i915_dependency *
117i915_dependency_alloc(struct drm_i915_private *i915)
118{
119 return kmem_cache_alloc(i915->dependencies, GFP_KERNEL);
120}
121
122static void
123i915_dependency_free(struct drm_i915_private *i915,
124 struct i915_dependency *dep)
125{
126 kmem_cache_free(i915->dependencies, dep);
127}
128
129static void
130__i915_priotree_add_dependency(struct i915_priotree *pt,
131 struct i915_priotree *signal,
132 struct i915_dependency *dep,
133 unsigned long flags)
134{
135 list_add(&dep->wait_link, &signal->waiters_list);
136 list_add(&dep->signal_link, &pt->signalers_list);
137 dep->signaler = signal;
138 dep->flags = flags;
139}
140
141static int
142i915_priotree_add_dependency(struct drm_i915_private *i915,
143 struct i915_priotree *pt,
144 struct i915_priotree *signal)
145{
146 struct i915_dependency *dep;
147
148 dep = i915_dependency_alloc(i915);
149 if (!dep)
150 return -ENOMEM;
151
152 __i915_priotree_add_dependency(pt, signal, dep, I915_DEPENDENCY_ALLOC);
153 return 0;
154}
155
156static void
157i915_priotree_fini(struct drm_i915_private *i915, struct i915_priotree *pt)
158{
159 struct i915_dependency *dep, *next;
160
161 /* Everyone we depended upon (the fences we wait to be signaled)
162 * should retire before us and remove themselves from our list.
163 * However, retirement is run independently on each timeline and
164 * so we may be called out-of-order.
165 */
166 list_for_each_entry_safe(dep, next, &pt->signalers_list, signal_link) {
167 list_del(&dep->wait_link);
168 if (dep->flags & I915_DEPENDENCY_ALLOC)
169 i915_dependency_free(i915, dep);
170 }
171
172 /* Remove ourselves from everyone who depends upon us */
173 list_for_each_entry_safe(dep, next, &pt->waiters_list, wait_link) {
174 list_del(&dep->signal_link);
175 if (dep->flags & I915_DEPENDENCY_ALLOC)
176 i915_dependency_free(i915, dep);
177 }
178}
179
180static void
181i915_priotree_init(struct i915_priotree *pt)
182{
183 INIT_LIST_HEAD(&pt->signalers_list);
184 INIT_LIST_HEAD(&pt->waiters_list);
185}
186
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100187void i915_gem_retire_noop(struct i915_gem_active *active,
188 struct drm_i915_gem_request *request)
189{
190 /* Space left intentionally blank */
191}
192
Chris Wilson05235c52016-07-20 09:21:08 +0100193static void i915_gem_request_retire(struct drm_i915_gem_request *request)
194{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100195 struct i915_gem_active *active, *next;
196
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100197 lockdep_assert_held(&request->i915->drm.struct_mutex);
198 GEM_BUG_ON(!i915_gem_request_completed(request));
199
Chris Wilson05235c52016-07-20 09:21:08 +0100200 trace_i915_gem_request_retire(request);
Chris Wilson80b204b2016-10-28 13:58:58 +0100201
202 spin_lock_irq(&request->engine->timeline->lock);
Chris Wilsone95433c2016-10-28 13:58:27 +0100203 list_del_init(&request->link);
Chris Wilson80b204b2016-10-28 13:58:58 +0100204 spin_unlock_irq(&request->engine->timeline->lock);
Chris Wilson05235c52016-07-20 09:21:08 +0100205
206 /* We know the GPU must have read the request to have
207 * sent us the seqno + interrupt, so use the position
208 * of tail of the request to update the last known position
209 * of the GPU head.
210 *
211 * Note this requires that we are always called in request
212 * completion order.
213 */
Chris Wilson675d9ad2016-08-04 07:52:36 +0100214 list_del(&request->ring_link);
Chris Wilson1dae2df2016-08-02 22:50:19 +0100215 request->ring->last_retired_head = request->postfix;
Chris Wilson28176ef2016-10-28 13:58:56 +0100216 request->i915->gt.active_requests--;
Chris Wilson05235c52016-07-20 09:21:08 +0100217
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100218 /* Walk through the active list, calling retire on each. This allows
219 * objects to track their GPU activity and mark themselves as idle
220 * when their *last* active request is completed (updating state
221 * tracking lists for eviction, active references for GEM, etc).
222 *
223 * As the ->retire() may free the node, we decouple it first and
224 * pass along the auxiliary information (to avoid dereferencing
225 * the node after the callback).
226 */
227 list_for_each_entry_safe(active, next, &request->active_list, link) {
228 /* In microbenchmarks or focusing upon time inside the kernel,
229 * we may spend an inordinate amount of time simply handling
230 * the retirement of requests and processing their callbacks.
231 * Of which, this loop itself is particularly hot due to the
232 * cache misses when jumping around the list of i915_gem_active.
233 * So we try to keep this loop as streamlined as possible and
234 * also prefetch the next i915_gem_active to try and hide
235 * the likely cache miss.
236 */
237 prefetchw(next);
238
239 INIT_LIST_HEAD(&active->link);
Chris Wilson0eafec62016-08-04 16:32:41 +0100240 RCU_INIT_POINTER(active->request, NULL);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100241
242 active->retire(active, request);
243 }
244
Chris Wilson05235c52016-07-20 09:21:08 +0100245 i915_gem_request_remove_from_client(request);
246
247 if (request->previous_context) {
248 if (i915.enable_execlists)
249 intel_lr_context_unpin(request->previous_context,
250 request->engine);
251 }
252
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100253 i915_gem_context_put(request->ctx);
Chris Wilsond07f0e52016-10-28 13:58:44 +0100254
255 dma_fence_signal(&request->fence);
Chris Wilson52e54202016-11-14 20:41:02 +0000256
257 i915_priotree_fini(request->i915, &request->priotree);
Chris Wilsone8a261e2016-07-20 13:31:49 +0100258 i915_gem_request_put(request);
Chris Wilson05235c52016-07-20 09:21:08 +0100259}
260
261void i915_gem_request_retire_upto(struct drm_i915_gem_request *req)
262{
263 struct intel_engine_cs *engine = req->engine;
264 struct drm_i915_gem_request *tmp;
265
266 lockdep_assert_held(&req->i915->drm.struct_mutex);
Chris Wilsone95433c2016-10-28 13:58:27 +0100267 if (list_empty(&req->link))
268 return;
Chris Wilson05235c52016-07-20 09:21:08 +0100269
270 do {
Chris Wilson73cb9702016-10-28 13:58:46 +0100271 tmp = list_first_entry(&engine->timeline->requests,
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100272 typeof(*tmp), link);
Chris Wilson05235c52016-07-20 09:21:08 +0100273
274 i915_gem_request_retire(tmp);
275 } while (tmp != req);
Chris Wilson05235c52016-07-20 09:21:08 +0100276}
277
Chris Wilson8af29b02016-09-09 14:11:47 +0100278static int i915_gem_check_wedge(struct drm_i915_private *dev_priv)
Chris Wilson05235c52016-07-20 09:21:08 +0100279{
Chris Wilson8af29b02016-09-09 14:11:47 +0100280 struct i915_gpu_error *error = &dev_priv->gpu_error;
281
282 if (i915_terminally_wedged(error))
Chris Wilson05235c52016-07-20 09:21:08 +0100283 return -EIO;
284
Chris Wilson8af29b02016-09-09 14:11:47 +0100285 if (i915_reset_in_progress(error)) {
Chris Wilson05235c52016-07-20 09:21:08 +0100286 /* Non-interruptible callers can't handle -EAGAIN, hence return
287 * -EIO unconditionally for these.
288 */
Chris Wilson8af29b02016-09-09 14:11:47 +0100289 if (!dev_priv->mm.interruptible)
Chris Wilson05235c52016-07-20 09:21:08 +0100290 return -EIO;
291
292 return -EAGAIN;
293 }
294
295 return 0;
296}
297
Chris Wilson85e17f52016-10-28 13:58:53 +0100298static int i915_gem_init_global_seqno(struct drm_i915_private *i915, u32 seqno)
Chris Wilson05235c52016-07-20 09:21:08 +0100299{
Chris Wilson85e17f52016-10-28 13:58:53 +0100300 struct i915_gem_timeline *timeline = &i915->gt.global_timeline;
Chris Wilson05235c52016-07-20 09:21:08 +0100301 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +0530302 enum intel_engine_id id;
Chris Wilson05235c52016-07-20 09:21:08 +0100303 int ret;
304
305 /* Carefully retire all requests without writing to the rings */
Chris Wilson85e17f52016-10-28 13:58:53 +0100306 ret = i915_gem_wait_for_idle(i915,
Chris Wilson73cb9702016-10-28 13:58:46 +0100307 I915_WAIT_INTERRUPTIBLE |
308 I915_WAIT_LOCKED);
309 if (ret)
310 return ret;
311
Chris Wilson85e17f52016-10-28 13:58:53 +0100312 i915_gem_retire_requests(i915);
Chris Wilson28176ef2016-10-28 13:58:56 +0100313 GEM_BUG_ON(i915->gt.active_requests > 1);
Chris Wilson05235c52016-07-20 09:21:08 +0100314
315 /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
Chris Wilson28176ef2016-10-28 13:58:56 +0100316 if (!i915_seqno_passed(seqno, atomic_read(&timeline->next_seqno))) {
Chris Wilson6a5d1db2016-11-08 14:37:19 +0000317 while (intel_breadcrumbs_busy(i915))
318 cond_resched(); /* spin until threads are complete */
Chris Wilson05235c52016-07-20 09:21:08 +0100319 }
Chris Wilson28176ef2016-10-28 13:58:56 +0100320 atomic_set(&timeline->next_seqno, seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100321
322 /* Finally reset hw state */
Chris Wilson85e17f52016-10-28 13:58:53 +0100323 for_each_engine(engine, i915, id)
Chris Wilson73cb9702016-10-28 13:58:46 +0100324 intel_engine_init_global_seqno(engine, seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100325
Chris Wilson85e17f52016-10-28 13:58:53 +0100326 list_for_each_entry(timeline, &i915->gt.timelines, link) {
327 for_each_engine(engine, i915, id) {
328 struct intel_timeline *tl = &timeline->engine[id];
329
330 memset(tl->sync_seqno, 0, sizeof(tl->sync_seqno));
331 }
332 }
333
Chris Wilson05235c52016-07-20 09:21:08 +0100334 return 0;
335}
336
Chris Wilson73cb9702016-10-28 13:58:46 +0100337int i915_gem_set_global_seqno(struct drm_device *dev, u32 seqno)
Chris Wilson05235c52016-07-20 09:21:08 +0100338{
339 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson05235c52016-07-20 09:21:08 +0100340
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100341 lockdep_assert_held(&dev_priv->drm.struct_mutex);
342
Chris Wilson05235c52016-07-20 09:21:08 +0100343 if (seqno == 0)
344 return -EINVAL;
345
346 /* HWS page needs to be set less than what we
347 * will inject to ring
348 */
Chris Wilson28176ef2016-10-28 13:58:56 +0100349 return i915_gem_init_global_seqno(dev_priv, seqno - 1);
350}
Chris Wilson05235c52016-07-20 09:21:08 +0100351
Chris Wilson28176ef2016-10-28 13:58:56 +0100352static int reserve_global_seqno(struct drm_i915_private *i915)
353{
354 u32 active_requests = ++i915->gt.active_requests;
355 u32 next_seqno = atomic_read(&i915->gt.global_timeline.next_seqno);
356 int ret;
357
358 /* Reservation is fine until we need to wrap around */
359 if (likely(next_seqno + active_requests > next_seqno))
360 return 0;
361
362 ret = i915_gem_init_global_seqno(i915, 0);
363 if (ret) {
364 i915->gt.active_requests--;
365 return ret;
366 }
367
Chris Wilson05235c52016-07-20 09:21:08 +0100368 return 0;
369}
370
Chris Wilson80b204b2016-10-28 13:58:58 +0100371static u32 __timeline_get_seqno(struct i915_gem_timeline *tl)
372{
373 /* next_seqno only incremented under a mutex */
374 return ++tl->next_seqno.counter;
375}
376
Chris Wilson28176ef2016-10-28 13:58:56 +0100377static u32 timeline_get_seqno(struct i915_gem_timeline *tl)
Chris Wilson05235c52016-07-20 09:21:08 +0100378{
Chris Wilson28176ef2016-10-28 13:58:56 +0100379 return atomic_inc_return(&tl->next_seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100380}
381
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000382void __i915_gem_request_submit(struct drm_i915_gem_request *request)
Chris Wilson5590af32016-09-09 14:11:54 +0100383{
Chris Wilson73cb9702016-10-28 13:58:46 +0100384 struct intel_engine_cs *engine = request->engine;
Chris Wilsonf2d13292016-10-28 13:58:57 +0100385 struct intel_timeline *timeline;
386 u32 seqno;
Chris Wilson5590af32016-09-09 14:11:54 +0100387
Chris Wilson80b204b2016-10-28 13:58:58 +0100388 /* Transfer from per-context onto the global per-engine timeline */
389 timeline = engine->timeline;
390 GEM_BUG_ON(timeline == request->timeline);
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000391 assert_spin_locked(&timeline->lock);
Chris Wilson5590af32016-09-09 14:11:54 +0100392
Chris Wilson80b204b2016-10-28 13:58:58 +0100393 seqno = timeline_get_seqno(timeline->common);
Chris Wilsonf2d13292016-10-28 13:58:57 +0100394 GEM_BUG_ON(!seqno);
395 GEM_BUG_ON(i915_seqno_passed(intel_engine_get_seqno(engine), seqno));
396
397 GEM_BUG_ON(i915_seqno_passed(timeline->last_submitted_seqno, seqno));
398 request->previous_seqno = timeline->last_submitted_seqno;
399 timeline->last_submitted_seqno = seqno;
400
401 /* We may be recursing from the signal callback of another i915 fence */
402 spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
403 request->global_seqno = seqno;
404 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
405 intel_engine_enable_signaling(request);
406 spin_unlock(&request->lock);
407
408 GEM_BUG_ON(!request->global_seqno);
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100409 engine->emit_breadcrumb(request,
410 request->ring->vaddr + request->postfix);
Chris Wilson5590af32016-09-09 14:11:54 +0100411
Chris Wilsonbb894852016-11-14 20:40:57 +0000412 spin_lock(&request->timeline->lock);
Chris Wilson80b204b2016-10-28 13:58:58 +0100413 list_move_tail(&request->link, &timeline->requests);
414 spin_unlock(&request->timeline->lock);
415
Chris Wilson23902e42016-11-14 20:40:58 +0000416 i915_sw_fence_commit(&request->execute);
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000417}
Chris Wilson23902e42016-11-14 20:40:58 +0000418
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000419void i915_gem_request_submit(struct drm_i915_gem_request *request)
420{
421 struct intel_engine_cs *engine = request->engine;
422 unsigned long flags;
423
424 /* Will be called from irq-context when using foreign fences. */
425 spin_lock_irqsave(&engine->timeline->lock, flags);
426
427 __i915_gem_request_submit(request);
428
429 spin_unlock_irqrestore(&engine->timeline->lock, flags);
430}
431
432static int __i915_sw_fence_call
433submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
434{
435 if (state == FENCE_COMPLETE) {
436 struct drm_i915_gem_request *request =
437 container_of(fence, typeof(*request), submit);
438
439 request->engine->submit_request(request);
440 }
Chris Wilson80b204b2016-10-28 13:58:58 +0100441
Chris Wilson5590af32016-09-09 14:11:54 +0100442 return NOTIFY_DONE;
443}
444
Chris Wilson23902e42016-11-14 20:40:58 +0000445static int __i915_sw_fence_call
446execute_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
447{
448 return NOTIFY_DONE;
449}
450
Chris Wilson8e637172016-08-02 22:50:26 +0100451/**
452 * i915_gem_request_alloc - allocate a request structure
453 *
454 * @engine: engine that we wish to issue the request on.
455 * @ctx: context that the request will be associated with.
456 * This can be NULL if the request is not directly related to
457 * any specific user context, in which case this function will
458 * choose an appropriate context to use.
459 *
460 * Returns a pointer to the allocated request if successful,
461 * or an error code if not.
462 */
463struct drm_i915_gem_request *
464i915_gem_request_alloc(struct intel_engine_cs *engine,
465 struct i915_gem_context *ctx)
Chris Wilson05235c52016-07-20 09:21:08 +0100466{
467 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson05235c52016-07-20 09:21:08 +0100468 struct drm_i915_gem_request *req;
469 int ret;
470
Chris Wilson28176ef2016-10-28 13:58:56 +0100471 lockdep_assert_held(&dev_priv->drm.struct_mutex);
472
Chris Wilson05235c52016-07-20 09:21:08 +0100473 /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
474 * EIO if the GPU is already wedged, or EAGAIN to drop the struct_mutex
475 * and restart.
476 */
Chris Wilson8af29b02016-09-09 14:11:47 +0100477 ret = i915_gem_check_wedge(dev_priv);
Chris Wilson05235c52016-07-20 09:21:08 +0100478 if (ret)
Chris Wilson8e637172016-08-02 22:50:26 +0100479 return ERR_PTR(ret);
Chris Wilson05235c52016-07-20 09:21:08 +0100480
Chris Wilson28176ef2016-10-28 13:58:56 +0100481 ret = reserve_global_seqno(dev_priv);
482 if (ret)
483 return ERR_PTR(ret);
484
Chris Wilson9b5f4e52016-07-20 09:21:09 +0100485 /* Move the oldest request to the slab-cache (if not in use!) */
Chris Wilson73cb9702016-10-28 13:58:46 +0100486 req = list_first_entry_or_null(&engine->timeline->requests,
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100487 typeof(*req), link);
Chris Wilson80b204b2016-10-28 13:58:58 +0100488 if (req && __i915_gem_request_completed(req))
Chris Wilson2a1d7752016-07-26 12:01:51 +0100489 i915_gem_request_retire(req);
Chris Wilson9b5f4e52016-07-20 09:21:09 +0100490
Chris Wilson5a198b82016-08-09 09:23:34 +0100491 /* Beware: Dragons be flying overhead.
492 *
493 * We use RCU to look up requests in flight. The lookups may
494 * race with the request being allocated from the slab freelist.
495 * That is the request we are writing to here, may be in the process
Chris Wilson1426f712016-08-09 17:03:22 +0100496 * of being read by __i915_gem_active_get_rcu(). As such,
Chris Wilson5a198b82016-08-09 09:23:34 +0100497 * we have to be very careful when overwriting the contents. During
498 * the RCU lookup, we change chase the request->engine pointer,
Chris Wilson65e47602016-10-28 13:58:49 +0100499 * read the request->global_seqno and increment the reference count.
Chris Wilson5a198b82016-08-09 09:23:34 +0100500 *
501 * The reference count is incremented atomically. If it is zero,
502 * the lookup knows the request is unallocated and complete. Otherwise,
503 * it is either still in use, or has been reallocated and reset
Chris Wilsonf54d1862016-10-25 13:00:45 +0100504 * with dma_fence_init(). This increment is safe for release as we
505 * check that the request we have a reference to and matches the active
Chris Wilson5a198b82016-08-09 09:23:34 +0100506 * request.
507 *
508 * Before we increment the refcount, we chase the request->engine
509 * pointer. We must not call kmem_cache_zalloc() or else we set
510 * that pointer to NULL and cause a crash during the lookup. If
511 * we see the request is completed (based on the value of the
512 * old engine and seqno), the lookup is complete and reports NULL.
513 * If we decide the request is not completed (new engine or seqno),
514 * then we grab a reference and double check that it is still the
515 * active request - which it won't be and restart the lookup.
516 *
517 * Do not use kmem_cache_zalloc() here!
518 */
519 req = kmem_cache_alloc(dev_priv->requests, GFP_KERNEL);
Chris Wilson28176ef2016-10-28 13:58:56 +0100520 if (!req) {
521 ret = -ENOMEM;
522 goto err_unreserve;
523 }
Chris Wilson05235c52016-07-20 09:21:08 +0100524
Chris Wilson80b204b2016-10-28 13:58:58 +0100525 req->timeline = i915_gem_context_lookup_timeline(ctx, engine);
526 GEM_BUG_ON(req->timeline == engine->timeline);
Chris Wilson73cb9702016-10-28 13:58:46 +0100527
Chris Wilson04769652016-07-20 09:21:11 +0100528 spin_lock_init(&req->lock);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100529 dma_fence_init(&req->fence,
530 &i915_fence_ops,
531 &req->lock,
Chris Wilson73cb9702016-10-28 13:58:46 +0100532 req->timeline->fence_context,
Chris Wilson80b204b2016-10-28 13:58:58 +0100533 __timeline_get_seqno(req->timeline->common));
Chris Wilson04769652016-07-20 09:21:11 +0100534
Chris Wilson5590af32016-09-09 14:11:54 +0100535 i915_sw_fence_init(&req->submit, submit_notify);
Chris Wilson23902e42016-11-14 20:40:58 +0000536 i915_sw_fence_init(&req->execute, execute_notify);
537 /* Ensure that the execute fence completes after the submit fence -
538 * as we complete the execute fence from within the submit fence
539 * callback, its completion would otherwise be visible first.
540 */
541 i915_sw_fence_await_sw_fence(&req->execute, &req->submit, &req->execq);
Chris Wilson5590af32016-09-09 14:11:54 +0100542
Chris Wilson52e54202016-11-14 20:41:02 +0000543 i915_priotree_init(&req->priotree);
544
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100545 INIT_LIST_HEAD(&req->active_list);
Chris Wilson05235c52016-07-20 09:21:08 +0100546 req->i915 = dev_priv;
547 req->engine = engine;
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100548 req->ctx = i915_gem_context_get(ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100549
Chris Wilson5a198b82016-08-09 09:23:34 +0100550 /* No zalloc, must clear what we need by hand */
Chris Wilsonf2d13292016-10-28 13:58:57 +0100551 req->global_seqno = 0;
Chris Wilson5a198b82016-08-09 09:23:34 +0100552 req->previous_context = NULL;
553 req->file_priv = NULL;
Chris Wilson058d88c2016-08-15 10:49:06 +0100554 req->batch = NULL;
Chris Wilson5a198b82016-08-09 09:23:34 +0100555
Chris Wilson05235c52016-07-20 09:21:08 +0100556 /*
557 * Reserve space in the ring buffer for all the commands required to
558 * eventually emit this request. This is to guarantee that the
559 * i915_add_request() call can't fail. Note that the reserve may need
560 * to be redone if the request is not actually submitted straight
561 * away, e.g. because a GPU scheduler has deferred it.
562 */
563 req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
Chris Wilson98f29e82016-10-28 13:58:51 +0100564 GEM_BUG_ON(req->reserved_space < engine->emit_breadcrumb_sz);
Chris Wilson05235c52016-07-20 09:21:08 +0100565
566 if (i915.enable_execlists)
567 ret = intel_logical_ring_alloc_request_extras(req);
568 else
569 ret = intel_ring_alloc_request_extras(req);
570 if (ret)
571 goto err_ctx;
572
Chris Wilsond0454462016-08-15 10:48:40 +0100573 /* Record the position of the start of the request so that
574 * should we detect the updated seqno part-way through the
575 * GPU processing the request, we never over-estimate the
576 * position of the head.
577 */
578 req->head = req->ring->tail;
579
Chris Wilson8e637172016-08-02 22:50:26 +0100580 return req;
Chris Wilson05235c52016-07-20 09:21:08 +0100581
582err_ctx:
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100583 i915_gem_context_put(ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100584 kmem_cache_free(dev_priv->requests, req);
Chris Wilson28176ef2016-10-28 13:58:56 +0100585err_unreserve:
586 dev_priv->gt.active_requests--;
Chris Wilson8e637172016-08-02 22:50:26 +0100587 return ERR_PTR(ret);
Chris Wilson05235c52016-07-20 09:21:08 +0100588}
589
Chris Wilsona2bc4692016-09-09 14:11:56 +0100590static int
591i915_gem_request_await_request(struct drm_i915_gem_request *to,
592 struct drm_i915_gem_request *from)
593{
Chris Wilson85e17f52016-10-28 13:58:53 +0100594 int ret;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100595
596 GEM_BUG_ON(to == from);
597
Chris Wilson52e54202016-11-14 20:41:02 +0000598 if (to->engine->schedule) {
599 ret = i915_priotree_add_dependency(to->i915,
600 &to->priotree,
601 &from->priotree);
602 if (ret < 0)
603 return ret;
604 }
605
Chris Wilson73cb9702016-10-28 13:58:46 +0100606 if (to->timeline == from->timeline)
Chris Wilsona2bc4692016-09-09 14:11:56 +0100607 return 0;
608
Chris Wilson73cb9702016-10-28 13:58:46 +0100609 if (to->engine == from->engine) {
610 ret = i915_sw_fence_await_sw_fence_gfp(&to->submit,
611 &from->submit,
612 GFP_KERNEL);
613 return ret < 0 ? ret : 0;
614 }
615
Chris Wilson65e47602016-10-28 13:58:49 +0100616 if (!from->global_seqno) {
617 ret = i915_sw_fence_await_dma_fence(&to->submit,
618 &from->fence, 0,
619 GFP_KERNEL);
620 return ret < 0 ? ret : 0;
621 }
622
Chris Wilson85e17f52016-10-28 13:58:53 +0100623 if (from->global_seqno <= to->timeline->sync_seqno[from->engine->id])
Chris Wilsona2bc4692016-09-09 14:11:56 +0100624 return 0;
625
626 trace_i915_gem_ring_sync_to(to, from);
627 if (!i915.semaphores) {
Chris Wilson0a046a02016-09-09 14:12:00 +0100628 if (!i915_spin_request(from, TASK_INTERRUPTIBLE, 2)) {
629 ret = i915_sw_fence_await_dma_fence(&to->submit,
630 &from->fence, 0,
631 GFP_KERNEL);
632 if (ret < 0)
633 return ret;
634 }
Chris Wilsona2bc4692016-09-09 14:11:56 +0100635 } else {
636 ret = to->engine->semaphore.sync_to(to, from);
637 if (ret)
638 return ret;
639 }
640
Chris Wilson85e17f52016-10-28 13:58:53 +0100641 to->timeline->sync_seqno[from->engine->id] = from->global_seqno;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100642 return 0;
643}
644
Chris Wilsonb52992c2016-10-28 13:58:24 +0100645int
646i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
647 struct dma_fence *fence)
648{
649 struct dma_fence_array *array;
650 int ret;
651 int i;
652
653 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
654 return 0;
655
656 if (dma_fence_is_i915(fence))
657 return i915_gem_request_await_request(req, to_request(fence));
658
659 if (!dma_fence_is_array(fence)) {
660 ret = i915_sw_fence_await_dma_fence(&req->submit,
661 fence, I915_FENCE_TIMEOUT,
662 GFP_KERNEL);
663 return ret < 0 ? ret : 0;
664 }
665
666 /* Note that if the fence-array was created in signal-on-any mode,
667 * we should *not* decompose it into its individual fences. However,
668 * we don't currently store which mode the fence-array is operating
669 * in. Fortunately, the only user of signal-on-any is private to
670 * amdgpu and we should not see any incoming fence-array from
671 * sync-file being in signal-on-any mode.
672 */
673
674 array = to_dma_fence_array(fence);
675 for (i = 0; i < array->num_fences; i++) {
676 struct dma_fence *child = array->fences[i];
677
678 if (dma_fence_is_i915(child))
679 ret = i915_gem_request_await_request(req,
680 to_request(child));
681 else
682 ret = i915_sw_fence_await_dma_fence(&req->submit,
683 child, I915_FENCE_TIMEOUT,
684 GFP_KERNEL);
685 if (ret < 0)
686 return ret;
687 }
688
689 return 0;
690}
691
Chris Wilsona2bc4692016-09-09 14:11:56 +0100692/**
693 * i915_gem_request_await_object - set this request to (async) wait upon a bo
694 *
695 * @to: request we are wishing to use
696 * @obj: object which may be in use on another ring.
697 *
698 * This code is meant to abstract object synchronization with the GPU.
699 * Conceptually we serialise writes between engines inside the GPU.
700 * We only allow one engine to write into a buffer at any time, but
701 * multiple readers. To ensure each has a coherent view of memory, we must:
702 *
703 * - If there is an outstanding write request to the object, the new
704 * request must wait for it to complete (either CPU or in hw, requests
705 * on the same ring will be naturally ordered).
706 *
707 * - If we are a write request (pending_write_domain is set), the new
708 * request must wait for outstanding read requests to complete.
709 *
710 * Returns 0 if successful, else propagates up the lower layer error.
711 */
712int
713i915_gem_request_await_object(struct drm_i915_gem_request *to,
714 struct drm_i915_gem_object *obj,
715 bool write)
716{
Chris Wilsond07f0e52016-10-28 13:58:44 +0100717 struct dma_fence *excl;
718 int ret = 0;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100719
720 if (write) {
Chris Wilsond07f0e52016-10-28 13:58:44 +0100721 struct dma_fence **shared;
722 unsigned int count, i;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100723
Chris Wilsond07f0e52016-10-28 13:58:44 +0100724 ret = reservation_object_get_fences_rcu(obj->resv,
725 &excl, &count, &shared);
Chris Wilsona2bc4692016-09-09 14:11:56 +0100726 if (ret)
727 return ret;
Chris Wilsond07f0e52016-10-28 13:58:44 +0100728
729 for (i = 0; i < count; i++) {
730 ret = i915_gem_request_await_dma_fence(to, shared[i]);
731 if (ret)
732 break;
733
734 dma_fence_put(shared[i]);
735 }
736
737 for (; i < count; i++)
738 dma_fence_put(shared[i]);
739 kfree(shared);
740 } else {
741 excl = reservation_object_get_excl_rcu(obj->resv);
Chris Wilsona2bc4692016-09-09 14:11:56 +0100742 }
743
Chris Wilsond07f0e52016-10-28 13:58:44 +0100744 if (excl) {
745 if (ret == 0)
746 ret = i915_gem_request_await_dma_fence(to, excl);
747
748 dma_fence_put(excl);
749 }
750
751 return ret;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100752}
753
Chris Wilson05235c52016-07-20 09:21:08 +0100754static void i915_gem_mark_busy(const struct intel_engine_cs *engine)
755{
756 struct drm_i915_private *dev_priv = engine->i915;
757
Chris Wilson05235c52016-07-20 09:21:08 +0100758 if (dev_priv->gt.awake)
759 return;
760
761 intel_runtime_pm_get_noresume(dev_priv);
762 dev_priv->gt.awake = true;
763
Chris Wilson54b4f682016-07-21 21:16:19 +0100764 intel_enable_gt_powersave(dev_priv);
Chris Wilson05235c52016-07-20 09:21:08 +0100765 i915_update_gfx_val(dev_priv);
766 if (INTEL_GEN(dev_priv) >= 6)
767 gen6_rps_busy(dev_priv);
768
769 queue_delayed_work(dev_priv->wq,
770 &dev_priv->gt.retire_work,
771 round_jiffies_up_relative(HZ));
772}
773
774/*
775 * NB: This function is not allowed to fail. Doing so would mean the the
776 * request is not being tracked for completion but the work itself is
777 * going to happen on the hardware. This would be a Bad Thing(tm).
778 */
Chris Wilson17f298cf2016-08-10 13:41:46 +0100779void __i915_add_request(struct drm_i915_gem_request *request, bool flush_caches)
Chris Wilson05235c52016-07-20 09:21:08 +0100780{
Chris Wilson95b2ab52016-08-15 10:48:46 +0100781 struct intel_engine_cs *engine = request->engine;
782 struct intel_ring *ring = request->ring;
Chris Wilson73cb9702016-10-28 13:58:46 +0100783 struct intel_timeline *timeline = request->timeline;
Chris Wilson0a046a02016-09-09 14:12:00 +0100784 struct drm_i915_gem_request *prev;
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100785 int err;
Chris Wilson05235c52016-07-20 09:21:08 +0100786
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100787 lockdep_assert_held(&request->i915->drm.struct_mutex);
Chris Wilson0f25dff2016-09-09 14:11:55 +0100788 trace_i915_gem_request_add(request);
789
Chris Wilson05235c52016-07-20 09:21:08 +0100790 /*
791 * To ensure that this call will not fail, space for its emissions
792 * should already have been reserved in the ring buffer. Let the ring
793 * know that it is time to use that space up.
794 */
Chris Wilson05235c52016-07-20 09:21:08 +0100795 request->reserved_space = 0;
796
797 /*
798 * Emit any outstanding flushes - execbuf can fail to emit the flush
799 * after having emitted the batchbuffer command. Hence we need to fix
800 * things up similar to emitting the lazy request. The difference here
801 * is that the flush _must_ happen before the next request, no matter
802 * what.
803 */
804 if (flush_caches) {
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100805 err = engine->emit_flush(request, EMIT_FLUSH);
Chris Wilsonc7fe7d22016-08-02 22:50:24 +0100806
Chris Wilson05235c52016-07-20 09:21:08 +0100807 /* Not allowed to fail! */
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100808 WARN(err, "engine->emit_flush() failed: %d!\n", err);
Chris Wilson05235c52016-07-20 09:21:08 +0100809 }
810
Chris Wilsond0454462016-08-15 10:48:40 +0100811 /* Record the position of the start of the breadcrumb so that
Chris Wilson05235c52016-07-20 09:21:08 +0100812 * should we detect the updated seqno part-way through the
813 * GPU processing the request, we never over-estimate the
Chris Wilsond0454462016-08-15 10:48:40 +0100814 * position of the ring's HEAD.
Chris Wilson05235c52016-07-20 09:21:08 +0100815 */
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100816 err = intel_ring_begin(request, engine->emit_breadcrumb_sz);
817 GEM_BUG_ON(err);
Chris Wilsonba76d912016-08-02 22:50:28 +0100818 request->postfix = ring->tail;
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100819 ring->tail += engine->emit_breadcrumb_sz * sizeof(u32);
Chris Wilson05235c52016-07-20 09:21:08 +0100820
Chris Wilson0f25dff2016-09-09 14:11:55 +0100821 /* Seal the request and mark it as pending execution. Note that
822 * we may inspect this state, without holding any locks, during
823 * hangcheck. Hence we apply the barrier to ensure that we do not
824 * see a more recent value in the hws than we are tracking.
825 */
Chris Wilson0a046a02016-09-09 14:12:00 +0100826
Chris Wilson73cb9702016-10-28 13:58:46 +0100827 prev = i915_gem_active_raw(&timeline->last_request,
Chris Wilson0a046a02016-09-09 14:12:00 +0100828 &request->i915->drm.struct_mutex);
Chris Wilson52e54202016-11-14 20:41:02 +0000829 if (prev) {
Chris Wilson0a046a02016-09-09 14:12:00 +0100830 i915_sw_fence_await_sw_fence(&request->submit, &prev->submit,
831 &request->submitq);
Chris Wilson52e54202016-11-14 20:41:02 +0000832 if (engine->schedule)
833 __i915_priotree_add_dependency(&request->priotree,
834 &prev->priotree,
835 &request->dep,
836 0);
837 }
Chris Wilson0a046a02016-09-09 14:12:00 +0100838
Chris Wilson80b204b2016-10-28 13:58:58 +0100839 spin_lock_irq(&timeline->lock);
Chris Wilsonf2d13292016-10-28 13:58:57 +0100840 list_add_tail(&request->link, &timeline->requests);
Chris Wilson80b204b2016-10-28 13:58:58 +0100841 spin_unlock_irq(&timeline->lock);
Chris Wilson28176ef2016-10-28 13:58:56 +0100842
Chris Wilson80b204b2016-10-28 13:58:58 +0100843 GEM_BUG_ON(i915_seqno_passed(timeline->last_submitted_seqno,
844 request->fence.seqno));
845
846 timeline->last_submitted_seqno = request->fence.seqno;
Chris Wilson73cb9702016-10-28 13:58:46 +0100847 i915_gem_active_set(&timeline->last_request, request);
Chris Wilsonf2d13292016-10-28 13:58:57 +0100848
Chris Wilson0f25dff2016-09-09 14:11:55 +0100849 list_add_tail(&request->ring_link, &ring->request_list);
Chris Wilsonf2d13292016-10-28 13:58:57 +0100850 request->emitted_jiffies = jiffies;
Chris Wilson0f25dff2016-09-09 14:11:55 +0100851
Chris Wilson05235c52016-07-20 09:21:08 +0100852 i915_gem_mark_busy(engine);
Chris Wilson5590af32016-09-09 14:11:54 +0100853
Chris Wilson0de91362016-11-14 20:41:01 +0000854 /* Let the backend know a new request has arrived that may need
855 * to adjust the existing execution schedule due to a high priority
856 * request - i.e. we may want to preempt the current request in order
857 * to run a high priority dependency chain *before* we can execute this
858 * request.
859 *
860 * This is called before the request is ready to run so that we can
861 * decide whether to preempt the entire chain so that it is ready to
862 * run at the earliest possible convenience.
863 */
864 if (engine->schedule)
865 engine->schedule(request, 0);
866
Chris Wilson5590af32016-09-09 14:11:54 +0100867 local_bh_disable();
868 i915_sw_fence_commit(&request->submit);
869 local_bh_enable(); /* Kick the execlists tasklet if just scheduled */
Chris Wilson05235c52016-07-20 09:21:08 +0100870}
871
Chris Wilson221fe792016-09-09 14:11:51 +0100872static void reset_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
873{
874 unsigned long flags;
875
876 spin_lock_irqsave(&q->lock, flags);
877 if (list_empty(&wait->task_list))
878 __add_wait_queue(q, wait);
879 spin_unlock_irqrestore(&q->lock, flags);
880}
881
Chris Wilson05235c52016-07-20 09:21:08 +0100882static unsigned long local_clock_us(unsigned int *cpu)
883{
884 unsigned long t;
885
886 /* Cheaply and approximately convert from nanoseconds to microseconds.
887 * The result and subsequent calculations are also defined in the same
888 * approximate microseconds units. The principal source of timing
889 * error here is from the simple truncation.
890 *
891 * Note that local_clock() is only defined wrt to the current CPU;
892 * the comparisons are no longer valid if we switch CPUs. Instead of
893 * blocking preemption for the entire busywait, we can detect the CPU
894 * switch and use that as indicator of system load and a reason to
895 * stop busywaiting, see busywait_stop().
896 */
897 *cpu = get_cpu();
898 t = local_clock() >> 10;
899 put_cpu();
900
901 return t;
902}
903
904static bool busywait_stop(unsigned long timeout, unsigned int cpu)
905{
906 unsigned int this_cpu;
907
908 if (time_after(local_clock_us(&this_cpu), timeout))
909 return true;
910
911 return this_cpu != cpu;
912}
913
914bool __i915_spin_request(const struct drm_i915_gem_request *req,
915 int state, unsigned long timeout_us)
916{
917 unsigned int cpu;
918
919 /* When waiting for high frequency requests, e.g. during synchronous
920 * rendering split between the CPU and GPU, the finite amount of time
921 * required to set up the irq and wait upon it limits the response
922 * rate. By busywaiting on the request completion for a short while we
923 * can service the high frequency waits as quick as possible. However,
924 * if it is a slow request, we want to sleep as quickly as possible.
925 * The tradeoff between waiting and sleeping is roughly the time it
926 * takes to sleep on a request, on the order of a microsecond.
927 */
928
929 timeout_us += local_clock_us(&cpu);
930 do {
Chris Wilson65e47602016-10-28 13:58:49 +0100931 if (__i915_gem_request_completed(req))
Chris Wilson05235c52016-07-20 09:21:08 +0100932 return true;
933
934 if (signal_pending_state(state, current))
935 break;
936
937 if (busywait_stop(timeout_us, cpu))
938 break;
939
940 cpu_relax_lowlatency();
941 } while (!need_resched());
942
943 return false;
944}
945
Chris Wilson4680816b2016-10-28 13:58:48 +0100946static long
Chris Wilson23902e42016-11-14 20:40:58 +0000947__i915_request_wait_for_execute(struct drm_i915_gem_request *request,
948 unsigned int flags,
949 long timeout)
Chris Wilson4680816b2016-10-28 13:58:48 +0100950{
951 const int state = flags & I915_WAIT_INTERRUPTIBLE ?
952 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
953 wait_queue_head_t *q = &request->i915->gpu_error.wait_queue;
954 DEFINE_WAIT(reset);
955 DEFINE_WAIT(wait);
956
957 if (flags & I915_WAIT_LOCKED)
958 add_wait_queue(q, &reset);
959
960 do {
Chris Wilson23902e42016-11-14 20:40:58 +0000961 prepare_to_wait(&request->execute.wait, &wait, state);
Chris Wilson4680816b2016-10-28 13:58:48 +0100962
Chris Wilson23902e42016-11-14 20:40:58 +0000963 if (i915_sw_fence_done(&request->execute))
Chris Wilson4680816b2016-10-28 13:58:48 +0100964 break;
965
966 if (flags & I915_WAIT_LOCKED &&
967 i915_reset_in_progress(&request->i915->gpu_error)) {
968 __set_current_state(TASK_RUNNING);
969 i915_reset(request->i915);
970 reset_wait_queue(q, &reset);
971 continue;
972 }
973
974 if (signal_pending_state(state, current)) {
975 timeout = -ERESTARTSYS;
976 break;
977 }
978
979 timeout = io_schedule_timeout(timeout);
980 } while (timeout);
Chris Wilson23902e42016-11-14 20:40:58 +0000981 finish_wait(&request->execute.wait, &wait);
Chris Wilson4680816b2016-10-28 13:58:48 +0100982
983 if (flags & I915_WAIT_LOCKED)
984 remove_wait_queue(q, &reset);
985
986 return timeout;
987}
988
Chris Wilson05235c52016-07-20 09:21:08 +0100989/**
Chris Wilson776f3232016-08-04 07:52:40 +0100990 * i915_wait_request - wait until execution of request has finished
Chris Wilsone95433c2016-10-28 13:58:27 +0100991 * @req: the request to wait upon
Chris Wilsonea746f32016-09-09 14:11:49 +0100992 * @flags: how to wait
Chris Wilsone95433c2016-10-28 13:58:27 +0100993 * @timeout: how long to wait in jiffies
Chris Wilson05235c52016-07-20 09:21:08 +0100994 *
Chris Wilsone95433c2016-10-28 13:58:27 +0100995 * i915_wait_request() waits for the request to be completed, for a
996 * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
997 * unbounded wait).
Chris Wilson05235c52016-07-20 09:21:08 +0100998 *
Chris Wilsone95433c2016-10-28 13:58:27 +0100999 * If the caller holds the struct_mutex, the caller must pass I915_WAIT_LOCKED
1000 * in via the flags, and vice versa if the struct_mutex is not held, the caller
1001 * must not specify that the wait is locked.
1002 *
1003 * Returns the remaining time (in jiffies) if the request completed, which may
1004 * be zero or -ETIME if the request is unfinished after the timeout expires.
1005 * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
1006 * pending before the request completes.
Chris Wilson05235c52016-07-20 09:21:08 +01001007 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001008long i915_wait_request(struct drm_i915_gem_request *req,
1009 unsigned int flags,
1010 long timeout)
Chris Wilson05235c52016-07-20 09:21:08 +01001011{
Chris Wilsonea746f32016-09-09 14:11:49 +01001012 const int state = flags & I915_WAIT_INTERRUPTIBLE ?
1013 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
Chris Wilson05235c52016-07-20 09:21:08 +01001014 DEFINE_WAIT(reset);
1015 struct intel_wait wait;
Chris Wilson05235c52016-07-20 09:21:08 +01001016
1017 might_sleep();
Chris Wilson22dd3bb2016-09-09 14:11:50 +01001018#if IS_ENABLED(CONFIG_LOCKDEP)
Chris Wilsone95433c2016-10-28 13:58:27 +01001019 GEM_BUG_ON(debug_locks &&
1020 !!lockdep_is_held(&req->i915->drm.struct_mutex) !=
Chris Wilson22dd3bb2016-09-09 14:11:50 +01001021 !!(flags & I915_WAIT_LOCKED));
1022#endif
Chris Wilsone95433c2016-10-28 13:58:27 +01001023 GEM_BUG_ON(timeout < 0);
Chris Wilson05235c52016-07-20 09:21:08 +01001024
Chris Wilson05235c52016-07-20 09:21:08 +01001025 if (i915_gem_request_completed(req))
Chris Wilsone95433c2016-10-28 13:58:27 +01001026 return timeout;
Chris Wilson05235c52016-07-20 09:21:08 +01001027
Chris Wilsone95433c2016-10-28 13:58:27 +01001028 if (!timeout)
1029 return -ETIME;
Chris Wilson05235c52016-07-20 09:21:08 +01001030
1031 trace_i915_gem_request_wait_begin(req);
1032
Chris Wilson23902e42016-11-14 20:40:58 +00001033 if (!i915_sw_fence_done(&req->execute)) {
1034 timeout = __i915_request_wait_for_execute(req, flags, timeout);
Chris Wilson4680816b2016-10-28 13:58:48 +01001035 if (timeout < 0)
1036 goto complete;
1037
Chris Wilson23902e42016-11-14 20:40:58 +00001038 GEM_BUG_ON(!i915_sw_fence_done(&req->execute));
Chris Wilson4680816b2016-10-28 13:58:48 +01001039 }
Chris Wilson23902e42016-11-14 20:40:58 +00001040 GEM_BUG_ON(!i915_sw_fence_done(&req->submit));
Chris Wilson65e47602016-10-28 13:58:49 +01001041 GEM_BUG_ON(!req->global_seqno);
Chris Wilson4680816b2016-10-28 13:58:48 +01001042
Daniel Vetter437c3082016-08-05 18:11:24 +02001043 /* Optimistic short spin before touching IRQs */
Chris Wilson05235c52016-07-20 09:21:08 +01001044 if (i915_spin_request(req, state, 5))
1045 goto complete;
1046
1047 set_current_state(state);
Chris Wilson22dd3bb2016-09-09 14:11:50 +01001048 if (flags & I915_WAIT_LOCKED)
1049 add_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
Chris Wilson05235c52016-07-20 09:21:08 +01001050
Chris Wilson65e47602016-10-28 13:58:49 +01001051 intel_wait_init(&wait, req->global_seqno);
Chris Wilson05235c52016-07-20 09:21:08 +01001052 if (intel_engine_add_wait(req->engine, &wait))
1053 /* In order to check that we haven't missed the interrupt
1054 * as we enabled it, we need to kick ourselves to do a
1055 * coherent check on the seqno before we sleep.
1056 */
1057 goto wakeup;
1058
1059 for (;;) {
1060 if (signal_pending_state(state, current)) {
Chris Wilsone95433c2016-10-28 13:58:27 +01001061 timeout = -ERESTARTSYS;
Chris Wilson05235c52016-07-20 09:21:08 +01001062 break;
1063 }
1064
Chris Wilsone95433c2016-10-28 13:58:27 +01001065 if (!timeout) {
1066 timeout = -ETIME;
Chris Wilson05235c52016-07-20 09:21:08 +01001067 break;
1068 }
1069
Chris Wilsone95433c2016-10-28 13:58:27 +01001070 timeout = io_schedule_timeout(timeout);
1071
Chris Wilson05235c52016-07-20 09:21:08 +01001072 if (intel_wait_complete(&wait))
1073 break;
1074
1075 set_current_state(state);
1076
1077wakeup:
1078 /* Carefully check if the request is complete, giving time
1079 * for the seqno to be visible following the interrupt.
1080 * We also have to check in case we are kicked by the GPU
1081 * reset in order to drop the struct_mutex.
1082 */
1083 if (__i915_request_irq_complete(req))
1084 break;
1085
Chris Wilson221fe792016-09-09 14:11:51 +01001086 /* If the GPU is hung, and we hold the lock, reset the GPU
1087 * and then check for completion. On a full reset, the engine's
1088 * HW seqno will be advanced passed us and we are complete.
1089 * If we do a partial reset, we have to wait for the GPU to
1090 * resume and update the breadcrumb.
1091 *
1092 * If we don't hold the mutex, we can just wait for the worker
1093 * to come along and update the breadcrumb (either directly
1094 * itself, or indirectly by recovering the GPU).
1095 */
1096 if (flags & I915_WAIT_LOCKED &&
1097 i915_reset_in_progress(&req->i915->gpu_error)) {
1098 __set_current_state(TASK_RUNNING);
1099 i915_reset(req->i915);
1100 reset_wait_queue(&req->i915->gpu_error.wait_queue,
1101 &reset);
1102 continue;
1103 }
1104
Chris Wilson05235c52016-07-20 09:21:08 +01001105 /* Only spin if we know the GPU is processing this request */
1106 if (i915_spin_request(req, state, 2))
1107 break;
1108 }
Chris Wilson05235c52016-07-20 09:21:08 +01001109
1110 intel_engine_remove_wait(req->engine, &wait);
Chris Wilson22dd3bb2016-09-09 14:11:50 +01001111 if (flags & I915_WAIT_LOCKED)
1112 remove_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
Chris Wilson05235c52016-07-20 09:21:08 +01001113 __set_current_state(TASK_RUNNING);
Chris Wilson22dd3bb2016-09-09 14:11:50 +01001114
Chris Wilson05235c52016-07-20 09:21:08 +01001115complete:
1116 trace_i915_gem_request_wait_end(req);
1117
Chris Wilsone95433c2016-10-28 13:58:27 +01001118 return timeout;
Chris Wilson05235c52016-07-20 09:21:08 +01001119}
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001120
Chris Wilson28176ef2016-10-28 13:58:56 +01001121static void engine_retire_requests(struct intel_engine_cs *engine)
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001122{
1123 struct drm_i915_gem_request *request, *next;
1124
Chris Wilson73cb9702016-10-28 13:58:46 +01001125 list_for_each_entry_safe(request, next,
1126 &engine->timeline->requests, link) {
Chris Wilson80b204b2016-10-28 13:58:58 +01001127 if (!__i915_gem_request_completed(request))
Chris Wilson28176ef2016-10-28 13:58:56 +01001128 return;
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001129
1130 i915_gem_request_retire(request);
1131 }
1132}
1133
1134void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
1135{
1136 struct intel_engine_cs *engine;
Chris Wilson28176ef2016-10-28 13:58:56 +01001137 enum intel_engine_id id;
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001138
1139 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1140
Chris Wilson28176ef2016-10-28 13:58:56 +01001141 if (!dev_priv->gt.active_requests)
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001142 return;
1143
1144 GEM_BUG_ON(!dev_priv->gt.awake);
1145
Chris Wilson28176ef2016-10-28 13:58:56 +01001146 for_each_engine(engine, dev_priv, id)
1147 engine_retire_requests(engine);
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001148
Chris Wilson28176ef2016-10-28 13:58:56 +01001149 if (!dev_priv->gt.active_requests)
Imre Deak5bd11a32016-11-07 11:20:02 +02001150 mod_delayed_work(dev_priv->wq,
1151 &dev_priv->gt.idle_work,
1152 msecs_to_jiffies(100));
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001153}