xref: /linux/drivers/gpu/drm/xe/xe_uc_fw.c (revision 917b10d90990fd2138b5dbc2d22cfa428c070ade)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #include <linux/bitfield.h>
7 #include <linux/fault-inject.h>
8 #include <linux/firmware.h>
9 
10 #include <drm/drm_managed.h>
11 
12 #include "regs/xe_guc_regs.h"
13 #include "xe_bo.h"
14 #include "xe_device_types.h"
15 #include "xe_force_wake.h"
16 #include "xe_gsc.h"
17 #include "xe_gt.h"
18 #include "xe_gt_printk.h"
19 #include "xe_gt_sriov_vf.h"
20 #include "xe_guc.h"
21 #include "xe_map.h"
22 #include "xe_mmio.h"
23 #include "xe_module.h"
24 #include "xe_sriov.h"
25 #include "xe_uc_fw.h"
26 
27 /*
28  * List of required GuC and HuC binaries per-platform. They must be ordered
29  * based on platform, from newer to older.
30  *
31  * Versioning follows the guidelines from
32  * Documentation/driver-api/firmware/firmware-usage-guidelines.rst. There is a
33  * distinction for platforms being officially supported by the driver or not.
34  * Platforms not available publicly or not yet officially supported by the
35  * driver (under force-probe), use the mmp_ver(): the firmware autoselect logic
36  * will select the firmware from disk with filename that matches the full
37  * "mpp version", i.e. major.minor.patch. mmp_ver() should only be used for
38  * this case.
39  *
40  * For platforms officially supported by the driver, the filename always only
41  * ever contains the major version (GuC) or no version at all (HuC).
42  *
43  * After loading the file, the driver parses the versions embedded in the blob.
44  * The major version needs to match a major version supported by the driver (if
45  * any). The minor version is also checked and a notice emitted to the log if
46  * the version found is smaller than the version wanted. This is done only for
47  * informational purposes so users may have a chance to upgrade, but the driver
48  * still loads and use the older firmware.
49  *
50  * Examples:
51  *
52  *	1) Platform officially supported by i915 - using Tigerlake as example.
53  *	   Driver loads the following firmware blobs from disk:
54  *
55  *		- i915/tgl_guc_<major>.bin
56  *		- i915/tgl_huc.bin
57  *
58  *	   <major> number for GuC is checked that it matches the version inside
59  *	   the blob. <minor> version is checked and if smaller than the expected
60  *	   an info message is emitted about that.
61  *
62  *	1) XE_<FUTUREINTELPLATFORM>, still under require_force_probe. Using
63  *	   "wipplat" as a short-name. Driver loads the following firmware blobs
64  *	   from disk:
65  *
66  *		- xe/wipplat_guc_<major>.<minor>.<patch>.bin
67  *		- xe/wipplat_huc_<major>.<minor>.<patch>.bin
68  *
69  *	   <major> and <minor> are checked that they match the version inside
70  *	   the blob. Both of them need to match exactly what the driver is
71  *	   expecting, otherwise it fails.
72  *
73  *	3) Platform officially supported by xe and out of force-probe. Using
74  *	   "plat" as a short-name. Except for the different directory, the
75  *	   behavior is the same as (1). Driver loads the following firmware
76  *	   blobs from disk:
77  *
78  *		- xe/plat_guc_<major>.bin
79  *		- xe/plat_huc.bin
80  *
81  *	   <major> number for GuC is checked that it matches the version inside
82  *	   the blob. <minor> version is checked and if smaller than the expected
83  *	   an info message is emitted about that.
84  *
85  * For the platforms already released with a major version, they should never be
86  * removed from the table. Instead new entries with newer versions may be added
87  * before them, so they take precedence.
88  *
89  * TODO: Currently there's no fallback on major version. That's because xe
90  * driver only supports the one major version of each firmware in the table.
91  * This needs to be fixed when the major version of GuC is updated.
92  */
93 
94 struct uc_fw_entry {
95 	enum xe_platform platform;
96 	enum xe_gt_type gt_type;
97 
98 	struct {
99 		const char *path;
100 		u16 major;
101 		u16 minor;
102 		u16 patch;
103 		bool full_ver_required;
104 	};
105 };
106 
107 struct fw_blobs_by_type {
108 	const struct uc_fw_entry *entries;
109 	u32 count;
110 };
111 
112 /*
113  * Add an "ANY" define just to convey the meaning it's given here.
114  */
115 #define XE_GT_TYPE_ANY XE_GT_TYPE_UNINITIALIZED
116 
117 #define XE_GUC_FIRMWARE_DEFS(fw_def, mmp_ver, major_ver)					\
118 	fw_def(BATTLEMAGE,	GT_TYPE_ANY,	major_ver(xe,	guc,	bmg,	70, 44, 1))	\
119 	fw_def(LUNARLAKE,	GT_TYPE_ANY,	major_ver(xe,	guc,	lnl,	70, 44, 1))	\
120 	fw_def(METEORLAKE,	GT_TYPE_ANY,	major_ver(i915,	guc,	mtl,	70, 44, 1))	\
121 	fw_def(DG2,		GT_TYPE_ANY,	major_ver(i915,	guc,	dg2,	70, 44, 1))	\
122 	fw_def(DG1,		GT_TYPE_ANY,	major_ver(i915,	guc,	dg1,	70, 44, 1))	\
123 	fw_def(ALDERLAKE_N,	GT_TYPE_ANY,	major_ver(i915,	guc,	tgl,	70, 44, 1))	\
124 	fw_def(ALDERLAKE_P,	GT_TYPE_ANY,	major_ver(i915,	guc,	adlp,	70, 44, 1))	\
125 	fw_def(ALDERLAKE_S,	GT_TYPE_ANY,	major_ver(i915,	guc,	tgl,	70, 44, 1))	\
126 	fw_def(ROCKETLAKE,	GT_TYPE_ANY,	major_ver(i915,	guc,	tgl,	70, 44, 1))	\
127 	fw_def(TIGERLAKE,	GT_TYPE_ANY,	major_ver(i915,	guc,	tgl,	70, 44, 1))
128 
129 #define XE_HUC_FIRMWARE_DEFS(fw_def, mmp_ver, no_ver)		\
130 	fw_def(BATTLEMAGE,	GT_TYPE_ANY,	no_ver(xe,	huc,		bmg))		\
131 	fw_def(LUNARLAKE,	GT_TYPE_ANY,	no_ver(xe,	huc,		lnl))		\
132 	fw_def(METEORLAKE,	GT_TYPE_ANY,	no_ver(i915,	huc_gsc,	mtl))		\
133 	fw_def(DG1,		GT_TYPE_ANY,	no_ver(i915,	huc,		dg1))		\
134 	fw_def(ALDERLAKE_P,	GT_TYPE_ANY,	no_ver(i915,	huc,		tgl))		\
135 	fw_def(ALDERLAKE_S,	GT_TYPE_ANY,	no_ver(i915,	huc,		tgl))		\
136 	fw_def(ROCKETLAKE,	GT_TYPE_ANY,	no_ver(i915,	huc,		tgl))		\
137 	fw_def(TIGERLAKE,	GT_TYPE_ANY,	no_ver(i915,	huc,		tgl))
138 
139 /* for the GSC FW we match the compatibility version and not the release one */
140 #define XE_GSC_FIRMWARE_DEFS(fw_def, major_ver)		\
141 	fw_def(LUNARLAKE,	GT_TYPE_ANY,	major_ver(xe,	gsc,	lnl,	104, 1, 0))	\
142 	fw_def(METEORLAKE,	GT_TYPE_ANY,	major_ver(i915,	gsc,	mtl,	102, 1, 0))
143 
144 #define MAKE_FW_PATH(dir__, uc__, shortname__, version__)			\
145 	__stringify(dir__) "/" __stringify(shortname__) "_" __stringify(uc__) version__ ".bin"
146 
147 #define fw_filename_mmp_ver(dir_, uc_, shortname_, a, b, c)			\
148 	MAKE_FW_PATH(dir_, uc_, shortname_, "_" __stringify(a ## . ## b ## . ## c))
149 #define fw_filename_major_ver(dir_, uc_, shortname_, a, b, c)			\
150 	MAKE_FW_PATH(dir_, uc_, shortname_, "_" __stringify(a))
151 #define fw_filename_no_ver(dir_, uc_, shortname_)				\
152 	MAKE_FW_PATH(dir_, uc_, shortname_, "")
153 #define fw_filename_gsc(dir_, uc_, shortname_, a, b, c)				\
154 	MAKE_FW_PATH(dir_, uc_, shortname_, "_" __stringify(b))
155 
156 #define uc_fw_entry_mmp_ver(dir_, uc_, shortname_, a, b, c)			\
157 	{ fw_filename_mmp_ver(dir_, uc_, shortname_, a, b, c),			\
158 	  a, b, c, true }
159 #define uc_fw_entry_major_ver(dir_, uc_, shortname_, a, b, c)			\
160 	{ fw_filename_major_ver(dir_, uc_, shortname_, a, b, c),		\
161 	  a, b, c }
162 #define uc_fw_entry_no_ver(dir_, uc_, shortname_)				\
163 	{ fw_filename_no_ver(dir_, uc_, shortname_),				\
164 	  0, 0 }
165 #define uc_fw_entry_gsc(dir_, uc_, shortname_, a, b, c)				\
166 	{ fw_filename_gsc(dir_, uc_, shortname_, a, b, c),			\
167 	  a, b, c }
168 
169 /* All blobs need to be declared via MODULE_FIRMWARE() */
170 #define XE_UC_MODULE_FIRMWARE(platform__, gt_type__, fw_filename)		\
171 	MODULE_FIRMWARE(fw_filename);
172 
173 #define XE_UC_FW_ENTRY(platform__, gt_type__, entry__)				\
174 	{									\
175 		.platform = XE_ ## platform__,					\
176 		.gt_type = XE_ ## gt_type__,					\
177 		entry__,							\
178 	},
179 
180 XE_GUC_FIRMWARE_DEFS(XE_UC_MODULE_FIRMWARE,
181 		     fw_filename_mmp_ver, fw_filename_major_ver)
182 XE_HUC_FIRMWARE_DEFS(XE_UC_MODULE_FIRMWARE,
183 		     fw_filename_mmp_ver, fw_filename_no_ver)
184 XE_GSC_FIRMWARE_DEFS(XE_UC_MODULE_FIRMWARE, fw_filename_gsc)
185 
186 static struct xe_gt *
187 __uc_fw_to_gt(struct xe_uc_fw *uc_fw, enum xe_uc_fw_type type)
188 {
189 	XE_WARN_ON(type >= XE_UC_FW_NUM_TYPES);
190 
191 	switch (type) {
192 	case XE_UC_FW_TYPE_GUC:
193 		return container_of(uc_fw, struct xe_gt, uc.guc.fw);
194 	case XE_UC_FW_TYPE_HUC:
195 		return container_of(uc_fw, struct xe_gt, uc.huc.fw);
196 	case XE_UC_FW_TYPE_GSC:
197 		return container_of(uc_fw, struct xe_gt, uc.gsc.fw);
198 	default:
199 		return NULL;
200 	}
201 }
202 
203 static struct xe_gt *uc_fw_to_gt(struct xe_uc_fw *uc_fw)
204 {
205 	return __uc_fw_to_gt(uc_fw, uc_fw->type);
206 }
207 
208 static struct xe_device *uc_fw_to_xe(struct xe_uc_fw *uc_fw)
209 {
210 	return gt_to_xe(uc_fw_to_gt(uc_fw));
211 }
212 
213 static void
214 uc_fw_auto_select(struct xe_device *xe, struct xe_uc_fw *uc_fw)
215 {
216 	static const struct uc_fw_entry entries_guc[] = {
217 		XE_GUC_FIRMWARE_DEFS(XE_UC_FW_ENTRY,
218 				     uc_fw_entry_mmp_ver,
219 				     uc_fw_entry_major_ver)
220 	};
221 	static const struct uc_fw_entry entries_huc[] = {
222 		XE_HUC_FIRMWARE_DEFS(XE_UC_FW_ENTRY,
223 				     uc_fw_entry_mmp_ver,
224 				     uc_fw_entry_no_ver)
225 	};
226 	static const struct uc_fw_entry entries_gsc[] = {
227 		XE_GSC_FIRMWARE_DEFS(XE_UC_FW_ENTRY, uc_fw_entry_gsc)
228 	};
229 	static const struct fw_blobs_by_type blobs_all[XE_UC_FW_NUM_TYPES] = {
230 		[XE_UC_FW_TYPE_GUC] = { entries_guc, ARRAY_SIZE(entries_guc) },
231 		[XE_UC_FW_TYPE_HUC] = { entries_huc, ARRAY_SIZE(entries_huc) },
232 		[XE_UC_FW_TYPE_GSC] = { entries_gsc, ARRAY_SIZE(entries_gsc) },
233 	};
234 	struct xe_gt *gt = uc_fw_to_gt(uc_fw);
235 	enum xe_platform p = xe->info.platform;
236 	const struct uc_fw_entry *entries;
237 	u32 count;
238 	int i;
239 
240 	xe_gt_assert(gt, uc_fw->type < ARRAY_SIZE(blobs_all));
241 	xe_gt_assert(gt, gt->info.type != XE_GT_TYPE_UNINITIALIZED);
242 
243 	entries = blobs_all[uc_fw->type].entries;
244 	count = blobs_all[uc_fw->type].count;
245 
246 	for (i = 0; i < count && p <= entries[i].platform; i++) {
247 		if (p != entries[i].platform)
248 			continue;
249 
250 		if (entries[i].gt_type != XE_GT_TYPE_ANY &&
251 		    entries[i].gt_type != gt->info.type)
252 			continue;
253 
254 		uc_fw->path = entries[i].path;
255 		uc_fw->versions.wanted.major = entries[i].major;
256 		uc_fw->versions.wanted.minor = entries[i].minor;
257 		uc_fw->versions.wanted.patch = entries[i].patch;
258 		uc_fw->full_ver_required = entries[i].full_ver_required;
259 
260 		if (uc_fw->type == XE_UC_FW_TYPE_GSC)
261 			uc_fw->versions.wanted_type = XE_UC_FW_VER_COMPATIBILITY;
262 		else
263 			uc_fw->versions.wanted_type = XE_UC_FW_VER_RELEASE;
264 
265 		break;
266 	}
267 }
268 
269 static void
270 uc_fw_override(struct xe_uc_fw *uc_fw)
271 {
272 	char *path_override = NULL;
273 
274 	/* empty string disables, but it's not allowed for GuC */
275 	switch (uc_fw->type) {
276 	case XE_UC_FW_TYPE_GUC:
277 		if (xe_modparam.guc_firmware_path && *xe_modparam.guc_firmware_path)
278 			path_override = xe_modparam.guc_firmware_path;
279 		break;
280 	case XE_UC_FW_TYPE_HUC:
281 		path_override = xe_modparam.huc_firmware_path;
282 		break;
283 	case XE_UC_FW_TYPE_GSC:
284 		path_override = xe_modparam.gsc_firmware_path;
285 		break;
286 	default:
287 		break;
288 	}
289 
290 	if (path_override) {
291 		uc_fw->path = path_override;
292 		uc_fw->user_overridden = true;
293 	}
294 }
295 
296 /**
297  * xe_uc_fw_copy_rsa - copy fw RSA to buffer
298  *
299  * @uc_fw: uC firmware
300  * @dst: dst buffer
301  * @max_len: max number of bytes to copy
302  *
303  * Return: number of copied bytes.
304  */
305 size_t xe_uc_fw_copy_rsa(struct xe_uc_fw *uc_fw, void *dst, u32 max_len)
306 {
307 	struct xe_device *xe = uc_fw_to_xe(uc_fw);
308 	u32 size = min_t(u32, uc_fw->rsa_size, max_len);
309 
310 	xe_assert(xe, !(size % 4));
311 	xe_assert(xe, xe_uc_fw_is_available(uc_fw));
312 
313 	xe_map_memcpy_from(xe, dst, &uc_fw->bo->vmap,
314 			   xe_uc_fw_rsa_offset(uc_fw), size);
315 
316 	return size;
317 }
318 
319 static void uc_fw_fini(struct drm_device *drm, void *arg)
320 {
321 	struct xe_uc_fw *uc_fw = arg;
322 
323 	if (!xe_uc_fw_is_available(uc_fw))
324 		return;
325 
326 	xe_uc_fw_change_status(uc_fw, XE_UC_FIRMWARE_SELECTED);
327 }
328 
329 static int guc_read_css_info(struct xe_uc_fw *uc_fw, struct uc_css_header *css)
330 {
331 	struct xe_gt *gt = uc_fw_to_gt(uc_fw);
332 	struct xe_uc_fw_version *release = &uc_fw->versions.found[XE_UC_FW_VER_RELEASE];
333 	struct xe_uc_fw_version *compatibility = &uc_fw->versions.found[XE_UC_FW_VER_COMPATIBILITY];
334 
335 	xe_gt_assert(gt, uc_fw->type == XE_UC_FW_TYPE_GUC);
336 
337 	/* We don't support GuC releases older than 70.29.2 */
338 	if (MAKE_GUC_VER_STRUCT(*release) < MAKE_GUC_VER(70, 29, 2)) {
339 		xe_gt_err(gt, "Unsupported GuC v%u.%u.%u! v70.29.2 or newer is required\n",
340 			  release->major, release->minor, release->patch);
341 		return -EINVAL;
342 	}
343 
344 	compatibility->major = FIELD_GET(CSS_SW_VERSION_UC_MAJOR, css->submission_version);
345 	compatibility->minor = FIELD_GET(CSS_SW_VERSION_UC_MINOR, css->submission_version);
346 	compatibility->patch = FIELD_GET(CSS_SW_VERSION_UC_PATCH, css->submission_version);
347 
348 	uc_fw->private_data_size = css->private_data_size;
349 
350 	return 0;
351 }
352 
353 int xe_uc_fw_check_version_requirements(struct xe_uc_fw *uc_fw)
354 {
355 	struct xe_device *xe = uc_fw_to_xe(uc_fw);
356 	struct xe_uc_fw_version *wanted = &uc_fw->versions.wanted;
357 	struct xe_uc_fw_version *found = &uc_fw->versions.found[uc_fw->versions.wanted_type];
358 
359 	/* Driver has no requirement on any version, any is good. */
360 	if (!wanted->major)
361 		return 0;
362 
363 	/*
364 	 * If full version is required, both major and minor should match.
365 	 * Otherwise, at least the major version.
366 	 */
367 	if (wanted->major != found->major ||
368 	    (uc_fw->full_ver_required &&
369 	     ((wanted->minor != found->minor) ||
370 	      (wanted->patch != found->patch)))) {
371 		drm_notice(&xe->drm, "%s firmware %s: unexpected version: %u.%u.%u != %u.%u.%u\n",
372 			   xe_uc_fw_type_repr(uc_fw->type), uc_fw->path,
373 			   found->major, found->minor, found->patch,
374 			   wanted->major, wanted->minor, wanted->patch);
375 		goto fail;
376 	}
377 
378 	if (wanted->minor > found->minor ||
379 	    (wanted->minor == found->minor && wanted->patch > found->patch)) {
380 		drm_notice(&xe->drm, "%s firmware (%u.%u.%u) is recommended, but only (%u.%u.%u) was found in %s\n",
381 			   xe_uc_fw_type_repr(uc_fw->type),
382 			   wanted->major, wanted->minor, wanted->patch,
383 			   found->major, found->minor, found->patch,
384 			   uc_fw->path);
385 		drm_info(&xe->drm, "Consider updating your linux-firmware pkg or downloading from %s\n",
386 			 XE_UC_FIRMWARE_URL);
387 	}
388 
389 	return 0;
390 
391 fail:
392 	if (xe_uc_fw_is_overridden(uc_fw))
393 		return 0;
394 
395 	return -ENOEXEC;
396 }
397 
398 /* Refer to the "CSS-based Firmware Layout" documentation entry for details */
399 static int parse_css_header(struct xe_uc_fw *uc_fw, const void *fw_data, size_t fw_size)
400 {
401 	struct xe_device *xe = uc_fw_to_xe(uc_fw);
402 	struct xe_uc_fw_version *release = &uc_fw->versions.found[XE_UC_FW_VER_RELEASE];
403 	struct uc_css_header *css;
404 	size_t size;
405 
406 	/* Check the size of the blob before examining buffer contents */
407 	if (unlikely(fw_size < sizeof(struct uc_css_header))) {
408 		drm_warn(&xe->drm, "%s firmware %s: invalid size: %zu < %zu\n",
409 			 xe_uc_fw_type_repr(uc_fw->type), uc_fw->path,
410 			 fw_size, sizeof(struct uc_css_header));
411 		return -ENODATA;
412 	}
413 
414 	css = (struct uc_css_header *)fw_data;
415 
416 	/* Check integrity of size values inside CSS header */
417 	size = (css->header_size_dw - css->key_size_dw - css->modulus_size_dw -
418 		css->exponent_size_dw) * sizeof(u32);
419 	if (unlikely(size != sizeof(struct uc_css_header))) {
420 		drm_warn(&xe->drm,
421 			 "%s firmware %s: unexpected header size: %zu != %zu\n",
422 			 xe_uc_fw_type_repr(uc_fw->type), uc_fw->path,
423 			 fw_size, sizeof(struct uc_css_header));
424 		return -EPROTO;
425 	}
426 
427 	/* uCode size must calculated from other sizes */
428 	uc_fw->ucode_size = (css->size_dw - css->header_size_dw) * sizeof(u32);
429 
430 	/* now RSA */
431 	uc_fw->rsa_size = css->key_size_dw * sizeof(u32);
432 
433 	/* At least, it should have header, uCode and RSA. Size of all three. */
434 	size = sizeof(struct uc_css_header) + uc_fw->ucode_size +
435 		uc_fw->rsa_size;
436 	if (unlikely(fw_size < size)) {
437 		drm_warn(&xe->drm, "%s firmware %s: invalid size: %zu < %zu\n",
438 			 xe_uc_fw_type_repr(uc_fw->type), uc_fw->path,
439 			 fw_size, size);
440 		return -ENOEXEC;
441 	}
442 
443 	/* Get version numbers from the CSS header */
444 	release->major = FIELD_GET(CSS_SW_VERSION_UC_MAJOR, css->sw_version);
445 	release->minor = FIELD_GET(CSS_SW_VERSION_UC_MINOR, css->sw_version);
446 	release->patch = FIELD_GET(CSS_SW_VERSION_UC_PATCH, css->sw_version);
447 
448 	if (uc_fw->type == XE_UC_FW_TYPE_GUC)
449 		return guc_read_css_info(uc_fw, css);
450 
451 	return 0;
452 }
453 
454 static bool is_cpd_header(const void *data)
455 {
456 	const u32 *marker = data;
457 
458 	return *marker == GSC_CPD_HEADER_MARKER;
459 }
460 
461 static u32 entry_offset(const struct gsc_cpd_header_v2 *header, const char *name)
462 {
463 	const struct gsc_cpd_entry *entry;
464 	int i;
465 
466 	entry = (void *)header + header->header_length;
467 
468 	for (i = 0; i < header->num_of_entries; i++, entry++)
469 		if (strcmp(entry->name, name) == 0)
470 			return entry->offset & GSC_CPD_ENTRY_OFFSET_MASK;
471 
472 	return 0;
473 }
474 
475 /* Refer to the "GSC-based Firmware Layout" documentation entry for details */
476 static int parse_cpd_header(struct xe_uc_fw *uc_fw, const void *data, size_t size,
477 			    const char *manifest_entry, const char *css_entry)
478 {
479 	struct xe_gt *gt = uc_fw_to_gt(uc_fw);
480 	struct xe_device *xe = gt_to_xe(gt);
481 	const struct gsc_cpd_header_v2 *header = data;
482 	struct xe_uc_fw_version *release = &uc_fw->versions.found[XE_UC_FW_VER_RELEASE];
483 	const struct gsc_manifest_header *manifest;
484 	size_t min_size = sizeof(*header);
485 	u32 offset;
486 
487 	/* manifest_entry is mandatory, css_entry is optional */
488 	xe_assert(xe, manifest_entry);
489 
490 	if (size < min_size || !is_cpd_header(header))
491 		return -ENOENT;
492 
493 	if (header->header_length < sizeof(struct gsc_cpd_header_v2)) {
494 		xe_gt_err(gt, "invalid CPD header length %u!\n", header->header_length);
495 		return -EINVAL;
496 	}
497 
498 	min_size = header->header_length + sizeof(struct gsc_cpd_entry) * header->num_of_entries;
499 	if (size < min_size) {
500 		xe_gt_err(gt, "FW too small! %zu < %zu\n", size, min_size);
501 		return -ENODATA;
502 	}
503 
504 	/* Look for the manifest first */
505 	offset = entry_offset(header, manifest_entry);
506 	if (!offset) {
507 		xe_gt_err(gt, "Failed to find %s manifest!\n",
508 			  xe_uc_fw_type_repr(uc_fw->type));
509 		return -ENODATA;
510 	}
511 
512 	min_size = offset + sizeof(struct gsc_manifest_header);
513 	if (size < min_size) {
514 		xe_gt_err(gt, "FW too small! %zu < %zu\n", size, min_size);
515 		return -ENODATA;
516 	}
517 
518 	manifest = data + offset;
519 
520 	release->major = manifest->fw_version.major;
521 	release->minor = manifest->fw_version.minor;
522 	release->patch = manifest->fw_version.hotfix;
523 
524 	if (uc_fw->type == XE_UC_FW_TYPE_GSC) {
525 		struct xe_gsc *gsc = container_of(uc_fw, struct xe_gsc, fw);
526 
527 		release->build = manifest->fw_version.build;
528 		gsc->security_version = manifest->security_version;
529 	}
530 
531 	/* then optionally look for the css header */
532 	if (css_entry) {
533 		int ret;
534 
535 		/*
536 		 * This section does not contain a CSS entry on DG2. We
537 		 * don't support DG2 HuC right now, so no need to handle
538 		 * it, just add a reminder in case that changes.
539 		 */
540 		xe_assert(xe, xe->info.platform != XE_DG2);
541 
542 		offset = entry_offset(header, css_entry);
543 
544 		/* the CSS header parser will check that the CSS header fits */
545 		if (offset > size) {
546 			xe_gt_err(gt, "FW too small! %zu < %u\n", size, offset);
547 			return -ENODATA;
548 		}
549 
550 		ret = parse_css_header(uc_fw, data + offset, size - offset);
551 		if (ret)
552 			return ret;
553 
554 		uc_fw->css_offset = offset;
555 	}
556 
557 	uc_fw->has_gsc_headers = true;
558 
559 	return 0;
560 }
561 
562 static int parse_gsc_layout(struct xe_uc_fw *uc_fw, const void *data, size_t size)
563 {
564 	struct xe_gt *gt = uc_fw_to_gt(uc_fw);
565 	const struct gsc_layout_pointers *layout = data;
566 	const struct gsc_bpdt_header *bpdt_header = NULL;
567 	const struct gsc_bpdt_entry *bpdt_entry = NULL;
568 	size_t min_size = sizeof(*layout);
569 	int i;
570 
571 	if (size < min_size) {
572 		xe_gt_err(gt, "GSC FW too small! %zu < %zu\n", size, min_size);
573 		return -ENODATA;
574 	}
575 
576 	min_size = layout->boot1.offset + layout->boot1.size;
577 	if (size < min_size) {
578 		xe_gt_err(gt, "GSC FW too small for boot section! %zu < %zu\n",
579 			  size, min_size);
580 		return -ENODATA;
581 	}
582 
583 	min_size = sizeof(*bpdt_header);
584 	if (layout->boot1.size < min_size) {
585 		xe_gt_err(gt, "GSC FW boot section too small for BPDT header: %u < %zu\n",
586 			  layout->boot1.size, min_size);
587 		return -ENODATA;
588 	}
589 
590 	bpdt_header = data + layout->boot1.offset;
591 	if (bpdt_header->signature != GSC_BPDT_HEADER_SIGNATURE) {
592 		xe_gt_err(gt, "invalid signature for BPDT header: 0x%08x!\n",
593 			  bpdt_header->signature);
594 		return -EINVAL;
595 	}
596 
597 	min_size += sizeof(*bpdt_entry) * bpdt_header->descriptor_count;
598 	if (layout->boot1.size < min_size) {
599 		xe_gt_err(gt, "GSC FW boot section too small for BPDT entries: %u < %zu\n",
600 			  layout->boot1.size, min_size);
601 		return -ENODATA;
602 	}
603 
604 	bpdt_entry = (void *)bpdt_header + sizeof(*bpdt_header);
605 	for (i = 0; i < bpdt_header->descriptor_count; i++, bpdt_entry++) {
606 		if ((bpdt_entry->type & GSC_BPDT_ENTRY_TYPE_MASK) !=
607 		    GSC_BPDT_ENTRY_TYPE_GSC_RBE)
608 			continue;
609 
610 		min_size = bpdt_entry->sub_partition_offset;
611 
612 		/* the CPD header parser will check that the CPD header fits */
613 		if (layout->boot1.size < min_size) {
614 			xe_gt_err(gt, "GSC FW boot section too small for CPD offset: %u < %zu\n",
615 				  layout->boot1.size, min_size);
616 			return -ENODATA;
617 		}
618 
619 		return parse_cpd_header(uc_fw,
620 					(void *)bpdt_header + min_size,
621 					layout->boot1.size - min_size,
622 					"RBEP.man", NULL);
623 	}
624 
625 	xe_gt_err(gt, "couldn't find CPD header in GSC binary!\n");
626 	return -ENODATA;
627 }
628 
629 static int parse_headers(struct xe_uc_fw *uc_fw, const struct firmware *fw)
630 {
631 	int ret;
632 
633 	/*
634 	 * All GuC releases and older HuC ones use CSS headers, while newer HuC
635 	 * releases use GSC CPD headers.
636 	 */
637 	switch (uc_fw->type) {
638 	case XE_UC_FW_TYPE_GSC:
639 		return parse_gsc_layout(uc_fw, fw->data, fw->size);
640 	case XE_UC_FW_TYPE_HUC:
641 		ret = parse_cpd_header(uc_fw, fw->data, fw->size, "HUCP.man", "huc_fw");
642 		if (!ret || ret != -ENOENT)
643 			return ret;
644 		fallthrough;
645 	case XE_UC_FW_TYPE_GUC:
646 		return parse_css_header(uc_fw, fw->data, fw->size);
647 	default:
648 		return -EINVAL;
649 	}
650 
651 	return 0;
652 }
653 
654 #define print_uc_fw_version(p_, version_, prefix_, ...) \
655 do { \
656 	struct xe_uc_fw_version *ver_ = (version_); \
657 	if (ver_->build) \
658 		drm_printf(p_, prefix_ " version %u.%u.%u.%u\n", ##__VA_ARGS__, \
659 			   ver_->major, ver_->minor, \
660 			   ver_->patch, ver_->build); \
661 	else \
662 		drm_printf(p_, prefix_ " version %u.%u.%u\n", ##__VA_ARGS__, \
663 			  ver_->major, ver_->minor, ver_->patch); \
664 } while (0)
665 
666 static void uc_fw_vf_override(struct xe_uc_fw *uc_fw)
667 {
668 	struct xe_uc_fw_version *compat = &uc_fw->versions.found[XE_UC_FW_VER_COMPATIBILITY];
669 	struct xe_uc_fw_version *wanted = &uc_fw->versions.wanted;
670 
671 	/* Only GuC/HuC are supported */
672 	if (uc_fw->type != XE_UC_FW_TYPE_GUC && uc_fw->type != XE_UC_FW_TYPE_HUC)
673 		uc_fw->path = NULL;
674 
675 	/* VF will support only firmwares that driver can autoselect */
676 	xe_uc_fw_change_status(uc_fw, uc_fw->path ?
677 			       XE_UC_FIRMWARE_PRELOADED :
678 			       XE_UC_FIRMWARE_NOT_SUPPORTED);
679 
680 	if (!xe_uc_fw_is_supported(uc_fw))
681 		return;
682 
683 	/* PF is doing the loading, so we don't need a path on the VF */
684 	uc_fw->path = "Loaded by PF";
685 
686 	/* The GuC versions are set up during the VF bootstrap */
687 	if (uc_fw->type == XE_UC_FW_TYPE_GUC) {
688 		uc_fw->versions.wanted_type = XE_UC_FW_VER_COMPATIBILITY;
689 		xe_gt_sriov_vf_guc_versions(uc_fw_to_gt(uc_fw), wanted, compat);
690 	}
691 }
692 
693 static int uc_fw_request(struct xe_uc_fw *uc_fw, const struct firmware **firmware_p)
694 {
695 	struct xe_device *xe = uc_fw_to_xe(uc_fw);
696 	struct xe_gt *gt = uc_fw_to_gt(uc_fw);
697 	struct drm_printer p = xe_gt_info_printer(gt);
698 	struct device *dev = xe->drm.dev;
699 	const struct firmware *fw = NULL;
700 	int err;
701 
702 	/*
703 	 * we use FIRMWARE_UNINITIALIZED to detect checks against uc_fw->status
704 	 * before we're looked at the HW caps to see if we have uc support
705 	 */
706 	BUILD_BUG_ON(XE_UC_FIRMWARE_UNINITIALIZED);
707 	xe_gt_assert(gt, !uc_fw->status);
708 	xe_gt_assert(gt, !uc_fw->path);
709 
710 	uc_fw_auto_select(xe, uc_fw);
711 
712 	if (IS_SRIOV_VF(xe)) {
713 		uc_fw_vf_override(uc_fw);
714 		return 0;
715 	}
716 
717 	uc_fw_override(uc_fw);
718 
719 	xe_uc_fw_change_status(uc_fw, uc_fw->path ?
720 			       XE_UC_FIRMWARE_SELECTED :
721 			       XE_UC_FIRMWARE_NOT_SUPPORTED);
722 
723 	if (!xe_uc_fw_is_supported(uc_fw)) {
724 		if (uc_fw->type == XE_UC_FW_TYPE_GUC) {
725 			xe_gt_err(gt, "No GuC firmware defined for platform\n");
726 			return -ENOENT;
727 		}
728 		return 0;
729 	}
730 
731 	/* an empty path means the firmware is disabled */
732 	if (!xe_device_uc_enabled(xe) || !(*uc_fw->path)) {
733 		xe_uc_fw_change_status(uc_fw, XE_UC_FIRMWARE_DISABLED);
734 		xe_gt_dbg(gt, "%s disabled\n", xe_uc_fw_type_repr(uc_fw->type));
735 		return 0;
736 	}
737 
738 	err = request_firmware(&fw, uc_fw->path, dev);
739 	if (err)
740 		goto fail;
741 
742 	err = parse_headers(uc_fw, fw);
743 	if (err)
744 		goto fail;
745 
746 	print_uc_fw_version(&p,
747 			    &uc_fw->versions.found[XE_UC_FW_VER_RELEASE],
748 			    "Using %s firmware from %s",
749 			    xe_uc_fw_type_repr(uc_fw->type), uc_fw->path);
750 
751 	/* for GSC FW we want the compatibility version, which we query after load */
752 	if (uc_fw->type != XE_UC_FW_TYPE_GSC) {
753 		err = xe_uc_fw_check_version_requirements(uc_fw);
754 		if (err)
755 			goto fail;
756 	}
757 
758 	*firmware_p = fw;
759 
760 	return 0;
761 
762 fail:
763 	xe_uc_fw_change_status(uc_fw, err == -ENOENT ?
764 			       XE_UC_FIRMWARE_MISSING :
765 			       XE_UC_FIRMWARE_ERROR);
766 
767 	xe_gt_notice(gt, "%s firmware %s: fetch failed with error %pe\n",
768 		     xe_uc_fw_type_repr(uc_fw->type), uc_fw->path, ERR_PTR(err));
769 	xe_gt_info(gt, "%s firmware(s) can be downloaded from %s\n",
770 		   xe_uc_fw_type_repr(uc_fw->type), XE_UC_FIRMWARE_URL);
771 
772 	release_firmware(fw);		/* OK even if fw is NULL */
773 
774 	return err;
775 }
776 
777 static void uc_fw_release(const struct firmware *fw)
778 {
779 	release_firmware(fw);
780 }
781 
782 static int uc_fw_copy(struct xe_uc_fw *uc_fw, const void *data, size_t size, u32 flags)
783 {
784 	struct xe_device *xe = uc_fw_to_xe(uc_fw);
785 	struct xe_gt *gt = uc_fw_to_gt(uc_fw);
786 	struct xe_tile *tile = gt_to_tile(gt);
787 	struct xe_bo *obj;
788 	int err;
789 
790 	obj = xe_managed_bo_create_from_data(xe, tile, data, size, flags);
791 	if (IS_ERR(obj)) {
792 		drm_notice(&xe->drm, "%s firmware %s: failed to create / populate bo",
793 			   xe_uc_fw_type_repr(uc_fw->type), uc_fw->path);
794 		err = PTR_ERR(obj);
795 		goto fail;
796 	}
797 
798 	uc_fw->bo = obj;
799 	uc_fw->size = size;
800 
801 	xe_uc_fw_change_status(uc_fw, XE_UC_FIRMWARE_AVAILABLE);
802 
803 	err = drmm_add_action_or_reset(&xe->drm, uc_fw_fini, uc_fw);
804 	if (err)
805 		goto fail;
806 
807 	return 0;
808 
809 fail:
810 	xe_uc_fw_change_status(uc_fw, XE_UC_FIRMWARE_ERROR);
811 	drm_notice(&xe->drm, "%s firmware %s: copy failed with error %d\n",
812 		   xe_uc_fw_type_repr(uc_fw->type), uc_fw->path, err);
813 
814 	return err;
815 }
816 
817 int xe_uc_fw_init(struct xe_uc_fw *uc_fw)
818 {
819 	const struct firmware *fw = NULL;
820 	int err;
821 
822 	err = uc_fw_request(uc_fw, &fw);
823 	if (err)
824 		return err;
825 
826 	/* no error and no firmware means nothing to copy */
827 	if (!fw)
828 		return 0;
829 
830 	err = uc_fw_copy(uc_fw, fw->data, fw->size,
831 			 XE_BO_FLAG_SYSTEM | XE_BO_FLAG_GGTT |
832 			 XE_BO_FLAG_GGTT_INVALIDATE);
833 
834 	uc_fw_release(fw);
835 
836 	return err;
837 }
838 ALLOW_ERROR_INJECTION(xe_uc_fw_init, ERRNO); /* See xe_pci_probe() */
839 
840 static u32 uc_fw_ggtt_offset(struct xe_uc_fw *uc_fw)
841 {
842 	return xe_bo_ggtt_addr(uc_fw->bo);
843 }
844 
845 static int uc_fw_xfer(struct xe_uc_fw *uc_fw, u32 offset, u32 dma_flags)
846 {
847 	struct xe_device *xe = uc_fw_to_xe(uc_fw);
848 	struct xe_gt *gt = uc_fw_to_gt(uc_fw);
849 	struct xe_mmio *mmio = &gt->mmio;
850 	u64 src_offset;
851 	u32 dma_ctrl;
852 	int ret;
853 
854 	xe_force_wake_assert_held(gt_to_fw(gt), XE_FW_GT);
855 
856 	/* Set the source address for the uCode */
857 	src_offset = uc_fw_ggtt_offset(uc_fw) + uc_fw->css_offset;
858 	xe_mmio_write32(mmio, DMA_ADDR_0_LOW, lower_32_bits(src_offset));
859 	xe_mmio_write32(mmio, DMA_ADDR_0_HIGH,
860 			upper_32_bits(src_offset) | DMA_ADDRESS_SPACE_GGTT);
861 
862 	/* Set the DMA destination */
863 	xe_mmio_write32(mmio, DMA_ADDR_1_LOW, offset);
864 	xe_mmio_write32(mmio, DMA_ADDR_1_HIGH, DMA_ADDRESS_SPACE_WOPCM);
865 
866 	/*
867 	 * Set the transfer size. The header plus uCode will be copied to WOPCM
868 	 * via DMA, excluding any other components
869 	 */
870 	xe_mmio_write32(mmio, DMA_COPY_SIZE,
871 			sizeof(struct uc_css_header) + uc_fw->ucode_size);
872 
873 	/* Start the DMA */
874 	xe_mmio_write32(mmio, DMA_CTRL,
875 			_MASKED_BIT_ENABLE(dma_flags | START_DMA));
876 
877 	/* Wait for DMA to finish */
878 	ret = xe_mmio_wait32(mmio, DMA_CTRL, START_DMA, 0, 100000, &dma_ctrl,
879 			     false);
880 	if (ret)
881 		drm_err(&xe->drm, "DMA for %s fw failed, DMA_CTRL=%u\n",
882 			xe_uc_fw_type_repr(uc_fw->type), dma_ctrl);
883 
884 	/* Disable the bits once DMA is over */
885 	xe_mmio_write32(mmio, DMA_CTRL, _MASKED_BIT_DISABLE(dma_flags));
886 
887 	return ret;
888 }
889 
890 int xe_uc_fw_upload(struct xe_uc_fw *uc_fw, u32 offset, u32 dma_flags)
891 {
892 	struct xe_device *xe = uc_fw_to_xe(uc_fw);
893 	int err;
894 
895 	/* make sure the status was cleared the last time we reset the uc */
896 	xe_assert(xe, !xe_uc_fw_is_loaded(uc_fw));
897 
898 	if (!xe_uc_fw_is_loadable(uc_fw))
899 		return -ENOEXEC;
900 
901 	/* Call custom loader */
902 	err = uc_fw_xfer(uc_fw, offset, dma_flags);
903 	if (err)
904 		goto fail;
905 
906 	xe_uc_fw_change_status(uc_fw, XE_UC_FIRMWARE_TRANSFERRED);
907 	return 0;
908 
909 fail:
910 	drm_err(&xe->drm, "Failed to load %s firmware %s (%d)\n",
911 		xe_uc_fw_type_repr(uc_fw->type), uc_fw->path,
912 		err);
913 	xe_uc_fw_change_status(uc_fw, XE_UC_FIRMWARE_LOAD_FAIL);
914 	return err;
915 }
916 
917 static const char *version_type_repr(enum xe_uc_fw_version_types type)
918 {
919 	switch (type) {
920 	case XE_UC_FW_VER_RELEASE:
921 		return "release";
922 	case XE_UC_FW_VER_COMPATIBILITY:
923 		return "compatibility";
924 	default:
925 		return "Unknown version type";
926 	}
927 }
928 
929 void xe_uc_fw_print(struct xe_uc_fw *uc_fw, struct drm_printer *p)
930 {
931 	int i;
932 
933 	drm_printf(p, "%s firmware: %s\n",
934 		   xe_uc_fw_type_repr(uc_fw->type), uc_fw->path);
935 	drm_printf(p, "\tstatus: %s\n",
936 		   xe_uc_fw_status_repr(uc_fw->status));
937 
938 	print_uc_fw_version(p, &uc_fw->versions.wanted, "\twanted %s",
939 			    version_type_repr(uc_fw->versions.wanted_type));
940 
941 	for (i = 0; i < XE_UC_FW_VER_TYPE_COUNT; i++) {
942 		struct xe_uc_fw_version *ver = &uc_fw->versions.found[i];
943 
944 		if (ver->major)
945 			print_uc_fw_version(p, ver, "\tfound %s",
946 					    version_type_repr(i));
947 	}
948 
949 	if (uc_fw->ucode_size)
950 		drm_printf(p, "\tuCode: %u bytes\n", uc_fw->ucode_size);
951 	if (uc_fw->rsa_size)
952 		drm_printf(p, "\tRSA: %u bytes\n", uc_fw->rsa_size);
953 }
954