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