blob: 85393be09c2778f478df1c8285ffdd7ea15b5b06 [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 Wilson05235c52016-07-20 09:21:08 +010091 /** Position in the ringbuffer of the start of the request */
92 u32 head;
93
94 /**
95 * Position in the ringbuffer of the start of the postfix.
96 * This is required to calculate the maximum available ringbuffer
97 * space without overwriting the postfix.
98 */
99 u32 postfix;
100
101 /** Position in the ringbuffer of the end of the whole request */
102 u32 tail;
103
104 /** Preallocate space in the ringbuffer for the emitting the request */
105 u32 reserved_space;
106
107 /**
108 * Context related to the previous request.
109 * As the contexts are accessed by the hardware until the switch is
110 * completed to a new context, the hardware may still be writing
111 * to the context object after the breadcrumb is visible. We must
112 * not unpin/unbind/prune that object whilst still active and so
113 * we keep the previous context pinned until the following (this)
114 * request is retired.
115 */
116 struct i915_gem_context *previous_context;
117
118 /** Batch buffer related to this request if any (used for
119 * error state dump only).
120 */
121 struct drm_i915_gem_object *batch_obj;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100122 struct list_head active_list;
Chris Wilson05235c52016-07-20 09:21:08 +0100123
124 /** Time at which this request was emitted, in jiffies. */
125 unsigned long emitted_jiffies;
126
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100127 /** engine->request_list entry for this request */
128 struct list_head link;
Chris Wilson05235c52016-07-20 09:21:08 +0100129
Chris Wilson675d9ad2016-08-04 07:52:36 +0100130 /** ring->request_list entry for this request */
131 struct list_head ring_link;
132
Chris Wilson05235c52016-07-20 09:21:08 +0100133 struct drm_i915_file_private *file_priv;
134 /** file_priv list entry for this request */
135 struct list_head client_list;
136
137 /** process identifier submitting this request */
138 struct pid *pid;
139
140 /**
141 * The ELSP only accepts two elements at a time, so we queue
142 * context/tail pairs on a given queue (ring->execlist_queue) until the
143 * hardware is available. The queue serves a double purpose: we also use
144 * it to keep track of the up to 2 contexts currently in the hardware
145 * (usually one in execution and the other queued up by the GPU): We
146 * only remove elements from the head of the queue when the hardware
147 * informs us that an element has been completed.
148 *
149 * All accesses to the queue are mediated by a spinlock
150 * (ring->execlist_lock).
151 */
152
153 /** Execlist link in the submission queue.*/
154 struct list_head execlist_link;
155
156 /** Execlists no. of times this request has been sent to the ELSP */
157 int elsp_submitted;
158
159 /** Execlists context hardware id. */
160 unsigned int ctx_hw_id;
161};
162
Chris Wilson04769652016-07-20 09:21:11 +0100163extern const struct fence_ops i915_fence_ops;
164
165static inline bool fence_is_i915(struct fence *fence)
166{
167 return fence->ops == &i915_fence_ops;
168}
169
Chris Wilson05235c52016-07-20 09:21:08 +0100170struct drm_i915_gem_request * __must_check
171i915_gem_request_alloc(struct intel_engine_cs *engine,
172 struct i915_gem_context *ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100173int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
174 struct drm_file *file);
175void i915_gem_request_retire_upto(struct drm_i915_gem_request *req);
176
177static inline u32
178i915_gem_request_get_seqno(struct drm_i915_gem_request *req)
179{
Chris Wilson04769652016-07-20 09:21:11 +0100180 return req ? req->fence.seqno : 0;
Chris Wilson05235c52016-07-20 09:21:08 +0100181}
182
183static inline struct intel_engine_cs *
184i915_gem_request_get_engine(struct drm_i915_gem_request *req)
185{
186 return req ? req->engine : NULL;
187}
188
189static inline struct drm_i915_gem_request *
Chris Wilson04769652016-07-20 09:21:11 +0100190to_request(struct fence *fence)
191{
192 /* We assume that NULL fence/request are interoperable */
193 BUILD_BUG_ON(offsetof(struct drm_i915_gem_request, fence) != 0);
194 GEM_BUG_ON(fence && !fence_is_i915(fence));
195 return container_of(fence, struct drm_i915_gem_request, fence);
196}
197
198static inline struct drm_i915_gem_request *
Chris Wilsone8a261e2016-07-20 13:31:49 +0100199i915_gem_request_get(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100200{
Chris Wilson04769652016-07-20 09:21:11 +0100201 return to_request(fence_get(&req->fence));
Chris Wilson05235c52016-07-20 09:21:08 +0100202}
203
Chris Wilson0eafec62016-08-04 16:32:41 +0100204static inline struct drm_i915_gem_request *
205i915_gem_request_get_rcu(struct drm_i915_gem_request *req)
206{
207 return to_request(fence_get_rcu(&req->fence));
208}
209
Chris Wilson05235c52016-07-20 09:21:08 +0100210static inline void
Chris Wilsone8a261e2016-07-20 13:31:49 +0100211i915_gem_request_put(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100212{
Chris Wilson04769652016-07-20 09:21:11 +0100213 fence_put(&req->fence);
Chris Wilson05235c52016-07-20 09:21:08 +0100214}
215
216static inline void i915_gem_request_assign(struct drm_i915_gem_request **pdst,
217 struct drm_i915_gem_request *src)
218{
219 if (src)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100220 i915_gem_request_get(src);
Chris Wilson05235c52016-07-20 09:21:08 +0100221
222 if (*pdst)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100223 i915_gem_request_put(*pdst);
Chris Wilson05235c52016-07-20 09:21:08 +0100224
225 *pdst = src;
226}
227
Chris Wilson17f298cf2016-08-10 13:41:46 +0100228void __i915_add_request(struct drm_i915_gem_request *req, bool flush_caches);
Chris Wilson05235c52016-07-20 09:21:08 +0100229#define i915_add_request(req) \
Chris Wilson17f298cf2016-08-10 13:41:46 +0100230 __i915_add_request(req, true)
Chris Wilson05235c52016-07-20 09:21:08 +0100231#define i915_add_request_no_flush(req) \
Chris Wilson17f298cf2016-08-10 13:41:46 +0100232 __i915_add_request(req, false)
Chris Wilson05235c52016-07-20 09:21:08 +0100233
234struct intel_rps_client;
Chris Wilson42df2712016-07-20 09:21:12 +0100235#define NO_WAITBOOST ERR_PTR(-1)
236#define IS_RPS_CLIENT(p) (!IS_ERR(p))
237#define IS_RPS_USER(p) (!IS_ERR_OR_NULL(p))
Chris Wilson05235c52016-07-20 09:21:08 +0100238
Chris Wilson776f3232016-08-04 07:52:40 +0100239int i915_wait_request(struct drm_i915_gem_request *req,
240 bool interruptible,
241 s64 *timeout,
242 struct intel_rps_client *rps)
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100243 __attribute__((nonnull(1)));
244
Chris Wilson05235c52016-07-20 09:21:08 +0100245static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine);
246
247/**
248 * Returns true if seq1 is later than seq2.
249 */
250static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
251{
252 return (s32)(seq1 - seq2) >= 0;
253}
254
255static inline bool
256i915_gem_request_started(const struct drm_i915_gem_request *req)
257{
258 return i915_seqno_passed(intel_engine_get_seqno(req->engine),
259 req->previous_seqno);
260}
261
262static inline bool
263i915_gem_request_completed(const struct drm_i915_gem_request *req)
264{
265 return i915_seqno_passed(intel_engine_get_seqno(req->engine),
Chris Wilson04769652016-07-20 09:21:11 +0100266 req->fence.seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100267}
268
269bool __i915_spin_request(const struct drm_i915_gem_request *request,
270 int state, unsigned long timeout_us);
271static inline bool i915_spin_request(const struct drm_i915_gem_request *request,
272 int state, unsigned long timeout_us)
273{
274 return (i915_gem_request_started(request) &&
275 __i915_spin_request(request, state, timeout_us));
276}
277
Chris Wilson381f3712016-08-04 07:52:29 +0100278/* We treat requests as fences. This is not be to confused with our
279 * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
280 * We use the fences to synchronize access from the CPU with activity on the
281 * GPU, for example, we should not rewrite an object's PTE whilst the GPU
282 * is reading them. We also track fences at a higher level to provide
283 * implicit synchronisation around GEM objects, e.g. set-domain will wait
284 * for outstanding GPU rendering before marking the object ready for CPU
285 * access, or a pageflip will wait until the GPU is complete before showing
286 * the frame on the scanout.
287 *
288 * In order to use a fence, the object must track the fence it needs to
289 * serialise with. For example, GEM objects want to track both read and
290 * write access so that we can perform concurrent read operations between
291 * the CPU and GPU engines, as well as waiting for all rendering to
292 * complete, or waiting for the last GPU user of a "fence register". The
293 * object then embeds a #i915_gem_active to track the most recent (in
294 * retirement order) request relevant for the desired mode of access.
295 * The #i915_gem_active is updated with i915_gem_active_set() to track the
296 * most recent fence request, typically this is done as part of
297 * i915_vma_move_to_active().
298 *
299 * When the #i915_gem_active completes (is retired), it will
300 * signal its completion to the owner through a callback as well as mark
301 * itself as idle (i915_gem_active.request == NULL). The owner
302 * can then perform any action, such as delayed freeing of an active
303 * resource including itself.
304 */
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100305struct i915_gem_active;
306
307typedef void (*i915_gem_retire_fn)(struct i915_gem_active *,
308 struct drm_i915_gem_request *);
309
Chris Wilson381f3712016-08-04 07:52:29 +0100310struct i915_gem_active {
Chris Wilson0eafec62016-08-04 16:32:41 +0100311 struct drm_i915_gem_request __rcu *request;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100312 struct list_head link;
313 i915_gem_retire_fn retire;
Chris Wilson381f3712016-08-04 07:52:29 +0100314};
315
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100316void i915_gem_retire_noop(struct i915_gem_active *,
317 struct drm_i915_gem_request *request);
318
319/**
320 * init_request_active - prepares the activity tracker for use
321 * @active - the active tracker
322 * @func - a callback when then the tracker is retired (becomes idle),
323 * can be NULL
324 *
325 * init_request_active() prepares the embedded @active struct for use as
326 * an activity tracker, that is for tracking the last known active request
327 * associated with it. When the last request becomes idle, when it is retired
328 * after completion, the optional callback @func is invoked.
329 */
330static inline void
331init_request_active(struct i915_gem_active *active,
332 i915_gem_retire_fn retire)
333{
334 INIT_LIST_HEAD(&active->link);
335 active->retire = retire ?: i915_gem_retire_noop;
336}
337
Chris Wilson27c01aa2016-08-04 07:52:30 +0100338/**
339 * i915_gem_active_set - updates the tracker to watch the current request
340 * @active - the active tracker
341 * @request - the request to watch
342 *
343 * i915_gem_active_set() watches the given @request for completion. Whilst
344 * that @request is busy, the @active reports busy. When that @request is
345 * retired, the @active tracker is updated to report idle.
346 */
Chris Wilson381f3712016-08-04 07:52:29 +0100347static inline void
348i915_gem_active_set(struct i915_gem_active *active,
349 struct drm_i915_gem_request *request)
350{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100351 list_move(&active->link, &request->active_list);
Chris Wilson0eafec62016-08-04 16:32:41 +0100352 rcu_assign_pointer(active->request, request);
Chris Wilson381f3712016-08-04 07:52:29 +0100353}
354
Chris Wilsond72d9082016-08-04 07:52:31 +0100355static inline struct drm_i915_gem_request *
356__i915_gem_active_peek(const struct i915_gem_active *active)
357{
Chris Wilson0eafec62016-08-04 16:32:41 +0100358 /* Inside the error capture (running with the driver in an unknown
359 * state), we want to bend the rules slightly (a lot).
360 *
361 * Work is in progress to make it safer, in the meantime this keeps
362 * the known issue from spamming the logs.
363 */
364 return rcu_dereference_protected(active->request, 1);
Chris Wilsond72d9082016-08-04 07:52:31 +0100365}
366
Chris Wilson27c01aa2016-08-04 07:52:30 +0100367/**
Chris Wilson385384a2016-08-09 08:37:01 +0100368 * i915_gem_active_raw - return the active request
369 * @active - the active tracker
370 *
371 * i915_gem_active_raw() returns the current request being tracked, or NULL.
372 * It does not obtain a reference on the request for the caller, so the caller
373 * must hold struct_mutex.
374 */
375static inline struct drm_i915_gem_request *
376i915_gem_active_raw(const struct i915_gem_active *active, struct mutex *mutex)
377{
378 return rcu_dereference_protected(active->request,
379 lockdep_is_held(mutex));
380}
381
382/**
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100383 * i915_gem_active_peek - report the active request being monitored
Chris Wilson27c01aa2016-08-04 07:52:30 +0100384 * @active - the active tracker
385 *
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100386 * i915_gem_active_peek() returns the current request being tracked if
387 * still active, or NULL. It does not obtain a reference on the request
388 * for the caller, so the caller must hold struct_mutex.
Chris Wilson27c01aa2016-08-04 07:52:30 +0100389 */
390static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100391i915_gem_active_peek(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100392{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100393 struct drm_i915_gem_request *request;
394
Chris Wilson385384a2016-08-09 08:37:01 +0100395 request = i915_gem_active_raw(active, mutex);
Chris Wilson0eafec62016-08-04 16:32:41 +0100396 if (!request || i915_gem_request_completed(request))
397 return NULL;
398
399 return request;
400}
401
402/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100403 * i915_gem_active_get - return a reference to the active request
404 * @active - the active tracker
405 *
406 * i915_gem_active_get() returns a reference to the active request, or NULL
407 * if the active tracker is idle. The caller must hold struct_mutex.
408 */
409static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100410i915_gem_active_get(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100411{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100412 return i915_gem_request_get(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100413}
414
415/**
Chris Wilson0eafec62016-08-04 16:32:41 +0100416 * __i915_gem_active_get_rcu - return a reference to the active request
417 * @active - the active tracker
418 *
419 * __i915_gem_active_get() returns a reference to the active request, or NULL
420 * if the active tracker is idle. The caller must hold the RCU read lock, but
421 * the returned pointer is safe to use outside of RCU.
422 */
423static inline struct drm_i915_gem_request *
424__i915_gem_active_get_rcu(const struct i915_gem_active *active)
425{
426 /* Performing a lockless retrieval of the active request is super
427 * tricky. SLAB_DESTROY_BY_RCU merely guarantees that the backing
428 * slab of request objects will not be freed whilst we hold the
429 * RCU read lock. It does not guarantee that the request itself
430 * will not be freed and then *reused*. Viz,
431 *
432 * Thread A Thread B
433 *
434 * req = active.request
435 * retire(req) -> free(req);
436 * (req is now first on the slab freelist)
437 * active.request = NULL
438 *
439 * req = new submission on a new object
440 * ref(req)
441 *
442 * To prevent the request from being reused whilst the caller
443 * uses it, we take a reference like normal. Whilst acquiring
444 * the reference we check that it is not in a destroyed state
445 * (refcnt == 0). That prevents the request being reallocated
446 * whilst the caller holds on to it. To check that the request
447 * was not reallocated as we acquired the reference we have to
448 * check that our request remains the active request across
449 * the lookup, in the same manner as a seqlock. The visibility
450 * of the pointer versus the reference counting is controlled
451 * by using RCU barriers (rcu_dereference and rcu_assign_pointer).
452 *
453 * In the middle of all that, we inspect whether the request is
454 * complete. Retiring is lazy so the request may be completed long
455 * before the active tracker is updated. Querying whether the
456 * request is complete is far cheaper (as it involves no locked
457 * instructions setting cachelines to exclusive) than acquiring
458 * the reference, so we do it first. The RCU read lock ensures the
459 * pointer dereference is valid, but does not ensure that the
460 * seqno nor HWS is the right one! However, if the request was
461 * reallocated, that means the active tracker's request was complete.
462 * If the new request is also complete, then both are and we can
463 * just report the active tracker is idle. If the new request is
464 * incomplete, then we acquire a reference on it and check that
465 * it remained the active request.
Chris Wilson5a198b82016-08-09 09:23:34 +0100466 *
467 * It is then imperative that we do not zero the request on
468 * reallocation, so that we can chase the dangling pointers!
469 * See i915_gem_request_alloc().
Chris Wilson0eafec62016-08-04 16:32:41 +0100470 */
471 do {
472 struct drm_i915_gem_request *request;
473
474 request = rcu_dereference(active->request);
475 if (!request || i915_gem_request_completed(request))
476 return NULL;
477
478 request = i915_gem_request_get_rcu(request);
479
480 /* What stops the following rcu_access_pointer() from occurring
481 * before the above i915_gem_request_get_rcu()? If we were
482 * to read the value before pausing to get the reference to
483 * the request, we may not notice a change in the active
484 * tracker.
485 *
486 * The rcu_access_pointer() is a mere compiler barrier, which
487 * means both the CPU and compiler are free to perform the
488 * memory read without constraint. The compiler only has to
489 * ensure that any operations after the rcu_access_pointer()
490 * occur afterwards in program order. This means the read may
491 * be performed earlier by an out-of-order CPU, or adventurous
492 * compiler.
493 *
494 * The atomic operation at the heart of
495 * i915_gem_request_get_rcu(), see fence_get_rcu(), is
496 * atomic_inc_not_zero() which is only a full memory barrier
497 * when successful. That is, if i915_gem_request_get_rcu()
498 * returns the request (and so with the reference counted
499 * incremented) then the following read for rcu_access_pointer()
500 * must occur after the atomic operation and so confirm
501 * that this request is the one currently being tracked.
Chris Wilsonedf6b762016-08-09 09:23:33 +0100502 *
503 * The corresponding write barrier is part of
504 * rcu_assign_pointer().
Chris Wilson0eafec62016-08-04 16:32:41 +0100505 */
506 if (!request || request == rcu_access_pointer(active->request))
507 return rcu_pointer_handoff(request);
508
509 i915_gem_request_put(request);
510 } while (1);
511}
512
513/**
514 * i915_gem_active_get_unlocked - return a reference to the active request
515 * @active - the active tracker
516 *
517 * i915_gem_active_get_unlocked() returns a reference to the active request,
518 * or NULL if the active tracker is idle. The reference is obtained under RCU,
519 * so no locking is required by the caller.
520 *
521 * The reference should be freed with i915_gem_request_put().
522 */
523static inline struct drm_i915_gem_request *
524i915_gem_active_get_unlocked(const struct i915_gem_active *active)
525{
526 struct drm_i915_gem_request *request;
527
528 rcu_read_lock();
529 request = __i915_gem_active_get_rcu(active);
530 rcu_read_unlock();
531
532 return request;
533}
534
535/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100536 * i915_gem_active_isset - report whether the active tracker is assigned
537 * @active - the active tracker
538 *
539 * i915_gem_active_isset() returns true if the active tracker is currently
540 * assigned to a request. Due to the lazy retiring, that request may be idle
541 * and this may report stale information.
542 */
543static inline bool
544i915_gem_active_isset(const struct i915_gem_active *active)
545{
Chris Wilson0eafec62016-08-04 16:32:41 +0100546 return rcu_access_pointer(active->request);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100547}
548
549/**
550 * i915_gem_active_is_idle - report whether the active tracker is idle
551 * @active - the active tracker
552 *
553 * i915_gem_active_is_idle() returns true if the active tracker is currently
554 * unassigned or if the request is complete (but not yet retired). Requires
555 * the caller to hold struct_mutex (but that can be relaxed if desired).
556 */
557static inline bool
Chris Wilsond72d9082016-08-04 07:52:31 +0100558i915_gem_active_is_idle(const struct i915_gem_active *active,
559 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100560{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100561 return !i915_gem_active_peek(active, mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100562}
563
564/**
565 * i915_gem_active_wait - waits until the request is completed
566 * @active - the active request on which to wait
567 *
568 * i915_gem_active_wait() waits until the request is completed before
569 * returning. Note that it does not guarantee that the request is
570 * retired first, see i915_gem_active_retire().
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100571 *
572 * i915_gem_active_wait() returns immediately if the active
573 * request is already complete.
Chris Wilson27c01aa2016-08-04 07:52:30 +0100574 */
575static inline int __must_check
Chris Wilsond72d9082016-08-04 07:52:31 +0100576i915_gem_active_wait(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100577{
578 struct drm_i915_gem_request *request;
579
Chris Wilsond72d9082016-08-04 07:52:31 +0100580 request = i915_gem_active_peek(active, mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100581 if (!request)
582 return 0;
583
Chris Wilson776f3232016-08-04 07:52:40 +0100584 return i915_wait_request(request, true, NULL, NULL);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100585}
586
587/**
Chris Wilson24676582016-08-05 10:14:06 +0100588 * i915_gem_active_wait_unlocked - waits until the request is completed
589 * @active - the active request on which to wait
590 * @interruptible - whether the wait can be woken by a userspace signal
591 * @timeout - how long to wait at most
592 * @rps - userspace client to charge for a waitboost
593 *
594 * i915_gem_active_wait_unlocked() waits until the request is completed before
595 * returning, without requiring any locks to be held. Note that it does not
596 * retire any requests before returning.
597 *
598 * This function relies on RCU in order to acquire the reference to the active
599 * request without holding any locks. See __i915_gem_active_get_rcu() for the
600 * glory details on how that is managed. Once the reference is acquired, we
601 * can then wait upon the request, and afterwards release our reference,
602 * free of any locking.
603 *
604 * This function wraps i915_wait_request(), see it for the full details on
605 * the arguments.
606 *
607 * Returns 0 if successful, or a negative error code.
608 */
609static inline int
610i915_gem_active_wait_unlocked(const struct i915_gem_active *active,
611 bool interruptible,
612 s64 *timeout,
613 struct intel_rps_client *rps)
614{
615 struct drm_i915_gem_request *request;
616 int ret = 0;
617
618 request = i915_gem_active_get_unlocked(active);
619 if (request) {
620 ret = i915_wait_request(request, interruptible, timeout, rps);
621 i915_gem_request_put(request);
622 }
623
624 return ret;
625}
626
627/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100628 * i915_gem_active_retire - waits until the request is retired
629 * @active - the active request on which to wait
630 *
631 * i915_gem_active_retire() waits until the request is completed,
632 * and then ensures that at least the retirement handler for this
633 * @active tracker is called before returning. If the @active
634 * tracker is idle, the function returns immediately.
635 */
636static inline int __must_check
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100637i915_gem_active_retire(struct i915_gem_active *active,
Chris Wilsond72d9082016-08-04 07:52:31 +0100638 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100639{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100640 struct drm_i915_gem_request *request;
641 int ret;
642
Chris Wilson385384a2016-08-09 08:37:01 +0100643 request = i915_gem_active_raw(active, mutex);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100644 if (!request)
645 return 0;
646
Chris Wilson776f3232016-08-04 07:52:40 +0100647 ret = i915_wait_request(request, true, 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 */