xref: /openbsd/sys/dev/pci/drm/i915/gt/intel_gt.c (revision 09e4f8a5)
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 
12 #include "i915_drv.h"
13 #include "i915_perf_oa_regs.h"
14 #include "i915_reg.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_print.h"
26 #include "intel_gt_regs.h"
27 #include "intel_gt_requests.h"
28 #include "intel_migrate.h"
29 #include "intel_mocs.h"
30 #include "intel_pci_config.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_tlb.h"
37 #include "intel_uncore.h"
38 #include "shmem_utils.h"
39 
intel_gt_common_init_early(struct intel_gt * gt)40 void intel_gt_common_init_early(struct intel_gt *gt)
41 {
42 	mtx_init(gt->irq_lock, IPL_TTY);
43 
44 	INIT_LIST_HEAD(&gt->closed_vma);
45 	mtx_init(&gt->closed_lock, IPL_TTY);
46 
47 	init_llist_head(&gt->watchdog.list);
48 	INIT_WORK(&gt->watchdog.work, intel_gt_watchdog_work);
49 
50 	intel_gt_init_buffer_pool(gt);
51 	intel_gt_init_reset(gt);
52 	intel_gt_init_requests(gt);
53 	intel_gt_init_timelines(gt);
54 	intel_gt_init_tlb(gt);
55 	intel_gt_pm_init_early(gt);
56 
57 	intel_wopcm_init_early(&gt->wopcm);
58 	intel_uc_init_early(&gt->uc);
59 	intel_rps_init_early(&gt->rps);
60 }
61 
62 /* Preliminary initialization of Tile 0 */
intel_root_gt_init_early(struct drm_i915_private * i915)63 int intel_root_gt_init_early(struct drm_i915_private *i915)
64 {
65 	struct intel_gt *gt = to_gt(i915);
66 
67 	gt->i915 = i915;
68 	gt->uncore = &i915->uncore;
69 	gt->irq_lock = drmm_kzalloc(&i915->drm, sizeof(*gt->irq_lock), GFP_KERNEL);
70 	if (!gt->irq_lock)
71 		return -ENOMEM;
72 
73 	intel_gt_common_init_early(gt);
74 
75 	return 0;
76 }
77 
intel_gt_probe_lmem(struct intel_gt * gt)78 static int intel_gt_probe_lmem(struct intel_gt *gt)
79 {
80 	struct drm_i915_private *i915 = gt->i915;
81 	unsigned int instance = gt->info.id;
82 	int id = INTEL_REGION_LMEM_0 + instance;
83 	struct intel_memory_region *mem;
84 	int err;
85 
86 	mem = intel_gt_setup_lmem(gt);
87 	if (IS_ERR(mem)) {
88 		err = PTR_ERR(mem);
89 		if (err == -ENODEV)
90 			return 0;
91 
92 		gt_err(gt, "Failed to setup region(%d) type=%d\n",
93 		       err, INTEL_MEMORY_LOCAL);
94 		return err;
95 	}
96 
97 	mem->id = id;
98 	mem->instance = instance;
99 
100 	intel_memory_region_set_name(mem, "local%u", mem->instance);
101 
102 	GEM_BUG_ON(!HAS_REGION(i915, id));
103 	GEM_BUG_ON(i915->mm.regions[id]);
104 	i915->mm.regions[id] = mem;
105 
106 	return 0;
107 }
108 
intel_gt_assign_ggtt(struct intel_gt * gt)109 int intel_gt_assign_ggtt(struct intel_gt *gt)
110 {
111 	/* Media GT shares primary GT's GGTT */
112 	if (gt->type == GT_MEDIA) {
113 		gt->ggtt = to_gt(gt->i915)->ggtt;
114 	} else {
115 		gt->ggtt = i915_ggtt_create(gt->i915);
116 		if (IS_ERR(gt->ggtt))
117 			return PTR_ERR(gt->ggtt);
118 	}
119 
120 	list_add_tail(&gt->ggtt_link, &gt->ggtt->gt_list);
121 
122 	return 0;
123 }
124 
intel_gt_init_mmio(struct intel_gt * gt)125 int intel_gt_init_mmio(struct intel_gt *gt)
126 {
127 	intel_gt_init_clock_frequency(gt);
128 
129 	intel_uc_init_mmio(&gt->uc);
130 	intel_sseu_info_init(gt);
131 	intel_gt_mcr_init(gt);
132 
133 	return intel_engines_init_mmio(gt);
134 }
135 
init_unused_ring(struct intel_gt * gt,u32 base)136 static void init_unused_ring(struct intel_gt *gt, u32 base)
137 {
138 	struct intel_uncore *uncore = gt->uncore;
139 
140 	intel_uncore_write(uncore, RING_CTL(base), 0);
141 	intel_uncore_write(uncore, RING_HEAD(base), 0);
142 	intel_uncore_write(uncore, RING_TAIL(base), 0);
143 	intel_uncore_write(uncore, RING_START(base), 0);
144 }
145 
init_unused_rings(struct intel_gt * gt)146 static void init_unused_rings(struct intel_gt *gt)
147 {
148 	struct drm_i915_private *i915 = gt->i915;
149 
150 	if (IS_I830(i915)) {
151 		init_unused_ring(gt, PRB1_BASE);
152 		init_unused_ring(gt, SRB0_BASE);
153 		init_unused_ring(gt, SRB1_BASE);
154 		init_unused_ring(gt, SRB2_BASE);
155 		init_unused_ring(gt, SRB3_BASE);
156 	} else if (GRAPHICS_VER(i915) == 2) {
157 		init_unused_ring(gt, SRB0_BASE);
158 		init_unused_ring(gt, SRB1_BASE);
159 	} else if (GRAPHICS_VER(i915) == 3) {
160 		init_unused_ring(gt, PRB1_BASE);
161 		init_unused_ring(gt, PRB2_BASE);
162 	}
163 }
164 
intel_gt_init_hw(struct intel_gt * gt)165 int intel_gt_init_hw(struct intel_gt *gt)
166 {
167 	struct drm_i915_private *i915 = gt->i915;
168 	struct intel_uncore *uncore = gt->uncore;
169 	int ret;
170 
171 	gt->last_init_time = ktime_get();
172 
173 	/* Double layer security blanket, see i915_gem_init() */
174 	intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
175 
176 	if (HAS_EDRAM(i915) && GRAPHICS_VER(i915) < 9)
177 		intel_uncore_rmw(uncore, HSW_IDICR, 0, IDIHASHMSK(0xf));
178 
179 	if (IS_HASWELL(i915))
180 		intel_uncore_write(uncore,
181 				   HSW_MI_PREDICATE_RESULT_2,
182 				   IS_HASWELL_GT3(i915) ?
183 				   LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
184 
185 	/* Apply the GT workarounds... */
186 	intel_gt_apply_workarounds(gt);
187 	/* ...and determine whether they are sticking. */
188 	intel_gt_verify_workarounds(gt, "init");
189 
190 	intel_gt_init_swizzling(gt);
191 
192 	/*
193 	 * At least 830 can leave some of the unused rings
194 	 * "active" (ie. head != tail) after resume which
195 	 * will prevent c3 entry. Makes sure all unused rings
196 	 * are totally idle.
197 	 */
198 	init_unused_rings(gt);
199 
200 	ret = i915_ppgtt_init_hw(gt);
201 	if (ret) {
202 		gt_err(gt, "Enabling PPGTT failed (%d)\n", ret);
203 		goto out;
204 	}
205 
206 	/* We can't enable contexts until all firmware is loaded */
207 	ret = intel_uc_init_hw(&gt->uc);
208 	if (ret) {
209 		gt_probe_error(gt, "Enabling uc failed (%d)\n", ret);
210 		goto out;
211 	}
212 
213 	intel_mocs_init(gt);
214 
215 out:
216 	intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
217 	return ret;
218 }
219 
gen6_clear_engine_error_register(struct intel_engine_cs * engine)220 static void gen6_clear_engine_error_register(struct intel_engine_cs *engine)
221 {
222 	GEN6_RING_FAULT_REG_RMW(engine, RING_FAULT_VALID, 0);
223 	GEN6_RING_FAULT_REG_POSTING_READ(engine);
224 }
225 
intel_gt_perf_limit_reasons_reg(struct intel_gt * gt)226 i915_reg_t intel_gt_perf_limit_reasons_reg(struct intel_gt *gt)
227 {
228 	/* GT0_PERF_LIMIT_REASONS is available only for Gen11+ */
229 	if (GRAPHICS_VER(gt->i915) < 11)
230 		return INVALID_MMIO_REG;
231 
232 	return gt->type == GT_MEDIA ?
233 		MTL_MEDIA_PERF_LIMIT_REASONS : GT0_PERF_LIMIT_REASONS;
234 }
235 
236 void
intel_gt_clear_error_registers(struct intel_gt * gt,intel_engine_mask_t engine_mask)237 intel_gt_clear_error_registers(struct intel_gt *gt,
238 			       intel_engine_mask_t engine_mask)
239 {
240 	struct drm_i915_private *i915 = gt->i915;
241 	struct intel_uncore *uncore = gt->uncore;
242 	u32 eir;
243 
244 	if (GRAPHICS_VER(i915) != 2)
245 		intel_uncore_write(uncore, PGTBL_ER, 0);
246 
247 	if (GRAPHICS_VER(i915) < 4)
248 		intel_uncore_write(uncore, IPEIR(RENDER_RING_BASE), 0);
249 	else
250 		intel_uncore_write(uncore, IPEIR_I965, 0);
251 
252 	intel_uncore_write(uncore, EIR, 0);
253 	eir = intel_uncore_read(uncore, EIR);
254 	if (eir) {
255 		/*
256 		 * some errors might have become stuck,
257 		 * mask them.
258 		 */
259 		gt_dbg(gt, "EIR stuck: 0x%08x, masking\n", eir);
260 		intel_uncore_rmw(uncore, EMR, 0, eir);
261 		intel_uncore_write(uncore, GEN2_IIR,
262 				   I915_MASTER_ERROR_INTERRUPT);
263 	}
264 
265 	if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 50)) {
266 		intel_gt_mcr_multicast_rmw(gt, XEHP_RING_FAULT_REG,
267 					   RING_FAULT_VALID, 0);
268 		intel_gt_mcr_read_any(gt, XEHP_RING_FAULT_REG);
269 	} else if (GRAPHICS_VER(i915) >= 12) {
270 		intel_uncore_rmw(uncore, GEN12_RING_FAULT_REG, RING_FAULT_VALID, 0);
271 		intel_uncore_posting_read(uncore, GEN12_RING_FAULT_REG);
272 	} else if (GRAPHICS_VER(i915) >= 8) {
273 		intel_uncore_rmw(uncore, GEN8_RING_FAULT_REG, RING_FAULT_VALID, 0);
274 		intel_uncore_posting_read(uncore, GEN8_RING_FAULT_REG);
275 	} else if (GRAPHICS_VER(i915) >= 6) {
276 		struct intel_engine_cs *engine;
277 		enum intel_engine_id id;
278 
279 		for_each_engine_masked(engine, gt, engine_mask, id)
280 			gen6_clear_engine_error_register(engine);
281 	}
282 }
283 
gen6_check_faults(struct intel_gt * gt)284 static void gen6_check_faults(struct intel_gt *gt)
285 {
286 	struct intel_engine_cs *engine;
287 	enum intel_engine_id id;
288 	u32 fault;
289 
290 	for_each_engine(engine, gt, id) {
291 		fault = GEN6_RING_FAULT_REG_READ(engine);
292 		if (fault & RING_FAULT_VALID) {
293 			gt_dbg(gt, "Unexpected fault\n"
294 			       "\tAddr: 0x%08lx\n"
295 			       "\tAddress space: %s\n"
296 			       "\tSource ID: %d\n"
297 			       "\tType: %d\n",
298 			       (unsigned long)(fault & LINUX_PAGE_MASK),
299 			       fault & RING_FAULT_GTTSEL_MASK ?
300 			       "GGTT" : "PPGTT",
301 			       RING_FAULT_SRCID(fault),
302 			       RING_FAULT_FAULT_TYPE(fault));
303 		}
304 	}
305 }
306 
xehp_check_faults(struct intel_gt * gt)307 static void xehp_check_faults(struct intel_gt *gt)
308 {
309 	u32 fault;
310 
311 	/*
312 	 * Although the fault register now lives in an MCR register range,
313 	 * the GAM registers are special and we only truly need to read
314 	 * the "primary" GAM instance rather than handling each instance
315 	 * individually.  intel_gt_mcr_read_any() will automatically steer
316 	 * toward the primary instance.
317 	 */
318 	fault = intel_gt_mcr_read_any(gt, XEHP_RING_FAULT_REG);
319 	if (fault & RING_FAULT_VALID) {
320 		u32 fault_data0, fault_data1;
321 		u64 fault_addr;
322 
323 		fault_data0 = intel_gt_mcr_read_any(gt, XEHP_FAULT_TLB_DATA0);
324 		fault_data1 = intel_gt_mcr_read_any(gt, XEHP_FAULT_TLB_DATA1);
325 
326 		fault_addr = ((u64)(fault_data1 & FAULT_VA_HIGH_BITS) << 44) |
327 			     ((u64)fault_data0 << 12);
328 
329 		gt_dbg(gt, "Unexpected fault\n"
330 		       "\tAddr: 0x%08x_%08x\n"
331 		       "\tAddress space: %s\n"
332 		       "\tEngine ID: %d\n"
333 		       "\tSource ID: %d\n"
334 		       "\tType: %d\n",
335 		       upper_32_bits(fault_addr), lower_32_bits(fault_addr),
336 		       fault_data1 & FAULT_GTT_SEL ? "GGTT" : "PPGTT",
337 		       GEN8_RING_FAULT_ENGINE_ID(fault),
338 		       RING_FAULT_SRCID(fault),
339 		       RING_FAULT_FAULT_TYPE(fault));
340 	}
341 }
342 
gen8_check_faults(struct intel_gt * gt)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 		gt_dbg(gt, "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 
intel_gt_check_and_clear_faults(struct intel_gt * gt)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_FULL(i915) >= IP_VER(12, 50))
390 		xehp_check_faults(gt);
391 	else if (GRAPHICS_VER(i915) >= 8)
392 		gen8_check_faults(gt);
393 	else if (GRAPHICS_VER(i915) >= 6)
394 		gen6_check_faults(gt);
395 	else
396 		return;
397 
398 	intel_gt_clear_error_registers(gt, ALL_ENGINES);
399 }
400 
intel_gt_flush_ggtt_writes(struct intel_gt * gt)401 void intel_gt_flush_ggtt_writes(struct intel_gt *gt)
402 {
403 	struct intel_uncore *uncore = gt->uncore;
404 	intel_wakeref_t wakeref;
405 
406 	/*
407 	 * No actual flushing is required for the GTT write domain for reads
408 	 * from the GTT domain. Writes to it "immediately" go to main memory
409 	 * as far as we know, so there's no chipset flush. It also doesn't
410 	 * land in the GPU render cache.
411 	 *
412 	 * However, we do have to enforce the order so that all writes through
413 	 * the GTT land before any writes to the device, such as updates to
414 	 * the GATT itself.
415 	 *
416 	 * We also have to wait a bit for the writes to land from the GTT.
417 	 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
418 	 * timing. This issue has only been observed when switching quickly
419 	 * between GTT writes and CPU reads from inside the kernel on recent hw,
420 	 * and it appears to only affect discrete GTT blocks (i.e. on LLC
421 	 * system agents we cannot reproduce this behaviour, until Cannonlake
422 	 * that was!).
423 	 */
424 
425 	wmb();
426 
427 	if (INTEL_INFO(gt->i915)->has_coherent_ggtt)
428 		return;
429 
430 	intel_gt_chipset_flush(gt);
431 
432 	with_intel_runtime_pm_if_in_use(uncore->rpm, wakeref) {
433 		unsigned long flags;
434 
435 		spin_lock_irqsave(&uncore->lock, flags);
436 		intel_uncore_posting_read_fw(uncore,
437 					     RING_HEAD(RENDER_RING_BASE));
438 		spin_unlock_irqrestore(&uncore->lock, flags);
439 	}
440 }
441 
intel_gt_chipset_flush(struct intel_gt * gt)442 void intel_gt_chipset_flush(struct intel_gt *gt)
443 {
444 	wmb();
445 	if (GRAPHICS_VER(gt->i915) < 6)
446 		intel_ggtt_gmch_flush();
447 }
448 
intel_gt_driver_register(struct intel_gt * gt)449 void intel_gt_driver_register(struct intel_gt *gt)
450 {
451 	intel_gsc_init(&gt->gsc, gt->i915);
452 
453 	intel_rps_driver_register(&gt->rps);
454 
455 	intel_gt_debugfs_register(gt);
456 	intel_gt_sysfs_register(gt);
457 }
458 
intel_gt_init_scratch(struct intel_gt * gt,unsigned int size)459 static int intel_gt_init_scratch(struct intel_gt *gt, unsigned int size)
460 {
461 	struct drm_i915_private *i915 = gt->i915;
462 	struct drm_i915_gem_object *obj;
463 	struct i915_vma *vma;
464 	int ret;
465 
466 	obj = i915_gem_object_create_lmem(i915, size,
467 					  I915_BO_ALLOC_VOLATILE |
468 					  I915_BO_ALLOC_GPU_ONLY);
469 	if (IS_ERR(obj) && !IS_METEORLAKE(i915)) /* Wa_22018444074 */
470 		obj = i915_gem_object_create_stolen(i915, size);
471 	if (IS_ERR(obj))
472 		obj = i915_gem_object_create_internal(i915, size);
473 	if (IS_ERR(obj)) {
474 		gt_err(gt, "Failed to allocate scratch page\n");
475 		return PTR_ERR(obj);
476 	}
477 
478 	vma = i915_vma_instance(obj, &gt->ggtt->vm, NULL);
479 	if (IS_ERR(vma)) {
480 		ret = PTR_ERR(vma);
481 		goto err_unref;
482 	}
483 
484 	ret = i915_ggtt_pin(vma, NULL, 0, PIN_HIGH);
485 	if (ret)
486 		goto err_unref;
487 
488 	gt->scratch = i915_vma_make_unshrinkable(vma);
489 
490 	return 0;
491 
492 err_unref:
493 	i915_gem_object_put(obj);
494 	return ret;
495 }
496 
intel_gt_fini_scratch(struct intel_gt * gt)497 static void intel_gt_fini_scratch(struct intel_gt *gt)
498 {
499 	i915_vma_unpin_and_release(&gt->scratch, 0);
500 }
501 
kernel_vm(struct intel_gt * gt)502 static struct i915_address_space *kernel_vm(struct intel_gt *gt)
503 {
504 	if (INTEL_PPGTT(gt->i915) > INTEL_PPGTT_ALIASING)
505 		return &i915_ppgtt_create(gt, I915_BO_ALLOC_PM_EARLY)->vm;
506 	else
507 		return i915_vm_get(&gt->ggtt->vm);
508 }
509 
__engines_record_defaults(struct intel_gt * gt)510 static int __engines_record_defaults(struct intel_gt *gt)
511 {
512 	struct i915_request *requests[I915_NUM_ENGINES] = {};
513 	struct intel_engine_cs *engine;
514 	enum intel_engine_id id;
515 	int err = 0;
516 
517 	/*
518 	 * As we reset the gpu during very early sanitisation, the current
519 	 * register state on the GPU should reflect its defaults values.
520 	 * We load a context onto the hw (with restore-inhibit), then switch
521 	 * over to a second context to save that default register state. We
522 	 * can then prime every new context with that state so they all start
523 	 * from the same default HW values.
524 	 */
525 
526 	for_each_engine(engine, gt, id) {
527 		struct intel_renderstate so;
528 		struct intel_context *ce;
529 		struct i915_request *rq;
530 
531 		/* We must be able to switch to something! */
532 		GEM_BUG_ON(!engine->kernel_context);
533 
534 		ce = intel_context_create(engine);
535 		if (IS_ERR(ce)) {
536 			err = PTR_ERR(ce);
537 			goto out;
538 		}
539 
540 		err = intel_renderstate_init(&so, ce);
541 		if (err)
542 			goto err;
543 
544 		rq = i915_request_create(ce);
545 		if (IS_ERR(rq)) {
546 			err = PTR_ERR(rq);
547 			goto err_fini;
548 		}
549 
550 		err = intel_engine_emit_ctx_wa(rq);
551 		if (err)
552 			goto err_rq;
553 
554 		err = intel_renderstate_emit(&so, rq);
555 		if (err)
556 			goto err_rq;
557 
558 err_rq:
559 		requests[id] = i915_request_get(rq);
560 		i915_request_add(rq);
561 err_fini:
562 		intel_renderstate_fini(&so, ce);
563 err:
564 		if (err) {
565 			intel_context_put(ce);
566 			goto out;
567 		}
568 	}
569 
570 	/* Flush the default context image to memory, and enable powersaving. */
571 	if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
572 		err = -EIO;
573 		goto out;
574 	}
575 
576 	for (id = 0; id < ARRAY_SIZE(requests); id++) {
577 		struct i915_request *rq;
578 		struct uvm_object *state;
579 
580 		rq = requests[id];
581 		if (!rq)
582 			continue;
583 
584 		if (rq->fence.error) {
585 			err = -EIO;
586 			goto out;
587 		}
588 
589 		GEM_BUG_ON(!test_bit(CONTEXT_ALLOC_BIT, &rq->context->flags));
590 		if (!rq->context->state)
591 			continue;
592 
593 		/* Keep a copy of the state's backing pages; free the obj */
594 #ifdef __linux__
595 		state = shmem_create_from_object(rq->context->state->obj);
596 #else
597 		state = uao_create_from_object(rq->context->state->obj);
598 #endif
599 		if (IS_ERR(state)) {
600 			err = PTR_ERR(state);
601 			goto out;
602 		}
603 		rq->engine->default_state = state;
604 	}
605 
606 out:
607 	/*
608 	 * If we have to abandon now, we expect the engines to be idle
609 	 * and ready to be torn-down. The quickest way we can accomplish
610 	 * this is by declaring ourselves wedged.
611 	 */
612 	if (err)
613 		intel_gt_set_wedged(gt);
614 
615 	for (id = 0; id < ARRAY_SIZE(requests); id++) {
616 		struct intel_context *ce;
617 		struct i915_request *rq;
618 
619 		rq = requests[id];
620 		if (!rq)
621 			continue;
622 
623 		ce = rq->context;
624 		i915_request_put(rq);
625 		intel_context_put(ce);
626 	}
627 	return err;
628 }
629 
__engines_verify_workarounds(struct intel_gt * gt)630 static int __engines_verify_workarounds(struct intel_gt *gt)
631 {
632 	struct intel_engine_cs *engine;
633 	enum intel_engine_id id;
634 	int err = 0;
635 
636 	if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
637 		return 0;
638 
639 	for_each_engine(engine, gt, id) {
640 		if (intel_engine_verify_workarounds(engine, "load"))
641 			err = -EIO;
642 	}
643 
644 	/* Flush and restore the kernel context for safety */
645 	if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME)
646 		err = -EIO;
647 
648 	return err;
649 }
650 
__intel_gt_disable(struct intel_gt * gt)651 static void __intel_gt_disable(struct intel_gt *gt)
652 {
653 	intel_gt_set_wedged_on_fini(gt);
654 
655 	intel_gt_suspend_prepare(gt);
656 	intel_gt_suspend_late(gt);
657 
658 	GEM_BUG_ON(intel_gt_pm_is_awake(gt));
659 }
660 
intel_gt_wait_for_idle(struct intel_gt * gt,long timeout)661 int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout)
662 {
663 	long remaining_timeout;
664 
665 	/* If the device is asleep, we have no requests outstanding */
666 	if (!intel_gt_pm_is_awake(gt))
667 		return 0;
668 
669 	while ((timeout = intel_gt_retire_requests_timeout(gt, timeout,
670 							   &remaining_timeout)) > 0) {
671 		cond_resched();
672 		if (signal_pending(current))
673 			return -EINTR;
674 	}
675 
676 	if (timeout)
677 		return timeout;
678 
679 	if (remaining_timeout < 0)
680 		remaining_timeout = 0;
681 
682 	return intel_uc_wait_for_idle(&gt->uc, remaining_timeout);
683 }
684 
intel_gt_init(struct intel_gt * gt)685 int intel_gt_init(struct intel_gt *gt)
686 {
687 	int err;
688 
689 	err = i915_inject_probe_error(gt->i915, -ENODEV);
690 	if (err)
691 		return err;
692 
693 	intel_gt_init_workarounds(gt);
694 
695 	/*
696 	 * This is just a security blanket to placate dragons.
697 	 * On some systems, we very sporadically observe that the first TLBs
698 	 * used by the CS may be stale, despite us poking the TLB reset. If
699 	 * we hold the forcewake during initialisation these problems
700 	 * just magically go away.
701 	 */
702 	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
703 
704 	err = intel_gt_init_scratch(gt,
705 				    GRAPHICS_VER(gt->i915) == 2 ? SZ_256K : SZ_4K);
706 	if (err)
707 		goto out_fw;
708 
709 	intel_gt_pm_init(gt);
710 
711 	gt->vm = kernel_vm(gt);
712 	if (!gt->vm) {
713 		err = -ENOMEM;
714 		goto err_pm;
715 	}
716 
717 	intel_set_mocs_index(gt);
718 
719 	err = intel_engines_init(gt);
720 	if (err)
721 		goto err_engines;
722 
723 	err = intel_uc_init(&gt->uc);
724 	if (err)
725 		goto err_engines;
726 
727 	err = intel_gt_resume(gt);
728 	if (err)
729 		goto err_uc_init;
730 
731 	err = intel_gt_init_hwconfig(gt);
732 	if (err)
733 		gt_err(gt, "Failed to retrieve hwconfig table: %pe\n", ERR_PTR(err));
734 
735 	err = __engines_record_defaults(gt);
736 	if (err)
737 		goto err_gt;
738 
739 	err = __engines_verify_workarounds(gt);
740 	if (err)
741 		goto err_gt;
742 
743 	err = i915_inject_probe_error(gt->i915, -EIO);
744 	if (err)
745 		goto err_gt;
746 
747 	intel_uc_init_late(&gt->uc);
748 
749 	intel_migrate_init(&gt->migrate, gt);
750 
751 	goto out_fw;
752 err_gt:
753 	__intel_gt_disable(gt);
754 	intel_uc_fini_hw(&gt->uc);
755 err_uc_init:
756 	intel_uc_fini(&gt->uc);
757 err_engines:
758 	intel_engines_release(gt);
759 	i915_vm_put(fetch_and_zero(&gt->vm));
760 err_pm:
761 	intel_gt_pm_fini(gt);
762 	intel_gt_fini_scratch(gt);
763 out_fw:
764 	if (err)
765 		intel_gt_set_wedged_on_init(gt);
766 	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
767 	return err;
768 }
769 
intel_gt_driver_remove(struct intel_gt * gt)770 void intel_gt_driver_remove(struct intel_gt *gt)
771 {
772 	__intel_gt_disable(gt);
773 
774 	intel_migrate_fini(&gt->migrate);
775 	intel_uc_driver_remove(&gt->uc);
776 
777 	intel_engines_release(gt);
778 
779 	intel_gt_flush_buffer_pool(gt);
780 }
781 
intel_gt_driver_unregister(struct intel_gt * gt)782 void intel_gt_driver_unregister(struct intel_gt *gt)
783 {
784 	intel_wakeref_t wakeref;
785 
786 	intel_gt_sysfs_unregister(gt);
787 	intel_rps_driver_unregister(&gt->rps);
788 	intel_gsc_fini(&gt->gsc);
789 
790 	/*
791 	 * If we unload the driver and wedge before the GSC worker is complete,
792 	 * the worker will hit an error on its submission to the GSC engine and
793 	 * then exit. This is hard to hit for a user, but it is reproducible
794 	 * with skipping selftests. The error is handled gracefully by the
795 	 * worker, so there are no functional issues, but we still end up with
796 	 * an error message in dmesg, which is something we want to avoid as
797 	 * this is a supported scenario. We could modify the worker to better
798 	 * handle a wedging occurring during its execution, but that gets
799 	 * complicated for a couple of reasons:
800 	 * - We do want the error on runtime wedging, because there are
801 	 *   implications for subsystems outside of GT (i.e., PXP, HDCP), it's
802 	 *   only the error on driver unload that we want to silence.
803 	 * - The worker is responsible for multiple submissions (GSC FW load,
804 	 *   HuC auth, SW proxy), so all of those will have to be adapted to
805 	 *   handle the wedged_on_fini scenario.
806 	 * Therefore, it's much simpler to just wait for the worker to be done
807 	 * before wedging on driver removal, also considering that the worker
808 	 * will likely already be idle in the great majority of non-selftest
809 	 * scenarios.
810 	 */
811 	intel_gsc_uc_flush_work(&gt->uc.gsc);
812 
813 	/*
814 	 * Upon unregistering the device to prevent any new users, cancel
815 	 * all in-flight requests so that we can quickly unbind the active
816 	 * resources.
817 	 */
818 	intel_gt_set_wedged_on_fini(gt);
819 
820 	/* Scrub all HW state upon release */
821 	with_intel_runtime_pm(gt->uncore->rpm, wakeref)
822 		__intel_gt_reset(gt, ALL_ENGINES);
823 }
824 
intel_gt_driver_release(struct intel_gt * gt)825 void intel_gt_driver_release(struct intel_gt *gt)
826 {
827 	struct i915_address_space *vm;
828 
829 	vm = fetch_and_zero(&gt->vm);
830 	if (vm) /* FIXME being called twice on error paths :( */
831 		i915_vm_put(vm);
832 
833 	intel_wa_list_free(&gt->wa_list);
834 	intel_gt_pm_fini(gt);
835 	intel_gt_fini_scratch(gt);
836 	intel_gt_fini_buffer_pool(gt);
837 	intel_gt_fini_hwconfig(gt);
838 }
839 
intel_gt_driver_late_release_all(struct drm_i915_private * i915)840 void intel_gt_driver_late_release_all(struct drm_i915_private *i915)
841 {
842 	struct intel_gt *gt;
843 	unsigned int id;
844 
845 	/* We need to wait for inflight RCU frees to release their grip */
846 	rcu_barrier();
847 
848 	for_each_gt(gt, i915, id) {
849 		intel_uc_driver_late_release(&gt->uc);
850 		intel_gt_fini_requests(gt);
851 		intel_gt_fini_reset(gt);
852 		intel_gt_fini_timelines(gt);
853 		intel_gt_fini_tlb(gt);
854 		intel_engines_free(gt);
855 	}
856 }
857 
intel_gt_tile_setup(struct intel_gt * gt,phys_addr_t phys_addr)858 static int intel_gt_tile_setup(struct intel_gt *gt, phys_addr_t phys_addr)
859 {
860 	int ret;
861 
862 	if (!gt_is_root(gt)) {
863 		struct intel_uncore *uncore;
864 		spinlock_t *irq_lock;
865 
866 		uncore = drmm_kzalloc(&gt->i915->drm, sizeof(*uncore), GFP_KERNEL);
867 		if (!uncore)
868 			return -ENOMEM;
869 
870 		irq_lock = drmm_kzalloc(&gt->i915->drm, sizeof(*irq_lock), GFP_KERNEL);
871 		if (!irq_lock)
872 			return -ENOMEM;
873 
874 		gt->uncore = uncore;
875 		gt->irq_lock = irq_lock;
876 
877 		intel_gt_common_init_early(gt);
878 	}
879 
880 	intel_uncore_init_early(gt->uncore, gt);
881 
882 	ret = intel_uncore_setup_mmio(gt->uncore, phys_addr);
883 	if (ret)
884 		return ret;
885 
886 	gt->phys_addr = phys_addr;
887 
888 	return 0;
889 }
890 
891 #ifdef __linux__
892 
intel_gt_probe_all(struct drm_i915_private * i915)893 int intel_gt_probe_all(struct drm_i915_private *i915)
894 {
895 	struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
896 	struct intel_gt *gt = to_gt(i915);
897 	const struct intel_gt_definition *gtdef;
898 	phys_addr_t phys_addr;
899 	unsigned int mmio_bar;
900 	unsigned int i;
901 	int ret;
902 
903 	mmio_bar = intel_mmio_bar(GRAPHICS_VER(i915));
904 	phys_addr = pci_resource_start(pdev, mmio_bar);
905 
906 	/*
907 	 * We always have at least one primary GT on any device
908 	 * and it has been already initialized early during probe
909 	 * in i915_driver_probe()
910 	 */
911 	gt->i915 = i915;
912 	gt->name = "Primary GT";
913 	gt->info.engine_mask = INTEL_INFO(i915)->platform_engine_mask;
914 
915 	gt_dbg(gt, "Setting up %s\n", gt->name);
916 	ret = intel_gt_tile_setup(gt, phys_addr);
917 	if (ret)
918 		return ret;
919 
920 	i915->gt[0] = gt;
921 
922 	if (!HAS_EXTRA_GT_LIST(i915))
923 		return 0;
924 
925 	for (i = 1, gtdef = &INTEL_INFO(i915)->extra_gt_list[i - 1];
926 	     gtdef->name != NULL;
927 	     i++, gtdef = &INTEL_INFO(i915)->extra_gt_list[i - 1]) {
928 		gt = drmm_kzalloc(&i915->drm, sizeof(*gt), GFP_KERNEL);
929 		if (!gt) {
930 			ret = -ENOMEM;
931 			goto err;
932 		}
933 
934 		gt->i915 = i915;
935 		gt->name = gtdef->name;
936 		gt->type = gtdef->type;
937 		gt->info.engine_mask = gtdef->engine_mask;
938 		gt->info.id = i;
939 
940 		gt_dbg(gt, "Setting up %s\n", gt->name);
941 		if (GEM_WARN_ON(range_overflows_t(resource_size_t,
942 						  gtdef->mapping_base,
943 						  SZ_16M,
944 						  pci_resource_len(pdev, mmio_bar)))) {
945 			ret = -ENODEV;
946 			goto err;
947 		}
948 
949 		switch (gtdef->type) {
950 		case GT_TILE:
951 			ret = intel_gt_tile_setup(gt, phys_addr + gtdef->mapping_base);
952 			break;
953 
954 		case GT_MEDIA:
955 			ret = intel_sa_mediagt_setup(gt, phys_addr + gtdef->mapping_base,
956 						     gtdef->gsi_offset);
957 			break;
958 
959 		case GT_PRIMARY:
960 			/* Primary GT should not appear in extra GT list */
961 		default:
962 			MISSING_CASE(gtdef->type);
963 			ret = -ENODEV;
964 		}
965 
966 		if (ret)
967 			goto err;
968 
969 		i915->gt[i] = gt;
970 	}
971 
972 	return 0;
973 
974 err:
975 	i915_probe_error(i915, "Failed to initialize %s! (%d)\n", gtdef->name, ret);
976 	return ret;
977 }
978 
979 #else
980 
intel_gt_probe_all(struct drm_i915_private * i915)981 int intel_gt_probe_all(struct drm_i915_private *i915)
982 {
983 	struct pci_dev *pdev = i915->drm.pdev;
984 	struct intel_gt *gt = to_gt(i915);
985 	const struct intel_gt_definition *gtdef;
986 	phys_addr_t phys_addr;
987 	bus_size_t len;
988 	pcireg_t type;
989 	int flags;
990 	unsigned int mmio_bar;
991 	unsigned int i;
992 	int ret;
993 
994 	mmio_bar = intel_mmio_bar(GRAPHICS_VER(i915));
995 	type = pci_mapreg_type(i915->pc, i915->tag, 0x10 + (mmio_bar * 4));
996 	ret = -pci_mapreg_info(i915->pc, i915->tag, 0x10 + (mmio_bar * 4), type,
997 	    &phys_addr, &len, NULL);
998 	if (ret)
999 		return ret;
1000 
1001 	/*
1002 	 * We always have at least one primary GT on any device
1003 	 * and it has been already initialized early during probe
1004 	 * in i915_driver_probe()
1005 	 */
1006 	gt->i915 = i915;
1007 	gt->name = "Primary GT";
1008 	gt->info.engine_mask = INTEL_INFO(i915)->platform_engine_mask;
1009 
1010 	gt_dbg(gt, "Setting up %s\n", gt->name);
1011 	ret = intel_gt_tile_setup(gt, phys_addr);
1012 	if (ret)
1013 		return ret;
1014 
1015 	i915->gt[0] = gt;
1016 
1017 	if (!HAS_EXTRA_GT_LIST(i915))
1018 		return 0;
1019 
1020 	for (i = 1, gtdef = &INTEL_INFO(i915)->extra_gt_list[i - 1];
1021 	     gtdef->name != NULL;
1022 	     i++, gtdef = &INTEL_INFO(i915)->extra_gt_list[i - 1]) {
1023 		gt = drmm_kzalloc(&i915->drm, sizeof(*gt), GFP_KERNEL);
1024 		if (!gt) {
1025 			ret = -ENOMEM;
1026 			goto err;
1027 		}
1028 
1029 		gt->i915 = i915;
1030 		gt->name = gtdef->name;
1031 		gt->type = gtdef->type;
1032 		gt->info.engine_mask = gtdef->engine_mask;
1033 		gt->info.id = i;
1034 
1035 		gt_dbg(gt, "Setting up %s\n", gt->name);
1036 		if (GEM_WARN_ON(range_overflows_t(resource_size_t,
1037 						  gtdef->mapping_base,
1038 						  SZ_16M,
1039 						  len))) {
1040 			ret = -ENODEV;
1041 			goto err;
1042 		}
1043 
1044 		switch (gtdef->type) {
1045 		case GT_TILE:
1046 			ret = intel_gt_tile_setup(gt, phys_addr + gtdef->mapping_base);
1047 			break;
1048 
1049 		case GT_MEDIA:
1050 			ret = intel_sa_mediagt_setup(gt, phys_addr + gtdef->mapping_base,
1051 						     gtdef->gsi_offset);
1052 			break;
1053 
1054 		case GT_PRIMARY:
1055 			/* Primary GT should not appear in extra GT list */
1056 		default:
1057 			MISSING_CASE(gtdef->type);
1058 			ret = -ENODEV;
1059 		}
1060 
1061 		if (ret)
1062 			goto err;
1063 
1064 		i915->gt[i] = gt;
1065 	}
1066 
1067 	return 0;
1068 
1069 err:
1070 	i915_probe_error(i915, "Failed to initialize %s! (%d)\n", gtdef->name, ret);
1071 	return ret;
1072 }
1073 
1074 #endif
1075 
__intel_gt_bind_context_set_ready(struct intel_gt * gt,bool ready)1076 static void __intel_gt_bind_context_set_ready(struct intel_gt *gt, bool ready)
1077 {
1078 	struct intel_engine_cs *engine = gt->engine[BCS0];
1079 
1080 	if (engine && engine->bind_context)
1081 		engine->bind_context_ready = ready;
1082 }
1083 
1084 /**
1085  * intel_gt_bind_context_set_ready - Set the context binding as ready
1086  *
1087  * @gt: GT structure
1088  *
1089  * This function marks the binder context as ready.
1090  */
intel_gt_bind_context_set_ready(struct intel_gt * gt)1091 void intel_gt_bind_context_set_ready(struct intel_gt *gt)
1092 {
1093 	__intel_gt_bind_context_set_ready(gt, true);
1094 }
1095 
1096 /**
1097  * intel_gt_bind_context_set_unready - Set the context binding as ready
1098  * @gt: GT structure
1099  *
1100  * This function marks the binder context as not ready.
1101  */
1102 
intel_gt_bind_context_set_unready(struct intel_gt * gt)1103 void intel_gt_bind_context_set_unready(struct intel_gt *gt)
1104 {
1105 	__intel_gt_bind_context_set_ready(gt, false);
1106 }
1107 
1108 /**
1109  * intel_gt_is_bind_context_ready - Check if context binding is ready
1110  *
1111  * @gt: GT structure
1112  *
1113  * This function returns binder context's ready status.
1114  */
intel_gt_is_bind_context_ready(struct intel_gt * gt)1115 bool intel_gt_is_bind_context_ready(struct intel_gt *gt)
1116 {
1117 	struct intel_engine_cs *engine = gt->engine[BCS0];
1118 
1119 	if (engine)
1120 		return engine->bind_context_ready;
1121 
1122 	return false;
1123 }
1124 
intel_gt_tiles_init(struct drm_i915_private * i915)1125 int intel_gt_tiles_init(struct drm_i915_private *i915)
1126 {
1127 	struct intel_gt *gt;
1128 	unsigned int id;
1129 	int ret;
1130 
1131 	for_each_gt(gt, i915, id) {
1132 		ret = intel_gt_probe_lmem(gt);
1133 		if (ret)
1134 			return ret;
1135 	}
1136 
1137 	return 0;
1138 }
1139 
intel_gt_info_print(const struct intel_gt_info * info,struct drm_printer * p)1140 void intel_gt_info_print(const struct intel_gt_info *info,
1141 			 struct drm_printer *p)
1142 {
1143 	drm_printf(p, "available engines: %x\n", info->engine_mask);
1144 
1145 	intel_sseu_dump(&info->sseu, p);
1146 }
1147 
intel_gt_coherent_map_type(struct intel_gt * gt,struct drm_i915_gem_object * obj,bool always_coherent)1148 enum i915_map_type intel_gt_coherent_map_type(struct intel_gt *gt,
1149 					      struct drm_i915_gem_object *obj,
1150 					      bool always_coherent)
1151 {
1152 	/*
1153 	 * Wa_22016122933: always return I915_MAP_WC for Media
1154 	 * version 13.0 when the object is on the Media GT
1155 	 */
1156 	if (i915_gem_object_is_lmem(obj) || intel_gt_needs_wa_22016122933(gt))
1157 		return I915_MAP_WC;
1158 	if (HAS_LLC(gt->i915) || always_coherent)
1159 		return I915_MAP_WB;
1160 	else
1161 		return I915_MAP_WC;
1162 }
1163