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