blob: ae0913adfec62be89be36fab581231ef56bf0cfb [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
25#ifndef I915_GEM_REQUEST_H
26#define I915_GEM_REQUEST_H
27
Chris Wilsonf54d1862016-10-25 13:00:45 +010028#include <linux/dma-fence.h>
Chris Wilson04769652016-07-20 09:21:11 +010029
30#include "i915_gem.h"
Chris Wilson5590af32016-09-09 14:11:54 +010031#include "i915_sw_fence.h"
Chris Wilson04769652016-07-20 09:21:11 +010032
Chris Wilsondcff85c2016-08-05 10:14:11 +010033struct intel_wait {
34 struct rb_node node;
35 struct task_struct *tsk;
36 u32 seqno;
37};
38
39struct intel_signal_node {
40 struct rb_node node;
41 struct intel_wait wait;
42};
43
Chris Wilson05235c52016-07-20 09:21:08 +010044/**
45 * Request queue structure.
46 *
47 * The request queue allows us to note sequence numbers that have been emitted
48 * and may be associated with active buffers to be retired.
49 *
50 * By keeping this list, we can avoid having to do questionable sequence
51 * number comparisons on buffer last_read|write_seqno. It also allows an
52 * emission time to be associated with the request for tracking how far ahead
53 * of the GPU the submission is.
54 *
Chris Wilson5a198b82016-08-09 09:23:34 +010055 * When modifying this structure be very aware that we perform a lockless
56 * RCU lookup of it that may race against reallocation of the struct
57 * from the slab freelist. We intentionally do not zero the structure on
58 * allocation so that the lookup can use the dangling pointers (and is
59 * cogniscent that those pointers may be wrong). Instead, everything that
60 * needs to be initialised must be done so explicitly.
61 *
Chris Wilson04769652016-07-20 09:21:11 +010062 * The requests are reference counted.
Chris Wilson05235c52016-07-20 09:21:08 +010063 */
64struct drm_i915_gem_request {
Chris Wilsonf54d1862016-10-25 13:00:45 +010065 struct dma_fence fence;
Chris Wilson04769652016-07-20 09:21:11 +010066 spinlock_t lock;
Chris Wilson05235c52016-07-20 09:21:08 +010067
68 /** On Which ring this request was generated */
69 struct drm_i915_private *i915;
70
71 /**
72 * Context and ring buffer related to this request
73 * Contexts are refcounted, so when this request is associated with a
74 * context, we must increment the context's refcount, to guarantee that
75 * it persists while any request is linked to it. Requests themselves
76 * are also refcounted, so the request will only be freed when the last
77 * reference to it is dismissed, and the code in
78 * i915_gem_request_free() will then decrement the refcount on the
79 * context.
80 */
81 struct i915_gem_context *ctx;
82 struct intel_engine_cs *engine;
Chris Wilson7e37f882016-08-02 22:50:21 +010083 struct intel_ring *ring;
Chris Wilson05235c52016-07-20 09:21:08 +010084 struct intel_signal_node signaling;
85
Chris Wilson5590af32016-09-09 14:11:54 +010086 struct i915_sw_fence submit;
Chris Wilson0a046a02016-09-09 14:12:00 +010087 wait_queue_t submitq;
Chris Wilson5590af32016-09-09 14:11:54 +010088
Chris Wilson05235c52016-07-20 09:21:08 +010089 /** GEM sequence number associated with the previous request,
90 * when the HWS breadcrumb is equal to this the GPU is processing
91 * this request.
92 */
93 u32 previous_seqno;
94
Chris Wilsona52abd22016-09-09 14:11:43 +010095 /** Position in the ring of the start of the request */
Chris Wilson05235c52016-07-20 09:21:08 +010096 u32 head;
97
98 /**
Chris Wilsona52abd22016-09-09 14:11:43 +010099 * Position in the ring of the start of the postfix.
100 * This is required to calculate the maximum available ring space
101 * without overwriting the postfix.
Chris Wilson05235c52016-07-20 09:21:08 +0100102 */
103 u32 postfix;
104
Chris Wilsona52abd22016-09-09 14:11:43 +0100105 /** Position in the ring of the end of the whole request */
Chris Wilson05235c52016-07-20 09:21:08 +0100106 u32 tail;
107
Chris Wilsona52abd22016-09-09 14:11:43 +0100108 /** Position in the ring of the end of any workarounds after the tail */
109 u32 wa_tail;
110
111 /** Preallocate space in the ring for the emitting the request */
Chris Wilson05235c52016-07-20 09:21:08 +0100112 u32 reserved_space;
113
114 /**
115 * Context related to the previous request.
116 * As the contexts are accessed by the hardware until the switch is
117 * completed to a new context, the hardware may still be writing
118 * to the context object after the breadcrumb is visible. We must
119 * not unpin/unbind/prune that object whilst still active and so
120 * we keep the previous context pinned until the following (this)
121 * request is retired.
122 */
123 struct i915_gem_context *previous_context;
124
125 /** Batch buffer related to this request if any (used for
126 * error state dump only).
127 */
Chris Wilson058d88c2016-08-15 10:49:06 +0100128 struct i915_vma *batch;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100129 struct list_head active_list;
Chris Wilson05235c52016-07-20 09:21:08 +0100130
131 /** Time at which this request was emitted, in jiffies. */
132 unsigned long emitted_jiffies;
133
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100134 /** engine->request_list entry for this request */
135 struct list_head link;
Chris Wilson05235c52016-07-20 09:21:08 +0100136
Chris Wilson675d9ad2016-08-04 07:52:36 +0100137 /** ring->request_list entry for this request */
138 struct list_head ring_link;
139
Chris Wilson05235c52016-07-20 09:21:08 +0100140 struct drm_i915_file_private *file_priv;
141 /** file_priv list entry for this request */
142 struct list_head client_list;
143
Chris Wilson70c2a242016-09-09 14:11:46 +0100144 /** Link in the execlist submission queue, guarded by execlist_lock. */
Chris Wilson05235c52016-07-20 09:21:08 +0100145 struct list_head execlist_link;
Chris Wilson05235c52016-07-20 09:21:08 +0100146};
147
Chris Wilsonf54d1862016-10-25 13:00:45 +0100148extern const struct dma_fence_ops i915_fence_ops;
Chris Wilson04769652016-07-20 09:21:11 +0100149
Chris Wilsonb52992c2016-10-28 13:58:24 +0100150static inline bool dma_fence_is_i915(const struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +0100151{
152 return fence->ops == &i915_fence_ops;
153}
154
Chris Wilson05235c52016-07-20 09:21:08 +0100155struct drm_i915_gem_request * __must_check
156i915_gem_request_alloc(struct intel_engine_cs *engine,
157 struct i915_gem_context *ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100158int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
159 struct drm_file *file);
160void i915_gem_request_retire_upto(struct drm_i915_gem_request *req);
161
162static inline u32
163i915_gem_request_get_seqno(struct drm_i915_gem_request *req)
164{
Chris Wilson04769652016-07-20 09:21:11 +0100165 return req ? req->fence.seqno : 0;
Chris Wilson05235c52016-07-20 09:21:08 +0100166}
167
168static inline struct intel_engine_cs *
169i915_gem_request_get_engine(struct drm_i915_gem_request *req)
170{
171 return req ? req->engine : NULL;
172}
173
174static inline struct drm_i915_gem_request *
Chris Wilsonf54d1862016-10-25 13:00:45 +0100175to_request(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +0100176{
177 /* We assume that NULL fence/request are interoperable */
178 BUILD_BUG_ON(offsetof(struct drm_i915_gem_request, fence) != 0);
Chris Wilsonb52992c2016-10-28 13:58:24 +0100179 GEM_BUG_ON(fence && !dma_fence_is_i915(fence));
Chris Wilson04769652016-07-20 09:21:11 +0100180 return container_of(fence, struct drm_i915_gem_request, fence);
181}
182
183static inline struct drm_i915_gem_request *
Chris Wilsone8a261e2016-07-20 13:31:49 +0100184i915_gem_request_get(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100185{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100186 return to_request(dma_fence_get(&req->fence));
Chris Wilson05235c52016-07-20 09:21:08 +0100187}
188
Chris Wilson0eafec62016-08-04 16:32:41 +0100189static inline struct drm_i915_gem_request *
190i915_gem_request_get_rcu(struct drm_i915_gem_request *req)
191{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100192 return to_request(dma_fence_get_rcu(&req->fence));
Chris Wilson0eafec62016-08-04 16:32:41 +0100193}
194
Chris Wilson05235c52016-07-20 09:21:08 +0100195static inline void
Chris Wilsone8a261e2016-07-20 13:31:49 +0100196i915_gem_request_put(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100197{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100198 dma_fence_put(&req->fence);
Chris Wilson05235c52016-07-20 09:21:08 +0100199}
200
201static inline void i915_gem_request_assign(struct drm_i915_gem_request **pdst,
202 struct drm_i915_gem_request *src)
203{
204 if (src)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100205 i915_gem_request_get(src);
Chris Wilson05235c52016-07-20 09:21:08 +0100206
207 if (*pdst)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100208 i915_gem_request_put(*pdst);
Chris Wilson05235c52016-07-20 09:21:08 +0100209
210 *pdst = src;
211}
212
Chris Wilsona2bc4692016-09-09 14:11:56 +0100213int
214i915_gem_request_await_object(struct drm_i915_gem_request *to,
215 struct drm_i915_gem_object *obj,
216 bool write);
Chris Wilsonb52992c2016-10-28 13:58:24 +0100217int i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
218 struct dma_fence *fence);
Chris Wilsona2bc4692016-09-09 14:11:56 +0100219
Chris Wilson17f298cf2016-08-10 13:41:46 +0100220void __i915_add_request(struct drm_i915_gem_request *req, bool flush_caches);
Chris Wilson05235c52016-07-20 09:21:08 +0100221#define i915_add_request(req) \
Chris Wilson17f298cf2016-08-10 13:41:46 +0100222 __i915_add_request(req, true)
Chris Wilson05235c52016-07-20 09:21:08 +0100223#define i915_add_request_no_flush(req) \
Chris Wilson17f298cf2016-08-10 13:41:46 +0100224 __i915_add_request(req, false)
Chris Wilson05235c52016-07-20 09:21:08 +0100225
226struct intel_rps_client;
Chris Wilson42df2712016-07-20 09:21:12 +0100227#define NO_WAITBOOST ERR_PTR(-1)
228#define IS_RPS_CLIENT(p) (!IS_ERR(p))
229#define IS_RPS_USER(p) (!IS_ERR_OR_NULL(p))
Chris Wilson05235c52016-07-20 09:21:08 +0100230
Chris Wilsone95433c2016-10-28 13:58:27 +0100231long i915_wait_request(struct drm_i915_gem_request *req,
232 unsigned int flags,
233 long timeout)
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100234 __attribute__((nonnull(1)));
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100235#define I915_WAIT_INTERRUPTIBLE BIT(0)
236#define I915_WAIT_LOCKED BIT(1) /* struct_mutex held, handle GPU reset */
Chris Wilsone95433c2016-10-28 13:58:27 +0100237#define I915_WAIT_ALL BIT(2) /* used by i915_gem_object_wait() */
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100238
Chris Wilson05235c52016-07-20 09:21:08 +0100239static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine);
240
241/**
242 * Returns true if seq1 is later than seq2.
243 */
244static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
245{
246 return (s32)(seq1 - seq2) >= 0;
247}
248
249static inline bool
250i915_gem_request_started(const struct drm_i915_gem_request *req)
251{
252 return i915_seqno_passed(intel_engine_get_seqno(req->engine),
253 req->previous_seqno);
254}
255
256static inline bool
257i915_gem_request_completed(const struct drm_i915_gem_request *req)
258{
259 return i915_seqno_passed(intel_engine_get_seqno(req->engine),
Chris Wilson04769652016-07-20 09:21:11 +0100260 req->fence.seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100261}
262
263bool __i915_spin_request(const struct drm_i915_gem_request *request,
264 int state, unsigned long timeout_us);
265static inline bool i915_spin_request(const struct drm_i915_gem_request *request,
266 int state, unsigned long timeout_us)
267{
268 return (i915_gem_request_started(request) &&
269 __i915_spin_request(request, state, timeout_us));
270}
271
Chris Wilson381f3712016-08-04 07:52:29 +0100272/* We treat requests as fences. This is not be to confused with our
273 * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
274 * We use the fences to synchronize access from the CPU with activity on the
275 * GPU, for example, we should not rewrite an object's PTE whilst the GPU
276 * is reading them. We also track fences at a higher level to provide
277 * implicit synchronisation around GEM objects, e.g. set-domain will wait
278 * for outstanding GPU rendering before marking the object ready for CPU
279 * access, or a pageflip will wait until the GPU is complete before showing
280 * the frame on the scanout.
281 *
282 * In order to use a fence, the object must track the fence it needs to
283 * serialise with. For example, GEM objects want to track both read and
284 * write access so that we can perform concurrent read operations between
285 * the CPU and GPU engines, as well as waiting for all rendering to
286 * complete, or waiting for the last GPU user of a "fence register". The
287 * object then embeds a #i915_gem_active to track the most recent (in
288 * retirement order) request relevant for the desired mode of access.
289 * The #i915_gem_active is updated with i915_gem_active_set() to track the
290 * most recent fence request, typically this is done as part of
291 * i915_vma_move_to_active().
292 *
293 * When the #i915_gem_active completes (is retired), it will
294 * signal its completion to the owner through a callback as well as mark
295 * itself as idle (i915_gem_active.request == NULL). The owner
296 * can then perform any action, such as delayed freeing of an active
297 * resource including itself.
298 */
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100299struct i915_gem_active;
300
301typedef void (*i915_gem_retire_fn)(struct i915_gem_active *,
302 struct drm_i915_gem_request *);
303
Chris Wilson381f3712016-08-04 07:52:29 +0100304struct i915_gem_active {
Chris Wilson0eafec62016-08-04 16:32:41 +0100305 struct drm_i915_gem_request __rcu *request;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100306 struct list_head link;
307 i915_gem_retire_fn retire;
Chris Wilson381f3712016-08-04 07:52:29 +0100308};
309
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100310void i915_gem_retire_noop(struct i915_gem_active *,
311 struct drm_i915_gem_request *request);
312
313/**
314 * init_request_active - prepares the activity tracker for use
315 * @active - the active tracker
316 * @func - a callback when then the tracker is retired (becomes idle),
317 * can be NULL
318 *
319 * init_request_active() prepares the embedded @active struct for use as
320 * an activity tracker, that is for tracking the last known active request
321 * associated with it. When the last request becomes idle, when it is retired
322 * after completion, the optional callback @func is invoked.
323 */
324static inline void
325init_request_active(struct i915_gem_active *active,
326 i915_gem_retire_fn retire)
327{
328 INIT_LIST_HEAD(&active->link);
329 active->retire = retire ?: i915_gem_retire_noop;
330}
331
Chris Wilson27c01aa2016-08-04 07:52:30 +0100332/**
333 * i915_gem_active_set - updates the tracker to watch the current request
334 * @active - the active tracker
335 * @request - the request to watch
336 *
337 * i915_gem_active_set() watches the given @request for completion. Whilst
338 * that @request is busy, the @active reports busy. When that @request is
339 * retired, the @active tracker is updated to report idle.
340 */
Chris Wilson381f3712016-08-04 07:52:29 +0100341static inline void
342i915_gem_active_set(struct i915_gem_active *active,
343 struct drm_i915_gem_request *request)
344{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100345 list_move(&active->link, &request->active_list);
Chris Wilson0eafec62016-08-04 16:32:41 +0100346 rcu_assign_pointer(active->request, request);
Chris Wilson381f3712016-08-04 07:52:29 +0100347}
348
Chris Wilsond72d9082016-08-04 07:52:31 +0100349static inline struct drm_i915_gem_request *
350__i915_gem_active_peek(const struct i915_gem_active *active)
351{
Chris Wilson0eafec62016-08-04 16:32:41 +0100352 /* Inside the error capture (running with the driver in an unknown
353 * state), we want to bend the rules slightly (a lot).
354 *
355 * Work is in progress to make it safer, in the meantime this keeps
356 * the known issue from spamming the logs.
357 */
358 return rcu_dereference_protected(active->request, 1);
Chris Wilsond72d9082016-08-04 07:52:31 +0100359}
360
Chris Wilson27c01aa2016-08-04 07:52:30 +0100361/**
Chris Wilson385384a2016-08-09 08:37:01 +0100362 * i915_gem_active_raw - return the active request
363 * @active - the active tracker
364 *
365 * i915_gem_active_raw() returns the current request being tracked, or NULL.
366 * It does not obtain a reference on the request for the caller, so the caller
367 * must hold struct_mutex.
368 */
369static inline struct drm_i915_gem_request *
370i915_gem_active_raw(const struct i915_gem_active *active, struct mutex *mutex)
371{
372 return rcu_dereference_protected(active->request,
373 lockdep_is_held(mutex));
374}
375
376/**
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100377 * i915_gem_active_peek - report the active request being monitored
Chris Wilson27c01aa2016-08-04 07:52:30 +0100378 * @active - the active tracker
379 *
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100380 * i915_gem_active_peek() returns the current request being tracked if
381 * still active, or NULL. It does not obtain a reference on the request
382 * for the caller, so the caller must hold struct_mutex.
Chris Wilson27c01aa2016-08-04 07:52:30 +0100383 */
384static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100385i915_gem_active_peek(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100386{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100387 struct drm_i915_gem_request *request;
388
Chris Wilson385384a2016-08-09 08:37:01 +0100389 request = i915_gem_active_raw(active, mutex);
Chris Wilson0eafec62016-08-04 16:32:41 +0100390 if (!request || i915_gem_request_completed(request))
391 return NULL;
392
393 return request;
394}
395
396/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100397 * i915_gem_active_get - return a reference to the active request
398 * @active - the active tracker
399 *
400 * i915_gem_active_get() returns a reference to the active request, or NULL
401 * if the active tracker is idle. The caller must hold struct_mutex.
402 */
403static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100404i915_gem_active_get(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100405{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100406 return i915_gem_request_get(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100407}
408
409/**
Chris Wilson0eafec62016-08-04 16:32:41 +0100410 * __i915_gem_active_get_rcu - return a reference to the active request
411 * @active - the active tracker
412 *
413 * __i915_gem_active_get() returns a reference to the active request, or NULL
414 * if the active tracker is idle. The caller must hold the RCU read lock, but
415 * the returned pointer is safe to use outside of RCU.
416 */
417static inline struct drm_i915_gem_request *
418__i915_gem_active_get_rcu(const struct i915_gem_active *active)
419{
420 /* Performing a lockless retrieval of the active request is super
421 * tricky. SLAB_DESTROY_BY_RCU merely guarantees that the backing
422 * slab of request objects will not be freed whilst we hold the
423 * RCU read lock. It does not guarantee that the request itself
424 * will not be freed and then *reused*. Viz,
425 *
426 * Thread A Thread B
427 *
428 * req = active.request
429 * retire(req) -> free(req);
430 * (req is now first on the slab freelist)
431 * active.request = NULL
432 *
433 * req = new submission on a new object
434 * ref(req)
435 *
436 * To prevent the request from being reused whilst the caller
437 * uses it, we take a reference like normal. Whilst acquiring
438 * the reference we check that it is not in a destroyed state
439 * (refcnt == 0). That prevents the request being reallocated
440 * whilst the caller holds on to it. To check that the request
441 * was not reallocated as we acquired the reference we have to
442 * check that our request remains the active request across
443 * the lookup, in the same manner as a seqlock. The visibility
444 * of the pointer versus the reference counting is controlled
445 * by using RCU barriers (rcu_dereference and rcu_assign_pointer).
446 *
447 * In the middle of all that, we inspect whether the request is
448 * complete. Retiring is lazy so the request may be completed long
449 * before the active tracker is updated. Querying whether the
450 * request is complete is far cheaper (as it involves no locked
451 * instructions setting cachelines to exclusive) than acquiring
452 * the reference, so we do it first. The RCU read lock ensures the
453 * pointer dereference is valid, but does not ensure that the
454 * seqno nor HWS is the right one! However, if the request was
455 * reallocated, that means the active tracker's request was complete.
456 * If the new request is also complete, then both are and we can
457 * just report the active tracker is idle. If the new request is
458 * incomplete, then we acquire a reference on it and check that
459 * it remained the active request.
Chris Wilson5a198b82016-08-09 09:23:34 +0100460 *
461 * It is then imperative that we do not zero the request on
462 * reallocation, so that we can chase the dangling pointers!
463 * See i915_gem_request_alloc().
Chris Wilson0eafec62016-08-04 16:32:41 +0100464 */
465 do {
466 struct drm_i915_gem_request *request;
467
468 request = rcu_dereference(active->request);
469 if (!request || i915_gem_request_completed(request))
470 return NULL;
471
Daniel Vetterc75870d2016-08-22 10:55:22 +0200472 /* An especially silly compiler could decide to recompute the
473 * result of i915_gem_request_completed, more specifically
474 * re-emit the load for request->fence.seqno. A race would catch
475 * a later seqno value, which could flip the result from true to
476 * false. Which means part of the instructions below might not
477 * be executed, while later on instructions are executed. Due to
478 * barriers within the refcounting the inconsistency can't reach
479 * past the call to i915_gem_request_get_rcu, but not executing
480 * that while still executing i915_gem_request_put() creates
481 * havoc enough. Prevent this with a compiler barrier.
482 */
483 barrier();
484
Chris Wilson0eafec62016-08-04 16:32:41 +0100485 request = i915_gem_request_get_rcu(request);
486
487 /* What stops the following rcu_access_pointer() from occurring
488 * before the above i915_gem_request_get_rcu()? If we were
489 * to read the value before pausing to get the reference to
490 * the request, we may not notice a change in the active
491 * tracker.
492 *
493 * The rcu_access_pointer() is a mere compiler barrier, which
494 * means both the CPU and compiler are free to perform the
495 * memory read without constraint. The compiler only has to
496 * ensure that any operations after the rcu_access_pointer()
497 * occur afterwards in program order. This means the read may
498 * be performed earlier by an out-of-order CPU, or adventurous
499 * compiler.
500 *
501 * The atomic operation at the heart of
Chris Wilsonf54d1862016-10-25 13:00:45 +0100502 * i915_gem_request_get_rcu(), see dma_fence_get_rcu(), is
Chris Wilson0eafec62016-08-04 16:32:41 +0100503 * atomic_inc_not_zero() which is only a full memory barrier
504 * when successful. That is, if i915_gem_request_get_rcu()
505 * returns the request (and so with the reference counted
506 * incremented) then the following read for rcu_access_pointer()
507 * must occur after the atomic operation and so confirm
508 * that this request is the one currently being tracked.
Chris Wilsonedf6b762016-08-09 09:23:33 +0100509 *
510 * The corresponding write barrier is part of
511 * rcu_assign_pointer().
Chris Wilson0eafec62016-08-04 16:32:41 +0100512 */
513 if (!request || request == rcu_access_pointer(active->request))
514 return rcu_pointer_handoff(request);
515
516 i915_gem_request_put(request);
517 } while (1);
518}
519
520/**
521 * i915_gem_active_get_unlocked - return a reference to the active request
522 * @active - the active tracker
523 *
524 * i915_gem_active_get_unlocked() returns a reference to the active request,
525 * or NULL if the active tracker is idle. The reference is obtained under RCU,
526 * so no locking is required by the caller.
527 *
528 * The reference should be freed with i915_gem_request_put().
529 */
530static inline struct drm_i915_gem_request *
531i915_gem_active_get_unlocked(const struct i915_gem_active *active)
532{
533 struct drm_i915_gem_request *request;
534
535 rcu_read_lock();
536 request = __i915_gem_active_get_rcu(active);
537 rcu_read_unlock();
538
539 return request;
540}
541
542/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100543 * i915_gem_active_isset - report whether the active tracker is assigned
544 * @active - the active tracker
545 *
546 * i915_gem_active_isset() returns true if the active tracker is currently
547 * assigned to a request. Due to the lazy retiring, that request may be idle
548 * and this may report stale information.
549 */
550static inline bool
551i915_gem_active_isset(const struct i915_gem_active *active)
552{
Chris Wilson0eafec62016-08-04 16:32:41 +0100553 return rcu_access_pointer(active->request);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100554}
555
556/**
557 * i915_gem_active_is_idle - report whether the active tracker is idle
558 * @active - the active tracker
559 *
560 * i915_gem_active_is_idle() returns true if the active tracker is currently
561 * unassigned or if the request is complete (but not yet retired). Requires
562 * the caller to hold struct_mutex (but that can be relaxed if desired).
563 */
564static inline bool
Chris Wilsond72d9082016-08-04 07:52:31 +0100565i915_gem_active_is_idle(const struct i915_gem_active *active,
566 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100567{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100568 return !i915_gem_active_peek(active, mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100569}
570
571/**
572 * i915_gem_active_wait - waits until the request is completed
573 * @active - the active request on which to wait
574 *
575 * i915_gem_active_wait() waits until the request is completed before
576 * returning. Note that it does not guarantee that the request is
577 * retired first, see i915_gem_active_retire().
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100578 *
579 * i915_gem_active_wait() returns immediately if the active
580 * request is already complete.
Chris Wilson27c01aa2016-08-04 07:52:30 +0100581 */
582static inline int __must_check
Chris Wilsond72d9082016-08-04 07:52:31 +0100583i915_gem_active_wait(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100584{
585 struct drm_i915_gem_request *request;
Chris Wilsone95433c2016-10-28 13:58:27 +0100586 long ret;
Chris Wilson27c01aa2016-08-04 07:52:30 +0100587
Chris Wilsond72d9082016-08-04 07:52:31 +0100588 request = i915_gem_active_peek(active, mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100589 if (!request)
590 return 0;
591
Chris Wilsone95433c2016-10-28 13:58:27 +0100592 ret = i915_wait_request(request,
593 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
594 MAX_SCHEDULE_TIMEOUT);
595 return ret < 0 ? ret : 0;
Chris Wilson27c01aa2016-08-04 07:52:30 +0100596}
597
598/**
Chris Wilson24676582016-08-05 10:14:06 +0100599 * i915_gem_active_wait_unlocked - waits until the request is completed
600 * @active - the active request on which to wait
Chris Wilsonea746f32016-09-09 14:11:49 +0100601 * @flags - how to wait
Chris Wilson24676582016-08-05 10:14:06 +0100602 * @timeout - how long to wait at most
603 * @rps - userspace client to charge for a waitboost
604 *
605 * i915_gem_active_wait_unlocked() waits until the request is completed before
606 * returning, without requiring any locks to be held. Note that it does not
607 * retire any requests before returning.
608 *
609 * This function relies on RCU in order to acquire the reference to the active
610 * request without holding any locks. See __i915_gem_active_get_rcu() for the
611 * glory details on how that is managed. Once the reference is acquired, we
612 * can then wait upon the request, and afterwards release our reference,
613 * free of any locking.
614 *
615 * This function wraps i915_wait_request(), see it for the full details on
616 * the arguments.
617 *
618 * Returns 0 if successful, or a negative error code.
619 */
620static inline int
621i915_gem_active_wait_unlocked(const struct i915_gem_active *active,
Chris Wilsone95433c2016-10-28 13:58:27 +0100622 unsigned int flags)
Chris Wilson24676582016-08-05 10:14:06 +0100623{
624 struct drm_i915_gem_request *request;
Chris Wilsone95433c2016-10-28 13:58:27 +0100625 long ret = 0;
Chris Wilson24676582016-08-05 10:14:06 +0100626
627 request = i915_gem_active_get_unlocked(active);
628 if (request) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100629 ret = i915_wait_request(request, flags, MAX_SCHEDULE_TIMEOUT);
Chris Wilson24676582016-08-05 10:14:06 +0100630 i915_gem_request_put(request);
631 }
632
Chris Wilsone95433c2016-10-28 13:58:27 +0100633 return ret < 0 ? ret : 0;
Chris Wilson24676582016-08-05 10:14:06 +0100634}
635
636/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100637 * i915_gem_active_retire - waits until the request is retired
638 * @active - the active request on which to wait
639 *
640 * i915_gem_active_retire() waits until the request is completed,
641 * and then ensures that at least the retirement handler for this
642 * @active tracker is called before returning. If the @active
643 * tracker is idle, the function returns immediately.
644 */
645static inline int __must_check
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100646i915_gem_active_retire(struct i915_gem_active *active,
Chris Wilsond72d9082016-08-04 07:52:31 +0100647 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100648{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100649 struct drm_i915_gem_request *request;
Chris Wilsone95433c2016-10-28 13:58:27 +0100650 long ret;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100651
Chris Wilson385384a2016-08-09 08:37:01 +0100652 request = i915_gem_active_raw(active, mutex);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100653 if (!request)
654 return 0;
655
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100656 ret = i915_wait_request(request,
657 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
Chris Wilsone95433c2016-10-28 13:58:27 +0100658 MAX_SCHEDULE_TIMEOUT);
659 if (ret < 0)
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100660 return ret;
661
662 list_del_init(&active->link);
Chris Wilson0eafec62016-08-04 16:32:41 +0100663 RCU_INIT_POINTER(active->request, NULL);
664
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100665 active->retire(active, request);
666
667 return 0;
Chris Wilson27c01aa2016-08-04 07:52:30 +0100668}
669
670/* Convenience functions for peeking at state inside active's request whilst
671 * guarded by the struct_mutex.
672 */
673
674static inline uint32_t
Chris Wilsond72d9082016-08-04 07:52:31 +0100675i915_gem_active_get_seqno(const struct i915_gem_active *active,
676 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100677{
Chris Wilsond72d9082016-08-04 07:52:31 +0100678 return i915_gem_request_get_seqno(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100679}
680
681static inline struct intel_engine_cs *
Chris Wilsond72d9082016-08-04 07:52:31 +0100682i915_gem_active_get_engine(const struct i915_gem_active *active,
683 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100684{
Chris Wilsond72d9082016-08-04 07:52:31 +0100685 return i915_gem_request_get_engine(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100686}
687
Chris Wilson381f3712016-08-04 07:52:29 +0100688#define for_each_active(mask, idx) \
689 for (; mask ? idx = ffs(mask) - 1, 1 : 0; mask &= ~BIT(idx))
690
Chris Wilson05235c52016-07-20 09:21:08 +0100691#endif /* I915_GEM_REQUEST_H */