intel_guc_ads.c 12.8 KB
Newer Older
1
// SPDX-License-Identifier: MIT
2
/*
3
 * Copyright © 2014-2019 Intel Corporation
4 5
 */

6 7
#include <linux/bsearch.h>

8
#include "gt/intel_gt.h"
9
#include "gt/intel_lrc.h"
10
#include "intel_guc_ads.h"
11
#include "intel_guc_fwif.h"
12 13 14 15 16
#include "intel_uc.h"
#include "i915_drv.h"

/*
 * The Additional Data Struct (ADS) has pointers for different buffers used by
17 18 19 20 21 22 23 24 25 26 27
 * the GuC. One single gem object contains the ADS struct itself (guc_ads) and
 * all the extra buffers indirectly linked via the ADS struct's entries.
 *
 * Layout of the ADS blob allocated for the GuC:
 *
 *      +---------------------------------------+ <== base
 *      | guc_ads                               |
 *      +---------------------------------------+
 *      | guc_policies                          |
 *      +---------------------------------------+
 *      | guc_gt_system_info                    |
28 29 30 31 32 33
 *      +---------------------------------------+ <== static
 *      | guc_mmio_reg[countA] (engine 0.0)     |
 *      | guc_mmio_reg[countB] (engine 0.1)     |
 *      | guc_mmio_reg[countC] (engine 1.0)     |
 *      |   ...                                 |
 *      +---------------------------------------+ <== dynamic
34 35 36 37 38 39
 *      | padding                               |
 *      +---------------------------------------+ <== 4K aligned
 *      | private data                          |
 *      +---------------------------------------+
 *      | padding                               |
 *      +---------------------------------------+ <== 4K aligned
40
 */
41 42 43 44
struct __guc_ads_blob {
	struct guc_ads ads;
	struct guc_policies policies;
	struct guc_gt_system_info system_info;
45 46
	/* From here on, location is dynamic! Refer to above diagram. */
	struct guc_mmio_reg regset[0];
47 48
} __packed;

49 50 51 52 53 54
static u32 guc_ads_regset_size(struct intel_guc *guc)
{
	GEM_BUG_ON(!guc->ads_regset_size);
	return guc->ads_regset_size;
}

55 56 57 58 59
static u32 guc_ads_private_data_size(struct intel_guc *guc)
{
	return PAGE_ALIGN(guc->fw.private_data_size);
}

60 61 62 63 64
static u32 guc_ads_regset_offset(struct intel_guc *guc)
{
	return offsetof(struct __guc_ads_blob, regset);
}

65 66
static u32 guc_ads_private_data_offset(struct intel_guc *guc)
{
67 68 69 70 71
	u32 offset;

	offset = guc_ads_regset_offset(guc) +
		 guc_ads_regset_size(guc);
	return PAGE_ALIGN(offset);
72 73 74 75 76 77 78
}

static u32 guc_ads_blob_size(struct intel_guc *guc)
{
	return guc_ads_private_data_offset(guc) +
	       guc_ads_private_data_size(guc);
}
79

80
static void guc_policies_init(struct intel_guc *guc, struct guc_policies *policies)
81
{
82 83 84
	struct intel_gt *gt = guc_to_gt(guc);
	struct drm_i915_private *i915 = gt->i915;

85 86
	policies->dpc_promote_time = GLOBAL_POLICY_DEFAULT_DPC_PROMOTE_TIME_US;
	policies->max_num_work_items = GLOBAL_POLICY_MAX_NUM_WI;
87

88
	policies->global_flags = 0;
89 90 91
	if (i915->params.reset < 2)
		policies->global_flags |= GLOBAL_POLICY_DISABLE_ENGINE_RESET;

92 93 94
	policies->is_valid = 1;
}

95 96 97 98 99 100 101 102 103 104 105 106 107 108
void intel_guc_ads_print_policy_info(struct intel_guc *guc,
				     struct drm_printer *dp)
{
	struct __guc_ads_blob *blob = guc->ads_blob;

	if (unlikely(!blob))
		return;

	drm_printf(dp, "Global scheduling policies:\n");
	drm_printf(dp, "  DPC promote time   = %u\n", blob->policies.dpc_promote_time);
	drm_printf(dp, "  Max num work items = %u\n", blob->policies.max_num_work_items);
	drm_printf(dp, "  Flags              = %u\n", blob->policies.global_flags);
}

109 110 111 112 113 114 115
static int guc_action_policies_update(struct intel_guc *guc, u32 policy_offset)
{
	u32 action[] = {
		INTEL_GUC_ACTION_GLOBAL_SCHED_POLICY_CHANGE,
		policy_offset
	};

116
	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
}

int intel_guc_global_policies_update(struct intel_guc *guc)
{
	struct __guc_ads_blob *blob = guc->ads_blob;
	struct intel_gt *gt = guc_to_gt(guc);
	intel_wakeref_t wakeref;
	int ret;

	if (!blob)
		return -EOPNOTSUPP;

	GEM_BUG_ON(!blob->ads.scheduler_policies);

	guc_policies_init(guc, &blob->policies);

	if (!intel_guc_is_ready(guc))
		return 0;

	with_intel_runtime_pm(&gt->i915->runtime_pm, wakeref)
		ret = guc_action_policies_update(guc, blob->ads.scheduler_policies);

	return ret;
}

142 143 144 145 146 147 148 149 150 151 152 153 154 155
static void guc_mapping_table_init(struct intel_gt *gt,
				   struct guc_gt_system_info *system_info)
{
	unsigned int i, j;
	struct intel_engine_cs *engine;
	enum intel_engine_id id;

	/* Table must be set to invalid values for entries not used */
	for (i = 0; i < GUC_MAX_ENGINE_CLASSES; ++i)
		for (j = 0; j < GUC_MAX_INSTANCES_PER_CLASS; ++j)
			system_info->mapping_table[i][j] =
				GUC_MAX_INSTANCES_PER_CLASS;

	for_each_engine(engine, gt, id) {
156
		u8 guc_class = engine_class_to_guc_class(engine->class);
157 158 159 160 161 162

		system_info->mapping_table[guc_class][engine->instance] =
			engine->instance;
	}
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
/*
 * The save/restore register list must be pre-calculated to a temporary
 * buffer of driver defined size before it can be generated in place
 * inside the ADS.
 */
#define MAX_MMIO_REGS	128	/* Arbitrary size, increase as needed */
struct temp_regset {
	struct guc_mmio_reg *registers;
	u32 used;
	u32 size;
};

static int guc_mmio_reg_cmp(const void *a, const void *b)
{
	const struct guc_mmio_reg *ra = a;
	const struct guc_mmio_reg *rb = b;

	return (int)ra->offset - (int)rb->offset;
}

static void guc_mmio_reg_add(struct temp_regset *regset,
			     u32 offset, u32 flags)
{
	u32 count = regset->used;
	struct guc_mmio_reg reg = {
		.offset = offset,
		.flags = flags,
	};
	struct guc_mmio_reg *slot;

	GEM_BUG_ON(count >= regset->size);

	/*
	 * The mmio list is built using separate lists within the driver.
	 * It's possible that at some point we may attempt to add the same
	 * register more than once. Do not consider this an error; silently
	 * move on if the register is already in the list.
	 */
	if (bsearch(&reg, regset->registers, count,
		    sizeof(reg), guc_mmio_reg_cmp))
		return;

	slot = &regset->registers[count];
	regset->used++;
	*slot = reg;

	while (slot-- > regset->registers) {
		GEM_BUG_ON(slot[0].offset == slot[1].offset);
		if (slot[1].offset > slot[0].offset)
			break;

		swap(slot[1], slot[0]);
	}
}

#define GUC_MMIO_REG_ADD(regset, reg, masked) \
	guc_mmio_reg_add(regset, \
			 i915_mmio_reg_offset((reg)), \
			 (masked) ? GUC_REGSET_MASKED : 0)

static void guc_mmio_regset_init(struct temp_regset *regset,
				 struct intel_engine_cs *engine)
{
	const u32 base = engine->mmio_base;
	struct i915_wa_list *wal = &engine->wa_list;
	struct i915_wa *wa;
	unsigned int i;

	regset->used = 0;

	GUC_MMIO_REG_ADD(regset, RING_MODE_GEN7(base), true);
	GUC_MMIO_REG_ADD(regset, RING_HWS_PGA(base), false);
	GUC_MMIO_REG_ADD(regset, RING_IMR(base), false);

	for (i = 0, wa = wal->list; i < wal->count; i++, wa++)
		GUC_MMIO_REG_ADD(regset, wa->reg, wa->masked_reg);

	/* Be extra paranoid and include all whitelist registers. */
	for (i = 0; i < RING_MAX_NONPRIV_SLOTS; i++)
		GUC_MMIO_REG_ADD(regset,
				 RING_FORCE_TO_NONPRIV(base, i),
				 false);

	/* add in local MOCS registers */
	for (i = 0; i < GEN9_LNCFCMOCS_REG_COUNT; i++)
		GUC_MMIO_REG_ADD(regset, GEN9_LNCFCMOCS(i), false);
}

static int guc_mmio_reg_state_query(struct intel_guc *guc)
{
	struct intel_gt *gt = guc_to_gt(guc);
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
	struct temp_regset temp_set;
	u32 total;

	/*
	 * Need to actually build the list in order to filter out
	 * duplicates and other such data dependent constructions.
	 */
	temp_set.size = MAX_MMIO_REGS;
	temp_set.registers = kmalloc_array(temp_set.size,
					   sizeof(*temp_set.registers),
					   GFP_KERNEL);
	if (!temp_set.registers)
		return -ENOMEM;

	total = 0;
	for_each_engine(engine, gt, id) {
		guc_mmio_regset_init(&temp_set, engine);
		total += temp_set.used;
	}

	kfree(temp_set.registers);

	return total * sizeof(struct guc_mmio_reg);
}

static void guc_mmio_reg_state_init(struct intel_guc *guc,
				    struct __guc_ads_blob *blob)
{
	struct intel_gt *gt = guc_to_gt(guc);
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
	struct temp_regset temp_set;
	struct guc_mmio_reg_set *ads_reg_set;
	u32 addr_ggtt, offset;
	u8 guc_class;

	offset = guc_ads_regset_offset(guc);
	addr_ggtt = intel_guc_ggtt_offset(guc, guc->ads_vma) + offset;
	temp_set.registers = (struct guc_mmio_reg *)(((u8 *)blob) + offset);
	temp_set.size = guc->ads_regset_size / sizeof(temp_set.registers[0]);

	for_each_engine(engine, gt, id) {
		/* Class index is checked in class converter */
		GEM_BUG_ON(engine->instance >= GUC_MAX_INSTANCES_PER_CLASS);

		guc_class = engine_class_to_guc_class(engine->class);
		ads_reg_set = &blob->ads.reg_state_list[guc_class][engine->instance];

		guc_mmio_regset_init(&temp_set, engine);
		if (!temp_set.used) {
			ads_reg_set->address = 0;
			ads_reg_set->count = 0;
			continue;
		}

		ads_reg_set->address = addr_ggtt;
		ads_reg_set->count = temp_set.used;

		temp_set.size -= temp_set.used;
		temp_set.registers += temp_set.used;
		addr_ggtt += temp_set.used * sizeof(struct guc_mmio_reg);
	}

	GEM_BUG_ON(temp_set.size);
}

322 323 324 325 326 327
/*
 * The first 80 dwords of the register state context, containing the
 * execlists and ppgtt registers.
 */
#define LR_HW_CONTEXT_SIZE	(80 * sizeof(u32))

328
static void __guc_ads_init(struct intel_guc *guc)
329
{
330
	struct intel_gt *gt = guc_to_gt(guc);
331
	struct drm_i915_private *i915 = gt->i915;
332
	struct __guc_ads_blob *blob = guc->ads_blob;
333 334
	const u32 skipped_size = LRC_PPHWSP_SZ * PAGE_SIZE + LR_HW_CONTEXT_SIZE;
	u32 base;
335
	u8 engine_class, guc_class;
336 337

	/* GuC scheduling policies */
338
	guc_policies_init(guc, &blob->policies);
339 340

	/*
341 342 343 344 345 346
	 * GuC expects a per-engine-class context image and size
	 * (minus hwsp and ring context). The context image will be
	 * used to reinitialize engines after a reset. It must exist
	 * and be pinned in the GGTT, so that the address won't change after
	 * we have told GuC where to find it. The context size will be used
	 * to validate that the LRC base + size fall within allowed GGTT.
347
	 */
348 349 350
	for (engine_class = 0; engine_class <= MAX_ENGINE_CLASS; ++engine_class) {
		if (engine_class == OTHER_CLASS)
			continue;
351 352 353

		guc_class = engine_class_to_guc_class(engine_class);

354 355 356 357
		/*
		 * TODO: Set context pointer to default state to allow
		 * GuC to re-init guilty contexts after internal reset.
		 */
358 359
		blob->ads.golden_context_lrca[guc_class] = 0;
		blob->ads.eng_state_size[guc_class] =
360
			intel_engine_context_size(gt, engine_class) -
361 362
			skipped_size;
	}
363

364
	/* System info */
365 366 367 368
	blob->system_info.engine_enabled_masks[GUC_RENDER_CLASS] = 1;
	blob->system_info.engine_enabled_masks[GUC_BLITTER_CLASS] = 1;
	blob->system_info.engine_enabled_masks[GUC_VIDEO_CLASS] = VDBOX_MASK(gt);
	blob->system_info.engine_enabled_masks[GUC_VIDEOENHANCE_CLASS] = VEBOX_MASK(gt);
369 370 371 372 373 374

	blob->system_info.generic_gt_sysinfo[GUC_GENERIC_GT_SYSINFO_SLICE_ENABLED] =
		hweight8(gt->info.sseu.slice_mask);
	blob->system_info.generic_gt_sysinfo[GUC_GENERIC_GT_SYSINFO_VDBOX_SFC_SUPPORT_MASK] =
		gt->info.vdbox_sfc_access;

375
	if (GRAPHICS_VER(i915) >= 12 && !IS_DGFX(i915)) {
376 377 378 379 380 381
		u32 distdbreg = intel_uncore_read(gt->uncore,
						  GEN12_DIST_DBS_POPULATED);
		blob->system_info.generic_gt_sysinfo[GUC_GENERIC_GT_SYSINFO_DOORBELL_COUNT_PER_SQIDI] =
			((distdbreg >> GEN12_DOORBELLS_PER_SQIDI_SHIFT) &
			 GEN12_DOORBELLS_PER_SQIDI) + 1;
	}
382

383
	guc_mapping_table_init(guc_to_gt(guc), &blob->system_info);
384

385
	base = intel_guc_ggtt_offset(guc, guc->ads_vma);
386 387

	/* ADS */
388
	blob->ads.scheduler_policies = base + ptr_offset(blob, policies);
389
	blob->ads.gt_system_info = base + ptr_offset(blob, system_info);
390

391 392 393
	/* MMIO save/restore list */
	guc_mmio_reg_state_init(guc, blob);

394 395 396
	/* Private Data */
	blob->ads.private_data = base + guc_ads_private_data_offset(guc);

397
	i915_gem_object_flush_map(guc->ads_vma->obj);
398 399 400 401 402 403 404 405 406 407 408
}

/**
 * intel_guc_ads_create() - allocates and initializes GuC ADS.
 * @guc: intel_guc struct
 *
 * GuC needs memory block (Additional Data Struct), where it will store
 * some data. Allocate and initialize such memory block for GuC use.
 */
int intel_guc_ads_create(struct intel_guc *guc)
{
409
	u32 size;
410 411 412 413
	int ret;

	GEM_BUG_ON(guc->ads_vma);

414 415 416 417 418 419
	/* Need to calculate the reg state size dynamically: */
	ret = guc_mmio_reg_state_query(guc);
	if (ret < 0)
		return ret;
	guc->ads_regset_size = ret;

420 421
	size = guc_ads_blob_size(guc);

422 423 424 425
	ret = intel_guc_allocate_and_map_vma(guc, size, &guc->ads_vma,
					     (void **)&guc->ads_blob);
	if (ret)
		return ret;
426

427
	__guc_ads_init(guc);
428 429

	return 0;
430 431 432 433
}

void intel_guc_ads_destroy(struct intel_guc *guc)
{
434
	i915_vma_unpin_and_release(&guc->ads_vma, I915_VMA_RELEASE_MAP);
435
	guc->ads_blob = NULL;
436
}
437

438 439 440 441 442 443 444 445 446 447 448 449
static void guc_ads_private_data_reset(struct intel_guc *guc)
{
	u32 size;

	size = guc_ads_private_data_size(guc);
	if (!size)
		return;

	memset((void *)guc->ads_blob + guc_ads_private_data_offset(guc), 0,
	       size);
}

450 451 452 453 454 455 456 457 458 459 460 461
/**
 * intel_guc_ads_reset() - prepares GuC Additional Data Struct for reuse
 * @guc: intel_guc struct
 *
 * GuC stores some data in ADS, which might be stale after a reset.
 * Reinitialize whole ADS in case any part of it was corrupted during
 * previous GuC run.
 */
void intel_guc_ads_reset(struct intel_guc *guc)
{
	if (!guc->ads_vma)
		return;
462

463
	__guc_ads_init(guc);
464 465

	guc_ads_private_data_reset(guc);
466
}