drm/i915: Clean up DSC vs. not bpp handling
[linux-2.6-block.git] / drivers / gpu / drm / i915 / i915_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
b52992c0 25#include <linux/dma-fence-array.h>
e8861964
CW
26#include <linux/irq_work.h>
27#include <linux/prefetch.h>
e6017571
IM
28#include <linux/sched.h>
29#include <linux/sched/clock.h>
f361bf4a 30#include <linux/sched/signal.h>
fa545cbf 31
21950ee7 32#include "i915_active.h"
696173b0 33#include "i915_drv.h"
103b76ee 34#include "i915_globals.h"
9f58892e 35#include "i915_reset.h"
696173b0 36#include "intel_pm.h"
05235c53 37
e8861964
CW
38struct execute_cb {
39 struct list_head link;
40 struct irq_work work;
41 struct i915_sw_fence *fence;
42};
43
32eb6bcf 44static struct i915_global_request {
103b76ee 45 struct i915_global base;
32eb6bcf
CW
46 struct kmem_cache *slab_requests;
47 struct kmem_cache *slab_dependencies;
e8861964 48 struct kmem_cache *slab_execute_cbs;
32eb6bcf
CW
49} global;
50
f54d1867 51static const char *i915_fence_get_driver_name(struct dma_fence *fence)
04769652
CW
52{
53 return "i915";
54}
55
f54d1867 56static const char *i915_fence_get_timeline_name(struct dma_fence *fence)
04769652 57{
e61e0f51
CW
58 /*
59 * The timeline struct (as part of the ppgtt underneath a context)
05506b5b
CW
60 * may be freed when the request is no longer in use by the GPU.
61 * We could extend the life of a context to beyond that of all
62 * fences, possibly keeping the hw resource around indefinitely,
63 * or we just give them a false name. Since
64 * dma_fence_ops.get_timeline_name is a debug feature, the occasional
65 * lie seems justifiable.
66 */
67 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
68 return "signaled";
69
4daffb66 70 return to_request(fence)->gem_context->name ?: "[i915]";
04769652
CW
71}
72
f54d1867 73static bool i915_fence_signaled(struct dma_fence *fence)
04769652 74{
e61e0f51 75 return i915_request_completed(to_request(fence));
04769652
CW
76}
77
f54d1867 78static bool i915_fence_enable_signaling(struct dma_fence *fence)
04769652 79{
52c0fdb2 80 return i915_request_enable_breadcrumb(to_request(fence));
04769652
CW
81}
82
f54d1867 83static signed long i915_fence_wait(struct dma_fence *fence,
04769652 84 bool interruptible,
e95433c7 85 signed long timeout)
04769652 86{
62eb3c24
CW
87 return i915_request_wait(to_request(fence),
88 interruptible | I915_WAIT_PRIORITY,
89 timeout);
04769652
CW
90}
91
f54d1867 92static void i915_fence_release(struct dma_fence *fence)
04769652 93{
e61e0f51 94 struct i915_request *rq = to_request(fence);
04769652 95
e61e0f51
CW
96 /*
97 * The request is put onto a RCU freelist (i.e. the address
fc158405
CW
98 * is immediately reused), mark the fences as being freed now.
99 * Otherwise the debugobjects for the fences are only marked as
100 * freed when the slab cache itself is freed, and so we would get
101 * caught trying to reuse dead objects.
102 */
e61e0f51 103 i915_sw_fence_fini(&rq->submit);
fc158405 104
32eb6bcf 105 kmem_cache_free(global.slab_requests, rq);
04769652
CW
106}
107
f54d1867 108const struct dma_fence_ops i915_fence_ops = {
04769652
CW
109 .get_driver_name = i915_fence_get_driver_name,
110 .get_timeline_name = i915_fence_get_timeline_name,
111 .enable_signaling = i915_fence_enable_signaling,
112 .signaled = i915_fence_signaled,
113 .wait = i915_fence_wait,
114 .release = i915_fence_release,
04769652
CW
115};
116
05235c53 117static inline void
e61e0f51 118i915_request_remove_from_client(struct i915_request *request)
05235c53 119{
c8659efa 120 struct drm_i915_file_private *file_priv;
05235c53 121
c8659efa 122 file_priv = request->file_priv;
05235c53
CW
123 if (!file_priv)
124 return;
125
126 spin_lock(&file_priv->mm.lock);
c8659efa
CW
127 if (request->file_priv) {
128 list_del(&request->client_link);
129 request->file_priv = NULL;
130 }
05235c53 131 spin_unlock(&file_priv->mm.lock);
05235c53
CW
132}
133
6faf5916 134static void reserve_gt(struct drm_i915_private *i915)
12d3173b 135{
636918f1 136 if (!i915->gt.active_requests++)
e4d2006f 137 i915_gem_unpark(i915);
12d3173b
CW
138}
139
52d7f16e 140static void unreserve_gt(struct drm_i915_private *i915)
9b6586ae 141{
b887d615 142 GEM_BUG_ON(!i915->gt.active_requests);
e4d2006f
CW
143 if (!--i915->gt.active_requests)
144 i915_gem_park(i915);
9b6586ae
CW
145}
146
e61e0f51 147static void advance_ring(struct i915_request *request)
cbb60b4b 148{
b887d615 149 struct intel_ring *ring = request->ring;
cbb60b4b
CW
150 unsigned int tail;
151
e61e0f51
CW
152 /*
153 * We know the GPU must have read the request to have
cbb60b4b
CW
154 * sent us the seqno + interrupt, so use the position
155 * of tail of the request to update the last known position
156 * of the GPU head.
157 *
158 * Note this requires that we are always called in request
159 * completion order.
160 */
b887d615
CW
161 GEM_BUG_ON(!list_is_first(&request->ring_link, &ring->request_list));
162 if (list_is_last(&request->ring_link, &ring->request_list)) {
e61e0f51
CW
163 /*
164 * We may race here with execlists resubmitting this request
e6ba9992
CW
165 * as we retire it. The resubmission will move the ring->tail
166 * forwards (to request->wa_tail). We either read the
167 * current value that was written to hw, or the value that
168 * is just about to be. Either works, if we miss the last two
169 * noops - they are safe to be replayed on a reset.
170 */
36620032 171 tail = READ_ONCE(request->tail);
643b450a 172 list_del(&ring->active_link);
e6ba9992 173 } else {
cbb60b4b 174 tail = request->postfix;
e6ba9992 175 }
b887d615 176 list_del_init(&request->ring_link);
cbb60b4b 177
b887d615 178 ring->head = tail;
cbb60b4b
CW
179}
180
e61e0f51 181static void free_capture_list(struct i915_request *request)
b0fd47ad 182{
e61e0f51 183 struct i915_capture_list *capture;
b0fd47ad
CW
184
185 capture = request->capture_list;
186 while (capture) {
e61e0f51 187 struct i915_capture_list *next = capture->next;
b0fd47ad
CW
188
189 kfree(capture);
190 capture = next;
191 }
192}
193
b887d615
CW
194static void __retire_engine_request(struct intel_engine_cs *engine,
195 struct i915_request *rq)
196{
b300fde8 197 GEM_TRACE("%s(%s) fence %llx:%lld, current %d\n",
b887d615
CW
198 __func__, engine->name,
199 rq->fence.context, rq->fence.seqno,
8892f477 200 hwsp_seqno(rq));
b887d615
CW
201
202 GEM_BUG_ON(!i915_request_completed(rq));
203
204 local_irq_disable();
205
a89d1f92
CW
206 spin_lock(&engine->timeline.lock);
207 GEM_BUG_ON(!list_is_first(&rq->link, &engine->timeline.requests));
b887d615 208 list_del_init(&rq->link);
a89d1f92 209 spin_unlock(&engine->timeline.lock);
b887d615
CW
210
211 spin_lock(&rq->lock);
5013eb8c 212 i915_request_mark_complete(rq);
0e21834e 213 if (!i915_request_signaled(rq))
b887d615
CW
214 dma_fence_signal_locked(&rq->fence);
215 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &rq->fence.flags))
52c0fdb2 216 i915_request_cancel_breadcrumb(rq);
b887d615
CW
217 if (rq->waitboost) {
218 GEM_BUG_ON(!atomic_read(&rq->i915->gt_pm.rps.num_waiters));
219 atomic_dec(&rq->i915->gt_pm.rps.num_waiters);
220 }
221 spin_unlock(&rq->lock);
222
223 local_irq_enable();
224
225 /*
226 * The backing object for the context is done after switching to the
227 * *next* context. Therefore we cannot retire the previous context until
228 * the next context has already started running. However, since we
229 * cannot take the required locks at i915_request_submit() we
230 * defer the unpinning of the active context to now, retirement of
231 * the subsequent request.
232 */
233 if (engine->last_retired_context)
1fc44d9b
CW
234 intel_context_unpin(engine->last_retired_context);
235 engine->last_retired_context = rq->hw_context;
b887d615
CW
236}
237
238static void __retire_engine_upto(struct intel_engine_cs *engine,
239 struct i915_request *rq)
240{
241 struct i915_request *tmp;
242
243 if (list_empty(&rq->link))
244 return;
245
246 do {
a89d1f92 247 tmp = list_first_entry(&engine->timeline.requests,
b887d615
CW
248 typeof(*tmp), link);
249
250 GEM_BUG_ON(tmp->engine != engine);
251 __retire_engine_request(engine, tmp);
252 } while (tmp != rq);
253}
254
e61e0f51 255static void i915_request_retire(struct i915_request *request)
05235c53 256{
21950ee7 257 struct i915_active_request *active, *next;
fa545cbf 258
b300fde8 259 GEM_TRACE("%s fence %llx:%lld, current %d\n",
b887d615 260 request->engine->name,
d9b13c4d 261 request->fence.context, request->fence.seqno,
8892f477 262 hwsp_seqno(request));
d9b13c4d 263
4c7d62c6 264 lockdep_assert_held(&request->i915->drm.struct_mutex);
48bc2a4a 265 GEM_BUG_ON(!i915_sw_fence_signaled(&request->submit));
e61e0f51 266 GEM_BUG_ON(!i915_request_completed(request));
4c7d62c6 267
e61e0f51 268 trace_i915_request_retire(request);
80b204bc 269
cbb60b4b 270 advance_ring(request);
b0fd47ad
CW
271 free_capture_list(request);
272
e61e0f51
CW
273 /*
274 * Walk through the active list, calling retire on each. This allows
fa545cbf
CW
275 * objects to track their GPU activity and mark themselves as idle
276 * when their *last* active request is completed (updating state
277 * tracking lists for eviction, active references for GEM, etc).
278 *
279 * As the ->retire() may free the node, we decouple it first and
280 * pass along the auxiliary information (to avoid dereferencing
281 * the node after the callback).
282 */
283 list_for_each_entry_safe(active, next, &request->active_list, link) {
e61e0f51
CW
284 /*
285 * In microbenchmarks or focusing upon time inside the kernel,
fa545cbf
CW
286 * we may spend an inordinate amount of time simply handling
287 * the retirement of requests and processing their callbacks.
288 * Of which, this loop itself is particularly hot due to the
21950ee7
CW
289 * cache misses when jumping around the list of
290 * i915_active_request. So we try to keep this loop as
291 * streamlined as possible and also prefetch the next
292 * i915_active_request to try and hide the likely cache miss.
fa545cbf
CW
293 */
294 prefetchw(next);
295
296 INIT_LIST_HEAD(&active->link);
0eafec6d 297 RCU_INIT_POINTER(active->request, NULL);
fa545cbf
CW
298
299 active->retire(active, request);
300 }
301
e61e0f51 302 i915_request_remove_from_client(request);
05235c53 303
1fc44d9b 304 intel_context_unpin(request->hw_context);
e5e1fc47 305
b887d615 306 __retire_engine_upto(request->engine, request);
52e54209 307
52d7f16e
CW
308 unreserve_gt(request->i915);
309
32eb6bcf 310 i915_sched_node_fini(&request->sched);
e61e0f51 311 i915_request_put(request);
05235c53
CW
312}
313
e61e0f51 314void i915_request_retire_upto(struct i915_request *rq)
05235c53 315{
b887d615 316 struct intel_ring *ring = rq->ring;
e61e0f51 317 struct i915_request *tmp;
05235c53 318
b300fde8 319 GEM_TRACE("%s fence %llx:%lld, current %d\n",
b887d615
CW
320 rq->engine->name,
321 rq->fence.context, rq->fence.seqno,
8892f477 322 hwsp_seqno(rq));
b887d615 323
e61e0f51
CW
324 lockdep_assert_held(&rq->i915->drm.struct_mutex);
325 GEM_BUG_ON(!i915_request_completed(rq));
4ffd6e0c 326
b887d615 327 if (list_empty(&rq->ring_link))
e95433c7 328 return;
05235c53
CW
329
330 do {
b887d615
CW
331 tmp = list_first_entry(&ring->request_list,
332 typeof(*tmp), ring_link);
05235c53 333
e61e0f51
CW
334 i915_request_retire(tmp);
335 } while (tmp != rq);
05235c53
CW
336}
337
e8861964
CW
338static void irq_execute_cb(struct irq_work *wrk)
339{
340 struct execute_cb *cb = container_of(wrk, typeof(*cb), work);
341
342 i915_sw_fence_complete(cb->fence);
343 kmem_cache_free(global.slab_execute_cbs, cb);
344}
345
346static void __notify_execute_cb(struct i915_request *rq)
347{
348 struct execute_cb *cb;
349
350 lockdep_assert_held(&rq->lock);
351
352 if (list_empty(&rq->execute_cb))
353 return;
354
355 list_for_each_entry(cb, &rq->execute_cb, link)
356 irq_work_queue(&cb->work);
357
358 /*
359 * XXX Rollback on __i915_request_unsubmit()
360 *
361 * In the future, perhaps when we have an active time-slicing scheduler,
362 * it will be interesting to unsubmit parallel execution and remove
363 * busywaits from the GPU until their master is restarted. This is
364 * quite hairy, we have to carefully rollback the fence and do a
365 * preempt-to-idle cycle on the target engine, all the while the
366 * master execute_cb may refire.
367 */
368 INIT_LIST_HEAD(&rq->execute_cb);
369}
370
371static int
372i915_request_await_execution(struct i915_request *rq,
373 struct i915_request *signal,
374 gfp_t gfp)
375{
376 struct execute_cb *cb;
377
378 if (i915_request_is_active(signal))
379 return 0;
380
381 cb = kmem_cache_alloc(global.slab_execute_cbs, gfp);
382 if (!cb)
383 return -ENOMEM;
384
385 cb->fence = &rq->submit;
386 i915_sw_fence_await(cb->fence);
387 init_irq_work(&cb->work, irq_execute_cb);
388
389 spin_lock_irq(&signal->lock);
390 if (i915_request_is_active(signal)) {
391 i915_sw_fence_complete(cb->fence);
392 kmem_cache_free(global.slab_execute_cbs, cb);
393 } else {
394 list_add_tail(&cb->link, &signal->execute_cb);
395 }
396 spin_unlock_irq(&signal->lock);
397
398 return 0;
399}
400
4ccfee92 401static void move_to_timeline(struct i915_request *request,
a89d1f92 402 struct i915_timeline *timeline)
4ccfee92 403{
a89d1f92
CW
404 GEM_BUG_ON(request->timeline == &request->engine->timeline);
405 lockdep_assert_held(&request->engine->timeline.lock);
4ccfee92 406
890fd185 407 spin_lock(&request->timeline->lock);
4ccfee92
CW
408 list_move_tail(&request->link, &timeline->requests);
409 spin_unlock(&request->timeline->lock);
410}
411
e61e0f51 412void __i915_request_submit(struct i915_request *request)
5590af3e 413{
73cb9701 414 struct intel_engine_cs *engine = request->engine;
5590af3e 415
b300fde8 416 GEM_TRACE("%s fence %llx:%lld -> current %d\n",
e7702760 417 engine->name,
d9b13c4d 418 request->fence.context, request->fence.seqno,
8892f477 419 hwsp_seqno(request));
d9b13c4d 420
e60a870d 421 GEM_BUG_ON(!irqs_disabled());
a89d1f92 422 lockdep_assert_held(&engine->timeline.lock);
e60a870d 423
d9e61b66
CW
424 if (i915_gem_context_is_banned(request->gem_context))
425 i915_request_skip(request, -EIO);
426
f2d13290
CW
427 /* We may be recursing from the signal callback of another i915 fence */
428 spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
b5773a36 429
52c0fdb2
CW
430 GEM_BUG_ON(test_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags));
431 set_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags);
b5773a36 432
52c0fdb2
CW
433 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags) &&
434 !i915_request_enable_breadcrumb(request))
435 intel_engine_queue_breadcrumbs(engine);
b5773a36 436
e8861964
CW
437 __notify_execute_cb(request);
438
f2d13290
CW
439 spin_unlock(&request->lock);
440
85474441
CW
441 engine->emit_fini_breadcrumb(request,
442 request->ring->vaddr + request->postfix);
5590af3e 443
4ccfee92 444 /* Transfer from per-context onto the global per-engine timeline */
a89d1f92 445 move_to_timeline(request, &engine->timeline);
80b204bc 446
e61e0f51 447 trace_i915_request_execute(request);
d55ac5bf
CW
448}
449
e61e0f51 450void i915_request_submit(struct i915_request *request)
d55ac5bf
CW
451{
452 struct intel_engine_cs *engine = request->engine;
453 unsigned long flags;
23902e49 454
d55ac5bf 455 /* Will be called from irq-context when using foreign fences. */
a89d1f92 456 spin_lock_irqsave(&engine->timeline.lock, flags);
d55ac5bf 457
e61e0f51 458 __i915_request_submit(request);
d55ac5bf 459
a89d1f92 460 spin_unlock_irqrestore(&engine->timeline.lock, flags);
d55ac5bf
CW
461}
462
e61e0f51 463void __i915_request_unsubmit(struct i915_request *request)
d55ac5bf 464{
d6a2289d 465 struct intel_engine_cs *engine = request->engine;
d55ac5bf 466
b300fde8 467 GEM_TRACE("%s fence %llx:%lld, current %d\n",
e7702760 468 engine->name,
d9b13c4d 469 request->fence.context, request->fence.seqno,
8892f477 470 hwsp_seqno(request));
d9b13c4d 471
e60a870d 472 GEM_BUG_ON(!irqs_disabled());
a89d1f92 473 lockdep_assert_held(&engine->timeline.lock);
48bc2a4a 474
e61e0f51
CW
475 /*
476 * Only unwind in reverse order, required so that the per-context list
d6a2289d
CW
477 * is kept in seqno/ring order.
478 */
80b204bc 479
d6a2289d
CW
480 /* We may be recursing from the signal callback of another i915 fence */
481 spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
b5773a36
CW
482
483 /*
484 * As we do not allow WAIT to preempt inflight requests,
485 * once we have executed a request, along with triggering
486 * any execution callbacks, we must preserve its ordering
487 * within the non-preemptible FIFO.
488 */
489 BUILD_BUG_ON(__NO_PREEMPTION & ~I915_PRIORITY_MASK); /* only internal */
490 request->sched.attr.priority |= __NO_PREEMPTION;
491
d6a2289d 492 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
52c0fdb2 493 i915_request_cancel_breadcrumb(request);
b5773a36 494
52c0fdb2
CW
495 GEM_BUG_ON(!test_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags));
496 clear_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags);
b5773a36 497
d6a2289d
CW
498 spin_unlock(&request->lock);
499
500 /* Transfer back from the global per-engine timeline to per-context */
4ccfee92 501 move_to_timeline(request, request->timeline);
d6a2289d 502
e61e0f51
CW
503 /*
504 * We don't need to wake_up any waiters on request->execute, they
d6a2289d 505 * will get woken by any other event or us re-adding this request
e61e0f51 506 * to the engine timeline (__i915_request_submit()). The waiters
d6a2289d
CW
507 * should be quite adapt at finding that the request now has a new
508 * global_seqno to the one they went to sleep on.
509 */
510}
511
e61e0f51 512void i915_request_unsubmit(struct i915_request *request)
d6a2289d
CW
513{
514 struct intel_engine_cs *engine = request->engine;
515 unsigned long flags;
516
517 /* Will be called from irq-context when using foreign fences. */
a89d1f92 518 spin_lock_irqsave(&engine->timeline.lock, flags);
d6a2289d 519
e61e0f51 520 __i915_request_unsubmit(request);
d6a2289d 521
a89d1f92 522 spin_unlock_irqrestore(&engine->timeline.lock, flags);
5590af3e
CW
523}
524
23902e49 525static int __i915_sw_fence_call
d55ac5bf 526submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
23902e49 527{
e61e0f51 528 struct i915_request *request =
48bc2a4a 529 container_of(fence, typeof(*request), submit);
48bc2a4a
CW
530
531 switch (state) {
532 case FENCE_COMPLETE:
e61e0f51 533 trace_i915_request_submit(request);
af7a8ffa 534 /*
e61e0f51
CW
535 * We need to serialize use of the submit_request() callback
536 * with its hotplugging performed during an emergency
537 * i915_gem_set_wedged(). We use the RCU mechanism to mark the
538 * critical section in order to force i915_gem_set_wedged() to
539 * wait until the submit_request() is completed before
540 * proceeding.
af7a8ffa
DV
541 */
542 rcu_read_lock();
d55ac5bf 543 request->engine->submit_request(request);
af7a8ffa 544 rcu_read_unlock();
48bc2a4a
CW
545 break;
546
547 case FENCE_FREE:
e61e0f51 548 i915_request_put(request);
48bc2a4a
CW
549 break;
550 }
551
23902e49
CW
552 return NOTIFY_DONE;
553}
554
b7404c7e
CW
555static int __i915_sw_fence_call
556semaphore_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
557{
558 struct i915_request *request =
559 container_of(fence, typeof(*request), semaphore);
560
561 switch (state) {
562 case FENCE_COMPLETE:
563 /*
564 * We only check a small portion of our dependencies
565 * and so cannot guarantee that there remains no
566 * semaphore chain across all. Instead of opting
567 * for the full NOSEMAPHORE boost, we go for the
568 * smaller (but still preempting) boost of
569 * NEWCLIENT. This will be enough to boost over
570 * a busywaiting request (as that cannot be
571 * NEWCLIENT) without accidentally boosting
572 * a busywait over real work elsewhere.
573 */
574 i915_schedule_bump_priority(request, I915_PRIORITY_NEWCLIENT);
575 break;
576
577 case FENCE_FREE:
578 i915_request_put(request);
579 break;
580 }
581
582 return NOTIFY_DONE;
583}
584
d22ba0cb
CW
585static void ring_retire_requests(struct intel_ring *ring)
586{
587 struct i915_request *rq, *rn;
588
589 list_for_each_entry_safe(rq, rn, &ring->request_list, ring_link) {
590 if (!i915_request_completed(rq))
591 break;
592
593 i915_request_retire(rq);
594 }
595}
596
597static noinline struct i915_request *
598i915_request_alloc_slow(struct intel_context *ce)
599{
600 struct intel_ring *ring = ce->ring;
601 struct i915_request *rq;
602
603 if (list_empty(&ring->request_list))
604 goto out;
605
606 /* Ratelimit ourselves to prevent oom from malicious clients */
607 rq = list_last_entry(&ring->request_list, typeof(*rq), ring_link);
608 cond_synchronize_rcu(rq->rcustate);
609
610 /* Retire our old requests in the hope that we free some */
611 ring_retire_requests(ring);
612
613out:
32eb6bcf 614 return kmem_cache_alloc(global.slab_requests, GFP_KERNEL);
d22ba0cb
CW
615}
616
8e637178 617/**
e61e0f51 618 * i915_request_alloc - allocate a request structure
8e637178
CW
619 *
620 * @engine: engine that we wish to issue the request on.
621 * @ctx: context that the request will be associated with.
8e637178
CW
622 *
623 * Returns a pointer to the allocated request if successful,
624 * or an error code if not.
625 */
e61e0f51
CW
626struct i915_request *
627i915_request_alloc(struct intel_engine_cs *engine, struct i915_gem_context *ctx)
05235c53 628{
e61e0f51 629 struct drm_i915_private *i915 = engine->i915;
1fc44d9b 630 struct intel_context *ce;
ebece753
CW
631 struct i915_timeline *tl;
632 struct i915_request *rq;
633 u32 seqno;
05235c53
CW
634 int ret;
635
e61e0f51 636 lockdep_assert_held(&i915->drm.struct_mutex);
28176ef4 637
e7af3116
CW
638 /*
639 * Preempt contexts are reserved for exclusive use to inject a
640 * preemption context switch. They are never to be used for any trivial
641 * request!
642 */
e61e0f51 643 GEM_BUG_ON(ctx == i915->preempt_context);
e7af3116 644
e61e0f51
CW
645 /*
646 * ABI: Before userspace accesses the GPU (e.g. execbuffer), report
6ffb7d07 647 * EIO if the GPU is already wedged.
05235c53 648 */
c41166f9
CW
649 ret = i915_terminally_wedged(i915);
650 if (ret)
651 return ERR_PTR(ret);
05235c53 652
e61e0f51
CW
653 /*
654 * Pinning the contexts may generate requests in order to acquire
e8a9c58f
CW
655 * GGTT space, so do this first before we reserve a seqno for
656 * ourselves.
657 */
1fc44d9b
CW
658 ce = intel_context_pin(ctx, engine);
659 if (IS_ERR(ce))
660 return ERR_CAST(ce);
28176ef4 661
6faf5916 662 reserve_gt(i915);
3ef71149 663 mutex_lock(&ce->ring->timeline->mutex);
e8a9c58f 664
b887d615 665 /* Move our oldest request to the slab-cache (if not in use!) */
1fc44d9b
CW
666 rq = list_first_entry(&ce->ring->request_list, typeof(*rq), ring_link);
667 if (!list_is_last(&rq->ring_link, &ce->ring->request_list) &&
7c572e1b 668 i915_request_completed(rq))
e61e0f51 669 i915_request_retire(rq);
9b5f4e5e 670
e61e0f51
CW
671 /*
672 * Beware: Dragons be flying overhead.
5a198b8c
CW
673 *
674 * We use RCU to look up requests in flight. The lookups may
675 * race with the request being allocated from the slab freelist.
676 * That is the request we are writing to here, may be in the process
21950ee7 677 * of being read by __i915_active_request_get_rcu(). As such,
5a198b8c
CW
678 * we have to be very careful when overwriting the contents. During
679 * the RCU lookup, we change chase the request->engine pointer,
65e4760e 680 * read the request->global_seqno and increment the reference count.
5a198b8c
CW
681 *
682 * The reference count is incremented atomically. If it is zero,
683 * the lookup knows the request is unallocated and complete. Otherwise,
684 * it is either still in use, or has been reallocated and reset
f54d1867
CW
685 * with dma_fence_init(). This increment is safe for release as we
686 * check that the request we have a reference to and matches the active
5a198b8c
CW
687 * request.
688 *
689 * Before we increment the refcount, we chase the request->engine
690 * pointer. We must not call kmem_cache_zalloc() or else we set
691 * that pointer to NULL and cause a crash during the lookup. If
692 * we see the request is completed (based on the value of the
693 * old engine and seqno), the lookup is complete and reports NULL.
694 * If we decide the request is not completed (new engine or seqno),
695 * then we grab a reference and double check that it is still the
696 * active request - which it won't be and restart the lookup.
697 *
698 * Do not use kmem_cache_zalloc() here!
699 */
32eb6bcf 700 rq = kmem_cache_alloc(global.slab_requests,
e61e0f51
CW
701 GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
702 if (unlikely(!rq)) {
d22ba0cb 703 rq = i915_request_alloc_slow(ce);
e61e0f51 704 if (!rq) {
31c70f97
CW
705 ret = -ENOMEM;
706 goto err_unreserve;
707 }
28176ef4 708 }
05235c53 709
65fcb806 710 INIT_LIST_HEAD(&rq->active_list);
e8861964 711 INIT_LIST_HEAD(&rq->execute_cb);
ebece753
CW
712
713 tl = ce->ring->timeline;
714 ret = i915_timeline_get_seqno(tl, rq, &seqno);
715 if (ret)
716 goto err_free;
717
65fcb806
CW
718 rq->i915 = i915;
719 rq->engine = engine;
4e0d64db 720 rq->gem_context = ctx;
1fc44d9b
CW
721 rq->hw_context = ce;
722 rq->ring = ce->ring;
ebece753 723 rq->timeline = tl;
a89d1f92 724 GEM_BUG_ON(rq->timeline == &engine->timeline);
ebece753
CW
725 rq->hwsp_seqno = tl->hwsp_seqno;
726 rq->hwsp_cacheline = tl->hwsp_cacheline;
727 rq->rcustate = get_state_synchronize_rcu(); /* acts as smp_mb() */
73cb9701 728
e61e0f51 729 spin_lock_init(&rq->lock);
ebece753
CW
730 dma_fence_init(&rq->fence, &i915_fence_ops, &rq->lock,
731 tl->fence_context, seqno);
04769652 732
48bc2a4a 733 /* We bump the ref for the fence chain */
e61e0f51 734 i915_sw_fence_init(&i915_request_get(rq)->submit, submit_notify);
b7404c7e 735 i915_sw_fence_init(&i915_request_get(rq)->semaphore, semaphore_notify);
5590af3e 736
0c7112a0 737 i915_sched_node_init(&rq->sched);
52e54209 738
5a198b8c 739 /* No zalloc, must clear what we need by hand */
e61e0f51
CW
740 rq->file_priv = NULL;
741 rq->batch = NULL;
742 rq->capture_list = NULL;
743 rq->waitboost = false;
5a198b8c 744
05235c53
CW
745 /*
746 * Reserve space in the ring buffer for all the commands required to
747 * eventually emit this request. This is to guarantee that the
e61e0f51 748 * i915_request_add() call can't fail. Note that the reserve may need
05235c53
CW
749 * to be redone if the request is not actually submitted straight
750 * away, e.g. because a GPU scheduler has deferred it.
ed2922c0
CW
751 *
752 * Note that due to how we add reserved_space to intel_ring_begin()
753 * we need to double our request to ensure that if we need to wrap
754 * around inside i915_request_add() there is sufficient space at
755 * the beginning of the ring as well.
05235c53 756 */
85474441 757 rq->reserved_space = 2 * engine->emit_fini_breadcrumb_dw * sizeof(u32);
05235c53 758
2113184c
CW
759 /*
760 * Record the position of the start of the request so that
d045446d
CW
761 * should we detect the updated seqno part-way through the
762 * GPU processing the request, we never over-estimate the
763 * position of the head.
764 */
e61e0f51 765 rq->head = rq->ring->emit;
d045446d 766
e61e0f51 767 ret = engine->request_alloc(rq);
b1c24a61
CW
768 if (ret)
769 goto err_unwind;
2113184c 770
b887d615 771 /* Keep a second pin for the dual retirement along engine and ring */
1fc44d9b 772 __intel_context_pin(ce);
b887d615 773
b3ee09a4
CW
774 rq->infix = rq->ring->emit; /* end of header; start of user payload */
775
9b6586ae 776 /* Check that we didn't interrupt ourselves with a new request */
b66ea2c2 777 lockdep_assert_held(&rq->timeline->mutex);
e61e0f51 778 GEM_BUG_ON(rq->timeline->seqno != rq->fence.seqno);
b66ea2c2
CW
779 rq->cookie = lockdep_pin_lock(&rq->timeline->mutex);
780
e61e0f51 781 return rq;
05235c53 782
b1c24a61 783err_unwind:
1fc44d9b 784 ce->ring->emit = rq->head;
b1c24a61 785
1618bdb8 786 /* Make sure we didn't add ourselves to external state before freeing */
e61e0f51 787 GEM_BUG_ON(!list_empty(&rq->active_list));
0c7112a0
CW
788 GEM_BUG_ON(!list_empty(&rq->sched.signalers_list));
789 GEM_BUG_ON(!list_empty(&rq->sched.waiters_list));
1618bdb8 790
ebece753 791err_free:
32eb6bcf 792 kmem_cache_free(global.slab_requests, rq);
28176ef4 793err_unreserve:
3ef71149 794 mutex_unlock(&ce->ring->timeline->mutex);
52d7f16e 795 unreserve_gt(i915);
1fc44d9b 796 intel_context_unpin(ce);
8e637178 797 return ERR_PTR(ret);
05235c53
CW
798}
799
e8861964
CW
800static int
801emit_semaphore_wait(struct i915_request *to,
802 struct i915_request *from,
803 gfp_t gfp)
804{
805 u32 hwsp_offset;
806 u32 *cs;
807 int err;
808
809 GEM_BUG_ON(!from->timeline->has_initial_breadcrumb);
810 GEM_BUG_ON(INTEL_GEN(to->i915) < 8);
811
7881e605
CW
812 /* Just emit the first semaphore we see as request space is limited. */
813 if (to->sched.semaphores & from->engine->mask)
814 return i915_sw_fence_await_dma_fence(&to->submit,
815 &from->fence, 0,
816 I915_FENCE_GFP);
817
b7404c7e
CW
818 err = i915_sw_fence_await_dma_fence(&to->semaphore,
819 &from->fence, 0,
820 I915_FENCE_GFP);
821 if (err < 0)
822 return err;
823
e8861964
CW
824 /* We need to pin the signaler's HWSP until we are finished reading. */
825 err = i915_timeline_read_hwsp(from, to, &hwsp_offset);
826 if (err)
827 return err;
828
829 /* Only submit our spinner after the signaler is running! */
830 err = i915_request_await_execution(to, from, gfp);
831 if (err)
832 return err;
833
834 cs = intel_ring_begin(to, 4);
835 if (IS_ERR(cs))
836 return PTR_ERR(cs);
837
838 /*
839 * Using greater-than-or-equal here means we have to worry
840 * about seqno wraparound. To side step that issue, we swap
841 * the timeline HWSP upon wrapping, so that everyone listening
842 * for the old (pre-wrap) values do not see the much smaller
843 * (post-wrap) values than they were expecting (and so wait
844 * forever).
845 */
846 *cs++ = MI_SEMAPHORE_WAIT |
847 MI_SEMAPHORE_GLOBAL_GTT |
848 MI_SEMAPHORE_POLL |
849 MI_SEMAPHORE_SAD_GTE_SDD;
850 *cs++ = from->fence.seqno;
851 *cs++ = hwsp_offset;
852 *cs++ = 0;
853
854 intel_ring_advance(to, cs);
7881e605
CW
855 to->sched.semaphores |= from->engine->mask;
856 to->sched.flags |= I915_SCHED_HAS_SEMAPHORE_CHAIN;
e8861964
CW
857 return 0;
858}
859
a2bc4695 860static int
e61e0f51 861i915_request_await_request(struct i915_request *to, struct i915_request *from)
a2bc4695 862{
85e17f59 863 int ret;
a2bc4695
CW
864
865 GEM_BUG_ON(to == from);
ceae14bd 866 GEM_BUG_ON(to->timeline == from->timeline);
a2bc4695 867
e61e0f51 868 if (i915_request_completed(from))
ade0b0c9
CW
869 return 0;
870
52e54209 871 if (to->engine->schedule) {
32eb6bcf 872 ret = i915_sched_node_add_dependency(&to->sched, &from->sched);
52e54209
CW
873 if (ret < 0)
874 return ret;
875 }
876
73cb9701
CW
877 if (to->engine == from->engine) {
878 ret = i915_sw_fence_await_sw_fence_gfp(&to->submit,
879 &from->submit,
2abe2f84 880 I915_FENCE_GFP);
e8861964
CW
881 } else if (intel_engine_has_semaphores(to->engine) &&
882 to->gem_context->sched.priority >= I915_PRIORITY_NORMAL) {
883 ret = emit_semaphore_wait(to, from, I915_FENCE_GFP);
6faf5916
CW
884 } else {
885 ret = i915_sw_fence_await_dma_fence(&to->submit,
886 &from->fence, 0,
887 I915_FENCE_GFP);
a2bc4695
CW
888 }
889
fc9d4d2b 890 return ret < 0 ? ret : 0;
a2bc4695
CW
891}
892
b52992c0 893int
e61e0f51 894i915_request_await_dma_fence(struct i915_request *rq, struct dma_fence *fence)
b52992c0 895{
29ef3fa9
CW
896 struct dma_fence **child = &fence;
897 unsigned int nchild = 1;
b52992c0 898 int ret;
b52992c0 899
e61e0f51
CW
900 /*
901 * Note that if the fence-array was created in signal-on-any mode,
b52992c0
CW
902 * we should *not* decompose it into its individual fences. However,
903 * we don't currently store which mode the fence-array is operating
904 * in. Fortunately, the only user of signal-on-any is private to
905 * amdgpu and we should not see any incoming fence-array from
906 * sync-file being in signal-on-any mode.
907 */
29ef3fa9
CW
908 if (dma_fence_is_array(fence)) {
909 struct dma_fence_array *array = to_dma_fence_array(fence);
910
911 child = array->fences;
912 nchild = array->num_fences;
913 GEM_BUG_ON(!nchild);
914 }
b52992c0 915
29ef3fa9
CW
916 do {
917 fence = *child++;
918 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
919 continue;
b52992c0 920
ceae14bd
CW
921 /*
922 * Requests on the same timeline are explicitly ordered, along
e61e0f51 923 * with their dependencies, by i915_request_add() which ensures
ceae14bd
CW
924 * that requests are submitted in-order through each ring.
925 */
e61e0f51 926 if (fence->context == rq->fence.context)
ceae14bd
CW
927 continue;
928
47979480 929 /* Squash repeated waits to the same timelines */
e61e0f51 930 if (fence->context != rq->i915->mm.unordered_timeline &&
a89d1f92 931 i915_timeline_sync_is_later(rq->timeline, fence))
47979480
CW
932 continue;
933
29ef3fa9 934 if (dma_fence_is_i915(fence))
e61e0f51 935 ret = i915_request_await_request(rq, to_request(fence));
b52992c0 936 else
e61e0f51 937 ret = i915_sw_fence_await_dma_fence(&rq->submit, fence,
29ef3fa9 938 I915_FENCE_TIMEOUT,
2abe2f84 939 I915_FENCE_GFP);
b52992c0
CW
940 if (ret < 0)
941 return ret;
47979480
CW
942
943 /* Record the latest fence used against each timeline */
e61e0f51 944 if (fence->context != rq->i915->mm.unordered_timeline)
a89d1f92 945 i915_timeline_sync_set(rq->timeline, fence);
29ef3fa9 946 } while (--nchild);
b52992c0
CW
947
948 return 0;
949}
950
a2bc4695 951/**
e61e0f51 952 * i915_request_await_object - set this request to (async) wait upon a bo
a2bc4695
CW
953 * @to: request we are wishing to use
954 * @obj: object which may be in use on another ring.
d8802126 955 * @write: whether the wait is on behalf of a writer
a2bc4695
CW
956 *
957 * This code is meant to abstract object synchronization with the GPU.
958 * Conceptually we serialise writes between engines inside the GPU.
959 * We only allow one engine to write into a buffer at any time, but
960 * multiple readers. To ensure each has a coherent view of memory, we must:
961 *
962 * - If there is an outstanding write request to the object, the new
963 * request must wait for it to complete (either CPU or in hw, requests
964 * on the same ring will be naturally ordered).
965 *
966 * - If we are a write request (pending_write_domain is set), the new
967 * request must wait for outstanding read requests to complete.
968 *
969 * Returns 0 if successful, else propagates up the lower layer error.
970 */
971int
e61e0f51
CW
972i915_request_await_object(struct i915_request *to,
973 struct drm_i915_gem_object *obj,
974 bool write)
a2bc4695 975{
d07f0e59
CW
976 struct dma_fence *excl;
977 int ret = 0;
a2bc4695
CW
978
979 if (write) {
d07f0e59
CW
980 struct dma_fence **shared;
981 unsigned int count, i;
982
983 ret = reservation_object_get_fences_rcu(obj->resv,
984 &excl, &count, &shared);
985 if (ret)
986 return ret;
987
988 for (i = 0; i < count; i++) {
e61e0f51 989 ret = i915_request_await_dma_fence(to, shared[i]);
d07f0e59
CW
990 if (ret)
991 break;
992
993 dma_fence_put(shared[i]);
994 }
995
996 for (; i < count; i++)
997 dma_fence_put(shared[i]);
998 kfree(shared);
a2bc4695 999 } else {
d07f0e59 1000 excl = reservation_object_get_excl_rcu(obj->resv);
a2bc4695
CW
1001 }
1002
d07f0e59
CW
1003 if (excl) {
1004 if (ret == 0)
e61e0f51 1005 ret = i915_request_await_dma_fence(to, excl);
a2bc4695 1006
d07f0e59 1007 dma_fence_put(excl);
a2bc4695
CW
1008 }
1009
d07f0e59 1010 return ret;
a2bc4695
CW
1011}
1012
6dd7526f
CW
1013void i915_request_skip(struct i915_request *rq, int error)
1014{
1015 void *vaddr = rq->ring->vaddr;
1016 u32 head;
1017
1018 GEM_BUG_ON(!IS_ERR_VALUE((long)error));
1019 dma_fence_set_error(&rq->fence, error);
1020
1021 /*
1022 * As this request likely depends on state from the lost
1023 * context, clear out all the user operations leaving the
1024 * breadcrumb at the end (so we get the fence notifications).
1025 */
1026 head = rq->infix;
1027 if (rq->postfix < head) {
1028 memset(vaddr + head, 0, rq->ring->size - head);
1029 head = 0;
1030 }
1031 memset(vaddr + head, 0, rq->postfix - head);
1032}
1033
ea593dbb
CW
1034static struct i915_request *
1035__i915_request_add_to_timeline(struct i915_request *rq)
1036{
1037 struct i915_timeline *timeline = rq->timeline;
1038 struct i915_request *prev;
1039
1040 /*
1041 * Dependency tracking and request ordering along the timeline
1042 * is special cased so that we can eliminate redundant ordering
1043 * operations while building the request (we know that the timeline
1044 * itself is ordered, and here we guarantee it).
1045 *
1046 * As we know we will need to emit tracking along the timeline,
1047 * we embed the hooks into our request struct -- at the cost of
1048 * having to have specialised no-allocation interfaces (which will
1049 * be beneficial elsewhere).
1050 *
1051 * A second benefit to open-coding i915_request_await_request is
1052 * that we can apply a slight variant of the rules specialised
1053 * for timelines that jump between engines (such as virtual engines).
1054 * If we consider the case of virtual engine, we must emit a dma-fence
1055 * to prevent scheduling of the second request until the first is
1056 * complete (to maximise our greedy late load balancing) and this
1057 * precludes optimising to use semaphores serialisation of a single
1058 * timeline across engines.
1059 */
1060 prev = i915_active_request_raw(&timeline->last_request,
1061 &rq->i915->drm.struct_mutex);
1062 if (prev && !i915_request_completed(prev)) {
1063 if (is_power_of_2(prev->engine->mask | rq->engine->mask))
1064 i915_sw_fence_await_sw_fence(&rq->submit,
1065 &prev->submit,
1066 &rq->submitq);
1067 else
1068 __i915_sw_fence_await_dma_fence(&rq->submit,
1069 &prev->fence,
1070 &rq->dmaq);
1071 if (rq->engine->schedule)
1072 __i915_sched_node_add_dependency(&rq->sched,
1073 &prev->sched,
1074 &rq->dep,
1075 0);
1076 }
1077
1078 spin_lock_irq(&timeline->lock);
1079 list_add_tail(&rq->link, &timeline->requests);
1080 spin_unlock_irq(&timeline->lock);
1081
1082 GEM_BUG_ON(timeline->seqno != rq->fence.seqno);
1083 __i915_active_request_set(&timeline->last_request, rq);
1084
1085 return prev;
1086}
1087
05235c53
CW
1088/*
1089 * NB: This function is not allowed to fail. Doing so would mean the the
1090 * request is not being tracked for completion but the work itself is
1091 * going to happen on the hardware. This would be a Bad Thing(tm).
1092 */
697b9a87 1093void i915_request_add(struct i915_request *request)
05235c53 1094{
95b2ab56 1095 struct intel_engine_cs *engine = request->engine;
a89d1f92 1096 struct i915_timeline *timeline = request->timeline;
1fc44d9b 1097 struct intel_ring *ring = request->ring;
e61e0f51 1098 struct i915_request *prev;
73dec95e 1099 u32 *cs;
05235c53 1100
dd847a70 1101 GEM_TRACE("%s fence %llx:%lld\n",
d9b13c4d
CW
1102 engine->name, request->fence.context, request->fence.seqno);
1103
3ef71149 1104 lockdep_assert_held(&request->timeline->mutex);
b66ea2c2
CW
1105 lockdep_unpin_lock(&request->timeline->mutex, request->cookie);
1106
e61e0f51 1107 trace_i915_request_add(request);
0f25dff6 1108
8ac71d1d
CW
1109 /*
1110 * Make sure that no request gazumped us - if it was allocated after
e61e0f51 1111 * our i915_request_alloc() and called __i915_request_add() before
c781c978
CW
1112 * us, the timeline will hold its seqno which is later than ours.
1113 */
9b6586ae 1114 GEM_BUG_ON(timeline->seqno != request->fence.seqno);
c781c978 1115
05235c53
CW
1116 /*
1117 * To ensure that this call will not fail, space for its emissions
1118 * should already have been reserved in the ring buffer. Let the ring
1119 * know that it is time to use that space up.
1120 */
ed2922c0 1121 GEM_BUG_ON(request->reserved_space > request->ring->space);
05235c53 1122 request->reserved_space = 0;
05235c53 1123
8ac71d1d
CW
1124 /*
1125 * Record the position of the start of the breadcrumb so that
05235c53
CW
1126 * should we detect the updated seqno part-way through the
1127 * GPU processing the request, we never over-estimate the
d045446d 1128 * position of the ring's HEAD.
05235c53 1129 */
85474441 1130 cs = intel_ring_begin(request, engine->emit_fini_breadcrumb_dw);
73dec95e
TU
1131 GEM_BUG_ON(IS_ERR(cs));
1132 request->postfix = intel_ring_offset(request, cs);
05235c53 1133
ea593dbb 1134 prev = __i915_request_add_to_timeline(request);
f2d13290 1135
0f25dff6 1136 list_add_tail(&request->ring_link, &ring->request_list);
4daffb66 1137 if (list_is_first(&request->ring_link, &ring->request_list))
643b450a 1138 list_add(&ring->active_link, &request->i915->gt.active_rings);
c6eeb479 1139 request->i915->gt.active_engines |= request->engine->mask;
f2d13290 1140 request->emitted_jiffies = jiffies;
0f25dff6 1141
8ac71d1d
CW
1142 /*
1143 * Let the backend know a new request has arrived that may need
0de9136d
CW
1144 * to adjust the existing execution schedule due to a high priority
1145 * request - i.e. we may want to preempt the current request in order
1146 * to run a high priority dependency chain *before* we can execute this
1147 * request.
1148 *
1149 * This is called before the request is ready to run so that we can
1150 * decide whether to preempt the entire chain so that it is ready to
1151 * run at the earliest possible convenience.
1152 */
71ace7ca 1153 local_bh_disable();
b7404c7e 1154 i915_sw_fence_commit(&request->semaphore);
71ace7ca 1155 rcu_read_lock(); /* RCU serialisation for set-wedged protection */
b16c7651
CW
1156 if (engine->schedule) {
1157 struct i915_sched_attr attr = request->gem_context->sched;
1158
f9e9e9de
CW
1159 /*
1160 * Boost actual workloads past semaphores!
1161 *
1162 * With semaphores we spin on one engine waiting for another,
1163 * simply to reduce the latency of starting our work when
1164 * the signaler completes. However, if there is any other
1165 * work that we could be doing on this engine instead, that
1166 * is better utilisation and will reduce the overall duration
1167 * of the current work. To avoid PI boosting a semaphore
1168 * far in the distance past over useful work, we keep a history
1169 * of any semaphore use along our dependency chain.
1170 */
7881e605 1171 if (!(request->sched.flags & I915_SCHED_HAS_SEMAPHORE_CHAIN))
f9e9e9de
CW
1172 attr.priority |= I915_PRIORITY_NOSEMAPHORE;
1173
b16c7651
CW
1174 /*
1175 * Boost priorities to new clients (new request flows).
1176 *
1177 * Allow interactive/synchronous clients to jump ahead of
1178 * the bulk clients. (FQ_CODEL)
1179 */
1413b2bc 1180 if (list_empty(&request->sched.signalers_list))
b16c7651
CW
1181 attr.priority |= I915_PRIORITY_NEWCLIENT;
1182
1183 engine->schedule(request, &attr);
1184 }
47650db0 1185 rcu_read_unlock();
5590af3e
CW
1186 i915_sw_fence_commit(&request->submit);
1187 local_bh_enable(); /* Kick the execlists tasklet if just scheduled */
c22b355f
CW
1188
1189 /*
1190 * In typical scenarios, we do not expect the previous request on
1191 * the timeline to be still tracked by timeline->last_request if it
1192 * has been completed. If the completed request is still here, that
1193 * implies that request retirement is a long way behind submission,
1194 * suggesting that we haven't been retiring frequently enough from
1195 * the combination of retire-before-alloc, waiters and the background
1196 * retirement worker. So if the last request on this timeline was
1197 * already completed, do a catch up pass, flushing the retirement queue
1198 * up to this client. Since we have now moved the heaviest operations
1199 * during retirement onto secondary workers, such as freeing objects
1200 * or contexts, retiring a bunch of requests is mostly list management
1201 * (and cache misses), and so we should not be overly penalizing this
1202 * client by performing excess work, though we may still performing
1203 * work on behalf of others -- but instead we should benefit from
1204 * improved resource management. (Well, that's the theory at least.)
1205 */
e61e0f51
CW
1206 if (prev && i915_request_completed(prev))
1207 i915_request_retire_upto(prev);
3ef71149
CW
1208
1209 mutex_unlock(&request->timeline->mutex);
05235c53
CW
1210}
1211
1212static unsigned long local_clock_us(unsigned int *cpu)
1213{
1214 unsigned long t;
1215
e61e0f51
CW
1216 /*
1217 * Cheaply and approximately convert from nanoseconds to microseconds.
05235c53
CW
1218 * The result and subsequent calculations are also defined in the same
1219 * approximate microseconds units. The principal source of timing
1220 * error here is from the simple truncation.
1221 *
1222 * Note that local_clock() is only defined wrt to the current CPU;
1223 * the comparisons are no longer valid if we switch CPUs. Instead of
1224 * blocking preemption for the entire busywait, we can detect the CPU
1225 * switch and use that as indicator of system load and a reason to
1226 * stop busywaiting, see busywait_stop().
1227 */
1228 *cpu = get_cpu();
1229 t = local_clock() >> 10;
1230 put_cpu();
1231
1232 return t;
1233}
1234
1235static bool busywait_stop(unsigned long timeout, unsigned int cpu)
1236{
1237 unsigned int this_cpu;
1238
1239 if (time_after(local_clock_us(&this_cpu), timeout))
1240 return true;
1241
1242 return this_cpu != cpu;
1243}
1244
52c0fdb2
CW
1245static bool __i915_spin_request(const struct i915_request * const rq,
1246 int state, unsigned long timeout_us)
05235c53 1247{
52c0fdb2 1248 unsigned int cpu;
b2f2f0fc
CW
1249
1250 /*
1251 * Only wait for the request if we know it is likely to complete.
1252 *
1253 * We don't track the timestamps around requests, nor the average
1254 * request length, so we do not have a good indicator that this
1255 * request will complete within the timeout. What we do know is the
52c0fdb2
CW
1256 * order in which requests are executed by the context and so we can
1257 * tell if the request has been started. If the request is not even
1258 * running yet, it is a fair assumption that it will not complete
1259 * within our relatively short timeout.
b2f2f0fc 1260 */
52c0fdb2 1261 if (!i915_request_is_running(rq))
b2f2f0fc
CW
1262 return false;
1263
e61e0f51
CW
1264 /*
1265 * When waiting for high frequency requests, e.g. during synchronous
05235c53
CW
1266 * rendering split between the CPU and GPU, the finite amount of time
1267 * required to set up the irq and wait upon it limits the response
1268 * rate. By busywaiting on the request completion for a short while we
1269 * can service the high frequency waits as quick as possible. However,
1270 * if it is a slow request, we want to sleep as quickly as possible.
1271 * The tradeoff between waiting and sleeping is roughly the time it
1272 * takes to sleep on a request, on the order of a microsecond.
1273 */
1274
1275 timeout_us += local_clock_us(&cpu);
1276 do {
52c0fdb2
CW
1277 if (i915_request_completed(rq))
1278 return true;
c33ed067 1279
05235c53
CW
1280 if (signal_pending_state(state, current))
1281 break;
1282
1283 if (busywait_stop(timeout_us, cpu))
1284 break;
1285
f2f09a4c 1286 cpu_relax();
05235c53
CW
1287 } while (!need_resched());
1288
1289 return false;
1290}
1291
52c0fdb2
CW
1292struct request_wait {
1293 struct dma_fence_cb cb;
1294 struct task_struct *tsk;
1295};
1296
1297static void request_wait_wake(struct dma_fence *fence, struct dma_fence_cb *cb)
1298{
1299 struct request_wait *wait = container_of(cb, typeof(*wait), cb);
1300
1301 wake_up_process(wait->tsk);
1302}
1303
05235c53 1304/**
e532be89 1305 * i915_request_wait - wait until execution of request has finished
e61e0f51 1306 * @rq: the request to wait upon
ea746f36 1307 * @flags: how to wait
e95433c7
CW
1308 * @timeout: how long to wait in jiffies
1309 *
e532be89 1310 * i915_request_wait() waits for the request to be completed, for a
e95433c7
CW
1311 * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
1312 * unbounded wait).
05235c53 1313 *
e95433c7
CW
1314 * If the caller holds the struct_mutex, the caller must pass I915_WAIT_LOCKED
1315 * in via the flags, and vice versa if the struct_mutex is not held, the caller
1316 * must not specify that the wait is locked.
05235c53 1317 *
e95433c7
CW
1318 * Returns the remaining time (in jiffies) if the request completed, which may
1319 * be zero or -ETIME if the request is unfinished after the timeout expires.
1320 * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
1321 * pending before the request completes.
05235c53 1322 */
e61e0f51 1323long i915_request_wait(struct i915_request *rq,
e95433c7
CW
1324 unsigned int flags,
1325 long timeout)
05235c53 1326{
ea746f36
CW
1327 const int state = flags & I915_WAIT_INTERRUPTIBLE ?
1328 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
52c0fdb2 1329 struct request_wait wait;
05235c53
CW
1330
1331 might_sleep();
e95433c7 1332 GEM_BUG_ON(timeout < 0);
05235c53 1333
e61e0f51 1334 if (i915_request_completed(rq))
e95433c7 1335 return timeout;
05235c53 1336
e95433c7
CW
1337 if (!timeout)
1338 return -ETIME;
05235c53 1339
e61e0f51 1340 trace_i915_request_wait_begin(rq, flags);
4680816b 1341
52c0fdb2
CW
1342 /* Optimistic short spin before touching IRQs */
1343 if (__i915_spin_request(rq, state, 5))
1344 goto out;
541ca6ed 1345
62eb3c24
CW
1346 /*
1347 * This client is about to stall waiting for the GPU. In many cases
1348 * this is undesirable and limits the throughput of the system, as
1349 * many clients cannot continue processing user input/output whilst
1350 * blocked. RPS autotuning may take tens of milliseconds to respond
1351 * to the GPU load and thus incurs additional latency for the client.
1352 * We can circumvent that by promoting the GPU frequency to maximum
1353 * before we sleep. This makes the GPU throttle up much more quickly
1354 * (good for benchmarks and user experience, e.g. window animations),
1355 * but at a cost of spending more power processing the workload
1356 * (bad for battery).
1357 */
1358 if (flags & I915_WAIT_PRIORITY) {
1359 if (!i915_request_started(rq) && INTEL_GEN(rq->i915) >= 6)
1360 gen6_rps_boost(rq);
b7404c7e 1361 local_bh_disable(); /* suspend tasklets for reprioritisation */
52c0fdb2 1362 i915_schedule_bump_priority(rq, I915_PRIORITY_WAIT);
b7404c7e 1363 local_bh_enable(); /* kick tasklets en masse */
62eb3c24 1364 }
4680816b 1365
52c0fdb2
CW
1366 wait.tsk = current;
1367 if (dma_fence_add_callback(&rq->fence, &wait.cb, request_wait_wake))
1368 goto out;
4680816b 1369
52c0fdb2
CW
1370 for (;;) {
1371 set_current_state(state);
05235c53 1372
52c0fdb2
CW
1373 if (i915_request_completed(rq))
1374 break;
05235c53 1375
05235c53 1376 if (signal_pending_state(state, current)) {
e95433c7 1377 timeout = -ERESTARTSYS;
05235c53
CW
1378 break;
1379 }
1380
e95433c7
CW
1381 if (!timeout) {
1382 timeout = -ETIME;
05235c53
CW
1383 break;
1384 }
1385
e95433c7 1386 timeout = io_schedule_timeout(timeout);
05235c53 1387 }
a49625f9 1388 __set_current_state(TASK_RUNNING);
05235c53 1389
52c0fdb2
CW
1390 dma_fence_remove_callback(&rq->fence, &wait.cb);
1391
1392out:
1393 trace_i915_request_wait_end(rq);
e95433c7 1394 return timeout;
05235c53 1395}
4b8de8e6 1396
e61e0f51 1397void i915_retire_requests(struct drm_i915_private *i915)
4b8de8e6 1398{
643b450a 1399 struct intel_ring *ring, *tmp;
4b8de8e6 1400
e61e0f51 1401 lockdep_assert_held(&i915->drm.struct_mutex);
4b8de8e6 1402
e61e0f51 1403 if (!i915->gt.active_requests)
4b8de8e6
CW
1404 return;
1405
65baf0ef
CW
1406 list_for_each_entry_safe(ring, tmp,
1407 &i915->gt.active_rings, active_link) {
1408 intel_ring_get(ring); /* last rq holds reference! */
b887d615 1409 ring_retire_requests(ring);
65baf0ef
CW
1410 intel_ring_put(ring);
1411 }
4b8de8e6 1412}
c835c550
CW
1413
1414#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1415#include "selftests/mock_request.c"
e61e0f51 1416#include "selftests/i915_request.c"
c835c550 1417#endif
32eb6bcf 1418
103b76ee
CW
1419static void i915_global_request_shrink(void)
1420{
1421 kmem_cache_shrink(global.slab_dependencies);
1422 kmem_cache_shrink(global.slab_execute_cbs);
1423 kmem_cache_shrink(global.slab_requests);
1424}
1425
1426static void i915_global_request_exit(void)
1427{
1428 kmem_cache_destroy(global.slab_dependencies);
1429 kmem_cache_destroy(global.slab_execute_cbs);
1430 kmem_cache_destroy(global.slab_requests);
1431}
1432
1433static struct i915_global_request global = { {
1434 .shrink = i915_global_request_shrink,
1435 .exit = i915_global_request_exit,
1436} };
1437
32eb6bcf
CW
1438int __init i915_global_request_init(void)
1439{
1440 global.slab_requests = KMEM_CACHE(i915_request,
1441 SLAB_HWCACHE_ALIGN |
1442 SLAB_RECLAIM_ACCOUNT |
1443 SLAB_TYPESAFE_BY_RCU);
1444 if (!global.slab_requests)
1445 return -ENOMEM;
1446
e8861964
CW
1447 global.slab_execute_cbs = KMEM_CACHE(execute_cb,
1448 SLAB_HWCACHE_ALIGN |
1449 SLAB_RECLAIM_ACCOUNT |
1450 SLAB_TYPESAFE_BY_RCU);
1451 if (!global.slab_execute_cbs)
1452 goto err_requests;
1453
32eb6bcf
CW
1454 global.slab_dependencies = KMEM_CACHE(i915_dependency,
1455 SLAB_HWCACHE_ALIGN |
1456 SLAB_RECLAIM_ACCOUNT);
1457 if (!global.slab_dependencies)
e8861964 1458 goto err_execute_cbs;
32eb6bcf 1459
103b76ee 1460 i915_global_register(&global.base);
32eb6bcf
CW
1461 return 0;
1462
e8861964
CW
1463err_execute_cbs:
1464 kmem_cache_destroy(global.slab_execute_cbs);
32eb6bcf
CW
1465err_requests:
1466 kmem_cache_destroy(global.slab_requests);
1467 return -ENOMEM;
1468}