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