1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  *
5  * High level crtc/connector/encoder modeset state verification.
6  */
7 
8 #include <drm/drm_atomic_state_helper.h>
9 
10 #include "i915_drv.h"
11 #include "intel_atomic.h"
12 #include "intel_crtc.h"
13 #include "intel_crtc_state_dump.h"
14 #include "intel_cx0_phy.h"
15 #include "intel_display.h"
16 #include "intel_display_types.h"
17 #include "intel_fdi.h"
18 #include "intel_modeset_verify.h"
19 #include "intel_snps_phy.h"
20 #include "skl_watermark.h"
21 
22 /*
23  * Cross check the actual hw state with our own modeset state tracking (and its
24  * internal consistency).
25  */
intel_connector_verify_state(const struct intel_crtc_state * crtc_state,const struct drm_connector_state * conn_state)26 static void intel_connector_verify_state(const struct intel_crtc_state *crtc_state,
27 					 const struct drm_connector_state *conn_state)
28 {
29 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
30 	struct intel_display *display = to_intel_display(connector);
31 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
32 
33 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s]\n",
34 		    connector->base.base.id, connector->base.name);
35 
36 	if (connector->get_hw_state(connector)) {
37 		struct intel_encoder *encoder = intel_attached_encoder(connector);
38 
39 		INTEL_DISPLAY_STATE_WARN(display, !crtc_state,
40 					 "connector enabled without attached crtc\n");
41 
42 		if (!crtc_state)
43 			return;
44 
45 		INTEL_DISPLAY_STATE_WARN(display, !crtc_state->hw.active,
46 					 "connector is active, but attached crtc isn't\n");
47 
48 		if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
49 			return;
50 
51 		INTEL_DISPLAY_STATE_WARN(display,
52 					 conn_state->best_encoder != &encoder->base,
53 					 "atomic encoder doesn't match attached encoder\n");
54 
55 		INTEL_DISPLAY_STATE_WARN(display, conn_state->crtc != encoder->base.crtc,
56 					 "attached encoder crtc differs from connector crtc\n");
57 	} else {
58 		INTEL_DISPLAY_STATE_WARN(display, crtc_state && crtc_state->hw.active,
59 					 "attached crtc is active, but connector isn't\n");
60 		INTEL_DISPLAY_STATE_WARN(display, !crtc_state && conn_state->best_encoder,
61 					 "best encoder set without crtc!\n");
62 	}
63 }
64 
65 static void
verify_connector_state(struct intel_atomic_state * state,struct intel_crtc * crtc)66 verify_connector_state(struct intel_atomic_state *state,
67 		       struct intel_crtc *crtc)
68 {
69 	struct intel_display *display = to_intel_display(state);
70 	struct drm_connector *connector;
71 	const struct drm_connector_state *new_conn_state;
72 	int i;
73 
74 	for_each_new_connector_in_state(&state->base, connector, new_conn_state, i) {
75 		struct drm_encoder *encoder = connector->encoder;
76 		const struct intel_crtc_state *crtc_state = NULL;
77 
78 		if (new_conn_state->crtc != &crtc->base)
79 			continue;
80 
81 		if (crtc)
82 			crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
83 
84 		intel_connector_verify_state(crtc_state, new_conn_state);
85 
86 		INTEL_DISPLAY_STATE_WARN(display, new_conn_state->best_encoder != encoder,
87 					 "connector's atomic encoder doesn't match legacy encoder\n");
88 	}
89 }
90 
intel_pipe_config_sanity_check(const struct intel_crtc_state * crtc_state)91 static void intel_pipe_config_sanity_check(const struct intel_crtc_state *crtc_state)
92 {
93 	struct intel_display *display = to_intel_display(crtc_state);
94 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
95 
96 	if (crtc_state->has_pch_encoder) {
97 		int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(display, crtc_state),
98 							    &crtc_state->fdi_m_n);
99 		int dotclock = crtc_state->hw.adjusted_mode.crtc_clock;
100 
101 		/*
102 		 * FDI already provided one idea for the dotclock.
103 		 * Yell if the encoder disagrees. Allow for slight
104 		 * rounding differences.
105 		 */
106 		drm_WARN(&i915->drm, abs(fdi_dotclock - dotclock) > 1,
107 			 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
108 			 fdi_dotclock, dotclock);
109 	}
110 }
111 
112 static void
verify_encoder_state(struct intel_atomic_state * state)113 verify_encoder_state(struct intel_atomic_state *state)
114 {
115 	struct intel_display *display = to_intel_display(state);
116 	struct drm_i915_private *i915 = to_i915(state->base.dev);
117 	struct intel_encoder *encoder;
118 	struct drm_connector *connector;
119 	const struct drm_connector_state *old_conn_state, *new_conn_state;
120 	int i;
121 
122 	for_each_intel_encoder(&i915->drm, encoder) {
123 		bool enabled = false, found = false;
124 		enum pipe pipe;
125 
126 		drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s]\n",
127 			    encoder->base.base.id,
128 			    encoder->base.name);
129 
130 		for_each_oldnew_connector_in_state(&state->base, connector, old_conn_state,
131 						   new_conn_state, i) {
132 			if (old_conn_state->best_encoder == &encoder->base)
133 				found = true;
134 
135 			if (new_conn_state->best_encoder != &encoder->base)
136 				continue;
137 
138 			found = true;
139 			enabled = true;
140 
141 			INTEL_DISPLAY_STATE_WARN(display,
142 						 new_conn_state->crtc != encoder->base.crtc,
143 						 "connector's crtc doesn't match encoder crtc\n");
144 		}
145 
146 		if (!found)
147 			continue;
148 
149 		INTEL_DISPLAY_STATE_WARN(display, !!encoder->base.crtc != enabled,
150 					 "encoder's enabled state mismatch (expected %i, found %i)\n",
151 					 !!encoder->base.crtc, enabled);
152 
153 		if (!encoder->base.crtc) {
154 			bool active;
155 
156 			active = encoder->get_hw_state(encoder, &pipe);
157 			INTEL_DISPLAY_STATE_WARN(display, active,
158 						 "encoder detached but still enabled on pipe %c.\n",
159 						 pipe_name(pipe));
160 		}
161 	}
162 }
163 
164 static void
verify_crtc_state(struct intel_atomic_state * state,struct intel_crtc * crtc)165 verify_crtc_state(struct intel_atomic_state *state,
166 		  struct intel_crtc *crtc)
167 {
168 	struct intel_display *display = to_intel_display(state);
169 	struct drm_i915_private *i915 = to_i915(display->drm);
170 	const struct intel_crtc_state *sw_crtc_state =
171 		intel_atomic_get_new_crtc_state(state, crtc);
172 	struct intel_crtc_state *hw_crtc_state;
173 	struct intel_crtc *primary_crtc;
174 	struct intel_encoder *encoder;
175 
176 	hw_crtc_state = intel_crtc_state_alloc(crtc);
177 	if (!hw_crtc_state)
178 		return;
179 
180 	drm_dbg_kms(display->drm, "[CRTC:%d:%s]\n", crtc->base.base.id,
181 		    crtc->base.name);
182 
183 	hw_crtc_state->hw.enable = sw_crtc_state->hw.enable;
184 
185 	intel_crtc_get_pipe_config(hw_crtc_state);
186 
187 	/* we keep both pipes enabled on 830 */
188 	if (IS_I830(i915) && hw_crtc_state->hw.active)
189 		hw_crtc_state->hw.active = sw_crtc_state->hw.active;
190 
191 	INTEL_DISPLAY_STATE_WARN(display,
192 				 sw_crtc_state->hw.active != hw_crtc_state->hw.active,
193 				 "crtc active state doesn't match with hw state (expected %i, found %i)\n",
194 				 sw_crtc_state->hw.active, hw_crtc_state->hw.active);
195 
196 	INTEL_DISPLAY_STATE_WARN(display, crtc->active != sw_crtc_state->hw.active,
197 				 "transitional active state does not match atomic hw state (expected %i, found %i)\n",
198 				 sw_crtc_state->hw.active, crtc->active);
199 
200 	primary_crtc = intel_primary_crtc(sw_crtc_state);
201 
202 	for_each_encoder_on_crtc(display->drm, &primary_crtc->base, encoder) {
203 		enum pipe pipe;
204 		bool active;
205 
206 		active = encoder->get_hw_state(encoder, &pipe);
207 		INTEL_DISPLAY_STATE_WARN(display, active != sw_crtc_state->hw.active,
208 					 "[ENCODER:%i] active %i with crtc active %i\n",
209 					 encoder->base.base.id, active,
210 					 sw_crtc_state->hw.active);
211 
212 		INTEL_DISPLAY_STATE_WARN(display, active && primary_crtc->pipe != pipe,
213 					 "Encoder connected to wrong pipe %c\n",
214 					 pipe_name(pipe));
215 
216 		if (active)
217 			intel_encoder_get_config(encoder, hw_crtc_state);
218 	}
219 
220 	if (!sw_crtc_state->hw.active)
221 		goto destroy_state;
222 
223 	intel_pipe_config_sanity_check(hw_crtc_state);
224 
225 	if (!intel_pipe_config_compare(sw_crtc_state,
226 				       hw_crtc_state, false)) {
227 		INTEL_DISPLAY_STATE_WARN(display, 1, "pipe state doesn't match!\n");
228 		intel_crtc_state_dump(hw_crtc_state, NULL, "hw state");
229 		intel_crtc_state_dump(sw_crtc_state, NULL, "sw state");
230 	}
231 
232 destroy_state:
233 	intel_crtc_destroy_state(&crtc->base, &hw_crtc_state->uapi);
234 }
235 
intel_modeset_verify_crtc(struct intel_atomic_state * state,struct intel_crtc * crtc)236 void intel_modeset_verify_crtc(struct intel_atomic_state *state,
237 			       struct intel_crtc *crtc)
238 {
239 	const struct intel_crtc_state *new_crtc_state =
240 		intel_atomic_get_new_crtc_state(state, crtc);
241 
242 	if (!intel_crtc_needs_modeset(new_crtc_state) &&
243 	    !intel_crtc_needs_fastset(new_crtc_state))
244 		return;
245 
246 	intel_wm_state_verify(state, crtc);
247 	verify_connector_state(state, crtc);
248 	verify_crtc_state(state, crtc);
249 	intel_shared_dpll_state_verify(state, crtc);
250 	intel_mpllb_state_verify(state, crtc);
251 	intel_cx0pll_state_verify(state, crtc);
252 }
253 
intel_modeset_verify_disabled(struct intel_atomic_state * state)254 void intel_modeset_verify_disabled(struct intel_atomic_state *state)
255 {
256 	verify_encoder_state(state);
257 	verify_connector_state(state, NULL);
258 	intel_shared_dpll_verify_disabled(state);
259 }
260