blob: def35721e9ed7dce4de2f7f481ee85da39bddfd9 [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 Wilson04769652016-07-20 09:21:11 +010028#include <linux/fence.h>
29
30#include "i915_gem.h"
31
Chris Wilsondcff85c2016-08-05 10:14:11 +010032struct intel_wait {
33 struct rb_node node;
34 struct task_struct *tsk;
35 u32 seqno;
36};
37
38struct intel_signal_node {
39 struct rb_node node;
40 struct intel_wait wait;
41};
42
Chris Wilson05235c52016-07-20 09:21:08 +010043/**
44 * Request queue structure.
45 *
46 * The request queue allows us to note sequence numbers that have been emitted
47 * and may be associated with active buffers to be retired.
48 *
49 * By keeping this list, we can avoid having to do questionable sequence
50 * number comparisons on buffer last_read|write_seqno. It also allows an
51 * emission time to be associated with the request for tracking how far ahead
52 * of the GPU the submission is.
53 *
Chris Wilson5a198b82016-08-09 09:23:34 +010054 * When modifying this structure be very aware that we perform a lockless
55 * RCU lookup of it that may race against reallocation of the struct
56 * from the slab freelist. We intentionally do not zero the structure on
57 * allocation so that the lookup can use the dangling pointers (and is
58 * cogniscent that those pointers may be wrong). Instead, everything that
59 * needs to be initialised must be done so explicitly.
60 *
Chris Wilson04769652016-07-20 09:21:11 +010061 * The requests are reference counted.
Chris Wilson05235c52016-07-20 09:21:08 +010062 */
63struct drm_i915_gem_request {
Chris Wilson04769652016-07-20 09:21:11 +010064 struct fence fence;
65 spinlock_t lock;
Chris Wilson05235c52016-07-20 09:21:08 +010066
67 /** On Which ring this request was generated */
68 struct drm_i915_private *i915;
69
70 /**
71 * Context and ring buffer related to this request
72 * Contexts are refcounted, so when this request is associated with a
73 * context, we must increment the context's refcount, to guarantee that
74 * it persists while any request is linked to it. Requests themselves
75 * are also refcounted, so the request will only be freed when the last
76 * reference to it is dismissed, and the code in
77 * i915_gem_request_free() will then decrement the refcount on the
78 * context.
79 */
80 struct i915_gem_context *ctx;
81 struct intel_engine_cs *engine;
Chris Wilson7e37f882016-08-02 22:50:21 +010082 struct intel_ring *ring;
Chris Wilson05235c52016-07-20 09:21:08 +010083 struct intel_signal_node signaling;
84
85 /** GEM sequence number associated with the previous request,
86 * when the HWS breadcrumb is equal to this the GPU is processing
87 * this request.
88 */
89 u32 previous_seqno;
90
Chris Wilsona52abd22016-09-09 14:11:43 +010091 /** Position in the ring of the start of the request */
Chris Wilson05235c52016-07-20 09:21:08 +010092 u32 head;
93
94 /**
Chris Wilsona52abd22016-09-09 14:11:43 +010095 * Position in the ring of the start of the postfix.
96 * This is required to calculate the maximum available ring space
97 * without overwriting the postfix.
Chris Wilson05235c52016-07-20 09:21:08 +010098 */
99 u32 postfix;
100
Chris Wilsona52abd22016-09-09 14:11:43 +0100101 /** Position in the ring of the end of the whole request */
Chris Wilson05235c52016-07-20 09:21:08 +0100102 u32 tail;
103
Chris Wilsona52abd22016-09-09 14:11:43 +0100104 /** Position in the ring of the end of any workarounds after the tail */
105 u32 wa_tail;
106
107 /** Preallocate space in the ring for the emitting the request */
Chris Wilson05235c52016-07-20 09:21:08 +0100108 u32 reserved_space;
109
110 /**
111 * Context related to the previous request.
112 * As the contexts are accessed by the hardware until the switch is
113 * completed to a new context, the hardware may still be writing
114 * to the context object after the breadcrumb is visible. We must
115 * not unpin/unbind/prune that object whilst still active and so
116 * we keep the previous context pinned until the following (this)
117 * request is retired.
118 */
119 struct i915_gem_context *previous_context;
120
121 /** Batch buffer related to this request if any (used for
122 * error state dump only).
123 */
Chris Wilson058d88c2016-08-15 10:49:06 +0100124 struct i915_vma *batch;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100125 struct list_head active_list;
Chris Wilson05235c52016-07-20 09:21:08 +0100126
127 /** Time at which this request was emitted, in jiffies. */
128 unsigned long emitted_jiffies;
129
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100130 /** engine->request_list entry for this request */
131 struct list_head link;
Chris Wilson05235c52016-07-20 09:21:08 +0100132
Chris Wilson675d9ad2016-08-04 07:52:36 +0100133 /** ring->request_list entry for this request */
134 struct list_head ring_link;
135
Chris Wilson05235c52016-07-20 09:21:08 +0100136 struct drm_i915_file_private *file_priv;
137 /** file_priv list entry for this request */
138 struct list_head client_list;
139
Chris Wilson70c2a242016-09-09 14:11:46 +0100140 /** Link in the execlist submission queue, guarded by execlist_lock. */
Chris Wilson05235c52016-07-20 09:21:08 +0100141 struct list_head execlist_link;
Chris Wilson05235c52016-07-20 09:21:08 +0100142};
143
Chris Wilson04769652016-07-20 09:21:11 +0100144extern const struct fence_ops i915_fence_ops;
145
146static inline bool fence_is_i915(struct fence *fence)
147{
148 return fence->ops == &i915_fence_ops;
149}
150
Chris Wilson05235c52016-07-20 09:21:08 +0100151struct drm_i915_gem_request * __must_check
152i915_gem_request_alloc(struct intel_engine_cs *engine,
153 struct i915_gem_context *ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100154int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
155 struct drm_file *file);
156void i915_gem_request_retire_upto(struct drm_i915_gem_request *req);
157
158static inline u32
159i915_gem_request_get_seqno(struct drm_i915_gem_request *req)
160{
Chris Wilson04769652016-07-20 09:21:11 +0100161 return req ? req->fence.seqno : 0;
Chris Wilson05235c52016-07-20 09:21:08 +0100162}
163
164static inline struct intel_engine_cs *
165i915_gem_request_get_engine(struct drm_i915_gem_request *req)
166{
167 return req ? req->engine : NULL;
168}
169
170static inline struct drm_i915_gem_request *
Chris Wilson04769652016-07-20 09:21:11 +0100171to_request(struct fence *fence)
172{
173 /* We assume that NULL fence/request are interoperable */
174 BUILD_BUG_ON(offsetof(struct drm_i915_gem_request, fence) != 0);
175 GEM_BUG_ON(fence && !fence_is_i915(fence));
176 return container_of(fence, struct drm_i915_gem_request, fence);
177}
178
179static inline struct drm_i915_gem_request *
Chris Wilsone8a261e2016-07-20 13:31:49 +0100180i915_gem_request_get(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100181{
Chris Wilson04769652016-07-20 09:21:11 +0100182 return to_request(fence_get(&req->fence));
Chris Wilson05235c52016-07-20 09:21:08 +0100183}
184
Chris Wilson0eafec62016-08-04 16:32:41 +0100185static inline struct drm_i915_gem_request *
186i915_gem_request_get_rcu(struct drm_i915_gem_request *req)
187{
188 return to_request(fence_get_rcu(&req->fence));
189}
190
Chris Wilson05235c52016-07-20 09:21:08 +0100191static inline void
Chris Wilsone8a261e2016-07-20 13:31:49 +0100192i915_gem_request_put(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100193{
Chris Wilson04769652016-07-20 09:21:11 +0100194 fence_put(&req->fence);
Chris Wilson05235c52016-07-20 09:21:08 +0100195}
196
197static inline void i915_gem_request_assign(struct drm_i915_gem_request **pdst,
198 struct drm_i915_gem_request *src)
199{
200 if (src)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100201 i915_gem_request_get(src);
Chris Wilson05235c52016-07-20 09:21:08 +0100202
203 if (*pdst)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100204 i915_gem_request_put(*pdst);
Chris Wilson05235c52016-07-20 09:21:08 +0100205
206 *pdst = src;
207}
208
Chris Wilson17f298cf2016-08-10 13:41:46 +0100209void __i915_add_request(struct drm_i915_gem_request *req, bool flush_caches);
Chris Wilson05235c52016-07-20 09:21:08 +0100210#define i915_add_request(req) \
Chris Wilson17f298cf2016-08-10 13:41:46 +0100211 __i915_add_request(req, true)
Chris Wilson05235c52016-07-20 09:21:08 +0100212#define i915_add_request_no_flush(req) \
Chris Wilson17f298cf2016-08-10 13:41:46 +0100213 __i915_add_request(req, false)
Chris Wilson05235c52016-07-20 09:21:08 +0100214
215struct intel_rps_client;
Chris Wilson42df2712016-07-20 09:21:12 +0100216#define NO_WAITBOOST ERR_PTR(-1)
217#define IS_RPS_CLIENT(p) (!IS_ERR(p))
218#define IS_RPS_USER(p) (!IS_ERR_OR_NULL(p))
Chris Wilson05235c52016-07-20 09:21:08 +0100219
Chris Wilson776f3232016-08-04 07:52:40 +0100220int i915_wait_request(struct drm_i915_gem_request *req,
Chris Wilsonea746f32016-09-09 14:11:49 +0100221 unsigned int flags,
Chris Wilson776f3232016-08-04 07:52:40 +0100222 s64 *timeout,
223 struct intel_rps_client *rps)
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100224 __attribute__((nonnull(1)));
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100225#define I915_WAIT_INTERRUPTIBLE BIT(0)
226#define I915_WAIT_LOCKED BIT(1) /* struct_mutex held, handle GPU reset */
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100227
Chris Wilson05235c52016-07-20 09:21:08 +0100228static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine);
229
230/**
231 * Returns true if seq1 is later than seq2.
232 */
233static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
234{
235 return (s32)(seq1 - seq2) >= 0;
236}
237
238static inline bool
239i915_gem_request_started(const struct drm_i915_gem_request *req)
240{
241 return i915_seqno_passed(intel_engine_get_seqno(req->engine),
242 req->previous_seqno);
243}
244
245static inline bool
246i915_gem_request_completed(const struct drm_i915_gem_request *req)
247{
248 return i915_seqno_passed(intel_engine_get_seqno(req->engine),
Chris Wilson04769652016-07-20 09:21:11 +0100249 req->fence.seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100250}
251
252bool __i915_spin_request(const struct drm_i915_gem_request *request,
253 int state, unsigned long timeout_us);
254static inline bool i915_spin_request(const struct drm_i915_gem_request *request,
255 int state, unsigned long timeout_us)
256{
257 return (i915_gem_request_started(request) &&
258 __i915_spin_request(request, state, timeout_us));
259}
260
Chris Wilson381f3712016-08-04 07:52:29 +0100261/* We treat requests as fences. This is not be to confused with our
262 * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
263 * We use the fences to synchronize access from the CPU with activity on the
264 * GPU, for example, we should not rewrite an object's PTE whilst the GPU
265 * is reading them. We also track fences at a higher level to provide
266 * implicit synchronisation around GEM objects, e.g. set-domain will wait
267 * for outstanding GPU rendering before marking the object ready for CPU
268 * access, or a pageflip will wait until the GPU is complete before showing
269 * the frame on the scanout.
270 *
271 * In order to use a fence, the object must track the fence it needs to
272 * serialise with. For example, GEM objects want to track both read and
273 * write access so that we can perform concurrent read operations between
274 * the CPU and GPU engines, as well as waiting for all rendering to
275 * complete, or waiting for the last GPU user of a "fence register". The
276 * object then embeds a #i915_gem_active to track the most recent (in
277 * retirement order) request relevant for the desired mode of access.
278 * The #i915_gem_active is updated with i915_gem_active_set() to track the
279 * most recent fence request, typically this is done as part of
280 * i915_vma_move_to_active().
281 *
282 * When the #i915_gem_active completes (is retired), it will
283 * signal its completion to the owner through a callback as well as mark
284 * itself as idle (i915_gem_active.request == NULL). The owner
285 * can then perform any action, such as delayed freeing of an active
286 * resource including itself.
287 */
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100288struct i915_gem_active;
289
290typedef void (*i915_gem_retire_fn)(struct i915_gem_active *,
291 struct drm_i915_gem_request *);
292
Chris Wilson381f3712016-08-04 07:52:29 +0100293struct i915_gem_active {
Chris Wilson0eafec62016-08-04 16:32:41 +0100294 struct drm_i915_gem_request __rcu *request;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100295 struct list_head link;
296 i915_gem_retire_fn retire;
Chris Wilson381f3712016-08-04 07:52:29 +0100297};
298
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100299void i915_gem_retire_noop(struct i915_gem_active *,
300 struct drm_i915_gem_request *request);
301
302/**
303 * init_request_active - prepares the activity tracker for use
304 * @active - the active tracker
305 * @func - a callback when then the tracker is retired (becomes idle),
306 * can be NULL
307 *
308 * init_request_active() prepares the embedded @active struct for use as
309 * an activity tracker, that is for tracking the last known active request
310 * associated with it. When the last request becomes idle, when it is retired
311 * after completion, the optional callback @func is invoked.
312 */
313static inline void
314init_request_active(struct i915_gem_active *active,
315 i915_gem_retire_fn retire)
316{
317 INIT_LIST_HEAD(&active->link);
318 active->retire = retire ?: i915_gem_retire_noop;
319}
320
Chris Wilson27c01aa2016-08-04 07:52:30 +0100321/**
322 * i915_gem_active_set - updates the tracker to watch the current request
323 * @active - the active tracker
324 * @request - the request to watch
325 *
326 * i915_gem_active_set() watches the given @request for completion. Whilst
327 * that @request is busy, the @active reports busy. When that @request is
328 * retired, the @active tracker is updated to report idle.
329 */
Chris Wilson381f3712016-08-04 07:52:29 +0100330static inline void
331i915_gem_active_set(struct i915_gem_active *active,
332 struct drm_i915_gem_request *request)
333{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100334 list_move(&active->link, &request->active_list);
Chris Wilson0eafec62016-08-04 16:32:41 +0100335 rcu_assign_pointer(active->request, request);
Chris Wilson381f3712016-08-04 07:52:29 +0100336}
337
Chris Wilsond72d9082016-08-04 07:52:31 +0100338static inline struct drm_i915_gem_request *
339__i915_gem_active_peek(const struct i915_gem_active *active)
340{
Chris Wilson0eafec62016-08-04 16:32:41 +0100341 /* Inside the error capture (running with the driver in an unknown
342 * state), we want to bend the rules slightly (a lot).
343 *
344 * Work is in progress to make it safer, in the meantime this keeps
345 * the known issue from spamming the logs.
346 */
347 return rcu_dereference_protected(active->request, 1);
Chris Wilsond72d9082016-08-04 07:52:31 +0100348}
349
Chris Wilson27c01aa2016-08-04 07:52:30 +0100350/**
Chris Wilson385384a2016-08-09 08:37:01 +0100351 * i915_gem_active_raw - return the active request
352 * @active - the active tracker
353 *
354 * i915_gem_active_raw() returns the current request being tracked, or NULL.
355 * It does not obtain a reference on the request for the caller, so the caller
356 * must hold struct_mutex.
357 */
358static inline struct drm_i915_gem_request *
359i915_gem_active_raw(const struct i915_gem_active *active, struct mutex *mutex)
360{
361 return rcu_dereference_protected(active->request,
362 lockdep_is_held(mutex));
363}
364
365/**
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100366 * i915_gem_active_peek - report the active request being monitored
Chris Wilson27c01aa2016-08-04 07:52:30 +0100367 * @active - the active tracker
368 *
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100369 * i915_gem_active_peek() returns the current request being tracked if
370 * still active, or NULL. It does not obtain a reference on the request
371 * for the caller, so the caller must hold struct_mutex.
Chris Wilson27c01aa2016-08-04 07:52:30 +0100372 */
373static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100374i915_gem_active_peek(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100375{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100376 struct drm_i915_gem_request *request;
377
Chris Wilson385384a2016-08-09 08:37:01 +0100378 request = i915_gem_active_raw(active, mutex);
Chris Wilson0eafec62016-08-04 16:32:41 +0100379 if (!request || i915_gem_request_completed(request))
380 return NULL;
381
382 return request;
383}
384
385/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100386 * i915_gem_active_get - return a reference to the active request
387 * @active - the active tracker
388 *
389 * i915_gem_active_get() returns a reference to the active request, or NULL
390 * if the active tracker is idle. The caller must hold struct_mutex.
391 */
392static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100393i915_gem_active_get(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100394{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100395 return i915_gem_request_get(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100396}
397
398/**
Chris Wilson0eafec62016-08-04 16:32:41 +0100399 * __i915_gem_active_get_rcu - return a reference to the active request
400 * @active - the active tracker
401 *
402 * __i915_gem_active_get() returns a reference to the active request, or NULL
403 * if the active tracker is idle. The caller must hold the RCU read lock, but
404 * the returned pointer is safe to use outside of RCU.
405 */
406static inline struct drm_i915_gem_request *
407__i915_gem_active_get_rcu(const struct i915_gem_active *active)
408{
409 /* Performing a lockless retrieval of the active request is super
410 * tricky. SLAB_DESTROY_BY_RCU merely guarantees that the backing
411 * slab of request objects will not be freed whilst we hold the
412 * RCU read lock. It does not guarantee that the request itself
413 * will not be freed and then *reused*. Viz,
414 *
415 * Thread A Thread B
416 *
417 * req = active.request
418 * retire(req) -> free(req);
419 * (req is now first on the slab freelist)
420 * active.request = NULL
421 *
422 * req = new submission on a new object
423 * ref(req)
424 *
425 * To prevent the request from being reused whilst the caller
426 * uses it, we take a reference like normal. Whilst acquiring
427 * the reference we check that it is not in a destroyed state
428 * (refcnt == 0). That prevents the request being reallocated
429 * whilst the caller holds on to it. To check that the request
430 * was not reallocated as we acquired the reference we have to
431 * check that our request remains the active request across
432 * the lookup, in the same manner as a seqlock. The visibility
433 * of the pointer versus the reference counting is controlled
434 * by using RCU barriers (rcu_dereference and rcu_assign_pointer).
435 *
436 * In the middle of all that, we inspect whether the request is
437 * complete. Retiring is lazy so the request may be completed long
438 * before the active tracker is updated. Querying whether the
439 * request is complete is far cheaper (as it involves no locked
440 * instructions setting cachelines to exclusive) than acquiring
441 * the reference, so we do it first. The RCU read lock ensures the
442 * pointer dereference is valid, but does not ensure that the
443 * seqno nor HWS is the right one! However, if the request was
444 * reallocated, that means the active tracker's request was complete.
445 * If the new request is also complete, then both are and we can
446 * just report the active tracker is idle. If the new request is
447 * incomplete, then we acquire a reference on it and check that
448 * it remained the active request.
Chris Wilson5a198b82016-08-09 09:23:34 +0100449 *
450 * It is then imperative that we do not zero the request on
451 * reallocation, so that we can chase the dangling pointers!
452 * See i915_gem_request_alloc().
Chris Wilson0eafec62016-08-04 16:32:41 +0100453 */
454 do {
455 struct drm_i915_gem_request *request;
456
457 request = rcu_dereference(active->request);
458 if (!request || i915_gem_request_completed(request))
459 return NULL;
460
Daniel Vetterc75870d2016-08-22 10:55:22 +0200461 /* An especially silly compiler could decide to recompute the
462 * result of i915_gem_request_completed, more specifically
463 * re-emit the load for request->fence.seqno. A race would catch
464 * a later seqno value, which could flip the result from true to
465 * false. Which means part of the instructions below might not
466 * be executed, while later on instructions are executed. Due to
467 * barriers within the refcounting the inconsistency can't reach
468 * past the call to i915_gem_request_get_rcu, but not executing
469 * that while still executing i915_gem_request_put() creates
470 * havoc enough. Prevent this with a compiler barrier.
471 */
472 barrier();
473
Chris Wilson0eafec62016-08-04 16:32:41 +0100474 request = i915_gem_request_get_rcu(request);
475
476 /* What stops the following rcu_access_pointer() from occurring
477 * before the above i915_gem_request_get_rcu()? If we were
478 * to read the value before pausing to get the reference to
479 * the request, we may not notice a change in the active
480 * tracker.
481 *
482 * The rcu_access_pointer() is a mere compiler barrier, which
483 * means both the CPU and compiler are free to perform the
484 * memory read without constraint. The compiler only has to
485 * ensure that any operations after the rcu_access_pointer()
486 * occur afterwards in program order. This means the read may
487 * be performed earlier by an out-of-order CPU, or adventurous
488 * compiler.
489 *
490 * The atomic operation at the heart of
491 * i915_gem_request_get_rcu(), see fence_get_rcu(), is
492 * atomic_inc_not_zero() which is only a full memory barrier
493 * when successful. That is, if i915_gem_request_get_rcu()
494 * returns the request (and so with the reference counted
495 * incremented) then the following read for rcu_access_pointer()
496 * must occur after the atomic operation and so confirm
497 * that this request is the one currently being tracked.
Chris Wilsonedf6b762016-08-09 09:23:33 +0100498 *
499 * The corresponding write barrier is part of
500 * rcu_assign_pointer().
Chris Wilson0eafec62016-08-04 16:32:41 +0100501 */
502 if (!request || request == rcu_access_pointer(active->request))
503 return rcu_pointer_handoff(request);
504
505 i915_gem_request_put(request);
506 } while (1);
507}
508
509/**
510 * i915_gem_active_get_unlocked - return a reference to the active request
511 * @active - the active tracker
512 *
513 * i915_gem_active_get_unlocked() returns a reference to the active request,
514 * or NULL if the active tracker is idle. The reference is obtained under RCU,
515 * so no locking is required by the caller.
516 *
517 * The reference should be freed with i915_gem_request_put().
518 */
519static inline struct drm_i915_gem_request *
520i915_gem_active_get_unlocked(const struct i915_gem_active *active)
521{
522 struct drm_i915_gem_request *request;
523
524 rcu_read_lock();
525 request = __i915_gem_active_get_rcu(active);
526 rcu_read_unlock();
527
528 return request;
529}
530
531/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100532 * i915_gem_active_isset - report whether the active tracker is assigned
533 * @active - the active tracker
534 *
535 * i915_gem_active_isset() returns true if the active tracker is currently
536 * assigned to a request. Due to the lazy retiring, that request may be idle
537 * and this may report stale information.
538 */
539static inline bool
540i915_gem_active_isset(const struct i915_gem_active *active)
541{
Chris Wilson0eafec62016-08-04 16:32:41 +0100542 return rcu_access_pointer(active->request);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100543}
544
545/**
546 * i915_gem_active_is_idle - report whether the active tracker is idle
547 * @active - the active tracker
548 *
549 * i915_gem_active_is_idle() returns true if the active tracker is currently
550 * unassigned or if the request is complete (but not yet retired). Requires
551 * the caller to hold struct_mutex (but that can be relaxed if desired).
552 */
553static inline bool
Chris Wilsond72d9082016-08-04 07:52:31 +0100554i915_gem_active_is_idle(const struct i915_gem_active *active,
555 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100556{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100557 return !i915_gem_active_peek(active, mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100558}
559
560/**
561 * i915_gem_active_wait - waits until the request is completed
562 * @active - the active request on which to wait
563 *
564 * i915_gem_active_wait() waits until the request is completed before
565 * returning. Note that it does not guarantee that the request is
566 * retired first, see i915_gem_active_retire().
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100567 *
568 * i915_gem_active_wait() returns immediately if the active
569 * request is already complete.
Chris Wilson27c01aa2016-08-04 07:52:30 +0100570 */
571static inline int __must_check
Chris Wilsond72d9082016-08-04 07:52:31 +0100572i915_gem_active_wait(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100573{
574 struct drm_i915_gem_request *request;
575
Chris Wilsond72d9082016-08-04 07:52:31 +0100576 request = i915_gem_active_peek(active, mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100577 if (!request)
578 return 0;
579
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100580 return i915_wait_request(request,
581 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
582 NULL, NULL);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100583}
584
585/**
Chris Wilson24676582016-08-05 10:14:06 +0100586 * i915_gem_active_wait_unlocked - waits until the request is completed
587 * @active - the active request on which to wait
Chris Wilsonea746f32016-09-09 14:11:49 +0100588 * @flags - how to wait
Chris Wilson24676582016-08-05 10:14:06 +0100589 * @timeout - how long to wait at most
590 * @rps - userspace client to charge for a waitboost
591 *
592 * i915_gem_active_wait_unlocked() waits until the request is completed before
593 * returning, without requiring any locks to be held. Note that it does not
594 * retire any requests before returning.
595 *
596 * This function relies on RCU in order to acquire the reference to the active
597 * request without holding any locks. See __i915_gem_active_get_rcu() for the
598 * glory details on how that is managed. Once the reference is acquired, we
599 * can then wait upon the request, and afterwards release our reference,
600 * free of any locking.
601 *
602 * This function wraps i915_wait_request(), see it for the full details on
603 * the arguments.
604 *
605 * Returns 0 if successful, or a negative error code.
606 */
607static inline int
608i915_gem_active_wait_unlocked(const struct i915_gem_active *active,
Chris Wilsonea746f32016-09-09 14:11:49 +0100609 unsigned int flags,
Chris Wilson24676582016-08-05 10:14:06 +0100610 s64 *timeout,
611 struct intel_rps_client *rps)
612{
613 struct drm_i915_gem_request *request;
614 int ret = 0;
615
616 request = i915_gem_active_get_unlocked(active);
617 if (request) {
Chris Wilsonea746f32016-09-09 14:11:49 +0100618 ret = i915_wait_request(request, flags, timeout, rps);
Chris Wilson24676582016-08-05 10:14:06 +0100619 i915_gem_request_put(request);
620 }
621
622 return ret;
623}
624
625/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100626 * i915_gem_active_retire - waits until the request is retired
627 * @active - the active request on which to wait
628 *
629 * i915_gem_active_retire() waits until the request is completed,
630 * and then ensures that at least the retirement handler for this
631 * @active tracker is called before returning. If the @active
632 * tracker is idle, the function returns immediately.
633 */
634static inline int __must_check
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100635i915_gem_active_retire(struct i915_gem_active *active,
Chris Wilsond72d9082016-08-04 07:52:31 +0100636 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100637{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100638 struct drm_i915_gem_request *request;
639 int ret;
640
Chris Wilson385384a2016-08-09 08:37:01 +0100641 request = i915_gem_active_raw(active, mutex);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100642 if (!request)
643 return 0;
644
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100645 ret = i915_wait_request(request,
646 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
647 NULL, NULL);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100648 if (ret)
649 return ret;
650
651 list_del_init(&active->link);
Chris Wilson0eafec62016-08-04 16:32:41 +0100652 RCU_INIT_POINTER(active->request, NULL);
653
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100654 active->retire(active, request);
655
656 return 0;
Chris Wilson27c01aa2016-08-04 07:52:30 +0100657}
658
659/* Convenience functions for peeking at state inside active's request whilst
660 * guarded by the struct_mutex.
661 */
662
663static inline uint32_t
Chris Wilsond72d9082016-08-04 07:52:31 +0100664i915_gem_active_get_seqno(const struct i915_gem_active *active,
665 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100666{
Chris Wilsond72d9082016-08-04 07:52:31 +0100667 return i915_gem_request_get_seqno(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100668}
669
670static inline struct intel_engine_cs *
Chris Wilsond72d9082016-08-04 07:52:31 +0100671i915_gem_active_get_engine(const struct i915_gem_active *active,
672 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100673{
Chris Wilsond72d9082016-08-04 07:52:31 +0100674 return i915_gem_request_get_engine(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100675}
676
Chris Wilson381f3712016-08-04 07:52:29 +0100677#define for_each_active(mask, idx) \
678 for (; mask ? idx = ffs(mask) - 1, 1 : 0; mask &= ~BIT(idx))
679
Chris Wilson05235c52016-07-20 09:21:08 +0100680#endif /* I915_GEM_REQUEST_H */