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