intel_csr.c 13.4 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
/*
 * 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.
 *
 */
#include <linux/firmware.h>
#include "i915_drv.h"
#include "i915_reg.h"

28 29 30 31 32 33 34 35 36
/**
 * DOC: csr support for dmc
 *
 * Display Context Save and Restore (CSR) firmware support added from gen9
 * onwards to drive newly added DMC (Display microcontroller) in display
 * engine to save and restore the state of display engine when it enter into
 * low-power state and comes back to normal.
 */

37
#define I915_CSR_KBL "i915/kbl_dmc_ver1_01.bin"
38 39 40
MODULE_FIRMWARE(I915_CSR_KBL);
#define KBL_CSR_VERSION_REQUIRED	CSR_VERSION(1, 1)

41
#define I915_CSR_SKL "i915/skl_dmc_ver1_26.bin"
42
MODULE_FIRMWARE(I915_CSR_SKL);
43
#define SKL_CSR_VERSION_REQUIRED	CSR_VERSION(1, 26)
44

45
#define I915_CSR_BXT "i915/bxt_dmc_ver1_07.bin"
46 47
MODULE_FIRMWARE(I915_CSR_BXT);
#define BXT_CSR_VERSION_REQUIRED	CSR_VERSION(1, 7)
48

49 50
#define FIRMWARE_URL  "https://01.org/linuxgraphics/intel-linux-graphics-firmwares"

51

52

53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
#define CSR_MAX_FW_SIZE			0x2FFF
#define CSR_DEFAULT_FW_OFFSET		0xFFFFFFFF

struct intel_css_header {
	/* 0x09 for DMC */
	uint32_t module_type;

	/* Includes the DMC specific header in dwords */
	uint32_t header_len;

	/* always value would be 0x10000 */
	uint32_t header_ver;

	/* Not used */
	uint32_t module_id;

	/* Not used */
	uint32_t module_vendor;

	/* in YYYYMMDD format */
	uint32_t date;

	/* Size in dwords (CSS_Headerlen + PackageHeaderLen + dmc FWsLen)/4 */
	uint32_t size;

	/* Not used */
	uint32_t key_size;

	/* Not used */
	uint32_t modulus_size;

	/* Not used */
	uint32_t exponent_size;

	/* Not used */
	uint32_t reserved1[12];

	/* Major Minor */
	uint32_t version;

	/* Not used */
	uint32_t reserved2[8];

	/* Not used */
	uint32_t kernel_header_info;
} __packed;

struct intel_fw_info {
	uint16_t reserved1;

	/* Stepping (A, B, C, ..., *). * is a wildcard */
	char stepping;

	/* Sub-stepping (0, 1, ..., *). * is a wildcard */
	char substepping;

	uint32_t offset;
	uint32_t reserved2;
} __packed;

struct intel_package_header {
	/* DMC container header length in dwords */
	unsigned char header_len;

	/* always value would be 0x01 */
	unsigned char header_ver;

	unsigned char reserved[10];

	/* Number of valid entries in the FWInfo array below */
	uint32_t num_entries;

	struct intel_fw_info fw_info[20];
} __packed;

struct intel_dmc_header {
	/* always value would be 0x40403E3E */
	uint32_t signature;

	/* DMC binary header length */
	unsigned char header_len;

	/* 0x01 */
	unsigned char header_ver;

	/* Reserved */
	uint16_t dmcc_ver;

	/* Major, Minor */
	uint32_t	project;

	/* Firmware program size (excluding header) in dwords */
	uint32_t	fw_size;

	/* Major Minor version */
	uint32_t fw_version;

	/* Number of valid MMIO cycles present. */
	uint32_t mmio_count;

	/* MMIO address */
	uint32_t mmioaddr[8];

	/* MMIO data */
	uint32_t mmiodata[8];

	/* FW filename  */
	unsigned char dfile[32];

	uint32_t reserved1[2];
} __packed;

struct stepping_info {
	char stepping;
	char substepping;
};

static const struct stepping_info skl_stepping_info[] = {
172 173
	{'A', '0'}, {'B', '0'}, {'C', '0'},
	{'D', '0'}, {'E', '0'}, {'F', '0'},
174 175
	{'G', '0'}, {'H', '0'}, {'I', '0'},
	{'J', '0'}, {'K', '0'}
176 177
};

J
Jani Nikula 已提交
178
static const struct stepping_info bxt_stepping_info[] = {
179 180 181 182
	{'A', '0'}, {'A', '1'}, {'A', '2'},
	{'B', '0'}, {'B', '1'}, {'B', '2'}
};

183 184 185 186
static const struct stepping_info no_stepping_info = { '*', '*' };

static const struct stepping_info *
intel_get_stepping_info(struct drm_i915_private *dev_priv)
187
{
188 189 190
	const struct stepping_info *si;
	unsigned int size;

191
	if (IS_SKYLAKE(dev_priv)) {
192 193
		size = ARRAY_SIZE(skl_stepping_info);
		si = skl_stepping_info;
194
	} else if (IS_BROXTON(dev_priv)) {
195 196 197
		size = ARRAY_SIZE(bxt_stepping_info);
		si = bxt_stepping_info;
	} else {
198
		size = 0;
199
	}
200

201 202
	if (INTEL_REVID(dev_priv) < size)
		return si + INTEL_REVID(dev_priv);
203

204
	return &no_stepping_info;
205 206
}

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
static void gen9_set_dc_state_debugmask(struct drm_i915_private *dev_priv)
{
	uint32_t val, mask;

	mask = DC_STATE_DEBUG_MASK_MEMORY_UP;

	if (IS_BROXTON(dev_priv))
		mask |= DC_STATE_DEBUG_MASK_CORES;

	/* The below bit doesn't need to be cleared ever afterwards */
	val = I915_READ(DC_STATE_DEBUG);
	if ((val & mask) != mask) {
		val |= mask;
		I915_WRITE(DC_STATE_DEBUG, val);
		POSTING_READ(DC_STATE_DEBUG);
	}
}

225 226
/**
 * intel_csr_load_program() - write the firmware from memory to register.
227
 * @dev_priv: i915 drm device.
228 229 230 231 232
 *
 * CSR firmware is read from a .bin file and kept in internal memory one time.
 * Everytime display comes back from low power state this function is called to
 * copy the firmware from internal memory to registers.
 */
233
void intel_csr_load_program(struct drm_i915_private *dev_priv)
234
{
235
	u32 *payload = dev_priv->csr.dmc_payload;
236 237
	uint32_t i, fw_size;

238
	if (!IS_GEN9(dev_priv)) {
239
		DRM_ERROR("No CSR support available for this platform\n");
240
		return;
241 242
	}

243 244
	if (!dev_priv->csr.dmc_payload) {
		DRM_ERROR("Tried to program CSR with empty payload\n");
245
		return;
246
	}
247

248 249
	fw_size = dev_priv->csr.dmc_fw_size;
	for (i = 0; i < fw_size; i++)
250
		I915_WRITE(CSR_PROGRAM(i), payload[i]);
251 252 253

	for (i = 0; i < dev_priv->csr.mmio_count; i++) {
		I915_WRITE(dev_priv->csr.mmioaddr[i],
254
			   dev_priv->csr.mmiodata[i]);
255
	}
256 257

	dev_priv->csr.dc_state = 0;
258

259
	gen9_set_dc_state_debugmask(dev_priv);
260 261
}

262 263
static uint32_t *parse_csr_fw(struct drm_i915_private *dev_priv,
			      const struct firmware *fw)
264 265 266 267 268
{
	struct intel_css_header *css_header;
	struct intel_package_header *package_header;
	struct intel_dmc_header *dmc_header;
	struct intel_csr *csr = &dev_priv->csr;
269
	const struct stepping_info *si = intel_get_stepping_info(dev_priv);
270 271
	uint32_t dmc_offset = CSR_DEFAULT_FW_OFFSET, readcount = 0, nbytes;
	uint32_t i;
272
	uint32_t *dmc_payload;
273
	uint32_t required_version;
274

275
	if (!fw)
276
		return NULL;
277 278 279 280

	/* Extract CSS Header information*/
	css_header = (struct intel_css_header *)fw->data;
	if (sizeof(struct intel_css_header) !=
281
	    (css_header->header_len * 4)) {
282
		DRM_ERROR("Firmware has wrong CSS header length %u bytes\n",
283
			  (css_header->header_len * 4));
284
		return NULL;
285
	}
286 287 288

	csr->version = css_header->version;

289
	if (IS_KABYLAKE(dev_priv)) {
290
		required_version = KBL_CSR_VERSION_REQUIRED;
291
	} else if (IS_SKYLAKE(dev_priv)) {
292
		required_version = SKL_CSR_VERSION_REQUIRED;
293
	} else if (IS_BROXTON(dev_priv)) {
294
		required_version = BXT_CSR_VERSION_REQUIRED;
295 296
	} else {
		MISSING_CASE(INTEL_REVID(dev_priv));
297
		required_version = 0;
298 299
	}

300 301 302
	if (csr->version != required_version) {
		DRM_INFO("Refusing to load DMC firmware v%u.%u,"
			 " please use v%u.%u [" FIRMWARE_URL "].\n",
303 304
			 CSR_VERSION_MAJOR(csr->version),
			 CSR_VERSION_MINOR(csr->version),
305 306
			 CSR_VERSION_MAJOR(required_version),
			 CSR_VERSION_MINOR(required_version));
307
		return NULL;
308 309
	}

310 311 312 313
	readcount += sizeof(struct intel_css_header);

	/* Extract Package Header information*/
	package_header = (struct intel_package_header *)
314
		&fw->data[readcount];
315
	if (sizeof(struct intel_package_header) !=
316
	    (package_header->header_len * 4)) {
317
		DRM_ERROR("Firmware has wrong package header length %u bytes\n",
318
			  (package_header->header_len * 4));
319
		return NULL;
320 321 322 323 324 325
	}
	readcount += sizeof(struct intel_package_header);

	/* Search for dmc_offset to find firware binary. */
	for (i = 0; i < package_header->num_entries; i++) {
		if (package_header->fw_info[i].substepping == '*' &&
326
		    si->stepping == package_header->fw_info[i].stepping) {
327 328
			dmc_offset = package_header->fw_info[i].offset;
			break;
329 330
		} else if (si->stepping == package_header->fw_info[i].stepping &&
			   si->substepping == package_header->fw_info[i].substepping) {
331 332 333
			dmc_offset = package_header->fw_info[i].offset;
			break;
		} else if (package_header->fw_info[i].stepping == '*' &&
334
			   package_header->fw_info[i].substepping == '*')
335 336 337
			dmc_offset = package_header->fw_info[i].offset;
	}
	if (dmc_offset == CSR_DEFAULT_FW_OFFSET) {
338 339
		DRM_ERROR("Firmware not supported for %c stepping\n",
			  si->stepping);
340
		return NULL;
341 342 343 344 345 346 347
	}
	readcount += dmc_offset;

	/* Extract dmc_header information. */
	dmc_header = (struct intel_dmc_header *)&fw->data[readcount];
	if (sizeof(struct intel_dmc_header) != (dmc_header->header_len)) {
		DRM_ERROR("Firmware has wrong dmc header length %u bytes\n",
348
			  (dmc_header->header_len));
349
		return NULL;
350 351 352 353 354 355
	}
	readcount += sizeof(struct intel_dmc_header);

	/* Cache the dmc header info. */
	if (dmc_header->mmio_count > ARRAY_SIZE(csr->mmioaddr)) {
		DRM_ERROR("Firmware has wrong mmio count %u\n",
356
			  dmc_header->mmio_count);
357
		return NULL;
358 359 360
	}
	csr->mmio_count = dmc_header->mmio_count;
	for (i = 0; i < dmc_header->mmio_count; i++) {
361
		if (dmc_header->mmioaddr[i] < CSR_MMIO_START_RANGE ||
362
		    dmc_header->mmioaddr[i] > CSR_MMIO_END_RANGE) {
363
			DRM_ERROR(" Firmware has wrong mmio address 0x%x\n",
364
				  dmc_header->mmioaddr[i]);
365
			return NULL;
366
		}
367
		csr->mmioaddr[i] = _MMIO(dmc_header->mmioaddr[i]);
368 369 370 371 372 373 374
		csr->mmiodata[i] = dmc_header->mmiodata[i];
	}

	/* fw_size is in dwords, so multiplied by 4 to convert into bytes. */
	nbytes = dmc_header->fw_size * 4;
	if (nbytes > CSR_MAX_FW_SIZE) {
		DRM_ERROR("CSR firmware too big (%u) bytes\n", nbytes);
375
		return NULL;
376 377 378
	}
	csr->dmc_fw_size = dmc_header->fw_size;

379 380
	dmc_payload = kmalloc(nbytes, GFP_KERNEL);
	if (!dmc_payload) {
381
		DRM_ERROR("Memory allocation failed for dmc payload\n");
382
		return NULL;
383 384
	}

385
	return memcpy(dmc_payload, &fw->data[readcount], nbytes);
386 387
}

388
static void csr_load_work_fn(struct work_struct *work)
389
{
390 391
	struct drm_i915_private *dev_priv;
	struct intel_csr *csr;
392
	const struct firmware *fw = NULL;
393 394 395 396
	int ret;

	dev_priv = container_of(work, typeof(*dev_priv), csr.work);
	csr = &dev_priv->csr;
397

398
	ret = request_firmware(&fw, dev_priv->csr.fw_path,
399
			       &dev_priv->drm.pdev->dev);
400 401
	if (fw)
		dev_priv->csr.dmc_payload = parse_csr_fw(dev_priv, fw);
402 403

	if (dev_priv->csr.dmc_payload) {
404 405
		intel_csr_load_program(dev_priv);

406
		intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
407

408
		DRM_INFO("Finished loading DMC firmware %s (v%u.%u)\n",
409 410 411 412
			 dev_priv->csr.fw_path,
			 CSR_VERSION_MAJOR(csr->version),
			 CSR_VERSION_MINOR(csr->version));
	} else {
413
		dev_notice(dev_priv->drm.dev,
414 415 416
			   "Failed to load DMC firmware"
			   " [" FIRMWARE_URL "],"
			   " disabling runtime power management.\n");
417 418
	}

419 420 421
	release_firmware(fw);
}

422 423
/**
 * intel_csr_ucode_init() - initialize the firmware loading.
424
 * @dev_priv: i915 drm device.
425 426 427 428
 *
 * This function is called at the time of loading the display driver to read
 * firmware from a .bin file and copied into a internal memory.
 */
429
void intel_csr_ucode_init(struct drm_i915_private *dev_priv)
430 431
{
	struct intel_csr *csr = &dev_priv->csr;
432 433

	INIT_WORK(&dev_priv->csr.work, csr_load_work_fn);
434

435
	if (!HAS_CSR(dev_priv))
436 437
		return;

438 439 440
	if (IS_KABYLAKE(dev_priv))
		csr->fw_path = I915_CSR_KBL;
	else if (IS_SKYLAKE(dev_priv))
441
		csr->fw_path = I915_CSR_SKL;
442 443
	else if (IS_BROXTON(dev_priv))
		csr->fw_path = I915_CSR_BXT;
444 445 446 447 448
	else {
		DRM_ERROR("Unexpected: no known CSR firmware for platform\n");
		return;
	}

449 450
	DRM_DEBUG_KMS("Loading %s\n", csr->fw_path);

451 452 453 454
	/*
	 * Obtain a runtime pm reference, until CSR is loaded,
	 * to avoid entering runtime-suspend.
	 */
455
	intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
456

457
	schedule_work(&dev_priv->csr.work);
458 459
}

460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
/**
 * intel_csr_ucode_suspend() - prepare CSR firmware before system suspend
 * @dev_priv: i915 drm device
 *
 * Prepare the DMC firmware before entering system suspend. This includes
 * flushing pending work items and releasing any resources acquired during
 * init.
 */
void intel_csr_ucode_suspend(struct drm_i915_private *dev_priv)
{
	if (!HAS_CSR(dev_priv))
		return;

	flush_work(&dev_priv->csr.work);

	/* Drop the reference held in case DMC isn't loaded. */
	if (!dev_priv->csr.dmc_payload)
		intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
}

/**
 * intel_csr_ucode_resume() - init CSR firmware during system resume
 * @dev_priv: i915 drm device
 *
 * Reinitialize the DMC firmware during system resume, reacquiring any
 * resources released in intel_csr_ucode_suspend().
 */
void intel_csr_ucode_resume(struct drm_i915_private *dev_priv)
{
	if (!HAS_CSR(dev_priv))
		return;

	/*
	 * Reacquire the reference to keep RPM disabled in case DMC isn't
	 * loaded.
	 */
	if (!dev_priv->csr.dmc_payload)
		intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
}

500 501
/**
 * intel_csr_ucode_fini() - unload the CSR firmware.
502
 * @dev_priv: i915 drm device.
503
 *
504
 * Firmmware unloading includes freeing the internal memory and reset the
505 506
 * firmware loading status.
 */
507
void intel_csr_ucode_fini(struct drm_i915_private *dev_priv)
508
{
509
	if (!HAS_CSR(dev_priv))
510 511
		return;

512
	intel_csr_ucode_suspend(dev_priv);
513

514 515
	kfree(dev_priv->csr.dmc_payload);
}