xref: /linux/drivers/gpu/drm/i915/display/intel_modeset_setup.c (revision 0ce92d548b44649a8de706f9bb9e74a4ed2f18a7)
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_state_helper.h>
10 #include <drm/drm_atomic_uapi.h>
11 #include <drm/drm_print.h>
12 #include <drm/drm_vblank.h>
13 
14 #include "i915_reg.h"
15 #include "i9xx_wm.h"
16 #include "intel_atomic.h"
17 #include "intel_bw.h"
18 #include "intel_cmtg.h"
19 #include "intel_color.h"
20 #include "intel_crtc.h"
21 #include "intel_crtc_state_dump.h"
22 #include "intel_ddi.h"
23 #include "intel_de.h"
24 #include "intel_display.h"
25 #include "intel_display_power.h"
26 #include "intel_display_types.h"
27 #include "intel_dmc.h"
28 #include "intel_fifo_underrun.h"
29 #include "intel_modeset_setup.h"
30 #include "intel_pch_display.h"
31 #include "intel_pmdemand.h"
32 #include "intel_tc.h"
33 #include "intel_vblank.h"
34 #include "intel_vga.h"
35 #include "intel_wm.h"
36 #include "skl_watermark.h"
37 
38 static void intel_crtc_disable_noatomic_begin(struct intel_crtc *crtc,
39 					      struct drm_modeset_acquire_ctx *ctx)
40 {
41 	struct intel_display *display = to_intel_display(crtc);
42 	struct intel_crtc_state *crtc_state =
43 		to_intel_crtc_state(crtc->base.state);
44 	struct intel_plane *plane;
45 	struct drm_atomic_state *state;
46 	struct intel_crtc *temp_crtc;
47 	enum pipe pipe = crtc->pipe;
48 
49 	if (!crtc_state->hw.active)
50 		return;
51 
52 	for_each_intel_plane_on_crtc(display->drm, crtc, plane) {
53 		const struct intel_plane_state *plane_state =
54 			to_intel_plane_state(plane->base.state);
55 
56 		if (plane_state->uapi.visible)
57 			intel_plane_disable_noatomic(crtc, plane);
58 	}
59 
60 	state = drm_atomic_state_alloc(display->drm);
61 	if (!state) {
62 		drm_dbg_kms(display->drm,
63 			    "failed to disable [CRTC:%d:%s], out of memory",
64 			    crtc->base.base.id, crtc->base.name);
65 		return;
66 	}
67 
68 	state->acquire_ctx = ctx;
69 	to_intel_atomic_state(state)->internal = true;
70 
71 	/* Everything's already locked, -EDEADLK can't happen. */
72 	for_each_intel_crtc_in_pipe_mask(display->drm, temp_crtc,
73 					 BIT(pipe) |
74 					 intel_crtc_joiner_secondary_pipes(crtc_state)) {
75 		struct intel_crtc_state *temp_crtc_state =
76 			intel_atomic_get_crtc_state(state, temp_crtc);
77 		int ret;
78 
79 		ret = drm_atomic_add_affected_connectors(state, &temp_crtc->base);
80 
81 		drm_WARN_ON(display->drm, IS_ERR(temp_crtc_state) || ret);
82 	}
83 
84 	display->funcs.display->crtc_disable(to_intel_atomic_state(state), crtc);
85 
86 	drm_atomic_state_put(state);
87 
88 	drm_dbg_kms(display->drm,
89 		    "[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
90 		    crtc->base.base.id, crtc->base.name);
91 
92 	crtc->active = false;
93 	crtc->base.enabled = false;
94 
95 	if (crtc_state->shared_dpll)
96 		intel_unreference_shared_dpll_crtc(crtc,
97 						   crtc_state->shared_dpll,
98 						   &crtc_state->shared_dpll->state);
99 }
100 
101 static void set_encoder_for_connector(struct intel_connector *connector,
102 				      struct intel_encoder *encoder)
103 {
104 	struct drm_connector_state *conn_state = connector->base.state;
105 
106 	if (conn_state->crtc)
107 		drm_connector_put(&connector->base);
108 
109 	if (encoder) {
110 		conn_state->best_encoder = &encoder->base;
111 		conn_state->crtc = encoder->base.crtc;
112 		drm_connector_get(&connector->base);
113 	} else {
114 		conn_state->best_encoder = NULL;
115 		conn_state->crtc = NULL;
116 	}
117 }
118 
119 static void reset_encoder_connector_state(struct intel_encoder *encoder)
120 {
121 	struct intel_display *display = to_intel_display(encoder);
122 	struct intel_pmdemand_state *pmdemand_state =
123 		to_intel_pmdemand_state(display->pmdemand.obj.state);
124 	struct intel_connector *connector;
125 	struct drm_connector_list_iter conn_iter;
126 
127 	drm_connector_list_iter_begin(display->drm, &conn_iter);
128 	for_each_intel_connector_iter(connector, &conn_iter) {
129 		if (connector->base.encoder != &encoder->base)
130 			continue;
131 
132 		/* Clear the corresponding bit in pmdemand active phys mask */
133 		intel_pmdemand_update_phys_mask(display, encoder,
134 						pmdemand_state, false);
135 
136 		set_encoder_for_connector(connector, NULL);
137 
138 		connector->base.dpms = DRM_MODE_DPMS_OFF;
139 		connector->base.encoder = NULL;
140 	}
141 	drm_connector_list_iter_end(&conn_iter);
142 }
143 
144 static void reset_crtc_encoder_state(struct intel_crtc *crtc)
145 {
146 	struct intel_display *display = to_intel_display(crtc);
147 	struct intel_encoder *encoder;
148 
149 	for_each_encoder_on_crtc(display->drm, &crtc->base, encoder) {
150 		reset_encoder_connector_state(encoder);
151 		encoder->base.crtc = NULL;
152 	}
153 }
154 
155 static void intel_crtc_disable_noatomic_complete(struct intel_crtc *crtc)
156 {
157 	struct intel_display *display = to_intel_display(crtc);
158 	struct intel_pmdemand_state *pmdemand_state =
159 		to_intel_pmdemand_state(display->pmdemand.obj.state);
160 	struct intel_crtc_state *crtc_state =
161 		to_intel_crtc_state(crtc->base.state);
162 	enum pipe pipe = crtc->pipe;
163 
164 	__drm_atomic_helper_crtc_destroy_state(&crtc_state->uapi);
165 	intel_crtc_free_hw_state(crtc_state);
166 	intel_crtc_state_reset(crtc_state, crtc);
167 
168 	reset_crtc_encoder_state(crtc);
169 
170 	intel_fbc_disable(crtc);
171 	intel_update_watermarks(display);
172 
173 	intel_display_power_put_all_in_set(display, &crtc->enabled_power_domains);
174 
175 	intel_cdclk_crtc_disable_noatomic(crtc);
176 	skl_wm_crtc_disable_noatomic(crtc);
177 	intel_bw_crtc_disable_noatomic(crtc);
178 
179 	intel_pmdemand_update_port_clock(display, pmdemand_state, pipe, 0);
180 }
181 
182 /*
183  * Return all the pipes using a transcoder in @transcoder_mask.
184  * For joiner configs return only the joiner primary.
185  */
186 static u8 get_transcoder_pipes(struct intel_display *display,
187 			       u8 transcoder_mask)
188 {
189 	struct intel_crtc *temp_crtc;
190 	u8 pipes = 0;
191 
192 	for_each_intel_crtc(display->drm, temp_crtc) {
193 		struct intel_crtc_state *temp_crtc_state =
194 			to_intel_crtc_state(temp_crtc->base.state);
195 
196 		if (temp_crtc_state->cpu_transcoder == INVALID_TRANSCODER)
197 			continue;
198 
199 		if (intel_crtc_is_joiner_secondary(temp_crtc_state))
200 			continue;
201 
202 		if (transcoder_mask & BIT(temp_crtc_state->cpu_transcoder))
203 			pipes |= BIT(temp_crtc->pipe);
204 	}
205 
206 	return pipes;
207 }
208 
209 /*
210  * Return the port sync master and slave pipes linked to @crtc.
211  * For joiner configs return only the joiner primary pipes.
212  */
213 static void get_portsync_pipes(struct intel_crtc *crtc,
214 			       u8 *master_pipe_mask, u8 *slave_pipes_mask)
215 {
216 	struct intel_display *display = to_intel_display(crtc);
217 	struct intel_crtc_state *crtc_state =
218 		to_intel_crtc_state(crtc->base.state);
219 	struct intel_crtc *master_crtc;
220 	struct intel_crtc_state *master_crtc_state;
221 	enum transcoder master_transcoder;
222 
223 	if (!is_trans_port_sync_mode(crtc_state)) {
224 		*master_pipe_mask = BIT(crtc->pipe);
225 		*slave_pipes_mask = 0;
226 
227 		return;
228 	}
229 
230 	if (is_trans_port_sync_master(crtc_state))
231 		master_transcoder = crtc_state->cpu_transcoder;
232 	else
233 		master_transcoder = crtc_state->master_transcoder;
234 
235 	*master_pipe_mask = get_transcoder_pipes(display, BIT(master_transcoder));
236 	drm_WARN_ON(display->drm, !is_power_of_2(*master_pipe_mask));
237 
238 	master_crtc = intel_crtc_for_pipe(display, ffs(*master_pipe_mask) - 1);
239 	master_crtc_state = to_intel_crtc_state(master_crtc->base.state);
240 	*slave_pipes_mask = get_transcoder_pipes(display, master_crtc_state->sync_mode_slaves_mask);
241 }
242 
243 static u8 get_joiner_secondary_pipes(struct intel_display *display, u8 primary_pipes_mask)
244 {
245 	struct intel_crtc *primary_crtc;
246 	u8 pipes = 0;
247 
248 	for_each_intel_crtc_in_pipe_mask(display->drm, primary_crtc, primary_pipes_mask) {
249 		struct intel_crtc_state *primary_crtc_state =
250 			to_intel_crtc_state(primary_crtc->base.state);
251 
252 		pipes |= intel_crtc_joiner_secondary_pipes(primary_crtc_state);
253 	}
254 
255 	return pipes;
256 }
257 
258 static void intel_crtc_disable_noatomic(struct intel_crtc *crtc,
259 					struct drm_modeset_acquire_ctx *ctx)
260 {
261 	struct intel_display *display = to_intel_display(crtc);
262 	struct intel_crtc *temp_crtc;
263 	u8 portsync_master_mask;
264 	u8 portsync_slaves_mask;
265 	u8 joiner_secondaries_mask;
266 
267 	/* TODO: Add support for MST */
268 	get_portsync_pipes(crtc, &portsync_master_mask, &portsync_slaves_mask);
269 	joiner_secondaries_mask = get_joiner_secondary_pipes(display,
270 							     portsync_master_mask |
271 							     portsync_slaves_mask);
272 
273 	drm_WARN_ON(display->drm,
274 		    portsync_master_mask & portsync_slaves_mask ||
275 		    portsync_master_mask & joiner_secondaries_mask ||
276 		    portsync_slaves_mask & joiner_secondaries_mask);
277 
278 	for_each_intel_crtc_in_pipe_mask(display->drm, temp_crtc, joiner_secondaries_mask)
279 		intel_crtc_disable_noatomic_begin(temp_crtc, ctx);
280 
281 	for_each_intel_crtc_in_pipe_mask(display->drm, temp_crtc, portsync_slaves_mask)
282 		intel_crtc_disable_noatomic_begin(temp_crtc, ctx);
283 
284 	for_each_intel_crtc_in_pipe_mask(display->drm, temp_crtc, portsync_master_mask)
285 		intel_crtc_disable_noatomic_begin(temp_crtc, ctx);
286 
287 	for_each_intel_crtc_in_pipe_mask(display->drm, temp_crtc,
288 					 joiner_secondaries_mask |
289 					 portsync_slaves_mask |
290 					 portsync_master_mask)
291 		intel_crtc_disable_noatomic_complete(temp_crtc);
292 }
293 
294 static void intel_modeset_update_connector_atomic_state(struct intel_display *display)
295 {
296 	struct intel_connector *connector;
297 	struct drm_connector_list_iter conn_iter;
298 
299 	drm_connector_list_iter_begin(display->drm, &conn_iter);
300 	for_each_intel_connector_iter(connector, &conn_iter) {
301 		struct drm_connector_state *conn_state = connector->base.state;
302 		struct intel_encoder *encoder =
303 			to_intel_encoder(connector->base.encoder);
304 
305 		set_encoder_for_connector(connector, encoder);
306 
307 		if (encoder) {
308 			struct intel_crtc *crtc =
309 				to_intel_crtc(encoder->base.crtc);
310 			const struct intel_crtc_state *crtc_state =
311 				to_intel_crtc_state(crtc->base.state);
312 
313 			conn_state->max_bpc = (crtc_state->pipe_bpp ?: 24) / 3;
314 		}
315 	}
316 	drm_connector_list_iter_end(&conn_iter);
317 }
318 
319 static void intel_crtc_copy_hw_to_uapi_state(struct intel_crtc_state *crtc_state)
320 {
321 	struct intel_display *display = to_intel_display(crtc_state);
322 
323 	if (intel_crtc_is_joiner_secondary(crtc_state))
324 		return;
325 
326 	crtc_state->uapi.enable = crtc_state->hw.enable;
327 	crtc_state->uapi.active = crtc_state->hw.active;
328 	drm_WARN_ON(crtc_state->uapi.crtc->dev,
329 		    drm_atomic_set_mode_for_crtc(&crtc_state->uapi, &crtc_state->hw.mode) < 0);
330 
331 	crtc_state->uapi.adjusted_mode = crtc_state->hw.adjusted_mode;
332 	crtc_state->uapi.scaling_filter = crtc_state->hw.scaling_filter;
333 
334 	if (DISPLAY_INFO(display)->color.degamma_lut_size) {
335 		/* assume 1:1 mapping */
336 		drm_property_replace_blob(&crtc_state->hw.degamma_lut,
337 					  crtc_state->pre_csc_lut);
338 		drm_property_replace_blob(&crtc_state->hw.gamma_lut,
339 					  crtc_state->post_csc_lut);
340 	} else {
341 		/*
342 		 * ilk/snb hw may be configured for either pre_csc_lut
343 		 * or post_csc_lut, but we don't advertise degamma_lut as
344 		 * being available in the uapi since there is only one
345 		 * hardware LUT. Always assign the result of the readout
346 		 * to gamma_lut as that is the only valid source of LUTs
347 		 * in the uapi.
348 		 */
349 		drm_WARN_ON(display->drm, crtc_state->post_csc_lut &&
350 			    crtc_state->pre_csc_lut);
351 
352 		drm_property_replace_blob(&crtc_state->hw.degamma_lut,
353 					  NULL);
354 		drm_property_replace_blob(&crtc_state->hw.gamma_lut,
355 					  crtc_state->post_csc_lut ?:
356 					  crtc_state->pre_csc_lut);
357 	}
358 
359 	drm_property_replace_blob(&crtc_state->uapi.degamma_lut,
360 				  crtc_state->hw.degamma_lut);
361 	drm_property_replace_blob(&crtc_state->uapi.gamma_lut,
362 				  crtc_state->hw.gamma_lut);
363 	drm_property_replace_blob(&crtc_state->uapi.ctm,
364 				  crtc_state->hw.ctm);
365 }
366 
367 static void
368 intel_sanitize_plane_mapping(struct intel_display *display)
369 {
370 	struct intel_crtc *crtc;
371 
372 	if (DISPLAY_VER(display) >= 4)
373 		return;
374 
375 	for_each_intel_crtc(display->drm, crtc) {
376 		struct intel_plane *plane =
377 			to_intel_plane(crtc->base.primary);
378 		struct intel_crtc *plane_crtc;
379 		enum pipe pipe;
380 
381 		if (!plane->get_hw_state(plane, &pipe))
382 			continue;
383 
384 		if (pipe == crtc->pipe)
385 			continue;
386 
387 		drm_dbg_kms(display->drm,
388 			    "[PLANE:%d:%s] attached to the wrong pipe, disabling plane\n",
389 			    plane->base.base.id, plane->base.name);
390 
391 		plane_crtc = intel_crtc_for_pipe(display, pipe);
392 		intel_plane_disable_noatomic(plane_crtc, plane);
393 	}
394 }
395 
396 static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
397 {
398 	struct drm_device *dev = crtc->base.dev;
399 	struct intel_encoder *encoder;
400 
401 	for_each_encoder_on_crtc(dev, &crtc->base, encoder)
402 		return true;
403 
404 	return false;
405 }
406 
407 static bool intel_crtc_needs_link_reset(struct intel_crtc *crtc)
408 {
409 	struct drm_device *dev = crtc->base.dev;
410 	struct intel_encoder *encoder;
411 
412 	for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
413 		struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
414 
415 		if (dig_port && intel_tc_port_link_needs_reset(dig_port))
416 			return true;
417 	}
418 
419 	return false;
420 }
421 
422 static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
423 {
424 	struct intel_display *display = to_intel_display(encoder);
425 	struct drm_connector_list_iter conn_iter;
426 	struct intel_connector *connector;
427 	struct intel_connector *found_connector = NULL;
428 
429 	drm_connector_list_iter_begin(display->drm, &conn_iter);
430 	for_each_intel_connector_iter(connector, &conn_iter) {
431 		if (&encoder->base == connector->base.encoder) {
432 			found_connector = connector;
433 			break;
434 		}
435 	}
436 	drm_connector_list_iter_end(&conn_iter);
437 
438 	return found_connector;
439 }
440 
441 static void intel_sanitize_fifo_underrun_reporting(const struct intel_crtc_state *crtc_state)
442 {
443 	struct intel_display *display = to_intel_display(crtc_state);
444 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
445 
446 	/*
447 	 * We start out with underrun reporting disabled on active
448 	 * pipes to avoid races.
449 	 *
450 	 * Also on gmch platforms we dont have any hardware bits to
451 	 * disable the underrun reporting. Which means we need to start
452 	 * out with underrun reporting disabled also on inactive pipes,
453 	 * since otherwise we'll complain about the garbage we read when
454 	 * e.g. coming up after runtime pm.
455 	 *
456 	 * No protection against concurrent access is required - at
457 	 * worst a fifo underrun happens which also sets this to false.
458 	 */
459 	intel_init_fifo_underrun_reporting(display, crtc,
460 					   !crtc_state->hw.active &&
461 					   !HAS_GMCH(display));
462 }
463 
464 static bool intel_sanitize_crtc(struct intel_crtc *crtc,
465 				struct drm_modeset_acquire_ctx *ctx)
466 {
467 	struct intel_display *display = to_intel_display(crtc);
468 	struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
469 	bool needs_link_reset;
470 
471 	if (crtc_state->hw.active) {
472 		struct intel_plane *plane;
473 
474 		/* Disable everything but the primary plane */
475 		for_each_intel_plane_on_crtc(display->drm, crtc, plane) {
476 			const struct intel_plane_state *plane_state =
477 				to_intel_plane_state(plane->base.state);
478 
479 			if (plane_state->uapi.visible &&
480 			    plane->base.type != DRM_PLANE_TYPE_PRIMARY)
481 				intel_plane_disable_noatomic(crtc, plane);
482 		}
483 
484 		/* Disable any background color/etc. set by the BIOS */
485 		intel_color_commit_noarm(NULL, crtc_state);
486 		intel_color_commit_arm(NULL, crtc_state);
487 	}
488 
489 	if (!crtc_state->hw.active ||
490 	    intel_crtc_is_joiner_secondary(crtc_state))
491 		return false;
492 
493 	needs_link_reset = intel_crtc_needs_link_reset(crtc);
494 
495 	/*
496 	 * Adjust the state of the output pipe according to whether we have
497 	 * active connectors/encoders.
498 	 */
499 	if (!needs_link_reset && intel_crtc_has_encoders(crtc))
500 		return false;
501 
502 	intel_crtc_disable_noatomic(crtc, ctx);
503 
504 	/*
505 	 * The HPD state on other active/disconnected TC ports may be stuck in
506 	 * the connected state until this port is disabled and a ~10ms delay has
507 	 * passed, wait here for that so that sanitizing other CRTCs will see the
508 	 * up-to-date HPD state.
509 	 */
510 	if (needs_link_reset)
511 		msleep(20);
512 
513 	return true;
514 }
515 
516 static void intel_sanitize_all_crtcs(struct intel_display *display,
517 				     struct drm_modeset_acquire_ctx *ctx)
518 {
519 	struct intel_crtc *crtc;
520 	u32 crtcs_forced_off = 0;
521 
522 	/*
523 	 * An active and disconnected TypeC port prevents the HPD live state
524 	 * to get updated on other active/disconnected TypeC ports, so after
525 	 * a port gets disabled the CRTCs using other TypeC ports must be
526 	 * rechecked wrt. their link status.
527 	 */
528 	for (;;) {
529 		u32 old_mask = crtcs_forced_off;
530 
531 		for_each_intel_crtc(display->drm, crtc) {
532 			u32 crtc_mask = drm_crtc_mask(&crtc->base);
533 
534 			if (crtcs_forced_off & crtc_mask)
535 				continue;
536 
537 			if (intel_sanitize_crtc(crtc, ctx))
538 				crtcs_forced_off |= crtc_mask;
539 		}
540 		if (crtcs_forced_off == old_mask)
541 			break;
542 	}
543 
544 	for_each_intel_crtc(display->drm, crtc) {
545 		struct intel_crtc_state *crtc_state =
546 			to_intel_crtc_state(crtc->base.state);
547 
548 		intel_crtc_state_dump(crtc_state, NULL, "setup_hw_state");
549 	}
550 }
551 
552 static bool has_bogus_dpll_config(const struct intel_crtc_state *crtc_state)
553 {
554 	struct intel_display *display = to_intel_display(crtc_state);
555 
556 	/*
557 	 * Some SNB BIOSen (eg. ASUS K53SV) are known to misprogram
558 	 * the hardware when a high res displays plugged in. DPLL P
559 	 * divider is zero, and the pipe timings are bonkers. We'll
560 	 * try to disable everything in that case.
561 	 *
562 	 * FIXME would be nice to be able to sanitize this state
563 	 * without several WARNs, but for now let's take the easy
564 	 * road.
565 	 */
566 	return display->platform.sandybridge &&
567 		crtc_state->hw.active &&
568 		crtc_state->shared_dpll &&
569 		crtc_state->port_clock == 0;
570 }
571 
572 static void intel_sanitize_encoder(struct intel_encoder *encoder)
573 {
574 	struct intel_display *display = to_intel_display(encoder);
575 	struct intel_connector *connector;
576 	struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
577 	struct intel_crtc_state *crtc_state = crtc ?
578 		to_intel_crtc_state(crtc->base.state) : NULL;
579 	struct intel_pmdemand_state *pmdemand_state =
580 		to_intel_pmdemand_state(display->pmdemand.obj.state);
581 
582 	/*
583 	 * We need to check both for a crtc link (meaning that the encoder is
584 	 * active and trying to read from a pipe) and the pipe itself being
585 	 * active.
586 	 */
587 	bool has_active_crtc = crtc_state &&
588 		crtc_state->hw.active;
589 
590 	if (crtc_state && has_bogus_dpll_config(crtc_state)) {
591 		drm_dbg_kms(display->drm,
592 			    "BIOS has misprogrammed the hardware. Disabling pipe %c\n",
593 			    pipe_name(crtc->pipe));
594 		has_active_crtc = false;
595 	}
596 
597 	connector = intel_encoder_find_connector(encoder);
598 	if (connector && !has_active_crtc) {
599 		drm_dbg_kms(display->drm,
600 			    "[ENCODER:%d:%s] has active connectors but no active pipe!\n",
601 			    encoder->base.base.id,
602 			    encoder->base.name);
603 
604 		/* Clear the corresponding bit in pmdemand active phys mask */
605 		intel_pmdemand_update_phys_mask(display, encoder,
606 						pmdemand_state, false);
607 
608 		/*
609 		 * Connector is active, but has no active pipe. This is fallout
610 		 * from our resume register restoring. Disable the encoder
611 		 * manually again.
612 		 */
613 		if (crtc_state) {
614 			struct drm_encoder *best_encoder;
615 
616 			drm_dbg_kms(display->drm,
617 				    "[ENCODER:%d:%s] manually disabled\n",
618 				    encoder->base.base.id,
619 				    encoder->base.name);
620 
621 			/* avoid oopsing in case the hooks consult best_encoder */
622 			best_encoder = connector->base.state->best_encoder;
623 			connector->base.state->best_encoder = &encoder->base;
624 
625 			/* FIXME NULL atomic state passed! */
626 			if (encoder->disable)
627 				encoder->disable(NULL, encoder, crtc_state,
628 						 connector->base.state);
629 			if (encoder->post_disable)
630 				encoder->post_disable(NULL, encoder, crtc_state,
631 						      connector->base.state);
632 
633 			connector->base.state->best_encoder = best_encoder;
634 		}
635 		encoder->base.crtc = NULL;
636 
637 		/*
638 		 * Inconsistent output/port/pipe state happens presumably due to
639 		 * a bug in one of the get_hw_state functions. Or someplace else
640 		 * in our code, like the register restore mess on resume. Clamp
641 		 * things to off as a safer default.
642 		 */
643 		connector->base.dpms = DRM_MODE_DPMS_OFF;
644 		connector->base.encoder = NULL;
645 	}
646 
647 	/* notify opregion of the sanitized encoder state */
648 	intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
649 
650 	if (HAS_DDI(display))
651 		intel_ddi_sanitize_encoder_pll_mapping(encoder);
652 }
653 
654 /* FIXME read out full plane state for all planes */
655 static void readout_plane_state(struct intel_display *display)
656 {
657 	struct intel_plane *plane;
658 	struct intel_crtc *crtc;
659 
660 	for_each_intel_plane(display->drm, plane) {
661 		struct intel_plane_state *plane_state =
662 			to_intel_plane_state(plane->base.state);
663 		struct intel_crtc_state *crtc_state;
664 		enum pipe pipe = PIPE_A;
665 		bool visible;
666 
667 		visible = plane->get_hw_state(plane, &pipe);
668 
669 		crtc = intel_crtc_for_pipe(display, pipe);
670 		crtc_state = to_intel_crtc_state(crtc->base.state);
671 
672 		intel_set_plane_visible(crtc_state, plane_state, visible);
673 
674 		drm_dbg_kms(display->drm,
675 			    "[PLANE:%d:%s] hw state readout: %s, pipe %c\n",
676 			    plane->base.base.id, plane->base.name,
677 			    str_enabled_disabled(visible), pipe_name(pipe));
678 	}
679 
680 	for_each_intel_crtc(display->drm, crtc) {
681 		struct intel_crtc_state *crtc_state =
682 			to_intel_crtc_state(crtc->base.state);
683 
684 		intel_plane_fixup_bitmasks(crtc_state);
685 	}
686 }
687 
688 static void intel_modeset_readout_hw_state(struct intel_display *display)
689 {
690 	struct intel_pmdemand_state *pmdemand_state =
691 		to_intel_pmdemand_state(display->pmdemand.obj.state);
692 	enum pipe pipe;
693 	struct intel_crtc *crtc;
694 	struct intel_encoder *encoder;
695 	struct intel_connector *connector;
696 	struct drm_connector_list_iter conn_iter;
697 
698 	for_each_intel_crtc(display->drm, crtc) {
699 		struct intel_crtc_state *crtc_state =
700 			to_intel_crtc_state(crtc->base.state);
701 
702 		__drm_atomic_helper_crtc_destroy_state(&crtc_state->uapi);
703 		intel_crtc_free_hw_state(crtc_state);
704 		intel_crtc_state_reset(crtc_state, crtc);
705 
706 		intel_crtc_get_pipe_config(crtc_state);
707 
708 		crtc_state->hw.enable = crtc_state->hw.active;
709 
710 		crtc->base.enabled = crtc_state->hw.enable;
711 		crtc->active = crtc_state->hw.active;
712 
713 		drm_dbg_kms(display->drm,
714 			    "[CRTC:%d:%s] hw state readout: %s\n",
715 			    crtc->base.base.id, crtc->base.name,
716 			    str_enabled_disabled(crtc_state->hw.active));
717 	}
718 
719 	readout_plane_state(display);
720 
721 	for_each_intel_encoder(display->drm, encoder) {
722 		struct intel_crtc_state *crtc_state = NULL;
723 
724 		pipe = 0;
725 
726 		if (encoder->get_hw_state(encoder, &pipe)) {
727 			crtc = intel_crtc_for_pipe(display, pipe);
728 			crtc_state = to_intel_crtc_state(crtc->base.state);
729 
730 			encoder->base.crtc = &crtc->base;
731 			intel_encoder_get_config(encoder, crtc_state);
732 
733 			/* read out to secondary crtc as well for joiner */
734 			if (crtc_state->joiner_pipes) {
735 				struct intel_crtc *secondary_crtc;
736 
737 				/* encoder should read be linked to joiner primary */
738 				WARN_ON(intel_crtc_is_joiner_secondary(crtc_state));
739 
740 				for_each_intel_crtc_in_pipe_mask(display->drm, secondary_crtc,
741 								 intel_crtc_joiner_secondary_pipes(crtc_state)) {
742 					struct intel_crtc_state *secondary_crtc_state;
743 
744 					secondary_crtc_state = to_intel_crtc_state(secondary_crtc->base.state);
745 					intel_encoder_get_config(encoder, secondary_crtc_state);
746 				}
747 			}
748 
749 			intel_pmdemand_update_phys_mask(display, encoder,
750 							pmdemand_state,
751 							true);
752 		} else {
753 			intel_pmdemand_update_phys_mask(display, encoder,
754 							pmdemand_state,
755 							false);
756 
757 			encoder->base.crtc = NULL;
758 		}
759 
760 		if (encoder->sync_state)
761 			encoder->sync_state(encoder, crtc_state);
762 
763 		drm_dbg_kms(display->drm,
764 			    "[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
765 			    encoder->base.base.id, encoder->base.name,
766 			    str_enabled_disabled(encoder->base.crtc),
767 			    pipe_name(pipe));
768 	}
769 
770 	intel_dpll_readout_hw_state(display);
771 
772 	drm_connector_list_iter_begin(display->drm, &conn_iter);
773 	for_each_intel_connector_iter(connector, &conn_iter) {
774 		struct intel_crtc_state *crtc_state = NULL;
775 
776 		if (connector->get_hw_state(connector)) {
777 			struct intel_crtc *crtc;
778 
779 			connector->base.dpms = DRM_MODE_DPMS_ON;
780 
781 			encoder = intel_attached_encoder(connector);
782 			connector->base.encoder = &encoder->base;
783 
784 			crtc = to_intel_crtc(encoder->base.crtc);
785 			crtc_state = crtc ? to_intel_crtc_state(crtc->base.state) : NULL;
786 
787 			if (crtc_state && crtc_state->hw.active) {
788 				/*
789 				 * This has to be done during hardware readout
790 				 * because anything calling .crtc_disable may
791 				 * rely on the connector_mask being accurate.
792 				 */
793 				crtc_state->uapi.connector_mask |=
794 					drm_connector_mask(&connector->base);
795 				crtc_state->uapi.encoder_mask |=
796 					drm_encoder_mask(&encoder->base);
797 			}
798 		} else {
799 			connector->base.dpms = DRM_MODE_DPMS_OFF;
800 			connector->base.encoder = NULL;
801 		}
802 
803 		if (connector->sync_state)
804 			connector->sync_state(connector, crtc_state);
805 
806 		drm_dbg_kms(display->drm,
807 			    "[CONNECTOR:%d:%s] hw state readout: %s\n",
808 			    connector->base.base.id, connector->base.name,
809 			    str_enabled_disabled(connector->base.encoder));
810 	}
811 	drm_connector_list_iter_end(&conn_iter);
812 
813 	for_each_intel_crtc(display->drm, crtc) {
814 		struct intel_crtc_state *crtc_state =
815 			to_intel_crtc_state(crtc->base.state);
816 		struct intel_plane *plane;
817 
818 		/*
819 		 * The initial mode needs to be set in order to keep
820 		 * the atomic core happy. It wants a valid mode if the
821 		 * crtc's enabled, so we do the above call.
822 		 *
823 		 * But we don't set all the derived state fully, hence
824 		 * set a flag to indicate that a full recalculation is
825 		 * needed on the next commit.
826 		 */
827 		crtc_state->inherited = true;
828 
829 		if (crtc_state->hw.active) {
830 			intel_crtc_update_active_timings(crtc_state,
831 							 crtc_state->vrr.enable);
832 
833 			intel_crtc_copy_hw_to_uapi_state(crtc_state);
834 		}
835 
836 		for_each_intel_plane_on_crtc(display->drm, crtc, plane) {
837 			const struct intel_plane_state *plane_state =
838 				to_intel_plane_state(plane->base.state);
839 
840 			/*
841 			 * FIXME don't have the fb yet, so can't
842 			 * use intel_plane_data_rate() :(
843 			 */
844 			if (plane_state->uapi.visible)
845 				crtc_state->data_rate[plane->id] =
846 					4 * crtc_state->pixel_rate;
847 			/*
848 			 * FIXME don't have the fb yet, so can't
849 			 * use plane->min_cdclk() :(
850 			 */
851 			if (plane_state->uapi.visible && plane->min_cdclk) {
852 				if (crtc_state->double_wide || DISPLAY_VER(display) >= 10)
853 					crtc_state->min_cdclk[plane->id] =
854 						DIV_ROUND_UP(crtc_state->pixel_rate, 2);
855 				else
856 					crtc_state->min_cdclk[plane->id] =
857 						crtc_state->pixel_rate;
858 			}
859 			drm_dbg_kms(display->drm,
860 				    "[PLANE:%d:%s] min_cdclk %d kHz\n",
861 				    plane->base.base.id, plane->base.name,
862 				    crtc_state->min_cdclk[plane->id]);
863 		}
864 
865 		intel_pmdemand_update_port_clock(display, pmdemand_state, pipe,
866 						 crtc_state->port_clock);
867 	}
868 
869 	/* TODO move here (or even earlier?) on all platforms */
870 	if (DISPLAY_VER(display) >= 9)
871 		intel_wm_get_hw_state(display);
872 
873 	intel_bw_update_hw_state(display);
874 	intel_cdclk_update_hw_state(display);
875 
876 	intel_pmdemand_init_pmdemand_params(display, pmdemand_state);
877 }
878 
879 static void
880 get_encoder_power_domains(struct intel_display *display)
881 {
882 	struct intel_encoder *encoder;
883 
884 	for_each_intel_encoder(display->drm, encoder) {
885 		struct intel_crtc_state *crtc_state;
886 
887 		if (!encoder->get_power_domains)
888 			continue;
889 
890 		/*
891 		 * MST-primary and inactive encoders don't have a crtc state
892 		 * and neither of these require any power domain references.
893 		 */
894 		if (!encoder->base.crtc)
895 			continue;
896 
897 		crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
898 		encoder->get_power_domains(encoder, crtc_state);
899 	}
900 }
901 
902 static void intel_early_display_was(struct intel_display *display)
903 {
904 	/*
905 	 * Display WA #1185 WaDisableDARBFClkGating:glk,icl,ehl,tgl
906 	 * Also known as Wa_14010480278.
907 	 */
908 	if (IS_DISPLAY_VER(display, 10, 12))
909 		intel_de_rmw(display, GEN9_CLKGATE_DIS_0, 0, DARBF_GATING_DIS);
910 
911 	/*
912 	 * WaRsPkgCStateDisplayPMReq:hsw
913 	 * System hang if this isn't done before disabling all planes!
914 	 */
915 	if (display->platform.haswell)
916 		intel_de_rmw(display, CHICKEN_PAR1_1, 0, FORCE_ARB_IDLE_PLANES);
917 
918 	if (display->platform.kabylake || display->platform.coffeelake ||
919 	    display->platform.cometlake) {
920 		/* Display WA #1142:kbl,cfl,cml */
921 		intel_de_rmw(display, CHICKEN_PAR1_1,
922 			     KBL_ARB_FILL_SPARE_22, KBL_ARB_FILL_SPARE_22);
923 		intel_de_rmw(display, CHICKEN_MISC_2,
924 			     KBL_ARB_FILL_SPARE_13 | KBL_ARB_FILL_SPARE_14,
925 			     KBL_ARB_FILL_SPARE_14);
926 	}
927 }
928 
929 void intel_modeset_setup_hw_state(struct intel_display *display,
930 				  struct drm_modeset_acquire_ctx *ctx)
931 {
932 	struct intel_encoder *encoder;
933 	struct intel_crtc *crtc;
934 	intel_wakeref_t wakeref;
935 
936 	wakeref = intel_display_power_get(display, POWER_DOMAIN_INIT);
937 
938 	intel_early_display_was(display);
939 	intel_vga_disable(display);
940 
941 	intel_modeset_readout_hw_state(display);
942 
943 	/* HW state is read out, now we need to sanitize this mess. */
944 	get_encoder_power_domains(display);
945 
946 	intel_pch_sanitize(display);
947 
948 	intel_cmtg_sanitize(display);
949 
950 	/*
951 	 * intel_sanitize_plane_mapping() may need to do vblank
952 	 * waits, so we need vblank interrupts restored beforehand.
953 	 */
954 	for_each_intel_crtc(display->drm, crtc) {
955 		struct intel_crtc_state *crtc_state =
956 			to_intel_crtc_state(crtc->base.state);
957 
958 		intel_sanitize_fifo_underrun_reporting(crtc_state);
959 
960 		drm_crtc_vblank_reset(&crtc->base);
961 
962 		if (crtc_state->hw.active) {
963 			intel_dmc_enable_pipe(display, crtc->pipe);
964 			intel_crtc_vblank_on(crtc_state);
965 		}
966 	}
967 
968 	intel_fbc_sanitize(display);
969 
970 	intel_sanitize_plane_mapping(display);
971 
972 	for_each_intel_encoder(display->drm, encoder)
973 		intel_sanitize_encoder(encoder);
974 
975 	/*
976 	 * Sanitizing CRTCs needs their connector atomic state to be
977 	 * up-to-date, so ensure that already here.
978 	 */
979 	intel_modeset_update_connector_atomic_state(display);
980 
981 	intel_sanitize_all_crtcs(display, ctx);
982 
983 	intel_dpll_sanitize_state(display);
984 
985 	/* TODO move earlier on all platforms */
986 	if (DISPLAY_VER(display) < 9)
987 		intel_wm_get_hw_state(display);
988 	intel_wm_sanitize(display);
989 
990 	for_each_intel_crtc(display->drm, crtc) {
991 		struct intel_crtc_state *crtc_state =
992 			to_intel_crtc_state(crtc->base.state);
993 		struct intel_power_domain_mask put_domains;
994 
995 		intel_modeset_get_crtc_power_domains(crtc_state, &put_domains);
996 		if (drm_WARN_ON(display->drm, !bitmap_empty(put_domains.bits, POWER_DOMAIN_NUM)))
997 			intel_modeset_put_crtc_power_domains(crtc, &put_domains);
998 	}
999 
1000 	intel_display_power_put(display, POWER_DOMAIN_INIT, wakeref);
1001 
1002 	intel_power_domains_sanitize_state(display);
1003 }
1004