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