xref: /linux/drivers/gpu/drm/i915/gt/intel_gt.c (revision b9d7eb6a31be296ca0af95641a23c4c758703c0a)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2019 Intel Corporation
4  */
5 
6 #include <drm/intel-gtt.h>
7 
8 #include "intel_gt_debugfs.h"
9 
10 #include "gem/i915_gem_lmem.h"
11 #include "i915_drv.h"
12 #include "intel_context.h"
13 #include "intel_engine_regs.h"
14 #include "intel_gt.h"
15 #include "intel_gt_buffer_pool.h"
16 #include "intel_gt_clock_utils.h"
17 #include "intel_gt_pm.h"
18 #include "intel_gt_regs.h"
19 #include "intel_gt_requests.h"
20 #include "intel_migrate.h"
21 #include "intel_mocs.h"
22 #include "intel_pm.h"
23 #include "intel_rc6.h"
24 #include "intel_renderstate.h"
25 #include "intel_rps.h"
26 #include "intel_uncore.h"
27 #include "shmem_utils.h"
28 #include "pxp/intel_pxp.h"
29 
30 void __intel_gt_init_early(struct intel_gt *gt, struct drm_i915_private *i915)
31 {
32 	spin_lock_init(&gt->irq_lock);
33 
34 	mutex_init(&gt->tlb_invalidate_lock);
35 
36 	INIT_LIST_HEAD(&gt->closed_vma);
37 	spin_lock_init(&gt->closed_lock);
38 
39 	init_llist_head(&gt->watchdog.list);
40 	INIT_WORK(&gt->watchdog.work, intel_gt_watchdog_work);
41 
42 	intel_gt_init_buffer_pool(gt);
43 	intel_gt_init_reset(gt);
44 	intel_gt_init_requests(gt);
45 	intel_gt_init_timelines(gt);
46 	intel_gt_pm_init_early(gt);
47 
48 	intel_uc_init_early(&gt->uc);
49 	intel_rps_init_early(&gt->rps);
50 }
51 
52 void intel_gt_init_early(struct intel_gt *gt, struct drm_i915_private *i915)
53 {
54 	gt->i915 = i915;
55 	gt->uncore = &i915->uncore;
56 }
57 
58 int intel_gt_probe_lmem(struct intel_gt *gt)
59 {
60 	struct drm_i915_private *i915 = gt->i915;
61 	struct intel_memory_region *mem;
62 	int id;
63 	int err;
64 
65 	mem = intel_gt_setup_lmem(gt);
66 	if (mem == ERR_PTR(-ENODEV))
67 		mem = intel_gt_setup_fake_lmem(gt);
68 	if (IS_ERR(mem)) {
69 		err = PTR_ERR(mem);
70 		if (err == -ENODEV)
71 			return 0;
72 
73 		drm_err(&i915->drm,
74 			"Failed to setup region(%d) type=%d\n",
75 			err, INTEL_MEMORY_LOCAL);
76 		return err;
77 	}
78 
79 	id = INTEL_REGION_LMEM;
80 
81 	mem->id = id;
82 
83 	intel_memory_region_set_name(mem, "local%u", mem->instance);
84 
85 	GEM_BUG_ON(!HAS_REGION(i915, id));
86 	GEM_BUG_ON(i915->mm.regions[id]);
87 	i915->mm.regions[id] = mem;
88 
89 	return 0;
90 }
91 
92 void intel_gt_init_hw_early(struct intel_gt *gt, struct i915_ggtt *ggtt)
93 {
94 	gt->ggtt = ggtt;
95 }
96 
97 static const struct intel_mmio_range icl_l3bank_steering_table[] = {
98 	{ 0x00B100, 0x00B3FF },
99 	{},
100 };
101 
102 static const struct intel_mmio_range xehpsdv_mslice_steering_table[] = {
103 	{ 0x004000, 0x004AFF },
104 	{ 0x00C800, 0x00CFFF },
105 	{ 0x00DD00, 0x00DDFF },
106 	{ 0x00E900, 0x00FFFF }, /* 0xEA00 - OxEFFF is unused */
107 	{},
108 };
109 
110 static const struct intel_mmio_range xehpsdv_lncf_steering_table[] = {
111 	{ 0x00B000, 0x00B0FF },
112 	{ 0x00D800, 0x00D8FF },
113 	{},
114 };
115 
116 static const struct intel_mmio_range dg2_lncf_steering_table[] = {
117 	{ 0x00B000, 0x00B0FF },
118 	{ 0x00D880, 0x00D8FF },
119 	{},
120 };
121 
122 static u16 slicemask(struct intel_gt *gt, int count)
123 {
124 	u64 dss_mask = intel_sseu_get_subslices(&gt->info.sseu, 0);
125 
126 	return intel_slicemask_from_dssmask(dss_mask, count);
127 }
128 
129 int intel_gt_init_mmio(struct intel_gt *gt)
130 {
131 	struct drm_i915_private *i915 = gt->i915;
132 
133 	intel_gt_init_clock_frequency(gt);
134 
135 	intel_uc_init_mmio(&gt->uc);
136 	intel_sseu_info_init(gt);
137 
138 	/*
139 	 * An mslice is unavailable only if both the meml3 for the slice is
140 	 * disabled *and* all of the DSS in the slice (quadrant) are disabled.
141 	 */
142 	if (HAS_MSLICES(i915))
143 		gt->info.mslice_mask =
144 			slicemask(gt, GEN_DSS_PER_MSLICE) |
145 			(intel_uncore_read(gt->uncore, GEN10_MIRROR_FUSE3) &
146 			 GEN12_MEML3_EN_MASK);
147 
148 	if (IS_DG2(i915)) {
149 		gt->steering_table[MSLICE] = xehpsdv_mslice_steering_table;
150 		gt->steering_table[LNCF] = dg2_lncf_steering_table;
151 	} else if (IS_XEHPSDV(i915)) {
152 		gt->steering_table[MSLICE] = xehpsdv_mslice_steering_table;
153 		gt->steering_table[LNCF] = xehpsdv_lncf_steering_table;
154 	} else if (GRAPHICS_VER(i915) >= 11 &&
155 		   GRAPHICS_VER_FULL(i915) < IP_VER(12, 50)) {
156 		gt->steering_table[L3BANK] = icl_l3bank_steering_table;
157 		gt->info.l3bank_mask =
158 			~intel_uncore_read(gt->uncore, GEN10_MIRROR_FUSE3) &
159 			GEN10_L3BANK_MASK;
160 	} else if (HAS_MSLICES(i915)) {
161 		MISSING_CASE(INTEL_INFO(i915)->platform);
162 	}
163 
164 	return intel_engines_init_mmio(gt);
165 }
166 
167 static void init_unused_ring(struct intel_gt *gt, u32 base)
168 {
169 	struct intel_uncore *uncore = gt->uncore;
170 
171 	intel_uncore_write(uncore, RING_CTL(base), 0);
172 	intel_uncore_write(uncore, RING_HEAD(base), 0);
173 	intel_uncore_write(uncore, RING_TAIL(base), 0);
174 	intel_uncore_write(uncore, RING_START(base), 0);
175 }
176 
177 static void init_unused_rings(struct intel_gt *gt)
178 {
179 	struct drm_i915_private *i915 = gt->i915;
180 
181 	if (IS_I830(i915)) {
182 		init_unused_ring(gt, PRB1_BASE);
183 		init_unused_ring(gt, SRB0_BASE);
184 		init_unused_ring(gt, SRB1_BASE);
185 		init_unused_ring(gt, SRB2_BASE);
186 		init_unused_ring(gt, SRB3_BASE);
187 	} else if (GRAPHICS_VER(i915) == 2) {
188 		init_unused_ring(gt, SRB0_BASE);
189 		init_unused_ring(gt, SRB1_BASE);
190 	} else if (GRAPHICS_VER(i915) == 3) {
191 		init_unused_ring(gt, PRB1_BASE);
192 		init_unused_ring(gt, PRB2_BASE);
193 	}
194 }
195 
196 int intel_gt_init_hw(struct intel_gt *gt)
197 {
198 	struct drm_i915_private *i915 = gt->i915;
199 	struct intel_uncore *uncore = gt->uncore;
200 	int ret;
201 
202 	gt->last_init_time = ktime_get();
203 
204 	/* Double layer security blanket, see i915_gem_init() */
205 	intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
206 
207 	if (HAS_EDRAM(i915) && GRAPHICS_VER(i915) < 9)
208 		intel_uncore_rmw(uncore, HSW_IDICR, 0, IDIHASHMSK(0xf));
209 
210 	if (IS_HASWELL(i915))
211 		intel_uncore_write(uncore,
212 				   HSW_MI_PREDICATE_RESULT_2,
213 				   IS_HSW_GT3(i915) ?
214 				   LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
215 
216 	/* Apply the GT workarounds... */
217 	intel_gt_apply_workarounds(gt);
218 	/* ...and determine whether they are sticking. */
219 	intel_gt_verify_workarounds(gt, "init");
220 
221 	intel_gt_init_swizzling(gt);
222 
223 	/*
224 	 * At least 830 can leave some of the unused rings
225 	 * "active" (ie. head != tail) after resume which
226 	 * will prevent c3 entry. Makes sure all unused rings
227 	 * are totally idle.
228 	 */
229 	init_unused_rings(gt);
230 
231 	ret = i915_ppgtt_init_hw(gt);
232 	if (ret) {
233 		DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
234 		goto out;
235 	}
236 
237 	/* We can't enable contexts until all firmware is loaded */
238 	ret = intel_uc_init_hw(&gt->uc);
239 	if (ret) {
240 		i915_probe_error(i915, "Enabling uc failed (%d)\n", ret);
241 		goto out;
242 	}
243 
244 	intel_mocs_init(gt);
245 
246 out:
247 	intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
248 	return ret;
249 }
250 
251 static void rmw_set(struct intel_uncore *uncore, i915_reg_t reg, u32 set)
252 {
253 	intel_uncore_rmw(uncore, reg, 0, set);
254 }
255 
256 static void rmw_clear(struct intel_uncore *uncore, i915_reg_t reg, u32 clr)
257 {
258 	intel_uncore_rmw(uncore, reg, clr, 0);
259 }
260 
261 static void clear_register(struct intel_uncore *uncore, i915_reg_t reg)
262 {
263 	intel_uncore_rmw(uncore, reg, 0, 0);
264 }
265 
266 static void gen6_clear_engine_error_register(struct intel_engine_cs *engine)
267 {
268 	GEN6_RING_FAULT_REG_RMW(engine, RING_FAULT_VALID, 0);
269 	GEN6_RING_FAULT_REG_POSTING_READ(engine);
270 }
271 
272 void
273 intel_gt_clear_error_registers(struct intel_gt *gt,
274 			       intel_engine_mask_t engine_mask)
275 {
276 	struct drm_i915_private *i915 = gt->i915;
277 	struct intel_uncore *uncore = gt->uncore;
278 	u32 eir;
279 
280 	if (GRAPHICS_VER(i915) != 2)
281 		clear_register(uncore, PGTBL_ER);
282 
283 	if (GRAPHICS_VER(i915) < 4)
284 		clear_register(uncore, IPEIR(RENDER_RING_BASE));
285 	else
286 		clear_register(uncore, IPEIR_I965);
287 
288 	clear_register(uncore, EIR);
289 	eir = intel_uncore_read(uncore, EIR);
290 	if (eir) {
291 		/*
292 		 * some errors might have become stuck,
293 		 * mask them.
294 		 */
295 		DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masking\n", eir);
296 		rmw_set(uncore, EMR, eir);
297 		intel_uncore_write(uncore, GEN2_IIR,
298 				   I915_MASTER_ERROR_INTERRUPT);
299 	}
300 
301 	if (GRAPHICS_VER(i915) >= 12) {
302 		rmw_clear(uncore, GEN12_RING_FAULT_REG, RING_FAULT_VALID);
303 		intel_uncore_posting_read(uncore, GEN12_RING_FAULT_REG);
304 	} else if (GRAPHICS_VER(i915) >= 8) {
305 		rmw_clear(uncore, GEN8_RING_FAULT_REG, RING_FAULT_VALID);
306 		intel_uncore_posting_read(uncore, GEN8_RING_FAULT_REG);
307 	} else if (GRAPHICS_VER(i915) >= 6) {
308 		struct intel_engine_cs *engine;
309 		enum intel_engine_id id;
310 
311 		for_each_engine_masked(engine, gt, engine_mask, id)
312 			gen6_clear_engine_error_register(engine);
313 	}
314 }
315 
316 static void gen6_check_faults(struct intel_gt *gt)
317 {
318 	struct intel_engine_cs *engine;
319 	enum intel_engine_id id;
320 	u32 fault;
321 
322 	for_each_engine(engine, gt, id) {
323 		fault = GEN6_RING_FAULT_REG_READ(engine);
324 		if (fault & RING_FAULT_VALID) {
325 			drm_dbg(&engine->i915->drm, "Unexpected fault\n"
326 				"\tAddr: 0x%08lx\n"
327 				"\tAddress space: %s\n"
328 				"\tSource ID: %d\n"
329 				"\tType: %d\n",
330 				fault & PAGE_MASK,
331 				fault & RING_FAULT_GTTSEL_MASK ?
332 				"GGTT" : "PPGTT",
333 				RING_FAULT_SRCID(fault),
334 				RING_FAULT_FAULT_TYPE(fault));
335 		}
336 	}
337 }
338 
339 static void gen8_check_faults(struct intel_gt *gt)
340 {
341 	struct intel_uncore *uncore = gt->uncore;
342 	i915_reg_t fault_reg, fault_data0_reg, fault_data1_reg;
343 	u32 fault;
344 
345 	if (GRAPHICS_VER(gt->i915) >= 12) {
346 		fault_reg = GEN12_RING_FAULT_REG;
347 		fault_data0_reg = GEN12_FAULT_TLB_DATA0;
348 		fault_data1_reg = GEN12_FAULT_TLB_DATA1;
349 	} else {
350 		fault_reg = GEN8_RING_FAULT_REG;
351 		fault_data0_reg = GEN8_FAULT_TLB_DATA0;
352 		fault_data1_reg = GEN8_FAULT_TLB_DATA1;
353 	}
354 
355 	fault = intel_uncore_read(uncore, fault_reg);
356 	if (fault & RING_FAULT_VALID) {
357 		u32 fault_data0, fault_data1;
358 		u64 fault_addr;
359 
360 		fault_data0 = intel_uncore_read(uncore, fault_data0_reg);
361 		fault_data1 = intel_uncore_read(uncore, fault_data1_reg);
362 
363 		fault_addr = ((u64)(fault_data1 & FAULT_VA_HIGH_BITS) << 44) |
364 			     ((u64)fault_data0 << 12);
365 
366 		drm_dbg(&uncore->i915->drm, "Unexpected fault\n"
367 			"\tAddr: 0x%08x_%08x\n"
368 			"\tAddress space: %s\n"
369 			"\tEngine ID: %d\n"
370 			"\tSource ID: %d\n"
371 			"\tType: %d\n",
372 			upper_32_bits(fault_addr), lower_32_bits(fault_addr),
373 			fault_data1 & FAULT_GTT_SEL ? "GGTT" : "PPGTT",
374 			GEN8_RING_FAULT_ENGINE_ID(fault),
375 			RING_FAULT_SRCID(fault),
376 			RING_FAULT_FAULT_TYPE(fault));
377 	}
378 }
379 
380 void intel_gt_check_and_clear_faults(struct intel_gt *gt)
381 {
382 	struct drm_i915_private *i915 = gt->i915;
383 
384 	/* From GEN8 onwards we only have one 'All Engine Fault Register' */
385 	if (GRAPHICS_VER(i915) >= 8)
386 		gen8_check_faults(gt);
387 	else if (GRAPHICS_VER(i915) >= 6)
388 		gen6_check_faults(gt);
389 	else
390 		return;
391 
392 	intel_gt_clear_error_registers(gt, ALL_ENGINES);
393 }
394 
395 void intel_gt_flush_ggtt_writes(struct intel_gt *gt)
396 {
397 	struct intel_uncore *uncore = gt->uncore;
398 	intel_wakeref_t wakeref;
399 
400 	/*
401 	 * No actual flushing is required for the GTT write domain for reads
402 	 * from the GTT domain. Writes to it "immediately" go to main memory
403 	 * as far as we know, so there's no chipset flush. It also doesn't
404 	 * land in the GPU render cache.
405 	 *
406 	 * However, we do have to enforce the order so that all writes through
407 	 * the GTT land before any writes to the device, such as updates to
408 	 * the GATT itself.
409 	 *
410 	 * We also have to wait a bit for the writes to land from the GTT.
411 	 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
412 	 * timing. This issue has only been observed when switching quickly
413 	 * between GTT writes and CPU reads from inside the kernel on recent hw,
414 	 * and it appears to only affect discrete GTT blocks (i.e. on LLC
415 	 * system agents we cannot reproduce this behaviour, until Cannonlake
416 	 * that was!).
417 	 */
418 
419 	wmb();
420 
421 	if (INTEL_INFO(gt->i915)->has_coherent_ggtt)
422 		return;
423 
424 	intel_gt_chipset_flush(gt);
425 
426 	with_intel_runtime_pm_if_in_use(uncore->rpm, wakeref) {
427 		unsigned long flags;
428 
429 		spin_lock_irqsave(&uncore->lock, flags);
430 		intel_uncore_posting_read_fw(uncore,
431 					     RING_HEAD(RENDER_RING_BASE));
432 		spin_unlock_irqrestore(&uncore->lock, flags);
433 	}
434 }
435 
436 void intel_gt_chipset_flush(struct intel_gt *gt)
437 {
438 	wmb();
439 	if (GRAPHICS_VER(gt->i915) < 6)
440 		intel_gtt_chipset_flush();
441 }
442 
443 void intel_gt_driver_register(struct intel_gt *gt)
444 {
445 	intel_rps_driver_register(&gt->rps);
446 
447 	intel_gt_debugfs_register(gt);
448 }
449 
450 static int intel_gt_init_scratch(struct intel_gt *gt, unsigned int size)
451 {
452 	struct drm_i915_private *i915 = gt->i915;
453 	struct drm_i915_gem_object *obj;
454 	struct i915_vma *vma;
455 	int ret;
456 
457 	obj = i915_gem_object_create_lmem(i915, size, I915_BO_ALLOC_VOLATILE);
458 	if (IS_ERR(obj))
459 		obj = i915_gem_object_create_stolen(i915, size);
460 	if (IS_ERR(obj))
461 		obj = i915_gem_object_create_internal(i915, size);
462 	if (IS_ERR(obj)) {
463 		drm_err(&i915->drm, "Failed to allocate scratch page\n");
464 		return PTR_ERR(obj);
465 	}
466 
467 	vma = i915_vma_instance(obj, &gt->ggtt->vm, NULL);
468 	if (IS_ERR(vma)) {
469 		ret = PTR_ERR(vma);
470 		goto err_unref;
471 	}
472 
473 	ret = i915_ggtt_pin(vma, NULL, 0, PIN_HIGH);
474 	if (ret)
475 		goto err_unref;
476 
477 	gt->scratch = i915_vma_make_unshrinkable(vma);
478 
479 	return 0;
480 
481 err_unref:
482 	i915_gem_object_put(obj);
483 	return ret;
484 }
485 
486 static void intel_gt_fini_scratch(struct intel_gt *gt)
487 {
488 	i915_vma_unpin_and_release(&gt->scratch, 0);
489 }
490 
491 static struct i915_address_space *kernel_vm(struct intel_gt *gt)
492 {
493 	if (INTEL_PPGTT(gt->i915) > INTEL_PPGTT_ALIASING)
494 		return &i915_ppgtt_create(gt, I915_BO_ALLOC_PM_EARLY)->vm;
495 	else
496 		return i915_vm_get(&gt->ggtt->vm);
497 }
498 
499 static int __engines_record_defaults(struct intel_gt *gt)
500 {
501 	struct i915_request *requests[I915_NUM_ENGINES] = {};
502 	struct intel_engine_cs *engine;
503 	enum intel_engine_id id;
504 	int err = 0;
505 
506 	/*
507 	 * As we reset the gpu during very early sanitisation, the current
508 	 * register state on the GPU should reflect its defaults values.
509 	 * We load a context onto the hw (with restore-inhibit), then switch
510 	 * over to a second context to save that default register state. We
511 	 * can then prime every new context with that state so they all start
512 	 * from the same default HW values.
513 	 */
514 
515 	for_each_engine(engine, gt, id) {
516 		struct intel_renderstate so;
517 		struct intel_context *ce;
518 		struct i915_request *rq;
519 
520 		/* We must be able to switch to something! */
521 		GEM_BUG_ON(!engine->kernel_context);
522 
523 		ce = intel_context_create(engine);
524 		if (IS_ERR(ce)) {
525 			err = PTR_ERR(ce);
526 			goto out;
527 		}
528 
529 		err = intel_renderstate_init(&so, ce);
530 		if (err)
531 			goto err;
532 
533 		rq = i915_request_create(ce);
534 		if (IS_ERR(rq)) {
535 			err = PTR_ERR(rq);
536 			goto err_fini;
537 		}
538 
539 		err = intel_engine_emit_ctx_wa(rq);
540 		if (err)
541 			goto err_rq;
542 
543 		err = intel_renderstate_emit(&so, rq);
544 		if (err)
545 			goto err_rq;
546 
547 err_rq:
548 		requests[id] = i915_request_get(rq);
549 		i915_request_add(rq);
550 err_fini:
551 		intel_renderstate_fini(&so, ce);
552 err:
553 		if (err) {
554 			intel_context_put(ce);
555 			goto out;
556 		}
557 	}
558 
559 	/* Flush the default context image to memory, and enable powersaving. */
560 	if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
561 		err = -EIO;
562 		goto out;
563 	}
564 
565 	for (id = 0; id < ARRAY_SIZE(requests); id++) {
566 		struct i915_request *rq;
567 		struct file *state;
568 
569 		rq = requests[id];
570 		if (!rq)
571 			continue;
572 
573 		if (rq->fence.error) {
574 			err = -EIO;
575 			goto out;
576 		}
577 
578 		GEM_BUG_ON(!test_bit(CONTEXT_ALLOC_BIT, &rq->context->flags));
579 		if (!rq->context->state)
580 			continue;
581 
582 		/* Keep a copy of the state's backing pages; free the obj */
583 		state = shmem_create_from_object(rq->context->state->obj);
584 		if (IS_ERR(state)) {
585 			err = PTR_ERR(state);
586 			goto out;
587 		}
588 		rq->engine->default_state = state;
589 	}
590 
591 out:
592 	/*
593 	 * If we have to abandon now, we expect the engines to be idle
594 	 * and ready to be torn-down. The quickest way we can accomplish
595 	 * this is by declaring ourselves wedged.
596 	 */
597 	if (err)
598 		intel_gt_set_wedged(gt);
599 
600 	for (id = 0; id < ARRAY_SIZE(requests); id++) {
601 		struct intel_context *ce;
602 		struct i915_request *rq;
603 
604 		rq = requests[id];
605 		if (!rq)
606 			continue;
607 
608 		ce = rq->context;
609 		i915_request_put(rq);
610 		intel_context_put(ce);
611 	}
612 	return err;
613 }
614 
615 static int __engines_verify_workarounds(struct intel_gt *gt)
616 {
617 	struct intel_engine_cs *engine;
618 	enum intel_engine_id id;
619 	int err = 0;
620 
621 	if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
622 		return 0;
623 
624 	for_each_engine(engine, gt, id) {
625 		if (intel_engine_verify_workarounds(engine, "load"))
626 			err = -EIO;
627 	}
628 
629 	/* Flush and restore the kernel context for safety */
630 	if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME)
631 		err = -EIO;
632 
633 	return err;
634 }
635 
636 static void __intel_gt_disable(struct intel_gt *gt)
637 {
638 	intel_gt_set_wedged_on_fini(gt);
639 
640 	intel_gt_suspend_prepare(gt);
641 	intel_gt_suspend_late(gt);
642 
643 	GEM_BUG_ON(intel_gt_pm_is_awake(gt));
644 }
645 
646 int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout)
647 {
648 	long remaining_timeout;
649 
650 	/* If the device is asleep, we have no requests outstanding */
651 	if (!intel_gt_pm_is_awake(gt))
652 		return 0;
653 
654 	while ((timeout = intel_gt_retire_requests_timeout(gt, timeout,
655 							   &remaining_timeout)) > 0) {
656 		cond_resched();
657 		if (signal_pending(current))
658 			return -EINTR;
659 	}
660 
661 	return timeout ? timeout : intel_uc_wait_for_idle(&gt->uc,
662 							  remaining_timeout);
663 }
664 
665 int intel_gt_init(struct intel_gt *gt)
666 {
667 	int err;
668 
669 	err = i915_inject_probe_error(gt->i915, -ENODEV);
670 	if (err)
671 		return err;
672 
673 	intel_gt_init_workarounds(gt);
674 
675 	/*
676 	 * This is just a security blanket to placate dragons.
677 	 * On some systems, we very sporadically observe that the first TLBs
678 	 * used by the CS may be stale, despite us poking the TLB reset. If
679 	 * we hold the forcewake during initialisation these problems
680 	 * just magically go away.
681 	 */
682 	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
683 
684 	err = intel_gt_init_scratch(gt,
685 				    GRAPHICS_VER(gt->i915) == 2 ? SZ_256K : SZ_4K);
686 	if (err)
687 		goto out_fw;
688 
689 	intel_gt_pm_init(gt);
690 
691 	gt->vm = kernel_vm(gt);
692 	if (!gt->vm) {
693 		err = -ENOMEM;
694 		goto err_pm;
695 	}
696 
697 	intel_set_mocs_index(gt);
698 
699 	err = intel_engines_init(gt);
700 	if (err)
701 		goto err_engines;
702 
703 	err = intel_uc_init(&gt->uc);
704 	if (err)
705 		goto err_engines;
706 
707 	err = intel_gt_resume(gt);
708 	if (err)
709 		goto err_uc_init;
710 
711 	err = __engines_record_defaults(gt);
712 	if (err)
713 		goto err_gt;
714 
715 	err = __engines_verify_workarounds(gt);
716 	if (err)
717 		goto err_gt;
718 
719 	intel_uc_init_late(&gt->uc);
720 
721 	err = i915_inject_probe_error(gt->i915, -EIO);
722 	if (err)
723 		goto err_gt;
724 
725 	intel_migrate_init(&gt->migrate, gt);
726 
727 	intel_pxp_init(&gt->pxp);
728 
729 	goto out_fw;
730 err_gt:
731 	__intel_gt_disable(gt);
732 	intel_uc_fini_hw(&gt->uc);
733 err_uc_init:
734 	intel_uc_fini(&gt->uc);
735 err_engines:
736 	intel_engines_release(gt);
737 	i915_vm_put(fetch_and_zero(&gt->vm));
738 err_pm:
739 	intel_gt_pm_fini(gt);
740 	intel_gt_fini_scratch(gt);
741 out_fw:
742 	if (err)
743 		intel_gt_set_wedged_on_init(gt);
744 	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
745 	return err;
746 }
747 
748 void intel_gt_driver_remove(struct intel_gt *gt)
749 {
750 	__intel_gt_disable(gt);
751 
752 	intel_migrate_fini(&gt->migrate);
753 	intel_uc_driver_remove(&gt->uc);
754 
755 	intel_engines_release(gt);
756 
757 	intel_gt_flush_buffer_pool(gt);
758 }
759 
760 void intel_gt_driver_unregister(struct intel_gt *gt)
761 {
762 	intel_wakeref_t wakeref;
763 
764 	intel_rps_driver_unregister(&gt->rps);
765 
766 	intel_pxp_fini(&gt->pxp);
767 
768 	/*
769 	 * Upon unregistering the device to prevent any new users, cancel
770 	 * all in-flight requests so that we can quickly unbind the active
771 	 * resources.
772 	 */
773 	intel_gt_set_wedged_on_fini(gt);
774 
775 	/* Scrub all HW state upon release */
776 	with_intel_runtime_pm(gt->uncore->rpm, wakeref)
777 		__intel_gt_reset(gt, ALL_ENGINES);
778 }
779 
780 void intel_gt_driver_release(struct intel_gt *gt)
781 {
782 	struct i915_address_space *vm;
783 
784 	vm = fetch_and_zero(&gt->vm);
785 	if (vm) /* FIXME being called twice on error paths :( */
786 		i915_vm_put(vm);
787 
788 	intel_wa_list_free(&gt->wa_list);
789 	intel_gt_pm_fini(gt);
790 	intel_gt_fini_scratch(gt);
791 	intel_gt_fini_buffer_pool(gt);
792 }
793 
794 void intel_gt_driver_late_release(struct intel_gt *gt)
795 {
796 	/* We need to wait for inflight RCU frees to release their grip */
797 	rcu_barrier();
798 
799 	intel_uc_driver_late_release(&gt->uc);
800 	intel_gt_fini_requests(gt);
801 	intel_gt_fini_reset(gt);
802 	intel_gt_fini_timelines(gt);
803 	intel_engines_free(gt);
804 }
805 
806 /**
807  * intel_gt_reg_needs_read_steering - determine whether a register read
808  *     requires explicit steering
809  * @gt: GT structure
810  * @reg: the register to check steering requirements for
811  * @type: type of multicast steering to check
812  *
813  * Determines whether @reg needs explicit steering of a specific type for
814  * reads.
815  *
816  * Returns false if @reg does not belong to a register range of the given
817  * steering type, or if the default (subslice-based) steering IDs are suitable
818  * for @type steering too.
819  */
820 static bool intel_gt_reg_needs_read_steering(struct intel_gt *gt,
821 					     i915_reg_t reg,
822 					     enum intel_steering_type type)
823 {
824 	const u32 offset = i915_mmio_reg_offset(reg);
825 	const struct intel_mmio_range *entry;
826 
827 	if (likely(!intel_gt_needs_read_steering(gt, type)))
828 		return false;
829 
830 	for (entry = gt->steering_table[type]; entry->end; entry++) {
831 		if (offset >= entry->start && offset <= entry->end)
832 			return true;
833 	}
834 
835 	return false;
836 }
837 
838 /**
839  * intel_gt_get_valid_steering - determines valid IDs for a class of MCR steering
840  * @gt: GT structure
841  * @type: multicast register type
842  * @sliceid: Slice ID returned
843  * @subsliceid: Subslice ID returned
844  *
845  * Determines sliceid and subsliceid values that will steer reads
846  * of a specific multicast register class to a valid value.
847  */
848 static void intel_gt_get_valid_steering(struct intel_gt *gt,
849 					enum intel_steering_type type,
850 					u8 *sliceid, u8 *subsliceid)
851 {
852 	switch (type) {
853 	case L3BANK:
854 		GEM_DEBUG_WARN_ON(!gt->info.l3bank_mask); /* should be impossible! */
855 
856 		*sliceid = 0;		/* unused */
857 		*subsliceid = __ffs(gt->info.l3bank_mask);
858 		break;
859 	case MSLICE:
860 		GEM_DEBUG_WARN_ON(!gt->info.mslice_mask); /* should be impossible! */
861 
862 		*sliceid = __ffs(gt->info.mslice_mask);
863 		*subsliceid = 0;	/* unused */
864 		break;
865 	case LNCF:
866 		GEM_DEBUG_WARN_ON(!gt->info.mslice_mask); /* should be impossible! */
867 
868 		/*
869 		 * An LNCF is always present if its mslice is present, so we
870 		 * can safely just steer to LNCF 0 in all cases.
871 		 */
872 		*sliceid = __ffs(gt->info.mslice_mask) << 1;
873 		*subsliceid = 0;	/* unused */
874 		break;
875 	default:
876 		MISSING_CASE(type);
877 		*sliceid = 0;
878 		*subsliceid = 0;
879 	}
880 }
881 
882 /**
883  * intel_gt_read_register_fw - reads a GT register with support for multicast
884  * @gt: GT structure
885  * @reg: register to read
886  *
887  * This function will read a GT register.  If the register is a multicast
888  * register, the read will be steered to a valid instance (i.e., one that
889  * isn't fused off or powered down by power gating).
890  *
891  * Returns the value from a valid instance of @reg.
892  */
893 u32 intel_gt_read_register_fw(struct intel_gt *gt, i915_reg_t reg)
894 {
895 	int type;
896 	u8 sliceid, subsliceid;
897 
898 	for (type = 0; type < NUM_STEERING_TYPES; type++) {
899 		if (intel_gt_reg_needs_read_steering(gt, reg, type)) {
900 			intel_gt_get_valid_steering(gt, type, &sliceid,
901 						    &subsliceid);
902 			return intel_uncore_read_with_mcr_steering_fw(gt->uncore,
903 								      reg,
904 								      sliceid,
905 								      subsliceid);
906 		}
907 	}
908 
909 	return intel_uncore_read_fw(gt->uncore, reg);
910 }
911 
912 void intel_gt_info_print(const struct intel_gt_info *info,
913 			 struct drm_printer *p)
914 {
915 	drm_printf(p, "available engines: %x\n", info->engine_mask);
916 
917 	intel_sseu_dump(&info->sseu, p);
918 }
919 
920 struct reg_and_bit {
921 	i915_reg_t reg;
922 	u32 bit;
923 };
924 
925 static struct reg_and_bit
926 get_reg_and_bit(const struct intel_engine_cs *engine, const bool gen8,
927 		const i915_reg_t *regs, const unsigned int num)
928 {
929 	const unsigned int class = engine->class;
930 	struct reg_and_bit rb = { };
931 
932 	if (drm_WARN_ON_ONCE(&engine->i915->drm,
933 			     class >= num || !regs[class].reg))
934 		return rb;
935 
936 	rb.reg = regs[class];
937 	if (gen8 && class == VIDEO_DECODE_CLASS)
938 		rb.reg.reg += 4 * engine->instance; /* GEN8_M2TCR */
939 	else
940 		rb.bit = engine->instance;
941 
942 	rb.bit = BIT(rb.bit);
943 
944 	return rb;
945 }
946 
947 void intel_gt_invalidate_tlbs(struct intel_gt *gt)
948 {
949 	static const i915_reg_t gen8_regs[] = {
950 		[RENDER_CLASS]			= GEN8_RTCR,
951 		[VIDEO_DECODE_CLASS]		= GEN8_M1TCR, /* , GEN8_M2TCR */
952 		[VIDEO_ENHANCEMENT_CLASS]	= GEN8_VTCR,
953 		[COPY_ENGINE_CLASS]		= GEN8_BTCR,
954 	};
955 	static const i915_reg_t gen12_regs[] = {
956 		[RENDER_CLASS]			= GEN12_GFX_TLB_INV_CR,
957 		[VIDEO_DECODE_CLASS]		= GEN12_VD_TLB_INV_CR,
958 		[VIDEO_ENHANCEMENT_CLASS]	= GEN12_VE_TLB_INV_CR,
959 		[COPY_ENGINE_CLASS]		= GEN12_BLT_TLB_INV_CR,
960 	};
961 	struct drm_i915_private *i915 = gt->i915;
962 	struct intel_uncore *uncore = gt->uncore;
963 	struct intel_engine_cs *engine;
964 	enum intel_engine_id id;
965 	const i915_reg_t *regs;
966 	unsigned int num = 0;
967 
968 	if (I915_SELFTEST_ONLY(gt->awake == -ENODEV))
969 		return;
970 
971 	if (GRAPHICS_VER(i915) == 12) {
972 		regs = gen12_regs;
973 		num = ARRAY_SIZE(gen12_regs);
974 	} else if (GRAPHICS_VER(i915) >= 8 && GRAPHICS_VER(i915) <= 11) {
975 		regs = gen8_regs;
976 		num = ARRAY_SIZE(gen8_regs);
977 	} else if (GRAPHICS_VER(i915) < 8) {
978 		return;
979 	}
980 
981 	if (drm_WARN_ONCE(&i915->drm, !num,
982 			  "Platform does not implement TLB invalidation!"))
983 		return;
984 
985 	GEM_TRACE("\n");
986 
987 	assert_rpm_wakelock_held(&i915->runtime_pm);
988 
989 	mutex_lock(&gt->tlb_invalidate_lock);
990 	intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
991 
992 	for_each_engine(engine, gt, id) {
993 		/*
994 		 * HW architecture suggest typical invalidation time at 40us,
995 		 * with pessimistic cases up to 100us and a recommendation to
996 		 * cap at 1ms. We go a bit higher just in case.
997 		 */
998 		const unsigned int timeout_us = 100;
999 		const unsigned int timeout_ms = 4;
1000 		struct reg_and_bit rb;
1001 
1002 		rb = get_reg_and_bit(engine, regs == gen8_regs, regs, num);
1003 		if (!i915_mmio_reg_offset(rb.reg))
1004 			continue;
1005 
1006 		intel_uncore_write_fw(uncore, rb.reg, rb.bit);
1007 		if (__intel_wait_for_register_fw(uncore,
1008 						 rb.reg, rb.bit, 0,
1009 						 timeout_us, timeout_ms,
1010 						 NULL))
1011 			drm_err_ratelimited(&gt->i915->drm,
1012 					    "%s TLB invalidation did not complete in %ums!\n",
1013 					    engine->name, timeout_ms);
1014 	}
1015 
1016 	/*
1017 	 * Use delayed put since a) we mostly expect a flurry of TLB
1018 	 * invalidations so it is good to avoid paying the forcewake cost and
1019 	 * b) it works around a bug in Icelake which cannot cope with too rapid
1020 	 * transitions.
1021 	 */
1022 	intel_uncore_forcewake_put_delayed(uncore, FORCEWAKE_ALL);
1023 	mutex_unlock(&gt->tlb_invalidate_lock);
1024 }
1025