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