blob: 0d6d39f19506d9cc71d9c228b34708bcef8567be [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
Chris Wilsonac14fbd2017-10-03 21:34:53 +010033#include <uapi/drm/i915_drm.h>
34
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +020035struct drm_file;
36struct drm_i915_gem_object;
Chris Wilson56299fb2017-02-27 20:58:48 +000037struct drm_i915_gem_request;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +020038
Chris Wilsondcff85c2016-08-05 10:14:11 +010039struct intel_wait {
40 struct rb_node node;
41 struct task_struct *tsk;
Chris Wilson56299fb2017-02-27 20:58:48 +000042 struct drm_i915_gem_request *request;
Chris Wilsondcff85c2016-08-05 10:14:11 +010043 u32 seqno;
44};
45
46struct intel_signal_node {
47 struct rb_node node;
48 struct intel_wait wait;
49};
50
Chris Wilson52e54202016-11-14 20:41:02 +000051struct i915_dependency {
52 struct i915_priotree *signaler;
53 struct list_head signal_link;
54 struct list_head wait_link;
Chris Wilson20311bd2016-11-14 20:41:03 +000055 struct list_head dfs_link;
Chris Wilson52e54202016-11-14 20:41:02 +000056 unsigned long flags;
57#define I915_DEPENDENCY_ALLOC BIT(0)
58};
59
60/* Requests exist in a complex web of interdependencies. Each request
61 * has to wait for some other request to complete before it is ready to be run
62 * (e.g. we have to wait until the pixels have been rendering into a texture
63 * before we can copy from it). We track the readiness of a request in terms
64 * of fences, but we also need to keep the dependency tree for the lifetime
65 * of the request (beyond the life of an individual fence). We use the tree
66 * at various points to reorder the requests whilst keeping the requests
67 * in order with respect to their various dependencies.
68 */
69struct i915_priotree {
70 struct list_head signalers_list; /* those before us, we depend upon */
71 struct list_head waiters_list; /* those after us, they depend upon us */
Chris Wilson6c067572017-05-17 13:10:03 +010072 struct list_head link;
Chris Wilson20311bd2016-11-14 20:41:03 +000073 int priority;
Chris Wilsonac14fbd2017-10-03 21:34:53 +010074};
75
76enum {
77 I915_PRIORITY_MIN = I915_CONTEXT_MIN_USER_PRIORITY - 1,
78 I915_PRIORITY_NORMAL = I915_CONTEXT_DEFAULT_PRIORITY,
79 I915_PRIORITY_MAX = I915_CONTEXT_MAX_USER_PRIORITY + 1,
80
81 I915_PRIORITY_INVALID = INT_MIN
Chris Wilson52e54202016-11-14 20:41:02 +000082};
83
Chris Wilsonb0fd47a2017-04-15 10:39:02 +010084struct i915_gem_capture_list {
85 struct i915_gem_capture_list *next;
86 struct i915_vma *vma;
87};
88
Chris Wilson05235c52016-07-20 09:21:08 +010089/**
90 * Request queue structure.
91 *
92 * The request queue allows us to note sequence numbers that have been emitted
93 * and may be associated with active buffers to be retired.
94 *
95 * By keeping this list, we can avoid having to do questionable sequence
96 * number comparisons on buffer last_read|write_seqno. It also allows an
97 * emission time to be associated with the request for tracking how far ahead
98 * of the GPU the submission is.
99 *
Chris Wilson5a198b82016-08-09 09:23:34 +0100100 * When modifying this structure be very aware that we perform a lockless
101 * RCU lookup of it that may race against reallocation of the struct
102 * from the slab freelist. We intentionally do not zero the structure on
103 * allocation so that the lookup can use the dangling pointers (and is
104 * cogniscent that those pointers may be wrong). Instead, everything that
105 * needs to be initialised must be done so explicitly.
106 *
Chris Wilson04769652016-07-20 09:21:11 +0100107 * The requests are reference counted.
Chris Wilson05235c52016-07-20 09:21:08 +0100108 */
109struct drm_i915_gem_request {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100110 struct dma_fence fence;
Chris Wilson04769652016-07-20 09:21:11 +0100111 spinlock_t lock;
Chris Wilson05235c52016-07-20 09:21:08 +0100112
113 /** On Which ring this request was generated */
114 struct drm_i915_private *i915;
115
116 /**
117 * Context and ring buffer related to this request
118 * Contexts are refcounted, so when this request is associated with a
119 * context, we must increment the context's refcount, to guarantee that
120 * it persists while any request is linked to it. Requests themselves
121 * are also refcounted, so the request will only be freed when the last
122 * reference to it is dismissed, and the code in
123 * i915_gem_request_free() will then decrement the refcount on the
124 * context.
125 */
126 struct i915_gem_context *ctx;
127 struct intel_engine_cs *engine;
Chris Wilson7e37f882016-08-02 22:50:21 +0100128 struct intel_ring *ring;
Chris Wilson73cb9702016-10-28 13:58:46 +0100129 struct intel_timeline *timeline;
Chris Wilson05235c52016-07-20 09:21:08 +0100130 struct intel_signal_node signaling;
131
Chris Wilson23902e42016-11-14 20:40:58 +0000132 /* Fences for the various phases in the request's lifetime.
133 *
134 * The submit fence is used to await upon all of the request's
135 * dependencies. When it is signaled, the request is ready to run.
136 * It is used by the driver to then queue the request for execution.
Chris Wilson23902e42016-11-14 20:40:58 +0000137 */
Chris Wilson5590af32016-09-09 14:11:54 +0100138 struct i915_sw_fence submit;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200139 wait_queue_entry_t submitq;
Chris Wilsonfe497892017-02-23 07:44:13 +0000140 wait_queue_head_t execute;
Chris Wilson5590af32016-09-09 14:11:54 +0100141
Chris Wilson52e54202016-11-14 20:41:02 +0000142 /* A list of everyone we wait upon, and everyone who waits upon us.
143 * Even though we will not be submitted to the hardware before the
144 * submit fence is signaled (it waits for all external events as well
145 * as our own requests), the scheduler still needs to know the
146 * dependency tree for the lifetime of the request (from execbuf
147 * to retirement), i.e. bidirectional dependency information for the
148 * request not tied to individual fences.
149 */
150 struct i915_priotree priotree;
151 struct i915_dependency dep;
152
Chris Wilson754c9fd2017-02-23 07:44:14 +0000153 /** GEM sequence number associated with this request on the
154 * global execution timeline. It is zero when the request is not
155 * on the HW queue (i.e. not on the engine timeline list).
156 * Its value is guarded by the timeline spinlock.
157 */
Chris Wilson65e47602016-10-28 13:58:49 +0100158 u32 global_seqno;
159
Chris Wilsona52abd22016-09-09 14:11:43 +0100160 /** Position in the ring of the start of the request */
Chris Wilson05235c52016-07-20 09:21:08 +0100161 u32 head;
162
163 /**
Chris Wilsona52abd22016-09-09 14:11:43 +0100164 * Position in the ring of the start of the postfix.
165 * This is required to calculate the maximum available ring space
166 * without overwriting the postfix.
Chris Wilson05235c52016-07-20 09:21:08 +0100167 */
168 u32 postfix;
169
Chris Wilsona52abd22016-09-09 14:11:43 +0100170 /** Position in the ring of the end of the whole request */
Chris Wilson05235c52016-07-20 09:21:08 +0100171 u32 tail;
172
Chris Wilsona52abd22016-09-09 14:11:43 +0100173 /** Position in the ring of the end of any workarounds after the tail */
174 u32 wa_tail;
175
176 /** Preallocate space in the ring for the emitting the request */
Chris Wilson05235c52016-07-20 09:21:08 +0100177 u32 reserved_space;
178
Chris Wilson05235c52016-07-20 09:21:08 +0100179 /** Batch buffer related to this request if any (used for
180 * error state dump only).
181 */
Chris Wilson058d88c2016-08-15 10:49:06 +0100182 struct i915_vma *batch;
Chris Wilsonb0fd47a2017-04-15 10:39:02 +0100183 /** Additional buffers requested by userspace to be captured upon
184 * a GPU hang. The vma/obj on this list are protected by their
185 * active reference - all objects on this list must also be
186 * on the active_list (of their final request).
187 */
188 struct i915_gem_capture_list *capture_list;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100189 struct list_head active_list;
Chris Wilson05235c52016-07-20 09:21:08 +0100190
191 /** Time at which this request was emitted, in jiffies. */
192 unsigned long emitted_jiffies;
193
Chris Wilson7b92c1b2017-06-28 13:35:48 +0100194 bool waitboost;
195
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100196 /** engine->request_list entry for this request */
197 struct list_head link;
Chris Wilson05235c52016-07-20 09:21:08 +0100198
Chris Wilson675d9ad2016-08-04 07:52:36 +0100199 /** ring->request_list entry for this request */
200 struct list_head ring_link;
201
Chris Wilson05235c52016-07-20 09:21:08 +0100202 struct drm_i915_file_private *file_priv;
203 /** file_priv list entry for this request */
Chris Wilsonc8659ef2017-03-02 12:25:25 +0000204 struct list_head client_link;
Chris Wilson05235c52016-07-20 09:21:08 +0100205};
206
Chris Wilson2abe2f82017-12-12 18:06:51 +0000207#define I915_FENCE_GFP (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN)
208
Chris Wilsonf54d1862016-10-25 13:00:45 +0100209extern const struct dma_fence_ops i915_fence_ops;
Chris Wilson04769652016-07-20 09:21:11 +0100210
Chris Wilsonb52992c2016-10-28 13:58:24 +0100211static inline bool dma_fence_is_i915(const struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +0100212{
213 return fence->ops == &i915_fence_ops;
214}
215
Chris Wilson05235c52016-07-20 09:21:08 +0100216struct drm_i915_gem_request * __must_check
217i915_gem_request_alloc(struct intel_engine_cs *engine,
218 struct i915_gem_context *ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100219void i915_gem_request_retire_upto(struct drm_i915_gem_request *req);
220
Chris Wilson05235c52016-07-20 09:21:08 +0100221static inline struct drm_i915_gem_request *
Chris Wilsonf54d1862016-10-25 13:00:45 +0100222to_request(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +0100223{
224 /* We assume that NULL fence/request are interoperable */
225 BUILD_BUG_ON(offsetof(struct drm_i915_gem_request, fence) != 0);
Chris Wilsonb52992c2016-10-28 13:58:24 +0100226 GEM_BUG_ON(fence && !dma_fence_is_i915(fence));
Chris Wilson04769652016-07-20 09:21:11 +0100227 return container_of(fence, struct drm_i915_gem_request, fence);
228}
229
230static inline struct drm_i915_gem_request *
Chris Wilsone8a261e2016-07-20 13:31:49 +0100231i915_gem_request_get(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100232{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100233 return to_request(dma_fence_get(&req->fence));
Chris Wilson05235c52016-07-20 09:21:08 +0100234}
235
Chris Wilson0eafec62016-08-04 16:32:41 +0100236static inline struct drm_i915_gem_request *
237i915_gem_request_get_rcu(struct drm_i915_gem_request *req)
238{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100239 return to_request(dma_fence_get_rcu(&req->fence));
Chris Wilson0eafec62016-08-04 16:32:41 +0100240}
241
Chris Wilson05235c52016-07-20 09:21:08 +0100242static inline void
Chris Wilsone8a261e2016-07-20 13:31:49 +0100243i915_gem_request_put(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100244{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100245 dma_fence_put(&req->fence);
Chris Wilson05235c52016-07-20 09:21:08 +0100246}
247
248static inline void i915_gem_request_assign(struct drm_i915_gem_request **pdst,
249 struct drm_i915_gem_request *src)
250{
251 if (src)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100252 i915_gem_request_get(src);
Chris Wilson05235c52016-07-20 09:21:08 +0100253
254 if (*pdst)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100255 i915_gem_request_put(*pdst);
Chris Wilson05235c52016-07-20 09:21:08 +0100256
257 *pdst = src;
258}
259
Chris Wilson754c9fd2017-02-23 07:44:14 +0000260/**
261 * i915_gem_request_global_seqno - report the current global seqno
262 * @request - the request
263 *
264 * A request is assigned a global seqno only when it is on the hardware
265 * execution queue. The global seqno can be used to maintain a list of
266 * requests on the same engine in retirement order, for example for
267 * constructing a priority queue for waiting. Prior to its execution, or
268 * if it is subsequently removed in the event of preemption, its global
269 * seqno is zero. As both insertion and removal from the execution queue
270 * may operate in IRQ context, it is not guarded by the usual struct_mutex
271 * BKL. Instead those relying on the global seqno must be prepared for its
272 * value to change between reads. Only when the request is complete can
273 * the global seqno be stable (due to the memory barriers on submitting
274 * the commands to the hardware to write the breadcrumb, if the HWS shows
275 * that it has passed the global seqno and the global seqno is unchanged
276 * after the read, it is indeed complete).
277 */
278static u32
279i915_gem_request_global_seqno(const struct drm_i915_gem_request *request)
280{
281 return READ_ONCE(request->global_seqno);
282}
283
Chris Wilsona2bc4692016-09-09 14:11:56 +0100284int
285i915_gem_request_await_object(struct drm_i915_gem_request *to,
286 struct drm_i915_gem_object *obj,
287 bool write);
Chris Wilsonb52992c2016-10-28 13:58:24 +0100288int i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
289 struct dma_fence *fence);
Chris Wilsona2bc4692016-09-09 14:11:56 +0100290
Chris Wilson17f298cf2016-08-10 13:41:46 +0100291void __i915_add_request(struct drm_i915_gem_request *req, bool flush_caches);
Chris Wilson05235c52016-07-20 09:21:08 +0100292#define i915_add_request(req) \
Chris Wilson17f298cf2016-08-10 13:41:46 +0100293 __i915_add_request(req, false)
Chris Wilson05235c52016-07-20 09:21:08 +0100294
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000295void __i915_gem_request_submit(struct drm_i915_gem_request *request);
296void i915_gem_request_submit(struct drm_i915_gem_request *request);
297
Chris Wilsond6a22892017-02-23 07:44:17 +0000298void __i915_gem_request_unsubmit(struct drm_i915_gem_request *request);
299void i915_gem_request_unsubmit(struct drm_i915_gem_request *request);
300
Chris Wilson05235c52016-07-20 09:21:08 +0100301struct intel_rps_client;
Chris Wilson42df2712016-07-20 09:21:12 +0100302#define NO_WAITBOOST ERR_PTR(-1)
303#define IS_RPS_CLIENT(p) (!IS_ERR(p))
304#define IS_RPS_USER(p) (!IS_ERR_OR_NULL(p))
Chris Wilson05235c52016-07-20 09:21:08 +0100305
Chris Wilsone95433c2016-10-28 13:58:27 +0100306long i915_wait_request(struct drm_i915_gem_request *req,
307 unsigned int flags,
308 long timeout)
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100309 __attribute__((nonnull(1)));
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100310#define I915_WAIT_INTERRUPTIBLE BIT(0)
311#define I915_WAIT_LOCKED BIT(1) /* struct_mutex held, handle GPU reset */
Chris Wilsone95433c2016-10-28 13:58:27 +0100312#define I915_WAIT_ALL BIT(2) /* used by i915_gem_object_wait() */
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100313
Chris Wilson05235c52016-07-20 09:21:08 +0100314static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine);
315
316/**
317 * Returns true if seq1 is later than seq2.
318 */
319static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
320{
321 return (s32)(seq1 - seq2) >= 0;
322}
323
324static inline bool
Chris Wilson754c9fd2017-02-23 07:44:14 +0000325__i915_gem_request_completed(const struct drm_i915_gem_request *req, u32 seqno)
Chris Wilson65e47602016-10-28 13:58:49 +0100326{
Chris Wilson754c9fd2017-02-23 07:44:14 +0000327 GEM_BUG_ON(!seqno);
328 return i915_seqno_passed(intel_engine_get_seqno(req->engine), seqno) &&
329 seqno == i915_gem_request_global_seqno(req);
Chris Wilson65e47602016-10-28 13:58:49 +0100330}
331
332static inline bool
Chris Wilson05235c52016-07-20 09:21:08 +0100333i915_gem_request_completed(const struct drm_i915_gem_request *req)
334{
Chris Wilson754c9fd2017-02-23 07:44:14 +0000335 u32 seqno;
336
337 seqno = i915_gem_request_global_seqno(req);
338 if (!seqno)
Chris Wilson65e47602016-10-28 13:58:49 +0100339 return false;
340
Chris Wilson754c9fd2017-02-23 07:44:14 +0000341 return __i915_gem_request_completed(req, seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100342}
343
Chris Wilson381f3712016-08-04 07:52:29 +0100344/* We treat requests as fences. This is not be to confused with our
345 * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
346 * We use the fences to synchronize access from the CPU with activity on the
347 * GPU, for example, we should not rewrite an object's PTE whilst the GPU
348 * is reading them. We also track fences at a higher level to provide
349 * implicit synchronisation around GEM objects, e.g. set-domain will wait
350 * for outstanding GPU rendering before marking the object ready for CPU
351 * access, or a pageflip will wait until the GPU is complete before showing
352 * the frame on the scanout.
353 *
354 * In order to use a fence, the object must track the fence it needs to
355 * serialise with. For example, GEM objects want to track both read and
356 * write access so that we can perform concurrent read operations between
357 * the CPU and GPU engines, as well as waiting for all rendering to
358 * complete, or waiting for the last GPU user of a "fence register". The
359 * object then embeds a #i915_gem_active to track the most recent (in
360 * retirement order) request relevant for the desired mode of access.
361 * The #i915_gem_active is updated with i915_gem_active_set() to track the
362 * most recent fence request, typically this is done as part of
363 * i915_vma_move_to_active().
364 *
365 * When the #i915_gem_active completes (is retired), it will
366 * signal its completion to the owner through a callback as well as mark
367 * itself as idle (i915_gem_active.request == NULL). The owner
368 * can then perform any action, such as delayed freeing of an active
369 * resource including itself.
370 */
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100371struct i915_gem_active;
372
373typedef void (*i915_gem_retire_fn)(struct i915_gem_active *,
374 struct drm_i915_gem_request *);
375
Chris Wilson381f3712016-08-04 07:52:29 +0100376struct i915_gem_active {
Chris Wilson0eafec62016-08-04 16:32:41 +0100377 struct drm_i915_gem_request __rcu *request;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100378 struct list_head link;
379 i915_gem_retire_fn retire;
Chris Wilson381f3712016-08-04 07:52:29 +0100380};
381
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100382void i915_gem_retire_noop(struct i915_gem_active *,
383 struct drm_i915_gem_request *request);
384
385/**
386 * init_request_active - prepares the activity tracker for use
387 * @active - the active tracker
388 * @func - a callback when then the tracker is retired (becomes idle),
389 * can be NULL
390 *
391 * init_request_active() prepares the embedded @active struct for use as
392 * an activity tracker, that is for tracking the last known active request
393 * associated with it. When the last request becomes idle, when it is retired
394 * after completion, the optional callback @func is invoked.
395 */
396static inline void
397init_request_active(struct i915_gem_active *active,
398 i915_gem_retire_fn retire)
399{
400 INIT_LIST_HEAD(&active->link);
401 active->retire = retire ?: i915_gem_retire_noop;
402}
403
Chris Wilson27c01aa2016-08-04 07:52:30 +0100404/**
405 * i915_gem_active_set - updates the tracker to watch the current request
406 * @active - the active tracker
407 * @request - the request to watch
408 *
409 * i915_gem_active_set() watches the given @request for completion. Whilst
410 * that @request is busy, the @active reports busy. When that @request is
411 * retired, the @active tracker is updated to report idle.
412 */
Chris Wilson381f3712016-08-04 07:52:29 +0100413static inline void
414i915_gem_active_set(struct i915_gem_active *active,
415 struct drm_i915_gem_request *request)
416{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100417 list_move(&active->link, &request->active_list);
Chris Wilson0eafec62016-08-04 16:32:41 +0100418 rcu_assign_pointer(active->request, request);
Chris Wilson381f3712016-08-04 07:52:29 +0100419}
420
Ville Syrjäläecd9caa02016-12-07 17:56:47 +0000421/**
422 * i915_gem_active_set_retire_fn - updates the retirement callback
423 * @active - the active tracker
424 * @fn - the routine called when the request is retired
425 * @mutex - struct_mutex used to guard retirements
426 *
427 * i915_gem_active_set_retire_fn() updates the function pointer that
428 * is called when the final request associated with the @active tracker
429 * is retired.
430 */
431static inline void
432i915_gem_active_set_retire_fn(struct i915_gem_active *active,
433 i915_gem_retire_fn fn,
434 struct mutex *mutex)
435{
436 lockdep_assert_held(mutex);
437 active->retire = fn ?: i915_gem_retire_noop;
438}
439
Chris Wilsond72d9082016-08-04 07:52:31 +0100440static inline struct drm_i915_gem_request *
441__i915_gem_active_peek(const struct i915_gem_active *active)
442{
Chris Wilson0eafec62016-08-04 16:32:41 +0100443 /* Inside the error capture (running with the driver in an unknown
444 * state), we want to bend the rules slightly (a lot).
445 *
446 * Work is in progress to make it safer, in the meantime this keeps
447 * the known issue from spamming the logs.
448 */
449 return rcu_dereference_protected(active->request, 1);
Chris Wilsond72d9082016-08-04 07:52:31 +0100450}
451
Chris Wilson27c01aa2016-08-04 07:52:30 +0100452/**
Chris Wilson385384a2016-08-09 08:37:01 +0100453 * i915_gem_active_raw - return the active request
454 * @active - the active tracker
455 *
456 * i915_gem_active_raw() returns the current request being tracked, or NULL.
457 * It does not obtain a reference on the request for the caller, so the caller
458 * must hold struct_mutex.
459 */
460static inline struct drm_i915_gem_request *
461i915_gem_active_raw(const struct i915_gem_active *active, struct mutex *mutex)
462{
463 return rcu_dereference_protected(active->request,
464 lockdep_is_held(mutex));
465}
466
467/**
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100468 * i915_gem_active_peek - report the active request being monitored
Chris Wilson27c01aa2016-08-04 07:52:30 +0100469 * @active - the active tracker
470 *
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100471 * i915_gem_active_peek() returns the current request being tracked if
472 * still active, or NULL. It does not obtain a reference on the request
473 * for the caller, so the caller must hold struct_mutex.
Chris Wilson27c01aa2016-08-04 07:52:30 +0100474 */
475static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100476i915_gem_active_peek(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100477{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100478 struct drm_i915_gem_request *request;
479
Chris Wilson385384a2016-08-09 08:37:01 +0100480 request = i915_gem_active_raw(active, mutex);
Chris Wilson0eafec62016-08-04 16:32:41 +0100481 if (!request || i915_gem_request_completed(request))
482 return NULL;
483
484 return request;
485}
486
487/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100488 * i915_gem_active_get - return a reference to the active request
489 * @active - the active tracker
490 *
491 * i915_gem_active_get() returns a reference to the active request, or NULL
492 * if the active tracker is idle. The caller must hold struct_mutex.
493 */
494static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100495i915_gem_active_get(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100496{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100497 return i915_gem_request_get(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100498}
499
500/**
Chris Wilson0eafec62016-08-04 16:32:41 +0100501 * __i915_gem_active_get_rcu - return a reference to the active request
502 * @active - the active tracker
503 *
504 * __i915_gem_active_get() returns a reference to the active request, or NULL
505 * if the active tracker is idle. The caller must hold the RCU read lock, but
506 * the returned pointer is safe to use outside of RCU.
507 */
508static inline struct drm_i915_gem_request *
509__i915_gem_active_get_rcu(const struct i915_gem_active *active)
510{
511 /* Performing a lockless retrieval of the active request is super
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800512 * tricky. SLAB_TYPESAFE_BY_RCU merely guarantees that the backing
Chris Wilson0eafec62016-08-04 16:32:41 +0100513 * slab of request objects will not be freed whilst we hold the
514 * RCU read lock. It does not guarantee that the request itself
515 * will not be freed and then *reused*. Viz,
516 *
517 * Thread A Thread B
518 *
519 * req = active.request
520 * retire(req) -> free(req);
521 * (req is now first on the slab freelist)
522 * active.request = NULL
523 *
524 * req = new submission on a new object
525 * ref(req)
526 *
527 * To prevent the request from being reused whilst the caller
528 * uses it, we take a reference like normal. Whilst acquiring
529 * the reference we check that it is not in a destroyed state
530 * (refcnt == 0). That prevents the request being reallocated
531 * whilst the caller holds on to it. To check that the request
532 * was not reallocated as we acquired the reference we have to
533 * check that our request remains the active request across
534 * the lookup, in the same manner as a seqlock. The visibility
535 * of the pointer versus the reference counting is controlled
536 * by using RCU barriers (rcu_dereference and rcu_assign_pointer).
537 *
538 * In the middle of all that, we inspect whether the request is
539 * complete. Retiring is lazy so the request may be completed long
540 * before the active tracker is updated. Querying whether the
541 * request is complete is far cheaper (as it involves no locked
542 * instructions setting cachelines to exclusive) than acquiring
543 * the reference, so we do it first. The RCU read lock ensures the
544 * pointer dereference is valid, but does not ensure that the
545 * seqno nor HWS is the right one! However, if the request was
546 * reallocated, that means the active tracker's request was complete.
547 * If the new request is also complete, then both are and we can
548 * just report the active tracker is idle. If the new request is
549 * incomplete, then we acquire a reference on it and check that
550 * it remained the active request.
Chris Wilson5a198b82016-08-09 09:23:34 +0100551 *
552 * It is then imperative that we do not zero the request on
553 * reallocation, so that we can chase the dangling pointers!
554 * See i915_gem_request_alloc().
Chris Wilson0eafec62016-08-04 16:32:41 +0100555 */
556 do {
557 struct drm_i915_gem_request *request;
558
559 request = rcu_dereference(active->request);
560 if (!request || i915_gem_request_completed(request))
561 return NULL;
562
Daniel Vetterc75870d2016-08-22 10:55:22 +0200563 /* An especially silly compiler could decide to recompute the
564 * result of i915_gem_request_completed, more specifically
565 * re-emit the load for request->fence.seqno. A race would catch
566 * a later seqno value, which could flip the result from true to
567 * false. Which means part of the instructions below might not
568 * be executed, while later on instructions are executed. Due to
569 * barriers within the refcounting the inconsistency can't reach
570 * past the call to i915_gem_request_get_rcu, but not executing
571 * that while still executing i915_gem_request_put() creates
572 * havoc enough. Prevent this with a compiler barrier.
573 */
574 barrier();
575
Chris Wilson0eafec62016-08-04 16:32:41 +0100576 request = i915_gem_request_get_rcu(request);
577
578 /* What stops the following rcu_access_pointer() from occurring
579 * before the above i915_gem_request_get_rcu()? If we were
580 * to read the value before pausing to get the reference to
581 * the request, we may not notice a change in the active
582 * tracker.
583 *
584 * The rcu_access_pointer() is a mere compiler barrier, which
585 * means both the CPU and compiler are free to perform the
586 * memory read without constraint. The compiler only has to
587 * ensure that any operations after the rcu_access_pointer()
588 * occur afterwards in program order. This means the read may
589 * be performed earlier by an out-of-order CPU, or adventurous
590 * compiler.
591 *
592 * The atomic operation at the heart of
Chris Wilsonf54d1862016-10-25 13:00:45 +0100593 * i915_gem_request_get_rcu(), see dma_fence_get_rcu(), is
Chris Wilson0eafec62016-08-04 16:32:41 +0100594 * atomic_inc_not_zero() which is only a full memory barrier
595 * when successful. That is, if i915_gem_request_get_rcu()
596 * returns the request (and so with the reference counted
597 * incremented) then the following read for rcu_access_pointer()
598 * must occur after the atomic operation and so confirm
599 * that this request is the one currently being tracked.
Chris Wilsonedf6b762016-08-09 09:23:33 +0100600 *
601 * The corresponding write barrier is part of
602 * rcu_assign_pointer().
Chris Wilson0eafec62016-08-04 16:32:41 +0100603 */
604 if (!request || request == rcu_access_pointer(active->request))
605 return rcu_pointer_handoff(request);
606
607 i915_gem_request_put(request);
608 } while (1);
609}
610
611/**
612 * i915_gem_active_get_unlocked - return a reference to the active request
613 * @active - the active tracker
614 *
615 * i915_gem_active_get_unlocked() returns a reference to the active request,
616 * or NULL if the active tracker is idle. The reference is obtained under RCU,
617 * so no locking is required by the caller.
618 *
619 * The reference should be freed with i915_gem_request_put().
620 */
621static inline struct drm_i915_gem_request *
622i915_gem_active_get_unlocked(const struct i915_gem_active *active)
623{
624 struct drm_i915_gem_request *request;
625
626 rcu_read_lock();
627 request = __i915_gem_active_get_rcu(active);
628 rcu_read_unlock();
629
630 return request;
631}
632
633/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100634 * i915_gem_active_isset - report whether the active tracker is assigned
635 * @active - the active tracker
636 *
637 * i915_gem_active_isset() returns true if the active tracker is currently
638 * assigned to a request. Due to the lazy retiring, that request may be idle
639 * and this may report stale information.
640 */
641static inline bool
642i915_gem_active_isset(const struct i915_gem_active *active)
643{
Chris Wilson0eafec62016-08-04 16:32:41 +0100644 return rcu_access_pointer(active->request);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100645}
646
647/**
Chris Wilsond07f0e52016-10-28 13:58:44 +0100648 * i915_gem_active_wait - waits until the request is completed
Chris Wilson24676582016-08-05 10:14:06 +0100649 * @active - the active request on which to wait
Chris Wilsonea746f32016-09-09 14:11:49 +0100650 * @flags - how to wait
Chris Wilson24676582016-08-05 10:14:06 +0100651 * @timeout - how long to wait at most
652 * @rps - userspace client to charge for a waitboost
653 *
Chris Wilson2e369912016-10-28 13:58:28 +0100654 * i915_gem_active_wait() waits until the request is completed before
Chris Wilson24676582016-08-05 10:14:06 +0100655 * returning, without requiring any locks to be held. Note that it does not
656 * retire any requests before returning.
657 *
658 * This function relies on RCU in order to acquire the reference to the active
659 * request without holding any locks. See __i915_gem_active_get_rcu() for the
660 * glory details on how that is managed. Once the reference is acquired, we
661 * can then wait upon the request, and afterwards release our reference,
662 * free of any locking.
663 *
664 * This function wraps i915_wait_request(), see it for the full details on
665 * the arguments.
666 *
667 * Returns 0 if successful, or a negative error code.
668 */
669static inline int
Chris Wilson2e369912016-10-28 13:58:28 +0100670i915_gem_active_wait(const struct i915_gem_active *active, unsigned int flags)
Chris Wilson24676582016-08-05 10:14:06 +0100671{
672 struct drm_i915_gem_request *request;
Chris Wilsone95433c2016-10-28 13:58:27 +0100673 long ret = 0;
Chris Wilson24676582016-08-05 10:14:06 +0100674
675 request = i915_gem_active_get_unlocked(active);
676 if (request) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100677 ret = i915_wait_request(request, flags, MAX_SCHEDULE_TIMEOUT);
Chris Wilson24676582016-08-05 10:14:06 +0100678 i915_gem_request_put(request);
679 }
680
Chris Wilsone95433c2016-10-28 13:58:27 +0100681 return ret < 0 ? ret : 0;
Chris Wilson24676582016-08-05 10:14:06 +0100682}
683
684/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100685 * i915_gem_active_retire - waits until the request is retired
686 * @active - the active request on which to wait
687 *
688 * i915_gem_active_retire() waits until the request is completed,
689 * and then ensures that at least the retirement handler for this
690 * @active tracker is called before returning. If the @active
691 * tracker is idle, the function returns immediately.
692 */
693static inline int __must_check
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100694i915_gem_active_retire(struct i915_gem_active *active,
Chris Wilsond72d9082016-08-04 07:52:31 +0100695 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100696{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100697 struct drm_i915_gem_request *request;
Chris Wilsone95433c2016-10-28 13:58:27 +0100698 long ret;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100699
Chris Wilson385384a2016-08-09 08:37:01 +0100700 request = i915_gem_active_raw(active, mutex);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100701 if (!request)
702 return 0;
703
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100704 ret = i915_wait_request(request,
705 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
Chris Wilsone95433c2016-10-28 13:58:27 +0100706 MAX_SCHEDULE_TIMEOUT);
707 if (ret < 0)
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100708 return ret;
709
710 list_del_init(&active->link);
Chris Wilson0eafec62016-08-04 16:32:41 +0100711 RCU_INIT_POINTER(active->request, NULL);
712
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100713 active->retire(active, request);
714
715 return 0;
Chris Wilson27c01aa2016-08-04 07:52:30 +0100716}
717
Chris Wilson381f3712016-08-04 07:52:29 +0100718#define for_each_active(mask, idx) \
719 for (; mask ? idx = ffs(mask) - 1, 1 : 0; mask &= ~BIT(idx))
720
Chris Wilson05235c52016-07-20 09:21:08 +0100721#endif /* I915_GEM_REQUEST_H */