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