1 // SPDX-License-Identifier: MIT
3 * Copyright © 2016-2019 Intel Corporation
6 #include <linux/string_helpers.h>
8 #include "gt/intel_gt.h"
9 #include "gt/intel_gt_print.h"
10 #include "gt/intel_reset.h"
11 #include "intel_gsc_fw.h"
12 #include "intel_gsc_uc.h"
13 #include "intel_guc.h"
14 #include "intel_guc_ads.h"
15 #include "intel_guc_print.h"
16 #include "intel_guc_submission.h"
17 #include "gt/intel_rps.h"
21 #include "i915_hwmon.h"
23 static const struct intel_uc_ops uc_ops_off;
24 static const struct intel_uc_ops uc_ops_on;
26 static void uc_expand_default_options(struct intel_uc *uc)
28 struct drm_i915_private *i915 = uc_to_gt(uc)->i915;
30 if (i915->params.enable_guc != -1)
33 /* Don't enable GuC/HuC on pre-Gen12 */
34 if (GRAPHICS_VER(i915) < 12) {
35 i915->params.enable_guc = 0;
39 /* Don't enable GuC/HuC on older Gen12 platforms */
40 if (IS_TIGERLAKE(i915) || IS_ROCKETLAKE(i915)) {
41 i915->params.enable_guc = 0;
45 /* Intermediate platforms are HuC authentication only */
46 if (IS_ALDERLAKE_S(i915) && !IS_RAPTORLAKE_S(i915)) {
47 i915->params.enable_guc = ENABLE_GUC_LOAD_HUC;
51 /* Default: enable HuC authentication and GuC submission */
52 i915->params.enable_guc = ENABLE_GUC_LOAD_HUC | ENABLE_GUC_SUBMISSION;
55 /* Reset GuC providing us with fresh state for both GuC and HuC.
57 static int __intel_uc_reset_hw(struct intel_uc *uc)
59 struct intel_gt *gt = uc_to_gt(uc);
63 ret = i915_inject_probe_error(gt->i915, -ENXIO);
67 ret = intel_reset_guc(gt);
69 gt_err(gt, "Failed to reset GuC, ret = %d\n", ret);
73 guc_status = intel_uncore_read(gt->uncore, GUC_STATUS);
74 gt_WARN(gt, !(guc_status & GS_MIA_IN_RESET),
75 "GuC status: 0x%x, MIA core expected to be in reset\n",
81 static void __confirm_options(struct intel_uc *uc)
83 struct intel_gt *gt = uc_to_gt(uc);
84 struct drm_i915_private *i915 = gt->i915;
86 gt_dbg(gt, "enable_guc=%d (guc:%s submission:%s huc:%s slpc:%s)\n",
87 i915->params.enable_guc,
88 str_yes_no(intel_uc_wants_guc(uc)),
89 str_yes_no(intel_uc_wants_guc_submission(uc)),
90 str_yes_no(intel_uc_wants_huc(uc)),
91 str_yes_no(intel_uc_wants_guc_slpc(uc)));
93 if (i915->params.enable_guc == 0) {
94 GEM_BUG_ON(intel_uc_wants_guc(uc));
95 GEM_BUG_ON(intel_uc_wants_guc_submission(uc));
96 GEM_BUG_ON(intel_uc_wants_huc(uc));
97 GEM_BUG_ON(intel_uc_wants_guc_slpc(uc));
101 if (!intel_uc_supports_guc(uc))
102 gt_info(gt, "Incompatible option enable_guc=%d - %s\n",
103 i915->params.enable_guc, "GuC is not supported!");
105 if (i915->params.enable_guc & ENABLE_GUC_SUBMISSION &&
106 !intel_uc_supports_guc_submission(uc))
107 gt_info(gt, "Incompatible option enable_guc=%d - %s\n",
108 i915->params.enable_guc, "GuC submission is N/A");
110 if (i915->params.enable_guc & ~ENABLE_GUC_MASK)
111 gt_info(gt, "Incompatible option enable_guc=%d - %s\n",
112 i915->params.enable_guc, "undocumented flag");
115 void intel_uc_init_early(struct intel_uc *uc)
117 uc_expand_default_options(uc);
119 intel_guc_init_early(&uc->guc);
120 intel_huc_init_early(&uc->huc);
121 intel_gsc_uc_init_early(&uc->gsc);
123 __confirm_options(uc);
125 if (intel_uc_wants_guc(uc))
126 uc->ops = &uc_ops_on;
128 uc->ops = &uc_ops_off;
131 void intel_uc_init_late(struct intel_uc *uc)
133 intel_guc_init_late(&uc->guc);
134 intel_gsc_uc_load_start(&uc->gsc);
137 void intel_uc_driver_late_release(struct intel_uc *uc)
142 * intel_uc_init_mmio - setup uC MMIO access
143 * @uc: the intel_uc structure
145 * Setup minimal state necessary for MMIO accesses later in the
146 * initialization sequence.
148 void intel_uc_init_mmio(struct intel_uc *uc)
150 intel_guc_init_send_regs(&uc->guc);
153 static void __uc_capture_load_err_log(struct intel_uc *uc)
155 struct intel_guc *guc = &uc->guc;
157 if (guc->log.vma && !uc->load_err_log)
158 uc->load_err_log = i915_gem_object_get(guc->log.vma->obj);
161 static void __uc_free_load_err_log(struct intel_uc *uc)
163 struct drm_i915_gem_object *log = fetch_and_zero(&uc->load_err_log);
166 i915_gem_object_put(log);
169 void intel_uc_driver_remove(struct intel_uc *uc)
171 intel_uc_fini_hw(uc);
173 __uc_free_load_err_log(uc);
177 * Events triggered while CT buffers are disabled are logged in the SCRATCH_15
178 * register using the same bits used in the CT message payload. Since our
179 * communication channel with guc is turned off at this point, we can save the
180 * message and handle it after we turn it back on.
182 static void guc_clear_mmio_msg(struct intel_guc *guc)
184 intel_uncore_write(guc_to_gt(guc)->uncore, SOFT_SCRATCH(15), 0);
187 static void guc_get_mmio_msg(struct intel_guc *guc)
191 spin_lock_irq(&guc->irq_lock);
193 val = intel_uncore_read(guc_to_gt(guc)->uncore, SOFT_SCRATCH(15));
194 guc->mmio_msg |= val & guc->msg_enabled_mask;
197 * clear all events, including the ones we're not currently servicing,
198 * to make sure we don't try to process a stale message if we enable
199 * handling of more events later.
201 guc_clear_mmio_msg(guc);
203 spin_unlock_irq(&guc->irq_lock);
206 static void guc_handle_mmio_msg(struct intel_guc *guc)
208 /* we need communication to be enabled to reply to GuC */
209 GEM_BUG_ON(!intel_guc_ct_enabled(&guc->ct));
211 spin_lock_irq(&guc->irq_lock);
213 intel_guc_to_host_process_recv_msg(guc, &guc->mmio_msg, 1);
216 spin_unlock_irq(&guc->irq_lock);
219 static int guc_enable_communication(struct intel_guc *guc)
221 struct intel_gt *gt = guc_to_gt(guc);
222 struct drm_i915_private *i915 = gt->i915;
225 GEM_BUG_ON(intel_guc_ct_enabled(&guc->ct));
227 ret = i915_inject_probe_error(i915, -ENXIO);
231 ret = intel_guc_ct_enable(&guc->ct);
235 /* check for mmio messages received before/during the CT enable */
236 guc_get_mmio_msg(guc);
237 guc_handle_mmio_msg(guc);
239 intel_guc_enable_interrupts(guc);
241 /* check for CT messages received before we enabled interrupts */
242 spin_lock_irq(gt->irq_lock);
243 intel_guc_ct_event_handler(&guc->ct);
244 spin_unlock_irq(gt->irq_lock);
246 guc_dbg(guc, "communication enabled\n");
251 static void guc_disable_communication(struct intel_guc *guc)
254 * Events generated during or after CT disable are logged by guc in
255 * via mmio. Make sure the register is clear before disabling CT since
256 * all events we cared about have already been processed via CT.
258 guc_clear_mmio_msg(guc);
260 intel_guc_disable_interrupts(guc);
262 intel_guc_ct_disable(&guc->ct);
265 * Check for messages received during/after the CT disable. We do not
266 * expect any messages to have arrived via CT between the interrupt
267 * disable and the CT disable because GuC should've been idle until we
268 * triggered the CT disable protocol.
270 guc_get_mmio_msg(guc);
272 guc_dbg(guc, "communication disabled\n");
275 static void __uc_fetch_firmwares(struct intel_uc *uc)
277 struct intel_gt *gt = uc_to_gt(uc);
280 GEM_BUG_ON(!intel_uc_wants_guc(uc));
282 err = intel_uc_fw_fetch(&uc->guc.fw);
284 /* Make sure we transition out of transient "SELECTED" state */
285 if (intel_uc_wants_huc(uc)) {
286 gt_dbg(gt, "Failed to fetch GuC fw (%pe) disabling HuC\n", ERR_PTR(err));
287 intel_uc_fw_change_status(&uc->huc.fw,
288 INTEL_UC_FIRMWARE_ERROR);
291 if (intel_uc_wants_gsc_uc(uc)) {
292 gt_dbg(gt, "Failed to fetch GuC fw (%pe) disabling GSC\n", ERR_PTR(err));
293 intel_uc_fw_change_status(&uc->gsc.fw,
294 INTEL_UC_FIRMWARE_ERROR);
300 if (intel_uc_wants_huc(uc))
301 intel_uc_fw_fetch(&uc->huc.fw);
303 if (intel_uc_wants_gsc_uc(uc))
304 intel_uc_fw_fetch(&uc->gsc.fw);
307 static void __uc_cleanup_firmwares(struct intel_uc *uc)
309 intel_uc_fw_cleanup_fetch(&uc->gsc.fw);
310 intel_uc_fw_cleanup_fetch(&uc->huc.fw);
311 intel_uc_fw_cleanup_fetch(&uc->guc.fw);
314 static int __uc_init(struct intel_uc *uc)
316 struct intel_guc *guc = &uc->guc;
317 struct intel_huc *huc = &uc->huc;
320 GEM_BUG_ON(!intel_uc_wants_guc(uc));
322 if (!intel_uc_uses_guc(uc))
325 if (i915_inject_probe_failure(uc_to_gt(uc)->i915))
328 ret = intel_guc_init(guc);
332 if (intel_uc_uses_huc(uc))
335 if (intel_uc_uses_gsc_uc(uc))
336 intel_gsc_uc_init(&uc->gsc);
341 static void __uc_fini(struct intel_uc *uc)
343 intel_gsc_uc_fini(&uc->gsc);
344 intel_huc_fini(&uc->huc);
345 intel_guc_fini(&uc->guc);
348 static int __uc_sanitize(struct intel_uc *uc)
350 struct intel_guc *guc = &uc->guc;
351 struct intel_huc *huc = &uc->huc;
353 GEM_BUG_ON(!intel_uc_supports_guc(uc));
355 intel_huc_sanitize(huc);
356 intel_guc_sanitize(guc);
358 return __intel_uc_reset_hw(uc);
361 /* Initialize and verify the uC regs related to uC positioning in WOPCM */
362 static int uc_init_wopcm(struct intel_uc *uc)
364 struct intel_gt *gt = uc_to_gt(uc);
365 struct intel_uncore *uncore = gt->uncore;
366 u32 base = intel_wopcm_guc_base(>->wopcm);
367 u32 size = intel_wopcm_guc_size(>->wopcm);
368 u32 huc_agent = intel_uc_uses_huc(uc) ? HUC_LOADING_AGENT_GUC : 0;
372 if (unlikely(!base || !size)) {
373 gt_probe_error(gt, "Unsuccessful WOPCM partitioning\n");
377 GEM_BUG_ON(!intel_uc_supports_guc(uc));
378 GEM_BUG_ON(!(base & GUC_WOPCM_OFFSET_MASK));
379 GEM_BUG_ON(base & ~GUC_WOPCM_OFFSET_MASK);
380 GEM_BUG_ON(!(size & GUC_WOPCM_SIZE_MASK));
381 GEM_BUG_ON(size & ~GUC_WOPCM_SIZE_MASK);
383 err = i915_inject_probe_error(gt->i915, -ENXIO);
387 mask = GUC_WOPCM_SIZE_MASK | GUC_WOPCM_SIZE_LOCKED;
388 err = intel_uncore_write_and_verify(uncore, GUC_WOPCM_SIZE, size, mask,
389 size | GUC_WOPCM_SIZE_LOCKED);
393 mask = GUC_WOPCM_OFFSET_MASK | GUC_WOPCM_OFFSET_VALID | huc_agent;
394 err = intel_uncore_write_and_verify(uncore, DMA_GUC_WOPCM_OFFSET,
395 base | huc_agent, mask,
397 GUC_WOPCM_OFFSET_VALID);
404 gt_probe_error(gt, "Failed to init uC WOPCM registers!\n");
405 gt_probe_error(gt, "%s(%#x)=%#x\n", "DMA_GUC_WOPCM_OFFSET",
406 i915_mmio_reg_offset(DMA_GUC_WOPCM_OFFSET),
407 intel_uncore_read(uncore, DMA_GUC_WOPCM_OFFSET));
408 gt_probe_error(gt, "%s(%#x)=%#x\n", "GUC_WOPCM_SIZE",
409 i915_mmio_reg_offset(GUC_WOPCM_SIZE),
410 intel_uncore_read(uncore, GUC_WOPCM_SIZE));
415 static bool uc_is_wopcm_locked(struct intel_uc *uc)
417 struct intel_gt *gt = uc_to_gt(uc);
418 struct intel_uncore *uncore = gt->uncore;
420 return (intel_uncore_read(uncore, GUC_WOPCM_SIZE) & GUC_WOPCM_SIZE_LOCKED) ||
421 (intel_uncore_read(uncore, DMA_GUC_WOPCM_OFFSET) & GUC_WOPCM_OFFSET_VALID);
424 static int __uc_check_hw(struct intel_uc *uc)
426 if (uc->fw_table_invalid)
429 if (!intel_uc_supports_guc(uc))
433 * We can silently continue without GuC only if it was never enabled
434 * before on this system after reboot, otherwise we risk GPU hangs.
435 * To check if GuC was loaded before we look at WOPCM registers.
437 if (uc_is_wopcm_locked(uc))
443 static void print_fw_ver(struct intel_gt *gt, struct intel_uc_fw *fw)
445 gt_info(gt, "%s firmware %s version %u.%u.%u\n",
446 intel_uc_fw_type_repr(fw->type), fw->file_selected.path,
447 fw->file_selected.ver.major,
448 fw->file_selected.ver.minor,
449 fw->file_selected.ver.patch);
452 static int __uc_init_hw(struct intel_uc *uc)
454 struct intel_gt *gt = uc_to_gt(uc);
455 struct drm_i915_private *i915 = gt->i915;
456 struct intel_guc *guc = &uc->guc;
457 struct intel_huc *huc = &uc->huc;
461 GEM_BUG_ON(!intel_uc_supports_guc(uc));
462 GEM_BUG_ON(!intel_uc_wants_guc(uc));
464 print_fw_ver(gt, &guc->fw);
466 if (intel_uc_uses_huc(uc))
467 print_fw_ver(gt, &huc->fw);
469 if (!intel_uc_fw_is_loadable(&guc->fw)) {
470 ret = __uc_check_hw(uc) ||
471 intel_uc_fw_is_overridden(&guc->fw) ||
472 intel_uc_wants_guc_submission(uc) ?
473 intel_uc_fw_status_to_error(guc->fw.status) : 0;
477 ret = uc_init_wopcm(uc);
481 intel_guc_reset_interrupts(guc);
483 /* WaEnableuKernelHeaderValidFix:skl */
484 /* WaEnableGuCBootHashCheckNotSet:skl,bxt,kbl */
485 if (GRAPHICS_VER(i915) == 9)
490 /* Disable a potentially low PL1 power limit to allow freq to be raised */
491 i915_hwmon_power_max_disable(gt->i915, &pl1en);
493 intel_rps_raise_unslice(&uc_to_gt(uc)->rps);
497 * Always reset the GuC just before (re)loading, so
498 * that the state and timing are fairly predictable
500 ret = __uc_sanitize(uc);
504 intel_huc_fw_upload(huc);
505 intel_guc_ads_reset(guc);
506 intel_guc_write_params(guc);
507 ret = intel_guc_fw_upload(guc);
511 gt_dbg(gt, "GuC fw load failed (%pe) will reset and retry %d more time(s)\n",
512 ERR_PTR(ret), attempts);
515 /* Did we succeded or run out of retries? */
517 goto err_log_capture;
519 ret = guc_enable_communication(guc);
521 goto err_log_capture;
524 * GSC-loaded HuC is authenticated by the GSC, so we don't need to
525 * trigger the auth here. However, given that the HuC loaded this way
526 * survive GT reset, we still need to update our SW bookkeeping to make
527 * sure it reflects the correct HW status.
529 if (intel_huc_is_loaded_by_gsc(huc))
530 intel_huc_update_auth_status(huc);
532 intel_huc_auth(huc, INTEL_HUC_AUTH_BY_GUC);
534 if (intel_uc_uses_guc_submission(uc)) {
535 ret = intel_guc_submission_enable(guc);
537 goto err_log_capture;
540 if (intel_uc_uses_guc_slpc(uc)) {
541 ret = intel_guc_slpc_enable(&guc->slpc);
545 /* Restore GT back to RPn for non-SLPC path */
546 intel_rps_lower_unslice(&uc_to_gt(uc)->rps);
549 i915_hwmon_power_max_restore(gt->i915, pl1en);
551 guc_info(guc, "submission %s\n", str_enabled_disabled(intel_uc_uses_guc_submission(uc)));
552 guc_info(guc, "SLPC %s\n", str_enabled_disabled(intel_uc_uses_guc_slpc(uc)));
557 * We've failed to load the firmware :(
560 intel_guc_submission_disable(guc);
562 __uc_capture_load_err_log(uc);
564 /* Return GT back to RPn */
565 intel_rps_lower_unslice(&uc_to_gt(uc)->rps);
567 i915_hwmon_power_max_restore(gt->i915, pl1en);
572 gt_notice(gt, "GuC is uninitialized\n");
573 /* We want to run without GuC submission */
577 gt_probe_error(gt, "GuC initialization failed %pe\n", ERR_PTR(ret));
579 /* We want to keep KMS alive */
583 static void __uc_fini_hw(struct intel_uc *uc)
585 struct intel_guc *guc = &uc->guc;
587 if (!intel_guc_is_fw_running(guc))
590 if (intel_uc_uses_guc_submission(uc))
591 intel_guc_submission_disable(guc);
597 * intel_uc_reset_prepare - Prepare for reset
598 * @uc: the intel_uc structure
600 * Preparing for full gpu reset.
602 void intel_uc_reset_prepare(struct intel_uc *uc)
604 struct intel_guc *guc = &uc->guc;
606 uc->reset_in_progress = true;
608 /* Nothing to do if GuC isn't supported */
609 if (!intel_uc_supports_guc(uc))
612 /* Firmware expected to be running when this function is called */
613 if (!intel_guc_is_ready(guc))
616 if (intel_uc_uses_guc_submission(uc))
617 intel_guc_submission_reset_prepare(guc);
623 void intel_uc_reset(struct intel_uc *uc, intel_engine_mask_t stalled)
625 struct intel_guc *guc = &uc->guc;
627 /* Firmware can not be running when this function is called */
628 if (intel_uc_uses_guc_submission(uc))
629 intel_guc_submission_reset(guc, stalled);
632 void intel_uc_reset_finish(struct intel_uc *uc)
634 struct intel_guc *guc = &uc->guc;
637 * NB: The wedge code path results in prepare -> prepare -> finish -> finish.
638 * So this function is sometimes called with the in-progress flag not set.
640 uc->reset_in_progress = false;
642 /* Firmware expected to be running when this function is called */
643 if (intel_uc_uses_guc_submission(uc))
644 intel_guc_submission_reset_finish(guc);
647 void intel_uc_cancel_requests(struct intel_uc *uc)
649 struct intel_guc *guc = &uc->guc;
651 /* Firmware can not be running when this function is called */
652 if (intel_uc_uses_guc_submission(uc))
653 intel_guc_submission_cancel_requests(guc);
656 void intel_uc_runtime_suspend(struct intel_uc *uc)
658 struct intel_guc *guc = &uc->guc;
660 if (!intel_guc_is_ready(guc)) {
661 guc->interrupts.enabled = false;
666 * Wait for any outstanding CTB before tearing down communication /w the
669 #define OUTSTANDING_CTB_TIMEOUT_PERIOD (HZ / 5)
670 intel_guc_wait_for_pending_msg(guc, &guc->outstanding_submission_g2h,
671 false, OUTSTANDING_CTB_TIMEOUT_PERIOD);
672 GEM_WARN_ON(atomic_read(&guc->outstanding_submission_g2h));
674 guc_disable_communication(guc);
677 void intel_uc_suspend(struct intel_uc *uc)
679 struct intel_guc *guc = &uc->guc;
680 intel_wakeref_t wakeref;
683 /* flush the GSC worker */
684 intel_gsc_uc_flush_work(&uc->gsc);
686 wake_up_all_tlb_invalidate(guc);
688 if (!intel_guc_is_ready(guc)) {
689 guc->interrupts.enabled = false;
693 intel_guc_submission_flush_work(guc);
695 with_intel_runtime_pm(&uc_to_gt(uc)->i915->runtime_pm, wakeref) {
696 err = intel_guc_suspend(guc);
698 guc_dbg(guc, "Failed to suspend, %pe", ERR_PTR(err));
702 static void __uc_resume_mappings(struct intel_uc *uc)
704 intel_uc_fw_resume_mapping(&uc->guc.fw);
705 intel_uc_fw_resume_mapping(&uc->huc.fw);
708 static int __uc_resume(struct intel_uc *uc, bool enable_communication)
710 struct intel_guc *guc = &uc->guc;
711 struct intel_gt *gt = guc_to_gt(guc);
714 if (!intel_guc_is_fw_running(guc))
717 /* Make sure we enable communication if and only if it's disabled */
718 GEM_BUG_ON(enable_communication == intel_guc_ct_enabled(&guc->ct));
720 if (enable_communication)
721 guc_enable_communication(guc);
723 /* If we are only resuming GuC communication but not reloading
724 * GuC, we need to ensure the ARAT timer interrupt is enabled
725 * again. In case of GuC reload, it is enabled during SLPC enable.
727 if (enable_communication && intel_uc_uses_guc_slpc(uc))
728 intel_guc_pm_intrmsk_enable(gt);
730 err = intel_guc_resume(guc);
732 guc_dbg(guc, "Failed to resume, %pe", ERR_PTR(err));
736 intel_gsc_uc_resume(&uc->gsc);
738 if (intel_guc_tlb_invalidation_is_available(guc)) {
739 intel_guc_invalidate_tlb_engines(guc);
740 intel_guc_invalidate_tlb_guc(guc);
746 int intel_uc_resume(struct intel_uc *uc)
749 * When coming out of S3/S4 we sanitize and re-init the HW, so
750 * communication is already re-enabled at this point.
752 return __uc_resume(uc, false);
755 int intel_uc_runtime_resume(struct intel_uc *uc)
758 * During runtime resume we don't sanitize, so we need to re-init
759 * communication as well.
761 return __uc_resume(uc, true);
764 static const struct intel_uc_ops uc_ops_off = {
765 .init_hw = __uc_check_hw,
766 .fini = __uc_fini, /* to clean-up the init_early initialization */
769 static const struct intel_uc_ops uc_ops_on = {
770 .sanitize = __uc_sanitize,
772 .init_fw = __uc_fetch_firmwares,
773 .fini_fw = __uc_cleanup_firmwares,
778 .init_hw = __uc_init_hw,
779 .fini_hw = __uc_fini_hw,
781 .resume_mappings = __uc_resume_mappings,