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