blob: 2faa3bb4c39bfc1ae15b383b5dec4e978632a29a [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 Wilson05235c52016-07-20 09:21:08 +0100140 /**
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
Daniel Vetterc75870d2016-08-22 10:55:22 +0200478 /* An especially silly compiler could decide to recompute the
479 * result of i915_gem_request_completed, more specifically
480 * re-emit the load for request->fence.seqno. A race would catch
481 * a later seqno value, which could flip the result from true to
482 * false. Which means part of the instructions below might not
483 * be executed, while later on instructions are executed. Due to
484 * barriers within the refcounting the inconsistency can't reach
485 * past the call to i915_gem_request_get_rcu, but not executing
486 * that while still executing i915_gem_request_put() creates
487 * havoc enough. Prevent this with a compiler barrier.
488 */
489 barrier();
490
Chris Wilson0eafec62016-08-04 16:32:41 +0100491 request = i915_gem_request_get_rcu(request);
492
493 /* What stops the following rcu_access_pointer() from occurring
494 * before the above i915_gem_request_get_rcu()? If we were
495 * to read the value before pausing to get the reference to
496 * the request, we may not notice a change in the active
497 * tracker.
498 *
499 * The rcu_access_pointer() is a mere compiler barrier, which
500 * means both the CPU and compiler are free to perform the
501 * memory read without constraint. The compiler only has to
502 * ensure that any operations after the rcu_access_pointer()
503 * occur afterwards in program order. This means the read may
504 * be performed earlier by an out-of-order CPU, or adventurous
505 * compiler.
506 *
507 * The atomic operation at the heart of
508 * i915_gem_request_get_rcu(), see fence_get_rcu(), is
509 * atomic_inc_not_zero() which is only a full memory barrier
510 * when successful. That is, if i915_gem_request_get_rcu()
511 * returns the request (and so with the reference counted
512 * incremented) then the following read for rcu_access_pointer()
513 * must occur after the atomic operation and so confirm
514 * that this request is the one currently being tracked.
Chris Wilsonedf6b762016-08-09 09:23:33 +0100515 *
516 * The corresponding write barrier is part of
517 * rcu_assign_pointer().
Chris Wilson0eafec62016-08-04 16:32:41 +0100518 */
519 if (!request || request == rcu_access_pointer(active->request))
520 return rcu_pointer_handoff(request);
521
522 i915_gem_request_put(request);
523 } while (1);
524}
525
526/**
527 * i915_gem_active_get_unlocked - return a reference to the active request
528 * @active - the active tracker
529 *
530 * i915_gem_active_get_unlocked() returns a reference to the active request,
531 * or NULL if the active tracker is idle. The reference is obtained under RCU,
532 * so no locking is required by the caller.
533 *
534 * The reference should be freed with i915_gem_request_put().
535 */
536static inline struct drm_i915_gem_request *
537i915_gem_active_get_unlocked(const struct i915_gem_active *active)
538{
539 struct drm_i915_gem_request *request;
540
541 rcu_read_lock();
542 request = __i915_gem_active_get_rcu(active);
543 rcu_read_unlock();
544
545 return request;
546}
547
548/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100549 * i915_gem_active_isset - report whether the active tracker is assigned
550 * @active - the active tracker
551 *
552 * i915_gem_active_isset() returns true if the active tracker is currently
553 * assigned to a request. Due to the lazy retiring, that request may be idle
554 * and this may report stale information.
555 */
556static inline bool
557i915_gem_active_isset(const struct i915_gem_active *active)
558{
Chris Wilson0eafec62016-08-04 16:32:41 +0100559 return rcu_access_pointer(active->request);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100560}
561
562/**
563 * i915_gem_active_is_idle - report whether the active tracker is idle
564 * @active - the active tracker
565 *
566 * i915_gem_active_is_idle() returns true if the active tracker is currently
567 * unassigned or if the request is complete (but not yet retired). Requires
568 * the caller to hold struct_mutex (but that can be relaxed if desired).
569 */
570static inline bool
Chris Wilsond72d9082016-08-04 07:52:31 +0100571i915_gem_active_is_idle(const struct i915_gem_active *active,
572 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100573{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100574 return !i915_gem_active_peek(active, mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100575}
576
577/**
578 * i915_gem_active_wait - waits until the request is completed
579 * @active - the active request on which to wait
580 *
581 * i915_gem_active_wait() waits until the request is completed before
582 * returning. Note that it does not guarantee that the request is
583 * retired first, see i915_gem_active_retire().
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100584 *
585 * i915_gem_active_wait() returns immediately if the active
586 * request is already complete.
Chris Wilson27c01aa2016-08-04 07:52:30 +0100587 */
588static inline int __must_check
Chris Wilsond72d9082016-08-04 07:52:31 +0100589i915_gem_active_wait(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100590{
591 struct drm_i915_gem_request *request;
592
Chris Wilsond72d9082016-08-04 07:52:31 +0100593 request = i915_gem_active_peek(active, mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100594 if (!request)
595 return 0;
596
Chris Wilson776f3232016-08-04 07:52:40 +0100597 return i915_wait_request(request, true, NULL, NULL);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100598}
599
600/**
Chris Wilson24676582016-08-05 10:14:06 +0100601 * i915_gem_active_wait_unlocked - waits until the request is completed
602 * @active - the active request on which to wait
603 * @interruptible - whether the wait can be woken by a userspace signal
604 * @timeout - how long to wait at most
605 * @rps - userspace client to charge for a waitboost
606 *
607 * i915_gem_active_wait_unlocked() waits until the request is completed before
608 * returning, without requiring any locks to be held. Note that it does not
609 * retire any requests before returning.
610 *
611 * This function relies on RCU in order to acquire the reference to the active
612 * request without holding any locks. See __i915_gem_active_get_rcu() for the
613 * glory details on how that is managed. Once the reference is acquired, we
614 * can then wait upon the request, and afterwards release our reference,
615 * free of any locking.
616 *
617 * This function wraps i915_wait_request(), see it for the full details on
618 * the arguments.
619 *
620 * Returns 0 if successful, or a negative error code.
621 */
622static inline int
623i915_gem_active_wait_unlocked(const struct i915_gem_active *active,
624 bool interruptible,
625 s64 *timeout,
626 struct intel_rps_client *rps)
627{
628 struct drm_i915_gem_request *request;
629 int ret = 0;
630
631 request = i915_gem_active_get_unlocked(active);
632 if (request) {
633 ret = i915_wait_request(request, interruptible, timeout, rps);
634 i915_gem_request_put(request);
635 }
636
637 return ret;
638}
639
640/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100641 * i915_gem_active_retire - waits until the request is retired
642 * @active - the active request on which to wait
643 *
644 * i915_gem_active_retire() waits until the request is completed,
645 * and then ensures that at least the retirement handler for this
646 * @active tracker is called before returning. If the @active
647 * tracker is idle, the function returns immediately.
648 */
649static inline int __must_check
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100650i915_gem_active_retire(struct i915_gem_active *active,
Chris Wilsond72d9082016-08-04 07:52:31 +0100651 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100652{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100653 struct drm_i915_gem_request *request;
654 int ret;
655
Chris Wilson385384a2016-08-09 08:37:01 +0100656 request = i915_gem_active_raw(active, mutex);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100657 if (!request)
658 return 0;
659
Chris Wilson776f3232016-08-04 07:52:40 +0100660 ret = i915_wait_request(request, true, NULL, NULL);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100661 if (ret)
662 return ret;
663
664 list_del_init(&active->link);
Chris Wilson0eafec62016-08-04 16:32:41 +0100665 RCU_INIT_POINTER(active->request, NULL);
666
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100667 active->retire(active, request);
668
669 return 0;
Chris Wilson27c01aa2016-08-04 07:52:30 +0100670}
671
672/* Convenience functions for peeking at state inside active's request whilst
673 * guarded by the struct_mutex.
674 */
675
676static inline uint32_t
Chris Wilsond72d9082016-08-04 07:52:31 +0100677i915_gem_active_get_seqno(const struct i915_gem_active *active,
678 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100679{
Chris Wilsond72d9082016-08-04 07:52:31 +0100680 return i915_gem_request_get_seqno(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100681}
682
683static inline struct intel_engine_cs *
Chris Wilsond72d9082016-08-04 07:52:31 +0100684i915_gem_active_get_engine(const struct i915_gem_active *active,
685 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100686{
Chris Wilsond72d9082016-08-04 07:52:31 +0100687 return i915_gem_request_get_engine(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100688}
689
Chris Wilson381f3712016-08-04 07:52:29 +0100690#define for_each_active(mask, idx) \
691 for (; mask ? idx = ffs(mask) - 1, 1 : 0; mask &= ~BIT(idx))
692
Chris Wilson05235c52016-07-20 09:21:08 +0100693#endif /* I915_GEM_REQUEST_H */