blob: 6c72bd8d9423c140cdabbf977eb3f2111bc9dc48 [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 */
Chris Wilson058d88c2016-08-15 10:49:06 +0100121 struct i915_vma *batch;
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
Chris Wilson05235c52016-07-20 09:21:08 +0100137 /**
138 * The ELSP only accepts two elements at a time, so we queue
139 * context/tail pairs on a given queue (ring->execlist_queue) until the
140 * hardware is available. The queue serves a double purpose: we also use
141 * it to keep track of the up to 2 contexts currently in the hardware
142 * (usually one in execution and the other queued up by the GPU): We
143 * only remove elements from the head of the queue when the hardware
144 * informs us that an element has been completed.
145 *
146 * All accesses to the queue are mediated by a spinlock
147 * (ring->execlist_lock).
148 */
149
150 /** Execlist link in the submission queue.*/
151 struct list_head execlist_link;
152
153 /** Execlists no. of times this request has been sent to the ELSP */
154 int elsp_submitted;
155
156 /** Execlists context hardware id. */
157 unsigned int ctx_hw_id;
158};
159
Chris Wilson04769652016-07-20 09:21:11 +0100160extern const struct fence_ops i915_fence_ops;
161
162static inline bool fence_is_i915(struct fence *fence)
163{
164 return fence->ops == &i915_fence_ops;
165}
166
Chris Wilson05235c52016-07-20 09:21:08 +0100167struct drm_i915_gem_request * __must_check
168i915_gem_request_alloc(struct intel_engine_cs *engine,
169 struct i915_gem_context *ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100170int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
171 struct drm_file *file);
172void i915_gem_request_retire_upto(struct drm_i915_gem_request *req);
173
174static inline u32
175i915_gem_request_get_seqno(struct drm_i915_gem_request *req)
176{
Chris Wilson04769652016-07-20 09:21:11 +0100177 return req ? req->fence.seqno : 0;
Chris Wilson05235c52016-07-20 09:21:08 +0100178}
179
180static inline struct intel_engine_cs *
181i915_gem_request_get_engine(struct drm_i915_gem_request *req)
182{
183 return req ? req->engine : NULL;
184}
185
186static inline struct drm_i915_gem_request *
Chris Wilson04769652016-07-20 09:21:11 +0100187to_request(struct fence *fence)
188{
189 /* We assume that NULL fence/request are interoperable */
190 BUILD_BUG_ON(offsetof(struct drm_i915_gem_request, fence) != 0);
191 GEM_BUG_ON(fence && !fence_is_i915(fence));
192 return container_of(fence, struct drm_i915_gem_request, fence);
193}
194
195static inline struct drm_i915_gem_request *
Chris Wilsone8a261e2016-07-20 13:31:49 +0100196i915_gem_request_get(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100197{
Chris Wilson04769652016-07-20 09:21:11 +0100198 return to_request(fence_get(&req->fence));
Chris Wilson05235c52016-07-20 09:21:08 +0100199}
200
Chris Wilson0eafec62016-08-04 16:32:41 +0100201static inline struct drm_i915_gem_request *
202i915_gem_request_get_rcu(struct drm_i915_gem_request *req)
203{
204 return to_request(fence_get_rcu(&req->fence));
205}
206
Chris Wilson05235c52016-07-20 09:21:08 +0100207static inline void
Chris Wilsone8a261e2016-07-20 13:31:49 +0100208i915_gem_request_put(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100209{
Chris Wilson04769652016-07-20 09:21:11 +0100210 fence_put(&req->fence);
Chris Wilson05235c52016-07-20 09:21:08 +0100211}
212
213static inline void i915_gem_request_assign(struct drm_i915_gem_request **pdst,
214 struct drm_i915_gem_request *src)
215{
216 if (src)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100217 i915_gem_request_get(src);
Chris Wilson05235c52016-07-20 09:21:08 +0100218
219 if (*pdst)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100220 i915_gem_request_put(*pdst);
Chris Wilson05235c52016-07-20 09:21:08 +0100221
222 *pdst = src;
223}
224
Chris Wilson17f298cf2016-08-10 13:41:46 +0100225void __i915_add_request(struct drm_i915_gem_request *req, bool flush_caches);
Chris Wilson05235c52016-07-20 09:21:08 +0100226#define i915_add_request(req) \
Chris Wilson17f298cf2016-08-10 13:41:46 +0100227 __i915_add_request(req, true)
Chris Wilson05235c52016-07-20 09:21:08 +0100228#define i915_add_request_no_flush(req) \
Chris Wilson17f298cf2016-08-10 13:41:46 +0100229 __i915_add_request(req, false)
Chris Wilson05235c52016-07-20 09:21:08 +0100230
231struct intel_rps_client;
Chris Wilson42df2712016-07-20 09:21:12 +0100232#define NO_WAITBOOST ERR_PTR(-1)
233#define IS_RPS_CLIENT(p) (!IS_ERR(p))
234#define IS_RPS_USER(p) (!IS_ERR_OR_NULL(p))
Chris Wilson05235c52016-07-20 09:21:08 +0100235
Chris Wilson776f3232016-08-04 07:52:40 +0100236int i915_wait_request(struct drm_i915_gem_request *req,
237 bool interruptible,
238 s64 *timeout,
239 struct intel_rps_client *rps)
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100240 __attribute__((nonnull(1)));
241
Chris Wilson05235c52016-07-20 09:21:08 +0100242static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine);
243
244/**
245 * Returns true if seq1 is later than seq2.
246 */
247static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
248{
249 return (s32)(seq1 - seq2) >= 0;
250}
251
252static inline bool
253i915_gem_request_started(const struct drm_i915_gem_request *req)
254{
255 return i915_seqno_passed(intel_engine_get_seqno(req->engine),
256 req->previous_seqno);
257}
258
259static inline bool
260i915_gem_request_completed(const struct drm_i915_gem_request *req)
261{
262 return i915_seqno_passed(intel_engine_get_seqno(req->engine),
Chris Wilson04769652016-07-20 09:21:11 +0100263 req->fence.seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100264}
265
266bool __i915_spin_request(const struct drm_i915_gem_request *request,
267 int state, unsigned long timeout_us);
268static inline bool i915_spin_request(const struct drm_i915_gem_request *request,
269 int state, unsigned long timeout_us)
270{
271 return (i915_gem_request_started(request) &&
272 __i915_spin_request(request, state, timeout_us));
273}
274
Chris Wilson381f3712016-08-04 07:52:29 +0100275/* We treat requests as fences. This is not be to confused with our
276 * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
277 * We use the fences to synchronize access from the CPU with activity on the
278 * GPU, for example, we should not rewrite an object's PTE whilst the GPU
279 * is reading them. We also track fences at a higher level to provide
280 * implicit synchronisation around GEM objects, e.g. set-domain will wait
281 * for outstanding GPU rendering before marking the object ready for CPU
282 * access, or a pageflip will wait until the GPU is complete before showing
283 * the frame on the scanout.
284 *
285 * In order to use a fence, the object must track the fence it needs to
286 * serialise with. For example, GEM objects want to track both read and
287 * write access so that we can perform concurrent read operations between
288 * the CPU and GPU engines, as well as waiting for all rendering to
289 * complete, or waiting for the last GPU user of a "fence register". The
290 * object then embeds a #i915_gem_active to track the most recent (in
291 * retirement order) request relevant for the desired mode of access.
292 * The #i915_gem_active is updated with i915_gem_active_set() to track the
293 * most recent fence request, typically this is done as part of
294 * i915_vma_move_to_active().
295 *
296 * When the #i915_gem_active completes (is retired), it will
297 * signal its completion to the owner through a callback as well as mark
298 * itself as idle (i915_gem_active.request == NULL). The owner
299 * can then perform any action, such as delayed freeing of an active
300 * resource including itself.
301 */
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100302struct i915_gem_active;
303
304typedef void (*i915_gem_retire_fn)(struct i915_gem_active *,
305 struct drm_i915_gem_request *);
306
Chris Wilson381f3712016-08-04 07:52:29 +0100307struct i915_gem_active {
Chris Wilson0eafec62016-08-04 16:32:41 +0100308 struct drm_i915_gem_request __rcu *request;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100309 struct list_head link;
310 i915_gem_retire_fn retire;
Chris Wilson381f3712016-08-04 07:52:29 +0100311};
312
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100313void i915_gem_retire_noop(struct i915_gem_active *,
314 struct drm_i915_gem_request *request);
315
316/**
317 * init_request_active - prepares the activity tracker for use
318 * @active - the active tracker
319 * @func - a callback when then the tracker is retired (becomes idle),
320 * can be NULL
321 *
322 * init_request_active() prepares the embedded @active struct for use as
323 * an activity tracker, that is for tracking the last known active request
324 * associated with it. When the last request becomes idle, when it is retired
325 * after completion, the optional callback @func is invoked.
326 */
327static inline void
328init_request_active(struct i915_gem_active *active,
329 i915_gem_retire_fn retire)
330{
331 INIT_LIST_HEAD(&active->link);
332 active->retire = retire ?: i915_gem_retire_noop;
333}
334
Chris Wilson27c01aa2016-08-04 07:52:30 +0100335/**
336 * i915_gem_active_set - updates the tracker to watch the current request
337 * @active - the active tracker
338 * @request - the request to watch
339 *
340 * i915_gem_active_set() watches the given @request for completion. Whilst
341 * that @request is busy, the @active reports busy. When that @request is
342 * retired, the @active tracker is updated to report idle.
343 */
Chris Wilson381f3712016-08-04 07:52:29 +0100344static inline void
345i915_gem_active_set(struct i915_gem_active *active,
346 struct drm_i915_gem_request *request)
347{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100348 list_move(&active->link, &request->active_list);
Chris Wilson0eafec62016-08-04 16:32:41 +0100349 rcu_assign_pointer(active->request, request);
Chris Wilson381f3712016-08-04 07:52:29 +0100350}
351
Chris Wilsond72d9082016-08-04 07:52:31 +0100352static inline struct drm_i915_gem_request *
353__i915_gem_active_peek(const struct i915_gem_active *active)
354{
Chris Wilson0eafec62016-08-04 16:32:41 +0100355 /* Inside the error capture (running with the driver in an unknown
356 * state), we want to bend the rules slightly (a lot).
357 *
358 * Work is in progress to make it safer, in the meantime this keeps
359 * the known issue from spamming the logs.
360 */
361 return rcu_dereference_protected(active->request, 1);
Chris Wilsond72d9082016-08-04 07:52:31 +0100362}
363
Chris Wilson27c01aa2016-08-04 07:52:30 +0100364/**
Chris Wilson385384a2016-08-09 08:37:01 +0100365 * i915_gem_active_raw - return the active request
366 * @active - the active tracker
367 *
368 * i915_gem_active_raw() returns the current request being tracked, or NULL.
369 * It does not obtain a reference on the request for the caller, so the caller
370 * must hold struct_mutex.
371 */
372static inline struct drm_i915_gem_request *
373i915_gem_active_raw(const struct i915_gem_active *active, struct mutex *mutex)
374{
375 return rcu_dereference_protected(active->request,
376 lockdep_is_held(mutex));
377}
378
379/**
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100380 * i915_gem_active_peek - report the active request being monitored
Chris Wilson27c01aa2016-08-04 07:52:30 +0100381 * @active - the active tracker
382 *
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100383 * i915_gem_active_peek() returns the current request being tracked if
384 * still active, or NULL. It does not obtain a reference on the request
385 * for the caller, so the caller must hold struct_mutex.
Chris Wilson27c01aa2016-08-04 07:52:30 +0100386 */
387static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100388i915_gem_active_peek(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100389{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100390 struct drm_i915_gem_request *request;
391
Chris Wilson385384a2016-08-09 08:37:01 +0100392 request = i915_gem_active_raw(active, mutex);
Chris Wilson0eafec62016-08-04 16:32:41 +0100393 if (!request || i915_gem_request_completed(request))
394 return NULL;
395
396 return request;
397}
398
399/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100400 * i915_gem_active_get - return a reference to the active request
401 * @active - the active tracker
402 *
403 * i915_gem_active_get() returns a reference to the active request, or NULL
404 * if the active tracker is idle. The caller must hold struct_mutex.
405 */
406static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100407i915_gem_active_get(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100408{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100409 return i915_gem_request_get(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100410}
411
412/**
Chris Wilson0eafec62016-08-04 16:32:41 +0100413 * __i915_gem_active_get_rcu - return a reference to the active request
414 * @active - the active tracker
415 *
416 * __i915_gem_active_get() returns a reference to the active request, or NULL
417 * if the active tracker is idle. The caller must hold the RCU read lock, but
418 * the returned pointer is safe to use outside of RCU.
419 */
420static inline struct drm_i915_gem_request *
421__i915_gem_active_get_rcu(const struct i915_gem_active *active)
422{
423 /* Performing a lockless retrieval of the active request is super
424 * tricky. SLAB_DESTROY_BY_RCU merely guarantees that the backing
425 * slab of request objects will not be freed whilst we hold the
426 * RCU read lock. It does not guarantee that the request itself
427 * will not be freed and then *reused*. Viz,
428 *
429 * Thread A Thread B
430 *
431 * req = active.request
432 * retire(req) -> free(req);
433 * (req is now first on the slab freelist)
434 * active.request = NULL
435 *
436 * req = new submission on a new object
437 * ref(req)
438 *
439 * To prevent the request from being reused whilst the caller
440 * uses it, we take a reference like normal. Whilst acquiring
441 * the reference we check that it is not in a destroyed state
442 * (refcnt == 0). That prevents the request being reallocated
443 * whilst the caller holds on to it. To check that the request
444 * was not reallocated as we acquired the reference we have to
445 * check that our request remains the active request across
446 * the lookup, in the same manner as a seqlock. The visibility
447 * of the pointer versus the reference counting is controlled
448 * by using RCU barriers (rcu_dereference and rcu_assign_pointer).
449 *
450 * In the middle of all that, we inspect whether the request is
451 * complete. Retiring is lazy so the request may be completed long
452 * before the active tracker is updated. Querying whether the
453 * request is complete is far cheaper (as it involves no locked
454 * instructions setting cachelines to exclusive) than acquiring
455 * the reference, so we do it first. The RCU read lock ensures the
456 * pointer dereference is valid, but does not ensure that the
457 * seqno nor HWS is the right one! However, if the request was
458 * reallocated, that means the active tracker's request was complete.
459 * If the new request is also complete, then both are and we can
460 * just report the active tracker is idle. If the new request is
461 * incomplete, then we acquire a reference on it and check that
462 * it remained the active request.
Chris Wilson5a198b82016-08-09 09:23:34 +0100463 *
464 * It is then imperative that we do not zero the request on
465 * reallocation, so that we can chase the dangling pointers!
466 * See i915_gem_request_alloc().
Chris Wilson0eafec62016-08-04 16:32:41 +0100467 */
468 do {
469 struct drm_i915_gem_request *request;
470
471 request = rcu_dereference(active->request);
472 if (!request || i915_gem_request_completed(request))
473 return NULL;
474
475 request = i915_gem_request_get_rcu(request);
476
477 /* What stops the following rcu_access_pointer() from occurring
478 * before the above i915_gem_request_get_rcu()? If we were
479 * to read the value before pausing to get the reference to
480 * the request, we may not notice a change in the active
481 * tracker.
482 *
483 * The rcu_access_pointer() is a mere compiler barrier, which
484 * means both the CPU and compiler are free to perform the
485 * memory read without constraint. The compiler only has to
486 * ensure that any operations after the rcu_access_pointer()
487 * occur afterwards in program order. This means the read may
488 * be performed earlier by an out-of-order CPU, or adventurous
489 * compiler.
490 *
491 * The atomic operation at the heart of
492 * i915_gem_request_get_rcu(), see fence_get_rcu(), is
493 * atomic_inc_not_zero() which is only a full memory barrier
494 * when successful. That is, if i915_gem_request_get_rcu()
495 * returns the request (and so with the reference counted
496 * incremented) then the following read for rcu_access_pointer()
497 * must occur after the atomic operation and so confirm
498 * that this request is the one currently being tracked.
Chris Wilsonedf6b762016-08-09 09:23:33 +0100499 *
500 * The corresponding write barrier is part of
501 * rcu_assign_pointer().
Chris Wilson0eafec62016-08-04 16:32:41 +0100502 */
503 if (!request || request == rcu_access_pointer(active->request))
504 return rcu_pointer_handoff(request);
505
506 i915_gem_request_put(request);
507 } while (1);
508}
509
510/**
511 * i915_gem_active_get_unlocked - return a reference to the active request
512 * @active - the active tracker
513 *
514 * i915_gem_active_get_unlocked() returns a reference to the active request,
515 * or NULL if the active tracker is idle. The reference is obtained under RCU,
516 * so no locking is required by the caller.
517 *
518 * The reference should be freed with i915_gem_request_put().
519 */
520static inline struct drm_i915_gem_request *
521i915_gem_active_get_unlocked(const struct i915_gem_active *active)
522{
523 struct drm_i915_gem_request *request;
524
525 rcu_read_lock();
526 request = __i915_gem_active_get_rcu(active);
527 rcu_read_unlock();
528
529 return request;
530}
531
532/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100533 * i915_gem_active_isset - report whether the active tracker is assigned
534 * @active - the active tracker
535 *
536 * i915_gem_active_isset() returns true if the active tracker is currently
537 * assigned to a request. Due to the lazy retiring, that request may be idle
538 * and this may report stale information.
539 */
540static inline bool
541i915_gem_active_isset(const struct i915_gem_active *active)
542{
Chris Wilson0eafec62016-08-04 16:32:41 +0100543 return rcu_access_pointer(active->request);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100544}
545
546/**
547 * i915_gem_active_is_idle - report whether the active tracker is idle
548 * @active - the active tracker
549 *
550 * i915_gem_active_is_idle() returns true if the active tracker is currently
551 * unassigned or if the request is complete (but not yet retired). Requires
552 * the caller to hold struct_mutex (but that can be relaxed if desired).
553 */
554static inline bool
Chris Wilsond72d9082016-08-04 07:52:31 +0100555i915_gem_active_is_idle(const struct i915_gem_active *active,
556 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100557{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100558 return !i915_gem_active_peek(active, mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100559}
560
561/**
562 * i915_gem_active_wait - waits until the request is completed
563 * @active - the active request on which to wait
564 *
565 * i915_gem_active_wait() waits until the request is completed before
566 * returning. Note that it does not guarantee that the request is
567 * retired first, see i915_gem_active_retire().
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100568 *
569 * i915_gem_active_wait() returns immediately if the active
570 * request is already complete.
Chris Wilson27c01aa2016-08-04 07:52:30 +0100571 */
572static inline int __must_check
Chris Wilsond72d9082016-08-04 07:52:31 +0100573i915_gem_active_wait(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100574{
575 struct drm_i915_gem_request *request;
576
Chris Wilsond72d9082016-08-04 07:52:31 +0100577 request = i915_gem_active_peek(active, mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100578 if (!request)
579 return 0;
580
Chris Wilson776f3232016-08-04 07:52:40 +0100581 return i915_wait_request(request, true, NULL, NULL);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100582}
583
584/**
Chris Wilson24676582016-08-05 10:14:06 +0100585 * i915_gem_active_wait_unlocked - waits until the request is completed
586 * @active - the active request on which to wait
587 * @interruptible - whether the wait can be woken by a userspace signal
588 * @timeout - how long to wait at most
589 * @rps - userspace client to charge for a waitboost
590 *
591 * i915_gem_active_wait_unlocked() waits until the request is completed before
592 * returning, without requiring any locks to be held. Note that it does not
593 * retire any requests before returning.
594 *
595 * This function relies on RCU in order to acquire the reference to the active
596 * request without holding any locks. See __i915_gem_active_get_rcu() for the
597 * glory details on how that is managed. Once the reference is acquired, we
598 * can then wait upon the request, and afterwards release our reference,
599 * free of any locking.
600 *
601 * This function wraps i915_wait_request(), see it for the full details on
602 * the arguments.
603 *
604 * Returns 0 if successful, or a negative error code.
605 */
606static inline int
607i915_gem_active_wait_unlocked(const struct i915_gem_active *active,
608 bool interruptible,
609 s64 *timeout,
610 struct intel_rps_client *rps)
611{
612 struct drm_i915_gem_request *request;
613 int ret = 0;
614
615 request = i915_gem_active_get_unlocked(active);
616 if (request) {
617 ret = i915_wait_request(request, interruptible, timeout, rps);
618 i915_gem_request_put(request);
619 }
620
621 return ret;
622}
623
624/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100625 * i915_gem_active_retire - waits until the request is retired
626 * @active - the active request on which to wait
627 *
628 * i915_gem_active_retire() waits until the request is completed,
629 * and then ensures that at least the retirement handler for this
630 * @active tracker is called before returning. If the @active
631 * tracker is idle, the function returns immediately.
632 */
633static inline int __must_check
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100634i915_gem_active_retire(struct i915_gem_active *active,
Chris Wilsond72d9082016-08-04 07:52:31 +0100635 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100636{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100637 struct drm_i915_gem_request *request;
638 int ret;
639
Chris Wilson385384a2016-08-09 08:37:01 +0100640 request = i915_gem_active_raw(active, mutex);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100641 if (!request)
642 return 0;
643
Chris Wilson776f3232016-08-04 07:52:40 +0100644 ret = i915_wait_request(request, true, NULL, NULL);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100645 if (ret)
646 return ret;
647
648 list_del_init(&active->link);
Chris Wilson0eafec62016-08-04 16:32:41 +0100649 RCU_INIT_POINTER(active->request, NULL);
650
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100651 active->retire(active, request);
652
653 return 0;
Chris Wilson27c01aa2016-08-04 07:52:30 +0100654}
655
656/* Convenience functions for peeking at state inside active's request whilst
657 * guarded by the struct_mutex.
658 */
659
660static inline uint32_t
Chris Wilsond72d9082016-08-04 07:52:31 +0100661i915_gem_active_get_seqno(const struct i915_gem_active *active,
662 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100663{
Chris Wilsond72d9082016-08-04 07:52:31 +0100664 return i915_gem_request_get_seqno(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100665}
666
667static inline struct intel_engine_cs *
Chris Wilsond72d9082016-08-04 07:52:31 +0100668i915_gem_active_get_engine(const struct i915_gem_active *active,
669 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100670{
Chris Wilsond72d9082016-08-04 07:52:31 +0100671 return i915_gem_request_get_engine(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100672}
673
Chris Wilson381f3712016-08-04 07:52:29 +0100674#define for_each_active(mask, idx) \
675 for (; mask ? idx = ffs(mask) - 1, 1 : 0; mask &= ~BIT(idx))
676
Chris Wilson05235c52016-07-20 09:21:08 +0100677#endif /* I915_GEM_REQUEST_H */