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