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