blob: df3fef393dbe00ea8fdc03603eaed49c77fb9d80 [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>
Chris Wilsonfa545cb2016-08-04 07:52:35 +010029
Chris Wilson05235c52016-07-20 09:21:08 +010030#include "i915_drv.h"
31
Chris Wilsonf54d1862016-10-25 13:00:45 +010032static const char *i915_fence_get_driver_name(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010033{
34 return "i915";
35}
36
Chris Wilsonf54d1862016-10-25 13:00:45 +010037static const char *i915_fence_get_timeline_name(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010038{
Chris Wilson73cb9702016-10-28 13:58:46 +010039 return to_request(fence)->timeline->common->name;
Chris Wilson04769652016-07-20 09:21:11 +010040}
41
Chris Wilsonf54d1862016-10-25 13:00:45 +010042static bool i915_fence_signaled(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010043{
44 return i915_gem_request_completed(to_request(fence));
45}
46
Chris Wilsonf54d1862016-10-25 13:00:45 +010047static bool i915_fence_enable_signaling(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010048{
49 if (i915_fence_signaled(fence))
50 return false;
51
52 intel_engine_enable_signaling(to_request(fence));
53 return true;
54}
55
Chris Wilsonf54d1862016-10-25 13:00:45 +010056static signed long i915_fence_wait(struct dma_fence *fence,
Chris Wilson04769652016-07-20 09:21:11 +010057 bool interruptible,
Chris Wilsone95433c2016-10-28 13:58:27 +010058 signed long timeout)
Chris Wilson04769652016-07-20 09:21:11 +010059{
Chris Wilsone95433c2016-10-28 13:58:27 +010060 return i915_wait_request(to_request(fence), interruptible, timeout);
Chris Wilson04769652016-07-20 09:21:11 +010061}
62
Chris Wilsonf54d1862016-10-25 13:00:45 +010063static void i915_fence_release(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010064{
65 struct drm_i915_gem_request *req = to_request(fence);
66
Chris Wilsonfc158402016-11-25 13:17:18 +000067 /* The request is put onto a RCU freelist (i.e. the address
68 * is immediately reused), mark the fences as being freed now.
69 * Otherwise the debugobjects for the fences are only marked as
70 * freed when the slab cache itself is freed, and so we would get
71 * caught trying to reuse dead objects.
72 */
73 i915_sw_fence_fini(&req->submit);
74 i915_sw_fence_fini(&req->execute);
75
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 +010088int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
89 struct drm_file *file)
90{
91 struct drm_i915_private *dev_private;
92 struct drm_i915_file_private *file_priv;
93
94 WARN_ON(!req || !file || req->file_priv);
95
96 if (!req || !file)
97 return -EINVAL;
98
99 if (req->file_priv)
100 return -EINVAL;
101
102 dev_private = req->i915;
103 file_priv = file->driver_priv;
104
105 spin_lock(&file_priv->mm.lock);
106 req->file_priv = file_priv;
107 list_add_tail(&req->client_list, &file_priv->mm.request_list);
108 spin_unlock(&file_priv->mm.lock);
109
Chris Wilson05235c52016-07-20 09:21:08 +0100110 return 0;
111}
112
113static inline void
114i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
115{
116 struct drm_i915_file_private *file_priv = request->file_priv;
117
118 if (!file_priv)
119 return;
120
121 spin_lock(&file_priv->mm.lock);
122 list_del(&request->client_list);
123 request->file_priv = NULL;
124 spin_unlock(&file_priv->mm.lock);
Chris Wilson05235c52016-07-20 09:21:08 +0100125}
126
Chris Wilson52e54202016-11-14 20:41:02 +0000127static struct i915_dependency *
128i915_dependency_alloc(struct drm_i915_private *i915)
129{
130 return kmem_cache_alloc(i915->dependencies, GFP_KERNEL);
131}
132
133static void
134i915_dependency_free(struct drm_i915_private *i915,
135 struct i915_dependency *dep)
136{
137 kmem_cache_free(i915->dependencies, dep);
138}
139
140static void
141__i915_priotree_add_dependency(struct i915_priotree *pt,
142 struct i915_priotree *signal,
143 struct i915_dependency *dep,
144 unsigned long flags)
145{
Chris Wilson20311bd2016-11-14 20:41:03 +0000146 INIT_LIST_HEAD(&dep->dfs_link);
Chris Wilson52e54202016-11-14 20:41:02 +0000147 list_add(&dep->wait_link, &signal->waiters_list);
148 list_add(&dep->signal_link, &pt->signalers_list);
149 dep->signaler = signal;
150 dep->flags = flags;
151}
152
153static int
154i915_priotree_add_dependency(struct drm_i915_private *i915,
155 struct i915_priotree *pt,
156 struct i915_priotree *signal)
157{
158 struct i915_dependency *dep;
159
160 dep = i915_dependency_alloc(i915);
161 if (!dep)
162 return -ENOMEM;
163
164 __i915_priotree_add_dependency(pt, signal, dep, I915_DEPENDENCY_ALLOC);
165 return 0;
166}
167
168static void
169i915_priotree_fini(struct drm_i915_private *i915, struct i915_priotree *pt)
170{
171 struct i915_dependency *dep, *next;
172
Chris Wilson20311bd2016-11-14 20:41:03 +0000173 GEM_BUG_ON(!RB_EMPTY_NODE(&pt->node));
174
Chris Wilson52e54202016-11-14 20:41:02 +0000175 /* Everyone we depended upon (the fences we wait to be signaled)
176 * should retire before us and remove themselves from our list.
177 * However, retirement is run independently on each timeline and
178 * so we may be called out-of-order.
179 */
180 list_for_each_entry_safe(dep, next, &pt->signalers_list, signal_link) {
181 list_del(&dep->wait_link);
182 if (dep->flags & I915_DEPENDENCY_ALLOC)
183 i915_dependency_free(i915, dep);
184 }
185
186 /* Remove ourselves from everyone who depends upon us */
187 list_for_each_entry_safe(dep, next, &pt->waiters_list, wait_link) {
188 list_del(&dep->signal_link);
189 if (dep->flags & I915_DEPENDENCY_ALLOC)
190 i915_dependency_free(i915, dep);
191 }
192}
193
194static void
195i915_priotree_init(struct i915_priotree *pt)
196{
197 INIT_LIST_HEAD(&pt->signalers_list);
198 INIT_LIST_HEAD(&pt->waiters_list);
Chris Wilson20311bd2016-11-14 20:41:03 +0000199 RB_CLEAR_NODE(&pt->node);
200 pt->priority = INT_MIN;
Chris Wilson52e54202016-11-14 20:41:02 +0000201}
202
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100203void i915_gem_retire_noop(struct i915_gem_active *active,
204 struct drm_i915_gem_request *request)
205{
206 /* Space left intentionally blank */
207}
208
Chris Wilson05235c52016-07-20 09:21:08 +0100209static void i915_gem_request_retire(struct drm_i915_gem_request *request)
210{
Chris Wilsone8a9c582016-12-18 15:37:20 +0000211 struct intel_engine_cs *engine = request->engine;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100212 struct i915_gem_active *active, *next;
213
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100214 lockdep_assert_held(&request->i915->drm.struct_mutex);
Chris Wilson48bc2a42016-11-25 13:17:17 +0000215 GEM_BUG_ON(!i915_sw_fence_signaled(&request->submit));
216 GEM_BUG_ON(!i915_sw_fence_signaled(&request->execute));
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100217 GEM_BUG_ON(!i915_gem_request_completed(request));
Chris Wilson43020552016-11-15 16:46:20 +0000218 GEM_BUG_ON(!request->i915->gt.active_requests);
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100219
Chris Wilson05235c52016-07-20 09:21:08 +0100220 trace_i915_gem_request_retire(request);
Chris Wilson80b204b2016-10-28 13:58:58 +0100221
Chris Wilsone8a9c582016-12-18 15:37:20 +0000222 spin_lock_irq(&engine->timeline->lock);
Chris Wilsone95433c2016-10-28 13:58:27 +0100223 list_del_init(&request->link);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000224 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson05235c52016-07-20 09:21:08 +0100225
226 /* We know the GPU must have read the request to have
227 * sent us the seqno + interrupt, so use the position
228 * of tail of the request to update the last known position
229 * of the GPU head.
230 *
231 * Note this requires that we are always called in request
232 * completion order.
233 */
Chris Wilson675d9ad2016-08-04 07:52:36 +0100234 list_del(&request->ring_link);
Chris Wilson1dae2df2016-08-02 22:50:19 +0100235 request->ring->last_retired_head = request->postfix;
Chris Wilson43020552016-11-15 16:46:20 +0000236 if (!--request->i915->gt.active_requests) {
237 GEM_BUG_ON(!request->i915->gt.awake);
238 mod_delayed_work(request->i915->wq,
239 &request->i915->gt.idle_work,
240 msecs_to_jiffies(100));
241 }
Chris Wilson05235c52016-07-20 09:21:08 +0100242
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100243 /* Walk through the active list, calling retire on each. This allows
244 * objects to track their GPU activity and mark themselves as idle
245 * when their *last* active request is completed (updating state
246 * tracking lists for eviction, active references for GEM, etc).
247 *
248 * As the ->retire() may free the node, we decouple it first and
249 * pass along the auxiliary information (to avoid dereferencing
250 * the node after the callback).
251 */
252 list_for_each_entry_safe(active, next, &request->active_list, link) {
253 /* In microbenchmarks or focusing upon time inside the kernel,
254 * we may spend an inordinate amount of time simply handling
255 * the retirement of requests and processing their callbacks.
256 * Of which, this loop itself is particularly hot due to the
257 * cache misses when jumping around the list of i915_gem_active.
258 * So we try to keep this loop as streamlined as possible and
259 * also prefetch the next i915_gem_active to try and hide
260 * the likely cache miss.
261 */
262 prefetchw(next);
263
264 INIT_LIST_HEAD(&active->link);
Chris Wilson0eafec62016-08-04 16:32:41 +0100265 RCU_INIT_POINTER(active->request, NULL);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100266
267 active->retire(active, request);
268 }
269
Chris Wilson05235c52016-07-20 09:21:08 +0100270 i915_gem_request_remove_from_client(request);
271
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +0200272 /* Retirement decays the ban score as it is a sign of ctx progress */
Mika Kuoppalabc1d53c2016-11-16 17:20:34 +0200273 if (request->ctx->ban_score > 0)
274 request->ctx->ban_score--;
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +0200275
Chris Wilsone8a9c582016-12-18 15:37:20 +0000276 /* The backing object for the context is done after switching to the
277 * *next* context. Therefore we cannot retire the previous context until
278 * the next context has already started running. However, since we
279 * cannot take the required locks at i915_gem_request_submit() we
280 * defer the unpinning of the active context to now, retirement of
281 * the subsequent request.
282 */
283 if (engine->last_retired_context)
284 engine->context_unpin(engine, engine->last_retired_context);
285 engine->last_retired_context = request->ctx;
Chris Wilsond07f0e52016-10-28 13:58:44 +0100286
287 dma_fence_signal(&request->fence);
Chris Wilson52e54202016-11-14 20:41:02 +0000288
289 i915_priotree_fini(request->i915, &request->priotree);
Chris Wilsone8a261e2016-07-20 13:31:49 +0100290 i915_gem_request_put(request);
Chris Wilson05235c52016-07-20 09:21:08 +0100291}
292
293void i915_gem_request_retire_upto(struct drm_i915_gem_request *req)
294{
295 struct intel_engine_cs *engine = req->engine;
296 struct drm_i915_gem_request *tmp;
297
298 lockdep_assert_held(&req->i915->drm.struct_mutex);
Chris Wilson4ffd6e02016-11-25 13:17:15 +0000299 GEM_BUG_ON(!i915_gem_request_completed(req));
300
Chris Wilsone95433c2016-10-28 13:58:27 +0100301 if (list_empty(&req->link))
302 return;
Chris Wilson05235c52016-07-20 09:21:08 +0100303
304 do {
Chris Wilson73cb9702016-10-28 13:58:46 +0100305 tmp = list_first_entry(&engine->timeline->requests,
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100306 typeof(*tmp), link);
Chris Wilson05235c52016-07-20 09:21:08 +0100307
308 i915_gem_request_retire(tmp);
309 } while (tmp != req);
Chris Wilson05235c52016-07-20 09:21:08 +0100310}
311
Chris Wilson85e17f52016-10-28 13:58:53 +0100312static int i915_gem_init_global_seqno(struct drm_i915_private *i915, u32 seqno)
Chris Wilson05235c52016-07-20 09:21:08 +0100313{
Chris Wilson85e17f52016-10-28 13:58:53 +0100314 struct i915_gem_timeline *timeline = &i915->gt.global_timeline;
Chris Wilson05235c52016-07-20 09:21:08 +0100315 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +0530316 enum intel_engine_id id;
Chris Wilson05235c52016-07-20 09:21:08 +0100317 int ret;
318
319 /* Carefully retire all requests without writing to the rings */
Chris Wilson85e17f52016-10-28 13:58:53 +0100320 ret = i915_gem_wait_for_idle(i915,
Chris Wilson73cb9702016-10-28 13:58:46 +0100321 I915_WAIT_INTERRUPTIBLE |
322 I915_WAIT_LOCKED);
323 if (ret)
324 return ret;
325
Chris Wilson85e17f52016-10-28 13:58:53 +0100326 i915_gem_retire_requests(i915);
Chris Wilson28176ef2016-10-28 13:58:56 +0100327 GEM_BUG_ON(i915->gt.active_requests > 1);
Chris Wilson05235c52016-07-20 09:21:08 +0100328
329 /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
Joonas Lahtinen4c266ed2016-11-24 14:47:49 +0000330 if (!i915_seqno_passed(seqno, atomic_read(&timeline->seqno))) {
Chris Wilson6a5d1db2016-11-08 14:37:19 +0000331 while (intel_breadcrumbs_busy(i915))
332 cond_resched(); /* spin until threads are complete */
Chris Wilson05235c52016-07-20 09:21:08 +0100333 }
Joonas Lahtinen4c266ed2016-11-24 14:47:49 +0000334 atomic_set(&timeline->seqno, seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100335
336 /* Finally reset hw state */
Chris Wilson85e17f52016-10-28 13:58:53 +0100337 for_each_engine(engine, i915, id)
Chris Wilson73cb9702016-10-28 13:58:46 +0100338 intel_engine_init_global_seqno(engine, seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100339
Chris Wilson85e17f52016-10-28 13:58:53 +0100340 list_for_each_entry(timeline, &i915->gt.timelines, link) {
341 for_each_engine(engine, i915, id) {
342 struct intel_timeline *tl = &timeline->engine[id];
343
344 memset(tl->sync_seqno, 0, sizeof(tl->sync_seqno));
345 }
346 }
347
Chris Wilson05235c52016-07-20 09:21:08 +0100348 return 0;
349}
350
Chris Wilson73cb9702016-10-28 13:58:46 +0100351int i915_gem_set_global_seqno(struct drm_device *dev, u32 seqno)
Chris Wilson05235c52016-07-20 09:21:08 +0100352{
353 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson05235c52016-07-20 09:21:08 +0100354
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100355 lockdep_assert_held(&dev_priv->drm.struct_mutex);
356
Chris Wilson05235c52016-07-20 09:21:08 +0100357 if (seqno == 0)
358 return -EINVAL;
359
360 /* HWS page needs to be set less than what we
361 * will inject to ring
362 */
Chris Wilson28176ef2016-10-28 13:58:56 +0100363 return i915_gem_init_global_seqno(dev_priv, seqno - 1);
364}
Chris Wilson05235c52016-07-20 09:21:08 +0100365
Chris Wilson28176ef2016-10-28 13:58:56 +0100366static int reserve_global_seqno(struct drm_i915_private *i915)
367{
368 u32 active_requests = ++i915->gt.active_requests;
Joonas Lahtinen4c266ed2016-11-24 14:47:49 +0000369 u32 seqno = atomic_read(&i915->gt.global_timeline.seqno);
Chris Wilson28176ef2016-10-28 13:58:56 +0100370 int ret;
371
372 /* Reservation is fine until we need to wrap around */
Joonas Lahtinen4c266ed2016-11-24 14:47:49 +0000373 if (likely(seqno + active_requests > seqno))
Chris Wilson28176ef2016-10-28 13:58:56 +0100374 return 0;
375
376 ret = i915_gem_init_global_seqno(i915, 0);
377 if (ret) {
378 i915->gt.active_requests--;
379 return ret;
380 }
381
Chris Wilson05235c52016-07-20 09:21:08 +0100382 return 0;
383}
384
Chris Wilson80b204b2016-10-28 13:58:58 +0100385static u32 __timeline_get_seqno(struct i915_gem_timeline *tl)
386{
Joonas Lahtinen4c266ed2016-11-24 14:47:49 +0000387 /* seqno only incremented under a mutex */
388 return ++tl->seqno.counter;
Chris Wilson80b204b2016-10-28 13:58:58 +0100389}
390
Chris Wilson28176ef2016-10-28 13:58:56 +0100391static u32 timeline_get_seqno(struct i915_gem_timeline *tl)
Chris Wilson05235c52016-07-20 09:21:08 +0100392{
Joonas Lahtinen4c266ed2016-11-24 14:47:49 +0000393 return atomic_inc_return(&tl->seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100394}
395
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000396void __i915_gem_request_submit(struct drm_i915_gem_request *request)
Chris Wilson5590af32016-09-09 14:11:54 +0100397{
Chris Wilson73cb9702016-10-28 13:58:46 +0100398 struct intel_engine_cs *engine = request->engine;
Chris Wilsonf2d13292016-10-28 13:58:57 +0100399 struct intel_timeline *timeline;
400 u32 seqno;
Chris Wilson5590af32016-09-09 14:11:54 +0100401
Chris Wilson80b204b2016-10-28 13:58:58 +0100402 /* Transfer from per-context onto the global per-engine timeline */
403 timeline = engine->timeline;
404 GEM_BUG_ON(timeline == request->timeline);
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000405 assert_spin_locked(&timeline->lock);
Chris Wilson5590af32016-09-09 14:11:54 +0100406
Chris Wilson80b204b2016-10-28 13:58:58 +0100407 seqno = timeline_get_seqno(timeline->common);
Chris Wilsonf2d13292016-10-28 13:58:57 +0100408 GEM_BUG_ON(!seqno);
409 GEM_BUG_ON(i915_seqno_passed(intel_engine_get_seqno(engine), seqno));
410
411 GEM_BUG_ON(i915_seqno_passed(timeline->last_submitted_seqno, seqno));
412 request->previous_seqno = timeline->last_submitted_seqno;
413 timeline->last_submitted_seqno = seqno;
414
415 /* We may be recursing from the signal callback of another i915 fence */
416 spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
417 request->global_seqno = seqno;
418 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
419 intel_engine_enable_signaling(request);
420 spin_unlock(&request->lock);
421
422 GEM_BUG_ON(!request->global_seqno);
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100423 engine->emit_breadcrumb(request,
424 request->ring->vaddr + request->postfix);
Chris Wilson5590af32016-09-09 14:11:54 +0100425
Chris Wilsonbb894852016-11-14 20:40:57 +0000426 spin_lock(&request->timeline->lock);
Chris Wilson80b204b2016-10-28 13:58:58 +0100427 list_move_tail(&request->link, &timeline->requests);
428 spin_unlock(&request->timeline->lock);
429
Chris Wilson23902e42016-11-14 20:40:58 +0000430 i915_sw_fence_commit(&request->execute);
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000431}
Chris Wilson23902e42016-11-14 20:40:58 +0000432
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000433void i915_gem_request_submit(struct drm_i915_gem_request *request)
434{
435 struct intel_engine_cs *engine = request->engine;
436 unsigned long flags;
437
438 /* Will be called from irq-context when using foreign fences. */
439 spin_lock_irqsave(&engine->timeline->lock, flags);
440
441 __i915_gem_request_submit(request);
442
443 spin_unlock_irqrestore(&engine->timeline->lock, flags);
444}
445
446static int __i915_sw_fence_call
447submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
448{
Chris Wilson48bc2a42016-11-25 13:17:17 +0000449 struct drm_i915_gem_request *request =
450 container_of(fence, typeof(*request), submit);
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000451
Chris Wilson48bc2a42016-11-25 13:17:17 +0000452 switch (state) {
453 case FENCE_COMPLETE:
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000454 request->engine->submit_request(request);
Chris Wilson48bc2a42016-11-25 13:17:17 +0000455 break;
456
457 case FENCE_FREE:
458 i915_gem_request_put(request);
459 break;
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000460 }
Chris Wilson80b204b2016-10-28 13:58:58 +0100461
Chris Wilson5590af32016-09-09 14:11:54 +0100462 return NOTIFY_DONE;
463}
464
Chris Wilson23902e42016-11-14 20:40:58 +0000465static int __i915_sw_fence_call
466execute_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
467{
Chris Wilson48bc2a42016-11-25 13:17:17 +0000468 struct drm_i915_gem_request *request =
469 container_of(fence, typeof(*request), execute);
470
471 switch (state) {
472 case FENCE_COMPLETE:
473 break;
474
475 case FENCE_FREE:
476 i915_gem_request_put(request);
477 break;
478 }
479
Chris Wilson23902e42016-11-14 20:40:58 +0000480 return NOTIFY_DONE;
481}
482
Chris Wilson8e637172016-08-02 22:50:26 +0100483/**
484 * i915_gem_request_alloc - allocate a request structure
485 *
486 * @engine: engine that we wish to issue the request on.
487 * @ctx: context that the request will be associated with.
488 * This can be NULL if the request is not directly related to
489 * any specific user context, in which case this function will
490 * choose an appropriate context to use.
491 *
492 * Returns a pointer to the allocated request if successful,
493 * or an error code if not.
494 */
495struct drm_i915_gem_request *
496i915_gem_request_alloc(struct intel_engine_cs *engine,
497 struct i915_gem_context *ctx)
Chris Wilson05235c52016-07-20 09:21:08 +0100498{
499 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson05235c52016-07-20 09:21:08 +0100500 struct drm_i915_gem_request *req;
501 int ret;
502
Chris Wilson28176ef2016-10-28 13:58:56 +0100503 lockdep_assert_held(&dev_priv->drm.struct_mutex);
504
Chris Wilson05235c52016-07-20 09:21:08 +0100505 /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
Chris Wilson6ffb7d02017-01-14 16:23:33 +0000506 * EIO if the GPU is already wedged.
Chris Wilson05235c52016-07-20 09:21:08 +0100507 */
Chris Wilson6ffb7d02017-01-14 16:23:33 +0000508 if (i915_terminally_wedged(&dev_priv->gpu_error))
509 return ERR_PTR(-EIO);
Chris Wilson05235c52016-07-20 09:21:08 +0100510
Chris Wilsone8a9c582016-12-18 15:37:20 +0000511 /* Pinning the contexts may generate requests in order to acquire
512 * GGTT space, so do this first before we reserve a seqno for
513 * ourselves.
514 */
515 ret = engine->context_pin(engine, ctx);
Chris Wilson28176ef2016-10-28 13:58:56 +0100516 if (ret)
517 return ERR_PTR(ret);
518
Chris Wilsone8a9c582016-12-18 15:37:20 +0000519 ret = reserve_global_seqno(dev_priv);
520 if (ret)
521 goto err_unpin;
522
Chris Wilson9b5f4e52016-07-20 09:21:09 +0100523 /* Move the oldest request to the slab-cache (if not in use!) */
Chris Wilson73cb9702016-10-28 13:58:46 +0100524 req = list_first_entry_or_null(&engine->timeline->requests,
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100525 typeof(*req), link);
Chris Wilson80b204b2016-10-28 13:58:58 +0100526 if (req && __i915_gem_request_completed(req))
Chris Wilson2a1d7752016-07-26 12:01:51 +0100527 i915_gem_request_retire(req);
Chris Wilson9b5f4e52016-07-20 09:21:09 +0100528
Chris Wilson5a198b82016-08-09 09:23:34 +0100529 /* Beware: Dragons be flying overhead.
530 *
531 * We use RCU to look up requests in flight. The lookups may
532 * race with the request being allocated from the slab freelist.
533 * That is the request we are writing to here, may be in the process
Chris Wilson1426f712016-08-09 17:03:22 +0100534 * of being read by __i915_gem_active_get_rcu(). As such,
Chris Wilson5a198b82016-08-09 09:23:34 +0100535 * we have to be very careful when overwriting the contents. During
536 * the RCU lookup, we change chase the request->engine pointer,
Chris Wilson65e47602016-10-28 13:58:49 +0100537 * read the request->global_seqno and increment the reference count.
Chris Wilson5a198b82016-08-09 09:23:34 +0100538 *
539 * The reference count is incremented atomically. If it is zero,
540 * the lookup knows the request is unallocated and complete. Otherwise,
541 * it is either still in use, or has been reallocated and reset
Chris Wilsonf54d1862016-10-25 13:00:45 +0100542 * with dma_fence_init(). This increment is safe for release as we
543 * check that the request we have a reference to and matches the active
Chris Wilson5a198b82016-08-09 09:23:34 +0100544 * request.
545 *
546 * Before we increment the refcount, we chase the request->engine
547 * pointer. We must not call kmem_cache_zalloc() or else we set
548 * that pointer to NULL and cause a crash during the lookup. If
549 * we see the request is completed (based on the value of the
550 * old engine and seqno), the lookup is complete and reports NULL.
551 * If we decide the request is not completed (new engine or seqno),
552 * then we grab a reference and double check that it is still the
553 * active request - which it won't be and restart the lookup.
554 *
555 * Do not use kmem_cache_zalloc() here!
556 */
557 req = kmem_cache_alloc(dev_priv->requests, GFP_KERNEL);
Chris Wilson28176ef2016-10-28 13:58:56 +0100558 if (!req) {
559 ret = -ENOMEM;
560 goto err_unreserve;
561 }
Chris Wilson05235c52016-07-20 09:21:08 +0100562
Chris Wilson80b204b2016-10-28 13:58:58 +0100563 req->timeline = i915_gem_context_lookup_timeline(ctx, engine);
564 GEM_BUG_ON(req->timeline == engine->timeline);
Chris Wilson73cb9702016-10-28 13:58:46 +0100565
Chris Wilson04769652016-07-20 09:21:11 +0100566 spin_lock_init(&req->lock);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100567 dma_fence_init(&req->fence,
568 &i915_fence_ops,
569 &req->lock,
Chris Wilson73cb9702016-10-28 13:58:46 +0100570 req->timeline->fence_context,
Chris Wilson80b204b2016-10-28 13:58:58 +0100571 __timeline_get_seqno(req->timeline->common));
Chris Wilson04769652016-07-20 09:21:11 +0100572
Chris Wilson48bc2a42016-11-25 13:17:17 +0000573 /* We bump the ref for the fence chain */
574 i915_sw_fence_init(&i915_gem_request_get(req)->submit, submit_notify);
575 i915_sw_fence_init(&i915_gem_request_get(req)->execute, execute_notify);
576
Chris Wilson23902e42016-11-14 20:40:58 +0000577 /* Ensure that the execute fence completes after the submit fence -
578 * as we complete the execute fence from within the submit fence
579 * callback, its completion would otherwise be visible first.
580 */
581 i915_sw_fence_await_sw_fence(&req->execute, &req->submit, &req->execq);
Chris Wilson5590af32016-09-09 14:11:54 +0100582
Chris Wilson52e54202016-11-14 20:41:02 +0000583 i915_priotree_init(&req->priotree);
584
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100585 INIT_LIST_HEAD(&req->active_list);
Chris Wilson05235c52016-07-20 09:21:08 +0100586 req->i915 = dev_priv;
587 req->engine = engine;
Chris Wilsone8a9c582016-12-18 15:37:20 +0000588 req->ctx = ctx;
Chris Wilson05235c52016-07-20 09:21:08 +0100589
Chris Wilson5a198b82016-08-09 09:23:34 +0100590 /* No zalloc, must clear what we need by hand */
Chris Wilsonf2d13292016-10-28 13:58:57 +0100591 req->global_seqno = 0;
Chris Wilson5a198b82016-08-09 09:23:34 +0100592 req->file_priv = NULL;
Chris Wilson058d88c2016-08-15 10:49:06 +0100593 req->batch = NULL;
Chris Wilson5a198b82016-08-09 09:23:34 +0100594
Chris Wilson05235c52016-07-20 09:21:08 +0100595 /*
596 * Reserve space in the ring buffer for all the commands required to
597 * eventually emit this request. This is to guarantee that the
598 * i915_add_request() call can't fail. Note that the reserve may need
599 * to be redone if the request is not actually submitted straight
600 * away, e.g. because a GPU scheduler has deferred it.
601 */
602 req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
Chris Wilson98f29e82016-10-28 13:58:51 +0100603 GEM_BUG_ON(req->reserved_space < engine->emit_breadcrumb_sz);
Chris Wilson05235c52016-07-20 09:21:08 +0100604
Chris Wilsonf73e7392016-12-18 15:37:24 +0000605 ret = engine->request_alloc(req);
Chris Wilson05235c52016-07-20 09:21:08 +0100606 if (ret)
607 goto err_ctx;
608
Chris Wilsond0454462016-08-15 10:48:40 +0100609 /* Record the position of the start of the request so that
610 * should we detect the updated seqno part-way through the
611 * GPU processing the request, we never over-estimate the
612 * position of the head.
613 */
614 req->head = req->ring->tail;
615
Chris Wilson8e637172016-08-02 22:50:26 +0100616 return req;
Chris Wilson05235c52016-07-20 09:21:08 +0100617
618err_ctx:
Chris Wilson1618bdb2016-11-25 13:17:16 +0000619 /* Make sure we didn't add ourselves to external state before freeing */
620 GEM_BUG_ON(!list_empty(&req->active_list));
621 GEM_BUG_ON(!list_empty(&req->priotree.signalers_list));
622 GEM_BUG_ON(!list_empty(&req->priotree.waiters_list));
623
Chris Wilson05235c52016-07-20 09:21:08 +0100624 kmem_cache_free(dev_priv->requests, req);
Chris Wilson28176ef2016-10-28 13:58:56 +0100625err_unreserve:
626 dev_priv->gt.active_requests--;
Chris Wilsone8a9c582016-12-18 15:37:20 +0000627err_unpin:
628 engine->context_unpin(engine, ctx);
Chris Wilson8e637172016-08-02 22:50:26 +0100629 return ERR_PTR(ret);
Chris Wilson05235c52016-07-20 09:21:08 +0100630}
631
Chris Wilsona2bc4692016-09-09 14:11:56 +0100632static int
633i915_gem_request_await_request(struct drm_i915_gem_request *to,
634 struct drm_i915_gem_request *from)
635{
Chris Wilson85e17f52016-10-28 13:58:53 +0100636 int ret;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100637
638 GEM_BUG_ON(to == from);
639
Chris Wilson52e54202016-11-14 20:41:02 +0000640 if (to->engine->schedule) {
641 ret = i915_priotree_add_dependency(to->i915,
642 &to->priotree,
643 &from->priotree);
644 if (ret < 0)
645 return ret;
646 }
647
Chris Wilson73cb9702016-10-28 13:58:46 +0100648 if (to->timeline == from->timeline)
Chris Wilsona2bc4692016-09-09 14:11:56 +0100649 return 0;
650
Chris Wilson73cb9702016-10-28 13:58:46 +0100651 if (to->engine == from->engine) {
652 ret = i915_sw_fence_await_sw_fence_gfp(&to->submit,
653 &from->submit,
654 GFP_KERNEL);
655 return ret < 0 ? ret : 0;
656 }
657
Chris Wilson65e47602016-10-28 13:58:49 +0100658 if (!from->global_seqno) {
659 ret = i915_sw_fence_await_dma_fence(&to->submit,
660 &from->fence, 0,
661 GFP_KERNEL);
662 return ret < 0 ? ret : 0;
663 }
664
Chris Wilson85e17f52016-10-28 13:58:53 +0100665 if (from->global_seqno <= to->timeline->sync_seqno[from->engine->id])
Chris Wilsona2bc4692016-09-09 14:11:56 +0100666 return 0;
667
668 trace_i915_gem_ring_sync_to(to, from);
669 if (!i915.semaphores) {
Chris Wilson0a046a02016-09-09 14:12:00 +0100670 if (!i915_spin_request(from, TASK_INTERRUPTIBLE, 2)) {
671 ret = i915_sw_fence_await_dma_fence(&to->submit,
672 &from->fence, 0,
673 GFP_KERNEL);
674 if (ret < 0)
675 return ret;
676 }
Chris Wilsona2bc4692016-09-09 14:11:56 +0100677 } else {
678 ret = to->engine->semaphore.sync_to(to, from);
679 if (ret)
680 return ret;
681 }
682
Chris Wilson85e17f52016-10-28 13:58:53 +0100683 to->timeline->sync_seqno[from->engine->id] = from->global_seqno;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100684 return 0;
685}
686
Chris Wilsonb52992c2016-10-28 13:58:24 +0100687int
688i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
689 struct dma_fence *fence)
690{
691 struct dma_fence_array *array;
692 int ret;
693 int i;
694
695 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
696 return 0;
697
698 if (dma_fence_is_i915(fence))
699 return i915_gem_request_await_request(req, to_request(fence));
700
701 if (!dma_fence_is_array(fence)) {
702 ret = i915_sw_fence_await_dma_fence(&req->submit,
703 fence, I915_FENCE_TIMEOUT,
704 GFP_KERNEL);
705 return ret < 0 ? ret : 0;
706 }
707
708 /* Note that if the fence-array was created in signal-on-any mode,
709 * we should *not* decompose it into its individual fences. However,
710 * we don't currently store which mode the fence-array is operating
711 * in. Fortunately, the only user of signal-on-any is private to
712 * amdgpu and we should not see any incoming fence-array from
713 * sync-file being in signal-on-any mode.
714 */
715
716 array = to_dma_fence_array(fence);
717 for (i = 0; i < array->num_fences; i++) {
718 struct dma_fence *child = array->fences[i];
719
720 if (dma_fence_is_i915(child))
721 ret = i915_gem_request_await_request(req,
722 to_request(child));
723 else
724 ret = i915_sw_fence_await_dma_fence(&req->submit,
725 child, I915_FENCE_TIMEOUT,
726 GFP_KERNEL);
727 if (ret < 0)
728 return ret;
729 }
730
731 return 0;
732}
733
Chris Wilsona2bc4692016-09-09 14:11:56 +0100734/**
735 * i915_gem_request_await_object - set this request to (async) wait upon a bo
736 *
737 * @to: request we are wishing to use
738 * @obj: object which may be in use on another ring.
739 *
740 * This code is meant to abstract object synchronization with the GPU.
741 * Conceptually we serialise writes between engines inside the GPU.
742 * We only allow one engine to write into a buffer at any time, but
743 * multiple readers. To ensure each has a coherent view of memory, we must:
744 *
745 * - If there is an outstanding write request to the object, the new
746 * request must wait for it to complete (either CPU or in hw, requests
747 * on the same ring will be naturally ordered).
748 *
749 * - If we are a write request (pending_write_domain is set), the new
750 * request must wait for outstanding read requests to complete.
751 *
752 * Returns 0 if successful, else propagates up the lower layer error.
753 */
754int
755i915_gem_request_await_object(struct drm_i915_gem_request *to,
756 struct drm_i915_gem_object *obj,
757 bool write)
758{
Chris Wilsond07f0e52016-10-28 13:58:44 +0100759 struct dma_fence *excl;
760 int ret = 0;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100761
762 if (write) {
Chris Wilsond07f0e52016-10-28 13:58:44 +0100763 struct dma_fence **shared;
764 unsigned int count, i;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100765
Chris Wilsond07f0e52016-10-28 13:58:44 +0100766 ret = reservation_object_get_fences_rcu(obj->resv,
767 &excl, &count, &shared);
Chris Wilsona2bc4692016-09-09 14:11:56 +0100768 if (ret)
769 return ret;
Chris Wilsond07f0e52016-10-28 13:58:44 +0100770
771 for (i = 0; i < count; i++) {
772 ret = i915_gem_request_await_dma_fence(to, shared[i]);
773 if (ret)
774 break;
775
776 dma_fence_put(shared[i]);
777 }
778
779 for (; i < count; i++)
780 dma_fence_put(shared[i]);
781 kfree(shared);
782 } else {
783 excl = reservation_object_get_excl_rcu(obj->resv);
Chris Wilsona2bc4692016-09-09 14:11:56 +0100784 }
785
Chris Wilsond07f0e52016-10-28 13:58:44 +0100786 if (excl) {
787 if (ret == 0)
788 ret = i915_gem_request_await_dma_fence(to, excl);
789
790 dma_fence_put(excl);
791 }
792
793 return ret;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100794}
795
Chris Wilson05235c52016-07-20 09:21:08 +0100796static void i915_gem_mark_busy(const struct intel_engine_cs *engine)
797{
798 struct drm_i915_private *dev_priv = engine->i915;
799
Chris Wilson05235c52016-07-20 09:21:08 +0100800 if (dev_priv->gt.awake)
801 return;
802
Chris Wilson43020552016-11-15 16:46:20 +0000803 GEM_BUG_ON(!dev_priv->gt.active_requests);
804
Chris Wilson05235c52016-07-20 09:21:08 +0100805 intel_runtime_pm_get_noresume(dev_priv);
806 dev_priv->gt.awake = true;
807
Chris Wilson54b4f682016-07-21 21:16:19 +0100808 intel_enable_gt_powersave(dev_priv);
Chris Wilson05235c52016-07-20 09:21:08 +0100809 i915_update_gfx_val(dev_priv);
810 if (INTEL_GEN(dev_priv) >= 6)
811 gen6_rps_busy(dev_priv);
812
813 queue_delayed_work(dev_priv->wq,
814 &dev_priv->gt.retire_work,
815 round_jiffies_up_relative(HZ));
816}
817
818/*
819 * NB: This function is not allowed to fail. Doing so would mean the the
820 * request is not being tracked for completion but the work itself is
821 * going to happen on the hardware. This would be a Bad Thing(tm).
822 */
Chris Wilson17f298cf2016-08-10 13:41:46 +0100823void __i915_add_request(struct drm_i915_gem_request *request, bool flush_caches)
Chris Wilson05235c52016-07-20 09:21:08 +0100824{
Chris Wilson95b2ab52016-08-15 10:48:46 +0100825 struct intel_engine_cs *engine = request->engine;
826 struct intel_ring *ring = request->ring;
Chris Wilson73cb9702016-10-28 13:58:46 +0100827 struct intel_timeline *timeline = request->timeline;
Chris Wilson0a046a02016-09-09 14:12:00 +0100828 struct drm_i915_gem_request *prev;
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100829 int err;
Chris Wilson05235c52016-07-20 09:21:08 +0100830
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100831 lockdep_assert_held(&request->i915->drm.struct_mutex);
Chris Wilson0f25dff2016-09-09 14:11:55 +0100832 trace_i915_gem_request_add(request);
833
Chris Wilsonc781c972017-01-11 14:08:58 +0000834 /* Make sure that no request gazumped us - if it was allocated after
835 * our i915_gem_request_alloc() and called __i915_add_request() before
836 * us, the timeline will hold its seqno which is later than ours.
837 */
838 GEM_BUG_ON(i915_seqno_passed(timeline->last_submitted_seqno,
839 request->fence.seqno));
840
Chris Wilson05235c52016-07-20 09:21:08 +0100841 /*
842 * To ensure that this call will not fail, space for its emissions
843 * should already have been reserved in the ring buffer. Let the ring
844 * know that it is time to use that space up.
845 */
Chris Wilson05235c52016-07-20 09:21:08 +0100846 request->reserved_space = 0;
847
848 /*
849 * Emit any outstanding flushes - execbuf can fail to emit the flush
850 * after having emitted the batchbuffer command. Hence we need to fix
851 * things up similar to emitting the lazy request. The difference here
852 * is that the flush _must_ happen before the next request, no matter
853 * what.
854 */
855 if (flush_caches) {
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100856 err = engine->emit_flush(request, EMIT_FLUSH);
Chris Wilsonc7fe7d22016-08-02 22:50:24 +0100857
Chris Wilson05235c52016-07-20 09:21:08 +0100858 /* Not allowed to fail! */
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100859 WARN(err, "engine->emit_flush() failed: %d!\n", err);
Chris Wilson05235c52016-07-20 09:21:08 +0100860 }
861
Chris Wilsond0454462016-08-15 10:48:40 +0100862 /* Record the position of the start of the breadcrumb so that
Chris Wilson05235c52016-07-20 09:21:08 +0100863 * should we detect the updated seqno part-way through the
864 * GPU processing the request, we never over-estimate the
Chris Wilsond0454462016-08-15 10:48:40 +0100865 * position of the ring's HEAD.
Chris Wilson05235c52016-07-20 09:21:08 +0100866 */
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100867 err = intel_ring_begin(request, engine->emit_breadcrumb_sz);
868 GEM_BUG_ON(err);
Chris Wilsonba76d912016-08-02 22:50:28 +0100869 request->postfix = ring->tail;
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100870 ring->tail += engine->emit_breadcrumb_sz * sizeof(u32);
Chris Wilson05235c52016-07-20 09:21:08 +0100871
Chris Wilson0f25dff2016-09-09 14:11:55 +0100872 /* Seal the request and mark it as pending execution. Note that
873 * we may inspect this state, without holding any locks, during
874 * hangcheck. Hence we apply the barrier to ensure that we do not
875 * see a more recent value in the hws than we are tracking.
876 */
Chris Wilson0a046a02016-09-09 14:12:00 +0100877
Chris Wilson73cb9702016-10-28 13:58:46 +0100878 prev = i915_gem_active_raw(&timeline->last_request,
Chris Wilson0a046a02016-09-09 14:12:00 +0100879 &request->i915->drm.struct_mutex);
Chris Wilson52e54202016-11-14 20:41:02 +0000880 if (prev) {
Chris Wilson0a046a02016-09-09 14:12:00 +0100881 i915_sw_fence_await_sw_fence(&request->submit, &prev->submit,
882 &request->submitq);
Chris Wilson52e54202016-11-14 20:41:02 +0000883 if (engine->schedule)
884 __i915_priotree_add_dependency(&request->priotree,
885 &prev->priotree,
886 &request->dep,
887 0);
888 }
Chris Wilson0a046a02016-09-09 14:12:00 +0100889
Chris Wilson80b204b2016-10-28 13:58:58 +0100890 spin_lock_irq(&timeline->lock);
Chris Wilsonf2d13292016-10-28 13:58:57 +0100891 list_add_tail(&request->link, &timeline->requests);
Chris Wilson80b204b2016-10-28 13:58:58 +0100892 spin_unlock_irq(&timeline->lock);
Chris Wilson28176ef2016-10-28 13:58:56 +0100893
Chris Wilson80b204b2016-10-28 13:58:58 +0100894 GEM_BUG_ON(i915_seqno_passed(timeline->last_submitted_seqno,
895 request->fence.seqno));
896
897 timeline->last_submitted_seqno = request->fence.seqno;
Chris Wilson73cb9702016-10-28 13:58:46 +0100898 i915_gem_active_set(&timeline->last_request, request);
Chris Wilsonf2d13292016-10-28 13:58:57 +0100899
Chris Wilson0f25dff2016-09-09 14:11:55 +0100900 list_add_tail(&request->ring_link, &ring->request_list);
Chris Wilsonf2d13292016-10-28 13:58:57 +0100901 request->emitted_jiffies = jiffies;
Chris Wilson0f25dff2016-09-09 14:11:55 +0100902
Chris Wilson05235c52016-07-20 09:21:08 +0100903 i915_gem_mark_busy(engine);
Chris Wilson5590af32016-09-09 14:11:54 +0100904
Chris Wilson0de91362016-11-14 20:41:01 +0000905 /* Let the backend know a new request has arrived that may need
906 * to adjust the existing execution schedule due to a high priority
907 * request - i.e. we may want to preempt the current request in order
908 * to run a high priority dependency chain *before* we can execute this
909 * request.
910 *
911 * This is called before the request is ready to run so that we can
912 * decide whether to preempt the entire chain so that it is ready to
913 * run at the earliest possible convenience.
914 */
915 if (engine->schedule)
Chris Wilson9f792eb2016-11-14 20:41:04 +0000916 engine->schedule(request, request->ctx->priority);
Chris Wilson0de91362016-11-14 20:41:01 +0000917
Chris Wilson5590af32016-09-09 14:11:54 +0100918 local_bh_disable();
919 i915_sw_fence_commit(&request->submit);
920 local_bh_enable(); /* Kick the execlists tasklet if just scheduled */
Chris Wilson05235c52016-07-20 09:21:08 +0100921}
922
Chris Wilson221fe792016-09-09 14:11:51 +0100923static void reset_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
924{
925 unsigned long flags;
926
927 spin_lock_irqsave(&q->lock, flags);
928 if (list_empty(&wait->task_list))
929 __add_wait_queue(q, wait);
930 spin_unlock_irqrestore(&q->lock, flags);
931}
932
Chris Wilson05235c52016-07-20 09:21:08 +0100933static unsigned long local_clock_us(unsigned int *cpu)
934{
935 unsigned long t;
936
937 /* Cheaply and approximately convert from nanoseconds to microseconds.
938 * The result and subsequent calculations are also defined in the same
939 * approximate microseconds units. The principal source of timing
940 * error here is from the simple truncation.
941 *
942 * Note that local_clock() is only defined wrt to the current CPU;
943 * the comparisons are no longer valid if we switch CPUs. Instead of
944 * blocking preemption for the entire busywait, we can detect the CPU
945 * switch and use that as indicator of system load and a reason to
946 * stop busywaiting, see busywait_stop().
947 */
948 *cpu = get_cpu();
949 t = local_clock() >> 10;
950 put_cpu();
951
952 return t;
953}
954
955static bool busywait_stop(unsigned long timeout, unsigned int cpu)
956{
957 unsigned int this_cpu;
958
959 if (time_after(local_clock_us(&this_cpu), timeout))
960 return true;
961
962 return this_cpu != cpu;
963}
964
965bool __i915_spin_request(const struct drm_i915_gem_request *req,
966 int state, unsigned long timeout_us)
967{
968 unsigned int cpu;
969
970 /* When waiting for high frequency requests, e.g. during synchronous
971 * rendering split between the CPU and GPU, the finite amount of time
972 * required to set up the irq and wait upon it limits the response
973 * rate. By busywaiting on the request completion for a short while we
974 * can service the high frequency waits as quick as possible. However,
975 * if it is a slow request, we want to sleep as quickly as possible.
976 * The tradeoff between waiting and sleeping is roughly the time it
977 * takes to sleep on a request, on the order of a microsecond.
978 */
979
980 timeout_us += local_clock_us(&cpu);
981 do {
Chris Wilson65e47602016-10-28 13:58:49 +0100982 if (__i915_gem_request_completed(req))
Chris Wilson05235c52016-07-20 09:21:08 +0100983 return true;
984
985 if (signal_pending_state(state, current))
986 break;
987
988 if (busywait_stop(timeout_us, cpu))
989 break;
990
Christian Borntraegerf2f09a42016-10-25 11:03:14 +0200991 cpu_relax();
Chris Wilson05235c52016-07-20 09:21:08 +0100992 } while (!need_resched());
993
994 return false;
995}
996
Chris Wilson4680816b2016-10-28 13:58:48 +0100997static long
Chris Wilson23902e42016-11-14 20:40:58 +0000998__i915_request_wait_for_execute(struct drm_i915_gem_request *request,
999 unsigned int flags,
1000 long timeout)
Chris Wilson4680816b2016-10-28 13:58:48 +01001001{
1002 const int state = flags & I915_WAIT_INTERRUPTIBLE ?
1003 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
1004 wait_queue_head_t *q = &request->i915->gpu_error.wait_queue;
1005 DEFINE_WAIT(reset);
1006 DEFINE_WAIT(wait);
1007
1008 if (flags & I915_WAIT_LOCKED)
1009 add_wait_queue(q, &reset);
1010
1011 do {
Chris Wilson23902e42016-11-14 20:40:58 +00001012 prepare_to_wait(&request->execute.wait, &wait, state);
Chris Wilson4680816b2016-10-28 13:58:48 +01001013
Chris Wilson23902e42016-11-14 20:40:58 +00001014 if (i915_sw_fence_done(&request->execute))
Chris Wilson4680816b2016-10-28 13:58:48 +01001015 break;
1016
1017 if (flags & I915_WAIT_LOCKED &&
1018 i915_reset_in_progress(&request->i915->gpu_error)) {
1019 __set_current_state(TASK_RUNNING);
1020 i915_reset(request->i915);
1021 reset_wait_queue(q, &reset);
1022 continue;
1023 }
1024
1025 if (signal_pending_state(state, current)) {
1026 timeout = -ERESTARTSYS;
1027 break;
1028 }
1029
Chris Wilson44a02702017-02-08 18:12:38 +00001030 if (!timeout) {
1031 timeout = -ETIME;
1032 break;
1033 }
1034
Chris Wilson4680816b2016-10-28 13:58:48 +01001035 timeout = io_schedule_timeout(timeout);
Chris Wilson44a02702017-02-08 18:12:38 +00001036 } while (1);
Chris Wilson23902e42016-11-14 20:40:58 +00001037 finish_wait(&request->execute.wait, &wait);
Chris Wilson4680816b2016-10-28 13:58:48 +01001038
1039 if (flags & I915_WAIT_LOCKED)
1040 remove_wait_queue(q, &reset);
1041
1042 return timeout;
1043}
1044
Chris Wilson05235c52016-07-20 09:21:08 +01001045/**
Chris Wilson776f3232016-08-04 07:52:40 +01001046 * i915_wait_request - wait until execution of request has finished
Chris Wilsone95433c2016-10-28 13:58:27 +01001047 * @req: the request to wait upon
Chris Wilsonea746f32016-09-09 14:11:49 +01001048 * @flags: how to wait
Chris Wilsone95433c2016-10-28 13:58:27 +01001049 * @timeout: how long to wait in jiffies
Chris Wilson05235c52016-07-20 09:21:08 +01001050 *
Chris Wilsone95433c2016-10-28 13:58:27 +01001051 * i915_wait_request() waits for the request to be completed, for a
1052 * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
1053 * unbounded wait).
Chris Wilson05235c52016-07-20 09:21:08 +01001054 *
Chris Wilsone95433c2016-10-28 13:58:27 +01001055 * If the caller holds the struct_mutex, the caller must pass I915_WAIT_LOCKED
1056 * in via the flags, and vice versa if the struct_mutex is not held, the caller
1057 * must not specify that the wait is locked.
1058 *
1059 * Returns the remaining time (in jiffies) if the request completed, which may
1060 * be zero or -ETIME if the request is unfinished after the timeout expires.
1061 * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
1062 * pending before the request completes.
Chris Wilson05235c52016-07-20 09:21:08 +01001063 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001064long i915_wait_request(struct drm_i915_gem_request *req,
1065 unsigned int flags,
1066 long timeout)
Chris Wilson05235c52016-07-20 09:21:08 +01001067{
Chris Wilsonea746f32016-09-09 14:11:49 +01001068 const int state = flags & I915_WAIT_INTERRUPTIBLE ?
1069 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
Chris Wilson05235c52016-07-20 09:21:08 +01001070 DEFINE_WAIT(reset);
1071 struct intel_wait wait;
Chris Wilson05235c52016-07-20 09:21:08 +01001072
1073 might_sleep();
Chris Wilson22dd3bb2016-09-09 14:11:50 +01001074#if IS_ENABLED(CONFIG_LOCKDEP)
Chris Wilsone95433c2016-10-28 13:58:27 +01001075 GEM_BUG_ON(debug_locks &&
1076 !!lockdep_is_held(&req->i915->drm.struct_mutex) !=
Chris Wilson22dd3bb2016-09-09 14:11:50 +01001077 !!(flags & I915_WAIT_LOCKED));
1078#endif
Chris Wilsone95433c2016-10-28 13:58:27 +01001079 GEM_BUG_ON(timeout < 0);
Chris Wilson05235c52016-07-20 09:21:08 +01001080
Chris Wilson05235c52016-07-20 09:21:08 +01001081 if (i915_gem_request_completed(req))
Chris Wilsone95433c2016-10-28 13:58:27 +01001082 return timeout;
Chris Wilson05235c52016-07-20 09:21:08 +01001083
Chris Wilsone95433c2016-10-28 13:58:27 +01001084 if (!timeout)
1085 return -ETIME;
Chris Wilson05235c52016-07-20 09:21:08 +01001086
1087 trace_i915_gem_request_wait_begin(req);
1088
Chris Wilson23902e42016-11-14 20:40:58 +00001089 if (!i915_sw_fence_done(&req->execute)) {
1090 timeout = __i915_request_wait_for_execute(req, flags, timeout);
Chris Wilson4680816b2016-10-28 13:58:48 +01001091 if (timeout < 0)
1092 goto complete;
1093
Chris Wilson23902e42016-11-14 20:40:58 +00001094 GEM_BUG_ON(!i915_sw_fence_done(&req->execute));
Chris Wilson4680816b2016-10-28 13:58:48 +01001095 }
Chris Wilson23902e42016-11-14 20:40:58 +00001096 GEM_BUG_ON(!i915_sw_fence_done(&req->submit));
Chris Wilson65e47602016-10-28 13:58:49 +01001097 GEM_BUG_ON(!req->global_seqno);
Chris Wilson4680816b2016-10-28 13:58:48 +01001098
Daniel Vetter437c3082016-08-05 18:11:24 +02001099 /* Optimistic short spin before touching IRQs */
Chris Wilson05235c52016-07-20 09:21:08 +01001100 if (i915_spin_request(req, state, 5))
1101 goto complete;
1102
1103 set_current_state(state);
Chris Wilson22dd3bb2016-09-09 14:11:50 +01001104 if (flags & I915_WAIT_LOCKED)
1105 add_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
Chris Wilson05235c52016-07-20 09:21:08 +01001106
Chris Wilson65e47602016-10-28 13:58:49 +01001107 intel_wait_init(&wait, req->global_seqno);
Chris Wilson05235c52016-07-20 09:21:08 +01001108 if (intel_engine_add_wait(req->engine, &wait))
1109 /* In order to check that we haven't missed the interrupt
1110 * as we enabled it, we need to kick ourselves to do a
1111 * coherent check on the seqno before we sleep.
1112 */
1113 goto wakeup;
1114
1115 for (;;) {
1116 if (signal_pending_state(state, current)) {
Chris Wilsone95433c2016-10-28 13:58:27 +01001117 timeout = -ERESTARTSYS;
Chris Wilson05235c52016-07-20 09:21:08 +01001118 break;
1119 }
1120
Chris Wilsone95433c2016-10-28 13:58:27 +01001121 if (!timeout) {
1122 timeout = -ETIME;
Chris Wilson05235c52016-07-20 09:21:08 +01001123 break;
1124 }
1125
Chris Wilsone95433c2016-10-28 13:58:27 +01001126 timeout = io_schedule_timeout(timeout);
1127
Chris Wilson05235c52016-07-20 09:21:08 +01001128 if (intel_wait_complete(&wait))
1129 break;
1130
1131 set_current_state(state);
1132
1133wakeup:
1134 /* Carefully check if the request is complete, giving time
1135 * for the seqno to be visible following the interrupt.
1136 * We also have to check in case we are kicked by the GPU
1137 * reset in order to drop the struct_mutex.
1138 */
1139 if (__i915_request_irq_complete(req))
1140 break;
1141
Chris Wilson221fe792016-09-09 14:11:51 +01001142 /* If the GPU is hung, and we hold the lock, reset the GPU
1143 * and then check for completion. On a full reset, the engine's
1144 * HW seqno will be advanced passed us and we are complete.
1145 * If we do a partial reset, we have to wait for the GPU to
1146 * resume and update the breadcrumb.
1147 *
1148 * If we don't hold the mutex, we can just wait for the worker
1149 * to come along and update the breadcrumb (either directly
1150 * itself, or indirectly by recovering the GPU).
1151 */
1152 if (flags & I915_WAIT_LOCKED &&
1153 i915_reset_in_progress(&req->i915->gpu_error)) {
1154 __set_current_state(TASK_RUNNING);
1155 i915_reset(req->i915);
1156 reset_wait_queue(&req->i915->gpu_error.wait_queue,
1157 &reset);
1158 continue;
1159 }
1160
Chris Wilson05235c52016-07-20 09:21:08 +01001161 /* Only spin if we know the GPU is processing this request */
1162 if (i915_spin_request(req, state, 2))
1163 break;
1164 }
Chris Wilson05235c52016-07-20 09:21:08 +01001165
1166 intel_engine_remove_wait(req->engine, &wait);
Chris Wilson22dd3bb2016-09-09 14:11:50 +01001167 if (flags & I915_WAIT_LOCKED)
1168 remove_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
Chris Wilson05235c52016-07-20 09:21:08 +01001169 __set_current_state(TASK_RUNNING);
Chris Wilson22dd3bb2016-09-09 14:11:50 +01001170
Chris Wilson05235c52016-07-20 09:21:08 +01001171complete:
1172 trace_i915_gem_request_wait_end(req);
1173
Chris Wilsone95433c2016-10-28 13:58:27 +01001174 return timeout;
Chris Wilson05235c52016-07-20 09:21:08 +01001175}
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001176
Chris Wilson28176ef2016-10-28 13:58:56 +01001177static void engine_retire_requests(struct intel_engine_cs *engine)
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001178{
1179 struct drm_i915_gem_request *request, *next;
1180
Chris Wilson73cb9702016-10-28 13:58:46 +01001181 list_for_each_entry_safe(request, next,
1182 &engine->timeline->requests, link) {
Chris Wilson80b204b2016-10-28 13:58:58 +01001183 if (!__i915_gem_request_completed(request))
Chris Wilson28176ef2016-10-28 13:58:56 +01001184 return;
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001185
1186 i915_gem_request_retire(request);
1187 }
1188}
1189
1190void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
1191{
1192 struct intel_engine_cs *engine;
Chris Wilson28176ef2016-10-28 13:58:56 +01001193 enum intel_engine_id id;
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001194
1195 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1196
Chris Wilson28176ef2016-10-28 13:58:56 +01001197 if (!dev_priv->gt.active_requests)
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001198 return;
1199
Chris Wilson28176ef2016-10-28 13:58:56 +01001200 for_each_engine(engine, dev_priv, id)
1201 engine_retire_requests(engine);
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001202}