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