blob: a56559e3b034524f3665ac62b43dcd997634cd13 [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 Wilsonf54d1862016-10-25 13:00:45 +010028#include <linux/dma-fence.h>
Chris Wilson04769652016-07-20 09:21:11 +010029
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
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +020033struct drm_file;
34struct drm_i915_gem_object;
35
Chris Wilsondcff85c2016-08-05 10:14:11 +010036struct intel_wait {
37 struct rb_node node;
38 struct task_struct *tsk;
39 u32 seqno;
40};
41
42struct intel_signal_node {
43 struct rb_node node;
44 struct intel_wait wait;
45};
46
Chris Wilson05235c52016-07-20 09:21:08 +010047/**
48 * Request queue structure.
49 *
50 * The request queue allows us to note sequence numbers that have been emitted
51 * and may be associated with active buffers to be retired.
52 *
53 * By keeping this list, we can avoid having to do questionable sequence
54 * number comparisons on buffer last_read|write_seqno. It also allows an
55 * emission time to be associated with the request for tracking how far ahead
56 * of the GPU the submission is.
57 *
Chris Wilson5a198b82016-08-09 09:23:34 +010058 * When modifying this structure be very aware that we perform a lockless
59 * RCU lookup of it that may race against reallocation of the struct
60 * from the slab freelist. We intentionally do not zero the structure on
61 * allocation so that the lookup can use the dangling pointers (and is
62 * cogniscent that those pointers may be wrong). Instead, everything that
63 * needs to be initialised must be done so explicitly.
64 *
Chris Wilson04769652016-07-20 09:21:11 +010065 * The requests are reference counted.
Chris Wilson05235c52016-07-20 09:21:08 +010066 */
67struct drm_i915_gem_request {
Chris Wilsonf54d1862016-10-25 13:00:45 +010068 struct dma_fence fence;
Chris Wilson04769652016-07-20 09:21:11 +010069 spinlock_t lock;
Chris Wilson05235c52016-07-20 09:21:08 +010070
71 /** On Which ring this request was generated */
72 struct drm_i915_private *i915;
73
74 /**
75 * Context and ring buffer related to this request
76 * Contexts are refcounted, so when this request is associated with a
77 * context, we must increment the context's refcount, to guarantee that
78 * it persists while any request is linked to it. Requests themselves
79 * are also refcounted, so the request will only be freed when the last
80 * reference to it is dismissed, and the code in
81 * i915_gem_request_free() will then decrement the refcount on the
82 * context.
83 */
84 struct i915_gem_context *ctx;
85 struct intel_engine_cs *engine;
Chris Wilson7e37f882016-08-02 22:50:21 +010086 struct intel_ring *ring;
Chris Wilson73cb9702016-10-28 13:58:46 +010087 struct intel_timeline *timeline;
Chris Wilson05235c52016-07-20 09:21:08 +010088 struct intel_signal_node signaling;
89
Chris Wilson5590af32016-09-09 14:11:54 +010090 struct i915_sw_fence submit;
Chris Wilson0a046a02016-09-09 14:12:00 +010091 wait_queue_t submitq;
Chris Wilson5590af32016-09-09 14:11:54 +010092
Chris Wilson65e47602016-10-28 13:58:49 +010093 u32 global_seqno;
94
Chris Wilson05235c52016-07-20 09:21:08 +010095 /** GEM sequence number associated with the previous request,
96 * when the HWS breadcrumb is equal to this the GPU is processing
97 * this request.
98 */
99 u32 previous_seqno;
100
Chris Wilsona52abd22016-09-09 14:11:43 +0100101 /** Position in the ring of the start of the request */
Chris Wilson05235c52016-07-20 09:21:08 +0100102 u32 head;
103
104 /**
Chris Wilsona52abd22016-09-09 14:11:43 +0100105 * Position in the ring of the start of the postfix.
106 * This is required to calculate the maximum available ring space
107 * without overwriting the postfix.
Chris Wilson05235c52016-07-20 09:21:08 +0100108 */
109 u32 postfix;
110
Chris Wilsona52abd22016-09-09 14:11:43 +0100111 /** Position in the ring of the end of the whole request */
Chris Wilson05235c52016-07-20 09:21:08 +0100112 u32 tail;
113
Chris Wilsona52abd22016-09-09 14:11:43 +0100114 /** Position in the ring of the end of any workarounds after the tail */
115 u32 wa_tail;
116
117 /** Preallocate space in the ring for the emitting the request */
Chris Wilson05235c52016-07-20 09:21:08 +0100118 u32 reserved_space;
119
120 /**
121 * Context related to the previous request.
122 * As the contexts are accessed by the hardware until the switch is
123 * completed to a new context, the hardware may still be writing
124 * to the context object after the breadcrumb is visible. We must
125 * not unpin/unbind/prune that object whilst still active and so
126 * we keep the previous context pinned until the following (this)
127 * request is retired.
128 */
129 struct i915_gem_context *previous_context;
130
131 /** Batch buffer related to this request if any (used for
132 * error state dump only).
133 */
Chris Wilson058d88c2016-08-15 10:49:06 +0100134 struct i915_vma *batch;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100135 struct list_head active_list;
Chris Wilson05235c52016-07-20 09:21:08 +0100136
137 /** Time at which this request was emitted, in jiffies. */
138 unsigned long emitted_jiffies;
139
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100140 /** engine->request_list entry for this request */
141 struct list_head link;
Chris Wilson05235c52016-07-20 09:21:08 +0100142
Chris Wilson675d9ad2016-08-04 07:52:36 +0100143 /** ring->request_list entry for this request */
144 struct list_head ring_link;
145
Chris Wilson05235c52016-07-20 09:21:08 +0100146 struct drm_i915_file_private *file_priv;
147 /** file_priv list entry for this request */
148 struct list_head client_list;
149
Chris Wilson70c2a242016-09-09 14:11:46 +0100150 /** Link in the execlist submission queue, guarded by execlist_lock. */
Chris Wilson05235c52016-07-20 09:21:08 +0100151 struct list_head execlist_link;
Chris Wilson05235c52016-07-20 09:21:08 +0100152};
153
Chris Wilsonf54d1862016-10-25 13:00:45 +0100154extern const struct dma_fence_ops i915_fence_ops;
Chris Wilson04769652016-07-20 09:21:11 +0100155
Chris Wilsonb52992c2016-10-28 13:58:24 +0100156static inline bool dma_fence_is_i915(const struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +0100157{
158 return fence->ops == &i915_fence_ops;
159}
160
Chris Wilson05235c52016-07-20 09:21:08 +0100161struct drm_i915_gem_request * __must_check
162i915_gem_request_alloc(struct intel_engine_cs *engine,
163 struct i915_gem_context *ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100164int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
165 struct drm_file *file);
166void i915_gem_request_retire_upto(struct drm_i915_gem_request *req);
167
Chris Wilson05235c52016-07-20 09:21:08 +0100168static inline struct drm_i915_gem_request *
Chris Wilsonf54d1862016-10-25 13:00:45 +0100169to_request(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +0100170{
171 /* We assume that NULL fence/request are interoperable */
172 BUILD_BUG_ON(offsetof(struct drm_i915_gem_request, fence) != 0);
Chris Wilsonb52992c2016-10-28 13:58:24 +0100173 GEM_BUG_ON(fence && !dma_fence_is_i915(fence));
Chris Wilson04769652016-07-20 09:21:11 +0100174 return container_of(fence, struct drm_i915_gem_request, fence);
175}
176
177static inline struct drm_i915_gem_request *
Chris Wilsone8a261e2016-07-20 13:31:49 +0100178i915_gem_request_get(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100179{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100180 return to_request(dma_fence_get(&req->fence));
Chris Wilson05235c52016-07-20 09:21:08 +0100181}
182
Chris Wilson0eafec62016-08-04 16:32:41 +0100183static inline struct drm_i915_gem_request *
184i915_gem_request_get_rcu(struct drm_i915_gem_request *req)
185{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100186 return to_request(dma_fence_get_rcu(&req->fence));
Chris Wilson0eafec62016-08-04 16:32:41 +0100187}
188
Chris Wilson05235c52016-07-20 09:21:08 +0100189static inline void
Chris Wilsone8a261e2016-07-20 13:31:49 +0100190i915_gem_request_put(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100191{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100192 dma_fence_put(&req->fence);
Chris Wilson05235c52016-07-20 09:21:08 +0100193}
194
195static inline void i915_gem_request_assign(struct drm_i915_gem_request **pdst,
196 struct drm_i915_gem_request *src)
197{
198 if (src)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100199 i915_gem_request_get(src);
Chris Wilson05235c52016-07-20 09:21:08 +0100200
201 if (*pdst)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100202 i915_gem_request_put(*pdst);
Chris Wilson05235c52016-07-20 09:21:08 +0100203
204 *pdst = src;
205}
206
Chris Wilsona2bc4692016-09-09 14:11:56 +0100207int
208i915_gem_request_await_object(struct drm_i915_gem_request *to,
209 struct drm_i915_gem_object *obj,
210 bool write);
Chris Wilsonb52992c2016-10-28 13:58:24 +0100211int i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
212 struct dma_fence *fence);
Chris Wilsona2bc4692016-09-09 14:11:56 +0100213
Chris Wilson17f298cf2016-08-10 13:41:46 +0100214void __i915_add_request(struct drm_i915_gem_request *req, bool flush_caches);
Chris Wilson05235c52016-07-20 09:21:08 +0100215#define i915_add_request(req) \
Chris Wilson17f298cf2016-08-10 13:41:46 +0100216 __i915_add_request(req, true)
Chris Wilson05235c52016-07-20 09:21:08 +0100217#define i915_add_request_no_flush(req) \
Chris Wilson17f298cf2016-08-10 13:41:46 +0100218 __i915_add_request(req, false)
Chris Wilson05235c52016-07-20 09:21:08 +0100219
220struct intel_rps_client;
Chris Wilson42df2712016-07-20 09:21:12 +0100221#define NO_WAITBOOST ERR_PTR(-1)
222#define IS_RPS_CLIENT(p) (!IS_ERR(p))
223#define IS_RPS_USER(p) (!IS_ERR_OR_NULL(p))
Chris Wilson05235c52016-07-20 09:21:08 +0100224
Chris Wilsone95433c2016-10-28 13:58:27 +0100225long i915_wait_request(struct drm_i915_gem_request *req,
226 unsigned int flags,
227 long timeout)
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100228 __attribute__((nonnull(1)));
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100229#define I915_WAIT_INTERRUPTIBLE BIT(0)
230#define I915_WAIT_LOCKED BIT(1) /* struct_mutex held, handle GPU reset */
Chris Wilsone95433c2016-10-28 13:58:27 +0100231#define I915_WAIT_ALL BIT(2) /* used by i915_gem_object_wait() */
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100232
Chris Wilson05235c52016-07-20 09:21:08 +0100233static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine);
234
235/**
236 * Returns true if seq1 is later than seq2.
237 */
238static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
239{
240 return (s32)(seq1 - seq2) >= 0;
241}
242
243static inline bool
Chris Wilson65e47602016-10-28 13:58:49 +0100244__i915_gem_request_started(const struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100245{
Chris Wilson65e47602016-10-28 13:58:49 +0100246 GEM_BUG_ON(!req->global_seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100247 return i915_seqno_passed(intel_engine_get_seqno(req->engine),
248 req->previous_seqno);
249}
250
251static inline bool
Chris Wilson65e47602016-10-28 13:58:49 +0100252i915_gem_request_started(const struct drm_i915_gem_request *req)
253{
254 if (!req->global_seqno)
255 return false;
256
257 return __i915_gem_request_started(req);
258}
259
260static inline bool
261__i915_gem_request_completed(const struct drm_i915_gem_request *req)
262{
263 GEM_BUG_ON(!req->global_seqno);
264 return i915_seqno_passed(intel_engine_get_seqno(req->engine),
265 req->global_seqno);
266}
267
268static inline bool
Chris Wilson05235c52016-07-20 09:21:08 +0100269i915_gem_request_completed(const struct drm_i915_gem_request *req)
270{
Chris Wilson65e47602016-10-28 13:58:49 +0100271 if (!req->global_seqno)
272 return false;
273
274 return __i915_gem_request_completed(req);
Chris Wilson05235c52016-07-20 09:21:08 +0100275}
276
277bool __i915_spin_request(const struct drm_i915_gem_request *request,
278 int state, unsigned long timeout_us);
279static inline bool i915_spin_request(const struct drm_i915_gem_request *request,
280 int state, unsigned long timeout_us)
281{
Chris Wilson65e47602016-10-28 13:58:49 +0100282 return (__i915_gem_request_started(request) &&
Chris Wilson05235c52016-07-20 09:21:08 +0100283 __i915_spin_request(request, state, timeout_us));
284}
285
Chris Wilson381f3712016-08-04 07:52:29 +0100286/* We treat requests as fences. This is not be to confused with our
287 * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
288 * We use the fences to synchronize access from the CPU with activity on the
289 * GPU, for example, we should not rewrite an object's PTE whilst the GPU
290 * is reading them. We also track fences at a higher level to provide
291 * implicit synchronisation around GEM objects, e.g. set-domain will wait
292 * for outstanding GPU rendering before marking the object ready for CPU
293 * access, or a pageflip will wait until the GPU is complete before showing
294 * the frame on the scanout.
295 *
296 * In order to use a fence, the object must track the fence it needs to
297 * serialise with. For example, GEM objects want to track both read and
298 * write access so that we can perform concurrent read operations between
299 * the CPU and GPU engines, as well as waiting for all rendering to
300 * complete, or waiting for the last GPU user of a "fence register". The
301 * object then embeds a #i915_gem_active to track the most recent (in
302 * retirement order) request relevant for the desired mode of access.
303 * The #i915_gem_active is updated with i915_gem_active_set() to track the
304 * most recent fence request, typically this is done as part of
305 * i915_vma_move_to_active().
306 *
307 * When the #i915_gem_active completes (is retired), it will
308 * signal its completion to the owner through a callback as well as mark
309 * itself as idle (i915_gem_active.request == NULL). The owner
310 * can then perform any action, such as delayed freeing of an active
311 * resource including itself.
312 */
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100313struct i915_gem_active;
314
315typedef void (*i915_gem_retire_fn)(struct i915_gem_active *,
316 struct drm_i915_gem_request *);
317
Chris Wilson381f3712016-08-04 07:52:29 +0100318struct i915_gem_active {
Chris Wilson0eafec62016-08-04 16:32:41 +0100319 struct drm_i915_gem_request __rcu *request;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100320 struct list_head link;
321 i915_gem_retire_fn retire;
Chris Wilson381f3712016-08-04 07:52:29 +0100322};
323
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100324void i915_gem_retire_noop(struct i915_gem_active *,
325 struct drm_i915_gem_request *request);
326
327/**
328 * init_request_active - prepares the activity tracker for use
329 * @active - the active tracker
330 * @func - a callback when then the tracker is retired (becomes idle),
331 * can be NULL
332 *
333 * init_request_active() prepares the embedded @active struct for use as
334 * an activity tracker, that is for tracking the last known active request
335 * associated with it. When the last request becomes idle, when it is retired
336 * after completion, the optional callback @func is invoked.
337 */
338static inline void
339init_request_active(struct i915_gem_active *active,
340 i915_gem_retire_fn retire)
341{
342 INIT_LIST_HEAD(&active->link);
343 active->retire = retire ?: i915_gem_retire_noop;
344}
345
Chris Wilson27c01aa2016-08-04 07:52:30 +0100346/**
347 * i915_gem_active_set - updates the tracker to watch the current request
348 * @active - the active tracker
349 * @request - the request to watch
350 *
351 * i915_gem_active_set() watches the given @request for completion. Whilst
352 * that @request is busy, the @active reports busy. When that @request is
353 * retired, the @active tracker is updated to report idle.
354 */
Chris Wilson381f3712016-08-04 07:52:29 +0100355static inline void
356i915_gem_active_set(struct i915_gem_active *active,
357 struct drm_i915_gem_request *request)
358{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100359 list_move(&active->link, &request->active_list);
Chris Wilson0eafec62016-08-04 16:32:41 +0100360 rcu_assign_pointer(active->request, request);
Chris Wilson381f3712016-08-04 07:52:29 +0100361}
362
Chris Wilsond72d9082016-08-04 07:52:31 +0100363static inline struct drm_i915_gem_request *
364__i915_gem_active_peek(const struct i915_gem_active *active)
365{
Chris Wilson0eafec62016-08-04 16:32:41 +0100366 /* Inside the error capture (running with the driver in an unknown
367 * state), we want to bend the rules slightly (a lot).
368 *
369 * Work is in progress to make it safer, in the meantime this keeps
370 * the known issue from spamming the logs.
371 */
372 return rcu_dereference_protected(active->request, 1);
Chris Wilsond72d9082016-08-04 07:52:31 +0100373}
374
Chris Wilson27c01aa2016-08-04 07:52:30 +0100375/**
Chris Wilson385384a2016-08-09 08:37:01 +0100376 * i915_gem_active_raw - return the active request
377 * @active - the active tracker
378 *
379 * i915_gem_active_raw() returns the current request being tracked, or NULL.
380 * It does not obtain a reference on the request for the caller, so the caller
381 * must hold struct_mutex.
382 */
383static inline struct drm_i915_gem_request *
384i915_gem_active_raw(const struct i915_gem_active *active, struct mutex *mutex)
385{
386 return rcu_dereference_protected(active->request,
387 lockdep_is_held(mutex));
388}
389
390/**
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100391 * i915_gem_active_peek - report the active request being monitored
Chris Wilson27c01aa2016-08-04 07:52:30 +0100392 * @active - the active tracker
393 *
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100394 * i915_gem_active_peek() returns the current request being tracked if
395 * still active, or NULL. It does not obtain a reference on the request
396 * for the caller, so the caller must hold struct_mutex.
Chris Wilson27c01aa2016-08-04 07:52:30 +0100397 */
398static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100399i915_gem_active_peek(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100400{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100401 struct drm_i915_gem_request *request;
402
Chris Wilson385384a2016-08-09 08:37:01 +0100403 request = i915_gem_active_raw(active, mutex);
Chris Wilson0eafec62016-08-04 16:32:41 +0100404 if (!request || i915_gem_request_completed(request))
405 return NULL;
406
407 return request;
408}
409
410/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100411 * i915_gem_active_get - return a reference to the active request
412 * @active - the active tracker
413 *
414 * i915_gem_active_get() returns a reference to the active request, or NULL
415 * if the active tracker is idle. The caller must hold struct_mutex.
416 */
417static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100418i915_gem_active_get(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100419{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100420 return i915_gem_request_get(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100421}
422
423/**
Chris Wilson0eafec62016-08-04 16:32:41 +0100424 * __i915_gem_active_get_rcu - return a reference to the active request
425 * @active - the active tracker
426 *
427 * __i915_gem_active_get() returns a reference to the active request, or NULL
428 * if the active tracker is idle. The caller must hold the RCU read lock, but
429 * the returned pointer is safe to use outside of RCU.
430 */
431static inline struct drm_i915_gem_request *
432__i915_gem_active_get_rcu(const struct i915_gem_active *active)
433{
434 /* Performing a lockless retrieval of the active request is super
435 * tricky. SLAB_DESTROY_BY_RCU merely guarantees that the backing
436 * slab of request objects will not be freed whilst we hold the
437 * RCU read lock. It does not guarantee that the request itself
438 * will not be freed and then *reused*. Viz,
439 *
440 * Thread A Thread B
441 *
442 * req = active.request
443 * retire(req) -> free(req);
444 * (req is now first on the slab freelist)
445 * active.request = NULL
446 *
447 * req = new submission on a new object
448 * ref(req)
449 *
450 * To prevent the request from being reused whilst the caller
451 * uses it, we take a reference like normal. Whilst acquiring
452 * the reference we check that it is not in a destroyed state
453 * (refcnt == 0). That prevents the request being reallocated
454 * whilst the caller holds on to it. To check that the request
455 * was not reallocated as we acquired the reference we have to
456 * check that our request remains the active request across
457 * the lookup, in the same manner as a seqlock. The visibility
458 * of the pointer versus the reference counting is controlled
459 * by using RCU barriers (rcu_dereference and rcu_assign_pointer).
460 *
461 * In the middle of all that, we inspect whether the request is
462 * complete. Retiring is lazy so the request may be completed long
463 * before the active tracker is updated. Querying whether the
464 * request is complete is far cheaper (as it involves no locked
465 * instructions setting cachelines to exclusive) than acquiring
466 * the reference, so we do it first. The RCU read lock ensures the
467 * pointer dereference is valid, but does not ensure that the
468 * seqno nor HWS is the right one! However, if the request was
469 * reallocated, that means the active tracker's request was complete.
470 * If the new request is also complete, then both are and we can
471 * just report the active tracker is idle. If the new request is
472 * incomplete, then we acquire a reference on it and check that
473 * it remained the active request.
Chris Wilson5a198b82016-08-09 09:23:34 +0100474 *
475 * It is then imperative that we do not zero the request on
476 * reallocation, so that we can chase the dangling pointers!
477 * See i915_gem_request_alloc().
Chris Wilson0eafec62016-08-04 16:32:41 +0100478 */
479 do {
480 struct drm_i915_gem_request *request;
481
482 request = rcu_dereference(active->request);
483 if (!request || i915_gem_request_completed(request))
484 return NULL;
485
Daniel Vetterc75870d2016-08-22 10:55:22 +0200486 /* An especially silly compiler could decide to recompute the
487 * result of i915_gem_request_completed, more specifically
488 * re-emit the load for request->fence.seqno. A race would catch
489 * a later seqno value, which could flip the result from true to
490 * false. Which means part of the instructions below might not
491 * be executed, while later on instructions are executed. Due to
492 * barriers within the refcounting the inconsistency can't reach
493 * past the call to i915_gem_request_get_rcu, but not executing
494 * that while still executing i915_gem_request_put() creates
495 * havoc enough. Prevent this with a compiler barrier.
496 */
497 barrier();
498
Chris Wilson0eafec62016-08-04 16:32:41 +0100499 request = i915_gem_request_get_rcu(request);
500
501 /* What stops the following rcu_access_pointer() from occurring
502 * before the above i915_gem_request_get_rcu()? If we were
503 * to read the value before pausing to get the reference to
504 * the request, we may not notice a change in the active
505 * tracker.
506 *
507 * The rcu_access_pointer() is a mere compiler barrier, which
508 * means both the CPU and compiler are free to perform the
509 * memory read without constraint. The compiler only has to
510 * ensure that any operations after the rcu_access_pointer()
511 * occur afterwards in program order. This means the read may
512 * be performed earlier by an out-of-order CPU, or adventurous
513 * compiler.
514 *
515 * The atomic operation at the heart of
Chris Wilsonf54d1862016-10-25 13:00:45 +0100516 * i915_gem_request_get_rcu(), see dma_fence_get_rcu(), is
Chris Wilson0eafec62016-08-04 16:32:41 +0100517 * atomic_inc_not_zero() which is only a full memory barrier
518 * when successful. That is, if i915_gem_request_get_rcu()
519 * returns the request (and so with the reference counted
520 * incremented) then the following read for rcu_access_pointer()
521 * must occur after the atomic operation and so confirm
522 * that this request is the one currently being tracked.
Chris Wilsonedf6b762016-08-09 09:23:33 +0100523 *
524 * The corresponding write barrier is part of
525 * rcu_assign_pointer().
Chris Wilson0eafec62016-08-04 16:32:41 +0100526 */
527 if (!request || request == rcu_access_pointer(active->request))
528 return rcu_pointer_handoff(request);
529
530 i915_gem_request_put(request);
531 } while (1);
532}
533
534/**
535 * i915_gem_active_get_unlocked - return a reference to the active request
536 * @active - the active tracker
537 *
538 * i915_gem_active_get_unlocked() returns a reference to the active request,
539 * or NULL if the active tracker is idle. The reference is obtained under RCU,
540 * so no locking is required by the caller.
541 *
542 * The reference should be freed with i915_gem_request_put().
543 */
544static inline struct drm_i915_gem_request *
545i915_gem_active_get_unlocked(const struct i915_gem_active *active)
546{
547 struct drm_i915_gem_request *request;
548
549 rcu_read_lock();
550 request = __i915_gem_active_get_rcu(active);
551 rcu_read_unlock();
552
553 return request;
554}
555
556/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100557 * i915_gem_active_isset - report whether the active tracker is assigned
558 * @active - the active tracker
559 *
560 * i915_gem_active_isset() returns true if the active tracker is currently
561 * assigned to a request. Due to the lazy retiring, that request may be idle
562 * and this may report stale information.
563 */
564static inline bool
565i915_gem_active_isset(const struct i915_gem_active *active)
566{
Chris Wilson0eafec62016-08-04 16:32:41 +0100567 return rcu_access_pointer(active->request);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100568}
569
570/**
Chris Wilsond07f0e52016-10-28 13:58:44 +0100571 * i915_gem_active_wait - waits until the request is completed
Chris Wilson24676582016-08-05 10:14:06 +0100572 * @active - the active request on which to wait
Chris Wilsonea746f32016-09-09 14:11:49 +0100573 * @flags - how to wait
Chris Wilson24676582016-08-05 10:14:06 +0100574 * @timeout - how long to wait at most
575 * @rps - userspace client to charge for a waitboost
576 *
Chris Wilson2e369912016-10-28 13:58:28 +0100577 * i915_gem_active_wait() waits until the request is completed before
Chris Wilson24676582016-08-05 10:14:06 +0100578 * returning, without requiring any locks to be held. Note that it does not
579 * retire any requests before returning.
580 *
581 * This function relies on RCU in order to acquire the reference to the active
582 * request without holding any locks. See __i915_gem_active_get_rcu() for the
583 * glory details on how that is managed. Once the reference is acquired, we
584 * can then wait upon the request, and afterwards release our reference,
585 * free of any locking.
586 *
587 * This function wraps i915_wait_request(), see it for the full details on
588 * the arguments.
589 *
590 * Returns 0 if successful, or a negative error code.
591 */
592static inline int
Chris Wilson2e369912016-10-28 13:58:28 +0100593i915_gem_active_wait(const struct i915_gem_active *active, unsigned int flags)
Chris Wilson24676582016-08-05 10:14:06 +0100594{
595 struct drm_i915_gem_request *request;
Chris Wilsone95433c2016-10-28 13:58:27 +0100596 long ret = 0;
Chris Wilson24676582016-08-05 10:14:06 +0100597
598 request = i915_gem_active_get_unlocked(active);
599 if (request) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100600 ret = i915_wait_request(request, flags, MAX_SCHEDULE_TIMEOUT);
Chris Wilson24676582016-08-05 10:14:06 +0100601 i915_gem_request_put(request);
602 }
603
Chris Wilsone95433c2016-10-28 13:58:27 +0100604 return ret < 0 ? ret : 0;
Chris Wilson24676582016-08-05 10:14:06 +0100605}
606
607/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100608 * i915_gem_active_retire - waits until the request is retired
609 * @active - the active request on which to wait
610 *
611 * i915_gem_active_retire() waits until the request is completed,
612 * and then ensures that at least the retirement handler for this
613 * @active tracker is called before returning. If the @active
614 * tracker is idle, the function returns immediately.
615 */
616static inline int __must_check
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100617i915_gem_active_retire(struct i915_gem_active *active,
Chris Wilsond72d9082016-08-04 07:52:31 +0100618 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100619{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100620 struct drm_i915_gem_request *request;
Chris Wilsone95433c2016-10-28 13:58:27 +0100621 long ret;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100622
Chris Wilson385384a2016-08-09 08:37:01 +0100623 request = i915_gem_active_raw(active, mutex);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100624 if (!request)
625 return 0;
626
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100627 ret = i915_wait_request(request,
628 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
Chris Wilsone95433c2016-10-28 13:58:27 +0100629 MAX_SCHEDULE_TIMEOUT);
630 if (ret < 0)
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100631 return ret;
632
633 list_del_init(&active->link);
Chris Wilson0eafec62016-08-04 16:32:41 +0100634 RCU_INIT_POINTER(active->request, NULL);
635
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100636 active->retire(active, request);
637
638 return 0;
Chris Wilson27c01aa2016-08-04 07:52:30 +0100639}
640
Chris Wilson381f3712016-08-04 07:52:29 +0100641#define for_each_active(mask, idx) \
642 for (; mask ? idx = ffs(mask) - 1, 1 : 0; mask &= ~BIT(idx))
643
Chris Wilson05235c52016-07-20 09:21:08 +0100644#endif /* I915_GEM_REQUEST_H */