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