intel_guc_loader.c 13.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * Copyright © 2014 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *    Vinit Azad <vinit.azad@intel.com>
 *    Ben Widawsky <ben@bwidawsk.net>
 *    Dave Gordon <david.s.gordon@intel.com>
 *    Alex Dai <yu.dai@intel.com>
 */
#include "i915_drv.h"
30
#include "intel_uc.h"
31 32

/**
A
Alex Dai 已提交
33
 * DOC: GuC-specific firmware loader
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
 *
 * intel_guc:
 * Top level structure of guc. It handles firmware loading and manages client
 * pool and doorbells. intel_guc owns a i915_guc_client to replace the legacy
 * ExecList submission.
 *
 * Firmware versioning:
 * The firmware build process will generate a version header file with major and
 * minor version defined. The versions are built into CSS header of firmware.
 * i915 kernel driver set the minimal firmware version required per platform.
 * The firmware installation package will install (symbolic link) proper version
 * of firmware.
 *
 * GuC address space:
 * GuC does not allow any gfx GGTT address that falls into range [0, WOPCM_TOP),
 * which is reserved for Boot ROM, SRAM and WOPCM. Currently this top address is
 * 512K. In order to exclude 0-512K address space from GGTT, all gfx objects
 * used by GuC is pinned with PIN_OFFSET_BIAS along with size of WOPCM.
 *
 */

55 56 57 58 59 60 61 62 63 64 65 66 67
#define SKL_FW_MAJOR 6
#define SKL_FW_MINOR 1

#define BXT_FW_MAJOR 8
#define BXT_FW_MINOR 7

#define KBL_FW_MAJOR 9
#define KBL_FW_MINOR 14

#define GUC_FW_PATH(platform, major, minor) \
       "i915/" __stringify(platform) "_guc_ver" __stringify(major) "_" __stringify(minor) ".bin"

#define I915_SKL_GUC_UCODE GUC_FW_PATH(skl, SKL_FW_MAJOR, SKL_FW_MINOR)
68 69
MODULE_FIRMWARE(I915_SKL_GUC_UCODE);

70
#define I915_BXT_GUC_UCODE GUC_FW_PATH(bxt, BXT_FW_MAJOR, BXT_FW_MINOR)
71 72
MODULE_FIRMWARE(I915_BXT_GUC_UCODE);

73
#define I915_KBL_GUC_UCODE GUC_FW_PATH(kbl, KBL_FW_MAJOR, KBL_FW_MINOR)
74 75
MODULE_FIRMWARE(I915_KBL_GUC_UCODE);

76
/* User-friendly representation of an enum */
77
const char *intel_uc_fw_status_repr(enum intel_uc_fw_status status)
78 79
{
	switch (status) {
80
	case INTEL_UC_FIRMWARE_FAIL:
81
		return "FAIL";
82
	case INTEL_UC_FIRMWARE_NONE:
83
		return "NONE";
84
	case INTEL_UC_FIRMWARE_PENDING:
85
		return "PENDING";
86
	case INTEL_UC_FIRMWARE_SUCCESS:
87 88 89 90 91 92 93 94 95 96 97 98 99 100
		return "SUCCESS";
	default:
		return "UNKNOWN!";
	}
};

static u32 get_gttype(struct drm_i915_private *dev_priv)
{
	/* XXX: GT type based on PCI device ID? field seems unused by fw */
	return 0;
}

static u32 get_core_family(struct drm_i915_private *dev_priv)
{
101 102 103
	u32 gen = INTEL_GEN(dev_priv);

	switch (gen) {
104 105 106 107
	case 9:
		return GFXCORE_FAMILY_GEN9;

	default:
108
		WARN(1, "GEN%d does not support GuC operation!\n", gen);
109 110 111 112
		return GFXCORE_FAMILY_UNKNOWN;
	}
}

113 114 115 116 117 118
/*
 * Initialise the GuC parameter block before starting the firmware
 * transfer. These parameters are read by the firmware on startup
 * and cannot be changed thereafter.
 */
static void guc_params_init(struct drm_i915_private *dev_priv)
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
{
	struct intel_guc *guc = &dev_priv->guc;
	u32 params[GUC_CTL_MAX_DWORDS];
	int i;

	memset(&params, 0, sizeof(params));

	params[GUC_CTL_DEVICE_INFO] |=
		(get_gttype(dev_priv) << GUC_CTL_GTTYPE_SHIFT) |
		(get_core_family(dev_priv) << GUC_CTL_COREFAMILY_SHIFT);

	/*
	 * GuC ARAT increment is 10 ns. GuC default scheduler quantum is one
	 * second. This ARAR is calculated by:
	 * Scheduler-Quantum-in-ns / ARAT-increment-in-ns = 1000000000 / 10
	 */
	params[GUC_CTL_ARAT_HIGH] = 0;
	params[GUC_CTL_ARAT_LOW] = 100000000;

	params[GUC_CTL_WA] |= GUC_CTL_WA_UK_BY_DRIVER;

	params[GUC_CTL_FEATURE] |= GUC_CTL_DISABLE_SCHEDULER |
			GUC_CTL_VCS2_ENABLED;

143
	params[GUC_CTL_LOG_PARAMS] = guc->log.flags;
144

145 146 147
	if (i915.guc_log_level >= 0) {
		params[GUC_CTL_DEBUG] =
			i915.guc_log_level << GUC_LOG_VERBOSITY_SHIFT;
148 149
	} else
		params[GUC_CTL_DEBUG] = GUC_LOG_DISABLED;
150

151
	if (guc->ads_vma) {
152
		u32 ads = guc_ggtt_offset(guc->ads_vma) >> PAGE_SHIFT;
153 154 155 156
		params[GUC_CTL_DEBUG] |= ads << GUC_ADS_ADDR_SHIFT;
		params[GUC_CTL_DEBUG] |= GUC_ADS_ENABLED;
	}

157 158
	/* If GuC submission is enabled, set up additional parameters here */
	if (i915.enable_guc_submission) {
159
		u32 pgs = guc_ggtt_offset(dev_priv->guc.ctx_pool_vma);
160 161 162 163 164 165 166 167 168 169 170 171
		u32 ctx_in_16 = GUC_MAX_GPU_CONTEXTS / 16;

		pgs >>= PAGE_SHIFT;
		params[GUC_CTL_CTXINFO] = (pgs << GUC_CTL_BASE_ADDR_SHIFT) |
			(ctx_in_16 << GUC_CTL_CTXNUM_IN16_SHIFT);

		params[GUC_CTL_FEATURE] |= GUC_CTL_KERNEL_SUBMISSIONS;

		/* Unmask this bit to enable the GuC's internal scheduler */
		params[GUC_CTL_FEATURE] &= ~GUC_CTL_DISABLE_SCHEDULER;
	}

172 173 174 175 176 177 178 179 180 181 182 183
	I915_WRITE(SOFT_SCRATCH(0), 0);

	for (i = 0; i < GUC_CTL_MAX_DWORDS; i++)
		I915_WRITE(SOFT_SCRATCH(1 + i), params[i]);
}

/*
 * Read the GuC status register (GUC_STATUS) and store it in the
 * specified location; then return a boolean indicating whether
 * the value matches either of two values representing completion
 * of the GuC boot process.
 *
184
 * This is used for polling the GuC status in a wait_for()
185 186 187 188 189 190
 * loop below.
 */
static inline bool guc_ucode_response(struct drm_i915_private *dev_priv,
				      u32 *status)
{
	u32 val = I915_READ(GUC_STATUS);
191
	u32 uk_val = val & GS_UKERNEL_MASK;
192
	*status = val;
193 194
	return (uk_val == GS_UKERNEL_READY ||
		((val & GS_MIA_CORE_STATE) && uk_val == GS_UKERNEL_LAPIC_DONE));
195 196 197 198 199 200 201 202 203 204 205 206
}

/*
 * Transfer the firmware image to RAM for execution by the microcontroller.
 *
 * Architecturally, the DMA engine is bidirectional, and can potentially even
 * transfer between GTT locations. This functionality is left out of the API
 * for now as there is no need for it.
 *
 * Note that GuC needs the CSS header plus uKernel code to be copied by the
 * DMA engine in one operation, whereas the RSA signature is loaded via MMIO.
 */
C
Chris Wilson 已提交
207 208
static int guc_ucode_xfer_dma(struct drm_i915_private *dev_priv,
			      struct i915_vma *vma)
209
{
210
	struct intel_uc_fw *guc_fw = &dev_priv->guc.fw;
211
	unsigned long offset;
C
Chris Wilson 已提交
212
	struct sg_table *sg = vma->pages;
A
Alex Dai 已提交
213
	u32 status, rsa[UOS_RSA_SCRATCH_MAX_COUNT];
214 215
	int i, ret = 0;

A
Alex Dai 已提交
216 217
	/* where RSA signature starts */
	offset = guc_fw->rsa_offset;
218 219

	/* Copy RSA signature from the fw image to HW for verification */
A
Alex Dai 已提交
220 221
	sg_pcopy_to_buffer(sg->sgl, sg->nents, rsa, sizeof(rsa), offset);
	for (i = 0; i < UOS_RSA_SCRATCH_MAX_COUNT; i++)
222
		I915_WRITE(UOS_RSA_SCRATCH(i), rsa[i]);
223

A
Alex Dai 已提交
224 225 226 227
	/* The header plus uCode will be copied to WOPCM via DMA, excluding any
	 * other components */
	I915_WRITE(DMA_COPY_SIZE, guc_fw->header_size + guc_fw->ucode_size);

228
	/* Set the source address for the new blob */
229
	offset = guc_ggtt_offset(vma) + guc_fw->header_offset;
230 231 232 233 234 235 236 237 238 239 240 241 242 243
	I915_WRITE(DMA_ADDR_0_LOW, lower_32_bits(offset));
	I915_WRITE(DMA_ADDR_0_HIGH, upper_32_bits(offset) & 0xFFFF);

	/*
	 * Set the DMA destination. Current uCode expects the code to be
	 * loaded at 8k; locations below this are used for the stack.
	 */
	I915_WRITE(DMA_ADDR_1_LOW, 0x2000);
	I915_WRITE(DMA_ADDR_1_HIGH, DMA_ADDRESS_SPACE_WOPCM);

	/* Finally start the DMA */
	I915_WRITE(DMA_CTRL, _MASKED_BIT_ENABLE(UOS_MOVE | START_DMA));

	/*
244
	 * Wait for the DMA to complete & the GuC to start up.
245 246 247 248 249 250
	 * NB: Docs recommend not using the interrupt for completion.
	 * Measurements indicate this should take no more than 20ms, so a
	 * timeout here indicates that the GuC has failed and is unusable.
	 * (Higher levels of the driver will attempt to fall back to
	 * execlist mode if this happens.)
	 */
251
	ret = wait_for(guc_ucode_response(dev_priv, &status), 100);
252 253 254 255 256 257 258 259 260 261 262 263 264 265

	DRM_DEBUG_DRIVER("DMA status 0x%x, GuC status 0x%x\n",
			I915_READ(DMA_CTRL), status);

	if ((status & GS_BOOTROM_MASK) == GS_BOOTROM_RSA_FAILED) {
		DRM_ERROR("GuC firmware signature verification failed\n");
		ret = -ENOEXEC;
	}

	DRM_DEBUG_DRIVER("returning %d\n", ret);

	return ret;
}

266
u32 intel_guc_wopcm_size(struct drm_i915_private *dev_priv)
267 268 269 270
{
	u32 wopcm_size = GUC_WOPCM_TOP;

	/* On BXT, the top of WOPCM is reserved for RC6 context */
271
	if (IS_GEN9_LP(dev_priv))
272 273 274 275 276
		wopcm_size -= BXT_GUC_WOPCM_RC6_RESERVED;

	return wopcm_size;
}

277 278 279 280 281
/*
 * Load the GuC firmware blob into the MinuteIA.
 */
static int guc_ucode_xfer(struct drm_i915_private *dev_priv)
{
282
	struct intel_uc_fw *guc_fw = &dev_priv->guc.fw;
C
Chris Wilson 已提交
283
	struct i915_vma *vma;
284 285
	int ret;

286
	ret = i915_gem_object_set_to_gtt_domain(guc_fw->obj, false);
287 288 289 290 291
	if (ret) {
		DRM_DEBUG_DRIVER("set-domain failed %d\n", ret);
		return ret;
	}

292
	vma = i915_gem_object_ggtt_pin(guc_fw->obj, NULL, 0, 0,
293
				       PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
C
Chris Wilson 已提交
294 295 296
	if (IS_ERR(vma)) {
		DRM_DEBUG_DRIVER("pin failed %d\n", (int)PTR_ERR(vma));
		return PTR_ERR(vma);
297 298 299 300 301
	}

	intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);

	/* init WOPCM */
302
	I915_WRITE(GUC_WOPCM_SIZE, intel_guc_wopcm_size(dev_priv));
303 304 305 306 307
	I915_WRITE(DMA_GUC_WOPCM_OFFSET, GUC_WOPCM_OFFSET_VALUE);

	/* Enable MIA caching. GuC clock gating is disabled. */
	I915_WRITE(GUC_SHIM_CONTROL, GUC_SHIM_CONTROL_VALUE);

308
	/* WaDisableMinuteIaClockGating:bxt */
309
	if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
310 311 312 313
		I915_WRITE(GUC_SHIM_CONTROL, (I915_READ(GUC_SHIM_CONTROL) &
					      ~GUC_ENABLE_MIA_CLOCK_GATING));
	}

314
	/* WaC6DisallowByGfxPause:bxt */
315
	if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
316
		I915_WRITE(GEN6_GFXPAUSE, 0x30FFF);
317

318
	if (IS_GEN9_LP(dev_priv))
319 320 321 322
		I915_WRITE(GEN9LP_GT_PM_CONFIG, GT_DOORBELL_ENABLE);
	else
		I915_WRITE(GEN9_GT_PM_CONFIG, GT_DOORBELL_ENABLE);

323
	if (IS_GEN9(dev_priv)) {
324 325 326 327
		/* DOP Clock Gating Enable for GuC clocks */
		I915_WRITE(GEN7_MISCCPCTL, (GEN8_DOP_CLOCK_GATE_GUC_ENABLE |
					    I915_READ(GEN7_MISCCPCTL)));

328
		/* allows for 5us (in 10ns units) before GT can go to RC6 */
329 330 331
		I915_WRITE(GUC_ARAT_C6DIS, 0x1FF);
	}

332
	guc_params_init(dev_priv);
333

C
Chris Wilson 已提交
334
	ret = guc_ucode_xfer_dma(dev_priv, vma);
335 336 337 338 339 340 341

	intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);

	/*
	 * We keep the object pages for reuse during resume. But we can unpin it
	 * now that DMA has completed, so it doesn't continue to take up space.
	 */
C
Chris Wilson 已提交
342
	i915_vma_unpin(vma);
343 344 345 346 347

	return ret;
}

/**
348 349
 * intel_guc_init_hw() - finish preparing the GuC for activity
 * @guc: intel_guc structure
350
 *
351
 * Called during driver loading and also after a GPU reset.
352
 *
353
 * The main action required here it to load the GuC uCode into the device.
354
 * The firmware image should have already been fetched into memory by the
355 356
 * earlier call to intel_guc_init(), so here we need only check that
 * worked, and then transfer the image to the h/w.
357 358 359
 *
 * Return:	non-zero code on error
 */
360
int intel_guc_init_hw(struct intel_guc *guc)
361
{
362 363
	struct drm_i915_private *dev_priv = guc_to_i915(guc);
	const char *fw_path = guc->fw.path;
364
	int ret;
365

366 367
	DRM_DEBUG_DRIVER("GuC fw status: path %s, fetch %s, load %s\n",
		fw_path,
368 369
		intel_uc_fw_status_repr(guc->fw.fetch_status),
		intel_uc_fw_status_repr(guc->fw.load_status));
370

371 372
	if (guc->fw.fetch_status != INTEL_UC_FIRMWARE_SUCCESS)
		return -EIO;
373

374
	guc->fw.load_status = INTEL_UC_FIRMWARE_PENDING;
375 376

	DRM_DEBUG_DRIVER("GuC fw status: fetch %s, load %s\n",
377 378
		intel_uc_fw_status_repr(guc->fw.fetch_status),
		intel_uc_fw_status_repr(guc->fw.load_status));
379

380
	ret = guc_ucode_xfer(dev_priv);
381

382 383
	if (ret)
		return -EAGAIN;
384

385
	guc->fw.load_status = INTEL_UC_FIRMWARE_SUCCESS;
386

387 388
	DRM_INFO("GuC %s (firmware %s [version %u.%u])\n",
		 i915.enable_guc_submission ? "submission enabled" : "loaded",
389 390
		 guc->fw.path,
		 guc->fw.major_ver_found, guc->fw.minor_ver_found);
391

392 393 394 395
	return 0;
}

/**
396
 * intel_guc_select_fw() - selects GuC firmware for loading
397
 * @guc:	intel_guc struct
398
 *
399
 * Return: zero when we know firmware, non-zero in other case
400
 */
401
int intel_guc_select_fw(struct intel_guc *guc)
402
{
403
	struct drm_i915_private *dev_priv = guc_to_i915(guc);
404 405 406 407 408

	guc->fw.path = NULL;
	guc->fw.fetch_status = INTEL_UC_FIRMWARE_NONE;
	guc->fw.load_status = INTEL_UC_FIRMWARE_NONE;
	guc->fw.fw = INTEL_UC_FW_TYPE_GUC;
409

410 411 412 413 414
	if (i915.guc_firmware_path) {
		guc->fw.path = i915.guc_firmware_path;
		guc->fw.major_ver_wanted = 0;
		guc->fw.minor_ver_wanted = 0;
	} else if (IS_SKYLAKE(dev_priv)) {
415
		guc->fw.path = I915_SKL_GUC_UCODE;
416 417
		guc->fw.major_ver_wanted = SKL_FW_MAJOR;
		guc->fw.minor_ver_wanted = SKL_FW_MINOR;
418
	} else if (IS_BROXTON(dev_priv)) {
419
		guc->fw.path = I915_BXT_GUC_UCODE;
420 421
		guc->fw.major_ver_wanted = BXT_FW_MAJOR;
		guc->fw.minor_ver_wanted = BXT_FW_MINOR;
422
	} else if (IS_KABYLAKE(dev_priv)) {
423
		guc->fw.path = I915_KBL_GUC_UCODE;
424 425
		guc->fw.major_ver_wanted = KBL_FW_MAJOR;
		guc->fw.minor_ver_wanted = KBL_FW_MINOR;
426
	} else {
427
		DRM_ERROR("No GuC firmware known for platform with GuC!\n");
428
		return -ENOENT;
429
	}
430

431
	return 0;
432 433 434
}

/**
435
 * intel_guc_fini() - clean up all allocated resources
436
 * @dev_priv:	i915 device private
437
 */
438
void intel_guc_fini(struct drm_i915_private *dev_priv)
439
{
440
	struct intel_uc_fw *guc_fw = &dev_priv->guc.fw;
441
	struct drm_i915_gem_object *obj;
442

443
	mutex_lock(&dev_priv->drm.struct_mutex);
444 445
	i915_guc_submission_disable(dev_priv);
	i915_guc_submission_fini(dev_priv);
446
	mutex_unlock(&dev_priv->drm.struct_mutex);
447

448 449 450 451
	obj = fetch_and_zero(&guc_fw->obj);
	if (obj)
		i915_gem_object_put(obj);

452
	guc_fw->fetch_status = INTEL_UC_FIRMWARE_NONE;
453
}