blob: 479896ef791e84d74fe776ba32e7f321319728b6 [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 Wilson04769652016-07-20 09:21:11 +010028#include <linux/fence.h>
29
30#include "i915_gem.h"
31
Chris Wilsondcff85c2016-08-05 10:14:11 +010032struct intel_wait {
33 struct rb_node node;
34 struct task_struct *tsk;
35 u32 seqno;
36};
37
38struct intel_signal_node {
39 struct rb_node node;
40 struct intel_wait wait;
41};
42
Chris Wilson05235c52016-07-20 09:21:08 +010043/**
44 * Request queue structure.
45 *
46 * The request queue allows us to note sequence numbers that have been emitted
47 * and may be associated with active buffers to be retired.
48 *
49 * By keeping this list, we can avoid having to do questionable sequence
50 * number comparisons on buffer last_read|write_seqno. It also allows an
51 * emission time to be associated with the request for tracking how far ahead
52 * of the GPU the submission is.
53 *
Chris Wilson5a198b82016-08-09 09:23:34 +010054 * When modifying this structure be very aware that we perform a lockless
55 * RCU lookup of it that may race against reallocation of the struct
56 * from the slab freelist. We intentionally do not zero the structure on
57 * allocation so that the lookup can use the dangling pointers (and is
58 * cogniscent that those pointers may be wrong). Instead, everything that
59 * needs to be initialised must be done so explicitly.
60 *
Chris Wilson04769652016-07-20 09:21:11 +010061 * The requests are reference counted.
Chris Wilson05235c52016-07-20 09:21:08 +010062 */
63struct drm_i915_gem_request {
Chris Wilson04769652016-07-20 09:21:11 +010064 struct fence fence;
65 spinlock_t lock;
Chris Wilson05235c52016-07-20 09:21:08 +010066
67 /** On Which ring this request was generated */
68 struct drm_i915_private *i915;
69
70 /**
71 * Context and ring buffer related to this request
72 * Contexts are refcounted, so when this request is associated with a
73 * context, we must increment the context's refcount, to guarantee that
74 * it persists while any request is linked to it. Requests themselves
75 * are also refcounted, so the request will only be freed when the last
76 * reference to it is dismissed, and the code in
77 * i915_gem_request_free() will then decrement the refcount on the
78 * context.
79 */
80 struct i915_gem_context *ctx;
81 struct intel_engine_cs *engine;
Chris Wilson7e37f882016-08-02 22:50:21 +010082 struct intel_ring *ring;
Chris Wilson05235c52016-07-20 09:21:08 +010083 struct intel_signal_node signaling;
84
85 /** GEM sequence number associated with the previous request,
86 * when the HWS breadcrumb is equal to this the GPU is processing
87 * this request.
88 */
89 u32 previous_seqno;
90
Chris Wilsona52abd22016-09-09 14:11:43 +010091 /** Position in the ring of the start of the request */
Chris Wilson05235c52016-07-20 09:21:08 +010092 u32 head;
93
94 /**
Chris Wilsona52abd22016-09-09 14:11:43 +010095 * Position in the ring of the start of the postfix.
96 * This is required to calculate the maximum available ring space
97 * without overwriting the postfix.
Chris Wilson05235c52016-07-20 09:21:08 +010098 */
99 u32 postfix;
100
Chris Wilsona52abd22016-09-09 14:11:43 +0100101 /** Position in the ring of the end of the whole request */
Chris Wilson05235c52016-07-20 09:21:08 +0100102 u32 tail;
103
Chris Wilsona52abd22016-09-09 14:11:43 +0100104 /** Position in the ring of the end of any workarounds after the tail */
105 u32 wa_tail;
106
107 /** Preallocate space in the ring for the emitting the request */
Chris Wilson05235c52016-07-20 09:21:08 +0100108 u32 reserved_space;
109
110 /**
111 * Context related to the previous request.
112 * As the contexts are accessed by the hardware until the switch is
113 * completed to a new context, the hardware may still be writing
114 * to the context object after the breadcrumb is visible. We must
115 * not unpin/unbind/prune that object whilst still active and so
116 * we keep the previous context pinned until the following (this)
117 * request is retired.
118 */
119 struct i915_gem_context *previous_context;
120
121 /** Batch buffer related to this request if any (used for
122 * error state dump only).
123 */
Chris Wilson058d88c2016-08-15 10:49:06 +0100124 struct i915_vma *batch;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100125 struct list_head active_list;
Chris Wilson05235c52016-07-20 09:21:08 +0100126
127 /** Time at which this request was emitted, in jiffies. */
128 unsigned long emitted_jiffies;
129
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100130 /** engine->request_list entry for this request */
131 struct list_head link;
Chris Wilson05235c52016-07-20 09:21:08 +0100132
Chris Wilson675d9ad2016-08-04 07:52:36 +0100133 /** ring->request_list entry for this request */
134 struct list_head ring_link;
135
Chris Wilson05235c52016-07-20 09:21:08 +0100136 struct drm_i915_file_private *file_priv;
137 /** file_priv list entry for this request */
138 struct list_head client_list;
139
Chris Wilson70c2a242016-09-09 14:11:46 +0100140 /** Link in the execlist submission queue, guarded by execlist_lock. */
Chris Wilson05235c52016-07-20 09:21:08 +0100141 struct list_head execlist_link;
Chris Wilson05235c52016-07-20 09:21:08 +0100142};
143
Chris Wilson04769652016-07-20 09:21:11 +0100144extern const struct fence_ops i915_fence_ops;
145
146static inline bool fence_is_i915(struct fence *fence)
147{
148 return fence->ops == &i915_fence_ops;
149}
150
Chris Wilson05235c52016-07-20 09:21:08 +0100151struct drm_i915_gem_request * __must_check
152i915_gem_request_alloc(struct intel_engine_cs *engine,
153 struct i915_gem_context *ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100154int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
155 struct drm_file *file);
156void i915_gem_request_retire_upto(struct drm_i915_gem_request *req);
157
158static inline u32
159i915_gem_request_get_seqno(struct drm_i915_gem_request *req)
160{
Chris Wilson04769652016-07-20 09:21:11 +0100161 return req ? req->fence.seqno : 0;
Chris Wilson05235c52016-07-20 09:21:08 +0100162}
163
164static inline struct intel_engine_cs *
165i915_gem_request_get_engine(struct drm_i915_gem_request *req)
166{
167 return req ? req->engine : NULL;
168}
169
170static inline struct drm_i915_gem_request *
Chris Wilson04769652016-07-20 09:21:11 +0100171to_request(struct fence *fence)
172{
173 /* We assume that NULL fence/request are interoperable */
174 BUILD_BUG_ON(offsetof(struct drm_i915_gem_request, fence) != 0);
175 GEM_BUG_ON(fence && !fence_is_i915(fence));
176 return container_of(fence, struct drm_i915_gem_request, fence);
177}
178
179static inline struct drm_i915_gem_request *
Chris Wilsone8a261e2016-07-20 13:31:49 +0100180i915_gem_request_get(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100181{
Chris Wilson04769652016-07-20 09:21:11 +0100182 return to_request(fence_get(&req->fence));
Chris Wilson05235c52016-07-20 09:21:08 +0100183}
184
Chris Wilson0eafec62016-08-04 16:32:41 +0100185static inline struct drm_i915_gem_request *
186i915_gem_request_get_rcu(struct drm_i915_gem_request *req)
187{
188 return to_request(fence_get_rcu(&req->fence));
189}
190
Chris Wilson05235c52016-07-20 09:21:08 +0100191static inline void
Chris Wilsone8a261e2016-07-20 13:31:49 +0100192i915_gem_request_put(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100193{
Chris Wilson04769652016-07-20 09:21:11 +0100194 fence_put(&req->fence);
Chris Wilson05235c52016-07-20 09:21:08 +0100195}
196
197static inline void i915_gem_request_assign(struct drm_i915_gem_request **pdst,
198 struct drm_i915_gem_request *src)
199{
200 if (src)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100201 i915_gem_request_get(src);
Chris Wilson05235c52016-07-20 09:21:08 +0100202
203 if (*pdst)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100204 i915_gem_request_put(*pdst);
Chris Wilson05235c52016-07-20 09:21:08 +0100205
206 *pdst = src;
207}
208
Chris Wilson17f298cf2016-08-10 13:41:46 +0100209void __i915_add_request(struct drm_i915_gem_request *req, bool flush_caches);
Chris Wilson05235c52016-07-20 09:21:08 +0100210#define i915_add_request(req) \
Chris Wilson17f298cf2016-08-10 13:41:46 +0100211 __i915_add_request(req, true)
Chris Wilson05235c52016-07-20 09:21:08 +0100212#define i915_add_request_no_flush(req) \
Chris Wilson17f298cf2016-08-10 13:41:46 +0100213 __i915_add_request(req, false)
Chris Wilson05235c52016-07-20 09:21:08 +0100214
215struct intel_rps_client;
Chris Wilson42df2712016-07-20 09:21:12 +0100216#define NO_WAITBOOST ERR_PTR(-1)
217#define IS_RPS_CLIENT(p) (!IS_ERR(p))
218#define IS_RPS_USER(p) (!IS_ERR_OR_NULL(p))
Chris Wilson05235c52016-07-20 09:21:08 +0100219
Chris Wilson776f3232016-08-04 07:52:40 +0100220int i915_wait_request(struct drm_i915_gem_request *req,
Chris Wilsonea746f32016-09-09 14:11:49 +0100221 unsigned int flags,
Chris Wilson776f3232016-08-04 07:52:40 +0100222 s64 *timeout,
223 struct intel_rps_client *rps)
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100224 __attribute__((nonnull(1)));
Chris Wilsonea746f32016-09-09 14:11:49 +0100225#define I915_WAIT_INTERRUPTIBLE BIT(0)
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100226
Chris Wilson05235c52016-07-20 09:21:08 +0100227static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine);
228
229/**
230 * Returns true if seq1 is later than seq2.
231 */
232static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
233{
234 return (s32)(seq1 - seq2) >= 0;
235}
236
237static inline bool
238i915_gem_request_started(const struct drm_i915_gem_request *req)
239{
240 return i915_seqno_passed(intel_engine_get_seqno(req->engine),
241 req->previous_seqno);
242}
243
244static inline bool
245i915_gem_request_completed(const struct drm_i915_gem_request *req)
246{
247 return i915_seqno_passed(intel_engine_get_seqno(req->engine),
Chris Wilson04769652016-07-20 09:21:11 +0100248 req->fence.seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100249}
250
251bool __i915_spin_request(const struct drm_i915_gem_request *request,
252 int state, unsigned long timeout_us);
253static inline bool i915_spin_request(const struct drm_i915_gem_request *request,
254 int state, unsigned long timeout_us)
255{
256 return (i915_gem_request_started(request) &&
257 __i915_spin_request(request, state, timeout_us));
258}
259
Chris Wilson381f3712016-08-04 07:52:29 +0100260/* We treat requests as fences. This is not be to confused with our
261 * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
262 * We use the fences to synchronize access from the CPU with activity on the
263 * GPU, for example, we should not rewrite an object's PTE whilst the GPU
264 * is reading them. We also track fences at a higher level to provide
265 * implicit synchronisation around GEM objects, e.g. set-domain will wait
266 * for outstanding GPU rendering before marking the object ready for CPU
267 * access, or a pageflip will wait until the GPU is complete before showing
268 * the frame on the scanout.
269 *
270 * In order to use a fence, the object must track the fence it needs to
271 * serialise with. For example, GEM objects want to track both read and
272 * write access so that we can perform concurrent read operations between
273 * the CPU and GPU engines, as well as waiting for all rendering to
274 * complete, or waiting for the last GPU user of a "fence register". The
275 * object then embeds a #i915_gem_active to track the most recent (in
276 * retirement order) request relevant for the desired mode of access.
277 * The #i915_gem_active is updated with i915_gem_active_set() to track the
278 * most recent fence request, typically this is done as part of
279 * i915_vma_move_to_active().
280 *
281 * When the #i915_gem_active completes (is retired), it will
282 * signal its completion to the owner through a callback as well as mark
283 * itself as idle (i915_gem_active.request == NULL). The owner
284 * can then perform any action, such as delayed freeing of an active
285 * resource including itself.
286 */
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100287struct i915_gem_active;
288
289typedef void (*i915_gem_retire_fn)(struct i915_gem_active *,
290 struct drm_i915_gem_request *);
291
Chris Wilson381f3712016-08-04 07:52:29 +0100292struct i915_gem_active {
Chris Wilson0eafec62016-08-04 16:32:41 +0100293 struct drm_i915_gem_request __rcu *request;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100294 struct list_head link;
295 i915_gem_retire_fn retire;
Chris Wilson381f3712016-08-04 07:52:29 +0100296};
297
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100298void i915_gem_retire_noop(struct i915_gem_active *,
299 struct drm_i915_gem_request *request);
300
301/**
302 * init_request_active - prepares the activity tracker for use
303 * @active - the active tracker
304 * @func - a callback when then the tracker is retired (becomes idle),
305 * can be NULL
306 *
307 * init_request_active() prepares the embedded @active struct for use as
308 * an activity tracker, that is for tracking the last known active request
309 * associated with it. When the last request becomes idle, when it is retired
310 * after completion, the optional callback @func is invoked.
311 */
312static inline void
313init_request_active(struct i915_gem_active *active,
314 i915_gem_retire_fn retire)
315{
316 INIT_LIST_HEAD(&active->link);
317 active->retire = retire ?: i915_gem_retire_noop;
318}
319
Chris Wilson27c01aa2016-08-04 07:52:30 +0100320/**
321 * i915_gem_active_set - updates the tracker to watch the current request
322 * @active - the active tracker
323 * @request - the request to watch
324 *
325 * i915_gem_active_set() watches the given @request for completion. Whilst
326 * that @request is busy, the @active reports busy. When that @request is
327 * retired, the @active tracker is updated to report idle.
328 */
Chris Wilson381f3712016-08-04 07:52:29 +0100329static inline void
330i915_gem_active_set(struct i915_gem_active *active,
331 struct drm_i915_gem_request *request)
332{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100333 list_move(&active->link, &request->active_list);
Chris Wilson0eafec62016-08-04 16:32:41 +0100334 rcu_assign_pointer(active->request, request);
Chris Wilson381f3712016-08-04 07:52:29 +0100335}
336
Chris Wilsond72d9082016-08-04 07:52:31 +0100337static inline struct drm_i915_gem_request *
338__i915_gem_active_peek(const struct i915_gem_active *active)
339{
Chris Wilson0eafec62016-08-04 16:32:41 +0100340 /* Inside the error capture (running with the driver in an unknown
341 * state), we want to bend the rules slightly (a lot).
342 *
343 * Work is in progress to make it safer, in the meantime this keeps
344 * the known issue from spamming the logs.
345 */
346 return rcu_dereference_protected(active->request, 1);
Chris Wilsond72d9082016-08-04 07:52:31 +0100347}
348
Chris Wilson27c01aa2016-08-04 07:52:30 +0100349/**
Chris Wilson385384a2016-08-09 08:37:01 +0100350 * i915_gem_active_raw - return the active request
351 * @active - the active tracker
352 *
353 * i915_gem_active_raw() returns the current request being tracked, or NULL.
354 * It does not obtain a reference on the request for the caller, so the caller
355 * must hold struct_mutex.
356 */
357static inline struct drm_i915_gem_request *
358i915_gem_active_raw(const struct i915_gem_active *active, struct mutex *mutex)
359{
360 return rcu_dereference_protected(active->request,
361 lockdep_is_held(mutex));
362}
363
364/**
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100365 * i915_gem_active_peek - report the active request being monitored
Chris Wilson27c01aa2016-08-04 07:52:30 +0100366 * @active - the active tracker
367 *
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100368 * i915_gem_active_peek() returns the current request being tracked if
369 * still active, or NULL. It does not obtain a reference on the request
370 * for the caller, so the caller must hold struct_mutex.
Chris Wilson27c01aa2016-08-04 07:52:30 +0100371 */
372static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100373i915_gem_active_peek(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100374{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100375 struct drm_i915_gem_request *request;
376
Chris Wilson385384a2016-08-09 08:37:01 +0100377 request = i915_gem_active_raw(active, mutex);
Chris Wilson0eafec62016-08-04 16:32:41 +0100378 if (!request || i915_gem_request_completed(request))
379 return NULL;
380
381 return request;
382}
383
384/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100385 * i915_gem_active_get - return a reference to the active request
386 * @active - the active tracker
387 *
388 * i915_gem_active_get() returns a reference to the active request, or NULL
389 * if the active tracker is idle. The caller must hold struct_mutex.
390 */
391static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100392i915_gem_active_get(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100393{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100394 return i915_gem_request_get(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100395}
396
397/**
Chris Wilson0eafec62016-08-04 16:32:41 +0100398 * __i915_gem_active_get_rcu - return a reference to the active request
399 * @active - the active tracker
400 *
401 * __i915_gem_active_get() returns a reference to the active request, or NULL
402 * if the active tracker is idle. The caller must hold the RCU read lock, but
403 * the returned pointer is safe to use outside of RCU.
404 */
405static inline struct drm_i915_gem_request *
406__i915_gem_active_get_rcu(const struct i915_gem_active *active)
407{
408 /* Performing a lockless retrieval of the active request is super
409 * tricky. SLAB_DESTROY_BY_RCU merely guarantees that the backing
410 * slab of request objects will not be freed whilst we hold the
411 * RCU read lock. It does not guarantee that the request itself
412 * will not be freed and then *reused*. Viz,
413 *
414 * Thread A Thread B
415 *
416 * req = active.request
417 * retire(req) -> free(req);
418 * (req is now first on the slab freelist)
419 * active.request = NULL
420 *
421 * req = new submission on a new object
422 * ref(req)
423 *
424 * To prevent the request from being reused whilst the caller
425 * uses it, we take a reference like normal. Whilst acquiring
426 * the reference we check that it is not in a destroyed state
427 * (refcnt == 0). That prevents the request being reallocated
428 * whilst the caller holds on to it. To check that the request
429 * was not reallocated as we acquired the reference we have to
430 * check that our request remains the active request across
431 * the lookup, in the same manner as a seqlock. The visibility
432 * of the pointer versus the reference counting is controlled
433 * by using RCU barriers (rcu_dereference and rcu_assign_pointer).
434 *
435 * In the middle of all that, we inspect whether the request is
436 * complete. Retiring is lazy so the request may be completed long
437 * before the active tracker is updated. Querying whether the
438 * request is complete is far cheaper (as it involves no locked
439 * instructions setting cachelines to exclusive) than acquiring
440 * the reference, so we do it first. The RCU read lock ensures the
441 * pointer dereference is valid, but does not ensure that the
442 * seqno nor HWS is the right one! However, if the request was
443 * reallocated, that means the active tracker's request was complete.
444 * If the new request is also complete, then both are and we can
445 * just report the active tracker is idle. If the new request is
446 * incomplete, then we acquire a reference on it and check that
447 * it remained the active request.
Chris Wilson5a198b82016-08-09 09:23:34 +0100448 *
449 * It is then imperative that we do not zero the request on
450 * reallocation, so that we can chase the dangling pointers!
451 * See i915_gem_request_alloc().
Chris Wilson0eafec62016-08-04 16:32:41 +0100452 */
453 do {
454 struct drm_i915_gem_request *request;
455
456 request = rcu_dereference(active->request);
457 if (!request || i915_gem_request_completed(request))
458 return NULL;
459
Daniel Vetterc75870d2016-08-22 10:55:22 +0200460 /* An especially silly compiler could decide to recompute the
461 * result of i915_gem_request_completed, more specifically
462 * re-emit the load for request->fence.seqno. A race would catch
463 * a later seqno value, which could flip the result from true to
464 * false. Which means part of the instructions below might not
465 * be executed, while later on instructions are executed. Due to
466 * barriers within the refcounting the inconsistency can't reach
467 * past the call to i915_gem_request_get_rcu, but not executing
468 * that while still executing i915_gem_request_put() creates
469 * havoc enough. Prevent this with a compiler barrier.
470 */
471 barrier();
472
Chris Wilson0eafec62016-08-04 16:32:41 +0100473 request = i915_gem_request_get_rcu(request);
474
475 /* What stops the following rcu_access_pointer() from occurring
476 * before the above i915_gem_request_get_rcu()? If we were
477 * to read the value before pausing to get the reference to
478 * the request, we may not notice a change in the active
479 * tracker.
480 *
481 * The rcu_access_pointer() is a mere compiler barrier, which
482 * means both the CPU and compiler are free to perform the
483 * memory read without constraint. The compiler only has to
484 * ensure that any operations after the rcu_access_pointer()
485 * occur afterwards in program order. This means the read may
486 * be performed earlier by an out-of-order CPU, or adventurous
487 * compiler.
488 *
489 * The atomic operation at the heart of
490 * i915_gem_request_get_rcu(), see fence_get_rcu(), is
491 * atomic_inc_not_zero() which is only a full memory barrier
492 * when successful. That is, if i915_gem_request_get_rcu()
493 * returns the request (and so with the reference counted
494 * incremented) then the following read for rcu_access_pointer()
495 * must occur after the atomic operation and so confirm
496 * that this request is the one currently being tracked.
Chris Wilsonedf6b762016-08-09 09:23:33 +0100497 *
498 * The corresponding write barrier is part of
499 * rcu_assign_pointer().
Chris Wilson0eafec62016-08-04 16:32:41 +0100500 */
501 if (!request || request == rcu_access_pointer(active->request))
502 return rcu_pointer_handoff(request);
503
504 i915_gem_request_put(request);
505 } while (1);
506}
507
508/**
509 * i915_gem_active_get_unlocked - return a reference to the active request
510 * @active - the active tracker
511 *
512 * i915_gem_active_get_unlocked() returns a reference to the active request,
513 * or NULL if the active tracker is idle. The reference is obtained under RCU,
514 * so no locking is required by the caller.
515 *
516 * The reference should be freed with i915_gem_request_put().
517 */
518static inline struct drm_i915_gem_request *
519i915_gem_active_get_unlocked(const struct i915_gem_active *active)
520{
521 struct drm_i915_gem_request *request;
522
523 rcu_read_lock();
524 request = __i915_gem_active_get_rcu(active);
525 rcu_read_unlock();
526
527 return request;
528}
529
530/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100531 * i915_gem_active_isset - report whether the active tracker is assigned
532 * @active - the active tracker
533 *
534 * i915_gem_active_isset() returns true if the active tracker is currently
535 * assigned to a request. Due to the lazy retiring, that request may be idle
536 * and this may report stale information.
537 */
538static inline bool
539i915_gem_active_isset(const struct i915_gem_active *active)
540{
Chris Wilson0eafec62016-08-04 16:32:41 +0100541 return rcu_access_pointer(active->request);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100542}
543
544/**
545 * i915_gem_active_is_idle - report whether the active tracker is idle
546 * @active - the active tracker
547 *
548 * i915_gem_active_is_idle() returns true if the active tracker is currently
549 * unassigned or if the request is complete (but not yet retired). Requires
550 * the caller to hold struct_mutex (but that can be relaxed if desired).
551 */
552static inline bool
Chris Wilsond72d9082016-08-04 07:52:31 +0100553i915_gem_active_is_idle(const struct i915_gem_active *active,
554 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100555{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100556 return !i915_gem_active_peek(active, mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100557}
558
559/**
560 * i915_gem_active_wait - waits until the request is completed
561 * @active - the active request on which to wait
562 *
563 * i915_gem_active_wait() waits until the request is completed before
564 * returning. Note that it does not guarantee that the request is
565 * retired first, see i915_gem_active_retire().
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100566 *
567 * i915_gem_active_wait() returns immediately if the active
568 * request is already complete.
Chris Wilson27c01aa2016-08-04 07:52:30 +0100569 */
570static inline int __must_check
Chris Wilsond72d9082016-08-04 07:52:31 +0100571i915_gem_active_wait(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100572{
573 struct drm_i915_gem_request *request;
574
Chris Wilsond72d9082016-08-04 07:52:31 +0100575 request = i915_gem_active_peek(active, mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100576 if (!request)
577 return 0;
578
Chris Wilsonea746f32016-09-09 14:11:49 +0100579 return i915_wait_request(request, I915_WAIT_INTERRUPTIBLE, NULL, NULL);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100580}
581
582/**
Chris Wilson24676582016-08-05 10:14:06 +0100583 * i915_gem_active_wait_unlocked - waits until the request is completed
584 * @active - the active request on which to wait
Chris Wilsonea746f32016-09-09 14:11:49 +0100585 * @flags - how to wait
Chris Wilson24676582016-08-05 10:14:06 +0100586 * @timeout - how long to wait at most
587 * @rps - userspace client to charge for a waitboost
588 *
589 * i915_gem_active_wait_unlocked() waits until the request is completed before
590 * returning, without requiring any locks to be held. Note that it does not
591 * retire any requests before returning.
592 *
593 * This function relies on RCU in order to acquire the reference to the active
594 * request without holding any locks. See __i915_gem_active_get_rcu() for the
595 * glory details on how that is managed. Once the reference is acquired, we
596 * can then wait upon the request, and afterwards release our reference,
597 * free of any locking.
598 *
599 * This function wraps i915_wait_request(), see it for the full details on
600 * the arguments.
601 *
602 * Returns 0 if successful, or a negative error code.
603 */
604static inline int
605i915_gem_active_wait_unlocked(const struct i915_gem_active *active,
Chris Wilsonea746f32016-09-09 14:11:49 +0100606 unsigned int flags,
Chris Wilson24676582016-08-05 10:14:06 +0100607 s64 *timeout,
608 struct intel_rps_client *rps)
609{
610 struct drm_i915_gem_request *request;
611 int ret = 0;
612
613 request = i915_gem_active_get_unlocked(active);
614 if (request) {
Chris Wilsonea746f32016-09-09 14:11:49 +0100615 ret = i915_wait_request(request, flags, timeout, rps);
Chris Wilson24676582016-08-05 10:14:06 +0100616 i915_gem_request_put(request);
617 }
618
619 return ret;
620}
621
622/**
Chris Wilson27c01aa2016-08-04 07:52:30 +0100623 * i915_gem_active_retire - waits until the request is retired
624 * @active - the active request on which to wait
625 *
626 * i915_gem_active_retire() waits until the request is completed,
627 * and then ensures that at least the retirement handler for this
628 * @active tracker is called before returning. If the @active
629 * tracker is idle, the function returns immediately.
630 */
631static inline int __must_check
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100632i915_gem_active_retire(struct i915_gem_active *active,
Chris Wilsond72d9082016-08-04 07:52:31 +0100633 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100634{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100635 struct drm_i915_gem_request *request;
636 int ret;
637
Chris Wilson385384a2016-08-09 08:37:01 +0100638 request = i915_gem_active_raw(active, mutex);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100639 if (!request)
640 return 0;
641
Chris Wilsonea746f32016-09-09 14:11:49 +0100642 ret = i915_wait_request(request, I915_WAIT_INTERRUPTIBLE, NULL, NULL);
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100643 if (ret)
644 return ret;
645
646 list_del_init(&active->link);
Chris Wilson0eafec62016-08-04 16:32:41 +0100647 RCU_INIT_POINTER(active->request, NULL);
648
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100649 active->retire(active, request);
650
651 return 0;
Chris Wilson27c01aa2016-08-04 07:52:30 +0100652}
653
654/* Convenience functions for peeking at state inside active's request whilst
655 * guarded by the struct_mutex.
656 */
657
658static inline uint32_t
Chris Wilsond72d9082016-08-04 07:52:31 +0100659i915_gem_active_get_seqno(const struct i915_gem_active *active,
660 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100661{
Chris Wilsond72d9082016-08-04 07:52:31 +0100662 return i915_gem_request_get_seqno(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100663}
664
665static inline struct intel_engine_cs *
Chris Wilsond72d9082016-08-04 07:52:31 +0100666i915_gem_active_get_engine(const struct i915_gem_active *active,
667 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100668{
Chris Wilsond72d9082016-08-04 07:52:31 +0100669 return i915_gem_request_get_engine(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100670}
671
Chris Wilson381f3712016-08-04 07:52:29 +0100672#define for_each_active(mask, idx) \
673 for (; mask ? idx = ffs(mask) - 1, 1 : 0; mask &= ~BIT(idx))
674
Chris Wilson05235c52016-07-20 09:21:08 +0100675#endif /* I915_GEM_REQUEST_H */