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_heartbeat.h"
13 #include "gt/intel_engine_pm.h"
14 #include "gt/intel_engine_regs.h"
15 #include "gt/intel_gpu_commands.h"
16 #include "gt/intel_gt.h"
17 #include "gt/intel_gt_clock_utils.h"
18 #include "gt/intel_gt_irq.h"
19 #include "gt/intel_gt_pm.h"
20 #include "gt/intel_gt_regs.h"
21 #include "gt/intel_gt_requests.h"
22 #include "gt/intel_lrc.h"
23 #include "gt/intel_lrc_reg.h"
24 #include "gt/intel_mocs.h"
25 #include "gt/intel_ring.h"
26 
27 #include "intel_guc_ads.h"
28 #include "intel_guc_submission.h"
29 
30 #include "i915_drv.h"
31 #include "i915_trace.h"
32 
33 /**
34  * DOC: GuC-based command submission
35  *
36  * The Scratch registers:
37  * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes
38  * a value to the action register (SOFT_SCRATCH_0) along with any data. It then
39  * triggers an interrupt on the GuC via another register write (0xC4C8).
40  * Firmware writes a success/fail code back to the action register after
41  * processes the request. The kernel driver polls waiting for this update and
42  * then proceeds.
43  *
44  * Command Transport buffers (CTBs):
45  * Covered in detail in other sections but CTBs (Host to GuC - H2G, GuC to Host
46  * - G2H) are a message interface between the i915 and GuC.
47  *
48  * Context registration:
49  * Before a context can be submitted it must be registered with the GuC via a
50  * H2G. A unique guc_id is associated with each context. The context is either
51  * registered at request creation time (normal operation) or at submission time
52  * (abnormal operation, e.g. after a reset).
53  *
54  * Context submission:
55  * The i915 updates the LRC tail value in memory. The i915 must enable the
56  * scheduling of the context within the GuC for the GuC to actually consider it.
57  * Therefore, the first time a disabled context is submitted we use a schedule
58  * enable H2G, while follow up submissions are done via the context submit H2G,
59  * which informs the GuC that a previously enabled context has new work
60  * available.
61  *
62  * Context unpin:
63  * To unpin a context a H2G is used to disable scheduling. When the
64  * corresponding G2H returns indicating the scheduling disable operation has
65  * completed it is safe to unpin the context. While a disable is in flight it
66  * isn't safe to resubmit the context so a fence is used to stall all future
67  * requests of that context until the G2H is returned.
68  *
69  * Context deregistration:
70  * Before a context can be destroyed or if we steal its guc_id we must
71  * deregister the context with the GuC via H2G. If stealing the guc_id it isn't
72  * safe to submit anything to this guc_id until the deregister completes so a
73  * fence is used to stall all requests associated with this guc_id until the
74  * corresponding G2H returns indicating the guc_id has been deregistered.
75  *
76  * submission_state.guc_ids:
77  * Unique number associated with private GuC context data passed in during
78  * context registration / submission / deregistration. 64k available. Simple ida
79  * is used for allocation.
80  *
81  * Stealing guc_ids:
82  * If no guc_ids are available they can be stolen from another context at
83  * request creation time if that context is unpinned. If a guc_id can't be found
84  * we punt this problem to the user as we believe this is near impossible to hit
85  * during normal use cases.
86  *
87  * Locking:
88  * In the GuC submission code we have 3 basic spin locks which protect
89  * everything. Details about each below.
90  *
91  * sched_engine->lock
92  * This is the submission lock for all contexts that share an i915 schedule
93  * engine (sched_engine), thus only one of the contexts which share a
94  * sched_engine can be submitting at a time. Currently only one sched_engine is
95  * used for all of GuC submission but that could change in the future.
96  *
97  * guc->submission_state.lock
98  * Global lock for GuC submission state. Protects guc_ids and destroyed contexts
99  * list.
100  *
101  * ce->guc_state.lock
102  * Protects everything under ce->guc_state. Ensures that a context is in the
103  * correct state before issuing a H2G. e.g. We don't issue a schedule disable
104  * on a disabled context (bad idea), we don't issue a schedule enable when a
105  * schedule disable is in flight, etc... Also protects list of inflight requests
106  * on the context and the priority management state. Lock is individual to each
107  * context.
108  *
109  * Lock ordering rules:
110  * sched_engine->lock -> ce->guc_state.lock
111  * guc->submission_state.lock -> ce->guc_state.lock
112  *
113  * Reset races:
114  * When a full GT reset is triggered it is assumed that some G2H responses to
115  * H2Gs can be lost as the GuC is also reset. Losing these G2H can prove to be
116  * fatal as we do certain operations upon receiving a G2H (e.g. destroy
117  * contexts, release guc_ids, etc...). When this occurs we can scrub the
118  * context state and cleanup appropriately, however this is quite racey.
119  * To avoid races, the reset code must disable submission before scrubbing for
120  * the missing G2H, while the submission code must check for submission being
121  * disabled and skip sending H2Gs and updating context states when it is. Both
122  * sides must also make sure to hold the relevant locks.
123  */
124 
125 /* GuC Virtual Engine */
126 struct guc_virtual_engine {
127 	struct intel_engine_cs base;
128 	struct intel_context context;
129 };
130 
131 static struct intel_context *
132 guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count,
133 		   unsigned long flags);
134 
135 static struct intel_context *
136 guc_create_parallel(struct intel_engine_cs **engines,
137 		    unsigned int num_siblings,
138 		    unsigned int width);
139 
140 #define GUC_REQUEST_SIZE 64 /* bytes */
141 
142 /*
143  * We reserve 1/16 of the guc_ids for multi-lrc as these need to be contiguous
144  * per the GuC submission interface. A different allocation algorithm is used
145  * (bitmap vs. ida) between multi-lrc and single-lrc hence the reason to
146  * partition the guc_id space. We believe the number of multi-lrc contexts in
147  * use should be low and 1/16 should be sufficient. Minimum of 32 guc_ids for
148  * multi-lrc.
149  */
150 #define NUMBER_MULTI_LRC_GUC_ID(guc)	\
151 	((guc)->submission_state.num_guc_ids / 16)
152 
153 /*
154  * Below is a set of functions which control the GuC scheduling state which
155  * require a lock.
156  */
157 #define SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER	BIT(0)
158 #define SCHED_STATE_DESTROYED				BIT(1)
159 #define SCHED_STATE_PENDING_DISABLE			BIT(2)
160 #define SCHED_STATE_BANNED				BIT(3)
161 #define SCHED_STATE_ENABLED				BIT(4)
162 #define SCHED_STATE_PENDING_ENABLE			BIT(5)
163 #define SCHED_STATE_REGISTERED				BIT(6)
164 #define SCHED_STATE_BLOCKED_SHIFT			7
165 #define SCHED_STATE_BLOCKED		BIT(SCHED_STATE_BLOCKED_SHIFT)
166 #define SCHED_STATE_BLOCKED_MASK	(0xfff << SCHED_STATE_BLOCKED_SHIFT)
167 
168 static inline void init_sched_state(struct intel_context *ce)
169 {
170 	lockdep_assert_held(&ce->guc_state.lock);
171 	ce->guc_state.sched_state &= SCHED_STATE_BLOCKED_MASK;
172 }
173 
174 __maybe_unused
175 static bool sched_state_is_init(struct intel_context *ce)
176 {
177 	/*
178 	 * XXX: Kernel contexts can have SCHED_STATE_NO_LOCK_REGISTERED after
179 	 * suspend.
180 	 */
181 	return !(ce->guc_state.sched_state &=
182 		 ~(SCHED_STATE_BLOCKED_MASK | SCHED_STATE_REGISTERED));
183 }
184 
185 static inline bool
186 context_wait_for_deregister_to_register(struct intel_context *ce)
187 {
188 	return ce->guc_state.sched_state &
189 		SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
190 }
191 
192 static inline void
193 set_context_wait_for_deregister_to_register(struct intel_context *ce)
194 {
195 	lockdep_assert_held(&ce->guc_state.lock);
196 	ce->guc_state.sched_state |=
197 		SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
198 }
199 
200 static inline void
201 clr_context_wait_for_deregister_to_register(struct intel_context *ce)
202 {
203 	lockdep_assert_held(&ce->guc_state.lock);
204 	ce->guc_state.sched_state &=
205 		~SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
206 }
207 
208 static inline bool
209 context_destroyed(struct intel_context *ce)
210 {
211 	return ce->guc_state.sched_state & SCHED_STATE_DESTROYED;
212 }
213 
214 static inline void
215 set_context_destroyed(struct intel_context *ce)
216 {
217 	lockdep_assert_held(&ce->guc_state.lock);
218 	ce->guc_state.sched_state |= SCHED_STATE_DESTROYED;
219 }
220 
221 static inline bool context_pending_disable(struct intel_context *ce)
222 {
223 	return ce->guc_state.sched_state & SCHED_STATE_PENDING_DISABLE;
224 }
225 
226 static inline void set_context_pending_disable(struct intel_context *ce)
227 {
228 	lockdep_assert_held(&ce->guc_state.lock);
229 	ce->guc_state.sched_state |= SCHED_STATE_PENDING_DISABLE;
230 }
231 
232 static inline void clr_context_pending_disable(struct intel_context *ce)
233 {
234 	lockdep_assert_held(&ce->guc_state.lock);
235 	ce->guc_state.sched_state &= ~SCHED_STATE_PENDING_DISABLE;
236 }
237 
238 static inline bool context_banned(struct intel_context *ce)
239 {
240 	return ce->guc_state.sched_state & SCHED_STATE_BANNED;
241 }
242 
243 static inline void set_context_banned(struct intel_context *ce)
244 {
245 	lockdep_assert_held(&ce->guc_state.lock);
246 	ce->guc_state.sched_state |= SCHED_STATE_BANNED;
247 }
248 
249 static inline void clr_context_banned(struct intel_context *ce)
250 {
251 	lockdep_assert_held(&ce->guc_state.lock);
252 	ce->guc_state.sched_state &= ~SCHED_STATE_BANNED;
253 }
254 
255 static inline bool context_enabled(struct intel_context *ce)
256 {
257 	return ce->guc_state.sched_state & SCHED_STATE_ENABLED;
258 }
259 
260 static inline void set_context_enabled(struct intel_context *ce)
261 {
262 	lockdep_assert_held(&ce->guc_state.lock);
263 	ce->guc_state.sched_state |= SCHED_STATE_ENABLED;
264 }
265 
266 static inline void clr_context_enabled(struct intel_context *ce)
267 {
268 	lockdep_assert_held(&ce->guc_state.lock);
269 	ce->guc_state.sched_state &= ~SCHED_STATE_ENABLED;
270 }
271 
272 static inline bool context_pending_enable(struct intel_context *ce)
273 {
274 	return ce->guc_state.sched_state & SCHED_STATE_PENDING_ENABLE;
275 }
276 
277 static inline void set_context_pending_enable(struct intel_context *ce)
278 {
279 	lockdep_assert_held(&ce->guc_state.lock);
280 	ce->guc_state.sched_state |= SCHED_STATE_PENDING_ENABLE;
281 }
282 
283 static inline void clr_context_pending_enable(struct intel_context *ce)
284 {
285 	lockdep_assert_held(&ce->guc_state.lock);
286 	ce->guc_state.sched_state &= ~SCHED_STATE_PENDING_ENABLE;
287 }
288 
289 static inline bool context_registered(struct intel_context *ce)
290 {
291 	return ce->guc_state.sched_state & SCHED_STATE_REGISTERED;
292 }
293 
294 static inline void set_context_registered(struct intel_context *ce)
295 {
296 	lockdep_assert_held(&ce->guc_state.lock);
297 	ce->guc_state.sched_state |= SCHED_STATE_REGISTERED;
298 }
299 
300 static inline void clr_context_registered(struct intel_context *ce)
301 {
302 	lockdep_assert_held(&ce->guc_state.lock);
303 	ce->guc_state.sched_state &= ~SCHED_STATE_REGISTERED;
304 }
305 
306 static inline u32 context_blocked(struct intel_context *ce)
307 {
308 	return (ce->guc_state.sched_state & SCHED_STATE_BLOCKED_MASK) >>
309 		SCHED_STATE_BLOCKED_SHIFT;
310 }
311 
312 static inline void incr_context_blocked(struct intel_context *ce)
313 {
314 	lockdep_assert_held(&ce->guc_state.lock);
315 
316 	ce->guc_state.sched_state += SCHED_STATE_BLOCKED;
317 
318 	GEM_BUG_ON(!context_blocked(ce));	/* Overflow check */
319 }
320 
321 static inline void decr_context_blocked(struct intel_context *ce)
322 {
323 	lockdep_assert_held(&ce->guc_state.lock);
324 
325 	GEM_BUG_ON(!context_blocked(ce));	/* Underflow check */
326 
327 	ce->guc_state.sched_state -= SCHED_STATE_BLOCKED;
328 }
329 
330 static inline bool context_has_committed_requests(struct intel_context *ce)
331 {
332 	return !!ce->guc_state.number_committed_requests;
333 }
334 
335 static inline void incr_context_committed_requests(struct intel_context *ce)
336 {
337 	lockdep_assert_held(&ce->guc_state.lock);
338 	++ce->guc_state.number_committed_requests;
339 	GEM_BUG_ON(ce->guc_state.number_committed_requests < 0);
340 }
341 
342 static inline void decr_context_committed_requests(struct intel_context *ce)
343 {
344 	lockdep_assert_held(&ce->guc_state.lock);
345 	--ce->guc_state.number_committed_requests;
346 	GEM_BUG_ON(ce->guc_state.number_committed_requests < 0);
347 }
348 
349 static struct intel_context *
350 request_to_scheduling_context(struct i915_request *rq)
351 {
352 	return intel_context_to_parent(rq->context);
353 }
354 
355 static inline bool context_guc_id_invalid(struct intel_context *ce)
356 {
357 	return ce->guc_id.id == GUC_INVALID_LRC_ID;
358 }
359 
360 static inline void set_context_guc_id_invalid(struct intel_context *ce)
361 {
362 	ce->guc_id.id = GUC_INVALID_LRC_ID;
363 }
364 
365 static inline struct intel_guc *ce_to_guc(struct intel_context *ce)
366 {
367 	return &ce->engine->gt->uc.guc;
368 }
369 
370 static inline struct i915_priolist *to_priolist(struct rb_node *rb)
371 {
372 	return rb_entry(rb, struct i915_priolist, node);
373 }
374 
375 /*
376  * When using multi-lrc submission a scratch memory area is reserved in the
377  * parent's context state for the process descriptor, work queue, and handshake
378  * between the parent + children contexts to insert safe preemption points
379  * between each of the BBs. Currently the scratch area is sized to a page.
380  *
381  * The layout of this scratch area is below:
382  * 0						guc_process_desc
383  * + sizeof(struct guc_process_desc)		child go
384  * + CACHELINE_BYTES				child join[0]
385  * ...
386  * + CACHELINE_BYTES				child join[n - 1]
387  * ...						unused
388  * PARENT_SCRATCH_SIZE / 2			work queue start
389  * ...						work queue
390  * PARENT_SCRATCH_SIZE - 1			work queue end
391  */
392 #define WQ_SIZE			(PARENT_SCRATCH_SIZE / 2)
393 #define WQ_OFFSET		(PARENT_SCRATCH_SIZE - WQ_SIZE)
394 
395 struct sync_semaphore {
396 	u32 semaphore;
397 	u8 unused[CACHELINE_BYTES - sizeof(u32)];
398 };
399 
400 struct parent_scratch {
401 	struct guc_process_desc pdesc;
402 
403 	struct sync_semaphore go;
404 	struct sync_semaphore join[MAX_ENGINE_INSTANCE + 1];
405 
406 	u8 unused[WQ_OFFSET - sizeof(struct guc_process_desc) -
407 		sizeof(struct sync_semaphore) * (MAX_ENGINE_INSTANCE + 2)];
408 
409 	u32 wq[WQ_SIZE / sizeof(u32)];
410 };
411 
412 static u32 __get_parent_scratch_offset(struct intel_context *ce)
413 {
414 	GEM_BUG_ON(!ce->parallel.guc.parent_page);
415 
416 	return ce->parallel.guc.parent_page * PAGE_SIZE;
417 }
418 
419 static u32 __get_wq_offset(struct intel_context *ce)
420 {
421 	BUILD_BUG_ON(offsetof(struct parent_scratch, wq) != WQ_OFFSET);
422 
423 	return __get_parent_scratch_offset(ce) + WQ_OFFSET;
424 }
425 
426 static struct parent_scratch *
427 __get_parent_scratch(struct intel_context *ce)
428 {
429 	BUILD_BUG_ON(sizeof(struct parent_scratch) != PARENT_SCRATCH_SIZE);
430 	BUILD_BUG_ON(sizeof(struct sync_semaphore) != CACHELINE_BYTES);
431 
432 	/*
433 	 * Need to subtract LRC_STATE_OFFSET here as the
434 	 * parallel.guc.parent_page is the offset into ce->state while
435 	 * ce->lrc_reg_reg is ce->state + LRC_STATE_OFFSET.
436 	 */
437 	return (struct parent_scratch *)
438 		(ce->lrc_reg_state +
439 		 ((__get_parent_scratch_offset(ce) -
440 		   LRC_STATE_OFFSET) / sizeof(u32)));
441 }
442 
443 static struct guc_process_desc *
444 __get_process_desc(struct intel_context *ce)
445 {
446 	struct parent_scratch *ps = __get_parent_scratch(ce);
447 
448 	return &ps->pdesc;
449 }
450 
451 static u32 *get_wq_pointer(struct guc_process_desc *desc,
452 			   struct intel_context *ce,
453 			   u32 wqi_size)
454 {
455 	/*
456 	 * Check for space in work queue. Caching a value of head pointer in
457 	 * intel_context structure in order reduce the number accesses to shared
458 	 * GPU memory which may be across a PCIe bus.
459 	 */
460 #define AVAILABLE_SPACE	\
461 	CIRC_SPACE(ce->parallel.guc.wqi_tail, ce->parallel.guc.wqi_head, WQ_SIZE)
462 	if (wqi_size > AVAILABLE_SPACE) {
463 		ce->parallel.guc.wqi_head = READ_ONCE(desc->head);
464 
465 		if (wqi_size > AVAILABLE_SPACE)
466 			return NULL;
467 	}
468 #undef AVAILABLE_SPACE
469 
470 	return &__get_parent_scratch(ce)->wq[ce->parallel.guc.wqi_tail / sizeof(u32)];
471 }
472 
473 static struct guc_lrc_desc *__get_lrc_desc(struct intel_guc *guc, u32 index)
474 {
475 	struct guc_lrc_desc *base = guc->lrc_desc_pool_vaddr;
476 
477 	GEM_BUG_ON(index >= GUC_MAX_LRC_DESCRIPTORS);
478 
479 	return &base[index];
480 }
481 
482 static inline struct intel_context *__get_context(struct intel_guc *guc, u32 id)
483 {
484 	struct intel_context *ce = xa_load(&guc->context_lookup, id);
485 
486 	GEM_BUG_ON(id >= GUC_MAX_LRC_DESCRIPTORS);
487 
488 	return ce;
489 }
490 
491 static int guc_lrc_desc_pool_create(struct intel_guc *guc)
492 {
493 	u32 size;
494 	int ret;
495 
496 	size = PAGE_ALIGN(sizeof(struct guc_lrc_desc) *
497 			  GUC_MAX_LRC_DESCRIPTORS);
498 	ret = intel_guc_allocate_and_map_vma(guc, size, &guc->lrc_desc_pool,
499 					     (void **)&guc->lrc_desc_pool_vaddr);
500 	if (ret)
501 		return ret;
502 
503 	return 0;
504 }
505 
506 static void guc_lrc_desc_pool_destroy(struct intel_guc *guc)
507 {
508 	guc->lrc_desc_pool_vaddr = NULL;
509 	i915_vma_unpin_and_release(&guc->lrc_desc_pool, I915_VMA_RELEASE_MAP);
510 }
511 
512 static inline bool guc_submission_initialized(struct intel_guc *guc)
513 {
514 	return !!guc->lrc_desc_pool_vaddr;
515 }
516 
517 static inline void reset_lrc_desc(struct intel_guc *guc, u32 id)
518 {
519 	if (likely(guc_submission_initialized(guc))) {
520 		struct guc_lrc_desc *desc = __get_lrc_desc(guc, id);
521 		unsigned long flags;
522 
523 		memset(desc, 0, sizeof(*desc));
524 
525 		/*
526 		 * xarray API doesn't have xa_erase_irqsave wrapper, so calling
527 		 * the lower level functions directly.
528 		 */
529 		xa_lock_irqsave(&guc->context_lookup, flags);
530 		__xa_erase(&guc->context_lookup, id);
531 		xa_unlock_irqrestore(&guc->context_lookup, flags);
532 	}
533 }
534 
535 static inline bool lrc_desc_registered(struct intel_guc *guc, u32 id)
536 {
537 	return __get_context(guc, id);
538 }
539 
540 static inline void set_lrc_desc_registered(struct intel_guc *guc, u32 id,
541 					   struct intel_context *ce)
542 {
543 	unsigned long flags;
544 
545 	/*
546 	 * xarray API doesn't have xa_save_irqsave wrapper, so calling the
547 	 * lower level functions directly.
548 	 */
549 	xa_lock_irqsave(&guc->context_lookup, flags);
550 	__xa_store(&guc->context_lookup, id, ce, GFP_ATOMIC);
551 	xa_unlock_irqrestore(&guc->context_lookup, flags);
552 }
553 
554 static void decr_outstanding_submission_g2h(struct intel_guc *guc)
555 {
556 	if (atomic_dec_and_test(&guc->outstanding_submission_g2h))
557 		wake_up_all(&guc->ct.wq);
558 }
559 
560 static int guc_submission_send_busy_loop(struct intel_guc *guc,
561 					 const u32 *action,
562 					 u32 len,
563 					 u32 g2h_len_dw,
564 					 bool loop)
565 {
566 	/*
567 	 * We always loop when a send requires a reply (i.e. g2h_len_dw > 0),
568 	 * so we don't handle the case where we don't get a reply because we
569 	 * aborted the send due to the channel being busy.
570 	 */
571 	GEM_BUG_ON(g2h_len_dw && !loop);
572 
573 	if (g2h_len_dw)
574 		atomic_inc(&guc->outstanding_submission_g2h);
575 
576 	return intel_guc_send_busy_loop(guc, action, len, g2h_len_dw, loop);
577 }
578 
579 int intel_guc_wait_for_pending_msg(struct intel_guc *guc,
580 				   atomic_t *wait_var,
581 				   bool interruptible,
582 				   long timeout)
583 {
584 	const int state = interruptible ?
585 		TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
586 	DEFINE_WAIT(wait);
587 
588 	might_sleep();
589 	GEM_BUG_ON(timeout < 0);
590 
591 	if (!atomic_read(wait_var))
592 		return 0;
593 
594 	if (!timeout)
595 		return -ETIME;
596 
597 	for (;;) {
598 		prepare_to_wait(&guc->ct.wq, &wait, state);
599 
600 		if (!atomic_read(wait_var))
601 			break;
602 
603 		if (signal_pending_state(state, current)) {
604 			timeout = -EINTR;
605 			break;
606 		}
607 
608 		if (!timeout) {
609 			timeout = -ETIME;
610 			break;
611 		}
612 
613 		timeout = io_schedule_timeout(timeout);
614 	}
615 	finish_wait(&guc->ct.wq, &wait);
616 
617 	return (timeout < 0) ? timeout : 0;
618 }
619 
620 int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout)
621 {
622 	if (!intel_uc_uses_guc_submission(&guc_to_gt(guc)->uc))
623 		return 0;
624 
625 	return intel_guc_wait_for_pending_msg(guc,
626 					      &guc->outstanding_submission_g2h,
627 					      true, timeout);
628 }
629 
630 static int guc_lrc_desc_pin(struct intel_context *ce, bool loop);
631 
632 static int __guc_add_request(struct intel_guc *guc, struct i915_request *rq)
633 {
634 	int err = 0;
635 	struct intel_context *ce = request_to_scheduling_context(rq);
636 	u32 action[3];
637 	int len = 0;
638 	u32 g2h_len_dw = 0;
639 	bool enabled;
640 
641 	lockdep_assert_held(&rq->engine->sched_engine->lock);
642 
643 	/*
644 	 * Corner case where requests were sitting in the priority list or a
645 	 * request resubmitted after the context was banned.
646 	 */
647 	if (unlikely(intel_context_is_banned(ce))) {
648 		i915_request_put(i915_request_mark_eio(rq));
649 		intel_engine_signal_breadcrumbs(ce->engine);
650 		return 0;
651 	}
652 
653 	GEM_BUG_ON(!atomic_read(&ce->guc_id.ref));
654 	GEM_BUG_ON(context_guc_id_invalid(ce));
655 
656 	spin_lock(&ce->guc_state.lock);
657 
658 	/*
659 	 * The request / context will be run on the hardware when scheduling
660 	 * gets enabled in the unblock. For multi-lrc we still submit the
661 	 * context to move the LRC tails.
662 	 */
663 	if (unlikely(context_blocked(ce) && !intel_context_is_parent(ce)))
664 		goto out;
665 
666 	enabled = context_enabled(ce) || context_blocked(ce);
667 
668 	if (!enabled) {
669 		action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
670 		action[len++] = ce->guc_id.id;
671 		action[len++] = GUC_CONTEXT_ENABLE;
672 		set_context_pending_enable(ce);
673 		intel_context_get(ce);
674 		g2h_len_dw = G2H_LEN_DW_SCHED_CONTEXT_MODE_SET;
675 	} else {
676 		action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT;
677 		action[len++] = ce->guc_id.id;
678 	}
679 
680 	err = intel_guc_send_nb(guc, action, len, g2h_len_dw);
681 	if (!enabled && !err) {
682 		trace_intel_context_sched_enable(ce);
683 		atomic_inc(&guc->outstanding_submission_g2h);
684 		set_context_enabled(ce);
685 
686 		/*
687 		 * Without multi-lrc KMD does the submission step (moving the
688 		 * lrc tail) so enabling scheduling is sufficient to submit the
689 		 * context. This isn't the case in multi-lrc submission as the
690 		 * GuC needs to move the tails, hence the need for another H2G
691 		 * to submit a multi-lrc context after enabling scheduling.
692 		 */
693 		if (intel_context_is_parent(ce)) {
694 			action[0] = INTEL_GUC_ACTION_SCHED_CONTEXT;
695 			err = intel_guc_send_nb(guc, action, len - 1, 0);
696 		}
697 	} else if (!enabled) {
698 		clr_context_pending_enable(ce);
699 		intel_context_put(ce);
700 	}
701 	if (likely(!err))
702 		trace_i915_request_guc_submit(rq);
703 
704 out:
705 	spin_unlock(&ce->guc_state.lock);
706 	return err;
707 }
708 
709 static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
710 {
711 	int ret = __guc_add_request(guc, rq);
712 
713 	if (unlikely(ret == -EBUSY)) {
714 		guc->stalled_request = rq;
715 		guc->submission_stall_reason = STALL_ADD_REQUEST;
716 	}
717 
718 	return ret;
719 }
720 
721 static inline void guc_set_lrc_tail(struct i915_request *rq)
722 {
723 	rq->context->lrc_reg_state[CTX_RING_TAIL] =
724 		intel_ring_set_tail(rq->ring, rq->tail);
725 }
726 
727 static inline int rq_prio(const struct i915_request *rq)
728 {
729 	return rq->sched.attr.priority;
730 }
731 
732 static bool is_multi_lrc_rq(struct i915_request *rq)
733 {
734 	return intel_context_is_parallel(rq->context);
735 }
736 
737 static bool can_merge_rq(struct i915_request *rq,
738 			 struct i915_request *last)
739 {
740 	return request_to_scheduling_context(rq) ==
741 		request_to_scheduling_context(last);
742 }
743 
744 static u32 wq_space_until_wrap(struct intel_context *ce)
745 {
746 	return (WQ_SIZE - ce->parallel.guc.wqi_tail);
747 }
748 
749 static void write_wqi(struct guc_process_desc *desc,
750 		      struct intel_context *ce,
751 		      u32 wqi_size)
752 {
753 	BUILD_BUG_ON(!is_power_of_2(WQ_SIZE));
754 
755 	/*
756 	 * Ensure WQI are visible before updating tail
757 	 */
758 	intel_guc_write_barrier(ce_to_guc(ce));
759 
760 	ce->parallel.guc.wqi_tail = (ce->parallel.guc.wqi_tail + wqi_size) &
761 		(WQ_SIZE - 1);
762 	WRITE_ONCE(desc->tail, ce->parallel.guc.wqi_tail);
763 }
764 
765 static int guc_wq_noop_append(struct intel_context *ce)
766 {
767 	struct guc_process_desc *desc = __get_process_desc(ce);
768 	u32 *wqi = get_wq_pointer(desc, ce, wq_space_until_wrap(ce));
769 	u32 len_dw = wq_space_until_wrap(ce) / sizeof(u32) - 1;
770 
771 	if (!wqi)
772 		return -EBUSY;
773 
774 	GEM_BUG_ON(!FIELD_FIT(WQ_LEN_MASK, len_dw));
775 
776 	*wqi = FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_NOOP) |
777 		FIELD_PREP(WQ_LEN_MASK, len_dw);
778 	ce->parallel.guc.wqi_tail = 0;
779 
780 	return 0;
781 }
782 
783 static int __guc_wq_item_append(struct i915_request *rq)
784 {
785 	struct intel_context *ce = request_to_scheduling_context(rq);
786 	struct intel_context *child;
787 	struct guc_process_desc *desc = __get_process_desc(ce);
788 	unsigned int wqi_size = (ce->parallel.number_children + 4) *
789 		sizeof(u32);
790 	u32 *wqi;
791 	u32 len_dw = (wqi_size / sizeof(u32)) - 1;
792 	int ret;
793 
794 	/* Ensure context is in correct state updating work queue */
795 	GEM_BUG_ON(!atomic_read(&ce->guc_id.ref));
796 	GEM_BUG_ON(context_guc_id_invalid(ce));
797 	GEM_BUG_ON(context_wait_for_deregister_to_register(ce));
798 	GEM_BUG_ON(!lrc_desc_registered(ce_to_guc(ce), ce->guc_id.id));
799 
800 	/* Insert NOOP if this work queue item will wrap the tail pointer. */
801 	if (wqi_size > wq_space_until_wrap(ce)) {
802 		ret = guc_wq_noop_append(ce);
803 		if (ret)
804 			return ret;
805 	}
806 
807 	wqi = get_wq_pointer(desc, ce, wqi_size);
808 	if (!wqi)
809 		return -EBUSY;
810 
811 	GEM_BUG_ON(!FIELD_FIT(WQ_LEN_MASK, len_dw));
812 
813 	*wqi++ = FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_MULTI_LRC) |
814 		FIELD_PREP(WQ_LEN_MASK, len_dw);
815 	*wqi++ = ce->lrc.lrca;
816 	*wqi++ = FIELD_PREP(WQ_GUC_ID_MASK, ce->guc_id.id) |
817 	       FIELD_PREP(WQ_RING_TAIL_MASK, ce->ring->tail / sizeof(u64));
818 	*wqi++ = 0;	/* fence_id */
819 	for_each_child(ce, child)
820 		*wqi++ = child->ring->tail / sizeof(u64);
821 
822 	write_wqi(desc, ce, wqi_size);
823 
824 	return 0;
825 }
826 
827 static int guc_wq_item_append(struct intel_guc *guc,
828 			      struct i915_request *rq)
829 {
830 	struct intel_context *ce = request_to_scheduling_context(rq);
831 	int ret = 0;
832 
833 	if (likely(!intel_context_is_banned(ce))) {
834 		ret = __guc_wq_item_append(rq);
835 
836 		if (unlikely(ret == -EBUSY)) {
837 			guc->stalled_request = rq;
838 			guc->submission_stall_reason = STALL_MOVE_LRC_TAIL;
839 		}
840 	}
841 
842 	return ret;
843 }
844 
845 static bool multi_lrc_submit(struct i915_request *rq)
846 {
847 	struct intel_context *ce = request_to_scheduling_context(rq);
848 
849 	intel_ring_set_tail(rq->ring, rq->tail);
850 
851 	/*
852 	 * We expect the front end (execbuf IOCTL) to set this flag on the last
853 	 * request generated from a multi-BB submission. This indicates to the
854 	 * backend (GuC interface) that we should submit this context thus
855 	 * submitting all the requests generated in parallel.
856 	 */
857 	return test_bit(I915_FENCE_FLAG_SUBMIT_PARALLEL, &rq->fence.flags) ||
858 		intel_context_is_banned(ce);
859 }
860 
861 static int guc_dequeue_one_context(struct intel_guc *guc)
862 {
863 	struct i915_sched_engine * const sched_engine = guc->sched_engine;
864 	struct i915_request *last = NULL;
865 	bool submit = false;
866 	struct rb_node *rb;
867 	int ret;
868 
869 	lockdep_assert_held(&sched_engine->lock);
870 
871 	if (guc->stalled_request) {
872 		submit = true;
873 		last = guc->stalled_request;
874 
875 		switch (guc->submission_stall_reason) {
876 		case STALL_REGISTER_CONTEXT:
877 			goto register_context;
878 		case STALL_MOVE_LRC_TAIL:
879 			goto move_lrc_tail;
880 		case STALL_ADD_REQUEST:
881 			goto add_request;
882 		default:
883 			MISSING_CASE(guc->submission_stall_reason);
884 		}
885 	}
886 
887 	while ((rb = rb_first_cached(&sched_engine->queue))) {
888 		struct i915_priolist *p = to_priolist(rb);
889 		struct i915_request *rq, *rn;
890 
891 		priolist_for_each_request_consume(rq, rn, p) {
892 			if (last && !can_merge_rq(rq, last))
893 				goto register_context;
894 
895 			list_del_init(&rq->sched.link);
896 
897 			__i915_request_submit(rq);
898 
899 			trace_i915_request_in(rq, 0);
900 			last = rq;
901 
902 			if (is_multi_lrc_rq(rq)) {
903 				/*
904 				 * We need to coalesce all multi-lrc requests in
905 				 * a relationship into a single H2G. We are
906 				 * guaranteed that all of these requests will be
907 				 * submitted sequentially.
908 				 */
909 				if (multi_lrc_submit(rq)) {
910 					submit = true;
911 					goto register_context;
912 				}
913 			} else {
914 				submit = true;
915 			}
916 		}
917 
918 		rb_erase_cached(&p->node, &sched_engine->queue);
919 		i915_priolist_free(p);
920 	}
921 
922 register_context:
923 	if (submit) {
924 		struct intel_context *ce = request_to_scheduling_context(last);
925 
926 		if (unlikely(!lrc_desc_registered(guc, ce->guc_id.id) &&
927 			     !intel_context_is_banned(ce))) {
928 			ret = guc_lrc_desc_pin(ce, false);
929 			if (unlikely(ret == -EPIPE)) {
930 				goto deadlk;
931 			} else if (ret == -EBUSY) {
932 				guc->stalled_request = last;
933 				guc->submission_stall_reason =
934 					STALL_REGISTER_CONTEXT;
935 				goto schedule_tasklet;
936 			} else if (ret != 0) {
937 				GEM_WARN_ON(ret);	/* Unexpected */
938 				goto deadlk;
939 			}
940 		}
941 
942 move_lrc_tail:
943 		if (is_multi_lrc_rq(last)) {
944 			ret = guc_wq_item_append(guc, last);
945 			if (ret == -EBUSY) {
946 				goto schedule_tasklet;
947 			} else if (ret != 0) {
948 				GEM_WARN_ON(ret);	/* Unexpected */
949 				goto deadlk;
950 			}
951 		} else {
952 			guc_set_lrc_tail(last);
953 		}
954 
955 add_request:
956 		ret = guc_add_request(guc, last);
957 		if (unlikely(ret == -EPIPE)) {
958 			goto deadlk;
959 		} else if (ret == -EBUSY) {
960 			goto schedule_tasklet;
961 		} else if (ret != 0) {
962 			GEM_WARN_ON(ret);	/* Unexpected */
963 			goto deadlk;
964 		}
965 	}
966 
967 	guc->stalled_request = NULL;
968 	guc->submission_stall_reason = STALL_NONE;
969 	return submit;
970 
971 deadlk:
972 	sched_engine->tasklet.callback = NULL;
973 	tasklet_disable_nosync(&sched_engine->tasklet);
974 	return false;
975 
976 schedule_tasklet:
977 	tasklet_schedule(&sched_engine->tasklet);
978 	return false;
979 }
980 
981 static void guc_submission_tasklet(struct tasklet_struct *t)
982 {
983 	struct i915_sched_engine *sched_engine =
984 		from_tasklet(sched_engine, t, tasklet);
985 	unsigned long flags;
986 	bool loop;
987 
988 	spin_lock_irqsave(&sched_engine->lock, flags);
989 
990 	do {
991 		loop = guc_dequeue_one_context(sched_engine->private_data);
992 	} while (loop);
993 
994 	i915_sched_engine_reset_on_empty(sched_engine);
995 
996 	spin_unlock_irqrestore(&sched_engine->lock, flags);
997 }
998 
999 static void cs_irq_handler(struct intel_engine_cs *engine, u16 iir)
1000 {
1001 	if (iir & GT_RENDER_USER_INTERRUPT)
1002 		intel_engine_signal_breadcrumbs(engine);
1003 }
1004 
1005 static void __guc_context_destroy(struct intel_context *ce);
1006 static void release_guc_id(struct intel_guc *guc, struct intel_context *ce);
1007 static void guc_signal_context_fence(struct intel_context *ce);
1008 static void guc_cancel_context_requests(struct intel_context *ce);
1009 static void guc_blocked_fence_complete(struct intel_context *ce);
1010 
1011 static void scrub_guc_desc_for_outstanding_g2h(struct intel_guc *guc)
1012 {
1013 	struct intel_context *ce;
1014 	unsigned long index, flags;
1015 	bool pending_disable, pending_enable, deregister, destroyed, banned;
1016 
1017 	xa_lock_irqsave(&guc->context_lookup, flags);
1018 	xa_for_each(&guc->context_lookup, index, ce) {
1019 		/*
1020 		 * Corner case where the ref count on the object is zero but and
1021 		 * deregister G2H was lost. In this case we don't touch the ref
1022 		 * count and finish the destroy of the context.
1023 		 */
1024 		bool do_put = kref_get_unless_zero(&ce->ref);
1025 
1026 		xa_unlock(&guc->context_lookup);
1027 
1028 		spin_lock(&ce->guc_state.lock);
1029 
1030 		/*
1031 		 * Once we are at this point submission_disabled() is guaranteed
1032 		 * to be visible to all callers who set the below flags (see above
1033 		 * flush and flushes in reset_prepare). If submission_disabled()
1034 		 * is set, the caller shouldn't set these flags.
1035 		 */
1036 
1037 		destroyed = context_destroyed(ce);
1038 		pending_enable = context_pending_enable(ce);
1039 		pending_disable = context_pending_disable(ce);
1040 		deregister = context_wait_for_deregister_to_register(ce);
1041 		banned = context_banned(ce);
1042 		init_sched_state(ce);
1043 
1044 		spin_unlock(&ce->guc_state.lock);
1045 
1046 		if (pending_enable || destroyed || deregister) {
1047 			decr_outstanding_submission_g2h(guc);
1048 			if (deregister)
1049 				guc_signal_context_fence(ce);
1050 			if (destroyed) {
1051 				intel_gt_pm_put_async(guc_to_gt(guc));
1052 				release_guc_id(guc, ce);
1053 				__guc_context_destroy(ce);
1054 			}
1055 			if (pending_enable || deregister)
1056 				intel_context_put(ce);
1057 		}
1058 
1059 		/* Not mutualy exclusive with above if statement. */
1060 		if (pending_disable) {
1061 			guc_signal_context_fence(ce);
1062 			if (banned) {
1063 				guc_cancel_context_requests(ce);
1064 				intel_engine_signal_breadcrumbs(ce->engine);
1065 			}
1066 			intel_context_sched_disable_unpin(ce);
1067 			decr_outstanding_submission_g2h(guc);
1068 
1069 			spin_lock(&ce->guc_state.lock);
1070 			guc_blocked_fence_complete(ce);
1071 			spin_unlock(&ce->guc_state.lock);
1072 
1073 			intel_context_put(ce);
1074 		}
1075 
1076 		if (do_put)
1077 			intel_context_put(ce);
1078 		xa_lock(&guc->context_lookup);
1079 	}
1080 	xa_unlock_irqrestore(&guc->context_lookup, flags);
1081 }
1082 
1083 /*
1084  * GuC stores busyness stats for each engine at context in/out boundaries. A
1085  * context 'in' logs execution start time, 'out' adds in -> out delta to total.
1086  * i915/kmd accesses 'start', 'total' and 'context id' from memory shared with
1087  * GuC.
1088  *
1089  * __i915_pmu_event_read samples engine busyness. When sampling, if context id
1090  * is valid (!= ~0) and start is non-zero, the engine is considered to be
1091  * active. For an active engine total busyness = total + (now - start), where
1092  * 'now' is the time at which the busyness is sampled. For inactive engine,
1093  * total busyness = total.
1094  *
1095  * All times are captured from GUCPMTIMESTAMP reg and are in gt clock domain.
1096  *
1097  * The start and total values provided by GuC are 32 bits and wrap around in a
1098  * few minutes. Since perf pmu provides busyness as 64 bit monotonically
1099  * increasing ns values, there is a need for this implementation to account for
1100  * overflows and extend the GuC provided values to 64 bits before returning
1101  * busyness to the user. In order to do that, a worker runs periodically at
1102  * frequency = 1/8th the time it takes for the timestamp to wrap (i.e. once in
1103  * 27 seconds for a gt clock frequency of 19.2 MHz).
1104  */
1105 
1106 #define WRAP_TIME_CLKS U32_MAX
1107 #define POLL_TIME_CLKS (WRAP_TIME_CLKS >> 3)
1108 
1109 static void
1110 __extend_last_switch(struct intel_guc *guc, u64 *prev_start, u32 new_start)
1111 {
1112 	u32 gt_stamp_hi = upper_32_bits(guc->timestamp.gt_stamp);
1113 	u32 gt_stamp_last = lower_32_bits(guc->timestamp.gt_stamp);
1114 
1115 	if (new_start == lower_32_bits(*prev_start))
1116 		return;
1117 
1118 	/*
1119 	 * When gt is unparked, we update the gt timestamp and start the ping
1120 	 * worker that updates the gt_stamp every POLL_TIME_CLKS. As long as gt
1121 	 * is unparked, all switched in contexts will have a start time that is
1122 	 * within +/- POLL_TIME_CLKS of the most recent gt_stamp.
1123 	 *
1124 	 * If neither gt_stamp nor new_start has rolled over, then the
1125 	 * gt_stamp_hi does not need to be adjusted, however if one of them has
1126 	 * rolled over, we need to adjust gt_stamp_hi accordingly.
1127 	 *
1128 	 * The below conditions address the cases of new_start rollover and
1129 	 * gt_stamp_last rollover respectively.
1130 	 */
1131 	if (new_start < gt_stamp_last &&
1132 	    (new_start - gt_stamp_last) <= POLL_TIME_CLKS)
1133 		gt_stamp_hi++;
1134 
1135 	if (new_start > gt_stamp_last &&
1136 	    (gt_stamp_last - new_start) <= POLL_TIME_CLKS && gt_stamp_hi)
1137 		gt_stamp_hi--;
1138 
1139 	*prev_start = ((u64)gt_stamp_hi << 32) | new_start;
1140 }
1141 
1142 /*
1143  * GuC updates shared memory and KMD reads it. Since this is not synchronized,
1144  * we run into a race where the value read is inconsistent. Sometimes the
1145  * inconsistency is in reading the upper MSB bytes of the last_in value when
1146  * this race occurs. 2 types of cases are seen - upper 8 bits are zero and upper
1147  * 24 bits are zero. Since these are non-zero values, it is non-trivial to
1148  * determine validity of these values. Instead we read the values multiple times
1149  * until they are consistent. In test runs, 3 attempts results in consistent
1150  * values. The upper bound is set to 6 attempts and may need to be tuned as per
1151  * any new occurences.
1152  */
1153 static void __get_engine_usage_record(struct intel_engine_cs *engine,
1154 				      u32 *last_in, u32 *id, u32 *total)
1155 {
1156 	struct guc_engine_usage_record *rec = intel_guc_engine_usage(engine);
1157 	int i = 0;
1158 
1159 	do {
1160 		*last_in = READ_ONCE(rec->last_switch_in_stamp);
1161 		*id = READ_ONCE(rec->current_context_index);
1162 		*total = READ_ONCE(rec->total_runtime);
1163 
1164 		if (READ_ONCE(rec->last_switch_in_stamp) == *last_in &&
1165 		    READ_ONCE(rec->current_context_index) == *id &&
1166 		    READ_ONCE(rec->total_runtime) == *total)
1167 			break;
1168 	} while (++i < 6);
1169 }
1170 
1171 static void guc_update_engine_gt_clks(struct intel_engine_cs *engine)
1172 {
1173 	struct intel_engine_guc_stats *stats = &engine->stats.guc;
1174 	struct intel_guc *guc = &engine->gt->uc.guc;
1175 	u32 last_switch, ctx_id, total;
1176 
1177 	lockdep_assert_held(&guc->timestamp.lock);
1178 
1179 	__get_engine_usage_record(engine, &last_switch, &ctx_id, &total);
1180 
1181 	stats->running = ctx_id != ~0U && last_switch;
1182 	if (stats->running)
1183 		__extend_last_switch(guc, &stats->start_gt_clk, last_switch);
1184 
1185 	/*
1186 	 * Instead of adjusting the total for overflow, just add the
1187 	 * difference from previous sample stats->total_gt_clks
1188 	 */
1189 	if (total && total != ~0U) {
1190 		stats->total_gt_clks += (u32)(total - stats->prev_total);
1191 		stats->prev_total = total;
1192 	}
1193 }
1194 
1195 static u32 gpm_timestamp_shift(struct intel_gt *gt)
1196 {
1197 	intel_wakeref_t wakeref;
1198 	u32 reg, shift;
1199 
1200 	with_intel_runtime_pm(gt->uncore->rpm, wakeref)
1201 		reg = intel_uncore_read(gt->uncore, RPM_CONFIG0);
1202 
1203 	shift = (reg & GEN10_RPM_CONFIG0_CTC_SHIFT_PARAMETER_MASK) >>
1204 		GEN10_RPM_CONFIG0_CTC_SHIFT_PARAMETER_SHIFT;
1205 
1206 	return 3 - shift;
1207 }
1208 
1209 static u64 gpm_timestamp(struct intel_gt *gt)
1210 {
1211 	u32 lo, hi, old_hi, loop = 0;
1212 
1213 	hi = intel_uncore_read(gt->uncore, MISC_STATUS1);
1214 	do {
1215 		lo = intel_uncore_read(gt->uncore, MISC_STATUS0);
1216 		old_hi = hi;
1217 		hi = intel_uncore_read(gt->uncore, MISC_STATUS1);
1218 	} while (old_hi != hi && loop++ < 2);
1219 
1220 	return ((u64)hi << 32) | lo;
1221 }
1222 
1223 static void guc_update_pm_timestamp(struct intel_guc *guc, ktime_t *now)
1224 {
1225 	struct intel_gt *gt = guc_to_gt(guc);
1226 	u32 gt_stamp_lo, gt_stamp_hi;
1227 	u64 gpm_ts;
1228 
1229 	lockdep_assert_held(&guc->timestamp.lock);
1230 
1231 	gt_stamp_hi = upper_32_bits(guc->timestamp.gt_stamp);
1232 	gpm_ts = gpm_timestamp(gt) >> guc->timestamp.shift;
1233 	gt_stamp_lo = lower_32_bits(gpm_ts);
1234 	*now = ktime_get();
1235 
1236 	if (gt_stamp_lo < lower_32_bits(guc->timestamp.gt_stamp))
1237 		gt_stamp_hi++;
1238 
1239 	guc->timestamp.gt_stamp = ((u64)gt_stamp_hi << 32) | gt_stamp_lo;
1240 }
1241 
1242 /*
1243  * Unlike the execlist mode of submission total and active times are in terms of
1244  * gt clocks. The *now parameter is retained to return the cpu time at which the
1245  * busyness was sampled.
1246  */
1247 static ktime_t guc_engine_busyness(struct intel_engine_cs *engine, ktime_t *now)
1248 {
1249 	struct intel_engine_guc_stats stats_saved, *stats = &engine->stats.guc;
1250 	struct i915_gpu_error *gpu_error = &engine->i915->gpu_error;
1251 	struct intel_gt *gt = engine->gt;
1252 	struct intel_guc *guc = &gt->uc.guc;
1253 	u64 total, gt_stamp_saved;
1254 	unsigned long flags;
1255 	u32 reset_count;
1256 	bool in_reset;
1257 
1258 	spin_lock_irqsave(&guc->timestamp.lock, flags);
1259 
1260 	/*
1261 	 * If a reset happened, we risk reading partially updated engine
1262 	 * busyness from GuC, so we just use the driver stored copy of busyness.
1263 	 * Synchronize with gt reset using reset_count and the
1264 	 * I915_RESET_BACKOFF flag. Note that reset flow updates the reset_count
1265 	 * after I915_RESET_BACKOFF flag, so ensure that the reset_count is
1266 	 * usable by checking the flag afterwards.
1267 	 */
1268 	reset_count = i915_reset_count(gpu_error);
1269 	in_reset = test_bit(I915_RESET_BACKOFF, &gt->reset.flags);
1270 
1271 	*now = ktime_get();
1272 
1273 	/*
1274 	 * The active busyness depends on start_gt_clk and gt_stamp.
1275 	 * gt_stamp is updated by i915 only when gt is awake and the
1276 	 * start_gt_clk is derived from GuC state. To get a consistent
1277 	 * view of activity, we query the GuC state only if gt is awake.
1278 	 */
1279 	if (!in_reset && intel_gt_pm_get_if_awake(gt)) {
1280 		stats_saved = *stats;
1281 		gt_stamp_saved = guc->timestamp.gt_stamp;
1282 		/*
1283 		 * Update gt_clks, then gt timestamp to simplify the 'gt_stamp -
1284 		 * start_gt_clk' calculation below for active engines.
1285 		 */
1286 		guc_update_engine_gt_clks(engine);
1287 		guc_update_pm_timestamp(guc, now);
1288 		intel_gt_pm_put_async(gt);
1289 		if (i915_reset_count(gpu_error) != reset_count) {
1290 			*stats = stats_saved;
1291 			guc->timestamp.gt_stamp = gt_stamp_saved;
1292 		}
1293 	}
1294 
1295 	total = intel_gt_clock_interval_to_ns(gt, stats->total_gt_clks);
1296 	if (stats->running) {
1297 		u64 clk = guc->timestamp.gt_stamp - stats->start_gt_clk;
1298 
1299 		total += intel_gt_clock_interval_to_ns(gt, clk);
1300 	}
1301 
1302 	spin_unlock_irqrestore(&guc->timestamp.lock, flags);
1303 
1304 	return ns_to_ktime(total);
1305 }
1306 
1307 static void __reset_guc_busyness_stats(struct intel_guc *guc)
1308 {
1309 	struct intel_gt *gt = guc_to_gt(guc);
1310 	struct intel_engine_cs *engine;
1311 	enum intel_engine_id id;
1312 	unsigned long flags;
1313 	ktime_t unused;
1314 
1315 	cancel_delayed_work_sync(&guc->timestamp.work);
1316 
1317 	spin_lock_irqsave(&guc->timestamp.lock, flags);
1318 
1319 	guc_update_pm_timestamp(guc, &unused);
1320 	for_each_engine(engine, gt, id) {
1321 		guc_update_engine_gt_clks(engine);
1322 		engine->stats.guc.prev_total = 0;
1323 	}
1324 
1325 	spin_unlock_irqrestore(&guc->timestamp.lock, flags);
1326 }
1327 
1328 static void __update_guc_busyness_stats(struct intel_guc *guc)
1329 {
1330 	struct intel_gt *gt = guc_to_gt(guc);
1331 	struct intel_engine_cs *engine;
1332 	enum intel_engine_id id;
1333 	unsigned long flags;
1334 	ktime_t unused;
1335 
1336 	spin_lock_irqsave(&guc->timestamp.lock, flags);
1337 
1338 	guc_update_pm_timestamp(guc, &unused);
1339 	for_each_engine(engine, gt, id)
1340 		guc_update_engine_gt_clks(engine);
1341 
1342 	spin_unlock_irqrestore(&guc->timestamp.lock, flags);
1343 }
1344 
1345 static void guc_timestamp_ping(struct work_struct *wrk)
1346 {
1347 	struct intel_guc *guc = container_of(wrk, typeof(*guc),
1348 					     timestamp.work.work);
1349 	struct intel_uc *uc = container_of(guc, typeof(*uc), guc);
1350 	struct intel_gt *gt = guc_to_gt(guc);
1351 	intel_wakeref_t wakeref;
1352 	int srcu, ret;
1353 
1354 	/*
1355 	 * Synchronize with gt reset to make sure the worker does not
1356 	 * corrupt the engine/guc stats.
1357 	 */
1358 	ret = intel_gt_reset_trylock(gt, &srcu);
1359 	if (ret)
1360 		return;
1361 
1362 	with_intel_runtime_pm(&gt->i915->runtime_pm, wakeref)
1363 		__update_guc_busyness_stats(guc);
1364 
1365 	intel_gt_reset_unlock(gt, srcu);
1366 
1367 	mod_delayed_work(system_highpri_wq, &guc->timestamp.work,
1368 			 guc->timestamp.ping_delay);
1369 }
1370 
1371 static int guc_action_enable_usage_stats(struct intel_guc *guc)
1372 {
1373 	u32 offset = intel_guc_engine_usage_offset(guc);
1374 	u32 action[] = {
1375 		INTEL_GUC_ACTION_SET_ENG_UTIL_BUFF,
1376 		offset,
1377 		0,
1378 	};
1379 
1380 	return intel_guc_send(guc, action, ARRAY_SIZE(action));
1381 }
1382 
1383 static void guc_init_engine_stats(struct intel_guc *guc)
1384 {
1385 	struct intel_gt *gt = guc_to_gt(guc);
1386 	intel_wakeref_t wakeref;
1387 
1388 	mod_delayed_work(system_highpri_wq, &guc->timestamp.work,
1389 			 guc->timestamp.ping_delay);
1390 
1391 	with_intel_runtime_pm(&gt->i915->runtime_pm, wakeref) {
1392 		int ret = guc_action_enable_usage_stats(guc);
1393 
1394 		if (ret)
1395 			drm_err(&gt->i915->drm,
1396 				"Failed to enable usage stats: %d!\n", ret);
1397 	}
1398 }
1399 
1400 void intel_guc_busyness_park(struct intel_gt *gt)
1401 {
1402 	struct intel_guc *guc = &gt->uc.guc;
1403 
1404 	if (!guc_submission_initialized(guc))
1405 		return;
1406 
1407 	cancel_delayed_work(&guc->timestamp.work);
1408 	__update_guc_busyness_stats(guc);
1409 }
1410 
1411 void intel_guc_busyness_unpark(struct intel_gt *gt)
1412 {
1413 	struct intel_guc *guc = &gt->uc.guc;
1414 	unsigned long flags;
1415 	ktime_t unused;
1416 
1417 	if (!guc_submission_initialized(guc))
1418 		return;
1419 
1420 	spin_lock_irqsave(&guc->timestamp.lock, flags);
1421 	guc_update_pm_timestamp(guc, &unused);
1422 	spin_unlock_irqrestore(&guc->timestamp.lock, flags);
1423 	mod_delayed_work(system_highpri_wq, &guc->timestamp.work,
1424 			 guc->timestamp.ping_delay);
1425 }
1426 
1427 static inline bool
1428 submission_disabled(struct intel_guc *guc)
1429 {
1430 	struct i915_sched_engine * const sched_engine = guc->sched_engine;
1431 
1432 	return unlikely(!sched_engine ||
1433 			!__tasklet_is_enabled(&sched_engine->tasklet));
1434 }
1435 
1436 static void disable_submission(struct intel_guc *guc)
1437 {
1438 	struct i915_sched_engine * const sched_engine = guc->sched_engine;
1439 
1440 	if (__tasklet_is_enabled(&sched_engine->tasklet)) {
1441 		GEM_BUG_ON(!guc->ct.enabled);
1442 		__tasklet_disable_sync_once(&sched_engine->tasklet);
1443 		sched_engine->tasklet.callback = NULL;
1444 	}
1445 }
1446 
1447 static void enable_submission(struct intel_guc *guc)
1448 {
1449 	struct i915_sched_engine * const sched_engine = guc->sched_engine;
1450 	unsigned long flags;
1451 
1452 	spin_lock_irqsave(&guc->sched_engine->lock, flags);
1453 	sched_engine->tasklet.callback = guc_submission_tasklet;
1454 	wmb();	/* Make sure callback visible */
1455 	if (!__tasklet_is_enabled(&sched_engine->tasklet) &&
1456 	    __tasklet_enable(&sched_engine->tasklet)) {
1457 		GEM_BUG_ON(!guc->ct.enabled);
1458 
1459 		/* And kick in case we missed a new request submission. */
1460 		tasklet_hi_schedule(&sched_engine->tasklet);
1461 	}
1462 	spin_unlock_irqrestore(&guc->sched_engine->lock, flags);
1463 }
1464 
1465 static void guc_flush_submissions(struct intel_guc *guc)
1466 {
1467 	struct i915_sched_engine * const sched_engine = guc->sched_engine;
1468 	unsigned long flags;
1469 
1470 	spin_lock_irqsave(&sched_engine->lock, flags);
1471 	spin_unlock_irqrestore(&sched_engine->lock, flags);
1472 }
1473 
1474 static void guc_flush_destroyed_contexts(struct intel_guc *guc);
1475 
1476 void intel_guc_submission_reset_prepare(struct intel_guc *guc)
1477 {
1478 	int i;
1479 
1480 	if (unlikely(!guc_submission_initialized(guc))) {
1481 		/* Reset called during driver load? GuC not yet initialised! */
1482 		return;
1483 	}
1484 
1485 	intel_gt_park_heartbeats(guc_to_gt(guc));
1486 	disable_submission(guc);
1487 	guc->interrupts.disable(guc);
1488 	__reset_guc_busyness_stats(guc);
1489 
1490 	/* Flush IRQ handler */
1491 	spin_lock_irq(&guc_to_gt(guc)->irq_lock);
1492 	spin_unlock_irq(&guc_to_gt(guc)->irq_lock);
1493 
1494 	guc_flush_submissions(guc);
1495 	guc_flush_destroyed_contexts(guc);
1496 
1497 	/*
1498 	 * Handle any outstanding G2Hs before reset. Call IRQ handler directly
1499 	 * each pass as interrupt have been disabled. We always scrub for
1500 	 * outstanding G2H as it is possible for outstanding_submission_g2h to
1501 	 * be incremented after the context state update.
1502 	 */
1503 	for (i = 0; i < 4 && atomic_read(&guc->outstanding_submission_g2h); ++i) {
1504 		intel_guc_to_host_event_handler(guc);
1505 #define wait_for_reset(guc, wait_var) \
1506 		intel_guc_wait_for_pending_msg(guc, wait_var, false, (HZ / 20))
1507 		do {
1508 			wait_for_reset(guc, &guc->outstanding_submission_g2h);
1509 		} while (!list_empty(&guc->ct.requests.incoming));
1510 	}
1511 
1512 	scrub_guc_desc_for_outstanding_g2h(guc);
1513 }
1514 
1515 static struct intel_engine_cs *
1516 guc_virtual_get_sibling(struct intel_engine_cs *ve, unsigned int sibling)
1517 {
1518 	struct intel_engine_cs *engine;
1519 	intel_engine_mask_t tmp, mask = ve->mask;
1520 	unsigned int num_siblings = 0;
1521 
1522 	for_each_engine_masked(engine, ve->gt, mask, tmp)
1523 		if (num_siblings++ == sibling)
1524 			return engine;
1525 
1526 	return NULL;
1527 }
1528 
1529 static inline struct intel_engine_cs *
1530 __context_to_physical_engine(struct intel_context *ce)
1531 {
1532 	struct intel_engine_cs *engine = ce->engine;
1533 
1534 	if (intel_engine_is_virtual(engine))
1535 		engine = guc_virtual_get_sibling(engine, 0);
1536 
1537 	return engine;
1538 }
1539 
1540 static void guc_reset_state(struct intel_context *ce, u32 head, bool scrub)
1541 {
1542 	struct intel_engine_cs *engine = __context_to_physical_engine(ce);
1543 
1544 	if (intel_context_is_banned(ce))
1545 		return;
1546 
1547 	GEM_BUG_ON(!intel_context_is_pinned(ce));
1548 
1549 	/*
1550 	 * We want a simple context + ring to execute the breadcrumb update.
1551 	 * We cannot rely on the context being intact across the GPU hang,
1552 	 * so clear it and rebuild just what we need for the breadcrumb.
1553 	 * All pending requests for this context will be zapped, and any
1554 	 * future request will be after userspace has had the opportunity
1555 	 * to recreate its own state.
1556 	 */
1557 	if (scrub)
1558 		lrc_init_regs(ce, engine, true);
1559 
1560 	/* Rerun the request; its payload has been neutered (if guilty). */
1561 	lrc_update_regs(ce, engine, head);
1562 }
1563 
1564 static void guc_reset_nop(struct intel_engine_cs *engine)
1565 {
1566 }
1567 
1568 static void guc_rewind_nop(struct intel_engine_cs *engine, bool stalled)
1569 {
1570 }
1571 
1572 static void
1573 __unwind_incomplete_requests(struct intel_context *ce)
1574 {
1575 	struct i915_request *rq, *rn;
1576 	struct list_head *pl;
1577 	int prio = I915_PRIORITY_INVALID;
1578 	struct i915_sched_engine * const sched_engine =
1579 		ce->engine->sched_engine;
1580 	unsigned long flags;
1581 
1582 	spin_lock_irqsave(&sched_engine->lock, flags);
1583 	spin_lock(&ce->guc_state.lock);
1584 	list_for_each_entry_safe_reverse(rq, rn,
1585 					 &ce->guc_state.requests,
1586 					 sched.link) {
1587 		if (i915_request_completed(rq))
1588 			continue;
1589 
1590 		list_del_init(&rq->sched.link);
1591 		__i915_request_unsubmit(rq);
1592 
1593 		/* Push the request back into the queue for later resubmission. */
1594 		GEM_BUG_ON(rq_prio(rq) == I915_PRIORITY_INVALID);
1595 		if (rq_prio(rq) != prio) {
1596 			prio = rq_prio(rq);
1597 			pl = i915_sched_lookup_priolist(sched_engine, prio);
1598 		}
1599 		GEM_BUG_ON(i915_sched_engine_is_empty(sched_engine));
1600 
1601 		list_add(&rq->sched.link, pl);
1602 		set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
1603 	}
1604 	spin_unlock(&ce->guc_state.lock);
1605 	spin_unlock_irqrestore(&sched_engine->lock, flags);
1606 }
1607 
1608 static void __guc_reset_context(struct intel_context *ce, bool stalled)
1609 {
1610 	bool local_stalled;
1611 	struct i915_request *rq;
1612 	unsigned long flags;
1613 	u32 head;
1614 	int i, number_children = ce->parallel.number_children;
1615 	bool skip = false;
1616 	struct intel_context *parent = ce;
1617 
1618 	GEM_BUG_ON(intel_context_is_child(ce));
1619 
1620 	intel_context_get(ce);
1621 
1622 	/*
1623 	 * GuC will implicitly mark the context as non-schedulable when it sends
1624 	 * the reset notification. Make sure our state reflects this change. The
1625 	 * context will be marked enabled on resubmission.
1626 	 *
1627 	 * XXX: If the context is reset as a result of the request cancellation
1628 	 * this G2H is received after the schedule disable complete G2H which is
1629 	 * wrong as this creates a race between the request cancellation code
1630 	 * re-submitting the context and this G2H handler. This is a bug in the
1631 	 * GuC but can be worked around in the meantime but converting this to a
1632 	 * NOP if a pending enable is in flight as this indicates that a request
1633 	 * cancellation has occurred.
1634 	 */
1635 	spin_lock_irqsave(&ce->guc_state.lock, flags);
1636 	if (likely(!context_pending_enable(ce)))
1637 		clr_context_enabled(ce);
1638 	else
1639 		skip = true;
1640 	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1641 	if (unlikely(skip))
1642 		goto out_put;
1643 
1644 	/*
1645 	 * For each context in the relationship find the hanging request
1646 	 * resetting each context / request as needed
1647 	 */
1648 	for (i = 0; i < number_children + 1; ++i) {
1649 		if (!intel_context_is_pinned(ce))
1650 			goto next_context;
1651 
1652 		local_stalled = false;
1653 		rq = intel_context_find_active_request(ce);
1654 		if (!rq) {
1655 			head = ce->ring->tail;
1656 			goto out_replay;
1657 		}
1658 
1659 		if (i915_request_started(rq))
1660 			local_stalled = true;
1661 
1662 		GEM_BUG_ON(i915_active_is_idle(&ce->active));
1663 		head = intel_ring_wrap(ce->ring, rq->head);
1664 
1665 		__i915_request_reset(rq, local_stalled && stalled);
1666 out_replay:
1667 		guc_reset_state(ce, head, local_stalled && stalled);
1668 next_context:
1669 		if (i != number_children)
1670 			ce = list_next_entry(ce, parallel.child_link);
1671 	}
1672 
1673 	__unwind_incomplete_requests(parent);
1674 out_put:
1675 	intel_context_put(parent);
1676 }
1677 
1678 void intel_guc_submission_reset(struct intel_guc *guc, bool stalled)
1679 {
1680 	struct intel_context *ce;
1681 	unsigned long index;
1682 	unsigned long flags;
1683 
1684 	if (unlikely(!guc_submission_initialized(guc))) {
1685 		/* Reset called during driver load? GuC not yet initialised! */
1686 		return;
1687 	}
1688 
1689 	xa_lock_irqsave(&guc->context_lookup, flags);
1690 	xa_for_each(&guc->context_lookup, index, ce) {
1691 		if (!kref_get_unless_zero(&ce->ref))
1692 			continue;
1693 
1694 		xa_unlock(&guc->context_lookup);
1695 
1696 		if (intel_context_is_pinned(ce) &&
1697 		    !intel_context_is_child(ce))
1698 			__guc_reset_context(ce, stalled);
1699 
1700 		intel_context_put(ce);
1701 
1702 		xa_lock(&guc->context_lookup);
1703 	}
1704 	xa_unlock_irqrestore(&guc->context_lookup, flags);
1705 
1706 	/* GuC is blown away, drop all references to contexts */
1707 	xa_destroy(&guc->context_lookup);
1708 }
1709 
1710 static void guc_cancel_context_requests(struct intel_context *ce)
1711 {
1712 	struct i915_sched_engine *sched_engine = ce_to_guc(ce)->sched_engine;
1713 	struct i915_request *rq;
1714 	unsigned long flags;
1715 
1716 	/* Mark all executing requests as skipped. */
1717 	spin_lock_irqsave(&sched_engine->lock, flags);
1718 	spin_lock(&ce->guc_state.lock);
1719 	list_for_each_entry(rq, &ce->guc_state.requests, sched.link)
1720 		i915_request_put(i915_request_mark_eio(rq));
1721 	spin_unlock(&ce->guc_state.lock);
1722 	spin_unlock_irqrestore(&sched_engine->lock, flags);
1723 }
1724 
1725 static void
1726 guc_cancel_sched_engine_requests(struct i915_sched_engine *sched_engine)
1727 {
1728 	struct i915_request *rq, *rn;
1729 	struct rb_node *rb;
1730 	unsigned long flags;
1731 
1732 	/* Can be called during boot if GuC fails to load */
1733 	if (!sched_engine)
1734 		return;
1735 
1736 	/*
1737 	 * Before we call engine->cancel_requests(), we should have exclusive
1738 	 * access to the submission state. This is arranged for us by the
1739 	 * caller disabling the interrupt generation, the tasklet and other
1740 	 * threads that may then access the same state, giving us a free hand
1741 	 * to reset state. However, we still need to let lockdep be aware that
1742 	 * we know this state may be accessed in hardirq context, so we
1743 	 * disable the irq around this manipulation and we want to keep
1744 	 * the spinlock focused on its duties and not accidentally conflate
1745 	 * coverage to the submission's irq state. (Similarly, although we
1746 	 * shouldn't need to disable irq around the manipulation of the
1747 	 * submission's irq state, we also wish to remind ourselves that
1748 	 * it is irq state.)
1749 	 */
1750 	spin_lock_irqsave(&sched_engine->lock, flags);
1751 
1752 	/* Flush the queued requests to the timeline list (for retiring). */
1753 	while ((rb = rb_first_cached(&sched_engine->queue))) {
1754 		struct i915_priolist *p = to_priolist(rb);
1755 
1756 		priolist_for_each_request_consume(rq, rn, p) {
1757 			list_del_init(&rq->sched.link);
1758 
1759 			__i915_request_submit(rq);
1760 
1761 			i915_request_put(i915_request_mark_eio(rq));
1762 		}
1763 
1764 		rb_erase_cached(&p->node, &sched_engine->queue);
1765 		i915_priolist_free(p);
1766 	}
1767 
1768 	/* Remaining _unready_ requests will be nop'ed when submitted */
1769 
1770 	sched_engine->queue_priority_hint = INT_MIN;
1771 	sched_engine->queue = RB_ROOT_CACHED;
1772 
1773 	spin_unlock_irqrestore(&sched_engine->lock, flags);
1774 }
1775 
1776 void intel_guc_submission_cancel_requests(struct intel_guc *guc)
1777 {
1778 	struct intel_context *ce;
1779 	unsigned long index;
1780 	unsigned long flags;
1781 
1782 	xa_lock_irqsave(&guc->context_lookup, flags);
1783 	xa_for_each(&guc->context_lookup, index, ce) {
1784 		if (!kref_get_unless_zero(&ce->ref))
1785 			continue;
1786 
1787 		xa_unlock(&guc->context_lookup);
1788 
1789 		if (intel_context_is_pinned(ce) &&
1790 		    !intel_context_is_child(ce))
1791 			guc_cancel_context_requests(ce);
1792 
1793 		intel_context_put(ce);
1794 
1795 		xa_lock(&guc->context_lookup);
1796 	}
1797 	xa_unlock_irqrestore(&guc->context_lookup, flags);
1798 
1799 	guc_cancel_sched_engine_requests(guc->sched_engine);
1800 
1801 	/* GuC is blown away, drop all references to contexts */
1802 	xa_destroy(&guc->context_lookup);
1803 }
1804 
1805 void intel_guc_submission_reset_finish(struct intel_guc *guc)
1806 {
1807 	/* Reset called during driver load or during wedge? */
1808 	if (unlikely(!guc_submission_initialized(guc) ||
1809 		     test_bit(I915_WEDGED, &guc_to_gt(guc)->reset.flags))) {
1810 		return;
1811 	}
1812 
1813 	/*
1814 	 * Technically possible for either of these values to be non-zero here,
1815 	 * but very unlikely + harmless. Regardless let's add a warn so we can
1816 	 * see in CI if this happens frequently / a precursor to taking down the
1817 	 * machine.
1818 	 */
1819 	GEM_WARN_ON(atomic_read(&guc->outstanding_submission_g2h));
1820 	atomic_set(&guc->outstanding_submission_g2h, 0);
1821 
1822 	intel_guc_global_policies_update(guc);
1823 	enable_submission(guc);
1824 	intel_gt_unpark_heartbeats(guc_to_gt(guc));
1825 }
1826 
1827 static void destroyed_worker_func(struct work_struct *w);
1828 
1829 /*
1830  * Set up the memory resources to be shared with the GuC (via the GGTT)
1831  * at firmware loading time.
1832  */
1833 int intel_guc_submission_init(struct intel_guc *guc)
1834 {
1835 	struct intel_gt *gt = guc_to_gt(guc);
1836 	int ret;
1837 
1838 	if (guc->lrc_desc_pool)
1839 		return 0;
1840 
1841 	ret = guc_lrc_desc_pool_create(guc);
1842 	if (ret)
1843 		return ret;
1844 	/*
1845 	 * Keep static analysers happy, let them know that we allocated the
1846 	 * vma after testing that it didn't exist earlier.
1847 	 */
1848 	GEM_BUG_ON(!guc->lrc_desc_pool);
1849 
1850 	xa_init_flags(&guc->context_lookup, XA_FLAGS_LOCK_IRQ);
1851 
1852 	spin_lock_init(&guc->submission_state.lock);
1853 	INIT_LIST_HEAD(&guc->submission_state.guc_id_list);
1854 	ida_init(&guc->submission_state.guc_ids);
1855 	INIT_LIST_HEAD(&guc->submission_state.destroyed_contexts);
1856 	INIT_WORK(&guc->submission_state.destroyed_worker,
1857 		  destroyed_worker_func);
1858 
1859 	guc->submission_state.guc_ids_bitmap =
1860 		bitmap_zalloc(NUMBER_MULTI_LRC_GUC_ID(guc), GFP_KERNEL);
1861 	if (!guc->submission_state.guc_ids_bitmap)
1862 		return -ENOMEM;
1863 
1864 	spin_lock_init(&guc->timestamp.lock);
1865 	INIT_DELAYED_WORK(&guc->timestamp.work, guc_timestamp_ping);
1866 	guc->timestamp.ping_delay = (POLL_TIME_CLKS / gt->clock_frequency + 1) * HZ;
1867 	guc->timestamp.shift = gpm_timestamp_shift(gt);
1868 
1869 	return 0;
1870 }
1871 
1872 void intel_guc_submission_fini(struct intel_guc *guc)
1873 {
1874 	if (!guc->lrc_desc_pool)
1875 		return;
1876 
1877 	guc_flush_destroyed_contexts(guc);
1878 	guc_lrc_desc_pool_destroy(guc);
1879 	i915_sched_engine_put(guc->sched_engine);
1880 	bitmap_free(guc->submission_state.guc_ids_bitmap);
1881 }
1882 
1883 static inline void queue_request(struct i915_sched_engine *sched_engine,
1884 				 struct i915_request *rq,
1885 				 int prio)
1886 {
1887 	GEM_BUG_ON(!list_empty(&rq->sched.link));
1888 	list_add_tail(&rq->sched.link,
1889 		      i915_sched_lookup_priolist(sched_engine, prio));
1890 	set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
1891 	tasklet_hi_schedule(&sched_engine->tasklet);
1892 }
1893 
1894 static int guc_bypass_tasklet_submit(struct intel_guc *guc,
1895 				     struct i915_request *rq)
1896 {
1897 	int ret = 0;
1898 
1899 	__i915_request_submit(rq);
1900 
1901 	trace_i915_request_in(rq, 0);
1902 
1903 	if (is_multi_lrc_rq(rq)) {
1904 		if (multi_lrc_submit(rq)) {
1905 			ret = guc_wq_item_append(guc, rq);
1906 			if (!ret)
1907 				ret = guc_add_request(guc, rq);
1908 		}
1909 	} else {
1910 		guc_set_lrc_tail(rq);
1911 		ret = guc_add_request(guc, rq);
1912 	}
1913 
1914 	if (unlikely(ret == -EPIPE))
1915 		disable_submission(guc);
1916 
1917 	return ret;
1918 }
1919 
1920 static bool need_tasklet(struct intel_guc *guc, struct i915_request *rq)
1921 {
1922 	struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
1923 	struct intel_context *ce = request_to_scheduling_context(rq);
1924 
1925 	return submission_disabled(guc) || guc->stalled_request ||
1926 		!i915_sched_engine_is_empty(sched_engine) ||
1927 		!lrc_desc_registered(guc, ce->guc_id.id);
1928 }
1929 
1930 static void guc_submit_request(struct i915_request *rq)
1931 {
1932 	struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
1933 	struct intel_guc *guc = &rq->engine->gt->uc.guc;
1934 	unsigned long flags;
1935 
1936 	/* Will be called from irq-context when using foreign fences. */
1937 	spin_lock_irqsave(&sched_engine->lock, flags);
1938 
1939 	if (need_tasklet(guc, rq))
1940 		queue_request(sched_engine, rq, rq_prio(rq));
1941 	else if (guc_bypass_tasklet_submit(guc, rq) == -EBUSY)
1942 		tasklet_hi_schedule(&sched_engine->tasklet);
1943 
1944 	spin_unlock_irqrestore(&sched_engine->lock, flags);
1945 }
1946 
1947 static int new_guc_id(struct intel_guc *guc, struct intel_context *ce)
1948 {
1949 	int ret;
1950 
1951 	GEM_BUG_ON(intel_context_is_child(ce));
1952 
1953 	if (intel_context_is_parent(ce))
1954 		ret = bitmap_find_free_region(guc->submission_state.guc_ids_bitmap,
1955 					      NUMBER_MULTI_LRC_GUC_ID(guc),
1956 					      order_base_2(ce->parallel.number_children
1957 							   + 1));
1958 	else
1959 		ret = ida_simple_get(&guc->submission_state.guc_ids,
1960 				     NUMBER_MULTI_LRC_GUC_ID(guc),
1961 				     guc->submission_state.num_guc_ids,
1962 				     GFP_KERNEL | __GFP_RETRY_MAYFAIL |
1963 				     __GFP_NOWARN);
1964 	if (unlikely(ret < 0))
1965 		return ret;
1966 
1967 	ce->guc_id.id = ret;
1968 	return 0;
1969 }
1970 
1971 static void __release_guc_id(struct intel_guc *guc, struct intel_context *ce)
1972 {
1973 	GEM_BUG_ON(intel_context_is_child(ce));
1974 
1975 	if (!context_guc_id_invalid(ce)) {
1976 		if (intel_context_is_parent(ce))
1977 			bitmap_release_region(guc->submission_state.guc_ids_bitmap,
1978 					      ce->guc_id.id,
1979 					      order_base_2(ce->parallel.number_children
1980 							   + 1));
1981 		else
1982 			ida_simple_remove(&guc->submission_state.guc_ids,
1983 					  ce->guc_id.id);
1984 		reset_lrc_desc(guc, ce->guc_id.id);
1985 		set_context_guc_id_invalid(ce);
1986 	}
1987 	if (!list_empty(&ce->guc_id.link))
1988 		list_del_init(&ce->guc_id.link);
1989 }
1990 
1991 static void release_guc_id(struct intel_guc *guc, struct intel_context *ce)
1992 {
1993 	unsigned long flags;
1994 
1995 	spin_lock_irqsave(&guc->submission_state.lock, flags);
1996 	__release_guc_id(guc, ce);
1997 	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
1998 }
1999 
2000 static int steal_guc_id(struct intel_guc *guc, struct intel_context *ce)
2001 {
2002 	struct intel_context *cn;
2003 
2004 	lockdep_assert_held(&guc->submission_state.lock);
2005 	GEM_BUG_ON(intel_context_is_child(ce));
2006 	GEM_BUG_ON(intel_context_is_parent(ce));
2007 
2008 	if (!list_empty(&guc->submission_state.guc_id_list)) {
2009 		cn = list_first_entry(&guc->submission_state.guc_id_list,
2010 				      struct intel_context,
2011 				      guc_id.link);
2012 
2013 		GEM_BUG_ON(atomic_read(&cn->guc_id.ref));
2014 		GEM_BUG_ON(context_guc_id_invalid(cn));
2015 		GEM_BUG_ON(intel_context_is_child(cn));
2016 		GEM_BUG_ON(intel_context_is_parent(cn));
2017 
2018 		list_del_init(&cn->guc_id.link);
2019 		ce->guc_id.id = cn->guc_id.id;
2020 
2021 		spin_lock(&cn->guc_state.lock);
2022 		clr_context_registered(cn);
2023 		spin_unlock(&cn->guc_state.lock);
2024 
2025 		set_context_guc_id_invalid(cn);
2026 
2027 #ifdef CONFIG_DRM_I915_SELFTEST
2028 		guc->number_guc_id_stolen++;
2029 #endif
2030 
2031 		return 0;
2032 	} else {
2033 		return -EAGAIN;
2034 	}
2035 }
2036 
2037 static int assign_guc_id(struct intel_guc *guc, struct intel_context *ce)
2038 {
2039 	int ret;
2040 
2041 	lockdep_assert_held(&guc->submission_state.lock);
2042 	GEM_BUG_ON(intel_context_is_child(ce));
2043 
2044 	ret = new_guc_id(guc, ce);
2045 	if (unlikely(ret < 0)) {
2046 		if (intel_context_is_parent(ce))
2047 			return -ENOSPC;
2048 
2049 		ret = steal_guc_id(guc, ce);
2050 		if (ret < 0)
2051 			return ret;
2052 	}
2053 
2054 	if (intel_context_is_parent(ce)) {
2055 		struct intel_context *child;
2056 		int i = 1;
2057 
2058 		for_each_child(ce, child)
2059 			child->guc_id.id = ce->guc_id.id + i++;
2060 	}
2061 
2062 	return 0;
2063 }
2064 
2065 #define PIN_GUC_ID_TRIES	4
2066 static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
2067 {
2068 	int ret = 0;
2069 	unsigned long flags, tries = PIN_GUC_ID_TRIES;
2070 
2071 	GEM_BUG_ON(atomic_read(&ce->guc_id.ref));
2072 
2073 try_again:
2074 	spin_lock_irqsave(&guc->submission_state.lock, flags);
2075 
2076 	might_lock(&ce->guc_state.lock);
2077 
2078 	if (context_guc_id_invalid(ce)) {
2079 		ret = assign_guc_id(guc, ce);
2080 		if (ret)
2081 			goto out_unlock;
2082 		ret = 1;	/* Indidcates newly assigned guc_id */
2083 	}
2084 	if (!list_empty(&ce->guc_id.link))
2085 		list_del_init(&ce->guc_id.link);
2086 	atomic_inc(&ce->guc_id.ref);
2087 
2088 out_unlock:
2089 	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
2090 
2091 	/*
2092 	 * -EAGAIN indicates no guc_id are available, let's retire any
2093 	 * outstanding requests to see if that frees up a guc_id. If the first
2094 	 * retire didn't help, insert a sleep with the timeslice duration before
2095 	 * attempting to retire more requests. Double the sleep period each
2096 	 * subsequent pass before finally giving up. The sleep period has max of
2097 	 * 100ms and minimum of 1ms.
2098 	 */
2099 	if (ret == -EAGAIN && --tries) {
2100 		if (PIN_GUC_ID_TRIES - tries > 1) {
2101 			unsigned int timeslice_shifted =
2102 				ce->engine->props.timeslice_duration_ms <<
2103 				(PIN_GUC_ID_TRIES - tries - 2);
2104 			unsigned int max = min_t(unsigned int, 100,
2105 						 timeslice_shifted);
2106 
2107 			msleep(max_t(unsigned int, max, 1));
2108 		}
2109 		intel_gt_retire_requests(guc_to_gt(guc));
2110 		goto try_again;
2111 	}
2112 
2113 	return ret;
2114 }
2115 
2116 static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
2117 {
2118 	unsigned long flags;
2119 
2120 	GEM_BUG_ON(atomic_read(&ce->guc_id.ref) < 0);
2121 	GEM_BUG_ON(intel_context_is_child(ce));
2122 
2123 	if (unlikely(context_guc_id_invalid(ce) ||
2124 		     intel_context_is_parent(ce)))
2125 		return;
2126 
2127 	spin_lock_irqsave(&guc->submission_state.lock, flags);
2128 	if (!context_guc_id_invalid(ce) && list_empty(&ce->guc_id.link) &&
2129 	    !atomic_read(&ce->guc_id.ref))
2130 		list_add_tail(&ce->guc_id.link,
2131 			      &guc->submission_state.guc_id_list);
2132 	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
2133 }
2134 
2135 static int __guc_action_register_multi_lrc(struct intel_guc *guc,
2136 					   struct intel_context *ce,
2137 					   u32 guc_id,
2138 					   u32 offset,
2139 					   bool loop)
2140 {
2141 	struct intel_context *child;
2142 	u32 action[4 + MAX_ENGINE_INSTANCE];
2143 	int len = 0;
2144 
2145 	GEM_BUG_ON(ce->parallel.number_children > MAX_ENGINE_INSTANCE);
2146 
2147 	action[len++] = INTEL_GUC_ACTION_REGISTER_CONTEXT_MULTI_LRC;
2148 	action[len++] = guc_id;
2149 	action[len++] = ce->parallel.number_children + 1;
2150 	action[len++] = offset;
2151 	for_each_child(ce, child) {
2152 		offset += sizeof(struct guc_lrc_desc);
2153 		action[len++] = offset;
2154 	}
2155 
2156 	return guc_submission_send_busy_loop(guc, action, len, 0, loop);
2157 }
2158 
2159 static int __guc_action_register_context(struct intel_guc *guc,
2160 					 u32 guc_id,
2161 					 u32 offset,
2162 					 bool loop)
2163 {
2164 	u32 action[] = {
2165 		INTEL_GUC_ACTION_REGISTER_CONTEXT,
2166 		guc_id,
2167 		offset,
2168 	};
2169 
2170 	return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
2171 					     0, loop);
2172 }
2173 
2174 static int register_context(struct intel_context *ce, bool loop)
2175 {
2176 	struct intel_guc *guc = ce_to_guc(ce);
2177 	u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool) +
2178 		ce->guc_id.id * sizeof(struct guc_lrc_desc);
2179 	int ret;
2180 
2181 	GEM_BUG_ON(intel_context_is_child(ce));
2182 	trace_intel_context_register(ce);
2183 
2184 	if (intel_context_is_parent(ce))
2185 		ret = __guc_action_register_multi_lrc(guc, ce, ce->guc_id.id,
2186 						      offset, loop);
2187 	else
2188 		ret = __guc_action_register_context(guc, ce->guc_id.id, offset,
2189 						    loop);
2190 	if (likely(!ret)) {
2191 		unsigned long flags;
2192 
2193 		spin_lock_irqsave(&ce->guc_state.lock, flags);
2194 		set_context_registered(ce);
2195 		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2196 	}
2197 
2198 	return ret;
2199 }
2200 
2201 static int __guc_action_deregister_context(struct intel_guc *guc,
2202 					   u32 guc_id)
2203 {
2204 	u32 action[] = {
2205 		INTEL_GUC_ACTION_DEREGISTER_CONTEXT,
2206 		guc_id,
2207 	};
2208 
2209 	return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
2210 					     G2H_LEN_DW_DEREGISTER_CONTEXT,
2211 					     true);
2212 }
2213 
2214 static int deregister_context(struct intel_context *ce, u32 guc_id)
2215 {
2216 	struct intel_guc *guc = ce_to_guc(ce);
2217 
2218 	GEM_BUG_ON(intel_context_is_child(ce));
2219 	trace_intel_context_deregister(ce);
2220 
2221 	return __guc_action_deregister_context(guc, guc_id);
2222 }
2223 
2224 static inline void clear_children_join_go_memory(struct intel_context *ce)
2225 {
2226 	struct parent_scratch *ps = __get_parent_scratch(ce);
2227 	int i;
2228 
2229 	ps->go.semaphore = 0;
2230 	for (i = 0; i < ce->parallel.number_children + 1; ++i)
2231 		ps->join[i].semaphore = 0;
2232 }
2233 
2234 static inline u32 get_children_go_value(struct intel_context *ce)
2235 {
2236 	return __get_parent_scratch(ce)->go.semaphore;
2237 }
2238 
2239 static inline u32 get_children_join_value(struct intel_context *ce,
2240 					  u8 child_index)
2241 {
2242 	return __get_parent_scratch(ce)->join[child_index].semaphore;
2243 }
2244 
2245 static void guc_context_policy_init(struct intel_engine_cs *engine,
2246 				    struct guc_lrc_desc *desc)
2247 {
2248 	desc->policy_flags = 0;
2249 
2250 	if (engine->flags & I915_ENGINE_WANT_FORCED_PREEMPTION)
2251 		desc->policy_flags |= CONTEXT_POLICY_FLAG_PREEMPT_TO_IDLE;
2252 
2253 	/* NB: For both of these, zero means disabled. */
2254 	desc->execution_quantum = engine->props.timeslice_duration_ms * 1000;
2255 	desc->preemption_timeout = engine->props.preempt_timeout_ms * 1000;
2256 }
2257 
2258 static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
2259 {
2260 	struct intel_engine_cs *engine = ce->engine;
2261 	struct intel_runtime_pm *runtime_pm = engine->uncore->rpm;
2262 	struct intel_guc *guc = &engine->gt->uc.guc;
2263 	u32 desc_idx = ce->guc_id.id;
2264 	struct guc_lrc_desc *desc;
2265 	bool context_registered;
2266 	intel_wakeref_t wakeref;
2267 	struct intel_context *child;
2268 	int ret = 0;
2269 
2270 	GEM_BUG_ON(!engine->mask);
2271 	GEM_BUG_ON(!sched_state_is_init(ce));
2272 
2273 	/*
2274 	 * Ensure LRC + CT vmas are is same region as write barrier is done
2275 	 * based on CT vma region.
2276 	 */
2277 	GEM_BUG_ON(i915_gem_object_is_lmem(guc->ct.vma->obj) !=
2278 		   i915_gem_object_is_lmem(ce->ring->vma->obj));
2279 
2280 	context_registered = lrc_desc_registered(guc, desc_idx);
2281 
2282 	reset_lrc_desc(guc, desc_idx);
2283 	set_lrc_desc_registered(guc, desc_idx, ce);
2284 
2285 	desc = __get_lrc_desc(guc, desc_idx);
2286 	desc->engine_class = engine_class_to_guc_class(engine->class);
2287 	desc->engine_submit_mask = engine->logical_mask;
2288 	desc->hw_context_desc = ce->lrc.lrca;
2289 	desc->priority = ce->guc_state.prio;
2290 	desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
2291 	guc_context_policy_init(engine, desc);
2292 
2293 	/*
2294 	 * If context is a parent, we need to register a process descriptor
2295 	 * describing a work queue and register all child contexts.
2296 	 */
2297 	if (intel_context_is_parent(ce)) {
2298 		struct guc_process_desc *pdesc;
2299 
2300 		ce->parallel.guc.wqi_tail = 0;
2301 		ce->parallel.guc.wqi_head = 0;
2302 
2303 		desc->process_desc = i915_ggtt_offset(ce->state) +
2304 			__get_parent_scratch_offset(ce);
2305 		desc->wq_addr = i915_ggtt_offset(ce->state) +
2306 			__get_wq_offset(ce);
2307 		desc->wq_size = WQ_SIZE;
2308 
2309 		pdesc = __get_process_desc(ce);
2310 		memset(pdesc, 0, sizeof(*(pdesc)));
2311 		pdesc->stage_id = ce->guc_id.id;
2312 		pdesc->wq_base_addr = desc->wq_addr;
2313 		pdesc->wq_size_bytes = desc->wq_size;
2314 		pdesc->wq_status = WQ_STATUS_ACTIVE;
2315 
2316 		for_each_child(ce, child) {
2317 			desc = __get_lrc_desc(guc, child->guc_id.id);
2318 
2319 			desc->engine_class =
2320 				engine_class_to_guc_class(engine->class);
2321 			desc->hw_context_desc = child->lrc.lrca;
2322 			desc->priority = ce->guc_state.prio;
2323 			desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
2324 			guc_context_policy_init(engine, desc);
2325 		}
2326 
2327 		clear_children_join_go_memory(ce);
2328 	}
2329 
2330 	/*
2331 	 * The context_lookup xarray is used to determine if the hardware
2332 	 * context is currently registered. There are two cases in which it
2333 	 * could be registered either the guc_id has been stolen from another
2334 	 * context or the lrc descriptor address of this context has changed. In
2335 	 * either case the context needs to be deregistered with the GuC before
2336 	 * registering this context.
2337 	 */
2338 	if (context_registered) {
2339 		bool disabled;
2340 		unsigned long flags;
2341 
2342 		trace_intel_context_steal_guc_id(ce);
2343 		GEM_BUG_ON(!loop);
2344 
2345 		/* Seal race with Reset */
2346 		spin_lock_irqsave(&ce->guc_state.lock, flags);
2347 		disabled = submission_disabled(guc);
2348 		if (likely(!disabled)) {
2349 			set_context_wait_for_deregister_to_register(ce);
2350 			intel_context_get(ce);
2351 		}
2352 		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2353 		if (unlikely(disabled)) {
2354 			reset_lrc_desc(guc, desc_idx);
2355 			return 0;	/* Will get registered later */
2356 		}
2357 
2358 		/*
2359 		 * If stealing the guc_id, this ce has the same guc_id as the
2360 		 * context whose guc_id was stolen.
2361 		 */
2362 		with_intel_runtime_pm(runtime_pm, wakeref)
2363 			ret = deregister_context(ce, ce->guc_id.id);
2364 		if (unlikely(ret == -ENODEV))
2365 			ret = 0;	/* Will get registered later */
2366 	} else {
2367 		with_intel_runtime_pm(runtime_pm, wakeref)
2368 			ret = register_context(ce, loop);
2369 		if (unlikely(ret == -EBUSY)) {
2370 			reset_lrc_desc(guc, desc_idx);
2371 		} else if (unlikely(ret == -ENODEV)) {
2372 			reset_lrc_desc(guc, desc_idx);
2373 			ret = 0;	/* Will get registered later */
2374 		}
2375 	}
2376 
2377 	return ret;
2378 }
2379 
2380 static int __guc_context_pre_pin(struct intel_context *ce,
2381 				 struct intel_engine_cs *engine,
2382 				 struct i915_gem_ww_ctx *ww,
2383 				 void **vaddr)
2384 {
2385 	return lrc_pre_pin(ce, engine, ww, vaddr);
2386 }
2387 
2388 static int __guc_context_pin(struct intel_context *ce,
2389 			     struct intel_engine_cs *engine,
2390 			     void *vaddr)
2391 {
2392 	if (i915_ggtt_offset(ce->state) !=
2393 	    (ce->lrc.lrca & CTX_GTT_ADDRESS_MASK))
2394 		set_bit(CONTEXT_LRCA_DIRTY, &ce->flags);
2395 
2396 	/*
2397 	 * GuC context gets pinned in guc_request_alloc. See that function for
2398 	 * explaination of why.
2399 	 */
2400 
2401 	return lrc_pin(ce, engine, vaddr);
2402 }
2403 
2404 static int guc_context_pre_pin(struct intel_context *ce,
2405 			       struct i915_gem_ww_ctx *ww,
2406 			       void **vaddr)
2407 {
2408 	return __guc_context_pre_pin(ce, ce->engine, ww, vaddr);
2409 }
2410 
2411 static int guc_context_pin(struct intel_context *ce, void *vaddr)
2412 {
2413 	int ret = __guc_context_pin(ce, ce->engine, vaddr);
2414 
2415 	if (likely(!ret && !intel_context_is_barrier(ce)))
2416 		intel_engine_pm_get(ce->engine);
2417 
2418 	return ret;
2419 }
2420 
2421 static void guc_context_unpin(struct intel_context *ce)
2422 {
2423 	struct intel_guc *guc = ce_to_guc(ce);
2424 
2425 	unpin_guc_id(guc, ce);
2426 	lrc_unpin(ce);
2427 
2428 	if (likely(!intel_context_is_barrier(ce)))
2429 		intel_engine_pm_put_async(ce->engine);
2430 }
2431 
2432 static void guc_context_post_unpin(struct intel_context *ce)
2433 {
2434 	lrc_post_unpin(ce);
2435 }
2436 
2437 static void __guc_context_sched_enable(struct intel_guc *guc,
2438 				       struct intel_context *ce)
2439 {
2440 	u32 action[] = {
2441 		INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET,
2442 		ce->guc_id.id,
2443 		GUC_CONTEXT_ENABLE
2444 	};
2445 
2446 	trace_intel_context_sched_enable(ce);
2447 
2448 	guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
2449 				      G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
2450 }
2451 
2452 static void __guc_context_sched_disable(struct intel_guc *guc,
2453 					struct intel_context *ce,
2454 					u16 guc_id)
2455 {
2456 	u32 action[] = {
2457 		INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET,
2458 		guc_id,	/* ce->guc_id.id not stable */
2459 		GUC_CONTEXT_DISABLE
2460 	};
2461 
2462 	GEM_BUG_ON(guc_id == GUC_INVALID_LRC_ID);
2463 
2464 	GEM_BUG_ON(intel_context_is_child(ce));
2465 	trace_intel_context_sched_disable(ce);
2466 
2467 	guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
2468 				      G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
2469 }
2470 
2471 static void guc_blocked_fence_complete(struct intel_context *ce)
2472 {
2473 	lockdep_assert_held(&ce->guc_state.lock);
2474 
2475 	if (!i915_sw_fence_done(&ce->guc_state.blocked))
2476 		i915_sw_fence_complete(&ce->guc_state.blocked);
2477 }
2478 
2479 static void guc_blocked_fence_reinit(struct intel_context *ce)
2480 {
2481 	lockdep_assert_held(&ce->guc_state.lock);
2482 	GEM_BUG_ON(!i915_sw_fence_done(&ce->guc_state.blocked));
2483 
2484 	/*
2485 	 * This fence is always complete unless a pending schedule disable is
2486 	 * outstanding. We arm the fence here and complete it when we receive
2487 	 * the pending schedule disable complete message.
2488 	 */
2489 	i915_sw_fence_fini(&ce->guc_state.blocked);
2490 	i915_sw_fence_reinit(&ce->guc_state.blocked);
2491 	i915_sw_fence_await(&ce->guc_state.blocked);
2492 	i915_sw_fence_commit(&ce->guc_state.blocked);
2493 }
2494 
2495 static u16 prep_context_pending_disable(struct intel_context *ce)
2496 {
2497 	lockdep_assert_held(&ce->guc_state.lock);
2498 
2499 	set_context_pending_disable(ce);
2500 	clr_context_enabled(ce);
2501 	guc_blocked_fence_reinit(ce);
2502 	intel_context_get(ce);
2503 
2504 	return ce->guc_id.id;
2505 }
2506 
2507 static struct i915_sw_fence *guc_context_block(struct intel_context *ce)
2508 {
2509 	struct intel_guc *guc = ce_to_guc(ce);
2510 	unsigned long flags;
2511 	struct intel_runtime_pm *runtime_pm = ce->engine->uncore->rpm;
2512 	intel_wakeref_t wakeref;
2513 	u16 guc_id;
2514 	bool enabled;
2515 
2516 	GEM_BUG_ON(intel_context_is_child(ce));
2517 
2518 	spin_lock_irqsave(&ce->guc_state.lock, flags);
2519 
2520 	incr_context_blocked(ce);
2521 
2522 	enabled = context_enabled(ce);
2523 	if (unlikely(!enabled || submission_disabled(guc))) {
2524 		if (enabled)
2525 			clr_context_enabled(ce);
2526 		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2527 		return &ce->guc_state.blocked;
2528 	}
2529 
2530 	/*
2531 	 * We add +2 here as the schedule disable complete CTB handler calls
2532 	 * intel_context_sched_disable_unpin (-2 to pin_count).
2533 	 */
2534 	atomic_add(2, &ce->pin_count);
2535 
2536 	guc_id = prep_context_pending_disable(ce);
2537 
2538 	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2539 
2540 	with_intel_runtime_pm(runtime_pm, wakeref)
2541 		__guc_context_sched_disable(guc, ce, guc_id);
2542 
2543 	return &ce->guc_state.blocked;
2544 }
2545 
2546 #define SCHED_STATE_MULTI_BLOCKED_MASK \
2547 	(SCHED_STATE_BLOCKED_MASK & ~SCHED_STATE_BLOCKED)
2548 #define SCHED_STATE_NO_UNBLOCK \
2549 	(SCHED_STATE_MULTI_BLOCKED_MASK | \
2550 	 SCHED_STATE_PENDING_DISABLE | \
2551 	 SCHED_STATE_BANNED)
2552 
2553 static bool context_cant_unblock(struct intel_context *ce)
2554 {
2555 	lockdep_assert_held(&ce->guc_state.lock);
2556 
2557 	return (ce->guc_state.sched_state & SCHED_STATE_NO_UNBLOCK) ||
2558 		context_guc_id_invalid(ce) ||
2559 		!lrc_desc_registered(ce_to_guc(ce), ce->guc_id.id) ||
2560 		!intel_context_is_pinned(ce);
2561 }
2562 
2563 static void guc_context_unblock(struct intel_context *ce)
2564 {
2565 	struct intel_guc *guc = ce_to_guc(ce);
2566 	unsigned long flags;
2567 	struct intel_runtime_pm *runtime_pm = ce->engine->uncore->rpm;
2568 	intel_wakeref_t wakeref;
2569 	bool enable;
2570 
2571 	GEM_BUG_ON(context_enabled(ce));
2572 	GEM_BUG_ON(intel_context_is_child(ce));
2573 
2574 	spin_lock_irqsave(&ce->guc_state.lock, flags);
2575 
2576 	if (unlikely(submission_disabled(guc) ||
2577 		     context_cant_unblock(ce))) {
2578 		enable = false;
2579 	} else {
2580 		enable = true;
2581 		set_context_pending_enable(ce);
2582 		set_context_enabled(ce);
2583 		intel_context_get(ce);
2584 	}
2585 
2586 	decr_context_blocked(ce);
2587 
2588 	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2589 
2590 	if (enable) {
2591 		with_intel_runtime_pm(runtime_pm, wakeref)
2592 			__guc_context_sched_enable(guc, ce);
2593 	}
2594 }
2595 
2596 static void guc_context_cancel_request(struct intel_context *ce,
2597 				       struct i915_request *rq)
2598 {
2599 	struct intel_context *block_context =
2600 		request_to_scheduling_context(rq);
2601 
2602 	if (i915_sw_fence_signaled(&rq->submit)) {
2603 		struct i915_sw_fence *fence;
2604 
2605 		intel_context_get(ce);
2606 		fence = guc_context_block(block_context);
2607 		i915_sw_fence_wait(fence);
2608 		if (!i915_request_completed(rq)) {
2609 			__i915_request_skip(rq);
2610 			guc_reset_state(ce, intel_ring_wrap(ce->ring, rq->head),
2611 					true);
2612 		}
2613 
2614 		/*
2615 		 * XXX: Racey if context is reset, see comment in
2616 		 * __guc_reset_context().
2617 		 */
2618 		flush_work(&ce_to_guc(ce)->ct.requests.worker);
2619 
2620 		guc_context_unblock(block_context);
2621 		intel_context_put(ce);
2622 	}
2623 }
2624 
2625 static void __guc_context_set_preemption_timeout(struct intel_guc *guc,
2626 						 u16 guc_id,
2627 						 u32 preemption_timeout)
2628 {
2629 	u32 action[] = {
2630 		INTEL_GUC_ACTION_SET_CONTEXT_PREEMPTION_TIMEOUT,
2631 		guc_id,
2632 		preemption_timeout
2633 	};
2634 
2635 	intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);
2636 }
2637 
2638 static void guc_context_ban(struct intel_context *ce, struct i915_request *rq)
2639 {
2640 	struct intel_guc *guc = ce_to_guc(ce);
2641 	struct intel_runtime_pm *runtime_pm =
2642 		&ce->engine->gt->i915->runtime_pm;
2643 	intel_wakeref_t wakeref;
2644 	unsigned long flags;
2645 
2646 	GEM_BUG_ON(intel_context_is_child(ce));
2647 
2648 	guc_flush_submissions(guc);
2649 
2650 	spin_lock_irqsave(&ce->guc_state.lock, flags);
2651 	set_context_banned(ce);
2652 
2653 	if (submission_disabled(guc) ||
2654 	    (!context_enabled(ce) && !context_pending_disable(ce))) {
2655 		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2656 
2657 		guc_cancel_context_requests(ce);
2658 		intel_engine_signal_breadcrumbs(ce->engine);
2659 	} else if (!context_pending_disable(ce)) {
2660 		u16 guc_id;
2661 
2662 		/*
2663 		 * We add +2 here as the schedule disable complete CTB handler
2664 		 * calls intel_context_sched_disable_unpin (-2 to pin_count).
2665 		 */
2666 		atomic_add(2, &ce->pin_count);
2667 
2668 		guc_id = prep_context_pending_disable(ce);
2669 		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2670 
2671 		/*
2672 		 * In addition to disabling scheduling, set the preemption
2673 		 * timeout to the minimum value (1 us) so the banned context
2674 		 * gets kicked off the HW ASAP.
2675 		 */
2676 		with_intel_runtime_pm(runtime_pm, wakeref) {
2677 			__guc_context_set_preemption_timeout(guc, guc_id, 1);
2678 			__guc_context_sched_disable(guc, ce, guc_id);
2679 		}
2680 	} else {
2681 		if (!context_guc_id_invalid(ce))
2682 			with_intel_runtime_pm(runtime_pm, wakeref)
2683 				__guc_context_set_preemption_timeout(guc,
2684 								     ce->guc_id.id,
2685 								     1);
2686 		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2687 	}
2688 }
2689 
2690 static void guc_context_sched_disable(struct intel_context *ce)
2691 {
2692 	struct intel_guc *guc = ce_to_guc(ce);
2693 	unsigned long flags;
2694 	struct intel_runtime_pm *runtime_pm = &ce->engine->gt->i915->runtime_pm;
2695 	intel_wakeref_t wakeref;
2696 	u16 guc_id;
2697 
2698 	GEM_BUG_ON(intel_context_is_child(ce));
2699 
2700 	spin_lock_irqsave(&ce->guc_state.lock, flags);
2701 
2702 	/*
2703 	 * We have to check if the context has been disabled by another thread,
2704 	 * check if submssion has been disabled to seal a race with reset and
2705 	 * finally check if any more requests have been committed to the
2706 	 * context ensursing that a request doesn't slip through the
2707 	 * 'context_pending_disable' fence.
2708 	 */
2709 	if (unlikely(!context_enabled(ce) || submission_disabled(guc) ||
2710 		     context_has_committed_requests(ce))) {
2711 		clr_context_enabled(ce);
2712 		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2713 		goto unpin;
2714 	}
2715 	guc_id = prep_context_pending_disable(ce);
2716 
2717 	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2718 
2719 	with_intel_runtime_pm(runtime_pm, wakeref)
2720 		__guc_context_sched_disable(guc, ce, guc_id);
2721 
2722 	return;
2723 unpin:
2724 	intel_context_sched_disable_unpin(ce);
2725 }
2726 
2727 static inline void guc_lrc_desc_unpin(struct intel_context *ce)
2728 {
2729 	struct intel_guc *guc = ce_to_guc(ce);
2730 	struct intel_gt *gt = guc_to_gt(guc);
2731 	unsigned long flags;
2732 	bool disabled;
2733 
2734 	GEM_BUG_ON(!intel_gt_pm_is_awake(gt));
2735 	GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id.id));
2736 	GEM_BUG_ON(ce != __get_context(guc, ce->guc_id.id));
2737 	GEM_BUG_ON(context_enabled(ce));
2738 
2739 	/* Seal race with Reset */
2740 	spin_lock_irqsave(&ce->guc_state.lock, flags);
2741 	disabled = submission_disabled(guc);
2742 	if (likely(!disabled)) {
2743 		__intel_gt_pm_get(gt);
2744 		set_context_destroyed(ce);
2745 		clr_context_registered(ce);
2746 	}
2747 	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2748 	if (unlikely(disabled)) {
2749 		release_guc_id(guc, ce);
2750 		__guc_context_destroy(ce);
2751 		return;
2752 	}
2753 
2754 	deregister_context(ce, ce->guc_id.id);
2755 }
2756 
2757 static void __guc_context_destroy(struct intel_context *ce)
2758 {
2759 	GEM_BUG_ON(ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_KMD_HIGH] ||
2760 		   ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_HIGH] ||
2761 		   ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_KMD_NORMAL] ||
2762 		   ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_NORMAL]);
2763 	GEM_BUG_ON(ce->guc_state.number_committed_requests);
2764 
2765 	lrc_fini(ce);
2766 	intel_context_fini(ce);
2767 
2768 	if (intel_engine_is_virtual(ce->engine)) {
2769 		struct guc_virtual_engine *ve =
2770 			container_of(ce, typeof(*ve), context);
2771 
2772 		if (ve->base.breadcrumbs)
2773 			intel_breadcrumbs_put(ve->base.breadcrumbs);
2774 
2775 		kfree(ve);
2776 	} else {
2777 		intel_context_free(ce);
2778 	}
2779 }
2780 
2781 static void guc_flush_destroyed_contexts(struct intel_guc *guc)
2782 {
2783 	struct intel_context *ce;
2784 	unsigned long flags;
2785 
2786 	GEM_BUG_ON(!submission_disabled(guc) &&
2787 		   guc_submission_initialized(guc));
2788 
2789 	while (!list_empty(&guc->submission_state.destroyed_contexts)) {
2790 		spin_lock_irqsave(&guc->submission_state.lock, flags);
2791 		ce = list_first_entry_or_null(&guc->submission_state.destroyed_contexts,
2792 					      struct intel_context,
2793 					      destroyed_link);
2794 		if (ce)
2795 			list_del_init(&ce->destroyed_link);
2796 		spin_unlock_irqrestore(&guc->submission_state.lock, flags);
2797 
2798 		if (!ce)
2799 			break;
2800 
2801 		release_guc_id(guc, ce);
2802 		__guc_context_destroy(ce);
2803 	}
2804 }
2805 
2806 static void deregister_destroyed_contexts(struct intel_guc *guc)
2807 {
2808 	struct intel_context *ce;
2809 	unsigned long flags;
2810 
2811 	while (!list_empty(&guc->submission_state.destroyed_contexts)) {
2812 		spin_lock_irqsave(&guc->submission_state.lock, flags);
2813 		ce = list_first_entry_or_null(&guc->submission_state.destroyed_contexts,
2814 					      struct intel_context,
2815 					      destroyed_link);
2816 		if (ce)
2817 			list_del_init(&ce->destroyed_link);
2818 		spin_unlock_irqrestore(&guc->submission_state.lock, flags);
2819 
2820 		if (!ce)
2821 			break;
2822 
2823 		guc_lrc_desc_unpin(ce);
2824 	}
2825 }
2826 
2827 static void destroyed_worker_func(struct work_struct *w)
2828 {
2829 	struct intel_guc *guc = container_of(w, struct intel_guc,
2830 					     submission_state.destroyed_worker);
2831 	struct intel_gt *gt = guc_to_gt(guc);
2832 	int tmp;
2833 
2834 	with_intel_gt_pm(gt, tmp)
2835 		deregister_destroyed_contexts(guc);
2836 }
2837 
2838 static void guc_context_destroy(struct kref *kref)
2839 {
2840 	struct intel_context *ce = container_of(kref, typeof(*ce), ref);
2841 	struct intel_guc *guc = ce_to_guc(ce);
2842 	unsigned long flags;
2843 	bool destroy;
2844 
2845 	/*
2846 	 * If the guc_id is invalid this context has been stolen and we can free
2847 	 * it immediately. Also can be freed immediately if the context is not
2848 	 * registered with the GuC or the GuC is in the middle of a reset.
2849 	 */
2850 	spin_lock_irqsave(&guc->submission_state.lock, flags);
2851 	destroy = submission_disabled(guc) || context_guc_id_invalid(ce) ||
2852 		!lrc_desc_registered(guc, ce->guc_id.id);
2853 	if (likely(!destroy)) {
2854 		if (!list_empty(&ce->guc_id.link))
2855 			list_del_init(&ce->guc_id.link);
2856 		list_add_tail(&ce->destroyed_link,
2857 			      &guc->submission_state.destroyed_contexts);
2858 	} else {
2859 		__release_guc_id(guc, ce);
2860 	}
2861 	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
2862 	if (unlikely(destroy)) {
2863 		__guc_context_destroy(ce);
2864 		return;
2865 	}
2866 
2867 	/*
2868 	 * We use a worker to issue the H2G to deregister the context as we can
2869 	 * take the GT PM for the first time which isn't allowed from an atomic
2870 	 * context.
2871 	 */
2872 	queue_work(system_unbound_wq, &guc->submission_state.destroyed_worker);
2873 }
2874 
2875 static int guc_context_alloc(struct intel_context *ce)
2876 {
2877 	return lrc_alloc(ce, ce->engine);
2878 }
2879 
2880 static void guc_context_set_prio(struct intel_guc *guc,
2881 				 struct intel_context *ce,
2882 				 u8 prio)
2883 {
2884 	u32 action[] = {
2885 		INTEL_GUC_ACTION_SET_CONTEXT_PRIORITY,
2886 		ce->guc_id.id,
2887 		prio,
2888 	};
2889 
2890 	GEM_BUG_ON(prio < GUC_CLIENT_PRIORITY_KMD_HIGH ||
2891 		   prio > GUC_CLIENT_PRIORITY_NORMAL);
2892 	lockdep_assert_held(&ce->guc_state.lock);
2893 
2894 	if (ce->guc_state.prio == prio || submission_disabled(guc) ||
2895 	    !context_registered(ce)) {
2896 		ce->guc_state.prio = prio;
2897 		return;
2898 	}
2899 
2900 	guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);
2901 
2902 	ce->guc_state.prio = prio;
2903 	trace_intel_context_set_prio(ce);
2904 }
2905 
2906 static inline u8 map_i915_prio_to_guc_prio(int prio)
2907 {
2908 	if (prio == I915_PRIORITY_NORMAL)
2909 		return GUC_CLIENT_PRIORITY_KMD_NORMAL;
2910 	else if (prio < I915_PRIORITY_NORMAL)
2911 		return GUC_CLIENT_PRIORITY_NORMAL;
2912 	else if (prio < I915_PRIORITY_DISPLAY)
2913 		return GUC_CLIENT_PRIORITY_HIGH;
2914 	else
2915 		return GUC_CLIENT_PRIORITY_KMD_HIGH;
2916 }
2917 
2918 static inline void add_context_inflight_prio(struct intel_context *ce,
2919 					     u8 guc_prio)
2920 {
2921 	lockdep_assert_held(&ce->guc_state.lock);
2922 	GEM_BUG_ON(guc_prio >= ARRAY_SIZE(ce->guc_state.prio_count));
2923 
2924 	++ce->guc_state.prio_count[guc_prio];
2925 
2926 	/* Overflow protection */
2927 	GEM_WARN_ON(!ce->guc_state.prio_count[guc_prio]);
2928 }
2929 
2930 static inline void sub_context_inflight_prio(struct intel_context *ce,
2931 					     u8 guc_prio)
2932 {
2933 	lockdep_assert_held(&ce->guc_state.lock);
2934 	GEM_BUG_ON(guc_prio >= ARRAY_SIZE(ce->guc_state.prio_count));
2935 
2936 	/* Underflow protection */
2937 	GEM_WARN_ON(!ce->guc_state.prio_count[guc_prio]);
2938 
2939 	--ce->guc_state.prio_count[guc_prio];
2940 }
2941 
2942 static inline void update_context_prio(struct intel_context *ce)
2943 {
2944 	struct intel_guc *guc = &ce->engine->gt->uc.guc;
2945 	int i;
2946 
2947 	BUILD_BUG_ON(GUC_CLIENT_PRIORITY_KMD_HIGH != 0);
2948 	BUILD_BUG_ON(GUC_CLIENT_PRIORITY_KMD_HIGH > GUC_CLIENT_PRIORITY_NORMAL);
2949 
2950 	lockdep_assert_held(&ce->guc_state.lock);
2951 
2952 	for (i = 0; i < ARRAY_SIZE(ce->guc_state.prio_count); ++i) {
2953 		if (ce->guc_state.prio_count[i]) {
2954 			guc_context_set_prio(guc, ce, i);
2955 			break;
2956 		}
2957 	}
2958 }
2959 
2960 static inline bool new_guc_prio_higher(u8 old_guc_prio, u8 new_guc_prio)
2961 {
2962 	/* Lower value is higher priority */
2963 	return new_guc_prio < old_guc_prio;
2964 }
2965 
2966 static void add_to_context(struct i915_request *rq)
2967 {
2968 	struct intel_context *ce = request_to_scheduling_context(rq);
2969 	u8 new_guc_prio = map_i915_prio_to_guc_prio(rq_prio(rq));
2970 
2971 	GEM_BUG_ON(intel_context_is_child(ce));
2972 	GEM_BUG_ON(rq->guc_prio == GUC_PRIO_FINI);
2973 
2974 	spin_lock(&ce->guc_state.lock);
2975 	list_move_tail(&rq->sched.link, &ce->guc_state.requests);
2976 
2977 	if (rq->guc_prio == GUC_PRIO_INIT) {
2978 		rq->guc_prio = new_guc_prio;
2979 		add_context_inflight_prio(ce, rq->guc_prio);
2980 	} else if (new_guc_prio_higher(rq->guc_prio, new_guc_prio)) {
2981 		sub_context_inflight_prio(ce, rq->guc_prio);
2982 		rq->guc_prio = new_guc_prio;
2983 		add_context_inflight_prio(ce, rq->guc_prio);
2984 	}
2985 	update_context_prio(ce);
2986 
2987 	spin_unlock(&ce->guc_state.lock);
2988 }
2989 
2990 static void guc_prio_fini(struct i915_request *rq, struct intel_context *ce)
2991 {
2992 	lockdep_assert_held(&ce->guc_state.lock);
2993 
2994 	if (rq->guc_prio != GUC_PRIO_INIT &&
2995 	    rq->guc_prio != GUC_PRIO_FINI) {
2996 		sub_context_inflight_prio(ce, rq->guc_prio);
2997 		update_context_prio(ce);
2998 	}
2999 	rq->guc_prio = GUC_PRIO_FINI;
3000 }
3001 
3002 static void remove_from_context(struct i915_request *rq)
3003 {
3004 	struct intel_context *ce = request_to_scheduling_context(rq);
3005 
3006 	GEM_BUG_ON(intel_context_is_child(ce));
3007 
3008 	spin_lock_irq(&ce->guc_state.lock);
3009 
3010 	list_del_init(&rq->sched.link);
3011 	clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
3012 
3013 	/* Prevent further __await_execution() registering a cb, then flush */
3014 	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
3015 
3016 	guc_prio_fini(rq, ce);
3017 
3018 	decr_context_committed_requests(ce);
3019 
3020 	spin_unlock_irq(&ce->guc_state.lock);
3021 
3022 	atomic_dec(&ce->guc_id.ref);
3023 	i915_request_notify_execute_cb_imm(rq);
3024 }
3025 
3026 static const struct intel_context_ops guc_context_ops = {
3027 	.alloc = guc_context_alloc,
3028 
3029 	.pre_pin = guc_context_pre_pin,
3030 	.pin = guc_context_pin,
3031 	.unpin = guc_context_unpin,
3032 	.post_unpin = guc_context_post_unpin,
3033 
3034 	.ban = guc_context_ban,
3035 
3036 	.cancel_request = guc_context_cancel_request,
3037 
3038 	.enter = intel_context_enter_engine,
3039 	.exit = intel_context_exit_engine,
3040 
3041 	.sched_disable = guc_context_sched_disable,
3042 
3043 	.reset = lrc_reset,
3044 	.destroy = guc_context_destroy,
3045 
3046 	.create_virtual = guc_create_virtual,
3047 	.create_parallel = guc_create_parallel,
3048 };
3049 
3050 static void submit_work_cb(struct irq_work *wrk)
3051 {
3052 	struct i915_request *rq = container_of(wrk, typeof(*rq), submit_work);
3053 
3054 	might_lock(&rq->engine->sched_engine->lock);
3055 	i915_sw_fence_complete(&rq->submit);
3056 }
3057 
3058 static void __guc_signal_context_fence(struct intel_context *ce)
3059 {
3060 	struct i915_request *rq, *rn;
3061 
3062 	lockdep_assert_held(&ce->guc_state.lock);
3063 
3064 	if (!list_empty(&ce->guc_state.fences))
3065 		trace_intel_context_fence_release(ce);
3066 
3067 	/*
3068 	 * Use an IRQ to ensure locking order of sched_engine->lock ->
3069 	 * ce->guc_state.lock is preserved.
3070 	 */
3071 	list_for_each_entry_safe(rq, rn, &ce->guc_state.fences,
3072 				 guc_fence_link) {
3073 		list_del(&rq->guc_fence_link);
3074 		irq_work_queue(&rq->submit_work);
3075 	}
3076 
3077 	INIT_LIST_HEAD(&ce->guc_state.fences);
3078 }
3079 
3080 static void guc_signal_context_fence(struct intel_context *ce)
3081 {
3082 	unsigned long flags;
3083 
3084 	GEM_BUG_ON(intel_context_is_child(ce));
3085 
3086 	spin_lock_irqsave(&ce->guc_state.lock, flags);
3087 	clr_context_wait_for_deregister_to_register(ce);
3088 	__guc_signal_context_fence(ce);
3089 	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
3090 }
3091 
3092 static bool context_needs_register(struct intel_context *ce, bool new_guc_id)
3093 {
3094 	return (new_guc_id || test_bit(CONTEXT_LRCA_DIRTY, &ce->flags) ||
3095 		!lrc_desc_registered(ce_to_guc(ce), ce->guc_id.id)) &&
3096 		!submission_disabled(ce_to_guc(ce));
3097 }
3098 
3099 static void guc_context_init(struct intel_context *ce)
3100 {
3101 	const struct i915_gem_context *ctx;
3102 	int prio = I915_CONTEXT_DEFAULT_PRIORITY;
3103 
3104 	rcu_read_lock();
3105 	ctx = rcu_dereference(ce->gem_context);
3106 	if (ctx)
3107 		prio = ctx->sched.priority;
3108 	rcu_read_unlock();
3109 
3110 	ce->guc_state.prio = map_i915_prio_to_guc_prio(prio);
3111 	set_bit(CONTEXT_GUC_INIT, &ce->flags);
3112 }
3113 
3114 static int guc_request_alloc(struct i915_request *rq)
3115 {
3116 	struct intel_context *ce = request_to_scheduling_context(rq);
3117 	struct intel_guc *guc = ce_to_guc(ce);
3118 	unsigned long flags;
3119 	int ret;
3120 
3121 	GEM_BUG_ON(!intel_context_is_pinned(rq->context));
3122 
3123 	/*
3124 	 * Flush enough space to reduce the likelihood of waiting after
3125 	 * we start building the request - in which case we will just
3126 	 * have to repeat work.
3127 	 */
3128 	rq->reserved_space += GUC_REQUEST_SIZE;
3129 
3130 	/*
3131 	 * Note that after this point, we have committed to using
3132 	 * this request as it is being used to both track the
3133 	 * state of engine initialisation and liveness of the
3134 	 * golden renderstate above. Think twice before you try
3135 	 * to cancel/unwind this request now.
3136 	 */
3137 
3138 	/* Unconditionally invalidate GPU caches and TLBs. */
3139 	ret = rq->engine->emit_flush(rq, EMIT_INVALIDATE);
3140 	if (ret)
3141 		return ret;
3142 
3143 	rq->reserved_space -= GUC_REQUEST_SIZE;
3144 
3145 	if (unlikely(!test_bit(CONTEXT_GUC_INIT, &ce->flags)))
3146 		guc_context_init(ce);
3147 
3148 	/*
3149 	 * Call pin_guc_id here rather than in the pinning step as with
3150 	 * dma_resv, contexts can be repeatedly pinned / unpinned trashing the
3151 	 * guc_id and creating horrible race conditions. This is especially bad
3152 	 * when guc_id are being stolen due to over subscription. By the time
3153 	 * this function is reached, it is guaranteed that the guc_id will be
3154 	 * persistent until the generated request is retired. Thus, sealing these
3155 	 * race conditions. It is still safe to fail here if guc_id are
3156 	 * exhausted and return -EAGAIN to the user indicating that they can try
3157 	 * again in the future.
3158 	 *
3159 	 * There is no need for a lock here as the timeline mutex ensures at
3160 	 * most one context can be executing this code path at once. The
3161 	 * guc_id_ref is incremented once for every request in flight and
3162 	 * decremented on each retire. When it is zero, a lock around the
3163 	 * increment (in pin_guc_id) is needed to seal a race with unpin_guc_id.
3164 	 */
3165 	if (atomic_add_unless(&ce->guc_id.ref, 1, 0))
3166 		goto out;
3167 
3168 	ret = pin_guc_id(guc, ce);	/* returns 1 if new guc_id assigned */
3169 	if (unlikely(ret < 0))
3170 		return ret;
3171 	if (context_needs_register(ce, !!ret)) {
3172 		ret = guc_lrc_desc_pin(ce, true);
3173 		if (unlikely(ret)) {	/* unwind */
3174 			if (ret == -EPIPE) {
3175 				disable_submission(guc);
3176 				goto out;	/* GPU will be reset */
3177 			}
3178 			atomic_dec(&ce->guc_id.ref);
3179 			unpin_guc_id(guc, ce);
3180 			return ret;
3181 		}
3182 	}
3183 
3184 	clear_bit(CONTEXT_LRCA_DIRTY, &ce->flags);
3185 
3186 out:
3187 	/*
3188 	 * We block all requests on this context if a G2H is pending for a
3189 	 * schedule disable or context deregistration as the GuC will fail a
3190 	 * schedule enable or context registration if either G2H is pending
3191 	 * respectfully. Once a G2H returns, the fence is released that is
3192 	 * blocking these requests (see guc_signal_context_fence).
3193 	 */
3194 	spin_lock_irqsave(&ce->guc_state.lock, flags);
3195 	if (context_wait_for_deregister_to_register(ce) ||
3196 	    context_pending_disable(ce)) {
3197 		init_irq_work(&rq->submit_work, submit_work_cb);
3198 		i915_sw_fence_await(&rq->submit);
3199 
3200 		list_add_tail(&rq->guc_fence_link, &ce->guc_state.fences);
3201 	}
3202 	incr_context_committed_requests(ce);
3203 	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
3204 
3205 	return 0;
3206 }
3207 
3208 static int guc_virtual_context_pre_pin(struct intel_context *ce,
3209 				       struct i915_gem_ww_ctx *ww,
3210 				       void **vaddr)
3211 {
3212 	struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
3213 
3214 	return __guc_context_pre_pin(ce, engine, ww, vaddr);
3215 }
3216 
3217 static int guc_virtual_context_pin(struct intel_context *ce, void *vaddr)
3218 {
3219 	struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
3220 	int ret = __guc_context_pin(ce, engine, vaddr);
3221 	intel_engine_mask_t tmp, mask = ce->engine->mask;
3222 
3223 	if (likely(!ret))
3224 		for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
3225 			intel_engine_pm_get(engine);
3226 
3227 	return ret;
3228 }
3229 
3230 static void guc_virtual_context_unpin(struct intel_context *ce)
3231 {
3232 	intel_engine_mask_t tmp, mask = ce->engine->mask;
3233 	struct intel_engine_cs *engine;
3234 	struct intel_guc *guc = ce_to_guc(ce);
3235 
3236 	GEM_BUG_ON(context_enabled(ce));
3237 	GEM_BUG_ON(intel_context_is_barrier(ce));
3238 
3239 	unpin_guc_id(guc, ce);
3240 	lrc_unpin(ce);
3241 
3242 	for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
3243 		intel_engine_pm_put_async(engine);
3244 }
3245 
3246 static void guc_virtual_context_enter(struct intel_context *ce)
3247 {
3248 	intel_engine_mask_t tmp, mask = ce->engine->mask;
3249 	struct intel_engine_cs *engine;
3250 
3251 	for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
3252 		intel_engine_pm_get(engine);
3253 
3254 	intel_timeline_enter(ce->timeline);
3255 }
3256 
3257 static void guc_virtual_context_exit(struct intel_context *ce)
3258 {
3259 	intel_engine_mask_t tmp, mask = ce->engine->mask;
3260 	struct intel_engine_cs *engine;
3261 
3262 	for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
3263 		intel_engine_pm_put(engine);
3264 
3265 	intel_timeline_exit(ce->timeline);
3266 }
3267 
3268 static int guc_virtual_context_alloc(struct intel_context *ce)
3269 {
3270 	struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
3271 
3272 	return lrc_alloc(ce, engine);
3273 }
3274 
3275 static const struct intel_context_ops virtual_guc_context_ops = {
3276 	.alloc = guc_virtual_context_alloc,
3277 
3278 	.pre_pin = guc_virtual_context_pre_pin,
3279 	.pin = guc_virtual_context_pin,
3280 	.unpin = guc_virtual_context_unpin,
3281 	.post_unpin = guc_context_post_unpin,
3282 
3283 	.ban = guc_context_ban,
3284 
3285 	.cancel_request = guc_context_cancel_request,
3286 
3287 	.enter = guc_virtual_context_enter,
3288 	.exit = guc_virtual_context_exit,
3289 
3290 	.sched_disable = guc_context_sched_disable,
3291 
3292 	.destroy = guc_context_destroy,
3293 
3294 	.get_sibling = guc_virtual_get_sibling,
3295 };
3296 
3297 static int guc_parent_context_pin(struct intel_context *ce, void *vaddr)
3298 {
3299 	struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
3300 	struct intel_guc *guc = ce_to_guc(ce);
3301 	int ret;
3302 
3303 	GEM_BUG_ON(!intel_context_is_parent(ce));
3304 	GEM_BUG_ON(!intel_engine_is_virtual(ce->engine));
3305 
3306 	ret = pin_guc_id(guc, ce);
3307 	if (unlikely(ret < 0))
3308 		return ret;
3309 
3310 	return __guc_context_pin(ce, engine, vaddr);
3311 }
3312 
3313 static int guc_child_context_pin(struct intel_context *ce, void *vaddr)
3314 {
3315 	struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
3316 
3317 	GEM_BUG_ON(!intel_context_is_child(ce));
3318 	GEM_BUG_ON(!intel_engine_is_virtual(ce->engine));
3319 
3320 	__intel_context_pin(ce->parallel.parent);
3321 	return __guc_context_pin(ce, engine, vaddr);
3322 }
3323 
3324 static void guc_parent_context_unpin(struct intel_context *ce)
3325 {
3326 	struct intel_guc *guc = ce_to_guc(ce);
3327 
3328 	GEM_BUG_ON(context_enabled(ce));
3329 	GEM_BUG_ON(intel_context_is_barrier(ce));
3330 	GEM_BUG_ON(!intel_context_is_parent(ce));
3331 	GEM_BUG_ON(!intel_engine_is_virtual(ce->engine));
3332 
3333 	if (ce->parallel.last_rq)
3334 		i915_request_put(ce->parallel.last_rq);
3335 	unpin_guc_id(guc, ce);
3336 	lrc_unpin(ce);
3337 }
3338 
3339 static void guc_child_context_unpin(struct intel_context *ce)
3340 {
3341 	GEM_BUG_ON(context_enabled(ce));
3342 	GEM_BUG_ON(intel_context_is_barrier(ce));
3343 	GEM_BUG_ON(!intel_context_is_child(ce));
3344 	GEM_BUG_ON(!intel_engine_is_virtual(ce->engine));
3345 
3346 	lrc_unpin(ce);
3347 }
3348 
3349 static void guc_child_context_post_unpin(struct intel_context *ce)
3350 {
3351 	GEM_BUG_ON(!intel_context_is_child(ce));
3352 	GEM_BUG_ON(!intel_context_is_pinned(ce->parallel.parent));
3353 	GEM_BUG_ON(!intel_engine_is_virtual(ce->engine));
3354 
3355 	lrc_post_unpin(ce);
3356 	intel_context_unpin(ce->parallel.parent);
3357 }
3358 
3359 static void guc_child_context_destroy(struct kref *kref)
3360 {
3361 	struct intel_context *ce = container_of(kref, typeof(*ce), ref);
3362 
3363 	__guc_context_destroy(ce);
3364 }
3365 
3366 static const struct intel_context_ops virtual_parent_context_ops = {
3367 	.alloc = guc_virtual_context_alloc,
3368 
3369 	.pre_pin = guc_context_pre_pin,
3370 	.pin = guc_parent_context_pin,
3371 	.unpin = guc_parent_context_unpin,
3372 	.post_unpin = guc_context_post_unpin,
3373 
3374 	.ban = guc_context_ban,
3375 
3376 	.cancel_request = guc_context_cancel_request,
3377 
3378 	.enter = guc_virtual_context_enter,
3379 	.exit = guc_virtual_context_exit,
3380 
3381 	.sched_disable = guc_context_sched_disable,
3382 
3383 	.destroy = guc_context_destroy,
3384 
3385 	.get_sibling = guc_virtual_get_sibling,
3386 };
3387 
3388 static const struct intel_context_ops virtual_child_context_ops = {
3389 	.alloc = guc_virtual_context_alloc,
3390 
3391 	.pre_pin = guc_context_pre_pin,
3392 	.pin = guc_child_context_pin,
3393 	.unpin = guc_child_context_unpin,
3394 	.post_unpin = guc_child_context_post_unpin,
3395 
3396 	.cancel_request = guc_context_cancel_request,
3397 
3398 	.enter = guc_virtual_context_enter,
3399 	.exit = guc_virtual_context_exit,
3400 
3401 	.destroy = guc_child_context_destroy,
3402 
3403 	.get_sibling = guc_virtual_get_sibling,
3404 };
3405 
3406 /*
3407  * The below override of the breadcrumbs is enabled when the user configures a
3408  * context for parallel submission (multi-lrc, parent-child).
3409  *
3410  * The overridden breadcrumbs implements an algorithm which allows the GuC to
3411  * safely preempt all the hw contexts configured for parallel submission
3412  * between each BB. The contract between the i915 and GuC is if the parent
3413  * context can be preempted, all the children can be preempted, and the GuC will
3414  * always try to preempt the parent before the children. A handshake between the
3415  * parent / children breadcrumbs ensures the i915 holds up its end of the deal
3416  * creating a window to preempt between each set of BBs.
3417  */
3418 static int emit_bb_start_parent_no_preempt_mid_batch(struct i915_request *rq,
3419 						     u64 offset, u32 len,
3420 						     const unsigned int flags);
3421 static int emit_bb_start_child_no_preempt_mid_batch(struct i915_request *rq,
3422 						    u64 offset, u32 len,
3423 						    const unsigned int flags);
3424 static u32 *
3425 emit_fini_breadcrumb_parent_no_preempt_mid_batch(struct i915_request *rq,
3426 						 u32 *cs);
3427 static u32 *
3428 emit_fini_breadcrumb_child_no_preempt_mid_batch(struct i915_request *rq,
3429 						u32 *cs);
3430 
3431 static struct intel_context *
3432 guc_create_parallel(struct intel_engine_cs **engines,
3433 		    unsigned int num_siblings,
3434 		    unsigned int width)
3435 {
3436 	struct intel_engine_cs **siblings = NULL;
3437 	struct intel_context *parent = NULL, *ce, *err;
3438 	int i, j;
3439 
3440 	siblings = kmalloc_array(num_siblings,
3441 				 sizeof(*siblings),
3442 				 GFP_KERNEL);
3443 	if (!siblings)
3444 		return ERR_PTR(-ENOMEM);
3445 
3446 	for (i = 0; i < width; ++i) {
3447 		for (j = 0; j < num_siblings; ++j)
3448 			siblings[j] = engines[i * num_siblings + j];
3449 
3450 		ce = intel_engine_create_virtual(siblings, num_siblings,
3451 						 FORCE_VIRTUAL);
3452 		if (IS_ERR(ce)) {
3453 			err = ERR_CAST(ce);
3454 			goto unwind;
3455 		}
3456 
3457 		if (i == 0) {
3458 			parent = ce;
3459 			parent->ops = &virtual_parent_context_ops;
3460 		} else {
3461 			ce->ops = &virtual_child_context_ops;
3462 			intel_context_bind_parent_child(parent, ce);
3463 		}
3464 	}
3465 
3466 	parent->parallel.fence_context = dma_fence_context_alloc(1);
3467 
3468 	parent->engine->emit_bb_start =
3469 		emit_bb_start_parent_no_preempt_mid_batch;
3470 	parent->engine->emit_fini_breadcrumb =
3471 		emit_fini_breadcrumb_parent_no_preempt_mid_batch;
3472 	parent->engine->emit_fini_breadcrumb_dw =
3473 		12 + 4 * parent->parallel.number_children;
3474 	for_each_child(parent, ce) {
3475 		ce->engine->emit_bb_start =
3476 			emit_bb_start_child_no_preempt_mid_batch;
3477 		ce->engine->emit_fini_breadcrumb =
3478 			emit_fini_breadcrumb_child_no_preempt_mid_batch;
3479 		ce->engine->emit_fini_breadcrumb_dw = 16;
3480 	}
3481 
3482 	kfree(siblings);
3483 	return parent;
3484 
3485 unwind:
3486 	if (parent)
3487 		intel_context_put(parent);
3488 	kfree(siblings);
3489 	return err;
3490 }
3491 
3492 static bool
3493 guc_irq_enable_breadcrumbs(struct intel_breadcrumbs *b)
3494 {
3495 	struct intel_engine_cs *sibling;
3496 	intel_engine_mask_t tmp, mask = b->engine_mask;
3497 	bool result = false;
3498 
3499 	for_each_engine_masked(sibling, b->irq_engine->gt, mask, tmp)
3500 		result |= intel_engine_irq_enable(sibling);
3501 
3502 	return result;
3503 }
3504 
3505 static void
3506 guc_irq_disable_breadcrumbs(struct intel_breadcrumbs *b)
3507 {
3508 	struct intel_engine_cs *sibling;
3509 	intel_engine_mask_t tmp, mask = b->engine_mask;
3510 
3511 	for_each_engine_masked(sibling, b->irq_engine->gt, mask, tmp)
3512 		intel_engine_irq_disable(sibling);
3513 }
3514 
3515 static void guc_init_breadcrumbs(struct intel_engine_cs *engine)
3516 {
3517 	int i;
3518 
3519 	/*
3520 	 * In GuC submission mode we do not know which physical engine a request
3521 	 * will be scheduled on, this creates a problem because the breadcrumb
3522 	 * interrupt is per physical engine. To work around this we attach
3523 	 * requests and direct all breadcrumb interrupts to the first instance
3524 	 * of an engine per class. In addition all breadcrumb interrupts are
3525 	 * enabled / disabled across an engine class in unison.
3526 	 */
3527 	for (i = 0; i < MAX_ENGINE_INSTANCE; ++i) {
3528 		struct intel_engine_cs *sibling =
3529 			engine->gt->engine_class[engine->class][i];
3530 
3531 		if (sibling) {
3532 			if (engine->breadcrumbs != sibling->breadcrumbs) {
3533 				intel_breadcrumbs_put(engine->breadcrumbs);
3534 				engine->breadcrumbs =
3535 					intel_breadcrumbs_get(sibling->breadcrumbs);
3536 			}
3537 			break;
3538 		}
3539 	}
3540 
3541 	if (engine->breadcrumbs) {
3542 		engine->breadcrumbs->engine_mask |= engine->mask;
3543 		engine->breadcrumbs->irq_enable = guc_irq_enable_breadcrumbs;
3544 		engine->breadcrumbs->irq_disable = guc_irq_disable_breadcrumbs;
3545 	}
3546 }
3547 
3548 static void guc_bump_inflight_request_prio(struct i915_request *rq,
3549 					   int prio)
3550 {
3551 	struct intel_context *ce = request_to_scheduling_context(rq);
3552 	u8 new_guc_prio = map_i915_prio_to_guc_prio(prio);
3553 
3554 	/* Short circuit function */
3555 	if (prio < I915_PRIORITY_NORMAL ||
3556 	    rq->guc_prio == GUC_PRIO_FINI ||
3557 	    (rq->guc_prio != GUC_PRIO_INIT &&
3558 	     !new_guc_prio_higher(rq->guc_prio, new_guc_prio)))
3559 		return;
3560 
3561 	spin_lock(&ce->guc_state.lock);
3562 	if (rq->guc_prio != GUC_PRIO_FINI) {
3563 		if (rq->guc_prio != GUC_PRIO_INIT)
3564 			sub_context_inflight_prio(ce, rq->guc_prio);
3565 		rq->guc_prio = new_guc_prio;
3566 		add_context_inflight_prio(ce, rq->guc_prio);
3567 		update_context_prio(ce);
3568 	}
3569 	spin_unlock(&ce->guc_state.lock);
3570 }
3571 
3572 static void guc_retire_inflight_request_prio(struct i915_request *rq)
3573 {
3574 	struct intel_context *ce = request_to_scheduling_context(rq);
3575 
3576 	spin_lock(&ce->guc_state.lock);
3577 	guc_prio_fini(rq, ce);
3578 	spin_unlock(&ce->guc_state.lock);
3579 }
3580 
3581 static void sanitize_hwsp(struct intel_engine_cs *engine)
3582 {
3583 	struct intel_timeline *tl;
3584 
3585 	list_for_each_entry(tl, &engine->status_page.timelines, engine_link)
3586 		intel_timeline_reset_seqno(tl);
3587 }
3588 
3589 static void guc_sanitize(struct intel_engine_cs *engine)
3590 {
3591 	/*
3592 	 * Poison residual state on resume, in case the suspend didn't!
3593 	 *
3594 	 * We have to assume that across suspend/resume (or other loss
3595 	 * of control) that the contents of our pinned buffers has been
3596 	 * lost, replaced by garbage. Since this doesn't always happen,
3597 	 * let's poison such state so that we more quickly spot when
3598 	 * we falsely assume it has been preserved.
3599 	 */
3600 	if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
3601 		memset(engine->status_page.addr, POISON_INUSE, PAGE_SIZE);
3602 
3603 	/*
3604 	 * The kernel_context HWSP is stored in the status_page. As above,
3605 	 * that may be lost on resume/initialisation, and so we need to
3606 	 * reset the value in the HWSP.
3607 	 */
3608 	sanitize_hwsp(engine);
3609 
3610 	/* And scrub the dirty cachelines for the HWSP */
3611 	clflush_cache_range(engine->status_page.addr, PAGE_SIZE);
3612 
3613 	intel_engine_reset_pinned_contexts(engine);
3614 }
3615 
3616 static void setup_hwsp(struct intel_engine_cs *engine)
3617 {
3618 	intel_engine_set_hwsp_writemask(engine, ~0u); /* HWSTAM */
3619 
3620 	ENGINE_WRITE_FW(engine,
3621 			RING_HWS_PGA,
3622 			i915_ggtt_offset(engine->status_page.vma));
3623 }
3624 
3625 static void start_engine(struct intel_engine_cs *engine)
3626 {
3627 	ENGINE_WRITE_FW(engine,
3628 			RING_MODE_GEN7,
3629 			_MASKED_BIT_ENABLE(GEN11_GFX_DISABLE_LEGACY_MODE));
3630 
3631 	ENGINE_WRITE_FW(engine, RING_MI_MODE, _MASKED_BIT_DISABLE(STOP_RING));
3632 	ENGINE_POSTING_READ(engine, RING_MI_MODE);
3633 }
3634 
3635 static int guc_resume(struct intel_engine_cs *engine)
3636 {
3637 	assert_forcewakes_active(engine->uncore, FORCEWAKE_ALL);
3638 
3639 	intel_mocs_init_engine(engine);
3640 
3641 	intel_breadcrumbs_reset(engine->breadcrumbs);
3642 
3643 	setup_hwsp(engine);
3644 	start_engine(engine);
3645 
3646 	return 0;
3647 }
3648 
3649 static bool guc_sched_engine_disabled(struct i915_sched_engine *sched_engine)
3650 {
3651 	return !sched_engine->tasklet.callback;
3652 }
3653 
3654 static void guc_set_default_submission(struct intel_engine_cs *engine)
3655 {
3656 	engine->submit_request = guc_submit_request;
3657 }
3658 
3659 static inline void guc_kernel_context_pin(struct intel_guc *guc,
3660 					  struct intel_context *ce)
3661 {
3662 	if (context_guc_id_invalid(ce))
3663 		pin_guc_id(guc, ce);
3664 	guc_lrc_desc_pin(ce, true);
3665 }
3666 
3667 static inline void guc_init_lrc_mapping(struct intel_guc *guc)
3668 {
3669 	struct intel_gt *gt = guc_to_gt(guc);
3670 	struct intel_engine_cs *engine;
3671 	enum intel_engine_id id;
3672 
3673 	/* make sure all descriptors are clean... */
3674 	xa_destroy(&guc->context_lookup);
3675 
3676 	/*
3677 	 * Some contexts might have been pinned before we enabled GuC
3678 	 * submission, so we need to add them to the GuC bookeeping.
3679 	 * Also, after a reset the of the GuC we want to make sure that the
3680 	 * information shared with GuC is properly reset. The kernel LRCs are
3681 	 * not attached to the gem_context, so they need to be added separately.
3682 	 *
3683 	 * Note: we purposefully do not check the return of guc_lrc_desc_pin,
3684 	 * because that function can only fail if a reset is just starting. This
3685 	 * is at the end of reset so presumably another reset isn't happening
3686 	 * and even it did this code would be run again.
3687 	 */
3688 
3689 	for_each_engine(engine, gt, id) {
3690 		struct intel_context *ce;
3691 
3692 		list_for_each_entry(ce, &engine->pinned_contexts_list,
3693 				    pinned_contexts_link)
3694 			guc_kernel_context_pin(guc, ce);
3695 	}
3696 }
3697 
3698 static void guc_release(struct intel_engine_cs *engine)
3699 {
3700 	engine->sanitize = NULL; /* no longer in control, nothing to sanitize */
3701 
3702 	intel_engine_cleanup_common(engine);
3703 	lrc_fini_wa_ctx(engine);
3704 }
3705 
3706 static void virtual_guc_bump_serial(struct intel_engine_cs *engine)
3707 {
3708 	struct intel_engine_cs *e;
3709 	intel_engine_mask_t tmp, mask = engine->mask;
3710 
3711 	for_each_engine_masked(e, engine->gt, mask, tmp)
3712 		e->serial++;
3713 }
3714 
3715 static void guc_default_vfuncs(struct intel_engine_cs *engine)
3716 {
3717 	/* Default vfuncs which can be overridden by each engine. */
3718 
3719 	engine->resume = guc_resume;
3720 
3721 	engine->cops = &guc_context_ops;
3722 	engine->request_alloc = guc_request_alloc;
3723 	engine->add_active_request = add_to_context;
3724 	engine->remove_active_request = remove_from_context;
3725 
3726 	engine->sched_engine->schedule = i915_schedule;
3727 
3728 	engine->reset.prepare = guc_reset_nop;
3729 	engine->reset.rewind = guc_rewind_nop;
3730 	engine->reset.cancel = guc_reset_nop;
3731 	engine->reset.finish = guc_reset_nop;
3732 
3733 	engine->emit_flush = gen8_emit_flush_xcs;
3734 	engine->emit_init_breadcrumb = gen8_emit_init_breadcrumb;
3735 	engine->emit_fini_breadcrumb = gen8_emit_fini_breadcrumb_xcs;
3736 	if (GRAPHICS_VER(engine->i915) >= 12) {
3737 		engine->emit_fini_breadcrumb = gen12_emit_fini_breadcrumb_xcs;
3738 		engine->emit_flush = gen12_emit_flush_xcs;
3739 	}
3740 	engine->set_default_submission = guc_set_default_submission;
3741 	engine->busyness = guc_engine_busyness;
3742 
3743 	engine->flags |= I915_ENGINE_SUPPORTS_STATS;
3744 	engine->flags |= I915_ENGINE_HAS_PREEMPTION;
3745 	engine->flags |= I915_ENGINE_HAS_TIMESLICES;
3746 
3747 	/*
3748 	 * TODO: GuC supports timeslicing and semaphores as well, but they're
3749 	 * handled by the firmware so some minor tweaks are required before
3750 	 * enabling.
3751 	 *
3752 	 * engine->flags |= I915_ENGINE_HAS_SEMAPHORES;
3753 	 */
3754 
3755 	engine->emit_bb_start = gen8_emit_bb_start;
3756 }
3757 
3758 static void rcs_submission_override(struct intel_engine_cs *engine)
3759 {
3760 	switch (GRAPHICS_VER(engine->i915)) {
3761 	case 12:
3762 		engine->emit_flush = gen12_emit_flush_rcs;
3763 		engine->emit_fini_breadcrumb = gen12_emit_fini_breadcrumb_rcs;
3764 		break;
3765 	case 11:
3766 		engine->emit_flush = gen11_emit_flush_rcs;
3767 		engine->emit_fini_breadcrumb = gen11_emit_fini_breadcrumb_rcs;
3768 		break;
3769 	default:
3770 		engine->emit_flush = gen8_emit_flush_rcs;
3771 		engine->emit_fini_breadcrumb = gen8_emit_fini_breadcrumb_rcs;
3772 		break;
3773 	}
3774 }
3775 
3776 static inline void guc_default_irqs(struct intel_engine_cs *engine)
3777 {
3778 	engine->irq_keep_mask = GT_RENDER_USER_INTERRUPT;
3779 	intel_engine_set_irq_handler(engine, cs_irq_handler);
3780 }
3781 
3782 static void guc_sched_engine_destroy(struct kref *kref)
3783 {
3784 	struct i915_sched_engine *sched_engine =
3785 		container_of(kref, typeof(*sched_engine), ref);
3786 	struct intel_guc *guc = sched_engine->private_data;
3787 
3788 	guc->sched_engine = NULL;
3789 	tasklet_kill(&sched_engine->tasklet); /* flush the callback */
3790 	kfree(sched_engine);
3791 }
3792 
3793 int intel_guc_submission_setup(struct intel_engine_cs *engine)
3794 {
3795 	struct drm_i915_private *i915 = engine->i915;
3796 	struct intel_guc *guc = &engine->gt->uc.guc;
3797 
3798 	/*
3799 	 * The setup relies on several assumptions (e.g. irqs always enabled)
3800 	 * that are only valid on gen11+
3801 	 */
3802 	GEM_BUG_ON(GRAPHICS_VER(i915) < 11);
3803 
3804 	if (!guc->sched_engine) {
3805 		guc->sched_engine = i915_sched_engine_create(ENGINE_VIRTUAL);
3806 		if (!guc->sched_engine)
3807 			return -ENOMEM;
3808 
3809 		guc->sched_engine->schedule = i915_schedule;
3810 		guc->sched_engine->disabled = guc_sched_engine_disabled;
3811 		guc->sched_engine->private_data = guc;
3812 		guc->sched_engine->destroy = guc_sched_engine_destroy;
3813 		guc->sched_engine->bump_inflight_request_prio =
3814 			guc_bump_inflight_request_prio;
3815 		guc->sched_engine->retire_inflight_request_prio =
3816 			guc_retire_inflight_request_prio;
3817 		tasklet_setup(&guc->sched_engine->tasklet,
3818 			      guc_submission_tasklet);
3819 	}
3820 	i915_sched_engine_put(engine->sched_engine);
3821 	engine->sched_engine = i915_sched_engine_get(guc->sched_engine);
3822 
3823 	guc_default_vfuncs(engine);
3824 	guc_default_irqs(engine);
3825 	guc_init_breadcrumbs(engine);
3826 
3827 	if (engine->class == RENDER_CLASS)
3828 		rcs_submission_override(engine);
3829 
3830 	lrc_init_wa_ctx(engine);
3831 
3832 	/* Finally, take ownership and responsibility for cleanup! */
3833 	engine->sanitize = guc_sanitize;
3834 	engine->release = guc_release;
3835 
3836 	return 0;
3837 }
3838 
3839 void intel_guc_submission_enable(struct intel_guc *guc)
3840 {
3841 	guc_init_lrc_mapping(guc);
3842 	guc_init_engine_stats(guc);
3843 }
3844 
3845 void intel_guc_submission_disable(struct intel_guc *guc)
3846 {
3847 	/* Note: By the time we're here, GuC may have already been reset */
3848 }
3849 
3850 static bool __guc_submission_supported(struct intel_guc *guc)
3851 {
3852 	/* GuC submission is unavailable for pre-Gen11 */
3853 	return intel_guc_is_supported(guc) &&
3854 	       GRAPHICS_VER(guc_to_gt(guc)->i915) >= 11;
3855 }
3856 
3857 static bool __guc_submission_selected(struct intel_guc *guc)
3858 {
3859 	struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
3860 
3861 	if (!intel_guc_submission_is_supported(guc))
3862 		return false;
3863 
3864 	return i915->params.enable_guc & ENABLE_GUC_SUBMISSION;
3865 }
3866 
3867 void intel_guc_submission_init_early(struct intel_guc *guc)
3868 {
3869 	guc->submission_state.num_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
3870 	guc->submission_supported = __guc_submission_supported(guc);
3871 	guc->submission_selected = __guc_submission_selected(guc);
3872 }
3873 
3874 static inline struct intel_context *
3875 g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
3876 {
3877 	struct intel_context *ce;
3878 
3879 	if (unlikely(desc_idx >= GUC_MAX_LRC_DESCRIPTORS)) {
3880 		drm_err(&guc_to_gt(guc)->i915->drm,
3881 			"Invalid desc_idx %u", desc_idx);
3882 		return NULL;
3883 	}
3884 
3885 	ce = __get_context(guc, desc_idx);
3886 	if (unlikely(!ce)) {
3887 		drm_err(&guc_to_gt(guc)->i915->drm,
3888 			"Context is NULL, desc_idx %u", desc_idx);
3889 		return NULL;
3890 	}
3891 
3892 	if (unlikely(intel_context_is_child(ce))) {
3893 		drm_err(&guc_to_gt(guc)->i915->drm,
3894 			"Context is child, desc_idx %u", desc_idx);
3895 		return NULL;
3896 	}
3897 
3898 	return ce;
3899 }
3900 
3901 int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
3902 					  const u32 *msg,
3903 					  u32 len)
3904 {
3905 	struct intel_context *ce;
3906 	u32 desc_idx = msg[0];
3907 
3908 	if (unlikely(len < 1)) {
3909 		drm_err(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);
3910 		return -EPROTO;
3911 	}
3912 
3913 	ce = g2h_context_lookup(guc, desc_idx);
3914 	if (unlikely(!ce))
3915 		return -EPROTO;
3916 
3917 	trace_intel_context_deregister_done(ce);
3918 
3919 #ifdef CONFIG_DRM_I915_SELFTEST
3920 	if (unlikely(ce->drop_deregister)) {
3921 		ce->drop_deregister = false;
3922 		return 0;
3923 	}
3924 #endif
3925 
3926 	if (context_wait_for_deregister_to_register(ce)) {
3927 		struct intel_runtime_pm *runtime_pm =
3928 			&ce->engine->gt->i915->runtime_pm;
3929 		intel_wakeref_t wakeref;
3930 
3931 		/*
3932 		 * Previous owner of this guc_id has been deregistered, now safe
3933 		 * register this context.
3934 		 */
3935 		with_intel_runtime_pm(runtime_pm, wakeref)
3936 			register_context(ce, true);
3937 		guc_signal_context_fence(ce);
3938 		intel_context_put(ce);
3939 	} else if (context_destroyed(ce)) {
3940 		/* Context has been destroyed */
3941 		intel_gt_pm_put_async(guc_to_gt(guc));
3942 		release_guc_id(guc, ce);
3943 		__guc_context_destroy(ce);
3944 	}
3945 
3946 	decr_outstanding_submission_g2h(guc);
3947 
3948 	return 0;
3949 }
3950 
3951 int intel_guc_sched_done_process_msg(struct intel_guc *guc,
3952 				     const u32 *msg,
3953 				     u32 len)
3954 {
3955 	struct intel_context *ce;
3956 	unsigned long flags;
3957 	u32 desc_idx = msg[0];
3958 
3959 	if (unlikely(len < 2)) {
3960 		drm_err(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);
3961 		return -EPROTO;
3962 	}
3963 
3964 	ce = g2h_context_lookup(guc, desc_idx);
3965 	if (unlikely(!ce))
3966 		return -EPROTO;
3967 
3968 	if (unlikely(context_destroyed(ce) ||
3969 		     (!context_pending_enable(ce) &&
3970 		     !context_pending_disable(ce)))) {
3971 		drm_err(&guc_to_gt(guc)->i915->drm,
3972 			"Bad context sched_state 0x%x, desc_idx %u",
3973 			ce->guc_state.sched_state, desc_idx);
3974 		return -EPROTO;
3975 	}
3976 
3977 	trace_intel_context_sched_done(ce);
3978 
3979 	if (context_pending_enable(ce)) {
3980 #ifdef CONFIG_DRM_I915_SELFTEST
3981 		if (unlikely(ce->drop_schedule_enable)) {
3982 			ce->drop_schedule_enable = false;
3983 			return 0;
3984 		}
3985 #endif
3986 
3987 		spin_lock_irqsave(&ce->guc_state.lock, flags);
3988 		clr_context_pending_enable(ce);
3989 		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
3990 	} else if (context_pending_disable(ce)) {
3991 		bool banned;
3992 
3993 #ifdef CONFIG_DRM_I915_SELFTEST
3994 		if (unlikely(ce->drop_schedule_disable)) {
3995 			ce->drop_schedule_disable = false;
3996 			return 0;
3997 		}
3998 #endif
3999 
4000 		/*
4001 		 * Unpin must be done before __guc_signal_context_fence,
4002 		 * otherwise a race exists between the requests getting
4003 		 * submitted + retired before this unpin completes resulting in
4004 		 * the pin_count going to zero and the context still being
4005 		 * enabled.
4006 		 */
4007 		intel_context_sched_disable_unpin(ce);
4008 
4009 		spin_lock_irqsave(&ce->guc_state.lock, flags);
4010 		banned = context_banned(ce);
4011 		clr_context_banned(ce);
4012 		clr_context_pending_disable(ce);
4013 		__guc_signal_context_fence(ce);
4014 		guc_blocked_fence_complete(ce);
4015 		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
4016 
4017 		if (banned) {
4018 			guc_cancel_context_requests(ce);
4019 			intel_engine_signal_breadcrumbs(ce->engine);
4020 		}
4021 	}
4022 
4023 	decr_outstanding_submission_g2h(guc);
4024 	intel_context_put(ce);
4025 
4026 	return 0;
4027 }
4028 
4029 static void capture_error_state(struct intel_guc *guc,
4030 				struct intel_context *ce)
4031 {
4032 	struct intel_gt *gt = guc_to_gt(guc);
4033 	struct drm_i915_private *i915 = gt->i915;
4034 	struct intel_engine_cs *engine = __context_to_physical_engine(ce);
4035 	intel_wakeref_t wakeref;
4036 
4037 	intel_engine_set_hung_context(engine, ce);
4038 	with_intel_runtime_pm(&i915->runtime_pm, wakeref)
4039 		i915_capture_error_state(gt, engine->mask);
4040 	atomic_inc(&i915->gpu_error.reset_engine_count[engine->uabi_class]);
4041 }
4042 
4043 static void guc_context_replay(struct intel_context *ce)
4044 {
4045 	struct i915_sched_engine *sched_engine = ce->engine->sched_engine;
4046 
4047 	__guc_reset_context(ce, true);
4048 	tasklet_hi_schedule(&sched_engine->tasklet);
4049 }
4050 
4051 static void guc_handle_context_reset(struct intel_guc *guc,
4052 				     struct intel_context *ce)
4053 {
4054 	trace_intel_context_reset(ce);
4055 
4056 	/*
4057 	 * XXX: Racey if request cancellation has occurred, see comment in
4058 	 * __guc_reset_context().
4059 	 */
4060 	if (likely(!intel_context_is_banned(ce) &&
4061 		   !context_blocked(ce))) {
4062 		capture_error_state(guc, ce);
4063 		guc_context_replay(ce);
4064 	}
4065 }
4066 
4067 int intel_guc_context_reset_process_msg(struct intel_guc *guc,
4068 					const u32 *msg, u32 len)
4069 {
4070 	struct intel_context *ce;
4071 	unsigned long flags;
4072 	int desc_idx;
4073 
4074 	if (unlikely(len != 1)) {
4075 		drm_err(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);
4076 		return -EPROTO;
4077 	}
4078 
4079 	desc_idx = msg[0];
4080 
4081 	/*
4082 	 * The context lookup uses the xarray but lookups only require an RCU lock
4083 	 * not the full spinlock. So take the lock explicitly and keep it until the
4084 	 * context has been reference count locked to ensure it can't be destroyed
4085 	 * asynchronously until the reset is done.
4086 	 */
4087 	xa_lock_irqsave(&guc->context_lookup, flags);
4088 	ce = g2h_context_lookup(guc, desc_idx);
4089 	if (ce)
4090 		intel_context_get(ce);
4091 	xa_unlock_irqrestore(&guc->context_lookup, flags);
4092 
4093 	if (unlikely(!ce))
4094 		return -EPROTO;
4095 
4096 	guc_handle_context_reset(guc, ce);
4097 	intel_context_put(ce);
4098 
4099 	return 0;
4100 }
4101 
4102 static struct intel_engine_cs *
4103 guc_lookup_engine(struct intel_guc *guc, u8 guc_class, u8 instance)
4104 {
4105 	struct intel_gt *gt = guc_to_gt(guc);
4106 	u8 engine_class = guc_class_to_engine_class(guc_class);
4107 
4108 	/* Class index is checked in class converter */
4109 	GEM_BUG_ON(instance > MAX_ENGINE_INSTANCE);
4110 
4111 	return gt->engine_class[engine_class][instance];
4112 }
4113 
4114 int intel_guc_engine_failure_process_msg(struct intel_guc *guc,
4115 					 const u32 *msg, u32 len)
4116 {
4117 	struct intel_engine_cs *engine;
4118 	struct intel_gt *gt = guc_to_gt(guc);
4119 	u8 guc_class, instance;
4120 	u32 reason;
4121 
4122 	if (unlikely(len != 3)) {
4123 		drm_err(&gt->i915->drm, "Invalid length %u", len);
4124 		return -EPROTO;
4125 	}
4126 
4127 	guc_class = msg[0];
4128 	instance = msg[1];
4129 	reason = msg[2];
4130 
4131 	engine = guc_lookup_engine(guc, guc_class, instance);
4132 	if (unlikely(!engine)) {
4133 		drm_err(&gt->i915->drm,
4134 			"Invalid engine %d:%d", guc_class, instance);
4135 		return -EPROTO;
4136 	}
4137 
4138 	/*
4139 	 * This is an unexpected failure of a hardware feature. So, log a real
4140 	 * error message not just the informational that comes with the reset.
4141 	 */
4142 	drm_err(&gt->i915->drm, "GuC engine reset request failed on %d:%d (%s) because 0x%08X",
4143 		guc_class, instance, engine->name, reason);
4144 
4145 	intel_gt_handle_error(gt, engine->mask,
4146 			      I915_ERROR_CAPTURE,
4147 			      "GuC failed to reset %s (reason=0x%08x)\n",
4148 			      engine->name, reason);
4149 
4150 	return 0;
4151 }
4152 
4153 void intel_guc_find_hung_context(struct intel_engine_cs *engine)
4154 {
4155 	struct intel_guc *guc = &engine->gt->uc.guc;
4156 	struct intel_context *ce;
4157 	struct i915_request *rq;
4158 	unsigned long index;
4159 	unsigned long flags;
4160 
4161 	/* Reset called during driver load? GuC not yet initialised! */
4162 	if (unlikely(!guc_submission_initialized(guc)))
4163 		return;
4164 
4165 	xa_lock_irqsave(&guc->context_lookup, flags);
4166 	xa_for_each(&guc->context_lookup, index, ce) {
4167 		if (!kref_get_unless_zero(&ce->ref))
4168 			continue;
4169 
4170 		xa_unlock(&guc->context_lookup);
4171 
4172 		if (!intel_context_is_pinned(ce))
4173 			goto next;
4174 
4175 		if (intel_engine_is_virtual(ce->engine)) {
4176 			if (!(ce->engine->mask & engine->mask))
4177 				goto next;
4178 		} else {
4179 			if (ce->engine != engine)
4180 				goto next;
4181 		}
4182 
4183 		list_for_each_entry(rq, &ce->guc_state.requests, sched.link) {
4184 			if (i915_test_request_state(rq) != I915_REQUEST_ACTIVE)
4185 				continue;
4186 
4187 			intel_engine_set_hung_context(engine, ce);
4188 
4189 			/* Can only cope with one hang at a time... */
4190 			intel_context_put(ce);
4191 			xa_lock(&guc->context_lookup);
4192 			goto done;
4193 		}
4194 next:
4195 		intel_context_put(ce);
4196 		xa_lock(&guc->context_lookup);
4197 	}
4198 done:
4199 	xa_unlock_irqrestore(&guc->context_lookup, flags);
4200 }
4201 
4202 void intel_guc_dump_active_requests(struct intel_engine_cs *engine,
4203 				    struct i915_request *hung_rq,
4204 				    struct drm_printer *m)
4205 {
4206 	struct intel_guc *guc = &engine->gt->uc.guc;
4207 	struct intel_context *ce;
4208 	unsigned long index;
4209 	unsigned long flags;
4210 
4211 	/* Reset called during driver load? GuC not yet initialised! */
4212 	if (unlikely(!guc_submission_initialized(guc)))
4213 		return;
4214 
4215 	xa_lock_irqsave(&guc->context_lookup, flags);
4216 	xa_for_each(&guc->context_lookup, index, ce) {
4217 		if (!kref_get_unless_zero(&ce->ref))
4218 			continue;
4219 
4220 		xa_unlock(&guc->context_lookup);
4221 
4222 		if (!intel_context_is_pinned(ce))
4223 			goto next;
4224 
4225 		if (intel_engine_is_virtual(ce->engine)) {
4226 			if (!(ce->engine->mask & engine->mask))
4227 				goto next;
4228 		} else {
4229 			if (ce->engine != engine)
4230 				goto next;
4231 		}
4232 
4233 		spin_lock(&ce->guc_state.lock);
4234 		intel_engine_dump_active_requests(&ce->guc_state.requests,
4235 						  hung_rq, m);
4236 		spin_unlock(&ce->guc_state.lock);
4237 
4238 next:
4239 		intel_context_put(ce);
4240 		xa_lock(&guc->context_lookup);
4241 	}
4242 	xa_unlock_irqrestore(&guc->context_lookup, flags);
4243 }
4244 
4245 void intel_guc_submission_print_info(struct intel_guc *guc,
4246 				     struct drm_printer *p)
4247 {
4248 	struct i915_sched_engine *sched_engine = guc->sched_engine;
4249 	struct rb_node *rb;
4250 	unsigned long flags;
4251 
4252 	if (!sched_engine)
4253 		return;
4254 
4255 	drm_printf(p, "GuC Number Outstanding Submission G2H: %u\n",
4256 		   atomic_read(&guc->outstanding_submission_g2h));
4257 	drm_printf(p, "GuC tasklet count: %u\n\n",
4258 		   atomic_read(&sched_engine->tasklet.count));
4259 
4260 	spin_lock_irqsave(&sched_engine->lock, flags);
4261 	drm_printf(p, "Requests in GuC submit tasklet:\n");
4262 	for (rb = rb_first_cached(&sched_engine->queue); rb; rb = rb_next(rb)) {
4263 		struct i915_priolist *pl = to_priolist(rb);
4264 		struct i915_request *rq;
4265 
4266 		priolist_for_each_request(rq, pl)
4267 			drm_printf(p, "guc_id=%u, seqno=%llu\n",
4268 				   rq->context->guc_id.id,
4269 				   rq->fence.seqno);
4270 	}
4271 	spin_unlock_irqrestore(&sched_engine->lock, flags);
4272 	drm_printf(p, "\n");
4273 }
4274 
4275 static inline void guc_log_context_priority(struct drm_printer *p,
4276 					    struct intel_context *ce)
4277 {
4278 	int i;
4279 
4280 	drm_printf(p, "\t\tPriority: %d\n", ce->guc_state.prio);
4281 	drm_printf(p, "\t\tNumber Requests (lower index == higher priority)\n");
4282 	for (i = GUC_CLIENT_PRIORITY_KMD_HIGH;
4283 	     i < GUC_CLIENT_PRIORITY_NUM; ++i) {
4284 		drm_printf(p, "\t\tNumber requests in priority band[%d]: %d\n",
4285 			   i, ce->guc_state.prio_count[i]);
4286 	}
4287 	drm_printf(p, "\n");
4288 }
4289 
4290 static inline void guc_log_context(struct drm_printer *p,
4291 				   struct intel_context *ce)
4292 {
4293 	drm_printf(p, "GuC lrc descriptor %u:\n", ce->guc_id.id);
4294 	drm_printf(p, "\tHW Context Desc: 0x%08x\n", ce->lrc.lrca);
4295 	drm_printf(p, "\t\tLRC Head: Internal %u, Memory %u\n",
4296 		   ce->ring->head,
4297 		   ce->lrc_reg_state[CTX_RING_HEAD]);
4298 	drm_printf(p, "\t\tLRC Tail: Internal %u, Memory %u\n",
4299 		   ce->ring->tail,
4300 		   ce->lrc_reg_state[CTX_RING_TAIL]);
4301 	drm_printf(p, "\t\tContext Pin Count: %u\n",
4302 		   atomic_read(&ce->pin_count));
4303 	drm_printf(p, "\t\tGuC ID Ref Count: %u\n",
4304 		   atomic_read(&ce->guc_id.ref));
4305 	drm_printf(p, "\t\tSchedule State: 0x%x\n\n",
4306 		   ce->guc_state.sched_state);
4307 }
4308 
4309 void intel_guc_submission_print_context_info(struct intel_guc *guc,
4310 					     struct drm_printer *p)
4311 {
4312 	struct intel_context *ce;
4313 	unsigned long index;
4314 	unsigned long flags;
4315 
4316 	xa_lock_irqsave(&guc->context_lookup, flags);
4317 	xa_for_each(&guc->context_lookup, index, ce) {
4318 		GEM_BUG_ON(intel_context_is_child(ce));
4319 
4320 		guc_log_context(p, ce);
4321 		guc_log_context_priority(p, ce);
4322 
4323 		if (intel_context_is_parent(ce)) {
4324 			struct guc_process_desc *desc = __get_process_desc(ce);
4325 			struct intel_context *child;
4326 
4327 			drm_printf(p, "\t\tNumber children: %u\n",
4328 				   ce->parallel.number_children);
4329 			drm_printf(p, "\t\tWQI Head: %u\n",
4330 				   READ_ONCE(desc->head));
4331 			drm_printf(p, "\t\tWQI Tail: %u\n",
4332 				   READ_ONCE(desc->tail));
4333 			drm_printf(p, "\t\tWQI Status: %u\n\n",
4334 				   READ_ONCE(desc->wq_status));
4335 
4336 			if (ce->engine->emit_bb_start ==
4337 			    emit_bb_start_parent_no_preempt_mid_batch) {
4338 				u8 i;
4339 
4340 				drm_printf(p, "\t\tChildren Go: %u\n\n",
4341 					   get_children_go_value(ce));
4342 				for (i = 0; i < ce->parallel.number_children; ++i)
4343 					drm_printf(p, "\t\tChildren Join: %u\n",
4344 						   get_children_join_value(ce, i));
4345 			}
4346 
4347 			for_each_child(ce, child)
4348 				guc_log_context(p, child);
4349 		}
4350 	}
4351 	xa_unlock_irqrestore(&guc->context_lookup, flags);
4352 }
4353 
4354 static inline u32 get_children_go_addr(struct intel_context *ce)
4355 {
4356 	GEM_BUG_ON(!intel_context_is_parent(ce));
4357 
4358 	return i915_ggtt_offset(ce->state) +
4359 		__get_parent_scratch_offset(ce) +
4360 		offsetof(struct parent_scratch, go.semaphore);
4361 }
4362 
4363 static inline u32 get_children_join_addr(struct intel_context *ce,
4364 					 u8 child_index)
4365 {
4366 	GEM_BUG_ON(!intel_context_is_parent(ce));
4367 
4368 	return i915_ggtt_offset(ce->state) +
4369 		__get_parent_scratch_offset(ce) +
4370 		offsetof(struct parent_scratch, join[child_index].semaphore);
4371 }
4372 
4373 #define PARENT_GO_BB			1
4374 #define PARENT_GO_FINI_BREADCRUMB	0
4375 #define CHILD_GO_BB			1
4376 #define CHILD_GO_FINI_BREADCRUMB	0
4377 static int emit_bb_start_parent_no_preempt_mid_batch(struct i915_request *rq,
4378 						     u64 offset, u32 len,
4379 						     const unsigned int flags)
4380 {
4381 	struct intel_context *ce = rq->context;
4382 	u32 *cs;
4383 	u8 i;
4384 
4385 	GEM_BUG_ON(!intel_context_is_parent(ce));
4386 
4387 	cs = intel_ring_begin(rq, 10 + 4 * ce->parallel.number_children);
4388 	if (IS_ERR(cs))
4389 		return PTR_ERR(cs);
4390 
4391 	/* Wait on children */
4392 	for (i = 0; i < ce->parallel.number_children; ++i) {
4393 		*cs++ = (MI_SEMAPHORE_WAIT |
4394 			 MI_SEMAPHORE_GLOBAL_GTT |
4395 			 MI_SEMAPHORE_POLL |
4396 			 MI_SEMAPHORE_SAD_EQ_SDD);
4397 		*cs++ = PARENT_GO_BB;
4398 		*cs++ = get_children_join_addr(ce, i);
4399 		*cs++ = 0;
4400 	}
4401 
4402 	/* Turn off preemption */
4403 	*cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
4404 	*cs++ = MI_NOOP;
4405 
4406 	/* Tell children go */
4407 	cs = gen8_emit_ggtt_write(cs,
4408 				  CHILD_GO_BB,
4409 				  get_children_go_addr(ce),
4410 				  0);
4411 
4412 	/* Jump to batch */
4413 	*cs++ = MI_BATCH_BUFFER_START_GEN8 |
4414 		(flags & I915_DISPATCH_SECURE ? 0 : BIT(8));
4415 	*cs++ = lower_32_bits(offset);
4416 	*cs++ = upper_32_bits(offset);
4417 	*cs++ = MI_NOOP;
4418 
4419 	intel_ring_advance(rq, cs);
4420 
4421 	return 0;
4422 }
4423 
4424 static int emit_bb_start_child_no_preempt_mid_batch(struct i915_request *rq,
4425 						    u64 offset, u32 len,
4426 						    const unsigned int flags)
4427 {
4428 	struct intel_context *ce = rq->context;
4429 	struct intel_context *parent = intel_context_to_parent(ce);
4430 	u32 *cs;
4431 
4432 	GEM_BUG_ON(!intel_context_is_child(ce));
4433 
4434 	cs = intel_ring_begin(rq, 12);
4435 	if (IS_ERR(cs))
4436 		return PTR_ERR(cs);
4437 
4438 	/* Signal parent */
4439 	cs = gen8_emit_ggtt_write(cs,
4440 				  PARENT_GO_BB,
4441 				  get_children_join_addr(parent,
4442 							 ce->parallel.child_index),
4443 				  0);
4444 
4445 	/* Wait on parent for go */
4446 	*cs++ = (MI_SEMAPHORE_WAIT |
4447 		 MI_SEMAPHORE_GLOBAL_GTT |
4448 		 MI_SEMAPHORE_POLL |
4449 		 MI_SEMAPHORE_SAD_EQ_SDD);
4450 	*cs++ = CHILD_GO_BB;
4451 	*cs++ = get_children_go_addr(parent);
4452 	*cs++ = 0;
4453 
4454 	/* Turn off preemption */
4455 	*cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
4456 
4457 	/* Jump to batch */
4458 	*cs++ = MI_BATCH_BUFFER_START_GEN8 |
4459 		(flags & I915_DISPATCH_SECURE ? 0 : BIT(8));
4460 	*cs++ = lower_32_bits(offset);
4461 	*cs++ = upper_32_bits(offset);
4462 
4463 	intel_ring_advance(rq, cs);
4464 
4465 	return 0;
4466 }
4467 
4468 static u32 *
4469 __emit_fini_breadcrumb_parent_no_preempt_mid_batch(struct i915_request *rq,
4470 						   u32 *cs)
4471 {
4472 	struct intel_context *ce = rq->context;
4473 	u8 i;
4474 
4475 	GEM_BUG_ON(!intel_context_is_parent(ce));
4476 
4477 	/* Wait on children */
4478 	for (i = 0; i < ce->parallel.number_children; ++i) {
4479 		*cs++ = (MI_SEMAPHORE_WAIT |
4480 			 MI_SEMAPHORE_GLOBAL_GTT |
4481 			 MI_SEMAPHORE_POLL |
4482 			 MI_SEMAPHORE_SAD_EQ_SDD);
4483 		*cs++ = PARENT_GO_FINI_BREADCRUMB;
4484 		*cs++ = get_children_join_addr(ce, i);
4485 		*cs++ = 0;
4486 	}
4487 
4488 	/* Turn on preemption */
4489 	*cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
4490 	*cs++ = MI_NOOP;
4491 
4492 	/* Tell children go */
4493 	cs = gen8_emit_ggtt_write(cs,
4494 				  CHILD_GO_FINI_BREADCRUMB,
4495 				  get_children_go_addr(ce),
4496 				  0);
4497 
4498 	return cs;
4499 }
4500 
4501 /*
4502  * If this true, a submission of multi-lrc requests had an error and the
4503  * requests need to be skipped. The front end (execuf IOCTL) should've called
4504  * i915_request_skip which squashes the BB but we still need to emit the fini
4505  * breadrcrumbs seqno write. At this point we don't know how many of the
4506  * requests in the multi-lrc submission were generated so we can't do the
4507  * handshake between the parent and children (e.g. if 4 requests should be
4508  * generated but 2nd hit an error only 1 would be seen by the GuC backend).
4509  * Simply skip the handshake, but still emit the breadcrumbd seqno, if an error
4510  * has occurred on any of the requests in submission / relationship.
4511  */
4512 static inline bool skip_handshake(struct i915_request *rq)
4513 {
4514 	return test_bit(I915_FENCE_FLAG_SKIP_PARALLEL, &rq->fence.flags);
4515 }
4516 
4517 static u32 *
4518 emit_fini_breadcrumb_parent_no_preempt_mid_batch(struct i915_request *rq,
4519 						 u32 *cs)
4520 {
4521 	struct intel_context *ce = rq->context;
4522 
4523 	GEM_BUG_ON(!intel_context_is_parent(ce));
4524 
4525 	if (unlikely(skip_handshake(rq))) {
4526 		/*
4527 		 * NOP everything in __emit_fini_breadcrumb_parent_no_preempt_mid_batch,
4528 		 * the -6 comes from the length of the emits below.
4529 		 */
4530 		memset(cs, 0, sizeof(u32) *
4531 		       (ce->engine->emit_fini_breadcrumb_dw - 6));
4532 		cs += ce->engine->emit_fini_breadcrumb_dw - 6;
4533 	} else {
4534 		cs = __emit_fini_breadcrumb_parent_no_preempt_mid_batch(rq, cs);
4535 	}
4536 
4537 	/* Emit fini breadcrumb */
4538 	cs = gen8_emit_ggtt_write(cs,
4539 				  rq->fence.seqno,
4540 				  i915_request_active_timeline(rq)->hwsp_offset,
4541 				  0);
4542 
4543 	/* User interrupt */
4544 	*cs++ = MI_USER_INTERRUPT;
4545 	*cs++ = MI_NOOP;
4546 
4547 	rq->tail = intel_ring_offset(rq, cs);
4548 
4549 	return cs;
4550 }
4551 
4552 static u32 *
4553 __emit_fini_breadcrumb_child_no_preempt_mid_batch(struct i915_request *rq,
4554 						  u32 *cs)
4555 {
4556 	struct intel_context *ce = rq->context;
4557 	struct intel_context *parent = intel_context_to_parent(ce);
4558 
4559 	GEM_BUG_ON(!intel_context_is_child(ce));
4560 
4561 	/* Turn on preemption */
4562 	*cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
4563 	*cs++ = MI_NOOP;
4564 
4565 	/* Signal parent */
4566 	cs = gen8_emit_ggtt_write(cs,
4567 				  PARENT_GO_FINI_BREADCRUMB,
4568 				  get_children_join_addr(parent,
4569 							 ce->parallel.child_index),
4570 				  0);
4571 
4572 	/* Wait parent on for go */
4573 	*cs++ = (MI_SEMAPHORE_WAIT |
4574 		 MI_SEMAPHORE_GLOBAL_GTT |
4575 		 MI_SEMAPHORE_POLL |
4576 		 MI_SEMAPHORE_SAD_EQ_SDD);
4577 	*cs++ = CHILD_GO_FINI_BREADCRUMB;
4578 	*cs++ = get_children_go_addr(parent);
4579 	*cs++ = 0;
4580 
4581 	return cs;
4582 }
4583 
4584 static u32 *
4585 emit_fini_breadcrumb_child_no_preempt_mid_batch(struct i915_request *rq,
4586 						u32 *cs)
4587 {
4588 	struct intel_context *ce = rq->context;
4589 
4590 	GEM_BUG_ON(!intel_context_is_child(ce));
4591 
4592 	if (unlikely(skip_handshake(rq))) {
4593 		/*
4594 		 * NOP everything in __emit_fini_breadcrumb_child_no_preempt_mid_batch,
4595 		 * the -6 comes from the length of the emits below.
4596 		 */
4597 		memset(cs, 0, sizeof(u32) *
4598 		       (ce->engine->emit_fini_breadcrumb_dw - 6));
4599 		cs += ce->engine->emit_fini_breadcrumb_dw - 6;
4600 	} else {
4601 		cs = __emit_fini_breadcrumb_child_no_preempt_mid_batch(rq, cs);
4602 	}
4603 
4604 	/* Emit fini breadcrumb */
4605 	cs = gen8_emit_ggtt_write(cs,
4606 				  rq->fence.seqno,
4607 				  i915_request_active_timeline(rq)->hwsp_offset,
4608 				  0);
4609 
4610 	/* User interrupt */
4611 	*cs++ = MI_USER_INTERRUPT;
4612 	*cs++ = MI_NOOP;
4613 
4614 	rq->tail = intel_ring_offset(rq, cs);
4615 
4616 	return cs;
4617 }
4618 
4619 static struct intel_context *
4620 guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count,
4621 		   unsigned long flags)
4622 {
4623 	struct guc_virtual_engine *ve;
4624 	struct intel_guc *guc;
4625 	unsigned int n;
4626 	int err;
4627 
4628 	ve = kzalloc(sizeof(*ve), GFP_KERNEL);
4629 	if (!ve)
4630 		return ERR_PTR(-ENOMEM);
4631 
4632 	guc = &siblings[0]->gt->uc.guc;
4633 
4634 	ve->base.i915 = siblings[0]->i915;
4635 	ve->base.gt = siblings[0]->gt;
4636 	ve->base.uncore = siblings[0]->uncore;
4637 	ve->base.id = -1;
4638 
4639 	ve->base.uabi_class = I915_ENGINE_CLASS_INVALID;
4640 	ve->base.instance = I915_ENGINE_CLASS_INVALID_VIRTUAL;
4641 	ve->base.uabi_instance = I915_ENGINE_CLASS_INVALID_VIRTUAL;
4642 	ve->base.saturated = ALL_ENGINES;
4643 
4644 	snprintf(ve->base.name, sizeof(ve->base.name), "virtual");
4645 
4646 	ve->base.sched_engine = i915_sched_engine_get(guc->sched_engine);
4647 
4648 	ve->base.cops = &virtual_guc_context_ops;
4649 	ve->base.request_alloc = guc_request_alloc;
4650 	ve->base.bump_serial = virtual_guc_bump_serial;
4651 
4652 	ve->base.submit_request = guc_submit_request;
4653 
4654 	ve->base.flags = I915_ENGINE_IS_VIRTUAL;
4655 
4656 	intel_context_init(&ve->context, &ve->base);
4657 
4658 	for (n = 0; n < count; n++) {
4659 		struct intel_engine_cs *sibling = siblings[n];
4660 
4661 		GEM_BUG_ON(!is_power_of_2(sibling->mask));
4662 		if (sibling->mask & ve->base.mask) {
4663 			DRM_DEBUG("duplicate %s entry in load balancer\n",
4664 				  sibling->name);
4665 			err = -EINVAL;
4666 			goto err_put;
4667 		}
4668 
4669 		ve->base.mask |= sibling->mask;
4670 		ve->base.logical_mask |= sibling->logical_mask;
4671 
4672 		if (n != 0 && ve->base.class != sibling->class) {
4673 			DRM_DEBUG("invalid mixing of engine class, sibling %d, already %d\n",
4674 				  sibling->class, ve->base.class);
4675 			err = -EINVAL;
4676 			goto err_put;
4677 		} else if (n == 0) {
4678 			ve->base.class = sibling->class;
4679 			ve->base.uabi_class = sibling->uabi_class;
4680 			snprintf(ve->base.name, sizeof(ve->base.name),
4681 				 "v%dx%d", ve->base.class, count);
4682 			ve->base.context_size = sibling->context_size;
4683 
4684 			ve->base.add_active_request =
4685 				sibling->add_active_request;
4686 			ve->base.remove_active_request =
4687 				sibling->remove_active_request;
4688 			ve->base.emit_bb_start = sibling->emit_bb_start;
4689 			ve->base.emit_flush = sibling->emit_flush;
4690 			ve->base.emit_init_breadcrumb =
4691 				sibling->emit_init_breadcrumb;
4692 			ve->base.emit_fini_breadcrumb =
4693 				sibling->emit_fini_breadcrumb;
4694 			ve->base.emit_fini_breadcrumb_dw =
4695 				sibling->emit_fini_breadcrumb_dw;
4696 			ve->base.breadcrumbs =
4697 				intel_breadcrumbs_get(sibling->breadcrumbs);
4698 
4699 			ve->base.flags |= sibling->flags;
4700 
4701 			ve->base.props.timeslice_duration_ms =
4702 				sibling->props.timeslice_duration_ms;
4703 			ve->base.props.preempt_timeout_ms =
4704 				sibling->props.preempt_timeout_ms;
4705 		}
4706 	}
4707 
4708 	return &ve->context;
4709 
4710 err_put:
4711 	intel_context_put(&ve->context);
4712 	return ERR_PTR(err);
4713 }
4714 
4715 bool intel_guc_virtual_engine_has_heartbeat(const struct intel_engine_cs *ve)
4716 {
4717 	struct intel_engine_cs *engine;
4718 	intel_engine_mask_t tmp, mask = ve->mask;
4719 
4720 	for_each_engine_masked(engine, ve->gt, mask, tmp)
4721 		if (READ_ONCE(engine->props.heartbeat_interval_ms))
4722 			return true;
4723 
4724 	return false;
4725 }
4726 
4727 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
4728 #include "selftest_guc.c"
4729 #include "selftest_guc_multi_lrc.c"
4730 #endif
4731