blob: 6b9e992d01debd4b6095f4dae2d50d1635782fde [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;
Chris Wilson56299fb2017-02-27 20:58:48 +000035struct drm_i915_gem_request;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +020036
Chris Wilsondcff85c2016-08-05 10:14:11 +010037struct intel_wait {
38 struct rb_node node;
39 struct task_struct *tsk;
Chris Wilson56299fb2017-02-27 20:58:48 +000040 struct drm_i915_gem_request *request;
Chris Wilsondcff85c2016-08-05 10:14:11 +010041 u32 seqno;
42};
43
44struct intel_signal_node {
45 struct rb_node node;
46 struct intel_wait wait;
47};
48
Chris Wilson52e54202016-11-14 20:41:02 +000049struct i915_dependency {
50 struct i915_priotree *signaler;
51 struct list_head signal_link;
52 struct list_head wait_link;
Chris Wilson20311bd2016-11-14 20:41:03 +000053 struct list_head dfs_link;
Chris Wilson52e54202016-11-14 20:41:02 +000054 unsigned long flags;
55#define I915_DEPENDENCY_ALLOC BIT(0)
56};
57
58/* Requests exist in a complex web of interdependencies. Each request
59 * has to wait for some other request to complete before it is ready to be run
60 * (e.g. we have to wait until the pixels have been rendering into a texture
61 * before we can copy from it). We track the readiness of a request in terms
62 * of fences, but we also need to keep the dependency tree for the lifetime
63 * of the request (beyond the life of an individual fence). We use the tree
64 * at various points to reorder the requests whilst keeping the requests
65 * in order with respect to their various dependencies.
66 */
67struct i915_priotree {
68 struct list_head signalers_list; /* those before us, we depend upon */
69 struct list_head waiters_list; /* those after us, they depend upon us */
Chris Wilson6c067572017-05-17 13:10:03 +010070 struct list_head link;
Chris Wilson20311bd2016-11-14 20:41:03 +000071 int priority;
72#define I915_PRIORITY_MAX 1024
Chris Wilsone4f815f2017-05-17 13:10:02 +010073#define I915_PRIORITY_NORMAL 0
Chris Wilson20311bd2016-11-14 20:41:03 +000074#define I915_PRIORITY_MIN (-I915_PRIORITY_MAX)
Chris Wilson7d1ea602017-09-28 20:39:00 +010075#define I915_PRIORITY_INVALID INT_MIN
Chris Wilson52e54202016-11-14 20:41:02 +000076};
77
Chris Wilsonb0fd47a2017-04-15 10:39:02 +010078struct i915_gem_capture_list {
79 struct i915_gem_capture_list *next;
80 struct i915_vma *vma;
81};
82
Chris Wilson05235c52016-07-20 09:21:08 +010083/**
84 * Request queue structure.
85 *
86 * The request queue allows us to note sequence numbers that have been emitted
87 * and may be associated with active buffers to be retired.
88 *
89 * By keeping this list, we can avoid having to do questionable sequence
90 * number comparisons on buffer last_read|write_seqno. It also allows an
91 * emission time to be associated with the request for tracking how far ahead
92 * of the GPU the submission is.
93 *
Chris Wilson5a198b82016-08-09 09:23:34 +010094 * When modifying this structure be very aware that we perform a lockless
95 * RCU lookup of it that may race against reallocation of the struct
96 * from the slab freelist. We intentionally do not zero the structure on
97 * allocation so that the lookup can use the dangling pointers (and is
98 * cogniscent that those pointers may be wrong). Instead, everything that
99 * needs to be initialised must be done so explicitly.
100 *
Chris Wilson04769652016-07-20 09:21:11 +0100101 * The requests are reference counted.
Chris Wilson05235c52016-07-20 09:21:08 +0100102 */
103struct drm_i915_gem_request {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100104 struct dma_fence fence;
Chris Wilson04769652016-07-20 09:21:11 +0100105 spinlock_t lock;
Chris Wilson05235c52016-07-20 09:21:08 +0100106
107 /** On Which ring this request was generated */
108 struct drm_i915_private *i915;
109
110 /**
111 * Context and ring buffer related to this request
112 * Contexts are refcounted, so when this request is associated with a
113 * context, we must increment the context's refcount, to guarantee that
114 * it persists while any request is linked to it. Requests themselves
115 * are also refcounted, so the request will only be freed when the last
116 * reference to it is dismissed, and the code in
117 * i915_gem_request_free() will then decrement the refcount on the
118 * context.
119 */
120 struct i915_gem_context *ctx;
121 struct intel_engine_cs *engine;
Chris Wilson7e37f882016-08-02 22:50:21 +0100122 struct intel_ring *ring;
Chris Wilson73cb9702016-10-28 13:58:46 +0100123 struct intel_timeline *timeline;
Chris Wilson05235c52016-07-20 09:21:08 +0100124 struct intel_signal_node signaling;
125
Chris Wilson23902e42016-11-14 20:40:58 +0000126 /* Fences for the various phases in the request's lifetime.
127 *
128 * The submit fence is used to await upon all of the request's
129 * dependencies. When it is signaled, the request is ready to run.
130 * It is used by the driver to then queue the request for execution.
Chris Wilson23902e42016-11-14 20:40:58 +0000131 */
Chris Wilson5590af32016-09-09 14:11:54 +0100132 struct i915_sw_fence submit;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200133 wait_queue_entry_t submitq;
Chris Wilsonfe497892017-02-23 07:44:13 +0000134 wait_queue_head_t execute;
Chris Wilson5590af32016-09-09 14:11:54 +0100135
Chris Wilson52e54202016-11-14 20:41:02 +0000136 /* A list of everyone we wait upon, and everyone who waits upon us.
137 * Even though we will not be submitted to the hardware before the
138 * submit fence is signaled (it waits for all external events as well
139 * as our own requests), the scheduler still needs to know the
140 * dependency tree for the lifetime of the request (from execbuf
141 * to retirement), i.e. bidirectional dependency information for the
142 * request not tied to individual fences.
143 */
144 struct i915_priotree priotree;
145 struct i915_dependency dep;
146
Chris Wilson754c9fd2017-02-23 07:44:14 +0000147 /** GEM sequence number associated with this request on the
148 * global execution timeline. It is zero when the request is not
149 * on the HW queue (i.e. not on the engine timeline list).
150 * Its value is guarded by the timeline spinlock.
151 */
Chris Wilson65e47602016-10-28 13:58:49 +0100152 u32 global_seqno;
153
Chris Wilsona52abd22016-09-09 14:11:43 +0100154 /** Position in the ring of the start of the request */
Chris Wilson05235c52016-07-20 09:21:08 +0100155 u32 head;
156
157 /**
Chris Wilsona52abd22016-09-09 14:11:43 +0100158 * Position in the ring of the start of the postfix.
159 * This is required to calculate the maximum available ring space
160 * without overwriting the postfix.
Chris Wilson05235c52016-07-20 09:21:08 +0100161 */
162 u32 postfix;
163
Chris Wilsona52abd22016-09-09 14:11:43 +0100164 /** Position in the ring of the end of the whole request */
Chris Wilson05235c52016-07-20 09:21:08 +0100165 u32 tail;
166
Chris Wilsona52abd22016-09-09 14:11:43 +0100167 /** Position in the ring of the end of any workarounds after the tail */
168 u32 wa_tail;
169
170 /** Preallocate space in the ring for the emitting the request */
Chris Wilson05235c52016-07-20 09:21:08 +0100171 u32 reserved_space;
172
Chris Wilson05235c52016-07-20 09:21:08 +0100173 /** Batch buffer related to this request if any (used for
174 * error state dump only).
175 */
Chris Wilson058d88c2016-08-15 10:49:06 +0100176 struct i915_vma *batch;
Chris Wilsonb0fd47a2017-04-15 10:39:02 +0100177 /** Additional buffers requested by userspace to be captured upon
178 * a GPU hang. The vma/obj on this list are protected by their
179 * active reference - all objects on this list must also be
180 * on the active_list (of their final request).
181 */
182 struct i915_gem_capture_list *capture_list;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100183 struct list_head active_list;
Chris Wilson05235c52016-07-20 09:21:08 +0100184
185 /** Time at which this request was emitted, in jiffies. */
186 unsigned long emitted_jiffies;
187
Chris Wilson7b92c1b2017-06-28 13:35:48 +0100188 bool waitboost;
189
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100190 /** engine->request_list entry for this request */
191 struct list_head link;
Chris Wilson05235c52016-07-20 09:21:08 +0100192
Chris Wilson675d9ad2016-08-04 07:52:36 +0100193 /** ring->request_list entry for this request */
194 struct list_head ring_link;
195
Chris Wilson05235c52016-07-20 09:21:08 +0100196 struct drm_i915_file_private *file_priv;
197 /** file_priv list entry for this request */
Chris Wilsonc8659ef2017-03-02 12:25:25 +0000198 struct list_head client_link;
Chris Wilson05235c52016-07-20 09:21:08 +0100199};
200
Chris Wilsonf54d1862016-10-25 13:00:45 +0100201extern const struct dma_fence_ops i915_fence_ops;
Chris Wilson04769652016-07-20 09:21:11 +0100202
Chris Wilsonb52992c2016-10-28 13:58:24 +0100203static inline bool dma_fence_is_i915(const struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +0100204{
205 return fence->ops == &i915_fence_ops;
206}
207
Chris Wilson05235c52016-07-20 09:21:08 +0100208struct drm_i915_gem_request * __must_check
209i915_gem_request_alloc(struct intel_engine_cs *engine,
210 struct i915_gem_context *ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100211void i915_gem_request_retire_upto(struct drm_i915_gem_request *req);
212
Chris Wilson05235c52016-07-20 09:21:08 +0100213static inline struct drm_i915_gem_request *
Chris Wilsonf54d1862016-10-25 13:00:45 +0100214to_request(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +0100215{
216 /* We assume that NULL fence/request are interoperable */
217 BUILD_BUG_ON(offsetof(struct drm_i915_gem_request, fence) != 0);
Chris Wilsonb52992c2016-10-28 13:58:24 +0100218 GEM_BUG_ON(fence && !dma_fence_is_i915(fence));
Chris Wilson04769652016-07-20 09:21:11 +0100219 return container_of(fence, struct drm_i915_gem_request, fence);
220}
221
222static inline struct drm_i915_gem_request *
Chris Wilsone8a261e2016-07-20 13:31:49 +0100223i915_gem_request_get(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100224{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100225 return to_request(dma_fence_get(&req->fence));
Chris Wilson05235c52016-07-20 09:21:08 +0100226}
227
Chris Wilson0eafec62016-08-04 16:32:41 +0100228static inline struct drm_i915_gem_request *
229i915_gem_request_get_rcu(struct drm_i915_gem_request *req)
230{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100231 return to_request(dma_fence_get_rcu(&req->fence));
Chris Wilson0eafec62016-08-04 16:32:41 +0100232}
233
Chris Wilson05235c52016-07-20 09:21:08 +0100234static inline void
Chris Wilsone8a261e2016-07-20 13:31:49 +0100235i915_gem_request_put(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100236{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100237 dma_fence_put(&req->fence);
Chris Wilson05235c52016-07-20 09:21:08 +0100238}
239
240static inline void i915_gem_request_assign(struct drm_i915_gem_request **pdst,
241 struct drm_i915_gem_request *src)
242{
243 if (src)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100244 i915_gem_request_get(src);
Chris Wilson05235c52016-07-20 09:21:08 +0100245
246 if (*pdst)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100247 i915_gem_request_put(*pdst);
Chris Wilson05235c52016-07-20 09:21:08 +0100248
249 *pdst = src;
250}
251
Chris Wilson754c9fd2017-02-23 07:44:14 +0000252/**
253 * i915_gem_request_global_seqno - report the current global seqno
254 * @request - the request
255 *
256 * A request is assigned a global seqno only when it is on the hardware
257 * execution queue. The global seqno can be used to maintain a list of
258 * requests on the same engine in retirement order, for example for
259 * constructing a priority queue for waiting. Prior to its execution, or
260 * if it is subsequently removed in the event of preemption, its global
261 * seqno is zero. As both insertion and removal from the execution queue
262 * may operate in IRQ context, it is not guarded by the usual struct_mutex
263 * BKL. Instead those relying on the global seqno must be prepared for its
264 * value to change between reads. Only when the request is complete can
265 * the global seqno be stable (due to the memory barriers on submitting
266 * the commands to the hardware to write the breadcrumb, if the HWS shows
267 * that it has passed the global seqno and the global seqno is unchanged
268 * after the read, it is indeed complete).
269 */
270static u32
271i915_gem_request_global_seqno(const struct drm_i915_gem_request *request)
272{
273 return READ_ONCE(request->global_seqno);
274}
275
Chris Wilsona2bc4692016-09-09 14:11:56 +0100276int
277i915_gem_request_await_object(struct drm_i915_gem_request *to,
278 struct drm_i915_gem_object *obj,
279 bool write);
Chris Wilsonb52992c2016-10-28 13:58:24 +0100280int i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
281 struct dma_fence *fence);
Chris Wilsona2bc4692016-09-09 14:11:56 +0100282
Chris Wilson17f298cf2016-08-10 13:41:46 +0100283void __i915_add_request(struct drm_i915_gem_request *req, bool flush_caches);
Chris Wilson05235c52016-07-20 09:21:08 +0100284#define i915_add_request(req) \
Chris Wilson17f298cf2016-08-10 13:41:46 +0100285 __i915_add_request(req, false)
Chris Wilson05235c52016-07-20 09:21:08 +0100286
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000287void __i915_gem_request_submit(struct drm_i915_gem_request *request);
288void i915_gem_request_submit(struct drm_i915_gem_request *request);
289
Chris Wilsond6a22892017-02-23 07:44:17 +0000290void __i915_gem_request_unsubmit(struct drm_i915_gem_request *request);
291void i915_gem_request_unsubmit(struct drm_i915_gem_request *request);
292
Chris Wilson05235c52016-07-20 09:21:08 +0100293struct intel_rps_client;
Chris Wilson42df2712016-07-20 09:21:12 +0100294#define NO_WAITBOOST ERR_PTR(-1)
295#define IS_RPS_CLIENT(p) (!IS_ERR(p))
296#define IS_RPS_USER(p) (!IS_ERR_OR_NULL(p))
Chris Wilson05235c52016-07-20 09:21:08 +0100297
Chris Wilsone95433c2016-10-28 13:58:27 +0100298long i915_wait_request(struct drm_i915_gem_request *req,
299 unsigned int flags,
300 long timeout)
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100301 __attribute__((nonnull(1)));
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100302#define I915_WAIT_INTERRUPTIBLE BIT(0)
303#define I915_WAIT_LOCKED BIT(1) /* struct_mutex held, handle GPU reset */
Chris Wilsone95433c2016-10-28 13:58:27 +0100304#define I915_WAIT_ALL BIT(2) /* used by i915_gem_object_wait() */
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100305
Chris Wilson05235c52016-07-20 09:21:08 +0100306static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine);
307
308/**
309 * Returns true if seq1 is later than seq2.
310 */
311static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
312{
313 return (s32)(seq1 - seq2) >= 0;
314}
315
316static inline bool
Chris Wilson754c9fd2017-02-23 07:44:14 +0000317__i915_gem_request_completed(const struct drm_i915_gem_request *req, u32 seqno)
Chris Wilson65e47602016-10-28 13:58:49 +0100318{
Chris Wilson754c9fd2017-02-23 07:44:14 +0000319 GEM_BUG_ON(!seqno);
320 return i915_seqno_passed(intel_engine_get_seqno(req->engine), seqno) &&
321 seqno == i915_gem_request_global_seqno(req);
Chris Wilson65e47602016-10-28 13:58:49 +0100322}
323
324static inline bool
Chris Wilson05235c52016-07-20 09:21:08 +0100325i915_gem_request_completed(const struct drm_i915_gem_request *req)
326{
Chris Wilson754c9fd2017-02-23 07:44:14 +0000327 u32 seqno;
328
329 seqno = i915_gem_request_global_seqno(req);
330 if (!seqno)
Chris Wilson65e47602016-10-28 13:58:49 +0100331 return false;
332
Chris Wilson754c9fd2017-02-23 07:44:14 +0000333 return __i915_gem_request_completed(req, seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100334}
335
Chris Wilson381f3712016-08-04 07:52:29 +0100336/* We treat requests as fences. This is not be to confused with our
337 * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
338 * We use the fences to synchronize access from the CPU with activity on the
339 * GPU, for example, we should not rewrite an object's PTE whilst the GPU
340 * is reading them. We also track fences at a higher level to provide
341 * implicit synchronisation around GEM objects, e.g. set-domain will wait
342 * for outstanding GPU rendering before marking the object ready for CPU
343 * access, or a pageflip will wait until the GPU is complete before showing
344 * the frame on the scanout.
345 *
346 * In order to use a fence, the object must track the fence it needs to
347 * serialise with. For example, GEM objects want to track both read and
348 * write access so that we can perform concurrent read operations between
349 * the CPU and GPU engines, as well as waiting for all rendering to
350 * complete, or waiting for the last GPU user of a "fence register". The
351 * object then embeds a #i915_gem_active to track the most recent (in
352 * retirement order) request relevant for the desired mode of access.
353 * The #i915_gem_active is updated with i915_gem_active_set() to track the
354 * most recent fence request, typically this is done as part of
355 * i915_vma_move_to_active().
356 *
357 * When the #i915_gem_active completes (is retired), it will
358 * signal its completion to the owner through a callback as well as mark
359 * itself as idle (i915_gem_active.request == NULL). The owner
360 * can then perform any action, such as delayed freeing of an active
361 * resource including itself.
362 */
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100363struct i915_gem_active;
364
365typedef void (*i915_gem_retire_fn)(struct i915_gem_active *,
366 struct drm_i915_gem_request *);
367
Chris Wilson381f3712016-08-04 07:52:29 +0100368struct i915_gem_active {
Chris Wilson0eafec62016-08-04 16:32:41 +0100369 struct drm_i915_gem_request __rcu *request;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100370 struct list_head link;
371 i915_gem_retire_fn retire;
Chris Wilson381f3712016-08-04 07:52:29 +0100372};
373
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100374void i915_gem_retire_noop(struct i915_gem_active *,
375 struct drm_i915_gem_request *request);
376
377/**
378 * init_request_active - prepares the activity tracker for use
379 * @active - the active tracker
380 * @func - a callback when then the tracker is retired (becomes idle),
381 * can be NULL
382 *
383 * init_request_active() prepares the embedded @active struct for use as
384 * an activity tracker, that is for tracking the last known active request
385 * associated with it. When the last request becomes idle, when it is retired
386 * after completion, the optional callback @func is invoked.
387 */
388static inline void
389init_request_active(struct i915_gem_active *active,
390 i915_gem_retire_fn retire)
391{
392 INIT_LIST_HEAD(&active->link);
393 active->retire = retire ?: i915_gem_retire_noop;
394}
395
Chris Wilson27c01aa2016-08-04 07:52:30 +0100396/**
397 * i915_gem_active_set - updates the tracker to watch the current request
398 * @active - the active tracker
399 * @request - the request to watch
400 *
401 * i915_gem_active_set() watches the given @request for completion. Whilst
402 * that @request is busy, the @active reports busy. When that @request is
403 * retired, the @active tracker is updated to report idle.
404 */
Chris Wilson381f3712016-08-04 07:52:29 +0100405static inline void
406i915_gem_active_set(struct i915_gem_active *active,
407 struct drm_i915_gem_request *request)
408{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100409 list_move(&active->link, &request->active_list);
Chris Wilson0eafec62016-08-04 16:32:41 +0100410 rcu_assign_pointer(active->request, request);
Chris Wilson381f3712016-08-04 07:52:29 +0100411}
412
Ville Syrjäläecd9caa02016-12-07 17:56:47 +0000413/**
414 * i915_gem_active_set_retire_fn - updates the retirement callback
415 * @active - the active tracker
416 * @fn - the routine called when the request is retired
417 * @mutex - struct_mutex used to guard retirements
418 *
419 * i915_gem_active_set_retire_fn() updates the function pointer that
420 * is called when the final request associated with the @active tracker
421 * is retired.
422 */
423static inline void
424i915_gem_active_set_retire_fn(struct i915_gem_active *active,
425 i915_gem_retire_fn fn,
426 struct mutex *mutex)
427{
428 lockdep_assert_held(mutex);
429 active->retire = fn ?: i915_gem_retire_noop;
430}
431
Chris Wilsond72d9082016-08-04 07:52:31 +0100432static inline struct drm_i915_gem_request *
433__i915_gem_active_peek(const struct i915_gem_active *active)
434{
Chris Wilson0eafec62016-08-04 16:32:41 +0100435 /* Inside the error capture (running with the driver in an unknown
436 * state), we want to bend the rules slightly (a lot).
437 *
438 * Work is in progress to make it safer, in the meantime this keeps
439 * the known issue from spamming the logs.
440 */
441 return rcu_dereference_protected(active->request, 1);
Chris Wilsond72d9082016-08-04 07:52:31 +0100442}
443
Chris Wilson27c01aa2016-08-04 07:52:30 +0100444/**
Chris Wilson385384a2016-08-09 08:37:01 +0100445 * i915_gem_active_raw - return the active request
446 * @active - the active tracker
447 *
448 * i915_gem_active_raw() returns the current request being tracked, or NULL.
449 * It does not obtain a reference on the request for the caller, so the caller
450 * must hold struct_mutex.
451 */
452static inline struct drm_i915_gem_request *
453i915_gem_active_raw(const struct i915_gem_active *active, struct mutex *mutex)
454{
455 return rcu_dereference_protected(active->request,
456 lockdep_is_held(mutex));
457}
458
459/**
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100460 * i915_gem_active_peek - report the active request being monitored
Chris Wilson27c01aa2016-08-04 07:52:30 +0100461 * @active - the active tracker
462 *
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100463 * i915_gem_active_peek() returns the current request being tracked if
464 * still active, or NULL. It does not obtain a reference on the request
465 * for the caller, so the caller must hold struct_mutex.
Chris Wilson27c01aa2016-08-04 07:52:30 +0100466 */
467static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100468i915_gem_active_peek(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100469{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100470 struct drm_i915_gem_request *request;
471
Chris Wilson385384a2016-08-09 08:37:01 +0100472 request = i915_gem_active_raw(active, mutex);
Chris Wilson0eafec62016-08-04 16:32:41 +0100473 if (!request || i915_gem_request_completed(request))
474 return NULL;
475
476 return request;
477}
478
479/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100480 * i915_gem_active_get - return a reference to the active request
481 * @active - the active tracker
482 *
483 * i915_gem_active_get() returns a reference to the active request, or NULL
484 * if the active tracker is idle. The caller must hold struct_mutex.
485 */
486static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100487i915_gem_active_get(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100488{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100489 return i915_gem_request_get(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100490}
491
492/**
Chris Wilson0eafec62016-08-04 16:32:41 +0100493 * __i915_gem_active_get_rcu - return a reference to the active request
494 * @active - the active tracker
495 *
496 * __i915_gem_active_get() returns a reference to the active request, or NULL
497 * if the active tracker is idle. The caller must hold the RCU read lock, but
498 * the returned pointer is safe to use outside of RCU.
499 */
500static inline struct drm_i915_gem_request *
501__i915_gem_active_get_rcu(const struct i915_gem_active *active)
502{
503 /* Performing a lockless retrieval of the active request is super
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800504 * tricky. SLAB_TYPESAFE_BY_RCU merely guarantees that the backing
Chris Wilson0eafec62016-08-04 16:32:41 +0100505 * slab of request objects will not be freed whilst we hold the
506 * RCU read lock. It does not guarantee that the request itself
507 * will not be freed and then *reused*. Viz,
508 *
509 * Thread A Thread B
510 *
511 * req = active.request
512 * retire(req) -> free(req);
513 * (req is now first on the slab freelist)
514 * active.request = NULL
515 *
516 * req = new submission on a new object
517 * ref(req)
518 *
519 * To prevent the request from being reused whilst the caller
520 * uses it, we take a reference like normal. Whilst acquiring
521 * the reference we check that it is not in a destroyed state
522 * (refcnt == 0). That prevents the request being reallocated
523 * whilst the caller holds on to it. To check that the request
524 * was not reallocated as we acquired the reference we have to
525 * check that our request remains the active request across
526 * the lookup, in the same manner as a seqlock. The visibility
527 * of the pointer versus the reference counting is controlled
528 * by using RCU barriers (rcu_dereference and rcu_assign_pointer).
529 *
530 * In the middle of all that, we inspect whether the request is
531 * complete. Retiring is lazy so the request may be completed long
532 * before the active tracker is updated. Querying whether the
533 * request is complete is far cheaper (as it involves no locked
534 * instructions setting cachelines to exclusive) than acquiring
535 * the reference, so we do it first. The RCU read lock ensures the
536 * pointer dereference is valid, but does not ensure that the
537 * seqno nor HWS is the right one! However, if the request was
538 * reallocated, that means the active tracker's request was complete.
539 * If the new request is also complete, then both are and we can
540 * just report the active tracker is idle. If the new request is
541 * incomplete, then we acquire a reference on it and check that
542 * it remained the active request.
Chris Wilson5a198b82016-08-09 09:23:34 +0100543 *
544 * It is then imperative that we do not zero the request on
545 * reallocation, so that we can chase the dangling pointers!
546 * See i915_gem_request_alloc().
Chris Wilson0eafec62016-08-04 16:32:41 +0100547 */
548 do {
549 struct drm_i915_gem_request *request;
550
551 request = rcu_dereference(active->request);
552 if (!request || i915_gem_request_completed(request))
553 return NULL;
554
Daniel Vetterc75870d2016-08-22 10:55:22 +0200555 /* An especially silly compiler could decide to recompute the
556 * result of i915_gem_request_completed, more specifically
557 * re-emit the load for request->fence.seqno. A race would catch
558 * a later seqno value, which could flip the result from true to
559 * false. Which means part of the instructions below might not
560 * be executed, while later on instructions are executed. Due to
561 * barriers within the refcounting the inconsistency can't reach
562 * past the call to i915_gem_request_get_rcu, but not executing
563 * that while still executing i915_gem_request_put() creates
564 * havoc enough. Prevent this with a compiler barrier.
565 */
566 barrier();
567
Chris Wilson0eafec62016-08-04 16:32:41 +0100568 request = i915_gem_request_get_rcu(request);
569
570 /* What stops the following rcu_access_pointer() from occurring
571 * before the above i915_gem_request_get_rcu()? If we were
572 * to read the value before pausing to get the reference to
573 * the request, we may not notice a change in the active
574 * tracker.
575 *
576 * The rcu_access_pointer() is a mere compiler barrier, which
577 * means both the CPU and compiler are free to perform the
578 * memory read without constraint. The compiler only has to
579 * ensure that any operations after the rcu_access_pointer()
580 * occur afterwards in program order. This means the read may
581 * be performed earlier by an out-of-order CPU, or adventurous
582 * compiler.
583 *
584 * The atomic operation at the heart of
Chris Wilsonf54d1862016-10-25 13:00:45 +0100585 * i915_gem_request_get_rcu(), see dma_fence_get_rcu(), is
Chris Wilson0eafec62016-08-04 16:32:41 +0100586 * atomic_inc_not_zero() which is only a full memory barrier
587 * when successful. That is, if i915_gem_request_get_rcu()
588 * returns the request (and so with the reference counted
589 * incremented) then the following read for rcu_access_pointer()
590 * must occur after the atomic operation and so confirm
591 * that this request is the one currently being tracked.
Chris Wilsonedf6b762016-08-09 09:23:33 +0100592 *
593 * The corresponding write barrier is part of
594 * rcu_assign_pointer().
Chris Wilson0eafec62016-08-04 16:32:41 +0100595 */
596 if (!request || request == rcu_access_pointer(active->request))
597 return rcu_pointer_handoff(request);
598
599 i915_gem_request_put(request);
600 } while (1);
601}
602
603/**
604 * i915_gem_active_get_unlocked - return a reference to the active request
605 * @active - the active tracker
606 *
607 * i915_gem_active_get_unlocked() returns a reference to the active request,
608 * or NULL if the active tracker is idle. The reference is obtained under RCU,
609 * so no locking is required by the caller.
610 *
611 * The reference should be freed with i915_gem_request_put().
612 */
613static inline struct drm_i915_gem_request *
614i915_gem_active_get_unlocked(const struct i915_gem_active *active)
615{
616 struct drm_i915_gem_request *request;
617
618 rcu_read_lock();
619 request = __i915_gem_active_get_rcu(active);
620 rcu_read_unlock();
621
622 return request;
623}
624
625/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100626 * i915_gem_active_isset - report whether the active tracker is assigned
627 * @active - the active tracker
628 *
629 * i915_gem_active_isset() returns true if the active tracker is currently
630 * assigned to a request. Due to the lazy retiring, that request may be idle
631 * and this may report stale information.
632 */
633static inline bool
634i915_gem_active_isset(const struct i915_gem_active *active)
635{
Chris Wilson0eafec62016-08-04 16:32:41 +0100636 return rcu_access_pointer(active->request);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100637}
638
639/**
Chris Wilsond07f0e52016-10-28 13:58:44 +0100640 * i915_gem_active_wait - waits until the request is completed
Chris Wilson24676582016-08-05 10:14:06 +0100641 * @active - the active request on which to wait
Chris Wilsonea746f32016-09-09 14:11:49 +0100642 * @flags - how to wait
Chris Wilson24676582016-08-05 10:14:06 +0100643 * @timeout - how long to wait at most
644 * @rps - userspace client to charge for a waitboost
645 *
Chris Wilson2e369912016-10-28 13:58:28 +0100646 * i915_gem_active_wait() waits until the request is completed before
Chris Wilson24676582016-08-05 10:14:06 +0100647 * returning, without requiring any locks to be held. Note that it does not
648 * retire any requests before returning.
649 *
650 * This function relies on RCU in order to acquire the reference to the active
651 * request without holding any locks. See __i915_gem_active_get_rcu() for the
652 * glory details on how that is managed. Once the reference is acquired, we
653 * can then wait upon the request, and afterwards release our reference,
654 * free of any locking.
655 *
656 * This function wraps i915_wait_request(), see it for the full details on
657 * the arguments.
658 *
659 * Returns 0 if successful, or a negative error code.
660 */
661static inline int
Chris Wilson2e369912016-10-28 13:58:28 +0100662i915_gem_active_wait(const struct i915_gem_active *active, unsigned int flags)
Chris Wilson24676582016-08-05 10:14:06 +0100663{
664 struct drm_i915_gem_request *request;
Chris Wilsone95433c2016-10-28 13:58:27 +0100665 long ret = 0;
Chris Wilson24676582016-08-05 10:14:06 +0100666
667 request = i915_gem_active_get_unlocked(active);
668 if (request) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100669 ret = i915_wait_request(request, flags, MAX_SCHEDULE_TIMEOUT);
Chris Wilson24676582016-08-05 10:14:06 +0100670 i915_gem_request_put(request);
671 }
672
Chris Wilsone95433c2016-10-28 13:58:27 +0100673 return ret < 0 ? ret : 0;
Chris Wilson24676582016-08-05 10:14:06 +0100674}
675
676/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100677 * i915_gem_active_retire - waits until the request is retired
678 * @active - the active request on which to wait
679 *
680 * i915_gem_active_retire() waits until the request is completed,
681 * and then ensures that at least the retirement handler for this
682 * @active tracker is called before returning. If the @active
683 * tracker is idle, the function returns immediately.
684 */
685static inline int __must_check
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100686i915_gem_active_retire(struct i915_gem_active *active,
Chris Wilsond72d9082016-08-04 07:52:31 +0100687 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100688{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100689 struct drm_i915_gem_request *request;
Chris Wilsone95433c2016-10-28 13:58:27 +0100690 long ret;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100691
Chris Wilson385384a2016-08-09 08:37:01 +0100692 request = i915_gem_active_raw(active, mutex);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100693 if (!request)
694 return 0;
695
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100696 ret = i915_wait_request(request,
697 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
Chris Wilsone95433c2016-10-28 13:58:27 +0100698 MAX_SCHEDULE_TIMEOUT);
699 if (ret < 0)
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100700 return ret;
701
702 list_del_init(&active->link);
Chris Wilson0eafec62016-08-04 16:32:41 +0100703 RCU_INIT_POINTER(active->request, NULL);
704
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100705 active->retire(active, request);
706
707 return 0;
Chris Wilson27c01aa2016-08-04 07:52:30 +0100708}
709
Chris Wilson381f3712016-08-04 07:52:29 +0100710#define for_each_active(mask, idx) \
711 for (; mask ? idx = ffs(mask) - 1, 1 : 0; mask &= ~BIT(idx))
712
Chris Wilson05235c52016-07-20 09:21:08 +0100713#endif /* I915_GEM_REQUEST_H */