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