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