xref: /openbmc/linux/drivers/gpu/drm/i915/gt/uc/intel_uc_fw.c (revision 248ed9e227e6cf59acb1aaf3aa30d530a0232c1a)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2016-2019 Intel Corporation
4  */
5 
6 #include <linux/bitfield.h>
7 #include <linux/firmware.h>
8 #include <linux/highmem.h>
9 
10 #include <drm/drm_cache.h>
11 #include <drm/drm_print.h>
12 
13 #include "gem/i915_gem_lmem.h"
14 #include "gt/intel_gt_print.h"
15 #include "intel_uc_fw.h"
16 #include "intel_uc_fw_abi.h"
17 #include "i915_drv.h"
18 #include "i915_reg.h"
19 
20 static inline struct intel_gt *
21 ____uc_fw_to_gt(struct intel_uc_fw *uc_fw, enum intel_uc_fw_type type)
22 {
23 	GEM_BUG_ON(type >= INTEL_UC_FW_NUM_TYPES);
24 
25 	switch (type) {
26 	case INTEL_UC_FW_TYPE_GUC:
27 		return container_of(uc_fw, struct intel_gt, uc.guc.fw);
28 	case INTEL_UC_FW_TYPE_HUC:
29 		return container_of(uc_fw, struct intel_gt, uc.huc.fw);
30 	case INTEL_UC_FW_TYPE_GSC:
31 		return container_of(uc_fw, struct intel_gt, uc.gsc.fw);
32 	}
33 
34 	return NULL;
35 }
36 
37 static inline struct intel_gt *__uc_fw_to_gt(struct intel_uc_fw *uc_fw)
38 {
39 	GEM_BUG_ON(uc_fw->status == INTEL_UC_FIRMWARE_UNINITIALIZED);
40 	return ____uc_fw_to_gt(uc_fw, uc_fw->type);
41 }
42 
43 #ifdef CONFIG_DRM_I915_DEBUG_GUC
44 void intel_uc_fw_change_status(struct intel_uc_fw *uc_fw,
45 			       enum intel_uc_fw_status status)
46 {
47 	uc_fw->__status =  status;
48 	gt_dbg(__uc_fw_to_gt(uc_fw), "%s firmware -> %s\n",
49 	       intel_uc_fw_type_repr(uc_fw->type),
50 	       status == INTEL_UC_FIRMWARE_SELECTED ?
51 	       uc_fw->file_selected.path : intel_uc_fw_status_repr(status));
52 }
53 #endif
54 
55 /*
56  * List of required GuC and HuC binaries per-platform.
57  * Must be ordered based on platform + revid, from newer to older.
58  *
59  * Note that RKL and ADL-S have the same GuC/HuC device ID's and use the same
60  * firmware as TGL.
61  *
62  * Version numbers:
63  * Originally, the driver required an exact match major/minor/patch furmware
64  * file and only supported that one version for any given platform. However,
65  * the new direction from upstream is to be backwards compatible with all
66  * prior releases and to be as flexible as possible as to what firmware is
67  * loaded.
68  *
69  * For GuC, the major version number signifies a backwards breaking API change.
70  * So, new format GuC firmware files are labelled by their major version only.
71  * For HuC, there is no KMD interaction, hence no version matching requirement.
72  * So, new format HuC firmware files have no version number at all.
73  *
74  * All of which means that the table below must keep all old format files with
75  * full three point version number. But newer files have reduced requirements.
76  * Having said that, the driver still needs to track the minor version number
77  * for GuC at least. As it is useful to report to the user that they are not
78  * running with a recent enough version for all KMD supported features,
79  * security fixes, etc. to be enabled.
80  */
81 #define INTEL_GUC_FIRMWARE_DEFS(fw_def, guc_maj, guc_mmp) \
82 	fw_def(DG2,          0, guc_maj(dg2,  70, 5)) \
83 	fw_def(ALDERLAKE_P,  0, guc_maj(adlp, 70, 5)) \
84 	fw_def(ALDERLAKE_P,  0, guc_mmp(adlp, 70, 1, 1)) \
85 	fw_def(ALDERLAKE_P,  0, guc_mmp(adlp, 69, 0, 3)) \
86 	fw_def(ALDERLAKE_S,  0, guc_maj(tgl,  70, 5)) \
87 	fw_def(ALDERLAKE_S,  0, guc_mmp(tgl,  70, 1, 1)) \
88 	fw_def(ALDERLAKE_S,  0, guc_mmp(tgl,  69, 0, 3)) \
89 	fw_def(DG1,          0, guc_maj(dg1,  70, 5)) \
90 	fw_def(ROCKETLAKE,   0, guc_mmp(tgl,  70, 1, 1)) \
91 	fw_def(TIGERLAKE,    0, guc_mmp(tgl,  70, 1, 1)) \
92 	fw_def(JASPERLAKE,   0, guc_mmp(ehl,  70, 1, 1)) \
93 	fw_def(ELKHARTLAKE,  0, guc_mmp(ehl,  70, 1, 1)) \
94 	fw_def(ICELAKE,      0, guc_mmp(icl,  70, 1, 1)) \
95 	fw_def(COMETLAKE,    5, guc_mmp(cml,  70, 1, 1)) \
96 	fw_def(COMETLAKE,    0, guc_mmp(kbl,  70, 1, 1)) \
97 	fw_def(COFFEELAKE,   0, guc_mmp(kbl,  70, 1, 1)) \
98 	fw_def(GEMINILAKE,   0, guc_mmp(glk,  70, 1, 1)) \
99 	fw_def(KABYLAKE,     0, guc_mmp(kbl,  70, 1, 1)) \
100 	fw_def(BROXTON,      0, guc_mmp(bxt,  70, 1, 1)) \
101 	fw_def(SKYLAKE,      0, guc_mmp(skl,  70, 1, 1))
102 
103 #define INTEL_HUC_FIRMWARE_DEFS(fw_def, huc_raw, huc_mmp, huc_gsc) \
104 	fw_def(DG2,          0, huc_gsc(dg2)) \
105 	fw_def(ALDERLAKE_P,  0, huc_raw(tgl)) \
106 	fw_def(ALDERLAKE_P,  0, huc_mmp(tgl,  7, 9, 3)) \
107 	fw_def(ALDERLAKE_S,  0, huc_raw(tgl)) \
108 	fw_def(ALDERLAKE_S,  0, huc_mmp(tgl,  7, 9, 3)) \
109 	fw_def(DG1,          0, huc_raw(dg1)) \
110 	fw_def(ROCKETLAKE,   0, huc_mmp(tgl,  7, 9, 3)) \
111 	fw_def(TIGERLAKE,    0, huc_mmp(tgl,  7, 9, 3)) \
112 	fw_def(JASPERLAKE,   0, huc_mmp(ehl,  9, 0, 0)) \
113 	fw_def(ELKHARTLAKE,  0, huc_mmp(ehl,  9, 0, 0)) \
114 	fw_def(ICELAKE,      0, huc_mmp(icl,  9, 0, 0)) \
115 	fw_def(COMETLAKE,    5, huc_mmp(cml,  4, 0, 0)) \
116 	fw_def(COMETLAKE,    0, huc_mmp(kbl,  4, 0, 0)) \
117 	fw_def(COFFEELAKE,   0, huc_mmp(kbl,  4, 0, 0)) \
118 	fw_def(GEMINILAKE,   0, huc_mmp(glk,  4, 0, 0)) \
119 	fw_def(KABYLAKE,     0, huc_mmp(kbl,  4, 0, 0)) \
120 	fw_def(BROXTON,      0, huc_mmp(bxt,  2, 0, 0)) \
121 	fw_def(SKYLAKE,      0, huc_mmp(skl,  2, 0, 0))
122 
123 /*
124  * Set of macros for producing a list of filenames from the above table.
125  */
126 #define __MAKE_UC_FW_PATH_BLANK(prefix_, name_) \
127 	"i915/" \
128 	__stringify(prefix_) "_" name_ ".bin"
129 
130 #define __MAKE_UC_FW_PATH_MAJOR(prefix_, name_, major_) \
131 	"i915/" \
132 	__stringify(prefix_) "_" name_ "_" \
133 	__stringify(major_) ".bin"
134 
135 #define __MAKE_UC_FW_PATH_MMP(prefix_, name_, major_, minor_, patch_) \
136 	"i915/" \
137 	__stringify(prefix_) "_" name_  "_" \
138 	__stringify(major_) "." \
139 	__stringify(minor_) "." \
140 	__stringify(patch_) ".bin"
141 
142 /* Minor for internal driver use, not part of file name */
143 #define MAKE_GUC_FW_PATH_MAJOR(prefix_, major_, minor_) \
144 	__MAKE_UC_FW_PATH_MAJOR(prefix_, "guc", major_)
145 
146 #define MAKE_GUC_FW_PATH_MMP(prefix_, major_, minor_, patch_) \
147 	__MAKE_UC_FW_PATH_MMP(prefix_, "guc", major_, minor_, patch_)
148 
149 #define MAKE_HUC_FW_PATH_BLANK(prefix_) \
150 	__MAKE_UC_FW_PATH_BLANK(prefix_, "huc")
151 
152 #define MAKE_HUC_FW_PATH_GSC(prefix_) \
153 	__MAKE_UC_FW_PATH_BLANK(prefix_, "huc_gsc")
154 
155 #define MAKE_HUC_FW_PATH_MMP(prefix_, major_, minor_, patch_) \
156 	__MAKE_UC_FW_PATH_MMP(prefix_, "huc", major_, minor_, patch_)
157 
158 /*
159  * All blobs need to be declared via MODULE_FIRMWARE().
160  * This first expansion of the table macros is solely to provide
161  * that declaration.
162  */
163 #define INTEL_UC_MODULE_FW(platform_, revid_, uc_) \
164 	MODULE_FIRMWARE(uc_);
165 
166 INTEL_GUC_FIRMWARE_DEFS(INTEL_UC_MODULE_FW, MAKE_GUC_FW_PATH_MAJOR, MAKE_GUC_FW_PATH_MMP)
167 INTEL_HUC_FIRMWARE_DEFS(INTEL_UC_MODULE_FW, MAKE_HUC_FW_PATH_BLANK, MAKE_HUC_FW_PATH_MMP, MAKE_HUC_FW_PATH_GSC)
168 
169 /*
170  * The next expansion of the table macros (in __uc_fw_auto_select below) provides
171  * actual data structures with both the filename and the version information.
172  * These structure arrays are then iterated over to the list of suitable files
173  * for the current platform and to then attempt to load those files, in the order
174  * listed, until one is successfully found.
175  */
176 struct __packed uc_fw_blob {
177 	const char *path;
178 	bool legacy;
179 	u8 major;
180 	u8 minor;
181 	u8 patch;
182 	bool loaded_via_gsc;
183 };
184 
185 #define UC_FW_BLOB_BASE(major_, minor_, patch_, path_) \
186 	.major = major_, \
187 	.minor = minor_, \
188 	.patch = patch_, \
189 	.path = path_,
190 
191 #define UC_FW_BLOB_NEW(major_, minor_, patch_, gsc_, path_) \
192 	{ UC_FW_BLOB_BASE(major_, minor_, patch_, path_) \
193 	  .legacy = false, .loaded_via_gsc = gsc_ }
194 
195 #define UC_FW_BLOB_OLD(major_, minor_, patch_, path_) \
196 	{ UC_FW_BLOB_BASE(major_, minor_, patch_, path_) \
197 	  .legacy = true }
198 
199 #define GUC_FW_BLOB(prefix_, major_, minor_) \
200 	UC_FW_BLOB_NEW(major_, minor_, 0, false, \
201 		       MAKE_GUC_FW_PATH_MAJOR(prefix_, major_, minor_))
202 
203 #define GUC_FW_BLOB_MMP(prefix_, major_, minor_, patch_) \
204 	UC_FW_BLOB_OLD(major_, minor_, patch_, \
205 		       MAKE_GUC_FW_PATH_MMP(prefix_, major_, minor_, patch_))
206 
207 #define HUC_FW_BLOB(prefix_) \
208 	UC_FW_BLOB_NEW(0, 0, 0, false, MAKE_HUC_FW_PATH_BLANK(prefix_))
209 
210 #define HUC_FW_BLOB_MMP(prefix_, major_, minor_, patch_) \
211 	UC_FW_BLOB_OLD(major_, minor_, patch_, \
212 		       MAKE_HUC_FW_PATH_MMP(prefix_, major_, minor_, patch_))
213 
214 #define HUC_FW_BLOB_GSC(prefix_) \
215 	UC_FW_BLOB_NEW(0, 0, 0, true, MAKE_HUC_FW_PATH_GSC(prefix_))
216 
217 struct __packed uc_fw_platform_requirement {
218 	enum intel_platform p;
219 	u8 rev; /* first platform rev using this FW */
220 	const struct uc_fw_blob blob;
221 };
222 
223 #define MAKE_FW_LIST(platform_, revid_, uc_) \
224 { \
225 	.p = INTEL_##platform_, \
226 	.rev = revid_, \
227 	.blob = uc_, \
228 },
229 
230 struct fw_blobs_by_type {
231 	const struct uc_fw_platform_requirement *blobs;
232 	u32 count;
233 };
234 
235 static void
236 __uc_fw_auto_select(struct drm_i915_private *i915, struct intel_uc_fw *uc_fw)
237 {
238 	static const struct uc_fw_platform_requirement blobs_guc[] = {
239 		INTEL_GUC_FIRMWARE_DEFS(MAKE_FW_LIST, GUC_FW_BLOB, GUC_FW_BLOB_MMP)
240 	};
241 	static const struct uc_fw_platform_requirement blobs_huc[] = {
242 		INTEL_HUC_FIRMWARE_DEFS(MAKE_FW_LIST, HUC_FW_BLOB, HUC_FW_BLOB_MMP, HUC_FW_BLOB_GSC)
243 	};
244 	static const struct fw_blobs_by_type blobs_all[INTEL_UC_FW_NUM_TYPES] = {
245 		[INTEL_UC_FW_TYPE_GUC] = { blobs_guc, ARRAY_SIZE(blobs_guc) },
246 		[INTEL_UC_FW_TYPE_HUC] = { blobs_huc, ARRAY_SIZE(blobs_huc) },
247 	};
248 	static bool verified[INTEL_UC_FW_NUM_TYPES];
249 	const struct uc_fw_platform_requirement *fw_blobs;
250 	enum intel_platform p = INTEL_INFO(i915)->platform;
251 	u32 fw_count;
252 	u8 rev = INTEL_REVID(i915);
253 	int i;
254 	bool found;
255 
256 	/*
257 	 * GSC FW support is still not fully in place, so we're not defining
258 	 * the FW blob yet because we don't want the driver to attempt to load
259 	 * it until we're ready for it.
260 	 */
261 	if (uc_fw->type == INTEL_UC_FW_TYPE_GSC)
262 		return;
263 
264 	/*
265 	 * The only difference between the ADL GuC FWs is the HWConfig support.
266 	 * ADL-N does not support HWConfig, so we should use the same binary as
267 	 * ADL-S, otherwise the GuC might attempt to fetch a config table that
268 	 * does not exist.
269 	 */
270 	if (IS_ADLP_N(i915))
271 		p = INTEL_ALDERLAKE_S;
272 
273 	GEM_BUG_ON(uc_fw->type >= ARRAY_SIZE(blobs_all));
274 	fw_blobs = blobs_all[uc_fw->type].blobs;
275 	fw_count = blobs_all[uc_fw->type].count;
276 
277 	found = false;
278 	for (i = 0; i < fw_count && p <= fw_blobs[i].p; i++) {
279 		const struct uc_fw_blob *blob = &fw_blobs[i].blob;
280 
281 		if (p != fw_blobs[i].p)
282 			continue;
283 
284 		if (rev < fw_blobs[i].rev)
285 			continue;
286 
287 		if (uc_fw->file_selected.path) {
288 			if (uc_fw->file_selected.path == blob->path)
289 				uc_fw->file_selected.path = NULL;
290 
291 			continue;
292 		}
293 
294 		uc_fw->file_selected.path = blob->path;
295 		uc_fw->file_wanted.path = blob->path;
296 		uc_fw->file_wanted.ver.major = blob->major;
297 		uc_fw->file_wanted.ver.minor = blob->minor;
298 		uc_fw->loaded_via_gsc = blob->loaded_via_gsc;
299 		found = true;
300 		break;
301 	}
302 
303 	if (!found && uc_fw->file_selected.path) {
304 		/* Failed to find a match for the last attempt?! */
305 		uc_fw->file_selected.path = NULL;
306 	}
307 
308 	/* make sure the list is ordered as expected */
309 	if (IS_ENABLED(CONFIG_DRM_I915_SELFTEST) && !verified[uc_fw->type]) {
310 		verified[uc_fw->type] = true;
311 
312 		for (i = 1; i < fw_count; i++) {
313 			/* Next platform is good: */
314 			if (fw_blobs[i].p < fw_blobs[i - 1].p)
315 				continue;
316 
317 			/* Next platform revision is good: */
318 			if (fw_blobs[i].p == fw_blobs[i - 1].p &&
319 			    fw_blobs[i].rev < fw_blobs[i - 1].rev)
320 				continue;
321 
322 			/* Platform/revision must be in order: */
323 			if (fw_blobs[i].p != fw_blobs[i - 1].p ||
324 			    fw_blobs[i].rev != fw_blobs[i - 1].rev)
325 				goto bad;
326 
327 			/* Next major version is good: */
328 			if (fw_blobs[i].blob.major < fw_blobs[i - 1].blob.major)
329 				continue;
330 
331 			/* New must be before legacy: */
332 			if (!fw_blobs[i].blob.legacy && fw_blobs[i - 1].blob.legacy)
333 				goto bad;
334 
335 			/* New to legacy also means 0.0 to X.Y (HuC), or X.0 to X.Y (GuC) */
336 			if (fw_blobs[i].blob.legacy && !fw_blobs[i - 1].blob.legacy) {
337 				if (!fw_blobs[i - 1].blob.major)
338 					continue;
339 
340 				if (fw_blobs[i].blob.major == fw_blobs[i - 1].blob.major)
341 					continue;
342 			}
343 
344 			/* Major versions must be in order: */
345 			if (fw_blobs[i].blob.major != fw_blobs[i - 1].blob.major)
346 				goto bad;
347 
348 			/* Next minor version is good: */
349 			if (fw_blobs[i].blob.minor < fw_blobs[i - 1].blob.minor)
350 				continue;
351 
352 			/* Minor versions must be in order: */
353 			if (fw_blobs[i].blob.minor != fw_blobs[i - 1].blob.minor)
354 				goto bad;
355 
356 			/* Patch versions must be in order: */
357 			if (fw_blobs[i].blob.patch <= fw_blobs[i - 1].blob.patch)
358 				continue;
359 
360 bad:
361 			drm_err(&i915->drm, "Invalid %s blob order: %s r%u %s%d.%d.%d comes before %s r%u %s%d.%d.%d\n",
362 				intel_uc_fw_type_repr(uc_fw->type),
363 				intel_platform_name(fw_blobs[i - 1].p), fw_blobs[i - 1].rev,
364 				fw_blobs[i - 1].blob.legacy ? "L" : "v",
365 				fw_blobs[i - 1].blob.major,
366 				fw_blobs[i - 1].blob.minor,
367 				fw_blobs[i - 1].blob.patch,
368 				intel_platform_name(fw_blobs[i].p), fw_blobs[i].rev,
369 				fw_blobs[i].blob.legacy ? "L" : "v",
370 				fw_blobs[i].blob.major,
371 				fw_blobs[i].blob.minor,
372 				fw_blobs[i].blob.patch);
373 
374 			uc_fw->file_selected.path = NULL;
375 		}
376 	}
377 }
378 
379 static const char *__override_guc_firmware_path(struct drm_i915_private *i915)
380 {
381 	if (i915->params.enable_guc & ENABLE_GUC_MASK)
382 		return i915->params.guc_firmware_path;
383 	return "";
384 }
385 
386 static const char *__override_huc_firmware_path(struct drm_i915_private *i915)
387 {
388 	if (i915->params.enable_guc & ENABLE_GUC_LOAD_HUC)
389 		return i915->params.huc_firmware_path;
390 	return "";
391 }
392 
393 static const char *__override_gsc_firmware_path(struct drm_i915_private *i915)
394 {
395 	return i915->params.gsc_firmware_path;
396 }
397 
398 static void __uc_fw_user_override(struct drm_i915_private *i915, struct intel_uc_fw *uc_fw)
399 {
400 	const char *path = NULL;
401 
402 	switch (uc_fw->type) {
403 	case INTEL_UC_FW_TYPE_GUC:
404 		path = __override_guc_firmware_path(i915);
405 		break;
406 	case INTEL_UC_FW_TYPE_HUC:
407 		path = __override_huc_firmware_path(i915);
408 		break;
409 	case INTEL_UC_FW_TYPE_GSC:
410 		path = __override_gsc_firmware_path(i915);
411 		break;
412 	}
413 
414 	if (unlikely(path)) {
415 		uc_fw->file_selected.path = path;
416 		uc_fw->user_overridden = true;
417 	}
418 }
419 
420 /**
421  * intel_uc_fw_init_early - initialize the uC object and select the firmware
422  * @uc_fw: uC firmware
423  * @type: type of uC
424  *
425  * Initialize the state of our uC object and relevant tracking and select the
426  * firmware to fetch and load.
427  */
428 void intel_uc_fw_init_early(struct intel_uc_fw *uc_fw,
429 			    enum intel_uc_fw_type type)
430 {
431 	struct drm_i915_private *i915 = ____uc_fw_to_gt(uc_fw, type)->i915;
432 
433 	/*
434 	 * we use FIRMWARE_UNINITIALIZED to detect checks against uc_fw->status
435 	 * before we're looked at the HW caps to see if we have uc support
436 	 */
437 	BUILD_BUG_ON(INTEL_UC_FIRMWARE_UNINITIALIZED);
438 	GEM_BUG_ON(uc_fw->status);
439 	GEM_BUG_ON(uc_fw->file_selected.path);
440 
441 	uc_fw->type = type;
442 
443 	if (HAS_GT_UC(i915)) {
444 		__uc_fw_auto_select(i915, uc_fw);
445 		__uc_fw_user_override(i915, uc_fw);
446 	}
447 
448 	intel_uc_fw_change_status(uc_fw, uc_fw->file_selected.path ? *uc_fw->file_selected.path ?
449 				  INTEL_UC_FIRMWARE_SELECTED :
450 				  INTEL_UC_FIRMWARE_DISABLED :
451 				  INTEL_UC_FIRMWARE_NOT_SUPPORTED);
452 }
453 
454 static void __force_fw_fetch_failures(struct intel_uc_fw *uc_fw, int e)
455 {
456 	struct drm_i915_private *i915 = __uc_fw_to_gt(uc_fw)->i915;
457 	bool user = e == -EINVAL;
458 
459 	if (i915_inject_probe_error(i915, e)) {
460 		/* non-existing blob */
461 		uc_fw->file_selected.path = "<invalid>";
462 		uc_fw->user_overridden = user;
463 	} else if (i915_inject_probe_error(i915, e)) {
464 		/* require next major version */
465 		uc_fw->file_wanted.ver.major += 1;
466 		uc_fw->file_wanted.ver.minor = 0;
467 		uc_fw->user_overridden = user;
468 	} else if (i915_inject_probe_error(i915, e)) {
469 		/* require next minor version */
470 		uc_fw->file_wanted.ver.minor += 1;
471 		uc_fw->user_overridden = user;
472 	} else if (uc_fw->file_wanted.ver.major &&
473 		   i915_inject_probe_error(i915, e)) {
474 		/* require prev major version */
475 		uc_fw->file_wanted.ver.major -= 1;
476 		uc_fw->file_wanted.ver.minor = 0;
477 		uc_fw->user_overridden = user;
478 	} else if (uc_fw->file_wanted.ver.minor &&
479 		   i915_inject_probe_error(i915, e)) {
480 		/* require prev minor version - hey, this should work! */
481 		uc_fw->file_wanted.ver.minor -= 1;
482 		uc_fw->user_overridden = user;
483 	} else if (user && i915_inject_probe_error(i915, e)) {
484 		/* officially unsupported platform */
485 		uc_fw->file_wanted.ver.major = 0;
486 		uc_fw->file_wanted.ver.minor = 0;
487 		uc_fw->user_overridden = true;
488 	}
489 }
490 
491 static int check_gsc_manifest(const struct firmware *fw,
492 			      struct intel_uc_fw *uc_fw)
493 {
494 	u32 *dw = (u32 *)fw->data;
495 	u32 version_hi = dw[HUC_GSC_VERSION_HI_DW];
496 	u32 version_lo = dw[HUC_GSC_VERSION_LO_DW];
497 
498 	uc_fw->file_selected.ver.major = FIELD_GET(HUC_GSC_MAJOR_VER_HI_MASK, version_hi);
499 	uc_fw->file_selected.ver.minor = FIELD_GET(HUC_GSC_MINOR_VER_HI_MASK, version_hi);
500 	uc_fw->file_selected.ver.patch = FIELD_GET(HUC_GSC_PATCH_VER_LO_MASK, version_lo);
501 
502 	return 0;
503 }
504 
505 static void uc_unpack_css_version(struct intel_uc_fw_ver *ver, u32 css_value)
506 {
507 	/* Get version numbers from the CSS header */
508 	ver->major = FIELD_GET(CSS_SW_VERSION_UC_MAJOR, css_value);
509 	ver->minor = FIELD_GET(CSS_SW_VERSION_UC_MINOR, css_value);
510 	ver->patch = FIELD_GET(CSS_SW_VERSION_UC_PATCH, css_value);
511 }
512 
513 static void guc_read_css_info(struct intel_uc_fw *uc_fw, struct uc_css_header *css)
514 {
515 	struct intel_guc *guc = container_of(uc_fw, struct intel_guc, fw);
516 
517 	/*
518 	 * The GuC firmware includes an extra version number to specify the
519 	 * submission API level. This allows submission code to work with
520 	 * multiple GuC versions without having to know the absolute firmware
521 	 * version number (there are likely to be multiple firmware releases
522 	 * which all support the same submission API level).
523 	 *
524 	 * Note that the spec for the CSS header defines this version number
525 	 * as 'vf_version' as it was originally intended for virtualisation.
526 	 * However, it is applicable to native submission as well.
527 	 *
528 	 * Unfortunately, due to an oversight, this version number was only
529 	 * exposed in the CSS header from v70.6.0.
530 	 */
531 	if (uc_fw->file_selected.ver.major >= 70) {
532 		if (uc_fw->file_selected.ver.minor >= 6) {
533 			/* v70.6.0 adds CSS header support */
534 			uc_unpack_css_version(&guc->submission_version, css->vf_version);
535 		} else if (uc_fw->file_selected.ver.minor >= 3) {
536 			/* v70.3.0 introduced v1.1.0 */
537 			guc->submission_version.major = 1;
538 			guc->submission_version.minor = 1;
539 			guc->submission_version.patch = 0;
540 		} else {
541 			/* v70.0.0 introduced v1.0.0 */
542 			guc->submission_version.major = 1;
543 			guc->submission_version.minor = 0;
544 			guc->submission_version.patch = 0;
545 		}
546 	} else if (uc_fw->file_selected.ver.major >= 69) {
547 		/* v69.0.0 introduced v0.10.0 */
548 		guc->submission_version.major = 0;
549 		guc->submission_version.minor = 10;
550 		guc->submission_version.patch = 0;
551 	} else {
552 		/* Prior versions were v0.1.0 */
553 		guc->submission_version.major = 0;
554 		guc->submission_version.minor = 1;
555 		guc->submission_version.patch = 0;
556 	}
557 
558 	uc_fw->private_data_size = css->private_data_size;
559 }
560 
561 static int check_ccs_header(struct intel_gt *gt,
562 			    const struct firmware *fw,
563 			    struct intel_uc_fw *uc_fw)
564 {
565 	struct uc_css_header *css;
566 	size_t size;
567 
568 	/* Check the size of the blob before examining buffer contents */
569 	if (unlikely(fw->size < sizeof(struct uc_css_header))) {
570 		gt_warn(gt, "%s firmware %s: invalid size: %zu < %zu\n",
571 			intel_uc_fw_type_repr(uc_fw->type), uc_fw->file_selected.path,
572 			fw->size, sizeof(struct uc_css_header));
573 		return -ENODATA;
574 	}
575 
576 	css = (struct uc_css_header *)fw->data;
577 
578 	/* Check integrity of size values inside CSS header */
579 	size = (css->header_size_dw - css->key_size_dw - css->modulus_size_dw -
580 		css->exponent_size_dw) * sizeof(u32);
581 	if (unlikely(size != sizeof(struct uc_css_header))) {
582 		gt_warn(gt, "%s firmware %s: unexpected header size: %zu != %zu\n",
583 			intel_uc_fw_type_repr(uc_fw->type), uc_fw->file_selected.path,
584 			fw->size, sizeof(struct uc_css_header));
585 		return -EPROTO;
586 	}
587 
588 	/* uCode size must calculated from other sizes */
589 	uc_fw->ucode_size = (css->size_dw - css->header_size_dw) * sizeof(u32);
590 
591 	/* now RSA */
592 	uc_fw->rsa_size = css->key_size_dw * sizeof(u32);
593 
594 	/* At least, it should have header, uCode and RSA. Size of all three. */
595 	size = sizeof(struct uc_css_header) + uc_fw->ucode_size + uc_fw->rsa_size;
596 	if (unlikely(fw->size < size)) {
597 		gt_warn(gt, "%s firmware %s: invalid size: %zu < %zu\n",
598 			intel_uc_fw_type_repr(uc_fw->type), uc_fw->file_selected.path,
599 			fw->size, size);
600 		return -ENOEXEC;
601 	}
602 
603 	/* Sanity check whether this fw is not larger than whole WOPCM memory */
604 	size = __intel_uc_fw_get_upload_size(uc_fw);
605 	if (unlikely(size >= gt->wopcm.size)) {
606 		gt_warn(gt, "%s firmware %s: invalid size: %zu > %zu\n",
607 			intel_uc_fw_type_repr(uc_fw->type), uc_fw->file_selected.path,
608 			size, (size_t)gt->wopcm.size);
609 		return -E2BIG;
610 	}
611 
612 	uc_unpack_css_version(&uc_fw->file_selected.ver, css->sw_version);
613 
614 	if (uc_fw->type == INTEL_UC_FW_TYPE_GUC)
615 		guc_read_css_info(uc_fw, css);
616 
617 	return 0;
618 }
619 
620 static bool is_ver_8bit(struct intel_uc_fw_ver *ver)
621 {
622 	return ver->major < 0xFF && ver->minor < 0xFF && ver->patch < 0xFF;
623 }
624 
625 static bool guc_check_version_range(struct intel_uc_fw *uc_fw)
626 {
627 	struct intel_guc *guc = container_of(uc_fw, struct intel_guc, fw);
628 
629 	/*
630 	 * GuC version number components are defined as being 8-bits.
631 	 * The submission code relies on this to optimise version comparison
632 	 * tests. So enforce the restriction here.
633 	 */
634 
635 	if (!is_ver_8bit(&uc_fw->file_selected.ver)) {
636 		gt_warn(__uc_fw_to_gt(uc_fw), "%s firmware: invalid file version: 0x%02X:%02X:%02X\n",
637 			intel_uc_fw_type_repr(uc_fw->type),
638 			uc_fw->file_selected.ver.major,
639 			uc_fw->file_selected.ver.minor,
640 			uc_fw->file_selected.ver.patch);
641 		return false;
642 	}
643 
644 	if (!is_ver_8bit(&guc->submission_version)) {
645 		gt_warn(__uc_fw_to_gt(uc_fw), "%s firmware: invalid submit version: 0x%02X:%02X:%02X\n",
646 			intel_uc_fw_type_repr(uc_fw->type),
647 			guc->submission_version.major,
648 			guc->submission_version.minor,
649 			guc->submission_version.patch);
650 		return false;
651 	}
652 
653 	return true;
654 }
655 
656 static int check_fw_header(struct intel_gt *gt,
657 			   const struct firmware *fw,
658 			   struct intel_uc_fw *uc_fw)
659 {
660 	int err = 0;
661 
662 	/* GSC FW version is queried after the FW is loaded */
663 	if (uc_fw->type == INTEL_UC_FW_TYPE_GSC)
664 		return 0;
665 
666 	if (uc_fw->loaded_via_gsc)
667 		err = check_gsc_manifest(fw, uc_fw);
668 	else
669 		err = check_ccs_header(gt, fw, uc_fw);
670 	if (err)
671 		return err;
672 
673 	return 0;
674 }
675 
676 static int try_firmware_load(struct intel_uc_fw *uc_fw, const struct firmware **fw)
677 {
678 	struct intel_gt *gt = __uc_fw_to_gt(uc_fw);
679 	struct device *dev = gt->i915->drm.dev;
680 	int err;
681 
682 	err = firmware_request_nowarn(fw, uc_fw->file_selected.path, dev);
683 
684 	if (err)
685 		return err;
686 
687 	if ((*fw)->size > INTEL_UC_RSVD_GGTT_PER_FW) {
688 		gt_err(gt, "%s firmware %s: size (%zuKB) exceeds max supported size (%uKB)\n",
689 		       intel_uc_fw_type_repr(uc_fw->type), uc_fw->file_selected.path,
690 		       (*fw)->size / SZ_1K, INTEL_UC_RSVD_GGTT_PER_FW / SZ_1K);
691 
692 		/* try to find another blob to load */
693 		release_firmware(*fw);
694 		*fw = NULL;
695 		return -ENOENT;
696 	}
697 
698 	return 0;
699 }
700 
701 /**
702  * intel_uc_fw_fetch - fetch uC firmware
703  * @uc_fw: uC firmware
704  *
705  * Fetch uC firmware into GEM obj.
706  *
707  * Return: 0 on success, a negative errno code on failure.
708  */
709 int intel_uc_fw_fetch(struct intel_uc_fw *uc_fw)
710 {
711 	struct intel_gt *gt = __uc_fw_to_gt(uc_fw);
712 	struct drm_i915_private *i915 = gt->i915;
713 	struct intel_uc_fw_file file_ideal;
714 	struct drm_i915_gem_object *obj;
715 	const struct firmware *fw = NULL;
716 	bool old_ver = false;
717 	int err;
718 
719 	GEM_BUG_ON(!gt->wopcm.size);
720 	GEM_BUG_ON(!intel_uc_fw_is_enabled(uc_fw));
721 
722 	err = i915_inject_probe_error(i915, -ENXIO);
723 	if (err)
724 		goto fail;
725 
726 	__force_fw_fetch_failures(uc_fw, -EINVAL);
727 	__force_fw_fetch_failures(uc_fw, -ESTALE);
728 
729 	err = try_firmware_load(uc_fw, &fw);
730 	memcpy(&file_ideal, &uc_fw->file_wanted, sizeof(file_ideal));
731 
732 	/* Any error is terminal if overriding. Don't bother searching for older versions */
733 	if (err && intel_uc_fw_is_overridden(uc_fw))
734 		goto fail;
735 
736 	while (err == -ENOENT) {
737 		old_ver = true;
738 
739 		__uc_fw_auto_select(i915, uc_fw);
740 		if (!uc_fw->file_selected.path) {
741 			/*
742 			 * No more options! But set the path back to something
743 			 * valid just in case it gets dereferenced.
744 			 */
745 			uc_fw->file_selected.path = file_ideal.path;
746 
747 			/* Also, preserve the version that was really wanted */
748 			memcpy(&uc_fw->file_wanted, &file_ideal, sizeof(uc_fw->file_wanted));
749 			break;
750 		}
751 
752 		err = try_firmware_load(uc_fw, &fw);
753 	}
754 
755 	if (err)
756 		goto fail;
757 
758 	err = check_fw_header(gt, fw, uc_fw);
759 	if (err)
760 		goto fail;
761 
762 	if (uc_fw->type == INTEL_UC_FW_TYPE_GUC && !guc_check_version_range(uc_fw))
763 		goto fail;
764 
765 	if (uc_fw->file_wanted.ver.major && uc_fw->file_selected.ver.major) {
766 		/* Check the file's major version was as it claimed */
767 		if (uc_fw->file_selected.ver.major != uc_fw->file_wanted.ver.major) {
768 			gt_notice(gt, "%s firmware %s: unexpected version: %u.%u != %u.%u\n",
769 				  intel_uc_fw_type_repr(uc_fw->type), uc_fw->file_selected.path,
770 				  uc_fw->file_selected.ver.major, uc_fw->file_selected.ver.minor,
771 				  uc_fw->file_wanted.ver.major, uc_fw->file_wanted.ver.minor);
772 			if (!intel_uc_fw_is_overridden(uc_fw)) {
773 				err = -ENOEXEC;
774 				goto fail;
775 			}
776 		} else {
777 			if (uc_fw->file_selected.ver.minor < uc_fw->file_wanted.ver.minor)
778 				old_ver = true;
779 		}
780 	}
781 
782 	if (old_ver && uc_fw->file_selected.ver.major) {
783 		/* Preserve the version that was really wanted */
784 		memcpy(&uc_fw->file_wanted, &file_ideal, sizeof(uc_fw->file_wanted));
785 
786 		gt_notice(gt, "%s firmware %s (%d.%d) is recommended, but only %s (%d.%d) was found\n",
787 			  intel_uc_fw_type_repr(uc_fw->type),
788 			  uc_fw->file_wanted.path,
789 			  uc_fw->file_wanted.ver.major, uc_fw->file_wanted.ver.minor,
790 			  uc_fw->file_selected.path,
791 			  uc_fw->file_selected.ver.major, uc_fw->file_selected.ver.minor);
792 		gt_info(gt, "Consider updating your linux-firmware pkg or downloading from %s\n",
793 			INTEL_UC_FIRMWARE_URL);
794 	}
795 
796 	if (HAS_LMEM(i915)) {
797 		obj = i915_gem_object_create_lmem_from_data(i915, fw->data, fw->size);
798 		if (!IS_ERR(obj))
799 			obj->flags |= I915_BO_ALLOC_PM_EARLY;
800 	} else {
801 		obj = i915_gem_object_create_shmem_from_data(i915, fw->data, fw->size);
802 	}
803 
804 	if (IS_ERR(obj)) {
805 		err = PTR_ERR(obj);
806 		goto fail;
807 	}
808 
809 	uc_fw->obj = obj;
810 	uc_fw->size = fw->size;
811 	intel_uc_fw_change_status(uc_fw, INTEL_UC_FIRMWARE_AVAILABLE);
812 
813 	release_firmware(fw);
814 	return 0;
815 
816 fail:
817 	intel_uc_fw_change_status(uc_fw, err == -ENOENT ?
818 				  INTEL_UC_FIRMWARE_MISSING :
819 				  INTEL_UC_FIRMWARE_ERROR);
820 
821 	gt_probe_error(gt, "%s firmware %s: fetch failed %pe\n",
822 		       intel_uc_fw_type_repr(uc_fw->type), uc_fw->file_selected.path, ERR_PTR(err));
823 	gt_info(gt, "%s firmware(s) can be downloaded from %s\n",
824 		intel_uc_fw_type_repr(uc_fw->type), INTEL_UC_FIRMWARE_URL);
825 
826 	release_firmware(fw);		/* OK even if fw is NULL */
827 	return err;
828 }
829 
830 static u32 uc_fw_ggtt_offset(struct intel_uc_fw *uc_fw)
831 {
832 	struct intel_gt *gt = __uc_fw_to_gt(uc_fw);
833 	struct i915_ggtt *ggtt = gt->ggtt;
834 	struct drm_mm_node *node = &ggtt->uc_fw;
835 	u32 offset = uc_fw->type * INTEL_UC_RSVD_GGTT_PER_FW;
836 
837 	/*
838 	 * The media GT shares the GGTT with the root GT, which means that
839 	 * we need to use different offsets for the binaries on the media GT.
840 	 * To keep the math simple, we use 8MB for the root tile and 8MB for
841 	 * the media one. This will need to be updated if we ever have more
842 	 * than 1 media GT.
843 	 */
844 	BUILD_BUG_ON(INTEL_UC_FW_NUM_TYPES * INTEL_UC_RSVD_GGTT_PER_FW > SZ_8M);
845 	GEM_BUG_ON(gt->type == GT_MEDIA && gt->info.id > 1);
846 	if (gt->type == GT_MEDIA)
847 		offset += SZ_8M;
848 
849 	GEM_BUG_ON(!drm_mm_node_allocated(node));
850 	GEM_BUG_ON(upper_32_bits(node->start));
851 	GEM_BUG_ON(upper_32_bits(node->start + node->size - 1));
852 	GEM_BUG_ON(offset + uc_fw->obj->base.size > node->size);
853 	GEM_BUG_ON(uc_fw->obj->base.size > INTEL_UC_RSVD_GGTT_PER_FW);
854 
855 	return lower_32_bits(node->start + offset);
856 }
857 
858 static void uc_fw_bind_ggtt(struct intel_uc_fw *uc_fw)
859 {
860 	struct drm_i915_gem_object *obj = uc_fw->obj;
861 	struct i915_ggtt *ggtt = __uc_fw_to_gt(uc_fw)->ggtt;
862 	struct i915_vma_resource *dummy = &uc_fw->dummy;
863 	u32 pte_flags = 0;
864 
865 	dummy->start = uc_fw_ggtt_offset(uc_fw);
866 	dummy->node_size = obj->base.size;
867 	dummy->bi.pages = obj->mm.pages;
868 
869 	GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
870 
871 	/* uc_fw->obj cache domains were not controlled across suspend */
872 	if (i915_gem_object_has_struct_page(obj))
873 		drm_clflush_sg(dummy->bi.pages);
874 
875 	if (i915_gem_object_is_lmem(obj))
876 		pte_flags |= PTE_LM;
877 
878 	if (ggtt->vm.raw_insert_entries)
879 		ggtt->vm.raw_insert_entries(&ggtt->vm, dummy, I915_CACHE_NONE, pte_flags);
880 	else
881 		ggtt->vm.insert_entries(&ggtt->vm, dummy, I915_CACHE_NONE, pte_flags);
882 }
883 
884 static void uc_fw_unbind_ggtt(struct intel_uc_fw *uc_fw)
885 {
886 	struct drm_i915_gem_object *obj = uc_fw->obj;
887 	struct i915_ggtt *ggtt = __uc_fw_to_gt(uc_fw)->ggtt;
888 	u64 start = uc_fw_ggtt_offset(uc_fw);
889 
890 	ggtt->vm.clear_range(&ggtt->vm, start, obj->base.size);
891 }
892 
893 static int uc_fw_xfer(struct intel_uc_fw *uc_fw, u32 dst_offset, u32 dma_flags)
894 {
895 	struct intel_gt *gt = __uc_fw_to_gt(uc_fw);
896 	struct intel_uncore *uncore = gt->uncore;
897 	u64 offset;
898 	int ret;
899 
900 	ret = i915_inject_probe_error(gt->i915, -ETIMEDOUT);
901 	if (ret)
902 		return ret;
903 
904 	intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
905 
906 	/* Set the source address for the uCode */
907 	offset = uc_fw_ggtt_offset(uc_fw);
908 	GEM_BUG_ON(upper_32_bits(offset) & 0xFFFF0000);
909 	intel_uncore_write_fw(uncore, DMA_ADDR_0_LOW, lower_32_bits(offset));
910 	intel_uncore_write_fw(uncore, DMA_ADDR_0_HIGH, upper_32_bits(offset));
911 
912 	/* Set the DMA destination */
913 	intel_uncore_write_fw(uncore, DMA_ADDR_1_LOW, dst_offset);
914 	intel_uncore_write_fw(uncore, DMA_ADDR_1_HIGH, DMA_ADDRESS_SPACE_WOPCM);
915 
916 	/*
917 	 * Set the transfer size. The header plus uCode will be copied to WOPCM
918 	 * via DMA, excluding any other components
919 	 */
920 	intel_uncore_write_fw(uncore, DMA_COPY_SIZE,
921 			      sizeof(struct uc_css_header) + uc_fw->ucode_size);
922 
923 	/* Start the DMA */
924 	intel_uncore_write_fw(uncore, DMA_CTRL,
925 			      _MASKED_BIT_ENABLE(dma_flags | START_DMA));
926 
927 	/* Wait for DMA to finish */
928 	ret = intel_wait_for_register_fw(uncore, DMA_CTRL, START_DMA, 0, 100);
929 	if (ret)
930 		gt_err(gt, "DMA for %s fw failed, DMA_CTRL=%u\n",
931 		       intel_uc_fw_type_repr(uc_fw->type),
932 		       intel_uncore_read_fw(uncore, DMA_CTRL));
933 
934 	/* Disable the bits once DMA is over */
935 	intel_uncore_write_fw(uncore, DMA_CTRL, _MASKED_BIT_DISABLE(dma_flags));
936 
937 	intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
938 
939 	return ret;
940 }
941 
942 int intel_uc_fw_mark_load_failed(struct intel_uc_fw *uc_fw, int err)
943 {
944 	struct intel_gt *gt = __uc_fw_to_gt(uc_fw);
945 
946 	GEM_BUG_ON(!intel_uc_fw_is_loadable(uc_fw));
947 
948 	gt_probe_error(gt, "Failed to load %s firmware %s %pe\n",
949 		       intel_uc_fw_type_repr(uc_fw->type), uc_fw->file_selected.path, ERR_PTR(err));
950 	intel_uc_fw_change_status(uc_fw, INTEL_UC_FIRMWARE_LOAD_FAIL);
951 
952 	return err;
953 }
954 
955 /**
956  * intel_uc_fw_upload - load uC firmware using custom loader
957  * @uc_fw: uC firmware
958  * @dst_offset: destination offset
959  * @dma_flags: flags for flags for dma ctrl
960  *
961  * Loads uC firmware and updates internal flags.
962  *
963  * Return: 0 on success, non-zero on failure.
964  */
965 int intel_uc_fw_upload(struct intel_uc_fw *uc_fw, u32 dst_offset, u32 dma_flags)
966 {
967 	struct intel_gt *gt = __uc_fw_to_gt(uc_fw);
968 	int err;
969 
970 	/* make sure the status was cleared the last time we reset the uc */
971 	GEM_BUG_ON(intel_uc_fw_is_loaded(uc_fw));
972 
973 	err = i915_inject_probe_error(gt->i915, -ENOEXEC);
974 	if (err)
975 		return err;
976 
977 	if (!intel_uc_fw_is_loadable(uc_fw))
978 		return -ENOEXEC;
979 
980 	/* Call custom loader */
981 	uc_fw_bind_ggtt(uc_fw);
982 	err = uc_fw_xfer(uc_fw, dst_offset, dma_flags);
983 	uc_fw_unbind_ggtt(uc_fw);
984 	if (err)
985 		goto fail;
986 
987 	intel_uc_fw_change_status(uc_fw, INTEL_UC_FIRMWARE_TRANSFERRED);
988 	return 0;
989 
990 fail:
991 	return intel_uc_fw_mark_load_failed(uc_fw, err);
992 }
993 
994 static inline bool uc_fw_need_rsa_in_memory(struct intel_uc_fw *uc_fw)
995 {
996 	/*
997 	 * The HW reads the GuC RSA from memory if the key size is > 256 bytes,
998 	 * while it reads it from the 64 RSA registers if it is smaller.
999 	 * The HuC RSA is always read from memory.
1000 	 */
1001 	return uc_fw->type == INTEL_UC_FW_TYPE_HUC || uc_fw->rsa_size > 256;
1002 }
1003 
1004 static int uc_fw_rsa_data_create(struct intel_uc_fw *uc_fw)
1005 {
1006 	struct intel_gt *gt = __uc_fw_to_gt(uc_fw);
1007 	struct i915_vma *vma;
1008 	size_t copied;
1009 	void *vaddr;
1010 	int err;
1011 
1012 	err = i915_inject_probe_error(gt->i915, -ENXIO);
1013 	if (err)
1014 		return err;
1015 
1016 	if (!uc_fw_need_rsa_in_memory(uc_fw))
1017 		return 0;
1018 
1019 	/*
1020 	 * uC firmwares will sit above GUC_GGTT_TOP and will not map through
1021 	 * GGTT. Unfortunately, this means that the GuC HW cannot perform the uC
1022 	 * authentication from memory, as the RSA offset now falls within the
1023 	 * GuC inaccessible range. We resort to perma-pinning an additional vma
1024 	 * within the accessible range that only contains the RSA signature.
1025 	 * The GuC HW can use this extra pinning to perform the authentication
1026 	 * since its GGTT offset will be GuC accessible.
1027 	 */
1028 	GEM_BUG_ON(uc_fw->rsa_size > PAGE_SIZE);
1029 	vma = intel_guc_allocate_vma(&gt->uc.guc, PAGE_SIZE);
1030 	if (IS_ERR(vma))
1031 		return PTR_ERR(vma);
1032 
1033 	vaddr = i915_gem_object_pin_map_unlocked(vma->obj,
1034 						 i915_coherent_map_type(gt->i915, vma->obj, true));
1035 	if (IS_ERR(vaddr)) {
1036 		i915_vma_unpin_and_release(&vma, 0);
1037 		err = PTR_ERR(vaddr);
1038 		goto unpin_out;
1039 	}
1040 
1041 	copied = intel_uc_fw_copy_rsa(uc_fw, vaddr, vma->size);
1042 	i915_gem_object_unpin_map(vma->obj);
1043 
1044 	if (copied < uc_fw->rsa_size) {
1045 		err = -ENOMEM;
1046 		goto unpin_out;
1047 	}
1048 
1049 	uc_fw->rsa_data = vma;
1050 
1051 	return 0;
1052 
1053 unpin_out:
1054 	i915_vma_unpin_and_release(&vma, 0);
1055 	return err;
1056 }
1057 
1058 static void uc_fw_rsa_data_destroy(struct intel_uc_fw *uc_fw)
1059 {
1060 	i915_vma_unpin_and_release(&uc_fw->rsa_data, 0);
1061 }
1062 
1063 int intel_uc_fw_init(struct intel_uc_fw *uc_fw)
1064 {
1065 	int err;
1066 
1067 	/* this should happen before the load! */
1068 	GEM_BUG_ON(intel_uc_fw_is_loaded(uc_fw));
1069 
1070 	if (!intel_uc_fw_is_available(uc_fw))
1071 		return -ENOEXEC;
1072 
1073 	err = i915_gem_object_pin_pages_unlocked(uc_fw->obj);
1074 	if (err) {
1075 		gt_dbg(__uc_fw_to_gt(uc_fw), "%s fw pin-pages failed %pe\n",
1076 		       intel_uc_fw_type_repr(uc_fw->type), ERR_PTR(err));
1077 		goto out;
1078 	}
1079 
1080 	err = uc_fw_rsa_data_create(uc_fw);
1081 	if (err) {
1082 		gt_dbg(__uc_fw_to_gt(uc_fw), "%s fw rsa data creation failed %pe\n",
1083 		       intel_uc_fw_type_repr(uc_fw->type), ERR_PTR(err));
1084 		goto out_unpin;
1085 	}
1086 
1087 	return 0;
1088 
1089 out_unpin:
1090 	i915_gem_object_unpin_pages(uc_fw->obj);
1091 out:
1092 	return err;
1093 }
1094 
1095 void intel_uc_fw_fini(struct intel_uc_fw *uc_fw)
1096 {
1097 	uc_fw_rsa_data_destroy(uc_fw);
1098 
1099 	if (i915_gem_object_has_pinned_pages(uc_fw->obj))
1100 		i915_gem_object_unpin_pages(uc_fw->obj);
1101 
1102 	intel_uc_fw_change_status(uc_fw, INTEL_UC_FIRMWARE_AVAILABLE);
1103 }
1104 
1105 /**
1106  * intel_uc_fw_cleanup_fetch - cleanup uC firmware
1107  * @uc_fw: uC firmware
1108  *
1109  * Cleans up uC firmware by releasing the firmware GEM obj.
1110  */
1111 void intel_uc_fw_cleanup_fetch(struct intel_uc_fw *uc_fw)
1112 {
1113 	if (!intel_uc_fw_is_available(uc_fw))
1114 		return;
1115 
1116 	i915_gem_object_put(fetch_and_zero(&uc_fw->obj));
1117 
1118 	intel_uc_fw_change_status(uc_fw, INTEL_UC_FIRMWARE_SELECTED);
1119 }
1120 
1121 /**
1122  * intel_uc_fw_copy_rsa - copy fw RSA to buffer
1123  *
1124  * @uc_fw: uC firmware
1125  * @dst: dst buffer
1126  * @max_len: max number of bytes to copy
1127  *
1128  * Return: number of copied bytes.
1129  */
1130 size_t intel_uc_fw_copy_rsa(struct intel_uc_fw *uc_fw, void *dst, u32 max_len)
1131 {
1132 	struct intel_memory_region *mr = uc_fw->obj->mm.region;
1133 	u32 size = min_t(u32, uc_fw->rsa_size, max_len);
1134 	u32 offset = sizeof(struct uc_css_header) + uc_fw->ucode_size;
1135 	struct sgt_iter iter;
1136 	size_t count = 0;
1137 	int idx;
1138 
1139 	/* Called during reset handling, must be atomic [no fs_reclaim] */
1140 	GEM_BUG_ON(!intel_uc_fw_is_available(uc_fw));
1141 
1142 	idx = offset >> PAGE_SHIFT;
1143 	offset = offset_in_page(offset);
1144 	if (i915_gem_object_has_struct_page(uc_fw->obj)) {
1145 		struct page *page;
1146 
1147 		for_each_sgt_page(page, iter, uc_fw->obj->mm.pages) {
1148 			u32 len = min_t(u32, size, PAGE_SIZE - offset);
1149 			void *vaddr;
1150 
1151 			if (idx > 0) {
1152 				idx--;
1153 				continue;
1154 			}
1155 
1156 			vaddr = kmap_atomic(page);
1157 			memcpy(dst, vaddr + offset, len);
1158 			kunmap_atomic(vaddr);
1159 
1160 			offset = 0;
1161 			dst += len;
1162 			size -= len;
1163 			count += len;
1164 			if (!size)
1165 				break;
1166 		}
1167 	} else {
1168 		dma_addr_t addr;
1169 
1170 		for_each_sgt_daddr(addr, iter, uc_fw->obj->mm.pages) {
1171 			u32 len = min_t(u32, size, PAGE_SIZE - offset);
1172 			void __iomem *vaddr;
1173 
1174 			if (idx > 0) {
1175 				idx--;
1176 				continue;
1177 			}
1178 
1179 			vaddr = io_mapping_map_atomic_wc(&mr->iomap,
1180 							 addr - mr->region.start);
1181 			memcpy_fromio(dst, vaddr + offset, len);
1182 			io_mapping_unmap_atomic(vaddr);
1183 
1184 			offset = 0;
1185 			dst += len;
1186 			size -= len;
1187 			count += len;
1188 			if (!size)
1189 				break;
1190 		}
1191 	}
1192 
1193 	return count;
1194 }
1195 
1196 /**
1197  * intel_uc_fw_dump - dump information about uC firmware
1198  * @uc_fw: uC firmware
1199  * @p: the &drm_printer
1200  *
1201  * Pretty printer for uC firmware.
1202  */
1203 void intel_uc_fw_dump(const struct intel_uc_fw *uc_fw, struct drm_printer *p)
1204 {
1205 	bool got_wanted;
1206 
1207 	drm_printf(p, "%s firmware: %s\n",
1208 		   intel_uc_fw_type_repr(uc_fw->type), uc_fw->file_selected.path);
1209 	if (uc_fw->file_selected.path != uc_fw->file_wanted.path)
1210 		drm_printf(p, "%s firmware wanted: %s\n",
1211 			   intel_uc_fw_type_repr(uc_fw->type), uc_fw->file_wanted.path);
1212 	drm_printf(p, "\tstatus: %s\n",
1213 		   intel_uc_fw_status_repr(uc_fw->status));
1214 
1215 	if (uc_fw->file_selected.ver.major < uc_fw->file_wanted.ver.major)
1216 		got_wanted = false;
1217 	else if ((uc_fw->file_selected.ver.major == uc_fw->file_wanted.ver.major) &&
1218 		 (uc_fw->file_selected.ver.minor < uc_fw->file_wanted.ver.minor))
1219 		got_wanted = false;
1220 	else if ((uc_fw->file_selected.ver.major == uc_fw->file_wanted.ver.major) &&
1221 		 (uc_fw->file_selected.ver.minor == uc_fw->file_wanted.ver.minor) &&
1222 		 (uc_fw->file_selected.ver.patch < uc_fw->file_wanted.ver.patch))
1223 		got_wanted = false;
1224 	else
1225 		got_wanted = true;
1226 
1227 	if (!got_wanted)
1228 		drm_printf(p, "\tversion: wanted %u.%u.%u, found %u.%u.%u\n",
1229 			   uc_fw->file_wanted.ver.major,
1230 			   uc_fw->file_wanted.ver.minor,
1231 			   uc_fw->file_wanted.ver.patch,
1232 			   uc_fw->file_selected.ver.major,
1233 			   uc_fw->file_selected.ver.minor,
1234 			   uc_fw->file_selected.ver.patch);
1235 	else
1236 		drm_printf(p, "\tversion: found %u.%u.%u\n",
1237 			   uc_fw->file_selected.ver.major,
1238 			   uc_fw->file_selected.ver.minor,
1239 			   uc_fw->file_selected.ver.patch);
1240 	drm_printf(p, "\tuCode: %u bytes\n", uc_fw->ucode_size);
1241 	drm_printf(p, "\tRSA: %u bytes\n", uc_fw->rsa_size);
1242 }
1243