blob: 0e8d1010cecb4c4dd7ab53ed3b0ff87cb3f0b837 [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>
Ingo Molnare6017572017-02-01 16:36:40 +010027#include <linux/sched.h>
28#include <linux/sched/clock.h>
Ingo Molnarf361bf42017-02-03 23:47:37 +010029#include <linux/sched/signal.h>
Chris Wilsonfa545cb2016-08-04 07:52:35 +010030
Chris Wilson05235c52016-07-20 09:21:08 +010031#include "i915_drv.h"
32
Chris Wilsonf54d1862016-10-25 13:00:45 +010033static const char *i915_fence_get_driver_name(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010034{
35 return "i915";
36}
37
Chris Wilsonf54d1862016-10-25 13:00:45 +010038static const char *i915_fence_get_timeline_name(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010039{
Chris Wilson73cb9702016-10-28 13:58:46 +010040 return to_request(fence)->timeline->common->name;
Chris Wilson04769652016-07-20 09:21:11 +010041}
42
Chris Wilsonf54d1862016-10-25 13:00:45 +010043static bool i915_fence_signaled(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010044{
45 return i915_gem_request_completed(to_request(fence));
46}
47
Chris Wilsonf54d1862016-10-25 13:00:45 +010048static bool i915_fence_enable_signaling(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010049{
50 if (i915_fence_signaled(fence))
51 return false;
52
53 intel_engine_enable_signaling(to_request(fence));
54 return true;
55}
56
Chris Wilsonf54d1862016-10-25 13:00:45 +010057static signed long i915_fence_wait(struct dma_fence *fence,
Chris Wilson04769652016-07-20 09:21:11 +010058 bool interruptible,
Chris Wilsone95433c2016-10-28 13:58:27 +010059 signed long timeout)
Chris Wilson04769652016-07-20 09:21:11 +010060{
Chris Wilsone95433c2016-10-28 13:58:27 +010061 return i915_wait_request(to_request(fence), interruptible, timeout);
Chris Wilson04769652016-07-20 09:21:11 +010062}
63
Chris Wilsonf54d1862016-10-25 13:00:45 +010064static void i915_fence_release(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010065{
66 struct drm_i915_gem_request *req = to_request(fence);
67
Chris Wilsonfc158402016-11-25 13:17:18 +000068 /* The request is put onto a RCU freelist (i.e. the address
69 * is immediately reused), mark the fences as being freed now.
70 * Otherwise the debugobjects for the fences are only marked as
71 * freed when the slab cache itself is freed, and so we would get
72 * caught trying to reuse dead objects.
73 */
74 i915_sw_fence_fini(&req->submit);
Chris Wilsonfc158402016-11-25 13:17:18 +000075
Chris Wilson04769652016-07-20 09:21:11 +010076 kmem_cache_free(req->i915->requests, req);
77}
78
Chris Wilsonf54d1862016-10-25 13:00:45 +010079const struct dma_fence_ops i915_fence_ops = {
Chris Wilson04769652016-07-20 09:21:11 +010080 .get_driver_name = i915_fence_get_driver_name,
81 .get_timeline_name = i915_fence_get_timeline_name,
82 .enable_signaling = i915_fence_enable_signaling,
83 .signaled = i915_fence_signaled,
84 .wait = i915_fence_wait,
85 .release = i915_fence_release,
Chris Wilson04769652016-07-20 09:21:11 +010086};
87
Chris Wilson05235c52016-07-20 09:21:08 +010088static inline void
89i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
90{
Chris Wilsonc8659ef2017-03-02 12:25:25 +000091 struct drm_i915_file_private *file_priv;
Chris Wilson05235c52016-07-20 09:21:08 +010092
Chris Wilsonc8659ef2017-03-02 12:25:25 +000093 file_priv = request->file_priv;
Chris Wilson05235c52016-07-20 09:21:08 +010094 if (!file_priv)
95 return;
96
97 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +000098 if (request->file_priv) {
99 list_del(&request->client_link);
100 request->file_priv = NULL;
101 }
Chris Wilson05235c52016-07-20 09:21:08 +0100102 spin_unlock(&file_priv->mm.lock);
Chris Wilson05235c52016-07-20 09:21:08 +0100103}
104
Chris Wilson52e54202016-11-14 20:41:02 +0000105static struct i915_dependency *
106i915_dependency_alloc(struct drm_i915_private *i915)
107{
108 return kmem_cache_alloc(i915->dependencies, GFP_KERNEL);
109}
110
111static void
112i915_dependency_free(struct drm_i915_private *i915,
113 struct i915_dependency *dep)
114{
115 kmem_cache_free(i915->dependencies, dep);
116}
117
118static void
119__i915_priotree_add_dependency(struct i915_priotree *pt,
120 struct i915_priotree *signal,
121 struct i915_dependency *dep,
122 unsigned long flags)
123{
Chris Wilson20311bd2016-11-14 20:41:03 +0000124 INIT_LIST_HEAD(&dep->dfs_link);
Chris Wilson52e54202016-11-14 20:41:02 +0000125 list_add(&dep->wait_link, &signal->waiters_list);
126 list_add(&dep->signal_link, &pt->signalers_list);
127 dep->signaler = signal;
128 dep->flags = flags;
129}
130
131static int
132i915_priotree_add_dependency(struct drm_i915_private *i915,
133 struct i915_priotree *pt,
134 struct i915_priotree *signal)
135{
136 struct i915_dependency *dep;
137
138 dep = i915_dependency_alloc(i915);
139 if (!dep)
140 return -ENOMEM;
141
142 __i915_priotree_add_dependency(pt, signal, dep, I915_DEPENDENCY_ALLOC);
143 return 0;
144}
145
146static void
147i915_priotree_fini(struct drm_i915_private *i915, struct i915_priotree *pt)
148{
149 struct i915_dependency *dep, *next;
150
Chris Wilson20311bd2016-11-14 20:41:03 +0000151 GEM_BUG_ON(!RB_EMPTY_NODE(&pt->node));
152
Chris Wilson52e54202016-11-14 20:41:02 +0000153 /* Everyone we depended upon (the fences we wait to be signaled)
154 * should retire before us and remove themselves from our list.
155 * However, retirement is run independently on each timeline and
156 * so we may be called out-of-order.
157 */
158 list_for_each_entry_safe(dep, next, &pt->signalers_list, signal_link) {
159 list_del(&dep->wait_link);
160 if (dep->flags & I915_DEPENDENCY_ALLOC)
161 i915_dependency_free(i915, dep);
162 }
163
164 /* Remove ourselves from everyone who depends upon us */
165 list_for_each_entry_safe(dep, next, &pt->waiters_list, wait_link) {
166 list_del(&dep->signal_link);
167 if (dep->flags & I915_DEPENDENCY_ALLOC)
168 i915_dependency_free(i915, dep);
169 }
170}
171
172static void
173i915_priotree_init(struct i915_priotree *pt)
174{
175 INIT_LIST_HEAD(&pt->signalers_list);
176 INIT_LIST_HEAD(&pt->waiters_list);
Chris Wilson20311bd2016-11-14 20:41:03 +0000177 RB_CLEAR_NODE(&pt->node);
178 pt->priority = INT_MIN;
Chris Wilson52e54202016-11-14 20:41:02 +0000179}
180
Chris Wilson12d31732017-02-23 07:44:09 +0000181static int reset_all_global_seqno(struct drm_i915_private *i915, u32 seqno)
182{
183 struct i915_gem_timeline *timeline = &i915->gt.global_timeline;
184 struct intel_engine_cs *engine;
185 enum intel_engine_id id;
186 int ret;
187
188 /* Carefully retire all requests without writing to the rings */
189 ret = i915_gem_wait_for_idle(i915,
190 I915_WAIT_INTERRUPTIBLE |
191 I915_WAIT_LOCKED);
192 if (ret)
193 return ret;
194
195 i915_gem_retire_requests(i915);
196 GEM_BUG_ON(i915->gt.active_requests > 1);
197
198 /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
199 for_each_engine(engine, i915, id) {
200 struct intel_timeline *tl = &timeline->engine[id];
201
Chris Wilson54003672017-03-03 12:19:46 +0000202 if (wait_for(intel_engine_is_idle(engine), 50))
203 return -EBUSY;
204
Chris Wilson12d31732017-02-23 07:44:09 +0000205 if (!i915_seqno_passed(seqno, tl->seqno)) {
206 /* spin until threads are complete */
207 while (intel_breadcrumbs_busy(engine))
208 cond_resched();
209 }
210
211 /* Finally reset hw state */
212 tl->seqno = seqno;
213 intel_engine_init_global_seqno(engine, seqno);
214 }
215
216 list_for_each_entry(timeline, &i915->gt.timelines, link) {
217 for_each_engine(engine, i915, id) {
218 struct intel_timeline *tl = &timeline->engine[id];
219
220 memset(tl->sync_seqno, 0, sizeof(tl->sync_seqno));
221 }
222 }
223
224 return 0;
225}
226
227int i915_gem_set_global_seqno(struct drm_device *dev, u32 seqno)
228{
229 struct drm_i915_private *dev_priv = to_i915(dev);
230
231 lockdep_assert_held(&dev_priv->drm.struct_mutex);
232
233 if (seqno == 0)
234 return -EINVAL;
235
236 /* HWS page needs to be set less than what we
237 * will inject to ring
238 */
239 return reset_all_global_seqno(dev_priv, seqno - 1);
240}
241
242static int reserve_seqno(struct intel_engine_cs *engine)
243{
244 u32 active = ++engine->timeline->inflight_seqnos;
245 u32 seqno = engine->timeline->seqno;
246 int ret;
247
248 /* Reservation is fine until we need to wrap around */
249 if (likely(!add_overflows(seqno, active)))
250 return 0;
251
252 ret = reset_all_global_seqno(engine->i915, 0);
253 if (ret) {
254 engine->timeline->inflight_seqnos--;
255 return ret;
256 }
257
258 return 0;
259}
260
Chris Wilson9b6586a2017-02-23 07:44:08 +0000261static void unreserve_seqno(struct intel_engine_cs *engine)
262{
263 GEM_BUG_ON(!engine->timeline->inflight_seqnos);
264 engine->timeline->inflight_seqnos--;
265}
266
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100267void i915_gem_retire_noop(struct i915_gem_active *active,
268 struct drm_i915_gem_request *request)
269{
270 /* Space left intentionally blank */
271}
272
Chris Wilson05235c52016-07-20 09:21:08 +0100273static void i915_gem_request_retire(struct drm_i915_gem_request *request)
274{
Chris Wilsone8a9c582016-12-18 15:37:20 +0000275 struct intel_engine_cs *engine = request->engine;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100276 struct i915_gem_active *active, *next;
277
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100278 lockdep_assert_held(&request->i915->drm.struct_mutex);
Chris Wilson48bc2a42016-11-25 13:17:17 +0000279 GEM_BUG_ON(!i915_sw_fence_signaled(&request->submit));
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100280 GEM_BUG_ON(!i915_gem_request_completed(request));
Chris Wilson43020552016-11-15 16:46:20 +0000281 GEM_BUG_ON(!request->i915->gt.active_requests);
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100282
Chris Wilson05235c52016-07-20 09:21:08 +0100283 trace_i915_gem_request_retire(request);
Chris Wilson80b204b2016-10-28 13:58:58 +0100284
Chris Wilsone8a9c582016-12-18 15:37:20 +0000285 spin_lock_irq(&engine->timeline->lock);
Chris Wilsone95433c2016-10-28 13:58:27 +0100286 list_del_init(&request->link);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000287 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson05235c52016-07-20 09:21:08 +0100288
289 /* We know the GPU must have read the request to have
290 * sent us the seqno + interrupt, so use the position
291 * of tail of the request to update the last known position
292 * of the GPU head.
293 *
294 * Note this requires that we are always called in request
295 * completion order.
296 */
Chris Wilson675d9ad2016-08-04 07:52:36 +0100297 list_del(&request->ring_link);
Chris Wilson1dae2df2016-08-02 22:50:19 +0100298 request->ring->last_retired_head = request->postfix;
Chris Wilson43020552016-11-15 16:46:20 +0000299 if (!--request->i915->gt.active_requests) {
300 GEM_BUG_ON(!request->i915->gt.awake);
301 mod_delayed_work(request->i915->wq,
302 &request->i915->gt.idle_work,
303 msecs_to_jiffies(100));
304 }
Chris Wilson9b6586a2017-02-23 07:44:08 +0000305 unreserve_seqno(request->engine);
Chris Wilson05235c52016-07-20 09:21:08 +0100306
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100307 /* Walk through the active list, calling retire on each. This allows
308 * objects to track their GPU activity and mark themselves as idle
309 * when their *last* active request is completed (updating state
310 * tracking lists for eviction, active references for GEM, etc).
311 *
312 * As the ->retire() may free the node, we decouple it first and
313 * pass along the auxiliary information (to avoid dereferencing
314 * the node after the callback).
315 */
316 list_for_each_entry_safe(active, next, &request->active_list, link) {
317 /* In microbenchmarks or focusing upon time inside the kernel,
318 * we may spend an inordinate amount of time simply handling
319 * the retirement of requests and processing their callbacks.
320 * Of which, this loop itself is particularly hot due to the
321 * cache misses when jumping around the list of i915_gem_active.
322 * So we try to keep this loop as streamlined as possible and
323 * also prefetch the next i915_gem_active to try and hide
324 * the likely cache miss.
325 */
326 prefetchw(next);
327
328 INIT_LIST_HEAD(&active->link);
Chris Wilson0eafec62016-08-04 16:32:41 +0100329 RCU_INIT_POINTER(active->request, NULL);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100330
331 active->retire(active, request);
332 }
333
Chris Wilson05235c52016-07-20 09:21:08 +0100334 i915_gem_request_remove_from_client(request);
335
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +0200336 /* Retirement decays the ban score as it is a sign of ctx progress */
Mika Kuoppalabc1d53c2016-11-16 17:20:34 +0200337 if (request->ctx->ban_score > 0)
338 request->ctx->ban_score--;
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +0200339
Chris Wilsone8a9c582016-12-18 15:37:20 +0000340 /* The backing object for the context is done after switching to the
341 * *next* context. Therefore we cannot retire the previous context until
342 * the next context has already started running. However, since we
343 * cannot take the required locks at i915_gem_request_submit() we
344 * defer the unpinning of the active context to now, retirement of
345 * the subsequent request.
346 */
347 if (engine->last_retired_context)
348 engine->context_unpin(engine, engine->last_retired_context);
349 engine->last_retired_context = request->ctx;
Chris Wilsond07f0e52016-10-28 13:58:44 +0100350
351 dma_fence_signal(&request->fence);
Chris Wilson52e54202016-11-14 20:41:02 +0000352
353 i915_priotree_fini(request->i915, &request->priotree);
Chris Wilsone8a261e2016-07-20 13:31:49 +0100354 i915_gem_request_put(request);
Chris Wilson05235c52016-07-20 09:21:08 +0100355}
356
357void i915_gem_request_retire_upto(struct drm_i915_gem_request *req)
358{
359 struct intel_engine_cs *engine = req->engine;
360 struct drm_i915_gem_request *tmp;
361
362 lockdep_assert_held(&req->i915->drm.struct_mutex);
Chris Wilson4ffd6e02016-11-25 13:17:15 +0000363 GEM_BUG_ON(!i915_gem_request_completed(req));
364
Chris Wilsone95433c2016-10-28 13:58:27 +0100365 if (list_empty(&req->link))
366 return;
Chris Wilson05235c52016-07-20 09:21:08 +0100367
368 do {
Chris Wilson73cb9702016-10-28 13:58:46 +0100369 tmp = list_first_entry(&engine->timeline->requests,
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100370 typeof(*tmp), link);
Chris Wilson05235c52016-07-20 09:21:08 +0100371
372 i915_gem_request_retire(tmp);
373 } while (tmp != req);
Chris Wilson05235c52016-07-20 09:21:08 +0100374}
375
Chris Wilson9b6586a2017-02-23 07:44:08 +0000376static u32 timeline_get_seqno(struct intel_timeline *tl)
Chris Wilson05235c52016-07-20 09:21:08 +0100377{
Chris Wilson9b6586a2017-02-23 07:44:08 +0000378 return ++tl->seqno;
Chris Wilson05235c52016-07-20 09:21:08 +0100379}
380
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000381void __i915_gem_request_submit(struct drm_i915_gem_request *request)
Chris Wilson5590af32016-09-09 14:11:54 +0100382{
Chris Wilson73cb9702016-10-28 13:58:46 +0100383 struct intel_engine_cs *engine = request->engine;
Chris Wilsonf2d13292016-10-28 13:58:57 +0100384 struct intel_timeline *timeline;
385 u32 seqno;
Chris Wilson5590af32016-09-09 14:11:54 +0100386
Chris Wilsone60a8702017-03-02 11:51:30 +0000387 GEM_BUG_ON(!irqs_disabled());
Chris Wilson67520412017-03-02 13:28:01 +0000388 lockdep_assert_held(&engine->timeline->lock);
Chris Wilsone60a8702017-03-02 11:51:30 +0000389
Chris Wilsonfe497892017-02-23 07:44:13 +0000390 trace_i915_gem_request_execute(request);
391
Chris Wilson80b204b2016-10-28 13:58:58 +0100392 /* Transfer from per-context onto the global per-engine timeline */
393 timeline = engine->timeline;
394 GEM_BUG_ON(timeline == request->timeline);
Chris Wilson5590af32016-09-09 14:11:54 +0100395
Chris Wilson9b6586a2017-02-23 07:44:08 +0000396 seqno = timeline_get_seqno(timeline);
Chris Wilsonf2d13292016-10-28 13:58:57 +0100397 GEM_BUG_ON(!seqno);
398 GEM_BUG_ON(i915_seqno_passed(intel_engine_get_seqno(engine), seqno));
399
Chris Wilsonf2d13292016-10-28 13:58:57 +0100400 /* We may be recursing from the signal callback of another i915 fence */
401 spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
402 request->global_seqno = seqno;
403 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
404 intel_engine_enable_signaling(request);
405 spin_unlock(&request->lock);
406
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100407 engine->emit_breadcrumb(request,
408 request->ring->vaddr + request->postfix);
Chris Wilson5590af32016-09-09 14:11:54 +0100409
Chris Wilsonbb894852016-11-14 20:40:57 +0000410 spin_lock(&request->timeline->lock);
Chris Wilson80b204b2016-10-28 13:58:58 +0100411 list_move_tail(&request->link, &timeline->requests);
412 spin_unlock(&request->timeline->lock);
413
Chris Wilsonfe497892017-02-23 07:44:13 +0000414 wake_up_all(&request->execute);
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000415}
Chris Wilson23902e42016-11-14 20:40:58 +0000416
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000417void i915_gem_request_submit(struct drm_i915_gem_request *request)
418{
419 struct intel_engine_cs *engine = request->engine;
420 unsigned long flags;
421
422 /* Will be called from irq-context when using foreign fences. */
423 spin_lock_irqsave(&engine->timeline->lock, flags);
424
425 __i915_gem_request_submit(request);
426
427 spin_unlock_irqrestore(&engine->timeline->lock, flags);
428}
429
Chris Wilsond6a22892017-02-23 07:44:17 +0000430void __i915_gem_request_unsubmit(struct drm_i915_gem_request *request)
431{
432 struct intel_engine_cs *engine = request->engine;
433 struct intel_timeline *timeline;
434
Chris Wilsone60a8702017-03-02 11:51:30 +0000435 GEM_BUG_ON(!irqs_disabled());
Chris Wilson67520412017-03-02 13:28:01 +0000436 lockdep_assert_held(&engine->timeline->lock);
Chris Wilsond6a22892017-02-23 07:44:17 +0000437
438 /* Only unwind in reverse order, required so that the per-context list
439 * is kept in seqno/ring order.
440 */
441 GEM_BUG_ON(request->global_seqno != engine->timeline->seqno);
442 engine->timeline->seqno--;
443
444 /* We may be recursing from the signal callback of another i915 fence */
445 spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
446 request->global_seqno = 0;
447 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
448 intel_engine_cancel_signaling(request);
449 spin_unlock(&request->lock);
450
451 /* Transfer back from the global per-engine timeline to per-context */
452 timeline = request->timeline;
453 GEM_BUG_ON(timeline == engine->timeline);
454
455 spin_lock(&timeline->lock);
456 list_move(&request->link, &timeline->requests);
457 spin_unlock(&timeline->lock);
458
459 /* We don't need to wake_up any waiters on request->execute, they
460 * will get woken by any other event or us re-adding this request
461 * to the engine timeline (__i915_gem_request_submit()). The waiters
462 * should be quite adapt at finding that the request now has a new
463 * global_seqno to the one they went to sleep on.
464 */
465}
466
467void i915_gem_request_unsubmit(struct drm_i915_gem_request *request)
468{
469 struct intel_engine_cs *engine = request->engine;
470 unsigned long flags;
471
472 /* Will be called from irq-context when using foreign fences. */
473 spin_lock_irqsave(&engine->timeline->lock, flags);
474
475 __i915_gem_request_unsubmit(request);
476
477 spin_unlock_irqrestore(&engine->timeline->lock, flags);
478}
479
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000480static int __i915_sw_fence_call
481submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
482{
Chris Wilson48bc2a42016-11-25 13:17:17 +0000483 struct drm_i915_gem_request *request =
484 container_of(fence, typeof(*request), submit);
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000485
Chris Wilson48bc2a42016-11-25 13:17:17 +0000486 switch (state) {
487 case FENCE_COMPLETE:
Tvrtko Ursulin354d0362017-02-21 11:01:42 +0000488 trace_i915_gem_request_submit(request);
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000489 request->engine->submit_request(request);
Chris Wilson48bc2a42016-11-25 13:17:17 +0000490 break;
491
492 case FENCE_FREE:
493 i915_gem_request_put(request);
494 break;
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000495 }
Chris Wilson80b204b2016-10-28 13:58:58 +0100496
Chris Wilson5590af32016-09-09 14:11:54 +0100497 return NOTIFY_DONE;
498}
499
Chris Wilson8e637172016-08-02 22:50:26 +0100500/**
501 * i915_gem_request_alloc - allocate a request structure
502 *
503 * @engine: engine that we wish to issue the request on.
504 * @ctx: context that the request will be associated with.
505 * This can be NULL if the request is not directly related to
506 * any specific user context, in which case this function will
507 * choose an appropriate context to use.
508 *
509 * Returns a pointer to the allocated request if successful,
510 * or an error code if not.
511 */
512struct drm_i915_gem_request *
513i915_gem_request_alloc(struct intel_engine_cs *engine,
514 struct i915_gem_context *ctx)
Chris Wilson05235c52016-07-20 09:21:08 +0100515{
516 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson05235c52016-07-20 09:21:08 +0100517 struct drm_i915_gem_request *req;
518 int ret;
519
Chris Wilson28176ef2016-10-28 13:58:56 +0100520 lockdep_assert_held(&dev_priv->drm.struct_mutex);
521
Chris Wilson05235c52016-07-20 09:21:08 +0100522 /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
Chris Wilson6ffb7d02017-01-14 16:23:33 +0000523 * EIO if the GPU is already wedged.
Chris Wilson05235c52016-07-20 09:21:08 +0100524 */
Chris Wilson6ffb7d02017-01-14 16:23:33 +0000525 if (i915_terminally_wedged(&dev_priv->gpu_error))
526 return ERR_PTR(-EIO);
Chris Wilson05235c52016-07-20 09:21:08 +0100527
Chris Wilsone8a9c582016-12-18 15:37:20 +0000528 /* Pinning the contexts may generate requests in order to acquire
529 * GGTT space, so do this first before we reserve a seqno for
530 * ourselves.
531 */
532 ret = engine->context_pin(engine, ctx);
Chris Wilson28176ef2016-10-28 13:58:56 +0100533 if (ret)
534 return ERR_PTR(ret);
535
Chris Wilson9b6586a2017-02-23 07:44:08 +0000536 ret = reserve_seqno(engine);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000537 if (ret)
538 goto err_unpin;
539
Chris Wilson9b5f4e52016-07-20 09:21:09 +0100540 /* Move the oldest request to the slab-cache (if not in use!) */
Chris Wilson73cb9702016-10-28 13:58:46 +0100541 req = list_first_entry_or_null(&engine->timeline->requests,
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100542 typeof(*req), link);
Chris Wilson754c9fd2017-02-23 07:44:14 +0000543 if (req && i915_gem_request_completed(req))
Chris Wilson2a1d7752016-07-26 12:01:51 +0100544 i915_gem_request_retire(req);
Chris Wilson9b5f4e52016-07-20 09:21:09 +0100545
Chris Wilson5a198b82016-08-09 09:23:34 +0100546 /* Beware: Dragons be flying overhead.
547 *
548 * We use RCU to look up requests in flight. The lookups may
549 * race with the request being allocated from the slab freelist.
550 * That is the request we are writing to here, may be in the process
Chris Wilson1426f712016-08-09 17:03:22 +0100551 * of being read by __i915_gem_active_get_rcu(). As such,
Chris Wilson5a198b82016-08-09 09:23:34 +0100552 * we have to be very careful when overwriting the contents. During
553 * the RCU lookup, we change chase the request->engine pointer,
Chris Wilson65e47602016-10-28 13:58:49 +0100554 * read the request->global_seqno and increment the reference count.
Chris Wilson5a198b82016-08-09 09:23:34 +0100555 *
556 * The reference count is incremented atomically. If it is zero,
557 * the lookup knows the request is unallocated and complete. Otherwise,
558 * it is either still in use, or has been reallocated and reset
Chris Wilsonf54d1862016-10-25 13:00:45 +0100559 * with dma_fence_init(). This increment is safe for release as we
560 * check that the request we have a reference to and matches the active
Chris Wilson5a198b82016-08-09 09:23:34 +0100561 * request.
562 *
563 * Before we increment the refcount, we chase the request->engine
564 * pointer. We must not call kmem_cache_zalloc() or else we set
565 * that pointer to NULL and cause a crash during the lookup. If
566 * we see the request is completed (based on the value of the
567 * old engine and seqno), the lookup is complete and reports NULL.
568 * If we decide the request is not completed (new engine or seqno),
569 * then we grab a reference and double check that it is still the
570 * active request - which it won't be and restart the lookup.
571 *
572 * Do not use kmem_cache_zalloc() here!
573 */
574 req = kmem_cache_alloc(dev_priv->requests, GFP_KERNEL);
Chris Wilson28176ef2016-10-28 13:58:56 +0100575 if (!req) {
576 ret = -ENOMEM;
577 goto err_unreserve;
578 }
Chris Wilson05235c52016-07-20 09:21:08 +0100579
Chris Wilson80b204b2016-10-28 13:58:58 +0100580 req->timeline = i915_gem_context_lookup_timeline(ctx, engine);
581 GEM_BUG_ON(req->timeline == engine->timeline);
Chris Wilson73cb9702016-10-28 13:58:46 +0100582
Chris Wilson04769652016-07-20 09:21:11 +0100583 spin_lock_init(&req->lock);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100584 dma_fence_init(&req->fence,
585 &i915_fence_ops,
586 &req->lock,
Chris Wilson73cb9702016-10-28 13:58:46 +0100587 req->timeline->fence_context,
Chris Wilson9b6586a2017-02-23 07:44:08 +0000588 timeline_get_seqno(req->timeline));
Chris Wilson04769652016-07-20 09:21:11 +0100589
Chris Wilson48bc2a42016-11-25 13:17:17 +0000590 /* We bump the ref for the fence chain */
591 i915_sw_fence_init(&i915_gem_request_get(req)->submit, submit_notify);
Chris Wilsonfe497892017-02-23 07:44:13 +0000592 init_waitqueue_head(&req->execute);
Chris Wilson5590af32016-09-09 14:11:54 +0100593
Chris Wilson52e54202016-11-14 20:41:02 +0000594 i915_priotree_init(&req->priotree);
595
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100596 INIT_LIST_HEAD(&req->active_list);
Chris Wilson05235c52016-07-20 09:21:08 +0100597 req->i915 = dev_priv;
598 req->engine = engine;
Chris Wilsone8a9c582016-12-18 15:37:20 +0000599 req->ctx = ctx;
Chris Wilson05235c52016-07-20 09:21:08 +0100600
Chris Wilson5a198b82016-08-09 09:23:34 +0100601 /* No zalloc, must clear what we need by hand */
Chris Wilsonf2d13292016-10-28 13:58:57 +0100602 req->global_seqno = 0;
Chris Wilson5a198b82016-08-09 09:23:34 +0100603 req->file_priv = NULL;
Chris Wilson058d88c2016-08-15 10:49:06 +0100604 req->batch = NULL;
Chris Wilson5a198b82016-08-09 09:23:34 +0100605
Chris Wilson05235c52016-07-20 09:21:08 +0100606 /*
607 * Reserve space in the ring buffer for all the commands required to
608 * eventually emit this request. This is to guarantee that the
609 * i915_add_request() call can't fail. Note that the reserve may need
610 * to be redone if the request is not actually submitted straight
611 * away, e.g. because a GPU scheduler has deferred it.
612 */
613 req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
Chris Wilson98f29e82016-10-28 13:58:51 +0100614 GEM_BUG_ON(req->reserved_space < engine->emit_breadcrumb_sz);
Chris Wilson05235c52016-07-20 09:21:08 +0100615
Chris Wilsonf73e7392016-12-18 15:37:24 +0000616 ret = engine->request_alloc(req);
Chris Wilson05235c52016-07-20 09:21:08 +0100617 if (ret)
618 goto err_ctx;
619
Chris Wilsond0454462016-08-15 10:48:40 +0100620 /* Record the position of the start of the request so that
621 * should we detect the updated seqno part-way through the
622 * GPU processing the request, we never over-estimate the
623 * position of the head.
624 */
625 req->head = req->ring->tail;
626
Chris Wilson9b6586a2017-02-23 07:44:08 +0000627 /* Check that we didn't interrupt ourselves with a new request */
628 GEM_BUG_ON(req->timeline->seqno != req->fence.seqno);
Chris Wilson8e637172016-08-02 22:50:26 +0100629 return req;
Chris Wilson05235c52016-07-20 09:21:08 +0100630
631err_ctx:
Chris Wilson1618bdb2016-11-25 13:17:16 +0000632 /* Make sure we didn't add ourselves to external state before freeing */
633 GEM_BUG_ON(!list_empty(&req->active_list));
634 GEM_BUG_ON(!list_empty(&req->priotree.signalers_list));
635 GEM_BUG_ON(!list_empty(&req->priotree.waiters_list));
636
Chris Wilson05235c52016-07-20 09:21:08 +0100637 kmem_cache_free(dev_priv->requests, req);
Chris Wilson28176ef2016-10-28 13:58:56 +0100638err_unreserve:
Chris Wilson9b6586a2017-02-23 07:44:08 +0000639 unreserve_seqno(engine);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000640err_unpin:
641 engine->context_unpin(engine, ctx);
Chris Wilson8e637172016-08-02 22:50:26 +0100642 return ERR_PTR(ret);
Chris Wilson05235c52016-07-20 09:21:08 +0100643}
644
Chris Wilsona2bc4692016-09-09 14:11:56 +0100645static int
646i915_gem_request_await_request(struct drm_i915_gem_request *to,
647 struct drm_i915_gem_request *from)
648{
Chris Wilson754c9fd2017-02-23 07:44:14 +0000649 u32 seqno;
Chris Wilson85e17f52016-10-28 13:58:53 +0100650 int ret;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100651
652 GEM_BUG_ON(to == from);
653
Chris Wilson52e54202016-11-14 20:41:02 +0000654 if (to->engine->schedule) {
655 ret = i915_priotree_add_dependency(to->i915,
656 &to->priotree,
657 &from->priotree);
658 if (ret < 0)
659 return ret;
660 }
661
Chris Wilson73cb9702016-10-28 13:58:46 +0100662 if (to->timeline == from->timeline)
Chris Wilsona2bc4692016-09-09 14:11:56 +0100663 return 0;
664
Chris Wilson73cb9702016-10-28 13:58:46 +0100665 if (to->engine == from->engine) {
666 ret = i915_sw_fence_await_sw_fence_gfp(&to->submit,
667 &from->submit,
668 GFP_KERNEL);
669 return ret < 0 ? ret : 0;
670 }
671
Chris Wilson754c9fd2017-02-23 07:44:14 +0000672 seqno = i915_gem_request_global_seqno(from);
673 if (!seqno) {
Chris Wilson65e47602016-10-28 13:58:49 +0100674 ret = i915_sw_fence_await_dma_fence(&to->submit,
675 &from->fence, 0,
676 GFP_KERNEL);
677 return ret < 0 ? ret : 0;
678 }
679
Chris Wilson754c9fd2017-02-23 07:44:14 +0000680 if (seqno <= to->timeline->sync_seqno[from->engine->id])
Chris Wilsona2bc4692016-09-09 14:11:56 +0100681 return 0;
682
683 trace_i915_gem_ring_sync_to(to, from);
684 if (!i915.semaphores) {
Chris Wilson0a046a02016-09-09 14:12:00 +0100685 if (!i915_spin_request(from, TASK_INTERRUPTIBLE, 2)) {
686 ret = i915_sw_fence_await_dma_fence(&to->submit,
687 &from->fence, 0,
688 GFP_KERNEL);
689 if (ret < 0)
690 return ret;
691 }
Chris Wilsona2bc4692016-09-09 14:11:56 +0100692 } else {
693 ret = to->engine->semaphore.sync_to(to, from);
694 if (ret)
695 return ret;
696 }
697
Chris Wilson754c9fd2017-02-23 07:44:14 +0000698 to->timeline->sync_seqno[from->engine->id] = seqno;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100699 return 0;
700}
701
Chris Wilsonb52992c2016-10-28 13:58:24 +0100702int
703i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
704 struct dma_fence *fence)
705{
706 struct dma_fence_array *array;
707 int ret;
708 int i;
709
710 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
711 return 0;
712
713 if (dma_fence_is_i915(fence))
714 return i915_gem_request_await_request(req, to_request(fence));
715
716 if (!dma_fence_is_array(fence)) {
717 ret = i915_sw_fence_await_dma_fence(&req->submit,
718 fence, I915_FENCE_TIMEOUT,
719 GFP_KERNEL);
720 return ret < 0 ? ret : 0;
721 }
722
723 /* Note that if the fence-array was created in signal-on-any mode,
724 * we should *not* decompose it into its individual fences. However,
725 * we don't currently store which mode the fence-array is operating
726 * in. Fortunately, the only user of signal-on-any is private to
727 * amdgpu and we should not see any incoming fence-array from
728 * sync-file being in signal-on-any mode.
729 */
730
731 array = to_dma_fence_array(fence);
732 for (i = 0; i < array->num_fences; i++) {
733 struct dma_fence *child = array->fences[i];
734
735 if (dma_fence_is_i915(child))
736 ret = i915_gem_request_await_request(req,
737 to_request(child));
738 else
739 ret = i915_sw_fence_await_dma_fence(&req->submit,
740 child, I915_FENCE_TIMEOUT,
741 GFP_KERNEL);
742 if (ret < 0)
743 return ret;
744 }
745
746 return 0;
747}
748
Chris Wilsona2bc4692016-09-09 14:11:56 +0100749/**
750 * i915_gem_request_await_object - set this request to (async) wait upon a bo
751 *
752 * @to: request we are wishing to use
753 * @obj: object which may be in use on another ring.
754 *
755 * This code is meant to abstract object synchronization with the GPU.
756 * Conceptually we serialise writes between engines inside the GPU.
757 * We only allow one engine to write into a buffer at any time, but
758 * multiple readers. To ensure each has a coherent view of memory, we must:
759 *
760 * - If there is an outstanding write request to the object, the new
761 * request must wait for it to complete (either CPU or in hw, requests
762 * on the same ring will be naturally ordered).
763 *
764 * - If we are a write request (pending_write_domain is set), the new
765 * request must wait for outstanding read requests to complete.
766 *
767 * Returns 0 if successful, else propagates up the lower layer error.
768 */
769int
770i915_gem_request_await_object(struct drm_i915_gem_request *to,
771 struct drm_i915_gem_object *obj,
772 bool write)
773{
Chris Wilsond07f0e52016-10-28 13:58:44 +0100774 struct dma_fence *excl;
775 int ret = 0;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100776
777 if (write) {
Chris Wilsond07f0e52016-10-28 13:58:44 +0100778 struct dma_fence **shared;
779 unsigned int count, i;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100780
Chris Wilsond07f0e52016-10-28 13:58:44 +0100781 ret = reservation_object_get_fences_rcu(obj->resv,
782 &excl, &count, &shared);
Chris Wilsona2bc4692016-09-09 14:11:56 +0100783 if (ret)
784 return ret;
Chris Wilsond07f0e52016-10-28 13:58:44 +0100785
786 for (i = 0; i < count; i++) {
787 ret = i915_gem_request_await_dma_fence(to, shared[i]);
788 if (ret)
789 break;
790
791 dma_fence_put(shared[i]);
792 }
793
794 for (; i < count; i++)
795 dma_fence_put(shared[i]);
796 kfree(shared);
797 } else {
798 excl = reservation_object_get_excl_rcu(obj->resv);
Chris Wilsona2bc4692016-09-09 14:11:56 +0100799 }
800
Chris Wilsond07f0e52016-10-28 13:58:44 +0100801 if (excl) {
802 if (ret == 0)
803 ret = i915_gem_request_await_dma_fence(to, excl);
804
805 dma_fence_put(excl);
806 }
807
808 return ret;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100809}
810
Chris Wilson05235c52016-07-20 09:21:08 +0100811static void i915_gem_mark_busy(const struct intel_engine_cs *engine)
812{
813 struct drm_i915_private *dev_priv = engine->i915;
814
Chris Wilson05235c52016-07-20 09:21:08 +0100815 if (dev_priv->gt.awake)
816 return;
817
Chris Wilson43020552016-11-15 16:46:20 +0000818 GEM_BUG_ON(!dev_priv->gt.active_requests);
819
Chris Wilson05235c52016-07-20 09:21:08 +0100820 intel_runtime_pm_get_noresume(dev_priv);
821 dev_priv->gt.awake = true;
822
Chris Wilson54b4f682016-07-21 21:16:19 +0100823 intel_enable_gt_powersave(dev_priv);
Chris Wilson05235c52016-07-20 09:21:08 +0100824 i915_update_gfx_val(dev_priv);
825 if (INTEL_GEN(dev_priv) >= 6)
826 gen6_rps_busy(dev_priv);
827
828 queue_delayed_work(dev_priv->wq,
829 &dev_priv->gt.retire_work,
830 round_jiffies_up_relative(HZ));
831}
832
833/*
834 * NB: This function is not allowed to fail. Doing so would mean the the
835 * request is not being tracked for completion but the work itself is
836 * going to happen on the hardware. This would be a Bad Thing(tm).
837 */
Chris Wilson17f298cf2016-08-10 13:41:46 +0100838void __i915_add_request(struct drm_i915_gem_request *request, bool flush_caches)
Chris Wilson05235c52016-07-20 09:21:08 +0100839{
Chris Wilson95b2ab52016-08-15 10:48:46 +0100840 struct intel_engine_cs *engine = request->engine;
841 struct intel_ring *ring = request->ring;
Chris Wilson73cb9702016-10-28 13:58:46 +0100842 struct intel_timeline *timeline = request->timeline;
Chris Wilson0a046a02016-09-09 14:12:00 +0100843 struct drm_i915_gem_request *prev;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000844 u32 *cs;
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100845 int err;
Chris Wilson05235c52016-07-20 09:21:08 +0100846
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100847 lockdep_assert_held(&request->i915->drm.struct_mutex);
Chris Wilson0f25dff2016-09-09 14:11:55 +0100848 trace_i915_gem_request_add(request);
849
Chris Wilsonc781c972017-01-11 14:08:58 +0000850 /* Make sure that no request gazumped us - if it was allocated after
851 * our i915_gem_request_alloc() and called __i915_add_request() before
852 * us, the timeline will hold its seqno which is later than ours.
853 */
Chris Wilson9b6586a2017-02-23 07:44:08 +0000854 GEM_BUG_ON(timeline->seqno != request->fence.seqno);
Chris Wilsonc781c972017-01-11 14:08:58 +0000855
Chris Wilson05235c52016-07-20 09:21:08 +0100856 /*
857 * To ensure that this call will not fail, space for its emissions
858 * should already have been reserved in the ring buffer. Let the ring
859 * know that it is time to use that space up.
860 */
Chris Wilson05235c52016-07-20 09:21:08 +0100861 request->reserved_space = 0;
862
863 /*
864 * Emit any outstanding flushes - execbuf can fail to emit the flush
865 * after having emitted the batchbuffer command. Hence we need to fix
866 * things up similar to emitting the lazy request. The difference here
867 * is that the flush _must_ happen before the next request, no matter
868 * what.
869 */
870 if (flush_caches) {
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100871 err = engine->emit_flush(request, EMIT_FLUSH);
Chris Wilsonc7fe7d22016-08-02 22:50:24 +0100872
Chris Wilson05235c52016-07-20 09:21:08 +0100873 /* Not allowed to fail! */
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100874 WARN(err, "engine->emit_flush() failed: %d!\n", err);
Chris Wilson05235c52016-07-20 09:21:08 +0100875 }
876
Chris Wilsond0454462016-08-15 10:48:40 +0100877 /* Record the position of the start of the breadcrumb so that
Chris Wilson05235c52016-07-20 09:21:08 +0100878 * should we detect the updated seqno part-way through the
879 * GPU processing the request, we never over-estimate the
Chris Wilsond0454462016-08-15 10:48:40 +0100880 * position of the ring's HEAD.
Chris Wilson05235c52016-07-20 09:21:08 +0100881 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000882 cs = intel_ring_begin(request, engine->emit_breadcrumb_sz);
883 GEM_BUG_ON(IS_ERR(cs));
884 request->postfix = intel_ring_offset(request, cs);
Chris Wilson05235c52016-07-20 09:21:08 +0100885
Chris Wilson0f25dff2016-09-09 14:11:55 +0100886 /* Seal the request and mark it as pending execution. Note that
887 * we may inspect this state, without holding any locks, during
888 * hangcheck. Hence we apply the barrier to ensure that we do not
889 * see a more recent value in the hws than we are tracking.
890 */
Chris Wilson0a046a02016-09-09 14:12:00 +0100891
Chris Wilson73cb9702016-10-28 13:58:46 +0100892 prev = i915_gem_active_raw(&timeline->last_request,
Chris Wilson0a046a02016-09-09 14:12:00 +0100893 &request->i915->drm.struct_mutex);
Chris Wilson52e54202016-11-14 20:41:02 +0000894 if (prev) {
Chris Wilson0a046a02016-09-09 14:12:00 +0100895 i915_sw_fence_await_sw_fence(&request->submit, &prev->submit,
896 &request->submitq);
Chris Wilson52e54202016-11-14 20:41:02 +0000897 if (engine->schedule)
898 __i915_priotree_add_dependency(&request->priotree,
899 &prev->priotree,
900 &request->dep,
901 0);
902 }
Chris Wilson0a046a02016-09-09 14:12:00 +0100903
Chris Wilson80b204b2016-10-28 13:58:58 +0100904 spin_lock_irq(&timeline->lock);
Chris Wilsonf2d13292016-10-28 13:58:57 +0100905 list_add_tail(&request->link, &timeline->requests);
Chris Wilson80b204b2016-10-28 13:58:58 +0100906 spin_unlock_irq(&timeline->lock);
Chris Wilson28176ef2016-10-28 13:58:56 +0100907
Chris Wilson9b6586a2017-02-23 07:44:08 +0000908 GEM_BUG_ON(timeline->seqno != request->fence.seqno);
Chris Wilson73cb9702016-10-28 13:58:46 +0100909 i915_gem_active_set(&timeline->last_request, request);
Chris Wilsonf2d13292016-10-28 13:58:57 +0100910
Chris Wilson0f25dff2016-09-09 14:11:55 +0100911 list_add_tail(&request->ring_link, &ring->request_list);
Chris Wilsonf2d13292016-10-28 13:58:57 +0100912 request->emitted_jiffies = jiffies;
Chris Wilson0f25dff2016-09-09 14:11:55 +0100913
Chris Wilson9b6586a2017-02-23 07:44:08 +0000914 if (!request->i915->gt.active_requests++)
915 i915_gem_mark_busy(engine);
Chris Wilson5590af32016-09-09 14:11:54 +0100916
Chris Wilson0de91362016-11-14 20:41:01 +0000917 /* Let the backend know a new request has arrived that may need
918 * to adjust the existing execution schedule due to a high priority
919 * request - i.e. we may want to preempt the current request in order
920 * to run a high priority dependency chain *before* we can execute this
921 * request.
922 *
923 * This is called before the request is ready to run so that we can
924 * decide whether to preempt the entire chain so that it is ready to
925 * run at the earliest possible convenience.
926 */
927 if (engine->schedule)
Chris Wilson9f792eb2016-11-14 20:41:04 +0000928 engine->schedule(request, request->ctx->priority);
Chris Wilson0de91362016-11-14 20:41:01 +0000929
Chris Wilson5590af32016-09-09 14:11:54 +0100930 local_bh_disable();
931 i915_sw_fence_commit(&request->submit);
932 local_bh_enable(); /* Kick the execlists tasklet if just scheduled */
Chris Wilson05235c52016-07-20 09:21:08 +0100933}
934
935static unsigned long local_clock_us(unsigned int *cpu)
936{
937 unsigned long t;
938
939 /* Cheaply and approximately convert from nanoseconds to microseconds.
940 * The result and subsequent calculations are also defined in the same
941 * approximate microseconds units. The principal source of timing
942 * error here is from the simple truncation.
943 *
944 * Note that local_clock() is only defined wrt to the current CPU;
945 * the comparisons are no longer valid if we switch CPUs. Instead of
946 * blocking preemption for the entire busywait, we can detect the CPU
947 * switch and use that as indicator of system load and a reason to
948 * stop busywaiting, see busywait_stop().
949 */
950 *cpu = get_cpu();
951 t = local_clock() >> 10;
952 put_cpu();
953
954 return t;
955}
956
957static bool busywait_stop(unsigned long timeout, unsigned int cpu)
958{
959 unsigned int this_cpu;
960
961 if (time_after(local_clock_us(&this_cpu), timeout))
962 return true;
963
964 return this_cpu != cpu;
965}
966
967bool __i915_spin_request(const struct drm_i915_gem_request *req,
Chris Wilson754c9fd2017-02-23 07:44:14 +0000968 u32 seqno, int state, unsigned long timeout_us)
Chris Wilson05235c52016-07-20 09:21:08 +0100969{
Chris Wilsonc33ed062017-02-17 15:13:01 +0000970 struct intel_engine_cs *engine = req->engine;
971 unsigned int irq, cpu;
Chris Wilson05235c52016-07-20 09:21:08 +0100972
973 /* When waiting for high frequency requests, e.g. during synchronous
974 * rendering split between the CPU and GPU, the finite amount of time
975 * required to set up the irq and wait upon it limits the response
976 * rate. By busywaiting on the request completion for a short while we
977 * can service the high frequency waits as quick as possible. However,
978 * if it is a slow request, we want to sleep as quickly as possible.
979 * The tradeoff between waiting and sleeping is roughly the time it
980 * takes to sleep on a request, on the order of a microsecond.
981 */
982
Chris Wilsonc33ed062017-02-17 15:13:01 +0000983 irq = atomic_read(&engine->irq_count);
Chris Wilson05235c52016-07-20 09:21:08 +0100984 timeout_us += local_clock_us(&cpu);
985 do {
Chris Wilson754c9fd2017-02-23 07:44:14 +0000986 if (seqno != i915_gem_request_global_seqno(req))
987 break;
988
989 if (i915_seqno_passed(intel_engine_get_seqno(req->engine),
990 seqno))
Chris Wilson05235c52016-07-20 09:21:08 +0100991 return true;
992
Chris Wilsonc33ed062017-02-17 15:13:01 +0000993 /* Seqno are meant to be ordered *before* the interrupt. If
994 * we see an interrupt without a corresponding seqno advance,
995 * assume we won't see one in the near future but require
996 * the engine->seqno_barrier() to fixup coherency.
997 */
998 if (atomic_read(&engine->irq_count) != irq)
999 break;
1000
Chris Wilson05235c52016-07-20 09:21:08 +01001001 if (signal_pending_state(state, current))
1002 break;
1003
1004 if (busywait_stop(timeout_us, cpu))
1005 break;
1006
Christian Borntraegerf2f09a42016-10-25 11:03:14 +02001007 cpu_relax();
Chris Wilson05235c52016-07-20 09:21:08 +01001008 } while (!need_resched());
1009
1010 return false;
1011}
1012
Chris Wilsone0705112017-02-23 07:44:20 +00001013static bool __i915_wait_request_check_and_reset(struct drm_i915_gem_request *request)
Chris Wilson4680816b2016-10-28 13:58:48 +01001014{
Chris Wilson8c185ec2017-03-16 17:13:02 +00001015 if (likely(!i915_reset_handoff(&request->i915->gpu_error)))
Chris Wilsone0705112017-02-23 07:44:20 +00001016 return false;
Chris Wilson4680816b2016-10-28 13:58:48 +01001017
Chris Wilsone0705112017-02-23 07:44:20 +00001018 __set_current_state(TASK_RUNNING);
1019 i915_reset(request->i915);
1020 return true;
Chris Wilson4680816b2016-10-28 13:58:48 +01001021}
1022
Chris Wilson05235c52016-07-20 09:21:08 +01001023/**
Chris Wilson776f3232016-08-04 07:52:40 +01001024 * i915_wait_request - wait until execution of request has finished
Chris Wilsone95433c2016-10-28 13:58:27 +01001025 * @req: the request to wait upon
Chris Wilsonea746f32016-09-09 14:11:49 +01001026 * @flags: how to wait
Chris Wilsone95433c2016-10-28 13:58:27 +01001027 * @timeout: how long to wait in jiffies
Chris Wilson05235c52016-07-20 09:21:08 +01001028 *
Chris Wilsone95433c2016-10-28 13:58:27 +01001029 * i915_wait_request() waits for the request to be completed, for a
1030 * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
1031 * unbounded wait).
Chris Wilson05235c52016-07-20 09:21:08 +01001032 *
Chris Wilsone95433c2016-10-28 13:58:27 +01001033 * If the caller holds the struct_mutex, the caller must pass I915_WAIT_LOCKED
1034 * in via the flags, and vice versa if the struct_mutex is not held, the caller
1035 * must not specify that the wait is locked.
1036 *
1037 * Returns the remaining time (in jiffies) if the request completed, which may
1038 * be zero or -ETIME if the request is unfinished after the timeout expires.
1039 * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
1040 * pending before the request completes.
Chris Wilson05235c52016-07-20 09:21:08 +01001041 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001042long i915_wait_request(struct drm_i915_gem_request *req,
1043 unsigned int flags,
1044 long timeout)
Chris Wilson05235c52016-07-20 09:21:08 +01001045{
Chris Wilsonea746f32016-09-09 14:11:49 +01001046 const int state = flags & I915_WAIT_INTERRUPTIBLE ?
1047 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
Chris Wilson4b36b2e2017-02-23 07:44:10 +00001048 wait_queue_head_t *errq = &req->i915->gpu_error.wait_queue;
Chris Wilsona49625f2017-02-23 07:44:19 +00001049 DEFINE_WAIT_FUNC(reset, default_wake_function);
1050 DEFINE_WAIT_FUNC(exec, default_wake_function);
Chris Wilson05235c52016-07-20 09:21:08 +01001051 struct intel_wait wait;
Chris Wilson05235c52016-07-20 09:21:08 +01001052
1053 might_sleep();
Chris Wilson22dd3bb2016-09-09 14:11:50 +01001054#if IS_ENABLED(CONFIG_LOCKDEP)
Chris Wilsone95433c2016-10-28 13:58:27 +01001055 GEM_BUG_ON(debug_locks &&
1056 !!lockdep_is_held(&req->i915->drm.struct_mutex) !=
Chris Wilson22dd3bb2016-09-09 14:11:50 +01001057 !!(flags & I915_WAIT_LOCKED));
1058#endif
Chris Wilsone95433c2016-10-28 13:58:27 +01001059 GEM_BUG_ON(timeout < 0);
Chris Wilson05235c52016-07-20 09:21:08 +01001060
Chris Wilson05235c52016-07-20 09:21:08 +01001061 if (i915_gem_request_completed(req))
Chris Wilsone95433c2016-10-28 13:58:27 +01001062 return timeout;
Chris Wilson05235c52016-07-20 09:21:08 +01001063
Chris Wilsone95433c2016-10-28 13:58:27 +01001064 if (!timeout)
1065 return -ETIME;
Chris Wilson05235c52016-07-20 09:21:08 +01001066
Tvrtko Ursulin936925022017-02-21 11:00:24 +00001067 trace_i915_gem_request_wait_begin(req, flags);
Chris Wilson05235c52016-07-20 09:21:08 +01001068
Chris Wilsona49625f2017-02-23 07:44:19 +00001069 add_wait_queue(&req->execute, &exec);
Chris Wilson7de53bf2017-02-23 07:44:11 +00001070 if (flags & I915_WAIT_LOCKED)
1071 add_wait_queue(errq, &reset);
1072
Chris Wilson56299fb2017-02-27 20:58:48 +00001073 intel_wait_init(&wait, req);
Chris Wilson754c9fd2017-02-23 07:44:14 +00001074
Chris Wilsond6a22892017-02-23 07:44:17 +00001075restart:
Chris Wilson0f2f61d2017-02-23 07:44:22 +00001076 do {
1077 set_current_state(state);
1078 if (intel_wait_update_request(&wait, req))
1079 break;
Chris Wilson754c9fd2017-02-23 07:44:14 +00001080
Chris Wilson0f2f61d2017-02-23 07:44:22 +00001081 if (flags & I915_WAIT_LOCKED &&
1082 __i915_wait_request_check_and_reset(req))
1083 continue;
Chris Wilson541ca6e2017-02-23 07:44:12 +00001084
Chris Wilson0f2f61d2017-02-23 07:44:22 +00001085 if (signal_pending_state(state, current)) {
1086 timeout = -ERESTARTSYS;
Chris Wilson4680816b2016-10-28 13:58:48 +01001087 goto complete;
Chris Wilson0f2f61d2017-02-23 07:44:22 +00001088 }
Chris Wilson4680816b2016-10-28 13:58:48 +01001089
Chris Wilson0f2f61d2017-02-23 07:44:22 +00001090 if (!timeout) {
1091 timeout = -ETIME;
1092 goto complete;
1093 }
Chris Wilson541ca6e2017-02-23 07:44:12 +00001094
Chris Wilson0f2f61d2017-02-23 07:44:22 +00001095 timeout = io_schedule_timeout(timeout);
1096 } while (1);
Chris Wilson541ca6e2017-02-23 07:44:12 +00001097
Chris Wilson0f2f61d2017-02-23 07:44:22 +00001098 GEM_BUG_ON(!intel_wait_has_seqno(&wait));
Chris Wilsonfe497892017-02-23 07:44:13 +00001099 GEM_BUG_ON(!i915_sw_fence_signaled(&req->submit));
Chris Wilson4680816b2016-10-28 13:58:48 +01001100
Daniel Vetter437c3082016-08-05 18:11:24 +02001101 /* Optimistic short spin before touching IRQs */
Chris Wilson05235c52016-07-20 09:21:08 +01001102 if (i915_spin_request(req, state, 5))
1103 goto complete;
1104
1105 set_current_state(state);
Chris Wilson05235c52016-07-20 09:21:08 +01001106 if (intel_engine_add_wait(req->engine, &wait))
1107 /* In order to check that we haven't missed the interrupt
1108 * as we enabled it, we need to kick ourselves to do a
1109 * coherent check on the seqno before we sleep.
1110 */
1111 goto wakeup;
1112
Chris Wilson24f417e2017-02-23 07:44:21 +00001113 if (flags & I915_WAIT_LOCKED)
1114 __i915_wait_request_check_and_reset(req);
1115
Chris Wilson05235c52016-07-20 09:21:08 +01001116 for (;;) {
1117 if (signal_pending_state(state, current)) {
Chris Wilsone95433c2016-10-28 13:58:27 +01001118 timeout = -ERESTARTSYS;
Chris Wilson05235c52016-07-20 09:21:08 +01001119 break;
1120 }
1121
Chris Wilsone95433c2016-10-28 13:58:27 +01001122 if (!timeout) {
1123 timeout = -ETIME;
Chris Wilson05235c52016-07-20 09:21:08 +01001124 break;
1125 }
1126
Chris Wilsone95433c2016-10-28 13:58:27 +01001127 timeout = io_schedule_timeout(timeout);
1128
Chris Wilson754c9fd2017-02-23 07:44:14 +00001129 if (intel_wait_complete(&wait) &&
1130 intel_wait_check_request(&wait, req))
Chris Wilson05235c52016-07-20 09:21:08 +01001131 break;
1132
1133 set_current_state(state);
1134
1135wakeup:
1136 /* Carefully check if the request is complete, giving time
1137 * for the seqno to be visible following the interrupt.
1138 * We also have to check in case we are kicked by the GPU
1139 * reset in order to drop the struct_mutex.
1140 */
1141 if (__i915_request_irq_complete(req))
1142 break;
1143
Chris Wilson221fe792016-09-09 14:11:51 +01001144 /* If the GPU is hung, and we hold the lock, reset the GPU
1145 * and then check for completion. On a full reset, the engine's
1146 * HW seqno will be advanced passed us and we are complete.
1147 * If we do a partial reset, we have to wait for the GPU to
1148 * resume and update the breadcrumb.
1149 *
1150 * If we don't hold the mutex, we can just wait for the worker
1151 * to come along and update the breadcrumb (either directly
1152 * itself, or indirectly by recovering the GPU).
1153 */
1154 if (flags & I915_WAIT_LOCKED &&
Chris Wilsone0705112017-02-23 07:44:20 +00001155 __i915_wait_request_check_and_reset(req))
Chris Wilson221fe792016-09-09 14:11:51 +01001156 continue;
Chris Wilson221fe792016-09-09 14:11:51 +01001157
Chris Wilson05235c52016-07-20 09:21:08 +01001158 /* Only spin if we know the GPU is processing this request */
1159 if (i915_spin_request(req, state, 2))
1160 break;
Chris Wilsond6a22892017-02-23 07:44:17 +00001161
1162 if (!intel_wait_check_request(&wait, req)) {
1163 intel_engine_remove_wait(req->engine, &wait);
1164 goto restart;
1165 }
Chris Wilson05235c52016-07-20 09:21:08 +01001166 }
Chris Wilson05235c52016-07-20 09:21:08 +01001167
1168 intel_engine_remove_wait(req->engine, &wait);
Chris Wilson05235c52016-07-20 09:21:08 +01001169complete:
Chris Wilsona49625f2017-02-23 07:44:19 +00001170 __set_current_state(TASK_RUNNING);
Chris Wilson7de53bf2017-02-23 07:44:11 +00001171 if (flags & I915_WAIT_LOCKED)
1172 remove_wait_queue(errq, &reset);
Chris Wilsona49625f2017-02-23 07:44:19 +00001173 remove_wait_queue(&req->execute, &exec);
Chris Wilson05235c52016-07-20 09:21:08 +01001174 trace_i915_gem_request_wait_end(req);
1175
Chris Wilsone95433c2016-10-28 13:58:27 +01001176 return timeout;
Chris Wilson05235c52016-07-20 09:21:08 +01001177}
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001178
Chris Wilson28176ef2016-10-28 13:58:56 +01001179static void engine_retire_requests(struct intel_engine_cs *engine)
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001180{
1181 struct drm_i915_gem_request *request, *next;
Chris Wilson754c9fd2017-02-23 07:44:14 +00001182 u32 seqno = intel_engine_get_seqno(engine);
1183 LIST_HEAD(retire);
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001184
Chris Wilson754c9fd2017-02-23 07:44:14 +00001185 spin_lock_irq(&engine->timeline->lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01001186 list_for_each_entry_safe(request, next,
1187 &engine->timeline->requests, link) {
Chris Wilson754c9fd2017-02-23 07:44:14 +00001188 if (!i915_seqno_passed(seqno, request->global_seqno))
1189 break;
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001190
Chris Wilson754c9fd2017-02-23 07:44:14 +00001191 list_move_tail(&request->link, &retire);
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001192 }
Chris Wilson754c9fd2017-02-23 07:44:14 +00001193 spin_unlock_irq(&engine->timeline->lock);
1194
1195 list_for_each_entry_safe(request, next, &retire, link)
1196 i915_gem_request_retire(request);
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001197}
1198
1199void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
1200{
1201 struct intel_engine_cs *engine;
Chris Wilson28176ef2016-10-28 13:58:56 +01001202 enum intel_engine_id id;
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001203
1204 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1205
Chris Wilson28176ef2016-10-28 13:58:56 +01001206 if (!dev_priv->gt.active_requests)
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001207 return;
1208
Chris Wilson28176ef2016-10-28 13:58:56 +01001209 for_each_engine(engine, dev_priv, id)
1210 engine_retire_requests(engine);
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001211}
Chris Wilsonc835c552017-02-13 17:15:21 +00001212
1213#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1214#include "selftests/mock_request.c"
1215#include "selftests/i915_gem_request.c"
1216#endif