blob: 5e38bc04a4f0c917c7ab27974b3f3f8161c737da [file] [log] [blame]
Chris Wilson05235c52016-07-20 09:21:08 +01001/*
2 * Copyright © 2008-2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
Chris Wilsonfa545cb2016-08-04 07:52:35 +010025#include <linux/prefetch.h>
Chris Wilsonb52992c2016-10-28 13:58:24 +010026#include <linux/dma-fence-array.h>
Chris Wilsonfa545cb2016-08-04 07:52:35 +010027
Chris Wilson05235c52016-07-20 09:21:08 +010028#include "i915_drv.h"
29
Chris Wilsonf54d1862016-10-25 13:00:45 +010030static const char *i915_fence_get_driver_name(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010031{
32 return "i915";
33}
34
Chris Wilsonf54d1862016-10-25 13:00:45 +010035static const char *i915_fence_get_timeline_name(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010036{
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 */
43 return "global";
44}
45
Chris Wilsonf54d1862016-10-25 13:00:45 +010046static bool i915_fence_signaled(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010047{
48 return i915_gem_request_completed(to_request(fence));
49}
50
Chris Wilsonf54d1862016-10-25 13:00:45 +010051static bool i915_fence_enable_signaling(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +010052{
53 if (i915_fence_signaled(fence))
54 return false;
55
56 intel_engine_enable_signaling(to_request(fence));
57 return true;
58}
59
Chris Wilsonf54d1862016-10-25 13:00:45 +010060static signed long i915_fence_wait(struct dma_fence *fence,
Chris Wilson04769652016-07-20 09:21:11 +010061 bool interruptible,
62 signed long timeout_jiffies)
63{
64 s64 timeout_ns, *timeout;
65 int ret;
66
67 if (timeout_jiffies != MAX_SCHEDULE_TIMEOUT) {
68 timeout_ns = jiffies_to_nsecs(timeout_jiffies);
69 timeout = &timeout_ns;
70 } else {
71 timeout = NULL;
72 }
73
Chris Wilson776f3232016-08-04 07:52:40 +010074 ret = i915_wait_request(to_request(fence),
75 interruptible, timeout,
76 NO_WAITBOOST);
Chris Wilson04769652016-07-20 09:21:11 +010077 if (ret == -ETIME)
78 return 0;
79
80 if (ret < 0)
81 return ret;
82
83 if (timeout_jiffies != MAX_SCHEDULE_TIMEOUT)
84 timeout_jiffies = nsecs_to_jiffies(timeout_ns);
85
86 return timeout_jiffies;
87}
88
Chris Wilsonf54d1862016-10-25 13:00:45 +010089static void i915_fence_value_str(struct dma_fence *fence, char *str, int size)
Chris Wilson04769652016-07-20 09:21:11 +010090{
91 snprintf(str, size, "%u", fence->seqno);
92}
93
Chris Wilsonf54d1862016-10-25 13:00:45 +010094static void i915_fence_timeline_value_str(struct dma_fence *fence, char *str,
Chris Wilson04769652016-07-20 09:21:11 +010095 int size)
96{
97 snprintf(str, size, "%u",
98 intel_engine_get_seqno(to_request(fence)->engine));
99}
100
Chris Wilsonf54d1862016-10-25 13:00:45 +0100101static void i915_fence_release(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +0100102{
103 struct drm_i915_gem_request *req = to_request(fence);
104
105 kmem_cache_free(req->i915->requests, req);
106}
107
Chris Wilsonf54d1862016-10-25 13:00:45 +0100108const struct dma_fence_ops i915_fence_ops = {
Chris Wilson04769652016-07-20 09:21:11 +0100109 .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,
115 .fence_value_str = i915_fence_value_str,
116 .timeline_value_str = i915_fence_timeline_value_str,
117};
118
Chris Wilson05235c52016-07-20 09:21:08 +0100119int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
120 struct drm_file *file)
121{
122 struct drm_i915_private *dev_private;
123 struct drm_i915_file_private *file_priv;
124
125 WARN_ON(!req || !file || req->file_priv);
126
127 if (!req || !file)
128 return -EINVAL;
129
130 if (req->file_priv)
131 return -EINVAL;
132
133 dev_private = req->i915;
134 file_priv = file->driver_priv;
135
136 spin_lock(&file_priv->mm.lock);
137 req->file_priv = file_priv;
138 list_add_tail(&req->client_list, &file_priv->mm.request_list);
139 spin_unlock(&file_priv->mm.lock);
140
Chris Wilson05235c52016-07-20 09:21:08 +0100141 return 0;
142}
143
144static inline void
145i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
146{
147 struct drm_i915_file_private *file_priv = request->file_priv;
148
149 if (!file_priv)
150 return;
151
152 spin_lock(&file_priv->mm.lock);
153 list_del(&request->client_list);
154 request->file_priv = NULL;
155 spin_unlock(&file_priv->mm.lock);
Chris Wilson05235c52016-07-20 09:21:08 +0100156}
157
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100158void i915_gem_retire_noop(struct i915_gem_active *active,
159 struct drm_i915_gem_request *request)
160{
161 /* Space left intentionally blank */
162}
163
Chris Wilson05235c52016-07-20 09:21:08 +0100164static void i915_gem_request_retire(struct drm_i915_gem_request *request)
165{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100166 struct i915_gem_active *active, *next;
167
Chris Wilson05235c52016-07-20 09:21:08 +0100168 trace_i915_gem_request_retire(request);
Chris Wilson209b3f72016-08-05 10:14:24 +0100169 list_del(&request->link);
Chris Wilson05235c52016-07-20 09:21:08 +0100170
171 /* We know the GPU must have read the request to have
172 * sent us the seqno + interrupt, so use the position
173 * of tail of the request to update the last known position
174 * of the GPU head.
175 *
176 * Note this requires that we are always called in request
177 * completion order.
178 */
Chris Wilson675d9ad2016-08-04 07:52:36 +0100179 list_del(&request->ring_link);
Chris Wilson1dae2df2016-08-02 22:50:19 +0100180 request->ring->last_retired_head = request->postfix;
Chris Wilson05235c52016-07-20 09:21:08 +0100181
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100182 /* Walk through the active list, calling retire on each. This allows
183 * objects to track their GPU activity and mark themselves as idle
184 * when their *last* active request is completed (updating state
185 * tracking lists for eviction, active references for GEM, etc).
186 *
187 * As the ->retire() may free the node, we decouple it first and
188 * pass along the auxiliary information (to avoid dereferencing
189 * the node after the callback).
190 */
191 list_for_each_entry_safe(active, next, &request->active_list, link) {
192 /* In microbenchmarks or focusing upon time inside the kernel,
193 * we may spend an inordinate amount of time simply handling
194 * the retirement of requests and processing their callbacks.
195 * Of which, this loop itself is particularly hot due to the
196 * cache misses when jumping around the list of i915_gem_active.
197 * So we try to keep this loop as streamlined as possible and
198 * also prefetch the next i915_gem_active to try and hide
199 * the likely cache miss.
200 */
201 prefetchw(next);
202
203 INIT_LIST_HEAD(&active->link);
Chris Wilson0eafec62016-08-04 16:32:41 +0100204 RCU_INIT_POINTER(active->request, NULL);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100205
206 active->retire(active, request);
207 }
208
Chris Wilson05235c52016-07-20 09:21:08 +0100209 i915_gem_request_remove_from_client(request);
210
211 if (request->previous_context) {
212 if (i915.enable_execlists)
213 intel_lr_context_unpin(request->previous_context,
214 request->engine);
215 }
216
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100217 i915_gem_context_put(request->ctx);
Chris Wilsone8a261e2016-07-20 13:31:49 +0100218 i915_gem_request_put(request);
Chris Wilson05235c52016-07-20 09:21:08 +0100219}
220
221void i915_gem_request_retire_upto(struct drm_i915_gem_request *req)
222{
223 struct intel_engine_cs *engine = req->engine;
224 struct drm_i915_gem_request *tmp;
225
226 lockdep_assert_held(&req->i915->drm.struct_mutex);
Chris Wilson209b3f72016-08-05 10:14:24 +0100227 GEM_BUG_ON(list_empty(&req->link));
Chris Wilson05235c52016-07-20 09:21:08 +0100228
229 do {
230 tmp = list_first_entry(&engine->request_list,
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100231 typeof(*tmp), link);
Chris Wilson05235c52016-07-20 09:21:08 +0100232
233 i915_gem_request_retire(tmp);
234 } while (tmp != req);
Chris Wilson05235c52016-07-20 09:21:08 +0100235}
236
Chris Wilson8af29b02016-09-09 14:11:47 +0100237static int i915_gem_check_wedge(struct drm_i915_private *dev_priv)
Chris Wilson05235c52016-07-20 09:21:08 +0100238{
Chris Wilson8af29b02016-09-09 14:11:47 +0100239 struct i915_gpu_error *error = &dev_priv->gpu_error;
240
241 if (i915_terminally_wedged(error))
Chris Wilson05235c52016-07-20 09:21:08 +0100242 return -EIO;
243
Chris Wilson8af29b02016-09-09 14:11:47 +0100244 if (i915_reset_in_progress(error)) {
Chris Wilson05235c52016-07-20 09:21:08 +0100245 /* Non-interruptible callers can't handle -EAGAIN, hence return
246 * -EIO unconditionally for these.
247 */
Chris Wilson8af29b02016-09-09 14:11:47 +0100248 if (!dev_priv->mm.interruptible)
Chris Wilson05235c52016-07-20 09:21:08 +0100249 return -EIO;
250
251 return -EAGAIN;
252 }
253
254 return 0;
255}
256
257static int i915_gem_init_seqno(struct drm_i915_private *dev_priv, u32 seqno)
258{
259 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +0530260 enum intel_engine_id id;
Chris Wilson05235c52016-07-20 09:21:08 +0100261 int ret;
262
263 /* Carefully retire all requests without writing to the rings */
Akash Goel3b3f1652016-10-13 22:44:48 +0530264 for_each_engine(engine, dev_priv, id) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100265 ret = intel_engine_idle(engine,
266 I915_WAIT_INTERRUPTIBLE |
267 I915_WAIT_LOCKED);
Chris Wilson05235c52016-07-20 09:21:08 +0100268 if (ret)
269 return ret;
270 }
271 i915_gem_retire_requests(dev_priv);
272
273 /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
274 if (!i915_seqno_passed(seqno, dev_priv->next_seqno)) {
275 while (intel_kick_waiters(dev_priv) ||
276 intel_kick_signalers(dev_priv))
277 yield();
278 }
279
280 /* Finally reset hw state */
Akash Goel3b3f1652016-10-13 22:44:48 +0530281 for_each_engine(engine, dev_priv, id)
Chris Wilson7e37f882016-08-02 22:50:21 +0100282 intel_engine_init_seqno(engine, seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100283
284 return 0;
285}
286
287int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
288{
289 struct drm_i915_private *dev_priv = to_i915(dev);
290 int ret;
291
292 if (seqno == 0)
293 return -EINVAL;
294
295 /* HWS page needs to be set less than what we
296 * will inject to ring
297 */
298 ret = i915_gem_init_seqno(dev_priv, seqno - 1);
299 if (ret)
300 return ret;
301
Chris Wilson05235c52016-07-20 09:21:08 +0100302 dev_priv->next_seqno = seqno;
Chris Wilson05235c52016-07-20 09:21:08 +0100303 return 0;
304}
305
306static int i915_gem_get_seqno(struct drm_i915_private *dev_priv, u32 *seqno)
307{
308 /* reserve 0 for non-seqno */
309 if (unlikely(dev_priv->next_seqno == 0)) {
310 int ret;
311
312 ret = i915_gem_init_seqno(dev_priv, 0);
313 if (ret)
314 return ret;
315
316 dev_priv->next_seqno = 1;
317 }
318
Chris Wilsonddf07be2016-08-02 22:50:39 +0100319 *seqno = dev_priv->next_seqno++;
Chris Wilson05235c52016-07-20 09:21:08 +0100320 return 0;
321}
322
Chris Wilson5590af32016-09-09 14:11:54 +0100323static int __i915_sw_fence_call
324submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
325{
326 struct drm_i915_gem_request *request =
327 container_of(fence, typeof(*request), submit);
328
329 /* Will be called from irq-context when using foreign DMA fences */
330
331 switch (state) {
332 case FENCE_COMPLETE:
Chris Wilson8687b3e2016-10-07 07:53:24 +0100333 request->engine->last_submitted_seqno = request->fence.seqno;
Chris Wilson5590af32016-09-09 14:11:54 +0100334 request->engine->submit_request(request);
335 break;
336
337 case FENCE_FREE:
338 break;
339 }
340
341 return NOTIFY_DONE;
342}
343
Chris Wilson8e637172016-08-02 22:50:26 +0100344/**
345 * i915_gem_request_alloc - allocate a request structure
346 *
347 * @engine: engine that we wish to issue the request on.
348 * @ctx: context that the request will be associated with.
349 * This can be NULL if the request is not directly related to
350 * any specific user context, in which case this function will
351 * choose an appropriate context to use.
352 *
353 * Returns a pointer to the allocated request if successful,
354 * or an error code if not.
355 */
356struct drm_i915_gem_request *
357i915_gem_request_alloc(struct intel_engine_cs *engine,
358 struct i915_gem_context *ctx)
Chris Wilson05235c52016-07-20 09:21:08 +0100359{
360 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson05235c52016-07-20 09:21:08 +0100361 struct drm_i915_gem_request *req;
Chris Wilson04769652016-07-20 09:21:11 +0100362 u32 seqno;
Chris Wilson05235c52016-07-20 09:21:08 +0100363 int ret;
364
Chris Wilson05235c52016-07-20 09:21:08 +0100365 /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
366 * EIO if the GPU is already wedged, or EAGAIN to drop the struct_mutex
367 * and restart.
368 */
Chris Wilson8af29b02016-09-09 14:11:47 +0100369 ret = i915_gem_check_wedge(dev_priv);
Chris Wilson05235c52016-07-20 09:21:08 +0100370 if (ret)
Chris Wilson8e637172016-08-02 22:50:26 +0100371 return ERR_PTR(ret);
Chris Wilson05235c52016-07-20 09:21:08 +0100372
Chris Wilson9b5f4e52016-07-20 09:21:09 +0100373 /* Move the oldest request to the slab-cache (if not in use!) */
Chris Wilson2a1d7752016-07-26 12:01:51 +0100374 req = list_first_entry_or_null(&engine->request_list,
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100375 typeof(*req), link);
Chris Wilson2a1d7752016-07-26 12:01:51 +0100376 if (req && i915_gem_request_completed(req))
377 i915_gem_request_retire(req);
Chris Wilson9b5f4e52016-07-20 09:21:09 +0100378
Chris Wilson5a198b82016-08-09 09:23:34 +0100379 /* Beware: Dragons be flying overhead.
380 *
381 * We use RCU to look up requests in flight. The lookups may
382 * race with the request being allocated from the slab freelist.
383 * That is the request we are writing to here, may be in the process
Chris Wilson1426f712016-08-09 17:03:22 +0100384 * of being read by __i915_gem_active_get_rcu(). As such,
Chris Wilson5a198b82016-08-09 09:23:34 +0100385 * we have to be very careful when overwriting the contents. During
386 * the RCU lookup, we change chase the request->engine pointer,
387 * read the request->fence.seqno and increment the reference count.
388 *
389 * The reference count is incremented atomically. If it is zero,
390 * the lookup knows the request is unallocated and complete. Otherwise,
391 * it is either still in use, or has been reallocated and reset
Chris Wilsonf54d1862016-10-25 13:00:45 +0100392 * with dma_fence_init(). This increment is safe for release as we
393 * check that the request we have a reference to and matches the active
Chris Wilson5a198b82016-08-09 09:23:34 +0100394 * request.
395 *
396 * Before we increment the refcount, we chase the request->engine
397 * pointer. We must not call kmem_cache_zalloc() or else we set
398 * that pointer to NULL and cause a crash during the lookup. If
399 * we see the request is completed (based on the value of the
400 * old engine and seqno), the lookup is complete and reports NULL.
401 * If we decide the request is not completed (new engine or seqno),
402 * then we grab a reference and double check that it is still the
403 * active request - which it won't be and restart the lookup.
404 *
405 * Do not use kmem_cache_zalloc() here!
406 */
407 req = kmem_cache_alloc(dev_priv->requests, GFP_KERNEL);
Chris Wilson05235c52016-07-20 09:21:08 +0100408 if (!req)
Chris Wilson8e637172016-08-02 22:50:26 +0100409 return ERR_PTR(-ENOMEM);
Chris Wilson05235c52016-07-20 09:21:08 +0100410
Chris Wilson04769652016-07-20 09:21:11 +0100411 ret = i915_gem_get_seqno(dev_priv, &seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100412 if (ret)
413 goto err;
414
Chris Wilson04769652016-07-20 09:21:11 +0100415 spin_lock_init(&req->lock);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100416 dma_fence_init(&req->fence,
417 &i915_fence_ops,
418 &req->lock,
419 engine->fence_context,
420 seqno);
Chris Wilson04769652016-07-20 09:21:11 +0100421
Chris Wilson5590af32016-09-09 14:11:54 +0100422 i915_sw_fence_init(&req->submit, submit_notify);
423
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100424 INIT_LIST_HEAD(&req->active_list);
Chris Wilson05235c52016-07-20 09:21:08 +0100425 req->i915 = dev_priv;
426 req->engine = engine;
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100427 req->ctx = i915_gem_context_get(ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100428
Chris Wilson5a198b82016-08-09 09:23:34 +0100429 /* No zalloc, must clear what we need by hand */
430 req->previous_context = NULL;
431 req->file_priv = NULL;
Chris Wilson058d88c2016-08-15 10:49:06 +0100432 req->batch = NULL;
Chris Wilson5a198b82016-08-09 09:23:34 +0100433
Chris Wilson05235c52016-07-20 09:21:08 +0100434 /*
435 * Reserve space in the ring buffer for all the commands required to
436 * eventually emit this request. This is to guarantee that the
437 * i915_add_request() call can't fail. Note that the reserve may need
438 * to be redone if the request is not actually submitted straight
439 * away, e.g. because a GPU scheduler has deferred it.
440 */
441 req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
442
443 if (i915.enable_execlists)
444 ret = intel_logical_ring_alloc_request_extras(req);
445 else
446 ret = intel_ring_alloc_request_extras(req);
447 if (ret)
448 goto err_ctx;
449
Chris Wilsond0454462016-08-15 10:48:40 +0100450 /* Record the position of the start of the request so that
451 * should we detect the updated seqno part-way through the
452 * GPU processing the request, we never over-estimate the
453 * position of the head.
454 */
455 req->head = req->ring->tail;
456
Chris Wilson8e637172016-08-02 22:50:26 +0100457 return req;
Chris Wilson05235c52016-07-20 09:21:08 +0100458
459err_ctx:
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100460 i915_gem_context_put(ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100461err:
462 kmem_cache_free(dev_priv->requests, req);
Chris Wilson8e637172016-08-02 22:50:26 +0100463 return ERR_PTR(ret);
Chris Wilson05235c52016-07-20 09:21:08 +0100464}
465
Chris Wilsona2bc4692016-09-09 14:11:56 +0100466static int
467i915_gem_request_await_request(struct drm_i915_gem_request *to,
468 struct drm_i915_gem_request *from)
469{
470 int idx, ret;
471
472 GEM_BUG_ON(to == from);
473
474 if (to->engine == from->engine)
475 return 0;
476
477 idx = intel_engine_sync_index(from->engine, to->engine);
478 if (from->fence.seqno <= from->engine->semaphore.sync_seqno[idx])
479 return 0;
480
481 trace_i915_gem_ring_sync_to(to, from);
482 if (!i915.semaphores) {
Chris Wilson0a046a02016-09-09 14:12:00 +0100483 if (!i915_spin_request(from, TASK_INTERRUPTIBLE, 2)) {
484 ret = i915_sw_fence_await_dma_fence(&to->submit,
485 &from->fence, 0,
486 GFP_KERNEL);
487 if (ret < 0)
488 return ret;
489 }
Chris Wilsona2bc4692016-09-09 14:11:56 +0100490 } else {
491 ret = to->engine->semaphore.sync_to(to, from);
492 if (ret)
493 return ret;
494 }
495
496 from->engine->semaphore.sync_seqno[idx] = from->fence.seqno;
497 return 0;
498}
499
Chris Wilsonb52992c2016-10-28 13:58:24 +0100500int
501i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
502 struct dma_fence *fence)
503{
504 struct dma_fence_array *array;
505 int ret;
506 int i;
507
508 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
509 return 0;
510
511 if (dma_fence_is_i915(fence))
512 return i915_gem_request_await_request(req, to_request(fence));
513
514 if (!dma_fence_is_array(fence)) {
515 ret = i915_sw_fence_await_dma_fence(&req->submit,
516 fence, I915_FENCE_TIMEOUT,
517 GFP_KERNEL);
518 return ret < 0 ? ret : 0;
519 }
520
521 /* Note that if the fence-array was created in signal-on-any mode,
522 * we should *not* decompose it into its individual fences. However,
523 * we don't currently store which mode the fence-array is operating
524 * in. Fortunately, the only user of signal-on-any is private to
525 * amdgpu and we should not see any incoming fence-array from
526 * sync-file being in signal-on-any mode.
527 */
528
529 array = to_dma_fence_array(fence);
530 for (i = 0; i < array->num_fences; i++) {
531 struct dma_fence *child = array->fences[i];
532
533 if (dma_fence_is_i915(child))
534 ret = i915_gem_request_await_request(req,
535 to_request(child));
536 else
537 ret = i915_sw_fence_await_dma_fence(&req->submit,
538 child, I915_FENCE_TIMEOUT,
539 GFP_KERNEL);
540 if (ret < 0)
541 return ret;
542 }
543
544 return 0;
545}
546
Chris Wilsona2bc4692016-09-09 14:11:56 +0100547/**
548 * i915_gem_request_await_object - set this request to (async) wait upon a bo
549 *
550 * @to: request we are wishing to use
551 * @obj: object which may be in use on another ring.
552 *
553 * This code is meant to abstract object synchronization with the GPU.
554 * Conceptually we serialise writes between engines inside the GPU.
555 * We only allow one engine to write into a buffer at any time, but
556 * multiple readers. To ensure each has a coherent view of memory, we must:
557 *
558 * - If there is an outstanding write request to the object, the new
559 * request must wait for it to complete (either CPU or in hw, requests
560 * on the same ring will be naturally ordered).
561 *
562 * - If we are a write request (pending_write_domain is set), the new
563 * request must wait for outstanding read requests to complete.
564 *
565 * Returns 0 if successful, else propagates up the lower layer error.
566 */
567int
568i915_gem_request_await_object(struct drm_i915_gem_request *to,
569 struct drm_i915_gem_object *obj,
570 bool write)
571{
572 struct i915_gem_active *active;
573 unsigned long active_mask;
574 int idx;
575
576 if (write) {
577 active_mask = i915_gem_object_get_active(obj);
578 active = obj->last_read;
579 } else {
580 active_mask = 1;
581 active = &obj->last_write;
582 }
583
584 for_each_active(active_mask, idx) {
585 struct drm_i915_gem_request *request;
586 int ret;
587
588 request = i915_gem_active_peek(&active[idx],
589 &obj->base.dev->struct_mutex);
590 if (!request)
591 continue;
592
593 ret = i915_gem_request_await_request(to, request);
594 if (ret)
595 return ret;
596 }
597
598 return 0;
599}
600
Chris Wilson05235c52016-07-20 09:21:08 +0100601static void i915_gem_mark_busy(const struct intel_engine_cs *engine)
602{
603 struct drm_i915_private *dev_priv = engine->i915;
604
605 dev_priv->gt.active_engines |= intel_engine_flag(engine);
606 if (dev_priv->gt.awake)
607 return;
608
609 intel_runtime_pm_get_noresume(dev_priv);
610 dev_priv->gt.awake = true;
611
Chris Wilson54b4f682016-07-21 21:16:19 +0100612 intel_enable_gt_powersave(dev_priv);
Chris Wilson05235c52016-07-20 09:21:08 +0100613 i915_update_gfx_val(dev_priv);
614 if (INTEL_GEN(dev_priv) >= 6)
615 gen6_rps_busy(dev_priv);
616
617 queue_delayed_work(dev_priv->wq,
618 &dev_priv->gt.retire_work,
619 round_jiffies_up_relative(HZ));
620}
621
622/*
623 * NB: This function is not allowed to fail. Doing so would mean the the
624 * request is not being tracked for completion but the work itself is
625 * going to happen on the hardware. This would be a Bad Thing(tm).
626 */
Chris Wilson17f298cf2016-08-10 13:41:46 +0100627void __i915_add_request(struct drm_i915_gem_request *request, bool flush_caches)
Chris Wilson05235c52016-07-20 09:21:08 +0100628{
Chris Wilson95b2ab52016-08-15 10:48:46 +0100629 struct intel_engine_cs *engine = request->engine;
630 struct intel_ring *ring = request->ring;
Chris Wilson0a046a02016-09-09 14:12:00 +0100631 struct drm_i915_gem_request *prev;
Chris Wilson05235c52016-07-20 09:21:08 +0100632 u32 request_start;
633 u32 reserved_tail;
634 int ret;
635
Chris Wilson0f25dff2016-09-09 14:11:55 +0100636 trace_i915_gem_request_add(request);
637
Chris Wilson05235c52016-07-20 09:21:08 +0100638 /*
639 * To ensure that this call will not fail, space for its emissions
640 * should already have been reserved in the ring buffer. Let the ring
641 * know that it is time to use that space up.
642 */
Chris Wilsonba76d912016-08-02 22:50:28 +0100643 request_start = ring->tail;
Chris Wilson05235c52016-07-20 09:21:08 +0100644 reserved_tail = request->reserved_space;
645 request->reserved_space = 0;
646
647 /*
648 * Emit any outstanding flushes - execbuf can fail to emit the flush
649 * after having emitted the batchbuffer command. Hence we need to fix
650 * things up similar to emitting the lazy request. The difference here
651 * is that the flush _must_ happen before the next request, no matter
652 * what.
653 */
654 if (flush_caches) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100655 ret = engine->emit_flush(request, EMIT_FLUSH);
Chris Wilsonc7fe7d22016-08-02 22:50:24 +0100656
Chris Wilson05235c52016-07-20 09:21:08 +0100657 /* Not allowed to fail! */
Chris Wilsonc7fe7d22016-08-02 22:50:24 +0100658 WARN(ret, "engine->emit_flush() failed: %d!\n", ret);
Chris Wilson05235c52016-07-20 09:21:08 +0100659 }
660
Chris Wilsond0454462016-08-15 10:48:40 +0100661 /* Record the position of the start of the breadcrumb so that
Chris Wilson05235c52016-07-20 09:21:08 +0100662 * should we detect the updated seqno part-way through the
663 * GPU processing the request, we never over-estimate the
Chris Wilsond0454462016-08-15 10:48:40 +0100664 * position of the ring's HEAD.
Chris Wilson05235c52016-07-20 09:21:08 +0100665 */
Chris Wilsonba76d912016-08-02 22:50:28 +0100666 request->postfix = ring->tail;
Chris Wilson05235c52016-07-20 09:21:08 +0100667
Chris Wilson05235c52016-07-20 09:21:08 +0100668 /* Not allowed to fail! */
Chris Wilsonddd66c52016-08-02 22:50:31 +0100669 ret = engine->emit_request(request);
670 WARN(ret, "(%s)->emit_request failed: %d!\n", engine->name, ret);
Chris Wilsonc5efa1a2016-08-02 22:50:29 +0100671
Chris Wilson05235c52016-07-20 09:21:08 +0100672 /* Sanity check that the reserved size was large enough. */
Chris Wilsonba76d912016-08-02 22:50:28 +0100673 ret = ring->tail - request_start;
Chris Wilson05235c52016-07-20 09:21:08 +0100674 if (ret < 0)
Chris Wilson1dae2df2016-08-02 22:50:19 +0100675 ret += ring->size;
Chris Wilson05235c52016-07-20 09:21:08 +0100676 WARN_ONCE(ret > reserved_tail,
677 "Not enough space reserved (%d bytes) "
678 "for adding the request (%d bytes)\n",
679 reserved_tail, ret);
680
Chris Wilson0f25dff2016-09-09 14:11:55 +0100681 /* Seal the request and mark it as pending execution. Note that
682 * we may inspect this state, without holding any locks, during
683 * hangcheck. Hence we apply the barrier to ensure that we do not
684 * see a more recent value in the hws than we are tracking.
685 */
Chris Wilson0a046a02016-09-09 14:12:00 +0100686
687 prev = i915_gem_active_raw(&engine->last_request,
688 &request->i915->drm.struct_mutex);
689 if (prev)
690 i915_sw_fence_await_sw_fence(&request->submit, &prev->submit,
691 &request->submitq);
692
Chris Wilson0f25dff2016-09-09 14:11:55 +0100693 request->emitted_jiffies = jiffies;
Chris Wilson8687b3e2016-10-07 07:53:24 +0100694 request->previous_seqno = engine->last_pending_seqno;
695 engine->last_pending_seqno = request->fence.seqno;
Chris Wilson0f25dff2016-09-09 14:11:55 +0100696 i915_gem_active_set(&engine->last_request, request);
697 list_add_tail(&request->link, &engine->request_list);
698 list_add_tail(&request->ring_link, &ring->request_list);
699
Chris Wilson05235c52016-07-20 09:21:08 +0100700 i915_gem_mark_busy(engine);
Chris Wilson5590af32016-09-09 14:11:54 +0100701
702 local_bh_disable();
703 i915_sw_fence_commit(&request->submit);
704 local_bh_enable(); /* Kick the execlists tasklet if just scheduled */
Chris Wilson05235c52016-07-20 09:21:08 +0100705}
706
Chris Wilson221fe792016-09-09 14:11:51 +0100707static void reset_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
708{
709 unsigned long flags;
710
711 spin_lock_irqsave(&q->lock, flags);
712 if (list_empty(&wait->task_list))
713 __add_wait_queue(q, wait);
714 spin_unlock_irqrestore(&q->lock, flags);
715}
716
Chris Wilson05235c52016-07-20 09:21:08 +0100717static unsigned long local_clock_us(unsigned int *cpu)
718{
719 unsigned long t;
720
721 /* Cheaply and approximately convert from nanoseconds to microseconds.
722 * The result and subsequent calculations are also defined in the same
723 * approximate microseconds units. The principal source of timing
724 * error here is from the simple truncation.
725 *
726 * Note that local_clock() is only defined wrt to the current CPU;
727 * the comparisons are no longer valid if we switch CPUs. Instead of
728 * blocking preemption for the entire busywait, we can detect the CPU
729 * switch and use that as indicator of system load and a reason to
730 * stop busywaiting, see busywait_stop().
731 */
732 *cpu = get_cpu();
733 t = local_clock() >> 10;
734 put_cpu();
735
736 return t;
737}
738
739static bool busywait_stop(unsigned long timeout, unsigned int cpu)
740{
741 unsigned int this_cpu;
742
743 if (time_after(local_clock_us(&this_cpu), timeout))
744 return true;
745
746 return this_cpu != cpu;
747}
748
749bool __i915_spin_request(const struct drm_i915_gem_request *req,
750 int state, unsigned long timeout_us)
751{
752 unsigned int cpu;
753
754 /* When waiting for high frequency requests, e.g. during synchronous
755 * rendering split between the CPU and GPU, the finite amount of time
756 * required to set up the irq and wait upon it limits the response
757 * rate. By busywaiting on the request completion for a short while we
758 * can service the high frequency waits as quick as possible. However,
759 * if it is a slow request, we want to sleep as quickly as possible.
760 * The tradeoff between waiting and sleeping is roughly the time it
761 * takes to sleep on a request, on the order of a microsecond.
762 */
763
764 timeout_us += local_clock_us(&cpu);
765 do {
766 if (i915_gem_request_completed(req))
767 return true;
768
769 if (signal_pending_state(state, current))
770 break;
771
772 if (busywait_stop(timeout_us, cpu))
773 break;
774
775 cpu_relax_lowlatency();
776 } while (!need_resched());
777
778 return false;
779}
780
781/**
Chris Wilson776f3232016-08-04 07:52:40 +0100782 * i915_wait_request - wait until execution of request has finished
Chris Wilson05235c52016-07-20 09:21:08 +0100783 * @req: duh!
Chris Wilsonea746f32016-09-09 14:11:49 +0100784 * @flags: how to wait
Chris Wilson05235c52016-07-20 09:21:08 +0100785 * @timeout: in - how long to wait (NULL forever); out - how much time remaining
786 * @rps: client to charge for RPS boosting
787 *
788 * Note: It is of utmost importance that the passed in seqno and reset_counter
789 * values have been read by the caller in an smp safe manner. Where read-side
790 * locks are involved, it is sufficient to read the reset_counter before
791 * unlocking the lock that protects the seqno. For lockless tricks, the
792 * reset_counter _must_ be read before, and an appropriate smp_rmb must be
793 * inserted.
794 *
795 * Returns 0 if the request was found within the alloted time. Else returns the
796 * errno with remaining time filled in timeout argument.
797 */
Chris Wilson776f3232016-08-04 07:52:40 +0100798int i915_wait_request(struct drm_i915_gem_request *req,
Chris Wilsonea746f32016-09-09 14:11:49 +0100799 unsigned int flags,
Chris Wilson776f3232016-08-04 07:52:40 +0100800 s64 *timeout,
801 struct intel_rps_client *rps)
Chris Wilson05235c52016-07-20 09:21:08 +0100802{
Chris Wilsonea746f32016-09-09 14:11:49 +0100803 const int state = flags & I915_WAIT_INTERRUPTIBLE ?
804 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
Chris Wilson05235c52016-07-20 09:21:08 +0100805 DEFINE_WAIT(reset);
806 struct intel_wait wait;
807 unsigned long timeout_remain;
808 int ret = 0;
809
810 might_sleep();
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100811#if IS_ENABLED(CONFIG_LOCKDEP)
812 GEM_BUG_ON(!!lockdep_is_held(&req->i915->drm.struct_mutex) !=
813 !!(flags & I915_WAIT_LOCKED));
814#endif
Chris Wilson05235c52016-07-20 09:21:08 +0100815
Chris Wilson05235c52016-07-20 09:21:08 +0100816 if (i915_gem_request_completed(req))
817 return 0;
818
819 timeout_remain = MAX_SCHEDULE_TIMEOUT;
820 if (timeout) {
821 if (WARN_ON(*timeout < 0))
822 return -EINVAL;
823
824 if (*timeout == 0)
825 return -ETIME;
826
827 /* Record current time in case interrupted, or wedged */
828 timeout_remain = nsecs_to_jiffies_timeout(*timeout);
829 *timeout += ktime_get_raw_ns();
830 }
831
832 trace_i915_gem_request_wait_begin(req);
833
834 /* This client is about to stall waiting for the GPU. In many cases
835 * this is undesirable and limits the throughput of the system, as
836 * many clients cannot continue processing user input/output whilst
837 * blocked. RPS autotuning may take tens of milliseconds to respond
838 * to the GPU load and thus incurs additional latency for the client.
839 * We can circumvent that by promoting the GPU frequency to maximum
840 * before we wait. This makes the GPU throttle up much more quickly
841 * (good for benchmarks and user experience, e.g. window animations),
842 * but at a cost of spending more power processing the workload
843 * (bad for battery). Not all clients even want their results
844 * immediately and for them we should just let the GPU select its own
845 * frequency to maximise efficiency. To prevent a single client from
846 * forcing the clocks too high for the whole system, we only allow
847 * each client to waitboost once in a busy period.
848 */
Chris Wilson42df2712016-07-20 09:21:12 +0100849 if (IS_RPS_CLIENT(rps) && INTEL_GEN(req->i915) >= 6)
Chris Wilson05235c52016-07-20 09:21:08 +0100850 gen6_rps_boost(req->i915, rps, req->emitted_jiffies);
851
Daniel Vetter437c3082016-08-05 18:11:24 +0200852 /* Optimistic short spin before touching IRQs */
Chris Wilson05235c52016-07-20 09:21:08 +0100853 if (i915_spin_request(req, state, 5))
854 goto complete;
855
856 set_current_state(state);
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100857 if (flags & I915_WAIT_LOCKED)
858 add_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
Chris Wilson05235c52016-07-20 09:21:08 +0100859
Chris Wilson04769652016-07-20 09:21:11 +0100860 intel_wait_init(&wait, req->fence.seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100861 if (intel_engine_add_wait(req->engine, &wait))
862 /* In order to check that we haven't missed the interrupt
863 * as we enabled it, we need to kick ourselves to do a
864 * coherent check on the seqno before we sleep.
865 */
866 goto wakeup;
867
868 for (;;) {
869 if (signal_pending_state(state, current)) {
870 ret = -ERESTARTSYS;
871 break;
872 }
873
874 timeout_remain = io_schedule_timeout(timeout_remain);
875 if (timeout_remain == 0) {
876 ret = -ETIME;
877 break;
878 }
879
880 if (intel_wait_complete(&wait))
881 break;
882
883 set_current_state(state);
884
885wakeup:
886 /* Carefully check if the request is complete, giving time
887 * for the seqno to be visible following the interrupt.
888 * We also have to check in case we are kicked by the GPU
889 * reset in order to drop the struct_mutex.
890 */
891 if (__i915_request_irq_complete(req))
892 break;
893
Chris Wilson221fe792016-09-09 14:11:51 +0100894 /* If the GPU is hung, and we hold the lock, reset the GPU
895 * and then check for completion. On a full reset, the engine's
896 * HW seqno will be advanced passed us and we are complete.
897 * If we do a partial reset, we have to wait for the GPU to
898 * resume and update the breadcrumb.
899 *
900 * If we don't hold the mutex, we can just wait for the worker
901 * to come along and update the breadcrumb (either directly
902 * itself, or indirectly by recovering the GPU).
903 */
904 if (flags & I915_WAIT_LOCKED &&
905 i915_reset_in_progress(&req->i915->gpu_error)) {
906 __set_current_state(TASK_RUNNING);
907 i915_reset(req->i915);
908 reset_wait_queue(&req->i915->gpu_error.wait_queue,
909 &reset);
910 continue;
911 }
912
Chris Wilson05235c52016-07-20 09:21:08 +0100913 /* Only spin if we know the GPU is processing this request */
914 if (i915_spin_request(req, state, 2))
915 break;
916 }
Chris Wilson05235c52016-07-20 09:21:08 +0100917
918 intel_engine_remove_wait(req->engine, &wait);
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100919 if (flags & I915_WAIT_LOCKED)
920 remove_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
Chris Wilson05235c52016-07-20 09:21:08 +0100921 __set_current_state(TASK_RUNNING);
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100922
Chris Wilson05235c52016-07-20 09:21:08 +0100923complete:
924 trace_i915_gem_request_wait_end(req);
925
926 if (timeout) {
927 *timeout -= ktime_get_raw_ns();
928 if (*timeout < 0)
929 *timeout = 0;
930
931 /*
932 * Apparently ktime isn't accurate enough and occasionally has a
933 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
934 * things up to make the test happy. We allow up to 1 jiffy.
935 *
936 * This is a regrssion from the timespec->ktime conversion.
937 */
938 if (ret == -ETIME && *timeout < jiffies_to_usecs(1)*1000)
939 *timeout = 0;
940 }
941
Chris Wilson42df2712016-07-20 09:21:12 +0100942 if (IS_RPS_USER(rps) &&
943 req->fence.seqno == req->engine->last_submitted_seqno) {
Chris Wilson05235c52016-07-20 09:21:08 +0100944 /* The GPU is now idle and this client has stalled.
945 * Since no other client has submitted a request in the
946 * meantime, assume that this client is the only one
947 * supplying work to the GPU but is unable to keep that
948 * work supplied because it is waiting. Since the GPU is
949 * then never kept fully busy, RPS autoclocking will
950 * keep the clocks relatively low, causing further delays.
951 * Compensate by giving the synchronous client credit for
952 * a waitboost next time.
953 */
954 spin_lock(&req->i915->rps.client_lock);
955 list_del_init(&rps->link);
956 spin_unlock(&req->i915->rps.client_lock);
957 }
958
959 return ret;
960}
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100961
Chris Wilsonf6407192016-08-27 08:54:00 +0100962static bool engine_retire_requests(struct intel_engine_cs *engine)
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100963{
964 struct drm_i915_gem_request *request, *next;
965
966 list_for_each_entry_safe(request, next, &engine->request_list, link) {
967 if (!i915_gem_request_completed(request))
Chris Wilsonf6407192016-08-27 08:54:00 +0100968 return false;
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100969
970 i915_gem_request_retire(request);
971 }
Chris Wilsonf6407192016-08-27 08:54:00 +0100972
973 return true;
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100974}
975
976void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
977{
978 struct intel_engine_cs *engine;
Chris Wilsonbafb0fc2016-08-27 08:54:01 +0100979 unsigned int tmp;
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100980
981 lockdep_assert_held(&dev_priv->drm.struct_mutex);
982
983 if (dev_priv->gt.active_engines == 0)
984 return;
985
986 GEM_BUG_ON(!dev_priv->gt.awake);
987
Chris Wilsonbafb0fc2016-08-27 08:54:01 +0100988 for_each_engine_masked(engine, dev_priv, dev_priv->gt.active_engines, tmp)
Chris Wilsonf6407192016-08-27 08:54:00 +0100989 if (engine_retire_requests(engine))
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100990 dev_priv->gt.active_engines &= ~intel_engine_flag(engine);
Chris Wilson4b8de8e2016-08-04 07:52:42 +0100991
992 if (dev_priv->gt.active_engines == 0)
993 queue_delayed_work(dev_priv->wq,
994 &dev_priv->gt.idle_work,
995 msecs_to_jiffies(100));
996}