xref: /openbmc/linux/drivers/gpu/drm/i915/display/intel_modeset_setup.c (revision c358f53871605a1a8d7ed6e544a05ea00e9c80cb)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  *
5  * Read out the current hardware modeset state, and sanitize it to the current
6  * state.
7  */
8 
9 #include <drm/drm_atomic_uapi.h>
10 #include <drm/drm_atomic_state_helper.h>
11 
12 #include "i915_drv.h"
13 #include "intel_atomic.h"
14 #include "intel_bw.h"
15 #include "intel_color.h"
16 #include "intel_crtc.h"
17 #include "intel_crtc_state_dump.h"
18 #include "intel_ddi.h"
19 #include "intel_de.h"
20 #include "intel_display.h"
21 #include "intel_display_power.h"
22 #include "intel_display_types.h"
23 #include "intel_modeset_setup.h"
24 #include "intel_pch_display.h"
25 #include "intel_pm.h"
26 #include "skl_watermark.h"
27 
28 static void intel_crtc_disable_noatomic(struct intel_crtc *crtc,
29 					struct drm_modeset_acquire_ctx *ctx)
30 {
31 	struct intel_encoder *encoder;
32 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
33 	struct intel_bw_state *bw_state =
34 		to_intel_bw_state(i915->display.bw.obj.state);
35 	struct intel_cdclk_state *cdclk_state =
36 		to_intel_cdclk_state(i915->display.cdclk.obj.state);
37 	struct intel_dbuf_state *dbuf_state =
38 		to_intel_dbuf_state(i915->display.dbuf.obj.state);
39 	struct intel_crtc_state *crtc_state =
40 		to_intel_crtc_state(crtc->base.state);
41 	struct intel_plane *plane;
42 	struct drm_atomic_state *state;
43 	struct intel_crtc_state *temp_crtc_state;
44 	enum pipe pipe = crtc->pipe;
45 	int ret;
46 
47 	if (!crtc_state->hw.active)
48 		return;
49 
50 	for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) {
51 		const struct intel_plane_state *plane_state =
52 			to_intel_plane_state(plane->base.state);
53 
54 		if (plane_state->uapi.visible)
55 			intel_plane_disable_noatomic(crtc, plane);
56 	}
57 
58 	state = drm_atomic_state_alloc(&i915->drm);
59 	if (!state) {
60 		drm_dbg_kms(&i915->drm,
61 			    "failed to disable [CRTC:%d:%s], out of memory",
62 			    crtc->base.base.id, crtc->base.name);
63 		return;
64 	}
65 
66 	state->acquire_ctx = ctx;
67 
68 	/* Everything's already locked, -EDEADLK can't happen. */
69 	temp_crtc_state = intel_atomic_get_crtc_state(state, crtc);
70 	ret = drm_atomic_add_affected_connectors(state, &crtc->base);
71 
72 	drm_WARN_ON(&i915->drm, IS_ERR(temp_crtc_state) || ret);
73 
74 	i915->display.funcs.display->crtc_disable(to_intel_atomic_state(state), crtc);
75 
76 	drm_atomic_state_put(state);
77 
78 	drm_dbg_kms(&i915->drm,
79 		    "[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
80 		    crtc->base.base.id, crtc->base.name);
81 
82 	crtc->active = false;
83 	crtc->base.enabled = false;
84 
85 	drm_WARN_ON(&i915->drm,
86 		    drm_atomic_set_mode_for_crtc(&crtc_state->uapi, NULL) < 0);
87 	crtc_state->uapi.active = false;
88 	crtc_state->uapi.connector_mask = 0;
89 	crtc_state->uapi.encoder_mask = 0;
90 	intel_crtc_free_hw_state(crtc_state);
91 	memset(&crtc_state->hw, 0, sizeof(crtc_state->hw));
92 
93 	for_each_encoder_on_crtc(&i915->drm, &crtc->base, encoder)
94 		encoder->base.crtc = NULL;
95 
96 	intel_fbc_disable(crtc);
97 	intel_update_watermarks(i915);
98 	intel_disable_shared_dpll(crtc_state);
99 
100 	intel_display_power_put_all_in_set(i915, &crtc->enabled_power_domains);
101 
102 	cdclk_state->min_cdclk[pipe] = 0;
103 	cdclk_state->min_voltage_level[pipe] = 0;
104 	cdclk_state->active_pipes &= ~BIT(pipe);
105 
106 	dbuf_state->active_pipes &= ~BIT(pipe);
107 
108 	bw_state->data_rate[pipe] = 0;
109 	bw_state->num_active_planes[pipe] = 0;
110 }
111 
112 static void intel_modeset_update_connector_atomic_state(struct drm_i915_private *i915)
113 {
114 	struct intel_connector *connector;
115 	struct drm_connector_list_iter conn_iter;
116 
117 	drm_connector_list_iter_begin(&i915->drm, &conn_iter);
118 	for_each_intel_connector_iter(connector, &conn_iter) {
119 		struct drm_connector_state *conn_state = connector->base.state;
120 		struct intel_encoder *encoder =
121 			to_intel_encoder(connector->base.encoder);
122 
123 		if (conn_state->crtc)
124 			drm_connector_put(&connector->base);
125 
126 		if (encoder) {
127 			struct intel_crtc *crtc =
128 				to_intel_crtc(encoder->base.crtc);
129 			const struct intel_crtc_state *crtc_state =
130 				to_intel_crtc_state(crtc->base.state);
131 
132 			conn_state->best_encoder = &encoder->base;
133 			conn_state->crtc = &crtc->base;
134 			conn_state->max_bpc = (crtc_state->pipe_bpp ?: 24) / 3;
135 
136 			drm_connector_get(&connector->base);
137 		} else {
138 			conn_state->best_encoder = NULL;
139 			conn_state->crtc = NULL;
140 		}
141 	}
142 	drm_connector_list_iter_end(&conn_iter);
143 }
144 
145 static void intel_crtc_copy_hw_to_uapi_state(struct intel_crtc_state *crtc_state)
146 {
147 	if (intel_crtc_is_bigjoiner_slave(crtc_state))
148 		return;
149 
150 	crtc_state->uapi.enable = crtc_state->hw.enable;
151 	crtc_state->uapi.active = crtc_state->hw.active;
152 	drm_WARN_ON(crtc_state->uapi.crtc->dev,
153 		    drm_atomic_set_mode_for_crtc(&crtc_state->uapi, &crtc_state->hw.mode) < 0);
154 
155 	crtc_state->uapi.adjusted_mode = crtc_state->hw.adjusted_mode;
156 	crtc_state->uapi.scaling_filter = crtc_state->hw.scaling_filter;
157 
158 	/* assume 1:1 mapping */
159 	drm_property_replace_blob(&crtc_state->hw.degamma_lut,
160 				  crtc_state->pre_csc_lut);
161 	drm_property_replace_blob(&crtc_state->hw.gamma_lut,
162 				  crtc_state->post_csc_lut);
163 
164 	drm_property_replace_blob(&crtc_state->uapi.degamma_lut,
165 				  crtc_state->hw.degamma_lut);
166 	drm_property_replace_blob(&crtc_state->uapi.gamma_lut,
167 				  crtc_state->hw.gamma_lut);
168 	drm_property_replace_blob(&crtc_state->uapi.ctm,
169 				  crtc_state->hw.ctm);
170 }
171 
172 static void
173 intel_sanitize_plane_mapping(struct drm_i915_private *i915)
174 {
175 	struct intel_crtc *crtc;
176 
177 	if (DISPLAY_VER(i915) >= 4)
178 		return;
179 
180 	for_each_intel_crtc(&i915->drm, crtc) {
181 		struct intel_plane *plane =
182 			to_intel_plane(crtc->base.primary);
183 		struct intel_crtc *plane_crtc;
184 		enum pipe pipe;
185 
186 		if (!plane->get_hw_state(plane, &pipe))
187 			continue;
188 
189 		if (pipe == crtc->pipe)
190 			continue;
191 
192 		drm_dbg_kms(&i915->drm,
193 			    "[PLANE:%d:%s] attached to the wrong pipe, disabling plane\n",
194 			    plane->base.base.id, plane->base.name);
195 
196 		plane_crtc = intel_crtc_for_pipe(i915, pipe);
197 		intel_plane_disable_noatomic(plane_crtc, plane);
198 	}
199 }
200 
201 static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
202 {
203 	struct drm_device *dev = crtc->base.dev;
204 	struct intel_encoder *encoder;
205 
206 	for_each_encoder_on_crtc(dev, &crtc->base, encoder)
207 		return true;
208 
209 	return false;
210 }
211 
212 static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
213 {
214 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
215 	struct drm_connector_list_iter conn_iter;
216 	struct intel_connector *connector;
217 	struct intel_connector *found_connector = NULL;
218 
219 	drm_connector_list_iter_begin(&i915->drm, &conn_iter);
220 	for_each_intel_connector_iter(connector, &conn_iter) {
221 		if (&encoder->base == connector->base.encoder) {
222 			found_connector = connector;
223 			break;
224 		}
225 	}
226 	drm_connector_list_iter_end(&conn_iter);
227 
228 	return found_connector;
229 }
230 
231 static void intel_sanitize_fifo_underrun_reporting(const struct intel_crtc_state *crtc_state)
232 {
233 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
234 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
235 
236 	if (!crtc_state->hw.active && !HAS_GMCH(i915))
237 		return;
238 
239 	/*
240 	 * We start out with underrun reporting disabled to avoid races.
241 	 * For correct bookkeeping mark this on active crtcs.
242 	 *
243 	 * Also on gmch platforms we dont have any hardware bits to
244 	 * disable the underrun reporting. Which means we need to start
245 	 * out with underrun reporting disabled also on inactive pipes,
246 	 * since otherwise we'll complain about the garbage we read when
247 	 * e.g. coming up after runtime pm.
248 	 *
249 	 * No protection against concurrent access is required - at
250 	 * worst a fifo underrun happens which also sets this to false.
251 	 */
252 	crtc->cpu_fifo_underrun_disabled = true;
253 
254 	/*
255 	 * We track the PCH trancoder underrun reporting state
256 	 * within the crtc. With crtc for pipe A housing the underrun
257 	 * reporting state for PCH transcoder A, crtc for pipe B housing
258 	 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
259 	 * and marking underrun reporting as disabled for the non-existing
260 	 * PCH transcoders B and C would prevent enabling the south
261 	 * error interrupt (see cpt_can_enable_serr_int()).
262 	 */
263 	if (intel_has_pch_trancoder(i915, crtc->pipe))
264 		crtc->pch_fifo_underrun_disabled = true;
265 }
266 
267 static void intel_sanitize_crtc(struct intel_crtc *crtc,
268 				struct drm_modeset_acquire_ctx *ctx)
269 {
270 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
271 	struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
272 
273 	if (crtc_state->hw.active) {
274 		struct intel_plane *plane;
275 
276 		/* Disable everything but the primary plane */
277 		for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) {
278 			const struct intel_plane_state *plane_state =
279 				to_intel_plane_state(plane->base.state);
280 
281 			if (plane_state->uapi.visible &&
282 			    plane->base.type != DRM_PLANE_TYPE_PRIMARY)
283 				intel_plane_disable_noatomic(crtc, plane);
284 		}
285 
286 		/* Disable any background color/etc. set by the BIOS */
287 		intel_color_commit_noarm(crtc_state);
288 		intel_color_commit_arm(crtc_state);
289 	}
290 
291 	/*
292 	 * Adjust the state of the output pipe according to whether we have
293 	 * active connectors/encoders.
294 	 */
295 	if (crtc_state->hw.active && !intel_crtc_has_encoders(crtc) &&
296 	    !intel_crtc_is_bigjoiner_slave(crtc_state))
297 		intel_crtc_disable_noatomic(crtc, ctx);
298 }
299 
300 static bool has_bogus_dpll_config(const struct intel_crtc_state *crtc_state)
301 {
302 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
303 
304 	/*
305 	 * Some SNB BIOSen (eg. ASUS K53SV) are known to misprogram
306 	 * the hardware when a high res displays plugged in. DPLL P
307 	 * divider is zero, and the pipe timings are bonkers. We'll
308 	 * try to disable everything in that case.
309 	 *
310 	 * FIXME would be nice to be able to sanitize this state
311 	 * without several WARNs, but for now let's take the easy
312 	 * road.
313 	 */
314 	return IS_SANDYBRIDGE(i915) &&
315 		crtc_state->hw.active &&
316 		crtc_state->shared_dpll &&
317 		crtc_state->port_clock == 0;
318 }
319 
320 static void intel_sanitize_encoder(struct intel_encoder *encoder)
321 {
322 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
323 	struct intel_connector *connector;
324 	struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
325 	struct intel_crtc_state *crtc_state = crtc ?
326 		to_intel_crtc_state(crtc->base.state) : NULL;
327 
328 	/*
329 	 * We need to check both for a crtc link (meaning that the encoder is
330 	 * active and trying to read from a pipe) and the pipe itself being
331 	 * active.
332 	 */
333 	bool has_active_crtc = crtc_state &&
334 		crtc_state->hw.active;
335 
336 	if (crtc_state && has_bogus_dpll_config(crtc_state)) {
337 		drm_dbg_kms(&i915->drm,
338 			    "BIOS has misprogrammed the hardware. Disabling pipe %c\n",
339 			    pipe_name(crtc->pipe));
340 		has_active_crtc = false;
341 	}
342 
343 	connector = intel_encoder_find_connector(encoder);
344 	if (connector && !has_active_crtc) {
345 		drm_dbg_kms(&i915->drm,
346 			    "[ENCODER:%d:%s] has active connectors but no active pipe!\n",
347 			    encoder->base.base.id,
348 			    encoder->base.name);
349 
350 		/*
351 		 * Connector is active, but has no active pipe. This is fallout
352 		 * from our resume register restoring. Disable the encoder
353 		 * manually again.
354 		 */
355 		if (crtc_state) {
356 			struct drm_encoder *best_encoder;
357 
358 			drm_dbg_kms(&i915->drm,
359 				    "[ENCODER:%d:%s] manually disabled\n",
360 				    encoder->base.base.id,
361 				    encoder->base.name);
362 
363 			/* avoid oopsing in case the hooks consult best_encoder */
364 			best_encoder = connector->base.state->best_encoder;
365 			connector->base.state->best_encoder = &encoder->base;
366 
367 			/* FIXME NULL atomic state passed! */
368 			if (encoder->disable)
369 				encoder->disable(NULL, encoder, crtc_state,
370 						 connector->base.state);
371 			if (encoder->post_disable)
372 				encoder->post_disable(NULL, encoder, crtc_state,
373 						      connector->base.state);
374 
375 			connector->base.state->best_encoder = best_encoder;
376 		}
377 		encoder->base.crtc = NULL;
378 
379 		/*
380 		 * Inconsistent output/port/pipe state happens presumably due to
381 		 * a bug in one of the get_hw_state functions. Or someplace else
382 		 * in our code, like the register restore mess on resume. Clamp
383 		 * things to off as a safer default.
384 		 */
385 		connector->base.dpms = DRM_MODE_DPMS_OFF;
386 		connector->base.encoder = NULL;
387 	}
388 
389 	/* notify opregion of the sanitized encoder state */
390 	intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
391 
392 	if (HAS_DDI(i915))
393 		intel_ddi_sanitize_encoder_pll_mapping(encoder);
394 }
395 
396 /* FIXME read out full plane state for all planes */
397 static void readout_plane_state(struct drm_i915_private *i915)
398 {
399 	struct intel_plane *plane;
400 	struct intel_crtc *crtc;
401 
402 	for_each_intel_plane(&i915->drm, plane) {
403 		struct intel_plane_state *plane_state =
404 			to_intel_plane_state(plane->base.state);
405 		struct intel_crtc_state *crtc_state;
406 		enum pipe pipe = PIPE_A;
407 		bool visible;
408 
409 		visible = plane->get_hw_state(plane, &pipe);
410 
411 		crtc = intel_crtc_for_pipe(i915, pipe);
412 		crtc_state = to_intel_crtc_state(crtc->base.state);
413 
414 		intel_set_plane_visible(crtc_state, plane_state, visible);
415 
416 		drm_dbg_kms(&i915->drm,
417 			    "[PLANE:%d:%s] hw state readout: %s, pipe %c\n",
418 			    plane->base.base.id, plane->base.name,
419 			    str_enabled_disabled(visible), pipe_name(pipe));
420 	}
421 
422 	for_each_intel_crtc(&i915->drm, crtc) {
423 		struct intel_crtc_state *crtc_state =
424 			to_intel_crtc_state(crtc->base.state);
425 
426 		intel_plane_fixup_bitmasks(crtc_state);
427 	}
428 }
429 
430 static void intel_modeset_readout_hw_state(struct drm_i915_private *i915)
431 {
432 	struct intel_cdclk_state *cdclk_state =
433 		to_intel_cdclk_state(i915->display.cdclk.obj.state);
434 	struct intel_dbuf_state *dbuf_state =
435 		to_intel_dbuf_state(i915->display.dbuf.obj.state);
436 	enum pipe pipe;
437 	struct intel_crtc *crtc;
438 	struct intel_encoder *encoder;
439 	struct intel_connector *connector;
440 	struct drm_connector_list_iter conn_iter;
441 	u8 active_pipes = 0;
442 
443 	for_each_intel_crtc(&i915->drm, crtc) {
444 		struct intel_crtc_state *crtc_state =
445 			to_intel_crtc_state(crtc->base.state);
446 
447 		__drm_atomic_helper_crtc_destroy_state(&crtc_state->uapi);
448 		intel_crtc_free_hw_state(crtc_state);
449 		intel_crtc_state_reset(crtc_state, crtc);
450 
451 		intel_crtc_get_pipe_config(crtc_state);
452 
453 		crtc_state->hw.enable = crtc_state->hw.active;
454 
455 		crtc->base.enabled = crtc_state->hw.enable;
456 		crtc->active = crtc_state->hw.active;
457 
458 		if (crtc_state->hw.active)
459 			active_pipes |= BIT(crtc->pipe);
460 
461 		drm_dbg_kms(&i915->drm,
462 			    "[CRTC:%d:%s] hw state readout: %s\n",
463 			    crtc->base.base.id, crtc->base.name,
464 			    str_enabled_disabled(crtc_state->hw.active));
465 	}
466 
467 	cdclk_state->active_pipes = active_pipes;
468 	dbuf_state->active_pipes = active_pipes;
469 
470 	readout_plane_state(i915);
471 
472 	for_each_intel_encoder(&i915->drm, encoder) {
473 		struct intel_crtc_state *crtc_state = NULL;
474 
475 		pipe = 0;
476 
477 		if (encoder->get_hw_state(encoder, &pipe)) {
478 			crtc = intel_crtc_for_pipe(i915, pipe);
479 			crtc_state = to_intel_crtc_state(crtc->base.state);
480 
481 			encoder->base.crtc = &crtc->base;
482 			intel_encoder_get_config(encoder, crtc_state);
483 
484 			/* read out to slave crtc as well for bigjoiner */
485 			if (crtc_state->bigjoiner_pipes) {
486 				struct intel_crtc *slave_crtc;
487 
488 				/* encoder should read be linked to bigjoiner master */
489 				WARN_ON(intel_crtc_is_bigjoiner_slave(crtc_state));
490 
491 				for_each_intel_crtc_in_pipe_mask(&i915->drm, slave_crtc,
492 								 intel_crtc_bigjoiner_slave_pipes(crtc_state)) {
493 					struct intel_crtc_state *slave_crtc_state;
494 
495 					slave_crtc_state = to_intel_crtc_state(slave_crtc->base.state);
496 					intel_encoder_get_config(encoder, slave_crtc_state);
497 				}
498 			}
499 		} else {
500 			encoder->base.crtc = NULL;
501 		}
502 
503 		if (encoder->sync_state)
504 			encoder->sync_state(encoder, crtc_state);
505 
506 		drm_dbg_kms(&i915->drm,
507 			    "[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
508 			    encoder->base.base.id, encoder->base.name,
509 			    str_enabled_disabled(encoder->base.crtc),
510 			    pipe_name(pipe));
511 	}
512 
513 	intel_dpll_readout_hw_state(i915);
514 
515 	drm_connector_list_iter_begin(&i915->drm, &conn_iter);
516 	for_each_intel_connector_iter(connector, &conn_iter) {
517 		if (connector->get_hw_state(connector)) {
518 			struct intel_crtc_state *crtc_state;
519 			struct intel_crtc *crtc;
520 
521 			connector->base.dpms = DRM_MODE_DPMS_ON;
522 
523 			encoder = intel_attached_encoder(connector);
524 			connector->base.encoder = &encoder->base;
525 
526 			crtc = to_intel_crtc(encoder->base.crtc);
527 			crtc_state = crtc ? to_intel_crtc_state(crtc->base.state) : NULL;
528 
529 			if (crtc_state && crtc_state->hw.active) {
530 				/*
531 				 * This has to be done during hardware readout
532 				 * because anything calling .crtc_disable may
533 				 * rely on the connector_mask being accurate.
534 				 */
535 				crtc_state->uapi.connector_mask |=
536 					drm_connector_mask(&connector->base);
537 				crtc_state->uapi.encoder_mask |=
538 					drm_encoder_mask(&encoder->base);
539 			}
540 		} else {
541 			connector->base.dpms = DRM_MODE_DPMS_OFF;
542 			connector->base.encoder = NULL;
543 		}
544 		drm_dbg_kms(&i915->drm,
545 			    "[CONNECTOR:%d:%s] hw state readout: %s\n",
546 			    connector->base.base.id, connector->base.name,
547 			    str_enabled_disabled(connector->base.encoder));
548 	}
549 	drm_connector_list_iter_end(&conn_iter);
550 
551 	for_each_intel_crtc(&i915->drm, crtc) {
552 		struct intel_bw_state *bw_state =
553 			to_intel_bw_state(i915->display.bw.obj.state);
554 		struct intel_crtc_state *crtc_state =
555 			to_intel_crtc_state(crtc->base.state);
556 		struct intel_plane *plane;
557 		int min_cdclk = 0;
558 
559 		if (crtc_state->hw.active) {
560 			/*
561 			 * The initial mode needs to be set in order to keep
562 			 * the atomic core happy. It wants a valid mode if the
563 			 * crtc's enabled, so we do the above call.
564 			 *
565 			 * But we don't set all the derived state fully, hence
566 			 * set a flag to indicate that a full recalculation is
567 			 * needed on the next commit.
568 			 */
569 			crtc_state->inherited = true;
570 
571 			intel_crtc_update_active_timings(crtc_state);
572 
573 			intel_crtc_copy_hw_to_uapi_state(crtc_state);
574 		}
575 
576 		for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) {
577 			const struct intel_plane_state *plane_state =
578 				to_intel_plane_state(plane->base.state);
579 
580 			/*
581 			 * FIXME don't have the fb yet, so can't
582 			 * use intel_plane_data_rate() :(
583 			 */
584 			if (plane_state->uapi.visible)
585 				crtc_state->data_rate[plane->id] =
586 					4 * crtc_state->pixel_rate;
587 			/*
588 			 * FIXME don't have the fb yet, so can't
589 			 * use plane->min_cdclk() :(
590 			 */
591 			if (plane_state->uapi.visible && plane->min_cdclk) {
592 				if (crtc_state->double_wide || DISPLAY_VER(i915) >= 10)
593 					crtc_state->min_cdclk[plane->id] =
594 						DIV_ROUND_UP(crtc_state->pixel_rate, 2);
595 				else
596 					crtc_state->min_cdclk[plane->id] =
597 						crtc_state->pixel_rate;
598 			}
599 			drm_dbg_kms(&i915->drm,
600 				    "[PLANE:%d:%s] min_cdclk %d kHz\n",
601 				    plane->base.base.id, plane->base.name,
602 				    crtc_state->min_cdclk[plane->id]);
603 		}
604 
605 		if (crtc_state->hw.active) {
606 			min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
607 			if (drm_WARN_ON(&i915->drm, min_cdclk < 0))
608 				min_cdclk = 0;
609 		}
610 
611 		cdclk_state->min_cdclk[crtc->pipe] = min_cdclk;
612 		cdclk_state->min_voltage_level[crtc->pipe] =
613 			crtc_state->min_voltage_level;
614 
615 		intel_bw_crtc_update(bw_state, crtc_state);
616 	}
617 }
618 
619 static void
620 get_encoder_power_domains(struct drm_i915_private *i915)
621 {
622 	struct intel_encoder *encoder;
623 
624 	for_each_intel_encoder(&i915->drm, encoder) {
625 		struct intel_crtc_state *crtc_state;
626 
627 		if (!encoder->get_power_domains)
628 			continue;
629 
630 		/*
631 		 * MST-primary and inactive encoders don't have a crtc state
632 		 * and neither of these require any power domain references.
633 		 */
634 		if (!encoder->base.crtc)
635 			continue;
636 
637 		crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
638 		encoder->get_power_domains(encoder, crtc_state);
639 	}
640 }
641 
642 static void intel_early_display_was(struct drm_i915_private *i915)
643 {
644 	/*
645 	 * Display WA #1185 WaDisableDARBFClkGating:glk,icl,ehl,tgl
646 	 * Also known as Wa_14010480278.
647 	 */
648 	if (IS_DISPLAY_VER(i915, 10, 12))
649 		intel_de_write(i915, GEN9_CLKGATE_DIS_0,
650 			       intel_de_read(i915, GEN9_CLKGATE_DIS_0) | DARBF_GATING_DIS);
651 
652 	if (IS_HASWELL(i915)) {
653 		/*
654 		 * WaRsPkgCStateDisplayPMReq:hsw
655 		 * System hang if this isn't done before disabling all planes!
656 		 */
657 		intel_de_write(i915, CHICKEN_PAR1_1,
658 			       intel_de_read(i915, CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
659 	}
660 
661 	if (IS_KABYLAKE(i915) || IS_COFFEELAKE(i915) || IS_COMETLAKE(i915)) {
662 		/* Display WA #1142:kbl,cfl,cml */
663 		intel_de_rmw(i915, CHICKEN_PAR1_1,
664 			     KBL_ARB_FILL_SPARE_22, KBL_ARB_FILL_SPARE_22);
665 		intel_de_rmw(i915, CHICKEN_MISC_2,
666 			     KBL_ARB_FILL_SPARE_13 | KBL_ARB_FILL_SPARE_14,
667 			     KBL_ARB_FILL_SPARE_14);
668 	}
669 }
670 
671 void intel_modeset_setup_hw_state(struct drm_i915_private *i915,
672 				  struct drm_modeset_acquire_ctx *ctx)
673 {
674 	struct intel_encoder *encoder;
675 	struct intel_crtc *crtc;
676 	intel_wakeref_t wakeref;
677 
678 	wakeref = intel_display_power_get(i915, POWER_DOMAIN_INIT);
679 
680 	intel_early_display_was(i915);
681 	intel_modeset_readout_hw_state(i915);
682 
683 	/* HW state is read out, now we need to sanitize this mess. */
684 	get_encoder_power_domains(i915);
685 
686 	intel_pch_sanitize(i915);
687 
688 	/*
689 	 * intel_sanitize_plane_mapping() may need to do vblank
690 	 * waits, so we need vblank interrupts restored beforehand.
691 	 */
692 	for_each_intel_crtc(&i915->drm, crtc) {
693 		struct intel_crtc_state *crtc_state =
694 			to_intel_crtc_state(crtc->base.state);
695 
696 		intel_sanitize_fifo_underrun_reporting(crtc_state);
697 
698 		drm_crtc_vblank_reset(&crtc->base);
699 
700 		if (crtc_state->hw.active)
701 			intel_crtc_vblank_on(crtc_state);
702 	}
703 
704 	intel_fbc_sanitize(i915);
705 
706 	intel_sanitize_plane_mapping(i915);
707 
708 	for_each_intel_encoder(&i915->drm, encoder)
709 		intel_sanitize_encoder(encoder);
710 
711 	for_each_intel_crtc(&i915->drm, crtc) {
712 		struct intel_crtc_state *crtc_state =
713 			to_intel_crtc_state(crtc->base.state);
714 
715 		intel_sanitize_crtc(crtc, ctx);
716 		intel_crtc_state_dump(crtc_state, NULL, "setup_hw_state");
717 	}
718 
719 	intel_modeset_update_connector_atomic_state(i915);
720 
721 	intel_dpll_sanitize_state(i915);
722 
723 	if (IS_G4X(i915)) {
724 		g4x_wm_get_hw_state(i915);
725 		g4x_wm_sanitize(i915);
726 	} else if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915)) {
727 		vlv_wm_get_hw_state(i915);
728 		vlv_wm_sanitize(i915);
729 	} else if (DISPLAY_VER(i915) >= 9) {
730 		skl_wm_get_hw_state(i915);
731 		skl_wm_sanitize(i915);
732 	} else if (HAS_PCH_SPLIT(i915)) {
733 		ilk_wm_get_hw_state(i915);
734 	}
735 
736 	for_each_intel_crtc(&i915->drm, crtc) {
737 		struct intel_crtc_state *crtc_state =
738 			to_intel_crtc_state(crtc->base.state);
739 		struct intel_power_domain_mask put_domains;
740 
741 		intel_modeset_get_crtc_power_domains(crtc_state, &put_domains);
742 		if (drm_WARN_ON(&i915->drm, !bitmap_empty(put_domains.bits, POWER_DOMAIN_NUM)))
743 			intel_modeset_put_crtc_power_domains(crtc, &put_domains);
744 	}
745 
746 	intel_display_power_put(i915, POWER_DOMAIN_INIT, wakeref);
747 
748 	intel_power_domains_sanitize_state(i915);
749 }
750