xref: /linux/drivers/gpu/drm/i915/display/intel_modeset_setup.c (revision c06b6cde2a1c3bcbb561bd57bb6f34eae9030921)
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->drm, 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->drm, 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->drm, 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->drm, temp_crtc, joiner_secondaries_mask)
282 		intel_crtc_disable_noatomic_begin(temp_crtc, ctx);
283 
284 	for_each_intel_crtc_in_pipe_mask(display->drm, temp_crtc, portsync_slaves_mask)
285 		intel_crtc_disable_noatomic_begin(temp_crtc, ctx);
286 
287 	for_each_intel_crtc_in_pipe_mask(display->drm, temp_crtc, portsync_master_mask)
288 		intel_crtc_disable_noatomic_begin(temp_crtc, ctx);
289 
290 	for_each_intel_crtc_in_pipe_mask(display->drm, 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 
338 	if (DISPLAY_INFO(display)->color.degamma_lut_size) {
339 		/* assume 1:1 mapping */
340 		drm_property_replace_blob(&crtc_state->hw.degamma_lut,
341 					  crtc_state->pre_csc_lut);
342 		drm_property_replace_blob(&crtc_state->hw.gamma_lut,
343 					  crtc_state->post_csc_lut);
344 	} else {
345 		/*
346 		 * ilk/snb hw may be configured for either pre_csc_lut
347 		 * or post_csc_lut, but we don't advertise degamma_lut as
348 		 * being available in the uapi since there is only one
349 		 * hardware LUT. Always assign the result of the readout
350 		 * to gamma_lut as that is the only valid source of LUTs
351 		 * in the uapi.
352 		 */
353 		drm_WARN_ON(display->drm, crtc_state->post_csc_lut &&
354 			    crtc_state->pre_csc_lut);
355 
356 		drm_property_replace_blob(&crtc_state->hw.degamma_lut,
357 					  NULL);
358 		drm_property_replace_blob(&crtc_state->hw.gamma_lut,
359 					  crtc_state->post_csc_lut ?:
360 					  crtc_state->pre_csc_lut);
361 	}
362 
363 	drm_property_replace_blob(&crtc_state->uapi.degamma_lut,
364 				  crtc_state->hw.degamma_lut);
365 	drm_property_replace_blob(&crtc_state->uapi.gamma_lut,
366 				  crtc_state->hw.gamma_lut);
367 	drm_property_replace_blob(&crtc_state->uapi.ctm,
368 				  crtc_state->hw.ctm);
369 }
370 
371 static void
372 intel_sanitize_plane_mapping(struct intel_display *display)
373 {
374 	struct intel_crtc *crtc;
375 
376 	if (DISPLAY_VER(display) >= 4)
377 		return;
378 
379 	for_each_intel_crtc(display->drm, crtc) {
380 		struct intel_plane *plane =
381 			to_intel_plane(crtc->base.primary);
382 		struct intel_crtc *plane_crtc;
383 		enum pipe pipe;
384 
385 		if (!plane->get_hw_state(plane, &pipe))
386 			continue;
387 
388 		if (pipe == crtc->pipe)
389 			continue;
390 
391 		drm_dbg_kms(display->drm,
392 			    "[PLANE:%d:%s] attached to the wrong pipe, disabling plane\n",
393 			    plane->base.base.id, plane->base.name);
394 
395 		plane_crtc = intel_crtc_for_pipe(display, pipe);
396 		intel_plane_disable_noatomic(plane_crtc, plane);
397 	}
398 }
399 
400 static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
401 {
402 	struct drm_device *dev = crtc->base.dev;
403 	struct intel_encoder *encoder;
404 
405 	for_each_encoder_on_crtc(dev, &crtc->base, encoder)
406 		return true;
407 
408 	return false;
409 }
410 
411 static bool intel_crtc_needs_link_reset(struct intel_crtc *crtc)
412 {
413 	struct drm_device *dev = crtc->base.dev;
414 	struct intel_encoder *encoder;
415 
416 	for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
417 		struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
418 
419 		if (dig_port && intel_tc_port_link_needs_reset(dig_port))
420 			return true;
421 	}
422 
423 	return false;
424 }
425 
426 static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
427 {
428 	struct intel_display *display = to_intel_display(encoder);
429 	struct drm_connector_list_iter conn_iter;
430 	struct intel_connector *connector;
431 	struct intel_connector *found_connector = NULL;
432 
433 	drm_connector_list_iter_begin(display->drm, &conn_iter);
434 	for_each_intel_connector_iter(connector, &conn_iter) {
435 		if (&encoder->base == connector->base.encoder) {
436 			found_connector = connector;
437 			break;
438 		}
439 	}
440 	drm_connector_list_iter_end(&conn_iter);
441 
442 	return found_connector;
443 }
444 
445 static void intel_sanitize_fifo_underrun_reporting(const struct intel_crtc_state *crtc_state)
446 {
447 	struct intel_display *display = to_intel_display(crtc_state);
448 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
449 
450 	/*
451 	 * We start out with underrun reporting disabled on active
452 	 * pipes to avoid races.
453 	 *
454 	 * Also on gmch platforms we dont have any hardware bits to
455 	 * disable the underrun reporting. Which means we need to start
456 	 * out with underrun reporting disabled also on inactive pipes,
457 	 * since otherwise we'll complain about the garbage we read when
458 	 * e.g. coming up after runtime pm.
459 	 *
460 	 * No protection against concurrent access is required - at
461 	 * worst a fifo underrun happens which also sets this to false.
462 	 */
463 	intel_init_fifo_underrun_reporting(display, crtc,
464 					   !crtc_state->hw.active &&
465 					   !HAS_GMCH(display));
466 }
467 
468 static bool intel_sanitize_crtc(struct intel_crtc *crtc,
469 				struct drm_modeset_acquire_ctx *ctx)
470 {
471 	struct intel_display *display = to_intel_display(crtc);
472 	struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
473 	bool needs_link_reset;
474 
475 	if (crtc_state->hw.active) {
476 		struct intel_plane *plane;
477 
478 		/* Disable everything but the primary plane */
479 		for_each_intel_plane_on_crtc(display->drm, crtc, plane) {
480 			const struct intel_plane_state *plane_state =
481 				to_intel_plane_state(plane->base.state);
482 
483 			if (plane_state->uapi.visible &&
484 			    plane->base.type != DRM_PLANE_TYPE_PRIMARY)
485 				intel_plane_disable_noatomic(crtc, plane);
486 		}
487 
488 		/* Disable any background color/etc. set by the BIOS */
489 		intel_color_commit_noarm(NULL, crtc_state);
490 		intel_color_commit_arm(NULL, crtc_state);
491 	}
492 
493 	if (!crtc_state->hw.active ||
494 	    intel_crtc_is_joiner_secondary(crtc_state))
495 		return false;
496 
497 	needs_link_reset = intel_crtc_needs_link_reset(crtc);
498 
499 	/*
500 	 * Adjust the state of the output pipe according to whether we have
501 	 * active connectors/encoders.
502 	 */
503 	if (!needs_link_reset && intel_crtc_has_encoders(crtc))
504 		return false;
505 
506 	intel_crtc_disable_noatomic(crtc, ctx);
507 
508 	/*
509 	 * The HPD state on other active/disconnected TC ports may be stuck in
510 	 * the connected state until this port is disabled and a ~10ms delay has
511 	 * passed, wait here for that so that sanitizing other CRTCs will see the
512 	 * up-to-date HPD state.
513 	 */
514 	if (needs_link_reset)
515 		msleep(20);
516 
517 	return true;
518 }
519 
520 static void intel_sanitize_all_crtcs(struct intel_display *display,
521 				     struct drm_modeset_acquire_ctx *ctx)
522 {
523 	struct intel_crtc *crtc;
524 	u32 crtcs_forced_off = 0;
525 
526 	/*
527 	 * An active and disconnected TypeC port prevents the HPD live state
528 	 * to get updated on other active/disconnected TypeC ports, so after
529 	 * a port gets disabled the CRTCs using other TypeC ports must be
530 	 * rechecked wrt. their link status.
531 	 */
532 	for (;;) {
533 		u32 old_mask = crtcs_forced_off;
534 
535 		for_each_intel_crtc(display->drm, crtc) {
536 			u32 crtc_mask = drm_crtc_mask(&crtc->base);
537 
538 			if (crtcs_forced_off & crtc_mask)
539 				continue;
540 
541 			if (intel_sanitize_crtc(crtc, ctx))
542 				crtcs_forced_off |= crtc_mask;
543 		}
544 		if (crtcs_forced_off == old_mask)
545 			break;
546 	}
547 
548 	for_each_intel_crtc(display->drm, crtc) {
549 		struct intel_crtc_state *crtc_state =
550 			to_intel_crtc_state(crtc->base.state);
551 
552 		intel_crtc_state_dump(crtc_state, NULL, "setup_hw_state");
553 	}
554 }
555 
556 static bool has_bogus_dpll_config(const struct intel_crtc_state *crtc_state)
557 {
558 	struct intel_display *display = to_intel_display(crtc_state);
559 
560 	/*
561 	 * Some SNB BIOSen (eg. ASUS K53SV) are known to misprogram
562 	 * the hardware when a high res displays plugged in. DPLL P
563 	 * divider is zero, and the pipe timings are bonkers. We'll
564 	 * try to disable everything in that case.
565 	 *
566 	 * FIXME would be nice to be able to sanitize this state
567 	 * without several WARNs, but for now let's take the easy
568 	 * road.
569 	 */
570 	return display->platform.sandybridge &&
571 		crtc_state->hw.active &&
572 		crtc_state->intel_dpll &&
573 		crtc_state->port_clock == 0;
574 }
575 
576 static void intel_sanitize_encoder(struct intel_encoder *encoder)
577 {
578 	struct intel_display *display = to_intel_display(encoder);
579 	struct intel_connector *connector;
580 	struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
581 	struct intel_crtc_state *crtc_state = crtc ?
582 		to_intel_crtc_state(crtc->base.state) : NULL;
583 	struct intel_pmdemand_state *pmdemand_state =
584 		to_intel_pmdemand_state(display->pmdemand.obj.state);
585 
586 	/*
587 	 * We need to check both for a crtc link (meaning that the encoder is
588 	 * active and trying to read from a pipe) and the pipe itself being
589 	 * active.
590 	 */
591 	bool has_active_crtc = crtc_state &&
592 		crtc_state->hw.active;
593 
594 	if (crtc_state && has_bogus_dpll_config(crtc_state)) {
595 		drm_dbg_kms(display->drm,
596 			    "BIOS has misprogrammed the hardware. Disabling pipe %c\n",
597 			    pipe_name(crtc->pipe));
598 		has_active_crtc = false;
599 	}
600 
601 	connector = intel_encoder_find_connector(encoder);
602 	if (connector && !has_active_crtc) {
603 		drm_dbg_kms(display->drm,
604 			    "[ENCODER:%d:%s] has active connectors but no active pipe!\n",
605 			    encoder->base.base.id,
606 			    encoder->base.name);
607 
608 		/* Clear the corresponding bit in pmdemand active phys mask */
609 		intel_pmdemand_update_phys_mask(display, encoder,
610 						pmdemand_state, false);
611 
612 		/*
613 		 * Connector is active, but has no active pipe. This is fallout
614 		 * from our resume register restoring. Disable the encoder
615 		 * manually again.
616 		 */
617 		if (crtc_state) {
618 			struct drm_encoder *best_encoder;
619 
620 			drm_dbg_kms(display->drm,
621 				    "[ENCODER:%d:%s] manually disabled\n",
622 				    encoder->base.base.id,
623 				    encoder->base.name);
624 
625 			/* avoid oopsing in case the hooks consult best_encoder */
626 			best_encoder = connector->base.state->best_encoder;
627 			connector->base.state->best_encoder = &encoder->base;
628 
629 			/* FIXME NULL atomic state passed! */
630 			if (encoder->disable)
631 				encoder->disable(NULL, encoder, crtc_state,
632 						 connector->base.state);
633 			if (encoder->post_disable)
634 				encoder->post_disable(NULL, encoder, crtc_state,
635 						      connector->base.state);
636 
637 			connector->base.state->best_encoder = best_encoder;
638 		}
639 		encoder->base.crtc = NULL;
640 
641 		/*
642 		 * Inconsistent output/port/pipe state happens presumably due to
643 		 * a bug in one of the get_hw_state functions. Or someplace else
644 		 * in our code, like the register restore mess on resume. Clamp
645 		 * things to off as a safer default.
646 		 */
647 		connector->base.dpms = DRM_MODE_DPMS_OFF;
648 		connector->base.encoder = NULL;
649 	}
650 
651 	/* notify opregion of the sanitized encoder state */
652 	intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
653 
654 	if (HAS_DDI(display))
655 		intel_ddi_sanitize_encoder_pll_mapping(encoder);
656 }
657 
658 /* FIXME read out full plane state for all planes */
659 static void readout_plane_state(struct intel_display *display)
660 {
661 	struct intel_plane *plane;
662 	struct intel_crtc *crtc;
663 
664 	for_each_intel_plane(display->drm, plane) {
665 		struct intel_plane_state *plane_state =
666 			to_intel_plane_state(plane->base.state);
667 		struct intel_crtc_state *crtc_state;
668 		enum pipe pipe = PIPE_A;
669 		bool visible;
670 
671 		visible = plane->get_hw_state(plane, &pipe);
672 
673 		crtc = intel_crtc_for_pipe(display, pipe);
674 		crtc_state = to_intel_crtc_state(crtc->base.state);
675 
676 		intel_set_plane_visible(crtc_state, plane_state, visible);
677 
678 		drm_dbg_kms(display->drm,
679 			    "[PLANE:%d:%s] hw state readout: %s, pipe %c\n",
680 			    plane->base.base.id, plane->base.name,
681 			    str_enabled_disabled(visible), pipe_name(pipe));
682 	}
683 
684 	for_each_intel_crtc(display->drm, crtc) {
685 		struct intel_crtc_state *crtc_state =
686 			to_intel_crtc_state(crtc->base.state);
687 
688 		intel_plane_fixup_bitmasks(crtc_state);
689 	}
690 }
691 
692 static void intel_modeset_readout_hw_state(struct intel_display *display)
693 {
694 	struct intel_pmdemand_state *pmdemand_state =
695 		to_intel_pmdemand_state(display->pmdemand.obj.state);
696 	enum pipe pipe;
697 	struct intel_crtc *crtc;
698 	struct intel_encoder *encoder;
699 	struct intel_connector *connector;
700 	struct drm_connector_list_iter conn_iter;
701 
702 	for_each_intel_crtc(display->drm, crtc) {
703 		struct intel_crtc_state *crtc_state =
704 			to_intel_crtc_state(crtc->base.state);
705 
706 		__drm_atomic_helper_crtc_destroy_state(&crtc_state->uapi);
707 		intel_crtc_free_hw_state(crtc_state);
708 		intel_crtc_state_reset(crtc_state, crtc);
709 
710 		intel_crtc_get_pipe_config(crtc_state);
711 
712 		crtc_state->hw.enable = crtc_state->hw.active;
713 
714 		crtc->base.enabled = crtc_state->hw.enable;
715 		crtc->active = crtc_state->hw.active;
716 
717 		drm_dbg_kms(display->drm,
718 			    "[CRTC:%d:%s] hw state readout: %s\n",
719 			    crtc->base.base.id, crtc->base.name,
720 			    str_enabled_disabled(crtc_state->hw.active));
721 	}
722 
723 	readout_plane_state(display);
724 
725 	for_each_intel_encoder(display->drm, encoder) {
726 		struct intel_crtc_state *crtc_state = NULL;
727 
728 		pipe = 0;
729 
730 		if (encoder->get_hw_state(encoder, &pipe)) {
731 			crtc = intel_crtc_for_pipe(display, pipe);
732 			crtc_state = to_intel_crtc_state(crtc->base.state);
733 
734 			encoder->base.crtc = &crtc->base;
735 			intel_encoder_get_config(encoder, crtc_state);
736 
737 			/* read out to secondary crtc as well for joiner */
738 			if (crtc_state->joiner_pipes) {
739 				struct intel_crtc *secondary_crtc;
740 
741 				/* encoder should read be linked to joiner primary */
742 				WARN_ON(intel_crtc_is_joiner_secondary(crtc_state));
743 
744 				for_each_intel_crtc_in_pipe_mask(display->drm, secondary_crtc,
745 								 intel_crtc_joiner_secondary_pipes(crtc_state)) {
746 					struct intel_crtc_state *secondary_crtc_state;
747 
748 					secondary_crtc_state = to_intel_crtc_state(secondary_crtc->base.state);
749 					intel_encoder_get_config(encoder, secondary_crtc_state);
750 				}
751 			}
752 
753 			intel_pmdemand_update_phys_mask(display, encoder,
754 							pmdemand_state,
755 							true);
756 		} else {
757 			intel_pmdemand_update_phys_mask(display, encoder,
758 							pmdemand_state,
759 							false);
760 
761 			encoder->base.crtc = NULL;
762 		}
763 
764 		if (encoder->sync_state)
765 			encoder->sync_state(encoder, crtc_state);
766 
767 		drm_dbg_kms(display->drm,
768 			    "[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
769 			    encoder->base.base.id, encoder->base.name,
770 			    str_enabled_disabled(encoder->base.crtc),
771 			    pipe_name(pipe));
772 	}
773 
774 	intel_dpll_readout_hw_state(display);
775 
776 	drm_connector_list_iter_begin(display->drm, &conn_iter);
777 	for_each_intel_connector_iter(connector, &conn_iter) {
778 		struct intel_crtc_state *crtc_state = NULL;
779 
780 		if (connector->get_hw_state(connector)) {
781 			struct intel_crtc *crtc;
782 
783 			connector->base.dpms = DRM_MODE_DPMS_ON;
784 
785 			encoder = intel_attached_encoder(connector);
786 			connector->base.encoder = &encoder->base;
787 
788 			crtc = to_intel_crtc(encoder->base.crtc);
789 			crtc_state = crtc ? to_intel_crtc_state(crtc->base.state) : NULL;
790 
791 			if (crtc_state && crtc_state->hw.active) {
792 				/*
793 				 * This has to be done during hardware readout
794 				 * because anything calling .crtc_disable may
795 				 * rely on the connector_mask being accurate.
796 				 */
797 				crtc_state->uapi.connector_mask |=
798 					drm_connector_mask(&connector->base);
799 				crtc_state->uapi.encoder_mask |=
800 					drm_encoder_mask(&encoder->base);
801 			}
802 		} else {
803 			connector->base.dpms = DRM_MODE_DPMS_OFF;
804 			connector->base.encoder = NULL;
805 		}
806 
807 		if (connector->sync_state)
808 			connector->sync_state(connector, crtc_state);
809 
810 		drm_dbg_kms(display->drm,
811 			    "[CONNECTOR:%d:%s] hw state readout: %s\n",
812 			    connector->base.base.id, connector->base.name,
813 			    str_enabled_disabled(connector->base.encoder));
814 	}
815 	drm_connector_list_iter_end(&conn_iter);
816 
817 	for_each_intel_crtc(display->drm, crtc) {
818 		struct intel_crtc_state *crtc_state =
819 			to_intel_crtc_state(crtc->base.state);
820 		struct intel_plane *plane;
821 
822 		/*
823 		 * The initial mode needs to be set in order to keep
824 		 * the atomic core happy. It wants a valid mode if the
825 		 * crtc's enabled, so we do the above call.
826 		 *
827 		 * But we don't set all the derived state fully, hence
828 		 * set a flag to indicate that a full recalculation is
829 		 * needed on the next commit.
830 		 */
831 		crtc_state->inherited = true;
832 
833 		if (crtc_state->hw.active) {
834 			intel_crtc_update_active_timings(crtc_state,
835 							 crtc_state->vrr.enable);
836 
837 			intel_crtc_copy_hw_to_uapi_state(crtc_state);
838 		}
839 
840 		for_each_intel_plane_on_crtc(display->drm, crtc, plane) {
841 			const struct intel_plane_state *plane_state =
842 				to_intel_plane_state(plane->base.state);
843 
844 			/*
845 			 * FIXME don't have the fb yet, so can't
846 			 * use intel_plane_data_rate() :(
847 			 */
848 			if (plane_state->uapi.visible)
849 				crtc_state->data_rate[plane->id] =
850 					4 * crtc_state->pixel_rate;
851 			/*
852 			 * FIXME don't have the fb yet, so can't
853 			 * use plane->min_cdclk() :(
854 			 */
855 			if (plane_state->uapi.visible && plane->min_cdclk) {
856 				if (crtc_state->double_wide || DISPLAY_VER(display) >= 10)
857 					crtc_state->plane_min_cdclk[plane->id] =
858 						DIV_ROUND_UP(crtc_state->pixel_rate, 2);
859 				else
860 					crtc_state->plane_min_cdclk[plane->id] =
861 						crtc_state->pixel_rate;
862 			}
863 			drm_dbg_kms(display->drm,
864 				    "[PLANE:%d:%s] min_cdclk %d kHz\n",
865 				    plane->base.base.id, plane->base.name,
866 				    crtc_state->plane_min_cdclk[plane->id]);
867 		}
868 
869 		crtc_state->min_cdclk = intel_crtc_min_cdclk(crtc_state);
870 
871 		drm_dbg_kms(display->drm, "[CRTC:%d:%s] min_cdclk %d kHz\n",
872 			    crtc->base.base.id, crtc->base.name, crtc_state->min_cdclk);
873 
874 		intel_pmdemand_update_port_clock(display, pmdemand_state, pipe,
875 						 crtc_state->port_clock);
876 	}
877 
878 	/* TODO move here (or even earlier?) on all platforms */
879 	if (DISPLAY_VER(display) >= 9)
880 		intel_wm_get_hw_state(display);
881 
882 	intel_bw_update_hw_state(display);
883 	intel_dbuf_bw_update_hw_state(display);
884 	intel_cdclk_update_hw_state(display);
885 
886 	intel_pmdemand_init_pmdemand_params(display, pmdemand_state);
887 }
888 
889 static void
890 get_encoder_power_domains(struct intel_display *display)
891 {
892 	struct intel_encoder *encoder;
893 
894 	for_each_intel_encoder(display->drm, encoder) {
895 		struct intel_crtc_state *crtc_state;
896 
897 		if (!encoder->get_power_domains)
898 			continue;
899 
900 		/*
901 		 * MST-primary and inactive encoders don't have a crtc state
902 		 * and neither of these require any power domain references.
903 		 */
904 		if (!encoder->base.crtc)
905 			continue;
906 
907 		crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
908 		encoder->get_power_domains(encoder, crtc_state);
909 	}
910 }
911 
912 static void intel_early_display_was(struct intel_display *display)
913 {
914 	/*
915 	 * Display WA #1185 WaDisableDARBFClkGating:glk,icl,ehl,tgl
916 	 * Also known as Wa_14010480278.
917 	 */
918 	if (intel_display_wa(display, INTEL_DISPLAY_WA_14010480278))
919 		intel_de_rmw(display, GEN9_CLKGATE_DIS_0, 0, DARBF_GATING_DIS);
920 
921 	/*
922 	 * WaRsPkgCStateDisplayPMReq:hsw
923 	 * System hang if this isn't done before disabling all planes!
924 	 */
925 	if (display->platform.haswell)
926 		intel_de_rmw(display, CHICKEN_PAR1_1, 0, FORCE_ARB_IDLE_PLANES);
927 
928 	if (display->platform.kabylake || display->platform.coffeelake ||
929 	    display->platform.cometlake) {
930 		/* Display WA #1142:kbl,cfl,cml */
931 		intel_de_rmw(display, CHICKEN_PAR1_1,
932 			     KBL_ARB_FILL_SPARE_22, KBL_ARB_FILL_SPARE_22);
933 		intel_de_rmw(display, CHICKEN_MISC_2,
934 			     KBL_ARB_FILL_SPARE_13 | KBL_ARB_FILL_SPARE_14,
935 			     KBL_ARB_FILL_SPARE_14);
936 	}
937 }
938 
939 void intel_modeset_setup_hw_state(struct intel_display *display,
940 				  struct drm_modeset_acquire_ctx *ctx)
941 {
942 	struct intel_encoder *encoder;
943 	struct intel_crtc *crtc;
944 	struct ref_tracker *wakeref;
945 
946 	wakeref = intel_display_power_get(display, POWER_DOMAIN_INIT);
947 
948 	intel_early_display_was(display);
949 	intel_vga_disable(display);
950 
951 	intel_modeset_readout_hw_state(display);
952 
953 	/* HW state is read out, now we need to sanitize this mess. */
954 	get_encoder_power_domains(display);
955 
956 	intel_pch_sanitize(display);
957 
958 	intel_cmtg_sanitize(display);
959 
960 	/*
961 	 * intel_sanitize_plane_mapping() may need to do vblank
962 	 * waits, so we need vblank interrupts restored beforehand.
963 	 */
964 	for_each_intel_crtc(display->drm, crtc) {
965 		struct intel_crtc_state *crtc_state =
966 			to_intel_crtc_state(crtc->base.state);
967 
968 		intel_sanitize_fifo_underrun_reporting(crtc_state);
969 
970 		drm_crtc_vblank_reset(&crtc->base);
971 
972 		if (crtc_state->hw.active) {
973 			intel_dmc_enable_pipe(crtc_state);
974 			intel_crtc_vblank_on(crtc_state);
975 		}
976 	}
977 
978 	intel_fbc_sanitize(display);
979 
980 	intel_sanitize_plane_mapping(display);
981 
982 	for_each_intel_encoder(display->drm, encoder)
983 		intel_sanitize_encoder(encoder);
984 
985 	/*
986 	 * Sanitizing CRTCs needs their connector atomic state to be
987 	 * up-to-date, so ensure that already here.
988 	 */
989 	intel_modeset_update_connector_atomic_state(display);
990 
991 	intel_sanitize_all_crtcs(display, ctx);
992 
993 	intel_dpll_sanitize_state(display);
994 
995 	/* TODO move earlier on all platforms */
996 	if (DISPLAY_VER(display) < 9)
997 		intel_wm_get_hw_state(display);
998 	intel_wm_sanitize(display);
999 
1000 	for_each_intel_crtc(display->drm, crtc) {
1001 		struct intel_crtc_state *crtc_state =
1002 			to_intel_crtc_state(crtc->base.state);
1003 		struct intel_power_domain_mask put_domains;
1004 
1005 		intel_modeset_get_crtc_power_domains(crtc_state, &put_domains);
1006 		if (drm_WARN_ON(display->drm, !bitmap_empty(put_domains.bits, POWER_DOMAIN_NUM)))
1007 			intel_modeset_put_crtc_power_domains(crtc, &put_domains);
1008 	}
1009 
1010 	intel_display_power_put(display, POWER_DOMAIN_INIT, wakeref);
1011 
1012 	intel_power_domains_sanitize_state(display);
1013 }
1014