xref: /linux/drivers/gpu/drm/i915/gt/uc/intel_uc_fw.c (revision 69bfec7548f4c1595bac0e3ddfc0458a5af31f4c)
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 "intel_uc_fw.h"
15 #include "intel_uc_fw_abi.h"
16 #include "i915_drv.h"
17 #include "i915_reg.h"
18 
19 static inline struct intel_gt *
20 ____uc_fw_to_gt(struct intel_uc_fw *uc_fw, enum intel_uc_fw_type type)
21 {
22 	GEM_BUG_ON(type >= INTEL_UC_FW_NUM_TYPES);
23 
24 	switch (type) {
25 	case INTEL_UC_FW_TYPE_GUC:
26 		return container_of(uc_fw, struct intel_gt, uc.guc.fw);
27 	case INTEL_UC_FW_TYPE_HUC:
28 		return container_of(uc_fw, struct intel_gt, uc.huc.fw);
29 	case INTEL_UC_FW_TYPE_GSC:
30 		return container_of(uc_fw, struct intel_gt, uc.gsc.fw);
31 	}
32 
33 	return NULL;
34 }
35 
36 static inline struct intel_gt *__uc_fw_to_gt(struct intel_uc_fw *uc_fw)
37 {
38 	GEM_BUG_ON(uc_fw->status == INTEL_UC_FIRMWARE_UNINITIALIZED);
39 	return ____uc_fw_to_gt(uc_fw, uc_fw->type);
40 }
41 
42 #ifdef CONFIG_DRM_I915_DEBUG_GUC
43 void intel_uc_fw_change_status(struct intel_uc_fw *uc_fw,
44 			       enum intel_uc_fw_status status)
45 {
46 	uc_fw->__status =  status;
47 	drm_dbg(&__uc_fw_to_gt(uc_fw)->i915->drm,
48 		"%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 drm_i915_private *i915 = gt->i915;
566 	struct uc_css_header *css;
567 	size_t size;
568 
569 	/* Check the size of the blob before examining buffer contents */
570 	if (unlikely(fw->size < sizeof(struct uc_css_header))) {
571 		drm_warn(&i915->drm, "%s firmware %s: invalid size: %zu < %zu\n",
572 			 intel_uc_fw_type_repr(uc_fw->type), uc_fw->file_selected.path,
573 			 fw->size, sizeof(struct uc_css_header));
574 		return -ENODATA;
575 	}
576 
577 	css = (struct uc_css_header *)fw->data;
578 
579 	/* Check integrity of size values inside CSS header */
580 	size = (css->header_size_dw - css->key_size_dw - css->modulus_size_dw -
581 		css->exponent_size_dw) * sizeof(u32);
582 	if (unlikely(size != sizeof(struct uc_css_header))) {
583 		drm_warn(&i915->drm,
584 			 "%s firmware %s: unexpected header size: %zu != %zu\n",
585 			 intel_uc_fw_type_repr(uc_fw->type), uc_fw->file_selected.path,
586 			 fw->size, sizeof(struct uc_css_header));
587 		return -EPROTO;
588 	}
589 
590 	/* uCode size must calculated from other sizes */
591 	uc_fw->ucode_size = (css->size_dw - css->header_size_dw) * sizeof(u32);
592 
593 	/* now RSA */
594 	uc_fw->rsa_size = css->key_size_dw * sizeof(u32);
595 
596 	/* At least, it should have header, uCode and RSA. Size of all three. */
597 	size = sizeof(struct uc_css_header) + uc_fw->ucode_size + uc_fw->rsa_size;
598 	if (unlikely(fw->size < size)) {
599 		drm_warn(&i915->drm, "%s firmware %s: invalid size: %zu < %zu\n",
600 			 intel_uc_fw_type_repr(uc_fw->type), uc_fw->file_selected.path,
601 			 fw->size, size);
602 		return -ENOEXEC;
603 	}
604 
605 	/* Sanity check whether this fw is not larger than whole WOPCM memory */
606 	size = __intel_uc_fw_get_upload_size(uc_fw);
607 	if (unlikely(size >= gt->wopcm.size)) {
608 		drm_warn(&i915->drm, "%s firmware %s: invalid size: %zu > %zu\n",
609 			 intel_uc_fw_type_repr(uc_fw->type), uc_fw->file_selected.path,
610 			 size, (size_t)gt->wopcm.size);
611 		return -E2BIG;
612 	}
613 
614 	uc_unpack_css_version(&uc_fw->file_selected.ver, css->sw_version);
615 
616 	if (uc_fw->type == INTEL_UC_FW_TYPE_GUC)
617 		guc_read_css_info(uc_fw, css);
618 
619 	return 0;
620 }
621 
622 static bool is_ver_8bit(struct intel_uc_fw_ver *ver)
623 {
624 	return ver->major < 0xFF && ver->minor < 0xFF && ver->patch < 0xFF;
625 }
626 
627 static bool guc_check_version_range(struct intel_uc_fw *uc_fw)
628 {
629 	struct intel_guc *guc = container_of(uc_fw, struct intel_guc, fw);
630 
631 	/*
632 	 * GuC version number components are defined as being 8-bits.
633 	 * The submission code relies on this to optimise version comparison
634 	 * tests. So enforce the restriction here.
635 	 */
636 
637 	if (!is_ver_8bit(&uc_fw->file_selected.ver)) {
638 		drm_warn(&__uc_fw_to_gt(uc_fw)->i915->drm, "%s firmware: invalid file version: 0x%02X:%02X:%02X\n",
639 			 intel_uc_fw_type_repr(uc_fw->type),
640 			 uc_fw->file_selected.ver.major,
641 			 uc_fw->file_selected.ver.minor,
642 			 uc_fw->file_selected.ver.patch);
643 		return false;
644 	}
645 
646 	if (!is_ver_8bit(&guc->submission_version)) {
647 		drm_warn(&__uc_fw_to_gt(uc_fw)->i915->drm, "%s firmware: invalid submit version: 0x%02X:%02X:%02X\n",
648 			 intel_uc_fw_type_repr(uc_fw->type),
649 			 guc->submission_version.major,
650 			 guc->submission_version.minor,
651 			 guc->submission_version.patch);
652 		return false;
653 	}
654 
655 	return true;
656 }
657 
658 static int check_fw_header(struct intel_gt *gt,
659 			   const struct firmware *fw,
660 			   struct intel_uc_fw *uc_fw)
661 {
662 	int err = 0;
663 
664 	/* GSC FW version is queried after the FW is loaded */
665 	if (uc_fw->type == INTEL_UC_FW_TYPE_GSC)
666 		return 0;
667 
668 	if (uc_fw->loaded_via_gsc)
669 		err = check_gsc_manifest(fw, uc_fw);
670 	else
671 		err = check_ccs_header(gt, fw, uc_fw);
672 	if (err)
673 		return err;
674 
675 	return 0;
676 }
677 
678 static int try_firmware_load(struct intel_uc_fw *uc_fw, const struct firmware **fw)
679 {
680 	struct intel_gt *gt = __uc_fw_to_gt(uc_fw);
681 	struct device *dev = gt->i915->drm.dev;
682 	int err;
683 
684 	err = firmware_request_nowarn(fw, uc_fw->file_selected.path, dev);
685 
686 	if (err)
687 		return err;
688 
689 	if ((*fw)->size > INTEL_UC_RSVD_GGTT_PER_FW) {
690 		drm_err(&gt->i915->drm,
691 			"%s firmware %s: size (%zuKB) exceeds max supported size (%uKB)\n",
692 			intel_uc_fw_type_repr(uc_fw->type), uc_fw->file_selected.path,
693 			(*fw)->size / SZ_1K, INTEL_UC_RSVD_GGTT_PER_FW / SZ_1K);
694 
695 		/* try to find another blob to load */
696 		release_firmware(*fw);
697 		*fw = NULL;
698 		return -ENOENT;
699 	}
700 
701 	return 0;
702 }
703 
704 /**
705  * intel_uc_fw_fetch - fetch uC firmware
706  * @uc_fw: uC firmware
707  *
708  * Fetch uC firmware into GEM obj.
709  *
710  * Return: 0 on success, a negative errno code on failure.
711  */
712 int intel_uc_fw_fetch(struct intel_uc_fw *uc_fw)
713 {
714 	struct intel_gt *gt = __uc_fw_to_gt(uc_fw);
715 	struct drm_i915_private *i915 = gt->i915;
716 	struct intel_uc_fw_file file_ideal;
717 	struct drm_i915_gem_object *obj;
718 	const struct firmware *fw = NULL;
719 	bool old_ver = false;
720 	int err;
721 
722 	GEM_BUG_ON(!gt->wopcm.size);
723 	GEM_BUG_ON(!intel_uc_fw_is_enabled(uc_fw));
724 
725 	err = i915_inject_probe_error(i915, -ENXIO);
726 	if (err)
727 		goto fail;
728 
729 	__force_fw_fetch_failures(uc_fw, -EINVAL);
730 	__force_fw_fetch_failures(uc_fw, -ESTALE);
731 
732 	err = try_firmware_load(uc_fw, &fw);
733 	memcpy(&file_ideal, &uc_fw->file_wanted, sizeof(file_ideal));
734 
735 	/* Any error is terminal if overriding. Don't bother searching for older versions */
736 	if (err && intel_uc_fw_is_overridden(uc_fw))
737 		goto fail;
738 
739 	while (err == -ENOENT) {
740 		old_ver = true;
741 
742 		__uc_fw_auto_select(i915, uc_fw);
743 		if (!uc_fw->file_selected.path) {
744 			/*
745 			 * No more options! But set the path back to something
746 			 * valid just in case it gets dereferenced.
747 			 */
748 			uc_fw->file_selected.path = file_ideal.path;
749 
750 			/* Also, preserve the version that was really wanted */
751 			memcpy(&uc_fw->file_wanted, &file_ideal, sizeof(uc_fw->file_wanted));
752 			break;
753 		}
754 
755 		err = try_firmware_load(uc_fw, &fw);
756 	}
757 
758 	if (err)
759 		goto fail;
760 
761 	err = check_fw_header(gt, fw, uc_fw);
762 	if (err)
763 		goto fail;
764 
765 	if (uc_fw->type == INTEL_UC_FW_TYPE_GUC && !guc_check_version_range(uc_fw))
766 		goto fail;
767 
768 	if (uc_fw->file_wanted.ver.major && uc_fw->file_selected.ver.major) {
769 		/* Check the file's major version was as it claimed */
770 		if (uc_fw->file_selected.ver.major != uc_fw->file_wanted.ver.major) {
771 			drm_notice(&i915->drm, "%s firmware %s: unexpected version: %u.%u != %u.%u\n",
772 				   intel_uc_fw_type_repr(uc_fw->type), uc_fw->file_selected.path,
773 				   uc_fw->file_selected.ver.major, uc_fw->file_selected.ver.minor,
774 				   uc_fw->file_wanted.ver.major, uc_fw->file_wanted.ver.minor);
775 			if (!intel_uc_fw_is_overridden(uc_fw)) {
776 				err = -ENOEXEC;
777 				goto fail;
778 			}
779 		} else {
780 			if (uc_fw->file_selected.ver.minor < uc_fw->file_wanted.ver.minor)
781 				old_ver = true;
782 		}
783 	}
784 
785 	if (old_ver && uc_fw->file_selected.ver.major) {
786 		/* Preserve the version that was really wanted */
787 		memcpy(&uc_fw->file_wanted, &file_ideal, sizeof(uc_fw->file_wanted));
788 
789 		drm_notice(&i915->drm,
790 			   "%s firmware %s (%d.%d) is recommended, but only %s (%d.%d) was found\n",
791 			   intel_uc_fw_type_repr(uc_fw->type),
792 			   uc_fw->file_wanted.path,
793 			   uc_fw->file_wanted.ver.major, uc_fw->file_wanted.ver.minor,
794 			   uc_fw->file_selected.path,
795 			   uc_fw->file_selected.ver.major, uc_fw->file_selected.ver.minor);
796 		drm_info(&i915->drm,
797 			 "Consider updating your linux-firmware pkg or downloading from %s\n",
798 			 INTEL_UC_FIRMWARE_URL);
799 	}
800 
801 	if (HAS_LMEM(i915)) {
802 		obj = i915_gem_object_create_lmem_from_data(i915, fw->data, fw->size);
803 		if (!IS_ERR(obj))
804 			obj->flags |= I915_BO_ALLOC_PM_EARLY;
805 	} else {
806 		obj = i915_gem_object_create_shmem_from_data(i915, fw->data, fw->size);
807 	}
808 
809 	if (IS_ERR(obj)) {
810 		err = PTR_ERR(obj);
811 		goto fail;
812 	}
813 
814 	uc_fw->obj = obj;
815 	uc_fw->size = fw->size;
816 	intel_uc_fw_change_status(uc_fw, INTEL_UC_FIRMWARE_AVAILABLE);
817 
818 	release_firmware(fw);
819 	return 0;
820 
821 fail:
822 	intel_uc_fw_change_status(uc_fw, err == -ENOENT ?
823 				  INTEL_UC_FIRMWARE_MISSING :
824 				  INTEL_UC_FIRMWARE_ERROR);
825 
826 	i915_probe_error(i915, "%s firmware %s: fetch failed with error %d\n",
827 			 intel_uc_fw_type_repr(uc_fw->type), uc_fw->file_selected.path, err);
828 	drm_info(&i915->drm, "%s firmware(s) can be downloaded from %s\n",
829 		 intel_uc_fw_type_repr(uc_fw->type), INTEL_UC_FIRMWARE_URL);
830 
831 	release_firmware(fw);		/* OK even if fw is NULL */
832 	return err;
833 }
834 
835 static u32 uc_fw_ggtt_offset(struct intel_uc_fw *uc_fw)
836 {
837 	struct intel_gt *gt = __uc_fw_to_gt(uc_fw);
838 	struct i915_ggtt *ggtt = gt->ggtt;
839 	struct drm_mm_node *node = &ggtt->uc_fw;
840 	u32 offset = uc_fw->type * INTEL_UC_RSVD_GGTT_PER_FW;
841 
842 	/*
843 	 * The media GT shares the GGTT with the root GT, which means that
844 	 * we need to use different offsets for the binaries on the media GT.
845 	 * To keep the math simple, we use 8MB for the root tile and 8MB for
846 	 * the media one. This will need to be updated if we ever have more
847 	 * than 1 media GT.
848 	 */
849 	BUILD_BUG_ON(INTEL_UC_FW_NUM_TYPES * INTEL_UC_RSVD_GGTT_PER_FW > SZ_8M);
850 	GEM_BUG_ON(gt->type == GT_MEDIA && gt->info.id > 1);
851 	if (gt->type == GT_MEDIA)
852 		offset += SZ_8M;
853 
854 	GEM_BUG_ON(!drm_mm_node_allocated(node));
855 	GEM_BUG_ON(upper_32_bits(node->start));
856 	GEM_BUG_ON(upper_32_bits(node->start + node->size - 1));
857 	GEM_BUG_ON(offset + uc_fw->obj->base.size > node->size);
858 	GEM_BUG_ON(uc_fw->obj->base.size > INTEL_UC_RSVD_GGTT_PER_FW);
859 
860 	return lower_32_bits(node->start + offset);
861 }
862 
863 static void uc_fw_bind_ggtt(struct intel_uc_fw *uc_fw)
864 {
865 	struct drm_i915_gem_object *obj = uc_fw->obj;
866 	struct i915_ggtt *ggtt = __uc_fw_to_gt(uc_fw)->ggtt;
867 	struct i915_vma_resource *dummy = &uc_fw->dummy;
868 	u32 pte_flags = 0;
869 
870 	dummy->start = uc_fw_ggtt_offset(uc_fw);
871 	dummy->node_size = obj->base.size;
872 	dummy->bi.pages = obj->mm.pages;
873 
874 	GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
875 
876 	/* uc_fw->obj cache domains were not controlled across suspend */
877 	if (i915_gem_object_has_struct_page(obj))
878 		drm_clflush_sg(dummy->bi.pages);
879 
880 	if (i915_gem_object_is_lmem(obj))
881 		pte_flags |= PTE_LM;
882 
883 	if (ggtt->vm.raw_insert_entries)
884 		ggtt->vm.raw_insert_entries(&ggtt->vm, dummy, I915_CACHE_NONE, pte_flags);
885 	else
886 		ggtt->vm.insert_entries(&ggtt->vm, dummy, I915_CACHE_NONE, pte_flags);
887 }
888 
889 static void uc_fw_unbind_ggtt(struct intel_uc_fw *uc_fw)
890 {
891 	struct drm_i915_gem_object *obj = uc_fw->obj;
892 	struct i915_ggtt *ggtt = __uc_fw_to_gt(uc_fw)->ggtt;
893 	u64 start = uc_fw_ggtt_offset(uc_fw);
894 
895 	ggtt->vm.clear_range(&ggtt->vm, start, obj->base.size);
896 }
897 
898 static int uc_fw_xfer(struct intel_uc_fw *uc_fw, u32 dst_offset, u32 dma_flags)
899 {
900 	struct intel_gt *gt = __uc_fw_to_gt(uc_fw);
901 	struct intel_uncore *uncore = gt->uncore;
902 	u64 offset;
903 	int ret;
904 
905 	ret = i915_inject_probe_error(gt->i915, -ETIMEDOUT);
906 	if (ret)
907 		return ret;
908 
909 	intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
910 
911 	/* Set the source address for the uCode */
912 	offset = uc_fw_ggtt_offset(uc_fw);
913 	GEM_BUG_ON(upper_32_bits(offset) & 0xFFFF0000);
914 	intel_uncore_write_fw(uncore, DMA_ADDR_0_LOW, lower_32_bits(offset));
915 	intel_uncore_write_fw(uncore, DMA_ADDR_0_HIGH, upper_32_bits(offset));
916 
917 	/* Set the DMA destination */
918 	intel_uncore_write_fw(uncore, DMA_ADDR_1_LOW, dst_offset);
919 	intel_uncore_write_fw(uncore, DMA_ADDR_1_HIGH, DMA_ADDRESS_SPACE_WOPCM);
920 
921 	/*
922 	 * Set the transfer size. The header plus uCode will be copied to WOPCM
923 	 * via DMA, excluding any other components
924 	 */
925 	intel_uncore_write_fw(uncore, DMA_COPY_SIZE,
926 			      sizeof(struct uc_css_header) + uc_fw->ucode_size);
927 
928 	/* Start the DMA */
929 	intel_uncore_write_fw(uncore, DMA_CTRL,
930 			      _MASKED_BIT_ENABLE(dma_flags | START_DMA));
931 
932 	/* Wait for DMA to finish */
933 	ret = intel_wait_for_register_fw(uncore, DMA_CTRL, START_DMA, 0, 100);
934 	if (ret)
935 		drm_err(&gt->i915->drm, "DMA for %s fw failed, DMA_CTRL=%u\n",
936 			intel_uc_fw_type_repr(uc_fw->type),
937 			intel_uncore_read_fw(uncore, DMA_CTRL));
938 
939 	/* Disable the bits once DMA is over */
940 	intel_uncore_write_fw(uncore, DMA_CTRL, _MASKED_BIT_DISABLE(dma_flags));
941 
942 	intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
943 
944 	return ret;
945 }
946 
947 int intel_uc_fw_mark_load_failed(struct intel_uc_fw *uc_fw, int err)
948 {
949 	struct intel_gt *gt = __uc_fw_to_gt(uc_fw);
950 
951 	GEM_BUG_ON(!intel_uc_fw_is_loadable(uc_fw));
952 
953 	i915_probe_error(gt->i915, "Failed to load %s firmware %s (%d)\n",
954 			 intel_uc_fw_type_repr(uc_fw->type), uc_fw->file_selected.path,
955 			 err);
956 	intel_uc_fw_change_status(uc_fw, INTEL_UC_FIRMWARE_LOAD_FAIL);
957 
958 	return err;
959 }
960 
961 /**
962  * intel_uc_fw_upload - load uC firmware using custom loader
963  * @uc_fw: uC firmware
964  * @dst_offset: destination offset
965  * @dma_flags: flags for flags for dma ctrl
966  *
967  * Loads uC firmware and updates internal flags.
968  *
969  * Return: 0 on success, non-zero on failure.
970  */
971 int intel_uc_fw_upload(struct intel_uc_fw *uc_fw, u32 dst_offset, u32 dma_flags)
972 {
973 	struct intel_gt *gt = __uc_fw_to_gt(uc_fw);
974 	int err;
975 
976 	/* make sure the status was cleared the last time we reset the uc */
977 	GEM_BUG_ON(intel_uc_fw_is_loaded(uc_fw));
978 
979 	err = i915_inject_probe_error(gt->i915, -ENOEXEC);
980 	if (err)
981 		return err;
982 
983 	if (!intel_uc_fw_is_loadable(uc_fw))
984 		return -ENOEXEC;
985 
986 	/* Call custom loader */
987 	uc_fw_bind_ggtt(uc_fw);
988 	err = uc_fw_xfer(uc_fw, dst_offset, dma_flags);
989 	uc_fw_unbind_ggtt(uc_fw);
990 	if (err)
991 		goto fail;
992 
993 	intel_uc_fw_change_status(uc_fw, INTEL_UC_FIRMWARE_TRANSFERRED);
994 	return 0;
995 
996 fail:
997 	return intel_uc_fw_mark_load_failed(uc_fw, err);
998 }
999 
1000 static inline bool uc_fw_need_rsa_in_memory(struct intel_uc_fw *uc_fw)
1001 {
1002 	/*
1003 	 * The HW reads the GuC RSA from memory if the key size is > 256 bytes,
1004 	 * while it reads it from the 64 RSA registers if it is smaller.
1005 	 * The HuC RSA is always read from memory.
1006 	 */
1007 	return uc_fw->type == INTEL_UC_FW_TYPE_HUC || uc_fw->rsa_size > 256;
1008 }
1009 
1010 static int uc_fw_rsa_data_create(struct intel_uc_fw *uc_fw)
1011 {
1012 	struct intel_gt *gt = __uc_fw_to_gt(uc_fw);
1013 	struct i915_vma *vma;
1014 	size_t copied;
1015 	void *vaddr;
1016 	int err;
1017 
1018 	err = i915_inject_probe_error(gt->i915, -ENXIO);
1019 	if (err)
1020 		return err;
1021 
1022 	if (!uc_fw_need_rsa_in_memory(uc_fw))
1023 		return 0;
1024 
1025 	/*
1026 	 * uC firmwares will sit above GUC_GGTT_TOP and will not map through
1027 	 * GGTT. Unfortunately, this means that the GuC HW cannot perform the uC
1028 	 * authentication from memory, as the RSA offset now falls within the
1029 	 * GuC inaccessible range. We resort to perma-pinning an additional vma
1030 	 * within the accessible range that only contains the RSA signature.
1031 	 * The GuC HW can use this extra pinning to perform the authentication
1032 	 * since its GGTT offset will be GuC accessible.
1033 	 */
1034 	GEM_BUG_ON(uc_fw->rsa_size > PAGE_SIZE);
1035 	vma = intel_guc_allocate_vma(&gt->uc.guc, PAGE_SIZE);
1036 	if (IS_ERR(vma))
1037 		return PTR_ERR(vma);
1038 
1039 	vaddr = i915_gem_object_pin_map_unlocked(vma->obj,
1040 						 i915_coherent_map_type(gt->i915, vma->obj, true));
1041 	if (IS_ERR(vaddr)) {
1042 		i915_vma_unpin_and_release(&vma, 0);
1043 		err = PTR_ERR(vaddr);
1044 		goto unpin_out;
1045 	}
1046 
1047 	copied = intel_uc_fw_copy_rsa(uc_fw, vaddr, vma->size);
1048 	i915_gem_object_unpin_map(vma->obj);
1049 
1050 	if (copied < uc_fw->rsa_size) {
1051 		err = -ENOMEM;
1052 		goto unpin_out;
1053 	}
1054 
1055 	uc_fw->rsa_data = vma;
1056 
1057 	return 0;
1058 
1059 unpin_out:
1060 	i915_vma_unpin_and_release(&vma, 0);
1061 	return err;
1062 }
1063 
1064 static void uc_fw_rsa_data_destroy(struct intel_uc_fw *uc_fw)
1065 {
1066 	i915_vma_unpin_and_release(&uc_fw->rsa_data, 0);
1067 }
1068 
1069 int intel_uc_fw_init(struct intel_uc_fw *uc_fw)
1070 {
1071 	int err;
1072 
1073 	/* this should happen before the load! */
1074 	GEM_BUG_ON(intel_uc_fw_is_loaded(uc_fw));
1075 
1076 	if (!intel_uc_fw_is_available(uc_fw))
1077 		return -ENOEXEC;
1078 
1079 	err = i915_gem_object_pin_pages_unlocked(uc_fw->obj);
1080 	if (err) {
1081 		DRM_DEBUG_DRIVER("%s fw pin-pages err=%d\n",
1082 				 intel_uc_fw_type_repr(uc_fw->type), err);
1083 		goto out;
1084 	}
1085 
1086 	err = uc_fw_rsa_data_create(uc_fw);
1087 	if (err) {
1088 		DRM_DEBUG_DRIVER("%s fw rsa data creation failed, err=%d\n",
1089 				 intel_uc_fw_type_repr(uc_fw->type), err);
1090 		goto out_unpin;
1091 	}
1092 
1093 	return 0;
1094 
1095 out_unpin:
1096 	i915_gem_object_unpin_pages(uc_fw->obj);
1097 out:
1098 	return err;
1099 }
1100 
1101 void intel_uc_fw_fini(struct intel_uc_fw *uc_fw)
1102 {
1103 	uc_fw_rsa_data_destroy(uc_fw);
1104 
1105 	if (i915_gem_object_has_pinned_pages(uc_fw->obj))
1106 		i915_gem_object_unpin_pages(uc_fw->obj);
1107 
1108 	intel_uc_fw_change_status(uc_fw, INTEL_UC_FIRMWARE_AVAILABLE);
1109 }
1110 
1111 /**
1112  * intel_uc_fw_cleanup_fetch - cleanup uC firmware
1113  * @uc_fw: uC firmware
1114  *
1115  * Cleans up uC firmware by releasing the firmware GEM obj.
1116  */
1117 void intel_uc_fw_cleanup_fetch(struct intel_uc_fw *uc_fw)
1118 {
1119 	if (!intel_uc_fw_is_available(uc_fw))
1120 		return;
1121 
1122 	i915_gem_object_put(fetch_and_zero(&uc_fw->obj));
1123 
1124 	intel_uc_fw_change_status(uc_fw, INTEL_UC_FIRMWARE_SELECTED);
1125 }
1126 
1127 /**
1128  * intel_uc_fw_copy_rsa - copy fw RSA to buffer
1129  *
1130  * @uc_fw: uC firmware
1131  * @dst: dst buffer
1132  * @max_len: max number of bytes to copy
1133  *
1134  * Return: number of copied bytes.
1135  */
1136 size_t intel_uc_fw_copy_rsa(struct intel_uc_fw *uc_fw, void *dst, u32 max_len)
1137 {
1138 	struct intel_memory_region *mr = uc_fw->obj->mm.region;
1139 	u32 size = min_t(u32, uc_fw->rsa_size, max_len);
1140 	u32 offset = sizeof(struct uc_css_header) + uc_fw->ucode_size;
1141 	struct sgt_iter iter;
1142 	size_t count = 0;
1143 	int idx;
1144 
1145 	/* Called during reset handling, must be atomic [no fs_reclaim] */
1146 	GEM_BUG_ON(!intel_uc_fw_is_available(uc_fw));
1147 
1148 	idx = offset >> PAGE_SHIFT;
1149 	offset = offset_in_page(offset);
1150 	if (i915_gem_object_has_struct_page(uc_fw->obj)) {
1151 		struct page *page;
1152 
1153 		for_each_sgt_page(page, iter, uc_fw->obj->mm.pages) {
1154 			u32 len = min_t(u32, size, PAGE_SIZE - offset);
1155 			void *vaddr;
1156 
1157 			if (idx > 0) {
1158 				idx--;
1159 				continue;
1160 			}
1161 
1162 			vaddr = kmap_atomic(page);
1163 			memcpy(dst, vaddr + offset, len);
1164 			kunmap_atomic(vaddr);
1165 
1166 			offset = 0;
1167 			dst += len;
1168 			size -= len;
1169 			count += len;
1170 			if (!size)
1171 				break;
1172 		}
1173 	} else {
1174 		dma_addr_t addr;
1175 
1176 		for_each_sgt_daddr(addr, iter, uc_fw->obj->mm.pages) {
1177 			u32 len = min_t(u32, size, PAGE_SIZE - offset);
1178 			void __iomem *vaddr;
1179 
1180 			if (idx > 0) {
1181 				idx--;
1182 				continue;
1183 			}
1184 
1185 			vaddr = io_mapping_map_atomic_wc(&mr->iomap,
1186 							 addr - mr->region.start);
1187 			memcpy_fromio(dst, vaddr + offset, len);
1188 			io_mapping_unmap_atomic(vaddr);
1189 
1190 			offset = 0;
1191 			dst += len;
1192 			size -= len;
1193 			count += len;
1194 			if (!size)
1195 				break;
1196 		}
1197 	}
1198 
1199 	return count;
1200 }
1201 
1202 /**
1203  * intel_uc_fw_dump - dump information about uC firmware
1204  * @uc_fw: uC firmware
1205  * @p: the &drm_printer
1206  *
1207  * Pretty printer for uC firmware.
1208  */
1209 void intel_uc_fw_dump(const struct intel_uc_fw *uc_fw, struct drm_printer *p)
1210 {
1211 	bool got_wanted;
1212 
1213 	drm_printf(p, "%s firmware: %s\n",
1214 		   intel_uc_fw_type_repr(uc_fw->type), uc_fw->file_selected.path);
1215 	if (uc_fw->file_selected.path != uc_fw->file_wanted.path)
1216 		drm_printf(p, "%s firmware wanted: %s\n",
1217 			   intel_uc_fw_type_repr(uc_fw->type), uc_fw->file_wanted.path);
1218 	drm_printf(p, "\tstatus: %s\n",
1219 		   intel_uc_fw_status_repr(uc_fw->status));
1220 
1221 	if (uc_fw->file_selected.ver.major < uc_fw->file_wanted.ver.major)
1222 		got_wanted = false;
1223 	else if ((uc_fw->file_selected.ver.major == uc_fw->file_wanted.ver.major) &&
1224 		 (uc_fw->file_selected.ver.minor < uc_fw->file_wanted.ver.minor))
1225 		got_wanted = false;
1226 	else if ((uc_fw->file_selected.ver.major == uc_fw->file_wanted.ver.major) &&
1227 		 (uc_fw->file_selected.ver.minor == uc_fw->file_wanted.ver.minor) &&
1228 		 (uc_fw->file_selected.ver.patch < uc_fw->file_wanted.ver.patch))
1229 		got_wanted = false;
1230 	else
1231 		got_wanted = true;
1232 
1233 	if (!got_wanted)
1234 		drm_printf(p, "\tversion: wanted %u.%u.%u, found %u.%u.%u\n",
1235 			   uc_fw->file_wanted.ver.major,
1236 			   uc_fw->file_wanted.ver.minor,
1237 			   uc_fw->file_wanted.ver.patch,
1238 			   uc_fw->file_selected.ver.major,
1239 			   uc_fw->file_selected.ver.minor,
1240 			   uc_fw->file_selected.ver.patch);
1241 	else
1242 		drm_printf(p, "\tversion: found %u.%u.%u\n",
1243 			   uc_fw->file_selected.ver.major,
1244 			   uc_fw->file_selected.ver.minor,
1245 			   uc_fw->file_selected.ver.patch);
1246 	drm_printf(p, "\tuCode: %u bytes\n", uc_fw->ucode_size);
1247 	drm_printf(p, "\tRSA: %u bytes\n", uc_fw->rsa_size);
1248 }
1249