2 * Copyright © 2014-2017 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #include "intel_guc.h"
26 #include "intel_guc_ads.h"
27 #include "intel_guc_submission.h"
30 static void gen8_guc_raise_irq(struct intel_guc *guc)
32 struct drm_i915_private *dev_priv = guc_to_i915(guc);
34 I915_WRITE(GUC_SEND_INTERRUPT, GUC_SEND_TRIGGER);
37 static inline i915_reg_t guc_send_reg(struct intel_guc *guc, u32 i)
39 GEM_BUG_ON(!guc->send_regs.base);
40 GEM_BUG_ON(!guc->send_regs.count);
41 GEM_BUG_ON(i >= guc->send_regs.count);
43 return _MMIO(guc->send_regs.base + 4 * i);
46 void intel_guc_init_send_regs(struct intel_guc *guc)
48 struct drm_i915_private *dev_priv = guc_to_i915(guc);
49 enum forcewake_domains fw_domains = 0;
52 guc->send_regs.base = i915_mmio_reg_offset(SOFT_SCRATCH(0));
53 guc->send_regs.count = SOFT_SCRATCH_COUNT - 1;
55 for (i = 0; i < guc->send_regs.count; i++) {
56 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
58 FW_REG_READ | FW_REG_WRITE);
60 guc->send_regs.fw_domains = fw_domains;
63 void intel_guc_init_early(struct intel_guc *guc)
65 intel_guc_fw_init_early(guc);
66 intel_guc_ct_init_early(&guc->ct);
67 intel_guc_log_init_early(&guc->log);
69 mutex_init(&guc->send_mutex);
70 spin_lock_init(&guc->irq_lock);
71 guc->send = intel_guc_send_nop;
72 guc->handler = intel_guc_to_host_event_handler_nop;
73 guc->notify = gen8_guc_raise_irq;
76 int intel_guc_init_wq(struct intel_guc *guc)
78 struct drm_i915_private *dev_priv = guc_to_i915(guc);
81 * GuC log buffer flush work item has to do register access to
82 * send the ack to GuC and this work item, if not synced before
83 * suspend, can potentially get executed after the GFX device is
85 * By marking the WQ as freezable, we don't have to bother about
86 * flushing of this work item from the suspend hooks, the pending
87 * work item if any will be either executed before the suspend
88 * or scheduled later on resume. This way the handling of work
89 * item can be kept same between system suspend & rpm suspend.
91 guc->log.relay.flush_wq =
92 alloc_ordered_workqueue("i915-guc_log",
93 WQ_HIGHPRI | WQ_FREEZABLE);
94 if (!guc->log.relay.flush_wq) {
95 DRM_ERROR("Couldn't allocate workqueue for GuC log\n");
100 * Even though both sending GuC action, and adding a new workitem to
101 * GuC workqueue are serialized (each with its own locking), since
102 * we're using mutliple engines, it's possible that we're going to
103 * issue a preempt request with two (or more - each for different
104 * engine) workitems in GuC queue. In this situation, GuC may submit
105 * all of them, which will make us very confused.
106 * Our preemption contexts may even already be complete - before we
107 * even had the chance to sent the preempt action to GuC!. Rather
108 * than introducing yet another lock, we can just use ordered workqueue
109 * to make sure we're always sending a single preemption request with a
112 if (HAS_LOGICAL_RING_PREEMPTION(dev_priv) &&
113 USES_GUC_SUBMISSION(dev_priv)) {
114 guc->preempt_wq = alloc_ordered_workqueue("i915-guc_preempt",
116 if (!guc->preempt_wq) {
117 destroy_workqueue(guc->log.relay.flush_wq);
118 DRM_ERROR("Couldn't allocate workqueue for GuC "
127 void intel_guc_fini_wq(struct intel_guc *guc)
129 struct drm_i915_private *dev_priv = guc_to_i915(guc);
131 if (HAS_LOGICAL_RING_PREEMPTION(dev_priv) &&
132 USES_GUC_SUBMISSION(dev_priv))
133 destroy_workqueue(guc->preempt_wq);
135 destroy_workqueue(guc->log.relay.flush_wq);
138 static int guc_shared_data_create(struct intel_guc *guc)
140 struct i915_vma *vma;
143 vma = intel_guc_allocate_vma(guc, PAGE_SIZE);
147 vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
149 i915_vma_unpin_and_release(&vma);
150 return PTR_ERR(vaddr);
153 guc->shared_data = vma;
154 guc->shared_data_vaddr = vaddr;
159 static void guc_shared_data_destroy(struct intel_guc *guc)
161 i915_gem_object_unpin_map(guc->shared_data->obj);
162 i915_vma_unpin_and_release(&guc->shared_data);
165 int intel_guc_init(struct intel_guc *guc)
167 struct drm_i915_private *dev_priv = guc_to_i915(guc);
170 ret = guc_shared_data_create(guc);
173 GEM_BUG_ON(!guc->shared_data);
175 ret = intel_guc_log_create(&guc->log);
179 ret = intel_guc_ads_create(guc);
182 GEM_BUG_ON(!guc->ads_vma);
184 /* We need to notify the guc whenever we change the GGTT */
185 i915_ggtt_enable_guc(dev_priv);
190 intel_guc_log_destroy(&guc->log);
192 guc_shared_data_destroy(guc);
196 void intel_guc_fini(struct intel_guc *guc)
198 struct drm_i915_private *dev_priv = guc_to_i915(guc);
200 i915_ggtt_disable_guc(dev_priv);
201 intel_guc_ads_destroy(guc);
202 intel_guc_log_destroy(&guc->log);
203 guc_shared_data_destroy(guc);
206 static u32 get_log_control_flags(void)
208 u32 level = i915_modparams.guc_log_level;
211 GEM_BUG_ON(level < 0);
213 if (!GUC_LOG_LEVEL_IS_ENABLED(level))
214 flags |= GUC_LOG_DEFAULT_DISABLED;
216 if (!GUC_LOG_LEVEL_IS_VERBOSE(level))
217 flags |= GUC_LOG_DISABLED;
219 flags |= GUC_LOG_LEVEL_TO_VERBOSITY(level) <<
220 GUC_LOG_VERBOSITY_SHIFT;
226 * Initialise the GuC parameter block before starting the firmware
227 * transfer. These parameters are read by the firmware on startup
228 * and cannot be changed thereafter.
230 void intel_guc_init_params(struct intel_guc *guc)
232 struct drm_i915_private *dev_priv = guc_to_i915(guc);
233 u32 params[GUC_CTL_MAX_DWORDS];
236 memset(params, 0, sizeof(params));
239 * GuC ARAT increment is 10 ns. GuC default scheduler quantum is one
240 * second. This ARAR is calculated by:
241 * Scheduler-Quantum-in-ns / ARAT-increment-in-ns = 1000000000 / 10
243 params[GUC_CTL_ARAT_HIGH] = 0;
244 params[GUC_CTL_ARAT_LOW] = 100000000;
246 params[GUC_CTL_WA] |= GUC_CTL_WA_UK_BY_DRIVER;
248 params[GUC_CTL_FEATURE] |= GUC_CTL_DISABLE_SCHEDULER |
249 GUC_CTL_VCS2_ENABLED;
251 params[GUC_CTL_LOG_PARAMS] = guc->log.flags;
253 params[GUC_CTL_DEBUG] = get_log_control_flags();
255 /* If GuC submission is enabled, set up additional parameters here */
256 if (USES_GUC_SUBMISSION(dev_priv)) {
257 u32 ads = intel_guc_ggtt_offset(guc,
258 guc->ads_vma) >> PAGE_SHIFT;
259 u32 pgs = intel_guc_ggtt_offset(guc, guc->stage_desc_pool);
260 u32 ctx_in_16 = GUC_MAX_STAGE_DESCRIPTORS / 16;
262 params[GUC_CTL_DEBUG] |= ads << GUC_ADS_ADDR_SHIFT;
263 params[GUC_CTL_DEBUG] |= GUC_ADS_ENABLED;
266 params[GUC_CTL_CTXINFO] = (pgs << GUC_CTL_BASE_ADDR_SHIFT) |
267 (ctx_in_16 << GUC_CTL_CTXNUM_IN16_SHIFT);
269 params[GUC_CTL_FEATURE] |= GUC_CTL_KERNEL_SUBMISSIONS;
271 /* Unmask this bit to enable the GuC's internal scheduler */
272 params[GUC_CTL_FEATURE] &= ~GUC_CTL_DISABLE_SCHEDULER;
276 * All SOFT_SCRATCH registers are in FORCEWAKE_BLITTER domain and
277 * they are power context saved so it's ok to release forcewake
278 * when we are done here and take it again at xfer time.
280 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_BLITTER);
282 I915_WRITE(SOFT_SCRATCH(0), 0);
284 for (i = 0; i < GUC_CTL_MAX_DWORDS; i++)
285 I915_WRITE(SOFT_SCRATCH(1 + i), params[i]);
287 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_BLITTER);
290 int intel_guc_send_nop(struct intel_guc *guc, const u32 *action, u32 len,
291 u32 *response_buf, u32 response_buf_size)
293 WARN(1, "Unexpected send: action=%#x\n", *action);
297 void intel_guc_to_host_event_handler_nop(struct intel_guc *guc)
299 WARN(1, "Unexpected event: no suitable handler\n");
303 * This function implements the MMIO based host to GuC interface.
305 int intel_guc_send_mmio(struct intel_guc *guc, const u32 *action, u32 len,
306 u32 *response_buf, u32 response_buf_size)
308 struct drm_i915_private *dev_priv = guc_to_i915(guc);
314 GEM_BUG_ON(len > guc->send_regs.count);
316 /* We expect only action code */
317 GEM_BUG_ON(*action & ~INTEL_GUC_MSG_CODE_MASK);
319 /* If CT is available, we expect to use MMIO only during init/fini */
320 GEM_BUG_ON(HAS_GUC_CT(dev_priv) &&
321 *action != INTEL_GUC_ACTION_REGISTER_COMMAND_TRANSPORT_BUFFER &&
322 *action != INTEL_GUC_ACTION_DEREGISTER_COMMAND_TRANSPORT_BUFFER);
324 mutex_lock(&guc->send_mutex);
325 intel_uncore_forcewake_get(dev_priv, guc->send_regs.fw_domains);
327 for (i = 0; i < len; i++)
328 I915_WRITE(guc_send_reg(guc, i), action[i]);
330 POSTING_READ(guc_send_reg(guc, i - 1));
332 intel_guc_notify(guc);
335 * No GuC command should ever take longer than 10ms.
336 * Fast commands should still complete in 10us.
338 ret = __intel_wait_for_register_fw(dev_priv,
339 guc_send_reg(guc, 0),
340 INTEL_GUC_MSG_TYPE_MASK,
341 INTEL_GUC_MSG_TYPE_RESPONSE <<
342 INTEL_GUC_MSG_TYPE_SHIFT,
344 /* If GuC explicitly returned an error, convert it to -EIO */
345 if (!ret && !INTEL_GUC_MSG_IS_RESPONSE_SUCCESS(status))
349 DRM_DEBUG_DRIVER("INTEL_GUC_SEND: Action 0x%X failed;"
350 " ret=%d status=0x%08X response=0x%08X\n",
351 action[0], ret, status,
352 I915_READ(SOFT_SCRATCH(15)));
357 int count = min(response_buf_size, guc->send_regs.count - 1);
359 for (i = 0; i < count; i++)
360 response_buf[i] = I915_READ(guc_send_reg(guc, i + 1));
363 /* Use data from the GuC response as our return value */
364 ret = INTEL_GUC_MSG_TO_DATA(status);
367 intel_uncore_forcewake_put(dev_priv, guc->send_regs.fw_domains);
368 mutex_unlock(&guc->send_mutex);
373 void intel_guc_to_host_event_handler_mmio(struct intel_guc *guc)
375 struct drm_i915_private *dev_priv = guc_to_i915(guc);
379 * Sample the log buffer flush related bits & clear them out now
380 * itself from the message identity register to minimize the
381 * probability of losing a flush interrupt, when there are back
382 * to back flush interrupts.
383 * There can be a new flush interrupt, for different log buffer
384 * type (like for ISR), whilst Host is handling one (for DPC).
385 * Since same bit is used in message register for ISR & DPC, it
386 * could happen that GuC sets the bit for 2nd interrupt but Host
387 * clears out the bit on handling the 1st interrupt.
389 spin_lock(&guc->irq_lock);
390 val = I915_READ(SOFT_SCRATCH(15));
391 msg = val & guc->msg_enabled_mask;
392 I915_WRITE(SOFT_SCRATCH(15), val & ~msg);
393 spin_unlock(&guc->irq_lock);
395 intel_guc_to_host_process_recv_msg(guc, msg);
398 void intel_guc_to_host_process_recv_msg(struct intel_guc *guc, u32 msg)
400 /* Make sure to handle only enabled messages */
401 msg &= guc->msg_enabled_mask;
403 if (msg & (INTEL_GUC_RECV_MSG_FLUSH_LOG_BUFFER |
404 INTEL_GUC_RECV_MSG_CRASH_DUMP_POSTED))
405 intel_guc_log_handle_flush_event(&guc->log);
408 int intel_guc_sample_forcewake(struct intel_guc *guc)
410 struct drm_i915_private *dev_priv = guc_to_i915(guc);
413 action[0] = INTEL_GUC_ACTION_SAMPLE_FORCEWAKE;
414 /* WaRsDisableCoarsePowerGating:skl,cnl */
415 if (!HAS_RC6(dev_priv) || NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
418 /* bit 0 and 1 are for Render and Media domain separately */
419 action[1] = GUC_FORCEWAKE_RENDER | GUC_FORCEWAKE_MEDIA;
421 return intel_guc_send(guc, action, ARRAY_SIZE(action));
425 * intel_guc_auth_huc() - Send action to GuC to authenticate HuC ucode
426 * @guc: intel_guc structure
427 * @rsa_offset: rsa offset w.r.t ggtt base of huc vma
429 * Triggers a HuC firmware authentication request to the GuC via intel_guc_send
430 * INTEL_GUC_ACTION_AUTHENTICATE_HUC interface. This function is invoked by
433 * Return: non-zero code on error
435 int intel_guc_auth_huc(struct intel_guc *guc, u32 rsa_offset)
438 INTEL_GUC_ACTION_AUTHENTICATE_HUC,
442 return intel_guc_send(guc, action, ARRAY_SIZE(action));
446 * intel_guc_suspend() - notify GuC entering suspend state
449 int intel_guc_suspend(struct intel_guc *guc)
452 INTEL_GUC_ACTION_ENTER_S_STATE,
453 GUC_POWER_D1, /* any value greater than GUC_POWER_D0 */
454 intel_guc_ggtt_offset(guc, guc->shared_data)
457 return intel_guc_send(guc, data, ARRAY_SIZE(data));
461 * intel_guc_reset_engine() - ask GuC to reset an engine
462 * @guc: intel_guc structure
463 * @engine: engine to be reset
465 int intel_guc_reset_engine(struct intel_guc *guc,
466 struct intel_engine_cs *engine)
470 GEM_BUG_ON(!guc->execbuf_client);
472 data[0] = INTEL_GUC_ACTION_REQUEST_ENGINE_RESET;
473 data[1] = engine->guc_id;
477 data[5] = guc->execbuf_client->stage_id;
478 data[6] = intel_guc_ggtt_offset(guc, guc->shared_data);
480 return intel_guc_send(guc, data, ARRAY_SIZE(data));
484 * intel_guc_resume() - notify GuC resuming from suspend state
487 int intel_guc_resume(struct intel_guc *guc)
490 INTEL_GUC_ACTION_EXIT_S_STATE,
492 intel_guc_ggtt_offset(guc, guc->shared_data)
495 return intel_guc_send(guc, data, ARRAY_SIZE(data));
499 * DOC: GuC Address Space
501 * The layout of GuC address space is shown below:
505 * +==============> +====================+ <== GUC_GGTT_TOP
512 * Address +========> +====================+ <== WOPCM Top
513 * Space ^ | HW contexts RSVD |
515 * | | +==> +--------------------+ <== GuC WOPCM Top
519 * | Bias WOPCM | WOPCM |
523 * +=====+=====+==> +====================+ <== GuC WOPCM Base
526 * +====================+ <== WOPCM Base
528 * The lower part of GuC Address Space [0, ggtt_pin_bias) is mapped to WOPCM
529 * while upper part of GuC Address Space [ggtt_pin_bias, GUC_GGTT_TOP) is mapped
530 * to DRAM. The value of the GuC ggtt_pin_bias is determined by WOPCM size and
531 * actual GuC WOPCM size.
535 * intel_guc_init_ggtt_pin_bias() - Initialize the GuC ggtt_pin_bias value.
536 * @guc: intel_guc structure.
538 * This function will calculate and initialize the ggtt_pin_bias value based on
539 * overall WOPCM size and GuC WOPCM size.
541 void intel_guc_init_ggtt_pin_bias(struct intel_guc *guc)
543 struct drm_i915_private *i915 = guc_to_i915(guc);
545 GEM_BUG_ON(!i915->wopcm.size);
546 GEM_BUG_ON(i915->wopcm.size < i915->wopcm.guc.base);
548 guc->ggtt_pin_bias = i915->wopcm.size - i915->wopcm.guc.base;
552 * intel_guc_allocate_vma() - Allocate a GGTT VMA for GuC usage
554 * @size: size of area to allocate (both virtual space and memory)
556 * This is a wrapper to create an object for use with the GuC. In order to
557 * use it inside the GuC, an object needs to be pinned lifetime, so we allocate
558 * both some backing storage and a range inside the Global GTT. We must pin
559 * it in the GGTT somewhere other than than [0, GUC ggtt_pin_bias) because that
560 * range is reserved inside GuC.
562 * Return: A i915_vma if successful, otherwise an ERR_PTR.
564 struct i915_vma *intel_guc_allocate_vma(struct intel_guc *guc, u32 size)
566 struct drm_i915_private *dev_priv = guc_to_i915(guc);
567 struct drm_i915_gem_object *obj;
568 struct i915_vma *vma;
571 obj = i915_gem_object_create(dev_priv, size);
573 return ERR_CAST(obj);
575 vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
579 ret = i915_vma_pin(vma, 0, PAGE_SIZE,
580 PIN_GLOBAL | PIN_OFFSET_BIAS | guc->ggtt_pin_bias);
589 i915_gem_object_put(obj);