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