xref: /linux/drivers/gpu/drm/i915/display/intel_display.c (revision 13c072b8e91a5ccb5855ca1ba6fe3ea467dbf94d)
1 /*
2  * Copyright © 2006-2007 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *	Eric Anholt <eric@anholt.net>
25  */
26 
27 #include <linux/dma-resv.h>
28 #include <linux/i2c.h>
29 #include <linux/input.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/string_helpers.h>
34 
35 #include <drm/display/drm_dp_helper.h>
36 #include <drm/display/drm_dp_tunnel.h>
37 #include <drm/drm_atomic.h>
38 #include <drm/drm_atomic_helper.h>
39 #include <drm/drm_atomic_uapi.h>
40 #include <drm/drm_damage_helper.h>
41 #include <drm/drm_edid.h>
42 #include <drm/drm_fixed.h>
43 #include <drm/drm_fourcc.h>
44 #include <drm/drm_print.h>
45 #include <drm/drm_probe_helper.h>
46 #include <drm/drm_rect.h>
47 #include <drm/drm_vblank.h>
48 
49 #include "g4x_dp.h"
50 #include "g4x_hdmi.h"
51 #include "hsw_ips.h"
52 #include "i915_config.h"
53 #include "i9xx_plane.h"
54 #include "i9xx_plane_regs.h"
55 #include "i9xx_wm.h"
56 #include "intel_alpm.h"
57 #include "intel_atomic.h"
58 #include "intel_audio.h"
59 #include "intel_bo.h"
60 #include "intel_bw.h"
61 #include "intel_casf.h"
62 #include "intel_cdclk.h"
63 #include "intel_clock_gating.h"
64 #include "intel_color.h"
65 #include "intel_crt.h"
66 #include "intel_crtc.h"
67 #include "intel_crtc_state_dump.h"
68 #include "intel_cursor.h"
69 #include "intel_cursor_regs.h"
70 #include "intel_cx0_phy.h"
71 #include "intel_ddi.h"
72 #include "intel_de.h"
73 #include "intel_display_driver.h"
74 #include "intel_display_power.h"
75 #include "intel_display_regs.h"
76 #include "intel_display_rpm.h"
77 #include "intel_display_types.h"
78 #include "intel_display_utils.h"
79 #include "intel_display_wa.h"
80 #include "intel_dmc.h"
81 #include "intel_dp.h"
82 #include "intel_dp_link_training.h"
83 #include "intel_dp_mst.h"
84 #include "intel_dp_tunnel.h"
85 #include "intel_dpll.h"
86 #include "intel_dpll_mgr.h"
87 #include "intel_dpt.h"
88 #include "intel_drrs.h"
89 #include "intel_dsb.h"
90 #include "intel_dsi.h"
91 #include "intel_dvo.h"
92 #include "intel_fb.h"
93 #include "intel_fbc.h"
94 #include "intel_fdi.h"
95 #include "intel_fifo_underrun.h"
96 #include "intel_flipq.h"
97 #include "intel_frontbuffer.h"
98 #include "intel_hdmi.h"
99 #include "intel_hotplug.h"
100 #include "intel_initial_plane.h"
101 #include "intel_link_bw.h"
102 #include "intel_lt_phy.h"
103 #include "intel_lvds.h"
104 #include "intel_lvds_regs.h"
105 #include "intel_modeset_setup.h"
106 #include "intel_modeset_verify.h"
107 #include "intel_overlay.h"
108 #include "intel_panel.h"
109 #include "intel_pch_display.h"
110 #include "intel_pch_refclk.h"
111 #include "intel_pfit.h"
112 #include "intel_pipe_crc.h"
113 #include "intel_plane.h"
114 #include "intel_pmdemand.h"
115 #include "intel_pps.h"
116 #include "intel_psr.h"
117 #include "intel_psr_regs.h"
118 #include "intel_sdvo.h"
119 #include "intel_snps_phy.h"
120 #include "intel_tc.h"
121 #include "intel_tdf.h"
122 #include "intel_tv.h"
123 #include "intel_vblank.h"
124 #include "intel_vdsc.h"
125 #include "intel_vdsc_regs.h"
126 #include "intel_vga.h"
127 #include "intel_vrr.h"
128 #include "intel_wm.h"
129 #include "skl_scaler.h"
130 #include "skl_universal_plane.h"
131 #include "skl_watermark.h"
132 #include "vlv_dsi.h"
133 #include "vlv_dsi_pll.h"
134 #include "vlv_dsi_regs.h"
135 
136 static void intel_set_transcoder_timings(const struct intel_crtc_state *crtc_state);
137 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state);
138 static void hsw_set_transconf(const struct intel_crtc_state *crtc_state);
139 static void bdw_set_pipe_misc(struct intel_dsb *dsb,
140 			      const struct intel_crtc_state *crtc_state);
141 
142 static bool is_hdr_mode(const struct intel_crtc_state *crtc_state)
143 {
144 	return (crtc_state->active_planes &
145 		~(icl_hdr_plane_mask() | BIT(PLANE_CURSOR))) == 0;
146 }
147 
148 /* WA Display #0827: Gen9:all */
149 static void
150 skl_wa_827(struct intel_display *display, enum pipe pipe, bool enable)
151 {
152 	intel_de_rmw(display, CLKGATE_DIS_PSL(pipe),
153 		     DUPS1_GATING_DIS | DUPS2_GATING_DIS,
154 		     enable ? DUPS1_GATING_DIS | DUPS2_GATING_DIS : 0);
155 }
156 
157 /* Wa_2006604312:icl,ehl */
158 static void
159 icl_wa_scalerclkgating(struct intel_display *display, enum pipe pipe,
160 		       bool enable)
161 {
162 	intel_de_rmw(display, CLKGATE_DIS_PSL(pipe),
163 		     DPFR_GATING_DIS,
164 		     enable ? DPFR_GATING_DIS : 0);
165 }
166 
167 /* Wa_1604331009:icl,jsl,ehl */
168 static void
169 icl_wa_cursorclkgating(struct intel_display *display, enum pipe pipe,
170 		       bool enable)
171 {
172 	intel_de_rmw(display, CLKGATE_DIS_PSL(pipe),
173 		     CURSOR_GATING_DIS,
174 		     enable ? CURSOR_GATING_DIS : 0);
175 }
176 
177 static bool
178 is_trans_port_sync_slave(const struct intel_crtc_state *crtc_state)
179 {
180 	return crtc_state->master_transcoder != INVALID_TRANSCODER;
181 }
182 
183 bool
184 is_trans_port_sync_master(const struct intel_crtc_state *crtc_state)
185 {
186 	return crtc_state->sync_mode_slaves_mask != 0;
187 }
188 
189 bool
190 is_trans_port_sync_mode(const struct intel_crtc_state *crtc_state)
191 {
192 	return is_trans_port_sync_master(crtc_state) ||
193 		is_trans_port_sync_slave(crtc_state);
194 }
195 
196 static enum pipe joiner_primary_pipe(const struct intel_crtc_state *crtc_state)
197 {
198 	return ffs(crtc_state->joiner_pipes) - 1;
199 }
200 
201 /*
202  * The following helper functions, despite being named for bigjoiner,
203  * are applicable to both bigjoiner and uncompressed joiner configurations.
204  */
205 static bool is_bigjoiner(const struct intel_crtc_state *crtc_state)
206 {
207 	return hweight8(crtc_state->joiner_pipes) >= 2;
208 }
209 
210 static u8 bigjoiner_primary_pipes(const struct intel_crtc_state *crtc_state)
211 {
212 	if (!is_bigjoiner(crtc_state))
213 		return 0;
214 
215 	return crtc_state->joiner_pipes & (0b01010101 << joiner_primary_pipe(crtc_state));
216 }
217 
218 static unsigned int bigjoiner_secondary_pipes(const struct intel_crtc_state *crtc_state)
219 {
220 	if (!is_bigjoiner(crtc_state))
221 		return 0;
222 
223 	return crtc_state->joiner_pipes & (0b10101010 << joiner_primary_pipe(crtc_state));
224 }
225 
226 bool intel_crtc_is_bigjoiner_primary(const struct intel_crtc_state *crtc_state)
227 {
228 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
229 
230 	if (!is_bigjoiner(crtc_state))
231 		return false;
232 
233 	return BIT(crtc->pipe) & bigjoiner_primary_pipes(crtc_state);
234 }
235 
236 bool intel_crtc_is_bigjoiner_secondary(const struct intel_crtc_state *crtc_state)
237 {
238 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
239 
240 	if (!is_bigjoiner(crtc_state))
241 		return false;
242 
243 	return BIT(crtc->pipe) & bigjoiner_secondary_pipes(crtc_state);
244 }
245 
246 u8 _intel_modeset_primary_pipes(const struct intel_crtc_state *crtc_state)
247 {
248 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
249 
250 	if (!is_bigjoiner(crtc_state))
251 		return BIT(crtc->pipe);
252 
253 	return bigjoiner_primary_pipes(crtc_state);
254 }
255 
256 u8 _intel_modeset_secondary_pipes(const struct intel_crtc_state *crtc_state)
257 {
258 	return bigjoiner_secondary_pipes(crtc_state);
259 }
260 
261 bool intel_crtc_is_ultrajoiner(const struct intel_crtc_state *crtc_state)
262 {
263 	return intel_crtc_num_joined_pipes(crtc_state) >= 4;
264 }
265 
266 static u8 ultrajoiner_primary_pipes(const struct intel_crtc_state *crtc_state)
267 {
268 	if (!intel_crtc_is_ultrajoiner(crtc_state))
269 		return 0;
270 
271 	return crtc_state->joiner_pipes & (0b00010001 << joiner_primary_pipe(crtc_state));
272 }
273 
274 bool intel_crtc_is_ultrajoiner_primary(const struct intel_crtc_state *crtc_state)
275 {
276 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
277 
278 	return intel_crtc_is_ultrajoiner(crtc_state) &&
279 	       BIT(crtc->pipe) & ultrajoiner_primary_pipes(crtc_state);
280 }
281 
282 /*
283  * The ultrajoiner enable bit doesn't seem to follow primary/secondary logic or
284  * any other logic, so lets just add helper function to
285  * at least hide this hassle..
286  */
287 static u8 ultrajoiner_enable_pipes(const struct intel_crtc_state *crtc_state)
288 {
289 	if (!intel_crtc_is_ultrajoiner(crtc_state))
290 		return 0;
291 
292 	return crtc_state->joiner_pipes & (0b01110111 << joiner_primary_pipe(crtc_state));
293 }
294 
295 bool intel_crtc_ultrajoiner_enable_needed(const struct intel_crtc_state *crtc_state)
296 {
297 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
298 
299 	return intel_crtc_is_ultrajoiner(crtc_state) &&
300 	       BIT(crtc->pipe) & ultrajoiner_enable_pipes(crtc_state);
301 }
302 
303 u8 intel_crtc_joiner_secondary_pipes(const struct intel_crtc_state *crtc_state)
304 {
305 	if (crtc_state->joiner_pipes)
306 		return crtc_state->joiner_pipes & ~BIT(joiner_primary_pipe(crtc_state));
307 	else
308 		return 0;
309 }
310 
311 bool intel_crtc_is_joiner_secondary(const struct intel_crtc_state *crtc_state)
312 {
313 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
314 
315 	return crtc_state->joiner_pipes &&
316 		crtc->pipe != joiner_primary_pipe(crtc_state);
317 }
318 
319 bool intel_crtc_is_joiner_primary(const struct intel_crtc_state *crtc_state)
320 {
321 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
322 
323 	return crtc_state->joiner_pipes &&
324 		crtc->pipe == joiner_primary_pipe(crtc_state);
325 }
326 
327 int intel_crtc_num_joined_pipes(const struct intel_crtc_state *crtc_state)
328 {
329 	return hweight8(intel_crtc_joined_pipe_mask(crtc_state));
330 }
331 
332 u8 intel_crtc_joined_pipe_mask(const struct intel_crtc_state *crtc_state)
333 {
334 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
335 
336 	return BIT(crtc->pipe) | crtc_state->joiner_pipes;
337 }
338 
339 struct intel_crtc *intel_primary_crtc(const struct intel_crtc_state *crtc_state)
340 {
341 	struct intel_display *display = to_intel_display(crtc_state);
342 
343 	if (intel_crtc_is_joiner_secondary(crtc_state))
344 		return intel_crtc_for_pipe(display, joiner_primary_pipe(crtc_state));
345 	else
346 		return to_intel_crtc(crtc_state->uapi.crtc);
347 }
348 
349 static void
350 intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
351 {
352 	struct intel_display *display = to_intel_display(old_crtc_state);
353 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
354 
355 	if (DISPLAY_VER(display) >= 4) {
356 		enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
357 
358 		/* Wait for the Pipe State to go off */
359 		if (intel_de_wait_for_clear_ms(display, TRANSCONF(display, cpu_transcoder),
360 					       TRANSCONF_STATE_ENABLE, 100))
361 			drm_WARN(display->drm, 1, "pipe_off wait timed out\n");
362 	} else {
363 		intel_wait_for_pipe_scanline_stopped(crtc);
364 	}
365 }
366 
367 void assert_transcoder(struct intel_display *display,
368 		       enum transcoder cpu_transcoder, bool state)
369 {
370 	bool cur_state;
371 	enum intel_display_power_domain power_domain;
372 	struct ref_tracker *wakeref;
373 
374 	/* we keep both pipes enabled on 830 */
375 	if (display->platform.i830)
376 		state = true;
377 
378 	power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
379 	wakeref = intel_display_power_get_if_enabled(display, power_domain);
380 	if (wakeref) {
381 		u32 val = intel_de_read(display,
382 					TRANSCONF(display, cpu_transcoder));
383 		cur_state = !!(val & TRANSCONF_ENABLE);
384 
385 		intel_display_power_put(display, power_domain, wakeref);
386 	} else {
387 		cur_state = false;
388 	}
389 
390 	INTEL_DISPLAY_STATE_WARN(display, cur_state != state,
391 				 "transcoder %s assertion failure (expected %s, current %s)\n",
392 				 transcoder_name(cpu_transcoder), str_on_off(state),
393 				 str_on_off(cur_state));
394 }
395 
396 static void assert_plane(struct intel_plane *plane, bool state)
397 {
398 	struct intel_display *display = to_intel_display(plane->base.dev);
399 	enum pipe pipe;
400 	bool cur_state;
401 
402 	cur_state = plane->get_hw_state(plane, &pipe);
403 
404 	INTEL_DISPLAY_STATE_WARN(display, cur_state != state,
405 				 "%s assertion failure (expected %s, current %s)\n",
406 				 plane->base.name, str_on_off(state),
407 				 str_on_off(cur_state));
408 }
409 
410 #define assert_plane_enabled(p) assert_plane(p, true)
411 #define assert_plane_disabled(p) assert_plane(p, false)
412 
413 static void assert_planes_disabled(struct intel_crtc *crtc)
414 {
415 	struct intel_display *display = to_intel_display(crtc);
416 	struct intel_plane *plane;
417 
418 	for_each_intel_plane_on_crtc(display->drm, crtc, plane)
419 		assert_plane_disabled(plane);
420 }
421 
422 void intel_enable_transcoder(const struct intel_crtc_state *new_crtc_state)
423 {
424 	struct intel_display *display = to_intel_display(new_crtc_state);
425 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
426 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
427 	enum pipe pipe = crtc->pipe;
428 	u32 val;
429 
430 	drm_dbg_kms(display->drm, "enabling pipe %c\n", pipe_name(pipe));
431 
432 	assert_planes_disabled(crtc);
433 
434 	/*
435 	 * A pipe without a PLL won't actually be able to drive bits from
436 	 * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
437 	 * need the check.
438 	 */
439 	if (HAS_GMCH(display)) {
440 		if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
441 			assert_dsi_pll_enabled(display);
442 		else
443 			assert_pll_enabled(display, pipe);
444 	} else {
445 		if (new_crtc_state->has_pch_encoder) {
446 			/* if driving the PCH, we need FDI enabled */
447 			assert_fdi_rx_pll_enabled(display,
448 						  intel_crtc_pch_transcoder(crtc));
449 			assert_fdi_tx_pll_enabled(display,
450 						  (enum pipe) cpu_transcoder);
451 		}
452 		/* FIXME: assert CPU port conditions for SNB+ */
453 	}
454 
455 	/* Wa_22012358565:adl-p */
456 	if (intel_display_wa(display, INTEL_DISPLAY_WA_22012358565))
457 		intel_de_rmw(display, PIPE_ARB_CTL(display, pipe),
458 			     0, PIPE_ARB_USE_PROG_SLOTS);
459 
460 	if (DISPLAY_VER(display) >= 14) {
461 		u32 clear = DP_DSC_INSERT_SF_AT_EOL_WA;
462 		u32 set = 0;
463 
464 		if (DISPLAY_VER(display) == 14)
465 			set |= DP_FEC_BS_JITTER_WA;
466 
467 		intel_de_rmw(display, CHICKEN_TRANS(display, cpu_transcoder),
468 			     clear, set);
469 	}
470 
471 	val = intel_de_read(display, TRANSCONF(display, cpu_transcoder));
472 	if (val & TRANSCONF_ENABLE) {
473 		/* we keep both pipes enabled on 830 */
474 		drm_WARN_ON(display->drm, !display->platform.i830);
475 		return;
476 	}
477 
478 	/* Wa_1409098942:adlp+ */
479 	if (DISPLAY_VER(display) >= 13 &&
480 	    new_crtc_state->dsc.compression_enable) {
481 		val &= ~TRANSCONF_PIXEL_COUNT_SCALING_MASK;
482 		val |= REG_FIELD_PREP(TRANSCONF_PIXEL_COUNT_SCALING_MASK,
483 				      TRANSCONF_PIXEL_COUNT_SCALING_X4);
484 	}
485 
486 	intel_de_write(display, TRANSCONF(display, cpu_transcoder),
487 		       val | TRANSCONF_ENABLE);
488 	intel_de_posting_read(display, TRANSCONF(display, cpu_transcoder));
489 
490 	/*
491 	 * Until the pipe starts PIPEDSL reads will return a stale value,
492 	 * which causes an apparent vblank timestamp jump when PIPEDSL
493 	 * resets to its proper value. That also messes up the frame count
494 	 * when it's derived from the timestamps. So let's wait for the
495 	 * pipe to start properly before we call drm_crtc_vblank_on()
496 	 */
497 	if (intel_crtc_max_vblank_count(new_crtc_state) == 0)
498 		intel_wait_for_pipe_scanline_moving(crtc);
499 }
500 
501 void intel_disable_transcoder(const struct intel_crtc_state *old_crtc_state)
502 {
503 	struct intel_display *display = to_intel_display(old_crtc_state);
504 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
505 	enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
506 	enum pipe pipe = crtc->pipe;
507 	u32 val;
508 
509 	drm_dbg_kms(display->drm, "disabling pipe %c\n", pipe_name(pipe));
510 
511 	/*
512 	 * Make sure planes won't keep trying to pump pixels to us,
513 	 * or we might hang the display.
514 	 */
515 	assert_planes_disabled(crtc);
516 
517 	val = intel_de_read(display, TRANSCONF(display, cpu_transcoder));
518 	if ((val & TRANSCONF_ENABLE) == 0)
519 		return;
520 
521 	/*
522 	 * Double wide has implications for planes
523 	 * so best keep it disabled when not needed.
524 	 */
525 	if (old_crtc_state->double_wide)
526 		val &= ~TRANSCONF_DOUBLE_WIDE;
527 
528 	/* Don't disable pipe or pipe PLLs if needed */
529 	if (!display->platform.i830)
530 		val &= ~TRANSCONF_ENABLE;
531 
532 	/* Wa_1409098942:adlp+ */
533 	if (DISPLAY_VER(display) >= 13 &&
534 	    old_crtc_state->dsc.compression_enable)
535 		val &= ~TRANSCONF_PIXEL_COUNT_SCALING_MASK;
536 
537 	intel_de_write(display, TRANSCONF(display, cpu_transcoder), val);
538 
539 	if (DISPLAY_VER(display) >= 12)
540 		intel_de_rmw(display, CHICKEN_TRANS(display, cpu_transcoder),
541 			     FECSTALL_DIS_DPTSTREAM_DPTTG, 0);
542 
543 	if ((val & TRANSCONF_ENABLE) == 0)
544 		intel_wait_for_pipe_off(old_crtc_state);
545 }
546 
547 u32 intel_plane_fb_max_stride(struct intel_display *display,
548 			      const struct drm_format_info *info,
549 			      u64 modifier)
550 {
551 	struct intel_crtc *crtc;
552 	struct intel_plane *plane;
553 
554 	/*
555 	 * We assume the primary plane for pipe A has
556 	 * the highest stride limits of them all,
557 	 * if in case pipe A is disabled, use the first pipe from pipe_mask.
558 	 */
559 	crtc = intel_first_crtc(display);
560 	if (!crtc)
561 		return 0;
562 
563 	plane = to_intel_plane(crtc->base.primary);
564 
565 	return plane->max_stride(plane, info, modifier,
566 				 DRM_MODE_ROTATE_0);
567 }
568 
569 u32 intel_dumb_fb_max_stride(struct drm_device *drm,
570 			     u32 pixel_format, u64 modifier)
571 {
572 	struct intel_display *display = to_intel_display(drm);
573 
574 	if (!HAS_DISPLAY(display))
575 		return 0;
576 
577 	return intel_plane_fb_max_stride(display,
578 					 drm_get_format_info(drm, pixel_format, modifier),
579 					 modifier);
580 }
581 
582 void intel_set_plane_visible(struct intel_crtc_state *crtc_state,
583 			     struct intel_plane_state *plane_state,
584 			     bool visible)
585 {
586 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
587 
588 	plane_state->uapi.visible = visible;
589 
590 	if (visible)
591 		crtc_state->uapi.plane_mask |= drm_plane_mask(&plane->base);
592 	else
593 		crtc_state->uapi.plane_mask &= ~drm_plane_mask(&plane->base);
594 }
595 
596 void intel_plane_fixup_bitmasks(struct intel_crtc_state *crtc_state)
597 {
598 	struct intel_display *display = to_intel_display(crtc_state);
599 	struct drm_plane *plane;
600 
601 	/*
602 	 * Active_planes aliases if multiple "primary" or cursor planes
603 	 * have been used on the same (or wrong) pipe. plane_mask uses
604 	 * unique ids, hence we can use that to reconstruct active_planes.
605 	 */
606 	crtc_state->enabled_planes = 0;
607 	crtc_state->active_planes = 0;
608 
609 	drm_for_each_plane_mask(plane, display->drm,
610 				crtc_state->uapi.plane_mask) {
611 		crtc_state->enabled_planes |= BIT(to_intel_plane(plane)->id);
612 		crtc_state->active_planes |= BIT(to_intel_plane(plane)->id);
613 	}
614 }
615 
616 void intel_plane_disable_noatomic(struct intel_crtc *crtc,
617 				  struct intel_plane *plane)
618 {
619 	struct intel_display *display = to_intel_display(crtc);
620 	struct intel_crtc_state *crtc_state =
621 		to_intel_crtc_state(crtc->base.state);
622 	struct intel_plane_state *plane_state =
623 		to_intel_plane_state(plane->base.state);
624 
625 	drm_dbg_kms(display->drm,
626 		    "Disabling [PLANE:%d:%s] on [CRTC:%d:%s]\n",
627 		    plane->base.base.id, plane->base.name,
628 		    crtc->base.base.id, crtc->base.name);
629 
630 	intel_plane_set_invisible(crtc_state, plane_state);
631 	intel_set_plane_visible(crtc_state, plane_state, false);
632 	intel_plane_fixup_bitmasks(crtc_state);
633 
634 	skl_wm_plane_disable_noatomic(crtc, plane);
635 
636 	if ((crtc_state->active_planes & ~BIT(PLANE_CURSOR)) == 0 &&
637 	    hsw_ips_disable(crtc_state)) {
638 		crtc_state->ips_enabled = false;
639 		intel_initial_plane_vblank_wait(crtc);
640 	}
641 
642 	/*
643 	 * Vblank time updates from the shadow to live plane control register
644 	 * are blocked if the memory self-refresh mode is active at that
645 	 * moment. So to make sure the plane gets truly disabled, disable
646 	 * first the self-refresh mode. The self-refresh enable bit in turn
647 	 * will be checked/applied by the HW only at the next frame start
648 	 * event which is after the vblank start event, so we need to have a
649 	 * wait-for-vblank between disabling the plane and the pipe.
650 	 */
651 	if (HAS_GMCH(display) &&
652 	    intel_set_memory_cxsr(display, false))
653 		intel_initial_plane_vblank_wait(crtc);
654 
655 	/*
656 	 * Gen2 reports pipe underruns whenever all planes are disabled.
657 	 * So disable underrun reporting before all the planes get disabled.
658 	 */
659 	if (DISPLAY_VER(display) == 2 && !crtc_state->active_planes)
660 		intel_set_cpu_fifo_underrun_reporting(display, crtc->pipe, false);
661 
662 	intel_plane_disable_arm(NULL, plane, crtc_state);
663 	intel_initial_plane_vblank_wait(crtc);
664 }
665 
666 unsigned int
667 intel_plane_fence_y_offset(const struct intel_plane_state *plane_state)
668 {
669 	int x = 0, y = 0;
670 
671 	intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
672 					  plane_state->view.color_plane[0].offset, 0);
673 
674 	return y;
675 }
676 
677 static void icl_set_pipe_chicken(const struct intel_crtc_state *crtc_state)
678 {
679 	struct intel_display *display = to_intel_display(crtc_state);
680 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
681 	enum pipe pipe = crtc->pipe;
682 	u32 tmp;
683 
684 	tmp = intel_de_read(display, PIPE_CHICKEN(pipe));
685 
686 	/*
687 	 * Display WA #1153: icl
688 	 * enable hardware to bypass the alpha math
689 	 * and rounding for per-pixel values 00 and 0xff
690 	 */
691 	tmp |= PER_PIXEL_ALPHA_BYPASS_EN;
692 	/*
693 	 * Display WA # 1605353570: icl
694 	 * Set the pixel rounding bit to 1 for allowing
695 	 * passthrough of Frame buffer pixels unmodified
696 	 * across pipe
697 	 */
698 	tmp |= PIXEL_ROUNDING_TRUNC_FB_PASSTHRU;
699 
700 	/*
701 	 * Underrun recovery must always be disabled on display 13+.
702 	 * DG2 chicken bit meaning is inverted compared to other platforms.
703 	 */
704 	if (display->platform.dg2)
705 		tmp &= ~UNDERRUN_RECOVERY_ENABLE_DG2;
706 	else if ((DISPLAY_VER(display) >= 13) && (DISPLAY_VER(display) < 30))
707 		tmp |= UNDERRUN_RECOVERY_DISABLE_ADLP;
708 
709 	/* Wa_14010547955:dg2 */
710 	if (intel_display_wa(display, INTEL_DISPLAY_WA_14010547955))
711 		tmp |= DG2_RENDER_CCSTAG_4_3_EN;
712 
713 	intel_de_write(display, PIPE_CHICKEN(pipe), tmp);
714 }
715 
716 bool intel_has_pending_fb_unpin(struct intel_display *display)
717 {
718 	struct drm_crtc *crtc;
719 	bool cleanup_done;
720 
721 	drm_for_each_crtc(crtc, display->drm) {
722 		struct drm_crtc_commit *commit;
723 		spin_lock(&crtc->commit_lock);
724 		commit = list_first_entry_or_null(&crtc->commit_list,
725 						  struct drm_crtc_commit, commit_entry);
726 		cleanup_done = commit ?
727 			try_wait_for_completion(&commit->cleanup_done) : true;
728 		spin_unlock(&crtc->commit_lock);
729 
730 		if (cleanup_done)
731 			continue;
732 
733 		intel_crtc_wait_for_next_vblank(to_intel_crtc(crtc));
734 
735 		return true;
736 	}
737 
738 	return false;
739 }
740 
741 /*
742  * Finds the encoder associated with the given CRTC. This can only be
743  * used when we know that the CRTC isn't feeding multiple encoders!
744  */
745 struct intel_encoder *
746 intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
747 			   const struct intel_crtc_state *crtc_state)
748 {
749 	const struct drm_connector_state *connector_state;
750 	const struct drm_connector *connector;
751 	struct intel_encoder *encoder = NULL;
752 	struct intel_crtc *primary_crtc;
753 	int num_encoders = 0;
754 	int i;
755 
756 	primary_crtc = intel_primary_crtc(crtc_state);
757 
758 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
759 		if (connector_state->crtc != &primary_crtc->base)
760 			continue;
761 
762 		encoder = to_intel_encoder(connector_state->best_encoder);
763 		num_encoders++;
764 	}
765 
766 	drm_WARN(state->base.dev, num_encoders != 1,
767 		 "%d encoders for pipe %c\n",
768 		 num_encoders, pipe_name(primary_crtc->pipe));
769 
770 	return encoder;
771 }
772 
773 static void intel_crtc_dpms_overlay_disable(struct intel_crtc *crtc)
774 {
775 	if (crtc->overlay)
776 		(void) intel_overlay_switch_off(crtc->overlay);
777 
778 	/* Let userspace switch the overlay on again. In most cases userspace
779 	 * has to recompute where to put it anyway.
780 	 */
781 }
782 
783 static bool needs_nv12_wa(const struct intel_crtc_state *crtc_state)
784 {
785 	struct intel_display *display = to_intel_display(crtc_state);
786 
787 	if (!crtc_state->nv12_planes)
788 		return false;
789 
790 	/* WA Display #0827: Gen9:all */
791 	if (DISPLAY_VER(display) == 9)
792 		return true;
793 
794 	return false;
795 }
796 
797 static bool needs_scalerclk_wa(const struct intel_crtc_state *crtc_state)
798 {
799 	struct intel_display *display = to_intel_display(crtc_state);
800 
801 	/* Wa_2006604312:icl,ehl */
802 	if (crtc_state->scaler_state.scaler_users > 0 && DISPLAY_VER(display) == 11)
803 		return true;
804 
805 	return false;
806 }
807 
808 static bool needs_cursorclk_wa(const struct intel_crtc_state *crtc_state)
809 {
810 	struct intel_display *display = to_intel_display(crtc_state);
811 
812 	/* Wa_1604331009:icl,jsl,ehl */
813 	if (is_hdr_mode(crtc_state) &&
814 	    crtc_state->active_planes & BIT(PLANE_CURSOR) &&
815 	    DISPLAY_VER(display) == 11)
816 		return true;
817 
818 	return false;
819 }
820 
821 static void intel_async_flip_vtd_wa(struct intel_display *display,
822 				    enum pipe pipe, bool enable)
823 {
824 	if (DISPLAY_VER(display) == 9) {
825 		/*
826 		 * "Plane N stretch max must be programmed to 11b (x1)
827 		 *  when Async flips are enabled on that plane."
828 		 */
829 		intel_de_rmw(display, CHICKEN_PIPESL_1(pipe),
830 			     SKL_PLANE1_STRETCH_MAX_MASK,
831 			     enable ? SKL_PLANE1_STRETCH_MAX_X1 : SKL_PLANE1_STRETCH_MAX_X8);
832 	} else {
833 		/* Also needed on HSW/BDW albeit undocumented */
834 		intel_de_rmw(display, CHICKEN_PIPESL_1(pipe),
835 			     HSW_PRI_STRETCH_MAX_MASK,
836 			     enable ? HSW_PRI_STRETCH_MAX_X1 : HSW_PRI_STRETCH_MAX_X8);
837 	}
838 }
839 
840 static bool needs_async_flip_vtd_wa(const struct intel_crtc_state *crtc_state)
841 {
842 	struct intel_display *display = to_intel_display(crtc_state);
843 
844 	return crtc_state->uapi.async_flip && intel_display_vtd_active(display) &&
845 		(DISPLAY_VER(display) == 9 || display->platform.broadwell ||
846 		 display->platform.haswell);
847 }
848 
849 static void intel_encoders_audio_enable(struct intel_atomic_state *state,
850 					struct intel_crtc *crtc)
851 {
852 	const struct intel_crtc_state *crtc_state =
853 		intel_atomic_get_new_crtc_state(state, crtc);
854 	const struct drm_connector_state *conn_state;
855 	struct drm_connector *conn;
856 	int i;
857 
858 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
859 		struct intel_encoder *encoder =
860 			to_intel_encoder(conn_state->best_encoder);
861 
862 		if (conn_state->crtc != &crtc->base)
863 			continue;
864 
865 		if (encoder->audio_enable)
866 			encoder->audio_enable(encoder, crtc_state, conn_state);
867 	}
868 }
869 
870 static void intel_encoders_audio_disable(struct intel_atomic_state *state,
871 					 struct intel_crtc *crtc)
872 {
873 	const struct intel_crtc_state *old_crtc_state =
874 		intel_atomic_get_old_crtc_state(state, crtc);
875 	const struct drm_connector_state *old_conn_state;
876 	struct drm_connector *conn;
877 	int i;
878 
879 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
880 		struct intel_encoder *encoder =
881 			to_intel_encoder(old_conn_state->best_encoder);
882 
883 		if (old_conn_state->crtc != &crtc->base)
884 			continue;
885 
886 		if (encoder->audio_disable)
887 			encoder->audio_disable(encoder, old_crtc_state, old_conn_state);
888 	}
889 }
890 
891 #define is_enabling(feature, old_crtc_state, new_crtc_state) \
892 	((!(old_crtc_state)->feature || intel_crtc_needs_modeset(new_crtc_state)) && \
893 	 (new_crtc_state)->feature)
894 #define is_disabling(feature, old_crtc_state, new_crtc_state) \
895 	((old_crtc_state)->feature && \
896 	 (!(new_crtc_state)->feature || intel_crtc_needs_modeset(new_crtc_state)))
897 
898 static bool planes_enabling(const struct intel_crtc_state *old_crtc_state,
899 			    const struct intel_crtc_state *new_crtc_state)
900 {
901 	if (!new_crtc_state->hw.active)
902 		return false;
903 
904 	return is_enabling(active_planes, old_crtc_state, new_crtc_state);
905 }
906 
907 static bool planes_disabling(const struct intel_crtc_state *old_crtc_state,
908 			     const struct intel_crtc_state *new_crtc_state)
909 {
910 	if (!old_crtc_state->hw.active)
911 		return false;
912 
913 	return is_disabling(active_planes, old_crtc_state, new_crtc_state);
914 }
915 
916 static bool vrr_params_changed(const struct intel_crtc_state *old_crtc_state,
917 			       const struct intel_crtc_state *new_crtc_state)
918 {
919 	return old_crtc_state->vrr.flipline != new_crtc_state->vrr.flipline ||
920 		old_crtc_state->vrr.vmin != new_crtc_state->vrr.vmin ||
921 		old_crtc_state->vrr.vmax != new_crtc_state->vrr.vmax ||
922 		old_crtc_state->vrr.guardband != new_crtc_state->vrr.guardband ||
923 		old_crtc_state->vrr.pipeline_full != new_crtc_state->vrr.pipeline_full ||
924 		old_crtc_state->vrr.vsync_start != new_crtc_state->vrr.vsync_start ||
925 		old_crtc_state->vrr.vsync_end != new_crtc_state->vrr.vsync_end;
926 }
927 
928 static bool cmrr_params_changed(const struct intel_crtc_state *old_crtc_state,
929 				const struct intel_crtc_state *new_crtc_state)
930 {
931 	return old_crtc_state->cmrr.cmrr_m != new_crtc_state->cmrr.cmrr_m ||
932 		old_crtc_state->cmrr.cmrr_n != new_crtc_state->cmrr.cmrr_n;
933 }
934 
935 static bool intel_crtc_vrr_enabling(struct intel_atomic_state *state,
936 				    struct intel_crtc *crtc)
937 {
938 	const struct intel_crtc_state *old_crtc_state =
939 		intel_atomic_get_old_crtc_state(state, crtc);
940 	const struct intel_crtc_state *new_crtc_state =
941 		intel_atomic_get_new_crtc_state(state, crtc);
942 
943 	if (!new_crtc_state->hw.active)
944 		return false;
945 
946 	return is_enabling(vrr.enable, old_crtc_state, new_crtc_state) ||
947 		(new_crtc_state->vrr.enable &&
948 		 (new_crtc_state->update_m_n || new_crtc_state->update_lrr ||
949 		  vrr_params_changed(old_crtc_state, new_crtc_state)));
950 }
951 
952 bool intel_crtc_vrr_disabling(struct intel_atomic_state *state,
953 			      struct intel_crtc *crtc)
954 {
955 	const struct intel_crtc_state *old_crtc_state =
956 		intel_atomic_get_old_crtc_state(state, crtc);
957 	const struct intel_crtc_state *new_crtc_state =
958 		intel_atomic_get_new_crtc_state(state, crtc);
959 
960 	if (!old_crtc_state->hw.active)
961 		return false;
962 
963 	return is_disabling(vrr.enable, old_crtc_state, new_crtc_state) ||
964 		(old_crtc_state->vrr.enable &&
965 		 (new_crtc_state->update_m_n || new_crtc_state->update_lrr ||
966 		  vrr_params_changed(old_crtc_state, new_crtc_state)));
967 }
968 
969 static bool audio_enabling(const struct intel_crtc_state *old_crtc_state,
970 			   const struct intel_crtc_state *new_crtc_state)
971 {
972 	if (!new_crtc_state->hw.active)
973 		return false;
974 
975 	return is_enabling(has_audio, old_crtc_state, new_crtc_state) ||
976 		(new_crtc_state->has_audio &&
977 		 memcmp(old_crtc_state->eld, new_crtc_state->eld, MAX_ELD_BYTES) != 0);
978 }
979 
980 static bool audio_disabling(const struct intel_crtc_state *old_crtc_state,
981 			    const struct intel_crtc_state *new_crtc_state)
982 {
983 	if (!old_crtc_state->hw.active)
984 		return false;
985 
986 	return is_disabling(has_audio, old_crtc_state, new_crtc_state) ||
987 		(old_crtc_state->has_audio &&
988 		 memcmp(old_crtc_state->eld, new_crtc_state->eld, MAX_ELD_BYTES) != 0);
989 }
990 
991 static bool intel_casf_enabling(const struct intel_crtc_state *new_crtc_state,
992 				const struct intel_crtc_state *old_crtc_state)
993 {
994 	if (!new_crtc_state->hw.active)
995 		return false;
996 
997 	return is_enabling(hw.casf_params.casf_enable, old_crtc_state, new_crtc_state);
998 }
999 
1000 static bool intel_casf_disabling(const struct intel_crtc_state *old_crtc_state,
1001 				 const struct intel_crtc_state *new_crtc_state)
1002 {
1003 	if (!new_crtc_state->hw.active)
1004 		return false;
1005 
1006 	return is_disabling(hw.casf_params.casf_enable, old_crtc_state, new_crtc_state);
1007 }
1008 
1009 static bool intel_crtc_lobf_enabling(const struct intel_crtc_state *old_crtc_state,
1010 				     const struct intel_crtc_state *new_crtc_state)
1011 {
1012 	if (!new_crtc_state->hw.active)
1013 		return false;
1014 
1015 	return is_enabling(has_lobf, old_crtc_state, new_crtc_state) ||
1016 	       (new_crtc_state->has_lobf &&
1017 		(new_crtc_state->update_lrr || new_crtc_state->update_m_n));
1018 }
1019 
1020 static bool intel_crtc_lobf_disabling(const struct intel_crtc_state *old_crtc_state,
1021 				      const struct intel_crtc_state *new_crtc_state)
1022 {
1023 	if (!old_crtc_state->hw.active)
1024 		return false;
1025 
1026 	return is_disabling(has_lobf, old_crtc_state, new_crtc_state) ||
1027 		(old_crtc_state->has_lobf &&
1028 		 (new_crtc_state->update_lrr || new_crtc_state->update_m_n));
1029 }
1030 
1031 #undef is_disabling
1032 #undef is_enabling
1033 
1034 static void intel_post_plane_update(struct intel_atomic_state *state,
1035 				    struct intel_crtc *crtc)
1036 {
1037 	struct intel_display *display = to_intel_display(state);
1038 	const struct intel_crtc_state *old_crtc_state =
1039 		intel_atomic_get_old_crtc_state(state, crtc);
1040 	const struct intel_crtc_state *new_crtc_state =
1041 		intel_atomic_get_new_crtc_state(state, crtc);
1042 	enum pipe pipe = crtc->pipe;
1043 
1044 	intel_frontbuffer_flip(display, new_crtc_state->fb_bits);
1045 
1046 	if (new_crtc_state->update_wm_post && new_crtc_state->hw.active)
1047 		intel_update_watermarks(display);
1048 
1049 	intel_fbc_post_update(state, crtc);
1050 
1051 	if (needs_async_flip_vtd_wa(old_crtc_state) &&
1052 	    !needs_async_flip_vtd_wa(new_crtc_state))
1053 		intel_async_flip_vtd_wa(display, pipe, false);
1054 
1055 	if (needs_nv12_wa(old_crtc_state) &&
1056 	    !needs_nv12_wa(new_crtc_state))
1057 		skl_wa_827(display, pipe, false);
1058 
1059 	if (needs_scalerclk_wa(old_crtc_state) &&
1060 	    !needs_scalerclk_wa(new_crtc_state))
1061 		icl_wa_scalerclkgating(display, pipe, false);
1062 
1063 	if (needs_cursorclk_wa(old_crtc_state) &&
1064 	    !needs_cursorclk_wa(new_crtc_state))
1065 		icl_wa_cursorclkgating(display, pipe, false);
1066 
1067 	if (intel_crtc_needs_color_update(new_crtc_state))
1068 		intel_color_post_update(new_crtc_state);
1069 
1070 	if (audio_enabling(old_crtc_state, new_crtc_state))
1071 		intel_encoders_audio_enable(state, crtc);
1072 
1073 	if (intel_display_wa(display, INTEL_DISPLAY_WA_14011503117)) {
1074 		if (old_crtc_state->pch_pfit.enabled != new_crtc_state->pch_pfit.enabled)
1075 			adl_scaler_ecc_unmask(new_crtc_state);
1076 	}
1077 
1078 	if (intel_crtc_lobf_enabling(old_crtc_state, new_crtc_state))
1079 		intel_alpm_lobf_enable(new_crtc_state);
1080 
1081 	intel_psr_post_plane_update(state, crtc);
1082 }
1083 
1084 static void intel_post_plane_update_after_readout(struct intel_atomic_state *state,
1085 						  struct intel_crtc *crtc)
1086 {
1087 	const struct intel_crtc_state *new_crtc_state =
1088 		intel_atomic_get_new_crtc_state(state, crtc);
1089 
1090 	/* Must be done after gamma readout due to HSW split gamma vs. IPS w/a */
1091 	hsw_ips_post_update(state, crtc);
1092 
1093 	/*
1094 	 * Activate DRRS after state readout to avoid
1095 	 * dp_m_n vs. dp_m2_n2 confusion on BDW+.
1096 	 */
1097 	intel_drrs_activate(new_crtc_state);
1098 }
1099 
1100 static void intel_crtc_enable_flip_done(struct intel_atomic_state *state,
1101 					struct intel_crtc *crtc)
1102 {
1103 	const struct intel_crtc_state *crtc_state =
1104 		intel_atomic_get_new_crtc_state(state, crtc);
1105 	u8 update_planes = crtc_state->update_planes;
1106 	const struct intel_plane_state __maybe_unused *plane_state;
1107 	struct intel_plane *plane;
1108 	int i;
1109 
1110 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1111 		if (plane->pipe == crtc->pipe &&
1112 		    update_planes & BIT(plane->id))
1113 			plane->enable_flip_done(plane);
1114 	}
1115 }
1116 
1117 static void intel_crtc_disable_flip_done(struct intel_atomic_state *state,
1118 					 struct intel_crtc *crtc)
1119 {
1120 	const struct intel_crtc_state *crtc_state =
1121 		intel_atomic_get_new_crtc_state(state, crtc);
1122 	u8 update_planes = crtc_state->update_planes;
1123 	const struct intel_plane_state __maybe_unused *plane_state;
1124 	struct intel_plane *plane;
1125 	int i;
1126 
1127 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1128 		if (plane->pipe == crtc->pipe &&
1129 		    update_planes & BIT(plane->id))
1130 			plane->disable_flip_done(plane);
1131 	}
1132 }
1133 
1134 static void intel_crtc_async_flip_disable_wa(struct intel_atomic_state *state,
1135 					     struct intel_crtc *crtc)
1136 {
1137 	const struct intel_crtc_state *old_crtc_state =
1138 		intel_atomic_get_old_crtc_state(state, crtc);
1139 	const struct intel_crtc_state *new_crtc_state =
1140 		intel_atomic_get_new_crtc_state(state, crtc);
1141 	u8 disable_async_flip_planes = old_crtc_state->async_flip_planes &
1142 				       ~new_crtc_state->async_flip_planes;
1143 	const struct intel_plane_state *old_plane_state;
1144 	struct intel_plane *plane;
1145 	bool need_vbl_wait = false;
1146 	int i;
1147 
1148 	for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
1149 		if (plane->need_async_flip_toggle_wa &&
1150 		    plane->pipe == crtc->pipe &&
1151 		    disable_async_flip_planes & BIT(plane->id)) {
1152 			/*
1153 			 * Apart from the async flip bit we want to
1154 			 * preserve the old state for the plane.
1155 			 */
1156 			intel_plane_async_flip(NULL, plane,
1157 					       old_crtc_state, old_plane_state, false);
1158 			need_vbl_wait = true;
1159 		}
1160 	}
1161 
1162 	if (need_vbl_wait)
1163 		intel_crtc_wait_for_next_vblank(crtc);
1164 }
1165 
1166 static void intel_pre_plane_update(struct intel_atomic_state *state,
1167 				   struct intel_crtc *crtc)
1168 {
1169 	struct intel_display *display = to_intel_display(state);
1170 	const struct intel_crtc_state *old_crtc_state =
1171 		intel_atomic_get_old_crtc_state(state, crtc);
1172 	const struct intel_crtc_state *new_crtc_state =
1173 		intel_atomic_get_new_crtc_state(state, crtc);
1174 	enum pipe pipe = crtc->pipe;
1175 
1176 	if (intel_crtc_lobf_disabling(old_crtc_state, new_crtc_state))
1177 		intel_alpm_lobf_disable(new_crtc_state);
1178 
1179 	intel_psr_pre_plane_update(state, crtc);
1180 
1181 	if (intel_crtc_vrr_disabling(state, crtc)) {
1182 		intel_vrr_disable(old_crtc_state);
1183 		intel_vrr_dcb_reset(old_crtc_state, crtc);
1184 		intel_crtc_update_active_timings(old_crtc_state, false);
1185 	}
1186 
1187 	if (audio_disabling(old_crtc_state, new_crtc_state))
1188 		intel_encoders_audio_disable(state, crtc);
1189 
1190 	if (intel_casf_disabling(old_crtc_state, new_crtc_state))
1191 		intel_casf_disable(new_crtc_state);
1192 
1193 	intel_drrs_deactivate(old_crtc_state);
1194 
1195 	if (hsw_ips_pre_update(state, crtc))
1196 		intel_crtc_wait_for_next_vblank(crtc);
1197 
1198 	if (intel_fbc_pre_update(state, crtc))
1199 		intel_crtc_wait_for_next_vblank(crtc);
1200 
1201 	if (!needs_async_flip_vtd_wa(old_crtc_state) &&
1202 	    needs_async_flip_vtd_wa(new_crtc_state))
1203 		intel_async_flip_vtd_wa(display, pipe, true);
1204 
1205 	/* Display WA 827 */
1206 	if (!needs_nv12_wa(old_crtc_state) &&
1207 	    needs_nv12_wa(new_crtc_state))
1208 		skl_wa_827(display, pipe, true);
1209 
1210 	/* Wa_2006604312:icl,ehl */
1211 	if (!needs_scalerclk_wa(old_crtc_state) &&
1212 	    needs_scalerclk_wa(new_crtc_state))
1213 		icl_wa_scalerclkgating(display, pipe, true);
1214 
1215 	/* Wa_1604331009:icl,jsl,ehl */
1216 	if (!needs_cursorclk_wa(old_crtc_state) &&
1217 	    needs_cursorclk_wa(new_crtc_state))
1218 		icl_wa_cursorclkgating(display, pipe, true);
1219 
1220 	/*
1221 	 * Vblank time updates from the shadow to live plane control register
1222 	 * are blocked if the memory self-refresh mode is active at that
1223 	 * moment. So to make sure the plane gets truly disabled, disable
1224 	 * first the self-refresh mode. The self-refresh enable bit in turn
1225 	 * will be checked/applied by the HW only at the next frame start
1226 	 * event which is after the vblank start event, so we need to have a
1227 	 * wait-for-vblank between disabling the plane and the pipe.
1228 	 */
1229 	if (HAS_GMCH(display) && old_crtc_state->hw.active &&
1230 	    new_crtc_state->disable_cxsr && intel_set_memory_cxsr(display, false))
1231 		intel_crtc_wait_for_next_vblank(crtc);
1232 
1233 	/*
1234 	 * IVB workaround: must disable low power watermarks for at least
1235 	 * one frame before enabling scaling.  LP watermarks can be re-enabled
1236 	 * when scaling is disabled.
1237 	 *
1238 	 * WaCxSRDisabledForSpriteScaling:ivb
1239 	 */
1240 	if (!HAS_GMCH(display) && old_crtc_state->hw.active &&
1241 	    new_crtc_state->disable_cxsr && ilk_disable_cxsr(display))
1242 		intel_crtc_wait_for_next_vblank(crtc);
1243 
1244 	/*
1245 	 * If we're doing a modeset we don't need to do any
1246 	 * pre-vblank watermark programming here.
1247 	 */
1248 	if (!intel_crtc_needs_modeset(new_crtc_state)) {
1249 		/*
1250 		 * For platforms that support atomic watermarks, program the
1251 		 * 'intermediate' watermarks immediately.  On pre-gen9 platforms, these
1252 		 * will be the intermediate values that are safe for both pre- and
1253 		 * post- vblank; when vblank happens, the 'active' values will be set
1254 		 * to the final 'target' values and we'll do this again to get the
1255 		 * optimal watermarks.  For gen9+ platforms, the values we program here
1256 		 * will be the final target values which will get automatically latched
1257 		 * at vblank time; no further programming will be necessary.
1258 		 *
1259 		 * If a platform hasn't been transitioned to atomic watermarks yet,
1260 		 * we'll continue to update watermarks the old way, if flags tell
1261 		 * us to.
1262 		 */
1263 		if (!intel_initial_watermarks(state, crtc))
1264 			if (new_crtc_state->update_wm_pre)
1265 				intel_update_watermarks(display);
1266 	}
1267 
1268 	/*
1269 	 * Gen2 reports pipe underruns whenever all planes are disabled.
1270 	 * So disable underrun reporting before all the planes get disabled.
1271 	 *
1272 	 * We do this after .initial_watermarks() so that we have a
1273 	 * chance of catching underruns with the intermediate watermarks
1274 	 * vs. the old plane configuration.
1275 	 */
1276 	if (DISPLAY_VER(display) == 2 && planes_disabling(old_crtc_state, new_crtc_state))
1277 		intel_set_cpu_fifo_underrun_reporting(display, pipe, false);
1278 
1279 	/*
1280 	 * WA for platforms where async address update enable bit
1281 	 * is double buffered and only latched at start of vblank.
1282 	 */
1283 	if (old_crtc_state->async_flip_planes & ~new_crtc_state->async_flip_planes)
1284 		intel_crtc_async_flip_disable_wa(state, crtc);
1285 }
1286 
1287 static void intel_crtc_disable_planes(struct intel_atomic_state *state,
1288 				      struct intel_crtc *crtc)
1289 {
1290 	struct intel_display *display = to_intel_display(state);
1291 	const struct intel_crtc_state *new_crtc_state =
1292 		intel_atomic_get_new_crtc_state(state, crtc);
1293 	unsigned int update_mask = new_crtc_state->update_planes;
1294 	const struct intel_plane_state *old_plane_state;
1295 	struct intel_plane *plane;
1296 	unsigned fb_bits = 0;
1297 	int i;
1298 
1299 	intel_crtc_dpms_overlay_disable(crtc);
1300 
1301 	for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
1302 		if (crtc->pipe != plane->pipe ||
1303 		    !(update_mask & BIT(plane->id)))
1304 			continue;
1305 
1306 		intel_plane_disable_arm(NULL, plane, new_crtc_state);
1307 
1308 		if (old_plane_state->uapi.visible)
1309 			fb_bits |= plane->frontbuffer_bit;
1310 	}
1311 
1312 	intel_frontbuffer_flip(display, fb_bits);
1313 }
1314 
1315 static void intel_encoders_update_prepare(struct intel_atomic_state *state)
1316 {
1317 	struct intel_display *display = to_intel_display(state);
1318 	struct intel_crtc_state *new_crtc_state, *old_crtc_state;
1319 	struct intel_crtc *crtc;
1320 	int i;
1321 
1322 	/*
1323 	 * Make sure the DPLL state is up-to-date for fastset TypeC ports after non-blocking commits.
1324 	 * TODO: Update the DPLL state for all cases in the encoder->update_prepare() hook.
1325 	 */
1326 	if (display->dpll.mgr) {
1327 		for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1328 			if (intel_crtc_needs_modeset(new_crtc_state))
1329 				continue;
1330 
1331 			new_crtc_state->intel_dpll = old_crtc_state->intel_dpll;
1332 			new_crtc_state->dpll_hw_state = old_crtc_state->dpll_hw_state;
1333 		}
1334 	}
1335 }
1336 
1337 static void intel_encoders_pre_pll_enable(struct intel_atomic_state *state,
1338 					  struct intel_crtc *crtc)
1339 {
1340 	const struct intel_crtc_state *crtc_state =
1341 		intel_atomic_get_new_crtc_state(state, crtc);
1342 	const struct drm_connector_state *conn_state;
1343 	struct drm_connector *conn;
1344 	int i;
1345 
1346 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
1347 		struct intel_encoder *encoder =
1348 			to_intel_encoder(conn_state->best_encoder);
1349 
1350 		if (conn_state->crtc != &crtc->base)
1351 			continue;
1352 
1353 		if (encoder->pre_pll_enable)
1354 			encoder->pre_pll_enable(state, encoder,
1355 						crtc_state, conn_state);
1356 	}
1357 }
1358 
1359 static void intel_encoders_pre_enable(struct intel_atomic_state *state,
1360 				      struct intel_crtc *crtc)
1361 {
1362 	const struct intel_crtc_state *crtc_state =
1363 		intel_atomic_get_new_crtc_state(state, crtc);
1364 	const struct drm_connector_state *conn_state;
1365 	struct drm_connector *conn;
1366 	int i;
1367 
1368 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
1369 		struct intel_encoder *encoder =
1370 			to_intel_encoder(conn_state->best_encoder);
1371 
1372 		if (conn_state->crtc != &crtc->base)
1373 			continue;
1374 
1375 		if (encoder->pre_enable)
1376 			encoder->pre_enable(state, encoder,
1377 					    crtc_state, conn_state);
1378 	}
1379 }
1380 
1381 static void intel_encoders_enable(struct intel_atomic_state *state,
1382 				  struct intel_crtc *crtc)
1383 {
1384 	const struct intel_crtc_state *crtc_state =
1385 		intel_atomic_get_new_crtc_state(state, crtc);
1386 	const struct drm_connector_state *conn_state;
1387 	struct drm_connector *conn;
1388 	int i;
1389 
1390 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
1391 		struct intel_encoder *encoder =
1392 			to_intel_encoder(conn_state->best_encoder);
1393 
1394 		if (conn_state->crtc != &crtc->base)
1395 			continue;
1396 
1397 		if (encoder->enable)
1398 			encoder->enable(state, encoder,
1399 					crtc_state, conn_state);
1400 		intel_opregion_notify_encoder(encoder, true);
1401 	}
1402 }
1403 
1404 static void intel_encoders_disable(struct intel_atomic_state *state,
1405 				   struct intel_crtc *crtc)
1406 {
1407 	const struct intel_crtc_state *old_crtc_state =
1408 		intel_atomic_get_old_crtc_state(state, crtc);
1409 	const struct drm_connector_state *old_conn_state;
1410 	struct drm_connector *conn;
1411 	int i;
1412 
1413 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
1414 		struct intel_encoder *encoder =
1415 			to_intel_encoder(old_conn_state->best_encoder);
1416 
1417 		if (old_conn_state->crtc != &crtc->base)
1418 			continue;
1419 
1420 		intel_opregion_notify_encoder(encoder, false);
1421 		if (encoder->disable)
1422 			encoder->disable(state, encoder,
1423 					 old_crtc_state, old_conn_state);
1424 	}
1425 }
1426 
1427 static void intel_encoders_post_disable(struct intel_atomic_state *state,
1428 					struct intel_crtc *crtc)
1429 {
1430 	const struct intel_crtc_state *old_crtc_state =
1431 		intel_atomic_get_old_crtc_state(state, crtc);
1432 	const struct drm_connector_state *old_conn_state;
1433 	struct drm_connector *conn;
1434 	int i;
1435 
1436 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
1437 		struct intel_encoder *encoder =
1438 			to_intel_encoder(old_conn_state->best_encoder);
1439 
1440 		if (old_conn_state->crtc != &crtc->base)
1441 			continue;
1442 
1443 		if (encoder->post_disable)
1444 			encoder->post_disable(state, encoder,
1445 					      old_crtc_state, old_conn_state);
1446 	}
1447 }
1448 
1449 static void intel_encoders_post_pll_disable(struct intel_atomic_state *state,
1450 					    struct intel_crtc *crtc)
1451 {
1452 	const struct intel_crtc_state *old_crtc_state =
1453 		intel_atomic_get_old_crtc_state(state, crtc);
1454 	const struct drm_connector_state *old_conn_state;
1455 	struct drm_connector *conn;
1456 	int i;
1457 
1458 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
1459 		struct intel_encoder *encoder =
1460 			to_intel_encoder(old_conn_state->best_encoder);
1461 
1462 		if (old_conn_state->crtc != &crtc->base)
1463 			continue;
1464 
1465 		if (encoder->post_pll_disable)
1466 			encoder->post_pll_disable(state, encoder,
1467 						  old_crtc_state, old_conn_state);
1468 	}
1469 }
1470 
1471 static void intel_encoders_update_pipe(struct intel_atomic_state *state,
1472 				       struct intel_crtc *crtc)
1473 {
1474 	const struct intel_crtc_state *crtc_state =
1475 		intel_atomic_get_new_crtc_state(state, crtc);
1476 	const struct drm_connector_state *conn_state;
1477 	struct drm_connector *conn;
1478 	int i;
1479 
1480 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
1481 		struct intel_encoder *encoder =
1482 			to_intel_encoder(conn_state->best_encoder);
1483 
1484 		if (conn_state->crtc != &crtc->base)
1485 			continue;
1486 
1487 		if (encoder->update_pipe)
1488 			encoder->update_pipe(state, encoder,
1489 					     crtc_state, conn_state);
1490 	}
1491 }
1492 
1493 static void ilk_configure_cpu_transcoder(const struct intel_crtc_state *crtc_state)
1494 {
1495 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1496 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1497 
1498 	if (crtc_state->has_pch_encoder) {
1499 		intel_cpu_transcoder_set_m1_n1(crtc, cpu_transcoder,
1500 					       &crtc_state->fdi_m_n);
1501 	} else if (intel_crtc_has_dp_encoder(crtc_state)) {
1502 		intel_cpu_transcoder_set_m1_n1(crtc, cpu_transcoder,
1503 					       &crtc_state->dp_m_n);
1504 		intel_cpu_transcoder_set_m2_n2(crtc, cpu_transcoder,
1505 					       &crtc_state->dp_m2_n2);
1506 	}
1507 
1508 	intel_set_transcoder_timings(crtc_state);
1509 
1510 	ilk_set_pipeconf(crtc_state);
1511 }
1512 
1513 static void ilk_crtc_enable(struct intel_atomic_state *state,
1514 			    struct intel_crtc *crtc)
1515 {
1516 	struct intel_display *display = to_intel_display(crtc);
1517 	const struct intel_crtc_state *new_crtc_state =
1518 		intel_atomic_get_new_crtc_state(state, crtc);
1519 	enum pipe pipe = crtc->pipe;
1520 
1521 	if (drm_WARN_ON(display->drm, crtc->active))
1522 		return;
1523 
1524 	/*
1525 	 * Sometimes spurious CPU pipe underruns happen during FDI
1526 	 * training, at least with VGA+HDMI cloning. Suppress them.
1527 	 *
1528 	 * On ILK we get an occasional spurious CPU pipe underruns
1529 	 * between eDP port A enable and vdd enable. Also PCH port
1530 	 * enable seems to result in the occasional CPU pipe underrun.
1531 	 *
1532 	 * Spurious PCH underruns also occur during PCH enabling.
1533 	 */
1534 	intel_set_cpu_fifo_underrun_reporting(display, pipe, false);
1535 	intel_set_pch_fifo_underrun_reporting(display, pipe, false);
1536 
1537 	ilk_configure_cpu_transcoder(new_crtc_state);
1538 
1539 	intel_set_pipe_src_size(new_crtc_state);
1540 
1541 	crtc->active = true;
1542 
1543 	intel_encoders_pre_enable(state, crtc);
1544 
1545 	if (new_crtc_state->has_pch_encoder) {
1546 		ilk_pch_pre_enable(state, crtc);
1547 	} else {
1548 		assert_fdi_tx_disabled(display, pipe);
1549 		assert_fdi_rx_disabled(display, pipe);
1550 	}
1551 
1552 	ilk_pfit_enable(new_crtc_state);
1553 
1554 	/*
1555 	 * On ILK+ LUT must be loaded before the pipe is running but with
1556 	 * clocks enabled
1557 	 */
1558 	intel_color_modeset(new_crtc_state);
1559 
1560 	intel_initial_watermarks(state, crtc);
1561 	intel_enable_transcoder(new_crtc_state);
1562 
1563 	if (new_crtc_state->has_pch_encoder)
1564 		ilk_pch_enable(state, crtc);
1565 
1566 	intel_crtc_vblank_on(new_crtc_state);
1567 
1568 	intel_encoders_enable(state, crtc);
1569 
1570 	if (HAS_PCH_CPT(display))
1571 		intel_wait_for_pipe_scanline_moving(crtc);
1572 
1573 	/*
1574 	 * Must wait for vblank to avoid spurious PCH FIFO underruns.
1575 	 * And a second vblank wait is needed at least on ILK with
1576 	 * some interlaced HDMI modes. Let's do the double wait always
1577 	 * in case there are more corner cases we don't know about.
1578 	 */
1579 	if (new_crtc_state->has_pch_encoder) {
1580 		intel_crtc_wait_for_next_vblank(crtc);
1581 		intel_crtc_wait_for_next_vblank(crtc);
1582 	}
1583 	intel_set_cpu_fifo_underrun_reporting(display, pipe, true);
1584 	intel_set_pch_fifo_underrun_reporting(display, pipe, true);
1585 }
1586 
1587 /* Display WA #1180: WaDisableScalarClockGating: glk */
1588 static bool glk_need_scaler_clock_gating_wa(const struct intel_crtc_state *crtc_state)
1589 {
1590 	struct intel_display *display = to_intel_display(crtc_state);
1591 
1592 	return DISPLAY_VER(display) == 10 && crtc_state->pch_pfit.enabled;
1593 }
1594 
1595 static void glk_pipe_scaler_clock_gating_wa(struct intel_crtc *crtc, bool enable)
1596 {
1597 	struct intel_display *display = to_intel_display(crtc);
1598 	u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
1599 
1600 	intel_de_rmw(display, CLKGATE_DIS_PSL(crtc->pipe),
1601 		     mask, enable ? mask : 0);
1602 }
1603 
1604 static void hsw_set_linetime_wm(const struct intel_crtc_state *crtc_state)
1605 {
1606 	struct intel_display *display = to_intel_display(crtc_state);
1607 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1608 
1609 	intel_de_write(display, WM_LINETIME(crtc->pipe),
1610 		       HSW_LINETIME(crtc_state->linetime) |
1611 		       HSW_IPS_LINETIME(crtc_state->ips_linetime));
1612 }
1613 
1614 static void hsw_set_frame_start_delay(const struct intel_crtc_state *crtc_state)
1615 {
1616 	struct intel_display *display = to_intel_display(crtc_state);
1617 
1618 	intel_de_rmw(display, CHICKEN_TRANS(display, crtc_state->cpu_transcoder),
1619 		     HSW_FRAME_START_DELAY_MASK,
1620 		     HSW_FRAME_START_DELAY(crtc_state->framestart_delay - 1));
1621 }
1622 
1623 static void hsw_configure_cpu_transcoder(const struct intel_crtc_state *crtc_state)
1624 {
1625 	struct intel_display *display = to_intel_display(crtc_state);
1626 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1627 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1628 
1629 	if (crtc_state->has_pch_encoder) {
1630 		intel_cpu_transcoder_set_m1_n1(crtc, cpu_transcoder,
1631 					       &crtc_state->fdi_m_n);
1632 	} else if (intel_crtc_has_dp_encoder(crtc_state)) {
1633 		intel_cpu_transcoder_set_m1_n1(crtc, cpu_transcoder,
1634 					       &crtc_state->dp_m_n);
1635 		intel_cpu_transcoder_set_m2_n2(crtc, cpu_transcoder,
1636 					       &crtc_state->dp_m2_n2);
1637 	}
1638 
1639 	intel_set_transcoder_timings(crtc_state);
1640 
1641 	if (cpu_transcoder != TRANSCODER_EDP)
1642 		intel_de_write(display, TRANS_MULT(display, cpu_transcoder),
1643 			       crtc_state->pixel_multiplier - 1);
1644 
1645 	hsw_set_frame_start_delay(crtc_state);
1646 
1647 	hsw_set_transconf(crtc_state);
1648 }
1649 
1650 static void hsw_crtc_enable(struct intel_atomic_state *state,
1651 			    struct intel_crtc *crtc)
1652 {
1653 	struct intel_display *display = to_intel_display(state);
1654 	const struct intel_crtc_state *new_crtc_state =
1655 		intel_atomic_get_new_crtc_state(state, crtc);
1656 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
1657 	struct intel_crtc *pipe_crtc;
1658 	int i;
1659 
1660 	if (drm_WARN_ON(display->drm, crtc->active))
1661 		return;
1662 	for_each_pipe_crtc_modeset_enable(display, pipe_crtc, new_crtc_state, i) {
1663 		const struct intel_crtc_state *new_pipe_crtc_state =
1664 			intel_atomic_get_new_crtc_state(state, pipe_crtc);
1665 
1666 		intel_dmc_enable_pipe(new_pipe_crtc_state);
1667 	}
1668 
1669 	intel_encoders_pre_pll_enable(state, crtc);
1670 
1671 	if (new_crtc_state->intel_dpll)
1672 		intel_dpll_enable(new_crtc_state);
1673 
1674 	intel_encoders_pre_enable(state, crtc);
1675 
1676 	for_each_pipe_crtc_modeset_enable(display, pipe_crtc, new_crtc_state, i) {
1677 		const struct intel_crtc_state *pipe_crtc_state =
1678 			intel_atomic_get_new_crtc_state(state, pipe_crtc);
1679 
1680 		intel_dsc_enable(pipe_crtc_state);
1681 
1682 		if (HAS_UNCOMPRESSED_JOINER(display))
1683 			intel_uncompressed_joiner_enable(pipe_crtc_state);
1684 
1685 		intel_set_pipe_src_size(pipe_crtc_state);
1686 
1687 		if (DISPLAY_VER(display) >= 9 || display->platform.broadwell)
1688 			bdw_set_pipe_misc(NULL, pipe_crtc_state);
1689 	}
1690 
1691 	if (!transcoder_is_dsi(cpu_transcoder))
1692 		hsw_configure_cpu_transcoder(new_crtc_state);
1693 
1694 	for_each_pipe_crtc_modeset_enable(display, pipe_crtc, new_crtc_state, i) {
1695 		const struct intel_crtc_state *pipe_crtc_state =
1696 			intel_atomic_get_new_crtc_state(state, pipe_crtc);
1697 
1698 		pipe_crtc->active = true;
1699 
1700 		if (glk_need_scaler_clock_gating_wa(pipe_crtc_state))
1701 			glk_pipe_scaler_clock_gating_wa(pipe_crtc, true);
1702 
1703 		if (DISPLAY_VER(display) >= 9)
1704 			skl_pfit_enable(pipe_crtc_state);
1705 		else
1706 			ilk_pfit_enable(pipe_crtc_state);
1707 
1708 		/*
1709 		 * On ILK+ LUT must be loaded before the pipe is running but with
1710 		 * clocks enabled
1711 		 */
1712 		intel_color_modeset(pipe_crtc_state);
1713 
1714 		hsw_set_linetime_wm(pipe_crtc_state);
1715 
1716 		if (DISPLAY_VER(display) >= 11)
1717 			icl_set_pipe_chicken(pipe_crtc_state);
1718 
1719 		intel_initial_watermarks(state, pipe_crtc);
1720 	}
1721 
1722 	intel_encoders_enable(state, crtc);
1723 
1724 	for_each_pipe_crtc_modeset_enable(display, pipe_crtc, new_crtc_state, i) {
1725 		const struct intel_crtc_state *pipe_crtc_state =
1726 			intel_atomic_get_new_crtc_state(state, pipe_crtc);
1727 		enum pipe hsw_workaround_pipe;
1728 
1729 		if (glk_need_scaler_clock_gating_wa(pipe_crtc_state)) {
1730 			intel_crtc_wait_for_next_vblank(pipe_crtc);
1731 			glk_pipe_scaler_clock_gating_wa(pipe_crtc, false);
1732 		}
1733 
1734 		/*
1735 		 * If we change the relative order between pipe/planes
1736 		 * enabling, we need to change the workaround.
1737 		 */
1738 		hsw_workaround_pipe = pipe_crtc_state->hsw_workaround_pipe;
1739 		if (display->platform.haswell && hsw_workaround_pipe != INVALID_PIPE) {
1740 			struct intel_crtc *wa_crtc =
1741 				intel_crtc_for_pipe(display, hsw_workaround_pipe);
1742 
1743 			intel_crtc_wait_for_next_vblank(wa_crtc);
1744 			intel_crtc_wait_for_next_vblank(wa_crtc);
1745 		}
1746 	}
1747 }
1748 
1749 static void ilk_crtc_disable(struct intel_atomic_state *state,
1750 			     struct intel_crtc *crtc)
1751 {
1752 	struct intel_display *display = to_intel_display(crtc);
1753 	const struct intel_crtc_state *old_crtc_state =
1754 		intel_atomic_get_old_crtc_state(state, crtc);
1755 	enum pipe pipe = crtc->pipe;
1756 
1757 	/*
1758 	 * Sometimes spurious CPU pipe underruns happen when the
1759 	 * pipe is already disabled, but FDI RX/TX is still enabled.
1760 	 * Happens at least with VGA+HDMI cloning. Suppress them.
1761 	 */
1762 	intel_set_cpu_fifo_underrun_reporting(display, pipe, false);
1763 	intel_set_pch_fifo_underrun_reporting(display, pipe, false);
1764 
1765 	intel_encoders_disable(state, crtc);
1766 
1767 	intel_crtc_vblank_off(old_crtc_state);
1768 
1769 	intel_disable_transcoder(old_crtc_state);
1770 
1771 	ilk_pfit_disable(old_crtc_state);
1772 
1773 	if (old_crtc_state->has_pch_encoder)
1774 		ilk_pch_disable(state, crtc);
1775 
1776 	intel_encoders_post_disable(state, crtc);
1777 
1778 	if (old_crtc_state->has_pch_encoder)
1779 		ilk_pch_post_disable(state, crtc);
1780 
1781 	intel_set_cpu_fifo_underrun_reporting(display, pipe, true);
1782 	intel_set_pch_fifo_underrun_reporting(display, pipe, true);
1783 }
1784 
1785 static void hsw_crtc_disable(struct intel_atomic_state *state,
1786 			     struct intel_crtc *crtc)
1787 {
1788 	struct intel_display *display = to_intel_display(state);
1789 	const struct intel_crtc_state *old_crtc_state =
1790 		intel_atomic_get_old_crtc_state(state, crtc);
1791 	struct intel_crtc *pipe_crtc;
1792 	int i;
1793 
1794 	/*
1795 	 * FIXME collapse everything to one hook.
1796 	 * Need care with mst->ddi interactions.
1797 	 */
1798 	intel_encoders_disable(state, crtc);
1799 	intel_encoders_post_disable(state, crtc);
1800 
1801 	intel_dpll_disable(old_crtc_state);
1802 
1803 	intel_encoders_post_pll_disable(state, crtc);
1804 
1805 	for_each_pipe_crtc_modeset_disable(display, pipe_crtc, old_crtc_state, i) {
1806 		const struct intel_crtc_state *old_pipe_crtc_state =
1807 			intel_atomic_get_old_crtc_state(state, pipe_crtc);
1808 
1809 		intel_dmc_disable_pipe(old_pipe_crtc_state);
1810 	}
1811 }
1812 
1813 /* Prefer intel_encoder_is_combo() */
1814 bool intel_phy_is_combo(struct intel_display *display, enum phy phy)
1815 {
1816 	if (phy == PHY_NONE)
1817 		return false;
1818 	else if (display->platform.alderlake_s)
1819 		return phy <= PHY_E;
1820 	else if (display->platform.dg1 || display->platform.rocketlake)
1821 		return phy <= PHY_D;
1822 	else if (display->platform.jasperlake || display->platform.elkhartlake)
1823 		return phy <= PHY_C;
1824 	else if (display->platform.alderlake_p || IS_DISPLAY_VER(display, 11, 12))
1825 		return phy <= PHY_B;
1826 	else
1827 		/*
1828 		 * DG2 outputs labelled as "combo PHY" in the bspec use
1829 		 * SNPS PHYs with completely different programming,
1830 		 * hence we always return false here.
1831 		 */
1832 		return false;
1833 }
1834 
1835 /*
1836  * This function returns true if the DDI port respective to the PHY enumeration
1837  * is a Type-C capable port.
1838  *
1839  * Depending on the VBT, the port might be configured
1840  * as a "dedicated external" port, meaning that actual physical PHY is outside
1841  * of the Type-C subsystem and, as such, not really a "Type-C PHY".
1842  *
1843  * Prefer intel_encoder_is_tc(), especially if you really need to know if we
1844  * are dealing with Type-C connections.
1845  */
1846 bool intel_phy_is_tc(struct intel_display *display, enum phy phy)
1847 {
1848 	/*
1849 	 * Discrete GPU phy's are not attached to FIA's to support TC
1850 	 * subsystem Legacy or non-legacy, and only support native DP/HDMI
1851 	 */
1852 	if (display->platform.dgfx)
1853 		return false;
1854 
1855 	if (DISPLAY_VER(display) >= 13)
1856 		return phy >= PHY_F && phy <= PHY_I;
1857 	else if (display->platform.tigerlake)
1858 		return phy >= PHY_D && phy <= PHY_I;
1859 	else if (display->platform.icelake)
1860 		return phy >= PHY_C && phy <= PHY_F;
1861 
1862 	return false;
1863 }
1864 
1865 /* Prefer intel_encoder_is_snps() */
1866 bool intel_phy_is_snps(struct intel_display *display, enum phy phy)
1867 {
1868 	/*
1869 	 * For DG2, and for DG2 only, all four "combo" ports and the TC1 port
1870 	 * (PHY E) use Synopsis PHYs. See intel_phy_is_tc().
1871 	 */
1872 	return display->platform.dg2 && phy > PHY_NONE && phy <= PHY_E;
1873 }
1874 
1875 /* Prefer intel_encoder_to_phy() */
1876 enum phy intel_port_to_phy(struct intel_display *display, enum port port)
1877 {
1878 	if (DISPLAY_VER(display) >= 13 && port >= PORT_D_XELPD)
1879 		return PHY_D + port - PORT_D_XELPD;
1880 	else if (DISPLAY_VER(display) >= 13 && port >= PORT_TC1)
1881 		return PHY_F + port - PORT_TC1;
1882 	else if (display->platform.alderlake_s && port >= PORT_TC1)
1883 		return PHY_B + port - PORT_TC1;
1884 	else if ((display->platform.dg1 || display->platform.rocketlake) && port >= PORT_TC1)
1885 		return PHY_C + port - PORT_TC1;
1886 	else if ((display->platform.jasperlake || display->platform.elkhartlake) &&
1887 		 port == PORT_D)
1888 		return PHY_A;
1889 
1890 	return PHY_A + port - PORT_A;
1891 }
1892 
1893 /* Prefer intel_encoder_to_tc() */
1894 /*
1895  * Return TC_PORT_1..I915_MAX_TC_PORTS for any TypeC DDI port. The function
1896  * can be also called for TypeC DDI ports not connected to a TypeC PHY such as
1897  * the PORT_TC1..4 ports on RKL/ADLS/BMG.
1898  */
1899 enum tc_port intel_port_to_tc(struct intel_display *display, enum port port)
1900 {
1901 	if (DISPLAY_VER(display) >= 12)
1902 		return TC_PORT_1 + port - PORT_TC1;
1903 	else
1904 		return TC_PORT_1 + port - PORT_C;
1905 }
1906 
1907 /*
1908  * Return TC_PORT_1..I915_MAX_TC_PORTS for TypeC DDI ports connected to a TypeC PHY.
1909  * Note that on RKL, ADLS, BMG the PORT_TC1..4 ports are connected to a non-TypeC
1910  * PHY, so on those platforms the function returns TC_PORT_NONE.
1911  */
1912 enum tc_port intel_tc_phy_port_to_tc(struct intel_display *display, enum port port)
1913 {
1914 	if (!intel_phy_is_tc(display, intel_port_to_phy(display, port)))
1915 		return TC_PORT_NONE;
1916 
1917 	return intel_port_to_tc(display, port);
1918 }
1919 
1920 enum phy intel_encoder_to_phy(struct intel_encoder *encoder)
1921 {
1922 	struct intel_display *display = to_intel_display(encoder);
1923 
1924 	return intel_port_to_phy(display, encoder->port);
1925 }
1926 
1927 bool intel_encoder_is_combo(struct intel_encoder *encoder)
1928 {
1929 	struct intel_display *display = to_intel_display(encoder);
1930 
1931 	return intel_phy_is_combo(display, intel_encoder_to_phy(encoder));
1932 }
1933 
1934 bool intel_encoder_is_snps(struct intel_encoder *encoder)
1935 {
1936 	struct intel_display *display = to_intel_display(encoder);
1937 
1938 	return intel_phy_is_snps(display, intel_encoder_to_phy(encoder));
1939 }
1940 
1941 bool intel_encoder_is_tc(struct intel_encoder *encoder)
1942 {
1943 	struct intel_display *display = to_intel_display(encoder);
1944 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
1945 
1946 	if (dig_port && dig_port->dedicated_external)
1947 		return false;
1948 
1949 	return intel_phy_is_tc(display, intel_encoder_to_phy(encoder));
1950 }
1951 
1952 enum tc_port intel_encoder_to_tc(struct intel_encoder *encoder)
1953 {
1954 	struct intel_display *display = to_intel_display(encoder);
1955 
1956 	return intel_tc_phy_port_to_tc(display, encoder->port);
1957 }
1958 
1959 enum intel_display_power_domain
1960 intel_aux_power_domain(struct intel_digital_port *dig_port)
1961 {
1962 	struct intel_display *display = to_intel_display(dig_port);
1963 
1964 	if (intel_tc_port_in_tbt_alt_mode(dig_port))
1965 		return intel_display_power_tbt_aux_domain(display, dig_port->aux_ch);
1966 
1967 	return intel_display_power_legacy_aux_domain(display, dig_port->aux_ch);
1968 }
1969 
1970 static void get_crtc_power_domains(struct intel_crtc_state *crtc_state,
1971 				   struct intel_power_domain_mask *mask)
1972 {
1973 	struct intel_display *display = to_intel_display(crtc_state);
1974 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1975 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1976 	struct drm_encoder *encoder;
1977 	enum pipe pipe = crtc->pipe;
1978 
1979 	bitmap_zero(mask->bits, POWER_DOMAIN_NUM);
1980 
1981 	if (!crtc_state->hw.active)
1982 		return;
1983 
1984 	set_bit(POWER_DOMAIN_PIPE(pipe), mask->bits);
1985 	set_bit(POWER_DOMAIN_TRANSCODER(cpu_transcoder), mask->bits);
1986 	if (crtc_state->pch_pfit.enabled ||
1987 	    crtc_state->pch_pfit.force_thru)
1988 		set_bit(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe), mask->bits);
1989 
1990 	drm_for_each_encoder_mask(encoder, display->drm,
1991 				  crtc_state->uapi.encoder_mask) {
1992 		struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
1993 
1994 		set_bit(intel_encoder->power_domain, mask->bits);
1995 	}
1996 
1997 	if (HAS_DDI(display) && crtc_state->has_audio)
1998 		set_bit(POWER_DOMAIN_AUDIO_MMIO, mask->bits);
1999 
2000 	if (crtc_state->intel_dpll)
2001 		set_bit(POWER_DOMAIN_DISPLAY_CORE, mask->bits);
2002 
2003 	if (crtc_state->dsc.compression_enable)
2004 		set_bit(intel_dsc_power_domain(crtc, cpu_transcoder), mask->bits);
2005 }
2006 
2007 void intel_modeset_get_crtc_power_domains(struct intel_crtc_state *crtc_state,
2008 					  struct intel_power_domain_mask *old_domains)
2009 {
2010 	struct intel_display *display = to_intel_display(crtc_state);
2011 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2012 	enum intel_display_power_domain domain;
2013 	struct intel_power_domain_mask domains, new_domains;
2014 
2015 	get_crtc_power_domains(crtc_state, &domains);
2016 
2017 	bitmap_andnot(new_domains.bits,
2018 		      domains.bits,
2019 		      crtc->enabled_power_domains.mask.bits,
2020 		      POWER_DOMAIN_NUM);
2021 	bitmap_andnot(old_domains->bits,
2022 		      crtc->enabled_power_domains.mask.bits,
2023 		      domains.bits,
2024 		      POWER_DOMAIN_NUM);
2025 
2026 	for_each_power_domain(domain, &new_domains)
2027 		intel_display_power_get_in_set(display,
2028 					       &crtc->enabled_power_domains,
2029 					       domain);
2030 }
2031 
2032 void intel_modeset_put_crtc_power_domains(struct intel_crtc *crtc,
2033 					  struct intel_power_domain_mask *domains)
2034 {
2035 	struct intel_display *display = to_intel_display(crtc);
2036 
2037 	intel_display_power_put_mask_in_set(display,
2038 					    &crtc->enabled_power_domains,
2039 					    domains);
2040 }
2041 
2042 static void i9xx_configure_cpu_transcoder(const struct intel_crtc_state *crtc_state)
2043 {
2044 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2045 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2046 
2047 	if (intel_crtc_has_dp_encoder(crtc_state)) {
2048 		intel_cpu_transcoder_set_m1_n1(crtc, cpu_transcoder,
2049 					       &crtc_state->dp_m_n);
2050 		intel_cpu_transcoder_set_m2_n2(crtc, cpu_transcoder,
2051 					       &crtc_state->dp_m2_n2);
2052 	}
2053 
2054 	intel_set_transcoder_timings(crtc_state);
2055 
2056 	i9xx_set_pipeconf(crtc_state);
2057 }
2058 
2059 static void valleyview_crtc_enable(struct intel_atomic_state *state,
2060 				   struct intel_crtc *crtc)
2061 {
2062 	struct intel_display *display = to_intel_display(crtc);
2063 	const struct intel_crtc_state *new_crtc_state =
2064 		intel_atomic_get_new_crtc_state(state, crtc);
2065 	enum pipe pipe = crtc->pipe;
2066 
2067 	if (drm_WARN_ON(display->drm, crtc->active))
2068 		return;
2069 
2070 	i9xx_configure_cpu_transcoder(new_crtc_state);
2071 
2072 	intel_set_pipe_src_size(new_crtc_state);
2073 
2074 	intel_de_write(display, VLV_PIPE_MSA_MISC(display, pipe), 0);
2075 
2076 	if (display->platform.cherryview && pipe == PIPE_B) {
2077 		intel_de_write(display, CHV_BLEND(display, pipe),
2078 			       CHV_BLEND_LEGACY);
2079 		intel_de_write(display, CHV_CANVAS(display, pipe), 0);
2080 	}
2081 
2082 	crtc->active = true;
2083 
2084 	intel_set_cpu_fifo_underrun_reporting(display, pipe, true);
2085 
2086 	intel_encoders_pre_pll_enable(state, crtc);
2087 
2088 	if (display->platform.cherryview)
2089 		chv_enable_pll(new_crtc_state);
2090 	else
2091 		vlv_enable_pll(new_crtc_state);
2092 
2093 	intel_encoders_pre_enable(state, crtc);
2094 
2095 	i9xx_pfit_enable(new_crtc_state);
2096 
2097 	intel_color_modeset(new_crtc_state);
2098 
2099 	intel_initial_watermarks(state, crtc);
2100 	intel_enable_transcoder(new_crtc_state);
2101 
2102 	intel_crtc_vblank_on(new_crtc_state);
2103 
2104 	intel_encoders_enable(state, crtc);
2105 }
2106 
2107 static void i9xx_crtc_enable(struct intel_atomic_state *state,
2108 			     struct intel_crtc *crtc)
2109 {
2110 	struct intel_display *display = to_intel_display(crtc);
2111 	const struct intel_crtc_state *new_crtc_state =
2112 		intel_atomic_get_new_crtc_state(state, crtc);
2113 	enum pipe pipe = crtc->pipe;
2114 
2115 	if (drm_WARN_ON(display->drm, crtc->active))
2116 		return;
2117 
2118 	i9xx_configure_cpu_transcoder(new_crtc_state);
2119 
2120 	intel_set_pipe_src_size(new_crtc_state);
2121 
2122 	crtc->active = true;
2123 
2124 	if (DISPLAY_VER(display) != 2)
2125 		intel_set_cpu_fifo_underrun_reporting(display, pipe, true);
2126 
2127 	intel_encoders_pre_enable(state, crtc);
2128 
2129 	i9xx_enable_pll(new_crtc_state);
2130 
2131 	i9xx_pfit_enable(new_crtc_state);
2132 
2133 	intel_color_modeset(new_crtc_state);
2134 
2135 	if (!intel_initial_watermarks(state, crtc))
2136 		intel_update_watermarks(display);
2137 	intel_enable_transcoder(new_crtc_state);
2138 
2139 	intel_crtc_vblank_on(new_crtc_state);
2140 
2141 	intel_encoders_enable(state, crtc);
2142 
2143 	/* prevents spurious underruns */
2144 	if (DISPLAY_VER(display) == 2)
2145 		intel_crtc_wait_for_next_vblank(crtc);
2146 }
2147 
2148 static void i9xx_crtc_disable(struct intel_atomic_state *state,
2149 			      struct intel_crtc *crtc)
2150 {
2151 	struct intel_display *display = to_intel_display(state);
2152 	struct intel_crtc_state *old_crtc_state =
2153 		intel_atomic_get_old_crtc_state(state, crtc);
2154 	enum pipe pipe = crtc->pipe;
2155 
2156 	/*
2157 	 * On gen2 planes are double buffered but the pipe isn't, so we must
2158 	 * wait for planes to fully turn off before disabling the pipe.
2159 	 */
2160 	if (DISPLAY_VER(display) == 2)
2161 		intel_crtc_wait_for_next_vblank(crtc);
2162 
2163 	intel_encoders_disable(state, crtc);
2164 
2165 	intel_crtc_vblank_off(old_crtc_state);
2166 
2167 	intel_disable_transcoder(old_crtc_state);
2168 
2169 	i9xx_pfit_disable(old_crtc_state);
2170 
2171 	intel_encoders_post_disable(state, crtc);
2172 
2173 	if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DSI)) {
2174 		if (display->platform.cherryview)
2175 			chv_disable_pll(display, pipe);
2176 		else if (display->platform.valleyview)
2177 			vlv_disable_pll(display, pipe);
2178 		else
2179 			i9xx_disable_pll(old_crtc_state);
2180 	}
2181 
2182 	intel_encoders_post_pll_disable(state, crtc);
2183 
2184 	if (DISPLAY_VER(display) != 2)
2185 		intel_set_cpu_fifo_underrun_reporting(display, pipe, false);
2186 
2187 	if (!display->funcs.wm->initial_watermarks)
2188 		intel_update_watermarks(display);
2189 
2190 	/* clock the pipe down to 640x480@60 to potentially save power */
2191 	if (display->platform.i830)
2192 		i830_enable_pipe(display, pipe);
2193 }
2194 
2195 void intel_encoder_destroy(struct drm_encoder *encoder)
2196 {
2197 	struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
2198 
2199 	drm_encoder_cleanup(encoder);
2200 	kfree(intel_encoder);
2201 }
2202 
2203 static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
2204 {
2205 	struct intel_display *display = to_intel_display(crtc);
2206 
2207 	/* GDG double wide on either pipe, otherwise pipe A only */
2208 	return HAS_DOUBLE_WIDE(display) &&
2209 		(crtc->pipe == PIPE_A || display->platform.i915g);
2210 }
2211 
2212 static u32 ilk_pipe_pixel_rate(const struct intel_crtc_state *crtc_state)
2213 {
2214 	u32 pixel_rate = crtc_state->hw.pipe_mode.crtc_clock;
2215 	struct drm_rect src;
2216 
2217 	/*
2218 	 * We only use IF-ID interlacing. If we ever use
2219 	 * PF-ID we'll need to adjust the pixel_rate here.
2220 	 */
2221 
2222 	if (!crtc_state->pch_pfit.enabled)
2223 		return pixel_rate;
2224 
2225 	drm_rect_init(&src, 0, 0,
2226 		      drm_rect_width(&crtc_state->pipe_src) << 16,
2227 		      drm_rect_height(&crtc_state->pipe_src) << 16);
2228 
2229 	return intel_adjusted_rate(&src, &crtc_state->pch_pfit.dst,
2230 				   pixel_rate);
2231 }
2232 
2233 static void intel_mode_from_crtc_timings(struct drm_display_mode *mode,
2234 					 const struct drm_display_mode *timings)
2235 {
2236 	mode->hdisplay = timings->crtc_hdisplay;
2237 	mode->htotal = timings->crtc_htotal;
2238 	mode->hsync_start = timings->crtc_hsync_start;
2239 	mode->hsync_end = timings->crtc_hsync_end;
2240 
2241 	mode->vdisplay = timings->crtc_vdisplay;
2242 	mode->vtotal = timings->crtc_vtotal;
2243 	mode->vsync_start = timings->crtc_vsync_start;
2244 	mode->vsync_end = timings->crtc_vsync_end;
2245 
2246 	mode->flags = timings->flags;
2247 	mode->type = DRM_MODE_TYPE_DRIVER;
2248 
2249 	mode->clock = timings->crtc_clock;
2250 
2251 	drm_mode_set_name(mode);
2252 }
2253 
2254 static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
2255 {
2256 	struct intel_display *display = to_intel_display(crtc_state);
2257 
2258 	if (HAS_GMCH(display))
2259 		/* FIXME calculate proper pipe pixel rate for GMCH pfit */
2260 		crtc_state->pixel_rate =
2261 			crtc_state->hw.pipe_mode.crtc_clock;
2262 	else
2263 		crtc_state->pixel_rate =
2264 			ilk_pipe_pixel_rate(crtc_state);
2265 }
2266 
2267 static void intel_joiner_adjust_timings(const struct intel_crtc_state *crtc_state,
2268 					struct drm_display_mode *mode)
2269 {
2270 	int num_pipes = intel_crtc_num_joined_pipes(crtc_state);
2271 
2272 	if (num_pipes == 1)
2273 		return;
2274 
2275 	mode->crtc_clock /= num_pipes;
2276 	mode->crtc_hdisplay /= num_pipes;
2277 	mode->crtc_hblank_start /= num_pipes;
2278 	mode->crtc_hblank_end /= num_pipes;
2279 	mode->crtc_hsync_start /= num_pipes;
2280 	mode->crtc_hsync_end /= num_pipes;
2281 	mode->crtc_htotal /= num_pipes;
2282 }
2283 
2284 static void intel_splitter_adjust_timings(const struct intel_crtc_state *crtc_state,
2285 					  struct drm_display_mode *mode)
2286 {
2287 	int overlap = crtc_state->splitter.pixel_overlap;
2288 	int n = crtc_state->splitter.link_count;
2289 
2290 	if (!crtc_state->splitter.enable)
2291 		return;
2292 
2293 	/*
2294 	 * eDP MSO uses segment timings from EDID for transcoder
2295 	 * timings, but full mode for everything else.
2296 	 *
2297 	 * h_full = (h_segment - pixel_overlap) * link_count
2298 	 */
2299 	mode->crtc_hdisplay = (mode->crtc_hdisplay - overlap) * n;
2300 	mode->crtc_hblank_start = (mode->crtc_hblank_start - overlap) * n;
2301 	mode->crtc_hblank_end = (mode->crtc_hblank_end - overlap) * n;
2302 	mode->crtc_hsync_start = (mode->crtc_hsync_start - overlap) * n;
2303 	mode->crtc_hsync_end = (mode->crtc_hsync_end - overlap) * n;
2304 	mode->crtc_htotal = (mode->crtc_htotal - overlap) * n;
2305 	mode->crtc_clock *= n;
2306 }
2307 
2308 static void intel_crtc_readout_derived_state(struct intel_crtc_state *crtc_state)
2309 {
2310 	struct drm_display_mode *mode = &crtc_state->hw.mode;
2311 	struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
2312 	struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
2313 
2314 	/*
2315 	 * Start with the adjusted_mode crtc timings, which
2316 	 * have been filled with the transcoder timings.
2317 	 */
2318 	drm_mode_copy(pipe_mode, adjusted_mode);
2319 
2320 	/* Expand MSO per-segment transcoder timings to full */
2321 	intel_splitter_adjust_timings(crtc_state, pipe_mode);
2322 
2323 	/*
2324 	 * We want the full numbers in adjusted_mode normal timings,
2325 	 * adjusted_mode crtc timings are left with the raw transcoder
2326 	 * timings.
2327 	 */
2328 	intel_mode_from_crtc_timings(adjusted_mode, pipe_mode);
2329 
2330 	/* Populate the "user" mode with full numbers */
2331 	drm_mode_copy(mode, pipe_mode);
2332 	intel_mode_from_crtc_timings(mode, mode);
2333 	mode->hdisplay = drm_rect_width(&crtc_state->pipe_src) *
2334 		intel_crtc_num_joined_pipes(crtc_state);
2335 	mode->vdisplay = drm_rect_height(&crtc_state->pipe_src);
2336 
2337 	/* Derive per-pipe timings in case joiner is used */
2338 	intel_joiner_adjust_timings(crtc_state, pipe_mode);
2339 	intel_mode_from_crtc_timings(pipe_mode, pipe_mode);
2340 
2341 	intel_crtc_compute_pixel_rate(crtc_state);
2342 }
2343 
2344 void intel_encoder_get_config(struct intel_encoder *encoder,
2345 			      struct intel_crtc_state *crtc_state)
2346 {
2347 	encoder->get_config(encoder, crtc_state);
2348 
2349 	intel_crtc_readout_derived_state(crtc_state);
2350 }
2351 
2352 static void intel_joiner_compute_pipe_src(struct intel_crtc_state *crtc_state)
2353 {
2354 	int num_pipes = intel_crtc_num_joined_pipes(crtc_state);
2355 	int width, height;
2356 
2357 	if (num_pipes == 1)
2358 		return;
2359 
2360 	width = drm_rect_width(&crtc_state->pipe_src);
2361 	height = drm_rect_height(&crtc_state->pipe_src);
2362 
2363 	drm_rect_init(&crtc_state->pipe_src, 0, 0,
2364 		      width / num_pipes, height);
2365 }
2366 
2367 static int intel_crtc_compute_pipe_src(struct intel_crtc_state *crtc_state)
2368 {
2369 	struct intel_display *display = to_intel_display(crtc_state);
2370 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2371 
2372 	intel_joiner_compute_pipe_src(crtc_state);
2373 
2374 	/*
2375 	 * Pipe horizontal size must be even in:
2376 	 * - DVO ganged mode
2377 	 * - LVDS dual channel mode
2378 	 * - Double wide pipe
2379 	 */
2380 	if (drm_rect_width(&crtc_state->pipe_src) & 1) {
2381 		if (crtc_state->double_wide) {
2382 			drm_dbg_kms(display->drm,
2383 				    "[CRTC:%d:%s] Odd pipe source width not supported with double wide pipe\n",
2384 				    crtc->base.base.id, crtc->base.name);
2385 			return -EINVAL;
2386 		}
2387 
2388 		if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
2389 		    intel_is_dual_link_lvds(display)) {
2390 			drm_dbg_kms(display->drm,
2391 				    "[CRTC:%d:%s] Odd pipe source width not supported with dual link LVDS\n",
2392 				    crtc->base.base.id, crtc->base.name);
2393 			return -EINVAL;
2394 		}
2395 	}
2396 
2397 	return 0;
2398 }
2399 
2400 static int intel_crtc_compute_pipe_mode(struct intel_crtc_state *crtc_state)
2401 {
2402 	struct intel_display *display = to_intel_display(crtc_state);
2403 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2404 	struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
2405 	struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
2406 	int clock_limit = display->cdclk.max_dotclk_freq;
2407 
2408 	/*
2409 	 * Start with the adjusted_mode crtc timings, which
2410 	 * have been filled with the transcoder timings.
2411 	 */
2412 	drm_mode_copy(pipe_mode, adjusted_mode);
2413 
2414 	/* Expand MSO per-segment transcoder timings to full */
2415 	intel_splitter_adjust_timings(crtc_state, pipe_mode);
2416 
2417 	/* Derive per-pipe timings in case joiner is used */
2418 	intel_joiner_adjust_timings(crtc_state, pipe_mode);
2419 	intel_mode_from_crtc_timings(pipe_mode, pipe_mode);
2420 
2421 	if (DISPLAY_VER(display) < 4) {
2422 		clock_limit = display->cdclk.max_cdclk_freq * 9 / 10;
2423 
2424 		/*
2425 		 * Enable double wide mode when the dot clock
2426 		 * is > 90% of the (display) core speed.
2427 		 */
2428 		if (intel_crtc_supports_double_wide(crtc) &&
2429 		    pipe_mode->crtc_clock > clock_limit) {
2430 			clock_limit = display->cdclk.max_dotclk_freq;
2431 			crtc_state->double_wide = true;
2432 		}
2433 	}
2434 
2435 	if (pipe_mode->crtc_clock > clock_limit) {
2436 		drm_dbg_kms(display->drm,
2437 			    "[CRTC:%d:%s] requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
2438 			    crtc->base.base.id, crtc->base.name,
2439 			    pipe_mode->crtc_clock, clock_limit,
2440 			    str_yes_no(crtc_state->double_wide));
2441 		return -EINVAL;
2442 	}
2443 
2444 	return 0;
2445 }
2446 
2447 static int intel_crtc_set_context_latency(struct intel_crtc_state *crtc_state)
2448 {
2449 	struct intel_display *display = to_intel_display(crtc_state);
2450 	int set_context_latency = 0;
2451 
2452 	if (!HAS_DSB(display))
2453 		return 0;
2454 
2455 	set_context_latency = max(set_context_latency,
2456 				  intel_psr_min_set_context_latency(crtc_state));
2457 
2458 	return set_context_latency;
2459 }
2460 
2461 static int intel_crtc_compute_set_context_latency(struct intel_atomic_state *state,
2462 						  struct intel_crtc *crtc)
2463 {
2464 	struct intel_display *display = to_intel_display(state);
2465 	struct intel_crtc_state *crtc_state =
2466 		intel_atomic_get_new_crtc_state(state, crtc);
2467 	struct drm_display_mode *adjusted_mode =
2468 		&crtc_state->hw.adjusted_mode;
2469 	int set_context_latency, max_vblank_delay;
2470 
2471 	set_context_latency = intel_crtc_set_context_latency(crtc_state);
2472 
2473 	max_vblank_delay = adjusted_mode->crtc_vblank_end - adjusted_mode->crtc_vblank_start - 1;
2474 
2475 	if (set_context_latency > max_vblank_delay) {
2476 		drm_dbg_kms(display->drm, "[CRTC:%d:%s] set context latency (%d) exceeds max (%d)\n",
2477 			    crtc->base.base.id, crtc->base.name,
2478 			    set_context_latency,
2479 			    max_vblank_delay);
2480 		return -EINVAL;
2481 	}
2482 
2483 	crtc_state->set_context_latency = set_context_latency;
2484 	adjusted_mode->crtc_vblank_start += set_context_latency;
2485 
2486 	return 0;
2487 }
2488 
2489 static int intel_crtc_compute_config(struct intel_atomic_state *state,
2490 				     struct intel_crtc *crtc)
2491 {
2492 	struct intel_crtc_state *crtc_state =
2493 		intel_atomic_get_new_crtc_state(state, crtc);
2494 	int ret;
2495 
2496 	ret = intel_dpll_crtc_compute_clock(state, crtc);
2497 	if (ret)
2498 		return ret;
2499 
2500 	ret = intel_crtc_compute_set_context_latency(state, crtc);
2501 	if (ret)
2502 		return ret;
2503 
2504 	ret = intel_crtc_compute_pipe_src(crtc_state);
2505 	if (ret)
2506 		return ret;
2507 
2508 	ret = intel_crtc_compute_pipe_mode(crtc_state);
2509 	if (ret)
2510 		return ret;
2511 
2512 	intel_crtc_compute_pixel_rate(crtc_state);
2513 
2514 	if (crtc_state->has_pch_encoder)
2515 		return ilk_fdi_compute_config(crtc, crtc_state);
2516 
2517 	intel_vrr_compute_guardband(crtc_state);
2518 
2519 	return 0;
2520 }
2521 
2522 static void
2523 intel_reduce_m_n_ratio(u32 *num, u32 *den)
2524 {
2525 	while (*num > DATA_LINK_M_N_MASK ||
2526 	       *den > DATA_LINK_M_N_MASK) {
2527 		*num >>= 1;
2528 		*den >>= 1;
2529 	}
2530 }
2531 
2532 static void compute_m_n(u32 *ret_m, u32 *ret_n,
2533 			u32 m, u32 n, u32 constant_n)
2534 {
2535 	if (constant_n)
2536 		*ret_n = constant_n;
2537 	else
2538 		*ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
2539 
2540 	*ret_m = div_u64(mul_u32_u32(m, *ret_n), n);
2541 	intel_reduce_m_n_ratio(ret_m, ret_n);
2542 }
2543 
2544 void
2545 intel_link_compute_m_n(u16 bits_per_pixel_x16, int nlanes,
2546 		       int pixel_clock, int link_clock,
2547 		       int bw_overhead,
2548 		       struct intel_link_m_n *m_n)
2549 {
2550 	u32 link_symbol_clock = intel_dp_link_symbol_clock(link_clock);
2551 	u32 data_m = intel_dp_effective_data_rate(pixel_clock, bits_per_pixel_x16,
2552 						  bw_overhead);
2553 	u32 data_n = drm_dp_max_dprx_data_rate(link_clock, nlanes);
2554 
2555 	/*
2556 	 * Windows/BIOS uses fixed M/N values always. Follow suit.
2557 	 *
2558 	 * Also several DP dongles in particular seem to be fussy
2559 	 * about too large link M/N values. Presumably the 20bit
2560 	 * value used by Windows/BIOS is acceptable to everyone.
2561 	 */
2562 	m_n->tu = 64;
2563 	compute_m_n(&m_n->data_m, &m_n->data_n,
2564 		    data_m, data_n,
2565 		    0x8000000);
2566 
2567 	compute_m_n(&m_n->link_m, &m_n->link_n,
2568 		    pixel_clock, link_symbol_clock,
2569 		    0x80000);
2570 }
2571 
2572 void intel_panel_sanitize_ssc(struct intel_display *display)
2573 {
2574 	/*
2575 	 * There may be no VBT; and if the BIOS enabled SSC we can
2576 	 * just keep using it to avoid unnecessary flicker.  Whereas if the
2577 	 * BIOS isn't using it, don't assume it will work even if the VBT
2578 	 * indicates as much.
2579 	 */
2580 	if (HAS_PCH_IBX(display) || HAS_PCH_CPT(display)) {
2581 		bool bios_lvds_use_ssc = intel_de_read(display,
2582 						       PCH_DREF_CONTROL) &
2583 			DREF_SSC1_ENABLE;
2584 
2585 		if (display->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
2586 			drm_dbg_kms(display->drm,
2587 				    "SSC %s by BIOS, overriding VBT which says %s\n",
2588 				    str_enabled_disabled(bios_lvds_use_ssc),
2589 				    str_enabled_disabled(display->vbt.lvds_use_ssc));
2590 			display->vbt.lvds_use_ssc = bios_lvds_use_ssc;
2591 		}
2592 	}
2593 }
2594 
2595 void intel_zero_m_n(struct intel_link_m_n *m_n)
2596 {
2597 	/* corresponds to 0 register value */
2598 	memset(m_n, 0, sizeof(*m_n));
2599 	m_n->tu = 1;
2600 }
2601 
2602 void intel_set_m_n(struct intel_display *display,
2603 		   const struct intel_link_m_n *m_n,
2604 		   i915_reg_t data_m_reg, i915_reg_t data_n_reg,
2605 		   i915_reg_t link_m_reg, i915_reg_t link_n_reg)
2606 {
2607 	intel_de_write(display, data_m_reg, TU_SIZE(m_n->tu) | m_n->data_m);
2608 	intel_de_write(display, data_n_reg, m_n->data_n);
2609 	intel_de_write(display, link_m_reg, m_n->link_m);
2610 	/*
2611 	 * On BDW+ writing LINK_N arms the double buffered update
2612 	 * of all the M/N registers, so it must be written last.
2613 	 */
2614 	intel_de_write(display, link_n_reg, m_n->link_n);
2615 }
2616 
2617 bool intel_cpu_transcoder_has_m2_n2(struct intel_display *display,
2618 				    enum transcoder transcoder)
2619 {
2620 	if (display->platform.haswell)
2621 		return transcoder == TRANSCODER_EDP;
2622 
2623 	return IS_DISPLAY_VER(display, 5, 7) || display->platform.cherryview;
2624 }
2625 
2626 void intel_cpu_transcoder_set_m1_n1(struct intel_crtc *crtc,
2627 				    enum transcoder transcoder,
2628 				    const struct intel_link_m_n *m_n)
2629 {
2630 	struct intel_display *display = to_intel_display(crtc);
2631 	enum pipe pipe = crtc->pipe;
2632 
2633 	if (DISPLAY_VER(display) >= 5)
2634 		intel_set_m_n(display, m_n,
2635 			      PIPE_DATA_M1(display, transcoder),
2636 			      PIPE_DATA_N1(display, transcoder),
2637 			      PIPE_LINK_M1(display, transcoder),
2638 			      PIPE_LINK_N1(display, transcoder));
2639 	else
2640 		intel_set_m_n(display, m_n,
2641 			      PIPE_DATA_M_G4X(pipe), PIPE_DATA_N_G4X(pipe),
2642 			      PIPE_LINK_M_G4X(pipe), PIPE_LINK_N_G4X(pipe));
2643 }
2644 
2645 void intel_cpu_transcoder_set_m2_n2(struct intel_crtc *crtc,
2646 				    enum transcoder transcoder,
2647 				    const struct intel_link_m_n *m_n)
2648 {
2649 	struct intel_display *display = to_intel_display(crtc);
2650 
2651 	if (!intel_cpu_transcoder_has_m2_n2(display, transcoder))
2652 		return;
2653 
2654 	intel_set_m_n(display, m_n,
2655 		      PIPE_DATA_M2(display, transcoder),
2656 		      PIPE_DATA_N2(display, transcoder),
2657 		      PIPE_LINK_M2(display, transcoder),
2658 		      PIPE_LINK_N2(display, transcoder));
2659 }
2660 
2661 static bool
2662 transcoder_has_vrr(const struct intel_crtc_state *crtc_state)
2663 {
2664 	struct intel_display *display = to_intel_display(crtc_state);
2665 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2666 
2667 	return HAS_VRR(display) && !transcoder_is_dsi(cpu_transcoder);
2668 }
2669 
2670 static void intel_set_transcoder_timings(const struct intel_crtc_state *crtc_state)
2671 {
2672 	struct intel_display *display = to_intel_display(crtc_state);
2673 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2674 	enum pipe pipe = crtc->pipe;
2675 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2676 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
2677 	u32 crtc_vdisplay, crtc_vtotal, crtc_vblank_start, crtc_vblank_end;
2678 	int vsyncshift = 0;
2679 
2680 	drm_WARN_ON(display->drm, transcoder_is_dsi(cpu_transcoder));
2681 
2682 	/* We need to be careful not to changed the adjusted mode, for otherwise
2683 	 * the hw state checker will get angry at the mismatch. */
2684 	crtc_vdisplay = adjusted_mode->crtc_vdisplay;
2685 	crtc_vtotal = adjusted_mode->crtc_vtotal;
2686 	crtc_vblank_start = adjusted_mode->crtc_vblank_start;
2687 	crtc_vblank_end = adjusted_mode->crtc_vblank_end;
2688 
2689 	if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
2690 		/* the chip adds 2 halflines automatically */
2691 		crtc_vtotal -= 1;
2692 		crtc_vblank_end -= 1;
2693 
2694 		if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
2695 			vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
2696 		else
2697 			vsyncshift = adjusted_mode->crtc_hsync_start -
2698 				adjusted_mode->crtc_htotal / 2;
2699 		if (vsyncshift < 0)
2700 			vsyncshift += adjusted_mode->crtc_htotal;
2701 	}
2702 
2703 	/*
2704 	 * VBLANK_START no longer works on ADL+, instead we must use
2705 	 * TRANS_SET_CONTEXT_LATENCY to configure the pipe vblank start.
2706 	 */
2707 	if (DISPLAY_VER(display) >= 13) {
2708 		intel_de_write(display,
2709 			       TRANS_SET_CONTEXT_LATENCY(display, cpu_transcoder),
2710 			       crtc_state->set_context_latency);
2711 
2712 		/*
2713 		 * VBLANK_START not used by hw, just clear it
2714 		 * to make it stand out in register dumps.
2715 		 */
2716 		crtc_vblank_start = 1;
2717 	} else if (DISPLAY_VER(display) == 12) {
2718 		/* VBLANK_START - VACTIVE defines SCL on TGL */
2719 		crtc_vblank_start = crtc_vdisplay + crtc_state->set_context_latency;
2720 	}
2721 
2722 	if (DISPLAY_VER(display) >= 4 && DISPLAY_VER(display) < 35)
2723 		intel_de_write(display,
2724 			       TRANS_VSYNCSHIFT(display, cpu_transcoder),
2725 			       vsyncshift);
2726 
2727 	intel_de_write(display, TRANS_HTOTAL(display, cpu_transcoder),
2728 		       HACTIVE(adjusted_mode->crtc_hdisplay - 1) |
2729 		       HTOTAL(adjusted_mode->crtc_htotal - 1));
2730 	intel_de_write(display, TRANS_HBLANK(display, cpu_transcoder),
2731 		       HBLANK_START(adjusted_mode->crtc_hblank_start - 1) |
2732 		       HBLANK_END(adjusted_mode->crtc_hblank_end - 1));
2733 	intel_de_write(display, TRANS_HSYNC(display, cpu_transcoder),
2734 		       HSYNC_START(adjusted_mode->crtc_hsync_start - 1) |
2735 		       HSYNC_END(adjusted_mode->crtc_hsync_end - 1));
2736 
2737 	/*
2738 	 * For platforms that always use VRR Timing Generator, the VTOTAL.Vtotal
2739 	 * bits are not required. Since the support for these bits is going to
2740 	 * be deprecated in upcoming platforms, avoid writing these bits for the
2741 	 * platforms that do not use legacy Timing Generator.
2742 	 */
2743 	if (intel_vrr_always_use_vrr_tg(display))
2744 		crtc_vtotal = 1;
2745 
2746 	intel_de_write(display, TRANS_VTOTAL(display, cpu_transcoder),
2747 		       VACTIVE(crtc_vdisplay - 1) |
2748 		       VTOTAL(crtc_vtotal - 1));
2749 	intel_de_write(display, TRANS_VBLANK(display, cpu_transcoder),
2750 		       VBLANK_START(crtc_vblank_start - 1) |
2751 		       VBLANK_END(crtc_vblank_end - 1));
2752 	intel_de_write(display, TRANS_VSYNC(display, cpu_transcoder),
2753 		       VSYNC_START(adjusted_mode->crtc_vsync_start - 1) |
2754 		       VSYNC_END(adjusted_mode->crtc_vsync_end - 1));
2755 
2756 	/* Workaround: when the EDP input selection is B, the VTOTAL_B must be
2757 	 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
2758 	 * documented on the DDI_FUNC_CTL register description, EDP Input Select
2759 	 * bits. */
2760 	if (display->platform.haswell && cpu_transcoder == TRANSCODER_EDP &&
2761 	    (pipe == PIPE_B || pipe == PIPE_C))
2762 		intel_de_write(display, TRANS_VTOTAL(display, pipe),
2763 			       VACTIVE(crtc_vdisplay - 1) |
2764 			       VTOTAL(crtc_vtotal - 1));
2765 
2766 	if (DISPLAY_VER(display) >= 30) {
2767 		/*
2768 		 * Address issues for resolutions with high refresh rate that
2769 		 * have small Hblank, specifically where Hblank is smaller than
2770 		 * one MTP. Simulations indicate this will address the
2771 		 * jitter issues that currently causes BS to be immediately
2772 		 * followed by BE which DPRX devices are unable to handle.
2773 		 * https://groups.vesa.org/wg/DP/document/20494
2774 		 */
2775 		intel_de_write(display, DP_MIN_HBLANK_CTL(cpu_transcoder),
2776 			       crtc_state->min_hblank);
2777 	}
2778 }
2779 
2780 static void intel_set_transcoder_timings_lrr(const struct intel_crtc_state *crtc_state)
2781 {
2782 	struct intel_display *display = to_intel_display(crtc_state);
2783 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2784 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
2785 	u32 crtc_vdisplay, crtc_vtotal, crtc_vblank_start, crtc_vblank_end;
2786 
2787 	drm_WARN_ON(display->drm, transcoder_is_dsi(cpu_transcoder));
2788 
2789 	crtc_vdisplay = adjusted_mode->crtc_vdisplay;
2790 	crtc_vtotal = adjusted_mode->crtc_vtotal;
2791 	crtc_vblank_start = adjusted_mode->crtc_vblank_start;
2792 	crtc_vblank_end = adjusted_mode->crtc_vblank_end;
2793 
2794 	if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
2795 		/* the chip adds 2 halflines automatically */
2796 		crtc_vtotal -= 1;
2797 		crtc_vblank_end -= 1;
2798 	}
2799 
2800 	if (DISPLAY_VER(display) >= 13) {
2801 		intel_de_write(display,
2802 			       TRANS_SET_CONTEXT_LATENCY(display, cpu_transcoder),
2803 			       crtc_state->set_context_latency);
2804 
2805 		/*
2806 		 * VBLANK_START not used by hw, just clear it
2807 		 * to make it stand out in register dumps.
2808 		 */
2809 		crtc_vblank_start = 1;
2810 	} else if (DISPLAY_VER(display) == 12) {
2811 		/* VBLANK_START - VACTIVE defines SCL on TGL */
2812 		crtc_vblank_start = crtc_vdisplay + crtc_state->set_context_latency;
2813 	}
2814 
2815 	/*
2816 	 * The hardware actually ignores TRANS_VBLANK.VBLANK_END in DP mode.
2817 	 * But let's write it anyway to keep the state checker happy.
2818 	 */
2819 	intel_de_write(display, TRANS_VBLANK(display, cpu_transcoder),
2820 		       VBLANK_START(crtc_vblank_start - 1) |
2821 		       VBLANK_END(crtc_vblank_end - 1));
2822 	/*
2823 	 * For platforms that always use VRR Timing Generator, the VTOTAL.Vtotal
2824 	 * bits are not required. Since the support for these bits is going to
2825 	 * be deprecated in upcoming platforms, avoid writing these bits for the
2826 	 * platforms that do not use legacy Timing Generator.
2827 	 */
2828 	if (intel_vrr_always_use_vrr_tg(display))
2829 		crtc_vtotal = 1;
2830 
2831 	/*
2832 	 * The double buffer latch point for TRANS_VTOTAL
2833 	 * is the transcoder's undelayed vblank.
2834 	 */
2835 	intel_de_write(display, TRANS_VTOTAL(display, cpu_transcoder),
2836 		       VACTIVE(crtc_vdisplay - 1) |
2837 		       VTOTAL(crtc_vtotal - 1));
2838 
2839 	intel_vrr_set_fixed_rr_timings(crtc_state);
2840 	intel_vrr_transcoder_enable(crtc_state);
2841 }
2842 
2843 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state)
2844 {
2845 	struct intel_display *display = to_intel_display(crtc_state);
2846 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2847 	int width = drm_rect_width(&crtc_state->pipe_src);
2848 	int height = drm_rect_height(&crtc_state->pipe_src);
2849 	enum pipe pipe = crtc->pipe;
2850 
2851 	/* pipesrc controls the size that is scaled from, which should
2852 	 * always be the user's requested size.
2853 	 */
2854 	intel_de_write(display, PIPESRC(display, pipe),
2855 		       PIPESRC_WIDTH(width - 1) | PIPESRC_HEIGHT(height - 1));
2856 }
2857 
2858 static bool intel_pipe_is_interlaced(const struct intel_crtc_state *crtc_state)
2859 {
2860 	struct intel_display *display = to_intel_display(crtc_state);
2861 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2862 
2863 	if (DISPLAY_VER(display) == 2 || DISPLAY_VER(display) >= 35)
2864 		return false;
2865 
2866 	if (DISPLAY_VER(display) >= 9 ||
2867 	    display->platform.broadwell || display->platform.haswell)
2868 		return intel_de_read(display,
2869 				     TRANSCONF(display, cpu_transcoder)) & TRANSCONF_INTERLACE_MASK_HSW;
2870 	else
2871 		return intel_de_read(display,
2872 				     TRANSCONF(display, cpu_transcoder)) & TRANSCONF_INTERLACE_MASK;
2873 }
2874 
2875 static void intel_get_transcoder_timings(struct intel_crtc *crtc,
2876 					 struct intel_crtc_state *pipe_config)
2877 {
2878 	struct intel_display *display = to_intel_display(crtc);
2879 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
2880 	struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
2881 	u32 tmp;
2882 
2883 	tmp = intel_de_read(display, TRANS_HTOTAL(display, cpu_transcoder));
2884 	adjusted_mode->crtc_hdisplay = REG_FIELD_GET(HACTIVE_MASK, tmp) + 1;
2885 	adjusted_mode->crtc_htotal = REG_FIELD_GET(HTOTAL_MASK, tmp) + 1;
2886 
2887 	if (!transcoder_is_dsi(cpu_transcoder)) {
2888 		tmp = intel_de_read(display,
2889 				    TRANS_HBLANK(display, cpu_transcoder));
2890 		adjusted_mode->crtc_hblank_start = REG_FIELD_GET(HBLANK_START_MASK, tmp) + 1;
2891 		adjusted_mode->crtc_hblank_end = REG_FIELD_GET(HBLANK_END_MASK, tmp) + 1;
2892 	}
2893 
2894 	tmp = intel_de_read(display, TRANS_HSYNC(display, cpu_transcoder));
2895 	adjusted_mode->crtc_hsync_start = REG_FIELD_GET(HSYNC_START_MASK, tmp) + 1;
2896 	adjusted_mode->crtc_hsync_end = REG_FIELD_GET(HSYNC_END_MASK, tmp) + 1;
2897 
2898 	tmp = intel_de_read(display, TRANS_VTOTAL(display, cpu_transcoder));
2899 	adjusted_mode->crtc_vdisplay = REG_FIELD_GET(VACTIVE_MASK, tmp) + 1;
2900 	adjusted_mode->crtc_vtotal = REG_FIELD_GET(VTOTAL_MASK, tmp) + 1;
2901 
2902 	/* FIXME TGL+ DSI transcoders have this! */
2903 	if (!transcoder_is_dsi(cpu_transcoder)) {
2904 		tmp = intel_de_read(display,
2905 				    TRANS_VBLANK(display, cpu_transcoder));
2906 		adjusted_mode->crtc_vblank_start = REG_FIELD_GET(VBLANK_START_MASK, tmp) + 1;
2907 		adjusted_mode->crtc_vblank_end = REG_FIELD_GET(VBLANK_END_MASK, tmp) + 1;
2908 	}
2909 	tmp = intel_de_read(display, TRANS_VSYNC(display, cpu_transcoder));
2910 	adjusted_mode->crtc_vsync_start = REG_FIELD_GET(VSYNC_START_MASK, tmp) + 1;
2911 	adjusted_mode->crtc_vsync_end = REG_FIELD_GET(VSYNC_END_MASK, tmp) + 1;
2912 
2913 	if (intel_pipe_is_interlaced(pipe_config)) {
2914 		adjusted_mode->flags |= DRM_MODE_FLAG_INTERLACE;
2915 		adjusted_mode->crtc_vtotal += 1;
2916 		adjusted_mode->crtc_vblank_end += 1;
2917 	}
2918 
2919 	if (DISPLAY_VER(display) >= 13 && !transcoder_is_dsi(cpu_transcoder)) {
2920 		pipe_config->set_context_latency =
2921 			intel_de_read(display,
2922 				      TRANS_SET_CONTEXT_LATENCY(display, cpu_transcoder));
2923 		adjusted_mode->crtc_vblank_start =
2924 			adjusted_mode->crtc_vdisplay +
2925 			pipe_config->set_context_latency;
2926 	} else if (DISPLAY_VER(display) == 12) {
2927 		/*
2928 		 * TGL doesn't have a dedicated register for SCL.
2929 		 * Instead, the hardware derives SCL from the difference between
2930 		 * TRANS_VBLANK.vblank_start and TRANS_VTOTAL.vactive.
2931 		 * To reflect the HW behaviour, readout the value for SCL as
2932 		 * Vblank start - Vactive.
2933 		 */
2934 		pipe_config->set_context_latency =
2935 			adjusted_mode->crtc_vblank_start - adjusted_mode->crtc_vdisplay;
2936 	}
2937 
2938 	if (DISPLAY_VER(display) >= 30)
2939 		pipe_config->min_hblank = intel_de_read(display,
2940 							DP_MIN_HBLANK_CTL(cpu_transcoder));
2941 }
2942 
2943 static void intel_joiner_adjust_pipe_src(struct intel_crtc_state *crtc_state)
2944 {
2945 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2946 	int num_pipes = intel_crtc_num_joined_pipes(crtc_state);
2947 	enum pipe primary_pipe, pipe = crtc->pipe;
2948 	int width;
2949 
2950 	if (num_pipes == 1)
2951 		return;
2952 
2953 	primary_pipe = joiner_primary_pipe(crtc_state);
2954 	width = drm_rect_width(&crtc_state->pipe_src);
2955 
2956 	drm_rect_translate_to(&crtc_state->pipe_src,
2957 			      (pipe - primary_pipe) * width, 0);
2958 }
2959 
2960 static void intel_get_pipe_src_size(struct intel_crtc *crtc,
2961 				    struct intel_crtc_state *pipe_config)
2962 {
2963 	struct intel_display *display = to_intel_display(crtc);
2964 	u32 tmp;
2965 
2966 	tmp = intel_de_read(display, PIPESRC(display, crtc->pipe));
2967 
2968 	drm_rect_init(&pipe_config->pipe_src, 0, 0,
2969 		      REG_FIELD_GET(PIPESRC_WIDTH_MASK, tmp) + 1,
2970 		      REG_FIELD_GET(PIPESRC_HEIGHT_MASK, tmp) + 1);
2971 
2972 	intel_joiner_adjust_pipe_src(pipe_config);
2973 }
2974 
2975 void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state)
2976 {
2977 	struct intel_display *display = to_intel_display(crtc_state);
2978 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2979 	u32 val = 0;
2980 
2981 	/*
2982 	 * - We keep both pipes enabled on 830
2983 	 * - During modeset the pipe is still disabled and must remain so
2984 	 * - During fastset the pipe is already enabled and must remain so
2985 	 */
2986 	if (display->platform.i830 || !intel_crtc_needs_modeset(crtc_state))
2987 		val |= TRANSCONF_ENABLE;
2988 
2989 	if (crtc_state->double_wide)
2990 		val |= TRANSCONF_DOUBLE_WIDE;
2991 
2992 	/* only g4x and later have fancy bpc/dither controls */
2993 	if (display->platform.g4x || display->platform.valleyview ||
2994 	    display->platform.cherryview) {
2995 		/* Bspec claims that we can't use dithering for 30bpp pipes. */
2996 		if (crtc_state->dither && crtc_state->pipe_bpp != 30)
2997 			val |= TRANSCONF_DITHER_EN |
2998 				TRANSCONF_DITHER_TYPE_SP;
2999 
3000 		switch (crtc_state->pipe_bpp) {
3001 		default:
3002 			/* Case prevented by intel_choose_pipe_bpp_dither. */
3003 			MISSING_CASE(crtc_state->pipe_bpp);
3004 			fallthrough;
3005 		case 18:
3006 			val |= TRANSCONF_BPC_6;
3007 			break;
3008 		case 24:
3009 			val |= TRANSCONF_BPC_8;
3010 			break;
3011 		case 30:
3012 			val |= TRANSCONF_BPC_10;
3013 			break;
3014 		}
3015 	}
3016 
3017 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
3018 		if (DISPLAY_VER(display) < 4 ||
3019 		    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
3020 			val |= TRANSCONF_INTERLACE_W_FIELD_INDICATION;
3021 		else
3022 			val |= TRANSCONF_INTERLACE_W_SYNC_SHIFT;
3023 	} else {
3024 		val |= TRANSCONF_INTERLACE_PROGRESSIVE;
3025 	}
3026 
3027 	if ((display->platform.valleyview || display->platform.cherryview) &&
3028 	    crtc_state->limited_color_range)
3029 		val |= TRANSCONF_COLOR_RANGE_SELECT;
3030 
3031 	val |= TRANSCONF_GAMMA_MODE(crtc_state->gamma_mode);
3032 
3033 	if (crtc_state->wgc_enable)
3034 		val |= TRANSCONF_WGC_ENABLE;
3035 
3036 	val |= TRANSCONF_FRAME_START_DELAY(crtc_state->framestart_delay - 1);
3037 
3038 	intel_de_write(display, TRANSCONF(display, cpu_transcoder), val);
3039 	intel_de_posting_read(display, TRANSCONF(display, cpu_transcoder));
3040 }
3041 
3042 static enum intel_output_format
3043 bdw_get_pipe_misc_output_format(struct intel_crtc *crtc)
3044 {
3045 	struct intel_display *display = to_intel_display(crtc);
3046 	u32 tmp;
3047 
3048 	tmp = intel_de_read(display, PIPE_MISC(crtc->pipe));
3049 
3050 	if (tmp & PIPE_MISC_YUV420_ENABLE) {
3051 		/*
3052 		 * We support 4:2:0 in full blend mode only.
3053 		 * For xe3_lpd+ this is implied in YUV420 Enable bit.
3054 		 * Ensure the same for prior platforms in YUV420 Mode bit.
3055 		 */
3056 		if (DISPLAY_VER(display) < 30)
3057 			drm_WARN_ON(display->drm,
3058 				    (tmp & PIPE_MISC_YUV420_MODE_FULL_BLEND) == 0);
3059 
3060 		return INTEL_OUTPUT_FORMAT_YCBCR420;
3061 	} else if (tmp & PIPE_MISC_OUTPUT_COLORSPACE_YUV) {
3062 		return INTEL_OUTPUT_FORMAT_YCBCR444;
3063 	} else {
3064 		return INTEL_OUTPUT_FORMAT_RGB;
3065 	}
3066 }
3067 
3068 static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
3069 				 struct intel_crtc_state *pipe_config)
3070 {
3071 	struct intel_display *display = to_intel_display(crtc);
3072 	enum intel_display_power_domain power_domain;
3073 	enum transcoder cpu_transcoder = (enum transcoder)crtc->pipe;
3074 	struct ref_tracker *wakeref;
3075 	bool ret = false;
3076 	u32 tmp;
3077 
3078 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
3079 	wakeref = intel_display_power_get_if_enabled(display, power_domain);
3080 	if (!wakeref)
3081 		return false;
3082 
3083 	tmp = intel_de_read(display, TRANSCONF(display, cpu_transcoder));
3084 	if (!(tmp & TRANSCONF_ENABLE))
3085 		goto out;
3086 
3087 	pipe_config->cpu_transcoder = cpu_transcoder;
3088 
3089 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
3090 	pipe_config->sink_format = pipe_config->output_format;
3091 
3092 	if (display->platform.g4x || display->platform.valleyview ||
3093 	    display->platform.cherryview) {
3094 		switch (tmp & TRANSCONF_BPC_MASK) {
3095 		case TRANSCONF_BPC_6:
3096 			pipe_config->pipe_bpp = 18;
3097 			break;
3098 		case TRANSCONF_BPC_8:
3099 			pipe_config->pipe_bpp = 24;
3100 			break;
3101 		case TRANSCONF_BPC_10:
3102 			pipe_config->pipe_bpp = 30;
3103 			break;
3104 		default:
3105 			MISSING_CASE(tmp);
3106 			break;
3107 		}
3108 	}
3109 
3110 	if ((display->platform.valleyview || display->platform.cherryview) &&
3111 	    (tmp & TRANSCONF_COLOR_RANGE_SELECT))
3112 		pipe_config->limited_color_range = true;
3113 
3114 	pipe_config->gamma_mode = REG_FIELD_GET(TRANSCONF_GAMMA_MODE_MASK_I9XX, tmp);
3115 
3116 	pipe_config->framestart_delay = REG_FIELD_GET(TRANSCONF_FRAME_START_DELAY_MASK, tmp) + 1;
3117 
3118 	if ((display->platform.valleyview || display->platform.cherryview) &&
3119 	    (tmp & TRANSCONF_WGC_ENABLE))
3120 		pipe_config->wgc_enable = true;
3121 
3122 	intel_color_get_config(pipe_config);
3123 
3124 	if (HAS_DOUBLE_WIDE(display))
3125 		pipe_config->double_wide = tmp & TRANSCONF_DOUBLE_WIDE;
3126 
3127 	intel_get_transcoder_timings(crtc, pipe_config);
3128 	intel_get_pipe_src_size(crtc, pipe_config);
3129 
3130 	i9xx_pfit_get_config(pipe_config);
3131 
3132 	i9xx_dpll_get_hw_state(crtc, &pipe_config->dpll_hw_state);
3133 
3134 	if (DISPLAY_VER(display) >= 4) {
3135 		tmp = pipe_config->dpll_hw_state.i9xx.dpll_md;
3136 		pipe_config->pixel_multiplier =
3137 			((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
3138 			 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
3139 	} else if (display->platform.i945g || display->platform.i945gm ||
3140 		   display->platform.g33 || display->platform.pineview) {
3141 		tmp = pipe_config->dpll_hw_state.i9xx.dpll;
3142 		pipe_config->pixel_multiplier =
3143 			((tmp & SDVO_MULTIPLIER_MASK)
3144 			 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
3145 	} else {
3146 		/* Note that on i915G/GM the pixel multiplier is in the sdvo
3147 		 * port and will be fixed up in the encoder->get_config
3148 		 * function. */
3149 		pipe_config->pixel_multiplier = 1;
3150 	}
3151 
3152 	if (display->platform.cherryview)
3153 		chv_crtc_clock_get(pipe_config);
3154 	else if (display->platform.valleyview)
3155 		vlv_crtc_clock_get(pipe_config);
3156 	else
3157 		i9xx_crtc_clock_get(pipe_config);
3158 
3159 	/*
3160 	 * Normally the dotclock is filled in by the encoder .get_config()
3161 	 * but in case the pipe is enabled w/o any ports we need a sane
3162 	 * default.
3163 	 */
3164 	pipe_config->hw.adjusted_mode.crtc_clock =
3165 		pipe_config->port_clock / pipe_config->pixel_multiplier;
3166 
3167 	ret = true;
3168 
3169 out:
3170 	intel_display_power_put(display, power_domain, wakeref);
3171 
3172 	return ret;
3173 }
3174 
3175 void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state)
3176 {
3177 	struct intel_display *display = to_intel_display(crtc_state);
3178 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
3179 	u32 val = 0;
3180 
3181 	/*
3182 	 * - During modeset the pipe is still disabled and must remain so
3183 	 * - During fastset the pipe is already enabled and must remain so
3184 	 */
3185 	if (!intel_crtc_needs_modeset(crtc_state))
3186 		val |= TRANSCONF_ENABLE;
3187 
3188 	switch (crtc_state->pipe_bpp) {
3189 	default:
3190 		/* Case prevented by intel_choose_pipe_bpp_dither. */
3191 		MISSING_CASE(crtc_state->pipe_bpp);
3192 		fallthrough;
3193 	case 18:
3194 		val |= TRANSCONF_BPC_6;
3195 		break;
3196 	case 24:
3197 		val |= TRANSCONF_BPC_8;
3198 		break;
3199 	case 30:
3200 		val |= TRANSCONF_BPC_10;
3201 		break;
3202 	case 36:
3203 		val |= TRANSCONF_BPC_12;
3204 		break;
3205 	}
3206 
3207 	if (crtc_state->dither)
3208 		val |= TRANSCONF_DITHER_EN | TRANSCONF_DITHER_TYPE_SP;
3209 
3210 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
3211 		val |= TRANSCONF_INTERLACE_IF_ID_ILK;
3212 	else
3213 		val |= TRANSCONF_INTERLACE_PF_PD_ILK;
3214 
3215 	/*
3216 	 * This would end up with an odd purple hue over
3217 	 * the entire display. Make sure we don't do it.
3218 	 */
3219 	drm_WARN_ON(display->drm, crtc_state->limited_color_range &&
3220 		    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
3221 
3222 	if (crtc_state->limited_color_range &&
3223 	    !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
3224 		val |= TRANSCONF_COLOR_RANGE_SELECT;
3225 
3226 	if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
3227 		val |= TRANSCONF_OUTPUT_COLORSPACE_YUV709;
3228 
3229 	val |= TRANSCONF_GAMMA_MODE(crtc_state->gamma_mode);
3230 
3231 	val |= TRANSCONF_FRAME_START_DELAY(crtc_state->framestart_delay - 1);
3232 	val |= TRANSCONF_MSA_TIMING_DELAY(crtc_state->msa_timing_delay);
3233 
3234 	intel_de_write(display, TRANSCONF(display, cpu_transcoder), val);
3235 	intel_de_posting_read(display, TRANSCONF(display, cpu_transcoder));
3236 }
3237 
3238 static void hsw_set_transconf(const struct intel_crtc_state *crtc_state)
3239 {
3240 	struct intel_display *display = to_intel_display(crtc_state);
3241 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
3242 	u32 val = 0;
3243 
3244 	/*
3245 	 * - During modeset the pipe is still disabled and must remain so
3246 	 * - During fastset the pipe is already enabled and must remain so
3247 	 */
3248 	if (!intel_crtc_needs_modeset(crtc_state))
3249 		val |= TRANSCONF_ENABLE;
3250 
3251 	if (display->platform.haswell && crtc_state->dither)
3252 		val |= TRANSCONF_DITHER_EN | TRANSCONF_DITHER_TYPE_SP;
3253 
3254 	if (DISPLAY_VER(display) < 35) {
3255 		if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
3256 			val |= TRANSCONF_INTERLACE_IF_ID_ILK;
3257 		else
3258 			val |= TRANSCONF_INTERLACE_PF_PD_ILK;
3259 	}
3260 
3261 	if (display->platform.haswell &&
3262 	    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
3263 		val |= TRANSCONF_OUTPUT_COLORSPACE_YUV_HSW;
3264 
3265 	intel_de_write(display, TRANSCONF(display, cpu_transcoder), val);
3266 	intel_de_posting_read(display, TRANSCONF(display, cpu_transcoder));
3267 }
3268 
3269 static void bdw_set_pipe_misc(struct intel_dsb *dsb,
3270 			      const struct intel_crtc_state *crtc_state)
3271 {
3272 	struct intel_display *display = to_intel_display(crtc_state);
3273 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3274 	u32 val = 0;
3275 
3276 	switch (crtc_state->pipe_bpp) {
3277 	case 18:
3278 		val |= PIPE_MISC_BPC_6;
3279 		break;
3280 	case 24:
3281 		val |= PIPE_MISC_BPC_8;
3282 		break;
3283 	case 30:
3284 		val |= PIPE_MISC_BPC_10;
3285 		break;
3286 	case 36:
3287 		/* Port output 12BPC defined for ADLP+ */
3288 		if (DISPLAY_VER(display) >= 13)
3289 			val |= PIPE_MISC_BPC_12_ADLP;
3290 		break;
3291 	default:
3292 		MISSING_CASE(crtc_state->pipe_bpp);
3293 		break;
3294 	}
3295 
3296 	if (crtc_state->dither)
3297 		val |= PIPE_MISC_DITHER_ENABLE | PIPE_MISC_DITHER_TYPE_SP;
3298 
3299 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
3300 	    crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
3301 		val |= PIPE_MISC_OUTPUT_COLORSPACE_YUV;
3302 
3303 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
3304 		val |= DISPLAY_VER(display) >= 30 ? PIPE_MISC_YUV420_ENABLE :
3305 			PIPE_MISC_YUV420_ENABLE | PIPE_MISC_YUV420_MODE_FULL_BLEND;
3306 
3307 	if (DISPLAY_VER(display) >= 11 && is_hdr_mode(crtc_state))
3308 		val |= PIPE_MISC_HDR_MODE_PRECISION;
3309 
3310 	if (DISPLAY_VER(display) >= 12)
3311 		val |= PIPE_MISC_PIXEL_ROUNDING_TRUNC;
3312 
3313 	/* allow PSR with sprite enabled */
3314 	if (display->platform.broadwell)
3315 		val |= PIPE_MISC_PSR_MASK_SPRITE_ENABLE;
3316 
3317 	intel_de_write_dsb(display, dsb, PIPE_MISC(crtc->pipe), val);
3318 }
3319 
3320 int bdw_get_pipe_misc_bpp(struct intel_crtc *crtc)
3321 {
3322 	struct intel_display *display = to_intel_display(crtc);
3323 	u32 tmp;
3324 
3325 	tmp = intel_de_read(display, PIPE_MISC(crtc->pipe));
3326 
3327 	switch (tmp & PIPE_MISC_BPC_MASK) {
3328 	case PIPE_MISC_BPC_6:
3329 		return 18;
3330 	case PIPE_MISC_BPC_8:
3331 		return 24;
3332 	case PIPE_MISC_BPC_10:
3333 		return 30;
3334 	/*
3335 	 * PORT OUTPUT 12 BPC defined for ADLP+.
3336 	 *
3337 	 * TODO:
3338 	 * For previous platforms with DSI interface, bits 5:7
3339 	 * are used for storing pipe_bpp irrespective of dithering.
3340 	 * Since the value of 12 BPC is not defined for these bits
3341 	 * on older platforms, need to find a workaround for 12 BPC
3342 	 * MIPI DSI HW readout.
3343 	 */
3344 	case PIPE_MISC_BPC_12_ADLP:
3345 		if (DISPLAY_VER(display) >= 13)
3346 			return 36;
3347 		fallthrough;
3348 	default:
3349 		MISSING_CASE(tmp);
3350 		return 0;
3351 	}
3352 }
3353 
3354 int ilk_get_lanes_required(int target_clock, int link_bw, int bpp)
3355 {
3356 	/*
3357 	 * Account for spread spectrum to avoid
3358 	 * oversubscribing the link. Max center spread
3359 	 * is 2.5%; use 5% for safety's sake.
3360 	 */
3361 	u32 bps = target_clock * bpp * 21 / 20;
3362 	return DIV_ROUND_UP(bps, link_bw * 8);
3363 }
3364 
3365 void intel_get_m_n(struct intel_display *display,
3366 		   struct intel_link_m_n *m_n,
3367 		   i915_reg_t data_m_reg, i915_reg_t data_n_reg,
3368 		   i915_reg_t link_m_reg, i915_reg_t link_n_reg)
3369 {
3370 	m_n->link_m = intel_de_read(display, link_m_reg) & DATA_LINK_M_N_MASK;
3371 	m_n->link_n = intel_de_read(display, link_n_reg) & DATA_LINK_M_N_MASK;
3372 	m_n->data_m = intel_de_read(display, data_m_reg) & DATA_LINK_M_N_MASK;
3373 	m_n->data_n = intel_de_read(display, data_n_reg) & DATA_LINK_M_N_MASK;
3374 	m_n->tu = REG_FIELD_GET(TU_SIZE_MASK, intel_de_read(display, data_m_reg)) + 1;
3375 }
3376 
3377 void intel_cpu_transcoder_get_m1_n1(struct intel_crtc *crtc,
3378 				    enum transcoder transcoder,
3379 				    struct intel_link_m_n *m_n)
3380 {
3381 	struct intel_display *display = to_intel_display(crtc);
3382 	enum pipe pipe = crtc->pipe;
3383 
3384 	if (DISPLAY_VER(display) >= 5)
3385 		intel_get_m_n(display, m_n,
3386 			      PIPE_DATA_M1(display, transcoder),
3387 			      PIPE_DATA_N1(display, transcoder),
3388 			      PIPE_LINK_M1(display, transcoder),
3389 			      PIPE_LINK_N1(display, transcoder));
3390 	else
3391 		intel_get_m_n(display, m_n,
3392 			      PIPE_DATA_M_G4X(pipe), PIPE_DATA_N_G4X(pipe),
3393 			      PIPE_LINK_M_G4X(pipe), PIPE_LINK_N_G4X(pipe));
3394 }
3395 
3396 void intel_cpu_transcoder_get_m2_n2(struct intel_crtc *crtc,
3397 				    enum transcoder transcoder,
3398 				    struct intel_link_m_n *m_n)
3399 {
3400 	struct intel_display *display = to_intel_display(crtc);
3401 
3402 	if (!intel_cpu_transcoder_has_m2_n2(display, transcoder))
3403 		return;
3404 
3405 	intel_get_m_n(display, m_n,
3406 		      PIPE_DATA_M2(display, transcoder),
3407 		      PIPE_DATA_N2(display, transcoder),
3408 		      PIPE_LINK_M2(display, transcoder),
3409 		      PIPE_LINK_N2(display, transcoder));
3410 }
3411 
3412 static bool ilk_get_pipe_config(struct intel_crtc *crtc,
3413 				struct intel_crtc_state *pipe_config)
3414 {
3415 	struct intel_display *display = to_intel_display(crtc);
3416 	enum intel_display_power_domain power_domain;
3417 	enum transcoder cpu_transcoder = (enum transcoder)crtc->pipe;
3418 	struct ref_tracker *wakeref;
3419 	bool ret = false;
3420 	u32 tmp;
3421 
3422 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
3423 	wakeref = intel_display_power_get_if_enabled(display, power_domain);
3424 	if (!wakeref)
3425 		return false;
3426 
3427 	tmp = intel_de_read(display, TRANSCONF(display, cpu_transcoder));
3428 	if (!(tmp & TRANSCONF_ENABLE))
3429 		goto out;
3430 
3431 	pipe_config->cpu_transcoder = cpu_transcoder;
3432 
3433 	switch (tmp & TRANSCONF_BPC_MASK) {
3434 	case TRANSCONF_BPC_6:
3435 		pipe_config->pipe_bpp = 18;
3436 		break;
3437 	case TRANSCONF_BPC_8:
3438 		pipe_config->pipe_bpp = 24;
3439 		break;
3440 	case TRANSCONF_BPC_10:
3441 		pipe_config->pipe_bpp = 30;
3442 		break;
3443 	case TRANSCONF_BPC_12:
3444 		pipe_config->pipe_bpp = 36;
3445 		break;
3446 	default:
3447 		break;
3448 	}
3449 
3450 	if (tmp & TRANSCONF_COLOR_RANGE_SELECT)
3451 		pipe_config->limited_color_range = true;
3452 
3453 	switch (tmp & TRANSCONF_OUTPUT_COLORSPACE_MASK) {
3454 	case TRANSCONF_OUTPUT_COLORSPACE_YUV601:
3455 	case TRANSCONF_OUTPUT_COLORSPACE_YUV709:
3456 		pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
3457 		break;
3458 	default:
3459 		pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
3460 		break;
3461 	}
3462 
3463 	pipe_config->sink_format = pipe_config->output_format;
3464 
3465 	pipe_config->gamma_mode = REG_FIELD_GET(TRANSCONF_GAMMA_MODE_MASK_ILK, tmp);
3466 
3467 	pipe_config->framestart_delay = REG_FIELD_GET(TRANSCONF_FRAME_START_DELAY_MASK, tmp) + 1;
3468 
3469 	pipe_config->msa_timing_delay = REG_FIELD_GET(TRANSCONF_MSA_TIMING_DELAY_MASK, tmp);
3470 
3471 	intel_color_get_config(pipe_config);
3472 
3473 	pipe_config->pixel_multiplier = 1;
3474 
3475 	ilk_pch_get_config(pipe_config);
3476 
3477 	intel_get_transcoder_timings(crtc, pipe_config);
3478 	intel_get_pipe_src_size(crtc, pipe_config);
3479 
3480 	ilk_pfit_get_config(pipe_config);
3481 
3482 	ret = true;
3483 
3484 out:
3485 	intel_display_power_put(display, power_domain, wakeref);
3486 
3487 	return ret;
3488 }
3489 
3490 static u8 joiner_pipes(struct intel_display *display)
3491 {
3492 	u8 pipes;
3493 
3494 	if (DISPLAY_VER(display) >= 12)
3495 		pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D);
3496 	else if (DISPLAY_VER(display) >= 11)
3497 		pipes = BIT(PIPE_B) | BIT(PIPE_C);
3498 	else
3499 		pipes = 0;
3500 
3501 	return pipes & DISPLAY_RUNTIME_INFO(display)->pipe_mask;
3502 }
3503 
3504 static bool transcoder_ddi_func_is_enabled(struct intel_display *display,
3505 					   enum transcoder cpu_transcoder)
3506 {
3507 	enum intel_display_power_domain power_domain;
3508 	u32 tmp = 0;
3509 
3510 	power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
3511 
3512 	with_intel_display_power_if_enabled(display, power_domain)
3513 		tmp = intel_de_read(display,
3514 				    TRANS_DDI_FUNC_CTL(display, cpu_transcoder));
3515 
3516 	return tmp & TRANS_DDI_FUNC_ENABLE;
3517 }
3518 
3519 static void enabled_uncompressed_joiner_pipes(struct intel_display *display,
3520 					      u8 *primary_pipes, u8 *secondary_pipes)
3521 {
3522 	struct intel_crtc *crtc;
3523 
3524 	*primary_pipes = 0;
3525 	*secondary_pipes = 0;
3526 
3527 	if (!HAS_UNCOMPRESSED_JOINER(display))
3528 		return;
3529 
3530 	for_each_intel_crtc_in_pipe_mask(display->drm, crtc,
3531 					 joiner_pipes(display)) {
3532 		enum intel_display_power_domain power_domain;
3533 		enum pipe pipe = crtc->pipe;
3534 
3535 		power_domain = POWER_DOMAIN_PIPE(pipe);
3536 		with_intel_display_power_if_enabled(display, power_domain) {
3537 			u32 tmp = intel_de_read(display, ICL_PIPE_DSS_CTL1(pipe));
3538 
3539 			if (tmp & UNCOMPRESSED_JOINER_PRIMARY)
3540 				*primary_pipes |= BIT(pipe);
3541 			if (tmp & UNCOMPRESSED_JOINER_SECONDARY)
3542 				*secondary_pipes |= BIT(pipe);
3543 		}
3544 	}
3545 }
3546 
3547 static void enabled_bigjoiner_pipes(struct intel_display *display,
3548 				    u8 *primary_pipes, u8 *secondary_pipes)
3549 {
3550 	struct intel_crtc *crtc;
3551 
3552 	*primary_pipes = 0;
3553 	*secondary_pipes = 0;
3554 
3555 	if (!HAS_BIGJOINER(display))
3556 		return;
3557 
3558 	for_each_intel_crtc_in_pipe_mask(display->drm, crtc,
3559 					 joiner_pipes(display)) {
3560 		enum intel_display_power_domain power_domain;
3561 		enum pipe pipe = crtc->pipe;
3562 
3563 		power_domain = intel_dsc_power_domain(crtc, (enum transcoder)pipe);
3564 		with_intel_display_power_if_enabled(display, power_domain) {
3565 			u32 tmp = intel_de_read(display, ICL_PIPE_DSS_CTL1(pipe));
3566 
3567 			if (!(tmp & BIG_JOINER_ENABLE))
3568 				continue;
3569 
3570 			if (tmp & PRIMARY_BIG_JOINER_ENABLE)
3571 				*primary_pipes |= BIT(pipe);
3572 			else
3573 				*secondary_pipes |= BIT(pipe);
3574 		}
3575 	}
3576 }
3577 
3578 static u8 expected_secondary_pipes(u8 primary_pipes, int num_pipes)
3579 {
3580 	u8 secondary_pipes = 0;
3581 
3582 	for (int i = 1; i < num_pipes; i++)
3583 		secondary_pipes |= primary_pipes << i;
3584 
3585 	return secondary_pipes;
3586 }
3587 
3588 static u8 expected_uncompressed_joiner_secondary_pipes(u8 uncompjoiner_primary_pipes)
3589 {
3590 	return expected_secondary_pipes(uncompjoiner_primary_pipes, 2);
3591 }
3592 
3593 static u8 expected_bigjoiner_secondary_pipes(u8 bigjoiner_primary_pipes)
3594 {
3595 	return expected_secondary_pipes(bigjoiner_primary_pipes, 2);
3596 }
3597 
3598 static u8 get_joiner_primary_pipe(enum pipe pipe, u8 primary_pipes)
3599 {
3600 	primary_pipes &= GENMASK(pipe, 0);
3601 
3602 	return primary_pipes ? BIT(fls(primary_pipes) - 1) : 0;
3603 }
3604 
3605 static u8 expected_ultrajoiner_secondary_pipes(u8 ultrajoiner_primary_pipes)
3606 {
3607 	return expected_secondary_pipes(ultrajoiner_primary_pipes, 4);
3608 }
3609 
3610 static u8 fixup_ultrajoiner_secondary_pipes(u8 ultrajoiner_primary_pipes,
3611 					    u8 ultrajoiner_secondary_pipes)
3612 {
3613 	return ultrajoiner_secondary_pipes | ultrajoiner_primary_pipes << 3;
3614 }
3615 
3616 static void enabled_ultrajoiner_pipes(struct intel_display *display,
3617 				      u8 *primary_pipes, u8 *secondary_pipes)
3618 {
3619 	struct intel_crtc *crtc;
3620 
3621 	*primary_pipes = 0;
3622 	*secondary_pipes = 0;
3623 
3624 	if (!HAS_ULTRAJOINER(display))
3625 		return;
3626 
3627 	for_each_intel_crtc_in_pipe_mask(display->drm, crtc,
3628 					 joiner_pipes(display)) {
3629 		enum intel_display_power_domain power_domain;
3630 		enum pipe pipe = crtc->pipe;
3631 
3632 		power_domain = intel_dsc_power_domain(crtc, (enum transcoder)pipe);
3633 		with_intel_display_power_if_enabled(display, power_domain) {
3634 			u32 tmp = intel_de_read(display, ICL_PIPE_DSS_CTL1(pipe));
3635 
3636 			if (!(tmp & ULTRA_JOINER_ENABLE))
3637 				continue;
3638 
3639 			if (tmp & PRIMARY_ULTRA_JOINER_ENABLE)
3640 				*primary_pipes |= BIT(pipe);
3641 			else
3642 				*secondary_pipes |= BIT(pipe);
3643 		}
3644 	}
3645 }
3646 
3647 static void enabled_joiner_pipes(struct intel_display *display,
3648 				 enum pipe pipe,
3649 				 u8 *primary_pipe, u8 *secondary_pipes)
3650 {
3651 	u8 primary_ultrajoiner_pipes;
3652 	u8 primary_uncompressed_joiner_pipes, primary_bigjoiner_pipes;
3653 	u8 secondary_ultrajoiner_pipes;
3654 	u8 secondary_uncompressed_joiner_pipes, secondary_bigjoiner_pipes;
3655 	u8 ultrajoiner_pipes;
3656 	u8 uncompressed_joiner_pipes, bigjoiner_pipes;
3657 
3658 	enabled_ultrajoiner_pipes(display, &primary_ultrajoiner_pipes,
3659 				  &secondary_ultrajoiner_pipes);
3660 	/*
3661 	 * For some strange reason the last pipe in the set of four
3662 	 * shouldn't have ultrajoiner enable bit set in hardware.
3663 	 * Set the bit anyway to make life easier.
3664 	 */
3665 	drm_WARN_ON(display->drm,
3666 		    expected_secondary_pipes(primary_ultrajoiner_pipes, 3) !=
3667 		    secondary_ultrajoiner_pipes);
3668 	secondary_ultrajoiner_pipes =
3669 		fixup_ultrajoiner_secondary_pipes(primary_ultrajoiner_pipes,
3670 						  secondary_ultrajoiner_pipes);
3671 
3672 	drm_WARN_ON(display->drm, (primary_ultrajoiner_pipes & secondary_ultrajoiner_pipes) != 0);
3673 
3674 	enabled_uncompressed_joiner_pipes(display, &primary_uncompressed_joiner_pipes,
3675 					  &secondary_uncompressed_joiner_pipes);
3676 
3677 	drm_WARN_ON(display->drm,
3678 		    (primary_uncompressed_joiner_pipes & secondary_uncompressed_joiner_pipes) != 0);
3679 
3680 	enabled_bigjoiner_pipes(display, &primary_bigjoiner_pipes,
3681 				&secondary_bigjoiner_pipes);
3682 
3683 	drm_WARN_ON(display->drm,
3684 		    (primary_bigjoiner_pipes & secondary_bigjoiner_pipes) != 0);
3685 
3686 	ultrajoiner_pipes = primary_ultrajoiner_pipes | secondary_ultrajoiner_pipes;
3687 	uncompressed_joiner_pipes = primary_uncompressed_joiner_pipes |
3688 				    secondary_uncompressed_joiner_pipes;
3689 	bigjoiner_pipes = primary_bigjoiner_pipes | secondary_bigjoiner_pipes;
3690 
3691 	drm_WARN(display->drm, (ultrajoiner_pipes & bigjoiner_pipes) != ultrajoiner_pipes,
3692 		 "Ultrajoiner pipes(%#x) should be bigjoiner pipes(%#x)\n",
3693 		 ultrajoiner_pipes, bigjoiner_pipes);
3694 
3695 	drm_WARN(display->drm, secondary_ultrajoiner_pipes !=
3696 		 expected_ultrajoiner_secondary_pipes(primary_ultrajoiner_pipes),
3697 		 "Wrong secondary ultrajoiner pipes(expected %#x, current %#x)\n",
3698 		 expected_ultrajoiner_secondary_pipes(primary_ultrajoiner_pipes),
3699 		 secondary_ultrajoiner_pipes);
3700 
3701 	drm_WARN(display->drm, (uncompressed_joiner_pipes & bigjoiner_pipes) != 0,
3702 		 "Uncompressed joiner pipes(%#x) and bigjoiner pipes(%#x) can't intersect\n",
3703 		 uncompressed_joiner_pipes, bigjoiner_pipes);
3704 
3705 	drm_WARN(display->drm, secondary_bigjoiner_pipes !=
3706 		 expected_bigjoiner_secondary_pipes(primary_bigjoiner_pipes),
3707 		 "Wrong secondary bigjoiner pipes(expected %#x, current %#x)\n",
3708 		 expected_bigjoiner_secondary_pipes(primary_bigjoiner_pipes),
3709 		 secondary_bigjoiner_pipes);
3710 
3711 	drm_WARN(display->drm, secondary_uncompressed_joiner_pipes !=
3712 		 expected_uncompressed_joiner_secondary_pipes(primary_uncompressed_joiner_pipes),
3713 		 "Wrong secondary uncompressed joiner pipes(expected %#x, current %#x)\n",
3714 		 expected_uncompressed_joiner_secondary_pipes(primary_uncompressed_joiner_pipes),
3715 		 secondary_uncompressed_joiner_pipes);
3716 
3717 	*primary_pipe = 0;
3718 	*secondary_pipes = 0;
3719 
3720 	if (ultrajoiner_pipes & BIT(pipe)) {
3721 		*primary_pipe = get_joiner_primary_pipe(pipe, primary_ultrajoiner_pipes);
3722 		*secondary_pipes = secondary_ultrajoiner_pipes &
3723 				   expected_ultrajoiner_secondary_pipes(*primary_pipe);
3724 
3725 		drm_WARN(display->drm,
3726 			 expected_ultrajoiner_secondary_pipes(*primary_pipe) !=
3727 			 *secondary_pipes,
3728 			 "Wrong ultrajoiner secondary pipes for primary_pipe %#x (expected %#x, current %#x)\n",
3729 			 *primary_pipe,
3730 			 expected_ultrajoiner_secondary_pipes(*primary_pipe),
3731 			 *secondary_pipes);
3732 		return;
3733 	}
3734 
3735 	if (uncompressed_joiner_pipes & BIT(pipe)) {
3736 		*primary_pipe = get_joiner_primary_pipe(pipe, primary_uncompressed_joiner_pipes);
3737 		*secondary_pipes = secondary_uncompressed_joiner_pipes &
3738 				   expected_uncompressed_joiner_secondary_pipes(*primary_pipe);
3739 
3740 		drm_WARN(display->drm,
3741 			 expected_uncompressed_joiner_secondary_pipes(*primary_pipe) !=
3742 			 *secondary_pipes,
3743 			 "Wrong uncompressed joiner secondary pipes for primary_pipe %#x (expected %#x, current %#x)\n",
3744 			 *primary_pipe,
3745 			 expected_uncompressed_joiner_secondary_pipes(*primary_pipe),
3746 			 *secondary_pipes);
3747 		return;
3748 	}
3749 
3750 	if (bigjoiner_pipes & BIT(pipe)) {
3751 		*primary_pipe = get_joiner_primary_pipe(pipe, primary_bigjoiner_pipes);
3752 		*secondary_pipes = secondary_bigjoiner_pipes &
3753 				   expected_bigjoiner_secondary_pipes(*primary_pipe);
3754 
3755 		drm_WARN(display->drm,
3756 			 expected_bigjoiner_secondary_pipes(*primary_pipe) !=
3757 			 *secondary_pipes,
3758 			 "Wrong bigjoiner secondary pipes for primary_pipe %#x (expected %#x, current %#x)\n",
3759 			 *primary_pipe,
3760 			 expected_bigjoiner_secondary_pipes(*primary_pipe),
3761 			 *secondary_pipes);
3762 		return;
3763 	}
3764 }
3765 
3766 static u8 hsw_panel_transcoders(struct intel_display *display)
3767 {
3768 	u8 panel_transcoder_mask = BIT(TRANSCODER_EDP);
3769 
3770 	if (DISPLAY_VER(display) >= 11)
3771 		panel_transcoder_mask |= BIT(TRANSCODER_DSI_0) | BIT(TRANSCODER_DSI_1);
3772 
3773 	return panel_transcoder_mask;
3774 }
3775 
3776 static u8 hsw_enabled_transcoders(struct intel_crtc *crtc)
3777 {
3778 	struct intel_display *display = to_intel_display(crtc);
3779 	u8 panel_transcoder_mask = hsw_panel_transcoders(display);
3780 	enum transcoder cpu_transcoder;
3781 	u8 primary_pipe, secondary_pipes;
3782 	u8 enabled_transcoders = 0;
3783 
3784 	/*
3785 	 * XXX: Do intel_display_power_get_if_enabled before reading this (for
3786 	 * consistency and less surprising code; it's in always on power).
3787 	 */
3788 	for_each_cpu_transcoder_masked(display, cpu_transcoder,
3789 				       panel_transcoder_mask) {
3790 		enum intel_display_power_domain power_domain;
3791 		enum pipe trans_pipe;
3792 		u32 tmp = 0;
3793 
3794 		power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
3795 		with_intel_display_power_if_enabled(display, power_domain)
3796 			tmp = intel_de_read(display,
3797 					    TRANS_DDI_FUNC_CTL(display, cpu_transcoder));
3798 
3799 		if (!(tmp & TRANS_DDI_FUNC_ENABLE))
3800 			continue;
3801 
3802 		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
3803 		default:
3804 			drm_WARN(display->drm, 1,
3805 				 "unknown pipe linked to transcoder %s\n",
3806 				 transcoder_name(cpu_transcoder));
3807 			fallthrough;
3808 		case TRANS_DDI_EDP_INPUT_A_ONOFF:
3809 		case TRANS_DDI_EDP_INPUT_A_ON:
3810 			trans_pipe = PIPE_A;
3811 			break;
3812 		case TRANS_DDI_EDP_INPUT_B_ONOFF:
3813 			trans_pipe = PIPE_B;
3814 			break;
3815 		case TRANS_DDI_EDP_INPUT_C_ONOFF:
3816 			trans_pipe = PIPE_C;
3817 			break;
3818 		case TRANS_DDI_EDP_INPUT_D_ONOFF:
3819 			trans_pipe = PIPE_D;
3820 			break;
3821 		}
3822 
3823 		if (trans_pipe == crtc->pipe)
3824 			enabled_transcoders |= BIT(cpu_transcoder);
3825 	}
3826 
3827 	/* single pipe or joiner primary */
3828 	cpu_transcoder = (enum transcoder) crtc->pipe;
3829 	if (transcoder_ddi_func_is_enabled(display, cpu_transcoder))
3830 		enabled_transcoders |= BIT(cpu_transcoder);
3831 
3832 	/* joiner secondary -> consider the primary pipe's transcoder as well */
3833 	enabled_joiner_pipes(display, crtc->pipe, &primary_pipe, &secondary_pipes);
3834 	if (secondary_pipes & BIT(crtc->pipe)) {
3835 		cpu_transcoder = (enum transcoder)ffs(primary_pipe) - 1;
3836 		if (transcoder_ddi_func_is_enabled(display, cpu_transcoder))
3837 			enabled_transcoders |= BIT(cpu_transcoder);
3838 	}
3839 
3840 	return enabled_transcoders;
3841 }
3842 
3843 static bool has_edp_transcoders(u8 enabled_transcoders)
3844 {
3845 	return enabled_transcoders & BIT(TRANSCODER_EDP);
3846 }
3847 
3848 static bool has_dsi_transcoders(u8 enabled_transcoders)
3849 {
3850 	return enabled_transcoders & (BIT(TRANSCODER_DSI_0) |
3851 				      BIT(TRANSCODER_DSI_1));
3852 }
3853 
3854 static bool has_pipe_transcoders(u8 enabled_transcoders)
3855 {
3856 	return enabled_transcoders & ~(BIT(TRANSCODER_EDP) |
3857 				       BIT(TRANSCODER_DSI_0) |
3858 				       BIT(TRANSCODER_DSI_1));
3859 }
3860 
3861 static void assert_enabled_transcoders(struct intel_display *display,
3862 				       u8 enabled_transcoders)
3863 {
3864 	/* Only one type of transcoder please */
3865 	drm_WARN_ON(display->drm,
3866 		    has_edp_transcoders(enabled_transcoders) +
3867 		    has_dsi_transcoders(enabled_transcoders) +
3868 		    has_pipe_transcoders(enabled_transcoders) > 1);
3869 
3870 	/* Only DSI transcoders can be ganged */
3871 	drm_WARN_ON(display->drm,
3872 		    !has_dsi_transcoders(enabled_transcoders) &&
3873 		    !is_power_of_2(enabled_transcoders));
3874 }
3875 
3876 static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
3877 				     struct intel_crtc_state *pipe_config,
3878 				     struct intel_display_power_domain_set *power_domain_set)
3879 {
3880 	struct intel_display *display = to_intel_display(crtc);
3881 	unsigned long enabled_transcoders;
3882 	u32 tmp;
3883 
3884 	enabled_transcoders = hsw_enabled_transcoders(crtc);
3885 	if (!enabled_transcoders)
3886 		return false;
3887 
3888 	assert_enabled_transcoders(display, enabled_transcoders);
3889 
3890 	/*
3891 	 * With the exception of DSI we should only ever have
3892 	 * a single enabled transcoder. With DSI let's just
3893 	 * pick the first one.
3894 	 */
3895 	pipe_config->cpu_transcoder = ffs(enabled_transcoders) - 1;
3896 
3897 	if (!intel_display_power_get_in_set_if_enabled(display, power_domain_set,
3898 						       POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
3899 		return false;
3900 
3901 	if (hsw_panel_transcoders(display) & BIT(pipe_config->cpu_transcoder)) {
3902 		tmp = intel_de_read(display,
3903 				    TRANS_DDI_FUNC_CTL(display, pipe_config->cpu_transcoder));
3904 
3905 		if ((tmp & TRANS_DDI_EDP_INPUT_MASK) == TRANS_DDI_EDP_INPUT_A_ONOFF)
3906 			pipe_config->pch_pfit.force_thru = true;
3907 	}
3908 
3909 	tmp = intel_de_read(display,
3910 			    TRANSCONF(display, pipe_config->cpu_transcoder));
3911 
3912 	return tmp & TRANSCONF_ENABLE;
3913 }
3914 
3915 static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
3916 					 struct intel_crtc_state *pipe_config,
3917 					 struct intel_display_power_domain_set *power_domain_set)
3918 {
3919 	struct intel_display *display = to_intel_display(crtc);
3920 	enum transcoder cpu_transcoder;
3921 	enum port port;
3922 	u32 tmp;
3923 
3924 	for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
3925 		if (port == PORT_A)
3926 			cpu_transcoder = TRANSCODER_DSI_A;
3927 		else
3928 			cpu_transcoder = TRANSCODER_DSI_C;
3929 
3930 		if (!intel_display_power_get_in_set_if_enabled(display, power_domain_set,
3931 							       POWER_DOMAIN_TRANSCODER(cpu_transcoder)))
3932 			continue;
3933 
3934 		/*
3935 		 * The PLL needs to be enabled with a valid divider
3936 		 * configuration, otherwise accessing DSI registers will hang
3937 		 * the machine. See BSpec North Display Engine
3938 		 * registers/MIPI[BXT]. We can break out here early, since we
3939 		 * need the same DSI PLL to be enabled for both DSI ports.
3940 		 */
3941 		if (!bxt_dsi_pll_is_enabled(display))
3942 			break;
3943 
3944 		/* XXX: this works for video mode only */
3945 		tmp = intel_de_read(display, BXT_MIPI_PORT_CTRL(port));
3946 		if (!(tmp & DPI_ENABLE))
3947 			continue;
3948 
3949 		tmp = intel_de_read(display, MIPI_CTRL(display, port));
3950 		if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
3951 			continue;
3952 
3953 		pipe_config->cpu_transcoder = cpu_transcoder;
3954 		break;
3955 	}
3956 
3957 	return transcoder_is_dsi(pipe_config->cpu_transcoder);
3958 }
3959 
3960 static void intel_joiner_get_config(struct intel_crtc_state *crtc_state)
3961 {
3962 	struct intel_display *display = to_intel_display(crtc_state);
3963 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3964 	u8 primary_pipe, secondary_pipes;
3965 	enum pipe pipe = crtc->pipe;
3966 
3967 	enabled_joiner_pipes(display, pipe, &primary_pipe, &secondary_pipes);
3968 
3969 	if (((primary_pipe | secondary_pipes) & BIT(pipe)) == 0)
3970 		return;
3971 
3972 	crtc_state->joiner_pipes = primary_pipe | secondary_pipes;
3973 }
3974 
3975 static bool hsw_get_pipe_config(struct intel_crtc *crtc,
3976 				struct intel_crtc_state *pipe_config)
3977 {
3978 	struct intel_display *display = to_intel_display(crtc);
3979 	bool active;
3980 	u32 tmp;
3981 
3982 	if (!intel_display_power_get_in_set_if_enabled(display, &crtc->hw_readout_power_domains,
3983 						       POWER_DOMAIN_PIPE(crtc->pipe)))
3984 		return false;
3985 
3986 	active = hsw_get_transcoder_state(crtc, pipe_config, &crtc->hw_readout_power_domains);
3987 
3988 	if ((display->platform.geminilake || display->platform.broxton) &&
3989 	    bxt_get_dsi_transcoder_state(crtc, pipe_config, &crtc->hw_readout_power_domains)) {
3990 		drm_WARN_ON(display->drm, active);
3991 		active = true;
3992 	}
3993 
3994 	if (!active)
3995 		goto out;
3996 
3997 	intel_joiner_get_config(pipe_config);
3998 	intel_dsc_get_config(pipe_config);
3999 
4000 	/* intel_vrr_get_config() depends on .framestart_delay */
4001 	if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4002 		tmp = intel_de_read(display, CHICKEN_TRANS(display, pipe_config->cpu_transcoder));
4003 
4004 		pipe_config->framestart_delay = REG_FIELD_GET(HSW_FRAME_START_DELAY_MASK, tmp) + 1;
4005 	} else {
4006 		/* no idea if this is correct */
4007 		pipe_config->framestart_delay = 1;
4008 	}
4009 
4010 	/*
4011 	 * intel_vrr_get_config() depends on TRANS_SET_CONTEXT_LATENCY
4012 	 * readout done by intel_get_transcoder_timings().
4013 	 */
4014 	if (!transcoder_is_dsi(pipe_config->cpu_transcoder) ||
4015 	    DISPLAY_VER(display) >= 11)
4016 		intel_get_transcoder_timings(crtc, pipe_config);
4017 
4018 	if (transcoder_has_vrr(pipe_config))
4019 		intel_vrr_get_config(pipe_config);
4020 
4021 	intel_get_pipe_src_size(crtc, pipe_config);
4022 
4023 	if (display->platform.haswell) {
4024 		u32 tmp = intel_de_read(display,
4025 					TRANSCONF(display, pipe_config->cpu_transcoder));
4026 
4027 		if (tmp & TRANSCONF_OUTPUT_COLORSPACE_YUV_HSW)
4028 			pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
4029 		else
4030 			pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
4031 	} else {
4032 		pipe_config->output_format =
4033 			bdw_get_pipe_misc_output_format(crtc);
4034 	}
4035 
4036 	pipe_config->sink_format = pipe_config->output_format;
4037 
4038 	intel_color_get_config(pipe_config);
4039 
4040 	tmp = intel_de_read(display, WM_LINETIME(crtc->pipe));
4041 	pipe_config->linetime = REG_FIELD_GET(HSW_LINETIME_MASK, tmp);
4042 	if (display->platform.broadwell || display->platform.haswell)
4043 		pipe_config->ips_linetime =
4044 			REG_FIELD_GET(HSW_IPS_LINETIME_MASK, tmp);
4045 
4046 	if (intel_display_power_get_in_set_if_enabled(display, &crtc->hw_readout_power_domains,
4047 						      POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe))) {
4048 		if (DISPLAY_VER(display) >= 9)
4049 			skl_scaler_get_config(pipe_config);
4050 		else
4051 			ilk_pfit_get_config(pipe_config);
4052 	}
4053 
4054 	hsw_ips_get_config(pipe_config);
4055 
4056 	if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
4057 	    !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4058 		pipe_config->pixel_multiplier =
4059 			intel_de_read(display,
4060 				      TRANS_MULT(display, pipe_config->cpu_transcoder)) + 1;
4061 	} else {
4062 		pipe_config->pixel_multiplier = 1;
4063 	}
4064 
4065 out:
4066 	intel_display_power_put_all_in_set(display, &crtc->hw_readout_power_domains);
4067 
4068 	return active;
4069 }
4070 
4071 bool intel_crtc_get_pipe_config(struct intel_crtc_state *crtc_state)
4072 {
4073 	struct intel_display *display = to_intel_display(crtc_state);
4074 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4075 
4076 	if (!display->funcs.display->get_pipe_config(crtc, crtc_state))
4077 		return false;
4078 
4079 	crtc_state->hw.active = true;
4080 
4081 	intel_crtc_readout_derived_state(crtc_state);
4082 
4083 	return true;
4084 }
4085 
4086 int intel_dotclock_calculate(int link_freq,
4087 			     const struct intel_link_m_n *m_n)
4088 {
4089 	/*
4090 	 * The calculation for the data clock -> pixel clock is:
4091 	 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
4092 	 * But we want to avoid losing precision if possible, so:
4093 	 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
4094 	 *
4095 	 * and for link freq (10kbs units) -> pixel clock it is:
4096 	 * link_symbol_clock = link_freq * 10 / link_symbol_size
4097 	 * pixel_clock = (m * link_symbol_clock) / n
4098 	 *    or for more precision:
4099 	 * pixel_clock = (m * link_freq * 10) / (n * link_symbol_size)
4100 	 */
4101 
4102 	if (!m_n->link_n)
4103 		return 0;
4104 
4105 	return DIV_ROUND_UP_ULL(mul_u32_u32(m_n->link_m, link_freq * 10),
4106 				m_n->link_n * intel_dp_link_symbol_size(link_freq));
4107 }
4108 
4109 int intel_crtc_dotclock(const struct intel_crtc_state *pipe_config)
4110 {
4111 	int dotclock;
4112 
4113 	if (intel_crtc_has_dp_encoder(pipe_config))
4114 		dotclock = intel_dotclock_calculate(pipe_config->port_clock,
4115 						    &pipe_config->dp_m_n);
4116 	else if (pipe_config->has_hdmi_sink && pipe_config->pipe_bpp > 24)
4117 		dotclock = DIV_ROUND_CLOSEST(pipe_config->port_clock * 24,
4118 					     pipe_config->pipe_bpp);
4119 	else
4120 		dotclock = pipe_config->port_clock;
4121 
4122 	if (pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 &&
4123 	    !intel_crtc_has_dp_encoder(pipe_config))
4124 		dotclock *= 2;
4125 
4126 	if (pipe_config->pixel_multiplier)
4127 		dotclock /= pipe_config->pixel_multiplier;
4128 
4129 	return dotclock;
4130 }
4131 
4132 /* Returns the currently programmed mode of the given encoder. */
4133 struct drm_display_mode *
4134 intel_encoder_current_mode(struct intel_encoder *encoder)
4135 {
4136 	struct intel_display *display = to_intel_display(encoder);
4137 	struct intel_crtc_state *crtc_state;
4138 	struct drm_display_mode *mode;
4139 	struct intel_crtc *crtc;
4140 	enum pipe pipe;
4141 
4142 	if (!encoder->get_hw_state(encoder, &pipe))
4143 		return NULL;
4144 
4145 	crtc = intel_crtc_for_pipe(display, pipe);
4146 
4147 	mode = kzalloc_obj(*mode);
4148 	if (!mode)
4149 		return NULL;
4150 
4151 	crtc_state = intel_crtc_state_alloc(crtc);
4152 	if (!crtc_state) {
4153 		kfree(mode);
4154 		return NULL;
4155 	}
4156 
4157 	if (!intel_crtc_get_pipe_config(crtc_state)) {
4158 		intel_crtc_destroy_state(&crtc->base, &crtc_state->uapi);
4159 		kfree(mode);
4160 		return NULL;
4161 	}
4162 
4163 	intel_encoder_get_config(encoder, crtc_state);
4164 
4165 	intel_mode_from_crtc_timings(mode, &crtc_state->hw.adjusted_mode);
4166 
4167 	intel_crtc_destroy_state(&crtc->base, &crtc_state->uapi);
4168 
4169 	return mode;
4170 }
4171 
4172 static bool encoders_cloneable(const struct intel_encoder *a,
4173 			       const struct intel_encoder *b)
4174 {
4175 	/* masks could be asymmetric, so check both ways */
4176 	return a == b || (a->cloneable & BIT(b->type) &&
4177 			  b->cloneable & BIT(a->type));
4178 }
4179 
4180 static bool check_single_encoder_cloning(struct intel_atomic_state *state,
4181 					 struct intel_crtc *crtc,
4182 					 struct intel_encoder *encoder)
4183 {
4184 	struct intel_encoder *source_encoder;
4185 	struct drm_connector *connector;
4186 	struct drm_connector_state *connector_state;
4187 	int i;
4188 
4189 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
4190 		if (connector_state->crtc != &crtc->base)
4191 			continue;
4192 
4193 		source_encoder =
4194 			to_intel_encoder(connector_state->best_encoder);
4195 		if (!encoders_cloneable(encoder, source_encoder))
4196 			return false;
4197 	}
4198 
4199 	return true;
4200 }
4201 
4202 static u16 hsw_linetime_wm(const struct intel_crtc_state *crtc_state)
4203 {
4204 	const struct drm_display_mode *pipe_mode =
4205 		&crtc_state->hw.pipe_mode;
4206 	int linetime_wm;
4207 
4208 	if (!crtc_state->hw.enable)
4209 		return 0;
4210 
4211 	linetime_wm = DIV_ROUND_CLOSEST(pipe_mode->crtc_htotal * 1000 * 8,
4212 					pipe_mode->crtc_clock);
4213 
4214 	return min(linetime_wm, 0x1ff);
4215 }
4216 
4217 static u16 hsw_ips_linetime_wm(const struct intel_crtc_state *crtc_state,
4218 			       const struct intel_cdclk_state *cdclk_state)
4219 {
4220 	const struct drm_display_mode *pipe_mode =
4221 		&crtc_state->hw.pipe_mode;
4222 	int linetime_wm;
4223 
4224 	if (!crtc_state->hw.enable)
4225 		return 0;
4226 
4227 	linetime_wm = DIV_ROUND_CLOSEST(pipe_mode->crtc_htotal * 1000 * 8,
4228 					intel_cdclk_logical(cdclk_state));
4229 
4230 	return min(linetime_wm, 0x1ff);
4231 }
4232 
4233 static u16 skl_linetime_wm(const struct intel_crtc_state *crtc_state)
4234 {
4235 	struct intel_display *display = to_intel_display(crtc_state);
4236 	const struct drm_display_mode *pipe_mode =
4237 		&crtc_state->hw.pipe_mode;
4238 	int linetime_wm;
4239 
4240 	if (!crtc_state->hw.enable)
4241 		return 0;
4242 
4243 	linetime_wm = DIV_ROUND_UP(pipe_mode->crtc_htotal * 1000 * 8,
4244 				   crtc_state->pixel_rate);
4245 
4246 	/* Display WA #1135: BXT:ALL GLK:ALL */
4247 	if ((display->platform.geminilake || display->platform.broxton) &&
4248 	    skl_watermark_ipc_enabled(display))
4249 		linetime_wm /= 2;
4250 
4251 	return min(linetime_wm, 0x1ff);
4252 }
4253 
4254 static int hsw_compute_linetime_wm(struct intel_atomic_state *state,
4255 				   struct intel_crtc *crtc)
4256 {
4257 	struct intel_display *display = to_intel_display(state);
4258 	struct intel_crtc_state *crtc_state =
4259 		intel_atomic_get_new_crtc_state(state, crtc);
4260 	const struct intel_cdclk_state *cdclk_state;
4261 
4262 	if (DISPLAY_VER(display) >= 9)
4263 		crtc_state->linetime = skl_linetime_wm(crtc_state);
4264 	else
4265 		crtc_state->linetime = hsw_linetime_wm(crtc_state);
4266 
4267 	if (!hsw_crtc_supports_ips(crtc))
4268 		return 0;
4269 
4270 	cdclk_state = intel_atomic_get_cdclk_state(state);
4271 	if (IS_ERR(cdclk_state))
4272 		return PTR_ERR(cdclk_state);
4273 
4274 	crtc_state->ips_linetime = hsw_ips_linetime_wm(crtc_state,
4275 						       cdclk_state);
4276 
4277 	return 0;
4278 }
4279 
4280 static int intel_crtc_atomic_check(struct intel_atomic_state *state,
4281 				   struct intel_crtc *crtc)
4282 {
4283 	struct intel_display *display = to_intel_display(crtc);
4284 	struct intel_crtc_state *crtc_state =
4285 		intel_atomic_get_new_crtc_state(state, crtc);
4286 	int ret;
4287 
4288 	if (DISPLAY_VER(display) < 5 && !display->platform.g4x &&
4289 	    intel_crtc_needs_modeset(crtc_state) &&
4290 	    !crtc_state->hw.active)
4291 		crtc_state->update_wm_post = true;
4292 
4293 	if (intel_crtc_needs_modeset(crtc_state)) {
4294 		ret = intel_dpll_crtc_get_dpll(state, crtc);
4295 		if (ret)
4296 			return ret;
4297 	}
4298 
4299 	ret = intel_color_check(state, crtc);
4300 	if (ret)
4301 		return ret;
4302 
4303 	ret = intel_wm_compute(state, crtc);
4304 	if (ret) {
4305 		drm_dbg_kms(display->drm,
4306 			    "[CRTC:%d:%s] watermarks are invalid\n",
4307 			    crtc->base.base.id, crtc->base.name);
4308 		return ret;
4309 	}
4310 
4311 	ret = intel_casf_compute_config(crtc_state);
4312 	if (ret)
4313 		return ret;
4314 
4315 	if (DISPLAY_VER(display) >= 9) {
4316 		if (intel_crtc_needs_modeset(crtc_state) ||
4317 		    intel_crtc_needs_fastset(crtc_state) ||
4318 		    intel_casf_needs_scaler(crtc_state)) {
4319 			ret = skl_update_scaler_crtc(crtc_state);
4320 			if (ret)
4321 				return ret;
4322 		}
4323 
4324 		ret = intel_atomic_setup_scalers(state, crtc);
4325 		if (ret)
4326 			return ret;
4327 	}
4328 
4329 	if (HAS_IPS(display)) {
4330 		ret = hsw_ips_compute_config(state, crtc);
4331 		if (ret)
4332 			return ret;
4333 	}
4334 
4335 	if (DISPLAY_VER(display) >= 9 ||
4336 	    display->platform.broadwell || display->platform.haswell) {
4337 		ret = hsw_compute_linetime_wm(state, crtc);
4338 		if (ret)
4339 			return ret;
4340 
4341 	}
4342 
4343 	ret = intel_psr2_sel_fetch_update(state, crtc);
4344 	if (ret)
4345 		return ret;
4346 
4347 	return 0;
4348 }
4349 
4350 static int bpc_to_bpp(int bpc)
4351 {
4352 	switch (bpc) {
4353 	case 6 ... 7:
4354 		return 6 * 3;
4355 	case 8 ... 9:
4356 		return 8 * 3;
4357 	case 10 ... 11:
4358 		return 10 * 3;
4359 	case 12 ... 16:
4360 		return 12 * 3;
4361 	default:
4362 		MISSING_CASE(bpc);
4363 		return -EINVAL;
4364 	}
4365 }
4366 
4367 static int
4368 compute_sink_pipe_bpp(const struct drm_connector_state *conn_state,
4369 		      struct intel_crtc_state *crtc_state)
4370 {
4371 	struct intel_display *display = to_intel_display(crtc_state);
4372 	struct drm_connector *connector = conn_state->connector;
4373 	const struct drm_display_info *info = &connector->display_info;
4374 	int edid_bpc = info->bpc ? : 8;
4375 	int target_pipe_bpp;
4376 	int max_edid_bpp;
4377 
4378 	max_edid_bpp = bpc_to_bpp(edid_bpc);
4379 	if (max_edid_bpp < 0)
4380 		return max_edid_bpp;
4381 
4382 	target_pipe_bpp = bpc_to_bpp(conn_state->max_bpc);
4383 	if (target_pipe_bpp < 0)
4384 		return target_pipe_bpp;
4385 
4386 	/*
4387 	 * The maximum pipe BPP is the minimum of the max platform BPP and
4388 	 * the max EDID BPP.
4389 	 */
4390 	crtc_state->max_pipe_bpp = min(crtc_state->pipe_bpp, max_edid_bpp);
4391 
4392 	if (target_pipe_bpp < crtc_state->pipe_bpp) {
4393 		drm_dbg_kms(display->drm,
4394 			    "[CONNECTOR:%d:%s] Limiting target display pipe bpp to %d "
4395 			    "(EDID bpp %d, max requested bpp %d, max platform bpp %d)\n",
4396 			    connector->base.id, connector->name,
4397 			    target_pipe_bpp, 3 * info->bpc,
4398 			    3 * conn_state->max_requested_bpc,
4399 			    crtc_state->pipe_bpp);
4400 
4401 		crtc_state->pipe_bpp = target_pipe_bpp;
4402 	}
4403 
4404 	return 0;
4405 }
4406 
4407 int intel_display_min_pipe_bpp(void)
4408 {
4409 	return 6 * 3;
4410 }
4411 
4412 int intel_display_max_pipe_bpp(struct intel_display *display)
4413 {
4414 	if (display->platform.g4x || display->platform.valleyview ||
4415 	    display->platform.cherryview)
4416 		return 10*3;
4417 	else if (DISPLAY_VER(display) >= 5)
4418 		return 12*3;
4419 	else
4420 		return 8*3;
4421 }
4422 
4423 static int
4424 compute_baseline_pipe_bpp(struct intel_atomic_state *state,
4425 			  struct intel_crtc *crtc)
4426 {
4427 	struct intel_display *display = to_intel_display(crtc);
4428 	struct intel_crtc_state *crtc_state =
4429 		intel_atomic_get_new_crtc_state(state, crtc);
4430 	struct drm_connector *connector;
4431 	struct drm_connector_state *connector_state;
4432 	int i;
4433 
4434 	crtc_state->pipe_bpp = intel_display_max_pipe_bpp(display);
4435 
4436 	/* Clamp display bpp to connector max bpp */
4437 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
4438 		int ret;
4439 
4440 		if (connector_state->crtc != &crtc->base)
4441 			continue;
4442 
4443 		ret = compute_sink_pipe_bpp(connector_state, crtc_state);
4444 		if (ret)
4445 			return ret;
4446 	}
4447 
4448 	return 0;
4449 }
4450 
4451 static bool check_digital_port_conflicts(struct intel_atomic_state *state)
4452 {
4453 	struct intel_display *display = to_intel_display(state);
4454 	struct drm_connector *connector;
4455 	struct drm_connector_list_iter conn_iter;
4456 	unsigned int used_ports = 0;
4457 	unsigned int used_mst_ports = 0;
4458 	bool ret = true;
4459 
4460 	/*
4461 	 * We're going to peek into connector->state,
4462 	 * hence connection_mutex must be held.
4463 	 */
4464 	drm_modeset_lock_assert_held(&display->drm->mode_config.connection_mutex);
4465 
4466 	/*
4467 	 * Walk the connector list instead of the encoder
4468 	 * list to detect the problem on ddi platforms
4469 	 * where there's just one encoder per digital port.
4470 	 */
4471 	drm_connector_list_iter_begin(display->drm, &conn_iter);
4472 	drm_for_each_connector_iter(connector, &conn_iter) {
4473 		struct drm_connector_state *connector_state;
4474 		struct intel_encoder *encoder;
4475 
4476 		connector_state =
4477 			drm_atomic_get_new_connector_state(&state->base,
4478 							   connector);
4479 		if (!connector_state)
4480 			connector_state = connector->state;
4481 
4482 		if (!connector_state->best_encoder)
4483 			continue;
4484 
4485 		encoder = to_intel_encoder(connector_state->best_encoder);
4486 
4487 		drm_WARN_ON(display->drm, !connector_state->crtc);
4488 
4489 		switch (encoder->type) {
4490 		case INTEL_OUTPUT_DDI:
4491 			if (drm_WARN_ON(display->drm, !HAS_DDI(display)))
4492 				break;
4493 			fallthrough;
4494 		case INTEL_OUTPUT_DP:
4495 		case INTEL_OUTPUT_HDMI:
4496 		case INTEL_OUTPUT_EDP:
4497 			/* the same port mustn't appear more than once */
4498 			if (used_ports & BIT(encoder->port))
4499 				ret = false;
4500 
4501 			used_ports |= BIT(encoder->port);
4502 			break;
4503 		case INTEL_OUTPUT_DP_MST:
4504 			used_mst_ports |=
4505 				1 << encoder->port;
4506 			break;
4507 		default:
4508 			break;
4509 		}
4510 	}
4511 	drm_connector_list_iter_end(&conn_iter);
4512 
4513 	/* can't mix MST and SST/HDMI on the same port */
4514 	if (used_ports & used_mst_ports)
4515 		return false;
4516 
4517 	return ret;
4518 }
4519 
4520 static void
4521 intel_crtc_copy_uapi_to_hw_state_nomodeset(struct intel_atomic_state *state,
4522 					   struct intel_crtc *crtc)
4523 {
4524 	struct intel_crtc_state *crtc_state =
4525 		intel_atomic_get_new_crtc_state(state, crtc);
4526 
4527 	WARN_ON(intel_crtc_is_joiner_secondary(crtc_state));
4528 
4529 	drm_property_replace_blob(&crtc_state->hw.degamma_lut,
4530 				  crtc_state->uapi.degamma_lut);
4531 	drm_property_replace_blob(&crtc_state->hw.gamma_lut,
4532 				  crtc_state->uapi.gamma_lut);
4533 	drm_property_replace_blob(&crtc_state->hw.ctm,
4534 				  crtc_state->uapi.ctm);
4535 }
4536 
4537 static void
4538 intel_crtc_copy_uapi_to_hw_state_modeset(struct intel_atomic_state *state,
4539 					 struct intel_crtc *crtc)
4540 {
4541 	struct intel_crtc_state *crtc_state =
4542 		intel_atomic_get_new_crtc_state(state, crtc);
4543 
4544 	WARN_ON(intel_crtc_is_joiner_secondary(crtc_state));
4545 
4546 	crtc_state->hw.enable = crtc_state->uapi.enable;
4547 	crtc_state->hw.active = crtc_state->uapi.active;
4548 	drm_mode_copy(&crtc_state->hw.mode,
4549 		      &crtc_state->uapi.mode);
4550 	drm_mode_copy(&crtc_state->hw.adjusted_mode,
4551 		      &crtc_state->uapi.adjusted_mode);
4552 	crtc_state->hw.scaling_filter = crtc_state->uapi.scaling_filter;
4553 
4554 	intel_crtc_copy_uapi_to_hw_state_nomodeset(state, crtc);
4555 }
4556 
4557 static void
4558 copy_joiner_crtc_state_nomodeset(struct intel_atomic_state *state,
4559 				 struct intel_crtc *secondary_crtc)
4560 {
4561 	struct intel_crtc_state *secondary_crtc_state =
4562 		intel_atomic_get_new_crtc_state(state, secondary_crtc);
4563 	struct intel_crtc *primary_crtc = intel_primary_crtc(secondary_crtc_state);
4564 	const struct intel_crtc_state *primary_crtc_state =
4565 		intel_atomic_get_new_crtc_state(state, primary_crtc);
4566 
4567 	drm_property_replace_blob(&secondary_crtc_state->hw.degamma_lut,
4568 				  primary_crtc_state->hw.degamma_lut);
4569 	drm_property_replace_blob(&secondary_crtc_state->hw.gamma_lut,
4570 				  primary_crtc_state->hw.gamma_lut);
4571 	drm_property_replace_blob(&secondary_crtc_state->hw.ctm,
4572 				  primary_crtc_state->hw.ctm);
4573 
4574 	secondary_crtc_state->uapi.color_mgmt_changed = primary_crtc_state->uapi.color_mgmt_changed;
4575 }
4576 
4577 static int
4578 copy_joiner_crtc_state_modeset(struct intel_atomic_state *state,
4579 			       struct intel_crtc *secondary_crtc)
4580 {
4581 	struct intel_crtc_state *secondary_crtc_state =
4582 		intel_atomic_get_new_crtc_state(state, secondary_crtc);
4583 	struct intel_crtc *primary_crtc = intel_primary_crtc(secondary_crtc_state);
4584 	const struct intel_crtc_state *primary_crtc_state =
4585 		intel_atomic_get_new_crtc_state(state, primary_crtc);
4586 	struct intel_crtc_state *saved_state;
4587 
4588 	WARN_ON(primary_crtc_state->joiner_pipes !=
4589 		secondary_crtc_state->joiner_pipes);
4590 
4591 	saved_state = kmemdup(primary_crtc_state, sizeof(*saved_state), GFP_KERNEL);
4592 	if (!saved_state)
4593 		return -ENOMEM;
4594 
4595 	/* preserve some things from the slave's original crtc state */
4596 	saved_state->uapi = secondary_crtc_state->uapi;
4597 	saved_state->scaler_state = secondary_crtc_state->scaler_state;
4598 	saved_state->intel_dpll = secondary_crtc_state->intel_dpll;
4599 	saved_state->crc_enabled = secondary_crtc_state->crc_enabled;
4600 
4601 	intel_crtc_free_hw_state(secondary_crtc_state);
4602 	if (secondary_crtc_state->dp_tunnel_ref.tunnel)
4603 		drm_dp_tunnel_ref_put(&secondary_crtc_state->dp_tunnel_ref);
4604 	memcpy(secondary_crtc_state, saved_state, sizeof(*secondary_crtc_state));
4605 	kfree(saved_state);
4606 
4607 	/* Re-init hw state */
4608 	memset(&secondary_crtc_state->hw, 0, sizeof(secondary_crtc_state->hw));
4609 	secondary_crtc_state->hw.enable = primary_crtc_state->hw.enable;
4610 	secondary_crtc_state->hw.active = primary_crtc_state->hw.active;
4611 	drm_mode_copy(&secondary_crtc_state->hw.mode,
4612 		      &primary_crtc_state->hw.mode);
4613 	drm_mode_copy(&secondary_crtc_state->hw.pipe_mode,
4614 		      &primary_crtc_state->hw.pipe_mode);
4615 	drm_mode_copy(&secondary_crtc_state->hw.adjusted_mode,
4616 		      &primary_crtc_state->hw.adjusted_mode);
4617 	secondary_crtc_state->hw.scaling_filter = primary_crtc_state->hw.scaling_filter;
4618 
4619 	if (primary_crtc_state->dp_tunnel_ref.tunnel)
4620 		drm_dp_tunnel_ref_get(primary_crtc_state->dp_tunnel_ref.tunnel,
4621 				      &secondary_crtc_state->dp_tunnel_ref);
4622 
4623 	copy_joiner_crtc_state_nomodeset(state, secondary_crtc);
4624 
4625 	secondary_crtc_state->uapi.mode_changed = primary_crtc_state->uapi.mode_changed;
4626 	secondary_crtc_state->uapi.connectors_changed = primary_crtc_state->uapi.connectors_changed;
4627 	secondary_crtc_state->uapi.active_changed = primary_crtc_state->uapi.active_changed;
4628 
4629 	WARN_ON(primary_crtc_state->joiner_pipes !=
4630 		secondary_crtc_state->joiner_pipes);
4631 
4632 	return 0;
4633 }
4634 
4635 static int
4636 intel_crtc_prepare_cleared_state(struct intel_atomic_state *state,
4637 				 struct intel_crtc *crtc)
4638 {
4639 	struct intel_display *display = to_intel_display(state);
4640 	struct intel_crtc_state *crtc_state =
4641 		intel_atomic_get_new_crtc_state(state, crtc);
4642 	struct intel_crtc_state *saved_state;
4643 
4644 	saved_state = intel_crtc_state_alloc(crtc);
4645 	if (!saved_state)
4646 		return -ENOMEM;
4647 
4648 	/* free the old crtc_state->hw members */
4649 	intel_crtc_free_hw_state(crtc_state);
4650 
4651 	intel_dp_tunnel_atomic_clear_stream_bw(state, crtc_state);
4652 
4653 	/* FIXME: before the switch to atomic started, a new pipe_config was
4654 	 * kzalloc'd. Code that depends on any field being zero should be
4655 	 * fixed, so that the crtc_state can be safely duplicated. For now,
4656 	 * only fields that are know to not cause problems are preserved. */
4657 
4658 	saved_state->uapi = crtc_state->uapi;
4659 	saved_state->inherited = crtc_state->inherited;
4660 	saved_state->scaler_state = crtc_state->scaler_state;
4661 	saved_state->intel_dpll = crtc_state->intel_dpll;
4662 	saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
4663 	memcpy(saved_state->icl_port_dplls, crtc_state->icl_port_dplls,
4664 	       sizeof(saved_state->icl_port_dplls));
4665 	saved_state->crc_enabled = crtc_state->crc_enabled;
4666 	if (display->platform.g4x ||
4667 	    display->platform.valleyview || display->platform.cherryview)
4668 		saved_state->wm = crtc_state->wm;
4669 
4670 	memcpy(crtc_state, saved_state, sizeof(*crtc_state));
4671 	kfree(saved_state);
4672 
4673 	intel_crtc_copy_uapi_to_hw_state_modeset(state, crtc);
4674 
4675 	return 0;
4676 }
4677 
4678 static int
4679 intel_modeset_pipe_config(struct intel_atomic_state *state,
4680 			  struct intel_crtc *crtc,
4681 			  const struct intel_link_bw_limits *limits)
4682 {
4683 	struct intel_display *display = to_intel_display(crtc);
4684 	struct intel_crtc_state *crtc_state =
4685 		intel_atomic_get_new_crtc_state(state, crtc);
4686 	struct drm_connector *connector;
4687 	struct drm_connector_state *connector_state;
4688 	int pipe_src_w, pipe_src_h;
4689 	int base_bpp, ret, i;
4690 
4691 	crtc_state->cpu_transcoder = (enum transcoder) crtc->pipe;
4692 
4693 	crtc_state->framestart_delay = 1;
4694 
4695 	/*
4696 	 * Sanitize sync polarity flags based on requested ones. If neither
4697 	 * positive or negative polarity is requested, treat this as meaning
4698 	 * negative polarity.
4699 	 */
4700 	if (!(crtc_state->hw.adjusted_mode.flags &
4701 	      (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
4702 		crtc_state->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
4703 
4704 	if (!(crtc_state->hw.adjusted_mode.flags &
4705 	      (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
4706 		crtc_state->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
4707 
4708 	ret = compute_baseline_pipe_bpp(state, crtc);
4709 	if (ret)
4710 		return ret;
4711 
4712 	crtc_state->dsc.compression_enabled_on_link = limits->link_dsc_pipes & BIT(crtc->pipe);
4713 	crtc_state->max_link_bpp_x16 = limits->max_bpp_x16[crtc->pipe];
4714 
4715 	if (crtc_state->pipe_bpp > fxp_q4_to_int(crtc_state->max_link_bpp_x16)) {
4716 		drm_dbg_kms(display->drm,
4717 			    "[CRTC:%d:%s] Link bpp limited to " FXP_Q4_FMT "\n",
4718 			    crtc->base.base.id, crtc->base.name,
4719 			    FXP_Q4_ARGS(crtc_state->max_link_bpp_x16));
4720 		crtc_state->bw_constrained = true;
4721 	}
4722 
4723 	base_bpp = crtc_state->pipe_bpp;
4724 
4725 	/*
4726 	 * Determine the real pipe dimensions. Note that stereo modes can
4727 	 * increase the actual pipe size due to the frame doubling and
4728 	 * insertion of additional space for blanks between the frame. This
4729 	 * is stored in the crtc timings. We use the requested mode to do this
4730 	 * computation to clearly distinguish it from the adjusted mode, which
4731 	 * can be changed by the connectors in the below retry loop.
4732 	 */
4733 	drm_mode_get_hv_timing(&crtc_state->hw.mode,
4734 			       &pipe_src_w, &pipe_src_h);
4735 	drm_rect_init(&crtc_state->pipe_src, 0, 0,
4736 		      pipe_src_w, pipe_src_h);
4737 
4738 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
4739 		struct intel_encoder *encoder =
4740 			to_intel_encoder(connector_state->best_encoder);
4741 
4742 		if (connector_state->crtc != &crtc->base)
4743 			continue;
4744 
4745 		if (!check_single_encoder_cloning(state, crtc, encoder)) {
4746 			drm_dbg_kms(display->drm,
4747 				    "[ENCODER:%d:%s] rejecting invalid cloning configuration\n",
4748 				    encoder->base.base.id, encoder->base.name);
4749 			return -EINVAL;
4750 		}
4751 
4752 		/*
4753 		 * Determine output_types before calling the .compute_config()
4754 		 * hooks so that the hooks can use this information safely.
4755 		 */
4756 		if (encoder->compute_output_type)
4757 			crtc_state->output_types |=
4758 				BIT(encoder->compute_output_type(encoder, crtc_state,
4759 								 connector_state));
4760 		else
4761 			crtc_state->output_types |= BIT(encoder->type);
4762 	}
4763 
4764 	/* Ensure the port clock defaults are reset when retrying. */
4765 	crtc_state->port_clock = 0;
4766 	crtc_state->pixel_multiplier = 1;
4767 
4768 	/* Fill in default crtc timings, allow encoders to overwrite them. */
4769 	drm_mode_set_crtcinfo(&crtc_state->hw.adjusted_mode,
4770 			      CRTC_STEREO_DOUBLE);
4771 
4772 	/* Pass our mode to the connectors and the CRTC to give them a chance to
4773 	 * adjust it according to limitations or connector properties, and also
4774 	 * a chance to reject the mode entirely.
4775 	 */
4776 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
4777 		struct intel_encoder *encoder =
4778 			to_intel_encoder(connector_state->best_encoder);
4779 
4780 		if (connector_state->crtc != &crtc->base)
4781 			continue;
4782 
4783 		ret = encoder->compute_config(encoder, crtc_state,
4784 					      connector_state);
4785 		if (ret == -EDEADLK)
4786 			return ret;
4787 		if (ret < 0) {
4788 			drm_dbg_kms(display->drm, "[ENCODER:%d:%s] config failure: %d\n",
4789 				    encoder->base.base.id, encoder->base.name, ret);
4790 			return ret;
4791 		}
4792 	}
4793 
4794 	/* Set default port clock if not overwritten by the encoder. Needs to be
4795 	 * done afterwards in case the encoder adjusts the mode. */
4796 	if (!crtc_state->port_clock)
4797 		crtc_state->port_clock = crtc_state->hw.adjusted_mode.crtc_clock
4798 			* crtc_state->pixel_multiplier;
4799 
4800 	ret = intel_crtc_compute_config(state, crtc);
4801 	if (ret == -EDEADLK)
4802 		return ret;
4803 	if (ret < 0) {
4804 		drm_dbg_kms(display->drm, "[CRTC:%d:%s] config failure: %d\n",
4805 			    crtc->base.base.id, crtc->base.name, ret);
4806 		return ret;
4807 	}
4808 
4809 	/* Dithering seems to not pass-through bits correctly when it should, so
4810 	 * only enable it on 6bpc panels and when its not a compliance
4811 	 * test requesting 6bpc video pattern.
4812 	 */
4813 	crtc_state->dither = (crtc_state->pipe_bpp == 6*3) &&
4814 		!crtc_state->dither_force_disable;
4815 	drm_dbg_kms(display->drm,
4816 		    "[CRTC:%d:%s] hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
4817 		    crtc->base.base.id, crtc->base.name,
4818 		    base_bpp, crtc_state->pipe_bpp, crtc_state->dither);
4819 
4820 	return 0;
4821 }
4822 
4823 static int
4824 intel_modeset_pipe_config_late(struct intel_atomic_state *state,
4825 			       struct intel_crtc *crtc)
4826 {
4827 	struct intel_crtc_state *crtc_state =
4828 		intel_atomic_get_new_crtc_state(state, crtc);
4829 	struct drm_connector_state *conn_state;
4830 	struct drm_connector *connector;
4831 	int i;
4832 
4833 	for_each_new_connector_in_state(&state->base, connector,
4834 					conn_state, i) {
4835 		struct intel_encoder *encoder =
4836 			to_intel_encoder(conn_state->best_encoder);
4837 		int ret;
4838 
4839 		if (conn_state->crtc != &crtc->base ||
4840 		    !encoder->compute_config_late)
4841 			continue;
4842 
4843 		ret = encoder->compute_config_late(encoder, crtc_state,
4844 						   conn_state);
4845 		if (ret)
4846 			return ret;
4847 	}
4848 
4849 	return 0;
4850 }
4851 
4852 bool intel_fuzzy_clock_check(int clock1, int clock2)
4853 {
4854 	int diff;
4855 
4856 	if (clock1 == clock2)
4857 		return true;
4858 
4859 	if (!clock1 || !clock2)
4860 		return false;
4861 
4862 	diff = abs(clock1 - clock2);
4863 
4864 	if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
4865 		return true;
4866 
4867 	return false;
4868 }
4869 
4870 static bool
4871 intel_compare_link_m_n(const struct intel_link_m_n *m_n,
4872 		       const struct intel_link_m_n *m2_n2)
4873 {
4874 	return m_n->tu == m2_n2->tu &&
4875 		m_n->data_m == m2_n2->data_m &&
4876 		m_n->data_n == m2_n2->data_n &&
4877 		m_n->link_m == m2_n2->link_m &&
4878 		m_n->link_n == m2_n2->link_n;
4879 }
4880 
4881 static bool
4882 intel_compare_infoframe(const union hdmi_infoframe *a,
4883 			const union hdmi_infoframe *b)
4884 {
4885 	return memcmp(a, b, sizeof(*a)) == 0;
4886 }
4887 
4888 static bool
4889 intel_compare_dp_vsc_sdp(const struct drm_dp_vsc_sdp *a,
4890 			 const struct drm_dp_vsc_sdp *b)
4891 {
4892 	return a->pixelformat == b->pixelformat &&
4893 		a->colorimetry == b->colorimetry &&
4894 		a->bpc == b->bpc &&
4895 		a->dynamic_range == b->dynamic_range &&
4896 		a->content_type == b->content_type;
4897 }
4898 
4899 static bool
4900 intel_compare_dp_as_sdp(const struct drm_dp_as_sdp *a,
4901 			const struct drm_dp_as_sdp *b)
4902 {
4903 	return a->vtotal == b->vtotal &&
4904 		a->target_rr == b->target_rr &&
4905 		a->duration_incr_ms == b->duration_incr_ms &&
4906 		a->duration_decr_ms == b->duration_decr_ms &&
4907 		a->mode == b->mode;
4908 }
4909 
4910 static bool
4911 intel_compare_buffer(const u8 *a, const u8 *b, size_t len)
4912 {
4913 	return memcmp(a, b, len) == 0;
4914 }
4915 
4916 static void __printf(5, 6)
4917 pipe_config_mismatch(struct drm_printer *p, bool fastset,
4918 		     const struct intel_crtc *crtc,
4919 		     const char *name, const char *format, ...)
4920 {
4921 	struct va_format vaf;
4922 	va_list args;
4923 
4924 	va_start(args, format);
4925 	vaf.fmt = format;
4926 	vaf.va = &args;
4927 
4928 	if (fastset)
4929 		drm_printf(p, "[CRTC:%d:%s] fastset requirement not met in %s %pV\n",
4930 			   crtc->base.base.id, crtc->base.name, name, &vaf);
4931 	else
4932 		drm_printf(p, "[CRTC:%d:%s] mismatch in %s %pV\n",
4933 			   crtc->base.base.id, crtc->base.name, name, &vaf);
4934 
4935 	va_end(args);
4936 }
4937 
4938 static void
4939 pipe_config_infoframe_mismatch(struct drm_printer *p, bool fastset,
4940 			       const struct intel_crtc *crtc,
4941 			       const char *name,
4942 			       const union hdmi_infoframe *a,
4943 			       const union hdmi_infoframe *b)
4944 {
4945 	struct intel_display *display = to_intel_display(crtc);
4946 	const char *loglevel;
4947 
4948 	if (fastset) {
4949 		if (!drm_debug_enabled(DRM_UT_KMS))
4950 			return;
4951 
4952 		loglevel = KERN_DEBUG;
4953 	} else {
4954 		loglevel = KERN_ERR;
4955 	}
4956 
4957 	pipe_config_mismatch(p, fastset, crtc, name, "infoframe");
4958 
4959 	drm_printf(p, "expected:\n");
4960 	hdmi_infoframe_log(loglevel, display->drm->dev, a);
4961 	drm_printf(p, "found:\n");
4962 	hdmi_infoframe_log(loglevel, display->drm->dev, b);
4963 }
4964 
4965 static void
4966 pipe_config_dp_vsc_sdp_mismatch(struct drm_printer *p, bool fastset,
4967 				const struct intel_crtc *crtc,
4968 				const char *name,
4969 				const struct drm_dp_vsc_sdp *a,
4970 				const struct drm_dp_vsc_sdp *b)
4971 {
4972 	pipe_config_mismatch(p, fastset, crtc, name, "dp vsc sdp");
4973 
4974 	drm_printf(p, "expected:\n");
4975 	drm_dp_vsc_sdp_log(p, a);
4976 	drm_printf(p, "found:\n");
4977 	drm_dp_vsc_sdp_log(p, b);
4978 }
4979 
4980 static void
4981 pipe_config_dp_as_sdp_mismatch(struct drm_printer *p, bool fastset,
4982 			       const struct intel_crtc *crtc,
4983 			       const char *name,
4984 			       const struct drm_dp_as_sdp *a,
4985 			       const struct drm_dp_as_sdp *b)
4986 {
4987 	pipe_config_mismatch(p, fastset, crtc, name, "dp as sdp");
4988 
4989 	drm_printf(p, "expected:\n");
4990 	drm_dp_as_sdp_log(p, a);
4991 	drm_printf(p, "found:\n");
4992 	drm_dp_as_sdp_log(p, b);
4993 }
4994 
4995 /* Returns the length up to and including the last differing byte */
4996 static size_t
4997 memcmp_diff_len(const u8 *a, const u8 *b, size_t len)
4998 {
4999 	int i;
5000 
5001 	for (i = len - 1; i >= 0; i--) {
5002 		if (a[i] != b[i])
5003 			return i + 1;
5004 	}
5005 
5006 	return 0;
5007 }
5008 
5009 static void
5010 pipe_config_buffer_mismatch(struct drm_printer *p, bool fastset,
5011 			    const struct intel_crtc *crtc,
5012 			    const char *name,
5013 			    const u8 *a, const u8 *b, size_t len)
5014 {
5015 	pipe_config_mismatch(p, fastset, crtc, name, "buffer");
5016 
5017 	/* only dump up to the last difference */
5018 	len = memcmp_diff_len(a, b, len);
5019 
5020 	drm_print_hex_dump(p, "expected: ", a, len);
5021 	drm_print_hex_dump(p, "found:    ", b, len);
5022 }
5023 
5024 static void
5025 pipe_config_pll_mismatch(struct drm_printer *p, bool fastset,
5026 			 const struct intel_crtc *crtc,
5027 			 const char *name,
5028 			 const struct intel_dpll_hw_state *a,
5029 			 const struct intel_dpll_hw_state *b)
5030 {
5031 	struct intel_display *display = to_intel_display(crtc);
5032 
5033 	pipe_config_mismatch(p, fastset, crtc, name, " "); /* stupid -Werror=format-zero-length */
5034 
5035 	drm_printf(p, "expected:\n");
5036 	intel_dpll_dump_hw_state(display, p, a);
5037 	drm_printf(p, "found:\n");
5038 	intel_dpll_dump_hw_state(display, p, b);
5039 }
5040 
5041 static bool allow_vblank_delay_fastset(const struct intel_crtc_state *old_crtc_state)
5042 {
5043 	struct intel_display *display = to_intel_display(old_crtc_state);
5044 
5045 	/*
5046 	 * Allow fastboot to fix up vblank delay (handled via LRR
5047 	 * codepaths), a bit dodgy as the registers aren't
5048 	 * double buffered but seems to be working more or less...
5049 	 *
5050 	 * Also allow this when the VRR timing generator is always on,
5051 	 * and optimized guardband is used. In such cases,
5052 	 * vblank delay may vary even without inherited state, but it's
5053 	 * still safe as VRR guardband is still same.
5054 	 */
5055 	return HAS_LRR(display) &&
5056 	       (old_crtc_state->inherited || intel_vrr_always_use_vrr_tg(display)) &&
5057 	       !intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DSI);
5058 }
5059 
5060 static void
5061 pipe_config_lt_phy_pll_mismatch(struct drm_printer *p, bool fastset,
5062 				const struct intel_crtc *crtc,
5063 				const char *name,
5064 				const struct intel_lt_phy_pll_state *a,
5065 				const struct intel_lt_phy_pll_state *b)
5066 {
5067 	struct intel_display *display = to_intel_display(crtc);
5068 	char *chipname = "LTPHY";
5069 
5070 	pipe_config_mismatch(p, fastset, crtc, name, chipname);
5071 
5072 	drm_printf(p, "expected:\n");
5073 	intel_lt_phy_dump_hw_state(display, a);
5074 	drm_printf(p, "found:\n");
5075 	intel_lt_phy_dump_hw_state(display, b);
5076 }
5077 
5078 bool
5079 intel_pipe_config_compare(const struct intel_crtc_state *current_config,
5080 			  const struct intel_crtc_state *pipe_config,
5081 			  bool fastset)
5082 {
5083 	struct intel_display *display = to_intel_display(current_config);
5084 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
5085 	struct drm_printer p;
5086 	u32 exclude_infoframes = 0;
5087 	bool ret = true;
5088 
5089 	if (fastset)
5090 		p = drm_dbg_printer(display->drm, DRM_UT_KMS, NULL);
5091 	else
5092 		p = drm_err_printer(display->drm, NULL);
5093 
5094 #define PIPE_CONF_CHECK_X(name) do { \
5095 	if (current_config->name != pipe_config->name) { \
5096 		BUILD_BUG_ON_MSG(__same_type(current_config->name, bool), \
5097 				 __stringify(name) " is bool");	\
5098 		pipe_config_mismatch(&p, fastset, crtc, __stringify(name), \
5099 				     "(expected 0x%08x, found 0x%08x)", \
5100 				     current_config->name, \
5101 				     pipe_config->name); \
5102 		ret = false; \
5103 	} \
5104 } while (0)
5105 
5106 #define PIPE_CONF_CHECK_X_WITH_MASK(name, mask) do { \
5107 	if ((current_config->name & (mask)) != (pipe_config->name & (mask))) { \
5108 		BUILD_BUG_ON_MSG(__same_type(current_config->name, bool), \
5109 				 __stringify(name) " is bool");	\
5110 		pipe_config_mismatch(&p, fastset, crtc, __stringify(name), \
5111 				     "(expected 0x%08x, found 0x%08x)", \
5112 				     current_config->name & (mask), \
5113 				     pipe_config->name & (mask)); \
5114 		ret = false; \
5115 	} \
5116 } while (0)
5117 
5118 #define PIPE_CONF_CHECK_I(name) do { \
5119 	if (current_config->name != pipe_config->name) { \
5120 		BUILD_BUG_ON_MSG(__same_type(current_config->name, bool), \
5121 				 __stringify(name) " is bool");	\
5122 		pipe_config_mismatch(&p, fastset, crtc, __stringify(name), \
5123 				     "(expected %i, found %i)", \
5124 				     current_config->name, \
5125 				     pipe_config->name); \
5126 		ret = false; \
5127 	} \
5128 } while (0)
5129 
5130 #define PIPE_CONF_CHECK_LLI(name) do { \
5131 	if (current_config->name != pipe_config->name) { \
5132 		pipe_config_mismatch(&p, fastset, crtc, __stringify(name), \
5133 				     "(expected %lli, found %lli)", \
5134 				     current_config->name, \
5135 				     pipe_config->name); \
5136 		ret = false; \
5137 	} \
5138 } while (0)
5139 
5140 #define PIPE_CONF_CHECK_BOOL(name) do { \
5141 	if (current_config->name != pipe_config->name) { \
5142 		BUILD_BUG_ON_MSG(!__same_type(current_config->name, bool), \
5143 				 __stringify(name) " is not bool");	\
5144 		pipe_config_mismatch(&p, fastset, crtc, __stringify(name), \
5145 				     "(expected %s, found %s)", \
5146 				     str_yes_no(current_config->name), \
5147 				     str_yes_no(pipe_config->name)); \
5148 		ret = false; \
5149 	} \
5150 } while (0)
5151 
5152 #define PIPE_CONF_CHECK_P(name) do { \
5153 	if (current_config->name != pipe_config->name) { \
5154 		pipe_config_mismatch(&p, fastset, crtc, __stringify(name), \
5155 				     "(expected %p, found %p)", \
5156 				     current_config->name, \
5157 				     pipe_config->name); \
5158 		ret = false; \
5159 	} \
5160 } while (0)
5161 
5162 #define PIPE_CONF_CHECK_M_N(name) do { \
5163 	if (!intel_compare_link_m_n(&current_config->name, \
5164 				    &pipe_config->name)) { \
5165 		pipe_config_mismatch(&p, fastset, crtc, __stringify(name), \
5166 				     "(expected tu %i data %i/%i link %i/%i, " \
5167 				     "found tu %i, data %i/%i link %i/%i)", \
5168 				     current_config->name.tu, \
5169 				     current_config->name.data_m, \
5170 				     current_config->name.data_n, \
5171 				     current_config->name.link_m, \
5172 				     current_config->name.link_n, \
5173 				     pipe_config->name.tu, \
5174 				     pipe_config->name.data_m, \
5175 				     pipe_config->name.data_n, \
5176 				     pipe_config->name.link_m, \
5177 				     pipe_config->name.link_n); \
5178 		ret = false; \
5179 	} \
5180 } while (0)
5181 
5182 #define PIPE_CONF_CHECK_PLL(name) do { \
5183 	if (!intel_dpll_compare_hw_state(display, &current_config->name, \
5184 					 &pipe_config->name)) { \
5185 		pipe_config_pll_mismatch(&p, fastset, crtc, __stringify(name), \
5186 					 &current_config->name, \
5187 					 &pipe_config->name); \
5188 		ret = false; \
5189 	} \
5190 } while (0)
5191 
5192 #define PIPE_CONF_CHECK_PLL_LT(name) do { \
5193 	if (!intel_lt_phy_pll_compare_hw_state(&current_config->name, \
5194 					       &pipe_config->name)) { \
5195 		pipe_config_lt_phy_pll_mismatch(&p, fastset, crtc, __stringify(name), \
5196 						&current_config->name, \
5197 						&pipe_config->name); \
5198 		ret = false; \
5199 	} \
5200 } while (0)
5201 
5202 #define PIPE_CONF_CHECK_TIMINGS(name) do {     \
5203 	PIPE_CONF_CHECK_I(name.crtc_hdisplay); \
5204 	PIPE_CONF_CHECK_I(name.crtc_htotal); \
5205 	PIPE_CONF_CHECK_I(name.crtc_hblank_start); \
5206 	PIPE_CONF_CHECK_I(name.crtc_hblank_end); \
5207 	PIPE_CONF_CHECK_I(name.crtc_hsync_start); \
5208 	PIPE_CONF_CHECK_I(name.crtc_hsync_end); \
5209 	PIPE_CONF_CHECK_I(name.crtc_vdisplay); \
5210 	if (!fastset || !allow_vblank_delay_fastset(current_config)) \
5211 		PIPE_CONF_CHECK_I(name.crtc_vblank_start); \
5212 	PIPE_CONF_CHECK_I(name.crtc_vsync_start); \
5213 	PIPE_CONF_CHECK_I(name.crtc_vsync_end); \
5214 	if (!fastset || !pipe_config->update_lrr) { \
5215 		PIPE_CONF_CHECK_I(name.crtc_vtotal); \
5216 		PIPE_CONF_CHECK_I(name.crtc_vblank_end); \
5217 	} \
5218 } while (0)
5219 
5220 #define PIPE_CONF_CHECK_RECT(name) do { \
5221 	PIPE_CONF_CHECK_I(name.x1); \
5222 	PIPE_CONF_CHECK_I(name.x2); \
5223 	PIPE_CONF_CHECK_I(name.y1); \
5224 	PIPE_CONF_CHECK_I(name.y2); \
5225 } while (0)
5226 
5227 #define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
5228 	if ((current_config->name ^ pipe_config->name) & (mask)) { \
5229 		pipe_config_mismatch(&p, fastset, crtc, __stringify(name), \
5230 				     "(%x) (expected %i, found %i)", \
5231 				     (mask), \
5232 				     current_config->name & (mask), \
5233 				     pipe_config->name & (mask)); \
5234 		ret = false; \
5235 	} \
5236 } while (0)
5237 
5238 #define PIPE_CONF_CHECK_INFOFRAME(name) do { \
5239 	if (!intel_compare_infoframe(&current_config->infoframes.name, \
5240 				     &pipe_config->infoframes.name)) { \
5241 		pipe_config_infoframe_mismatch(&p, fastset, crtc, __stringify(name), \
5242 					       &current_config->infoframes.name, \
5243 					       &pipe_config->infoframes.name); \
5244 		ret = false; \
5245 	} \
5246 } while (0)
5247 
5248 #define PIPE_CONF_CHECK_DP_VSC_SDP(name) do { \
5249 	if (!intel_compare_dp_vsc_sdp(&current_config->infoframes.name, \
5250 				      &pipe_config->infoframes.name)) { \
5251 		pipe_config_dp_vsc_sdp_mismatch(&p, fastset, crtc, __stringify(name), \
5252 						&current_config->infoframes.name, \
5253 						&pipe_config->infoframes.name); \
5254 		ret = false; \
5255 	} \
5256 } while (0)
5257 
5258 #define PIPE_CONF_CHECK_DP_AS_SDP(name) do { \
5259 	if (!intel_compare_dp_as_sdp(&current_config->infoframes.name, \
5260 				      &pipe_config->infoframes.name)) { \
5261 		pipe_config_dp_as_sdp_mismatch(&p, fastset, crtc, __stringify(name), \
5262 						&current_config->infoframes.name, \
5263 						&pipe_config->infoframes.name); \
5264 		ret = false; \
5265 	} \
5266 } while (0)
5267 
5268 #define PIPE_CONF_CHECK_BUFFER(name, len) do { \
5269 	BUILD_BUG_ON(sizeof(current_config->name) != (len)); \
5270 	BUILD_BUG_ON(sizeof(pipe_config->name) != (len)); \
5271 	if (!intel_compare_buffer(current_config->name, pipe_config->name, (len))) { \
5272 		pipe_config_buffer_mismatch(&p, fastset, crtc, __stringify(name), \
5273 					    current_config->name, \
5274 					    pipe_config->name, \
5275 					    (len)); \
5276 		ret = false; \
5277 	} \
5278 } while (0)
5279 
5280 #define PIPE_CONF_CHECK_COLOR_LUT(lut, is_pre_csc_lut) do { \
5281 	if (current_config->gamma_mode == pipe_config->gamma_mode && \
5282 	    !intel_color_lut_equal(current_config, \
5283 				   current_config->lut, pipe_config->lut, \
5284 				   is_pre_csc_lut)) {	\
5285 		pipe_config_mismatch(&p, fastset, crtc, __stringify(lut), \
5286 				     "hw_state doesn't match sw_state"); \
5287 		ret = false; \
5288 	} \
5289 } while (0)
5290 
5291 #define PIPE_CONF_CHECK_CSC(name) do { \
5292 	PIPE_CONF_CHECK_X(name.preoff[0]); \
5293 	PIPE_CONF_CHECK_X(name.preoff[1]); \
5294 	PIPE_CONF_CHECK_X(name.preoff[2]); \
5295 	PIPE_CONF_CHECK_X(name.coeff[0]); \
5296 	PIPE_CONF_CHECK_X(name.coeff[1]); \
5297 	PIPE_CONF_CHECK_X(name.coeff[2]); \
5298 	PIPE_CONF_CHECK_X(name.coeff[3]); \
5299 	PIPE_CONF_CHECK_X(name.coeff[4]); \
5300 	PIPE_CONF_CHECK_X(name.coeff[5]); \
5301 	PIPE_CONF_CHECK_X(name.coeff[6]); \
5302 	PIPE_CONF_CHECK_X(name.coeff[7]); \
5303 	PIPE_CONF_CHECK_X(name.coeff[8]); \
5304 	PIPE_CONF_CHECK_X(name.postoff[0]); \
5305 	PIPE_CONF_CHECK_X(name.postoff[1]); \
5306 	PIPE_CONF_CHECK_X(name.postoff[2]); \
5307 } while (0)
5308 
5309 #define PIPE_CONF_QUIRK(quirk) \
5310 	((current_config->quirks | pipe_config->quirks) & (quirk))
5311 
5312 	PIPE_CONF_CHECK_BOOL(hw.enable);
5313 	PIPE_CONF_CHECK_BOOL(hw.active);
5314 
5315 	PIPE_CONF_CHECK_I(cpu_transcoder);
5316 	PIPE_CONF_CHECK_I(mst_master_transcoder);
5317 
5318 	PIPE_CONF_CHECK_BOOL(has_pch_encoder);
5319 	PIPE_CONF_CHECK_I(fdi_lanes);
5320 	PIPE_CONF_CHECK_M_N(fdi_m_n);
5321 
5322 	PIPE_CONF_CHECK_I(lane_count);
5323 	PIPE_CONF_CHECK_X(lane_lat_optim_mask);
5324 
5325 	PIPE_CONF_CHECK_I(min_hblank);
5326 
5327 	if (HAS_DOUBLE_BUFFERED_M_N(display)) {
5328 		if (!fastset || !pipe_config->update_m_n)
5329 			PIPE_CONF_CHECK_M_N(dp_m_n);
5330 	} else {
5331 		PIPE_CONF_CHECK_M_N(dp_m_n);
5332 		PIPE_CONF_CHECK_M_N(dp_m2_n2);
5333 	}
5334 
5335 	PIPE_CONF_CHECK_X(output_types);
5336 
5337 	PIPE_CONF_CHECK_I(framestart_delay);
5338 	PIPE_CONF_CHECK_I(msa_timing_delay);
5339 
5340 	PIPE_CONF_CHECK_TIMINGS(hw.pipe_mode);
5341 	PIPE_CONF_CHECK_TIMINGS(hw.adjusted_mode);
5342 
5343 	PIPE_CONF_CHECK_I(pixel_multiplier);
5344 
5345 	PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
5346 			      DRM_MODE_FLAG_INTERLACE);
5347 
5348 	if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
5349 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
5350 				      DRM_MODE_FLAG_PHSYNC);
5351 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
5352 				      DRM_MODE_FLAG_NHSYNC);
5353 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
5354 				      DRM_MODE_FLAG_PVSYNC);
5355 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
5356 				      DRM_MODE_FLAG_NVSYNC);
5357 	}
5358 
5359 	PIPE_CONF_CHECK_I(output_format);
5360 	PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
5361 	if ((DISPLAY_VER(display) < 8 && !display->platform.haswell) ||
5362 	    display->platform.valleyview || display->platform.cherryview)
5363 		PIPE_CONF_CHECK_BOOL(limited_color_range);
5364 
5365 	PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
5366 	PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
5367 	PIPE_CONF_CHECK_BOOL(has_infoframe);
5368 	PIPE_CONF_CHECK_BOOL(enhanced_framing);
5369 	PIPE_CONF_CHECK_BOOL(fec_enable);
5370 
5371 	if (!fastset) {
5372 		PIPE_CONF_CHECK_BOOL(has_audio);
5373 		PIPE_CONF_CHECK_BUFFER(eld, MAX_ELD_BYTES);
5374 	}
5375 
5376 	PIPE_CONF_CHECK_X(gmch_pfit.control);
5377 	/* pfit ratios are autocomputed by the hw on gen4+ */
5378 	if (DISPLAY_VER(display) < 4)
5379 		PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
5380 	PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
5381 
5382 	/*
5383 	 * Changing the EDP transcoder input mux
5384 	 * (A_ONOFF vs. A_ON) requires a full modeset.
5385 	 */
5386 	PIPE_CONF_CHECK_BOOL(pch_pfit.force_thru);
5387 
5388 	if (!fastset) {
5389 		PIPE_CONF_CHECK_RECT(pipe_src);
5390 
5391 		PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
5392 		PIPE_CONF_CHECK_RECT(pch_pfit.dst);
5393 
5394 		PIPE_CONF_CHECK_I(scaler_state.scaler_id);
5395 		PIPE_CONF_CHECK_I(pixel_rate);
5396 		PIPE_CONF_CHECK_BOOL(hw.casf_params.casf_enable);
5397 		PIPE_CONF_CHECK_I(hw.casf_params.win_size);
5398 		PIPE_CONF_CHECK_I(hw.casf_params.strength);
5399 
5400 		PIPE_CONF_CHECK_X(gamma_mode);
5401 		if (display->platform.cherryview)
5402 			PIPE_CONF_CHECK_X(cgm_mode);
5403 		else
5404 			PIPE_CONF_CHECK_X(csc_mode);
5405 		PIPE_CONF_CHECK_BOOL(gamma_enable);
5406 		PIPE_CONF_CHECK_BOOL(csc_enable);
5407 		PIPE_CONF_CHECK_BOOL(wgc_enable);
5408 
5409 		PIPE_CONF_CHECK_I(linetime);
5410 		PIPE_CONF_CHECK_I(ips_linetime);
5411 
5412 		PIPE_CONF_CHECK_COLOR_LUT(pre_csc_lut, true);
5413 		PIPE_CONF_CHECK_COLOR_LUT(post_csc_lut, false);
5414 
5415 		PIPE_CONF_CHECK_CSC(csc);
5416 		PIPE_CONF_CHECK_CSC(output_csc);
5417 	}
5418 
5419 	PIPE_CONF_CHECK_BOOL(double_wide);
5420 
5421 	if (display->dpll.mgr)
5422 		PIPE_CONF_CHECK_P(intel_dpll);
5423 
5424 	/* FIXME convert everything over the dpll_mgr */
5425 	if (display->dpll.mgr || HAS_GMCH(display))
5426 		PIPE_CONF_CHECK_PLL(dpll_hw_state);
5427 
5428 	/* FIXME convert MTL+ platforms over to dpll_mgr */
5429 	if (HAS_LT_PHY(display))
5430 		PIPE_CONF_CHECK_PLL_LT(dpll_hw_state.ltpll);
5431 
5432 	PIPE_CONF_CHECK_X(dsi_pll.ctrl);
5433 	PIPE_CONF_CHECK_X(dsi_pll.div);
5434 
5435 	if (display->platform.g4x || DISPLAY_VER(display) >= 5)
5436 		PIPE_CONF_CHECK_I(pipe_bpp);
5437 
5438 	if (!fastset || !pipe_config->update_m_n) {
5439 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_clock);
5440 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_clock);
5441 	}
5442 	PIPE_CONF_CHECK_I(port_clock);
5443 
5444 	PIPE_CONF_CHECK_I(min_voltage_level);
5445 
5446 	if (current_config->has_psr || pipe_config->has_psr)
5447 		exclude_infoframes |= intel_hdmi_infoframe_enable(DP_SDP_VSC);
5448 
5449 	if (current_config->vrr.enable || pipe_config->vrr.enable)
5450 		exclude_infoframes |= intel_hdmi_infoframe_enable(DP_SDP_ADAPTIVE_SYNC);
5451 
5452 	PIPE_CONF_CHECK_X_WITH_MASK(infoframes.enable, ~exclude_infoframes);
5453 	PIPE_CONF_CHECK_X(infoframes.gcp);
5454 	PIPE_CONF_CHECK_INFOFRAME(avi);
5455 	PIPE_CONF_CHECK_INFOFRAME(spd);
5456 	PIPE_CONF_CHECK_INFOFRAME(hdmi);
5457 	if (!fastset) {
5458 		PIPE_CONF_CHECK_INFOFRAME(drm);
5459 		PIPE_CONF_CHECK_DP_AS_SDP(as_sdp);
5460 	}
5461 	PIPE_CONF_CHECK_DP_VSC_SDP(vsc);
5462 
5463 	PIPE_CONF_CHECK_X(sync_mode_slaves_mask);
5464 	PIPE_CONF_CHECK_I(master_transcoder);
5465 	PIPE_CONF_CHECK_X(joiner_pipes);
5466 
5467 	PIPE_CONF_CHECK_BOOL(dsc.config.block_pred_enable);
5468 	PIPE_CONF_CHECK_BOOL(dsc.config.convert_rgb);
5469 	PIPE_CONF_CHECK_BOOL(dsc.config.simple_422);
5470 	PIPE_CONF_CHECK_BOOL(dsc.config.native_422);
5471 	PIPE_CONF_CHECK_BOOL(dsc.config.native_420);
5472 	PIPE_CONF_CHECK_BOOL(dsc.config.vbr_enable);
5473 	PIPE_CONF_CHECK_I(dsc.config.line_buf_depth);
5474 	PIPE_CONF_CHECK_I(dsc.config.bits_per_component);
5475 	PIPE_CONF_CHECK_I(dsc.config.pic_width);
5476 	PIPE_CONF_CHECK_I(dsc.config.pic_height);
5477 	PIPE_CONF_CHECK_I(dsc.config.slice_width);
5478 	PIPE_CONF_CHECK_I(dsc.config.slice_height);
5479 	PIPE_CONF_CHECK_I(dsc.config.initial_dec_delay);
5480 	PIPE_CONF_CHECK_I(dsc.config.initial_xmit_delay);
5481 	PIPE_CONF_CHECK_I(dsc.config.scale_decrement_interval);
5482 	PIPE_CONF_CHECK_I(dsc.config.scale_increment_interval);
5483 	PIPE_CONF_CHECK_I(dsc.config.initial_scale_value);
5484 	PIPE_CONF_CHECK_I(dsc.config.first_line_bpg_offset);
5485 	PIPE_CONF_CHECK_I(dsc.config.flatness_min_qp);
5486 	PIPE_CONF_CHECK_I(dsc.config.flatness_max_qp);
5487 	PIPE_CONF_CHECK_I(dsc.config.slice_bpg_offset);
5488 	PIPE_CONF_CHECK_I(dsc.config.nfl_bpg_offset);
5489 	PIPE_CONF_CHECK_I(dsc.config.initial_offset);
5490 	PIPE_CONF_CHECK_I(dsc.config.final_offset);
5491 	PIPE_CONF_CHECK_I(dsc.config.rc_model_size);
5492 	PIPE_CONF_CHECK_I(dsc.config.rc_quant_incr_limit0);
5493 	PIPE_CONF_CHECK_I(dsc.config.rc_quant_incr_limit1);
5494 	PIPE_CONF_CHECK_I(dsc.config.slice_chunk_size);
5495 	PIPE_CONF_CHECK_I(dsc.config.second_line_bpg_offset);
5496 	PIPE_CONF_CHECK_I(dsc.config.nsl_bpg_offset);
5497 
5498 	PIPE_CONF_CHECK_BOOL(dsc.compression_enable);
5499 	PIPE_CONF_CHECK_I(dsc.slice_config.streams_per_pipe);
5500 	PIPE_CONF_CHECK_I(dsc.compressed_bpp_x16);
5501 
5502 	PIPE_CONF_CHECK_BOOL(splitter.enable);
5503 	PIPE_CONF_CHECK_I(splitter.link_count);
5504 	PIPE_CONF_CHECK_I(splitter.pixel_overlap);
5505 
5506 	if (!fastset) {
5507 		PIPE_CONF_CHECK_BOOL(vrr.enable);
5508 		PIPE_CONF_CHECK_I(vrr.vmin);
5509 		PIPE_CONF_CHECK_I(vrr.vmax);
5510 		PIPE_CONF_CHECK_I(vrr.flipline);
5511 		PIPE_CONF_CHECK_I(vrr.vsync_start);
5512 		PIPE_CONF_CHECK_I(vrr.vsync_end);
5513 		PIPE_CONF_CHECK_LLI(cmrr.cmrr_m);
5514 		PIPE_CONF_CHECK_LLI(cmrr.cmrr_n);
5515 		PIPE_CONF_CHECK_BOOL(cmrr.enable);
5516 		PIPE_CONF_CHECK_I(vrr.dc_balance.vmin);
5517 		PIPE_CONF_CHECK_I(vrr.dc_balance.vmax);
5518 		PIPE_CONF_CHECK_I(vrr.dc_balance.guardband);
5519 		PIPE_CONF_CHECK_I(vrr.dc_balance.slope);
5520 		PIPE_CONF_CHECK_I(vrr.dc_balance.max_increase);
5521 		PIPE_CONF_CHECK_I(vrr.dc_balance.max_decrease);
5522 		PIPE_CONF_CHECK_I(vrr.dc_balance.vblank_target);
5523 	}
5524 
5525 	if (!fastset || intel_vrr_always_use_vrr_tg(display)) {
5526 		PIPE_CONF_CHECK_I(vrr.pipeline_full);
5527 		PIPE_CONF_CHECK_I(vrr.guardband);
5528 	}
5529 
5530 	PIPE_CONF_CHECK_I(set_context_latency);
5531 
5532 #undef PIPE_CONF_CHECK_X
5533 #undef PIPE_CONF_CHECK_I
5534 #undef PIPE_CONF_CHECK_LLI
5535 #undef PIPE_CONF_CHECK_BOOL
5536 #undef PIPE_CONF_CHECK_P
5537 #undef PIPE_CONF_CHECK_FLAGS
5538 #undef PIPE_CONF_CHECK_COLOR_LUT
5539 #undef PIPE_CONF_CHECK_TIMINGS
5540 #undef PIPE_CONF_CHECK_RECT
5541 #undef PIPE_CONF_QUIRK
5542 
5543 	return ret;
5544 }
5545 
5546 static void
5547 intel_verify_planes(struct intel_atomic_state *state)
5548 {
5549 	struct intel_plane *plane;
5550 	const struct intel_plane_state *plane_state;
5551 	int i;
5552 
5553 	for_each_new_intel_plane_in_state(state, plane,
5554 					  plane_state, i)
5555 		assert_plane(plane, plane_state->is_y_plane ||
5556 			     plane_state->uapi.visible);
5557 }
5558 
5559 static int intel_modeset_pipe(struct intel_atomic_state *state,
5560 			      struct intel_crtc_state *crtc_state,
5561 			      const char *reason)
5562 {
5563 	struct intel_display *display = to_intel_display(state);
5564 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5565 	int ret;
5566 
5567 	drm_dbg_kms(display->drm, "[CRTC:%d:%s] Full modeset due to %s\n",
5568 		    crtc->base.base.id, crtc->base.name, reason);
5569 
5570 	ret = drm_atomic_add_affected_connectors(&state->base,
5571 						 &crtc->base);
5572 	if (ret)
5573 		return ret;
5574 
5575 	ret = intel_dp_tunnel_atomic_add_state_for_crtc(state, crtc);
5576 	if (ret)
5577 		return ret;
5578 
5579 	ret = intel_dp_mst_add_topology_state_for_crtc(state, crtc);
5580 	if (ret)
5581 		return ret;
5582 
5583 	ret = intel_plane_add_affected(state, crtc);
5584 	if (ret)
5585 		return ret;
5586 
5587 	crtc_state->uapi.mode_changed = true;
5588 
5589 	return 0;
5590 }
5591 
5592 /**
5593  * intel_modeset_pipes_in_mask_early - force a full modeset on a set of pipes
5594  * @state: intel atomic state
5595  * @reason: the reason for the full modeset
5596  * @mask: mask of pipes to modeset
5597  *
5598  * Add pipes in @mask to @state and force a full modeset on the enabled ones
5599  * due to the description in @reason.
5600  * This function can be called only before new plane states are computed.
5601  *
5602  * Returns 0 in case of success, negative error code otherwise.
5603  */
5604 int intel_modeset_pipes_in_mask_early(struct intel_atomic_state *state,
5605 				      const char *reason, u8 mask)
5606 {
5607 	struct intel_display *display = to_intel_display(state);
5608 	struct intel_crtc *crtc;
5609 
5610 	for_each_intel_crtc_in_pipe_mask(display->drm, crtc, mask) {
5611 		struct intel_crtc_state *crtc_state;
5612 		int ret;
5613 
5614 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
5615 		if (IS_ERR(crtc_state))
5616 			return PTR_ERR(crtc_state);
5617 
5618 		if (!crtc_state->hw.enable ||
5619 		    intel_crtc_needs_modeset(crtc_state))
5620 			continue;
5621 
5622 		ret = intel_modeset_pipe(state, crtc_state, reason);
5623 		if (ret)
5624 			return ret;
5625 	}
5626 
5627 	return 0;
5628 }
5629 
5630 static void
5631 intel_crtc_flag_modeset(struct intel_crtc_state *crtc_state)
5632 {
5633 	crtc_state->uapi.mode_changed = true;
5634 
5635 	crtc_state->update_pipe = false;
5636 	crtc_state->update_m_n = false;
5637 	crtc_state->update_lrr = false;
5638 }
5639 
5640 /**
5641  * intel_modeset_all_pipes_late - force a full modeset on all pipes
5642  * @state: intel atomic state
5643  * @reason: the reason for the full modeset
5644  *
5645  * Add all pipes to @state and force a full modeset on the active ones due to
5646  * the description in @reason.
5647  * This function can be called only after new plane states are computed already.
5648  *
5649  * Returns 0 in case of success, negative error code otherwise.
5650  */
5651 int intel_modeset_all_pipes_late(struct intel_atomic_state *state,
5652 				 const char *reason)
5653 {
5654 	struct intel_display *display = to_intel_display(state);
5655 	struct intel_crtc *crtc;
5656 
5657 	for_each_intel_crtc(display->drm, crtc) {
5658 		struct intel_crtc_state *crtc_state;
5659 		int ret;
5660 
5661 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
5662 		if (IS_ERR(crtc_state))
5663 			return PTR_ERR(crtc_state);
5664 
5665 		if (!crtc_state->hw.active ||
5666 		    intel_crtc_needs_modeset(crtc_state))
5667 			continue;
5668 
5669 		ret = intel_modeset_pipe(state, crtc_state, reason);
5670 		if (ret)
5671 			return ret;
5672 
5673 		intel_crtc_flag_modeset(crtc_state);
5674 
5675 		crtc_state->update_planes |= crtc_state->active_planes;
5676 		crtc_state->async_flip_planes = 0;
5677 		crtc_state->do_async_flip = false;
5678 	}
5679 
5680 	return 0;
5681 }
5682 
5683 int intel_modeset_commit_pipes(struct intel_display *display,
5684 			       u8 pipe_mask,
5685 			       struct drm_modeset_acquire_ctx *ctx)
5686 {
5687 	struct drm_atomic_state *state;
5688 	struct intel_crtc *crtc;
5689 	int ret;
5690 
5691 	state = drm_atomic_state_alloc(display->drm);
5692 	if (!state)
5693 		return -ENOMEM;
5694 
5695 	state->acquire_ctx = ctx;
5696 	to_intel_atomic_state(state)->internal = true;
5697 
5698 	for_each_intel_crtc_in_pipe_mask(display->drm, crtc, pipe_mask) {
5699 		struct intel_crtc_state *crtc_state =
5700 			intel_atomic_get_crtc_state(state, crtc);
5701 
5702 		if (IS_ERR(crtc_state)) {
5703 			ret = PTR_ERR(crtc_state);
5704 			goto out;
5705 		}
5706 
5707 		crtc_state->uapi.connectors_changed = true;
5708 	}
5709 
5710 	ret = drm_atomic_commit(state);
5711 out:
5712 	drm_atomic_state_put(state);
5713 
5714 	return ret;
5715 }
5716 
5717 /*
5718  * This implements the workaround described in the "notes" section of the mode
5719  * set sequence documentation. When going from no pipes or single pipe to
5720  * multiple pipes, and planes are enabled after the pipe, we need to wait at
5721  * least 2 vblanks on the first pipe before enabling planes on the second pipe.
5722  */
5723 static int hsw_mode_set_planes_workaround(struct intel_atomic_state *state)
5724 {
5725 	struct intel_crtc_state *crtc_state;
5726 	struct intel_crtc *crtc;
5727 	struct intel_crtc_state *first_crtc_state = NULL;
5728 	struct intel_crtc_state *other_crtc_state = NULL;
5729 	enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
5730 	int i;
5731 
5732 	/* look at all crtc's that are going to be enabled in during modeset */
5733 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
5734 		if (!crtc_state->hw.active ||
5735 		    !intel_crtc_needs_modeset(crtc_state))
5736 			continue;
5737 
5738 		if (first_crtc_state) {
5739 			other_crtc_state = crtc_state;
5740 			break;
5741 		} else {
5742 			first_crtc_state = crtc_state;
5743 			first_pipe = crtc->pipe;
5744 		}
5745 	}
5746 
5747 	/* No workaround needed? */
5748 	if (!first_crtc_state)
5749 		return 0;
5750 
5751 	/* w/a possibly needed, check how many crtc's are already enabled. */
5752 	for_each_intel_crtc(state->base.dev, crtc) {
5753 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
5754 		if (IS_ERR(crtc_state))
5755 			return PTR_ERR(crtc_state);
5756 
5757 		crtc_state->hsw_workaround_pipe = INVALID_PIPE;
5758 
5759 		if (!crtc_state->hw.active ||
5760 		    intel_crtc_needs_modeset(crtc_state))
5761 			continue;
5762 
5763 		/* 2 or more enabled crtcs means no need for w/a */
5764 		if (enabled_pipe != INVALID_PIPE)
5765 			return 0;
5766 
5767 		enabled_pipe = crtc->pipe;
5768 	}
5769 
5770 	if (enabled_pipe != INVALID_PIPE)
5771 		first_crtc_state->hsw_workaround_pipe = enabled_pipe;
5772 	else if (other_crtc_state)
5773 		other_crtc_state->hsw_workaround_pipe = first_pipe;
5774 
5775 	return 0;
5776 }
5777 
5778 u8 intel_calc_enabled_pipes(struct intel_atomic_state *state,
5779 			    u8 enabled_pipes)
5780 {
5781 	const struct intel_crtc_state *crtc_state;
5782 	struct intel_crtc *crtc;
5783 	int i;
5784 
5785 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
5786 		if (crtc_state->hw.enable)
5787 			enabled_pipes |= BIT(crtc->pipe);
5788 		else
5789 			enabled_pipes &= ~BIT(crtc->pipe);
5790 	}
5791 
5792 	return enabled_pipes;
5793 }
5794 
5795 u8 intel_calc_active_pipes(struct intel_atomic_state *state,
5796 			   u8 active_pipes)
5797 {
5798 	const struct intel_crtc_state *crtc_state;
5799 	struct intel_crtc *crtc;
5800 	int i;
5801 
5802 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
5803 		if (crtc_state->hw.active)
5804 			active_pipes |= BIT(crtc->pipe);
5805 		else
5806 			active_pipes &= ~BIT(crtc->pipe);
5807 	}
5808 
5809 	return active_pipes;
5810 }
5811 
5812 static int intel_modeset_checks(struct intel_atomic_state *state)
5813 {
5814 	struct intel_display *display = to_intel_display(state);
5815 
5816 	state->modeset = true;
5817 
5818 	if (display->platform.haswell)
5819 		return hsw_mode_set_planes_workaround(state);
5820 
5821 	return 0;
5822 }
5823 
5824 static bool lrr_params_changed(const struct intel_crtc_state *old_crtc_state,
5825 			       const struct intel_crtc_state *new_crtc_state)
5826 {
5827 	const struct drm_display_mode *old_adjusted_mode = &old_crtc_state->hw.adjusted_mode;
5828 	const struct drm_display_mode *new_adjusted_mode = &new_crtc_state->hw.adjusted_mode;
5829 
5830 	return old_adjusted_mode->crtc_vblank_start != new_adjusted_mode->crtc_vblank_start ||
5831 		old_adjusted_mode->crtc_vblank_end != new_adjusted_mode->crtc_vblank_end ||
5832 		old_adjusted_mode->crtc_vtotal != new_adjusted_mode->crtc_vtotal ||
5833 		old_crtc_state->set_context_latency != new_crtc_state->set_context_latency;
5834 }
5835 
5836 static void intel_crtc_check_fastset(const struct intel_crtc_state *old_crtc_state,
5837 				     struct intel_crtc_state *new_crtc_state)
5838 {
5839 	struct intel_display *display = to_intel_display(new_crtc_state);
5840 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
5841 
5842 	/* only allow LRR when the timings stay within the VRR range */
5843 	if (old_crtc_state->vrr.in_range != new_crtc_state->vrr.in_range)
5844 		new_crtc_state->update_lrr = false;
5845 
5846 	if (!intel_pipe_config_compare(old_crtc_state, new_crtc_state, true)) {
5847 		drm_dbg_kms(display->drm, "[CRTC:%d:%s] fastset requirement not met, forcing full modeset\n",
5848 			    crtc->base.base.id, crtc->base.name);
5849 	} else {
5850 		if (allow_vblank_delay_fastset(old_crtc_state))
5851 			new_crtc_state->update_lrr = true;
5852 		new_crtc_state->uapi.mode_changed = false;
5853 	}
5854 
5855 	if (intel_compare_link_m_n(&old_crtc_state->dp_m_n,
5856 				   &new_crtc_state->dp_m_n))
5857 		new_crtc_state->update_m_n = false;
5858 
5859 	if (!lrr_params_changed(old_crtc_state, new_crtc_state))
5860 		new_crtc_state->update_lrr = false;
5861 
5862 	if (intel_crtc_needs_modeset(new_crtc_state))
5863 		intel_crtc_flag_modeset(new_crtc_state);
5864 	else
5865 		new_crtc_state->update_pipe = true;
5866 }
5867 
5868 static int intel_atomic_check_crtcs(struct intel_atomic_state *state)
5869 {
5870 	struct intel_display *display = to_intel_display(state);
5871 	struct intel_crtc_state __maybe_unused *crtc_state;
5872 	struct intel_crtc *crtc;
5873 	int i;
5874 
5875 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
5876 		int ret;
5877 
5878 		ret = intel_crtc_atomic_check(state, crtc);
5879 		if (ret) {
5880 			drm_dbg_atomic(display->drm,
5881 				       "[CRTC:%d:%s] atomic driver check failed\n",
5882 				       crtc->base.base.id, crtc->base.name);
5883 			return ret;
5884 		}
5885 	}
5886 
5887 	return 0;
5888 }
5889 
5890 static bool intel_cpu_transcoders_need_modeset(struct intel_atomic_state *state,
5891 					       u8 transcoders)
5892 {
5893 	const struct intel_crtc_state *new_crtc_state;
5894 	struct intel_crtc *crtc;
5895 	int i;
5896 
5897 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
5898 		if (new_crtc_state->hw.enable &&
5899 		    transcoders & BIT(new_crtc_state->cpu_transcoder) &&
5900 		    intel_crtc_needs_modeset(new_crtc_state))
5901 			return true;
5902 	}
5903 
5904 	return false;
5905 }
5906 
5907 static bool intel_pipes_need_modeset(struct intel_atomic_state *state,
5908 				     u8 pipes)
5909 {
5910 	const struct intel_crtc_state *new_crtc_state;
5911 	struct intel_crtc *crtc;
5912 	int i;
5913 
5914 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
5915 		if (new_crtc_state->hw.enable &&
5916 		    pipes & BIT(crtc->pipe) &&
5917 		    intel_crtc_needs_modeset(new_crtc_state))
5918 			return true;
5919 	}
5920 
5921 	return false;
5922 }
5923 
5924 static int intel_atomic_check_joiner(struct intel_atomic_state *state,
5925 				     struct intel_crtc *primary_crtc)
5926 {
5927 	struct intel_display *display = to_intel_display(state);
5928 	struct intel_crtc_state *primary_crtc_state =
5929 		intel_atomic_get_new_crtc_state(state, primary_crtc);
5930 	struct intel_crtc *secondary_crtc;
5931 
5932 	if (!primary_crtc_state->joiner_pipes)
5933 		return 0;
5934 
5935 	/* sanity check */
5936 	if (drm_WARN_ON(display->drm,
5937 			primary_crtc->pipe != joiner_primary_pipe(primary_crtc_state)))
5938 		return -EINVAL;
5939 
5940 	if (primary_crtc_state->joiner_pipes & ~joiner_pipes(display)) {
5941 		drm_dbg_kms(display->drm,
5942 			    "[CRTC:%d:%s] Cannot act as joiner primary "
5943 			    "(need 0x%x as pipes, only 0x%x possible)\n",
5944 			    primary_crtc->base.base.id, primary_crtc->base.name,
5945 			    primary_crtc_state->joiner_pipes, joiner_pipes(display));
5946 		return -EINVAL;
5947 	}
5948 
5949 	for_each_intel_crtc_in_pipe_mask(display->drm, secondary_crtc,
5950 					 intel_crtc_joiner_secondary_pipes(primary_crtc_state)) {
5951 		struct intel_crtc_state *secondary_crtc_state;
5952 		int ret;
5953 
5954 		secondary_crtc_state = intel_atomic_get_crtc_state(&state->base, secondary_crtc);
5955 		if (IS_ERR(secondary_crtc_state))
5956 			return PTR_ERR(secondary_crtc_state);
5957 
5958 		/* primary being enabled, secondary was already configured? */
5959 		if (secondary_crtc_state->uapi.enable) {
5960 			drm_dbg_kms(display->drm,
5961 				    "[CRTC:%d:%s] secondary is enabled as normal CRTC, but "
5962 				    "[CRTC:%d:%s] claiming this CRTC for joiner.\n",
5963 				    secondary_crtc->base.base.id, secondary_crtc->base.name,
5964 				    primary_crtc->base.base.id, primary_crtc->base.name);
5965 			return -EINVAL;
5966 		}
5967 
5968 		/*
5969 		 * The state copy logic assumes the primary crtc gets processed
5970 		 * before the secondary crtc during the main compute_config loop.
5971 		 * This works because the crtcs are created in pipe order,
5972 		 * and the hardware requires primary pipe < secondary pipe as well.
5973 		 * Should that change we need to rethink the logic.
5974 		 */
5975 		if (WARN_ON(drm_crtc_index(&primary_crtc->base) >
5976 			    drm_crtc_index(&secondary_crtc->base)))
5977 			return -EINVAL;
5978 
5979 		drm_dbg_kms(display->drm,
5980 			    "[CRTC:%d:%s] Used as secondary for joiner primary [CRTC:%d:%s]\n",
5981 			    secondary_crtc->base.base.id, secondary_crtc->base.name,
5982 			    primary_crtc->base.base.id, primary_crtc->base.name);
5983 
5984 		secondary_crtc_state->joiner_pipes =
5985 			primary_crtc_state->joiner_pipes;
5986 
5987 		ret = copy_joiner_crtc_state_modeset(state, secondary_crtc);
5988 		if (ret)
5989 			return ret;
5990 	}
5991 
5992 	return 0;
5993 }
5994 
5995 static void kill_joiner_secondaries(struct intel_atomic_state *state,
5996 				    struct intel_crtc *primary_crtc)
5997 {
5998 	struct intel_display *display = to_intel_display(state);
5999 	struct intel_crtc_state *primary_crtc_state =
6000 		intel_atomic_get_new_crtc_state(state, primary_crtc);
6001 	struct intel_crtc *secondary_crtc;
6002 
6003 	for_each_intel_crtc_in_pipe_mask(display->drm, secondary_crtc,
6004 					 intel_crtc_joiner_secondary_pipes(primary_crtc_state)) {
6005 		struct intel_crtc_state *secondary_crtc_state =
6006 			intel_atomic_get_new_crtc_state(state, secondary_crtc);
6007 
6008 		secondary_crtc_state->joiner_pipes = 0;
6009 
6010 		intel_crtc_copy_uapi_to_hw_state_modeset(state, secondary_crtc);
6011 	}
6012 
6013 	primary_crtc_state->joiner_pipes = 0;
6014 }
6015 
6016 /**
6017  * DOC: asynchronous flip implementation
6018  *
6019  * Asynchronous page flip is the implementation for the DRM_MODE_PAGE_FLIP_ASYNC
6020  * flag. Currently async flip is only supported via the drmModePageFlip IOCTL.
6021  * Correspondingly, support is currently added for primary plane only.
6022  *
6023  * Async flip can only change the plane surface address, so anything else
6024  * changing is rejected from the intel_async_flip_check_hw() function.
6025  * Once this check is cleared, flip done interrupt is enabled using
6026  * the intel_crtc_enable_flip_done() function.
6027  *
6028  * As soon as the surface address register is written, flip done interrupt is
6029  * generated and the requested events are sent to the userspace in the interrupt
6030  * handler itself. The timestamp and sequence sent during the flip done event
6031  * correspond to the last vblank and have no relation to the actual time when
6032  * the flip done event was sent.
6033  */
6034 static int intel_async_flip_check_uapi(struct intel_atomic_state *state,
6035 				       struct intel_crtc *crtc)
6036 {
6037 	struct intel_display *display = to_intel_display(state);
6038 	const struct intel_crtc_state *new_crtc_state =
6039 		intel_atomic_get_new_crtc_state(state, crtc);
6040 	const struct intel_plane_state *old_plane_state;
6041 	struct intel_plane_state *new_plane_state;
6042 	struct intel_plane *plane;
6043 	int i;
6044 
6045 	if (!new_crtc_state->uapi.async_flip)
6046 		return 0;
6047 
6048 	if (!new_crtc_state->uapi.active) {
6049 		drm_dbg_kms(display->drm,
6050 			    "[CRTC:%d:%s] not active\n",
6051 			    crtc->base.base.id, crtc->base.name);
6052 		return -EINVAL;
6053 	}
6054 
6055 	if (intel_crtc_needs_modeset(new_crtc_state)) {
6056 		drm_dbg_kms(display->drm,
6057 			    "[CRTC:%d:%s] modeset required\n",
6058 			    crtc->base.base.id, crtc->base.name);
6059 		return -EINVAL;
6060 	}
6061 
6062 	/*
6063 	 * FIXME: joiner+async flip is busted currently.
6064 	 * Remove this check once the issues are fixed.
6065 	 */
6066 	if (new_crtc_state->joiner_pipes) {
6067 		drm_dbg_kms(display->drm,
6068 			    "[CRTC:%d:%s] async flip disallowed with joiner\n",
6069 			    crtc->base.base.id, crtc->base.name);
6070 		return -EINVAL;
6071 	}
6072 
6073 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
6074 					     new_plane_state, i) {
6075 		if (plane->pipe != crtc->pipe)
6076 			continue;
6077 
6078 		/*
6079 		 * TODO: Async flip is only supported through the page flip IOCTL
6080 		 * as of now. So support currently added for primary plane only.
6081 		 * Support for other planes on platforms on which supports
6082 		 * this(vlv/chv and icl+) should be added when async flip is
6083 		 * enabled in the atomic IOCTL path.
6084 		 */
6085 		if (!plane->async_flip) {
6086 			drm_dbg_kms(display->drm,
6087 				    "[PLANE:%d:%s] async flip not supported\n",
6088 				    plane->base.base.id, plane->base.name);
6089 			return -EINVAL;
6090 		}
6091 
6092 		if (!old_plane_state->uapi.fb || !new_plane_state->uapi.fb) {
6093 			drm_dbg_kms(display->drm,
6094 				    "[PLANE:%d:%s] no old or new framebuffer\n",
6095 				    plane->base.base.id, plane->base.name);
6096 			return -EINVAL;
6097 		}
6098 	}
6099 
6100 	return 0;
6101 }
6102 
6103 static int intel_async_flip_check_hw(struct intel_atomic_state *state, struct intel_crtc *crtc)
6104 {
6105 	struct intel_display *display = to_intel_display(state);
6106 	const struct intel_crtc_state *old_crtc_state, *new_crtc_state;
6107 	const struct intel_plane_state *new_plane_state, *old_plane_state;
6108 	struct intel_plane *plane;
6109 	int i;
6110 
6111 	old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc);
6112 	new_crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
6113 
6114 	if (!new_crtc_state->uapi.async_flip)
6115 		return 0;
6116 
6117 	if (!new_crtc_state->hw.active) {
6118 		drm_dbg_kms(display->drm,
6119 			    "[CRTC:%d:%s] not active\n",
6120 			    crtc->base.base.id, crtc->base.name);
6121 		return -EINVAL;
6122 	}
6123 
6124 	if (intel_crtc_needs_modeset(new_crtc_state)) {
6125 		drm_dbg_kms(display->drm,
6126 			    "[CRTC:%d:%s] modeset required\n",
6127 			    crtc->base.base.id, crtc->base.name);
6128 		return -EINVAL;
6129 	}
6130 
6131 	if (old_crtc_state->active_planes != new_crtc_state->active_planes) {
6132 		drm_dbg_kms(display->drm,
6133 			    "[CRTC:%d:%s] Active planes cannot be in async flip\n",
6134 			    crtc->base.base.id, crtc->base.name);
6135 		return -EINVAL;
6136 	}
6137 
6138 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
6139 					     new_plane_state, i) {
6140 		if (plane->pipe != crtc->pipe)
6141 			continue;
6142 
6143 		/*
6144 		 * Only async flip capable planes should be in the state
6145 		 * if we're really about to ask the hardware to perform
6146 		 * an async flip. We should never get this far otherwise.
6147 		 */
6148 		if (drm_WARN_ON(display->drm,
6149 				new_crtc_state->do_async_flip && !plane->async_flip))
6150 			return -EINVAL;
6151 
6152 		/*
6153 		 * Only check async flip capable planes other planes
6154 		 * may be involved in the initial commit due to
6155 		 * the wm0/ddb optimization.
6156 		 *
6157 		 * TODO maybe should track which planes actually
6158 		 * were requested to do the async flip...
6159 		 */
6160 		if (!plane->async_flip)
6161 			continue;
6162 
6163 		if (!intel_plane_can_async_flip(plane, new_plane_state->hw.fb->format,
6164 						new_plane_state->hw.fb->modifier)) {
6165 			drm_dbg_kms(display->drm,
6166 				    "[PLANE:%d:%s] pixel format %p4cc / modifier 0x%llx does not support async flip\n",
6167 				    plane->base.base.id, plane->base.name,
6168 				    &new_plane_state->hw.fb->format->format,
6169 				    new_plane_state->hw.fb->modifier);
6170 			return -EINVAL;
6171 		}
6172 
6173 		/*
6174 		 * We turn the first async flip request into a sync flip
6175 		 * so that we can reconfigure the plane (eg. change modifier).
6176 		 */
6177 		if (!new_crtc_state->do_async_flip)
6178 			continue;
6179 
6180 		if (old_plane_state->view.color_plane[0].mapping_stride !=
6181 		    new_plane_state->view.color_plane[0].mapping_stride) {
6182 			drm_dbg_kms(display->drm,
6183 				    "[PLANE:%d:%s] Stride cannot be changed in async flip\n",
6184 				    plane->base.base.id, plane->base.name);
6185 			return -EINVAL;
6186 		}
6187 
6188 		if (old_plane_state->hw.fb->modifier !=
6189 		    new_plane_state->hw.fb->modifier) {
6190 			drm_dbg_kms(display->drm,
6191 				    "[PLANE:%d:%s] Modifier cannot be changed in async flip\n",
6192 				    plane->base.base.id, plane->base.name);
6193 			return -EINVAL;
6194 		}
6195 
6196 		if (old_plane_state->hw.fb->format !=
6197 		    new_plane_state->hw.fb->format) {
6198 			drm_dbg_kms(display->drm,
6199 				    "[PLANE:%d:%s] Pixel format cannot be changed in async flip\n",
6200 				    plane->base.base.id, plane->base.name);
6201 			return -EINVAL;
6202 		}
6203 
6204 		if (old_plane_state->hw.rotation !=
6205 		    new_plane_state->hw.rotation) {
6206 			drm_dbg_kms(display->drm,
6207 				    "[PLANE:%d:%s] Rotation cannot be changed in async flip\n",
6208 				    plane->base.base.id, plane->base.name);
6209 			return -EINVAL;
6210 		}
6211 
6212 		if (skl_plane_aux_dist(old_plane_state, 0) !=
6213 		    skl_plane_aux_dist(new_plane_state, 0)) {
6214 			drm_dbg_kms(display->drm,
6215 				    "[PLANE:%d:%s] AUX_DIST cannot be changed in async flip\n",
6216 				    plane->base.base.id, plane->base.name);
6217 			return -EINVAL;
6218 		}
6219 
6220 		if (!drm_rect_equals(&old_plane_state->uapi.src, &new_plane_state->uapi.src) ||
6221 		    !drm_rect_equals(&old_plane_state->uapi.dst, &new_plane_state->uapi.dst)) {
6222 			drm_dbg_kms(display->drm,
6223 				    "[PLANE:%d:%s] Size/co-ordinates cannot be changed in async flip\n",
6224 				    plane->base.base.id, plane->base.name);
6225 			return -EINVAL;
6226 		}
6227 
6228 		if (old_plane_state->hw.alpha != new_plane_state->hw.alpha) {
6229 			drm_dbg_kms(display->drm,
6230 				    "[PLANES:%d:%s] Alpha value cannot be changed in async flip\n",
6231 				    plane->base.base.id, plane->base.name);
6232 			return -EINVAL;
6233 		}
6234 
6235 		if (old_plane_state->hw.pixel_blend_mode !=
6236 		    new_plane_state->hw.pixel_blend_mode) {
6237 			drm_dbg_kms(display->drm,
6238 				    "[PLANE:%d:%s] Pixel blend mode cannot be changed in async flip\n",
6239 				    plane->base.base.id, plane->base.name);
6240 			return -EINVAL;
6241 		}
6242 
6243 		if (old_plane_state->hw.color_encoding != new_plane_state->hw.color_encoding) {
6244 			drm_dbg_kms(display->drm,
6245 				    "[PLANE:%d:%s] Color encoding cannot be changed in async flip\n",
6246 				    plane->base.base.id, plane->base.name);
6247 			return -EINVAL;
6248 		}
6249 
6250 		if (old_plane_state->hw.color_range != new_plane_state->hw.color_range) {
6251 			drm_dbg_kms(display->drm,
6252 				    "[PLANE:%d:%s] Color range cannot be changed in async flip\n",
6253 				    plane->base.base.id, plane->base.name);
6254 			return -EINVAL;
6255 		}
6256 
6257 		/* plane decryption is allow to change only in synchronous flips */
6258 		if (old_plane_state->decrypt != new_plane_state->decrypt) {
6259 			drm_dbg_kms(display->drm,
6260 				    "[PLANE:%d:%s] Decryption cannot be changed in async flip\n",
6261 				    plane->base.base.id, plane->base.name);
6262 			return -EINVAL;
6263 		}
6264 	}
6265 
6266 	return 0;
6267 }
6268 
6269 static int intel_joiner_add_affected_crtcs(struct intel_atomic_state *state)
6270 {
6271 	struct intel_display *display = to_intel_display(state);
6272 	const struct intel_plane_state *plane_state;
6273 	struct intel_crtc_state *crtc_state;
6274 	struct intel_plane *plane;
6275 	struct intel_crtc *crtc;
6276 	u8 affected_pipes = 0;
6277 	u8 modeset_pipes = 0;
6278 	int i;
6279 
6280 	/*
6281 	 * Any plane which is in use by the joiner needs its crtc.
6282 	 * Pull those in first as this will not have happened yet
6283 	 * if the plane remains disabled according to uapi.
6284 	 */
6285 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
6286 		crtc = to_intel_crtc(plane_state->hw.crtc);
6287 		if (!crtc)
6288 			continue;
6289 
6290 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
6291 		if (IS_ERR(crtc_state))
6292 			return PTR_ERR(crtc_state);
6293 	}
6294 
6295 	/* Now pull in all joined crtcs */
6296 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
6297 		affected_pipes |= crtc_state->joiner_pipes;
6298 		if (intel_crtc_needs_modeset(crtc_state))
6299 			modeset_pipes |= crtc_state->joiner_pipes;
6300 	}
6301 
6302 	for_each_intel_crtc_in_pipe_mask(display->drm, crtc, affected_pipes) {
6303 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
6304 		if (IS_ERR(crtc_state))
6305 			return PTR_ERR(crtc_state);
6306 	}
6307 
6308 	for_each_intel_crtc_in_pipe_mask(display->drm, crtc, modeset_pipes) {
6309 		int ret;
6310 
6311 		crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
6312 
6313 		crtc_state->uapi.mode_changed = true;
6314 
6315 		ret = drm_atomic_add_affected_connectors(&state->base, &crtc->base);
6316 		if (ret)
6317 			return ret;
6318 
6319 		ret = intel_plane_add_affected(state, crtc);
6320 		if (ret)
6321 			return ret;
6322 	}
6323 
6324 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
6325 		/* Kill old joiner link, we may re-establish afterwards */
6326 		if (intel_crtc_needs_modeset(crtc_state) &&
6327 		    intel_crtc_is_joiner_primary(crtc_state))
6328 			kill_joiner_secondaries(state, crtc);
6329 	}
6330 
6331 	return 0;
6332 }
6333 
6334 static int intel_atomic_check_config(struct intel_atomic_state *state,
6335 				     struct intel_link_bw_limits *limits,
6336 				     enum pipe *failed_pipe)
6337 {
6338 	struct intel_display *display = to_intel_display(state);
6339 	struct intel_crtc_state *new_crtc_state;
6340 	struct intel_crtc *crtc;
6341 	int ret;
6342 	int i;
6343 
6344 	*failed_pipe = INVALID_PIPE;
6345 
6346 	ret = intel_joiner_add_affected_crtcs(state);
6347 	if (ret)
6348 		return ret;
6349 
6350 	ret = intel_fdi_add_affected_crtcs(state);
6351 	if (ret)
6352 		return ret;
6353 
6354 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6355 		if (!intel_crtc_needs_modeset(new_crtc_state)) {
6356 			if (intel_crtc_is_joiner_secondary(new_crtc_state))
6357 				copy_joiner_crtc_state_nomodeset(state, crtc);
6358 			else
6359 				intel_crtc_copy_uapi_to_hw_state_nomodeset(state, crtc);
6360 			continue;
6361 		}
6362 
6363 		if (drm_WARN_ON(display->drm, intel_crtc_is_joiner_secondary(new_crtc_state)))
6364 			continue;
6365 
6366 		ret = intel_crtc_prepare_cleared_state(state, crtc);
6367 		if (ret)
6368 			goto fail;
6369 
6370 		if (!new_crtc_state->hw.enable)
6371 			continue;
6372 
6373 		ret = intel_modeset_pipe_config(state, crtc, limits);
6374 		if (ret)
6375 			goto fail;
6376 	}
6377 
6378 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6379 		if (!intel_crtc_needs_modeset(new_crtc_state))
6380 			continue;
6381 
6382 		if (drm_WARN_ON(display->drm, intel_crtc_is_joiner_secondary(new_crtc_state)))
6383 			continue;
6384 
6385 		if (!new_crtc_state->hw.enable)
6386 			continue;
6387 
6388 		ret = intel_modeset_pipe_config_late(state, crtc);
6389 		if (ret)
6390 			goto fail;
6391 	}
6392 
6393 fail:
6394 	if (ret)
6395 		*failed_pipe = crtc->pipe;
6396 
6397 	return ret;
6398 }
6399 
6400 static int intel_atomic_check_config_and_link(struct intel_atomic_state *state)
6401 {
6402 	struct intel_link_bw_limits new_limits;
6403 	struct intel_link_bw_limits old_limits;
6404 	int ret;
6405 
6406 	intel_link_bw_init_limits(state, &new_limits);
6407 	old_limits = new_limits;
6408 
6409 	while (true) {
6410 		enum pipe failed_pipe;
6411 
6412 		ret = intel_atomic_check_config(state, &new_limits,
6413 						&failed_pipe);
6414 		if (ret) {
6415 			/*
6416 			 * The bpp limit for a pipe is below the minimum it supports, set the
6417 			 * limit to the minimum and recalculate the config.
6418 			 */
6419 			if (ret == -EINVAL &&
6420 			    intel_link_bw_set_bpp_limit_for_pipe(state,
6421 								 &old_limits,
6422 								 &new_limits,
6423 								 failed_pipe))
6424 				continue;
6425 
6426 			break;
6427 		}
6428 
6429 		old_limits = new_limits;
6430 
6431 		ret = intel_link_bw_atomic_check(state, &new_limits);
6432 		if (ret != -EAGAIN)
6433 			break;
6434 	}
6435 
6436 	return ret;
6437 }
6438 /**
6439  * intel_atomic_check - validate state object
6440  * @dev: drm device
6441  * @_state: state to validate
6442  */
6443 int intel_atomic_check(struct drm_device *dev,
6444 		       struct drm_atomic_state *_state)
6445 {
6446 	struct intel_display *display = to_intel_display(dev);
6447 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
6448 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
6449 	struct intel_crtc *crtc;
6450 	int ret, i;
6451 
6452 	if (!intel_display_driver_check_access(display))
6453 		return -ENODEV;
6454 
6455 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6456 					    new_crtc_state, i) {
6457 		/*
6458 		 * crtc's state no longer considered to be inherited
6459 		 * after the first userspace/client initiated commit.
6460 		 */
6461 		if (!state->internal)
6462 			new_crtc_state->inherited = false;
6463 
6464 		if (new_crtc_state->inherited != old_crtc_state->inherited)
6465 			new_crtc_state->uapi.mode_changed = true;
6466 
6467 		if (new_crtc_state->uapi.scaling_filter !=
6468 		    old_crtc_state->uapi.scaling_filter)
6469 			new_crtc_state->uapi.mode_changed = true;
6470 	}
6471 
6472 	intel_vrr_check_modeset(state);
6473 
6474 	ret = drm_atomic_helper_check_modeset(dev, &state->base);
6475 	if (ret)
6476 		goto fail;
6477 
6478 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6479 		ret = intel_async_flip_check_uapi(state, crtc);
6480 		if (ret)
6481 			return ret;
6482 	}
6483 
6484 	ret = intel_atomic_check_config_and_link(state);
6485 	if (ret)
6486 		goto fail;
6487 
6488 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6489 		if (!intel_crtc_needs_modeset(new_crtc_state))
6490 			continue;
6491 
6492 		if (intel_crtc_is_joiner_secondary(new_crtc_state)) {
6493 			drm_WARN_ON(display->drm, new_crtc_state->uapi.enable);
6494 			continue;
6495 		}
6496 
6497 		ret = intel_atomic_check_joiner(state, crtc);
6498 		if (ret)
6499 			goto fail;
6500 	}
6501 
6502 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6503 					    new_crtc_state, i) {
6504 		if (!intel_crtc_needs_modeset(new_crtc_state))
6505 			continue;
6506 
6507 		intel_joiner_adjust_pipe_src(new_crtc_state);
6508 
6509 		intel_crtc_check_fastset(old_crtc_state, new_crtc_state);
6510 	}
6511 
6512 	/**
6513 	 * Check if fastset is allowed by external dependencies like other
6514 	 * pipes and transcoders.
6515 	 *
6516 	 * Right now it only forces a fullmodeset when the MST master
6517 	 * transcoder did not changed but the pipe of the master transcoder
6518 	 * needs a fullmodeset so all slaves also needs to do a fullmodeset or
6519 	 * in case of port synced crtcs, if one of the synced crtcs
6520 	 * needs a full modeset, all other synced crtcs should be
6521 	 * forced a full modeset.
6522 	 */
6523 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6524 		if (!new_crtc_state->hw.enable || intel_crtc_needs_modeset(new_crtc_state))
6525 			continue;
6526 
6527 		if (intel_dp_mst_crtc_needs_modeset(state, crtc))
6528 			intel_crtc_flag_modeset(new_crtc_state);
6529 
6530 		if (intel_dp_mst_is_slave_trans(new_crtc_state)) {
6531 			enum transcoder master = new_crtc_state->mst_master_transcoder;
6532 
6533 			if (intel_cpu_transcoders_need_modeset(state, BIT(master)))
6534 				intel_crtc_flag_modeset(new_crtc_state);
6535 		}
6536 
6537 		if (is_trans_port_sync_mode(new_crtc_state)) {
6538 			u8 trans = new_crtc_state->sync_mode_slaves_mask;
6539 
6540 			if (new_crtc_state->master_transcoder != INVALID_TRANSCODER)
6541 				trans |= BIT(new_crtc_state->master_transcoder);
6542 
6543 			if (intel_cpu_transcoders_need_modeset(state, trans))
6544 				intel_crtc_flag_modeset(new_crtc_state);
6545 		}
6546 
6547 		if (new_crtc_state->joiner_pipes) {
6548 			if (intel_pipes_need_modeset(state, new_crtc_state->joiner_pipes))
6549 				intel_crtc_flag_modeset(new_crtc_state);
6550 		}
6551 	}
6552 
6553 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6554 					    new_crtc_state, i) {
6555 		if (!intel_crtc_needs_modeset(new_crtc_state))
6556 			continue;
6557 
6558 		intel_dpll_release(state, crtc);
6559 	}
6560 
6561 	if (intel_any_crtc_needs_modeset(state) && !check_digital_port_conflicts(state)) {
6562 		drm_dbg_kms(display->drm, "rejecting conflicting digital port configuration\n");
6563 		ret = -EINVAL;
6564 		goto fail;
6565 	}
6566 
6567 	ret = intel_plane_atomic_check(state);
6568 	if (ret)
6569 		goto fail;
6570 
6571 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
6572 		new_crtc_state->min_cdclk = intel_crtc_min_cdclk(new_crtc_state);
6573 
6574 	ret = intel_compute_global_watermarks(state);
6575 	if (ret)
6576 		goto fail;
6577 
6578 	ret = intel_bw_atomic_check(state);
6579 	if (ret)
6580 		goto fail;
6581 
6582 	ret = intel_cdclk_atomic_check(state);
6583 	if (ret)
6584 		goto fail;
6585 
6586 	if (intel_any_crtc_needs_modeset(state)) {
6587 		ret = intel_modeset_checks(state);
6588 		if (ret)
6589 			goto fail;
6590 	}
6591 
6592 	ret = intel_pmdemand_atomic_check(state);
6593 	if (ret)
6594 		goto fail;
6595 
6596 	ret = intel_atomic_check_crtcs(state);
6597 	if (ret)
6598 		goto fail;
6599 
6600 	ret = intel_fbc_atomic_check(state);
6601 	if (ret)
6602 		goto fail;
6603 
6604 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6605 					    new_crtc_state, i) {
6606 		intel_color_assert_luts(new_crtc_state);
6607 
6608 		ret = intel_async_flip_check_hw(state, crtc);
6609 		if (ret)
6610 			goto fail;
6611 
6612 		/* Either full modeset or fastset (or neither), never both */
6613 		drm_WARN_ON(display->drm,
6614 			    intel_crtc_needs_modeset(new_crtc_state) &&
6615 			    intel_crtc_needs_fastset(new_crtc_state));
6616 
6617 		if (!intel_crtc_needs_modeset(new_crtc_state) &&
6618 		    !intel_crtc_needs_fastset(new_crtc_state))
6619 			continue;
6620 
6621 		intel_crtc_state_dump(new_crtc_state, state,
6622 				      intel_crtc_needs_modeset(new_crtc_state) ?
6623 				      "modeset" : "fastset");
6624 	}
6625 
6626 	return 0;
6627 
6628  fail:
6629 	if (ret == -EDEADLK)
6630 		return ret;
6631 
6632 	/*
6633 	 * FIXME would probably be nice to know which crtc specifically
6634 	 * caused the failure, in cases where we can pinpoint it.
6635 	 */
6636 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6637 					    new_crtc_state, i)
6638 		intel_crtc_state_dump(new_crtc_state, state, "failed");
6639 
6640 	return ret;
6641 }
6642 
6643 static int intel_atomic_prepare_commit(struct intel_atomic_state *state)
6644 {
6645 	int ret;
6646 
6647 	ret = drm_atomic_helper_prepare_planes(state->base.dev, &state->base);
6648 	if (ret < 0)
6649 		return ret;
6650 
6651 	return 0;
6652 }
6653 
6654 void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
6655 				  struct intel_crtc_state *crtc_state)
6656 {
6657 	struct intel_display *display = to_intel_display(crtc);
6658 
6659 	if (DISPLAY_VER(display) != 2 || crtc_state->active_planes)
6660 		intel_set_cpu_fifo_underrun_reporting(display, crtc->pipe, true);
6661 
6662 	if (crtc_state->has_pch_encoder) {
6663 		enum pipe pch_transcoder =
6664 			intel_crtc_pch_transcoder(crtc);
6665 
6666 		intel_set_pch_fifo_underrun_reporting(display, pch_transcoder, true);
6667 	}
6668 }
6669 
6670 static void intel_pipe_fastset(const struct intel_crtc_state *old_crtc_state,
6671 			       const struct intel_crtc_state *new_crtc_state)
6672 {
6673 	struct intel_display *display = to_intel_display(new_crtc_state);
6674 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
6675 
6676 	/*
6677 	 * Update pipe size and adjust fitter if needed: the reason for this is
6678 	 * that in compute_mode_changes we check the native mode (not the pfit
6679 	 * mode) to see if we can flip rather than do a full mode set. In the
6680 	 * fastboot case, we'll flip, but if we don't update the pipesrc and
6681 	 * pfit state, we'll end up with a big fb scanned out into the wrong
6682 	 * sized surface.
6683 	 */
6684 	intel_set_pipe_src_size(new_crtc_state);
6685 
6686 	/* on skylake this is done by detaching scalers */
6687 	if (DISPLAY_VER(display) >= 9) {
6688 		if (new_crtc_state->pch_pfit.enabled)
6689 			skl_pfit_enable(new_crtc_state);
6690 	} else if (HAS_PCH_SPLIT(display)) {
6691 		if (new_crtc_state->pch_pfit.enabled)
6692 			ilk_pfit_enable(new_crtc_state);
6693 		else if (old_crtc_state->pch_pfit.enabled)
6694 			ilk_pfit_disable(old_crtc_state);
6695 	}
6696 
6697 	/*
6698 	 * The register is supposedly single buffered so perhaps
6699 	 * not 100% correct to do this here. But SKL+ calculate
6700 	 * this based on the adjust pixel rate so pfit changes do
6701 	 * affect it and so it must be updated for fastsets.
6702 	 * HSW/BDW only really need this here for fastboot, after
6703 	 * that the value should not change without a full modeset.
6704 	 */
6705 	if (DISPLAY_VER(display) >= 9 ||
6706 	    display->platform.broadwell || display->platform.haswell)
6707 		hsw_set_linetime_wm(new_crtc_state);
6708 
6709 	if (new_crtc_state->update_m_n)
6710 		intel_cpu_transcoder_set_m1_n1(crtc, new_crtc_state->cpu_transcoder,
6711 					       &new_crtc_state->dp_m_n);
6712 
6713 	if (new_crtc_state->update_lrr)
6714 		intel_set_transcoder_timings_lrr(new_crtc_state);
6715 }
6716 
6717 static void commit_pipe_pre_planes(struct intel_atomic_state *state,
6718 				   struct intel_crtc *crtc)
6719 {
6720 	struct intel_display *display = to_intel_display(state);
6721 	const struct intel_crtc_state *old_crtc_state =
6722 		intel_atomic_get_old_crtc_state(state, crtc);
6723 	const struct intel_crtc_state *new_crtc_state =
6724 		intel_atomic_get_new_crtc_state(state, crtc);
6725 	bool modeset = intel_crtc_needs_modeset(new_crtc_state);
6726 
6727 	drm_WARN_ON(display->drm, new_crtc_state->use_dsb || new_crtc_state->use_flipq);
6728 
6729 	/*
6730 	 * During modesets pipe configuration was programmed as the
6731 	 * CRTC was enabled.
6732 	 */
6733 	if (!modeset) {
6734 		if (intel_crtc_needs_color_update(new_crtc_state))
6735 			intel_color_commit_arm(NULL, new_crtc_state);
6736 
6737 		if (DISPLAY_VER(display) >= 9 || display->platform.broadwell)
6738 			bdw_set_pipe_misc(NULL, new_crtc_state);
6739 
6740 		if (intel_crtc_needs_fastset(new_crtc_state))
6741 			intel_pipe_fastset(old_crtc_state, new_crtc_state);
6742 	}
6743 
6744 	intel_psr2_program_trans_man_trk_ctl(NULL, new_crtc_state);
6745 
6746 	intel_atomic_update_watermarks(state, crtc);
6747 }
6748 
6749 static void commit_pipe_post_planes(struct intel_atomic_state *state,
6750 				    struct intel_crtc *crtc)
6751 {
6752 	struct intel_display *display = to_intel_display(state);
6753 	const struct intel_crtc_state *new_crtc_state =
6754 		intel_atomic_get_new_crtc_state(state, crtc);
6755 	bool modeset = intel_crtc_needs_modeset(new_crtc_state);
6756 
6757 	drm_WARN_ON(display->drm, new_crtc_state->use_dsb || new_crtc_state->use_flipq);
6758 
6759 	/*
6760 	 * Disable the scaler(s) after the plane(s) so that we don't
6761 	 * get a catastrophic underrun even if the two operations
6762 	 * end up happening in two different frames.
6763 	 */
6764 	if (DISPLAY_VER(display) >= 9 && !modeset)
6765 		skl_detach_scalers(NULL, new_crtc_state);
6766 
6767 	if (!modeset &&
6768 	    intel_crtc_needs_color_update(new_crtc_state) &&
6769 	    !intel_color_uses_dsb(new_crtc_state) &&
6770 	    HAS_DOUBLE_BUFFERED_LUT(display))
6771 		intel_color_load_luts(new_crtc_state);
6772 
6773 	if (intel_crtc_vrr_enabling(state, crtc))
6774 		intel_vrr_enable(new_crtc_state);
6775 }
6776 
6777 static void intel_enable_crtc(struct intel_atomic_state *state,
6778 			      struct intel_crtc *crtc)
6779 {
6780 	struct intel_display *display = to_intel_display(state);
6781 	const struct intel_crtc_state *new_crtc_state =
6782 		intel_atomic_get_new_crtc_state(state, crtc);
6783 	struct intel_crtc *pipe_crtc;
6784 
6785 	if (!intel_crtc_needs_modeset(new_crtc_state))
6786 		return;
6787 
6788 	for_each_intel_crtc_in_pipe_mask_reverse(display->drm, pipe_crtc,
6789 						 intel_crtc_joined_pipe_mask(new_crtc_state)) {
6790 		const struct intel_crtc_state *pipe_crtc_state =
6791 			intel_atomic_get_new_crtc_state(state, pipe_crtc);
6792 
6793 		/* VRR will be enable later, if required */
6794 		intel_crtc_update_active_timings(pipe_crtc_state, false);
6795 	}
6796 
6797 	intel_psr_notify_pipe_change(state, crtc, true);
6798 
6799 	display->funcs.display->crtc_enable(state, crtc);
6800 
6801 	/* vblanks work again, re-enable pipe CRC. */
6802 	intel_crtc_enable_pipe_crc(crtc);
6803 }
6804 
6805 static void intel_pre_update_crtc(struct intel_atomic_state *state,
6806 				  struct intel_crtc *crtc)
6807 {
6808 	struct intel_display *display = to_intel_display(state);
6809 	const struct intel_crtc_state *old_crtc_state =
6810 		intel_atomic_get_old_crtc_state(state, crtc);
6811 	struct intel_crtc_state *new_crtc_state =
6812 		intel_atomic_get_new_crtc_state(state, crtc);
6813 	bool modeset = intel_crtc_needs_modeset(new_crtc_state);
6814 
6815 	if (old_crtc_state->inherited ||
6816 	    intel_crtc_needs_modeset(new_crtc_state)) {
6817 		if (HAS_DPT(display))
6818 			intel_dpt_configure(crtc);
6819 	}
6820 
6821 	if (!modeset) {
6822 		if (new_crtc_state->preload_luts &&
6823 		    intel_crtc_needs_color_update(new_crtc_state))
6824 			intel_color_load_luts(new_crtc_state);
6825 
6826 		intel_pre_plane_update(state, crtc);
6827 
6828 		if (intel_crtc_needs_fastset(new_crtc_state))
6829 			intel_encoders_update_pipe(state, crtc);
6830 
6831 		if (DISPLAY_VER(display) >= 11 &&
6832 		    intel_crtc_needs_fastset(new_crtc_state))
6833 			icl_set_pipe_chicken(new_crtc_state);
6834 
6835 		if (vrr_params_changed(old_crtc_state, new_crtc_state) ||
6836 		    cmrr_params_changed(old_crtc_state, new_crtc_state))
6837 			intel_vrr_set_transcoder_timings(new_crtc_state);
6838 	}
6839 
6840 	if (intel_casf_enabling(new_crtc_state, old_crtc_state))
6841 		intel_casf_enable(new_crtc_state);
6842 	else if (new_crtc_state->hw.casf_params.strength != old_crtc_state->hw.casf_params.strength)
6843 		intel_casf_update_strength(new_crtc_state);
6844 
6845 	intel_fbc_update(state, crtc);
6846 
6847 	drm_WARN_ON(display->drm, !intel_display_power_is_enabled(display, POWER_DOMAIN_DC_OFF));
6848 
6849 	if (!modeset &&
6850 	    intel_crtc_needs_color_update(new_crtc_state) &&
6851 	    !new_crtc_state->use_dsb && !new_crtc_state->use_flipq)
6852 		intel_color_commit_noarm(NULL, new_crtc_state);
6853 
6854 	if (!new_crtc_state->use_dsb && !new_crtc_state->use_flipq)
6855 		intel_crtc_planes_update_noarm(NULL, state, crtc);
6856 }
6857 
6858 static void intel_update_crtc(struct intel_atomic_state *state,
6859 			      struct intel_crtc *crtc)
6860 {
6861 	const struct intel_crtc_state *old_crtc_state =
6862 		intel_atomic_get_old_crtc_state(state, crtc);
6863 	struct intel_crtc_state *new_crtc_state =
6864 		intel_atomic_get_new_crtc_state(state, crtc);
6865 
6866 	if (new_crtc_state->use_flipq) {
6867 		intel_flipq_enable(new_crtc_state);
6868 
6869 		intel_crtc_prepare_vblank_event(new_crtc_state, &crtc->flipq_event);
6870 
6871 		intel_flipq_add(crtc, INTEL_FLIPQ_PLANE_1, 0, INTEL_DSB_0,
6872 				new_crtc_state->dsb_commit);
6873 	} else if (new_crtc_state->use_dsb) {
6874 		intel_crtc_prepare_vblank_event(new_crtc_state, &crtc->dsb_event);
6875 
6876 		intel_dsb_commit(new_crtc_state->dsb_commit);
6877 	} else {
6878 		/* Perform vblank evasion around commit operation */
6879 		intel_pipe_update_start(state, crtc);
6880 
6881 		if (new_crtc_state->dsb_commit)
6882 			intel_dsb_commit(new_crtc_state->dsb_commit);
6883 
6884 		commit_pipe_pre_planes(state, crtc);
6885 
6886 		intel_crtc_planes_update_arm(NULL, state, crtc);
6887 
6888 		commit_pipe_post_planes(state, crtc);
6889 
6890 		intel_pipe_update_end(state, crtc);
6891 	}
6892 
6893 	/*
6894 	 * VRR/Seamless M/N update may need to update frame timings.
6895 	 *
6896 	 * FIXME Should be synchronized with the start of vblank somehow...
6897 	 */
6898 	if (intel_crtc_vrr_enabling(state, crtc) ||
6899 	    new_crtc_state->update_m_n || new_crtc_state->update_lrr)
6900 		intel_crtc_update_active_timings(new_crtc_state,
6901 						 new_crtc_state->vrr.enable);
6902 
6903 	if (new_crtc_state->vrr.dc_balance.enable)
6904 		intel_vrr_dcb_increment_flip_count(new_crtc_state, crtc);
6905 
6906 	/*
6907 	 * We usually enable FIFO underrun interrupts as part of the
6908 	 * CRTC enable sequence during modesets.  But when we inherit a
6909 	 * valid pipe configuration from the BIOS we need to take care
6910 	 * of enabling them on the CRTC's first fastset.
6911 	 */
6912 	if (intel_crtc_needs_fastset(new_crtc_state) &&
6913 	    old_crtc_state->inherited)
6914 		intel_crtc_arm_fifo_underrun(crtc, new_crtc_state);
6915 }
6916 
6917 static void intel_old_crtc_state_disables(struct intel_atomic_state *state,
6918 					  struct intel_crtc *crtc)
6919 {
6920 	struct intel_display *display = to_intel_display(state);
6921 	const struct intel_crtc_state *old_crtc_state =
6922 		intel_atomic_get_old_crtc_state(state, crtc);
6923 	struct intel_crtc *pipe_crtc;
6924 
6925 	/*
6926 	 * We need to disable pipe CRC before disabling the pipe,
6927 	 * or we race against vblank off.
6928 	 */
6929 	for_each_intel_crtc_in_pipe_mask(display->drm, pipe_crtc,
6930 					 intel_crtc_joined_pipe_mask(old_crtc_state))
6931 		intel_crtc_disable_pipe_crc(pipe_crtc);
6932 
6933 	intel_psr_notify_pipe_change(state, crtc, false);
6934 
6935 	display->funcs.display->crtc_disable(state, crtc);
6936 
6937 	for_each_intel_crtc_in_pipe_mask(display->drm, pipe_crtc,
6938 					 intel_crtc_joined_pipe_mask(old_crtc_state)) {
6939 		const struct intel_crtc_state *new_pipe_crtc_state =
6940 			intel_atomic_get_new_crtc_state(state, pipe_crtc);
6941 
6942 		pipe_crtc->active = false;
6943 		intel_fbc_disable(pipe_crtc);
6944 
6945 		if (!new_pipe_crtc_state->hw.active)
6946 			intel_initial_watermarks(state, pipe_crtc);
6947 	}
6948 }
6949 
6950 static void intel_commit_modeset_disables(struct intel_atomic_state *state)
6951 {
6952 	struct intel_display *display = to_intel_display(state);
6953 	const struct intel_crtc_state *new_crtc_state, *old_crtc_state;
6954 	struct intel_crtc *crtc;
6955 	u8 disable_pipes = 0;
6956 	int i;
6957 
6958 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6959 					    new_crtc_state, i) {
6960 		if (!intel_crtc_needs_modeset(new_crtc_state))
6961 			continue;
6962 
6963 		/*
6964 		 * Needs to be done even for pipes
6965 		 * that weren't enabled previously.
6966 		 */
6967 		intel_pre_plane_update(state, crtc);
6968 
6969 		if (!old_crtc_state->hw.active)
6970 			continue;
6971 
6972 		disable_pipes |= BIT(crtc->pipe);
6973 	}
6974 
6975 	for_each_old_intel_crtc_in_state(state, crtc, old_crtc_state, i) {
6976 		if ((disable_pipes & BIT(crtc->pipe)) == 0)
6977 			continue;
6978 
6979 		intel_crtc_disable_planes(state, crtc);
6980 
6981 		drm_vblank_work_flush_all(&crtc->base);
6982 	}
6983 
6984 	/* Only disable port sync and MST slaves */
6985 	for_each_old_intel_crtc_in_state(state, crtc, old_crtc_state, i) {
6986 		if ((disable_pipes & BIT(crtc->pipe)) == 0)
6987 			continue;
6988 
6989 		if (intel_crtc_is_joiner_secondary(old_crtc_state))
6990 			continue;
6991 
6992 		/* In case of Transcoder port Sync master slave CRTCs can be
6993 		 * assigned in any order and we need to make sure that
6994 		 * slave CRTCs are disabled first and then master CRTC since
6995 		 * Slave vblanks are masked till Master Vblanks.
6996 		 */
6997 		if (!is_trans_port_sync_slave(old_crtc_state) &&
6998 		    !intel_dp_mst_is_slave_trans(old_crtc_state))
6999 			continue;
7000 
7001 		intel_old_crtc_state_disables(state, crtc);
7002 
7003 		disable_pipes &= ~intel_crtc_joined_pipe_mask(old_crtc_state);
7004 	}
7005 
7006 	/* Disable everything else left on */
7007 	for_each_old_intel_crtc_in_state(state, crtc, old_crtc_state, i) {
7008 		if ((disable_pipes & BIT(crtc->pipe)) == 0)
7009 			continue;
7010 
7011 		if (intel_crtc_is_joiner_secondary(old_crtc_state))
7012 			continue;
7013 
7014 		intel_old_crtc_state_disables(state, crtc);
7015 
7016 		disable_pipes &= ~intel_crtc_joined_pipe_mask(old_crtc_state);
7017 	}
7018 
7019 	drm_WARN_ON(display->drm, disable_pipes);
7020 }
7021 
7022 static void intel_commit_modeset_enables(struct intel_atomic_state *state)
7023 {
7024 	struct intel_crtc_state *new_crtc_state;
7025 	struct intel_crtc *crtc;
7026 	int i;
7027 
7028 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7029 		if (!new_crtc_state->hw.active)
7030 			continue;
7031 
7032 		intel_enable_crtc(state, crtc);
7033 		intel_pre_update_crtc(state, crtc);
7034 	}
7035 
7036 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7037 		if (!new_crtc_state->hw.active)
7038 			continue;
7039 
7040 		intel_update_crtc(state, crtc);
7041 	}
7042 }
7043 
7044 static void skl_commit_modeset_enables(struct intel_atomic_state *state)
7045 {
7046 	struct intel_display *display = to_intel_display(state);
7047 	struct intel_crtc *crtc;
7048 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
7049 	struct skl_ddb_entry entries[I915_MAX_PIPES] = {};
7050 	u8 update_pipes = 0, modeset_pipes = 0;
7051 	int i;
7052 
7053 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
7054 		enum pipe pipe = crtc->pipe;
7055 
7056 		if (!new_crtc_state->hw.active)
7057 			continue;
7058 
7059 		/* ignore allocations for crtc's that have been turned off. */
7060 		if (!intel_crtc_needs_modeset(new_crtc_state)) {
7061 			entries[pipe] = old_crtc_state->wm.skl.ddb;
7062 			update_pipes |= BIT(pipe);
7063 		} else {
7064 			modeset_pipes |= BIT(pipe);
7065 		}
7066 	}
7067 
7068 	/*
7069 	 * Whenever the number of active pipes changes, we need to make sure we
7070 	 * update the pipes in the right order so that their ddb allocations
7071 	 * never overlap with each other between CRTC updates. Otherwise we'll
7072 	 * cause pipe underruns and other bad stuff.
7073 	 *
7074 	 * So first lets enable all pipes that do not need a fullmodeset as
7075 	 * those don't have any external dependency.
7076 	 */
7077 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7078 		enum pipe pipe = crtc->pipe;
7079 
7080 		if ((update_pipes & BIT(pipe)) == 0)
7081 			continue;
7082 
7083 		intel_pre_update_crtc(state, crtc);
7084 	}
7085 
7086 	intel_dbuf_mbus_pre_ddb_update(state);
7087 
7088 	while (update_pipes) {
7089 		/*
7090 		 * Commit in reverse order to make joiner primary
7091 		 * send the uapi events after secondaries are done.
7092 		 */
7093 		for_each_oldnew_intel_crtc_in_state_reverse(state, crtc, old_crtc_state,
7094 							    new_crtc_state, i) {
7095 			enum pipe pipe = crtc->pipe;
7096 
7097 			if ((update_pipes & BIT(pipe)) == 0)
7098 				continue;
7099 
7100 			if (skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
7101 							entries, I915_MAX_PIPES, pipe))
7102 				continue;
7103 
7104 			entries[pipe] = new_crtc_state->wm.skl.ddb;
7105 			update_pipes &= ~BIT(pipe);
7106 
7107 			intel_update_crtc(state, crtc);
7108 
7109 			/*
7110 			 * If this is an already active pipe, it's DDB changed,
7111 			 * and this isn't the last pipe that needs updating
7112 			 * then we need to wait for a vblank to pass for the
7113 			 * new ddb allocation to take effect.
7114 			 */
7115 			if (!skl_ddb_entry_equal(&new_crtc_state->wm.skl.ddb,
7116 						 &old_crtc_state->wm.skl.ddb) &&
7117 			    (update_pipes | modeset_pipes))
7118 				intel_crtc_wait_for_next_vblank(crtc);
7119 		}
7120 	}
7121 
7122 	intel_dbuf_mbus_post_ddb_update(state);
7123 
7124 	update_pipes = modeset_pipes;
7125 
7126 	/*
7127 	 * Enable all pipes that needs a modeset and do not depends on other
7128 	 * pipes
7129 	 */
7130 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7131 		enum pipe pipe = crtc->pipe;
7132 
7133 		if ((modeset_pipes & BIT(pipe)) == 0)
7134 			continue;
7135 
7136 		if (intel_crtc_is_joiner_secondary(new_crtc_state))
7137 			continue;
7138 
7139 		if (intel_dp_mst_is_slave_trans(new_crtc_state) ||
7140 		    is_trans_port_sync_master(new_crtc_state))
7141 			continue;
7142 
7143 		modeset_pipes &= ~intel_crtc_joined_pipe_mask(new_crtc_state);
7144 
7145 		intel_enable_crtc(state, crtc);
7146 	}
7147 
7148 	/*
7149 	 * Then we enable all remaining pipes that depend on other
7150 	 * pipes: MST slaves and port sync masters
7151 	 */
7152 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7153 		enum pipe pipe = crtc->pipe;
7154 
7155 		if ((modeset_pipes & BIT(pipe)) == 0)
7156 			continue;
7157 
7158 		if (intel_crtc_is_joiner_secondary(new_crtc_state))
7159 			continue;
7160 
7161 		modeset_pipes &= ~intel_crtc_joined_pipe_mask(new_crtc_state);
7162 
7163 		intel_enable_crtc(state, crtc);
7164 	}
7165 
7166 	/*
7167 	 * Finally we do the plane updates/etc. for all pipes that got enabled.
7168 	 */
7169 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7170 		enum pipe pipe = crtc->pipe;
7171 
7172 		if ((update_pipes & BIT(pipe)) == 0)
7173 			continue;
7174 
7175 		intel_pre_update_crtc(state, crtc);
7176 	}
7177 
7178 	/*
7179 	 * Commit in reverse order to make joiner primary
7180 	 * send the uapi events after secondaries are done.
7181 	 */
7182 	for_each_new_intel_crtc_in_state_reverse(state, crtc, new_crtc_state, i) {
7183 		enum pipe pipe = crtc->pipe;
7184 
7185 		if ((update_pipes & BIT(pipe)) == 0)
7186 			continue;
7187 
7188 		drm_WARN_ON(display->drm,
7189 			    skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
7190 							entries, I915_MAX_PIPES, pipe));
7191 
7192 		entries[pipe] = new_crtc_state->wm.skl.ddb;
7193 		update_pipes &= ~BIT(pipe);
7194 
7195 		intel_update_crtc(state, crtc);
7196 	}
7197 
7198 	drm_WARN_ON(display->drm, modeset_pipes);
7199 	drm_WARN_ON(display->drm, update_pipes);
7200 }
7201 
7202 static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
7203 {
7204 	struct drm_plane *plane;
7205 	struct drm_plane_state *new_plane_state;
7206 	long ret;
7207 	int i;
7208 
7209 	for_each_new_plane_in_state(&intel_state->base, plane, new_plane_state, i) {
7210 		if (new_plane_state->fence) {
7211 			ret = dma_fence_wait_timeout(new_plane_state->fence, false,
7212 						     i915_fence_timeout());
7213 			if (ret <= 0)
7214 				break;
7215 
7216 			dma_fence_put(new_plane_state->fence);
7217 			new_plane_state->fence = NULL;
7218 		}
7219 	}
7220 }
7221 
7222 static void intel_atomic_dsb_wait_commit(struct intel_crtc_state *crtc_state)
7223 {
7224 	if (crtc_state->dsb_commit)
7225 		intel_dsb_wait(crtc_state->dsb_commit);
7226 
7227 	intel_color_wait_commit(crtc_state);
7228 }
7229 
7230 static void intel_atomic_dsb_cleanup(struct intel_crtc_state *crtc_state)
7231 {
7232 	if (crtc_state->dsb_commit) {
7233 		intel_dsb_cleanup(crtc_state->dsb_commit);
7234 		crtc_state->dsb_commit = NULL;
7235 	}
7236 
7237 	intel_color_cleanup_commit(crtc_state);
7238 }
7239 
7240 static void intel_atomic_cleanup_work(struct work_struct *work)
7241 {
7242 	struct intel_atomic_state *state =
7243 		container_of(work, struct intel_atomic_state, cleanup_work);
7244 	struct intel_display *display = to_intel_display(state);
7245 	struct intel_crtc_state *old_crtc_state;
7246 	struct intel_crtc *crtc;
7247 	int i;
7248 
7249 	for_each_old_intel_crtc_in_state(state, crtc, old_crtc_state, i)
7250 		intel_atomic_dsb_cleanup(old_crtc_state);
7251 
7252 	drm_atomic_helper_cleanup_planes(display->drm, &state->base);
7253 	drm_atomic_helper_commit_cleanup_done(&state->base);
7254 	drm_atomic_state_put(&state->base);
7255 }
7256 
7257 static void intel_atomic_prepare_plane_clear_colors(struct intel_atomic_state *state)
7258 {
7259 	struct intel_display *display = to_intel_display(state);
7260 	struct intel_plane *plane;
7261 	struct intel_plane_state *plane_state;
7262 	int i;
7263 
7264 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
7265 		struct drm_framebuffer *fb = plane_state->hw.fb;
7266 		int cc_plane;
7267 		int ret;
7268 
7269 		if (!fb)
7270 			continue;
7271 
7272 		cc_plane = intel_fb_rc_ccs_cc_plane(fb);
7273 		if (cc_plane < 0)
7274 			continue;
7275 
7276 		/*
7277 		 * The layout of the fast clear color value expected by HW
7278 		 * (the DRM ABI requiring this value to be located in fb at
7279 		 * offset 0 of cc plane, plane #2 previous generations or
7280 		 * plane #1 for flat ccs):
7281 		 * - 4 x 4 bytes per-channel value
7282 		 *   (in surface type specific float/int format provided by the fb user)
7283 		 * - 8 bytes native color value used by the display
7284 		 *   (converted/written by GPU during a fast clear operation using the
7285 		 *    above per-channel values)
7286 		 *
7287 		 * The commit's FB prepare hook already ensured that FB obj is pinned and the
7288 		 * caller made sure that the object is synced wrt. the related color clear value
7289 		 * GPU write on it.
7290 		 */
7291 		ret = intel_bo_read_from_page(intel_fb_bo(fb),
7292 					      fb->offsets[cc_plane] + 16,
7293 					      &plane_state->ccval,
7294 					      sizeof(plane_state->ccval));
7295 		/* The above could only fail if the FB obj has an unexpected backing store type. */
7296 		drm_WARN_ON(display->drm, ret);
7297 	}
7298 }
7299 
7300 static void intel_atomic_dsb_prepare(struct intel_atomic_state *state,
7301 				     struct intel_crtc *crtc)
7302 {
7303 	struct intel_display *display = to_intel_display(state);
7304 	struct intel_crtc_state *new_crtc_state =
7305 		intel_atomic_get_new_crtc_state(state, crtc);
7306 
7307 	if (!new_crtc_state->hw.active)
7308 		return;
7309 
7310 	if (state->base.legacy_cursor_update)
7311 		return;
7312 
7313 	/* FIXME deal with everything */
7314 	new_crtc_state->use_flipq =
7315 		intel_flipq_supported(display) &&
7316 		!new_crtc_state->do_async_flip &&
7317 		!new_crtc_state->vrr.enable &&
7318 		!new_crtc_state->has_psr &&
7319 		!intel_crtc_needs_modeset(new_crtc_state) &&
7320 		!intel_crtc_needs_fastset(new_crtc_state) &&
7321 		!intel_crtc_needs_color_update(new_crtc_state);
7322 
7323 	new_crtc_state->use_dsb =
7324 		!new_crtc_state->use_flipq &&
7325 		!new_crtc_state->do_async_flip &&
7326 		(DISPLAY_VER(display) >= 20 || !new_crtc_state->has_psr) &&
7327 		!intel_crtc_needs_modeset(new_crtc_state) &&
7328 		!intel_crtc_needs_fastset(new_crtc_state);
7329 
7330 	intel_color_prepare_commit(state, crtc);
7331 }
7332 
7333 static void intel_atomic_dsb_finish(struct intel_atomic_state *state,
7334 				    struct intel_crtc *crtc)
7335 {
7336 	struct intel_display *display = to_intel_display(state);
7337 	struct intel_crtc_state *new_crtc_state =
7338 		intel_atomic_get_new_crtc_state(state, crtc);
7339 	unsigned int size = new_crtc_state->plane_color_changed ? 8192 : 1024;
7340 
7341 	if (!new_crtc_state->use_flipq &&
7342 	    !new_crtc_state->use_dsb &&
7343 	    !new_crtc_state->dsb_color)
7344 		return;
7345 
7346 	/*
7347 	 * Rough estimate:
7348 	 * ~64 registers per each plane * 8 planes = 512
7349 	 * Double that for pipe stuff and other overhead.
7350 	 * ~4913 registers for 3DLUT
7351 	 * ~200 color registers * 3 HDR planes
7352 	 */
7353 	new_crtc_state->dsb_commit = intel_dsb_prepare(state, crtc, INTEL_DSB_0,
7354 						       new_crtc_state->use_dsb ||
7355 						       new_crtc_state->use_flipq ? size : 16);
7356 	if (!new_crtc_state->dsb_commit) {
7357 		new_crtc_state->use_flipq = false;
7358 		new_crtc_state->use_dsb = false;
7359 		intel_color_cleanup_commit(new_crtc_state);
7360 		return;
7361 	}
7362 
7363 	if (new_crtc_state->use_flipq || new_crtc_state->use_dsb) {
7364 		/* Wa_18034343758 */
7365 		if (new_crtc_state->use_flipq)
7366 			intel_flipq_wait_dmc_halt(new_crtc_state->dsb_commit, crtc);
7367 
7368 		if (new_crtc_state->vrr.dc_balance.enable) {
7369 			/*
7370 			 * Pause the DMC DC balancing for the remainder of
7371 			 * the commit so that vmin/vmax won't change after
7372 			 * we've baked them into the DSB vblank evasion
7373 			 * commands.
7374 			 *
7375 			 * FIXME maybe need a small delay here to make sure
7376 			 * DMC has finished updating the values? Or we need
7377 			 * a better DMC<->driver protocol that gives is real
7378 			 * guarantees about that...
7379 			 */
7380 			intel_pipedmc_dcb_disable(NULL, crtc);
7381 		}
7382 
7383 		if (intel_crtc_needs_color_update(new_crtc_state))
7384 			intel_color_commit_noarm(new_crtc_state->dsb_commit,
7385 						 new_crtc_state);
7386 		intel_crtc_planes_update_noarm(new_crtc_state->dsb_commit,
7387 					       state, crtc);
7388 
7389 		/*
7390 		 * Ensure we have "Frame Change" event when PSR state is
7391 		 * SRDENT(PSR1) or DEEP_SLEEP(PSR2). Otherwise DSB vblank
7392 		 * evasion hangs as PIPEDSL is reading as 0.
7393 		 */
7394 		intel_psr_trigger_frame_change_event(new_crtc_state->dsb_commit,
7395 						     state, crtc);
7396 
7397 		if (new_crtc_state->use_dsb)
7398 			intel_dsb_vblank_evade(state, new_crtc_state->dsb_commit);
7399 
7400 		if (intel_crtc_needs_color_update(new_crtc_state))
7401 			intel_color_commit_arm(new_crtc_state->dsb_commit,
7402 					       new_crtc_state);
7403 		bdw_set_pipe_misc(new_crtc_state->dsb_commit,
7404 				  new_crtc_state);
7405 		intel_psr2_program_trans_man_trk_ctl(new_crtc_state->dsb_commit,
7406 						     new_crtc_state);
7407 		intel_crtc_planes_update_arm(new_crtc_state->dsb_commit,
7408 					     state, crtc);
7409 
7410 		if (DISPLAY_VER(display) >= 9)
7411 			skl_detach_scalers(new_crtc_state->dsb_commit,
7412 					   new_crtc_state);
7413 
7414 		/* Wa_18034343758 */
7415 		if (new_crtc_state->use_flipq)
7416 			intel_flipq_unhalt_dmc(new_crtc_state->dsb_commit, crtc);
7417 	}
7418 
7419 	if (intel_color_uses_chained_dsb(new_crtc_state))
7420 		intel_dsb_chain(state, new_crtc_state->dsb_commit,
7421 				new_crtc_state->dsb_color, true);
7422 	else if (intel_color_uses_gosub_dsb(new_crtc_state))
7423 		intel_dsb_gosub(new_crtc_state->dsb_commit,
7424 				new_crtc_state->dsb_color);
7425 
7426 	if (new_crtc_state->use_dsb && !intel_color_uses_chained_dsb(new_crtc_state)) {
7427 		/*
7428 		 * Dsb wait vblank may or may not skip. Let's remove it for PSR
7429 		 * trans push case to ensure we are not waiting two vblanks
7430 		 */
7431 		if (!intel_psr_use_trans_push(new_crtc_state))
7432 			intel_dsb_wait_vblanks(new_crtc_state->dsb_commit, 1);
7433 
7434 		intel_vrr_send_push(new_crtc_state->dsb_commit, new_crtc_state);
7435 
7436 		/*
7437 		 * Wait for idle is needed for corner case where PSR HW
7438 		 * is transitioning into DEEP_SLEEP/SRDENT_OFF when
7439 		 * new Frame Change event comes in. It is ok to do it
7440 		 * here for both Frame Change mechanism (trans push
7441 		 * and register write).
7442 		 */
7443 		intel_psr_wait_for_idle_dsb(new_crtc_state->dsb_commit,
7444 					    new_crtc_state);
7445 
7446 		/*
7447 		 * In case PSR uses trans push as a "frame change" event and
7448 		 * VRR is not in use we need to wait vblank. Otherwise we may
7449 		 * miss selective updates. DSB skips all waits while PSR is
7450 		 * active. Check push send is skipped as well because trans push
7451 		 * send bit is not reset by the HW if VRR is not
7452 		 * enabled -> we may start configuring new selective
7453 		 * update while previous is not complete.
7454 		 */
7455 		if (intel_psr_use_trans_push(new_crtc_state))
7456 			intel_dsb_wait_vblanks(new_crtc_state->dsb_commit, 1);
7457 
7458 		intel_dsb_wait_for_delayed_vblank(state, new_crtc_state->dsb_commit);
7459 		intel_vrr_check_push_sent(new_crtc_state->dsb_commit,
7460 					  new_crtc_state);
7461 
7462 		if (new_crtc_state->vrr.dc_balance.enable)
7463 			intel_pipedmc_dcb_enable(new_crtc_state->dsb_commit, crtc);
7464 
7465 		intel_dsb_interrupt(new_crtc_state->dsb_commit);
7466 	}
7467 
7468 	intel_dsb_finish(new_crtc_state->dsb_commit);
7469 }
7470 
7471 static void intel_atomic_commit_tail(struct intel_atomic_state *state)
7472 {
7473 	struct intel_display *display = to_intel_display(state);
7474 	struct intel_uncore *uncore = to_intel_uncore(display->drm);
7475 	struct intel_crtc_state *new_crtc_state, *old_crtc_state;
7476 	struct intel_crtc *crtc;
7477 	struct intel_power_domain_mask put_domains[I915_MAX_PIPES] = {};
7478 	struct ref_tracker *wakeref = NULL;
7479 	int i;
7480 
7481 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
7482 		intel_atomic_dsb_prepare(state, crtc);
7483 
7484 	intel_atomic_commit_fence_wait(state);
7485 
7486 	intel_td_flush(display);
7487 
7488 	intel_atomic_prepare_plane_clear_colors(state);
7489 
7490 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
7491 		intel_fbc_prepare_dirty_rect(state, crtc);
7492 
7493 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
7494 		intel_atomic_dsb_finish(state, crtc);
7495 
7496 	drm_atomic_helper_wait_for_dependencies(&state->base);
7497 	drm_dp_mst_atomic_wait_for_dependencies(&state->base);
7498 	intel_atomic_global_state_wait_for_dependencies(state);
7499 
7500 	/*
7501 	 * During full modesets we write a lot of registers, wait
7502 	 * for PLLs, etc. Doing that while DC states are enabled
7503 	 * is not a good idea.
7504 	 *
7505 	 * During fastsets and other updates we also need to
7506 	 * disable DC states due to the following scenario:
7507 	 * 1. DC5 exit and PSR exit happen
7508 	 * 2. Some or all _noarm() registers are written
7509 	 * 3. Due to some long delay PSR is re-entered
7510 	 * 4. DC5 entry -> DMC saves the already written new
7511 	 *    _noarm() registers and the old not yet written
7512 	 *    _arm() registers
7513 	 * 5. DC5 exit -> DMC restores a mixture of old and
7514 	 *    new register values and arms the update
7515 	 * 6. PSR exit -> hardware latches a mixture of old and
7516 	 *    new register values -> corrupted frame, or worse
7517 	 * 7. New _arm() registers are finally written
7518 	 * 8. Hardware finally latches a complete set of new
7519 	 *    register values, and subsequent frames will be OK again
7520 	 *
7521 	 * Also note that due to the pipe CSC hardware issues on
7522 	 * SKL/GLK DC states must remain off until the pipe CSC
7523 	 * state readout has happened. Otherwise we risk corrupting
7524 	 * the CSC latched register values with the readout (see
7525 	 * skl_read_csc() and skl_color_commit_noarm()).
7526 	 */
7527 	wakeref = intel_display_power_get(display, POWER_DOMAIN_DC_OFF);
7528 
7529 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
7530 					    new_crtc_state, i) {
7531 		if (intel_crtc_needs_modeset(new_crtc_state) ||
7532 		    intel_crtc_needs_fastset(new_crtc_state))
7533 			intel_modeset_get_crtc_power_domains(new_crtc_state, &put_domains[crtc->pipe]);
7534 	}
7535 
7536 	intel_commit_modeset_disables(state);
7537 
7538 	intel_dp_tunnel_atomic_alloc_bw(state);
7539 
7540 	/* FIXME: Eventually get rid of our crtc->config pointer */
7541 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
7542 		crtc->config = new_crtc_state;
7543 
7544 	/*
7545 	 * In XE_LPD+ Pmdemand combines many parameters such as voltage index,
7546 	 * plls, cdclk frequency, QGV point selection parameter etc. Voltage
7547 	 * index, cdclk/ddiclk frequencies are supposed to be configured before
7548 	 * the cdclk config is set.
7549 	 */
7550 	intel_pmdemand_pre_plane_update(state);
7551 
7552 	if (state->modeset)
7553 		drm_atomic_helper_update_legacy_modeset_state(display->drm, &state->base);
7554 
7555 	intel_set_cdclk_pre_plane_update(state);
7556 
7557 	if (state->modeset)
7558 		intel_modeset_verify_disabled(state);
7559 
7560 	intel_sagv_pre_plane_update(state);
7561 
7562 	/* Complete the events for pipes that have now been disabled */
7563 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7564 		bool modeset = intel_crtc_needs_modeset(new_crtc_state);
7565 
7566 		/* Complete events for now disable pipes here. */
7567 		if (modeset && !new_crtc_state->hw.active && new_crtc_state->uapi.event) {
7568 			spin_lock_irq(&display->drm->event_lock);
7569 			drm_crtc_send_vblank_event(&crtc->base,
7570 						   new_crtc_state->uapi.event);
7571 			spin_unlock_irq(&display->drm->event_lock);
7572 
7573 			new_crtc_state->uapi.event = NULL;
7574 		}
7575 	}
7576 
7577 	intel_encoders_update_prepare(state);
7578 
7579 	intel_dbuf_pre_plane_update(state);
7580 
7581 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7582 		if (new_crtc_state->do_async_flip)
7583 			intel_crtc_enable_flip_done(state, crtc);
7584 	}
7585 
7586 	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
7587 	display->funcs.display->commit_modeset_enables(state);
7588 
7589 	/* FIXME probably need to sequence this properly */
7590 	intel_program_dpkgc_latency(state);
7591 
7592 	intel_wait_for_vblank_workers(state);
7593 
7594 	/* FIXME: We should call drm_atomic_helper_commit_hw_done() here
7595 	 * already, but still need the state for the delayed optimization. To
7596 	 * fix this:
7597 	 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
7598 	 * - schedule that vblank worker _before_ calling hw_done
7599 	 * - at the start of commit_tail, cancel it _synchrously
7600 	 * - switch over to the vblank wait helper in the core after that since
7601 	 *   we don't need out special handling any more.
7602 	 */
7603 	drm_atomic_helper_wait_for_flip_done(display->drm, &state->base);
7604 
7605 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7606 		if (new_crtc_state->do_async_flip)
7607 			intel_crtc_disable_flip_done(state, crtc);
7608 
7609 		intel_atomic_dsb_wait_commit(new_crtc_state);
7610 
7611 		if (!state->base.legacy_cursor_update && !new_crtc_state->use_dsb)
7612 			intel_vrr_check_push_sent(NULL, new_crtc_state);
7613 
7614 		if (new_crtc_state->use_flipq)
7615 			intel_flipq_disable(new_crtc_state);
7616 	}
7617 
7618 	/*
7619 	 * Now that the vblank has passed, we can go ahead and program the
7620 	 * optimal watermarks on platforms that need two-step watermark
7621 	 * programming.
7622 	 *
7623 	 * TODO: Move this (and other cleanup) to an async worker eventually.
7624 	 */
7625 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
7626 					    new_crtc_state, i) {
7627 		/*
7628 		 * Gen2 reports pipe underruns whenever all planes are disabled.
7629 		 * So re-enable underrun reporting after some planes get enabled.
7630 		 *
7631 		 * We do this before .optimize_watermarks() so that we have a
7632 		 * chance of catching underruns with the intermediate watermarks
7633 		 * vs. the new plane configuration.
7634 		 */
7635 		if (DISPLAY_VER(display) == 2 && planes_enabling(old_crtc_state, new_crtc_state))
7636 			intel_set_cpu_fifo_underrun_reporting(display, crtc->pipe, true);
7637 
7638 		intel_optimize_watermarks(state, crtc);
7639 	}
7640 
7641 	intel_dbuf_post_plane_update(state);
7642 
7643 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
7644 		intel_post_plane_update(state, crtc);
7645 
7646 		intel_modeset_put_crtc_power_domains(crtc, &put_domains[crtc->pipe]);
7647 
7648 		intel_modeset_verify_crtc(state, crtc);
7649 
7650 		intel_post_plane_update_after_readout(state, crtc);
7651 
7652 		/*
7653 		 * DSB cleanup is done in cleanup_work aligning with framebuffer
7654 		 * cleanup. So copy and reset the dsb structure to sync with
7655 		 * commit_done and later do dsb cleanup in cleanup_work.
7656 		 *
7657 		 * FIXME get rid of this funny new->old swapping
7658 		 */
7659 		old_crtc_state->dsb_color = fetch_and_zero(&new_crtc_state->dsb_color);
7660 		old_crtc_state->dsb_commit = fetch_and_zero(&new_crtc_state->dsb_commit);
7661 	}
7662 
7663 	/* Underruns don't always raise interrupts, so check manually */
7664 	intel_check_cpu_fifo_underruns(display);
7665 	intel_check_pch_fifo_underruns(display);
7666 
7667 	if (state->modeset)
7668 		intel_verify_planes(state);
7669 
7670 	intel_sagv_post_plane_update(state);
7671 	intel_set_cdclk_post_plane_update(state);
7672 	intel_pmdemand_post_plane_update(state);
7673 
7674 	drm_atomic_helper_commit_hw_done(&state->base);
7675 	intel_atomic_global_state_commit_done(state);
7676 
7677 	if (state->modeset) {
7678 		/* As one of the primary mmio accessors, KMS has a high
7679 		 * likelihood of triggering bugs in unclaimed access. After we
7680 		 * finish modesetting, see if an error has been flagged, and if
7681 		 * so enable debugging for the next modeset - and hope we catch
7682 		 * the culprit.
7683 		 */
7684 		intel_uncore_arm_unclaimed_mmio_detection(uncore);
7685 	}
7686 	/*
7687 	 * Delay re-enabling DC states by 17 ms to avoid the off->on->off
7688 	 * toggling overhead at and above 60 FPS.
7689 	 */
7690 	intel_display_power_put_async_delay(display, POWER_DOMAIN_DC_OFF, wakeref, 17);
7691 	intel_display_rpm_put(display, state->wakeref);
7692 
7693 	/*
7694 	 * Defer the cleanup of the old state to a separate worker to not
7695 	 * impede the current task (userspace for blocking modesets) that
7696 	 * are executed inline. For out-of-line asynchronous modesets/flips,
7697 	 * deferring to a new worker seems overkill, but we would place a
7698 	 * schedule point (cond_resched()) here anyway to keep latencies
7699 	 * down.
7700 	 */
7701 	INIT_WORK(&state->cleanup_work, intel_atomic_cleanup_work);
7702 	queue_work(display->wq.cleanup, &state->cleanup_work);
7703 }
7704 
7705 static void intel_atomic_commit_work(struct work_struct *work)
7706 {
7707 	struct intel_atomic_state *state =
7708 		container_of(work, struct intel_atomic_state, base.commit_work);
7709 
7710 	intel_atomic_commit_tail(state);
7711 }
7712 
7713 static void intel_atomic_track_fbs(struct intel_atomic_state *state)
7714 {
7715 	struct intel_plane_state *old_plane_state, *new_plane_state;
7716 	struct intel_plane *plane;
7717 	int i;
7718 
7719 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
7720 					     new_plane_state, i)
7721 		intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
7722 					to_intel_frontbuffer(new_plane_state->hw.fb),
7723 					plane->frontbuffer_bit);
7724 }
7725 
7726 static int intel_atomic_setup_commit(struct intel_atomic_state *state, bool nonblock)
7727 {
7728 	int ret;
7729 
7730 	ret = drm_atomic_helper_setup_commit(&state->base, nonblock);
7731 	if (ret)
7732 		return ret;
7733 
7734 	ret = intel_atomic_global_state_setup_commit(state);
7735 	if (ret)
7736 		return ret;
7737 
7738 	return 0;
7739 }
7740 
7741 static int intel_atomic_swap_state(struct intel_atomic_state *state)
7742 {
7743 	int ret;
7744 
7745 	ret = drm_atomic_helper_swap_state(&state->base, true);
7746 	if (ret)
7747 		return ret;
7748 
7749 	intel_atomic_swap_global_state(state);
7750 
7751 	intel_dpll_swap_state(state);
7752 
7753 	intel_atomic_track_fbs(state);
7754 
7755 	return 0;
7756 }
7757 
7758 int intel_atomic_commit(struct drm_device *dev, struct drm_atomic_state *_state,
7759 			bool nonblock)
7760 {
7761 	struct intel_display *display = to_intel_display(dev);
7762 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
7763 	int ret = 0;
7764 
7765 	state->wakeref = intel_display_rpm_get(display);
7766 
7767 	/*
7768 	 * The intel_legacy_cursor_update() fast path takes care
7769 	 * of avoiding the vblank waits for simple cursor
7770 	 * movement and flips. For cursor on/off and size changes,
7771 	 * we want to perform the vblank waits so that watermark
7772 	 * updates happen during the correct frames. Gen9+ have
7773 	 * double buffered watermarks and so shouldn't need this.
7774 	 *
7775 	 * Unset state->legacy_cursor_update before the call to
7776 	 * drm_atomic_helper_setup_commit() because otherwise
7777 	 * drm_atomic_helper_wait_for_flip_done() is a noop and
7778 	 * we get FIFO underruns because we didn't wait
7779 	 * for vblank.
7780 	 *
7781 	 * FIXME doing watermarks and fb cleanup from a vblank worker
7782 	 * (assuming we had any) would solve these problems.
7783 	 */
7784 	if (DISPLAY_VER(display) < 9 && state->base.legacy_cursor_update) {
7785 		struct intel_crtc_state *new_crtc_state;
7786 		struct intel_crtc *crtc;
7787 		int i;
7788 
7789 		for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
7790 			if (new_crtc_state->wm.need_postvbl_update ||
7791 			    new_crtc_state->update_wm_post)
7792 				state->base.legacy_cursor_update = false;
7793 	}
7794 
7795 	ret = intel_atomic_prepare_commit(state);
7796 	if (ret) {
7797 		drm_dbg_atomic(display->drm,
7798 			       "Preparing state failed with %i\n", ret);
7799 		intel_display_rpm_put(display, state->wakeref);
7800 		return ret;
7801 	}
7802 
7803 	ret = intel_atomic_setup_commit(state, nonblock);
7804 	if (!ret)
7805 		ret = intel_atomic_swap_state(state);
7806 
7807 	if (ret) {
7808 		drm_atomic_helper_unprepare_planes(dev, &state->base);
7809 		intel_display_rpm_put(display, state->wakeref);
7810 		return ret;
7811 	}
7812 
7813 	drm_atomic_state_get(&state->base);
7814 	INIT_WORK(&state->base.commit_work, intel_atomic_commit_work);
7815 
7816 	if (nonblock && state->modeset) {
7817 		queue_work(display->wq.modeset, &state->base.commit_work);
7818 	} else if (nonblock) {
7819 		queue_work(display->wq.flip, &state->base.commit_work);
7820 	} else {
7821 		if (state->modeset)
7822 			flush_workqueue(display->wq.modeset);
7823 		intel_atomic_commit_tail(state);
7824 	}
7825 
7826 	return 0;
7827 }
7828 
7829 static u32 intel_encoder_possible_clones(struct intel_encoder *encoder)
7830 {
7831 	struct intel_display *display = to_intel_display(encoder);
7832 	struct intel_encoder *source_encoder;
7833 	u32 possible_clones = 0;
7834 
7835 	for_each_intel_encoder(display->drm, source_encoder) {
7836 		if (encoders_cloneable(encoder, source_encoder))
7837 			possible_clones |= drm_encoder_mask(&source_encoder->base);
7838 	}
7839 
7840 	return possible_clones;
7841 }
7842 
7843 static u32 intel_encoder_possible_crtcs(struct intel_encoder *encoder)
7844 {
7845 	struct intel_display *display = to_intel_display(encoder);
7846 	struct intel_crtc *crtc;
7847 	u32 possible_crtcs = 0;
7848 
7849 	for_each_intel_crtc_in_pipe_mask(display->drm, crtc, encoder->pipe_mask)
7850 		possible_crtcs |= drm_crtc_mask(&crtc->base);
7851 
7852 	return possible_crtcs;
7853 }
7854 
7855 static bool ilk_has_edp_a(struct intel_display *display)
7856 {
7857 	if (!display->platform.mobile)
7858 		return false;
7859 
7860 	if ((intel_de_read(display, DP_A) & DP_DETECTED) == 0)
7861 		return false;
7862 
7863 	if (display->platform.ironlake && (intel_de_read(display, FUSE_STRAP) & ILK_eDP_A_DISABLE))
7864 		return false;
7865 
7866 	return true;
7867 }
7868 
7869 static bool intel_ddi_crt_present(struct intel_display *display)
7870 {
7871 	if (DISPLAY_VER(display) >= 9)
7872 		return false;
7873 
7874 	if (display->platform.haswell_ult || display->platform.broadwell_ult)
7875 		return false;
7876 
7877 	if (HAS_PCH_LPT_H(display) &&
7878 	    intel_de_read(display, SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
7879 		return false;
7880 
7881 	/* DDI E can't be used if DDI A requires 4 lanes */
7882 	if (intel_de_read(display, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
7883 		return false;
7884 
7885 	if (!display->vbt.int_crt_support)
7886 		return false;
7887 
7888 	return true;
7889 }
7890 
7891 bool assert_port_valid(struct intel_display *display, enum port port)
7892 {
7893 	return !drm_WARN(display->drm, !(DISPLAY_RUNTIME_INFO(display)->port_mask & BIT(port)),
7894 			 "Platform does not support port %c\n", port_name(port));
7895 }
7896 
7897 void intel_setup_outputs(struct intel_display *display)
7898 {
7899 	struct intel_encoder *encoder;
7900 	bool dpd_is_edp = false;
7901 
7902 	intel_pps_unlock_regs_wa(display);
7903 
7904 	if (!HAS_DISPLAY(display))
7905 		return;
7906 
7907 	if (HAS_DDI(display)) {
7908 		if (intel_ddi_crt_present(display))
7909 			intel_crt_init(display);
7910 
7911 		intel_bios_for_each_encoder(display, intel_ddi_init);
7912 
7913 		if (display->platform.geminilake || display->platform.broxton)
7914 			vlv_dsi_init(display);
7915 	} else if (HAS_PCH_SPLIT(display)) {
7916 		int found;
7917 
7918 		/*
7919 		 * intel_edp_init_connector() depends on this completing first,
7920 		 * to prevent the registration of both eDP and LVDS and the
7921 		 * incorrect sharing of the PPS.
7922 		 */
7923 		intel_lvds_init(display);
7924 		intel_crt_init(display);
7925 
7926 		dpd_is_edp = intel_dp_is_port_edp(display, PORT_D);
7927 
7928 		if (ilk_has_edp_a(display))
7929 			g4x_dp_init(display, DP_A, PORT_A);
7930 
7931 		if (intel_de_read(display, PCH_HDMIB) & SDVO_DETECTED) {
7932 			/* PCH SDVOB multiplex with HDMIB */
7933 			found = intel_sdvo_init(display, PCH_SDVOB, PORT_B);
7934 			if (!found)
7935 				g4x_hdmi_init(display, PCH_HDMIB, PORT_B);
7936 			if (!found && (intel_de_read(display, PCH_DP_B) & DP_DETECTED))
7937 				g4x_dp_init(display, PCH_DP_B, PORT_B);
7938 		}
7939 
7940 		if (intel_de_read(display, PCH_HDMIC) & SDVO_DETECTED)
7941 			g4x_hdmi_init(display, PCH_HDMIC, PORT_C);
7942 
7943 		if (!dpd_is_edp && intel_de_read(display, PCH_HDMID) & SDVO_DETECTED)
7944 			g4x_hdmi_init(display, PCH_HDMID, PORT_D);
7945 
7946 		if (intel_de_read(display, PCH_DP_C) & DP_DETECTED)
7947 			g4x_dp_init(display, PCH_DP_C, PORT_C);
7948 
7949 		if (intel_de_read(display, PCH_DP_D) & DP_DETECTED)
7950 			g4x_dp_init(display, PCH_DP_D, PORT_D);
7951 	} else if (display->platform.valleyview || display->platform.cherryview) {
7952 		bool has_edp, has_port;
7953 
7954 		if (display->platform.valleyview && display->vbt.int_crt_support)
7955 			intel_crt_init(display);
7956 
7957 		/*
7958 		 * The DP_DETECTED bit is the latched state of the DDC
7959 		 * SDA pin at boot. However since eDP doesn't require DDC
7960 		 * (no way to plug in a DP->HDMI dongle) the DDC pins for
7961 		 * eDP ports may have been muxed to an alternate function.
7962 		 * Thus we can't rely on the DP_DETECTED bit alone to detect
7963 		 * eDP ports. Consult the VBT as well as DP_DETECTED to
7964 		 * detect eDP ports.
7965 		 *
7966 		 * Sadly the straps seem to be missing sometimes even for HDMI
7967 		 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
7968 		 * and VBT for the presence of the port. Additionally we can't
7969 		 * trust the port type the VBT declares as we've seen at least
7970 		 * HDMI ports that the VBT claim are DP or eDP.
7971 		 */
7972 		has_edp = intel_dp_is_port_edp(display, PORT_B);
7973 		has_port = intel_bios_is_port_present(display, PORT_B);
7974 		if (intel_de_read(display, VLV_DP_B) & DP_DETECTED || has_port)
7975 			has_edp &= g4x_dp_init(display, VLV_DP_B, PORT_B);
7976 		if ((intel_de_read(display, VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
7977 			g4x_hdmi_init(display, VLV_HDMIB, PORT_B);
7978 
7979 		has_edp = intel_dp_is_port_edp(display, PORT_C);
7980 		has_port = intel_bios_is_port_present(display, PORT_C);
7981 		if (intel_de_read(display, VLV_DP_C) & DP_DETECTED || has_port)
7982 			has_edp &= g4x_dp_init(display, VLV_DP_C, PORT_C);
7983 		if ((intel_de_read(display, VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
7984 			g4x_hdmi_init(display, VLV_HDMIC, PORT_C);
7985 
7986 		if (display->platform.cherryview) {
7987 			/*
7988 			 * eDP not supported on port D,
7989 			 * so no need to worry about it
7990 			 */
7991 			has_port = intel_bios_is_port_present(display, PORT_D);
7992 			if (intel_de_read(display, CHV_DP_D) & DP_DETECTED || has_port)
7993 				g4x_dp_init(display, CHV_DP_D, PORT_D);
7994 			if (intel_de_read(display, CHV_HDMID) & SDVO_DETECTED || has_port)
7995 				g4x_hdmi_init(display, CHV_HDMID, PORT_D);
7996 		}
7997 
7998 		vlv_dsi_init(display);
7999 	} else if (display->platform.pineview) {
8000 		intel_lvds_init(display);
8001 		intel_crt_init(display);
8002 	} else if (IS_DISPLAY_VER(display, 3, 4)) {
8003 		bool found = false;
8004 
8005 		if (display->platform.mobile)
8006 			intel_lvds_init(display);
8007 
8008 		intel_crt_init(display);
8009 
8010 		if (intel_de_read(display, GEN3_SDVOB) & SDVO_DETECTED) {
8011 			drm_dbg_kms(display->drm, "probing SDVOB\n");
8012 			found = intel_sdvo_init(display, GEN3_SDVOB, PORT_B);
8013 			if (!found && display->platform.g4x) {
8014 				drm_dbg_kms(display->drm,
8015 					    "probing HDMI on SDVOB\n");
8016 				g4x_hdmi_init(display, GEN4_HDMIB, PORT_B);
8017 			}
8018 
8019 			if (!found && display->platform.g4x)
8020 				g4x_dp_init(display, DP_B, PORT_B);
8021 		}
8022 
8023 		/* Before G4X SDVOC doesn't have its own detect register */
8024 
8025 		if (intel_de_read(display, GEN3_SDVOB) & SDVO_DETECTED) {
8026 			drm_dbg_kms(display->drm, "probing SDVOC\n");
8027 			found = intel_sdvo_init(display, GEN3_SDVOC, PORT_C);
8028 		}
8029 
8030 		if (!found && (intel_de_read(display, GEN3_SDVOC) & SDVO_DETECTED)) {
8031 
8032 			if (display->platform.g4x) {
8033 				drm_dbg_kms(display->drm,
8034 					    "probing HDMI on SDVOC\n");
8035 				g4x_hdmi_init(display, GEN4_HDMIC, PORT_C);
8036 			}
8037 			if (display->platform.g4x)
8038 				g4x_dp_init(display, DP_C, PORT_C);
8039 		}
8040 
8041 		if (display->platform.g4x && (intel_de_read(display, DP_D) & DP_DETECTED))
8042 			g4x_dp_init(display, DP_D, PORT_D);
8043 
8044 		if (SUPPORTS_TV(display))
8045 			intel_tv_init(display);
8046 	} else if (DISPLAY_VER(display) == 2) {
8047 		if (display->platform.i85x)
8048 			intel_lvds_init(display);
8049 
8050 		intel_crt_init(display);
8051 		intel_dvo_init(display);
8052 	}
8053 
8054 	for_each_intel_encoder(display->drm, encoder) {
8055 		encoder->base.possible_crtcs =
8056 			intel_encoder_possible_crtcs(encoder);
8057 		encoder->base.possible_clones =
8058 			intel_encoder_possible_clones(encoder);
8059 	}
8060 
8061 	intel_init_pch_refclk(display);
8062 
8063 	drm_helper_move_panel_connectors_to_head(display->drm);
8064 }
8065 
8066 int intel_max_uncompressed_dotclock(struct intel_display *display)
8067 {
8068 	int max_dotclock = display->cdclk.max_dotclk_freq;
8069 	int limit = max_dotclock;
8070 
8071 	if (DISPLAY_VERx100(display) == 3002)
8072 		limit = 937500;
8073 	else if (DISPLAY_VER(display) >= 30)
8074 		limit = 1350000;
8075 	/*
8076 	 * Note: For other platforms though there are limits given
8077 	 * in the Bspec, however the limit is intentionally not
8078 	 * enforced to avoid regressions, unless real issues are
8079 	 * observed.
8080 	 */
8081 
8082 	return min(max_dotclock, limit);
8083 }
8084 
8085 static int max_dotclock(struct intel_display *display)
8086 {
8087 	int max_dotclock = display->cdclk.max_dotclk_freq;
8088 
8089 	if (HAS_ULTRAJOINER(display))
8090 		max_dotclock *= 4;
8091 	else if (HAS_UNCOMPRESSED_JOINER(display) || HAS_BIGJOINER(display))
8092 		max_dotclock *= 2;
8093 
8094 	return max_dotclock;
8095 }
8096 
8097 enum drm_mode_status intel_mode_valid(struct drm_device *dev,
8098 				      const struct drm_display_mode *mode)
8099 {
8100 	struct intel_display *display = to_intel_display(dev);
8101 	int hdisplay_max, htotal_max;
8102 	int vdisplay_max, vtotal_max;
8103 
8104 	/*
8105 	 * Can't reject DBLSCAN here because Xorg ddxen can add piles
8106 	 * of DBLSCAN modes to the output's mode list when they detect
8107 	 * the scaling mode property on the connector. And they don't
8108 	 * ask the kernel to validate those modes in any way until
8109 	 * modeset time at which point the client gets a protocol error.
8110 	 * So in order to not upset those clients we silently ignore the
8111 	 * DBLSCAN flag on such connectors. For other connectors we will
8112 	 * reject modes with the DBLSCAN flag in encoder->compute_config().
8113 	 * And we always reject DBLSCAN modes in connector->mode_valid()
8114 	 * as we never want such modes on the connector's mode list.
8115 	 */
8116 
8117 	if (mode->vscan > 1)
8118 		return MODE_NO_VSCAN;
8119 
8120 	if (mode->flags & DRM_MODE_FLAG_HSKEW)
8121 		return MODE_H_ILLEGAL;
8122 
8123 	if (mode->flags & (DRM_MODE_FLAG_CSYNC |
8124 			   DRM_MODE_FLAG_NCSYNC |
8125 			   DRM_MODE_FLAG_PCSYNC))
8126 		return MODE_HSYNC;
8127 
8128 	if (mode->flags & (DRM_MODE_FLAG_BCAST |
8129 			   DRM_MODE_FLAG_PIXMUX |
8130 			   DRM_MODE_FLAG_CLKDIV2))
8131 		return MODE_BAD;
8132 
8133 	/*
8134 	 * Reject clearly excessive dotclocks early to
8135 	 * avoid having to worry about huge integers later.
8136 	 */
8137 	if (mode->clock > max_dotclock(display))
8138 		return MODE_CLOCK_HIGH;
8139 
8140 	/* Transcoder timing limits */
8141 	if (DISPLAY_VER(display) >= 11) {
8142 		hdisplay_max = 16384;
8143 		vdisplay_max = 8192;
8144 		htotal_max = 16384;
8145 		vtotal_max = 8192;
8146 	} else if (DISPLAY_VER(display) >= 9 ||
8147 		   display->platform.broadwell || display->platform.haswell) {
8148 		hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
8149 		vdisplay_max = 4096;
8150 		htotal_max = 8192;
8151 		vtotal_max = 8192;
8152 	} else if (DISPLAY_VER(display) >= 3) {
8153 		hdisplay_max = 4096;
8154 		vdisplay_max = 4096;
8155 		htotal_max = 8192;
8156 		vtotal_max = 8192;
8157 	} else {
8158 		hdisplay_max = 2048;
8159 		vdisplay_max = 2048;
8160 		htotal_max = 4096;
8161 		vtotal_max = 4096;
8162 	}
8163 
8164 	if (mode->hdisplay > hdisplay_max ||
8165 	    mode->hsync_start > htotal_max ||
8166 	    mode->hsync_end > htotal_max ||
8167 	    mode->htotal > htotal_max)
8168 		return MODE_H_ILLEGAL;
8169 
8170 	if (mode->vdisplay > vdisplay_max ||
8171 	    mode->vsync_start > vtotal_max ||
8172 	    mode->vsync_end > vtotal_max ||
8173 	    mode->vtotal > vtotal_max)
8174 		return MODE_V_ILLEGAL;
8175 
8176 	/*
8177 	 * WM_LINETIME only goes up to (almost) 64 usec, and also
8178 	 * knowing that the linetime is always bounded will ease the
8179 	 * mind during various calculations.
8180 	 */
8181 	if (DIV_ROUND_UP(mode->htotal * 1000, mode->clock) > 64)
8182 		return MODE_H_ILLEGAL;
8183 
8184 	return MODE_OK;
8185 }
8186 
8187 enum drm_mode_status intel_cpu_transcoder_mode_valid(struct intel_display *display,
8188 						     const struct drm_display_mode *mode)
8189 {
8190 	/*
8191 	 * Additional transcoder timing limits,
8192 	 * excluding BXT/GLK DSI transcoders.
8193 	 */
8194 	if (DISPLAY_VER(display) >= 5) {
8195 		if (mode->hdisplay < 64 ||
8196 		    mode->htotal - mode->hdisplay < 32)
8197 			return MODE_H_ILLEGAL;
8198 
8199 		if (mode->vtotal - mode->vdisplay < 5)
8200 			return MODE_V_ILLEGAL;
8201 	} else {
8202 		if (mode->htotal - mode->hdisplay < 32)
8203 			return MODE_H_ILLEGAL;
8204 
8205 		if (mode->vtotal - mode->vdisplay < 3)
8206 			return MODE_V_ILLEGAL;
8207 	}
8208 
8209 	/*
8210 	 * Cantiga+ cannot handle modes with a hsync front porch of 0.
8211 	 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
8212 	 */
8213 	if ((DISPLAY_VER(display) >= 5 || display->platform.g4x) &&
8214 	    mode->hsync_start == mode->hdisplay)
8215 		return MODE_H_ILLEGAL;
8216 
8217 	return MODE_OK;
8218 }
8219 
8220 enum drm_mode_status
8221 intel_mode_valid_max_plane_size(struct intel_display *display,
8222 				const struct drm_display_mode *mode,
8223 				int num_joined_pipes)
8224 {
8225 	int plane_width_max, plane_height_max;
8226 
8227 	/*
8228 	 * intel_mode_valid() should be
8229 	 * sufficient on older platforms.
8230 	 */
8231 	if (DISPLAY_VER(display) < 9)
8232 		return MODE_OK;
8233 
8234 	/*
8235 	 * Most people will probably want a fullscreen
8236 	 * plane so let's not advertize modes that are
8237 	 * too big for that.
8238 	 */
8239 	if (DISPLAY_VER(display) >= 30) {
8240 		plane_width_max = 6144 * num_joined_pipes;
8241 		plane_height_max = 4800;
8242 	} else if (DISPLAY_VER(display) >= 11) {
8243 		plane_width_max = 5120 * num_joined_pipes;
8244 		plane_height_max = 4320;
8245 	} else {
8246 		plane_width_max = 5120;
8247 		plane_height_max = 4096;
8248 	}
8249 
8250 	if (mode->hdisplay > plane_width_max)
8251 		return MODE_H_ILLEGAL;
8252 
8253 	if (mode->vdisplay > plane_height_max)
8254 		return MODE_V_ILLEGAL;
8255 
8256 	return MODE_OK;
8257 }
8258 
8259 static const struct intel_display_funcs skl_display_funcs = {
8260 	.get_pipe_config = hsw_get_pipe_config,
8261 	.crtc_enable = hsw_crtc_enable,
8262 	.crtc_disable = hsw_crtc_disable,
8263 	.commit_modeset_enables = skl_commit_modeset_enables,
8264 	.get_initial_plane_config = skl_get_initial_plane_config,
8265 	.fixup_initial_plane_config = skl_fixup_initial_plane_config,
8266 };
8267 
8268 static const struct intel_display_funcs ddi_display_funcs = {
8269 	.get_pipe_config = hsw_get_pipe_config,
8270 	.crtc_enable = hsw_crtc_enable,
8271 	.crtc_disable = hsw_crtc_disable,
8272 	.commit_modeset_enables = intel_commit_modeset_enables,
8273 	.get_initial_plane_config = i9xx_get_initial_plane_config,
8274 	.fixup_initial_plane_config = i9xx_fixup_initial_plane_config,
8275 };
8276 
8277 static const struct intel_display_funcs pch_split_display_funcs = {
8278 	.get_pipe_config = ilk_get_pipe_config,
8279 	.crtc_enable = ilk_crtc_enable,
8280 	.crtc_disable = ilk_crtc_disable,
8281 	.commit_modeset_enables = intel_commit_modeset_enables,
8282 	.get_initial_plane_config = i9xx_get_initial_plane_config,
8283 	.fixup_initial_plane_config = i9xx_fixup_initial_plane_config,
8284 };
8285 
8286 static const struct intel_display_funcs vlv_display_funcs = {
8287 	.get_pipe_config = i9xx_get_pipe_config,
8288 	.crtc_enable = valleyview_crtc_enable,
8289 	.crtc_disable = i9xx_crtc_disable,
8290 	.commit_modeset_enables = intel_commit_modeset_enables,
8291 	.get_initial_plane_config = i9xx_get_initial_plane_config,
8292 	.fixup_initial_plane_config = i9xx_fixup_initial_plane_config,
8293 };
8294 
8295 static const struct intel_display_funcs i9xx_display_funcs = {
8296 	.get_pipe_config = i9xx_get_pipe_config,
8297 	.crtc_enable = i9xx_crtc_enable,
8298 	.crtc_disable = i9xx_crtc_disable,
8299 	.commit_modeset_enables = intel_commit_modeset_enables,
8300 	.get_initial_plane_config = i9xx_get_initial_plane_config,
8301 	.fixup_initial_plane_config = i9xx_fixup_initial_plane_config,
8302 };
8303 
8304 /**
8305  * intel_init_display_hooks - initialize the display modesetting hooks
8306  * @display: display device private
8307  */
8308 void intel_init_display_hooks(struct intel_display *display)
8309 {
8310 	if (DISPLAY_VER(display) >= 9) {
8311 		display->funcs.display = &skl_display_funcs;
8312 	} else if (HAS_DDI(display)) {
8313 		display->funcs.display = &ddi_display_funcs;
8314 	} else if (HAS_PCH_SPLIT(display)) {
8315 		display->funcs.display = &pch_split_display_funcs;
8316 	} else if (display->platform.cherryview ||
8317 		   display->platform.valleyview) {
8318 		display->funcs.display = &vlv_display_funcs;
8319 	} else {
8320 		display->funcs.display = &i9xx_display_funcs;
8321 	}
8322 }
8323 
8324 int intel_initial_commit(struct intel_display *display)
8325 {
8326 	struct drm_atomic_state *state = NULL;
8327 	struct drm_modeset_acquire_ctx ctx;
8328 	struct intel_crtc *crtc;
8329 	int ret = 0;
8330 
8331 	state = drm_atomic_state_alloc(display->drm);
8332 	if (!state)
8333 		return -ENOMEM;
8334 
8335 	drm_modeset_acquire_init(&ctx, 0);
8336 
8337 	state->acquire_ctx = &ctx;
8338 	to_intel_atomic_state(state)->internal = true;
8339 
8340 retry:
8341 	for_each_intel_crtc(display->drm, crtc) {
8342 		struct intel_crtc_state *crtc_state =
8343 			intel_atomic_get_crtc_state(state, crtc);
8344 
8345 		if (IS_ERR(crtc_state)) {
8346 			ret = PTR_ERR(crtc_state);
8347 			goto out;
8348 		}
8349 
8350 		if (!crtc_state->hw.active)
8351 			crtc_state->inherited = false;
8352 
8353 		if (crtc_state->hw.active) {
8354 			struct intel_encoder *encoder;
8355 
8356 			ret = drm_atomic_add_affected_planes(state, &crtc->base);
8357 			if (ret)
8358 				goto out;
8359 
8360 			/*
8361 			 * FIXME hack to force a LUT update to avoid the
8362 			 * plane update forcing the pipe gamma on without
8363 			 * having a proper LUT loaded. Remove once we
8364 			 * have readout for pipe gamma enable.
8365 			 */
8366 			crtc_state->uapi.color_mgmt_changed = true;
8367 
8368 			for_each_intel_encoder_mask(display->drm, encoder,
8369 						    crtc_state->uapi.encoder_mask) {
8370 				if (encoder->initial_fastset_check &&
8371 				    !encoder->initial_fastset_check(encoder, crtc_state)) {
8372 					ret = drm_atomic_add_affected_connectors(state,
8373 										 &crtc->base);
8374 					if (ret)
8375 						goto out;
8376 				}
8377 			}
8378 		}
8379 	}
8380 
8381 	ret = drm_atomic_commit(state);
8382 
8383 out:
8384 	if (ret == -EDEADLK) {
8385 		drm_atomic_state_clear(state);
8386 		drm_modeset_backoff(&ctx);
8387 		goto retry;
8388 	}
8389 
8390 	drm_atomic_state_put(state);
8391 
8392 	drm_modeset_drop_locks(&ctx);
8393 	drm_modeset_acquire_fini(&ctx);
8394 
8395 	return ret;
8396 }
8397 
8398 void i830_enable_pipe(struct intel_display *display, enum pipe pipe)
8399 {
8400 	struct intel_crtc *crtc = intel_crtc_for_pipe(display, pipe);
8401 	enum transcoder cpu_transcoder = (enum transcoder)pipe;
8402 	/* 640x480@60Hz, ~25175 kHz */
8403 	struct dpll clock = {
8404 		.m1 = 18,
8405 		.m2 = 7,
8406 		.p1 = 13,
8407 		.p2 = 4,
8408 		.n = 2,
8409 	};
8410 	u32 dpll, fp;
8411 	int i;
8412 
8413 	drm_WARN_ON(display->drm,
8414 		    i9xx_calc_dpll_params(48000, &clock) != 25154);
8415 
8416 	drm_dbg_kms(display->drm,
8417 		    "enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
8418 		    pipe_name(pipe), clock.vco, clock.dot);
8419 
8420 	fp = i9xx_dpll_compute_fp(&clock);
8421 	dpll = DPLL_DVO_2X_MODE |
8422 		DPLL_VGA_MODE_DIS |
8423 		((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
8424 		PLL_P2_DIVIDE_BY_4 |
8425 		PLL_REF_INPUT_DREFCLK |
8426 		DPLL_VCO_ENABLE;
8427 
8428 	intel_de_write(display, TRANS_HTOTAL(display, cpu_transcoder),
8429 		       HACTIVE(640 - 1) | HTOTAL(800 - 1));
8430 	intel_de_write(display, TRANS_HBLANK(display, cpu_transcoder),
8431 		       HBLANK_START(640 - 1) | HBLANK_END(800 - 1));
8432 	intel_de_write(display, TRANS_HSYNC(display, cpu_transcoder),
8433 		       HSYNC_START(656 - 1) | HSYNC_END(752 - 1));
8434 	intel_de_write(display, TRANS_VTOTAL(display, cpu_transcoder),
8435 		       VACTIVE(480 - 1) | VTOTAL(525 - 1));
8436 	intel_de_write(display, TRANS_VBLANK(display, cpu_transcoder),
8437 		       VBLANK_START(480 - 1) | VBLANK_END(525 - 1));
8438 	intel_de_write(display, TRANS_VSYNC(display, cpu_transcoder),
8439 		       VSYNC_START(490 - 1) | VSYNC_END(492 - 1));
8440 	intel_de_write(display, PIPESRC(display, pipe),
8441 		       PIPESRC_WIDTH(640 - 1) | PIPESRC_HEIGHT(480 - 1));
8442 
8443 	intel_de_write(display, FP0(pipe), fp);
8444 	intel_de_write(display, FP1(pipe), fp);
8445 
8446 	/*
8447 	 * Apparently we need to have VGA mode enabled prior to changing
8448 	 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
8449 	 * dividers, even though the register value does change.
8450 	 */
8451 	intel_de_write(display, DPLL(display, pipe),
8452 		       dpll & ~DPLL_VGA_MODE_DIS);
8453 	intel_de_write(display, DPLL(display, pipe), dpll);
8454 
8455 	/* Wait for the clocks to stabilize. */
8456 	intel_de_posting_read(display, DPLL(display, pipe));
8457 	udelay(150);
8458 
8459 	/* The pixel multiplier can only be updated once the
8460 	 * DPLL is enabled and the clocks are stable.
8461 	 *
8462 	 * So write it again.
8463 	 */
8464 	intel_de_write(display, DPLL(display, pipe), dpll);
8465 
8466 	/* We do this three times for luck */
8467 	for (i = 0; i < 3 ; i++) {
8468 		intel_de_write(display, DPLL(display, pipe), dpll);
8469 		intel_de_posting_read(display, DPLL(display, pipe));
8470 		udelay(150); /* wait for warmup */
8471 	}
8472 
8473 	intel_de_write(display, TRANSCONF(display, pipe), TRANSCONF_ENABLE);
8474 	intel_de_posting_read(display, TRANSCONF(display, pipe));
8475 
8476 	intel_wait_for_pipe_scanline_moving(crtc);
8477 }
8478 
8479 void i830_disable_pipe(struct intel_display *display, enum pipe pipe)
8480 {
8481 	struct intel_crtc *crtc = intel_crtc_for_pipe(display, pipe);
8482 
8483 	drm_dbg_kms(display->drm, "disabling pipe %c due to force quirk\n",
8484 		    pipe_name(pipe));
8485 
8486 	drm_WARN_ON(display->drm,
8487 		    intel_de_read(display, DSPCNTR(display, PLANE_A)) & DISP_ENABLE);
8488 	drm_WARN_ON(display->drm,
8489 		    intel_de_read(display, DSPCNTR(display, PLANE_B)) & DISP_ENABLE);
8490 	drm_WARN_ON(display->drm,
8491 		    intel_de_read(display, DSPCNTR(display, PLANE_C)) & DISP_ENABLE);
8492 	drm_WARN_ON(display->drm,
8493 		    intel_de_read(display, CURCNTR(display, PIPE_A)) & MCURSOR_MODE_MASK);
8494 	drm_WARN_ON(display->drm,
8495 		    intel_de_read(display, CURCNTR(display, PIPE_B)) & MCURSOR_MODE_MASK);
8496 
8497 	intel_de_write(display, TRANSCONF(display, pipe), 0);
8498 	intel_de_posting_read(display, TRANSCONF(display, pipe));
8499 
8500 	intel_wait_for_pipe_scanline_stopped(crtc);
8501 
8502 	intel_de_write(display, DPLL(display, pipe), DPLL_VGA_MODE_DIS);
8503 	intel_de_posting_read(display, DPLL(display, pipe));
8504 }
8505 
8506 bool intel_scanout_needs_vtd_wa(struct intel_display *display)
8507 {
8508 	return IS_DISPLAY_VER(display, 6, 11) && intel_display_vtd_active(display);
8509 }
8510