1 /*
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
28 #include <linux/dma-fence.h>
29
30 #include "i915_gem.h"
31 #include "i915_sw_fence.h"
32
33 struct drm_file;
34 struct drm_i915_gem_object;
35 struct drm_i915_gem_request;
36
37 struct intel_wait {
38 struct rb_node node;
39 struct task_struct *tsk;
40 struct drm_i915_gem_request *request;
41 u32 seqno;
42 };
43
44 struct intel_signal_node {
45 struct rb_node node;
46 struct intel_wait wait;
47 };
48
49 struct i915_dependency {
50 struct i915_priotree *signaler;
51 struct list_head signal_link;
52 struct list_head wait_link;
53 struct list_head dfs_link;
54 unsigned long flags;
55 #define I915_DEPENDENCY_ALLOC BIT(0)
56 };
57
58 /* Requests exist in a complex web of interdependencies. Each request
59 * has to wait for some other request to complete before it is ready to be run
60 * (e.g. we have to wait until the pixels have been rendering into a texture
61 * before we can copy from it). We track the readiness of a request in terms
62 * of fences, but we also need to keep the dependency tree for the lifetime
63 * of the request (beyond the life of an individual fence). We use the tree
64 * at various points to reorder the requests whilst keeping the requests
65 * in order with respect to their various dependencies.
66 */
67 struct i915_priotree {
68 struct list_head signalers_list; /* those before us, we depend upon */
69 struct list_head waiters_list; /* those after us, they depend upon us */
70 struct list_head link;
71 int priority;
72 #define I915_PRIORITY_MAX 1024
73 #define I915_PRIORITY_NORMAL 0
74 #define I915_PRIORITY_MIN (-I915_PRIORITY_MAX)
75 };
76
77 struct i915_gem_capture_list {
78 struct i915_gem_capture_list *next;
79 struct i915_vma *vma;
80 };
81
82 /**
83 * Request queue structure.
84 *
85 * The request queue allows us to note sequence numbers that have been emitted
86 * and may be associated with active buffers to be retired.
87 *
88 * By keeping this list, we can avoid having to do questionable sequence
89 * number comparisons on buffer last_read|write_seqno. It also allows an
90 * emission time to be associated with the request for tracking how far ahead
91 * of the GPU the submission is.
92 *
93 * When modifying this structure be very aware that we perform a lockless
94 * RCU lookup of it that may race against reallocation of the struct
95 * from the slab freelist. We intentionally do not zero the structure on
96 * allocation so that the lookup can use the dangling pointers (and is
97 * cogniscent that those pointers may be wrong). Instead, everything that
98 * needs to be initialised must be done so explicitly.
99 *
100 * The requests are reference counted.
101 */
102 struct drm_i915_gem_request {
103 struct dma_fence fence;
104 spinlock_t lock;
105
106 /** On Which ring this request was generated */
107 struct drm_i915_private *i915;
108
109 /**
110 * Context and ring buffer related to this request
111 * Contexts are refcounted, so when this request is associated with a
112 * context, we must increment the context's refcount, to guarantee that
113 * it persists while any request is linked to it. Requests themselves
114 * are also refcounted, so the request will only be freed when the last
115 * reference to it is dismissed, and the code in
116 * i915_gem_request_free() will then decrement the refcount on the
117 * context.
118 */
119 struct i915_gem_context *ctx;
120 struct intel_engine_cs *engine;
121 struct intel_ring *ring;
122 struct intel_timeline *timeline;
123 struct intel_signal_node signaling;
124
125 /* Fences for the various phases in the request's lifetime.
126 *
127 * The submit fence is used to await upon all of the request's
128 * dependencies. When it is signaled, the request is ready to run.
129 * It is used by the driver to then queue the request for execution.
130 */
131 struct i915_sw_fence submit;
132 wait_queue_entry_t submitq;
133 wait_queue_head_t execute;
134
135 /* A list of everyone we wait upon, and everyone who waits upon us.
136 * Even though we will not be submitted to the hardware before the
137 * submit fence is signaled (it waits for all external events as well
138 * as our own requests), the scheduler still needs to know the
139 * dependency tree for the lifetime of the request (from execbuf
140 * to retirement), i.e. bidirectional dependency information for the
141 * request not tied to individual fences.
142 */
143 struct i915_priotree priotree;
144 struct i915_dependency dep;
145
146 /** GEM sequence number associated with this request on the
147 * global execution timeline. It is zero when the request is not
148 * on the HW queue (i.e. not on the engine timeline list).
149 * Its value is guarded by the timeline spinlock.
150 */
151 u32 global_seqno;
152
153 /** Position in the ring of the start of the request */
154 u32 head;
155
156 /**
157 * Position in the ring of the start of the postfix.
158 * This is required to calculate the maximum available ring space
159 * without overwriting the postfix.
160 */
161 u32 postfix;
162
163 /** Position in the ring of the end of the whole request */
164 u32 tail;
165
166 /** Position in the ring of the end of any workarounds after the tail */
167 u32 wa_tail;
168
169 /** Preallocate space in the ring for the emitting the request */
170 u32 reserved_space;
171
172 /** Batch buffer related to this request if any (used for
173 * error state dump only).
174 */
175 struct i915_vma *batch;
176 /** Additional buffers requested by userspace to be captured upon
177 * a GPU hang. The vma/obj on this list are protected by their
178 * active reference - all objects on this list must also be
179 * on the active_list (of their final request).
180 */
181 struct i915_gem_capture_list *capture_list;
182 struct list_head active_list;
183
184 /** Time at which this request was emitted, in jiffies. */
185 unsigned long emitted_jiffies;
186
187 bool waitboost;
188
189 /** engine->request_list entry for this request */
190 struct list_head link;
191
192 /** ring->request_list entry for this request */
193 struct list_head ring_link;
194
195 struct drm_i915_file_private *file_priv;
196 /** file_priv list entry for this request */
197 struct list_head client_link;
198 };
199
200 extern const struct dma_fence_ops i915_fence_ops;
201
dma_fence_is_i915(const struct dma_fence * fence)202 static inline bool dma_fence_is_i915(const struct dma_fence *fence)
203 {
204 return fence->ops == &i915_fence_ops;
205 }
206
207 struct drm_i915_gem_request * __must_check
208 i915_gem_request_alloc(struct intel_engine_cs *engine,
209 struct i915_gem_context *ctx);
210 void i915_gem_request_retire_upto(struct drm_i915_gem_request *req);
211
212 static inline struct drm_i915_gem_request *
to_request(struct dma_fence * fence)213 to_request(struct dma_fence *fence)
214 {
215 /* We assume that NULL fence/request are interoperable */
216 BUILD_BUG_ON(offsetof(struct drm_i915_gem_request, fence) != 0);
217 GEM_BUG_ON(fence && !dma_fence_is_i915(fence));
218 return container_of(fence, struct drm_i915_gem_request, fence);
219 }
220
221 static inline struct drm_i915_gem_request *
i915_gem_request_get(struct drm_i915_gem_request * req)222 i915_gem_request_get(struct drm_i915_gem_request *req)
223 {
224 return to_request(dma_fence_get(&req->fence));
225 }
226
227 static inline struct drm_i915_gem_request *
i915_gem_request_get_rcu(struct drm_i915_gem_request * req)228 i915_gem_request_get_rcu(struct drm_i915_gem_request *req)
229 {
230 return to_request(dma_fence_get_rcu(&req->fence));
231 }
232
233 static inline void
i915_gem_request_put(struct drm_i915_gem_request * req)234 i915_gem_request_put(struct drm_i915_gem_request *req)
235 {
236 dma_fence_put(&req->fence);
237 }
238
i915_gem_request_assign(struct drm_i915_gem_request ** pdst,struct drm_i915_gem_request * src)239 static inline void i915_gem_request_assign(struct drm_i915_gem_request **pdst,
240 struct drm_i915_gem_request *src)
241 {
242 if (src)
243 i915_gem_request_get(src);
244
245 if (*pdst)
246 i915_gem_request_put(*pdst);
247
248 *pdst = src;
249 }
250
251 /**
252 * i915_gem_request_global_seqno - report the current global seqno
253 * @request - the request
254 *
255 * A request is assigned a global seqno only when it is on the hardware
256 * execution queue. The global seqno can be used to maintain a list of
257 * requests on the same engine in retirement order, for example for
258 * constructing a priority queue for waiting. Prior to its execution, or
259 * if it is subsequently removed in the event of preemption, its global
260 * seqno is zero. As both insertion and removal from the execution queue
261 * may operate in IRQ context, it is not guarded by the usual struct_mutex
262 * BKL. Instead those relying on the global seqno must be prepared for its
263 * value to change between reads. Only when the request is complete can
264 * the global seqno be stable (due to the memory barriers on submitting
265 * the commands to the hardware to write the breadcrumb, if the HWS shows
266 * that it has passed the global seqno and the global seqno is unchanged
267 * after the read, it is indeed complete).
268 */
269 static u32
i915_gem_request_global_seqno(const struct drm_i915_gem_request * request)270 i915_gem_request_global_seqno(const struct drm_i915_gem_request *request)
271 {
272 return READ_ONCE(request->global_seqno);
273 }
274
275 int
276 i915_gem_request_await_object(struct drm_i915_gem_request *to,
277 struct drm_i915_gem_object *obj,
278 bool write);
279 int i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
280 struct dma_fence *fence);
281
282 void __i915_add_request(struct drm_i915_gem_request *req, bool flush_caches);
283 #define i915_add_request(req) \
284 __i915_add_request(req, false)
285
286 void __i915_gem_request_submit(struct drm_i915_gem_request *request);
287 void i915_gem_request_submit(struct drm_i915_gem_request *request);
288
289 void __i915_gem_request_unsubmit(struct drm_i915_gem_request *request);
290 void i915_gem_request_unsubmit(struct drm_i915_gem_request *request);
291
292 struct intel_rps_client;
293 #define NO_WAITBOOST ERR_PTR(-1)
294 #define IS_RPS_CLIENT(p) (!IS_ERR(p))
295 #define IS_RPS_USER(p) (!IS_ERR_OR_NULL(p))
296
297 long i915_wait_request(struct drm_i915_gem_request *req,
298 unsigned int flags,
299 long timeout)
300 __attribute__((nonnull(1)));
301 #define I915_WAIT_INTERRUPTIBLE BIT(0)
302 #define I915_WAIT_LOCKED BIT(1) /* struct_mutex held, handle GPU reset */
303 #define I915_WAIT_ALL BIT(2) /* used by i915_gem_object_wait() */
304
305 static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine);
306
307 /**
308 * Returns true if seq1 is later than seq2.
309 */
i915_seqno_passed(u32 seq1,u32 seq2)310 static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
311 {
312 return (s32)(seq1 - seq2) >= 0;
313 }
314
315 static inline bool
__i915_gem_request_started(const struct drm_i915_gem_request * req,u32 seqno)316 __i915_gem_request_started(const struct drm_i915_gem_request *req, u32 seqno)
317 {
318 GEM_BUG_ON(!seqno);
319 return i915_seqno_passed(intel_engine_get_seqno(req->engine),
320 seqno - 1);
321 }
322
323 static inline bool
i915_gem_request_started(const struct drm_i915_gem_request * req)324 i915_gem_request_started(const struct drm_i915_gem_request *req)
325 {
326 u32 seqno;
327
328 seqno = i915_gem_request_global_seqno(req);
329 if (!seqno)
330 return false;
331
332 return __i915_gem_request_started(req, seqno);
333 }
334
335 static inline bool
__i915_gem_request_completed(const struct drm_i915_gem_request * req,u32 seqno)336 __i915_gem_request_completed(const struct drm_i915_gem_request *req, u32 seqno)
337 {
338 GEM_BUG_ON(!seqno);
339 return i915_seqno_passed(intel_engine_get_seqno(req->engine), seqno) &&
340 seqno == i915_gem_request_global_seqno(req);
341 }
342
343 static inline bool
i915_gem_request_completed(const struct drm_i915_gem_request * req)344 i915_gem_request_completed(const struct drm_i915_gem_request *req)
345 {
346 u32 seqno;
347
348 seqno = i915_gem_request_global_seqno(req);
349 if (!seqno)
350 return false;
351
352 return __i915_gem_request_completed(req, seqno);
353 }
354
355 bool __i915_spin_request(const struct drm_i915_gem_request *request,
356 u32 seqno, int state, unsigned long timeout_us);
i915_spin_request(const struct drm_i915_gem_request * request,int state,unsigned long timeout_us)357 static inline bool i915_spin_request(const struct drm_i915_gem_request *request,
358 int state, unsigned long timeout_us)
359 {
360 u32 seqno;
361
362 seqno = i915_gem_request_global_seqno(request);
363 if (!seqno)
364 return 0;
365
366 return (__i915_gem_request_started(request, seqno) &&
367 __i915_spin_request(request, seqno, state, timeout_us));
368 }
369
370 /* We treat requests as fences. This is not be to confused with our
371 * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
372 * We use the fences to synchronize access from the CPU with activity on the
373 * GPU, for example, we should not rewrite an object's PTE whilst the GPU
374 * is reading them. We also track fences at a higher level to provide
375 * implicit synchronisation around GEM objects, e.g. set-domain will wait
376 * for outstanding GPU rendering before marking the object ready for CPU
377 * access, or a pageflip will wait until the GPU is complete before showing
378 * the frame on the scanout.
379 *
380 * In order to use a fence, the object must track the fence it needs to
381 * serialise with. For example, GEM objects want to track both read and
382 * write access so that we can perform concurrent read operations between
383 * the CPU and GPU engines, as well as waiting for all rendering to
384 * complete, or waiting for the last GPU user of a "fence register". The
385 * object then embeds a #i915_gem_active to track the most recent (in
386 * retirement order) request relevant for the desired mode of access.
387 * The #i915_gem_active is updated with i915_gem_active_set() to track the
388 * most recent fence request, typically this is done as part of
389 * i915_vma_move_to_active().
390 *
391 * When the #i915_gem_active completes (is retired), it will
392 * signal its completion to the owner through a callback as well as mark
393 * itself as idle (i915_gem_active.request == NULL). The owner
394 * can then perform any action, such as delayed freeing of an active
395 * resource including itself.
396 */
397 struct i915_gem_active;
398
399 typedef void (*i915_gem_retire_fn)(struct i915_gem_active *,
400 struct drm_i915_gem_request *);
401
402 struct i915_gem_active {
403 struct drm_i915_gem_request __rcu *request;
404 struct list_head link;
405 i915_gem_retire_fn retire;
406 };
407
408 void i915_gem_retire_noop(struct i915_gem_active *,
409 struct drm_i915_gem_request *request);
410
411 /**
412 * init_request_active - prepares the activity tracker for use
413 * @active - the active tracker
414 * @func - a callback when then the tracker is retired (becomes idle),
415 * can be NULL
416 *
417 * init_request_active() prepares the embedded @active struct for use as
418 * an activity tracker, that is for tracking the last known active request
419 * associated with it. When the last request becomes idle, when it is retired
420 * after completion, the optional callback @func is invoked.
421 */
422 static inline void
init_request_active(struct i915_gem_active * active,i915_gem_retire_fn retire)423 init_request_active(struct i915_gem_active *active,
424 i915_gem_retire_fn retire)
425 {
426 INIT_LIST_HEAD(&active->link);
427 active->retire = retire ?: i915_gem_retire_noop;
428 }
429
430 /**
431 * i915_gem_active_set - updates the tracker to watch the current request
432 * @active - the active tracker
433 * @request - the request to watch
434 *
435 * i915_gem_active_set() watches the given @request for completion. Whilst
436 * that @request is busy, the @active reports busy. When that @request is
437 * retired, the @active tracker is updated to report idle.
438 */
439 static inline void
i915_gem_active_set(struct i915_gem_active * active,struct drm_i915_gem_request * request)440 i915_gem_active_set(struct i915_gem_active *active,
441 struct drm_i915_gem_request *request)
442 {
443 list_move(&active->link, &request->active_list);
444 rcu_assign_pointer(active->request, request);
445 }
446
447 /**
448 * i915_gem_active_set_retire_fn - updates the retirement callback
449 * @active - the active tracker
450 * @fn - the routine called when the request is retired
451 * @mutex - struct_mutex used to guard retirements
452 *
453 * i915_gem_active_set_retire_fn() updates the function pointer that
454 * is called when the final request associated with the @active tracker
455 * is retired.
456 */
457 static inline void
i915_gem_active_set_retire_fn(struct i915_gem_active * active,i915_gem_retire_fn fn,struct mutex * mutex)458 i915_gem_active_set_retire_fn(struct i915_gem_active *active,
459 i915_gem_retire_fn fn,
460 struct mutex *mutex)
461 {
462 lockdep_assert_held(mutex);
463 active->retire = fn ?: i915_gem_retire_noop;
464 }
465
466 static inline struct drm_i915_gem_request *
__i915_gem_active_peek(const struct i915_gem_active * active)467 __i915_gem_active_peek(const struct i915_gem_active *active)
468 {
469 /* Inside the error capture (running with the driver in an unknown
470 * state), we want to bend the rules slightly (a lot).
471 *
472 * Work is in progress to make it safer, in the meantime this keeps
473 * the known issue from spamming the logs.
474 */
475 return rcu_dereference_protected(active->request, 1);
476 }
477
478 /**
479 * i915_gem_active_raw - return the active request
480 * @active - the active tracker
481 *
482 * i915_gem_active_raw() returns the current request being tracked, or NULL.
483 * It does not obtain a reference on the request for the caller, so the caller
484 * must hold struct_mutex.
485 */
486 static inline struct drm_i915_gem_request *
i915_gem_active_raw(const struct i915_gem_active * active,struct mutex * mutex)487 i915_gem_active_raw(const struct i915_gem_active *active, struct mutex *mutex)
488 {
489 return rcu_dereference_protected(active->request,
490 lockdep_is_held(mutex));
491 }
492
493 /**
494 * i915_gem_active_peek - report the active request being monitored
495 * @active - the active tracker
496 *
497 * i915_gem_active_peek() returns the current request being tracked if
498 * still active, or NULL. It does not obtain a reference on the request
499 * for the caller, so the caller must hold struct_mutex.
500 */
501 static inline struct drm_i915_gem_request *
i915_gem_active_peek(const struct i915_gem_active * active,struct mutex * mutex)502 i915_gem_active_peek(const struct i915_gem_active *active, struct mutex *mutex)
503 {
504 struct drm_i915_gem_request *request;
505
506 request = i915_gem_active_raw(active, mutex);
507 if (!request || i915_gem_request_completed(request))
508 return NULL;
509
510 return request;
511 }
512
513 /**
514 * i915_gem_active_get - return a reference to the active request
515 * @active - the active tracker
516 *
517 * i915_gem_active_get() returns a reference to the active request, or NULL
518 * if the active tracker is idle. The caller must hold struct_mutex.
519 */
520 static inline struct drm_i915_gem_request *
i915_gem_active_get(const struct i915_gem_active * active,struct mutex * mutex)521 i915_gem_active_get(const struct i915_gem_active *active, struct mutex *mutex)
522 {
523 return i915_gem_request_get(i915_gem_active_peek(active, mutex));
524 }
525
526 /**
527 * __i915_gem_active_get_rcu - return a reference to the active request
528 * @active - the active tracker
529 *
530 * __i915_gem_active_get() returns a reference to the active request, or NULL
531 * if the active tracker is idle. The caller must hold the RCU read lock, but
532 * the returned pointer is safe to use outside of RCU.
533 */
534 static inline struct drm_i915_gem_request *
__i915_gem_active_get_rcu(const struct i915_gem_active * active)535 __i915_gem_active_get_rcu(const struct i915_gem_active *active)
536 {
537 /* Performing a lockless retrieval of the active request is super
538 * tricky. SLAB_TYPESAFE_BY_RCU merely guarantees that the backing
539 * slab of request objects will not be freed whilst we hold the
540 * RCU read lock. It does not guarantee that the request itself
541 * will not be freed and then *reused*. Viz,
542 *
543 * Thread A Thread B
544 *
545 * req = active.request
546 * retire(req) -> free(req);
547 * (req is now first on the slab freelist)
548 * active.request = NULL
549 *
550 * req = new submission on a new object
551 * ref(req)
552 *
553 * To prevent the request from being reused whilst the caller
554 * uses it, we take a reference like normal. Whilst acquiring
555 * the reference we check that it is not in a destroyed state
556 * (refcnt == 0). That prevents the request being reallocated
557 * whilst the caller holds on to it. To check that the request
558 * was not reallocated as we acquired the reference we have to
559 * check that our request remains the active request across
560 * the lookup, in the same manner as a seqlock. The visibility
561 * of the pointer versus the reference counting is controlled
562 * by using RCU barriers (rcu_dereference and rcu_assign_pointer).
563 *
564 * In the middle of all that, we inspect whether the request is
565 * complete. Retiring is lazy so the request may be completed long
566 * before the active tracker is updated. Querying whether the
567 * request is complete is far cheaper (as it involves no locked
568 * instructions setting cachelines to exclusive) than acquiring
569 * the reference, so we do it first. The RCU read lock ensures the
570 * pointer dereference is valid, but does not ensure that the
571 * seqno nor HWS is the right one! However, if the request was
572 * reallocated, that means the active tracker's request was complete.
573 * If the new request is also complete, then both are and we can
574 * just report the active tracker is idle. If the new request is
575 * incomplete, then we acquire a reference on it and check that
576 * it remained the active request.
577 *
578 * It is then imperative that we do not zero the request on
579 * reallocation, so that we can chase the dangling pointers!
580 * See i915_gem_request_alloc().
581 */
582 do {
583 struct drm_i915_gem_request *request;
584
585 request = rcu_dereference(active->request);
586 if (!request || i915_gem_request_completed(request))
587 return NULL;
588
589 /* An especially silly compiler could decide to recompute the
590 * result of i915_gem_request_completed, more specifically
591 * re-emit the load for request->fence.seqno. A race would catch
592 * a later seqno value, which could flip the result from true to
593 * false. Which means part of the instructions below might not
594 * be executed, while later on instructions are executed. Due to
595 * barriers within the refcounting the inconsistency can't reach
596 * past the call to i915_gem_request_get_rcu, but not executing
597 * that while still executing i915_gem_request_put() creates
598 * havoc enough. Prevent this with a compiler barrier.
599 */
600 barrier();
601
602 request = i915_gem_request_get_rcu(request);
603
604 /* What stops the following rcu_access_pointer() from occurring
605 * before the above i915_gem_request_get_rcu()? If we were
606 * to read the value before pausing to get the reference to
607 * the request, we may not notice a change in the active
608 * tracker.
609 *
610 * The rcu_access_pointer() is a mere compiler barrier, which
611 * means both the CPU and compiler are free to perform the
612 * memory read without constraint. The compiler only has to
613 * ensure that any operations after the rcu_access_pointer()
614 * occur afterwards in program order. This means the read may
615 * be performed earlier by an out-of-order CPU, or adventurous
616 * compiler.
617 *
618 * The atomic operation at the heart of
619 * i915_gem_request_get_rcu(), see dma_fence_get_rcu(), is
620 * atomic_inc_not_zero() which is only a full memory barrier
621 * when successful. That is, if i915_gem_request_get_rcu()
622 * returns the request (and so with the reference counted
623 * incremented) then the following read for rcu_access_pointer()
624 * must occur after the atomic operation and so confirm
625 * that this request is the one currently being tracked.
626 *
627 * The corresponding write barrier is part of
628 * rcu_assign_pointer().
629 */
630 if (!request || request == rcu_access_pointer(active->request))
631 return rcu_pointer_handoff(request);
632
633 i915_gem_request_put(request);
634 } while (1);
635 }
636
637 /**
638 * i915_gem_active_get_unlocked - return a reference to the active request
639 * @active - the active tracker
640 *
641 * i915_gem_active_get_unlocked() returns a reference to the active request,
642 * or NULL if the active tracker is idle. The reference is obtained under RCU,
643 * so no locking is required by the caller.
644 *
645 * The reference should be freed with i915_gem_request_put().
646 */
647 static inline struct drm_i915_gem_request *
i915_gem_active_get_unlocked(const struct i915_gem_active * active)648 i915_gem_active_get_unlocked(const struct i915_gem_active *active)
649 {
650 struct drm_i915_gem_request *request;
651
652 rcu_read_lock();
653 request = __i915_gem_active_get_rcu(active);
654 rcu_read_unlock();
655
656 return request;
657 }
658
659 /**
660 * i915_gem_active_isset - report whether the active tracker is assigned
661 * @active - the active tracker
662 *
663 * i915_gem_active_isset() returns true if the active tracker is currently
664 * assigned to a request. Due to the lazy retiring, that request may be idle
665 * and this may report stale information.
666 */
667 static inline bool
i915_gem_active_isset(const struct i915_gem_active * active)668 i915_gem_active_isset(const struct i915_gem_active *active)
669 {
670 return rcu_access_pointer(active->request);
671 }
672
673 /**
674 * i915_gem_active_wait - waits until the request is completed
675 * @active - the active request on which to wait
676 * @flags - how to wait
677 * @timeout - how long to wait at most
678 * @rps - userspace client to charge for a waitboost
679 *
680 * i915_gem_active_wait() waits until the request is completed before
681 * returning, without requiring any locks to be held. Note that it does not
682 * retire any requests before returning.
683 *
684 * This function relies on RCU in order to acquire the reference to the active
685 * request without holding any locks. See __i915_gem_active_get_rcu() for the
686 * glory details on how that is managed. Once the reference is acquired, we
687 * can then wait upon the request, and afterwards release our reference,
688 * free of any locking.
689 *
690 * This function wraps i915_wait_request(), see it for the full details on
691 * the arguments.
692 *
693 * Returns 0 if successful, or a negative error code.
694 */
695 static inline int
i915_gem_active_wait(const struct i915_gem_active * active,unsigned int flags)696 i915_gem_active_wait(const struct i915_gem_active *active, unsigned int flags)
697 {
698 struct drm_i915_gem_request *request;
699 long ret = 0;
700
701 request = i915_gem_active_get_unlocked(active);
702 if (request) {
703 ret = i915_wait_request(request, flags, MAX_SCHEDULE_TIMEOUT);
704 i915_gem_request_put(request);
705 }
706
707 return ret < 0 ? ret : 0;
708 }
709
710 /**
711 * i915_gem_active_retire - waits until the request is retired
712 * @active - the active request on which to wait
713 *
714 * i915_gem_active_retire() waits until the request is completed,
715 * and then ensures that at least the retirement handler for this
716 * @active tracker is called before returning. If the @active
717 * tracker is idle, the function returns immediately.
718 */
719 static inline int __must_check
i915_gem_active_retire(struct i915_gem_active * active,struct mutex * mutex)720 i915_gem_active_retire(struct i915_gem_active *active,
721 struct mutex *mutex)
722 {
723 struct drm_i915_gem_request *request;
724 long ret;
725
726 request = i915_gem_active_raw(active, mutex);
727 if (!request)
728 return 0;
729
730 ret = i915_wait_request(request,
731 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
732 MAX_SCHEDULE_TIMEOUT);
733 if (ret < 0)
734 return ret;
735
736 list_del_init(&active->link);
737 RCU_INIT_POINTER(active->request, NULL);
738
739 active->retire(active, request);
740
741 return 0;
742 }
743
744 #define for_each_active(mask, idx) \
745 for (; mask ? idx = ffs(mask) - 1, 1 : 0; mask &= ~BIT(idx))
746
747 #endif /* I915_GEM_REQUEST_H */
748