1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright © 2008-2015 Intel Corporation
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*4882a593Smuzhiyun * and/or sell copies of the Software, and to permit persons to whom the
9*4882a593Smuzhiyun * Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * The above copyright notice and this permission notice (including the next
12*4882a593Smuzhiyun * paragraph) shall be included in all copies or substantial portions of the
13*4882a593Smuzhiyun * Software.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18*4882a593Smuzhiyun * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*4882a593Smuzhiyun * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*4882a593Smuzhiyun * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21*4882a593Smuzhiyun * IN THE SOFTWARE.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun */
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun #include <linux/dma-fence-array.h>
26*4882a593Smuzhiyun #include <linux/dma-fence-chain.h>
27*4882a593Smuzhiyun #include <linux/irq_work.h>
28*4882a593Smuzhiyun #include <linux/prefetch.h>
29*4882a593Smuzhiyun #include <linux/sched.h>
30*4882a593Smuzhiyun #include <linux/sched/clock.h>
31*4882a593Smuzhiyun #include <linux/sched/signal.h>
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #include "gem/i915_gem_context.h"
34*4882a593Smuzhiyun #include "gt/intel_breadcrumbs.h"
35*4882a593Smuzhiyun #include "gt/intel_context.h"
36*4882a593Smuzhiyun #include "gt/intel_ring.h"
37*4882a593Smuzhiyun #include "gt/intel_rps.h"
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun #include "i915_active.h"
40*4882a593Smuzhiyun #include "i915_drv.h"
41*4882a593Smuzhiyun #include "i915_globals.h"
42*4882a593Smuzhiyun #include "i915_trace.h"
43*4882a593Smuzhiyun #include "intel_pm.h"
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun struct execute_cb {
46*4882a593Smuzhiyun struct irq_work work;
47*4882a593Smuzhiyun struct i915_sw_fence *fence;
48*4882a593Smuzhiyun void (*hook)(struct i915_request *rq, struct dma_fence *signal);
49*4882a593Smuzhiyun struct i915_request *signal;
50*4882a593Smuzhiyun };
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun static struct i915_global_request {
53*4882a593Smuzhiyun struct i915_global base;
54*4882a593Smuzhiyun struct kmem_cache *slab_requests;
55*4882a593Smuzhiyun struct kmem_cache *slab_execute_cbs;
56*4882a593Smuzhiyun } global;
57*4882a593Smuzhiyun
i915_fence_get_driver_name(struct dma_fence * fence)58*4882a593Smuzhiyun static const char *i915_fence_get_driver_name(struct dma_fence *fence)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun return dev_name(to_request(fence)->engine->i915->drm.dev);
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun
i915_fence_get_timeline_name(struct dma_fence * fence)63*4882a593Smuzhiyun static const char *i915_fence_get_timeline_name(struct dma_fence *fence)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun const struct i915_gem_context *ctx;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun /*
68*4882a593Smuzhiyun * The timeline struct (as part of the ppgtt underneath a context)
69*4882a593Smuzhiyun * may be freed when the request is no longer in use by the GPU.
70*4882a593Smuzhiyun * We could extend the life of a context to beyond that of all
71*4882a593Smuzhiyun * fences, possibly keeping the hw resource around indefinitely,
72*4882a593Smuzhiyun * or we just give them a false name. Since
73*4882a593Smuzhiyun * dma_fence_ops.get_timeline_name is a debug feature, the occasional
74*4882a593Smuzhiyun * lie seems justifiable.
75*4882a593Smuzhiyun */
76*4882a593Smuzhiyun if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
77*4882a593Smuzhiyun return "signaled";
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun ctx = i915_request_gem_context(to_request(fence));
80*4882a593Smuzhiyun if (!ctx)
81*4882a593Smuzhiyun return "[" DRIVER_NAME "]";
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun return ctx->name;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
i915_fence_signaled(struct dma_fence * fence)86*4882a593Smuzhiyun static bool i915_fence_signaled(struct dma_fence *fence)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun return i915_request_completed(to_request(fence));
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
i915_fence_enable_signaling(struct dma_fence * fence)91*4882a593Smuzhiyun static bool i915_fence_enable_signaling(struct dma_fence *fence)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun return i915_request_enable_breadcrumb(to_request(fence));
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
i915_fence_wait(struct dma_fence * fence,bool interruptible,signed long timeout)96*4882a593Smuzhiyun static signed long i915_fence_wait(struct dma_fence *fence,
97*4882a593Smuzhiyun bool interruptible,
98*4882a593Smuzhiyun signed long timeout)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun return i915_request_wait(to_request(fence),
101*4882a593Smuzhiyun interruptible | I915_WAIT_PRIORITY,
102*4882a593Smuzhiyun timeout);
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
i915_request_slab_cache(void)105*4882a593Smuzhiyun struct kmem_cache *i915_request_slab_cache(void)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun return global.slab_requests;
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
i915_fence_release(struct dma_fence * fence)110*4882a593Smuzhiyun static void i915_fence_release(struct dma_fence *fence)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun struct i915_request *rq = to_request(fence);
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /*
115*4882a593Smuzhiyun * The request is put onto a RCU freelist (i.e. the address
116*4882a593Smuzhiyun * is immediately reused), mark the fences as being freed now.
117*4882a593Smuzhiyun * Otherwise the debugobjects for the fences are only marked as
118*4882a593Smuzhiyun * freed when the slab cache itself is freed, and so we would get
119*4882a593Smuzhiyun * caught trying to reuse dead objects.
120*4882a593Smuzhiyun */
121*4882a593Smuzhiyun i915_sw_fence_fini(&rq->submit);
122*4882a593Smuzhiyun i915_sw_fence_fini(&rq->semaphore);
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun /*
125*4882a593Smuzhiyun * Keep one request on each engine for reserved use under mempressure
126*4882a593Smuzhiyun *
127*4882a593Smuzhiyun * We do not hold a reference to the engine here and so have to be
128*4882a593Smuzhiyun * very careful in what rq->engine we poke. The virtual engine is
129*4882a593Smuzhiyun * referenced via the rq->context and we released that ref during
130*4882a593Smuzhiyun * i915_request_retire(), ergo we must not dereference a virtual
131*4882a593Smuzhiyun * engine here. Not that we would want to, as the only consumer of
132*4882a593Smuzhiyun * the reserved engine->request_pool is the power management parking,
133*4882a593Smuzhiyun * which must-not-fail, and that is only run on the physical engines.
134*4882a593Smuzhiyun *
135*4882a593Smuzhiyun * Since the request must have been executed to be have completed,
136*4882a593Smuzhiyun * we know that it will have been processed by the HW and will
137*4882a593Smuzhiyun * not be unsubmitted again, so rq->engine and rq->execution_mask
138*4882a593Smuzhiyun * at this point is stable. rq->execution_mask will be a single
139*4882a593Smuzhiyun * bit if the last and _only_ engine it could execution on was a
140*4882a593Smuzhiyun * physical engine, if it's multiple bits then it started on and
141*4882a593Smuzhiyun * could still be on a virtual engine. Thus if the mask is not a
142*4882a593Smuzhiyun * power-of-two we assume that rq->engine may still be a virtual
143*4882a593Smuzhiyun * engine and so a dangling invalid pointer that we cannot dereference
144*4882a593Smuzhiyun *
145*4882a593Smuzhiyun * For example, consider the flow of a bonded request through a virtual
146*4882a593Smuzhiyun * engine. The request is created with a wide engine mask (all engines
147*4882a593Smuzhiyun * that we might execute on). On processing the bond, the request mask
148*4882a593Smuzhiyun * is reduced to one or more engines. If the request is subsequently
149*4882a593Smuzhiyun * bound to a single engine, it will then be constrained to only
150*4882a593Smuzhiyun * execute on that engine and never returned to the virtual engine
151*4882a593Smuzhiyun * after timeslicing away, see __unwind_incomplete_requests(). Thus we
152*4882a593Smuzhiyun * know that if the rq->execution_mask is a single bit, rq->engine
153*4882a593Smuzhiyun * can be a physical engine with the exact corresponding mask.
154*4882a593Smuzhiyun */
155*4882a593Smuzhiyun if (is_power_of_2(rq->execution_mask) &&
156*4882a593Smuzhiyun !cmpxchg(&rq->engine->request_pool, NULL, rq))
157*4882a593Smuzhiyun return;
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun kmem_cache_free(global.slab_requests, rq);
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun const struct dma_fence_ops i915_fence_ops = {
163*4882a593Smuzhiyun .get_driver_name = i915_fence_get_driver_name,
164*4882a593Smuzhiyun .get_timeline_name = i915_fence_get_timeline_name,
165*4882a593Smuzhiyun .enable_signaling = i915_fence_enable_signaling,
166*4882a593Smuzhiyun .signaled = i915_fence_signaled,
167*4882a593Smuzhiyun .wait = i915_fence_wait,
168*4882a593Smuzhiyun .release = i915_fence_release,
169*4882a593Smuzhiyun };
170*4882a593Smuzhiyun
irq_execute_cb(struct irq_work * wrk)171*4882a593Smuzhiyun static void irq_execute_cb(struct irq_work *wrk)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun struct execute_cb *cb = container_of(wrk, typeof(*cb), work);
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun i915_sw_fence_complete(cb->fence);
176*4882a593Smuzhiyun kmem_cache_free(global.slab_execute_cbs, cb);
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
irq_execute_cb_hook(struct irq_work * wrk)179*4882a593Smuzhiyun static void irq_execute_cb_hook(struct irq_work *wrk)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun struct execute_cb *cb = container_of(wrk, typeof(*cb), work);
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun cb->hook(container_of(cb->fence, struct i915_request, submit),
184*4882a593Smuzhiyun &cb->signal->fence);
185*4882a593Smuzhiyun i915_request_put(cb->signal);
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun irq_execute_cb(wrk);
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun static __always_inline void
__notify_execute_cb(struct i915_request * rq,bool (* fn)(struct irq_work * wrk))191*4882a593Smuzhiyun __notify_execute_cb(struct i915_request *rq, bool (*fn)(struct irq_work *wrk))
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun struct execute_cb *cb, *cn;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun if (llist_empty(&rq->execute_cb))
196*4882a593Smuzhiyun return;
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun llist_for_each_entry_safe(cb, cn,
199*4882a593Smuzhiyun llist_del_all(&rq->execute_cb),
200*4882a593Smuzhiyun work.llnode)
201*4882a593Smuzhiyun fn(&cb->work);
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun
__notify_execute_cb_irq(struct i915_request * rq)204*4882a593Smuzhiyun static void __notify_execute_cb_irq(struct i915_request *rq)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun __notify_execute_cb(rq, irq_work_queue);
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun
irq_work_imm(struct irq_work * wrk)209*4882a593Smuzhiyun static bool irq_work_imm(struct irq_work *wrk)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun wrk->func(wrk);
212*4882a593Smuzhiyun return false;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun
__notify_execute_cb_imm(struct i915_request * rq)215*4882a593Smuzhiyun static void __notify_execute_cb_imm(struct i915_request *rq)
216*4882a593Smuzhiyun {
217*4882a593Smuzhiyun __notify_execute_cb(rq, irq_work_imm);
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun
free_capture_list(struct i915_request * request)220*4882a593Smuzhiyun static void free_capture_list(struct i915_request *request)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun struct i915_capture_list *capture;
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun capture = fetch_and_zero(&request->capture_list);
225*4882a593Smuzhiyun while (capture) {
226*4882a593Smuzhiyun struct i915_capture_list *next = capture->next;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun kfree(capture);
229*4882a593Smuzhiyun capture = next;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun
__i915_request_fill(struct i915_request * rq,u8 val)233*4882a593Smuzhiyun static void __i915_request_fill(struct i915_request *rq, u8 val)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun void *vaddr = rq->ring->vaddr;
236*4882a593Smuzhiyun u32 head;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun head = rq->infix;
239*4882a593Smuzhiyun if (rq->postfix < head) {
240*4882a593Smuzhiyun memset(vaddr + head, val, rq->ring->size - head);
241*4882a593Smuzhiyun head = 0;
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun memset(vaddr + head, val, rq->postfix - head);
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun
remove_from_engine(struct i915_request * rq)246*4882a593Smuzhiyun static void remove_from_engine(struct i915_request *rq)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun struct intel_engine_cs *engine, *locked;
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun /*
251*4882a593Smuzhiyun * Virtual engines complicate acquiring the engine timeline lock,
252*4882a593Smuzhiyun * as their rq->engine pointer is not stable until under that
253*4882a593Smuzhiyun * engine lock. The simple ploy we use is to take the lock then
254*4882a593Smuzhiyun * check that the rq still belongs to the newly locked engine.
255*4882a593Smuzhiyun */
256*4882a593Smuzhiyun locked = READ_ONCE(rq->engine);
257*4882a593Smuzhiyun spin_lock_irq(&locked->active.lock);
258*4882a593Smuzhiyun while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
259*4882a593Smuzhiyun spin_unlock(&locked->active.lock);
260*4882a593Smuzhiyun spin_lock(&engine->active.lock);
261*4882a593Smuzhiyun locked = engine;
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun list_del_init(&rq->sched.link);
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
266*4882a593Smuzhiyun clear_bit(I915_FENCE_FLAG_HOLD, &rq->fence.flags);
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun /* Prevent further __await_execution() registering a cb, then flush */
269*4882a593Smuzhiyun set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun spin_unlock_irq(&locked->active.lock);
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun __notify_execute_cb_imm(rq);
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun
i915_request_retire(struct i915_request * rq)276*4882a593Smuzhiyun bool i915_request_retire(struct i915_request *rq)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun if (!i915_request_completed(rq))
279*4882a593Smuzhiyun return false;
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun RQ_TRACE(rq, "\n");
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun GEM_BUG_ON(!i915_sw_fence_signaled(&rq->submit));
284*4882a593Smuzhiyun trace_i915_request_retire(rq);
285*4882a593Smuzhiyun i915_request_mark_complete(rq);
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun /*
288*4882a593Smuzhiyun * We know the GPU must have read the request to have
289*4882a593Smuzhiyun * sent us the seqno + interrupt, so use the position
290*4882a593Smuzhiyun * of tail of the request to update the last known position
291*4882a593Smuzhiyun * of the GPU head.
292*4882a593Smuzhiyun *
293*4882a593Smuzhiyun * Note this requires that we are always called in request
294*4882a593Smuzhiyun * completion order.
295*4882a593Smuzhiyun */
296*4882a593Smuzhiyun GEM_BUG_ON(!list_is_first(&rq->link,
297*4882a593Smuzhiyun &i915_request_timeline(rq)->requests));
298*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
299*4882a593Smuzhiyun /* Poison before we release our space in the ring */
300*4882a593Smuzhiyun __i915_request_fill(rq, POISON_FREE);
301*4882a593Smuzhiyun rq->ring->head = rq->postfix;
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun if (!i915_request_signaled(rq)) {
304*4882a593Smuzhiyun spin_lock_irq(&rq->lock);
305*4882a593Smuzhiyun dma_fence_signal_locked(&rq->fence);
306*4882a593Smuzhiyun spin_unlock_irq(&rq->lock);
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun if (i915_request_has_waitboost(rq)) {
310*4882a593Smuzhiyun GEM_BUG_ON(!atomic_read(&rq->engine->gt->rps.num_waiters));
311*4882a593Smuzhiyun atomic_dec(&rq->engine->gt->rps.num_waiters);
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun /*
315*4882a593Smuzhiyun * We only loosely track inflight requests across preemption,
316*4882a593Smuzhiyun * and so we may find ourselves attempting to retire a _completed_
317*4882a593Smuzhiyun * request that we have removed from the HW and put back on a run
318*4882a593Smuzhiyun * queue.
319*4882a593Smuzhiyun *
320*4882a593Smuzhiyun * As we set I915_FENCE_FLAG_ACTIVE on the request, this should be
321*4882a593Smuzhiyun * after removing the breadcrumb and signaling it, so that we do not
322*4882a593Smuzhiyun * inadvertently attach the breadcrumb to a completed request.
323*4882a593Smuzhiyun */
324*4882a593Smuzhiyun remove_from_engine(rq);
325*4882a593Smuzhiyun GEM_BUG_ON(!llist_empty(&rq->execute_cb));
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun __list_del_entry(&rq->link); /* poison neither prev/next (RCU walks) */
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun intel_context_exit(rq->context);
330*4882a593Smuzhiyun intel_context_unpin(rq->context);
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun free_capture_list(rq);
333*4882a593Smuzhiyun i915_sched_node_fini(&rq->sched);
334*4882a593Smuzhiyun i915_request_put(rq);
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun return true;
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun
i915_request_retire_upto(struct i915_request * rq)339*4882a593Smuzhiyun void i915_request_retire_upto(struct i915_request *rq)
340*4882a593Smuzhiyun {
341*4882a593Smuzhiyun struct intel_timeline * const tl = i915_request_timeline(rq);
342*4882a593Smuzhiyun struct i915_request *tmp;
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun RQ_TRACE(rq, "\n");
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun GEM_BUG_ON(!i915_request_completed(rq));
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun do {
349*4882a593Smuzhiyun tmp = list_first_entry(&tl->requests, typeof(*tmp), link);
350*4882a593Smuzhiyun } while (i915_request_retire(tmp) && tmp != rq);
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun static struct i915_request * const *
__engine_active(struct intel_engine_cs * engine)354*4882a593Smuzhiyun __engine_active(struct intel_engine_cs *engine)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun return READ_ONCE(engine->execlists.active);
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun
__request_in_flight(const struct i915_request * signal)359*4882a593Smuzhiyun static bool __request_in_flight(const struct i915_request *signal)
360*4882a593Smuzhiyun {
361*4882a593Smuzhiyun struct i915_request * const *port, *rq;
362*4882a593Smuzhiyun bool inflight = false;
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun if (!i915_request_is_ready(signal))
365*4882a593Smuzhiyun return false;
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun /*
368*4882a593Smuzhiyun * Even if we have unwound the request, it may still be on
369*4882a593Smuzhiyun * the GPU (preempt-to-busy). If that request is inside an
370*4882a593Smuzhiyun * unpreemptible critical section, it will not be removed. Some
371*4882a593Smuzhiyun * GPU functions may even be stuck waiting for the paired request
372*4882a593Smuzhiyun * (__await_execution) to be submitted and cannot be preempted
373*4882a593Smuzhiyun * until the bond is executing.
374*4882a593Smuzhiyun *
375*4882a593Smuzhiyun * As we know that there are always preemption points between
376*4882a593Smuzhiyun * requests, we know that only the currently executing request
377*4882a593Smuzhiyun * may be still active even though we have cleared the flag.
378*4882a593Smuzhiyun * However, we can't rely on our tracking of ELSP[0] to know
379*4882a593Smuzhiyun * which request is currently active and so maybe stuck, as
380*4882a593Smuzhiyun * the tracking maybe an event behind. Instead assume that
381*4882a593Smuzhiyun * if the context is still inflight, then it is still active
382*4882a593Smuzhiyun * even if the active flag has been cleared.
383*4882a593Smuzhiyun *
384*4882a593Smuzhiyun * To further complicate matters, if there a pending promotion, the HW
385*4882a593Smuzhiyun * may either perform a context switch to the second inflight execlists,
386*4882a593Smuzhiyun * or it may switch to the pending set of execlists. In the case of the
387*4882a593Smuzhiyun * latter, it may send the ACK and we process the event copying the
388*4882a593Smuzhiyun * pending[] over top of inflight[], _overwriting_ our *active. Since
389*4882a593Smuzhiyun * this implies the HW is arbitrating and not struck in *active, we do
390*4882a593Smuzhiyun * not worry about complete accuracy, but we do require no read/write
391*4882a593Smuzhiyun * tearing of the pointer [the read of the pointer must be valid, even
392*4882a593Smuzhiyun * as the array is being overwritten, for which we require the writes
393*4882a593Smuzhiyun * to avoid tearing.]
394*4882a593Smuzhiyun *
395*4882a593Smuzhiyun * Note that the read of *execlists->active may race with the promotion
396*4882a593Smuzhiyun * of execlists->pending[] to execlists->inflight[], overwritting
397*4882a593Smuzhiyun * the value at *execlists->active. This is fine. The promotion implies
398*4882a593Smuzhiyun * that we received an ACK from the HW, and so the context is not
399*4882a593Smuzhiyun * stuck -- if we do not see ourselves in *active, the inflight status
400*4882a593Smuzhiyun * is valid. If instead we see ourselves being copied into *active,
401*4882a593Smuzhiyun * we are inflight and may signal the callback.
402*4882a593Smuzhiyun */
403*4882a593Smuzhiyun if (!intel_context_inflight(signal->context))
404*4882a593Smuzhiyun return false;
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun rcu_read_lock();
407*4882a593Smuzhiyun for (port = __engine_active(signal->engine);
408*4882a593Smuzhiyun (rq = READ_ONCE(*port)); /* may race with promotion of pending[] */
409*4882a593Smuzhiyun port++) {
410*4882a593Smuzhiyun if (rq->context == signal->context) {
411*4882a593Smuzhiyun inflight = i915_seqno_passed(rq->fence.seqno,
412*4882a593Smuzhiyun signal->fence.seqno);
413*4882a593Smuzhiyun break;
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun rcu_read_unlock();
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun return inflight;
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun static int
__await_execution(struct i915_request * rq,struct i915_request * signal,void (* hook)(struct i915_request * rq,struct dma_fence * signal),gfp_t gfp)422*4882a593Smuzhiyun __await_execution(struct i915_request *rq,
423*4882a593Smuzhiyun struct i915_request *signal,
424*4882a593Smuzhiyun void (*hook)(struct i915_request *rq,
425*4882a593Smuzhiyun struct dma_fence *signal),
426*4882a593Smuzhiyun gfp_t gfp)
427*4882a593Smuzhiyun {
428*4882a593Smuzhiyun struct execute_cb *cb;
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun if (i915_request_is_active(signal)) {
431*4882a593Smuzhiyun if (hook)
432*4882a593Smuzhiyun hook(rq, &signal->fence);
433*4882a593Smuzhiyun return 0;
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun cb = kmem_cache_alloc(global.slab_execute_cbs, gfp);
437*4882a593Smuzhiyun if (!cb)
438*4882a593Smuzhiyun return -ENOMEM;
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun cb->fence = &rq->submit;
441*4882a593Smuzhiyun i915_sw_fence_await(cb->fence);
442*4882a593Smuzhiyun init_irq_work(&cb->work, irq_execute_cb);
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun if (hook) {
445*4882a593Smuzhiyun cb->hook = hook;
446*4882a593Smuzhiyun cb->signal = i915_request_get(signal);
447*4882a593Smuzhiyun cb->work.func = irq_execute_cb_hook;
448*4882a593Smuzhiyun }
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun /*
451*4882a593Smuzhiyun * Register the callback first, then see if the signaler is already
452*4882a593Smuzhiyun * active. This ensures that if we race with the
453*4882a593Smuzhiyun * __notify_execute_cb from i915_request_submit() and we are not
454*4882a593Smuzhiyun * included in that list, we get a second bite of the cherry and
455*4882a593Smuzhiyun * execute it ourselves. After this point, a future
456*4882a593Smuzhiyun * i915_request_submit() will notify us.
457*4882a593Smuzhiyun *
458*4882a593Smuzhiyun * In i915_request_retire() we set the ACTIVE bit on a completed
459*4882a593Smuzhiyun * request (then flush the execute_cb). So by registering the
460*4882a593Smuzhiyun * callback first, then checking the ACTIVE bit, we serialise with
461*4882a593Smuzhiyun * the completed/retired request.
462*4882a593Smuzhiyun */
463*4882a593Smuzhiyun if (llist_add(&cb->work.llnode, &signal->execute_cb)) {
464*4882a593Smuzhiyun if (i915_request_is_active(signal) ||
465*4882a593Smuzhiyun __request_in_flight(signal))
466*4882a593Smuzhiyun __notify_execute_cb_imm(signal);
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun return 0;
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun
fatal_error(int error)472*4882a593Smuzhiyun static bool fatal_error(int error)
473*4882a593Smuzhiyun {
474*4882a593Smuzhiyun switch (error) {
475*4882a593Smuzhiyun case 0: /* not an error! */
476*4882a593Smuzhiyun case -EAGAIN: /* innocent victim of a GT reset (__i915_request_reset) */
477*4882a593Smuzhiyun case -ETIMEDOUT: /* waiting for Godot (timer_i915_sw_fence_wake) */
478*4882a593Smuzhiyun return false;
479*4882a593Smuzhiyun default:
480*4882a593Smuzhiyun return true;
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun
__i915_request_skip(struct i915_request * rq)484*4882a593Smuzhiyun void __i915_request_skip(struct i915_request *rq)
485*4882a593Smuzhiyun {
486*4882a593Smuzhiyun GEM_BUG_ON(!fatal_error(rq->fence.error));
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun if (rq->infix == rq->postfix)
489*4882a593Smuzhiyun return;
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun /*
492*4882a593Smuzhiyun * As this request likely depends on state from the lost
493*4882a593Smuzhiyun * context, clear out all the user operations leaving the
494*4882a593Smuzhiyun * breadcrumb at the end (so we get the fence notifications).
495*4882a593Smuzhiyun */
496*4882a593Smuzhiyun __i915_request_fill(rq, 0);
497*4882a593Smuzhiyun rq->infix = rq->postfix;
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun
i915_request_set_error_once(struct i915_request * rq,int error)500*4882a593Smuzhiyun void i915_request_set_error_once(struct i915_request *rq, int error)
501*4882a593Smuzhiyun {
502*4882a593Smuzhiyun int old;
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun GEM_BUG_ON(!IS_ERR_VALUE((long)error));
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun if (i915_request_signaled(rq))
507*4882a593Smuzhiyun return;
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun old = READ_ONCE(rq->fence.error);
510*4882a593Smuzhiyun do {
511*4882a593Smuzhiyun if (fatal_error(old))
512*4882a593Smuzhiyun return;
513*4882a593Smuzhiyun } while (!try_cmpxchg(&rq->fence.error, &old, error));
514*4882a593Smuzhiyun }
515*4882a593Smuzhiyun
__i915_request_submit(struct i915_request * request)516*4882a593Smuzhiyun bool __i915_request_submit(struct i915_request *request)
517*4882a593Smuzhiyun {
518*4882a593Smuzhiyun struct intel_engine_cs *engine = request->engine;
519*4882a593Smuzhiyun bool result = false;
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun RQ_TRACE(request, "\n");
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun GEM_BUG_ON(!irqs_disabled());
524*4882a593Smuzhiyun lockdep_assert_held(&engine->active.lock);
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun /*
527*4882a593Smuzhiyun * With the advent of preempt-to-busy, we frequently encounter
528*4882a593Smuzhiyun * requests that we have unsubmitted from HW, but left running
529*4882a593Smuzhiyun * until the next ack and so have completed in the meantime. On
530*4882a593Smuzhiyun * resubmission of that completed request, we can skip
531*4882a593Smuzhiyun * updating the payload, and execlists can even skip submitting
532*4882a593Smuzhiyun * the request.
533*4882a593Smuzhiyun *
534*4882a593Smuzhiyun * We must remove the request from the caller's priority queue,
535*4882a593Smuzhiyun * and the caller must only call us when the request is in their
536*4882a593Smuzhiyun * priority queue, under the active.lock. This ensures that the
537*4882a593Smuzhiyun * request has *not* yet been retired and we can safely move
538*4882a593Smuzhiyun * the request into the engine->active.list where it will be
539*4882a593Smuzhiyun * dropped upon retiring. (Otherwise if resubmit a *retired*
540*4882a593Smuzhiyun * request, this would be a horrible use-after-free.)
541*4882a593Smuzhiyun */
542*4882a593Smuzhiyun if (i915_request_completed(request))
543*4882a593Smuzhiyun goto xfer;
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun if (unlikely(intel_context_is_closed(request->context) &&
546*4882a593Smuzhiyun !intel_engine_has_heartbeat(engine)))
547*4882a593Smuzhiyun intel_context_set_banned(request->context);
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun if (unlikely(intel_context_is_banned(request->context)))
550*4882a593Smuzhiyun i915_request_set_error_once(request, -EIO);
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun if (unlikely(fatal_error(request->fence.error)))
553*4882a593Smuzhiyun __i915_request_skip(request);
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun /*
556*4882a593Smuzhiyun * Are we using semaphores when the gpu is already saturated?
557*4882a593Smuzhiyun *
558*4882a593Smuzhiyun * Using semaphores incurs a cost in having the GPU poll a
559*4882a593Smuzhiyun * memory location, busywaiting for it to change. The continual
560*4882a593Smuzhiyun * memory reads can have a noticeable impact on the rest of the
561*4882a593Smuzhiyun * system with the extra bus traffic, stalling the cpu as it too
562*4882a593Smuzhiyun * tries to access memory across the bus (perf stat -e bus-cycles).
563*4882a593Smuzhiyun *
564*4882a593Smuzhiyun * If we installed a semaphore on this request and we only submit
565*4882a593Smuzhiyun * the request after the signaler completed, that indicates the
566*4882a593Smuzhiyun * system is overloaded and using semaphores at this time only
567*4882a593Smuzhiyun * increases the amount of work we are doing. If so, we disable
568*4882a593Smuzhiyun * further use of semaphores until we are idle again, whence we
569*4882a593Smuzhiyun * optimistically try again.
570*4882a593Smuzhiyun */
571*4882a593Smuzhiyun if (request->sched.semaphores &&
572*4882a593Smuzhiyun i915_sw_fence_signaled(&request->semaphore))
573*4882a593Smuzhiyun engine->saturated |= request->sched.semaphores;
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun engine->emit_fini_breadcrumb(request,
576*4882a593Smuzhiyun request->ring->vaddr + request->postfix);
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun trace_i915_request_execute(request);
579*4882a593Smuzhiyun engine->serial++;
580*4882a593Smuzhiyun result = true;
581*4882a593Smuzhiyun
582*4882a593Smuzhiyun xfer:
583*4882a593Smuzhiyun if (!test_and_set_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags)) {
584*4882a593Smuzhiyun list_move_tail(&request->sched.link, &engine->active.requests);
585*4882a593Smuzhiyun clear_bit(I915_FENCE_FLAG_PQUEUE, &request->fence.flags);
586*4882a593Smuzhiyun }
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun /*
589*4882a593Smuzhiyun * XXX Rollback bonded-execution on __i915_request_unsubmit()?
590*4882a593Smuzhiyun *
591*4882a593Smuzhiyun * In the future, perhaps when we have an active time-slicing scheduler,
592*4882a593Smuzhiyun * it will be interesting to unsubmit parallel execution and remove
593*4882a593Smuzhiyun * busywaits from the GPU until their master is restarted. This is
594*4882a593Smuzhiyun * quite hairy, we have to carefully rollback the fence and do a
595*4882a593Smuzhiyun * preempt-to-idle cycle on the target engine, all the while the
596*4882a593Smuzhiyun * master execute_cb may refire.
597*4882a593Smuzhiyun */
598*4882a593Smuzhiyun __notify_execute_cb_irq(request);
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun /* We may be recursing from the signal callback of another i915 fence */
601*4882a593Smuzhiyun if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
602*4882a593Smuzhiyun i915_request_enable_breadcrumb(request);
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun return result;
605*4882a593Smuzhiyun }
606*4882a593Smuzhiyun
i915_request_submit(struct i915_request * request)607*4882a593Smuzhiyun void i915_request_submit(struct i915_request *request)
608*4882a593Smuzhiyun {
609*4882a593Smuzhiyun struct intel_engine_cs *engine = request->engine;
610*4882a593Smuzhiyun unsigned long flags;
611*4882a593Smuzhiyun
612*4882a593Smuzhiyun /* Will be called from irq-context when using foreign fences. */
613*4882a593Smuzhiyun spin_lock_irqsave(&engine->active.lock, flags);
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun __i915_request_submit(request);
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun spin_unlock_irqrestore(&engine->active.lock, flags);
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun
__i915_request_unsubmit(struct i915_request * request)620*4882a593Smuzhiyun void __i915_request_unsubmit(struct i915_request *request)
621*4882a593Smuzhiyun {
622*4882a593Smuzhiyun struct intel_engine_cs *engine = request->engine;
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun /*
625*4882a593Smuzhiyun * Only unwind in reverse order, required so that the per-context list
626*4882a593Smuzhiyun * is kept in seqno/ring order.
627*4882a593Smuzhiyun */
628*4882a593Smuzhiyun RQ_TRACE(request, "\n");
629*4882a593Smuzhiyun
630*4882a593Smuzhiyun GEM_BUG_ON(!irqs_disabled());
631*4882a593Smuzhiyun lockdep_assert_held(&engine->active.lock);
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun /*
634*4882a593Smuzhiyun * Before we remove this breadcrumb from the signal list, we have
635*4882a593Smuzhiyun * to ensure that a concurrent dma_fence_enable_signaling() does not
636*4882a593Smuzhiyun * attach itself. We first mark the request as no longer active and
637*4882a593Smuzhiyun * make sure that is visible to other cores, and then remove the
638*4882a593Smuzhiyun * breadcrumb if attached.
639*4882a593Smuzhiyun */
640*4882a593Smuzhiyun GEM_BUG_ON(!test_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags));
641*4882a593Smuzhiyun clear_bit_unlock(I915_FENCE_FLAG_ACTIVE, &request->fence.flags);
642*4882a593Smuzhiyun if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
643*4882a593Smuzhiyun i915_request_cancel_breadcrumb(request);
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun /* We've already spun, don't charge on resubmitting. */
646*4882a593Smuzhiyun if (request->sched.semaphores && i915_request_started(request))
647*4882a593Smuzhiyun request->sched.semaphores = 0;
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun /*
650*4882a593Smuzhiyun * We don't need to wake_up any waiters on request->execute, they
651*4882a593Smuzhiyun * will get woken by any other event or us re-adding this request
652*4882a593Smuzhiyun * to the engine timeline (__i915_request_submit()). The waiters
653*4882a593Smuzhiyun * should be quite adapt at finding that the request now has a new
654*4882a593Smuzhiyun * global_seqno to the one they went to sleep on.
655*4882a593Smuzhiyun */
656*4882a593Smuzhiyun }
657*4882a593Smuzhiyun
i915_request_unsubmit(struct i915_request * request)658*4882a593Smuzhiyun void i915_request_unsubmit(struct i915_request *request)
659*4882a593Smuzhiyun {
660*4882a593Smuzhiyun struct intel_engine_cs *engine = request->engine;
661*4882a593Smuzhiyun unsigned long flags;
662*4882a593Smuzhiyun
663*4882a593Smuzhiyun /* Will be called from irq-context when using foreign fences. */
664*4882a593Smuzhiyun spin_lock_irqsave(&engine->active.lock, flags);
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun __i915_request_unsubmit(request);
667*4882a593Smuzhiyun
668*4882a593Smuzhiyun spin_unlock_irqrestore(&engine->active.lock, flags);
669*4882a593Smuzhiyun }
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun static int __i915_sw_fence_call
submit_notify(struct i915_sw_fence * fence,enum i915_sw_fence_notify state)672*4882a593Smuzhiyun submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
673*4882a593Smuzhiyun {
674*4882a593Smuzhiyun struct i915_request *request =
675*4882a593Smuzhiyun container_of(fence, typeof(*request), submit);
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun switch (state) {
678*4882a593Smuzhiyun case FENCE_COMPLETE:
679*4882a593Smuzhiyun trace_i915_request_submit(request);
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun if (unlikely(fence->error))
682*4882a593Smuzhiyun i915_request_set_error_once(request, fence->error);
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun /*
685*4882a593Smuzhiyun * We need to serialize use of the submit_request() callback
686*4882a593Smuzhiyun * with its hotplugging performed during an emergency
687*4882a593Smuzhiyun * i915_gem_set_wedged(). We use the RCU mechanism to mark the
688*4882a593Smuzhiyun * critical section in order to force i915_gem_set_wedged() to
689*4882a593Smuzhiyun * wait until the submit_request() is completed before
690*4882a593Smuzhiyun * proceeding.
691*4882a593Smuzhiyun */
692*4882a593Smuzhiyun rcu_read_lock();
693*4882a593Smuzhiyun request->engine->submit_request(request);
694*4882a593Smuzhiyun rcu_read_unlock();
695*4882a593Smuzhiyun break;
696*4882a593Smuzhiyun
697*4882a593Smuzhiyun case FENCE_FREE:
698*4882a593Smuzhiyun i915_request_put(request);
699*4882a593Smuzhiyun break;
700*4882a593Smuzhiyun }
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun return NOTIFY_DONE;
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun
705*4882a593Smuzhiyun static int __i915_sw_fence_call
semaphore_notify(struct i915_sw_fence * fence,enum i915_sw_fence_notify state)706*4882a593Smuzhiyun semaphore_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
707*4882a593Smuzhiyun {
708*4882a593Smuzhiyun struct i915_request *rq = container_of(fence, typeof(*rq), semaphore);
709*4882a593Smuzhiyun
710*4882a593Smuzhiyun switch (state) {
711*4882a593Smuzhiyun case FENCE_COMPLETE:
712*4882a593Smuzhiyun break;
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun case FENCE_FREE:
715*4882a593Smuzhiyun i915_request_put(rq);
716*4882a593Smuzhiyun break;
717*4882a593Smuzhiyun }
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun return NOTIFY_DONE;
720*4882a593Smuzhiyun }
721*4882a593Smuzhiyun
retire_requests(struct intel_timeline * tl)722*4882a593Smuzhiyun static void retire_requests(struct intel_timeline *tl)
723*4882a593Smuzhiyun {
724*4882a593Smuzhiyun struct i915_request *rq, *rn;
725*4882a593Smuzhiyun
726*4882a593Smuzhiyun list_for_each_entry_safe(rq, rn, &tl->requests, link)
727*4882a593Smuzhiyun if (!i915_request_retire(rq))
728*4882a593Smuzhiyun break;
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun
731*4882a593Smuzhiyun static noinline struct i915_request *
request_alloc_slow(struct intel_timeline * tl,struct i915_request ** rsvd,gfp_t gfp)732*4882a593Smuzhiyun request_alloc_slow(struct intel_timeline *tl,
733*4882a593Smuzhiyun struct i915_request **rsvd,
734*4882a593Smuzhiyun gfp_t gfp)
735*4882a593Smuzhiyun {
736*4882a593Smuzhiyun struct i915_request *rq;
737*4882a593Smuzhiyun
738*4882a593Smuzhiyun /* If we cannot wait, dip into our reserves */
739*4882a593Smuzhiyun if (!gfpflags_allow_blocking(gfp)) {
740*4882a593Smuzhiyun rq = xchg(rsvd, NULL);
741*4882a593Smuzhiyun if (!rq) /* Use the normal failure path for one final WARN */
742*4882a593Smuzhiyun goto out;
743*4882a593Smuzhiyun
744*4882a593Smuzhiyun return rq;
745*4882a593Smuzhiyun }
746*4882a593Smuzhiyun
747*4882a593Smuzhiyun if (list_empty(&tl->requests))
748*4882a593Smuzhiyun goto out;
749*4882a593Smuzhiyun
750*4882a593Smuzhiyun /* Move our oldest request to the slab-cache (if not in use!) */
751*4882a593Smuzhiyun rq = list_first_entry(&tl->requests, typeof(*rq), link);
752*4882a593Smuzhiyun i915_request_retire(rq);
753*4882a593Smuzhiyun
754*4882a593Smuzhiyun rq = kmem_cache_alloc(global.slab_requests,
755*4882a593Smuzhiyun gfp | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
756*4882a593Smuzhiyun if (rq)
757*4882a593Smuzhiyun return rq;
758*4882a593Smuzhiyun
759*4882a593Smuzhiyun /* Ratelimit ourselves to prevent oom from malicious clients */
760*4882a593Smuzhiyun rq = list_last_entry(&tl->requests, typeof(*rq), link);
761*4882a593Smuzhiyun cond_synchronize_rcu(rq->rcustate);
762*4882a593Smuzhiyun
763*4882a593Smuzhiyun /* Retire our old requests in the hope that we free some */
764*4882a593Smuzhiyun retire_requests(tl);
765*4882a593Smuzhiyun
766*4882a593Smuzhiyun out:
767*4882a593Smuzhiyun return kmem_cache_alloc(global.slab_requests, gfp);
768*4882a593Smuzhiyun }
769*4882a593Smuzhiyun
__i915_request_ctor(void * arg)770*4882a593Smuzhiyun static void __i915_request_ctor(void *arg)
771*4882a593Smuzhiyun {
772*4882a593Smuzhiyun struct i915_request *rq = arg;
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun spin_lock_init(&rq->lock);
775*4882a593Smuzhiyun i915_sched_node_init(&rq->sched);
776*4882a593Smuzhiyun i915_sw_fence_init(&rq->submit, submit_notify);
777*4882a593Smuzhiyun i915_sw_fence_init(&rq->semaphore, semaphore_notify);
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun rq->capture_list = NULL;
780*4882a593Smuzhiyun
781*4882a593Smuzhiyun init_llist_head(&rq->execute_cb);
782*4882a593Smuzhiyun }
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun struct i915_request *
__i915_request_create(struct intel_context * ce,gfp_t gfp)785*4882a593Smuzhiyun __i915_request_create(struct intel_context *ce, gfp_t gfp)
786*4882a593Smuzhiyun {
787*4882a593Smuzhiyun struct intel_timeline *tl = ce->timeline;
788*4882a593Smuzhiyun struct i915_request *rq;
789*4882a593Smuzhiyun u32 seqno;
790*4882a593Smuzhiyun int ret;
791*4882a593Smuzhiyun
792*4882a593Smuzhiyun might_sleep_if(gfpflags_allow_blocking(gfp));
793*4882a593Smuzhiyun
794*4882a593Smuzhiyun /* Check that the caller provided an already pinned context */
795*4882a593Smuzhiyun __intel_context_pin(ce);
796*4882a593Smuzhiyun
797*4882a593Smuzhiyun /*
798*4882a593Smuzhiyun * Beware: Dragons be flying overhead.
799*4882a593Smuzhiyun *
800*4882a593Smuzhiyun * We use RCU to look up requests in flight. The lookups may
801*4882a593Smuzhiyun * race with the request being allocated from the slab freelist.
802*4882a593Smuzhiyun * That is the request we are writing to here, may be in the process
803*4882a593Smuzhiyun * of being read by __i915_active_request_get_rcu(). As such,
804*4882a593Smuzhiyun * we have to be very careful when overwriting the contents. During
805*4882a593Smuzhiyun * the RCU lookup, we change chase the request->engine pointer,
806*4882a593Smuzhiyun * read the request->global_seqno and increment the reference count.
807*4882a593Smuzhiyun *
808*4882a593Smuzhiyun * The reference count is incremented atomically. If it is zero,
809*4882a593Smuzhiyun * the lookup knows the request is unallocated and complete. Otherwise,
810*4882a593Smuzhiyun * it is either still in use, or has been reallocated and reset
811*4882a593Smuzhiyun * with dma_fence_init(). This increment is safe for release as we
812*4882a593Smuzhiyun * check that the request we have a reference to and matches the active
813*4882a593Smuzhiyun * request.
814*4882a593Smuzhiyun *
815*4882a593Smuzhiyun * Before we increment the refcount, we chase the request->engine
816*4882a593Smuzhiyun * pointer. We must not call kmem_cache_zalloc() or else we set
817*4882a593Smuzhiyun * that pointer to NULL and cause a crash during the lookup. If
818*4882a593Smuzhiyun * we see the request is completed (based on the value of the
819*4882a593Smuzhiyun * old engine and seqno), the lookup is complete and reports NULL.
820*4882a593Smuzhiyun * If we decide the request is not completed (new engine or seqno),
821*4882a593Smuzhiyun * then we grab a reference and double check that it is still the
822*4882a593Smuzhiyun * active request - which it won't be and restart the lookup.
823*4882a593Smuzhiyun *
824*4882a593Smuzhiyun * Do not use kmem_cache_zalloc() here!
825*4882a593Smuzhiyun */
826*4882a593Smuzhiyun rq = kmem_cache_alloc(global.slab_requests,
827*4882a593Smuzhiyun gfp | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
828*4882a593Smuzhiyun if (unlikely(!rq)) {
829*4882a593Smuzhiyun rq = request_alloc_slow(tl, &ce->engine->request_pool, gfp);
830*4882a593Smuzhiyun if (!rq) {
831*4882a593Smuzhiyun ret = -ENOMEM;
832*4882a593Smuzhiyun goto err_unreserve;
833*4882a593Smuzhiyun }
834*4882a593Smuzhiyun }
835*4882a593Smuzhiyun
836*4882a593Smuzhiyun rq->context = ce;
837*4882a593Smuzhiyun rq->engine = ce->engine;
838*4882a593Smuzhiyun rq->ring = ce->ring;
839*4882a593Smuzhiyun rq->execution_mask = ce->engine->mask;
840*4882a593Smuzhiyun
841*4882a593Smuzhiyun ret = intel_timeline_get_seqno(tl, rq, &seqno);
842*4882a593Smuzhiyun if (ret)
843*4882a593Smuzhiyun goto err_free;
844*4882a593Smuzhiyun
845*4882a593Smuzhiyun dma_fence_init(&rq->fence, &i915_fence_ops, &rq->lock,
846*4882a593Smuzhiyun tl->fence_context, seqno);
847*4882a593Smuzhiyun
848*4882a593Smuzhiyun RCU_INIT_POINTER(rq->timeline, tl);
849*4882a593Smuzhiyun RCU_INIT_POINTER(rq->hwsp_cacheline, tl->hwsp_cacheline);
850*4882a593Smuzhiyun rq->hwsp_seqno = tl->hwsp_seqno;
851*4882a593Smuzhiyun GEM_BUG_ON(i915_request_completed(rq));
852*4882a593Smuzhiyun
853*4882a593Smuzhiyun rq->rcustate = get_state_synchronize_rcu(); /* acts as smp_mb() */
854*4882a593Smuzhiyun
855*4882a593Smuzhiyun /* We bump the ref for the fence chain */
856*4882a593Smuzhiyun i915_sw_fence_reinit(&i915_request_get(rq)->submit);
857*4882a593Smuzhiyun i915_sw_fence_reinit(&i915_request_get(rq)->semaphore);
858*4882a593Smuzhiyun
859*4882a593Smuzhiyun i915_sched_node_reinit(&rq->sched);
860*4882a593Smuzhiyun
861*4882a593Smuzhiyun /* No zalloc, everything must be cleared after use */
862*4882a593Smuzhiyun rq->batch = NULL;
863*4882a593Smuzhiyun GEM_BUG_ON(rq->capture_list);
864*4882a593Smuzhiyun GEM_BUG_ON(!llist_empty(&rq->execute_cb));
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun /*
867*4882a593Smuzhiyun * Reserve space in the ring buffer for all the commands required to
868*4882a593Smuzhiyun * eventually emit this request. This is to guarantee that the
869*4882a593Smuzhiyun * i915_request_add() call can't fail. Note that the reserve may need
870*4882a593Smuzhiyun * to be redone if the request is not actually submitted straight
871*4882a593Smuzhiyun * away, e.g. because a GPU scheduler has deferred it.
872*4882a593Smuzhiyun *
873*4882a593Smuzhiyun * Note that due to how we add reserved_space to intel_ring_begin()
874*4882a593Smuzhiyun * we need to double our request to ensure that if we need to wrap
875*4882a593Smuzhiyun * around inside i915_request_add() there is sufficient space at
876*4882a593Smuzhiyun * the beginning of the ring as well.
877*4882a593Smuzhiyun */
878*4882a593Smuzhiyun rq->reserved_space =
879*4882a593Smuzhiyun 2 * rq->engine->emit_fini_breadcrumb_dw * sizeof(u32);
880*4882a593Smuzhiyun
881*4882a593Smuzhiyun /*
882*4882a593Smuzhiyun * Record the position of the start of the request so that
883*4882a593Smuzhiyun * should we detect the updated seqno part-way through the
884*4882a593Smuzhiyun * GPU processing the request, we never over-estimate the
885*4882a593Smuzhiyun * position of the head.
886*4882a593Smuzhiyun */
887*4882a593Smuzhiyun rq->head = rq->ring->emit;
888*4882a593Smuzhiyun
889*4882a593Smuzhiyun ret = rq->engine->request_alloc(rq);
890*4882a593Smuzhiyun if (ret)
891*4882a593Smuzhiyun goto err_unwind;
892*4882a593Smuzhiyun
893*4882a593Smuzhiyun rq->infix = rq->ring->emit; /* end of header; start of user payload */
894*4882a593Smuzhiyun
895*4882a593Smuzhiyun intel_context_mark_active(ce);
896*4882a593Smuzhiyun list_add_tail_rcu(&rq->link, &tl->requests);
897*4882a593Smuzhiyun
898*4882a593Smuzhiyun return rq;
899*4882a593Smuzhiyun
900*4882a593Smuzhiyun err_unwind:
901*4882a593Smuzhiyun ce->ring->emit = rq->head;
902*4882a593Smuzhiyun
903*4882a593Smuzhiyun /* Make sure we didn't add ourselves to external state before freeing */
904*4882a593Smuzhiyun GEM_BUG_ON(!list_empty(&rq->sched.signalers_list));
905*4882a593Smuzhiyun GEM_BUG_ON(!list_empty(&rq->sched.waiters_list));
906*4882a593Smuzhiyun
907*4882a593Smuzhiyun err_free:
908*4882a593Smuzhiyun kmem_cache_free(global.slab_requests, rq);
909*4882a593Smuzhiyun err_unreserve:
910*4882a593Smuzhiyun intel_context_unpin(ce);
911*4882a593Smuzhiyun return ERR_PTR(ret);
912*4882a593Smuzhiyun }
913*4882a593Smuzhiyun
914*4882a593Smuzhiyun struct i915_request *
i915_request_create(struct intel_context * ce)915*4882a593Smuzhiyun i915_request_create(struct intel_context *ce)
916*4882a593Smuzhiyun {
917*4882a593Smuzhiyun struct i915_request *rq;
918*4882a593Smuzhiyun struct intel_timeline *tl;
919*4882a593Smuzhiyun
920*4882a593Smuzhiyun tl = intel_context_timeline_lock(ce);
921*4882a593Smuzhiyun if (IS_ERR(tl))
922*4882a593Smuzhiyun return ERR_CAST(tl);
923*4882a593Smuzhiyun
924*4882a593Smuzhiyun /* Move our oldest request to the slab-cache (if not in use!) */
925*4882a593Smuzhiyun rq = list_first_entry(&tl->requests, typeof(*rq), link);
926*4882a593Smuzhiyun if (!list_is_last(&rq->link, &tl->requests))
927*4882a593Smuzhiyun i915_request_retire(rq);
928*4882a593Smuzhiyun
929*4882a593Smuzhiyun intel_context_enter(ce);
930*4882a593Smuzhiyun rq = __i915_request_create(ce, GFP_KERNEL);
931*4882a593Smuzhiyun intel_context_exit(ce); /* active reference transferred to request */
932*4882a593Smuzhiyun if (IS_ERR(rq))
933*4882a593Smuzhiyun goto err_unlock;
934*4882a593Smuzhiyun
935*4882a593Smuzhiyun /* Check that we do not interrupt ourselves with a new request */
936*4882a593Smuzhiyun rq->cookie = lockdep_pin_lock(&tl->mutex);
937*4882a593Smuzhiyun
938*4882a593Smuzhiyun return rq;
939*4882a593Smuzhiyun
940*4882a593Smuzhiyun err_unlock:
941*4882a593Smuzhiyun intel_context_timeline_unlock(tl);
942*4882a593Smuzhiyun return rq;
943*4882a593Smuzhiyun }
944*4882a593Smuzhiyun
945*4882a593Smuzhiyun static int
i915_request_await_start(struct i915_request * rq,struct i915_request * signal)946*4882a593Smuzhiyun i915_request_await_start(struct i915_request *rq, struct i915_request *signal)
947*4882a593Smuzhiyun {
948*4882a593Smuzhiyun struct dma_fence *fence;
949*4882a593Smuzhiyun int err;
950*4882a593Smuzhiyun
951*4882a593Smuzhiyun if (i915_request_timeline(rq) == rcu_access_pointer(signal->timeline))
952*4882a593Smuzhiyun return 0;
953*4882a593Smuzhiyun
954*4882a593Smuzhiyun if (i915_request_started(signal))
955*4882a593Smuzhiyun return 0;
956*4882a593Smuzhiyun
957*4882a593Smuzhiyun fence = NULL;
958*4882a593Smuzhiyun rcu_read_lock();
959*4882a593Smuzhiyun spin_lock_irq(&signal->lock);
960*4882a593Smuzhiyun do {
961*4882a593Smuzhiyun struct list_head *pos = READ_ONCE(signal->link.prev);
962*4882a593Smuzhiyun struct i915_request *prev;
963*4882a593Smuzhiyun
964*4882a593Smuzhiyun /* Confirm signal has not been retired, the link is valid */
965*4882a593Smuzhiyun if (unlikely(i915_request_started(signal)))
966*4882a593Smuzhiyun break;
967*4882a593Smuzhiyun
968*4882a593Smuzhiyun /* Is signal the earliest request on its timeline? */
969*4882a593Smuzhiyun if (pos == &rcu_dereference(signal->timeline)->requests)
970*4882a593Smuzhiyun break;
971*4882a593Smuzhiyun
972*4882a593Smuzhiyun /*
973*4882a593Smuzhiyun * Peek at the request before us in the timeline. That
974*4882a593Smuzhiyun * request will only be valid before it is retired, so
975*4882a593Smuzhiyun * after acquiring a reference to it, confirm that it is
976*4882a593Smuzhiyun * still part of the signaler's timeline.
977*4882a593Smuzhiyun */
978*4882a593Smuzhiyun prev = list_entry(pos, typeof(*prev), link);
979*4882a593Smuzhiyun if (!i915_request_get_rcu(prev))
980*4882a593Smuzhiyun break;
981*4882a593Smuzhiyun
982*4882a593Smuzhiyun /* After the strong barrier, confirm prev is still attached */
983*4882a593Smuzhiyun if (unlikely(READ_ONCE(prev->link.next) != &signal->link)) {
984*4882a593Smuzhiyun i915_request_put(prev);
985*4882a593Smuzhiyun break;
986*4882a593Smuzhiyun }
987*4882a593Smuzhiyun
988*4882a593Smuzhiyun fence = &prev->fence;
989*4882a593Smuzhiyun } while (0);
990*4882a593Smuzhiyun spin_unlock_irq(&signal->lock);
991*4882a593Smuzhiyun rcu_read_unlock();
992*4882a593Smuzhiyun if (!fence)
993*4882a593Smuzhiyun return 0;
994*4882a593Smuzhiyun
995*4882a593Smuzhiyun err = 0;
996*4882a593Smuzhiyun if (!intel_timeline_sync_is_later(i915_request_timeline(rq), fence))
997*4882a593Smuzhiyun err = i915_sw_fence_await_dma_fence(&rq->submit,
998*4882a593Smuzhiyun fence, 0,
999*4882a593Smuzhiyun I915_FENCE_GFP);
1000*4882a593Smuzhiyun dma_fence_put(fence);
1001*4882a593Smuzhiyun
1002*4882a593Smuzhiyun return err;
1003*4882a593Smuzhiyun }
1004*4882a593Smuzhiyun
1005*4882a593Smuzhiyun static intel_engine_mask_t
already_busywaiting(struct i915_request * rq)1006*4882a593Smuzhiyun already_busywaiting(struct i915_request *rq)
1007*4882a593Smuzhiyun {
1008*4882a593Smuzhiyun /*
1009*4882a593Smuzhiyun * Polling a semaphore causes bus traffic, delaying other users of
1010*4882a593Smuzhiyun * both the GPU and CPU. We want to limit the impact on others,
1011*4882a593Smuzhiyun * while taking advantage of early submission to reduce GPU
1012*4882a593Smuzhiyun * latency. Therefore we restrict ourselves to not using more
1013*4882a593Smuzhiyun * than one semaphore from each source, and not using a semaphore
1014*4882a593Smuzhiyun * if we have detected the engine is saturated (i.e. would not be
1015*4882a593Smuzhiyun * submitted early and cause bus traffic reading an already passed
1016*4882a593Smuzhiyun * semaphore).
1017*4882a593Smuzhiyun *
1018*4882a593Smuzhiyun * See the are-we-too-late? check in __i915_request_submit().
1019*4882a593Smuzhiyun */
1020*4882a593Smuzhiyun return rq->sched.semaphores | READ_ONCE(rq->engine->saturated);
1021*4882a593Smuzhiyun }
1022*4882a593Smuzhiyun
1023*4882a593Smuzhiyun static int
__emit_semaphore_wait(struct i915_request * to,struct i915_request * from,u32 seqno)1024*4882a593Smuzhiyun __emit_semaphore_wait(struct i915_request *to,
1025*4882a593Smuzhiyun struct i915_request *from,
1026*4882a593Smuzhiyun u32 seqno)
1027*4882a593Smuzhiyun {
1028*4882a593Smuzhiyun const int has_token = INTEL_GEN(to->engine->i915) >= 12;
1029*4882a593Smuzhiyun u32 hwsp_offset;
1030*4882a593Smuzhiyun int len, err;
1031*4882a593Smuzhiyun u32 *cs;
1032*4882a593Smuzhiyun
1033*4882a593Smuzhiyun GEM_BUG_ON(INTEL_GEN(to->engine->i915) < 8);
1034*4882a593Smuzhiyun GEM_BUG_ON(i915_request_has_initial_breadcrumb(to));
1035*4882a593Smuzhiyun
1036*4882a593Smuzhiyun /* We need to pin the signaler's HWSP until we are finished reading. */
1037*4882a593Smuzhiyun err = intel_timeline_read_hwsp(from, to, &hwsp_offset);
1038*4882a593Smuzhiyun if (err)
1039*4882a593Smuzhiyun return err;
1040*4882a593Smuzhiyun
1041*4882a593Smuzhiyun len = 4;
1042*4882a593Smuzhiyun if (has_token)
1043*4882a593Smuzhiyun len += 2;
1044*4882a593Smuzhiyun
1045*4882a593Smuzhiyun cs = intel_ring_begin(to, len);
1046*4882a593Smuzhiyun if (IS_ERR(cs))
1047*4882a593Smuzhiyun return PTR_ERR(cs);
1048*4882a593Smuzhiyun
1049*4882a593Smuzhiyun /*
1050*4882a593Smuzhiyun * Using greater-than-or-equal here means we have to worry
1051*4882a593Smuzhiyun * about seqno wraparound. To side step that issue, we swap
1052*4882a593Smuzhiyun * the timeline HWSP upon wrapping, so that everyone listening
1053*4882a593Smuzhiyun * for the old (pre-wrap) values do not see the much smaller
1054*4882a593Smuzhiyun * (post-wrap) values than they were expecting (and so wait
1055*4882a593Smuzhiyun * forever).
1056*4882a593Smuzhiyun */
1057*4882a593Smuzhiyun *cs++ = (MI_SEMAPHORE_WAIT |
1058*4882a593Smuzhiyun MI_SEMAPHORE_GLOBAL_GTT |
1059*4882a593Smuzhiyun MI_SEMAPHORE_POLL |
1060*4882a593Smuzhiyun MI_SEMAPHORE_SAD_GTE_SDD) +
1061*4882a593Smuzhiyun has_token;
1062*4882a593Smuzhiyun *cs++ = seqno;
1063*4882a593Smuzhiyun *cs++ = hwsp_offset;
1064*4882a593Smuzhiyun *cs++ = 0;
1065*4882a593Smuzhiyun if (has_token) {
1066*4882a593Smuzhiyun *cs++ = 0;
1067*4882a593Smuzhiyun *cs++ = MI_NOOP;
1068*4882a593Smuzhiyun }
1069*4882a593Smuzhiyun
1070*4882a593Smuzhiyun intel_ring_advance(to, cs);
1071*4882a593Smuzhiyun return 0;
1072*4882a593Smuzhiyun }
1073*4882a593Smuzhiyun
1074*4882a593Smuzhiyun static int
emit_semaphore_wait(struct i915_request * to,struct i915_request * from,gfp_t gfp)1075*4882a593Smuzhiyun emit_semaphore_wait(struct i915_request *to,
1076*4882a593Smuzhiyun struct i915_request *from,
1077*4882a593Smuzhiyun gfp_t gfp)
1078*4882a593Smuzhiyun {
1079*4882a593Smuzhiyun const intel_engine_mask_t mask = READ_ONCE(from->engine)->mask;
1080*4882a593Smuzhiyun struct i915_sw_fence *wait = &to->submit;
1081*4882a593Smuzhiyun
1082*4882a593Smuzhiyun if (!intel_context_use_semaphores(to->context))
1083*4882a593Smuzhiyun goto await_fence;
1084*4882a593Smuzhiyun
1085*4882a593Smuzhiyun if (i915_request_has_initial_breadcrumb(to))
1086*4882a593Smuzhiyun goto await_fence;
1087*4882a593Smuzhiyun
1088*4882a593Smuzhiyun if (!rcu_access_pointer(from->hwsp_cacheline))
1089*4882a593Smuzhiyun goto await_fence;
1090*4882a593Smuzhiyun
1091*4882a593Smuzhiyun /*
1092*4882a593Smuzhiyun * If this or its dependents are waiting on an external fence
1093*4882a593Smuzhiyun * that may fail catastrophically, then we want to avoid using
1094*4882a593Smuzhiyun * sempahores as they bypass the fence signaling metadata, and we
1095*4882a593Smuzhiyun * lose the fence->error propagation.
1096*4882a593Smuzhiyun */
1097*4882a593Smuzhiyun if (from->sched.flags & I915_SCHED_HAS_EXTERNAL_CHAIN)
1098*4882a593Smuzhiyun goto await_fence;
1099*4882a593Smuzhiyun
1100*4882a593Smuzhiyun /* Just emit the first semaphore we see as request space is limited. */
1101*4882a593Smuzhiyun if (already_busywaiting(to) & mask)
1102*4882a593Smuzhiyun goto await_fence;
1103*4882a593Smuzhiyun
1104*4882a593Smuzhiyun if (i915_request_await_start(to, from) < 0)
1105*4882a593Smuzhiyun goto await_fence;
1106*4882a593Smuzhiyun
1107*4882a593Smuzhiyun /* Only submit our spinner after the signaler is running! */
1108*4882a593Smuzhiyun if (__await_execution(to, from, NULL, gfp))
1109*4882a593Smuzhiyun goto await_fence;
1110*4882a593Smuzhiyun
1111*4882a593Smuzhiyun if (__emit_semaphore_wait(to, from, from->fence.seqno))
1112*4882a593Smuzhiyun goto await_fence;
1113*4882a593Smuzhiyun
1114*4882a593Smuzhiyun to->sched.semaphores |= mask;
1115*4882a593Smuzhiyun wait = &to->semaphore;
1116*4882a593Smuzhiyun
1117*4882a593Smuzhiyun await_fence:
1118*4882a593Smuzhiyun return i915_sw_fence_await_dma_fence(wait,
1119*4882a593Smuzhiyun &from->fence, 0,
1120*4882a593Smuzhiyun I915_FENCE_GFP);
1121*4882a593Smuzhiyun }
1122*4882a593Smuzhiyun
intel_timeline_sync_has_start(struct intel_timeline * tl,struct dma_fence * fence)1123*4882a593Smuzhiyun static bool intel_timeline_sync_has_start(struct intel_timeline *tl,
1124*4882a593Smuzhiyun struct dma_fence *fence)
1125*4882a593Smuzhiyun {
1126*4882a593Smuzhiyun return __intel_timeline_sync_is_later(tl,
1127*4882a593Smuzhiyun fence->context,
1128*4882a593Smuzhiyun fence->seqno - 1);
1129*4882a593Smuzhiyun }
1130*4882a593Smuzhiyun
intel_timeline_sync_set_start(struct intel_timeline * tl,const struct dma_fence * fence)1131*4882a593Smuzhiyun static int intel_timeline_sync_set_start(struct intel_timeline *tl,
1132*4882a593Smuzhiyun const struct dma_fence *fence)
1133*4882a593Smuzhiyun {
1134*4882a593Smuzhiyun return __intel_timeline_sync_set(tl, fence->context, fence->seqno - 1);
1135*4882a593Smuzhiyun }
1136*4882a593Smuzhiyun
1137*4882a593Smuzhiyun static int
__i915_request_await_execution(struct i915_request * to,struct i915_request * from,void (* hook)(struct i915_request * rq,struct dma_fence * signal))1138*4882a593Smuzhiyun __i915_request_await_execution(struct i915_request *to,
1139*4882a593Smuzhiyun struct i915_request *from,
1140*4882a593Smuzhiyun void (*hook)(struct i915_request *rq,
1141*4882a593Smuzhiyun struct dma_fence *signal))
1142*4882a593Smuzhiyun {
1143*4882a593Smuzhiyun int err;
1144*4882a593Smuzhiyun
1145*4882a593Smuzhiyun GEM_BUG_ON(intel_context_is_barrier(from->context));
1146*4882a593Smuzhiyun
1147*4882a593Smuzhiyun /* Submit both requests at the same time */
1148*4882a593Smuzhiyun err = __await_execution(to, from, hook, I915_FENCE_GFP);
1149*4882a593Smuzhiyun if (err)
1150*4882a593Smuzhiyun return err;
1151*4882a593Smuzhiyun
1152*4882a593Smuzhiyun /* Squash repeated depenendices to the same timelines */
1153*4882a593Smuzhiyun if (intel_timeline_sync_has_start(i915_request_timeline(to),
1154*4882a593Smuzhiyun &from->fence))
1155*4882a593Smuzhiyun return 0;
1156*4882a593Smuzhiyun
1157*4882a593Smuzhiyun /*
1158*4882a593Smuzhiyun * Wait until the start of this request.
1159*4882a593Smuzhiyun *
1160*4882a593Smuzhiyun * The execution cb fires when we submit the request to HW. But in
1161*4882a593Smuzhiyun * many cases this may be long before the request itself is ready to
1162*4882a593Smuzhiyun * run (consider that we submit 2 requests for the same context, where
1163*4882a593Smuzhiyun * the request of interest is behind an indefinite spinner). So we hook
1164*4882a593Smuzhiyun * up to both to reduce our queues and keep the execution lag minimised
1165*4882a593Smuzhiyun * in the worst case, though we hope that the await_start is elided.
1166*4882a593Smuzhiyun */
1167*4882a593Smuzhiyun err = i915_request_await_start(to, from);
1168*4882a593Smuzhiyun if (err < 0)
1169*4882a593Smuzhiyun return err;
1170*4882a593Smuzhiyun
1171*4882a593Smuzhiyun /*
1172*4882a593Smuzhiyun * Ensure both start together [after all semaphores in signal]
1173*4882a593Smuzhiyun *
1174*4882a593Smuzhiyun * Now that we are queued to the HW at roughly the same time (thanks
1175*4882a593Smuzhiyun * to the execute cb) and are ready to run at roughly the same time
1176*4882a593Smuzhiyun * (thanks to the await start), our signaler may still be indefinitely
1177*4882a593Smuzhiyun * delayed by waiting on a semaphore from a remote engine. If our
1178*4882a593Smuzhiyun * signaler depends on a semaphore, so indirectly do we, and we do not
1179*4882a593Smuzhiyun * want to start our payload until our signaler also starts theirs.
1180*4882a593Smuzhiyun * So we wait.
1181*4882a593Smuzhiyun *
1182*4882a593Smuzhiyun * However, there is also a second condition for which we need to wait
1183*4882a593Smuzhiyun * for the precise start of the signaler. Consider that the signaler
1184*4882a593Smuzhiyun * was submitted in a chain of requests following another context
1185*4882a593Smuzhiyun * (with just an ordinary intra-engine fence dependency between the
1186*4882a593Smuzhiyun * two). In this case the signaler is queued to HW, but not for
1187*4882a593Smuzhiyun * immediate execution, and so we must wait until it reaches the
1188*4882a593Smuzhiyun * active slot.
1189*4882a593Smuzhiyun */
1190*4882a593Smuzhiyun if (intel_engine_has_semaphores(to->engine) &&
1191*4882a593Smuzhiyun !i915_request_has_initial_breadcrumb(to)) {
1192*4882a593Smuzhiyun err = __emit_semaphore_wait(to, from, from->fence.seqno - 1);
1193*4882a593Smuzhiyun if (err < 0)
1194*4882a593Smuzhiyun return err;
1195*4882a593Smuzhiyun }
1196*4882a593Smuzhiyun
1197*4882a593Smuzhiyun /* Couple the dependency tree for PI on this exposed to->fence */
1198*4882a593Smuzhiyun if (to->engine->schedule) {
1199*4882a593Smuzhiyun err = i915_sched_node_add_dependency(&to->sched,
1200*4882a593Smuzhiyun &from->sched,
1201*4882a593Smuzhiyun I915_DEPENDENCY_WEAK);
1202*4882a593Smuzhiyun if (err < 0)
1203*4882a593Smuzhiyun return err;
1204*4882a593Smuzhiyun }
1205*4882a593Smuzhiyun
1206*4882a593Smuzhiyun return intel_timeline_sync_set_start(i915_request_timeline(to),
1207*4882a593Smuzhiyun &from->fence);
1208*4882a593Smuzhiyun }
1209*4882a593Smuzhiyun
mark_external(struct i915_request * rq)1210*4882a593Smuzhiyun static void mark_external(struct i915_request *rq)
1211*4882a593Smuzhiyun {
1212*4882a593Smuzhiyun /*
1213*4882a593Smuzhiyun * The downside of using semaphores is that we lose metadata passing
1214*4882a593Smuzhiyun * along the signaling chain. This is particularly nasty when we
1215*4882a593Smuzhiyun * need to pass along a fatal error such as EFAULT or EDEADLK. For
1216*4882a593Smuzhiyun * fatal errors we want to scrub the request before it is executed,
1217*4882a593Smuzhiyun * which means that we cannot preload the request onto HW and have
1218*4882a593Smuzhiyun * it wait upon a semaphore.
1219*4882a593Smuzhiyun */
1220*4882a593Smuzhiyun rq->sched.flags |= I915_SCHED_HAS_EXTERNAL_CHAIN;
1221*4882a593Smuzhiyun }
1222*4882a593Smuzhiyun
1223*4882a593Smuzhiyun static int
__i915_request_await_external(struct i915_request * rq,struct dma_fence * fence)1224*4882a593Smuzhiyun __i915_request_await_external(struct i915_request *rq, struct dma_fence *fence)
1225*4882a593Smuzhiyun {
1226*4882a593Smuzhiyun mark_external(rq);
1227*4882a593Smuzhiyun return i915_sw_fence_await_dma_fence(&rq->submit, fence,
1228*4882a593Smuzhiyun i915_fence_context_timeout(rq->engine->i915,
1229*4882a593Smuzhiyun fence->context),
1230*4882a593Smuzhiyun I915_FENCE_GFP);
1231*4882a593Smuzhiyun }
1232*4882a593Smuzhiyun
1233*4882a593Smuzhiyun static int
i915_request_await_external(struct i915_request * rq,struct dma_fence * fence)1234*4882a593Smuzhiyun i915_request_await_external(struct i915_request *rq, struct dma_fence *fence)
1235*4882a593Smuzhiyun {
1236*4882a593Smuzhiyun struct dma_fence *iter;
1237*4882a593Smuzhiyun int err = 0;
1238*4882a593Smuzhiyun
1239*4882a593Smuzhiyun if (!to_dma_fence_chain(fence))
1240*4882a593Smuzhiyun return __i915_request_await_external(rq, fence);
1241*4882a593Smuzhiyun
1242*4882a593Smuzhiyun dma_fence_chain_for_each(iter, fence) {
1243*4882a593Smuzhiyun struct dma_fence_chain *chain = to_dma_fence_chain(iter);
1244*4882a593Smuzhiyun
1245*4882a593Smuzhiyun if (!dma_fence_is_i915(chain->fence)) {
1246*4882a593Smuzhiyun err = __i915_request_await_external(rq, iter);
1247*4882a593Smuzhiyun break;
1248*4882a593Smuzhiyun }
1249*4882a593Smuzhiyun
1250*4882a593Smuzhiyun err = i915_request_await_dma_fence(rq, chain->fence);
1251*4882a593Smuzhiyun if (err < 0)
1252*4882a593Smuzhiyun break;
1253*4882a593Smuzhiyun }
1254*4882a593Smuzhiyun
1255*4882a593Smuzhiyun dma_fence_put(iter);
1256*4882a593Smuzhiyun return err;
1257*4882a593Smuzhiyun }
1258*4882a593Smuzhiyun
1259*4882a593Smuzhiyun int
i915_request_await_execution(struct i915_request * rq,struct dma_fence * fence,void (* hook)(struct i915_request * rq,struct dma_fence * signal))1260*4882a593Smuzhiyun i915_request_await_execution(struct i915_request *rq,
1261*4882a593Smuzhiyun struct dma_fence *fence,
1262*4882a593Smuzhiyun void (*hook)(struct i915_request *rq,
1263*4882a593Smuzhiyun struct dma_fence *signal))
1264*4882a593Smuzhiyun {
1265*4882a593Smuzhiyun struct dma_fence **child = &fence;
1266*4882a593Smuzhiyun unsigned int nchild = 1;
1267*4882a593Smuzhiyun int ret;
1268*4882a593Smuzhiyun
1269*4882a593Smuzhiyun if (dma_fence_is_array(fence)) {
1270*4882a593Smuzhiyun struct dma_fence_array *array = to_dma_fence_array(fence);
1271*4882a593Smuzhiyun
1272*4882a593Smuzhiyun /* XXX Error for signal-on-any fence arrays */
1273*4882a593Smuzhiyun
1274*4882a593Smuzhiyun child = array->fences;
1275*4882a593Smuzhiyun nchild = array->num_fences;
1276*4882a593Smuzhiyun GEM_BUG_ON(!nchild);
1277*4882a593Smuzhiyun }
1278*4882a593Smuzhiyun
1279*4882a593Smuzhiyun do {
1280*4882a593Smuzhiyun fence = *child++;
1281*4882a593Smuzhiyun if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
1282*4882a593Smuzhiyun continue;
1283*4882a593Smuzhiyun
1284*4882a593Smuzhiyun if (fence->context == rq->fence.context)
1285*4882a593Smuzhiyun continue;
1286*4882a593Smuzhiyun
1287*4882a593Smuzhiyun /*
1288*4882a593Smuzhiyun * We don't squash repeated fence dependencies here as we
1289*4882a593Smuzhiyun * want to run our callback in all cases.
1290*4882a593Smuzhiyun */
1291*4882a593Smuzhiyun
1292*4882a593Smuzhiyun if (dma_fence_is_i915(fence))
1293*4882a593Smuzhiyun ret = __i915_request_await_execution(rq,
1294*4882a593Smuzhiyun to_request(fence),
1295*4882a593Smuzhiyun hook);
1296*4882a593Smuzhiyun else
1297*4882a593Smuzhiyun ret = i915_request_await_external(rq, fence);
1298*4882a593Smuzhiyun if (ret < 0)
1299*4882a593Smuzhiyun return ret;
1300*4882a593Smuzhiyun } while (--nchild);
1301*4882a593Smuzhiyun
1302*4882a593Smuzhiyun return 0;
1303*4882a593Smuzhiyun }
1304*4882a593Smuzhiyun
1305*4882a593Smuzhiyun static int
await_request_submit(struct i915_request * to,struct i915_request * from)1306*4882a593Smuzhiyun await_request_submit(struct i915_request *to, struct i915_request *from)
1307*4882a593Smuzhiyun {
1308*4882a593Smuzhiyun /*
1309*4882a593Smuzhiyun * If we are waiting on a virtual engine, then it may be
1310*4882a593Smuzhiyun * constrained to execute on a single engine *prior* to submission.
1311*4882a593Smuzhiyun * When it is submitted, it will be first submitted to the virtual
1312*4882a593Smuzhiyun * engine and then passed to the physical engine. We cannot allow
1313*4882a593Smuzhiyun * the waiter to be submitted immediately to the physical engine
1314*4882a593Smuzhiyun * as it may then bypass the virtual request.
1315*4882a593Smuzhiyun */
1316*4882a593Smuzhiyun if (to->engine == READ_ONCE(from->engine))
1317*4882a593Smuzhiyun return i915_sw_fence_await_sw_fence_gfp(&to->submit,
1318*4882a593Smuzhiyun &from->submit,
1319*4882a593Smuzhiyun I915_FENCE_GFP);
1320*4882a593Smuzhiyun else
1321*4882a593Smuzhiyun return __i915_request_await_execution(to, from, NULL);
1322*4882a593Smuzhiyun }
1323*4882a593Smuzhiyun
1324*4882a593Smuzhiyun static int
i915_request_await_request(struct i915_request * to,struct i915_request * from)1325*4882a593Smuzhiyun i915_request_await_request(struct i915_request *to, struct i915_request *from)
1326*4882a593Smuzhiyun {
1327*4882a593Smuzhiyun int ret;
1328*4882a593Smuzhiyun
1329*4882a593Smuzhiyun GEM_BUG_ON(to == from);
1330*4882a593Smuzhiyun GEM_BUG_ON(to->timeline == from->timeline);
1331*4882a593Smuzhiyun
1332*4882a593Smuzhiyun if (i915_request_completed(from)) {
1333*4882a593Smuzhiyun i915_sw_fence_set_error_once(&to->submit, from->fence.error);
1334*4882a593Smuzhiyun return 0;
1335*4882a593Smuzhiyun }
1336*4882a593Smuzhiyun
1337*4882a593Smuzhiyun if (to->engine->schedule) {
1338*4882a593Smuzhiyun ret = i915_sched_node_add_dependency(&to->sched,
1339*4882a593Smuzhiyun &from->sched,
1340*4882a593Smuzhiyun I915_DEPENDENCY_EXTERNAL);
1341*4882a593Smuzhiyun if (ret < 0)
1342*4882a593Smuzhiyun return ret;
1343*4882a593Smuzhiyun }
1344*4882a593Smuzhiyun
1345*4882a593Smuzhiyun if (is_power_of_2(to->execution_mask | READ_ONCE(from->execution_mask)))
1346*4882a593Smuzhiyun ret = await_request_submit(to, from);
1347*4882a593Smuzhiyun else
1348*4882a593Smuzhiyun ret = emit_semaphore_wait(to, from, I915_FENCE_GFP);
1349*4882a593Smuzhiyun if (ret < 0)
1350*4882a593Smuzhiyun return ret;
1351*4882a593Smuzhiyun
1352*4882a593Smuzhiyun return 0;
1353*4882a593Smuzhiyun }
1354*4882a593Smuzhiyun
1355*4882a593Smuzhiyun int
i915_request_await_dma_fence(struct i915_request * rq,struct dma_fence * fence)1356*4882a593Smuzhiyun i915_request_await_dma_fence(struct i915_request *rq, struct dma_fence *fence)
1357*4882a593Smuzhiyun {
1358*4882a593Smuzhiyun struct dma_fence **child = &fence;
1359*4882a593Smuzhiyun unsigned int nchild = 1;
1360*4882a593Smuzhiyun int ret;
1361*4882a593Smuzhiyun
1362*4882a593Smuzhiyun /*
1363*4882a593Smuzhiyun * Note that if the fence-array was created in signal-on-any mode,
1364*4882a593Smuzhiyun * we should *not* decompose it into its individual fences. However,
1365*4882a593Smuzhiyun * we don't currently store which mode the fence-array is operating
1366*4882a593Smuzhiyun * in. Fortunately, the only user of signal-on-any is private to
1367*4882a593Smuzhiyun * amdgpu and we should not see any incoming fence-array from
1368*4882a593Smuzhiyun * sync-file being in signal-on-any mode.
1369*4882a593Smuzhiyun */
1370*4882a593Smuzhiyun if (dma_fence_is_array(fence)) {
1371*4882a593Smuzhiyun struct dma_fence_array *array = to_dma_fence_array(fence);
1372*4882a593Smuzhiyun
1373*4882a593Smuzhiyun child = array->fences;
1374*4882a593Smuzhiyun nchild = array->num_fences;
1375*4882a593Smuzhiyun GEM_BUG_ON(!nchild);
1376*4882a593Smuzhiyun }
1377*4882a593Smuzhiyun
1378*4882a593Smuzhiyun do {
1379*4882a593Smuzhiyun fence = *child++;
1380*4882a593Smuzhiyun if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
1381*4882a593Smuzhiyun continue;
1382*4882a593Smuzhiyun
1383*4882a593Smuzhiyun /*
1384*4882a593Smuzhiyun * Requests on the same timeline are explicitly ordered, along
1385*4882a593Smuzhiyun * with their dependencies, by i915_request_add() which ensures
1386*4882a593Smuzhiyun * that requests are submitted in-order through each ring.
1387*4882a593Smuzhiyun */
1388*4882a593Smuzhiyun if (fence->context == rq->fence.context)
1389*4882a593Smuzhiyun continue;
1390*4882a593Smuzhiyun
1391*4882a593Smuzhiyun /* Squash repeated waits to the same timelines */
1392*4882a593Smuzhiyun if (fence->context &&
1393*4882a593Smuzhiyun intel_timeline_sync_is_later(i915_request_timeline(rq),
1394*4882a593Smuzhiyun fence))
1395*4882a593Smuzhiyun continue;
1396*4882a593Smuzhiyun
1397*4882a593Smuzhiyun if (dma_fence_is_i915(fence))
1398*4882a593Smuzhiyun ret = i915_request_await_request(rq, to_request(fence));
1399*4882a593Smuzhiyun else
1400*4882a593Smuzhiyun ret = i915_request_await_external(rq, fence);
1401*4882a593Smuzhiyun if (ret < 0)
1402*4882a593Smuzhiyun return ret;
1403*4882a593Smuzhiyun
1404*4882a593Smuzhiyun /* Record the latest fence used against each timeline */
1405*4882a593Smuzhiyun if (fence->context)
1406*4882a593Smuzhiyun intel_timeline_sync_set(i915_request_timeline(rq),
1407*4882a593Smuzhiyun fence);
1408*4882a593Smuzhiyun } while (--nchild);
1409*4882a593Smuzhiyun
1410*4882a593Smuzhiyun return 0;
1411*4882a593Smuzhiyun }
1412*4882a593Smuzhiyun
1413*4882a593Smuzhiyun /**
1414*4882a593Smuzhiyun * i915_request_await_object - set this request to (async) wait upon a bo
1415*4882a593Smuzhiyun * @to: request we are wishing to use
1416*4882a593Smuzhiyun * @obj: object which may be in use on another ring.
1417*4882a593Smuzhiyun * @write: whether the wait is on behalf of a writer
1418*4882a593Smuzhiyun *
1419*4882a593Smuzhiyun * This code is meant to abstract object synchronization with the GPU.
1420*4882a593Smuzhiyun * Conceptually we serialise writes between engines inside the GPU.
1421*4882a593Smuzhiyun * We only allow one engine to write into a buffer at any time, but
1422*4882a593Smuzhiyun * multiple readers. To ensure each has a coherent view of memory, we must:
1423*4882a593Smuzhiyun *
1424*4882a593Smuzhiyun * - If there is an outstanding write request to the object, the new
1425*4882a593Smuzhiyun * request must wait for it to complete (either CPU or in hw, requests
1426*4882a593Smuzhiyun * on the same ring will be naturally ordered).
1427*4882a593Smuzhiyun *
1428*4882a593Smuzhiyun * - If we are a write request (pending_write_domain is set), the new
1429*4882a593Smuzhiyun * request must wait for outstanding read requests to complete.
1430*4882a593Smuzhiyun *
1431*4882a593Smuzhiyun * Returns 0 if successful, else propagates up the lower layer error.
1432*4882a593Smuzhiyun */
1433*4882a593Smuzhiyun int
i915_request_await_object(struct i915_request * to,struct drm_i915_gem_object * obj,bool write)1434*4882a593Smuzhiyun i915_request_await_object(struct i915_request *to,
1435*4882a593Smuzhiyun struct drm_i915_gem_object *obj,
1436*4882a593Smuzhiyun bool write)
1437*4882a593Smuzhiyun {
1438*4882a593Smuzhiyun struct dma_fence *excl;
1439*4882a593Smuzhiyun int ret = 0;
1440*4882a593Smuzhiyun
1441*4882a593Smuzhiyun if (write) {
1442*4882a593Smuzhiyun struct dma_fence **shared;
1443*4882a593Smuzhiyun unsigned int count, i;
1444*4882a593Smuzhiyun
1445*4882a593Smuzhiyun ret = dma_resv_get_fences_rcu(obj->base.resv,
1446*4882a593Smuzhiyun &excl, &count, &shared);
1447*4882a593Smuzhiyun if (ret)
1448*4882a593Smuzhiyun return ret;
1449*4882a593Smuzhiyun
1450*4882a593Smuzhiyun for (i = 0; i < count; i++) {
1451*4882a593Smuzhiyun ret = i915_request_await_dma_fence(to, shared[i]);
1452*4882a593Smuzhiyun if (ret)
1453*4882a593Smuzhiyun break;
1454*4882a593Smuzhiyun
1455*4882a593Smuzhiyun dma_fence_put(shared[i]);
1456*4882a593Smuzhiyun }
1457*4882a593Smuzhiyun
1458*4882a593Smuzhiyun for (; i < count; i++)
1459*4882a593Smuzhiyun dma_fence_put(shared[i]);
1460*4882a593Smuzhiyun kfree(shared);
1461*4882a593Smuzhiyun } else {
1462*4882a593Smuzhiyun excl = dma_resv_get_excl_rcu(obj->base.resv);
1463*4882a593Smuzhiyun }
1464*4882a593Smuzhiyun
1465*4882a593Smuzhiyun if (excl) {
1466*4882a593Smuzhiyun if (ret == 0)
1467*4882a593Smuzhiyun ret = i915_request_await_dma_fence(to, excl);
1468*4882a593Smuzhiyun
1469*4882a593Smuzhiyun dma_fence_put(excl);
1470*4882a593Smuzhiyun }
1471*4882a593Smuzhiyun
1472*4882a593Smuzhiyun return ret;
1473*4882a593Smuzhiyun }
1474*4882a593Smuzhiyun
1475*4882a593Smuzhiyun static struct i915_request *
__i915_request_add_to_timeline(struct i915_request * rq)1476*4882a593Smuzhiyun __i915_request_add_to_timeline(struct i915_request *rq)
1477*4882a593Smuzhiyun {
1478*4882a593Smuzhiyun struct intel_timeline *timeline = i915_request_timeline(rq);
1479*4882a593Smuzhiyun struct i915_request *prev;
1480*4882a593Smuzhiyun
1481*4882a593Smuzhiyun /*
1482*4882a593Smuzhiyun * Dependency tracking and request ordering along the timeline
1483*4882a593Smuzhiyun * is special cased so that we can eliminate redundant ordering
1484*4882a593Smuzhiyun * operations while building the request (we know that the timeline
1485*4882a593Smuzhiyun * itself is ordered, and here we guarantee it).
1486*4882a593Smuzhiyun *
1487*4882a593Smuzhiyun * As we know we will need to emit tracking along the timeline,
1488*4882a593Smuzhiyun * we embed the hooks into our request struct -- at the cost of
1489*4882a593Smuzhiyun * having to have specialised no-allocation interfaces (which will
1490*4882a593Smuzhiyun * be beneficial elsewhere).
1491*4882a593Smuzhiyun *
1492*4882a593Smuzhiyun * A second benefit to open-coding i915_request_await_request is
1493*4882a593Smuzhiyun * that we can apply a slight variant of the rules specialised
1494*4882a593Smuzhiyun * for timelines that jump between engines (such as virtual engines).
1495*4882a593Smuzhiyun * If we consider the case of virtual engine, we must emit a dma-fence
1496*4882a593Smuzhiyun * to prevent scheduling of the second request until the first is
1497*4882a593Smuzhiyun * complete (to maximise our greedy late load balancing) and this
1498*4882a593Smuzhiyun * precludes optimising to use semaphores serialisation of a single
1499*4882a593Smuzhiyun * timeline across engines.
1500*4882a593Smuzhiyun */
1501*4882a593Smuzhiyun prev = to_request(__i915_active_fence_set(&timeline->last_request,
1502*4882a593Smuzhiyun &rq->fence));
1503*4882a593Smuzhiyun if (prev && !i915_request_completed(prev)) {
1504*4882a593Smuzhiyun /*
1505*4882a593Smuzhiyun * The requests are supposed to be kept in order. However,
1506*4882a593Smuzhiyun * we need to be wary in case the timeline->last_request
1507*4882a593Smuzhiyun * is used as a barrier for external modification to this
1508*4882a593Smuzhiyun * context.
1509*4882a593Smuzhiyun */
1510*4882a593Smuzhiyun GEM_BUG_ON(prev->context == rq->context &&
1511*4882a593Smuzhiyun i915_seqno_passed(prev->fence.seqno,
1512*4882a593Smuzhiyun rq->fence.seqno));
1513*4882a593Smuzhiyun
1514*4882a593Smuzhiyun if (is_power_of_2(READ_ONCE(prev->engine)->mask | rq->engine->mask))
1515*4882a593Smuzhiyun i915_sw_fence_await_sw_fence(&rq->submit,
1516*4882a593Smuzhiyun &prev->submit,
1517*4882a593Smuzhiyun &rq->submitq);
1518*4882a593Smuzhiyun else
1519*4882a593Smuzhiyun __i915_sw_fence_await_dma_fence(&rq->submit,
1520*4882a593Smuzhiyun &prev->fence,
1521*4882a593Smuzhiyun &rq->dmaq);
1522*4882a593Smuzhiyun if (rq->engine->schedule)
1523*4882a593Smuzhiyun __i915_sched_node_add_dependency(&rq->sched,
1524*4882a593Smuzhiyun &prev->sched,
1525*4882a593Smuzhiyun &rq->dep,
1526*4882a593Smuzhiyun 0);
1527*4882a593Smuzhiyun }
1528*4882a593Smuzhiyun
1529*4882a593Smuzhiyun /*
1530*4882a593Smuzhiyun * Make sure that no request gazumped us - if it was allocated after
1531*4882a593Smuzhiyun * our i915_request_alloc() and called __i915_request_add() before
1532*4882a593Smuzhiyun * us, the timeline will hold its seqno which is later than ours.
1533*4882a593Smuzhiyun */
1534*4882a593Smuzhiyun GEM_BUG_ON(timeline->seqno != rq->fence.seqno);
1535*4882a593Smuzhiyun
1536*4882a593Smuzhiyun return prev;
1537*4882a593Smuzhiyun }
1538*4882a593Smuzhiyun
1539*4882a593Smuzhiyun /*
1540*4882a593Smuzhiyun * NB: This function is not allowed to fail. Doing so would mean the the
1541*4882a593Smuzhiyun * request is not being tracked for completion but the work itself is
1542*4882a593Smuzhiyun * going to happen on the hardware. This would be a Bad Thing(tm).
1543*4882a593Smuzhiyun */
__i915_request_commit(struct i915_request * rq)1544*4882a593Smuzhiyun struct i915_request *__i915_request_commit(struct i915_request *rq)
1545*4882a593Smuzhiyun {
1546*4882a593Smuzhiyun struct intel_engine_cs *engine = rq->engine;
1547*4882a593Smuzhiyun struct intel_ring *ring = rq->ring;
1548*4882a593Smuzhiyun u32 *cs;
1549*4882a593Smuzhiyun
1550*4882a593Smuzhiyun RQ_TRACE(rq, "\n");
1551*4882a593Smuzhiyun
1552*4882a593Smuzhiyun /*
1553*4882a593Smuzhiyun * To ensure that this call will not fail, space for its emissions
1554*4882a593Smuzhiyun * should already have been reserved in the ring buffer. Let the ring
1555*4882a593Smuzhiyun * know that it is time to use that space up.
1556*4882a593Smuzhiyun */
1557*4882a593Smuzhiyun GEM_BUG_ON(rq->reserved_space > ring->space);
1558*4882a593Smuzhiyun rq->reserved_space = 0;
1559*4882a593Smuzhiyun rq->emitted_jiffies = jiffies;
1560*4882a593Smuzhiyun
1561*4882a593Smuzhiyun /*
1562*4882a593Smuzhiyun * Record the position of the start of the breadcrumb so that
1563*4882a593Smuzhiyun * should we detect the updated seqno part-way through the
1564*4882a593Smuzhiyun * GPU processing the request, we never over-estimate the
1565*4882a593Smuzhiyun * position of the ring's HEAD.
1566*4882a593Smuzhiyun */
1567*4882a593Smuzhiyun cs = intel_ring_begin(rq, engine->emit_fini_breadcrumb_dw);
1568*4882a593Smuzhiyun GEM_BUG_ON(IS_ERR(cs));
1569*4882a593Smuzhiyun rq->postfix = intel_ring_offset(rq, cs);
1570*4882a593Smuzhiyun
1571*4882a593Smuzhiyun return __i915_request_add_to_timeline(rq);
1572*4882a593Smuzhiyun }
1573*4882a593Smuzhiyun
__i915_request_queue(struct i915_request * rq,const struct i915_sched_attr * attr)1574*4882a593Smuzhiyun void __i915_request_queue(struct i915_request *rq,
1575*4882a593Smuzhiyun const struct i915_sched_attr *attr)
1576*4882a593Smuzhiyun {
1577*4882a593Smuzhiyun /*
1578*4882a593Smuzhiyun * Let the backend know a new request has arrived that may need
1579*4882a593Smuzhiyun * to adjust the existing execution schedule due to a high priority
1580*4882a593Smuzhiyun * request - i.e. we may want to preempt the current request in order
1581*4882a593Smuzhiyun * to run a high priority dependency chain *before* we can execute this
1582*4882a593Smuzhiyun * request.
1583*4882a593Smuzhiyun *
1584*4882a593Smuzhiyun * This is called before the request is ready to run so that we can
1585*4882a593Smuzhiyun * decide whether to preempt the entire chain so that it is ready to
1586*4882a593Smuzhiyun * run at the earliest possible convenience.
1587*4882a593Smuzhiyun */
1588*4882a593Smuzhiyun if (attr && rq->engine->schedule)
1589*4882a593Smuzhiyun rq->engine->schedule(rq, attr);
1590*4882a593Smuzhiyun i915_sw_fence_commit(&rq->semaphore);
1591*4882a593Smuzhiyun i915_sw_fence_commit(&rq->submit);
1592*4882a593Smuzhiyun }
1593*4882a593Smuzhiyun
i915_request_add(struct i915_request * rq)1594*4882a593Smuzhiyun void i915_request_add(struct i915_request *rq)
1595*4882a593Smuzhiyun {
1596*4882a593Smuzhiyun struct intel_timeline * const tl = i915_request_timeline(rq);
1597*4882a593Smuzhiyun struct i915_sched_attr attr = {};
1598*4882a593Smuzhiyun struct i915_gem_context *ctx;
1599*4882a593Smuzhiyun
1600*4882a593Smuzhiyun lockdep_assert_held(&tl->mutex);
1601*4882a593Smuzhiyun lockdep_unpin_lock(&tl->mutex, rq->cookie);
1602*4882a593Smuzhiyun
1603*4882a593Smuzhiyun trace_i915_request_add(rq);
1604*4882a593Smuzhiyun __i915_request_commit(rq);
1605*4882a593Smuzhiyun
1606*4882a593Smuzhiyun /* XXX placeholder for selftests */
1607*4882a593Smuzhiyun rcu_read_lock();
1608*4882a593Smuzhiyun ctx = rcu_dereference(rq->context->gem_context);
1609*4882a593Smuzhiyun if (ctx)
1610*4882a593Smuzhiyun attr = ctx->sched;
1611*4882a593Smuzhiyun rcu_read_unlock();
1612*4882a593Smuzhiyun
1613*4882a593Smuzhiyun __i915_request_queue(rq, &attr);
1614*4882a593Smuzhiyun
1615*4882a593Smuzhiyun mutex_unlock(&tl->mutex);
1616*4882a593Smuzhiyun }
1617*4882a593Smuzhiyun
local_clock_ns(unsigned int * cpu)1618*4882a593Smuzhiyun static unsigned long local_clock_ns(unsigned int *cpu)
1619*4882a593Smuzhiyun {
1620*4882a593Smuzhiyun unsigned long t;
1621*4882a593Smuzhiyun
1622*4882a593Smuzhiyun /*
1623*4882a593Smuzhiyun * Cheaply and approximately convert from nanoseconds to microseconds.
1624*4882a593Smuzhiyun * The result and subsequent calculations are also defined in the same
1625*4882a593Smuzhiyun * approximate microseconds units. The principal source of timing
1626*4882a593Smuzhiyun * error here is from the simple truncation.
1627*4882a593Smuzhiyun *
1628*4882a593Smuzhiyun * Note that local_clock() is only defined wrt to the current CPU;
1629*4882a593Smuzhiyun * the comparisons are no longer valid if we switch CPUs. Instead of
1630*4882a593Smuzhiyun * blocking preemption for the entire busywait, we can detect the CPU
1631*4882a593Smuzhiyun * switch and use that as indicator of system load and a reason to
1632*4882a593Smuzhiyun * stop busywaiting, see busywait_stop().
1633*4882a593Smuzhiyun */
1634*4882a593Smuzhiyun *cpu = get_cpu();
1635*4882a593Smuzhiyun t = local_clock();
1636*4882a593Smuzhiyun put_cpu();
1637*4882a593Smuzhiyun
1638*4882a593Smuzhiyun return t;
1639*4882a593Smuzhiyun }
1640*4882a593Smuzhiyun
busywait_stop(unsigned long timeout,unsigned int cpu)1641*4882a593Smuzhiyun static bool busywait_stop(unsigned long timeout, unsigned int cpu)
1642*4882a593Smuzhiyun {
1643*4882a593Smuzhiyun unsigned int this_cpu;
1644*4882a593Smuzhiyun
1645*4882a593Smuzhiyun if (time_after(local_clock_ns(&this_cpu), timeout))
1646*4882a593Smuzhiyun return true;
1647*4882a593Smuzhiyun
1648*4882a593Smuzhiyun return this_cpu != cpu;
1649*4882a593Smuzhiyun }
1650*4882a593Smuzhiyun
__i915_spin_request(struct i915_request * const rq,int state)1651*4882a593Smuzhiyun static bool __i915_spin_request(struct i915_request * const rq, int state)
1652*4882a593Smuzhiyun {
1653*4882a593Smuzhiyun unsigned long timeout_ns;
1654*4882a593Smuzhiyun unsigned int cpu;
1655*4882a593Smuzhiyun
1656*4882a593Smuzhiyun /*
1657*4882a593Smuzhiyun * Only wait for the request if we know it is likely to complete.
1658*4882a593Smuzhiyun *
1659*4882a593Smuzhiyun * We don't track the timestamps around requests, nor the average
1660*4882a593Smuzhiyun * request length, so we do not have a good indicator that this
1661*4882a593Smuzhiyun * request will complete within the timeout. What we do know is the
1662*4882a593Smuzhiyun * order in which requests are executed by the context and so we can
1663*4882a593Smuzhiyun * tell if the request has been started. If the request is not even
1664*4882a593Smuzhiyun * running yet, it is a fair assumption that it will not complete
1665*4882a593Smuzhiyun * within our relatively short timeout.
1666*4882a593Smuzhiyun */
1667*4882a593Smuzhiyun if (!i915_request_is_running(rq))
1668*4882a593Smuzhiyun return false;
1669*4882a593Smuzhiyun
1670*4882a593Smuzhiyun /*
1671*4882a593Smuzhiyun * When waiting for high frequency requests, e.g. during synchronous
1672*4882a593Smuzhiyun * rendering split between the CPU and GPU, the finite amount of time
1673*4882a593Smuzhiyun * required to set up the irq and wait upon it limits the response
1674*4882a593Smuzhiyun * rate. By busywaiting on the request completion for a short while we
1675*4882a593Smuzhiyun * can service the high frequency waits as quick as possible. However,
1676*4882a593Smuzhiyun * if it is a slow request, we want to sleep as quickly as possible.
1677*4882a593Smuzhiyun * The tradeoff between waiting and sleeping is roughly the time it
1678*4882a593Smuzhiyun * takes to sleep on a request, on the order of a microsecond.
1679*4882a593Smuzhiyun */
1680*4882a593Smuzhiyun
1681*4882a593Smuzhiyun timeout_ns = READ_ONCE(rq->engine->props.max_busywait_duration_ns);
1682*4882a593Smuzhiyun timeout_ns += local_clock_ns(&cpu);
1683*4882a593Smuzhiyun do {
1684*4882a593Smuzhiyun if (dma_fence_is_signaled(&rq->fence))
1685*4882a593Smuzhiyun return true;
1686*4882a593Smuzhiyun
1687*4882a593Smuzhiyun if (signal_pending_state(state, current))
1688*4882a593Smuzhiyun break;
1689*4882a593Smuzhiyun
1690*4882a593Smuzhiyun if (busywait_stop(timeout_ns, cpu))
1691*4882a593Smuzhiyun break;
1692*4882a593Smuzhiyun
1693*4882a593Smuzhiyun cpu_relax();
1694*4882a593Smuzhiyun } while (!need_resched());
1695*4882a593Smuzhiyun
1696*4882a593Smuzhiyun return false;
1697*4882a593Smuzhiyun }
1698*4882a593Smuzhiyun
1699*4882a593Smuzhiyun struct request_wait {
1700*4882a593Smuzhiyun struct dma_fence_cb cb;
1701*4882a593Smuzhiyun struct task_struct *tsk;
1702*4882a593Smuzhiyun };
1703*4882a593Smuzhiyun
request_wait_wake(struct dma_fence * fence,struct dma_fence_cb * cb)1704*4882a593Smuzhiyun static void request_wait_wake(struct dma_fence *fence, struct dma_fence_cb *cb)
1705*4882a593Smuzhiyun {
1706*4882a593Smuzhiyun struct request_wait *wait = container_of(cb, typeof(*wait), cb);
1707*4882a593Smuzhiyun
1708*4882a593Smuzhiyun wake_up_process(fetch_and_zero(&wait->tsk));
1709*4882a593Smuzhiyun }
1710*4882a593Smuzhiyun
1711*4882a593Smuzhiyun /**
1712*4882a593Smuzhiyun * i915_request_wait - wait until execution of request has finished
1713*4882a593Smuzhiyun * @rq: the request to wait upon
1714*4882a593Smuzhiyun * @flags: how to wait
1715*4882a593Smuzhiyun * @timeout: how long to wait in jiffies
1716*4882a593Smuzhiyun *
1717*4882a593Smuzhiyun * i915_request_wait() waits for the request to be completed, for a
1718*4882a593Smuzhiyun * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
1719*4882a593Smuzhiyun * unbounded wait).
1720*4882a593Smuzhiyun *
1721*4882a593Smuzhiyun * Returns the remaining time (in jiffies) if the request completed, which may
1722*4882a593Smuzhiyun * be zero or -ETIME if the request is unfinished after the timeout expires.
1723*4882a593Smuzhiyun * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
1724*4882a593Smuzhiyun * pending before the request completes.
1725*4882a593Smuzhiyun */
i915_request_wait(struct i915_request * rq,unsigned int flags,long timeout)1726*4882a593Smuzhiyun long i915_request_wait(struct i915_request *rq,
1727*4882a593Smuzhiyun unsigned int flags,
1728*4882a593Smuzhiyun long timeout)
1729*4882a593Smuzhiyun {
1730*4882a593Smuzhiyun const int state = flags & I915_WAIT_INTERRUPTIBLE ?
1731*4882a593Smuzhiyun TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
1732*4882a593Smuzhiyun struct request_wait wait;
1733*4882a593Smuzhiyun
1734*4882a593Smuzhiyun might_sleep();
1735*4882a593Smuzhiyun GEM_BUG_ON(timeout < 0);
1736*4882a593Smuzhiyun
1737*4882a593Smuzhiyun if (dma_fence_is_signaled(&rq->fence))
1738*4882a593Smuzhiyun return timeout;
1739*4882a593Smuzhiyun
1740*4882a593Smuzhiyun if (!timeout)
1741*4882a593Smuzhiyun return -ETIME;
1742*4882a593Smuzhiyun
1743*4882a593Smuzhiyun trace_i915_request_wait_begin(rq, flags);
1744*4882a593Smuzhiyun
1745*4882a593Smuzhiyun /*
1746*4882a593Smuzhiyun * We must never wait on the GPU while holding a lock as we
1747*4882a593Smuzhiyun * may need to perform a GPU reset. So while we don't need to
1748*4882a593Smuzhiyun * serialise wait/reset with an explicit lock, we do want
1749*4882a593Smuzhiyun * lockdep to detect potential dependency cycles.
1750*4882a593Smuzhiyun */
1751*4882a593Smuzhiyun mutex_acquire(&rq->engine->gt->reset.mutex.dep_map, 0, 0, _THIS_IP_);
1752*4882a593Smuzhiyun
1753*4882a593Smuzhiyun /*
1754*4882a593Smuzhiyun * Optimistic spin before touching IRQs.
1755*4882a593Smuzhiyun *
1756*4882a593Smuzhiyun * We may use a rather large value here to offset the penalty of
1757*4882a593Smuzhiyun * switching away from the active task. Frequently, the client will
1758*4882a593Smuzhiyun * wait upon an old swapbuffer to throttle itself to remain within a
1759*4882a593Smuzhiyun * frame of the gpu. If the client is running in lockstep with the gpu,
1760*4882a593Smuzhiyun * then it should not be waiting long at all, and a sleep now will incur
1761*4882a593Smuzhiyun * extra scheduler latency in producing the next frame. To try to
1762*4882a593Smuzhiyun * avoid adding the cost of enabling/disabling the interrupt to the
1763*4882a593Smuzhiyun * short wait, we first spin to see if the request would have completed
1764*4882a593Smuzhiyun * in the time taken to setup the interrupt.
1765*4882a593Smuzhiyun *
1766*4882a593Smuzhiyun * We need upto 5us to enable the irq, and upto 20us to hide the
1767*4882a593Smuzhiyun * scheduler latency of a context switch, ignoring the secondary
1768*4882a593Smuzhiyun * impacts from a context switch such as cache eviction.
1769*4882a593Smuzhiyun *
1770*4882a593Smuzhiyun * The scheme used for low-latency IO is called "hybrid interrupt
1771*4882a593Smuzhiyun * polling". The suggestion there is to sleep until just before you
1772*4882a593Smuzhiyun * expect to be woken by the device interrupt and then poll for its
1773*4882a593Smuzhiyun * completion. That requires having a good predictor for the request
1774*4882a593Smuzhiyun * duration, which we currently lack.
1775*4882a593Smuzhiyun */
1776*4882a593Smuzhiyun if (IS_ACTIVE(CONFIG_DRM_I915_MAX_REQUEST_BUSYWAIT) &&
1777*4882a593Smuzhiyun __i915_spin_request(rq, state))
1778*4882a593Smuzhiyun goto out;
1779*4882a593Smuzhiyun
1780*4882a593Smuzhiyun /*
1781*4882a593Smuzhiyun * This client is about to stall waiting for the GPU. In many cases
1782*4882a593Smuzhiyun * this is undesirable and limits the throughput of the system, as
1783*4882a593Smuzhiyun * many clients cannot continue processing user input/output whilst
1784*4882a593Smuzhiyun * blocked. RPS autotuning may take tens of milliseconds to respond
1785*4882a593Smuzhiyun * to the GPU load and thus incurs additional latency for the client.
1786*4882a593Smuzhiyun * We can circumvent that by promoting the GPU frequency to maximum
1787*4882a593Smuzhiyun * before we sleep. This makes the GPU throttle up much more quickly
1788*4882a593Smuzhiyun * (good for benchmarks and user experience, e.g. window animations),
1789*4882a593Smuzhiyun * but at a cost of spending more power processing the workload
1790*4882a593Smuzhiyun * (bad for battery).
1791*4882a593Smuzhiyun */
1792*4882a593Smuzhiyun if (flags & I915_WAIT_PRIORITY && !i915_request_started(rq))
1793*4882a593Smuzhiyun intel_rps_boost(rq);
1794*4882a593Smuzhiyun
1795*4882a593Smuzhiyun wait.tsk = current;
1796*4882a593Smuzhiyun if (dma_fence_add_callback(&rq->fence, &wait.cb, request_wait_wake))
1797*4882a593Smuzhiyun goto out;
1798*4882a593Smuzhiyun
1799*4882a593Smuzhiyun /*
1800*4882a593Smuzhiyun * Flush the submission tasklet, but only if it may help this request.
1801*4882a593Smuzhiyun *
1802*4882a593Smuzhiyun * We sometimes experience some latency between the HW interrupts and
1803*4882a593Smuzhiyun * tasklet execution (mostly due to ksoftirqd latency, but it can also
1804*4882a593Smuzhiyun * be due to lazy CS events), so lets run the tasklet manually if there
1805*4882a593Smuzhiyun * is a chance it may submit this request. If the request is not ready
1806*4882a593Smuzhiyun * to run, as it is waiting for other fences to be signaled, flushing
1807*4882a593Smuzhiyun * the tasklet is busy work without any advantage for this client.
1808*4882a593Smuzhiyun *
1809*4882a593Smuzhiyun * If the HW is being lazy, this is the last chance before we go to
1810*4882a593Smuzhiyun * sleep to catch any pending events. We will check periodically in
1811*4882a593Smuzhiyun * the heartbeat to flush the submission tasklets as a last resort
1812*4882a593Smuzhiyun * for unhappy HW.
1813*4882a593Smuzhiyun */
1814*4882a593Smuzhiyun if (i915_request_is_ready(rq))
1815*4882a593Smuzhiyun intel_engine_flush_submission(rq->engine);
1816*4882a593Smuzhiyun
1817*4882a593Smuzhiyun for (;;) {
1818*4882a593Smuzhiyun set_current_state(state);
1819*4882a593Smuzhiyun
1820*4882a593Smuzhiyun if (dma_fence_is_signaled(&rq->fence))
1821*4882a593Smuzhiyun break;
1822*4882a593Smuzhiyun
1823*4882a593Smuzhiyun if (signal_pending_state(state, current)) {
1824*4882a593Smuzhiyun timeout = -ERESTARTSYS;
1825*4882a593Smuzhiyun break;
1826*4882a593Smuzhiyun }
1827*4882a593Smuzhiyun
1828*4882a593Smuzhiyun if (!timeout) {
1829*4882a593Smuzhiyun timeout = -ETIME;
1830*4882a593Smuzhiyun break;
1831*4882a593Smuzhiyun }
1832*4882a593Smuzhiyun
1833*4882a593Smuzhiyun timeout = io_schedule_timeout(timeout);
1834*4882a593Smuzhiyun }
1835*4882a593Smuzhiyun __set_current_state(TASK_RUNNING);
1836*4882a593Smuzhiyun
1837*4882a593Smuzhiyun if (READ_ONCE(wait.tsk))
1838*4882a593Smuzhiyun dma_fence_remove_callback(&rq->fence, &wait.cb);
1839*4882a593Smuzhiyun GEM_BUG_ON(!list_empty(&wait.cb.node));
1840*4882a593Smuzhiyun
1841*4882a593Smuzhiyun out:
1842*4882a593Smuzhiyun mutex_release(&rq->engine->gt->reset.mutex.dep_map, _THIS_IP_);
1843*4882a593Smuzhiyun trace_i915_request_wait_end(rq);
1844*4882a593Smuzhiyun return timeout;
1845*4882a593Smuzhiyun }
1846*4882a593Smuzhiyun
1847*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1848*4882a593Smuzhiyun #include "selftests/mock_request.c"
1849*4882a593Smuzhiyun #include "selftests/i915_request.c"
1850*4882a593Smuzhiyun #endif
1851*4882a593Smuzhiyun
i915_global_request_shrink(void)1852*4882a593Smuzhiyun static void i915_global_request_shrink(void)
1853*4882a593Smuzhiyun {
1854*4882a593Smuzhiyun kmem_cache_shrink(global.slab_execute_cbs);
1855*4882a593Smuzhiyun kmem_cache_shrink(global.slab_requests);
1856*4882a593Smuzhiyun }
1857*4882a593Smuzhiyun
i915_global_request_exit(void)1858*4882a593Smuzhiyun static void i915_global_request_exit(void)
1859*4882a593Smuzhiyun {
1860*4882a593Smuzhiyun kmem_cache_destroy(global.slab_execute_cbs);
1861*4882a593Smuzhiyun kmem_cache_destroy(global.slab_requests);
1862*4882a593Smuzhiyun }
1863*4882a593Smuzhiyun
1864*4882a593Smuzhiyun static struct i915_global_request global = { {
1865*4882a593Smuzhiyun .shrink = i915_global_request_shrink,
1866*4882a593Smuzhiyun .exit = i915_global_request_exit,
1867*4882a593Smuzhiyun } };
1868*4882a593Smuzhiyun
i915_global_request_init(void)1869*4882a593Smuzhiyun int __init i915_global_request_init(void)
1870*4882a593Smuzhiyun {
1871*4882a593Smuzhiyun global.slab_requests =
1872*4882a593Smuzhiyun kmem_cache_create("i915_request",
1873*4882a593Smuzhiyun sizeof(struct i915_request),
1874*4882a593Smuzhiyun __alignof__(struct i915_request),
1875*4882a593Smuzhiyun SLAB_HWCACHE_ALIGN |
1876*4882a593Smuzhiyun SLAB_RECLAIM_ACCOUNT |
1877*4882a593Smuzhiyun SLAB_TYPESAFE_BY_RCU,
1878*4882a593Smuzhiyun __i915_request_ctor);
1879*4882a593Smuzhiyun if (!global.slab_requests)
1880*4882a593Smuzhiyun return -ENOMEM;
1881*4882a593Smuzhiyun
1882*4882a593Smuzhiyun global.slab_execute_cbs = KMEM_CACHE(execute_cb,
1883*4882a593Smuzhiyun SLAB_HWCACHE_ALIGN |
1884*4882a593Smuzhiyun SLAB_RECLAIM_ACCOUNT |
1885*4882a593Smuzhiyun SLAB_TYPESAFE_BY_RCU);
1886*4882a593Smuzhiyun if (!global.slab_execute_cbs)
1887*4882a593Smuzhiyun goto err_requests;
1888*4882a593Smuzhiyun
1889*4882a593Smuzhiyun i915_global_register(&global.base);
1890*4882a593Smuzhiyun return 0;
1891*4882a593Smuzhiyun
1892*4882a593Smuzhiyun err_requests:
1893*4882a593Smuzhiyun kmem_cache_destroy(global.slab_requests);
1894*4882a593Smuzhiyun return -ENOMEM;
1895*4882a593Smuzhiyun }
1896