blob: 883df3bdb3819301546850badc298fa9221ce6cd [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"
Chris Wilson5590af32016-09-09 14:11:54 +010031#include "i915_sw_fence.h"
Chris Wilson04769652016-07-20 09:21:11 +010032
Chris Wilsondcff85c2016-08-05 10:14:11 +010033struct intel_wait {
34 struct rb_node node;
35 struct task_struct *tsk;
36 u32 seqno;
37};
38
39struct intel_signal_node {
40 struct rb_node node;
41 struct intel_wait wait;
42};
43
Chris Wilson05235c52016-07-20 09:21:08 +010044/**
45 * Request queue structure.
46 *
47 * The request queue allows us to note sequence numbers that have been emitted
48 * and may be associated with active buffers to be retired.
49 *
50 * By keeping this list, we can avoid having to do questionable sequence
51 * number comparisons on buffer last_read|write_seqno. It also allows an
52 * emission time to be associated with the request for tracking how far ahead
53 * of the GPU the submission is.
54 *
Chris Wilson5a198b82016-08-09 09:23:34 +010055 * When modifying this structure be very aware that we perform a lockless
56 * RCU lookup of it that may race against reallocation of the struct
57 * from the slab freelist. We intentionally do not zero the structure on
58 * allocation so that the lookup can use the dangling pointers (and is
59 * cogniscent that those pointers may be wrong). Instead, everything that
60 * needs to be initialised must be done so explicitly.
61 *
Chris Wilson04769652016-07-20 09:21:11 +010062 * The requests are reference counted.
Chris Wilson05235c52016-07-20 09:21:08 +010063 */
64struct drm_i915_gem_request {
Chris Wilson04769652016-07-20 09:21:11 +010065 struct fence fence;
66 spinlock_t lock;
Chris Wilson05235c52016-07-20 09:21:08 +010067
68 /** On Which ring this request was generated */
69 struct drm_i915_private *i915;
70
71 /**
72 * Context and ring buffer related to this request
73 * Contexts are refcounted, so when this request is associated with a
74 * context, we must increment the context's refcount, to guarantee that
75 * it persists while any request is linked to it. Requests themselves
76 * are also refcounted, so the request will only be freed when the last
77 * reference to it is dismissed, and the code in
78 * i915_gem_request_free() will then decrement the refcount on the
79 * context.
80 */
81 struct i915_gem_context *ctx;
82 struct intel_engine_cs *engine;
Chris Wilson7e37f882016-08-02 22:50:21 +010083 struct intel_ring *ring;
Chris Wilson05235c52016-07-20 09:21:08 +010084 struct intel_signal_node signaling;
85
Chris Wilson5590af32016-09-09 14:11:54 +010086 struct i915_sw_fence submit;
87
Chris Wilson05235c52016-07-20 09:21:08 +010088 /** GEM sequence number associated with the previous request,
89 * when the HWS breadcrumb is equal to this the GPU is processing
90 * this request.
91 */
92 u32 previous_seqno;
93
Chris Wilsona52abd22016-09-09 14:11:43 +010094 /** Position in the ring of the start of the request */
Chris Wilson05235c52016-07-20 09:21:08 +010095 u32 head;
96
97 /**
Chris Wilsona52abd22016-09-09 14:11:43 +010098 * Position in the ring of the start of the postfix.
99 * This is required to calculate the maximum available ring space
100 * without overwriting the postfix.
Chris Wilson05235c52016-07-20 09:21:08 +0100101 */
102 u32 postfix;
103
Chris Wilsona52abd22016-09-09 14:11:43 +0100104 /** Position in the ring of the end of the whole request */
Chris Wilson05235c52016-07-20 09:21:08 +0100105 u32 tail;
106
Chris Wilsona52abd22016-09-09 14:11:43 +0100107 /** Position in the ring of the end of any workarounds after the tail */
108 u32 wa_tail;
109
110 /** Preallocate space in the ring for the emitting the request */
Chris Wilson05235c52016-07-20 09:21:08 +0100111 u32 reserved_space;
112
113 /**
114 * Context related to the previous request.
115 * As the contexts are accessed by the hardware until the switch is
116 * completed to a new context, the hardware may still be writing
117 * to the context object after the breadcrumb is visible. We must
118 * not unpin/unbind/prune that object whilst still active and so
119 * we keep the previous context pinned until the following (this)
120 * request is retired.
121 */
122 struct i915_gem_context *previous_context;
123
124 /** Batch buffer related to this request if any (used for
125 * error state dump only).
126 */
Chris Wilson058d88c2016-08-15 10:49:06 +0100127 struct i915_vma *batch;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100128 struct list_head active_list;
Chris Wilson05235c52016-07-20 09:21:08 +0100129
130 /** Time at which this request was emitted, in jiffies. */
131 unsigned long emitted_jiffies;
132
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100133 /** engine->request_list entry for this request */
134 struct list_head link;
Chris Wilson05235c52016-07-20 09:21:08 +0100135
Chris Wilson675d9ad2016-08-04 07:52:36 +0100136 /** ring->request_list entry for this request */
137 struct list_head ring_link;
138
Chris Wilson05235c52016-07-20 09:21:08 +0100139 struct drm_i915_file_private *file_priv;
140 /** file_priv list entry for this request */
141 struct list_head client_list;
142
Chris Wilson70c2a242016-09-09 14:11:46 +0100143 /** Link in the execlist submission queue, guarded by execlist_lock. */
Chris Wilson05235c52016-07-20 09:21:08 +0100144 struct list_head execlist_link;
Chris Wilson05235c52016-07-20 09:21:08 +0100145};
146
Chris Wilson04769652016-07-20 09:21:11 +0100147extern const struct fence_ops i915_fence_ops;
148
149static inline bool fence_is_i915(struct fence *fence)
150{
151 return fence->ops == &i915_fence_ops;
152}
153
Chris Wilson05235c52016-07-20 09:21:08 +0100154struct drm_i915_gem_request * __must_check
155i915_gem_request_alloc(struct intel_engine_cs *engine,
156 struct i915_gem_context *ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100157int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
158 struct drm_file *file);
159void i915_gem_request_retire_upto(struct drm_i915_gem_request *req);
160
161static inline u32
162i915_gem_request_get_seqno(struct drm_i915_gem_request *req)
163{
Chris Wilson04769652016-07-20 09:21:11 +0100164 return req ? req->fence.seqno : 0;
Chris Wilson05235c52016-07-20 09:21:08 +0100165}
166
167static inline struct intel_engine_cs *
168i915_gem_request_get_engine(struct drm_i915_gem_request *req)
169{
170 return req ? req->engine : NULL;
171}
172
173static inline struct drm_i915_gem_request *
Chris Wilson04769652016-07-20 09:21:11 +0100174to_request(struct fence *fence)
175{
176 /* We assume that NULL fence/request are interoperable */
177 BUILD_BUG_ON(offsetof(struct drm_i915_gem_request, fence) != 0);
178 GEM_BUG_ON(fence && !fence_is_i915(fence));
179 return container_of(fence, struct drm_i915_gem_request, fence);
180}
181
182static inline struct drm_i915_gem_request *
Chris Wilsone8a261e2016-07-20 13:31:49 +0100183i915_gem_request_get(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100184{
Chris Wilson04769652016-07-20 09:21:11 +0100185 return to_request(fence_get(&req->fence));
Chris Wilson05235c52016-07-20 09:21:08 +0100186}
187
Chris Wilson0eafec62016-08-04 16:32:41 +0100188static inline struct drm_i915_gem_request *
189i915_gem_request_get_rcu(struct drm_i915_gem_request *req)
190{
191 return to_request(fence_get_rcu(&req->fence));
192}
193
Chris Wilson05235c52016-07-20 09:21:08 +0100194static inline void
Chris Wilsone8a261e2016-07-20 13:31:49 +0100195i915_gem_request_put(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100196{
Chris Wilson04769652016-07-20 09:21:11 +0100197 fence_put(&req->fence);
Chris Wilson05235c52016-07-20 09:21:08 +0100198}
199
200static inline void i915_gem_request_assign(struct drm_i915_gem_request **pdst,
201 struct drm_i915_gem_request *src)
202{
203 if (src)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100204 i915_gem_request_get(src);
Chris Wilson05235c52016-07-20 09:21:08 +0100205
206 if (*pdst)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100207 i915_gem_request_put(*pdst);
Chris Wilson05235c52016-07-20 09:21:08 +0100208
209 *pdst = src;
210}
211
Chris Wilsona2bc4692016-09-09 14:11:56 +0100212int
213i915_gem_request_await_object(struct drm_i915_gem_request *to,
214 struct drm_i915_gem_object *obj,
215 bool write);
216
Chris Wilson17f298cf2016-08-10 13:41:46 +0100217void __i915_add_request(struct drm_i915_gem_request *req, bool flush_caches);
Chris Wilson05235c52016-07-20 09:21:08 +0100218#define i915_add_request(req) \
Chris Wilson17f298cf2016-08-10 13:41:46 +0100219 __i915_add_request(req, true)
Chris Wilson05235c52016-07-20 09:21:08 +0100220#define i915_add_request_no_flush(req) \
Chris Wilson17f298cf2016-08-10 13:41:46 +0100221 __i915_add_request(req, false)
Chris Wilson05235c52016-07-20 09:21:08 +0100222
223struct intel_rps_client;
Chris Wilson42df2712016-07-20 09:21:12 +0100224#define NO_WAITBOOST ERR_PTR(-1)
225#define IS_RPS_CLIENT(p) (!IS_ERR(p))
226#define IS_RPS_USER(p) (!IS_ERR_OR_NULL(p))
Chris Wilson05235c52016-07-20 09:21:08 +0100227
Chris Wilson776f3232016-08-04 07:52:40 +0100228int i915_wait_request(struct drm_i915_gem_request *req,
Chris Wilsonea746f32016-09-09 14:11:49 +0100229 unsigned int flags,
Chris Wilson776f3232016-08-04 07:52:40 +0100230 s64 *timeout,
231 struct intel_rps_client *rps)
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100232 __attribute__((nonnull(1)));
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100233#define I915_WAIT_INTERRUPTIBLE BIT(0)
234#define I915_WAIT_LOCKED BIT(1) /* struct_mutex held, handle GPU reset */
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100235
Chris Wilson05235c52016-07-20 09:21:08 +0100236static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine);
237
238/**
239 * Returns true if seq1 is later than seq2.
240 */
241static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
242{
243 return (s32)(seq1 - seq2) >= 0;
244}
245
246static inline bool
247i915_gem_request_started(const struct drm_i915_gem_request *req)
248{
249 return i915_seqno_passed(intel_engine_get_seqno(req->engine),
250 req->previous_seqno);
251}
252
253static inline bool
254i915_gem_request_completed(const struct drm_i915_gem_request *req)
255{
256 return i915_seqno_passed(intel_engine_get_seqno(req->engine),
Chris Wilson04769652016-07-20 09:21:11 +0100257 req->fence.seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100258}
259
260bool __i915_spin_request(const struct drm_i915_gem_request *request,
261 int state, unsigned long timeout_us);
262static inline bool i915_spin_request(const struct drm_i915_gem_request *request,
263 int state, unsigned long timeout_us)
264{
265 return (i915_gem_request_started(request) &&
266 __i915_spin_request(request, state, timeout_us));
267}
268
Chris Wilson381f3712016-08-04 07:52:29 +0100269/* We treat requests as fences. This is not be to confused with our
270 * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
271 * We use the fences to synchronize access from the CPU with activity on the
272 * GPU, for example, we should not rewrite an object's PTE whilst the GPU
273 * is reading them. We also track fences at a higher level to provide
274 * implicit synchronisation around GEM objects, e.g. set-domain will wait
275 * for outstanding GPU rendering before marking the object ready for CPU
276 * access, or a pageflip will wait until the GPU is complete before showing
277 * the frame on the scanout.
278 *
279 * In order to use a fence, the object must track the fence it needs to
280 * serialise with. For example, GEM objects want to track both read and
281 * write access so that we can perform concurrent read operations between
282 * the CPU and GPU engines, as well as waiting for all rendering to
283 * complete, or waiting for the last GPU user of a "fence register". The
284 * object then embeds a #i915_gem_active to track the most recent (in
285 * retirement order) request relevant for the desired mode of access.
286 * The #i915_gem_active is updated with i915_gem_active_set() to track the
287 * most recent fence request, typically this is done as part of
288 * i915_vma_move_to_active().
289 *
290 * When the #i915_gem_active completes (is retired), it will
291 * signal its completion to the owner through a callback as well as mark
292 * itself as idle (i915_gem_active.request == NULL). The owner
293 * can then perform any action, such as delayed freeing of an active
294 * resource including itself.
295 */
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100296struct i915_gem_active;
297
298typedef void (*i915_gem_retire_fn)(struct i915_gem_active *,
299 struct drm_i915_gem_request *);
300
Chris Wilson381f3712016-08-04 07:52:29 +0100301struct i915_gem_active {
Chris Wilson0eafec62016-08-04 16:32:41 +0100302 struct drm_i915_gem_request __rcu *request;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100303 struct list_head link;
304 i915_gem_retire_fn retire;
Chris Wilson381f3712016-08-04 07:52:29 +0100305};
306
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100307void i915_gem_retire_noop(struct i915_gem_active *,
308 struct drm_i915_gem_request *request);
309
310/**
311 * init_request_active - prepares the activity tracker for use
312 * @active - the active tracker
313 * @func - a callback when then the tracker is retired (becomes idle),
314 * can be NULL
315 *
316 * init_request_active() prepares the embedded @active struct for use as
317 * an activity tracker, that is for tracking the last known active request
318 * associated with it. When the last request becomes idle, when it is retired
319 * after completion, the optional callback @func is invoked.
320 */
321static inline void
322init_request_active(struct i915_gem_active *active,
323 i915_gem_retire_fn retire)
324{
325 INIT_LIST_HEAD(&active->link);
326 active->retire = retire ?: i915_gem_retire_noop;
327}
328
Chris Wilson27c01aa2016-08-04 07:52:30 +0100329/**
330 * i915_gem_active_set - updates the tracker to watch the current request
331 * @active - the active tracker
332 * @request - the request to watch
333 *
334 * i915_gem_active_set() watches the given @request for completion. Whilst
335 * that @request is busy, the @active reports busy. When that @request is
336 * retired, the @active tracker is updated to report idle.
337 */
Chris Wilson381f3712016-08-04 07:52:29 +0100338static inline void
339i915_gem_active_set(struct i915_gem_active *active,
340 struct drm_i915_gem_request *request)
341{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100342 list_move(&active->link, &request->active_list);
Chris Wilson0eafec62016-08-04 16:32:41 +0100343 rcu_assign_pointer(active->request, request);
Chris Wilson381f3712016-08-04 07:52:29 +0100344}
345
Chris Wilsond72d9082016-08-04 07:52:31 +0100346static inline struct drm_i915_gem_request *
347__i915_gem_active_peek(const struct i915_gem_active *active)
348{
Chris Wilson0eafec62016-08-04 16:32:41 +0100349 /* Inside the error capture (running with the driver in an unknown
350 * state), we want to bend the rules slightly (a lot).
351 *
352 * Work is in progress to make it safer, in the meantime this keeps
353 * the known issue from spamming the logs.
354 */
355 return rcu_dereference_protected(active->request, 1);
Chris Wilsond72d9082016-08-04 07:52:31 +0100356}
357
Chris Wilson27c01aa2016-08-04 07:52:30 +0100358/**
Chris Wilson385384a2016-08-09 08:37:01 +0100359 * i915_gem_active_raw - return the active request
360 * @active - the active tracker
361 *
362 * i915_gem_active_raw() returns the current request being tracked, or NULL.
363 * It does not obtain a reference on the request for the caller, so the caller
364 * must hold struct_mutex.
365 */
366static inline struct drm_i915_gem_request *
367i915_gem_active_raw(const struct i915_gem_active *active, struct mutex *mutex)
368{
369 return rcu_dereference_protected(active->request,
370 lockdep_is_held(mutex));
371}
372
373/**
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100374 * i915_gem_active_peek - report the active request being monitored
Chris Wilson27c01aa2016-08-04 07:52:30 +0100375 * @active - the active tracker
376 *
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100377 * i915_gem_active_peek() returns the current request being tracked if
378 * still active, or NULL. It does not obtain a reference on the request
379 * for the caller, so the caller must hold struct_mutex.
Chris Wilson27c01aa2016-08-04 07:52:30 +0100380 */
381static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100382i915_gem_active_peek(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100383{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100384 struct drm_i915_gem_request *request;
385
Chris Wilson385384a2016-08-09 08:37:01 +0100386 request = i915_gem_active_raw(active, mutex);
Chris Wilson0eafec62016-08-04 16:32:41 +0100387 if (!request || i915_gem_request_completed(request))
388 return NULL;
389
390 return request;
391}
392
393/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100394 * i915_gem_active_get - return a reference to the active request
395 * @active - the active tracker
396 *
397 * i915_gem_active_get() returns a reference to the active request, or NULL
398 * if the active tracker is idle. The caller must hold struct_mutex.
399 */
400static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100401i915_gem_active_get(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100402{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100403 return i915_gem_request_get(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100404}
405
406/**
Chris Wilson0eafec62016-08-04 16:32:41 +0100407 * __i915_gem_active_get_rcu - return a reference to the active request
408 * @active - the active tracker
409 *
410 * __i915_gem_active_get() returns a reference to the active request, or NULL
411 * if the active tracker is idle. The caller must hold the RCU read lock, but
412 * the returned pointer is safe to use outside of RCU.
413 */
414static inline struct drm_i915_gem_request *
415__i915_gem_active_get_rcu(const struct i915_gem_active *active)
416{
417 /* Performing a lockless retrieval of the active request is super
418 * tricky. SLAB_DESTROY_BY_RCU merely guarantees that the backing
419 * slab of request objects will not be freed whilst we hold the
420 * RCU read lock. It does not guarantee that the request itself
421 * will not be freed and then *reused*. Viz,
422 *
423 * Thread A Thread B
424 *
425 * req = active.request
426 * retire(req) -> free(req);
427 * (req is now first on the slab freelist)
428 * active.request = NULL
429 *
430 * req = new submission on a new object
431 * ref(req)
432 *
433 * To prevent the request from being reused whilst the caller
434 * uses it, we take a reference like normal. Whilst acquiring
435 * the reference we check that it is not in a destroyed state
436 * (refcnt == 0). That prevents the request being reallocated
437 * whilst the caller holds on to it. To check that the request
438 * was not reallocated as we acquired the reference we have to
439 * check that our request remains the active request across
440 * the lookup, in the same manner as a seqlock. The visibility
441 * of the pointer versus the reference counting is controlled
442 * by using RCU barriers (rcu_dereference and rcu_assign_pointer).
443 *
444 * In the middle of all that, we inspect whether the request is
445 * complete. Retiring is lazy so the request may be completed long
446 * before the active tracker is updated. Querying whether the
447 * request is complete is far cheaper (as it involves no locked
448 * instructions setting cachelines to exclusive) than acquiring
449 * the reference, so we do it first. The RCU read lock ensures the
450 * pointer dereference is valid, but does not ensure that the
451 * seqno nor HWS is the right one! However, if the request was
452 * reallocated, that means the active tracker's request was complete.
453 * If the new request is also complete, then both are and we can
454 * just report the active tracker is idle. If the new request is
455 * incomplete, then we acquire a reference on it and check that
456 * it remained the active request.
Chris Wilson5a198b82016-08-09 09:23:34 +0100457 *
458 * It is then imperative that we do not zero the request on
459 * reallocation, so that we can chase the dangling pointers!
460 * See i915_gem_request_alloc().
Chris Wilson0eafec62016-08-04 16:32:41 +0100461 */
462 do {
463 struct drm_i915_gem_request *request;
464
465 request = rcu_dereference(active->request);
466 if (!request || i915_gem_request_completed(request))
467 return NULL;
468
Daniel Vetterc75870d2016-08-22 10:55:22 +0200469 /* An especially silly compiler could decide to recompute the
470 * result of i915_gem_request_completed, more specifically
471 * re-emit the load for request->fence.seqno. A race would catch
472 * a later seqno value, which could flip the result from true to
473 * false. Which means part of the instructions below might not
474 * be executed, while later on instructions are executed. Due to
475 * barriers within the refcounting the inconsistency can't reach
476 * past the call to i915_gem_request_get_rcu, but not executing
477 * that while still executing i915_gem_request_put() creates
478 * havoc enough. Prevent this with a compiler barrier.
479 */
480 barrier();
481
Chris Wilson0eafec62016-08-04 16:32:41 +0100482 request = i915_gem_request_get_rcu(request);
483
484 /* What stops the following rcu_access_pointer() from occurring
485 * before the above i915_gem_request_get_rcu()? If we were
486 * to read the value before pausing to get the reference to
487 * the request, we may not notice a change in the active
488 * tracker.
489 *
490 * The rcu_access_pointer() is a mere compiler barrier, which
491 * means both the CPU and compiler are free to perform the
492 * memory read without constraint. The compiler only has to
493 * ensure that any operations after the rcu_access_pointer()
494 * occur afterwards in program order. This means the read may
495 * be performed earlier by an out-of-order CPU, or adventurous
496 * compiler.
497 *
498 * The atomic operation at the heart of
499 * i915_gem_request_get_rcu(), see fence_get_rcu(), is
500 * atomic_inc_not_zero() which is only a full memory barrier
501 * when successful. That is, if i915_gem_request_get_rcu()
502 * returns the request (and so with the reference counted
503 * incremented) then the following read for rcu_access_pointer()
504 * must occur after the atomic operation and so confirm
505 * that this request is the one currently being tracked.
Chris Wilsonedf6b762016-08-09 09:23:33 +0100506 *
507 * The corresponding write barrier is part of
508 * rcu_assign_pointer().
Chris Wilson0eafec62016-08-04 16:32:41 +0100509 */
510 if (!request || request == rcu_access_pointer(active->request))
511 return rcu_pointer_handoff(request);
512
513 i915_gem_request_put(request);
514 } while (1);
515}
516
517/**
518 * i915_gem_active_get_unlocked - return a reference to the active request
519 * @active - the active tracker
520 *
521 * i915_gem_active_get_unlocked() returns a reference to the active request,
522 * or NULL if the active tracker is idle. The reference is obtained under RCU,
523 * so no locking is required by the caller.
524 *
525 * The reference should be freed with i915_gem_request_put().
526 */
527static inline struct drm_i915_gem_request *
528i915_gem_active_get_unlocked(const struct i915_gem_active *active)
529{
530 struct drm_i915_gem_request *request;
531
532 rcu_read_lock();
533 request = __i915_gem_active_get_rcu(active);
534 rcu_read_unlock();
535
536 return request;
537}
538
539/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100540 * i915_gem_active_isset - report whether the active tracker is assigned
541 * @active - the active tracker
542 *
543 * i915_gem_active_isset() returns true if the active tracker is currently
544 * assigned to a request. Due to the lazy retiring, that request may be idle
545 * and this may report stale information.
546 */
547static inline bool
548i915_gem_active_isset(const struct i915_gem_active *active)
549{
Chris Wilson0eafec62016-08-04 16:32:41 +0100550 return rcu_access_pointer(active->request);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100551}
552
553/**
554 * i915_gem_active_is_idle - report whether the active tracker is idle
555 * @active - the active tracker
556 *
557 * i915_gem_active_is_idle() returns true if the active tracker is currently
558 * unassigned or if the request is complete (but not yet retired). Requires
559 * the caller to hold struct_mutex (but that can be relaxed if desired).
560 */
561static inline bool
Chris Wilsond72d9082016-08-04 07:52:31 +0100562i915_gem_active_is_idle(const struct i915_gem_active *active,
563 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100564{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100565 return !i915_gem_active_peek(active, mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100566}
567
568/**
569 * i915_gem_active_wait - waits until the request is completed
570 * @active - the active request on which to wait
571 *
572 * i915_gem_active_wait() waits until the request is completed before
573 * returning. Note that it does not guarantee that the request is
574 * retired first, see i915_gem_active_retire().
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100575 *
576 * i915_gem_active_wait() returns immediately if the active
577 * request is already complete.
Chris Wilson27c01aa2016-08-04 07:52:30 +0100578 */
579static inline int __must_check
Chris Wilsond72d9082016-08-04 07:52:31 +0100580i915_gem_active_wait(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100581{
582 struct drm_i915_gem_request *request;
583
Chris Wilsond72d9082016-08-04 07:52:31 +0100584 request = i915_gem_active_peek(active, mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100585 if (!request)
586 return 0;
587
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100588 return i915_wait_request(request,
589 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
590 NULL, NULL);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100591}
592
593/**
Chris Wilson24676582016-08-05 10:14:06 +0100594 * i915_gem_active_wait_unlocked - waits until the request is completed
595 * @active - the active request on which to wait
Chris Wilsonea746f32016-09-09 14:11:49 +0100596 * @flags - how to wait
Chris Wilson24676582016-08-05 10:14:06 +0100597 * @timeout - how long to wait at most
598 * @rps - userspace client to charge for a waitboost
599 *
600 * i915_gem_active_wait_unlocked() waits until the request is completed before
601 * returning, without requiring any locks to be held. Note that it does not
602 * retire any requests before returning.
603 *
604 * This function relies on RCU in order to acquire the reference to the active
605 * request without holding any locks. See __i915_gem_active_get_rcu() for the
606 * glory details on how that is managed. Once the reference is acquired, we
607 * can then wait upon the request, and afterwards release our reference,
608 * free of any locking.
609 *
610 * This function wraps i915_wait_request(), see it for the full details on
611 * the arguments.
612 *
613 * Returns 0 if successful, or a negative error code.
614 */
615static inline int
616i915_gem_active_wait_unlocked(const struct i915_gem_active *active,
Chris Wilsonea746f32016-09-09 14:11:49 +0100617 unsigned int flags,
Chris Wilson24676582016-08-05 10:14:06 +0100618 s64 *timeout,
619 struct intel_rps_client *rps)
620{
621 struct drm_i915_gem_request *request;
622 int ret = 0;
623
624 request = i915_gem_active_get_unlocked(active);
625 if (request) {
Chris Wilsonea746f32016-09-09 14:11:49 +0100626 ret = i915_wait_request(request, flags, timeout, rps);
Chris Wilson24676582016-08-05 10:14:06 +0100627 i915_gem_request_put(request);
628 }
629
630 return ret;
631}
632
633/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100634 * i915_gem_active_retire - waits until the request is retired
635 * @active - the active request on which to wait
636 *
637 * i915_gem_active_retire() waits until the request is completed,
638 * and then ensures that at least the retirement handler for this
639 * @active tracker is called before returning. If the @active
640 * tracker is idle, the function returns immediately.
641 */
642static inline int __must_check
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100643i915_gem_active_retire(struct i915_gem_active *active,
Chris Wilsond72d9082016-08-04 07:52:31 +0100644 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100645{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100646 struct drm_i915_gem_request *request;
647 int ret;
648
Chris Wilson385384a2016-08-09 08:37:01 +0100649 request = i915_gem_active_raw(active, mutex);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100650 if (!request)
651 return 0;
652
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100653 ret = i915_wait_request(request,
654 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
655 NULL, NULL);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100656 if (ret)
657 return ret;
658
659 list_del_init(&active->link);
Chris Wilson0eafec62016-08-04 16:32:41 +0100660 RCU_INIT_POINTER(active->request, NULL);
661
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100662 active->retire(active, request);
663
664 return 0;
Chris Wilson27c01aa2016-08-04 07:52:30 +0100665}
666
667/* Convenience functions for peeking at state inside active's request whilst
668 * guarded by the struct_mutex.
669 */
670
671static inline uint32_t
Chris Wilsond72d9082016-08-04 07:52:31 +0100672i915_gem_active_get_seqno(const struct i915_gem_active *active,
673 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100674{
Chris Wilsond72d9082016-08-04 07:52:31 +0100675 return i915_gem_request_get_seqno(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100676}
677
678static inline struct intel_engine_cs *
Chris Wilsond72d9082016-08-04 07:52:31 +0100679i915_gem_active_get_engine(const struct i915_gem_active *active,
680 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100681{
Chris Wilsond72d9082016-08-04 07:52:31 +0100682 return i915_gem_request_get_engine(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100683}
684
Chris Wilson381f3712016-08-04 07:52:29 +0100685#define for_each_active(mask, idx) \
686 for (; mask ? idx = ffs(mask) - 1, 1 : 0; mask &= ~BIT(idx))
687
Chris Wilson05235c52016-07-20 09:21:08 +0100688#endif /* I915_GEM_REQUEST_H */