blob: 543cef57972bfd5fde1604ccdc8ff4ce137ad493 [file] [log] [blame]
Chris Wilson05235c52016-07-20 09:21:08 +01001/*
2 * Copyright © 2008-2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
Chris Wilsonfa545cb2016-08-04 07:52:35 +010025#include <linux/prefetch.h>
Chris Wilsonb52992c2016-10-28 13:58:24 +010026#include <linux/dma-fence-array.h>
Chris Wilsonfa545cb2016-08-04 07:52:35 +010027
Chris Wilson05235c52016-07-20 09:21:08 +010028#include "i915_drv.h"
29
Chris Wilsonf54d1862016-10-25 13:00:45 +010030static const char *i915_fence_get_driver_name(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010031{
32 return "i915";
33}
34
Chris Wilsonf54d1862016-10-25 13:00:45 +010035static const char *i915_fence_get_timeline_name(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010036{
Chris Wilson73cb9702016-10-28 13:58:46 +010037 return to_request(fence)->timeline->common->name;
Chris Wilson04769652016-07-20 09:21:11 +010038}
39
Chris Wilsonf54d1862016-10-25 13:00:45 +010040static bool i915_fence_signaled(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010041{
42 return i915_gem_request_completed(to_request(fence));
43}
44
Chris Wilsonf54d1862016-10-25 13:00:45 +010045static bool i915_fence_enable_signaling(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010046{
47 if (i915_fence_signaled(fence))
48 return false;
49
50 intel_engine_enable_signaling(to_request(fence));
51 return true;
52}
53
Chris Wilsonf54d1862016-10-25 13:00:45 +010054static signed long i915_fence_wait(struct dma_fence *fence,
Chris Wilson04769652016-07-20 09:21:11 +010055 bool interruptible,
Chris Wilsone95433c2016-10-28 13:58:27 +010056 signed long timeout)
Chris Wilson04769652016-07-20 09:21:11 +010057{
Chris Wilsone95433c2016-10-28 13:58:27 +010058 return i915_wait_request(to_request(fence), interruptible, timeout);
Chris Wilson04769652016-07-20 09:21:11 +010059}
60
Chris Wilsonf54d1862016-10-25 13:00:45 +010061static void i915_fence_release(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010062{
63 struct drm_i915_gem_request *req = to_request(fence);
64
Chris Wilsonfc158402016-11-25 13:17:18 +000065 /* The request is put onto a RCU freelist (i.e. the address
66 * is immediately reused), mark the fences as being freed now.
67 * Otherwise the debugobjects for the fences are only marked as
68 * freed when the slab cache itself is freed, and so we would get
69 * caught trying to reuse dead objects.
70 */
71 i915_sw_fence_fini(&req->submit);
72 i915_sw_fence_fini(&req->execute);
73
Chris Wilson04769652016-07-20 09:21:11 +010074 kmem_cache_free(req->i915->requests, req);
75}
76
Chris Wilsonf54d1862016-10-25 13:00:45 +010077const struct dma_fence_ops i915_fence_ops = {
Chris Wilson04769652016-07-20 09:21:11 +010078 .get_driver_name = i915_fence_get_driver_name,
79 .get_timeline_name = i915_fence_get_timeline_name,
80 .enable_signaling = i915_fence_enable_signaling,
81 .signaled = i915_fence_signaled,
82 .wait = i915_fence_wait,
83 .release = i915_fence_release,
Chris Wilson04769652016-07-20 09:21:11 +010084};
85
Chris Wilson05235c52016-07-20 09:21:08 +010086int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
87 struct drm_file *file)
88{
89 struct drm_i915_private *dev_private;
90 struct drm_i915_file_private *file_priv;
91
92 WARN_ON(!req || !file || req->file_priv);
93
94 if (!req || !file)
95 return -EINVAL;
96
97 if (req->file_priv)
98 return -EINVAL;
99
100 dev_private = req->i915;
101 file_priv = file->driver_priv;
102
103 spin_lock(&file_priv->mm.lock);
104 req->file_priv = file_priv;
105 list_add_tail(&req->client_list, &file_priv->mm.request_list);
106 spin_unlock(&file_priv->mm.lock);
107
Chris Wilson05235c52016-07-20 09:21:08 +0100108 return 0;
109}
110
111static inline void
112i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
113{
114 struct drm_i915_file_private *file_priv = request->file_priv;
115
116 if (!file_priv)
117 return;
118
119 spin_lock(&file_priv->mm.lock);
120 list_del(&request->client_list);
121 request->file_priv = NULL;
122 spin_unlock(&file_priv->mm.lock);
Chris Wilson05235c52016-07-20 09:21:08 +0100123}
124
Chris Wilson52e54202016-11-14 20:41:02 +0000125static struct i915_dependency *
126i915_dependency_alloc(struct drm_i915_private *i915)
127{
128 return kmem_cache_alloc(i915->dependencies, GFP_KERNEL);
129}
130
131static void
132i915_dependency_free(struct drm_i915_private *i915,
133 struct i915_dependency *dep)
134{
135 kmem_cache_free(i915->dependencies, dep);
136}
137
138static void
139__i915_priotree_add_dependency(struct i915_priotree *pt,
140 struct i915_priotree *signal,
141 struct i915_dependency *dep,
142 unsigned long flags)
143{
Chris Wilson20311bd2016-11-14 20:41:03 +0000144 INIT_LIST_HEAD(&dep->dfs_link);
Chris Wilson52e54202016-11-14 20:41:02 +0000145 list_add(&dep->wait_link, &signal->waiters_list);
146 list_add(&dep->signal_link, &pt->signalers_list);
147 dep->signaler = signal;
148 dep->flags = flags;
149}
150
151static int
152i915_priotree_add_dependency(struct drm_i915_private *i915,
153 struct i915_priotree *pt,
154 struct i915_priotree *signal)
155{
156 struct i915_dependency *dep;
157
158 dep = i915_dependency_alloc(i915);
159 if (!dep)
160 return -ENOMEM;
161
162 __i915_priotree_add_dependency(pt, signal, dep, I915_DEPENDENCY_ALLOC);
163 return 0;
164}
165
166static void
167i915_priotree_fini(struct drm_i915_private *i915, struct i915_priotree *pt)
168{
169 struct i915_dependency *dep, *next;
170
Chris Wilson20311bd2016-11-14 20:41:03 +0000171 GEM_BUG_ON(!RB_EMPTY_NODE(&pt->node));
172
Chris Wilson52e54202016-11-14 20:41:02 +0000173 /* Everyone we depended upon (the fences we wait to be signaled)
174 * should retire before us and remove themselves from our list.
175 * However, retirement is run independently on each timeline and
176 * so we may be called out-of-order.
177 */
178 list_for_each_entry_safe(dep, next, &pt->signalers_list, signal_link) {
179 list_del(&dep->wait_link);
180 if (dep->flags & I915_DEPENDENCY_ALLOC)
181 i915_dependency_free(i915, dep);
182 }
183
184 /* Remove ourselves from everyone who depends upon us */
185 list_for_each_entry_safe(dep, next, &pt->waiters_list, wait_link) {
186 list_del(&dep->signal_link);
187 if (dep->flags & I915_DEPENDENCY_ALLOC)
188 i915_dependency_free(i915, dep);
189 }
190}
191
192static void
193i915_priotree_init(struct i915_priotree *pt)
194{
195 INIT_LIST_HEAD(&pt->signalers_list);
196 INIT_LIST_HEAD(&pt->waiters_list);
Chris Wilson20311bd2016-11-14 20:41:03 +0000197 RB_CLEAR_NODE(&pt->node);
198 pt->priority = INT_MIN;
Chris Wilson52e54202016-11-14 20:41:02 +0000199}
200
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100201void i915_gem_retire_noop(struct i915_gem_active *active,
202 struct drm_i915_gem_request *request)
203{
204 /* Space left intentionally blank */
205}
206
Chris Wilson05235c52016-07-20 09:21:08 +0100207static void i915_gem_request_retire(struct drm_i915_gem_request *request)
208{
Chris Wilsone8a9c582016-12-18 15:37:20 +0000209 struct intel_engine_cs *engine = request->engine;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100210 struct i915_gem_active *active, *next;
211
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100212 lockdep_assert_held(&request->i915->drm.struct_mutex);
Chris Wilson48bc2a42016-11-25 13:17:17 +0000213 GEM_BUG_ON(!i915_sw_fence_signaled(&request->submit));
214 GEM_BUG_ON(!i915_sw_fence_signaled(&request->execute));
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100215 GEM_BUG_ON(!i915_gem_request_completed(request));
Chris Wilson43020552016-11-15 16:46:20 +0000216 GEM_BUG_ON(!request->i915->gt.active_requests);
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100217
Chris Wilson05235c52016-07-20 09:21:08 +0100218 trace_i915_gem_request_retire(request);
Chris Wilson80b204b2016-10-28 13:58:58 +0100219
Chris Wilsone8a9c582016-12-18 15:37:20 +0000220 spin_lock_irq(&engine->timeline->lock);
Chris Wilsone95433c2016-10-28 13:58:27 +0100221 list_del_init(&request->link);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000222 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson05235c52016-07-20 09:21:08 +0100223
224 /* We know the GPU must have read the request to have
225 * sent us the seqno + interrupt, so use the position
226 * of tail of the request to update the last known position
227 * of the GPU head.
228 *
229 * Note this requires that we are always called in request
230 * completion order.
231 */
Chris Wilson675d9ad2016-08-04 07:52:36 +0100232 list_del(&request->ring_link);
Chris Wilson1dae2df2016-08-02 22:50:19 +0100233 request->ring->last_retired_head = request->postfix;
Chris Wilson43020552016-11-15 16:46:20 +0000234 if (!--request->i915->gt.active_requests) {
235 GEM_BUG_ON(!request->i915->gt.awake);
236 mod_delayed_work(request->i915->wq,
237 &request->i915->gt.idle_work,
238 msecs_to_jiffies(100));
239 }
Chris Wilson05235c52016-07-20 09:21:08 +0100240
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100241 /* Walk through the active list, calling retire on each. This allows
242 * objects to track their GPU activity and mark themselves as idle
243 * when their *last* active request is completed (updating state
244 * tracking lists for eviction, active references for GEM, etc).
245 *
246 * As the ->retire() may free the node, we decouple it first and
247 * pass along the auxiliary information (to avoid dereferencing
248 * the node after the callback).
249 */
250 list_for_each_entry_safe(active, next, &request->active_list, link) {
251 /* In microbenchmarks or focusing upon time inside the kernel,
252 * we may spend an inordinate amount of time simply handling
253 * the retirement of requests and processing their callbacks.
254 * Of which, this loop itself is particularly hot due to the
255 * cache misses when jumping around the list of i915_gem_active.
256 * So we try to keep this loop as streamlined as possible and
257 * also prefetch the next i915_gem_active to try and hide
258 * the likely cache miss.
259 */
260 prefetchw(next);
261
262 INIT_LIST_HEAD(&active->link);
Chris Wilson0eafec62016-08-04 16:32:41 +0100263 RCU_INIT_POINTER(active->request, NULL);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100264
265 active->retire(active, request);
266 }
267
Chris Wilson05235c52016-07-20 09:21:08 +0100268 i915_gem_request_remove_from_client(request);
269
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +0200270 /* Retirement decays the ban score as it is a sign of ctx progress */
Mika Kuoppalabc1d53c2016-11-16 17:20:34 +0200271 if (request->ctx->ban_score > 0)
272 request->ctx->ban_score--;
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +0200273
Chris Wilsone8a9c582016-12-18 15:37:20 +0000274 /* The backing object for the context is done after switching to the
275 * *next* context. Therefore we cannot retire the previous context until
276 * the next context has already started running. However, since we
277 * cannot take the required locks at i915_gem_request_submit() we
278 * defer the unpinning of the active context to now, retirement of
279 * the subsequent request.
280 */
281 if (engine->last_retired_context)
282 engine->context_unpin(engine, engine->last_retired_context);
283 engine->last_retired_context = request->ctx;
Chris Wilsond07f0e52016-10-28 13:58:44 +0100284
285 dma_fence_signal(&request->fence);
Chris Wilson52e54202016-11-14 20:41:02 +0000286
287 i915_priotree_fini(request->i915, &request->priotree);
Chris Wilsone8a261e2016-07-20 13:31:49 +0100288 i915_gem_request_put(request);
Chris Wilson05235c52016-07-20 09:21:08 +0100289}
290
291void i915_gem_request_retire_upto(struct drm_i915_gem_request *req)
292{
293 struct intel_engine_cs *engine = req->engine;
294 struct drm_i915_gem_request *tmp;
295
296 lockdep_assert_held(&req->i915->drm.struct_mutex);
Chris Wilson4ffd6e02016-11-25 13:17:15 +0000297 GEM_BUG_ON(!i915_gem_request_completed(req));
298
Chris Wilsone95433c2016-10-28 13:58:27 +0100299 if (list_empty(&req->link))
300 return;
Chris Wilson05235c52016-07-20 09:21:08 +0100301
302 do {
Chris Wilson73cb9702016-10-28 13:58:46 +0100303 tmp = list_first_entry(&engine->timeline->requests,
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100304 typeof(*tmp), link);
Chris Wilson05235c52016-07-20 09:21:08 +0100305
306 i915_gem_request_retire(tmp);
307 } while (tmp != req);
Chris Wilson05235c52016-07-20 09:21:08 +0100308}
309
Chris Wilson85e17f52016-10-28 13:58:53 +0100310static int i915_gem_init_global_seqno(struct drm_i915_private *i915, u32 seqno)
Chris Wilson05235c52016-07-20 09:21:08 +0100311{
Chris Wilson85e17f52016-10-28 13:58:53 +0100312 struct i915_gem_timeline *timeline = &i915->gt.global_timeline;
Chris Wilson05235c52016-07-20 09:21:08 +0100313 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +0530314 enum intel_engine_id id;
Chris Wilson05235c52016-07-20 09:21:08 +0100315 int ret;
316
317 /* Carefully retire all requests without writing to the rings */
Chris Wilson85e17f52016-10-28 13:58:53 +0100318 ret = i915_gem_wait_for_idle(i915,
Chris Wilson73cb9702016-10-28 13:58:46 +0100319 I915_WAIT_INTERRUPTIBLE |
320 I915_WAIT_LOCKED);
321 if (ret)
322 return ret;
323
Chris Wilson85e17f52016-10-28 13:58:53 +0100324 i915_gem_retire_requests(i915);
Chris Wilson28176ef2016-10-28 13:58:56 +0100325 GEM_BUG_ON(i915->gt.active_requests > 1);
Chris Wilson05235c52016-07-20 09:21:08 +0100326
327 /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
Joonas Lahtinen4c266ed2016-11-24 14:47:49 +0000328 if (!i915_seqno_passed(seqno, atomic_read(&timeline->seqno))) {
Chris Wilson6a5d1db2016-11-08 14:37:19 +0000329 while (intel_breadcrumbs_busy(i915))
330 cond_resched(); /* spin until threads are complete */
Chris Wilson05235c52016-07-20 09:21:08 +0100331 }
Joonas Lahtinen4c266ed2016-11-24 14:47:49 +0000332 atomic_set(&timeline->seqno, seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100333
334 /* Finally reset hw state */
Chris Wilson85e17f52016-10-28 13:58:53 +0100335 for_each_engine(engine, i915, id)
Chris Wilson73cb9702016-10-28 13:58:46 +0100336 intel_engine_init_global_seqno(engine, seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100337
Chris Wilson85e17f52016-10-28 13:58:53 +0100338 list_for_each_entry(timeline, &i915->gt.timelines, link) {
339 for_each_engine(engine, i915, id) {
340 struct intel_timeline *tl = &timeline->engine[id];
341
342 memset(tl->sync_seqno, 0, sizeof(tl->sync_seqno));
343 }
344 }
345
Chris Wilson05235c52016-07-20 09:21:08 +0100346 return 0;
347}
348
Chris Wilson73cb9702016-10-28 13:58:46 +0100349int i915_gem_set_global_seqno(struct drm_device *dev, u32 seqno)
Chris Wilson05235c52016-07-20 09:21:08 +0100350{
351 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson05235c52016-07-20 09:21:08 +0100352
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100353 lockdep_assert_held(&dev_priv->drm.struct_mutex);
354
Chris Wilson05235c52016-07-20 09:21:08 +0100355 if (seqno == 0)
356 return -EINVAL;
357
358 /* HWS page needs to be set less than what we
359 * will inject to ring
360 */
Chris Wilson28176ef2016-10-28 13:58:56 +0100361 return i915_gem_init_global_seqno(dev_priv, seqno - 1);
362}
Chris Wilson05235c52016-07-20 09:21:08 +0100363
Chris Wilson28176ef2016-10-28 13:58:56 +0100364static int reserve_global_seqno(struct drm_i915_private *i915)
365{
366 u32 active_requests = ++i915->gt.active_requests;
Joonas Lahtinen4c266ed2016-11-24 14:47:49 +0000367 u32 seqno = atomic_read(&i915->gt.global_timeline.seqno);
Chris Wilson28176ef2016-10-28 13:58:56 +0100368 int ret;
369
370 /* Reservation is fine until we need to wrap around */
Joonas Lahtinen4c266ed2016-11-24 14:47:49 +0000371 if (likely(seqno + active_requests > seqno))
Chris Wilson28176ef2016-10-28 13:58:56 +0100372 return 0;
373
374 ret = i915_gem_init_global_seqno(i915, 0);
375 if (ret) {
376 i915->gt.active_requests--;
377 return ret;
378 }
379
Chris Wilson05235c52016-07-20 09:21:08 +0100380 return 0;
381}
382
Chris Wilson80b204b2016-10-28 13:58:58 +0100383static u32 __timeline_get_seqno(struct i915_gem_timeline *tl)
384{
Joonas Lahtinen4c266ed2016-11-24 14:47:49 +0000385 /* seqno only incremented under a mutex */
386 return ++tl->seqno.counter;
Chris Wilson80b204b2016-10-28 13:58:58 +0100387}
388
Chris Wilson28176ef2016-10-28 13:58:56 +0100389static u32 timeline_get_seqno(struct i915_gem_timeline *tl)
Chris Wilson05235c52016-07-20 09:21:08 +0100390{
Joonas Lahtinen4c266ed2016-11-24 14:47:49 +0000391 return atomic_inc_return(&tl->seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100392}
393
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000394void __i915_gem_request_submit(struct drm_i915_gem_request *request)
Chris Wilson5590af32016-09-09 14:11:54 +0100395{
Chris Wilson73cb9702016-10-28 13:58:46 +0100396 struct intel_engine_cs *engine = request->engine;
Chris Wilsonf2d13292016-10-28 13:58:57 +0100397 struct intel_timeline *timeline;
398 u32 seqno;
Chris Wilson5590af32016-09-09 14:11:54 +0100399
Chris Wilson80b204b2016-10-28 13:58:58 +0100400 /* Transfer from per-context onto the global per-engine timeline */
401 timeline = engine->timeline;
402 GEM_BUG_ON(timeline == request->timeline);
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000403 assert_spin_locked(&timeline->lock);
Chris Wilson5590af32016-09-09 14:11:54 +0100404
Chris Wilson80b204b2016-10-28 13:58:58 +0100405 seqno = timeline_get_seqno(timeline->common);
Chris Wilsonf2d13292016-10-28 13:58:57 +0100406 GEM_BUG_ON(!seqno);
407 GEM_BUG_ON(i915_seqno_passed(intel_engine_get_seqno(engine), seqno));
408
409 GEM_BUG_ON(i915_seqno_passed(timeline->last_submitted_seqno, seqno));
410 request->previous_seqno = timeline->last_submitted_seqno;
411 timeline->last_submitted_seqno = seqno;
412
413 /* We may be recursing from the signal callback of another i915 fence */
414 spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
415 request->global_seqno = seqno;
416 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
417 intel_engine_enable_signaling(request);
418 spin_unlock(&request->lock);
419
420 GEM_BUG_ON(!request->global_seqno);
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100421 engine->emit_breadcrumb(request,
422 request->ring->vaddr + request->postfix);
Chris Wilson5590af32016-09-09 14:11:54 +0100423
Chris Wilsonbb894852016-11-14 20:40:57 +0000424 spin_lock(&request->timeline->lock);
Chris Wilson80b204b2016-10-28 13:58:58 +0100425 list_move_tail(&request->link, &timeline->requests);
426 spin_unlock(&request->timeline->lock);
427
Chris Wilson23902e42016-11-14 20:40:58 +0000428 i915_sw_fence_commit(&request->execute);
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000429}
Chris Wilson23902e42016-11-14 20:40:58 +0000430
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000431void i915_gem_request_submit(struct drm_i915_gem_request *request)
432{
433 struct intel_engine_cs *engine = request->engine;
434 unsigned long flags;
435
436 /* Will be called from irq-context when using foreign fences. */
437 spin_lock_irqsave(&engine->timeline->lock, flags);
438
439 __i915_gem_request_submit(request);
440
441 spin_unlock_irqrestore(&engine->timeline->lock, flags);
442}
443
444static int __i915_sw_fence_call
445submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
446{
Chris Wilson48bc2a42016-11-25 13:17:17 +0000447 struct drm_i915_gem_request *request =
448 container_of(fence, typeof(*request), submit);
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000449
Chris Wilson48bc2a42016-11-25 13:17:17 +0000450 switch (state) {
451 case FENCE_COMPLETE:
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000452 request->engine->submit_request(request);
Chris Wilson48bc2a42016-11-25 13:17:17 +0000453 break;
454
455 case FENCE_FREE:
456 i915_gem_request_put(request);
457 break;
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000458 }
Chris Wilson80b204b2016-10-28 13:58:58 +0100459
Chris Wilson5590af32016-09-09 14:11:54 +0100460 return NOTIFY_DONE;
461}
462
Chris Wilson23902e42016-11-14 20:40:58 +0000463static int __i915_sw_fence_call
464execute_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
465{
Chris Wilson48bc2a42016-11-25 13:17:17 +0000466 struct drm_i915_gem_request *request =
467 container_of(fence, typeof(*request), execute);
468
469 switch (state) {
470 case FENCE_COMPLETE:
471 break;
472
473 case FENCE_FREE:
474 i915_gem_request_put(request);
475 break;
476 }
477
Chris Wilson23902e42016-11-14 20:40:58 +0000478 return NOTIFY_DONE;
479}
480
Chris Wilson8e637172016-08-02 22:50:26 +0100481/**
482 * i915_gem_request_alloc - allocate a request structure
483 *
484 * @engine: engine that we wish to issue the request on.
485 * @ctx: context that the request will be associated with.
486 * This can be NULL if the request is not directly related to
487 * any specific user context, in which case this function will
488 * choose an appropriate context to use.
489 *
490 * Returns a pointer to the allocated request if successful,
491 * or an error code if not.
492 */
493struct drm_i915_gem_request *
494i915_gem_request_alloc(struct intel_engine_cs *engine,
495 struct i915_gem_context *ctx)
Chris Wilson05235c52016-07-20 09:21:08 +0100496{
497 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson05235c52016-07-20 09:21:08 +0100498 struct drm_i915_gem_request *req;
499 int ret;
500
Chris Wilson28176ef2016-10-28 13:58:56 +0100501 lockdep_assert_held(&dev_priv->drm.struct_mutex);
502
Chris Wilson05235c52016-07-20 09:21:08 +0100503 /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
Chris Wilson6ffb7d02017-01-14 16:23:33 +0000504 * EIO if the GPU is already wedged.
Chris Wilson05235c52016-07-20 09:21:08 +0100505 */
Chris Wilson6ffb7d02017-01-14 16:23:33 +0000506 if (i915_terminally_wedged(&dev_priv->gpu_error))
507 return ERR_PTR(-EIO);
Chris Wilson05235c52016-07-20 09:21:08 +0100508
Chris Wilsone8a9c582016-12-18 15:37:20 +0000509 /* Pinning the contexts may generate requests in order to acquire
510 * GGTT space, so do this first before we reserve a seqno for
511 * ourselves.
512 */
513 ret = engine->context_pin(engine, ctx);
Chris Wilson28176ef2016-10-28 13:58:56 +0100514 if (ret)
515 return ERR_PTR(ret);
516
Chris Wilsone8a9c582016-12-18 15:37:20 +0000517 ret = reserve_global_seqno(dev_priv);
518 if (ret)
519 goto err_unpin;
520
Chris Wilson9b5f4e52016-07-20 09:21:09 +0100521 /* Move the oldest request to the slab-cache (if not in use!) */
Chris Wilson73cb9702016-10-28 13:58:46 +0100522 req = list_first_entry_or_null(&engine->timeline->requests,
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100523 typeof(*req), link);
Chris Wilson80b204b2016-10-28 13:58:58 +0100524 if (req && __i915_gem_request_completed(req))
Chris Wilson2a1d7752016-07-26 12:01:51 +0100525 i915_gem_request_retire(req);
Chris Wilson9b5f4e52016-07-20 09:21:09 +0100526
Chris Wilson5a198b82016-08-09 09:23:34 +0100527 /* Beware: Dragons be flying overhead.
528 *
529 * We use RCU to look up requests in flight. The lookups may
530 * race with the request being allocated from the slab freelist.
531 * That is the request we are writing to here, may be in the process
Chris Wilson1426f712016-08-09 17:03:22 +0100532 * of being read by __i915_gem_active_get_rcu(). As such,
Chris Wilson5a198b82016-08-09 09:23:34 +0100533 * we have to be very careful when overwriting the contents. During
534 * the RCU lookup, we change chase the request->engine pointer,
Chris Wilson65e47602016-10-28 13:58:49 +0100535 * read the request->global_seqno and increment the reference count.
Chris Wilson5a198b82016-08-09 09:23:34 +0100536 *
537 * The reference count is incremented atomically. If it is zero,
538 * the lookup knows the request is unallocated and complete. Otherwise,
539 * it is either still in use, or has been reallocated and reset
Chris Wilsonf54d1862016-10-25 13:00:45 +0100540 * with dma_fence_init(). This increment is safe for release as we
541 * check that the request we have a reference to and matches the active
Chris Wilson5a198b82016-08-09 09:23:34 +0100542 * request.
543 *
544 * Before we increment the refcount, we chase the request->engine
545 * pointer. We must not call kmem_cache_zalloc() or else we set
546 * that pointer to NULL and cause a crash during the lookup. If
547 * we see the request is completed (based on the value of the
548 * old engine and seqno), the lookup is complete and reports NULL.
549 * If we decide the request is not completed (new engine or seqno),
550 * then we grab a reference and double check that it is still the
551 * active request - which it won't be and restart the lookup.
552 *
553 * Do not use kmem_cache_zalloc() here!
554 */
555 req = kmem_cache_alloc(dev_priv->requests, GFP_KERNEL);
Chris Wilson28176ef2016-10-28 13:58:56 +0100556 if (!req) {
557 ret = -ENOMEM;
558 goto err_unreserve;
559 }
Chris Wilson05235c52016-07-20 09:21:08 +0100560
Chris Wilson80b204b2016-10-28 13:58:58 +0100561 req->timeline = i915_gem_context_lookup_timeline(ctx, engine);
562 GEM_BUG_ON(req->timeline == engine->timeline);
Chris Wilson73cb9702016-10-28 13:58:46 +0100563
Chris Wilson04769652016-07-20 09:21:11 +0100564 spin_lock_init(&req->lock);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100565 dma_fence_init(&req->fence,
566 &i915_fence_ops,
567 &req->lock,
Chris Wilson73cb9702016-10-28 13:58:46 +0100568 req->timeline->fence_context,
Chris Wilson80b204b2016-10-28 13:58:58 +0100569 __timeline_get_seqno(req->timeline->common));
Chris Wilson04769652016-07-20 09:21:11 +0100570
Chris Wilson48bc2a42016-11-25 13:17:17 +0000571 /* We bump the ref for the fence chain */
572 i915_sw_fence_init(&i915_gem_request_get(req)->submit, submit_notify);
573 i915_sw_fence_init(&i915_gem_request_get(req)->execute, execute_notify);
574
Chris Wilson23902e42016-11-14 20:40:58 +0000575 /* Ensure that the execute fence completes after the submit fence -
576 * as we complete the execute fence from within the submit fence
577 * callback, its completion would otherwise be visible first.
578 */
579 i915_sw_fence_await_sw_fence(&req->execute, &req->submit, &req->execq);
Chris Wilson5590af32016-09-09 14:11:54 +0100580
Chris Wilson52e54202016-11-14 20:41:02 +0000581 i915_priotree_init(&req->priotree);
582
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100583 INIT_LIST_HEAD(&req->active_list);
Chris Wilson05235c52016-07-20 09:21:08 +0100584 req->i915 = dev_priv;
585 req->engine = engine;
Chris Wilsone8a9c582016-12-18 15:37:20 +0000586 req->ctx = ctx;
Chris Wilson05235c52016-07-20 09:21:08 +0100587
Chris Wilson5a198b82016-08-09 09:23:34 +0100588 /* No zalloc, must clear what we need by hand */
Chris Wilsonf2d13292016-10-28 13:58:57 +0100589 req->global_seqno = 0;
Chris Wilson5a198b82016-08-09 09:23:34 +0100590 req->file_priv = NULL;
Chris Wilson058d88c2016-08-15 10:49:06 +0100591 req->batch = NULL;
Chris Wilson5a198b82016-08-09 09:23:34 +0100592
Chris Wilson05235c52016-07-20 09:21:08 +0100593 /*
594 * Reserve space in the ring buffer for all the commands required to
595 * eventually emit this request. This is to guarantee that the
596 * i915_add_request() call can't fail. Note that the reserve may need
597 * to be redone if the request is not actually submitted straight
598 * away, e.g. because a GPU scheduler has deferred it.
599 */
600 req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
Chris Wilson98f29e82016-10-28 13:58:51 +0100601 GEM_BUG_ON(req->reserved_space < engine->emit_breadcrumb_sz);
Chris Wilson05235c52016-07-20 09:21:08 +0100602
Chris Wilsonf73e7392016-12-18 15:37:24 +0000603 ret = engine->request_alloc(req);
Chris Wilson05235c52016-07-20 09:21:08 +0100604 if (ret)
605 goto err_ctx;
606
Chris Wilsond0454462016-08-15 10:48:40 +0100607 /* Record the position of the start of the request so that
608 * should we detect the updated seqno part-way through the
609 * GPU processing the request, we never over-estimate the
610 * position of the head.
611 */
612 req->head = req->ring->tail;
613
Chris Wilson8e637172016-08-02 22:50:26 +0100614 return req;
Chris Wilson05235c52016-07-20 09:21:08 +0100615
616err_ctx:
Chris Wilson1618bdb2016-11-25 13:17:16 +0000617 /* Make sure we didn't add ourselves to external state before freeing */
618 GEM_BUG_ON(!list_empty(&req->active_list));
619 GEM_BUG_ON(!list_empty(&req->priotree.signalers_list));
620 GEM_BUG_ON(!list_empty(&req->priotree.waiters_list));
621
Chris Wilson05235c52016-07-20 09:21:08 +0100622 kmem_cache_free(dev_priv->requests, req);
Chris Wilson28176ef2016-10-28 13:58:56 +0100623err_unreserve:
624 dev_priv->gt.active_requests--;
Chris Wilsone8a9c582016-12-18 15:37:20 +0000625err_unpin:
626 engine->context_unpin(engine, ctx);
Chris Wilson8e637172016-08-02 22:50:26 +0100627 return ERR_PTR(ret);
Chris Wilson05235c52016-07-20 09:21:08 +0100628}
629
Chris Wilsona2bc4692016-09-09 14:11:56 +0100630static int
631i915_gem_request_await_request(struct drm_i915_gem_request *to,
632 struct drm_i915_gem_request *from)
633{
Chris Wilson85e17f52016-10-28 13:58:53 +0100634 int ret;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100635
636 GEM_BUG_ON(to == from);
637
Chris Wilson52e54202016-11-14 20:41:02 +0000638 if (to->engine->schedule) {
639 ret = i915_priotree_add_dependency(to->i915,
640 &to->priotree,
641 &from->priotree);
642 if (ret < 0)
643 return ret;
644 }
645
Chris Wilson73cb9702016-10-28 13:58:46 +0100646 if (to->timeline == from->timeline)
Chris Wilsona2bc4692016-09-09 14:11:56 +0100647 return 0;
648
Chris Wilson73cb9702016-10-28 13:58:46 +0100649 if (to->engine == from->engine) {
650 ret = i915_sw_fence_await_sw_fence_gfp(&to->submit,
651 &from->submit,
652 GFP_KERNEL);
653 return ret < 0 ? ret : 0;
654 }
655
Chris Wilson65e47602016-10-28 13:58:49 +0100656 if (!from->global_seqno) {
657 ret = i915_sw_fence_await_dma_fence(&to->submit,
658 &from->fence, 0,
659 GFP_KERNEL);
660 return ret < 0 ? ret : 0;
661 }
662
Chris Wilson85e17f52016-10-28 13:58:53 +0100663 if (from->global_seqno <= to->timeline->sync_seqno[from->engine->id])
Chris Wilsona2bc4692016-09-09 14:11:56 +0100664 return 0;
665
666 trace_i915_gem_ring_sync_to(to, from);
667 if (!i915.semaphores) {
Chris Wilson0a046a02016-09-09 14:12:00 +0100668 if (!i915_spin_request(from, TASK_INTERRUPTIBLE, 2)) {
669 ret = i915_sw_fence_await_dma_fence(&to->submit,
670 &from->fence, 0,
671 GFP_KERNEL);
672 if (ret < 0)
673 return ret;
674 }
Chris Wilsona2bc4692016-09-09 14:11:56 +0100675 } else {
676 ret = to->engine->semaphore.sync_to(to, from);
677 if (ret)
678 return ret;
679 }
680
Chris Wilson85e17f52016-10-28 13:58:53 +0100681 to->timeline->sync_seqno[from->engine->id] = from->global_seqno;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100682 return 0;
683}
684
Chris Wilsonb52992c2016-10-28 13:58:24 +0100685int
686i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
687 struct dma_fence *fence)
688{
689 struct dma_fence_array *array;
690 int ret;
691 int i;
692
693 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
694 return 0;
695
696 if (dma_fence_is_i915(fence))
697 return i915_gem_request_await_request(req, to_request(fence));
698
699 if (!dma_fence_is_array(fence)) {
700 ret = i915_sw_fence_await_dma_fence(&req->submit,
701 fence, I915_FENCE_TIMEOUT,
702 GFP_KERNEL);
703 return ret < 0 ? ret : 0;
704 }
705
706 /* Note that if the fence-array was created in signal-on-any mode,
707 * we should *not* decompose it into its individual fences. However,
708 * we don't currently store which mode the fence-array is operating
709 * in. Fortunately, the only user of signal-on-any is private to
710 * amdgpu and we should not see any incoming fence-array from
711 * sync-file being in signal-on-any mode.
712 */
713
714 array = to_dma_fence_array(fence);
715 for (i = 0; i < array->num_fences; i++) {
716 struct dma_fence *child = array->fences[i];
717
718 if (dma_fence_is_i915(child))
719 ret = i915_gem_request_await_request(req,
720 to_request(child));
721 else
722 ret = i915_sw_fence_await_dma_fence(&req->submit,
723 child, I915_FENCE_TIMEOUT,
724 GFP_KERNEL);
725 if (ret < 0)
726 return ret;
727 }
728
729 return 0;
730}
731
Chris Wilsona2bc4692016-09-09 14:11:56 +0100732/**
733 * i915_gem_request_await_object - set this request to (async) wait upon a bo
734 *
735 * @to: request we are wishing to use
736 * @obj: object which may be in use on another ring.
737 *
738 * This code is meant to abstract object synchronization with the GPU.
739 * Conceptually we serialise writes between engines inside the GPU.
740 * We only allow one engine to write into a buffer at any time, but
741 * multiple readers. To ensure each has a coherent view of memory, we must:
742 *
743 * - If there is an outstanding write request to the object, the new
744 * request must wait for it to complete (either CPU or in hw, requests
745 * on the same ring will be naturally ordered).
746 *
747 * - If we are a write request (pending_write_domain is set), the new
748 * request must wait for outstanding read requests to complete.
749 *
750 * Returns 0 if successful, else propagates up the lower layer error.
751 */
752int
753i915_gem_request_await_object(struct drm_i915_gem_request *to,
754 struct drm_i915_gem_object *obj,
755 bool write)
756{
Chris Wilsond07f0e52016-10-28 13:58:44 +0100757 struct dma_fence *excl;
758 int ret = 0;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100759
760 if (write) {
Chris Wilsond07f0e52016-10-28 13:58:44 +0100761 struct dma_fence **shared;
762 unsigned int count, i;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100763
Chris Wilsond07f0e52016-10-28 13:58:44 +0100764 ret = reservation_object_get_fences_rcu(obj->resv,
765 &excl, &count, &shared);
Chris Wilsona2bc4692016-09-09 14:11:56 +0100766 if (ret)
767 return ret;
Chris Wilsond07f0e52016-10-28 13:58:44 +0100768
769 for (i = 0; i < count; i++) {
770 ret = i915_gem_request_await_dma_fence(to, shared[i]);
771 if (ret)
772 break;
773
774 dma_fence_put(shared[i]);
775 }
776
777 for (; i < count; i++)
778 dma_fence_put(shared[i]);
779 kfree(shared);
780 } else {
781 excl = reservation_object_get_excl_rcu(obj->resv);
Chris Wilsona2bc4692016-09-09 14:11:56 +0100782 }
783
Chris Wilsond07f0e52016-10-28 13:58:44 +0100784 if (excl) {
785 if (ret == 0)
786 ret = i915_gem_request_await_dma_fence(to, excl);
787
788 dma_fence_put(excl);
789 }
790
791 return ret;
Chris Wilsona2bc4692016-09-09 14:11:56 +0100792}
793
Chris Wilson05235c52016-07-20 09:21:08 +0100794static void i915_gem_mark_busy(const struct intel_engine_cs *engine)
795{
796 struct drm_i915_private *dev_priv = engine->i915;
797
Chris Wilson05235c52016-07-20 09:21:08 +0100798 if (dev_priv->gt.awake)
799 return;
800
Chris Wilson43020552016-11-15 16:46:20 +0000801 GEM_BUG_ON(!dev_priv->gt.active_requests);
802
Chris Wilson05235c52016-07-20 09:21:08 +0100803 intel_runtime_pm_get_noresume(dev_priv);
804 dev_priv->gt.awake = true;
805
Chris Wilson54b4f682016-07-21 21:16:19 +0100806 intel_enable_gt_powersave(dev_priv);
Chris Wilson05235c52016-07-20 09:21:08 +0100807 i915_update_gfx_val(dev_priv);
808 if (INTEL_GEN(dev_priv) >= 6)
809 gen6_rps_busy(dev_priv);
810
811 queue_delayed_work(dev_priv->wq,
812 &dev_priv->gt.retire_work,
813 round_jiffies_up_relative(HZ));
814}
815
816/*
817 * NB: This function is not allowed to fail. Doing so would mean the the
818 * request is not being tracked for completion but the work itself is
819 * going to happen on the hardware. This would be a Bad Thing(tm).
820 */
Chris Wilson17f298cf2016-08-10 13:41:46 +0100821void __i915_add_request(struct drm_i915_gem_request *request, bool flush_caches)
Chris Wilson05235c52016-07-20 09:21:08 +0100822{
Chris Wilson95b2ab52016-08-15 10:48:46 +0100823 struct intel_engine_cs *engine = request->engine;
824 struct intel_ring *ring = request->ring;
Chris Wilson73cb9702016-10-28 13:58:46 +0100825 struct intel_timeline *timeline = request->timeline;
Chris Wilson0a046a02016-09-09 14:12:00 +0100826 struct drm_i915_gem_request *prev;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000827 u32 *cs;
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100828 int err;
Chris Wilson05235c52016-07-20 09:21:08 +0100829
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100830 lockdep_assert_held(&request->i915->drm.struct_mutex);
Chris Wilson0f25dff2016-09-09 14:11:55 +0100831 trace_i915_gem_request_add(request);
832
Chris Wilsonc781c972017-01-11 14:08:58 +0000833 /* Make sure that no request gazumped us - if it was allocated after
834 * our i915_gem_request_alloc() and called __i915_add_request() before
835 * us, the timeline will hold its seqno which is later than ours.
836 */
837 GEM_BUG_ON(i915_seqno_passed(timeline->last_submitted_seqno,
838 request->fence.seqno));
839
Chris Wilson05235c52016-07-20 09:21:08 +0100840 /*
841 * To ensure that this call will not fail, space for its emissions
842 * should already have been reserved in the ring buffer. Let the ring
843 * know that it is time to use that space up.
844 */
Chris Wilson05235c52016-07-20 09:21:08 +0100845 request->reserved_space = 0;
846
847 /*
848 * Emit any outstanding flushes - execbuf can fail to emit the flush
849 * after having emitted the batchbuffer command. Hence we need to fix
850 * things up similar to emitting the lazy request. The difference here
851 * is that the flush _must_ happen before the next request, no matter
852 * what.
853 */
854 if (flush_caches) {
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100855 err = engine->emit_flush(request, EMIT_FLUSH);
Chris Wilsonc7fe7d22016-08-02 22:50:24 +0100856
Chris Wilson05235c52016-07-20 09:21:08 +0100857 /* Not allowed to fail! */
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100858 WARN(err, "engine->emit_flush() failed: %d!\n", err);
Chris Wilson05235c52016-07-20 09:21:08 +0100859 }
860
Chris Wilsond0454462016-08-15 10:48:40 +0100861 /* Record the position of the start of the breadcrumb so that
Chris Wilson05235c52016-07-20 09:21:08 +0100862 * should we detect the updated seqno part-way through the
863 * GPU processing the request, we never over-estimate the
Chris Wilsond0454462016-08-15 10:48:40 +0100864 * position of the ring's HEAD.
Chris Wilson05235c52016-07-20 09:21:08 +0100865 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000866 cs = intel_ring_begin(request, engine->emit_breadcrumb_sz);
867 GEM_BUG_ON(IS_ERR(cs));
868 request->postfix = intel_ring_offset(request, cs);
Chris Wilson05235c52016-07-20 09:21:08 +0100869
Chris Wilson0f25dff2016-09-09 14:11:55 +0100870 /* Seal the request and mark it as pending execution. Note that
871 * we may inspect this state, without holding any locks, during
872 * hangcheck. Hence we apply the barrier to ensure that we do not
873 * see a more recent value in the hws than we are tracking.
874 */
Chris Wilson0a046a02016-09-09 14:12:00 +0100875
Chris Wilson73cb9702016-10-28 13:58:46 +0100876 prev = i915_gem_active_raw(&timeline->last_request,
Chris Wilson0a046a02016-09-09 14:12:00 +0100877 &request->i915->drm.struct_mutex);
Chris Wilson52e54202016-11-14 20:41:02 +0000878 if (prev) {
Chris Wilson0a046a02016-09-09 14:12:00 +0100879 i915_sw_fence_await_sw_fence(&request->submit, &prev->submit,
880 &request->submitq);
Chris Wilson52e54202016-11-14 20:41:02 +0000881 if (engine->schedule)
882 __i915_priotree_add_dependency(&request->priotree,
883 &prev->priotree,
884 &request->dep,
885 0);
886 }
Chris Wilson0a046a02016-09-09 14:12:00 +0100887
Chris Wilson80b204b2016-10-28 13:58:58 +0100888 spin_lock_irq(&timeline->lock);
Chris Wilsonf2d13292016-10-28 13:58:57 +0100889 list_add_tail(&request->link, &timeline->requests);
Chris Wilson80b204b2016-10-28 13:58:58 +0100890 spin_unlock_irq(&timeline->lock);
Chris Wilson28176ef2016-10-28 13:58:56 +0100891
Chris Wilson80b204b2016-10-28 13:58:58 +0100892 GEM_BUG_ON(i915_seqno_passed(timeline->last_submitted_seqno,
893 request->fence.seqno));
894
895 timeline->last_submitted_seqno = request->fence.seqno;
Chris Wilson73cb9702016-10-28 13:58:46 +0100896 i915_gem_active_set(&timeline->last_request, request);
Chris Wilsonf2d13292016-10-28 13:58:57 +0100897
Chris Wilson0f25dff2016-09-09 14:11:55 +0100898 list_add_tail(&request->ring_link, &ring->request_list);
Chris Wilsonf2d13292016-10-28 13:58:57 +0100899 request->emitted_jiffies = jiffies;
Chris Wilson0f25dff2016-09-09 14:11:55 +0100900
Chris Wilson05235c52016-07-20 09:21:08 +0100901 i915_gem_mark_busy(engine);
Chris Wilson5590af32016-09-09 14:11:54 +0100902
Chris Wilson0de91362016-11-14 20:41:01 +0000903 /* Let the backend know a new request has arrived that may need
904 * to adjust the existing execution schedule due to a high priority
905 * request - i.e. we may want to preempt the current request in order
906 * to run a high priority dependency chain *before* we can execute this
907 * request.
908 *
909 * This is called before the request is ready to run so that we can
910 * decide whether to preempt the entire chain so that it is ready to
911 * run at the earliest possible convenience.
912 */
913 if (engine->schedule)
Chris Wilson9f792eb2016-11-14 20:41:04 +0000914 engine->schedule(request, request->ctx->priority);
Chris Wilson0de91362016-11-14 20:41:01 +0000915
Chris Wilson5590af32016-09-09 14:11:54 +0100916 local_bh_disable();
917 i915_sw_fence_commit(&request->submit);
918 local_bh_enable(); /* Kick the execlists tasklet if just scheduled */
Chris Wilson05235c52016-07-20 09:21:08 +0100919}
920
Chris Wilson221fe792016-09-09 14:11:51 +0100921static void reset_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
922{
923 unsigned long flags;
924
925 spin_lock_irqsave(&q->lock, flags);
926 if (list_empty(&wait->task_list))
927 __add_wait_queue(q, wait);
928 spin_unlock_irqrestore(&q->lock, flags);
929}
930
Chris Wilson05235c52016-07-20 09:21:08 +0100931static unsigned long local_clock_us(unsigned int *cpu)
932{
933 unsigned long t;
934
935 /* Cheaply and approximately convert from nanoseconds to microseconds.
936 * The result and subsequent calculations are also defined in the same
937 * approximate microseconds units. The principal source of timing
938 * error here is from the simple truncation.
939 *
940 * Note that local_clock() is only defined wrt to the current CPU;
941 * the comparisons are no longer valid if we switch CPUs. Instead of
942 * blocking preemption for the entire busywait, we can detect the CPU
943 * switch and use that as indicator of system load and a reason to
944 * stop busywaiting, see busywait_stop().
945 */
946 *cpu = get_cpu();
947 t = local_clock() >> 10;
948 put_cpu();
949
950 return t;
951}
952
953static bool busywait_stop(unsigned long timeout, unsigned int cpu)
954{
955 unsigned int this_cpu;
956
957 if (time_after(local_clock_us(&this_cpu), timeout))
958 return true;
959
960 return this_cpu != cpu;
961}
962
963bool __i915_spin_request(const struct drm_i915_gem_request *req,
964 int state, unsigned long timeout_us)
965{
Chris Wilsonc33ed062017-02-17 15:13:01 +0000966 struct intel_engine_cs *engine = req->engine;
967 unsigned int irq, cpu;
Chris Wilson05235c52016-07-20 09:21:08 +0100968
969 /* When waiting for high frequency requests, e.g. during synchronous
970 * rendering split between the CPU and GPU, the finite amount of time
971 * required to set up the irq and wait upon it limits the response
972 * rate. By busywaiting on the request completion for a short while we
973 * can service the high frequency waits as quick as possible. However,
974 * if it is a slow request, we want to sleep as quickly as possible.
975 * The tradeoff between waiting and sleeping is roughly the time it
976 * takes to sleep on a request, on the order of a microsecond.
977 */
978
Chris Wilsonc33ed062017-02-17 15:13:01 +0000979 irq = atomic_read(&engine->irq_count);
Chris Wilson05235c52016-07-20 09:21:08 +0100980 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
Chris Wilsonc33ed062017-02-17 15:13:01 +0000985 /* Seqno are meant to be ordered *before* the interrupt. If
986 * we see an interrupt without a corresponding seqno advance,
987 * assume we won't see one in the near future but require
988 * the engine->seqno_barrier() to fixup coherency.
989 */
990 if (atomic_read(&engine->irq_count) != irq)
991 break;
992
Chris Wilson05235c52016-07-20 09:21:08 +0100993 if (signal_pending_state(state, current))
994 break;
995
996 if (busywait_stop(timeout_us, cpu))
997 break;
998
Christian Borntraegerf2f09a42016-10-25 11:03:14 +0200999 cpu_relax();
Chris Wilson05235c52016-07-20 09:21:08 +01001000 } while (!need_resched());
1001
1002 return false;
1003}
1004
Chris Wilson4680816b2016-10-28 13:58:48 +01001005static long
Chris Wilson23902e42016-11-14 20:40:58 +00001006__i915_request_wait_for_execute(struct drm_i915_gem_request *request,
1007 unsigned int flags,
1008 long timeout)
Chris Wilson4680816b2016-10-28 13:58:48 +01001009{
1010 const int state = flags & I915_WAIT_INTERRUPTIBLE ?
1011 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
1012 wait_queue_head_t *q = &request->i915->gpu_error.wait_queue;
1013 DEFINE_WAIT(reset);
1014 DEFINE_WAIT(wait);
1015
1016 if (flags & I915_WAIT_LOCKED)
1017 add_wait_queue(q, &reset);
1018
1019 do {
Chris Wilson23902e42016-11-14 20:40:58 +00001020 prepare_to_wait(&request->execute.wait, &wait, state);
Chris Wilson4680816b2016-10-28 13:58:48 +01001021
Chris Wilson23902e42016-11-14 20:40:58 +00001022 if (i915_sw_fence_done(&request->execute))
Chris Wilson4680816b2016-10-28 13:58:48 +01001023 break;
1024
1025 if (flags & I915_WAIT_LOCKED &&
1026 i915_reset_in_progress(&request->i915->gpu_error)) {
1027 __set_current_state(TASK_RUNNING);
1028 i915_reset(request->i915);
1029 reset_wait_queue(q, &reset);
1030 continue;
1031 }
1032
1033 if (signal_pending_state(state, current)) {
1034 timeout = -ERESTARTSYS;
1035 break;
1036 }
1037
Chris Wilson969bb722017-02-08 18:12:38 +00001038 if (!timeout) {
1039 timeout = -ETIME;
1040 break;
1041 }
1042
Chris Wilson4680816b2016-10-28 13:58:48 +01001043 timeout = io_schedule_timeout(timeout);
Chris Wilson969bb722017-02-08 18:12:38 +00001044 } while (1);
Chris Wilson23902e42016-11-14 20:40:58 +00001045 finish_wait(&request->execute.wait, &wait);
Chris Wilson4680816b2016-10-28 13:58:48 +01001046
1047 if (flags & I915_WAIT_LOCKED)
1048 remove_wait_queue(q, &reset);
1049
1050 return timeout;
1051}
1052
Chris Wilson05235c52016-07-20 09:21:08 +01001053/**
Chris Wilson776f3232016-08-04 07:52:40 +01001054 * i915_wait_request - wait until execution of request has finished
Chris Wilsone95433c2016-10-28 13:58:27 +01001055 * @req: the request to wait upon
Chris Wilsonea746f32016-09-09 14:11:49 +01001056 * @flags: how to wait
Chris Wilsone95433c2016-10-28 13:58:27 +01001057 * @timeout: how long to wait in jiffies
Chris Wilson05235c52016-07-20 09:21:08 +01001058 *
Chris Wilsone95433c2016-10-28 13:58:27 +01001059 * i915_wait_request() waits for the request to be completed, for a
1060 * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
1061 * unbounded wait).
Chris Wilson05235c52016-07-20 09:21:08 +01001062 *
Chris Wilsone95433c2016-10-28 13:58:27 +01001063 * If the caller holds the struct_mutex, the caller must pass I915_WAIT_LOCKED
1064 * in via the flags, and vice versa if the struct_mutex is not held, the caller
1065 * must not specify that the wait is locked.
1066 *
1067 * Returns the remaining time (in jiffies) if the request completed, which may
1068 * be zero or -ETIME if the request is unfinished after the timeout expires.
1069 * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
1070 * pending before the request completes.
Chris Wilson05235c52016-07-20 09:21:08 +01001071 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001072long i915_wait_request(struct drm_i915_gem_request *req,
1073 unsigned int flags,
1074 long timeout)
Chris Wilson05235c52016-07-20 09:21:08 +01001075{
Chris Wilsonea746f32016-09-09 14:11:49 +01001076 const int state = flags & I915_WAIT_INTERRUPTIBLE ?
1077 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
Chris Wilson05235c52016-07-20 09:21:08 +01001078 DEFINE_WAIT(reset);
1079 struct intel_wait wait;
Chris Wilson05235c52016-07-20 09:21:08 +01001080
1081 might_sleep();
Chris Wilson22dd3bb2016-09-09 14:11:50 +01001082#if IS_ENABLED(CONFIG_LOCKDEP)
Chris Wilsone95433c2016-10-28 13:58:27 +01001083 GEM_BUG_ON(debug_locks &&
1084 !!lockdep_is_held(&req->i915->drm.struct_mutex) !=
Chris Wilson22dd3bb2016-09-09 14:11:50 +01001085 !!(flags & I915_WAIT_LOCKED));
1086#endif
Chris Wilsone95433c2016-10-28 13:58:27 +01001087 GEM_BUG_ON(timeout < 0);
Chris Wilson05235c52016-07-20 09:21:08 +01001088
Chris Wilson05235c52016-07-20 09:21:08 +01001089 if (i915_gem_request_completed(req))
Chris Wilsone95433c2016-10-28 13:58:27 +01001090 return timeout;
Chris Wilson05235c52016-07-20 09:21:08 +01001091
Chris Wilsone95433c2016-10-28 13:58:27 +01001092 if (!timeout)
1093 return -ETIME;
Chris Wilson05235c52016-07-20 09:21:08 +01001094
Tvrtko Ursulin936925022017-02-21 11:00:24 +00001095 trace_i915_gem_request_wait_begin(req, flags);
Chris Wilson05235c52016-07-20 09:21:08 +01001096
Chris Wilson23902e42016-11-14 20:40:58 +00001097 if (!i915_sw_fence_done(&req->execute)) {
1098 timeout = __i915_request_wait_for_execute(req, flags, timeout);
Chris Wilson4680816b2016-10-28 13:58:48 +01001099 if (timeout < 0)
1100 goto complete;
1101
Chris Wilson23902e42016-11-14 20:40:58 +00001102 GEM_BUG_ON(!i915_sw_fence_done(&req->execute));
Chris Wilson4680816b2016-10-28 13:58:48 +01001103 }
Chris Wilson23902e42016-11-14 20:40:58 +00001104 GEM_BUG_ON(!i915_sw_fence_done(&req->submit));
Chris Wilson65e47602016-10-28 13:58:49 +01001105 GEM_BUG_ON(!req->global_seqno);
Chris Wilson4680816b2016-10-28 13:58:48 +01001106
Daniel Vetter437c3082016-08-05 18:11:24 +02001107 /* Optimistic short spin before touching IRQs */
Chris Wilson05235c52016-07-20 09:21:08 +01001108 if (i915_spin_request(req, state, 5))
1109 goto complete;
1110
1111 set_current_state(state);
Chris Wilson22dd3bb2016-09-09 14:11:50 +01001112 if (flags & I915_WAIT_LOCKED)
1113 add_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
Chris Wilson05235c52016-07-20 09:21:08 +01001114
Chris Wilson65e47602016-10-28 13:58:49 +01001115 intel_wait_init(&wait, req->global_seqno);
Chris Wilson05235c52016-07-20 09:21:08 +01001116 if (intel_engine_add_wait(req->engine, &wait))
1117 /* In order to check that we haven't missed the interrupt
1118 * as we enabled it, we need to kick ourselves to do a
1119 * coherent check on the seqno before we sleep.
1120 */
1121 goto wakeup;
1122
1123 for (;;) {
1124 if (signal_pending_state(state, current)) {
Chris Wilsone95433c2016-10-28 13:58:27 +01001125 timeout = -ERESTARTSYS;
Chris Wilson05235c52016-07-20 09:21:08 +01001126 break;
1127 }
1128
Chris Wilsone95433c2016-10-28 13:58:27 +01001129 if (!timeout) {
1130 timeout = -ETIME;
Chris Wilson05235c52016-07-20 09:21:08 +01001131 break;
1132 }
1133
Chris Wilsone95433c2016-10-28 13:58:27 +01001134 timeout = io_schedule_timeout(timeout);
1135
Chris Wilson05235c52016-07-20 09:21:08 +01001136 if (intel_wait_complete(&wait))
1137 break;
1138
1139 set_current_state(state);
1140
1141wakeup:
1142 /* Carefully check if the request is complete, giving time
1143 * for the seqno to be visible following the interrupt.
1144 * We also have to check in case we are kicked by the GPU
1145 * reset in order to drop the struct_mutex.
1146 */
1147 if (__i915_request_irq_complete(req))
1148 break;
1149
Chris Wilson221fe792016-09-09 14:11:51 +01001150 /* If the GPU is hung, and we hold the lock, reset the GPU
1151 * and then check for completion. On a full reset, the engine's
1152 * HW seqno will be advanced passed us and we are complete.
1153 * If we do a partial reset, we have to wait for the GPU to
1154 * resume and update the breadcrumb.
1155 *
1156 * If we don't hold the mutex, we can just wait for the worker
1157 * to come along and update the breadcrumb (either directly
1158 * itself, or indirectly by recovering the GPU).
1159 */
1160 if (flags & I915_WAIT_LOCKED &&
1161 i915_reset_in_progress(&req->i915->gpu_error)) {
1162 __set_current_state(TASK_RUNNING);
1163 i915_reset(req->i915);
1164 reset_wait_queue(&req->i915->gpu_error.wait_queue,
1165 &reset);
1166 continue;
1167 }
1168
Chris Wilson05235c52016-07-20 09:21:08 +01001169 /* Only spin if we know the GPU is processing this request */
1170 if (i915_spin_request(req, state, 2))
1171 break;
1172 }
Chris Wilson05235c52016-07-20 09:21:08 +01001173
1174 intel_engine_remove_wait(req->engine, &wait);
Chris Wilson22dd3bb2016-09-09 14:11:50 +01001175 if (flags & I915_WAIT_LOCKED)
1176 remove_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
Chris Wilson05235c52016-07-20 09:21:08 +01001177 __set_current_state(TASK_RUNNING);
Chris Wilson22dd3bb2016-09-09 14:11:50 +01001178
Chris Wilson05235c52016-07-20 09:21:08 +01001179complete:
1180 trace_i915_gem_request_wait_end(req);
1181
Chris Wilsone95433c2016-10-28 13:58:27 +01001182 return timeout;
Chris Wilson05235c52016-07-20 09:21:08 +01001183}
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001184
Chris Wilson28176ef2016-10-28 13:58:56 +01001185static void engine_retire_requests(struct intel_engine_cs *engine)
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001186{
1187 struct drm_i915_gem_request *request, *next;
1188
Chris Wilson73cb9702016-10-28 13:58:46 +01001189 list_for_each_entry_safe(request, next,
1190 &engine->timeline->requests, link) {
Chris Wilson80b204b2016-10-28 13:58:58 +01001191 if (!__i915_gem_request_completed(request))
Chris Wilson28176ef2016-10-28 13:58:56 +01001192 return;
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001193
1194 i915_gem_request_retire(request);
1195 }
1196}
1197
1198void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
1199{
1200 struct intel_engine_cs *engine;
Chris Wilson28176ef2016-10-28 13:58:56 +01001201 enum intel_engine_id id;
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001202
1203 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1204
Chris Wilson28176ef2016-10-28 13:58:56 +01001205 if (!dev_priv->gt.active_requests)
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001206 return;
1207
Chris Wilson28176ef2016-10-28 13:58:56 +01001208 for_each_engine(engine, dev_priv, id)
1209 engine_retire_requests(engine);
Chris Wilson4b8de8e2016-08-04 07:52:42 +01001210}
Chris Wilsonc835c552017-02-13 17:15:21 +00001211
1212#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1213#include "selftests/mock_request.c"
1214#include "selftests/i915_gem_request.c"
1215#endif