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