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