drm/i915: Defer setting of global seqno on request to submission
[linux-2.6-block.git] / drivers / gpu / drm / i915 / i915_gem_request.c
CommitLineData
05235c53
CW
1/*
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
fa545cbf 25#include <linux/prefetch.h>
b52992c0 26#include <linux/dma-fence-array.h>
fa545cbf 27
05235c53
CW
28#include "i915_drv.h"
29
f54d1867 30static const char *i915_fence_get_driver_name(struct dma_fence *fence)
04769652
CW
31{
32 return "i915";
33}
34
f54d1867 35static const char *i915_fence_get_timeline_name(struct dma_fence *fence)
04769652
CW
36{
37 /* Timelines are bound by eviction to a VM. However, since
38 * we only have a global seqno at the moment, we only have
39 * a single timeline. Note that each timeline will have
40 * multiple execution contexts (fence contexts) as we allow
41 * engines within a single timeline to execute in parallel.
42 */
73cb9701 43 return to_request(fence)->timeline->common->name;
04769652
CW
44}
45
f54d1867 46static bool i915_fence_signaled(struct dma_fence *fence)
04769652
CW
47{
48 return i915_gem_request_completed(to_request(fence));
49}
50
f54d1867 51static bool i915_fence_enable_signaling(struct dma_fence *fence)
04769652
CW
52{
53 if (i915_fence_signaled(fence))
54 return false;
55
56 intel_engine_enable_signaling(to_request(fence));
57 return true;
58}
59
f54d1867 60static signed long i915_fence_wait(struct dma_fence *fence,
04769652 61 bool interruptible,
e95433c7 62 signed long timeout)
04769652 63{
e95433c7 64 return i915_wait_request(to_request(fence), interruptible, timeout);
04769652
CW
65}
66
f54d1867 67static void i915_fence_value_str(struct dma_fence *fence, char *str, int size)
04769652
CW
68{
69 snprintf(str, size, "%u", fence->seqno);
70}
71
f54d1867 72static void i915_fence_timeline_value_str(struct dma_fence *fence, char *str,
04769652
CW
73 int size)
74{
75 snprintf(str, size, "%u",
76 intel_engine_get_seqno(to_request(fence)->engine));
77}
78
f54d1867 79static void i915_fence_release(struct dma_fence *fence)
04769652
CW
80{
81 struct drm_i915_gem_request *req = to_request(fence);
82
83 kmem_cache_free(req->i915->requests, req);
84}
85
f54d1867 86const struct dma_fence_ops i915_fence_ops = {
04769652
CW
87 .get_driver_name = i915_fence_get_driver_name,
88 .get_timeline_name = i915_fence_get_timeline_name,
89 .enable_signaling = i915_fence_enable_signaling,
90 .signaled = i915_fence_signaled,
91 .wait = i915_fence_wait,
92 .release = i915_fence_release,
93 .fence_value_str = i915_fence_value_str,
94 .timeline_value_str = i915_fence_timeline_value_str,
95};
96
05235c53
CW
97int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
98 struct drm_file *file)
99{
100 struct drm_i915_private *dev_private;
101 struct drm_i915_file_private *file_priv;
102
103 WARN_ON(!req || !file || req->file_priv);
104
105 if (!req || !file)
106 return -EINVAL;
107
108 if (req->file_priv)
109 return -EINVAL;
110
111 dev_private = req->i915;
112 file_priv = file->driver_priv;
113
114 spin_lock(&file_priv->mm.lock);
115 req->file_priv = file_priv;
116 list_add_tail(&req->client_list, &file_priv->mm.request_list);
117 spin_unlock(&file_priv->mm.lock);
118
05235c53
CW
119 return 0;
120}
121
122static inline void
123i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
124{
125 struct drm_i915_file_private *file_priv = request->file_priv;
126
127 if (!file_priv)
128 return;
129
130 spin_lock(&file_priv->mm.lock);
131 list_del(&request->client_list);
132 request->file_priv = NULL;
133 spin_unlock(&file_priv->mm.lock);
05235c53
CW
134}
135
fa545cbf
CW
136void i915_gem_retire_noop(struct i915_gem_active *active,
137 struct drm_i915_gem_request *request)
138{
139 /* Space left intentionally blank */
140}
141
05235c53
CW
142static void i915_gem_request_retire(struct drm_i915_gem_request *request)
143{
fa545cbf
CW
144 struct i915_gem_active *active, *next;
145
4c7d62c6
CW
146 lockdep_assert_held(&request->i915->drm.struct_mutex);
147 GEM_BUG_ON(!i915_gem_request_completed(request));
148
05235c53 149 trace_i915_gem_request_retire(request);
e95433c7 150 list_del_init(&request->link);
05235c53
CW
151
152 /* We know the GPU must have read the request to have
153 * sent us the seqno + interrupt, so use the position
154 * of tail of the request to update the last known position
155 * of the GPU head.
156 *
157 * Note this requires that we are always called in request
158 * completion order.
159 */
675d9ad7 160 list_del(&request->ring_link);
1dae2dfb 161 request->ring->last_retired_head = request->postfix;
28176ef4 162 request->i915->gt.active_requests--;
05235c53 163
fa545cbf
CW
164 /* Walk through the active list, calling retire on each. This allows
165 * objects to track their GPU activity and mark themselves as idle
166 * when their *last* active request is completed (updating state
167 * tracking lists for eviction, active references for GEM, etc).
168 *
169 * As the ->retire() may free the node, we decouple it first and
170 * pass along the auxiliary information (to avoid dereferencing
171 * the node after the callback).
172 */
173 list_for_each_entry_safe(active, next, &request->active_list, link) {
174 /* In microbenchmarks or focusing upon time inside the kernel,
175 * we may spend an inordinate amount of time simply handling
176 * the retirement of requests and processing their callbacks.
177 * Of which, this loop itself is particularly hot due to the
178 * cache misses when jumping around the list of i915_gem_active.
179 * So we try to keep this loop as streamlined as possible and
180 * also prefetch the next i915_gem_active to try and hide
181 * the likely cache miss.
182 */
183 prefetchw(next);
184
185 INIT_LIST_HEAD(&active->link);
0eafec6d 186 RCU_INIT_POINTER(active->request, NULL);
fa545cbf
CW
187
188 active->retire(active, request);
189 }
190
05235c53
CW
191 i915_gem_request_remove_from_client(request);
192
193 if (request->previous_context) {
194 if (i915.enable_execlists)
195 intel_lr_context_unpin(request->previous_context,
196 request->engine);
197 }
198
9a6feaf0 199 i915_gem_context_put(request->ctx);
d07f0e59
CW
200
201 dma_fence_signal(&request->fence);
e8a261ea 202 i915_gem_request_put(request);
05235c53
CW
203}
204
205void i915_gem_request_retire_upto(struct drm_i915_gem_request *req)
206{
207 struct intel_engine_cs *engine = req->engine;
208 struct drm_i915_gem_request *tmp;
209
210 lockdep_assert_held(&req->i915->drm.struct_mutex);
e95433c7
CW
211 if (list_empty(&req->link))
212 return;
05235c53
CW
213
214 do {
73cb9701 215 tmp = list_first_entry(&engine->timeline->requests,
efdf7c06 216 typeof(*tmp), link);
05235c53
CW
217
218 i915_gem_request_retire(tmp);
219 } while (tmp != req);
05235c53
CW
220}
221
8af29b0c 222static int i915_gem_check_wedge(struct drm_i915_private *dev_priv)
05235c53 223{
8af29b0c
CW
224 struct i915_gpu_error *error = &dev_priv->gpu_error;
225
226 if (i915_terminally_wedged(error))
05235c53
CW
227 return -EIO;
228
8af29b0c 229 if (i915_reset_in_progress(error)) {
05235c53
CW
230 /* Non-interruptible callers can't handle -EAGAIN, hence return
231 * -EIO unconditionally for these.
232 */
8af29b0c 233 if (!dev_priv->mm.interruptible)
05235c53
CW
234 return -EIO;
235
236 return -EAGAIN;
237 }
238
239 return 0;
240}
241
85e17f59 242static int i915_gem_init_global_seqno(struct drm_i915_private *i915, u32 seqno)
05235c53 243{
85e17f59 244 struct i915_gem_timeline *timeline = &i915->gt.global_timeline;
05235c53 245 struct intel_engine_cs *engine;
3b3f1650 246 enum intel_engine_id id;
05235c53
CW
247 int ret;
248
249 /* Carefully retire all requests without writing to the rings */
85e17f59 250 ret = i915_gem_wait_for_idle(i915,
73cb9701
CW
251 I915_WAIT_INTERRUPTIBLE |
252 I915_WAIT_LOCKED);
253 if (ret)
254 return ret;
255
85e17f59 256 i915_gem_retire_requests(i915);
28176ef4 257 GEM_BUG_ON(i915->gt.active_requests > 1);
05235c53
CW
258
259 /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
28176ef4 260 if (!i915_seqno_passed(seqno, atomic_read(&timeline->next_seqno))) {
85e17f59 261 while (intel_kick_waiters(i915) || intel_kick_signalers(i915))
05235c53 262 yield();
73cb9701 263 yield();
05235c53 264 }
28176ef4 265 atomic_set(&timeline->next_seqno, seqno);
05235c53
CW
266
267 /* Finally reset hw state */
85e17f59 268 for_each_engine(engine, i915, id)
73cb9701 269 intel_engine_init_global_seqno(engine, seqno);
05235c53 270
85e17f59
CW
271 list_for_each_entry(timeline, &i915->gt.timelines, link) {
272 for_each_engine(engine, i915, id) {
273 struct intel_timeline *tl = &timeline->engine[id];
274
275 memset(tl->sync_seqno, 0, sizeof(tl->sync_seqno));
276 }
277 }
278
05235c53
CW
279 return 0;
280}
281
73cb9701 282int i915_gem_set_global_seqno(struct drm_device *dev, u32 seqno)
05235c53
CW
283{
284 struct drm_i915_private *dev_priv = to_i915(dev);
05235c53 285
4c7d62c6
CW
286 lockdep_assert_held(&dev_priv->drm.struct_mutex);
287
05235c53
CW
288 if (seqno == 0)
289 return -EINVAL;
290
291 /* HWS page needs to be set less than what we
292 * will inject to ring
293 */
28176ef4 294 return i915_gem_init_global_seqno(dev_priv, seqno - 1);
05235c53
CW
295}
296
28176ef4 297static int reserve_global_seqno(struct drm_i915_private *i915)
05235c53 298{
28176ef4
CW
299 u32 active_requests = ++i915->gt.active_requests;
300 u32 next_seqno = atomic_read(&i915->gt.global_timeline.next_seqno);
301 int ret;
05235c53 302
28176ef4
CW
303 /* Reservation is fine until we need to wrap around */
304 if (likely(next_seqno + active_requests > next_seqno))
305 return 0;
05235c53 306
28176ef4
CW
307 ret = i915_gem_init_global_seqno(i915, 0);
308 if (ret) {
309 i915->gt.active_requests--;
310 return ret;
05235c53
CW
311 }
312
05235c53
CW
313 return 0;
314}
315
28176ef4
CW
316static u32 timeline_get_seqno(struct i915_gem_timeline *tl)
317{
318 return atomic_inc_return(&tl->next_seqno);
319}
320
5590af3e
CW
321static int __i915_sw_fence_call
322submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
323{
324 struct drm_i915_gem_request *request =
325 container_of(fence, typeof(*request), submit);
73cb9701 326 struct intel_engine_cs *engine = request->engine;
f2d13290
CW
327 struct intel_timeline *timeline;
328 u32 seqno;
5590af3e 329
caddfe71
CW
330 if (state != FENCE_COMPLETE)
331 return NOTIFY_DONE;
332
5590af3e
CW
333 /* Will be called from irq-context when using foreign DMA fences */
334
f2d13290 335 timeline = request->timeline;
5590af3e 336
f2d13290
CW
337 seqno = request->fence.seqno;
338 GEM_BUG_ON(!seqno);
339 GEM_BUG_ON(i915_seqno_passed(intel_engine_get_seqno(engine), seqno));
340
341 GEM_BUG_ON(i915_seqno_passed(timeline->last_submitted_seqno, seqno));
342 request->previous_seqno = timeline->last_submitted_seqno;
343 timeline->last_submitted_seqno = seqno;
344
345 /* We may be recursing from the signal callback of another i915 fence */
346 spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
347 request->global_seqno = seqno;
348 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
349 intel_engine_enable_signaling(request);
350 spin_unlock(&request->lock);
351
352 GEM_BUG_ON(!request->global_seqno);
caddfe71
CW
353 engine->emit_breadcrumb(request,
354 request->ring->vaddr + request->postfix);
355 engine->submit_request(request);
5590af3e
CW
356
357 return NOTIFY_DONE;
358}
359
8e637178
CW
360/**
361 * i915_gem_request_alloc - allocate a request structure
362 *
363 * @engine: engine that we wish to issue the request on.
364 * @ctx: context that the request will be associated with.
365 * This can be NULL if the request is not directly related to
366 * any specific user context, in which case this function will
367 * choose an appropriate context to use.
368 *
369 * Returns a pointer to the allocated request if successful,
370 * or an error code if not.
371 */
372struct drm_i915_gem_request *
373i915_gem_request_alloc(struct intel_engine_cs *engine,
374 struct i915_gem_context *ctx)
05235c53
CW
375{
376 struct drm_i915_private *dev_priv = engine->i915;
05235c53
CW
377 struct drm_i915_gem_request *req;
378 int ret;
379
28176ef4
CW
380 lockdep_assert_held(&dev_priv->drm.struct_mutex);
381
05235c53
CW
382 /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
383 * EIO if the GPU is already wedged, or EAGAIN to drop the struct_mutex
384 * and restart.
385 */
8af29b0c 386 ret = i915_gem_check_wedge(dev_priv);
05235c53 387 if (ret)
8e637178 388 return ERR_PTR(ret);
05235c53 389
28176ef4
CW
390 ret = reserve_global_seqno(dev_priv);
391 if (ret)
392 return ERR_PTR(ret);
393
9b5f4e5e 394 /* Move the oldest request to the slab-cache (if not in use!) */
73cb9701 395 req = list_first_entry_or_null(&engine->timeline->requests,
efdf7c06 396 typeof(*req), link);
2a1d7752
CW
397 if (req && i915_gem_request_completed(req))
398 i915_gem_request_retire(req);
9b5f4e5e 399
5a198b8c
CW
400 /* Beware: Dragons be flying overhead.
401 *
402 * We use RCU to look up requests in flight. The lookups may
403 * race with the request being allocated from the slab freelist.
404 * That is the request we are writing to here, may be in the process
1426f715 405 * of being read by __i915_gem_active_get_rcu(). As such,
5a198b8c
CW
406 * we have to be very careful when overwriting the contents. During
407 * the RCU lookup, we change chase the request->engine pointer,
65e4760e 408 * read the request->global_seqno and increment the reference count.
5a198b8c
CW
409 *
410 * The reference count is incremented atomically. If it is zero,
411 * the lookup knows the request is unallocated and complete. Otherwise,
412 * it is either still in use, or has been reallocated and reset
f54d1867
CW
413 * with dma_fence_init(). This increment is safe for release as we
414 * check that the request we have a reference to and matches the active
5a198b8c
CW
415 * request.
416 *
417 * Before we increment the refcount, we chase the request->engine
418 * pointer. We must not call kmem_cache_zalloc() or else we set
419 * that pointer to NULL and cause a crash during the lookup. If
420 * we see the request is completed (based on the value of the
421 * old engine and seqno), the lookup is complete and reports NULL.
422 * If we decide the request is not completed (new engine or seqno),
423 * then we grab a reference and double check that it is still the
424 * active request - which it won't be and restart the lookup.
425 *
426 * Do not use kmem_cache_zalloc() here!
427 */
428 req = kmem_cache_alloc(dev_priv->requests, GFP_KERNEL);
28176ef4
CW
429 if (!req) {
430 ret = -ENOMEM;
431 goto err_unreserve;
432 }
05235c53 433
73cb9701
CW
434 req->timeline = engine->timeline;
435
04769652 436 spin_lock_init(&req->lock);
f54d1867
CW
437 dma_fence_init(&req->fence,
438 &i915_fence_ops,
439 &req->lock,
73cb9701 440 req->timeline->fence_context,
28176ef4 441 timeline_get_seqno(req->timeline->common));
04769652 442
5590af3e
CW
443 i915_sw_fence_init(&req->submit, submit_notify);
444
fa545cbf 445 INIT_LIST_HEAD(&req->active_list);
05235c53
CW
446 req->i915 = dev_priv;
447 req->engine = engine;
9a6feaf0 448 req->ctx = i915_gem_context_get(ctx);
05235c53 449
5a198b8c 450 /* No zalloc, must clear what we need by hand */
f2d13290 451 req->global_seqno = 0;
5a198b8c
CW
452 req->previous_context = NULL;
453 req->file_priv = NULL;
058d88c4 454 req->batch = NULL;
5a198b8c 455
05235c53
CW
456 /*
457 * Reserve space in the ring buffer for all the commands required to
458 * eventually emit this request. This is to guarantee that the
459 * i915_add_request() call can't fail. Note that the reserve may need
460 * to be redone if the request is not actually submitted straight
461 * away, e.g. because a GPU scheduler has deferred it.
462 */
463 req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
98f29e8d 464 GEM_BUG_ON(req->reserved_space < engine->emit_breadcrumb_sz);
05235c53
CW
465
466 if (i915.enable_execlists)
467 ret = intel_logical_ring_alloc_request_extras(req);
468 else
469 ret = intel_ring_alloc_request_extras(req);
470 if (ret)
471 goto err_ctx;
472
d045446d
CW
473 /* Record the position of the start of the request so that
474 * should we detect the updated seqno part-way through the
475 * GPU processing the request, we never over-estimate the
476 * position of the head.
477 */
478 req->head = req->ring->tail;
479
8e637178 480 return req;
05235c53
CW
481
482err_ctx:
9a6feaf0 483 i915_gem_context_put(ctx);
05235c53 484 kmem_cache_free(dev_priv->requests, req);
28176ef4
CW
485err_unreserve:
486 dev_priv->gt.active_requests--;
8e637178 487 return ERR_PTR(ret);
05235c53
CW
488}
489
a2bc4695
CW
490static int
491i915_gem_request_await_request(struct drm_i915_gem_request *to,
492 struct drm_i915_gem_request *from)
493{
85e17f59 494 int ret;
a2bc4695
CW
495
496 GEM_BUG_ON(to == from);
497
73cb9701 498 if (to->timeline == from->timeline)
a2bc4695
CW
499 return 0;
500
73cb9701
CW
501 if (to->engine == from->engine) {
502 ret = i915_sw_fence_await_sw_fence_gfp(&to->submit,
503 &from->submit,
504 GFP_KERNEL);
505 return ret < 0 ? ret : 0;
506 }
507
65e4760e
CW
508 if (!from->global_seqno) {
509 ret = i915_sw_fence_await_dma_fence(&to->submit,
510 &from->fence, 0,
511 GFP_KERNEL);
512 return ret < 0 ? ret : 0;
513 }
514
85e17f59 515 if (from->global_seqno <= to->timeline->sync_seqno[from->engine->id])
a2bc4695
CW
516 return 0;
517
518 trace_i915_gem_ring_sync_to(to, from);
519 if (!i915.semaphores) {
0a046a0e
CW
520 if (!i915_spin_request(from, TASK_INTERRUPTIBLE, 2)) {
521 ret = i915_sw_fence_await_dma_fence(&to->submit,
522 &from->fence, 0,
523 GFP_KERNEL);
524 if (ret < 0)
525 return ret;
526 }
a2bc4695
CW
527 } else {
528 ret = to->engine->semaphore.sync_to(to, from);
529 if (ret)
530 return ret;
531 }
532
85e17f59 533 to->timeline->sync_seqno[from->engine->id] = from->global_seqno;
a2bc4695
CW
534 return 0;
535}
536
b52992c0
CW
537int
538i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
539 struct dma_fence *fence)
540{
541 struct dma_fence_array *array;
542 int ret;
543 int i;
544
545 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
546 return 0;
547
548 if (dma_fence_is_i915(fence))
549 return i915_gem_request_await_request(req, to_request(fence));
550
551 if (!dma_fence_is_array(fence)) {
552 ret = i915_sw_fence_await_dma_fence(&req->submit,
553 fence, I915_FENCE_TIMEOUT,
554 GFP_KERNEL);
555 return ret < 0 ? ret : 0;
556 }
557
558 /* Note that if the fence-array was created in signal-on-any mode,
559 * we should *not* decompose it into its individual fences. However,
560 * we don't currently store which mode the fence-array is operating
561 * in. Fortunately, the only user of signal-on-any is private to
562 * amdgpu and we should not see any incoming fence-array from
563 * sync-file being in signal-on-any mode.
564 */
565
566 array = to_dma_fence_array(fence);
567 for (i = 0; i < array->num_fences; i++) {
568 struct dma_fence *child = array->fences[i];
569
570 if (dma_fence_is_i915(child))
571 ret = i915_gem_request_await_request(req,
572 to_request(child));
573 else
574 ret = i915_sw_fence_await_dma_fence(&req->submit,
575 child, I915_FENCE_TIMEOUT,
576 GFP_KERNEL);
577 if (ret < 0)
578 return ret;
579 }
580
581 return 0;
582}
583
a2bc4695
CW
584/**
585 * i915_gem_request_await_object - set this request to (async) wait upon a bo
586 *
587 * @to: request we are wishing to use
588 * @obj: object which may be in use on another ring.
589 *
590 * This code is meant to abstract object synchronization with the GPU.
591 * Conceptually we serialise writes between engines inside the GPU.
592 * We only allow one engine to write into a buffer at any time, but
593 * multiple readers. To ensure each has a coherent view of memory, we must:
594 *
595 * - If there is an outstanding write request to the object, the new
596 * request must wait for it to complete (either CPU or in hw, requests
597 * on the same ring will be naturally ordered).
598 *
599 * - If we are a write request (pending_write_domain is set), the new
600 * request must wait for outstanding read requests to complete.
601 *
602 * Returns 0 if successful, else propagates up the lower layer error.
603 */
604int
605i915_gem_request_await_object(struct drm_i915_gem_request *to,
606 struct drm_i915_gem_object *obj,
607 bool write)
608{
d07f0e59
CW
609 struct dma_fence *excl;
610 int ret = 0;
a2bc4695
CW
611
612 if (write) {
d07f0e59
CW
613 struct dma_fence **shared;
614 unsigned int count, i;
615
616 ret = reservation_object_get_fences_rcu(obj->resv,
617 &excl, &count, &shared);
618 if (ret)
619 return ret;
620
621 for (i = 0; i < count; i++) {
622 ret = i915_gem_request_await_dma_fence(to, shared[i]);
623 if (ret)
624 break;
625
626 dma_fence_put(shared[i]);
627 }
628
629 for (; i < count; i++)
630 dma_fence_put(shared[i]);
631 kfree(shared);
a2bc4695 632 } else {
d07f0e59 633 excl = reservation_object_get_excl_rcu(obj->resv);
a2bc4695
CW
634 }
635
d07f0e59
CW
636 if (excl) {
637 if (ret == 0)
638 ret = i915_gem_request_await_dma_fence(to, excl);
a2bc4695 639
d07f0e59 640 dma_fence_put(excl);
a2bc4695
CW
641 }
642
d07f0e59 643 return ret;
a2bc4695
CW
644}
645
05235c53
CW
646static void i915_gem_mark_busy(const struct intel_engine_cs *engine)
647{
648 struct drm_i915_private *dev_priv = engine->i915;
649
05235c53
CW
650 if (dev_priv->gt.awake)
651 return;
652
653 intel_runtime_pm_get_noresume(dev_priv);
654 dev_priv->gt.awake = true;
655
54b4f68f 656 intel_enable_gt_powersave(dev_priv);
05235c53
CW
657 i915_update_gfx_val(dev_priv);
658 if (INTEL_GEN(dev_priv) >= 6)
659 gen6_rps_busy(dev_priv);
660
661 queue_delayed_work(dev_priv->wq,
662 &dev_priv->gt.retire_work,
663 round_jiffies_up_relative(HZ));
664}
665
666/*
667 * NB: This function is not allowed to fail. Doing so would mean the the
668 * request is not being tracked for completion but the work itself is
669 * going to happen on the hardware. This would be a Bad Thing(tm).
670 */
17f298cf 671void __i915_add_request(struct drm_i915_gem_request *request, bool flush_caches)
05235c53 672{
95b2ab56
CW
673 struct intel_engine_cs *engine = request->engine;
674 struct intel_ring *ring = request->ring;
73cb9701 675 struct intel_timeline *timeline = request->timeline;
0a046a0e 676 struct drm_i915_gem_request *prev;
caddfe71 677 int err;
05235c53 678
4c7d62c6 679 lockdep_assert_held(&request->i915->drm.struct_mutex);
0f25dff6
CW
680 trace_i915_gem_request_add(request);
681
05235c53
CW
682 /*
683 * To ensure that this call will not fail, space for its emissions
684 * should already have been reserved in the ring buffer. Let the ring
685 * know that it is time to use that space up.
686 */
05235c53
CW
687 request->reserved_space = 0;
688
689 /*
690 * Emit any outstanding flushes - execbuf can fail to emit the flush
691 * after having emitted the batchbuffer command. Hence we need to fix
692 * things up similar to emitting the lazy request. The difference here
693 * is that the flush _must_ happen before the next request, no matter
694 * what.
695 */
696 if (flush_caches) {
caddfe71 697 err = engine->emit_flush(request, EMIT_FLUSH);
c7fe7d25 698
05235c53 699 /* Not allowed to fail! */
caddfe71 700 WARN(err, "engine->emit_flush() failed: %d!\n", err);
05235c53
CW
701 }
702
d045446d 703 /* Record the position of the start of the breadcrumb so that
05235c53
CW
704 * should we detect the updated seqno part-way through the
705 * GPU processing the request, we never over-estimate the
d045446d 706 * position of the ring's HEAD.
05235c53 707 */
caddfe71
CW
708 err = intel_ring_begin(request, engine->emit_breadcrumb_sz);
709 GEM_BUG_ON(err);
ba76d91b 710 request->postfix = ring->tail;
caddfe71 711 ring->tail += engine->emit_breadcrumb_sz * sizeof(u32);
05235c53 712
0f25dff6
CW
713 /* Seal the request and mark it as pending execution. Note that
714 * we may inspect this state, without holding any locks, during
715 * hangcheck. Hence we apply the barrier to ensure that we do not
716 * see a more recent value in the hws than we are tracking.
717 */
0a046a0e 718
73cb9701 719 prev = i915_gem_active_raw(&timeline->last_request,
0a046a0e
CW
720 &request->i915->drm.struct_mutex);
721 if (prev)
722 i915_sw_fence_await_sw_fence(&request->submit, &prev->submit,
723 &request->submitq);
724
f2d13290 725 list_add_tail(&request->link, &timeline->requests);
28176ef4 726
73cb9701
CW
727 timeline->last_pending_seqno = request->fence.seqno;
728 i915_gem_active_set(&timeline->last_request, request);
f2d13290 729
0f25dff6 730 list_add_tail(&request->ring_link, &ring->request_list);
f2d13290 731 request->emitted_jiffies = jiffies;
0f25dff6 732
05235c53 733 i915_gem_mark_busy(engine);
5590af3e
CW
734
735 local_bh_disable();
736 i915_sw_fence_commit(&request->submit);
737 local_bh_enable(); /* Kick the execlists tasklet if just scheduled */
05235c53
CW
738}
739
221fe799
CW
740static void reset_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
741{
742 unsigned long flags;
743
744 spin_lock_irqsave(&q->lock, flags);
745 if (list_empty(&wait->task_list))
746 __add_wait_queue(q, wait);
747 spin_unlock_irqrestore(&q->lock, flags);
748}
749
05235c53
CW
750static unsigned long local_clock_us(unsigned int *cpu)
751{
752 unsigned long t;
753
754 /* Cheaply and approximately convert from nanoseconds to microseconds.
755 * The result and subsequent calculations are also defined in the same
756 * approximate microseconds units. The principal source of timing
757 * error here is from the simple truncation.
758 *
759 * Note that local_clock() is only defined wrt to the current CPU;
760 * the comparisons are no longer valid if we switch CPUs. Instead of
761 * blocking preemption for the entire busywait, we can detect the CPU
762 * switch and use that as indicator of system load and a reason to
763 * stop busywaiting, see busywait_stop().
764 */
765 *cpu = get_cpu();
766 t = local_clock() >> 10;
767 put_cpu();
768
769 return t;
770}
771
772static bool busywait_stop(unsigned long timeout, unsigned int cpu)
773{
774 unsigned int this_cpu;
775
776 if (time_after(local_clock_us(&this_cpu), timeout))
777 return true;
778
779 return this_cpu != cpu;
780}
781
782bool __i915_spin_request(const struct drm_i915_gem_request *req,
783 int state, unsigned long timeout_us)
784{
785 unsigned int cpu;
786
787 /* When waiting for high frequency requests, e.g. during synchronous
788 * rendering split between the CPU and GPU, the finite amount of time
789 * required to set up the irq and wait upon it limits the response
790 * rate. By busywaiting on the request completion for a short while we
791 * can service the high frequency waits as quick as possible. However,
792 * if it is a slow request, we want to sleep as quickly as possible.
793 * The tradeoff between waiting and sleeping is roughly the time it
794 * takes to sleep on a request, on the order of a microsecond.
795 */
796
797 timeout_us += local_clock_us(&cpu);
798 do {
65e4760e 799 if (__i915_gem_request_completed(req))
05235c53
CW
800 return true;
801
802 if (signal_pending_state(state, current))
803 break;
804
805 if (busywait_stop(timeout_us, cpu))
806 break;
807
808 cpu_relax_lowlatency();
809 } while (!need_resched());
810
811 return false;
812}
813
4680816b
CW
814static long
815__i915_request_wait_for_submit(struct drm_i915_gem_request *request,
816 unsigned int flags,
817 long timeout)
818{
819 const int state = flags & I915_WAIT_INTERRUPTIBLE ?
820 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
821 wait_queue_head_t *q = &request->i915->gpu_error.wait_queue;
822 DEFINE_WAIT(reset);
823 DEFINE_WAIT(wait);
824
825 if (flags & I915_WAIT_LOCKED)
826 add_wait_queue(q, &reset);
827
828 do {
829 prepare_to_wait(&request->submit.wait, &wait, state);
830
831 if (i915_sw_fence_done(&request->submit))
832 break;
833
834 if (flags & I915_WAIT_LOCKED &&
835 i915_reset_in_progress(&request->i915->gpu_error)) {
836 __set_current_state(TASK_RUNNING);
837 i915_reset(request->i915);
838 reset_wait_queue(q, &reset);
839 continue;
840 }
841
842 if (signal_pending_state(state, current)) {
843 timeout = -ERESTARTSYS;
844 break;
845 }
846
847 timeout = io_schedule_timeout(timeout);
848 } while (timeout);
849 finish_wait(&request->submit.wait, &wait);
850
851 if (flags & I915_WAIT_LOCKED)
852 remove_wait_queue(q, &reset);
853
854 return timeout;
855}
856
05235c53 857/**
776f3236 858 * i915_wait_request - wait until execution of request has finished
e95433c7 859 * @req: the request to wait upon
ea746f36 860 * @flags: how to wait
e95433c7
CW
861 * @timeout: how long to wait in jiffies
862 *
863 * i915_wait_request() waits for the request to be completed, for a
864 * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
865 * unbounded wait).
05235c53 866 *
e95433c7
CW
867 * If the caller holds the struct_mutex, the caller must pass I915_WAIT_LOCKED
868 * in via the flags, and vice versa if the struct_mutex is not held, the caller
869 * must not specify that the wait is locked.
05235c53 870 *
e95433c7
CW
871 * Returns the remaining time (in jiffies) if the request completed, which may
872 * be zero or -ETIME if the request is unfinished after the timeout expires.
873 * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
874 * pending before the request completes.
05235c53 875 */
e95433c7
CW
876long i915_wait_request(struct drm_i915_gem_request *req,
877 unsigned int flags,
878 long timeout)
05235c53 879{
ea746f36
CW
880 const int state = flags & I915_WAIT_INTERRUPTIBLE ?
881 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
05235c53
CW
882 DEFINE_WAIT(reset);
883 struct intel_wait wait;
05235c53
CW
884
885 might_sleep();
22dd3bb9 886#if IS_ENABLED(CONFIG_LOCKDEP)
e95433c7
CW
887 GEM_BUG_ON(debug_locks &&
888 !!lockdep_is_held(&req->i915->drm.struct_mutex) !=
22dd3bb9
CW
889 !!(flags & I915_WAIT_LOCKED));
890#endif
e95433c7 891 GEM_BUG_ON(timeout < 0);
05235c53 892
05235c53 893 if (i915_gem_request_completed(req))
e95433c7 894 return timeout;
05235c53 895
e95433c7
CW
896 if (!timeout)
897 return -ETIME;
05235c53
CW
898
899 trace_i915_gem_request_wait_begin(req);
900
4680816b
CW
901 if (!i915_sw_fence_done(&req->submit)) {
902 timeout = __i915_request_wait_for_submit(req, flags, timeout);
903 if (timeout < 0)
904 goto complete;
905
906 GEM_BUG_ON(!i915_sw_fence_done(&req->submit));
907 }
65e4760e 908 GEM_BUG_ON(!req->global_seqno);
4680816b 909
437c3087 910 /* Optimistic short spin before touching IRQs */
05235c53
CW
911 if (i915_spin_request(req, state, 5))
912 goto complete;
913
914 set_current_state(state);
22dd3bb9
CW
915 if (flags & I915_WAIT_LOCKED)
916 add_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
05235c53 917
65e4760e 918 intel_wait_init(&wait, req->global_seqno);
05235c53
CW
919 if (intel_engine_add_wait(req->engine, &wait))
920 /* In order to check that we haven't missed the interrupt
921 * as we enabled it, we need to kick ourselves to do a
922 * coherent check on the seqno before we sleep.
923 */
924 goto wakeup;
925
926 for (;;) {
927 if (signal_pending_state(state, current)) {
e95433c7 928 timeout = -ERESTARTSYS;
05235c53
CW
929 break;
930 }
931
e95433c7
CW
932 if (!timeout) {
933 timeout = -ETIME;
05235c53
CW
934 break;
935 }
936
e95433c7
CW
937 timeout = io_schedule_timeout(timeout);
938
05235c53
CW
939 if (intel_wait_complete(&wait))
940 break;
941
942 set_current_state(state);
943
944wakeup:
945 /* Carefully check if the request is complete, giving time
946 * for the seqno to be visible following the interrupt.
947 * We also have to check in case we are kicked by the GPU
948 * reset in order to drop the struct_mutex.
949 */
950 if (__i915_request_irq_complete(req))
951 break;
952
221fe799
CW
953 /* If the GPU is hung, and we hold the lock, reset the GPU
954 * and then check for completion. On a full reset, the engine's
955 * HW seqno will be advanced passed us and we are complete.
956 * If we do a partial reset, we have to wait for the GPU to
957 * resume and update the breadcrumb.
958 *
959 * If we don't hold the mutex, we can just wait for the worker
960 * to come along and update the breadcrumb (either directly
961 * itself, or indirectly by recovering the GPU).
962 */
963 if (flags & I915_WAIT_LOCKED &&
964 i915_reset_in_progress(&req->i915->gpu_error)) {
965 __set_current_state(TASK_RUNNING);
966 i915_reset(req->i915);
967 reset_wait_queue(&req->i915->gpu_error.wait_queue,
968 &reset);
969 continue;
970 }
971
05235c53
CW
972 /* Only spin if we know the GPU is processing this request */
973 if (i915_spin_request(req, state, 2))
974 break;
975 }
05235c53
CW
976
977 intel_engine_remove_wait(req->engine, &wait);
22dd3bb9
CW
978 if (flags & I915_WAIT_LOCKED)
979 remove_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
05235c53 980 __set_current_state(TASK_RUNNING);
22dd3bb9 981
05235c53
CW
982complete:
983 trace_i915_gem_request_wait_end(req);
984
e95433c7 985 return timeout;
05235c53 986}
4b8de8e6 987
28176ef4 988static void engine_retire_requests(struct intel_engine_cs *engine)
4b8de8e6
CW
989{
990 struct drm_i915_gem_request *request, *next;
991
73cb9701
CW
992 list_for_each_entry_safe(request, next,
993 &engine->timeline->requests, link) {
4b8de8e6 994 if (!i915_gem_request_completed(request))
28176ef4 995 return;
4b8de8e6
CW
996
997 i915_gem_request_retire(request);
998 }
999}
1000
1001void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
1002{
1003 struct intel_engine_cs *engine;
28176ef4 1004 enum intel_engine_id id;
4b8de8e6
CW
1005
1006 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1007
28176ef4 1008 if (!dev_priv->gt.active_requests)
4b8de8e6
CW
1009 return;
1010
1011 GEM_BUG_ON(!dev_priv->gt.awake);
1012
28176ef4
CW
1013 for_each_engine(engine, dev_priv, id)
1014 engine_retire_requests(engine);
4b8de8e6 1015
28176ef4 1016 if (!dev_priv->gt.active_requests)
4b8de8e6
CW
1017 queue_delayed_work(dev_priv->wq,
1018 &dev_priv->gt.idle_work,
1019 msecs_to_jiffies(100));
1020}