xref: /linux/drivers/gpu/drm/i915/display/intel_dp.c (revision 570f7e331f5febb30f1384817463c7e42b65ca7d)
1 /*
2  * Copyright © 2008 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 DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Keith Packard <keithp@keithp.com>
25  *
26  */
27 
28 #include <linux/export.h>
29 #include <linux/i2c.h>
30 #include <linux/iopoll.h>
31 #include <linux/log2.h>
32 #include <linux/math.h>
33 #include <linux/notifier.h>
34 #include <linux/seq_buf.h>
35 #include <linux/slab.h>
36 #include <linux/string_helpers.h>
37 #include <linux/timekeeping.h>
38 #include <linux/types.h>
39 #include <asm/byteorder.h>
40 
41 #include <drm/display/drm_dp_helper.h>
42 #include <drm/display/drm_dp_tunnel.h>
43 #include <drm/display/drm_dsc_helper.h>
44 #include <drm/display/drm_hdmi_helper.h>
45 #include <drm/drm_atomic_helper.h>
46 #include <drm/drm_crtc.h>
47 #include <drm/drm_edid.h>
48 #include <drm/drm_fixed.h>
49 #include <drm/drm_print.h>
50 #include <drm/drm_probe_helper.h>
51 
52 #include "g4x_dp.h"
53 #include "intel_alpm.h"
54 #include "intel_atomic.h"
55 #include "intel_audio.h"
56 #include "intel_backlight.h"
57 #include "intel_combo_phy_regs.h"
58 #include "intel_connector.h"
59 #include "intel_crtc.h"
60 #include "intel_crtc_state_dump.h"
61 #include "intel_cx0_phy.h"
62 #include "intel_ddi.h"
63 #include "intel_de.h"
64 #include "intel_display_driver.h"
65 #include "intel_display_jiffies.h"
66 #include "intel_display_utils.h"
67 #include "intel_display_regs.h"
68 #include "intel_display_rpm.h"
69 #include "intel_display_types.h"
70 #include "intel_dp.h"
71 #include "intel_dp_aux.h"
72 #include "intel_dp_hdcp.h"
73 #include "intel_dp_link_caps.h"
74 #include "intel_dp_link_training.h"
75 #include "intel_dp_mst.h"
76 #include "intel_dp_test.h"
77 #include "intel_dp_tunnel.h"
78 #include "intel_dpio_phy.h"
79 #include "intel_dpll.h"
80 #include "intel_drrs.h"
81 #include "intel_encoder.h"
82 #include "intel_fifo_underrun.h"
83 #include "intel_hdcp.h"
84 #include "intel_hdmi.h"
85 #include "intel_hotplug.h"
86 #include "intel_hotplug_irq.h"
87 #include "intel_lspcon.h"
88 #include "intel_lvds.h"
89 #include "intel_modeset_lock.h"
90 #include "intel_panel.h"
91 #include "intel_pch_display.h"
92 #include "intel_pfit.h"
93 #include "intel_pps.h"
94 #include "intel_psr.h"
95 #include "intel_quirks.h"
96 #include "intel_tc.h"
97 #include "intel_vblank.h"
98 #include "intel_vdsc.h"
99 #include "intel_vrr.h"
100 
101 /* Max DSC line buffer depth supported by HW. */
102 #define INTEL_DP_DSC_MAX_LINE_BUF_DEPTH		13
103 
104 /* DP DSC FEC Overhead factor in ppm = 1/(0.972261) = 1.028530 */
105 #define DP_DSC_FEC_OVERHEAD_FACTOR		1028530
106 
107 /* Constants for DP DSC configurations */
108 static const u8 valid_dsc_bpp[] = {6, 8, 10, 12, 15};
109 
110 /**
111  * intel_dp_is_edp - is the given port attached to an eDP panel (either CPU or PCH)
112  * @intel_dp: DP struct
113  *
114  * If a CPU or PCH DP output is attached to an eDP panel, this function
115  * will return true, and false otherwise.
116  *
117  * This function is not safe to use prior to encoder type being set.
118  */
119 bool intel_dp_is_edp(struct intel_dp *intel_dp)
120 {
121 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
122 
123 	return dig_port->base.type == INTEL_OUTPUT_EDP;
124 }
125 
126 static void intel_dp_unset_edid(struct intel_dp *intel_dp);
127 
128 /* Is link rate UHBR and thus 128b/132b? */
129 bool intel_dp_is_uhbr(const struct intel_crtc_state *crtc_state)
130 {
131 	return drm_dp_is_uhbr_rate(crtc_state->port_clock);
132 }
133 
134 /**
135  * intel_dp_link_symbol_size - get the link symbol size for a given link rate
136  * @rate: link rate in 10kbit/s units
137  *
138  * Returns the link symbol size in bits/symbol units depending on the link
139  * rate -> channel coding.
140  */
141 int intel_dp_link_symbol_size(int rate)
142 {
143 	return drm_dp_is_uhbr_rate(rate) ? 32 : 10;
144 }
145 
146 /**
147  * intel_dp_link_symbol_clock - convert link rate to link symbol clock
148  * @rate: link rate in 10kbit/s units
149  *
150  * Returns the link symbol clock frequency in kHz units depending on the
151  * link rate and channel coding.
152  */
153 int intel_dp_link_symbol_clock(int rate)
154 {
155 	return DIV_ROUND_CLOSEST(rate * 10, intel_dp_link_symbol_size(rate));
156 }
157 
158 static int max_dprx_rate(struct intel_dp *intel_dp)
159 {
160 	struct intel_display *display = to_intel_display(intel_dp);
161 	int max_rate;
162 
163 	if (intel_dp_tunnel_bw_alloc_is_enabled(intel_dp))
164 		max_rate = drm_dp_tunnel_max_dprx_rate(intel_dp->tunnel);
165 	else
166 		max_rate = drm_dp_bw_code_to_link_rate(intel_dp->dpcd[DP_MAX_LINK_RATE]);
167 
168 	/*
169 	 * Some platforms + eDP panels may not reliably support HBR3
170 	 * due to signal integrity limitations, despite advertising it.
171 	 * Cap the link rate to HBR2 to avoid unstable configurations for the
172 	 * known machines.
173 	 */
174 	if (intel_dp_is_edp(intel_dp) && intel_has_quirk(display, QUIRK_EDP_LIMIT_RATE_HBR2))
175 		max_rate = min(max_rate, 540000);
176 
177 	return max_rate;
178 }
179 
180 static int max_dprx_lane_count(struct intel_dp *intel_dp)
181 {
182 	if (intel_dp_tunnel_bw_alloc_is_enabled(intel_dp))
183 		return drm_dp_tunnel_max_dprx_lane_count(intel_dp->tunnel);
184 
185 	return drm_dp_max_lane_count(intel_dp->dpcd);
186 }
187 
188 static void intel_dp_set_default_sink_rates(struct intel_dp *intel_dp)
189 {
190 	intel_dp->sink_rates[0] = 162000;
191 	intel_dp->num_sink_rates = 1;
192 }
193 
194 static bool dprx_supports_128b132b(struct intel_dp *intel_dp)
195 {
196 	if (intel_dp_tunnel_bw_alloc_is_enabled(intel_dp))
197 		return drm_dp_tunnel_128b132b_supported(intel_dp->tunnel);
198 	else
199 		return drm_dp_128b132b_supported(intel_dp->dpcd);
200 }
201 
202 static u8 dprx_128b132b_link_rates(struct intel_dp *intel_dp, u8 no_bwa_rates)
203 {
204 	u8 ret;
205 
206 	if (!intel_dp_tunnel_bw_alloc_is_enabled(intel_dp))
207 		return no_bwa_rates;
208 
209 	ret = drm_dp_tunnel_128b132b_dprx_rates(intel_dp->tunnel);
210 	static_assert(DP_TUNNELING_10GBPS_PER_LANE_SUPPORT == DP_UHBR10 &&
211 		      DP_TUNNELING_13_5GBPS_PER_LANE_SUPPORT == DP_UHBR13_5 &&
212 		      DP_TUNNELING_20GBPS_PER_LANE_SUPPORT == DP_UHBR20);
213 
214 	return ret;
215 }
216 
217 /* update sink rates from dpcd */
218 static void intel_dp_set_dpcd_sink_rates(struct intel_dp *intel_dp)
219 {
220 	static const int dp_rates[] = {
221 		162000, 270000, 540000, 810000
222 	};
223 	int i, max_rate;
224 	int max_lttpr_rate;
225 	u8 uhbr_rates = 0;
226 
227 	if (drm_dp_has_quirk(&intel_dp->desc, DP_DPCD_QUIRK_CAN_DO_MAX_LINK_RATE_3_24_GBPS)) {
228 		/* Needed, e.g., for Apple MBP 2017, 15 inch eDP Retina panel */
229 		static const int quirk_rates[] = { 162000, 270000, 324000 };
230 
231 		memcpy(intel_dp->sink_rates, quirk_rates, sizeof(quirk_rates));
232 		intel_dp->num_sink_rates = ARRAY_SIZE(quirk_rates);
233 
234 		return;
235 	}
236 
237 	/*
238 	 * Sink rates for 8b/10b.
239 	 */
240 	max_rate = max_dprx_rate(intel_dp);
241 	max_lttpr_rate = drm_dp_lttpr_max_link_rate(intel_dp->lttpr_common_caps);
242 	if (max_lttpr_rate)
243 		max_rate = min(max_rate, max_lttpr_rate);
244 
245 	for (i = 0; i < ARRAY_SIZE(dp_rates); i++) {
246 		if (dp_rates[i] > max_rate)
247 			break;
248 		intel_dp->sink_rates[i] = dp_rates[i];
249 	}
250 
251 	/*
252 	 * The following register must be read unconditionally for the later
253 	 * DP tunnel 128b132b detection to work, see DP Standard v2.1 5.14.3 .
254 	 */
255 	drm_dp_dpcd_read_byte(&intel_dp->aux, DP_128B132B_SUPPORTED_LINK_RATES, &uhbr_rates);
256 
257 	/*
258 	 * Sink rates for 128b/132b. If set, sink should support all 8b/10b
259 	 * rates and 10 Gbps.
260 	 */
261 	if (dprx_supports_128b132b(intel_dp)) {
262 		BUILD_BUG_ON(ARRAY_SIZE(intel_dp->sink_rates) < ARRAY_SIZE(dp_rates) + 3);
263 
264 		uhbr_rates = dprx_128b132b_link_rates(intel_dp, uhbr_rates);
265 
266 		if (drm_dp_lttpr_count(intel_dp->lttpr_common_caps)) {
267 			/* We have a repeater */
268 			if (intel_dp->lttpr_common_caps[0] >= 0x20 &&
269 			    intel_dp->lttpr_common_caps[DP_MAIN_LINK_CHANNEL_CODING_PHY_REPEATER -
270 							DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV] &
271 			    DP_PHY_REPEATER_128B132B_SUPPORTED) {
272 				/* Repeater supports 128b/132b, valid UHBR rates */
273 				uhbr_rates &= intel_dp->lttpr_common_caps[DP_PHY_REPEATER_128B132B_RATES -
274 									  DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
275 			} else {
276 				/* Does not support 128b/132b */
277 				uhbr_rates = 0;
278 			}
279 		}
280 
281 		if (uhbr_rates & DP_UHBR10)
282 			intel_dp->sink_rates[i++] = 1000000;
283 		if (uhbr_rates & DP_UHBR13_5)
284 			intel_dp->sink_rates[i++] = 1350000;
285 		if (uhbr_rates & DP_UHBR20)
286 			intel_dp->sink_rates[i++] = 2000000;
287 	}
288 
289 	intel_dp->num_sink_rates = i;
290 }
291 
292 static void intel_dp_set_sink_rates(struct intel_dp *intel_dp)
293 {
294 	struct intel_display *display = to_intel_display(intel_dp);
295 	struct intel_connector *connector = intel_dp->attached_connector;
296 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
297 	struct intel_encoder *encoder = &intel_dig_port->base;
298 
299 	intel_dp_set_dpcd_sink_rates(intel_dp);
300 
301 	if (intel_dp->num_sink_rates)
302 		return;
303 
304 	drm_err(display->drm,
305 		"[CONNECTOR:%d:%s][ENCODER:%d:%s] Invalid DPCD with no link rates, using defaults\n",
306 		connector->base.base.id, connector->base.name,
307 		encoder->base.base.id, encoder->base.name);
308 
309 	intel_dp_set_default_sink_rates(intel_dp);
310 }
311 
312 static void intel_dp_set_default_max_sink_lane_count(struct intel_dp *intel_dp)
313 {
314 	intel_dp->max_sink_lane_count = 1;
315 }
316 
317 static void intel_dp_set_max_sink_lane_count(struct intel_dp *intel_dp)
318 {
319 	struct intel_display *display = to_intel_display(intel_dp);
320 	struct intel_connector *connector = intel_dp->attached_connector;
321 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
322 	struct intel_encoder *encoder = &intel_dig_port->base;
323 
324 	intel_dp->max_sink_lane_count = max_dprx_lane_count(intel_dp);
325 
326 	switch (intel_dp->max_sink_lane_count) {
327 	case 1:
328 	case 2:
329 	case 4:
330 		return;
331 	}
332 
333 	drm_err(display->drm,
334 		"[CONNECTOR:%d:%s][ENCODER:%d:%s] Invalid DPCD max lane count (%d), using default\n",
335 		connector->base.base.id, connector->base.name,
336 		encoder->base.base.id, encoder->base.name,
337 		intel_dp->max_sink_lane_count);
338 
339 	intel_dp_set_default_max_sink_lane_count(intel_dp);
340 }
341 
342 /* Get length of rates array potentially limited by max_rate. */
343 int intel_dp_rate_limit_len(const int *rates, int len, int max_rate)
344 {
345 	int i;
346 
347 	/* Limit results by potentially reduced max rate */
348 	for (i = 0; i < len; i++) {
349 		if (rates[len - i - 1] <= max_rate)
350 			return len - i;
351 	}
352 
353 	return 0;
354 }
355 
356 int intel_dp_max_source_lane_count(struct intel_digital_port *dig_port)
357 {
358 	int vbt_max_lanes = intel_bios_dp_max_lane_count(dig_port->base.devdata);
359 	int max_lanes = dig_port->max_lanes;
360 
361 	if (vbt_max_lanes)
362 		max_lanes = min(max_lanes, vbt_max_lanes);
363 
364 	return max_lanes;
365 }
366 
367 /*
368  * Theoretical max between source and sink.
369  */
370 static int intel_dp_get_max_common_lane_count(struct intel_dp *intel_dp)
371 {
372 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
373 	int source_max = intel_dp_max_source_lane_count(dig_port);
374 	int sink_max = intel_dp->max_sink_lane_count;
375 	int lane_max = intel_tc_port_max_lane_count(dig_port);
376 	int lttpr_max = drm_dp_lttpr_max_lane_count(intel_dp->lttpr_common_caps);
377 
378 	if (lttpr_max)
379 		sink_max = min(sink_max, lttpr_max);
380 
381 	return min3(source_max, sink_max, lane_max);
382 }
383 
384 int intel_dp_link_bw_overhead(int link_clock, int lane_count, int hdisplay,
385 			      int dsc_slice_count, int bpp_x16, unsigned long flags)
386 {
387 	int overhead;
388 
389 	WARN_ON(flags & ~(DRM_DP_BW_OVERHEAD_MST | DRM_DP_BW_OVERHEAD_SSC_REF_CLK |
390 			  DRM_DP_BW_OVERHEAD_FEC));
391 
392 	if (drm_dp_is_uhbr_rate(link_clock))
393 		flags |= DRM_DP_BW_OVERHEAD_UHBR;
394 
395 	if (dsc_slice_count)
396 		flags |= DRM_DP_BW_OVERHEAD_DSC;
397 
398 	overhead = drm_dp_bw_overhead(lane_count, hdisplay,
399 				      dsc_slice_count,
400 				      bpp_x16,
401 				      flags);
402 
403 	/*
404 	 * TODO: clarify whether a minimum required by the fixed FEC overhead
405 	 * in the bspec audio programming sequence is required here.
406 	 */
407 	return max(overhead, intel_dp_bw_fec_overhead(flags & DRM_DP_BW_OVERHEAD_FEC));
408 }
409 
410 /*
411  * The required data bandwidth for a mode with given pixel clock and bpp. This
412  * is the required net bandwidth independent of the data bandwidth efficiency.
413  */
414 int intel_dp_link_required(int link_clock, int lane_count,
415 			   int mode_clock, int mode_hdisplay,
416 			   int link_bpp_x16, unsigned long bw_overhead_flags)
417 {
418 	int bw_overhead = intel_dp_link_bw_overhead(link_clock, lane_count, mode_hdisplay,
419 						    0, link_bpp_x16, bw_overhead_flags);
420 
421 	return intel_dp_effective_data_rate(mode_clock, link_bpp_x16, bw_overhead);
422 }
423 
424 /**
425  * intel_dp_effective_data_rate - Return the pixel data rate accounting for BW allocation overhead
426  * @pixel_clock: pixel clock in kHz
427  * @bpp_x16: bits per pixel .4 fixed point format
428  * @bw_overhead: BW allocation overhead in 1ppm units
429  *
430  * Return the effective pixel data rate in kB/sec units taking into account
431  * the provided SSC, FEC, DSC BW allocation overhead.
432  */
433 int intel_dp_effective_data_rate(int pixel_clock, int bpp_x16,
434 				 int bw_overhead)
435 {
436 	return DIV_ROUND_UP_ULL(mul_u32_u32(pixel_clock * bpp_x16, bw_overhead),
437 				1000000 * 16 * 8);
438 }
439 
440 /**
441  * intel_dp_max_link_data_rate: Calculate the maximum rate for the given link params
442  * @intel_dp: Intel DP object
443  * @max_dprx_rate: Maximum data rate of the DPRX
444  * @max_dprx_lanes: Maximum lane count of the DPRX
445  *
446  * Calculate the maximum data rate for the provided link parameters taking into
447  * account any BW limitations by a DP tunnel attached to @intel_dp.
448  *
449  * Returns the maximum data rate in kBps units.
450  */
451 int intel_dp_max_link_data_rate(struct intel_dp *intel_dp,
452 				int max_dprx_rate, int max_dprx_lanes)
453 {
454 	int max_rate = drm_dp_max_dprx_data_rate(max_dprx_rate, max_dprx_lanes);
455 
456 	if (intel_dp_tunnel_bw_alloc_is_enabled(intel_dp))
457 		max_rate = min(max_rate,
458 			       drm_dp_tunnel_available_bw(intel_dp->tunnel));
459 
460 	return max_rate;
461 }
462 
463 bool intel_dp_has_joiner(struct intel_dp *intel_dp)
464 {
465 	struct intel_display *display = to_intel_display(intel_dp);
466 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
467 	struct intel_encoder *encoder = &intel_dig_port->base;
468 	struct intel_connector *connector = intel_dp->attached_connector;
469 
470 	/* eDP MSO is not compatible with joiner */
471 	if (intel_dp->mso_link_count)
472 		return false;
473 
474 	if (intel_dp_is_edp(intel_dp) &&
475 	    !connector->panel.vbt.edp.pipe_joiner_enable)
476 		return false;
477 
478 	return DISPLAY_VER(display) >= 12 ||
479 		(DISPLAY_VER(display) == 11 &&
480 		 encoder->port != PORT_A);
481 }
482 
483 static int dg2_max_source_rate(struct intel_dp *intel_dp)
484 {
485 	return intel_dp_is_edp(intel_dp) ? 810000 : 1350000;
486 }
487 
488 static int icl_max_source_rate(struct intel_dp *intel_dp)
489 {
490 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
491 
492 	if (intel_encoder_is_combo(encoder) && !intel_dp_is_edp(intel_dp))
493 		return 540000;
494 
495 	return 810000;
496 }
497 
498 static int ehl_max_source_rate(struct intel_dp *intel_dp)
499 {
500 	if (intel_dp_is_edp(intel_dp))
501 		return 540000;
502 
503 	return 810000;
504 }
505 
506 static int mtl_max_source_rate(struct intel_dp *intel_dp)
507 {
508 	struct intel_display *display = to_intel_display(intel_dp);
509 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
510 
511 	if (intel_encoder_is_c10phy(encoder) ||
512 	    display->platform.pantherlake_wildcatlake)
513 		return 810000;
514 
515 	if (DISPLAY_VERx100(display) == 1401)
516 		return 1350000;
517 
518 	return 2000000;
519 }
520 
521 static int vbt_max_link_rate(struct intel_dp *intel_dp)
522 {
523 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
524 	int max_rate;
525 
526 	max_rate = intel_bios_dp_max_link_rate(encoder->devdata);
527 
528 	if (intel_dp_is_edp(intel_dp)) {
529 		struct intel_connector *connector = intel_dp->attached_connector;
530 		int edp_max_rate = connector->panel.vbt.edp.max_link_rate;
531 
532 		if (max_rate && edp_max_rate)
533 			max_rate = min(max_rate, edp_max_rate);
534 		else if (edp_max_rate)
535 			max_rate = edp_max_rate;
536 	}
537 
538 	return max_rate;
539 }
540 
541 static void
542 intel_dp_set_source_rates(struct intel_dp *intel_dp)
543 {
544 	/* The values must be in increasing order */
545 	static const int bmg_rates[] = {
546 		162000, 216000, 243000, 270000, 324000, 432000, 540000, 675000,
547 		810000,	1000000, 1350000,
548 	};
549 	static const int mtl_rates[] = {
550 		162000, 216000, 243000, 270000, 324000, 432000, 540000, 675000,
551 		810000,	1000000, 2000000,
552 	};
553 	static const int icl_rates[] = {
554 		162000, 216000, 270000, 324000, 432000, 540000, 648000, 810000,
555 		1000000, 1350000,
556 	};
557 	static const int bxt_rates[] = {
558 		162000, 216000, 243000, 270000, 324000, 432000, 540000
559 	};
560 	static const int skl_rates[] = {
561 		162000, 216000, 270000, 324000, 432000, 540000
562 	};
563 	static const int hsw_rates[] = {
564 		162000, 270000, 540000
565 	};
566 	static const int g4x_rates[] = {
567 		162000, 270000
568 	};
569 	struct intel_display *display = to_intel_display(intel_dp);
570 	const int *source_rates;
571 	int size, max_rate = 0, vbt_max_rate;
572 
573 	/* This should only be done once */
574 	drm_WARN_ON(display->drm,
575 		    intel_dp->source_rates || intel_dp->num_source_rates);
576 
577 	if (DISPLAY_VER(display) >= 14) {
578 		if (display->platform.battlemage) {
579 			source_rates = bmg_rates;
580 			size = ARRAY_SIZE(bmg_rates);
581 		} else {
582 			source_rates = mtl_rates;
583 			size = ARRAY_SIZE(mtl_rates);
584 		}
585 		max_rate = mtl_max_source_rate(intel_dp);
586 	} else if (DISPLAY_VER(display) >= 11) {
587 		source_rates = icl_rates;
588 		size = ARRAY_SIZE(icl_rates);
589 		if (display->platform.dg2)
590 			max_rate = dg2_max_source_rate(intel_dp);
591 		else if (display->platform.alderlake_p || display->platform.alderlake_s ||
592 			 display->platform.dg1 || display->platform.rocketlake)
593 			max_rate = 810000;
594 		else if (display->platform.jasperlake || display->platform.elkhartlake)
595 			max_rate = ehl_max_source_rate(intel_dp);
596 		else
597 			max_rate = icl_max_source_rate(intel_dp);
598 	} else if (display->platform.geminilake || display->platform.broxton) {
599 		source_rates = bxt_rates;
600 		size = ARRAY_SIZE(bxt_rates);
601 	} else if (DISPLAY_VER(display) == 9) {
602 		source_rates = skl_rates;
603 		size = ARRAY_SIZE(skl_rates);
604 	} else if ((display->platform.haswell && !display->platform.haswell_ulx) ||
605 		   display->platform.broadwell) {
606 		source_rates = hsw_rates;
607 		size = ARRAY_SIZE(hsw_rates);
608 	} else {
609 		source_rates = g4x_rates;
610 		size = ARRAY_SIZE(g4x_rates);
611 	}
612 
613 	vbt_max_rate = vbt_max_link_rate(intel_dp);
614 	if (max_rate && vbt_max_rate)
615 		max_rate = min(max_rate, vbt_max_rate);
616 	else if (vbt_max_rate)
617 		max_rate = vbt_max_rate;
618 
619 	if (max_rate)
620 		size = intel_dp_rate_limit_len(source_rates, size, max_rate);
621 
622 	intel_dp->source_rates = source_rates;
623 	intel_dp->num_source_rates = size;
624 }
625 
626 static int intersect_rates(const int *source_rates, int source_len,
627 			   const int *sink_rates, int sink_len,
628 			   int *common_rates)
629 {
630 	int i = 0, j = 0, k = 0;
631 
632 	while (i < source_len && j < sink_len) {
633 		if (source_rates[i] == sink_rates[j]) {
634 			if (WARN_ON(k >= DP_MAX_SUPPORTED_RATES))
635 				return k;
636 			common_rates[k] = source_rates[i];
637 			++k;
638 			++i;
639 			++j;
640 		} else if (source_rates[i] < sink_rates[j]) {
641 			++i;
642 		} else {
643 			++j;
644 		}
645 	}
646 	return k;
647 }
648 
649 /* return index of rate in rates array, or -1 if not found */
650 int intel_dp_rate_index(const int *rates, int len, int rate)
651 {
652 	int i;
653 
654 	for (i = 0; i < len; i++)
655 		if (rate == rates[i])
656 			return i;
657 
658 	return -1;
659 }
660 
661 static void intel_dp_get_common_rates(struct intel_dp *intel_dp,
662 				      int common_rates[DP_MAX_SUPPORTED_RATES],
663 				      int *num_common_rates)
664 {
665 	struct intel_display *display = to_intel_display(intel_dp);
666 
667 	drm_WARN_ON(display->drm,
668 		    !intel_dp->num_source_rates || !intel_dp->num_sink_rates);
669 
670 	*num_common_rates = intersect_rates(intel_dp->source_rates,
671 					    intel_dp->num_source_rates,
672 					    intel_dp->sink_rates,
673 					    intel_dp->num_sink_rates,
674 					    common_rates);
675 
676 	/* Paranoia, there should always be something in common. */
677 	if (drm_WARN_ON(display->drm, *num_common_rates == 0)) {
678 		common_rates[0] = 162000;
679 		*num_common_rates = 1;
680 	}
681 }
682 
683 /* Return %true if any common link param changed. */
684 static bool intel_dp_set_common_link_params(struct intel_dp *intel_dp)
685 {
686 	int num_common_rates;
687 	int common_rates[DP_MAX_SUPPORTED_RATES];
688 	bool params_changed = false;
689 
690 	intel_dp_get_common_rates(intel_dp, common_rates, &num_common_rates);
691 	if (intel_dp_link_caps_update(intel_dp->link.caps,
692 				      common_rates, num_common_rates,
693 				      intel_dp_get_max_common_lane_count(intel_dp),
694 				      intel_dp->reset_link_params))
695 		params_changed = true;
696 
697 	return params_changed;
698 }
699 
700 u32 intel_dp_mode_to_fec_clock(u32 mode_clock)
701 {
702 	return div_u64(mul_u32_u32(mode_clock, DP_DSC_FEC_OVERHEAD_FACTOR),
703 		       1000000U);
704 }
705 
706 int intel_dp_bw_fec_overhead(bool fec_enabled)
707 {
708 	/*
709 	 * TODO: Calculate the actual overhead for a given mode.
710 	 * The hard-coded 1/0.972261=2.853% overhead factor
711 	 * corresponds (for instance) to the 8b/10b DP FEC 2.4% +
712 	 * 0.453% DSC overhead. This is enough for a 3840 width mode,
713 	 * which has a DSC overhead of up to ~0.2%, but may not be
714 	 * enough for a 1024 width mode where this is ~0.8% (on a 4
715 	 * lane DP link, with 2 DSC slices and 8 bpp color depth).
716 	 */
717 	return fec_enabled ? DP_DSC_FEC_OVERHEAD_FACTOR : 1000000;
718 }
719 
720 static int
721 small_joiner_ram_size_bits(struct intel_display *display)
722 {
723 	if (DISPLAY_VER(display) >= 13)
724 		return 17280 * 8;
725 	else if (DISPLAY_VER(display) >= 11)
726 		return 7680 * 8;
727 	else
728 		return 6144 * 8;
729 }
730 
731 static int align_min_vesa_compressed_bpp_x16(int min_link_bpp_x16)
732 {
733 	int i;
734 
735 	for (i = 0; i < ARRAY_SIZE(valid_dsc_bpp); i++) {
736 		int vesa_bpp_x16 = fxp_q4_from_int(valid_dsc_bpp[i]);
737 
738 		if (vesa_bpp_x16 >= min_link_bpp_x16)
739 			return vesa_bpp_x16;
740 	}
741 
742 	return 0;
743 }
744 
745 static int align_max_vesa_compressed_bpp_x16(int max_link_bpp_x16)
746 {
747 	int i;
748 
749 	for (i = ARRAY_SIZE(valid_dsc_bpp) - 1; i >= 0; i--) {
750 		int vesa_bpp_x16 = fxp_q4_from_int(valid_dsc_bpp[i]);
751 
752 		if (vesa_bpp_x16 <= max_link_bpp_x16)
753 			return vesa_bpp_x16;
754 	}
755 
756 	return 0;
757 }
758 
759 static int bigjoiner_interface_bits(struct intel_display *display)
760 {
761 	return DISPLAY_VER(display) >= 14 ? 36 : 24;
762 }
763 
764 static u32 bigjoiner_bw_max_bpp(struct intel_display *display, u32 mode_clock,
765 				int num_joined_pipes)
766 {
767 	u32 max_bpp;
768 	/* With bigjoiner multiple dsc engines are used in parallel so PPC is 2 */
769 	int ppc = 2;
770 	int num_big_joiners = num_joined_pipes / 2;
771 
772 	max_bpp = display->cdclk.max_cdclk_freq * ppc * bigjoiner_interface_bits(display) /
773 		  intel_dp_mode_to_fec_clock(mode_clock);
774 
775 	max_bpp *= num_big_joiners;
776 
777 	return max_bpp;
778 
779 }
780 
781 static u32 small_joiner_ram_max_bpp(struct intel_display *display,
782 				    u32 mode_hdisplay,
783 				    int num_joined_pipes)
784 {
785 	u32 max_bpp;
786 
787 	/* Small Joiner Check: output bpp <= joiner RAM (bits) / Horiz. width */
788 	max_bpp = small_joiner_ram_size_bits(display) / mode_hdisplay;
789 
790 	max_bpp *= num_joined_pipes;
791 
792 	return max_bpp;
793 }
794 
795 static int ultrajoiner_ram_bits(void)
796 {
797 	return 4 * 72 * 512;
798 }
799 
800 static u32 ultrajoiner_ram_max_bpp(u32 mode_hdisplay)
801 {
802 	return ultrajoiner_ram_bits() / mode_hdisplay;
803 }
804 
805 /* TODO: return a bpp_x16 value */
806 static
807 u32 get_max_compressed_bpp_with_joiner(struct intel_display *display,
808 				       u32 mode_clock, u32 mode_hdisplay,
809 				       int num_joined_pipes)
810 {
811 	u32 max_bpp = small_joiner_ram_max_bpp(display, mode_hdisplay, num_joined_pipes);
812 
813 	if (num_joined_pipes > 1)
814 		max_bpp = min(max_bpp, bigjoiner_bw_max_bpp(display, mode_clock,
815 							    num_joined_pipes));
816 	if (num_joined_pipes == 4)
817 		max_bpp = min(max_bpp, ultrajoiner_ram_max_bpp(mode_hdisplay));
818 
819 	return max_bpp;
820 }
821 
822 static int intel_dp_dsc_min_slice_count(const struct intel_connector *connector,
823 					int mode_clock, int mode_hdisplay)
824 {
825 	struct intel_display *display = to_intel_display(connector);
826 	bool is_edp =
827 		connector->base.connector_type == DRM_MODE_CONNECTOR_eDP;
828 	int min_slice_count;
829 	int max_slice_width;
830 	int tp_rgb_yuv444;
831 	int tp_yuv422_420;
832 
833 	/*
834 	 * TODO: allow using less than the maximum number of slices
835 	 * supported by the eDP sink, to allow using fewer DSC engines.
836 	 */
837 	if (is_edp)
838 		return drm_dp_dsc_sink_max_slice_count(connector->dp.dsc_dpcd, true);
839 
840 	/*
841 	 * TODO: Use the throughput value specific to the actual RGB/YUV
842 	 * format of the output.
843 	 * The RGB/YUV444 throughput value should be always either equal
844 	 * or smaller than the YUV422/420 value, but let's not depend on
845 	 * this assumption.
846 	 */
847 	if (mode_clock > max(connector->dp.dsc_branch_caps.overall_throughput.rgb_yuv444,
848 			     connector->dp.dsc_branch_caps.overall_throughput.yuv422_420))
849 		return 0;
850 
851 	if (mode_hdisplay > connector->dp.dsc_branch_caps.max_line_width)
852 		return 0;
853 
854 	/*
855 	 * TODO: Pass the total pixel rate of all the streams transferred to
856 	 * an MST tiled display, calculate the total slice count for all tiles
857 	 * from this and the per-tile slice count from the total slice count.
858 	 */
859 	tp_rgb_yuv444 = drm_dp_dsc_sink_max_slice_throughput(connector->dp.dsc_dpcd,
860 							     mode_clock, true);
861 	tp_yuv422_420 = drm_dp_dsc_sink_max_slice_throughput(connector->dp.dsc_dpcd,
862 							     mode_clock, false);
863 
864 	/*
865 	 * TODO: Use the throughput value specific to the actual RGB/YUV
866 	 * format of the output.
867 	 * For now use the smaller of these, which is ok, potentially
868 	 * resulting in a higher than required minimum slice count.
869 	 * The RGB/YUV444 throughput value should be always either equal
870 	 * or smaller than the YUV422/420 value, but let's not depend on
871 	 * this assumption.
872 	 */
873 	min_slice_count = DIV_ROUND_UP(mode_clock, min(tp_rgb_yuv444, tp_yuv422_420));
874 
875 	/*
876 	 * Due to some DSC engine BW limitations, we need to enable second
877 	 * slice and VDSC engine, whenever we approach close enough to max CDCLK
878 	 */
879 	if (mode_clock >= ((display->cdclk.max_cdclk_freq * 85) / 100))
880 		min_slice_count = max(min_slice_count, 2);
881 
882 	max_slice_width = drm_dp_dsc_sink_max_slice_width(connector->dp.dsc_dpcd);
883 	if (max_slice_width < DP_DSC_MIN_SLICE_WIDTH_VALUE) {
884 		drm_dbg_kms(display->drm,
885 			    "Unsupported slice width %d by DP DSC Sink device\n",
886 			    max_slice_width);
887 		return 0;
888 	}
889 	/* Also take into account max slice width */
890 	min_slice_count = max(min_slice_count,
891 			      DIV_ROUND_UP(mode_hdisplay, max_slice_width));
892 
893 	return min_slice_count;
894 }
895 
896 static bool
897 intel_dp_dsc_get_slice_config(const struct intel_connector *connector,
898 			      int mode_clock, int mode_hdisplay,
899 			      int num_joined_pipes,
900 			      struct intel_dsc_slice_config *config_ret)
901 {
902 	struct intel_display *display = to_intel_display(connector);
903 	int min_slice_count =
904 		intel_dp_dsc_min_slice_count(connector, mode_clock, mode_hdisplay);
905 	bool is_edp =
906 		connector->base.connector_type == DRM_MODE_CONNECTOR_eDP;
907 	u32 sink_slice_count_mask =
908 		drm_dp_dsc_sink_slice_count_mask(connector->dp.dsc_dpcd, is_edp);
909 	int slices_per_pipe;
910 
911 	/*
912 	 * Find the closest match to the valid slice count values
913 	 *
914 	 * Max HW DSC-per-pipe x slice-per-DSC (= slice-per-pipe) capability:
915 	 * ICL:  2x2
916 	 * BMG:  2x2, or for ultrajoined 4 pipes: 3x1
917 	 * TGL+: 2x4 (TODO: Add support for this)
918 	 *
919 	 * TODO: Explore if it's worth increasing the number of slices (from 1
920 	 * to 2 or 3), so that multiple VDSC engines can be used, thus
921 	 * reducing the minimum CDCLK requirement, which in turn is determined
922 	 * by the 1 pixel per clock VDSC engine throughput in
923 	 * intel_vdsc_min_cdclk().
924 	 */
925 	for (slices_per_pipe = 1; slices_per_pipe <= 4; slices_per_pipe++) {
926 		struct intel_dsc_slice_config config;
927 		int slices_per_line;
928 
929 		if (!intel_dsc_get_slice_config(display,
930 						num_joined_pipes, slices_per_pipe,
931 						&config))
932 			continue;
933 
934 		slices_per_line = intel_dsc_line_slice_count(&config);
935 
936 		if (!(drm_dp_dsc_slice_count_to_mask(slices_per_line) &
937 		      sink_slice_count_mask))
938 			continue;
939 
940 		if (mode_hdisplay % slices_per_line)
941 			continue;
942 
943 		if (min_slice_count <= slices_per_line) {
944 			*config_ret = config;
945 
946 			return true;
947 		}
948 	}
949 
950 	/* Print slice count 1,2,4,..24 if bit#0,1,3,..23 is set in the mask. */
951 	sink_slice_count_mask <<= 1;
952 	drm_dbg_kms(display->drm,
953 		    "[CONNECTOR:%d:%s] Unsupported slice count (min: %d, sink supported: %*pbl)\n",
954 		    connector->base.base.id, connector->base.name,
955 		    min_slice_count,
956 		    (int)BITS_PER_TYPE(sink_slice_count_mask), &sink_slice_count_mask);
957 
958 	return false;
959 }
960 
961 u8 intel_dp_dsc_get_slice_count(const struct intel_connector *connector,
962 				int mode_clock, int mode_hdisplay,
963 				int num_joined_pipes)
964 {
965 	struct intel_dsc_slice_config config;
966 
967 	if (!intel_dp_dsc_get_slice_config(connector,
968 					   mode_clock, mode_hdisplay,
969 					   num_joined_pipes, &config))
970 		return 0;
971 
972 	return intel_dsc_line_slice_count(&config);
973 }
974 
975 static bool source_can_output(struct intel_dp *intel_dp,
976 			      enum intel_output_format format)
977 {
978 	struct intel_display *display = to_intel_display(intel_dp);
979 
980 	switch (format) {
981 	case INTEL_OUTPUT_FORMAT_RGB:
982 		return true;
983 
984 	case INTEL_OUTPUT_FORMAT_YCBCR444:
985 		/*
986 		 * No YCbCr output support on gmch platforms.
987 		 * Also, ILK doesn't seem capable of DP YCbCr output.
988 		 * The displayed image is severely corrupted. SNB+ is fine.
989 		 */
990 		return !HAS_GMCH(display) && !display->platform.ironlake;
991 
992 	case INTEL_OUTPUT_FORMAT_YCBCR420:
993 		/* Platform < Gen 11 cannot output YCbCr420 format */
994 		return DISPLAY_VER(display) >= 11;
995 
996 	default:
997 		MISSING_CASE(format);
998 		return false;
999 	}
1000 }
1001 
1002 static bool
1003 dfp_can_convert_from_rgb(struct intel_dp *intel_dp,
1004 			 enum intel_output_format sink_format)
1005 {
1006 	if (!drm_dp_is_branch(intel_dp->dpcd))
1007 		return false;
1008 
1009 	if (sink_format == INTEL_OUTPUT_FORMAT_YCBCR444)
1010 		return intel_dp->dfp.rgb_to_ycbcr;
1011 
1012 	if (sink_format == INTEL_OUTPUT_FORMAT_YCBCR420)
1013 		return intel_dp->dfp.rgb_to_ycbcr &&
1014 			intel_dp->dfp.ycbcr_444_to_420;
1015 
1016 	return false;
1017 }
1018 
1019 static bool
1020 dfp_can_convert_from_ycbcr444(struct intel_dp *intel_dp,
1021 			      enum intel_output_format sink_format)
1022 {
1023 	if (!drm_dp_is_branch(intel_dp->dpcd))
1024 		return false;
1025 
1026 	if (sink_format == INTEL_OUTPUT_FORMAT_YCBCR420)
1027 		return intel_dp->dfp.ycbcr_444_to_420;
1028 
1029 	return false;
1030 }
1031 
1032 static bool
1033 dfp_can_convert(struct intel_dp *intel_dp,
1034 		enum intel_output_format output_format,
1035 		enum intel_output_format sink_format)
1036 {
1037 	switch (output_format) {
1038 	case INTEL_OUTPUT_FORMAT_RGB:
1039 		return dfp_can_convert_from_rgb(intel_dp, sink_format);
1040 	case INTEL_OUTPUT_FORMAT_YCBCR444:
1041 		return dfp_can_convert_from_ycbcr444(intel_dp, sink_format);
1042 	default:
1043 		MISSING_CASE(output_format);
1044 		return false;
1045 	}
1046 
1047 	return false;
1048 }
1049 
1050 static enum intel_output_format
1051 intel_dp_output_format(struct intel_connector *connector,
1052 		       enum intel_output_format sink_format)
1053 {
1054 	struct intel_display *display = to_intel_display(connector);
1055 	struct intel_dp *intel_dp = intel_attached_dp(connector);
1056 	enum intel_output_format force_dsc_output_format =
1057 		intel_dp->force_dsc_output_format;
1058 	enum intel_output_format output_format;
1059 	if (force_dsc_output_format) {
1060 		if (source_can_output(intel_dp, force_dsc_output_format) &&
1061 		    (!drm_dp_is_branch(intel_dp->dpcd) ||
1062 		     sink_format != force_dsc_output_format ||
1063 		     dfp_can_convert(intel_dp, force_dsc_output_format, sink_format)))
1064 			return force_dsc_output_format;
1065 
1066 		drm_dbg_kms(display->drm, "Cannot force DSC output format\n");
1067 	}
1068 
1069 	if (sink_format == INTEL_OUTPUT_FORMAT_RGB ||
1070 	    dfp_can_convert_from_rgb(intel_dp, sink_format))
1071 		output_format = INTEL_OUTPUT_FORMAT_RGB;
1072 
1073 	else if (sink_format == INTEL_OUTPUT_FORMAT_YCBCR444 ||
1074 		 dfp_can_convert_from_ycbcr444(intel_dp, sink_format))
1075 		output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
1076 
1077 	else
1078 		output_format = INTEL_OUTPUT_FORMAT_YCBCR420;
1079 
1080 	drm_WARN_ON(display->drm, !source_can_output(intel_dp, output_format));
1081 
1082 	return output_format;
1083 }
1084 
1085 int intel_dp_min_bpp(enum intel_output_format output_format)
1086 {
1087 	if (output_format == INTEL_OUTPUT_FORMAT_RGB)
1088 		return intel_display_min_pipe_bpp();
1089 	else
1090 		return 8 * 3;
1091 }
1092 
1093 int intel_dp_output_format_link_bpp_x16(enum intel_output_format output_format, int pipe_bpp)
1094 {
1095 	/*
1096 	 * bpp value was assumed to RGB format. And YCbCr 4:2:0 output
1097 	 * format of the number of bytes per pixel will be half the number
1098 	 * of bytes of RGB pixel.
1099 	 */
1100 	if (output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
1101 		pipe_bpp /= 2;
1102 
1103 	return fxp_q4_from_int(pipe_bpp);
1104 }
1105 
1106 static int
1107 intel_dp_mode_min_link_bpp_x16(struct intel_connector *connector,
1108 			       const struct drm_display_mode *mode,
1109 			       enum intel_output_format output_format)
1110 {
1111 	return intel_dp_output_format_link_bpp_x16(output_format,
1112 						   intel_dp_min_bpp(output_format));
1113 }
1114 
1115 static bool intel_dp_hdisplay_bad(struct intel_display *display,
1116 				  int hdisplay)
1117 {
1118 	/*
1119 	 * Older platforms don't like hdisplay==4096 with DP.
1120 	 *
1121 	 * On ILK/SNB/IVB the pipe seems to be somewhat running (scanline
1122 	 * and frame counter increment), but we don't get vblank interrupts,
1123 	 * and the pipe underruns immediately. The link also doesn't seem
1124 	 * to get trained properly.
1125 	 *
1126 	 * On CHV the vblank interrupts don't seem to disappear but
1127 	 * otherwise the symptoms are similar.
1128 	 *
1129 	 * TODO: confirm the behaviour on HSW+
1130 	 */
1131 	return hdisplay == 4096 && !HAS_DDI(display);
1132 }
1133 
1134 static int intel_dp_max_tmds_clock(struct intel_dp *intel_dp)
1135 {
1136 	struct intel_connector *connector = intel_dp->attached_connector;
1137 	const struct drm_display_info *info = &connector->base.display_info;
1138 	int max_tmds_clock = intel_dp->dfp.max_tmds_clock;
1139 
1140 	/* Only consider the sink's max TMDS clock if we know this is a HDMI DFP */
1141 	if (max_tmds_clock && info->max_tmds_clock)
1142 		max_tmds_clock = min(max_tmds_clock, info->max_tmds_clock);
1143 
1144 	return max_tmds_clock;
1145 }
1146 
1147 static enum drm_mode_status
1148 intel_dp_tmds_clock_valid(struct intel_dp *intel_dp,
1149 			  int clock, int bpc,
1150 			  enum intel_output_format sink_format,
1151 			  bool respect_downstream_limits)
1152 {
1153 	int tmds_clock, min_tmds_clock, max_tmds_clock;
1154 
1155 	if (!respect_downstream_limits)
1156 		return MODE_OK;
1157 
1158 	tmds_clock = intel_hdmi_tmds_clock(clock, bpc, sink_format);
1159 
1160 	min_tmds_clock = intel_dp->dfp.min_tmds_clock;
1161 	max_tmds_clock = intel_dp_max_tmds_clock(intel_dp);
1162 
1163 	if (min_tmds_clock && tmds_clock < min_tmds_clock)
1164 		return MODE_CLOCK_LOW;
1165 
1166 	if (max_tmds_clock && tmds_clock > max_tmds_clock)
1167 		return MODE_CLOCK_HIGH;
1168 
1169 	return MODE_OK;
1170 }
1171 
1172 static int frl_required_bw(int clock, int bpc,
1173 			   enum intel_output_format sink_format)
1174 {
1175 	if (sink_format == INTEL_OUTPUT_FORMAT_YCBCR420)
1176 		clock /= 2;
1177 
1178 	return clock * bpc * 3;
1179 }
1180 
1181 static enum drm_mode_status
1182 intel_dp_mode_valid_downstream(struct intel_connector *connector,
1183 			       const struct drm_display_mode *mode,
1184 			       int target_clock,
1185 			       enum intel_output_format sink_format)
1186 {
1187 	struct intel_dp *intel_dp = intel_attached_dp(connector);
1188 
1189 	/* If PCON supports FRL MODE, check FRL bandwidth constraints */
1190 	if (intel_dp->dfp.pcon_max_frl_bw) {
1191 		int target_bw, max_frl_bw;
1192 
1193 		/* Assume 8bpc for the FRL bandwidth check */
1194 		target_bw = frl_required_bw(target_clock, 8, sink_format);
1195 
1196 		max_frl_bw = intel_dp->dfp.pcon_max_frl_bw;
1197 
1198 		/* converting bw from Gbps to Kbps*/
1199 		max_frl_bw = max_frl_bw * 1000000;
1200 
1201 		if (target_bw > max_frl_bw)
1202 			return MODE_CLOCK_HIGH;
1203 
1204 		return MODE_OK;
1205 	}
1206 
1207 	if (intel_dp->dfp.max_dotclock &&
1208 	    target_clock > intel_dp->dfp.max_dotclock)
1209 		return MODE_CLOCK_HIGH;
1210 
1211 	/* Assume 8bpc for the DP++/HDMI/DVI TMDS clock check */
1212 	return intel_dp_tmds_clock_valid(intel_dp, target_clock,
1213 					 8, sink_format, true);
1214 }
1215 
1216 static enum drm_mode_status
1217 intel_dp_sink_format_valid(struct intel_connector *connector,
1218 			   const struct drm_display_mode *mode,
1219 			   enum intel_output_format sink_format)
1220 {
1221 	struct intel_dp *intel_dp = intel_attached_dp(connector);
1222 	const struct drm_display_info *info = &connector->base.display_info;
1223 
1224 	switch (sink_format) {
1225 	case INTEL_OUTPUT_FORMAT_YCBCR420:
1226 		if (intel_dp->dfp.min_tmds_clock &&
1227 		    !intel_dp_has_hdmi_sink(intel_dp))
1228 			return MODE_NO_420;
1229 
1230 		if (!connector->base.ycbcr_420_allowed ||
1231 		    !drm_mode_is_420(info, mode))
1232 			return MODE_NO_420;
1233 
1234 		return MODE_OK;
1235 	case INTEL_OUTPUT_FORMAT_RGB:
1236 		return MODE_OK;
1237 	default:
1238 		MISSING_CASE(sink_format);
1239 		return MODE_BAD;
1240 	}
1241 }
1242 
1243 int intel_dp_max_hdisplay_per_pipe(struct intel_display *display)
1244 {
1245 	return DISPLAY_VER(display) >= 30 ? 6144 : 5120;
1246 }
1247 
1248 bool intel_dp_has_dsc(const struct intel_connector *connector)
1249 {
1250 	struct intel_display *display = to_intel_display(connector);
1251 
1252 	if (!HAS_DSC(display))
1253 		return false;
1254 
1255 	if (connector->mst.dp && !HAS_DSC_MST(display))
1256 		return false;
1257 
1258 	if (connector->base.connector_type == DRM_MODE_CONNECTOR_eDP &&
1259 	    connector->panel.vbt.edp.dsc_disable)
1260 		return false;
1261 
1262 	if (!drm_dp_sink_supports_dsc(connector->dp.dsc_dpcd))
1263 		return false;
1264 
1265 	return true;
1266 }
1267 
1268 static
1269 bool intel_dp_can_join(struct intel_dp *intel_dp,
1270 		       int num_joined_pipes)
1271 {
1272 	struct intel_display *display = to_intel_display(intel_dp);
1273 
1274 	if (num_joined_pipes > 1 && !intel_dp_has_joiner(intel_dp))
1275 		return false;
1276 
1277 	switch (num_joined_pipes) {
1278 	case 1:
1279 		return true;
1280 	case 2:
1281 		return HAS_BIGJOINER(display) ||
1282 		       HAS_UNCOMPRESSED_JOINER(display);
1283 	case 4:
1284 		return HAS_ULTRAJOINER(display);
1285 	default:
1286 		return false;
1287 	}
1288 }
1289 
1290 bool intel_dp_dotclk_valid(struct intel_display *display,
1291 			   int target_clock,
1292 			   int htotal,
1293 			   int dsc_slice_count,
1294 			   int num_joined_pipes)
1295 {
1296 	int max_dotclk = display->cdclk.max_dotclk_freq;
1297 	int effective_dotclk_limit;
1298 
1299 	effective_dotclk_limit = max_dotclk * num_joined_pipes;
1300 
1301 	if (dsc_slice_count)
1302 		target_clock = intel_dsc_get_pixel_rate_with_dsc_bubbles(display,
1303 									 target_clock,
1304 									 htotal,
1305 									 dsc_slice_count);
1306 	else
1307 		effective_dotclk_limit =
1308 			intel_max_uncompressed_dotclock(display) * num_joined_pipes;
1309 
1310 	return target_clock <= effective_dotclk_limit;
1311 }
1312 
1313 static enum drm_mode_status
1314 intel_dp_mode_valid_format(struct intel_connector *connector,
1315 			   const struct drm_display_mode *mode,
1316 			   int target_clock,
1317 			   enum intel_output_format sink_format)
1318 {
1319 	struct intel_display *display = to_intel_display(connector);
1320 	struct intel_dp *intel_dp = intel_attached_dp(connector);
1321 	enum intel_output_format output_format;
1322 	int max_rate, mode_rate, max_lanes, max_link_clock;
1323 	struct intel_dp_link_config max_bw_config;
1324 	u16 dsc_max_compressed_bpp = 0;
1325 	enum drm_mode_status status;
1326 	bool dsc = false;
1327 	int num_joined_pipes;
1328 	int link_bpp_x16;
1329 
1330 	status = intel_dp_sink_format_valid(connector, mode, sink_format);
1331 	if (status != MODE_OK)
1332 		return status;
1333 
1334 	output_format = intel_dp_output_format(connector, sink_format);
1335 
1336 	intel_dp_link_caps_get_max_bw_config(intel_dp->link.caps, &max_bw_config);
1337 	max_link_clock = max_bw_config.rate;
1338 	max_lanes = max_bw_config.lane_count;
1339 
1340 	max_rate = intel_dp_max_link_data_rate(intel_dp, max_link_clock, max_lanes);
1341 
1342 	link_bpp_x16 = intel_dp_mode_min_link_bpp_x16(connector, mode,
1343 						      output_format);
1344 	mode_rate = intel_dp_link_required(max_link_clock, max_lanes,
1345 					   target_clock, mode->hdisplay,
1346 					   link_bpp_x16, 0);
1347 
1348 	/*
1349 	 * We cannot determine the required pipe‑join count before knowing whether
1350 	 * DSC is needed, nor can we determine DSC need without knowing the pipe
1351 	 * count.
1352 	 * Because of this dependency cycle, the only correct approach is to iterate
1353 	 * over candidate pipe counts and evaluate each combination.
1354 	 */
1355 	status = MODE_CLOCK_HIGH;
1356 	for_each_joiner_candidate(connector, mode, num_joined_pipes) {
1357 		int dsc_slice_count = 0;
1358 
1359 		status = intel_pfit_mode_valid(display, mode, output_format, num_joined_pipes);
1360 		if (status != MODE_OK)
1361 			continue;
1362 
1363 		if (intel_dp_has_dsc(connector)) {
1364 			int pipe_bpp;
1365 
1366 			dsc_slice_count = intel_dp_dsc_get_slice_count(connector,
1367 								       target_clock,
1368 								       mode->hdisplay,
1369 								       num_joined_pipes);
1370 
1371 			/*
1372 			 * TBD pass the connector BPC,
1373 			 * for now U8_MAX so that max BPC on that platform would be picked
1374 			 */
1375 			pipe_bpp = intel_dp_dsc_compute_max_bpp(connector, U8_MAX);
1376 
1377 			/*
1378 			 * Output bpp is stored in 6.4 format so right shift by 4 to get the
1379 			 * integer value since we support only integer values of bpp.
1380 			 */
1381 			if (intel_dp_is_edp(intel_dp)) {
1382 				dsc_max_compressed_bpp =
1383 					drm_edp_dsc_sink_output_bpp(connector->dp.dsc_dpcd) >> 4;
1384 
1385 				dsc = dsc_max_compressed_bpp && dsc_slice_count;
1386 			} else if (drm_dp_sink_supports_fec(connector->dp.fec_capability)) {
1387 				unsigned long bw_overhead_flags = 0;
1388 
1389 				if (!drm_dp_is_uhbr_rate(max_link_clock))
1390 					bw_overhead_flags |= DRM_DP_BW_OVERHEAD_FEC;
1391 
1392 				dsc = intel_dp_mode_valid_with_dsc(connector,
1393 								   max_link_clock, max_lanes,
1394 								   target_clock, mode->hdisplay,
1395 								   num_joined_pipes,
1396 								   output_format, pipe_bpp,
1397 								   bw_overhead_flags);
1398 			}
1399 		}
1400 
1401 		if (intel_dp_joiner_needs_dsc(display, num_joined_pipes) && !dsc) {
1402 			status = MODE_CLOCK_HIGH;
1403 			continue;
1404 		}
1405 
1406 		if (mode_rate > max_rate && !dsc) {
1407 			status = MODE_CLOCK_HIGH;
1408 			continue;
1409 		}
1410 
1411 		status = intel_mode_valid_max_plane_size(display, mode, num_joined_pipes);
1412 		if (status != MODE_OK)
1413 			continue;
1414 
1415 		if (!dsc)
1416 			dsc_slice_count = 0;
1417 
1418 		if (!intel_dp_dotclk_valid(display,
1419 					   target_clock,
1420 					   mode->htotal,
1421 					   dsc_slice_count,
1422 					   num_joined_pipes)) {
1423 			status = MODE_CLOCK_HIGH;
1424 			continue;
1425 		}
1426 
1427 		break;
1428 	}
1429 
1430 	if (status != MODE_OK)
1431 		return status;
1432 
1433 	return intel_dp_mode_valid_downstream(connector, mode,
1434 					      target_clock, sink_format);
1435 }
1436 
1437 static enum drm_mode_status
1438 intel_dp_mode_valid(struct drm_connector *_connector,
1439 		    const struct drm_display_mode *mode)
1440 {
1441 	struct intel_display *display = to_intel_display(_connector->dev);
1442 	struct intel_connector *connector = to_intel_connector(_connector);
1443 	const struct drm_display_info *info = &connector->base.display_info;
1444 	struct intel_dp *intel_dp = intel_attached_dp(connector);
1445 	int target_clock = mode->clock;
1446 	enum drm_mode_status status;
1447 
1448 	status = intel_cpu_transcoder_mode_valid(display, mode);
1449 	if (status != MODE_OK)
1450 		return status;
1451 
1452 	if (mode->flags & DRM_MODE_FLAG_DBLCLK)
1453 		return MODE_H_ILLEGAL;
1454 
1455 	if (mode->clock < 10000)
1456 		return MODE_CLOCK_LOW;
1457 
1458 	if (intel_dp_hdisplay_bad(display, mode->hdisplay))
1459 		return MODE_H_ILLEGAL;
1460 
1461 	if (intel_dp_is_edp(intel_dp)) {
1462 		status = intel_panel_mode_valid(connector, mode, &target_clock);
1463 		if (status != MODE_OK)
1464 			return status;
1465 	}
1466 
1467 	/*
1468 	 * TODO: Even when using a 4:2:0 sink_format intel_dp_output_format()
1469 	 * will always choose a 4:4:4 output_format if the DFP can do the
1470 	 * 4:4:4->4:2:0 conversion for us. Thus a mode may still be rejected
1471 	 * if we only have enough DP link bandwidth for 4:2:0 but not for
1472 	 * 4:4:4. Another attempt with an explicit 4:2:0 output_format might
1473 	 * be needed here. intel_dp_compute_config() would need the same
1474 	 * logic, or else the actual modeset would still fail.
1475 	 *
1476 	 * Also a lot of the checks only depend on output_format but not
1477 	 * sink_format, so we are potentially doing redundant work by
1478 	 * testing the same output_format for two different sink_formats.
1479 	 */
1480 	if (drm_mode_is_420_only(info, mode)) {
1481 		status = intel_dp_mode_valid_format(connector, mode, target_clock,
1482 						    INTEL_OUTPUT_FORMAT_YCBCR420);
1483 	} else {
1484 		status = intel_dp_mode_valid_format(connector, mode, target_clock,
1485 						    INTEL_OUTPUT_FORMAT_RGB);
1486 
1487 		if (status != MODE_OK && drm_mode_is_420_also(info, mode))
1488 			status = intel_dp_mode_valid_format(connector, mode, target_clock,
1489 							    INTEL_OUTPUT_FORMAT_YCBCR420);
1490 	}
1491 
1492 	return status;
1493 }
1494 
1495 bool intel_dp_source_supports_tps3(struct intel_display *display)
1496 {
1497 	return DISPLAY_VER(display) >= 9 ||
1498 		display->platform.broadwell || display->platform.haswell;
1499 }
1500 
1501 bool intel_dp_source_supports_tps4(struct intel_display *display)
1502 {
1503 	return DISPLAY_VER(display) >= 10;
1504 }
1505 
1506 static void seq_buf_print_array(struct seq_buf *s, const int *array, int nelem)
1507 {
1508 	int i;
1509 
1510 	for (i = 0; i < nelem; i++)
1511 		seq_buf_printf(s, "%s%d", i ? ", " : "", array[i]);
1512 }
1513 
1514 static void intel_dp_print_rates(struct intel_dp *intel_dp)
1515 {
1516 	struct intel_display *display = to_intel_display(intel_dp);
1517 	DECLARE_SEQ_BUF(s, 128); /* FIXME: too big for stack? */
1518 
1519 	if (!drm_debug_enabled(DRM_UT_KMS))
1520 		return;
1521 
1522 	seq_buf_print_array(&s, intel_dp->source_rates, intel_dp->num_source_rates);
1523 	drm_dbg_kms(display->drm, "source rates: %s\n", seq_buf_str(&s));
1524 
1525 	seq_buf_clear(&s);
1526 	seq_buf_print_array(&s, intel_dp->sink_rates, intel_dp->num_sink_rates);
1527 	drm_dbg_kms(display->drm, "sink rates: %s\n", seq_buf_str(&s));
1528 
1529 	intel_dp_link_caps_print_common_rates(intel_dp->link.caps);
1530 }
1531 
1532 int intel_dp_rate_select(struct intel_dp *intel_dp, int rate)
1533 {
1534 	struct intel_display *display = to_intel_display(intel_dp);
1535 	int i = intel_dp_rate_index(intel_dp->sink_rates,
1536 				    intel_dp->num_sink_rates, rate);
1537 
1538 	if (drm_WARN_ON(display->drm, i < 0))
1539 		i = 0;
1540 
1541 	return i;
1542 }
1543 
1544 void intel_dp_compute_rate(struct intel_dp *intel_dp, int port_clock,
1545 			   u8 *link_bw, u8 *rate_select)
1546 {
1547 	struct intel_display *display = to_intel_display(intel_dp);
1548 
1549 	/* FIXME g4x can't generate an exact 2.7GHz with the 96MHz non-SSC refclk */
1550 	if (display->platform.g4x && port_clock == 268800)
1551 		port_clock = 270000;
1552 
1553 	/* eDP 1.4 rate select method. */
1554 	if (intel_dp->use_rate_select) {
1555 		*link_bw = 0;
1556 		*rate_select =
1557 			intel_dp_rate_select(intel_dp, port_clock);
1558 	} else {
1559 		*link_bw = drm_dp_link_rate_to_bw_code(port_clock);
1560 		*rate_select = 0;
1561 	}
1562 }
1563 
1564 bool intel_dp_has_hdmi_sink(struct intel_dp *intel_dp)
1565 {
1566 	struct intel_connector *connector = intel_dp->attached_connector;
1567 
1568 	return connector->base.display_info.is_hdmi;
1569 }
1570 
1571 static bool intel_dp_source_supports_fec(struct intel_dp *intel_dp,
1572 					 const struct intel_crtc_state *pipe_config)
1573 {
1574 	struct intel_display *display = to_intel_display(intel_dp);
1575 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
1576 
1577 	if (DISPLAY_VER(display) >= 12)
1578 		return true;
1579 
1580 	if (DISPLAY_VER(display) == 11 && encoder->port != PORT_A &&
1581 	    !intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DP_MST))
1582 		return true;
1583 
1584 	return false;
1585 }
1586 
1587 bool intel_dp_supports_fec(struct intel_dp *intel_dp,
1588 			   const struct intel_connector *connector,
1589 			   const struct intel_crtc_state *pipe_config)
1590 {
1591 	return intel_dp_source_supports_fec(intel_dp, pipe_config) &&
1592 		drm_dp_sink_supports_fec(connector->dp.fec_capability);
1593 }
1594 
1595 bool intel_dp_supports_dsc(struct intel_dp *intel_dp,
1596 			   const struct intel_connector *connector,
1597 			   const struct intel_crtc_state *crtc_state)
1598 {
1599 	if (!intel_dp_has_dsc(connector))
1600 		return false;
1601 
1602 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP) &&
1603 	    !intel_dp_supports_fec(intel_dp, connector, crtc_state))
1604 		return false;
1605 
1606 	return intel_dsc_source_support(crtc_state);
1607 }
1608 
1609 static int intel_dp_hdmi_compute_bpc(struct intel_dp *intel_dp,
1610 				     const struct intel_crtc_state *crtc_state,
1611 				     int bpc, bool respect_downstream_limits)
1612 {
1613 	int clock = crtc_state->hw.adjusted_mode.crtc_clock;
1614 
1615 	/*
1616 	 * Current bpc could already be below 8bpc due to
1617 	 * FDI bandwidth constraints or other limits.
1618 	 * HDMI minimum is 8bpc however.
1619 	 */
1620 	bpc = max(bpc, 8);
1621 
1622 	/*
1623 	 * We will never exceed downstream TMDS clock limits while
1624 	 * attempting deep color. If the user insists on forcing an
1625 	 * out of spec mode they will have to be satisfied with 8bpc.
1626 	 */
1627 	if (!respect_downstream_limits)
1628 		bpc = 8;
1629 
1630 	for (; bpc >= 8; bpc -= 2) {
1631 		if (intel_hdmi_bpc_possible(crtc_state, bpc,
1632 					    intel_dp_has_hdmi_sink(intel_dp)) &&
1633 		    intel_dp_tmds_clock_valid(intel_dp, clock, bpc, crtc_state->sink_format,
1634 					      respect_downstream_limits) == MODE_OK)
1635 			return bpc;
1636 	}
1637 
1638 	return -EINVAL;
1639 }
1640 
1641 static int intel_dp_max_bpp(struct intel_dp *intel_dp,
1642 			    const struct intel_crtc_state *crtc_state,
1643 			    bool respect_downstream_limits)
1644 {
1645 	struct intel_display *display = to_intel_display(intel_dp);
1646 	struct intel_connector *connector = intel_dp->attached_connector;
1647 	int bpp, bpc;
1648 
1649 	bpc = crtc_state->max_pipe_bpp / 3;
1650 
1651 	if (intel_dp->dfp.max_bpc)
1652 		bpc = min_t(int, bpc, intel_dp->dfp.max_bpc);
1653 
1654 	if (intel_dp->dfp.min_tmds_clock) {
1655 		int max_hdmi_bpc;
1656 
1657 		max_hdmi_bpc = intel_dp_hdmi_compute_bpc(intel_dp, crtc_state, bpc,
1658 							 respect_downstream_limits);
1659 		if (max_hdmi_bpc < 0)
1660 			return 0;
1661 
1662 		bpc = min(bpc, max_hdmi_bpc);
1663 	}
1664 
1665 	bpp = bpc * 3;
1666 	if (intel_dp_is_edp(intel_dp)) {
1667 		/* Get bpp from vbt only for panels that dont have bpp in edid */
1668 		if (connector->base.display_info.bpc == 0 &&
1669 		    connector->panel.vbt.edp.bpp &&
1670 		    connector->panel.vbt.edp.bpp < bpp) {
1671 			drm_dbg_kms(display->drm,
1672 				    "clamping bpp for eDP panel to BIOS-provided %i\n",
1673 				    connector->panel.vbt.edp.bpp);
1674 			bpp = connector->panel.vbt.edp.bpp;
1675 		}
1676 	}
1677 
1678 	return bpp;
1679 }
1680 
1681 static bool has_seamless_m_n(struct intel_connector *connector)
1682 {
1683 	struct intel_display *display = to_intel_display(connector);
1684 
1685 	/*
1686 	 * Seamless M/N reprogramming only implemented
1687 	 * for BDW+ double buffered M/N registers so far.
1688 	 */
1689 	return HAS_DOUBLE_BUFFERED_M_N(display) &&
1690 		intel_panel_drrs_type(connector) == DRRS_TYPE_SEAMLESS;
1691 }
1692 
1693 static int intel_dp_mode_clock(const struct intel_crtc_state *crtc_state,
1694 			       const struct drm_connector_state *conn_state)
1695 {
1696 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1697 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
1698 
1699 	/* FIXME a bit of a mess wrt clock vs. crtc_clock */
1700 	if (has_seamless_m_n(connector))
1701 		return intel_panel_highest_mode(connector, adjusted_mode)->clock;
1702 	else
1703 		return adjusted_mode->crtc_clock;
1704 }
1705 
1706 /* Optimize link config in order: max bpp, min clock, min lanes */
1707 static int
1708 intel_dp_compute_link_config_wide(struct intel_dp *intel_dp,
1709 				  struct intel_crtc_state *pipe_config,
1710 				  const struct drm_connector_state *conn_state,
1711 				  const struct link_config_limits *limits)
1712 {
1713 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1714 	int bpp, clock = intel_dp_mode_clock(pipe_config, conn_state);
1715 	struct intel_dp_link_caps *link_caps = intel_dp->link.caps;
1716 	struct intel_dp_link_caps_order order =
1717 		intel_dp_link_caps_connector_compute_order(connector);
1718 	int err = -EINVAL;
1719 	int link_avail;
1720 
1721 	for (bpp = fxp_q4_to_int(limits->link.max_bpp_x16);
1722 	     bpp >= fxp_q4_to_int(limits->link.min_bpp_x16);
1723 	     bpp -= 2 * 3) {
1724 		int link_bpp_x16 =
1725 			intel_dp_output_format_link_bpp_x16(pipe_config->output_format, bpp);
1726 		struct intel_dp_link_config link_config;
1727 		struct intel_dp_link_caps_iter iter;
1728 
1729 		intel_dp_link_caps_iter_start(&iter, link_caps, order, limits->link_config_filter);
1730 		for_each_dp_link_config(&iter, &link_config) {
1731 			const struct drm_display_mode *adjusted_mode =
1732 					&pipe_config->hw.adjusted_mode;
1733 			int mode_rate;
1734 
1735 			mode_rate = intel_dp_link_required(link_config.rate,
1736 							   link_config.lane_count,
1737 							   clock, adjusted_mode->hdisplay,
1738 							   link_bpp_x16, 0);
1739 
1740 			link_avail = intel_dp_max_link_data_rate(intel_dp,
1741 								 link_config.rate,
1742 								 link_config.lane_count);
1743 
1744 			if (mode_rate <= link_avail) {
1745 				pipe_config->lane_count = link_config.lane_count;
1746 				pipe_config->pipe_bpp = bpp;
1747 				pipe_config->port_clock = link_config.rate;
1748 
1749 				err = 0;
1750 
1751 				break;
1752 			}
1753 		}
1754 		intel_dp_link_caps_iter_end(&iter);
1755 
1756 		if (!err)
1757 			break;
1758 	}
1759 
1760 	return err;
1761 }
1762 
1763 int intel_dp_dsc_max_src_input_bpc(struct intel_display *display)
1764 {
1765 	/* Max DSC Input BPC for ICL is 10 and for TGL+ is 12 */
1766 	if (DISPLAY_VER(display) >= 12)
1767 		return 12;
1768 	if (DISPLAY_VER(display) == 11)
1769 		return 10;
1770 
1771 	return intel_dp_dsc_min_src_input_bpc();
1772 }
1773 
1774 static int align_min_sink_dsc_input_bpp(const struct intel_connector *connector,
1775 					int min_pipe_bpp)
1776 {
1777 	u8 dsc_bpc[3];
1778 	int num_bpc;
1779 	int i;
1780 
1781 	num_bpc = drm_dp_dsc_sink_supported_input_bpcs(connector->dp.dsc_dpcd,
1782 						       dsc_bpc);
1783 	for (i = num_bpc - 1; i >= 0; i--) {
1784 		if (dsc_bpc[i] * 3 >= min_pipe_bpp)
1785 			return dsc_bpc[i] * 3;
1786 	}
1787 
1788 	return 0;
1789 }
1790 
1791 static int align_max_sink_dsc_input_bpp(const struct intel_connector *connector,
1792 					int max_pipe_bpp)
1793 {
1794 	u8 dsc_bpc[3];
1795 	int num_bpc;
1796 	int i;
1797 
1798 	num_bpc = drm_dp_dsc_sink_supported_input_bpcs(connector->dp.dsc_dpcd,
1799 						       dsc_bpc);
1800 	for (i = 0; i < num_bpc; i++) {
1801 		if (dsc_bpc[i] * 3 <= max_pipe_bpp)
1802 			return dsc_bpc[i] * 3;
1803 	}
1804 
1805 	return 0;
1806 }
1807 
1808 int intel_dp_dsc_compute_max_bpp(const struct intel_connector *connector,
1809 				 u8 max_req_bpc)
1810 {
1811 	struct intel_display *display = to_intel_display(connector);
1812 	int dsc_max_bpc;
1813 
1814 	dsc_max_bpc = intel_dp_dsc_max_src_input_bpc(display);
1815 
1816 	if (!dsc_max_bpc)
1817 		return dsc_max_bpc;
1818 
1819 	dsc_max_bpc = min(dsc_max_bpc, max_req_bpc);
1820 
1821 	return align_max_sink_dsc_input_bpp(connector, dsc_max_bpc * 3);
1822 }
1823 
1824 static int intel_dp_source_dsc_version_minor(struct intel_display *display)
1825 {
1826 	return DISPLAY_VER(display) >= 14 ? 2 : 1;
1827 }
1828 
1829 static int intel_dp_sink_dsc_version_minor(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE])
1830 {
1831 	return (dsc_dpcd[DP_DSC_REV - DP_DSC_SUPPORT] & DP_DSC_MINOR_MASK) >>
1832 		DP_DSC_MINOR_SHIFT;
1833 }
1834 
1835 static int intel_dp_get_slice_height(int vactive)
1836 {
1837 	int slice_height;
1838 
1839 	/*
1840 	 * VDSC 1.2a spec in Section 3.8 Options for Slices implies that 108
1841 	 * lines is an optimal slice height, but any size can be used as long as
1842 	 * vertical active integer multiple and maximum vertical slice count
1843 	 * requirements are met.
1844 	 */
1845 	for (slice_height = 108; slice_height <= vactive; slice_height += 2)
1846 		if (vactive % slice_height == 0)
1847 			return slice_height;
1848 
1849 	/*
1850 	 * Highly unlikely we reach here as most of the resolutions will end up
1851 	 * finding appropriate slice_height in above loop but returning
1852 	 * slice_height as 2 here as it should work with all resolutions.
1853 	 */
1854 	return 2;
1855 }
1856 
1857 static int intel_dp_dsc_compute_params(const struct intel_connector *connector,
1858 				       struct intel_crtc_state *crtc_state)
1859 {
1860 	struct intel_display *display = to_intel_display(connector);
1861 	struct drm_dsc_config *vdsc_cfg = &crtc_state->dsc.config;
1862 	int ret;
1863 
1864 	/*
1865 	 * RC_MODEL_SIZE is currently a constant across all configurations.
1866 	 *
1867 	 * FIXME: Look into using sink defined DPCD DP_DSC_RC_BUF_BLK_SIZE and
1868 	 * DP_DSC_RC_BUF_SIZE for this.
1869 	 */
1870 	vdsc_cfg->rc_model_size = DSC_RC_MODEL_SIZE_CONST;
1871 	vdsc_cfg->pic_height = crtc_state->hw.adjusted_mode.crtc_vdisplay;
1872 
1873 	vdsc_cfg->slice_height = intel_dp_get_slice_height(vdsc_cfg->pic_height);
1874 
1875 	ret = intel_dsc_compute_params(crtc_state);
1876 	if (ret)
1877 		return ret;
1878 
1879 	vdsc_cfg->dsc_version_major =
1880 		(connector->dp.dsc_dpcd[DP_DSC_REV - DP_DSC_SUPPORT] &
1881 		 DP_DSC_MAJOR_MASK) >> DP_DSC_MAJOR_SHIFT;
1882 	vdsc_cfg->dsc_version_minor =
1883 		min(intel_dp_source_dsc_version_minor(display),
1884 		    intel_dp_sink_dsc_version_minor(connector->dp.dsc_dpcd));
1885 	if (vdsc_cfg->convert_rgb)
1886 		vdsc_cfg->convert_rgb =
1887 			connector->dp.dsc_dpcd[DP_DSC_DEC_COLOR_FORMAT_CAP - DP_DSC_SUPPORT] &
1888 			DP_DSC_RGB;
1889 
1890 	vdsc_cfg->line_buf_depth = min(INTEL_DP_DSC_MAX_LINE_BUF_DEPTH,
1891 				       drm_dp_dsc_sink_line_buf_depth(connector->dp.dsc_dpcd));
1892 	if (!vdsc_cfg->line_buf_depth) {
1893 		drm_dbg_kms(display->drm,
1894 			    "DSC Sink Line Buffer Depth invalid\n");
1895 		return -EINVAL;
1896 	}
1897 
1898 	vdsc_cfg->block_pred_enable =
1899 		connector->dp.dsc_dpcd[DP_DSC_BLK_PREDICTION_SUPPORT - DP_DSC_SUPPORT] &
1900 		DP_DSC_BLK_PREDICTION_IS_SUPPORTED;
1901 
1902 	return drm_dsc_compute_rc_parameters(vdsc_cfg);
1903 }
1904 
1905 static bool intel_dp_dsc_supports_format(const struct intel_connector *connector,
1906 					 enum intel_output_format output_format)
1907 {
1908 	struct intel_display *display = to_intel_display(connector);
1909 	u8 sink_dsc_format;
1910 
1911 	switch (output_format) {
1912 	case INTEL_OUTPUT_FORMAT_RGB:
1913 		sink_dsc_format = DP_DSC_RGB;
1914 		break;
1915 	case INTEL_OUTPUT_FORMAT_YCBCR444:
1916 		sink_dsc_format = DP_DSC_YCbCr444;
1917 		break;
1918 	case INTEL_OUTPUT_FORMAT_YCBCR420:
1919 		if (min(intel_dp_source_dsc_version_minor(display),
1920 			intel_dp_sink_dsc_version_minor(connector->dp.dsc_dpcd)) < 2)
1921 			return false;
1922 		sink_dsc_format = DP_DSC_YCbCr420_Native;
1923 		break;
1924 	default:
1925 		return false;
1926 	}
1927 
1928 	return drm_dp_dsc_sink_supports_format(connector->dp.dsc_dpcd, sink_dsc_format);
1929 }
1930 
1931 static bool is_bw_sufficient_for_dsc_config(struct intel_dp *intel_dp,
1932 					    int link_clock, int lane_count,
1933 					    int mode_clock, int mode_hdisplay,
1934 					    int dsc_slice_count, int link_bpp_x16,
1935 					    unsigned long bw_overhead_flags)
1936 {
1937 	int available_bw;
1938 	int required_bw;
1939 
1940 	available_bw = intel_dp_max_link_data_rate(intel_dp, link_clock, lane_count);
1941 	required_bw = intel_dp_link_required(link_clock, lane_count,
1942 					     mode_clock, mode_hdisplay,
1943 					     link_bpp_x16, bw_overhead_flags);
1944 
1945 	return available_bw >= required_bw;
1946 }
1947 
1948 static int dsc_compute_link_config(struct intel_dp *intel_dp,
1949 				   struct intel_crtc_state *pipe_config,
1950 				   struct drm_connector_state *conn_state,
1951 				   const struct link_config_limits *limits,
1952 				   int dsc_bpp_x16)
1953 {
1954 	const struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
1955 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1956 	struct intel_dp_link_caps *link_caps = intel_dp->link.caps;
1957 	struct intel_dp_link_caps_order order =
1958 		intel_dp_link_caps_connector_compute_order(connector);
1959 	struct intel_dp_link_config link_config;
1960 	struct intel_dp_link_caps_iter iter;
1961 	int err = -EINVAL;
1962 
1963 	intel_dp_link_caps_iter_start(&iter, link_caps, order, limits->link_config_filter);
1964 	for_each_dp_link_config(&iter, &link_config) {
1965 		/*
1966 		 * FIXME: intel_dp_mtp_tu_compute_config() requires
1967 		 * ->lane_count and ->port_clock set before we know
1968 		 * they'll work. If we end up failing altogether,
1969 		 * they'll remain in crtc state. This shouldn't matter,
1970 		 * as we'd then bail out from compute config, but it's
1971 		 * just ugly.
1972 		 */
1973 		pipe_config->lane_count = link_config.lane_count;
1974 		pipe_config->port_clock = link_config.rate;
1975 
1976 		if (drm_dp_is_uhbr_rate(link_config.rate)) {
1977 			int ret;
1978 
1979 			ret = intel_dp_mtp_tu_compute_config(intel_dp,
1980 							     pipe_config,
1981 							     conn_state,
1982 							     dsc_bpp_x16,
1983 							     dsc_bpp_x16,
1984 							     0, true);
1985 			if (ret)
1986 				continue;
1987 		} else {
1988 			unsigned long bw_overhead_flags =
1989 				pipe_config->fec_enable ? DRM_DP_BW_OVERHEAD_FEC : 0;
1990 			int line_slice_count =
1991 				intel_dsc_line_slice_count(&pipe_config->dsc.slice_config);
1992 
1993 			if (!is_bw_sufficient_for_dsc_config(intel_dp,
1994 							     link_config.rate,
1995 							     link_config.lane_count,
1996 							     adjusted_mode->crtc_clock,
1997 							     adjusted_mode->hdisplay,
1998 							     line_slice_count,
1999 							     dsc_bpp_x16,
2000 							     bw_overhead_flags))
2001 				continue;
2002 		}
2003 
2004 		err = 0;
2005 
2006 		break;
2007 	}
2008 	intel_dp_link_caps_iter_end(&iter);
2009 
2010 	return err;
2011 }
2012 
2013 static u16 intel_dp_dsc_max_delta_bppx16(const struct intel_connector *connector,
2014 					 enum intel_output_format output_format)
2015 {
2016 	const u8 *dsc_dpcd = connector->dp.dsc_dpcd;
2017 	u8 max_bpp_delta_v1 = dsc_dpcd[DP_DSC_MAX_BPP_DELTA_VERSION_1 - DP_DSC_SUPPORT];
2018 	int max_bpp;
2019 
2020 	if (!(dsc_dpcd[DP_DSC_MAX_BITS_PER_PIXEL_HI - DP_DSC_SUPPORT] &
2021 	    DP_DSC_MAX_BPP_DELTA_AVAILABILITY))
2022 		return 0;
2023 
2024 	switch (output_format) {
2025 	case INTEL_OUTPUT_FORMAT_RGB:
2026 	case INTEL_OUTPUT_FORMAT_YCBCR444:
2027 		max_bpp =  max_bpp_delta_v1 & DP_DSC_RGB_YCbCr444_MAX_BPP_DELTA_MASK;
2028 		if (max_bpp >= 1 && max_bpp <= 21)
2029 			max_bpp =  max_bpp + DP_DSC_BPP_DELTA_444 - 1;
2030 		else
2031 			max_bpp = 0;
2032 		break;
2033 	case INTEL_OUTPUT_FORMAT_YCBCR420:
2034 		max_bpp = (max_bpp_delta_v1 & DP_DSC_NATIVE_YCbCr420_MAX_BPP_DELTA_MASK) >>
2035 			  DP_DSC_BPP_DELTA_SHIFT_420;
2036 		if (max_bpp >= 1 && max_bpp <= 7)
2037 			max_bpp = max_bpp + DP_DSC_BPP_DELTA_420 - 1;
2038 		break;
2039 	default:
2040 		MISSING_CASE(output_format);
2041 		return 0;
2042 	}
2043 
2044 	return max_bpp << 4;
2045 }
2046 
2047 static
2048 u16 intel_dp_dsc_max_sink_compressed_bppx16(const struct intel_connector *connector,
2049 					    enum intel_output_format output_format,
2050 					    int bpc)
2051 {
2052 	u16 max_bppx16 = intel_dp_dsc_max_delta_bppx16(connector, output_format);
2053 
2054 	if (max_bppx16)
2055 		return max_bppx16;
2056 
2057 	max_bppx16 = drm_edp_dsc_sink_output_bpp(connector->dp.dsc_dpcd);
2058 
2059 	if (max_bppx16)
2060 		return max_bppx16;
2061 	/*
2062 	 * If support not given in DPCD 67h, 68h, 6Eh, 6Fh use the Maximum Allowed bit rate
2063 	 * values as given in spec Table 2-157 DP v2.0
2064 	 */
2065 	switch (output_format) {
2066 	case INTEL_OUTPUT_FORMAT_RGB:
2067 	case INTEL_OUTPUT_FORMAT_YCBCR444:
2068 		return (3 * bpc) << 4;
2069 	case INTEL_OUTPUT_FORMAT_YCBCR420:
2070 		return (3 * (bpc / 2)) << 4;
2071 	default:
2072 		MISSING_CASE(output_format);
2073 		break;
2074 	}
2075 
2076 	return 0;
2077 }
2078 
2079 static int intel_dp_dsc_sink_min_compressed_bpp(enum intel_output_format output_format)
2080 {
2081 	/* From Mandatory bit rate range Support Table 2-157 (DP v2.0) */
2082 	switch (output_format) {
2083 	case INTEL_OUTPUT_FORMAT_RGB:
2084 	case INTEL_OUTPUT_FORMAT_YCBCR444:
2085 		return 8;
2086 	case INTEL_OUTPUT_FORMAT_YCBCR420:
2087 		return 6;
2088 	default:
2089 		MISSING_CASE(output_format);
2090 		break;
2091 	}
2092 
2093 	return 0;
2094 }
2095 
2096 static int intel_dp_dsc_sink_max_compressed_bpp(const struct intel_connector *connector,
2097 						enum intel_output_format output_format,
2098 						int bpc)
2099 {
2100 	return intel_dp_dsc_max_sink_compressed_bppx16(connector,
2101 						       output_format, bpc) >> 4;
2102 }
2103 
2104 int intel_dp_dsc_min_src_compressed_bpp(void)
2105 {
2106 	/* Min Compressed bpp supported by source is 8 */
2107 	return 8;
2108 }
2109 
2110 static int dsc_src_max_compressed_bpp(struct intel_dp *intel_dp)
2111 {
2112 	struct intel_display *display = to_intel_display(intel_dp);
2113 
2114 	/*
2115 	 * Forcing DSC and using the platform's max compressed bpp is seen to cause
2116 	 * underruns. Since DSC isn't needed in these cases, limit the
2117 	 * max compressed bpp to 18, which is a safe value across platforms with different
2118 	 * pipe bpps.
2119 	 */
2120 	if (intel_dp->force_dsc_en)
2121 		return 18;
2122 
2123 	/*
2124 	 * Max Compressed bpp for Gen 13+ is 27bpp.
2125 	 * For earlier platform is 23bpp. (Bspec:49259).
2126 	 */
2127 	if (DISPLAY_VER(display) < 13)
2128 		return 23;
2129 	else
2130 		return 27;
2131 }
2132 
2133 /*
2134  * Note: for pre-13 display you still need to check the validity of each step.
2135  */
2136 int intel_dp_dsc_bpp_step_x16(const struct intel_connector *connector)
2137 {
2138 	struct intel_display *display = to_intel_display(connector);
2139 	u8 incr = drm_dp_dsc_sink_bpp_incr(connector->dp.dsc_dpcd);
2140 
2141 	if (DISPLAY_VER(display) < 14 || !incr)
2142 		return fxp_q4_from_int(1);
2143 
2144 	if (connector->mst.dp &&
2145 	    !connector->link.force_bpp_x16 && !connector->mst.dp->force_dsc_fractional_bpp_en)
2146 		return fxp_q4_from_int(1);
2147 
2148 	/* fxp q4 */
2149 	return fxp_q4_from_int(1) / incr;
2150 }
2151 
2152 /*
2153  * Note: for bpp_x16 to be valid it must be also within the source/sink's
2154  * min..max bpp capability range.
2155  */
2156 bool intel_dp_dsc_valid_compressed_bpp(struct intel_dp *intel_dp, int bpp_x16)
2157 {
2158 	struct intel_display *display = to_intel_display(intel_dp);
2159 
2160 	if (DISPLAY_VER(display) >= 13) {
2161 		if (intel_dp->force_dsc_fractional_bpp_en && !fxp_q4_to_frac(bpp_x16))
2162 			return false;
2163 
2164 		return true;
2165 	}
2166 
2167 	if (fxp_q4_to_frac(bpp_x16))
2168 		return false;
2169 
2170 	return align_max_vesa_compressed_bpp_x16(bpp_x16) == bpp_x16;
2171 }
2172 
2173 static int align_min_compressed_bpp_x16(const struct intel_connector *connector, int min_bpp_x16)
2174 {
2175 	struct intel_display *display = to_intel_display(connector);
2176 
2177 	if (DISPLAY_VER(display) >= 13) {
2178 		int bpp_step_x16 = intel_dp_dsc_bpp_step_x16(connector);
2179 
2180 		drm_WARN_ON(display->drm, !is_power_of_2(bpp_step_x16));
2181 
2182 		return round_up(min_bpp_x16, bpp_step_x16);
2183 	} else {
2184 		return align_min_vesa_compressed_bpp_x16(min_bpp_x16);
2185 	}
2186 }
2187 
2188 static int align_max_compressed_bpp_x16(const struct intel_connector *connector,
2189 					enum intel_output_format output_format,
2190 					int pipe_bpp, int max_bpp_x16)
2191 {
2192 	struct intel_display *display = to_intel_display(connector);
2193 	int link_bpp_x16 = intel_dp_output_format_link_bpp_x16(output_format, pipe_bpp);
2194 	int bpp_step_x16 = intel_dp_dsc_bpp_step_x16(connector);
2195 
2196 	max_bpp_x16 = min(max_bpp_x16, link_bpp_x16 - bpp_step_x16);
2197 
2198 	if (DISPLAY_VER(display) >= 13) {
2199 		drm_WARN_ON(display->drm, !is_power_of_2(bpp_step_x16));
2200 
2201 		return round_down(max_bpp_x16, bpp_step_x16);
2202 	} else {
2203 		return align_max_vesa_compressed_bpp_x16(max_bpp_x16);
2204 	}
2205 }
2206 
2207 /*
2208  * Find the max compressed BPP we can find a link configuration for. The BPPs to
2209  * try depend on the source (platform) and sink.
2210  */
2211 static int dsc_compute_compressed_bpp(struct intel_dp *intel_dp,
2212 				      struct intel_crtc_state *pipe_config,
2213 				      struct drm_connector_state *conn_state,
2214 				      const struct link_config_limits *limits,
2215 				      int pipe_bpp)
2216 {
2217 	struct intel_display *display = to_intel_display(intel_dp);
2218 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
2219 	int min_bpp_x16, max_bpp_x16, bpp_step_x16;
2220 	int bpp_x16;
2221 	int ret;
2222 
2223 	min_bpp_x16 = limits->link.min_bpp_x16;
2224 	max_bpp_x16 = limits->link.max_bpp_x16;
2225 	bpp_step_x16 = intel_dp_dsc_bpp_step_x16(connector);
2226 
2227 	max_bpp_x16 = align_max_compressed_bpp_x16(connector, pipe_config->output_format,
2228 						   pipe_bpp, max_bpp_x16);
2229 	if (intel_dp_is_edp(intel_dp)) {
2230 		struct intel_dp_link_config max_link_config;
2231 
2232 		/*
2233 		 * FIXME: Clarify why eDP does not use the regular SST BW
2234 		 * check and instead always uses the maximum link config,
2235 		 * regardless of intel_dp::use_max_params. Then unify this eDP
2236 		 * path with the regular DP path.
2237 		 */
2238 		if (!intel_dp_get_connector_max_link_config(connector, limits, &max_link_config))
2239 			return -EINVAL;
2240 
2241 		pipe_config->port_clock = max_link_config.rate;
2242 		pipe_config->lane_count = max_link_config.lane_count;
2243 
2244 		pipe_config->dsc.compressed_bpp_x16 = max_bpp_x16;
2245 
2246 		return 0;
2247 	}
2248 
2249 	for (bpp_x16 = max_bpp_x16; bpp_x16 >= min_bpp_x16; bpp_x16 -= bpp_step_x16) {
2250 		if (!intel_dp_dsc_valid_compressed_bpp(intel_dp, bpp_x16))
2251 			continue;
2252 
2253 		ret = dsc_compute_link_config(intel_dp,
2254 					      pipe_config,
2255 					      conn_state,
2256 					      limits,
2257 					      bpp_x16);
2258 		if (ret == 0) {
2259 			pipe_config->dsc.compressed_bpp_x16 = bpp_x16;
2260 			if (intel_dp->force_dsc_fractional_bpp_en &&
2261 			    fxp_q4_to_frac(bpp_x16))
2262 				drm_dbg_kms(display->drm,
2263 					    "Forcing DSC fractional bpp\n");
2264 
2265 			return 0;
2266 		}
2267 	}
2268 
2269 	return -EINVAL;
2270 }
2271 
2272 int intel_dp_dsc_min_src_input_bpc(void)
2273 {
2274 	/* Min DSC Input BPC for ICL+ is 8 */
2275 	return 8;
2276 }
2277 
2278 static
2279 bool is_dsc_pipe_bpp_sufficient(const struct link_config_limits *limits,
2280 				int pipe_bpp)
2281 {
2282 	return pipe_bpp >= limits->pipe.min_bpp &&
2283 	       pipe_bpp <= limits->pipe.max_bpp;
2284 }
2285 
2286 static
2287 int intel_dp_force_dsc_pipe_bpp(struct intel_dp *intel_dp,
2288 				const struct link_config_limits *limits)
2289 {
2290 	struct intel_display *display = to_intel_display(intel_dp);
2291 	int forced_bpp;
2292 
2293 	if (!intel_dp->force_dsc_bpc)
2294 		return 0;
2295 
2296 	forced_bpp = intel_dp->force_dsc_bpc * 3;
2297 
2298 	if (is_dsc_pipe_bpp_sufficient(limits, forced_bpp)) {
2299 		drm_dbg_kms(display->drm, "Input DSC BPC forced to %d\n",
2300 			    intel_dp->force_dsc_bpc);
2301 		return forced_bpp;
2302 	}
2303 
2304 	drm_dbg_kms(display->drm,
2305 		    "Cannot force DSC BPC:%d, due to DSC BPC limits\n",
2306 		    intel_dp->force_dsc_bpc);
2307 
2308 	return 0;
2309 }
2310 
2311 static int intel_dp_dsc_compute_pipe_bpp(struct intel_dp *intel_dp,
2312 					 struct intel_crtc_state *pipe_config,
2313 					 struct drm_connector_state *conn_state,
2314 					 const struct link_config_limits *limits)
2315 {
2316 	int forced_bpp, pipe_bpp;
2317 	int ret;
2318 
2319 	forced_bpp = intel_dp_force_dsc_pipe_bpp(intel_dp, limits);
2320 	if (forced_bpp)
2321 		pipe_bpp = forced_bpp;
2322 	else
2323 		pipe_bpp = limits->pipe.max_bpp;
2324 
2325 	ret = dsc_compute_compressed_bpp(intel_dp, pipe_config, conn_state,
2326 					 limits, pipe_bpp);
2327 	if (ret)
2328 		return -EINVAL;
2329 
2330 	pipe_config->pipe_bpp = pipe_bpp;
2331 
2332 	return 0;
2333 }
2334 
2335 /*
2336  * Return whether FEC must be enabled for 8b10b SST or MST links. On 128b132b
2337  * links FEC is always enabled implicitly by the HW, so this function returns
2338  * false for that case.
2339  */
2340 bool intel_dp_needs_8b10b_fec(const struct intel_crtc_state *crtc_state,
2341 			      bool dsc_enabled_on_crtc)
2342 {
2343 	if (intel_dp_is_uhbr(crtc_state))
2344 		return false;
2345 
2346 	/*
2347 	 * Though eDP v1.5 supports FEC with DSC, unlike DP, it is optional.
2348 	 * Since, FEC is a bandwidth overhead, continue to not enable it for
2349 	 * eDP. Until, there is a good reason to do so.
2350 	 */
2351 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP))
2352 		return false;
2353 
2354 	return dsc_enabled_on_crtc || intel_dsc_enabled_on_link(crtc_state);
2355 }
2356 
2357 void intel_dp_dsc_reset_config(struct intel_crtc_state *crtc_state)
2358 {
2359 	crtc_state->fec_enable = false;
2360 
2361 	crtc_state->dsc.compression_enable = false;
2362 	crtc_state->dsc.compressed_bpp_x16 = 0;
2363 
2364 	memset(&crtc_state->dsc.slice_config, 0, sizeof(crtc_state->dsc.slice_config));
2365 	memset(&crtc_state->dsc.config, 0, sizeof(crtc_state->dsc.config));
2366 }
2367 
2368 int intel_dp_dsc_compute_config(struct intel_dp *intel_dp,
2369 				struct intel_crtc_state *pipe_config,
2370 				struct drm_connector_state *conn_state,
2371 				const struct link_config_limits *limits,
2372 				int timeslots)
2373 {
2374 	struct intel_display *display = to_intel_display(intel_dp);
2375 	const struct intel_connector *connector =
2376 		to_intel_connector(conn_state->connector);
2377 	const struct drm_display_mode *adjusted_mode =
2378 		&pipe_config->hw.adjusted_mode;
2379 	int num_joined_pipes = intel_crtc_num_joined_pipes(pipe_config);
2380 	bool is_mst = intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DP_MST);
2381 	int ret;
2382 
2383 	/*
2384 	 * FIXME: set the FEC enabled state once pipe_config->port_clock is
2385 	 * already known, so the UHBR/non-UHBR mode can be determined.
2386 	 */
2387 	pipe_config->fec_enable = intel_dp_needs_8b10b_fec(pipe_config, true);
2388 
2389 	if (!intel_dp_dsc_supports_format(connector, pipe_config->output_format))
2390 		return -EINVAL;
2391 
2392 	/*
2393 	 * Link parameters, pipe bpp and compressed bpp have already been
2394 	 * figured out for DP MST DSC.
2395 	 */
2396 	if (!is_mst) {
2397 		ret = intel_dp_dsc_compute_pipe_bpp(intel_dp, pipe_config,
2398 						    conn_state, limits);
2399 		if (ret) {
2400 			drm_dbg_kms(display->drm,
2401 				    "No Valid pipe bpp for given mode ret = %d\n", ret);
2402 			return ret;
2403 		}
2404 	}
2405 
2406 	if (!intel_dp_dsc_get_slice_config(connector, adjusted_mode->crtc_clock,
2407 					   adjusted_mode->crtc_hdisplay, num_joined_pipes,
2408 					   &pipe_config->dsc.slice_config))
2409 		return -EINVAL;
2410 
2411 	ret = intel_dp_dsc_compute_params(connector, pipe_config);
2412 	if (ret < 0) {
2413 		drm_dbg_kms(display->drm,
2414 			    "Cannot compute valid DSC parameters for Input Bpp = %d"
2415 			    "Compressed BPP = " FXP_Q4_FMT "\n",
2416 			    pipe_config->pipe_bpp,
2417 			    FXP_Q4_ARGS(pipe_config->dsc.compressed_bpp_x16));
2418 		return ret;
2419 	}
2420 
2421 	intel_dsc_enable_on_crtc(pipe_config);
2422 
2423 	drm_dbg_kms(display->drm, "DP DSC computed with Input Bpp = %d "
2424 		    "Compressed Bpp = " FXP_Q4_FMT " Slice Count = %d\n",
2425 		    pipe_config->pipe_bpp,
2426 		    FXP_Q4_ARGS(pipe_config->dsc.compressed_bpp_x16),
2427 		    intel_dsc_line_slice_count(&pipe_config->dsc.slice_config));
2428 
2429 	return 0;
2430 }
2431 
2432 static int
2433 dsc_throughput_quirk_max_bpp_x16(const struct intel_connector *connector,
2434 				 int mode_clock)
2435 {
2436 	if (!connector->dp.dsc_throughput_quirk)
2437 		return INT_MAX;
2438 
2439 	/*
2440 	 * Synaptics Panamera branch devices have a problem decompressing a
2441 	 * stream with a compressed link-bpp higher than 12, if the pixel
2442 	 * clock is higher than ~50 % of the maximum overall throughput
2443 	 * reported by the branch device. Work around this by limiting the
2444 	 * maximum link bpp for such pixel clocks.
2445 	 *
2446 	 * TODO: Use the throughput value specific to the actual RGB/YUV
2447 	 * format of the output, after determining the pixel clock limit for
2448 	 * YUV modes. For now use the smaller of the throughput values, which
2449 	 * may result in limiting the link-bpp value already at a lower than
2450 	 * required mode clock in case of native YUV422/420 output formats.
2451 	 * The RGB/YUV444 throughput value should be always either equal or
2452 	 * smaller than the YUV422/420 value, but let's not depend on this
2453 	 * assumption.
2454 	 */
2455 	if (mode_clock <
2456 	    min(connector->dp.dsc_branch_caps.overall_throughput.rgb_yuv444,
2457 		connector->dp.dsc_branch_caps.overall_throughput.yuv422_420) / 2)
2458 		return INT_MAX;
2459 
2460 	return fxp_q4_from_int(12);
2461 }
2462 
2463 int intel_dp_compute_min_compressed_bpp_x16(struct intel_connector *connector,
2464 					    enum intel_output_format output_format)
2465 {
2466 	int dsc_src_min_bpp, dsc_sink_min_bpp, dsc_min_bpp;
2467 	int min_bpp_x16;
2468 
2469 	dsc_src_min_bpp = intel_dp_dsc_min_src_compressed_bpp();
2470 	dsc_sink_min_bpp = intel_dp_dsc_sink_min_compressed_bpp(output_format);
2471 	dsc_min_bpp = max(dsc_src_min_bpp, dsc_sink_min_bpp);
2472 
2473 	min_bpp_x16 = fxp_q4_from_int(dsc_min_bpp);
2474 
2475 	min_bpp_x16 = align_min_compressed_bpp_x16(connector, min_bpp_x16);
2476 
2477 	return min_bpp_x16;
2478 }
2479 
2480 static int compute_max_compressed_bpp_x16(struct intel_connector *connector,
2481 					  int mode_clock, int mode_hdisplay,
2482 					  int num_joined_pipes,
2483 					  enum intel_output_format output_format,
2484 					  int pipe_max_bpp, int max_link_bpp_x16)
2485 {
2486 	struct intel_display *display = to_intel_display(connector);
2487 	struct intel_dp *intel_dp = intel_attached_dp(connector);
2488 	int dsc_src_max_bpp, dsc_sink_max_bpp, dsc_max_bpp;
2489 	int throughput_max_bpp_x16;
2490 	int joiner_max_bpp;
2491 
2492 	dsc_src_max_bpp = dsc_src_max_compressed_bpp(intel_dp);
2493 	joiner_max_bpp = get_max_compressed_bpp_with_joiner(display,
2494 							    mode_clock,
2495 							    mode_hdisplay,
2496 							    num_joined_pipes);
2497 	dsc_sink_max_bpp = intel_dp_dsc_sink_max_compressed_bpp(connector,
2498 								output_format,
2499 								pipe_max_bpp / 3);
2500 	dsc_max_bpp = min(dsc_sink_max_bpp, dsc_src_max_bpp);
2501 	dsc_max_bpp = min(dsc_max_bpp, joiner_max_bpp);
2502 
2503 	max_link_bpp_x16 = min(max_link_bpp_x16, fxp_q4_from_int(dsc_max_bpp));
2504 
2505 	throughput_max_bpp_x16 = dsc_throughput_quirk_max_bpp_x16(connector,
2506 								  mode_clock);
2507 	if (throughput_max_bpp_x16 < max_link_bpp_x16) {
2508 		max_link_bpp_x16 = throughput_max_bpp_x16;
2509 
2510 		drm_dbg_kms(display->drm,
2511 			    "[CONNECTOR:%d:%s] Decreasing link max bpp to " FXP_Q4_FMT " due to DSC throughput quirk\n",
2512 			    connector->base.base.id, connector->base.name,
2513 			    FXP_Q4_ARGS(max_link_bpp_x16));
2514 	}
2515 
2516 	max_link_bpp_x16 = align_max_compressed_bpp_x16(connector, output_format,
2517 							pipe_max_bpp, max_link_bpp_x16);
2518 
2519 	return max_link_bpp_x16;
2520 }
2521 
2522 bool intel_dp_mode_valid_with_dsc(struct intel_connector *connector,
2523 				  int link_clock, int lane_count,
2524 				  int mode_clock, int mode_hdisplay,
2525 				  int num_joined_pipes,
2526 				  enum intel_output_format output_format,
2527 				  int pipe_bpp, unsigned long bw_overhead_flags)
2528 {
2529 	struct intel_dp *intel_dp = intel_attached_dp(connector);
2530 	int min_bpp_x16 = intel_dp_compute_min_compressed_bpp_x16(connector,
2531 								  output_format);
2532 	int max_bpp_x16 = compute_max_compressed_bpp_x16(connector,
2533 							 mode_clock, mode_hdisplay,
2534 							 num_joined_pipes,
2535 							 output_format,
2536 							 pipe_bpp, INT_MAX);
2537 	int dsc_slice_count = intel_dp_dsc_get_slice_count(connector,
2538 							   mode_clock,
2539 							   mode_hdisplay,
2540 							   num_joined_pipes);
2541 
2542 	if (min_bpp_x16 <= 0 || min_bpp_x16 > max_bpp_x16)
2543 		return false;
2544 
2545 	if (dsc_slice_count == 0)
2546 		return false;
2547 
2548 	return is_bw_sufficient_for_dsc_config(intel_dp,
2549 					       link_clock, lane_count,
2550 					       mode_clock, mode_hdisplay,
2551 					       dsc_slice_count, min_bpp_x16,
2552 					       bw_overhead_flags);
2553 }
2554 
2555 bool
2556 intel_dp_get_connector_max_link_config(struct intel_connector *connector,
2557 				       const struct link_config_limits *limits,
2558 				       struct intel_dp_link_config *max_link_config)
2559 {
2560 	struct intel_dp *intel_dp = intel_attached_dp(connector);
2561 	struct intel_dp_link_caps *link_caps = intel_dp->link.caps;
2562 	struct intel_dp_link_caps_order order =
2563 		intel_dp_link_caps_connector_compute_order(connector);
2564 
2565 	return intel_dp_link_caps_get_max_config(link_caps, order.key, limits->link_config_filter,
2566 						 max_link_config);
2567 }
2568 
2569 /*
2570  * Calculate the output link min, max bpp values in limits based on the pipe bpp
2571  * range, crtc_state and dsc mode. Return true on success.
2572  */
2573 static bool
2574 intel_dp_compute_config_link_bpp_limits(struct intel_connector *connector,
2575 					const struct intel_crtc_state *crtc_state,
2576 					bool dsc,
2577 					struct link_config_limits *limits)
2578 {
2579 	struct intel_display *display = to_intel_display(connector);
2580 	struct intel_dp *intel_dp = intel_attached_dp(connector);
2581 	const struct drm_display_mode *adjusted_mode =
2582 		&crtc_state->hw.adjusted_mode;
2583 	const struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2584 	const struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
2585 	struct intel_dp_link_config max_link_config;
2586 	int max_link_bpp_x16;
2587 
2588 	max_link_bpp_x16 = min(crtc_state->max_link_bpp_x16,
2589 			       fxp_q4_from_int(limits->pipe.max_bpp));
2590 
2591 	if (!dsc) {
2592 		max_link_bpp_x16 = rounddown(max_link_bpp_x16, fxp_q4_from_int(2 * 3));
2593 
2594 		if (max_link_bpp_x16 < fxp_q4_from_int(limits->pipe.min_bpp))
2595 			return false;
2596 
2597 		limits->link.min_bpp_x16 = fxp_q4_from_int(limits->pipe.min_bpp);
2598 	} else {
2599 		limits->link.min_bpp_x16 =
2600 			intel_dp_compute_min_compressed_bpp_x16(connector,
2601 								crtc_state->output_format);
2602 
2603 		max_link_bpp_x16 =
2604 			compute_max_compressed_bpp_x16(connector,
2605 						       adjusted_mode->crtc_clock,
2606 						       adjusted_mode->hdisplay,
2607 						       intel_crtc_num_joined_pipes(crtc_state),
2608 						       crtc_state->output_format,
2609 						       limits->pipe.max_bpp,
2610 						       max_link_bpp_x16);
2611 	}
2612 
2613 	limits->link.max_bpp_x16 = max_link_bpp_x16;
2614 
2615 	if (!intel_dp_get_connector_max_link_config(connector, limits, &max_link_config))
2616 		return false;
2617 
2618 	drm_dbg_kms(display->drm,
2619 		    "[ENCODER:%d:%s][CRTC:%d:%s] DP link limits: pixel clock %d kHz DSC %s max link %dx%d max pipe_bpp %d min link_bpp " FXP_Q4_FMT " max link_bpp " FXP_Q4_FMT "\n",
2620 		    encoder->base.base.id, encoder->base.name,
2621 		    crtc->base.base.id, crtc->base.name,
2622 		    adjusted_mode->crtc_clock,
2623 		    str_on_off(dsc),
2624 		    max_link_config.lane_count,
2625 		    max_link_config.rate,
2626 		    limits->pipe.max_bpp,
2627 		    FXP_Q4_ARGS(limits->link.min_bpp_x16),
2628 		    FXP_Q4_ARGS(limits->link.max_bpp_x16));
2629 
2630 	if (limits->link.min_bpp_x16 <= 0 ||
2631 	    limits->link.min_bpp_x16 > limits->link.max_bpp_x16)
2632 		return false;
2633 
2634 	return true;
2635 }
2636 
2637 static bool
2638 intel_dp_dsc_compute_pipe_bpp_limits(struct intel_connector *connector,
2639 				     struct link_config_limits *limits)
2640 {
2641 	struct intel_display *display = to_intel_display(connector);
2642 	const struct link_config_limits orig_limits = *limits;
2643 	int dsc_min_bpc = intel_dp_dsc_min_src_input_bpc();
2644 	int dsc_max_bpc = intel_dp_dsc_max_src_input_bpc(display);
2645 
2646 	limits->pipe.min_bpp = max(limits->pipe.min_bpp, dsc_min_bpc * 3);
2647 	limits->pipe.min_bpp = align_min_sink_dsc_input_bpp(connector, limits->pipe.min_bpp);
2648 
2649 	limits->pipe.max_bpp = min(limits->pipe.max_bpp, dsc_max_bpc * 3);
2650 	limits->pipe.max_bpp = align_max_sink_dsc_input_bpp(connector, limits->pipe.max_bpp);
2651 
2652 	if (limits->pipe.min_bpp <= 0 ||
2653 	    limits->pipe.min_bpp > limits->pipe.max_bpp) {
2654 		drm_dbg_kms(display->drm,
2655 			    "[CONNECTOR:%d:%s] Invalid DSC src/sink input BPP (src:%d-%d pipe:%d-%d sink-align:%d-%d)\n",
2656 			    connector->base.base.id, connector->base.name,
2657 			    dsc_min_bpc * 3, dsc_max_bpc * 3,
2658 			    orig_limits.pipe.min_bpp, orig_limits.pipe.max_bpp,
2659 			    limits->pipe.min_bpp, limits->pipe.max_bpp);
2660 
2661 		return false;
2662 	}
2663 
2664 	return true;
2665 }
2666 
2667 bool
2668 intel_dp_compute_config_limits(struct intel_dp *intel_dp,
2669 			       struct drm_connector_state *conn_state,
2670 			       struct intel_crtc_state *crtc_state,
2671 			       bool respect_downstream_limits,
2672 			       bool dsc,
2673 			       struct link_config_limits *limits)
2674 {
2675 	struct intel_display *display = to_intel_display(intel_dp);
2676 	struct intel_dp_link_caps *link_caps = intel_dp->link.caps;
2677 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
2678 	struct intel_connector *connector =
2679 		to_intel_connector(conn_state->connector);
2680 
2681 	limits->link_config_filter = INTEL_DP_LINK_CAPS_FILTER_ALL;
2682 
2683 	limits->pipe.min_bpp = intel_dp_min_bpp(crtc_state->output_format);
2684 	if (is_mst) {
2685 		/*
2686 		 * FIXME: If all the streams can't fit into the link with their
2687 		 * current pipe_bpp we should reduce pipe_bpp across the board
2688 		 * until things start to fit. Until then we limit to <= 8bpc
2689 		 * since that's what was hardcoded for all MST streams
2690 		 * previously. This hack should be removed once we have the
2691 		 * proper retry logic in place.
2692 		 */
2693 		limits->pipe.max_bpp = min(crtc_state->max_pipe_bpp, 24);
2694 	} else {
2695 		limits->pipe.max_bpp = intel_dp_max_bpp(intel_dp, crtc_state,
2696 							respect_downstream_limits);
2697 	}
2698 
2699 	if (!dsc && intel_dp_in_hdr_mode(conn_state)) {
2700 		if (intel_dp_supports_dsc(intel_dp, connector, crtc_state) &&
2701 		    limits->pipe.max_bpp >= 30)
2702 			limits->pipe.min_bpp = max(limits->pipe.min_bpp, 30);
2703 		else
2704 			drm_dbg_kms(display->drm,
2705 				    "[CONNECTOR:%d:%s] Can't force 30 bpp for HDR (pipe bpp: %d-%d DSC-support: %s)\n",
2706 				    connector->base.base.id, connector->base.name,
2707 				    limits->pipe.min_bpp, limits->pipe.max_bpp,
2708 				    str_yes_no(intel_dp_supports_dsc(intel_dp, connector,
2709 								     crtc_state)));
2710 	}
2711 
2712 	/*
2713 	 * HDMI knows no 6 bpc transport format, and DSC with an at least
2714 	 * 8 bpc input provides a better output quality than a dithered
2715 	 * 6 bpc output. Prefer it for HDMI sinks, by failing the
2716 	 * uncompressed link config for modes which would fit only with a
2717 	 * 6 bpc pipe BPP. Honor a lower limit set via the max bpc
2718 	 * connector property.
2719 	 */
2720 	if (!dsc && intel_dp_has_hdmi_sink(intel_dp) &&
2721 	    intel_dp_supports_dsc(intel_dp, connector, crtc_state) &&
2722 	    limits->pipe.max_bpp >= 24 &&
2723 	    crtc_state->pipe_bpp >= 24)
2724 		limits->pipe.min_bpp = max(limits->pipe.min_bpp, 24);
2725 
2726 	if (limits->pipe.min_bpp <= 0 ||
2727 	    limits->pipe.min_bpp > limits->pipe.max_bpp) {
2728 		drm_dbg_kms(display->drm, "[CONNECTOR:%d:%s] Invalid pipe bpp range: %d-%d\n",
2729 			    connector->base.base.id, connector->base.name,
2730 			    limits->pipe.min_bpp, limits->pipe.max_bpp);
2731 
2732 		return false;
2733 	}
2734 
2735 	if (dsc && !intel_dp_dsc_compute_pipe_bpp_limits(connector, limits))
2736 		return false;
2737 
2738 	/*
2739 	 * crtc_state->pipe_bpp is the non-DP specific baseline (platform /
2740 	 * EDID) maximum pipe BPP limited by the max-BPC connector property
2741 	 * request. Since by now pipe.max_bpp is <= the above baseline
2742 	 * maximum BPP, the only remaining reason for adjusting pipe.max_bpp
2743 	 * is the max-BPC connector property request. Adjust pipe.max_bpp to
2744 	 * this request within the current valid pipe.min_bpp .. pipe.max_bpp
2745 	 * range.
2746 	 */
2747 	limits->pipe.max_bpp = clamp(crtc_state->pipe_bpp, limits->pipe.min_bpp,
2748 				     limits->pipe.max_bpp);
2749 	if (dsc)
2750 		limits->pipe.max_bpp = align_max_sink_dsc_input_bpp(connector,
2751 								    limits->pipe.max_bpp);
2752 
2753 	if (limits->pipe.max_bpp != crtc_state->pipe_bpp)
2754 		drm_dbg_kms(display->drm,
2755 			    "[CONNECTOR:%d:%s] Adjusting requested max pipe bpp %d -> %d\n",
2756 			    connector->base.base.id, connector->base.name,
2757 			    crtc_state->pipe_bpp, limits->pipe.max_bpp);
2758 
2759 	if (is_mst || intel_dp->use_max_params) {
2760 		struct intel_dp_link_caps_filter new_filter = INTEL_DP_LINK_CAPS_FILTER_NONE;
2761 		struct intel_dp_link_config max_config;
2762 
2763 		/*
2764 		 * For MST we always configure max link bw - the spec doesn't
2765 		 * seem to suggest we should do otherwise.
2766 		 *
2767 		 * Use the maximum clock and number of lanes the eDP panel
2768 		 * advertizes being capable of in case the initial fast
2769 		 * optimal params failed us. The panels are generally
2770 		 * designed to support only a single clock and lane
2771 		 * configuration, and typically on older panels these
2772 		 * values correspond to the native resolution of the panel.
2773 		 */
2774 		if (!intel_dp_get_connector_max_link_config(connector, limits, &max_config))
2775 			return false;
2776 
2777 		if (!intel_dp_link_caps_filter_add(link_caps, &new_filter, &max_config))
2778 			return false;
2779 
2780 		limits->link_config_filter = new_filter;
2781 	}
2782 
2783 	if (!intel_dp_test_compute_config(connector, crtc_state, limits))
2784 		return false;
2785 
2786 	return intel_dp_compute_config_link_bpp_limits(connector,
2787 						       crtc_state,
2788 						       dsc,
2789 						       limits);
2790 }
2791 
2792 int intel_dp_config_required_rate(const struct intel_crtc_state *crtc_state)
2793 {
2794 	const struct drm_display_mode *adjusted_mode =
2795 		&crtc_state->hw.adjusted_mode;
2796 	int link_bpp_x16 = crtc_state->dsc.compression_enable ?
2797 		crtc_state->dsc.compressed_bpp_x16 :
2798 		fxp_q4_from_int(crtc_state->pipe_bpp);
2799 
2800 	return intel_dp_link_required(crtc_state->port_clock, crtc_state->lane_count,
2801 				      adjusted_mode->crtc_clock, adjusted_mode->hdisplay,
2802 				      link_bpp_x16, 0);
2803 }
2804 
2805 bool intel_dp_joiner_needs_dsc(struct intel_display *display,
2806 			       int num_joined_pipes)
2807 {
2808 	/*
2809 	 * Pipe joiner needs compression up to display 12 due to bandwidth
2810 	 * limitation. DG2 onwards pipe joiner can be enabled without
2811 	 * compression.
2812 	 * Ultrajoiner always needs compression.
2813 	 */
2814 	return (!HAS_UNCOMPRESSED_JOINER(display) && num_joined_pipes == 2) ||
2815 		num_joined_pipes == 4;
2816 }
2817 
2818 static int
2819 intel_dp_compute_link_for_joined_pipes(struct intel_encoder *encoder,
2820 				       struct intel_crtc_state *pipe_config,
2821 				       struct drm_connector_state *conn_state,
2822 				       bool respect_downstream_limits)
2823 {
2824 	struct intel_display *display = to_intel_display(encoder);
2825 	int num_joined_pipes = intel_crtc_num_joined_pipes(pipe_config);
2826 	struct intel_connector *connector =
2827 		to_intel_connector(conn_state->connector);
2828 	const struct drm_display_mode *adjusted_mode =
2829 		&pipe_config->hw.adjusted_mode;
2830 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2831 	struct link_config_limits limits;
2832 	bool dsc_needed, joiner_needs_dsc;
2833 	int ret = 0;
2834 
2835 	intel_dp_dsc_reset_config(pipe_config);
2836 
2837 	joiner_needs_dsc = intel_dp_joiner_needs_dsc(display, num_joined_pipes);
2838 
2839 	dsc_needed = joiner_needs_dsc || intel_dp->force_dsc_en ||
2840 		     !intel_dp_compute_config_limits(intel_dp, conn_state, pipe_config,
2841 						     respect_downstream_limits,
2842 						     false,
2843 						     &limits);
2844 
2845 	if (!dsc_needed) {
2846 		/*
2847 		 * Optimize for slow and wide for everything, because there are some
2848 		 * eDP 1.3 and 1.4 panels don't work well with fast and narrow.
2849 		 */
2850 		ret = intel_dp_compute_link_config_wide(intel_dp, pipe_config,
2851 							conn_state, &limits);
2852 		if (!ret && intel_dp_is_uhbr(pipe_config))
2853 			ret = intel_dp_mtp_tu_compute_config(intel_dp,
2854 							     pipe_config,
2855 							     conn_state,
2856 							     fxp_q4_from_int(pipe_config->pipe_bpp),
2857 							     fxp_q4_from_int(pipe_config->pipe_bpp),
2858 							     0, false);
2859 
2860 		if (ret ||
2861 		    !intel_dp_dotclk_valid(display,
2862 					   adjusted_mode->crtc_clock,
2863 					   adjusted_mode->crtc_htotal,
2864 					   0,
2865 					   num_joined_pipes))
2866 			dsc_needed = true;
2867 	}
2868 
2869 	if (dsc_needed && !intel_dp_supports_dsc(intel_dp, connector, pipe_config)) {
2870 		drm_dbg_kms(display->drm, "DSC required but not available\n");
2871 		return -EINVAL;
2872 	}
2873 
2874 	if (dsc_needed) {
2875 		int dsc_slice_count;
2876 
2877 		drm_dbg_kms(display->drm,
2878 			    "Try DSC (fallback=%s, joiner=%s, force=%s)\n",
2879 			    str_yes_no(ret), str_yes_no(joiner_needs_dsc),
2880 			    str_yes_no(intel_dp->force_dsc_en));
2881 
2882 		if (!intel_dp_compute_config_limits(intel_dp, conn_state, pipe_config,
2883 						    respect_downstream_limits,
2884 						    true,
2885 						    &limits))
2886 			return -EINVAL;
2887 
2888 		ret = intel_dp_dsc_compute_config(intel_dp, pipe_config,
2889 						  conn_state, &limits, 64);
2890 		if (ret < 0)
2891 			return ret;
2892 
2893 		dsc_slice_count = intel_dsc_line_slice_count(&pipe_config->dsc.slice_config);
2894 
2895 		if (!intel_dp_dotclk_valid(display,
2896 					   adjusted_mode->crtc_clock,
2897 					   adjusted_mode->crtc_htotal,
2898 					   dsc_slice_count,
2899 					   num_joined_pipes))
2900 			return -EINVAL;
2901 	}
2902 
2903 	drm_dbg_kms(display->drm,
2904 		    "DP lane count %d clock %d bpp input %d compressed " FXP_Q4_FMT " HDR %s link rate required %d available %d\n",
2905 		    pipe_config->lane_count, pipe_config->port_clock,
2906 		    pipe_config->pipe_bpp,
2907 		    FXP_Q4_ARGS(pipe_config->dsc.compressed_bpp_x16),
2908 		    str_yes_no(intel_dp_in_hdr_mode(conn_state)),
2909 		    intel_dp_config_required_rate(pipe_config),
2910 		    intel_dp_max_link_data_rate(intel_dp,
2911 						pipe_config->port_clock,
2912 						pipe_config->lane_count));
2913 
2914 	return 0;
2915 }
2916 
2917 static int
2918 intel_dp_compute_link_config(struct intel_encoder *encoder,
2919 			     struct intel_crtc_state *crtc_state,
2920 			     struct drm_connector_state *conn_state,
2921 			     bool respect_downstream_limits)
2922 {
2923 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2924 	struct intel_connector *connector =
2925 		to_intel_connector(conn_state->connector);
2926 	const struct drm_display_mode *adjusted_mode =
2927 		&crtc_state->hw.adjusted_mode;
2928 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2929 	int num_joined_pipes;
2930 	int ret = -EINVAL;
2931 
2932 	if (crtc_state->fec_enable &&
2933 	    !intel_dp_supports_fec(intel_dp, connector, crtc_state))
2934 		return -EINVAL;
2935 
2936 	for_each_joiner_candidate(connector, adjusted_mode, num_joined_pipes) {
2937 		/*
2938 		 * NOTE:
2939 		 * The crtc_state->joiner_pipes should have been set at the end
2940 		 * only if all the conditions are met. However that would mean
2941 		 * that num_joined_pipes is passed around to all helpers and
2942 		 * make them use it instead of using crtc_state->joiner_pipes
2943 		 * directly or indirectly (via intel_crtc_num_joined_pipes()).
2944 		 *
2945 		 * For now, setting crtc_state->joiner_pipes to the candidate
2946 		 * value to avoid the above churn and resetting it to 0, in case
2947 		 * no joiner candidate is found to be suitable for the given
2948 		 * configuration.
2949 		 */
2950 		if (num_joined_pipes > 1)
2951 			crtc_state->joiner_pipes = GENMASK(crtc->pipe + num_joined_pipes - 1,
2952 							   crtc->pipe);
2953 
2954 		ret = intel_dp_compute_link_for_joined_pipes(encoder, crtc_state, conn_state,
2955 							     respect_downstream_limits);
2956 		if (ret == 0 || ret == -EDEADLK)
2957 			break;
2958 	}
2959 
2960 	if (ret < 0)
2961 		crtc_state->joiner_pipes = 0;
2962 
2963 	return ret;
2964 }
2965 
2966 bool intel_dp_limited_color_range(const struct intel_crtc_state *crtc_state,
2967 				  const struct drm_connector_state *conn_state)
2968 {
2969 	const struct intel_digital_connector_state *intel_conn_state =
2970 		to_intel_digital_connector_state(conn_state);
2971 	const struct drm_display_mode *adjusted_mode =
2972 		&crtc_state->hw.adjusted_mode;
2973 
2974 	/*
2975 	 * Our YCbCr output is always limited range.
2976 	 * crtc_state->limited_color_range only applies to RGB,
2977 	 * and it must never be set for YCbCr or we risk setting
2978 	 * some conflicting bits in TRANSCONF which will mess up
2979 	 * the colors on the monitor.
2980 	 */
2981 	if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
2982 		return false;
2983 
2984 	if (intel_conn_state->broadcast_rgb == INTEL_BROADCAST_RGB_AUTO) {
2985 		/*
2986 		 * See:
2987 		 * CEA-861-E - 5.1 Default Encoding Parameters
2988 		 * VESA DisplayPort Ver.1.2a - 5.1.1.1 Video Colorimetry
2989 		 */
2990 		return crtc_state->pipe_bpp != 18 &&
2991 			drm_default_rgb_quant_range(adjusted_mode) ==
2992 			HDMI_QUANTIZATION_RANGE_LIMITED;
2993 	} else {
2994 		return intel_conn_state->broadcast_rgb ==
2995 			INTEL_BROADCAST_RGB_LIMITED;
2996 	}
2997 }
2998 
2999 static bool intel_dp_port_has_audio(struct intel_display *display, enum port port)
3000 {
3001 	if (display->platform.g4x)
3002 		return false;
3003 	if (DISPLAY_VER(display) < 12 && port == PORT_A)
3004 		return false;
3005 
3006 	return true;
3007 }
3008 
3009 static void intel_dp_compute_vsc_colorimetry(const struct intel_crtc_state *crtc_state,
3010 					     const struct drm_connector_state *conn_state,
3011 					     struct drm_dp_vsc_sdp *vsc)
3012 {
3013 	struct intel_display *display = to_intel_display(crtc_state);
3014 
3015 	if (crtc_state->has_panel_replay) {
3016 		/*
3017 		 * Prepare VSC Header for SU as per DP 2.0 spec, Table 2-223
3018 		 * VSC SDP supporting 3D stereo, Panel Replay, and Pixel
3019 		 * Encoding/Colorimetry Format indication.
3020 		 */
3021 		vsc->revision = 0x7;
3022 	} else {
3023 		/*
3024 		 * Prepare VSC Header for SU as per DP 1.4 spec, Table 2-118
3025 		 * VSC SDP supporting 3D stereo, PSR2, and Pixel Encoding/
3026 		 * Colorimetry Format indication.
3027 		 */
3028 		vsc->revision = 0x5;
3029 	}
3030 
3031 	vsc->length = 0x13;
3032 
3033 	/* DP 1.4a spec, Table 2-120 */
3034 	switch (crtc_state->output_format) {
3035 	case INTEL_OUTPUT_FORMAT_YCBCR444:
3036 		vsc->pixelformat = DP_PIXELFORMAT_YUV444;
3037 		break;
3038 	case INTEL_OUTPUT_FORMAT_YCBCR420:
3039 		vsc->pixelformat = DP_PIXELFORMAT_YUV420;
3040 		break;
3041 	case INTEL_OUTPUT_FORMAT_RGB:
3042 	default:
3043 		vsc->pixelformat = DP_PIXELFORMAT_RGB;
3044 	}
3045 
3046 	switch (conn_state->colorspace) {
3047 	case DRM_MODE_COLORIMETRY_BT709_YCC:
3048 		vsc->colorimetry = DP_COLORIMETRY_BT709_YCC;
3049 		break;
3050 	case DRM_MODE_COLORIMETRY_XVYCC_601:
3051 		vsc->colorimetry = DP_COLORIMETRY_XVYCC_601;
3052 		break;
3053 	case DRM_MODE_COLORIMETRY_XVYCC_709:
3054 		vsc->colorimetry = DP_COLORIMETRY_XVYCC_709;
3055 		break;
3056 	case DRM_MODE_COLORIMETRY_SYCC_601:
3057 		vsc->colorimetry = DP_COLORIMETRY_SYCC_601;
3058 		break;
3059 	case DRM_MODE_COLORIMETRY_OPYCC_601:
3060 		vsc->colorimetry = DP_COLORIMETRY_OPYCC_601;
3061 		break;
3062 	case DRM_MODE_COLORIMETRY_BT2020_CYCC:
3063 		vsc->colorimetry = DP_COLORIMETRY_BT2020_CYCC;
3064 		break;
3065 	case DRM_MODE_COLORIMETRY_BT2020_RGB:
3066 		vsc->colorimetry = DP_COLORIMETRY_BT2020_RGB;
3067 		break;
3068 	case DRM_MODE_COLORIMETRY_BT2020_YCC:
3069 		vsc->colorimetry = DP_COLORIMETRY_BT2020_YCC;
3070 		break;
3071 	case DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65:
3072 	case DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER:
3073 		vsc->colorimetry = DP_COLORIMETRY_DCI_P3_RGB;
3074 		break;
3075 	default:
3076 		/*
3077 		 * RGB->YCBCR color conversion uses the BT.709
3078 		 * color space.
3079 		 */
3080 		if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
3081 			vsc->colorimetry = DP_COLORIMETRY_BT709_YCC;
3082 		else
3083 			vsc->colorimetry = DP_COLORIMETRY_DEFAULT;
3084 		break;
3085 	}
3086 
3087 	vsc->bpc = crtc_state->pipe_bpp / 3;
3088 
3089 	/* only RGB pixelformat supports 6 bpc */
3090 	drm_WARN_ON(display->drm,
3091 		    vsc->bpc == 6 && vsc->pixelformat != DP_PIXELFORMAT_RGB);
3092 
3093 	/* All YCbCr formats are always limited range. */
3094 	if (vsc->pixelformat == DP_PIXELFORMAT_RGB)
3095 		vsc->dynamic_range = crtc_state->limited_color_range ?
3096 			DP_DYNAMIC_RANGE_CTA : DP_DYNAMIC_RANGE_VESA;
3097 	else
3098 		vsc->dynamic_range = DP_DYNAMIC_RANGE_CTA;
3099 
3100 	vsc->content_type = DP_CONTENT_TYPE_NOT_DEFINED;
3101 }
3102 
3103 static bool intel_dp_needs_as_sdp(struct intel_dp *intel_dp,
3104 				  struct intel_crtc_state *crtc_state)
3105 {
3106 	if (!intel_dp->as_sdp_v2_supported)
3107 		return false;
3108 
3109 	/*
3110 	 * #TODO: Add AS SDP v1 support for PCONs (DP branch devices).
3111 	 */
3112 	if (drm_dp_is_branch(intel_dp->dpcd))
3113 		return false;
3114 
3115 	if (intel_psr_needs_alpm_aux_less(intel_dp, crtc_state) &&
3116 	    !intel_psr_pr_async_video_timing_supported(intel_dp))
3117 		return true;
3118 
3119 	return intel_vrr_possible(crtc_state);
3120 }
3121 
3122 static void intel_dp_compute_as_sdp(struct intel_dp *intel_dp,
3123 				    struct intel_crtc_state *crtc_state)
3124 {
3125 	struct drm_dp_as_sdp *as_sdp = &crtc_state->infoframes.as_sdp;
3126 	const struct drm_display_mode *adjusted_mode =
3127 		&crtc_state->hw.adjusted_mode;
3128 
3129 	/*
3130 	 * #FIXME: SDP/infoframe updates aren’t truly atomic, and with the new
3131 	 * cdclk->tc clock crossing we may transiently send a corrupted packet
3132 	 * if the update lands mid‑transmission.
3133 	 */
3134 	if (!intel_dp_needs_as_sdp(intel_dp, crtc_state))
3135 		return;
3136 
3137 	crtc_state->infoframes.enable |= intel_hdmi_infoframe_enable(DP_SDP_ADAPTIVE_SYNC);
3138 
3139 	as_sdp->sdp_type = DP_SDP_ADAPTIVE_SYNC;
3140 	as_sdp->length = 0x9;
3141 	as_sdp->duration_incr_ms = 0;
3142 	as_sdp->revision = 0x2;
3143 	as_sdp->vtotal = intel_vrr_vmin_vtotal(crtc_state);
3144 
3145 	if (crtc_state->cmrr.enable) {
3146 		as_sdp->mode = DP_AS_SDP_FAVT_TRR_REACHED;
3147 		as_sdp->target_rr = drm_mode_vrefresh(adjusted_mode);
3148 		as_sdp->target_rr_divider = true;
3149 	} else if (crtc_state->vrr.enable) {
3150 		as_sdp->mode = DP_AS_SDP_AVT_DYNAMIC_VTOTAL;
3151 	} else {
3152 		as_sdp->mode = DP_AS_SDP_AVT_FIXED_VTOTAL;
3153 	}
3154 
3155 	/*
3156 	 * For Panel Replay with Async Video Timing support, the source can
3157 	 * disable sending the AS SDP during PR Active state. In that case,
3158 	 * the sink needs the coasting vtotal value to maintain the refresh
3159 	 * rate. The HW only samples this on PR_ALPM_CTL[AS SDP Transmission
3160 	 * in Active Disable], which we never program, so providing the value
3161 	 * unconditionally when the sink advertises the capability is safe.
3162 	 *
3163 	 * #TODO:
3164 	 * If we ever advertise support for coasting at other refresh targets,
3165 	 * this logic could be revisited. For now, use the minimum refresh rate
3166 	 * as the only safe coasting value.
3167 	 */
3168 	if (intel_psr_pr_async_video_timing_supported(intel_dp))
3169 		as_sdp->coasting_vtotal = crtc_state->vrr.vmax;
3170 }
3171 
3172 static void intel_dp_compute_vsc_sdp(struct intel_dp *intel_dp,
3173 				     struct intel_crtc_state *crtc_state,
3174 				     const struct drm_connector_state *conn_state)
3175 {
3176 	struct drm_dp_vsc_sdp *vsc;
3177 
3178 	if (!intel_dp_needs_vsc_colorimetry(crtc_state, conn_state) &&
3179 	    !crtc_state->has_psr)
3180 		return;
3181 
3182 	vsc = &crtc_state->infoframes.vsc;
3183 
3184 	crtc_state->infoframes.enable |= intel_hdmi_infoframe_enable(DP_SDP_VSC);
3185 	vsc->sdp_type = DP_SDP_VSC;
3186 
3187 	if (intel_dp_needs_vsc_colorimetry(crtc_state, conn_state)) {
3188 		intel_dp_compute_vsc_colorimetry(crtc_state, conn_state,
3189 						 vsc);
3190 	} else if (crtc_state->has_panel_replay) {
3191 		/*
3192 		 * [Panel Replay without colorimetry info]
3193 		 * Prepare VSC Header for SU as per DP 2.0 spec, Table 2-223
3194 		 * VSC SDP supporting 3D stereo + Panel Replay.
3195 		 */
3196 		vsc->revision = 0x6;
3197 		vsc->length = 0x10;
3198 	} else if (crtc_state->has_sel_update) {
3199 		/*
3200 		 * [PSR2 without colorimetry]
3201 		 * Prepare VSC Header for SU as per eDP 1.4 spec, Table 6-11
3202 		 * 3D stereo + PSR/PSR2 + Y-coordinate.
3203 		 */
3204 		vsc->revision = 0x4;
3205 		vsc->length = 0xe;
3206 	} else {
3207 		/*
3208 		 * [PSR1]
3209 		 * Prepare VSC Header for SU as per DP 1.4 spec, Table 2-118
3210 		 * VSC SDP supporting 3D stereo + PSR (applies to eDP v1.3 or
3211 		 * higher).
3212 		 */
3213 		vsc->revision = 0x2;
3214 		vsc->length = 0x8;
3215 	}
3216 }
3217 
3218 bool
3219 intel_dp_in_hdr_mode(const struct drm_connector_state *conn_state)
3220 {
3221 	struct hdr_output_metadata *hdr_metadata;
3222 
3223 	if (!conn_state->hdr_output_metadata)
3224 		return false;
3225 
3226 	hdr_metadata = conn_state->hdr_output_metadata->data;
3227 
3228 	return hdr_metadata->hdmi_metadata_type1.eotf == HDMI_EOTF_SMPTE_ST2084;
3229 }
3230 
3231 static void
3232 intel_dp_compute_hdr_metadata_infoframe_sdp(struct intel_dp *intel_dp,
3233 					    struct intel_crtc_state *crtc_state,
3234 					    const struct drm_connector_state *conn_state)
3235 {
3236 	struct intel_display *display = to_intel_display(intel_dp);
3237 	int ret;
3238 	struct hdmi_drm_infoframe *drm_infoframe = &crtc_state->infoframes.drm.drm;
3239 
3240 	if (!conn_state->hdr_output_metadata)
3241 		return;
3242 
3243 	ret = drm_hdmi_infoframe_set_hdr_metadata(drm_infoframe, conn_state);
3244 
3245 	if (ret) {
3246 		drm_dbg_kms(display->drm,
3247 			    "couldn't set HDR metadata in infoframe\n");
3248 		return;
3249 	}
3250 
3251 	crtc_state->infoframes.enable |=
3252 		intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GAMUT_METADATA);
3253 }
3254 
3255 static bool can_enable_drrs(struct intel_connector *connector,
3256 			    const struct intel_crtc_state *pipe_config,
3257 			    const struct drm_display_mode *downclock_mode)
3258 {
3259 	struct intel_display *display = to_intel_display(connector);
3260 
3261 	if (pipe_config->vrr.enable)
3262 		return false;
3263 
3264 	/*
3265 	 * DRRS and PSR can't be enable together, so giving preference to PSR
3266 	 * as it allows more power-savings by complete shutting down display,
3267 	 * so to guarantee this, intel_drrs_compute_config() must be called
3268 	 * after intel_psr_compute_config().
3269 	 */
3270 	if (pipe_config->has_psr)
3271 		return false;
3272 
3273 	/* FIXME missing FDI M2/N2 etc. */
3274 	if (pipe_config->has_pch_encoder)
3275 		return false;
3276 
3277 	if (!intel_cpu_transcoder_has_drrs(display, pipe_config->cpu_transcoder))
3278 		return false;
3279 
3280 	return downclock_mode &&
3281 		intel_panel_drrs_type(connector) == DRRS_TYPE_SEAMLESS;
3282 }
3283 
3284 static void
3285 intel_dp_drrs_compute_config(struct intel_connector *connector,
3286 			     struct intel_crtc_state *pipe_config,
3287 			     int link_bpp_x16)
3288 {
3289 	struct intel_display *display = to_intel_display(connector);
3290 	const struct drm_display_mode *downclock_mode =
3291 		intel_panel_downclock_mode(connector, &pipe_config->hw.adjusted_mode);
3292 	int pixel_clock;
3293 
3294 	/*
3295 	 * FIXME all joined pipes share the same transcoder.
3296 	 * Need to account for that when updating M/N live.
3297 	 */
3298 	if (has_seamless_m_n(connector) && !pipe_config->joiner_pipes)
3299 		pipe_config->update_m_n = true;
3300 
3301 	if (!can_enable_drrs(connector, pipe_config, downclock_mode)) {
3302 		if (intel_cpu_transcoder_has_m2_n2(display, pipe_config->cpu_transcoder))
3303 			intel_zero_m_n(&pipe_config->dp_m2_n2);
3304 		return;
3305 	}
3306 
3307 	if (display->platform.ironlake || display->platform.sandybridge ||
3308 	    display->platform.ivybridge)
3309 		pipe_config->msa_timing_delay = connector->panel.vbt.edp.drrs_msa_timing_delay;
3310 
3311 	pipe_config->has_drrs = true;
3312 
3313 	pixel_clock = downclock_mode->clock;
3314 	if (pipe_config->splitter.enable)
3315 		pixel_clock /= pipe_config->splitter.link_count;
3316 
3317 	intel_link_compute_m_n(link_bpp_x16, pipe_config->lane_count, pixel_clock,
3318 			       pipe_config->port_clock,
3319 			       intel_dp_bw_fec_overhead(pipe_config->fec_enable),
3320 			       &pipe_config->dp_m2_n2);
3321 
3322 	/* FIXME: abstract this better */
3323 	if (pipe_config->splitter.enable)
3324 		pipe_config->dp_m2_n2.data_m *= pipe_config->splitter.link_count;
3325 }
3326 
3327 static bool intel_dp_has_audio(struct intel_encoder *encoder,
3328 			       const struct drm_connector_state *conn_state)
3329 {
3330 	struct intel_display *display = to_intel_display(encoder);
3331 	const struct intel_digital_connector_state *intel_conn_state =
3332 		to_intel_digital_connector_state(conn_state);
3333 	struct intel_connector *connector =
3334 		to_intel_connector(conn_state->connector);
3335 
3336 	if (!intel_dp_port_has_audio(display, encoder->port))
3337 		return false;
3338 
3339 	if (intel_conn_state->force_audio == HDMI_AUDIO_AUTO)
3340 		return connector->base.display_info.has_audio;
3341 	else
3342 		return intel_conn_state->force_audio == HDMI_AUDIO_ON;
3343 }
3344 
3345 static int
3346 intel_dp_compute_output_format(struct intel_encoder *encoder,
3347 			       struct intel_crtc_state *crtc_state,
3348 			       struct drm_connector_state *conn_state,
3349 			       bool respect_downstream_limits,
3350 			       enum intel_output_format sink_format)
3351 {
3352 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3353 	struct intel_connector *connector = intel_dp->attached_connector;
3354 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
3355 
3356 	if (intel_dp_sink_format_valid(connector, adjusted_mode,
3357 				       sink_format) != MODE_OK)
3358 		return -EINVAL;
3359 
3360 	crtc_state->sink_format = sink_format;
3361 	crtc_state->output_format = intel_dp_output_format(connector, crtc_state->sink_format);
3362 
3363 	return intel_dp_compute_link_config(encoder, crtc_state, conn_state,
3364 					    respect_downstream_limits);
3365 }
3366 
3367 static int
3368 intel_dp_compute_formats(struct intel_encoder *encoder,
3369 			 struct intel_crtc_state *crtc_state,
3370 			 struct drm_connector_state *conn_state,
3371 			 bool respect_downstream_limits)
3372 {
3373 	struct intel_display *display = to_intel_display(encoder);
3374 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3375 	struct intel_connector *connector = intel_dp->attached_connector;
3376 	const struct drm_display_info *info = &connector->base.display_info;
3377 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
3378 	int ret;
3379 
3380 	if (drm_mode_is_420_only(info, adjusted_mode)) {
3381 		ret = intel_dp_compute_output_format(encoder, crtc_state, conn_state,
3382 						     respect_downstream_limits,
3383 						     INTEL_OUTPUT_FORMAT_YCBCR420);
3384 
3385 		if (ret && !respect_downstream_limits) {
3386 			drm_dbg_kms(display->drm,
3387 				    "YCbCr 4:2:0 mode but YCbCr 4:2:0 output not possible. Falling back to RGB.\n");
3388 
3389 			ret = intel_dp_compute_output_format(encoder, crtc_state, conn_state,
3390 							     respect_downstream_limits,
3391 							     INTEL_OUTPUT_FORMAT_RGB);
3392 		}
3393 	} else {
3394 		ret = intel_dp_compute_output_format(encoder, crtc_state, conn_state,
3395 						     respect_downstream_limits,
3396 						     INTEL_OUTPUT_FORMAT_RGB);
3397 
3398 		if (ret && drm_mode_is_420_also(info, adjusted_mode))
3399 			ret = intel_dp_compute_output_format(encoder, crtc_state, conn_state,
3400 							     respect_downstream_limits,
3401 							     INTEL_OUTPUT_FORMAT_YCBCR420);
3402 	}
3403 
3404 	return ret;
3405 }
3406 
3407 void
3408 intel_dp_audio_compute_config(struct intel_encoder *encoder,
3409 			      struct intel_crtc_state *pipe_config,
3410 			      struct drm_connector_state *conn_state)
3411 {
3412 	pipe_config->has_audio =
3413 		intel_dp_has_audio(encoder, conn_state) &&
3414 		intel_audio_compute_config(encoder, pipe_config, conn_state);
3415 
3416 	pipe_config->sdp_split_enable = pipe_config->has_audio &&
3417 					intel_dp_is_uhbr(pipe_config);
3418 }
3419 
3420 void
3421 intel_dp_queue_modeset_retry_for_link(struct intel_atomic_state *state,
3422 				      struct intel_encoder *encoder,
3423 				      const struct intel_crtc_state *crtc_state)
3424 {
3425 	struct intel_connector *connector;
3426 	struct intel_digital_connector_state *conn_state;
3427 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3428 	int i;
3429 
3430 	if (intel_dp->needs_modeset_retry)
3431 		return;
3432 
3433 	intel_dp->needs_modeset_retry = true;
3434 
3435 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
3436 		intel_connector_queue_modeset_retry_work(intel_dp->attached_connector);
3437 
3438 		return;
3439 	}
3440 
3441 	for_each_new_intel_connector_in_state(state, connector, conn_state, i) {
3442 		if (!conn_state->base.crtc)
3443 			continue;
3444 
3445 		if (connector->mst.dp == intel_dp)
3446 			intel_connector_queue_modeset_retry_work(connector);
3447 	}
3448 }
3449 
3450 int intel_dp_compute_min_hblank(struct intel_crtc_state *crtc_state,
3451 				const struct drm_connector_state *conn_state)
3452 {
3453 	struct intel_display *display = to_intel_display(crtc_state);
3454 	const struct drm_display_mode *adjusted_mode =
3455 					&crtc_state->hw.adjusted_mode;
3456 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
3457 	int symbol_size = intel_dp_is_uhbr(crtc_state) ? 32 : 8;
3458 	/*
3459 	 * min symbol cycles is 3(BS,VBID, BE) for 128b/132b and
3460 	 * 5(BS, VBID, MVID, MAUD, BE) for 8b/10b
3461 	 */
3462 	int min_sym_cycles = intel_dp_is_uhbr(crtc_state) ? 3 : 5;
3463 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
3464 	int num_joined_pipes = intel_crtc_num_joined_pipes(crtc_state);
3465 	int min_hblank;
3466 	int max_lane_count = 4;
3467 	int hactive_sym_cycles, htotal_sym_cycles;
3468 	int dsc_slices = 0;
3469 	int link_bpp_x16;
3470 
3471 	if (DISPLAY_VER(display) < 30)
3472 		return 0;
3473 
3474 	/* MIN_HBLANK should be set only for 8b/10b MST or for 128b/132b SST/MST */
3475 	if (!is_mst && !intel_dp_is_uhbr(crtc_state))
3476 		return 0;
3477 
3478 	if (crtc_state->dsc.compression_enable) {
3479 		dsc_slices = intel_dp_dsc_get_slice_count(connector,
3480 							  adjusted_mode->crtc_clock,
3481 							  adjusted_mode->crtc_hdisplay,
3482 							  num_joined_pipes);
3483 		if (!dsc_slices) {
3484 			drm_dbg(display->drm, "failed to calculate dsc slice count\n");
3485 			return -EINVAL;
3486 		}
3487 	}
3488 
3489 	if (crtc_state->dsc.compression_enable)
3490 		link_bpp_x16 = crtc_state->dsc.compressed_bpp_x16;
3491 	else
3492 		link_bpp_x16 = intel_dp_output_format_link_bpp_x16(crtc_state->output_format,
3493 								   crtc_state->pipe_bpp);
3494 
3495 	/* Calculate min Hblank Link Layer Symbol Cycle Count for 8b/10b MST & 128b/132b */
3496 	hactive_sym_cycles = drm_dp_link_symbol_cycles(max_lane_count,
3497 						       adjusted_mode->hdisplay,
3498 						       dsc_slices,
3499 						       link_bpp_x16,
3500 						       symbol_size, is_mst);
3501 	htotal_sym_cycles = adjusted_mode->htotal * hactive_sym_cycles /
3502 			     adjusted_mode->hdisplay;
3503 
3504 	min_hblank = htotal_sym_cycles - hactive_sym_cycles;
3505 	/* minimum Hblank calculation: https://groups.vesa.org/wg/DP/document/20494 */
3506 	min_hblank = max(min_hblank, min_sym_cycles);
3507 
3508 	/*
3509 	 * adjust the BlankingStart/BlankingEnd framing control from
3510 	 * the calculated value
3511 	 */
3512 	min_hblank = min_hblank - 2;
3513 
3514 	/*
3515 	 * min_hblank formula is undergoing a change, to avoid underrun use the
3516 	 * recomended value in spec to compare with the calculated one and use the
3517 	 * minimum value
3518 	 */
3519 	if (intel_dp_is_uhbr(crtc_state)) {
3520 		/*
3521 		 * Note: Bspec requires a min_hblank of 2 for YCBCR420
3522 		 * with compressed bpp 6, but the minimum compressed bpp
3523 		 * supported by the driver is 8.
3524 		 */
3525 		drm_WARN_ON(display->drm,
3526 			    (crtc_state->dsc.compression_enable &&
3527 			     crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 &&
3528 			     crtc_state->dsc.compressed_bpp_x16 < fxp_q4_from_int(8)));
3529 		min_hblank = min(3, min_hblank);
3530 	} else {
3531 		min_hblank = min(10, min_hblank);
3532 	}
3533 
3534 	crtc_state->min_hblank = min_hblank;
3535 
3536 	return 0;
3537 }
3538 
3539 int
3540 intel_dp_compute_config(struct intel_atomic_state *state,
3541 			struct intel_encoder *encoder,
3542 			struct intel_crtc_state *pipe_config,
3543 			struct drm_connector_state *conn_state)
3544 {
3545 	struct intel_display *display = to_intel_display(encoder);
3546 	struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
3547 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3548 	struct intel_connector *connector = intel_dp->attached_connector;
3549 	int ret = 0, link_bpp_x16;
3550 
3551 	if (intel_dp_is_edp(intel_dp)) {
3552 		ret = intel_panel_compute_config(state, pipe_config, connector);
3553 		if (ret)
3554 			return ret;
3555 	}
3556 
3557 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
3558 		return -EINVAL;
3559 
3560 	if (!connector->base.interlace_allowed &&
3561 	    adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
3562 		return -EINVAL;
3563 
3564 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
3565 		return -EINVAL;
3566 
3567 	if (intel_dp_hdisplay_bad(display, adjusted_mode->crtc_hdisplay))
3568 		return -EINVAL;
3569 
3570 	/*
3571 	 * Try to respect downstream TMDS clock limits first, if
3572 	 * that fails assume the user might know something we don't.
3573 	 */
3574 	ret = intel_dp_compute_formats(encoder, pipe_config, conn_state, true);
3575 	if (ret)
3576 		ret = intel_dp_compute_formats(encoder, pipe_config, conn_state, false);
3577 	if (ret)
3578 		return ret;
3579 
3580 	ret = intel_pfit_compute_config(pipe_config, conn_state);
3581 	if (ret)
3582 		return ret;
3583 
3584 	pipe_config->limited_color_range =
3585 		intel_dp_limited_color_range(pipe_config, conn_state);
3586 
3587 	if (intel_dp_is_uhbr(pipe_config)) {
3588 		/* 128b/132b SST also needs this */
3589 		pipe_config->mst_master_transcoder = pipe_config->cpu_transcoder;
3590 	} else {
3591 		pipe_config->enhanced_framing =
3592 			drm_dp_enhanced_frame_cap(intel_dp->dpcd);
3593 	}
3594 
3595 	if (pipe_config->dsc.compression_enable)
3596 		link_bpp_x16 = pipe_config->dsc.compressed_bpp_x16;
3597 	else
3598 		link_bpp_x16 = intel_dp_output_format_link_bpp_x16(pipe_config->output_format,
3599 								   pipe_config->pipe_bpp);
3600 
3601 	if (intel_dp->mso_link_count) {
3602 		int n = intel_dp->mso_link_count;
3603 		int overlap = intel_dp->mso_pixel_overlap;
3604 
3605 		pipe_config->splitter.enable = true;
3606 		pipe_config->splitter.link_count = n;
3607 		pipe_config->splitter.pixel_overlap = overlap;
3608 
3609 		drm_dbg_kms(display->drm,
3610 			    "MSO link count %d, pixel overlap %d\n",
3611 			    n, overlap);
3612 
3613 		adjusted_mode->crtc_hdisplay = adjusted_mode->crtc_hdisplay / n + overlap;
3614 		adjusted_mode->crtc_hblank_start = adjusted_mode->crtc_hblank_start / n + overlap;
3615 		adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_hblank_end / n + overlap;
3616 		adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hsync_start / n + overlap;
3617 		adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_end / n + overlap;
3618 		adjusted_mode->crtc_htotal = adjusted_mode->crtc_htotal / n + overlap;
3619 		adjusted_mode->crtc_clock /= n;
3620 	}
3621 
3622 	intel_dp_audio_compute_config(encoder, pipe_config, conn_state);
3623 
3624 	if (!intel_dp_is_uhbr(pipe_config)) {
3625 		intel_link_compute_m_n(link_bpp_x16,
3626 				       pipe_config->lane_count,
3627 				       adjusted_mode->crtc_clock,
3628 				       pipe_config->port_clock,
3629 				       intel_dp_bw_fec_overhead(pipe_config->fec_enable),
3630 				       &pipe_config->dp_m_n);
3631 	}
3632 
3633 	ret = intel_dp_compute_min_hblank(pipe_config, conn_state);
3634 	if (ret)
3635 		return ret;
3636 
3637 	/* FIXME: abstract this better */
3638 	if (pipe_config->splitter.enable)
3639 		pipe_config->dp_m_n.data_m *= pipe_config->splitter.link_count;
3640 
3641 	intel_vrr_compute_config(pipe_config, conn_state);
3642 	intel_psr_compute_config(intel_dp, pipe_config, conn_state);
3643 	intel_dp_compute_as_sdp(intel_dp, pipe_config);
3644 	intel_alpm_lobf_compute_config(intel_dp, pipe_config, conn_state);
3645 	intel_dp_drrs_compute_config(connector, pipe_config, link_bpp_x16);
3646 	intel_dp_compute_vsc_sdp(intel_dp, pipe_config, conn_state);
3647 	intel_dp_compute_hdr_metadata_infoframe_sdp(intel_dp, pipe_config, conn_state);
3648 
3649 	return intel_dp_tunnel_atomic_compute_stream_bw(state, intel_dp, connector,
3650 							pipe_config);
3651 }
3652 
3653 void intel_dp_set_link_params(struct intel_dp *intel_dp,
3654 			      int link_rate, int lane_count)
3655 {
3656 	memset(intel_dp->train_set, 0, sizeof(intel_dp->train_set));
3657 	intel_dp->link.active = false;
3658 	intel_dp->needs_modeset_retry = false;
3659 	intel_dp->link_rate = link_rate;
3660 	intel_dp->lane_count = lane_count;
3661 }
3662 
3663 void intel_dp_reset_link_params(struct intel_dp *intel_dp)
3664 {
3665 	/*
3666 	 * TODO: Remove the following reset of link capabilities, as
3667 	 * this isn't needed after intel_dp_link_caps_update(reset=true)
3668 	 * was called.
3669 	 */
3670 	intel_dp_link_caps_reset(intel_dp->link.caps);
3671 	intel_dp_tunnel_uhbr_lanes_wa_apply(intel_dp);
3672 
3673 	intel_dp->link.mst_probed_lane_count = 0;
3674 	intel_dp->link.mst_probed_rate = 0;
3675 	intel_dp_link_training_reset(intel_dp->link.training);
3676 }
3677 
3678 /* Enable backlight PWM and backlight PP control. */
3679 void intel_edp_backlight_on(const struct intel_crtc_state *crtc_state,
3680 			    const struct drm_connector_state *conn_state)
3681 {
3682 	struct intel_display *display = to_intel_display(crtc_state);
3683 	struct intel_dp *intel_dp = enc_to_intel_dp(to_intel_encoder(conn_state->best_encoder));
3684 
3685 	if (!intel_dp_is_edp(intel_dp))
3686 		return;
3687 
3688 	drm_dbg_kms(display->drm, "\n");
3689 
3690 	intel_backlight_enable(crtc_state, conn_state);
3691 	intel_pps_backlight_on(intel_dp);
3692 }
3693 
3694 /* Disable backlight PP control and backlight PWM. */
3695 void intel_edp_backlight_off(const struct drm_connector_state *old_conn_state)
3696 {
3697 	struct intel_dp *intel_dp = enc_to_intel_dp(to_intel_encoder(old_conn_state->best_encoder));
3698 	struct intel_display *display = to_intel_display(intel_dp);
3699 
3700 	if (!intel_dp_is_edp(intel_dp))
3701 		return;
3702 
3703 	drm_dbg_kms(display->drm, "\n");
3704 
3705 	intel_pps_backlight_off(intel_dp);
3706 	intel_backlight_disable(old_conn_state);
3707 }
3708 
3709 static bool downstream_hpd_needs_d0(struct intel_dp *intel_dp)
3710 {
3711 	/*
3712 	 * DPCD 1.2+ should support BRANCH_DEVICE_CTRL, and thus
3713 	 * be capable of signalling downstream hpd with a long pulse.
3714 	 * Whether or not that means D3 is safe to use is not clear,
3715 	 * but let's assume so until proven otherwise.
3716 	 *
3717 	 * FIXME should really check all downstream ports...
3718 	 */
3719 	return intel_dp->dpcd[DP_DPCD_REV] == 0x11 &&
3720 		drm_dp_is_branch(intel_dp->dpcd) &&
3721 		intel_dp->downstream_ports[0] & DP_DS_PORT_HPD;
3722 }
3723 
3724 static int
3725 write_dsc_decompression_flag(struct drm_dp_aux *aux, u8 flag, bool set)
3726 {
3727 	int err;
3728 	u8 val;
3729 
3730 	err = drm_dp_dpcd_readb(aux, DP_DSC_ENABLE, &val);
3731 	if (err < 0)
3732 		return err;
3733 
3734 	if (set)
3735 		val |= flag;
3736 	else
3737 		val &= ~flag;
3738 
3739 	return drm_dp_dpcd_writeb(aux, DP_DSC_ENABLE, val);
3740 }
3741 
3742 static void
3743 intel_dp_sink_set_dsc_decompression(struct intel_connector *connector,
3744 				    bool enable)
3745 {
3746 	struct intel_display *display = to_intel_display(connector);
3747 
3748 	if (write_dsc_decompression_flag(connector->dp.dsc_decompression_aux,
3749 					 DP_DECOMPRESSION_EN, enable) < 0)
3750 		drm_dbg_kms(display->drm,
3751 			    "Failed to %s sink decompression state\n",
3752 			    str_enable_disable(enable));
3753 }
3754 
3755 static void
3756 intel_dp_sink_set_dsc_passthrough(const struct intel_connector *connector,
3757 				  bool enable)
3758 {
3759 	struct intel_display *display = to_intel_display(connector);
3760 	struct drm_dp_aux *aux = connector->mst.port ?
3761 				 connector->mst.port->passthrough_aux : NULL;
3762 
3763 	if (!aux)
3764 		return;
3765 
3766 	if (write_dsc_decompression_flag(aux,
3767 					 DP_DSC_PASSTHROUGH_EN, enable) < 0)
3768 		drm_dbg_kms(display->drm,
3769 			    "Failed to %s sink compression passthrough state\n",
3770 			    str_enable_disable(enable));
3771 }
3772 
3773 static int intel_dp_dsc_aux_ref_count(struct intel_atomic_state *state,
3774 				      const struct intel_connector *connector,
3775 				      bool for_get_ref)
3776 {
3777 	struct intel_display *display = to_intel_display(state);
3778 	struct drm_connector *_connector_iter;
3779 	struct drm_connector_state *old_conn_state;
3780 	struct drm_connector_state *new_conn_state;
3781 	int ref_count = 0;
3782 	int i;
3783 
3784 	/*
3785 	 * On SST the decompression AUX device won't be shared, each connector
3786 	 * uses for this its own AUX targeting the sink device.
3787 	 */
3788 	if (!connector->mst.dp)
3789 		return connector->dp.dsc_decompression_enabled ? 1 : 0;
3790 
3791 	for_each_oldnew_connector_in_state(&state->base, _connector_iter,
3792 					   old_conn_state, new_conn_state, i) {
3793 		const struct intel_connector *
3794 			connector_iter = to_intel_connector(_connector_iter);
3795 
3796 		if (connector_iter->mst.dp != connector->mst.dp)
3797 			continue;
3798 
3799 		if (!connector_iter->dp.dsc_decompression_enabled)
3800 			continue;
3801 
3802 		drm_WARN_ON(display->drm,
3803 			    (for_get_ref && !new_conn_state->crtc) ||
3804 			    (!for_get_ref && !old_conn_state->crtc));
3805 
3806 		if (connector_iter->dp.dsc_decompression_aux ==
3807 		    connector->dp.dsc_decompression_aux)
3808 			ref_count++;
3809 	}
3810 
3811 	return ref_count;
3812 }
3813 
3814 static bool intel_dp_dsc_aux_get_ref(struct intel_atomic_state *state,
3815 				     struct intel_connector *connector)
3816 {
3817 	bool ret = intel_dp_dsc_aux_ref_count(state, connector, true) == 0;
3818 
3819 	connector->dp.dsc_decompression_enabled = true;
3820 
3821 	return ret;
3822 }
3823 
3824 static bool intel_dp_dsc_aux_put_ref(struct intel_atomic_state *state,
3825 				     struct intel_connector *connector)
3826 {
3827 	connector->dp.dsc_decompression_enabled = false;
3828 
3829 	return intel_dp_dsc_aux_ref_count(state, connector, false) == 0;
3830 }
3831 
3832 /**
3833  * intel_dp_sink_enable_decompression - Enable DSC decompression in sink/last branch device
3834  * @state: atomic state
3835  * @connector: connector to enable the decompression for
3836  * @new_crtc_state: new state for the CRTC driving @connector
3837  *
3838  * Enable the DSC decompression if required in the %DP_DSC_ENABLE DPCD
3839  * register of the appropriate sink/branch device. On SST this is always the
3840  * sink device, whereas on MST based on each device's DSC capabilities it's
3841  * either the last branch device (enabling decompression in it) or both the
3842  * last branch device (enabling passthrough in it) and the sink device
3843  * (enabling decompression in it).
3844  */
3845 void intel_dp_sink_enable_decompression(struct intel_atomic_state *state,
3846 					struct intel_connector *connector,
3847 					const struct intel_crtc_state *new_crtc_state)
3848 {
3849 	struct intel_display *display = to_intel_display(state);
3850 
3851 	if (!new_crtc_state->dsc.compression_enable)
3852 		return;
3853 
3854 	if (drm_WARN_ON(display->drm,
3855 			!connector->dp.dsc_decompression_aux ||
3856 			connector->dp.dsc_decompression_enabled))
3857 		return;
3858 
3859 	if (!intel_dp_dsc_aux_get_ref(state, connector))
3860 		return;
3861 
3862 	intel_dp_sink_set_dsc_passthrough(connector, true);
3863 	intel_dp_sink_set_dsc_decompression(connector, true);
3864 }
3865 
3866 /**
3867  * intel_dp_sink_disable_decompression - Disable DSC decompression in sink/last branch device
3868  * @state: atomic state
3869  * @connector: connector to disable the decompression for
3870  * @old_crtc_state: old state for the CRTC driving @connector
3871  *
3872  * Disable the DSC decompression if required in the %DP_DSC_ENABLE DPCD
3873  * register of the appropriate sink/branch device, corresponding to the
3874  * sequence in intel_dp_sink_enable_decompression().
3875  */
3876 void intel_dp_sink_disable_decompression(struct intel_atomic_state *state,
3877 					 struct intel_connector *connector,
3878 					 const struct intel_crtc_state *old_crtc_state)
3879 {
3880 	struct intel_display *display = to_intel_display(state);
3881 
3882 	if (!old_crtc_state->dsc.compression_enable)
3883 		return;
3884 
3885 	if (drm_WARN_ON(display->drm,
3886 			!connector->dp.dsc_decompression_aux ||
3887 			!connector->dp.dsc_decompression_enabled))
3888 		return;
3889 
3890 	if (!intel_dp_dsc_aux_put_ref(state, connector))
3891 		return;
3892 
3893 	intel_dp_sink_set_dsc_decompression(connector, false);
3894 	intel_dp_sink_set_dsc_passthrough(connector, false);
3895 }
3896 
3897 static void
3898 intel_dp_init_source_oui(struct intel_dp *intel_dp)
3899 {
3900 	struct intel_display *display = to_intel_display(intel_dp);
3901 	u8 oui[] = { 0x00, 0xaa, 0x01 };
3902 	u8 buf[3] = {};
3903 
3904 	if (READ_ONCE(intel_dp->oui_valid))
3905 		return;
3906 
3907 	WRITE_ONCE(intel_dp->oui_valid, true);
3908 
3909 	/*
3910 	 * During driver init, we want to be careful and avoid changing the source OUI if it's
3911 	 * already set to what we want, so as to avoid clearing any state by accident
3912 	 */
3913 	if (drm_dp_dpcd_read(&intel_dp->aux, DP_SOURCE_OUI, buf, sizeof(buf)) < 0)
3914 		drm_dbg_kms(display->drm, "Failed to read source OUI\n");
3915 
3916 	if (memcmp(oui, buf, sizeof(oui)) == 0) {
3917 		/* Assume the OUI was written now. */
3918 		intel_dp->last_oui_write = jiffies;
3919 		return;
3920 	}
3921 
3922 	if (drm_dp_dpcd_write(&intel_dp->aux, DP_SOURCE_OUI, oui, sizeof(oui)) < 0) {
3923 		drm_dbg_kms(display->drm, "Failed to write source OUI\n");
3924 		WRITE_ONCE(intel_dp->oui_valid, false);
3925 	}
3926 
3927 	intel_dp->last_oui_write = jiffies;
3928 }
3929 
3930 void intel_dp_invalidate_source_oui(struct intel_dp *intel_dp)
3931 {
3932 	WRITE_ONCE(intel_dp->oui_valid, false);
3933 }
3934 
3935 void intel_dp_wait_source_oui(struct intel_dp *intel_dp)
3936 {
3937 	struct intel_display *display = to_intel_display(intel_dp);
3938 	struct intel_connector *connector = intel_dp->attached_connector;
3939 
3940 	drm_dbg_kms(display->drm,
3941 		    "[CONNECTOR:%d:%s] Performing OUI wait (%u ms)\n",
3942 		    connector->base.base.id, connector->base.name,
3943 		    connector->panel.vbt.backlight.hdr_dpcd_refresh_timeout);
3944 
3945 	wait_remaining_ms_from_jiffies(intel_dp->last_oui_write,
3946 				       connector->panel.vbt.backlight.hdr_dpcd_refresh_timeout);
3947 }
3948 
3949 /* If the device supports it, try to set the power state appropriately */
3950 void intel_dp_set_power(struct intel_dp *intel_dp, u8 mode)
3951 {
3952 	struct intel_display *display = to_intel_display(intel_dp);
3953 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3954 	int ret, i;
3955 
3956 	/* Should have a valid DPCD by this point */
3957 	if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
3958 		return;
3959 
3960 	if (mode != DP_SET_POWER_D0) {
3961 		if (downstream_hpd_needs_d0(intel_dp))
3962 			return;
3963 
3964 		ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER, mode);
3965 	} else {
3966 		struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
3967 
3968 		intel_lspcon_resume(dig_port);
3969 
3970 		/* Write the source OUI as early as possible */
3971 		intel_dp_init_source_oui(intel_dp);
3972 
3973 		/*
3974 		 * When turning on, we need to retry for 1ms to give the sink
3975 		 * time to wake up.
3976 		 */
3977 		for (i = 0; i < 3; i++) {
3978 			ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER, mode);
3979 			if (ret == 1)
3980 				break;
3981 			msleep(1);
3982 		}
3983 
3984 		if (ret == 1 && intel_lspcon_active(dig_port))
3985 			intel_lspcon_wait_pcon_mode(dig_port);
3986 	}
3987 
3988 	if (ret != 1)
3989 		drm_dbg_kms(display->drm,
3990 			    "[ENCODER:%d:%s] Set power to %s failed\n",
3991 			    encoder->base.base.id, encoder->base.name,
3992 			    mode == DP_SET_POWER_D0 ? "D0" : "D3");
3993 }
3994 
3995 static bool
3996 intel_dp_get_dpcd(struct intel_dp *intel_dp);
3997 
3998 /**
3999  * intel_dp_sync_state - sync the encoder state during init/resume
4000  * @encoder: intel encoder to sync
4001  * @crtc_state: state for the CRTC connected to the encoder
4002  *
4003  * Sync any state stored in the encoder wrt. HW state during driver init
4004  * and system resume.
4005  */
4006 void intel_dp_sync_state(struct intel_encoder *encoder,
4007 			 const struct intel_crtc_state *crtc_state)
4008 {
4009 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4010 	bool dpcd_updated = false;
4011 
4012 	/*
4013 	 * Don't clobber DPCD if it's been already read out during output
4014 	 * setup (eDP) or detect.
4015 	 */
4016 	if (crtc_state && intel_dp->dpcd[DP_DPCD_REV] == 0) {
4017 		intel_dp_get_dpcd(intel_dp);
4018 		dpcd_updated = true;
4019 	}
4020 
4021 	intel_dp_tunnel_resume(intel_dp, crtc_state, dpcd_updated);
4022 
4023 	if (crtc_state) {
4024 		intel_dp_reset_link_params(intel_dp);
4025 		intel_dp_set_link_params(intel_dp, crtc_state->port_clock, crtc_state->lane_count);
4026 		intel_dp->link.active = true;
4027 	}
4028 }
4029 
4030 bool intel_dp_initial_fastset_check(struct intel_encoder *encoder,
4031 				    struct intel_crtc_state *crtc_state)
4032 {
4033 	struct intel_display *display = to_intel_display(encoder);
4034 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4035 	bool fastset = true;
4036 
4037 	/*
4038 	 * If BIOS has set an unsupported or non-standard link rate for some
4039 	 * reason force an encoder recompute and full modeset.
4040 	 */
4041 	if (intel_dp_rate_index(intel_dp->source_rates, intel_dp->num_source_rates,
4042 				crtc_state->port_clock) < 0) {
4043 		drm_dbg_kms(display->drm,
4044 			    "[ENCODER:%d:%s] Forcing full modeset due to unsupported link rate\n",
4045 			    encoder->base.base.id, encoder->base.name);
4046 		crtc_state->uapi.connectors_changed = true;
4047 		fastset = false;
4048 	}
4049 
4050 	/*
4051 	 * FIXME hack to force full modeset when DSC is being used.
4052 	 *
4053 	 * As long as we do not have full state readout and config comparison
4054 	 * of crtc_state->dsc, we have no way to ensure reliable fastset.
4055 	 * Remove once we have readout for DSC.
4056 	 */
4057 	if (crtc_state->dsc.compression_enable) {
4058 		drm_dbg_kms(display->drm,
4059 			    "[ENCODER:%d:%s] Forcing full modeset due to DSC being enabled\n",
4060 			    encoder->base.base.id, encoder->base.name);
4061 		crtc_state->uapi.mode_changed = true;
4062 		fastset = false;
4063 	}
4064 
4065 	if (CAN_PANEL_REPLAY(intel_dp)) {
4066 		drm_dbg_kms(display->drm,
4067 			    "[ENCODER:%d:%s] Forcing full modeset to compute panel replay state\n",
4068 			    encoder->base.base.id, encoder->base.name);
4069 		crtc_state->uapi.mode_changed = true;
4070 		fastset = false;
4071 	}
4072 
4073 	return fastset;
4074 }
4075 
4076 static void intel_dp_get_pcon_dsc_cap(struct intel_dp *intel_dp)
4077 {
4078 	struct intel_display *display = to_intel_display(intel_dp);
4079 
4080 	/* Clear the cached register set to avoid using stale values */
4081 
4082 	memset(intel_dp->pcon_dsc_dpcd, 0, sizeof(intel_dp->pcon_dsc_dpcd));
4083 
4084 	if (!drm_dp_is_branch(intel_dp->dpcd))
4085 		return;
4086 
4087 	if (drm_dp_dpcd_read(&intel_dp->aux, DP_PCON_DSC_ENCODER,
4088 			     intel_dp->pcon_dsc_dpcd,
4089 			     sizeof(intel_dp->pcon_dsc_dpcd)) < 0)
4090 		drm_err(display->drm, "Failed to read DPCD register 0x%x\n",
4091 			DP_PCON_DSC_ENCODER);
4092 
4093 	drm_dbg_kms(display->drm, "PCON ENCODER DSC DPCD: %*ph\n",
4094 		    (int)sizeof(intel_dp->pcon_dsc_dpcd), intel_dp->pcon_dsc_dpcd);
4095 }
4096 
4097 static int intel_dp_pcon_get_frl_mask(u8 frl_bw_mask)
4098 {
4099 	static const int bw_gbps[] = {9, 18, 24, 32, 40, 48};
4100 	int i;
4101 
4102 	for (i = ARRAY_SIZE(bw_gbps) - 1; i >= 0; i--) {
4103 		if (frl_bw_mask & (1 << i))
4104 			return bw_gbps[i];
4105 	}
4106 	return 0;
4107 }
4108 
4109 static int intel_dp_pcon_set_frl_mask(int max_frl)
4110 {
4111 	switch (max_frl) {
4112 	case 48:
4113 		return DP_PCON_FRL_BW_MASK_48GBPS;
4114 	case 40:
4115 		return DP_PCON_FRL_BW_MASK_40GBPS;
4116 	case 32:
4117 		return DP_PCON_FRL_BW_MASK_32GBPS;
4118 	case 24:
4119 		return DP_PCON_FRL_BW_MASK_24GBPS;
4120 	case 18:
4121 		return DP_PCON_FRL_BW_MASK_18GBPS;
4122 	case 9:
4123 		return DP_PCON_FRL_BW_MASK_9GBPS;
4124 	}
4125 
4126 	return 0;
4127 }
4128 
4129 static int intel_dp_hdmi_sink_max_frl(struct intel_dp *intel_dp)
4130 {
4131 	struct intel_connector *connector = intel_dp->attached_connector;
4132 	const struct drm_display_info *info = &connector->base.display_info;
4133 	int max_frl_rate;
4134 	int max_lanes, rate_per_lane;
4135 	int max_dsc_lanes, dsc_rate_per_lane;
4136 
4137 	max_lanes = info->hdmi.max_lanes;
4138 	rate_per_lane = info->hdmi.max_frl_rate_per_lane;
4139 	max_frl_rate = max_lanes * rate_per_lane;
4140 
4141 	/*
4142 	 * The sink's DSC max FRL rate only applies to compressed video
4143 	 * transport, which requires a DSC 1.2 encoder in the PCON. Without
4144 	 * one the HDMI link always carries uncompressed video, for which
4145 	 * the regular max FRL rate is the limit.
4146 	 */
4147 	if (drm_dp_pcon_enc_is_dsc_1_2(intel_dp->pcon_dsc_dpcd) &&
4148 	    info->hdmi.dsc_cap.v_1p2) {
4149 		max_dsc_lanes = info->hdmi.dsc_cap.max_lanes;
4150 		dsc_rate_per_lane = info->hdmi.dsc_cap.max_frl_rate_per_lane;
4151 		if (max_dsc_lanes && dsc_rate_per_lane)
4152 			max_frl_rate = min(max_frl_rate, max_dsc_lanes * dsc_rate_per_lane);
4153 	}
4154 
4155 	return max_frl_rate;
4156 }
4157 
4158 static bool
4159 intel_dp_pcon_is_frl_trained(struct intel_dp *intel_dp,
4160 			     u8 max_frl_bw_mask, u8 *frl_trained_mask)
4161 {
4162 	if (drm_dp_pcon_hdmi_link_active(&intel_dp->aux) &&
4163 	    drm_dp_pcon_hdmi_link_mode(&intel_dp->aux, frl_trained_mask) == DP_PCON_HDMI_MODE_FRL &&
4164 	    *frl_trained_mask >= max_frl_bw_mask)
4165 		return true;
4166 
4167 	return false;
4168 }
4169 
4170 static int intel_dp_pcon_start_frl_training(struct intel_dp *intel_dp)
4171 {
4172 	struct intel_display *display = to_intel_display(intel_dp);
4173 #define TIMEOUT_FRL_READY_MS 500
4174 #define TIMEOUT_HDMI_LINK_ACTIVE_MS 1000
4175 	int max_frl_bw, max_pcon_frl_bw, max_edid_frl_bw, ret;
4176 	u8 max_frl_bw_mask = 0, frl_trained_mask;
4177 	bool is_active;
4178 
4179 	max_pcon_frl_bw = intel_dp->dfp.pcon_max_frl_bw;
4180 	drm_dbg(display->drm, "PCON max rate = %d Gbps\n", max_pcon_frl_bw);
4181 
4182 	max_edid_frl_bw = intel_dp_hdmi_sink_max_frl(intel_dp);
4183 	drm_dbg(display->drm, "Sink max rate from EDID = %d Gbps\n",
4184 		max_edid_frl_bw);
4185 
4186 	max_frl_bw = min(max_edid_frl_bw, max_pcon_frl_bw);
4187 
4188 	if (max_frl_bw <= 0)
4189 		return -EINVAL;
4190 
4191 	max_frl_bw_mask = intel_dp_pcon_set_frl_mask(max_frl_bw);
4192 	drm_dbg(display->drm, "MAX_FRL_BW_MASK = %u\n", max_frl_bw_mask);
4193 
4194 	if (intel_dp_pcon_is_frl_trained(intel_dp, max_frl_bw_mask, &frl_trained_mask))
4195 		goto frl_trained;
4196 
4197 	ret = drm_dp_pcon_frl_prepare(&intel_dp->aux, false);
4198 	if (ret < 0)
4199 		return ret;
4200 	/* Wait for PCON to be FRL Ready */
4201 	ret = poll_timeout_us(is_active = drm_dp_pcon_is_frl_ready(&intel_dp->aux),
4202 			      is_active,
4203 			      1000, TIMEOUT_FRL_READY_MS * 1000, false);
4204 	if (ret)
4205 		return ret;
4206 
4207 	ret = drm_dp_pcon_frl_configure_1(&intel_dp->aux, max_frl_bw,
4208 					  DP_PCON_ENABLE_SEQUENTIAL_LINK);
4209 	if (ret < 0)
4210 		return ret;
4211 	ret = drm_dp_pcon_frl_configure_2(&intel_dp->aux, max_frl_bw_mask,
4212 					  DP_PCON_FRL_LINK_TRAIN_NORMAL);
4213 	if (ret < 0)
4214 		return ret;
4215 	ret = drm_dp_pcon_frl_enable(&intel_dp->aux);
4216 	if (ret < 0)
4217 		return ret;
4218 	/*
4219 	 * Wait for FRL to be completed
4220 	 * Check if the HDMI Link is up and active.
4221 	 */
4222 	ret = poll_timeout_us(is_active = intel_dp_pcon_is_frl_trained(intel_dp, max_frl_bw_mask, &frl_trained_mask),
4223 			      is_active,
4224 			      1000, TIMEOUT_HDMI_LINK_ACTIVE_MS * 1000, false);
4225 	if (ret)
4226 		return ret;
4227 
4228 frl_trained:
4229 	drm_dbg(display->drm, "FRL_TRAINED_MASK = %u\n", frl_trained_mask);
4230 	intel_dp->frl.trained_rate_gbps = intel_dp_pcon_get_frl_mask(frl_trained_mask);
4231 	intel_dp->frl.is_trained = true;
4232 	drm_dbg(display->drm, "FRL trained with : %d Gbps\n",
4233 		intel_dp->frl.trained_rate_gbps);
4234 
4235 	return 0;
4236 }
4237 
4238 static bool intel_dp_is_hdmi_2_1_sink(struct intel_dp *intel_dp)
4239 {
4240 	if (drm_dp_is_branch(intel_dp->dpcd) &&
4241 	    intel_dp_has_hdmi_sink(intel_dp) &&
4242 	    intel_dp_hdmi_sink_max_frl(intel_dp) > 0)
4243 		return true;
4244 
4245 	return false;
4246 }
4247 
4248 static
4249 int intel_dp_pcon_set_tmds_mode(struct intel_dp *intel_dp)
4250 {
4251 	int ret;
4252 	u8 buf = 0;
4253 
4254 	/* Set PCON source control mode */
4255 	buf |= DP_PCON_ENABLE_SOURCE_CTL_MODE;
4256 
4257 	ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
4258 	if (ret < 0)
4259 		return ret;
4260 
4261 	/* Set HDMI LINK ENABLE */
4262 	buf |= DP_PCON_ENABLE_HDMI_LINK;
4263 	ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
4264 	if (ret < 0)
4265 		return ret;
4266 
4267 	return 0;
4268 }
4269 
4270 void intel_dp_check_frl_training(struct intel_dp *intel_dp)
4271 {
4272 	struct intel_display *display = to_intel_display(intel_dp);
4273 
4274 	/*
4275 	 * Always go for FRL training if:
4276 	 * -PCON supports SRC_CTL_MODE (VESA DP2.0-HDMI2.1 PCON Spec Draft-1 Sec-7)
4277 	 * -sink is HDMI2.1
4278 	 */
4279 	if (!(intel_dp->downstream_ports[2] & DP_PCON_SOURCE_CTL_MODE) ||
4280 	    !intel_dp_is_hdmi_2_1_sink(intel_dp) ||
4281 	    intel_dp->frl.is_trained)
4282 		return;
4283 
4284 	if (intel_dp_pcon_start_frl_training(intel_dp) < 0) {
4285 		int ret, mode;
4286 
4287 		drm_dbg(display->drm,
4288 			"Couldn't set FRL mode, continuing with TMDS mode\n");
4289 		ret = intel_dp_pcon_set_tmds_mode(intel_dp);
4290 		mode = drm_dp_pcon_hdmi_link_mode(&intel_dp->aux, NULL);
4291 
4292 		if (ret < 0 || mode != DP_PCON_HDMI_MODE_TMDS)
4293 			drm_dbg(display->drm,
4294 				"Issue with PCON, cannot set TMDS mode\n");
4295 	} else {
4296 		drm_dbg(display->drm, "FRL training Completed\n");
4297 	}
4298 }
4299 
4300 static int
4301 intel_dp_pcon_dsc_enc_slice_height(const struct intel_crtc_state *crtc_state)
4302 {
4303 	int vactive = crtc_state->hw.adjusted_mode.vdisplay;
4304 
4305 	return intel_hdmi_dsc_get_slice_height(vactive);
4306 }
4307 
4308 static int
4309 intel_dp_pcon_dsc_enc_slices(struct intel_dp *intel_dp,
4310 			     const struct intel_crtc_state *crtc_state)
4311 {
4312 	struct intel_connector *connector = intel_dp->attached_connector;
4313 	const struct drm_display_info *info = &connector->base.display_info;
4314 	int hdmi_throughput = info->hdmi.dsc_cap.clk_per_slice;
4315 	int hdmi_max_slices = info->hdmi.dsc_cap.max_slices;
4316 	int pcon_max_slices = drm_dp_pcon_dsc_max_slices(intel_dp->pcon_dsc_dpcd);
4317 	int pcon_max_slice_width = drm_dp_pcon_dsc_max_slice_width(intel_dp->pcon_dsc_dpcd);
4318 
4319 	return intel_hdmi_dsc_get_num_slices(crtc_state, pcon_max_slices,
4320 					     pcon_max_slice_width,
4321 					     hdmi_max_slices, hdmi_throughput);
4322 }
4323 
4324 static int
4325 intel_dp_pcon_dsc_enc_bpp(struct intel_dp *intel_dp,
4326 			  const struct intel_crtc_state *crtc_state,
4327 			  int num_slices, int slice_width)
4328 {
4329 	struct intel_connector *connector = intel_dp->attached_connector;
4330 	const struct drm_display_info *info = &connector->base.display_info;
4331 	int output_format = crtc_state->output_format;
4332 	bool hdmi_all_bpp = info->hdmi.dsc_cap.all_bpp;
4333 	int pcon_fractional_bpp = drm_dp_pcon_dsc_bpp_incr(intel_dp->pcon_dsc_dpcd);
4334 	int hdmi_max_chunk_bytes =
4335 		info->hdmi.dsc_cap.total_chunk_kbytes * 1024;
4336 
4337 	return intel_hdmi_dsc_get_bpp(pcon_fractional_bpp, slice_width,
4338 				      num_slices, output_format, hdmi_all_bpp,
4339 				      hdmi_max_chunk_bytes);
4340 }
4341 
4342 void
4343 intel_dp_pcon_dsc_configure(struct intel_dp *intel_dp,
4344 			    const struct intel_crtc_state *crtc_state)
4345 {
4346 	struct intel_display *display = to_intel_display(intel_dp);
4347 	struct intel_connector *connector = intel_dp->attached_connector;
4348 	const struct drm_display_info *info;
4349 	u8 pps_param[6];
4350 	int slice_height;
4351 	int slice_width;
4352 	int num_slices;
4353 	int bits_per_pixel;
4354 	int ret;
4355 	bool hdmi_is_dsc_1_2;
4356 
4357 	if (!intel_dp_is_hdmi_2_1_sink(intel_dp))
4358 		return;
4359 
4360 	if (!connector)
4361 		return;
4362 
4363 	info = &connector->base.display_info;
4364 
4365 	hdmi_is_dsc_1_2 = info->hdmi.dsc_cap.v_1p2;
4366 
4367 	if (!drm_dp_pcon_enc_is_dsc_1_2(intel_dp->pcon_dsc_dpcd) ||
4368 	    !hdmi_is_dsc_1_2)
4369 		return;
4370 
4371 	slice_height = intel_dp_pcon_dsc_enc_slice_height(crtc_state);
4372 	if (!slice_height)
4373 		return;
4374 
4375 	num_slices = intel_dp_pcon_dsc_enc_slices(intel_dp, crtc_state);
4376 	if (!num_slices)
4377 		return;
4378 
4379 	slice_width = DIV_ROUND_UP(crtc_state->hw.adjusted_mode.hdisplay,
4380 				   num_slices);
4381 
4382 	bits_per_pixel = intel_dp_pcon_dsc_enc_bpp(intel_dp, crtc_state,
4383 						   num_slices, slice_width);
4384 	if (!bits_per_pixel)
4385 		return;
4386 
4387 	pps_param[0] = slice_height & 0xFF;
4388 	pps_param[1] = slice_height >> 8;
4389 	pps_param[2] = slice_width & 0xFF;
4390 	pps_param[3] = slice_width >> 8;
4391 	pps_param[4] = bits_per_pixel & 0xFF;
4392 	pps_param[5] = (bits_per_pixel >> 8) & 0x3;
4393 
4394 	ret = drm_dp_pcon_pps_override_param(&intel_dp->aux, pps_param);
4395 	if (ret < 0)
4396 		drm_dbg_kms(display->drm, "Failed to set pcon DSC\n");
4397 }
4398 
4399 void intel_dp_configure_protocol_converter(struct intel_dp *intel_dp,
4400 					   const struct intel_crtc_state *crtc_state)
4401 {
4402 	struct intel_display *display = to_intel_display(intel_dp);
4403 	bool ycbcr444_to_420 = false;
4404 	bool rgb_to_ycbcr = false;
4405 	u8 tmp;
4406 
4407 	if (intel_dp->dpcd[DP_DPCD_REV] < 0x13)
4408 		return;
4409 
4410 	if (!drm_dp_is_branch(intel_dp->dpcd))
4411 		return;
4412 
4413 	tmp = intel_dp_has_hdmi_sink(intel_dp) ? DP_HDMI_DVI_OUTPUT_CONFIG : 0;
4414 
4415 	if (drm_dp_dpcd_writeb(&intel_dp->aux,
4416 			       DP_PROTOCOL_CONVERTER_CONTROL_0, tmp) != 1)
4417 		drm_dbg_kms(display->drm,
4418 			    "Failed to %s protocol converter HDMI mode\n",
4419 			    str_enable_disable(intel_dp_has_hdmi_sink(intel_dp)));
4420 
4421 	if (crtc_state->sink_format == INTEL_OUTPUT_FORMAT_YCBCR420) {
4422 		switch (crtc_state->output_format) {
4423 		case INTEL_OUTPUT_FORMAT_YCBCR420:
4424 			break;
4425 		case INTEL_OUTPUT_FORMAT_YCBCR444:
4426 			ycbcr444_to_420 = true;
4427 			break;
4428 		case INTEL_OUTPUT_FORMAT_RGB:
4429 			rgb_to_ycbcr = true;
4430 			ycbcr444_to_420 = true;
4431 			break;
4432 		default:
4433 			MISSING_CASE(crtc_state->output_format);
4434 			break;
4435 		}
4436 	} else if (crtc_state->sink_format == INTEL_OUTPUT_FORMAT_YCBCR444) {
4437 		switch (crtc_state->output_format) {
4438 		case INTEL_OUTPUT_FORMAT_YCBCR444:
4439 			break;
4440 		case INTEL_OUTPUT_FORMAT_RGB:
4441 			rgb_to_ycbcr = true;
4442 			break;
4443 		default:
4444 			MISSING_CASE(crtc_state->output_format);
4445 			break;
4446 		}
4447 	}
4448 
4449 	tmp = ycbcr444_to_420 ? DP_CONVERSION_TO_YCBCR420_ENABLE : 0;
4450 
4451 	if (drm_dp_dpcd_writeb(&intel_dp->aux,
4452 			       DP_PROTOCOL_CONVERTER_CONTROL_1, tmp) != 1)
4453 		drm_dbg_kms(display->drm,
4454 			    "Failed to %s protocol converter YCbCr 4:2:0 conversion mode\n",
4455 			    str_enable_disable(intel_dp->dfp.ycbcr_444_to_420));
4456 
4457 	tmp = rgb_to_ycbcr ? DP_CONVERSION_BT709_RGB_YCBCR_ENABLE : 0;
4458 
4459 	if (drm_dp_pcon_convert_rgb_to_ycbcr(&intel_dp->aux, tmp) < 0)
4460 		drm_dbg_kms(display->drm,
4461 			    "Failed to %s protocol converter RGB->YCbCr conversion mode\n",
4462 			    str_enable_disable(tmp));
4463 }
4464 
4465 static bool intel_dp_get_colorimetry_status(struct intel_dp *intel_dp)
4466 {
4467 	u8 dprx = 0;
4468 
4469 	if (drm_dp_dpcd_readb(&intel_dp->aux, DP_DPRX_FEATURE_ENUMERATION_LIST,
4470 			      &dprx) != 1)
4471 		return false;
4472 	return dprx & DP_VSC_SDP_EXT_FOR_COLORIMETRY_SUPPORTED;
4473 }
4474 
4475 static int intel_dp_read_dsc_dpcd(struct drm_dp_aux *aux,
4476 				  u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE])
4477 {
4478 	int ret;
4479 
4480 	ret = drm_dp_dpcd_read_data(aux, DP_DSC_SUPPORT, dsc_dpcd,
4481 				    DP_DSC_RECEIVER_CAP_SIZE);
4482 	if (ret) {
4483 		drm_dbg_kms(aux->drm_dev,
4484 			    "Could not read DSC DPCD register 0x%x Error: %pe\n",
4485 			    DP_DSC_SUPPORT, ERR_PTR(ret));
4486 		return ret;
4487 	}
4488 
4489 	drm_dbg_kms(aux->drm_dev, "DSC DPCD: %*ph\n",
4490 		    DP_DSC_RECEIVER_CAP_SIZE,
4491 		    dsc_dpcd);
4492 	return 0;
4493 }
4494 
4495 static void init_dsc_overall_throughput_limits(struct intel_connector *connector, bool is_branch)
4496 {
4497 	u8 branch_caps[DP_DSC_BRANCH_CAP_SIZE];
4498 	int line_width;
4499 
4500 	connector->dp.dsc_branch_caps.overall_throughput.rgb_yuv444 = INT_MAX;
4501 	connector->dp.dsc_branch_caps.overall_throughput.yuv422_420 = INT_MAX;
4502 	connector->dp.dsc_branch_caps.max_line_width = INT_MAX;
4503 
4504 	if (!is_branch)
4505 		return;
4506 
4507 	if (drm_dp_dpcd_read_data(connector->dp.dsc_decompression_aux,
4508 				  DP_DSC_BRANCH_OVERALL_THROUGHPUT_0, branch_caps,
4509 				  sizeof(branch_caps)) != 0)
4510 		return;
4511 
4512 	connector->dp.dsc_branch_caps.overall_throughput.rgb_yuv444 =
4513 		drm_dp_dsc_branch_max_overall_throughput(branch_caps, true) ? : INT_MAX;
4514 
4515 	connector->dp.dsc_branch_caps.overall_throughput.yuv422_420 =
4516 		drm_dp_dsc_branch_max_overall_throughput(branch_caps, false) ? : INT_MAX;
4517 
4518 	line_width = drm_dp_dsc_branch_max_line_width(branch_caps);
4519 	connector->dp.dsc_branch_caps.max_line_width = line_width > 0 ? line_width : INT_MAX;
4520 }
4521 
4522 void intel_dp_get_dsc_sink_cap(u8 dpcd_rev,
4523 			       const struct drm_dp_desc *desc, bool is_branch,
4524 			       struct intel_connector *connector)
4525 {
4526 	struct intel_display *display = to_intel_display(connector);
4527 
4528 	/*
4529 	 * Clear the cached register set to avoid using stale values
4530 	 * for the sinks that do not support DSC.
4531 	 */
4532 	memset(connector->dp.dsc_dpcd, 0, sizeof(connector->dp.dsc_dpcd));
4533 
4534 	/* Clear fec_capable to avoid using stale values */
4535 	connector->dp.fec_capability = 0;
4536 
4537 	memset(&connector->dp.dsc_branch_caps, 0, sizeof(connector->dp.dsc_branch_caps));
4538 	connector->dp.dsc_throughput_quirk = false;
4539 
4540 	if (dpcd_rev < DP_DPCD_REV_14)
4541 		return;
4542 
4543 	if (intel_dp_read_dsc_dpcd(connector->dp.dsc_decompression_aux,
4544 				   connector->dp.dsc_dpcd) < 0)
4545 		return;
4546 
4547 	if (drm_dp_dpcd_readb(connector->dp.dsc_decompression_aux, DP_FEC_CAPABILITY,
4548 			      &connector->dp.fec_capability) < 0) {
4549 		drm_dbg_kms(display->drm, "Could not read FEC DPCD register\n");
4550 		return;
4551 	}
4552 
4553 	drm_dbg_kms(display->drm, "FEC CAPABILITY: %x\n",
4554 		    connector->dp.fec_capability);
4555 
4556 	if (!(connector->dp.dsc_dpcd[0] & DP_DSC_DECOMPRESSION_IS_SUPPORTED))
4557 		return;
4558 
4559 	init_dsc_overall_throughput_limits(connector, is_branch);
4560 
4561 	/*
4562 	 * TODO: Move the HW rev check as well to the DRM core quirk table if
4563 	 * that's required after clarifying the list of affected devices.
4564 	 */
4565 	if (drm_dp_has_quirk(desc, DP_DPCD_QUIRK_DSC_THROUGHPUT_BPP_LIMIT) &&
4566 	    desc->ident.hw_rev == 0x10)
4567 		connector->dp.dsc_throughput_quirk = true;
4568 }
4569 
4570 static void intel_edp_get_dsc_sink_cap(u8 edp_dpcd_rev, struct intel_connector *connector)
4571 {
4572 	if (edp_dpcd_rev < DP_EDP_14)
4573 		return;
4574 
4575 	if (intel_dp_read_dsc_dpcd(connector->dp.dsc_decompression_aux,
4576 				   connector->dp.dsc_dpcd) < 0)
4577 		return;
4578 
4579 	if (connector->dp.dsc_dpcd[0] & DP_DSC_DECOMPRESSION_IS_SUPPORTED)
4580 		init_dsc_overall_throughput_limits(connector, false);
4581 }
4582 
4583 static void
4584 intel_dp_detect_dsc_caps(struct intel_dp *intel_dp, struct intel_connector *connector)
4585 {
4586 	struct intel_display *display = to_intel_display(intel_dp);
4587 
4588 	/* Read DP Sink DSC Cap DPCD regs for DP v1.4 */
4589 	if (!HAS_DSC(display))
4590 		return;
4591 
4592 	if (intel_dp_is_edp(intel_dp))
4593 		intel_edp_get_dsc_sink_cap(intel_dp->edp_dpcd[0],
4594 					   connector);
4595 	else
4596 		intel_dp_get_dsc_sink_cap(intel_dp->dpcd[DP_DPCD_REV],
4597 					  &intel_dp->desc, drm_dp_is_branch(intel_dp->dpcd),
4598 					  connector);
4599 }
4600 
4601 static void intel_edp_mso_mode_fixup(struct intel_connector *connector,
4602 				     struct drm_display_mode *mode)
4603 {
4604 	struct intel_display *display = to_intel_display(connector);
4605 	struct intel_dp *intel_dp = intel_attached_dp(connector);
4606 	int n = intel_dp->mso_link_count;
4607 	int overlap = intel_dp->mso_pixel_overlap;
4608 
4609 	if (!mode || !n)
4610 		return;
4611 
4612 	mode->hdisplay = (mode->hdisplay - overlap) * n;
4613 	mode->hsync_start = (mode->hsync_start - overlap) * n;
4614 	mode->hsync_end = (mode->hsync_end - overlap) * n;
4615 	mode->htotal = (mode->htotal - overlap) * n;
4616 	mode->clock *= n;
4617 
4618 	drm_mode_set_name(mode);
4619 
4620 	drm_dbg_kms(display->drm,
4621 		    "[CONNECTOR:%d:%s] using generated MSO mode: " DRM_MODE_FMT "\n",
4622 		    connector->base.base.id, connector->base.name,
4623 		    DRM_MODE_ARG(mode));
4624 }
4625 
4626 void intel_edp_fixup_vbt_bpp(struct intel_encoder *encoder, int pipe_bpp)
4627 {
4628 	struct intel_display *display = to_intel_display(encoder);
4629 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4630 	struct intel_connector *connector = intel_dp->attached_connector;
4631 
4632 	if (connector->panel.vbt.edp.bpp && pipe_bpp > connector->panel.vbt.edp.bpp) {
4633 		/*
4634 		 * This is a big fat ugly hack.
4635 		 *
4636 		 * Some machines in UEFI boot mode provide us a VBT that has 18
4637 		 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
4638 		 * unknown we fail to light up. Yet the same BIOS boots up with
4639 		 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
4640 		 * max, not what it tells us to use.
4641 		 *
4642 		 * Note: This will still be broken if the eDP panel is not lit
4643 		 * up by the BIOS, and thus we can't get the mode at module
4644 		 * load.
4645 		 */
4646 		drm_dbg_kms(display->drm,
4647 			    "pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
4648 			    pipe_bpp, connector->panel.vbt.edp.bpp);
4649 		connector->panel.vbt.edp.bpp = pipe_bpp;
4650 	}
4651 }
4652 
4653 static void intel_edp_mso_init(struct intel_dp *intel_dp)
4654 {
4655 	struct intel_display *display = to_intel_display(intel_dp);
4656 	struct intel_connector *connector = intel_dp->attached_connector;
4657 	struct drm_display_info *info = &connector->base.display_info;
4658 	u8 mso;
4659 
4660 	if (intel_dp->edp_dpcd[0] < DP_EDP_14)
4661 		return;
4662 
4663 	if (drm_dp_dpcd_readb(&intel_dp->aux, DP_EDP_MSO_LINK_CAPABILITIES, &mso) != 1) {
4664 		drm_err(display->drm, "Failed to read MSO cap\n");
4665 		return;
4666 	}
4667 
4668 	/* Valid configurations are SST or MSO 2x1, 2x2, 4x1 */
4669 	mso &= DP_EDP_MSO_NUMBER_OF_LINKS_MASK;
4670 	if (mso % 2 || mso > drm_dp_max_lane_count(intel_dp->dpcd)) {
4671 		drm_err(display->drm, "Invalid MSO link count cap %u\n", mso);
4672 		mso = 0;
4673 	}
4674 
4675 	if (mso) {
4676 		drm_dbg_kms(display->drm,
4677 			    "Sink MSO %ux%u configuration, pixel overlap %u\n",
4678 			    mso, drm_dp_max_lane_count(intel_dp->dpcd) / mso,
4679 			    info->mso_pixel_overlap);
4680 		if (!HAS_MSO(display)) {
4681 			drm_err(display->drm,
4682 				"No source MSO support, disabling\n");
4683 			mso = 0;
4684 		}
4685 	}
4686 
4687 	intel_dp->mso_link_count = mso;
4688 	intel_dp->mso_pixel_overlap = mso ? info->mso_pixel_overlap : 0;
4689 }
4690 
4691 static void
4692 intel_edp_set_data_override_rates(struct intel_dp *intel_dp)
4693 {
4694 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
4695 	int *sink_rates = intel_dp->sink_rates;
4696 	int i, count = 0;
4697 
4698 	for (i = 0; i < intel_dp->num_sink_rates; i++) {
4699 		if (intel_bios_encoder_reject_edp_rate(encoder->devdata,
4700 						       intel_dp->sink_rates[i]))
4701 			continue;
4702 
4703 		sink_rates[count++] = intel_dp->sink_rates[i];
4704 	}
4705 	intel_dp->num_sink_rates = count;
4706 }
4707 
4708 static void
4709 intel_edp_set_sink_rates(struct intel_dp *intel_dp)
4710 {
4711 	struct intel_display *display = to_intel_display(intel_dp);
4712 
4713 	intel_dp->num_sink_rates = 0;
4714 
4715 	if (intel_dp->edp_dpcd[0] >= DP_EDP_14) {
4716 		__le16 sink_rates[DP_MAX_SUPPORTED_RATES];
4717 		int ret;
4718 		int i;
4719 
4720 		ret = drm_dp_dpcd_read_data(&intel_dp->aux,
4721 					    DP_SUPPORTED_LINK_RATES,
4722 					    sink_rates, sizeof(sink_rates));
4723 		if (ret < 0) {
4724 			drm_dbg_kms(display->drm,
4725 				    "Unable to read eDP supported link rates, using default rates\n");
4726 			memset(sink_rates, 0, sizeof(sink_rates));
4727 		}
4728 
4729 		for (i = 0; i < ARRAY_SIZE(sink_rates); i++) {
4730 			int rate;
4731 
4732 			/* Value read multiplied by 200kHz gives the per-lane
4733 			 * link rate in kHz. The source rates are, however,
4734 			 * stored in terms of LS_Clk kHz. The full conversion
4735 			 * back to symbols is
4736 			 * (val * 200kHz)*(8/10 ch. encoding)*(1/8 bit to Byte)
4737 			 */
4738 			rate = le16_to_cpu(sink_rates[i]) * 200 / 10;
4739 
4740 			if (rate == 0)
4741 				break;
4742 
4743 			/*
4744 			 * Some platforms cannot reliably drive HBR3 rates due to PHY limitations,
4745 			 * even if the sink advertises support. Reject any sink rates above HBR2 on
4746 			 * the known machines for stable output.
4747 			 */
4748 			if (rate > 540000 &&
4749 			    intel_has_quirk(display, QUIRK_EDP_LIMIT_RATE_HBR2))
4750 				break;
4751 
4752 			intel_dp->sink_rates[i] = rate;
4753 		}
4754 		intel_dp->num_sink_rates = i;
4755 	}
4756 
4757 	/*
4758 	 * Use DP_LINK_RATE_SET if DP_SUPPORTED_LINK_RATES are available,
4759 	 * default to DP_MAX_LINK_RATE and DP_LINK_BW_SET otherwise.
4760 	 */
4761 	if (intel_dp->num_sink_rates)
4762 		intel_dp->use_rate_select = true;
4763 	else
4764 		intel_dp_set_sink_rates(intel_dp);
4765 
4766 	intel_edp_set_data_override_rates(intel_dp);
4767 }
4768 
4769 static bool
4770 intel_edp_init_dpcd(struct intel_dp *intel_dp, struct intel_connector *connector)
4771 {
4772 	struct intel_display *display = to_intel_display(intel_dp);
4773 	int ret;
4774 
4775 	/* this function is meant to be called only once */
4776 	drm_WARN_ON(display->drm, intel_dp->dpcd[DP_DPCD_REV] != 0);
4777 
4778 	if (drm_dp_read_dpcd_caps(&intel_dp->aux, intel_dp->dpcd) != 0)
4779 		return false;
4780 
4781 	drm_dp_read_desc(&intel_dp->aux, &intel_dp->desc,
4782 			 drm_dp_is_branch(intel_dp->dpcd));
4783 	intel_init_dpcd_quirks(intel_dp, &intel_dp->desc.ident);
4784 
4785 	intel_dp->colorimetry_support =
4786 		intel_dp_get_colorimetry_status(intel_dp);
4787 
4788 	/*
4789 	 * Read the eDP display control registers.
4790 	 *
4791 	 * Do this independent of DP_DPCD_DISPLAY_CONTROL_CAPABLE bit in
4792 	 * DP_EDP_CONFIGURATION_CAP, because some buggy displays do not have it
4793 	 * set, but require eDP 1.4+ detection (e.g. for supported link rates
4794 	 * method). The display control registers should read zero if they're
4795 	 * not supported anyway.
4796 	 */
4797 	if (drm_dp_dpcd_read(&intel_dp->aux, DP_EDP_DPCD_REV,
4798 			     intel_dp->edp_dpcd, sizeof(intel_dp->edp_dpcd)) ==
4799 			     sizeof(intel_dp->edp_dpcd)) {
4800 		drm_dbg_kms(display->drm, "eDP DPCD: %*ph\n",
4801 			    (int)sizeof(intel_dp->edp_dpcd),
4802 			    intel_dp->edp_dpcd);
4803 
4804 		intel_dp->use_max_params = intel_dp->edp_dpcd[0] < DP_EDP_14;
4805 	}
4806 
4807 	/*
4808 	 * If needed, program our source OUI so we can make various Intel-specific AUX services
4809 	 * available (such as HDR backlight controls)
4810 	 */
4811 	intel_dp_init_source_oui(intel_dp);
4812 
4813 	/* Read the ALPM DPCD caps */
4814 	ret = drm_dp_dpcd_read_byte(&intel_dp->aux, DP_RECEIVER_ALPM_CAP,
4815 				    &intel_dp->alpm_dpcd);
4816 	if (ret < 0)
4817 		return false;
4818 
4819 	/*
4820 	 * This has to be called after intel_dp->edp_dpcd is filled, PSR checks
4821 	 * for SET_POWER_CAPABLE bit in intel_dp->edp_dpcd[1]
4822 	 */
4823 	intel_psr_init_dpcd(intel_dp, connector);
4824 
4825 	intel_edp_set_sink_rates(intel_dp);
4826 	intel_dp_set_max_sink_lane_count(intel_dp);
4827 
4828 	/* Read the eDP DSC DPCD registers */
4829 	intel_dp_detect_dsc_caps(intel_dp, connector);
4830 
4831 	return true;
4832 }
4833 
4834 static bool
4835 intel_dp_has_sink_count(struct intel_dp *intel_dp)
4836 {
4837 	if (!intel_dp->attached_connector)
4838 		return false;
4839 
4840 	return drm_dp_read_sink_count_cap(&intel_dp->attached_connector->base,
4841 					  intel_dp->dpcd,
4842 					  &intel_dp->desc);
4843 }
4844 
4845 void intel_dp_update_sink_caps(struct intel_dp *intel_dp)
4846 {
4847 	struct intel_display *display = to_intel_display(intel_dp);
4848 
4849 	intel_dp_set_sink_rates(intel_dp);
4850 	intel_dp_set_max_sink_lane_count(intel_dp);
4851 	/*
4852 	 * Handle unexpected sink cap changes, or a race between setting
4853 	 * the deferred link params flag in the HPD IRQ handler and
4854 	 * clearing the flag during connector detect.
4855 	 */
4856 	if (intel_dp_set_common_link_params(intel_dp) &&
4857 	    !intel_dp->reset_link_params) {
4858 		drm_dbg_kms(display->drm,
4859 			    "DPRX capabilities changed before long HPD or RX_CAP_CHANGED signal\n");
4860 		intel_dp->reset_link_params = true;
4861 	}
4862 }
4863 
4864 static bool
4865 intel_dp_get_dpcd(struct intel_dp *intel_dp)
4866 {
4867 	int ret;
4868 
4869 	if (intel_dp_init_lttpr_and_dprx_caps(intel_dp) < 0)
4870 		return false;
4871 
4872 	/*
4873 	 * Don't clobber cached eDP rates. Also skip re-reading
4874 	 * the OUI/ID since we know it won't change.
4875 	 */
4876 	if (!intel_dp_is_edp(intel_dp)) {
4877 		drm_dp_read_desc(&intel_dp->aux, &intel_dp->desc,
4878 				 drm_dp_is_branch(intel_dp->dpcd));
4879 
4880 		intel_init_dpcd_quirks(intel_dp, &intel_dp->desc.ident);
4881 
4882 		intel_dp->colorimetry_support =
4883 			intel_dp_get_colorimetry_status(intel_dp);
4884 
4885 		intel_dp_update_sink_caps(intel_dp);
4886 	}
4887 
4888 	if (intel_dp_has_sink_count(intel_dp)) {
4889 		ret = drm_dp_read_sink_count(&intel_dp->aux);
4890 		if (ret < 0)
4891 			return false;
4892 
4893 		/*
4894 		 * Sink count can change between short pulse hpd hence
4895 		 * a member variable in intel_dp will track any changes
4896 		 * between short pulse interrupts.
4897 		 */
4898 		intel_dp->sink_count = ret;
4899 
4900 		/*
4901 		 * SINK_COUNT == 0 and DOWNSTREAM_PORT_PRESENT == 1 implies that
4902 		 * a dongle is present but no display. Unless we require to know
4903 		 * if a dongle is present or not, we don't need to update
4904 		 * downstream port information. So, an early return here saves
4905 		 * time from performing other operations which are not required.
4906 		 */
4907 		if (!intel_dp->sink_count)
4908 			return false;
4909 	}
4910 
4911 	return drm_dp_read_downstream_info(&intel_dp->aux, intel_dp->dpcd,
4912 					   intel_dp->downstream_ports) == 0;
4913 }
4914 
4915 static const char *intel_dp_mst_mode_str(enum drm_dp_mst_mode mst_mode)
4916 {
4917 	if (mst_mode == DRM_DP_MST)
4918 		return "MST";
4919 	else if (mst_mode == DRM_DP_SST_SIDEBAND_MSG)
4920 		return "SST w/ sideband messaging";
4921 	else
4922 		return "SST";
4923 }
4924 
4925 static enum drm_dp_mst_mode
4926 intel_dp_mst_mode_choose(struct intel_dp *intel_dp,
4927 			 enum drm_dp_mst_mode sink_mst_mode)
4928 {
4929 	struct intel_display *display = to_intel_display(intel_dp);
4930 
4931 	if (!display->params.enable_dp_mst)
4932 		return DRM_DP_SST;
4933 
4934 	if (!intel_dp_mst_source_support(intel_dp))
4935 		return DRM_DP_SST;
4936 
4937 	if (sink_mst_mode == DRM_DP_SST_SIDEBAND_MSG &&
4938 	    !(intel_dp->dpcd[DP_MAIN_LINK_CHANNEL_CODING] & DP_CAP_ANSI_128B132B))
4939 		return DRM_DP_SST;
4940 
4941 	return sink_mst_mode;
4942 }
4943 
4944 static enum drm_dp_mst_mode
4945 intel_dp_mst_detect(struct intel_dp *intel_dp)
4946 {
4947 	struct intel_display *display = to_intel_display(intel_dp);
4948 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
4949 	enum drm_dp_mst_mode sink_mst_mode;
4950 	enum drm_dp_mst_mode mst_detect;
4951 
4952 	sink_mst_mode = drm_dp_read_mst_cap(&intel_dp->aux, intel_dp->dpcd);
4953 
4954 	mst_detect = intel_dp_mst_mode_choose(intel_dp, sink_mst_mode);
4955 
4956 	drm_dbg_kms(display->drm,
4957 		    "[ENCODER:%d:%s] MST support: port: %s, sink: %s, modparam: %s -> enable: %s\n",
4958 		    encoder->base.base.id, encoder->base.name,
4959 		    str_yes_no(intel_dp_mst_source_support(intel_dp)),
4960 		    intel_dp_mst_mode_str(sink_mst_mode),
4961 		    str_yes_no(display->params.enable_dp_mst),
4962 		    intel_dp_mst_mode_str(mst_detect));
4963 
4964 	return mst_detect;
4965 }
4966 
4967 static void
4968 intel_dp_mst_configure(struct intel_dp *intel_dp)
4969 {
4970 	if (!intel_dp_mst_source_support(intel_dp))
4971 		return;
4972 
4973 	intel_dp->is_mst = intel_dp->mst_detect != DRM_DP_SST;
4974 
4975 	if (intel_dp->is_mst)
4976 		intel_dp_mst_prepare_probe(intel_dp);
4977 
4978 	drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst.mgr, intel_dp->is_mst);
4979 
4980 	/* Avoid stale info on the next detect cycle. */
4981 	intel_dp->mst_detect = DRM_DP_SST;
4982 }
4983 
4984 static void
4985 intel_dp_mst_disconnect(struct intel_dp *intel_dp)
4986 {
4987 	struct intel_display *display = to_intel_display(intel_dp);
4988 
4989 	if (!intel_dp->is_mst)
4990 		return;
4991 
4992 	drm_dbg_kms(display->drm,
4993 		    "MST device may have disappeared %d vs %d\n",
4994 		    intel_dp->is_mst, intel_dp->mst.mgr.mst_state);
4995 	intel_dp->is_mst = false;
4996 	drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst.mgr, intel_dp->is_mst);
4997 }
4998 
4999 #define INTEL_DP_DEVICE_SERVICE_IRQ_MASK_SST	(DP_AUTOMATED_TEST_REQUEST | \
5000 						 DP_CP_IRQ | \
5001 						 DP_SINK_SPECIFIC_IRQ)
5002 
5003 #define INTEL_DP_DEVICE_SERVICE_IRQ_MASK_MST	(DP_CP_IRQ | \
5004 						 DP_DOWN_REP_MSG_RDY | \
5005 						 DP_UP_REQ_MSG_RDY)
5006 
5007 #define INTEL_DP_LINK_SERVICE_IRQ_MASK_SST	(RX_CAP_CHANGED | \
5008 						 LINK_STATUS_CHANGED | \
5009 						 HDMI_LINK_STATUS_CHANGED | \
5010 						 CONNECTED_OFF_ENTRY_REQUESTED | \
5011 						 DP_TUNNELING_IRQ)
5012 
5013 #define INTEL_DP_LINK_SERVICE_IRQ_MASK_MST	(RX_CAP_CHANGED | \
5014 						 LINK_STATUS_CHANGED | \
5015 						 DP_TUNNELING_IRQ)
5016 
5017 static bool
5018 intel_dp_get_sink_irq_esi(struct intel_dp *intel_dp, u8 *esi)
5019 {
5020 	struct intel_display *display = to_intel_display(intel_dp);
5021 
5022 	/*
5023 	 * Display WA for HSD #13013007775: mtl/arl/lnl
5024 	 * Read the sink count and link service IRQ registers in separate
5025 	 * transactions to prevent disconnecting the sink on a TBT link
5026 	 * inadvertently.
5027 	 */
5028 	if (IS_DISPLAY_VER(display, 14, 20) && !display->platform.battlemage) {
5029 		if (drm_dp_dpcd_read(&intel_dp->aux, DP_SINK_COUNT_ESI, esi, 3) != 3)
5030 			return false;
5031 
5032 		/* DP_SINK_COUNT_ESI + 3 == DP_LINK_SERVICE_IRQ_VECTOR_ESI0 */
5033 		return drm_dp_dpcd_readb(&intel_dp->aux, DP_LINK_SERVICE_IRQ_VECTOR_ESI0,
5034 					 &esi[3]) == 1;
5035 	}
5036 
5037 	return drm_dp_dpcd_read(&intel_dp->aux, DP_SINK_COUNT_ESI, esi, 4) == 4;
5038 }
5039 
5040 static bool intel_dp_ack_sink_irq_esi(struct intel_dp *intel_dp, u8 esi[4])
5041 {
5042 	int retry;
5043 
5044 	for (retry = 0; retry < 3; retry++) {
5045 		if (drm_dp_dpcd_write(&intel_dp->aux, DP_SINK_COUNT_ESI + 1,
5046 				      &esi[1], 3) == 3)
5047 			return true;
5048 	}
5049 
5050 	return false;
5051 }
5052 
5053 /* Return %true if reading the ESI vector succeeded, %false otherwise. */
5054 static bool intel_dp_get_sink_irq_esi_sst(struct intel_dp *intel_dp, u8 esi[4])
5055 {
5056 	memset(esi, 0, 4);
5057 
5058 	/*
5059 	 * TODO: For DP_DPCD_REV >= 0x12 read
5060 	 * DP_SINK_COUNT_ESI and DP_DEVICE_SERVICE_IRQ_VECTOR_ESI0.
5061 	 */
5062 	if (drm_dp_dpcd_read_data(&intel_dp->aux, DP_SINK_COUNT, esi, 2) != 0)
5063 		return false;
5064 
5065 	if (intel_dp->dpcd[DP_DPCD_REV] < DP_DPCD_REV_12)
5066 		return true;
5067 
5068 	/* TODO: Read DP_DEVICE_SERVICE_IRQ_VECTOR_ESI1 as well */
5069 	if (drm_dp_dpcd_read_byte(&intel_dp->aux, DP_LINK_SERVICE_IRQ_VECTOR_ESI0, &esi[3]) != 0)
5070 		return false;
5071 
5072 	return true;
5073 }
5074 
5075 /* Return %true if acking the ESI vector IRQ events succeeded, %false otherwise. */
5076 static bool intel_dp_ack_sink_irq_esi_sst(struct intel_dp *intel_dp, u8 esi[4])
5077 {
5078 	/*
5079 	 * TODO: For DP_DPCD_REV >= 0x12 write
5080 	 * DP_DEVICE_SERVICE_IRQ_VECTOR_ESI0
5081 	 */
5082 	if (drm_dp_dpcd_write_byte(&intel_dp->aux, DP_DEVICE_SERVICE_IRQ_VECTOR, esi[1]) != 0)
5083 		return false;
5084 
5085 	if (intel_dp->dpcd[DP_DPCD_REV] < DP_DPCD_REV_12)
5086 		return true;
5087 
5088 	/* TODO: Read DP_DEVICE_SERVICE_IRQ_VECTOR_ESI1 as well */
5089 	if (drm_dp_dpcd_write_byte(&intel_dp->aux, DP_LINK_SERVICE_IRQ_VECTOR_ESI0, esi[3]) != 0)
5090 		return false;
5091 
5092 	return true;
5093 }
5094 
5095 /*
5096  * Return %true if reading the ESI vector and acking the ESI IRQ events succeeded,
5097  * %false otherwise.
5098  */
5099 static bool intel_dp_get_and_ack_sink_irq_esi_sst(struct intel_dp *intel_dp, u8 esi[4])
5100 {
5101 	struct intel_display *display = to_intel_display(intel_dp);
5102 	struct intel_connector *connector = intel_dp->attached_connector;
5103 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
5104 
5105 	if (!intel_dp_get_sink_irq_esi_sst(intel_dp, esi))
5106 		return false;
5107 
5108 	drm_dbg_kms(display->drm,
5109 		    "[CONNECTOR:%d:%s][ENCODER:%d:%s] DPRX ESI: %4ph\n",
5110 		    connector->base.base.id, connector->base.name,
5111 		    encoder->base.base.id, encoder->base.name,
5112 		    esi);
5113 
5114 	esi[1] &= INTEL_DP_DEVICE_SERVICE_IRQ_MASK_SST;
5115 	esi[3] &= INTEL_DP_LINK_SERVICE_IRQ_MASK_SST;
5116 
5117 	if (mem_is_zero(&esi[1], 3))
5118 		return true;
5119 
5120 	if (!intel_dp_ack_sink_irq_esi_sst(intel_dp, esi))
5121 		return false;
5122 
5123 	return true;
5124 }
5125 
5126 bool
5127 intel_dp_needs_vsc_colorimetry(const struct intel_crtc_state *crtc_state,
5128 			       const struct drm_connector_state *conn_state)
5129 {
5130 	struct intel_dp *intel_dp =
5131 		enc_to_intel_dp(to_intel_encoder(conn_state->best_encoder));
5132 
5133 	/*
5134 	 * As per DP 1.4a spec section 2.2.4.3 [MSA Field for Indication
5135 	 * of Color Encoding Format and Content Color Gamut], in order to
5136 	 * send YCBCR 420 or HDR BT.2020 signals we should use DP VSC SDP.
5137 	 * Only signal this when the sink advertises VSC SDP colorimetry
5138 	 * support.
5139 	 */
5140 	if (!intel_dp->colorimetry_support)
5141 		return false;
5142 
5143 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
5144 		return true;
5145 
5146 	switch (conn_state->colorspace) {
5147 	case DRM_MODE_COLORIMETRY_SYCC_601:
5148 	case DRM_MODE_COLORIMETRY_OPYCC_601:
5149 	case DRM_MODE_COLORIMETRY_BT2020_YCC:
5150 	case DRM_MODE_COLORIMETRY_BT2020_RGB:
5151 	case DRM_MODE_COLORIMETRY_BT2020_CYCC:
5152 		return true;
5153 	default:
5154 		break;
5155 	}
5156 
5157 	return false;
5158 }
5159 
5160 static ssize_t intel_dp_as_sdp_pack(const struct drm_dp_as_sdp *as_sdp,
5161 				    struct dp_sdp *sdp, size_t size)
5162 {
5163 	size_t length = sizeof(struct dp_sdp);
5164 
5165 	if (size < length)
5166 		return -ENOSPC;
5167 
5168 	memset(sdp, 0, size);
5169 
5170 	/* Prepare AS (Adaptive Sync) SDP Header */
5171 	sdp->sdp_header.HB0 = 0;
5172 	sdp->sdp_header.HB1 = as_sdp->sdp_type;
5173 	sdp->sdp_header.HB2 = as_sdp->revision;
5174 	sdp->sdp_header.HB3 = as_sdp->length;
5175 
5176 	/* Fill AS (Adaptive Sync) SDP Payload */
5177 	sdp->db[0] = as_sdp->mode;
5178 	sdp->db[1] = as_sdp->vtotal & 0xFF;
5179 	sdp->db[2] = (as_sdp->vtotal >> 8) & 0xFF;
5180 	sdp->db[3] = as_sdp->target_rr & 0xFF;
5181 	sdp->db[4] = (as_sdp->target_rr >> 8) & 0x3;
5182 
5183 	if (as_sdp->target_rr_divider)
5184 		sdp->db[4] |= 0x20;
5185 
5186 	sdp->db[7] = as_sdp->coasting_vtotal & 0xFF;
5187 	sdp->db[8] = (as_sdp->coasting_vtotal >> 8) & 0xFF;
5188 
5189 	return length;
5190 }
5191 
5192 static ssize_t
5193 intel_dp_hdr_metadata_infoframe_sdp_pack(struct intel_display *display,
5194 					 const struct hdmi_drm_infoframe *drm_infoframe,
5195 					 struct dp_sdp *sdp,
5196 					 size_t size)
5197 {
5198 	size_t length = sizeof(struct dp_sdp);
5199 	const int infoframe_size = HDMI_INFOFRAME_HEADER_SIZE + HDMI_DRM_INFOFRAME_SIZE;
5200 	unsigned char buf[HDMI_INFOFRAME_HEADER_SIZE + HDMI_DRM_INFOFRAME_SIZE];
5201 	ssize_t len;
5202 
5203 	if (size < length)
5204 		return -ENOSPC;
5205 
5206 	memset(sdp, 0, size);
5207 
5208 	len = hdmi_drm_infoframe_pack_only(drm_infoframe, buf, sizeof(buf));
5209 	if (len < 0) {
5210 		drm_dbg_kms(display->drm,
5211 			    "buffer size is smaller than hdr metadata infoframe\n");
5212 		return -ENOSPC;
5213 	}
5214 
5215 	if (len != infoframe_size) {
5216 		drm_dbg_kms(display->drm, "wrong static hdr metadata size\n");
5217 		return -ENOSPC;
5218 	}
5219 
5220 	/*
5221 	 * Set up the infoframe sdp packet for HDR static metadata.
5222 	 * Prepare VSC Header for SU as per DP 1.4a spec,
5223 	 * Table 2-100 and Table 2-101
5224 	 */
5225 
5226 	/* Secondary-Data Packet ID, 00h for non-Audio INFOFRAME */
5227 	sdp->sdp_header.HB0 = 0;
5228 	/*
5229 	 * Packet Type 80h + Non-audio INFOFRAME Type value
5230 	 * HDMI_INFOFRAME_TYPE_DRM: 0x87
5231 	 * - 80h + Non-audio INFOFRAME Type value
5232 	 * - InfoFrame Type: 0x07
5233 	 *    [CTA-861-G Table-42 Dynamic Range and Mastering InfoFrame]
5234 	 */
5235 	sdp->sdp_header.HB1 = drm_infoframe->type;
5236 	/*
5237 	 * Least Significant Eight Bits of (Data Byte Count – 1)
5238 	 * infoframe_size - 1
5239 	 */
5240 	sdp->sdp_header.HB2 = 0x1D;
5241 	/* INFOFRAME SDP Version Number */
5242 	sdp->sdp_header.HB3 = (0x13 << 2);
5243 	/* CTA Header Byte 2 (INFOFRAME Version Number) */
5244 	sdp->db[0] = drm_infoframe->version;
5245 	/* CTA Header Byte 3 (Length of INFOFRAME): HDMI_DRM_INFOFRAME_SIZE */
5246 	sdp->db[1] = drm_infoframe->length;
5247 	/*
5248 	 * Copy HDMI_DRM_INFOFRAME_SIZE size from a buffer after
5249 	 * HDMI_INFOFRAME_HEADER_SIZE
5250 	 */
5251 	BUILD_BUG_ON(sizeof(sdp->db) < HDMI_DRM_INFOFRAME_SIZE + 2);
5252 	memcpy(&sdp->db[2], &buf[HDMI_INFOFRAME_HEADER_SIZE],
5253 	       HDMI_DRM_INFOFRAME_SIZE);
5254 
5255 	/*
5256 	 * Size of DP infoframe sdp packet for HDR static metadata consists of
5257 	 * - DP SDP Header(struct dp_sdp_header): 4 bytes
5258 	 * - Two Data Blocks: 2 bytes
5259 	 *    CTA Header Byte2 (INFOFRAME Version Number)
5260 	 *    CTA Header Byte3 (Length of INFOFRAME)
5261 	 * - HDMI_DRM_INFOFRAME_SIZE: 26 bytes
5262 	 *
5263 	 * Prior to GEN11's GMP register size is identical to DP HDR static metadata
5264 	 * infoframe size. But GEN11+ has larger than that size, write_infoframe
5265 	 * will pad rest of the size.
5266 	 */
5267 	return sizeof(struct dp_sdp_header) + 2 + HDMI_DRM_INFOFRAME_SIZE;
5268 }
5269 
5270 static void intel_write_dp_sdp(struct intel_encoder *encoder,
5271 			       const struct intel_crtc_state *crtc_state,
5272 			       unsigned int type)
5273 {
5274 	struct intel_display *display = to_intel_display(encoder);
5275 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
5276 	struct dp_sdp sdp = {};
5277 	ssize_t len;
5278 
5279 	if ((crtc_state->infoframes.enable &
5280 	     intel_hdmi_infoframe_enable(type)) == 0)
5281 		return;
5282 
5283 	switch (type) {
5284 	case DP_SDP_VSC:
5285 		len = drm_dp_vsc_sdp_pack(&crtc_state->infoframes.vsc, &sdp);
5286 		break;
5287 	case HDMI_PACKET_TYPE_GAMUT_METADATA:
5288 		len = intel_dp_hdr_metadata_infoframe_sdp_pack(display,
5289 							       &crtc_state->infoframes.drm.drm,
5290 							       &sdp, sizeof(sdp));
5291 		break;
5292 	case DP_SDP_ADAPTIVE_SYNC:
5293 		len = intel_dp_as_sdp_pack(&crtc_state->infoframes.as_sdp, &sdp,
5294 					   sizeof(sdp));
5295 		break;
5296 	default:
5297 		MISSING_CASE(type);
5298 		return;
5299 	}
5300 
5301 	if (drm_WARN_ON(display->drm, len < 0))
5302 		return;
5303 
5304 	dig_port->write_infoframe(encoder, crtc_state, type, &sdp, len);
5305 }
5306 
5307 void intel_dp_set_infoframes(struct intel_encoder *encoder,
5308 			     bool enable,
5309 			     const struct intel_crtc_state *crtc_state,
5310 			     const struct drm_connector_state *conn_state)
5311 {
5312 	struct intel_display *display = to_intel_display(encoder);
5313 	intel_reg_t reg = HSW_TVIDEO_DIP_CTL(display, crtc_state->cpu_transcoder);
5314 	u32 dip_enable = VIDEO_DIP_ENABLE_AVI_HSW | VIDEO_DIP_ENABLE_GCP_HSW |
5315 			 VIDEO_DIP_ENABLE_VS_HSW | VIDEO_DIP_ENABLE_GMP_HSW |
5316 			 VIDEO_DIP_ENABLE_SPD_HSW | VIDEO_DIP_ENABLE_DRM_GLK;
5317 
5318 	if (HAS_AS_SDP(display))
5319 		dip_enable |= VIDEO_DIP_ENABLE_AS_ADL;
5320 
5321 	u32 val = intel_de_read(display, reg) & ~dip_enable;
5322 
5323 	/* TODO: Sanitize DSC enabling wrt. intel_dsc_dp_pps_write(). */
5324 	if (!enable && HAS_DSC(display))
5325 		val &= ~VDIP_ENABLE_PPS;
5326 
5327 	/*
5328 	 * This routine disables VSC DIP if the function is called
5329 	 * to disable SDP or if it does not have PSR
5330 	 */
5331 	if (!enable || !crtc_state->has_psr)
5332 		val &= ~VIDEO_DIP_ENABLE_VSC_HSW;
5333 
5334 	intel_de_write(display, reg, val);
5335 	intel_de_posting_read(display, reg);
5336 
5337 	if (!enable)
5338 		return;
5339 
5340 	intel_write_dp_sdp(encoder, crtc_state, DP_SDP_VSC);
5341 	intel_write_dp_sdp(encoder, crtc_state, DP_SDP_ADAPTIVE_SYNC);
5342 
5343 	intel_write_dp_sdp(encoder, crtc_state, HDMI_PACKET_TYPE_GAMUT_METADATA);
5344 }
5345 
5346 static
5347 int intel_dp_as_sdp_unpack(struct drm_dp_as_sdp *as_sdp,
5348 			   const void *buffer, size_t size)
5349 {
5350 	const struct dp_sdp *sdp = buffer;
5351 
5352 	if (size < sizeof(struct dp_sdp))
5353 		return -EINVAL;
5354 
5355 	memset(as_sdp, 0, sizeof(*as_sdp));
5356 
5357 	if (sdp->sdp_header.HB0 != 0)
5358 		return -EINVAL;
5359 
5360 	if (sdp->sdp_header.HB1 != DP_SDP_ADAPTIVE_SYNC)
5361 		return -EINVAL;
5362 
5363 	if ((sdp->sdp_header.HB3 & 0x3F) != 9)
5364 		return -EINVAL;
5365 
5366 	as_sdp->sdp_type = sdp->sdp_header.HB1;
5367 	as_sdp->length = sdp->sdp_header.HB3 & DP_AS_SDP_LENGTH_MASK;
5368 	as_sdp->revision = sdp->sdp_header.HB2;
5369 	as_sdp->mode = sdp->db[0] & DP_AS_SDP_OPERATION_MODE_MASK;
5370 	as_sdp->vtotal = (sdp->db[2] << 8) | sdp->db[1];
5371 	as_sdp->target_rr = ((sdp->db[4] & 0x3) << 8) | sdp->db[3];
5372 	as_sdp->target_rr_divider = sdp->db[4] & 0x20 ? true : false;
5373 	as_sdp->coasting_vtotal = (sdp->db[8] << 8) | sdp->db[7];
5374 
5375 	return 0;
5376 }
5377 
5378 static int intel_dp_vsc_sdp_unpack(struct drm_dp_vsc_sdp *vsc,
5379 				   const void *buffer, size_t size)
5380 {
5381 	const struct dp_sdp *sdp = buffer;
5382 
5383 	if (size < sizeof(struct dp_sdp))
5384 		return -EINVAL;
5385 
5386 	memset(vsc, 0, sizeof(*vsc));
5387 
5388 	if (sdp->sdp_header.HB0 != 0)
5389 		return -EINVAL;
5390 
5391 	if (sdp->sdp_header.HB1 != DP_SDP_VSC)
5392 		return -EINVAL;
5393 
5394 	vsc->sdp_type = sdp->sdp_header.HB1;
5395 	vsc->revision = sdp->sdp_header.HB2;
5396 	vsc->length = sdp->sdp_header.HB3;
5397 
5398 	if ((sdp->sdp_header.HB2 == 0x2 && sdp->sdp_header.HB3 == 0x8) ||
5399 	    (sdp->sdp_header.HB2 == 0x4 && sdp->sdp_header.HB3 == 0xe) ||
5400 	    (sdp->sdp_header.HB2 == 0x6 && sdp->sdp_header.HB3 == 0x10)) {
5401 		/*
5402 		 * - HB2 = 0x2, HB3 = 0x8
5403 		 *   VSC SDP supporting 3D stereo + PSR
5404 		 * - HB2 = 0x4, HB3 = 0xe
5405 		 *   VSC SDP supporting 3D stereo + PSR2 with Y-coordinate of
5406 		 *   first scan line of the SU region (applies to eDP v1.4b
5407 		 *   and higher).
5408 		 * - HB2 = 0x6, HB3 = 0x10
5409 		 *   VSC SDP supporting 3D stereo + Panel Replay.
5410 		 */
5411 		return 0;
5412 	} else if (sdp->sdp_header.HB2 == 0x5 && sdp->sdp_header.HB3 == 0x13) {
5413 		/*
5414 		 * - HB2 = 0x5, HB3 = 0x13
5415 		 *   VSC SDP supporting 3D stereo + PSR2 + Pixel Encoding/Colorimetry
5416 		 *   Format.
5417 		 */
5418 		vsc->pixelformat = (sdp->db[16] >> 4) & 0xf;
5419 		vsc->colorimetry = sdp->db[16] & 0xf;
5420 		vsc->dynamic_range = (sdp->db[17] >> 7) & 0x1;
5421 
5422 		switch (sdp->db[17] & 0x7) {
5423 		case 0x0:
5424 			vsc->bpc = 6;
5425 			break;
5426 		case 0x1:
5427 			vsc->bpc = 8;
5428 			break;
5429 		case 0x2:
5430 			vsc->bpc = 10;
5431 			break;
5432 		case 0x3:
5433 			vsc->bpc = 12;
5434 			break;
5435 		case 0x4:
5436 			vsc->bpc = 16;
5437 			break;
5438 		default:
5439 			MISSING_CASE(sdp->db[17] & 0x7);
5440 			return -EINVAL;
5441 		}
5442 
5443 		vsc->content_type = sdp->db[18] & 0x7;
5444 	} else {
5445 		return -EINVAL;
5446 	}
5447 
5448 	return 0;
5449 }
5450 
5451 static void
5452 intel_read_dp_as_sdp(struct intel_encoder *encoder,
5453 		     struct intel_crtc_state *crtc_state,
5454 		     struct drm_dp_as_sdp *as_sdp)
5455 {
5456 	struct intel_display *display = to_intel_display(encoder);
5457 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
5458 	unsigned int type = DP_SDP_ADAPTIVE_SYNC;
5459 	struct dp_sdp sdp = {};
5460 	int ret;
5461 
5462 	if ((crtc_state->infoframes.enable &
5463 	     intel_hdmi_infoframe_enable(type)) == 0)
5464 		return;
5465 
5466 	dig_port->read_infoframe(encoder, crtc_state, type, &sdp,
5467 				 sizeof(sdp));
5468 
5469 	ret = intel_dp_as_sdp_unpack(as_sdp, &sdp, sizeof(sdp));
5470 	if (ret)
5471 		drm_dbg_kms(display->drm, "Failed to unpack DP AS SDP\n");
5472 }
5473 
5474 static int
5475 intel_dp_hdr_metadata_infoframe_sdp_unpack(struct hdmi_drm_infoframe *drm_infoframe,
5476 					   const void *buffer, size_t size)
5477 {
5478 	int ret;
5479 
5480 	const struct dp_sdp *sdp = buffer;
5481 
5482 	if (size < sizeof(struct dp_sdp))
5483 		return -EINVAL;
5484 
5485 	if (sdp->sdp_header.HB0 != 0)
5486 		return -EINVAL;
5487 
5488 	if (sdp->sdp_header.HB1 != HDMI_INFOFRAME_TYPE_DRM)
5489 		return -EINVAL;
5490 
5491 	/*
5492 	 * Least Significant Eight Bits of (Data Byte Count – 1)
5493 	 * 1Dh (i.e., Data Byte Count = 30 bytes).
5494 	 */
5495 	if (sdp->sdp_header.HB2 != 0x1D)
5496 		return -EINVAL;
5497 
5498 	/* Most Significant Two Bits of (Data Byte Count – 1), Clear to 00b. */
5499 	if ((sdp->sdp_header.HB3 & 0x3) != 0)
5500 		return -EINVAL;
5501 
5502 	/* INFOFRAME SDP Version Number */
5503 	if (((sdp->sdp_header.HB3 >> 2) & 0x3f) != 0x13)
5504 		return -EINVAL;
5505 
5506 	/* CTA Header Byte 2 (INFOFRAME Version Number) */
5507 	if (sdp->db[0] != 1)
5508 		return -EINVAL;
5509 
5510 	/* CTA Header Byte 3 (Length of INFOFRAME): HDMI_DRM_INFOFRAME_SIZE */
5511 	if (sdp->db[1] != HDMI_DRM_INFOFRAME_SIZE)
5512 		return -EINVAL;
5513 
5514 	ret = hdmi_drm_infoframe_unpack_only(drm_infoframe, &sdp->db[2],
5515 					     HDMI_DRM_INFOFRAME_SIZE);
5516 
5517 	return ret;
5518 }
5519 
5520 static void intel_read_dp_vsc_sdp(struct intel_encoder *encoder,
5521 				  struct intel_crtc_state *crtc_state,
5522 				  struct drm_dp_vsc_sdp *vsc)
5523 {
5524 	struct intel_display *display = to_intel_display(encoder);
5525 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
5526 	unsigned int type = DP_SDP_VSC;
5527 	struct dp_sdp sdp = {};
5528 	int ret;
5529 
5530 	if ((crtc_state->infoframes.enable &
5531 	     intel_hdmi_infoframe_enable(type)) == 0)
5532 		return;
5533 
5534 	dig_port->read_infoframe(encoder, crtc_state, type, &sdp, sizeof(sdp));
5535 
5536 	ret = intel_dp_vsc_sdp_unpack(vsc, &sdp, sizeof(sdp));
5537 
5538 	if (ret)
5539 		drm_dbg_kms(display->drm, "Failed to unpack DP VSC SDP\n");
5540 }
5541 
5542 static void intel_read_dp_hdr_metadata_infoframe_sdp(struct intel_encoder *encoder,
5543 						     struct intel_crtc_state *crtc_state,
5544 						     struct hdmi_drm_infoframe *drm_infoframe)
5545 {
5546 	struct intel_display *display = to_intel_display(encoder);
5547 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
5548 	unsigned int type = HDMI_PACKET_TYPE_GAMUT_METADATA;
5549 	struct dp_sdp sdp = {};
5550 	int ret;
5551 
5552 	if ((crtc_state->infoframes.enable &
5553 	    intel_hdmi_infoframe_enable(type)) == 0)
5554 		return;
5555 
5556 	dig_port->read_infoframe(encoder, crtc_state, type, &sdp,
5557 				 sizeof(sdp));
5558 
5559 	ret = intel_dp_hdr_metadata_infoframe_sdp_unpack(drm_infoframe, &sdp,
5560 							 sizeof(sdp));
5561 
5562 	if (ret)
5563 		drm_dbg_kms(display->drm,
5564 			    "Failed to unpack DP HDR Metadata Infoframe SDP\n");
5565 }
5566 
5567 void intel_read_dp_sdp(struct intel_encoder *encoder,
5568 		       struct intel_crtc_state *crtc_state,
5569 		       unsigned int type)
5570 {
5571 	switch (type) {
5572 	case DP_SDP_VSC:
5573 		intel_read_dp_vsc_sdp(encoder, crtc_state,
5574 				      &crtc_state->infoframes.vsc);
5575 		break;
5576 	case HDMI_PACKET_TYPE_GAMUT_METADATA:
5577 		intel_read_dp_hdr_metadata_infoframe_sdp(encoder, crtc_state,
5578 							 &crtc_state->infoframes.drm.drm);
5579 		break;
5580 	case DP_SDP_ADAPTIVE_SYNC:
5581 		intel_read_dp_as_sdp(encoder, crtc_state,
5582 				     &crtc_state->infoframes.as_sdp);
5583 		break;
5584 	default:
5585 		MISSING_CASE(type);
5586 		break;
5587 	}
5588 }
5589 
5590 static void
5591 intel_dp_mst_hpd_irq(struct intel_dp *intel_dp, u8 *esi, u8 *ack)
5592 {
5593 	bool handled = false;
5594 
5595 	drm_dp_mst_hpd_irq_handle_event(&intel_dp->mst.mgr, esi, ack, &handled);
5596 
5597 	if (esi[1] & DP_CP_IRQ) {
5598 		intel_hdcp_handle_cp_irq(intel_dp->attached_connector);
5599 		ack[1] |= DP_CP_IRQ;
5600 	}
5601 }
5602 
5603 static bool intel_dp_handle_link_service_irq(struct intel_dp *intel_dp, u8 irq_mask);
5604 
5605 /**
5606  * intel_dp_check_mst_status - service any pending MST interrupts, check link status
5607  * @intel_dp: Intel DP struct
5608  *
5609  * Read any pending MST interrupts, call MST core to handle these and ack the
5610  * interrupts. Check if the main and AUX link state is ok.
5611  *
5612  * Returns:
5613  * - %true if pending interrupts were serviced (or no interrupts were
5614  *   pending) w/o detecting an error condition.
5615  * - %false if an error condition - like AUX failure or a loss of link - is
5616  *   detected, or another condition - like a DP tunnel BW state change - needs
5617  *   servicing from the hotplug work.
5618  */
5619 static bool
5620 intel_dp_check_mst_status(struct intel_dp *intel_dp)
5621 {
5622 	struct intel_display *display = to_intel_display(intel_dp);
5623 	bool force_retrain = intel_dp_link_training_get_force_retrain(intel_dp->link.training);
5624 	bool reprobe_needed = false;
5625 	int tries = 33;
5626 
5627 	while (--tries) {
5628 		u8 esi[4] = {};
5629 		u8 ack[4] = {};
5630 		bool new_irqs;
5631 
5632 		if (!intel_dp_get_sink_irq_esi(intel_dp, esi)) {
5633 			drm_dbg_kms(display->drm,
5634 				    "failed to get ESI - device may have failed\n");
5635 			reprobe_needed = true;
5636 
5637 			break;
5638 		}
5639 
5640 		drm_dbg_kms(display->drm, "DPRX ESI: %4ph\n", esi);
5641 
5642 		ack[3] |= esi[3] & INTEL_DP_LINK_SERVICE_IRQ_MASK_MST;
5643 
5644 		intel_dp_mst_hpd_irq(intel_dp, esi, ack);
5645 
5646 		new_irqs = !mem_is_zero(ack, sizeof(ack));
5647 
5648 		drm_WARN_ON(display->drm, ack[1] & ~INTEL_DP_DEVICE_SERVICE_IRQ_MASK_MST);
5649 		drm_WARN_ON(display->drm, ack[3] & ~INTEL_DP_LINK_SERVICE_IRQ_MASK_MST);
5650 
5651 		if (new_irqs && !intel_dp_ack_sink_irq_esi(intel_dp, ack))
5652 			drm_dbg_kms(display->drm, "Failed to ack ESI\n");
5653 
5654 		if (ack[1] & (DP_DOWN_REP_MSG_RDY | DP_UP_REQ_MSG_RDY))
5655 			drm_dp_mst_hpd_irq_send_new_request(&intel_dp->mst.mgr);
5656 
5657 		if (force_retrain) {
5658 			/* Defer forced retraining to the regular link status check. */
5659 			ack[3] |= LINK_STATUS_CHANGED;
5660 			force_retrain = false;
5661 		}
5662 
5663 		if (intel_dp_handle_link_service_irq(intel_dp, ack[3]))
5664 			reprobe_needed = true;
5665 
5666 		if (!new_irqs)
5667 			break;
5668 	}
5669 
5670 	if (!tries) {
5671 		drm_dbg_kms(display->drm, "DPRX ESI not clearing, device may be stuck\n");
5672 		reprobe_needed = true;
5673 	}
5674 
5675 	return !reprobe_needed;
5676 }
5677 
5678 static void
5679 intel_dp_handle_hdmi_link_status_change(struct intel_dp *intel_dp)
5680 {
5681 	bool is_active;
5682 	u8 buf = 0;
5683 
5684 	is_active = drm_dp_pcon_hdmi_link_active(&intel_dp->aux);
5685 	if (intel_dp->frl.is_trained && !is_active) {
5686 		if (drm_dp_dpcd_readb(&intel_dp->aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf) < 0)
5687 			return;
5688 
5689 		buf &=  ~DP_PCON_ENABLE_HDMI_LINK;
5690 		if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_PCON_HDMI_LINK_CONFIG_1, buf) < 0)
5691 			return;
5692 
5693 		drm_dp_pcon_hdmi_frl_link_error_count(&intel_dp->aux, &intel_dp->attached_connector->base);
5694 
5695 		intel_dp->frl.is_trained = false;
5696 
5697 		/* Restart FRL training or fall back to TMDS mode */
5698 		intel_dp_check_frl_training(intel_dp);
5699 	}
5700 }
5701 
5702 bool intel_dp_has_connector(struct intel_dp *intel_dp,
5703 			    const struct drm_connector_state *conn_state)
5704 {
5705 	struct intel_display *display = to_intel_display(intel_dp);
5706 	struct intel_encoder *encoder;
5707 	enum pipe pipe;
5708 
5709 	if (!conn_state->best_encoder)
5710 		return false;
5711 
5712 	/* SST */
5713 	encoder = &dp_to_dig_port(intel_dp)->base;
5714 	if (conn_state->best_encoder == &encoder->base)
5715 		return true;
5716 
5717 	/* MST */
5718 	for_each_pipe(display, pipe) {
5719 		encoder = &intel_dp->mst.stream_encoders[pipe]->base;
5720 		if (conn_state->best_encoder == &encoder->base)
5721 			return true;
5722 	}
5723 
5724 	return false;
5725 }
5726 
5727 static void wait_for_connector_hw_done(const struct drm_connector_state *conn_state)
5728 {
5729 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
5730 	struct intel_display *display = to_intel_display(connector);
5731 
5732 	drm_modeset_lock_assert_held(&display->drm->mode_config.connection_mutex);
5733 
5734 	if (!conn_state->commit)
5735 		return;
5736 
5737 	drm_WARN_ON(display->drm,
5738 		    !wait_for_completion_timeout(&conn_state->commit->hw_done,
5739 						 msecs_to_jiffies(5000)));
5740 }
5741 
5742 int intel_dp_get_active_pipes(struct intel_dp *intel_dp,
5743 			      struct drm_modeset_acquire_ctx *ctx,
5744 			      u8 *pipe_mask)
5745 {
5746 	struct intel_display *display = to_intel_display(intel_dp);
5747 	struct drm_connector_list_iter conn_iter;
5748 	struct intel_connector *connector;
5749 	int ret = 0;
5750 
5751 	*pipe_mask = 0;
5752 
5753 	drm_connector_list_iter_begin(display->drm, &conn_iter);
5754 	for_each_intel_connector_iter(connector, &conn_iter) {
5755 		struct drm_connector_state *conn_state =
5756 			connector->base.state;
5757 		struct intel_crtc_state *crtc_state;
5758 		struct intel_crtc *crtc;
5759 
5760 		if (!intel_dp_has_connector(intel_dp, conn_state))
5761 			continue;
5762 
5763 		crtc = to_intel_crtc(conn_state->crtc);
5764 		if (!crtc)
5765 			continue;
5766 
5767 		ret = drm_modeset_lock(&crtc->base.mutex, ctx);
5768 		if (ret)
5769 			break;
5770 
5771 		crtc_state = to_intel_crtc_state(crtc->base.state);
5772 
5773 		drm_WARN_ON(display->drm,
5774 			    !intel_crtc_has_dp_encoder(crtc_state));
5775 
5776 		if (!crtc_state->hw.active)
5777 			continue;
5778 
5779 		wait_for_connector_hw_done(conn_state);
5780 
5781 		*pipe_mask |= BIT(crtc->pipe);
5782 	}
5783 	drm_connector_list_iter_end(&conn_iter);
5784 
5785 	return ret;
5786 }
5787 
5788 void intel_dp_flush_connector_commits(struct intel_connector *connector)
5789 {
5790 	wait_for_connector_hw_done(connector->base.state);
5791 }
5792 
5793 static void intel_dp_handle_device_service_irq(struct intel_dp *intel_dp, u8 irq_mask)
5794 {
5795 	struct intel_display *display = to_intel_display(intel_dp);
5796 
5797 	drm_WARN_ON(display->drm, irq_mask & ~INTEL_DP_DEVICE_SERVICE_IRQ_MASK_SST);
5798 
5799 	if (irq_mask & DP_AUTOMATED_TEST_REQUEST)
5800 		intel_dp_test_request(intel_dp);
5801 
5802 	if (irq_mask & DP_CP_IRQ)
5803 		intel_hdcp_handle_cp_irq(intel_dp->attached_connector);
5804 
5805 	if (irq_mask & DP_SINK_SPECIFIC_IRQ)
5806 		drm_dbg_kms(display->drm, "Sink specific irq unhandled\n");
5807 }
5808 
5809 
5810 /*
5811  * Return %true if a full connector reprobe is required after handling a link
5812  * service IRQ event.
5813  */
5814 static bool intel_dp_handle_link_service_irq(struct intel_dp *intel_dp, u8 irq_mask)
5815 {
5816 	struct intel_display *display = to_intel_display(intel_dp);
5817 	struct intel_connector *connector = intel_dp->attached_connector;
5818 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
5819 	bool reprobe_needed = false;
5820 
5821 	drm_WARN_ON(display->drm, irq_mask & ~(INTEL_DP_LINK_SERVICE_IRQ_MASK_SST |
5822 					       INTEL_DP_LINK_SERVICE_IRQ_MASK_MST));
5823 
5824 	if (irq_mask & RX_CAP_CHANGED) {
5825 		intel_dp->reset_link_params = true;
5826 		reprobe_needed = true;
5827 	}
5828 
5829 	if (irq_mask & LINK_STATUS_CHANGED)
5830 		intel_dp_check_link_state(intel_dp);
5831 
5832 	if (irq_mask & HDMI_LINK_STATUS_CHANGED)
5833 		intel_dp_handle_hdmi_link_status_change(intel_dp);
5834 
5835 	if (irq_mask & CONNECTED_OFF_ENTRY_REQUESTED)
5836 		drm_dbg_kms(display->drm,
5837 			    "[CONNECTOR:%d:%s][ENCODER:%d:%s] Allowing connected off request\n",
5838 			    connector->base.base.id, connector->base.name,
5839 			    encoder->base.base.id, encoder->base.name);
5840 
5841 	if ((irq_mask & DP_TUNNELING_IRQ) &&
5842 	    drm_dp_tunnel_handle_irq(display->dp_tunnel_mgr,
5843 				     &intel_dp->aux))
5844 		reprobe_needed = true;
5845 
5846 	return reprobe_needed;
5847 }
5848 
5849 /*
5850  * According to DP spec
5851  * 5.1.2:
5852  *  1. Read DPCD
5853  *  2. Configure link according to Receiver Capabilities
5854  *  3. Use Link Training from 2.5.3.3 and 3.5.1.3
5855  *  4. Check link status on receipt of hot-plug interrupt
5856  *
5857  * intel_dp_short_pulse -  handles short pulse interrupts
5858  * when full detection is not required.
5859  * Returns %true if short pulse is handled and full detection
5860  * is NOT required and %false otherwise.
5861  */
5862 static bool
5863 intel_dp_short_pulse(struct intel_dp *intel_dp)
5864 {
5865 	bool reprobe_needed = false;
5866 	u8 esi[4] = {};
5867 
5868 	intel_dp_test_reset(intel_dp);
5869 
5870 	if (!intel_dp_get_and_ack_sink_irq_esi_sst(intel_dp, esi))
5871 		return false;
5872 
5873 	/*
5874 	 * If the current value of sink count doesn't match with
5875 	 * the value that was stored earlier we need to do full
5876 	 * detection.
5877 	 */
5878 	if (intel_dp_has_sink_count(intel_dp) &&
5879 	    DP_GET_SINK_COUNT(esi[0]) != intel_dp->sink_count)
5880 		/* No need to proceed if we are going to do full detect */
5881 		return false;
5882 
5883 	intel_dp_handle_device_service_irq(intel_dp, esi[1]);
5884 
5885 	/*
5886 	 * Force checking the link status for DPCD_REV < 1.2
5887 	 * TODO: let the link status check depend on LINK_STATUS_CHANGED
5888 	 * or intel_dp->link.training.force_retrain for DPCD_REV >= 1.2
5889 	 */
5890 	esi[3] |= LINK_STATUS_CHANGED;
5891 	if (intel_dp_handle_link_service_irq(intel_dp, esi[3]))
5892 		reprobe_needed = true;
5893 
5894 	/* Handle CEC interrupts, if any */
5895 	drm_dp_cec_irq(&intel_dp->aux);
5896 
5897 	if (READ_ONCE(intel_dp->downstream_port_changed)) {
5898 		WRITE_ONCE(intel_dp->downstream_port_changed, false);
5899 		reprobe_needed = true;
5900 	}
5901 
5902 	intel_psr_short_pulse(intel_dp);
5903 
5904 	if (intel_alpm_get_error(intel_dp)) {
5905 		intel_alpm_disable(intel_dp);
5906 		intel_dp->alpm.sink_alpm_error = true;
5907 	}
5908 
5909 	if (intel_dp_test_short_pulse(intel_dp))
5910 		reprobe_needed = true;
5911 
5912 	return !reprobe_needed;
5913 }
5914 
5915 /* XXX this is probably wrong for multiple downstream ports */
5916 static enum drm_connector_status
5917 intel_dp_detect_dpcd(struct intel_dp *intel_dp)
5918 {
5919 	struct intel_display *display = to_intel_display(intel_dp);
5920 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
5921 	u8 *dpcd = intel_dp->dpcd;
5922 	u8 type;
5923 
5924 	if (drm_WARN_ON(display->drm, intel_dp_is_edp(intel_dp)))
5925 		return connector_status_connected;
5926 
5927 	WRITE_ONCE(intel_dp->downstream_port_changed, false);
5928 
5929 	intel_lspcon_resume(dig_port);
5930 
5931 	if (!intel_dp_get_dpcd(intel_dp))
5932 		return connector_status_disconnected;
5933 
5934 	intel_dp->mst_detect = intel_dp_mst_detect(intel_dp);
5935 
5936 	/* if there's no downstream port, we're done */
5937 	if (!drm_dp_is_branch(dpcd))
5938 		return connector_status_connected;
5939 
5940 	/* If we're HPD-aware, SINK_COUNT changes dynamically */
5941 	if (intel_dp_has_sink_count(intel_dp) &&
5942 	    intel_dp->downstream_ports[0] & DP_DS_PORT_HPD) {
5943 		return intel_dp->sink_count ?
5944 		connector_status_connected : connector_status_disconnected;
5945 	}
5946 
5947 	if (intel_dp->mst_detect == DRM_DP_MST)
5948 		return connector_status_connected;
5949 
5950 	/* If no HPD, poke DDC gently */
5951 	if (drm_probe_ddc(&intel_dp->aux.ddc))
5952 		return connector_status_connected;
5953 
5954 	/* Well we tried, say unknown for unreliable port types */
5955 	if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11) {
5956 		type = intel_dp->downstream_ports[0] & DP_DS_PORT_TYPE_MASK;
5957 		if (type == DP_DS_PORT_TYPE_VGA ||
5958 		    type == DP_DS_PORT_TYPE_NON_EDID)
5959 			return connector_status_unknown;
5960 	} else {
5961 		type = intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
5962 			DP_DWN_STRM_PORT_TYPE_MASK;
5963 		if (type == DP_DWN_STRM_PORT_TYPE_ANALOG ||
5964 		    type == DP_DWN_STRM_PORT_TYPE_OTHER)
5965 			return connector_status_unknown;
5966 	}
5967 
5968 	/* Anything else is out of spec, warn and ignore */
5969 	drm_dbg_kms(display->drm, "Broken DP branch device, ignoring\n");
5970 	return connector_status_disconnected;
5971 }
5972 
5973 static enum drm_connector_status
5974 edp_detect(struct intel_dp *intel_dp)
5975 {
5976 	return connector_status_connected;
5977 }
5978 
5979 void intel_digital_port_lock(struct intel_encoder *encoder)
5980 {
5981 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
5982 
5983 	if (dig_port->lock)
5984 		dig_port->lock(dig_port);
5985 }
5986 
5987 void intel_digital_port_unlock(struct intel_encoder *encoder)
5988 {
5989 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
5990 
5991 	if (dig_port->unlock)
5992 		dig_port->unlock(dig_port);
5993 }
5994 
5995 /*
5996  * intel_digital_port_connected_locked - is the specified port connected?
5997  * @encoder: intel_encoder
5998  *
5999  * In cases where there's a connector physically connected but it can't be used
6000  * by our hardware we also return false, since the rest of the driver should
6001  * pretty much treat the port as disconnected. This is relevant for type-C
6002  * (starting on ICL) where there's ownership involved.
6003  *
6004  * The caller must hold the lock acquired by calling intel_digital_port_lock()
6005  * when calling this function.
6006  *
6007  * Return %true if port is connected, %false otherwise.
6008  */
6009 bool intel_digital_port_connected_locked(struct intel_encoder *encoder)
6010 {
6011 	struct intel_display *display = to_intel_display(encoder);
6012 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
6013 	bool is_glitch_free = intel_tc_port_handles_hpd_glitches(dig_port);
6014 	bool is_connected = false;
6015 
6016 	with_intel_display_power(display, POWER_DOMAIN_DISPLAY_CORE) {
6017 		poll_timeout_us(is_connected = dig_port->connected(encoder),
6018 				is_connected || is_glitch_free,
6019 				30, 4000, false);
6020 	}
6021 
6022 	return is_connected;
6023 }
6024 
6025 bool intel_digital_port_connected(struct intel_encoder *encoder)
6026 {
6027 	bool ret;
6028 
6029 	intel_digital_port_lock(encoder);
6030 	ret = intel_digital_port_connected_locked(encoder);
6031 	intel_digital_port_unlock(encoder);
6032 
6033 	return ret;
6034 }
6035 
6036 static const struct drm_edid *
6037 intel_dp_get_edid(struct intel_dp *intel_dp)
6038 {
6039 	struct intel_connector *connector = intel_dp->attached_connector;
6040 	const struct drm_edid *fixed_edid = connector->panel.fixed_edid;
6041 
6042 	/* Use panel fixed edid if we have one */
6043 	if (fixed_edid) {
6044 		/* invalid edid */
6045 		if (IS_ERR(fixed_edid))
6046 			return NULL;
6047 
6048 		return drm_edid_dup(fixed_edid);
6049 	}
6050 
6051 	return drm_edid_read_ddc(&connector->base, &intel_dp->aux.ddc);
6052 }
6053 
6054 static void
6055 intel_dp_update_dfp(struct intel_dp *intel_dp,
6056 		    const struct drm_edid *drm_edid)
6057 {
6058 	struct intel_display *display = to_intel_display(intel_dp);
6059 	struct intel_connector *connector = intel_dp->attached_connector;
6060 
6061 	intel_dp->dfp.max_bpc =
6062 		drm_dp_downstream_max_bpc(intel_dp->dpcd,
6063 					  intel_dp->downstream_ports, drm_edid);
6064 
6065 	intel_dp->dfp.max_dotclock =
6066 		drm_dp_downstream_max_dotclock(intel_dp->dpcd,
6067 					       intel_dp->downstream_ports);
6068 
6069 	intel_dp->dfp.min_tmds_clock =
6070 		drm_dp_downstream_min_tmds_clock(intel_dp->dpcd,
6071 						 intel_dp->downstream_ports,
6072 						 drm_edid);
6073 	intel_dp->dfp.max_tmds_clock =
6074 		drm_dp_downstream_max_tmds_clock(intel_dp->dpcd,
6075 						 intel_dp->downstream_ports,
6076 						 drm_edid);
6077 
6078 	intel_dp->dfp.pcon_max_frl_bw =
6079 		drm_dp_get_pcon_max_frl_bw(intel_dp->dpcd,
6080 					   intel_dp->downstream_ports);
6081 
6082 	drm_dbg_kms(display->drm,
6083 		    "[CONNECTOR:%d:%s] DFP max bpc %d, max dotclock %d, TMDS clock %d-%d, PCON Max FRL BW %dGbps\n",
6084 		    connector->base.base.id, connector->base.name,
6085 		    intel_dp->dfp.max_bpc,
6086 		    intel_dp->dfp.max_dotclock,
6087 		    intel_dp->dfp.min_tmds_clock,
6088 		    intel_dp->dfp.max_tmds_clock,
6089 		    intel_dp->dfp.pcon_max_frl_bw);
6090 
6091 	intel_dp_get_pcon_dsc_cap(intel_dp);
6092 }
6093 
6094 static bool
6095 intel_dp_can_ycbcr420(struct intel_dp *intel_dp)
6096 {
6097 	if (source_can_output(intel_dp, INTEL_OUTPUT_FORMAT_YCBCR420) &&
6098 	    (!drm_dp_is_branch(intel_dp->dpcd) || intel_dp->dfp.ycbcr420_passthrough))
6099 		return true;
6100 
6101 	if (source_can_output(intel_dp, INTEL_OUTPUT_FORMAT_RGB) &&
6102 	    dfp_can_convert_from_rgb(intel_dp, INTEL_OUTPUT_FORMAT_YCBCR420))
6103 		return true;
6104 
6105 	if (source_can_output(intel_dp, INTEL_OUTPUT_FORMAT_YCBCR444) &&
6106 	    dfp_can_convert_from_ycbcr444(intel_dp, INTEL_OUTPUT_FORMAT_YCBCR420))
6107 		return true;
6108 
6109 	return false;
6110 }
6111 
6112 static void
6113 intel_dp_update_420(struct intel_dp *intel_dp)
6114 {
6115 	struct intel_display *display = to_intel_display(intel_dp);
6116 	struct intel_connector *connector = intel_dp->attached_connector;
6117 
6118 	intel_dp->dfp.ycbcr420_passthrough =
6119 		drm_dp_downstream_420_passthrough(intel_dp->dpcd,
6120 						  intel_dp->downstream_ports);
6121 	/* on-board LSPCON always assumed to support 4:4:4->4:2:0 conversion */
6122 	intel_dp->dfp.ycbcr_444_to_420 =
6123 		intel_lspcon_active(dp_to_dig_port(intel_dp)) ||
6124 		drm_dp_downstream_444_to_420_conversion(intel_dp->dpcd,
6125 							intel_dp->downstream_ports);
6126 	intel_dp->dfp.rgb_to_ycbcr =
6127 		drm_dp_downstream_rgb_to_ycbcr_conversion(intel_dp->dpcd,
6128 							  intel_dp->downstream_ports,
6129 							  DP_DS_HDMI_BT709_RGB_YCBCR_CONV);
6130 
6131 	connector->base.ycbcr_420_allowed = intel_dp_can_ycbcr420(intel_dp);
6132 
6133 	drm_dbg_kms(display->drm,
6134 		    "[CONNECTOR:%d:%s] RGB->YcbCr conversion? %s, YCbCr 4:2:0 allowed? %s, YCbCr 4:4:4->4:2:0 conversion? %s\n",
6135 		    connector->base.base.id, connector->base.name,
6136 		    str_yes_no(intel_dp->dfp.rgb_to_ycbcr),
6137 		    str_yes_no(connector->base.ycbcr_420_allowed),
6138 		    str_yes_no(intel_dp->dfp.ycbcr_444_to_420));
6139 }
6140 
6141 static void
6142 intel_dp_set_edid(struct intel_dp *intel_dp)
6143 {
6144 	struct intel_display *display = to_intel_display(intel_dp);
6145 	struct intel_connector *connector = intel_dp->attached_connector;
6146 	const struct drm_edid *drm_edid;
6147 	bool vrr_capable;
6148 
6149 	intel_dp_unset_edid(intel_dp);
6150 	drm_edid = intel_dp_get_edid(intel_dp);
6151 	connector->detect_edid = drm_edid;
6152 
6153 	/* Below we depend on display info having been updated */
6154 	drm_edid_connector_update(&connector->base, drm_edid);
6155 
6156 	vrr_capable = intel_vrr_is_capable(connector);
6157 	drm_dbg_kms(display->drm, "[CONNECTOR:%d:%s] VRR capable: %s\n",
6158 		    connector->base.base.id, connector->base.name, str_yes_no(vrr_capable));
6159 	drm_connector_set_vrr_capable_property(&connector->base, vrr_capable);
6160 
6161 	intel_dp_update_dfp(intel_dp, drm_edid);
6162 	intel_dp_update_420(intel_dp);
6163 
6164 	drm_dp_cec_attach(&intel_dp->aux,
6165 			  connector->base.display_info.source_physical_address);
6166 }
6167 
6168 static void
6169 intel_dp_unset_edid(struct intel_dp *intel_dp)
6170 {
6171 	struct intel_connector *connector = intel_dp->attached_connector;
6172 
6173 	drm_dp_cec_unset_edid(&intel_dp->aux);
6174 	drm_edid_free(connector->detect_edid);
6175 	connector->detect_edid = NULL;
6176 
6177 	intel_dp->dfp.max_bpc = 0;
6178 	intel_dp->dfp.max_dotclock = 0;
6179 	intel_dp->dfp.min_tmds_clock = 0;
6180 	intel_dp->dfp.max_tmds_clock = 0;
6181 
6182 	intel_dp->dfp.pcon_max_frl_bw = 0;
6183 
6184 	intel_dp->dfp.ycbcr_444_to_420 = false;
6185 	connector->base.ycbcr_420_allowed = false;
6186 
6187 	drm_connector_set_vrr_capable_property(&connector->base,
6188 					       false);
6189 }
6190 
6191 static bool
6192 intel_dp_sink_supports_as_sdp_v2(struct intel_dp *intel_dp)
6193 {
6194 	u8 rx_features;
6195 
6196 	/*
6197 	 * The DP spec does not explicitly provide the AS SDP v2 capability.
6198 	 * So based on the DP v2.1 SCR, we infer it from the following bits:
6199 	 *
6200 	 * DP_AS_SDP_FAVT_PAYLOAD_FIELDS_PARSING_SUPPORTED indicates support for
6201 	 * FAVT, which is explicitly defined to use AS SDP v2.
6202 	 *
6203 	 * DP_ASYNC_VIDEO_TIMING_NOT_SUPPORTED_IN_PR indicates that the sink
6204 	 * does not support asynchronous video timing while in PR Active,
6205 	 * requiring the source to keep transmitting Adaptive-Sync SDPs. The
6206 	 * spec mandates that such sinks shall support AS SDP v2.
6207 	 *
6208 	 * #TODO: Check the Adaptive-Sync DisplayID 2.1 block once DisplayID
6209 	 * parsing is available. This may help detect AS SDP v2 support for
6210 	 * native DP 2.1 sinks that do not expose FAVT or PR-based capability
6211 	 * bits.
6212 	 *
6213 	 * In the presence of PCONs, check PCON support from DPCD and sink
6214 	 * support from Display ID.
6215 	 */
6216 
6217 	if (drm_dp_dpcd_read_byte(&intel_dp->aux,
6218 				  DP_DPRX_FEATURE_ENUMERATION_LIST_CONT_1,
6219 				  &rx_features) == 1) {
6220 		if (rx_features & DP_AS_SDP_FAVT_PAYLOAD_FIELDS_PARSING_SUPPORTED)
6221 			return true;
6222 	}
6223 
6224 	if (intel_dp->psr.sink_panel_replay_support &&
6225 	    !intel_psr_pr_async_video_timing_supported(intel_dp))
6226 		return true;
6227 
6228 	return false;
6229 }
6230 
6231 static void
6232 intel_dp_detect_sdp_caps(struct intel_dp *intel_dp)
6233 {
6234 	struct intel_display *display = to_intel_display(intel_dp);
6235 
6236 	intel_dp->as_sdp_supported = HAS_AS_SDP(display) &&
6237 		drm_dp_as_sdp_supported(&intel_dp->aux, intel_dp->dpcd);
6238 
6239 	if (!intel_dp->as_sdp_supported)
6240 		return;
6241 
6242 	/* eDP Adaptive-Sync SDP always uses AS SDP v2 */
6243 	if (intel_dp_is_edp(intel_dp))
6244 		intel_dp->as_sdp_v2_supported =  true;
6245 	else
6246 		intel_dp->as_sdp_v2_supported = intel_dp_sink_supports_as_sdp_v2(intel_dp);
6247 }
6248 
6249 static bool intel_dp_needs_dpcd_probe(struct intel_dp *intel_dp, bool force_on_external)
6250 {
6251 	struct intel_connector *connector = intel_dp->attached_connector;
6252 
6253 	if (intel_dp_is_edp(intel_dp))
6254 		return false;
6255 
6256 	if (force_on_external)
6257 		return true;
6258 
6259 	if (intel_dp->is_mst)
6260 		return false;
6261 
6262 	return drm_edid_has_quirk(&connector->base, DRM_EDID_QUIRK_DP_DPCD_PROBE);
6263 }
6264 
6265 void intel_dp_dpcd_set_probe(struct intel_dp *intel_dp, bool force_on_external)
6266 {
6267 	drm_dp_dpcd_set_probe(&intel_dp->aux,
6268 			      intel_dp_needs_dpcd_probe(intel_dp, force_on_external));
6269 }
6270 
6271 static int
6272 intel_dp_detect(struct drm_connector *_connector,
6273 		struct drm_modeset_acquire_ctx *ctx,
6274 		bool force)
6275 {
6276 	struct intel_display *display = to_intel_display(_connector->dev);
6277 	struct intel_connector *connector = to_intel_connector(_connector);
6278 	struct intel_dp *intel_dp = intel_attached_dp(connector);
6279 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
6280 	struct intel_encoder *encoder = &dig_port->base;
6281 	enum drm_connector_status status;
6282 	int ret;
6283 
6284 	drm_dbg_kms(display->drm, "[CONNECTOR:%d:%s]\n",
6285 		    connector->base.base.id, connector->base.name);
6286 	drm_WARN_ON(display->drm,
6287 		    !drm_modeset_is_locked(&display->drm->mode_config.connection_mutex));
6288 
6289 	if (!intel_display_device_enabled(display))
6290 		return connector_status_disconnected;
6291 
6292 	if (!intel_display_driver_check_access(display))
6293 		return connector->base.status;
6294 
6295 	intel_dp_flush_connector_commits(connector);
6296 
6297 	intel_pps_vdd_on(intel_dp);
6298 
6299 	/* Can't disconnect eDP */
6300 	if (intel_dp_is_edp(intel_dp))
6301 		status = edp_detect(intel_dp);
6302 	else if (intel_digital_port_connected(encoder))
6303 		status = intel_dp_detect_dpcd(intel_dp);
6304 	else
6305 		status = connector_status_disconnected;
6306 
6307 	if (status != connector_status_disconnected &&
6308 	    !intel_dp_mst_verify_dpcd_state(intel_dp))
6309 		/*
6310 		 * This requires retrying detection for instance to re-enable
6311 		 * the MST mode that got reset via a long HPD pulse. The retry
6312 		 * will happen either via the hotplug handler's retry logic,
6313 		 * ensured by setting the connector here to SST/disconnected,
6314 		 * or via a userspace connector probing in response to the
6315 		 * hotplug uevent sent when removing the MST connectors.
6316 		 */
6317 		status = connector_status_disconnected;
6318 
6319 	if (status == connector_status_disconnected) {
6320 		intel_dp_test_reset(intel_dp);
6321 		/*
6322 		 * FIXME: Resetting these caps here cause
6323 		 * state computation fail if the connector need to be
6324 		 * modeset after sink disconnect. Move resetting them
6325 		 * to where new sink is connected.
6326 		 */
6327 		memset(connector->dp.dsc_dpcd, 0, sizeof(connector->dp.dsc_dpcd));
6328 		memset(connector->dp.panel_replay_caps.dpcd, 0,
6329 		       sizeof(connector->dp.panel_replay_caps.dpcd));
6330 		intel_dp->psr.sink_panel_replay_support = false;
6331 		connector->dp.panel_replay_caps.support = false;
6332 		connector->dp.panel_replay_caps.su_support = false;
6333 		connector->dp.panel_replay_caps.dsc_support =
6334 			INTEL_DP_PANEL_REPLAY_DSC_NOT_SUPPORTED;
6335 
6336 		intel_dp_mst_disconnect(intel_dp);
6337 
6338 		intel_dp_tunnel_disconnect(intel_dp);
6339 
6340 		intel_dp_tunnel_uhbr_lanes_wa_reset(intel_dp);
6341 
6342 		goto out_unset_edid;
6343 	}
6344 
6345 	intel_dp_init_source_oui(intel_dp);
6346 
6347 	ret = intel_dp_tunnel_detect(intel_dp, ctx);
6348 	if (ret == -EDEADLK) {
6349 		status = ret;
6350 
6351 		goto out_vdd_off;
6352 	}
6353 
6354 	if (ret == 1)
6355 		connector->base.epoch_counter++;
6356 
6357 	if (!intel_dp_is_edp(intel_dp))
6358 		intel_psr_init_dpcd(intel_dp, connector);
6359 
6360 	intel_dp_detect_dsc_caps(intel_dp, connector);
6361 
6362 	intel_dp_detect_sdp_caps(intel_dp);
6363 
6364 	if (intel_dp->reset_link_params) {
6365 		intel_dp_reset_link_params(intel_dp);
6366 		intel_dp->reset_link_params = false;
6367 	}
6368 
6369 	intel_dp_mst_configure(intel_dp);
6370 
6371 	intel_dp_print_rates(intel_dp);
6372 
6373 	if (intel_dp->is_mst) {
6374 		/*
6375 		 * If we are in MST mode then this connector
6376 		 * won't appear connected or have anything
6377 		 * with EDID on it
6378 		 */
6379 		status = connector_status_disconnected;
6380 		goto out_unset_edid;
6381 	}
6382 
6383 	/*
6384 	 * Some external monitors do not signal loss of link synchronization
6385 	 * with an IRQ_HPD, so force a link status check.
6386 	 *
6387 	 * TODO: this probably became redundant, so remove it: the link state
6388 	 * is rechecked/recovered now after modesets, where the loss of
6389 	 * synchronization tends to occur.
6390 	 */
6391 	if (!intel_dp_is_edp(intel_dp))
6392 		intel_dp_check_link_state(intel_dp);
6393 
6394 	/*
6395 	 * Clearing NACK and defer counts to get their exact values
6396 	 * while reading EDID which are required by Compliance tests
6397 	 * 4.2.2.4 and 4.2.2.5
6398 	 */
6399 	intel_dp->aux.i2c_nack_count = 0;
6400 	intel_dp->aux.i2c_defer_count = 0;
6401 
6402 	intel_dp_set_edid(intel_dp);
6403 	if (intel_dp_is_edp(intel_dp) || connector->detect_edid)
6404 		status = connector_status_connected;
6405 
6406 	if (intel_dp_tunnel_uhbr_lanes_wa_setup(intel_dp))
6407 		intel_dp_tunnel_uhbr_lanes_wa_apply(intel_dp);
6408 
6409 out_unset_edid:
6410 	if (status != connector_status_connected && !intel_dp->is_mst)
6411 		intel_dp_unset_edid(intel_dp);
6412 
6413 	intel_dp_dpcd_set_probe(intel_dp, false);
6414 
6415 	if (!intel_dp_is_edp(intel_dp))
6416 		drm_dp_set_subconnector_property(&connector->base,
6417 						 status,
6418 						 intel_dp->dpcd,
6419 						 intel_dp->downstream_ports);
6420 out_vdd_off:
6421 	intel_pps_vdd_off(intel_dp);
6422 
6423 	return status;
6424 }
6425 
6426 static void
6427 intel_dp_force(struct drm_connector *_connector)
6428 {
6429 	struct intel_connector *connector = to_intel_connector(_connector);
6430 	struct intel_display *display = to_intel_display(connector);
6431 	struct intel_dp *intel_dp = intel_attached_dp(connector);
6432 
6433 	drm_dbg_kms(display->drm, "[CONNECTOR:%d:%s]\n",
6434 		    connector->base.base.id, connector->base.name);
6435 
6436 	if (!intel_display_driver_check_access(display))
6437 		return;
6438 
6439 	intel_dp_unset_edid(intel_dp);
6440 
6441 	if (connector->base.status != connector_status_connected)
6442 		return;
6443 
6444 	intel_dp_set_edid(intel_dp);
6445 
6446 	intel_dp_dpcd_set_probe(intel_dp, false);
6447 }
6448 
6449 static int intel_dp_get_modes(struct drm_connector *_connector)
6450 {
6451 	struct intel_display *display = to_intel_display(_connector->dev);
6452 	struct intel_connector *connector = to_intel_connector(_connector);
6453 	struct intel_dp *intel_dp = intel_attached_dp(connector);
6454 	int num_modes;
6455 
6456 	/* drm_edid_connector_update() done in ->detect() or ->force() */
6457 	num_modes = drm_edid_connector_add_modes(&connector->base);
6458 
6459 	/* Also add fixed mode, which may or may not be present in EDID */
6460 	if (intel_dp_is_edp(intel_dp))
6461 		num_modes += intel_panel_get_modes(connector);
6462 
6463 	if (num_modes)
6464 		return num_modes;
6465 
6466 	if (!connector->detect_edid) {
6467 		struct drm_display_mode *mode;
6468 
6469 		mode = drm_dp_downstream_mode(display->drm,
6470 					      intel_dp->dpcd,
6471 					      intel_dp->downstream_ports);
6472 		if (mode) {
6473 			drm_mode_probed_add(&connector->base, mode);
6474 			num_modes++;
6475 		}
6476 	}
6477 
6478 	return num_modes;
6479 }
6480 
6481 static int
6482 intel_dp_connector_register(struct drm_connector *_connector)
6483 {
6484 	struct intel_connector *connector = to_intel_connector(_connector);
6485 	struct intel_display *display = to_intel_display(connector);
6486 	struct intel_dp *intel_dp = intel_attached_dp(connector);
6487 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
6488 	int ret;
6489 
6490 	ret = intel_connector_register(&connector->base);
6491 	if (ret)
6492 		return ret;
6493 
6494 	drm_dbg_kms(display->drm, "registering %s bus for %s\n",
6495 		    intel_dp->aux.name, connector->base.kdev->kobj.name);
6496 
6497 	intel_dp->aux.dev = connector->base.kdev;
6498 	ret = drm_dp_aux_register(&intel_dp->aux);
6499 	if (!ret)
6500 		drm_dp_cec_register_connector(&intel_dp->aux, &connector->base);
6501 
6502 	if (!intel_bios_encoder_is_lspcon(dig_port->base.devdata))
6503 		return ret;
6504 
6505 	/*
6506 	 * ToDo: Clean this up to handle lspcon init and resume more
6507 	 * efficiently and streamlined.
6508 	 */
6509 	if (intel_lspcon_init(dig_port)) {
6510 		if (intel_lspcon_detect_hdr_capability(dig_port))
6511 			drm_connector_attach_hdr_output_metadata_property(&connector->base);
6512 	}
6513 
6514 	return ret;
6515 }
6516 
6517 static void
6518 intel_dp_connector_unregister(struct drm_connector *_connector)
6519 {
6520 	struct intel_connector *connector = to_intel_connector(_connector);
6521 	struct intel_dp *intel_dp = intel_attached_dp(connector);
6522 
6523 	drm_dp_cec_unregister_connector(&intel_dp->aux);
6524 	drm_dp_aux_unregister(&intel_dp->aux);
6525 	intel_connector_unregister(&connector->base);
6526 }
6527 
6528 void intel_dp_connector_sync_state(struct intel_connector *connector,
6529 				   const struct intel_crtc_state *crtc_state)
6530 {
6531 	struct intel_display *display = to_intel_display(connector);
6532 
6533 	if (crtc_state && crtc_state->dsc.compression_enable) {
6534 		drm_WARN_ON(display->drm,
6535 			    !connector->dp.dsc_decompression_aux);
6536 		connector->dp.dsc_decompression_enabled = true;
6537 	} else {
6538 		connector->dp.dsc_decompression_enabled = false;
6539 	}
6540 }
6541 
6542 void intel_dp_encoder_flush_work(struct drm_encoder *_encoder)
6543 {
6544 	struct intel_encoder *encoder = to_intel_encoder(_encoder);
6545 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
6546 	struct intel_dp *intel_dp = &dig_port->dp;
6547 
6548 	intel_encoder_link_check_flush_work(encoder);
6549 
6550 	intel_dp_mst_encoder_cleanup(dig_port);
6551 
6552 	intel_dp_tunnel_destroy(intel_dp);
6553 
6554 	intel_pps_vdd_off_sync(intel_dp);
6555 
6556 	/*
6557 	 * Ensure power off delay is respected on module remove, so that we can
6558 	 * reduce delays at driver probe. See pps_init_timestamps().
6559 	 */
6560 	intel_pps_wait_power_cycle(intel_dp);
6561 
6562 	intel_dp_aux_fini(intel_dp);
6563 }
6564 
6565 void intel_dp_encoder_suspend(struct intel_encoder *encoder)
6566 {
6567 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
6568 
6569 	intel_pps_vdd_off_sync(intel_dp);
6570 
6571 	intel_dp_tunnel_suspend(intel_dp);
6572 }
6573 
6574 void intel_dp_encoder_shutdown(struct intel_encoder *encoder)
6575 {
6576 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
6577 
6578 	intel_pps_wait_power_cycle(intel_dp);
6579 }
6580 
6581 static int intel_modeset_tile_group(struct intel_atomic_state *state,
6582 				    int tile_group_id)
6583 {
6584 	struct intel_display *display = to_intel_display(state);
6585 	struct drm_connector_list_iter conn_iter;
6586 	struct intel_connector *connector;
6587 	int ret = 0;
6588 
6589 	drm_connector_list_iter_begin(display->drm, &conn_iter);
6590 	for_each_intel_connector_iter(connector, &conn_iter) {
6591 		struct drm_connector_state *conn_state;
6592 		struct intel_crtc_state *crtc_state;
6593 		struct intel_crtc *crtc;
6594 
6595 		if (!connector->base.has_tile ||
6596 		    connector->base.tile_group->id != tile_group_id)
6597 			continue;
6598 
6599 		conn_state = drm_atomic_get_connector_state(&state->base,
6600 							    &connector->base);
6601 		if (IS_ERR(conn_state)) {
6602 			ret = PTR_ERR(conn_state);
6603 			break;
6604 		}
6605 
6606 		crtc = to_intel_crtc(conn_state->crtc);
6607 
6608 		if (!crtc)
6609 			continue;
6610 
6611 		crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
6612 		crtc_state->uapi.mode_changed = true;
6613 
6614 		ret = drm_atomic_add_affected_planes(&state->base, &crtc->base);
6615 		if (ret)
6616 			break;
6617 	}
6618 	drm_connector_list_iter_end(&conn_iter);
6619 
6620 	return ret;
6621 }
6622 
6623 static int intel_modeset_affected_transcoders(struct intel_atomic_state *state, u16 transcoders)
6624 {
6625 	struct intel_display *display = to_intel_display(state);
6626 	struct intel_crtc *crtc;
6627 
6628 	if (transcoders == 0)
6629 		return 0;
6630 
6631 	for_each_intel_crtc(display, crtc) {
6632 		struct intel_crtc_state *crtc_state;
6633 		int ret;
6634 
6635 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
6636 		if (IS_ERR(crtc_state))
6637 			return PTR_ERR(crtc_state);
6638 
6639 		if (!crtc_state->hw.enable)
6640 			continue;
6641 
6642 		if (!(transcoders & BIT(crtc_state->cpu_transcoder)))
6643 			continue;
6644 
6645 		crtc_state->uapi.mode_changed = true;
6646 
6647 		ret = drm_atomic_add_affected_connectors(&state->base, &crtc->base);
6648 		if (ret)
6649 			return ret;
6650 
6651 		ret = drm_atomic_add_affected_planes(&state->base, &crtc->base);
6652 		if (ret)
6653 			return ret;
6654 
6655 		transcoders &= ~BIT(crtc_state->cpu_transcoder);
6656 	}
6657 
6658 	drm_WARN_ON(display->drm, transcoders != 0);
6659 
6660 	return 0;
6661 }
6662 
6663 static int intel_modeset_synced_crtcs(struct intel_atomic_state *state,
6664 				      struct drm_connector *_connector)
6665 {
6666 	struct intel_connector *connector = to_intel_connector(_connector);
6667 	const struct drm_connector_state *old_conn_state =
6668 		drm_atomic_get_old_connector_state(&state->base, &connector->base);
6669 	const struct intel_crtc_state *old_crtc_state;
6670 	struct intel_crtc *crtc;
6671 	u16 transcoders;
6672 
6673 	crtc = to_intel_crtc(old_conn_state->crtc);
6674 	if (!crtc)
6675 		return 0;
6676 
6677 	old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc);
6678 
6679 	if (!old_crtc_state->hw.active)
6680 		return 0;
6681 
6682 	transcoders = old_crtc_state->sync_mode_slaves_mask;
6683 	if (old_crtc_state->master_transcoder != INVALID_TRANSCODER)
6684 		transcoders |= BIT(old_crtc_state->master_transcoder);
6685 
6686 	return intel_modeset_affected_transcoders(state,
6687 						  transcoders);
6688 }
6689 
6690 static int intel_dp_connector_atomic_check(struct drm_connector *_connector,
6691 					   struct drm_atomic_commit *_state)
6692 {
6693 	struct intel_connector *connector = to_intel_connector(_connector);
6694 	struct intel_display *display = to_intel_display(connector);
6695 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
6696 	struct drm_connector_state *conn_state =
6697 		drm_atomic_get_new_connector_state(_state, &connector->base);
6698 	struct intel_dp *intel_dp = enc_to_intel_dp(connector->encoder);
6699 	int ret;
6700 
6701 	ret = intel_digital_connector_atomic_check(&connector->base, &state->base);
6702 	if (ret)
6703 		return ret;
6704 
6705 	if (intel_dp_mst_source_support(intel_dp)) {
6706 		ret = drm_dp_mst_root_conn_atomic_check(conn_state, &intel_dp->mst.mgr);
6707 		if (ret)
6708 			return ret;
6709 	}
6710 
6711 	if (!intel_connector_needs_modeset(state, &connector->base))
6712 		return 0;
6713 
6714 	ret = intel_dp_tunnel_atomic_check_state(state,
6715 						 intel_dp,
6716 						 connector);
6717 	if (ret)
6718 		return ret;
6719 
6720 	/*
6721 	 * We don't enable port sync on BDW due to missing w/as and
6722 	 * due to not having adjusted the modeset sequence appropriately.
6723 	 */
6724 	if (DISPLAY_VER(display) < 9)
6725 		return 0;
6726 
6727 	if (connector->base.has_tile) {
6728 		ret = intel_modeset_tile_group(state, connector->base.tile_group->id);
6729 		if (ret)
6730 			return ret;
6731 	}
6732 
6733 	return intel_modeset_synced_crtcs(state, &connector->base);
6734 }
6735 
6736 static void intel_dp_oob_hotplug_event(struct drm_connector *_connector,
6737 				       enum drm_connector_status hpd_state)
6738 {
6739 	struct intel_connector *connector = to_intel_connector(_connector);
6740 	struct intel_display *display = to_intel_display(connector);
6741 	struct intel_encoder *encoder = intel_attached_encoder(connector);
6742 	bool hpd_high = hpd_state == connector_status_connected;
6743 	unsigned int hpd_pin = encoder->hpd_pin;
6744 	bool need_work = false;
6745 
6746 	spin_lock_irq(&display->irq.lock);
6747 	if (hpd_high != test_bit(hpd_pin, &display->hotplug.oob_hotplug_last_state)) {
6748 		display->hotplug.event_bits |= BIT(hpd_pin);
6749 
6750 		__assign_bit(hpd_pin,
6751 			     &display->hotplug.oob_hotplug_last_state,
6752 			     hpd_high);
6753 		need_work = true;
6754 	}
6755 	spin_unlock_irq(&display->irq.lock);
6756 
6757 	if (need_work)
6758 		intel_hpd_schedule_detection(display);
6759 }
6760 
6761 static const struct drm_connector_funcs intel_dp_connector_funcs = {
6762 	.force = intel_dp_force,
6763 	.fill_modes = drm_helper_probe_single_connector_modes,
6764 	.atomic_get_property = intel_digital_connector_atomic_get_property,
6765 	.atomic_set_property = intel_digital_connector_atomic_set_property,
6766 	.late_register = intel_dp_connector_register,
6767 	.early_unregister = intel_dp_connector_unregister,
6768 	.destroy = intel_connector_destroy,
6769 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
6770 	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
6771 	.oob_hotplug_event = intel_dp_oob_hotplug_event,
6772 };
6773 
6774 static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
6775 	.detect_ctx = intel_dp_detect,
6776 	.get_modes = intel_dp_get_modes,
6777 	.mode_valid = intel_dp_mode_valid,
6778 	.atomic_check = intel_dp_connector_atomic_check,
6779 };
6780 
6781 enum irqreturn
6782 intel_dp_hpd_pulse(struct intel_digital_port *dig_port, bool long_hpd)
6783 {
6784 	struct intel_display *display = to_intel_display(dig_port);
6785 	struct intel_dp *intel_dp = &dig_port->dp;
6786 	u8 dpcd[DP_RECEIVER_CAP_SIZE];
6787 
6788 	if (dig_port->base.type == INTEL_OUTPUT_EDP &&
6789 	    (long_hpd ||
6790 	     intel_display_rpm_suspended(display) ||
6791 	     !intel_pps_have_panel_power_or_vdd(intel_dp))) {
6792 		/*
6793 		 * vdd off can generate a long/short pulse on eDP which
6794 		 * would require vdd on to handle it, and thus we
6795 		 * would end up in an endless cycle of
6796 		 * "vdd off -> long/short hpd -> vdd on -> detect -> vdd off -> ..."
6797 		 */
6798 		drm_dbg_kms(display->drm,
6799 			    "ignoring %s hpd on eDP [ENCODER:%d:%s]\n",
6800 			    long_hpd ? "long" : "short",
6801 			    dig_port->base.base.base.id,
6802 			    dig_port->base.base.name);
6803 		return IRQ_HANDLED;
6804 	}
6805 
6806 	drm_dbg_kms(display->drm, "got hpd irq on [ENCODER:%d:%s] - %s\n",
6807 		    dig_port->base.base.base.id,
6808 		    dig_port->base.base.name,
6809 		    long_hpd ? "long" : "short");
6810 
6811 	/*
6812 	 * TBT DP tunnels require the GFX driver to read out the DPRX caps in
6813 	 * response to long HPD pulses. The DP hotplug handler does that,
6814 	 * however the hotplug handler may be blocked by another
6815 	 * connector's/encoder's hotplug handler. Since the TBT CM may not
6816 	 * complete the DP tunnel BW request for the latter connector/encoder
6817 	 * waiting for this encoder's DPRX read, perform a dummy read here.
6818 	 */
6819 	if (long_hpd) {
6820 		intel_dp_dpcd_set_probe(intel_dp, true);
6821 
6822 		intel_dp_read_dprx_caps(intel_dp, dpcd);
6823 
6824 		intel_dp->reset_link_params = true;
6825 		intel_dp_invalidate_source_oui(intel_dp);
6826 
6827 		return IRQ_NONE;
6828 	}
6829 
6830 	if (intel_dp->is_mst) {
6831 		if (!intel_dp_check_mst_status(intel_dp))
6832 			return IRQ_NONE;
6833 	} else if (!intel_dp_short_pulse(intel_dp)) {
6834 		return IRQ_NONE;
6835 	}
6836 
6837 	return IRQ_HANDLED;
6838 }
6839 
6840 static bool _intel_dp_is_port_edp(struct intel_display *display,
6841 				  const struct intel_bios_encoder_data *devdata,
6842 				  enum port port)
6843 {
6844 	/*
6845 	 * eDP not supported on g4x. so bail out early just
6846 	 * for a bit extra safety in case the VBT is bonkers.
6847 	 */
6848 	if (DISPLAY_VER(display) < 5)
6849 		return false;
6850 
6851 	if (DISPLAY_VER(display) < 9 && port == PORT_A)
6852 		return true;
6853 
6854 	return devdata && intel_bios_encoder_supports_edp(devdata);
6855 }
6856 
6857 bool intel_dp_is_port_edp(struct intel_display *display, enum port port)
6858 {
6859 	const struct intel_bios_encoder_data *devdata =
6860 		intel_bios_encoder_data_lookup(display, port);
6861 
6862 	return _intel_dp_is_port_edp(display, devdata, port);
6863 }
6864 
6865 bool
6866 intel_dp_has_gamut_metadata_dip(struct intel_encoder *encoder)
6867 {
6868 	struct intel_display *display = to_intel_display(encoder);
6869 	enum port port = encoder->port;
6870 
6871 	if (intel_bios_encoder_is_lspcon(encoder->devdata))
6872 		return false;
6873 
6874 	if (DISPLAY_VER(display) >= 11)
6875 		return true;
6876 
6877 	if (port == PORT_A)
6878 		return false;
6879 
6880 	if (display->platform.haswell || display->platform.broadwell ||
6881 	    DISPLAY_VER(display) >= 9)
6882 		return true;
6883 
6884 	return false;
6885 }
6886 
6887 static void
6888 intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *_connector)
6889 {
6890 	struct intel_connector *connector = to_intel_connector(_connector);
6891 	struct intel_display *display = to_intel_display(intel_dp);
6892 	enum port port = dp_to_dig_port(intel_dp)->base.port;
6893 
6894 	if (!intel_dp_is_edp(intel_dp))
6895 		drm_connector_attach_dp_subconnector_property(&connector->base);
6896 
6897 	if (!display->platform.g4x && port != PORT_A)
6898 		intel_attach_force_audio_property(&connector->base);
6899 
6900 	intel_attach_broadcast_rgb_property(&connector->base);
6901 	if (HAS_GMCH(display))
6902 		drm_connector_attach_max_bpc_property(&connector->base, 6, 10);
6903 	else if (DISPLAY_VER(display) >= 5)
6904 		drm_connector_attach_max_bpc_property(&connector->base, 6, 12);
6905 
6906 	/* Register HDMI colorspace for case of lspcon */
6907 	if (intel_bios_encoder_is_lspcon(dp_to_dig_port(intel_dp)->base.devdata)) {
6908 		drm_connector_attach_content_type_property(&connector->base);
6909 		intel_attach_hdmi_colorspace_property(&connector->base);
6910 	} else {
6911 		intel_attach_dp_colorspace_property(&connector->base);
6912 	}
6913 
6914 	if (intel_dp_has_gamut_metadata_dip(&dp_to_dig_port(intel_dp)->base))
6915 		drm_connector_attach_hdr_output_metadata_property(&connector->base);
6916 
6917 	if (HAS_VRR(display))
6918 		drm_connector_attach_vrr_capable_property(&connector->base);
6919 }
6920 
6921 static void
6922 intel_edp_add_properties(struct intel_dp *intel_dp)
6923 {
6924 	struct intel_display *display = to_intel_display(intel_dp);
6925 	struct intel_connector *connector = intel_dp->attached_connector;
6926 	const struct drm_display_mode *fixed_mode =
6927 		intel_panel_preferred_fixed_mode(connector);
6928 
6929 	intel_attach_scaling_mode_property(&connector->base);
6930 
6931 	drm_connector_set_panel_orientation_with_quirk(&connector->base,
6932 						       display->vbt.orientation,
6933 						       fixed_mode->hdisplay,
6934 						       fixed_mode->vdisplay);
6935 }
6936 
6937 static void intel_edp_backlight_setup(struct intel_dp *intel_dp,
6938 				      struct intel_connector *connector)
6939 {
6940 	struct intel_display *display = to_intel_display(intel_dp);
6941 	enum pipe pipe = INVALID_PIPE;
6942 
6943 	if (display->platform.valleyview || display->platform.cherryview)
6944 		pipe = vlv_pps_backlight_initial_pipe(intel_dp);
6945 
6946 	intel_backlight_setup(connector, pipe);
6947 }
6948 
6949 static bool intel_edp_init_connector(struct intel_dp *intel_dp,
6950 				     struct intel_connector *connector)
6951 {
6952 	struct intel_display *display = to_intel_display(intel_dp);
6953 	struct drm_display_mode *fixed_mode;
6954 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
6955 	bool has_dpcd;
6956 	const struct drm_edid *drm_edid;
6957 
6958 	if (!intel_dp_is_edp(intel_dp))
6959 		return true;
6960 
6961 	/*
6962 	 * On IBX/CPT we may get here with LVDS already registered. Since the
6963 	 * driver uses the only internal power sequencer available for both
6964 	 * eDP and LVDS bail out early in this case to prevent interfering
6965 	 * with an already powered-on LVDS power sequencer.
6966 	 */
6967 	if (intel_get_lvds_encoder(display)) {
6968 		drm_WARN_ON(display->drm,
6969 			    !(HAS_PCH_IBX(display) || HAS_PCH_CPT(display)));
6970 		drm_info(display->drm,
6971 			 "LVDS was detected, not registering eDP\n");
6972 
6973 		return false;
6974 	}
6975 
6976 	intel_bios_init_panel_early(display, &connector->panel,
6977 				    encoder->devdata);
6978 
6979 	if (!intel_pps_init(intel_dp)) {
6980 		drm_info(display->drm,
6981 			 "[ENCODER:%d:%s] unusable PPS, disabling eDP\n",
6982 			 encoder->base.base.id, encoder->base.name);
6983 		/*
6984 		 * The BIOS may have still enabled VDD on the PPS even
6985 		 * though it's unusable. Make sure we turn it back off
6986 		 * and to release the power domain references/etc.
6987 		 */
6988 		goto out_vdd_off;
6989 	}
6990 
6991 	/*
6992 	 * Enable HPD sense for live status check.
6993 	 * intel_hpd_irq_setup() will turn it off again
6994 	 * if it's no longer needed later.
6995 	 *
6996 	 * The DPCD probe below will make sure VDD is on.
6997 	 */
6998 	intel_hpd_enable_detection(encoder);
6999 
7000 	intel_alpm_init(intel_dp);
7001 
7002 	/* Cache DPCD and EDID for edp. */
7003 	has_dpcd = intel_edp_init_dpcd(intel_dp, connector);
7004 
7005 	if (!has_dpcd) {
7006 		/* if this fails, presume the device is a ghost */
7007 		drm_info(display->drm,
7008 			 "[ENCODER:%d:%s] failed to retrieve link info, disabling eDP\n",
7009 			 encoder->base.base.id, encoder->base.name);
7010 		goto out_vdd_off;
7011 	}
7012 
7013 	/*
7014 	 * VBT and straps are liars. Also check HPD as that seems
7015 	 * to be the most reliable piece of information available.
7016 	 *
7017 	 * ... expect on devices that forgot to hook HPD up for eDP
7018 	 * (eg. Acer Chromebook C710), so we'll check it only if multiple
7019 	 * ports are attempting to use the same AUX CH, according to VBT.
7020 	 */
7021 	if (intel_bios_dp_has_shared_aux_ch(encoder->devdata)) {
7022 		/*
7023 		 * If this fails, presume the DPCD answer came
7024 		 * from some other port using the same AUX CH.
7025 		 *
7026 		 * FIXME maybe cleaner to check this before the
7027 		 * DPCD read? Would need sort out the VDD handling...
7028 		 */
7029 		if (!intel_digital_port_connected(encoder)) {
7030 			drm_info(display->drm,
7031 				 "[ENCODER:%d:%s] HPD is down, disabling eDP\n",
7032 				 encoder->base.base.id, encoder->base.name);
7033 			goto out_vdd_off;
7034 		}
7035 
7036 		/*
7037 		 * Unfortunately even the HPD based detection fails on
7038 		 * eg. Asus B360M-A (CFL+CNP), so as a last resort fall
7039 		 * back to checking for a VGA branch device. Only do this
7040 		 * on known affected platforms to minimize false positives.
7041 		 */
7042 		if (DISPLAY_VER(display) == 9 && drm_dp_is_branch(intel_dp->dpcd) &&
7043 		    (intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) ==
7044 		    DP_DWN_STRM_PORT_TYPE_ANALOG) {
7045 			drm_info(display->drm,
7046 				 "[ENCODER:%d:%s] VGA converter detected, disabling eDP\n",
7047 				 encoder->base.base.id, encoder->base.name);
7048 			goto out_vdd_off;
7049 		}
7050 	}
7051 
7052 	mutex_lock(&display->drm->mode_config.mutex);
7053 	drm_edid = drm_edid_read_ddc(&connector->base, connector->base.ddc);
7054 	if (!drm_edid) {
7055 		/* Fallback to EDID from ACPI OpRegion, if any */
7056 		drm_edid = intel_opregion_get_edid(connector);
7057 		if (drm_edid)
7058 			drm_dbg_kms(display->drm,
7059 				    "[CONNECTOR:%d:%s] Using OpRegion EDID\n",
7060 				    connector->base.base.id, connector->base.name);
7061 	}
7062 	if (drm_edid) {
7063 		if (drm_edid_connector_update(&connector->base, drm_edid) ||
7064 		    !drm_edid_connector_add_modes(&connector->base)) {
7065 			drm_edid_connector_update(&connector->base, NULL);
7066 			drm_edid_free(drm_edid);
7067 			drm_edid = ERR_PTR(-EINVAL);
7068 		}
7069 	} else {
7070 		drm_edid = ERR_PTR(-ENOENT);
7071 	}
7072 
7073 	intel_bios_init_panel_late(display, &connector->panel, encoder->devdata,
7074 				   IS_ERR(drm_edid) ? NULL : drm_edid);
7075 
7076 	intel_panel_add_edid_fixed_modes(connector, true);
7077 
7078 	/* MSO requires information from the EDID */
7079 	intel_edp_mso_init(intel_dp);
7080 
7081 	/* multiply the mode clock and horizontal timings for MSO */
7082 	list_for_each_entry(fixed_mode, &connector->panel.fixed_modes, head)
7083 		intel_edp_mso_mode_fixup(connector, fixed_mode);
7084 
7085 	/* fallback to VBT if available for eDP */
7086 	if (!intel_panel_preferred_fixed_mode(connector))
7087 		intel_panel_add_vbt_lfp_fixed_mode(connector);
7088 
7089 	mutex_unlock(&display->drm->mode_config.mutex);
7090 
7091 	if (!intel_panel_preferred_fixed_mode(connector)) {
7092 		drm_info(display->drm,
7093 			 "[ENCODER:%d:%s] failed to find fixed mode for the panel, disabling eDP\n",
7094 			 encoder->base.base.id, encoder->base.name);
7095 		goto out_vdd_off;
7096 	}
7097 
7098 	intel_panel_init(connector, drm_edid);
7099 
7100 	intel_edp_backlight_setup(intel_dp, connector);
7101 
7102 	intel_edp_add_properties(intel_dp);
7103 
7104 	intel_pps_init_late(intel_dp);
7105 
7106 	return true;
7107 
7108 out_vdd_off:
7109 	intel_pps_vdd_off_sync(intel_dp);
7110 	intel_bios_fini_panel(&connector->panel);
7111 
7112 	return false;
7113 }
7114 
7115 bool
7116 intel_dp_init_connector(struct intel_digital_port *dig_port,
7117 			struct intel_connector *connector)
7118 {
7119 	struct intel_display *display = to_intel_display(dig_port);
7120 	struct intel_dp *intel_dp = &dig_port->dp;
7121 	struct intel_encoder *encoder = &dig_port->base;
7122 	struct drm_device *dev = encoder->base.dev;
7123 	enum port port = encoder->port;
7124 	int type;
7125 
7126 	if (drm_WARN(dev, dig_port->max_lanes < 1,
7127 		     "Not enough lanes (%d) for DP on [ENCODER:%d:%s]\n",
7128 		     dig_port->max_lanes, encoder->base.base.id,
7129 		     encoder->base.name))
7130 		return false;
7131 
7132 	intel_dp->reset_link_params = true;
7133 
7134 	/* Preserve the current hw state. */
7135 	intel_dp->DP = intel_de_read(display, intel_dp->output_reg);
7136 	intel_dp->attached_connector = connector;
7137 
7138 	if (_intel_dp_is_port_edp(display, encoder->devdata, port)) {
7139 		/*
7140 		 * Currently we don't support eDP on TypeC ports for DISPLAY_VER < 30,
7141 		 * although in theory it could work on TypeC legacy ports.
7142 		 */
7143 		drm_WARN_ON(dev, intel_encoder_is_tc(encoder) &&
7144 			    DISPLAY_VER(display) < 30);
7145 		type = DRM_MODE_CONNECTOR_eDP;
7146 		encoder->type = INTEL_OUTPUT_EDP;
7147 
7148 		/* eDP only on port B and/or C on vlv/chv */
7149 		if (drm_WARN_ON(dev, (display->platform.valleyview ||
7150 				      display->platform.cherryview) &&
7151 				port != PORT_B && port != PORT_C))
7152 			return false;
7153 	} else {
7154 		type = DRM_MODE_CONNECTOR_DisplayPort;
7155 	}
7156 
7157 	intel_dp_set_default_sink_rates(intel_dp);
7158 	intel_dp_set_default_max_sink_lane_count(intel_dp);
7159 
7160 	if (display->platform.valleyview || display->platform.cherryview)
7161 		vlv_pps_pipe_init(intel_dp);
7162 
7163 	intel_dp_aux_init(intel_dp);
7164 	connector->dp.dsc_decompression_aux = &intel_dp->aux;
7165 
7166 	drm_dbg_kms(display->drm,
7167 		    "Adding %s connector on [ENCODER:%d:%s]\n",
7168 		    type == DRM_MODE_CONNECTOR_eDP ? "eDP" : "DP",
7169 		    encoder->base.base.id, encoder->base.name);
7170 
7171 	drm_connector_init_with_ddc(dev, &connector->base, &intel_dp_connector_funcs,
7172 				    type, &intel_dp->aux.ddc);
7173 	drm_connector_helper_add(&connector->base, &intel_dp_connector_helper_funcs);
7174 
7175 	if (!HAS_GMCH(display) && DISPLAY_VER(display) < 12)
7176 		connector->base.interlace_allowed = true;
7177 
7178 	if (type != DRM_MODE_CONNECTOR_eDP)
7179 		connector->polled = DRM_CONNECTOR_POLL_HPD;
7180 	connector->base.polled = connector->polled;
7181 
7182 	intel_connector_attach_encoder(connector, encoder);
7183 
7184 	if (HAS_DDI(display))
7185 		connector->get_hw_state = intel_ddi_connector_get_hw_state;
7186 	else
7187 		connector->get_hw_state = intel_connector_get_hw_state;
7188 	connector->sync_state = intel_dp_connector_sync_state;
7189 
7190 	if (!intel_edp_init_connector(intel_dp, connector)) {
7191 		intel_dp_aux_fini(intel_dp);
7192 		goto fail;
7193 	}
7194 
7195 	intel_dp_set_source_rates(intel_dp);
7196 	intel_dp_set_common_link_params(intel_dp);
7197 	intel_dp_reset_link_params(intel_dp);
7198 
7199 	/* init MST on ports that can support it */
7200 	intel_dp_mst_encoder_init(dig_port, connector->base.base.id);
7201 
7202 	intel_dp_add_properties(intel_dp, &connector->base);
7203 
7204 	if (is_hdcp_supported(display, port) && !intel_dp_is_edp(intel_dp)) {
7205 		int ret = intel_dp_hdcp_init(dig_port, connector);
7206 		if (ret)
7207 			drm_dbg_kms(display->drm,
7208 				    "HDCP init failed, skipping.\n");
7209 	}
7210 
7211 	intel_dp->frl.is_trained = false;
7212 	intel_dp->frl.trained_rate_gbps = 0;
7213 
7214 	intel_psr_init(intel_dp);
7215 
7216 	return true;
7217 
7218 fail:
7219 	intel_display_power_flush_work(display);
7220 	drm_connector_cleanup(&connector->base);
7221 
7222 	return false;
7223 }
7224 
7225 void intel_dp_cleanup_connector(struct intel_digital_port *dig_port,
7226 				struct intel_connector *connector)
7227 {
7228 	struct intel_display *display = to_intel_display(connector);
7229 	struct intel_dp *intel_dp = &dig_port->dp;
7230 
7231 	intel_display_power_flush_work(display);
7232 
7233 	intel_dp_mst_encoder_cleanup(dig_port);
7234 	intel_dp_aux_fini(intel_dp);
7235 	drm_connector_cleanup(&connector->base);
7236 }
7237 
7238 void intel_dp_mst_suspend(struct intel_display *display)
7239 {
7240 	struct intel_encoder *encoder;
7241 
7242 	if (!HAS_DISPLAY(display))
7243 		return;
7244 
7245 	for_each_intel_encoder(display->drm, encoder) {
7246 		struct intel_dp *intel_dp;
7247 
7248 		if (encoder->type != INTEL_OUTPUT_DDI)
7249 			continue;
7250 
7251 		intel_dp = enc_to_intel_dp(encoder);
7252 
7253 		if (!intel_dp_mst_source_support(intel_dp))
7254 			continue;
7255 
7256 		if (intel_dp->is_mst)
7257 			drm_dp_mst_topology_mgr_suspend(&intel_dp->mst.mgr);
7258 	}
7259 }
7260 
7261 void intel_dp_mst_resume(struct intel_display *display)
7262 {
7263 	struct intel_encoder *encoder;
7264 
7265 	if (!HAS_DISPLAY(display))
7266 		return;
7267 
7268 	for_each_intel_encoder(display->drm, encoder) {
7269 		struct intel_dp *intel_dp;
7270 		int ret;
7271 
7272 		if (encoder->type != INTEL_OUTPUT_DDI)
7273 			continue;
7274 
7275 		intel_dp = enc_to_intel_dp(encoder);
7276 
7277 		if (!intel_dp_mst_source_support(intel_dp))
7278 			continue;
7279 
7280 		ret = drm_dp_mst_topology_mgr_resume(&intel_dp->mst.mgr, true);
7281 		if (ret) {
7282 			intel_dp->is_mst = false;
7283 			drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst.mgr, false);
7284 		}
7285 	}
7286 }
7287 
7288 static
7289 int intel_dp_sdp_compute_config_late(struct intel_crtc_state *crtc_state)
7290 {
7291 	struct intel_display *display = to_intel_display(crtc_state);
7292 	int guardband = intel_crtc_vblank_length(crtc_state);
7293 	int min_sdp_guardband = intel_dp_sdp_min_guardband(crtc_state, false);
7294 
7295 	if (guardband < min_sdp_guardband) {
7296 		drm_dbg_kms(display->drm, "guardband %d < min sdp guardband %d\n",
7297 			    guardband, min_sdp_guardband);
7298 		return -EINVAL;
7299 	}
7300 
7301 	return 0;
7302 }
7303 
7304 int intel_dp_compute_config_late(struct intel_encoder *encoder,
7305 				 struct intel_crtc_state *crtc_state,
7306 				 struct drm_connector_state *conn_state)
7307 {
7308 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
7309 	int ret;
7310 
7311 	intel_psr_compute_config_late(intel_dp, crtc_state);
7312 
7313 	ret = intel_dp_sdp_compute_config_late(crtc_state);
7314 	if (ret)
7315 		return ret;
7316 
7317 	intel_alpm_lobf_compute_config_late(intel_dp, crtc_state);
7318 
7319 	return 0;
7320 }
7321 
7322 static
7323 int intel_dp_get_lines_for_sdp(const struct intel_crtc_state *crtc_state, u32 type)
7324 {
7325 	switch (type) {
7326 	case DP_SDP_VSC_EXT_VESA:
7327 	case DP_SDP_VSC_EXT_CEA:
7328 		return 10;
7329 	case HDMI_PACKET_TYPE_GAMUT_METADATA:
7330 		return 8;
7331 	case DP_SDP_PPS:
7332 		return 7;
7333 	case DP_SDP_ADAPTIVE_SYNC:
7334 		return crtc_state->vrr.vsync_start + 1;
7335 	default:
7336 		break;
7337 	}
7338 
7339 	return 0;
7340 }
7341 
7342 int intel_dp_sdp_min_guardband(const struct intel_crtc_state *crtc_state,
7343 			       bool assume_all_enabled)
7344 {
7345 	int sdp_guardband = 0;
7346 
7347 	if (assume_all_enabled ||
7348 	    crtc_state->infoframes.enable &
7349 	    intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GAMUT_METADATA))
7350 		sdp_guardband = max(sdp_guardband,
7351 				    intel_dp_get_lines_for_sdp(crtc_state,
7352 							       HDMI_PACKET_TYPE_GAMUT_METADATA));
7353 
7354 	if (assume_all_enabled ||
7355 	    crtc_state->dsc.compression_enable)
7356 		sdp_guardband = max(sdp_guardband,
7357 				    intel_dp_get_lines_for_sdp(crtc_state, DP_SDP_PPS));
7358 
7359 	if (crtc_state->infoframes.enable &
7360 	    intel_hdmi_infoframe_enable(DP_SDP_ADAPTIVE_SYNC))
7361 		sdp_guardband = max(sdp_guardband,
7362 				    intel_dp_get_lines_for_sdp(crtc_state, DP_SDP_ADAPTIVE_SYNC));
7363 
7364 	return sdp_guardband;
7365 }
7366 
7367 bool intel_dp_joiner_candidate_valid(struct intel_connector *connector,
7368 				     int hdisplay,
7369 				     int num_joined_pipes)
7370 {
7371 	struct intel_display *display = to_intel_display(connector);
7372 	struct intel_dp *intel_dp = intel_attached_dp(connector);
7373 
7374 	if (!intel_dp_can_join(intel_dp, num_joined_pipes))
7375 		return false;
7376 
7377 	if (hdisplay > num_joined_pipes * intel_dp_max_hdisplay_per_pipe(display))
7378 		return false;
7379 
7380 	if (connector->force_joined_pipes && connector->force_joined_pipes != num_joined_pipes)
7381 		return false;
7382 
7383 	return true;
7384 }
7385 
7386 u8 intel_dp_as_sdp_transmission_time(void)
7387 {
7388 	/*
7389 	 * DP allows AS SDP position to move during PR active in some cases, but
7390 	 * software-controlled refresh rate changes with DC6v / ALPM require the
7391 	 * AS SDP to remain at T1. Use T1 unconditionally for now.
7392 	 */
7393 
7394 	return DP_PR_AS_SDP_SETUP_TIME_T1;
7395 }
7396 
7397 int intel_dp_link_init(struct intel_dp *intel_dp)
7398 {
7399 	intel_dp->link.training = intel_dp_link_training_init(intel_dp);
7400 	if (!intel_dp->link.training)
7401 		return -ENOMEM;
7402 
7403 	intel_dp->link.caps = intel_dp_link_caps_init(intel_dp);
7404 	if (!intel_dp->link.caps) {
7405 		intel_dp_link_training_cleanup(intel_dp->link.training);
7406 
7407 		return -ENOMEM;
7408 	}
7409 
7410 	return 0;
7411 }
7412 
7413 void intel_dp_link_cleanup(struct intel_dp *intel_dp)
7414 {
7415 	intel_dp_link_caps_cleanup(intel_dp->link.caps);
7416 	intel_dp_link_training_cleanup(intel_dp->link.training);
7417 }
7418