1 // SPDX-License-Identifier: MIT
3 * Copyright © 2019 Intel Corporation
6 #include <drm/drm_managed.h>
7 #include <drm/intel-gtt.h>
9 #include "gem/i915_gem_internal.h"
10 #include "gem/i915_gem_lmem.h"
11 #include "pxp/intel_pxp.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"
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"
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"
39 void intel_gt_common_init_early(struct intel_gt *gt)
41 spin_lock_init(gt->irq_lock);
43 INIT_LIST_HEAD(>->lmem_userfault_list);
44 mutex_init(>->lmem_userfault_lock);
45 INIT_LIST_HEAD(>->closed_vma);
46 spin_lock_init(>->closed_lock);
48 init_llist_head(>->watchdog.list);
49 INIT_WORK(>->watchdog.work, intel_gt_watchdog_work);
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(>->tlb.invalidate_lock);
56 seqcount_mutex_init(>->tlb.seqno, >->tlb.invalidate_lock);
57 intel_gt_pm_init_early(gt);
59 intel_uc_init_early(>->uc);
60 intel_rps_init_early(>->rps);
63 /* Preliminary initialization of Tile 0 */
64 int intel_root_gt_init_early(struct drm_i915_private *i915)
66 struct intel_gt *gt = to_gt(i915);
69 gt->uncore = &i915->uncore;
70 gt->irq_lock = drmm_kzalloc(&i915->drm, sizeof(*gt->irq_lock), GFP_KERNEL);
74 intel_gt_common_init_early(gt);
79 static int intel_gt_probe_lmem(struct intel_gt *gt)
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;
87 mem = intel_gt_setup_lmem(gt);
94 "Failed to setup region(%d) type=%d\n",
95 err, INTEL_MEMORY_LOCAL);
100 mem->instance = instance;
102 intel_memory_region_set_name(mem, "local%u", mem->instance);
104 GEM_BUG_ON(!HAS_REGION(i915, id));
105 GEM_BUG_ON(i915->mm.regions[id]);
106 i915->mm.regions[id] = mem;
111 int intel_gt_assign_ggtt(struct intel_gt *gt)
113 gt->ggtt = drmm_kzalloc(>->i915->drm, sizeof(*gt->ggtt), GFP_KERNEL);
115 return gt->ggtt ? 0 : -ENOMEM;
118 int intel_gt_init_mmio(struct intel_gt *gt)
120 intel_gt_init_clock_frequency(gt);
122 intel_uc_init_mmio(>->uc);
123 intel_sseu_info_init(gt);
124 intel_gt_mcr_init(gt);
126 return intel_engines_init_mmio(gt);
129 static void init_unused_ring(struct intel_gt *gt, u32 base)
131 struct intel_uncore *uncore = gt->uncore;
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);
139 static void init_unused_rings(struct intel_gt *gt)
141 struct drm_i915_private *i915 = gt->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);
158 int intel_gt_init_hw(struct intel_gt *gt)
160 struct drm_i915_private *i915 = gt->i915;
161 struct intel_uncore *uncore = gt->uncore;
164 gt->last_init_time = ktime_get();
166 /* Double layer security blanket, see i915_gem_init() */
167 intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
169 if (HAS_EDRAM(i915) && GRAPHICS_VER(i915) < 9)
170 intel_uncore_rmw(uncore, HSW_IDICR, 0, IDIHASHMSK(0xf));
172 if (IS_HASWELL(i915))
173 intel_uncore_write(uncore,
174 HSW_MI_PREDICATE_RESULT_2,
176 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
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");
183 intel_gt_init_swizzling(gt);
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
191 init_unused_rings(gt);
193 ret = i915_ppgtt_init_hw(gt);
195 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
199 /* We can't enable contexts until all firmware is loaded */
200 ret = intel_uc_init_hw(>->uc);
202 i915_probe_error(i915, "Enabling uc failed (%d)\n", ret);
209 intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
213 static void rmw_set(struct intel_uncore *uncore, i915_reg_t reg, u32 set)
215 intel_uncore_rmw(uncore, reg, 0, set);
218 static void rmw_clear(struct intel_uncore *uncore, i915_reg_t reg, u32 clr)
220 intel_uncore_rmw(uncore, reg, clr, 0);
223 static void clear_register(struct intel_uncore *uncore, i915_reg_t reg)
225 intel_uncore_rmw(uncore, reg, 0, 0);
228 static void gen6_clear_engine_error_register(struct intel_engine_cs *engine)
230 GEN6_RING_FAULT_REG_RMW(engine, RING_FAULT_VALID, 0);
231 GEN6_RING_FAULT_REG_POSTING_READ(engine);
235 intel_gt_clear_error_registers(struct intel_gt *gt,
236 intel_engine_mask_t engine_mask)
238 struct drm_i915_private *i915 = gt->i915;
239 struct intel_uncore *uncore = gt->uncore;
242 if (GRAPHICS_VER(i915) != 2)
243 clear_register(uncore, PGTBL_ER);
245 if (GRAPHICS_VER(i915) < 4)
246 clear_register(uncore, IPEIR(RENDER_RING_BASE));
248 clear_register(uncore, IPEIR_I965);
250 clear_register(uncore, EIR);
251 eir = intel_uncore_read(uncore, EIR);
254 * some errors might have become stuck,
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);
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;
273 for_each_engine_masked(engine, gt, engine_mask, id)
274 gen6_clear_engine_error_register(engine);
278 static void gen6_check_faults(struct intel_gt *gt)
280 struct intel_engine_cs *engine;
281 enum intel_engine_id id;
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"
289 "\tAddress space: %s\n"
293 fault & RING_FAULT_GTTSEL_MASK ?
295 RING_FAULT_SRCID(fault),
296 RING_FAULT_FAULT_TYPE(fault));
301 static void gen8_check_faults(struct intel_gt *gt)
303 struct intel_uncore *uncore = gt->uncore;
304 i915_reg_t fault_reg, fault_data0_reg, fault_data1_reg;
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;
312 fault_reg = GEN8_RING_FAULT_REG;
313 fault_data0_reg = GEN8_FAULT_TLB_DATA0;
314 fault_data1_reg = GEN8_FAULT_TLB_DATA1;
317 fault = intel_uncore_read(uncore, fault_reg);
318 if (fault & RING_FAULT_VALID) {
319 u32 fault_data0, fault_data1;
322 fault_data0 = intel_uncore_read(uncore, fault_data0_reg);
323 fault_data1 = intel_uncore_read(uncore, fault_data1_reg);
325 fault_addr = ((u64)(fault_data1 & FAULT_VA_HIGH_BITS) << 44) |
326 ((u64)fault_data0 << 12);
328 drm_dbg(&uncore->i915->drm, "Unexpected fault\n"
329 "\tAddr: 0x%08x_%08x\n"
330 "\tAddress space: %s\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));
342 void intel_gt_check_and_clear_faults(struct intel_gt *gt)
344 struct drm_i915_private *i915 = gt->i915;
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);
354 intel_gt_clear_error_registers(gt, ALL_ENGINES);
357 void intel_gt_flush_ggtt_writes(struct intel_gt *gt)
359 struct intel_uncore *uncore = gt->uncore;
360 intel_wakeref_t wakeref;
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.
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
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
383 if (INTEL_INFO(gt->i915)->has_coherent_ggtt)
386 intel_gt_chipset_flush(gt);
388 with_intel_runtime_pm_if_in_use(uncore->rpm, wakeref) {
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);
398 void intel_gt_chipset_flush(struct intel_gt *gt)
401 if (GRAPHICS_VER(gt->i915) < 6)
402 intel_ggtt_gmch_flush();
405 void intel_gt_driver_register(struct intel_gt *gt)
407 intel_gsc_init(>->gsc, gt->i915);
409 intel_rps_driver_register(>->rps);
411 intel_gt_debugfs_register(gt);
412 intel_gt_sysfs_register(gt);
415 static int intel_gt_init_scratch(struct intel_gt *gt, unsigned int size)
417 struct drm_i915_private *i915 = gt->i915;
418 struct drm_i915_gem_object *obj;
419 struct i915_vma *vma;
422 obj = i915_gem_object_create_lmem(i915, size,
423 I915_BO_ALLOC_VOLATILE |
424 I915_BO_ALLOC_GPU_ONLY);
426 obj = i915_gem_object_create_stolen(i915, size);
428 obj = i915_gem_object_create_internal(i915, size);
430 drm_err(&i915->drm, "Failed to allocate scratch page\n");
434 vma = i915_vma_instance(obj, >->ggtt->vm, NULL);
440 ret = i915_ggtt_pin(vma, NULL, 0, PIN_HIGH);
444 gt->scratch = i915_vma_make_unshrinkable(vma);
449 i915_gem_object_put(obj);
453 static void intel_gt_fini_scratch(struct intel_gt *gt)
455 i915_vma_unpin_and_release(>->scratch, 0);
458 static struct i915_address_space *kernel_vm(struct intel_gt *gt)
460 if (INTEL_PPGTT(gt->i915) > INTEL_PPGTT_ALIASING)
461 return &i915_ppgtt_create(gt, I915_BO_ALLOC_PM_EARLY)->vm;
463 return i915_vm_get(>->ggtt->vm);
466 static int __engines_record_defaults(struct intel_gt *gt)
468 struct i915_request *requests[I915_NUM_ENGINES] = {};
469 struct intel_engine_cs *engine;
470 enum intel_engine_id id;
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.
482 for_each_engine(engine, gt, id) {
483 struct intel_renderstate so;
484 struct intel_context *ce;
485 struct i915_request *rq;
487 /* We must be able to switch to something! */
488 GEM_BUG_ON(!engine->kernel_context);
490 ce = intel_context_create(engine);
496 err = intel_renderstate_init(&so, ce);
500 rq = i915_request_create(ce);
506 err = intel_engine_emit_ctx_wa(rq);
510 err = intel_renderstate_emit(&so, rq);
515 requests[id] = i915_request_get(rq);
516 i915_request_add(rq);
518 intel_renderstate_fini(&so, ce);
521 intel_context_put(ce);
526 /* Flush the default context image to memory, and enable powersaving. */
527 if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
532 for (id = 0; id < ARRAY_SIZE(requests); id++) {
533 struct i915_request *rq;
540 if (rq->fence.error) {
545 GEM_BUG_ON(!test_bit(CONTEXT_ALLOC_BIT, &rq->context->flags));
546 if (!rq->context->state)
549 /* Keep a copy of the state's backing pages; free the obj */
550 state = shmem_create_from_object(rq->context->state->obj);
552 err = PTR_ERR(state);
555 rq->engine->default_state = state;
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.
565 intel_gt_set_wedged(gt);
567 for (id = 0; id < ARRAY_SIZE(requests); id++) {
568 struct intel_context *ce;
569 struct i915_request *rq;
576 i915_request_put(rq);
577 intel_context_put(ce);
582 static int __engines_verify_workarounds(struct intel_gt *gt)
584 struct intel_engine_cs *engine;
585 enum intel_engine_id id;
588 if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
591 for_each_engine(engine, gt, id) {
592 if (intel_engine_verify_workarounds(engine, "load"))
596 /* Flush and restore the kernel context for safety */
597 if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME)
603 static void __intel_gt_disable(struct intel_gt *gt)
605 intel_gt_set_wedged_on_fini(gt);
607 intel_gt_suspend_prepare(gt);
608 intel_gt_suspend_late(gt);
610 GEM_BUG_ON(intel_gt_pm_is_awake(gt));
613 int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout)
615 long remaining_timeout;
617 /* If the device is asleep, we have no requests outstanding */
618 if (!intel_gt_pm_is_awake(gt))
621 while ((timeout = intel_gt_retire_requests_timeout(gt, timeout,
622 &remaining_timeout)) > 0) {
624 if (signal_pending(current))
628 return timeout ? timeout : intel_uc_wait_for_idle(>->uc,
632 int intel_gt_init(struct intel_gt *gt)
636 err = i915_inject_probe_error(gt->i915, -ENODEV);
640 intel_gt_init_workarounds(gt);
643 * This is just a security blanket to placate dragons.
644 * On some systems, we very sporadically observe that the first TLBs
645 * used by the CS may be stale, despite us poking the TLB reset. If
646 * we hold the forcewake during initialisation these problems
647 * just magically go away.
649 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
651 err = intel_gt_init_scratch(gt,
652 GRAPHICS_VER(gt->i915) == 2 ? SZ_256K : SZ_4K);
656 intel_gt_pm_init(gt);
658 gt->vm = kernel_vm(gt);
664 intel_set_mocs_index(gt);
666 err = intel_engines_init(gt);
670 err = intel_uc_init(>->uc);
674 err = intel_gt_resume(gt);
678 err = intel_gt_init_hwconfig(gt);
680 drm_err(>->i915->drm, "Failed to retrieve hwconfig table: %pe\n",
683 err = __engines_record_defaults(gt);
687 err = __engines_verify_workarounds(gt);
691 intel_uc_init_late(>->uc);
693 err = i915_inject_probe_error(gt->i915, -EIO);
697 intel_migrate_init(>->migrate, gt);
699 intel_pxp_init(>->pxp);
703 __intel_gt_disable(gt);
704 intel_uc_fini_hw(>->uc);
706 intel_uc_fini(>->uc);
708 intel_engines_release(gt);
709 i915_vm_put(fetch_and_zero(>->vm));
711 intel_gt_pm_fini(gt);
712 intel_gt_fini_scratch(gt);
715 intel_gt_set_wedged_on_init(gt);
716 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
720 void intel_gt_driver_remove(struct intel_gt *gt)
722 __intel_gt_disable(gt);
724 intel_migrate_fini(>->migrate);
725 intel_uc_driver_remove(>->uc);
727 intel_engines_release(gt);
729 intel_gt_flush_buffer_pool(gt);
732 void intel_gt_driver_unregister(struct intel_gt *gt)
734 intel_wakeref_t wakeref;
736 intel_gt_sysfs_unregister(gt);
737 intel_rps_driver_unregister(>->rps);
738 intel_gsc_fini(>->gsc);
740 intel_pxp_fini(>->pxp);
743 * Upon unregistering the device to prevent any new users, cancel
744 * all in-flight requests so that we can quickly unbind the active
747 intel_gt_set_wedged_on_fini(gt);
749 /* Scrub all HW state upon release */
750 with_intel_runtime_pm(gt->uncore->rpm, wakeref)
751 __intel_gt_reset(gt, ALL_ENGINES);
754 void intel_gt_driver_release(struct intel_gt *gt)
756 struct i915_address_space *vm;
758 vm = fetch_and_zero(>->vm);
759 if (vm) /* FIXME being called twice on error paths :( */
762 intel_wa_list_free(>->wa_list);
763 intel_gt_pm_fini(gt);
764 intel_gt_fini_scratch(gt);
765 intel_gt_fini_buffer_pool(gt);
766 intel_gt_fini_hwconfig(gt);
769 void intel_gt_driver_late_release_all(struct drm_i915_private *i915)
774 /* We need to wait for inflight RCU frees to release their grip */
777 for_each_gt(gt, i915, id) {
778 intel_uc_driver_late_release(>->uc);
779 intel_gt_fini_requests(gt);
780 intel_gt_fini_reset(gt);
781 intel_gt_fini_timelines(gt);
782 mutex_destroy(>->tlb.invalidate_lock);
783 intel_engines_free(gt);
787 static int intel_gt_tile_setup(struct intel_gt *gt, phys_addr_t phys_addr)
791 if (!gt_is_root(gt)) {
792 struct intel_uncore *uncore;
793 spinlock_t *irq_lock;
795 uncore = drmm_kzalloc(>->i915->drm, sizeof(*uncore), GFP_KERNEL);
799 irq_lock = drmm_kzalloc(>->i915->drm, sizeof(*irq_lock), GFP_KERNEL);
804 gt->irq_lock = irq_lock;
806 intel_gt_common_init_early(gt);
809 intel_uncore_init_early(gt->uncore, gt);
810 intel_wakeref_auto_init(>->userfault_wakeref, gt->uncore->rpm);
812 ret = intel_uncore_setup_mmio(gt->uncore, phys_addr);
816 gt->phys_addr = phys_addr;
821 int intel_gt_probe_all(struct drm_i915_private *i915)
823 struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
824 struct intel_gt *gt = &i915->gt0;
825 const struct intel_gt_definition *gtdef;
826 phys_addr_t phys_addr;
827 unsigned int mmio_bar;
831 mmio_bar = GRAPHICS_VER(i915) == 2 ? GEN2_GTTMMADR_BAR : GTTMMADR_BAR;
832 phys_addr = pci_resource_start(pdev, mmio_bar);
835 * We always have at least one primary GT on any device
836 * and it has been already initialized early during probe
837 * in i915_driver_probe()
840 gt->name = "Primary GT";
841 gt->info.engine_mask = RUNTIME_INFO(i915)->platform_engine_mask;
843 drm_dbg(&i915->drm, "Setting up %s\n", gt->name);
844 ret = intel_gt_tile_setup(gt, phys_addr);
850 if (!HAS_EXTRA_GT_LIST(i915))
853 for (i = 1, gtdef = &INTEL_INFO(i915)->extra_gt_list[i - 1];
855 i++, gtdef = &INTEL_INFO(i915)->extra_gt_list[i - 1]) {
856 gt = drmm_kzalloc(&i915->drm, sizeof(*gt), GFP_KERNEL);
863 gt->name = gtdef->name;
864 gt->type = gtdef->type;
865 gt->info.engine_mask = gtdef->engine_mask;
868 drm_dbg(&i915->drm, "Setting up %s\n", gt->name);
869 if (GEM_WARN_ON(range_overflows_t(resource_size_t,
872 pci_resource_len(pdev, mmio_bar)))) {
877 switch (gtdef->type) {
879 ret = intel_gt_tile_setup(gt, phys_addr + gtdef->mapping_base);
883 ret = intel_sa_mediagt_setup(gt, phys_addr + gtdef->mapping_base,
888 /* Primary GT should not appear in extra GT list */
890 MISSING_CASE(gtdef->type);
903 i915_probe_error(i915, "Failed to initialize %s! (%d)\n", gtdef->name, ret);
904 intel_gt_release_all(i915);
909 int intel_gt_tiles_init(struct drm_i915_private *i915)
915 for_each_gt(gt, i915, id) {
916 ret = intel_gt_probe_lmem(gt);
924 void intel_gt_release_all(struct drm_i915_private *i915)
929 for_each_gt(gt, i915, id)
933 void intel_gt_info_print(const struct intel_gt_info *info,
934 struct drm_printer *p)
936 drm_printf(p, "available engines: %x\n", info->engine_mask);
938 intel_sseu_dump(&info->sseu, p);
946 static struct reg_and_bit
947 get_reg_and_bit(const struct intel_engine_cs *engine, const bool gen8,
948 const i915_reg_t *regs, const unsigned int num)
950 const unsigned int class = engine->class;
951 struct reg_and_bit rb = { };
953 if (drm_WARN_ON_ONCE(&engine->i915->drm,
954 class >= num || !regs[class].reg))
957 rb.reg = regs[class];
958 if (gen8 && class == VIDEO_DECODE_CLASS)
959 rb.reg.reg += 4 * engine->instance; /* GEN8_M2TCR */
961 rb.bit = engine->instance;
963 rb.bit = BIT(rb.bit);
968 static void mmio_invalidate_full(struct intel_gt *gt)
970 static const i915_reg_t gen8_regs[] = {
971 [RENDER_CLASS] = GEN8_RTCR,
972 [VIDEO_DECODE_CLASS] = GEN8_M1TCR, /* , GEN8_M2TCR */
973 [VIDEO_ENHANCEMENT_CLASS] = GEN8_VTCR,
974 [COPY_ENGINE_CLASS] = GEN8_BTCR,
976 static const i915_reg_t gen12_regs[] = {
977 [RENDER_CLASS] = GEN12_GFX_TLB_INV_CR,
978 [VIDEO_DECODE_CLASS] = GEN12_VD_TLB_INV_CR,
979 [VIDEO_ENHANCEMENT_CLASS] = GEN12_VE_TLB_INV_CR,
980 [COPY_ENGINE_CLASS] = GEN12_BLT_TLB_INV_CR,
981 [COMPUTE_CLASS] = GEN12_COMPCTX_TLB_INV_CR,
983 struct drm_i915_private *i915 = gt->i915;
984 struct intel_uncore *uncore = gt->uncore;
985 struct intel_engine_cs *engine;
986 intel_engine_mask_t awake, tmp;
987 enum intel_engine_id id;
988 const i915_reg_t *regs;
989 unsigned int num = 0;
991 if (GRAPHICS_VER(i915) == 12) {
993 num = ARRAY_SIZE(gen12_regs);
994 } else if (GRAPHICS_VER(i915) >= 8 && GRAPHICS_VER(i915) <= 11) {
996 num = ARRAY_SIZE(gen8_regs);
997 } else if (GRAPHICS_VER(i915) < 8) {
1001 if (drm_WARN_ONCE(&i915->drm, !num,
1002 "Platform does not implement TLB invalidation!"))
1005 intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
1007 spin_lock_irq(&uncore->lock); /* serialise invalidate with GT reset */
1010 for_each_engine(engine, gt, id) {
1011 struct reg_and_bit rb;
1013 if (!intel_engine_pm_is_awake(engine))
1016 rb = get_reg_and_bit(engine, regs == gen8_regs, regs, num);
1017 if (!i915_mmio_reg_offset(rb.reg))
1020 intel_uncore_write_fw(uncore, rb.reg, rb.bit);
1021 awake |= engine->mask;
1024 GT_TRACE(gt, "invalidated engines %08x\n", awake);
1026 /* Wa_2207587034:tgl,dg1,rkl,adl-s,adl-p */
1028 (IS_TIGERLAKE(i915) ||
1030 IS_ROCKETLAKE(i915) ||
1031 IS_ALDERLAKE_S(i915) ||
1032 IS_ALDERLAKE_P(i915)))
1033 intel_uncore_write_fw(uncore, GEN12_OA_TLB_INV_CR, 1);
1035 spin_unlock_irq(&uncore->lock);
1037 for_each_engine_masked(engine, gt, awake, tmp) {
1038 struct reg_and_bit rb;
1041 * HW architecture suggest typical invalidation time at 40us,
1042 * with pessimistic cases up to 100us and a recommendation to
1043 * cap at 1ms. We go a bit higher just in case.
1045 const unsigned int timeout_us = 100;
1046 const unsigned int timeout_ms = 4;
1048 rb = get_reg_and_bit(engine, regs == gen8_regs, regs, num);
1049 if (__intel_wait_for_register_fw(uncore,
1051 timeout_us, timeout_ms,
1053 drm_err_ratelimited(>->i915->drm,
1054 "%s TLB invalidation did not complete in %ums!\n",
1055 engine->name, timeout_ms);
1059 * Use delayed put since a) we mostly expect a flurry of TLB
1060 * invalidations so it is good to avoid paying the forcewake cost and
1061 * b) it works around a bug in Icelake which cannot cope with too rapid
1064 intel_uncore_forcewake_put_delayed(uncore, FORCEWAKE_ALL);
1067 static bool tlb_seqno_passed(const struct intel_gt *gt, u32 seqno)
1069 u32 cur = intel_gt_tlb_seqno(gt);
1071 /* Only skip if a *full* TLB invalidate barrier has passed */
1072 return (s32)(cur - ALIGN(seqno, 2)) > 0;
1075 void intel_gt_invalidate_tlb(struct intel_gt *gt, u32 seqno)
1077 intel_wakeref_t wakeref;
1079 if (I915_SELFTEST_ONLY(gt->awake == -ENODEV))
1082 if (intel_gt_is_wedged(gt))
1085 if (tlb_seqno_passed(gt, seqno))
1088 with_intel_gt_pm_if_awake(gt, wakeref) {
1089 mutex_lock(>->tlb.invalidate_lock);
1090 if (tlb_seqno_passed(gt, seqno))
1093 mmio_invalidate_full(gt);
1095 write_seqcount_invalidate(>->tlb.seqno);
1097 mutex_unlock(>->tlb.invalidate_lock);