1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2014-2019 Intel Corporation
4 */
5
6 #include "gem/i915_gem_lmem.h"
7 #include "gt/intel_gt.h"
8 #include "gt/intel_gt_irq.h"
9 #include "gt/intel_gt_pm_irq.h"
10 #include "gt/intel_gt_regs.h"
11 #include "intel_guc.h"
12 #include "intel_guc_ads.h"
13 #include "intel_guc_capture.h"
14 #include "intel_guc_print.h"
15 #include "intel_guc_slpc.h"
16 #include "intel_guc_submission.h"
17 #include "i915_drv.h"
18 #include "i915_irq.h"
19 #include "i915_reg.h"
20
21 /**
22 * DOC: GuC
23 *
24 * The GuC is a microcontroller inside the GT HW, introduced in gen9. The GuC is
25 * designed to offload some of the functionality usually performed by the host
26 * driver; currently the main operations it can take care of are:
27 *
28 * - Authentication of the HuC, which is required to fully enable HuC usage.
29 * - Low latency graphics context scheduling (a.k.a. GuC submission).
30 * - GT Power management.
31 *
32 * The enable_guc module parameter can be used to select which of those
33 * operations to enable within GuC. Note that not all the operations are
34 * supported on all gen9+ platforms.
35 *
36 * Enabling the GuC is not mandatory and therefore the firmware is only loaded
37 * if at least one of the operations is selected. However, not loading the GuC
38 * might result in the loss of some features that do require the GuC (currently
39 * just the HuC, but more are expected to land in the future).
40 */
41
intel_guc_notify(struct intel_guc * guc)42 void intel_guc_notify(struct intel_guc *guc)
43 {
44 struct intel_gt *gt = guc_to_gt(guc);
45
46 /*
47 * On Gen11+, the value written to the register is passes as a payload
48 * to the FW. However, the FW currently treats all values the same way
49 * (H2G interrupt), so we can just write the value that the HW expects
50 * on older gens.
51 */
52 intel_uncore_write(gt->uncore, guc->notify_reg, GUC_SEND_TRIGGER);
53 }
54
guc_send_reg(struct intel_guc * guc,u32 i)55 static inline i915_reg_t guc_send_reg(struct intel_guc *guc, u32 i)
56 {
57 GEM_BUG_ON(!guc->send_regs.base);
58 GEM_BUG_ON(!guc->send_regs.count);
59 GEM_BUG_ON(i >= guc->send_regs.count);
60
61 return _MMIO(guc->send_regs.base + 4 * i);
62 }
63
intel_guc_init_send_regs(struct intel_guc * guc)64 void intel_guc_init_send_regs(struct intel_guc *guc)
65 {
66 struct intel_gt *gt = guc_to_gt(guc);
67 enum forcewake_domains fw_domains = 0;
68 unsigned int i;
69
70 GEM_BUG_ON(!guc->send_regs.base);
71 GEM_BUG_ON(!guc->send_regs.count);
72
73 for (i = 0; i < guc->send_regs.count; i++) {
74 fw_domains |= intel_uncore_forcewake_for_reg(gt->uncore,
75 guc_send_reg(guc, i),
76 FW_REG_READ | FW_REG_WRITE);
77 }
78 guc->send_regs.fw_domains = fw_domains;
79 }
80
gen9_reset_guc_interrupts(struct intel_guc * guc)81 static void gen9_reset_guc_interrupts(struct intel_guc *guc)
82 {
83 struct intel_gt *gt = guc_to_gt(guc);
84
85 assert_rpm_wakelock_held(>->i915->runtime_pm);
86
87 spin_lock_irq(gt->irq_lock);
88 gen6_gt_pm_reset_iir(gt, gt->pm_guc_events);
89 spin_unlock_irq(gt->irq_lock);
90 }
91
gen9_enable_guc_interrupts(struct intel_guc * guc)92 static void gen9_enable_guc_interrupts(struct intel_guc *guc)
93 {
94 struct intel_gt *gt = guc_to_gt(guc);
95
96 assert_rpm_wakelock_held(>->i915->runtime_pm);
97
98 spin_lock_irq(gt->irq_lock);
99 guc_WARN_ON_ONCE(guc, intel_uncore_read(gt->uncore, GEN8_GT_IIR(2)) &
100 gt->pm_guc_events);
101 gen6_gt_pm_enable_irq(gt, gt->pm_guc_events);
102 spin_unlock_irq(gt->irq_lock);
103
104 guc->interrupts.enabled = true;
105 }
106
gen9_disable_guc_interrupts(struct intel_guc * guc)107 static void gen9_disable_guc_interrupts(struct intel_guc *guc)
108 {
109 struct intel_gt *gt = guc_to_gt(guc);
110
111 assert_rpm_wakelock_held(>->i915->runtime_pm);
112 guc->interrupts.enabled = false;
113
114 spin_lock_irq(gt->irq_lock);
115
116 gen6_gt_pm_disable_irq(gt, gt->pm_guc_events);
117
118 spin_unlock_irq(gt->irq_lock);
119 intel_synchronize_irq(gt->i915);
120
121 gen9_reset_guc_interrupts(guc);
122 }
123
__gen11_reset_guc_interrupts(struct intel_gt * gt)124 static bool __gen11_reset_guc_interrupts(struct intel_gt *gt)
125 {
126 u32 irq = gt->type == GT_MEDIA ? MTL_MGUC : GEN11_GUC;
127
128 lockdep_assert_held(gt->irq_lock);
129 return gen11_gt_reset_one_iir(gt, 0, irq);
130 }
131
gen11_reset_guc_interrupts(struct intel_guc * guc)132 static void gen11_reset_guc_interrupts(struct intel_guc *guc)
133 {
134 struct intel_gt *gt = guc_to_gt(guc);
135
136 spin_lock_irq(gt->irq_lock);
137 __gen11_reset_guc_interrupts(gt);
138 spin_unlock_irq(gt->irq_lock);
139 }
140
gen11_enable_guc_interrupts(struct intel_guc * guc)141 static void gen11_enable_guc_interrupts(struct intel_guc *guc)
142 {
143 struct intel_gt *gt = guc_to_gt(guc);
144
145 spin_lock_irq(gt->irq_lock);
146 __gen11_reset_guc_interrupts(gt);
147 spin_unlock_irq(gt->irq_lock);
148
149 guc->interrupts.enabled = true;
150 }
151
gen11_disable_guc_interrupts(struct intel_guc * guc)152 static void gen11_disable_guc_interrupts(struct intel_guc *guc)
153 {
154 struct intel_gt *gt = guc_to_gt(guc);
155
156 guc->interrupts.enabled = false;
157 intel_synchronize_irq(gt->i915);
158
159 gen11_reset_guc_interrupts(guc);
160 }
161
guc_dead_worker_func(struct work_struct * w)162 static void guc_dead_worker_func(struct work_struct *w)
163 {
164 struct intel_guc *guc = container_of(w, struct intel_guc, dead_guc_worker);
165 struct intel_gt *gt = guc_to_gt(guc);
166 unsigned long last = guc->last_dead_guc_jiffies;
167 unsigned long delta = jiffies_to_msecs(jiffies - last);
168
169 if (delta < 500) {
170 intel_gt_set_wedged(gt);
171 } else {
172 intel_gt_handle_error(gt, ALL_ENGINES, I915_ERROR_CAPTURE, "dead GuC");
173 guc->last_dead_guc_jiffies = jiffies;
174 }
175 }
176
intel_guc_init_early(struct intel_guc * guc)177 void intel_guc_init_early(struct intel_guc *guc)
178 {
179 struct intel_gt *gt = guc_to_gt(guc);
180 struct drm_i915_private *i915 = gt->i915;
181
182 intel_uc_fw_init_early(&guc->fw, INTEL_UC_FW_TYPE_GUC, true);
183 intel_guc_ct_init_early(&guc->ct);
184 intel_guc_log_init_early(&guc->log);
185 intel_guc_submission_init_early(guc);
186 intel_guc_slpc_init_early(&guc->slpc);
187 intel_guc_rc_init_early(guc);
188
189 INIT_WORK(&guc->dead_guc_worker, guc_dead_worker_func);
190
191 mutex_init(&guc->send_mutex);
192 spin_lock_init(&guc->irq_lock);
193 if (GRAPHICS_VER(i915) >= 11) {
194 guc->interrupts.reset = gen11_reset_guc_interrupts;
195 guc->interrupts.enable = gen11_enable_guc_interrupts;
196 guc->interrupts.disable = gen11_disable_guc_interrupts;
197 if (gt->type == GT_MEDIA) {
198 guc->notify_reg = MEDIA_GUC_HOST_INTERRUPT;
199 guc->send_regs.base = i915_mmio_reg_offset(MEDIA_SOFT_SCRATCH(0));
200 } else {
201 guc->notify_reg = GEN11_GUC_HOST_INTERRUPT;
202 guc->send_regs.base = i915_mmio_reg_offset(GEN11_SOFT_SCRATCH(0));
203 }
204
205 guc->send_regs.count = GEN11_SOFT_SCRATCH_COUNT;
206
207 } else {
208 guc->notify_reg = GUC_SEND_INTERRUPT;
209 guc->interrupts.reset = gen9_reset_guc_interrupts;
210 guc->interrupts.enable = gen9_enable_guc_interrupts;
211 guc->interrupts.disable = gen9_disable_guc_interrupts;
212 guc->send_regs.base = i915_mmio_reg_offset(SOFT_SCRATCH(0));
213 guc->send_regs.count = GUC_MAX_MMIO_MSG_LEN;
214 BUILD_BUG_ON(GUC_MAX_MMIO_MSG_LEN > SOFT_SCRATCH_COUNT);
215 }
216
217 intel_guc_enable_msg(guc, INTEL_GUC_RECV_MSG_EXCEPTION |
218 INTEL_GUC_RECV_MSG_CRASH_DUMP_POSTED);
219 }
220
intel_guc_init_late(struct intel_guc * guc)221 void intel_guc_init_late(struct intel_guc *guc)
222 {
223 intel_guc_ads_init_late(guc);
224 }
225
guc_ctl_debug_flags(struct intel_guc * guc)226 static u32 guc_ctl_debug_flags(struct intel_guc *guc)
227 {
228 u32 level = intel_guc_log_get_level(&guc->log);
229 u32 flags = 0;
230
231 if (!GUC_LOG_LEVEL_IS_VERBOSE(level))
232 flags |= GUC_LOG_DISABLED;
233 else
234 flags |= GUC_LOG_LEVEL_TO_VERBOSITY(level) <<
235 GUC_LOG_VERBOSITY_SHIFT;
236
237 return flags;
238 }
239
guc_ctl_feature_flags(struct intel_guc * guc)240 static u32 guc_ctl_feature_flags(struct intel_guc *guc)
241 {
242 struct intel_gt *gt = guc_to_gt(guc);
243 u32 flags = 0;
244
245 /*
246 * Enable PXP GuC autoteardown flow.
247 * NB: MTL does things differently.
248 */
249 if (HAS_PXP(gt->i915) && !IS_METEORLAKE(gt->i915))
250 flags |= GUC_CTL_ENABLE_GUC_PXP_CTL;
251
252 if (!intel_guc_submission_is_used(guc))
253 flags |= GUC_CTL_DISABLE_SCHEDULER;
254
255 if (intel_guc_slpc_is_used(guc))
256 flags |= GUC_CTL_ENABLE_SLPC;
257
258 return flags;
259 }
260
guc_ctl_log_params_flags(struct intel_guc * guc)261 static u32 guc_ctl_log_params_flags(struct intel_guc *guc)
262 {
263 struct intel_guc_log *log = &guc->log;
264 u32 offset, flags;
265
266 GEM_BUG_ON(!log->sizes_initialised);
267
268 offset = intel_guc_ggtt_offset(guc, log->vma) >> PAGE_SHIFT;
269
270 flags = GUC_LOG_VALID |
271 GUC_LOG_NOTIFY_ON_HALF_FULL |
272 log->sizes[GUC_LOG_SECTIONS_DEBUG].flag |
273 log->sizes[GUC_LOG_SECTIONS_CAPTURE].flag |
274 (log->sizes[GUC_LOG_SECTIONS_CRASH].count << GUC_LOG_CRASH_SHIFT) |
275 (log->sizes[GUC_LOG_SECTIONS_DEBUG].count << GUC_LOG_DEBUG_SHIFT) |
276 (log->sizes[GUC_LOG_SECTIONS_CAPTURE].count << GUC_LOG_CAPTURE_SHIFT) |
277 (offset << GUC_LOG_BUF_ADDR_SHIFT);
278
279 return flags;
280 }
281
guc_ctl_ads_flags(struct intel_guc * guc)282 static u32 guc_ctl_ads_flags(struct intel_guc *guc)
283 {
284 u32 ads = intel_guc_ggtt_offset(guc, guc->ads_vma) >> PAGE_SHIFT;
285 u32 flags = ads << GUC_ADS_ADDR_SHIFT;
286
287 return flags;
288 }
289
guc_ctl_wa_flags(struct intel_guc * guc)290 static u32 guc_ctl_wa_flags(struct intel_guc *guc)
291 {
292 struct intel_gt *gt = guc_to_gt(guc);
293 u32 flags = 0;
294
295 /* Wa_22012773006:gen11,gen12 < XeHP */
296 if (GRAPHICS_VER(gt->i915) >= 11 &&
297 GRAPHICS_VER_FULL(gt->i915) < IP_VER(12, 55))
298 flags |= GUC_WA_POLLCS;
299
300 /* Wa_14014475959 */
301 if (IS_GFX_GT_IP_STEP(gt, IP_VER(12, 70), STEP_A0, STEP_B0) ||
302 IS_DG2(gt->i915))
303 flags |= GUC_WA_HOLD_CCS_SWITCHOUT;
304
305 /* Wa_16019325821 */
306 /* Wa_14019159160 */
307 if (IS_GFX_GT_IP_RANGE(gt, IP_VER(12, 70), IP_VER(12, 74)))
308 flags |= GUC_WA_RCS_CCS_SWITCHOUT;
309
310 /*
311 * Wa_14012197797
312 * Wa_22011391025
313 *
314 * The same WA bit is used for both and 22011391025 is applicable to
315 * all DG2.
316 *
317 * Platforms post DG2 prevent this issue in hardware by stalling
318 * submissions. With this flag GuC will schedule as to avoid such
319 * stalls.
320 */
321 if (IS_DG2(gt->i915) ||
322 (CCS_MASK(gt) && GRAPHICS_VER_FULL(gt->i915) >= IP_VER(12, 70)))
323 flags |= GUC_WA_DUAL_QUEUE;
324
325 /* Wa_22011802037: graphics version 11/12 */
326 if (intel_engine_reset_needs_wa_22011802037(gt))
327 flags |= GUC_WA_PRE_PARSER;
328
329 /*
330 * Wa_22012727170
331 * Wa_22012727685
332 */
333 if (IS_DG2_G11(gt->i915))
334 flags |= GUC_WA_CONTEXT_ISOLATION;
335
336 /*
337 * Wa_14018913170: Applicable to all platforms supported by i915 so
338 * don't bother testing for all X/Y/Z platforms explicitly.
339 */
340 if (GUC_FIRMWARE_VER(guc) >= MAKE_GUC_VER(70, 7, 0))
341 flags |= GUC_WA_ENABLE_TSC_CHECK_ON_RC6;
342
343 return flags;
344 }
345
guc_ctl_devid(struct intel_guc * guc)346 static u32 guc_ctl_devid(struct intel_guc *guc)
347 {
348 struct drm_i915_private *i915 = guc_to_i915(guc);
349
350 return (INTEL_DEVID(i915) << 16) | INTEL_REVID(i915);
351 }
352
353 /*
354 * Initialise the GuC parameter block before starting the firmware
355 * transfer. These parameters are read by the firmware on startup
356 * and cannot be changed thereafter.
357 */
guc_init_params(struct intel_guc * guc)358 static void guc_init_params(struct intel_guc *guc)
359 {
360 u32 *params = guc->params;
361 int i;
362
363 BUILD_BUG_ON(sizeof(guc->params) != GUC_CTL_MAX_DWORDS * sizeof(u32));
364
365 params[GUC_CTL_LOG_PARAMS] = guc_ctl_log_params_flags(guc);
366 params[GUC_CTL_FEATURE] = guc_ctl_feature_flags(guc);
367 params[GUC_CTL_DEBUG] = guc_ctl_debug_flags(guc);
368 params[GUC_CTL_ADS] = guc_ctl_ads_flags(guc);
369 params[GUC_CTL_WA] = guc_ctl_wa_flags(guc);
370 params[GUC_CTL_DEVID] = guc_ctl_devid(guc);
371
372 for (i = 0; i < GUC_CTL_MAX_DWORDS; i++)
373 guc_dbg(guc, "param[%2d] = %#x\n", i, params[i]);
374 }
375
376 /*
377 * Initialise the GuC parameter block before starting the firmware
378 * transfer. These parameters are read by the firmware on startup
379 * and cannot be changed thereafter.
380 */
intel_guc_write_params(struct intel_guc * guc)381 void intel_guc_write_params(struct intel_guc *guc)
382 {
383 struct intel_uncore *uncore = guc_to_gt(guc)->uncore;
384 int i;
385
386 /*
387 * All SOFT_SCRATCH registers are in FORCEWAKE_GT domain and
388 * they are power context saved so it's ok to release forcewake
389 * when we are done here and take it again at xfer time.
390 */
391 intel_uncore_forcewake_get(uncore, FORCEWAKE_GT);
392
393 intel_uncore_write(uncore, SOFT_SCRATCH(0), 0);
394
395 for (i = 0; i < GUC_CTL_MAX_DWORDS; i++)
396 intel_uncore_write(uncore, SOFT_SCRATCH(1 + i), guc->params[i]);
397
398 intel_uncore_forcewake_put(uncore, FORCEWAKE_GT);
399 }
400
intel_guc_dump_time_info(struct intel_guc * guc,struct drm_printer * p)401 void intel_guc_dump_time_info(struct intel_guc *guc, struct drm_printer *p)
402 {
403 struct intel_gt *gt = guc_to_gt(guc);
404 intel_wakeref_t wakeref;
405 u32 stamp = 0;
406 u64 ktime;
407
408 with_intel_runtime_pm(>->i915->runtime_pm, wakeref)
409 stamp = intel_uncore_read(gt->uncore, GUCPMTIMESTAMP);
410 ktime = ktime_get_boottime_ns();
411
412 drm_printf(p, "Kernel timestamp: 0x%08llX [%llu]\n", ktime, ktime);
413 drm_printf(p, "GuC timestamp: 0x%08X [%u]\n", stamp, stamp);
414 drm_printf(p, "CS timestamp frequency: %u Hz, %u ns\n",
415 gt->clock_frequency, gt->clock_period_ns);
416 }
417
intel_guc_init(struct intel_guc * guc)418 int intel_guc_init(struct intel_guc *guc)
419 {
420 int ret;
421
422 ret = intel_uc_fw_init(&guc->fw);
423 if (ret)
424 goto out;
425
426 ret = intel_guc_log_create(&guc->log);
427 if (ret)
428 goto err_fw;
429
430 ret = intel_guc_capture_init(guc);
431 if (ret)
432 goto err_log;
433
434 ret = intel_guc_ads_create(guc);
435 if (ret)
436 goto err_capture;
437
438 GEM_BUG_ON(!guc->ads_vma);
439
440 ret = intel_guc_ct_init(&guc->ct);
441 if (ret)
442 goto err_ads;
443
444 if (intel_guc_submission_is_used(guc)) {
445 /*
446 * This is stuff we need to have available at fw load time
447 * if we are planning to enable submission later
448 */
449 ret = intel_guc_submission_init(guc);
450 if (ret)
451 goto err_ct;
452 }
453
454 if (intel_guc_slpc_is_used(guc)) {
455 ret = intel_guc_slpc_init(&guc->slpc);
456 if (ret)
457 goto err_submission;
458 }
459
460 /* now that everything is perma-pinned, initialize the parameters */
461 guc_init_params(guc);
462
463 intel_uc_fw_change_status(&guc->fw, INTEL_UC_FIRMWARE_LOADABLE);
464
465 return 0;
466
467 err_submission:
468 intel_guc_submission_fini(guc);
469 err_ct:
470 intel_guc_ct_fini(&guc->ct);
471 err_ads:
472 intel_guc_ads_destroy(guc);
473 err_capture:
474 intel_guc_capture_destroy(guc);
475 err_log:
476 intel_guc_log_destroy(&guc->log);
477 err_fw:
478 intel_uc_fw_fini(&guc->fw);
479 out:
480 intel_uc_fw_change_status(&guc->fw, INTEL_UC_FIRMWARE_INIT_FAIL);
481 guc_probe_error(guc, "failed with %pe\n", ERR_PTR(ret));
482 return ret;
483 }
484
intel_guc_fini(struct intel_guc * guc)485 void intel_guc_fini(struct intel_guc *guc)
486 {
487 if (!intel_uc_fw_is_loadable(&guc->fw))
488 return;
489
490 flush_work(&guc->dead_guc_worker);
491
492 if (intel_guc_slpc_is_used(guc))
493 intel_guc_slpc_fini(&guc->slpc);
494
495 if (intel_guc_submission_is_used(guc))
496 intel_guc_submission_fini(guc);
497
498 intel_guc_ct_fini(&guc->ct);
499
500 intel_guc_ads_destroy(guc);
501 intel_guc_capture_destroy(guc);
502 intel_guc_log_destroy(&guc->log);
503 intel_uc_fw_fini(&guc->fw);
504 }
505
506 /*
507 * This function implements the MMIO based host to GuC interface.
508 */
intel_guc_send_mmio(struct intel_guc * guc,const u32 * request,u32 len,u32 * response_buf,u32 response_buf_size)509 int intel_guc_send_mmio(struct intel_guc *guc, const u32 *request, u32 len,
510 u32 *response_buf, u32 response_buf_size)
511 {
512 struct intel_uncore *uncore = guc_to_gt(guc)->uncore;
513 u32 header;
514 int i;
515 int ret;
516
517 GEM_BUG_ON(!len);
518 GEM_BUG_ON(len > guc->send_regs.count);
519
520 GEM_BUG_ON(FIELD_GET(GUC_HXG_MSG_0_ORIGIN, request[0]) != GUC_HXG_ORIGIN_HOST);
521 GEM_BUG_ON(FIELD_GET(GUC_HXG_MSG_0_TYPE, request[0]) != GUC_HXG_TYPE_REQUEST);
522
523 mutex_lock(&guc->send_mutex);
524 intel_uncore_forcewake_get(uncore, guc->send_regs.fw_domains);
525
526 retry:
527 for (i = 0; i < len; i++)
528 intel_uncore_write(uncore, guc_send_reg(guc, i), request[i]);
529
530 intel_uncore_posting_read(uncore, guc_send_reg(guc, i - 1));
531
532 intel_guc_notify(guc);
533
534 /*
535 * No GuC command should ever take longer than 10ms.
536 * Fast commands should still complete in 10us.
537 */
538 ret = __intel_wait_for_register_fw(uncore,
539 guc_send_reg(guc, 0),
540 GUC_HXG_MSG_0_ORIGIN,
541 FIELD_PREP(GUC_HXG_MSG_0_ORIGIN,
542 GUC_HXG_ORIGIN_GUC),
543 10, 10, &header);
544 if (unlikely(ret)) {
545 timeout:
546 guc_err(guc, "mmio request %#x: no reply %x\n",
547 request[0], header);
548 goto out;
549 }
550
551 if (FIELD_GET(GUC_HXG_MSG_0_TYPE, header) == GUC_HXG_TYPE_NO_RESPONSE_BUSY) {
552 #define done ({ header = intel_uncore_read(uncore, guc_send_reg(guc, 0)); \
553 FIELD_GET(GUC_HXG_MSG_0_ORIGIN, header) != GUC_HXG_ORIGIN_GUC || \
554 FIELD_GET(GUC_HXG_MSG_0_TYPE, header) != GUC_HXG_TYPE_NO_RESPONSE_BUSY; })
555
556 ret = wait_for(done, 1000);
557 if (unlikely(ret))
558 goto timeout;
559 if (unlikely(FIELD_GET(GUC_HXG_MSG_0_ORIGIN, header) !=
560 GUC_HXG_ORIGIN_GUC))
561 goto proto;
562 #undef done
563 }
564
565 if (FIELD_GET(GUC_HXG_MSG_0_TYPE, header) == GUC_HXG_TYPE_NO_RESPONSE_RETRY) {
566 u32 reason = FIELD_GET(GUC_HXG_RETRY_MSG_0_REASON, header);
567
568 guc_dbg(guc, "mmio request %#x: retrying, reason %u\n",
569 request[0], reason);
570 goto retry;
571 }
572
573 if (FIELD_GET(GUC_HXG_MSG_0_TYPE, header) == GUC_HXG_TYPE_RESPONSE_FAILURE) {
574 u32 hint = FIELD_GET(GUC_HXG_FAILURE_MSG_0_HINT, header);
575 u32 error = FIELD_GET(GUC_HXG_FAILURE_MSG_0_ERROR, header);
576
577 guc_err(guc, "mmio request %#x: failure %x/%u\n",
578 request[0], error, hint);
579 ret = -ENXIO;
580 goto out;
581 }
582
583 if (FIELD_GET(GUC_HXG_MSG_0_TYPE, header) != GUC_HXG_TYPE_RESPONSE_SUCCESS) {
584 proto:
585 guc_err(guc, "mmio request %#x: unexpected reply %#x\n",
586 request[0], header);
587 ret = -EPROTO;
588 goto out;
589 }
590
591 if (response_buf) {
592 int count = min(response_buf_size, guc->send_regs.count);
593
594 GEM_BUG_ON(!count);
595
596 response_buf[0] = header;
597
598 for (i = 1; i < count; i++)
599 response_buf[i] = intel_uncore_read(uncore,
600 guc_send_reg(guc, i));
601
602 /* Use number of copied dwords as our return value */
603 ret = count;
604 } else {
605 /* Use data from the GuC response as our return value */
606 ret = FIELD_GET(GUC_HXG_RESPONSE_MSG_0_DATA0, header);
607 }
608
609 out:
610 intel_uncore_forcewake_put(uncore, guc->send_regs.fw_domains);
611 mutex_unlock(&guc->send_mutex);
612
613 return ret;
614 }
615
intel_guc_crash_process_msg(struct intel_guc * guc,u32 action)616 int intel_guc_crash_process_msg(struct intel_guc *guc, u32 action)
617 {
618 if (action == INTEL_GUC_ACTION_NOTIFY_CRASH_DUMP_POSTED)
619 guc_err(guc, "Crash dump notification\n");
620 else if (action == INTEL_GUC_ACTION_NOTIFY_EXCEPTION)
621 guc_err(guc, "Exception notification\n");
622 else
623 guc_err(guc, "Unknown crash notification: 0x%04X\n", action);
624
625 queue_work(system_unbound_wq, &guc->dead_guc_worker);
626
627 return 0;
628 }
629
intel_guc_to_host_process_recv_msg(struct intel_guc * guc,const u32 * payload,u32 len)630 int intel_guc_to_host_process_recv_msg(struct intel_guc *guc,
631 const u32 *payload, u32 len)
632 {
633 u32 msg;
634
635 if (unlikely(!len))
636 return -EPROTO;
637
638 /* Make sure to handle only enabled messages */
639 msg = payload[0] & guc->msg_enabled_mask;
640
641 if (msg & INTEL_GUC_RECV_MSG_CRASH_DUMP_POSTED)
642 guc_err(guc, "Received early crash dump notification!\n");
643 if (msg & INTEL_GUC_RECV_MSG_EXCEPTION)
644 guc_err(guc, "Received early exception notification!\n");
645
646 if (msg & (INTEL_GUC_RECV_MSG_CRASH_DUMP_POSTED | INTEL_GUC_RECV_MSG_EXCEPTION))
647 queue_work(system_unbound_wq, &guc->dead_guc_worker);
648
649 return 0;
650 }
651
652 /**
653 * intel_guc_auth_huc() - Send action to GuC to authenticate HuC ucode
654 * @guc: intel_guc structure
655 * @rsa_offset: rsa offset w.r.t ggtt base of huc vma
656 *
657 * Triggers a HuC firmware authentication request to the GuC via intel_guc_send
658 * INTEL_GUC_ACTION_AUTHENTICATE_HUC interface. This function is invoked by
659 * intel_huc_auth().
660 *
661 * Return: non-zero code on error
662 */
intel_guc_auth_huc(struct intel_guc * guc,u32 rsa_offset)663 int intel_guc_auth_huc(struct intel_guc *guc, u32 rsa_offset)
664 {
665 u32 action[] = {
666 INTEL_GUC_ACTION_AUTHENTICATE_HUC,
667 rsa_offset
668 };
669
670 return intel_guc_send(guc, action, ARRAY_SIZE(action));
671 }
672
673 /**
674 * intel_guc_suspend() - notify GuC entering suspend state
675 * @guc: the guc
676 */
intel_guc_suspend(struct intel_guc * guc)677 int intel_guc_suspend(struct intel_guc *guc)
678 {
679 int ret;
680 u32 action[] = {
681 INTEL_GUC_ACTION_CLIENT_SOFT_RESET,
682 };
683
684 if (!intel_guc_is_ready(guc))
685 return 0;
686
687 if (intel_guc_submission_is_used(guc)) {
688 flush_work(&guc->dead_guc_worker);
689
690 /*
691 * This H2G MMIO command tears down the GuC in two steps. First it will
692 * generate a G2H CTB for every active context indicating a reset. In
693 * practice the i915 shouldn't ever get a G2H as suspend should only be
694 * called when the GPU is idle. Next, it tears down the CTBs and this
695 * H2G MMIO command completes.
696 *
697 * Don't abort on a failure code from the GuC. Keep going and do the
698 * clean up in sanitize() and re-initialisation on resume and hopefully
699 * the error here won't be problematic.
700 */
701 ret = intel_guc_send_mmio(guc, action, ARRAY_SIZE(action), NULL, 0);
702 if (ret)
703 guc_err(guc, "suspend: RESET_CLIENT action failed with %pe\n",
704 ERR_PTR(ret));
705 }
706
707 /* Signal that the GuC isn't running. */
708 intel_guc_sanitize(guc);
709
710 return 0;
711 }
712
713 /**
714 * intel_guc_resume() - notify GuC resuming from suspend state
715 * @guc: the guc
716 */
intel_guc_resume(struct intel_guc * guc)717 int intel_guc_resume(struct intel_guc *guc)
718 {
719 /*
720 * NB: This function can still be called even if GuC submission is
721 * disabled, e.g. if GuC is enabled for HuC authentication only. Thus,
722 * if any code is later added here, it must be support doing nothing
723 * if submission is disabled (as per intel_guc_suspend).
724 */
725 return 0;
726 }
727
728 /**
729 * DOC: GuC Memory Management
730 *
731 * GuC can't allocate any memory for its own usage, so all the allocations must
732 * be handled by the host driver. GuC accesses the memory via the GGTT, with the
733 * exception of the top and bottom parts of the 4GB address space, which are
734 * instead re-mapped by the GuC HW to memory location of the FW itself (WOPCM)
735 * or other parts of the HW. The driver must take care not to place objects that
736 * the GuC is going to access in these reserved ranges. The layout of the GuC
737 * address space is shown below:
738 *
739 * ::
740 *
741 * +===========> +====================+ <== FFFF_FFFF
742 * ^ | Reserved |
743 * | +====================+ <== GUC_GGTT_TOP
744 * | | |
745 * | | DRAM |
746 * GuC | |
747 * Address +===> +====================+ <== GuC ggtt_pin_bias
748 * Space ^ | |
749 * | | | |
750 * | GuC | GuC |
751 * | WOPCM | WOPCM |
752 * | Size | |
753 * | | | |
754 * v v | |
755 * +=======+===> +====================+ <== 0000_0000
756 *
757 * The lower part of GuC Address Space [0, ggtt_pin_bias) is mapped to GuC WOPCM
758 * while upper part of GuC Address Space [ggtt_pin_bias, GUC_GGTT_TOP) is mapped
759 * to DRAM. The value of the GuC ggtt_pin_bias is the GuC WOPCM size.
760 */
761
762 /**
763 * intel_guc_allocate_vma() - Allocate a GGTT VMA for GuC usage
764 * @guc: the guc
765 * @size: size of area to allocate (both virtual space and memory)
766 *
767 * This is a wrapper to create an object for use with the GuC. In order to
768 * use it inside the GuC, an object needs to be pinned lifetime, so we allocate
769 * both some backing storage and a range inside the Global GTT. We must pin
770 * it in the GGTT somewhere other than than [0, GUC ggtt_pin_bias) because that
771 * range is reserved inside GuC.
772 *
773 * Return: A i915_vma if successful, otherwise an ERR_PTR.
774 */
intel_guc_allocate_vma(struct intel_guc * guc,u32 size)775 struct i915_vma *intel_guc_allocate_vma(struct intel_guc *guc, u32 size)
776 {
777 struct intel_gt *gt = guc_to_gt(guc);
778 struct drm_i915_gem_object *obj;
779 struct i915_vma *vma;
780 u64 flags;
781 int ret;
782
783 if (HAS_LMEM(gt->i915))
784 obj = i915_gem_object_create_lmem(gt->i915, size,
785 I915_BO_ALLOC_CPU_CLEAR |
786 I915_BO_ALLOC_CONTIGUOUS |
787 I915_BO_ALLOC_PM_EARLY);
788 else
789 obj = i915_gem_object_create_shmem(gt->i915, size);
790
791 if (IS_ERR(obj))
792 return ERR_CAST(obj);
793
794 /*
795 * Wa_22016122933: For Media version 13.0, all Media GT shared
796 * memory needs to be mapped as WC on CPU side and UC (PAT
797 * index 2) on GPU side.
798 */
799 if (intel_gt_needs_wa_22016122933(gt))
800 i915_gem_object_set_cache_coherency(obj, I915_CACHE_NONE);
801
802 vma = i915_vma_instance(obj, >->ggtt->vm, NULL);
803 if (IS_ERR(vma))
804 goto err;
805
806 flags = PIN_OFFSET_BIAS | i915_ggtt_pin_bias(vma);
807 ret = i915_ggtt_pin(vma, NULL, 0, flags);
808 if (ret) {
809 vma = ERR_PTR(ret);
810 goto err;
811 }
812
813 return i915_vma_make_unshrinkable(vma);
814
815 err:
816 i915_gem_object_put(obj);
817 return vma;
818 }
819
820 /**
821 * intel_guc_allocate_and_map_vma() - Allocate and map VMA for GuC usage
822 * @guc: the guc
823 * @size: size of area to allocate (both virtual space and memory)
824 * @out_vma: return variable for the allocated vma pointer
825 * @out_vaddr: return variable for the obj mapping
826 *
827 * This wrapper calls intel_guc_allocate_vma() and then maps the allocated
828 * object with I915_MAP_WB.
829 *
830 * Return: 0 if successful, a negative errno code otherwise.
831 */
intel_guc_allocate_and_map_vma(struct intel_guc * guc,u32 size,struct i915_vma ** out_vma,void ** out_vaddr)832 int intel_guc_allocate_and_map_vma(struct intel_guc *guc, u32 size,
833 struct i915_vma **out_vma, void **out_vaddr)
834 {
835 struct i915_vma *vma;
836 void *vaddr;
837
838 vma = intel_guc_allocate_vma(guc, size);
839 if (IS_ERR(vma))
840 return PTR_ERR(vma);
841
842 vaddr = i915_gem_object_pin_map_unlocked(vma->obj,
843 intel_gt_coherent_map_type(guc_to_gt(guc),
844 vma->obj, true));
845 if (IS_ERR(vaddr)) {
846 i915_vma_unpin_and_release(&vma, 0);
847 return PTR_ERR(vaddr);
848 }
849
850 *out_vma = vma;
851 *out_vaddr = vaddr;
852
853 return 0;
854 }
855
__guc_action_self_cfg(struct intel_guc * guc,u16 key,u16 len,u64 value)856 static int __guc_action_self_cfg(struct intel_guc *guc, u16 key, u16 len, u64 value)
857 {
858 u32 request[HOST2GUC_SELF_CFG_REQUEST_MSG_LEN] = {
859 FIELD_PREP(GUC_HXG_MSG_0_ORIGIN, GUC_HXG_ORIGIN_HOST) |
860 FIELD_PREP(GUC_HXG_MSG_0_TYPE, GUC_HXG_TYPE_REQUEST) |
861 FIELD_PREP(GUC_HXG_REQUEST_MSG_0_ACTION, GUC_ACTION_HOST2GUC_SELF_CFG),
862 FIELD_PREP(HOST2GUC_SELF_CFG_REQUEST_MSG_1_KLV_KEY, key) |
863 FIELD_PREP(HOST2GUC_SELF_CFG_REQUEST_MSG_1_KLV_LEN, len),
864 FIELD_PREP(HOST2GUC_SELF_CFG_REQUEST_MSG_2_VALUE32, lower_32_bits(value)),
865 FIELD_PREP(HOST2GUC_SELF_CFG_REQUEST_MSG_3_VALUE64, upper_32_bits(value)),
866 };
867 int ret;
868
869 GEM_BUG_ON(len > 2);
870 GEM_BUG_ON(len == 1 && upper_32_bits(value));
871
872 /* Self config must go over MMIO */
873 ret = intel_guc_send_mmio(guc, request, ARRAY_SIZE(request), NULL, 0);
874
875 if (unlikely(ret < 0))
876 return ret;
877 if (unlikely(ret > 1))
878 return -EPROTO;
879 if (unlikely(!ret))
880 return -ENOKEY;
881
882 return 0;
883 }
884
__guc_self_cfg(struct intel_guc * guc,u16 key,u16 len,u64 value)885 static int __guc_self_cfg(struct intel_guc *guc, u16 key, u16 len, u64 value)
886 {
887 int err = __guc_action_self_cfg(guc, key, len, value);
888
889 if (unlikely(err))
890 guc_probe_error(guc, "Unsuccessful self-config (%pe) key %#hx value %#llx\n",
891 ERR_PTR(err), key, value);
892 return err;
893 }
894
intel_guc_self_cfg32(struct intel_guc * guc,u16 key,u32 value)895 int intel_guc_self_cfg32(struct intel_guc *guc, u16 key, u32 value)
896 {
897 return __guc_self_cfg(guc, key, 1, value);
898 }
899
intel_guc_self_cfg64(struct intel_guc * guc,u16 key,u64 value)900 int intel_guc_self_cfg64(struct intel_guc *guc, u16 key, u64 value)
901 {
902 return __guc_self_cfg(guc, key, 2, value);
903 }
904
905 /**
906 * intel_guc_load_status - dump information about GuC load status
907 * @guc: the GuC
908 * @p: the &drm_printer
909 *
910 * Pretty printer for GuC load status.
911 */
intel_guc_load_status(struct intel_guc * guc,struct drm_printer * p)912 void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p)
913 {
914 struct intel_gt *gt = guc_to_gt(guc);
915 struct intel_uncore *uncore = gt->uncore;
916 intel_wakeref_t wakeref;
917
918 if (!intel_guc_is_supported(guc)) {
919 drm_printf(p, "GuC not supported\n");
920 return;
921 }
922
923 if (!intel_guc_is_wanted(guc)) {
924 drm_printf(p, "GuC disabled\n");
925 return;
926 }
927
928 intel_uc_fw_dump(&guc->fw, p);
929
930 with_intel_runtime_pm(uncore->rpm, wakeref) {
931 u32 status = intel_uncore_read(uncore, GUC_STATUS);
932 u32 i;
933
934 drm_printf(p, "GuC status 0x%08x:\n", status);
935 drm_printf(p, "\tBootrom status = 0x%x\n",
936 (status & GS_BOOTROM_MASK) >> GS_BOOTROM_SHIFT);
937 drm_printf(p, "\tuKernel status = 0x%x\n",
938 (status & GS_UKERNEL_MASK) >> GS_UKERNEL_SHIFT);
939 drm_printf(p, "\tMIA Core status = 0x%x\n",
940 (status & GS_MIA_MASK) >> GS_MIA_SHIFT);
941 drm_puts(p, "Scratch registers:\n");
942 for (i = 0; i < 16; i++) {
943 drm_printf(p, "\t%2d: \t0x%x\n",
944 i, intel_uncore_read(uncore, SOFT_SCRATCH(i)));
945 }
946 }
947 }
948
intel_guc_write_barrier(struct intel_guc * guc)949 void intel_guc_write_barrier(struct intel_guc *guc)
950 {
951 struct intel_gt *gt = guc_to_gt(guc);
952
953 if (i915_gem_object_is_lmem(guc->ct.vma->obj)) {
954 /*
955 * Ensure intel_uncore_write_fw can be used rather than
956 * intel_uncore_write.
957 */
958 GEM_BUG_ON(guc->send_regs.fw_domains);
959
960 /*
961 * This register is used by the i915 and GuC for MMIO based
962 * communication. Once we are in this code CTBs are the only
963 * method the i915 uses to communicate with the GuC so it is
964 * safe to write to this register (a value of 0 is NOP for MMIO
965 * communication). If we ever start mixing CTBs and MMIOs a new
966 * register will have to be chosen. This function is also used
967 * to enforce ordering of a work queue item write and an update
968 * to the process descriptor. When a work queue is being used,
969 * CTBs are also the only mechanism of communication.
970 */
971 intel_uncore_write_fw(gt->uncore, GEN11_SOFT_SCRATCH(0), 0);
972 } else {
973 /* wmb() sufficient for a barrier if in smem */
974 wmb();
975 }
976 }
977