xref: /linux/drivers/gpu/drm/xe/xe_uc_fw.c (revision 473b62763b76e8bb0793ac5f030779c43ecd79e6)
1dd08ebf6SMatthew Brost // SPDX-License-Identifier: MIT
2dd08ebf6SMatthew Brost /*
3dd08ebf6SMatthew Brost  * Copyright © 2022 Intel Corporation
4dd08ebf6SMatthew Brost  */
5dd08ebf6SMatthew Brost 
6dd08ebf6SMatthew Brost #include <linux/bitfield.h>
7dd08ebf6SMatthew Brost #include <linux/firmware.h>
8dd08ebf6SMatthew Brost 
9dd08ebf6SMatthew Brost #include <drm/drm_managed.h>
10dd08ebf6SMatthew Brost 
11a9b1a136SLucas De Marchi #include "regs/xe_guc_regs.h"
12dd08ebf6SMatthew Brost #include "xe_bo.h"
13dd08ebf6SMatthew Brost #include "xe_device_types.h"
14dd08ebf6SMatthew Brost #include "xe_force_wake.h"
15985d5a49SDaniele Ceraolo Spurio #include "xe_gsc.h"
16dd08ebf6SMatthew Brost #include "xe_gt.h"
17dd08ebf6SMatthew Brost #include "xe_map.h"
18dd08ebf6SMatthew Brost #include "xe_mmio.h"
19a455ed04SDaniele Ceraolo Spurio #include "xe_module.h"
20dd08ebf6SMatthew Brost #include "xe_uc_fw.h"
21dd08ebf6SMatthew Brost 
22ad55ead7SLucas De Marchi /*
23ad55ead7SLucas De Marchi  * List of required GuC and HuC binaries per-platform. They must be ordered
24ad55ead7SLucas De Marchi  * based on platform, from newer to older.
25ad55ead7SLucas De Marchi  *
26ad55ead7SLucas De Marchi  * Versioning follows the guidelines from
27ad55ead7SLucas De Marchi  * Documentation/driver-api/firmware/firmware-usage-guidelines.rst. There is a
28ad55ead7SLucas De Marchi  * distinction for platforms being officially supported by the driver or not.
29ad55ead7SLucas De Marchi  * Platforms not available publicly or not yet officially supported by the
30ad55ead7SLucas De Marchi  * driver (under force-probe), use the mmp_ver(): the firmware autoselect logic
31ad55ead7SLucas De Marchi  * will select the firmware from disk with filename that matches the full
32ad55ead7SLucas De Marchi  * "mpp version", i.e. major.minor.patch. mmp_ver() should only be used for
33ad55ead7SLucas De Marchi  * this case.
34ad55ead7SLucas De Marchi  *
35ad55ead7SLucas De Marchi  * For platforms officially supported by the driver, the filename always only
36ad55ead7SLucas De Marchi  * ever contains the major version (GuC) or no version at all (HuC).
37ad55ead7SLucas De Marchi  *
38ad55ead7SLucas De Marchi  * After loading the file, the driver parses the versions embedded in the blob.
39ad55ead7SLucas De Marchi  * The major version needs to match a major version supported by the driver (if
40ad55ead7SLucas De Marchi  * any). The minor version is also checked and a notice emitted to the log if
41ad55ead7SLucas De Marchi  * the version found is smaller than the version wanted. This is done only for
42ad55ead7SLucas De Marchi  * informational purposes so users may have a chance to upgrade, but the driver
43ad55ead7SLucas De Marchi  * still loads and use the older firmware.
44ad55ead7SLucas De Marchi  *
45ad55ead7SLucas De Marchi  * Examples:
46ad55ead7SLucas De Marchi  *
47ad55ead7SLucas De Marchi  *	1) Platform officially supported by i915 - using Tigerlake as example.
48ad55ead7SLucas De Marchi  *	   Driver loads the following firmware blobs from disk:
49ad55ead7SLucas De Marchi  *
50ad55ead7SLucas De Marchi  *		- i915/tgl_guc_<major>.bin
51ad55ead7SLucas De Marchi  *		- i915/tgl_huc.bin
52ad55ead7SLucas De Marchi  *
53ad55ead7SLucas De Marchi  *	   <major> number for GuC is checked that it matches the version inside
54ad55ead7SLucas De Marchi  *	   the blob. <minor> version is checked and if smaller than the expected
55ad55ead7SLucas De Marchi  *	   an info message is emitted about that.
56ad55ead7SLucas De Marchi  *
57ad55ead7SLucas De Marchi  *	1) XE_<FUTUREINTELPLATFORM>, still under require_force_probe. Using
58ad55ead7SLucas De Marchi  *	   "wipplat" as a short-name. Driver loads the following firmware blobs
59ad55ead7SLucas De Marchi  *	   from disk:
60ad55ead7SLucas De Marchi  *
61ad55ead7SLucas De Marchi  *		- xe/wipplat_guc_<major>.<minor>.<patch>.bin
62ad55ead7SLucas De Marchi  *		- xe/wipplat_huc_<major>.<minor>.<patch>.bin
63ad55ead7SLucas De Marchi  *
64ad55ead7SLucas De Marchi  *	   <major> and <minor> are checked that they match the version inside
65ad55ead7SLucas De Marchi  *	   the blob. Both of them need to match exactly what the driver is
66ad55ead7SLucas De Marchi  *	   expecting, otherwise it fails.
67ad55ead7SLucas De Marchi  *
68ad55ead7SLucas De Marchi  *	3) Platform officially supported by xe and out of force-probe. Using
69ad55ead7SLucas De Marchi  *	   "plat" as a short-name. Except for the different directory, the
70ad55ead7SLucas De Marchi  *	   behavior is the same as (1). Driver loads the following firmware
71ad55ead7SLucas De Marchi  *	   blobs from disk:
72ad55ead7SLucas De Marchi  *
73ad55ead7SLucas De Marchi  *		- xe/plat_guc_<major>.bin
74ad55ead7SLucas De Marchi  *		- xe/plat_huc.bin
75ad55ead7SLucas De Marchi  *
76ad55ead7SLucas De Marchi  *	   <major> number for GuC is checked that it matches the version inside
77ad55ead7SLucas De Marchi  *	   the blob. <minor> version is checked and if smaller than the expected
78ad55ead7SLucas De Marchi  *	   an info message is emitted about that.
79ad55ead7SLucas De Marchi  *
80ad55ead7SLucas De Marchi  * For the platforms already released with a major version, they should never be
81ad55ead7SLucas De Marchi  * removed from the table. Instead new entries with newer versions may be added
82ad55ead7SLucas De Marchi  * before them, so they take precedence.
83ad55ead7SLucas De Marchi  *
84ad55ead7SLucas De Marchi  * TODO: Currently there's no fallback on major version. That's because xe
85ad55ead7SLucas De Marchi  * driver only supports the one major version of each firmware in the table.
86ad55ead7SLucas De Marchi  * This needs to be fixed when the major version of GuC is updated.
87ad55ead7SLucas De Marchi  */
88ad55ead7SLucas De Marchi 
89ad55ead7SLucas De Marchi struct uc_fw_entry {
90ad55ead7SLucas De Marchi 	enum xe_platform platform;
91ad55ead7SLucas De Marchi 	struct {
92ad55ead7SLucas De Marchi 		const char *path;
93ad55ead7SLucas De Marchi 		u16 major;
94ad55ead7SLucas De Marchi 		u16 minor;
95ad55ead7SLucas De Marchi 		bool full_ver_required;
96ad55ead7SLucas De Marchi 	};
97ad55ead7SLucas De Marchi };
98ad55ead7SLucas De Marchi 
99ad55ead7SLucas De Marchi struct fw_blobs_by_type {
100ad55ead7SLucas De Marchi 	const struct uc_fw_entry *entries;
101ad55ead7SLucas De Marchi 	u32 count;
102ad55ead7SLucas De Marchi };
103ad55ead7SLucas De Marchi 
104ad55ead7SLucas De Marchi #define XE_GUC_FIRMWARE_DEFS(fw_def, mmp_ver, major_ver)			\
105943c01b7SMatt Roper 	fw_def(LUNARLAKE,	mmp_ver(xe,	guc,	lnl,	70, 6, 8))	\
106430003b8SDaniele Ceraolo Spurio 	fw_def(METEORLAKE,	major_ver(i915,	guc,	mtl,	70, 7))		\
1079b497627SDaniele Ceraolo Spurio 	fw_def(PVC,		mmp_ver(xe,	guc,	pvc,	70, 9, 1))	\
108ad55ead7SLucas De Marchi 	fw_def(DG2,		major_ver(i915,	guc,	dg2,	70, 5))		\
109ad55ead7SLucas De Marchi 	fw_def(DG1,		major_ver(i915,	guc,	dg1,	70, 5))		\
110500f9062SMatt Roper 	fw_def(ALDERLAKE_N,	major_ver(i915,	guc,	tgl,	70, 5))		\
111ad55ead7SLucas De Marchi 	fw_def(ALDERLAKE_P,	major_ver(i915,	guc,	adlp,	70, 5))		\
112ad55ead7SLucas De Marchi 	fw_def(ALDERLAKE_S,	major_ver(i915,	guc,	tgl,	70, 5))		\
11394324e6bSAnusha Srivatsa 	fw_def(ROCKETLAKE,	major_ver(i915,	guc,	tgl,	70, 5))		\
114ad55ead7SLucas De Marchi 	fw_def(TIGERLAKE,	major_ver(i915,	guc,	tgl,	70, 5))
115ad55ead7SLucas De Marchi 
116ad55ead7SLucas De Marchi #define XE_HUC_FIRMWARE_DEFS(fw_def, mmp_ver, no_ver)		\
117bfeb4ac5SDaniele Ceraolo Spurio 	fw_def(METEORLAKE,	no_ver(i915,	huc_gsc,	mtl))		\
118420c6a6fSDaniele Ceraolo Spurio 	fw_def(DG1,		no_ver(i915,	huc,		dg1))		\
11985635f5dSLucas De Marchi 	fw_def(ALDERLAKE_P,	no_ver(i915,	huc,		tgl))		\
120ad55ead7SLucas De Marchi 	fw_def(ALDERLAKE_S,	no_ver(i915,	huc,		tgl))		\
12194324e6bSAnusha Srivatsa 	fw_def(ROCKETLAKE,	no_ver(i915,	huc,		tgl))		\
122ad55ead7SLucas De Marchi 	fw_def(TIGERLAKE,	no_ver(i915,	huc,		tgl))
123ad55ead7SLucas De Marchi 
1245152234eSDaniele Ceraolo Spurio /* for the GSC FW we match the compatibility version and not the release one */
1255152234eSDaniele Ceraolo Spurio #define XE_GSC_FIRMWARE_DEFS(fw_def, major_ver)		\
1265152234eSDaniele Ceraolo Spurio 	fw_def(METEORLAKE,	major_ver(i915,	gsc,	mtl,	1, 0))
1275152234eSDaniele Ceraolo Spurio 
128ad55ead7SLucas De Marchi #define MAKE_FW_PATH(dir__, uc__, shortname__, version__)			\
129ad55ead7SLucas De Marchi 	__stringify(dir__) "/" __stringify(shortname__) "_" __stringify(uc__) version__ ".bin"
130ad55ead7SLucas De Marchi 
131ad55ead7SLucas De Marchi #define fw_filename_mmp_ver(dir_, uc_, shortname_, a, b, c)			\
132ad55ead7SLucas De Marchi 	MAKE_FW_PATH(dir_, uc_, shortname_, "_" __stringify(a ## . ## b ## . ## c))
133ad55ead7SLucas De Marchi #define fw_filename_major_ver(dir_, uc_, shortname_, a, b)			\
134ad55ead7SLucas De Marchi 	MAKE_FW_PATH(dir_, uc_, shortname_, "_" __stringify(a))
135ad55ead7SLucas De Marchi #define fw_filename_no_ver(dir_, uc_, shortname_)				\
136ad55ead7SLucas De Marchi 	MAKE_FW_PATH(dir_, uc_, shortname_, "")
137ad55ead7SLucas De Marchi 
138ad55ead7SLucas De Marchi #define uc_fw_entry_mmp_ver(dir_, uc_, shortname_, a, b, c)			\
139ad55ead7SLucas De Marchi 	{ fw_filename_mmp_ver(dir_, uc_, shortname_, a, b, c),			\
140ad55ead7SLucas De Marchi 	  a, b, true }
141ad55ead7SLucas De Marchi #define uc_fw_entry_major_ver(dir_, uc_, shortname_, a, b)			\
142ad55ead7SLucas De Marchi 	{ fw_filename_major_ver(dir_, uc_, shortname_, a, b),			\
143ad55ead7SLucas De Marchi 	  a, b }
144ad55ead7SLucas De Marchi #define uc_fw_entry_no_ver(dir_, uc_, shortname_)				\
145ad55ead7SLucas De Marchi 	{ fw_filename_no_ver(dir_, uc_, shortname_),				\
146ad55ead7SLucas De Marchi 	  0, 0 }
147ad55ead7SLucas De Marchi 
148ad55ead7SLucas De Marchi /* All blobs need to be declared via MODULE_FIRMWARE() */
149ad55ead7SLucas De Marchi #define XE_UC_MODULE_FIRMWARE(platform__, fw_filename)				\
150ad55ead7SLucas De Marchi 	MODULE_FIRMWARE(fw_filename);
151ad55ead7SLucas De Marchi 
152ad55ead7SLucas De Marchi #define XE_UC_FW_ENTRY(platform__, entry__)					\
153ad55ead7SLucas De Marchi 	{									\
154ad55ead7SLucas De Marchi 		.platform = XE_ ## platform__,					\
155ad55ead7SLucas De Marchi 		entry__,							\
156ad55ead7SLucas De Marchi 	},
157ad55ead7SLucas De Marchi 
1583e8e7ee6SFrancois Dugast XE_GUC_FIRMWARE_DEFS(XE_UC_MODULE_FIRMWARE,
159ad55ead7SLucas De Marchi 		     fw_filename_mmp_ver, fw_filename_major_ver)
1603e8e7ee6SFrancois Dugast XE_HUC_FIRMWARE_DEFS(XE_UC_MODULE_FIRMWARE,
161ad55ead7SLucas De Marchi 		     fw_filename_mmp_ver, fw_filename_no_ver)
1625152234eSDaniele Ceraolo Spurio XE_GSC_FIRMWARE_DEFS(XE_UC_MODULE_FIRMWARE, fw_filename_major_ver)
163ad55ead7SLucas De Marchi 
164dd08ebf6SMatthew Brost static struct xe_gt *
165dd08ebf6SMatthew Brost __uc_fw_to_gt(struct xe_uc_fw *uc_fw, enum xe_uc_fw_type type)
166dd08ebf6SMatthew Brost {
1670d1caff4SDaniele Ceraolo Spurio 	XE_WARN_ON(type >= XE_UC_FW_NUM_TYPES);
168dd08ebf6SMatthew Brost 
1690d1caff4SDaniele Ceraolo Spurio 	switch (type) {
1700d1caff4SDaniele Ceraolo Spurio 	case XE_UC_FW_TYPE_GUC:
1710d1caff4SDaniele Ceraolo Spurio 		return container_of(uc_fw, struct xe_gt, uc.guc.fw);
1720d1caff4SDaniele Ceraolo Spurio 	case XE_UC_FW_TYPE_HUC:
173dd08ebf6SMatthew Brost 		return container_of(uc_fw, struct xe_gt, uc.huc.fw);
1740d1caff4SDaniele Ceraolo Spurio 	case XE_UC_FW_TYPE_GSC:
1750d1caff4SDaniele Ceraolo Spurio 		return container_of(uc_fw, struct xe_gt, uc.gsc.fw);
1760d1caff4SDaniele Ceraolo Spurio 	default:
1770d1caff4SDaniele Ceraolo Spurio 		return NULL;
1780d1caff4SDaniele Ceraolo Spurio 	}
179dd08ebf6SMatthew Brost }
180dd08ebf6SMatthew Brost 
181dd08ebf6SMatthew Brost static struct xe_gt *uc_fw_to_gt(struct xe_uc_fw *uc_fw)
182dd08ebf6SMatthew Brost {
183dd08ebf6SMatthew Brost 	return __uc_fw_to_gt(uc_fw, uc_fw->type);
184dd08ebf6SMatthew Brost }
185dd08ebf6SMatthew Brost 
186dd08ebf6SMatthew Brost static struct xe_device *uc_fw_to_xe(struct xe_uc_fw *uc_fw)
187dd08ebf6SMatthew Brost {
188dd08ebf6SMatthew Brost 	return gt_to_xe(uc_fw_to_gt(uc_fw));
189dd08ebf6SMatthew Brost }
190dd08ebf6SMatthew Brost 
191dd08ebf6SMatthew Brost static void
192dd08ebf6SMatthew Brost uc_fw_auto_select(struct xe_device *xe, struct xe_uc_fw *uc_fw)
193dd08ebf6SMatthew Brost {
194ad55ead7SLucas De Marchi 	static const struct uc_fw_entry entries_guc[] = {
195ad55ead7SLucas De Marchi 		XE_GUC_FIRMWARE_DEFS(XE_UC_FW_ENTRY,
196ad55ead7SLucas De Marchi 				     uc_fw_entry_mmp_ver,
197ad55ead7SLucas De Marchi 				     uc_fw_entry_major_ver)
198dd08ebf6SMatthew Brost 	};
199ad55ead7SLucas De Marchi 	static const struct uc_fw_entry entries_huc[] = {
200ad55ead7SLucas De Marchi 		XE_HUC_FIRMWARE_DEFS(XE_UC_FW_ENTRY,
201ad55ead7SLucas De Marchi 				     uc_fw_entry_mmp_ver,
202ad55ead7SLucas De Marchi 				     uc_fw_entry_no_ver)
203dd08ebf6SMatthew Brost 	};
2045152234eSDaniele Ceraolo Spurio 	static const struct uc_fw_entry entries_gsc[] = {
2055152234eSDaniele Ceraolo Spurio 		XE_GSC_FIRMWARE_DEFS(XE_UC_FW_ENTRY, uc_fw_entry_major_ver)
2065152234eSDaniele Ceraolo Spurio 	};
207dd08ebf6SMatthew Brost 	static const struct fw_blobs_by_type blobs_all[XE_UC_FW_NUM_TYPES] = {
208ad55ead7SLucas De Marchi 		[XE_UC_FW_TYPE_GUC] = { entries_guc, ARRAY_SIZE(entries_guc) },
209ad55ead7SLucas De Marchi 		[XE_UC_FW_TYPE_HUC] = { entries_huc, ARRAY_SIZE(entries_huc) },
2105152234eSDaniele Ceraolo Spurio 		[XE_UC_FW_TYPE_GSC] = { entries_gsc, ARRAY_SIZE(entries_gsc) },
211dd08ebf6SMatthew Brost 	};
212ad55ead7SLucas De Marchi 	static const struct uc_fw_entry *entries;
213dd08ebf6SMatthew Brost 	enum xe_platform p = xe->info.platform;
214ad55ead7SLucas De Marchi 	u32 count;
215dd08ebf6SMatthew Brost 	int i;
216dd08ebf6SMatthew Brost 
217c73acc1eSFrancois Dugast 	xe_assert(xe, uc_fw->type < ARRAY_SIZE(blobs_all));
218ad55ead7SLucas De Marchi 	entries = blobs_all[uc_fw->type].entries;
219ad55ead7SLucas De Marchi 	count = blobs_all[uc_fw->type].count;
220dd08ebf6SMatthew Brost 
221ad55ead7SLucas De Marchi 	for (i = 0; i < count && p <= entries[i].platform; i++) {
222ad55ead7SLucas De Marchi 		if (p == entries[i].platform) {
223ad55ead7SLucas De Marchi 			uc_fw->path = entries[i].path;
2242e7227b4SDaniele Ceraolo Spurio 			uc_fw->versions.wanted.major = entries[i].major;
2252e7227b4SDaniele Ceraolo Spurio 			uc_fw->versions.wanted.minor = entries[i].minor;
226ad55ead7SLucas De Marchi 			uc_fw->full_ver_required = entries[i].full_ver_required;
2272e7227b4SDaniele Ceraolo Spurio 
2280881cbe0SDaniele Ceraolo Spurio 			if (uc_fw->type == XE_UC_FW_TYPE_GSC)
2290881cbe0SDaniele Ceraolo Spurio 				uc_fw->versions.wanted_type = XE_UC_FW_VER_COMPATIBILITY;
2300881cbe0SDaniele Ceraolo Spurio 			else
2312e7227b4SDaniele Ceraolo Spurio 				uc_fw->versions.wanted_type = XE_UC_FW_VER_RELEASE;
2320881cbe0SDaniele Ceraolo Spurio 
233dd08ebf6SMatthew Brost 			break;
234dd08ebf6SMatthew Brost 		}
235dd08ebf6SMatthew Brost 	}
236dd08ebf6SMatthew Brost }
237dd08ebf6SMatthew Brost 
238a455ed04SDaniele Ceraolo Spurio static void
239a455ed04SDaniele Ceraolo Spurio uc_fw_override(struct xe_uc_fw *uc_fw)
240a455ed04SDaniele Ceraolo Spurio {
241a455ed04SDaniele Ceraolo Spurio 	char *path_override = NULL;
242a455ed04SDaniele Ceraolo Spurio 
243a455ed04SDaniele Ceraolo Spurio 	/* empty string disables, but it's not allowed for GuC */
244a455ed04SDaniele Ceraolo Spurio 	switch (uc_fw->type) {
245a455ed04SDaniele Ceraolo Spurio 	case XE_UC_FW_TYPE_GUC:
246adce1b39SBommithi Sakeena 		if (xe_modparam.guc_firmware_path && *xe_modparam.guc_firmware_path)
247adce1b39SBommithi Sakeena 			path_override = xe_modparam.guc_firmware_path;
248a455ed04SDaniele Ceraolo Spurio 		break;
249a455ed04SDaniele Ceraolo Spurio 	case XE_UC_FW_TYPE_HUC:
250adce1b39SBommithi Sakeena 		path_override = xe_modparam.huc_firmware_path;
251a455ed04SDaniele Ceraolo Spurio 		break;
2525152234eSDaniele Ceraolo Spurio 	case XE_UC_FW_TYPE_GSC:
2535152234eSDaniele Ceraolo Spurio 		path_override = xe_modparam.gsc_firmware_path;
2545152234eSDaniele Ceraolo Spurio 		break;
255a455ed04SDaniele Ceraolo Spurio 	default:
256a455ed04SDaniele Ceraolo Spurio 		break;
257a455ed04SDaniele Ceraolo Spurio 	}
258a455ed04SDaniele Ceraolo Spurio 
259a455ed04SDaniele Ceraolo Spurio 	if (path_override) {
260a455ed04SDaniele Ceraolo Spurio 		uc_fw->path = path_override;
261a455ed04SDaniele Ceraolo Spurio 		uc_fw->user_overridden = true;
262a455ed04SDaniele Ceraolo Spurio 	}
263a455ed04SDaniele Ceraolo Spurio }
264a455ed04SDaniele Ceraolo Spurio 
265dd08ebf6SMatthew Brost /**
266dd08ebf6SMatthew Brost  * xe_uc_fw_copy_rsa - copy fw RSA to buffer
267dd08ebf6SMatthew Brost  *
268dd08ebf6SMatthew Brost  * @uc_fw: uC firmware
269dd08ebf6SMatthew Brost  * @dst: dst buffer
270dd08ebf6SMatthew Brost  * @max_len: max number of bytes to copy
271dd08ebf6SMatthew Brost  *
272dd08ebf6SMatthew Brost  * Return: number of copied bytes.
273dd08ebf6SMatthew Brost  */
274dd08ebf6SMatthew Brost size_t xe_uc_fw_copy_rsa(struct xe_uc_fw *uc_fw, void *dst, u32 max_len)
275dd08ebf6SMatthew Brost {
276dd08ebf6SMatthew Brost 	struct xe_device *xe = uc_fw_to_xe(uc_fw);
277dd08ebf6SMatthew Brost 	u32 size = min_t(u32, uc_fw->rsa_size, max_len);
278dd08ebf6SMatthew Brost 
279c73acc1eSFrancois Dugast 	xe_assert(xe, !(size % 4));
280c73acc1eSFrancois Dugast 	xe_assert(xe, xe_uc_fw_is_available(uc_fw));
281dd08ebf6SMatthew Brost 
282dd08ebf6SMatthew Brost 	xe_map_memcpy_from(xe, dst, &uc_fw->bo->vmap,
283dd08ebf6SMatthew Brost 			   xe_uc_fw_rsa_offset(uc_fw), size);
284dd08ebf6SMatthew Brost 
285dd08ebf6SMatthew Brost 	return size;
286dd08ebf6SMatthew Brost }
287dd08ebf6SMatthew Brost 
288dd08ebf6SMatthew Brost static void uc_fw_fini(struct drm_device *drm, void *arg)
289dd08ebf6SMatthew Brost {
290dd08ebf6SMatthew Brost 	struct xe_uc_fw *uc_fw = arg;
291dd08ebf6SMatthew Brost 
292dd08ebf6SMatthew Brost 	if (!xe_uc_fw_is_available(uc_fw))
293dd08ebf6SMatthew Brost 		return;
294dd08ebf6SMatthew Brost 
295dd08ebf6SMatthew Brost 	xe_bo_unpin_map_no_vm(uc_fw->bo);
296dd08ebf6SMatthew Brost 	xe_uc_fw_change_status(uc_fw, XE_UC_FIRMWARE_SELECTED);
297dd08ebf6SMatthew Brost }
298dd08ebf6SMatthew Brost 
29999c821b0SMatthew Brost static void guc_read_css_info(struct xe_uc_fw *uc_fw, struct uc_css_header *css)
30099c821b0SMatthew Brost {
30199c821b0SMatthew Brost 	struct xe_gt *gt = uc_fw_to_gt(uc_fw);
3022e7227b4SDaniele Ceraolo Spurio 	struct xe_uc_fw_version *release = &uc_fw->versions.found[XE_UC_FW_VER_RELEASE];
3032e7227b4SDaniele Ceraolo Spurio 	struct xe_uc_fw_version *compatibility = &uc_fw->versions.found[XE_UC_FW_VER_COMPATIBILITY];
30499c821b0SMatthew Brost 
305c73acc1eSFrancois Dugast 	xe_gt_assert(gt, uc_fw->type == XE_UC_FW_TYPE_GUC);
3062e7227b4SDaniele Ceraolo Spurio 	xe_gt_assert(gt, release->major >= 70);
30799c821b0SMatthew Brost 
3082e7227b4SDaniele Ceraolo Spurio 	if (release->major > 70 || release->minor >= 6) {
30999c821b0SMatthew Brost 		/* v70.6.0 adds CSS header support */
3102e7227b4SDaniele Ceraolo Spurio 		compatibility->major = FIELD_GET(CSS_SW_VERSION_UC_MAJOR,
31199c821b0SMatthew Brost 						 css->submission_version);
3122e7227b4SDaniele Ceraolo Spurio 		compatibility->minor = FIELD_GET(CSS_SW_VERSION_UC_MINOR,
31399c821b0SMatthew Brost 						 css->submission_version);
3142e7227b4SDaniele Ceraolo Spurio 		compatibility->patch = FIELD_GET(CSS_SW_VERSION_UC_PATCH,
31599c821b0SMatthew Brost 						 css->submission_version);
3162e7227b4SDaniele Ceraolo Spurio 	} else if (release->minor >= 3) {
31799c821b0SMatthew Brost 		/* v70.3.0 introduced v1.1.0 */
3182e7227b4SDaniele Ceraolo Spurio 		compatibility->major = 1;
3192e7227b4SDaniele Ceraolo Spurio 		compatibility->minor = 1;
3202e7227b4SDaniele Ceraolo Spurio 		compatibility->patch = 0;
32199c821b0SMatthew Brost 	} else {
32299c821b0SMatthew Brost 		/* v70.0.0 introduced v1.0.0 */
3232e7227b4SDaniele Ceraolo Spurio 		compatibility->major = 1;
3242e7227b4SDaniele Ceraolo Spurio 		compatibility->minor = 0;
3252e7227b4SDaniele Ceraolo Spurio 		compatibility->patch = 0;
32699c821b0SMatthew Brost 	}
32799c821b0SMatthew Brost 
32899c821b0SMatthew Brost 	uc_fw->private_data_size = css->private_data_size;
32999c821b0SMatthew Brost }
33099c821b0SMatthew Brost 
3310881cbe0SDaniele Ceraolo Spurio int xe_uc_fw_check_version_requirements(struct xe_uc_fw *uc_fw)
332ad55ead7SLucas De Marchi {
333ad55ead7SLucas De Marchi 	struct xe_device *xe = uc_fw_to_xe(uc_fw);
3342e7227b4SDaniele Ceraolo Spurio 	struct xe_uc_fw_version *wanted = &uc_fw->versions.wanted;
3352e7227b4SDaniele Ceraolo Spurio 	struct xe_uc_fw_version *found = &uc_fw->versions.found[uc_fw->versions.wanted_type];
336ad55ead7SLucas De Marchi 
337ad55ead7SLucas De Marchi 	/* Driver has no requirement on any version, any is good. */
3382e7227b4SDaniele Ceraolo Spurio 	if (!wanted->major)
339ad55ead7SLucas De Marchi 		return 0;
340ad55ead7SLucas De Marchi 
341ad55ead7SLucas De Marchi 	/*
342ad55ead7SLucas De Marchi 	 * If full version is required, both major and minor should match.
343ad55ead7SLucas De Marchi 	 * Otherwise, at least the major version.
344ad55ead7SLucas De Marchi 	 */
3452e7227b4SDaniele Ceraolo Spurio 	if (wanted->major != found->major ||
3462e7227b4SDaniele Ceraolo Spurio 	    (uc_fw->full_ver_required && wanted->minor != found->minor)) {
347ad55ead7SLucas De Marchi 		drm_notice(&xe->drm, "%s firmware %s: unexpected version: %u.%u != %u.%u\n",
348ad55ead7SLucas De Marchi 			   xe_uc_fw_type_repr(uc_fw->type), uc_fw->path,
3492e7227b4SDaniele Ceraolo Spurio 			   found->major, found->minor,
3502e7227b4SDaniele Ceraolo Spurio 			   wanted->major, wanted->minor);
351ad55ead7SLucas De Marchi 		goto fail;
352ad55ead7SLucas De Marchi 	}
353ad55ead7SLucas De Marchi 
3542e7227b4SDaniele Ceraolo Spurio 	if (wanted->minor > found->minor) {
355ad55ead7SLucas De Marchi 		drm_notice(&xe->drm, "%s firmware (%u.%u) is recommended, but only (%u.%u) was found in %s\n",
356ad55ead7SLucas De Marchi 			   xe_uc_fw_type_repr(uc_fw->type),
3572e7227b4SDaniele Ceraolo Spurio 			   wanted->major, wanted->minor,
3582e7227b4SDaniele Ceraolo Spurio 			   found->major, found->minor,
359ad55ead7SLucas De Marchi 			   uc_fw->path);
360ad55ead7SLucas De Marchi 		drm_info(&xe->drm, "Consider updating your linux-firmware pkg or downloading from %s\n",
361ad55ead7SLucas De Marchi 			 XE_UC_FIRMWARE_URL);
362ad55ead7SLucas De Marchi 	}
363ad55ead7SLucas De Marchi 
364ad55ead7SLucas De Marchi 	return 0;
365ad55ead7SLucas De Marchi 
366ad55ead7SLucas De Marchi fail:
367ad55ead7SLucas De Marchi 	if (xe_uc_fw_is_overridden(uc_fw))
368ad55ead7SLucas De Marchi 		return 0;
369ad55ead7SLucas De Marchi 
370ad55ead7SLucas De Marchi 	return -ENOEXEC;
371ad55ead7SLucas De Marchi }
372ad55ead7SLucas De Marchi 
373a9a95523SDaniele Ceraolo Spurio /* Refer to the "CSS-based Firmware Layout" documentation entry for details */
374a9a95523SDaniele Ceraolo Spurio static int parse_css_header(struct xe_uc_fw *uc_fw, const void *fw_data, size_t fw_size)
375a9a95523SDaniele Ceraolo Spurio {
376a9a95523SDaniele Ceraolo Spurio 	struct xe_device *xe = uc_fw_to_xe(uc_fw);
3772e7227b4SDaniele Ceraolo Spurio 	struct xe_uc_fw_version *release = &uc_fw->versions.found[XE_UC_FW_VER_RELEASE];
378a9a95523SDaniele Ceraolo Spurio 	struct uc_css_header *css;
379a9a95523SDaniele Ceraolo Spurio 	size_t size;
380a9a95523SDaniele Ceraolo Spurio 
381a9a95523SDaniele Ceraolo Spurio 	/* Check the size of the blob before examining buffer contents */
382a9a95523SDaniele Ceraolo Spurio 	if (unlikely(fw_size < sizeof(struct uc_css_header))) {
383a9a95523SDaniele Ceraolo Spurio 		drm_warn(&xe->drm, "%s firmware %s: invalid size: %zu < %zu\n",
384a9a95523SDaniele Ceraolo Spurio 			 xe_uc_fw_type_repr(uc_fw->type), uc_fw->path,
385a9a95523SDaniele Ceraolo Spurio 			 fw_size, sizeof(struct uc_css_header));
386a9a95523SDaniele Ceraolo Spurio 		return -ENODATA;
387a9a95523SDaniele Ceraolo Spurio 	}
388a9a95523SDaniele Ceraolo Spurio 
389a9a95523SDaniele Ceraolo Spurio 	css = (struct uc_css_header *)fw_data;
390a9a95523SDaniele Ceraolo Spurio 
391a9a95523SDaniele Ceraolo Spurio 	/* Check integrity of size values inside CSS header */
392a9a95523SDaniele Ceraolo Spurio 	size = (css->header_size_dw - css->key_size_dw - css->modulus_size_dw -
393a9a95523SDaniele Ceraolo Spurio 		css->exponent_size_dw) * sizeof(u32);
394a9a95523SDaniele Ceraolo Spurio 	if (unlikely(size != sizeof(struct uc_css_header))) {
395a9a95523SDaniele Ceraolo Spurio 		drm_warn(&xe->drm,
396a9a95523SDaniele Ceraolo Spurio 			 "%s firmware %s: unexpected header size: %zu != %zu\n",
397a9a95523SDaniele Ceraolo Spurio 			 xe_uc_fw_type_repr(uc_fw->type), uc_fw->path,
398a9a95523SDaniele Ceraolo Spurio 			 fw_size, sizeof(struct uc_css_header));
399a9a95523SDaniele Ceraolo Spurio 		return -EPROTO;
400a9a95523SDaniele Ceraolo Spurio 	}
401a9a95523SDaniele Ceraolo Spurio 
402a9a95523SDaniele Ceraolo Spurio 	/* uCode size must calculated from other sizes */
403a9a95523SDaniele Ceraolo Spurio 	uc_fw->ucode_size = (css->size_dw - css->header_size_dw) * sizeof(u32);
404a9a95523SDaniele Ceraolo Spurio 
405a9a95523SDaniele Ceraolo Spurio 	/* now RSA */
406a9a95523SDaniele Ceraolo Spurio 	uc_fw->rsa_size = css->key_size_dw * sizeof(u32);
407a9a95523SDaniele Ceraolo Spurio 
408a9a95523SDaniele Ceraolo Spurio 	/* At least, it should have header, uCode and RSA. Size of all three. */
409a9a95523SDaniele Ceraolo Spurio 	size = sizeof(struct uc_css_header) + uc_fw->ucode_size +
410a9a95523SDaniele Ceraolo Spurio 		uc_fw->rsa_size;
411a9a95523SDaniele Ceraolo Spurio 	if (unlikely(fw_size < size)) {
412a9a95523SDaniele Ceraolo Spurio 		drm_warn(&xe->drm, "%s firmware %s: invalid size: %zu < %zu\n",
413a9a95523SDaniele Ceraolo Spurio 			 xe_uc_fw_type_repr(uc_fw->type), uc_fw->path,
414a9a95523SDaniele Ceraolo Spurio 			 fw_size, size);
415a9a95523SDaniele Ceraolo Spurio 		return -ENOEXEC;
416a9a95523SDaniele Ceraolo Spurio 	}
417a9a95523SDaniele Ceraolo Spurio 
418a9a95523SDaniele Ceraolo Spurio 	/* Get version numbers from the CSS header */
4192e7227b4SDaniele Ceraolo Spurio 	release->major = FIELD_GET(CSS_SW_VERSION_UC_MAJOR, css->sw_version);
4202e7227b4SDaniele Ceraolo Spurio 	release->minor = FIELD_GET(CSS_SW_VERSION_UC_MINOR, css->sw_version);
4212e7227b4SDaniele Ceraolo Spurio 	release->patch = FIELD_GET(CSS_SW_VERSION_UC_PATCH, css->sw_version);
422a9a95523SDaniele Ceraolo Spurio 
423a9a95523SDaniele Ceraolo Spurio 	if (uc_fw->type == XE_UC_FW_TYPE_GUC)
424a9a95523SDaniele Ceraolo Spurio 		guc_read_css_info(uc_fw, css);
425a9a95523SDaniele Ceraolo Spurio 
426a9a95523SDaniele Ceraolo Spurio 	return 0;
427a9a95523SDaniele Ceraolo Spurio }
428a9a95523SDaniele Ceraolo Spurio 
429484ecffaSDaniele Ceraolo Spurio static bool is_cpd_header(const void *data)
430484ecffaSDaniele Ceraolo Spurio {
431484ecffaSDaniele Ceraolo Spurio 	const u32 *marker = data;
432484ecffaSDaniele Ceraolo Spurio 
433484ecffaSDaniele Ceraolo Spurio 	return *marker == GSC_CPD_HEADER_MARKER;
434484ecffaSDaniele Ceraolo Spurio }
435484ecffaSDaniele Ceraolo Spurio 
436484ecffaSDaniele Ceraolo Spurio static u32 entry_offset(const struct gsc_cpd_header_v2 *header, const char *name)
437484ecffaSDaniele Ceraolo Spurio {
438484ecffaSDaniele Ceraolo Spurio 	const struct gsc_cpd_entry *entry;
439484ecffaSDaniele Ceraolo Spurio 	int i;
440484ecffaSDaniele Ceraolo Spurio 
441484ecffaSDaniele Ceraolo Spurio 	entry = (void *)header + header->header_length;
442484ecffaSDaniele Ceraolo Spurio 
443484ecffaSDaniele Ceraolo Spurio 	for (i = 0; i < header->num_of_entries; i++, entry++)
444484ecffaSDaniele Ceraolo Spurio 		if (strcmp(entry->name, name) == 0)
445484ecffaSDaniele Ceraolo Spurio 			return entry->offset & GSC_CPD_ENTRY_OFFSET_MASK;
446484ecffaSDaniele Ceraolo Spurio 
447484ecffaSDaniele Ceraolo Spurio 	return 0;
448484ecffaSDaniele Ceraolo Spurio }
449484ecffaSDaniele Ceraolo Spurio 
450484ecffaSDaniele Ceraolo Spurio /* Refer to the "GSC-based Firmware Layout" documentation entry for details */
451484ecffaSDaniele Ceraolo Spurio static int parse_cpd_header(struct xe_uc_fw *uc_fw, const void *data, size_t size,
452484ecffaSDaniele Ceraolo Spurio 			    const char *manifest_entry, const char *css_entry)
453484ecffaSDaniele Ceraolo Spurio {
454484ecffaSDaniele Ceraolo Spurio 	struct xe_gt *gt = uc_fw_to_gt(uc_fw);
455484ecffaSDaniele Ceraolo Spurio 	struct xe_device *xe = gt_to_xe(gt);
456484ecffaSDaniele Ceraolo Spurio 	const struct gsc_cpd_header_v2 *header = data;
4572e7227b4SDaniele Ceraolo Spurio 	struct xe_uc_fw_version *release = &uc_fw->versions.found[XE_UC_FW_VER_RELEASE];
458484ecffaSDaniele Ceraolo Spurio 	const struct gsc_manifest_header *manifest;
459484ecffaSDaniele Ceraolo Spurio 	size_t min_size = sizeof(*header);
460484ecffaSDaniele Ceraolo Spurio 	u32 offset;
461484ecffaSDaniele Ceraolo Spurio 
462484ecffaSDaniele Ceraolo Spurio 	/* manifest_entry is mandatory, css_entry is optional */
463484ecffaSDaniele Ceraolo Spurio 	xe_assert(xe, manifest_entry);
464484ecffaSDaniele Ceraolo Spurio 
465484ecffaSDaniele Ceraolo Spurio 	if (size < min_size || !is_cpd_header(header))
466484ecffaSDaniele Ceraolo Spurio 		return -ENOENT;
467484ecffaSDaniele Ceraolo Spurio 
468484ecffaSDaniele Ceraolo Spurio 	if (header->header_length < sizeof(struct gsc_cpd_header_v2)) {
469484ecffaSDaniele Ceraolo Spurio 		xe_gt_err(gt, "invalid CPD header length %u!\n", header->header_length);
470484ecffaSDaniele Ceraolo Spurio 		return -EINVAL;
471484ecffaSDaniele Ceraolo Spurio 	}
472484ecffaSDaniele Ceraolo Spurio 
473484ecffaSDaniele Ceraolo Spurio 	min_size = header->header_length + sizeof(struct gsc_cpd_entry) * header->num_of_entries;
474484ecffaSDaniele Ceraolo Spurio 	if (size < min_size) {
475484ecffaSDaniele Ceraolo Spurio 		xe_gt_err(gt, "FW too small! %zu < %zu\n", size, min_size);
476484ecffaSDaniele Ceraolo Spurio 		return -ENODATA;
477484ecffaSDaniele Ceraolo Spurio 	}
478484ecffaSDaniele Ceraolo Spurio 
479484ecffaSDaniele Ceraolo Spurio 	/* Look for the manifest first */
480484ecffaSDaniele Ceraolo Spurio 	offset = entry_offset(header, manifest_entry);
481484ecffaSDaniele Ceraolo Spurio 	if (!offset) {
482484ecffaSDaniele Ceraolo Spurio 		xe_gt_err(gt, "Failed to find %s manifest!\n",
483484ecffaSDaniele Ceraolo Spurio 			  xe_uc_fw_type_repr(uc_fw->type));
484484ecffaSDaniele Ceraolo Spurio 		return -ENODATA;
485484ecffaSDaniele Ceraolo Spurio 	}
486484ecffaSDaniele Ceraolo Spurio 
487484ecffaSDaniele Ceraolo Spurio 	min_size = offset + sizeof(struct gsc_manifest_header);
488484ecffaSDaniele Ceraolo Spurio 	if (size < min_size) {
489484ecffaSDaniele Ceraolo Spurio 		xe_gt_err(gt, "FW too small! %zu < %zu\n", size, min_size);
490484ecffaSDaniele Ceraolo Spurio 		return -ENODATA;
491484ecffaSDaniele Ceraolo Spurio 	}
492484ecffaSDaniele Ceraolo Spurio 
493484ecffaSDaniele Ceraolo Spurio 	manifest = data + offset;
494484ecffaSDaniele Ceraolo Spurio 
4952e7227b4SDaniele Ceraolo Spurio 	release->major = manifest->fw_version.major;
4962e7227b4SDaniele Ceraolo Spurio 	release->minor = manifest->fw_version.minor;
4972e7227b4SDaniele Ceraolo Spurio 	release->patch = manifest->fw_version.hotfix;
498484ecffaSDaniele Ceraolo Spurio 
499985d5a49SDaniele Ceraolo Spurio 	if (uc_fw->type == XE_UC_FW_TYPE_GSC) {
500985d5a49SDaniele Ceraolo Spurio 		struct xe_gsc *gsc = container_of(uc_fw, struct xe_gsc, fw);
501985d5a49SDaniele Ceraolo Spurio 
502985d5a49SDaniele Ceraolo Spurio 		release->build = manifest->fw_version.build;
503985d5a49SDaniele Ceraolo Spurio 		gsc->security_version = manifest->security_version;
504985d5a49SDaniele Ceraolo Spurio 	}
505985d5a49SDaniele Ceraolo Spurio 
506484ecffaSDaniele Ceraolo Spurio 	/* then optionally look for the css header */
507484ecffaSDaniele Ceraolo Spurio 	if (css_entry) {
508484ecffaSDaniele Ceraolo Spurio 		int ret;
509484ecffaSDaniele Ceraolo Spurio 
510484ecffaSDaniele Ceraolo Spurio 		/*
511484ecffaSDaniele Ceraolo Spurio 		 * This section does not contain a CSS entry on DG2. We
512484ecffaSDaniele Ceraolo Spurio 		 * don't support DG2 HuC right now, so no need to handle
513484ecffaSDaniele Ceraolo Spurio 		 * it, just add a reminder in case that changes.
514484ecffaSDaniele Ceraolo Spurio 		 */
515484ecffaSDaniele Ceraolo Spurio 		xe_assert(xe, xe->info.platform != XE_DG2);
516484ecffaSDaniele Ceraolo Spurio 
517484ecffaSDaniele Ceraolo Spurio 		offset = entry_offset(header, css_entry);
518484ecffaSDaniele Ceraolo Spurio 
519484ecffaSDaniele Ceraolo Spurio 		/* the CSS header parser will check that the CSS header fits */
520484ecffaSDaniele Ceraolo Spurio 		if (offset > size) {
521484ecffaSDaniele Ceraolo Spurio 			xe_gt_err(gt, "FW too small! %zu < %u\n", size, offset);
522484ecffaSDaniele Ceraolo Spurio 			return -ENODATA;
523484ecffaSDaniele Ceraolo Spurio 		}
524484ecffaSDaniele Ceraolo Spurio 
525484ecffaSDaniele Ceraolo Spurio 		ret = parse_css_header(uc_fw, data + offset, size - offset);
526484ecffaSDaniele Ceraolo Spurio 		if (ret)
527484ecffaSDaniele Ceraolo Spurio 			return ret;
528484ecffaSDaniele Ceraolo Spurio 
529484ecffaSDaniele Ceraolo Spurio 		uc_fw->css_offset = offset;
530484ecffaSDaniele Ceraolo Spurio 	}
531484ecffaSDaniele Ceraolo Spurio 
532484ecffaSDaniele Ceraolo Spurio 	return 0;
533484ecffaSDaniele Ceraolo Spurio }
534484ecffaSDaniele Ceraolo Spurio 
535985d5a49SDaniele Ceraolo Spurio static int parse_gsc_layout(struct xe_uc_fw *uc_fw, const void *data, size_t size)
536985d5a49SDaniele Ceraolo Spurio {
537985d5a49SDaniele Ceraolo Spurio 	struct xe_gt *gt = uc_fw_to_gt(uc_fw);
538985d5a49SDaniele Ceraolo Spurio 	const struct gsc_layout_pointers *layout = data;
539985d5a49SDaniele Ceraolo Spurio 	const struct gsc_bpdt_header *bpdt_header = NULL;
540985d5a49SDaniele Ceraolo Spurio 	const struct gsc_bpdt_entry *bpdt_entry = NULL;
541985d5a49SDaniele Ceraolo Spurio 	size_t min_size = sizeof(*layout);
542985d5a49SDaniele Ceraolo Spurio 	int i;
543985d5a49SDaniele Ceraolo Spurio 
544985d5a49SDaniele Ceraolo Spurio 	if (size < min_size) {
545985d5a49SDaniele Ceraolo Spurio 		xe_gt_err(gt, "GSC FW too small! %zu < %zu\n", size, min_size);
546985d5a49SDaniele Ceraolo Spurio 		return -ENODATA;
547985d5a49SDaniele Ceraolo Spurio 	}
548985d5a49SDaniele Ceraolo Spurio 
549985d5a49SDaniele Ceraolo Spurio 	min_size = layout->boot1.offset + layout->boot1.size;
550985d5a49SDaniele Ceraolo Spurio 	if (size < min_size) {
551985d5a49SDaniele Ceraolo Spurio 		xe_gt_err(gt, "GSC FW too small for boot section! %zu < %zu\n",
552985d5a49SDaniele Ceraolo Spurio 			  size, min_size);
553985d5a49SDaniele Ceraolo Spurio 		return -ENODATA;
554985d5a49SDaniele Ceraolo Spurio 	}
555985d5a49SDaniele Ceraolo Spurio 
556985d5a49SDaniele Ceraolo Spurio 	min_size = sizeof(*bpdt_header);
557985d5a49SDaniele Ceraolo Spurio 	if (layout->boot1.size < min_size) {
558985d5a49SDaniele Ceraolo Spurio 		xe_gt_err(gt, "GSC FW boot section too small for BPDT header: %u < %zu\n",
559985d5a49SDaniele Ceraolo Spurio 			  layout->boot1.size, min_size);
560985d5a49SDaniele Ceraolo Spurio 		return -ENODATA;
561985d5a49SDaniele Ceraolo Spurio 	}
562985d5a49SDaniele Ceraolo Spurio 
563985d5a49SDaniele Ceraolo Spurio 	bpdt_header = data + layout->boot1.offset;
564985d5a49SDaniele Ceraolo Spurio 	if (bpdt_header->signature != GSC_BPDT_HEADER_SIGNATURE) {
565985d5a49SDaniele Ceraolo Spurio 		xe_gt_err(gt, "invalid signature for BPDT header: 0x%08x!\n",
566985d5a49SDaniele Ceraolo Spurio 			  bpdt_header->signature);
567985d5a49SDaniele Ceraolo Spurio 		return -EINVAL;
568985d5a49SDaniele Ceraolo Spurio 	}
569985d5a49SDaniele Ceraolo Spurio 
570985d5a49SDaniele Ceraolo Spurio 	min_size += sizeof(*bpdt_entry) * bpdt_header->descriptor_count;
571985d5a49SDaniele Ceraolo Spurio 	if (layout->boot1.size < min_size) {
572985d5a49SDaniele Ceraolo Spurio 		xe_gt_err(gt, "GSC FW boot section too small for BPDT entries: %u < %zu\n",
573985d5a49SDaniele Ceraolo Spurio 			  layout->boot1.size, min_size);
574985d5a49SDaniele Ceraolo Spurio 		return -ENODATA;
575985d5a49SDaniele Ceraolo Spurio 	}
576985d5a49SDaniele Ceraolo Spurio 
577985d5a49SDaniele Ceraolo Spurio 	bpdt_entry = (void *)bpdt_header + sizeof(*bpdt_header);
578985d5a49SDaniele Ceraolo Spurio 	for (i = 0; i < bpdt_header->descriptor_count; i++, bpdt_entry++) {
579985d5a49SDaniele Ceraolo Spurio 		if ((bpdt_entry->type & GSC_BPDT_ENTRY_TYPE_MASK) !=
580985d5a49SDaniele Ceraolo Spurio 		    GSC_BPDT_ENTRY_TYPE_GSC_RBE)
581985d5a49SDaniele Ceraolo Spurio 			continue;
582985d5a49SDaniele Ceraolo Spurio 
583985d5a49SDaniele Ceraolo Spurio 		min_size = bpdt_entry->sub_partition_offset;
584985d5a49SDaniele Ceraolo Spurio 
585985d5a49SDaniele Ceraolo Spurio 		/* the CPD header parser will check that the CPD header fits */
586985d5a49SDaniele Ceraolo Spurio 		if (layout->boot1.size < min_size) {
587985d5a49SDaniele Ceraolo Spurio 			xe_gt_err(gt, "GSC FW boot section too small for CPD offset: %u < %zu\n",
588985d5a49SDaniele Ceraolo Spurio 				  layout->boot1.size, min_size);
589985d5a49SDaniele Ceraolo Spurio 			return -ENODATA;
590985d5a49SDaniele Ceraolo Spurio 		}
591985d5a49SDaniele Ceraolo Spurio 
592985d5a49SDaniele Ceraolo Spurio 		return parse_cpd_header(uc_fw,
593985d5a49SDaniele Ceraolo Spurio 					(void *)bpdt_header + min_size,
594985d5a49SDaniele Ceraolo Spurio 					layout->boot1.size - min_size,
595985d5a49SDaniele Ceraolo Spurio 					"RBEP.man", NULL);
596985d5a49SDaniele Ceraolo Spurio 	}
597985d5a49SDaniele Ceraolo Spurio 
598985d5a49SDaniele Ceraolo Spurio 	xe_gt_err(gt, "couldn't find CPD header in GSC binary!\n");
599985d5a49SDaniele Ceraolo Spurio 	return -ENODATA;
600985d5a49SDaniele Ceraolo Spurio }
601985d5a49SDaniele Ceraolo Spurio 
602a9a95523SDaniele Ceraolo Spurio static int parse_headers(struct xe_uc_fw *uc_fw, const struct firmware *fw)
603a9a95523SDaniele Ceraolo Spurio {
604484ecffaSDaniele Ceraolo Spurio 	int ret;
605484ecffaSDaniele Ceraolo Spurio 
606484ecffaSDaniele Ceraolo Spurio 	/*
607484ecffaSDaniele Ceraolo Spurio 	 * All GuC releases and older HuC ones use CSS headers, while newer HuC
608484ecffaSDaniele Ceraolo Spurio 	 * releases use GSC CPD headers.
609484ecffaSDaniele Ceraolo Spurio 	 */
610484ecffaSDaniele Ceraolo Spurio 	switch (uc_fw->type) {
611985d5a49SDaniele Ceraolo Spurio 	case XE_UC_FW_TYPE_GSC:
612985d5a49SDaniele Ceraolo Spurio 		return parse_gsc_layout(uc_fw, fw->data, fw->size);
613484ecffaSDaniele Ceraolo Spurio 	case XE_UC_FW_TYPE_HUC:
614484ecffaSDaniele Ceraolo Spurio 		ret = parse_cpd_header(uc_fw, fw->data, fw->size, "HUCP.man", "huc_fw");
615484ecffaSDaniele Ceraolo Spurio 		if (!ret || ret != -ENOENT)
616484ecffaSDaniele Ceraolo Spurio 			return ret;
617484ecffaSDaniele Ceraolo Spurio 		fallthrough;
618484ecffaSDaniele Ceraolo Spurio 	case XE_UC_FW_TYPE_GUC:
619a9a95523SDaniele Ceraolo Spurio 		return parse_css_header(uc_fw, fw->data, fw->size);
620484ecffaSDaniele Ceraolo Spurio 	default:
621484ecffaSDaniele Ceraolo Spurio 		return -EINVAL;
622484ecffaSDaniele Ceraolo Spurio 	}
623484ecffaSDaniele Ceraolo Spurio 
624484ecffaSDaniele Ceraolo Spurio 	return 0;
625a9a95523SDaniele Ceraolo Spurio }
626a9a95523SDaniele Ceraolo Spurio 
6272e7227b4SDaniele Ceraolo Spurio #define print_uc_fw_version(p_, version_, prefix_, ...) \
6282e7227b4SDaniele Ceraolo Spurio do { \
6292e7227b4SDaniele Ceraolo Spurio 	struct xe_uc_fw_version *ver_ = (version_); \
6302e7227b4SDaniele Ceraolo Spurio 	if (ver_->build) \
6312e7227b4SDaniele Ceraolo Spurio 		drm_printf(p_, prefix_ " version %u.%u.%u.%u\n", ##__VA_ARGS__, \
6322e7227b4SDaniele Ceraolo Spurio 			   ver_->major, ver_->minor, \
6332e7227b4SDaniele Ceraolo Spurio 			   ver_->patch, ver_->build); \
6342e7227b4SDaniele Ceraolo Spurio 	else \
6352e7227b4SDaniele Ceraolo Spurio 		drm_printf(p_, prefix_ " version %u.%u.%u\n", ##__VA_ARGS__, \
6362e7227b4SDaniele Ceraolo Spurio 			  ver_->major, ver_->minor, ver_->patch); \
6372e7227b4SDaniele Ceraolo Spurio } while (0)
6382e7227b4SDaniele Ceraolo Spurio 
639dd08ebf6SMatthew Brost int xe_uc_fw_init(struct xe_uc_fw *uc_fw)
640dd08ebf6SMatthew Brost {
641dd08ebf6SMatthew Brost 	struct xe_device *xe = uc_fw_to_xe(uc_fw);
642dd08ebf6SMatthew Brost 	struct xe_gt *gt = uc_fw_to_gt(uc_fw);
643876611c2SMatt Roper 	struct xe_tile *tile = gt_to_tile(gt);
644dd08ebf6SMatthew Brost 	struct device *dev = xe->drm.dev;
6452e7227b4SDaniele Ceraolo Spurio 	struct drm_printer p = drm_info_printer(dev);
646dd08ebf6SMatthew Brost 	const struct firmware *fw = NULL;
647dd08ebf6SMatthew Brost 	struct xe_bo *obj;
648dd08ebf6SMatthew Brost 	int err;
649dd08ebf6SMatthew Brost 
650dd08ebf6SMatthew Brost 	/*
651dd08ebf6SMatthew Brost 	 * we use FIRMWARE_UNINITIALIZED to detect checks against uc_fw->status
652dd08ebf6SMatthew Brost 	 * before we're looked at the HW caps to see if we have uc support
653dd08ebf6SMatthew Brost 	 */
654dd08ebf6SMatthew Brost 	BUILD_BUG_ON(XE_UC_FIRMWARE_UNINITIALIZED);
655c73acc1eSFrancois Dugast 	xe_assert(xe, !uc_fw->status);
656c73acc1eSFrancois Dugast 	xe_assert(xe, !uc_fw->path);
657dd08ebf6SMatthew Brost 
658dd08ebf6SMatthew Brost 	uc_fw_auto_select(xe, uc_fw);
65975730847SDaniele Ceraolo Spurio 	xe_uc_fw_change_status(uc_fw, uc_fw->path ?
660dd08ebf6SMatthew Brost 			       XE_UC_FIRMWARE_SELECTED :
661dd08ebf6SMatthew Brost 			       XE_UC_FIRMWARE_NOT_SUPPORTED);
662dd08ebf6SMatthew Brost 
66375730847SDaniele Ceraolo Spurio 	if (!xe_uc_fw_is_supported(uc_fw))
66475730847SDaniele Ceraolo Spurio 		return 0;
66575730847SDaniele Ceraolo Spurio 
666a455ed04SDaniele Ceraolo Spurio 	uc_fw_override(uc_fw);
667a455ed04SDaniele Ceraolo Spurio 
668a455ed04SDaniele Ceraolo Spurio 	/* an empty path means the firmware is disabled */
669a455ed04SDaniele Ceraolo Spurio 	if (!xe_device_uc_enabled(xe) || !(*uc_fw->path)) {
670dd08ebf6SMatthew Brost 		xe_uc_fw_change_status(uc_fw, XE_UC_FIRMWARE_DISABLED);
67175730847SDaniele Ceraolo Spurio 		drm_dbg(&xe->drm, "%s disabled", xe_uc_fw_type_repr(uc_fw->type));
67275730847SDaniele Ceraolo Spurio 		return 0;
673dd08ebf6SMatthew Brost 	}
67475730847SDaniele Ceraolo Spurio 
675dd08ebf6SMatthew Brost 	err = request_firmware(&fw, uc_fw->path, dev);
676dd08ebf6SMatthew Brost 	if (err)
677dd08ebf6SMatthew Brost 		goto fail;
678dd08ebf6SMatthew Brost 
679a9a95523SDaniele Ceraolo Spurio 	err = parse_headers(uc_fw, fw);
680a9a95523SDaniele Ceraolo Spurio 	if (err)
681dd08ebf6SMatthew Brost 		goto fail;
682dd08ebf6SMatthew Brost 
6832e7227b4SDaniele Ceraolo Spurio 	print_uc_fw_version(&p,
6842e7227b4SDaniele Ceraolo Spurio 			    &uc_fw->versions.found[XE_UC_FW_VER_RELEASE],
6852e7227b4SDaniele Ceraolo Spurio 			    "Using %s firmware from %s",
6862e7227b4SDaniele Ceraolo Spurio 			    xe_uc_fw_type_repr(uc_fw->type), uc_fw->path);
68761e72e77SLucas De Marchi 
6880881cbe0SDaniele Ceraolo Spurio 	/* for GSC FW we want the compatibility version, which we query after load */
6890881cbe0SDaniele Ceraolo Spurio 	if (uc_fw->type != XE_UC_FW_TYPE_GSC) {
6900881cbe0SDaniele Ceraolo Spurio 		err = xe_uc_fw_check_version_requirements(uc_fw);
691ad55ead7SLucas De Marchi 		if (err)
692dd08ebf6SMatthew Brost 			goto fail;
6930881cbe0SDaniele Ceraolo Spurio 	}
694dd08ebf6SMatthew Brost 
695876611c2SMatt Roper 	obj = xe_bo_create_from_data(xe, tile, fw->data, fw->size,
696dd08ebf6SMatthew Brost 				     ttm_bo_type_kernel,
697876611c2SMatt Roper 				     XE_BO_CREATE_VRAM_IF_DGFX(tile) |
698dd08ebf6SMatthew Brost 				     XE_BO_CREATE_GGTT_BIT);
699dd08ebf6SMatthew Brost 	if (IS_ERR(obj)) {
700dd08ebf6SMatthew Brost 		drm_notice(&xe->drm, "%s firmware %s: failed to create / populate bo",
701dd08ebf6SMatthew Brost 			   xe_uc_fw_type_repr(uc_fw->type), uc_fw->path);
702dd08ebf6SMatthew Brost 		err = PTR_ERR(obj);
703dd08ebf6SMatthew Brost 		goto fail;
704dd08ebf6SMatthew Brost 	}
705dd08ebf6SMatthew Brost 
706dd08ebf6SMatthew Brost 	uc_fw->bo = obj;
707dd08ebf6SMatthew Brost 	uc_fw->size = fw->size;
708dd08ebf6SMatthew Brost 	xe_uc_fw_change_status(uc_fw, XE_UC_FIRMWARE_AVAILABLE);
709dd08ebf6SMatthew Brost 
710dd08ebf6SMatthew Brost 	release_firmware(fw);
711dd08ebf6SMatthew Brost 
712dd08ebf6SMatthew Brost 	err = drmm_add_action_or_reset(&xe->drm, uc_fw_fini, uc_fw);
713dd08ebf6SMatthew Brost 	if (err)
714dd08ebf6SMatthew Brost 		return err;
715dd08ebf6SMatthew Brost 
716dd08ebf6SMatthew Brost 	return 0;
717dd08ebf6SMatthew Brost 
718dd08ebf6SMatthew Brost fail:
719dd08ebf6SMatthew Brost 	xe_uc_fw_change_status(uc_fw, err == -ENOENT ?
720dd08ebf6SMatthew Brost 			       XE_UC_FIRMWARE_MISSING :
721dd08ebf6SMatthew Brost 			       XE_UC_FIRMWARE_ERROR);
722dd08ebf6SMatthew Brost 
723dd08ebf6SMatthew Brost 	drm_notice(&xe->drm, "%s firmware %s: fetch failed with error %d\n",
724dd08ebf6SMatthew Brost 		   xe_uc_fw_type_repr(uc_fw->type), uc_fw->path, err);
725dd08ebf6SMatthew Brost 	drm_info(&xe->drm, "%s firmware(s) can be downloaded from %s\n",
726dd08ebf6SMatthew Brost 		 xe_uc_fw_type_repr(uc_fw->type), XE_UC_FIRMWARE_URL);
727dd08ebf6SMatthew Brost 
728dd08ebf6SMatthew Brost 	release_firmware(fw);		/* OK even if fw is NULL */
729dd08ebf6SMatthew Brost 	return err;
730dd08ebf6SMatthew Brost }
731dd08ebf6SMatthew Brost 
732dd08ebf6SMatthew Brost static u32 uc_fw_ggtt_offset(struct xe_uc_fw *uc_fw)
733dd08ebf6SMatthew Brost {
734dd08ebf6SMatthew Brost 	return xe_bo_ggtt_addr(uc_fw->bo);
735dd08ebf6SMatthew Brost }
736dd08ebf6SMatthew Brost 
737dd08ebf6SMatthew Brost static int uc_fw_xfer(struct xe_uc_fw *uc_fw, u32 offset, u32 dma_flags)
738dd08ebf6SMatthew Brost {
739dd08ebf6SMatthew Brost 	struct xe_device *xe = uc_fw_to_xe(uc_fw);
740dd08ebf6SMatthew Brost 	struct xe_gt *gt = uc_fw_to_gt(uc_fw);
7417aaec3a6SRodrigo Vivi 	u32 src_offset, dma_ctrl;
742dd08ebf6SMatthew Brost 	int ret;
743dd08ebf6SMatthew Brost 
744dd08ebf6SMatthew Brost 	xe_force_wake_assert_held(gt_to_fw(gt), XE_FW_GT);
745dd08ebf6SMatthew Brost 
746dd08ebf6SMatthew Brost 	/* Set the source address for the uCode */
747484ecffaSDaniele Ceraolo Spurio 	src_offset = uc_fw_ggtt_offset(uc_fw) + uc_fw->css_offset;
748ce8bf5bdSLucas De Marchi 	xe_mmio_write32(gt, DMA_ADDR_0_LOW, lower_32_bits(src_offset));
749473b6276SFei Yang 	xe_mmio_write32(gt, DMA_ADDR_0_HIGH,
750473b6276SFei Yang 			upper_32_bits(src_offset) | DMA_ADDRESS_SPACE_GGTT);
751dd08ebf6SMatthew Brost 
752dd08ebf6SMatthew Brost 	/* Set the DMA destination */
753ce8bf5bdSLucas De Marchi 	xe_mmio_write32(gt, DMA_ADDR_1_LOW, offset);
754ce8bf5bdSLucas De Marchi 	xe_mmio_write32(gt, DMA_ADDR_1_HIGH, DMA_ADDRESS_SPACE_WOPCM);
755dd08ebf6SMatthew Brost 
756dd08ebf6SMatthew Brost 	/*
757dd08ebf6SMatthew Brost 	 * Set the transfer size. The header plus uCode will be copied to WOPCM
758dd08ebf6SMatthew Brost 	 * via DMA, excluding any other components
759dd08ebf6SMatthew Brost 	 */
760ce8bf5bdSLucas De Marchi 	xe_mmio_write32(gt, DMA_COPY_SIZE,
761dd08ebf6SMatthew Brost 			sizeof(struct uc_css_header) + uc_fw->ucode_size);
762dd08ebf6SMatthew Brost 
763dd08ebf6SMatthew Brost 	/* Start the DMA */
764ce8bf5bdSLucas De Marchi 	xe_mmio_write32(gt, DMA_CTRL,
765dd08ebf6SMatthew Brost 			_MASKED_BIT_ENABLE(dma_flags | START_DMA));
766dd08ebf6SMatthew Brost 
767dd08ebf6SMatthew Brost 	/* Wait for DMA to finish */
768063e09afSRodrigo Vivi 	ret = xe_mmio_wait32(gt, DMA_CTRL, START_DMA, 0, 100000, &dma_ctrl,
7697dc9b92dSRodrigo Vivi 			     false);
770dd08ebf6SMatthew Brost 	if (ret)
771dd08ebf6SMatthew Brost 		drm_err(&xe->drm, "DMA for %s fw failed, DMA_CTRL=%u\n",
7727aaec3a6SRodrigo Vivi 			xe_uc_fw_type_repr(uc_fw->type), dma_ctrl);
773dd08ebf6SMatthew Brost 
774dd08ebf6SMatthew Brost 	/* Disable the bits once DMA is over */
775ce8bf5bdSLucas De Marchi 	xe_mmio_write32(gt, DMA_CTRL, _MASKED_BIT_DISABLE(dma_flags));
776dd08ebf6SMatthew Brost 
777dd08ebf6SMatthew Brost 	return ret;
778dd08ebf6SMatthew Brost }
779dd08ebf6SMatthew Brost 
780dd08ebf6SMatthew Brost int xe_uc_fw_upload(struct xe_uc_fw *uc_fw, u32 offset, u32 dma_flags)
781dd08ebf6SMatthew Brost {
782dd08ebf6SMatthew Brost 	struct xe_device *xe = uc_fw_to_xe(uc_fw);
783dd08ebf6SMatthew Brost 	int err;
784dd08ebf6SMatthew Brost 
785dd08ebf6SMatthew Brost 	/* make sure the status was cleared the last time we reset the uc */
786c73acc1eSFrancois Dugast 	xe_assert(xe, !xe_uc_fw_is_loaded(uc_fw));
787dd08ebf6SMatthew Brost 
788dd08ebf6SMatthew Brost 	if (!xe_uc_fw_is_loadable(uc_fw))
789dd08ebf6SMatthew Brost 		return -ENOEXEC;
790dd08ebf6SMatthew Brost 
791dd08ebf6SMatthew Brost 	/* Call custom loader */
792dd08ebf6SMatthew Brost 	err = uc_fw_xfer(uc_fw, offset, dma_flags);
793dd08ebf6SMatthew Brost 	if (err)
794dd08ebf6SMatthew Brost 		goto fail;
795dd08ebf6SMatthew Brost 
796dd08ebf6SMatthew Brost 	xe_uc_fw_change_status(uc_fw, XE_UC_FIRMWARE_TRANSFERRED);
797dd08ebf6SMatthew Brost 	return 0;
798dd08ebf6SMatthew Brost 
799dd08ebf6SMatthew Brost fail:
800dd08ebf6SMatthew Brost 	drm_err(&xe->drm, "Failed to load %s firmware %s (%d)\n",
801dd08ebf6SMatthew Brost 		xe_uc_fw_type_repr(uc_fw->type), uc_fw->path,
802dd08ebf6SMatthew Brost 		err);
803dd08ebf6SMatthew Brost 	xe_uc_fw_change_status(uc_fw, XE_UC_FIRMWARE_LOAD_FAIL);
804dd08ebf6SMatthew Brost 	return err;
805dd08ebf6SMatthew Brost }
806dd08ebf6SMatthew Brost 
8072e7227b4SDaniele Ceraolo Spurio static const char *version_type_repr(enum xe_uc_fw_version_types type)
8082e7227b4SDaniele Ceraolo Spurio {
8092e7227b4SDaniele Ceraolo Spurio 	switch (type) {
8102e7227b4SDaniele Ceraolo Spurio 	case XE_UC_FW_VER_RELEASE:
8112e7227b4SDaniele Ceraolo Spurio 		return "release";
8122e7227b4SDaniele Ceraolo Spurio 	case XE_UC_FW_VER_COMPATIBILITY:
8132e7227b4SDaniele Ceraolo Spurio 		return "compatibility";
8142e7227b4SDaniele Ceraolo Spurio 	default:
8152e7227b4SDaniele Ceraolo Spurio 		return "Unknown version type";
8162e7227b4SDaniele Ceraolo Spurio 	}
8172e7227b4SDaniele Ceraolo Spurio }
818dd08ebf6SMatthew Brost 
819dd08ebf6SMatthew Brost void xe_uc_fw_print(struct xe_uc_fw *uc_fw, struct drm_printer *p)
820dd08ebf6SMatthew Brost {
8212e7227b4SDaniele Ceraolo Spurio 	int i;
8222e7227b4SDaniele Ceraolo Spurio 
823dd08ebf6SMatthew Brost 	drm_printf(p, "%s firmware: %s\n",
824dd08ebf6SMatthew Brost 		   xe_uc_fw_type_repr(uc_fw->type), uc_fw->path);
825dd08ebf6SMatthew Brost 	drm_printf(p, "\tstatus: %s\n",
826dd08ebf6SMatthew Brost 		   xe_uc_fw_status_repr(uc_fw->status));
82799c821b0SMatthew Brost 
8282e7227b4SDaniele Ceraolo Spurio 	print_uc_fw_version(p, &uc_fw->versions.wanted, "\twanted %s",
8292e7227b4SDaniele Ceraolo Spurio 			    version_type_repr(uc_fw->versions.wanted_type));
83099c821b0SMatthew Brost 
8312e7227b4SDaniele Ceraolo Spurio 	for (i = 0; i < XE_UC_FW_VER_TYPE_COUNT; i++) {
8322e7227b4SDaniele Ceraolo Spurio 		struct xe_uc_fw_version *ver = &uc_fw->versions.found[i];
8332e7227b4SDaniele Ceraolo Spurio 
8342e7227b4SDaniele Ceraolo Spurio 		if (ver->major)
8352e7227b4SDaniele Ceraolo Spurio 			print_uc_fw_version(p, ver, "\tfound %s",
8362e7227b4SDaniele Ceraolo Spurio 					    version_type_repr(i));
83799c821b0SMatthew Brost 	}
8382e7227b4SDaniele Ceraolo Spurio 
8392e7227b4SDaniele Ceraolo Spurio 	if (uc_fw->ucode_size)
8402e7227b4SDaniele Ceraolo Spurio 		drm_printf(p, "\tuCode: %u bytes\n", uc_fw->ucode_size);
8412e7227b4SDaniele Ceraolo Spurio 	if (uc_fw->rsa_size)
8422e7227b4SDaniele Ceraolo Spurio 		drm_printf(p, "\tRSA: %u bytes\n", uc_fw->rsa_size);
843dd08ebf6SMatthew Brost }
844