intel_gt.c 31.2 KB
Newer Older
1 2 3 4 5
// SPDX-License-Identifier: MIT
/*
 * Copyright © 2019 Intel Corporation
 */

A
Andi Shyti 已提交
6 7
#include <drm/drm_managed.h>

8
#include "gem/i915_gem_internal.h"
9
#include "gem/i915_gem_lmem.h"
10 11
#include "pxp/intel_pxp.h"

12
#include "i915_drv.h"
13
#include "intel_context.h"
14
#include "intel_engine_regs.h"
15
#include "intel_gt.h"
16
#include "intel_gt_buffer_pool.h"
17
#include "intel_gt_clock_utils.h"
18
#include "intel_gt_debugfs.h"
19
#include "intel_gt_gmch.h"
20
#include "intel_gt_pm.h"
21
#include "intel_gt_regs.h"
22
#include "intel_gt_requests.h"
23
#include "intel_migrate.h"
24
#include "intel_mocs.h"
25
#include "intel_pm.h"
26
#include "intel_rc6.h"
27
#include "intel_renderstate.h"
28
#include "intel_rps.h"
29
#include "intel_gt_sysfs.h"
30
#include "intel_uncore.h"
31
#include "shmem_utils.h"
32

33
static void __intel_gt_init_early(struct intel_gt *gt)
34
{
35 36
	spin_lock_init(&gt->irq_lock);

37 38
	mutex_init(&gt->tlb_invalidate_lock);

39
	INIT_LIST_HEAD(&gt->closed_vma);
40
	spin_lock_init(&gt->closed_lock);
41

42 43 44
	init_llist_head(&gt->watchdog.list);
	INIT_WORK(&gt->watchdog.work, intel_gt_watchdog_work);

45
	intel_gt_init_buffer_pool(gt);
46
	intel_gt_init_reset(gt);
47
	intel_gt_init_requests(gt);
48
	intel_gt_init_timelines(gt);
49
	intel_gt_pm_init_early(gt);
50

51
	intel_uc_init_early(&gt->uc);
52
	intel_rps_init_early(&gt->rps);
53
}
54

55 56
/* Preliminary initialization of Tile 0 */
void intel_root_gt_init_early(struct drm_i915_private *i915)
57
{
58 59
	struct intel_gt *gt = to_gt(i915);

60 61
	gt->i915 = i915;
	gt->uncore = &i915->uncore;
62 63

	__intel_gt_init_early(gt);
64 65
}

66
static int intel_gt_probe_lmem(struct intel_gt *gt)
67 68
{
	struct drm_i915_private *i915 = gt->i915;
69 70
	unsigned int instance = gt->info.id;
	int id = INTEL_REGION_LMEM_0 + instance;
71 72 73
	struct intel_memory_region *mem;
	int err;

M
Matthew Auld 已提交
74
	mem = intel_gt_setup_lmem(gt);
75 76 77 78 79 80 81 82 83 84 85 86
	if (IS_ERR(mem)) {
		err = PTR_ERR(mem);
		if (err == -ENODEV)
			return 0;

		drm_err(&i915->drm,
			"Failed to setup region(%d) type=%d\n",
			err, INTEL_MEMORY_LOCAL);
		return err;
	}

	mem->id = id;
87
	mem->instance = instance;
88

89 90
	intel_memory_region_set_name(mem, "local%u", mem->instance);

91 92 93 94 95 96 97
	GEM_BUG_ON(!HAS_REGION(i915, id));
	GEM_BUG_ON(i915->mm.regions[id]);
	i915->mm.regions[id] = mem;

	return 0;
}

A
Andi Shyti 已提交
98
int intel_gt_assign_ggtt(struct intel_gt *gt)
99
{
A
Andi Shyti 已提交
100 101 102
	gt->ggtt = drmm_kzalloc(&gt->i915->drm, sizeof(*gt->ggtt), GFP_KERNEL);

	return gt->ggtt ? 0 : -ENOMEM;
103 104
}

105 106 107 108
static const char * const intel_steering_types[] = {
	"L3BANK",
	"MSLICE",
	"LNCF",
M
Matt Roper 已提交
109
	"INSTANCE 0",
110 111
};

112 113 114 115 116
static const struct intel_mmio_range icl_l3bank_steering_table[] = {
	{ 0x00B100, 0x00B3FF },
	{},
};

117 118 119 120 121 122 123 124 125 126 127 128 129 130
static const struct intel_mmio_range xehpsdv_mslice_steering_table[] = {
	{ 0x004000, 0x004AFF },
	{ 0x00C800, 0x00CFFF },
	{ 0x00DD00, 0x00DDFF },
	{ 0x00E900, 0x00FFFF }, /* 0xEA00 - OxEFFF is unused */
	{},
};

static const struct intel_mmio_range xehpsdv_lncf_steering_table[] = {
	{ 0x00B000, 0x00B0FF },
	{ 0x00D800, 0x00D8FF },
	{},
};

131 132 133 134 135 136
static const struct intel_mmio_range dg2_lncf_steering_table[] = {
	{ 0x00B000, 0x00B0FF },
	{ 0x00D880, 0x00D8FF },
	{},
};

M
Matt Roper 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
/*
 * We have several types of MCR registers on PVC where steering to (0,0)
 * will always provide us with a non-terminated value.  We'll stick them
 * all in the same table for simplicity.
 */
static const struct intel_mmio_range pvc_instance0_steering_table[] = {
	{ 0x004000, 0x004AFF },		/* HALF-BSLICE */
	{ 0x008800, 0x00887F },		/* CC */
	{ 0x008A80, 0x008AFF },		/* TILEPSMI */
	{ 0x00B000, 0x00B0FF },		/* HALF-BSLICE */
	{ 0x00B100, 0x00B3FF },		/* L3BANK */
	{ 0x00C800, 0x00CFFF },		/* HALF-BSLICE */
	{ 0x00D800, 0x00D8FF },		/* HALF-BSLICE */
	{ 0x00DD00, 0x00DDFF },		/* BSLICE */
	{ 0x00E900, 0x00E9FF },		/* HALF-BSLICE */
	{ 0x00EC00, 0x00EEFF },		/* HALF-BSLICE */
	{ 0x00F000, 0x00FFFF },		/* HALF-BSLICE */
	{ 0x024180, 0x0241FF },		/* HALF-BSLICE */
	{},
};

158 159
int intel_gt_init_mmio(struct intel_gt *gt)
{
160 161
	struct drm_i915_private *i915 = gt->i915;

162 163
	intel_gt_init_clock_frequency(gt);

164
	intel_uc_init_mmio(&gt->uc);
165
	intel_sseu_info_init(gt);
166

167 168 169 170
	/*
	 * An mslice is unavailable only if both the meml3 for the slice is
	 * disabled *and* all of the DSS in the slice (quadrant) are disabled.
	 */
M
Matt Roper 已提交
171
	if (HAS_MSLICE_STEERING(i915)) {
172
		gt->info.mslice_mask =
173 174 175
			intel_slicemask_from_xehp_dssmask(gt->info.sseu.subslice_mask,
							  GEN_DSS_PER_MSLICE);
		gt->info.mslice_mask |=
176 177
			(intel_uncore_read(gt->uncore, GEN10_MIRROR_FUSE3) &
			 GEN12_MEML3_EN_MASK);
178

179 180 181
		if (!gt->info.mslice_mask) /* should be impossible! */
			drm_warn(&i915->drm, "mslice mask all zero!\n");
	}
182

M
Matt Roper 已提交
183 184 185
	if (IS_PONTEVECCHIO(i915)) {
		gt->steering_table[INSTANCE0] = pvc_instance0_steering_table;
	} else if (IS_DG2(i915)) {
186 187 188
		gt->steering_table[MSLICE] = xehpsdv_mslice_steering_table;
		gt->steering_table[LNCF] = dg2_lncf_steering_table;
	} else if (IS_XEHPSDV(i915)) {
189 190 191
		gt->steering_table[MSLICE] = xehpsdv_mslice_steering_table;
		gt->steering_table[LNCF] = xehpsdv_lncf_steering_table;
	} else if (GRAPHICS_VER(i915) >= 11 &&
192
		   GRAPHICS_VER_FULL(i915) < IP_VER(12, 50)) {
193 194 195 196
		gt->steering_table[L3BANK] = icl_l3bank_steering_table;
		gt->info.l3bank_mask =
			~intel_uncore_read(gt->uncore, GEN10_MIRROR_FUSE3) &
			GEN10_L3BANK_MASK;
197 198
		if (!gt->info.l3bank_mask) /* should be impossible! */
			drm_warn(&i915->drm, "L3 bank mask is all zero!\n");
M
Matt Roper 已提交
199 200 201 202 203
	} else if (GRAPHICS_VER(i915) >= 11) {
		/*
		 * We expect all modern platforms to have at least some
		 * type of steering that needs to be initialized.
		 */
204
		MISSING_CASE(INTEL_INFO(i915)->platform);
205 206
	}

207 208 209
	return intel_engines_init_mmio(gt);
}

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
static void init_unused_ring(struct intel_gt *gt, u32 base)
{
	struct intel_uncore *uncore = gt->uncore;

	intel_uncore_write(uncore, RING_CTL(base), 0);
	intel_uncore_write(uncore, RING_HEAD(base), 0);
	intel_uncore_write(uncore, RING_TAIL(base), 0);
	intel_uncore_write(uncore, RING_START(base), 0);
}

static void init_unused_rings(struct intel_gt *gt)
{
	struct drm_i915_private *i915 = gt->i915;

	if (IS_I830(i915)) {
		init_unused_ring(gt, PRB1_BASE);
		init_unused_ring(gt, SRB0_BASE);
		init_unused_ring(gt, SRB1_BASE);
		init_unused_ring(gt, SRB2_BASE);
		init_unused_ring(gt, SRB3_BASE);
230
	} else if (GRAPHICS_VER(i915) == 2) {
231 232
		init_unused_ring(gt, SRB0_BASE);
		init_unused_ring(gt, SRB1_BASE);
233
	} else if (GRAPHICS_VER(i915) == 3) {
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
		init_unused_ring(gt, PRB1_BASE);
		init_unused_ring(gt, PRB2_BASE);
	}
}

int intel_gt_init_hw(struct intel_gt *gt)
{
	struct drm_i915_private *i915 = gt->i915;
	struct intel_uncore *uncore = gt->uncore;
	int ret;

	gt->last_init_time = ktime_get();

	/* Double layer security blanket, see i915_gem_init() */
	intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);

250
	if (HAS_EDRAM(i915) && GRAPHICS_VER(i915) < 9)
251 252 253 254
		intel_uncore_rmw(uncore, HSW_IDICR, 0, IDIHASHMSK(0xf));

	if (IS_HASWELL(i915))
		intel_uncore_write(uncore,
255
				   HSW_MI_PREDICATE_RESULT_2,
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
				   IS_HSW_GT3(i915) ?
				   LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);

	/* Apply the GT workarounds... */
	intel_gt_apply_workarounds(gt);
	/* ...and determine whether they are sticking. */
	intel_gt_verify_workarounds(gt, "init");

	intel_gt_init_swizzling(gt);

	/*
	 * At least 830 can leave some of the unused rings
	 * "active" (ie. head != tail) after resume which
	 * will prevent c3 entry. Makes sure all unused rings
	 * are totally idle.
	 */
	init_unused_rings(gt);

	ret = i915_ppgtt_init_hw(gt);
	if (ret) {
		DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
		goto out;
	}

	/* We can't enable contexts until all firmware is loaded */
	ret = intel_uc_init_hw(&gt->uc);
	if (ret) {
		i915_probe_error(i915, "Enabling uc failed (%d)\n", ret);
		goto out;
	}

	intel_mocs_init(gt);

out:
	intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
	return ret;
}

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
static void rmw_set(struct intel_uncore *uncore, i915_reg_t reg, u32 set)
{
	intel_uncore_rmw(uncore, reg, 0, set);
}

static void rmw_clear(struct intel_uncore *uncore, i915_reg_t reg, u32 clr)
{
	intel_uncore_rmw(uncore, reg, clr, 0);
}

static void clear_register(struct intel_uncore *uncore, i915_reg_t reg)
{
	intel_uncore_rmw(uncore, reg, 0, 0);
}

309
static void gen6_clear_engine_error_register(struct intel_engine_cs *engine)
310 311 312 313 314 315 316 317 318 319 320 321 322
{
	GEN6_RING_FAULT_REG_RMW(engine, RING_FAULT_VALID, 0);
	GEN6_RING_FAULT_REG_POSTING_READ(engine);
}

void
intel_gt_clear_error_registers(struct intel_gt *gt,
			       intel_engine_mask_t engine_mask)
{
	struct drm_i915_private *i915 = gt->i915;
	struct intel_uncore *uncore = gt->uncore;
	u32 eir;

323
	if (GRAPHICS_VER(i915) != 2)
324 325
		clear_register(uncore, PGTBL_ER);

326
	if (GRAPHICS_VER(i915) < 4)
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
		clear_register(uncore, IPEIR(RENDER_RING_BASE));
	else
		clear_register(uncore, IPEIR_I965);

	clear_register(uncore, EIR);
	eir = intel_uncore_read(uncore, EIR);
	if (eir) {
		/*
		 * some errors might have become stuck,
		 * mask them.
		 */
		DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masking\n", eir);
		rmw_set(uncore, EMR, eir);
		intel_uncore_write(uncore, GEN2_IIR,
				   I915_MASTER_ERROR_INTERRUPT);
	}

344
	if (GRAPHICS_VER(i915) >= 12) {
345 346
		rmw_clear(uncore, GEN12_RING_FAULT_REG, RING_FAULT_VALID);
		intel_uncore_posting_read(uncore, GEN12_RING_FAULT_REG);
347
	} else if (GRAPHICS_VER(i915) >= 8) {
348 349
		rmw_clear(uncore, GEN8_RING_FAULT_REG, RING_FAULT_VALID);
		intel_uncore_posting_read(uncore, GEN8_RING_FAULT_REG);
350
	} else if (GRAPHICS_VER(i915) >= 6) {
351 352 353
		struct intel_engine_cs *engine;
		enum intel_engine_id id;

354
		for_each_engine_masked(engine, gt, engine_mask, id)
355
			gen6_clear_engine_error_register(engine);
356 357 358 359 360 361 362 363 364
	}
}

static void gen6_check_faults(struct intel_gt *gt)
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
	u32 fault;

365
	for_each_engine(engine, gt, id) {
366 367
		fault = GEN6_RING_FAULT_REG_READ(engine);
		if (fault & RING_FAULT_VALID) {
368 369 370 371 372 373 374 375 376 377
			drm_dbg(&engine->i915->drm, "Unexpected fault\n"
				"\tAddr: 0x%08lx\n"
				"\tAddress space: %s\n"
				"\tSource ID: %d\n"
				"\tType: %d\n",
				fault & PAGE_MASK,
				fault & RING_FAULT_GTTSEL_MASK ?
				"GGTT" : "PPGTT",
				RING_FAULT_SRCID(fault),
				RING_FAULT_FAULT_TYPE(fault));
378 379 380 381 382 383 384
		}
	}
}

static void gen8_check_faults(struct intel_gt *gt)
{
	struct intel_uncore *uncore = gt->uncore;
385 386 387
	i915_reg_t fault_reg, fault_data0_reg, fault_data1_reg;
	u32 fault;

388
	if (GRAPHICS_VER(gt->i915) >= 12) {
389 390 391 392 393 394 395 396
		fault_reg = GEN12_RING_FAULT_REG;
		fault_data0_reg = GEN12_FAULT_TLB_DATA0;
		fault_data1_reg = GEN12_FAULT_TLB_DATA1;
	} else {
		fault_reg = GEN8_RING_FAULT_REG;
		fault_data0_reg = GEN8_FAULT_TLB_DATA0;
		fault_data1_reg = GEN8_FAULT_TLB_DATA1;
	}
397

398
	fault = intel_uncore_read(uncore, fault_reg);
399 400 401 402
	if (fault & RING_FAULT_VALID) {
		u32 fault_data0, fault_data1;
		u64 fault_addr;

403 404 405
		fault_data0 = intel_uncore_read(uncore, fault_data0_reg);
		fault_data1 = intel_uncore_read(uncore, fault_data1_reg);

406 407 408
		fault_addr = ((u64)(fault_data1 & FAULT_VA_HIGH_BITS) << 44) |
			     ((u64)fault_data0 << 12);

409 410 411 412 413 414 415 416 417 418 419
		drm_dbg(&uncore->i915->drm, "Unexpected fault\n"
			"\tAddr: 0x%08x_%08x\n"
			"\tAddress space: %s\n"
			"\tEngine ID: %d\n"
			"\tSource ID: %d\n"
			"\tType: %d\n",
			upper_32_bits(fault_addr), lower_32_bits(fault_addr),
			fault_data1 & FAULT_GTT_SEL ? "GGTT" : "PPGTT",
			GEN8_RING_FAULT_ENGINE_ID(fault),
			RING_FAULT_SRCID(fault),
			RING_FAULT_FAULT_TYPE(fault));
420 421 422 423 424 425 426 427
	}
}

void intel_gt_check_and_clear_faults(struct intel_gt *gt)
{
	struct drm_i915_private *i915 = gt->i915;

	/* From GEN8 onwards we only have one 'All Engine Fault Register' */
428
	if (GRAPHICS_VER(i915) >= 8)
429
		gen8_check_faults(gt);
430
	else if (GRAPHICS_VER(i915) >= 6)
431 432 433 434 435 436
		gen6_check_faults(gt);
	else
		return;

	intel_gt_clear_error_registers(gt, ALL_ENGINES);
}
437 438 439

void intel_gt_flush_ggtt_writes(struct intel_gt *gt)
{
440
	struct intel_uncore *uncore = gt->uncore;
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
	intel_wakeref_t wakeref;

	/*
	 * No actual flushing is required for the GTT write domain for reads
	 * from the GTT domain. Writes to it "immediately" go to main memory
	 * as far as we know, so there's no chipset flush. It also doesn't
	 * land in the GPU render cache.
	 *
	 * However, we do have to enforce the order so that all writes through
	 * the GTT land before any writes to the device, such as updates to
	 * the GATT itself.
	 *
	 * We also have to wait a bit for the writes to land from the GTT.
	 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
	 * timing. This issue has only been observed when switching quickly
	 * between GTT writes and CPU reads from inside the kernel on recent hw,
	 * and it appears to only affect discrete GTT blocks (i.e. on LLC
	 * system agents we cannot reproduce this behaviour, until Cannonlake
	 * that was!).
	 */

	wmb();

464
	if (INTEL_INFO(gt->i915)->has_coherent_ggtt)
465 466
		return;

467
	intel_gt_chipset_flush(gt);
468

469
	with_intel_runtime_pm_if_in_use(uncore->rpm, wakeref) {
470
		unsigned long flags;
471

472
		spin_lock_irqsave(&uncore->lock, flags);
473 474
		intel_uncore_posting_read_fw(uncore,
					     RING_HEAD(RENDER_RING_BASE));
475
		spin_unlock_irqrestore(&uncore->lock, flags);
476 477
	}
}
478 479 480 481

void intel_gt_chipset_flush(struct intel_gt *gt)
{
	wmb();
482
	if (GRAPHICS_VER(gt->i915) < 6)
483
		intel_gt_gmch_gen5_chipset_flush(gt);
484
}
485

486 487
void intel_gt_driver_register(struct intel_gt *gt)
{
488 489
	intel_gsc_init(&gt->gsc, gt->i915);

490
	intel_rps_driver_register(&gt->rps);
491

492
	intel_gt_debugfs_register(gt);
493
	intel_gt_sysfs_register(gt);
494 495 496
}

static int intel_gt_init_scratch(struct intel_gt *gt, unsigned int size)
497 498 499 500 501 502
{
	struct drm_i915_private *i915 = gt->i915;
	struct drm_i915_gem_object *obj;
	struct i915_vma *vma;
	int ret;

503 504 505
	obj = i915_gem_object_create_lmem(i915, size,
					  I915_BO_ALLOC_VOLATILE |
					  I915_BO_ALLOC_GPU_ONLY);
506 507
	if (IS_ERR(obj))
		obj = i915_gem_object_create_stolen(i915, size);
508
	if (IS_ERR(obj))
509 510
		obj = i915_gem_object_create_internal(i915, size);
	if (IS_ERR(obj)) {
511
		drm_err(&i915->drm, "Failed to allocate scratch page\n");
512 513 514 515 516 517 518 519 520
		return PTR_ERR(obj);
	}

	vma = i915_vma_instance(obj, &gt->ggtt->vm, NULL);
	if (IS_ERR(vma)) {
		ret = PTR_ERR(vma);
		goto err_unref;
	}

521
	ret = i915_ggtt_pin(vma, NULL, 0, PIN_HIGH);
522 523 524
	if (ret)
		goto err_unref;

525 526
	gt->scratch = i915_vma_make_unshrinkable(vma);

527 528 529 530 531 532 533
	return 0;

err_unref:
	i915_gem_object_put(obj);
	return ret;
}

534
static void intel_gt_fini_scratch(struct intel_gt *gt)
535 536 537
{
	i915_vma_unpin_and_release(&gt->scratch, 0);
}
538

539 540 541
static struct i915_address_space *kernel_vm(struct intel_gt *gt)
{
	if (INTEL_PPGTT(gt->i915) > INTEL_PPGTT_ALIASING)
542
		return &i915_ppgtt_create(gt, I915_BO_ALLOC_PM_EARLY)->vm;
543 544 545 546
	else
		return i915_vm_get(&gt->ggtt->vm);
}

547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
static int __engines_record_defaults(struct intel_gt *gt)
{
	struct i915_request *requests[I915_NUM_ENGINES] = {};
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
	int err = 0;

	/*
	 * As we reset the gpu during very early sanitisation, the current
	 * register state on the GPU should reflect its defaults values.
	 * We load a context onto the hw (with restore-inhibit), then switch
	 * over to a second context to save that default register state. We
	 * can then prime every new context with that state so they all start
	 * from the same default HW values.
	 */

	for_each_engine(engine, gt, id) {
		struct intel_renderstate so;
		struct intel_context *ce;
		struct i915_request *rq;

568 569 570
		/* We must be able to switch to something! */
		GEM_BUG_ON(!engine->kernel_context);

571 572 573 574 575 576
		ce = intel_context_create(engine);
		if (IS_ERR(ce)) {
			err = PTR_ERR(ce);
			goto out;
		}

577 578 579 580 581
		err = intel_renderstate_init(&so, ce);
		if (err)
			goto err;

		rq = i915_request_create(ce);
582 583
		if (IS_ERR(rq)) {
			err = PTR_ERR(rq);
584
			goto err_fini;
585 586 587 588 589 590 591 592 593 594 595 596 597
		}

		err = intel_engine_emit_ctx_wa(rq);
		if (err)
			goto err_rq;

		err = intel_renderstate_emit(&so, rq);
		if (err)
			goto err_rq;

err_rq:
		requests[id] = i915_request_get(rq);
		i915_request_add(rq);
598 599 600 601 602
err_fini:
		intel_renderstate_fini(&so, ce);
err:
		if (err) {
			intel_context_put(ce);
603
			goto out;
604
		}
605 606 607 608 609 610 611 612 613 614
	}

	/* Flush the default context image to memory, and enable powersaving. */
	if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
		err = -EIO;
		goto out;
	}

	for (id = 0; id < ARRAY_SIZE(requests); id++) {
		struct i915_request *rq;
615
		struct file *state;
616 617 618 619 620

		rq = requests[id];
		if (!rq)
			continue;

621 622 623 624 625
		if (rq->fence.error) {
			err = -EIO;
			goto out;
		}

626
		GEM_BUG_ON(!test_bit(CONTEXT_ALLOC_BIT, &rq->context->flags));
627
		if (!rq->context->state)
628 629
			continue;

630 631 632 633
		/* Keep a copy of the state's backing pages; free the obj */
		state = shmem_create_from_object(rq->context->state->obj);
		if (IS_ERR(state)) {
			err = PTR_ERR(state);
634 635
			goto out;
		}
636
		rq->engine->default_state = state;
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
	}

out:
	/*
	 * If we have to abandon now, we expect the engines to be idle
	 * and ready to be torn-down. The quickest way we can accomplish
	 * this is by declaring ourselves wedged.
	 */
	if (err)
		intel_gt_set_wedged(gt);

	for (id = 0; id < ARRAY_SIZE(requests); id++) {
		struct intel_context *ce;
		struct i915_request *rq;

		rq = requests[id];
		if (!rq)
			continue;

		ce = rq->context;
		i915_request_put(rq);
		intel_context_put(ce);
	}
	return err;
}

static int __engines_verify_workarounds(struct intel_gt *gt)
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
	int err = 0;

	if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
		return 0;

	for_each_engine(engine, gt, id) {
		if (intel_engine_verify_workarounds(engine, "load"))
			err = -EIO;
	}

677 678 679 680
	/* Flush and restore the kernel context for safety */
	if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME)
		err = -EIO;

681 682 683 684 685
	return err;
}

static void __intel_gt_disable(struct intel_gt *gt)
{
686
	intel_gt_set_wedged_on_fini(gt);
687 688 689 690 691 692 693

	intel_gt_suspend_prepare(gt);
	intel_gt_suspend_late(gt);

	GEM_BUG_ON(intel_gt_pm_is_awake(gt));
}

694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout)
{
	long remaining_timeout;

	/* If the device is asleep, we have no requests outstanding */
	if (!intel_gt_pm_is_awake(gt))
		return 0;

	while ((timeout = intel_gt_retire_requests_timeout(gt, timeout,
							   &remaining_timeout)) > 0) {
		cond_resched();
		if (signal_pending(current))
			return -EINTR;
	}

	return timeout ? timeout : intel_uc_wait_for_idle(&gt->uc,
							  remaining_timeout);
}

713 714 715 716
int intel_gt_init(struct intel_gt *gt)
{
	int err;

717
	err = i915_inject_probe_error(gt->i915, -ENODEV);
718 719 720
	if (err)
		return err;

721 722
	intel_gt_init_workarounds(gt);

723 724 725 726 727 728 729 730 731
	/*
	 * This is just a security blanket to placate dragons.
	 * On some systems, we very sporadically observe that the first TLBs
	 * used by the CS may be stale, despite us poking the TLB reset. If
	 * we hold the forcewake during initialisation these problems
	 * just magically go away.
	 */
	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);

732 733
	err = intel_gt_init_scratch(gt,
				    GRAPHICS_VER(gt->i915) == 2 ? SZ_256K : SZ_4K);
734 735 736
	if (err)
		goto out_fw;

737 738
	intel_gt_pm_init(gt);

739 740 741
	gt->vm = kernel_vm(gt);
	if (!gt->vm) {
		err = -ENOMEM;
742
		goto err_pm;
743 744
	}

745
	intel_set_mocs_index(gt);
746

747 748 749 750
	err = intel_engines_init(gt);
	if (err)
		goto err_engines;

751 752 753
	err = intel_uc_init(&gt->uc);
	if (err)
		goto err_engines;
754 755 756

	err = intel_gt_resume(gt);
	if (err)
757
		goto err_uc_init;
758

759 760 761 762 763
	err = intel_gt_init_hwconfig(gt);
	if (err)
		drm_err(&gt->i915->drm, "Failed to retrieve hwconfig table: %pe\n",
			ERR_PTR(err));

764 765 766 767 768 769 770 771
	err = __engines_record_defaults(gt);
	if (err)
		goto err_gt;

	err = __engines_verify_workarounds(gt);
	if (err)
		goto err_gt;

772 773
	intel_uc_init_late(&gt->uc);

774 775 776 777
	err = i915_inject_probe_error(gt->i915, -EIO);
	if (err)
		goto err_gt;

778 779
	intel_migrate_init(&gt->migrate, gt);

780 781
	intel_pxp_init(&gt->pxp);

782 783 784 785 786 787 788 789 790 791 792
	goto out_fw;
err_gt:
	__intel_gt_disable(gt);
	intel_uc_fini_hw(&gt->uc);
err_uc_init:
	intel_uc_fini(&gt->uc);
err_engines:
	intel_engines_release(gt);
	i915_vm_put(fetch_and_zero(&gt->vm));
err_pm:
	intel_gt_pm_fini(gt);
793
	intel_gt_fini_scratch(gt);
794 795 796 797
out_fw:
	if (err)
		intel_gt_set_wedged_on_init(gt);
	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
798
	return err;
799 800 801 802
}

void intel_gt_driver_remove(struct intel_gt *gt)
{
803 804
	__intel_gt_disable(gt);

805
	intel_migrate_fini(&gt->migrate);
806
	intel_uc_driver_remove(&gt->uc);
807 808

	intel_engines_release(gt);
809 810

	intel_gt_flush_buffer_pool(gt);
811 812 813 814
}

void intel_gt_driver_unregister(struct intel_gt *gt)
{
815 816
	intel_wakeref_t wakeref;

817
	intel_gt_sysfs_unregister(gt);
818
	intel_rps_driver_unregister(&gt->rps);
819
	intel_gsc_fini(&gt->gsc);
820

821 822
	intel_pxp_fini(&gt->pxp);

823 824 825 826 827
	/*
	 * Upon unregistering the device to prevent any new users, cancel
	 * all in-flight requests so that we can quickly unbind the active
	 * resources.
	 */
828
	intel_gt_set_wedged_on_fini(gt);
829 830 831 832

	/* Scrub all HW state upon release */
	with_intel_runtime_pm(gt->uncore->rpm, wakeref)
		__intel_gt_reset(gt, ALL_ENGINES);
833 834 835 836
}

void intel_gt_driver_release(struct intel_gt *gt)
{
837 838 839 840 841 842
	struct i915_address_space *vm;

	vm = fetch_and_zero(&gt->vm);
	if (vm) /* FIXME being called twice on error paths :( */
		i915_vm_put(vm);

843
	intel_wa_list_free(&gt->wa_list);
844
	intel_gt_pm_fini(gt);
845
	intel_gt_fini_scratch(gt);
846
	intel_gt_fini_buffer_pool(gt);
847
	intel_gt_fini_hwconfig(gt);
848 849
}

850
void intel_gt_driver_late_release_all(struct drm_i915_private *i915)
851
{
852 853 854
	struct intel_gt *gt;
	unsigned int id;

855 856 857
	/* We need to wait for inflight RCU frees to release their grip */
	rcu_barrier();

858 859 860 861 862 863 864
	for_each_gt(gt, i915, id) {
		intel_uc_driver_late_release(&gt->uc);
		intel_gt_fini_requests(gt);
		intel_gt_fini_reset(gt);
		intel_gt_fini_timelines(gt);
		intel_engines_free(gt);
	}
865
}
866

867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
/**
 * intel_gt_reg_needs_read_steering - determine whether a register read
 *     requires explicit steering
 * @gt: GT structure
 * @reg: the register to check steering requirements for
 * @type: type of multicast steering to check
 *
 * Determines whether @reg needs explicit steering of a specific type for
 * reads.
 *
 * Returns false if @reg does not belong to a register range of the given
 * steering type, or if the default (subslice-based) steering IDs are suitable
 * for @type steering too.
 */
static bool intel_gt_reg_needs_read_steering(struct intel_gt *gt,
					     i915_reg_t reg,
					     enum intel_steering_type type)
{
	const u32 offset = i915_mmio_reg_offset(reg);
	const struct intel_mmio_range *entry;

	if (likely(!intel_gt_needs_read_steering(gt, type)))
		return false;

	for (entry = gt->steering_table[type]; entry->end; entry++) {
		if (offset >= entry->start && offset <= entry->end)
			return true;
	}

	return false;
}

/**
 * intel_gt_get_valid_steering - determines valid IDs for a class of MCR steering
 * @gt: GT structure
 * @type: multicast register type
 * @sliceid: Slice ID returned
 * @subsliceid: Subslice ID returned
 *
 * Determines sliceid and subsliceid values that will steer reads
 * of a specific multicast register class to a valid value.
 */
static void intel_gt_get_valid_steering(struct intel_gt *gt,
					enum intel_steering_type type,
					u8 *sliceid, u8 *subsliceid)
{
	switch (type) {
914 915 916 917
	case L3BANK:
		*sliceid = 0;		/* unused */
		*subsliceid = __ffs(gt->info.l3bank_mask);
		break;
918
	case MSLICE:
M
Matt Roper 已提交
919
		GEM_WARN_ON(!HAS_MSLICE_STEERING(gt->i915));
920 921 922 923 924 925 926 927
		*sliceid = __ffs(gt->info.mslice_mask);
		*subsliceid = 0;	/* unused */
		break;
	case LNCF:
		/*
		 * An LNCF is always present if its mslice is present, so we
		 * can safely just steer to LNCF 0 in all cases.
		 */
M
Matt Roper 已提交
928
		GEM_WARN_ON(!HAS_MSLICE_STEERING(gt->i915));
929 930 931
		*sliceid = __ffs(gt->info.mslice_mask) << 1;
		*subsliceid = 0;	/* unused */
		break;
M
Matt Roper 已提交
932 933 934 935 936 937 938 939
	case INSTANCE0:
		/*
		 * There are a lot of MCR types for which instance (0, 0)
		 * will always provide a non-terminated value.
		 */
		*sliceid = 0;
		*subsliceid = 0;
		break;
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
	default:
		MISSING_CASE(type);
		*sliceid = 0;
		*subsliceid = 0;
	}
}

/**
 * intel_gt_read_register_fw - reads a GT register with support for multicast
 * @gt: GT structure
 * @reg: register to read
 *
 * This function will read a GT register.  If the register is a multicast
 * register, the read will be steered to a valid instance (i.e., one that
 * isn't fused off or powered down by power gating).
 *
 * Returns the value from a valid instance of @reg.
 */
u32 intel_gt_read_register_fw(struct intel_gt *gt, i915_reg_t reg)
{
	int type;
	u8 sliceid, subsliceid;

	for (type = 0; type < NUM_STEERING_TYPES; type++) {
		if (intel_gt_reg_needs_read_steering(gt, reg, type)) {
			intel_gt_get_valid_steering(gt, type, &sliceid,
						    &subsliceid);
			return intel_uncore_read_with_mcr_steering_fw(gt->uncore,
								      reg,
								      sliceid,
								      subsliceid);
		}
	}

	return intel_uncore_read_fw(gt->uncore, reg);
}

977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
/**
 * intel_gt_get_valid_steering_for_reg - get a valid steering for a register
 * @gt: GT structure
 * @reg: register for which the steering is required
 * @sliceid: return variable for slice steering
 * @subsliceid: return variable for subslice steering
 *
 * This function returns a slice/subslice pair that is guaranteed to work for
 * read steering of the given register. Note that a value will be returned even
 * if the register is not replicated and therefore does not actually require
 * steering.
 */
void intel_gt_get_valid_steering_for_reg(struct intel_gt *gt, i915_reg_t reg,
					 u8 *sliceid, u8 *subsliceid)
{
	int type;

	for (type = 0; type < NUM_STEERING_TYPES; type++) {
		if (intel_gt_reg_needs_read_steering(gt, reg, type)) {
			intel_gt_get_valid_steering(gt, type, sliceid,
						    subsliceid);
			return;
		}
	}

	*sliceid = gt->default_steering.groupid;
	*subsliceid = gt->default_steering.instanceid;
}

1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
u32 intel_gt_read_register(struct intel_gt *gt, i915_reg_t reg)
{
	int type;
	u8 sliceid, subsliceid;

	for (type = 0; type < NUM_STEERING_TYPES; type++) {
		if (intel_gt_reg_needs_read_steering(gt, reg, type)) {
			intel_gt_get_valid_steering(gt, type, &sliceid,
						    &subsliceid);
			return intel_uncore_read_with_mcr_steering(gt->uncore,
								   reg,
								   sliceid,
								   subsliceid);
		}
	}

	return intel_uncore_read(gt->uncore, reg);
}

1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
static void report_steering_type(struct drm_printer *p,
				 struct intel_gt *gt,
				 enum intel_steering_type type,
				 bool dump_table)
{
	const struct intel_mmio_range *entry;
	u8 slice, subslice;

	BUILD_BUG_ON(ARRAY_SIZE(intel_steering_types) != NUM_STEERING_TYPES);

	if (!gt->steering_table[type]) {
		drm_printf(p, "%s steering: uses default steering\n",
			   intel_steering_types[type]);
		return;
	}

	intel_gt_get_valid_steering(gt, type, &slice, &subslice);
	drm_printf(p, "%s steering: sliceid=0x%x, subsliceid=0x%x\n",
		   intel_steering_types[type], slice, subslice);

	if (!dump_table)
		return;

	for (entry = gt->steering_table[type]; entry->end; entry++)
		drm_printf(p, "\t0x%06x - 0x%06x\n", entry->start, entry->end);
}

void intel_gt_report_steering(struct drm_printer *p, struct intel_gt *gt,
			      bool dump_table)
{
	drm_printf(p, "Default steering: sliceid=0x%x, subsliceid=0x%x\n",
		   gt->default_steering.groupid,
		   gt->default_steering.instanceid);

M
Matt Roper 已提交
1059 1060 1061
	if (IS_PONTEVECCHIO(gt->i915)) {
		report_steering_type(p, gt, INSTANCE0, dump_table);
	} else if (HAS_MSLICE_STEERING(gt->i915)) {
1062 1063 1064 1065 1066
		report_steering_type(p, gt, MSLICE, dump_table);
		report_steering_type(p, gt, LNCF, dump_table);
	}
}

1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 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 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
static int intel_gt_tile_setup(struct intel_gt *gt, phys_addr_t phys_addr)
{
	int ret;

	if (!gt_is_root(gt)) {
		struct intel_uncore_mmio_debug *mmio_debug;
		struct intel_uncore *uncore;

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

		mmio_debug = kzalloc(sizeof(*mmio_debug), GFP_KERNEL);
		if (!mmio_debug) {
			kfree(uncore);
			return -ENOMEM;
		}

		gt->uncore = uncore;
		gt->uncore->debug = mmio_debug;

		__intel_gt_init_early(gt);
	}

	intel_uncore_init_early(gt->uncore, gt);

	ret = intel_uncore_setup_mmio(gt->uncore, phys_addr);
	if (ret)
		return ret;

	gt->phys_addr = phys_addr;

	return 0;
}

static void
intel_gt_tile_cleanup(struct intel_gt *gt)
{
	intel_uncore_cleanup_mmio(gt->uncore);

	if (!gt_is_root(gt)) {
		kfree(gt->uncore->debug);
		kfree(gt->uncore);
		kfree(gt);
	}
}

int intel_gt_probe_all(struct drm_i915_private *i915)
{
	struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
	struct intel_gt *gt = &i915->gt0;
	phys_addr_t phys_addr;
	unsigned int mmio_bar;
	int ret;

	mmio_bar = GRAPHICS_VER(i915) == 2 ? 1 : 0;
	phys_addr = pci_resource_start(pdev, mmio_bar);

	/*
	 * We always have at least one primary GT on any device
	 * and it has been already initialized early during probe
	 * in i915_driver_probe()
	 */
	ret = intel_gt_tile_setup(gt, phys_addr);
	if (ret)
		return ret;

	i915->gt[0] = gt;

	/* TODO: add more tiles */
	return 0;
}

int intel_gt_tiles_init(struct drm_i915_private *i915)
{
	struct intel_gt *gt;
	unsigned int id;
	int ret;

	for_each_gt(gt, i915, id) {
		ret = intel_gt_probe_lmem(gt);
		if (ret)
			return ret;
	}

	return 0;
}

void intel_gt_release_all(struct drm_i915_private *i915)
{
	struct intel_gt *gt;
	unsigned int id;

	for_each_gt(gt, i915, id) {
		intel_gt_tile_cleanup(gt);
		i915->gt[id] = NULL;
	}
}

1166 1167 1168 1169
void intel_gt_info_print(const struct intel_gt_info *info,
			 struct drm_printer *p)
{
	drm_printf(p, "available engines: %x\n", info->engine_mask);
1170 1171

	intel_sseu_dump(&info->sseu, p);
1172
}
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213

struct reg_and_bit {
	i915_reg_t reg;
	u32 bit;
};

static struct reg_and_bit
get_reg_and_bit(const struct intel_engine_cs *engine, const bool gen8,
		const i915_reg_t *regs, const unsigned int num)
{
	const unsigned int class = engine->class;
	struct reg_and_bit rb = { };

	if (drm_WARN_ON_ONCE(&engine->i915->drm,
			     class >= num || !regs[class].reg))
		return rb;

	rb.reg = regs[class];
	if (gen8 && class == VIDEO_DECODE_CLASS)
		rb.reg.reg += 4 * engine->instance; /* GEN8_M2TCR */
	else
		rb.bit = engine->instance;

	rb.bit = BIT(rb.bit);

	return rb;
}

void intel_gt_invalidate_tlbs(struct intel_gt *gt)
{
	static const i915_reg_t gen8_regs[] = {
		[RENDER_CLASS]			= GEN8_RTCR,
		[VIDEO_DECODE_CLASS]		= GEN8_M1TCR, /* , GEN8_M2TCR */
		[VIDEO_ENHANCEMENT_CLASS]	= GEN8_VTCR,
		[COPY_ENGINE_CLASS]		= GEN8_BTCR,
	};
	static const i915_reg_t gen12_regs[] = {
		[RENDER_CLASS]			= GEN12_GFX_TLB_INV_CR,
		[VIDEO_DECODE_CLASS]		= GEN12_VD_TLB_INV_CR,
		[VIDEO_ENHANCEMENT_CLASS]	= GEN12_VE_TLB_INV_CR,
		[COPY_ENGINE_CLASS]		= GEN12_BLT_TLB_INV_CR,
1214
		[COMPUTE_CLASS]			= GEN12_COMPCTX_TLB_INV_CR,
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
	};
	struct drm_i915_private *i915 = gt->i915;
	struct intel_uncore *uncore = gt->uncore;
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
	const i915_reg_t *regs;
	unsigned int num = 0;

	if (I915_SELFTEST_ONLY(gt->awake == -ENODEV))
		return;

	if (GRAPHICS_VER(i915) == 12) {
		regs = gen12_regs;
		num = ARRAY_SIZE(gen12_regs);
	} else if (GRAPHICS_VER(i915) >= 8 && GRAPHICS_VER(i915) <= 11) {
		regs = gen8_regs;
		num = ARRAY_SIZE(gen8_regs);
	} else if (GRAPHICS_VER(i915) < 8) {
		return;
	}

	if (drm_WARN_ONCE(&i915->drm, !num,
			  "Platform does not implement TLB invalidation!"))
		return;

	GEM_TRACE("\n");

	assert_rpm_wakelock_held(&i915->runtime_pm);

	mutex_lock(&gt->tlb_invalidate_lock);
	intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);

	for_each_engine(engine, gt, id) {
		/*
		 * HW architecture suggest typical invalidation time at 40us,
		 * with pessimistic cases up to 100us and a recommendation to
		 * cap at 1ms. We go a bit higher just in case.
		 */
		const unsigned int timeout_us = 100;
		const unsigned int timeout_ms = 4;
		struct reg_and_bit rb;

		rb = get_reg_and_bit(engine, regs == gen8_regs, regs, num);
		if (!i915_mmio_reg_offset(rb.reg))
			continue;

		intel_uncore_write_fw(uncore, rb.reg, rb.bit);
		if (__intel_wait_for_register_fw(uncore,
						 rb.reg, rb.bit, 0,
						 timeout_us, timeout_ms,
						 NULL))
			drm_err_ratelimited(&gt->i915->drm,
					    "%s TLB invalidation did not complete in %ums!\n",
					    engine->name, timeout_ms);
	}

	/*
	 * Use delayed put since a) we mostly expect a flurry of TLB
	 * invalidations so it is good to avoid paying the forcewake cost and
	 * b) it works around a bug in Icelake which cannot cope with too rapid
	 * transitions.
	 */
	intel_uncore_forcewake_put_delayed(uncore, FORCEWAKE_ALL);
	mutex_unlock(&gt->tlb_invalidate_lock);
}