drm/i915/guc: Drop guc_active move everything into guc_state
[sfrench/cifs-2.6.git] / drivers / gpu / drm / i915 / gt / uc / intel_guc_submission.c
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2014 Intel Corporation
4  */
5
6 #include <linux/circ_buf.h>
7
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"
22
23 #include "intel_guc_submission.h"
24
25 #include "i915_drv.h"
26 #include "i915_trace.h"
27
28 /**
29  * DOC: GuC-based command submission
30  *
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.
36  *
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.
45  *
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
52  * then proceeds.
53  *
54  * Work Items:
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()
61  *
62  */
63
64 /* GuC Virtual Engine */
65 struct guc_virtual_engine {
66         struct intel_engine_cs base;
67         struct intel_context context;
68 };
69
70 static struct intel_context *
71 guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count);
72
73 #define GUC_REQUEST_SIZE 64 /* bytes */
74
75 /*
76  * Below is a set of functions which control the GuC scheduling state which
77  * require a lock.
78  */
79 #define SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER     BIT(0)
80 #define SCHED_STATE_DESTROYED                           BIT(1)
81 #define SCHED_STATE_PENDING_DISABLE                     BIT(2)
82 #define SCHED_STATE_BANNED                              BIT(3)
83 #define SCHED_STATE_ENABLED                             BIT(4)
84 #define SCHED_STATE_PENDING_ENABLE                      BIT(5)
85 #define SCHED_STATE_REGISTERED                          BIT(6)
86 #define SCHED_STATE_BLOCKED_SHIFT                       7
87 #define SCHED_STATE_BLOCKED             BIT(SCHED_STATE_BLOCKED_SHIFT)
88 #define SCHED_STATE_BLOCKED_MASK        (0xfff << SCHED_STATE_BLOCKED_SHIFT)
89
90 static inline void init_sched_state(struct intel_context *ce)
91 {
92         lockdep_assert_held(&ce->guc_state.lock);
93         ce->guc_state.sched_state &= SCHED_STATE_BLOCKED_MASK;
94 }
95
96 __maybe_unused
97 static bool sched_state_is_init(struct intel_context *ce)
98 {
99         /*
100          * XXX: Kernel contexts can have SCHED_STATE_NO_LOCK_REGISTERED after
101          * suspend.
102          */
103         return !(ce->guc_state.sched_state &=
104                  ~(SCHED_STATE_BLOCKED_MASK | SCHED_STATE_REGISTERED));
105 }
106
107 static inline bool
108 context_wait_for_deregister_to_register(struct intel_context *ce)
109 {
110         return ce->guc_state.sched_state &
111                 SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
112 }
113
114 static inline void
115 set_context_wait_for_deregister_to_register(struct intel_context *ce)
116 {
117         lockdep_assert_held(&ce->guc_state.lock);
118         ce->guc_state.sched_state |=
119                 SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
120 }
121
122 static inline void
123 clr_context_wait_for_deregister_to_register(struct intel_context *ce)
124 {
125         lockdep_assert_held(&ce->guc_state.lock);
126         ce->guc_state.sched_state &=
127                 ~SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
128 }
129
130 static inline bool
131 context_destroyed(struct intel_context *ce)
132 {
133         return ce->guc_state.sched_state & SCHED_STATE_DESTROYED;
134 }
135
136 static inline void
137 set_context_destroyed(struct intel_context *ce)
138 {
139         lockdep_assert_held(&ce->guc_state.lock);
140         ce->guc_state.sched_state |= SCHED_STATE_DESTROYED;
141 }
142
143 static inline bool context_pending_disable(struct intel_context *ce)
144 {
145         return ce->guc_state.sched_state & SCHED_STATE_PENDING_DISABLE;
146 }
147
148 static inline void set_context_pending_disable(struct intel_context *ce)
149 {
150         lockdep_assert_held(&ce->guc_state.lock);
151         ce->guc_state.sched_state |= SCHED_STATE_PENDING_DISABLE;
152 }
153
154 static inline void clr_context_pending_disable(struct intel_context *ce)
155 {
156         lockdep_assert_held(&ce->guc_state.lock);
157         ce->guc_state.sched_state &= ~SCHED_STATE_PENDING_DISABLE;
158 }
159
160 static inline bool context_banned(struct intel_context *ce)
161 {
162         return ce->guc_state.sched_state & SCHED_STATE_BANNED;
163 }
164
165 static inline void set_context_banned(struct intel_context *ce)
166 {
167         lockdep_assert_held(&ce->guc_state.lock);
168         ce->guc_state.sched_state |= SCHED_STATE_BANNED;
169 }
170
171 static inline void clr_context_banned(struct intel_context *ce)
172 {
173         lockdep_assert_held(&ce->guc_state.lock);
174         ce->guc_state.sched_state &= ~SCHED_STATE_BANNED;
175 }
176
177 static inline bool context_enabled(struct intel_context *ce)
178 {
179         return ce->guc_state.sched_state & SCHED_STATE_ENABLED;
180 }
181
182 static inline void set_context_enabled(struct intel_context *ce)
183 {
184         lockdep_assert_held(&ce->guc_state.lock);
185         ce->guc_state.sched_state |= SCHED_STATE_ENABLED;
186 }
187
188 static inline void clr_context_enabled(struct intel_context *ce)
189 {
190         lockdep_assert_held(&ce->guc_state.lock);
191         ce->guc_state.sched_state &= ~SCHED_STATE_ENABLED;
192 }
193
194 static inline bool context_pending_enable(struct intel_context *ce)
195 {
196         return ce->guc_state.sched_state & SCHED_STATE_PENDING_ENABLE;
197 }
198
199 static inline void set_context_pending_enable(struct intel_context *ce)
200 {
201         lockdep_assert_held(&ce->guc_state.lock);
202         ce->guc_state.sched_state |= SCHED_STATE_PENDING_ENABLE;
203 }
204
205 static inline void clr_context_pending_enable(struct intel_context *ce)
206 {
207         lockdep_assert_held(&ce->guc_state.lock);
208         ce->guc_state.sched_state &= ~SCHED_STATE_PENDING_ENABLE;
209 }
210
211 static inline bool context_registered(struct intel_context *ce)
212 {
213         return ce->guc_state.sched_state & SCHED_STATE_REGISTERED;
214 }
215
216 static inline void set_context_registered(struct intel_context *ce)
217 {
218         lockdep_assert_held(&ce->guc_state.lock);
219         ce->guc_state.sched_state |= SCHED_STATE_REGISTERED;
220 }
221
222 static inline void clr_context_registered(struct intel_context *ce)
223 {
224         lockdep_assert_held(&ce->guc_state.lock);
225         ce->guc_state.sched_state &= ~SCHED_STATE_REGISTERED;
226 }
227
228 static inline u32 context_blocked(struct intel_context *ce)
229 {
230         return (ce->guc_state.sched_state & SCHED_STATE_BLOCKED_MASK) >>
231                 SCHED_STATE_BLOCKED_SHIFT;
232 }
233
234 static inline void incr_context_blocked(struct intel_context *ce)
235 {
236         lockdep_assert_held(&ce->guc_state.lock);
237
238         ce->guc_state.sched_state += SCHED_STATE_BLOCKED;
239
240         GEM_BUG_ON(!context_blocked(ce));       /* Overflow check */
241 }
242
243 static inline void decr_context_blocked(struct intel_context *ce)
244 {
245         lockdep_assert_held(&ce->guc_state.lock);
246
247         GEM_BUG_ON(!context_blocked(ce));       /* Underflow check */
248
249         ce->guc_state.sched_state -= SCHED_STATE_BLOCKED;
250 }
251
252 static inline bool context_has_committed_requests(struct intel_context *ce)
253 {
254         return !!ce->guc_state.number_committed_requests;
255 }
256
257 static inline void incr_context_committed_requests(struct intel_context *ce)
258 {
259         lockdep_assert_held(&ce->guc_state.lock);
260         ++ce->guc_state.number_committed_requests;
261         GEM_BUG_ON(ce->guc_state.number_committed_requests < 0);
262 }
263
264 static inline void decr_context_committed_requests(struct intel_context *ce)
265 {
266         lockdep_assert_held(&ce->guc_state.lock);
267         --ce->guc_state.number_committed_requests;
268         GEM_BUG_ON(ce->guc_state.number_committed_requests < 0);
269 }
270
271 static inline bool context_guc_id_invalid(struct intel_context *ce)
272 {
273         return ce->guc_id.id == GUC_INVALID_LRC_ID;
274 }
275
276 static inline void set_context_guc_id_invalid(struct intel_context *ce)
277 {
278         ce->guc_id.id = GUC_INVALID_LRC_ID;
279 }
280
281 static inline struct intel_guc *ce_to_guc(struct intel_context *ce)
282 {
283         return &ce->engine->gt->uc.guc;
284 }
285
286 static inline struct i915_priolist *to_priolist(struct rb_node *rb)
287 {
288         return rb_entry(rb, struct i915_priolist, node);
289 }
290
291 static struct guc_lrc_desc *__get_lrc_desc(struct intel_guc *guc, u32 index)
292 {
293         struct guc_lrc_desc *base = guc->lrc_desc_pool_vaddr;
294
295         GEM_BUG_ON(index >= GUC_MAX_LRC_DESCRIPTORS);
296
297         return &base[index];
298 }
299
300 static inline struct intel_context *__get_context(struct intel_guc *guc, u32 id)
301 {
302         struct intel_context *ce = xa_load(&guc->context_lookup, id);
303
304         GEM_BUG_ON(id >= GUC_MAX_LRC_DESCRIPTORS);
305
306         return ce;
307 }
308
309 static int guc_lrc_desc_pool_create(struct intel_guc *guc)
310 {
311         u32 size;
312         int ret;
313
314         size = PAGE_ALIGN(sizeof(struct guc_lrc_desc) *
315                           GUC_MAX_LRC_DESCRIPTORS);
316         ret = intel_guc_allocate_and_map_vma(guc, size, &guc->lrc_desc_pool,
317                                              (void **)&guc->lrc_desc_pool_vaddr);
318         if (ret)
319                 return ret;
320
321         return 0;
322 }
323
324 static void guc_lrc_desc_pool_destroy(struct intel_guc *guc)
325 {
326         guc->lrc_desc_pool_vaddr = NULL;
327         i915_vma_unpin_and_release(&guc->lrc_desc_pool, I915_VMA_RELEASE_MAP);
328 }
329
330 static inline bool guc_submission_initialized(struct intel_guc *guc)
331 {
332         return !!guc->lrc_desc_pool_vaddr;
333 }
334
335 static inline void reset_lrc_desc(struct intel_guc *guc, u32 id)
336 {
337         if (likely(guc_submission_initialized(guc))) {
338                 struct guc_lrc_desc *desc = __get_lrc_desc(guc, id);
339                 unsigned long flags;
340
341                 memset(desc, 0, sizeof(*desc));
342
343                 /*
344                  * xarray API doesn't have xa_erase_irqsave wrapper, so calling
345                  * the lower level functions directly.
346                  */
347                 xa_lock_irqsave(&guc->context_lookup, flags);
348                 __xa_erase(&guc->context_lookup, id);
349                 xa_unlock_irqrestore(&guc->context_lookup, flags);
350         }
351 }
352
353 static inline bool lrc_desc_registered(struct intel_guc *guc, u32 id)
354 {
355         return __get_context(guc, id);
356 }
357
358 static inline void set_lrc_desc_registered(struct intel_guc *guc, u32 id,
359                                            struct intel_context *ce)
360 {
361         unsigned long flags;
362
363         /*
364          * xarray API doesn't have xa_save_irqsave wrapper, so calling the
365          * lower level functions directly.
366          */
367         xa_lock_irqsave(&guc->context_lookup, flags);
368         __xa_store(&guc->context_lookup, id, ce, GFP_ATOMIC);
369         xa_unlock_irqrestore(&guc->context_lookup, flags);
370 }
371
372 static void decr_outstanding_submission_g2h(struct intel_guc *guc)
373 {
374         if (atomic_dec_and_test(&guc->outstanding_submission_g2h))
375                 wake_up_all(&guc->ct.wq);
376 }
377
378 static int guc_submission_send_busy_loop(struct intel_guc *guc,
379                                          const u32 *action,
380                                          u32 len,
381                                          u32 g2h_len_dw,
382                                          bool loop)
383 {
384         /*
385          * We always loop when a send requires a reply (i.e. g2h_len_dw > 0),
386          * so we don't handle the case where we don't get a reply because we
387          * aborted the send due to the channel being busy.
388          */
389         GEM_BUG_ON(g2h_len_dw && !loop);
390
391         if (g2h_len_dw)
392                 atomic_inc(&guc->outstanding_submission_g2h);
393
394         return intel_guc_send_busy_loop(guc, action, len, g2h_len_dw, loop);
395 }
396
397 int intel_guc_wait_for_pending_msg(struct intel_guc *guc,
398                                    atomic_t *wait_var,
399                                    bool interruptible,
400                                    long timeout)
401 {
402         const int state = interruptible ?
403                 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
404         DEFINE_WAIT(wait);
405
406         might_sleep();
407         GEM_BUG_ON(timeout < 0);
408
409         if (!atomic_read(wait_var))
410                 return 0;
411
412         if (!timeout)
413                 return -ETIME;
414
415         for (;;) {
416                 prepare_to_wait(&guc->ct.wq, &wait, state);
417
418                 if (!atomic_read(wait_var))
419                         break;
420
421                 if (signal_pending_state(state, current)) {
422                         timeout = -EINTR;
423                         break;
424                 }
425
426                 if (!timeout) {
427                         timeout = -ETIME;
428                         break;
429                 }
430
431                 timeout = io_schedule_timeout(timeout);
432         }
433         finish_wait(&guc->ct.wq, &wait);
434
435         return (timeout < 0) ? timeout : 0;
436 }
437
438 int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout)
439 {
440         if (!intel_uc_uses_guc_submission(&guc_to_gt(guc)->uc))
441                 return 0;
442
443         return intel_guc_wait_for_pending_msg(guc,
444                                               &guc->outstanding_submission_g2h,
445                                               true, timeout);
446 }
447
448 static int guc_lrc_desc_pin(struct intel_context *ce, bool loop);
449
450 static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
451 {
452         int err = 0;
453         struct intel_context *ce = rq->context;
454         u32 action[3];
455         int len = 0;
456         u32 g2h_len_dw = 0;
457         bool enabled;
458
459         lockdep_assert_held(&rq->engine->sched_engine->lock);
460
461         /*
462          * Corner case where requests were sitting in the priority list or a
463          * request resubmitted after the context was banned.
464          */
465         if (unlikely(intel_context_is_banned(ce))) {
466                 i915_request_put(i915_request_mark_eio(rq));
467                 intel_engine_signal_breadcrumbs(ce->engine);
468                 return 0;
469         }
470
471         GEM_BUG_ON(!atomic_read(&ce->guc_id.ref));
472         GEM_BUG_ON(context_guc_id_invalid(ce));
473
474         /*
475          * Corner case where the GuC firmware was blown away and reloaded while
476          * this context was pinned.
477          */
478         if (unlikely(!lrc_desc_registered(guc, ce->guc_id.id))) {
479                 err = guc_lrc_desc_pin(ce, false);
480                 if (unlikely(err))
481                         return err;
482         }
483
484         spin_lock(&ce->guc_state.lock);
485
486         /*
487          * The request / context will be run on the hardware when scheduling
488          * gets enabled in the unblock.
489          */
490         if (unlikely(context_blocked(ce)))
491                 goto out;
492
493         enabled = context_enabled(ce);
494
495         if (!enabled) {
496                 action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
497                 action[len++] = ce->guc_id.id;
498                 action[len++] = GUC_CONTEXT_ENABLE;
499                 set_context_pending_enable(ce);
500                 intel_context_get(ce);
501                 g2h_len_dw = G2H_LEN_DW_SCHED_CONTEXT_MODE_SET;
502         } else {
503                 action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT;
504                 action[len++] = ce->guc_id.id;
505         }
506
507         err = intel_guc_send_nb(guc, action, len, g2h_len_dw);
508         if (!enabled && !err) {
509                 trace_intel_context_sched_enable(ce);
510                 atomic_inc(&guc->outstanding_submission_g2h);
511                 set_context_enabled(ce);
512         } else if (!enabled) {
513                 clr_context_pending_enable(ce);
514                 intel_context_put(ce);
515         }
516         if (likely(!err))
517                 trace_i915_request_guc_submit(rq);
518
519 out:
520         spin_unlock(&ce->guc_state.lock);
521         return err;
522 }
523
524 static inline void guc_set_lrc_tail(struct i915_request *rq)
525 {
526         rq->context->lrc_reg_state[CTX_RING_TAIL] =
527                 intel_ring_set_tail(rq->ring, rq->tail);
528 }
529
530 static inline int rq_prio(const struct i915_request *rq)
531 {
532         return rq->sched.attr.priority;
533 }
534
535 static int guc_dequeue_one_context(struct intel_guc *guc)
536 {
537         struct i915_sched_engine * const sched_engine = guc->sched_engine;
538         struct i915_request *last = NULL;
539         bool submit = false;
540         struct rb_node *rb;
541         int ret;
542
543         lockdep_assert_held(&sched_engine->lock);
544
545         if (guc->stalled_request) {
546                 submit = true;
547                 last = guc->stalled_request;
548                 goto resubmit;
549         }
550
551         while ((rb = rb_first_cached(&sched_engine->queue))) {
552                 struct i915_priolist *p = to_priolist(rb);
553                 struct i915_request *rq, *rn;
554
555                 priolist_for_each_request_consume(rq, rn, p) {
556                         if (last && rq->context != last->context)
557                                 goto done;
558
559                         list_del_init(&rq->sched.link);
560
561                         __i915_request_submit(rq);
562
563                         trace_i915_request_in(rq, 0);
564                         last = rq;
565                         submit = true;
566                 }
567
568                 rb_erase_cached(&p->node, &sched_engine->queue);
569                 i915_priolist_free(p);
570         }
571 done:
572         if (submit) {
573                 guc_set_lrc_tail(last);
574 resubmit:
575                 ret = guc_add_request(guc, last);
576                 if (unlikely(ret == -EPIPE))
577                         goto deadlk;
578                 else if (ret == -EBUSY) {
579                         tasklet_schedule(&sched_engine->tasklet);
580                         guc->stalled_request = last;
581                         return false;
582                 }
583         }
584
585         guc->stalled_request = NULL;
586         return submit;
587
588 deadlk:
589         sched_engine->tasklet.callback = NULL;
590         tasklet_disable_nosync(&sched_engine->tasklet);
591         return false;
592 }
593
594 static void guc_submission_tasklet(struct tasklet_struct *t)
595 {
596         struct i915_sched_engine *sched_engine =
597                 from_tasklet(sched_engine, t, tasklet);
598         unsigned long flags;
599         bool loop;
600
601         spin_lock_irqsave(&sched_engine->lock, flags);
602
603         do {
604                 loop = guc_dequeue_one_context(sched_engine->private_data);
605         } while (loop);
606
607         i915_sched_engine_reset_on_empty(sched_engine);
608
609         spin_unlock_irqrestore(&sched_engine->lock, flags);
610 }
611
612 static void cs_irq_handler(struct intel_engine_cs *engine, u16 iir)
613 {
614         if (iir & GT_RENDER_USER_INTERRUPT)
615                 intel_engine_signal_breadcrumbs(engine);
616 }
617
618 static void __guc_context_destroy(struct intel_context *ce);
619 static void release_guc_id(struct intel_guc *guc, struct intel_context *ce);
620 static void guc_signal_context_fence(struct intel_context *ce);
621 static void guc_cancel_context_requests(struct intel_context *ce);
622 static void guc_blocked_fence_complete(struct intel_context *ce);
623
624 static void scrub_guc_desc_for_outstanding_g2h(struct intel_guc *guc)
625 {
626         struct intel_context *ce;
627         unsigned long index, flags;
628         bool pending_disable, pending_enable, deregister, destroyed, banned;
629
630         xa_lock_irqsave(&guc->context_lookup, flags);
631         xa_for_each(&guc->context_lookup, index, ce) {
632                 /*
633                  * Corner case where the ref count on the object is zero but and
634                  * deregister G2H was lost. In this case we don't touch the ref
635                  * count and finish the destroy of the context.
636                  */
637                 bool do_put = kref_get_unless_zero(&ce->ref);
638
639                 xa_unlock(&guc->context_lookup);
640
641                 spin_lock(&ce->guc_state.lock);
642
643                 /*
644                  * Once we are at this point submission_disabled() is guaranteed
645                  * to be visible to all callers who set the below flags (see above
646                  * flush and flushes in reset_prepare). If submission_disabled()
647                  * is set, the caller shouldn't set these flags.
648                  */
649
650                 destroyed = context_destroyed(ce);
651                 pending_enable = context_pending_enable(ce);
652                 pending_disable = context_pending_disable(ce);
653                 deregister = context_wait_for_deregister_to_register(ce);
654                 banned = context_banned(ce);
655                 init_sched_state(ce);
656
657                 spin_unlock(&ce->guc_state.lock);
658
659                 GEM_BUG_ON(!do_put && !destroyed);
660
661                 if (pending_enable || destroyed || deregister) {
662                         decr_outstanding_submission_g2h(guc);
663                         if (deregister)
664                                 guc_signal_context_fence(ce);
665                         if (destroyed) {
666                                 release_guc_id(guc, ce);
667                                 __guc_context_destroy(ce);
668                         }
669                         if (pending_enable || deregister)
670                                 intel_context_put(ce);
671                 }
672
673                 /* Not mutualy exclusive with above if statement. */
674                 if (pending_disable) {
675                         guc_signal_context_fence(ce);
676                         if (banned) {
677                                 guc_cancel_context_requests(ce);
678                                 intel_engine_signal_breadcrumbs(ce->engine);
679                         }
680                         intel_context_sched_disable_unpin(ce);
681                         decr_outstanding_submission_g2h(guc);
682
683                         spin_lock(&ce->guc_state.lock);
684                         guc_blocked_fence_complete(ce);
685                         spin_unlock(&ce->guc_state.lock);
686
687                         intel_context_put(ce);
688                 }
689
690                 if (do_put)
691                         intel_context_put(ce);
692                 xa_lock(&guc->context_lookup);
693         }
694         xa_unlock_irqrestore(&guc->context_lookup, flags);
695 }
696
697 static inline bool
698 submission_disabled(struct intel_guc *guc)
699 {
700         struct i915_sched_engine * const sched_engine = guc->sched_engine;
701
702         return unlikely(!sched_engine ||
703                         !__tasklet_is_enabled(&sched_engine->tasklet));
704 }
705
706 static void disable_submission(struct intel_guc *guc)
707 {
708         struct i915_sched_engine * const sched_engine = guc->sched_engine;
709
710         if (__tasklet_is_enabled(&sched_engine->tasklet)) {
711                 GEM_BUG_ON(!guc->ct.enabled);
712                 __tasklet_disable_sync_once(&sched_engine->tasklet);
713                 sched_engine->tasklet.callback = NULL;
714         }
715 }
716
717 static void enable_submission(struct intel_guc *guc)
718 {
719         struct i915_sched_engine * const sched_engine = guc->sched_engine;
720         unsigned long flags;
721
722         spin_lock_irqsave(&guc->sched_engine->lock, flags);
723         sched_engine->tasklet.callback = guc_submission_tasklet;
724         wmb();  /* Make sure callback visible */
725         if (!__tasklet_is_enabled(&sched_engine->tasklet) &&
726             __tasklet_enable(&sched_engine->tasklet)) {
727                 GEM_BUG_ON(!guc->ct.enabled);
728
729                 /* And kick in case we missed a new request submission. */
730                 tasklet_hi_schedule(&sched_engine->tasklet);
731         }
732         spin_unlock_irqrestore(&guc->sched_engine->lock, flags);
733 }
734
735 static void guc_flush_submissions(struct intel_guc *guc)
736 {
737         struct i915_sched_engine * const sched_engine = guc->sched_engine;
738         unsigned long flags;
739
740         spin_lock_irqsave(&sched_engine->lock, flags);
741         spin_unlock_irqrestore(&sched_engine->lock, flags);
742 }
743
744 void intel_guc_submission_reset_prepare(struct intel_guc *guc)
745 {
746         int i;
747
748         if (unlikely(!guc_submission_initialized(guc))) {
749                 /* Reset called during driver load? GuC not yet initialised! */
750                 return;
751         }
752
753         intel_gt_park_heartbeats(guc_to_gt(guc));
754         disable_submission(guc);
755         guc->interrupts.disable(guc);
756
757         /* Flush IRQ handler */
758         spin_lock_irq(&guc_to_gt(guc)->irq_lock);
759         spin_unlock_irq(&guc_to_gt(guc)->irq_lock);
760
761         guc_flush_submissions(guc);
762
763         /*
764          * Handle any outstanding G2Hs before reset. Call IRQ handler directly
765          * each pass as interrupt have been disabled. We always scrub for
766          * outstanding G2H as it is possible for outstanding_submission_g2h to
767          * be incremented after the context state update.
768          */
769         for (i = 0; i < 4 && atomic_read(&guc->outstanding_submission_g2h); ++i) {
770                 intel_guc_to_host_event_handler(guc);
771 #define wait_for_reset(guc, wait_var) \
772                 intel_guc_wait_for_pending_msg(guc, wait_var, false, (HZ / 20))
773                 do {
774                         wait_for_reset(guc, &guc->outstanding_submission_g2h);
775                 } while (!list_empty(&guc->ct.requests.incoming));
776         }
777
778         scrub_guc_desc_for_outstanding_g2h(guc);
779 }
780
781 static struct intel_engine_cs *
782 guc_virtual_get_sibling(struct intel_engine_cs *ve, unsigned int sibling)
783 {
784         struct intel_engine_cs *engine;
785         intel_engine_mask_t tmp, mask = ve->mask;
786         unsigned int num_siblings = 0;
787
788         for_each_engine_masked(engine, ve->gt, mask, tmp)
789                 if (num_siblings++ == sibling)
790                         return engine;
791
792         return NULL;
793 }
794
795 static inline struct intel_engine_cs *
796 __context_to_physical_engine(struct intel_context *ce)
797 {
798         struct intel_engine_cs *engine = ce->engine;
799
800         if (intel_engine_is_virtual(engine))
801                 engine = guc_virtual_get_sibling(engine, 0);
802
803         return engine;
804 }
805
806 static void guc_reset_state(struct intel_context *ce, u32 head, bool scrub)
807 {
808         struct intel_engine_cs *engine = __context_to_physical_engine(ce);
809
810         if (intel_context_is_banned(ce))
811                 return;
812
813         GEM_BUG_ON(!intel_context_is_pinned(ce));
814
815         /*
816          * We want a simple context + ring to execute the breadcrumb update.
817          * We cannot rely on the context being intact across the GPU hang,
818          * so clear it and rebuild just what we need for the breadcrumb.
819          * All pending requests for this context will be zapped, and any
820          * future request will be after userspace has had the opportunity
821          * to recreate its own state.
822          */
823         if (scrub)
824                 lrc_init_regs(ce, engine, true);
825
826         /* Rerun the request; its payload has been neutered (if guilty). */
827         lrc_update_regs(ce, engine, head);
828 }
829
830 static void guc_reset_nop(struct intel_engine_cs *engine)
831 {
832 }
833
834 static void guc_rewind_nop(struct intel_engine_cs *engine, bool stalled)
835 {
836 }
837
838 static void
839 __unwind_incomplete_requests(struct intel_context *ce)
840 {
841         struct i915_request *rq, *rn;
842         struct list_head *pl;
843         int prio = I915_PRIORITY_INVALID;
844         struct i915_sched_engine * const sched_engine =
845                 ce->engine->sched_engine;
846         unsigned long flags;
847
848         spin_lock_irqsave(&sched_engine->lock, flags);
849         spin_lock(&ce->guc_state.lock);
850         list_for_each_entry_safe_reverse(rq, rn,
851                                          &ce->guc_state.requests,
852                                          sched.link) {
853                 if (i915_request_completed(rq))
854                         continue;
855
856                 list_del_init(&rq->sched.link);
857                 __i915_request_unsubmit(rq);
858
859                 /* Push the request back into the queue for later resubmission. */
860                 GEM_BUG_ON(rq_prio(rq) == I915_PRIORITY_INVALID);
861                 if (rq_prio(rq) != prio) {
862                         prio = rq_prio(rq);
863                         pl = i915_sched_lookup_priolist(sched_engine, prio);
864                 }
865                 GEM_BUG_ON(i915_sched_engine_is_empty(sched_engine));
866
867                 list_add(&rq->sched.link, pl);
868                 set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
869         }
870         spin_unlock(&ce->guc_state.lock);
871         spin_unlock_irqrestore(&sched_engine->lock, flags);
872 }
873
874 static void __guc_reset_context(struct intel_context *ce, bool stalled)
875 {
876         struct i915_request *rq;
877         unsigned long flags;
878         u32 head;
879         bool skip = false;
880
881         intel_context_get(ce);
882
883         /*
884          * GuC will implicitly mark the context as non-schedulable when it sends
885          * the reset notification. Make sure our state reflects this change. The
886          * context will be marked enabled on resubmission.
887          *
888          * XXX: If the context is reset as a result of the request cancellation
889          * this G2H is received after the schedule disable complete G2H which is
890          * wrong as this creates a race between the request cancellation code
891          * re-submitting the context and this G2H handler. This is a bug in the
892          * GuC but can be worked around in the meantime but converting this to a
893          * NOP if a pending enable is in flight as this indicates that a request
894          * cancellation has occurred.
895          */
896         spin_lock_irqsave(&ce->guc_state.lock, flags);
897         if (likely(!context_pending_enable(ce)))
898                 clr_context_enabled(ce);
899         else
900                 skip = true;
901         spin_unlock_irqrestore(&ce->guc_state.lock, flags);
902         if (unlikely(skip))
903                 goto out_put;
904
905         rq = intel_context_find_active_request(ce);
906         if (!rq) {
907                 head = ce->ring->tail;
908                 stalled = false;
909                 goto out_replay;
910         }
911
912         if (!i915_request_started(rq))
913                 stalled = false;
914
915         GEM_BUG_ON(i915_active_is_idle(&ce->active));
916         head = intel_ring_wrap(ce->ring, rq->head);
917         __i915_request_reset(rq, stalled);
918
919 out_replay:
920         guc_reset_state(ce, head, stalled);
921         __unwind_incomplete_requests(ce);
922 out_put:
923         intel_context_put(ce);
924 }
925
926 void intel_guc_submission_reset(struct intel_guc *guc, bool stalled)
927 {
928         struct intel_context *ce;
929         unsigned long index;
930         unsigned long flags;
931
932         if (unlikely(!guc_submission_initialized(guc))) {
933                 /* Reset called during driver load? GuC not yet initialised! */
934                 return;
935         }
936
937         xa_lock_irqsave(&guc->context_lookup, flags);
938         xa_for_each(&guc->context_lookup, index, ce) {
939                 if (!kref_get_unless_zero(&ce->ref))
940                         continue;
941
942                 xa_unlock(&guc->context_lookup);
943
944                 if (intel_context_is_pinned(ce))
945                         __guc_reset_context(ce, stalled);
946
947                 intel_context_put(ce);
948
949                 xa_lock(&guc->context_lookup);
950         }
951         xa_unlock_irqrestore(&guc->context_lookup, flags);
952
953         /* GuC is blown away, drop all references to contexts */
954         xa_destroy(&guc->context_lookup);
955 }
956
957 static void guc_cancel_context_requests(struct intel_context *ce)
958 {
959         struct i915_sched_engine *sched_engine = ce_to_guc(ce)->sched_engine;
960         struct i915_request *rq;
961         unsigned long flags;
962
963         /* Mark all executing requests as skipped. */
964         spin_lock_irqsave(&sched_engine->lock, flags);
965         spin_lock(&ce->guc_state.lock);
966         list_for_each_entry(rq, &ce->guc_state.requests, sched.link)
967                 i915_request_put(i915_request_mark_eio(rq));
968         spin_unlock(&ce->guc_state.lock);
969         spin_unlock_irqrestore(&sched_engine->lock, flags);
970 }
971
972 static void
973 guc_cancel_sched_engine_requests(struct i915_sched_engine *sched_engine)
974 {
975         struct i915_request *rq, *rn;
976         struct rb_node *rb;
977         unsigned long flags;
978
979         /* Can be called during boot if GuC fails to load */
980         if (!sched_engine)
981                 return;
982
983         /*
984          * Before we call engine->cancel_requests(), we should have exclusive
985          * access to the submission state. This is arranged for us by the
986          * caller disabling the interrupt generation, the tasklet and other
987          * threads that may then access the same state, giving us a free hand
988          * to reset state. However, we still need to let lockdep be aware that
989          * we know this state may be accessed in hardirq context, so we
990          * disable the irq around this manipulation and we want to keep
991          * the spinlock focused on its duties and not accidentally conflate
992          * coverage to the submission's irq state. (Similarly, although we
993          * shouldn't need to disable irq around the manipulation of the
994          * submission's irq state, we also wish to remind ourselves that
995          * it is irq state.)
996          */
997         spin_lock_irqsave(&sched_engine->lock, flags);
998
999         /* Flush the queued requests to the timeline list (for retiring). */
1000         while ((rb = rb_first_cached(&sched_engine->queue))) {
1001                 struct i915_priolist *p = to_priolist(rb);
1002
1003                 priolist_for_each_request_consume(rq, rn, p) {
1004                         list_del_init(&rq->sched.link);
1005
1006                         __i915_request_submit(rq);
1007
1008                         i915_request_put(i915_request_mark_eio(rq));
1009                 }
1010
1011                 rb_erase_cached(&p->node, &sched_engine->queue);
1012                 i915_priolist_free(p);
1013         }
1014
1015         /* Remaining _unready_ requests will be nop'ed when submitted */
1016
1017         sched_engine->queue_priority_hint = INT_MIN;
1018         sched_engine->queue = RB_ROOT_CACHED;
1019
1020         spin_unlock_irqrestore(&sched_engine->lock, flags);
1021 }
1022
1023 void intel_guc_submission_cancel_requests(struct intel_guc *guc)
1024 {
1025         struct intel_context *ce;
1026         unsigned long index;
1027         unsigned long flags;
1028
1029         xa_lock_irqsave(&guc->context_lookup, flags);
1030         xa_for_each(&guc->context_lookup, index, ce) {
1031                 if (!kref_get_unless_zero(&ce->ref))
1032                         continue;
1033
1034                 xa_unlock(&guc->context_lookup);
1035
1036                 if (intel_context_is_pinned(ce))
1037                         guc_cancel_context_requests(ce);
1038
1039                 intel_context_put(ce);
1040
1041                 xa_lock(&guc->context_lookup);
1042         }
1043         xa_unlock_irqrestore(&guc->context_lookup, flags);
1044
1045         guc_cancel_sched_engine_requests(guc->sched_engine);
1046
1047         /* GuC is blown away, drop all references to contexts */
1048         xa_destroy(&guc->context_lookup);
1049 }
1050
1051 void intel_guc_submission_reset_finish(struct intel_guc *guc)
1052 {
1053         /* Reset called during driver load or during wedge? */
1054         if (unlikely(!guc_submission_initialized(guc) ||
1055                      test_bit(I915_WEDGED, &guc_to_gt(guc)->reset.flags))) {
1056                 return;
1057         }
1058
1059         /*
1060          * Technically possible for either of these values to be non-zero here,
1061          * but very unlikely + harmless. Regardless let's add a warn so we can
1062          * see in CI if this happens frequently / a precursor to taking down the
1063          * machine.
1064          */
1065         GEM_WARN_ON(atomic_read(&guc->outstanding_submission_g2h));
1066         atomic_set(&guc->outstanding_submission_g2h, 0);
1067
1068         intel_guc_global_policies_update(guc);
1069         enable_submission(guc);
1070         intel_gt_unpark_heartbeats(guc_to_gt(guc));
1071 }
1072
1073 /*
1074  * Set up the memory resources to be shared with the GuC (via the GGTT)
1075  * at firmware loading time.
1076  */
1077 int intel_guc_submission_init(struct intel_guc *guc)
1078 {
1079         int ret;
1080
1081         if (guc->lrc_desc_pool)
1082                 return 0;
1083
1084         ret = guc_lrc_desc_pool_create(guc);
1085         if (ret)
1086                 return ret;
1087         /*
1088          * Keep static analysers happy, let them know that we allocated the
1089          * vma after testing that it didn't exist earlier.
1090          */
1091         GEM_BUG_ON(!guc->lrc_desc_pool);
1092
1093         xa_init_flags(&guc->context_lookup, XA_FLAGS_LOCK_IRQ);
1094
1095         spin_lock_init(&guc->contexts_lock);
1096         INIT_LIST_HEAD(&guc->guc_id_list);
1097         ida_init(&guc->guc_ids);
1098
1099         return 0;
1100 }
1101
1102 void intel_guc_submission_fini(struct intel_guc *guc)
1103 {
1104         if (!guc->lrc_desc_pool)
1105                 return;
1106
1107         guc_lrc_desc_pool_destroy(guc);
1108         i915_sched_engine_put(guc->sched_engine);
1109 }
1110
1111 static inline void queue_request(struct i915_sched_engine *sched_engine,
1112                                  struct i915_request *rq,
1113                                  int prio)
1114 {
1115         GEM_BUG_ON(!list_empty(&rq->sched.link));
1116         list_add_tail(&rq->sched.link,
1117                       i915_sched_lookup_priolist(sched_engine, prio));
1118         set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
1119         tasklet_hi_schedule(&sched_engine->tasklet);
1120 }
1121
1122 static int guc_bypass_tasklet_submit(struct intel_guc *guc,
1123                                      struct i915_request *rq)
1124 {
1125         int ret;
1126
1127         __i915_request_submit(rq);
1128
1129         trace_i915_request_in(rq, 0);
1130
1131         guc_set_lrc_tail(rq);
1132         ret = guc_add_request(guc, rq);
1133         if (ret == -EBUSY)
1134                 guc->stalled_request = rq;
1135
1136         if (unlikely(ret == -EPIPE))
1137                 disable_submission(guc);
1138
1139         return ret;
1140 }
1141
1142 static void guc_submit_request(struct i915_request *rq)
1143 {
1144         struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
1145         struct intel_guc *guc = &rq->engine->gt->uc.guc;
1146         unsigned long flags;
1147
1148         /* Will be called from irq-context when using foreign fences. */
1149         spin_lock_irqsave(&sched_engine->lock, flags);
1150
1151         if (submission_disabled(guc) || guc->stalled_request ||
1152             !i915_sched_engine_is_empty(sched_engine))
1153                 queue_request(sched_engine, rq, rq_prio(rq));
1154         else if (guc_bypass_tasklet_submit(guc, rq) == -EBUSY)
1155                 tasklet_hi_schedule(&sched_engine->tasklet);
1156
1157         spin_unlock_irqrestore(&sched_engine->lock, flags);
1158 }
1159
1160 static int new_guc_id(struct intel_guc *guc)
1161 {
1162         return ida_simple_get(&guc->guc_ids, 0,
1163                               GUC_MAX_LRC_DESCRIPTORS, GFP_KERNEL |
1164                               __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
1165 }
1166
1167 static void __release_guc_id(struct intel_guc *guc, struct intel_context *ce)
1168 {
1169         if (!context_guc_id_invalid(ce)) {
1170                 ida_simple_remove(&guc->guc_ids, ce->guc_id.id);
1171                 reset_lrc_desc(guc, ce->guc_id.id);
1172                 set_context_guc_id_invalid(ce);
1173         }
1174         if (!list_empty(&ce->guc_id.link))
1175                 list_del_init(&ce->guc_id.link);
1176 }
1177
1178 static void release_guc_id(struct intel_guc *guc, struct intel_context *ce)
1179 {
1180         unsigned long flags;
1181
1182         spin_lock_irqsave(&guc->contexts_lock, flags);
1183         __release_guc_id(guc, ce);
1184         spin_unlock_irqrestore(&guc->contexts_lock, flags);
1185 }
1186
1187 static int steal_guc_id(struct intel_guc *guc)
1188 {
1189         struct intel_context *ce;
1190         int guc_id;
1191
1192         lockdep_assert_held(&guc->contexts_lock);
1193
1194         if (!list_empty(&guc->guc_id_list)) {
1195                 ce = list_first_entry(&guc->guc_id_list,
1196                                       struct intel_context,
1197                                       guc_id.link);
1198
1199                 GEM_BUG_ON(atomic_read(&ce->guc_id.ref));
1200                 GEM_BUG_ON(context_guc_id_invalid(ce));
1201
1202                 list_del_init(&ce->guc_id.link);
1203                 guc_id = ce->guc_id.id;
1204
1205                 spin_lock(&ce->guc_state.lock);
1206                 clr_context_registered(ce);
1207                 spin_unlock(&ce->guc_state.lock);
1208
1209                 set_context_guc_id_invalid(ce);
1210                 return guc_id;
1211         } else {
1212                 return -EAGAIN;
1213         }
1214 }
1215
1216 static int assign_guc_id(struct intel_guc *guc, u16 *out)
1217 {
1218         int ret;
1219
1220         lockdep_assert_held(&guc->contexts_lock);
1221
1222         ret = new_guc_id(guc);
1223         if (unlikely(ret < 0)) {
1224                 ret = steal_guc_id(guc);
1225                 if (ret < 0)
1226                         return ret;
1227         }
1228
1229         *out = ret;
1230         return 0;
1231 }
1232
1233 #define PIN_GUC_ID_TRIES        4
1234 static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
1235 {
1236         int ret = 0;
1237         unsigned long flags, tries = PIN_GUC_ID_TRIES;
1238
1239         GEM_BUG_ON(atomic_read(&ce->guc_id.ref));
1240
1241 try_again:
1242         spin_lock_irqsave(&guc->contexts_lock, flags);
1243
1244         might_lock(&ce->guc_state.lock);
1245
1246         if (context_guc_id_invalid(ce)) {
1247                 ret = assign_guc_id(guc, &ce->guc_id.id);
1248                 if (ret)
1249                         goto out_unlock;
1250                 ret = 1;        /* Indidcates newly assigned guc_id */
1251         }
1252         if (!list_empty(&ce->guc_id.link))
1253                 list_del_init(&ce->guc_id.link);
1254         atomic_inc(&ce->guc_id.ref);
1255
1256 out_unlock:
1257         spin_unlock_irqrestore(&guc->contexts_lock, flags);
1258
1259         /*
1260          * -EAGAIN indicates no guc_id are available, let's retire any
1261          * outstanding requests to see if that frees up a guc_id. If the first
1262          * retire didn't help, insert a sleep with the timeslice duration before
1263          * attempting to retire more requests. Double the sleep period each
1264          * subsequent pass before finally giving up. The sleep period has max of
1265          * 100ms and minimum of 1ms.
1266          */
1267         if (ret == -EAGAIN && --tries) {
1268                 if (PIN_GUC_ID_TRIES - tries > 1) {
1269                         unsigned int timeslice_shifted =
1270                                 ce->engine->props.timeslice_duration_ms <<
1271                                 (PIN_GUC_ID_TRIES - tries - 2);
1272                         unsigned int max = min_t(unsigned int, 100,
1273                                                  timeslice_shifted);
1274
1275                         msleep(max_t(unsigned int, max, 1));
1276                 }
1277                 intel_gt_retire_requests(guc_to_gt(guc));
1278                 goto try_again;
1279         }
1280
1281         return ret;
1282 }
1283
1284 static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
1285 {
1286         unsigned long flags;
1287
1288         GEM_BUG_ON(atomic_read(&ce->guc_id.ref) < 0);
1289
1290         if (unlikely(context_guc_id_invalid(ce)))
1291                 return;
1292
1293         spin_lock_irqsave(&guc->contexts_lock, flags);
1294         if (!context_guc_id_invalid(ce) && list_empty(&ce->guc_id.link) &&
1295             !atomic_read(&ce->guc_id.ref))
1296                 list_add_tail(&ce->guc_id.link, &guc->guc_id_list);
1297         spin_unlock_irqrestore(&guc->contexts_lock, flags);
1298 }
1299
1300 static int __guc_action_register_context(struct intel_guc *guc,
1301                                          u32 guc_id,
1302                                          u32 offset,
1303                                          bool loop)
1304 {
1305         u32 action[] = {
1306                 INTEL_GUC_ACTION_REGISTER_CONTEXT,
1307                 guc_id,
1308                 offset,
1309         };
1310
1311         return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
1312                                              0, loop);
1313 }
1314
1315 static int register_context(struct intel_context *ce, bool loop)
1316 {
1317         struct intel_guc *guc = ce_to_guc(ce);
1318         u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool) +
1319                 ce->guc_id.id * sizeof(struct guc_lrc_desc);
1320         int ret;
1321
1322         trace_intel_context_register(ce);
1323
1324         ret = __guc_action_register_context(guc, ce->guc_id.id, offset, loop);
1325         if (likely(!ret)) {
1326                 unsigned long flags;
1327
1328                 spin_lock_irqsave(&ce->guc_state.lock, flags);
1329                 set_context_registered(ce);
1330                 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1331         }
1332
1333         return ret;
1334 }
1335
1336 static int __guc_action_deregister_context(struct intel_guc *guc,
1337                                            u32 guc_id)
1338 {
1339         u32 action[] = {
1340                 INTEL_GUC_ACTION_DEREGISTER_CONTEXT,
1341                 guc_id,
1342         };
1343
1344         return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
1345                                              G2H_LEN_DW_DEREGISTER_CONTEXT,
1346                                              true);
1347 }
1348
1349 static int deregister_context(struct intel_context *ce, u32 guc_id)
1350 {
1351         struct intel_guc *guc = ce_to_guc(ce);
1352
1353         trace_intel_context_deregister(ce);
1354
1355         return __guc_action_deregister_context(guc, guc_id);
1356 }
1357
1358 static intel_engine_mask_t adjust_engine_mask(u8 class, intel_engine_mask_t mask)
1359 {
1360         switch (class) {
1361         case RENDER_CLASS:
1362                 return mask >> RCS0;
1363         case VIDEO_ENHANCEMENT_CLASS:
1364                 return mask >> VECS0;
1365         case VIDEO_DECODE_CLASS:
1366                 return mask >> VCS0;
1367         case COPY_ENGINE_CLASS:
1368                 return mask >> BCS0;
1369         default:
1370                 MISSING_CASE(class);
1371                 return 0;
1372         }
1373 }
1374
1375 static void guc_context_policy_init(struct intel_engine_cs *engine,
1376                                     struct guc_lrc_desc *desc)
1377 {
1378         desc->policy_flags = 0;
1379
1380         if (engine->flags & I915_ENGINE_WANT_FORCED_PREEMPTION)
1381                 desc->policy_flags |= CONTEXT_POLICY_FLAG_PREEMPT_TO_IDLE;
1382
1383         /* NB: For both of these, zero means disabled. */
1384         desc->execution_quantum = engine->props.timeslice_duration_ms * 1000;
1385         desc->preemption_timeout = engine->props.preempt_timeout_ms * 1000;
1386 }
1387
1388 static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
1389 {
1390         struct intel_engine_cs *engine = ce->engine;
1391         struct intel_runtime_pm *runtime_pm = engine->uncore->rpm;
1392         struct intel_guc *guc = &engine->gt->uc.guc;
1393         u32 desc_idx = ce->guc_id.id;
1394         struct guc_lrc_desc *desc;
1395         bool context_registered;
1396         intel_wakeref_t wakeref;
1397         int ret = 0;
1398
1399         GEM_BUG_ON(!engine->mask);
1400         GEM_BUG_ON(!sched_state_is_init(ce));
1401
1402         /*
1403          * Ensure LRC + CT vmas are is same region as write barrier is done
1404          * based on CT vma region.
1405          */
1406         GEM_BUG_ON(i915_gem_object_is_lmem(guc->ct.vma->obj) !=
1407                    i915_gem_object_is_lmem(ce->ring->vma->obj));
1408
1409         context_registered = lrc_desc_registered(guc, desc_idx);
1410
1411         reset_lrc_desc(guc, desc_idx);
1412         set_lrc_desc_registered(guc, desc_idx, ce);
1413
1414         desc = __get_lrc_desc(guc, desc_idx);
1415         desc->engine_class = engine_class_to_guc_class(engine->class);
1416         desc->engine_submit_mask = adjust_engine_mask(engine->class,
1417                                                       engine->mask);
1418         desc->hw_context_desc = ce->lrc.lrca;
1419         desc->priority = ce->guc_state.prio;
1420         desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
1421         guc_context_policy_init(engine, desc);
1422
1423         /*
1424          * The context_lookup xarray is used to determine if the hardware
1425          * context is currently registered. There are two cases in which it
1426          * could be registered either the guc_id has been stolen from another
1427          * context or the lrc descriptor address of this context has changed. In
1428          * either case the context needs to be deregistered with the GuC before
1429          * registering this context.
1430          */
1431         if (context_registered) {
1432                 bool disabled;
1433                 unsigned long flags;
1434
1435                 trace_intel_context_steal_guc_id(ce);
1436                 GEM_BUG_ON(!loop);
1437
1438                 /* Seal race with Reset */
1439                 spin_lock_irqsave(&ce->guc_state.lock, flags);
1440                 disabled = submission_disabled(guc);
1441                 if (likely(!disabled)) {
1442                         set_context_wait_for_deregister_to_register(ce);
1443                         intel_context_get(ce);
1444                 }
1445                 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1446                 if (unlikely(disabled)) {
1447                         reset_lrc_desc(guc, desc_idx);
1448                         return 0;       /* Will get registered later */
1449                 }
1450
1451                 /*
1452                  * If stealing the guc_id, this ce has the same guc_id as the
1453                  * context whose guc_id was stolen.
1454                  */
1455                 with_intel_runtime_pm(runtime_pm, wakeref)
1456                         ret = deregister_context(ce, ce->guc_id.id);
1457                 if (unlikely(ret == -ENODEV))
1458                         ret = 0;        /* Will get registered later */
1459         } else {
1460                 with_intel_runtime_pm(runtime_pm, wakeref)
1461                         ret = register_context(ce, loop);
1462                 if (unlikely(ret == -EBUSY)) {
1463                         reset_lrc_desc(guc, desc_idx);
1464                 } else if (unlikely(ret == -ENODEV)) {
1465                         reset_lrc_desc(guc, desc_idx);
1466                         ret = 0;        /* Will get registered later */
1467                 }
1468         }
1469
1470         return ret;
1471 }
1472
1473 static int __guc_context_pre_pin(struct intel_context *ce,
1474                                  struct intel_engine_cs *engine,
1475                                  struct i915_gem_ww_ctx *ww,
1476                                  void **vaddr)
1477 {
1478         return lrc_pre_pin(ce, engine, ww, vaddr);
1479 }
1480
1481 static int __guc_context_pin(struct intel_context *ce,
1482                              struct intel_engine_cs *engine,
1483                              void *vaddr)
1484 {
1485         if (i915_ggtt_offset(ce->state) !=
1486             (ce->lrc.lrca & CTX_GTT_ADDRESS_MASK))
1487                 set_bit(CONTEXT_LRCA_DIRTY, &ce->flags);
1488
1489         /*
1490          * GuC context gets pinned in guc_request_alloc. See that function for
1491          * explaination of why.
1492          */
1493
1494         return lrc_pin(ce, engine, vaddr);
1495 }
1496
1497 static int guc_context_pre_pin(struct intel_context *ce,
1498                                struct i915_gem_ww_ctx *ww,
1499                                void **vaddr)
1500 {
1501         return __guc_context_pre_pin(ce, ce->engine, ww, vaddr);
1502 }
1503
1504 static int guc_context_pin(struct intel_context *ce, void *vaddr)
1505 {
1506         return __guc_context_pin(ce, ce->engine, vaddr);
1507 }
1508
1509 static void guc_context_unpin(struct intel_context *ce)
1510 {
1511         struct intel_guc *guc = ce_to_guc(ce);
1512
1513         unpin_guc_id(guc, ce);
1514         lrc_unpin(ce);
1515 }
1516
1517 static void guc_context_post_unpin(struct intel_context *ce)
1518 {
1519         lrc_post_unpin(ce);
1520 }
1521
1522 static void __guc_context_sched_enable(struct intel_guc *guc,
1523                                        struct intel_context *ce)
1524 {
1525         u32 action[] = {
1526                 INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET,
1527                 ce->guc_id.id,
1528                 GUC_CONTEXT_ENABLE
1529         };
1530
1531         trace_intel_context_sched_enable(ce);
1532
1533         guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
1534                                       G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
1535 }
1536
1537 static void __guc_context_sched_disable(struct intel_guc *guc,
1538                                         struct intel_context *ce,
1539                                         u16 guc_id)
1540 {
1541         u32 action[] = {
1542                 INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET,
1543                 guc_id, /* ce->guc_id.id not stable */
1544                 GUC_CONTEXT_DISABLE
1545         };
1546
1547         GEM_BUG_ON(guc_id == GUC_INVALID_LRC_ID);
1548
1549         trace_intel_context_sched_disable(ce);
1550
1551         guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
1552                                       G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
1553 }
1554
1555 static void guc_blocked_fence_complete(struct intel_context *ce)
1556 {
1557         lockdep_assert_held(&ce->guc_state.lock);
1558
1559         if (!i915_sw_fence_done(&ce->guc_state.blocked))
1560                 i915_sw_fence_complete(&ce->guc_state.blocked);
1561 }
1562
1563 static void guc_blocked_fence_reinit(struct intel_context *ce)
1564 {
1565         lockdep_assert_held(&ce->guc_state.lock);
1566         GEM_BUG_ON(!i915_sw_fence_done(&ce->guc_state.blocked));
1567
1568         /*
1569          * This fence is always complete unless a pending schedule disable is
1570          * outstanding. We arm the fence here and complete it when we receive
1571          * the pending schedule disable complete message.
1572          */
1573         i915_sw_fence_fini(&ce->guc_state.blocked);
1574         i915_sw_fence_reinit(&ce->guc_state.blocked);
1575         i915_sw_fence_await(&ce->guc_state.blocked);
1576         i915_sw_fence_commit(&ce->guc_state.blocked);
1577 }
1578
1579 static u16 prep_context_pending_disable(struct intel_context *ce)
1580 {
1581         lockdep_assert_held(&ce->guc_state.lock);
1582
1583         set_context_pending_disable(ce);
1584         clr_context_enabled(ce);
1585         guc_blocked_fence_reinit(ce);
1586         intel_context_get(ce);
1587
1588         return ce->guc_id.id;
1589 }
1590
1591 static struct i915_sw_fence *guc_context_block(struct intel_context *ce)
1592 {
1593         struct intel_guc *guc = ce_to_guc(ce);
1594         unsigned long flags;
1595         struct intel_runtime_pm *runtime_pm = ce->engine->uncore->rpm;
1596         intel_wakeref_t wakeref;
1597         u16 guc_id;
1598         bool enabled;
1599
1600         spin_lock_irqsave(&ce->guc_state.lock, flags);
1601
1602         incr_context_blocked(ce);
1603
1604         enabled = context_enabled(ce);
1605         if (unlikely(!enabled || submission_disabled(guc))) {
1606                 if (enabled)
1607                         clr_context_enabled(ce);
1608                 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1609                 return &ce->guc_state.blocked;
1610         }
1611
1612         /*
1613          * We add +2 here as the schedule disable complete CTB handler calls
1614          * intel_context_sched_disable_unpin (-2 to pin_count).
1615          */
1616         atomic_add(2, &ce->pin_count);
1617
1618         guc_id = prep_context_pending_disable(ce);
1619
1620         spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1621
1622         with_intel_runtime_pm(runtime_pm, wakeref)
1623                 __guc_context_sched_disable(guc, ce, guc_id);
1624
1625         return &ce->guc_state.blocked;
1626 }
1627
1628 #define SCHED_STATE_MULTI_BLOCKED_MASK \
1629         (SCHED_STATE_BLOCKED_MASK & ~SCHED_STATE_BLOCKED)
1630 #define SCHED_STATE_NO_UNBLOCK \
1631         (SCHED_STATE_MULTI_BLOCKED_MASK | \
1632          SCHED_STATE_PENDING_DISABLE | \
1633          SCHED_STATE_BANNED)
1634
1635 static bool context_cant_unblock(struct intel_context *ce)
1636 {
1637         lockdep_assert_held(&ce->guc_state.lock);
1638
1639         return (ce->guc_state.sched_state & SCHED_STATE_NO_UNBLOCK) ||
1640                 context_guc_id_invalid(ce) ||
1641                 !lrc_desc_registered(ce_to_guc(ce), ce->guc_id.id) ||
1642                 !intel_context_is_pinned(ce);
1643 }
1644
1645 static void guc_context_unblock(struct intel_context *ce)
1646 {
1647         struct intel_guc *guc = ce_to_guc(ce);
1648         unsigned long flags;
1649         struct intel_runtime_pm *runtime_pm = ce->engine->uncore->rpm;
1650         intel_wakeref_t wakeref;
1651         bool enable;
1652
1653         GEM_BUG_ON(context_enabled(ce));
1654
1655         spin_lock_irqsave(&ce->guc_state.lock, flags);
1656
1657         if (unlikely(submission_disabled(guc) ||
1658                      context_cant_unblock(ce))) {
1659                 enable = false;
1660         } else {
1661                 enable = true;
1662                 set_context_pending_enable(ce);
1663                 set_context_enabled(ce);
1664                 intel_context_get(ce);
1665         }
1666
1667         decr_context_blocked(ce);
1668
1669         spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1670
1671         if (enable) {
1672                 with_intel_runtime_pm(runtime_pm, wakeref)
1673                         __guc_context_sched_enable(guc, ce);
1674         }
1675 }
1676
1677 static void guc_context_cancel_request(struct intel_context *ce,
1678                                        struct i915_request *rq)
1679 {
1680         if (i915_sw_fence_signaled(&rq->submit)) {
1681                 struct i915_sw_fence *fence;
1682
1683                 intel_context_get(ce);
1684                 fence = guc_context_block(ce);
1685                 i915_sw_fence_wait(fence);
1686                 if (!i915_request_completed(rq)) {
1687                         __i915_request_skip(rq);
1688                         guc_reset_state(ce, intel_ring_wrap(ce->ring, rq->head),
1689                                         true);
1690                 }
1691
1692                 /*
1693                  * XXX: Racey if context is reset, see comment in
1694                  * __guc_reset_context().
1695                  */
1696                 flush_work(&ce_to_guc(ce)->ct.requests.worker);
1697
1698                 guc_context_unblock(ce);
1699                 intel_context_put(ce);
1700         }
1701 }
1702
1703 static void __guc_context_set_preemption_timeout(struct intel_guc *guc,
1704                                                  u16 guc_id,
1705                                                  u32 preemption_timeout)
1706 {
1707         u32 action[] = {
1708                 INTEL_GUC_ACTION_SET_CONTEXT_PREEMPTION_TIMEOUT,
1709                 guc_id,
1710                 preemption_timeout
1711         };
1712
1713         intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);
1714 }
1715
1716 static void guc_context_ban(struct intel_context *ce, struct i915_request *rq)
1717 {
1718         struct intel_guc *guc = ce_to_guc(ce);
1719         struct intel_runtime_pm *runtime_pm =
1720                 &ce->engine->gt->i915->runtime_pm;
1721         intel_wakeref_t wakeref;
1722         unsigned long flags;
1723
1724         guc_flush_submissions(guc);
1725
1726         spin_lock_irqsave(&ce->guc_state.lock, flags);
1727         set_context_banned(ce);
1728
1729         if (submission_disabled(guc) ||
1730             (!context_enabled(ce) && !context_pending_disable(ce))) {
1731                 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1732
1733                 guc_cancel_context_requests(ce);
1734                 intel_engine_signal_breadcrumbs(ce->engine);
1735         } else if (!context_pending_disable(ce)) {
1736                 u16 guc_id;
1737
1738                 /*
1739                  * We add +2 here as the schedule disable complete CTB handler
1740                  * calls intel_context_sched_disable_unpin (-2 to pin_count).
1741                  */
1742                 atomic_add(2, &ce->pin_count);
1743
1744                 guc_id = prep_context_pending_disable(ce);
1745                 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1746
1747                 /*
1748                  * In addition to disabling scheduling, set the preemption
1749                  * timeout to the minimum value (1 us) so the banned context
1750                  * gets kicked off the HW ASAP.
1751                  */
1752                 with_intel_runtime_pm(runtime_pm, wakeref) {
1753                         __guc_context_set_preemption_timeout(guc, guc_id, 1);
1754                         __guc_context_sched_disable(guc, ce, guc_id);
1755                 }
1756         } else {
1757                 if (!context_guc_id_invalid(ce))
1758                         with_intel_runtime_pm(runtime_pm, wakeref)
1759                                 __guc_context_set_preemption_timeout(guc,
1760                                                                      ce->guc_id.id,
1761                                                                      1);
1762                 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1763         }
1764 }
1765
1766 static void guc_context_sched_disable(struct intel_context *ce)
1767 {
1768         struct intel_guc *guc = ce_to_guc(ce);
1769         unsigned long flags;
1770         struct intel_runtime_pm *runtime_pm = &ce->engine->gt->i915->runtime_pm;
1771         intel_wakeref_t wakeref;
1772         u16 guc_id;
1773
1774         spin_lock_irqsave(&ce->guc_state.lock, flags);
1775
1776         /*
1777          * We have to check if the context has been disabled by another thread,
1778          * check if submssion has been disabled to seal a race with reset and
1779          * finally check if any more requests have been committed to the
1780          * context ensursing that a request doesn't slip through the
1781          * 'context_pending_disable' fence.
1782          */
1783         if (unlikely(!context_enabled(ce) || submission_disabled(guc) ||
1784                      context_has_committed_requests(ce))) {
1785                 clr_context_enabled(ce);
1786                 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1787                 goto unpin;
1788         }
1789         guc_id = prep_context_pending_disable(ce);
1790
1791         spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1792
1793         with_intel_runtime_pm(runtime_pm, wakeref)
1794                 __guc_context_sched_disable(guc, ce, guc_id);
1795
1796         return;
1797 unpin:
1798         intel_context_sched_disable_unpin(ce);
1799 }
1800
1801 static inline void guc_lrc_desc_unpin(struct intel_context *ce)
1802 {
1803         struct intel_guc *guc = ce_to_guc(ce);
1804
1805         GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id.id));
1806         GEM_BUG_ON(ce != __get_context(guc, ce->guc_id.id));
1807         GEM_BUG_ON(context_enabled(ce));
1808
1809         deregister_context(ce, ce->guc_id.id);
1810 }
1811
1812 static void __guc_context_destroy(struct intel_context *ce)
1813 {
1814         GEM_BUG_ON(ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_KMD_HIGH] ||
1815                    ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_HIGH] ||
1816                    ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_KMD_NORMAL] ||
1817                    ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_NORMAL]);
1818         GEM_BUG_ON(ce->guc_state.number_committed_requests);
1819
1820         lrc_fini(ce);
1821         intel_context_fini(ce);
1822
1823         if (intel_engine_is_virtual(ce->engine)) {
1824                 struct guc_virtual_engine *ve =
1825                         container_of(ce, typeof(*ve), context);
1826
1827                 if (ve->base.breadcrumbs)
1828                         intel_breadcrumbs_put(ve->base.breadcrumbs);
1829
1830                 kfree(ve);
1831         } else {
1832                 intel_context_free(ce);
1833         }
1834 }
1835
1836 static void guc_context_destroy(struct kref *kref)
1837 {
1838         struct intel_context *ce = container_of(kref, typeof(*ce), ref);
1839         struct intel_runtime_pm *runtime_pm = ce->engine->uncore->rpm;
1840         struct intel_guc *guc = ce_to_guc(ce);
1841         intel_wakeref_t wakeref;
1842         unsigned long flags;
1843         bool disabled;
1844
1845         /*
1846          * If the guc_id is invalid this context has been stolen and we can free
1847          * it immediately. Also can be freed immediately if the context is not
1848          * registered with the GuC or the GuC is in the middle of a reset.
1849          */
1850         if (context_guc_id_invalid(ce)) {
1851                 __guc_context_destroy(ce);
1852                 return;
1853         } else if (submission_disabled(guc) ||
1854                    !lrc_desc_registered(guc, ce->guc_id.id)) {
1855                 release_guc_id(guc, ce);
1856                 __guc_context_destroy(ce);
1857                 return;
1858         }
1859
1860         /*
1861          * We have to acquire the context spinlock and check guc_id again, if it
1862          * is valid it hasn't been stolen and needs to be deregistered. We
1863          * delete this context from the list of unpinned guc_id available to
1864          * steal to seal a race with guc_lrc_desc_pin(). When the G2H CTB
1865          * returns indicating this context has been deregistered the guc_id is
1866          * returned to the pool of available guc_id.
1867          */
1868         spin_lock_irqsave(&guc->contexts_lock, flags);
1869         if (context_guc_id_invalid(ce)) {
1870                 spin_unlock_irqrestore(&guc->contexts_lock, flags);
1871                 __guc_context_destroy(ce);
1872                 return;
1873         }
1874
1875         if (!list_empty(&ce->guc_id.link))
1876                 list_del_init(&ce->guc_id.link);
1877         spin_unlock_irqrestore(&guc->contexts_lock, flags);
1878
1879         /* Seal race with Reset */
1880         spin_lock_irqsave(&ce->guc_state.lock, flags);
1881         disabled = submission_disabled(guc);
1882         if (likely(!disabled)) {
1883                 set_context_destroyed(ce);
1884                 clr_context_registered(ce);
1885         }
1886         spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1887         if (unlikely(disabled)) {
1888                 release_guc_id(guc, ce);
1889                 __guc_context_destroy(ce);
1890                 return;
1891         }
1892
1893         /*
1894          * We defer GuC context deregistration until the context is destroyed
1895          * in order to save on CTBs. With this optimization ideally we only need
1896          * 1 CTB to register the context during the first pin and 1 CTB to
1897          * deregister the context when the context is destroyed. Without this
1898          * optimization, a CTB would be needed every pin & unpin.
1899          *
1900          * XXX: Need to acqiure the runtime wakeref as this can be triggered
1901          * from context_free_worker when runtime wakeref is not held.
1902          * guc_lrc_desc_unpin requires the runtime as a GuC register is written
1903          * in H2G CTB to deregister the context. A future patch may defer this
1904          * H2G CTB if the runtime wakeref is zero.
1905          */
1906         with_intel_runtime_pm(runtime_pm, wakeref)
1907                 guc_lrc_desc_unpin(ce);
1908 }
1909
1910 static int guc_context_alloc(struct intel_context *ce)
1911 {
1912         return lrc_alloc(ce, ce->engine);
1913 }
1914
1915 static void guc_context_set_prio(struct intel_guc *guc,
1916                                  struct intel_context *ce,
1917                                  u8 prio)
1918 {
1919         u32 action[] = {
1920                 INTEL_GUC_ACTION_SET_CONTEXT_PRIORITY,
1921                 ce->guc_id.id,
1922                 prio,
1923         };
1924
1925         GEM_BUG_ON(prio < GUC_CLIENT_PRIORITY_KMD_HIGH ||
1926                    prio > GUC_CLIENT_PRIORITY_NORMAL);
1927         lockdep_assert_held(&ce->guc_state.lock);
1928
1929         if (ce->guc_state.prio == prio || submission_disabled(guc) ||
1930             !context_registered(ce)) {
1931                 ce->guc_state.prio = prio;
1932                 return;
1933         }
1934
1935         guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);
1936
1937         ce->guc_state.prio = prio;
1938         trace_intel_context_set_prio(ce);
1939 }
1940
1941 static inline u8 map_i915_prio_to_guc_prio(int prio)
1942 {
1943         if (prio == I915_PRIORITY_NORMAL)
1944                 return GUC_CLIENT_PRIORITY_KMD_NORMAL;
1945         else if (prio < I915_PRIORITY_NORMAL)
1946                 return GUC_CLIENT_PRIORITY_NORMAL;
1947         else if (prio < I915_PRIORITY_DISPLAY)
1948                 return GUC_CLIENT_PRIORITY_HIGH;
1949         else
1950                 return GUC_CLIENT_PRIORITY_KMD_HIGH;
1951 }
1952
1953 static inline void add_context_inflight_prio(struct intel_context *ce,
1954                                              u8 guc_prio)
1955 {
1956         lockdep_assert_held(&ce->guc_state.lock);
1957         GEM_BUG_ON(guc_prio >= ARRAY_SIZE(ce->guc_state.prio_count));
1958
1959         ++ce->guc_state.prio_count[guc_prio];
1960
1961         /* Overflow protection */
1962         GEM_WARN_ON(!ce->guc_state.prio_count[guc_prio]);
1963 }
1964
1965 static inline void sub_context_inflight_prio(struct intel_context *ce,
1966                                              u8 guc_prio)
1967 {
1968         lockdep_assert_held(&ce->guc_state.lock);
1969         GEM_BUG_ON(guc_prio >= ARRAY_SIZE(ce->guc_state.prio_count));
1970
1971         /* Underflow protection */
1972         GEM_WARN_ON(!ce->guc_state.prio_count[guc_prio]);
1973
1974         --ce->guc_state.prio_count[guc_prio];
1975 }
1976
1977 static inline void update_context_prio(struct intel_context *ce)
1978 {
1979         struct intel_guc *guc = &ce->engine->gt->uc.guc;
1980         int i;
1981
1982         BUILD_BUG_ON(GUC_CLIENT_PRIORITY_KMD_HIGH != 0);
1983         BUILD_BUG_ON(GUC_CLIENT_PRIORITY_KMD_HIGH > GUC_CLIENT_PRIORITY_NORMAL);
1984
1985         lockdep_assert_held(&ce->guc_state.lock);
1986
1987         for (i = 0; i < ARRAY_SIZE(ce->guc_state.prio_count); ++i) {
1988                 if (ce->guc_state.prio_count[i]) {
1989                         guc_context_set_prio(guc, ce, i);
1990                         break;
1991                 }
1992         }
1993 }
1994
1995 static inline bool new_guc_prio_higher(u8 old_guc_prio, u8 new_guc_prio)
1996 {
1997         /* Lower value is higher priority */
1998         return new_guc_prio < old_guc_prio;
1999 }
2000
2001 static void add_to_context(struct i915_request *rq)
2002 {
2003         struct intel_context *ce = rq->context;
2004         u8 new_guc_prio = map_i915_prio_to_guc_prio(rq_prio(rq));
2005
2006         GEM_BUG_ON(rq->guc_prio == GUC_PRIO_FINI);
2007
2008         spin_lock(&ce->guc_state.lock);
2009         list_move_tail(&rq->sched.link, &ce->guc_state.requests);
2010
2011         if (rq->guc_prio == GUC_PRIO_INIT) {
2012                 rq->guc_prio = new_guc_prio;
2013                 add_context_inflight_prio(ce, rq->guc_prio);
2014         } else if (new_guc_prio_higher(rq->guc_prio, new_guc_prio)) {
2015                 sub_context_inflight_prio(ce, rq->guc_prio);
2016                 rq->guc_prio = new_guc_prio;
2017                 add_context_inflight_prio(ce, rq->guc_prio);
2018         }
2019         update_context_prio(ce);
2020
2021         spin_unlock(&ce->guc_state.lock);
2022 }
2023
2024 static void guc_prio_fini(struct i915_request *rq, struct intel_context *ce)
2025 {
2026         lockdep_assert_held(&ce->guc_state.lock);
2027
2028         if (rq->guc_prio != GUC_PRIO_INIT &&
2029             rq->guc_prio != GUC_PRIO_FINI) {
2030                 sub_context_inflight_prio(ce, rq->guc_prio);
2031                 update_context_prio(ce);
2032         }
2033         rq->guc_prio = GUC_PRIO_FINI;
2034 }
2035
2036 static void remove_from_context(struct i915_request *rq)
2037 {
2038         struct intel_context *ce = rq->context;
2039
2040         spin_lock_irq(&ce->guc_state.lock);
2041
2042         list_del_init(&rq->sched.link);
2043         clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
2044
2045         /* Prevent further __await_execution() registering a cb, then flush */
2046         set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
2047
2048         guc_prio_fini(rq, ce);
2049
2050         decr_context_committed_requests(ce);
2051
2052         spin_unlock_irq(&ce->guc_state.lock);
2053
2054         atomic_dec(&ce->guc_id.ref);
2055         i915_request_notify_execute_cb_imm(rq);
2056 }
2057
2058 static const struct intel_context_ops guc_context_ops = {
2059         .alloc = guc_context_alloc,
2060
2061         .pre_pin = guc_context_pre_pin,
2062         .pin = guc_context_pin,
2063         .unpin = guc_context_unpin,
2064         .post_unpin = guc_context_post_unpin,
2065
2066         .ban = guc_context_ban,
2067
2068         .cancel_request = guc_context_cancel_request,
2069
2070         .enter = intel_context_enter_engine,
2071         .exit = intel_context_exit_engine,
2072
2073         .sched_disable = guc_context_sched_disable,
2074
2075         .reset = lrc_reset,
2076         .destroy = guc_context_destroy,
2077
2078         .create_virtual = guc_create_virtual,
2079 };
2080
2081 static void submit_work_cb(struct irq_work *wrk)
2082 {
2083         struct i915_request *rq = container_of(wrk, typeof(*rq), submit_work);
2084
2085         might_lock(&rq->engine->sched_engine->lock);
2086         i915_sw_fence_complete(&rq->submit);
2087 }
2088
2089 static void __guc_signal_context_fence(struct intel_context *ce)
2090 {
2091         struct i915_request *rq, *rn;
2092
2093         lockdep_assert_held(&ce->guc_state.lock);
2094
2095         if (!list_empty(&ce->guc_state.fences))
2096                 trace_intel_context_fence_release(ce);
2097
2098         /*
2099          * Use an IRQ to ensure locking order of sched_engine->lock ->
2100          * ce->guc_state.lock is preserved.
2101          */
2102         list_for_each_entry_safe(rq, rn, &ce->guc_state.fences,
2103                                  guc_fence_link) {
2104                 list_del(&rq->guc_fence_link);
2105                 irq_work_queue(&rq->submit_work);
2106         }
2107
2108         INIT_LIST_HEAD(&ce->guc_state.fences);
2109 }
2110
2111 static void guc_signal_context_fence(struct intel_context *ce)
2112 {
2113         unsigned long flags;
2114
2115         spin_lock_irqsave(&ce->guc_state.lock, flags);
2116         clr_context_wait_for_deregister_to_register(ce);
2117         __guc_signal_context_fence(ce);
2118         spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2119 }
2120
2121 static bool context_needs_register(struct intel_context *ce, bool new_guc_id)
2122 {
2123         return (new_guc_id || test_bit(CONTEXT_LRCA_DIRTY, &ce->flags) ||
2124                 !lrc_desc_registered(ce_to_guc(ce), ce->guc_id.id)) &&
2125                 !submission_disabled(ce_to_guc(ce));
2126 }
2127
2128 static void guc_context_init(struct intel_context *ce)
2129 {
2130         const struct i915_gem_context *ctx;
2131         int prio = I915_CONTEXT_DEFAULT_PRIORITY;
2132
2133         rcu_read_lock();
2134         ctx = rcu_dereference(ce->gem_context);
2135         if (ctx)
2136                 prio = ctx->sched.priority;
2137         rcu_read_unlock();
2138
2139         ce->guc_state.prio = map_i915_prio_to_guc_prio(prio);
2140         set_bit(CONTEXT_GUC_INIT, &ce->flags);
2141 }
2142
2143 static int guc_request_alloc(struct i915_request *rq)
2144 {
2145         struct intel_context *ce = rq->context;
2146         struct intel_guc *guc = ce_to_guc(ce);
2147         unsigned long flags;
2148         int ret;
2149
2150         GEM_BUG_ON(!intel_context_is_pinned(rq->context));
2151
2152         /*
2153          * Flush enough space to reduce the likelihood of waiting after
2154          * we start building the request - in which case we will just
2155          * have to repeat work.
2156          */
2157         rq->reserved_space += GUC_REQUEST_SIZE;
2158
2159         /*
2160          * Note that after this point, we have committed to using
2161          * this request as it is being used to both track the
2162          * state of engine initialisation and liveness of the
2163          * golden renderstate above. Think twice before you try
2164          * to cancel/unwind this request now.
2165          */
2166
2167         /* Unconditionally invalidate GPU caches and TLBs. */
2168         ret = rq->engine->emit_flush(rq, EMIT_INVALIDATE);
2169         if (ret)
2170                 return ret;
2171
2172         rq->reserved_space -= GUC_REQUEST_SIZE;
2173
2174         if (unlikely(!test_bit(CONTEXT_GUC_INIT, &ce->flags)))
2175                 guc_context_init(ce);
2176
2177         /*
2178          * Call pin_guc_id here rather than in the pinning step as with
2179          * dma_resv, contexts can be repeatedly pinned / unpinned trashing the
2180          * guc_id and creating horrible race conditions. This is especially bad
2181          * when guc_id are being stolen due to over subscription. By the time
2182          * this function is reached, it is guaranteed that the guc_id will be
2183          * persistent until the generated request is retired. Thus, sealing these
2184          * race conditions. It is still safe to fail here if guc_id are
2185          * exhausted and return -EAGAIN to the user indicating that they can try
2186          * again in the future.
2187          *
2188          * There is no need for a lock here as the timeline mutex ensures at
2189          * most one context can be executing this code path at once. The
2190          * guc_id_ref is incremented once for every request in flight and
2191          * decremented on each retire. When it is zero, a lock around the
2192          * increment (in pin_guc_id) is needed to seal a race with unpin_guc_id.
2193          */
2194         if (atomic_add_unless(&ce->guc_id.ref, 1, 0))
2195                 goto out;
2196
2197         ret = pin_guc_id(guc, ce);      /* returns 1 if new guc_id assigned */
2198         if (unlikely(ret < 0))
2199                 return ret;
2200         if (context_needs_register(ce, !!ret)) {
2201                 ret = guc_lrc_desc_pin(ce, true);
2202                 if (unlikely(ret)) {    /* unwind */
2203                         if (ret == -EPIPE) {
2204                                 disable_submission(guc);
2205                                 goto out;       /* GPU will be reset */
2206                         }
2207                         atomic_dec(&ce->guc_id.ref);
2208                         unpin_guc_id(guc, ce);
2209                         return ret;
2210                 }
2211         }
2212
2213         clear_bit(CONTEXT_LRCA_DIRTY, &ce->flags);
2214
2215 out:
2216         /*
2217          * We block all requests on this context if a G2H is pending for a
2218          * schedule disable or context deregistration as the GuC will fail a
2219          * schedule enable or context registration if either G2H is pending
2220          * respectfully. Once a G2H returns, the fence is released that is
2221          * blocking these requests (see guc_signal_context_fence).
2222          */
2223         spin_lock_irqsave(&ce->guc_state.lock, flags);
2224         if (context_wait_for_deregister_to_register(ce) ||
2225             context_pending_disable(ce)) {
2226                 init_irq_work(&rq->submit_work, submit_work_cb);
2227                 i915_sw_fence_await(&rq->submit);
2228
2229                 list_add_tail(&rq->guc_fence_link, &ce->guc_state.fences);
2230         }
2231         incr_context_committed_requests(ce);
2232         spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2233
2234         return 0;
2235 }
2236
2237 static int guc_virtual_context_pre_pin(struct intel_context *ce,
2238                                        struct i915_gem_ww_ctx *ww,
2239                                        void **vaddr)
2240 {
2241         struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
2242
2243         return __guc_context_pre_pin(ce, engine, ww, vaddr);
2244 }
2245
2246 static int guc_virtual_context_pin(struct intel_context *ce, void *vaddr)
2247 {
2248         struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
2249
2250         return __guc_context_pin(ce, engine, vaddr);
2251 }
2252
2253 static void guc_virtual_context_enter(struct intel_context *ce)
2254 {
2255         intel_engine_mask_t tmp, mask = ce->engine->mask;
2256         struct intel_engine_cs *engine;
2257
2258         for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
2259                 intel_engine_pm_get(engine);
2260
2261         intel_timeline_enter(ce->timeline);
2262 }
2263
2264 static void guc_virtual_context_exit(struct intel_context *ce)
2265 {
2266         intel_engine_mask_t tmp, mask = ce->engine->mask;
2267         struct intel_engine_cs *engine;
2268
2269         for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
2270                 intel_engine_pm_put(engine);
2271
2272         intel_timeline_exit(ce->timeline);
2273 }
2274
2275 static int guc_virtual_context_alloc(struct intel_context *ce)
2276 {
2277         struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
2278
2279         return lrc_alloc(ce, engine);
2280 }
2281
2282 static const struct intel_context_ops virtual_guc_context_ops = {
2283         .alloc = guc_virtual_context_alloc,
2284
2285         .pre_pin = guc_virtual_context_pre_pin,
2286         .pin = guc_virtual_context_pin,
2287         .unpin = guc_context_unpin,
2288         .post_unpin = guc_context_post_unpin,
2289
2290         .ban = guc_context_ban,
2291
2292         .cancel_request = guc_context_cancel_request,
2293
2294         .enter = guc_virtual_context_enter,
2295         .exit = guc_virtual_context_exit,
2296
2297         .sched_disable = guc_context_sched_disable,
2298
2299         .destroy = guc_context_destroy,
2300
2301         .get_sibling = guc_virtual_get_sibling,
2302 };
2303
2304 static bool
2305 guc_irq_enable_breadcrumbs(struct intel_breadcrumbs *b)
2306 {
2307         struct intel_engine_cs *sibling;
2308         intel_engine_mask_t tmp, mask = b->engine_mask;
2309         bool result = false;
2310
2311         for_each_engine_masked(sibling, b->irq_engine->gt, mask, tmp)
2312                 result |= intel_engine_irq_enable(sibling);
2313
2314         return result;
2315 }
2316
2317 static void
2318 guc_irq_disable_breadcrumbs(struct intel_breadcrumbs *b)
2319 {
2320         struct intel_engine_cs *sibling;
2321         intel_engine_mask_t tmp, mask = b->engine_mask;
2322
2323         for_each_engine_masked(sibling, b->irq_engine->gt, mask, tmp)
2324                 intel_engine_irq_disable(sibling);
2325 }
2326
2327 static void guc_init_breadcrumbs(struct intel_engine_cs *engine)
2328 {
2329         int i;
2330
2331         /*
2332          * In GuC submission mode we do not know which physical engine a request
2333          * will be scheduled on, this creates a problem because the breadcrumb
2334          * interrupt is per physical engine. To work around this we attach
2335          * requests and direct all breadcrumb interrupts to the first instance
2336          * of an engine per class. In addition all breadcrumb interrupts are
2337          * enabled / disabled across an engine class in unison.
2338          */
2339         for (i = 0; i < MAX_ENGINE_INSTANCE; ++i) {
2340                 struct intel_engine_cs *sibling =
2341                         engine->gt->engine_class[engine->class][i];
2342
2343                 if (sibling) {
2344                         if (engine->breadcrumbs != sibling->breadcrumbs) {
2345                                 intel_breadcrumbs_put(engine->breadcrumbs);
2346                                 engine->breadcrumbs =
2347                                         intel_breadcrumbs_get(sibling->breadcrumbs);
2348                         }
2349                         break;
2350                 }
2351         }
2352
2353         if (engine->breadcrumbs) {
2354                 engine->breadcrumbs->engine_mask |= engine->mask;
2355                 engine->breadcrumbs->irq_enable = guc_irq_enable_breadcrumbs;
2356                 engine->breadcrumbs->irq_disable = guc_irq_disable_breadcrumbs;
2357         }
2358 }
2359
2360 static void guc_bump_inflight_request_prio(struct i915_request *rq,
2361                                            int prio)
2362 {
2363         struct intel_context *ce = rq->context;
2364         u8 new_guc_prio = map_i915_prio_to_guc_prio(prio);
2365
2366         /* Short circuit function */
2367         if (prio < I915_PRIORITY_NORMAL ||
2368             rq->guc_prio == GUC_PRIO_FINI ||
2369             (rq->guc_prio != GUC_PRIO_INIT &&
2370              !new_guc_prio_higher(rq->guc_prio, new_guc_prio)))
2371                 return;
2372
2373         spin_lock(&ce->guc_state.lock);
2374         if (rq->guc_prio != GUC_PRIO_FINI) {
2375                 if (rq->guc_prio != GUC_PRIO_INIT)
2376                         sub_context_inflight_prio(ce, rq->guc_prio);
2377                 rq->guc_prio = new_guc_prio;
2378                 add_context_inflight_prio(ce, rq->guc_prio);
2379                 update_context_prio(ce);
2380         }
2381         spin_unlock(&ce->guc_state.lock);
2382 }
2383
2384 static void guc_retire_inflight_request_prio(struct i915_request *rq)
2385 {
2386         struct intel_context *ce = rq->context;
2387
2388         spin_lock(&ce->guc_state.lock);
2389         guc_prio_fini(rq, ce);
2390         spin_unlock(&ce->guc_state.lock);
2391 }
2392
2393 static void sanitize_hwsp(struct intel_engine_cs *engine)
2394 {
2395         struct intel_timeline *tl;
2396
2397         list_for_each_entry(tl, &engine->status_page.timelines, engine_link)
2398                 intel_timeline_reset_seqno(tl);
2399 }
2400
2401 static void guc_sanitize(struct intel_engine_cs *engine)
2402 {
2403         /*
2404          * Poison residual state on resume, in case the suspend didn't!
2405          *
2406          * We have to assume that across suspend/resume (or other loss
2407          * of control) that the contents of our pinned buffers has been
2408          * lost, replaced by garbage. Since this doesn't always happen,
2409          * let's poison such state so that we more quickly spot when
2410          * we falsely assume it has been preserved.
2411          */
2412         if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
2413                 memset(engine->status_page.addr, POISON_INUSE, PAGE_SIZE);
2414
2415         /*
2416          * The kernel_context HWSP is stored in the status_page. As above,
2417          * that may be lost on resume/initialisation, and so we need to
2418          * reset the value in the HWSP.
2419          */
2420         sanitize_hwsp(engine);
2421
2422         /* And scrub the dirty cachelines for the HWSP */
2423         clflush_cache_range(engine->status_page.addr, PAGE_SIZE);
2424 }
2425
2426 static void setup_hwsp(struct intel_engine_cs *engine)
2427 {
2428         intel_engine_set_hwsp_writemask(engine, ~0u); /* HWSTAM */
2429
2430         ENGINE_WRITE_FW(engine,
2431                         RING_HWS_PGA,
2432                         i915_ggtt_offset(engine->status_page.vma));
2433 }
2434
2435 static void start_engine(struct intel_engine_cs *engine)
2436 {
2437         ENGINE_WRITE_FW(engine,
2438                         RING_MODE_GEN7,
2439                         _MASKED_BIT_ENABLE(GEN11_GFX_DISABLE_LEGACY_MODE));
2440
2441         ENGINE_WRITE_FW(engine, RING_MI_MODE, _MASKED_BIT_DISABLE(STOP_RING));
2442         ENGINE_POSTING_READ(engine, RING_MI_MODE);
2443 }
2444
2445 static int guc_resume(struct intel_engine_cs *engine)
2446 {
2447         assert_forcewakes_active(engine->uncore, FORCEWAKE_ALL);
2448
2449         intel_mocs_init_engine(engine);
2450
2451         intel_breadcrumbs_reset(engine->breadcrumbs);
2452
2453         setup_hwsp(engine);
2454         start_engine(engine);
2455
2456         return 0;
2457 }
2458
2459 static bool guc_sched_engine_disabled(struct i915_sched_engine *sched_engine)
2460 {
2461         return !sched_engine->tasklet.callback;
2462 }
2463
2464 static void guc_set_default_submission(struct intel_engine_cs *engine)
2465 {
2466         engine->submit_request = guc_submit_request;
2467 }
2468
2469 static inline void guc_kernel_context_pin(struct intel_guc *guc,
2470                                           struct intel_context *ce)
2471 {
2472         if (context_guc_id_invalid(ce))
2473                 pin_guc_id(guc, ce);
2474         guc_lrc_desc_pin(ce, true);
2475 }
2476
2477 static inline void guc_init_lrc_mapping(struct intel_guc *guc)
2478 {
2479         struct intel_gt *gt = guc_to_gt(guc);
2480         struct intel_engine_cs *engine;
2481         enum intel_engine_id id;
2482
2483         /* make sure all descriptors are clean... */
2484         xa_destroy(&guc->context_lookup);
2485
2486         /*
2487          * Some contexts might have been pinned before we enabled GuC
2488          * submission, so we need to add them to the GuC bookeeping.
2489          * Also, after a reset the of the GuC we want to make sure that the
2490          * information shared with GuC is properly reset. The kernel LRCs are
2491          * not attached to the gem_context, so they need to be added separately.
2492          *
2493          * Note: we purposefully do not check the return of guc_lrc_desc_pin,
2494          * because that function can only fail if a reset is just starting. This
2495          * is at the end of reset so presumably another reset isn't happening
2496          * and even it did this code would be run again.
2497          */
2498
2499         for_each_engine(engine, gt, id)
2500                 if (engine->kernel_context)
2501                         guc_kernel_context_pin(guc, engine->kernel_context);
2502 }
2503
2504 static void guc_release(struct intel_engine_cs *engine)
2505 {
2506         engine->sanitize = NULL; /* no longer in control, nothing to sanitize */
2507
2508         intel_engine_cleanup_common(engine);
2509         lrc_fini_wa_ctx(engine);
2510 }
2511
2512 static void virtual_guc_bump_serial(struct intel_engine_cs *engine)
2513 {
2514         struct intel_engine_cs *e;
2515         intel_engine_mask_t tmp, mask = engine->mask;
2516
2517         for_each_engine_masked(e, engine->gt, mask, tmp)
2518                 e->serial++;
2519 }
2520
2521 static void guc_default_vfuncs(struct intel_engine_cs *engine)
2522 {
2523         /* Default vfuncs which can be overridden by each engine. */
2524
2525         engine->resume = guc_resume;
2526
2527         engine->cops = &guc_context_ops;
2528         engine->request_alloc = guc_request_alloc;
2529         engine->add_active_request = add_to_context;
2530         engine->remove_active_request = remove_from_context;
2531
2532         engine->sched_engine->schedule = i915_schedule;
2533
2534         engine->reset.prepare = guc_reset_nop;
2535         engine->reset.rewind = guc_rewind_nop;
2536         engine->reset.cancel = guc_reset_nop;
2537         engine->reset.finish = guc_reset_nop;
2538
2539         engine->emit_flush = gen8_emit_flush_xcs;
2540         engine->emit_init_breadcrumb = gen8_emit_init_breadcrumb;
2541         engine->emit_fini_breadcrumb = gen8_emit_fini_breadcrumb_xcs;
2542         if (GRAPHICS_VER(engine->i915) >= 12) {
2543                 engine->emit_fini_breadcrumb = gen12_emit_fini_breadcrumb_xcs;
2544                 engine->emit_flush = gen12_emit_flush_xcs;
2545         }
2546         engine->set_default_submission = guc_set_default_submission;
2547
2548         engine->flags |= I915_ENGINE_HAS_PREEMPTION;
2549         engine->flags |= I915_ENGINE_HAS_TIMESLICES;
2550
2551         /*
2552          * TODO: GuC supports timeslicing and semaphores as well, but they're
2553          * handled by the firmware so some minor tweaks are required before
2554          * enabling.
2555          *
2556          * engine->flags |= I915_ENGINE_HAS_SEMAPHORES;
2557          */
2558
2559         engine->emit_bb_start = gen8_emit_bb_start;
2560 }
2561
2562 static void rcs_submission_override(struct intel_engine_cs *engine)
2563 {
2564         switch (GRAPHICS_VER(engine->i915)) {
2565         case 12:
2566                 engine->emit_flush = gen12_emit_flush_rcs;
2567                 engine->emit_fini_breadcrumb = gen12_emit_fini_breadcrumb_rcs;
2568                 break;
2569         case 11:
2570                 engine->emit_flush = gen11_emit_flush_rcs;
2571                 engine->emit_fini_breadcrumb = gen11_emit_fini_breadcrumb_rcs;
2572                 break;
2573         default:
2574                 engine->emit_flush = gen8_emit_flush_rcs;
2575                 engine->emit_fini_breadcrumb = gen8_emit_fini_breadcrumb_rcs;
2576                 break;
2577         }
2578 }
2579
2580 static inline void guc_default_irqs(struct intel_engine_cs *engine)
2581 {
2582         engine->irq_keep_mask = GT_RENDER_USER_INTERRUPT;
2583         intel_engine_set_irq_handler(engine, cs_irq_handler);
2584 }
2585
2586 static void guc_sched_engine_destroy(struct kref *kref)
2587 {
2588         struct i915_sched_engine *sched_engine =
2589                 container_of(kref, typeof(*sched_engine), ref);
2590         struct intel_guc *guc = sched_engine->private_data;
2591
2592         guc->sched_engine = NULL;
2593         tasklet_kill(&sched_engine->tasklet); /* flush the callback */
2594         kfree(sched_engine);
2595 }
2596
2597 int intel_guc_submission_setup(struct intel_engine_cs *engine)
2598 {
2599         struct drm_i915_private *i915 = engine->i915;
2600         struct intel_guc *guc = &engine->gt->uc.guc;
2601
2602         /*
2603          * The setup relies on several assumptions (e.g. irqs always enabled)
2604          * that are only valid on gen11+
2605          */
2606         GEM_BUG_ON(GRAPHICS_VER(i915) < 11);
2607
2608         if (!guc->sched_engine) {
2609                 guc->sched_engine = i915_sched_engine_create(ENGINE_VIRTUAL);
2610                 if (!guc->sched_engine)
2611                         return -ENOMEM;
2612
2613                 guc->sched_engine->schedule = i915_schedule;
2614                 guc->sched_engine->disabled = guc_sched_engine_disabled;
2615                 guc->sched_engine->private_data = guc;
2616                 guc->sched_engine->destroy = guc_sched_engine_destroy;
2617                 guc->sched_engine->bump_inflight_request_prio =
2618                         guc_bump_inflight_request_prio;
2619                 guc->sched_engine->retire_inflight_request_prio =
2620                         guc_retire_inflight_request_prio;
2621                 tasklet_setup(&guc->sched_engine->tasklet,
2622                               guc_submission_tasklet);
2623         }
2624         i915_sched_engine_put(engine->sched_engine);
2625         engine->sched_engine = i915_sched_engine_get(guc->sched_engine);
2626
2627         guc_default_vfuncs(engine);
2628         guc_default_irqs(engine);
2629         guc_init_breadcrumbs(engine);
2630
2631         if (engine->class == RENDER_CLASS)
2632                 rcs_submission_override(engine);
2633
2634         lrc_init_wa_ctx(engine);
2635
2636         /* Finally, take ownership and responsibility for cleanup! */
2637         engine->sanitize = guc_sanitize;
2638         engine->release = guc_release;
2639
2640         return 0;
2641 }
2642
2643 void intel_guc_submission_enable(struct intel_guc *guc)
2644 {
2645         guc_init_lrc_mapping(guc);
2646 }
2647
2648 void intel_guc_submission_disable(struct intel_guc *guc)
2649 {
2650         /* Note: By the time we're here, GuC may have already been reset */
2651 }
2652
2653 static bool __guc_submission_supported(struct intel_guc *guc)
2654 {
2655         /* GuC submission is unavailable for pre-Gen11 */
2656         return intel_guc_is_supported(guc) &&
2657                GRAPHICS_VER(guc_to_gt(guc)->i915) >= 11;
2658 }
2659
2660 static bool __guc_submission_selected(struct intel_guc *guc)
2661 {
2662         struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
2663
2664         if (!intel_guc_submission_is_supported(guc))
2665                 return false;
2666
2667         return i915->params.enable_guc & ENABLE_GUC_SUBMISSION;
2668 }
2669
2670 void intel_guc_submission_init_early(struct intel_guc *guc)
2671 {
2672         guc->submission_supported = __guc_submission_supported(guc);
2673         guc->submission_selected = __guc_submission_selected(guc);
2674 }
2675
2676 static inline struct intel_context *
2677 g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
2678 {
2679         struct intel_context *ce;
2680
2681         if (unlikely(desc_idx >= GUC_MAX_LRC_DESCRIPTORS)) {
2682                 drm_err(&guc_to_gt(guc)->i915->drm,
2683                         "Invalid desc_idx %u", desc_idx);
2684                 return NULL;
2685         }
2686
2687         ce = __get_context(guc, desc_idx);
2688         if (unlikely(!ce)) {
2689                 drm_err(&guc_to_gt(guc)->i915->drm,
2690                         "Context is NULL, desc_idx %u", desc_idx);
2691                 return NULL;
2692         }
2693
2694         return ce;
2695 }
2696
2697 int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
2698                                           const u32 *msg,
2699                                           u32 len)
2700 {
2701         struct intel_context *ce;
2702         u32 desc_idx = msg[0];
2703
2704         if (unlikely(len < 1)) {
2705                 drm_err(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);
2706                 return -EPROTO;
2707         }
2708
2709         ce = g2h_context_lookup(guc, desc_idx);
2710         if (unlikely(!ce))
2711                 return -EPROTO;
2712
2713         trace_intel_context_deregister_done(ce);
2714
2715 #ifdef CONFIG_DRM_I915_SELFTEST
2716         if (unlikely(ce->drop_deregister)) {
2717                 ce->drop_deregister = false;
2718                 return 0;
2719         }
2720 #endif
2721
2722         if (context_wait_for_deregister_to_register(ce)) {
2723                 struct intel_runtime_pm *runtime_pm =
2724                         &ce->engine->gt->i915->runtime_pm;
2725                 intel_wakeref_t wakeref;
2726
2727                 /*
2728                  * Previous owner of this guc_id has been deregistered, now safe
2729                  * register this context.
2730                  */
2731                 with_intel_runtime_pm(runtime_pm, wakeref)
2732                         register_context(ce, true);
2733                 guc_signal_context_fence(ce);
2734                 intel_context_put(ce);
2735         } else if (context_destroyed(ce)) {
2736                 /* Context has been destroyed */
2737                 release_guc_id(guc, ce);
2738                 __guc_context_destroy(ce);
2739         }
2740
2741         decr_outstanding_submission_g2h(guc);
2742
2743         return 0;
2744 }
2745
2746 int intel_guc_sched_done_process_msg(struct intel_guc *guc,
2747                                      const u32 *msg,
2748                                      u32 len)
2749 {
2750         struct intel_context *ce;
2751         unsigned long flags;
2752         u32 desc_idx = msg[0];
2753
2754         if (unlikely(len < 2)) {
2755                 drm_err(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);
2756                 return -EPROTO;
2757         }
2758
2759         ce = g2h_context_lookup(guc, desc_idx);
2760         if (unlikely(!ce))
2761                 return -EPROTO;
2762
2763         if (unlikely(context_destroyed(ce) ||
2764                      (!context_pending_enable(ce) &&
2765                      !context_pending_disable(ce)))) {
2766                 drm_err(&guc_to_gt(guc)->i915->drm,
2767                         "Bad context sched_state 0x%x, desc_idx %u",
2768                         ce->guc_state.sched_state, desc_idx);
2769                 return -EPROTO;
2770         }
2771
2772         trace_intel_context_sched_done(ce);
2773
2774         if (context_pending_enable(ce)) {
2775 #ifdef CONFIG_DRM_I915_SELFTEST
2776                 if (unlikely(ce->drop_schedule_enable)) {
2777                         ce->drop_schedule_enable = false;
2778                         return 0;
2779                 }
2780 #endif
2781
2782                 spin_lock_irqsave(&ce->guc_state.lock, flags);
2783                 clr_context_pending_enable(ce);
2784                 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2785         } else if (context_pending_disable(ce)) {
2786                 bool banned;
2787
2788 #ifdef CONFIG_DRM_I915_SELFTEST
2789                 if (unlikely(ce->drop_schedule_disable)) {
2790                         ce->drop_schedule_disable = false;
2791                         return 0;
2792                 }
2793 #endif
2794
2795                 /*
2796                  * Unpin must be done before __guc_signal_context_fence,
2797                  * otherwise a race exists between the requests getting
2798                  * submitted + retired before this unpin completes resulting in
2799                  * the pin_count going to zero and the context still being
2800                  * enabled.
2801                  */
2802                 intel_context_sched_disable_unpin(ce);
2803
2804                 spin_lock_irqsave(&ce->guc_state.lock, flags);
2805                 banned = context_banned(ce);
2806                 clr_context_banned(ce);
2807                 clr_context_pending_disable(ce);
2808                 __guc_signal_context_fence(ce);
2809                 guc_blocked_fence_complete(ce);
2810                 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2811
2812                 if (banned) {
2813                         guc_cancel_context_requests(ce);
2814                         intel_engine_signal_breadcrumbs(ce->engine);
2815                 }
2816         }
2817
2818         decr_outstanding_submission_g2h(guc);
2819         intel_context_put(ce);
2820
2821         return 0;
2822 }
2823
2824 static void capture_error_state(struct intel_guc *guc,
2825                                 struct intel_context *ce)
2826 {
2827         struct intel_gt *gt = guc_to_gt(guc);
2828         struct drm_i915_private *i915 = gt->i915;
2829         struct intel_engine_cs *engine = __context_to_physical_engine(ce);
2830         intel_wakeref_t wakeref;
2831
2832         intel_engine_set_hung_context(engine, ce);
2833         with_intel_runtime_pm(&i915->runtime_pm, wakeref)
2834                 i915_capture_error_state(gt, engine->mask);
2835         atomic_inc(&i915->gpu_error.reset_engine_count[engine->uabi_class]);
2836 }
2837
2838 static void guc_context_replay(struct intel_context *ce)
2839 {
2840         struct i915_sched_engine *sched_engine = ce->engine->sched_engine;
2841
2842         __guc_reset_context(ce, true);
2843         tasklet_hi_schedule(&sched_engine->tasklet);
2844 }
2845
2846 static void guc_handle_context_reset(struct intel_guc *guc,
2847                                      struct intel_context *ce)
2848 {
2849         trace_intel_context_reset(ce);
2850
2851         /*
2852          * XXX: Racey if request cancellation has occurred, see comment in
2853          * __guc_reset_context().
2854          */
2855         if (likely(!intel_context_is_banned(ce) &&
2856                    !context_blocked(ce))) {
2857                 capture_error_state(guc, ce);
2858                 guc_context_replay(ce);
2859         }
2860 }
2861
2862 int intel_guc_context_reset_process_msg(struct intel_guc *guc,
2863                                         const u32 *msg, u32 len)
2864 {
2865         struct intel_context *ce;
2866         int desc_idx;
2867
2868         if (unlikely(len != 1)) {
2869                 drm_err(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);
2870                 return -EPROTO;
2871         }
2872
2873         desc_idx = msg[0];
2874         ce = g2h_context_lookup(guc, desc_idx);
2875         if (unlikely(!ce))
2876                 return -EPROTO;
2877
2878         guc_handle_context_reset(guc, ce);
2879
2880         return 0;
2881 }
2882
2883 static struct intel_engine_cs *
2884 guc_lookup_engine(struct intel_guc *guc, u8 guc_class, u8 instance)
2885 {
2886         struct intel_gt *gt = guc_to_gt(guc);
2887         u8 engine_class = guc_class_to_engine_class(guc_class);
2888
2889         /* Class index is checked in class converter */
2890         GEM_BUG_ON(instance > MAX_ENGINE_INSTANCE);
2891
2892         return gt->engine_class[engine_class][instance];
2893 }
2894
2895 int intel_guc_engine_failure_process_msg(struct intel_guc *guc,
2896                                          const u32 *msg, u32 len)
2897 {
2898         struct intel_engine_cs *engine;
2899         u8 guc_class, instance;
2900         u32 reason;
2901
2902         if (unlikely(len != 3)) {
2903                 drm_err(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);
2904                 return -EPROTO;
2905         }
2906
2907         guc_class = msg[0];
2908         instance = msg[1];
2909         reason = msg[2];
2910
2911         engine = guc_lookup_engine(guc, guc_class, instance);
2912         if (unlikely(!engine)) {
2913                 drm_err(&guc_to_gt(guc)->i915->drm,
2914                         "Invalid engine %d:%d", guc_class, instance);
2915                 return -EPROTO;
2916         }
2917
2918         intel_gt_handle_error(guc_to_gt(guc), engine->mask,
2919                               I915_ERROR_CAPTURE,
2920                               "GuC failed to reset %s (reason=0x%08x)\n",
2921                               engine->name, reason);
2922
2923         return 0;
2924 }
2925
2926 void intel_guc_find_hung_context(struct intel_engine_cs *engine)
2927 {
2928         struct intel_guc *guc = &engine->gt->uc.guc;
2929         struct intel_context *ce;
2930         struct i915_request *rq;
2931         unsigned long index;
2932         unsigned long flags;
2933
2934         /* Reset called during driver load? GuC not yet initialised! */
2935         if (unlikely(!guc_submission_initialized(guc)))
2936                 return;
2937
2938         xa_lock_irqsave(&guc->context_lookup, flags);
2939         xa_for_each(&guc->context_lookup, index, ce) {
2940                 if (!kref_get_unless_zero(&ce->ref))
2941                         continue;
2942
2943                 xa_unlock(&guc->context_lookup);
2944
2945                 if (!intel_context_is_pinned(ce))
2946                         goto next;
2947
2948                 if (intel_engine_is_virtual(ce->engine)) {
2949                         if (!(ce->engine->mask & engine->mask))
2950                                 goto next;
2951                 } else {
2952                         if (ce->engine != engine)
2953                                 goto next;
2954                 }
2955
2956                 list_for_each_entry(rq, &ce->guc_state.requests, sched.link) {
2957                         if (i915_test_request_state(rq) != I915_REQUEST_ACTIVE)
2958                                 continue;
2959
2960                         intel_engine_set_hung_context(engine, ce);
2961
2962                         /* Can only cope with one hang at a time... */
2963                         intel_context_put(ce);
2964                         xa_lock(&guc->context_lookup);
2965                         goto done;
2966                 }
2967 next:
2968                 intel_context_put(ce);
2969                 xa_lock(&guc->context_lookup);
2970         }
2971 done:
2972         xa_unlock_irqrestore(&guc->context_lookup, flags);
2973 }
2974
2975 void intel_guc_dump_active_requests(struct intel_engine_cs *engine,
2976                                     struct i915_request *hung_rq,
2977                                     struct drm_printer *m)
2978 {
2979         struct intel_guc *guc = &engine->gt->uc.guc;
2980         struct intel_context *ce;
2981         unsigned long index;
2982         unsigned long flags;
2983
2984         /* Reset called during driver load? GuC not yet initialised! */
2985         if (unlikely(!guc_submission_initialized(guc)))
2986                 return;
2987
2988         xa_lock_irqsave(&guc->context_lookup, flags);
2989         xa_for_each(&guc->context_lookup, index, ce) {
2990                 if (!kref_get_unless_zero(&ce->ref))
2991                         continue;
2992
2993                 xa_unlock(&guc->context_lookup);
2994
2995                 if (!intel_context_is_pinned(ce))
2996                         goto next;
2997
2998                 if (intel_engine_is_virtual(ce->engine)) {
2999                         if (!(ce->engine->mask & engine->mask))
3000                                 goto next;
3001                 } else {
3002                         if (ce->engine != engine)
3003                                 goto next;
3004                 }
3005
3006                 spin_lock(&ce->guc_state.lock);
3007                 intel_engine_dump_active_requests(&ce->guc_state.requests,
3008                                                   hung_rq, m);
3009                 spin_unlock(&ce->guc_state.lock);
3010
3011 next:
3012                 intel_context_put(ce);
3013                 xa_lock(&guc->context_lookup);
3014         }
3015         xa_unlock_irqrestore(&guc->context_lookup, flags);
3016 }
3017
3018 void intel_guc_submission_print_info(struct intel_guc *guc,
3019                                      struct drm_printer *p)
3020 {
3021         struct i915_sched_engine *sched_engine = guc->sched_engine;
3022         struct rb_node *rb;
3023         unsigned long flags;
3024
3025         if (!sched_engine)
3026                 return;
3027
3028         drm_printf(p, "GuC Number Outstanding Submission G2H: %u\n",
3029                    atomic_read(&guc->outstanding_submission_g2h));
3030         drm_printf(p, "GuC tasklet count: %u\n\n",
3031                    atomic_read(&sched_engine->tasklet.count));
3032
3033         spin_lock_irqsave(&sched_engine->lock, flags);
3034         drm_printf(p, "Requests in GuC submit tasklet:\n");
3035         for (rb = rb_first_cached(&sched_engine->queue); rb; rb = rb_next(rb)) {
3036                 struct i915_priolist *pl = to_priolist(rb);
3037                 struct i915_request *rq;
3038
3039                 priolist_for_each_request(rq, pl)
3040                         drm_printf(p, "guc_id=%u, seqno=%llu\n",
3041                                    rq->context->guc_id.id,
3042                                    rq->fence.seqno);
3043         }
3044         spin_unlock_irqrestore(&sched_engine->lock, flags);
3045         drm_printf(p, "\n");
3046 }
3047
3048 static inline void guc_log_context_priority(struct drm_printer *p,
3049                                             struct intel_context *ce)
3050 {
3051         int i;
3052
3053         drm_printf(p, "\t\tPriority: %d\n", ce->guc_state.prio);
3054         drm_printf(p, "\t\tNumber Requests (lower index == higher priority)\n");
3055         for (i = GUC_CLIENT_PRIORITY_KMD_HIGH;
3056              i < GUC_CLIENT_PRIORITY_NUM; ++i) {
3057                 drm_printf(p, "\t\tNumber requests in priority band[%d]: %d\n",
3058                            i, ce->guc_state.prio_count[i]);
3059         }
3060         drm_printf(p, "\n");
3061 }
3062
3063 void intel_guc_submission_print_context_info(struct intel_guc *guc,
3064                                              struct drm_printer *p)
3065 {
3066         struct intel_context *ce;
3067         unsigned long index;
3068         unsigned long flags;
3069
3070         xa_lock_irqsave(&guc->context_lookup, flags);
3071         xa_for_each(&guc->context_lookup, index, ce) {
3072                 drm_printf(p, "GuC lrc descriptor %u:\n", ce->guc_id.id);
3073                 drm_printf(p, "\tHW Context Desc: 0x%08x\n", ce->lrc.lrca);
3074                 drm_printf(p, "\t\tLRC Head: Internal %u, Memory %u\n",
3075                            ce->ring->head,
3076                            ce->lrc_reg_state[CTX_RING_HEAD]);
3077                 drm_printf(p, "\t\tLRC Tail: Internal %u, Memory %u\n",
3078                            ce->ring->tail,
3079                            ce->lrc_reg_state[CTX_RING_TAIL]);
3080                 drm_printf(p, "\t\tContext Pin Count: %u\n",
3081                            atomic_read(&ce->pin_count));
3082                 drm_printf(p, "\t\tGuC ID Ref Count: %u\n",
3083                            atomic_read(&ce->guc_id.ref));
3084                 drm_printf(p, "\t\tSchedule State: 0x%x\n\n",
3085                            ce->guc_state.sched_state);
3086
3087                 guc_log_context_priority(p, ce);
3088         }
3089         xa_unlock_irqrestore(&guc->context_lookup, flags);
3090 }
3091
3092 static struct intel_context *
3093 guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
3094 {
3095         struct guc_virtual_engine *ve;
3096         struct intel_guc *guc;
3097         unsigned int n;
3098         int err;
3099
3100         ve = kzalloc(sizeof(*ve), GFP_KERNEL);
3101         if (!ve)
3102                 return ERR_PTR(-ENOMEM);
3103
3104         guc = &siblings[0]->gt->uc.guc;
3105
3106         ve->base.i915 = siblings[0]->i915;
3107         ve->base.gt = siblings[0]->gt;
3108         ve->base.uncore = siblings[0]->uncore;
3109         ve->base.id = -1;
3110
3111         ve->base.uabi_class = I915_ENGINE_CLASS_INVALID;
3112         ve->base.instance = I915_ENGINE_CLASS_INVALID_VIRTUAL;
3113         ve->base.uabi_instance = I915_ENGINE_CLASS_INVALID_VIRTUAL;
3114         ve->base.saturated = ALL_ENGINES;
3115
3116         snprintf(ve->base.name, sizeof(ve->base.name), "virtual");
3117
3118         ve->base.sched_engine = i915_sched_engine_get(guc->sched_engine);
3119
3120         ve->base.cops = &virtual_guc_context_ops;
3121         ve->base.request_alloc = guc_request_alloc;
3122         ve->base.bump_serial = virtual_guc_bump_serial;
3123
3124         ve->base.submit_request = guc_submit_request;
3125
3126         ve->base.flags = I915_ENGINE_IS_VIRTUAL;
3127
3128         intel_context_init(&ve->context, &ve->base);
3129
3130         for (n = 0; n < count; n++) {
3131                 struct intel_engine_cs *sibling = siblings[n];
3132
3133                 GEM_BUG_ON(!is_power_of_2(sibling->mask));
3134                 if (sibling->mask & ve->base.mask) {
3135                         DRM_DEBUG("duplicate %s entry in load balancer\n",
3136                                   sibling->name);
3137                         err = -EINVAL;
3138                         goto err_put;
3139                 }
3140
3141                 ve->base.mask |= sibling->mask;
3142
3143                 if (n != 0 && ve->base.class != sibling->class) {
3144                         DRM_DEBUG("invalid mixing of engine class, sibling %d, already %d\n",
3145                                   sibling->class, ve->base.class);
3146                         err = -EINVAL;
3147                         goto err_put;
3148                 } else if (n == 0) {
3149                         ve->base.class = sibling->class;
3150                         ve->base.uabi_class = sibling->uabi_class;
3151                         snprintf(ve->base.name, sizeof(ve->base.name),
3152                                  "v%dx%d", ve->base.class, count);
3153                         ve->base.context_size = sibling->context_size;
3154
3155                         ve->base.add_active_request =
3156                                 sibling->add_active_request;
3157                         ve->base.remove_active_request =
3158                                 sibling->remove_active_request;
3159                         ve->base.emit_bb_start = sibling->emit_bb_start;
3160                         ve->base.emit_flush = sibling->emit_flush;
3161                         ve->base.emit_init_breadcrumb =
3162                                 sibling->emit_init_breadcrumb;
3163                         ve->base.emit_fini_breadcrumb =
3164                                 sibling->emit_fini_breadcrumb;
3165                         ve->base.emit_fini_breadcrumb_dw =
3166                                 sibling->emit_fini_breadcrumb_dw;
3167                         ve->base.breadcrumbs =
3168                                 intel_breadcrumbs_get(sibling->breadcrumbs);
3169
3170                         ve->base.flags |= sibling->flags;
3171
3172                         ve->base.props.timeslice_duration_ms =
3173                                 sibling->props.timeslice_duration_ms;
3174                         ve->base.props.preempt_timeout_ms =
3175                                 sibling->props.preempt_timeout_ms;
3176                 }
3177         }
3178
3179         return &ve->context;
3180
3181 err_put:
3182         intel_context_put(&ve->context);
3183         return ERR_PTR(err);
3184 }
3185
3186 bool intel_guc_virtual_engine_has_heartbeat(const struct intel_engine_cs *ve)
3187 {
3188         struct intel_engine_cs *engine;
3189         intel_engine_mask_t tmp, mask = ve->mask;
3190
3191         for_each_engine_masked(engine, ve->gt, mask, tmp)
3192                 if (READ_ONCE(engine->props.heartbeat_interval_ms))
3193                         return true;
3194
3195         return false;
3196 }
3197
3198 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
3199 #include "selftest_guc.c"
3200 #endif