intel_guc_submission.c 116.6 KB
Newer Older
1
// SPDX-License-Identifier: MIT
2 3 4 5
/*
 * Copyright © 2014 Intel Corporation
 */

6
#include <linux/circ_buf.h>
7

8
#include "gem/i915_gem_context.h"
9 10
#include "gt/gen8_engine_cs.h"
#include "gt/intel_breadcrumbs.h"
11 12
#include "gt/intel_context.h"
#include "gt/intel_engine_pm.h"
13
#include "gt/intel_engine_heartbeat.h"
14
#include "gt/intel_gpu_commands.h"
15
#include "gt/intel_gt.h"
16
#include "gt/intel_gt_irq.h"
17
#include "gt/intel_gt_pm.h"
18
#include "gt/intel_gt_requests.h"
19
#include "gt/intel_lrc.h"
20
#include "gt/intel_lrc_reg.h"
21
#include "gt/intel_mocs.h"
22 23
#include "gt/intel_ring.h"

24
#include "intel_guc_submission.h"
25

26
#include "i915_drv.h"
27
#include "i915_trace.h"
28

29
/**
A
Alex Dai 已提交
30
 * DOC: GuC-based command submission
31 32 33 34 35 36 37 38 39
 *
 * The Scratch registers:
 * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes
 * a value to the action register (SOFT_SCRATCH_0) along with any data. It then
 * triggers an interrupt on the GuC via another register write (0xC4C8).
 * Firmware writes a success/fail code back to the action register after
 * processes the request. The kernel driver polls waiting for this update and
 * then proceeds.
 *
M
Matthew Brost 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
 * Command Transport buffers (CTBs):
 * Covered in detail in other sections but CTBs (Host to GuC - H2G, GuC to Host
 * - G2H) are a message interface between the i915 and GuC.
 *
 * Context registration:
 * Before a context can be submitted it must be registered with the GuC via a
 * H2G. A unique guc_id is associated with each context. The context is either
 * registered at request creation time (normal operation) or at submission time
 * (abnormal operation, e.g. after a reset).
 *
 * Context submission:
 * The i915 updates the LRC tail value in memory. The i915 must enable the
 * scheduling of the context within the GuC for the GuC to actually consider it.
 * Therefore, the first time a disabled context is submitted we use a schedule
 * enable H2G, while follow up submissions are done via the context submit H2G,
 * which informs the GuC that a previously enabled context has new work
 * available.
 *
 * Context unpin:
 * To unpin a context a H2G is used to disable scheduling. When the
 * corresponding G2H returns indicating the scheduling disable operation has
 * completed it is safe to unpin the context. While a disable is in flight it
 * isn't safe to resubmit the context so a fence is used to stall all future
 * requests of that context until the G2H is returned.
 *
 * Context deregistration:
 * Before a context can be destroyed or if we steal its guc_id we must
 * deregister the context with the GuC via H2G. If stealing the guc_id it isn't
 * safe to submit anything to this guc_id until the deregister completes so a
 * fence is used to stall all requests associated with this guc_id until the
 * corresponding G2H returns indicating the guc_id has been deregistered.
 *
72
 * submission_state.guc_ids:
M
Matthew Brost 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
 * Unique number associated with private GuC context data passed in during
 * context registration / submission / deregistration. 64k available. Simple ida
 * is used for allocation.
 *
 * Stealing guc_ids:
 * If no guc_ids are available they can be stolen from another context at
 * request creation time if that context is unpinned. If a guc_id can't be found
 * we punt this problem to the user as we believe this is near impossible to hit
 * during normal use cases.
 *
 * Locking:
 * In the GuC submission code we have 3 basic spin locks which protect
 * everything. Details about each below.
 *
 * sched_engine->lock
 * This is the submission lock for all contexts that share an i915 schedule
 * engine (sched_engine), thus only one of the contexts which share a
 * sched_engine can be submitting at a time. Currently only one sched_engine is
 * used for all of GuC submission but that could change in the future.
 *
93
 * guc->submission_state.lock
94 95
 * Global lock for GuC submission state. Protects guc_ids and destroyed contexts
 * list.
M
Matthew Brost 已提交
96 97 98 99 100 101 102 103 104 105 106
 *
 * ce->guc_state.lock
 * Protects everything under ce->guc_state. Ensures that a context is in the
 * correct state before issuing a H2G. e.g. We don't issue a schedule disable
 * on a disabled context (bad idea), we don't issue a schedule enable when a
 * schedule disable is in flight, etc... Also protects list of inflight requests
 * on the context and the priority management state. Lock is individual to each
 * context.
 *
 * Lock ordering rules:
 * sched_engine->lock -> ce->guc_state.lock
107
 * guc->submission_state.lock -> ce->guc_state.lock
108
 *
M
Matthew Brost 已提交
109 110 111 112 113 114 115 116 117 118
 * Reset races:
 * When a full GT reset is triggered it is assumed that some G2H responses to
 * H2Gs can be lost as the GuC is also reset. Losing these G2H can prove to be
 * fatal as we do certain operations upon receiving a G2H (e.g. destroy
 * contexts, release guc_ids, etc...). When this occurs we can scrub the
 * context state and cleanup appropriately, however this is quite racey.
 * To avoid races, the reset code must disable submission before scrubbing for
 * the missing G2H, while the submission code must check for submission being
 * disabled and skip sending H2Gs and updating context states when it is. Both
 * sides must also make sure to hold the relevant locks.
119 120
 */

121 122 123 124 125 126 127
/* GuC Virtual Engine */
struct guc_virtual_engine {
	struct intel_engine_cs base;
	struct intel_context context;
};

static struct intel_context *
128 129 130 131 132 133 134
guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count,
		   unsigned long flags);

static struct intel_context *
guc_create_parallel(struct intel_engine_cs **engines,
		    unsigned int num_siblings,
		    unsigned int width);
135

136 137
#define GUC_REQUEST_SIZE 64 /* bytes */

138 139 140 141 142 143 144 145 146 147
/*
 * We reserve 1/16 of the guc_ids for multi-lrc as these need to be contiguous
 * per the GuC submission interface. A different allocation algorithm is used
 * (bitmap vs. ida) between multi-lrc and single-lrc hence the reason to
 * partition the guc_id space. We believe the number of multi-lrc contexts in
 * use should be low and 1/16 should be sufficient. Minimum of 32 guc_ids for
 * multi-lrc.
 */
#define NUMBER_MULTI_LRC_GUC_ID		(GUC_MAX_LRC_DESCRIPTORS / 16)

148 149
/*
 * Below is a set of functions which control the GuC scheduling state which
150
 * require a lock.
151 152 153
 */
#define SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER	BIT(0)
#define SCHED_STATE_DESTROYED				BIT(1)
154
#define SCHED_STATE_PENDING_DISABLE			BIT(2)
155
#define SCHED_STATE_BANNED				BIT(3)
156 157 158 159
#define SCHED_STATE_ENABLED				BIT(4)
#define SCHED_STATE_PENDING_ENABLE			BIT(5)
#define SCHED_STATE_REGISTERED				BIT(6)
#define SCHED_STATE_BLOCKED_SHIFT			7
160 161
#define SCHED_STATE_BLOCKED		BIT(SCHED_STATE_BLOCKED_SHIFT)
#define SCHED_STATE_BLOCKED_MASK	(0xfff << SCHED_STATE_BLOCKED_SHIFT)
162

163 164
static inline void init_sched_state(struct intel_context *ce)
{
165
	lockdep_assert_held(&ce->guc_state.lock);
166
	ce->guc_state.sched_state &= SCHED_STATE_BLOCKED_MASK;
167 168
}

169 170 171 172 173 174 175
__maybe_unused
static bool sched_state_is_init(struct intel_context *ce)
{
	/*
	 * XXX: Kernel contexts can have SCHED_STATE_NO_LOCK_REGISTERED after
	 * suspend.
	 */
176 177
	return !(ce->guc_state.sched_state &=
		 ~(SCHED_STATE_BLOCKED_MASK | SCHED_STATE_REGISTERED));
178 179
}

180 181 182 183 184 185 186 187 188 189
static inline bool
context_wait_for_deregister_to_register(struct intel_context *ce)
{
	return ce->guc_state.sched_state &
		SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
}

static inline void
set_context_wait_for_deregister_to_register(struct intel_context *ce)
{
190
	lockdep_assert_held(&ce->guc_state.lock);
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
	ce->guc_state.sched_state |=
		SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
}

static inline void
clr_context_wait_for_deregister_to_register(struct intel_context *ce)
{
	lockdep_assert_held(&ce->guc_state.lock);
	ce->guc_state.sched_state &=
		~SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
}

static inline bool
context_destroyed(struct intel_context *ce)
{
	return ce->guc_state.sched_state & SCHED_STATE_DESTROYED;
}

static inline void
set_context_destroyed(struct intel_context *ce)
{
	lockdep_assert_held(&ce->guc_state.lock);
	ce->guc_state.sched_state |= SCHED_STATE_DESTROYED;
}

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
static inline bool context_pending_disable(struct intel_context *ce)
{
	return ce->guc_state.sched_state & SCHED_STATE_PENDING_DISABLE;
}

static inline void set_context_pending_disable(struct intel_context *ce)
{
	lockdep_assert_held(&ce->guc_state.lock);
	ce->guc_state.sched_state |= SCHED_STATE_PENDING_DISABLE;
}

static inline void clr_context_pending_disable(struct intel_context *ce)
{
	lockdep_assert_held(&ce->guc_state.lock);
	ce->guc_state.sched_state &= ~SCHED_STATE_PENDING_DISABLE;
}

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
static inline bool context_banned(struct intel_context *ce)
{
	return ce->guc_state.sched_state & SCHED_STATE_BANNED;
}

static inline void set_context_banned(struct intel_context *ce)
{
	lockdep_assert_held(&ce->guc_state.lock);
	ce->guc_state.sched_state |= SCHED_STATE_BANNED;
}

static inline void clr_context_banned(struct intel_context *ce)
{
	lockdep_assert_held(&ce->guc_state.lock);
	ce->guc_state.sched_state &= ~SCHED_STATE_BANNED;
}

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
static inline bool context_enabled(struct intel_context *ce)
{
	return ce->guc_state.sched_state & SCHED_STATE_ENABLED;
}

static inline void set_context_enabled(struct intel_context *ce)
{
	lockdep_assert_held(&ce->guc_state.lock);
	ce->guc_state.sched_state |= SCHED_STATE_ENABLED;
}

static inline void clr_context_enabled(struct intel_context *ce)
{
	lockdep_assert_held(&ce->guc_state.lock);
	ce->guc_state.sched_state &= ~SCHED_STATE_ENABLED;
}

static inline bool context_pending_enable(struct intel_context *ce)
{
	return ce->guc_state.sched_state & SCHED_STATE_PENDING_ENABLE;
}

static inline void set_context_pending_enable(struct intel_context *ce)
{
	lockdep_assert_held(&ce->guc_state.lock);
	ce->guc_state.sched_state |= SCHED_STATE_PENDING_ENABLE;
}

static inline void clr_context_pending_enable(struct intel_context *ce)
{
	lockdep_assert_held(&ce->guc_state.lock);
	ce->guc_state.sched_state &= ~SCHED_STATE_PENDING_ENABLE;
}

static inline bool context_registered(struct intel_context *ce)
{
	return ce->guc_state.sched_state & SCHED_STATE_REGISTERED;
}

static inline void set_context_registered(struct intel_context *ce)
{
	lockdep_assert_held(&ce->guc_state.lock);
	ce->guc_state.sched_state |= SCHED_STATE_REGISTERED;
}

static inline void clr_context_registered(struct intel_context *ce)
{
	lockdep_assert_held(&ce->guc_state.lock);
	ce->guc_state.sched_state &= ~SCHED_STATE_REGISTERED;
}

301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
static inline u32 context_blocked(struct intel_context *ce)
{
	return (ce->guc_state.sched_state & SCHED_STATE_BLOCKED_MASK) >>
		SCHED_STATE_BLOCKED_SHIFT;
}

static inline void incr_context_blocked(struct intel_context *ce)
{
	lockdep_assert_held(&ce->guc_state.lock);

	ce->guc_state.sched_state += SCHED_STATE_BLOCKED;

	GEM_BUG_ON(!context_blocked(ce));	/* Overflow check */
}

static inline void decr_context_blocked(struct intel_context *ce)
{
	lockdep_assert_held(&ce->guc_state.lock);

	GEM_BUG_ON(!context_blocked(ce));	/* Underflow check */

	ce->guc_state.sched_state -= SCHED_STATE_BLOCKED;
}

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
static inline bool context_has_committed_requests(struct intel_context *ce)
{
	return !!ce->guc_state.number_committed_requests;
}

static inline void incr_context_committed_requests(struct intel_context *ce)
{
	lockdep_assert_held(&ce->guc_state.lock);
	++ce->guc_state.number_committed_requests;
	GEM_BUG_ON(ce->guc_state.number_committed_requests < 0);
}

static inline void decr_context_committed_requests(struct intel_context *ce)
{
	lockdep_assert_held(&ce->guc_state.lock);
	--ce->guc_state.number_committed_requests;
	GEM_BUG_ON(ce->guc_state.number_committed_requests < 0);
}

344 345 346 347 348 349
static struct intel_context *
request_to_scheduling_context(struct i915_request *rq)
{
	return intel_context_to_parent(rq->context);
}

350 351
static inline bool context_guc_id_invalid(struct intel_context *ce)
{
352
	return ce->guc_id.id == GUC_INVALID_LRC_ID;
353 354 355 356
}

static inline void set_context_guc_id_invalid(struct intel_context *ce)
{
357
	ce->guc_id.id = GUC_INVALID_LRC_ID;
358 359 360 361 362 363 364
}

static inline struct intel_guc *ce_to_guc(struct intel_context *ce)
{
	return &ce->engine->gt->uc.guc;
}

365 366 367 368 369
static inline struct i915_priolist *to_priolist(struct rb_node *rb)
{
	return rb_entry(rb, struct i915_priolist, node);
}

370 371
/*
 * When using multi-lrc submission a scratch memory area is reserved in the
372 373 374
 * parent's context state for the process descriptor, work queue, and handshake
 * between the parent + children contexts to insert safe preemption points
 * between each of the BBs. Currently the scratch area is sized to a page.
375 376 377
 *
 * The layout of this scratch area is below:
 * 0						guc_process_desc
378 379 380 381
 * + sizeof(struct guc_process_desc)		child go
 * + CACHELINE_BYTES				child join[0]
 * ...
 * + CACHELINE_BYTES				child join[n - 1]
382 383 384 385 386 387 388
 * ...						unused
 * PARENT_SCRATCH_SIZE / 2			work queue start
 * ...						work queue
 * PARENT_SCRATCH_SIZE - 1			work queue end
 */
#define WQ_SIZE			(PARENT_SCRATCH_SIZE / 2)
#define WQ_OFFSET		(PARENT_SCRATCH_SIZE - WQ_SIZE)
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

struct sync_semaphore {
	u32 semaphore;
	u8 unused[CACHELINE_BYTES - sizeof(u32)];
};

struct parent_scratch {
	struct guc_process_desc pdesc;

	struct sync_semaphore go;
	struct sync_semaphore join[MAX_ENGINE_INSTANCE + 1];

	u8 unused[WQ_OFFSET - sizeof(struct guc_process_desc) -
		sizeof(struct sync_semaphore) * (MAX_ENGINE_INSTANCE + 2)];

	u32 wq[WQ_SIZE / sizeof(u32)];
};

static u32 __get_parent_scratch_offset(struct intel_context *ce)
408 409 410 411 412 413 414 415
{
	GEM_BUG_ON(!ce->parallel.guc.parent_page);

	return ce->parallel.guc.parent_page * PAGE_SIZE;
}

static u32 __get_wq_offset(struct intel_context *ce)
{
416 417 418
	BUILD_BUG_ON(offsetof(struct parent_scratch, wq) != WQ_OFFSET);

	return __get_parent_scratch_offset(ce) + WQ_OFFSET;
419 420
}

421 422
static struct parent_scratch *
__get_parent_scratch(struct intel_context *ce)
423
{
424 425 426
	BUILD_BUG_ON(sizeof(struct parent_scratch) != PARENT_SCRATCH_SIZE);
	BUILD_BUG_ON(sizeof(struct sync_semaphore) != CACHELINE_BYTES);

427 428 429 430 431
	/*
	 * Need to subtract LRC_STATE_OFFSET here as the
	 * parallel.guc.parent_page is the offset into ce->state while
	 * ce->lrc_reg_reg is ce->state + LRC_STATE_OFFSET.
	 */
432
	return (struct parent_scratch *)
433
		(ce->lrc_reg_state +
434
		 ((__get_parent_scratch_offset(ce) -
435 436 437
		   LRC_STATE_OFFSET) / sizeof(u32)));
}

438 439 440 441 442 443 444 445
static struct guc_process_desc *
__get_process_desc(struct intel_context *ce)
{
	struct parent_scratch *ps = __get_parent_scratch(ce);

	return &ps->pdesc;
}

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
static u32 *get_wq_pointer(struct guc_process_desc *desc,
			   struct intel_context *ce,
			   u32 wqi_size)
{
	/*
	 * Check for space in work queue. Caching a value of head pointer in
	 * intel_context structure in order reduce the number accesses to shared
	 * GPU memory which may be across a PCIe bus.
	 */
#define AVAILABLE_SPACE	\
	CIRC_SPACE(ce->parallel.guc.wqi_tail, ce->parallel.guc.wqi_head, WQ_SIZE)
	if (wqi_size > AVAILABLE_SPACE) {
		ce->parallel.guc.wqi_head = READ_ONCE(desc->head);

		if (wqi_size > AVAILABLE_SPACE)
			return NULL;
	}
#undef AVAILABLE_SPACE

465
	return &__get_parent_scratch(ce)->wq[ce->parallel.guc.wqi_tail / sizeof(u32)];
466 467
}

468
static struct guc_lrc_desc *__get_lrc_desc(struct intel_guc *guc, u32 index)
469
{
470
	struct guc_lrc_desc *base = guc->lrc_desc_pool_vaddr;
471

472
	GEM_BUG_ON(index >= GUC_MAX_LRC_DESCRIPTORS);
473

474
	return &base[index];
475 476
}

477 478 479 480 481 482 483 484 485
static inline struct intel_context *__get_context(struct intel_guc *guc, u32 id)
{
	struct intel_context *ce = xa_load(&guc->context_lookup, id);

	GEM_BUG_ON(id >= GUC_MAX_LRC_DESCRIPTORS);

	return ce;
}

486
static int guc_lrc_desc_pool_create(struct intel_guc *guc)
487
{
488 489
	u32 size;
	int ret;
490

491 492 493 494 495 496
	size = PAGE_ALIGN(sizeof(struct guc_lrc_desc) *
			  GUC_MAX_LRC_DESCRIPTORS);
	ret = intel_guc_allocate_and_map_vma(guc, size, &guc->lrc_desc_pool,
					     (void **)&guc->lrc_desc_pool_vaddr);
	if (ret)
		return ret;
497

498
	return 0;
499 500
}

501
static void guc_lrc_desc_pool_destroy(struct intel_guc *guc)
502
{
503
	guc->lrc_desc_pool_vaddr = NULL;
504
	i915_vma_unpin_and_release(&guc->lrc_desc_pool, I915_VMA_RELEASE_MAP);
505 506
}

507 508 509 510 511
static inline bool guc_submission_initialized(struct intel_guc *guc)
{
	return !!guc->lrc_desc_pool_vaddr;
}

512 513
static inline void reset_lrc_desc(struct intel_guc *guc, u32 id)
{
514 515 516
	if (likely(guc_submission_initialized(guc))) {
		struct guc_lrc_desc *desc = __get_lrc_desc(guc, id);
		unsigned long flags;
517

518 519 520 521 522 523 524 525 526 527
		memset(desc, 0, sizeof(*desc));

		/*
		 * xarray API doesn't have xa_erase_irqsave wrapper, so calling
		 * the lower level functions directly.
		 */
		xa_lock_irqsave(&guc->context_lookup, flags);
		__xa_erase(&guc->context_lookup, id);
		xa_unlock_irqrestore(&guc->context_lookup, flags);
	}
528 529 530 531 532 533 534 535 536 537
}

static inline bool lrc_desc_registered(struct intel_guc *guc, u32 id)
{
	return __get_context(guc, id);
}

static inline void set_lrc_desc_registered(struct intel_guc *guc, u32 id,
					   struct intel_context *ce)
{
538 539 540 541 542 543 544 545 546
	unsigned long flags;

	/*
	 * xarray API doesn't have xa_save_irqsave wrapper, so calling the
	 * lower level functions directly.
	 */
	xa_lock_irqsave(&guc->context_lookup, flags);
	__xa_store(&guc->context_lookup, id, ce, GFP_ATOMIC);
	xa_unlock_irqrestore(&guc->context_lookup, flags);
547 548
}

549 550 551 552 553 554
static void decr_outstanding_submission_g2h(struct intel_guc *guc)
{
	if (atomic_dec_and_test(&guc->outstanding_submission_g2h))
		wake_up_all(&guc->ct.wq);
}

555 556 557 558 559 560
static int guc_submission_send_busy_loop(struct intel_guc *guc,
					 const u32 *action,
					 u32 len,
					 u32 g2h_len_dw,
					 bool loop)
{
561 562 563 564 565 566
	/*
	 * We always loop when a send requires a reply (i.e. g2h_len_dw > 0),
	 * so we don't handle the case where we don't get a reply because we
	 * aborted the send due to the channel being busy.
	 */
	GEM_BUG_ON(g2h_len_dw && !loop);
567

568
	if (g2h_len_dw)
569 570
		atomic_inc(&guc->outstanding_submission_g2h);

571
	return intel_guc_send_busy_loop(guc, action, len, g2h_len_dw, loop);
572 573
}

574 575 576 577
int intel_guc_wait_for_pending_msg(struct intel_guc *guc,
				   atomic_t *wait_var,
				   bool interruptible,
				   long timeout)
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
{
	const int state = interruptible ?
		TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
	DEFINE_WAIT(wait);

	might_sleep();
	GEM_BUG_ON(timeout < 0);

	if (!atomic_read(wait_var))
		return 0;

	if (!timeout)
		return -ETIME;

	for (;;) {
		prepare_to_wait(&guc->ct.wq, &wait, state);

		if (!atomic_read(wait_var))
			break;

		if (signal_pending_state(state, current)) {
			timeout = -EINTR;
			break;
		}

		if (!timeout) {
			timeout = -ETIME;
			break;
		}

		timeout = io_schedule_timeout(timeout);
	}
	finish_wait(&guc->ct.wq, &wait);

	return (timeout < 0) ? timeout : 0;
}

int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout)
{
	if (!intel_uc_uses_guc_submission(&guc_to_gt(guc)->uc))
		return 0;

620 621 622
	return intel_guc_wait_for_pending_msg(guc,
					      &guc->outstanding_submission_g2h,
					      true, timeout);
623 624
}

625 626
static int guc_lrc_desc_pin(struct intel_context *ce, bool loop);

627
static int __guc_add_request(struct intel_guc *guc, struct i915_request *rq)
628
{
629
	int err = 0;
630
	struct intel_context *ce = request_to_scheduling_context(rq);
631 632
	u32 action[3];
	int len = 0;
633
	u32 g2h_len_dw = 0;
634
	bool enabled;
635

636 637
	lockdep_assert_held(&rq->engine->sched_engine->lock);

638 639 640 641 642 643 644
	/*
	 * Corner case where requests were sitting in the priority list or a
	 * request resubmitted after the context was banned.
	 */
	if (unlikely(intel_context_is_banned(ce))) {
		i915_request_put(i915_request_mark_eio(rq));
		intel_engine_signal_breadcrumbs(ce->engine);
645
		return 0;
646 647
	}

648
	GEM_BUG_ON(!atomic_read(&ce->guc_id.ref));
649 650
	GEM_BUG_ON(context_guc_id_invalid(ce));

651 652
	spin_lock(&ce->guc_state.lock);

653 654
	/*
	 * The request / context will be run on the hardware when scheduling
655 656
	 * gets enabled in the unblock. For multi-lrc we still submit the
	 * context to move the LRC tails.
657
	 */
658
	if (unlikely(context_blocked(ce) && !intel_context_is_parent(ce)))
659 660
		goto out;

661
	enabled = context_enabled(ce) || context_blocked(ce);
662

663 664
	if (!enabled) {
		action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
665
		action[len++] = ce->guc_id.id;
666
		action[len++] = GUC_CONTEXT_ENABLE;
667 668
		set_context_pending_enable(ce);
		intel_context_get(ce);
669
		g2h_len_dw = G2H_LEN_DW_SCHED_CONTEXT_MODE_SET;
670 671
	} else {
		action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT;
672
		action[len++] = ce->guc_id.id;
673
	}
674

675
	err = intel_guc_send_nb(guc, action, len, g2h_len_dw);
676
	if (!enabled && !err) {
677
		trace_intel_context_sched_enable(ce);
678
		atomic_inc(&guc->outstanding_submission_g2h);
679
		set_context_enabled(ce);
680 681 682 683 684 685 686 687 688 689 690 691

		/*
		 * Without multi-lrc KMD does the submission step (moving the
		 * lrc tail) so enabling scheduling is sufficient to submit the
		 * context. This isn't the case in multi-lrc submission as the
		 * GuC needs to move the tails, hence the need for another H2G
		 * to submit a multi-lrc context after enabling scheduling.
		 */
		if (intel_context_is_parent(ce)) {
			action[0] = INTEL_GUC_ACTION_SCHED_CONTEXT;
			err = intel_guc_send_nb(guc, action, len - 1, 0);
		}
692 693 694 695
	} else if (!enabled) {
		clr_context_pending_enable(ce);
		intel_context_put(ce);
	}
696 697
	if (likely(!err))
		trace_i915_request_guc_submit(rq);
698

699
out:
700
	spin_unlock(&ce->guc_state.lock);
701
	return err;
702 703
}

704 705 706 707 708 709 710 711 712 713 714 715
static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
{
	int ret = __guc_add_request(guc, rq);

	if (unlikely(ret == -EBUSY)) {
		guc->stalled_request = rq;
		guc->submission_stall_reason = STALL_ADD_REQUEST;
	}

	return ret;
}

716 717 718 719 720 721
static inline void guc_set_lrc_tail(struct i915_request *rq)
{
	rq->context->lrc_reg_state[CTX_RING_TAIL] =
		intel_ring_set_tail(rq->ring, rq->tail);
}

722
static inline int rq_prio(const struct i915_request *rq)
723
{
724
	return rq->sched.attr.priority;
725 726
}

727 728
static bool is_multi_lrc_rq(struct i915_request *rq)
{
729
	return intel_context_is_parallel(rq->context);
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
}

static bool can_merge_rq(struct i915_request *rq,
			 struct i915_request *last)
{
	return request_to_scheduling_context(rq) ==
		request_to_scheduling_context(last);
}

static u32 wq_space_until_wrap(struct intel_context *ce)
{
	return (WQ_SIZE - ce->parallel.guc.wqi_tail);
}

static void write_wqi(struct guc_process_desc *desc,
		      struct intel_context *ce,
		      u32 wqi_size)
{
	BUILD_BUG_ON(!is_power_of_2(WQ_SIZE));

	/*
	 * Ensure WQI are visible before updating tail
	 */
	intel_guc_write_barrier(ce_to_guc(ce));

	ce->parallel.guc.wqi_tail = (ce->parallel.guc.wqi_tail + wqi_size) &
		(WQ_SIZE - 1);
	WRITE_ONCE(desc->tail, ce->parallel.guc.wqi_tail);
}

static int guc_wq_noop_append(struct intel_context *ce)
{
	struct guc_process_desc *desc = __get_process_desc(ce);
	u32 *wqi = get_wq_pointer(desc, ce, wq_space_until_wrap(ce));
	u32 len_dw = wq_space_until_wrap(ce) / sizeof(u32) - 1;

	if (!wqi)
		return -EBUSY;

	GEM_BUG_ON(!FIELD_FIT(WQ_LEN_MASK, len_dw));

	*wqi = FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_NOOP) |
		FIELD_PREP(WQ_LEN_MASK, len_dw);
	ce->parallel.guc.wqi_tail = 0;

	return 0;
}

static int __guc_wq_item_append(struct i915_request *rq)
{
	struct intel_context *ce = request_to_scheduling_context(rq);
	struct intel_context *child;
	struct guc_process_desc *desc = __get_process_desc(ce);
	unsigned int wqi_size = (ce->parallel.number_children + 4) *
		sizeof(u32);
	u32 *wqi;
	u32 len_dw = (wqi_size / sizeof(u32)) - 1;
	int ret;

	/* Ensure context is in correct state updating work queue */
	GEM_BUG_ON(!atomic_read(&ce->guc_id.ref));
	GEM_BUG_ON(context_guc_id_invalid(ce));
	GEM_BUG_ON(context_wait_for_deregister_to_register(ce));
	GEM_BUG_ON(!lrc_desc_registered(ce_to_guc(ce), ce->guc_id.id));

	/* Insert NOOP if this work queue item will wrap the tail pointer. */
	if (wqi_size > wq_space_until_wrap(ce)) {
		ret = guc_wq_noop_append(ce);
		if (ret)
			return ret;
	}

	wqi = get_wq_pointer(desc, ce, wqi_size);
	if (!wqi)
		return -EBUSY;

	GEM_BUG_ON(!FIELD_FIT(WQ_LEN_MASK, len_dw));

	*wqi++ = FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_MULTI_LRC) |
		FIELD_PREP(WQ_LEN_MASK, len_dw);
	*wqi++ = ce->lrc.lrca;
	*wqi++ = FIELD_PREP(WQ_GUC_ID_MASK, ce->guc_id.id) |
	       FIELD_PREP(WQ_RING_TAIL_MASK, ce->ring->tail / sizeof(u64));
	*wqi++ = 0;	/* fence_id */
	for_each_child(ce, child)
		*wqi++ = child->ring->tail / sizeof(u64);

	write_wqi(desc, ce, wqi_size);

	return 0;
}

static int guc_wq_item_append(struct intel_guc *guc,
			      struct i915_request *rq)
{
	struct intel_context *ce = request_to_scheduling_context(rq);
	int ret = 0;

	if (likely(!intel_context_is_banned(ce))) {
		ret = __guc_wq_item_append(rq);

		if (unlikely(ret == -EBUSY)) {
			guc->stalled_request = rq;
			guc->submission_stall_reason = STALL_MOVE_LRC_TAIL;
		}
	}

	return ret;
}

static bool multi_lrc_submit(struct i915_request *rq)
{
	struct intel_context *ce = request_to_scheduling_context(rq);

	intel_ring_set_tail(rq->ring, rq->tail);

	/*
	 * We expect the front end (execbuf IOCTL) to set this flag on the last
	 * request generated from a multi-BB submission. This indicates to the
	 * backend (GuC interface) that we should submit this context thus
	 * submitting all the requests generated in parallel.
	 */
	return test_bit(I915_FENCE_FLAG_SUBMIT_PARALLEL, &rq->fence.flags) ||
		intel_context_is_banned(ce);
}

856
static int guc_dequeue_one_context(struct intel_guc *guc)
857
{
858 859
	struct i915_sched_engine * const sched_engine = guc->sched_engine;
	struct i915_request *last = NULL;
860
	bool submit = false;
861
	struct rb_node *rb;
862
	int ret;
863

864
	lockdep_assert_held(&sched_engine->lock);
865

866 867 868
	if (guc->stalled_request) {
		submit = true;
		last = guc->stalled_request;
869 870 871 872 873 874 875 876 877 878 879

		switch (guc->submission_stall_reason) {
		case STALL_REGISTER_CONTEXT:
			goto register_context;
		case STALL_MOVE_LRC_TAIL:
			goto move_lrc_tail;
		case STALL_ADD_REQUEST:
			goto add_request;
		default:
			MISSING_CASE(guc->submission_stall_reason);
		}
880 881
	}

882
	while ((rb = rb_first_cached(&sched_engine->queue))) {
883
		struct i915_priolist *p = to_priolist(rb);
884
		struct i915_request *rq, *rn;
885

886
		priolist_for_each_request_consume(rq, rn, p) {
887 888
			if (last && !can_merge_rq(rq, last))
				goto register_context;
889

890
			list_del_init(&rq->sched.link);
891

892
			__i915_request_submit(rq);
893 894

			trace_i915_request_in(rq, 0);
895
			last = rq;
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910

			if (is_multi_lrc_rq(rq)) {
				/*
				 * We need to coalesce all multi-lrc requests in
				 * a relationship into a single H2G. We are
				 * guaranteed that all of these requests will be
				 * submitted sequentially.
				 */
				if (multi_lrc_submit(rq)) {
					submit = true;
					goto register_context;
				}
			} else {
				submit = true;
			}
911 912
		}

913
		rb_erase_cached(&p->node, &sched_engine->queue);
914
		i915_priolist_free(p);
915
	}
916 917

register_context:
918
	if (submit) {
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
		struct intel_context *ce = request_to_scheduling_context(last);

		if (unlikely(!lrc_desc_registered(guc, ce->guc_id.id) &&
			     !intel_context_is_banned(ce))) {
			ret = guc_lrc_desc_pin(ce, false);
			if (unlikely(ret == -EPIPE)) {
				goto deadlk;
			} else if (ret == -EBUSY) {
				guc->stalled_request = last;
				guc->submission_stall_reason =
					STALL_REGISTER_CONTEXT;
				goto schedule_tasklet;
			} else if (ret != 0) {
				GEM_WARN_ON(ret);	/* Unexpected */
				goto deadlk;
			}
		}

move_lrc_tail:
		if (is_multi_lrc_rq(last)) {
			ret = guc_wq_item_append(guc, last);
			if (ret == -EBUSY) {
				goto schedule_tasklet;
			} else if (ret != 0) {
				GEM_WARN_ON(ret);	/* Unexpected */
				goto deadlk;
			}
		} else {
			guc_set_lrc_tail(last);
		}

add_request:
951
		ret = guc_add_request(guc, last);
952 953 954 955 956 957
		if (unlikely(ret == -EPIPE)) {
			goto deadlk;
		} else if (ret == -EBUSY) {
			goto schedule_tasklet;
		} else if (ret != 0) {
			GEM_WARN_ON(ret);	/* Unexpected */
958
			goto deadlk;
959
		}
960
	}
961 962

	guc->stalled_request = NULL;
963
	guc->submission_stall_reason = STALL_NONE;
964
	return submit;
965 966 967 968 969

deadlk:
	sched_engine->tasklet.callback = NULL;
	tasklet_disable_nosync(&sched_engine->tasklet);
	return false;
970 971 972 973

schedule_tasklet:
	tasklet_schedule(&sched_engine->tasklet);
	return false;
974 975
}

976
static void guc_submission_tasklet(struct tasklet_struct *t)
977
{
978 979
	struct i915_sched_engine *sched_engine =
		from_tasklet(sched_engine, t, tasklet);
980
	unsigned long flags;
981
	bool loop;
982

983
	spin_lock_irqsave(&sched_engine->lock, flags);
984

985 986 987
	do {
		loop = guc_dequeue_one_context(sched_engine->private_data);
	} while (loop);
988

989
	i915_sched_engine_reset_on_empty(sched_engine);
990

991
	spin_unlock_irqrestore(&sched_engine->lock, flags);
992 993
}

994 995
static void cs_irq_handler(struct intel_engine_cs *engine, u16 iir)
{
996
	if (iir & GT_RENDER_USER_INTERRUPT)
997 998 999
		intel_engine_signal_breadcrumbs(engine);
}

1000 1001 1002
static void __guc_context_destroy(struct intel_context *ce);
static void release_guc_id(struct intel_guc *guc, struct intel_context *ce);
static void guc_signal_context_fence(struct intel_context *ce);
1003
static void guc_cancel_context_requests(struct intel_context *ce);
1004
static void guc_blocked_fence_complete(struct intel_context *ce);
1005 1006 1007 1008 1009

static void scrub_guc_desc_for_outstanding_g2h(struct intel_guc *guc)
{
	struct intel_context *ce;
	unsigned long index, flags;
1010
	bool pending_disable, pending_enable, deregister, destroyed, banned;
1011

1012
	xa_lock_irqsave(&guc->context_lookup, flags);
1013
	xa_for_each(&guc->context_lookup, index, ce) {
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
		/*
		 * Corner case where the ref count on the object is zero but and
		 * deregister G2H was lost. In this case we don't touch the ref
		 * count and finish the destroy of the context.
		 */
		bool do_put = kref_get_unless_zero(&ce->ref);

		xa_unlock(&guc->context_lookup);

		spin_lock(&ce->guc_state.lock);
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035

		/*
		 * Once we are at this point submission_disabled() is guaranteed
		 * to be visible to all callers who set the below flags (see above
		 * flush and flushes in reset_prepare). If submission_disabled()
		 * is set, the caller shouldn't set these flags.
		 */

		destroyed = context_destroyed(ce);
		pending_enable = context_pending_enable(ce);
		pending_disable = context_pending_disable(ce);
		deregister = context_wait_for_deregister_to_register(ce);
1036
		banned = context_banned(ce);
1037 1038
		init_sched_state(ce);

1039 1040 1041
		spin_unlock(&ce->guc_state.lock);

		GEM_BUG_ON(!do_put && !destroyed);
1042

1043
		if (pending_enable || destroyed || deregister) {
1044
			decr_outstanding_submission_g2h(guc);
1045 1046 1047
			if (deregister)
				guc_signal_context_fence(ce);
			if (destroyed) {
1048
				intel_gt_pm_put_async(guc_to_gt(guc));
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
				release_guc_id(guc, ce);
				__guc_context_destroy(ce);
			}
			if (pending_enable || deregister)
				intel_context_put(ce);
		}

		/* Not mutualy exclusive with above if statement. */
		if (pending_disable) {
			guc_signal_context_fence(ce);
1059 1060 1061 1062
			if (banned) {
				guc_cancel_context_requests(ce);
				intel_engine_signal_breadcrumbs(ce->engine);
			}
1063
			intel_context_sched_disable_unpin(ce);
1064
			decr_outstanding_submission_g2h(guc);
1065 1066

			spin_lock(&ce->guc_state.lock);
1067
			guc_blocked_fence_complete(ce);
1068
			spin_unlock(&ce->guc_state.lock);
1069

1070 1071
			intel_context_put(ce);
		}
1072 1073 1074 1075

		if (do_put)
			intel_context_put(ce);
		xa_lock(&guc->context_lookup);
1076
	}
1077
	xa_unlock_irqrestore(&guc->context_lookup, flags);
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
}

static inline bool
submission_disabled(struct intel_guc *guc)
{
	struct i915_sched_engine * const sched_engine = guc->sched_engine;

	return unlikely(!sched_engine ||
			!__tasklet_is_enabled(&sched_engine->tasklet));
}

static void disable_submission(struct intel_guc *guc)
{
	struct i915_sched_engine * const sched_engine = guc->sched_engine;

	if (__tasklet_is_enabled(&sched_engine->tasklet)) {
		GEM_BUG_ON(!guc->ct.enabled);
		__tasklet_disable_sync_once(&sched_engine->tasklet);
		sched_engine->tasklet.callback = NULL;
	}
}

static void enable_submission(struct intel_guc *guc)
{
	struct i915_sched_engine * const sched_engine = guc->sched_engine;
	unsigned long flags;

	spin_lock_irqsave(&guc->sched_engine->lock, flags);
	sched_engine->tasklet.callback = guc_submission_tasklet;
	wmb();	/* Make sure callback visible */
	if (!__tasklet_is_enabled(&sched_engine->tasklet) &&
	    __tasklet_enable(&sched_engine->tasklet)) {
		GEM_BUG_ON(!guc->ct.enabled);

		/* And kick in case we missed a new request submission. */
		tasklet_hi_schedule(&sched_engine->tasklet);
	}
	spin_unlock_irqrestore(&guc->sched_engine->lock, flags);
}

static void guc_flush_submissions(struct intel_guc *guc)
{
	struct i915_sched_engine * const sched_engine = guc->sched_engine;
	unsigned long flags;

	spin_lock_irqsave(&sched_engine->lock, flags);
	spin_unlock_irqrestore(&sched_engine->lock, flags);
}

1127 1128
static void guc_flush_destroyed_contexts(struct intel_guc *guc);

1129
void intel_guc_submission_reset_prepare(struct intel_guc *guc)
1130
{
1131 1132 1133 1134 1135 1136 1137
	int i;

	if (unlikely(!guc_submission_initialized(guc))) {
		/* Reset called during driver load? GuC not yet initialised! */
		return;
	}

1138
	intel_gt_park_heartbeats(guc_to_gt(guc));
1139 1140 1141 1142 1143 1144 1145 1146
	disable_submission(guc);
	guc->interrupts.disable(guc);

	/* Flush IRQ handler */
	spin_lock_irq(&guc_to_gt(guc)->irq_lock);
	spin_unlock_irq(&guc_to_gt(guc)->irq_lock);

	guc_flush_submissions(guc);
1147
	guc_flush_destroyed_contexts(guc);
1148 1149

	/*
1150 1151 1152 1153
	 * Handle any outstanding G2Hs before reset. Call IRQ handler directly
	 * each pass as interrupt have been disabled. We always scrub for
	 * outstanding G2H as it is possible for outstanding_submission_g2h to
	 * be incremented after the context state update.
1154
	 */
1155 1156 1157
	for (i = 0; i < 4 && atomic_read(&guc->outstanding_submission_g2h); ++i) {
		intel_guc_to_host_event_handler(guc);
#define wait_for_reset(guc, wait_var) \
1158
		intel_guc_wait_for_pending_msg(guc, wait_var, false, (HZ / 20))
1159 1160 1161 1162
		do {
			wait_for_reset(guc, &guc->outstanding_submission_g2h);
		} while (!list_empty(&guc->ct.requests.incoming));
	}
1163

1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
	scrub_guc_desc_for_outstanding_g2h(guc);
}

static struct intel_engine_cs *
guc_virtual_get_sibling(struct intel_engine_cs *ve, unsigned int sibling)
{
	struct intel_engine_cs *engine;
	intel_engine_mask_t tmp, mask = ve->mask;
	unsigned int num_siblings = 0;

	for_each_engine_masked(engine, ve->gt, mask, tmp)
		if (num_siblings++ == sibling)
			return engine;

	return NULL;
}

static inline struct intel_engine_cs *
__context_to_physical_engine(struct intel_context *ce)
{
	struct intel_engine_cs *engine = ce->engine;

	if (intel_engine_is_virtual(engine))
		engine = guc_virtual_get_sibling(engine, 0);

	return engine;
1190 1191
}

1192
static void guc_reset_state(struct intel_context *ce, u32 head, bool scrub)
1193
{
1194 1195
	struct intel_engine_cs *engine = __context_to_physical_engine(ce);

1196 1197 1198
	if (intel_context_is_banned(ce))
		return;

1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
	GEM_BUG_ON(!intel_context_is_pinned(ce));

	/*
	 * We want a simple context + ring to execute the breadcrumb update.
	 * We cannot rely on the context being intact across the GPU hang,
	 * so clear it and rebuild just what we need for the breadcrumb.
	 * All pending requests for this context will be zapped, and any
	 * future request will be after userspace has had the opportunity
	 * to recreate its own state.
	 */
	if (scrub)
		lrc_init_regs(ce, engine, true);

	/* Rerun the request; its payload has been neutered (if guilty). */
	lrc_update_regs(ce, engine, head);
}

1216
static void guc_reset_nop(struct intel_engine_cs *engine)
1217
{
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
}

static void guc_rewind_nop(struct intel_engine_cs *engine, bool stalled)
{
}

static void
__unwind_incomplete_requests(struct intel_context *ce)
{
	struct i915_request *rq, *rn;
	struct list_head *pl;
	int prio = I915_PRIORITY_INVALID;
	struct i915_sched_engine * const sched_engine =
		ce->engine->sched_engine;
	unsigned long flags;

	spin_lock_irqsave(&sched_engine->lock, flags);
1235
	spin_lock(&ce->guc_state.lock);
1236
	list_for_each_entry_safe_reverse(rq, rn,
1237
					 &ce->guc_state.requests,
1238
					 sched.link) {
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
		if (i915_request_completed(rq))
			continue;

		list_del_init(&rq->sched.link);
		__i915_request_unsubmit(rq);

		/* Push the request back into the queue for later resubmission. */
		GEM_BUG_ON(rq_prio(rq) == I915_PRIORITY_INVALID);
		if (rq_prio(rq) != prio) {
			prio = rq_prio(rq);
			pl = i915_sched_lookup_priolist(sched_engine, prio);
		}
		GEM_BUG_ON(i915_sched_engine_is_empty(sched_engine));

1253
		list_add(&rq->sched.link, pl);
1254 1255
		set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
	}
1256
	spin_unlock(&ce->guc_state.lock);
1257 1258 1259 1260 1261
	spin_unlock_irqrestore(&sched_engine->lock, flags);
}

static void __guc_reset_context(struct intel_context *ce, bool stalled)
{
1262
	bool local_stalled;
1263
	struct i915_request *rq;
1264
	unsigned long flags;
1265
	u32 head;
1266
	int i, number_children = ce->parallel.number_children;
1267
	bool skip = false;
1268 1269 1270
	struct intel_context *parent = ce;

	GEM_BUG_ON(intel_context_is_child(ce));
1271

1272 1273
	intel_context_get(ce);

1274
	/*
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
	 * GuC will implicitly mark the context as non-schedulable when it sends
	 * the reset notification. Make sure our state reflects this change. The
	 * context will be marked enabled on resubmission.
	 *
	 * XXX: If the context is reset as a result of the request cancellation
	 * this G2H is received after the schedule disable complete G2H which is
	 * wrong as this creates a race between the request cancellation code
	 * re-submitting the context and this G2H handler. This is a bug in the
	 * GuC but can be worked around in the meantime but converting this to a
	 * NOP if a pending enable is in flight as this indicates that a request
	 * cancellation has occurred.
1286
	 */
1287 1288 1289 1290 1291 1292 1293 1294
	spin_lock_irqsave(&ce->guc_state.lock, flags);
	if (likely(!context_pending_enable(ce)))
		clr_context_enabled(ce);
	else
		skip = true;
	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
	if (unlikely(skip))
		goto out_put;
1295

1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
	/*
	 * For each context in the relationship find the hanging request
	 * resetting each context / request as needed
	 */
	for (i = 0; i < number_children + 1; ++i) {
		if (!intel_context_is_pinned(ce))
			goto next_context;

		local_stalled = false;
		rq = intel_context_find_active_request(ce);
		if (!rq) {
			head = ce->ring->tail;
			goto out_replay;
		}
1310

1311 1312
		if (i915_request_started(rq))
			local_stalled = true;
1313

1314 1315
		GEM_BUG_ON(i915_active_is_idle(&ce->active));
		head = intel_ring_wrap(ce->ring, rq->head);
1316

1317
		__i915_request_reset(rq, local_stalled && stalled);
1318
out_replay:
1319 1320 1321 1322 1323 1324 1325
		guc_reset_state(ce, head, local_stalled && stalled);
next_context:
		if (i != number_children)
			ce = list_next_entry(ce, parallel.child_link);
	}

	__unwind_incomplete_requests(parent);
1326
out_put:
1327
	intel_context_put(parent);
1328 1329 1330 1331 1332 1333
}

void intel_guc_submission_reset(struct intel_guc *guc, bool stalled)
{
	struct intel_context *ce;
	unsigned long index;
1334
	unsigned long flags;
1335 1336 1337 1338 1339 1340

	if (unlikely(!guc_submission_initialized(guc))) {
		/* Reset called during driver load? GuC not yet initialised! */
		return;
	}

1341 1342 1343 1344 1345 1346 1347
	xa_lock_irqsave(&guc->context_lookup, flags);
	xa_for_each(&guc->context_lookup, index, ce) {
		if (!kref_get_unless_zero(&ce->ref))
			continue;

		xa_unlock(&guc->context_lookup);

1348 1349
		if (intel_context_is_pinned(ce) &&
		    !intel_context_is_child(ce))
1350 1351
			__guc_reset_context(ce, stalled);

1352 1353 1354 1355 1356 1357
		intel_context_put(ce);

		xa_lock(&guc->context_lookup);
	}
	xa_unlock_irqrestore(&guc->context_lookup, flags);

1358 1359
	/* GuC is blown away, drop all references to contexts */
	xa_destroy(&guc->context_lookup);
1360 1361
}

1362 1363 1364 1365 1366 1367 1368 1369
static void guc_cancel_context_requests(struct intel_context *ce)
{
	struct i915_sched_engine *sched_engine = ce_to_guc(ce)->sched_engine;
	struct i915_request *rq;
	unsigned long flags;

	/* Mark all executing requests as skipped. */
	spin_lock_irqsave(&sched_engine->lock, flags);
1370 1371
	spin_lock(&ce->guc_state.lock);
	list_for_each_entry(rq, &ce->guc_state.requests, sched.link)
1372
		i915_request_put(i915_request_mark_eio(rq));
1373
	spin_unlock(&ce->guc_state.lock);
1374 1375 1376 1377 1378
	spin_unlock_irqrestore(&sched_engine->lock, flags);
}

static void
guc_cancel_sched_engine_requests(struct i915_sched_engine *sched_engine)
1379 1380 1381 1382 1383
{
	struct i915_request *rq, *rn;
	struct rb_node *rb;
	unsigned long flags;

1384
	/* Can be called during boot if GuC fails to load */
1385
	if (!sched_engine)
1386 1387
		return;

1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
	/*
	 * Before we call engine->cancel_requests(), we should have exclusive
	 * access to the submission state. This is arranged for us by the
	 * caller disabling the interrupt generation, the tasklet and other
	 * threads that may then access the same state, giving us a free hand
	 * to reset state. However, we still need to let lockdep be aware that
	 * we know this state may be accessed in hardirq context, so we
	 * disable the irq around this manipulation and we want to keep
	 * the spinlock focused on its duties and not accidentally conflate
	 * coverage to the submission's irq state. (Similarly, although we
	 * shouldn't need to disable irq around the manipulation of the
	 * submission's irq state, we also wish to remind ourselves that
	 * it is irq state.)
	 */
1402
	spin_lock_irqsave(&sched_engine->lock, flags);
1403 1404

	/* Flush the queued requests to the timeline list (for retiring). */
1405
	while ((rb = rb_first_cached(&sched_engine->queue))) {
1406 1407
		struct i915_priolist *p = to_priolist(rb);

1408
		priolist_for_each_request_consume(rq, rn, p) {
1409
			list_del_init(&rq->sched.link);
1410

1411
			__i915_request_submit(rq);
1412 1413

			i915_request_put(i915_request_mark_eio(rq));
1414 1415
		}

1416
		rb_erase_cached(&p->node, &sched_engine->queue);
1417 1418 1419 1420 1421
		i915_priolist_free(p);
	}

	/* Remaining _unready_ requests will be nop'ed when submitted */

1422 1423
	sched_engine->queue_priority_hint = INT_MIN;
	sched_engine->queue = RB_ROOT_CACHED;
1424

1425
	spin_unlock_irqrestore(&sched_engine->lock, flags);
1426 1427
}

1428
void intel_guc_submission_cancel_requests(struct intel_guc *guc)
1429
{
1430 1431
	struct intel_context *ce;
	unsigned long index;
1432 1433 1434 1435 1436 1437 1438 1439
	unsigned long flags;

	xa_lock_irqsave(&guc->context_lookup, flags);
	xa_for_each(&guc->context_lookup, index, ce) {
		if (!kref_get_unless_zero(&ce->ref))
			continue;

		xa_unlock(&guc->context_lookup);
1440

1441 1442
		if (intel_context_is_pinned(ce) &&
		    !intel_context_is_child(ce))
1443 1444
			guc_cancel_context_requests(ce);

1445 1446 1447 1448 1449 1450
		intel_context_put(ce);

		xa_lock(&guc->context_lookup);
	}
	xa_unlock_irqrestore(&guc->context_lookup, flags);

1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
	guc_cancel_sched_engine_requests(guc->sched_engine);

	/* GuC is blown away, drop all references to contexts */
	xa_destroy(&guc->context_lookup);
}

void intel_guc_submission_reset_finish(struct intel_guc *guc)
{
	/* Reset called during driver load or during wedge? */
	if (unlikely(!guc_submission_initialized(guc) ||
		     test_bit(I915_WEDGED, &guc_to_gt(guc)->reset.flags))) {
		return;
	}
1464

1465 1466 1467 1468 1469 1470 1471 1472 1473
	/*
	 * Technically possible for either of these values to be non-zero here,
	 * but very unlikely + harmless. Regardless let's add a warn so we can
	 * see in CI if this happens frequently / a precursor to taking down the
	 * machine.
	 */
	GEM_WARN_ON(atomic_read(&guc->outstanding_submission_g2h));
	atomic_set(&guc->outstanding_submission_g2h, 0);

1474
	intel_guc_global_policies_update(guc);
1475
	enable_submission(guc);
1476
	intel_gt_unpark_heartbeats(guc_to_gt(guc));
1477 1478
}

1479 1480
static void destroyed_worker_func(struct work_struct *w);

1481
/*
1482 1483
 * Set up the memory resources to be shared with the GuC (via the GGTT)
 * at firmware loading time.
1484
 */
1485
int intel_guc_submission_init(struct intel_guc *guc)
1486
{
1487
	int ret;
1488

1489
	if (guc->lrc_desc_pool)
1490
		return 0;
1491

1492
	ret = guc_lrc_desc_pool_create(guc);
1493 1494
	if (ret)
		return ret;
1495 1496 1497 1498
	/*
	 * Keep static analysers happy, let them know that we allocated the
	 * vma after testing that it didn't exist earlier.
	 */
1499
	GEM_BUG_ON(!guc->lrc_desc_pool);
1500

1501 1502
	xa_init_flags(&guc->context_lookup, XA_FLAGS_LOCK_IRQ);

1503 1504 1505
	spin_lock_init(&guc->submission_state.lock);
	INIT_LIST_HEAD(&guc->submission_state.guc_id_list);
	ida_init(&guc->submission_state.guc_ids);
1506 1507 1508
	INIT_LIST_HEAD(&guc->submission_state.destroyed_contexts);
	INIT_WORK(&guc->submission_state.destroyed_worker,
		  destroyed_worker_func);
1509

1510 1511 1512 1513 1514
	guc->submission_state.guc_ids_bitmap =
		bitmap_zalloc(NUMBER_MULTI_LRC_GUC_ID, GFP_KERNEL);
	if (!guc->submission_state.guc_ids_bitmap)
		return -ENOMEM;

1515
	return 0;
1516 1517
}

1518
void intel_guc_submission_fini(struct intel_guc *guc)
1519
{
1520 1521 1522
	if (!guc->lrc_desc_pool)
		return;

1523
	guc_flush_destroyed_contexts(guc);
1524 1525
	guc_lrc_desc_pool_destroy(guc);
	i915_sched_engine_put(guc->sched_engine);
1526
	bitmap_free(guc->submission_state.guc_ids_bitmap);
1527 1528
}

1529 1530 1531
static inline void queue_request(struct i915_sched_engine *sched_engine,
				 struct i915_request *rq,
				 int prio)
1532
{
1533 1534 1535 1536
	GEM_BUG_ON(!list_empty(&rq->sched.link));
	list_add_tail(&rq->sched.link,
		      i915_sched_lookup_priolist(sched_engine, prio));
	set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
1537
	tasklet_hi_schedule(&sched_engine->tasklet);
1538 1539 1540 1541 1542
}

static int guc_bypass_tasklet_submit(struct intel_guc *guc,
				     struct i915_request *rq)
{
1543
	int ret = 0;
1544 1545 1546 1547 1548

	__i915_request_submit(rq);

	trace_i915_request_in(rq, 0);

1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
	if (is_multi_lrc_rq(rq)) {
		if (multi_lrc_submit(rq)) {
			ret = guc_wq_item_append(guc, rq);
			if (!ret)
				ret = guc_add_request(guc, rq);
		}
	} else {
		guc_set_lrc_tail(rq);
		ret = guc_add_request(guc, rq);
	}
1559

1560 1561 1562
	if (unlikely(ret == -EPIPE))
		disable_submission(guc);

1563 1564 1565
	return ret;
}

1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
static bool need_tasklet(struct intel_guc *guc, struct i915_request *rq)
{
	struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
	struct intel_context *ce = request_to_scheduling_context(rq);

	return submission_disabled(guc) || guc->stalled_request ||
		!i915_sched_engine_is_empty(sched_engine) ||
		!lrc_desc_registered(guc, ce->guc_id.id);
}

1576 1577 1578 1579 1580 1581 1582 1583 1584
static void guc_submit_request(struct i915_request *rq)
{
	struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
	struct intel_guc *guc = &rq->engine->gt->uc.guc;
	unsigned long flags;

	/* Will be called from irq-context when using foreign fences. */
	spin_lock_irqsave(&sched_engine->lock, flags);

1585
	if (need_tasklet(guc, rq))
1586 1587 1588 1589 1590 1591 1592
		queue_request(sched_engine, rq, rq_prio(rq));
	else if (guc_bypass_tasklet_submit(guc, rq) == -EBUSY)
		tasklet_hi_schedule(&sched_engine->tasklet);

	spin_unlock_irqrestore(&sched_engine->lock, flags);
}

1593
static int new_guc_id(struct intel_guc *guc, struct intel_context *ce)
1594
{
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
	int ret;

	GEM_BUG_ON(intel_context_is_child(ce));

	if (intel_context_is_parent(ce))
		ret = bitmap_find_free_region(guc->submission_state.guc_ids_bitmap,
					      NUMBER_MULTI_LRC_GUC_ID,
					      order_base_2(ce->parallel.number_children
							   + 1));
	else
		ret = ida_simple_get(&guc->submission_state.guc_ids,
				     NUMBER_MULTI_LRC_GUC_ID,
				     GUC_MAX_LRC_DESCRIPTORS,
				     GFP_KERNEL | __GFP_RETRY_MAYFAIL |
				     __GFP_NOWARN);
	if (unlikely(ret < 0))
		return ret;

	ce->guc_id.id = ret;
	return 0;
1615 1616 1617 1618
}

static void __release_guc_id(struct intel_guc *guc, struct intel_context *ce)
{
1619 1620
	GEM_BUG_ON(intel_context_is_child(ce));

1621
	if (!context_guc_id_invalid(ce)) {
1622 1623 1624 1625 1626 1627 1628 1629
		if (intel_context_is_parent(ce))
			bitmap_release_region(guc->submission_state.guc_ids_bitmap,
					      ce->guc_id.id,
					      order_base_2(ce->parallel.number_children
							   + 1));
		else
			ida_simple_remove(&guc->submission_state.guc_ids,
					  ce->guc_id.id);
1630
		reset_lrc_desc(guc, ce->guc_id.id);
1631 1632
		set_context_guc_id_invalid(ce);
	}
1633 1634
	if (!list_empty(&ce->guc_id.link))
		list_del_init(&ce->guc_id.link);
1635 1636 1637 1638 1639 1640
}

static void release_guc_id(struct intel_guc *guc, struct intel_context *ce)
{
	unsigned long flags;

1641
	spin_lock_irqsave(&guc->submission_state.lock, flags);
1642
	__release_guc_id(guc, ce);
1643
	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
1644 1645
}

1646
static int steal_guc_id(struct intel_guc *guc, struct intel_context *ce)
1647
{
1648
	struct intel_context *cn;
1649

1650
	lockdep_assert_held(&guc->submission_state.lock);
1651 1652
	GEM_BUG_ON(intel_context_is_child(ce));
	GEM_BUG_ON(intel_context_is_parent(ce));
1653

1654
	if (!list_empty(&guc->submission_state.guc_id_list)) {
1655
		cn = list_first_entry(&guc->submission_state.guc_id_list,
1656
				      struct intel_context,
1657
				      guc_id.link);
1658

1659 1660 1661 1662
		GEM_BUG_ON(atomic_read(&cn->guc_id.ref));
		GEM_BUG_ON(context_guc_id_invalid(cn));
		GEM_BUG_ON(intel_context_is_child(cn));
		GEM_BUG_ON(intel_context_is_parent(cn));
1663

1664 1665
		list_del_init(&cn->guc_id.link);
		ce->guc_id = cn->guc_id;
1666 1667

		spin_lock(&ce->guc_state.lock);
1668
		clr_context_registered(cn);
1669 1670
		spin_unlock(&ce->guc_state.lock);

1671 1672 1673
		set_context_guc_id_invalid(cn);

		return 0;
1674 1675 1676 1677 1678
	} else {
		return -EAGAIN;
	}
}

1679
static int assign_guc_id(struct intel_guc *guc, struct intel_context *ce)
1680 1681 1682
{
	int ret;

1683
	lockdep_assert_held(&guc->submission_state.lock);
1684
	GEM_BUG_ON(intel_context_is_child(ce));
1685

1686
	ret = new_guc_id(guc, ce);
1687
	if (unlikely(ret < 0)) {
1688 1689 1690 1691
		if (intel_context_is_parent(ce))
			return -ENOSPC;

		ret = steal_guc_id(guc, ce);
1692 1693 1694 1695
		if (ret < 0)
			return ret;
	}

1696 1697 1698 1699 1700 1701 1702 1703
	if (intel_context_is_parent(ce)) {
		struct intel_context *child;
		int i = 1;

		for_each_child(ce, child)
			child->guc_id.id = ce->guc_id.id + i++;
	}

1704 1705 1706 1707 1708 1709 1710 1711 1712
	return 0;
}

#define PIN_GUC_ID_TRIES	4
static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
{
	int ret = 0;
	unsigned long flags, tries = PIN_GUC_ID_TRIES;

1713
	GEM_BUG_ON(atomic_read(&ce->guc_id.ref));
1714 1715

try_again:
1716
	spin_lock_irqsave(&guc->submission_state.lock, flags);
1717

1718 1719
	might_lock(&ce->guc_state.lock);

1720
	if (context_guc_id_invalid(ce)) {
1721
		ret = assign_guc_id(guc, ce);
1722 1723 1724 1725
		if (ret)
			goto out_unlock;
		ret = 1;	/* Indidcates newly assigned guc_id */
	}
1726 1727 1728
	if (!list_empty(&ce->guc_id.link))
		list_del_init(&ce->guc_id.link);
	atomic_inc(&ce->guc_id.ref);
1729 1730

out_unlock:
1731
	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
1732 1733

	/*
1734
	 * -EAGAIN indicates no guc_id are available, let's retire any
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
	 * outstanding requests to see if that frees up a guc_id. If the first
	 * retire didn't help, insert a sleep with the timeslice duration before
	 * attempting to retire more requests. Double the sleep period each
	 * subsequent pass before finally giving up. The sleep period has max of
	 * 100ms and minimum of 1ms.
	 */
	if (ret == -EAGAIN && --tries) {
		if (PIN_GUC_ID_TRIES - tries > 1) {
			unsigned int timeslice_shifted =
				ce->engine->props.timeslice_duration_ms <<
				(PIN_GUC_ID_TRIES - tries - 2);
			unsigned int max = min_t(unsigned int, 100,
						 timeslice_shifted);

			msleep(max_t(unsigned int, max, 1));
		}
		intel_gt_retire_requests(guc_to_gt(guc));
		goto try_again;
	}

	return ret;
}

static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
{
	unsigned long flags;

1762
	GEM_BUG_ON(atomic_read(&ce->guc_id.ref) < 0);
1763
	GEM_BUG_ON(intel_context_is_child(ce));
1764

1765 1766
	if (unlikely(context_guc_id_invalid(ce) ||
		     intel_context_is_parent(ce)))
1767 1768
		return;

1769
	spin_lock_irqsave(&guc->submission_state.lock, flags);
1770 1771
	if (!context_guc_id_invalid(ce) && list_empty(&ce->guc_id.link) &&
	    !atomic_read(&ce->guc_id.ref))
1772 1773 1774
		list_add_tail(&ce->guc_id.link,
			      &guc->submission_state.guc_id_list);
	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
1775 1776
}

1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
static int __guc_action_register_multi_lrc(struct intel_guc *guc,
					   struct intel_context *ce,
					   u32 guc_id,
					   u32 offset,
					   bool loop)
{
	struct intel_context *child;
	u32 action[4 + MAX_ENGINE_INSTANCE];
	int len = 0;

	GEM_BUG_ON(ce->parallel.number_children > MAX_ENGINE_INSTANCE);

	action[len++] = INTEL_GUC_ACTION_REGISTER_CONTEXT_MULTI_LRC;
	action[len++] = guc_id;
	action[len++] = ce->parallel.number_children + 1;
	action[len++] = offset;
	for_each_child(ce, child) {
		offset += sizeof(struct guc_lrc_desc);
		action[len++] = offset;
	}

	return guc_submission_send_busy_loop(guc, action, len, 0, loop);
}

1801 1802
static int __guc_action_register_context(struct intel_guc *guc,
					 u32 guc_id,
1803 1804
					 u32 offset,
					 bool loop)
1805 1806 1807 1808 1809 1810 1811
{
	u32 action[] = {
		INTEL_GUC_ACTION_REGISTER_CONTEXT,
		guc_id,
		offset,
	};

1812
	return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
1813
					     0, loop);
1814 1815
}

1816
static int register_context(struct intel_context *ce, bool loop)
1817 1818 1819
{
	struct intel_guc *guc = ce_to_guc(ce);
	u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool) +
1820
		ce->guc_id.id * sizeof(struct guc_lrc_desc);
1821
	int ret;
1822

1823
	GEM_BUG_ON(intel_context_is_child(ce));
1824 1825
	trace_intel_context_register(ce);

1826 1827 1828 1829 1830 1831
	if (intel_context_is_parent(ce))
		ret = __guc_action_register_multi_lrc(guc, ce, ce->guc_id.id,
						      offset, loop);
	else
		ret = __guc_action_register_context(guc, ce->guc_id.id, offset,
						    loop);
1832 1833 1834 1835
	if (likely(!ret)) {
		unsigned long flags;

		spin_lock_irqsave(&ce->guc_state.lock, flags);
1836
		set_context_registered(ce);
1837 1838
		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
	}
1839 1840

	return ret;
1841 1842 1843
}

static int __guc_action_deregister_context(struct intel_guc *guc,
1844
					   u32 guc_id)
1845 1846 1847 1848 1849 1850
{
	u32 action[] = {
		INTEL_GUC_ACTION_DEREGISTER_CONTEXT,
		guc_id,
	};

1851 1852
	return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
					     G2H_LEN_DW_DEREGISTER_CONTEXT,
1853
					     true);
1854 1855
}

1856
static int deregister_context(struct intel_context *ce, u32 guc_id)
1857 1858 1859
{
	struct intel_guc *guc = ce_to_guc(ce);

1860
	GEM_BUG_ON(intel_context_is_child(ce));
1861 1862
	trace_intel_context_deregister(ce);

1863
	return __guc_action_deregister_context(guc, guc_id);
1864 1865
}

1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
static inline void clear_children_join_go_memory(struct intel_context *ce)
{
	struct parent_scratch *ps = __get_parent_scratch(ce);
	int i;

	ps->go.semaphore = 0;
	for (i = 0; i < ce->parallel.number_children + 1; ++i)
		ps->join[i].semaphore = 0;
}

static inline u32 get_children_go_value(struct intel_context *ce)
{
	return __get_parent_scratch(ce)->go.semaphore;
}

static inline u32 get_children_join_value(struct intel_context *ce,
					  u8 child_index)
{
	return __get_parent_scratch(ce)->join[child_index].semaphore;
}

1887 1888 1889 1890 1891
static void guc_context_policy_init(struct intel_engine_cs *engine,
				    struct guc_lrc_desc *desc)
{
	desc->policy_flags = 0;

1892 1893 1894
	if (engine->flags & I915_ENGINE_WANT_FORCED_PREEMPTION)
		desc->policy_flags |= CONTEXT_POLICY_FLAG_PREEMPT_TO_IDLE;

1895 1896 1897
	/* NB: For both of these, zero means disabled. */
	desc->execution_quantum = engine->props.timeslice_duration_ms * 1000;
	desc->preemption_timeout = engine->props.preempt_timeout_ms * 1000;
1898 1899
}

1900
static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
1901 1902 1903 1904
{
	struct intel_engine_cs *engine = ce->engine;
	struct intel_runtime_pm *runtime_pm = engine->uncore->rpm;
	struct intel_guc *guc = &engine->gt->uc.guc;
1905
	u32 desc_idx = ce->guc_id.id;
1906 1907 1908
	struct guc_lrc_desc *desc;
	bool context_registered;
	intel_wakeref_t wakeref;
1909
	struct intel_context *child;
1910 1911 1912
	int ret = 0;

	GEM_BUG_ON(!engine->mask);
1913
	GEM_BUG_ON(!sched_state_is_init(ce));
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928

	/*
	 * Ensure LRC + CT vmas are is same region as write barrier is done
	 * based on CT vma region.
	 */
	GEM_BUG_ON(i915_gem_object_is_lmem(guc->ct.vma->obj) !=
		   i915_gem_object_is_lmem(ce->ring->vma->obj));

	context_registered = lrc_desc_registered(guc, desc_idx);

	reset_lrc_desc(guc, desc_idx);
	set_lrc_desc_registered(guc, desc_idx, ce);

	desc = __get_lrc_desc(guc, desc_idx);
	desc->engine_class = engine_class_to_guc_class(engine->class);
1929
	desc->engine_submit_mask = engine->logical_mask;
1930
	desc->hw_context_desc = ce->lrc.lrca;
1931
	desc->priority = ce->guc_state.prio;
1932 1933 1934
	desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
	guc_context_policy_init(engine, desc);

1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
	/*
	 * If context is a parent, we need to register a process descriptor
	 * describing a work queue and register all child contexts.
	 */
	if (intel_context_is_parent(ce)) {
		struct guc_process_desc *pdesc;

		ce->parallel.guc.wqi_tail = 0;
		ce->parallel.guc.wqi_head = 0;

		desc->process_desc = i915_ggtt_offset(ce->state) +
1946
			__get_parent_scratch_offset(ce);
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
		desc->wq_addr = i915_ggtt_offset(ce->state) +
			__get_wq_offset(ce);
		desc->wq_size = WQ_SIZE;

		pdesc = __get_process_desc(ce);
		memset(pdesc, 0, sizeof(*(pdesc)));
		pdesc->stage_id = ce->guc_id.id;
		pdesc->wq_base_addr = desc->wq_addr;
		pdesc->wq_size_bytes = desc->wq_size;
		pdesc->wq_status = WQ_STATUS_ACTIVE;

		for_each_child(ce, child) {
			desc = __get_lrc_desc(guc, child->guc_id.id);

			desc->engine_class =
				engine_class_to_guc_class(engine->class);
			desc->hw_context_desc = child->lrc.lrca;
			desc->priority = ce->guc_state.prio;
			desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
			guc_context_policy_init(engine, desc);
		}
1968 1969

		clear_children_join_go_memory(ce);
1970 1971
	}

1972 1973 1974 1975 1976 1977 1978 1979 1980
	/*
	 * The context_lookup xarray is used to determine if the hardware
	 * context is currently registered. There are two cases in which it
	 * could be registered either the guc_id has been stolen from another
	 * context or the lrc descriptor address of this context has changed. In
	 * either case the context needs to be deregistered with the GuC before
	 * registering this context.
	 */
	if (context_registered) {
1981 1982 1983
		bool disabled;
		unsigned long flags;

1984
		trace_intel_context_steal_guc_id(ce);
1985 1986 1987 1988 1989 1990
		GEM_BUG_ON(!loop);

		/* Seal race with Reset */
		spin_lock_irqsave(&ce->guc_state.lock, flags);
		disabled = submission_disabled(guc);
		if (likely(!disabled)) {
1991 1992
			set_context_wait_for_deregister_to_register(ce);
			intel_context_get(ce);
1993 1994 1995 1996 1997
		}
		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
		if (unlikely(disabled)) {
			reset_lrc_desc(guc, desc_idx);
			return 0;	/* Will get registered later */
1998
		}
1999 2000 2001 2002 2003 2004

		/*
		 * If stealing the guc_id, this ce has the same guc_id as the
		 * context whose guc_id was stolen.
		 */
		with_intel_runtime_pm(runtime_pm, wakeref)
2005
			ret = deregister_context(ce, ce->guc_id.id);
2006
		if (unlikely(ret == -ENODEV))
2007
			ret = 0;	/* Will get registered later */
2008 2009
	} else {
		with_intel_runtime_pm(runtime_pm, wakeref)
2010
			ret = register_context(ce, loop);
2011 2012 2013
		if (unlikely(ret == -EBUSY)) {
			reset_lrc_desc(guc, desc_idx);
		} else if (unlikely(ret == -ENODEV)) {
2014 2015
			reset_lrc_desc(guc, desc_idx);
			ret = 0;	/* Will get registered later */
2016
		}
2017 2018 2019
	}

	return ret;
2020 2021
}

2022 2023 2024 2025
static int __guc_context_pre_pin(struct intel_context *ce,
				 struct intel_engine_cs *engine,
				 struct i915_gem_ww_ctx *ww,
				 void **vaddr)
2026
{
2027
	return lrc_pre_pin(ce, engine, ww, vaddr);
2028 2029
}

2030 2031 2032
static int __guc_context_pin(struct intel_context *ce,
			     struct intel_engine_cs *engine,
			     void *vaddr)
2033
{
2034 2035 2036 2037 2038 2039 2040 2041 2042
	if (i915_ggtt_offset(ce->state) !=
	    (ce->lrc.lrca & CTX_GTT_ADDRESS_MASK))
		set_bit(CONTEXT_LRCA_DIRTY, &ce->flags);

	/*
	 * GuC context gets pinned in guc_request_alloc. See that function for
	 * explaination of why.
	 */

2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054
	return lrc_pin(ce, engine, vaddr);
}

static int guc_context_pre_pin(struct intel_context *ce,
			       struct i915_gem_ww_ctx *ww,
			       void **vaddr)
{
	return __guc_context_pre_pin(ce, ce->engine, ww, vaddr);
}

static int guc_context_pin(struct intel_context *ce, void *vaddr)
{
2055 2056 2057 2058 2059 2060
	int ret = __guc_context_pin(ce, ce->engine, vaddr);

	if (likely(!ret && !intel_context_is_barrier(ce)))
		intel_engine_pm_get(ce->engine);

	return ret;
2061 2062
}

2063 2064 2065 2066 2067 2068
static void guc_context_unpin(struct intel_context *ce)
{
	struct intel_guc *guc = ce_to_guc(ce);

	unpin_guc_id(guc, ce);
	lrc_unpin(ce);
2069 2070 2071

	if (likely(!intel_context_is_barrier(ce)))
		intel_engine_pm_put_async(ce->engine);
2072 2073 2074 2075 2076 2077 2078
}

static void guc_context_post_unpin(struct intel_context *ce)
{
	lrc_post_unpin(ce);
}

2079 2080 2081 2082 2083
static void __guc_context_sched_enable(struct intel_guc *guc,
				       struct intel_context *ce)
{
	u32 action[] = {
		INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET,
2084
		ce->guc_id.id,
2085 2086 2087 2088 2089 2090 2091 2092 2093
		GUC_CONTEXT_ENABLE
	};

	trace_intel_context_sched_enable(ce);

	guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
				      G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
}

2094 2095 2096 2097 2098 2099
static void __guc_context_sched_disable(struct intel_guc *guc,
					struct intel_context *ce,
					u16 guc_id)
{
	u32 action[] = {
		INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET,
2100
		guc_id,	/* ce->guc_id.id not stable */
2101 2102 2103 2104 2105
		GUC_CONTEXT_DISABLE
	};

	GEM_BUG_ON(guc_id == GUC_INVALID_LRC_ID);

2106
	GEM_BUG_ON(intel_context_is_child(ce));
2107
	trace_intel_context_sched_disable(ce);
2108

2109 2110
	guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
				      G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
2111 2112
}

2113 2114 2115 2116
static void guc_blocked_fence_complete(struct intel_context *ce)
{
	lockdep_assert_held(&ce->guc_state.lock);

2117 2118
	if (!i915_sw_fence_done(&ce->guc_state.blocked))
		i915_sw_fence_complete(&ce->guc_state.blocked);
2119 2120 2121 2122 2123
}

static void guc_blocked_fence_reinit(struct intel_context *ce)
{
	lockdep_assert_held(&ce->guc_state.lock);
2124
	GEM_BUG_ON(!i915_sw_fence_done(&ce->guc_state.blocked));
2125 2126 2127 2128 2129 2130

	/*
	 * This fence is always complete unless a pending schedule disable is
	 * outstanding. We arm the fence here and complete it when we receive
	 * the pending schedule disable complete message.
	 */
2131 2132 2133 2134
	i915_sw_fence_fini(&ce->guc_state.blocked);
	i915_sw_fence_reinit(&ce->guc_state.blocked);
	i915_sw_fence_await(&ce->guc_state.blocked);
	i915_sw_fence_commit(&ce->guc_state.blocked);
2135 2136
}

2137 2138 2139 2140 2141 2142
static u16 prep_context_pending_disable(struct intel_context *ce)
{
	lockdep_assert_held(&ce->guc_state.lock);

	set_context_pending_disable(ce);
	clr_context_enabled(ce);
2143
	guc_blocked_fence_reinit(ce);
2144
	intel_context_get(ce);
2145

2146
	return ce->guc_id.id;
2147 2148
}

2149 2150 2151 2152 2153 2154 2155 2156 2157
static struct i915_sw_fence *guc_context_block(struct intel_context *ce)
{
	struct intel_guc *guc = ce_to_guc(ce);
	unsigned long flags;
	struct intel_runtime_pm *runtime_pm = ce->engine->uncore->rpm;
	intel_wakeref_t wakeref;
	u16 guc_id;
	bool enabled;

2158 2159
	GEM_BUG_ON(intel_context_is_child(ce));

2160 2161 2162 2163 2164 2165 2166 2167 2168
	spin_lock_irqsave(&ce->guc_state.lock, flags);

	incr_context_blocked(ce);

	enabled = context_enabled(ce);
	if (unlikely(!enabled || submission_disabled(guc))) {
		if (enabled)
			clr_context_enabled(ce);
		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2169
		return &ce->guc_state.blocked;
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184
	}

	/*
	 * We add +2 here as the schedule disable complete CTB handler calls
	 * intel_context_sched_disable_unpin (-2 to pin_count).
	 */
	atomic_add(2, &ce->pin_count);

	guc_id = prep_context_pending_disable(ce);

	spin_unlock_irqrestore(&ce->guc_state.lock, flags);

	with_intel_runtime_pm(runtime_pm, wakeref)
		__guc_context_sched_disable(guc, ce, guc_id);

2185
	return &ce->guc_state.blocked;
2186 2187
}

2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200
#define SCHED_STATE_MULTI_BLOCKED_MASK \
	(SCHED_STATE_BLOCKED_MASK & ~SCHED_STATE_BLOCKED)
#define SCHED_STATE_NO_UNBLOCK \
	(SCHED_STATE_MULTI_BLOCKED_MASK | \
	 SCHED_STATE_PENDING_DISABLE | \
	 SCHED_STATE_BANNED)

static bool context_cant_unblock(struct intel_context *ce)
{
	lockdep_assert_held(&ce->guc_state.lock);

	return (ce->guc_state.sched_state & SCHED_STATE_NO_UNBLOCK) ||
		context_guc_id_invalid(ce) ||
2201
		!lrc_desc_registered(ce_to_guc(ce), ce->guc_id.id) ||
2202 2203 2204
		!intel_context_is_pinned(ce);
}

2205 2206 2207 2208 2209 2210 2211 2212 2213
static void guc_context_unblock(struct intel_context *ce)
{
	struct intel_guc *guc = ce_to_guc(ce);
	unsigned long flags;
	struct intel_runtime_pm *runtime_pm = ce->engine->uncore->rpm;
	intel_wakeref_t wakeref;
	bool enable;

	GEM_BUG_ON(context_enabled(ce));
2214
	GEM_BUG_ON(intel_context_is_child(ce));
2215 2216 2217 2218

	spin_lock_irqsave(&ce->guc_state.lock, flags);

	if (unlikely(submission_disabled(guc) ||
2219
		     context_cant_unblock(ce))) {
2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
		enable = false;
	} else {
		enable = true;
		set_context_pending_enable(ce);
		set_context_enabled(ce);
		intel_context_get(ce);
	}

	decr_context_blocked(ce);

	spin_unlock_irqrestore(&ce->guc_state.lock, flags);

	if (enable) {
		with_intel_runtime_pm(runtime_pm, wakeref)
			__guc_context_sched_enable(guc, ce);
	}
}

static void guc_context_cancel_request(struct intel_context *ce,
				       struct i915_request *rq)
{
2241 2242 2243
	struct intel_context *block_context =
		request_to_scheduling_context(rq);

2244
	if (i915_sw_fence_signaled(&rq->submit)) {
2245
		struct i915_sw_fence *fence;
2246

2247
		intel_context_get(ce);
2248
		fence = guc_context_block(block_context);
2249 2250 2251 2252 2253 2254
		i915_sw_fence_wait(fence);
		if (!i915_request_completed(rq)) {
			__i915_request_skip(rq);
			guc_reset_state(ce, intel_ring_wrap(ce->ring, rq->head),
					true);
		}
2255 2256 2257 2258 2259 2260 2261

		/*
		 * XXX: Racey if context is reset, see comment in
		 * __guc_reset_context().
		 */
		flush_work(&ce_to_guc(ce)->ct.requests.worker);

2262
		guc_context_unblock(block_context);
2263
		intel_context_put(ce);
2264 2265 2266
	}
}

2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287
static void __guc_context_set_preemption_timeout(struct intel_guc *guc,
						 u16 guc_id,
						 u32 preemption_timeout)
{
	u32 action[] = {
		INTEL_GUC_ACTION_SET_CONTEXT_PREEMPTION_TIMEOUT,
		guc_id,
		preemption_timeout
	};

	intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);
}

static void guc_context_ban(struct intel_context *ce, struct i915_request *rq)
{
	struct intel_guc *guc = ce_to_guc(ce);
	struct intel_runtime_pm *runtime_pm =
		&ce->engine->gt->i915->runtime_pm;
	intel_wakeref_t wakeref;
	unsigned long flags;

2288 2289
	GEM_BUG_ON(intel_context_is_child(ce));

2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325
	guc_flush_submissions(guc);

	spin_lock_irqsave(&ce->guc_state.lock, flags);
	set_context_banned(ce);

	if (submission_disabled(guc) ||
	    (!context_enabled(ce) && !context_pending_disable(ce))) {
		spin_unlock_irqrestore(&ce->guc_state.lock, flags);

		guc_cancel_context_requests(ce);
		intel_engine_signal_breadcrumbs(ce->engine);
	} else if (!context_pending_disable(ce)) {
		u16 guc_id;

		/*
		 * We add +2 here as the schedule disable complete CTB handler
		 * calls intel_context_sched_disable_unpin (-2 to pin_count).
		 */
		atomic_add(2, &ce->pin_count);

		guc_id = prep_context_pending_disable(ce);
		spin_unlock_irqrestore(&ce->guc_state.lock, flags);

		/*
		 * In addition to disabling scheduling, set the preemption
		 * timeout to the minimum value (1 us) so the banned context
		 * gets kicked off the HW ASAP.
		 */
		with_intel_runtime_pm(runtime_pm, wakeref) {
			__guc_context_set_preemption_timeout(guc, guc_id, 1);
			__guc_context_sched_disable(guc, ce, guc_id);
		}
	} else {
		if (!context_guc_id_invalid(ce))
			with_intel_runtime_pm(runtime_pm, wakeref)
				__guc_context_set_preemption_timeout(guc,
2326
								     ce->guc_id.id,
2327 2328 2329 2330 2331
								     1);
		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
	}
}

2332 2333 2334 2335
static void guc_context_sched_disable(struct intel_context *ce)
{
	struct intel_guc *guc = ce_to_guc(ce);
	unsigned long flags;
2336
	struct intel_runtime_pm *runtime_pm = &ce->engine->gt->i915->runtime_pm;
2337
	intel_wakeref_t wakeref;
2338
	u16 guc_id;
2339

2340 2341
	GEM_BUG_ON(intel_context_is_child(ce));

2342
	spin_lock_irqsave(&ce->guc_state.lock, flags);
2343 2344

	/*
2345 2346 2347 2348 2349
	 * We have to check if the context has been disabled by another thread,
	 * check if submssion has been disabled to seal a race with reset and
	 * finally check if any more requests have been committed to the
	 * context ensursing that a request doesn't slip through the
	 * 'context_pending_disable' fence.
2350
	 */
2351 2352
	if (unlikely(!context_enabled(ce) || submission_disabled(guc) ||
		     context_has_committed_requests(ce))) {
2353
		clr_context_enabled(ce);
2354 2355 2356
		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
		goto unpin;
	}
2357
	guc_id = prep_context_pending_disable(ce);
2358

2359 2360 2361 2362 2363 2364 2365 2366 2367 2368
	spin_unlock_irqrestore(&ce->guc_state.lock, flags);

	with_intel_runtime_pm(runtime_pm, wakeref)
		__guc_context_sched_disable(guc, ce, guc_id);

	return;
unpin:
	intel_context_sched_disable_unpin(ce);
}

2369 2370 2371
static inline void guc_lrc_desc_unpin(struct intel_context *ce)
{
	struct intel_guc *guc = ce_to_guc(ce);
2372 2373 2374
	struct intel_gt *gt = guc_to_gt(guc);
	unsigned long flags;
	bool disabled;
2375

2376
	lockdep_assert_held(&guc->submission_state.lock);
2377
	GEM_BUG_ON(!intel_gt_pm_is_awake(gt));
2378 2379
	GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id.id));
	GEM_BUG_ON(ce != __get_context(guc, ce->guc_id.id));
2380
	GEM_BUG_ON(context_enabled(ce));
2381

2382 2383 2384 2385 2386 2387 2388 2389 2390 2391
	/* Seal race with Reset */
	spin_lock_irqsave(&ce->guc_state.lock, flags);
	disabled = submission_disabled(guc);
	if (likely(!disabled)) {
		__intel_gt_pm_get(gt);
		set_context_destroyed(ce);
		clr_context_registered(ce);
	}
	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
	if (unlikely(disabled)) {
2392
		__release_guc_id(guc, ce);
2393 2394 2395 2396
		__guc_context_destroy(ce);
		return;
	}

2397
	deregister_context(ce, ce->guc_id.id);
2398 2399
}

2400 2401
static void __guc_context_destroy(struct intel_context *ce)
{
2402 2403 2404 2405
	GEM_BUG_ON(ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_KMD_HIGH] ||
		   ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_HIGH] ||
		   ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_KMD_NORMAL] ||
		   ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_NORMAL]);
2406
	GEM_BUG_ON(ce->guc_state.number_committed_requests);
2407

2408 2409 2410 2411 2412 2413 2414
	lrc_fini(ce);
	intel_context_fini(ce);

	if (intel_engine_is_virtual(ce->engine)) {
		struct guc_virtual_engine *ve =
			container_of(ce, typeof(*ve), context);

2415 2416 2417
		if (ve->base.breadcrumbs)
			intel_breadcrumbs_put(ve->base.breadcrumbs);

2418 2419 2420 2421 2422 2423
		kfree(ve);
	} else {
		intel_context_free(ce);
	}
}

2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468
static void guc_flush_destroyed_contexts(struct intel_guc *guc)
{
	struct intel_context *ce, *cn;
	unsigned long flags;

	GEM_BUG_ON(!submission_disabled(guc) &&
		   guc_submission_initialized(guc));

	spin_lock_irqsave(&guc->submission_state.lock, flags);
	list_for_each_entry_safe(ce, cn,
				 &guc->submission_state.destroyed_contexts,
				 destroyed_link) {
		list_del_init(&ce->destroyed_link);
		__release_guc_id(guc, ce);
		__guc_context_destroy(ce);
	}
	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
}

static void deregister_destroyed_contexts(struct intel_guc *guc)
{
	struct intel_context *ce, *cn;
	unsigned long flags;

	spin_lock_irqsave(&guc->submission_state.lock, flags);
	list_for_each_entry_safe(ce, cn,
				 &guc->submission_state.destroyed_contexts,
				 destroyed_link) {
		list_del_init(&ce->destroyed_link);
		guc_lrc_desc_unpin(ce);
	}
	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
}

static void destroyed_worker_func(struct work_struct *w)
{
	struct intel_guc *guc = container_of(w, struct intel_guc,
					     submission_state.destroyed_worker);
	struct intel_gt *gt = guc_to_gt(guc);
	int tmp;

	with_intel_gt_pm(gt, tmp)
		deregister_destroyed_contexts(guc);
}

2469 2470 2471 2472 2473
static void guc_context_destroy(struct kref *kref)
{
	struct intel_context *ce = container_of(kref, typeof(*ce), ref);
	struct intel_guc *guc = ce_to_guc(ce);
	unsigned long flags;
2474
	bool destroy;
2475 2476 2477 2478

	/*
	 * If the guc_id is invalid this context has been stolen and we can free
	 * it immediately. Also can be freed immediately if the context is not
2479
	 * registered with the GuC or the GuC is in the middle of a reset.
2480
	 */
2481
	spin_lock_irqsave(&guc->submission_state.lock, flags);
2482 2483 2484 2485 2486 2487 2488 2489 2490
	destroy = submission_disabled(guc) || context_guc_id_invalid(ce) ||
		!lrc_desc_registered(guc, ce->guc_id.id);
	if (likely(!destroy)) {
		if (!list_empty(&ce->guc_id.link))
			list_del_init(&ce->guc_id.link);
		list_add_tail(&ce->destroyed_link,
			      &guc->submission_state.destroyed_contexts);
	} else {
		__release_guc_id(guc, ce);
2491
	}
2492
	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
2493
	if (unlikely(destroy)) {
2494 2495 2496 2497
		__guc_context_destroy(ce);
		return;
	}

2498
	/*
2499 2500 2501
	 * We use a worker to issue the H2G to deregister the context as we can
	 * take the GT PM for the first time which isn't allowed from an atomic
	 * context.
2502
	 */
2503
	queue_work(system_unbound_wq, &guc->submission_state.destroyed_worker);
2504 2505 2506 2507 2508 2509 2510
}

static int guc_context_alloc(struct intel_context *ce)
{
	return lrc_alloc(ce, ce->engine);
}

2511 2512 2513 2514 2515 2516
static void guc_context_set_prio(struct intel_guc *guc,
				 struct intel_context *ce,
				 u8 prio)
{
	u32 action[] = {
		INTEL_GUC_ACTION_SET_CONTEXT_PRIORITY,
2517
		ce->guc_id.id,
2518 2519 2520 2521 2522
		prio,
	};

	GEM_BUG_ON(prio < GUC_CLIENT_PRIORITY_KMD_HIGH ||
		   prio > GUC_CLIENT_PRIORITY_NORMAL);
2523
	lockdep_assert_held(&ce->guc_state.lock);
2524

2525
	if (ce->guc_state.prio == prio || submission_disabled(guc) ||
2526
	    !context_registered(ce)) {
2527
		ce->guc_state.prio = prio;
2528
		return;
2529
	}
2530 2531 2532

	guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);

2533
	ce->guc_state.prio = prio;
2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551
	trace_intel_context_set_prio(ce);
}

static inline u8 map_i915_prio_to_guc_prio(int prio)
{
	if (prio == I915_PRIORITY_NORMAL)
		return GUC_CLIENT_PRIORITY_KMD_NORMAL;
	else if (prio < I915_PRIORITY_NORMAL)
		return GUC_CLIENT_PRIORITY_NORMAL;
	else if (prio < I915_PRIORITY_DISPLAY)
		return GUC_CLIENT_PRIORITY_HIGH;
	else
		return GUC_CLIENT_PRIORITY_KMD_HIGH;
}

static inline void add_context_inflight_prio(struct intel_context *ce,
					     u8 guc_prio)
{
2552 2553
	lockdep_assert_held(&ce->guc_state.lock);
	GEM_BUG_ON(guc_prio >= ARRAY_SIZE(ce->guc_state.prio_count));
2554

2555
	++ce->guc_state.prio_count[guc_prio];
2556 2557

	/* Overflow protection */
2558
	GEM_WARN_ON(!ce->guc_state.prio_count[guc_prio]);
2559 2560 2561 2562 2563
}

static inline void sub_context_inflight_prio(struct intel_context *ce,
					     u8 guc_prio)
{
2564 2565
	lockdep_assert_held(&ce->guc_state.lock);
	GEM_BUG_ON(guc_prio >= ARRAY_SIZE(ce->guc_state.prio_count));
2566 2567

	/* Underflow protection */
2568
	GEM_WARN_ON(!ce->guc_state.prio_count[guc_prio]);
2569

2570
	--ce->guc_state.prio_count[guc_prio];
2571 2572 2573 2574 2575 2576 2577 2578 2579 2580
}

static inline void update_context_prio(struct intel_context *ce)
{
	struct intel_guc *guc = &ce->engine->gt->uc.guc;
	int i;

	BUILD_BUG_ON(GUC_CLIENT_PRIORITY_KMD_HIGH != 0);
	BUILD_BUG_ON(GUC_CLIENT_PRIORITY_KMD_HIGH > GUC_CLIENT_PRIORITY_NORMAL);

2581
	lockdep_assert_held(&ce->guc_state.lock);
2582

2583 2584
	for (i = 0; i < ARRAY_SIZE(ce->guc_state.prio_count); ++i) {
		if (ce->guc_state.prio_count[i]) {
2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596
			guc_context_set_prio(guc, ce, i);
			break;
		}
	}
}

static inline bool new_guc_prio_higher(u8 old_guc_prio, u8 new_guc_prio)
{
	/* Lower value is higher priority */
	return new_guc_prio < old_guc_prio;
}

2597 2598
static void add_to_context(struct i915_request *rq)
{
2599
	struct intel_context *ce = request_to_scheduling_context(rq);
2600 2601
	u8 new_guc_prio = map_i915_prio_to_guc_prio(rq_prio(rq));

2602
	GEM_BUG_ON(intel_context_is_child(ce));
2603
	GEM_BUG_ON(rq->guc_prio == GUC_PRIO_FINI);
2604

2605 2606
	spin_lock(&ce->guc_state.lock);
	list_move_tail(&rq->sched.link, &ce->guc_state.requests);
2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617

	if (rq->guc_prio == GUC_PRIO_INIT) {
		rq->guc_prio = new_guc_prio;
		add_context_inflight_prio(ce, rq->guc_prio);
	} else if (new_guc_prio_higher(rq->guc_prio, new_guc_prio)) {
		sub_context_inflight_prio(ce, rq->guc_prio);
		rq->guc_prio = new_guc_prio;
		add_context_inflight_prio(ce, rq->guc_prio);
	}
	update_context_prio(ce);

2618
	spin_unlock(&ce->guc_state.lock);
2619 2620
}

2621 2622
static void guc_prio_fini(struct i915_request *rq, struct intel_context *ce)
{
2623
	lockdep_assert_held(&ce->guc_state.lock);
2624 2625 2626 2627 2628 2629 2630 2631 2632

	if (rq->guc_prio != GUC_PRIO_INIT &&
	    rq->guc_prio != GUC_PRIO_FINI) {
		sub_context_inflight_prio(ce, rq->guc_prio);
		update_context_prio(ce);
	}
	rq->guc_prio = GUC_PRIO_FINI;
}

2633 2634
static void remove_from_context(struct i915_request *rq)
{
2635 2636 2637
	struct intel_context *ce = request_to_scheduling_context(rq);

	GEM_BUG_ON(intel_context_is_child(ce));
2638

2639
	spin_lock_irq(&ce->guc_state.lock);
2640 2641 2642 2643 2644 2645 2646

	list_del_init(&rq->sched.link);
	clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);

	/* Prevent further __await_execution() registering a cb, then flush */
	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);

2647 2648
	guc_prio_fini(rq, ce);

2649
	decr_context_committed_requests(ce);
2650

2651 2652
	spin_unlock_irq(&ce->guc_state.lock);

2653
	atomic_dec(&ce->guc_id.ref);
2654 2655 2656
	i915_request_notify_execute_cb_imm(rq);
}

2657 2658 2659 2660 2661
static const struct intel_context_ops guc_context_ops = {
	.alloc = guc_context_alloc,

	.pre_pin = guc_context_pre_pin,
	.pin = guc_context_pin,
2662 2663
	.unpin = guc_context_unpin,
	.post_unpin = guc_context_post_unpin,
2664

2665 2666
	.ban = guc_context_ban,

2667 2668
	.cancel_request = guc_context_cancel_request,

2669 2670 2671
	.enter = intel_context_enter_engine,
	.exit = intel_context_exit_engine,

2672 2673
	.sched_disable = guc_context_sched_disable,

2674
	.reset = lrc_reset,
2675
	.destroy = guc_context_destroy,
2676 2677

	.create_virtual = guc_create_virtual,
2678
	.create_parallel = guc_create_parallel,
2679 2680
};

2681 2682 2683 2684 2685 2686 2687 2688
static void submit_work_cb(struct irq_work *wrk)
{
	struct i915_request *rq = container_of(wrk, typeof(*rq), submit_work);

	might_lock(&rq->engine->sched_engine->lock);
	i915_sw_fence_complete(&rq->submit);
}

2689 2690
static void __guc_signal_context_fence(struct intel_context *ce)
{
2691
	struct i915_request *rq, *rn;
2692 2693 2694

	lockdep_assert_held(&ce->guc_state.lock);

2695 2696 2697
	if (!list_empty(&ce->guc_state.fences))
		trace_intel_context_fence_release(ce);

2698 2699 2700 2701 2702 2703 2704 2705 2706
	/*
	 * Use an IRQ to ensure locking order of sched_engine->lock ->
	 * ce->guc_state.lock is preserved.
	 */
	list_for_each_entry_safe(rq, rn, &ce->guc_state.fences,
				 guc_fence_link) {
		list_del(&rq->guc_fence_link);
		irq_work_queue(&rq->submit_work);
	}
2707 2708 2709 2710 2711 2712 2713 2714

	INIT_LIST_HEAD(&ce->guc_state.fences);
}

static void guc_signal_context_fence(struct intel_context *ce)
{
	unsigned long flags;

2715 2716
	GEM_BUG_ON(intel_context_is_child(ce));

2717 2718 2719 2720 2721 2722
	spin_lock_irqsave(&ce->guc_state.lock, flags);
	clr_context_wait_for_deregister_to_register(ce);
	__guc_signal_context_fence(ce);
	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
}

2723 2724
static bool context_needs_register(struct intel_context *ce, bool new_guc_id)
{
2725
	return (new_guc_id || test_bit(CONTEXT_LRCA_DIRTY, &ce->flags) ||
2726
		!lrc_desc_registered(ce_to_guc(ce), ce->guc_id.id)) &&
2727
		!submission_disabled(ce_to_guc(ce));
2728 2729
}

2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740
static void guc_context_init(struct intel_context *ce)
{
	const struct i915_gem_context *ctx;
	int prio = I915_CONTEXT_DEFAULT_PRIORITY;

	rcu_read_lock();
	ctx = rcu_dereference(ce->gem_context);
	if (ctx)
		prio = ctx->sched.priority;
	rcu_read_unlock();

2741
	ce->guc_state.prio = map_i915_prio_to_guc_prio(prio);
2742 2743 2744
	set_bit(CONTEXT_GUC_INIT, &ce->flags);
}

2745
static int guc_request_alloc(struct i915_request *rq)
2746
{
2747
	struct intel_context *ce = request_to_scheduling_context(rq);
2748
	struct intel_guc *guc = ce_to_guc(ce);
2749
	unsigned long flags;
2750 2751
	int ret;

2752
	GEM_BUG_ON(!intel_context_is_pinned(rq->context));
2753 2754 2755 2756 2757 2758

	/*
	 * Flush enough space to reduce the likelihood of waiting after
	 * we start building the request - in which case we will just
	 * have to repeat work.
	 */
2759
	rq->reserved_space += GUC_REQUEST_SIZE;
2760 2761 2762 2763 2764 2765 2766 2767 2768 2769

	/*
	 * Note that after this point, we have committed to using
	 * this request as it is being used to both track the
	 * state of engine initialisation and liveness of the
	 * golden renderstate above. Think twice before you try
	 * to cancel/unwind this request now.
	 */

	/* Unconditionally invalidate GPU caches and TLBs. */
2770
	ret = rq->engine->emit_flush(rq, EMIT_INVALIDATE);
2771 2772 2773
	if (ret)
		return ret;

2774
	rq->reserved_space -= GUC_REQUEST_SIZE;
2775

2776 2777 2778
	if (unlikely(!test_bit(CONTEXT_GUC_INIT, &ce->flags)))
		guc_context_init(ce);

2779 2780 2781
	/*
	 * Call pin_guc_id here rather than in the pinning step as with
	 * dma_resv, contexts can be repeatedly pinned / unpinned trashing the
2782 2783
	 * guc_id and creating horrible race conditions. This is especially bad
	 * when guc_id are being stolen due to over subscription. By the time
2784 2785
	 * this function is reached, it is guaranteed that the guc_id will be
	 * persistent until the generated request is retired. Thus, sealing these
2786
	 * race conditions. It is still safe to fail here if guc_id are
2787 2788 2789 2790 2791 2792 2793 2794 2795
	 * exhausted and return -EAGAIN to the user indicating that they can try
	 * again in the future.
	 *
	 * There is no need for a lock here as the timeline mutex ensures at
	 * most one context can be executing this code path at once. The
	 * guc_id_ref is incremented once for every request in flight and
	 * decremented on each retire. When it is zero, a lock around the
	 * increment (in pin_guc_id) is needed to seal a race with unpin_guc_id.
	 */
2796
	if (atomic_add_unless(&ce->guc_id.ref, 1, 0))
2797
		goto out;
2798

2799 2800 2801 2802
	ret = pin_guc_id(guc, ce);	/* returns 1 if new guc_id assigned */
	if (unlikely(ret < 0))
		return ret;
	if (context_needs_register(ce, !!ret)) {
2803
		ret = guc_lrc_desc_pin(ce, true);
2804
		if (unlikely(ret)) {	/* unwind */
2805 2806 2807 2808
			if (ret == -EPIPE) {
				disable_submission(guc);
				goto out;	/* GPU will be reset */
			}
2809
			atomic_dec(&ce->guc_id.ref);
2810 2811 2812 2813
			unpin_guc_id(guc, ce);
			return ret;
		}
	}
2814

2815
	clear_bit(CONTEXT_LRCA_DIRTY, &ce->flags);
2816

2817 2818 2819
out:
	/*
	 * We block all requests on this context if a G2H is pending for a
2820 2821 2822 2823
	 * schedule disable or context deregistration as the GuC will fail a
	 * schedule enable or context registration if either G2H is pending
	 * respectfully. Once a G2H returns, the fence is released that is
	 * blocking these requests (see guc_signal_context_fence).
2824 2825
	 */
	spin_lock_irqsave(&ce->guc_state.lock, flags);
2826 2827
	if (context_wait_for_deregister_to_register(ce) ||
	    context_pending_disable(ce)) {
2828
		init_irq_work(&rq->submit_work, submit_work_cb);
2829 2830 2831 2832
		i915_sw_fence_await(&rq->submit);

		list_add_tail(&rq->guc_fence_link, &ce->guc_state.fences);
	}
2833
	incr_context_committed_requests(ce);
2834 2835
	spin_unlock_irqrestore(&ce->guc_state.lock, flags);

2836
	return 0;
2837 2838
}

2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850
static int guc_virtual_context_pre_pin(struct intel_context *ce,
				       struct i915_gem_ww_ctx *ww,
				       void **vaddr)
{
	struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);

	return __guc_context_pre_pin(ce, engine, ww, vaddr);
}

static int guc_virtual_context_pin(struct intel_context *ce, void *vaddr)
{
	struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
2851 2852 2853 2854 2855 2856
	int ret = __guc_context_pin(ce, engine, vaddr);
	intel_engine_mask_t tmp, mask = ce->engine->mask;

	if (likely(!ret))
		for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
			intel_engine_pm_get(engine);
2857

2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874
	return ret;
}

static void guc_virtual_context_unpin(struct intel_context *ce)
{
	intel_engine_mask_t tmp, mask = ce->engine->mask;
	struct intel_engine_cs *engine;
	struct intel_guc *guc = ce_to_guc(ce);

	GEM_BUG_ON(context_enabled(ce));
	GEM_BUG_ON(intel_context_is_barrier(ce));

	unpin_guc_id(guc, ce);
	lrc_unpin(ce);

	for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
		intel_engine_pm_put_async(engine);
2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910
}

static void guc_virtual_context_enter(struct intel_context *ce)
{
	intel_engine_mask_t tmp, mask = ce->engine->mask;
	struct intel_engine_cs *engine;

	for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
		intel_engine_pm_get(engine);

	intel_timeline_enter(ce->timeline);
}

static void guc_virtual_context_exit(struct intel_context *ce)
{
	intel_engine_mask_t tmp, mask = ce->engine->mask;
	struct intel_engine_cs *engine;

	for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
		intel_engine_pm_put(engine);

	intel_timeline_exit(ce->timeline);
}

static int guc_virtual_context_alloc(struct intel_context *ce)
{
	struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);

	return lrc_alloc(ce, engine);
}

static const struct intel_context_ops virtual_guc_context_ops = {
	.alloc = guc_virtual_context_alloc,

	.pre_pin = guc_virtual_context_pre_pin,
	.pin = guc_virtual_context_pin,
2911
	.unpin = guc_virtual_context_unpin,
2912 2913
	.post_unpin = guc_context_post_unpin,

2914 2915
	.ban = guc_context_ban,

2916 2917
	.cancel_request = guc_context_cancel_request,

2918 2919 2920 2921 2922 2923 2924 2925 2926 2927
	.enter = guc_virtual_context_enter,
	.exit = guc_virtual_context_exit,

	.sched_disable = guc_context_sched_disable,

	.destroy = guc_context_destroy,

	.get_sibling = guc_virtual_get_sibling,
};

2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963
static int guc_parent_context_pin(struct intel_context *ce, void *vaddr)
{
	struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
	struct intel_guc *guc = ce_to_guc(ce);
	int ret;

	GEM_BUG_ON(!intel_context_is_parent(ce));
	GEM_BUG_ON(!intel_engine_is_virtual(ce->engine));

	ret = pin_guc_id(guc, ce);
	if (unlikely(ret < 0))
		return ret;

	return __guc_context_pin(ce, engine, vaddr);
}

static int guc_child_context_pin(struct intel_context *ce, void *vaddr)
{
	struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);

	GEM_BUG_ON(!intel_context_is_child(ce));
	GEM_BUG_ON(!intel_engine_is_virtual(ce->engine));

	__intel_context_pin(ce->parallel.parent);
	return __guc_context_pin(ce, engine, vaddr);
}

static void guc_parent_context_unpin(struct intel_context *ce)
{
	struct intel_guc *guc = ce_to_guc(ce);

	GEM_BUG_ON(context_enabled(ce));
	GEM_BUG_ON(intel_context_is_barrier(ce));
	GEM_BUG_ON(!intel_context_is_parent(ce));
	GEM_BUG_ON(!intel_engine_is_virtual(ce->engine));

2964 2965
	if (ce->parallel.last_rq)
		i915_request_put(ce->parallel.last_rq);
2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989
	unpin_guc_id(guc, ce);
	lrc_unpin(ce);
}

static void guc_child_context_unpin(struct intel_context *ce)
{
	GEM_BUG_ON(context_enabled(ce));
	GEM_BUG_ON(intel_context_is_barrier(ce));
	GEM_BUG_ON(!intel_context_is_child(ce));
	GEM_BUG_ON(!intel_engine_is_virtual(ce->engine));

	lrc_unpin(ce);
}

static void guc_child_context_post_unpin(struct intel_context *ce)
{
	GEM_BUG_ON(!intel_context_is_child(ce));
	GEM_BUG_ON(!intel_context_is_pinned(ce->parallel.parent));
	GEM_BUG_ON(!intel_engine_is_virtual(ce->engine));

	lrc_post_unpin(ce);
	intel_context_unpin(ce->parallel.parent);
}

2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036
static void guc_child_context_destroy(struct kref *kref)
{
	struct intel_context *ce = container_of(kref, typeof(*ce), ref);

	__guc_context_destroy(ce);
}

static const struct intel_context_ops virtual_parent_context_ops = {
	.alloc = guc_virtual_context_alloc,

	.pre_pin = guc_context_pre_pin,
	.pin = guc_parent_context_pin,
	.unpin = guc_parent_context_unpin,
	.post_unpin = guc_context_post_unpin,

	.ban = guc_context_ban,

	.cancel_request = guc_context_cancel_request,

	.enter = guc_virtual_context_enter,
	.exit = guc_virtual_context_exit,

	.sched_disable = guc_context_sched_disable,

	.destroy = guc_context_destroy,

	.get_sibling = guc_virtual_get_sibling,
};

static const struct intel_context_ops virtual_child_context_ops = {
	.alloc = guc_virtual_context_alloc,

	.pre_pin = guc_context_pre_pin,
	.pin = guc_child_context_pin,
	.unpin = guc_child_context_unpin,
	.post_unpin = guc_child_context_post_unpin,

	.cancel_request = guc_context_cancel_request,

	.enter = guc_virtual_context_enter,
	.exit = guc_virtual_context_exit,

	.destroy = guc_child_context_destroy,

	.get_sibling = guc_virtual_get_sibling,
};

3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061
/*
 * The below override of the breadcrumbs is enabled when the user configures a
 * context for parallel submission (multi-lrc, parent-child).
 *
 * The overridden breadcrumbs implements an algorithm which allows the GuC to
 * safely preempt all the hw contexts configured for parallel submission
 * between each BB. The contract between the i915 and GuC is if the parent
 * context can be preempted, all the children can be preempted, and the GuC will
 * always try to preempt the parent before the children. A handshake between the
 * parent / children breadcrumbs ensures the i915 holds up its end of the deal
 * creating a window to preempt between each set of BBs.
 */
static int emit_bb_start_parent_no_preempt_mid_batch(struct i915_request *rq,
						     u64 offset, u32 len,
						     const unsigned int flags);
static int emit_bb_start_child_no_preempt_mid_batch(struct i915_request *rq,
						    u64 offset, u32 len,
						    const unsigned int flags);
static u32 *
emit_fini_breadcrumb_parent_no_preempt_mid_batch(struct i915_request *rq,
						 u32 *cs);
static u32 *
emit_fini_breadcrumb_child_no_preempt_mid_batch(struct i915_request *rq,
						u32 *cs);

3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096
static struct intel_context *
guc_create_parallel(struct intel_engine_cs **engines,
		    unsigned int num_siblings,
		    unsigned int width)
{
	struct intel_engine_cs **siblings = NULL;
	struct intel_context *parent = NULL, *ce, *err;
	int i, j;

	siblings = kmalloc_array(num_siblings,
				 sizeof(*siblings),
				 GFP_KERNEL);
	if (!siblings)
		return ERR_PTR(-ENOMEM);

	for (i = 0; i < width; ++i) {
		for (j = 0; j < num_siblings; ++j)
			siblings[j] = engines[i * num_siblings + j];

		ce = intel_engine_create_virtual(siblings, num_siblings,
						 FORCE_VIRTUAL);
		if (!ce) {
			err = ERR_PTR(-ENOMEM);
			goto unwind;
		}

		if (i == 0) {
			parent = ce;
			parent->ops = &virtual_parent_context_ops;
		} else {
			ce->ops = &virtual_child_context_ops;
			intel_context_bind_parent_child(parent, ce);
		}
	}

M
Matthew Brost 已提交
3097 3098
	parent->parallel.fence_context = dma_fence_context_alloc(1);

3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112
	parent->engine->emit_bb_start =
		emit_bb_start_parent_no_preempt_mid_batch;
	parent->engine->emit_fini_breadcrumb =
		emit_fini_breadcrumb_parent_no_preempt_mid_batch;
	parent->engine->emit_fini_breadcrumb_dw =
		12 + 4 * parent->parallel.number_children;
	for_each_child(parent, ce) {
		ce->engine->emit_bb_start =
			emit_bb_start_child_no_preempt_mid_batch;
		ce->engine->emit_fini_breadcrumb =
			emit_fini_breadcrumb_child_no_preempt_mid_batch;
		ce->engine->emit_fini_breadcrumb_dw = 16;
	}

3113 3114 3115 3116 3117 3118 3119 3120 3121 3122
	kfree(siblings);
	return parent;

unwind:
	if (parent)
		intel_context_put(parent);
	kfree(siblings);
	return err;
}

3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178
static bool
guc_irq_enable_breadcrumbs(struct intel_breadcrumbs *b)
{
	struct intel_engine_cs *sibling;
	intel_engine_mask_t tmp, mask = b->engine_mask;
	bool result = false;

	for_each_engine_masked(sibling, b->irq_engine->gt, mask, tmp)
		result |= intel_engine_irq_enable(sibling);

	return result;
}

static void
guc_irq_disable_breadcrumbs(struct intel_breadcrumbs *b)
{
	struct intel_engine_cs *sibling;
	intel_engine_mask_t tmp, mask = b->engine_mask;

	for_each_engine_masked(sibling, b->irq_engine->gt, mask, tmp)
		intel_engine_irq_disable(sibling);
}

static void guc_init_breadcrumbs(struct intel_engine_cs *engine)
{
	int i;

	/*
	 * In GuC submission mode we do not know which physical engine a request
	 * will be scheduled on, this creates a problem because the breadcrumb
	 * interrupt is per physical engine. To work around this we attach
	 * requests and direct all breadcrumb interrupts to the first instance
	 * of an engine per class. In addition all breadcrumb interrupts are
	 * enabled / disabled across an engine class in unison.
	 */
	for (i = 0; i < MAX_ENGINE_INSTANCE; ++i) {
		struct intel_engine_cs *sibling =
			engine->gt->engine_class[engine->class][i];

		if (sibling) {
			if (engine->breadcrumbs != sibling->breadcrumbs) {
				intel_breadcrumbs_put(engine->breadcrumbs);
				engine->breadcrumbs =
					intel_breadcrumbs_get(sibling->breadcrumbs);
			}
			break;
		}
	}

	if (engine->breadcrumbs) {
		engine->breadcrumbs->engine_mask |= engine->mask;
		engine->breadcrumbs->irq_enable = guc_irq_enable_breadcrumbs;
		engine->breadcrumbs->irq_disable = guc_irq_disable_breadcrumbs;
	}
}

3179 3180 3181
static void guc_bump_inflight_request_prio(struct i915_request *rq,
					   int prio)
{
3182
	struct intel_context *ce = request_to_scheduling_context(rq);
3183 3184 3185 3186 3187 3188 3189 3190 3191
	u8 new_guc_prio = map_i915_prio_to_guc_prio(prio);

	/* Short circuit function */
	if (prio < I915_PRIORITY_NORMAL ||
	    rq->guc_prio == GUC_PRIO_FINI ||
	    (rq->guc_prio != GUC_PRIO_INIT &&
	     !new_guc_prio_higher(rq->guc_prio, new_guc_prio)))
		return;

3192
	spin_lock(&ce->guc_state.lock);
3193 3194 3195 3196 3197 3198 3199
	if (rq->guc_prio != GUC_PRIO_FINI) {
		if (rq->guc_prio != GUC_PRIO_INIT)
			sub_context_inflight_prio(ce, rq->guc_prio);
		rq->guc_prio = new_guc_prio;
		add_context_inflight_prio(ce, rq->guc_prio);
		update_context_prio(ce);
	}
3200
	spin_unlock(&ce->guc_state.lock);
3201 3202 3203 3204
}

static void guc_retire_inflight_request_prio(struct i915_request *rq)
{
3205
	struct intel_context *ce = request_to_scheduling_context(rq);
3206

3207
	spin_lock(&ce->guc_state.lock);
3208
	guc_prio_fini(rq, ce);
3209
	spin_unlock(&ce->guc_state.lock);
3210 3211
}

3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242
static void sanitize_hwsp(struct intel_engine_cs *engine)
{
	struct intel_timeline *tl;

	list_for_each_entry(tl, &engine->status_page.timelines, engine_link)
		intel_timeline_reset_seqno(tl);
}

static void guc_sanitize(struct intel_engine_cs *engine)
{
	/*
	 * Poison residual state on resume, in case the suspend didn't!
	 *
	 * We have to assume that across suspend/resume (or other loss
	 * of control) that the contents of our pinned buffers has been
	 * lost, replaced by garbage. Since this doesn't always happen,
	 * let's poison such state so that we more quickly spot when
	 * we falsely assume it has been preserved.
	 */
	if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
		memset(engine->status_page.addr, POISON_INUSE, PAGE_SIZE);

	/*
	 * The kernel_context HWSP is stored in the status_page. As above,
	 * that may be lost on resume/initialisation, and so we need to
	 * reset the value in the HWSP.
	 */
	sanitize_hwsp(engine);

	/* And scrub the dirty cachelines for the HWSP */
	clflush_cache_range(engine->status_page.addr, PAGE_SIZE);
3243 3244

	intel_engine_reset_pinned_contexts(engine);
3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279
}

static void setup_hwsp(struct intel_engine_cs *engine)
{
	intel_engine_set_hwsp_writemask(engine, ~0u); /* HWSTAM */

	ENGINE_WRITE_FW(engine,
			RING_HWS_PGA,
			i915_ggtt_offset(engine->status_page.vma));
}

static void start_engine(struct intel_engine_cs *engine)
{
	ENGINE_WRITE_FW(engine,
			RING_MODE_GEN7,
			_MASKED_BIT_ENABLE(GEN11_GFX_DISABLE_LEGACY_MODE));

	ENGINE_WRITE_FW(engine, RING_MI_MODE, _MASKED_BIT_DISABLE(STOP_RING));
	ENGINE_POSTING_READ(engine, RING_MI_MODE);
}

static int guc_resume(struct intel_engine_cs *engine)
{
	assert_forcewakes_active(engine->uncore, FORCEWAKE_ALL);

	intel_mocs_init_engine(engine);

	intel_breadcrumbs_reset(engine->breadcrumbs);

	setup_hwsp(engine);
	start_engine(engine);

	return 0;
}

3280 3281 3282 3283 3284
static bool guc_sched_engine_disabled(struct i915_sched_engine *sched_engine)
{
	return !sched_engine->tasklet.callback;
}

3285 3286
static void guc_set_default_submission(struct intel_engine_cs *engine)
{
3287
	engine->submit_request = guc_submit_request;
3288 3289
}

3290 3291 3292 3293 3294
static inline void guc_kernel_context_pin(struct intel_guc *guc,
					  struct intel_context *ce)
{
	if (context_guc_id_invalid(ce))
		pin_guc_id(guc, ce);
3295
	guc_lrc_desc_pin(ce, true);
3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319
}

static inline void guc_init_lrc_mapping(struct intel_guc *guc)
{
	struct intel_gt *gt = guc_to_gt(guc);
	struct intel_engine_cs *engine;
	enum intel_engine_id id;

	/* make sure all descriptors are clean... */
	xa_destroy(&guc->context_lookup);

	/*
	 * Some contexts might have been pinned before we enabled GuC
	 * submission, so we need to add them to the GuC bookeeping.
	 * Also, after a reset the of the GuC we want to make sure that the
	 * information shared with GuC is properly reset. The kernel LRCs are
	 * not attached to the gem_context, so they need to be added separately.
	 *
	 * Note: we purposefully do not check the return of guc_lrc_desc_pin,
	 * because that function can only fail if a reset is just starting. This
	 * is at the end of reset so presumably another reset isn't happening
	 * and even it did this code would be run again.
	 */

3320 3321 3322 3323 3324 3325 3326
	for_each_engine(engine, gt, id) {
		struct intel_context *ce;

		list_for_each_entry(ce, &engine->pinned_contexts_list,
				    pinned_contexts_link)
			guc_kernel_context_pin(guc, ce);
	}
3327 3328
}

3329
static void guc_release(struct intel_engine_cs *engine)
3330
{
3331
	engine->sanitize = NULL; /* no longer in control, nothing to sanitize */
3332

3333 3334 3335 3336
	intel_engine_cleanup_common(engine);
	lrc_fini_wa_ctx(engine);
}

3337 3338 3339 3340 3341 3342 3343 3344 3345
static void virtual_guc_bump_serial(struct intel_engine_cs *engine)
{
	struct intel_engine_cs *e;
	intel_engine_mask_t tmp, mask = engine->mask;

	for_each_engine_masked(e, engine->gt, mask, tmp)
		e->serial++;
}

3346 3347 3348 3349 3350 3351 3352 3353
static void guc_default_vfuncs(struct intel_engine_cs *engine)
{
	/* Default vfuncs which can be overridden by each engine. */

	engine->resume = guc_resume;

	engine->cops = &guc_context_ops;
	engine->request_alloc = guc_request_alloc;
3354 3355
	engine->add_active_request = add_to_context;
	engine->remove_active_request = remove_from_context;
3356

3357
	engine->sched_engine->schedule = i915_schedule;
3358

3359 3360 3361 3362
	engine->reset.prepare = guc_reset_nop;
	engine->reset.rewind = guc_rewind_nop;
	engine->reset.cancel = guc_reset_nop;
	engine->reset.finish = guc_reset_nop;
3363

3364 3365 3366
	engine->emit_flush = gen8_emit_flush_xcs;
	engine->emit_init_breadcrumb = gen8_emit_init_breadcrumb;
	engine->emit_fini_breadcrumb = gen8_emit_fini_breadcrumb_xcs;
3367
	if (GRAPHICS_VER(engine->i915) >= 12) {
3368 3369 3370 3371
		engine->emit_fini_breadcrumb = gen12_emit_fini_breadcrumb_xcs;
		engine->emit_flush = gen12_emit_flush_xcs;
	}
	engine->set_default_submission = guc_set_default_submission;
3372 3373

	engine->flags |= I915_ENGINE_HAS_PREEMPTION;
3374
	engine->flags |= I915_ENGINE_HAS_TIMESLICES;
3375 3376 3377 3378 3379 3380 3381 3382 3383 3384

	/*
	 * TODO: GuC supports timeslicing and semaphores as well, but they're
	 * handled by the firmware so some minor tweaks are required before
	 * enabling.
	 *
	 * engine->flags |= I915_ENGINE_HAS_SEMAPHORES;
	 */

	engine->emit_bb_start = gen8_emit_bb_start;
3385
}
3386

3387 3388
static void rcs_submission_override(struct intel_engine_cs *engine)
{
3389
	switch (GRAPHICS_VER(engine->i915)) {
3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401
	case 12:
		engine->emit_flush = gen12_emit_flush_rcs;
		engine->emit_fini_breadcrumb = gen12_emit_fini_breadcrumb_rcs;
		break;
	case 11:
		engine->emit_flush = gen11_emit_flush_rcs;
		engine->emit_fini_breadcrumb = gen11_emit_fini_breadcrumb_rcs;
		break;
	default:
		engine->emit_flush = gen8_emit_flush_rcs;
		engine->emit_fini_breadcrumb = gen8_emit_fini_breadcrumb_rcs;
		break;
3402
	}
3403 3404
}

3405 3406 3407
static inline void guc_default_irqs(struct intel_engine_cs *engine)
{
	engine->irq_keep_mask = GT_RENDER_USER_INTERRUPT;
3408
	intel_engine_set_irq_handler(engine, cs_irq_handler);
3409 3410
}

3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421
static void guc_sched_engine_destroy(struct kref *kref)
{
	struct i915_sched_engine *sched_engine =
		container_of(kref, typeof(*sched_engine), ref);
	struct intel_guc *guc = sched_engine->private_data;

	guc->sched_engine = NULL;
	tasklet_kill(&sched_engine->tasklet); /* flush the callback */
	kfree(sched_engine);
}

3422 3423 3424
int intel_guc_submission_setup(struct intel_engine_cs *engine)
{
	struct drm_i915_private *i915 = engine->i915;
3425
	struct intel_guc *guc = &engine->gt->uc.guc;
3426 3427 3428 3429 3430

	/*
	 * The setup relies on several assumptions (e.g. irqs always enabled)
	 * that are only valid on gen11+
	 */
3431
	GEM_BUG_ON(GRAPHICS_VER(i915) < 11);
3432

3433 3434 3435 3436 3437 3438
	if (!guc->sched_engine) {
		guc->sched_engine = i915_sched_engine_create(ENGINE_VIRTUAL);
		if (!guc->sched_engine)
			return -ENOMEM;

		guc->sched_engine->schedule = i915_schedule;
3439
		guc->sched_engine->disabled = guc_sched_engine_disabled;
3440
		guc->sched_engine->private_data = guc;
3441
		guc->sched_engine->destroy = guc_sched_engine_destroy;
3442 3443 3444 3445
		guc->sched_engine->bump_inflight_request_prio =
			guc_bump_inflight_request_prio;
		guc->sched_engine->retire_inflight_request_prio =
			guc_retire_inflight_request_prio;
3446 3447 3448 3449 3450
		tasklet_setup(&guc->sched_engine->tasklet,
			      guc_submission_tasklet);
	}
	i915_sched_engine_put(engine->sched_engine);
	engine->sched_engine = i915_sched_engine_get(guc->sched_engine);
3451 3452 3453

	guc_default_vfuncs(engine);
	guc_default_irqs(engine);
3454
	guc_init_breadcrumbs(engine);
3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469

	if (engine->class == RENDER_CLASS)
		rcs_submission_override(engine);

	lrc_init_wa_ctx(engine);

	/* Finally, take ownership and responsibility for cleanup! */
	engine->sanitize = guc_sanitize;
	engine->release = guc_release;

	return 0;
}

void intel_guc_submission_enable(struct intel_guc *guc)
{
3470
	guc_init_lrc_mapping(guc);
3471 3472
}

3473
void intel_guc_submission_disable(struct intel_guc *guc)
3474
{
3475
	/* Note: By the time we're here, GuC may have already been reset */
3476
}
3477

3478 3479 3480 3481 3482 3483 3484
static bool __guc_submission_supported(struct intel_guc *guc)
{
	/* GuC submission is unavailable for pre-Gen11 */
	return intel_guc_is_supported(guc) &&
	       GRAPHICS_VER(guc_to_gt(guc)->i915) >= 11;
}

3485
static bool __guc_submission_selected(struct intel_guc *guc)
3486
{
3487 3488
	struct drm_i915_private *i915 = guc_to_gt(guc)->i915;

3489
	if (!intel_guc_submission_is_supported(guc))
3490 3491
		return false;

3492
	return i915->params.enable_guc & ENABLE_GUC_SUBMISSION;
3493 3494 3495 3496
}

void intel_guc_submission_init_early(struct intel_guc *guc)
{
3497
	guc->submission_supported = __guc_submission_supported(guc);
3498
	guc->submission_selected = __guc_submission_selected(guc);
3499
}
3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518

static inline struct intel_context *
g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
{
	struct intel_context *ce;

	if (unlikely(desc_idx >= GUC_MAX_LRC_DESCRIPTORS)) {
		drm_err(&guc_to_gt(guc)->i915->drm,
			"Invalid desc_idx %u", desc_idx);
		return NULL;
	}

	ce = __get_context(guc, desc_idx);
	if (unlikely(!ce)) {
		drm_err(&guc_to_gt(guc)->i915->drm,
			"Context is NULL, desc_idx %u", desc_idx);
		return NULL;
	}

3519 3520 3521 3522 3523 3524
	if (unlikely(intel_context_is_child(ce))) {
		drm_err(&guc_to_gt(guc)->i915->drm,
			"Context is child, desc_idx %u", desc_idx);
		return NULL;
	}

3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543
	return ce;
}

int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
					  const u32 *msg,
					  u32 len)
{
	struct intel_context *ce;
	u32 desc_idx = msg[0];

	if (unlikely(len < 1)) {
		drm_err(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);
		return -EPROTO;
	}

	ce = g2h_context_lookup(guc, desc_idx);
	if (unlikely(!ce))
		return -EPROTO;

3544 3545
	trace_intel_context_deregister_done(ce);

3546 3547 3548 3549 3550 3551 3552
#ifdef CONFIG_DRM_I915_SELFTEST
	if (unlikely(ce->drop_deregister)) {
		ce->drop_deregister = false;
		return 0;
	}
#endif

3553 3554 3555 3556 3557 3558 3559 3560 3561 3562
	if (context_wait_for_deregister_to_register(ce)) {
		struct intel_runtime_pm *runtime_pm =
			&ce->engine->gt->i915->runtime_pm;
		intel_wakeref_t wakeref;

		/*
		 * Previous owner of this guc_id has been deregistered, now safe
		 * register this context.
		 */
		with_intel_runtime_pm(runtime_pm, wakeref)
3563
			register_context(ce, true);
3564
		guc_signal_context_fence(ce);
3565 3566 3567
		intel_context_put(ce);
	} else if (context_destroyed(ce)) {
		/* Context has been destroyed */
3568
		intel_gt_pm_put_async(guc_to_gt(guc));
3569
		release_guc_id(guc, ce);
3570
		__guc_context_destroy(ce);
3571 3572
	}

3573 3574
	decr_outstanding_submission_g2h(guc);

3575 3576
	return 0;
}
3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598

int intel_guc_sched_done_process_msg(struct intel_guc *guc,
				     const u32 *msg,
				     u32 len)
{
	struct intel_context *ce;
	unsigned long flags;
	u32 desc_idx = msg[0];

	if (unlikely(len < 2)) {
		drm_err(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);
		return -EPROTO;
	}

	ce = g2h_context_lookup(guc, desc_idx);
	if (unlikely(!ce))
		return -EPROTO;

	if (unlikely(context_destroyed(ce) ||
		     (!context_pending_enable(ce) &&
		     !context_pending_disable(ce)))) {
		drm_err(&guc_to_gt(guc)->i915->drm,
3599
			"Bad context sched_state 0x%x, desc_idx %u",
3600 3601 3602 3603
			ce->guc_state.sched_state, desc_idx);
		return -EPROTO;
	}

3604 3605
	trace_intel_context_sched_done(ce);

3606
	if (context_pending_enable(ce)) {
3607 3608 3609 3610 3611 3612 3613
#ifdef CONFIG_DRM_I915_SELFTEST
		if (unlikely(ce->drop_schedule_enable)) {
			ce->drop_schedule_enable = false;
			return 0;
		}
#endif

3614
		spin_lock_irqsave(&ce->guc_state.lock, flags);
3615
		clr_context_pending_enable(ce);
3616
		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
3617
	} else if (context_pending_disable(ce)) {
3618 3619
		bool banned;

3620 3621 3622 3623 3624 3625 3626
#ifdef CONFIG_DRM_I915_SELFTEST
		if (unlikely(ce->drop_schedule_disable)) {
			ce->drop_schedule_disable = false;
			return 0;
		}
#endif

3627 3628 3629 3630 3631 3632 3633
		/*
		 * Unpin must be done before __guc_signal_context_fence,
		 * otherwise a race exists between the requests getting
		 * submitted + retired before this unpin completes resulting in
		 * the pin_count going to zero and the context still being
		 * enabled.
		 */
3634 3635 3636
		intel_context_sched_disable_unpin(ce);

		spin_lock_irqsave(&ce->guc_state.lock, flags);
3637 3638
		banned = context_banned(ce);
		clr_context_banned(ce);
3639
		clr_context_pending_disable(ce);
3640
		__guc_signal_context_fence(ce);
3641
		guc_blocked_fence_complete(ce);
3642
		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
3643 3644 3645 3646 3647

		if (banned) {
			guc_cancel_context_requests(ce);
			intel_engine_signal_breadcrumbs(ce->engine);
		}
3648 3649
	}

3650
	decr_outstanding_submission_g2h(guc);
3651 3652 3653 3654
	intel_context_put(ce);

	return 0;
}
3655

3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669
static void capture_error_state(struct intel_guc *guc,
				struct intel_context *ce)
{
	struct intel_gt *gt = guc_to_gt(guc);
	struct drm_i915_private *i915 = gt->i915;
	struct intel_engine_cs *engine = __context_to_physical_engine(ce);
	intel_wakeref_t wakeref;

	intel_engine_set_hung_context(engine, ce);
	with_intel_runtime_pm(&i915->runtime_pm, wakeref)
		i915_capture_error_state(gt, engine->mask);
	atomic_inc(&i915->gpu_error.reset_engine_count[engine->uabi_class]);
}

3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681
static void guc_context_replay(struct intel_context *ce)
{
	struct i915_sched_engine *sched_engine = ce->engine->sched_engine;

	__guc_reset_context(ce, true);
	tasklet_hi_schedule(&sched_engine->tasklet);
}

static void guc_handle_context_reset(struct intel_guc *guc,
				     struct intel_context *ce)
{
	trace_intel_context_reset(ce);
3682

3683 3684 3685 3686 3687 3688
	/*
	 * XXX: Racey if request cancellation has occurred, see comment in
	 * __guc_reset_context().
	 */
	if (likely(!intel_context_is_banned(ce) &&
		   !context_blocked(ce))) {
3689 3690 3691
		capture_error_state(guc, ce);
		guc_context_replay(ce);
	}
3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714
}

int intel_guc_context_reset_process_msg(struct intel_guc *guc,
					const u32 *msg, u32 len)
{
	struct intel_context *ce;
	int desc_idx;

	if (unlikely(len != 1)) {
		drm_err(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);
		return -EPROTO;
	}

	desc_idx = msg[0];
	ce = g2h_context_lookup(guc, desc_idx);
	if (unlikely(!ce))
		return -EPROTO;

	guc_handle_context_reset(guc, ce);

	return 0;
}

3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757
static struct intel_engine_cs *
guc_lookup_engine(struct intel_guc *guc, u8 guc_class, u8 instance)
{
	struct intel_gt *gt = guc_to_gt(guc);
	u8 engine_class = guc_class_to_engine_class(guc_class);

	/* Class index is checked in class converter */
	GEM_BUG_ON(instance > MAX_ENGINE_INSTANCE);

	return gt->engine_class[engine_class][instance];
}

int intel_guc_engine_failure_process_msg(struct intel_guc *guc,
					 const u32 *msg, u32 len)
{
	struct intel_engine_cs *engine;
	u8 guc_class, instance;
	u32 reason;

	if (unlikely(len != 3)) {
		drm_err(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);
		return -EPROTO;
	}

	guc_class = msg[0];
	instance = msg[1];
	reason = msg[2];

	engine = guc_lookup_engine(guc, guc_class, instance);
	if (unlikely(!engine)) {
		drm_err(&guc_to_gt(guc)->i915->drm,
			"Invalid engine %d:%d", guc_class, instance);
		return -EPROTO;
	}

	intel_gt_handle_error(guc_to_gt(guc), engine->mask,
			      I915_ERROR_CAPTURE,
			      "GuC failed to reset %s (reason=0x%08x)\n",
			      engine->name, reason);

	return 0;
}

3758 3759 3760 3761 3762 3763
void intel_guc_find_hung_context(struct intel_engine_cs *engine)
{
	struct intel_guc *guc = &engine->gt->uc.guc;
	struct intel_context *ce;
	struct i915_request *rq;
	unsigned long index;
3764
	unsigned long flags;
3765 3766 3767 3768 3769

	/* Reset called during driver load? GuC not yet initialised! */
	if (unlikely(!guc_submission_initialized(guc)))
		return;

3770
	xa_lock_irqsave(&guc->context_lookup, flags);
3771
	xa_for_each(&guc->context_lookup, index, ce) {
3772
		if (!kref_get_unless_zero(&ce->ref))
3773 3774
			continue;

3775 3776 3777 3778 3779
		xa_unlock(&guc->context_lookup);

		if (!intel_context_is_pinned(ce))
			goto next;

3780 3781
		if (intel_engine_is_virtual(ce->engine)) {
			if (!(ce->engine->mask & engine->mask))
3782
				goto next;
3783 3784
		} else {
			if (ce->engine != engine)
3785
				goto next;
3786 3787
		}

3788
		list_for_each_entry(rq, &ce->guc_state.requests, sched.link) {
3789 3790 3791 3792 3793 3794
			if (i915_test_request_state(rq) != I915_REQUEST_ACTIVE)
				continue;

			intel_engine_set_hung_context(engine, ce);

			/* Can only cope with one hang at a time... */
3795 3796 3797
			intel_context_put(ce);
			xa_lock(&guc->context_lookup);
			goto done;
3798
		}
3799 3800 3801
next:
		intel_context_put(ce);
		xa_lock(&guc->context_lookup);
3802
	}
3803 3804
done:
	xa_unlock_irqrestore(&guc->context_lookup, flags);
3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819
}

void intel_guc_dump_active_requests(struct intel_engine_cs *engine,
				    struct i915_request *hung_rq,
				    struct drm_printer *m)
{
	struct intel_guc *guc = &engine->gt->uc.guc;
	struct intel_context *ce;
	unsigned long index;
	unsigned long flags;

	/* Reset called during driver load? GuC not yet initialised! */
	if (unlikely(!guc_submission_initialized(guc)))
		return;

3820
	xa_lock_irqsave(&guc->context_lookup, flags);
3821
	xa_for_each(&guc->context_lookup, index, ce) {
3822
		if (!kref_get_unless_zero(&ce->ref))
3823 3824
			continue;

3825 3826 3827 3828 3829
		xa_unlock(&guc->context_lookup);

		if (!intel_context_is_pinned(ce))
			goto next;

3830 3831
		if (intel_engine_is_virtual(ce->engine)) {
			if (!(ce->engine->mask & engine->mask))
3832
				goto next;
3833 3834
		} else {
			if (ce->engine != engine)
3835
				goto next;
3836 3837
		}

3838 3839
		spin_lock(&ce->guc_state.lock);
		intel_engine_dump_active_requests(&ce->guc_state.requests,
3840
						  hung_rq, m);
3841
		spin_unlock(&ce->guc_state.lock);
3842 3843 3844 3845

next:
		intel_context_put(ce);
		xa_lock(&guc->context_lookup);
3846
	}
3847
	xa_unlock_irqrestore(&guc->context_lookup, flags);
3848 3849
}

3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872
void intel_guc_submission_print_info(struct intel_guc *guc,
				     struct drm_printer *p)
{
	struct i915_sched_engine *sched_engine = guc->sched_engine;
	struct rb_node *rb;
	unsigned long flags;

	if (!sched_engine)
		return;

	drm_printf(p, "GuC Number Outstanding Submission G2H: %u\n",
		   atomic_read(&guc->outstanding_submission_g2h));
	drm_printf(p, "GuC tasklet count: %u\n\n",
		   atomic_read(&sched_engine->tasklet.count));

	spin_lock_irqsave(&sched_engine->lock, flags);
	drm_printf(p, "Requests in GuC submit tasklet:\n");
	for (rb = rb_first_cached(&sched_engine->queue); rb; rb = rb_next(rb)) {
		struct i915_priolist *pl = to_priolist(rb);
		struct i915_request *rq;

		priolist_for_each_request(rq, pl)
			drm_printf(p, "guc_id=%u, seqno=%llu\n",
3873
				   rq->context->guc_id.id,
3874 3875 3876 3877 3878 3879
				   rq->fence.seqno);
	}
	spin_unlock_irqrestore(&sched_engine->lock, flags);
	drm_printf(p, "\n");
}

3880 3881 3882 3883 3884
static inline void guc_log_context_priority(struct drm_printer *p,
					    struct intel_context *ce)
{
	int i;

3885
	drm_printf(p, "\t\tPriority: %d\n", ce->guc_state.prio);
3886 3887 3888 3889
	drm_printf(p, "\t\tNumber Requests (lower index == higher priority)\n");
	for (i = GUC_CLIENT_PRIORITY_KMD_HIGH;
	     i < GUC_CLIENT_PRIORITY_NUM; ++i) {
		drm_printf(p, "\t\tNumber requests in priority band[%d]: %d\n",
3890
			   i, ce->guc_state.prio_count[i]);
3891 3892 3893 3894
	}
	drm_printf(p, "\n");
}

3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913
static inline void guc_log_context(struct drm_printer *p,
				   struct intel_context *ce)
{
	drm_printf(p, "GuC lrc descriptor %u:\n", ce->guc_id.id);
	drm_printf(p, "\tHW Context Desc: 0x%08x\n", ce->lrc.lrca);
	drm_printf(p, "\t\tLRC Head: Internal %u, Memory %u\n",
		   ce->ring->head,
		   ce->lrc_reg_state[CTX_RING_HEAD]);
	drm_printf(p, "\t\tLRC Tail: Internal %u, Memory %u\n",
		   ce->ring->tail,
		   ce->lrc_reg_state[CTX_RING_TAIL]);
	drm_printf(p, "\t\tContext Pin Count: %u\n",
		   atomic_read(&ce->pin_count));
	drm_printf(p, "\t\tGuC ID Ref Count: %u\n",
		   atomic_read(&ce->guc_id.ref));
	drm_printf(p, "\t\tSchedule State: 0x%x\n\n",
		   ce->guc_state.sched_state);
}

3914 3915 3916 3917 3918
void intel_guc_submission_print_context_info(struct intel_guc *guc,
					     struct drm_printer *p)
{
	struct intel_context *ce;
	unsigned long index;
3919
	unsigned long flags;
3920

3921
	xa_lock_irqsave(&guc->context_lookup, flags);
3922
	xa_for_each(&guc->context_lookup, index, ce) {
3923
		GEM_BUG_ON(intel_context_is_child(ce));
3924

3925
		guc_log_context(p, ce);
3926
		guc_log_context_priority(p, ce);
3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940

		if (intel_context_is_parent(ce)) {
			struct guc_process_desc *desc = __get_process_desc(ce);
			struct intel_context *child;

			drm_printf(p, "\t\tNumber children: %u\n",
				   ce->parallel.number_children);
			drm_printf(p, "\t\tWQI Head: %u\n",
				   READ_ONCE(desc->head));
			drm_printf(p, "\t\tWQI Tail: %u\n",
				   READ_ONCE(desc->tail));
			drm_printf(p, "\t\tWQI Status: %u\n\n",
				   READ_ONCE(desc->wq_status));

3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951
			if (ce->engine->emit_bb_start ==
			    emit_bb_start_parent_no_preempt_mid_batch) {
				u8 i;

				drm_printf(p, "\t\tChildren Go: %u\n\n",
					   get_children_go_value(ce));
				for (i = 0; i < ce->parallel.number_children; ++i)
					drm_printf(p, "\t\tChildren Join: %u\n",
						   get_children_join_value(ce, i));
			}

3952 3953 3954
			for_each_child(ce, child)
				guc_log_context(p, child);
		}
3955
	}
3956
	xa_unlock_irqrestore(&guc->context_lookup, flags);
3957
}
3958

3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073
static inline u32 get_children_go_addr(struct intel_context *ce)
{
	GEM_BUG_ON(!intel_context_is_parent(ce));

	return i915_ggtt_offset(ce->state) +
		__get_parent_scratch_offset(ce) +
		offsetof(struct parent_scratch, go.semaphore);
}

static inline u32 get_children_join_addr(struct intel_context *ce,
					 u8 child_index)
{
	GEM_BUG_ON(!intel_context_is_parent(ce));

	return i915_ggtt_offset(ce->state) +
		__get_parent_scratch_offset(ce) +
		offsetof(struct parent_scratch, join[child_index].semaphore);
}

#define PARENT_GO_BB			1
#define PARENT_GO_FINI_BREADCRUMB	0
#define CHILD_GO_BB			1
#define CHILD_GO_FINI_BREADCRUMB	0
static int emit_bb_start_parent_no_preempt_mid_batch(struct i915_request *rq,
						     u64 offset, u32 len,
						     const unsigned int flags)
{
	struct intel_context *ce = rq->context;
	u32 *cs;
	u8 i;

	GEM_BUG_ON(!intel_context_is_parent(ce));

	cs = intel_ring_begin(rq, 10 + 4 * ce->parallel.number_children);
	if (IS_ERR(cs))
		return PTR_ERR(cs);

	/* Wait on children */
	for (i = 0; i < ce->parallel.number_children; ++i) {
		*cs++ = (MI_SEMAPHORE_WAIT |
			 MI_SEMAPHORE_GLOBAL_GTT |
			 MI_SEMAPHORE_POLL |
			 MI_SEMAPHORE_SAD_EQ_SDD);
		*cs++ = PARENT_GO_BB;
		*cs++ = get_children_join_addr(ce, i);
		*cs++ = 0;
	}

	/* Turn off preemption */
	*cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
	*cs++ = MI_NOOP;

	/* Tell children go */
	cs = gen8_emit_ggtt_write(cs,
				  CHILD_GO_BB,
				  get_children_go_addr(ce),
				  0);

	/* Jump to batch */
	*cs++ = MI_BATCH_BUFFER_START_GEN8 |
		(flags & I915_DISPATCH_SECURE ? 0 : BIT(8));
	*cs++ = lower_32_bits(offset);
	*cs++ = upper_32_bits(offset);
	*cs++ = MI_NOOP;

	intel_ring_advance(rq, cs);

	return 0;
}

static int emit_bb_start_child_no_preempt_mid_batch(struct i915_request *rq,
						    u64 offset, u32 len,
						    const unsigned int flags)
{
	struct intel_context *ce = rq->context;
	struct intel_context *parent = intel_context_to_parent(ce);
	u32 *cs;

	GEM_BUG_ON(!intel_context_is_child(ce));

	cs = intel_ring_begin(rq, 12);
	if (IS_ERR(cs))
		return PTR_ERR(cs);

	/* Signal parent */
	cs = gen8_emit_ggtt_write(cs,
				  PARENT_GO_BB,
				  get_children_join_addr(parent,
							 ce->parallel.child_index),
				  0);

	/* Wait on parent for go */
	*cs++ = (MI_SEMAPHORE_WAIT |
		 MI_SEMAPHORE_GLOBAL_GTT |
		 MI_SEMAPHORE_POLL |
		 MI_SEMAPHORE_SAD_EQ_SDD);
	*cs++ = CHILD_GO_BB;
	*cs++ = get_children_go_addr(parent);
	*cs++ = 0;

	/* Turn off preemption */
	*cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;

	/* Jump to batch */
	*cs++ = MI_BATCH_BUFFER_START_GEN8 |
		(flags & I915_DISPATCH_SECURE ? 0 : BIT(8));
	*cs++ = lower_32_bits(offset);
	*cs++ = upper_32_bits(offset);

	intel_ring_advance(rq, cs);

	return 0;
}

static u32 *
4074 4075
__emit_fini_breadcrumb_parent_no_preempt_mid_batch(struct i915_request *rq,
						   u32 *cs)
4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102
{
	struct intel_context *ce = rq->context;
	u8 i;

	GEM_BUG_ON(!intel_context_is_parent(ce));

	/* Wait on children */
	for (i = 0; i < ce->parallel.number_children; ++i) {
		*cs++ = (MI_SEMAPHORE_WAIT |
			 MI_SEMAPHORE_GLOBAL_GTT |
			 MI_SEMAPHORE_POLL |
			 MI_SEMAPHORE_SAD_EQ_SDD);
		*cs++ = PARENT_GO_FINI_BREADCRUMB;
		*cs++ = get_children_join_addr(ce, i);
		*cs++ = 0;
	}

	/* Turn on preemption */
	*cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
	*cs++ = MI_NOOP;

	/* Tell children go */
	cs = gen8_emit_ggtt_write(cs,
				  CHILD_GO_FINI_BREADCRUMB,
				  get_children_go_addr(ce),
				  0);

4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141
	return cs;
}

/*
 * If this true, a submission of multi-lrc requests had an error and the
 * requests need to be skipped. The front end (execuf IOCTL) should've called
 * i915_request_skip which squashes the BB but we still need to emit the fini
 * breadrcrumbs seqno write. At this point we don't know how many of the
 * requests in the multi-lrc submission were generated so we can't do the
 * handshake between the parent and children (e.g. if 4 requests should be
 * generated but 2nd hit an error only 1 would be seen by the GuC backend).
 * Simply skip the handshake, but still emit the breadcrumbd seqno, if an error
 * has occurred on any of the requests in submission / relationship.
 */
static inline bool skip_handshake(struct i915_request *rq)
{
	return test_bit(I915_FENCE_FLAG_SKIP_PARALLEL, &rq->fence.flags);
}

static u32 *
emit_fini_breadcrumb_parent_no_preempt_mid_batch(struct i915_request *rq,
						 u32 *cs)
{
	struct intel_context *ce = rq->context;

	GEM_BUG_ON(!intel_context_is_parent(ce));

	if (unlikely(skip_handshake(rq))) {
		/*
		 * NOP everything in __emit_fini_breadcrumb_parent_no_preempt_mid_batch,
		 * the -6 comes from the length of the emits below.
		 */
		memset(cs, 0, sizeof(u32) *
		       (ce->engine->emit_fini_breadcrumb_dw - 6));
		cs += ce->engine->emit_fini_breadcrumb_dw - 6;
	} else {
		cs = __emit_fini_breadcrumb_parent_no_preempt_mid_batch(rq, cs);
	}

4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157
	/* Emit fini breadcrumb */
	cs = gen8_emit_ggtt_write(cs,
				  rq->fence.seqno,
				  i915_request_active_timeline(rq)->hwsp_offset,
				  0);

	/* User interrupt */
	*cs++ = MI_USER_INTERRUPT;
	*cs++ = MI_NOOP;

	rq->tail = intel_ring_offset(rq, cs);

	return cs;
}

static u32 *
4158 4159
__emit_fini_breadcrumb_child_no_preempt_mid_batch(struct i915_request *rq,
						  u32 *cs)
4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185
{
	struct intel_context *ce = rq->context;
	struct intel_context *parent = intel_context_to_parent(ce);

	GEM_BUG_ON(!intel_context_is_child(ce));

	/* Turn on preemption */
	*cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
	*cs++ = MI_NOOP;

	/* Signal parent */
	cs = gen8_emit_ggtt_write(cs,
				  PARENT_GO_FINI_BREADCRUMB,
				  get_children_join_addr(parent,
							 ce->parallel.child_index),
				  0);

	/* Wait parent on for go */
	*cs++ = (MI_SEMAPHORE_WAIT |
		 MI_SEMAPHORE_GLOBAL_GTT |
		 MI_SEMAPHORE_POLL |
		 MI_SEMAPHORE_SAD_EQ_SDD);
	*cs++ = CHILD_GO_FINI_BREADCRUMB;
	*cs++ = get_children_go_addr(parent);
	*cs++ = 0;

4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208
	return cs;
}

static u32 *
emit_fini_breadcrumb_child_no_preempt_mid_batch(struct i915_request *rq,
						u32 *cs)
{
	struct intel_context *ce = rq->context;

	GEM_BUG_ON(!intel_context_is_child(ce));

	if (unlikely(skip_handshake(rq))) {
		/*
		 * NOP everything in __emit_fini_breadcrumb_child_no_preempt_mid_batch,
		 * the -6 comes from the length of the emits below.
		 */
		memset(cs, 0, sizeof(u32) *
		       (ce->engine->emit_fini_breadcrumb_dw - 6));
		cs += ce->engine->emit_fini_breadcrumb_dw - 6;
	} else {
		cs = __emit_fini_breadcrumb_child_no_preempt_mid_batch(rq, cs);
	}

4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223
	/* Emit fini breadcrumb */
	cs = gen8_emit_ggtt_write(cs,
				  rq->fence.seqno,
				  i915_request_active_timeline(rq)->hwsp_offset,
				  0);

	/* User interrupt */
	*cs++ = MI_USER_INTERRUPT;
	*cs++ = MI_NOOP;

	rq->tail = intel_ring_offset(rq, cs);

	return cs;
}

4224
static struct intel_context *
4225 4226
guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count,
		   unsigned long flags)
4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254
{
	struct guc_virtual_engine *ve;
	struct intel_guc *guc;
	unsigned int n;
	int err;

	ve = kzalloc(sizeof(*ve), GFP_KERNEL);
	if (!ve)
		return ERR_PTR(-ENOMEM);

	guc = &siblings[0]->gt->uc.guc;

	ve->base.i915 = siblings[0]->i915;
	ve->base.gt = siblings[0]->gt;
	ve->base.uncore = siblings[0]->uncore;
	ve->base.id = -1;

	ve->base.uabi_class = I915_ENGINE_CLASS_INVALID;
	ve->base.instance = I915_ENGINE_CLASS_INVALID_VIRTUAL;
	ve->base.uabi_instance = I915_ENGINE_CLASS_INVALID_VIRTUAL;
	ve->base.saturated = ALL_ENGINES;

	snprintf(ve->base.name, sizeof(ve->base.name), "virtual");

	ve->base.sched_engine = i915_sched_engine_get(guc->sched_engine);

	ve->base.cops = &virtual_guc_context_ops;
	ve->base.request_alloc = guc_request_alloc;
4255
	ve->base.bump_serial = virtual_guc_bump_serial;
4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274

	ve->base.submit_request = guc_submit_request;

	ve->base.flags = I915_ENGINE_IS_VIRTUAL;

	intel_context_init(&ve->context, &ve->base);

	for (n = 0; n < count; n++) {
		struct intel_engine_cs *sibling = siblings[n];

		GEM_BUG_ON(!is_power_of_2(sibling->mask));
		if (sibling->mask & ve->base.mask) {
			DRM_DEBUG("duplicate %s entry in load balancer\n",
				  sibling->name);
			err = -EINVAL;
			goto err_put;
		}

		ve->base.mask |= sibling->mask;
4275
		ve->base.logical_mask |= sibling->logical_mask;
4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288

		if (n != 0 && ve->base.class != sibling->class) {
			DRM_DEBUG("invalid mixing of engine class, sibling %d, already %d\n",
				  sibling->class, ve->base.class);
			err = -EINVAL;
			goto err_put;
		} else if (n == 0) {
			ve->base.class = sibling->class;
			ve->base.uabi_class = sibling->uabi_class;
			snprintf(ve->base.name, sizeof(ve->base.name),
				 "v%dx%d", ve->base.class, count);
			ve->base.context_size = sibling->context_size;

4289 4290 4291 4292
			ve->base.add_active_request =
				sibling->add_active_request;
			ve->base.remove_active_request =
				sibling->remove_active_request;
4293 4294 4295 4296 4297 4298 4299 4300
			ve->base.emit_bb_start = sibling->emit_bb_start;
			ve->base.emit_flush = sibling->emit_flush;
			ve->base.emit_init_breadcrumb =
				sibling->emit_init_breadcrumb;
			ve->base.emit_fini_breadcrumb =
				sibling->emit_fini_breadcrumb;
			ve->base.emit_fini_breadcrumb_dw =
				sibling->emit_fini_breadcrumb_dw;
4301 4302
			ve->base.breadcrumbs =
				intel_breadcrumbs_get(sibling->breadcrumbs);
4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330

			ve->base.flags |= sibling->flags;

			ve->base.props.timeslice_duration_ms =
				sibling->props.timeslice_duration_ms;
			ve->base.props.preempt_timeout_ms =
				sibling->props.preempt_timeout_ms;
		}
	}

	return &ve->context;

err_put:
	intel_context_put(&ve->context);
	return ERR_PTR(err);
}

bool intel_guc_virtual_engine_has_heartbeat(const struct intel_engine_cs *ve)
{
	struct intel_engine_cs *engine;
	intel_engine_mask_t tmp, mask = ve->mask;

	for_each_engine_masked(engine, ve->gt, mask, tmp)
		if (READ_ONCE(engine->props.heartbeat_interval_ms))
			return true;

	return false;
}
4331 4332 4333

#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
#include "selftest_guc.c"
4334
#include "selftest_guc_multi_lrc.c"
4335
#endif