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