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