1 // SPDX-License-Identifier: MIT
3 * Copyright © 2014-2019 Intel Corporation
6 #include <linux/bsearch.h>
8 #include "gt/intel_engine_regs.h"
9 #include "gt/intel_gt.h"
10 #include "gt/intel_gt_regs.h"
11 #include "gt/intel_lrc.h"
12 #include "gt/shmem_utils.h"
13 #include "intel_guc_ads.h"
14 #include "intel_guc_fwif.h"
19 * The Additional Data Struct (ADS) has pointers for different buffers used by
20 * the GuC. One single gem object contains the ADS struct itself (guc_ads) and
21 * all the extra buffers indirectly linked via the ADS struct's entries.
23 * Layout of the ADS blob allocated for the GuC:
25 * +---------------------------------------+ <== base
27 * +---------------------------------------+
29 * +---------------------------------------+
30 * | guc_gt_system_info |
31 * +---------------------------------------+
32 * | guc_engine_usage |
33 * +---------------------------------------+ <== static
34 * | guc_mmio_reg[countA] (engine 0.0) |
35 * | guc_mmio_reg[countB] (engine 0.1) |
36 * | guc_mmio_reg[countC] (engine 1.0) |
38 * +---------------------------------------+ <== dynamic
40 * +---------------------------------------+ <== 4K aligned
42 * +---------------------------------------+
44 * +---------------------------------------+ <== 4K aligned
46 * +---------------------------------------+
48 * +---------------------------------------+ <== 4K aligned
50 * +---------------------------------------+
52 * +---------------------------------------+ <== 4K aligned
54 struct __guc_ads_blob {
56 struct guc_policies policies;
57 struct guc_gt_system_info system_info;
58 struct guc_engine_usage engine_usage;
59 /* From here on, location is dynamic! Refer to above diagram. */
60 struct guc_mmio_reg regset[];
63 #define ads_blob_read(guc_, field_) \
64 iosys_map_rd_field(&(guc_)->ads_map, 0, struct __guc_ads_blob, field_)
66 #define ads_blob_write(guc_, field_, val_) \
67 iosys_map_wr_field(&(guc_)->ads_map, 0, struct __guc_ads_blob, \
70 #define info_map_write(map_, field_, val_) \
71 iosys_map_wr_field(map_, 0, struct guc_gt_system_info, field_, val_)
73 #define info_map_read(map_, field_) \
74 iosys_map_rd_field(map_, 0, struct guc_gt_system_info, field_)
76 static u32 guc_ads_regset_size(struct intel_guc *guc)
78 GEM_BUG_ON(!guc->ads_regset_size);
79 return guc->ads_regset_size;
82 static u32 guc_ads_golden_ctxt_size(struct intel_guc *guc)
84 return PAGE_ALIGN(guc->ads_golden_ctxt_size);
87 static u32 guc_ads_capture_size(struct intel_guc *guc)
89 /* FIXME: Allocate a proper capture list */
90 return PAGE_ALIGN(PAGE_SIZE);
93 static u32 guc_ads_private_data_size(struct intel_guc *guc)
95 return PAGE_ALIGN(guc->fw.private_data_size);
98 static u32 guc_ads_regset_offset(struct intel_guc *guc)
100 return offsetof(struct __guc_ads_blob, regset);
103 static u32 guc_ads_golden_ctxt_offset(struct intel_guc *guc)
107 offset = guc_ads_regset_offset(guc) +
108 guc_ads_regset_size(guc);
110 return PAGE_ALIGN(offset);
113 static u32 guc_ads_capture_offset(struct intel_guc *guc)
117 offset = guc_ads_golden_ctxt_offset(guc) +
118 guc_ads_golden_ctxt_size(guc);
120 return PAGE_ALIGN(offset);
123 static u32 guc_ads_private_data_offset(struct intel_guc *guc)
127 offset = guc_ads_capture_offset(guc) +
128 guc_ads_capture_size(guc);
130 return PAGE_ALIGN(offset);
133 static u32 guc_ads_blob_size(struct intel_guc *guc)
135 return guc_ads_private_data_offset(guc) +
136 guc_ads_private_data_size(guc);
139 static void guc_policies_init(struct intel_guc *guc)
141 struct intel_gt *gt = guc_to_gt(guc);
142 struct drm_i915_private *i915 = gt->i915;
143 u32 global_flags = 0;
145 ads_blob_write(guc, policies.dpc_promote_time,
146 GLOBAL_POLICY_DEFAULT_DPC_PROMOTE_TIME_US);
147 ads_blob_write(guc, policies.max_num_work_items,
148 GLOBAL_POLICY_MAX_NUM_WI);
150 if (i915->params.reset < 2)
151 global_flags |= GLOBAL_POLICY_DISABLE_ENGINE_RESET;
153 ads_blob_write(guc, policies.global_flags, global_flags);
154 ads_blob_write(guc, policies.is_valid, 1);
157 void intel_guc_ads_print_policy_info(struct intel_guc *guc,
158 struct drm_printer *dp)
160 if (unlikely(iosys_map_is_null(&guc->ads_map)))
163 drm_printf(dp, "Global scheduling policies:\n");
164 drm_printf(dp, " DPC promote time = %u\n",
165 ads_blob_read(guc, policies.dpc_promote_time));
166 drm_printf(dp, " Max num work items = %u\n",
167 ads_blob_read(guc, policies.max_num_work_items));
168 drm_printf(dp, " Flags = %u\n",
169 ads_blob_read(guc, policies.global_flags));
172 static int guc_action_policies_update(struct intel_guc *guc, u32 policy_offset)
175 INTEL_GUC_ACTION_GLOBAL_SCHED_POLICY_CHANGE,
179 return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);
182 int intel_guc_global_policies_update(struct intel_guc *guc)
184 struct intel_gt *gt = guc_to_gt(guc);
185 u32 scheduler_policies;
186 intel_wakeref_t wakeref;
189 if (iosys_map_is_null(&guc->ads_map))
192 scheduler_policies = ads_blob_read(guc, ads.scheduler_policies);
193 GEM_BUG_ON(!scheduler_policies);
195 guc_policies_init(guc);
197 if (!intel_guc_is_ready(guc))
200 with_intel_runtime_pm(>->i915->runtime_pm, wakeref)
201 ret = guc_action_policies_update(guc, scheduler_policies);
206 static void guc_mapping_table_init(struct intel_gt *gt,
207 struct iosys_map *info_map)
210 struct intel_engine_cs *engine;
211 enum intel_engine_id id;
213 /* Table must be set to invalid values for entries not used */
214 for (i = 0; i < GUC_MAX_ENGINE_CLASSES; ++i)
215 for (j = 0; j < GUC_MAX_INSTANCES_PER_CLASS; ++j)
216 info_map_write(info_map, mapping_table[i][j],
217 GUC_MAX_INSTANCES_PER_CLASS);
219 for_each_engine(engine, gt, id) {
220 u8 guc_class = engine_class_to_guc_class(engine->class);
222 info_map_write(info_map, mapping_table[guc_class][ilog2(engine->logical_mask)],
228 * The save/restore register list must be pre-calculated to a temporary
229 * buffer before it can be copied inside the ADS.
233 * ptr to the section of the storage for the engine currently being
236 struct guc_mmio_reg *registers;
237 /* ptr to the base of the allocated storage for all engines */
238 struct guc_mmio_reg *storage;
243 static int guc_mmio_reg_cmp(const void *a, const void *b)
245 const struct guc_mmio_reg *ra = a;
246 const struct guc_mmio_reg *rb = b;
248 return (int)ra->offset - (int)rb->offset;
251 static struct guc_mmio_reg * __must_check
252 __mmio_reg_add(struct temp_regset *regset, struct guc_mmio_reg *reg)
254 u32 pos = regset->storage_used;
255 struct guc_mmio_reg *slot;
257 if (pos >= regset->storage_max) {
258 size_t size = ALIGN((pos + 1) * sizeof(*slot), PAGE_SIZE);
259 struct guc_mmio_reg *r = krealloc(regset->storage,
262 WARN_ONCE(1, "Incomplete regset list: can't add register (%d)\n",
264 return ERR_PTR(-ENOMEM);
267 regset->registers = r + (regset->registers - regset->storage);
269 regset->storage_max = size / sizeof(*slot);
272 slot = ®set->storage[pos];
273 regset->storage_used++;
279 static long __must_check guc_mmio_reg_add(struct temp_regset *regset,
280 u32 offset, u32 flags)
282 u32 count = regset->storage_used - (regset->registers - regset->storage);
283 struct guc_mmio_reg reg = {
287 struct guc_mmio_reg *slot;
290 * The mmio list is built using separate lists within the driver.
291 * It's possible that at some point we may attempt to add the same
292 * register more than once. Do not consider this an error; silently
293 * move on if the register is already in the list.
295 if (bsearch(®, regset->registers, count,
296 sizeof(reg), guc_mmio_reg_cmp))
299 slot = __mmio_reg_add(regset, ®);
301 return PTR_ERR(slot);
303 while (slot-- > regset->registers) {
304 GEM_BUG_ON(slot[0].offset == slot[1].offset);
305 if (slot[1].offset > slot[0].offset)
308 swap(slot[1], slot[0]);
314 #define GUC_MMIO_REG_ADD(regset, reg, masked) \
315 guc_mmio_reg_add(regset, \
316 i915_mmio_reg_offset((reg)), \
317 (masked) ? GUC_REGSET_MASKED : 0)
319 static int guc_mmio_regset_init(struct temp_regset *regset,
320 struct intel_engine_cs *engine)
322 const u32 base = engine->mmio_base;
323 struct i915_wa_list *wal = &engine->wa_list;
329 * Each engine's registers point to a new start relative to
332 regset->registers = regset->storage + regset->storage_used;
334 ret |= GUC_MMIO_REG_ADD(regset, RING_MODE_GEN7(base), true);
335 ret |= GUC_MMIO_REG_ADD(regset, RING_HWS_PGA(base), false);
336 ret |= GUC_MMIO_REG_ADD(regset, RING_IMR(base), false);
338 if (engine->class == RENDER_CLASS &&
339 CCS_MASK(engine->gt))
340 ret |= GUC_MMIO_REG_ADD(regset, GEN12_RCU_MODE, true);
342 for (i = 0, wa = wal->list; i < wal->count; i++, wa++)
343 ret |= GUC_MMIO_REG_ADD(regset, wa->reg, wa->masked_reg);
345 /* Be extra paranoid and include all whitelist registers. */
346 for (i = 0; i < RING_MAX_NONPRIV_SLOTS; i++)
347 ret |= GUC_MMIO_REG_ADD(regset,
348 RING_FORCE_TO_NONPRIV(base, i),
351 /* add in local MOCS registers */
352 for (i = 0; i < GEN9_LNCFCMOCS_REG_COUNT; i++)
353 ret |= GUC_MMIO_REG_ADD(regset, GEN9_LNCFCMOCS(i), false);
358 static long guc_mmio_reg_state_create(struct intel_guc *guc)
360 struct intel_gt *gt = guc_to_gt(guc);
361 struct intel_engine_cs *engine;
362 enum intel_engine_id id;
363 struct temp_regset temp_set = {};
367 for_each_engine(engine, gt, id) {
368 u32 used = temp_set.storage_used;
370 ret = guc_mmio_regset_init(&temp_set, engine);
372 goto fail_regset_init;
374 guc->ads_regset_count[id] = temp_set.storage_used - used;
375 total += guc->ads_regset_count[id];
378 guc->ads_regset = temp_set.storage;
380 drm_dbg(&guc_to_gt(guc)->i915->drm, "Used %zu KB for temporary ADS regset\n",
381 (temp_set.storage_max * sizeof(struct guc_mmio_reg)) >> 10);
383 return total * sizeof(struct guc_mmio_reg);
386 kfree(temp_set.storage);
390 static void guc_mmio_reg_state_init(struct intel_guc *guc)
392 struct intel_gt *gt = guc_to_gt(guc);
393 struct intel_engine_cs *engine;
394 enum intel_engine_id id;
395 u32 addr_ggtt, offset;
397 offset = guc_ads_regset_offset(guc);
398 addr_ggtt = intel_guc_ggtt_offset(guc, guc->ads_vma) + offset;
400 iosys_map_memcpy_to(&guc->ads_map, offset, guc->ads_regset,
401 guc->ads_regset_size);
403 for_each_engine(engine, gt, id) {
404 u32 count = guc->ads_regset_count[id];
407 /* Class index is checked in class converter */
408 GEM_BUG_ON(engine->instance >= GUC_MAX_INSTANCES_PER_CLASS);
410 guc_class = engine_class_to_guc_class(engine->class);
414 ads.reg_state_list[guc_class][engine->instance].address,
417 ads.reg_state_list[guc_class][engine->instance].count,
423 ads.reg_state_list[guc_class][engine->instance].address,
426 ads.reg_state_list[guc_class][engine->instance].count,
429 addr_ggtt += count * sizeof(struct guc_mmio_reg);
433 static void fill_engine_enable_masks(struct intel_gt *gt,
434 struct iosys_map *info_map)
436 info_map_write(info_map, engine_enabled_masks[GUC_RENDER_CLASS], 1);
437 info_map_write(info_map, engine_enabled_masks[GUC_COMPUTE_CLASS], CCS_MASK(gt));
438 info_map_write(info_map, engine_enabled_masks[GUC_BLITTER_CLASS], 1);
439 info_map_write(info_map, engine_enabled_masks[GUC_VIDEO_CLASS], VDBOX_MASK(gt));
440 info_map_write(info_map, engine_enabled_masks[GUC_VIDEOENHANCE_CLASS], VEBOX_MASK(gt));
443 #define LR_HW_CONTEXT_SIZE (80 * sizeof(u32))
444 #define LRC_SKIP_SIZE (LRC_PPHWSP_SZ * PAGE_SIZE + LR_HW_CONTEXT_SIZE)
445 static int guc_prep_golden_context(struct intel_guc *guc)
447 struct intel_gt *gt = guc_to_gt(guc);
448 u32 addr_ggtt, offset;
449 u32 total_size = 0, alloc_size, real_size;
450 u8 engine_class, guc_class;
451 struct guc_gt_system_info local_info;
452 struct iosys_map info_map;
455 * Reserve the memory for the golden contexts and point GuC at it but
456 * leave it empty for now. The context data will be filled in later
457 * once there is something available to put there.
459 * Note that the HWSP and ring context are not included.
461 * Note also that the storage must be pinned in the GGTT, so that the
462 * address won't change after GuC has been told where to find it. The
463 * GuC will also validate that the LRC base + size fall within the
464 * allowed GGTT range.
466 if (!iosys_map_is_null(&guc->ads_map)) {
467 offset = guc_ads_golden_ctxt_offset(guc);
468 addr_ggtt = intel_guc_ggtt_offset(guc, guc->ads_vma) + offset;
469 info_map = IOSYS_MAP_INIT_OFFSET(&guc->ads_map,
470 offsetof(struct __guc_ads_blob, system_info));
472 memset(&local_info, 0, sizeof(local_info));
473 iosys_map_set_vaddr(&info_map, &local_info);
474 fill_engine_enable_masks(gt, &info_map);
477 for (engine_class = 0; engine_class <= MAX_ENGINE_CLASS; ++engine_class) {
478 if (engine_class == OTHER_CLASS)
481 guc_class = engine_class_to_guc_class(engine_class);
483 if (!info_map_read(&info_map, engine_enabled_masks[guc_class]))
486 real_size = intel_engine_context_size(gt, engine_class);
487 alloc_size = PAGE_ALIGN(real_size);
488 total_size += alloc_size;
490 if (iosys_map_is_null(&guc->ads_map))
494 * This interface is slightly confusing. We need to pass the
495 * base address of the full golden context and the size of just
496 * the engine state, which is the section of the context image
497 * that starts after the execlists context. This is required to
498 * allow the GuC to restore just the engine state when a
499 * watchdog reset occurs.
500 * We calculate the engine state size by removing the size of
501 * what comes before it in the context image (which is identical
504 ads_blob_write(guc, ads.eng_state_size[guc_class],
505 real_size - LRC_SKIP_SIZE);
506 ads_blob_write(guc, ads.golden_context_lrca[guc_class],
509 addr_ggtt += alloc_size;
512 /* Make sure current size matches what we calculated previously */
513 if (guc->ads_golden_ctxt_size)
514 GEM_BUG_ON(guc->ads_golden_ctxt_size != total_size);
519 static struct intel_engine_cs *find_engine_state(struct intel_gt *gt, u8 engine_class)
521 struct intel_engine_cs *engine;
522 enum intel_engine_id id;
524 for_each_engine(engine, gt, id) {
525 if (engine->class != engine_class)
528 if (!engine->default_state)
537 static void guc_init_golden_context(struct intel_guc *guc)
539 struct intel_engine_cs *engine;
540 struct intel_gt *gt = guc_to_gt(guc);
541 unsigned long offset;
542 u32 addr_ggtt, total_size = 0, alloc_size, real_size;
543 u8 engine_class, guc_class;
545 if (!intel_uc_uses_guc_submission(>->uc))
548 GEM_BUG_ON(iosys_map_is_null(&guc->ads_map));
551 * Go back and fill in the golden context data now that it is
554 offset = guc_ads_golden_ctxt_offset(guc);
555 addr_ggtt = intel_guc_ggtt_offset(guc, guc->ads_vma) + offset;
557 for (engine_class = 0; engine_class <= MAX_ENGINE_CLASS; ++engine_class) {
558 if (engine_class == OTHER_CLASS)
561 guc_class = engine_class_to_guc_class(engine_class);
562 if (!ads_blob_read(guc, system_info.engine_enabled_masks[guc_class]))
565 real_size = intel_engine_context_size(gt, engine_class);
566 alloc_size = PAGE_ALIGN(real_size);
567 total_size += alloc_size;
569 engine = find_engine_state(gt, engine_class);
571 drm_err(>->i915->drm, "No engine state recorded for class %d!\n",
573 ads_blob_write(guc, ads.eng_state_size[guc_class], 0);
574 ads_blob_write(guc, ads.golden_context_lrca[guc_class], 0);
578 GEM_BUG_ON(ads_blob_read(guc, ads.eng_state_size[guc_class]) !=
579 real_size - LRC_SKIP_SIZE);
580 GEM_BUG_ON(ads_blob_read(guc, ads.golden_context_lrca[guc_class]) != addr_ggtt);
582 addr_ggtt += alloc_size;
584 shmem_read_to_iosys_map(engine->default_state, 0, &guc->ads_map,
586 offset += alloc_size;
589 GEM_BUG_ON(guc->ads_golden_ctxt_size != total_size);
592 static void guc_capture_list_init(struct intel_guc *guc)
595 u32 addr_ggtt, offset;
597 offset = guc_ads_capture_offset(guc);
598 addr_ggtt = intel_guc_ggtt_offset(guc, guc->ads_vma) + offset;
600 /* FIXME: Populate a proper capture list */
602 for (i = 0; i < GUC_CAPTURE_LIST_INDEX_MAX; i++) {
603 for (j = 0; j < GUC_MAX_ENGINE_CLASSES; j++) {
604 ads_blob_write(guc, ads.capture_instance[i][j], addr_ggtt);
605 ads_blob_write(guc, ads.capture_class[i][j], addr_ggtt);
608 ads_blob_write(guc, ads.capture_global[i], addr_ggtt);
612 static void __guc_ads_init(struct intel_guc *guc)
614 struct intel_gt *gt = guc_to_gt(guc);
615 struct drm_i915_private *i915 = gt->i915;
616 struct iosys_map info_map = IOSYS_MAP_INIT_OFFSET(&guc->ads_map,
617 offsetof(struct __guc_ads_blob, system_info));
620 /* GuC scheduling policies */
621 guc_policies_init(guc);
624 fill_engine_enable_masks(gt, &info_map);
626 ads_blob_write(guc, system_info.generic_gt_sysinfo[GUC_GENERIC_GT_SYSINFO_SLICE_ENABLED],
627 hweight8(gt->info.sseu.slice_mask));
628 ads_blob_write(guc, system_info.generic_gt_sysinfo[GUC_GENERIC_GT_SYSINFO_VDBOX_SFC_SUPPORT_MASK],
629 gt->info.vdbox_sfc_access);
631 if (GRAPHICS_VER(i915) >= 12 && !IS_DGFX(i915)) {
632 u32 distdbreg = intel_uncore_read(gt->uncore,
633 GEN12_DIST_DBS_POPULATED);
635 system_info.generic_gt_sysinfo[GUC_GENERIC_GT_SYSINFO_DOORBELL_COUNT_PER_SQIDI],
636 ((distdbreg >> GEN12_DOORBELLS_PER_SQIDI_SHIFT)
637 & GEN12_DOORBELLS_PER_SQIDI) + 1);
640 /* Golden contexts for re-initialising after a watchdog reset */
641 guc_prep_golden_context(guc);
643 guc_mapping_table_init(guc_to_gt(guc), &info_map);
645 base = intel_guc_ggtt_offset(guc, guc->ads_vma);
647 /* Capture list for hang debug */
648 guc_capture_list_init(guc);
651 ads_blob_write(guc, ads.scheduler_policies, base +
652 offsetof(struct __guc_ads_blob, policies));
653 ads_blob_write(guc, ads.gt_system_info, base +
654 offsetof(struct __guc_ads_blob, system_info));
656 /* MMIO save/restore list */
657 guc_mmio_reg_state_init(guc);
660 ads_blob_write(guc, ads.private_data, base +
661 guc_ads_private_data_offset(guc));
663 i915_gem_object_flush_map(guc->ads_vma->obj);
667 * intel_guc_ads_create() - allocates and initializes GuC ADS.
668 * @guc: intel_guc struct
670 * GuC needs memory block (Additional Data Struct), where it will store
671 * some data. Allocate and initialize such memory block for GuC use.
673 int intel_guc_ads_create(struct intel_guc *guc)
679 GEM_BUG_ON(guc->ads_vma);
682 * Create reg state size dynamically on system memory to be copied to
683 * the final ads blob on gt init/reset
685 ret = guc_mmio_reg_state_create(guc);
688 guc->ads_regset_size = ret;
690 /* Likewise the golden contexts: */
691 ret = guc_prep_golden_context(guc);
694 guc->ads_golden_ctxt_size = ret;
696 /* Now the total size can be determined: */
697 size = guc_ads_blob_size(guc);
699 ret = intel_guc_allocate_and_map_vma(guc, size, &guc->ads_vma,
704 if (i915_gem_object_is_lmem(guc->ads_vma->obj))
705 iosys_map_set_vaddr_iomem(&guc->ads_map, (void __iomem *)ads_blob);
707 iosys_map_set_vaddr(&guc->ads_map, ads_blob);
714 void intel_guc_ads_init_late(struct intel_guc *guc)
717 * The golden context setup requires the saved engine state from
718 * __engines_record_defaults(). However, that requires engines to be
719 * operational which means the ADS must already have been configured.
720 * Fortunately, the golden context state is not needed until a hang
721 * occurs, so it can be filled in during this late init phase.
723 guc_init_golden_context(guc);
726 void intel_guc_ads_destroy(struct intel_guc *guc)
728 i915_vma_unpin_and_release(&guc->ads_vma, I915_VMA_RELEASE_MAP);
729 iosys_map_clear(&guc->ads_map);
730 kfree(guc->ads_regset);
733 static void guc_ads_private_data_reset(struct intel_guc *guc)
737 size = guc_ads_private_data_size(guc);
741 iosys_map_memset(&guc->ads_map, guc_ads_private_data_offset(guc),
746 * intel_guc_ads_reset() - prepares GuC Additional Data Struct for reuse
747 * @guc: intel_guc struct
749 * GuC stores some data in ADS, which might be stale after a reset.
750 * Reinitialize whole ADS in case any part of it was corrupted during
753 void intel_guc_ads_reset(struct intel_guc *guc)
760 guc_ads_private_data_reset(guc);
763 u32 intel_guc_engine_usage_offset(struct intel_guc *guc)
765 return intel_guc_ggtt_offset(guc, guc->ads_vma) +
766 offsetof(struct __guc_ads_blob, engine_usage);
769 struct iosys_map intel_guc_engine_usage_record_map(struct intel_engine_cs *engine)
771 struct intel_guc *guc = &engine->gt->uc.guc;
772 u8 guc_class = engine_class_to_guc_class(engine->class);
773 size_t offset = offsetof(struct __guc_ads_blob,
774 engine_usage.engines[guc_class][ilog2(engine->logical_mask)]);
776 return IOSYS_MAP_INIT_OFFSET(&guc->ads_map, offset);