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;
71 mem->type = INTEL_MEMORY_LOCAL;
74 intel_memory_region_set_name(mem, "local%u", mem->instance);
76 GEM_BUG_ON(!HAS_REGION(i915, id));
77 GEM_BUG_ON(i915->mm.regions[id]);
78 i915->mm.regions[id] = mem;
83 void intel_gt_init_hw_early(struct intel_gt *gt, struct i915_ggtt *ggtt)
88 int intel_gt_init_mmio(struct intel_gt *gt)
90 intel_gt_init_clock_frequency(gt);
92 intel_uc_init_mmio(>->uc);
93 intel_sseu_info_init(gt);
95 return intel_engines_init_mmio(gt);
98 static void init_unused_ring(struct intel_gt *gt, u32 base)
100 struct intel_uncore *uncore = gt->uncore;
102 intel_uncore_write(uncore, RING_CTL(base), 0);
103 intel_uncore_write(uncore, RING_HEAD(base), 0);
104 intel_uncore_write(uncore, RING_TAIL(base), 0);
105 intel_uncore_write(uncore, RING_START(base), 0);
108 static void init_unused_rings(struct intel_gt *gt)
110 struct drm_i915_private *i915 = gt->i915;
113 init_unused_ring(gt, PRB1_BASE);
114 init_unused_ring(gt, SRB0_BASE);
115 init_unused_ring(gt, SRB1_BASE);
116 init_unused_ring(gt, SRB2_BASE);
117 init_unused_ring(gt, SRB3_BASE);
118 } else if (IS_GEN(i915, 2)) {
119 init_unused_ring(gt, SRB0_BASE);
120 init_unused_ring(gt, SRB1_BASE);
121 } else if (IS_GEN(i915, 3)) {
122 init_unused_ring(gt, PRB1_BASE);
123 init_unused_ring(gt, PRB2_BASE);
127 int intel_gt_init_hw(struct intel_gt *gt)
129 struct drm_i915_private *i915 = gt->i915;
130 struct intel_uncore *uncore = gt->uncore;
133 gt->last_init_time = ktime_get();
135 /* Double layer security blanket, see i915_gem_init() */
136 intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
138 if (HAS_EDRAM(i915) && INTEL_GEN(i915) < 9)
139 intel_uncore_rmw(uncore, HSW_IDICR, 0, IDIHASHMSK(0xf));
141 if (IS_HASWELL(i915))
142 intel_uncore_write(uncore,
143 MI_PREDICATE_RESULT_2,
145 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
147 /* Apply the GT workarounds... */
148 intel_gt_apply_workarounds(gt);
149 /* ...and determine whether they are sticking. */
150 intel_gt_verify_workarounds(gt, "init");
152 intel_gt_init_swizzling(gt);
155 * At least 830 can leave some of the unused rings
156 * "active" (ie. head != tail) after resume which
157 * will prevent c3 entry. Makes sure all unused rings
160 init_unused_rings(gt);
162 ret = i915_ppgtt_init_hw(gt);
164 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
168 /* We can't enable contexts until all firmware is loaded */
169 ret = intel_uc_init_hw(>->uc);
171 i915_probe_error(i915, "Enabling uc failed (%d)\n", ret);
178 intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
182 static void rmw_set(struct intel_uncore *uncore, i915_reg_t reg, u32 set)
184 intel_uncore_rmw(uncore, reg, 0, set);
187 static void rmw_clear(struct intel_uncore *uncore, i915_reg_t reg, u32 clr)
189 intel_uncore_rmw(uncore, reg, clr, 0);
192 static void clear_register(struct intel_uncore *uncore, i915_reg_t reg)
194 intel_uncore_rmw(uncore, reg, 0, 0);
197 static void gen8_clear_engine_error_register(struct intel_engine_cs *engine)
199 GEN6_RING_FAULT_REG_RMW(engine, RING_FAULT_VALID, 0);
200 GEN6_RING_FAULT_REG_POSTING_READ(engine);
204 intel_gt_clear_error_registers(struct intel_gt *gt,
205 intel_engine_mask_t engine_mask)
207 struct drm_i915_private *i915 = gt->i915;
208 struct intel_uncore *uncore = gt->uncore;
211 if (!IS_GEN(i915, 2))
212 clear_register(uncore, PGTBL_ER);
214 if (INTEL_GEN(i915) < 4)
215 clear_register(uncore, IPEIR(RENDER_RING_BASE));
217 clear_register(uncore, IPEIR_I965);
219 clear_register(uncore, EIR);
220 eir = intel_uncore_read(uncore, EIR);
223 * some errors might have become stuck,
226 DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masking\n", eir);
227 rmw_set(uncore, EMR, eir);
228 intel_uncore_write(uncore, GEN2_IIR,
229 I915_MASTER_ERROR_INTERRUPT);
232 if (INTEL_GEN(i915) >= 12) {
233 rmw_clear(uncore, GEN12_RING_FAULT_REG, RING_FAULT_VALID);
234 intel_uncore_posting_read(uncore, GEN12_RING_FAULT_REG);
235 } else if (INTEL_GEN(i915) >= 8) {
236 rmw_clear(uncore, GEN8_RING_FAULT_REG, RING_FAULT_VALID);
237 intel_uncore_posting_read(uncore, GEN8_RING_FAULT_REG);
238 } else if (INTEL_GEN(i915) >= 6) {
239 struct intel_engine_cs *engine;
240 enum intel_engine_id id;
242 for_each_engine_masked(engine, gt, engine_mask, id)
243 gen8_clear_engine_error_register(engine);
247 static void gen6_check_faults(struct intel_gt *gt)
249 struct intel_engine_cs *engine;
250 enum intel_engine_id id;
253 for_each_engine(engine, gt, id) {
254 fault = GEN6_RING_FAULT_REG_READ(engine);
255 if (fault & RING_FAULT_VALID) {
256 drm_dbg(&engine->i915->drm, "Unexpected fault\n"
258 "\tAddress space: %s\n"
262 fault & RING_FAULT_GTTSEL_MASK ?
264 RING_FAULT_SRCID(fault),
265 RING_FAULT_FAULT_TYPE(fault));
270 static void gen8_check_faults(struct intel_gt *gt)
272 struct intel_uncore *uncore = gt->uncore;
273 i915_reg_t fault_reg, fault_data0_reg, fault_data1_reg;
276 if (INTEL_GEN(gt->i915) >= 12) {
277 fault_reg = GEN12_RING_FAULT_REG;
278 fault_data0_reg = GEN12_FAULT_TLB_DATA0;
279 fault_data1_reg = GEN12_FAULT_TLB_DATA1;
281 fault_reg = GEN8_RING_FAULT_REG;
282 fault_data0_reg = GEN8_FAULT_TLB_DATA0;
283 fault_data1_reg = GEN8_FAULT_TLB_DATA1;
286 fault = intel_uncore_read(uncore, fault_reg);
287 if (fault & RING_FAULT_VALID) {
288 u32 fault_data0, fault_data1;
291 fault_data0 = intel_uncore_read(uncore, fault_data0_reg);
292 fault_data1 = intel_uncore_read(uncore, fault_data1_reg);
294 fault_addr = ((u64)(fault_data1 & FAULT_VA_HIGH_BITS) << 44) |
295 ((u64)fault_data0 << 12);
297 drm_dbg(&uncore->i915->drm, "Unexpected fault\n"
298 "\tAddr: 0x%08x_%08x\n"
299 "\tAddress space: %s\n"
303 upper_32_bits(fault_addr), lower_32_bits(fault_addr),
304 fault_data1 & FAULT_GTT_SEL ? "GGTT" : "PPGTT",
305 GEN8_RING_FAULT_ENGINE_ID(fault),
306 RING_FAULT_SRCID(fault),
307 RING_FAULT_FAULT_TYPE(fault));
311 void intel_gt_check_and_clear_faults(struct intel_gt *gt)
313 struct drm_i915_private *i915 = gt->i915;
315 /* From GEN8 onwards we only have one 'All Engine Fault Register' */
316 if (INTEL_GEN(i915) >= 8)
317 gen8_check_faults(gt);
318 else if (INTEL_GEN(i915) >= 6)
319 gen6_check_faults(gt);
323 intel_gt_clear_error_registers(gt, ALL_ENGINES);
326 void intel_gt_flush_ggtt_writes(struct intel_gt *gt)
328 struct intel_uncore *uncore = gt->uncore;
329 intel_wakeref_t wakeref;
332 * No actual flushing is required for the GTT write domain for reads
333 * from the GTT domain. Writes to it "immediately" go to main memory
334 * as far as we know, so there's no chipset flush. It also doesn't
335 * land in the GPU render cache.
337 * However, we do have to enforce the order so that all writes through
338 * the GTT land before any writes to the device, such as updates to
341 * We also have to wait a bit for the writes to land from the GTT.
342 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
343 * timing. This issue has only been observed when switching quickly
344 * between GTT writes and CPU reads from inside the kernel on recent hw,
345 * and it appears to only affect discrete GTT blocks (i.e. on LLC
346 * system agents we cannot reproduce this behaviour, until Cannonlake
352 if (INTEL_INFO(gt->i915)->has_coherent_ggtt)
355 intel_gt_chipset_flush(gt);
357 with_intel_runtime_pm_if_in_use(uncore->rpm, wakeref) {
360 spin_lock_irqsave(&uncore->lock, flags);
361 intel_uncore_posting_read_fw(uncore,
362 RING_HEAD(RENDER_RING_BASE));
363 spin_unlock_irqrestore(&uncore->lock, flags);
367 void intel_gt_chipset_flush(struct intel_gt *gt)
370 if (INTEL_GEN(gt->i915) < 6)
371 intel_gtt_chipset_flush();
374 void intel_gt_driver_register(struct intel_gt *gt)
376 intel_rps_driver_register(>->rps);
378 debugfs_gt_register(gt);
381 static int intel_gt_init_scratch(struct intel_gt *gt, unsigned int size)
383 struct drm_i915_private *i915 = gt->i915;
384 struct drm_i915_gem_object *obj;
385 struct i915_vma *vma;
388 obj = i915_gem_object_create_lmem(i915, size, I915_BO_ALLOC_VOLATILE);
390 obj = i915_gem_object_create_stolen(i915, size);
392 obj = i915_gem_object_create_internal(i915, size);
394 drm_err(&i915->drm, "Failed to allocate scratch page\n");
398 vma = i915_vma_instance(obj, >->ggtt->vm, NULL);
404 ret = i915_ggtt_pin(vma, NULL, 0, PIN_HIGH);
408 gt->scratch = i915_vma_make_unshrinkable(vma);
413 i915_gem_object_put(obj);
417 static void intel_gt_fini_scratch(struct intel_gt *gt)
419 i915_vma_unpin_and_release(>->scratch, 0);
422 static struct i915_address_space *kernel_vm(struct intel_gt *gt)
424 if (INTEL_PPGTT(gt->i915) > INTEL_PPGTT_ALIASING)
425 return &i915_ppgtt_create(gt)->vm;
427 return i915_vm_get(>->ggtt->vm);
430 static int __engines_record_defaults(struct intel_gt *gt)
432 struct i915_request *requests[I915_NUM_ENGINES] = {};
433 struct intel_engine_cs *engine;
434 enum intel_engine_id id;
438 * As we reset the gpu during very early sanitisation, the current
439 * register state on the GPU should reflect its defaults values.
440 * We load a context onto the hw (with restore-inhibit), then switch
441 * over to a second context to save that default register state. We
442 * can then prime every new context with that state so they all start
443 * from the same default HW values.
446 for_each_engine(engine, gt, id) {
447 struct intel_renderstate so;
448 struct intel_context *ce;
449 struct i915_request *rq;
451 /* We must be able to switch to something! */
452 GEM_BUG_ON(!engine->kernel_context);
454 ce = intel_context_create(engine);
460 err = intel_renderstate_init(&so, ce);
464 rq = i915_request_create(ce);
470 err = intel_engine_emit_ctx_wa(rq);
474 err = intel_renderstate_emit(&so, rq);
479 requests[id] = i915_request_get(rq);
480 i915_request_add(rq);
482 intel_renderstate_fini(&so, ce);
485 intel_context_put(ce);
490 /* Flush the default context image to memory, and enable powersaving. */
491 if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
496 for (id = 0; id < ARRAY_SIZE(requests); id++) {
497 struct i915_request *rq;
504 if (rq->fence.error) {
509 GEM_BUG_ON(!test_bit(CONTEXT_ALLOC_BIT, &rq->context->flags));
510 if (!rq->context->state)
513 /* Keep a copy of the state's backing pages; free the obj */
514 state = shmem_create_from_object(rq->context->state->obj);
516 err = PTR_ERR(state);
519 rq->engine->default_state = state;
524 * If we have to abandon now, we expect the engines to be idle
525 * and ready to be torn-down. The quickest way we can accomplish
526 * this is by declaring ourselves wedged.
529 intel_gt_set_wedged(gt);
531 for (id = 0; id < ARRAY_SIZE(requests); id++) {
532 struct intel_context *ce;
533 struct i915_request *rq;
540 i915_request_put(rq);
541 intel_context_put(ce);
546 static int __engines_verify_workarounds(struct intel_gt *gt)
548 struct intel_engine_cs *engine;
549 enum intel_engine_id id;
552 if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
555 for_each_engine(engine, gt, id) {
556 if (intel_engine_verify_workarounds(engine, "load"))
560 /* Flush and restore the kernel context for safety */
561 if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME)
567 static void __intel_gt_disable(struct intel_gt *gt)
569 intel_gt_set_wedged_on_fini(gt);
571 intel_gt_suspend_prepare(gt);
572 intel_gt_suspend_late(gt);
574 GEM_BUG_ON(intel_gt_pm_is_awake(gt));
577 int intel_gt_init(struct intel_gt *gt)
581 err = i915_inject_probe_error(gt->i915, -ENODEV);
586 * This is just a security blanket to placate dragons.
587 * On some systems, we very sporadically observe that the first TLBs
588 * used by the CS may be stale, despite us poking the TLB reset. If
589 * we hold the forcewake during initialisation these problems
590 * just magically go away.
592 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
594 err = intel_gt_init_scratch(gt, IS_GEN(gt->i915, 2) ? SZ_256K : SZ_4K);
598 intel_gt_pm_init(gt);
600 gt->vm = kernel_vm(gt);
606 err = intel_engines_init(gt);
610 err = intel_uc_init(>->uc);
614 err = intel_gt_resume(gt);
618 err = __engines_record_defaults(gt);
622 err = __engines_verify_workarounds(gt);
626 err = i915_inject_probe_error(gt->i915, -EIO);
632 __intel_gt_disable(gt);
633 intel_uc_fini_hw(>->uc);
635 intel_uc_fini(>->uc);
637 intel_engines_release(gt);
638 i915_vm_put(fetch_and_zero(>->vm));
640 intel_gt_pm_fini(gt);
641 intel_gt_fini_scratch(gt);
644 intel_gt_set_wedged_on_init(gt);
645 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
649 void intel_gt_driver_remove(struct intel_gt *gt)
651 __intel_gt_disable(gt);
653 intel_uc_driver_remove(>->uc);
655 intel_engines_release(gt);
658 void intel_gt_driver_unregister(struct intel_gt *gt)
660 intel_wakeref_t wakeref;
662 intel_rps_driver_unregister(>->rps);
665 * Upon unregistering the device to prevent any new users, cancel
666 * all in-flight requests so that we can quickly unbind the active
669 intel_gt_set_wedged(gt);
671 /* Scrub all HW state upon release */
672 with_intel_runtime_pm(gt->uncore->rpm, wakeref)
673 __intel_gt_reset(gt, ALL_ENGINES);
676 void intel_gt_driver_release(struct intel_gt *gt)
678 struct i915_address_space *vm;
680 vm = fetch_and_zero(>->vm);
681 if (vm) /* FIXME being called twice on error paths :( */
684 intel_gt_pm_fini(gt);
685 intel_gt_fini_scratch(gt);
686 intel_gt_fini_buffer_pool(gt);
689 void intel_gt_driver_late_release(struct intel_gt *gt)
691 /* We need to wait for inflight RCU frees to release their grip */
694 intel_uc_driver_late_release(>->uc);
695 intel_gt_fini_requests(gt);
696 intel_gt_fini_reset(gt);
697 intel_gt_fini_timelines(gt);
698 intel_engines_free(gt);
701 void intel_gt_info_print(const struct intel_gt_info *info,
702 struct drm_printer *p)
704 drm_printf(p, "available engines: %x\n", info->engine_mask);
706 intel_sseu_dump(&info->sseu, p);