1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2016-2019 Intel Corporation 4 */ 5 6 #include <linux/types.h> 7 8 #include "gt/intel_gt.h" 9 #include "intel_guc_reg.h" 10 #include "intel_huc.h" 11 #include "i915_drv.h" 12 13 #include <linux/device/bus.h> 14 #include <linux/mei_aux.h> 15 16 /** 17 * DOC: HuC 18 * 19 * The HuC is a dedicated microcontroller for usage in media HEVC (High 20 * Efficiency Video Coding) operations. Userspace can directly use the firmware 21 * capabilities by adding HuC specific commands to batch buffers. 22 * 23 * The kernel driver is only responsible for loading the HuC firmware and 24 * triggering its security authentication, which is performed by the GuC on 25 * older platforms and by the GSC on newer ones. For the GuC to correctly 26 * perform the authentication, the HuC binary must be loaded before the GuC one. 27 * Loading the HuC is optional; however, not using the HuC might negatively 28 * impact power usage and/or performance of media workloads, depending on the 29 * use-cases. 30 * HuC must be reloaded on events that cause the WOPCM to lose its contents 31 * (S3/S4, FLR); GuC-authenticated HuC must also be reloaded on GuC/GT reset, 32 * while GSC-managed HuC will survive that. 33 * 34 * See https://github.com/intel/media-driver for the latest details on HuC 35 * functionality. 36 */ 37 38 /** 39 * DOC: HuC Memory Management 40 * 41 * Similarly to the GuC, the HuC can't do any memory allocations on its own, 42 * with the difference being that the allocations for HuC usage are handled by 43 * the userspace driver instead of the kernel one. The HuC accesses the memory 44 * via the PPGTT belonging to the context loaded on the VCS executing the 45 * HuC-specific commands. 46 */ 47 48 /* 49 * MEI-GSC load is an async process. The probing of the exposed aux device 50 * (see intel_gsc.c) usually happens a few seconds after i915 probe, depending 51 * on when the kernel schedules it. Unless something goes terribly wrong, we're 52 * guaranteed for this to happen during boot, so the big timeout is a safety net 53 * that we never expect to need. 54 * MEI-PXP + HuC load usually takes ~300ms, but if the GSC needs to be resumed 55 * and/or reset, this can take longer. Note that the kernel might schedule 56 * other work between the i915 init/resume and the MEI one, which can add to 57 * the delay. 58 */ 59 #define GSC_INIT_TIMEOUT_MS 10000 60 #define PXP_INIT_TIMEOUT_MS 5000 61 62 static int sw_fence_dummy_notify(struct i915_sw_fence *sf, 63 enum i915_sw_fence_notify state) 64 { 65 return NOTIFY_DONE; 66 } 67 68 static void __delayed_huc_load_complete(struct intel_huc *huc) 69 { 70 if (!i915_sw_fence_done(&huc->delayed_load.fence)) 71 i915_sw_fence_complete(&huc->delayed_load.fence); 72 } 73 74 static void delayed_huc_load_complete(struct intel_huc *huc) 75 { 76 hrtimer_cancel(&huc->delayed_load.timer); 77 __delayed_huc_load_complete(huc); 78 } 79 80 static void __gsc_init_error(struct intel_huc *huc) 81 { 82 huc->delayed_load.status = INTEL_HUC_DELAYED_LOAD_ERROR; 83 __delayed_huc_load_complete(huc); 84 } 85 86 static void gsc_init_error(struct intel_huc *huc) 87 { 88 hrtimer_cancel(&huc->delayed_load.timer); 89 __gsc_init_error(huc); 90 } 91 92 static void gsc_init_done(struct intel_huc *huc) 93 { 94 hrtimer_cancel(&huc->delayed_load.timer); 95 96 /* MEI-GSC init is done, now we wait for MEI-PXP to bind */ 97 huc->delayed_load.status = INTEL_HUC_WAITING_ON_PXP; 98 if (!i915_sw_fence_done(&huc->delayed_load.fence)) 99 hrtimer_start(&huc->delayed_load.timer, 100 ms_to_ktime(PXP_INIT_TIMEOUT_MS), 101 HRTIMER_MODE_REL); 102 } 103 104 static enum hrtimer_restart huc_delayed_load_timer_callback(struct hrtimer *hrtimer) 105 { 106 struct intel_huc *huc = container_of(hrtimer, struct intel_huc, delayed_load.timer); 107 108 if (!intel_huc_is_authenticated(huc)) { 109 if (huc->delayed_load.status == INTEL_HUC_WAITING_ON_GSC) 110 drm_notice(&huc_to_gt(huc)->i915->drm, 111 "timed out waiting for MEI GSC init to load HuC\n"); 112 else if (huc->delayed_load.status == INTEL_HUC_WAITING_ON_PXP) 113 drm_notice(&huc_to_gt(huc)->i915->drm, 114 "timed out waiting for MEI PXP init to load HuC\n"); 115 else 116 MISSING_CASE(huc->delayed_load.status); 117 118 __gsc_init_error(huc); 119 } 120 121 return HRTIMER_NORESTART; 122 } 123 124 static void huc_delayed_load_start(struct intel_huc *huc) 125 { 126 ktime_t delay; 127 128 GEM_BUG_ON(intel_huc_is_authenticated(huc)); 129 130 /* 131 * On resume we don't have to wait for MEI-GSC to be re-probed, but we 132 * do need to wait for MEI-PXP to reset & re-bind 133 */ 134 switch (huc->delayed_load.status) { 135 case INTEL_HUC_WAITING_ON_GSC: 136 delay = ms_to_ktime(GSC_INIT_TIMEOUT_MS); 137 break; 138 case INTEL_HUC_WAITING_ON_PXP: 139 delay = ms_to_ktime(PXP_INIT_TIMEOUT_MS); 140 break; 141 default: 142 gsc_init_error(huc); 143 return; 144 } 145 146 /* 147 * This fence is always complete unless we're waiting for the 148 * GSC device to come up to load the HuC. We arm the fence here 149 * and complete it when we confirm that the HuC is loaded from 150 * the PXP bind callback. 151 */ 152 GEM_BUG_ON(!i915_sw_fence_done(&huc->delayed_load.fence)); 153 i915_sw_fence_fini(&huc->delayed_load.fence); 154 i915_sw_fence_reinit(&huc->delayed_load.fence); 155 i915_sw_fence_await(&huc->delayed_load.fence); 156 i915_sw_fence_commit(&huc->delayed_load.fence); 157 158 hrtimer_start(&huc->delayed_load.timer, delay, HRTIMER_MODE_REL); 159 } 160 161 static int gsc_notifier(struct notifier_block *nb, unsigned long action, void *data) 162 { 163 struct device *dev = data; 164 struct intel_huc *huc = container_of(nb, struct intel_huc, delayed_load.nb); 165 struct intel_gsc_intf *intf = &huc_to_gt(huc)->gsc.intf[0]; 166 167 if (!intf->adev || &intf->adev->aux_dev.dev != dev) 168 return 0; 169 170 switch (action) { 171 case BUS_NOTIFY_BOUND_DRIVER: /* mei driver bound to aux device */ 172 gsc_init_done(huc); 173 break; 174 175 case BUS_NOTIFY_DRIVER_NOT_BOUND: /* mei driver fails to be bound */ 176 case BUS_NOTIFY_UNBIND_DRIVER: /* mei driver about to be unbound */ 177 drm_info(&huc_to_gt(huc)->i915->drm, 178 "mei driver not bound, disabling HuC load\n"); 179 gsc_init_error(huc); 180 break; 181 } 182 183 return 0; 184 } 185 186 void intel_huc_register_gsc_notifier(struct intel_huc *huc, struct bus_type *bus) 187 { 188 int ret; 189 190 if (!intel_huc_is_loaded_by_gsc(huc)) 191 return; 192 193 huc->delayed_load.nb.notifier_call = gsc_notifier; 194 ret = bus_register_notifier(bus, &huc->delayed_load.nb); 195 if (ret) { 196 drm_err(&huc_to_gt(huc)->i915->drm, 197 "failed to register GSC notifier\n"); 198 huc->delayed_load.nb.notifier_call = NULL; 199 gsc_init_error(huc); 200 } 201 } 202 203 void intel_huc_unregister_gsc_notifier(struct intel_huc *huc, struct bus_type *bus) 204 { 205 if (!huc->delayed_load.nb.notifier_call) 206 return; 207 208 delayed_huc_load_complete(huc); 209 210 bus_unregister_notifier(bus, &huc->delayed_load.nb); 211 huc->delayed_load.nb.notifier_call = NULL; 212 } 213 214 static void delayed_huc_load_init(struct intel_huc *huc) 215 { 216 /* 217 * Initialize fence to be complete as this is expected to be complete 218 * unless there is a delayed HuC load in progress. 219 */ 220 i915_sw_fence_init(&huc->delayed_load.fence, 221 sw_fence_dummy_notify); 222 i915_sw_fence_commit(&huc->delayed_load.fence); 223 224 hrtimer_init(&huc->delayed_load.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 225 huc->delayed_load.timer.function = huc_delayed_load_timer_callback; 226 } 227 228 static void delayed_huc_load_fini(struct intel_huc *huc) 229 { 230 /* 231 * the fence is initialized in init_early, so we need to clean it up 232 * even if HuC loading is off. 233 */ 234 delayed_huc_load_complete(huc); 235 i915_sw_fence_fini(&huc->delayed_load.fence); 236 } 237 238 int intel_huc_sanitize(struct intel_huc *huc) 239 { 240 delayed_huc_load_complete(huc); 241 intel_uc_fw_sanitize(&huc->fw); 242 return 0; 243 } 244 245 static bool vcs_supported(struct intel_gt *gt) 246 { 247 intel_engine_mask_t mask = gt->info.engine_mask; 248 249 /* 250 * We reach here from i915_driver_early_probe for the primary GT before 251 * its engine mask is set, so we use the device info engine mask for it; 252 * this means we're not taking VCS fusing into account, but if the 253 * primary GT supports VCS engines we expect at least one of them to 254 * remain unfused so we're fine. 255 * For other GTs we expect the GT-specific mask to be set before we 256 * call this function. 257 */ 258 GEM_BUG_ON(!gt_is_root(gt) && !gt->info.engine_mask); 259 260 if (gt_is_root(gt)) 261 mask = RUNTIME_INFO(gt->i915)->platform_engine_mask; 262 else 263 mask = gt->info.engine_mask; 264 265 return __ENGINE_INSTANCES_MASK(mask, VCS0, I915_MAX_VCS); 266 } 267 268 void intel_huc_init_early(struct intel_huc *huc) 269 { 270 struct drm_i915_private *i915 = huc_to_gt(huc)->i915; 271 struct intel_gt *gt = huc_to_gt(huc); 272 273 intel_uc_fw_init_early(&huc->fw, INTEL_UC_FW_TYPE_HUC); 274 275 /* 276 * we always init the fence as already completed, even if HuC is not 277 * supported. This way we don't have to distinguish between HuC not 278 * supported/disabled or already loaded, and can focus on if the load 279 * is currently in progress (fence not complete) or not, which is what 280 * we care about for stalling userspace submissions. 281 */ 282 delayed_huc_load_init(huc); 283 284 if (!vcs_supported(gt)) { 285 intel_uc_fw_change_status(&huc->fw, INTEL_UC_FIRMWARE_NOT_SUPPORTED); 286 return; 287 } 288 289 if (GRAPHICS_VER(i915) >= 11) { 290 huc->status.reg = GEN11_HUC_KERNEL_LOAD_INFO; 291 huc->status.mask = HUC_LOAD_SUCCESSFUL; 292 huc->status.value = HUC_LOAD_SUCCESSFUL; 293 } else { 294 huc->status.reg = HUC_STATUS2; 295 huc->status.mask = HUC_FW_VERIFIED; 296 huc->status.value = HUC_FW_VERIFIED; 297 } 298 } 299 300 #define HUC_LOAD_MODE_STRING(x) (x ? "GSC" : "legacy") 301 static int check_huc_loading_mode(struct intel_huc *huc) 302 { 303 struct intel_gt *gt = huc_to_gt(huc); 304 bool fw_needs_gsc = intel_huc_is_loaded_by_gsc(huc); 305 bool hw_uses_gsc = false; 306 307 /* 308 * The fuse for HuC load via GSC is only valid on platforms that have 309 * GuC deprivilege. 310 */ 311 if (HAS_GUC_DEPRIVILEGE(gt->i915)) 312 hw_uses_gsc = intel_uncore_read(gt->uncore, GUC_SHIM_CONTROL2) & 313 GSC_LOADS_HUC; 314 315 if (fw_needs_gsc != hw_uses_gsc) { 316 drm_err(>->i915->drm, 317 "mismatch between HuC FW (%s) and HW (%s) load modes\n", 318 HUC_LOAD_MODE_STRING(fw_needs_gsc), 319 HUC_LOAD_MODE_STRING(hw_uses_gsc)); 320 return -ENOEXEC; 321 } 322 323 /* make sure we can access the GSC via the mei driver if we need it */ 324 if (!(IS_ENABLED(CONFIG_INTEL_MEI_PXP) && IS_ENABLED(CONFIG_INTEL_MEI_GSC)) && 325 fw_needs_gsc) { 326 drm_info(>->i915->drm, 327 "Can't load HuC due to missing MEI modules\n"); 328 return -EIO; 329 } 330 331 drm_dbg(>->i915->drm, "GSC loads huc=%s\n", str_yes_no(fw_needs_gsc)); 332 333 return 0; 334 } 335 336 int intel_huc_init(struct intel_huc *huc) 337 { 338 struct drm_i915_private *i915 = huc_to_gt(huc)->i915; 339 int err; 340 341 err = check_huc_loading_mode(huc); 342 if (err) 343 goto out; 344 345 err = intel_uc_fw_init(&huc->fw); 346 if (err) 347 goto out; 348 349 intel_uc_fw_change_status(&huc->fw, INTEL_UC_FIRMWARE_LOADABLE); 350 351 return 0; 352 353 out: 354 intel_uc_fw_change_status(&huc->fw, INTEL_UC_FIRMWARE_INIT_FAIL); 355 drm_info(&i915->drm, "HuC init failed with %d\n", err); 356 return err; 357 } 358 359 void intel_huc_fini(struct intel_huc *huc) 360 { 361 /* 362 * the fence is initialized in init_early, so we need to clean it up 363 * even if HuC loading is off. 364 */ 365 delayed_huc_load_fini(huc); 366 367 if (intel_uc_fw_is_loadable(&huc->fw)) 368 intel_uc_fw_fini(&huc->fw); 369 } 370 371 void intel_huc_suspend(struct intel_huc *huc) 372 { 373 if (!intel_uc_fw_is_loadable(&huc->fw)) 374 return; 375 376 /* 377 * in the unlikely case that we're suspending before the GSC has 378 * completed its loading sequence, just stop waiting. We'll restart 379 * on resume. 380 */ 381 delayed_huc_load_complete(huc); 382 } 383 384 int intel_huc_wait_for_auth_complete(struct intel_huc *huc) 385 { 386 struct intel_gt *gt = huc_to_gt(huc); 387 int ret; 388 389 ret = __intel_wait_for_register(gt->uncore, 390 huc->status.reg, 391 huc->status.mask, 392 huc->status.value, 393 2, 50, NULL); 394 395 /* mark the load process as complete even if the wait failed */ 396 delayed_huc_load_complete(huc); 397 398 if (ret) { 399 drm_err(>->i915->drm, "HuC: Firmware not verified %d\n", ret); 400 intel_uc_fw_change_status(&huc->fw, INTEL_UC_FIRMWARE_LOAD_FAIL); 401 return ret; 402 } 403 404 intel_uc_fw_change_status(&huc->fw, INTEL_UC_FIRMWARE_RUNNING); 405 drm_info(>->i915->drm, "HuC authenticated\n"); 406 return 0; 407 } 408 409 /** 410 * intel_huc_auth() - Authenticate HuC uCode 411 * @huc: intel_huc structure 412 * 413 * Called after HuC and GuC firmware loading during intel_uc_init_hw(). 414 * 415 * This function invokes the GuC action to authenticate the HuC firmware, 416 * passing the offset of the RSA signature to intel_guc_auth_huc(). It then 417 * waits for up to 50ms for firmware verification ACK. 418 */ 419 int intel_huc_auth(struct intel_huc *huc) 420 { 421 struct intel_gt *gt = huc_to_gt(huc); 422 struct intel_guc *guc = >->uc.guc; 423 int ret; 424 425 if (!intel_uc_fw_is_loaded(&huc->fw)) 426 return -ENOEXEC; 427 428 /* GSC will do the auth */ 429 if (intel_huc_is_loaded_by_gsc(huc)) 430 return -ENODEV; 431 432 ret = i915_inject_probe_error(gt->i915, -ENXIO); 433 if (ret) 434 goto fail; 435 436 GEM_BUG_ON(intel_uc_fw_is_running(&huc->fw)); 437 438 ret = intel_guc_auth_huc(guc, intel_guc_ggtt_offset(guc, huc->fw.rsa_data)); 439 if (ret) { 440 DRM_ERROR("HuC: GuC did not ack Auth request %d\n", ret); 441 goto fail; 442 } 443 444 /* Check authentication status, it should be done by now */ 445 ret = intel_huc_wait_for_auth_complete(huc); 446 if (ret) 447 goto fail; 448 449 return 0; 450 451 fail: 452 i915_probe_error(gt->i915, "HuC: Authentication failed %d\n", ret); 453 return ret; 454 } 455 456 bool intel_huc_is_authenticated(struct intel_huc *huc) 457 { 458 struct intel_gt *gt = huc_to_gt(huc); 459 intel_wakeref_t wakeref; 460 u32 status = 0; 461 462 with_intel_runtime_pm(gt->uncore->rpm, wakeref) 463 status = intel_uncore_read(gt->uncore, huc->status.reg); 464 465 return (status & huc->status.mask) == huc->status.value; 466 } 467 468 /** 469 * intel_huc_check_status() - check HuC status 470 * @huc: intel_huc structure 471 * 472 * This function reads status register to verify if HuC 473 * firmware was successfully loaded. 474 * 475 * The return values match what is expected for the I915_PARAM_HUC_STATUS 476 * getparam. 477 */ 478 int intel_huc_check_status(struct intel_huc *huc) 479 { 480 switch (__intel_uc_fw_status(&huc->fw)) { 481 case INTEL_UC_FIRMWARE_NOT_SUPPORTED: 482 return -ENODEV; 483 case INTEL_UC_FIRMWARE_DISABLED: 484 return -EOPNOTSUPP; 485 case INTEL_UC_FIRMWARE_MISSING: 486 return -ENOPKG; 487 case INTEL_UC_FIRMWARE_ERROR: 488 return -ENOEXEC; 489 case INTEL_UC_FIRMWARE_INIT_FAIL: 490 return -ENOMEM; 491 case INTEL_UC_FIRMWARE_LOAD_FAIL: 492 return -EIO; 493 default: 494 break; 495 } 496 497 return intel_huc_is_authenticated(huc); 498 } 499 500 static bool huc_has_delayed_load(struct intel_huc *huc) 501 { 502 return intel_huc_is_loaded_by_gsc(huc) && 503 (huc->delayed_load.status != INTEL_HUC_DELAYED_LOAD_ERROR); 504 } 505 506 void intel_huc_update_auth_status(struct intel_huc *huc) 507 { 508 if (!intel_uc_fw_is_loadable(&huc->fw)) 509 return; 510 511 if (intel_huc_is_authenticated(huc)) 512 intel_uc_fw_change_status(&huc->fw, 513 INTEL_UC_FIRMWARE_RUNNING); 514 else if (huc_has_delayed_load(huc)) 515 huc_delayed_load_start(huc); 516 } 517 518 /** 519 * intel_huc_load_status - dump information about HuC load status 520 * @huc: the HuC 521 * @p: the &drm_printer 522 * 523 * Pretty printer for HuC load status. 524 */ 525 void intel_huc_load_status(struct intel_huc *huc, struct drm_printer *p) 526 { 527 struct intel_gt *gt = huc_to_gt(huc); 528 intel_wakeref_t wakeref; 529 530 if (!intel_huc_is_supported(huc)) { 531 drm_printf(p, "HuC not supported\n"); 532 return; 533 } 534 535 if (!intel_huc_is_wanted(huc)) { 536 drm_printf(p, "HuC disabled\n"); 537 return; 538 } 539 540 intel_uc_fw_dump(&huc->fw, p); 541 542 with_intel_runtime_pm(gt->uncore->rpm, wakeref) 543 drm_printf(p, "HuC status: 0x%08x\n", 544 intel_uncore_read(gt->uncore, huc->status.reg)); 545 } 546