xref: /openbmc/linux/drivers/gpu/drm/i915/gt/uc/intel_uc.c (revision 82df5b73)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2016-2019 Intel Corporation
4  */
5 
6 #include "gt/intel_gt.h"
7 #include "gt/intel_reset.h"
8 #include "intel_guc.h"
9 #include "intel_guc_ads.h"
10 #include "intel_guc_submission.h"
11 #include "intel_uc.h"
12 
13 #include "i915_drv.h"
14 
15 static const struct intel_uc_ops uc_ops_off;
16 static const struct intel_uc_ops uc_ops_on;
17 
18 /* Reset GuC providing us with fresh state for both GuC and HuC.
19  */
20 static int __intel_uc_reset_hw(struct intel_uc *uc)
21 {
22 	struct intel_gt *gt = uc_to_gt(uc);
23 	int ret;
24 	u32 guc_status;
25 
26 	ret = i915_inject_probe_error(gt->i915, -ENXIO);
27 	if (ret)
28 		return ret;
29 
30 	ret = intel_reset_guc(gt);
31 	if (ret) {
32 		DRM_ERROR("Failed to reset GuC, ret = %d\n", ret);
33 		return ret;
34 	}
35 
36 	guc_status = intel_uncore_read(gt->uncore, GUC_STATUS);
37 	WARN(!(guc_status & GS_MIA_IN_RESET),
38 	     "GuC status: 0x%x, MIA core expected to be in reset\n",
39 	     guc_status);
40 
41 	return ret;
42 }
43 
44 static void __confirm_options(struct intel_uc *uc)
45 {
46 	struct drm_i915_private *i915 = uc_to_gt(uc)->i915;
47 
48 	drm_dbg(&i915->drm,
49 		"enable_guc=%d (guc:%s submission:%s huc:%s)\n",
50 		i915_modparams.enable_guc,
51 		yesno(intel_uc_wants_guc(uc)),
52 		yesno(intel_uc_wants_guc_submission(uc)),
53 		yesno(intel_uc_wants_huc(uc)));
54 
55 	if (i915_modparams.enable_guc == -1)
56 		return;
57 
58 	if (i915_modparams.enable_guc == 0) {
59 		GEM_BUG_ON(intel_uc_wants_guc(uc));
60 		GEM_BUG_ON(intel_uc_wants_guc_submission(uc));
61 		GEM_BUG_ON(intel_uc_wants_huc(uc));
62 		return;
63 	}
64 
65 	if (!intel_uc_supports_guc(uc))
66 		drm_info(&i915->drm,
67 			 "Incompatible option enable_guc=%d - %s\n",
68 			 i915_modparams.enable_guc, "GuC is not supported!");
69 
70 	if (i915_modparams.enable_guc & ENABLE_GUC_LOAD_HUC &&
71 	    !intel_uc_supports_huc(uc))
72 		drm_info(&i915->drm,
73 			 "Incompatible option enable_guc=%d - %s\n",
74 			 i915_modparams.enable_guc, "HuC is not supported!");
75 
76 	if (i915_modparams.enable_guc & ENABLE_GUC_SUBMISSION &&
77 	    !intel_uc_supports_guc_submission(uc))
78 		drm_info(&i915->drm,
79 			 "Incompatible option enable_guc=%d - %s\n",
80 			 i915_modparams.enable_guc, "GuC submission is N/A");
81 
82 	if (i915_modparams.enable_guc & ~(ENABLE_GUC_SUBMISSION |
83 					  ENABLE_GUC_LOAD_HUC))
84 		drm_info(&i915->drm,
85 			 "Incompatible option enable_guc=%d - %s\n",
86 			 i915_modparams.enable_guc, "undocumented flag");
87 }
88 
89 void intel_uc_init_early(struct intel_uc *uc)
90 {
91 	intel_guc_init_early(&uc->guc);
92 	intel_huc_init_early(&uc->huc);
93 
94 	__confirm_options(uc);
95 
96 	if (intel_uc_wants_guc(uc))
97 		uc->ops = &uc_ops_on;
98 	else
99 		uc->ops = &uc_ops_off;
100 }
101 
102 void intel_uc_driver_late_release(struct intel_uc *uc)
103 {
104 }
105 
106 /**
107  * intel_uc_init_mmio - setup uC MMIO access
108  * @uc: the intel_uc structure
109  *
110  * Setup minimal state necessary for MMIO accesses later in the
111  * initialization sequence.
112  */
113 void intel_uc_init_mmio(struct intel_uc *uc)
114 {
115 	intel_guc_init_send_regs(&uc->guc);
116 }
117 
118 static void __uc_capture_load_err_log(struct intel_uc *uc)
119 {
120 	struct intel_guc *guc = &uc->guc;
121 
122 	if (guc->log.vma && !uc->load_err_log)
123 		uc->load_err_log = i915_gem_object_get(guc->log.vma->obj);
124 }
125 
126 static void __uc_free_load_err_log(struct intel_uc *uc)
127 {
128 	struct drm_i915_gem_object *log = fetch_and_zero(&uc->load_err_log);
129 
130 	if (log)
131 		i915_gem_object_put(log);
132 }
133 
134 void intel_uc_driver_remove(struct intel_uc *uc)
135 {
136 	intel_uc_fini_hw(uc);
137 	intel_uc_fini(uc);
138 	__uc_free_load_err_log(uc);
139 }
140 
141 static inline bool guc_communication_enabled(struct intel_guc *guc)
142 {
143 	return intel_guc_ct_enabled(&guc->ct);
144 }
145 
146 /*
147  * Events triggered while CT buffers are disabled are logged in the SCRATCH_15
148  * register using the same bits used in the CT message payload. Since our
149  * communication channel with guc is turned off at this point, we can save the
150  * message and handle it after we turn it back on.
151  */
152 static void guc_clear_mmio_msg(struct intel_guc *guc)
153 {
154 	intel_uncore_write(guc_to_gt(guc)->uncore, SOFT_SCRATCH(15), 0);
155 }
156 
157 static void guc_get_mmio_msg(struct intel_guc *guc)
158 {
159 	u32 val;
160 
161 	spin_lock_irq(&guc->irq_lock);
162 
163 	val = intel_uncore_read(guc_to_gt(guc)->uncore, SOFT_SCRATCH(15));
164 	guc->mmio_msg |= val & guc->msg_enabled_mask;
165 
166 	/*
167 	 * clear all events, including the ones we're not currently servicing,
168 	 * to make sure we don't try to process a stale message if we enable
169 	 * handling of more events later.
170 	 */
171 	guc_clear_mmio_msg(guc);
172 
173 	spin_unlock_irq(&guc->irq_lock);
174 }
175 
176 static void guc_handle_mmio_msg(struct intel_guc *guc)
177 {
178 	struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
179 
180 	/* we need communication to be enabled to reply to GuC */
181 	GEM_BUG_ON(!guc_communication_enabled(guc));
182 
183 	if (!guc->mmio_msg)
184 		return;
185 
186 	spin_lock_irq(&i915->irq_lock);
187 	intel_guc_to_host_process_recv_msg(guc, &guc->mmio_msg, 1);
188 	spin_unlock_irq(&i915->irq_lock);
189 
190 	guc->mmio_msg = 0;
191 }
192 
193 static void guc_reset_interrupts(struct intel_guc *guc)
194 {
195 	guc->interrupts.reset(guc);
196 }
197 
198 static void guc_enable_interrupts(struct intel_guc *guc)
199 {
200 	guc->interrupts.enable(guc);
201 }
202 
203 static void guc_disable_interrupts(struct intel_guc *guc)
204 {
205 	guc->interrupts.disable(guc);
206 }
207 
208 static int guc_enable_communication(struct intel_guc *guc)
209 {
210 	struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
211 	int ret;
212 
213 	GEM_BUG_ON(guc_communication_enabled(guc));
214 
215 	ret = i915_inject_probe_error(i915, -ENXIO);
216 	if (ret)
217 		return ret;
218 
219 	ret = intel_guc_ct_enable(&guc->ct);
220 	if (ret)
221 		return ret;
222 
223 	/* check for mmio messages received before/during the CT enable */
224 	guc_get_mmio_msg(guc);
225 	guc_handle_mmio_msg(guc);
226 
227 	guc_enable_interrupts(guc);
228 
229 	/* check for CT messages received before we enabled interrupts */
230 	spin_lock_irq(&i915->irq_lock);
231 	intel_guc_ct_event_handler(&guc->ct);
232 	spin_unlock_irq(&i915->irq_lock);
233 
234 	DRM_INFO("GuC communication enabled\n");
235 
236 	return 0;
237 }
238 
239 static void guc_disable_communication(struct intel_guc *guc)
240 {
241 	/*
242 	 * Events generated during or after CT disable are logged by guc in
243 	 * via mmio. Make sure the register is clear before disabling CT since
244 	 * all events we cared about have already been processed via CT.
245 	 */
246 	guc_clear_mmio_msg(guc);
247 
248 	guc_disable_interrupts(guc);
249 
250 	intel_guc_ct_disable(&guc->ct);
251 
252 	/*
253 	 * Check for messages received during/after the CT disable. We do not
254 	 * expect any messages to have arrived via CT between the interrupt
255 	 * disable and the CT disable because GuC should've been idle until we
256 	 * triggered the CT disable protocol.
257 	 */
258 	guc_get_mmio_msg(guc);
259 
260 	DRM_INFO("GuC communication disabled\n");
261 }
262 
263 static void __uc_fetch_firmwares(struct intel_uc *uc)
264 {
265 	int err;
266 
267 	GEM_BUG_ON(!intel_uc_wants_guc(uc));
268 
269 	err = intel_uc_fw_fetch(&uc->guc.fw);
270 	if (err)
271 		return;
272 
273 	if (intel_uc_wants_huc(uc))
274 		intel_uc_fw_fetch(&uc->huc.fw);
275 }
276 
277 static void __uc_cleanup_firmwares(struct intel_uc *uc)
278 {
279 	intel_uc_fw_cleanup_fetch(&uc->huc.fw);
280 	intel_uc_fw_cleanup_fetch(&uc->guc.fw);
281 }
282 
283 static int __uc_init(struct intel_uc *uc)
284 {
285 	struct intel_guc *guc = &uc->guc;
286 	struct intel_huc *huc = &uc->huc;
287 	int ret;
288 
289 	GEM_BUG_ON(!intel_uc_wants_guc(uc));
290 
291 	if (!intel_uc_uses_guc(uc))
292 		return 0;
293 
294 	if (i915_inject_probe_failure(uc_to_gt(uc)->i915))
295 		return -ENOMEM;
296 
297 	/* XXX: GuC submission is unavailable for now */
298 	GEM_BUG_ON(intel_uc_uses_guc_submission(uc));
299 
300 	ret = intel_guc_init(guc);
301 	if (ret)
302 		return ret;
303 
304 	if (intel_uc_uses_huc(uc)) {
305 		ret = intel_huc_init(huc);
306 		if (ret)
307 			goto out_guc;
308 	}
309 
310 	return 0;
311 
312 out_guc:
313 	intel_guc_fini(guc);
314 	return ret;
315 }
316 
317 static void __uc_fini(struct intel_uc *uc)
318 {
319 	intel_huc_fini(&uc->huc);
320 	intel_guc_fini(&uc->guc);
321 }
322 
323 static int __uc_sanitize(struct intel_uc *uc)
324 {
325 	struct intel_guc *guc = &uc->guc;
326 	struct intel_huc *huc = &uc->huc;
327 
328 	GEM_BUG_ON(!intel_uc_supports_guc(uc));
329 
330 	intel_huc_sanitize(huc);
331 	intel_guc_sanitize(guc);
332 
333 	return __intel_uc_reset_hw(uc);
334 }
335 
336 /* Initialize and verify the uC regs related to uC positioning in WOPCM */
337 static int uc_init_wopcm(struct intel_uc *uc)
338 {
339 	struct intel_gt *gt = uc_to_gt(uc);
340 	struct intel_uncore *uncore = gt->uncore;
341 	u32 base = intel_wopcm_guc_base(&gt->i915->wopcm);
342 	u32 size = intel_wopcm_guc_size(&gt->i915->wopcm);
343 	u32 huc_agent = intel_uc_uses_huc(uc) ? HUC_LOADING_AGENT_GUC : 0;
344 	u32 mask;
345 	int err;
346 
347 	if (unlikely(!base || !size)) {
348 		i915_probe_error(gt->i915, "Unsuccessful WOPCM partitioning\n");
349 		return -E2BIG;
350 	}
351 
352 	GEM_BUG_ON(!intel_uc_supports_guc(uc));
353 	GEM_BUG_ON(!(base & GUC_WOPCM_OFFSET_MASK));
354 	GEM_BUG_ON(base & ~GUC_WOPCM_OFFSET_MASK);
355 	GEM_BUG_ON(!(size & GUC_WOPCM_SIZE_MASK));
356 	GEM_BUG_ON(size & ~GUC_WOPCM_SIZE_MASK);
357 
358 	err = i915_inject_probe_error(gt->i915, -ENXIO);
359 	if (err)
360 		return err;
361 
362 	mask = GUC_WOPCM_SIZE_MASK | GUC_WOPCM_SIZE_LOCKED;
363 	err = intel_uncore_write_and_verify(uncore, GUC_WOPCM_SIZE, size, mask,
364 					    size | GUC_WOPCM_SIZE_LOCKED);
365 	if (err)
366 		goto err_out;
367 
368 	mask = GUC_WOPCM_OFFSET_MASK | GUC_WOPCM_OFFSET_VALID | huc_agent;
369 	err = intel_uncore_write_and_verify(uncore, DMA_GUC_WOPCM_OFFSET,
370 					    base | huc_agent, mask,
371 					    base | huc_agent |
372 					    GUC_WOPCM_OFFSET_VALID);
373 	if (err)
374 		goto err_out;
375 
376 	return 0;
377 
378 err_out:
379 	i915_probe_error(gt->i915, "Failed to init uC WOPCM registers!\n");
380 	i915_probe_error(gt->i915, "%s(%#x)=%#x\n", "DMA_GUC_WOPCM_OFFSET",
381 			 i915_mmio_reg_offset(DMA_GUC_WOPCM_OFFSET),
382 			 intel_uncore_read(uncore, DMA_GUC_WOPCM_OFFSET));
383 	i915_probe_error(gt->i915, "%s(%#x)=%#x\n", "GUC_WOPCM_SIZE",
384 			 i915_mmio_reg_offset(GUC_WOPCM_SIZE),
385 			 intel_uncore_read(uncore, GUC_WOPCM_SIZE));
386 
387 	return err;
388 }
389 
390 static bool uc_is_wopcm_locked(struct intel_uc *uc)
391 {
392 	struct intel_gt *gt = uc_to_gt(uc);
393 	struct intel_uncore *uncore = gt->uncore;
394 
395 	return (intel_uncore_read(uncore, GUC_WOPCM_SIZE) & GUC_WOPCM_SIZE_LOCKED) ||
396 	       (intel_uncore_read(uncore, DMA_GUC_WOPCM_OFFSET) & GUC_WOPCM_OFFSET_VALID);
397 }
398 
399 static int __uc_check_hw(struct intel_uc *uc)
400 {
401 	if (!intel_uc_supports_guc(uc))
402 		return 0;
403 
404 	/*
405 	 * We can silently continue without GuC only if it was never enabled
406 	 * before on this system after reboot, otherwise we risk GPU hangs.
407 	 * To check if GuC was loaded before we look at WOPCM registers.
408 	 */
409 	if (uc_is_wopcm_locked(uc))
410 		return -EIO;
411 
412 	return 0;
413 }
414 
415 static int __uc_init_hw(struct intel_uc *uc)
416 {
417 	struct drm_i915_private *i915 = uc_to_gt(uc)->i915;
418 	struct intel_guc *guc = &uc->guc;
419 	struct intel_huc *huc = &uc->huc;
420 	int ret, attempts;
421 
422 	GEM_BUG_ON(!intel_uc_supports_guc(uc));
423 	GEM_BUG_ON(!intel_uc_wants_guc(uc));
424 
425 	if (!intel_uc_fw_is_loadable(&guc->fw)) {
426 		ret = __uc_check_hw(uc) ||
427 		      intel_uc_fw_is_overridden(&guc->fw) ||
428 		      intel_uc_wants_guc_submission(uc) ?
429 		      intel_uc_fw_status_to_error(guc->fw.status) : 0;
430 		goto err_out;
431 	}
432 
433 	ret = uc_init_wopcm(uc);
434 	if (ret)
435 		goto err_out;
436 
437 	guc_reset_interrupts(guc);
438 
439 	/* WaEnableuKernelHeaderValidFix:skl */
440 	/* WaEnableGuCBootHashCheckNotSet:skl,bxt,kbl */
441 	if (IS_GEN(i915, 9))
442 		attempts = 3;
443 	else
444 		attempts = 1;
445 
446 	while (attempts--) {
447 		/*
448 		 * Always reset the GuC just before (re)loading, so
449 		 * that the state and timing are fairly predictable
450 		 */
451 		ret = __uc_sanitize(uc);
452 		if (ret)
453 			goto err_out;
454 
455 		intel_huc_fw_upload(huc);
456 		intel_guc_ads_reset(guc);
457 		intel_guc_write_params(guc);
458 		ret = intel_guc_fw_upload(guc);
459 		if (ret == 0)
460 			break;
461 
462 		DRM_DEBUG_DRIVER("GuC fw load failed: %d; will reset and "
463 				 "retry %d more time(s)\n", ret, attempts);
464 	}
465 
466 	/* Did we succeded or run out of retries? */
467 	if (ret)
468 		goto err_log_capture;
469 
470 	ret = guc_enable_communication(guc);
471 	if (ret)
472 		goto err_log_capture;
473 
474 	intel_huc_auth(huc);
475 
476 	ret = intel_guc_sample_forcewake(guc);
477 	if (ret)
478 		goto err_communication;
479 
480 	if (intel_uc_uses_guc_submission(uc))
481 		intel_guc_submission_enable(guc);
482 
483 	drm_info(&i915->drm, "%s firmware %s version %u.%u %s:%s\n",
484 		 intel_uc_fw_type_repr(INTEL_UC_FW_TYPE_GUC), guc->fw.path,
485 		 guc->fw.major_ver_found, guc->fw.minor_ver_found,
486 		 "submission",
487 		 enableddisabled(intel_uc_uses_guc_submission(uc)));
488 
489 	if (intel_uc_uses_huc(uc)) {
490 		drm_info(&i915->drm, "%s firmware %s version %u.%u %s:%s\n",
491 			 intel_uc_fw_type_repr(INTEL_UC_FW_TYPE_HUC),
492 			 huc->fw.path,
493 			 huc->fw.major_ver_found, huc->fw.minor_ver_found,
494 			 "authenticated",
495 			 yesno(intel_huc_is_authenticated(huc)));
496 	}
497 
498 	return 0;
499 
500 	/*
501 	 * We've failed to load the firmware :(
502 	 */
503 err_communication:
504 	guc_disable_communication(guc);
505 err_log_capture:
506 	__uc_capture_load_err_log(uc);
507 err_out:
508 	__uc_sanitize(uc);
509 
510 	if (!ret) {
511 		drm_notice(&i915->drm, "GuC is uninitialized\n");
512 		/* We want to run without GuC submission */
513 		return 0;
514 	}
515 
516 	i915_probe_error(i915, "GuC initialization failed %d\n", ret);
517 
518 	/* We want to keep KMS alive */
519 	return -EIO;
520 }
521 
522 static void __uc_fini_hw(struct intel_uc *uc)
523 {
524 	struct intel_guc *guc = &uc->guc;
525 
526 	if (!intel_guc_is_fw_running(guc))
527 		return;
528 
529 	if (intel_uc_uses_guc_submission(uc))
530 		intel_guc_submission_disable(guc);
531 
532 	if (guc_communication_enabled(guc))
533 		guc_disable_communication(guc);
534 
535 	__uc_sanitize(uc);
536 }
537 
538 /**
539  * intel_uc_reset_prepare - Prepare for reset
540  * @uc: the intel_uc structure
541  *
542  * Preparing for full gpu reset.
543  */
544 void intel_uc_reset_prepare(struct intel_uc *uc)
545 {
546 	struct intel_guc *guc = &uc->guc;
547 
548 	if (!intel_guc_is_ready(guc))
549 		return;
550 
551 	guc_disable_communication(guc);
552 	__uc_sanitize(uc);
553 }
554 
555 void intel_uc_runtime_suspend(struct intel_uc *uc)
556 {
557 	struct intel_guc *guc = &uc->guc;
558 	int err;
559 
560 	if (!intel_guc_is_ready(guc))
561 		return;
562 
563 	err = intel_guc_suspend(guc);
564 	if (err)
565 		DRM_DEBUG_DRIVER("Failed to suspend GuC, err=%d", err);
566 
567 	guc_disable_communication(guc);
568 }
569 
570 void intel_uc_suspend(struct intel_uc *uc)
571 {
572 	struct intel_guc *guc = &uc->guc;
573 	intel_wakeref_t wakeref;
574 
575 	if (!intel_guc_is_ready(guc))
576 		return;
577 
578 	with_intel_runtime_pm(uc_to_gt(uc)->uncore->rpm, wakeref)
579 		intel_uc_runtime_suspend(uc);
580 }
581 
582 static int __uc_resume(struct intel_uc *uc, bool enable_communication)
583 {
584 	struct intel_guc *guc = &uc->guc;
585 	int err;
586 
587 	if (!intel_guc_is_fw_running(guc))
588 		return 0;
589 
590 	/* Make sure we enable communication if and only if it's disabled */
591 	GEM_BUG_ON(enable_communication == guc_communication_enabled(guc));
592 
593 	if (enable_communication)
594 		guc_enable_communication(guc);
595 
596 	err = intel_guc_resume(guc);
597 	if (err) {
598 		DRM_DEBUG_DRIVER("Failed to resume GuC, err=%d", err);
599 		return err;
600 	}
601 
602 	return 0;
603 }
604 
605 int intel_uc_resume(struct intel_uc *uc)
606 {
607 	/*
608 	 * When coming out of S3/S4 we sanitize and re-init the HW, so
609 	 * communication is already re-enabled at this point.
610 	 */
611 	return __uc_resume(uc, false);
612 }
613 
614 int intel_uc_runtime_resume(struct intel_uc *uc)
615 {
616 	/*
617 	 * During runtime resume we don't sanitize, so we need to re-init
618 	 * communication as well.
619 	 */
620 	return __uc_resume(uc, true);
621 }
622 
623 static const struct intel_uc_ops uc_ops_off = {
624 	.init_hw = __uc_check_hw,
625 };
626 
627 static const struct intel_uc_ops uc_ops_on = {
628 	.sanitize = __uc_sanitize,
629 
630 	.init_fw = __uc_fetch_firmwares,
631 	.fini_fw = __uc_cleanup_firmwares,
632 
633 	.init = __uc_init,
634 	.fini = __uc_fini,
635 
636 	.init_hw = __uc_init_hw,
637 	.fini_hw = __uc_fini_hw,
638 };
639