blob: 4976039189ea4e5afbf8e696fecd8b346b476bfe [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 Wilson23902e42016-11-14 20:40:58 +000090 /* Fences for the various phases in the request's lifetime.
91 *
92 * The submit fence is used to await upon all of the request's
93 * dependencies. When it is signaled, the request is ready to run.
94 * It is used by the driver to then queue the request for execution.
95 *
96 * The execute fence is used to signal when the request has been
97 * sent to hardware.
98 *
99 * It is illegal for the submit fence of one request to wait upon the
100 * execute fence of an earlier request. It should be sufficient to
101 * wait upon the submit fence of the earlier request.
102 */
Chris Wilson5590af32016-09-09 14:11:54 +0100103 struct i915_sw_fence submit;
Chris Wilson23902e42016-11-14 20:40:58 +0000104 struct i915_sw_fence execute;
Chris Wilson0a046a02016-09-09 14:12:00 +0100105 wait_queue_t submitq;
Chris Wilson23902e42016-11-14 20:40:58 +0000106 wait_queue_t execq;
Chris Wilson5590af32016-09-09 14:11:54 +0100107
Chris Wilson65e47602016-10-28 13:58:49 +0100108 u32 global_seqno;
109
Chris Wilson05235c52016-07-20 09:21:08 +0100110 /** GEM sequence number associated with the previous request,
111 * when the HWS breadcrumb is equal to this the GPU is processing
112 * this request.
113 */
114 u32 previous_seqno;
115
Chris Wilsona52abd22016-09-09 14:11:43 +0100116 /** Position in the ring of the start of the request */
Chris Wilson05235c52016-07-20 09:21:08 +0100117 u32 head;
118
119 /**
Chris Wilsona52abd22016-09-09 14:11:43 +0100120 * Position in the ring of the start of the postfix.
121 * This is required to calculate the maximum available ring space
122 * without overwriting the postfix.
Chris Wilson05235c52016-07-20 09:21:08 +0100123 */
124 u32 postfix;
125
Chris Wilsona52abd22016-09-09 14:11:43 +0100126 /** Position in the ring of the end of the whole request */
Chris Wilson05235c52016-07-20 09:21:08 +0100127 u32 tail;
128
Chris Wilsona52abd22016-09-09 14:11:43 +0100129 /** Position in the ring of the end of any workarounds after the tail */
130 u32 wa_tail;
131
132 /** Preallocate space in the ring for the emitting the request */
Chris Wilson05235c52016-07-20 09:21:08 +0100133 u32 reserved_space;
134
135 /**
136 * Context related to the previous request.
137 * As the contexts are accessed by the hardware until the switch is
138 * completed to a new context, the hardware may still be writing
139 * to the context object after the breadcrumb is visible. We must
140 * not unpin/unbind/prune that object whilst still active and so
141 * we keep the previous context pinned until the following (this)
142 * request is retired.
143 */
144 struct i915_gem_context *previous_context;
145
146 /** Batch buffer related to this request if any (used for
147 * error state dump only).
148 */
Chris Wilson058d88c2016-08-15 10:49:06 +0100149 struct i915_vma *batch;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100150 struct list_head active_list;
Chris Wilson05235c52016-07-20 09:21:08 +0100151
152 /** Time at which this request was emitted, in jiffies. */
153 unsigned long emitted_jiffies;
154
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100155 /** engine->request_list entry for this request */
156 struct list_head link;
Chris Wilson05235c52016-07-20 09:21:08 +0100157
Chris Wilson675d9ad2016-08-04 07:52:36 +0100158 /** ring->request_list entry for this request */
159 struct list_head ring_link;
160
Chris Wilson05235c52016-07-20 09:21:08 +0100161 struct drm_i915_file_private *file_priv;
162 /** file_priv list entry for this request */
163 struct list_head client_list;
164
Chris Wilson70c2a242016-09-09 14:11:46 +0100165 /** Link in the execlist submission queue, guarded by execlist_lock. */
Chris Wilson05235c52016-07-20 09:21:08 +0100166 struct list_head execlist_link;
Chris Wilson05235c52016-07-20 09:21:08 +0100167};
168
Chris Wilsonf54d1862016-10-25 13:00:45 +0100169extern const struct dma_fence_ops i915_fence_ops;
Chris Wilson04769652016-07-20 09:21:11 +0100170
Chris Wilsonb52992c2016-10-28 13:58:24 +0100171static inline bool dma_fence_is_i915(const struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +0100172{
173 return fence->ops == &i915_fence_ops;
174}
175
Chris Wilson05235c52016-07-20 09:21:08 +0100176struct drm_i915_gem_request * __must_check
177i915_gem_request_alloc(struct intel_engine_cs *engine,
178 struct i915_gem_context *ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100179int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
180 struct drm_file *file);
181void i915_gem_request_retire_upto(struct drm_i915_gem_request *req);
182
Chris Wilson05235c52016-07-20 09:21:08 +0100183static inline struct drm_i915_gem_request *
Chris Wilsonf54d1862016-10-25 13:00:45 +0100184to_request(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +0100185{
186 /* We assume that NULL fence/request are interoperable */
187 BUILD_BUG_ON(offsetof(struct drm_i915_gem_request, fence) != 0);
Chris Wilsonb52992c2016-10-28 13:58:24 +0100188 GEM_BUG_ON(fence && !dma_fence_is_i915(fence));
Chris Wilson04769652016-07-20 09:21:11 +0100189 return container_of(fence, struct drm_i915_gem_request, fence);
190}
191
192static inline struct drm_i915_gem_request *
Chris Wilsone8a261e2016-07-20 13:31:49 +0100193i915_gem_request_get(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100194{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100195 return to_request(dma_fence_get(&req->fence));
Chris Wilson05235c52016-07-20 09:21:08 +0100196}
197
Chris Wilson0eafec62016-08-04 16:32:41 +0100198static inline struct drm_i915_gem_request *
199i915_gem_request_get_rcu(struct drm_i915_gem_request *req)
200{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100201 return to_request(dma_fence_get_rcu(&req->fence));
Chris Wilson0eafec62016-08-04 16:32:41 +0100202}
203
Chris Wilson05235c52016-07-20 09:21:08 +0100204static inline void
Chris Wilsone8a261e2016-07-20 13:31:49 +0100205i915_gem_request_put(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100206{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100207 dma_fence_put(&req->fence);
Chris Wilson05235c52016-07-20 09:21:08 +0100208}
209
210static inline void i915_gem_request_assign(struct drm_i915_gem_request **pdst,
211 struct drm_i915_gem_request *src)
212{
213 if (src)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100214 i915_gem_request_get(src);
Chris Wilson05235c52016-07-20 09:21:08 +0100215
216 if (*pdst)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100217 i915_gem_request_put(*pdst);
Chris Wilson05235c52016-07-20 09:21:08 +0100218
219 *pdst = src;
220}
221
Chris Wilsona2bc4692016-09-09 14:11:56 +0100222int
223i915_gem_request_await_object(struct drm_i915_gem_request *to,
224 struct drm_i915_gem_object *obj,
225 bool write);
Chris Wilsonb52992c2016-10-28 13:58:24 +0100226int i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
227 struct dma_fence *fence);
Chris Wilsona2bc4692016-09-09 14:11:56 +0100228
Chris Wilson17f298cf2016-08-10 13:41:46 +0100229void __i915_add_request(struct drm_i915_gem_request *req, bool flush_caches);
Chris Wilson05235c52016-07-20 09:21:08 +0100230#define i915_add_request(req) \
Chris Wilson17f298cf2016-08-10 13:41:46 +0100231 __i915_add_request(req, true)
Chris Wilson05235c52016-07-20 09:21:08 +0100232#define i915_add_request_no_flush(req) \
Chris Wilson17f298cf2016-08-10 13:41:46 +0100233 __i915_add_request(req, false)
Chris Wilson05235c52016-07-20 09:21:08 +0100234
235struct intel_rps_client;
Chris Wilson42df2712016-07-20 09:21:12 +0100236#define NO_WAITBOOST ERR_PTR(-1)
237#define IS_RPS_CLIENT(p) (!IS_ERR(p))
238#define IS_RPS_USER(p) (!IS_ERR_OR_NULL(p))
Chris Wilson05235c52016-07-20 09:21:08 +0100239
Chris Wilsone95433c2016-10-28 13:58:27 +0100240long i915_wait_request(struct drm_i915_gem_request *req,
241 unsigned int flags,
242 long timeout)
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100243 __attribute__((nonnull(1)));
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100244#define I915_WAIT_INTERRUPTIBLE BIT(0)
245#define I915_WAIT_LOCKED BIT(1) /* struct_mutex held, handle GPU reset */
Chris Wilsone95433c2016-10-28 13:58:27 +0100246#define I915_WAIT_ALL BIT(2) /* used by i915_gem_object_wait() */
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100247
Chris Wilson05235c52016-07-20 09:21:08 +0100248static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine);
249
250/**
251 * Returns true if seq1 is later than seq2.
252 */
253static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
254{
255 return (s32)(seq1 - seq2) >= 0;
256}
257
258static inline bool
Chris Wilson65e47602016-10-28 13:58:49 +0100259__i915_gem_request_started(const struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100260{
Chris Wilson65e47602016-10-28 13:58:49 +0100261 GEM_BUG_ON(!req->global_seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100262 return i915_seqno_passed(intel_engine_get_seqno(req->engine),
263 req->previous_seqno);
264}
265
266static inline bool
Chris Wilson65e47602016-10-28 13:58:49 +0100267i915_gem_request_started(const struct drm_i915_gem_request *req)
268{
269 if (!req->global_seqno)
270 return false;
271
272 return __i915_gem_request_started(req);
273}
274
275static inline bool
276__i915_gem_request_completed(const struct drm_i915_gem_request *req)
277{
278 GEM_BUG_ON(!req->global_seqno);
279 return i915_seqno_passed(intel_engine_get_seqno(req->engine),
280 req->global_seqno);
281}
282
283static inline bool
Chris Wilson05235c52016-07-20 09:21:08 +0100284i915_gem_request_completed(const struct drm_i915_gem_request *req)
285{
Chris Wilson65e47602016-10-28 13:58:49 +0100286 if (!req->global_seqno)
287 return false;
288
289 return __i915_gem_request_completed(req);
Chris Wilson05235c52016-07-20 09:21:08 +0100290}
291
292bool __i915_spin_request(const struct drm_i915_gem_request *request,
293 int state, unsigned long timeout_us);
294static inline bool i915_spin_request(const struct drm_i915_gem_request *request,
295 int state, unsigned long timeout_us)
296{
Chris Wilson65e47602016-10-28 13:58:49 +0100297 return (__i915_gem_request_started(request) &&
Chris Wilson05235c52016-07-20 09:21:08 +0100298 __i915_spin_request(request, state, timeout_us));
299}
300
Chris Wilson381f3712016-08-04 07:52:29 +0100301/* We treat requests as fences. This is not be to confused with our
302 * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
303 * We use the fences to synchronize access from the CPU with activity on the
304 * GPU, for example, we should not rewrite an object's PTE whilst the GPU
305 * is reading them. We also track fences at a higher level to provide
306 * implicit synchronisation around GEM objects, e.g. set-domain will wait
307 * for outstanding GPU rendering before marking the object ready for CPU
308 * access, or a pageflip will wait until the GPU is complete before showing
309 * the frame on the scanout.
310 *
311 * In order to use a fence, the object must track the fence it needs to
312 * serialise with. For example, GEM objects want to track both read and
313 * write access so that we can perform concurrent read operations between
314 * the CPU and GPU engines, as well as waiting for all rendering to
315 * complete, or waiting for the last GPU user of a "fence register". The
316 * object then embeds a #i915_gem_active to track the most recent (in
317 * retirement order) request relevant for the desired mode of access.
318 * The #i915_gem_active is updated with i915_gem_active_set() to track the
319 * most recent fence request, typically this is done as part of
320 * i915_vma_move_to_active().
321 *
322 * When the #i915_gem_active completes (is retired), it will
323 * signal its completion to the owner through a callback as well as mark
324 * itself as idle (i915_gem_active.request == NULL). The owner
325 * can then perform any action, such as delayed freeing of an active
326 * resource including itself.
327 */
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100328struct i915_gem_active;
329
330typedef void (*i915_gem_retire_fn)(struct i915_gem_active *,
331 struct drm_i915_gem_request *);
332
Chris Wilson381f3712016-08-04 07:52:29 +0100333struct i915_gem_active {
Chris Wilson0eafec62016-08-04 16:32:41 +0100334 struct drm_i915_gem_request __rcu *request;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100335 struct list_head link;
336 i915_gem_retire_fn retire;
Chris Wilson381f3712016-08-04 07:52:29 +0100337};
338
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100339void i915_gem_retire_noop(struct i915_gem_active *,
340 struct drm_i915_gem_request *request);
341
342/**
343 * init_request_active - prepares the activity tracker for use
344 * @active - the active tracker
345 * @func - a callback when then the tracker is retired (becomes idle),
346 * can be NULL
347 *
348 * init_request_active() prepares the embedded @active struct for use as
349 * an activity tracker, that is for tracking the last known active request
350 * associated with it. When the last request becomes idle, when it is retired
351 * after completion, the optional callback @func is invoked.
352 */
353static inline void
354init_request_active(struct i915_gem_active *active,
355 i915_gem_retire_fn retire)
356{
357 INIT_LIST_HEAD(&active->link);
358 active->retire = retire ?: i915_gem_retire_noop;
359}
360
Chris Wilson27c01aa2016-08-04 07:52:30 +0100361/**
362 * i915_gem_active_set - updates the tracker to watch the current request
363 * @active - the active tracker
364 * @request - the request to watch
365 *
366 * i915_gem_active_set() watches the given @request for completion. Whilst
367 * that @request is busy, the @active reports busy. When that @request is
368 * retired, the @active tracker is updated to report idle.
369 */
Chris Wilson381f3712016-08-04 07:52:29 +0100370static inline void
371i915_gem_active_set(struct i915_gem_active *active,
372 struct drm_i915_gem_request *request)
373{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100374 list_move(&active->link, &request->active_list);
Chris Wilson0eafec62016-08-04 16:32:41 +0100375 rcu_assign_pointer(active->request, request);
Chris Wilson381f3712016-08-04 07:52:29 +0100376}
377
Chris Wilsond72d9082016-08-04 07:52:31 +0100378static inline struct drm_i915_gem_request *
379__i915_gem_active_peek(const struct i915_gem_active *active)
380{
Chris Wilson0eafec62016-08-04 16:32:41 +0100381 /* Inside the error capture (running with the driver in an unknown
382 * state), we want to bend the rules slightly (a lot).
383 *
384 * Work is in progress to make it safer, in the meantime this keeps
385 * the known issue from spamming the logs.
386 */
387 return rcu_dereference_protected(active->request, 1);
Chris Wilsond72d9082016-08-04 07:52:31 +0100388}
389
Chris Wilson27c01aa2016-08-04 07:52:30 +0100390/**
Chris Wilson385384a2016-08-09 08:37:01 +0100391 * i915_gem_active_raw - return the active request
392 * @active - the active tracker
393 *
394 * i915_gem_active_raw() returns the current request being tracked, or NULL.
395 * It does not obtain a reference on the request for the caller, so the caller
396 * must hold struct_mutex.
397 */
398static inline struct drm_i915_gem_request *
399i915_gem_active_raw(const struct i915_gem_active *active, struct mutex *mutex)
400{
401 return rcu_dereference_protected(active->request,
402 lockdep_is_held(mutex));
403}
404
405/**
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100406 * i915_gem_active_peek - report the active request being monitored
Chris Wilson27c01aa2016-08-04 07:52:30 +0100407 * @active - the active tracker
408 *
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100409 * i915_gem_active_peek() returns the current request being tracked if
410 * still active, or NULL. It does not obtain a reference on the request
411 * for the caller, so the caller must hold struct_mutex.
Chris Wilson27c01aa2016-08-04 07:52:30 +0100412 */
413static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100414i915_gem_active_peek(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100415{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100416 struct drm_i915_gem_request *request;
417
Chris Wilson385384a2016-08-09 08:37:01 +0100418 request = i915_gem_active_raw(active, mutex);
Chris Wilson0eafec62016-08-04 16:32:41 +0100419 if (!request || i915_gem_request_completed(request))
420 return NULL;
421
422 return request;
423}
424
425/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100426 * i915_gem_active_get - return a reference to the active request
427 * @active - the active tracker
428 *
429 * i915_gem_active_get() returns a reference to the active request, or NULL
430 * if the active tracker is idle. The caller must hold struct_mutex.
431 */
432static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100433i915_gem_active_get(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100434{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100435 return i915_gem_request_get(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100436}
437
438/**
Chris Wilson0eafec62016-08-04 16:32:41 +0100439 * __i915_gem_active_get_rcu - return a reference to the active request
440 * @active - the active tracker
441 *
442 * __i915_gem_active_get() returns a reference to the active request, or NULL
443 * if the active tracker is idle. The caller must hold the RCU read lock, but
444 * the returned pointer is safe to use outside of RCU.
445 */
446static inline struct drm_i915_gem_request *
447__i915_gem_active_get_rcu(const struct i915_gem_active *active)
448{
449 /* Performing a lockless retrieval of the active request is super
450 * tricky. SLAB_DESTROY_BY_RCU merely guarantees that the backing
451 * slab of request objects will not be freed whilst we hold the
452 * RCU read lock. It does not guarantee that the request itself
453 * will not be freed and then *reused*. Viz,
454 *
455 * Thread A Thread B
456 *
457 * req = active.request
458 * retire(req) -> free(req);
459 * (req is now first on the slab freelist)
460 * active.request = NULL
461 *
462 * req = new submission on a new object
463 * ref(req)
464 *
465 * To prevent the request from being reused whilst the caller
466 * uses it, we take a reference like normal. Whilst acquiring
467 * the reference we check that it is not in a destroyed state
468 * (refcnt == 0). That prevents the request being reallocated
469 * whilst the caller holds on to it. To check that the request
470 * was not reallocated as we acquired the reference we have to
471 * check that our request remains the active request across
472 * the lookup, in the same manner as a seqlock. The visibility
473 * of the pointer versus the reference counting is controlled
474 * by using RCU barriers (rcu_dereference and rcu_assign_pointer).
475 *
476 * In the middle of all that, we inspect whether the request is
477 * complete. Retiring is lazy so the request may be completed long
478 * before the active tracker is updated. Querying whether the
479 * request is complete is far cheaper (as it involves no locked
480 * instructions setting cachelines to exclusive) than acquiring
481 * the reference, so we do it first. The RCU read lock ensures the
482 * pointer dereference is valid, but does not ensure that the
483 * seqno nor HWS is the right one! However, if the request was
484 * reallocated, that means the active tracker's request was complete.
485 * If the new request is also complete, then both are and we can
486 * just report the active tracker is idle. If the new request is
487 * incomplete, then we acquire a reference on it and check that
488 * it remained the active request.
Chris Wilson5a198b82016-08-09 09:23:34 +0100489 *
490 * It is then imperative that we do not zero the request on
491 * reallocation, so that we can chase the dangling pointers!
492 * See i915_gem_request_alloc().
Chris Wilson0eafec62016-08-04 16:32:41 +0100493 */
494 do {
495 struct drm_i915_gem_request *request;
496
497 request = rcu_dereference(active->request);
498 if (!request || i915_gem_request_completed(request))
499 return NULL;
500
Daniel Vetterc75870d2016-08-22 10:55:22 +0200501 /* An especially silly compiler could decide to recompute the
502 * result of i915_gem_request_completed, more specifically
503 * re-emit the load for request->fence.seqno. A race would catch
504 * a later seqno value, which could flip the result from true to
505 * false. Which means part of the instructions below might not
506 * be executed, while later on instructions are executed. Due to
507 * barriers within the refcounting the inconsistency can't reach
508 * past the call to i915_gem_request_get_rcu, but not executing
509 * that while still executing i915_gem_request_put() creates
510 * havoc enough. Prevent this with a compiler barrier.
511 */
512 barrier();
513
Chris Wilson0eafec62016-08-04 16:32:41 +0100514 request = i915_gem_request_get_rcu(request);
515
516 /* What stops the following rcu_access_pointer() from occurring
517 * before the above i915_gem_request_get_rcu()? If we were
518 * to read the value before pausing to get the reference to
519 * the request, we may not notice a change in the active
520 * tracker.
521 *
522 * The rcu_access_pointer() is a mere compiler barrier, which
523 * means both the CPU and compiler are free to perform the
524 * memory read without constraint. The compiler only has to
525 * ensure that any operations after the rcu_access_pointer()
526 * occur afterwards in program order. This means the read may
527 * be performed earlier by an out-of-order CPU, or adventurous
528 * compiler.
529 *
530 * The atomic operation at the heart of
Chris Wilsonf54d1862016-10-25 13:00:45 +0100531 * i915_gem_request_get_rcu(), see dma_fence_get_rcu(), is
Chris Wilson0eafec62016-08-04 16:32:41 +0100532 * atomic_inc_not_zero() which is only a full memory barrier
533 * when successful. That is, if i915_gem_request_get_rcu()
534 * returns the request (and so with the reference counted
535 * incremented) then the following read for rcu_access_pointer()
536 * must occur after the atomic operation and so confirm
537 * that this request is the one currently being tracked.
Chris Wilsonedf6b762016-08-09 09:23:33 +0100538 *
539 * The corresponding write barrier is part of
540 * rcu_assign_pointer().
Chris Wilson0eafec62016-08-04 16:32:41 +0100541 */
542 if (!request || request == rcu_access_pointer(active->request))
543 return rcu_pointer_handoff(request);
544
545 i915_gem_request_put(request);
546 } while (1);
547}
548
549/**
550 * i915_gem_active_get_unlocked - return a reference to the active request
551 * @active - the active tracker
552 *
553 * i915_gem_active_get_unlocked() returns a reference to the active request,
554 * or NULL if the active tracker is idle. The reference is obtained under RCU,
555 * so no locking is required by the caller.
556 *
557 * The reference should be freed with i915_gem_request_put().
558 */
559static inline struct drm_i915_gem_request *
560i915_gem_active_get_unlocked(const struct i915_gem_active *active)
561{
562 struct drm_i915_gem_request *request;
563
564 rcu_read_lock();
565 request = __i915_gem_active_get_rcu(active);
566 rcu_read_unlock();
567
568 return request;
569}
570
571/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100572 * i915_gem_active_isset - report whether the active tracker is assigned
573 * @active - the active tracker
574 *
575 * i915_gem_active_isset() returns true if the active tracker is currently
576 * assigned to a request. Due to the lazy retiring, that request may be idle
577 * and this may report stale information.
578 */
579static inline bool
580i915_gem_active_isset(const struct i915_gem_active *active)
581{
Chris Wilson0eafec62016-08-04 16:32:41 +0100582 return rcu_access_pointer(active->request);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100583}
584
585/**
Chris Wilsond07f0e52016-10-28 13:58:44 +0100586 * i915_gem_active_wait - waits until the request is completed
Chris Wilson24676582016-08-05 10:14:06 +0100587 * @active - the active request on which to wait
Chris Wilsonea746f32016-09-09 14:11:49 +0100588 * @flags - how to wait
Chris Wilson24676582016-08-05 10:14:06 +0100589 * @timeout - how long to wait at most
590 * @rps - userspace client to charge for a waitboost
591 *
Chris Wilson2e369912016-10-28 13:58:28 +0100592 * i915_gem_active_wait() waits until the request is completed before
Chris Wilson24676582016-08-05 10:14:06 +0100593 * returning, without requiring any locks to be held. Note that it does not
594 * retire any requests before returning.
595 *
596 * This function relies on RCU in order to acquire the reference to the active
597 * request without holding any locks. See __i915_gem_active_get_rcu() for the
598 * glory details on how that is managed. Once the reference is acquired, we
599 * can then wait upon the request, and afterwards release our reference,
600 * free of any locking.
601 *
602 * This function wraps i915_wait_request(), see it for the full details on
603 * the arguments.
604 *
605 * Returns 0 if successful, or a negative error code.
606 */
607static inline int
Chris Wilson2e369912016-10-28 13:58:28 +0100608i915_gem_active_wait(const struct i915_gem_active *active, unsigned int flags)
Chris Wilson24676582016-08-05 10:14:06 +0100609{
610 struct drm_i915_gem_request *request;
Chris Wilsone95433c2016-10-28 13:58:27 +0100611 long ret = 0;
Chris Wilson24676582016-08-05 10:14:06 +0100612
613 request = i915_gem_active_get_unlocked(active);
614 if (request) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100615 ret = i915_wait_request(request, flags, MAX_SCHEDULE_TIMEOUT);
Chris Wilson24676582016-08-05 10:14:06 +0100616 i915_gem_request_put(request);
617 }
618
Chris Wilsone95433c2016-10-28 13:58:27 +0100619 return ret < 0 ? ret : 0;
Chris Wilson24676582016-08-05 10:14:06 +0100620}
621
622/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100623 * i915_gem_active_retire - waits until the request is retired
624 * @active - the active request on which to wait
625 *
626 * i915_gem_active_retire() waits until the request is completed,
627 * and then ensures that at least the retirement handler for this
628 * @active tracker is called before returning. If the @active
629 * tracker is idle, the function returns immediately.
630 */
631static inline int __must_check
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100632i915_gem_active_retire(struct i915_gem_active *active,
Chris Wilsond72d9082016-08-04 07:52:31 +0100633 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100634{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100635 struct drm_i915_gem_request *request;
Chris Wilsone95433c2016-10-28 13:58:27 +0100636 long ret;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100637
Chris Wilson385384a2016-08-09 08:37:01 +0100638 request = i915_gem_active_raw(active, mutex);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100639 if (!request)
640 return 0;
641
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100642 ret = i915_wait_request(request,
643 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
Chris Wilsone95433c2016-10-28 13:58:27 +0100644 MAX_SCHEDULE_TIMEOUT);
645 if (ret < 0)
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100646 return ret;
647
648 list_del_init(&active->link);
Chris Wilson0eafec62016-08-04 16:32:41 +0100649 RCU_INIT_POINTER(active->request, NULL);
650
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100651 active->retire(active, request);
652
653 return 0;
Chris Wilson27c01aa2016-08-04 07:52:30 +0100654}
655
Chris Wilson381f3712016-08-04 07:52:29 +0100656#define for_each_active(mask, idx) \
657 for (; mask ? idx = ffs(mask) - 1, 1 : 0; mask &= ~BIT(idx))
658
Chris Wilson05235c52016-07-20 09:21:08 +0100659#endif /* I915_GEM_REQUEST_H */