1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2016-2019 Intel Corporation 4 */ 5 6 #include "gt/intel_gt.h" 7 #include "gt/intel_reset.h" 8 #include "intel_guc.h" 9 #include "intel_guc_ads.h" 10 #include "intel_guc_submission.h" 11 #include "intel_uc.h" 12 13 #include "i915_drv.h" 14 15 static const struct intel_uc_ops uc_ops_off; 16 static const struct intel_uc_ops uc_ops_on; 17 18 /* Reset GuC providing us with fresh state for both GuC and HuC. 19 */ 20 static int __intel_uc_reset_hw(struct intel_uc *uc) 21 { 22 struct intel_gt *gt = uc_to_gt(uc); 23 int ret; 24 u32 guc_status; 25 26 ret = i915_inject_probe_error(gt->i915, -ENXIO); 27 if (ret) 28 return ret; 29 30 ret = intel_reset_guc(gt); 31 if (ret) { 32 DRM_ERROR("Failed to reset GuC, ret = %d\n", ret); 33 return ret; 34 } 35 36 guc_status = intel_uncore_read(gt->uncore, GUC_STATUS); 37 WARN(!(guc_status & GS_MIA_IN_RESET), 38 "GuC status: 0x%x, MIA core expected to be in reset\n", 39 guc_status); 40 41 return ret; 42 } 43 44 static void __confirm_options(struct intel_uc *uc) 45 { 46 struct drm_i915_private *i915 = uc_to_gt(uc)->i915; 47 48 drm_dbg(&i915->drm, 49 "enable_guc=%d (guc:%s submission:%s huc:%s)\n", 50 i915->params.enable_guc, 51 yesno(intel_uc_wants_guc(uc)), 52 yesno(intel_uc_wants_guc_submission(uc)), 53 yesno(intel_uc_wants_huc(uc))); 54 55 if (i915->params.enable_guc == -1) 56 return; 57 58 if (i915->params.enable_guc == 0) { 59 GEM_BUG_ON(intel_uc_wants_guc(uc)); 60 GEM_BUG_ON(intel_uc_wants_guc_submission(uc)); 61 GEM_BUG_ON(intel_uc_wants_huc(uc)); 62 return; 63 } 64 65 if (!intel_uc_supports_guc(uc)) 66 drm_info(&i915->drm, 67 "Incompatible option enable_guc=%d - %s\n", 68 i915->params.enable_guc, "GuC is not supported!"); 69 70 if (i915->params.enable_guc & ENABLE_GUC_LOAD_HUC && 71 !intel_uc_supports_huc(uc)) 72 drm_info(&i915->drm, 73 "Incompatible option enable_guc=%d - %s\n", 74 i915->params.enable_guc, "HuC is not supported!"); 75 76 if (i915->params.enable_guc & ENABLE_GUC_SUBMISSION && 77 !intel_uc_supports_guc_submission(uc)) 78 drm_info(&i915->drm, 79 "Incompatible option enable_guc=%d - %s\n", 80 i915->params.enable_guc, "GuC submission is N/A"); 81 82 if (i915->params.enable_guc & ~(ENABLE_GUC_SUBMISSION | 83 ENABLE_GUC_LOAD_HUC)) 84 drm_info(&i915->drm, 85 "Incompatible option enable_guc=%d - %s\n", 86 i915->params.enable_guc, "undocumented flag"); 87 } 88 89 void intel_uc_init_early(struct intel_uc *uc) 90 { 91 intel_guc_init_early(&uc->guc); 92 intel_huc_init_early(&uc->huc); 93 94 __confirm_options(uc); 95 96 if (intel_uc_wants_guc(uc)) 97 uc->ops = &uc_ops_on; 98 else 99 uc->ops = &uc_ops_off; 100 } 101 102 void intel_uc_driver_late_release(struct intel_uc *uc) 103 { 104 } 105 106 /** 107 * intel_uc_init_mmio - setup uC MMIO access 108 * @uc: the intel_uc structure 109 * 110 * Setup minimal state necessary for MMIO accesses later in the 111 * initialization sequence. 112 */ 113 void intel_uc_init_mmio(struct intel_uc *uc) 114 { 115 intel_guc_init_send_regs(&uc->guc); 116 } 117 118 static void __uc_capture_load_err_log(struct intel_uc *uc) 119 { 120 struct intel_guc *guc = &uc->guc; 121 122 if (guc->log.vma && !uc->load_err_log) 123 uc->load_err_log = i915_gem_object_get(guc->log.vma->obj); 124 } 125 126 static void __uc_free_load_err_log(struct intel_uc *uc) 127 { 128 struct drm_i915_gem_object *log = fetch_and_zero(&uc->load_err_log); 129 130 if (log) 131 i915_gem_object_put(log); 132 } 133 134 void intel_uc_driver_remove(struct intel_uc *uc) 135 { 136 intel_uc_fini_hw(uc); 137 intel_uc_fini(uc); 138 __uc_free_load_err_log(uc); 139 } 140 141 static inline bool guc_communication_enabled(struct intel_guc *guc) 142 { 143 return intel_guc_ct_enabled(&guc->ct); 144 } 145 146 /* 147 * Events triggered while CT buffers are disabled are logged in the SCRATCH_15 148 * register using the same bits used in the CT message payload. Since our 149 * communication channel with guc is turned off at this point, we can save the 150 * message and handle it after we turn it back on. 151 */ 152 static void guc_clear_mmio_msg(struct intel_guc *guc) 153 { 154 intel_uncore_write(guc_to_gt(guc)->uncore, SOFT_SCRATCH(15), 0); 155 } 156 157 static void guc_get_mmio_msg(struct intel_guc *guc) 158 { 159 u32 val; 160 161 spin_lock_irq(&guc->irq_lock); 162 163 val = intel_uncore_read(guc_to_gt(guc)->uncore, SOFT_SCRATCH(15)); 164 guc->mmio_msg |= val & guc->msg_enabled_mask; 165 166 /* 167 * clear all events, including the ones we're not currently servicing, 168 * to make sure we don't try to process a stale message if we enable 169 * handling of more events later. 170 */ 171 guc_clear_mmio_msg(guc); 172 173 spin_unlock_irq(&guc->irq_lock); 174 } 175 176 static void guc_handle_mmio_msg(struct intel_guc *guc) 177 { 178 /* we need communication to be enabled to reply to GuC */ 179 GEM_BUG_ON(!guc_communication_enabled(guc)); 180 181 spin_lock_irq(&guc->irq_lock); 182 if (guc->mmio_msg) { 183 intel_guc_to_host_process_recv_msg(guc, &guc->mmio_msg, 1); 184 guc->mmio_msg = 0; 185 } 186 spin_unlock_irq(&guc->irq_lock); 187 } 188 189 static void guc_reset_interrupts(struct intel_guc *guc) 190 { 191 guc->interrupts.reset(guc); 192 } 193 194 static void guc_enable_interrupts(struct intel_guc *guc) 195 { 196 guc->interrupts.enable(guc); 197 } 198 199 static void guc_disable_interrupts(struct intel_guc *guc) 200 { 201 guc->interrupts.disable(guc); 202 } 203 204 static int guc_enable_communication(struct intel_guc *guc) 205 { 206 struct intel_gt *gt = guc_to_gt(guc); 207 struct drm_i915_private *i915 = gt->i915; 208 int ret; 209 210 GEM_BUG_ON(guc_communication_enabled(guc)); 211 212 ret = i915_inject_probe_error(i915, -ENXIO); 213 if (ret) 214 return ret; 215 216 ret = intel_guc_ct_enable(&guc->ct); 217 if (ret) 218 return ret; 219 220 /* check for mmio messages received before/during the CT enable */ 221 guc_get_mmio_msg(guc); 222 guc_handle_mmio_msg(guc); 223 224 guc_enable_interrupts(guc); 225 226 /* check for CT messages received before we enabled interrupts */ 227 spin_lock_irq(>->irq_lock); 228 intel_guc_ct_event_handler(&guc->ct); 229 spin_unlock_irq(>->irq_lock); 230 231 drm_dbg(&i915->drm, "GuC communication enabled\n"); 232 233 return 0; 234 } 235 236 static void guc_disable_communication(struct intel_guc *guc) 237 { 238 struct drm_i915_private *i915 = guc_to_gt(guc)->i915; 239 240 /* 241 * Events generated during or after CT disable are logged by guc in 242 * via mmio. Make sure the register is clear before disabling CT since 243 * all events we cared about have already been processed via CT. 244 */ 245 guc_clear_mmio_msg(guc); 246 247 guc_disable_interrupts(guc); 248 249 intel_guc_ct_disable(&guc->ct); 250 251 /* 252 * Check for messages received during/after the CT disable. We do not 253 * expect any messages to have arrived via CT between the interrupt 254 * disable and the CT disable because GuC should've been idle until we 255 * triggered the CT disable protocol. 256 */ 257 guc_get_mmio_msg(guc); 258 259 drm_dbg(&i915->drm, "GuC communication disabled\n"); 260 } 261 262 static void __uc_fetch_firmwares(struct intel_uc *uc) 263 { 264 int err; 265 266 GEM_BUG_ON(!intel_uc_wants_guc(uc)); 267 268 err = intel_uc_fw_fetch(&uc->guc.fw); 269 if (err) { 270 /* Make sure we transition out of transient "SELECTED" state */ 271 if (intel_uc_wants_huc(uc)) { 272 drm_dbg(&uc_to_gt(uc)->i915->drm, 273 "Failed to fetch GuC: %d disabling HuC\n", err); 274 intel_uc_fw_change_status(&uc->huc.fw, 275 INTEL_UC_FIRMWARE_ERROR); 276 } 277 278 return; 279 } 280 281 if (intel_uc_wants_huc(uc)) 282 intel_uc_fw_fetch(&uc->huc.fw); 283 } 284 285 static void __uc_cleanup_firmwares(struct intel_uc *uc) 286 { 287 intel_uc_fw_cleanup_fetch(&uc->huc.fw); 288 intel_uc_fw_cleanup_fetch(&uc->guc.fw); 289 } 290 291 static int __uc_init(struct intel_uc *uc) 292 { 293 struct intel_guc *guc = &uc->guc; 294 struct intel_huc *huc = &uc->huc; 295 int ret; 296 297 GEM_BUG_ON(!intel_uc_wants_guc(uc)); 298 299 if (!intel_uc_uses_guc(uc)) 300 return 0; 301 302 if (i915_inject_probe_failure(uc_to_gt(uc)->i915)) 303 return -ENOMEM; 304 305 /* XXX: GuC submission is unavailable for now */ 306 GEM_BUG_ON(intel_uc_uses_guc_submission(uc)); 307 308 ret = intel_guc_init(guc); 309 if (ret) 310 return ret; 311 312 if (intel_uc_uses_huc(uc)) { 313 ret = intel_huc_init(huc); 314 if (ret) 315 goto out_guc; 316 } 317 318 return 0; 319 320 out_guc: 321 intel_guc_fini(guc); 322 return ret; 323 } 324 325 static void __uc_fini(struct intel_uc *uc) 326 { 327 intel_huc_fini(&uc->huc); 328 intel_guc_fini(&uc->guc); 329 } 330 331 static int __uc_sanitize(struct intel_uc *uc) 332 { 333 struct intel_guc *guc = &uc->guc; 334 struct intel_huc *huc = &uc->huc; 335 336 GEM_BUG_ON(!intel_uc_supports_guc(uc)); 337 338 intel_huc_sanitize(huc); 339 intel_guc_sanitize(guc); 340 341 return __intel_uc_reset_hw(uc); 342 } 343 344 /* Initialize and verify the uC regs related to uC positioning in WOPCM */ 345 static int uc_init_wopcm(struct intel_uc *uc) 346 { 347 struct intel_gt *gt = uc_to_gt(uc); 348 struct intel_uncore *uncore = gt->uncore; 349 u32 base = intel_wopcm_guc_base(>->i915->wopcm); 350 u32 size = intel_wopcm_guc_size(>->i915->wopcm); 351 u32 huc_agent = intel_uc_uses_huc(uc) ? HUC_LOADING_AGENT_GUC : 0; 352 u32 mask; 353 int err; 354 355 if (unlikely(!base || !size)) { 356 i915_probe_error(gt->i915, "Unsuccessful WOPCM partitioning\n"); 357 return -E2BIG; 358 } 359 360 GEM_BUG_ON(!intel_uc_supports_guc(uc)); 361 GEM_BUG_ON(!(base & GUC_WOPCM_OFFSET_MASK)); 362 GEM_BUG_ON(base & ~GUC_WOPCM_OFFSET_MASK); 363 GEM_BUG_ON(!(size & GUC_WOPCM_SIZE_MASK)); 364 GEM_BUG_ON(size & ~GUC_WOPCM_SIZE_MASK); 365 366 err = i915_inject_probe_error(gt->i915, -ENXIO); 367 if (err) 368 return err; 369 370 mask = GUC_WOPCM_SIZE_MASK | GUC_WOPCM_SIZE_LOCKED; 371 err = intel_uncore_write_and_verify(uncore, GUC_WOPCM_SIZE, size, mask, 372 size | GUC_WOPCM_SIZE_LOCKED); 373 if (err) 374 goto err_out; 375 376 mask = GUC_WOPCM_OFFSET_MASK | GUC_WOPCM_OFFSET_VALID | huc_agent; 377 err = intel_uncore_write_and_verify(uncore, DMA_GUC_WOPCM_OFFSET, 378 base | huc_agent, mask, 379 base | huc_agent | 380 GUC_WOPCM_OFFSET_VALID); 381 if (err) 382 goto err_out; 383 384 return 0; 385 386 err_out: 387 i915_probe_error(gt->i915, "Failed to init uC WOPCM registers!\n"); 388 i915_probe_error(gt->i915, "%s(%#x)=%#x\n", "DMA_GUC_WOPCM_OFFSET", 389 i915_mmio_reg_offset(DMA_GUC_WOPCM_OFFSET), 390 intel_uncore_read(uncore, DMA_GUC_WOPCM_OFFSET)); 391 i915_probe_error(gt->i915, "%s(%#x)=%#x\n", "GUC_WOPCM_SIZE", 392 i915_mmio_reg_offset(GUC_WOPCM_SIZE), 393 intel_uncore_read(uncore, GUC_WOPCM_SIZE)); 394 395 return err; 396 } 397 398 static bool uc_is_wopcm_locked(struct intel_uc *uc) 399 { 400 struct intel_gt *gt = uc_to_gt(uc); 401 struct intel_uncore *uncore = gt->uncore; 402 403 return (intel_uncore_read(uncore, GUC_WOPCM_SIZE) & GUC_WOPCM_SIZE_LOCKED) || 404 (intel_uncore_read(uncore, DMA_GUC_WOPCM_OFFSET) & GUC_WOPCM_OFFSET_VALID); 405 } 406 407 static int __uc_check_hw(struct intel_uc *uc) 408 { 409 if (!intel_uc_supports_guc(uc)) 410 return 0; 411 412 /* 413 * We can silently continue without GuC only if it was never enabled 414 * before on this system after reboot, otherwise we risk GPU hangs. 415 * To check if GuC was loaded before we look at WOPCM registers. 416 */ 417 if (uc_is_wopcm_locked(uc)) 418 return -EIO; 419 420 return 0; 421 } 422 423 static int __uc_init_hw(struct intel_uc *uc) 424 { 425 struct drm_i915_private *i915 = uc_to_gt(uc)->i915; 426 struct intel_guc *guc = &uc->guc; 427 struct intel_huc *huc = &uc->huc; 428 int ret, attempts; 429 430 GEM_BUG_ON(!intel_uc_supports_guc(uc)); 431 GEM_BUG_ON(!intel_uc_wants_guc(uc)); 432 433 if (!intel_uc_fw_is_loadable(&guc->fw)) { 434 ret = __uc_check_hw(uc) || 435 intel_uc_fw_is_overridden(&guc->fw) || 436 intel_uc_wants_guc_submission(uc) ? 437 intel_uc_fw_status_to_error(guc->fw.status) : 0; 438 goto err_out; 439 } 440 441 ret = uc_init_wopcm(uc); 442 if (ret) 443 goto err_out; 444 445 guc_reset_interrupts(guc); 446 447 /* WaEnableuKernelHeaderValidFix:skl */ 448 /* WaEnableGuCBootHashCheckNotSet:skl,bxt,kbl */ 449 if (IS_GEN(i915, 9)) 450 attempts = 3; 451 else 452 attempts = 1; 453 454 while (attempts--) { 455 /* 456 * Always reset the GuC just before (re)loading, so 457 * that the state and timing are fairly predictable 458 */ 459 ret = __uc_sanitize(uc); 460 if (ret) 461 goto err_out; 462 463 intel_huc_fw_upload(huc); 464 intel_guc_ads_reset(guc); 465 intel_guc_write_params(guc); 466 ret = intel_guc_fw_upload(guc); 467 if (ret == 0) 468 break; 469 470 DRM_DEBUG_DRIVER("GuC fw load failed: %d; will reset and " 471 "retry %d more time(s)\n", ret, attempts); 472 } 473 474 /* Did we succeded or run out of retries? */ 475 if (ret) 476 goto err_log_capture; 477 478 ret = guc_enable_communication(guc); 479 if (ret) 480 goto err_log_capture; 481 482 intel_huc_auth(huc); 483 484 ret = intel_guc_sample_forcewake(guc); 485 if (ret) 486 goto err_communication; 487 488 if (intel_uc_uses_guc_submission(uc)) 489 intel_guc_submission_enable(guc); 490 491 drm_info(&i915->drm, "%s firmware %s version %u.%u %s:%s\n", 492 intel_uc_fw_type_repr(INTEL_UC_FW_TYPE_GUC), guc->fw.path, 493 guc->fw.major_ver_found, guc->fw.minor_ver_found, 494 "submission", 495 enableddisabled(intel_uc_uses_guc_submission(uc))); 496 497 if (intel_uc_uses_huc(uc)) { 498 drm_info(&i915->drm, "%s firmware %s version %u.%u %s:%s\n", 499 intel_uc_fw_type_repr(INTEL_UC_FW_TYPE_HUC), 500 huc->fw.path, 501 huc->fw.major_ver_found, huc->fw.minor_ver_found, 502 "authenticated", 503 yesno(intel_huc_is_authenticated(huc))); 504 } 505 506 return 0; 507 508 /* 509 * We've failed to load the firmware :( 510 */ 511 err_communication: 512 guc_disable_communication(guc); 513 err_log_capture: 514 __uc_capture_load_err_log(uc); 515 err_out: 516 __uc_sanitize(uc); 517 518 if (!ret) { 519 drm_notice(&i915->drm, "GuC is uninitialized\n"); 520 /* We want to run without GuC submission */ 521 return 0; 522 } 523 524 i915_probe_error(i915, "GuC initialization failed %d\n", ret); 525 526 /* We want to keep KMS alive */ 527 return -EIO; 528 } 529 530 static void __uc_fini_hw(struct intel_uc *uc) 531 { 532 struct intel_guc *guc = &uc->guc; 533 534 if (!intel_guc_is_fw_running(guc)) 535 return; 536 537 if (intel_uc_uses_guc_submission(uc)) 538 intel_guc_submission_disable(guc); 539 540 if (guc_communication_enabled(guc)) 541 guc_disable_communication(guc); 542 543 __uc_sanitize(uc); 544 } 545 546 /** 547 * intel_uc_reset_prepare - Prepare for reset 548 * @uc: the intel_uc structure 549 * 550 * Preparing for full gpu reset. 551 */ 552 void intel_uc_reset_prepare(struct intel_uc *uc) 553 { 554 struct intel_guc *guc = &uc->guc; 555 556 if (!intel_guc_is_ready(guc)) 557 return; 558 559 guc_disable_communication(guc); 560 __uc_sanitize(uc); 561 } 562 563 void intel_uc_runtime_suspend(struct intel_uc *uc) 564 { 565 struct intel_guc *guc = &uc->guc; 566 int err; 567 568 if (!intel_guc_is_ready(guc)) 569 return; 570 571 err = intel_guc_suspend(guc); 572 if (err) 573 DRM_DEBUG_DRIVER("Failed to suspend GuC, err=%d", err); 574 575 guc_disable_communication(guc); 576 } 577 578 void intel_uc_suspend(struct intel_uc *uc) 579 { 580 struct intel_guc *guc = &uc->guc; 581 intel_wakeref_t wakeref; 582 583 if (!intel_guc_is_ready(guc)) 584 return; 585 586 with_intel_runtime_pm(uc_to_gt(uc)->uncore->rpm, wakeref) 587 intel_uc_runtime_suspend(uc); 588 } 589 590 static int __uc_resume(struct intel_uc *uc, bool enable_communication) 591 { 592 struct intel_guc *guc = &uc->guc; 593 int err; 594 595 if (!intel_guc_is_fw_running(guc)) 596 return 0; 597 598 /* Make sure we enable communication if and only if it's disabled */ 599 GEM_BUG_ON(enable_communication == guc_communication_enabled(guc)); 600 601 if (enable_communication) 602 guc_enable_communication(guc); 603 604 err = intel_guc_resume(guc); 605 if (err) { 606 DRM_DEBUG_DRIVER("Failed to resume GuC, err=%d", err); 607 return err; 608 } 609 610 return 0; 611 } 612 613 int intel_uc_resume(struct intel_uc *uc) 614 { 615 /* 616 * When coming out of S3/S4 we sanitize and re-init the HW, so 617 * communication is already re-enabled at this point. 618 */ 619 return __uc_resume(uc, false); 620 } 621 622 int intel_uc_runtime_resume(struct intel_uc *uc) 623 { 624 /* 625 * During runtime resume we don't sanitize, so we need to re-init 626 * communication as well. 627 */ 628 return __uc_resume(uc, true); 629 } 630 631 static const struct intel_uc_ops uc_ops_off = { 632 .init_hw = __uc_check_hw, 633 }; 634 635 static const struct intel_uc_ops uc_ops_on = { 636 .sanitize = __uc_sanitize, 637 638 .init_fw = __uc_fetch_firmwares, 639 .fini_fw = __uc_cleanup_firmwares, 640 641 .init = __uc_init, 642 .fini = __uc_fini, 643 644 .init_hw = __uc_init_hw, 645 .fini_hw = __uc_fini_hw, 646 }; 647