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