xref: /dragonfly/sys/dev/drm/i915/intel_csr.c (revision 3f2dd94a)
119c468b4SFrançois Tigeot /*
219c468b4SFrançois Tigeot  * Copyright © 2014 Intel Corporation
319c468b4SFrançois Tigeot  *
419c468b4SFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
519c468b4SFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
619c468b4SFrançois Tigeot  * to deal in the Software without restriction, including without limitation
719c468b4SFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
819c468b4SFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
919c468b4SFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
1019c468b4SFrançois Tigeot  *
1119c468b4SFrançois Tigeot  * The above copyright notice and this permission notice (including the next
1219c468b4SFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
1319c468b4SFrançois Tigeot  * Software.
1419c468b4SFrançois Tigeot  *
1519c468b4SFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1619c468b4SFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1719c468b4SFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1819c468b4SFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919c468b4SFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2019c468b4SFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2119c468b4SFrançois Tigeot  * IN THE SOFTWARE.
2219c468b4SFrançois Tigeot  *
2319c468b4SFrançois Tigeot  */
2419c468b4SFrançois Tigeot #include <linux/firmware.h>
2519c468b4SFrançois Tigeot #include "i915_drv.h"
2619c468b4SFrançois Tigeot #include "i915_reg.h"
2719c468b4SFrançois Tigeot 
2819c468b4SFrançois Tigeot /**
2919c468b4SFrançois Tigeot  * DOC: csr support for dmc
3019c468b4SFrançois Tigeot  *
3119c468b4SFrançois Tigeot  * Display Context Save and Restore (CSR) firmware support added from gen9
3219c468b4SFrançois Tigeot  * onwards to drive newly added DMC (Display microcontroller) in display
3319c468b4SFrançois Tigeot  * engine to save and restore the state of display engine when it enter into
3419c468b4SFrançois Tigeot  * low-power state and comes back to normal.
3519c468b4SFrançois Tigeot  */
3619c468b4SFrançois Tigeot 
37a85cb24fSFrançois Tigeot #define I915_CSR_GLK "i915/glk_dmc_ver1_04.bin"
38a85cb24fSFrançois Tigeot #define GLK_CSR_VERSION_REQUIRED	CSR_VERSION(1, 4)
39a85cb24fSFrançois Tigeot 
40*3f2dd94aSFrançois Tigeot #define I915_CSR_CNL "i915/cnl_dmc_ver1_04.bin"
41*3f2dd94aSFrançois Tigeot #define CNL_CSR_VERSION_REQUIRED	CSR_VERSION(1, 4)
42*3f2dd94aSFrançois Tigeot 
431487f786SFrançois Tigeot #define I915_CSR_KBL "i915/kbl_dmc_ver1_01.bin"
448621f407SFrançois Tigeot MODULE_FIRMWARE(I915_CSR_KBL);
458621f407SFrançois Tigeot #define KBL_CSR_VERSION_REQUIRED	CSR_VERSION(1, 1)
468621f407SFrançois Tigeot 
471487f786SFrançois Tigeot #define I915_CSR_SKL "i915/skl_dmc_ver1_26.bin"
488621f407SFrançois Tigeot MODULE_FIRMWARE(I915_CSR_SKL);
491487f786SFrançois Tigeot #define SKL_CSR_VERSION_REQUIRED	CSR_VERSION(1, 26)
508621f407SFrançois Tigeot 
511487f786SFrançois Tigeot #define I915_CSR_BXT "i915/bxt_dmc_ver1_07.bin"
528621f407SFrançois Tigeot MODULE_FIRMWARE(I915_CSR_BXT);
538621f407SFrançois Tigeot #define BXT_CSR_VERSION_REQUIRED	CSR_VERSION(1, 7)
5419c468b4SFrançois Tigeot 
55aee94f86SFrançois Tigeot 
5619c468b4SFrançois Tigeot #define CSR_MAX_FW_SIZE			0x2FFF
5719c468b4SFrançois Tigeot #define CSR_DEFAULT_FW_OFFSET		0xFFFFFFFF
5819c468b4SFrançois Tigeot 
5919c468b4SFrançois Tigeot struct intel_css_header {
6019c468b4SFrançois Tigeot 	/* 0x09 for DMC */
6119c468b4SFrançois Tigeot 	uint32_t module_type;
6219c468b4SFrançois Tigeot 
6319c468b4SFrançois Tigeot 	/* Includes the DMC specific header in dwords */
6419c468b4SFrançois Tigeot 	uint32_t header_len;
6519c468b4SFrançois Tigeot 
6619c468b4SFrançois Tigeot 	/* always value would be 0x10000 */
6719c468b4SFrançois Tigeot 	uint32_t header_ver;
6819c468b4SFrançois Tigeot 
6919c468b4SFrançois Tigeot 	/* Not used */
7019c468b4SFrançois Tigeot 	uint32_t module_id;
7119c468b4SFrançois Tigeot 
7219c468b4SFrançois Tigeot 	/* Not used */
7319c468b4SFrançois Tigeot 	uint32_t module_vendor;
7419c468b4SFrançois Tigeot 
7519c468b4SFrançois Tigeot 	/* in YYYYMMDD format */
7619c468b4SFrançois Tigeot 	uint32_t date;
7719c468b4SFrançois Tigeot 
7819c468b4SFrançois Tigeot 	/* Size in dwords (CSS_Headerlen + PackageHeaderLen + dmc FWsLen)/4 */
7919c468b4SFrançois Tigeot 	uint32_t size;
8019c468b4SFrançois Tigeot 
8119c468b4SFrançois Tigeot 	/* Not used */
8219c468b4SFrançois Tigeot 	uint32_t key_size;
8319c468b4SFrançois Tigeot 
8419c468b4SFrançois Tigeot 	/* Not used */
8519c468b4SFrançois Tigeot 	uint32_t modulus_size;
8619c468b4SFrançois Tigeot 
8719c468b4SFrançois Tigeot 	/* Not used */
8819c468b4SFrançois Tigeot 	uint32_t exponent_size;
8919c468b4SFrançois Tigeot 
9019c468b4SFrançois Tigeot 	/* Not used */
9119c468b4SFrançois Tigeot 	uint32_t reserved1[12];
9219c468b4SFrançois Tigeot 
9319c468b4SFrançois Tigeot 	/* Major Minor */
9419c468b4SFrançois Tigeot 	uint32_t version;
9519c468b4SFrançois Tigeot 
9619c468b4SFrançois Tigeot 	/* Not used */
9719c468b4SFrançois Tigeot 	uint32_t reserved2[8];
9819c468b4SFrançois Tigeot 
9919c468b4SFrançois Tigeot 	/* Not used */
10019c468b4SFrançois Tigeot 	uint32_t kernel_header_info;
10119c468b4SFrançois Tigeot } __packed;
10219c468b4SFrançois Tigeot 
10319c468b4SFrançois Tigeot struct intel_fw_info {
10419c468b4SFrançois Tigeot 	uint16_t reserved1;
10519c468b4SFrançois Tigeot 
10619c468b4SFrançois Tigeot 	/* Stepping (A, B, C, ..., *). * is a wildcard */
10719c468b4SFrançois Tigeot 	char stepping;
10819c468b4SFrançois Tigeot 
10919c468b4SFrançois Tigeot 	/* Sub-stepping (0, 1, ..., *). * is a wildcard */
11019c468b4SFrançois Tigeot 	char substepping;
11119c468b4SFrançois Tigeot 
11219c468b4SFrançois Tigeot 	uint32_t offset;
11319c468b4SFrançois Tigeot 	uint32_t reserved2;
11419c468b4SFrançois Tigeot } __packed;
11519c468b4SFrançois Tigeot 
11619c468b4SFrançois Tigeot struct intel_package_header {
11719c468b4SFrançois Tigeot 	/* DMC container header length in dwords */
11819c468b4SFrançois Tigeot 	unsigned char header_len;
11919c468b4SFrançois Tigeot 
12019c468b4SFrançois Tigeot 	/* always value would be 0x01 */
12119c468b4SFrançois Tigeot 	unsigned char header_ver;
12219c468b4SFrançois Tigeot 
12319c468b4SFrançois Tigeot 	unsigned char reserved[10];
12419c468b4SFrançois Tigeot 
12519c468b4SFrançois Tigeot 	/* Number of valid entries in the FWInfo array below */
12619c468b4SFrançois Tigeot 	uint32_t num_entries;
12719c468b4SFrançois Tigeot 
12819c468b4SFrançois Tigeot 	struct intel_fw_info fw_info[20];
12919c468b4SFrançois Tigeot } __packed;
13019c468b4SFrançois Tigeot 
13119c468b4SFrançois Tigeot struct intel_dmc_header {
13219c468b4SFrançois Tigeot 	/* always value would be 0x40403E3E */
13319c468b4SFrançois Tigeot 	uint32_t signature;
13419c468b4SFrançois Tigeot 
13519c468b4SFrançois Tigeot 	/* DMC binary header length */
13619c468b4SFrançois Tigeot 	unsigned char header_len;
13719c468b4SFrançois Tigeot 
13819c468b4SFrançois Tigeot 	/* 0x01 */
13919c468b4SFrançois Tigeot 	unsigned char header_ver;
14019c468b4SFrançois Tigeot 
14119c468b4SFrançois Tigeot 	/* Reserved */
14219c468b4SFrançois Tigeot 	uint16_t dmcc_ver;
14319c468b4SFrançois Tigeot 
14419c468b4SFrançois Tigeot 	/* Major, Minor */
14519c468b4SFrançois Tigeot 	uint32_t	project;
14619c468b4SFrançois Tigeot 
14719c468b4SFrançois Tigeot 	/* Firmware program size (excluding header) in dwords */
14819c468b4SFrançois Tigeot 	uint32_t	fw_size;
14919c468b4SFrançois Tigeot 
15019c468b4SFrançois Tigeot 	/* Major Minor version */
15119c468b4SFrançois Tigeot 	uint32_t fw_version;
15219c468b4SFrançois Tigeot 
15319c468b4SFrançois Tigeot 	/* Number of valid MMIO cycles present. */
15419c468b4SFrançois Tigeot 	uint32_t mmio_count;
15519c468b4SFrançois Tigeot 
15619c468b4SFrançois Tigeot 	/* MMIO address */
15719c468b4SFrançois Tigeot 	uint32_t mmioaddr[8];
15819c468b4SFrançois Tigeot 
15919c468b4SFrançois Tigeot 	/* MMIO data */
16019c468b4SFrançois Tigeot 	uint32_t mmiodata[8];
16119c468b4SFrançois Tigeot 
16219c468b4SFrançois Tigeot 	/* FW filename  */
16319c468b4SFrançois Tigeot 	unsigned char dfile[32];
16419c468b4SFrançois Tigeot 
16519c468b4SFrançois Tigeot 	uint32_t reserved1[2];
16619c468b4SFrançois Tigeot } __packed;
16719c468b4SFrançois Tigeot 
16819c468b4SFrançois Tigeot struct stepping_info {
16919c468b4SFrançois Tigeot 	char stepping;
17019c468b4SFrançois Tigeot 	char substepping;
17119c468b4SFrançois Tigeot };
17219c468b4SFrançois Tigeot 
17319c468b4SFrançois Tigeot static const struct stepping_info skl_stepping_info[] = {
17419c468b4SFrançois Tigeot 	{'A', '0'}, {'B', '0'}, {'C', '0'},
17519c468b4SFrançois Tigeot 	{'D', '0'}, {'E', '0'}, {'F', '0'},
176c0e85e96SFrançois Tigeot 	{'G', '0'}, {'H', '0'}, {'I', '0'},
177c0e85e96SFrançois Tigeot 	{'J', '0'}, {'K', '0'}
17819c468b4SFrançois Tigeot };
17919c468b4SFrançois Tigeot 
180aee94f86SFrançois Tigeot static const struct stepping_info bxt_stepping_info[] = {
181352ff8bdSFrançois Tigeot 	{'A', '0'}, {'A', '1'}, {'A', '2'},
182352ff8bdSFrançois Tigeot 	{'B', '0'}, {'B', '1'}, {'B', '2'}
183352ff8bdSFrançois Tigeot };
184352ff8bdSFrançois Tigeot 
1858621f407SFrançois Tigeot static const struct stepping_info no_stepping_info = { '*', '*' };
1868621f407SFrançois Tigeot 
1878621f407SFrançois Tigeot static const struct stepping_info *
intel_get_stepping_info(struct drm_i915_private * dev_priv)1888621f407SFrançois Tigeot intel_get_stepping_info(struct drm_i915_private *dev_priv)
18919c468b4SFrançois Tigeot {
190aee94f86SFrançois Tigeot 	const struct stepping_info *si;
191aee94f86SFrançois Tigeot 	unsigned int size;
192aee94f86SFrançois Tigeot 
1934be47400SFrançois Tigeot 	if (IS_SKYLAKE(dev_priv)) {
194aee94f86SFrançois Tigeot 		size = ARRAY_SIZE(skl_stepping_info);
195aee94f86SFrançois Tigeot 		si = skl_stepping_info;
1968621f407SFrançois Tigeot 	} else if (IS_BROXTON(dev_priv)) {
197aee94f86SFrançois Tigeot 		size = ARRAY_SIZE(bxt_stepping_info);
198aee94f86SFrançois Tigeot 		si = bxt_stepping_info;
199aee94f86SFrançois Tigeot 	} else {
2008621f407SFrançois Tigeot 		size = 0;
20119c468b4SFrançois Tigeot 	}
20219c468b4SFrançois Tigeot 
2038621f407SFrançois Tigeot 	if (INTEL_REVID(dev_priv) < size)
2048621f407SFrançois Tigeot 		return si + INTEL_REVID(dev_priv);
20519c468b4SFrançois Tigeot 
2068621f407SFrançois Tigeot 	return &no_stepping_info;
2078621f407SFrançois Tigeot }
2088621f407SFrançois Tigeot 
gen9_set_dc_state_debugmask(struct drm_i915_private * dev_priv)2098621f407SFrançois Tigeot static void gen9_set_dc_state_debugmask(struct drm_i915_private *dev_priv)
2108621f407SFrançois Tigeot {
2118621f407SFrançois Tigeot 	uint32_t val, mask;
2128621f407SFrançois Tigeot 
2138621f407SFrançois Tigeot 	mask = DC_STATE_DEBUG_MASK_MEMORY_UP;
2148621f407SFrançois Tigeot 
215*3f2dd94aSFrançois Tigeot 	if (IS_GEN9_LP(dev_priv))
2168621f407SFrançois Tigeot 		mask |= DC_STATE_DEBUG_MASK_CORES;
2178621f407SFrançois Tigeot 
2188621f407SFrançois Tigeot 	/* The below bit doesn't need to be cleared ever afterwards */
2198621f407SFrançois Tigeot 	val = I915_READ(DC_STATE_DEBUG);
2208621f407SFrançois Tigeot 	if ((val & mask) != mask) {
2218621f407SFrançois Tigeot 		val |= mask;
2228621f407SFrançois Tigeot 		I915_WRITE(DC_STATE_DEBUG, val);
2238621f407SFrançois Tigeot 		POSTING_READ(DC_STATE_DEBUG);
2248621f407SFrançois Tigeot 	}
22519c468b4SFrançois Tigeot }
22619c468b4SFrançois Tigeot 
22719c468b4SFrançois Tigeot /**
22819c468b4SFrançois Tigeot  * intel_csr_load_program() - write the firmware from memory to register.
229aee94f86SFrançois Tigeot  * @dev_priv: i915 drm device.
23019c468b4SFrançois Tigeot  *
23119c468b4SFrançois Tigeot  * CSR firmware is read from a .bin file and kept in internal memory one time.
23219c468b4SFrançois Tigeot  * Everytime display comes back from low power state this function is called to
23319c468b4SFrançois Tigeot  * copy the firmware from internal memory to registers.
23419c468b4SFrançois Tigeot  */
intel_csr_load_program(struct drm_i915_private * dev_priv)2358621f407SFrançois Tigeot void intel_csr_load_program(struct drm_i915_private *dev_priv)
23619c468b4SFrançois Tigeot {
237a05eeebfSFrançois Tigeot 	u32 *payload = dev_priv->csr.dmc_payload;
23819c468b4SFrançois Tigeot 	uint32_t i, fw_size;
23919c468b4SFrançois Tigeot 
240*3f2dd94aSFrançois Tigeot 	if (!HAS_CSR(dev_priv)) {
24119c468b4SFrançois Tigeot 		DRM_ERROR("No CSR support available for this platform\n");
2428621f407SFrançois Tigeot 		return;
24319c468b4SFrançois Tigeot 	}
24419c468b4SFrançois Tigeot 
245aee94f86SFrançois Tigeot 	if (!dev_priv->csr.dmc_payload) {
246aee94f86SFrançois Tigeot 		DRM_ERROR("Tried to program CSR with empty payload\n");
2478621f407SFrançois Tigeot 		return;
248aee94f86SFrançois Tigeot 	}
249352ff8bdSFrançois Tigeot 
25019c468b4SFrançois Tigeot 	fw_size = dev_priv->csr.dmc_fw_size;
251*3f2dd94aSFrançois Tigeot 	assert_rpm_wakelock_held(dev_priv);
252*3f2dd94aSFrançois Tigeot 
253*3f2dd94aSFrançois Tigeot 	preempt_disable();
254*3f2dd94aSFrançois Tigeot 
25519c468b4SFrançois Tigeot 	for (i = 0; i < fw_size; i++)
256*3f2dd94aSFrançois Tigeot 		I915_WRITE_FW(CSR_PROGRAM(i), payload[i]);
257*3f2dd94aSFrançois Tigeot 
258*3f2dd94aSFrançois Tigeot 	preempt_enable();
25919c468b4SFrançois Tigeot 
26019c468b4SFrançois Tigeot 	for (i = 0; i < dev_priv->csr.mmio_count; i++) {
26119c468b4SFrançois Tigeot 		I915_WRITE(dev_priv->csr.mmioaddr[i],
26219c468b4SFrançois Tigeot 			   dev_priv->csr.mmiodata[i]);
26319c468b4SFrançois Tigeot 	}
26419c468b4SFrançois Tigeot 
265aee94f86SFrançois Tigeot 	dev_priv->csr.dc_state = 0;
266c0e85e96SFrançois Tigeot 
2678621f407SFrançois Tigeot 	gen9_set_dc_state_debugmask(dev_priv);
26819c468b4SFrançois Tigeot }
26919c468b4SFrançois Tigeot 
parse_csr_fw(struct drm_i915_private * dev_priv,const struct firmware * fw)270aee94f86SFrançois Tigeot static uint32_t *parse_csr_fw(struct drm_i915_private *dev_priv,
271aee94f86SFrançois Tigeot 			      const struct firmware *fw)
27219c468b4SFrançois Tigeot {
2734be47400SFrançois Tigeot 	struct intel_css_header *css_header;
2744be47400SFrançois Tigeot 	struct intel_package_header *package_header;
2754be47400SFrançois Tigeot 	struct intel_dmc_header *dmc_header;
27619c468b4SFrançois Tigeot 	struct intel_csr *csr = &dev_priv->csr;
2778621f407SFrançois Tigeot 	const struct stepping_info *si = intel_get_stepping_info(dev_priv);
27819c468b4SFrançois Tigeot 	uint32_t dmc_offset = CSR_DEFAULT_FW_OFFSET, readcount = 0, nbytes;
27919c468b4SFrançois Tigeot 	uint32_t i;
280a05eeebfSFrançois Tigeot 	uint32_t *dmc_payload;
2811487f786SFrançois Tigeot 	uint32_t required_version;
28219c468b4SFrançois Tigeot 
283aee94f86SFrançois Tigeot 	if (!fw)
284aee94f86SFrançois Tigeot 		return NULL;
28519c468b4SFrançois Tigeot 
28619c468b4SFrançois Tigeot 	/* Extract CSS Header information*/
2874be47400SFrançois Tigeot 	css_header = (struct intel_css_header *)fw->data;
28819c468b4SFrançois Tigeot 	if (sizeof(struct intel_css_header) !=
28919c468b4SFrançois Tigeot 	    (css_header->header_len * 4)) {
290*3f2dd94aSFrançois Tigeot 		DRM_ERROR("DMC firmware has wrong CSS header length "
291*3f2dd94aSFrançois Tigeot 			  "(%u bytes)\n",
29219c468b4SFrançois Tigeot 			  (css_header->header_len * 4));
293aee94f86SFrançois Tigeot 		return NULL;
29419c468b4SFrançois Tigeot 	}
295aee94f86SFrançois Tigeot 
296aee94f86SFrançois Tigeot 	csr->version = css_header->version;
297aee94f86SFrançois Tigeot 
298*3f2dd94aSFrançois Tigeot 	if (IS_CANNONLAKE(dev_priv)) {
299*3f2dd94aSFrançois Tigeot 		required_version = CNL_CSR_VERSION_REQUIRED;
300*3f2dd94aSFrançois Tigeot 	} else if (IS_GEMINILAKE(dev_priv)) {
301a85cb24fSFrançois Tigeot 		required_version = GLK_CSR_VERSION_REQUIRED;
302*3f2dd94aSFrançois Tigeot 	} else if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) {
3031487f786SFrançois Tigeot 		required_version = KBL_CSR_VERSION_REQUIRED;
3048621f407SFrançois Tigeot 	} else if (IS_SKYLAKE(dev_priv)) {
3051487f786SFrançois Tigeot 		required_version = SKL_CSR_VERSION_REQUIRED;
3068621f407SFrançois Tigeot 	} else if (IS_BROXTON(dev_priv)) {
3071487f786SFrançois Tigeot 		required_version = BXT_CSR_VERSION_REQUIRED;
3088621f407SFrançois Tigeot 	} else {
3098621f407SFrançois Tigeot 		MISSING_CASE(INTEL_REVID(dev_priv));
3101487f786SFrançois Tigeot 		required_version = 0;
3118621f407SFrançois Tigeot 	}
3128621f407SFrançois Tigeot 
3131487f786SFrançois Tigeot 	if (csr->version != required_version) {
3141487f786SFrançois Tigeot 		DRM_INFO("Refusing to load DMC firmware v%u.%u,"
315*3f2dd94aSFrançois Tigeot 			 " please use v%u.%u\n",
316aee94f86SFrançois Tigeot 			 CSR_VERSION_MAJOR(csr->version),
317aee94f86SFrançois Tigeot 			 CSR_VERSION_MINOR(csr->version),
3181487f786SFrançois Tigeot 			 CSR_VERSION_MAJOR(required_version),
3191487f786SFrançois Tigeot 			 CSR_VERSION_MINOR(required_version));
320aee94f86SFrançois Tigeot 		return NULL;
321aee94f86SFrançois Tigeot 	}
322aee94f86SFrançois Tigeot 
32319c468b4SFrançois Tigeot 	readcount += sizeof(struct intel_css_header);
32419c468b4SFrançois Tigeot 
32519c468b4SFrançois Tigeot 	/* Extract Package Header information*/
3264be47400SFrançois Tigeot 	package_header = (struct intel_package_header *)
32719c468b4SFrançois Tigeot 		&fw->data[readcount];
32819c468b4SFrançois Tigeot 	if (sizeof(struct intel_package_header) !=
32919c468b4SFrançois Tigeot 	    (package_header->header_len * 4)) {
330*3f2dd94aSFrançois Tigeot 		DRM_ERROR("DMC firmware has wrong package header length "
331*3f2dd94aSFrançois Tigeot 			  "(%u bytes)\n",
33219c468b4SFrançois Tigeot 			  (package_header->header_len * 4));
333aee94f86SFrançois Tigeot 		return NULL;
33419c468b4SFrançois Tigeot 	}
33519c468b4SFrançois Tigeot 	readcount += sizeof(struct intel_package_header);
33619c468b4SFrançois Tigeot 
33719c468b4SFrançois Tigeot 	/* Search for dmc_offset to find firware binary. */
33819c468b4SFrançois Tigeot 	for (i = 0; i < package_header->num_entries; i++) {
33919c468b4SFrançois Tigeot 		if (package_header->fw_info[i].substepping == '*' &&
3408621f407SFrançois Tigeot 		    si->stepping == package_header->fw_info[i].stepping) {
34119c468b4SFrançois Tigeot 			dmc_offset = package_header->fw_info[i].offset;
34219c468b4SFrançois Tigeot 			break;
3438621f407SFrançois Tigeot 		} else if (si->stepping == package_header->fw_info[i].stepping &&
3448621f407SFrançois Tigeot 			   si->substepping == package_header->fw_info[i].substepping) {
34519c468b4SFrançois Tigeot 			dmc_offset = package_header->fw_info[i].offset;
34619c468b4SFrançois Tigeot 			break;
34719c468b4SFrançois Tigeot 		} else if (package_header->fw_info[i].stepping == '*' &&
34819c468b4SFrançois Tigeot 			   package_header->fw_info[i].substepping == '*')
34919c468b4SFrançois Tigeot 			dmc_offset = package_header->fw_info[i].offset;
35019c468b4SFrançois Tigeot 	}
35119c468b4SFrançois Tigeot 	if (dmc_offset == CSR_DEFAULT_FW_OFFSET) {
352*3f2dd94aSFrançois Tigeot 		DRM_ERROR("DMC firmware not supported for %c stepping\n",
3538621f407SFrançois Tigeot 			  si->stepping);
354aee94f86SFrançois Tigeot 		return NULL;
35519c468b4SFrançois Tigeot 	}
35619c468b4SFrançois Tigeot 	readcount += dmc_offset;
35719c468b4SFrançois Tigeot 
35819c468b4SFrançois Tigeot 	/* Extract dmc_header information. */
3594be47400SFrançois Tigeot 	dmc_header = (struct intel_dmc_header *)&fw->data[readcount];
36019c468b4SFrançois Tigeot 	if (sizeof(struct intel_dmc_header) != (dmc_header->header_len)) {
361*3f2dd94aSFrançois Tigeot 		DRM_ERROR("DMC firmware has wrong dmc header length "
362*3f2dd94aSFrançois Tigeot 			  "(%u bytes)\n",
36319c468b4SFrançois Tigeot 			  (dmc_header->header_len));
364aee94f86SFrançois Tigeot 		return NULL;
36519c468b4SFrançois Tigeot 	}
36619c468b4SFrançois Tigeot 	readcount += sizeof(struct intel_dmc_header);
36719c468b4SFrançois Tigeot 
36819c468b4SFrançois Tigeot 	/* Cache the dmc header info. */
36919c468b4SFrançois Tigeot 	if (dmc_header->mmio_count > ARRAY_SIZE(csr->mmioaddr)) {
370*3f2dd94aSFrançois Tigeot 		DRM_ERROR("DMC firmware has wrong mmio count %u\n",
37119c468b4SFrançois Tigeot 			  dmc_header->mmio_count);
372aee94f86SFrançois Tigeot 		return NULL;
37319c468b4SFrançois Tigeot 	}
37419c468b4SFrançois Tigeot 	csr->mmio_count = dmc_header->mmio_count;
37519c468b4SFrançois Tigeot 	for (i = 0; i < dmc_header->mmio_count; i++) {
376a05eeebfSFrançois Tigeot 		if (dmc_header->mmioaddr[i] < CSR_MMIO_START_RANGE ||
37719c468b4SFrançois Tigeot 		    dmc_header->mmioaddr[i] > CSR_MMIO_END_RANGE) {
378*3f2dd94aSFrançois Tigeot 			DRM_ERROR("DMC firmware has wrong mmio address 0x%x\n",
37919c468b4SFrançois Tigeot 				  dmc_header->mmioaddr[i]);
380aee94f86SFrançois Tigeot 			return NULL;
38119c468b4SFrançois Tigeot 		}
382aee94f86SFrançois Tigeot 		csr->mmioaddr[i] = _MMIO(dmc_header->mmioaddr[i]);
38319c468b4SFrançois Tigeot 		csr->mmiodata[i] = dmc_header->mmiodata[i];
38419c468b4SFrançois Tigeot 	}
38519c468b4SFrançois Tigeot 
38619c468b4SFrançois Tigeot 	/* fw_size is in dwords, so multiplied by 4 to convert into bytes. */
38719c468b4SFrançois Tigeot 	nbytes = dmc_header->fw_size * 4;
38819c468b4SFrançois Tigeot 	if (nbytes > CSR_MAX_FW_SIZE) {
389*3f2dd94aSFrançois Tigeot 		DRM_ERROR("DMC firmware too big (%u bytes)\n", nbytes);
390aee94f86SFrançois Tigeot 		return NULL;
39119c468b4SFrançois Tigeot 	}
39219c468b4SFrançois Tigeot 	csr->dmc_fw_size = dmc_header->fw_size;
39319c468b4SFrançois Tigeot 
3944be47400SFrançois Tigeot 	dmc_payload = kmalloc(nbytes, M_DRM, GFP_KERNEL);
395aee94f86SFrançois Tigeot 	if (!dmc_payload) {
39619c468b4SFrançois Tigeot 		DRM_ERROR("Memory allocation failed for dmc payload\n");
397aee94f86SFrançois Tigeot 		return NULL;
39819c468b4SFrançois Tigeot 	}
39919c468b4SFrançois Tigeot 
4008621f407SFrançois Tigeot 	return memcpy(dmc_payload, &fw->data[readcount], nbytes);
401aee94f86SFrançois Tigeot }
40219c468b4SFrançois Tigeot 
csr_load_work_fn(struct work_struct * work)403aee94f86SFrançois Tigeot static void csr_load_work_fn(struct work_struct *work)
404aee94f86SFrançois Tigeot {
405aee94f86SFrançois Tigeot 	struct drm_i915_private *dev_priv;
406aee94f86SFrançois Tigeot 	struct intel_csr *csr;
407a85cb24fSFrançois Tigeot 	const struct firmware *fw = NULL;
408aee94f86SFrançois Tigeot 
409aee94f86SFrançois Tigeot 	dev_priv = container_of(work, typeof(*dev_priv), csr.work);
410aee94f86SFrançois Tigeot 	csr = &dev_priv->csr;
411aee94f86SFrançois Tigeot 
412a85cb24fSFrançois Tigeot 	request_firmware(&fw, dev_priv->csr.fw_path, &dev_priv->drm.pdev->dev);
4138621f407SFrançois Tigeot 	if (fw)
414aee94f86SFrançois Tigeot 		dev_priv->csr.dmc_payload = parse_csr_fw(dev_priv, fw);
415aee94f86SFrançois Tigeot 
4168621f407SFrançois Tigeot 	if (dev_priv->csr.dmc_payload) {
417aee94f86SFrançois Tigeot 		intel_csr_load_program(dev_priv);
418aee94f86SFrançois Tigeot 
419aee94f86SFrançois Tigeot 		intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
420aee94f86SFrançois Tigeot 
421a85cb24fSFrançois Tigeot 		DRM_INFO("Finished loading DMC firmware %s (v%u.%u)\n",
422aee94f86SFrançois Tigeot 			 dev_priv->csr.fw_path,
423aee94f86SFrançois Tigeot 			 CSR_VERSION_MAJOR(csr->version),
424aee94f86SFrançois Tigeot 			 CSR_VERSION_MINOR(csr->version));
425aee94f86SFrançois Tigeot 	} else {
4264be47400SFrançois Tigeot 		dev_notice(dev_priv->drm.dev,
427*3f2dd94aSFrançois Tigeot 			   "Failed to load DMC firmware %s."
428*3f2dd94aSFrançois Tigeot 			   " Disabling runtime power management.\n",
429*3f2dd94aSFrançois Tigeot 			   csr->fw_path);
430*3f2dd94aSFrançois Tigeot 		dev_notice(dev_priv->drm.dev, "DMC firmware homepage: %s",
431*3f2dd94aSFrançois Tigeot 			   INTEL_UC_FIRMWARE_URL);
432aee94f86SFrançois Tigeot 	}
43319c468b4SFrançois Tigeot 
43419c468b4SFrançois Tigeot 	release_firmware(fw);
43519c468b4SFrançois Tigeot }
43619c468b4SFrançois Tigeot 
43719c468b4SFrançois Tigeot /**
43819c468b4SFrançois Tigeot  * intel_csr_ucode_init() - initialize the firmware loading.
439aee94f86SFrançois Tigeot  * @dev_priv: i915 drm device.
44019c468b4SFrançois Tigeot  *
44119c468b4SFrançois Tigeot  * This function is called at the time of loading the display driver to read
44219c468b4SFrançois Tigeot  * firmware from a .bin file and copied into a internal memory.
44319c468b4SFrançois Tigeot  */
intel_csr_ucode_init(struct drm_i915_private * dev_priv)444aee94f86SFrançois Tigeot void intel_csr_ucode_init(struct drm_i915_private *dev_priv)
44519c468b4SFrançois Tigeot {
44619c468b4SFrançois Tigeot 	struct intel_csr *csr = &dev_priv->csr;
44719c468b4SFrançois Tigeot 
448aee94f86SFrançois Tigeot 	INIT_WORK(&dev_priv->csr.work, csr_load_work_fn);
449aee94f86SFrançois Tigeot 
450aee94f86SFrançois Tigeot 	if (!HAS_CSR(dev_priv))
45119c468b4SFrançois Tigeot 		return;
45219c468b4SFrançois Tigeot 
453*3f2dd94aSFrançois Tigeot 	if (IS_CANNONLAKE(dev_priv))
454*3f2dd94aSFrançois Tigeot 		csr->fw_path = I915_CSR_CNL;
455*3f2dd94aSFrançois Tigeot 	else if (IS_GEMINILAKE(dev_priv))
456a85cb24fSFrançois Tigeot 		csr->fw_path = I915_CSR_GLK;
457*3f2dd94aSFrançois Tigeot 	else if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv))
4588621f407SFrançois Tigeot 		csr->fw_path = I915_CSR_KBL;
4598621f407SFrançois Tigeot 	else if (IS_SKYLAKE(dev_priv))
46019c468b4SFrançois Tigeot 		csr->fw_path = I915_CSR_SKL;
461352ff8bdSFrançois Tigeot 	else if (IS_BROXTON(dev_priv))
462352ff8bdSFrançois Tigeot 		csr->fw_path = I915_CSR_BXT;
46319c468b4SFrançois Tigeot 	else {
46419c468b4SFrançois Tigeot 		DRM_ERROR("Unexpected: no known CSR firmware for platform\n");
46519c468b4SFrançois Tigeot 		return;
46619c468b4SFrançois Tigeot 	}
46719c468b4SFrançois Tigeot 
468a05eeebfSFrançois Tigeot 	DRM_DEBUG_KMS("Loading %s\n", csr->fw_path);
469a05eeebfSFrançois Tigeot 
47019c468b4SFrançois Tigeot 	/*
47119c468b4SFrançois Tigeot 	 * Obtain a runtime pm reference, until CSR is loaded,
47219c468b4SFrançois Tigeot 	 * to avoid entering runtime-suspend.
47319c468b4SFrançois Tigeot 	 */
474aee94f86SFrançois Tigeot 	intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
47519c468b4SFrançois Tigeot 
476aee94f86SFrançois Tigeot 	schedule_work(&dev_priv->csr.work);
47719c468b4SFrançois Tigeot }
47819c468b4SFrançois Tigeot 
47919c468b4SFrançois Tigeot /**
4808621f407SFrançois Tigeot  * intel_csr_ucode_suspend() - prepare CSR firmware before system suspend
4818621f407SFrançois Tigeot  * @dev_priv: i915 drm device
4828621f407SFrançois Tigeot  *
4838621f407SFrançois Tigeot  * Prepare the DMC firmware before entering system suspend. This includes
4848621f407SFrançois Tigeot  * flushing pending work items and releasing any resources acquired during
4858621f407SFrançois Tigeot  * init.
4868621f407SFrançois Tigeot  */
intel_csr_ucode_suspend(struct drm_i915_private * dev_priv)4878621f407SFrançois Tigeot void intel_csr_ucode_suspend(struct drm_i915_private *dev_priv)
4888621f407SFrançois Tigeot {
4898621f407SFrançois Tigeot 	if (!HAS_CSR(dev_priv))
4908621f407SFrançois Tigeot 		return;
4918621f407SFrançois Tigeot 
4928621f407SFrançois Tigeot 	flush_work(&dev_priv->csr.work);
4938621f407SFrançois Tigeot 
4948621f407SFrançois Tigeot 	/* Drop the reference held in case DMC isn't loaded. */
4958621f407SFrançois Tigeot 	if (!dev_priv->csr.dmc_payload)
4968621f407SFrançois Tigeot 		intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
4978621f407SFrançois Tigeot }
4988621f407SFrançois Tigeot 
4998621f407SFrançois Tigeot /**
5008621f407SFrançois Tigeot  * intel_csr_ucode_resume() - init CSR firmware during system resume
5018621f407SFrançois Tigeot  * @dev_priv: i915 drm device
5028621f407SFrançois Tigeot  *
5038621f407SFrançois Tigeot  * Reinitialize the DMC firmware during system resume, reacquiring any
5048621f407SFrançois Tigeot  * resources released in intel_csr_ucode_suspend().
5058621f407SFrançois Tigeot  */
intel_csr_ucode_resume(struct drm_i915_private * dev_priv)5068621f407SFrançois Tigeot void intel_csr_ucode_resume(struct drm_i915_private *dev_priv)
5078621f407SFrançois Tigeot {
5088621f407SFrançois Tigeot 	if (!HAS_CSR(dev_priv))
5098621f407SFrançois Tigeot 		return;
5108621f407SFrançois Tigeot 
5118621f407SFrançois Tigeot 	/*
5128621f407SFrançois Tigeot 	 * Reacquire the reference to keep RPM disabled in case DMC isn't
5138621f407SFrançois Tigeot 	 * loaded.
5148621f407SFrançois Tigeot 	 */
5158621f407SFrançois Tigeot 	if (!dev_priv->csr.dmc_payload)
5168621f407SFrançois Tigeot 		intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
5178621f407SFrançois Tigeot }
5188621f407SFrançois Tigeot 
5198621f407SFrançois Tigeot /**
52019c468b4SFrançois Tigeot  * intel_csr_ucode_fini() - unload the CSR firmware.
521aee94f86SFrançois Tigeot  * @dev_priv: i915 drm device.
52219c468b4SFrançois Tigeot  *
5238621f407SFrançois Tigeot  * Firmmware unloading includes freeing the internal memory and reset the
52419c468b4SFrançois Tigeot  * firmware loading status.
52519c468b4SFrançois Tigeot  */
intel_csr_ucode_fini(struct drm_i915_private * dev_priv)526aee94f86SFrançois Tigeot void intel_csr_ucode_fini(struct drm_i915_private *dev_priv)
52719c468b4SFrançois Tigeot {
528aee94f86SFrançois Tigeot 	if (!HAS_CSR(dev_priv))
52919c468b4SFrançois Tigeot 		return;
53019c468b4SFrançois Tigeot 
5318621f407SFrançois Tigeot 	intel_csr_ucode_suspend(dev_priv);
53219c468b4SFrançois Tigeot 
533aee94f86SFrançois Tigeot 	kfree(dev_priv->csr.dmc_payload);
53419c468b4SFrançois Tigeot }
535