1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2022 Intel Corporation 4 */ 5 6 #include "gem/i915_gem_lmem.h" 7 #include "gt/intel_engine_pm.h" 8 #include "gt/intel_gpu_commands.h" 9 #include "gt/intel_gt.h" 10 #include "gt/intel_gt_print.h" 11 #include "gt/intel_ring.h" 12 #include "intel_gsc_binary_headers.h" 13 #include "intel_gsc_fw.h" 14 #include "intel_gsc_uc_heci_cmd_submit.h" 15 #include "i915_reg.h" 16 17 static bool gsc_is_in_reset(struct intel_uncore *uncore) 18 { 19 u32 fw_status = intel_uncore_read(uncore, HECI_FWSTS(MTL_GSC_HECI1_BASE, 1)); 20 21 return REG_FIELD_GET(HECI1_FWSTS1_CURRENT_STATE, fw_status) == 22 HECI1_FWSTS1_CURRENT_STATE_RESET; 23 } 24 25 static u32 gsc_uc_get_fw_status(struct intel_uncore *uncore, bool needs_wakeref) 26 { 27 intel_wakeref_t wakeref; 28 u32 fw_status = 0; 29 30 if (needs_wakeref) 31 wakeref = intel_runtime_pm_get(uncore->rpm); 32 33 fw_status = intel_uncore_read(uncore, HECI_FWSTS(MTL_GSC_HECI1_BASE, 1)); 34 35 if (needs_wakeref) 36 intel_runtime_pm_put(uncore->rpm, wakeref); 37 return fw_status; 38 } 39 40 bool intel_gsc_uc_fw_proxy_init_done(struct intel_gsc_uc *gsc, bool needs_wakeref) 41 { 42 return REG_FIELD_GET(HECI1_FWSTS1_CURRENT_STATE, 43 gsc_uc_get_fw_status(gsc_uc_to_gt(gsc)->uncore, 44 needs_wakeref)) == 45 HECI1_FWSTS1_PROXY_STATE_NORMAL; 46 } 47 48 int intel_gsc_uc_fw_proxy_get_status(struct intel_gsc_uc *gsc) 49 { 50 if (!(IS_ENABLED(CONFIG_INTEL_MEI_GSC_PROXY))) 51 return -ENODEV; 52 if (!intel_uc_fw_is_loadable(&gsc->fw)) 53 return -ENODEV; 54 if (__intel_uc_fw_status(&gsc->fw) == INTEL_UC_FIRMWARE_LOAD_FAIL) 55 return -ENOLINK; 56 if (!intel_gsc_uc_fw_proxy_init_done(gsc, true)) 57 return -EAGAIN; 58 59 return 0; 60 } 61 62 bool intel_gsc_uc_fw_init_done(struct intel_gsc_uc *gsc) 63 { 64 return gsc_uc_get_fw_status(gsc_uc_to_gt(gsc)->uncore, false) & 65 HECI1_FWSTS1_INIT_COMPLETE; 66 } 67 68 static inline u32 cpd_entry_offset(const struct intel_gsc_cpd_entry *entry) 69 { 70 return entry->offset & INTEL_GSC_CPD_ENTRY_OFFSET_MASK; 71 } 72 73 int intel_gsc_fw_get_binary_info(struct intel_uc_fw *gsc_fw, const void *data, size_t size) 74 { 75 struct intel_gsc_uc *gsc = container_of(gsc_fw, struct intel_gsc_uc, fw); 76 struct intel_gt *gt = gsc_uc_to_gt(gsc); 77 const struct intel_gsc_layout_pointers *layout = data; 78 const struct intel_gsc_bpdt_header *bpdt_header = NULL; 79 const struct intel_gsc_bpdt_entry *bpdt_entry = NULL; 80 const struct intel_gsc_cpd_header_v2 *cpd_header = NULL; 81 const struct intel_gsc_cpd_entry *cpd_entry = NULL; 82 const struct intel_gsc_manifest_header *manifest; 83 size_t min_size = sizeof(*layout); 84 int i; 85 86 if (size < min_size) { 87 gt_err(gt, "GSC FW too small! %zu < %zu\n", size, min_size); 88 return -ENODATA; 89 } 90 91 /* 92 * The GSC binary starts with the pointer layout, which contains the 93 * locations of the various partitions of the binary. The one we're 94 * interested in to get the version is the boot1 partition, where we can 95 * find a BPDT header followed by entries, one of which points to the 96 * RBE sub-section of the partition. From here, we can parse the CPD 97 * header and the following entries to find the manifest location 98 * (entry identified by the "RBEP.man" name), from which we can finally 99 * extract the version. 100 * 101 * -------------------------------------------------- 102 * [ intel_gsc_layout_pointers ] 103 * [ ... ] 104 * [ boot1.offset >---------------------------]------o 105 * [ ... ] | 106 * -------------------------------------------------- | 107 * | 108 * -------------------------------------------------- | 109 * [ intel_gsc_bpdt_header ]<-----o 110 * -------------------------------------------------- 111 * [ intel_gsc_bpdt_entry[] ] 112 * [ entry1 ] 113 * [ ... ] 114 * [ entryX ] 115 * [ type == GSC_RBE ] 116 * [ offset >-----------------------------]------o 117 * [ ... ] | 118 * -------------------------------------------------- | 119 * | 120 * -------------------------------------------------- | 121 * [ intel_gsc_cpd_header_v2 ]<-----o 122 * -------------------------------------------------- 123 * [ intel_gsc_cpd_entry[] ] 124 * [ entry1 ] 125 * [ ... ] 126 * [ entryX ] 127 * [ "RBEP.man" ] 128 * [ ... ] 129 * [ offset >----------------------------]------o 130 * [ ... ] | 131 * -------------------------------------------------- | 132 * | 133 * -------------------------------------------------- | 134 * [ intel_gsc_manifest_header ]<-----o 135 * [ ... ] 136 * [ intel_gsc_version fw_version ] 137 * [ ... ] 138 * -------------------------------------------------- 139 */ 140 141 min_size = layout->boot1.offset + layout->boot1.size; 142 if (size < min_size) { 143 gt_err(gt, "GSC FW too small for boot section! %zu < %zu\n", 144 size, min_size); 145 return -ENODATA; 146 } 147 148 min_size = sizeof(*bpdt_header); 149 if (layout->boot1.size < min_size) { 150 gt_err(gt, "GSC FW boot section too small for BPDT header: %u < %zu\n", 151 layout->boot1.size, min_size); 152 return -ENODATA; 153 } 154 155 bpdt_header = data + layout->boot1.offset; 156 if (bpdt_header->signature != INTEL_GSC_BPDT_HEADER_SIGNATURE) { 157 gt_err(gt, "invalid signature for BPDT header: 0x%08x!\n", 158 bpdt_header->signature); 159 return -EINVAL; 160 } 161 162 min_size += sizeof(*bpdt_entry) * bpdt_header->descriptor_count; 163 if (layout->boot1.size < min_size) { 164 gt_err(gt, "GSC FW boot section too small for BPDT entries: %u < %zu\n", 165 layout->boot1.size, min_size); 166 return -ENODATA; 167 } 168 169 bpdt_entry = (void *)bpdt_header + sizeof(*bpdt_header); 170 for (i = 0; i < bpdt_header->descriptor_count; i++, bpdt_entry++) { 171 if ((bpdt_entry->type & INTEL_GSC_BPDT_ENTRY_TYPE_MASK) != 172 INTEL_GSC_BPDT_ENTRY_TYPE_GSC_RBE) 173 continue; 174 175 cpd_header = (void *)bpdt_header + bpdt_entry->sub_partition_offset; 176 min_size = bpdt_entry->sub_partition_offset + sizeof(*cpd_header); 177 break; 178 } 179 180 if (!cpd_header) { 181 gt_err(gt, "couldn't find CPD header in GSC binary!\n"); 182 return -ENODATA; 183 } 184 185 if (layout->boot1.size < min_size) { 186 gt_err(gt, "GSC FW boot section too small for CPD header: %u < %zu\n", 187 layout->boot1.size, min_size); 188 return -ENODATA; 189 } 190 191 if (cpd_header->header_marker != INTEL_GSC_CPD_HEADER_MARKER) { 192 gt_err(gt, "invalid marker for CPD header in GSC bin: 0x%08x!\n", 193 cpd_header->header_marker); 194 return -EINVAL; 195 } 196 197 min_size += sizeof(*cpd_entry) * cpd_header->num_of_entries; 198 if (layout->boot1.size < min_size) { 199 gt_err(gt, "GSC FW boot section too small for CPD entries: %u < %zu\n", 200 layout->boot1.size, min_size); 201 return -ENODATA; 202 } 203 204 cpd_entry = (void *)cpd_header + cpd_header->header_length; 205 for (i = 0; i < cpd_header->num_of_entries; i++, cpd_entry++) { 206 if (strcmp(cpd_entry->name, "RBEP.man") == 0) { 207 manifest = (void *)cpd_header + cpd_entry_offset(cpd_entry); 208 intel_uc_fw_version_from_gsc_manifest(&gsc->release, 209 manifest); 210 gsc->security_version = manifest->security_version; 211 break; 212 } 213 } 214 215 return 0; 216 } 217 218 static int emit_gsc_fw_load(struct i915_request *rq, struct intel_gsc_uc *gsc) 219 { 220 u32 offset = i915_ggtt_offset(gsc->local); 221 u32 *cs; 222 223 cs = intel_ring_begin(rq, 4); 224 if (IS_ERR(cs)) 225 return PTR_ERR(cs); 226 227 *cs++ = GSC_FW_LOAD; 228 *cs++ = lower_32_bits(offset); 229 *cs++ = upper_32_bits(offset); 230 *cs++ = (gsc->local->size / SZ_4K) | HECI1_FW_LIMIT_VALID; 231 232 intel_ring_advance(rq, cs); 233 234 return 0; 235 } 236 237 static int gsc_fw_load(struct intel_gsc_uc *gsc) 238 { 239 struct intel_context *ce = gsc->ce; 240 struct i915_request *rq; 241 int err; 242 243 if (!ce) 244 return -ENODEV; 245 246 rq = i915_request_create(ce); 247 if (IS_ERR(rq)) 248 return PTR_ERR(rq); 249 250 if (ce->engine->emit_init_breadcrumb) { 251 err = ce->engine->emit_init_breadcrumb(rq); 252 if (err) 253 goto out_rq; 254 } 255 256 err = emit_gsc_fw_load(rq, gsc); 257 if (err) 258 goto out_rq; 259 260 err = ce->engine->emit_flush(rq, 0); 261 262 out_rq: 263 i915_request_get(rq); 264 265 if (unlikely(err)) 266 i915_request_set_error_once(rq, err); 267 268 i915_request_add(rq); 269 270 if (!err && i915_request_wait(rq, 0, msecs_to_jiffies(500)) < 0) 271 err = -ETIME; 272 273 i915_request_put(rq); 274 275 if (err) 276 gt_err(gsc_uc_to_gt(gsc), "Request submission for GSC load failed %pe\n", 277 ERR_PTR(err)); 278 279 return err; 280 } 281 282 static int gsc_fw_load_prepare(struct intel_gsc_uc *gsc) 283 { 284 struct intel_gt *gt = gsc_uc_to_gt(gsc); 285 void *src; 286 287 if (!gsc->local) 288 return -ENODEV; 289 290 if (gsc->local->size < gsc->fw.size) 291 return -ENOSPC; 292 293 src = i915_gem_object_pin_map_unlocked(gsc->fw.obj, 294 intel_gt_coherent_map_type(gt, gsc->fw.obj, true)); 295 if (IS_ERR(src)) 296 return PTR_ERR(src); 297 298 memcpy_toio(gsc->local_vaddr, src, gsc->fw.size); 299 memset_io(gsc->local_vaddr + gsc->fw.size, 0, gsc->local->size - gsc->fw.size); 300 301 /* 302 * Wa_22016122933: Making sure the data in dst is 303 * visible to GSC right away 304 */ 305 intel_guc_write_barrier(>->uc.guc); 306 307 i915_gem_object_unpin_map(gsc->fw.obj); 308 309 return 0; 310 } 311 312 static int gsc_fw_wait(struct intel_gt *gt) 313 { 314 return intel_wait_for_register(gt->uncore, 315 HECI_FWSTS(MTL_GSC_HECI1_BASE, 1), 316 HECI1_FWSTS1_INIT_COMPLETE, 317 HECI1_FWSTS1_INIT_COMPLETE, 318 500); 319 } 320 321 struct intel_gsc_mkhi_header { 322 u8 group_id; 323 #define MKHI_GROUP_ID_GFX_SRV 0x30 324 325 u8 command; 326 #define MKHI_GFX_SRV_GET_HOST_COMPATIBILITY_VERSION (0x42) 327 328 u8 reserved; 329 u8 result; 330 } __packed; 331 332 struct mtl_gsc_ver_msg_in { 333 struct intel_gsc_mtl_header header; 334 struct intel_gsc_mkhi_header mkhi; 335 } __packed; 336 337 struct mtl_gsc_ver_msg_out { 338 struct intel_gsc_mtl_header header; 339 struct intel_gsc_mkhi_header mkhi; 340 u16 proj_major; 341 u16 compat_major; 342 u16 compat_minor; 343 u16 reserved[5]; 344 } __packed; 345 346 #define GSC_VER_PKT_SZ SZ_4K 347 348 static int gsc_fw_query_compatibility_version(struct intel_gsc_uc *gsc) 349 { 350 struct intel_gt *gt = gsc_uc_to_gt(gsc); 351 struct mtl_gsc_ver_msg_in *msg_in; 352 struct mtl_gsc_ver_msg_out *msg_out; 353 struct i915_vma *vma; 354 u64 offset; 355 void *vaddr; 356 int err; 357 358 err = intel_guc_allocate_and_map_vma(>->uc.guc, GSC_VER_PKT_SZ * 2, 359 &vma, &vaddr); 360 if (err) { 361 gt_err(gt, "failed to allocate vma for GSC version query\n"); 362 return err; 363 } 364 365 offset = i915_ggtt_offset(vma); 366 msg_in = vaddr; 367 msg_out = vaddr + GSC_VER_PKT_SZ; 368 369 intel_gsc_uc_heci_cmd_emit_mtl_header(&msg_in->header, 370 HECI_MEADDRESS_MKHI, 371 sizeof(*msg_in), 0); 372 msg_in->mkhi.group_id = MKHI_GROUP_ID_GFX_SRV; 373 msg_in->mkhi.command = MKHI_GFX_SRV_GET_HOST_COMPATIBILITY_VERSION; 374 375 err = intel_gsc_uc_heci_cmd_submit_packet(>->uc.gsc, 376 offset, 377 sizeof(*msg_in), 378 offset + GSC_VER_PKT_SZ, 379 GSC_VER_PKT_SZ); 380 if (err) { 381 gt_err(gt, 382 "failed to submit GSC request for compatibility version: %d\n", 383 err); 384 goto out_vma; 385 } 386 387 if (msg_out->header.message_size != sizeof(*msg_out)) { 388 gt_err(gt, "invalid GSC reply length %u [expected %zu], s=0x%x, f=0x%x, r=0x%x\n", 389 msg_out->header.message_size, sizeof(*msg_out), 390 msg_out->header.status, msg_out->header.flags, msg_out->mkhi.result); 391 err = -EPROTO; 392 goto out_vma; 393 } 394 395 gsc->fw.file_selected.ver.major = msg_out->compat_major; 396 gsc->fw.file_selected.ver.minor = msg_out->compat_minor; 397 398 out_vma: 399 i915_vma_unpin_and_release(&vma, I915_VMA_RELEASE_MAP); 400 return err; 401 } 402 403 int intel_gsc_uc_fw_upload(struct intel_gsc_uc *gsc) 404 { 405 struct intel_gt *gt = gsc_uc_to_gt(gsc); 406 struct intel_uc_fw *gsc_fw = &gsc->fw; 407 int err; 408 409 /* check current fw status */ 410 if (intel_gsc_uc_fw_init_done(gsc)) { 411 if (GEM_WARN_ON(!intel_uc_fw_is_loaded(gsc_fw))) 412 intel_uc_fw_change_status(gsc_fw, INTEL_UC_FIRMWARE_TRANSFERRED); 413 return -EEXIST; 414 } 415 416 if (!intel_uc_fw_is_loadable(gsc_fw)) 417 return -ENOEXEC; 418 419 /* FW blob is ok, so clean the status */ 420 intel_uc_fw_sanitize(&gsc->fw); 421 422 if (!gsc_is_in_reset(gt->uncore)) 423 return -EIO; 424 425 err = gsc_fw_load_prepare(gsc); 426 if (err) 427 goto fail; 428 429 /* 430 * GSC is only killed by an FLR, so we need to trigger one on unload to 431 * make sure we stop it. This is because we assign a chunk of memory to 432 * the GSC as part of the FW load , so we need to make sure it stops 433 * using it when we release it to the system on driver unload. Note that 434 * this is not a problem of the unload per-se, because the GSC will not 435 * touch that memory unless there are requests for it coming from the 436 * driver; therefore, no accesses will happen while i915 is not loaded, 437 * but if we re-load the driver then the GSC might wake up and try to 438 * access that old memory location again. 439 * Given that an FLR is a very disruptive action (see the FLR function 440 * for details), we want to do it as the last action before releasing 441 * the access to the MMIO bar, which means we need to do it as part of 442 * the primary uncore cleanup. 443 * An alternative approach to the FLR would be to use a memory location 444 * that survives driver unload, like e.g. stolen memory, and keep the 445 * GSC loaded across reloads. However, this requires us to make sure we 446 * preserve that memory location on unload and then determine and 447 * reserve its offset on each subsequent load, which is not trivial, so 448 * it is easier to just kill everything and start fresh. 449 */ 450 intel_uncore_set_flr_on_fini(>->i915->uncore); 451 452 err = gsc_fw_load(gsc); 453 if (err) 454 goto fail; 455 456 err = gsc_fw_wait(gt); 457 if (err) 458 goto fail; 459 460 err = gsc_fw_query_compatibility_version(gsc); 461 if (err) 462 goto fail; 463 464 /* we only support compatibility version 1.0 at the moment */ 465 err = intel_uc_check_file_version(gsc_fw, NULL); 466 if (err) 467 goto fail; 468 469 /* FW is not fully operational until we enable SW proxy */ 470 intel_uc_fw_change_status(gsc_fw, INTEL_UC_FIRMWARE_TRANSFERRED); 471 472 gt_info(gt, "Loaded GSC firmware %s (cv%u.%u, r%u.%u.%u.%u, svn %u)\n", 473 gsc_fw->file_selected.path, 474 gsc_fw->file_selected.ver.major, gsc_fw->file_selected.ver.minor, 475 gsc->release.major, gsc->release.minor, 476 gsc->release.patch, gsc->release.build, 477 gsc->security_version); 478 479 return 0; 480 481 fail: 482 return intel_uc_fw_mark_load_failed(gsc_fw, err); 483 } 484