1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2019 Intel Corporation
4 */
5
6 #include "debugfs_gt.h"
7
8 #include "gem/i915_gem_lmem.h"
9 #include "i915_drv.h"
10 #include "intel_context.h"
11 #include "intel_gt.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_migrate.h"
17 #include "intel_mocs.h"
18 #include "intel_rc6.h"
19 #include "intel_renderstate.h"
20 #include "intel_rps.h"
21 #include "intel_uncore.h"
22 #include "intel_pm.h"
23 #include "shmem_utils.h"
24
intel_gt_init_early(struct intel_gt * gt,struct drm_i915_private * i915)25 void intel_gt_init_early(struct intel_gt *gt, struct drm_i915_private *i915)
26 {
27 gt->i915 = i915;
28 gt->uncore = &i915->uncore;
29
30 spin_lock_init(>->irq_lock);
31
32 mutex_init(>->tlb_invalidate_lock);
33
34 INIT_LIST_HEAD(>->closed_vma);
35 spin_lock_init(>->closed_lock);
36
37 init_llist_head(>->watchdog.list);
38 INIT_WORK(>->watchdog.work, intel_gt_watchdog_work);
39
40 intel_gt_init_buffer_pool(gt);
41 intel_gt_init_reset(gt);
42 intel_gt_init_requests(gt);
43 intel_gt_init_timelines(gt);
44 intel_gt_pm_init_early(gt);
45
46 intel_uc_init_early(>->uc);
47 intel_rps_init_early(>->rps);
48 }
49
intel_gt_probe_lmem(struct intel_gt * gt)50 int intel_gt_probe_lmem(struct intel_gt *gt)
51 {
52 struct drm_i915_private *i915 = gt->i915;
53 struct intel_memory_region *mem;
54 int id;
55 int err;
56
57 mem = intel_gt_setup_lmem(gt);
58 if (mem == ERR_PTR(-ENODEV))
59 mem = intel_gt_setup_fake_lmem(gt);
60 if (IS_ERR(mem)) {
61 err = PTR_ERR(mem);
62 if (err == -ENODEV)
63 return 0;
64
65 drm_err(&i915->drm,
66 "Failed to setup region(%d) type=%d\n",
67 err, INTEL_MEMORY_LOCAL);
68 return err;
69 }
70
71 id = INTEL_REGION_LMEM;
72
73 mem->id = id;
74
75 intel_memory_region_set_name(mem, "local%u", mem->instance);
76
77 GEM_BUG_ON(!HAS_REGION(i915, id));
78 GEM_BUG_ON(i915->mm.regions[id]);
79 i915->mm.regions[id] = mem;
80
81 return 0;
82 }
83
intel_gt_init_hw_early(struct intel_gt * gt,struct i915_ggtt * ggtt)84 void intel_gt_init_hw_early(struct intel_gt *gt, struct i915_ggtt *ggtt)
85 {
86 gt->ggtt = ggtt;
87 }
88
89 static const struct intel_mmio_range icl_l3bank_steering_table[] = {
90 { 0x00B100, 0x00B3FF },
91 {},
92 };
93
94 static const struct intel_mmio_range xehpsdv_mslice_steering_table[] = {
95 { 0x004000, 0x004AFF },
96 { 0x00C800, 0x00CFFF },
97 { 0x00DD00, 0x00DDFF },
98 { 0x00E900, 0x00FFFF }, /* 0xEA00 - OxEFFF is unused */
99 {},
100 };
101
102 static const struct intel_mmio_range xehpsdv_lncf_steering_table[] = {
103 { 0x00B000, 0x00B0FF },
104 { 0x00D800, 0x00D8FF },
105 {},
106 };
107
108 static const struct intel_mmio_range dg2_lncf_steering_table[] = {
109 { 0x00B000, 0x00B0FF },
110 { 0x00D880, 0x00D8FF },
111 {},
112 };
113
slicemask(struct intel_gt * gt,int count)114 static u16 slicemask(struct intel_gt *gt, int count)
115 {
116 u64 dss_mask = intel_sseu_get_subslices(>->info.sseu, 0);
117
118 return intel_slicemask_from_dssmask(dss_mask, count);
119 }
120
intel_gt_init_mmio(struct intel_gt * gt)121 int intel_gt_init_mmio(struct intel_gt *gt)
122 {
123 struct drm_i915_private *i915 = gt->i915;
124
125 intel_gt_init_clock_frequency(gt);
126
127 intel_uc_init_mmio(>->uc);
128 intel_sseu_info_init(gt);
129
130 /*
131 * An mslice is unavailable only if both the meml3 for the slice is
132 * disabled *and* all of the DSS in the slice (quadrant) are disabled.
133 */
134 if (HAS_MSLICES(i915))
135 gt->info.mslice_mask =
136 slicemask(gt, GEN_DSS_PER_MSLICE) |
137 (intel_uncore_read(gt->uncore, GEN10_MIRROR_FUSE3) &
138 GEN12_MEML3_EN_MASK);
139
140 if (IS_DG2(i915)) {
141 gt->steering_table[MSLICE] = xehpsdv_mslice_steering_table;
142 gt->steering_table[LNCF] = dg2_lncf_steering_table;
143 } else if (IS_XEHPSDV(i915)) {
144 gt->steering_table[MSLICE] = xehpsdv_mslice_steering_table;
145 gt->steering_table[LNCF] = xehpsdv_lncf_steering_table;
146 } else if (GRAPHICS_VER(i915) >= 11 &&
147 GRAPHICS_VER_FULL(i915) < IP_VER(12, 50)) {
148 gt->steering_table[L3BANK] = icl_l3bank_steering_table;
149 gt->info.l3bank_mask =
150 ~intel_uncore_read(gt->uncore, GEN10_MIRROR_FUSE3) &
151 GEN10_L3BANK_MASK;
152 } else if (HAS_MSLICES(i915)) {
153 MISSING_CASE(INTEL_INFO(i915)->platform);
154 }
155
156 return intel_engines_init_mmio(gt);
157 }
158
init_unused_ring(struct intel_gt * gt,u32 base)159 static void init_unused_ring(struct intel_gt *gt, u32 base)
160 {
161 struct intel_uncore *uncore = gt->uncore;
162
163 intel_uncore_write(uncore, RING_CTL(base), 0);
164 intel_uncore_write(uncore, RING_HEAD(base), 0);
165 intel_uncore_write(uncore, RING_TAIL(base), 0);
166 intel_uncore_write(uncore, RING_START(base), 0);
167 }
168
init_unused_rings(struct intel_gt * gt)169 static void init_unused_rings(struct intel_gt *gt)
170 {
171 struct drm_i915_private *i915 = gt->i915;
172
173 if (IS_I830(i915)) {
174 init_unused_ring(gt, PRB1_BASE);
175 init_unused_ring(gt, SRB0_BASE);
176 init_unused_ring(gt, SRB1_BASE);
177 init_unused_ring(gt, SRB2_BASE);
178 init_unused_ring(gt, SRB3_BASE);
179 } else if (GRAPHICS_VER(i915) == 2) {
180 init_unused_ring(gt, SRB0_BASE);
181 init_unused_ring(gt, SRB1_BASE);
182 } else if (GRAPHICS_VER(i915) == 3) {
183 init_unused_ring(gt, PRB1_BASE);
184 init_unused_ring(gt, PRB2_BASE);
185 }
186 }
187
intel_gt_init_hw(struct intel_gt * gt)188 int intel_gt_init_hw(struct intel_gt *gt)
189 {
190 struct drm_i915_private *i915 = gt->i915;
191 struct intel_uncore *uncore = gt->uncore;
192 int ret;
193
194 gt->last_init_time = ktime_get();
195
196 /* Double layer security blanket, see i915_gem_init() */
197 intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
198
199 if (HAS_EDRAM(i915) && GRAPHICS_VER(i915) < 9)
200 intel_uncore_rmw(uncore, HSW_IDICR, 0, IDIHASHMSK(0xf));
201
202 if (IS_HASWELL(i915))
203 intel_uncore_write(uncore,
204 MI_PREDICATE_RESULT_2,
205 IS_HSW_GT3(i915) ?
206 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
207
208 /* Apply the GT workarounds... */
209 intel_gt_apply_workarounds(gt);
210 /* ...and determine whether they are sticking. */
211 intel_gt_verify_workarounds(gt, "init");
212
213 intel_gt_init_swizzling(gt);
214
215 /*
216 * At least 830 can leave some of the unused rings
217 * "active" (ie. head != tail) after resume which
218 * will prevent c3 entry. Makes sure all unused rings
219 * are totally idle.
220 */
221 init_unused_rings(gt);
222
223 ret = i915_ppgtt_init_hw(gt);
224 if (ret) {
225 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
226 goto out;
227 }
228
229 /* We can't enable contexts until all firmware is loaded */
230 ret = intel_uc_init_hw(>->uc);
231 if (ret) {
232 i915_probe_error(i915, "Enabling uc failed (%d)\n", ret);
233 goto out;
234 }
235
236 intel_mocs_init(gt);
237
238 out:
239 intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
240 return ret;
241 }
242
rmw_set(struct intel_uncore * uncore,i915_reg_t reg,u32 set)243 static void rmw_set(struct intel_uncore *uncore, i915_reg_t reg, u32 set)
244 {
245 intel_uncore_rmw(uncore, reg, 0, set);
246 }
247
rmw_clear(struct intel_uncore * uncore,i915_reg_t reg,u32 clr)248 static void rmw_clear(struct intel_uncore *uncore, i915_reg_t reg, u32 clr)
249 {
250 intel_uncore_rmw(uncore, reg, clr, 0);
251 }
252
clear_register(struct intel_uncore * uncore,i915_reg_t reg)253 static void clear_register(struct intel_uncore *uncore, i915_reg_t reg)
254 {
255 intel_uncore_rmw(uncore, reg, 0, 0);
256 }
257
gen6_clear_engine_error_register(struct intel_engine_cs * engine)258 static void gen6_clear_engine_error_register(struct intel_engine_cs *engine)
259 {
260 GEN6_RING_FAULT_REG_RMW(engine, RING_FAULT_VALID, 0);
261 GEN6_RING_FAULT_REG_POSTING_READ(engine);
262 }
263
264 void
intel_gt_clear_error_registers(struct intel_gt * gt,intel_engine_mask_t engine_mask)265 intel_gt_clear_error_registers(struct intel_gt *gt,
266 intel_engine_mask_t engine_mask)
267 {
268 struct drm_i915_private *i915 = gt->i915;
269 struct intel_uncore *uncore = gt->uncore;
270 u32 eir;
271
272 if (GRAPHICS_VER(i915) != 2)
273 clear_register(uncore, PGTBL_ER);
274
275 if (GRAPHICS_VER(i915) < 4)
276 clear_register(uncore, IPEIR(RENDER_RING_BASE));
277 else
278 clear_register(uncore, IPEIR_I965);
279
280 clear_register(uncore, EIR);
281 eir = intel_uncore_read(uncore, EIR);
282 if (eir) {
283 /*
284 * some errors might have become stuck,
285 * mask them.
286 */
287 DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masking\n", eir);
288 rmw_set(uncore, EMR, eir);
289 intel_uncore_write(uncore, GEN2_IIR,
290 I915_MASTER_ERROR_INTERRUPT);
291 }
292
293 if (GRAPHICS_VER(i915) >= 12) {
294 rmw_clear(uncore, GEN12_RING_FAULT_REG, RING_FAULT_VALID);
295 intel_uncore_posting_read(uncore, GEN12_RING_FAULT_REG);
296 } else if (GRAPHICS_VER(i915) >= 8) {
297 rmw_clear(uncore, GEN8_RING_FAULT_REG, RING_FAULT_VALID);
298 intel_uncore_posting_read(uncore, GEN8_RING_FAULT_REG);
299 } else if (GRAPHICS_VER(i915) >= 6) {
300 struct intel_engine_cs *engine;
301 enum intel_engine_id id;
302
303 for_each_engine_masked(engine, gt, engine_mask, id)
304 gen6_clear_engine_error_register(engine);
305 }
306 }
307
gen6_check_faults(struct intel_gt * gt)308 static void gen6_check_faults(struct intel_gt *gt)
309 {
310 struct intel_engine_cs *engine;
311 enum intel_engine_id id;
312 u32 fault;
313
314 for_each_engine(engine, gt, id) {
315 fault = GEN6_RING_FAULT_REG_READ(engine);
316 if (fault & RING_FAULT_VALID) {
317 drm_dbg(&engine->i915->drm, "Unexpected fault\n"
318 "\tAddr: 0x%08lx\n"
319 "\tAddress space: %s\n"
320 "\tSource ID: %d\n"
321 "\tType: %d\n",
322 fault & PAGE_MASK,
323 fault & RING_FAULT_GTTSEL_MASK ?
324 "GGTT" : "PPGTT",
325 RING_FAULT_SRCID(fault),
326 RING_FAULT_FAULT_TYPE(fault));
327 }
328 }
329 }
330
gen8_check_faults(struct intel_gt * gt)331 static void gen8_check_faults(struct intel_gt *gt)
332 {
333 struct intel_uncore *uncore = gt->uncore;
334 i915_reg_t fault_reg, fault_data0_reg, fault_data1_reg;
335 u32 fault;
336
337 if (GRAPHICS_VER(gt->i915) >= 12) {
338 fault_reg = GEN12_RING_FAULT_REG;
339 fault_data0_reg = GEN12_FAULT_TLB_DATA0;
340 fault_data1_reg = GEN12_FAULT_TLB_DATA1;
341 } else {
342 fault_reg = GEN8_RING_FAULT_REG;
343 fault_data0_reg = GEN8_FAULT_TLB_DATA0;
344 fault_data1_reg = GEN8_FAULT_TLB_DATA1;
345 }
346
347 fault = intel_uncore_read(uncore, fault_reg);
348 if (fault & RING_FAULT_VALID) {
349 u32 fault_data0, fault_data1;
350 u64 fault_addr;
351
352 fault_data0 = intel_uncore_read(uncore, fault_data0_reg);
353 fault_data1 = intel_uncore_read(uncore, fault_data1_reg);
354
355 fault_addr = ((u64)(fault_data1 & FAULT_VA_HIGH_BITS) << 44) |
356 ((u64)fault_data0 << 12);
357
358 drm_dbg(&uncore->i915->drm, "Unexpected fault\n"
359 "\tAddr: 0x%08x_%08x\n"
360 "\tAddress space: %s\n"
361 "\tEngine ID: %d\n"
362 "\tSource ID: %d\n"
363 "\tType: %d\n",
364 upper_32_bits(fault_addr), lower_32_bits(fault_addr),
365 fault_data1 & FAULT_GTT_SEL ? "GGTT" : "PPGTT",
366 GEN8_RING_FAULT_ENGINE_ID(fault),
367 RING_FAULT_SRCID(fault),
368 RING_FAULT_FAULT_TYPE(fault));
369 }
370 }
371
intel_gt_check_and_clear_faults(struct intel_gt * gt)372 void intel_gt_check_and_clear_faults(struct intel_gt *gt)
373 {
374 struct drm_i915_private *i915 = gt->i915;
375
376 /* From GEN8 onwards we only have one 'All Engine Fault Register' */
377 if (GRAPHICS_VER(i915) >= 8)
378 gen8_check_faults(gt);
379 else if (GRAPHICS_VER(i915) >= 6)
380 gen6_check_faults(gt);
381 else
382 return;
383
384 intel_gt_clear_error_registers(gt, ALL_ENGINES);
385 }
386
intel_gt_flush_ggtt_writes(struct intel_gt * gt)387 void intel_gt_flush_ggtt_writes(struct intel_gt *gt)
388 {
389 struct intel_uncore *uncore = gt->uncore;
390 intel_wakeref_t wakeref;
391
392 /*
393 * No actual flushing is required for the GTT write domain for reads
394 * from the GTT domain. Writes to it "immediately" go to main memory
395 * as far as we know, so there's no chipset flush. It also doesn't
396 * land in the GPU render cache.
397 *
398 * However, we do have to enforce the order so that all writes through
399 * the GTT land before any writes to the device, such as updates to
400 * the GATT itself.
401 *
402 * We also have to wait a bit for the writes to land from the GTT.
403 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
404 * timing. This issue has only been observed when switching quickly
405 * between GTT writes and CPU reads from inside the kernel on recent hw,
406 * and it appears to only affect discrete GTT blocks (i.e. on LLC
407 * system agents we cannot reproduce this behaviour, until Cannonlake
408 * that was!).
409 */
410
411 wmb();
412
413 if (INTEL_INFO(gt->i915)->has_coherent_ggtt)
414 return;
415
416 intel_gt_chipset_flush(gt);
417
418 with_intel_runtime_pm_if_in_use(uncore->rpm, wakeref) {
419 unsigned long flags;
420
421 spin_lock_irqsave(&uncore->lock, flags);
422 intel_uncore_posting_read_fw(uncore,
423 RING_HEAD(RENDER_RING_BASE));
424 spin_unlock_irqrestore(&uncore->lock, flags);
425 }
426 }
427
intel_gt_chipset_flush(struct intel_gt * gt)428 void intel_gt_chipset_flush(struct intel_gt *gt)
429 {
430 wmb();
431 if (GRAPHICS_VER(gt->i915) < 6)
432 intel_gtt_chipset_flush();
433 }
434
intel_gt_driver_register(struct intel_gt * gt)435 void intel_gt_driver_register(struct intel_gt *gt)
436 {
437 intel_rps_driver_register(>->rps);
438
439 debugfs_gt_register(gt);
440 }
441
intel_gt_init_scratch(struct intel_gt * gt,unsigned int size)442 static int intel_gt_init_scratch(struct intel_gt *gt, unsigned int size)
443 {
444 struct drm_i915_private *i915 = gt->i915;
445 struct drm_i915_gem_object *obj;
446 struct i915_vma *vma;
447 int ret;
448
449 obj = i915_gem_object_create_lmem(i915, size, I915_BO_ALLOC_VOLATILE);
450 if (IS_ERR(obj))
451 obj = i915_gem_object_create_stolen(i915, size);
452 if (IS_ERR(obj))
453 obj = i915_gem_object_create_internal(i915, size);
454 if (IS_ERR(obj)) {
455 drm_err(&i915->drm, "Failed to allocate scratch page\n");
456 return PTR_ERR(obj);
457 }
458
459 vma = i915_vma_instance(obj, >->ggtt->vm, NULL);
460 if (IS_ERR(vma)) {
461 ret = PTR_ERR(vma);
462 goto err_unref;
463 }
464
465 ret = i915_ggtt_pin(vma, NULL, 0, PIN_HIGH);
466 if (ret)
467 goto err_unref;
468
469 gt->scratch = i915_vma_make_unshrinkable(vma);
470
471 return 0;
472
473 err_unref:
474 i915_gem_object_put(obj);
475 return ret;
476 }
477
intel_gt_fini_scratch(struct intel_gt * gt)478 static void intel_gt_fini_scratch(struct intel_gt *gt)
479 {
480 i915_vma_unpin_and_release(>->scratch, 0);
481 }
482
kernel_vm(struct intel_gt * gt)483 static struct i915_address_space *kernel_vm(struct intel_gt *gt)
484 {
485 if (INTEL_PPGTT(gt->i915) > INTEL_PPGTT_ALIASING)
486 return &i915_ppgtt_create(gt)->vm;
487 else
488 return i915_vm_get(>->ggtt->vm);
489 }
490
__engines_record_defaults(struct intel_gt * gt)491 static int __engines_record_defaults(struct intel_gt *gt)
492 {
493 struct i915_request *requests[I915_NUM_ENGINES] = {};
494 struct intel_engine_cs *engine;
495 enum intel_engine_id id;
496 int err = 0;
497
498 /*
499 * As we reset the gpu during very early sanitisation, the current
500 * register state on the GPU should reflect its defaults values.
501 * We load a context onto the hw (with restore-inhibit), then switch
502 * over to a second context to save that default register state. We
503 * can then prime every new context with that state so they all start
504 * from the same default HW values.
505 */
506
507 for_each_engine(engine, gt, id) {
508 struct intel_renderstate so;
509 struct intel_context *ce;
510 struct i915_request *rq;
511
512 /* We must be able to switch to something! */
513 GEM_BUG_ON(!engine->kernel_context);
514
515 ce = intel_context_create(engine);
516 if (IS_ERR(ce)) {
517 err = PTR_ERR(ce);
518 goto out;
519 }
520
521 err = intel_renderstate_init(&so, ce);
522 if (err)
523 goto err;
524
525 rq = i915_request_create(ce);
526 if (IS_ERR(rq)) {
527 err = PTR_ERR(rq);
528 goto err_fini;
529 }
530
531 err = intel_engine_emit_ctx_wa(rq);
532 if (err)
533 goto err_rq;
534
535 err = intel_renderstate_emit(&so, rq);
536 if (err)
537 goto err_rq;
538
539 err_rq:
540 requests[id] = i915_request_get(rq);
541 i915_request_add(rq);
542 err_fini:
543 intel_renderstate_fini(&so, ce);
544 err:
545 if (err) {
546 intel_context_put(ce);
547 goto out;
548 }
549 }
550
551 /* Flush the default context image to memory, and enable powersaving. */
552 if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
553 err = -EIO;
554 goto out;
555 }
556
557 for (id = 0; id < ARRAY_SIZE(requests); id++) {
558 struct i915_request *rq;
559 struct file *state;
560
561 rq = requests[id];
562 if (!rq)
563 continue;
564
565 if (rq->fence.error) {
566 err = -EIO;
567 goto out;
568 }
569
570 GEM_BUG_ON(!test_bit(CONTEXT_ALLOC_BIT, &rq->context->flags));
571 if (!rq->context->state)
572 continue;
573
574 /* Keep a copy of the state's backing pages; free the obj */
575 state = shmem_create_from_object(rq->context->state->obj);
576 if (IS_ERR(state)) {
577 err = PTR_ERR(state);
578 goto out;
579 }
580 rq->engine->default_state = state;
581 }
582
583 out:
584 /*
585 * If we have to abandon now, we expect the engines to be idle
586 * and ready to be torn-down. The quickest way we can accomplish
587 * this is by declaring ourselves wedged.
588 */
589 if (err)
590 intel_gt_set_wedged(gt);
591
592 for (id = 0; id < ARRAY_SIZE(requests); id++) {
593 struct intel_context *ce;
594 struct i915_request *rq;
595
596 rq = requests[id];
597 if (!rq)
598 continue;
599
600 ce = rq->context;
601 i915_request_put(rq);
602 intel_context_put(ce);
603 }
604 return err;
605 }
606
__engines_verify_workarounds(struct intel_gt * gt)607 static int __engines_verify_workarounds(struct intel_gt *gt)
608 {
609 struct intel_engine_cs *engine;
610 enum intel_engine_id id;
611 int err = 0;
612
613 if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
614 return 0;
615
616 for_each_engine(engine, gt, id) {
617 if (intel_engine_verify_workarounds(engine, "load"))
618 err = -EIO;
619 }
620
621 /* Flush and restore the kernel context for safety */
622 if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME)
623 err = -EIO;
624
625 return err;
626 }
627
__intel_gt_disable(struct intel_gt * gt)628 static void __intel_gt_disable(struct intel_gt *gt)
629 {
630 intel_gt_set_wedged_on_fini(gt);
631
632 intel_gt_suspend_prepare(gt);
633 intel_gt_suspend_late(gt);
634
635 GEM_BUG_ON(intel_gt_pm_is_awake(gt));
636 }
637
intel_gt_wait_for_idle(struct intel_gt * gt,long timeout)638 int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout)
639 {
640 long remaining_timeout;
641
642 /* If the device is asleep, we have no requests outstanding */
643 if (!intel_gt_pm_is_awake(gt))
644 return 0;
645
646 while ((timeout = intel_gt_retire_requests_timeout(gt, timeout,
647 &remaining_timeout)) > 0) {
648 cond_resched();
649 if (signal_pending(current))
650 return -EINTR;
651 }
652
653 if (timeout)
654 return timeout;
655
656 if (remaining_timeout < 0)
657 remaining_timeout = 0;
658
659 return intel_uc_wait_for_idle(>->uc, remaining_timeout);
660 }
661
intel_gt_init(struct intel_gt * gt)662 int intel_gt_init(struct intel_gt *gt)
663 {
664 int err;
665
666 err = i915_inject_probe_error(gt->i915, -ENODEV);
667 if (err)
668 return err;
669
670 /*
671 * This is just a security blanket to placate dragons.
672 * On some systems, we very sporadically observe that the first TLBs
673 * used by the CS may be stale, despite us poking the TLB reset. If
674 * we hold the forcewake during initialisation these problems
675 * just magically go away.
676 */
677 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
678
679 err = intel_gt_init_scratch(gt,
680 GRAPHICS_VER(gt->i915) == 2 ? SZ_256K : SZ_4K);
681 if (err)
682 goto out_fw;
683
684 intel_gt_pm_init(gt);
685
686 gt->vm = kernel_vm(gt);
687 if (!gt->vm) {
688 err = -ENOMEM;
689 goto err_pm;
690 }
691
692 err = intel_engines_init(gt);
693 if (err)
694 goto err_engines;
695
696 err = intel_uc_init(>->uc);
697 if (err)
698 goto err_engines;
699
700 err = intel_gt_resume(gt);
701 if (err)
702 goto err_uc_init;
703
704 err = __engines_record_defaults(gt);
705 if (err)
706 goto err_gt;
707
708 err = __engines_verify_workarounds(gt);
709 if (err)
710 goto err_gt;
711
712 err = i915_inject_probe_error(gt->i915, -EIO);
713 if (err)
714 goto err_gt;
715
716 intel_uc_init_late(>->uc);
717
718 intel_migrate_init(>->migrate, gt);
719
720 goto out_fw;
721 err_gt:
722 __intel_gt_disable(gt);
723 intel_uc_fini_hw(>->uc);
724 err_uc_init:
725 intel_uc_fini(>->uc);
726 err_engines:
727 intel_engines_release(gt);
728 i915_vm_put(fetch_and_zero(>->vm));
729 err_pm:
730 intel_gt_pm_fini(gt);
731 intel_gt_fini_scratch(gt);
732 out_fw:
733 if (err)
734 intel_gt_set_wedged_on_init(gt);
735 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
736 return err;
737 }
738
intel_gt_driver_remove(struct intel_gt * gt)739 void intel_gt_driver_remove(struct intel_gt *gt)
740 {
741 __intel_gt_disable(gt);
742
743 intel_migrate_fini(>->migrate);
744 intel_uc_driver_remove(>->uc);
745
746 intel_engines_release(gt);
747 }
748
intel_gt_driver_unregister(struct intel_gt * gt)749 void intel_gt_driver_unregister(struct intel_gt *gt)
750 {
751 intel_wakeref_t wakeref;
752
753 intel_rps_driver_unregister(>->rps);
754
755 /*
756 * Upon unregistering the device to prevent any new users, cancel
757 * all in-flight requests so that we can quickly unbind the active
758 * resources.
759 */
760 intel_gt_set_wedged(gt);
761
762 /* Scrub all HW state upon release */
763 with_intel_runtime_pm(gt->uncore->rpm, wakeref)
764 __intel_gt_reset(gt, ALL_ENGINES);
765 }
766
intel_gt_driver_release(struct intel_gt * gt)767 void intel_gt_driver_release(struct intel_gt *gt)
768 {
769 struct i915_address_space *vm;
770
771 vm = fetch_and_zero(>->vm);
772 if (vm) /* FIXME being called twice on error paths :( */
773 i915_vm_put(vm);
774
775 intel_gt_pm_fini(gt);
776 intel_gt_fini_scratch(gt);
777 intel_gt_fini_buffer_pool(gt);
778 }
779
intel_gt_driver_late_release(struct intel_gt * gt)780 void intel_gt_driver_late_release(struct intel_gt *gt)
781 {
782 /* We need to wait for inflight RCU frees to release their grip */
783 rcu_barrier();
784
785 intel_uc_driver_late_release(>->uc);
786 intel_gt_fini_requests(gt);
787 intel_gt_fini_reset(gt);
788 intel_gt_fini_timelines(gt);
789 intel_engines_free(gt);
790 }
791
792 /**
793 * intel_gt_reg_needs_read_steering - determine whether a register read
794 * requires explicit steering
795 * @gt: GT structure
796 * @reg: the register to check steering requirements for
797 * @type: type of multicast steering to check
798 *
799 * Determines whether @reg needs explicit steering of a specific type for
800 * reads.
801 *
802 * Returns false if @reg does not belong to a register range of the given
803 * steering type, or if the default (subslice-based) steering IDs are suitable
804 * for @type steering too.
805 */
intel_gt_reg_needs_read_steering(struct intel_gt * gt,i915_reg_t reg,enum intel_steering_type type)806 static bool intel_gt_reg_needs_read_steering(struct intel_gt *gt,
807 i915_reg_t reg,
808 enum intel_steering_type type)
809 {
810 const u32 offset = i915_mmio_reg_offset(reg);
811 const struct intel_mmio_range *entry;
812
813 if (likely(!intel_gt_needs_read_steering(gt, type)))
814 return false;
815
816 for (entry = gt->steering_table[type]; entry->end; entry++) {
817 if (offset >= entry->start && offset <= entry->end)
818 return true;
819 }
820
821 return false;
822 }
823
824 /**
825 * intel_gt_get_valid_steering - determines valid IDs for a class of MCR steering
826 * @gt: GT structure
827 * @type: multicast register type
828 * @sliceid: Slice ID returned
829 * @subsliceid: Subslice ID returned
830 *
831 * Determines sliceid and subsliceid values that will steer reads
832 * of a specific multicast register class to a valid value.
833 */
intel_gt_get_valid_steering(struct intel_gt * gt,enum intel_steering_type type,u8 * sliceid,u8 * subsliceid)834 static void intel_gt_get_valid_steering(struct intel_gt *gt,
835 enum intel_steering_type type,
836 u8 *sliceid, u8 *subsliceid)
837 {
838 switch (type) {
839 case L3BANK:
840 GEM_DEBUG_WARN_ON(!gt->info.l3bank_mask); /* should be impossible! */
841
842 *sliceid = 0; /* unused */
843 *subsliceid = __ffs(gt->info.l3bank_mask);
844 break;
845 case MSLICE:
846 GEM_DEBUG_WARN_ON(!gt->info.mslice_mask); /* should be impossible! */
847
848 *sliceid = __ffs(gt->info.mslice_mask);
849 *subsliceid = 0; /* unused */
850 break;
851 case LNCF:
852 GEM_DEBUG_WARN_ON(!gt->info.mslice_mask); /* should be impossible! */
853
854 /*
855 * An LNCF is always present if its mslice is present, so we
856 * can safely just steer to LNCF 0 in all cases.
857 */
858 *sliceid = __ffs(gt->info.mslice_mask) << 1;
859 *subsliceid = 0; /* unused */
860 break;
861 default:
862 MISSING_CASE(type);
863 *sliceid = 0;
864 *subsliceid = 0;
865 }
866 }
867
868 /**
869 * intel_gt_read_register_fw - reads a GT register with support for multicast
870 * @gt: GT structure
871 * @reg: register to read
872 *
873 * This function will read a GT register. If the register is a multicast
874 * register, the read will be steered to a valid instance (i.e., one that
875 * isn't fused off or powered down by power gating).
876 *
877 * Returns the value from a valid instance of @reg.
878 */
intel_gt_read_register_fw(struct intel_gt * gt,i915_reg_t reg)879 u32 intel_gt_read_register_fw(struct intel_gt *gt, i915_reg_t reg)
880 {
881 int type;
882 u8 sliceid, subsliceid;
883
884 for (type = 0; type < NUM_STEERING_TYPES; type++) {
885 if (intel_gt_reg_needs_read_steering(gt, reg, type)) {
886 intel_gt_get_valid_steering(gt, type, &sliceid,
887 &subsliceid);
888 return intel_uncore_read_with_mcr_steering_fw(gt->uncore,
889 reg,
890 sliceid,
891 subsliceid);
892 }
893 }
894
895 return intel_uncore_read_fw(gt->uncore, reg);
896 }
897
intel_gt_info_print(const struct intel_gt_info * info,struct drm_printer * p)898 void intel_gt_info_print(const struct intel_gt_info *info,
899 struct drm_printer *p)
900 {
901 drm_printf(p, "available engines: %x\n", info->engine_mask);
902
903 intel_sseu_dump(&info->sseu, p);
904 }
905
906 struct reg_and_bit {
907 i915_reg_t reg;
908 u32 bit;
909 };
910
911 static struct reg_and_bit
get_reg_and_bit(const struct intel_engine_cs * engine,const bool gen8,const i915_reg_t * regs,const unsigned int num)912 get_reg_and_bit(const struct intel_engine_cs *engine, const bool gen8,
913 const i915_reg_t *regs, const unsigned int num)
914 {
915 const unsigned int class = engine->class;
916 struct reg_and_bit rb = { };
917
918 if (drm_WARN_ON_ONCE(&engine->i915->drm,
919 class >= num || !regs[class].reg))
920 return rb;
921
922 rb.reg = regs[class];
923 if (gen8 && class == VIDEO_DECODE_CLASS)
924 rb.reg.reg += 4 * engine->instance; /* GEN8_M2TCR */
925 else
926 rb.bit = engine->instance;
927
928 rb.bit = BIT(rb.bit);
929
930 return rb;
931 }
932
intel_gt_invalidate_tlbs(struct intel_gt * gt)933 void intel_gt_invalidate_tlbs(struct intel_gt *gt)
934 {
935 static const i915_reg_t gen8_regs[] = {
936 [RENDER_CLASS] = GEN8_RTCR,
937 [VIDEO_DECODE_CLASS] = GEN8_M1TCR, /* , GEN8_M2TCR */
938 [VIDEO_ENHANCEMENT_CLASS] = GEN8_VTCR,
939 [COPY_ENGINE_CLASS] = GEN8_BTCR,
940 };
941 static const i915_reg_t gen12_regs[] = {
942 [RENDER_CLASS] = GEN12_GFX_TLB_INV_CR,
943 [VIDEO_DECODE_CLASS] = GEN12_VD_TLB_INV_CR,
944 [VIDEO_ENHANCEMENT_CLASS] = GEN12_VE_TLB_INV_CR,
945 [COPY_ENGINE_CLASS] = GEN12_BLT_TLB_INV_CR,
946 };
947 struct drm_i915_private *i915 = gt->i915;
948 struct intel_uncore *uncore = gt->uncore;
949 struct intel_engine_cs *engine;
950 enum intel_engine_id id;
951 const i915_reg_t *regs;
952 unsigned int num = 0;
953
954 if (I915_SELFTEST_ONLY(gt->awake == -ENODEV))
955 return;
956
957 if (intel_gt_is_wedged(gt))
958 return;
959
960 if (GRAPHICS_VER(i915) == 12) {
961 regs = gen12_regs;
962 num = ARRAY_SIZE(gen12_regs);
963 } else if (GRAPHICS_VER(i915) >= 8 && GRAPHICS_VER(i915) <= 11) {
964 regs = gen8_regs;
965 num = ARRAY_SIZE(gen8_regs);
966 } else if (GRAPHICS_VER(i915) < 8) {
967 return;
968 }
969
970 if (drm_WARN_ONCE(&i915->drm, !num,
971 "Platform does not implement TLB invalidation!"))
972 return;
973
974 GEM_TRACE("\n");
975
976 assert_rpm_wakelock_held(&i915->runtime_pm);
977
978 mutex_lock(>->tlb_invalidate_lock);
979 intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
980
981 spin_lock_irq(&uncore->lock); /* serialise invalidate with GT reset */
982
983 for_each_engine(engine, gt, id) {
984 struct reg_and_bit rb;
985
986 rb = get_reg_and_bit(engine, regs == gen8_regs, regs, num);
987 if (!i915_mmio_reg_offset(rb.reg))
988 continue;
989
990 if (GRAPHICS_VER(i915) == 12 && (engine->class == VIDEO_DECODE_CLASS ||
991 engine->class == VIDEO_ENHANCEMENT_CLASS))
992 rb.bit = _MASKED_BIT_ENABLE(rb.bit);
993
994 intel_uncore_write_fw(uncore, rb.reg, rb.bit);
995 }
996
997 spin_unlock_irq(&uncore->lock);
998
999 for_each_engine(engine, gt, id) {
1000 /*
1001 * HW architecture suggest typical invalidation time at 40us,
1002 * with pessimistic cases up to 100us and a recommendation to
1003 * cap at 1ms. We go a bit higher just in case.
1004 */
1005 const unsigned int timeout_us = 100;
1006 const unsigned int timeout_ms = 4;
1007 struct reg_and_bit rb;
1008
1009 rb = get_reg_and_bit(engine, regs == gen8_regs, regs, num);
1010 if (!i915_mmio_reg_offset(rb.reg))
1011 continue;
1012
1013 if (__intel_wait_for_register_fw(uncore,
1014 rb.reg, rb.bit, 0,
1015 timeout_us, timeout_ms,
1016 NULL))
1017 drm_err_ratelimited(>->i915->drm,
1018 "%s TLB invalidation did not complete in %ums!\n",
1019 engine->name, timeout_ms);
1020 }
1021
1022 intel_uncore_forcewake_put_delayed(uncore, FORCEWAKE_ALL);
1023 mutex_unlock(>->tlb_invalidate_lock);
1024 }
1025