xref: /openbsd-src/sys/dev/pci/drm/i915/i915_request.c (revision f005ef32267c16bdb134f0e9fa4477dbe07c263a)
17f4dd379Sjsg /*
27f4dd379Sjsg  * Copyright © 2008-2015 Intel Corporation
37f4dd379Sjsg  *
47f4dd379Sjsg  * Permission is hereby granted, free of charge, to any person obtaining a
57f4dd379Sjsg  * copy of this software and associated documentation files (the "Software"),
67f4dd379Sjsg  * to deal in the Software without restriction, including without limitation
77f4dd379Sjsg  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87f4dd379Sjsg  * and/or sell copies of the Software, and to permit persons to whom the
97f4dd379Sjsg  * Software is furnished to do so, subject to the following conditions:
107f4dd379Sjsg  *
117f4dd379Sjsg  * The above copyright notice and this permission notice (including the next
127f4dd379Sjsg  * paragraph) shall be included in all copies or substantial portions of the
137f4dd379Sjsg  * Software.
147f4dd379Sjsg  *
157f4dd379Sjsg  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167f4dd379Sjsg  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177f4dd379Sjsg  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187f4dd379Sjsg  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197f4dd379Sjsg  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207f4dd379Sjsg  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217f4dd379Sjsg  * IN THE SOFTWARE.
227f4dd379Sjsg  *
237f4dd379Sjsg  */
247f4dd379Sjsg 
257f4dd379Sjsg #include <linux/dma-fence-array.h>
26ad8b1aafSjsg #include <linux/dma-fence-chain.h>
27c349dbc7Sjsg #include <linux/irq_work.h>
28c349dbc7Sjsg #include <linux/prefetch.h>
297f4dd379Sjsg #include <linux/sched.h>
307f4dd379Sjsg #include <linux/sched/clock.h>
317f4dd379Sjsg #include <linux/sched/signal.h>
3240f95102Sjsg #include <linux/sched/mm.h>
337f4dd379Sjsg 
34c349dbc7Sjsg #include "gem/i915_gem_context.h"
35ad8b1aafSjsg #include "gt/intel_breadcrumbs.h"
36c349dbc7Sjsg #include "gt/intel_context.h"
375ca02815Sjsg #include "gt/intel_engine.h"
385ca02815Sjsg #include "gt/intel_engine_heartbeat.h"
391bb76ff1Sjsg #include "gt/intel_engine_regs.h"
405ca02815Sjsg #include "gt/intel_gpu_commands.h"
415ca02815Sjsg #include "gt/intel_reset.h"
42c349dbc7Sjsg #include "gt/intel_ring.h"
43c349dbc7Sjsg #include "gt/intel_rps.h"
44c349dbc7Sjsg 
45c349dbc7Sjsg #include "i915_active.h"
46*f005ef32Sjsg #include "i915_config.h"
471bb76ff1Sjsg #include "i915_deps.h"
481bb76ff1Sjsg #include "i915_driver.h"
497f4dd379Sjsg #include "i915_drv.h"
50c349dbc7Sjsg #include "i915_trace.h"
51c349dbc7Sjsg 
52c349dbc7Sjsg struct execute_cb {
53c349dbc7Sjsg 	struct irq_work work;
54c349dbc7Sjsg 	struct i915_sw_fence *fence;
55c349dbc7Sjsg 	struct i915_request *signal;
56c349dbc7Sjsg };
57c349dbc7Sjsg 
585ca02815Sjsg static struct pool slab_requests;
595ca02815Sjsg static struct pool slab_execute_cbs;
607f4dd379Sjsg 
i915_fence_get_driver_name(struct dma_fence * fence)617f4dd379Sjsg static const char *i915_fence_get_driver_name(struct dma_fence *fence)
627f4dd379Sjsg {
631bb76ff1Sjsg 	return dev_name(to_request(fence)->i915->drm.dev);
647f4dd379Sjsg }
657f4dd379Sjsg 
i915_fence_get_timeline_name(struct dma_fence * fence)667f4dd379Sjsg static const char *i915_fence_get_timeline_name(struct dma_fence *fence)
677f4dd379Sjsg {
68c349dbc7Sjsg 	const struct i915_gem_context *ctx;
69c349dbc7Sjsg 
707f4dd379Sjsg 	/*
717f4dd379Sjsg 	 * The timeline struct (as part of the ppgtt underneath a context)
727f4dd379Sjsg 	 * may be freed when the request is no longer in use by the GPU.
737f4dd379Sjsg 	 * We could extend the life of a context to beyond that of all
747f4dd379Sjsg 	 * fences, possibly keeping the hw resource around indefinitely,
757f4dd379Sjsg 	 * or we just give them a false name. Since
767f4dd379Sjsg 	 * dma_fence_ops.get_timeline_name is a debug feature, the occasional
777f4dd379Sjsg 	 * lie seems justifiable.
787f4dd379Sjsg 	 */
797f4dd379Sjsg 	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
807f4dd379Sjsg 		return "signaled";
817f4dd379Sjsg 
82c349dbc7Sjsg 	ctx = i915_request_gem_context(to_request(fence));
83c349dbc7Sjsg 	if (!ctx)
84c349dbc7Sjsg 		return "[" DRIVER_NAME "]";
85c349dbc7Sjsg 
86c349dbc7Sjsg 	return ctx->name;
877f4dd379Sjsg }
887f4dd379Sjsg 
i915_fence_signaled(struct dma_fence * fence)897f4dd379Sjsg static bool i915_fence_signaled(struct dma_fence *fence)
907f4dd379Sjsg {
917f4dd379Sjsg 	return i915_request_completed(to_request(fence));
927f4dd379Sjsg }
937f4dd379Sjsg 
i915_fence_enable_signaling(struct dma_fence * fence)947f4dd379Sjsg static bool i915_fence_enable_signaling(struct dma_fence *fence)
957f4dd379Sjsg {
96c349dbc7Sjsg 	return i915_request_enable_breadcrumb(to_request(fence));
977f4dd379Sjsg }
987f4dd379Sjsg 
i915_fence_wait(struct dma_fence * fence,bool interruptible,signed long timeout)997f4dd379Sjsg static signed long i915_fence_wait(struct dma_fence *fence,
1007f4dd379Sjsg 				   bool interruptible,
1017f4dd379Sjsg 				   signed long timeout)
1027f4dd379Sjsg {
1031bb76ff1Sjsg 	return i915_request_wait_timeout(to_request(fence),
104c349dbc7Sjsg 					 interruptible | I915_WAIT_PRIORITY,
105c349dbc7Sjsg 					 timeout);
1067f4dd379Sjsg }
1077f4dd379Sjsg 
108ad8b1aafSjsg #ifdef __linux__
i915_request_slab_cache(void)109ad8b1aafSjsg struct kmem_cache *i915_request_slab_cache(void)
110ad8b1aafSjsg {
1115ca02815Sjsg 	return slab_requests;
112ad8b1aafSjsg }
113ad8b1aafSjsg #else
i915_request_slab_cache(void)114ad8b1aafSjsg struct pool *i915_request_slab_cache(void)
115ad8b1aafSjsg {
1165ca02815Sjsg 	return &slab_requests;
117ad8b1aafSjsg }
118ad8b1aafSjsg #endif
119ad8b1aafSjsg 
i915_fence_release(struct dma_fence * fence)1207f4dd379Sjsg static void i915_fence_release(struct dma_fence *fence)
1217f4dd379Sjsg {
1227f4dd379Sjsg 	struct i915_request *rq = to_request(fence);
1237f4dd379Sjsg 
1245ca02815Sjsg 	GEM_BUG_ON(rq->guc_prio != GUC_PRIO_INIT &&
1255ca02815Sjsg 		   rq->guc_prio != GUC_PRIO_FINI);
1265ca02815Sjsg 
1271bb76ff1Sjsg 	i915_request_free_capture_list(fetch_and_zero(&rq->capture_list));
1281bb76ff1Sjsg 	if (rq->batch_res) {
1291bb76ff1Sjsg 		i915_vma_resource_put(rq->batch_res);
1301bb76ff1Sjsg 		rq->batch_res = NULL;
1311bb76ff1Sjsg 	}
1321bb76ff1Sjsg 
1337f4dd379Sjsg 	/*
1347f4dd379Sjsg 	 * The request is put onto a RCU freelist (i.e. the address
1357f4dd379Sjsg 	 * is immediately reused), mark the fences as being freed now.
1367f4dd379Sjsg 	 * Otherwise the debugobjects for the fences are only marked as
1377f4dd379Sjsg 	 * freed when the slab cache itself is freed, and so we would get
1387f4dd379Sjsg 	 * caught trying to reuse dead objects.
1397f4dd379Sjsg 	 */
1407f4dd379Sjsg 	i915_sw_fence_fini(&rq->submit);
141c349dbc7Sjsg 	i915_sw_fence_fini(&rq->semaphore);
1427f4dd379Sjsg 
143ad8b1aafSjsg 	/*
1442e4f5bb9Sjsg 	 * Keep one request on each engine for reserved use under mempressure.
1451bb76ff1Sjsg 	 *
1461bb76ff1Sjsg 	 * We do not hold a reference to the engine here and so have to be
1471bb76ff1Sjsg 	 * very careful in what rq->engine we poke. The virtual engine is
1481bb76ff1Sjsg 	 * referenced via the rq->context and we released that ref during
1491bb76ff1Sjsg 	 * i915_request_retire(), ergo we must not dereference a virtual
1501bb76ff1Sjsg 	 * engine here. Not that we would want to, as the only consumer of
1511bb76ff1Sjsg 	 * the reserved engine->request_pool is the power management parking,
1521bb76ff1Sjsg 	 * which must-not-fail, and that is only run on the physical engines.
1531bb76ff1Sjsg 	 *
1541bb76ff1Sjsg 	 * Since the request must have been executed to be have completed,
1551bb76ff1Sjsg 	 * we know that it will have been processed by the HW and will
1561bb76ff1Sjsg 	 * not be unsubmitted again, so rq->engine and rq->execution_mask
1571bb76ff1Sjsg 	 * at this point is stable. rq->execution_mask will be a single
1581bb76ff1Sjsg 	 * bit if the last and _only_ engine it could execution on was a
1591bb76ff1Sjsg 	 * physical engine, if it's multiple bits then it started on and
1601bb76ff1Sjsg 	 * could still be on a virtual engine. Thus if the mask is not a
1611bb76ff1Sjsg 	 * power-of-two we assume that rq->engine may still be a virtual
1621bb76ff1Sjsg 	 * engine and so a dangling invalid pointer that we cannot dereference
1631bb76ff1Sjsg 	 *
1641bb76ff1Sjsg 	 * For example, consider the flow of a bonded request through a virtual
1651bb76ff1Sjsg 	 * engine. The request is created with a wide engine mask (all engines
1661bb76ff1Sjsg 	 * that we might execute on). On processing the bond, the request mask
1671bb76ff1Sjsg 	 * is reduced to one or more engines. If the request is subsequently
1681bb76ff1Sjsg 	 * bound to a single engine, it will then be constrained to only
1691bb76ff1Sjsg 	 * execute on that engine and never returned to the virtual engine
1701bb76ff1Sjsg 	 * after timeslicing away, see __unwind_incomplete_requests(). Thus we
1711bb76ff1Sjsg 	 * know that if the rq->execution_mask is a single bit, rq->engine
1721bb76ff1Sjsg 	 * can be a physical engine with the exact corresponding mask.
173ad8b1aafSjsg 	 */
1742e4f5bb9Sjsg 	if (is_power_of_2(rq->execution_mask) &&
1751bb76ff1Sjsg 	    !cmpxchg(&rq->engine->request_pool, NULL, rq))
176ad8b1aafSjsg 		return;
177ad8b1aafSjsg 
1787f4dd379Sjsg #ifdef __linux__
1795ca02815Sjsg 	kmem_cache_free(slab_requests, rq);
1807f4dd379Sjsg #else
1815ca02815Sjsg 	pool_put(&slab_requests, rq);
1827f4dd379Sjsg #endif
1837f4dd379Sjsg }
1847f4dd379Sjsg 
1857f4dd379Sjsg const struct dma_fence_ops i915_fence_ops = {
1867f4dd379Sjsg 	.get_driver_name = i915_fence_get_driver_name,
1877f4dd379Sjsg 	.get_timeline_name = i915_fence_get_timeline_name,
1887f4dd379Sjsg 	.enable_signaling = i915_fence_enable_signaling,
1897f4dd379Sjsg 	.signaled = i915_fence_signaled,
1907f4dd379Sjsg 	.wait = i915_fence_wait,
1917f4dd379Sjsg 	.release = i915_fence_release,
1927f4dd379Sjsg };
1937f4dd379Sjsg 
irq_execute_cb(struct irq_work * wrk)194c349dbc7Sjsg static void irq_execute_cb(struct irq_work *wrk)
195c349dbc7Sjsg {
196c349dbc7Sjsg 	struct execute_cb *cb = container_of(wrk, typeof(*cb), work);
197c349dbc7Sjsg 
198c349dbc7Sjsg 	i915_sw_fence_complete(cb->fence);
199e17fad91Sjsg #ifdef __linux__
2005ca02815Sjsg 	kmem_cache_free(slab_execute_cbs, cb);
201c349dbc7Sjsg #else
2025ca02815Sjsg 	pool_put(&slab_execute_cbs, cb);
203c349dbc7Sjsg #endif
204c349dbc7Sjsg }
205c349dbc7Sjsg 
206ad8b1aafSjsg static __always_inline void
__notify_execute_cb(struct i915_request * rq,bool (* fn)(struct irq_work * wrk))207ad8b1aafSjsg __notify_execute_cb(struct i915_request *rq, bool (*fn)(struct irq_work *wrk))
208c349dbc7Sjsg {
209ad8b1aafSjsg 	struct execute_cb *cb, *cn;
210c349dbc7Sjsg 
211ad8b1aafSjsg 	if (llist_empty(&rq->execute_cb))
212c349dbc7Sjsg 		return;
213c349dbc7Sjsg 
214ad8b1aafSjsg 	llist_for_each_entry_safe(cb, cn,
215ad8b1aafSjsg 				  llist_del_all(&rq->execute_cb),
2165ca02815Sjsg 				  work.node.llist)
217ad8b1aafSjsg 		fn(&cb->work);
218c349dbc7Sjsg }
219c349dbc7Sjsg 
__notify_execute_cb_irq(struct i915_request * rq)220ad8b1aafSjsg static void __notify_execute_cb_irq(struct i915_request *rq)
2217f4dd379Sjsg {
222ad8b1aafSjsg 	__notify_execute_cb(rq, irq_work_queue);
2237f4dd379Sjsg }
224ad8b1aafSjsg 
irq_work_imm(struct irq_work * wrk)225ad8b1aafSjsg static bool irq_work_imm(struct irq_work *wrk)
226ad8b1aafSjsg {
227e17fad91Sjsg #ifdef __linux__
228ad8b1aafSjsg 	wrk->func(wrk);
229ad8b1aafSjsg #else
230e17fad91Sjsg 	wrk->task.t_func(wrk);
231ad8b1aafSjsg #endif
232ad8b1aafSjsg 	return false;
233ad8b1aafSjsg }
234ad8b1aafSjsg 
i915_request_notify_execute_cb_imm(struct i915_request * rq)2355ca02815Sjsg void i915_request_notify_execute_cb_imm(struct i915_request *rq)
236ad8b1aafSjsg {
237ad8b1aafSjsg 	__notify_execute_cb(rq, irq_work_imm);
2387f4dd379Sjsg }
2397f4dd379Sjsg 
__i915_request_fill(struct i915_request * rq,u8 val)240c349dbc7Sjsg static void __i915_request_fill(struct i915_request *rq, u8 val)
2417f4dd379Sjsg {
242c349dbc7Sjsg 	void *vaddr = rq->ring->vaddr;
243c349dbc7Sjsg 	u32 head;
244c349dbc7Sjsg 
245c349dbc7Sjsg 	head = rq->infix;
246c349dbc7Sjsg 	if (rq->postfix < head) {
247c349dbc7Sjsg 		memset(vaddr + head, val, rq->ring->size - head);
248c349dbc7Sjsg 		head = 0;
249c349dbc7Sjsg 	}
250c349dbc7Sjsg 	memset(vaddr + head, val, rq->postfix - head);
2517f4dd379Sjsg }
2527f4dd379Sjsg 
2535ca02815Sjsg /**
2545ca02815Sjsg  * i915_request_active_engine
2555ca02815Sjsg  * @rq: request to inspect
2565ca02815Sjsg  * @active: pointer in which to return the active engine
2575ca02815Sjsg  *
2585ca02815Sjsg  * Fills the currently active engine to the @active pointer if the request
2595ca02815Sjsg  * is active and still not completed.
2605ca02815Sjsg  *
2615ca02815Sjsg  * Returns true if request was active or false otherwise.
2625ca02815Sjsg  */
2635ca02815Sjsg bool
i915_request_active_engine(struct i915_request * rq,struct intel_engine_cs ** active)2645ca02815Sjsg i915_request_active_engine(struct i915_request *rq,
2655ca02815Sjsg 			   struct intel_engine_cs **active)
2667f4dd379Sjsg {
267c349dbc7Sjsg 	struct intel_engine_cs *engine, *locked;
2685ca02815Sjsg 	bool ret = false;
2697f4dd379Sjsg 
2707f4dd379Sjsg 	/*
2715ca02815Sjsg 	 * Serialise with __i915_request_submit() so that it sees
2725ca02815Sjsg 	 * is-banned?, or we know the request is already inflight.
2735ca02815Sjsg 	 *
2745ca02815Sjsg 	 * Note that rq->engine is unstable, and so we double
2755ca02815Sjsg 	 * check that we have acquired the lock on the final engine.
2767f4dd379Sjsg 	 */
277c349dbc7Sjsg 	locked = READ_ONCE(rq->engine);
2785ca02815Sjsg 	spin_lock_irq(&locked->sched_engine->lock);
279c349dbc7Sjsg 	while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
2805ca02815Sjsg 		spin_unlock(&locked->sched_engine->lock);
281c349dbc7Sjsg 		locked = engine;
2825ca02815Sjsg 		spin_lock(&locked->sched_engine->lock);
283c349dbc7Sjsg 	}
284ad8b1aafSjsg 
2855ca02815Sjsg 	if (i915_request_is_active(rq)) {
2865ca02815Sjsg 		if (!__i915_request_is_complete(rq))
2875ca02815Sjsg 			*active = locked;
2885ca02815Sjsg 		ret = true;
2895ca02815Sjsg 	}
290ad8b1aafSjsg 
2915ca02815Sjsg 	spin_unlock_irq(&locked->sched_engine->lock);
292ad8b1aafSjsg 
2935ca02815Sjsg 	return ret;
2945ca02815Sjsg }
295ad8b1aafSjsg 
__rq_init_watchdog(struct i915_request * rq)2965ca02815Sjsg static void __rq_init_watchdog(struct i915_request *rq)
2975ca02815Sjsg {
2985ca02815Sjsg 	rq->watchdog.timer.to_func = NULL;
2995ca02815Sjsg }
3005ca02815Sjsg 
3015ca02815Sjsg #ifdef __linux__
3025ca02815Sjsg 
__rq_watchdog_expired(struct hrtimer * hrtimer)3035ca02815Sjsg static enum hrtimer_restart __rq_watchdog_expired(struct hrtimer *hrtimer)
3045ca02815Sjsg {
3055ca02815Sjsg 	struct i915_request *rq =
3065ca02815Sjsg 		container_of(hrtimer, struct i915_request, watchdog.timer);
3075ca02815Sjsg 	struct intel_gt *gt = rq->engine->gt;
3085ca02815Sjsg 
3095ca02815Sjsg 	if (!i915_request_completed(rq)) {
3105ca02815Sjsg 		if (llist_add(&rq->watchdog.link, &gt->watchdog.list))
311*f005ef32Sjsg 			queue_work(gt->i915->unordered_wq, &gt->watchdog.work);
3125ca02815Sjsg 	} else {
3135ca02815Sjsg 		i915_request_put(rq);
3145ca02815Sjsg 	}
3155ca02815Sjsg 
3165ca02815Sjsg 	return HRTIMER_NORESTART;
3175ca02815Sjsg }
3185ca02815Sjsg 
3195ca02815Sjsg #else
3205ca02815Sjsg 
3215ca02815Sjsg static void
__rq_watchdog_expired(void * arg)3225ca02815Sjsg __rq_watchdog_expired(void *arg)
3235ca02815Sjsg {
3245ca02815Sjsg 	struct i915_request *rq = (struct i915_request *)arg;
3255ca02815Sjsg 	struct intel_gt *gt = rq->engine->gt;
3265ca02815Sjsg 
3275ca02815Sjsg 	if (!i915_request_completed(rq)) {
3285ca02815Sjsg 		if (llist_add(&rq->watchdog.link, &gt->watchdog.list))
3295ca02815Sjsg 			schedule_work(&gt->watchdog.work);
3305ca02815Sjsg 	} else {
3315ca02815Sjsg 		i915_request_put(rq);
3325ca02815Sjsg 	}
3335ca02815Sjsg }
3345ca02815Sjsg 
3355ca02815Sjsg #endif
3365ca02815Sjsg 
__rq_arm_watchdog(struct i915_request * rq)3375ca02815Sjsg static void __rq_arm_watchdog(struct i915_request *rq)
3385ca02815Sjsg {
3395ca02815Sjsg 	struct i915_request_watchdog *wdg = &rq->watchdog;
3405ca02815Sjsg 	struct intel_context *ce = rq->context;
3415ca02815Sjsg 
3425ca02815Sjsg 	if (!ce->watchdog.timeout_us)
3435ca02815Sjsg 		return;
3445ca02815Sjsg 
3455ca02815Sjsg 	i915_request_get(rq);
3465ca02815Sjsg 
3475ca02815Sjsg #ifdef __linux__
3485ca02815Sjsg 	hrtimer_init(&wdg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3495ca02815Sjsg 	wdg->timer.function = __rq_watchdog_expired;
3505ca02815Sjsg 	hrtimer_start_range_ns(&wdg->timer,
3515ca02815Sjsg 			       ns_to_ktime(ce->watchdog.timeout_us *
3525ca02815Sjsg 					   NSEC_PER_USEC),
3535ca02815Sjsg 			       NSEC_PER_MSEC,
3545ca02815Sjsg 			       HRTIMER_MODE_REL);
3555ca02815Sjsg #else
3565ca02815Sjsg 	timeout_set(&wdg->timer, __rq_watchdog_expired, rq);
3575ca02815Sjsg 	timeout_add_msec(&wdg->timer, 1);
3585ca02815Sjsg #endif
3595ca02815Sjsg }
3605ca02815Sjsg 
__rq_cancel_watchdog(struct i915_request * rq)3615ca02815Sjsg static void __rq_cancel_watchdog(struct i915_request *rq)
3625ca02815Sjsg {
3635ca02815Sjsg 	struct i915_request_watchdog *wdg = &rq->watchdog;
3645ca02815Sjsg 
3655ca02815Sjsg 	if (wdg->timer.to_func && hrtimer_try_to_cancel(&wdg->timer) > 0)
3665ca02815Sjsg 		i915_request_put(rq);
3677f4dd379Sjsg }
3687f4dd379Sjsg 
3691bb76ff1Sjsg #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
3701bb76ff1Sjsg 
3711bb76ff1Sjsg /**
3721bb76ff1Sjsg  * i915_request_free_capture_list - Free a capture list
3731bb76ff1Sjsg  * @capture: Pointer to the first list item or NULL
3741bb76ff1Sjsg  *
3751bb76ff1Sjsg  */
i915_request_free_capture_list(struct i915_capture_list * capture)3761bb76ff1Sjsg void i915_request_free_capture_list(struct i915_capture_list *capture)
3771bb76ff1Sjsg {
3781bb76ff1Sjsg 	while (capture) {
3791bb76ff1Sjsg 		struct i915_capture_list *next = capture->next;
3801bb76ff1Sjsg 
3811bb76ff1Sjsg 		i915_vma_resource_put(capture->vma_res);
3821bb76ff1Sjsg 		kfree(capture);
3831bb76ff1Sjsg 		capture = next;
3841bb76ff1Sjsg 	}
3851bb76ff1Sjsg }
3861bb76ff1Sjsg 
3871bb76ff1Sjsg #define assert_capture_list_is_null(_rq) GEM_BUG_ON((_rq)->capture_list)
3881bb76ff1Sjsg 
3891bb76ff1Sjsg #define clear_capture_list(_rq) ((_rq)->capture_list = NULL)
3901bb76ff1Sjsg 
3911bb76ff1Sjsg #else
3921bb76ff1Sjsg 
3931bb76ff1Sjsg #define i915_request_free_capture_list(_a) do {} while (0)
3941bb76ff1Sjsg 
3951bb76ff1Sjsg #define assert_capture_list_is_null(_a) do {} while (0)
3961bb76ff1Sjsg 
3971bb76ff1Sjsg #define clear_capture_list(_rq) do {} while (0)
3981bb76ff1Sjsg 
3991bb76ff1Sjsg #endif
4001bb76ff1Sjsg 
i915_request_retire(struct i915_request * rq)401c349dbc7Sjsg bool i915_request_retire(struct i915_request *rq)
4027f4dd379Sjsg {
4035ca02815Sjsg 	if (!__i915_request_is_complete(rq))
404c349dbc7Sjsg 		return false;
4057f4dd379Sjsg 
406c349dbc7Sjsg 	RQ_TRACE(rq, "\n");
4077f4dd379Sjsg 
408c349dbc7Sjsg 	GEM_BUG_ON(!i915_sw_fence_signaled(&rq->submit));
409c349dbc7Sjsg 	trace_i915_request_retire(rq);
410ad8b1aafSjsg 	i915_request_mark_complete(rq);
4117f4dd379Sjsg 
4125ca02815Sjsg 	__rq_cancel_watchdog(rq);
4135ca02815Sjsg 
4147f4dd379Sjsg 	/*
4157f4dd379Sjsg 	 * We know the GPU must have read the request to have
4167f4dd379Sjsg 	 * sent us the seqno + interrupt, so use the position
4177f4dd379Sjsg 	 * of tail of the request to update the last known position
4187f4dd379Sjsg 	 * of the GPU head.
4197f4dd379Sjsg 	 *
4207f4dd379Sjsg 	 * Note this requires that we are always called in request
4217f4dd379Sjsg 	 * completion order.
4227f4dd379Sjsg 	 */
423c349dbc7Sjsg 	GEM_BUG_ON(!list_is_first(&rq->link,
424c349dbc7Sjsg 				  &i915_request_timeline(rq)->requests));
425c349dbc7Sjsg 	if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
426c349dbc7Sjsg 		/* Poison before we release our space in the ring */
427c349dbc7Sjsg 		__i915_request_fill(rq, POISON_FREE);
428c349dbc7Sjsg 	rq->ring->head = rq->postfix;
429c349dbc7Sjsg 
430ad8b1aafSjsg 	if (!i915_request_signaled(rq)) {
431ad8b1aafSjsg 		spin_lock_irq(&rq->lock);
432ad8b1aafSjsg 		dma_fence_signal_locked(&rq->fence);
433ad8b1aafSjsg 		spin_unlock_irq(&rq->lock);
434ad8b1aafSjsg 	}
435ad8b1aafSjsg 
4365ca02815Sjsg 	if (test_and_set_bit(I915_FENCE_FLAG_BOOST, &rq->fence.flags))
4371bb76ff1Sjsg 		intel_rps_dec_waiters(&rq->engine->gt->rps);
438ad8b1aafSjsg 
4397f4dd379Sjsg 	/*
440c349dbc7Sjsg 	 * We only loosely track inflight requests across preemption,
441c349dbc7Sjsg 	 * and so we may find ourselves attempting to retire a _completed_
442c349dbc7Sjsg 	 * request that we have removed from the HW and put back on a run
443c349dbc7Sjsg 	 * queue.
444ad8b1aafSjsg 	 *
445ad8b1aafSjsg 	 * As we set I915_FENCE_FLAG_ACTIVE on the request, this should be
446ad8b1aafSjsg 	 * after removing the breadcrumb and signaling it, so that we do not
447ad8b1aafSjsg 	 * inadvertently attach the breadcrumb to a completed request.
4487f4dd379Sjsg 	 */
4495ca02815Sjsg 	rq->engine->remove_active_request(rq);
450ad8b1aafSjsg 	GEM_BUG_ON(!llist_empty(&rq->execute_cb));
4517f4dd379Sjsg 
452c349dbc7Sjsg 	__list_del_entry(&rq->link); /* poison neither prev/next (RCU walks) */
4537f4dd379Sjsg 
454c349dbc7Sjsg 	intel_context_exit(rq->context);
455c349dbc7Sjsg 	intel_context_unpin(rq->context);
4567f4dd379Sjsg 
457c349dbc7Sjsg 	i915_sched_node_fini(&rq->sched);
458c349dbc7Sjsg 	i915_request_put(rq);
4597f4dd379Sjsg 
460c349dbc7Sjsg 	return true;
4617f4dd379Sjsg }
4627f4dd379Sjsg 
i915_request_retire_upto(struct i915_request * rq)4637f4dd379Sjsg void i915_request_retire_upto(struct i915_request *rq)
4647f4dd379Sjsg {
465c349dbc7Sjsg 	struct intel_timeline * const tl = i915_request_timeline(rq);
4667f4dd379Sjsg 	struct i915_request *tmp;
4677f4dd379Sjsg 
468c349dbc7Sjsg 	RQ_TRACE(rq, "\n");
4695ca02815Sjsg 	GEM_BUG_ON(!__i915_request_is_complete(rq));
4707f4dd379Sjsg 
471c349dbc7Sjsg 	do {
472c349dbc7Sjsg 		tmp = list_first_entry(&tl->requests, typeof(*tmp), link);
4735ca02815Sjsg 		GEM_BUG_ON(!i915_request_completed(tmp));
474c349dbc7Sjsg 	} while (i915_request_retire(tmp) && tmp != rq);
475c349dbc7Sjsg }
476c349dbc7Sjsg 
477ad8b1aafSjsg static struct i915_request * const *
__engine_active(struct intel_engine_cs * engine)478ad8b1aafSjsg __engine_active(struct intel_engine_cs *engine)
479ad8b1aafSjsg {
480ad8b1aafSjsg 	return READ_ONCE(engine->execlists.active);
481ad8b1aafSjsg }
482ad8b1aafSjsg 
__request_in_flight(const struct i915_request * signal)483ad8b1aafSjsg static bool __request_in_flight(const struct i915_request *signal)
484ad8b1aafSjsg {
485ad8b1aafSjsg 	struct i915_request * const *port, *rq;
486ad8b1aafSjsg 	bool inflight = false;
487ad8b1aafSjsg 
488ad8b1aafSjsg 	if (!i915_request_is_ready(signal))
489ad8b1aafSjsg 		return false;
490ad8b1aafSjsg 
491ad8b1aafSjsg 	/*
492ad8b1aafSjsg 	 * Even if we have unwound the request, it may still be on
493ad8b1aafSjsg 	 * the GPU (preempt-to-busy). If that request is inside an
494ad8b1aafSjsg 	 * unpreemptible critical section, it will not be removed. Some
495ad8b1aafSjsg 	 * GPU functions may even be stuck waiting for the paired request
496ad8b1aafSjsg 	 * (__await_execution) to be submitted and cannot be preempted
497ad8b1aafSjsg 	 * until the bond is executing.
498ad8b1aafSjsg 	 *
499ad8b1aafSjsg 	 * As we know that there are always preemption points between
500ad8b1aafSjsg 	 * requests, we know that only the currently executing request
501ad8b1aafSjsg 	 * may be still active even though we have cleared the flag.
502ad8b1aafSjsg 	 * However, we can't rely on our tracking of ELSP[0] to know
503ad8b1aafSjsg 	 * which request is currently active and so maybe stuck, as
504ad8b1aafSjsg 	 * the tracking maybe an event behind. Instead assume that
505ad8b1aafSjsg 	 * if the context is still inflight, then it is still active
506ad8b1aafSjsg 	 * even if the active flag has been cleared.
507ad8b1aafSjsg 	 *
508ad8b1aafSjsg 	 * To further complicate matters, if there a pending promotion, the HW
509ad8b1aafSjsg 	 * may either perform a context switch to the second inflight execlists,
510ad8b1aafSjsg 	 * or it may switch to the pending set of execlists. In the case of the
511ad8b1aafSjsg 	 * latter, it may send the ACK and we process the event copying the
512ad8b1aafSjsg 	 * pending[] over top of inflight[], _overwriting_ our *active. Since
513ad8b1aafSjsg 	 * this implies the HW is arbitrating and not struck in *active, we do
514ad8b1aafSjsg 	 * not worry about complete accuracy, but we do require no read/write
515ad8b1aafSjsg 	 * tearing of the pointer [the read of the pointer must be valid, even
516ad8b1aafSjsg 	 * as the array is being overwritten, for which we require the writes
517ad8b1aafSjsg 	 * to avoid tearing.]
518ad8b1aafSjsg 	 *
519ad8b1aafSjsg 	 * Note that the read of *execlists->active may race with the promotion
520ad8b1aafSjsg 	 * of execlists->pending[] to execlists->inflight[], overwritting
521ad8b1aafSjsg 	 * the value at *execlists->active. This is fine. The promotion implies
522ad8b1aafSjsg 	 * that we received an ACK from the HW, and so the context is not
523ad8b1aafSjsg 	 * stuck -- if we do not see ourselves in *active, the inflight status
524ad8b1aafSjsg 	 * is valid. If instead we see ourselves being copied into *active,
525ad8b1aafSjsg 	 * we are inflight and may signal the callback.
526ad8b1aafSjsg 	 */
527ad8b1aafSjsg 	if (!intel_context_inflight(signal->context))
528ad8b1aafSjsg 		return false;
529ad8b1aafSjsg 
530ad8b1aafSjsg 	rcu_read_lock();
531ad8b1aafSjsg 	for (port = __engine_active(signal->engine);
532ad8b1aafSjsg 	     (rq = READ_ONCE(*port)); /* may race with promotion of pending[] */
533ad8b1aafSjsg 	     port++) {
534ad8b1aafSjsg 		if (rq->context == signal->context) {
535ad8b1aafSjsg 			inflight = i915_seqno_passed(rq->fence.seqno,
536ad8b1aafSjsg 						     signal->fence.seqno);
537ad8b1aafSjsg 			break;
538ad8b1aafSjsg 		}
539ad8b1aafSjsg 	}
540ad8b1aafSjsg 	rcu_read_unlock();
541ad8b1aafSjsg 
542ad8b1aafSjsg 	return inflight;
543ad8b1aafSjsg }
544ad8b1aafSjsg 
545c349dbc7Sjsg static int
__await_execution(struct i915_request * rq,struct i915_request * signal,gfp_t gfp)546c349dbc7Sjsg __await_execution(struct i915_request *rq,
547c349dbc7Sjsg 		  struct i915_request *signal,
548c349dbc7Sjsg 		  gfp_t gfp)
549c349dbc7Sjsg {
550c349dbc7Sjsg 	struct execute_cb *cb;
551c349dbc7Sjsg 
5525ca02815Sjsg 	if (i915_request_is_active(signal))
553c349dbc7Sjsg 		return 0;
554c349dbc7Sjsg 
555c349dbc7Sjsg #ifdef __linux__
5565ca02815Sjsg 	cb = kmem_cache_alloc(slab_execute_cbs, gfp);
557c349dbc7Sjsg #else
5585ca02815Sjsg 	cb = pool_get(&slab_execute_cbs,
559c349dbc7Sjsg 	    (gfp & GFP_NOWAIT) ? PR_NOWAIT : PR_WAITOK);
560c349dbc7Sjsg #endif
561c349dbc7Sjsg 	if (!cb)
562c349dbc7Sjsg 		return -ENOMEM;
563c349dbc7Sjsg 
564c349dbc7Sjsg 	cb->fence = &rq->submit;
565c349dbc7Sjsg 	i915_sw_fence_await(cb->fence);
566c349dbc7Sjsg 	init_irq_work(&cb->work, irq_execute_cb);
567c349dbc7Sjsg 
568ad8b1aafSjsg 	/*
569ad8b1aafSjsg 	 * Register the callback first, then see if the signaler is already
570ad8b1aafSjsg 	 * active. This ensures that if we race with the
571ad8b1aafSjsg 	 * __notify_execute_cb from i915_request_submit() and we are not
572ad8b1aafSjsg 	 * included in that list, we get a second bite of the cherry and
573ad8b1aafSjsg 	 * execute it ourselves. After this point, a future
574ad8b1aafSjsg 	 * i915_request_submit() will notify us.
575ad8b1aafSjsg 	 *
576ad8b1aafSjsg 	 * In i915_request_retire() we set the ACTIVE bit on a completed
577ad8b1aafSjsg 	 * request (then flush the execute_cb). So by registering the
578ad8b1aafSjsg 	 * callback first, then checking the ACTIVE bit, we serialise with
579ad8b1aafSjsg 	 * the completed/retired request.
580ad8b1aafSjsg 	 */
5815ca02815Sjsg 	if (llist_add(&cb->work.node.llist, &signal->execute_cb)) {
582ad8b1aafSjsg 		if (i915_request_is_active(signal) ||
583ad8b1aafSjsg 		    __request_in_flight(signal))
5845ca02815Sjsg 			i915_request_notify_execute_cb_imm(signal);
585c349dbc7Sjsg 	}
586c349dbc7Sjsg 
587c349dbc7Sjsg 	return 0;
588c349dbc7Sjsg }
589c349dbc7Sjsg 
fatal_error(int error)590c349dbc7Sjsg static bool fatal_error(int error)
591c349dbc7Sjsg {
592c349dbc7Sjsg 	switch (error) {
593c349dbc7Sjsg 	case 0: /* not an error! */
594c349dbc7Sjsg 	case -EAGAIN: /* innocent victim of a GT reset (__i915_request_reset) */
595c349dbc7Sjsg 	case -ETIMEDOUT: /* waiting for Godot (timer_i915_sw_fence_wake) */
596c349dbc7Sjsg 		return false;
597c349dbc7Sjsg 	default:
598c349dbc7Sjsg 		return true;
599c349dbc7Sjsg 	}
600c349dbc7Sjsg }
601c349dbc7Sjsg 
__i915_request_skip(struct i915_request * rq)602c349dbc7Sjsg void __i915_request_skip(struct i915_request *rq)
603c349dbc7Sjsg {
604c349dbc7Sjsg 	GEM_BUG_ON(!fatal_error(rq->fence.error));
605c349dbc7Sjsg 
606c349dbc7Sjsg 	if (rq->infix == rq->postfix)
6077f4dd379Sjsg 		return;
6087f4dd379Sjsg 
6095ca02815Sjsg 	RQ_TRACE(rq, "error: %d\n", rq->fence.error);
6105ca02815Sjsg 
611c349dbc7Sjsg 	/*
612c349dbc7Sjsg 	 * As this request likely depends on state from the lost
613c349dbc7Sjsg 	 * context, clear out all the user operations leaving the
614c349dbc7Sjsg 	 * breadcrumb at the end (so we get the fence notifications).
615c349dbc7Sjsg 	 */
616c349dbc7Sjsg 	__i915_request_fill(rq, 0);
617c349dbc7Sjsg 	rq->infix = rq->postfix;
618c349dbc7Sjsg }
619c349dbc7Sjsg 
i915_request_set_error_once(struct i915_request * rq,int error)6205ca02815Sjsg bool i915_request_set_error_once(struct i915_request *rq, int error)
621c349dbc7Sjsg {
622c349dbc7Sjsg 	int old;
623c349dbc7Sjsg 
624c349dbc7Sjsg 	GEM_BUG_ON(!IS_ERR_VALUE((long)error));
625c349dbc7Sjsg 
626c349dbc7Sjsg 	if (i915_request_signaled(rq))
6275ca02815Sjsg 		return false;
628c349dbc7Sjsg 
629c349dbc7Sjsg 	old = READ_ONCE(rq->fence.error);
6307f4dd379Sjsg 	do {
631c349dbc7Sjsg 		if (fatal_error(old))
6325ca02815Sjsg 			return false;
633c349dbc7Sjsg 	} while (!try_cmpxchg(&rq->fence.error, &old, error));
6345ca02815Sjsg 
6355ca02815Sjsg 	return true;
6365ca02815Sjsg }
6375ca02815Sjsg 
i915_request_mark_eio(struct i915_request * rq)6385ca02815Sjsg struct i915_request *i915_request_mark_eio(struct i915_request *rq)
6395ca02815Sjsg {
6405ca02815Sjsg 	if (__i915_request_is_complete(rq))
6415ca02815Sjsg 		return NULL;
6425ca02815Sjsg 
6435ca02815Sjsg 	GEM_BUG_ON(i915_request_signaled(rq));
6445ca02815Sjsg 
6455ca02815Sjsg 	/* As soon as the request is completed, it may be retired */
6465ca02815Sjsg 	rq = i915_request_get(rq);
6475ca02815Sjsg 
6485ca02815Sjsg 	i915_request_set_error_once(rq, -EIO);
6495ca02815Sjsg 	i915_request_mark_complete(rq);
6505ca02815Sjsg 
6515ca02815Sjsg 	return rq;
6527f4dd379Sjsg }
6537f4dd379Sjsg 
__i915_request_submit(struct i915_request * request)654c349dbc7Sjsg bool __i915_request_submit(struct i915_request *request)
6557f4dd379Sjsg {
6567f4dd379Sjsg 	struct intel_engine_cs *engine = request->engine;
657c349dbc7Sjsg 	bool result = false;
6587f4dd379Sjsg 
659c349dbc7Sjsg 	RQ_TRACE(request, "\n");
6607f4dd379Sjsg 
6617f4dd379Sjsg 	GEM_BUG_ON(!irqs_disabled());
6625ca02815Sjsg 	lockdep_assert_held(&engine->sched_engine->lock);
6637f4dd379Sjsg 
664c349dbc7Sjsg 	/*
665c349dbc7Sjsg 	 * With the advent of preempt-to-busy, we frequently encounter
666c349dbc7Sjsg 	 * requests that we have unsubmitted from HW, but left running
667c349dbc7Sjsg 	 * until the next ack and so have completed in the meantime. On
668c349dbc7Sjsg 	 * resubmission of that completed request, we can skip
669c349dbc7Sjsg 	 * updating the payload, and execlists can even skip submitting
670c349dbc7Sjsg 	 * the request.
671c349dbc7Sjsg 	 *
672c349dbc7Sjsg 	 * We must remove the request from the caller's priority queue,
673c349dbc7Sjsg 	 * and the caller must only call us when the request is in their
6745ca02815Sjsg 	 * priority queue, under the sched_engine->lock. This ensures that the
675c349dbc7Sjsg 	 * request has *not* yet been retired and we can safely move
676c349dbc7Sjsg 	 * the request into the engine->active.list where it will be
677c349dbc7Sjsg 	 * dropped upon retiring. (Otherwise if resubmit a *retired*
678c349dbc7Sjsg 	 * request, this would be a horrible use-after-free.)
679c349dbc7Sjsg 	 */
6805ca02815Sjsg 	if (__i915_request_is_complete(request)) {
6815ca02815Sjsg 		list_del_init(&request->sched.link);
6825ca02815Sjsg 		goto active;
6835ca02815Sjsg 	}
684ad8b1aafSjsg 
6851bb76ff1Sjsg 	if (unlikely(!intel_context_is_schedulable(request->context)))
686c349dbc7Sjsg 		i915_request_set_error_once(request, -EIO);
687ad8b1aafSjsg 
688c349dbc7Sjsg 	if (unlikely(fatal_error(request->fence.error)))
689c349dbc7Sjsg 		__i915_request_skip(request);
6907f4dd379Sjsg 
691c349dbc7Sjsg 	/*
692c349dbc7Sjsg 	 * Are we using semaphores when the gpu is already saturated?
693c349dbc7Sjsg 	 *
694c349dbc7Sjsg 	 * Using semaphores incurs a cost in having the GPU poll a
695c349dbc7Sjsg 	 * memory location, busywaiting for it to change. The continual
696c349dbc7Sjsg 	 * memory reads can have a noticeable impact on the rest of the
697c349dbc7Sjsg 	 * system with the extra bus traffic, stalling the cpu as it too
698c349dbc7Sjsg 	 * tries to access memory across the bus (perf stat -e bus-cycles).
699c349dbc7Sjsg 	 *
700c349dbc7Sjsg 	 * If we installed a semaphore on this request and we only submit
701c349dbc7Sjsg 	 * the request after the signaler completed, that indicates the
702c349dbc7Sjsg 	 * system is overloaded and using semaphores at this time only
703c349dbc7Sjsg 	 * increases the amount of work we are doing. If so, we disable
704c349dbc7Sjsg 	 * further use of semaphores until we are idle again, whence we
705c349dbc7Sjsg 	 * optimistically try again.
706c349dbc7Sjsg 	 */
707c349dbc7Sjsg 	if (request->sched.semaphores &&
708c349dbc7Sjsg 	    i915_sw_fence_signaled(&request->semaphore))
709c349dbc7Sjsg 		engine->saturated |= request->sched.semaphores;
7107f4dd379Sjsg 
711c349dbc7Sjsg 	engine->emit_fini_breadcrumb(request,
7127f4dd379Sjsg 				     request->ring->vaddr + request->postfix);
7137f4dd379Sjsg 
7147f4dd379Sjsg 	trace_i915_request_execute(request);
7155ca02815Sjsg 	if (engine->bump_serial)
7165ca02815Sjsg 		engine->bump_serial(engine);
7175ca02815Sjsg 	else
718c349dbc7Sjsg 		engine->serial++;
7195ca02815Sjsg 
720c349dbc7Sjsg 	result = true;
7217f4dd379Sjsg 
7225ca02815Sjsg 	GEM_BUG_ON(test_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags));
7235ca02815Sjsg 	engine->add_active_request(request);
7245ca02815Sjsg active:
725c349dbc7Sjsg 	clear_bit(I915_FENCE_FLAG_PQUEUE, &request->fence.flags);
7265ca02815Sjsg 	set_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags);
727c349dbc7Sjsg 
728ad8b1aafSjsg 	/*
729ad8b1aafSjsg 	 * XXX Rollback bonded-execution on __i915_request_unsubmit()?
730ad8b1aafSjsg 	 *
731ad8b1aafSjsg 	 * In the future, perhaps when we have an active time-slicing scheduler,
732ad8b1aafSjsg 	 * it will be interesting to unsubmit parallel execution and remove
733ad8b1aafSjsg 	 * busywaits from the GPU until their master is restarted. This is
734ad8b1aafSjsg 	 * quite hairy, we have to carefully rollback the fence and do a
735ad8b1aafSjsg 	 * preempt-to-idle cycle on the target engine, all the while the
736ad8b1aafSjsg 	 * master execute_cb may refire.
737ad8b1aafSjsg 	 */
738ad8b1aafSjsg 	__notify_execute_cb_irq(request);
739c349dbc7Sjsg 
740ad8b1aafSjsg 	/* We may be recursing from the signal callback of another i915 fence */
741ad8b1aafSjsg 	if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
742ad8b1aafSjsg 		i915_request_enable_breadcrumb(request);
743c349dbc7Sjsg 
744c349dbc7Sjsg 	return result;
7457f4dd379Sjsg }
7467f4dd379Sjsg 
i915_request_submit(struct i915_request * request)7477f4dd379Sjsg void i915_request_submit(struct i915_request *request)
7487f4dd379Sjsg {
7497f4dd379Sjsg 	struct intel_engine_cs *engine = request->engine;
7507f4dd379Sjsg 	unsigned long flags;
7517f4dd379Sjsg 
7527f4dd379Sjsg 	/* Will be called from irq-context when using foreign fences. */
7535ca02815Sjsg 	spin_lock_irqsave(&engine->sched_engine->lock, flags);
7547f4dd379Sjsg 
7557f4dd379Sjsg 	__i915_request_submit(request);
7567f4dd379Sjsg 
7575ca02815Sjsg 	spin_unlock_irqrestore(&engine->sched_engine->lock, flags);
7587f4dd379Sjsg }
7597f4dd379Sjsg 
__i915_request_unsubmit(struct i915_request * request)7607f4dd379Sjsg void __i915_request_unsubmit(struct i915_request *request)
7617f4dd379Sjsg {
7627f4dd379Sjsg 	struct intel_engine_cs *engine = request->engine;
7637f4dd379Sjsg 
764ad8b1aafSjsg 	/*
765ad8b1aafSjsg 	 * Only unwind in reverse order, required so that the per-context list
766ad8b1aafSjsg 	 * is kept in seqno/ring order.
767ad8b1aafSjsg 	 */
768c349dbc7Sjsg 	RQ_TRACE(request, "\n");
7697f4dd379Sjsg 
7707f4dd379Sjsg 	GEM_BUG_ON(!irqs_disabled());
7715ca02815Sjsg 	lockdep_assert_held(&engine->sched_engine->lock);
7727f4dd379Sjsg 
7737f4dd379Sjsg 	/*
774ad8b1aafSjsg 	 * Before we remove this breadcrumb from the signal list, we have
775ad8b1aafSjsg 	 * to ensure that a concurrent dma_fence_enable_signaling() does not
776ad8b1aafSjsg 	 * attach itself. We first mark the request as no longer active and
777ad8b1aafSjsg 	 * make sure that is visible to other cores, and then remove the
778ad8b1aafSjsg 	 * breadcrumb if attached.
7797f4dd379Sjsg 	 */
780ad8b1aafSjsg 	GEM_BUG_ON(!test_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags));
781ad8b1aafSjsg 	clear_bit_unlock(I915_FENCE_FLAG_ACTIVE, &request->fence.flags);
7827f4dd379Sjsg 	if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
783c349dbc7Sjsg 		i915_request_cancel_breadcrumb(request);
784c349dbc7Sjsg 
785c349dbc7Sjsg 	/* We've already spun, don't charge on resubmitting. */
7865ca02815Sjsg 	if (request->sched.semaphores && __i915_request_has_started(request))
787c349dbc7Sjsg 		request->sched.semaphores = 0;
7887f4dd379Sjsg 
7897f4dd379Sjsg 	/*
7907f4dd379Sjsg 	 * We don't need to wake_up any waiters on request->execute, they
7917f4dd379Sjsg 	 * will get woken by any other event or us re-adding this request
7927f4dd379Sjsg 	 * to the engine timeline (__i915_request_submit()). The waiters
7937f4dd379Sjsg 	 * should be quite adapt at finding that the request now has a new
7947f4dd379Sjsg 	 * global_seqno to the one they went to sleep on.
7957f4dd379Sjsg 	 */
7967f4dd379Sjsg }
7977f4dd379Sjsg 
i915_request_unsubmit(struct i915_request * request)7987f4dd379Sjsg void i915_request_unsubmit(struct i915_request *request)
7997f4dd379Sjsg {
8007f4dd379Sjsg 	struct intel_engine_cs *engine = request->engine;
8017f4dd379Sjsg 	unsigned long flags;
8027f4dd379Sjsg 
8037f4dd379Sjsg 	/* Will be called from irq-context when using foreign fences. */
8045ca02815Sjsg 	spin_lock_irqsave(&engine->sched_engine->lock, flags);
8057f4dd379Sjsg 
8067f4dd379Sjsg 	__i915_request_unsubmit(request);
8077f4dd379Sjsg 
8085ca02815Sjsg 	spin_unlock_irqrestore(&engine->sched_engine->lock, flags);
8095ca02815Sjsg }
8105ca02815Sjsg 
i915_request_cancel(struct i915_request * rq,int error)8115ca02815Sjsg void i915_request_cancel(struct i915_request *rq, int error)
8125ca02815Sjsg {
8135ca02815Sjsg 	if (!i915_request_set_error_once(rq, error))
8145ca02815Sjsg 		return;
8155ca02815Sjsg 
8165ca02815Sjsg 	set_bit(I915_FENCE_FLAG_SENTINEL, &rq->fence.flags);
8175ca02815Sjsg 
8185ca02815Sjsg 	intel_context_cancel_request(rq->context, rq);
8197f4dd379Sjsg }
8207f4dd379Sjsg 
8211bb76ff1Sjsg static int
submit_notify(struct i915_sw_fence * fence,enum i915_sw_fence_notify state)8227f4dd379Sjsg submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
8237f4dd379Sjsg {
8247f4dd379Sjsg 	struct i915_request *request =
8257f4dd379Sjsg 		container_of(fence, typeof(*request), submit);
8267f4dd379Sjsg 
8277f4dd379Sjsg 	switch (state) {
8287f4dd379Sjsg 	case FENCE_COMPLETE:
8297f4dd379Sjsg 		trace_i915_request_submit(request);
830c349dbc7Sjsg 
831c349dbc7Sjsg 		if (unlikely(fence->error))
832c349dbc7Sjsg 			i915_request_set_error_once(request, fence->error);
8335ca02815Sjsg 		else
8345ca02815Sjsg 			__rq_arm_watchdog(request);
835c349dbc7Sjsg 
8367f4dd379Sjsg 		/*
8377f4dd379Sjsg 		 * We need to serialize use of the submit_request() callback
8387f4dd379Sjsg 		 * with its hotplugging performed during an emergency
8397f4dd379Sjsg 		 * i915_gem_set_wedged().  We use the RCU mechanism to mark the
8407f4dd379Sjsg 		 * critical section in order to force i915_gem_set_wedged() to
8417f4dd379Sjsg 		 * wait until the submit_request() is completed before
8427f4dd379Sjsg 		 * proceeding.
8437f4dd379Sjsg 		 */
8447f4dd379Sjsg 		rcu_read_lock();
8457f4dd379Sjsg 		request->engine->submit_request(request);
8467f4dd379Sjsg 		rcu_read_unlock();
8477f4dd379Sjsg 		break;
8487f4dd379Sjsg 
8497f4dd379Sjsg 	case FENCE_FREE:
8507f4dd379Sjsg 		i915_request_put(request);
8517f4dd379Sjsg 		break;
8527f4dd379Sjsg 	}
8537f4dd379Sjsg 
8547f4dd379Sjsg 	return NOTIFY_DONE;
8557f4dd379Sjsg }
8567f4dd379Sjsg 
8571bb76ff1Sjsg static int
semaphore_notify(struct i915_sw_fence * fence,enum i915_sw_fence_notify state)858c349dbc7Sjsg semaphore_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
859c349dbc7Sjsg {
860c349dbc7Sjsg 	struct i915_request *rq = container_of(fence, typeof(*rq), semaphore);
861c349dbc7Sjsg 
862c349dbc7Sjsg 	switch (state) {
863c349dbc7Sjsg 	case FENCE_COMPLETE:
864c349dbc7Sjsg 		break;
865c349dbc7Sjsg 
866c349dbc7Sjsg 	case FENCE_FREE:
867c349dbc7Sjsg 		i915_request_put(rq);
868c349dbc7Sjsg 		break;
869c349dbc7Sjsg 	}
870c349dbc7Sjsg 
871c349dbc7Sjsg 	return NOTIFY_DONE;
872c349dbc7Sjsg }
873c349dbc7Sjsg 
retire_requests(struct intel_timeline * tl)874c349dbc7Sjsg static void retire_requests(struct intel_timeline *tl)
875c349dbc7Sjsg {
876c349dbc7Sjsg 	struct i915_request *rq, *rn;
877c349dbc7Sjsg 
878c349dbc7Sjsg 	list_for_each_entry_safe(rq, rn, &tl->requests, link)
879c349dbc7Sjsg 		if (!i915_request_retire(rq))
880c349dbc7Sjsg 			break;
881c349dbc7Sjsg }
882c349dbc7Sjsg 
883c349dbc7Sjsg static void __i915_request_ctor(void *);
884c349dbc7Sjsg 
885c349dbc7Sjsg static noinline struct i915_request *
request_alloc_slow(struct intel_timeline * tl,struct i915_request ** rsvd,gfp_t gfp)886ad8b1aafSjsg request_alloc_slow(struct intel_timeline *tl,
887ad8b1aafSjsg 		   struct i915_request **rsvd,
888ad8b1aafSjsg 		   gfp_t gfp)
889c349dbc7Sjsg {
8907f4dd379Sjsg 	struct i915_request *rq;
8917f4dd379Sjsg 
892ad8b1aafSjsg 	/* If we cannot wait, dip into our reserves */
893ad8b1aafSjsg 	if (!gfpflags_allow_blocking(gfp)) {
894ad8b1aafSjsg 		rq = xchg(rsvd, NULL);
895ad8b1aafSjsg 		if (!rq) /* Use the normal failure path for one final WARN */
896c349dbc7Sjsg 			goto out;
8977f4dd379Sjsg 
898ad8b1aafSjsg 		return rq;
899ad8b1aafSjsg 	}
900ad8b1aafSjsg 
901ad8b1aafSjsg 	if (list_empty(&tl->requests))
902c349dbc7Sjsg 		goto out;
9037f4dd379Sjsg 
9047f4dd379Sjsg 	/* Move our oldest request to the slab-cache (if not in use!) */
905c349dbc7Sjsg 	rq = list_first_entry(&tl->requests, typeof(*rq), link);
9067f4dd379Sjsg 	i915_request_retire(rq);
9077f4dd379Sjsg 
908c349dbc7Sjsg #ifdef __linux__
9095ca02815Sjsg 	rq = kmem_cache_alloc(slab_requests,
910c349dbc7Sjsg 			      gfp | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
911c349dbc7Sjsg #else
9125ca02815Sjsg 	rq = pool_get(&slab_requests,
913c349dbc7Sjsg 	    (gfp & GFP_NOWAIT) ? PR_NOWAIT : PR_WAITOK);
914c349dbc7Sjsg 	if (rq)
915c349dbc7Sjsg 		__i915_request_ctor(rq);
916c349dbc7Sjsg #endif
917c349dbc7Sjsg 	if (rq)
918c349dbc7Sjsg 		return rq;
919c349dbc7Sjsg 
920c349dbc7Sjsg 	/* Ratelimit ourselves to prevent oom from malicious clients */
921c349dbc7Sjsg 	rq = list_last_entry(&tl->requests, typeof(*rq), link);
922c349dbc7Sjsg 	cond_synchronize_rcu(rq->rcustate);
923c349dbc7Sjsg 
924c349dbc7Sjsg 	/* Retire our old requests in the hope that we free some */
925c349dbc7Sjsg 	retire_requests(tl);
926c349dbc7Sjsg 
927c349dbc7Sjsg out:
928c349dbc7Sjsg #ifdef __linux__
9295ca02815Sjsg 	return kmem_cache_alloc(slab_requests, gfp);
930c349dbc7Sjsg #else
9315ca02815Sjsg 	rq = pool_get(&slab_requests,
932c349dbc7Sjsg 	    (gfp & GFP_NOWAIT) ? PR_NOWAIT : PR_WAITOK);
933c349dbc7Sjsg 	if (rq)
934c349dbc7Sjsg 		__i915_request_ctor(rq);
935c349dbc7Sjsg 	return rq;
936c349dbc7Sjsg #endif
937c349dbc7Sjsg }
938c349dbc7Sjsg 
__i915_request_ctor(void * arg)939c349dbc7Sjsg static void __i915_request_ctor(void *arg)
940c349dbc7Sjsg {
941c349dbc7Sjsg 	struct i915_request *rq = arg;
942c349dbc7Sjsg 
943ad8b1aafSjsg 	/*
944ad8b1aafSjsg 	 * witness does not understand spin_lock_nested()
945ad8b1aafSjsg 	 * order reversal in i915 with this lock
946ad8b1aafSjsg 	 */
947ad8b1aafSjsg 	mtx_init_flags(&rq->lock, IPL_TTY, NULL, MTX_NOWITNESS);
948c349dbc7Sjsg 	i915_sched_node_init(&rq->sched);
949c349dbc7Sjsg 	i915_sw_fence_init(&rq->submit, submit_notify);
950c349dbc7Sjsg 	i915_sw_fence_init(&rq->semaphore, semaphore_notify);
951c349dbc7Sjsg 
9521bb76ff1Sjsg 	clear_capture_list(rq);
9531bb76ff1Sjsg 	rq->batch_res = NULL;
954c349dbc7Sjsg 
955ad8b1aafSjsg 	init_llist_head(&rq->execute_cb);
956c349dbc7Sjsg }
957c349dbc7Sjsg 
9581bb76ff1Sjsg #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
9591bb76ff1Sjsg #define clear_batch_ptr(_rq) ((_rq)->batch = NULL)
9601bb76ff1Sjsg #else
9611bb76ff1Sjsg #define clear_batch_ptr(_a) do {} while (0)
9621bb76ff1Sjsg #endif
9631bb76ff1Sjsg 
964c349dbc7Sjsg struct i915_request *
__i915_request_create(struct intel_context * ce,gfp_t gfp)965c349dbc7Sjsg __i915_request_create(struct intel_context *ce, gfp_t gfp)
966c349dbc7Sjsg {
967c349dbc7Sjsg 	struct intel_timeline *tl = ce->timeline;
968c349dbc7Sjsg 	struct i915_request *rq;
969c349dbc7Sjsg 	u32 seqno;
970c349dbc7Sjsg 	int ret;
971c349dbc7Sjsg 
9725ca02815Sjsg 	might_alloc(gfp);
973c349dbc7Sjsg 
974c349dbc7Sjsg 	/* Check that the caller provided an already pinned context */
975c349dbc7Sjsg 	__intel_context_pin(ce);
976c349dbc7Sjsg 
9777f4dd379Sjsg 	/*
9787f4dd379Sjsg 	 * Beware: Dragons be flying overhead.
9797f4dd379Sjsg 	 *
9807f4dd379Sjsg 	 * We use RCU to look up requests in flight. The lookups may
9817f4dd379Sjsg 	 * race with the request being allocated from the slab freelist.
9827f4dd379Sjsg 	 * That is the request we are writing to here, may be in the process
983c349dbc7Sjsg 	 * of being read by __i915_active_request_get_rcu(). As such,
9847f4dd379Sjsg 	 * we have to be very careful when overwriting the contents. During
9857f4dd379Sjsg 	 * the RCU lookup, we change chase the request->engine pointer,
9867f4dd379Sjsg 	 * read the request->global_seqno and increment the reference count.
9877f4dd379Sjsg 	 *
9887f4dd379Sjsg 	 * The reference count is incremented atomically. If it is zero,
9897f4dd379Sjsg 	 * the lookup knows the request is unallocated and complete. Otherwise,
9907f4dd379Sjsg 	 * it is either still in use, or has been reallocated and reset
9917f4dd379Sjsg 	 * with dma_fence_init(). This increment is safe for release as we
9927f4dd379Sjsg 	 * check that the request we have a reference to and matches the active
9937f4dd379Sjsg 	 * request.
9947f4dd379Sjsg 	 *
9957f4dd379Sjsg 	 * Before we increment the refcount, we chase the request->engine
9967f4dd379Sjsg 	 * pointer. We must not call kmem_cache_zalloc() or else we set
9977f4dd379Sjsg 	 * that pointer to NULL and cause a crash during the lookup. If
9987f4dd379Sjsg 	 * we see the request is completed (based on the value of the
9997f4dd379Sjsg 	 * old engine and seqno), the lookup is complete and reports NULL.
10007f4dd379Sjsg 	 * If we decide the request is not completed (new engine or seqno),
10017f4dd379Sjsg 	 * then we grab a reference and double check that it is still the
10027f4dd379Sjsg 	 * active request - which it won't be and restart the lookup.
10037f4dd379Sjsg 	 *
10047f4dd379Sjsg 	 * Do not use kmem_cache_zalloc() here!
10057f4dd379Sjsg 	 */
10067f4dd379Sjsg #ifdef __linux__
10075ca02815Sjsg 	rq = kmem_cache_alloc(slab_requests,
1008c349dbc7Sjsg 			      gfp | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
10097f4dd379Sjsg #else
10105ca02815Sjsg 	rq = pool_get(&slab_requests,
1011c349dbc7Sjsg 	    (gfp & GFP_NOWAIT) ? PR_NOWAIT : PR_WAITOK);
1012c349dbc7Sjsg 	if (rq)
1013c349dbc7Sjsg 		__i915_request_ctor(rq);
10147f4dd379Sjsg #endif
10157f4dd379Sjsg 	if (unlikely(!rq)) {
1016ad8b1aafSjsg 		rq = request_alloc_slow(tl, &ce->engine->request_pool, gfp);
10177f4dd379Sjsg 		if (!rq) {
10187f4dd379Sjsg 			ret = -ENOMEM;
10197f4dd379Sjsg 			goto err_unreserve;
10207f4dd379Sjsg 		}
10217f4dd379Sjsg 	}
10227f4dd379Sjsg 
10231bb76ff1Sjsg 	rq->context = ce;
1024c349dbc7Sjsg 	rq->engine = ce->engine;
10257f4dd379Sjsg 	rq->ring = ce->ring;
1026c349dbc7Sjsg 	rq->execution_mask = ce->engine->mask;
10271bb76ff1Sjsg 	rq->i915 = ce->engine->i915;
10287f4dd379Sjsg 
1029c349dbc7Sjsg 	ret = intel_timeline_get_seqno(tl, rq, &seqno);
1030c349dbc7Sjsg 	if (ret)
1031c349dbc7Sjsg 		goto err_free;
1032c349dbc7Sjsg 
10330a9c143cSjsg 	dma_fence_init(&rq->fence, &i915_fence_ops, &rq->lock,
10340a9c143cSjsg 		       tl->fence_context, seqno);
1035c349dbc7Sjsg 
1036c349dbc7Sjsg 	RCU_INIT_POINTER(rq->timeline, tl);
1037c349dbc7Sjsg 	rq->hwsp_seqno = tl->hwsp_seqno;
10385ca02815Sjsg 	GEM_BUG_ON(__i915_request_is_complete(rq));
1039c349dbc7Sjsg 
1040c349dbc7Sjsg 	rq->rcustate = get_state_synchronize_rcu(); /* acts as smp_mb() */
10417f4dd379Sjsg 
10425ca02815Sjsg 	rq->guc_prio = GUC_PRIO_INIT;
10435ca02815Sjsg 
10447f4dd379Sjsg 	/* We bump the ref for the fence chain */
1045c349dbc7Sjsg 	i915_sw_fence_reinit(&i915_request_get(rq)->submit);
1046c349dbc7Sjsg 	i915_sw_fence_reinit(&i915_request_get(rq)->semaphore);
10477f4dd379Sjsg 
1048c349dbc7Sjsg 	i915_sched_node_reinit(&rq->sched);
10497f4dd379Sjsg 
1050c349dbc7Sjsg 	/* No zalloc, everything must be cleared after use */
10511bb76ff1Sjsg 	clear_batch_ptr(rq);
10525ca02815Sjsg 	__rq_init_watchdog(rq);
10531bb76ff1Sjsg 	assert_capture_list_is_null(rq);
1054ad8b1aafSjsg 	GEM_BUG_ON(!llist_empty(&rq->execute_cb));
10551bb76ff1Sjsg 	GEM_BUG_ON(rq->batch_res);
10567f4dd379Sjsg 
10577f4dd379Sjsg 	/*
10587f4dd379Sjsg 	 * Reserve space in the ring buffer for all the commands required to
10597f4dd379Sjsg 	 * eventually emit this request. This is to guarantee that the
10607f4dd379Sjsg 	 * i915_request_add() call can't fail. Note that the reserve may need
10617f4dd379Sjsg 	 * to be redone if the request is not actually submitted straight
10627f4dd379Sjsg 	 * away, e.g. because a GPU scheduler has deferred it.
1063c349dbc7Sjsg 	 *
1064c349dbc7Sjsg 	 * Note that due to how we add reserved_space to intel_ring_begin()
1065c349dbc7Sjsg 	 * we need to double our request to ensure that if we need to wrap
1066c349dbc7Sjsg 	 * around inside i915_request_add() there is sufficient space at
1067c349dbc7Sjsg 	 * the beginning of the ring as well.
10687f4dd379Sjsg 	 */
1069c349dbc7Sjsg 	rq->reserved_space =
1070c349dbc7Sjsg 		2 * rq->engine->emit_fini_breadcrumb_dw * sizeof(u32);
10717f4dd379Sjsg 
10727f4dd379Sjsg 	/*
10737f4dd379Sjsg 	 * Record the position of the start of the request so that
10747f4dd379Sjsg 	 * should we detect the updated seqno part-way through the
10757f4dd379Sjsg 	 * GPU processing the request, we never over-estimate the
10767f4dd379Sjsg 	 * position of the head.
10777f4dd379Sjsg 	 */
10787f4dd379Sjsg 	rq->head = rq->ring->emit;
10797f4dd379Sjsg 
1080c349dbc7Sjsg 	ret = rq->engine->request_alloc(rq);
10817f4dd379Sjsg 	if (ret)
10827f4dd379Sjsg 		goto err_unwind;
10837f4dd379Sjsg 
10847f4dd379Sjsg 	rq->infix = rq->ring->emit; /* end of header; start of user payload */
10857f4dd379Sjsg 
1086c349dbc7Sjsg 	intel_context_mark_active(ce);
1087c349dbc7Sjsg 	list_add_tail_rcu(&rq->link, &tl->requests);
1088c349dbc7Sjsg 
10897f4dd379Sjsg 	return rq;
10907f4dd379Sjsg 
10917f4dd379Sjsg err_unwind:
10927f4dd379Sjsg 	ce->ring->emit = rq->head;
10937f4dd379Sjsg 
10947f4dd379Sjsg 	/* Make sure we didn't add ourselves to external state before freeing */
10957f4dd379Sjsg 	GEM_BUG_ON(!list_empty(&rq->sched.signalers_list));
10967f4dd379Sjsg 	GEM_BUG_ON(!list_empty(&rq->sched.waiters_list));
10977f4dd379Sjsg 
1098c349dbc7Sjsg err_free:
10997f4dd379Sjsg #ifdef __linux__
11005ca02815Sjsg 	kmem_cache_free(slab_requests, rq);
11017f4dd379Sjsg #else
11025ca02815Sjsg 	pool_put(&slab_requests, rq);
11037f4dd379Sjsg #endif
11047f4dd379Sjsg err_unreserve:
11057f4dd379Sjsg 	intel_context_unpin(ce);
11067f4dd379Sjsg 	return ERR_PTR(ret);
11077f4dd379Sjsg }
11087f4dd379Sjsg 
1109c349dbc7Sjsg struct i915_request *
i915_request_create(struct intel_context * ce)1110c349dbc7Sjsg i915_request_create(struct intel_context *ce)
1111c349dbc7Sjsg {
1112c349dbc7Sjsg 	struct i915_request *rq;
1113c349dbc7Sjsg 	struct intel_timeline *tl;
1114c349dbc7Sjsg 
1115c349dbc7Sjsg 	tl = intel_context_timeline_lock(ce);
1116c349dbc7Sjsg 	if (IS_ERR(tl))
1117c349dbc7Sjsg 		return ERR_CAST(tl);
1118c349dbc7Sjsg 
1119c349dbc7Sjsg 	/* Move our oldest request to the slab-cache (if not in use!) */
1120c349dbc7Sjsg 	rq = list_first_entry(&tl->requests, typeof(*rq), link);
1121c349dbc7Sjsg 	if (!list_is_last(&rq->link, &tl->requests))
1122c349dbc7Sjsg 		i915_request_retire(rq);
1123c349dbc7Sjsg 
1124c349dbc7Sjsg 	intel_context_enter(ce);
1125c349dbc7Sjsg 	rq = __i915_request_create(ce, GFP_KERNEL);
1126c349dbc7Sjsg 	intel_context_exit(ce); /* active reference transferred to request */
1127c349dbc7Sjsg 	if (IS_ERR(rq))
1128c349dbc7Sjsg 		goto err_unlock;
1129c349dbc7Sjsg 
1130c349dbc7Sjsg 	/* Check that we do not interrupt ourselves with a new request */
1131c349dbc7Sjsg 	rq->cookie = lockdep_pin_lock(&tl->mutex);
1132c349dbc7Sjsg 
1133c349dbc7Sjsg 	return rq;
1134c349dbc7Sjsg 
1135c349dbc7Sjsg err_unlock:
1136c349dbc7Sjsg 	intel_context_timeline_unlock(tl);
1137c349dbc7Sjsg 	return rq;
1138c349dbc7Sjsg }
1139c349dbc7Sjsg 
1140c349dbc7Sjsg static int
i915_request_await_start(struct i915_request * rq,struct i915_request * signal)1141c349dbc7Sjsg i915_request_await_start(struct i915_request *rq, struct i915_request *signal)
1142c349dbc7Sjsg {
1143c349dbc7Sjsg 	struct dma_fence *fence;
1144c349dbc7Sjsg 	int err;
1145c349dbc7Sjsg 
1146c349dbc7Sjsg 	if (i915_request_timeline(rq) == rcu_access_pointer(signal->timeline))
1147c349dbc7Sjsg 		return 0;
1148c349dbc7Sjsg 
1149c349dbc7Sjsg 	if (i915_request_started(signal))
1150c349dbc7Sjsg 		return 0;
1151c349dbc7Sjsg 
11525ca02815Sjsg 	/*
11535ca02815Sjsg 	 * The caller holds a reference on @signal, but we do not serialise
11545ca02815Sjsg 	 * against it being retired and removed from the lists.
11555ca02815Sjsg 	 *
11565ca02815Sjsg 	 * We do not hold a reference to the request before @signal, and
11575ca02815Sjsg 	 * so must be very careful to ensure that it is not _recycled_ as
11585ca02815Sjsg 	 * we follow the link backwards.
11595ca02815Sjsg 	 */
1160c349dbc7Sjsg 	fence = NULL;
1161c349dbc7Sjsg 	rcu_read_lock();
1162c349dbc7Sjsg 	do {
1163c349dbc7Sjsg 		struct list_head *pos = READ_ONCE(signal->link.prev);
1164c349dbc7Sjsg 		struct i915_request *prev;
1165c349dbc7Sjsg 
1166c349dbc7Sjsg 		/* Confirm signal has not been retired, the link is valid */
11675ca02815Sjsg 		if (unlikely(__i915_request_has_started(signal)))
1168c349dbc7Sjsg 			break;
1169c349dbc7Sjsg 
1170c349dbc7Sjsg 		/* Is signal the earliest request on its timeline? */
1171c349dbc7Sjsg 		if (pos == &rcu_dereference(signal->timeline)->requests)
1172c349dbc7Sjsg 			break;
1173c349dbc7Sjsg 
1174c349dbc7Sjsg 		/*
1175c349dbc7Sjsg 		 * Peek at the request before us in the timeline. That
1176c349dbc7Sjsg 		 * request will only be valid before it is retired, so
1177c349dbc7Sjsg 		 * after acquiring a reference to it, confirm that it is
1178c349dbc7Sjsg 		 * still part of the signaler's timeline.
1179c349dbc7Sjsg 		 */
1180c349dbc7Sjsg 		prev = list_entry(pos, typeof(*prev), link);
1181c349dbc7Sjsg 		if (!i915_request_get_rcu(prev))
1182c349dbc7Sjsg 			break;
1183c349dbc7Sjsg 
1184c349dbc7Sjsg 		/* After the strong barrier, confirm prev is still attached */
1185c349dbc7Sjsg 		if (unlikely(READ_ONCE(prev->link.next) != &signal->link)) {
1186c349dbc7Sjsg 			i915_request_put(prev);
1187c349dbc7Sjsg 			break;
1188c349dbc7Sjsg 		}
1189c349dbc7Sjsg 
1190c349dbc7Sjsg 		fence = &prev->fence;
1191c349dbc7Sjsg 	} while (0);
1192c349dbc7Sjsg 	rcu_read_unlock();
1193c349dbc7Sjsg 	if (!fence)
1194c349dbc7Sjsg 		return 0;
1195c349dbc7Sjsg 
1196c349dbc7Sjsg 	err = 0;
1197c349dbc7Sjsg 	if (!intel_timeline_sync_is_later(i915_request_timeline(rq), fence))
1198c349dbc7Sjsg 		err = i915_sw_fence_await_dma_fence(&rq->submit,
1199c349dbc7Sjsg 						    fence, 0,
1200c349dbc7Sjsg 						    I915_FENCE_GFP);
1201c349dbc7Sjsg 	dma_fence_put(fence);
1202c349dbc7Sjsg 
1203c349dbc7Sjsg 	return err;
1204c349dbc7Sjsg }
1205c349dbc7Sjsg 
1206c349dbc7Sjsg static intel_engine_mask_t
already_busywaiting(struct i915_request * rq)1207c349dbc7Sjsg already_busywaiting(struct i915_request *rq)
1208c349dbc7Sjsg {
1209c349dbc7Sjsg 	/*
1210c349dbc7Sjsg 	 * Polling a semaphore causes bus traffic, delaying other users of
1211c349dbc7Sjsg 	 * both the GPU and CPU. We want to limit the impact on others,
1212c349dbc7Sjsg 	 * while taking advantage of early submission to reduce GPU
1213c349dbc7Sjsg 	 * latency. Therefore we restrict ourselves to not using more
1214c349dbc7Sjsg 	 * than one semaphore from each source, and not using a semaphore
1215c349dbc7Sjsg 	 * if we have detected the engine is saturated (i.e. would not be
1216c349dbc7Sjsg 	 * submitted early and cause bus traffic reading an already passed
1217c349dbc7Sjsg 	 * semaphore).
1218c349dbc7Sjsg 	 *
1219c349dbc7Sjsg 	 * See the are-we-too-late? check in __i915_request_submit().
1220c349dbc7Sjsg 	 */
1221c349dbc7Sjsg 	return rq->sched.semaphores | READ_ONCE(rq->engine->saturated);
1222c349dbc7Sjsg }
1223c349dbc7Sjsg 
1224c349dbc7Sjsg static int
__emit_semaphore_wait(struct i915_request * to,struct i915_request * from,u32 seqno)1225c349dbc7Sjsg __emit_semaphore_wait(struct i915_request *to,
1226c349dbc7Sjsg 		      struct i915_request *from,
1227c349dbc7Sjsg 		      u32 seqno)
1228c349dbc7Sjsg {
12295ca02815Sjsg 	const int has_token = GRAPHICS_VER(to->engine->i915) >= 12;
1230c349dbc7Sjsg 	u32 hwsp_offset;
1231c349dbc7Sjsg 	int len, err;
1232c349dbc7Sjsg 	u32 *cs;
1233c349dbc7Sjsg 
12345ca02815Sjsg 	GEM_BUG_ON(GRAPHICS_VER(to->engine->i915) < 8);
1235ad8b1aafSjsg 	GEM_BUG_ON(i915_request_has_initial_breadcrumb(to));
1236c349dbc7Sjsg 
1237c349dbc7Sjsg 	/* We need to pin the signaler's HWSP until we are finished reading. */
1238c349dbc7Sjsg 	err = intel_timeline_read_hwsp(from, to, &hwsp_offset);
1239c349dbc7Sjsg 	if (err)
1240c349dbc7Sjsg 		return err;
1241c349dbc7Sjsg 
1242c349dbc7Sjsg 	len = 4;
1243c349dbc7Sjsg 	if (has_token)
1244c349dbc7Sjsg 		len += 2;
1245c349dbc7Sjsg 
1246c349dbc7Sjsg 	cs = intel_ring_begin(to, len);
1247c349dbc7Sjsg 	if (IS_ERR(cs))
1248c349dbc7Sjsg 		return PTR_ERR(cs);
1249c349dbc7Sjsg 
1250c349dbc7Sjsg 	/*
1251c349dbc7Sjsg 	 * Using greater-than-or-equal here means we have to worry
1252c349dbc7Sjsg 	 * about seqno wraparound. To side step that issue, we swap
1253c349dbc7Sjsg 	 * the timeline HWSP upon wrapping, so that everyone listening
1254c349dbc7Sjsg 	 * for the old (pre-wrap) values do not see the much smaller
1255c349dbc7Sjsg 	 * (post-wrap) values than they were expecting (and so wait
1256c349dbc7Sjsg 	 * forever).
1257c349dbc7Sjsg 	 */
1258c349dbc7Sjsg 	*cs++ = (MI_SEMAPHORE_WAIT |
1259c349dbc7Sjsg 		 MI_SEMAPHORE_GLOBAL_GTT |
1260c349dbc7Sjsg 		 MI_SEMAPHORE_POLL |
1261c349dbc7Sjsg 		 MI_SEMAPHORE_SAD_GTE_SDD) +
1262c349dbc7Sjsg 		has_token;
1263c349dbc7Sjsg 	*cs++ = seqno;
1264c349dbc7Sjsg 	*cs++ = hwsp_offset;
1265c349dbc7Sjsg 	*cs++ = 0;
1266c349dbc7Sjsg 	if (has_token) {
1267c349dbc7Sjsg 		*cs++ = 0;
1268c349dbc7Sjsg 		*cs++ = MI_NOOP;
1269c349dbc7Sjsg 	}
1270c349dbc7Sjsg 
1271c349dbc7Sjsg 	intel_ring_advance(to, cs);
1272c349dbc7Sjsg 	return 0;
1273c349dbc7Sjsg }
1274c349dbc7Sjsg 
12751bb76ff1Sjsg static bool
can_use_semaphore_wait(struct i915_request * to,struct i915_request * from)12761bb76ff1Sjsg can_use_semaphore_wait(struct i915_request *to, struct i915_request *from)
12771bb76ff1Sjsg {
12781bb76ff1Sjsg 	return to->engine->gt->ggtt == from->engine->gt->ggtt;
12791bb76ff1Sjsg }
12801bb76ff1Sjsg 
1281c349dbc7Sjsg static int
emit_semaphore_wait(struct i915_request * to,struct i915_request * from,gfp_t gfp)1282c349dbc7Sjsg emit_semaphore_wait(struct i915_request *to,
1283c349dbc7Sjsg 		    struct i915_request *from,
1284c349dbc7Sjsg 		    gfp_t gfp)
1285c349dbc7Sjsg {
1286c349dbc7Sjsg 	const intel_engine_mask_t mask = READ_ONCE(from->engine)->mask;
1287ad8b1aafSjsg 	struct i915_sw_fence *wait = &to->submit;
1288c349dbc7Sjsg 
12891bb76ff1Sjsg 	if (!can_use_semaphore_wait(to, from))
12901bb76ff1Sjsg 		goto await_fence;
12911bb76ff1Sjsg 
1292c349dbc7Sjsg 	if (!intel_context_use_semaphores(to->context))
1293c349dbc7Sjsg 		goto await_fence;
1294c349dbc7Sjsg 
1295ad8b1aafSjsg 	if (i915_request_has_initial_breadcrumb(to))
1296ad8b1aafSjsg 		goto await_fence;
1297ad8b1aafSjsg 
1298ad8b1aafSjsg 	/*
1299ad8b1aafSjsg 	 * If this or its dependents are waiting on an external fence
1300ad8b1aafSjsg 	 * that may fail catastrophically, then we want to avoid using
1301*f005ef32Sjsg 	 * semaphores as they bypass the fence signaling metadata, and we
1302ad8b1aafSjsg 	 * lose the fence->error propagation.
1303ad8b1aafSjsg 	 */
1304ad8b1aafSjsg 	if (from->sched.flags & I915_SCHED_HAS_EXTERNAL_CHAIN)
1305ad8b1aafSjsg 		goto await_fence;
1306ad8b1aafSjsg 
1307c349dbc7Sjsg 	/* Just emit the first semaphore we see as request space is limited. */
1308c349dbc7Sjsg 	if (already_busywaiting(to) & mask)
1309c349dbc7Sjsg 		goto await_fence;
1310c349dbc7Sjsg 
1311c349dbc7Sjsg 	if (i915_request_await_start(to, from) < 0)
1312c349dbc7Sjsg 		goto await_fence;
1313c349dbc7Sjsg 
1314c349dbc7Sjsg 	/* Only submit our spinner after the signaler is running! */
13155ca02815Sjsg 	if (__await_execution(to, from, gfp))
1316c349dbc7Sjsg 		goto await_fence;
1317c349dbc7Sjsg 
1318c349dbc7Sjsg 	if (__emit_semaphore_wait(to, from, from->fence.seqno))
1319c349dbc7Sjsg 		goto await_fence;
1320c349dbc7Sjsg 
1321c349dbc7Sjsg 	to->sched.semaphores |= mask;
1322ad8b1aafSjsg 	wait = &to->semaphore;
1323c349dbc7Sjsg 
1324c349dbc7Sjsg await_fence:
1325ad8b1aafSjsg 	return i915_sw_fence_await_dma_fence(wait,
1326c349dbc7Sjsg 					     &from->fence, 0,
1327c349dbc7Sjsg 					     I915_FENCE_GFP);
1328c349dbc7Sjsg }
1329c349dbc7Sjsg 
intel_timeline_sync_has_start(struct intel_timeline * tl,struct dma_fence * fence)1330c349dbc7Sjsg static bool intel_timeline_sync_has_start(struct intel_timeline *tl,
1331c349dbc7Sjsg 					  struct dma_fence *fence)
1332c349dbc7Sjsg {
1333c349dbc7Sjsg 	return __intel_timeline_sync_is_later(tl,
1334c349dbc7Sjsg 					      fence->context,
1335c349dbc7Sjsg 					      fence->seqno - 1);
1336c349dbc7Sjsg }
1337c349dbc7Sjsg 
intel_timeline_sync_set_start(struct intel_timeline * tl,const struct dma_fence * fence)1338c349dbc7Sjsg static int intel_timeline_sync_set_start(struct intel_timeline *tl,
1339c349dbc7Sjsg 					 const struct dma_fence *fence)
1340c349dbc7Sjsg {
1341c349dbc7Sjsg 	return __intel_timeline_sync_set(tl, fence->context, fence->seqno - 1);
1342c349dbc7Sjsg }
1343c349dbc7Sjsg 
1344c349dbc7Sjsg static int
__i915_request_await_execution(struct i915_request * to,struct i915_request * from)1345c349dbc7Sjsg __i915_request_await_execution(struct i915_request *to,
13465ca02815Sjsg 			       struct i915_request *from)
1347c349dbc7Sjsg {
1348c349dbc7Sjsg 	int err;
1349c349dbc7Sjsg 
1350c349dbc7Sjsg 	GEM_BUG_ON(intel_context_is_barrier(from->context));
1351c349dbc7Sjsg 
1352c349dbc7Sjsg 	/* Submit both requests at the same time */
13535ca02815Sjsg 	err = __await_execution(to, from, I915_FENCE_GFP);
1354c349dbc7Sjsg 	if (err)
1355c349dbc7Sjsg 		return err;
1356c349dbc7Sjsg 
1357c349dbc7Sjsg 	/* Squash repeated depenendices to the same timelines */
1358c349dbc7Sjsg 	if (intel_timeline_sync_has_start(i915_request_timeline(to),
1359c349dbc7Sjsg 					  &from->fence))
1360c349dbc7Sjsg 		return 0;
1361c349dbc7Sjsg 
1362c349dbc7Sjsg 	/*
1363c349dbc7Sjsg 	 * Wait until the start of this request.
1364c349dbc7Sjsg 	 *
1365c349dbc7Sjsg 	 * The execution cb fires when we submit the request to HW. But in
1366c349dbc7Sjsg 	 * many cases this may be long before the request itself is ready to
1367c349dbc7Sjsg 	 * run (consider that we submit 2 requests for the same context, where
1368c349dbc7Sjsg 	 * the request of interest is behind an indefinite spinner). So we hook
1369c349dbc7Sjsg 	 * up to both to reduce our queues and keep the execution lag minimised
1370c349dbc7Sjsg 	 * in the worst case, though we hope that the await_start is elided.
1371c349dbc7Sjsg 	 */
1372c349dbc7Sjsg 	err = i915_request_await_start(to, from);
1373c349dbc7Sjsg 	if (err < 0)
1374c349dbc7Sjsg 		return err;
1375c349dbc7Sjsg 
1376c349dbc7Sjsg 	/*
1377c349dbc7Sjsg 	 * Ensure both start together [after all semaphores in signal]
1378c349dbc7Sjsg 	 *
1379c349dbc7Sjsg 	 * Now that we are queued to the HW at roughly the same time (thanks
1380c349dbc7Sjsg 	 * to the execute cb) and are ready to run at roughly the same time
1381c349dbc7Sjsg 	 * (thanks to the await start), our signaler may still be indefinitely
1382c349dbc7Sjsg 	 * delayed by waiting on a semaphore from a remote engine. If our
1383c349dbc7Sjsg 	 * signaler depends on a semaphore, so indirectly do we, and we do not
1384c349dbc7Sjsg 	 * want to start our payload until our signaler also starts theirs.
1385c349dbc7Sjsg 	 * So we wait.
1386c349dbc7Sjsg 	 *
1387c349dbc7Sjsg 	 * However, there is also a second condition for which we need to wait
1388c349dbc7Sjsg 	 * for the precise start of the signaler. Consider that the signaler
1389c349dbc7Sjsg 	 * was submitted in a chain of requests following another context
1390c349dbc7Sjsg 	 * (with just an ordinary intra-engine fence dependency between the
1391c349dbc7Sjsg 	 * two). In this case the signaler is queued to HW, but not for
1392c349dbc7Sjsg 	 * immediate execution, and so we must wait until it reaches the
1393c349dbc7Sjsg 	 * active slot.
1394c349dbc7Sjsg 	 */
13951bb76ff1Sjsg 	if (can_use_semaphore_wait(to, from) &&
13961bb76ff1Sjsg 	    intel_engine_has_semaphores(to->engine) &&
1397ad8b1aafSjsg 	    !i915_request_has_initial_breadcrumb(to)) {
1398c349dbc7Sjsg 		err = __emit_semaphore_wait(to, from, from->fence.seqno - 1);
1399c349dbc7Sjsg 		if (err < 0)
1400c349dbc7Sjsg 			return err;
1401c349dbc7Sjsg 	}
1402c349dbc7Sjsg 
1403c349dbc7Sjsg 	/* Couple the dependency tree for PI on this exposed to->fence */
14045ca02815Sjsg 	if (to->engine->sched_engine->schedule) {
1405c349dbc7Sjsg 		err = i915_sched_node_add_dependency(&to->sched,
1406c349dbc7Sjsg 						     &from->sched,
1407c349dbc7Sjsg 						     I915_DEPENDENCY_WEAK);
1408c349dbc7Sjsg 		if (err < 0)
1409c349dbc7Sjsg 			return err;
1410c349dbc7Sjsg 	}
1411c349dbc7Sjsg 
1412c349dbc7Sjsg 	return intel_timeline_sync_set_start(i915_request_timeline(to),
1413c349dbc7Sjsg 					     &from->fence);
1414c349dbc7Sjsg }
1415c349dbc7Sjsg 
mark_external(struct i915_request * rq)1416ad8b1aafSjsg static void mark_external(struct i915_request *rq)
1417ad8b1aafSjsg {
1418ad8b1aafSjsg 	/*
1419ad8b1aafSjsg 	 * The downside of using semaphores is that we lose metadata passing
1420ad8b1aafSjsg 	 * along the signaling chain. This is particularly nasty when we
1421ad8b1aafSjsg 	 * need to pass along a fatal error such as EFAULT or EDEADLK. For
1422ad8b1aafSjsg 	 * fatal errors we want to scrub the request before it is executed,
1423ad8b1aafSjsg 	 * which means that we cannot preload the request onto HW and have
1424ad8b1aafSjsg 	 * it wait upon a semaphore.
1425ad8b1aafSjsg 	 */
1426ad8b1aafSjsg 	rq->sched.flags |= I915_SCHED_HAS_EXTERNAL_CHAIN;
1427ad8b1aafSjsg }
1428ad8b1aafSjsg 
1429ad8b1aafSjsg static int
__i915_request_await_external(struct i915_request * rq,struct dma_fence * fence)1430ad8b1aafSjsg __i915_request_await_external(struct i915_request *rq, struct dma_fence *fence)
1431ad8b1aafSjsg {
1432ad8b1aafSjsg 	mark_external(rq);
1433ad8b1aafSjsg 	return i915_sw_fence_await_dma_fence(&rq->submit, fence,
1434*f005ef32Sjsg 					     i915_fence_context_timeout(rq->i915,
1435ad8b1aafSjsg 									fence->context),
1436ad8b1aafSjsg 					     I915_FENCE_GFP);
1437ad8b1aafSjsg }
1438ad8b1aafSjsg 
1439ad8b1aafSjsg static int
i915_request_await_external(struct i915_request * rq,struct dma_fence * fence)1440ad8b1aafSjsg i915_request_await_external(struct i915_request *rq, struct dma_fence *fence)
1441ad8b1aafSjsg {
1442ad8b1aafSjsg 	struct dma_fence *iter;
1443ad8b1aafSjsg 	int err = 0;
1444ad8b1aafSjsg 
1445ad8b1aafSjsg 	if (!to_dma_fence_chain(fence))
1446ad8b1aafSjsg 		return __i915_request_await_external(rq, fence);
1447ad8b1aafSjsg 
1448ad8b1aafSjsg 	dma_fence_chain_for_each(iter, fence) {
1449ad8b1aafSjsg 		struct dma_fence_chain *chain = to_dma_fence_chain(iter);
1450ad8b1aafSjsg 
1451ad8b1aafSjsg 		if (!dma_fence_is_i915(chain->fence)) {
1452ad8b1aafSjsg 			err = __i915_request_await_external(rq, iter);
1453ad8b1aafSjsg 			break;
1454ad8b1aafSjsg 		}
1455ad8b1aafSjsg 
1456ad8b1aafSjsg 		err = i915_request_await_dma_fence(rq, chain->fence);
1457ad8b1aafSjsg 		if (err < 0)
1458ad8b1aafSjsg 			break;
1459ad8b1aafSjsg 	}
1460ad8b1aafSjsg 
1461ad8b1aafSjsg 	dma_fence_put(iter);
1462ad8b1aafSjsg 	return err;
1463ad8b1aafSjsg }
1464ad8b1aafSjsg 
is_parallel_rq(struct i915_request * rq)14651bb76ff1Sjsg static inline bool is_parallel_rq(struct i915_request *rq)
14661bb76ff1Sjsg {
14671bb76ff1Sjsg 	return intel_context_is_parallel(rq->context);
14681bb76ff1Sjsg }
14691bb76ff1Sjsg 
request_to_parent(struct i915_request * rq)14701bb76ff1Sjsg static inline struct intel_context *request_to_parent(struct i915_request *rq)
14711bb76ff1Sjsg {
14721bb76ff1Sjsg 	return intel_context_to_parent(rq->context);
14731bb76ff1Sjsg }
14741bb76ff1Sjsg 
is_same_parallel_context(struct i915_request * to,struct i915_request * from)14751bb76ff1Sjsg static bool is_same_parallel_context(struct i915_request *to,
14761bb76ff1Sjsg 				     struct i915_request *from)
14771bb76ff1Sjsg {
14781bb76ff1Sjsg 	if (is_parallel_rq(to))
14791bb76ff1Sjsg 		return request_to_parent(to) == request_to_parent(from);
14801bb76ff1Sjsg 
14811bb76ff1Sjsg 	return false;
14821bb76ff1Sjsg }
14831bb76ff1Sjsg 
1484c349dbc7Sjsg int
i915_request_await_execution(struct i915_request * rq,struct dma_fence * fence)1485c349dbc7Sjsg i915_request_await_execution(struct i915_request *rq,
14865ca02815Sjsg 			     struct dma_fence *fence)
1487c349dbc7Sjsg {
1488c349dbc7Sjsg 	struct dma_fence **child = &fence;
1489c349dbc7Sjsg 	unsigned int nchild = 1;
1490c349dbc7Sjsg 	int ret;
1491c349dbc7Sjsg 
1492c349dbc7Sjsg 	if (dma_fence_is_array(fence)) {
1493c349dbc7Sjsg 		struct dma_fence_array *array = to_dma_fence_array(fence);
1494c349dbc7Sjsg 
1495c349dbc7Sjsg 		/* XXX Error for signal-on-any fence arrays */
1496c349dbc7Sjsg 
1497c349dbc7Sjsg 		child = array->fences;
1498c349dbc7Sjsg 		nchild = array->num_fences;
1499c349dbc7Sjsg 		GEM_BUG_ON(!nchild);
1500c349dbc7Sjsg 	}
1501c349dbc7Sjsg 
1502c349dbc7Sjsg 	do {
1503c349dbc7Sjsg 		fence = *child++;
1504bac21eb5Sjsg 		if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
1505c349dbc7Sjsg 			continue;
1506c349dbc7Sjsg 
1507ad8b1aafSjsg 		if (fence->context == rq->fence.context)
1508ad8b1aafSjsg 			continue;
1509ad8b1aafSjsg 
1510c349dbc7Sjsg 		/*
1511c349dbc7Sjsg 		 * We don't squash repeated fence dependencies here as we
1512c349dbc7Sjsg 		 * want to run our callback in all cases.
1513c349dbc7Sjsg 		 */
1514c349dbc7Sjsg 
15151bb76ff1Sjsg 		if (dma_fence_is_i915(fence)) {
15161bb76ff1Sjsg 			if (is_same_parallel_context(rq, to_request(fence)))
15171bb76ff1Sjsg 				continue;
1518c349dbc7Sjsg 			ret = __i915_request_await_execution(rq,
15195ca02815Sjsg 							     to_request(fence));
15201bb76ff1Sjsg 		} else {
1521ad8b1aafSjsg 			ret = i915_request_await_external(rq, fence);
15221bb76ff1Sjsg 		}
1523c349dbc7Sjsg 		if (ret < 0)
1524c349dbc7Sjsg 			return ret;
15257f4dd379Sjsg 	} while (--nchild);
15267f4dd379Sjsg 
15277f4dd379Sjsg 	return 0;
15287f4dd379Sjsg }
15297f4dd379Sjsg 
1530ad8b1aafSjsg static int
await_request_submit(struct i915_request * to,struct i915_request * from)1531ad8b1aafSjsg await_request_submit(struct i915_request *to, struct i915_request *from)
1532ad8b1aafSjsg {
1533ad8b1aafSjsg 	/*
1534ad8b1aafSjsg 	 * If we are waiting on a virtual engine, then it may be
1535ad8b1aafSjsg 	 * constrained to execute on a single engine *prior* to submission.
1536ad8b1aafSjsg 	 * When it is submitted, it will be first submitted to the virtual
1537ad8b1aafSjsg 	 * engine and then passed to the physical engine. We cannot allow
1538ad8b1aafSjsg 	 * the waiter to be submitted immediately to the physical engine
1539ad8b1aafSjsg 	 * as it may then bypass the virtual request.
1540ad8b1aafSjsg 	 */
1541ad8b1aafSjsg 	if (to->engine == READ_ONCE(from->engine))
1542ad8b1aafSjsg 		return i915_sw_fence_await_sw_fence_gfp(&to->submit,
1543ad8b1aafSjsg 							&from->submit,
1544ad8b1aafSjsg 							I915_FENCE_GFP);
1545ad8b1aafSjsg 	else
15465ca02815Sjsg 		return __i915_request_await_execution(to, from);
1547ad8b1aafSjsg }
1548ad8b1aafSjsg 
1549ad8b1aafSjsg static int
i915_request_await_request(struct i915_request * to,struct i915_request * from)1550ad8b1aafSjsg i915_request_await_request(struct i915_request *to, struct i915_request *from)
1551ad8b1aafSjsg {
1552ad8b1aafSjsg 	int ret;
1553ad8b1aafSjsg 
1554ad8b1aafSjsg 	GEM_BUG_ON(to == from);
1555ad8b1aafSjsg 	GEM_BUG_ON(to->timeline == from->timeline);
1556ad8b1aafSjsg 
1557ad8b1aafSjsg 	if (i915_request_completed(from)) {
1558ad8b1aafSjsg 		i915_sw_fence_set_error_once(&to->submit, from->fence.error);
1559ad8b1aafSjsg 		return 0;
1560ad8b1aafSjsg 	}
1561ad8b1aafSjsg 
15625ca02815Sjsg 	if (to->engine->sched_engine->schedule) {
1563ad8b1aafSjsg 		ret = i915_sched_node_add_dependency(&to->sched,
1564ad8b1aafSjsg 						     &from->sched,
1565ad8b1aafSjsg 						     I915_DEPENDENCY_EXTERNAL);
1566ad8b1aafSjsg 		if (ret < 0)
1567ad8b1aafSjsg 			return ret;
1568ad8b1aafSjsg 	}
1569ad8b1aafSjsg 
15705ca02815Sjsg 	if (!intel_engine_uses_guc(to->engine) &&
15715ca02815Sjsg 	    is_power_of_2(to->execution_mask | READ_ONCE(from->execution_mask)))
1572ad8b1aafSjsg 		ret = await_request_submit(to, from);
1573ad8b1aafSjsg 	else
1574ad8b1aafSjsg 		ret = emit_semaphore_wait(to, from, I915_FENCE_GFP);
1575ad8b1aafSjsg 	if (ret < 0)
1576ad8b1aafSjsg 		return ret;
1577ad8b1aafSjsg 
1578ad8b1aafSjsg 	return 0;
1579ad8b1aafSjsg }
1580ad8b1aafSjsg 
1581ad8b1aafSjsg int
i915_request_await_dma_fence(struct i915_request * rq,struct dma_fence * fence)1582ad8b1aafSjsg i915_request_await_dma_fence(struct i915_request *rq, struct dma_fence *fence)
1583ad8b1aafSjsg {
1584ad8b1aafSjsg 	struct dma_fence **child = &fence;
1585ad8b1aafSjsg 	unsigned int nchild = 1;
1586ad8b1aafSjsg 	int ret;
1587ad8b1aafSjsg 
1588ad8b1aafSjsg 	/*
1589ad8b1aafSjsg 	 * Note that if the fence-array was created in signal-on-any mode,
1590ad8b1aafSjsg 	 * we should *not* decompose it into its individual fences. However,
1591ad8b1aafSjsg 	 * we don't currently store which mode the fence-array is operating
1592ad8b1aafSjsg 	 * in. Fortunately, the only user of signal-on-any is private to
1593ad8b1aafSjsg 	 * amdgpu and we should not see any incoming fence-array from
1594ad8b1aafSjsg 	 * sync-file being in signal-on-any mode.
1595ad8b1aafSjsg 	 */
1596ad8b1aafSjsg 	if (dma_fence_is_array(fence)) {
1597ad8b1aafSjsg 		struct dma_fence_array *array = to_dma_fence_array(fence);
1598ad8b1aafSjsg 
1599ad8b1aafSjsg 		child = array->fences;
1600ad8b1aafSjsg 		nchild = array->num_fences;
1601ad8b1aafSjsg 		GEM_BUG_ON(!nchild);
1602ad8b1aafSjsg 	}
1603ad8b1aafSjsg 
1604ad8b1aafSjsg 	do {
1605ad8b1aafSjsg 		fence = *child++;
1606bac21eb5Sjsg 		if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
1607ad8b1aafSjsg 			continue;
1608ad8b1aafSjsg 
1609ad8b1aafSjsg 		/*
1610ad8b1aafSjsg 		 * Requests on the same timeline are explicitly ordered, along
1611ad8b1aafSjsg 		 * with their dependencies, by i915_request_add() which ensures
1612ad8b1aafSjsg 		 * that requests are submitted in-order through each ring.
1613ad8b1aafSjsg 		 */
1614ad8b1aafSjsg 		if (fence->context == rq->fence.context)
1615ad8b1aafSjsg 			continue;
1616ad8b1aafSjsg 
1617ad8b1aafSjsg 		/* Squash repeated waits to the same timelines */
1618ad8b1aafSjsg 		if (fence->context &&
1619ad8b1aafSjsg 		    intel_timeline_sync_is_later(i915_request_timeline(rq),
1620ad8b1aafSjsg 						 fence))
1621ad8b1aafSjsg 			continue;
1622ad8b1aafSjsg 
16231bb76ff1Sjsg 		if (dma_fence_is_i915(fence)) {
16241bb76ff1Sjsg 			if (is_same_parallel_context(rq, to_request(fence)))
16251bb76ff1Sjsg 				continue;
1626ad8b1aafSjsg 			ret = i915_request_await_request(rq, to_request(fence));
16271bb76ff1Sjsg 		} else {
1628ad8b1aafSjsg 			ret = i915_request_await_external(rq, fence);
16291bb76ff1Sjsg 		}
1630ad8b1aafSjsg 		if (ret < 0)
1631ad8b1aafSjsg 			return ret;
1632ad8b1aafSjsg 
1633ad8b1aafSjsg 		/* Record the latest fence used against each timeline */
1634ad8b1aafSjsg 		if (fence->context)
1635ad8b1aafSjsg 			intel_timeline_sync_set(i915_request_timeline(rq),
1636ad8b1aafSjsg 						fence);
1637ad8b1aafSjsg 	} while (--nchild);
1638ad8b1aafSjsg 
1639ad8b1aafSjsg 	return 0;
1640ad8b1aafSjsg }
1641ad8b1aafSjsg 
16427f4dd379Sjsg /**
16431bb76ff1Sjsg  * i915_request_await_deps - set this request to (async) wait upon a struct
16441bb76ff1Sjsg  * i915_deps dma_fence collection
16451bb76ff1Sjsg  * @rq: request we are wishing to use
16461bb76ff1Sjsg  * @deps: The struct i915_deps containing the dependencies.
16471bb76ff1Sjsg  *
16481bb76ff1Sjsg  * Returns 0 if successful, negative error code on error.
16491bb76ff1Sjsg  */
i915_request_await_deps(struct i915_request * rq,const struct i915_deps * deps)16501bb76ff1Sjsg int i915_request_await_deps(struct i915_request *rq, const struct i915_deps *deps)
16511bb76ff1Sjsg {
16521bb76ff1Sjsg 	int i, err;
16531bb76ff1Sjsg 
16541bb76ff1Sjsg 	for (i = 0; i < deps->num_deps; ++i) {
16551bb76ff1Sjsg 		err = i915_request_await_dma_fence(rq, deps->fences[i]);
16561bb76ff1Sjsg 		if (err)
16571bb76ff1Sjsg 			return err;
16581bb76ff1Sjsg 	}
16591bb76ff1Sjsg 
16601bb76ff1Sjsg 	return 0;
16611bb76ff1Sjsg }
16621bb76ff1Sjsg 
16631bb76ff1Sjsg /**
16647f4dd379Sjsg  * i915_request_await_object - set this request to (async) wait upon a bo
16657f4dd379Sjsg  * @to: request we are wishing to use
16667f4dd379Sjsg  * @obj: object which may be in use on another ring.
16677f4dd379Sjsg  * @write: whether the wait is on behalf of a writer
16687f4dd379Sjsg  *
16697f4dd379Sjsg  * This code is meant to abstract object synchronization with the GPU.
16707f4dd379Sjsg  * Conceptually we serialise writes between engines inside the GPU.
16717f4dd379Sjsg  * We only allow one engine to write into a buffer at any time, but
16727f4dd379Sjsg  * multiple readers. To ensure each has a coherent view of memory, we must:
16737f4dd379Sjsg  *
16747f4dd379Sjsg  * - If there is an outstanding write request to the object, the new
16757f4dd379Sjsg  *   request must wait for it to complete (either CPU or in hw, requests
16767f4dd379Sjsg  *   on the same ring will be naturally ordered).
16777f4dd379Sjsg  *
16787f4dd379Sjsg  * - If we are a write request (pending_write_domain is set), the new
16797f4dd379Sjsg  *   request must wait for outstanding read requests to complete.
16807f4dd379Sjsg  *
16817f4dd379Sjsg  * Returns 0 if successful, else propagates up the lower layer error.
16827f4dd379Sjsg  */
16837f4dd379Sjsg int
i915_request_await_object(struct i915_request * to,struct drm_i915_gem_object * obj,bool write)16847f4dd379Sjsg i915_request_await_object(struct i915_request *to,
16857f4dd379Sjsg 			  struct drm_i915_gem_object *obj,
16867f4dd379Sjsg 			  bool write)
16877f4dd379Sjsg {
16881bb76ff1Sjsg 	struct dma_resv_iter cursor;
16891bb76ff1Sjsg 	struct dma_fence *fence;
16907f4dd379Sjsg 	int ret = 0;
16917f4dd379Sjsg 
16921bb76ff1Sjsg 	dma_resv_for_each_fence(&cursor, obj->base.resv,
16931bb76ff1Sjsg 				dma_resv_usage_rw(write), fence) {
16941bb76ff1Sjsg 		ret = i915_request_await_dma_fence(to, fence);
16957f4dd379Sjsg 		if (ret)
16967f4dd379Sjsg 			break;
16977f4dd379Sjsg 	}
16987f4dd379Sjsg 
16997f4dd379Sjsg 	return ret;
17007f4dd379Sjsg }
17017f4dd379Sjsg 
i915_request_await_huc(struct i915_request * rq)1702*f005ef32Sjsg static void i915_request_await_huc(struct i915_request *rq)
1703*f005ef32Sjsg {
1704*f005ef32Sjsg 	struct intel_huc *huc = &rq->context->engine->gt->uc.huc;
1705*f005ef32Sjsg 
1706*f005ef32Sjsg 	/* don't stall kernel submissions! */
1707*f005ef32Sjsg 	if (!rcu_access_pointer(rq->context->gem_context))
1708*f005ef32Sjsg 		return;
1709*f005ef32Sjsg 
1710*f005ef32Sjsg 	if (intel_huc_wait_required(huc))
1711*f005ef32Sjsg 		i915_sw_fence_await_sw_fence(&rq->submit,
1712*f005ef32Sjsg 					     &huc->delayed_load.fence,
1713*f005ef32Sjsg 					     &rq->hucq);
1714*f005ef32Sjsg }
1715*f005ef32Sjsg 
1716c349dbc7Sjsg static struct i915_request *
__i915_request_ensure_parallel_ordering(struct i915_request * rq,struct intel_timeline * timeline)17171bb76ff1Sjsg __i915_request_ensure_parallel_ordering(struct i915_request *rq,
17181bb76ff1Sjsg 					struct intel_timeline *timeline)
17191bb76ff1Sjsg {
17201bb76ff1Sjsg 	struct i915_request *prev;
17211bb76ff1Sjsg 
17221bb76ff1Sjsg 	GEM_BUG_ON(!is_parallel_rq(rq));
17231bb76ff1Sjsg 
17241bb76ff1Sjsg 	prev = request_to_parent(rq)->parallel.last_rq;
17251bb76ff1Sjsg 	if (prev) {
17261bb76ff1Sjsg 		if (!__i915_request_is_complete(prev)) {
17271bb76ff1Sjsg 			i915_sw_fence_await_sw_fence(&rq->submit,
17281bb76ff1Sjsg 						     &prev->submit,
17291bb76ff1Sjsg 						     &rq->submitq);
17301bb76ff1Sjsg 
17311bb76ff1Sjsg 			if (rq->engine->sched_engine->schedule)
17321bb76ff1Sjsg 				__i915_sched_node_add_dependency(&rq->sched,
17331bb76ff1Sjsg 								 &prev->sched,
17341bb76ff1Sjsg 								 &rq->dep,
17351bb76ff1Sjsg 								 0);
17361bb76ff1Sjsg 		}
17371bb76ff1Sjsg 		i915_request_put(prev);
17381bb76ff1Sjsg 	}
17391bb76ff1Sjsg 
17401bb76ff1Sjsg 	request_to_parent(rq)->parallel.last_rq = i915_request_get(rq);
17411bb76ff1Sjsg 
1742fa60bc73Sjsg 	/*
1743fa60bc73Sjsg 	 * Users have to put a reference potentially got by
1744fa60bc73Sjsg 	 * __i915_active_fence_set() to the returned request
1745fa60bc73Sjsg 	 * when no longer needed
1746fa60bc73Sjsg 	 */
17471bb76ff1Sjsg 	return to_request(__i915_active_fence_set(&timeline->last_request,
17481bb76ff1Sjsg 						  &rq->fence));
17491bb76ff1Sjsg }
17501bb76ff1Sjsg 
17511bb76ff1Sjsg static struct i915_request *
__i915_request_ensure_ordering(struct i915_request * rq,struct intel_timeline * timeline)17521bb76ff1Sjsg __i915_request_ensure_ordering(struct i915_request *rq,
17531bb76ff1Sjsg 			       struct intel_timeline *timeline)
17541bb76ff1Sjsg {
17551bb76ff1Sjsg 	struct i915_request *prev;
17561bb76ff1Sjsg 
17571bb76ff1Sjsg 	GEM_BUG_ON(is_parallel_rq(rq));
17581bb76ff1Sjsg 
17591bb76ff1Sjsg 	prev = to_request(__i915_active_fence_set(&timeline->last_request,
17601bb76ff1Sjsg 						  &rq->fence));
17611bb76ff1Sjsg 
17621bb76ff1Sjsg 	if (prev && !__i915_request_is_complete(prev)) {
17631bb76ff1Sjsg 		bool uses_guc = intel_engine_uses_guc(rq->engine);
17641bb76ff1Sjsg 		bool pow2 = is_power_of_2(READ_ONCE(prev->engine)->mask |
17651bb76ff1Sjsg 					  rq->engine->mask);
17661bb76ff1Sjsg 		bool same_context = prev->context == rq->context;
17671bb76ff1Sjsg 
17681bb76ff1Sjsg 		/*
17691bb76ff1Sjsg 		 * The requests are supposed to be kept in order. However,
17701bb76ff1Sjsg 		 * we need to be wary in case the timeline->last_request
17711bb76ff1Sjsg 		 * is used as a barrier for external modification to this
17721bb76ff1Sjsg 		 * context.
17731bb76ff1Sjsg 		 */
17741bb76ff1Sjsg 		GEM_BUG_ON(same_context &&
17751bb76ff1Sjsg 			   i915_seqno_passed(prev->fence.seqno,
17761bb76ff1Sjsg 					     rq->fence.seqno));
17771bb76ff1Sjsg 
17781bb76ff1Sjsg 		if ((same_context && uses_guc) || (!uses_guc && pow2))
17791bb76ff1Sjsg 			i915_sw_fence_await_sw_fence(&rq->submit,
17801bb76ff1Sjsg 						     &prev->submit,
17811bb76ff1Sjsg 						     &rq->submitq);
17821bb76ff1Sjsg 		else
17831bb76ff1Sjsg 			__i915_sw_fence_await_dma_fence(&rq->submit,
17841bb76ff1Sjsg 							&prev->fence,
17851bb76ff1Sjsg 							&rq->dmaq);
17861bb76ff1Sjsg 		if (rq->engine->sched_engine->schedule)
17871bb76ff1Sjsg 			__i915_sched_node_add_dependency(&rq->sched,
17881bb76ff1Sjsg 							 &prev->sched,
17891bb76ff1Sjsg 							 &rq->dep,
17901bb76ff1Sjsg 							 0);
17911bb76ff1Sjsg 	}
17921bb76ff1Sjsg 
1793fa60bc73Sjsg 	/*
1794fa60bc73Sjsg 	 * Users have to put the reference to prev potentially got
1795fa60bc73Sjsg 	 * by __i915_active_fence_set() when no longer needed
1796fa60bc73Sjsg 	 */
17971bb76ff1Sjsg 	return prev;
17981bb76ff1Sjsg }
17991bb76ff1Sjsg 
18001bb76ff1Sjsg static struct i915_request *
__i915_request_add_to_timeline(struct i915_request * rq)1801c349dbc7Sjsg __i915_request_add_to_timeline(struct i915_request *rq)
18027f4dd379Sjsg {
1803c349dbc7Sjsg 	struct intel_timeline *timeline = i915_request_timeline(rq);
1804c349dbc7Sjsg 	struct i915_request *prev;
18057f4dd379Sjsg 
18067f4dd379Sjsg 	/*
1807*f005ef32Sjsg 	 * Media workloads may require HuC, so stall them until HuC loading is
1808*f005ef32Sjsg 	 * complete. Note that HuC not being loaded when a user submission
1809*f005ef32Sjsg 	 * arrives can only happen when HuC is loaded via GSC and in that case
1810*f005ef32Sjsg 	 * we still expect the window between us starting to accept submissions
1811*f005ef32Sjsg 	 * and HuC loading completion to be small (a few hundred ms).
1812*f005ef32Sjsg 	 */
1813*f005ef32Sjsg 	if (rq->engine->class == VIDEO_DECODE_CLASS)
1814*f005ef32Sjsg 		i915_request_await_huc(rq);
1815*f005ef32Sjsg 
1816*f005ef32Sjsg 	/*
1817c349dbc7Sjsg 	 * Dependency tracking and request ordering along the timeline
1818c349dbc7Sjsg 	 * is special cased so that we can eliminate redundant ordering
1819c349dbc7Sjsg 	 * operations while building the request (we know that the timeline
1820c349dbc7Sjsg 	 * itself is ordered, and here we guarantee it).
1821c349dbc7Sjsg 	 *
1822c349dbc7Sjsg 	 * As we know we will need to emit tracking along the timeline,
1823c349dbc7Sjsg 	 * we embed the hooks into our request struct -- at the cost of
1824c349dbc7Sjsg 	 * having to have specialised no-allocation interfaces (which will
1825c349dbc7Sjsg 	 * be beneficial elsewhere).
1826c349dbc7Sjsg 	 *
1827c349dbc7Sjsg 	 * A second benefit to open-coding i915_request_await_request is
1828c349dbc7Sjsg 	 * that we can apply a slight variant of the rules specialised
1829c349dbc7Sjsg 	 * for timelines that jump between engines (such as virtual engines).
1830c349dbc7Sjsg 	 * If we consider the case of virtual engine, we must emit a dma-fence
1831c349dbc7Sjsg 	 * to prevent scheduling of the second request until the first is
1832c349dbc7Sjsg 	 * complete (to maximise our greedy late load balancing) and this
1833c349dbc7Sjsg 	 * precludes optimising to use semaphores serialisation of a single
1834c349dbc7Sjsg 	 * timeline across engines.
18351bb76ff1Sjsg 	 *
18361bb76ff1Sjsg 	 * We do not order parallel submission requests on the timeline as each
18371bb76ff1Sjsg 	 * parallel submission context has its own timeline and the ordering
18381bb76ff1Sjsg 	 * rules for parallel requests are that they must be submitted in the
18391bb76ff1Sjsg 	 * order received from the execbuf IOCTL. So rather than using the
18401bb76ff1Sjsg 	 * timeline we store a pointer to last request submitted in the
18411bb76ff1Sjsg 	 * relationship in the gem context and insert a submission fence
18421bb76ff1Sjsg 	 * between that request and request passed into this function or
18431bb76ff1Sjsg 	 * alternatively we use completion fence if gem context has a single
18441bb76ff1Sjsg 	 * timeline and this is the first submission of an execbuf IOCTL.
18457f4dd379Sjsg 	 */
18461bb76ff1Sjsg 	if (likely(!is_parallel_rq(rq)))
18471bb76ff1Sjsg 		prev = __i915_request_ensure_ordering(rq, timeline);
1848c349dbc7Sjsg 	else
18491bb76ff1Sjsg 		prev = __i915_request_ensure_parallel_ordering(rq, timeline);
1850fa60bc73Sjsg 	if (prev)
1851fa60bc73Sjsg 		i915_request_put(prev);
1852c349dbc7Sjsg 
1853c349dbc7Sjsg 	/*
1854c349dbc7Sjsg 	 * Make sure that no request gazumped us - if it was allocated after
1855c349dbc7Sjsg 	 * our i915_request_alloc() and called __i915_request_add() before
1856c349dbc7Sjsg 	 * us, the timeline will hold its seqno which is later than ours.
1857c349dbc7Sjsg 	 */
1858c349dbc7Sjsg 	GEM_BUG_ON(timeline->seqno != rq->fence.seqno);
1859c349dbc7Sjsg 
1860c349dbc7Sjsg 	return prev;
18617f4dd379Sjsg }
18627f4dd379Sjsg 
18637f4dd379Sjsg /*
18647f4dd379Sjsg  * NB: This function is not allowed to fail. Doing so would mean the the
18657f4dd379Sjsg  * request is not being tracked for completion but the work itself is
18667f4dd379Sjsg  * going to happen on the hardware. This would be a Bad Thing(tm).
18677f4dd379Sjsg  */
__i915_request_commit(struct i915_request * rq)1868c349dbc7Sjsg struct i915_request *__i915_request_commit(struct i915_request *rq)
18697f4dd379Sjsg {
1870c349dbc7Sjsg 	struct intel_engine_cs *engine = rq->engine;
1871c349dbc7Sjsg 	struct intel_ring *ring = rq->ring;
18727f4dd379Sjsg 	u32 *cs;
18737f4dd379Sjsg 
1874c349dbc7Sjsg 	RQ_TRACE(rq, "\n");
18757f4dd379Sjsg 
18767f4dd379Sjsg 	/*
18777f4dd379Sjsg 	 * To ensure that this call will not fail, space for its emissions
18787f4dd379Sjsg 	 * should already have been reserved in the ring buffer. Let the ring
18797f4dd379Sjsg 	 * know that it is time to use that space up.
18807f4dd379Sjsg 	 */
1881c349dbc7Sjsg 	GEM_BUG_ON(rq->reserved_space > ring->space);
1882c349dbc7Sjsg 	rq->reserved_space = 0;
1883c349dbc7Sjsg 	rq->emitted_jiffies = jiffies;
18847f4dd379Sjsg 
18857f4dd379Sjsg 	/*
18867f4dd379Sjsg 	 * Record the position of the start of the breadcrumb so that
18877f4dd379Sjsg 	 * should we detect the updated seqno part-way through the
18887f4dd379Sjsg 	 * GPU processing the request, we never over-estimate the
18897f4dd379Sjsg 	 * position of the ring's HEAD.
18907f4dd379Sjsg 	 */
1891c349dbc7Sjsg 	cs = intel_ring_begin(rq, engine->emit_fini_breadcrumb_dw);
18927f4dd379Sjsg 	GEM_BUG_ON(IS_ERR(cs));
1893c349dbc7Sjsg 	rq->postfix = intel_ring_offset(rq, cs);
18947f4dd379Sjsg 
1895c349dbc7Sjsg 	return __i915_request_add_to_timeline(rq);
18967f4dd379Sjsg }
18977f4dd379Sjsg 
__i915_request_queue_bh(struct i915_request * rq)18985ca02815Sjsg void __i915_request_queue_bh(struct i915_request *rq)
18995ca02815Sjsg {
19005ca02815Sjsg 	i915_sw_fence_commit(&rq->semaphore);
19015ca02815Sjsg 	i915_sw_fence_commit(&rq->submit);
19025ca02815Sjsg }
19035ca02815Sjsg 
__i915_request_queue(struct i915_request * rq,const struct i915_sched_attr * attr)1904c349dbc7Sjsg void __i915_request_queue(struct i915_request *rq,
1905c349dbc7Sjsg 			  const struct i915_sched_attr *attr)
1906c349dbc7Sjsg {
19077f4dd379Sjsg 	/*
19087f4dd379Sjsg 	 * Let the backend know a new request has arrived that may need
19097f4dd379Sjsg 	 * to adjust the existing execution schedule due to a high priority
19107f4dd379Sjsg 	 * request - i.e. we may want to preempt the current request in order
19117f4dd379Sjsg 	 * to run a high priority dependency chain *before* we can execute this
19127f4dd379Sjsg 	 * request.
19137f4dd379Sjsg 	 *
19147f4dd379Sjsg 	 * This is called before the request is ready to run so that we can
19157f4dd379Sjsg 	 * decide whether to preempt the entire chain so that it is ready to
19167f4dd379Sjsg 	 * run at the earliest possible convenience.
19177f4dd379Sjsg 	 */
19185ca02815Sjsg 	if (attr && rq->engine->sched_engine->schedule)
19195ca02815Sjsg 		rq->engine->sched_engine->schedule(rq, attr);
19205ca02815Sjsg 
19215ca02815Sjsg 	local_bh_disable();
19225ca02815Sjsg 	__i915_request_queue_bh(rq);
19235ca02815Sjsg 	local_bh_enable(); /* kick tasklets */
19247f4dd379Sjsg }
19257f4dd379Sjsg 
i915_request_add(struct i915_request * rq)1926c349dbc7Sjsg void i915_request_add(struct i915_request *rq)
1927c349dbc7Sjsg {
1928c349dbc7Sjsg 	struct intel_timeline * const tl = i915_request_timeline(rq);
1929c349dbc7Sjsg 	struct i915_sched_attr attr = {};
1930c349dbc7Sjsg 	struct i915_gem_context *ctx;
1931c349dbc7Sjsg 
1932c349dbc7Sjsg 	lockdep_assert_held(&tl->mutex);
1933c349dbc7Sjsg 	lockdep_unpin_lock(&tl->mutex, rq->cookie);
1934c349dbc7Sjsg 
1935c349dbc7Sjsg 	trace_i915_request_add(rq);
1936c349dbc7Sjsg 	__i915_request_commit(rq);
1937c349dbc7Sjsg 
1938c349dbc7Sjsg 	/* XXX placeholder for selftests */
1939c349dbc7Sjsg 	rcu_read_lock();
1940c349dbc7Sjsg 	ctx = rcu_dereference(rq->context->gem_context);
1941c349dbc7Sjsg 	if (ctx)
1942c349dbc7Sjsg 		attr = ctx->sched;
1943c349dbc7Sjsg 	rcu_read_unlock();
1944c349dbc7Sjsg 
1945c349dbc7Sjsg 	__i915_request_queue(rq, &attr);
1946c349dbc7Sjsg 
1947c349dbc7Sjsg 	mutex_unlock(&tl->mutex);
1948c349dbc7Sjsg }
1949c349dbc7Sjsg 
local_clock_ns(unsigned int * cpu)1950c349dbc7Sjsg static unsigned long local_clock_ns(unsigned int *cpu)
19517f4dd379Sjsg {
19527f4dd379Sjsg 	unsigned long t;
19537f4dd379Sjsg 
19547f4dd379Sjsg 	/*
19557f4dd379Sjsg 	 * Cheaply and approximately convert from nanoseconds to microseconds.
19567f4dd379Sjsg 	 * The result and subsequent calculations are also defined in the same
19577f4dd379Sjsg 	 * approximate microseconds units. The principal source of timing
19587f4dd379Sjsg 	 * error here is from the simple truncation.
19597f4dd379Sjsg 	 *
19607f4dd379Sjsg 	 * Note that local_clock() is only defined wrt to the current CPU;
19617f4dd379Sjsg 	 * the comparisons are no longer valid if we switch CPUs. Instead of
19627f4dd379Sjsg 	 * blocking preemption for the entire busywait, we can detect the CPU
19637f4dd379Sjsg 	 * switch and use that as indicator of system load and a reason to
19647f4dd379Sjsg 	 * stop busywaiting, see busywait_stop().
19657f4dd379Sjsg 	 */
19667f4dd379Sjsg 	*cpu = get_cpu();
1967c349dbc7Sjsg 	t = local_clock();
19687f4dd379Sjsg 	put_cpu();
19697f4dd379Sjsg 
19707f4dd379Sjsg 	return t;
19717f4dd379Sjsg }
19727f4dd379Sjsg 
busywait_stop(unsigned long timeout,unsigned int cpu)19737f4dd379Sjsg static bool busywait_stop(unsigned long timeout, unsigned int cpu)
19747f4dd379Sjsg {
19757f4dd379Sjsg 	unsigned int this_cpu;
19767f4dd379Sjsg 
1977c349dbc7Sjsg 	if (time_after(local_clock_ns(&this_cpu), timeout))
19787f4dd379Sjsg 		return true;
19797f4dd379Sjsg 
19807f4dd379Sjsg 	return this_cpu != cpu;
19817f4dd379Sjsg }
19827f4dd379Sjsg 
__i915_spin_request(struct i915_request * const rq,int state)1983ad8b1aafSjsg static bool __i915_spin_request(struct i915_request * const rq, int state)
19847f4dd379Sjsg {
1985c349dbc7Sjsg 	unsigned long timeout_ns;
1986c349dbc7Sjsg 	unsigned int cpu;
19877f4dd379Sjsg 
19887f4dd379Sjsg 	/*
19897f4dd379Sjsg 	 * Only wait for the request if we know it is likely to complete.
19907f4dd379Sjsg 	 *
19917f4dd379Sjsg 	 * We don't track the timestamps around requests, nor the average
19927f4dd379Sjsg 	 * request length, so we do not have a good indicator that this
19937f4dd379Sjsg 	 * request will complete within the timeout. What we do know is the
1994c349dbc7Sjsg 	 * order in which requests are executed by the context and so we can
1995c349dbc7Sjsg 	 * tell if the request has been started. If the request is not even
1996c349dbc7Sjsg 	 * running yet, it is a fair assumption that it will not complete
1997c349dbc7Sjsg 	 * within our relatively short timeout.
19987f4dd379Sjsg 	 */
1999c349dbc7Sjsg 	if (!i915_request_is_running(rq))
20007f4dd379Sjsg 		return false;
20017f4dd379Sjsg 
20027f4dd379Sjsg 	/*
20037f4dd379Sjsg 	 * When waiting for high frequency requests, e.g. during synchronous
20047f4dd379Sjsg 	 * rendering split between the CPU and GPU, the finite amount of time
20057f4dd379Sjsg 	 * required to set up the irq and wait upon it limits the response
20067f4dd379Sjsg 	 * rate. By busywaiting on the request completion for a short while we
20077f4dd379Sjsg 	 * can service the high frequency waits as quick as possible. However,
20087f4dd379Sjsg 	 * if it is a slow request, we want to sleep as quickly as possible.
20097f4dd379Sjsg 	 * The tradeoff between waiting and sleeping is roughly the time it
20107f4dd379Sjsg 	 * takes to sleep on a request, on the order of a microsecond.
20117f4dd379Sjsg 	 */
20127f4dd379Sjsg 
2013c349dbc7Sjsg 	timeout_ns = READ_ONCE(rq->engine->props.max_busywait_duration_ns);
2014c349dbc7Sjsg 	timeout_ns += local_clock_ns(&cpu);
20157f4dd379Sjsg 	do {
2016ad8b1aafSjsg 		if (dma_fence_is_signaled(&rq->fence))
2017c349dbc7Sjsg 			return true;
20187f4dd379Sjsg 
20197f4dd379Sjsg 		if (signal_pending_state(state, current))
20207f4dd379Sjsg 			break;
20217f4dd379Sjsg 
2022c349dbc7Sjsg 		if (busywait_stop(timeout_ns, cpu))
20237f4dd379Sjsg 			break;
20247f4dd379Sjsg 
20257f4dd379Sjsg 		cpu_relax();
20267f4dd379Sjsg 	} while (!drm_need_resched());
20277f4dd379Sjsg 
20287f4dd379Sjsg 	return false;
20297f4dd379Sjsg }
20307f4dd379Sjsg 
2031c349dbc7Sjsg struct request_wait {
2032c349dbc7Sjsg 	struct dma_fence_cb cb;
2033c349dbc7Sjsg #ifdef __linux__
2034c349dbc7Sjsg 	struct task_struct *tsk;
2035c349dbc7Sjsg #else
2036c349dbc7Sjsg 	struct proc *tsk;
2037c349dbc7Sjsg #endif
2038c349dbc7Sjsg };
2039c349dbc7Sjsg 
request_wait_wake(struct dma_fence * fence,struct dma_fence_cb * cb)2040c349dbc7Sjsg static void request_wait_wake(struct dma_fence *fence, struct dma_fence_cb *cb)
20417f4dd379Sjsg {
2042c349dbc7Sjsg 	struct request_wait *wait = container_of(cb, typeof(*wait), cb);
20437f4dd379Sjsg 
2044ad8b1aafSjsg 	wake_up_process(fetch_and_zero(&wait->tsk));
20457f4dd379Sjsg }
20467f4dd379Sjsg 
20477f4dd379Sjsg /**
20481bb76ff1Sjsg  * i915_request_wait_timeout - wait until execution of request has finished
20497f4dd379Sjsg  * @rq: the request to wait upon
20507f4dd379Sjsg  * @flags: how to wait
20517f4dd379Sjsg  * @timeout: how long to wait in jiffies
20527f4dd379Sjsg  *
20531bb76ff1Sjsg  * i915_request_wait_timeout() waits for the request to be completed, for a
20547f4dd379Sjsg  * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
20557f4dd379Sjsg  * unbounded wait).
20567f4dd379Sjsg  *
20577f4dd379Sjsg  * Returns the remaining time (in jiffies) if the request completed, which may
20581bb76ff1Sjsg  * be zero if the request is unfinished after the timeout expires.
20591bb76ff1Sjsg  * If the timeout is 0, it will return 1 if the fence is signaled.
20601bb76ff1Sjsg  *
20617f4dd379Sjsg  * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
20627f4dd379Sjsg  * pending before the request completes.
20631bb76ff1Sjsg  *
20641bb76ff1Sjsg  * NOTE: This function has the same wait semantics as dma-fence.
20657f4dd379Sjsg  */
i915_request_wait_timeout(struct i915_request * rq,unsigned int flags,long timeout)20661bb76ff1Sjsg long i915_request_wait_timeout(struct i915_request *rq,
20677f4dd379Sjsg 			       unsigned int flags,
20687f4dd379Sjsg 			       long timeout)
20697f4dd379Sjsg {
20707f4dd379Sjsg 	const int state = flags & I915_WAIT_INTERRUPTIBLE ?
20717f4dd379Sjsg 		TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
2072c349dbc7Sjsg 	struct request_wait wait;
20737f4dd379Sjsg 
20747f4dd379Sjsg 	might_sleep();
20757f4dd379Sjsg 	GEM_BUG_ON(timeout < 0);
20767f4dd379Sjsg 
2077c349dbc7Sjsg 	if (dma_fence_is_signaled(&rq->fence))
20781bb76ff1Sjsg 		return timeout ?: 1;
20797f4dd379Sjsg 
20807f4dd379Sjsg 	if (!timeout)
20817f4dd379Sjsg 		return -ETIME;
20827f4dd379Sjsg 
20837f4dd379Sjsg 	trace_i915_request_wait_begin(rq, flags);
20847f4dd379Sjsg 
20857f4dd379Sjsg 	/*
2086c349dbc7Sjsg 	 * We must never wait on the GPU while holding a lock as we
2087c349dbc7Sjsg 	 * may need to perform a GPU reset. So while we don't need to
2088c349dbc7Sjsg 	 * serialise wait/reset with an explicit lock, we do want
2089c349dbc7Sjsg 	 * lockdep to detect potential dependency cycles.
20907f4dd379Sjsg 	 */
2091c349dbc7Sjsg 	mutex_acquire(&rq->engine->gt->reset.mutex.dep_map, 0, 0, _THIS_IP_);
20927f4dd379Sjsg 
2093c349dbc7Sjsg 	/*
2094c349dbc7Sjsg 	 * Optimistic spin before touching IRQs.
2095c349dbc7Sjsg 	 *
2096c349dbc7Sjsg 	 * We may use a rather large value here to offset the penalty of
2097c349dbc7Sjsg 	 * switching away from the active task. Frequently, the client will
2098c349dbc7Sjsg 	 * wait upon an old swapbuffer to throttle itself to remain within a
2099c349dbc7Sjsg 	 * frame of the gpu. If the client is running in lockstep with the gpu,
2100c349dbc7Sjsg 	 * then it should not be waiting long at all, and a sleep now will incur
2101c349dbc7Sjsg 	 * extra scheduler latency in producing the next frame. To try to
2102c349dbc7Sjsg 	 * avoid adding the cost of enabling/disabling the interrupt to the
2103c349dbc7Sjsg 	 * short wait, we first spin to see if the request would have completed
2104c349dbc7Sjsg 	 * in the time taken to setup the interrupt.
2105c349dbc7Sjsg 	 *
2106c349dbc7Sjsg 	 * We need upto 5us to enable the irq, and upto 20us to hide the
2107c349dbc7Sjsg 	 * scheduler latency of a context switch, ignoring the secondary
2108c349dbc7Sjsg 	 * impacts from a context switch such as cache eviction.
2109c349dbc7Sjsg 	 *
2110c349dbc7Sjsg 	 * The scheme used for low-latency IO is called "hybrid interrupt
2111c349dbc7Sjsg 	 * polling". The suggestion there is to sleep until just before you
2112c349dbc7Sjsg 	 * expect to be woken by the device interrupt and then poll for its
2113c349dbc7Sjsg 	 * completion. That requires having a good predictor for the request
2114c349dbc7Sjsg 	 * duration, which we currently lack.
2115c349dbc7Sjsg 	 */
21161bb76ff1Sjsg 	if (CONFIG_DRM_I915_MAX_REQUEST_BUSYWAIT &&
2117ad8b1aafSjsg 	    __i915_spin_request(rq, state))
2118c349dbc7Sjsg 		goto out;
2119c349dbc7Sjsg 
2120c349dbc7Sjsg 	/*
2121c349dbc7Sjsg 	 * This client is about to stall waiting for the GPU. In many cases
2122c349dbc7Sjsg 	 * this is undesirable and limits the throughput of the system, as
2123c349dbc7Sjsg 	 * many clients cannot continue processing user input/output whilst
2124c349dbc7Sjsg 	 * blocked. RPS autotuning may take tens of milliseconds to respond
2125c349dbc7Sjsg 	 * to the GPU load and thus incurs additional latency for the client.
2126c349dbc7Sjsg 	 * We can circumvent that by promoting the GPU frequency to maximum
2127c349dbc7Sjsg 	 * before we sleep. This makes the GPU throttle up much more quickly
2128c349dbc7Sjsg 	 * (good for benchmarks and user experience, e.g. window animations),
2129c349dbc7Sjsg 	 * but at a cost of spending more power processing the workload
2130c349dbc7Sjsg 	 * (bad for battery).
2131c349dbc7Sjsg 	 */
2132ad8b1aafSjsg 	if (flags & I915_WAIT_PRIORITY && !i915_request_started(rq))
2133c349dbc7Sjsg 		intel_rps_boost(rq);
2134c349dbc7Sjsg 
2135c349dbc7Sjsg #ifdef __linux__
2136c349dbc7Sjsg 	wait.tsk = current;
2137c349dbc7Sjsg #else
2138c349dbc7Sjsg 	wait.tsk = curproc;
2139c349dbc7Sjsg #endif
2140c349dbc7Sjsg 	if (dma_fence_add_callback(&rq->fence, &wait.cb, request_wait_wake))
2141c349dbc7Sjsg 		goto out;
21427f4dd379Sjsg 
2143ad8b1aafSjsg 	/*
2144ad8b1aafSjsg 	 * Flush the submission tasklet, but only if it may help this request.
2145ad8b1aafSjsg 	 *
2146ad8b1aafSjsg 	 * We sometimes experience some latency between the HW interrupts and
2147ad8b1aafSjsg 	 * tasklet execution (mostly due to ksoftirqd latency, but it can also
2148ad8b1aafSjsg 	 * be due to lazy CS events), so lets run the tasklet manually if there
2149ad8b1aafSjsg 	 * is a chance it may submit this request. If the request is not ready
2150ad8b1aafSjsg 	 * to run, as it is waiting for other fences to be signaled, flushing
2151ad8b1aafSjsg 	 * the tasklet is busy work without any advantage for this client.
2152ad8b1aafSjsg 	 *
2153ad8b1aafSjsg 	 * If the HW is being lazy, this is the last chance before we go to
2154ad8b1aafSjsg 	 * sleep to catch any pending events. We will check periodically in
2155ad8b1aafSjsg 	 * the heartbeat to flush the submission tasklets as a last resort
2156ad8b1aafSjsg 	 * for unhappy HW.
2157ad8b1aafSjsg 	 */
2158ad8b1aafSjsg 	if (i915_request_is_ready(rq))
21595ca02815Sjsg 		__intel_engine_flush_submission(rq->engine, false);
2160ad8b1aafSjsg 
21617f4dd379Sjsg 	for (;;) {
2162c349dbc7Sjsg 		set_current_state(state);
2163c349dbc7Sjsg 
2164ad8b1aafSjsg 		if (dma_fence_is_signaled(&rq->fence))
2165c349dbc7Sjsg 			break;
2166c349dbc7Sjsg 
21677f4dd379Sjsg 		if (signal_pending_state(state, current)) {
21687f4dd379Sjsg 			timeout = -ERESTARTSYS;
21697f4dd379Sjsg 			break;
21707f4dd379Sjsg 		}
21717f4dd379Sjsg 
21727f4dd379Sjsg 		if (!timeout) {
21737f4dd379Sjsg 			timeout = -ETIME;
21747f4dd379Sjsg 			break;
21757f4dd379Sjsg 		}
21767f4dd379Sjsg 
21777f4dd379Sjsg 		timeout = io_schedule_timeout(timeout);
21787f4dd379Sjsg 	}
21797f4dd379Sjsg 	__set_current_state(TASK_RUNNING);
2180c349dbc7Sjsg 
2181ad8b1aafSjsg 	if (READ_ONCE(wait.tsk))
2182c349dbc7Sjsg 		dma_fence_remove_callback(&rq->fence, &wait.cb);
2183ad8b1aafSjsg 	GEM_BUG_ON(!list_empty(&wait.cb.node));
2184c349dbc7Sjsg 
2185c349dbc7Sjsg out:
2186c349dbc7Sjsg 	mutex_release(&rq->engine->gt->reset.mutex.dep_map, _THIS_IP_);
21877f4dd379Sjsg 	trace_i915_request_wait_end(rq);
21887f4dd379Sjsg 	return timeout;
21897f4dd379Sjsg }
21907f4dd379Sjsg 
21911bb76ff1Sjsg /**
21921bb76ff1Sjsg  * i915_request_wait - wait until execution of request has finished
21931bb76ff1Sjsg  * @rq: the request to wait upon
21941bb76ff1Sjsg  * @flags: how to wait
21951bb76ff1Sjsg  * @timeout: how long to wait in jiffies
21961bb76ff1Sjsg  *
21971bb76ff1Sjsg  * i915_request_wait() waits for the request to be completed, for a
21981bb76ff1Sjsg  * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
21991bb76ff1Sjsg  * unbounded wait).
22001bb76ff1Sjsg  *
22011bb76ff1Sjsg  * Returns the remaining time (in jiffies) if the request completed, which may
22021bb76ff1Sjsg  * be zero or -ETIME if the request is unfinished after the timeout expires.
22031bb76ff1Sjsg  * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
22041bb76ff1Sjsg  * pending before the request completes.
22051bb76ff1Sjsg  *
22061bb76ff1Sjsg  * NOTE: This function behaves differently from dma-fence wait semantics for
22071bb76ff1Sjsg  * timeout = 0. It returns 0 on success, and -ETIME if not signaled.
22081bb76ff1Sjsg  */
i915_request_wait(struct i915_request * rq,unsigned int flags,long timeout)22091bb76ff1Sjsg long i915_request_wait(struct i915_request *rq,
22101bb76ff1Sjsg 		       unsigned int flags,
22111bb76ff1Sjsg 		       long timeout)
22121bb76ff1Sjsg {
22131bb76ff1Sjsg 	long ret = i915_request_wait_timeout(rq, flags, timeout);
22141bb76ff1Sjsg 
22151bb76ff1Sjsg 	if (!ret)
22161bb76ff1Sjsg 		return -ETIME;
22171bb76ff1Sjsg 
22181bb76ff1Sjsg 	if (ret > 0 && !timeout)
22191bb76ff1Sjsg 		return 0;
22201bb76ff1Sjsg 
22211bb76ff1Sjsg 	return ret;
22221bb76ff1Sjsg }
22231bb76ff1Sjsg 
print_sched_attr(const struct i915_sched_attr * attr,char * buf,int x,int len)22245ca02815Sjsg static int print_sched_attr(const struct i915_sched_attr *attr,
22255ca02815Sjsg 			    char *buf, int x, int len)
22265ca02815Sjsg {
22275ca02815Sjsg 	if (attr->priority == I915_PRIORITY_INVALID)
22285ca02815Sjsg 		return x;
22295ca02815Sjsg 
22305ca02815Sjsg 	x += snprintf(buf + x, len - x,
22315ca02815Sjsg 		      " prio=%d", attr->priority);
22325ca02815Sjsg 
22335ca02815Sjsg 	return x;
22345ca02815Sjsg }
22355ca02815Sjsg 
queue_status(const struct i915_request * rq)22365ca02815Sjsg static char queue_status(const struct i915_request *rq)
22375ca02815Sjsg {
22385ca02815Sjsg 	if (i915_request_is_active(rq))
22395ca02815Sjsg 		return 'E';
22405ca02815Sjsg 
22415ca02815Sjsg 	if (i915_request_is_ready(rq))
22425ca02815Sjsg 		return intel_engine_is_virtual(rq->engine) ? 'V' : 'R';
22435ca02815Sjsg 
22445ca02815Sjsg 	return 'U';
22455ca02815Sjsg }
22465ca02815Sjsg 
run_status(const struct i915_request * rq)22475ca02815Sjsg static const char *run_status(const struct i915_request *rq)
22485ca02815Sjsg {
22495ca02815Sjsg 	if (__i915_request_is_complete(rq))
22505ca02815Sjsg 		return "!";
22515ca02815Sjsg 
22525ca02815Sjsg 	if (__i915_request_has_started(rq))
22535ca02815Sjsg 		return "*";
22545ca02815Sjsg 
22555ca02815Sjsg 	if (!i915_sw_fence_signaled(&rq->semaphore))
22565ca02815Sjsg 		return "&";
22575ca02815Sjsg 
22585ca02815Sjsg 	return "";
22595ca02815Sjsg }
22605ca02815Sjsg 
fence_status(const struct i915_request * rq)22615ca02815Sjsg static const char *fence_status(const struct i915_request *rq)
22625ca02815Sjsg {
22635ca02815Sjsg 	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags))
22645ca02815Sjsg 		return "+";
22655ca02815Sjsg 
22665ca02815Sjsg 	if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &rq->fence.flags))
22675ca02815Sjsg 		return "-";
22685ca02815Sjsg 
22695ca02815Sjsg 	return "";
22705ca02815Sjsg }
22715ca02815Sjsg 
i915_request_show(struct drm_printer * m,const struct i915_request * rq,const char * prefix,int indent)22725ca02815Sjsg void i915_request_show(struct drm_printer *m,
22735ca02815Sjsg 		       const struct i915_request *rq,
22745ca02815Sjsg 		       const char *prefix,
22755ca02815Sjsg 		       int indent)
22765ca02815Sjsg {
22775ca02815Sjsg 	const char *name = rq->fence.ops->get_timeline_name((struct dma_fence *)&rq->fence);
22785ca02815Sjsg 	char buf[80] = "";
22795ca02815Sjsg 	int x = 0;
22805ca02815Sjsg 
22815ca02815Sjsg 	/*
22825ca02815Sjsg 	 * The prefix is used to show the queue status, for which we use
22835ca02815Sjsg 	 * the following flags:
22845ca02815Sjsg 	 *
22855ca02815Sjsg 	 *  U [Unready]
22865ca02815Sjsg 	 *    - initial status upon being submitted by the user
22875ca02815Sjsg 	 *
22885ca02815Sjsg 	 *    - the request is not ready for execution as it is waiting
22895ca02815Sjsg 	 *      for external fences
22905ca02815Sjsg 	 *
22915ca02815Sjsg 	 *  R [Ready]
22925ca02815Sjsg 	 *    - all fences the request was waiting on have been signaled,
22935ca02815Sjsg 	 *      and the request is now ready for execution and will be
22945ca02815Sjsg 	 *      in a backend queue
22955ca02815Sjsg 	 *
22965ca02815Sjsg 	 *    - a ready request may still need to wait on semaphores
22975ca02815Sjsg 	 *      [internal fences]
22985ca02815Sjsg 	 *
22995ca02815Sjsg 	 *  V [Ready/virtual]
23005ca02815Sjsg 	 *    - same as ready, but queued over multiple backends
23015ca02815Sjsg 	 *
23025ca02815Sjsg 	 *  E [Executing]
23035ca02815Sjsg 	 *    - the request has been transferred from the backend queue and
23045ca02815Sjsg 	 *      submitted for execution on HW
23055ca02815Sjsg 	 *
23065ca02815Sjsg 	 *    - a completed request may still be regarded as executing, its
23075ca02815Sjsg 	 *      status may not be updated until it is retired and removed
23085ca02815Sjsg 	 *      from the lists
23095ca02815Sjsg 	 */
23105ca02815Sjsg 
23115ca02815Sjsg 	x = print_sched_attr(&rq->sched.attr, buf, x, sizeof(buf));
23125ca02815Sjsg 
23135ca02815Sjsg 	drm_printf(m, "%s%.*s%c %llx:%lld%s%s %s @ %dms: %s\n",
23145ca02815Sjsg 		   prefix, indent, "                ",
23155ca02815Sjsg 		   queue_status(rq),
23165ca02815Sjsg 		   rq->fence.context, rq->fence.seqno,
23175ca02815Sjsg 		   run_status(rq),
23185ca02815Sjsg 		   fence_status(rq),
23195ca02815Sjsg 		   buf,
23205ca02815Sjsg 		   jiffies_to_msecs(jiffies - rq->emitted_jiffies),
23215ca02815Sjsg 		   name);
23225ca02815Sjsg }
23235ca02815Sjsg 
engine_match_ring(struct intel_engine_cs * engine,struct i915_request * rq)23245ca02815Sjsg static bool engine_match_ring(struct intel_engine_cs *engine, struct i915_request *rq)
23255ca02815Sjsg {
23265ca02815Sjsg 	u32 ring = ENGINE_READ(engine, RING_START);
23275ca02815Sjsg 
23285ca02815Sjsg 	return ring == i915_ggtt_offset(rq->ring->vma);
23295ca02815Sjsg }
23305ca02815Sjsg 
match_ring(struct i915_request * rq)23315ca02815Sjsg static bool match_ring(struct i915_request *rq)
23325ca02815Sjsg {
23335ca02815Sjsg 	struct intel_engine_cs *engine;
23345ca02815Sjsg 	bool found;
23355ca02815Sjsg 	int i;
23365ca02815Sjsg 
23375ca02815Sjsg 	if (!intel_engine_is_virtual(rq->engine))
23385ca02815Sjsg 		return engine_match_ring(rq->engine, rq);
23395ca02815Sjsg 
23405ca02815Sjsg 	found = false;
23415ca02815Sjsg 	i = 0;
23425ca02815Sjsg 	while ((engine = intel_engine_get_sibling(rq->engine, i++))) {
23435ca02815Sjsg 		found = engine_match_ring(engine, rq);
23445ca02815Sjsg 		if (found)
23455ca02815Sjsg 			break;
23465ca02815Sjsg 	}
23475ca02815Sjsg 
23485ca02815Sjsg 	return found;
23495ca02815Sjsg }
23505ca02815Sjsg 
i915_test_request_state(struct i915_request * rq)23515ca02815Sjsg enum i915_request_state i915_test_request_state(struct i915_request *rq)
23525ca02815Sjsg {
23535ca02815Sjsg 	if (i915_request_completed(rq))
23545ca02815Sjsg 		return I915_REQUEST_COMPLETE;
23555ca02815Sjsg 
23565ca02815Sjsg 	if (!i915_request_started(rq))
23575ca02815Sjsg 		return I915_REQUEST_PENDING;
23585ca02815Sjsg 
23595ca02815Sjsg 	if (match_ring(rq))
23605ca02815Sjsg 		return I915_REQUEST_ACTIVE;
23615ca02815Sjsg 
23625ca02815Sjsg 	return I915_REQUEST_QUEUED;
23635ca02815Sjsg }
23645ca02815Sjsg 
23657f4dd379Sjsg #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
23667f4dd379Sjsg #include "selftests/mock_request.c"
23677f4dd379Sjsg #include "selftests/i915_request.c"
23687f4dd379Sjsg #endif
2369c349dbc7Sjsg 
i915_request_module_exit(void)23705ca02815Sjsg void i915_request_module_exit(void)
2371c349dbc7Sjsg {
2372c349dbc7Sjsg #ifdef __linux__
23735ca02815Sjsg 	kmem_cache_destroy(slab_execute_cbs);
23745ca02815Sjsg 	kmem_cache_destroy(slab_requests);
2375c349dbc7Sjsg #else
23765ca02815Sjsg 	pool_destroy(&slab_execute_cbs);
23775ca02815Sjsg 	pool_destroy(&slab_requests);
2378c349dbc7Sjsg #endif
2379c349dbc7Sjsg }
2380c349dbc7Sjsg 
i915_request_module_init(void)23815ca02815Sjsg int __init i915_request_module_init(void)
2382c349dbc7Sjsg {
2383c349dbc7Sjsg #ifdef __linux__
23845ca02815Sjsg 	slab_requests =
2385c349dbc7Sjsg 		kmem_cache_create("i915_request",
2386c349dbc7Sjsg 				  sizeof(struct i915_request),
2387c349dbc7Sjsg 				  __alignof__(struct i915_request),
2388c349dbc7Sjsg 				  SLAB_HWCACHE_ALIGN |
2389c349dbc7Sjsg 				  SLAB_RECLAIM_ACCOUNT |
2390c349dbc7Sjsg 				  SLAB_TYPESAFE_BY_RCU,
2391c349dbc7Sjsg 				  __i915_request_ctor);
23925ca02815Sjsg 	if (!slab_requests)
2393c349dbc7Sjsg 		return -ENOMEM;
2394c349dbc7Sjsg 
23955ca02815Sjsg 	slab_execute_cbs = KMEM_CACHE(execute_cb,
2396c349dbc7Sjsg 					     SLAB_HWCACHE_ALIGN |
2397c349dbc7Sjsg 					     SLAB_RECLAIM_ACCOUNT |
2398c349dbc7Sjsg 					     SLAB_TYPESAFE_BY_RCU);
23995ca02815Sjsg 	if (!slab_execute_cbs)
2400c349dbc7Sjsg 		goto err_requests;
2401c349dbc7Sjsg #else
24025ca02815Sjsg 	pool_init(&slab_requests, sizeof(struct i915_request),
24030f557061Sjsg 	    CACHELINESIZE, IPL_TTY, 0, "i915_request", NULL);
24045ca02815Sjsg 	pool_init(&slab_execute_cbs, sizeof(struct execute_cb),
24050f557061Sjsg 	    CACHELINESIZE, IPL_TTY, 0, "i915_exec", NULL);
2406c349dbc7Sjsg #endif
2407c349dbc7Sjsg 
2408c349dbc7Sjsg 	return 0;
2409c349dbc7Sjsg 
2410c349dbc7Sjsg #ifdef __linux__
2411c349dbc7Sjsg err_requests:
24125ca02815Sjsg 	kmem_cache_destroy(slab_requests);
2413c349dbc7Sjsg 	return -ENOMEM;
2414c349dbc7Sjsg #endif
2415c349dbc7Sjsg }
2416