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