1 // SPDX-License-Identifier: MIT
3 * Copyright © 2014-2019 Intel Corporation
6 #include "gt/intel_gt.h"
7 #include "gt/intel_gt_irq.h"
8 #include "gt/intel_gt_pm_irq.h"
10 #include "intel_guc_ads.h"
11 #include "intel_guc_submission.h"
17 * The GuC is a microcontroller inside the GT HW, introduced in gen9. The GuC is
18 * designed to offload some of the functionality usually performed by the host
19 * driver; currently the main operations it can take care of are:
21 * - Authentication of the HuC, which is required to fully enable HuC usage.
22 * - Low latency graphics context scheduling (a.k.a. GuC submission).
23 * - GT Power management.
25 * The enable_guc module parameter can be used to select which of those
26 * operations to enable within GuC. Note that not all the operations are
27 * supported on all gen9+ platforms.
29 * Enabling the GuC is not mandatory and therefore the firmware is only loaded
30 * if at least one of the operations is selected. However, not loading the GuC
31 * might result in the loss of some features that do require the GuC (currently
32 * just the HuC, but more are expected to land in the future).
35 void intel_guc_notify(struct intel_guc *guc)
37 struct intel_gt *gt = guc_to_gt(guc);
40 * On Gen11+, the value written to the register is passes as a payload
41 * to the FW. However, the FW currently treats all values the same way
42 * (H2G interrupt), so we can just write the value that the HW expects
45 intel_uncore_write(gt->uncore, guc->notify_reg, GUC_SEND_TRIGGER);
48 static inline i915_reg_t guc_send_reg(struct intel_guc *guc, u32 i)
50 GEM_BUG_ON(!guc->send_regs.base);
51 GEM_BUG_ON(!guc->send_regs.count);
52 GEM_BUG_ON(i >= guc->send_regs.count);
54 return _MMIO(guc->send_regs.base + 4 * i);
57 void intel_guc_init_send_regs(struct intel_guc *guc)
59 struct intel_gt *gt = guc_to_gt(guc);
60 enum forcewake_domains fw_domains = 0;
63 GEM_BUG_ON(!guc->send_regs.base);
64 GEM_BUG_ON(!guc->send_regs.count);
66 for (i = 0; i < guc->send_regs.count; i++) {
67 fw_domains |= intel_uncore_forcewake_for_reg(gt->uncore,
69 FW_REG_READ | FW_REG_WRITE);
71 guc->send_regs.fw_domains = fw_domains;
74 static void gen9_reset_guc_interrupts(struct intel_guc *guc)
76 struct intel_gt *gt = guc_to_gt(guc);
78 assert_rpm_wakelock_held(>->i915->runtime_pm);
80 spin_lock_irq(>->irq_lock);
81 gen6_gt_pm_reset_iir(gt, gt->pm_guc_events);
82 spin_unlock_irq(>->irq_lock);
85 static void gen9_enable_guc_interrupts(struct intel_guc *guc)
87 struct intel_gt *gt = guc_to_gt(guc);
89 assert_rpm_wakelock_held(>->i915->runtime_pm);
91 spin_lock_irq(>->irq_lock);
92 WARN_ON_ONCE(intel_uncore_read(gt->uncore, GEN8_GT_IIR(2)) &
94 gen6_gt_pm_enable_irq(gt, gt->pm_guc_events);
95 spin_unlock_irq(>->irq_lock);
98 static void gen9_disable_guc_interrupts(struct intel_guc *guc)
100 struct intel_gt *gt = guc_to_gt(guc);
102 assert_rpm_wakelock_held(>->i915->runtime_pm);
104 spin_lock_irq(>->irq_lock);
106 gen6_gt_pm_disable_irq(gt, gt->pm_guc_events);
108 spin_unlock_irq(>->irq_lock);
109 intel_synchronize_irq(gt->i915);
111 gen9_reset_guc_interrupts(guc);
114 static void gen11_reset_guc_interrupts(struct intel_guc *guc)
116 struct intel_gt *gt = guc_to_gt(guc);
118 spin_lock_irq(>->irq_lock);
119 gen11_gt_reset_one_iir(gt, 0, GEN11_GUC);
120 spin_unlock_irq(>->irq_lock);
123 static void gen11_enable_guc_interrupts(struct intel_guc *guc)
125 struct intel_gt *gt = guc_to_gt(guc);
126 u32 events = REG_FIELD_PREP(ENGINE1_MASK, GUC_INTR_GUC2HOST);
128 spin_lock_irq(>->irq_lock);
129 WARN_ON_ONCE(gen11_gt_reset_one_iir(gt, 0, GEN11_GUC));
130 intel_uncore_write(gt->uncore,
131 GEN11_GUC_SG_INTR_ENABLE, events);
132 intel_uncore_write(gt->uncore,
133 GEN11_GUC_SG_INTR_MASK, ~events);
134 spin_unlock_irq(>->irq_lock);
137 static void gen11_disable_guc_interrupts(struct intel_guc *guc)
139 struct intel_gt *gt = guc_to_gt(guc);
141 spin_lock_irq(>->irq_lock);
143 intel_uncore_write(gt->uncore, GEN11_GUC_SG_INTR_MASK, ~0);
144 intel_uncore_write(gt->uncore, GEN11_GUC_SG_INTR_ENABLE, 0);
146 spin_unlock_irq(>->irq_lock);
147 intel_synchronize_irq(gt->i915);
149 gen11_reset_guc_interrupts(guc);
152 void intel_guc_init_early(struct intel_guc *guc)
154 struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
156 intel_uc_fw_init_early(&guc->fw, INTEL_UC_FW_TYPE_GUC);
157 intel_guc_ct_init_early(&guc->ct);
158 intel_guc_log_init_early(&guc->log);
159 intel_guc_submission_init_early(guc);
161 mutex_init(&guc->send_mutex);
162 spin_lock_init(&guc->irq_lock);
163 if (GRAPHICS_VER(i915) >= 11) {
164 guc->notify_reg = GEN11_GUC_HOST_INTERRUPT;
165 guc->interrupts.reset = gen11_reset_guc_interrupts;
166 guc->interrupts.enable = gen11_enable_guc_interrupts;
167 guc->interrupts.disable = gen11_disable_guc_interrupts;
168 guc->send_regs.base =
169 i915_mmio_reg_offset(GEN11_SOFT_SCRATCH(0));
170 guc->send_regs.count = GEN11_SOFT_SCRATCH_COUNT;
173 guc->notify_reg = GUC_SEND_INTERRUPT;
174 guc->interrupts.reset = gen9_reset_guc_interrupts;
175 guc->interrupts.enable = gen9_enable_guc_interrupts;
176 guc->interrupts.disable = gen9_disable_guc_interrupts;
177 guc->send_regs.base = i915_mmio_reg_offset(SOFT_SCRATCH(0));
178 guc->send_regs.count = GUC_MAX_MMIO_MSG_LEN;
179 BUILD_BUG_ON(GUC_MAX_MMIO_MSG_LEN > SOFT_SCRATCH_COUNT);
183 static u32 guc_ctl_debug_flags(struct intel_guc *guc)
185 u32 level = intel_guc_log_get_level(&guc->log);
188 if (!GUC_LOG_LEVEL_IS_VERBOSE(level))
189 flags |= GUC_LOG_DISABLED;
191 flags |= GUC_LOG_LEVEL_TO_VERBOSITY(level) <<
192 GUC_LOG_VERBOSITY_SHIFT;
197 static u32 guc_ctl_feature_flags(struct intel_guc *guc)
201 if (!intel_guc_submission_is_used(guc))
202 flags |= GUC_CTL_DISABLE_SCHEDULER;
207 static u32 guc_ctl_log_params_flags(struct intel_guc *guc)
209 u32 offset = intel_guc_ggtt_offset(guc, guc->log.vma) >> PAGE_SHIFT;
212 #if (((CRASH_BUFFER_SIZE) % SZ_1M) == 0)
214 #define FLAG GUC_LOG_ALLOC_IN_MEGABYTE
220 BUILD_BUG_ON(!CRASH_BUFFER_SIZE);
221 BUILD_BUG_ON(!IS_ALIGNED(CRASH_BUFFER_SIZE, UNIT));
222 BUILD_BUG_ON(!DPC_BUFFER_SIZE);
223 BUILD_BUG_ON(!IS_ALIGNED(DPC_BUFFER_SIZE, UNIT));
224 BUILD_BUG_ON(!ISR_BUFFER_SIZE);
225 BUILD_BUG_ON(!IS_ALIGNED(ISR_BUFFER_SIZE, UNIT));
227 BUILD_BUG_ON((CRASH_BUFFER_SIZE / UNIT - 1) >
228 (GUC_LOG_CRASH_MASK >> GUC_LOG_CRASH_SHIFT));
229 BUILD_BUG_ON((DPC_BUFFER_SIZE / UNIT - 1) >
230 (GUC_LOG_DPC_MASK >> GUC_LOG_DPC_SHIFT));
231 BUILD_BUG_ON((ISR_BUFFER_SIZE / UNIT - 1) >
232 (GUC_LOG_ISR_MASK >> GUC_LOG_ISR_SHIFT));
234 flags = GUC_LOG_VALID |
235 GUC_LOG_NOTIFY_ON_HALF_FULL |
237 ((CRASH_BUFFER_SIZE / UNIT - 1) << GUC_LOG_CRASH_SHIFT) |
238 ((DPC_BUFFER_SIZE / UNIT - 1) << GUC_LOG_DPC_SHIFT) |
239 ((ISR_BUFFER_SIZE / UNIT - 1) << GUC_LOG_ISR_SHIFT) |
240 (offset << GUC_LOG_BUF_ADDR_SHIFT);
248 static u32 guc_ctl_ads_flags(struct intel_guc *guc)
250 u32 ads = intel_guc_ggtt_offset(guc, guc->ads_vma) >> PAGE_SHIFT;
251 u32 flags = ads << GUC_ADS_ADDR_SHIFT;
257 * Initialise the GuC parameter block before starting the firmware
258 * transfer. These parameters are read by the firmware on startup
259 * and cannot be changed thereafter.
261 static void guc_init_params(struct intel_guc *guc)
263 u32 *params = guc->params;
266 BUILD_BUG_ON(sizeof(guc->params) != GUC_CTL_MAX_DWORDS * sizeof(u32));
268 params[GUC_CTL_LOG_PARAMS] = guc_ctl_log_params_flags(guc);
269 params[GUC_CTL_FEATURE] = guc_ctl_feature_flags(guc);
270 params[GUC_CTL_DEBUG] = guc_ctl_debug_flags(guc);
271 params[GUC_CTL_ADS] = guc_ctl_ads_flags(guc);
273 for (i = 0; i < GUC_CTL_MAX_DWORDS; i++)
274 DRM_DEBUG_DRIVER("param[%2d] = %#x\n", i, params[i]);
278 * Initialise the GuC parameter block before starting the firmware
279 * transfer. These parameters are read by the firmware on startup
280 * and cannot be changed thereafter.
282 void intel_guc_write_params(struct intel_guc *guc)
284 struct intel_uncore *uncore = guc_to_gt(guc)->uncore;
288 * All SOFT_SCRATCH registers are in FORCEWAKE_GT domain and
289 * they are power context saved so it's ok to release forcewake
290 * when we are done here and take it again at xfer time.
292 intel_uncore_forcewake_get(uncore, FORCEWAKE_GT);
294 intel_uncore_write(uncore, SOFT_SCRATCH(0), 0);
296 for (i = 0; i < GUC_CTL_MAX_DWORDS; i++)
297 intel_uncore_write(uncore, SOFT_SCRATCH(1 + i), guc->params[i]);
299 intel_uncore_forcewake_put(uncore, FORCEWAKE_GT);
302 int intel_guc_init(struct intel_guc *guc)
304 struct intel_gt *gt = guc_to_gt(guc);
307 ret = intel_uc_fw_init(&guc->fw);
311 ret = intel_guc_log_create(&guc->log);
315 ret = intel_guc_ads_create(guc);
318 GEM_BUG_ON(!guc->ads_vma);
320 ret = intel_guc_ct_init(&guc->ct);
324 if (intel_guc_submission_is_used(guc)) {
326 * This is stuff we need to have available at fw load time
327 * if we are planning to enable submission later
329 ret = intel_guc_submission_init(guc);
334 /* now that everything is perma-pinned, initialize the parameters */
335 guc_init_params(guc);
337 /* We need to notify the guc whenever we change the GGTT */
338 i915_ggtt_enable_guc(gt->ggtt);
340 intel_uc_fw_change_status(&guc->fw, INTEL_UC_FIRMWARE_LOADABLE);
345 intel_guc_ct_fini(&guc->ct);
347 intel_guc_ads_destroy(guc);
349 intel_guc_log_destroy(&guc->log);
351 intel_uc_fw_fini(&guc->fw);
353 i915_probe_error(gt->i915, "failed with %d\n", ret);
357 void intel_guc_fini(struct intel_guc *guc)
359 struct intel_gt *gt = guc_to_gt(guc);
361 if (!intel_uc_fw_is_loadable(&guc->fw))
364 i915_ggtt_disable_guc(gt->ggtt);
366 if (intel_guc_submission_is_used(guc))
367 intel_guc_submission_fini(guc);
369 intel_guc_ct_fini(&guc->ct);
371 intel_guc_ads_destroy(guc);
372 intel_guc_log_destroy(&guc->log);
373 intel_uc_fw_fini(&guc->fw);
377 * This function implements the MMIO based host to GuC interface.
379 int intel_guc_send_mmio(struct intel_guc *guc, const u32 *action, u32 len,
380 u32 *response_buf, u32 response_buf_size)
382 struct intel_uncore *uncore = guc_to_gt(guc)->uncore;
388 GEM_BUG_ON(len > guc->send_regs.count);
390 /* We expect only action code */
391 GEM_BUG_ON(*action & ~INTEL_GUC_MSG_CODE_MASK);
393 /* If CT is available, we expect to use MMIO only during init/fini */
394 GEM_BUG_ON(*action != INTEL_GUC_ACTION_REGISTER_COMMAND_TRANSPORT_BUFFER &&
395 *action != INTEL_GUC_ACTION_DEREGISTER_COMMAND_TRANSPORT_BUFFER);
397 mutex_lock(&guc->send_mutex);
398 intel_uncore_forcewake_get(uncore, guc->send_regs.fw_domains);
400 for (i = 0; i < len; i++)
401 intel_uncore_write(uncore, guc_send_reg(guc, i), action[i]);
403 intel_uncore_posting_read(uncore, guc_send_reg(guc, i - 1));
405 intel_guc_notify(guc);
408 * No GuC command should ever take longer than 10ms.
409 * Fast commands should still complete in 10us.
411 ret = __intel_wait_for_register_fw(uncore,
412 guc_send_reg(guc, 0),
413 INTEL_GUC_MSG_TYPE_MASK,
414 INTEL_GUC_MSG_TYPE_RESPONSE <<
415 INTEL_GUC_MSG_TYPE_SHIFT,
417 /* If GuC explicitly returned an error, convert it to -EIO */
418 if (!ret && !INTEL_GUC_MSG_IS_RESPONSE_SUCCESS(status))
422 DRM_ERROR("MMIO: GuC action %#x failed with error %d %#x\n",
423 action[0], ret, status);
428 int count = min(response_buf_size, guc->send_regs.count - 1);
430 for (i = 0; i < count; i++)
431 response_buf[i] = intel_uncore_read(uncore,
432 guc_send_reg(guc, i + 1));
435 /* Use data from the GuC response as our return value */
436 ret = INTEL_GUC_MSG_TO_DATA(status);
439 intel_uncore_forcewake_put(uncore, guc->send_regs.fw_domains);
440 mutex_unlock(&guc->send_mutex);
445 int intel_guc_to_host_process_recv_msg(struct intel_guc *guc,
446 const u32 *payload, u32 len)
453 /* Make sure to handle only enabled messages */
454 msg = payload[0] & guc->msg_enabled_mask;
456 if (msg & (INTEL_GUC_RECV_MSG_FLUSH_LOG_BUFFER |
457 INTEL_GUC_RECV_MSG_CRASH_DUMP_POSTED))
458 intel_guc_log_handle_flush_event(&guc->log);
464 * intel_guc_auth_huc() - Send action to GuC to authenticate HuC ucode
465 * @guc: intel_guc structure
466 * @rsa_offset: rsa offset w.r.t ggtt base of huc vma
468 * Triggers a HuC firmware authentication request to the GuC via intel_guc_send
469 * INTEL_GUC_ACTION_AUTHENTICATE_HUC interface. This function is invoked by
472 * Return: non-zero code on error
474 int intel_guc_auth_huc(struct intel_guc *guc, u32 rsa_offset)
477 INTEL_GUC_ACTION_AUTHENTICATE_HUC,
481 return intel_guc_send(guc, action, ARRAY_SIZE(action));
485 * intel_guc_suspend() - notify GuC entering suspend state
488 int intel_guc_suspend(struct intel_guc *guc)
490 struct intel_uncore *uncore = guc_to_gt(guc)->uncore;
494 INTEL_GUC_ACTION_ENTER_S_STATE,
495 GUC_POWER_D1, /* any value greater than GUC_POWER_D0 */
499 * If GuC communication is enabled but submission is not supported,
500 * we do not need to suspend the GuC.
502 if (!intel_guc_submission_is_used(guc) || !intel_guc_is_ready(guc))
506 * The ENTER_S_STATE action queues the save/restore operation in GuC FW
507 * and then returns, so waiting on the H2G is not enough to guarantee
508 * GuC is done. When all the processing is done, GuC writes
509 * INTEL_GUC_SLEEP_STATE_SUCCESS to scratch register 14, so we can poll
510 * on that. Note that GuC does not ensure that the value in the register
511 * is different from INTEL_GUC_SLEEP_STATE_SUCCESS while the action is
512 * in progress so we need to take care of that ourselves as well.
515 intel_uncore_write(uncore, SOFT_SCRATCH(14),
516 INTEL_GUC_SLEEP_STATE_INVALID_MASK);
518 ret = intel_guc_send(guc, action, ARRAY_SIZE(action));
522 ret = __intel_wait_for_register(uncore, SOFT_SCRATCH(14),
523 INTEL_GUC_SLEEP_STATE_INVALID_MASK,
528 if (status != INTEL_GUC_SLEEP_STATE_SUCCESS) {
529 DRM_ERROR("GuC failed to change sleep state. "
530 "action=0x%x, err=%u\n",
539 * intel_guc_reset_engine() - ask GuC to reset an engine
540 * @guc: intel_guc structure
541 * @engine: engine to be reset
543 int intel_guc_reset_engine(struct intel_guc *guc,
544 struct intel_engine_cs *engine)
546 /* XXX: to be implemented with submission interface rework */
552 * intel_guc_resume() - notify GuC resuming from suspend state
555 int intel_guc_resume(struct intel_guc *guc)
557 /* XXX: to be implemented with submission interface rework */
562 * DOC: GuC Memory Management
564 * GuC can't allocate any memory for its own usage, so all the allocations must
565 * be handled by the host driver. GuC accesses the memory via the GGTT, with the
566 * exception of the top and bottom parts of the 4GB address space, which are
567 * instead re-mapped by the GuC HW to memory location of the FW itself (WOPCM)
568 * or other parts of the HW. The driver must take care not to place objects that
569 * the GuC is going to access in these reserved ranges. The layout of the GuC
570 * address space is shown below:
574 * +===========> +====================+ <== FFFF_FFFF
576 * | +====================+ <== GUC_GGTT_TOP
580 * Address +===> +====================+ <== GuC ggtt_pin_bias
588 * +=======+===> +====================+ <== 0000_0000
590 * The lower part of GuC Address Space [0, ggtt_pin_bias) is mapped to GuC WOPCM
591 * while upper part of GuC Address Space [ggtt_pin_bias, GUC_GGTT_TOP) is mapped
592 * to DRAM. The value of the GuC ggtt_pin_bias is the GuC WOPCM size.
596 * intel_guc_allocate_vma() - Allocate a GGTT VMA for GuC usage
598 * @size: size of area to allocate (both virtual space and memory)
600 * This is a wrapper to create an object for use with the GuC. In order to
601 * use it inside the GuC, an object needs to be pinned lifetime, so we allocate
602 * both some backing storage and a range inside the Global GTT. We must pin
603 * it in the GGTT somewhere other than than [0, GUC ggtt_pin_bias) because that
604 * range is reserved inside GuC.
606 * Return: A i915_vma if successful, otherwise an ERR_PTR.
608 struct i915_vma *intel_guc_allocate_vma(struct intel_guc *guc, u32 size)
610 struct intel_gt *gt = guc_to_gt(guc);
611 struct drm_i915_gem_object *obj;
612 struct i915_vma *vma;
616 obj = i915_gem_object_create_shmem(gt->i915, size);
618 return ERR_CAST(obj);
620 vma = i915_vma_instance(obj, >->ggtt->vm, NULL);
624 flags = PIN_OFFSET_BIAS | i915_ggtt_pin_bias(vma);
625 ret = i915_ggtt_pin(vma, NULL, 0, flags);
631 return i915_vma_make_unshrinkable(vma);
634 i915_gem_object_put(obj);
639 * intel_guc_allocate_and_map_vma() - Allocate and map VMA for GuC usage
641 * @size: size of area to allocate (both virtual space and memory)
642 * @out_vma: return variable for the allocated vma pointer
643 * @out_vaddr: return variable for the obj mapping
645 * This wrapper calls intel_guc_allocate_vma() and then maps the allocated
646 * object with I915_MAP_WB.
648 * Return: 0 if successful, a negative errno code otherwise.
650 int intel_guc_allocate_and_map_vma(struct intel_guc *guc, u32 size,
651 struct i915_vma **out_vma, void **out_vaddr)
653 struct i915_vma *vma;
656 vma = intel_guc_allocate_vma(guc, size);
660 vaddr = i915_gem_object_pin_map_unlocked(vma->obj,
661 i915_coherent_map_type(guc_to_gt(guc)->i915,
664 i915_vma_unpin_and_release(&vma, 0);
665 return PTR_ERR(vaddr);
675 * intel_guc_load_status - dump information about GuC load status
677 * @p: the &drm_printer
679 * Pretty printer for GuC load status.
681 void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p)
683 struct intel_gt *gt = guc_to_gt(guc);
684 struct intel_uncore *uncore = gt->uncore;
685 intel_wakeref_t wakeref;
687 if (!intel_guc_is_supported(guc)) {
688 drm_printf(p, "GuC not supported\n");
692 if (!intel_guc_is_wanted(guc)) {
693 drm_printf(p, "GuC disabled\n");
697 intel_uc_fw_dump(&guc->fw, p);
699 with_intel_runtime_pm(uncore->rpm, wakeref) {
700 u32 status = intel_uncore_read(uncore, GUC_STATUS);
703 drm_printf(p, "\nGuC status 0x%08x:\n", status);
704 drm_printf(p, "\tBootrom status = 0x%x\n",
705 (status & GS_BOOTROM_MASK) >> GS_BOOTROM_SHIFT);
706 drm_printf(p, "\tuKernel status = 0x%x\n",
707 (status & GS_UKERNEL_MASK) >> GS_UKERNEL_SHIFT);
708 drm_printf(p, "\tMIA Core status = 0x%x\n",
709 (status & GS_MIA_MASK) >> GS_MIA_SHIFT);
710 drm_puts(p, "\nScratch registers:\n");
711 for (i = 0; i < 16; i++) {
712 drm_printf(p, "\t%2d: \t0x%x\n",
713 i, intel_uncore_read(uncore, SOFT_SCRATCH(i)));