1 // SPDX-License-Identifier: MIT
3 * Copyright © 2014 Intel Corporation
6 #include <linux/circ_buf.h>
8 #include "gem/i915_gem_context.h"
9 #include "gt/gen8_engine_cs.h"
10 #include "gt/intel_breadcrumbs.h"
11 #include "gt/intel_context.h"
12 #include "gt/intel_engine_pm.h"
13 #include "gt/intel_engine_heartbeat.h"
14 #include "gt/intel_gt.h"
15 #include "gt/intel_gt_irq.h"
16 #include "gt/intel_gt_pm.h"
17 #include "gt/intel_gt_requests.h"
18 #include "gt/intel_lrc.h"
19 #include "gt/intel_lrc_reg.h"
20 #include "gt/intel_mocs.h"
21 #include "gt/intel_ring.h"
23 #include "intel_guc_submission.h"
26 #include "i915_trace.h"
29 * DOC: GuC-based command submission
31 * IMPORTANT NOTE: GuC submission is currently not supported in i915. The GuC
32 * firmware is moving to an updated submission interface and we plan to
33 * turn submission back on when that lands. The below documentation (and related
34 * code) matches the old submission model and will be updated as part of the
35 * upgrade to the new flow.
37 * GuC stage descriptor:
38 * During initialization, the driver allocates a static pool of 1024 such
39 * descriptors, and shares them with the GuC. Currently, we only use one
40 * descriptor. This stage descriptor lets the GuC know about the workqueue and
41 * process descriptor. Theoretically, it also lets the GuC know about our HW
42 * contexts (context ID, etc...), but we actually employ a kind of submission
43 * where the GuC uses the LRCA sent via the work item instead. This is called
44 * a "proxy" submission.
46 * The Scratch registers:
47 * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes
48 * a value to the action register (SOFT_SCRATCH_0) along with any data. It then
49 * triggers an interrupt on the GuC via another register write (0xC4C8).
50 * Firmware writes a success/fail code back to the action register after
51 * processes the request. The kernel driver polls waiting for this update and
55 * There are several types of work items that the host may place into a
56 * workqueue, each with its own requirements and limitations. Currently only
57 * WQ_TYPE_INORDER is needed to support legacy submission via GuC, which
58 * represents in-order queue. The kernel driver packs ring tail pointer and an
59 * ELSP context descriptor dword into Work Item.
60 * See guc_add_request()
64 /* GuC Virtual Engine */
65 struct guc_virtual_engine {
66 struct intel_engine_cs base;
67 struct intel_context context;
70 static struct intel_context *
71 guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count);
73 #define GUC_REQUEST_SIZE 64 /* bytes */
76 * Below is a set of functions which control the GuC scheduling state which do
77 * not require a lock as all state transitions are mutually exclusive. i.e. It
78 * is not possible for the context pinning code and submission, for the same
79 * context, to be executing simultaneously. We still need an atomic as it is
80 * possible for some of the bits to changing at the same time though.
82 #define SCHED_STATE_NO_LOCK_ENABLED BIT(0)
83 #define SCHED_STATE_NO_LOCK_PENDING_ENABLE BIT(1)
84 #define SCHED_STATE_NO_LOCK_REGISTERED BIT(2)
85 static inline bool context_enabled(struct intel_context *ce)
87 return (atomic_read(&ce->guc_sched_state_no_lock) &
88 SCHED_STATE_NO_LOCK_ENABLED);
91 static inline void set_context_enabled(struct intel_context *ce)
93 atomic_or(SCHED_STATE_NO_LOCK_ENABLED, &ce->guc_sched_state_no_lock);
96 static inline void clr_context_enabled(struct intel_context *ce)
98 atomic_and((u32)~SCHED_STATE_NO_LOCK_ENABLED,
99 &ce->guc_sched_state_no_lock);
102 static inline bool context_pending_enable(struct intel_context *ce)
104 return (atomic_read(&ce->guc_sched_state_no_lock) &
105 SCHED_STATE_NO_LOCK_PENDING_ENABLE);
108 static inline void set_context_pending_enable(struct intel_context *ce)
110 atomic_or(SCHED_STATE_NO_LOCK_PENDING_ENABLE,
111 &ce->guc_sched_state_no_lock);
114 static inline void clr_context_pending_enable(struct intel_context *ce)
116 atomic_and((u32)~SCHED_STATE_NO_LOCK_PENDING_ENABLE,
117 &ce->guc_sched_state_no_lock);
120 static inline bool context_registered(struct intel_context *ce)
122 return (atomic_read(&ce->guc_sched_state_no_lock) &
123 SCHED_STATE_NO_LOCK_REGISTERED);
126 static inline void set_context_registered(struct intel_context *ce)
128 atomic_or(SCHED_STATE_NO_LOCK_REGISTERED,
129 &ce->guc_sched_state_no_lock);
132 static inline void clr_context_registered(struct intel_context *ce)
134 atomic_and((u32)~SCHED_STATE_NO_LOCK_REGISTERED,
135 &ce->guc_sched_state_no_lock);
139 * Below is a set of functions which control the GuC scheduling state which
140 * require a lock, aside from the special case where the functions are called
141 * from guc_lrc_desc_pin(). In that case it isn't possible for any other code
142 * path to be executing on the context.
144 #define SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER BIT(0)
145 #define SCHED_STATE_DESTROYED BIT(1)
146 #define SCHED_STATE_PENDING_DISABLE BIT(2)
147 #define SCHED_STATE_BANNED BIT(3)
148 #define SCHED_STATE_BLOCKED_SHIFT 4
149 #define SCHED_STATE_BLOCKED BIT(SCHED_STATE_BLOCKED_SHIFT)
150 #define SCHED_STATE_BLOCKED_MASK (0xfff << SCHED_STATE_BLOCKED_SHIFT)
151 static inline void init_sched_state(struct intel_context *ce)
153 /* Only should be called from guc_lrc_desc_pin() */
154 atomic_set(&ce->guc_sched_state_no_lock, 0);
155 ce->guc_state.sched_state = 0;
159 context_wait_for_deregister_to_register(struct intel_context *ce)
161 return ce->guc_state.sched_state &
162 SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
166 set_context_wait_for_deregister_to_register(struct intel_context *ce)
168 /* Only should be called from guc_lrc_desc_pin() without lock */
169 ce->guc_state.sched_state |=
170 SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
174 clr_context_wait_for_deregister_to_register(struct intel_context *ce)
176 lockdep_assert_held(&ce->guc_state.lock);
177 ce->guc_state.sched_state &=
178 ~SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
182 context_destroyed(struct intel_context *ce)
184 return ce->guc_state.sched_state & SCHED_STATE_DESTROYED;
188 set_context_destroyed(struct intel_context *ce)
190 lockdep_assert_held(&ce->guc_state.lock);
191 ce->guc_state.sched_state |= SCHED_STATE_DESTROYED;
194 static inline bool context_pending_disable(struct intel_context *ce)
196 return ce->guc_state.sched_state & SCHED_STATE_PENDING_DISABLE;
199 static inline void set_context_pending_disable(struct intel_context *ce)
201 lockdep_assert_held(&ce->guc_state.lock);
202 ce->guc_state.sched_state |= SCHED_STATE_PENDING_DISABLE;
205 static inline void clr_context_pending_disable(struct intel_context *ce)
207 lockdep_assert_held(&ce->guc_state.lock);
208 ce->guc_state.sched_state &= ~SCHED_STATE_PENDING_DISABLE;
211 static inline bool context_banned(struct intel_context *ce)
213 return ce->guc_state.sched_state & SCHED_STATE_BANNED;
216 static inline void set_context_banned(struct intel_context *ce)
218 lockdep_assert_held(&ce->guc_state.lock);
219 ce->guc_state.sched_state |= SCHED_STATE_BANNED;
222 static inline void clr_context_banned(struct intel_context *ce)
224 lockdep_assert_held(&ce->guc_state.lock);
225 ce->guc_state.sched_state &= ~SCHED_STATE_BANNED;
228 static inline u32 context_blocked(struct intel_context *ce)
230 return (ce->guc_state.sched_state & SCHED_STATE_BLOCKED_MASK) >>
231 SCHED_STATE_BLOCKED_SHIFT;
234 static inline void incr_context_blocked(struct intel_context *ce)
236 lockdep_assert_held(&ce->engine->sched_engine->lock);
237 lockdep_assert_held(&ce->guc_state.lock);
239 ce->guc_state.sched_state += SCHED_STATE_BLOCKED;
241 GEM_BUG_ON(!context_blocked(ce)); /* Overflow check */
244 static inline void decr_context_blocked(struct intel_context *ce)
246 lockdep_assert_held(&ce->engine->sched_engine->lock);
247 lockdep_assert_held(&ce->guc_state.lock);
249 GEM_BUG_ON(!context_blocked(ce)); /* Underflow check */
251 ce->guc_state.sched_state -= SCHED_STATE_BLOCKED;
254 static inline bool context_guc_id_invalid(struct intel_context *ce)
256 return ce->guc_id == GUC_INVALID_LRC_ID;
259 static inline void set_context_guc_id_invalid(struct intel_context *ce)
261 ce->guc_id = GUC_INVALID_LRC_ID;
264 static inline struct intel_guc *ce_to_guc(struct intel_context *ce)
266 return &ce->engine->gt->uc.guc;
269 static inline struct i915_priolist *to_priolist(struct rb_node *rb)
271 return rb_entry(rb, struct i915_priolist, node);
274 static struct guc_lrc_desc *__get_lrc_desc(struct intel_guc *guc, u32 index)
276 struct guc_lrc_desc *base = guc->lrc_desc_pool_vaddr;
278 GEM_BUG_ON(index >= GUC_MAX_LRC_DESCRIPTORS);
283 static inline struct intel_context *__get_context(struct intel_guc *guc, u32 id)
285 struct intel_context *ce = xa_load(&guc->context_lookup, id);
287 GEM_BUG_ON(id >= GUC_MAX_LRC_DESCRIPTORS);
292 static int guc_lrc_desc_pool_create(struct intel_guc *guc)
297 size = PAGE_ALIGN(sizeof(struct guc_lrc_desc) *
298 GUC_MAX_LRC_DESCRIPTORS);
299 ret = intel_guc_allocate_and_map_vma(guc, size, &guc->lrc_desc_pool,
300 (void **)&guc->lrc_desc_pool_vaddr);
307 static void guc_lrc_desc_pool_destroy(struct intel_guc *guc)
309 guc->lrc_desc_pool_vaddr = NULL;
310 i915_vma_unpin_and_release(&guc->lrc_desc_pool, I915_VMA_RELEASE_MAP);
313 static inline bool guc_submission_initialized(struct intel_guc *guc)
315 return !!guc->lrc_desc_pool_vaddr;
318 static inline void reset_lrc_desc(struct intel_guc *guc, u32 id)
320 if (likely(guc_submission_initialized(guc))) {
321 struct guc_lrc_desc *desc = __get_lrc_desc(guc, id);
324 memset(desc, 0, sizeof(*desc));
327 * xarray API doesn't have xa_erase_irqsave wrapper, so calling
328 * the lower level functions directly.
330 xa_lock_irqsave(&guc->context_lookup, flags);
331 __xa_erase(&guc->context_lookup, id);
332 xa_unlock_irqrestore(&guc->context_lookup, flags);
336 static inline bool lrc_desc_registered(struct intel_guc *guc, u32 id)
338 return __get_context(guc, id);
341 static inline void set_lrc_desc_registered(struct intel_guc *guc, u32 id,
342 struct intel_context *ce)
347 * xarray API doesn't have xa_save_irqsave wrapper, so calling the
348 * lower level functions directly.
350 xa_lock_irqsave(&guc->context_lookup, flags);
351 __xa_store(&guc->context_lookup, id, ce, GFP_ATOMIC);
352 xa_unlock_irqrestore(&guc->context_lookup, flags);
355 static int guc_submission_send_busy_loop(struct intel_guc *guc,
363 err = intel_guc_send_busy_loop(guc, action, len, g2h_len_dw, loop);
365 if (!err && g2h_len_dw)
366 atomic_inc(&guc->outstanding_submission_g2h);
371 int intel_guc_wait_for_pending_msg(struct intel_guc *guc,
376 const int state = interruptible ?
377 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
381 GEM_BUG_ON(timeout < 0);
383 if (!atomic_read(wait_var))
390 prepare_to_wait(&guc->ct.wq, &wait, state);
392 if (!atomic_read(wait_var))
395 if (signal_pending_state(state, current)) {
405 timeout = io_schedule_timeout(timeout);
407 finish_wait(&guc->ct.wq, &wait);
409 return (timeout < 0) ? timeout : 0;
412 int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout)
414 if (!intel_uc_uses_guc_submission(&guc_to_gt(guc)->uc))
417 return intel_guc_wait_for_pending_msg(guc,
418 &guc->outstanding_submission_g2h,
422 static int guc_lrc_desc_pin(struct intel_context *ce, bool loop);
424 static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
427 struct intel_context *ce = rq->context;
434 * Corner case where requests were sitting in the priority list or a
435 * request resubmitted after the context was banned.
437 if (unlikely(intel_context_is_banned(ce))) {
438 i915_request_put(i915_request_mark_eio(rq));
439 intel_engine_signal_breadcrumbs(ce->engine);
443 GEM_BUG_ON(!atomic_read(&ce->guc_id_ref));
444 GEM_BUG_ON(context_guc_id_invalid(ce));
447 * Corner case where the GuC firmware was blown away and reloaded while
448 * this context was pinned.
450 if (unlikely(!lrc_desc_registered(guc, ce->guc_id))) {
451 err = guc_lrc_desc_pin(ce, false);
457 * The request / context will be run on the hardware when scheduling
458 * gets enabled in the unblock.
460 if (unlikely(context_blocked(ce)))
463 enabled = context_enabled(ce);
466 action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
467 action[len++] = ce->guc_id;
468 action[len++] = GUC_CONTEXT_ENABLE;
469 set_context_pending_enable(ce);
470 intel_context_get(ce);
471 g2h_len_dw = G2H_LEN_DW_SCHED_CONTEXT_MODE_SET;
473 action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT;
474 action[len++] = ce->guc_id;
477 err = intel_guc_send_nb(guc, action, len, g2h_len_dw);
478 if (!enabled && !err) {
479 trace_intel_context_sched_enable(ce);
480 atomic_inc(&guc->outstanding_submission_g2h);
481 set_context_enabled(ce);
482 } else if (!enabled) {
483 clr_context_pending_enable(ce);
484 intel_context_put(ce);
487 trace_i915_request_guc_submit(rq);
493 static inline void guc_set_lrc_tail(struct i915_request *rq)
495 rq->context->lrc_reg_state[CTX_RING_TAIL] =
496 intel_ring_set_tail(rq->ring, rq->tail);
499 static inline int rq_prio(const struct i915_request *rq)
501 return rq->sched.attr.priority;
504 static int guc_dequeue_one_context(struct intel_guc *guc)
506 struct i915_sched_engine * const sched_engine = guc->sched_engine;
507 struct i915_request *last = NULL;
512 lockdep_assert_held(&sched_engine->lock);
514 if (guc->stalled_request) {
516 last = guc->stalled_request;
520 while ((rb = rb_first_cached(&sched_engine->queue))) {
521 struct i915_priolist *p = to_priolist(rb);
522 struct i915_request *rq, *rn;
524 priolist_for_each_request_consume(rq, rn, p) {
525 if (last && rq->context != last->context)
528 list_del_init(&rq->sched.link);
530 __i915_request_submit(rq);
532 trace_i915_request_in(rq, 0);
537 rb_erase_cached(&p->node, &sched_engine->queue);
538 i915_priolist_free(p);
542 guc_set_lrc_tail(last);
544 ret = guc_add_request(guc, last);
545 if (unlikely(ret == -EPIPE))
547 else if (ret == -EBUSY) {
548 tasklet_schedule(&sched_engine->tasklet);
549 guc->stalled_request = last;
554 guc->stalled_request = NULL;
558 sched_engine->tasklet.callback = NULL;
559 tasklet_disable_nosync(&sched_engine->tasklet);
563 static void guc_submission_tasklet(struct tasklet_struct *t)
565 struct i915_sched_engine *sched_engine =
566 from_tasklet(sched_engine, t, tasklet);
570 spin_lock_irqsave(&sched_engine->lock, flags);
573 loop = guc_dequeue_one_context(sched_engine->private_data);
576 i915_sched_engine_reset_on_empty(sched_engine);
578 spin_unlock_irqrestore(&sched_engine->lock, flags);
581 static void cs_irq_handler(struct intel_engine_cs *engine, u16 iir)
583 if (iir & GT_RENDER_USER_INTERRUPT)
584 intel_engine_signal_breadcrumbs(engine);
587 static void __guc_context_destroy(struct intel_context *ce);
588 static void release_guc_id(struct intel_guc *guc, struct intel_context *ce);
589 static void guc_signal_context_fence(struct intel_context *ce);
590 static void guc_cancel_context_requests(struct intel_context *ce);
591 static void guc_blocked_fence_complete(struct intel_context *ce);
593 static void scrub_guc_desc_for_outstanding_g2h(struct intel_guc *guc)
595 struct intel_context *ce;
596 unsigned long index, flags;
597 bool pending_disable, pending_enable, deregister, destroyed, banned;
599 xa_for_each(&guc->context_lookup, index, ce) {
601 spin_lock_irqsave(&ce->guc_state.lock, flags);
602 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
605 * Once we are at this point submission_disabled() is guaranteed
606 * to be visible to all callers who set the below flags (see above
607 * flush and flushes in reset_prepare). If submission_disabled()
608 * is set, the caller shouldn't set these flags.
611 destroyed = context_destroyed(ce);
612 pending_enable = context_pending_enable(ce);
613 pending_disable = context_pending_disable(ce);
614 deregister = context_wait_for_deregister_to_register(ce);
615 banned = context_banned(ce);
616 init_sched_state(ce);
618 if (pending_enable || destroyed || deregister) {
619 atomic_dec(&guc->outstanding_submission_g2h);
621 guc_signal_context_fence(ce);
623 release_guc_id(guc, ce);
624 __guc_context_destroy(ce);
626 if (pending_enable || deregister)
627 intel_context_put(ce);
630 /* Not mutualy exclusive with above if statement. */
631 if (pending_disable) {
632 guc_signal_context_fence(ce);
634 guc_cancel_context_requests(ce);
635 intel_engine_signal_breadcrumbs(ce->engine);
637 intel_context_sched_disable_unpin(ce);
638 atomic_dec(&guc->outstanding_submission_g2h);
639 spin_lock_irqsave(&ce->guc_state.lock, flags);
640 guc_blocked_fence_complete(ce);
641 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
643 intel_context_put(ce);
649 submission_disabled(struct intel_guc *guc)
651 struct i915_sched_engine * const sched_engine = guc->sched_engine;
653 return unlikely(!sched_engine ||
654 !__tasklet_is_enabled(&sched_engine->tasklet));
657 static void disable_submission(struct intel_guc *guc)
659 struct i915_sched_engine * const sched_engine = guc->sched_engine;
661 if (__tasklet_is_enabled(&sched_engine->tasklet)) {
662 GEM_BUG_ON(!guc->ct.enabled);
663 __tasklet_disable_sync_once(&sched_engine->tasklet);
664 sched_engine->tasklet.callback = NULL;
668 static void enable_submission(struct intel_guc *guc)
670 struct i915_sched_engine * const sched_engine = guc->sched_engine;
673 spin_lock_irqsave(&guc->sched_engine->lock, flags);
674 sched_engine->tasklet.callback = guc_submission_tasklet;
675 wmb(); /* Make sure callback visible */
676 if (!__tasklet_is_enabled(&sched_engine->tasklet) &&
677 __tasklet_enable(&sched_engine->tasklet)) {
678 GEM_BUG_ON(!guc->ct.enabled);
680 /* And kick in case we missed a new request submission. */
681 tasklet_hi_schedule(&sched_engine->tasklet);
683 spin_unlock_irqrestore(&guc->sched_engine->lock, flags);
686 static void guc_flush_submissions(struct intel_guc *guc)
688 struct i915_sched_engine * const sched_engine = guc->sched_engine;
691 spin_lock_irqsave(&sched_engine->lock, flags);
692 spin_unlock_irqrestore(&sched_engine->lock, flags);
695 void intel_guc_submission_reset_prepare(struct intel_guc *guc)
699 if (unlikely(!guc_submission_initialized(guc))) {
700 /* Reset called during driver load? GuC not yet initialised! */
704 intel_gt_park_heartbeats(guc_to_gt(guc));
705 disable_submission(guc);
706 guc->interrupts.disable(guc);
708 /* Flush IRQ handler */
709 spin_lock_irq(&guc_to_gt(guc)->irq_lock);
710 spin_unlock_irq(&guc_to_gt(guc)->irq_lock);
712 guc_flush_submissions(guc);
715 * Handle any outstanding G2Hs before reset. Call IRQ handler directly
716 * each pass as interrupt have been disabled. We always scrub for
717 * outstanding G2H as it is possible for outstanding_submission_g2h to
718 * be incremented after the context state update.
720 for (i = 0; i < 4 && atomic_read(&guc->outstanding_submission_g2h); ++i) {
721 intel_guc_to_host_event_handler(guc);
722 #define wait_for_reset(guc, wait_var) \
723 intel_guc_wait_for_pending_msg(guc, wait_var, false, (HZ / 20))
725 wait_for_reset(guc, &guc->outstanding_submission_g2h);
726 } while (!list_empty(&guc->ct.requests.incoming));
728 scrub_guc_desc_for_outstanding_g2h(guc);
731 static struct intel_engine_cs *
732 guc_virtual_get_sibling(struct intel_engine_cs *ve, unsigned int sibling)
734 struct intel_engine_cs *engine;
735 intel_engine_mask_t tmp, mask = ve->mask;
736 unsigned int num_siblings = 0;
738 for_each_engine_masked(engine, ve->gt, mask, tmp)
739 if (num_siblings++ == sibling)
745 static inline struct intel_engine_cs *
746 __context_to_physical_engine(struct intel_context *ce)
748 struct intel_engine_cs *engine = ce->engine;
750 if (intel_engine_is_virtual(engine))
751 engine = guc_virtual_get_sibling(engine, 0);
756 static void guc_reset_state(struct intel_context *ce, u32 head, bool scrub)
758 struct intel_engine_cs *engine = __context_to_physical_engine(ce);
760 if (intel_context_is_banned(ce))
763 GEM_BUG_ON(!intel_context_is_pinned(ce));
766 * We want a simple context + ring to execute the breadcrumb update.
767 * We cannot rely on the context being intact across the GPU hang,
768 * so clear it and rebuild just what we need for the breadcrumb.
769 * All pending requests for this context will be zapped, and any
770 * future request will be after userspace has had the opportunity
771 * to recreate its own state.
774 lrc_init_regs(ce, engine, true);
776 /* Rerun the request; its payload has been neutered (if guilty). */
777 lrc_update_regs(ce, engine, head);
780 static void guc_reset_nop(struct intel_engine_cs *engine)
784 static void guc_rewind_nop(struct intel_engine_cs *engine, bool stalled)
789 __unwind_incomplete_requests(struct intel_context *ce)
791 struct i915_request *rq, *rn;
792 struct list_head *pl;
793 int prio = I915_PRIORITY_INVALID;
794 struct i915_sched_engine * const sched_engine =
795 ce->engine->sched_engine;
798 spin_lock_irqsave(&sched_engine->lock, flags);
799 spin_lock(&ce->guc_active.lock);
800 list_for_each_entry_safe(rq, rn,
801 &ce->guc_active.requests,
803 if (i915_request_completed(rq))
806 list_del_init(&rq->sched.link);
807 spin_unlock(&ce->guc_active.lock);
809 __i915_request_unsubmit(rq);
811 /* Push the request back into the queue for later resubmission. */
812 GEM_BUG_ON(rq_prio(rq) == I915_PRIORITY_INVALID);
813 if (rq_prio(rq) != prio) {
815 pl = i915_sched_lookup_priolist(sched_engine, prio);
817 GEM_BUG_ON(i915_sched_engine_is_empty(sched_engine));
819 list_add_tail(&rq->sched.link, pl);
820 set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
822 spin_lock(&ce->guc_active.lock);
824 spin_unlock(&ce->guc_active.lock);
825 spin_unlock_irqrestore(&sched_engine->lock, flags);
828 static void __guc_reset_context(struct intel_context *ce, bool stalled)
830 struct i915_request *rq;
833 intel_context_get(ce);
836 * GuC will implicitly mark the context as non-schedulable
837 * when it sends the reset notification. Make sure our state
838 * reflects this change. The context will be marked enabled
841 clr_context_enabled(ce);
843 rq = intel_context_find_active_request(ce);
845 head = ce->ring->tail;
850 if (!i915_request_started(rq))
853 GEM_BUG_ON(i915_active_is_idle(&ce->active));
854 head = intel_ring_wrap(ce->ring, rq->head);
855 __i915_request_reset(rq, stalled);
858 guc_reset_state(ce, head, stalled);
859 __unwind_incomplete_requests(ce);
860 intel_context_put(ce);
863 void intel_guc_submission_reset(struct intel_guc *guc, bool stalled)
865 struct intel_context *ce;
868 if (unlikely(!guc_submission_initialized(guc))) {
869 /* Reset called during driver load? GuC not yet initialised! */
873 xa_for_each(&guc->context_lookup, index, ce)
874 if (intel_context_is_pinned(ce))
875 __guc_reset_context(ce, stalled);
877 /* GuC is blown away, drop all references to contexts */
878 xa_destroy(&guc->context_lookup);
881 static void guc_cancel_context_requests(struct intel_context *ce)
883 struct i915_sched_engine *sched_engine = ce_to_guc(ce)->sched_engine;
884 struct i915_request *rq;
887 /* Mark all executing requests as skipped. */
888 spin_lock_irqsave(&sched_engine->lock, flags);
889 spin_lock(&ce->guc_active.lock);
890 list_for_each_entry(rq, &ce->guc_active.requests, sched.link)
891 i915_request_put(i915_request_mark_eio(rq));
892 spin_unlock(&ce->guc_active.lock);
893 spin_unlock_irqrestore(&sched_engine->lock, flags);
897 guc_cancel_sched_engine_requests(struct i915_sched_engine *sched_engine)
899 struct i915_request *rq, *rn;
903 /* Can be called during boot if GuC fails to load */
908 * Before we call engine->cancel_requests(), we should have exclusive
909 * access to the submission state. This is arranged for us by the
910 * caller disabling the interrupt generation, the tasklet and other
911 * threads that may then access the same state, giving us a free hand
912 * to reset state. However, we still need to let lockdep be aware that
913 * we know this state may be accessed in hardirq context, so we
914 * disable the irq around this manipulation and we want to keep
915 * the spinlock focused on its duties and not accidentally conflate
916 * coverage to the submission's irq state. (Similarly, although we
917 * shouldn't need to disable irq around the manipulation of the
918 * submission's irq state, we also wish to remind ourselves that
921 spin_lock_irqsave(&sched_engine->lock, flags);
923 /* Flush the queued requests to the timeline list (for retiring). */
924 while ((rb = rb_first_cached(&sched_engine->queue))) {
925 struct i915_priolist *p = to_priolist(rb);
927 priolist_for_each_request_consume(rq, rn, p) {
928 list_del_init(&rq->sched.link);
930 __i915_request_submit(rq);
932 i915_request_put(i915_request_mark_eio(rq));
935 rb_erase_cached(&p->node, &sched_engine->queue);
936 i915_priolist_free(p);
939 /* Remaining _unready_ requests will be nop'ed when submitted */
941 sched_engine->queue_priority_hint = INT_MIN;
942 sched_engine->queue = RB_ROOT_CACHED;
944 spin_unlock_irqrestore(&sched_engine->lock, flags);
947 void intel_guc_submission_cancel_requests(struct intel_guc *guc)
949 struct intel_context *ce;
952 xa_for_each(&guc->context_lookup, index, ce)
953 if (intel_context_is_pinned(ce))
954 guc_cancel_context_requests(ce);
956 guc_cancel_sched_engine_requests(guc->sched_engine);
958 /* GuC is blown away, drop all references to contexts */
959 xa_destroy(&guc->context_lookup);
962 void intel_guc_submission_reset_finish(struct intel_guc *guc)
964 /* Reset called during driver load or during wedge? */
965 if (unlikely(!guc_submission_initialized(guc) ||
966 test_bit(I915_WEDGED, &guc_to_gt(guc)->reset.flags))) {
971 * Technically possible for either of these values to be non-zero here,
972 * but very unlikely + harmless. Regardless let's add a warn so we can
973 * see in CI if this happens frequently / a precursor to taking down the
976 GEM_WARN_ON(atomic_read(&guc->outstanding_submission_g2h));
977 atomic_set(&guc->outstanding_submission_g2h, 0);
979 intel_guc_global_policies_update(guc);
980 enable_submission(guc);
981 intel_gt_unpark_heartbeats(guc_to_gt(guc));
985 * Set up the memory resources to be shared with the GuC (via the GGTT)
986 * at firmware loading time.
988 int intel_guc_submission_init(struct intel_guc *guc)
992 if (guc->lrc_desc_pool)
995 ret = guc_lrc_desc_pool_create(guc);
999 * Keep static analysers happy, let them know that we allocated the
1000 * vma after testing that it didn't exist earlier.
1002 GEM_BUG_ON(!guc->lrc_desc_pool);
1004 xa_init_flags(&guc->context_lookup, XA_FLAGS_LOCK_IRQ);
1006 spin_lock_init(&guc->contexts_lock);
1007 INIT_LIST_HEAD(&guc->guc_id_list);
1008 ida_init(&guc->guc_ids);
1013 void intel_guc_submission_fini(struct intel_guc *guc)
1015 if (!guc->lrc_desc_pool)
1018 guc_lrc_desc_pool_destroy(guc);
1019 i915_sched_engine_put(guc->sched_engine);
1022 static inline void queue_request(struct i915_sched_engine *sched_engine,
1023 struct i915_request *rq,
1026 GEM_BUG_ON(!list_empty(&rq->sched.link));
1027 list_add_tail(&rq->sched.link,
1028 i915_sched_lookup_priolist(sched_engine, prio));
1029 set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
1032 static int guc_bypass_tasklet_submit(struct intel_guc *guc,
1033 struct i915_request *rq)
1037 __i915_request_submit(rq);
1039 trace_i915_request_in(rq, 0);
1041 guc_set_lrc_tail(rq);
1042 ret = guc_add_request(guc, rq);
1044 guc->stalled_request = rq;
1046 if (unlikely(ret == -EPIPE))
1047 disable_submission(guc);
1052 static void guc_submit_request(struct i915_request *rq)
1054 struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
1055 struct intel_guc *guc = &rq->engine->gt->uc.guc;
1056 unsigned long flags;
1058 /* Will be called from irq-context when using foreign fences. */
1059 spin_lock_irqsave(&sched_engine->lock, flags);
1061 if (submission_disabled(guc) || guc->stalled_request ||
1062 !i915_sched_engine_is_empty(sched_engine))
1063 queue_request(sched_engine, rq, rq_prio(rq));
1064 else if (guc_bypass_tasklet_submit(guc, rq) == -EBUSY)
1065 tasklet_hi_schedule(&sched_engine->tasklet);
1067 spin_unlock_irqrestore(&sched_engine->lock, flags);
1070 static int new_guc_id(struct intel_guc *guc)
1072 return ida_simple_get(&guc->guc_ids, 0,
1073 GUC_MAX_LRC_DESCRIPTORS, GFP_KERNEL |
1074 __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
1077 static void __release_guc_id(struct intel_guc *guc, struct intel_context *ce)
1079 if (!context_guc_id_invalid(ce)) {
1080 ida_simple_remove(&guc->guc_ids, ce->guc_id);
1081 reset_lrc_desc(guc, ce->guc_id);
1082 set_context_guc_id_invalid(ce);
1084 if (!list_empty(&ce->guc_id_link))
1085 list_del_init(&ce->guc_id_link);
1088 static void release_guc_id(struct intel_guc *guc, struct intel_context *ce)
1090 unsigned long flags;
1092 spin_lock_irqsave(&guc->contexts_lock, flags);
1093 __release_guc_id(guc, ce);
1094 spin_unlock_irqrestore(&guc->contexts_lock, flags);
1097 static int steal_guc_id(struct intel_guc *guc)
1099 struct intel_context *ce;
1102 lockdep_assert_held(&guc->contexts_lock);
1104 if (!list_empty(&guc->guc_id_list)) {
1105 ce = list_first_entry(&guc->guc_id_list,
1106 struct intel_context,
1109 GEM_BUG_ON(atomic_read(&ce->guc_id_ref));
1110 GEM_BUG_ON(context_guc_id_invalid(ce));
1112 list_del_init(&ce->guc_id_link);
1113 guc_id = ce->guc_id;
1114 clr_context_registered(ce);
1115 set_context_guc_id_invalid(ce);
1122 static int assign_guc_id(struct intel_guc *guc, u16 *out)
1126 lockdep_assert_held(&guc->contexts_lock);
1128 ret = new_guc_id(guc);
1129 if (unlikely(ret < 0)) {
1130 ret = steal_guc_id(guc);
1139 #define PIN_GUC_ID_TRIES 4
1140 static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
1143 unsigned long flags, tries = PIN_GUC_ID_TRIES;
1145 GEM_BUG_ON(atomic_read(&ce->guc_id_ref));
1148 spin_lock_irqsave(&guc->contexts_lock, flags);
1150 if (context_guc_id_invalid(ce)) {
1151 ret = assign_guc_id(guc, &ce->guc_id);
1154 ret = 1; /* Indidcates newly assigned guc_id */
1156 if (!list_empty(&ce->guc_id_link))
1157 list_del_init(&ce->guc_id_link);
1158 atomic_inc(&ce->guc_id_ref);
1161 spin_unlock_irqrestore(&guc->contexts_lock, flags);
1164 * -EAGAIN indicates no guc_ids are available, let's retire any
1165 * outstanding requests to see if that frees up a guc_id. If the first
1166 * retire didn't help, insert a sleep with the timeslice duration before
1167 * attempting to retire more requests. Double the sleep period each
1168 * subsequent pass before finally giving up. The sleep period has max of
1169 * 100ms and minimum of 1ms.
1171 if (ret == -EAGAIN && --tries) {
1172 if (PIN_GUC_ID_TRIES - tries > 1) {
1173 unsigned int timeslice_shifted =
1174 ce->engine->props.timeslice_duration_ms <<
1175 (PIN_GUC_ID_TRIES - tries - 2);
1176 unsigned int max = min_t(unsigned int, 100,
1179 msleep(max_t(unsigned int, max, 1));
1181 intel_gt_retire_requests(guc_to_gt(guc));
1188 static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
1190 unsigned long flags;
1192 GEM_BUG_ON(atomic_read(&ce->guc_id_ref) < 0);
1194 if (unlikely(context_guc_id_invalid(ce)))
1197 spin_lock_irqsave(&guc->contexts_lock, flags);
1198 if (!context_guc_id_invalid(ce) && list_empty(&ce->guc_id_link) &&
1199 !atomic_read(&ce->guc_id_ref))
1200 list_add_tail(&ce->guc_id_link, &guc->guc_id_list);
1201 spin_unlock_irqrestore(&guc->contexts_lock, flags);
1204 static int __guc_action_register_context(struct intel_guc *guc,
1210 INTEL_GUC_ACTION_REGISTER_CONTEXT,
1215 return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
1219 static int register_context(struct intel_context *ce, bool loop)
1221 struct intel_guc *guc = ce_to_guc(ce);
1222 u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool) +
1223 ce->guc_id * sizeof(struct guc_lrc_desc);
1226 trace_intel_context_register(ce);
1228 ret = __guc_action_register_context(guc, ce->guc_id, offset, loop);
1230 set_context_registered(ce);
1235 static int __guc_action_deregister_context(struct intel_guc *guc,
1240 INTEL_GUC_ACTION_DEREGISTER_CONTEXT,
1244 return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
1245 G2H_LEN_DW_DEREGISTER_CONTEXT,
1249 static int deregister_context(struct intel_context *ce, u32 guc_id, bool loop)
1251 struct intel_guc *guc = ce_to_guc(ce);
1253 trace_intel_context_deregister(ce);
1255 return __guc_action_deregister_context(guc, guc_id, loop);
1258 static intel_engine_mask_t adjust_engine_mask(u8 class, intel_engine_mask_t mask)
1262 return mask >> RCS0;
1263 case VIDEO_ENHANCEMENT_CLASS:
1264 return mask >> VECS0;
1265 case VIDEO_DECODE_CLASS:
1266 return mask >> VCS0;
1267 case COPY_ENGINE_CLASS:
1268 return mask >> BCS0;
1270 MISSING_CASE(class);
1275 static void guc_context_policy_init(struct intel_engine_cs *engine,
1276 struct guc_lrc_desc *desc)
1278 desc->policy_flags = 0;
1280 if (engine->flags & I915_ENGINE_WANT_FORCED_PREEMPTION)
1281 desc->policy_flags |= CONTEXT_POLICY_FLAG_PREEMPT_TO_IDLE;
1283 /* NB: For both of these, zero means disabled. */
1284 desc->execution_quantum = engine->props.timeslice_duration_ms * 1000;
1285 desc->preemption_timeout = engine->props.preempt_timeout_ms * 1000;
1288 static inline u8 map_i915_prio_to_guc_prio(int prio);
1290 static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
1292 struct intel_engine_cs *engine = ce->engine;
1293 struct intel_runtime_pm *runtime_pm = engine->uncore->rpm;
1294 struct intel_guc *guc = &engine->gt->uc.guc;
1295 u32 desc_idx = ce->guc_id;
1296 struct guc_lrc_desc *desc;
1297 const struct i915_gem_context *ctx;
1298 int prio = I915_CONTEXT_DEFAULT_PRIORITY;
1299 bool context_registered;
1300 intel_wakeref_t wakeref;
1303 GEM_BUG_ON(!engine->mask);
1306 * Ensure LRC + CT vmas are is same region as write barrier is done
1307 * based on CT vma region.
1309 GEM_BUG_ON(i915_gem_object_is_lmem(guc->ct.vma->obj) !=
1310 i915_gem_object_is_lmem(ce->ring->vma->obj));
1312 context_registered = lrc_desc_registered(guc, desc_idx);
1315 ctx = rcu_dereference(ce->gem_context);
1317 prio = ctx->sched.priority;
1320 reset_lrc_desc(guc, desc_idx);
1321 set_lrc_desc_registered(guc, desc_idx, ce);
1323 desc = __get_lrc_desc(guc, desc_idx);
1324 desc->engine_class = engine_class_to_guc_class(engine->class);
1325 desc->engine_submit_mask = adjust_engine_mask(engine->class,
1327 desc->hw_context_desc = ce->lrc.lrca;
1328 ce->guc_prio = map_i915_prio_to_guc_prio(prio);
1329 desc->priority = ce->guc_prio;
1330 desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
1331 guc_context_policy_init(engine, desc);
1332 init_sched_state(ce);
1335 * The context_lookup xarray is used to determine if the hardware
1336 * context is currently registered. There are two cases in which it
1337 * could be registered either the guc_id has been stolen from another
1338 * context or the lrc descriptor address of this context has changed. In
1339 * either case the context needs to be deregistered with the GuC before
1340 * registering this context.
1342 if (context_registered) {
1343 trace_intel_context_steal_guc_id(ce);
1345 set_context_wait_for_deregister_to_register(ce);
1346 intel_context_get(ce);
1349 unsigned long flags;
1351 /* Seal race with Reset */
1352 spin_lock_irqsave(&ce->guc_state.lock, flags);
1353 disabled = submission_disabled(guc);
1354 if (likely(!disabled)) {
1355 set_context_wait_for_deregister_to_register(ce);
1356 intel_context_get(ce);
1358 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1359 if (unlikely(disabled)) {
1360 reset_lrc_desc(guc, desc_idx);
1361 return 0; /* Will get registered later */
1366 * If stealing the guc_id, this ce has the same guc_id as the
1367 * context whose guc_id was stolen.
1369 with_intel_runtime_pm(runtime_pm, wakeref)
1370 ret = deregister_context(ce, ce->guc_id, loop);
1371 if (unlikely(ret == -EBUSY)) {
1372 clr_context_wait_for_deregister_to_register(ce);
1373 intel_context_put(ce);
1374 } else if (unlikely(ret == -ENODEV)) {
1375 ret = 0; /* Will get registered later */
1378 with_intel_runtime_pm(runtime_pm, wakeref)
1379 ret = register_context(ce, loop);
1380 if (unlikely(ret == -EBUSY))
1381 reset_lrc_desc(guc, desc_idx);
1382 else if (unlikely(ret == -ENODEV))
1383 ret = 0; /* Will get registered later */
1389 static int __guc_context_pre_pin(struct intel_context *ce,
1390 struct intel_engine_cs *engine,
1391 struct i915_gem_ww_ctx *ww,
1394 return lrc_pre_pin(ce, engine, ww, vaddr);
1397 static int __guc_context_pin(struct intel_context *ce,
1398 struct intel_engine_cs *engine,
1401 if (i915_ggtt_offset(ce->state) !=
1402 (ce->lrc.lrca & CTX_GTT_ADDRESS_MASK))
1403 set_bit(CONTEXT_LRCA_DIRTY, &ce->flags);
1406 * GuC context gets pinned in guc_request_alloc. See that function for
1407 * explaination of why.
1410 return lrc_pin(ce, engine, vaddr);
1413 static int guc_context_pre_pin(struct intel_context *ce,
1414 struct i915_gem_ww_ctx *ww,
1417 return __guc_context_pre_pin(ce, ce->engine, ww, vaddr);
1420 static int guc_context_pin(struct intel_context *ce, void *vaddr)
1422 return __guc_context_pin(ce, ce->engine, vaddr);
1425 static void guc_context_unpin(struct intel_context *ce)
1427 struct intel_guc *guc = ce_to_guc(ce);
1429 unpin_guc_id(guc, ce);
1433 static void guc_context_post_unpin(struct intel_context *ce)
1438 static void __guc_context_sched_enable(struct intel_guc *guc,
1439 struct intel_context *ce)
1442 INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET,
1447 trace_intel_context_sched_enable(ce);
1449 guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
1450 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
1453 static void __guc_context_sched_disable(struct intel_guc *guc,
1454 struct intel_context *ce,
1458 INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET,
1459 guc_id, /* ce->guc_id not stable */
1463 GEM_BUG_ON(guc_id == GUC_INVALID_LRC_ID);
1465 trace_intel_context_sched_disable(ce);
1467 guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
1468 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
1471 static void guc_blocked_fence_complete(struct intel_context *ce)
1473 lockdep_assert_held(&ce->guc_state.lock);
1475 if (!i915_sw_fence_done(&ce->guc_blocked))
1476 i915_sw_fence_complete(&ce->guc_blocked);
1479 static void guc_blocked_fence_reinit(struct intel_context *ce)
1481 lockdep_assert_held(&ce->guc_state.lock);
1482 GEM_BUG_ON(!i915_sw_fence_done(&ce->guc_blocked));
1485 * This fence is always complete unless a pending schedule disable is
1486 * outstanding. We arm the fence here and complete it when we receive
1487 * the pending schedule disable complete message.
1489 i915_sw_fence_fini(&ce->guc_blocked);
1490 i915_sw_fence_reinit(&ce->guc_blocked);
1491 i915_sw_fence_await(&ce->guc_blocked);
1492 i915_sw_fence_commit(&ce->guc_blocked);
1495 static u16 prep_context_pending_disable(struct intel_context *ce)
1497 lockdep_assert_held(&ce->guc_state.lock);
1499 set_context_pending_disable(ce);
1500 clr_context_enabled(ce);
1501 guc_blocked_fence_reinit(ce);
1502 intel_context_get(ce);
1507 static struct i915_sw_fence *guc_context_block(struct intel_context *ce)
1509 struct intel_guc *guc = ce_to_guc(ce);
1510 struct i915_sched_engine *sched_engine = ce->engine->sched_engine;
1511 unsigned long flags;
1512 struct intel_runtime_pm *runtime_pm = ce->engine->uncore->rpm;
1513 intel_wakeref_t wakeref;
1517 spin_lock_irqsave(&ce->guc_state.lock, flags);
1520 * Sync with submission path, increment before below changes to context
1523 spin_lock(&sched_engine->lock);
1524 incr_context_blocked(ce);
1525 spin_unlock(&sched_engine->lock);
1527 enabled = context_enabled(ce);
1528 if (unlikely(!enabled || submission_disabled(guc))) {
1530 clr_context_enabled(ce);
1531 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1532 return &ce->guc_blocked;
1536 * We add +2 here as the schedule disable complete CTB handler calls
1537 * intel_context_sched_disable_unpin (-2 to pin_count).
1539 atomic_add(2, &ce->pin_count);
1541 guc_id = prep_context_pending_disable(ce);
1543 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1545 with_intel_runtime_pm(runtime_pm, wakeref)
1546 __guc_context_sched_disable(guc, ce, guc_id);
1548 return &ce->guc_blocked;
1551 static void guc_context_unblock(struct intel_context *ce)
1553 struct intel_guc *guc = ce_to_guc(ce);
1554 struct i915_sched_engine *sched_engine = ce->engine->sched_engine;
1555 unsigned long flags;
1556 struct intel_runtime_pm *runtime_pm = ce->engine->uncore->rpm;
1557 intel_wakeref_t wakeref;
1560 GEM_BUG_ON(context_enabled(ce));
1562 spin_lock_irqsave(&ce->guc_state.lock, flags);
1564 if (unlikely(submission_disabled(guc) ||
1565 !intel_context_is_pinned(ce) ||
1566 context_pending_disable(ce) ||
1567 context_blocked(ce) > 1)) {
1571 set_context_pending_enable(ce);
1572 set_context_enabled(ce);
1573 intel_context_get(ce);
1577 * Sync with submission path, decrement after above changes to context
1580 spin_lock(&sched_engine->lock);
1581 decr_context_blocked(ce);
1582 spin_unlock(&sched_engine->lock);
1584 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1587 with_intel_runtime_pm(runtime_pm, wakeref)
1588 __guc_context_sched_enable(guc, ce);
1592 static void guc_context_cancel_request(struct intel_context *ce,
1593 struct i915_request *rq)
1595 if (i915_sw_fence_signaled(&rq->submit)) {
1596 struct i915_sw_fence *fence = guc_context_block(ce);
1598 i915_sw_fence_wait(fence);
1599 if (!i915_request_completed(rq)) {
1600 __i915_request_skip(rq);
1601 guc_reset_state(ce, intel_ring_wrap(ce->ring, rq->head),
1604 guc_context_unblock(ce);
1608 static void __guc_context_set_preemption_timeout(struct intel_guc *guc,
1610 u32 preemption_timeout)
1613 INTEL_GUC_ACTION_SET_CONTEXT_PREEMPTION_TIMEOUT,
1618 intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);
1621 static void guc_context_ban(struct intel_context *ce, struct i915_request *rq)
1623 struct intel_guc *guc = ce_to_guc(ce);
1624 struct intel_runtime_pm *runtime_pm =
1625 &ce->engine->gt->i915->runtime_pm;
1626 intel_wakeref_t wakeref;
1627 unsigned long flags;
1629 guc_flush_submissions(guc);
1631 spin_lock_irqsave(&ce->guc_state.lock, flags);
1632 set_context_banned(ce);
1634 if (submission_disabled(guc) ||
1635 (!context_enabled(ce) && !context_pending_disable(ce))) {
1636 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1638 guc_cancel_context_requests(ce);
1639 intel_engine_signal_breadcrumbs(ce->engine);
1640 } else if (!context_pending_disable(ce)) {
1644 * We add +2 here as the schedule disable complete CTB handler
1645 * calls intel_context_sched_disable_unpin (-2 to pin_count).
1647 atomic_add(2, &ce->pin_count);
1649 guc_id = prep_context_pending_disable(ce);
1650 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1653 * In addition to disabling scheduling, set the preemption
1654 * timeout to the minimum value (1 us) so the banned context
1655 * gets kicked off the HW ASAP.
1657 with_intel_runtime_pm(runtime_pm, wakeref) {
1658 __guc_context_set_preemption_timeout(guc, guc_id, 1);
1659 __guc_context_sched_disable(guc, ce, guc_id);
1662 if (!context_guc_id_invalid(ce))
1663 with_intel_runtime_pm(runtime_pm, wakeref)
1664 __guc_context_set_preemption_timeout(guc,
1667 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1671 static void guc_context_sched_disable(struct intel_context *ce)
1673 struct intel_guc *guc = ce_to_guc(ce);
1674 unsigned long flags;
1675 struct intel_runtime_pm *runtime_pm = &ce->engine->gt->i915->runtime_pm;
1676 intel_wakeref_t wakeref;
1680 if (submission_disabled(guc) || context_guc_id_invalid(ce) ||
1681 !lrc_desc_registered(guc, ce->guc_id)) {
1682 clr_context_enabled(ce);
1686 if (!context_enabled(ce))
1689 spin_lock_irqsave(&ce->guc_state.lock, flags);
1692 * We have to check if the context has been disabled by another thread.
1693 * We also have to check if the context has been pinned again as another
1694 * pin operation is allowed to pass this function. Checking the pin
1695 * count, within ce->guc_state.lock, synchronizes this function with
1696 * guc_request_alloc ensuring a request doesn't slip through the
1697 * 'context_pending_disable' fence. Checking within the spin lock (can't
1698 * sleep) ensures another process doesn't pin this context and generate
1699 * a request before we set the 'context_pending_disable' flag here.
1701 enabled = context_enabled(ce);
1702 if (unlikely(!enabled || submission_disabled(guc))) {
1704 clr_context_enabled(ce);
1705 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1708 if (unlikely(atomic_add_unless(&ce->pin_count, -2, 2))) {
1709 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1712 guc_id = prep_context_pending_disable(ce);
1714 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1716 with_intel_runtime_pm(runtime_pm, wakeref)
1717 __guc_context_sched_disable(guc, ce, guc_id);
1721 intel_context_sched_disable_unpin(ce);
1724 static inline void guc_lrc_desc_unpin(struct intel_context *ce)
1726 struct intel_guc *guc = ce_to_guc(ce);
1728 GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id));
1729 GEM_BUG_ON(ce != __get_context(guc, ce->guc_id));
1730 GEM_BUG_ON(context_enabled(ce));
1732 clr_context_registered(ce);
1733 deregister_context(ce, ce->guc_id, true);
1736 static void __guc_context_destroy(struct intel_context *ce)
1738 GEM_BUG_ON(ce->guc_prio_count[GUC_CLIENT_PRIORITY_KMD_HIGH] ||
1739 ce->guc_prio_count[GUC_CLIENT_PRIORITY_HIGH] ||
1740 ce->guc_prio_count[GUC_CLIENT_PRIORITY_KMD_NORMAL] ||
1741 ce->guc_prio_count[GUC_CLIENT_PRIORITY_NORMAL]);
1744 intel_context_fini(ce);
1746 if (intel_engine_is_virtual(ce->engine)) {
1747 struct guc_virtual_engine *ve =
1748 container_of(ce, typeof(*ve), context);
1750 if (ve->base.breadcrumbs)
1751 intel_breadcrumbs_put(ve->base.breadcrumbs);
1755 intel_context_free(ce);
1759 static void guc_context_destroy(struct kref *kref)
1761 struct intel_context *ce = container_of(kref, typeof(*ce), ref);
1762 struct intel_runtime_pm *runtime_pm = ce->engine->uncore->rpm;
1763 struct intel_guc *guc = ce_to_guc(ce);
1764 intel_wakeref_t wakeref;
1765 unsigned long flags;
1769 * If the guc_id is invalid this context has been stolen and we can free
1770 * it immediately. Also can be freed immediately if the context is not
1771 * registered with the GuC or the GuC is in the middle of a reset.
1773 if (context_guc_id_invalid(ce)) {
1774 __guc_context_destroy(ce);
1776 } else if (submission_disabled(guc) ||
1777 !lrc_desc_registered(guc, ce->guc_id)) {
1778 release_guc_id(guc, ce);
1779 __guc_context_destroy(ce);
1784 * We have to acquire the context spinlock and check guc_id again, if it
1785 * is valid it hasn't been stolen and needs to be deregistered. We
1786 * delete this context from the list of unpinned guc_ids available to
1787 * steal to seal a race with guc_lrc_desc_pin(). When the G2H CTB
1788 * returns indicating this context has been deregistered the guc_id is
1789 * returned to the pool of available guc_ids.
1791 spin_lock_irqsave(&guc->contexts_lock, flags);
1792 if (context_guc_id_invalid(ce)) {
1793 spin_unlock_irqrestore(&guc->contexts_lock, flags);
1794 __guc_context_destroy(ce);
1798 if (!list_empty(&ce->guc_id_link))
1799 list_del_init(&ce->guc_id_link);
1800 spin_unlock_irqrestore(&guc->contexts_lock, flags);
1802 /* Seal race with Reset */
1803 spin_lock_irqsave(&ce->guc_state.lock, flags);
1804 disabled = submission_disabled(guc);
1805 if (likely(!disabled))
1806 set_context_destroyed(ce);
1807 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1808 if (unlikely(disabled)) {
1809 release_guc_id(guc, ce);
1810 __guc_context_destroy(ce);
1815 * We defer GuC context deregistration until the context is destroyed
1816 * in order to save on CTBs. With this optimization ideally we only need
1817 * 1 CTB to register the context during the first pin and 1 CTB to
1818 * deregister the context when the context is destroyed. Without this
1819 * optimization, a CTB would be needed every pin & unpin.
1821 * XXX: Need to acqiure the runtime wakeref as this can be triggered
1822 * from context_free_worker when runtime wakeref is not held.
1823 * guc_lrc_desc_unpin requires the runtime as a GuC register is written
1824 * in H2G CTB to deregister the context. A future patch may defer this
1825 * H2G CTB if the runtime wakeref is zero.
1827 with_intel_runtime_pm(runtime_pm, wakeref)
1828 guc_lrc_desc_unpin(ce);
1831 static int guc_context_alloc(struct intel_context *ce)
1833 return lrc_alloc(ce, ce->engine);
1836 static void guc_context_set_prio(struct intel_guc *guc,
1837 struct intel_context *ce,
1841 INTEL_GUC_ACTION_SET_CONTEXT_PRIORITY,
1846 GEM_BUG_ON(prio < GUC_CLIENT_PRIORITY_KMD_HIGH ||
1847 prio > GUC_CLIENT_PRIORITY_NORMAL);
1849 if (ce->guc_prio == prio || submission_disabled(guc) ||
1850 !context_registered(ce))
1853 guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);
1855 ce->guc_prio = prio;
1856 trace_intel_context_set_prio(ce);
1859 static inline u8 map_i915_prio_to_guc_prio(int prio)
1861 if (prio == I915_PRIORITY_NORMAL)
1862 return GUC_CLIENT_PRIORITY_KMD_NORMAL;
1863 else if (prio < I915_PRIORITY_NORMAL)
1864 return GUC_CLIENT_PRIORITY_NORMAL;
1865 else if (prio < I915_PRIORITY_DISPLAY)
1866 return GUC_CLIENT_PRIORITY_HIGH;
1868 return GUC_CLIENT_PRIORITY_KMD_HIGH;
1871 static inline void add_context_inflight_prio(struct intel_context *ce,
1874 lockdep_assert_held(&ce->guc_active.lock);
1875 GEM_BUG_ON(guc_prio >= ARRAY_SIZE(ce->guc_prio_count));
1877 ++ce->guc_prio_count[guc_prio];
1879 /* Overflow protection */
1880 GEM_WARN_ON(!ce->guc_prio_count[guc_prio]);
1883 static inline void sub_context_inflight_prio(struct intel_context *ce,
1886 lockdep_assert_held(&ce->guc_active.lock);
1887 GEM_BUG_ON(guc_prio >= ARRAY_SIZE(ce->guc_prio_count));
1889 /* Underflow protection */
1890 GEM_WARN_ON(!ce->guc_prio_count[guc_prio]);
1892 --ce->guc_prio_count[guc_prio];
1895 static inline void update_context_prio(struct intel_context *ce)
1897 struct intel_guc *guc = &ce->engine->gt->uc.guc;
1900 BUILD_BUG_ON(GUC_CLIENT_PRIORITY_KMD_HIGH != 0);
1901 BUILD_BUG_ON(GUC_CLIENT_PRIORITY_KMD_HIGH > GUC_CLIENT_PRIORITY_NORMAL);
1903 lockdep_assert_held(&ce->guc_active.lock);
1905 for (i = 0; i < ARRAY_SIZE(ce->guc_prio_count); ++i) {
1906 if (ce->guc_prio_count[i]) {
1907 guc_context_set_prio(guc, ce, i);
1913 static inline bool new_guc_prio_higher(u8 old_guc_prio, u8 new_guc_prio)
1915 /* Lower value is higher priority */
1916 return new_guc_prio < old_guc_prio;
1919 static void add_to_context(struct i915_request *rq)
1921 struct intel_context *ce = rq->context;
1922 u8 new_guc_prio = map_i915_prio_to_guc_prio(rq_prio(rq));
1924 GEM_BUG_ON(rq->guc_prio == GUC_PRIO_FINI);
1926 spin_lock(&ce->guc_active.lock);
1927 list_move_tail(&rq->sched.link, &ce->guc_active.requests);
1929 if (rq->guc_prio == GUC_PRIO_INIT) {
1930 rq->guc_prio = new_guc_prio;
1931 add_context_inflight_prio(ce, rq->guc_prio);
1932 } else if (new_guc_prio_higher(rq->guc_prio, new_guc_prio)) {
1933 sub_context_inflight_prio(ce, rq->guc_prio);
1934 rq->guc_prio = new_guc_prio;
1935 add_context_inflight_prio(ce, rq->guc_prio);
1937 update_context_prio(ce);
1939 spin_unlock(&ce->guc_active.lock);
1942 static void guc_prio_fini(struct i915_request *rq, struct intel_context *ce)
1944 lockdep_assert_held(&ce->guc_active.lock);
1946 if (rq->guc_prio != GUC_PRIO_INIT &&
1947 rq->guc_prio != GUC_PRIO_FINI) {
1948 sub_context_inflight_prio(ce, rq->guc_prio);
1949 update_context_prio(ce);
1951 rq->guc_prio = GUC_PRIO_FINI;
1954 static void remove_from_context(struct i915_request *rq)
1956 struct intel_context *ce = rq->context;
1958 spin_lock_irq(&ce->guc_active.lock);
1960 list_del_init(&rq->sched.link);
1961 clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
1963 /* Prevent further __await_execution() registering a cb, then flush */
1964 set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
1966 guc_prio_fini(rq, ce);
1968 spin_unlock_irq(&ce->guc_active.lock);
1970 atomic_dec(&ce->guc_id_ref);
1971 i915_request_notify_execute_cb_imm(rq);
1974 static const struct intel_context_ops guc_context_ops = {
1975 .alloc = guc_context_alloc,
1977 .pre_pin = guc_context_pre_pin,
1978 .pin = guc_context_pin,
1979 .unpin = guc_context_unpin,
1980 .post_unpin = guc_context_post_unpin,
1982 .ban = guc_context_ban,
1984 .cancel_request = guc_context_cancel_request,
1986 .enter = intel_context_enter_engine,
1987 .exit = intel_context_exit_engine,
1989 .sched_disable = guc_context_sched_disable,
1992 .destroy = guc_context_destroy,
1994 .create_virtual = guc_create_virtual,
1997 static void __guc_signal_context_fence(struct intel_context *ce)
1999 struct i915_request *rq;
2001 lockdep_assert_held(&ce->guc_state.lock);
2003 if (!list_empty(&ce->guc_state.fences))
2004 trace_intel_context_fence_release(ce);
2006 list_for_each_entry(rq, &ce->guc_state.fences, guc_fence_link)
2007 i915_sw_fence_complete(&rq->submit);
2009 INIT_LIST_HEAD(&ce->guc_state.fences);
2012 static void guc_signal_context_fence(struct intel_context *ce)
2014 unsigned long flags;
2016 spin_lock_irqsave(&ce->guc_state.lock, flags);
2017 clr_context_wait_for_deregister_to_register(ce);
2018 __guc_signal_context_fence(ce);
2019 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2022 static bool context_needs_register(struct intel_context *ce, bool new_guc_id)
2024 return (new_guc_id || test_bit(CONTEXT_LRCA_DIRTY, &ce->flags) ||
2025 !lrc_desc_registered(ce_to_guc(ce), ce->guc_id)) &&
2026 !submission_disabled(ce_to_guc(ce));
2029 static int guc_request_alloc(struct i915_request *rq)
2031 struct intel_context *ce = rq->context;
2032 struct intel_guc *guc = ce_to_guc(ce);
2033 unsigned long flags;
2036 GEM_BUG_ON(!intel_context_is_pinned(rq->context));
2039 * Flush enough space to reduce the likelihood of waiting after
2040 * we start building the request - in which case we will just
2041 * have to repeat work.
2043 rq->reserved_space += GUC_REQUEST_SIZE;
2046 * Note that after this point, we have committed to using
2047 * this request as it is being used to both track the
2048 * state of engine initialisation and liveness of the
2049 * golden renderstate above. Think twice before you try
2050 * to cancel/unwind this request now.
2053 /* Unconditionally invalidate GPU caches and TLBs. */
2054 ret = rq->engine->emit_flush(rq, EMIT_INVALIDATE);
2058 rq->reserved_space -= GUC_REQUEST_SIZE;
2061 * Call pin_guc_id here rather than in the pinning step as with
2062 * dma_resv, contexts can be repeatedly pinned / unpinned trashing the
2063 * guc_ids and creating horrible race conditions. This is especially bad
2064 * when guc_ids are being stolen due to over subscription. By the time
2065 * this function is reached, it is guaranteed that the guc_id will be
2066 * persistent until the generated request is retired. Thus, sealing these
2067 * race conditions. It is still safe to fail here if guc_ids are
2068 * exhausted and return -EAGAIN to the user indicating that they can try
2069 * again in the future.
2071 * There is no need for a lock here as the timeline mutex ensures at
2072 * most one context can be executing this code path at once. The
2073 * guc_id_ref is incremented once for every request in flight and
2074 * decremented on each retire. When it is zero, a lock around the
2075 * increment (in pin_guc_id) is needed to seal a race with unpin_guc_id.
2077 if (atomic_add_unless(&ce->guc_id_ref, 1, 0))
2080 ret = pin_guc_id(guc, ce); /* returns 1 if new guc_id assigned */
2081 if (unlikely(ret < 0))
2083 if (context_needs_register(ce, !!ret)) {
2084 ret = guc_lrc_desc_pin(ce, true);
2085 if (unlikely(ret)) { /* unwind */
2086 if (ret == -EPIPE) {
2087 disable_submission(guc);
2088 goto out; /* GPU will be reset */
2090 atomic_dec(&ce->guc_id_ref);
2091 unpin_guc_id(guc, ce);
2096 clear_bit(CONTEXT_LRCA_DIRTY, &ce->flags);
2100 * We block all requests on this context if a G2H is pending for a
2101 * schedule disable or context deregistration as the GuC will fail a
2102 * schedule enable or context registration if either G2H is pending
2103 * respectfully. Once a G2H returns, the fence is released that is
2104 * blocking these requests (see guc_signal_context_fence).
2106 * We can safely check the below fields outside of the lock as it isn't
2107 * possible for these fields to transition from being clear to set but
2108 * converse is possible, hence the need for the check within the lock.
2110 if (likely(!context_wait_for_deregister_to_register(ce) &&
2111 !context_pending_disable(ce)))
2114 spin_lock_irqsave(&ce->guc_state.lock, flags);
2115 if (context_wait_for_deregister_to_register(ce) ||
2116 context_pending_disable(ce)) {
2117 i915_sw_fence_await(&rq->submit);
2119 list_add_tail(&rq->guc_fence_link, &ce->guc_state.fences);
2121 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2126 static int guc_virtual_context_pre_pin(struct intel_context *ce,
2127 struct i915_gem_ww_ctx *ww,
2130 struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
2132 return __guc_context_pre_pin(ce, engine, ww, vaddr);
2135 static int guc_virtual_context_pin(struct intel_context *ce, void *vaddr)
2137 struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
2139 return __guc_context_pin(ce, engine, vaddr);
2142 static void guc_virtual_context_enter(struct intel_context *ce)
2144 intel_engine_mask_t tmp, mask = ce->engine->mask;
2145 struct intel_engine_cs *engine;
2147 for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
2148 intel_engine_pm_get(engine);
2150 intel_timeline_enter(ce->timeline);
2153 static void guc_virtual_context_exit(struct intel_context *ce)
2155 intel_engine_mask_t tmp, mask = ce->engine->mask;
2156 struct intel_engine_cs *engine;
2158 for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
2159 intel_engine_pm_put(engine);
2161 intel_timeline_exit(ce->timeline);
2164 static int guc_virtual_context_alloc(struct intel_context *ce)
2166 struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
2168 return lrc_alloc(ce, engine);
2171 static const struct intel_context_ops virtual_guc_context_ops = {
2172 .alloc = guc_virtual_context_alloc,
2174 .pre_pin = guc_virtual_context_pre_pin,
2175 .pin = guc_virtual_context_pin,
2176 .unpin = guc_context_unpin,
2177 .post_unpin = guc_context_post_unpin,
2179 .ban = guc_context_ban,
2181 .cancel_request = guc_context_cancel_request,
2183 .enter = guc_virtual_context_enter,
2184 .exit = guc_virtual_context_exit,
2186 .sched_disable = guc_context_sched_disable,
2188 .destroy = guc_context_destroy,
2190 .get_sibling = guc_virtual_get_sibling,
2194 guc_irq_enable_breadcrumbs(struct intel_breadcrumbs *b)
2196 struct intel_engine_cs *sibling;
2197 intel_engine_mask_t tmp, mask = b->engine_mask;
2198 bool result = false;
2200 for_each_engine_masked(sibling, b->irq_engine->gt, mask, tmp)
2201 result |= intel_engine_irq_enable(sibling);
2207 guc_irq_disable_breadcrumbs(struct intel_breadcrumbs *b)
2209 struct intel_engine_cs *sibling;
2210 intel_engine_mask_t tmp, mask = b->engine_mask;
2212 for_each_engine_masked(sibling, b->irq_engine->gt, mask, tmp)
2213 intel_engine_irq_disable(sibling);
2216 static void guc_init_breadcrumbs(struct intel_engine_cs *engine)
2221 * In GuC submission mode we do not know which physical engine a request
2222 * will be scheduled on, this creates a problem because the breadcrumb
2223 * interrupt is per physical engine. To work around this we attach
2224 * requests and direct all breadcrumb interrupts to the first instance
2225 * of an engine per class. In addition all breadcrumb interrupts are
2226 * enabled / disabled across an engine class in unison.
2228 for (i = 0; i < MAX_ENGINE_INSTANCE; ++i) {
2229 struct intel_engine_cs *sibling =
2230 engine->gt->engine_class[engine->class][i];
2233 if (engine->breadcrumbs != sibling->breadcrumbs) {
2234 intel_breadcrumbs_put(engine->breadcrumbs);
2235 engine->breadcrumbs =
2236 intel_breadcrumbs_get(sibling->breadcrumbs);
2242 if (engine->breadcrumbs) {
2243 engine->breadcrumbs->engine_mask |= engine->mask;
2244 engine->breadcrumbs->irq_enable = guc_irq_enable_breadcrumbs;
2245 engine->breadcrumbs->irq_disable = guc_irq_disable_breadcrumbs;
2249 static void guc_bump_inflight_request_prio(struct i915_request *rq,
2252 struct intel_context *ce = rq->context;
2253 u8 new_guc_prio = map_i915_prio_to_guc_prio(prio);
2255 /* Short circuit function */
2256 if (prio < I915_PRIORITY_NORMAL ||
2257 rq->guc_prio == GUC_PRIO_FINI ||
2258 (rq->guc_prio != GUC_PRIO_INIT &&
2259 !new_guc_prio_higher(rq->guc_prio, new_guc_prio)))
2262 spin_lock(&ce->guc_active.lock);
2263 if (rq->guc_prio != GUC_PRIO_FINI) {
2264 if (rq->guc_prio != GUC_PRIO_INIT)
2265 sub_context_inflight_prio(ce, rq->guc_prio);
2266 rq->guc_prio = new_guc_prio;
2267 add_context_inflight_prio(ce, rq->guc_prio);
2268 update_context_prio(ce);
2270 spin_unlock(&ce->guc_active.lock);
2273 static void guc_retire_inflight_request_prio(struct i915_request *rq)
2275 struct intel_context *ce = rq->context;
2277 spin_lock(&ce->guc_active.lock);
2278 guc_prio_fini(rq, ce);
2279 spin_unlock(&ce->guc_active.lock);
2282 static void sanitize_hwsp(struct intel_engine_cs *engine)
2284 struct intel_timeline *tl;
2286 list_for_each_entry(tl, &engine->status_page.timelines, engine_link)
2287 intel_timeline_reset_seqno(tl);
2290 static void guc_sanitize(struct intel_engine_cs *engine)
2293 * Poison residual state on resume, in case the suspend didn't!
2295 * We have to assume that across suspend/resume (or other loss
2296 * of control) that the contents of our pinned buffers has been
2297 * lost, replaced by garbage. Since this doesn't always happen,
2298 * let's poison such state so that we more quickly spot when
2299 * we falsely assume it has been preserved.
2301 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
2302 memset(engine->status_page.addr, POISON_INUSE, PAGE_SIZE);
2305 * The kernel_context HWSP is stored in the status_page. As above,
2306 * that may be lost on resume/initialisation, and so we need to
2307 * reset the value in the HWSP.
2309 sanitize_hwsp(engine);
2311 /* And scrub the dirty cachelines for the HWSP */
2312 clflush_cache_range(engine->status_page.addr, PAGE_SIZE);
2315 static void setup_hwsp(struct intel_engine_cs *engine)
2317 intel_engine_set_hwsp_writemask(engine, ~0u); /* HWSTAM */
2319 ENGINE_WRITE_FW(engine,
2321 i915_ggtt_offset(engine->status_page.vma));
2324 static void start_engine(struct intel_engine_cs *engine)
2326 ENGINE_WRITE_FW(engine,
2328 _MASKED_BIT_ENABLE(GEN11_GFX_DISABLE_LEGACY_MODE));
2330 ENGINE_WRITE_FW(engine, RING_MI_MODE, _MASKED_BIT_DISABLE(STOP_RING));
2331 ENGINE_POSTING_READ(engine, RING_MI_MODE);
2334 static int guc_resume(struct intel_engine_cs *engine)
2336 assert_forcewakes_active(engine->uncore, FORCEWAKE_ALL);
2338 intel_mocs_init_engine(engine);
2340 intel_breadcrumbs_reset(engine->breadcrumbs);
2343 start_engine(engine);
2348 static bool guc_sched_engine_disabled(struct i915_sched_engine *sched_engine)
2350 return !sched_engine->tasklet.callback;
2353 static void guc_set_default_submission(struct intel_engine_cs *engine)
2355 engine->submit_request = guc_submit_request;
2358 static inline void guc_kernel_context_pin(struct intel_guc *guc,
2359 struct intel_context *ce)
2361 if (context_guc_id_invalid(ce))
2362 pin_guc_id(guc, ce);
2363 guc_lrc_desc_pin(ce, true);
2366 static inline void guc_init_lrc_mapping(struct intel_guc *guc)
2368 struct intel_gt *gt = guc_to_gt(guc);
2369 struct intel_engine_cs *engine;
2370 enum intel_engine_id id;
2372 /* make sure all descriptors are clean... */
2373 xa_destroy(&guc->context_lookup);
2376 * Some contexts might have been pinned before we enabled GuC
2377 * submission, so we need to add them to the GuC bookeeping.
2378 * Also, after a reset the of the GuC we want to make sure that the
2379 * information shared with GuC is properly reset. The kernel LRCs are
2380 * not attached to the gem_context, so they need to be added separately.
2382 * Note: we purposefully do not check the return of guc_lrc_desc_pin,
2383 * because that function can only fail if a reset is just starting. This
2384 * is at the end of reset so presumably another reset isn't happening
2385 * and even it did this code would be run again.
2388 for_each_engine(engine, gt, id)
2389 if (engine->kernel_context)
2390 guc_kernel_context_pin(guc, engine->kernel_context);
2393 static void guc_release(struct intel_engine_cs *engine)
2395 engine->sanitize = NULL; /* no longer in control, nothing to sanitize */
2397 intel_engine_cleanup_common(engine);
2398 lrc_fini_wa_ctx(engine);
2401 static void virtual_guc_bump_serial(struct intel_engine_cs *engine)
2403 struct intel_engine_cs *e;
2404 intel_engine_mask_t tmp, mask = engine->mask;
2406 for_each_engine_masked(e, engine->gt, mask, tmp)
2410 static void guc_default_vfuncs(struct intel_engine_cs *engine)
2412 /* Default vfuncs which can be overridden by each engine. */
2414 engine->resume = guc_resume;
2416 engine->cops = &guc_context_ops;
2417 engine->request_alloc = guc_request_alloc;
2418 engine->add_active_request = add_to_context;
2419 engine->remove_active_request = remove_from_context;
2421 engine->sched_engine->schedule = i915_schedule;
2423 engine->reset.prepare = guc_reset_nop;
2424 engine->reset.rewind = guc_rewind_nop;
2425 engine->reset.cancel = guc_reset_nop;
2426 engine->reset.finish = guc_reset_nop;
2428 engine->emit_flush = gen8_emit_flush_xcs;
2429 engine->emit_init_breadcrumb = gen8_emit_init_breadcrumb;
2430 engine->emit_fini_breadcrumb = gen8_emit_fini_breadcrumb_xcs;
2431 if (GRAPHICS_VER(engine->i915) >= 12) {
2432 engine->emit_fini_breadcrumb = gen12_emit_fini_breadcrumb_xcs;
2433 engine->emit_flush = gen12_emit_flush_xcs;
2435 engine->set_default_submission = guc_set_default_submission;
2437 engine->flags |= I915_ENGINE_HAS_PREEMPTION;
2438 engine->flags |= I915_ENGINE_HAS_TIMESLICES;
2441 * TODO: GuC supports timeslicing and semaphores as well, but they're
2442 * handled by the firmware so some minor tweaks are required before
2445 * engine->flags |= I915_ENGINE_HAS_SEMAPHORES;
2448 engine->emit_bb_start = gen8_emit_bb_start;
2451 static void rcs_submission_override(struct intel_engine_cs *engine)
2453 switch (GRAPHICS_VER(engine->i915)) {
2455 engine->emit_flush = gen12_emit_flush_rcs;
2456 engine->emit_fini_breadcrumb = gen12_emit_fini_breadcrumb_rcs;
2459 engine->emit_flush = gen11_emit_flush_rcs;
2460 engine->emit_fini_breadcrumb = gen11_emit_fini_breadcrumb_rcs;
2463 engine->emit_flush = gen8_emit_flush_rcs;
2464 engine->emit_fini_breadcrumb = gen8_emit_fini_breadcrumb_rcs;
2469 static inline void guc_default_irqs(struct intel_engine_cs *engine)
2471 engine->irq_keep_mask = GT_RENDER_USER_INTERRUPT;
2472 intel_engine_set_irq_handler(engine, cs_irq_handler);
2475 static void guc_sched_engine_destroy(struct kref *kref)
2477 struct i915_sched_engine *sched_engine =
2478 container_of(kref, typeof(*sched_engine), ref);
2479 struct intel_guc *guc = sched_engine->private_data;
2481 guc->sched_engine = NULL;
2482 tasklet_kill(&sched_engine->tasklet); /* flush the callback */
2483 kfree(sched_engine);
2486 int intel_guc_submission_setup(struct intel_engine_cs *engine)
2488 struct drm_i915_private *i915 = engine->i915;
2489 struct intel_guc *guc = &engine->gt->uc.guc;
2492 * The setup relies on several assumptions (e.g. irqs always enabled)
2493 * that are only valid on gen11+
2495 GEM_BUG_ON(GRAPHICS_VER(i915) < 11);
2497 if (!guc->sched_engine) {
2498 guc->sched_engine = i915_sched_engine_create(ENGINE_VIRTUAL);
2499 if (!guc->sched_engine)
2502 guc->sched_engine->schedule = i915_schedule;
2503 guc->sched_engine->disabled = guc_sched_engine_disabled;
2504 guc->sched_engine->private_data = guc;
2505 guc->sched_engine->destroy = guc_sched_engine_destroy;
2506 guc->sched_engine->bump_inflight_request_prio =
2507 guc_bump_inflight_request_prio;
2508 guc->sched_engine->retire_inflight_request_prio =
2509 guc_retire_inflight_request_prio;
2510 tasklet_setup(&guc->sched_engine->tasklet,
2511 guc_submission_tasklet);
2513 i915_sched_engine_put(engine->sched_engine);
2514 engine->sched_engine = i915_sched_engine_get(guc->sched_engine);
2516 guc_default_vfuncs(engine);
2517 guc_default_irqs(engine);
2518 guc_init_breadcrumbs(engine);
2520 if (engine->class == RENDER_CLASS)
2521 rcs_submission_override(engine);
2523 lrc_init_wa_ctx(engine);
2525 /* Finally, take ownership and responsibility for cleanup! */
2526 engine->sanitize = guc_sanitize;
2527 engine->release = guc_release;
2532 void intel_guc_submission_enable(struct intel_guc *guc)
2534 guc_init_lrc_mapping(guc);
2537 void intel_guc_submission_disable(struct intel_guc *guc)
2539 /* Note: By the time we're here, GuC may have already been reset */
2542 static bool __guc_submission_supported(struct intel_guc *guc)
2544 /* GuC submission is unavailable for pre-Gen11 */
2545 return intel_guc_is_supported(guc) &&
2546 GRAPHICS_VER(guc_to_gt(guc)->i915) >= 11;
2549 static bool __guc_submission_selected(struct intel_guc *guc)
2551 struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
2553 if (!intel_guc_submission_is_supported(guc))
2556 return i915->params.enable_guc & ENABLE_GUC_SUBMISSION;
2559 void intel_guc_submission_init_early(struct intel_guc *guc)
2561 guc->submission_supported = __guc_submission_supported(guc);
2562 guc->submission_selected = __guc_submission_selected(guc);
2565 static inline struct intel_context *
2566 g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
2568 struct intel_context *ce;
2570 if (unlikely(desc_idx >= GUC_MAX_LRC_DESCRIPTORS)) {
2571 drm_err(&guc_to_gt(guc)->i915->drm,
2572 "Invalid desc_idx %u", desc_idx);
2576 ce = __get_context(guc, desc_idx);
2577 if (unlikely(!ce)) {
2578 drm_err(&guc_to_gt(guc)->i915->drm,
2579 "Context is NULL, desc_idx %u", desc_idx);
2586 static void decr_outstanding_submission_g2h(struct intel_guc *guc)
2588 if (atomic_dec_and_test(&guc->outstanding_submission_g2h))
2589 wake_up_all(&guc->ct.wq);
2592 int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
2596 struct intel_context *ce;
2597 u32 desc_idx = msg[0];
2599 if (unlikely(len < 1)) {
2600 drm_err(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);
2604 ce = g2h_context_lookup(guc, desc_idx);
2608 trace_intel_context_deregister_done(ce);
2610 if (context_wait_for_deregister_to_register(ce)) {
2611 struct intel_runtime_pm *runtime_pm =
2612 &ce->engine->gt->i915->runtime_pm;
2613 intel_wakeref_t wakeref;
2616 * Previous owner of this guc_id has been deregistered, now safe
2617 * register this context.
2619 with_intel_runtime_pm(runtime_pm, wakeref)
2620 register_context(ce, true);
2621 guc_signal_context_fence(ce);
2622 intel_context_put(ce);
2623 } else if (context_destroyed(ce)) {
2624 /* Context has been destroyed */
2625 release_guc_id(guc, ce);
2626 __guc_context_destroy(ce);
2629 decr_outstanding_submission_g2h(guc);
2634 int intel_guc_sched_done_process_msg(struct intel_guc *guc,
2638 struct intel_context *ce;
2639 unsigned long flags;
2640 u32 desc_idx = msg[0];
2642 if (unlikely(len < 2)) {
2643 drm_err(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);
2647 ce = g2h_context_lookup(guc, desc_idx);
2651 if (unlikely(context_destroyed(ce) ||
2652 (!context_pending_enable(ce) &&
2653 !context_pending_disable(ce)))) {
2654 drm_err(&guc_to_gt(guc)->i915->drm,
2655 "Bad context sched_state 0x%x, 0x%x, desc_idx %u",
2656 atomic_read(&ce->guc_sched_state_no_lock),
2657 ce->guc_state.sched_state, desc_idx);
2661 trace_intel_context_sched_done(ce);
2663 if (context_pending_enable(ce)) {
2664 clr_context_pending_enable(ce);
2665 } else if (context_pending_disable(ce)) {
2669 * Unpin must be done before __guc_signal_context_fence,
2670 * otherwise a race exists between the requests getting
2671 * submitted + retired before this unpin completes resulting in
2672 * the pin_count going to zero and the context still being
2675 intel_context_sched_disable_unpin(ce);
2677 spin_lock_irqsave(&ce->guc_state.lock, flags);
2678 banned = context_banned(ce);
2679 clr_context_banned(ce);
2680 clr_context_pending_disable(ce);
2681 __guc_signal_context_fence(ce);
2682 guc_blocked_fence_complete(ce);
2683 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2686 guc_cancel_context_requests(ce);
2687 intel_engine_signal_breadcrumbs(ce->engine);
2691 decr_outstanding_submission_g2h(guc);
2692 intel_context_put(ce);
2697 static void capture_error_state(struct intel_guc *guc,
2698 struct intel_context *ce)
2700 struct intel_gt *gt = guc_to_gt(guc);
2701 struct drm_i915_private *i915 = gt->i915;
2702 struct intel_engine_cs *engine = __context_to_physical_engine(ce);
2703 intel_wakeref_t wakeref;
2705 intel_engine_set_hung_context(engine, ce);
2706 with_intel_runtime_pm(&i915->runtime_pm, wakeref)
2707 i915_capture_error_state(gt, engine->mask);
2708 atomic_inc(&i915->gpu_error.reset_engine_count[engine->uabi_class]);
2711 static void guc_context_replay(struct intel_context *ce)
2713 struct i915_sched_engine *sched_engine = ce->engine->sched_engine;
2715 __guc_reset_context(ce, true);
2716 tasklet_hi_schedule(&sched_engine->tasklet);
2719 static void guc_handle_context_reset(struct intel_guc *guc,
2720 struct intel_context *ce)
2722 trace_intel_context_reset(ce);
2724 if (likely(!intel_context_is_banned(ce))) {
2725 capture_error_state(guc, ce);
2726 guc_context_replay(ce);
2730 int intel_guc_context_reset_process_msg(struct intel_guc *guc,
2731 const u32 *msg, u32 len)
2733 struct intel_context *ce;
2736 if (unlikely(len != 1)) {
2737 drm_err(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);
2742 ce = g2h_context_lookup(guc, desc_idx);
2746 guc_handle_context_reset(guc, ce);
2751 static struct intel_engine_cs *
2752 guc_lookup_engine(struct intel_guc *guc, u8 guc_class, u8 instance)
2754 struct intel_gt *gt = guc_to_gt(guc);
2755 u8 engine_class = guc_class_to_engine_class(guc_class);
2757 /* Class index is checked in class converter */
2758 GEM_BUG_ON(instance > MAX_ENGINE_INSTANCE);
2760 return gt->engine_class[engine_class][instance];
2763 int intel_guc_engine_failure_process_msg(struct intel_guc *guc,
2764 const u32 *msg, u32 len)
2766 struct intel_engine_cs *engine;
2767 u8 guc_class, instance;
2770 if (unlikely(len != 3)) {
2771 drm_err(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);
2779 engine = guc_lookup_engine(guc, guc_class, instance);
2780 if (unlikely(!engine)) {
2781 drm_err(&guc_to_gt(guc)->i915->drm,
2782 "Invalid engine %d:%d", guc_class, instance);
2786 intel_gt_handle_error(guc_to_gt(guc), engine->mask,
2788 "GuC failed to reset %s (reason=0x%08x)\n",
2789 engine->name, reason);
2794 void intel_guc_find_hung_context(struct intel_engine_cs *engine)
2796 struct intel_guc *guc = &engine->gt->uc.guc;
2797 struct intel_context *ce;
2798 struct i915_request *rq;
2799 unsigned long index;
2801 /* Reset called during driver load? GuC not yet initialised! */
2802 if (unlikely(!guc_submission_initialized(guc)))
2805 xa_for_each(&guc->context_lookup, index, ce) {
2806 if (!intel_context_is_pinned(ce))
2809 if (intel_engine_is_virtual(ce->engine)) {
2810 if (!(ce->engine->mask & engine->mask))
2813 if (ce->engine != engine)
2817 list_for_each_entry(rq, &ce->guc_active.requests, sched.link) {
2818 if (i915_test_request_state(rq) != I915_REQUEST_ACTIVE)
2821 intel_engine_set_hung_context(engine, ce);
2823 /* Can only cope with one hang at a time... */
2829 void intel_guc_dump_active_requests(struct intel_engine_cs *engine,
2830 struct i915_request *hung_rq,
2831 struct drm_printer *m)
2833 struct intel_guc *guc = &engine->gt->uc.guc;
2834 struct intel_context *ce;
2835 unsigned long index;
2836 unsigned long flags;
2838 /* Reset called during driver load? GuC not yet initialised! */
2839 if (unlikely(!guc_submission_initialized(guc)))
2842 xa_for_each(&guc->context_lookup, index, ce) {
2843 if (!intel_context_is_pinned(ce))
2846 if (intel_engine_is_virtual(ce->engine)) {
2847 if (!(ce->engine->mask & engine->mask))
2850 if (ce->engine != engine)
2854 spin_lock_irqsave(&ce->guc_active.lock, flags);
2855 intel_engine_dump_active_requests(&ce->guc_active.requests,
2857 spin_unlock_irqrestore(&ce->guc_active.lock, flags);
2861 void intel_guc_submission_print_info(struct intel_guc *guc,
2862 struct drm_printer *p)
2864 struct i915_sched_engine *sched_engine = guc->sched_engine;
2866 unsigned long flags;
2871 drm_printf(p, "GuC Number Outstanding Submission G2H: %u\n",
2872 atomic_read(&guc->outstanding_submission_g2h));
2873 drm_printf(p, "GuC tasklet count: %u\n\n",
2874 atomic_read(&sched_engine->tasklet.count));
2876 spin_lock_irqsave(&sched_engine->lock, flags);
2877 drm_printf(p, "Requests in GuC submit tasklet:\n");
2878 for (rb = rb_first_cached(&sched_engine->queue); rb; rb = rb_next(rb)) {
2879 struct i915_priolist *pl = to_priolist(rb);
2880 struct i915_request *rq;
2882 priolist_for_each_request(rq, pl)
2883 drm_printf(p, "guc_id=%u, seqno=%llu\n",
2884 rq->context->guc_id,
2887 spin_unlock_irqrestore(&sched_engine->lock, flags);
2888 drm_printf(p, "\n");
2891 static inline void guc_log_context_priority(struct drm_printer *p,
2892 struct intel_context *ce)
2896 drm_printf(p, "\t\tPriority: %d\n",
2898 drm_printf(p, "\t\tNumber Requests (lower index == higher priority)\n");
2899 for (i = GUC_CLIENT_PRIORITY_KMD_HIGH;
2900 i < GUC_CLIENT_PRIORITY_NUM; ++i) {
2901 drm_printf(p, "\t\tNumber requests in priority band[%d]: %d\n",
2902 i, ce->guc_prio_count[i]);
2904 drm_printf(p, "\n");
2907 void intel_guc_submission_print_context_info(struct intel_guc *guc,
2908 struct drm_printer *p)
2910 struct intel_context *ce;
2911 unsigned long index;
2913 xa_for_each(&guc->context_lookup, index, ce) {
2914 drm_printf(p, "GuC lrc descriptor %u:\n", ce->guc_id);
2915 drm_printf(p, "\tHW Context Desc: 0x%08x\n", ce->lrc.lrca);
2916 drm_printf(p, "\t\tLRC Head: Internal %u, Memory %u\n",
2918 ce->lrc_reg_state[CTX_RING_HEAD]);
2919 drm_printf(p, "\t\tLRC Tail: Internal %u, Memory %u\n",
2921 ce->lrc_reg_state[CTX_RING_TAIL]);
2922 drm_printf(p, "\t\tContext Pin Count: %u\n",
2923 atomic_read(&ce->pin_count));
2924 drm_printf(p, "\t\tGuC ID Ref Count: %u\n",
2925 atomic_read(&ce->guc_id_ref));
2926 drm_printf(p, "\t\tSchedule State: 0x%x, 0x%x\n\n",
2927 ce->guc_state.sched_state,
2928 atomic_read(&ce->guc_sched_state_no_lock));
2930 guc_log_context_priority(p, ce);
2934 static struct intel_context *
2935 guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
2937 struct guc_virtual_engine *ve;
2938 struct intel_guc *guc;
2942 ve = kzalloc(sizeof(*ve), GFP_KERNEL);
2944 return ERR_PTR(-ENOMEM);
2946 guc = &siblings[0]->gt->uc.guc;
2948 ve->base.i915 = siblings[0]->i915;
2949 ve->base.gt = siblings[0]->gt;
2950 ve->base.uncore = siblings[0]->uncore;
2953 ve->base.uabi_class = I915_ENGINE_CLASS_INVALID;
2954 ve->base.instance = I915_ENGINE_CLASS_INVALID_VIRTUAL;
2955 ve->base.uabi_instance = I915_ENGINE_CLASS_INVALID_VIRTUAL;
2956 ve->base.saturated = ALL_ENGINES;
2958 snprintf(ve->base.name, sizeof(ve->base.name), "virtual");
2960 ve->base.sched_engine = i915_sched_engine_get(guc->sched_engine);
2962 ve->base.cops = &virtual_guc_context_ops;
2963 ve->base.request_alloc = guc_request_alloc;
2964 ve->base.bump_serial = virtual_guc_bump_serial;
2966 ve->base.submit_request = guc_submit_request;
2968 ve->base.flags = I915_ENGINE_IS_VIRTUAL;
2970 intel_context_init(&ve->context, &ve->base);
2972 for (n = 0; n < count; n++) {
2973 struct intel_engine_cs *sibling = siblings[n];
2975 GEM_BUG_ON(!is_power_of_2(sibling->mask));
2976 if (sibling->mask & ve->base.mask) {
2977 DRM_DEBUG("duplicate %s entry in load balancer\n",
2983 ve->base.mask |= sibling->mask;
2985 if (n != 0 && ve->base.class != sibling->class) {
2986 DRM_DEBUG("invalid mixing of engine class, sibling %d, already %d\n",
2987 sibling->class, ve->base.class);
2990 } else if (n == 0) {
2991 ve->base.class = sibling->class;
2992 ve->base.uabi_class = sibling->uabi_class;
2993 snprintf(ve->base.name, sizeof(ve->base.name),
2994 "v%dx%d", ve->base.class, count);
2995 ve->base.context_size = sibling->context_size;
2997 ve->base.add_active_request =
2998 sibling->add_active_request;
2999 ve->base.remove_active_request =
3000 sibling->remove_active_request;
3001 ve->base.emit_bb_start = sibling->emit_bb_start;
3002 ve->base.emit_flush = sibling->emit_flush;
3003 ve->base.emit_init_breadcrumb =
3004 sibling->emit_init_breadcrumb;
3005 ve->base.emit_fini_breadcrumb =
3006 sibling->emit_fini_breadcrumb;
3007 ve->base.emit_fini_breadcrumb_dw =
3008 sibling->emit_fini_breadcrumb_dw;
3009 ve->base.breadcrumbs =
3010 intel_breadcrumbs_get(sibling->breadcrumbs);
3012 ve->base.flags |= sibling->flags;
3014 ve->base.props.timeslice_duration_ms =
3015 sibling->props.timeslice_duration_ms;
3016 ve->base.props.preempt_timeout_ms =
3017 sibling->props.preempt_timeout_ms;
3021 return &ve->context;
3024 intel_context_put(&ve->context);
3025 return ERR_PTR(err);
3028 bool intel_guc_virtual_engine_has_heartbeat(const struct intel_engine_cs *ve)
3030 struct intel_engine_cs *engine;
3031 intel_engine_mask_t tmp, mask = ve->mask;
3033 for_each_engine_masked(engine, ve->gt, mask, tmp)
3034 if (READ_ONCE(engine->props.heartbeat_interval_ms))