1 // SPDX-License-Identifier: MIT
3 * Copyright © 2019 Intel Corporation
6 #include "debugfs_gt.h"
8 #include "gem/i915_gem_lmem.h"
10 #include "intel_context.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_mocs.h"
17 #include "intel_rc6.h"
18 #include "intel_renderstate.h"
19 #include "intel_rps.h"
20 #include "intel_uncore.h"
22 #include "shmem_utils.h"
24 void intel_gt_init_early(struct intel_gt *gt, struct drm_i915_private *i915)
27 gt->uncore = &i915->uncore;
29 spin_lock_init(>->irq_lock);
31 INIT_LIST_HEAD(>->closed_vma);
32 spin_lock_init(>->closed_lock);
34 init_llist_head(>->watchdog.list);
35 INIT_WORK(>->watchdog.work, intel_gt_watchdog_work);
37 intel_gt_init_buffer_pool(gt);
38 intel_gt_init_reset(gt);
39 intel_gt_init_requests(gt);
40 intel_gt_init_timelines(gt);
41 intel_gt_pm_init_early(gt);
43 intel_rps_init_early(>->rps);
44 intel_uc_init_early(>->uc);
47 int intel_gt_probe_lmem(struct intel_gt *gt)
49 struct drm_i915_private *i915 = gt->i915;
50 struct intel_memory_region *mem;
54 mem = intel_gt_setup_lmem(gt);
55 if (mem == ERR_PTR(-ENODEV))
56 mem = intel_gt_setup_fake_lmem(gt);
63 "Failed to setup region(%d) type=%d\n",
64 err, INTEL_MEMORY_LOCAL);
68 id = INTEL_REGION_LMEM;
72 intel_memory_region_set_name(mem, "local%u", mem->instance);
74 GEM_BUG_ON(!HAS_REGION(i915, id));
75 GEM_BUG_ON(i915->mm.regions[id]);
76 i915->mm.regions[id] = mem;
81 void intel_gt_init_hw_early(struct intel_gt *gt, struct i915_ggtt *ggtt)
86 int intel_gt_init_mmio(struct intel_gt *gt)
88 intel_gt_init_clock_frequency(gt);
90 intel_uc_init_mmio(>->uc);
91 intel_sseu_info_init(gt);
93 return intel_engines_init_mmio(gt);
96 static void init_unused_ring(struct intel_gt *gt, u32 base)
98 struct intel_uncore *uncore = gt->uncore;
100 intel_uncore_write(uncore, RING_CTL(base), 0);
101 intel_uncore_write(uncore, RING_HEAD(base), 0);
102 intel_uncore_write(uncore, RING_TAIL(base), 0);
103 intel_uncore_write(uncore, RING_START(base), 0);
106 static void init_unused_rings(struct intel_gt *gt)
108 struct drm_i915_private *i915 = gt->i915;
111 init_unused_ring(gt, PRB1_BASE);
112 init_unused_ring(gt, SRB0_BASE);
113 init_unused_ring(gt, SRB1_BASE);
114 init_unused_ring(gt, SRB2_BASE);
115 init_unused_ring(gt, SRB3_BASE);
116 } else if (GRAPHICS_VER(i915) == 2) {
117 init_unused_ring(gt, SRB0_BASE);
118 init_unused_ring(gt, SRB1_BASE);
119 } else if (GRAPHICS_VER(i915) == 3) {
120 init_unused_ring(gt, PRB1_BASE);
121 init_unused_ring(gt, PRB2_BASE);
125 int intel_gt_init_hw(struct intel_gt *gt)
127 struct drm_i915_private *i915 = gt->i915;
128 struct intel_uncore *uncore = gt->uncore;
131 gt->last_init_time = ktime_get();
133 /* Double layer security blanket, see i915_gem_init() */
134 intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
136 if (HAS_EDRAM(i915) && GRAPHICS_VER(i915) < 9)
137 intel_uncore_rmw(uncore, HSW_IDICR, 0, IDIHASHMSK(0xf));
139 if (IS_HASWELL(i915))
140 intel_uncore_write(uncore,
141 MI_PREDICATE_RESULT_2,
143 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
145 /* Apply the GT workarounds... */
146 intel_gt_apply_workarounds(gt);
147 /* ...and determine whether they are sticking. */
148 intel_gt_verify_workarounds(gt, "init");
150 intel_gt_init_swizzling(gt);
153 * At least 830 can leave some of the unused rings
154 * "active" (ie. head != tail) after resume which
155 * will prevent c3 entry. Makes sure all unused rings
158 init_unused_rings(gt);
160 ret = i915_ppgtt_init_hw(gt);
162 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
166 /* We can't enable contexts until all firmware is loaded */
167 ret = intel_uc_init_hw(>->uc);
169 i915_probe_error(i915, "Enabling uc failed (%d)\n", ret);
176 intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
180 static void rmw_set(struct intel_uncore *uncore, i915_reg_t reg, u32 set)
182 intel_uncore_rmw(uncore, reg, 0, set);
185 static void rmw_clear(struct intel_uncore *uncore, i915_reg_t reg, u32 clr)
187 intel_uncore_rmw(uncore, reg, clr, 0);
190 static void clear_register(struct intel_uncore *uncore, i915_reg_t reg)
192 intel_uncore_rmw(uncore, reg, 0, 0);
195 static void gen8_clear_engine_error_register(struct intel_engine_cs *engine)
197 GEN6_RING_FAULT_REG_RMW(engine, RING_FAULT_VALID, 0);
198 GEN6_RING_FAULT_REG_POSTING_READ(engine);
202 intel_gt_clear_error_registers(struct intel_gt *gt,
203 intel_engine_mask_t engine_mask)
205 struct drm_i915_private *i915 = gt->i915;
206 struct intel_uncore *uncore = gt->uncore;
209 if (GRAPHICS_VER(i915) != 2)
210 clear_register(uncore, PGTBL_ER);
212 if (GRAPHICS_VER(i915) < 4)
213 clear_register(uncore, IPEIR(RENDER_RING_BASE));
215 clear_register(uncore, IPEIR_I965);
217 clear_register(uncore, EIR);
218 eir = intel_uncore_read(uncore, EIR);
221 * some errors might have become stuck,
224 DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masking\n", eir);
225 rmw_set(uncore, EMR, eir);
226 intel_uncore_write(uncore, GEN2_IIR,
227 I915_MASTER_ERROR_INTERRUPT);
230 if (GRAPHICS_VER(i915) >= 12) {
231 rmw_clear(uncore, GEN12_RING_FAULT_REG, RING_FAULT_VALID);
232 intel_uncore_posting_read(uncore, GEN12_RING_FAULT_REG);
233 } else if (GRAPHICS_VER(i915) >= 8) {
234 rmw_clear(uncore, GEN8_RING_FAULT_REG, RING_FAULT_VALID);
235 intel_uncore_posting_read(uncore, GEN8_RING_FAULT_REG);
236 } else if (GRAPHICS_VER(i915) >= 6) {
237 struct intel_engine_cs *engine;
238 enum intel_engine_id id;
240 for_each_engine_masked(engine, gt, engine_mask, id)
241 gen8_clear_engine_error_register(engine);
245 static void gen6_check_faults(struct intel_gt *gt)
247 struct intel_engine_cs *engine;
248 enum intel_engine_id id;
251 for_each_engine(engine, gt, id) {
252 fault = GEN6_RING_FAULT_REG_READ(engine);
253 if (fault & RING_FAULT_VALID) {
254 drm_dbg(&engine->i915->drm, "Unexpected fault\n"
256 "\tAddress space: %s\n"
260 fault & RING_FAULT_GTTSEL_MASK ?
262 RING_FAULT_SRCID(fault),
263 RING_FAULT_FAULT_TYPE(fault));
268 static void gen8_check_faults(struct intel_gt *gt)
270 struct intel_uncore *uncore = gt->uncore;
271 i915_reg_t fault_reg, fault_data0_reg, fault_data1_reg;
274 if (GRAPHICS_VER(gt->i915) >= 12) {
275 fault_reg = GEN12_RING_FAULT_REG;
276 fault_data0_reg = GEN12_FAULT_TLB_DATA0;
277 fault_data1_reg = GEN12_FAULT_TLB_DATA1;
279 fault_reg = GEN8_RING_FAULT_REG;
280 fault_data0_reg = GEN8_FAULT_TLB_DATA0;
281 fault_data1_reg = GEN8_FAULT_TLB_DATA1;
284 fault = intel_uncore_read(uncore, fault_reg);
285 if (fault & RING_FAULT_VALID) {
286 u32 fault_data0, fault_data1;
289 fault_data0 = intel_uncore_read(uncore, fault_data0_reg);
290 fault_data1 = intel_uncore_read(uncore, fault_data1_reg);
292 fault_addr = ((u64)(fault_data1 & FAULT_VA_HIGH_BITS) << 44) |
293 ((u64)fault_data0 << 12);
295 drm_dbg(&uncore->i915->drm, "Unexpected fault\n"
296 "\tAddr: 0x%08x_%08x\n"
297 "\tAddress space: %s\n"
301 upper_32_bits(fault_addr), lower_32_bits(fault_addr),
302 fault_data1 & FAULT_GTT_SEL ? "GGTT" : "PPGTT",
303 GEN8_RING_FAULT_ENGINE_ID(fault),
304 RING_FAULT_SRCID(fault),
305 RING_FAULT_FAULT_TYPE(fault));
309 void intel_gt_check_and_clear_faults(struct intel_gt *gt)
311 struct drm_i915_private *i915 = gt->i915;
313 /* From GEN8 onwards we only have one 'All Engine Fault Register' */
314 if (GRAPHICS_VER(i915) >= 8)
315 gen8_check_faults(gt);
316 else if (GRAPHICS_VER(i915) >= 6)
317 gen6_check_faults(gt);
321 intel_gt_clear_error_registers(gt, ALL_ENGINES);
324 void intel_gt_flush_ggtt_writes(struct intel_gt *gt)
326 struct intel_uncore *uncore = gt->uncore;
327 intel_wakeref_t wakeref;
330 * No actual flushing is required for the GTT write domain for reads
331 * from the GTT domain. Writes to it "immediately" go to main memory
332 * as far as we know, so there's no chipset flush. It also doesn't
333 * land in the GPU render cache.
335 * However, we do have to enforce the order so that all writes through
336 * the GTT land before any writes to the device, such as updates to
339 * We also have to wait a bit for the writes to land from the GTT.
340 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
341 * timing. This issue has only been observed when switching quickly
342 * between GTT writes and CPU reads from inside the kernel on recent hw,
343 * and it appears to only affect discrete GTT blocks (i.e. on LLC
344 * system agents we cannot reproduce this behaviour, until Cannonlake
350 if (INTEL_INFO(gt->i915)->has_coherent_ggtt)
353 intel_gt_chipset_flush(gt);
355 with_intel_runtime_pm_if_in_use(uncore->rpm, wakeref) {
358 spin_lock_irqsave(&uncore->lock, flags);
359 intel_uncore_posting_read_fw(uncore,
360 RING_HEAD(RENDER_RING_BASE));
361 spin_unlock_irqrestore(&uncore->lock, flags);
365 void intel_gt_chipset_flush(struct intel_gt *gt)
368 if (GRAPHICS_VER(gt->i915) < 6)
369 intel_gtt_chipset_flush();
372 void intel_gt_driver_register(struct intel_gt *gt)
374 intel_rps_driver_register(>->rps);
376 debugfs_gt_register(gt);
379 static int intel_gt_init_scratch(struct intel_gt *gt, unsigned int size)
381 struct drm_i915_private *i915 = gt->i915;
382 struct drm_i915_gem_object *obj;
383 struct i915_vma *vma;
386 obj = i915_gem_object_create_lmem(i915, size, I915_BO_ALLOC_VOLATILE);
388 obj = i915_gem_object_create_stolen(i915, size);
390 obj = i915_gem_object_create_internal(i915, size);
392 drm_err(&i915->drm, "Failed to allocate scratch page\n");
396 vma = i915_vma_instance(obj, >->ggtt->vm, NULL);
402 ret = i915_ggtt_pin(vma, NULL, 0, PIN_HIGH);
406 gt->scratch = i915_vma_make_unshrinkable(vma);
411 i915_gem_object_put(obj);
415 static void intel_gt_fini_scratch(struct intel_gt *gt)
417 i915_vma_unpin_and_release(>->scratch, 0);
420 static struct i915_address_space *kernel_vm(struct intel_gt *gt)
422 if (INTEL_PPGTT(gt->i915) > INTEL_PPGTT_ALIASING)
423 return &i915_ppgtt_create(gt)->vm;
425 return i915_vm_get(>->ggtt->vm);
428 static int __engines_record_defaults(struct intel_gt *gt)
430 struct i915_request *requests[I915_NUM_ENGINES] = {};
431 struct intel_engine_cs *engine;
432 enum intel_engine_id id;
436 * As we reset the gpu during very early sanitisation, the current
437 * register state on the GPU should reflect its defaults values.
438 * We load a context onto the hw (with restore-inhibit), then switch
439 * over to a second context to save that default register state. We
440 * can then prime every new context with that state so they all start
441 * from the same default HW values.
444 for_each_engine(engine, gt, id) {
445 struct intel_renderstate so;
446 struct intel_context *ce;
447 struct i915_request *rq;
449 /* We must be able to switch to something! */
450 GEM_BUG_ON(!engine->kernel_context);
452 ce = intel_context_create(engine);
458 err = intel_renderstate_init(&so, ce);
462 rq = i915_request_create(ce);
468 err = intel_engine_emit_ctx_wa(rq);
472 err = intel_renderstate_emit(&so, rq);
477 requests[id] = i915_request_get(rq);
478 i915_request_add(rq);
480 intel_renderstate_fini(&so, ce);
483 intel_context_put(ce);
488 /* Flush the default context image to memory, and enable powersaving. */
489 if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
494 for (id = 0; id < ARRAY_SIZE(requests); id++) {
495 struct i915_request *rq;
502 if (rq->fence.error) {
507 GEM_BUG_ON(!test_bit(CONTEXT_ALLOC_BIT, &rq->context->flags));
508 if (!rq->context->state)
511 /* Keep a copy of the state's backing pages; free the obj */
512 state = shmem_create_from_object(rq->context->state->obj);
514 err = PTR_ERR(state);
517 rq->engine->default_state = state;
522 * If we have to abandon now, we expect the engines to be idle
523 * and ready to be torn-down. The quickest way we can accomplish
524 * this is by declaring ourselves wedged.
527 intel_gt_set_wedged(gt);
529 for (id = 0; id < ARRAY_SIZE(requests); id++) {
530 struct intel_context *ce;
531 struct i915_request *rq;
538 i915_request_put(rq);
539 intel_context_put(ce);
544 static int __engines_verify_workarounds(struct intel_gt *gt)
546 struct intel_engine_cs *engine;
547 enum intel_engine_id id;
550 if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
553 for_each_engine(engine, gt, id) {
554 if (intel_engine_verify_workarounds(engine, "load"))
558 /* Flush and restore the kernel context for safety */
559 if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME)
565 static void __intel_gt_disable(struct intel_gt *gt)
567 intel_gt_set_wedged_on_fini(gt);
569 intel_gt_suspend_prepare(gt);
570 intel_gt_suspend_late(gt);
572 GEM_BUG_ON(intel_gt_pm_is_awake(gt));
575 int intel_gt_init(struct intel_gt *gt)
579 err = i915_inject_probe_error(gt->i915, -ENODEV);
584 * This is just a security blanket to placate dragons.
585 * On some systems, we very sporadically observe that the first TLBs
586 * used by the CS may be stale, despite us poking the TLB reset. If
587 * we hold the forcewake during initialisation these problems
588 * just magically go away.
590 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
592 err = intel_gt_init_scratch(gt,
593 GRAPHICS_VER(gt->i915) == 2 ? SZ_256K : SZ_4K);
597 intel_gt_pm_init(gt);
599 gt->vm = kernel_vm(gt);
605 err = intel_engines_init(gt);
609 err = intel_uc_init(>->uc);
613 err = intel_gt_resume(gt);
617 err = __engines_record_defaults(gt);
621 err = __engines_verify_workarounds(gt);
625 err = i915_inject_probe_error(gt->i915, -EIO);
631 __intel_gt_disable(gt);
632 intel_uc_fini_hw(>->uc);
634 intel_uc_fini(>->uc);
636 intel_engines_release(gt);
637 i915_vm_put(fetch_and_zero(>->vm));
639 intel_gt_pm_fini(gt);
640 intel_gt_fini_scratch(gt);
643 intel_gt_set_wedged_on_init(gt);
644 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
648 void intel_gt_driver_remove(struct intel_gt *gt)
650 __intel_gt_disable(gt);
652 intel_uc_driver_remove(>->uc);
654 intel_engines_release(gt);
657 void intel_gt_driver_unregister(struct intel_gt *gt)
659 intel_wakeref_t wakeref;
661 intel_rps_driver_unregister(>->rps);
664 * Upon unregistering the device to prevent any new users, cancel
665 * all in-flight requests so that we can quickly unbind the active
668 intel_gt_set_wedged(gt);
670 /* Scrub all HW state upon release */
671 with_intel_runtime_pm(gt->uncore->rpm, wakeref)
672 __intel_gt_reset(gt, ALL_ENGINES);
675 void intel_gt_driver_release(struct intel_gt *gt)
677 struct i915_address_space *vm;
679 vm = fetch_and_zero(>->vm);
680 if (vm) /* FIXME being called twice on error paths :( */
683 intel_gt_pm_fini(gt);
684 intel_gt_fini_scratch(gt);
685 intel_gt_fini_buffer_pool(gt);
688 void intel_gt_driver_late_release(struct intel_gt *gt)
690 /* We need to wait for inflight RCU frees to release their grip */
693 intel_uc_driver_late_release(>->uc);
694 intel_gt_fini_requests(gt);
695 intel_gt_fini_reset(gt);
696 intel_gt_fini_timelines(gt);
697 intel_engines_free(gt);
700 void intel_gt_info_print(const struct intel_gt_info *info,
701 struct drm_printer *p)
703 drm_printf(p, "available engines: %x\n", info->engine_mask);
705 intel_sseu_dump(&info->sseu, p);