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