blob: 1f91dc8c417134fe9f7ce316824d0aa946ed43e9 [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>
26
Chris Wilson05235c52016-07-20 09:21:08 +010027#include "i915_drv.h"
28
Chris Wilson04769652016-07-20 09:21:11 +010029static const char *i915_fence_get_driver_name(struct fence *fence)
30{
31 return "i915";
32}
33
34static const char *i915_fence_get_timeline_name(struct fence *fence)
35{
36 /* Timelines are bound by eviction to a VM. However, since
37 * we only have a global seqno at the moment, we only have
38 * a single timeline. Note that each timeline will have
39 * multiple execution contexts (fence contexts) as we allow
40 * engines within a single timeline to execute in parallel.
41 */
42 return "global";
43}
44
45static bool i915_fence_signaled(struct fence *fence)
46{
47 return i915_gem_request_completed(to_request(fence));
48}
49
50static bool i915_fence_enable_signaling(struct fence *fence)
51{
52 if (i915_fence_signaled(fence))
53 return false;
54
55 intel_engine_enable_signaling(to_request(fence));
56 return true;
57}
58
59static signed long i915_fence_wait(struct fence *fence,
60 bool interruptible,
61 signed long timeout_jiffies)
62{
63 s64 timeout_ns, *timeout;
64 int ret;
65
66 if (timeout_jiffies != MAX_SCHEDULE_TIMEOUT) {
67 timeout_ns = jiffies_to_nsecs(timeout_jiffies);
68 timeout = &timeout_ns;
69 } else {
70 timeout = NULL;
71 }
72
Chris Wilson776f3232016-08-04 07:52:40 +010073 ret = i915_wait_request(to_request(fence),
74 interruptible, timeout,
75 NO_WAITBOOST);
Chris Wilson04769652016-07-20 09:21:11 +010076 if (ret == -ETIME)
77 return 0;
78
79 if (ret < 0)
80 return ret;
81
82 if (timeout_jiffies != MAX_SCHEDULE_TIMEOUT)
83 timeout_jiffies = nsecs_to_jiffies(timeout_ns);
84
85 return timeout_jiffies;
86}
87
88static void i915_fence_value_str(struct fence *fence, char *str, int size)
89{
90 snprintf(str, size, "%u", fence->seqno);
91}
92
93static void i915_fence_timeline_value_str(struct fence *fence, char *str,
94 int size)
95{
96 snprintf(str, size, "%u",
97 intel_engine_get_seqno(to_request(fence)->engine));
98}
99
100static void i915_fence_release(struct fence *fence)
101{
102 struct drm_i915_gem_request *req = to_request(fence);
103
104 kmem_cache_free(req->i915->requests, req);
105}
106
107const struct fence_ops i915_fence_ops = {
108 .get_driver_name = i915_fence_get_driver_name,
109 .get_timeline_name = i915_fence_get_timeline_name,
110 .enable_signaling = i915_fence_enable_signaling,
111 .signaled = i915_fence_signaled,
112 .wait = i915_fence_wait,
113 .release = i915_fence_release,
114 .fence_value_str = i915_fence_value_str,
115 .timeline_value_str = i915_fence_timeline_value_str,
116};
117
Chris Wilson05235c52016-07-20 09:21:08 +0100118int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
119 struct drm_file *file)
120{
121 struct drm_i915_private *dev_private;
122 struct drm_i915_file_private *file_priv;
123
124 WARN_ON(!req || !file || req->file_priv);
125
126 if (!req || !file)
127 return -EINVAL;
128
129 if (req->file_priv)
130 return -EINVAL;
131
132 dev_private = req->i915;
133 file_priv = file->driver_priv;
134
135 spin_lock(&file_priv->mm.lock);
136 req->file_priv = file_priv;
137 list_add_tail(&req->client_list, &file_priv->mm.request_list);
138 spin_unlock(&file_priv->mm.lock);
139
140 req->pid = get_pid(task_pid(current));
141
142 return 0;
143}
144
145static inline void
146i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
147{
148 struct drm_i915_file_private *file_priv = request->file_priv;
149
150 if (!file_priv)
151 return;
152
153 spin_lock(&file_priv->mm.lock);
154 list_del(&request->client_list);
155 request->file_priv = NULL;
156 spin_unlock(&file_priv->mm.lock);
157
158 put_pid(request->pid);
159 request->pid = NULL;
160}
161
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100162void i915_gem_retire_noop(struct i915_gem_active *active,
163 struct drm_i915_gem_request *request)
164{
165 /* Space left intentionally blank */
166}
167
Chris Wilson05235c52016-07-20 09:21:08 +0100168static void i915_gem_request_retire(struct drm_i915_gem_request *request)
169{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100170 struct i915_gem_active *active, *next;
171
Chris Wilson05235c52016-07-20 09:21:08 +0100172 trace_i915_gem_request_retire(request);
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100173 list_del_init(&request->link);
Chris Wilson05235c52016-07-20 09:21:08 +0100174
175 /* We know the GPU must have read the request to have
176 * sent us the seqno + interrupt, so use the position
177 * of tail of the request to update the last known position
178 * of the GPU head.
179 *
180 * Note this requires that we are always called in request
181 * completion order.
182 */
Chris Wilson675d9ad2016-08-04 07:52:36 +0100183 list_del(&request->ring_link);
Chris Wilson1dae2df2016-08-02 22:50:19 +0100184 request->ring->last_retired_head = request->postfix;
Chris Wilson05235c52016-07-20 09:21:08 +0100185
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100186 /* Walk through the active list, calling retire on each. This allows
187 * objects to track their GPU activity and mark themselves as idle
188 * when their *last* active request is completed (updating state
189 * tracking lists for eviction, active references for GEM, etc).
190 *
191 * As the ->retire() may free the node, we decouple it first and
192 * pass along the auxiliary information (to avoid dereferencing
193 * the node after the callback).
194 */
195 list_for_each_entry_safe(active, next, &request->active_list, link) {
196 /* In microbenchmarks or focusing upon time inside the kernel,
197 * we may spend an inordinate amount of time simply handling
198 * the retirement of requests and processing their callbacks.
199 * Of which, this loop itself is particularly hot due to the
200 * cache misses when jumping around the list of i915_gem_active.
201 * So we try to keep this loop as streamlined as possible and
202 * also prefetch the next i915_gem_active to try and hide
203 * the likely cache miss.
204 */
205 prefetchw(next);
206
207 INIT_LIST_HEAD(&active->link);
Chris Wilson0eafec62016-08-04 16:32:41 +0100208 RCU_INIT_POINTER(active->request, NULL);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100209
210 active->retire(active, request);
211 }
212
Chris Wilson05235c52016-07-20 09:21:08 +0100213 i915_gem_request_remove_from_client(request);
214
215 if (request->previous_context) {
216 if (i915.enable_execlists)
217 intel_lr_context_unpin(request->previous_context,
218 request->engine);
219 }
220
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100221 i915_gem_context_put(request->ctx);
Chris Wilsone8a261e2016-07-20 13:31:49 +0100222 i915_gem_request_put(request);
Chris Wilson05235c52016-07-20 09:21:08 +0100223}
224
225void i915_gem_request_retire_upto(struct drm_i915_gem_request *req)
226{
227 struct intel_engine_cs *engine = req->engine;
228 struct drm_i915_gem_request *tmp;
229
230 lockdep_assert_held(&req->i915->drm.struct_mutex);
231
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100232 if (list_empty(&req->link))
Chris Wilson05235c52016-07-20 09:21:08 +0100233 return;
234
235 do {
236 tmp = list_first_entry(&engine->request_list,
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100237 typeof(*tmp), link);
Chris Wilson05235c52016-07-20 09:21:08 +0100238
239 i915_gem_request_retire(tmp);
240 } while (tmp != req);
Chris Wilson05235c52016-07-20 09:21:08 +0100241}
242
243static int i915_gem_check_wedge(unsigned int reset_counter, bool interruptible)
244{
245 if (__i915_terminally_wedged(reset_counter))
246 return -EIO;
247
248 if (__i915_reset_in_progress(reset_counter)) {
249 /* Non-interruptible callers can't handle -EAGAIN, hence return
250 * -EIO unconditionally for these.
251 */
252 if (!interruptible)
253 return -EIO;
254
255 return -EAGAIN;
256 }
257
258 return 0;
259}
260
261static int i915_gem_init_seqno(struct drm_i915_private *dev_priv, u32 seqno)
262{
263 struct intel_engine_cs *engine;
264 int ret;
265
266 /* Carefully retire all requests without writing to the rings */
267 for_each_engine(engine, dev_priv) {
Chris Wilsondcff85c2016-08-05 10:14:11 +0100268 ret = intel_engine_idle(engine, true);
Chris Wilson05235c52016-07-20 09:21:08 +0100269 if (ret)
270 return ret;
271 }
272 i915_gem_retire_requests(dev_priv);
273
274 /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
275 if (!i915_seqno_passed(seqno, dev_priv->next_seqno)) {
276 while (intel_kick_waiters(dev_priv) ||
277 intel_kick_signalers(dev_priv))
278 yield();
279 }
280
281 /* Finally reset hw state */
282 for_each_engine(engine, dev_priv)
Chris Wilson7e37f882016-08-02 22:50:21 +0100283 intel_engine_init_seqno(engine, seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100284
285 return 0;
286}
287
288int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
289{
290 struct drm_i915_private *dev_priv = to_i915(dev);
291 int ret;
292
293 if (seqno == 0)
294 return -EINVAL;
295
296 /* HWS page needs to be set less than what we
297 * will inject to ring
298 */
299 ret = i915_gem_init_seqno(dev_priv, seqno - 1);
300 if (ret)
301 return ret;
302
Chris Wilson05235c52016-07-20 09:21:08 +0100303 dev_priv->next_seqno = seqno;
Chris Wilson05235c52016-07-20 09:21:08 +0100304 return 0;
305}
306
307static int i915_gem_get_seqno(struct drm_i915_private *dev_priv, u32 *seqno)
308{
309 /* reserve 0 for non-seqno */
310 if (unlikely(dev_priv->next_seqno == 0)) {
311 int ret;
312
313 ret = i915_gem_init_seqno(dev_priv, 0);
314 if (ret)
315 return ret;
316
317 dev_priv->next_seqno = 1;
318 }
319
Chris Wilsonddf07be2016-08-02 22:50:39 +0100320 *seqno = dev_priv->next_seqno++;
Chris Wilson05235c52016-07-20 09:21:08 +0100321 return 0;
322}
323
Chris Wilson8e637172016-08-02 22:50:26 +0100324/**
325 * i915_gem_request_alloc - allocate a request structure
326 *
327 * @engine: engine that we wish to issue the request on.
328 * @ctx: context that the request will be associated with.
329 * This can be NULL if the request is not directly related to
330 * any specific user context, in which case this function will
331 * choose an appropriate context to use.
332 *
333 * Returns a pointer to the allocated request if successful,
334 * or an error code if not.
335 */
336struct drm_i915_gem_request *
337i915_gem_request_alloc(struct intel_engine_cs *engine,
338 struct i915_gem_context *ctx)
Chris Wilson05235c52016-07-20 09:21:08 +0100339{
340 struct drm_i915_private *dev_priv = engine->i915;
341 unsigned int reset_counter = i915_reset_counter(&dev_priv->gpu_error);
342 struct drm_i915_gem_request *req;
Chris Wilson04769652016-07-20 09:21:11 +0100343 u32 seqno;
Chris Wilson05235c52016-07-20 09:21:08 +0100344 int ret;
345
Chris Wilson05235c52016-07-20 09:21:08 +0100346 /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
347 * EIO if the GPU is already wedged, or EAGAIN to drop the struct_mutex
348 * and restart.
349 */
350 ret = i915_gem_check_wedge(reset_counter, dev_priv->mm.interruptible);
351 if (ret)
Chris Wilson8e637172016-08-02 22:50:26 +0100352 return ERR_PTR(ret);
Chris Wilson05235c52016-07-20 09:21:08 +0100353
Chris Wilson9b5f4e52016-07-20 09:21:09 +0100354 /* Move the oldest request to the slab-cache (if not in use!) */
Chris Wilson2a1d7752016-07-26 12:01:51 +0100355 req = list_first_entry_or_null(&engine->request_list,
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100356 typeof(*req), link);
Chris Wilson2a1d7752016-07-26 12:01:51 +0100357 if (req && i915_gem_request_completed(req))
358 i915_gem_request_retire(req);
Chris Wilson9b5f4e52016-07-20 09:21:09 +0100359
Chris Wilson05235c52016-07-20 09:21:08 +0100360 req = kmem_cache_zalloc(dev_priv->requests, GFP_KERNEL);
361 if (!req)
Chris Wilson8e637172016-08-02 22:50:26 +0100362 return ERR_PTR(-ENOMEM);
Chris Wilson05235c52016-07-20 09:21:08 +0100363
Chris Wilson04769652016-07-20 09:21:11 +0100364 ret = i915_gem_get_seqno(dev_priv, &seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100365 if (ret)
366 goto err;
367
Chris Wilson04769652016-07-20 09:21:11 +0100368 spin_lock_init(&req->lock);
369 fence_init(&req->fence,
370 &i915_fence_ops,
371 &req->lock,
372 engine->fence_context,
373 seqno);
374
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100375 INIT_LIST_HEAD(&req->active_list);
Chris Wilson05235c52016-07-20 09:21:08 +0100376 req->i915 = dev_priv;
377 req->engine = engine;
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100378 req->ctx = i915_gem_context_get(ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100379
380 /*
381 * Reserve space in the ring buffer for all the commands required to
382 * eventually emit this request. This is to guarantee that the
383 * i915_add_request() call can't fail. Note that the reserve may need
384 * to be redone if the request is not actually submitted straight
385 * away, e.g. because a GPU scheduler has deferred it.
386 */
387 req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
388
389 if (i915.enable_execlists)
390 ret = intel_logical_ring_alloc_request_extras(req);
391 else
392 ret = intel_ring_alloc_request_extras(req);
393 if (ret)
394 goto err_ctx;
395
Chris Wilson8e637172016-08-02 22:50:26 +0100396 return req;
Chris Wilson05235c52016-07-20 09:21:08 +0100397
398err_ctx:
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100399 i915_gem_context_put(ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100400err:
401 kmem_cache_free(dev_priv->requests, req);
Chris Wilson8e637172016-08-02 22:50:26 +0100402 return ERR_PTR(ret);
Chris Wilson05235c52016-07-20 09:21:08 +0100403}
404
405static void i915_gem_mark_busy(const struct intel_engine_cs *engine)
406{
407 struct drm_i915_private *dev_priv = engine->i915;
408
409 dev_priv->gt.active_engines |= intel_engine_flag(engine);
410 if (dev_priv->gt.awake)
411 return;
412
413 intel_runtime_pm_get_noresume(dev_priv);
414 dev_priv->gt.awake = true;
415
Chris Wilson54b4f682016-07-21 21:16:19 +0100416 intel_enable_gt_powersave(dev_priv);
Chris Wilson05235c52016-07-20 09:21:08 +0100417 i915_update_gfx_val(dev_priv);
418 if (INTEL_GEN(dev_priv) >= 6)
419 gen6_rps_busy(dev_priv);
420
421 queue_delayed_work(dev_priv->wq,
422 &dev_priv->gt.retire_work,
423 round_jiffies_up_relative(HZ));
424}
425
426/*
427 * NB: This function is not allowed to fail. Doing so would mean the the
428 * request is not being tracked for completion but the work itself is
429 * going to happen on the hardware. This would be a Bad Thing(tm).
430 */
431void __i915_add_request(struct drm_i915_gem_request *request,
432 struct drm_i915_gem_object *obj,
433 bool flush_caches)
434{
435 struct intel_engine_cs *engine;
Chris Wilson7e37f882016-08-02 22:50:21 +0100436 struct intel_ring *ring;
Chris Wilson05235c52016-07-20 09:21:08 +0100437 u32 request_start;
438 u32 reserved_tail;
439 int ret;
440
441 if (WARN_ON(!request))
442 return;
443
444 engine = request->engine;
Chris Wilson1dae2df2016-08-02 22:50:19 +0100445 ring = request->ring;
Chris Wilson05235c52016-07-20 09:21:08 +0100446
447 /*
448 * To ensure that this call will not fail, space for its emissions
449 * should already have been reserved in the ring buffer. Let the ring
450 * know that it is time to use that space up.
451 */
Chris Wilsonba76d912016-08-02 22:50:28 +0100452 request_start = ring->tail;
Chris Wilson05235c52016-07-20 09:21:08 +0100453 reserved_tail = request->reserved_space;
454 request->reserved_space = 0;
455
456 /*
457 * Emit any outstanding flushes - execbuf can fail to emit the flush
458 * after having emitted the batchbuffer command. Hence we need to fix
459 * things up similar to emitting the lazy request. The difference here
460 * is that the flush _must_ happen before the next request, no matter
461 * what.
462 */
463 if (flush_caches) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100464 ret = engine->emit_flush(request, EMIT_FLUSH);
Chris Wilsonc7fe7d22016-08-02 22:50:24 +0100465
Chris Wilson05235c52016-07-20 09:21:08 +0100466 /* Not allowed to fail! */
Chris Wilsonc7fe7d22016-08-02 22:50:24 +0100467 WARN(ret, "engine->emit_flush() failed: %d!\n", ret);
Chris Wilson05235c52016-07-20 09:21:08 +0100468 }
469
470 trace_i915_gem_request_add(request);
471
472 request->head = request_start;
473
474 /* Whilst this request exists, batch_obj will be on the
475 * active_list, and so will hold the active reference. Only when this
476 * request is retired will the the batch_obj be moved onto the
477 * inactive_list and lose its active reference. Hence we do not need
478 * to explicitly hold another reference here.
479 */
480 request->batch_obj = obj;
481
482 /* Seal the request and mark it as pending execution. Note that
483 * we may inspect this state, without holding any locks, during
484 * hangcheck. Hence we apply the barrier to ensure that we do not
485 * see a more recent value in the hws than we are tracking.
486 */
487 request->emitted_jiffies = jiffies;
488 request->previous_seqno = engine->last_submitted_seqno;
Chris Wilsondcff85c2016-08-05 10:14:11 +0100489 engine->last_submitted_seqno = request->fence.seqno;
490 i915_gem_active_set(&engine->last_request, request);
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100491 list_add_tail(&request->link, &engine->request_list);
Chris Wilson675d9ad2016-08-04 07:52:36 +0100492 list_add_tail(&request->ring_link, &ring->request_list);
Chris Wilson05235c52016-07-20 09:21:08 +0100493
494 /* Record the position of the start of the request so that
495 * should we detect the updated seqno part-way through the
496 * GPU processing the request, we never over-estimate the
497 * position of the head.
498 */
Chris Wilsonba76d912016-08-02 22:50:28 +0100499 request->postfix = ring->tail;
Chris Wilson05235c52016-07-20 09:21:08 +0100500
Chris Wilson05235c52016-07-20 09:21:08 +0100501 /* Not allowed to fail! */
Chris Wilsonddd66c52016-08-02 22:50:31 +0100502 ret = engine->emit_request(request);
503 WARN(ret, "(%s)->emit_request failed: %d!\n", engine->name, ret);
Chris Wilsonc5efa1a2016-08-02 22:50:29 +0100504
Chris Wilson05235c52016-07-20 09:21:08 +0100505 /* Sanity check that the reserved size was large enough. */
Chris Wilsonba76d912016-08-02 22:50:28 +0100506 ret = ring->tail - request_start;
Chris Wilson05235c52016-07-20 09:21:08 +0100507 if (ret < 0)
Chris Wilson1dae2df2016-08-02 22:50:19 +0100508 ret += ring->size;
Chris Wilson05235c52016-07-20 09:21:08 +0100509 WARN_ONCE(ret > reserved_tail,
510 "Not enough space reserved (%d bytes) "
511 "for adding the request (%d bytes)\n",
512 reserved_tail, ret);
513
514 i915_gem_mark_busy(engine);
Chris Wilsonddd66c52016-08-02 22:50:31 +0100515 engine->submit_request(request);
Chris Wilson05235c52016-07-20 09:21:08 +0100516}
517
518static unsigned long local_clock_us(unsigned int *cpu)
519{
520 unsigned long t;
521
522 /* Cheaply and approximately convert from nanoseconds to microseconds.
523 * The result and subsequent calculations are also defined in the same
524 * approximate microseconds units. The principal source of timing
525 * error here is from the simple truncation.
526 *
527 * Note that local_clock() is only defined wrt to the current CPU;
528 * the comparisons are no longer valid if we switch CPUs. Instead of
529 * blocking preemption for the entire busywait, we can detect the CPU
530 * switch and use that as indicator of system load and a reason to
531 * stop busywaiting, see busywait_stop().
532 */
533 *cpu = get_cpu();
534 t = local_clock() >> 10;
535 put_cpu();
536
537 return t;
538}
539
540static bool busywait_stop(unsigned long timeout, unsigned int cpu)
541{
542 unsigned int this_cpu;
543
544 if (time_after(local_clock_us(&this_cpu), timeout))
545 return true;
546
547 return this_cpu != cpu;
548}
549
550bool __i915_spin_request(const struct drm_i915_gem_request *req,
551 int state, unsigned long timeout_us)
552{
553 unsigned int cpu;
554
555 /* When waiting for high frequency requests, e.g. during synchronous
556 * rendering split between the CPU and GPU, the finite amount of time
557 * required to set up the irq and wait upon it limits the response
558 * rate. By busywaiting on the request completion for a short while we
559 * can service the high frequency waits as quick as possible. However,
560 * if it is a slow request, we want to sleep as quickly as possible.
561 * The tradeoff between waiting and sleeping is roughly the time it
562 * takes to sleep on a request, on the order of a microsecond.
563 */
564
565 timeout_us += local_clock_us(&cpu);
566 do {
567 if (i915_gem_request_completed(req))
568 return true;
569
570 if (signal_pending_state(state, current))
571 break;
572
573 if (busywait_stop(timeout_us, cpu))
574 break;
575
576 cpu_relax_lowlatency();
577 } while (!need_resched());
578
579 return false;
580}
581
582/**
Chris Wilson776f3232016-08-04 07:52:40 +0100583 * i915_wait_request - wait until execution of request has finished
Chris Wilson05235c52016-07-20 09:21:08 +0100584 * @req: duh!
585 * @interruptible: do an interruptible wait (normally yes)
586 * @timeout: in - how long to wait (NULL forever); out - how much time remaining
587 * @rps: client to charge for RPS boosting
588 *
589 * Note: It is of utmost importance that the passed in seqno and reset_counter
590 * values have been read by the caller in an smp safe manner. Where read-side
591 * locks are involved, it is sufficient to read the reset_counter before
592 * unlocking the lock that protects the seqno. For lockless tricks, the
593 * reset_counter _must_ be read before, and an appropriate smp_rmb must be
594 * inserted.
595 *
596 * Returns 0 if the request was found within the alloted time. Else returns the
597 * errno with remaining time filled in timeout argument.
598 */
Chris Wilson776f3232016-08-04 07:52:40 +0100599int i915_wait_request(struct drm_i915_gem_request *req,
600 bool interruptible,
601 s64 *timeout,
602 struct intel_rps_client *rps)
Chris Wilson05235c52016-07-20 09:21:08 +0100603{
604 int state = interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
605 DEFINE_WAIT(reset);
606 struct intel_wait wait;
607 unsigned long timeout_remain;
608 int ret = 0;
609
610 might_sleep();
611
Chris Wilson05235c52016-07-20 09:21:08 +0100612 if (i915_gem_request_completed(req))
613 return 0;
614
615 timeout_remain = MAX_SCHEDULE_TIMEOUT;
616 if (timeout) {
617 if (WARN_ON(*timeout < 0))
618 return -EINVAL;
619
620 if (*timeout == 0)
621 return -ETIME;
622
623 /* Record current time in case interrupted, or wedged */
624 timeout_remain = nsecs_to_jiffies_timeout(*timeout);
625 *timeout += ktime_get_raw_ns();
626 }
627
628 trace_i915_gem_request_wait_begin(req);
629
630 /* This client is about to stall waiting for the GPU. In many cases
631 * this is undesirable and limits the throughput of the system, as
632 * many clients cannot continue processing user input/output whilst
633 * blocked. RPS autotuning may take tens of milliseconds to respond
634 * to the GPU load and thus incurs additional latency for the client.
635 * We can circumvent that by promoting the GPU frequency to maximum
636 * before we wait. This makes the GPU throttle up much more quickly
637 * (good for benchmarks and user experience, e.g. window animations),
638 * but at a cost of spending more power processing the workload
639 * (bad for battery). Not all clients even want their results
640 * immediately and for them we should just let the GPU select its own
641 * frequency to maximise efficiency. To prevent a single client from
642 * forcing the clocks too high for the whole system, we only allow
643 * each client to waitboost once in a busy period.
644 */
Chris Wilson42df2712016-07-20 09:21:12 +0100645 if (IS_RPS_CLIENT(rps) && INTEL_GEN(req->i915) >= 6)
Chris Wilson05235c52016-07-20 09:21:08 +0100646 gen6_rps_boost(req->i915, rps, req->emitted_jiffies);
647
648 /* Optimistic spin for the next ~jiffie before touching IRQs */
649 if (i915_spin_request(req, state, 5))
650 goto complete;
651
652 set_current_state(state);
653 add_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
654
Chris Wilson04769652016-07-20 09:21:11 +0100655 intel_wait_init(&wait, req->fence.seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100656 if (intel_engine_add_wait(req->engine, &wait))
657 /* In order to check that we haven't missed the interrupt
658 * as we enabled it, we need to kick ourselves to do a
659 * coherent check on the seqno before we sleep.
660 */
661 goto wakeup;
662
663 for (;;) {
664 if (signal_pending_state(state, current)) {
665 ret = -ERESTARTSYS;
666 break;
667 }
668
669 timeout_remain = io_schedule_timeout(timeout_remain);
670 if (timeout_remain == 0) {
671 ret = -ETIME;
672 break;
673 }
674
675 if (intel_wait_complete(&wait))
676 break;
677
678 set_current_state(state);
679
680wakeup:
681 /* Carefully check if the request is complete, giving time
682 * for the seqno to be visible following the interrupt.
683 * We also have to check in case we are kicked by the GPU
684 * reset in order to drop the struct_mutex.
685 */
686 if (__i915_request_irq_complete(req))
687 break;
688
689 /* Only spin if we know the GPU is processing this request */
690 if (i915_spin_request(req, state, 2))
691 break;
692 }
693 remove_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
694
695 intel_engine_remove_wait(req->engine, &wait);
696 __set_current_state(TASK_RUNNING);
697complete:
698 trace_i915_gem_request_wait_end(req);
699
700 if (timeout) {
701 *timeout -= ktime_get_raw_ns();
702 if (*timeout < 0)
703 *timeout = 0;
704
705 /*
706 * Apparently ktime isn't accurate enough and occasionally has a
707 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
708 * things up to make the test happy. We allow up to 1 jiffy.
709 *
710 * This is a regrssion from the timespec->ktime conversion.
711 */
712 if (ret == -ETIME && *timeout < jiffies_to_usecs(1)*1000)
713 *timeout = 0;
714 }
715
Chris Wilson42df2712016-07-20 09:21:12 +0100716 if (IS_RPS_USER(rps) &&
717 req->fence.seqno == req->engine->last_submitted_seqno) {
Chris Wilson05235c52016-07-20 09:21:08 +0100718 /* The GPU is now idle and this client has stalled.
719 * Since no other client has submitted a request in the
720 * meantime, assume that this client is the only one
721 * supplying work to the GPU but is unable to keep that
722 * work supplied because it is waiting. Since the GPU is
723 * then never kept fully busy, RPS autoclocking will
724 * keep the clocks relatively low, causing further delays.
725 * Compensate by giving the synchronous client credit for
726 * a waitboost next time.
727 */
728 spin_lock(&req->i915->rps.client_lock);
729 list_del_init(&rps->link);
730 spin_unlock(&req->i915->rps.client_lock);
731 }
732
733 return ret;
734}
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100735
Chris Wilson0340d9f2016-08-04 16:32:20 +0100736static void engine_retire_requests(struct intel_engine_cs *engine)
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100737{
738 struct drm_i915_gem_request *request, *next;
739
740 list_for_each_entry_safe(request, next, &engine->request_list, link) {
741 if (!i915_gem_request_completed(request))
742 break;
743
744 i915_gem_request_retire(request);
745 }
746}
747
748void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
749{
750 struct intel_engine_cs *engine;
751
752 lockdep_assert_held(&dev_priv->drm.struct_mutex);
753
754 if (dev_priv->gt.active_engines == 0)
755 return;
756
757 GEM_BUG_ON(!dev_priv->gt.awake);
758
759 for_each_engine(engine, dev_priv) {
Chris Wilson0340d9f2016-08-04 16:32:20 +0100760 engine_retire_requests(engine);
Chris Wilsondcff85c2016-08-05 10:14:11 +0100761 if (!intel_engine_is_active(engine))
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100762 dev_priv->gt.active_engines &= ~intel_engine_flag(engine);
763 }
764
765 if (dev_priv->gt.active_engines == 0)
766 queue_delayed_work(dev_priv->wq,
767 &dev_priv->gt.idle_work,
768 msecs_to_jiffies(100));
769}