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