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