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