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