1 // SPDX-License-Identifier: MIT
3 * Copyright © 2019 Intel Corporation
6 #include "debugfs_gt.h"
8 #include "intel_context.h"
10 #include "intel_gt_pm.h"
11 #include "intel_gt_requests.h"
12 #include "intel_mocs.h"
13 #include "intel_rc6.h"
14 #include "intel_renderstate.h"
15 #include "intel_rps.h"
16 #include "intel_uncore.h"
19 void intel_gt_init_early(struct intel_gt
*gt
, struct drm_i915_private
*i915
)
22 gt
->uncore
= &i915
->uncore
;
24 spin_lock_init(>
->irq_lock
);
26 INIT_LIST_HEAD(>
->closed_vma
);
27 spin_lock_init(>
->closed_lock
);
29 intel_gt_init_reset(gt
);
30 intel_gt_init_requests(gt
);
31 intel_gt_init_timelines(gt
);
32 intel_gt_pm_init_early(gt
);
34 intel_rps_init_early(>
->rps
);
35 intel_uc_init_early(>
->uc
);
38 void intel_gt_init_hw_early(struct intel_gt
*gt
, struct i915_ggtt
*ggtt
)
43 static void init_unused_ring(struct intel_gt
*gt
, u32 base
)
45 struct intel_uncore
*uncore
= gt
->uncore
;
47 intel_uncore_write(uncore
, RING_CTL(base
), 0);
48 intel_uncore_write(uncore
, RING_HEAD(base
), 0);
49 intel_uncore_write(uncore
, RING_TAIL(base
), 0);
50 intel_uncore_write(uncore
, RING_START(base
), 0);
53 static void init_unused_rings(struct intel_gt
*gt
)
55 struct drm_i915_private
*i915
= gt
->i915
;
58 init_unused_ring(gt
, PRB1_BASE
);
59 init_unused_ring(gt
, SRB0_BASE
);
60 init_unused_ring(gt
, SRB1_BASE
);
61 init_unused_ring(gt
, SRB2_BASE
);
62 init_unused_ring(gt
, SRB3_BASE
);
63 } else if (IS_GEN(i915
, 2)) {
64 init_unused_ring(gt
, SRB0_BASE
);
65 init_unused_ring(gt
, SRB1_BASE
);
66 } else if (IS_GEN(i915
, 3)) {
67 init_unused_ring(gt
, PRB1_BASE
);
68 init_unused_ring(gt
, PRB2_BASE
);
72 int intel_gt_init_hw(struct intel_gt
*gt
)
74 struct drm_i915_private
*i915
= gt
->i915
;
75 struct intel_uncore
*uncore
= gt
->uncore
;
78 gt
->last_init_time
= ktime_get();
80 /* Double layer security blanket, see i915_gem_init() */
81 intel_uncore_forcewake_get(uncore
, FORCEWAKE_ALL
);
83 if (HAS_EDRAM(i915
) && INTEL_GEN(i915
) < 9)
84 intel_uncore_rmw(uncore
, HSW_IDICR
, 0, IDIHASHMSK(0xf));
87 intel_uncore_write(uncore
,
88 MI_PREDICATE_RESULT_2
,
90 LOWER_SLICE_ENABLED
: LOWER_SLICE_DISABLED
);
92 /* Apply the GT workarounds... */
93 intel_gt_apply_workarounds(gt
);
94 /* ...and determine whether they are sticking. */
95 intel_gt_verify_workarounds(gt
, "init");
97 intel_gt_init_swizzling(gt
);
100 * At least 830 can leave some of the unused rings
101 * "active" (ie. head != tail) after resume which
102 * will prevent c3 entry. Makes sure all unused rings
105 init_unused_rings(gt
);
107 ret
= i915_ppgtt_init_hw(gt
);
109 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret
);
113 /* We can't enable contexts until all firmware is loaded */
114 ret
= intel_uc_init_hw(>
->uc
);
116 i915_probe_error(i915
, "Enabling uc failed (%d)\n", ret
);
123 intel_uncore_forcewake_put(uncore
, FORCEWAKE_ALL
);
127 static void rmw_set(struct intel_uncore
*uncore
, i915_reg_t reg
, u32 set
)
129 intel_uncore_rmw(uncore
, reg
, 0, set
);
132 static void rmw_clear(struct intel_uncore
*uncore
, i915_reg_t reg
, u32 clr
)
134 intel_uncore_rmw(uncore
, reg
, clr
, 0);
137 static void clear_register(struct intel_uncore
*uncore
, i915_reg_t reg
)
139 intel_uncore_rmw(uncore
, reg
, 0, 0);
142 static void gen8_clear_engine_error_register(struct intel_engine_cs
*engine
)
144 GEN6_RING_FAULT_REG_RMW(engine
, RING_FAULT_VALID
, 0);
145 GEN6_RING_FAULT_REG_POSTING_READ(engine
);
149 intel_gt_clear_error_registers(struct intel_gt
*gt
,
150 intel_engine_mask_t engine_mask
)
152 struct drm_i915_private
*i915
= gt
->i915
;
153 struct intel_uncore
*uncore
= gt
->uncore
;
156 if (!IS_GEN(i915
, 2))
157 clear_register(uncore
, PGTBL_ER
);
159 if (INTEL_GEN(i915
) < 4)
160 clear_register(uncore
, IPEIR(RENDER_RING_BASE
));
162 clear_register(uncore
, IPEIR_I965
);
164 clear_register(uncore
, EIR
);
165 eir
= intel_uncore_read(uncore
, EIR
);
168 * some errors might have become stuck,
171 DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masking\n", eir
);
172 rmw_set(uncore
, EMR
, eir
);
173 intel_uncore_write(uncore
, GEN2_IIR
,
174 I915_MASTER_ERROR_INTERRUPT
);
177 if (INTEL_GEN(i915
) >= 12) {
178 rmw_clear(uncore
, GEN12_RING_FAULT_REG
, RING_FAULT_VALID
);
179 intel_uncore_posting_read(uncore
, GEN12_RING_FAULT_REG
);
180 } else if (INTEL_GEN(i915
) >= 8) {
181 rmw_clear(uncore
, GEN8_RING_FAULT_REG
, RING_FAULT_VALID
);
182 intel_uncore_posting_read(uncore
, GEN8_RING_FAULT_REG
);
183 } else if (INTEL_GEN(i915
) >= 6) {
184 struct intel_engine_cs
*engine
;
185 enum intel_engine_id id
;
187 for_each_engine_masked(engine
, gt
, engine_mask
, id
)
188 gen8_clear_engine_error_register(engine
);
192 static void gen6_check_faults(struct intel_gt
*gt
)
194 struct intel_engine_cs
*engine
;
195 enum intel_engine_id id
;
198 for_each_engine(engine
, gt
, id
) {
199 fault
= GEN6_RING_FAULT_REG_READ(engine
);
200 if (fault
& RING_FAULT_VALID
) {
201 DRM_DEBUG_DRIVER("Unexpected fault\n"
203 "\tAddress space: %s\n"
207 fault
& RING_FAULT_GTTSEL_MASK
?
209 RING_FAULT_SRCID(fault
),
210 RING_FAULT_FAULT_TYPE(fault
));
215 static void gen8_check_faults(struct intel_gt
*gt
)
217 struct intel_uncore
*uncore
= gt
->uncore
;
218 i915_reg_t fault_reg
, fault_data0_reg
, fault_data1_reg
;
221 if (INTEL_GEN(gt
->i915
) >= 12) {
222 fault_reg
= GEN12_RING_FAULT_REG
;
223 fault_data0_reg
= GEN12_FAULT_TLB_DATA0
;
224 fault_data1_reg
= GEN12_FAULT_TLB_DATA1
;
226 fault_reg
= GEN8_RING_FAULT_REG
;
227 fault_data0_reg
= GEN8_FAULT_TLB_DATA0
;
228 fault_data1_reg
= GEN8_FAULT_TLB_DATA1
;
231 fault
= intel_uncore_read(uncore
, fault_reg
);
232 if (fault
& RING_FAULT_VALID
) {
233 u32 fault_data0
, fault_data1
;
236 fault_data0
= intel_uncore_read(uncore
, fault_data0_reg
);
237 fault_data1
= intel_uncore_read(uncore
, fault_data1_reg
);
239 fault_addr
= ((u64
)(fault_data1
& FAULT_VA_HIGH_BITS
) << 44) |
240 ((u64
)fault_data0
<< 12);
242 DRM_DEBUG_DRIVER("Unexpected fault\n"
243 "\tAddr: 0x%08x_%08x\n"
244 "\tAddress space: %s\n"
248 upper_32_bits(fault_addr
),
249 lower_32_bits(fault_addr
),
250 fault_data1
& FAULT_GTT_SEL
? "GGTT" : "PPGTT",
251 GEN8_RING_FAULT_ENGINE_ID(fault
),
252 RING_FAULT_SRCID(fault
),
253 RING_FAULT_FAULT_TYPE(fault
));
257 void intel_gt_check_and_clear_faults(struct intel_gt
*gt
)
259 struct drm_i915_private
*i915
= gt
->i915
;
261 /* From GEN8 onwards we only have one 'All Engine Fault Register' */
262 if (INTEL_GEN(i915
) >= 8)
263 gen8_check_faults(gt
);
264 else if (INTEL_GEN(i915
) >= 6)
265 gen6_check_faults(gt
);
269 intel_gt_clear_error_registers(gt
, ALL_ENGINES
);
272 void intel_gt_flush_ggtt_writes(struct intel_gt
*gt
)
274 struct intel_uncore
*uncore
= gt
->uncore
;
275 intel_wakeref_t wakeref
;
278 * No actual flushing is required for the GTT write domain for reads
279 * from the GTT domain. Writes to it "immediately" go to main memory
280 * as far as we know, so there's no chipset flush. It also doesn't
281 * land in the GPU render cache.
283 * However, we do have to enforce the order so that all writes through
284 * the GTT land before any writes to the device, such as updates to
287 * We also have to wait a bit for the writes to land from the GTT.
288 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
289 * timing. This issue has only been observed when switching quickly
290 * between GTT writes and CPU reads from inside the kernel on recent hw,
291 * and it appears to only affect discrete GTT blocks (i.e. on LLC
292 * system agents we cannot reproduce this behaviour, until Cannonlake
298 if (INTEL_INFO(gt
->i915
)->has_coherent_ggtt
)
301 intel_gt_chipset_flush(gt
);
303 with_intel_runtime_pm_if_in_use(uncore
->rpm
, wakeref
) {
306 spin_lock_irqsave(&uncore
->lock
, flags
);
307 intel_uncore_posting_read_fw(uncore
,
308 RING_HEAD(RENDER_RING_BASE
));
309 spin_unlock_irqrestore(&uncore
->lock
, flags
);
313 void intel_gt_chipset_flush(struct intel_gt
*gt
)
316 if (INTEL_GEN(gt
->i915
) < 6)
317 intel_gtt_chipset_flush();
320 void intel_gt_driver_register(struct intel_gt
*gt
)
322 intel_rps_driver_register(>
->rps
);
324 debugfs_gt_register(gt
);
327 static int intel_gt_init_scratch(struct intel_gt
*gt
, unsigned int size
)
329 struct drm_i915_private
*i915
= gt
->i915
;
330 struct drm_i915_gem_object
*obj
;
331 struct i915_vma
*vma
;
334 obj
= i915_gem_object_create_stolen(i915
, size
);
336 obj
= i915_gem_object_create_internal(i915
, size
);
338 DRM_ERROR("Failed to allocate scratch page\n");
342 vma
= i915_vma_instance(obj
, >
->ggtt
->vm
, NULL
);
348 ret
= i915_vma_pin(vma
, 0, 0, PIN_GLOBAL
| PIN_HIGH
);
352 gt
->scratch
= i915_vma_make_unshrinkable(vma
);
357 i915_gem_object_put(obj
);
361 static void intel_gt_fini_scratch(struct intel_gt
*gt
)
363 i915_vma_unpin_and_release(>
->scratch
, 0);
366 static struct i915_address_space
*kernel_vm(struct intel_gt
*gt
)
368 if (INTEL_PPGTT(gt
->i915
) > INTEL_PPGTT_ALIASING
)
369 return &i915_ppgtt_create(gt
)->vm
;
371 return i915_vm_get(>
->ggtt
->vm
);
374 static int __intel_context_flush_retire(struct intel_context
*ce
)
376 struct intel_timeline
*tl
;
378 tl
= intel_context_timeline_lock(ce
);
382 intel_context_timeline_unlock(tl
);
386 static int __engines_record_defaults(struct intel_gt
*gt
)
388 struct i915_request
*requests
[I915_NUM_ENGINES
] = {};
389 struct intel_engine_cs
*engine
;
390 enum intel_engine_id id
;
394 * As we reset the gpu during very early sanitisation, the current
395 * register state on the GPU should reflect its defaults values.
396 * We load a context onto the hw (with restore-inhibit), then switch
397 * over to a second context to save that default register state. We
398 * can then prime every new context with that state so they all start
399 * from the same default HW values.
402 for_each_engine(engine
, gt
, id
) {
403 struct intel_renderstate so
;
404 struct intel_context
*ce
;
405 struct i915_request
*rq
;
407 /* We must be able to switch to something! */
408 GEM_BUG_ON(!engine
->kernel_context
);
410 err
= intel_renderstate_init(&so
, engine
);
414 ce
= intel_context_create(engine
);
420 rq
= intel_context_create_request(ce
);
423 intel_context_put(ce
);
427 err
= intel_engine_emit_ctx_wa(rq
);
431 err
= intel_renderstate_emit(&so
, rq
);
436 requests
[id
] = i915_request_get(rq
);
437 i915_request_add(rq
);
438 intel_renderstate_fini(&so
);
443 /* Flush the default context image to memory, and enable powersaving. */
444 if (intel_gt_wait_for_idle(gt
, I915_GEM_IDLE_TIMEOUT
) == -ETIME
) {
449 for (id
= 0; id
< ARRAY_SIZE(requests
); id
++) {
450 struct i915_request
*rq
;
451 struct i915_vma
*state
;
458 GEM_BUG_ON(!test_bit(CONTEXT_ALLOC_BIT
, &rq
->context
->flags
));
459 state
= rq
->context
->state
;
463 /* Serialise with retirement on another CPU */
464 GEM_BUG_ON(!i915_request_completed(rq
));
465 err
= __intel_context_flush_retire(rq
->context
);
469 /* We want to be able to unbind the state from the GGTT */
470 GEM_BUG_ON(intel_context_is_pinned(rq
->context
));
473 * As we will hold a reference to the logical state, it will
474 * not be torn down with the context, and importantly the
475 * object will hold onto its vma (making it possible for a
476 * stray GTT write to corrupt our defaults). Unmap the vma
477 * from the GTT to prevent such accidents and reclaim the
480 err
= i915_vma_unbind(state
);
484 i915_gem_object_lock(state
->obj
);
485 err
= i915_gem_object_set_to_cpu_domain(state
->obj
, false);
486 i915_gem_object_unlock(state
->obj
);
490 i915_gem_object_set_cache_coherency(state
->obj
, I915_CACHE_LLC
);
492 /* Check we can acquire the image of the context state */
493 vaddr
= i915_gem_object_pin_map(state
->obj
, I915_MAP_FORCE_WB
);
495 err
= PTR_ERR(vaddr
);
499 rq
->engine
->default_state
= i915_gem_object_get(state
->obj
);
500 i915_gem_object_unpin_map(state
->obj
);
505 * If we have to abandon now, we expect the engines to be idle
506 * and ready to be torn-down. The quickest way we can accomplish
507 * this is by declaring ourselves wedged.
510 intel_gt_set_wedged(gt
);
512 for (id
= 0; id
< ARRAY_SIZE(requests
); id
++) {
513 struct intel_context
*ce
;
514 struct i915_request
*rq
;
521 i915_request_put(rq
);
522 intel_context_put(ce
);
527 static int __engines_verify_workarounds(struct intel_gt
*gt
)
529 struct intel_engine_cs
*engine
;
530 enum intel_engine_id id
;
533 if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM
))
536 for_each_engine(engine
, gt
, id
) {
537 if (intel_engine_verify_workarounds(engine
, "load"))
544 static void __intel_gt_disable(struct intel_gt
*gt
)
546 intel_gt_set_wedged_on_init(gt
);
548 intel_gt_suspend_prepare(gt
);
549 intel_gt_suspend_late(gt
);
551 GEM_BUG_ON(intel_gt_pm_is_awake(gt
));
554 int intel_gt_init(struct intel_gt
*gt
)
558 err
= i915_inject_probe_error(gt
->i915
, -ENODEV
);
563 * This is just a security blanket to placate dragons.
564 * On some systems, we very sporadically observe that the first TLBs
565 * used by the CS may be stale, despite us poking the TLB reset. If
566 * we hold the forcewake during initialisation these problems
567 * just magically go away.
569 intel_uncore_forcewake_get(gt
->uncore
, FORCEWAKE_ALL
);
571 err
= intel_gt_init_scratch(gt
, IS_GEN(gt
->i915
, 2) ? SZ_256K
: SZ_4K
);
575 intel_gt_pm_init(gt
);
577 gt
->vm
= kernel_vm(gt
);
583 err
= intel_engines_init(gt
);
587 intel_uc_init(>
->uc
);
589 err
= intel_gt_resume(gt
);
593 err
= __engines_record_defaults(gt
);
597 err
= __engines_verify_workarounds(gt
);
601 err
= i915_inject_probe_error(gt
->i915
, -EIO
);
607 __intel_gt_disable(gt
);
608 intel_uc_fini_hw(>
->uc
);
610 intel_uc_fini(>
->uc
);
612 intel_engines_release(gt
);
613 i915_vm_put(fetch_and_zero(>
->vm
));
615 intel_gt_pm_fini(gt
);
616 intel_gt_fini_scratch(gt
);
619 intel_gt_set_wedged_on_init(gt
);
620 intel_uncore_forcewake_put(gt
->uncore
, FORCEWAKE_ALL
);
624 void intel_gt_driver_remove(struct intel_gt
*gt
)
626 __intel_gt_disable(gt
);
628 intel_uc_fini_hw(>
->uc
);
629 intel_uc_fini(>
->uc
);
631 intel_engines_release(gt
);
634 void intel_gt_driver_unregister(struct intel_gt
*gt
)
636 intel_rps_driver_unregister(>
->rps
);
639 void intel_gt_driver_release(struct intel_gt
*gt
)
641 struct i915_address_space
*vm
;
643 vm
= fetch_and_zero(>
->vm
);
644 if (vm
) /* FIXME being called twice on error paths :( */
647 intel_gt_pm_fini(gt
);
648 intel_gt_fini_scratch(gt
);
651 void intel_gt_driver_late_release(struct intel_gt
*gt
)
653 intel_uc_driver_late_release(>
->uc
);
654 intel_gt_fini_requests(gt
);
655 intel_gt_fini_reset(gt
);
656 intel_gt_fini_timelines(gt
);
657 intel_engines_free(gt
);