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