xref: /linux/drivers/gpu/drm/i915/display/intel_display_reset.c (revision ca220141fa8ebae09765a242076b2b77338106b0)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2023 Intel Corporation
4  */
5 
6 #include <drm/drm_atomic_helper.h>
7 #include <drm/drm_print.h>
8 
9 #include "intel_clock_gating.h"
10 #include "intel_cx0_phy.h"
11 #include "intel_display_core.h"
12 #include "intel_display_driver.h"
13 #include "intel_display_reset.h"
14 #include "intel_display_types.h"
15 #include "intel_display_utils.h"
16 #include "intel_hotplug.h"
17 #include "intel_pps.h"
18 
19 bool intel_display_reset_test(struct intel_display *display)
20 {
21 	return display->params.force_reset_modeset_test;
22 }
23 
24 /* returns true if intel_display_reset_finish() needs to be called */
25 bool intel_display_reset_prepare(struct intel_display *display,
26 				 modeset_stuck_fn modeset_stuck, void *context)
27 {
28 	struct drm_modeset_acquire_ctx *ctx = &display->restore.reset_ctx;
29 	struct drm_atomic_state *state;
30 	int ret;
31 
32 	if (!HAS_DISPLAY(display))
33 		return false;
34 
35 	if (atomic_read(&display->restore.pending_fb_pin)) {
36 		drm_dbg_kms(display->drm,
37 			    "Modeset potentially stuck, unbreaking through wedging\n");
38 		modeset_stuck(context);
39 	}
40 
41 	/*
42 	 * Need mode_config.mutex so that we don't
43 	 * trample ongoing ->detect() and whatnot.
44 	 */
45 	mutex_lock(&display->drm->mode_config.mutex);
46 	drm_modeset_acquire_init(ctx, 0);
47 	while (1) {
48 		ret = drm_modeset_lock_all_ctx(display->drm, ctx);
49 		if (ret != -EDEADLK)
50 			break;
51 
52 		drm_modeset_backoff(ctx);
53 	}
54 	/*
55 	 * Disabling the crtcs gracefully seems nicer. Also the
56 	 * g33 docs say we should at least disable all the planes.
57 	 */
58 	state = drm_atomic_helper_duplicate_state(display->drm, ctx);
59 	if (IS_ERR(state)) {
60 		ret = PTR_ERR(state);
61 		drm_err(display->drm, "Duplicating state failed with %i\n",
62 			ret);
63 		return true;
64 	}
65 
66 	ret = drm_atomic_helper_disable_all(display->drm, ctx);
67 	if (ret) {
68 		drm_err(display->drm, "Suspending crtc's failed with %i\n",
69 			ret);
70 		drm_atomic_state_put(state);
71 		return true;
72 	}
73 
74 	display->restore.modeset_state = state;
75 	state->acquire_ctx = ctx;
76 
77 	return true;
78 }
79 
80 void intel_display_reset_finish(struct intel_display *display, bool test_only)
81 {
82 	struct drm_modeset_acquire_ctx *ctx = &display->restore.reset_ctx;
83 	struct drm_atomic_state *state;
84 	int ret;
85 
86 	if (!HAS_DISPLAY(display))
87 		return;
88 
89 	state = fetch_and_zero(&display->restore.modeset_state);
90 	if (!state)
91 		goto unlock;
92 
93 	/* reset doesn't touch the display */
94 	if (test_only) {
95 		/* for testing only restore the display */
96 		ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
97 		if (ret) {
98 			drm_WARN_ON(display->drm, ret == -EDEADLK);
99 			drm_err(display->drm,
100 				"Restoring old state failed with %i\n", ret);
101 		}
102 	} else {
103 		/*
104 		 * The display has been reset as well,
105 		 * so need a full re-initialization.
106 		 */
107 		intel_pps_unlock_regs_wa(display);
108 		intel_display_driver_init_hw(display);
109 		intel_clock_gating_init(display->drm);
110 		intel_cx0_pll_power_save_wa(display);
111 		intel_hpd_init(display);
112 
113 		ret = __intel_display_driver_resume(display, state, ctx);
114 		if (ret)
115 			drm_err(display->drm,
116 				"Restoring old state failed with %i\n", ret);
117 
118 		intel_hpd_poll_disable(display);
119 	}
120 
121 	drm_atomic_state_put(state);
122 unlock:
123 	drm_modeset_drop_locks(ctx);
124 	drm_modeset_acquire_fini(ctx);
125 	mutex_unlock(&display->drm->mode_config.mutex);
126 }
127