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