blob: b81f6709905c2646e1f92a0582eada3e39859d18 [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 Wilson52e54202016-11-14 20:41:02 +000047struct i915_dependency {
48 struct i915_priotree *signaler;
49 struct list_head signal_link;
50 struct list_head wait_link;
Chris Wilson20311bd2016-11-14 20:41:03 +000051 struct list_head dfs_link;
Chris Wilson52e54202016-11-14 20:41:02 +000052 unsigned long flags;
53#define I915_DEPENDENCY_ALLOC BIT(0)
54};
55
56/* Requests exist in a complex web of interdependencies. Each request
57 * has to wait for some other request to complete before it is ready to be run
58 * (e.g. we have to wait until the pixels have been rendering into a texture
59 * before we can copy from it). We track the readiness of a request in terms
60 * of fences, but we also need to keep the dependency tree for the lifetime
61 * of the request (beyond the life of an individual fence). We use the tree
62 * at various points to reorder the requests whilst keeping the requests
63 * in order with respect to their various dependencies.
64 */
65struct i915_priotree {
66 struct list_head signalers_list; /* those before us, we depend upon */
67 struct list_head waiters_list; /* those after us, they depend upon us */
Chris Wilson20311bd2016-11-14 20:41:03 +000068 struct rb_node node;
69 int priority;
70#define I915_PRIORITY_MAX 1024
71#define I915_PRIORITY_MIN (-I915_PRIORITY_MAX)
Chris Wilson52e54202016-11-14 20:41:02 +000072};
73
Chris Wilson05235c52016-07-20 09:21:08 +010074/**
75 * Request queue structure.
76 *
77 * The request queue allows us to note sequence numbers that have been emitted
78 * and may be associated with active buffers to be retired.
79 *
80 * By keeping this list, we can avoid having to do questionable sequence
81 * number comparisons on buffer last_read|write_seqno. It also allows an
82 * emission time to be associated with the request for tracking how far ahead
83 * of the GPU the submission is.
84 *
Chris Wilson5a198b82016-08-09 09:23:34 +010085 * When modifying this structure be very aware that we perform a lockless
86 * RCU lookup of it that may race against reallocation of the struct
87 * from the slab freelist. We intentionally do not zero the structure on
88 * allocation so that the lookup can use the dangling pointers (and is
89 * cogniscent that those pointers may be wrong). Instead, everything that
90 * needs to be initialised must be done so explicitly.
91 *
Chris Wilson04769652016-07-20 09:21:11 +010092 * The requests are reference counted.
Chris Wilson05235c52016-07-20 09:21:08 +010093 */
94struct drm_i915_gem_request {
Chris Wilsonf54d1862016-10-25 13:00:45 +010095 struct dma_fence fence;
Chris Wilson04769652016-07-20 09:21:11 +010096 spinlock_t lock;
Chris Wilson05235c52016-07-20 09:21:08 +010097
98 /** On Which ring this request was generated */
99 struct drm_i915_private *i915;
100
101 /**
102 * Context and ring buffer related to this request
103 * Contexts are refcounted, so when this request is associated with a
104 * context, we must increment the context's refcount, to guarantee that
105 * it persists while any request is linked to it. Requests themselves
106 * are also refcounted, so the request will only be freed when the last
107 * reference to it is dismissed, and the code in
108 * i915_gem_request_free() will then decrement the refcount on the
109 * context.
110 */
111 struct i915_gem_context *ctx;
112 struct intel_engine_cs *engine;
Chris Wilson7e37f882016-08-02 22:50:21 +0100113 struct intel_ring *ring;
Chris Wilson73cb9702016-10-28 13:58:46 +0100114 struct intel_timeline *timeline;
Chris Wilson05235c52016-07-20 09:21:08 +0100115 struct intel_signal_node signaling;
116
Chris Wilson23902e42016-11-14 20:40:58 +0000117 /* Fences for the various phases in the request's lifetime.
118 *
119 * The submit fence is used to await upon all of the request's
120 * dependencies. When it is signaled, the request is ready to run.
121 * It is used by the driver to then queue the request for execution.
Chris Wilson23902e42016-11-14 20:40:58 +0000122 */
Chris Wilson5590af32016-09-09 14:11:54 +0100123 struct i915_sw_fence submit;
Chris Wilson0a046a02016-09-09 14:12:00 +0100124 wait_queue_t submitq;
Chris Wilsonfe497892017-02-23 07:44:13 +0000125 wait_queue_head_t execute;
Chris Wilson5590af32016-09-09 14:11:54 +0100126
Chris Wilson52e54202016-11-14 20:41:02 +0000127 /* A list of everyone we wait upon, and everyone who waits upon us.
128 * Even though we will not be submitted to the hardware before the
129 * submit fence is signaled (it waits for all external events as well
130 * as our own requests), the scheduler still needs to know the
131 * dependency tree for the lifetime of the request (from execbuf
132 * to retirement), i.e. bidirectional dependency information for the
133 * request not tied to individual fences.
134 */
135 struct i915_priotree priotree;
136 struct i915_dependency dep;
137
Chris Wilson754c9fd2017-02-23 07:44:14 +0000138 /** GEM sequence number associated with this request on the
139 * global execution timeline. It is zero when the request is not
140 * on the HW queue (i.e. not on the engine timeline list).
141 * Its value is guarded by the timeline spinlock.
142 */
Chris Wilson65e47602016-10-28 13:58:49 +0100143 u32 global_seqno;
144
Chris Wilsona52abd22016-09-09 14:11:43 +0100145 /** Position in the ring of the start of the request */
Chris Wilson05235c52016-07-20 09:21:08 +0100146 u32 head;
147
148 /**
Chris Wilsona52abd22016-09-09 14:11:43 +0100149 * Position in the ring of the start of the postfix.
150 * This is required to calculate the maximum available ring space
151 * without overwriting the postfix.
Chris Wilson05235c52016-07-20 09:21:08 +0100152 */
153 u32 postfix;
154
Chris Wilsona52abd22016-09-09 14:11:43 +0100155 /** Position in the ring of the end of the whole request */
Chris Wilson05235c52016-07-20 09:21:08 +0100156 u32 tail;
157
Chris Wilsona52abd22016-09-09 14:11:43 +0100158 /** Position in the ring of the end of any workarounds after the tail */
159 u32 wa_tail;
160
161 /** Preallocate space in the ring for the emitting the request */
Chris Wilson05235c52016-07-20 09:21:08 +0100162 u32 reserved_space;
163
Chris Wilson05235c52016-07-20 09:21:08 +0100164 /** Batch buffer related to this request if any (used for
165 * error state dump only).
166 */
Chris Wilson058d88c2016-08-15 10:49:06 +0100167 struct i915_vma *batch;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100168 struct list_head active_list;
Chris Wilson05235c52016-07-20 09:21:08 +0100169
170 /** Time at which this request was emitted, in jiffies. */
171 unsigned long emitted_jiffies;
172
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100173 /** engine->request_list entry for this request */
174 struct list_head link;
Chris Wilson05235c52016-07-20 09:21:08 +0100175
Chris Wilson675d9ad2016-08-04 07:52:36 +0100176 /** ring->request_list entry for this request */
177 struct list_head ring_link;
178
Chris Wilson05235c52016-07-20 09:21:08 +0100179 struct drm_i915_file_private *file_priv;
180 /** file_priv list entry for this request */
181 struct list_head client_list;
Chris Wilson05235c52016-07-20 09:21:08 +0100182};
183
Chris Wilsonf54d1862016-10-25 13:00:45 +0100184extern const struct dma_fence_ops i915_fence_ops;
Chris Wilson04769652016-07-20 09:21:11 +0100185
Chris Wilsonb52992c2016-10-28 13:58:24 +0100186static inline bool dma_fence_is_i915(const struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +0100187{
188 return fence->ops == &i915_fence_ops;
189}
190
Chris Wilson05235c52016-07-20 09:21:08 +0100191struct drm_i915_gem_request * __must_check
192i915_gem_request_alloc(struct intel_engine_cs *engine,
193 struct i915_gem_context *ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100194int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
195 struct drm_file *file);
196void i915_gem_request_retire_upto(struct drm_i915_gem_request *req);
197
Chris Wilson05235c52016-07-20 09:21:08 +0100198static inline struct drm_i915_gem_request *
Chris Wilsonf54d1862016-10-25 13:00:45 +0100199to_request(struct dma_fence *fence)
Chris Wilson04769652016-07-20 09:21:11 +0100200{
201 /* We assume that NULL fence/request are interoperable */
202 BUILD_BUG_ON(offsetof(struct drm_i915_gem_request, fence) != 0);
Chris Wilsonb52992c2016-10-28 13:58:24 +0100203 GEM_BUG_ON(fence && !dma_fence_is_i915(fence));
Chris Wilson04769652016-07-20 09:21:11 +0100204 return container_of(fence, struct drm_i915_gem_request, fence);
205}
206
207static inline struct drm_i915_gem_request *
Chris Wilsone8a261e2016-07-20 13:31:49 +0100208i915_gem_request_get(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100209{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100210 return to_request(dma_fence_get(&req->fence));
Chris Wilson05235c52016-07-20 09:21:08 +0100211}
212
Chris Wilson0eafec62016-08-04 16:32:41 +0100213static inline struct drm_i915_gem_request *
214i915_gem_request_get_rcu(struct drm_i915_gem_request *req)
215{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100216 return to_request(dma_fence_get_rcu(&req->fence));
Chris Wilson0eafec62016-08-04 16:32:41 +0100217}
218
Chris Wilson05235c52016-07-20 09:21:08 +0100219static inline void
Chris Wilsone8a261e2016-07-20 13:31:49 +0100220i915_gem_request_put(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100221{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100222 dma_fence_put(&req->fence);
Chris Wilson05235c52016-07-20 09:21:08 +0100223}
224
225static inline void i915_gem_request_assign(struct drm_i915_gem_request **pdst,
226 struct drm_i915_gem_request *src)
227{
228 if (src)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100229 i915_gem_request_get(src);
Chris Wilson05235c52016-07-20 09:21:08 +0100230
231 if (*pdst)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100232 i915_gem_request_put(*pdst);
Chris Wilson05235c52016-07-20 09:21:08 +0100233
234 *pdst = src;
235}
236
Chris Wilson754c9fd2017-02-23 07:44:14 +0000237/**
238 * i915_gem_request_global_seqno - report the current global seqno
239 * @request - the request
240 *
241 * A request is assigned a global seqno only when it is on the hardware
242 * execution queue. The global seqno can be used to maintain a list of
243 * requests on the same engine in retirement order, for example for
244 * constructing a priority queue for waiting. Prior to its execution, or
245 * if it is subsequently removed in the event of preemption, its global
246 * seqno is zero. As both insertion and removal from the execution queue
247 * may operate in IRQ context, it is not guarded by the usual struct_mutex
248 * BKL. Instead those relying on the global seqno must be prepared for its
249 * value to change between reads. Only when the request is complete can
250 * the global seqno be stable (due to the memory barriers on submitting
251 * the commands to the hardware to write the breadcrumb, if the HWS shows
252 * that it has passed the global seqno and the global seqno is unchanged
253 * after the read, it is indeed complete).
254 */
255static u32
256i915_gem_request_global_seqno(const struct drm_i915_gem_request *request)
257{
258 return READ_ONCE(request->global_seqno);
259}
260
Chris Wilsona2bc4692016-09-09 14:11:56 +0100261int
262i915_gem_request_await_object(struct drm_i915_gem_request *to,
263 struct drm_i915_gem_object *obj,
264 bool write);
Chris Wilsonb52992c2016-10-28 13:58:24 +0100265int i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
266 struct dma_fence *fence);
Chris Wilsona2bc4692016-09-09 14:11:56 +0100267
Chris Wilson17f298cf2016-08-10 13:41:46 +0100268void __i915_add_request(struct drm_i915_gem_request *req, bool flush_caches);
Chris Wilson05235c52016-07-20 09:21:08 +0100269#define i915_add_request(req) \
Chris Wilson17f298cf2016-08-10 13:41:46 +0100270 __i915_add_request(req, true)
Chris Wilson05235c52016-07-20 09:21:08 +0100271#define i915_add_request_no_flush(req) \
Chris Wilson17f298cf2016-08-10 13:41:46 +0100272 __i915_add_request(req, false)
Chris Wilson05235c52016-07-20 09:21:08 +0100273
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000274void __i915_gem_request_submit(struct drm_i915_gem_request *request);
275void i915_gem_request_submit(struct drm_i915_gem_request *request);
276
Chris Wilson05235c52016-07-20 09:21:08 +0100277struct intel_rps_client;
Chris Wilson42df2712016-07-20 09:21:12 +0100278#define NO_WAITBOOST ERR_PTR(-1)
279#define IS_RPS_CLIENT(p) (!IS_ERR(p))
280#define IS_RPS_USER(p) (!IS_ERR_OR_NULL(p))
Chris Wilson05235c52016-07-20 09:21:08 +0100281
Chris Wilsone95433c2016-10-28 13:58:27 +0100282long i915_wait_request(struct drm_i915_gem_request *req,
283 unsigned int flags,
284 long timeout)
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100285 __attribute__((nonnull(1)));
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100286#define I915_WAIT_INTERRUPTIBLE BIT(0)
287#define I915_WAIT_LOCKED BIT(1) /* struct_mutex held, handle GPU reset */
Chris Wilsone95433c2016-10-28 13:58:27 +0100288#define I915_WAIT_ALL BIT(2) /* used by i915_gem_object_wait() */
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100289
Chris Wilson05235c52016-07-20 09:21:08 +0100290static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine);
291
292/**
293 * Returns true if seq1 is later than seq2.
294 */
295static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
296{
297 return (s32)(seq1 - seq2) >= 0;
298}
299
300static inline bool
Chris Wilson754c9fd2017-02-23 07:44:14 +0000301__i915_gem_request_started(const struct drm_i915_gem_request *req, u32 seqno)
Chris Wilson05235c52016-07-20 09:21:08 +0100302{
Chris Wilson754c9fd2017-02-23 07:44:14 +0000303 GEM_BUG_ON(!seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100304 return i915_seqno_passed(intel_engine_get_seqno(req->engine),
Chris Wilson754c9fd2017-02-23 07:44:14 +0000305 seqno - 1);
Chris Wilson05235c52016-07-20 09:21:08 +0100306}
307
308static inline bool
Chris Wilson65e47602016-10-28 13:58:49 +0100309i915_gem_request_started(const struct drm_i915_gem_request *req)
310{
Chris Wilson754c9fd2017-02-23 07:44:14 +0000311 u32 seqno;
312
313 seqno = i915_gem_request_global_seqno(req);
314 if (!seqno)
Chris Wilson65e47602016-10-28 13:58:49 +0100315 return false;
316
Chris Wilson754c9fd2017-02-23 07:44:14 +0000317 return __i915_gem_request_started(req, seqno);
Chris Wilson65e47602016-10-28 13:58:49 +0100318}
319
320static inline bool
Chris Wilson754c9fd2017-02-23 07:44:14 +0000321__i915_gem_request_completed(const struct drm_i915_gem_request *req, u32 seqno)
Chris Wilson65e47602016-10-28 13:58:49 +0100322{
Chris Wilson754c9fd2017-02-23 07:44:14 +0000323 GEM_BUG_ON(!seqno);
324 return i915_seqno_passed(intel_engine_get_seqno(req->engine), seqno) &&
325 seqno == i915_gem_request_global_seqno(req);
Chris Wilson65e47602016-10-28 13:58:49 +0100326}
327
328static inline bool
Chris Wilson05235c52016-07-20 09:21:08 +0100329i915_gem_request_completed(const struct drm_i915_gem_request *req)
330{
Chris Wilson754c9fd2017-02-23 07:44:14 +0000331 u32 seqno;
332
333 seqno = i915_gem_request_global_seqno(req);
334 if (!seqno)
Chris Wilson65e47602016-10-28 13:58:49 +0100335 return false;
336
Chris Wilson754c9fd2017-02-23 07:44:14 +0000337 return __i915_gem_request_completed(req, seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100338}
339
340bool __i915_spin_request(const struct drm_i915_gem_request *request,
Chris Wilson754c9fd2017-02-23 07:44:14 +0000341 u32 seqno, int state, unsigned long timeout_us);
Chris Wilson05235c52016-07-20 09:21:08 +0100342static inline bool i915_spin_request(const struct drm_i915_gem_request *request,
343 int state, unsigned long timeout_us)
344{
Chris Wilson754c9fd2017-02-23 07:44:14 +0000345 u32 seqno;
346
347 seqno = i915_gem_request_global_seqno(request);
348 return (__i915_gem_request_started(request, seqno) &&
349 __i915_spin_request(request, seqno, state, timeout_us));
Chris Wilson05235c52016-07-20 09:21:08 +0100350}
351
Chris Wilson381f3712016-08-04 07:52:29 +0100352/* We treat requests as fences. This is not be to confused with our
353 * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
354 * We use the fences to synchronize access from the CPU with activity on the
355 * GPU, for example, we should not rewrite an object's PTE whilst the GPU
356 * is reading them. We also track fences at a higher level to provide
357 * implicit synchronisation around GEM objects, e.g. set-domain will wait
358 * for outstanding GPU rendering before marking the object ready for CPU
359 * access, or a pageflip will wait until the GPU is complete before showing
360 * the frame on the scanout.
361 *
362 * In order to use a fence, the object must track the fence it needs to
363 * serialise with. For example, GEM objects want to track both read and
364 * write access so that we can perform concurrent read operations between
365 * the CPU and GPU engines, as well as waiting for all rendering to
366 * complete, or waiting for the last GPU user of a "fence register". The
367 * object then embeds a #i915_gem_active to track the most recent (in
368 * retirement order) request relevant for the desired mode of access.
369 * The #i915_gem_active is updated with i915_gem_active_set() to track the
370 * most recent fence request, typically this is done as part of
371 * i915_vma_move_to_active().
372 *
373 * When the #i915_gem_active completes (is retired), it will
374 * signal its completion to the owner through a callback as well as mark
375 * itself as idle (i915_gem_active.request == NULL). The owner
376 * can then perform any action, such as delayed freeing of an active
377 * resource including itself.
378 */
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100379struct i915_gem_active;
380
381typedef void (*i915_gem_retire_fn)(struct i915_gem_active *,
382 struct drm_i915_gem_request *);
383
Chris Wilson381f3712016-08-04 07:52:29 +0100384struct i915_gem_active {
Chris Wilson0eafec62016-08-04 16:32:41 +0100385 struct drm_i915_gem_request __rcu *request;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100386 struct list_head link;
387 i915_gem_retire_fn retire;
Chris Wilson381f3712016-08-04 07:52:29 +0100388};
389
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100390void i915_gem_retire_noop(struct i915_gem_active *,
391 struct drm_i915_gem_request *request);
392
393/**
394 * init_request_active - prepares the activity tracker for use
395 * @active - the active tracker
396 * @func - a callback when then the tracker is retired (becomes idle),
397 * can be NULL
398 *
399 * init_request_active() prepares the embedded @active struct for use as
400 * an activity tracker, that is for tracking the last known active request
401 * associated with it. When the last request becomes idle, when it is retired
402 * after completion, the optional callback @func is invoked.
403 */
404static inline void
405init_request_active(struct i915_gem_active *active,
406 i915_gem_retire_fn retire)
407{
408 INIT_LIST_HEAD(&active->link);
409 active->retire = retire ?: i915_gem_retire_noop;
410}
411
Chris Wilson27c01aa2016-08-04 07:52:30 +0100412/**
413 * i915_gem_active_set - updates the tracker to watch the current request
414 * @active - the active tracker
415 * @request - the request to watch
416 *
417 * i915_gem_active_set() watches the given @request for completion. Whilst
418 * that @request is busy, the @active reports busy. When that @request is
419 * retired, the @active tracker is updated to report idle.
420 */
Chris Wilson381f3712016-08-04 07:52:29 +0100421static inline void
422i915_gem_active_set(struct i915_gem_active *active,
423 struct drm_i915_gem_request *request)
424{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100425 list_move(&active->link, &request->active_list);
Chris Wilson0eafec62016-08-04 16:32:41 +0100426 rcu_assign_pointer(active->request, request);
Chris Wilson381f3712016-08-04 07:52:29 +0100427}
428
Ville Syrjäläecd9caa02016-12-07 17:56:47 +0000429/**
430 * i915_gem_active_set_retire_fn - updates the retirement callback
431 * @active - the active tracker
432 * @fn - the routine called when the request is retired
433 * @mutex - struct_mutex used to guard retirements
434 *
435 * i915_gem_active_set_retire_fn() updates the function pointer that
436 * is called when the final request associated with the @active tracker
437 * is retired.
438 */
439static inline void
440i915_gem_active_set_retire_fn(struct i915_gem_active *active,
441 i915_gem_retire_fn fn,
442 struct mutex *mutex)
443{
444 lockdep_assert_held(mutex);
445 active->retire = fn ?: i915_gem_retire_noop;
446}
447
Chris Wilsond72d9082016-08-04 07:52:31 +0100448static inline struct drm_i915_gem_request *
449__i915_gem_active_peek(const struct i915_gem_active *active)
450{
Chris Wilson0eafec62016-08-04 16:32:41 +0100451 /* Inside the error capture (running with the driver in an unknown
452 * state), we want to bend the rules slightly (a lot).
453 *
454 * Work is in progress to make it safer, in the meantime this keeps
455 * the known issue from spamming the logs.
456 */
457 return rcu_dereference_protected(active->request, 1);
Chris Wilsond72d9082016-08-04 07:52:31 +0100458}
459
Chris Wilson27c01aa2016-08-04 07:52:30 +0100460/**
Chris Wilson385384a2016-08-09 08:37:01 +0100461 * i915_gem_active_raw - return the active request
462 * @active - the active tracker
463 *
464 * i915_gem_active_raw() returns the current request being tracked, or NULL.
465 * It does not obtain a reference on the request for the caller, so the caller
466 * must hold struct_mutex.
467 */
468static inline struct drm_i915_gem_request *
469i915_gem_active_raw(const struct i915_gem_active *active, struct mutex *mutex)
470{
471 return rcu_dereference_protected(active->request,
472 lockdep_is_held(mutex));
473}
474
475/**
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100476 * i915_gem_active_peek - report the active request being monitored
Chris Wilson27c01aa2016-08-04 07:52:30 +0100477 * @active - the active tracker
478 *
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100479 * i915_gem_active_peek() returns the current request being tracked if
480 * still active, or NULL. It does not obtain a reference on the request
481 * for the caller, so the caller must hold struct_mutex.
Chris Wilson27c01aa2016-08-04 07:52:30 +0100482 */
483static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100484i915_gem_active_peek(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100485{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100486 struct drm_i915_gem_request *request;
487
Chris Wilson385384a2016-08-09 08:37:01 +0100488 request = i915_gem_active_raw(active, mutex);
Chris Wilson0eafec62016-08-04 16:32:41 +0100489 if (!request || i915_gem_request_completed(request))
490 return NULL;
491
492 return request;
493}
494
495/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100496 * i915_gem_active_get - return a reference to the active request
497 * @active - the active tracker
498 *
499 * i915_gem_active_get() returns a reference to the active request, or NULL
500 * if the active tracker is idle. The caller must hold struct_mutex.
501 */
502static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100503i915_gem_active_get(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100504{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100505 return i915_gem_request_get(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100506}
507
508/**
Chris Wilson0eafec62016-08-04 16:32:41 +0100509 * __i915_gem_active_get_rcu - return a reference to the active request
510 * @active - the active tracker
511 *
512 * __i915_gem_active_get() returns a reference to the active request, or NULL
513 * if the active tracker is idle. The caller must hold the RCU read lock, but
514 * the returned pointer is safe to use outside of RCU.
515 */
516static inline struct drm_i915_gem_request *
517__i915_gem_active_get_rcu(const struct i915_gem_active *active)
518{
519 /* Performing a lockless retrieval of the active request is super
520 * tricky. SLAB_DESTROY_BY_RCU merely guarantees that the backing
521 * slab of request objects will not be freed whilst we hold the
522 * RCU read lock. It does not guarantee that the request itself
523 * will not be freed and then *reused*. Viz,
524 *
525 * Thread A Thread B
526 *
527 * req = active.request
528 * retire(req) -> free(req);
529 * (req is now first on the slab freelist)
530 * active.request = NULL
531 *
532 * req = new submission on a new object
533 * ref(req)
534 *
535 * To prevent the request from being reused whilst the caller
536 * uses it, we take a reference like normal. Whilst acquiring
537 * the reference we check that it is not in a destroyed state
538 * (refcnt == 0). That prevents the request being reallocated
539 * whilst the caller holds on to it. To check that the request
540 * was not reallocated as we acquired the reference we have to
541 * check that our request remains the active request across
542 * the lookup, in the same manner as a seqlock. The visibility
543 * of the pointer versus the reference counting is controlled
544 * by using RCU barriers (rcu_dereference and rcu_assign_pointer).
545 *
546 * In the middle of all that, we inspect whether the request is
547 * complete. Retiring is lazy so the request may be completed long
548 * before the active tracker is updated. Querying whether the
549 * request is complete is far cheaper (as it involves no locked
550 * instructions setting cachelines to exclusive) than acquiring
551 * the reference, so we do it first. The RCU read lock ensures the
552 * pointer dereference is valid, but does not ensure that the
553 * seqno nor HWS is the right one! However, if the request was
554 * reallocated, that means the active tracker's request was complete.
555 * If the new request is also complete, then both are and we can
556 * just report the active tracker is idle. If the new request is
557 * incomplete, then we acquire a reference on it and check that
558 * it remained the active request.
Chris Wilson5a198b82016-08-09 09:23:34 +0100559 *
560 * It is then imperative that we do not zero the request on
561 * reallocation, so that we can chase the dangling pointers!
562 * See i915_gem_request_alloc().
Chris Wilson0eafec62016-08-04 16:32:41 +0100563 */
564 do {
565 struct drm_i915_gem_request *request;
566
567 request = rcu_dereference(active->request);
568 if (!request || i915_gem_request_completed(request))
569 return NULL;
570
Daniel Vetterc75870d2016-08-22 10:55:22 +0200571 /* An especially silly compiler could decide to recompute the
572 * result of i915_gem_request_completed, more specifically
573 * re-emit the load for request->fence.seqno. A race would catch
574 * a later seqno value, which could flip the result from true to
575 * false. Which means part of the instructions below might not
576 * be executed, while later on instructions are executed. Due to
577 * barriers within the refcounting the inconsistency can't reach
578 * past the call to i915_gem_request_get_rcu, but not executing
579 * that while still executing i915_gem_request_put() creates
580 * havoc enough. Prevent this with a compiler barrier.
581 */
582 barrier();
583
Chris Wilson0eafec62016-08-04 16:32:41 +0100584 request = i915_gem_request_get_rcu(request);
585
586 /* What stops the following rcu_access_pointer() from occurring
587 * before the above i915_gem_request_get_rcu()? If we were
588 * to read the value before pausing to get the reference to
589 * the request, we may not notice a change in the active
590 * tracker.
591 *
592 * The rcu_access_pointer() is a mere compiler barrier, which
593 * means both the CPU and compiler are free to perform the
594 * memory read without constraint. The compiler only has to
595 * ensure that any operations after the rcu_access_pointer()
596 * occur afterwards in program order. This means the read may
597 * be performed earlier by an out-of-order CPU, or adventurous
598 * compiler.
599 *
600 * The atomic operation at the heart of
Chris Wilsonf54d1862016-10-25 13:00:45 +0100601 * i915_gem_request_get_rcu(), see dma_fence_get_rcu(), is
Chris Wilson0eafec62016-08-04 16:32:41 +0100602 * atomic_inc_not_zero() which is only a full memory barrier
603 * when successful. That is, if i915_gem_request_get_rcu()
604 * returns the request (and so with the reference counted
605 * incremented) then the following read for rcu_access_pointer()
606 * must occur after the atomic operation and so confirm
607 * that this request is the one currently being tracked.
Chris Wilsonedf6b762016-08-09 09:23:33 +0100608 *
609 * The corresponding write barrier is part of
610 * rcu_assign_pointer().
Chris Wilson0eafec62016-08-04 16:32:41 +0100611 */
612 if (!request || request == rcu_access_pointer(active->request))
613 return rcu_pointer_handoff(request);
614
615 i915_gem_request_put(request);
616 } while (1);
617}
618
619/**
620 * i915_gem_active_get_unlocked - return a reference to the active request
621 * @active - the active tracker
622 *
623 * i915_gem_active_get_unlocked() returns a reference to the active request,
624 * or NULL if the active tracker is idle. The reference is obtained under RCU,
625 * so no locking is required by the caller.
626 *
627 * The reference should be freed with i915_gem_request_put().
628 */
629static inline struct drm_i915_gem_request *
630i915_gem_active_get_unlocked(const struct i915_gem_active *active)
631{
632 struct drm_i915_gem_request *request;
633
634 rcu_read_lock();
635 request = __i915_gem_active_get_rcu(active);
636 rcu_read_unlock();
637
638 return request;
639}
640
641/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100642 * i915_gem_active_isset - report whether the active tracker is assigned
643 * @active - the active tracker
644 *
645 * i915_gem_active_isset() returns true if the active tracker is currently
646 * assigned to a request. Due to the lazy retiring, that request may be idle
647 * and this may report stale information.
648 */
649static inline bool
650i915_gem_active_isset(const struct i915_gem_active *active)
651{
Chris Wilson0eafec62016-08-04 16:32:41 +0100652 return rcu_access_pointer(active->request);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100653}
654
655/**
Chris Wilsond07f0e52016-10-28 13:58:44 +0100656 * i915_gem_active_wait - waits until the request is completed
Chris Wilson24676582016-08-05 10:14:06 +0100657 * @active - the active request on which to wait
Chris Wilsonea746f32016-09-09 14:11:49 +0100658 * @flags - how to wait
Chris Wilson24676582016-08-05 10:14:06 +0100659 * @timeout - how long to wait at most
660 * @rps - userspace client to charge for a waitboost
661 *
Chris Wilson2e369912016-10-28 13:58:28 +0100662 * i915_gem_active_wait() waits until the request is completed before
Chris Wilson24676582016-08-05 10:14:06 +0100663 * returning, without requiring any locks to be held. Note that it does not
664 * retire any requests before returning.
665 *
666 * This function relies on RCU in order to acquire the reference to the active
667 * request without holding any locks. See __i915_gem_active_get_rcu() for the
668 * glory details on how that is managed. Once the reference is acquired, we
669 * can then wait upon the request, and afterwards release our reference,
670 * free of any locking.
671 *
672 * This function wraps i915_wait_request(), see it for the full details on
673 * the arguments.
674 *
675 * Returns 0 if successful, or a negative error code.
676 */
677static inline int
Chris Wilson2e369912016-10-28 13:58:28 +0100678i915_gem_active_wait(const struct i915_gem_active *active, unsigned int flags)
Chris Wilson24676582016-08-05 10:14:06 +0100679{
680 struct drm_i915_gem_request *request;
Chris Wilsone95433c2016-10-28 13:58:27 +0100681 long ret = 0;
Chris Wilson24676582016-08-05 10:14:06 +0100682
683 request = i915_gem_active_get_unlocked(active);
684 if (request) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100685 ret = i915_wait_request(request, flags, MAX_SCHEDULE_TIMEOUT);
Chris Wilson24676582016-08-05 10:14:06 +0100686 i915_gem_request_put(request);
687 }
688
Chris Wilsone95433c2016-10-28 13:58:27 +0100689 return ret < 0 ? ret : 0;
Chris Wilson24676582016-08-05 10:14:06 +0100690}
691
692/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100693 * i915_gem_active_retire - waits until the request is retired
694 * @active - the active request on which to wait
695 *
696 * i915_gem_active_retire() waits until the request is completed,
697 * and then ensures that at least the retirement handler for this
698 * @active tracker is called before returning. If the @active
699 * tracker is idle, the function returns immediately.
700 */
701static inline int __must_check
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100702i915_gem_active_retire(struct i915_gem_active *active,
Chris Wilsond72d9082016-08-04 07:52:31 +0100703 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100704{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100705 struct drm_i915_gem_request *request;
Chris Wilsone95433c2016-10-28 13:58:27 +0100706 long ret;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100707
Chris Wilson385384a2016-08-09 08:37:01 +0100708 request = i915_gem_active_raw(active, mutex);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100709 if (!request)
710 return 0;
711
Chris Wilson22dd3bb2016-09-09 14:11:50 +0100712 ret = i915_wait_request(request,
713 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
Chris Wilsone95433c2016-10-28 13:58:27 +0100714 MAX_SCHEDULE_TIMEOUT);
715 if (ret < 0)
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100716 return ret;
717
718 list_del_init(&active->link);
Chris Wilson0eafec62016-08-04 16:32:41 +0100719 RCU_INIT_POINTER(active->request, NULL);
720
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100721 active->retire(active, request);
722
723 return 0;
Chris Wilson27c01aa2016-08-04 07:52:30 +0100724}
725
Chris Wilson381f3712016-08-04 07:52:29 +0100726#define for_each_active(mask, idx) \
727 for (; mask ? idx = ffs(mask) - 1, 1 : 0; mask &= ~BIT(idx))
728
Chris Wilson05235c52016-07-20 09:21:08 +0100729#endif /* I915_GEM_REQUEST_H */