1 // SPDX-License-Identifier: MIT
3 * Copyright © 2016-2019 Intel Corporation
6 #include "gt/intel_gt.h"
7 #include "gt/intel_reset.h"
9 #include "intel_guc_ads.h"
10 #include "intel_guc_submission.h"
15 static const struct intel_uc_ops uc_ops_off;
16 static const struct intel_uc_ops uc_ops_on;
18 /* Reset GuC providing us with fresh state for both GuC and HuC.
20 static int __intel_uc_reset_hw(struct intel_uc *uc)
22 struct intel_gt *gt = uc_to_gt(uc);
26 ret = i915_inject_probe_error(gt->i915, -ENXIO);
30 ret = intel_reset_guc(gt);
32 DRM_ERROR("Failed to reset GuC, ret = %d\n", ret);
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",
44 static void __confirm_options(struct intel_uc *uc)
46 struct drm_i915_private *i915 = uc_to_gt(uc)->i915;
48 DRM_DEV_DEBUG_DRIVER(i915->drm.dev,
49 "enable_guc=%d (guc:%s submission:%s huc:%s)\n",
50 i915_modparams.enable_guc,
51 yesno(intel_uc_uses_guc(uc)),
52 yesno(intel_uc_uses_guc_submission(uc)),
53 yesno(intel_uc_uses_huc(uc)));
55 if (i915_modparams.enable_guc == -1)
58 if (i915_modparams.enable_guc == 0) {
59 GEM_BUG_ON(intel_uc_uses_guc(uc));
60 GEM_BUG_ON(intel_uc_uses_guc_submission(uc));
61 GEM_BUG_ON(intel_uc_uses_huc(uc));
65 if (!intel_uc_supports_guc(uc))
66 dev_info(i915->drm.dev,
67 "Incompatible option enable_guc=%d - %s\n",
68 i915_modparams.enable_guc, "GuC is not supported!");
70 if (i915_modparams.enable_guc & ENABLE_GUC_LOAD_HUC &&
71 !intel_uc_supports_huc(uc))
72 dev_info(i915->drm.dev,
73 "Incompatible option enable_guc=%d - %s\n",
74 i915_modparams.enable_guc, "HuC is not supported!");
76 if (i915_modparams.enable_guc & ENABLE_GUC_SUBMISSION &&
77 !intel_uc_supports_guc_submission(uc))
78 dev_info(i915->drm.dev,
79 "Incompatible option enable_guc=%d - %s\n",
80 i915_modparams.enable_guc, "GuC submission is N/A");
82 if (i915_modparams.enable_guc & ~(ENABLE_GUC_SUBMISSION |
84 dev_info(i915->drm.dev,
85 "Incompatible option enable_guc=%d - %s\n",
86 i915_modparams.enable_guc, "undocumented flag");
89 void intel_uc_init_early(struct intel_uc *uc)
91 intel_guc_init_early(&uc->guc);
92 intel_huc_init_early(&uc->huc);
94 __confirm_options(uc);
96 if (intel_uc_uses_guc(uc))
99 uc->ops = &uc_ops_off;
102 void intel_uc_driver_late_release(struct intel_uc *uc)
107 * intel_uc_init_mmio - setup uC MMIO access
108 * @uc: the intel_uc structure
110 * Setup minimal state necessary for MMIO accesses later in the
111 * initialization sequence.
113 void intel_uc_init_mmio(struct intel_uc *uc)
115 intel_guc_init_send_regs(&uc->guc);
118 static void __uc_capture_load_err_log(struct intel_uc *uc)
120 struct intel_guc *guc = &uc->guc;
122 if (guc->log.vma && !uc->load_err_log)
123 uc->load_err_log = i915_gem_object_get(guc->log.vma->obj);
126 static void __uc_free_load_err_log(struct intel_uc *uc)
128 struct drm_i915_gem_object *log = fetch_and_zero(&uc->load_err_log);
131 i915_gem_object_put(log);
134 static inline bool guc_communication_enabled(struct intel_guc *guc)
136 return intel_guc_ct_enabled(&guc->ct);
140 * Events triggered while CT buffers are disabled are logged in the SCRATCH_15
141 * register using the same bits used in the CT message payload. Since our
142 * communication channel with guc is turned off at this point, we can save the
143 * message and handle it after we turn it back on.
145 static void guc_clear_mmio_msg(struct intel_guc *guc)
147 intel_uncore_write(guc_to_gt(guc)->uncore, SOFT_SCRATCH(15), 0);
150 static void guc_get_mmio_msg(struct intel_guc *guc)
154 spin_lock_irq(&guc->irq_lock);
156 val = intel_uncore_read(guc_to_gt(guc)->uncore, SOFT_SCRATCH(15));
157 guc->mmio_msg |= val & guc->msg_enabled_mask;
160 * clear all events, including the ones we're not currently servicing,
161 * to make sure we don't try to process a stale message if we enable
162 * handling of more events later.
164 guc_clear_mmio_msg(guc);
166 spin_unlock_irq(&guc->irq_lock);
169 static void guc_handle_mmio_msg(struct intel_guc *guc)
171 struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
173 /* we need communication to be enabled to reply to GuC */
174 GEM_BUG_ON(!guc_communication_enabled(guc));
179 spin_lock_irq(&i915->irq_lock);
180 intel_guc_to_host_process_recv_msg(guc, &guc->mmio_msg, 1);
181 spin_unlock_irq(&i915->irq_lock);
186 static void guc_reset_interrupts(struct intel_guc *guc)
188 guc->interrupts.reset(guc);
191 static void guc_enable_interrupts(struct intel_guc *guc)
193 guc->interrupts.enable(guc);
196 static void guc_disable_interrupts(struct intel_guc *guc)
198 guc->interrupts.disable(guc);
201 static int guc_enable_communication(struct intel_guc *guc)
203 struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
206 GEM_BUG_ON(guc_communication_enabled(guc));
208 ret = i915_inject_probe_error(i915, -ENXIO);
212 ret = intel_guc_ct_enable(&guc->ct);
216 /* check for mmio messages received before/during the CT enable */
217 guc_get_mmio_msg(guc);
218 guc_handle_mmio_msg(guc);
220 guc_enable_interrupts(guc);
222 /* check for CT messages received before we enabled interrupts */
223 spin_lock_irq(&i915->irq_lock);
224 intel_guc_ct_event_handler(&guc->ct);
225 spin_unlock_irq(&i915->irq_lock);
227 DRM_INFO("GuC communication enabled\n");
232 static void guc_disable_communication(struct intel_guc *guc)
235 * Events generated during or after CT disable are logged by guc in
236 * via mmio. Make sure the register is clear before disabling CT since
237 * all events we cared about have already been processed via CT.
239 guc_clear_mmio_msg(guc);
241 guc_disable_interrupts(guc);
243 intel_guc_ct_disable(&guc->ct);
246 * Check for messages received during/after the CT disable. We do not
247 * expect any messages to have arrived via CT between the interrupt
248 * disable and the CT disable because GuC should've been idle until we
249 * triggered the CT disable protocol.
251 guc_get_mmio_msg(guc);
253 DRM_INFO("GuC communication disabled\n");
256 void intel_uc_fetch_firmwares(struct intel_uc *uc)
260 if (!intel_uc_uses_guc(uc))
263 err = intel_uc_fw_fetch(&uc->guc.fw);
267 if (intel_uc_uses_huc(uc))
268 intel_uc_fw_fetch(&uc->huc.fw);
271 void intel_uc_cleanup_firmwares(struct intel_uc *uc)
273 intel_uc_fw_cleanup_fetch(&uc->huc.fw);
274 intel_uc_fw_cleanup_fetch(&uc->guc.fw);
277 void intel_uc_init(struct intel_uc *uc)
279 struct intel_guc *guc = &uc->guc;
280 struct intel_huc *huc = &uc->huc;
283 if (!intel_uc_uses_guc(uc))
286 /* XXX: GuC submission is unavailable for now */
287 GEM_BUG_ON(intel_uc_supports_guc_submission(uc));
289 ret = intel_guc_init(guc);
291 intel_uc_fw_cleanup_fetch(&huc->fw);
295 if (intel_uc_uses_huc(uc))
299 void intel_uc_fini(struct intel_uc *uc)
301 intel_huc_fini(&uc->huc);
302 intel_guc_fini(&uc->guc);
304 __uc_free_load_err_log(uc);
307 static int __uc_sanitize(struct intel_uc *uc)
309 struct intel_guc *guc = &uc->guc;
310 struct intel_huc *huc = &uc->huc;
312 GEM_BUG_ON(!intel_uc_supports_guc(uc));
314 intel_huc_sanitize(huc);
315 intel_guc_sanitize(guc);
317 return __intel_uc_reset_hw(uc);
320 void intel_uc_sanitize(struct intel_uc *uc)
322 if (!intel_uc_supports_guc(uc))
328 /* Initialize and verify the uC regs related to uC positioning in WOPCM */
329 static int uc_init_wopcm(struct intel_uc *uc)
331 struct intel_gt *gt = uc_to_gt(uc);
332 struct intel_uncore *uncore = gt->uncore;
333 u32 base = intel_wopcm_guc_base(>->i915->wopcm);
334 u32 size = intel_wopcm_guc_size(>->i915->wopcm);
335 u32 huc_agent = intel_uc_uses_huc(uc) ? HUC_LOADING_AGENT_GUC : 0;
339 if (unlikely(!base || !size)) {
340 i915_probe_error(gt->i915, "Unsuccessful WOPCM partitioning\n");
344 GEM_BUG_ON(!intel_uc_supports_guc(uc));
345 GEM_BUG_ON(!(base & GUC_WOPCM_OFFSET_MASK));
346 GEM_BUG_ON(base & ~GUC_WOPCM_OFFSET_MASK);
347 GEM_BUG_ON(!(size & GUC_WOPCM_SIZE_MASK));
348 GEM_BUG_ON(size & ~GUC_WOPCM_SIZE_MASK);
350 err = i915_inject_probe_error(gt->i915, -ENXIO);
354 mask = GUC_WOPCM_SIZE_MASK | GUC_WOPCM_SIZE_LOCKED;
355 err = intel_uncore_write_and_verify(uncore, GUC_WOPCM_SIZE, size, mask,
356 size | GUC_WOPCM_SIZE_LOCKED);
360 mask = GUC_WOPCM_OFFSET_MASK | GUC_WOPCM_OFFSET_VALID | huc_agent;
361 err = intel_uncore_write_and_verify(uncore, DMA_GUC_WOPCM_OFFSET,
362 base | huc_agent, mask,
364 GUC_WOPCM_OFFSET_VALID);
371 i915_probe_error(gt->i915, "Failed to init uC WOPCM registers!\n");
372 i915_probe_error(gt->i915, "%s(%#x)=%#x\n", "DMA_GUC_WOPCM_OFFSET",
373 i915_mmio_reg_offset(DMA_GUC_WOPCM_OFFSET),
374 intel_uncore_read(uncore, DMA_GUC_WOPCM_OFFSET));
375 i915_probe_error(gt->i915, "%s(%#x)=%#x\n", "GUC_WOPCM_SIZE",
376 i915_mmio_reg_offset(GUC_WOPCM_SIZE),
377 intel_uncore_read(uncore, GUC_WOPCM_SIZE));
382 static bool uc_is_wopcm_locked(struct intel_uc *uc)
384 struct intel_gt *gt = uc_to_gt(uc);
385 struct intel_uncore *uncore = gt->uncore;
387 return (intel_uncore_read(uncore, GUC_WOPCM_SIZE) & GUC_WOPCM_SIZE_LOCKED) ||
388 (intel_uncore_read(uncore, DMA_GUC_WOPCM_OFFSET) & GUC_WOPCM_OFFSET_VALID);
391 static int __uc_check_hw(struct intel_uc *uc)
393 if (!intel_uc_supports_guc(uc))
397 * We can silently continue without GuC only if it was never enabled
398 * before on this system after reboot, otherwise we risk GPU hangs.
399 * To check if GuC was loaded before we look at WOPCM registers.
401 if (uc_is_wopcm_locked(uc))
407 static int __uc_init_hw(struct intel_uc *uc)
409 struct drm_i915_private *i915 = uc_to_gt(uc)->i915;
410 struct intel_guc *guc = &uc->guc;
411 struct intel_huc *huc = &uc->huc;
414 GEM_BUG_ON(!intel_uc_supports_guc(uc));
415 GEM_BUG_ON(!intel_uc_uses_guc(uc));
417 if (!intel_uc_fw_is_available(&guc->fw)) {
418 ret = __uc_check_hw(uc) ||
419 intel_uc_fw_is_overridden(&guc->fw) ||
420 intel_uc_supports_guc_submission(uc) ?
421 intel_uc_fw_status_to_error(guc->fw.status) : 0;
425 ret = uc_init_wopcm(uc);
429 guc_reset_interrupts(guc);
431 /* WaEnableuKernelHeaderValidFix:skl */
432 /* WaEnableGuCBootHashCheckNotSet:skl,bxt,kbl */
440 * Always reset the GuC just before (re)loading, so
441 * that the state and timing are fairly predictable
443 ret = __uc_sanitize(uc);
447 intel_huc_fw_upload(huc);
448 intel_guc_ads_reset(guc);
449 intel_guc_write_params(guc);
450 ret = intel_guc_fw_upload(guc);
454 DRM_DEBUG_DRIVER("GuC fw load failed: %d; will reset and "
455 "retry %d more time(s)\n", ret, attempts);
458 /* Did we succeded or run out of retries? */
460 goto err_log_capture;
462 ret = guc_enable_communication(guc);
464 goto err_log_capture;
468 ret = intel_guc_sample_forcewake(guc);
470 goto err_communication;
472 if (intel_uc_supports_guc_submission(uc))
473 intel_guc_submission_enable(guc);
475 dev_info(i915->drm.dev, "%s firmware %s version %u.%u %s:%s\n",
476 intel_uc_fw_type_repr(INTEL_UC_FW_TYPE_GUC), guc->fw.path,
477 guc->fw.major_ver_found, guc->fw.minor_ver_found,
479 enableddisabled(intel_uc_supports_guc_submission(uc)));
481 if (intel_uc_uses_huc(uc)) {
482 dev_info(i915->drm.dev, "%s firmware %s version %u.%u %s:%s\n",
483 intel_uc_fw_type_repr(INTEL_UC_FW_TYPE_HUC),
485 huc->fw.major_ver_found, huc->fw.minor_ver_found,
487 yesno(intel_huc_is_authenticated(huc)));
493 * We've failed to load the firmware :(
496 guc_disable_communication(guc);
498 __uc_capture_load_err_log(uc);
503 dev_notice(i915->drm.dev, "GuC is uninitialized\n");
504 /* We want to run without GuC submission */
508 i915_probe_error(i915, "GuC initialization failed %d\n", ret);
510 /* We want to keep KMS alive */
514 static void __uc_fini_hw(struct intel_uc *uc)
516 struct intel_guc *guc = &uc->guc;
518 if (!intel_guc_is_running(guc))
521 if (intel_uc_supports_guc_submission(uc))
522 intel_guc_submission_disable(guc);
524 if (guc_communication_enabled(guc))
525 guc_disable_communication(guc);
531 * intel_uc_reset_prepare - Prepare for reset
532 * @uc: the intel_uc structure
534 * Preparing for full gpu reset.
536 void intel_uc_reset_prepare(struct intel_uc *uc)
538 struct intel_guc *guc = &uc->guc;
540 if (!intel_guc_is_running(guc))
543 guc_disable_communication(guc);
547 void intel_uc_runtime_suspend(struct intel_uc *uc)
549 struct intel_guc *guc = &uc->guc;
552 if (!intel_guc_is_running(guc))
555 err = intel_guc_suspend(guc);
557 DRM_DEBUG_DRIVER("Failed to suspend GuC, err=%d", err);
559 guc_disable_communication(guc);
562 void intel_uc_suspend(struct intel_uc *uc)
564 struct intel_guc *guc = &uc->guc;
565 intel_wakeref_t wakeref;
567 if (!intel_guc_is_running(guc))
570 with_intel_runtime_pm(uc_to_gt(uc)->uncore->rpm, wakeref)
571 intel_uc_runtime_suspend(uc);
574 static int __uc_resume(struct intel_uc *uc, bool enable_communication)
576 struct intel_guc *guc = &uc->guc;
579 if (!intel_guc_is_running(guc))
582 /* Make sure we enable communication if and only if it's disabled */
583 GEM_BUG_ON(enable_communication == guc_communication_enabled(guc));
585 if (enable_communication)
586 guc_enable_communication(guc);
588 err = intel_guc_resume(guc);
590 DRM_DEBUG_DRIVER("Failed to resume GuC, err=%d", err);
597 int intel_uc_resume(struct intel_uc *uc)
600 * When coming out of S3/S4 we sanitize and re-init the HW, so
601 * communication is already re-enabled at this point.
603 return __uc_resume(uc, false);
606 int intel_uc_runtime_resume(struct intel_uc *uc)
609 * During runtime resume we don't sanitize, so we need to re-init
610 * communication as well.
612 return __uc_resume(uc, true);
615 static const struct intel_uc_ops uc_ops_off = {
616 .init_hw = __uc_check_hw,
619 static const struct intel_uc_ops uc_ops_on = {
620 .init_hw = __uc_init_hw,
621 .fini_hw = __uc_fini_hw,