1 // SPDX-License-Identifier: MIT
3 * Copyright © 2022 Intel Corporation
6 #include <linux/bitfield.h>
7 #include <linux/fault-inject.h>
8 #include <linux/firmware.h>
10 #include <drm/drm_managed.h>
12 #include "regs/xe_guc_regs.h"
14 #include "xe_device_types.h"
15 #include "xe_force_wake.h"
18 #include "xe_gt_printk.h"
22 #include "xe_module.h"
27 * List of required GuC and HuC binaries per-platform. They must be ordered
28 * based on platform, from newer to older.
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
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).
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.
51 * 1) Platform officially supported by i915 - using Tigerlake as example.
52 * Driver loads the following firmware blobs from disk:
54 * - i915/tgl_guc_<major>.bin
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.
61 * 1) XE_<FUTUREINTELPLATFORM>, still under require_force_probe. Using
62 * "wipplat" as a short-name. Driver loads the following firmware blobs
65 * - xe/wipplat_guc_<major>.<minor>.<patch>.bin
66 * - xe/wipplat_huc_<major>.<minor>.<patch>.bin
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.
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
77 * - xe/plat_guc_<major>.bin
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.
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.
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.
94 enum xe_platform platform;
100 bool full_ver_required;
104 struct fw_blobs_by_type {
105 const struct uc_fw_entry *entries;
109 #define XE_GUC_FIRMWARE_DEFS(fw_def, mmp_ver, major_ver) \
110 fw_def(BATTLEMAGE, major_ver(xe, guc, bmg, 70, 29, 2)) \
111 fw_def(LUNARLAKE, major_ver(xe, guc, lnl, 70, 29, 2)) \
112 fw_def(METEORLAKE, major_ver(i915, guc, mtl, 70, 29, 2)) \
113 fw_def(DG2, major_ver(i915, guc, dg2, 70, 29, 2)) \
114 fw_def(DG1, major_ver(i915, guc, dg1, 70, 29, 2)) \
115 fw_def(ALDERLAKE_N, major_ver(i915, guc, tgl, 70, 29, 2)) \
116 fw_def(ALDERLAKE_P, major_ver(i915, guc, adlp, 70, 29, 2)) \
117 fw_def(ALDERLAKE_S, major_ver(i915, guc, tgl, 70, 29, 2)) \
118 fw_def(ROCKETLAKE, major_ver(i915, guc, tgl, 70, 29, 2)) \
119 fw_def(TIGERLAKE, major_ver(i915, guc, tgl, 70, 29, 2))
121 #define XE_HUC_FIRMWARE_DEFS(fw_def, mmp_ver, no_ver) \
122 fw_def(BATTLEMAGE, no_ver(xe, huc, bmg)) \
123 fw_def(LUNARLAKE, no_ver(xe, huc, lnl)) \
124 fw_def(METEORLAKE, no_ver(i915, huc_gsc, mtl)) \
125 fw_def(DG1, no_ver(i915, huc, dg1)) \
126 fw_def(ALDERLAKE_P, no_ver(i915, huc, tgl)) \
127 fw_def(ALDERLAKE_S, no_ver(i915, huc, tgl)) \
128 fw_def(ROCKETLAKE, no_ver(i915, huc, tgl)) \
129 fw_def(TIGERLAKE, no_ver(i915, huc, tgl))
131 /* for the GSC FW we match the compatibility version and not the release one */
132 #define XE_GSC_FIRMWARE_DEFS(fw_def, major_ver) \
133 fw_def(LUNARLAKE, major_ver(xe, gsc, lnl, 104, 1, 0)) \
134 fw_def(METEORLAKE, major_ver(i915, gsc, mtl, 102, 1, 0))
136 #define MAKE_FW_PATH(dir__, uc__, shortname__, version__) \
137 __stringify(dir__) "/" __stringify(shortname__) "_" __stringify(uc__) version__ ".bin"
139 #define fw_filename_mmp_ver(dir_, uc_, shortname_, a, b, c) \
140 MAKE_FW_PATH(dir_, uc_, shortname_, "_" __stringify(a ## . ## b ## . ## c))
141 #define fw_filename_major_ver(dir_, uc_, shortname_, a, b, c) \
142 MAKE_FW_PATH(dir_, uc_, shortname_, "_" __stringify(a))
143 #define fw_filename_no_ver(dir_, uc_, shortname_) \
144 MAKE_FW_PATH(dir_, uc_, shortname_, "")
145 #define fw_filename_gsc(dir_, uc_, shortname_, a, b, c) \
146 MAKE_FW_PATH(dir_, uc_, shortname_, "_" __stringify(b))
148 #define uc_fw_entry_mmp_ver(dir_, uc_, shortname_, a, b, c) \
149 { fw_filename_mmp_ver(dir_, uc_, shortname_, a, b, c), \
151 #define uc_fw_entry_major_ver(dir_, uc_, shortname_, a, b, c) \
152 { fw_filename_major_ver(dir_, uc_, shortname_, a, b, c), \
154 #define uc_fw_entry_no_ver(dir_, uc_, shortname_) \
155 { fw_filename_no_ver(dir_, uc_, shortname_), \
157 #define uc_fw_entry_gsc(dir_, uc_, shortname_, a, b, c) \
158 { fw_filename_gsc(dir_, uc_, shortname_, a, b, c), \
161 /* All blobs need to be declared via MODULE_FIRMWARE() */
162 #define XE_UC_MODULE_FIRMWARE(platform__, fw_filename) \
163 MODULE_FIRMWARE(fw_filename);
165 #define XE_UC_FW_ENTRY(platform__, entry__) \
167 .platform = XE_ ## platform__, \
171 XE_GUC_FIRMWARE_DEFS(XE_UC_MODULE_FIRMWARE,
172 fw_filename_mmp_ver, fw_filename_major_ver)
173 XE_HUC_FIRMWARE_DEFS(XE_UC_MODULE_FIRMWARE,
174 fw_filename_mmp_ver, fw_filename_no_ver)
175 XE_GSC_FIRMWARE_DEFS(XE_UC_MODULE_FIRMWARE, fw_filename_gsc)
177 static struct xe_gt *
178 __uc_fw_to_gt(struct xe_uc_fw *uc_fw, enum xe_uc_fw_type type)
180 XE_WARN_ON(type >= XE_UC_FW_NUM_TYPES);
183 case XE_UC_FW_TYPE_GUC:
184 return container_of(uc_fw, struct xe_gt, uc.guc.fw);
185 case XE_UC_FW_TYPE_HUC:
186 return container_of(uc_fw, struct xe_gt, uc.huc.fw);
187 case XE_UC_FW_TYPE_GSC:
188 return container_of(uc_fw, struct xe_gt, uc.gsc.fw);
194 static struct xe_gt *uc_fw_to_gt(struct xe_uc_fw *uc_fw)
196 return __uc_fw_to_gt(uc_fw, uc_fw->type);
199 static struct xe_device *uc_fw_to_xe(struct xe_uc_fw *uc_fw)
201 return gt_to_xe(uc_fw_to_gt(uc_fw));
205 uc_fw_auto_select(struct xe_device *xe, struct xe_uc_fw *uc_fw)
207 static const struct uc_fw_entry entries_guc[] = {
208 XE_GUC_FIRMWARE_DEFS(XE_UC_FW_ENTRY,
210 uc_fw_entry_major_ver)
212 static const struct uc_fw_entry entries_huc[] = {
213 XE_HUC_FIRMWARE_DEFS(XE_UC_FW_ENTRY,
217 static const struct uc_fw_entry entries_gsc[] = {
218 XE_GSC_FIRMWARE_DEFS(XE_UC_FW_ENTRY, uc_fw_entry_gsc)
220 static const struct fw_blobs_by_type blobs_all[XE_UC_FW_NUM_TYPES] = {
221 [XE_UC_FW_TYPE_GUC] = { entries_guc, ARRAY_SIZE(entries_guc) },
222 [XE_UC_FW_TYPE_HUC] = { entries_huc, ARRAY_SIZE(entries_huc) },
223 [XE_UC_FW_TYPE_GSC] = { entries_gsc, ARRAY_SIZE(entries_gsc) },
225 static const struct uc_fw_entry *entries;
226 enum xe_platform p = xe->info.platform;
230 xe_assert(xe, uc_fw->type < ARRAY_SIZE(blobs_all));
231 entries = blobs_all[uc_fw->type].entries;
232 count = blobs_all[uc_fw->type].count;
234 for (i = 0; i < count && p <= entries[i].platform; i++) {
235 if (p == entries[i].platform) {
236 uc_fw->path = entries[i].path;
237 uc_fw->versions.wanted.major = entries[i].major;
238 uc_fw->versions.wanted.minor = entries[i].minor;
239 uc_fw->versions.wanted.patch = entries[i].patch;
240 uc_fw->full_ver_required = entries[i].full_ver_required;
242 if (uc_fw->type == XE_UC_FW_TYPE_GSC)
243 uc_fw->versions.wanted_type = XE_UC_FW_VER_COMPATIBILITY;
245 uc_fw->versions.wanted_type = XE_UC_FW_VER_RELEASE;
253 uc_fw_override(struct xe_uc_fw *uc_fw)
255 char *path_override = NULL;
257 /* empty string disables, but it's not allowed for GuC */
258 switch (uc_fw->type) {
259 case XE_UC_FW_TYPE_GUC:
260 if (xe_modparam.guc_firmware_path && *xe_modparam.guc_firmware_path)
261 path_override = xe_modparam.guc_firmware_path;
263 case XE_UC_FW_TYPE_HUC:
264 path_override = xe_modparam.huc_firmware_path;
266 case XE_UC_FW_TYPE_GSC:
267 path_override = xe_modparam.gsc_firmware_path;
274 uc_fw->path = path_override;
275 uc_fw->user_overridden = true;
280 * xe_uc_fw_copy_rsa - copy fw RSA to buffer
282 * @uc_fw: uC firmware
284 * @max_len: max number of bytes to copy
286 * Return: number of copied bytes.
288 size_t xe_uc_fw_copy_rsa(struct xe_uc_fw *uc_fw, void *dst, u32 max_len)
290 struct xe_device *xe = uc_fw_to_xe(uc_fw);
291 u32 size = min_t(u32, uc_fw->rsa_size, max_len);
293 xe_assert(xe, !(size % 4));
294 xe_assert(xe, xe_uc_fw_is_available(uc_fw));
296 xe_map_memcpy_from(xe, dst, &uc_fw->bo->vmap,
297 xe_uc_fw_rsa_offset(uc_fw), size);
302 static void uc_fw_fini(struct drm_device *drm, void *arg)
304 struct xe_uc_fw *uc_fw = arg;
306 if (!xe_uc_fw_is_available(uc_fw))
309 xe_uc_fw_change_status(uc_fw, XE_UC_FIRMWARE_SELECTED);
312 static int guc_read_css_info(struct xe_uc_fw *uc_fw, struct uc_css_header *css)
314 struct xe_gt *gt = uc_fw_to_gt(uc_fw);
315 struct xe_uc_fw_version *release = &uc_fw->versions.found[XE_UC_FW_VER_RELEASE];
316 struct xe_uc_fw_version *compatibility = &uc_fw->versions.found[XE_UC_FW_VER_COMPATIBILITY];
318 xe_gt_assert(gt, uc_fw->type == XE_UC_FW_TYPE_GUC);
320 /* We don't support GuC releases older than 70.29.2 */
321 if (MAKE_GUC_VER_STRUCT(*release) < MAKE_GUC_VER(70, 29, 2)) {
322 xe_gt_err(gt, "Unsupported GuC v%u.%u.%u! v70.29.2 or newer is required\n",
323 release->major, release->minor, release->patch);
327 compatibility->major = FIELD_GET(CSS_SW_VERSION_UC_MAJOR, css->submission_version);
328 compatibility->minor = FIELD_GET(CSS_SW_VERSION_UC_MINOR, css->submission_version);
329 compatibility->patch = FIELD_GET(CSS_SW_VERSION_UC_PATCH, css->submission_version);
331 uc_fw->private_data_size = css->private_data_size;
336 int xe_uc_fw_check_version_requirements(struct xe_uc_fw *uc_fw)
338 struct xe_device *xe = uc_fw_to_xe(uc_fw);
339 struct xe_uc_fw_version *wanted = &uc_fw->versions.wanted;
340 struct xe_uc_fw_version *found = &uc_fw->versions.found[uc_fw->versions.wanted_type];
342 /* Driver has no requirement on any version, any is good. */
347 * If full version is required, both major and minor should match.
348 * Otherwise, at least the major version.
350 if (wanted->major != found->major ||
351 (uc_fw->full_ver_required &&
352 ((wanted->minor != found->minor) ||
353 (wanted->patch != found->patch)))) {
354 drm_notice(&xe->drm, "%s firmware %s: unexpected version: %u.%u.%u != %u.%u.%u\n",
355 xe_uc_fw_type_repr(uc_fw->type), uc_fw->path,
356 found->major, found->minor, found->patch,
357 wanted->major, wanted->minor, wanted->patch);
361 if (wanted->minor > found->minor ||
362 (wanted->minor == found->minor && wanted->patch > found->patch)) {
363 drm_notice(&xe->drm, "%s firmware (%u.%u.%u) is recommended, but only (%u.%u.%u) was found in %s\n",
364 xe_uc_fw_type_repr(uc_fw->type),
365 wanted->major, wanted->minor, wanted->patch,
366 found->major, found->minor, found->patch,
368 drm_info(&xe->drm, "Consider updating your linux-firmware pkg or downloading from %s\n",
375 if (xe_uc_fw_is_overridden(uc_fw))
381 /* Refer to the "CSS-based Firmware Layout" documentation entry for details */
382 static int parse_css_header(struct xe_uc_fw *uc_fw, const void *fw_data, size_t fw_size)
384 struct xe_device *xe = uc_fw_to_xe(uc_fw);
385 struct xe_uc_fw_version *release = &uc_fw->versions.found[XE_UC_FW_VER_RELEASE];
386 struct uc_css_header *css;
389 /* Check the size of the blob before examining buffer contents */
390 if (unlikely(fw_size < sizeof(struct uc_css_header))) {
391 drm_warn(&xe->drm, "%s firmware %s: invalid size: %zu < %zu\n",
392 xe_uc_fw_type_repr(uc_fw->type), uc_fw->path,
393 fw_size, sizeof(struct uc_css_header));
397 css = (struct uc_css_header *)fw_data;
399 /* Check integrity of size values inside CSS header */
400 size = (css->header_size_dw - css->key_size_dw - css->modulus_size_dw -
401 css->exponent_size_dw) * sizeof(u32);
402 if (unlikely(size != sizeof(struct uc_css_header))) {
404 "%s firmware %s: unexpected header size: %zu != %zu\n",
405 xe_uc_fw_type_repr(uc_fw->type), uc_fw->path,
406 fw_size, sizeof(struct uc_css_header));
410 /* uCode size must calculated from other sizes */
411 uc_fw->ucode_size = (css->size_dw - css->header_size_dw) * sizeof(u32);
414 uc_fw->rsa_size = css->key_size_dw * sizeof(u32);
416 /* At least, it should have header, uCode and RSA. Size of all three. */
417 size = sizeof(struct uc_css_header) + uc_fw->ucode_size +
419 if (unlikely(fw_size < size)) {
420 drm_warn(&xe->drm, "%s firmware %s: invalid size: %zu < %zu\n",
421 xe_uc_fw_type_repr(uc_fw->type), uc_fw->path,
426 /* Get version numbers from the CSS header */
427 release->major = FIELD_GET(CSS_SW_VERSION_UC_MAJOR, css->sw_version);
428 release->minor = FIELD_GET(CSS_SW_VERSION_UC_MINOR, css->sw_version);
429 release->patch = FIELD_GET(CSS_SW_VERSION_UC_PATCH, css->sw_version);
431 if (uc_fw->type == XE_UC_FW_TYPE_GUC)
432 return guc_read_css_info(uc_fw, css);
437 static bool is_cpd_header(const void *data)
439 const u32 *marker = data;
441 return *marker == GSC_CPD_HEADER_MARKER;
444 static u32 entry_offset(const struct gsc_cpd_header_v2 *header, const char *name)
446 const struct gsc_cpd_entry *entry;
449 entry = (void *)header + header->header_length;
451 for (i = 0; i < header->num_of_entries; i++, entry++)
452 if (strcmp(entry->name, name) == 0)
453 return entry->offset & GSC_CPD_ENTRY_OFFSET_MASK;
458 /* Refer to the "GSC-based Firmware Layout" documentation entry for details */
459 static int parse_cpd_header(struct xe_uc_fw *uc_fw, const void *data, size_t size,
460 const char *manifest_entry, const char *css_entry)
462 struct xe_gt *gt = uc_fw_to_gt(uc_fw);
463 struct xe_device *xe = gt_to_xe(gt);
464 const struct gsc_cpd_header_v2 *header = data;
465 struct xe_uc_fw_version *release = &uc_fw->versions.found[XE_UC_FW_VER_RELEASE];
466 const struct gsc_manifest_header *manifest;
467 size_t min_size = sizeof(*header);
470 /* manifest_entry is mandatory, css_entry is optional */
471 xe_assert(xe, manifest_entry);
473 if (size < min_size || !is_cpd_header(header))
476 if (header->header_length < sizeof(struct gsc_cpd_header_v2)) {
477 xe_gt_err(gt, "invalid CPD header length %u!\n", header->header_length);
481 min_size = header->header_length + sizeof(struct gsc_cpd_entry) * header->num_of_entries;
482 if (size < min_size) {
483 xe_gt_err(gt, "FW too small! %zu < %zu\n", size, min_size);
487 /* Look for the manifest first */
488 offset = entry_offset(header, manifest_entry);
490 xe_gt_err(gt, "Failed to find %s manifest!\n",
491 xe_uc_fw_type_repr(uc_fw->type));
495 min_size = offset + sizeof(struct gsc_manifest_header);
496 if (size < min_size) {
497 xe_gt_err(gt, "FW too small! %zu < %zu\n", size, min_size);
501 manifest = data + offset;
503 release->major = manifest->fw_version.major;
504 release->minor = manifest->fw_version.minor;
505 release->patch = manifest->fw_version.hotfix;
507 if (uc_fw->type == XE_UC_FW_TYPE_GSC) {
508 struct xe_gsc *gsc = container_of(uc_fw, struct xe_gsc, fw);
510 release->build = manifest->fw_version.build;
511 gsc->security_version = manifest->security_version;
514 /* then optionally look for the css header */
519 * This section does not contain a CSS entry on DG2. We
520 * don't support DG2 HuC right now, so no need to handle
521 * it, just add a reminder in case that changes.
523 xe_assert(xe, xe->info.platform != XE_DG2);
525 offset = entry_offset(header, css_entry);
527 /* the CSS header parser will check that the CSS header fits */
529 xe_gt_err(gt, "FW too small! %zu < %u\n", size, offset);
533 ret = parse_css_header(uc_fw, data + offset, size - offset);
537 uc_fw->css_offset = offset;
540 uc_fw->has_gsc_headers = true;
545 static int parse_gsc_layout(struct xe_uc_fw *uc_fw, const void *data, size_t size)
547 struct xe_gt *gt = uc_fw_to_gt(uc_fw);
548 const struct gsc_layout_pointers *layout = data;
549 const struct gsc_bpdt_header *bpdt_header = NULL;
550 const struct gsc_bpdt_entry *bpdt_entry = NULL;
551 size_t min_size = sizeof(*layout);
554 if (size < min_size) {
555 xe_gt_err(gt, "GSC FW too small! %zu < %zu\n", size, min_size);
559 min_size = layout->boot1.offset + layout->boot1.size;
560 if (size < min_size) {
561 xe_gt_err(gt, "GSC FW too small for boot section! %zu < %zu\n",
566 min_size = sizeof(*bpdt_header);
567 if (layout->boot1.size < min_size) {
568 xe_gt_err(gt, "GSC FW boot section too small for BPDT header: %u < %zu\n",
569 layout->boot1.size, min_size);
573 bpdt_header = data + layout->boot1.offset;
574 if (bpdt_header->signature != GSC_BPDT_HEADER_SIGNATURE) {
575 xe_gt_err(gt, "invalid signature for BPDT header: 0x%08x!\n",
576 bpdt_header->signature);
580 min_size += sizeof(*bpdt_entry) * bpdt_header->descriptor_count;
581 if (layout->boot1.size < min_size) {
582 xe_gt_err(gt, "GSC FW boot section too small for BPDT entries: %u < %zu\n",
583 layout->boot1.size, min_size);
587 bpdt_entry = (void *)bpdt_header + sizeof(*bpdt_header);
588 for (i = 0; i < bpdt_header->descriptor_count; i++, bpdt_entry++) {
589 if ((bpdt_entry->type & GSC_BPDT_ENTRY_TYPE_MASK) !=
590 GSC_BPDT_ENTRY_TYPE_GSC_RBE)
593 min_size = bpdt_entry->sub_partition_offset;
595 /* the CPD header parser will check that the CPD header fits */
596 if (layout->boot1.size < min_size) {
597 xe_gt_err(gt, "GSC FW boot section too small for CPD offset: %u < %zu\n",
598 layout->boot1.size, min_size);
602 return parse_cpd_header(uc_fw,
603 (void *)bpdt_header + min_size,
604 layout->boot1.size - min_size,
608 xe_gt_err(gt, "couldn't find CPD header in GSC binary!\n");
612 static int parse_headers(struct xe_uc_fw *uc_fw, const struct firmware *fw)
617 * All GuC releases and older HuC ones use CSS headers, while newer HuC
618 * releases use GSC CPD headers.
620 switch (uc_fw->type) {
621 case XE_UC_FW_TYPE_GSC:
622 return parse_gsc_layout(uc_fw, fw->data, fw->size);
623 case XE_UC_FW_TYPE_HUC:
624 ret = parse_cpd_header(uc_fw, fw->data, fw->size, "HUCP.man", "huc_fw");
625 if (!ret || ret != -ENOENT)
628 case XE_UC_FW_TYPE_GUC:
629 return parse_css_header(uc_fw, fw->data, fw->size);
637 #define print_uc_fw_version(p_, version_, prefix_, ...) \
639 struct xe_uc_fw_version *ver_ = (version_); \
641 drm_printf(p_, prefix_ " version %u.%u.%u.%u\n", ##__VA_ARGS__, \
642 ver_->major, ver_->minor, \
643 ver_->patch, ver_->build); \
645 drm_printf(p_, prefix_ " version %u.%u.%u\n", ##__VA_ARGS__, \
646 ver_->major, ver_->minor, ver_->patch); \
649 static int uc_fw_request(struct xe_uc_fw *uc_fw, const struct firmware **firmware_p)
651 struct xe_device *xe = uc_fw_to_xe(uc_fw);
652 struct device *dev = xe->drm.dev;
653 struct drm_printer p = drm_info_printer(dev);
654 const struct firmware *fw = NULL;
658 * we use FIRMWARE_UNINITIALIZED to detect checks against uc_fw->status
659 * before we're looked at the HW caps to see if we have uc support
661 BUILD_BUG_ON(XE_UC_FIRMWARE_UNINITIALIZED);
662 xe_assert(xe, !uc_fw->status);
663 xe_assert(xe, !uc_fw->path);
665 uc_fw_auto_select(xe, uc_fw);
667 if (IS_SRIOV_VF(xe)) {
668 /* Only GuC/HuC are supported */
669 if (uc_fw->type != XE_UC_FW_TYPE_GUC &&
670 uc_fw->type != XE_UC_FW_TYPE_HUC)
672 /* VF will support only firmwares that driver can autoselect */
673 xe_uc_fw_change_status(uc_fw, uc_fw->path ?
674 XE_UC_FIRMWARE_PRELOADED :
675 XE_UC_FIRMWARE_NOT_SUPPORTED);
679 uc_fw_override(uc_fw);
681 xe_uc_fw_change_status(uc_fw, uc_fw->path ?
682 XE_UC_FIRMWARE_SELECTED :
683 XE_UC_FIRMWARE_NOT_SUPPORTED);
685 if (!xe_uc_fw_is_supported(uc_fw)) {
686 if (uc_fw->type == XE_UC_FW_TYPE_GUC) {
687 drm_err(&xe->drm, "No GuC firmware defined for platform\n");
693 /* an empty path means the firmware is disabled */
694 if (!xe_device_uc_enabled(xe) || !(*uc_fw->path)) {
695 xe_uc_fw_change_status(uc_fw, XE_UC_FIRMWARE_DISABLED);
696 drm_dbg(&xe->drm, "%s disabled", xe_uc_fw_type_repr(uc_fw->type));
700 err = request_firmware(&fw, uc_fw->path, dev);
704 err = parse_headers(uc_fw, fw);
708 print_uc_fw_version(&p,
709 &uc_fw->versions.found[XE_UC_FW_VER_RELEASE],
710 "Using %s firmware from %s",
711 xe_uc_fw_type_repr(uc_fw->type), uc_fw->path);
713 /* for GSC FW we want the compatibility version, which we query after load */
714 if (uc_fw->type != XE_UC_FW_TYPE_GSC) {
715 err = xe_uc_fw_check_version_requirements(uc_fw);
725 xe_uc_fw_change_status(uc_fw, err == -ENOENT ?
726 XE_UC_FIRMWARE_MISSING :
727 XE_UC_FIRMWARE_ERROR);
729 drm_notice(&xe->drm, "%s firmware %s: fetch failed with error %d\n",
730 xe_uc_fw_type_repr(uc_fw->type), uc_fw->path, err);
731 drm_info(&xe->drm, "%s firmware(s) can be downloaded from %s\n",
732 xe_uc_fw_type_repr(uc_fw->type), XE_UC_FIRMWARE_URL);
734 release_firmware(fw); /* OK even if fw is NULL */
739 static void uc_fw_release(const struct firmware *fw)
741 release_firmware(fw);
744 static int uc_fw_copy(struct xe_uc_fw *uc_fw, const void *data, size_t size, u32 flags)
746 struct xe_device *xe = uc_fw_to_xe(uc_fw);
747 struct xe_gt *gt = uc_fw_to_gt(uc_fw);
748 struct xe_tile *tile = gt_to_tile(gt);
752 obj = xe_managed_bo_create_from_data(xe, tile, data, size, flags);
754 drm_notice(&xe->drm, "%s firmware %s: failed to create / populate bo",
755 xe_uc_fw_type_repr(uc_fw->type), uc_fw->path);
763 xe_uc_fw_change_status(uc_fw, XE_UC_FIRMWARE_AVAILABLE);
765 err = drmm_add_action_or_reset(&xe->drm, uc_fw_fini, uc_fw);
772 xe_uc_fw_change_status(uc_fw, XE_UC_FIRMWARE_ERROR);
773 drm_notice(&xe->drm, "%s firmware %s: copy failed with error %d\n",
774 xe_uc_fw_type_repr(uc_fw->type), uc_fw->path, err);
779 int xe_uc_fw_init(struct xe_uc_fw *uc_fw)
781 const struct firmware *fw = NULL;
784 err = uc_fw_request(uc_fw, &fw);
788 /* no error and no firmware means nothing to copy */
792 err = uc_fw_copy(uc_fw, fw->data, fw->size,
793 XE_BO_FLAG_SYSTEM | XE_BO_FLAG_GGTT |
794 XE_BO_FLAG_GGTT_INVALIDATE);
800 ALLOW_ERROR_INJECTION(xe_uc_fw_init, ERRNO); /* See xe_pci_probe() */
802 static u32 uc_fw_ggtt_offset(struct xe_uc_fw *uc_fw)
804 return xe_bo_ggtt_addr(uc_fw->bo);
807 static int uc_fw_xfer(struct xe_uc_fw *uc_fw, u32 offset, u32 dma_flags)
809 struct xe_device *xe = uc_fw_to_xe(uc_fw);
810 struct xe_gt *gt = uc_fw_to_gt(uc_fw);
811 struct xe_mmio *mmio = >->mmio;
816 xe_force_wake_assert_held(gt_to_fw(gt), XE_FW_GT);
818 /* Set the source address for the uCode */
819 src_offset = uc_fw_ggtt_offset(uc_fw) + uc_fw->css_offset;
820 xe_mmio_write32(mmio, DMA_ADDR_0_LOW, lower_32_bits(src_offset));
821 xe_mmio_write32(mmio, DMA_ADDR_0_HIGH,
822 upper_32_bits(src_offset) | DMA_ADDRESS_SPACE_GGTT);
824 /* Set the DMA destination */
825 xe_mmio_write32(mmio, DMA_ADDR_1_LOW, offset);
826 xe_mmio_write32(mmio, DMA_ADDR_1_HIGH, DMA_ADDRESS_SPACE_WOPCM);
829 * Set the transfer size. The header plus uCode will be copied to WOPCM
830 * via DMA, excluding any other components
832 xe_mmio_write32(mmio, DMA_COPY_SIZE,
833 sizeof(struct uc_css_header) + uc_fw->ucode_size);
836 xe_mmio_write32(mmio, DMA_CTRL,
837 _MASKED_BIT_ENABLE(dma_flags | START_DMA));
839 /* Wait for DMA to finish */
840 ret = xe_mmio_wait32(mmio, DMA_CTRL, START_DMA, 0, 100000, &dma_ctrl,
843 drm_err(&xe->drm, "DMA for %s fw failed, DMA_CTRL=%u\n",
844 xe_uc_fw_type_repr(uc_fw->type), dma_ctrl);
846 /* Disable the bits once DMA is over */
847 xe_mmio_write32(mmio, DMA_CTRL, _MASKED_BIT_DISABLE(dma_flags));
852 int xe_uc_fw_upload(struct xe_uc_fw *uc_fw, u32 offset, u32 dma_flags)
854 struct xe_device *xe = uc_fw_to_xe(uc_fw);
857 /* make sure the status was cleared the last time we reset the uc */
858 xe_assert(xe, !xe_uc_fw_is_loaded(uc_fw));
860 if (!xe_uc_fw_is_loadable(uc_fw))
863 /* Call custom loader */
864 err = uc_fw_xfer(uc_fw, offset, dma_flags);
868 xe_uc_fw_change_status(uc_fw, XE_UC_FIRMWARE_TRANSFERRED);
872 drm_err(&xe->drm, "Failed to load %s firmware %s (%d)\n",
873 xe_uc_fw_type_repr(uc_fw->type), uc_fw->path,
875 xe_uc_fw_change_status(uc_fw, XE_UC_FIRMWARE_LOAD_FAIL);
879 static const char *version_type_repr(enum xe_uc_fw_version_types type)
882 case XE_UC_FW_VER_RELEASE:
884 case XE_UC_FW_VER_COMPATIBILITY:
885 return "compatibility";
887 return "Unknown version type";
891 void xe_uc_fw_print(struct xe_uc_fw *uc_fw, struct drm_printer *p)
895 drm_printf(p, "%s firmware: %s\n",
896 xe_uc_fw_type_repr(uc_fw->type), uc_fw->path);
897 drm_printf(p, "\tstatus: %s\n",
898 xe_uc_fw_status_repr(uc_fw->status));
900 print_uc_fw_version(p, &uc_fw->versions.wanted, "\twanted %s",
901 version_type_repr(uc_fw->versions.wanted_type));
903 for (i = 0; i < XE_UC_FW_VER_TYPE_COUNT; i++) {
904 struct xe_uc_fw_version *ver = &uc_fw->versions.found[i];
907 print_uc_fw_version(p, ver, "\tfound %s",
908 version_type_repr(i));
911 if (uc_fw->ucode_size)
912 drm_printf(p, "\tuCode: %u bytes\n", uc_fw->ucode_size);
914 drm_printf(p, "\tRSA: %u bytes\n", uc_fw->rsa_size);