Home | History | Annotate | Line # | Download | only in i915
intel_csr.c revision 1.1.1.2
      1 /*	$NetBSD: intel_csr.c,v 1.1.1.2 2021/12/18 20:15:26 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright  2014 Intel Corporation
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice (including the next
     14  * paragraph) shall be included in all copies or substantial portions of the
     15  * Software.
     16  *
     17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     23  * IN THE SOFTWARE.
     24  *
     25  */
     26 
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: intel_csr.c,v 1.1.1.2 2021/12/18 20:15:26 riastradh Exp $");
     29 
     30 #include <linux/firmware.h>
     31 
     32 #include "i915_drv.h"
     33 #include "i915_reg.h"
     34 #include "intel_csr.h"
     35 
     36 /**
     37  * DOC: csr support for dmc
     38  *
     39  * Display Context Save and Restore (CSR) firmware support added from gen9
     40  * onwards to drive newly added DMC (Display microcontroller) in display
     41  * engine to save and restore the state of display engine when it enter into
     42  * low-power state and comes back to normal.
     43  */
     44 
     45 #define GEN12_CSR_MAX_FW_SIZE		ICL_CSR_MAX_FW_SIZE
     46 
     47 #define TGL_CSR_PATH			"i915/tgl_dmc_ver2_04.bin"
     48 #define TGL_CSR_VERSION_REQUIRED	CSR_VERSION(2, 4)
     49 #define TGL_CSR_MAX_FW_SIZE		0x6000
     50 MODULE_FIRMWARE(TGL_CSR_PATH);
     51 
     52 #define ICL_CSR_PATH			"i915/icl_dmc_ver1_09.bin"
     53 #define ICL_CSR_VERSION_REQUIRED	CSR_VERSION(1, 9)
     54 #define ICL_CSR_MAX_FW_SIZE		0x6000
     55 MODULE_FIRMWARE(ICL_CSR_PATH);
     56 
     57 #define CNL_CSR_PATH			"i915/cnl_dmc_ver1_07.bin"
     58 #define CNL_CSR_VERSION_REQUIRED	CSR_VERSION(1, 7)
     59 #define CNL_CSR_MAX_FW_SIZE		GLK_CSR_MAX_FW_SIZE
     60 MODULE_FIRMWARE(CNL_CSR_PATH);
     61 
     62 #define GLK_CSR_PATH			"i915/glk_dmc_ver1_04.bin"
     63 #define GLK_CSR_VERSION_REQUIRED	CSR_VERSION(1, 4)
     64 #define GLK_CSR_MAX_FW_SIZE		0x4000
     65 MODULE_FIRMWARE(GLK_CSR_PATH);
     66 
     67 #define KBL_CSR_PATH			"i915/kbl_dmc_ver1_04.bin"
     68 #define KBL_CSR_VERSION_REQUIRED	CSR_VERSION(1, 4)
     69 #define KBL_CSR_MAX_FW_SIZE		BXT_CSR_MAX_FW_SIZE
     70 MODULE_FIRMWARE(KBL_CSR_PATH);
     71 
     72 #define SKL_CSR_PATH			"i915/skl_dmc_ver1_27.bin"
     73 #define SKL_CSR_VERSION_REQUIRED	CSR_VERSION(1, 27)
     74 #define SKL_CSR_MAX_FW_SIZE		BXT_CSR_MAX_FW_SIZE
     75 MODULE_FIRMWARE(SKL_CSR_PATH);
     76 
     77 #define BXT_CSR_PATH			"i915/bxt_dmc_ver1_07.bin"
     78 #define BXT_CSR_VERSION_REQUIRED	CSR_VERSION(1, 7)
     79 #define BXT_CSR_MAX_FW_SIZE		0x3000
     80 MODULE_FIRMWARE(BXT_CSR_PATH);
     81 
     82 #define CSR_DEFAULT_FW_OFFSET		0xFFFFFFFF
     83 #define PACKAGE_MAX_FW_INFO_ENTRIES	20
     84 #define PACKAGE_V2_MAX_FW_INFO_ENTRIES	32
     85 #define DMC_V1_MAX_MMIO_COUNT		8
     86 #define DMC_V3_MAX_MMIO_COUNT		20
     87 
     88 struct intel_css_header {
     89 	/* 0x09 for DMC */
     90 	u32 module_type;
     91 
     92 	/* Includes the DMC specific header in dwords */
     93 	u32 header_len;
     94 
     95 	/* always value would be 0x10000 */
     96 	u32 header_ver;
     97 
     98 	/* Not used */
     99 	u32 module_id;
    100 
    101 	/* Not used */
    102 	u32 module_vendor;
    103 
    104 	/* in YYYYMMDD format */
    105 	u32 date;
    106 
    107 	/* Size in dwords (CSS_Headerlen + PackageHeaderLen + dmc FWsLen)/4 */
    108 	u32 size;
    109 
    110 	/* Not used */
    111 	u32 key_size;
    112 
    113 	/* Not used */
    114 	u32 modulus_size;
    115 
    116 	/* Not used */
    117 	u32 exponent_size;
    118 
    119 	/* Not used */
    120 	u32 reserved1[12];
    121 
    122 	/* Major Minor */
    123 	u32 version;
    124 
    125 	/* Not used */
    126 	u32 reserved2[8];
    127 
    128 	/* Not used */
    129 	u32 kernel_header_info;
    130 } __packed;
    131 
    132 struct intel_fw_info {
    133 	u8 reserved1;
    134 
    135 	/* reserved on package_header version 1, must be 0 on version 2 */
    136 	u8 dmc_id;
    137 
    138 	/* Stepping (A, B, C, ..., *). * is a wildcard */
    139 	char stepping;
    140 
    141 	/* Sub-stepping (0, 1, ..., *). * is a wildcard */
    142 	char substepping;
    143 
    144 	u32 offset;
    145 	u32 reserved2;
    146 } __packed;
    147 
    148 struct intel_package_header {
    149 	/* DMC container header length in dwords */
    150 	u8 header_len;
    151 
    152 	/* 0x01, 0x02 */
    153 	u8 header_ver;
    154 
    155 	u8 reserved[10];
    156 
    157 	/* Number of valid entries in the FWInfo array below */
    158 	u32 num_entries;
    159 } __packed;
    160 
    161 struct intel_dmc_header_base {
    162 	/* always value would be 0x40403E3E */
    163 	u32 signature;
    164 
    165 	/* DMC binary header length */
    166 	u8 header_len;
    167 
    168 	/* 0x01 */
    169 	u8 header_ver;
    170 
    171 	/* Reserved */
    172 	u16 dmcc_ver;
    173 
    174 	/* Major, Minor */
    175 	u32 project;
    176 
    177 	/* Firmware program size (excluding header) in dwords */
    178 	u32 fw_size;
    179 
    180 	/* Major Minor version */
    181 	u32 fw_version;
    182 } __packed;
    183 
    184 struct intel_dmc_header_v1 {
    185 	struct intel_dmc_header_base base;
    186 
    187 	/* Number of valid MMIO cycles present. */
    188 	u32 mmio_count;
    189 
    190 	/* MMIO address */
    191 	u32 mmioaddr[DMC_V1_MAX_MMIO_COUNT];
    192 
    193 	/* MMIO data */
    194 	u32 mmiodata[DMC_V1_MAX_MMIO_COUNT];
    195 
    196 	/* FW filename  */
    197 	char dfile[32];
    198 
    199 	u32 reserved1[2];
    200 } __packed;
    201 
    202 struct intel_dmc_header_v3 {
    203 	struct intel_dmc_header_base base;
    204 
    205 	/* DMC RAM start MMIO address */
    206 	u32 start_mmioaddr;
    207 
    208 	u32 reserved[9];
    209 
    210 	/* FW filename */
    211 	char dfile[32];
    212 
    213 	/* Number of valid MMIO cycles present. */
    214 	u32 mmio_count;
    215 
    216 	/* MMIO address */
    217 	u32 mmioaddr[DMC_V3_MAX_MMIO_COUNT];
    218 
    219 	/* MMIO data */
    220 	u32 mmiodata[DMC_V3_MAX_MMIO_COUNT];
    221 } __packed;
    222 
    223 struct stepping_info {
    224 	char stepping;
    225 	char substepping;
    226 };
    227 
    228 static const struct stepping_info skl_stepping_info[] = {
    229 	{'A', '0'}, {'B', '0'}, {'C', '0'},
    230 	{'D', '0'}, {'E', '0'}, {'F', '0'},
    231 	{'G', '0'}, {'H', '0'}, {'I', '0'},
    232 	{'J', '0'}, {'K', '0'}
    233 };
    234 
    235 static const struct stepping_info bxt_stepping_info[] = {
    236 	{'A', '0'}, {'A', '1'}, {'A', '2'},
    237 	{'B', '0'}, {'B', '1'}, {'B', '2'}
    238 };
    239 
    240 static const struct stepping_info icl_stepping_info[] = {
    241 	{'A', '0'}, {'A', '1'}, {'A', '2'},
    242 	{'B', '0'}, {'B', '2'},
    243 	{'C', '0'}
    244 };
    245 
    246 static const struct stepping_info no_stepping_info = { '*', '*' };
    247 
    248 static const struct stepping_info *
    249 intel_get_stepping_info(struct drm_i915_private *dev_priv)
    250 {
    251 	const struct stepping_info *si;
    252 	unsigned int size;
    253 
    254 	if (IS_ICELAKE(dev_priv)) {
    255 		size = ARRAY_SIZE(icl_stepping_info);
    256 		si = icl_stepping_info;
    257 	} else if (IS_SKYLAKE(dev_priv)) {
    258 		size = ARRAY_SIZE(skl_stepping_info);
    259 		si = skl_stepping_info;
    260 	} else if (IS_BROXTON(dev_priv)) {
    261 		size = ARRAY_SIZE(bxt_stepping_info);
    262 		si = bxt_stepping_info;
    263 	} else {
    264 		size = 0;
    265 		si = NULL;
    266 	}
    267 
    268 	if (INTEL_REVID(dev_priv) < size)
    269 		return si + INTEL_REVID(dev_priv);
    270 
    271 	return &no_stepping_info;
    272 }
    273 
    274 static void gen9_set_dc_state_debugmask(struct drm_i915_private *dev_priv)
    275 {
    276 	u32 val, mask;
    277 
    278 	mask = DC_STATE_DEBUG_MASK_MEMORY_UP;
    279 
    280 	if (IS_GEN9_LP(dev_priv))
    281 		mask |= DC_STATE_DEBUG_MASK_CORES;
    282 
    283 	/* The below bit doesn't need to be cleared ever afterwards */
    284 	val = I915_READ(DC_STATE_DEBUG);
    285 	if ((val & mask) != mask) {
    286 		val |= mask;
    287 		I915_WRITE(DC_STATE_DEBUG, val);
    288 		POSTING_READ(DC_STATE_DEBUG);
    289 	}
    290 }
    291 
    292 /**
    293  * intel_csr_load_program() - write the firmware from memory to register.
    294  * @dev_priv: i915 drm device.
    295  *
    296  * CSR firmware is read from a .bin file and kept in internal memory one time.
    297  * Everytime display comes back from low power state this function is called to
    298  * copy the firmware from internal memory to registers.
    299  */
    300 void intel_csr_load_program(struct drm_i915_private *dev_priv)
    301 {
    302 	u32 *payload = dev_priv->csr.dmc_payload;
    303 	u32 i, fw_size;
    304 
    305 	if (!HAS_CSR(dev_priv)) {
    306 		DRM_ERROR("No CSR support available for this platform\n");
    307 		return;
    308 	}
    309 
    310 	if (!dev_priv->csr.dmc_payload) {
    311 		DRM_ERROR("Tried to program CSR with empty payload\n");
    312 		return;
    313 	}
    314 
    315 	fw_size = dev_priv->csr.dmc_fw_size;
    316 	assert_rpm_wakelock_held(&dev_priv->runtime_pm);
    317 
    318 	preempt_disable();
    319 
    320 	for (i = 0; i < fw_size; i++)
    321 		I915_WRITE_FW(CSR_PROGRAM(i), payload[i]);
    322 
    323 	preempt_enable();
    324 
    325 	for (i = 0; i < dev_priv->csr.mmio_count; i++) {
    326 		I915_WRITE(dev_priv->csr.mmioaddr[i],
    327 			   dev_priv->csr.mmiodata[i]);
    328 	}
    329 
    330 	dev_priv->csr.dc_state = 0;
    331 
    332 	gen9_set_dc_state_debugmask(dev_priv);
    333 }
    334 
    335 /*
    336  * Search fw_info table for dmc_offset to find firmware binary: num_entries is
    337  * already sanitized.
    338  */
    339 static u32 find_dmc_fw_offset(const struct intel_fw_info *fw_info,
    340 			      unsigned int num_entries,
    341 			      const struct stepping_info *si,
    342 			      u8 package_ver)
    343 {
    344 	u32 dmc_offset = CSR_DEFAULT_FW_OFFSET;
    345 	unsigned int i;
    346 
    347 	for (i = 0; i < num_entries; i++) {
    348 		if (package_ver > 1 && fw_info[i].dmc_id != 0)
    349 			continue;
    350 
    351 		if (fw_info[i].substepping == '*' &&
    352 		    si->stepping == fw_info[i].stepping) {
    353 			dmc_offset = fw_info[i].offset;
    354 			break;
    355 		}
    356 
    357 		if (si->stepping == fw_info[i].stepping &&
    358 		    si->substepping == fw_info[i].substepping) {
    359 			dmc_offset = fw_info[i].offset;
    360 			break;
    361 		}
    362 
    363 		if (fw_info[i].stepping == '*' &&
    364 		    fw_info[i].substepping == '*') {
    365 			/*
    366 			 * In theory we should stop the search as generic
    367 			 * entries should always come after the more specific
    368 			 * ones, but let's continue to make sure to work even
    369 			 * with "broken" firmwares. If we don't find a more
    370 			 * specific one, then we use this entry
    371 			 */
    372 			dmc_offset = fw_info[i].offset;
    373 		}
    374 	}
    375 
    376 	return dmc_offset;
    377 }
    378 
    379 static u32 parse_csr_fw_dmc(struct intel_csr *csr,
    380 			    const struct intel_dmc_header_base *dmc_header,
    381 			    size_t rem_size)
    382 {
    383 	unsigned int header_len_bytes, dmc_header_size, payload_size, i;
    384 	const u32 *mmioaddr, *mmiodata;
    385 	u32 mmio_count, mmio_count_max;
    386 	u8 *payload;
    387 
    388 	BUILD_BUG_ON(ARRAY_SIZE(csr->mmioaddr) < DMC_V3_MAX_MMIO_COUNT ||
    389 		     ARRAY_SIZE(csr->mmioaddr) < DMC_V1_MAX_MMIO_COUNT);
    390 
    391 	/*
    392 	 * Check if we can access common fields, we will checkc again below
    393 	 * after we have read the version
    394 	 */
    395 	if (rem_size < sizeof(struct intel_dmc_header_base))
    396 		goto error_truncated;
    397 
    398 	/* Cope with small differences between v1 and v3 */
    399 	if (dmc_header->header_ver == 3) {
    400 		const struct intel_dmc_header_v3 *v3 =
    401 			(const struct intel_dmc_header_v3 *)dmc_header;
    402 
    403 		if (rem_size < sizeof(struct intel_dmc_header_v3))
    404 			goto error_truncated;
    405 
    406 		mmioaddr = v3->mmioaddr;
    407 		mmiodata = v3->mmiodata;
    408 		mmio_count = v3->mmio_count;
    409 		mmio_count_max = DMC_V3_MAX_MMIO_COUNT;
    410 		/* header_len is in dwords */
    411 		header_len_bytes = dmc_header->header_len * 4;
    412 		dmc_header_size = sizeof(*v3);
    413 	} else if (dmc_header->header_ver == 1) {
    414 		const struct intel_dmc_header_v1 *v1 =
    415 			(const struct intel_dmc_header_v1 *)dmc_header;
    416 
    417 		if (rem_size < sizeof(struct intel_dmc_header_v1))
    418 			goto error_truncated;
    419 
    420 		mmioaddr = v1->mmioaddr;
    421 		mmiodata = v1->mmiodata;
    422 		mmio_count = v1->mmio_count;
    423 		mmio_count_max = DMC_V1_MAX_MMIO_COUNT;
    424 		header_len_bytes = dmc_header->header_len;
    425 		dmc_header_size = sizeof(*v1);
    426 	} else {
    427 		DRM_ERROR("Unknown DMC fw header version: %u\n",
    428 			  dmc_header->header_ver);
    429 		return 0;
    430 	}
    431 
    432 	if (header_len_bytes != dmc_header_size) {
    433 		DRM_ERROR("DMC firmware has wrong dmc header length "
    434 			  "(%u bytes)\n", header_len_bytes);
    435 		return 0;
    436 	}
    437 
    438 	/* Cache the dmc header info. */
    439 	if (mmio_count > mmio_count_max) {
    440 		DRM_ERROR("DMC firmware has wrong mmio count %u\n", mmio_count);
    441 		return 0;
    442 	}
    443 
    444 	for (i = 0; i < mmio_count; i++) {
    445 		if (mmioaddr[i] < CSR_MMIO_START_RANGE ||
    446 		    mmioaddr[i] > CSR_MMIO_END_RANGE) {
    447 			DRM_ERROR("DMC firmware has wrong mmio address 0x%x\n",
    448 				  mmioaddr[i]);
    449 			return 0;
    450 		}
    451 		csr->mmioaddr[i] = _MMIO(mmioaddr[i]);
    452 		csr->mmiodata[i] = mmiodata[i];
    453 	}
    454 	csr->mmio_count = mmio_count;
    455 
    456 	rem_size -= header_len_bytes;
    457 
    458 	/* fw_size is in dwords, so multiplied by 4 to convert into bytes. */
    459 	payload_size = dmc_header->fw_size * 4;
    460 	if (rem_size < payload_size)
    461 		goto error_truncated;
    462 
    463 	if (payload_size > csr->max_fw_size) {
    464 		DRM_ERROR("DMC FW too big (%u bytes)\n", payload_size);
    465 		return 0;
    466 	}
    467 	csr->dmc_fw_size = dmc_header->fw_size;
    468 
    469 	csr->dmc_payload = kmalloc(payload_size, GFP_KERNEL);
    470 	if (!csr->dmc_payload) {
    471 		DRM_ERROR("Memory allocation failed for dmc payload\n");
    472 		return 0;
    473 	}
    474 
    475 	payload = (u8 *)(dmc_header) + header_len_bytes;
    476 	memcpy(csr->dmc_payload, payload, payload_size);
    477 
    478 	return header_len_bytes + payload_size;
    479 
    480 error_truncated:
    481 	DRM_ERROR("Truncated DMC firmware, refusing.\n");
    482 	return 0;
    483 }
    484 
    485 static u32
    486 parse_csr_fw_package(struct intel_csr *csr,
    487 		     const struct intel_package_header *package_header,
    488 		     const struct stepping_info *si,
    489 		     size_t rem_size)
    490 {
    491 	u32 package_size = sizeof(struct intel_package_header);
    492 	u32 num_entries, max_entries, dmc_offset;
    493 	const struct intel_fw_info *fw_info;
    494 
    495 	if (rem_size < package_size)
    496 		goto error_truncated;
    497 
    498 	if (package_header->header_ver == 1) {
    499 		max_entries = PACKAGE_MAX_FW_INFO_ENTRIES;
    500 	} else if (package_header->header_ver == 2) {
    501 		max_entries = PACKAGE_V2_MAX_FW_INFO_ENTRIES;
    502 	} else {
    503 		DRM_ERROR("DMC firmware has unknown header version %u\n",
    504 			  package_header->header_ver);
    505 		return 0;
    506 	}
    507 
    508 	/*
    509 	 * We should always have space for max_entries,
    510 	 * even if not all are used
    511 	 */
    512 	package_size += max_entries * sizeof(struct intel_fw_info);
    513 	if (rem_size < package_size)
    514 		goto error_truncated;
    515 
    516 	if (package_header->header_len * 4 != package_size) {
    517 		DRM_ERROR("DMC firmware has wrong package header length "
    518 			  "(%u bytes)\n", package_size);
    519 		return 0;
    520 	}
    521 
    522 	num_entries = package_header->num_entries;
    523 	if (WARN_ON(package_header->num_entries > max_entries))
    524 		num_entries = max_entries;
    525 
    526 	fw_info = (const struct intel_fw_info *)
    527 		((u8 *)package_header + sizeof(*package_header));
    528 	dmc_offset = find_dmc_fw_offset(fw_info, num_entries, si,
    529 					package_header->header_ver);
    530 	if (dmc_offset == CSR_DEFAULT_FW_OFFSET) {
    531 		DRM_ERROR("DMC firmware not supported for %c stepping\n",
    532 			  si->stepping);
    533 		return 0;
    534 	}
    535 
    536 	/* dmc_offset is in dwords */
    537 	return package_size + dmc_offset * 4;
    538 
    539 error_truncated:
    540 	DRM_ERROR("Truncated DMC firmware, refusing.\n");
    541 	return 0;
    542 }
    543 
    544 /* Return number of bytes parsed or 0 on error */
    545 static u32 parse_csr_fw_css(struct intel_csr *csr,
    546 			    struct intel_css_header *css_header,
    547 			    size_t rem_size)
    548 {
    549 	if (rem_size < sizeof(struct intel_css_header)) {
    550 		DRM_ERROR("Truncated DMC firmware, refusing.\n");
    551 		return 0;
    552 	}
    553 
    554 	if (sizeof(struct intel_css_header) !=
    555 	    (css_header->header_len * 4)) {
    556 		DRM_ERROR("DMC firmware has wrong CSS header length "
    557 			  "(%u bytes)\n",
    558 			  (css_header->header_len * 4));
    559 		return 0;
    560 	}
    561 
    562 	if (csr->required_version &&
    563 	    css_header->version != csr->required_version) {
    564 		DRM_INFO("Refusing to load DMC firmware v%u.%u,"
    565 			 " please use v%u.%u\n",
    566 			 CSR_VERSION_MAJOR(css_header->version),
    567 			 CSR_VERSION_MINOR(css_header->version),
    568 			 CSR_VERSION_MAJOR(csr->required_version),
    569 			 CSR_VERSION_MINOR(csr->required_version));
    570 		return 0;
    571 	}
    572 
    573 	csr->version = css_header->version;
    574 
    575 	return sizeof(struct intel_css_header);
    576 }
    577 
    578 static void parse_csr_fw(struct drm_i915_private *dev_priv,
    579 			 const struct firmware *fw)
    580 {
    581 	struct intel_css_header *css_header;
    582 	struct intel_package_header *package_header;
    583 	struct intel_dmc_header_base *dmc_header;
    584 	struct intel_csr *csr = &dev_priv->csr;
    585 	const struct stepping_info *si = intel_get_stepping_info(dev_priv);
    586 	u32 readcount = 0;
    587 	u32 r;
    588 
    589 	if (!fw)
    590 		return;
    591 
    592 	/* Extract CSS Header information */
    593 	css_header = (struct intel_css_header *)fw->data;
    594 	r = parse_csr_fw_css(csr, css_header, fw->size);
    595 	if (!r)
    596 		return;
    597 
    598 	readcount += r;
    599 
    600 	/* Extract Package Header information */
    601 	package_header = (struct intel_package_header *)&fw->data[readcount];
    602 	r = parse_csr_fw_package(csr, package_header, si, fw->size - readcount);
    603 	if (!r)
    604 		return;
    605 
    606 	readcount += r;
    607 
    608 	/* Extract dmc_header information */
    609 	dmc_header = (struct intel_dmc_header_base *)&fw->data[readcount];
    610 	parse_csr_fw_dmc(csr, dmc_header, fw->size - readcount);
    611 }
    612 
    613 static void intel_csr_runtime_pm_get(struct drm_i915_private *dev_priv)
    614 {
    615 	WARN_ON(dev_priv->csr.wakeref);
    616 	dev_priv->csr.wakeref =
    617 		intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
    618 }
    619 
    620 static void intel_csr_runtime_pm_put(struct drm_i915_private *dev_priv)
    621 {
    622 	intel_wakeref_t wakeref __maybe_unused =
    623 		fetch_and_zero(&dev_priv->csr.wakeref);
    624 
    625 	intel_display_power_put(dev_priv, POWER_DOMAIN_INIT, wakeref);
    626 }
    627 
    628 static void csr_load_work_fn(struct work_struct *work)
    629 {
    630 	struct drm_i915_private *dev_priv;
    631 	struct intel_csr *csr;
    632 	const struct firmware *fw = NULL;
    633 
    634 	dev_priv = container_of(work, typeof(*dev_priv), csr.work);
    635 	csr = &dev_priv->csr;
    636 
    637 	request_firmware(&fw, dev_priv->csr.fw_path, &dev_priv->drm.pdev->dev);
    638 	parse_csr_fw(dev_priv, fw);
    639 
    640 	if (dev_priv->csr.dmc_payload) {
    641 		intel_csr_load_program(dev_priv);
    642 		intel_csr_runtime_pm_put(dev_priv);
    643 
    644 		DRM_INFO("Finished loading DMC firmware %s (v%u.%u)\n",
    645 			 dev_priv->csr.fw_path,
    646 			 CSR_VERSION_MAJOR(csr->version),
    647 			 CSR_VERSION_MINOR(csr->version));
    648 	} else {
    649 		dev_notice(dev_priv->drm.dev,
    650 			   "Failed to load DMC firmware %s."
    651 			   " Disabling runtime power management.\n",
    652 			   csr->fw_path);
    653 		dev_notice(dev_priv->drm.dev, "DMC firmware homepage: %s",
    654 			   INTEL_UC_FIRMWARE_URL);
    655 	}
    656 
    657 	release_firmware(fw);
    658 }
    659 
    660 /**
    661  * intel_csr_ucode_init() - initialize the firmware loading.
    662  * @dev_priv: i915 drm device.
    663  *
    664  * This function is called at the time of loading the display driver to read
    665  * firmware from a .bin file and copied into a internal memory.
    666  */
    667 void intel_csr_ucode_init(struct drm_i915_private *dev_priv)
    668 {
    669 	struct intel_csr *csr = &dev_priv->csr;
    670 
    671 	INIT_WORK(&dev_priv->csr.work, csr_load_work_fn);
    672 
    673 	if (!HAS_CSR(dev_priv))
    674 		return;
    675 
    676 	/*
    677 	 * Obtain a runtime pm reference, until CSR is loaded, to avoid entering
    678 	 * runtime-suspend.
    679 	 *
    680 	 * On error, we return with the rpm wakeref held to prevent runtime
    681 	 * suspend as runtime suspend *requires* a working CSR for whatever
    682 	 * reason.
    683 	 */
    684 	intel_csr_runtime_pm_get(dev_priv);
    685 
    686 	if (INTEL_GEN(dev_priv) >= 12) {
    687 		csr->fw_path = TGL_CSR_PATH;
    688 		csr->required_version = TGL_CSR_VERSION_REQUIRED;
    689 		/* Allow to load fw via parameter using the last known size */
    690 		csr->max_fw_size = GEN12_CSR_MAX_FW_SIZE;
    691 	} else if (IS_GEN(dev_priv, 11)) {
    692 		csr->fw_path = ICL_CSR_PATH;
    693 		csr->required_version = ICL_CSR_VERSION_REQUIRED;
    694 		csr->max_fw_size = ICL_CSR_MAX_FW_SIZE;
    695 	} else if (IS_CANNONLAKE(dev_priv)) {
    696 		csr->fw_path = CNL_CSR_PATH;
    697 		csr->required_version = CNL_CSR_VERSION_REQUIRED;
    698 		csr->max_fw_size = CNL_CSR_MAX_FW_SIZE;
    699 	} else if (IS_GEMINILAKE(dev_priv)) {
    700 		csr->fw_path = GLK_CSR_PATH;
    701 		csr->required_version = GLK_CSR_VERSION_REQUIRED;
    702 		csr->max_fw_size = GLK_CSR_MAX_FW_SIZE;
    703 	} else if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) {
    704 		csr->fw_path = KBL_CSR_PATH;
    705 		csr->required_version = KBL_CSR_VERSION_REQUIRED;
    706 		csr->max_fw_size = KBL_CSR_MAX_FW_SIZE;
    707 	} else if (IS_SKYLAKE(dev_priv)) {
    708 		csr->fw_path = SKL_CSR_PATH;
    709 		csr->required_version = SKL_CSR_VERSION_REQUIRED;
    710 		csr->max_fw_size = SKL_CSR_MAX_FW_SIZE;
    711 	} else if (IS_BROXTON(dev_priv)) {
    712 		csr->fw_path = BXT_CSR_PATH;
    713 		csr->required_version = BXT_CSR_VERSION_REQUIRED;
    714 		csr->max_fw_size = BXT_CSR_MAX_FW_SIZE;
    715 	}
    716 
    717 	if (i915_modparams.dmc_firmware_path) {
    718 		if (strlen(i915_modparams.dmc_firmware_path) == 0) {
    719 			csr->fw_path = NULL;
    720 			DRM_INFO("Disabling CSR firmware and runtime PM\n");
    721 			return;
    722 		}
    723 
    724 		csr->fw_path = i915_modparams.dmc_firmware_path;
    725 		/* Bypass version check for firmware override. */
    726 		csr->required_version = 0;
    727 	}
    728 
    729 	if (csr->fw_path == NULL) {
    730 		DRM_DEBUG_KMS("No known CSR firmware for platform, disabling runtime PM\n");
    731 		return;
    732 	}
    733 
    734 	DRM_DEBUG_KMS("Loading %s\n", csr->fw_path);
    735 	schedule_work(&dev_priv->csr.work);
    736 }
    737 
    738 /**
    739  * intel_csr_ucode_suspend() - prepare CSR firmware before system suspend
    740  * @dev_priv: i915 drm device
    741  *
    742  * Prepare the DMC firmware before entering system suspend. This includes
    743  * flushing pending work items and releasing any resources acquired during
    744  * init.
    745  */
    746 void intel_csr_ucode_suspend(struct drm_i915_private *dev_priv)
    747 {
    748 	if (!HAS_CSR(dev_priv))
    749 		return;
    750 
    751 	flush_work(&dev_priv->csr.work);
    752 
    753 	/* Drop the reference held in case DMC isn't loaded. */
    754 	if (!dev_priv->csr.dmc_payload)
    755 		intel_csr_runtime_pm_put(dev_priv);
    756 }
    757 
    758 /**
    759  * intel_csr_ucode_resume() - init CSR firmware during system resume
    760  * @dev_priv: i915 drm device
    761  *
    762  * Reinitialize the DMC firmware during system resume, reacquiring any
    763  * resources released in intel_csr_ucode_suspend().
    764  */
    765 void intel_csr_ucode_resume(struct drm_i915_private *dev_priv)
    766 {
    767 	if (!HAS_CSR(dev_priv))
    768 		return;
    769 
    770 	/*
    771 	 * Reacquire the reference to keep RPM disabled in case DMC isn't
    772 	 * loaded.
    773 	 */
    774 	if (!dev_priv->csr.dmc_payload)
    775 		intel_csr_runtime_pm_get(dev_priv);
    776 }
    777 
    778 /**
    779  * intel_csr_ucode_fini() - unload the CSR firmware.
    780  * @dev_priv: i915 drm device.
    781  *
    782  * Firmmware unloading includes freeing the internal memory and reset the
    783  * firmware loading status.
    784  */
    785 void intel_csr_ucode_fini(struct drm_i915_private *dev_priv)
    786 {
    787 	if (!HAS_CSR(dev_priv))
    788 		return;
    789 
    790 	intel_csr_ucode_suspend(dev_priv);
    791 	WARN_ON(dev_priv->csr.wakeref);
    792 
    793 	kfree(dev_priv->csr.dmc_payload);
    794 }
    795