xref: /linux/drivers/gpu/drm/i915/display/intel_dp.c (revision dec1c62e91ba268ab2a6e339d4d7a59287d5eba1)
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/notifier.h>
31 #include <linux/slab.h>
32 #include <linux/string_helpers.h>
33 #include <linux/timekeeping.h>
34 #include <linux/types.h>
35 
36 #include <asm/byteorder.h>
37 
38 #include <drm/display/drm_dp_helper.h>
39 #include <drm/display/drm_dsc_helper.h>
40 #include <drm/display/drm_hdmi_helper.h>
41 #include <drm/drm_atomic_helper.h>
42 #include <drm/drm_crtc.h>
43 #include <drm/drm_probe_helper.h>
44 
45 #include "g4x_dp.h"
46 #include "i915_debugfs.h"
47 #include "i915_drv.h"
48 #include "intel_atomic.h"
49 #include "intel_audio.h"
50 #include "intel_backlight.h"
51 #include "intel_combo_phy_regs.h"
52 #include "intel_connector.h"
53 #include "intel_crtc.h"
54 #include "intel_ddi.h"
55 #include "intel_de.h"
56 #include "intel_display_types.h"
57 #include "intel_dp.h"
58 #include "intel_dp_aux.h"
59 #include "intel_dp_hdcp.h"
60 #include "intel_dp_link_training.h"
61 #include "intel_dp_mst.h"
62 #include "intel_dpio_phy.h"
63 #include "intel_dpll.h"
64 #include "intel_fifo_underrun.h"
65 #include "intel_hdcp.h"
66 #include "intel_hdmi.h"
67 #include "intel_hotplug.h"
68 #include "intel_lspcon.h"
69 #include "intel_lvds.h"
70 #include "intel_panel.h"
71 #include "intel_pch_display.h"
72 #include "intel_pps.h"
73 #include "intel_psr.h"
74 #include "intel_tc.h"
75 #include "intel_vdsc.h"
76 #include "intel_vrr.h"
77 
78 /* DP DSC throughput values used for slice count calculations KPixels/s */
79 #define DP_DSC_PEAK_PIXEL_RATE			2720000
80 #define DP_DSC_MAX_ENC_THROUGHPUT_0		340000
81 #define DP_DSC_MAX_ENC_THROUGHPUT_1		400000
82 
83 /* DP DSC FEC Overhead factor = 1/(0.972261) */
84 #define DP_DSC_FEC_OVERHEAD_FACTOR		972261
85 
86 /* Compliance test status bits  */
87 #define INTEL_DP_RESOLUTION_SHIFT_MASK	0
88 #define INTEL_DP_RESOLUTION_PREFERRED	(1 << INTEL_DP_RESOLUTION_SHIFT_MASK)
89 #define INTEL_DP_RESOLUTION_STANDARD	(2 << INTEL_DP_RESOLUTION_SHIFT_MASK)
90 #define INTEL_DP_RESOLUTION_FAILSAFE	(3 << INTEL_DP_RESOLUTION_SHIFT_MASK)
91 
92 
93 /* Constants for DP DSC configurations */
94 static const u8 valid_dsc_bpp[] = {6, 8, 10, 12, 15};
95 
96 /* With Single pipe configuration, HW is capable of supporting maximum
97  * of 4 slices per line.
98  */
99 static const u8 valid_dsc_slicecount[] = {1, 2, 4};
100 
101 /**
102  * intel_dp_is_edp - is the given port attached to an eDP panel (either CPU or PCH)
103  * @intel_dp: DP struct
104  *
105  * If a CPU or PCH DP output is attached to an eDP panel, this function
106  * will return true, and false otherwise.
107  *
108  * This function is not safe to use prior to encoder type being set.
109  */
110 bool intel_dp_is_edp(struct intel_dp *intel_dp)
111 {
112 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
113 
114 	return dig_port->base.type == INTEL_OUTPUT_EDP;
115 }
116 
117 static void intel_dp_unset_edid(struct intel_dp *intel_dp);
118 static int intel_dp_dsc_compute_bpp(struct intel_dp *intel_dp, u8 dsc_max_bpc);
119 
120 /* Is link rate UHBR and thus 128b/132b? */
121 bool intel_dp_is_uhbr(const struct intel_crtc_state *crtc_state)
122 {
123 	return crtc_state->port_clock >= 1000000;
124 }
125 
126 static void intel_dp_set_default_sink_rates(struct intel_dp *intel_dp)
127 {
128 	intel_dp->sink_rates[0] = 162000;
129 	intel_dp->num_sink_rates = 1;
130 }
131 
132 /* update sink rates from dpcd */
133 static void intel_dp_set_dpcd_sink_rates(struct intel_dp *intel_dp)
134 {
135 	static const int dp_rates[] = {
136 		162000, 270000, 540000, 810000
137 	};
138 	int i, max_rate;
139 	int max_lttpr_rate;
140 
141 	if (drm_dp_has_quirk(&intel_dp->desc, DP_DPCD_QUIRK_CAN_DO_MAX_LINK_RATE_3_24_GBPS)) {
142 		/* Needed, e.g., for Apple MBP 2017, 15 inch eDP Retina panel */
143 		static const int quirk_rates[] = { 162000, 270000, 324000 };
144 
145 		memcpy(intel_dp->sink_rates, quirk_rates, sizeof(quirk_rates));
146 		intel_dp->num_sink_rates = ARRAY_SIZE(quirk_rates);
147 
148 		return;
149 	}
150 
151 	/*
152 	 * Sink rates for 8b/10b.
153 	 */
154 	max_rate = drm_dp_bw_code_to_link_rate(intel_dp->dpcd[DP_MAX_LINK_RATE]);
155 	max_lttpr_rate = drm_dp_lttpr_max_link_rate(intel_dp->lttpr_common_caps);
156 	if (max_lttpr_rate)
157 		max_rate = min(max_rate, max_lttpr_rate);
158 
159 	for (i = 0; i < ARRAY_SIZE(dp_rates); i++) {
160 		if (dp_rates[i] > max_rate)
161 			break;
162 		intel_dp->sink_rates[i] = dp_rates[i];
163 	}
164 
165 	/*
166 	 * Sink rates for 128b/132b. If set, sink should support all 8b/10b
167 	 * rates and 10 Gbps.
168 	 */
169 	if (intel_dp->dpcd[DP_MAIN_LINK_CHANNEL_CODING] & DP_CAP_ANSI_128B132B) {
170 		u8 uhbr_rates = 0;
171 
172 		BUILD_BUG_ON(ARRAY_SIZE(intel_dp->sink_rates) < ARRAY_SIZE(dp_rates) + 3);
173 
174 		drm_dp_dpcd_readb(&intel_dp->aux,
175 				  DP_128B132B_SUPPORTED_LINK_RATES, &uhbr_rates);
176 
177 		if (drm_dp_lttpr_count(intel_dp->lttpr_common_caps)) {
178 			/* We have a repeater */
179 			if (intel_dp->lttpr_common_caps[0] >= 0x20 &&
180 			    intel_dp->lttpr_common_caps[DP_MAIN_LINK_CHANNEL_CODING_PHY_REPEATER -
181 							DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV] &
182 			    DP_PHY_REPEATER_128B132B_SUPPORTED) {
183 				/* Repeater supports 128b/132b, valid UHBR rates */
184 				uhbr_rates &= intel_dp->lttpr_common_caps[DP_PHY_REPEATER_128B132B_RATES -
185 									  DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
186 			} else {
187 				/* Does not support 128b/132b */
188 				uhbr_rates = 0;
189 			}
190 		}
191 
192 		if (uhbr_rates & DP_UHBR10)
193 			intel_dp->sink_rates[i++] = 1000000;
194 		if (uhbr_rates & DP_UHBR13_5)
195 			intel_dp->sink_rates[i++] = 1350000;
196 		if (uhbr_rates & DP_UHBR20)
197 			intel_dp->sink_rates[i++] = 2000000;
198 	}
199 
200 	intel_dp->num_sink_rates = i;
201 }
202 
203 static void intel_dp_set_sink_rates(struct intel_dp *intel_dp)
204 {
205 	struct intel_connector *connector = intel_dp->attached_connector;
206 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
207 	struct intel_encoder *encoder = &intel_dig_port->base;
208 
209 	intel_dp_set_dpcd_sink_rates(intel_dp);
210 
211 	if (intel_dp->num_sink_rates)
212 		return;
213 
214 	drm_err(&dp_to_i915(intel_dp)->drm,
215 		"[CONNECTOR:%d:%s][ENCODER:%d:%s] Invalid DPCD with no link rates, using defaults\n",
216 		connector->base.base.id, connector->base.name,
217 		encoder->base.base.id, encoder->base.name);
218 
219 	intel_dp_set_default_sink_rates(intel_dp);
220 }
221 
222 static void intel_dp_set_default_max_sink_lane_count(struct intel_dp *intel_dp)
223 {
224 	intel_dp->max_sink_lane_count = 1;
225 }
226 
227 static void intel_dp_set_max_sink_lane_count(struct intel_dp *intel_dp)
228 {
229 	struct intel_connector *connector = intel_dp->attached_connector;
230 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
231 	struct intel_encoder *encoder = &intel_dig_port->base;
232 
233 	intel_dp->max_sink_lane_count = drm_dp_max_lane_count(intel_dp->dpcd);
234 
235 	switch (intel_dp->max_sink_lane_count) {
236 	case 1:
237 	case 2:
238 	case 4:
239 		return;
240 	}
241 
242 	drm_err(&dp_to_i915(intel_dp)->drm,
243 		"[CONNECTOR:%d:%s][ENCODER:%d:%s] Invalid DPCD max lane count (%d), using default\n",
244 		connector->base.base.id, connector->base.name,
245 		encoder->base.base.id, encoder->base.name,
246 		intel_dp->max_sink_lane_count);
247 
248 	intel_dp_set_default_max_sink_lane_count(intel_dp);
249 }
250 
251 /* Get length of rates array potentially limited by max_rate. */
252 static int intel_dp_rate_limit_len(const int *rates, int len, int max_rate)
253 {
254 	int i;
255 
256 	/* Limit results by potentially reduced max rate */
257 	for (i = 0; i < len; i++) {
258 		if (rates[len - i - 1] <= max_rate)
259 			return len - i;
260 	}
261 
262 	return 0;
263 }
264 
265 /* Get length of common rates array potentially limited by max_rate. */
266 static int intel_dp_common_len_rate_limit(const struct intel_dp *intel_dp,
267 					  int max_rate)
268 {
269 	return intel_dp_rate_limit_len(intel_dp->common_rates,
270 				       intel_dp->num_common_rates, max_rate);
271 }
272 
273 static int intel_dp_common_rate(struct intel_dp *intel_dp, int index)
274 {
275 	if (drm_WARN_ON(&dp_to_i915(intel_dp)->drm,
276 			index < 0 || index >= intel_dp->num_common_rates))
277 		return 162000;
278 
279 	return intel_dp->common_rates[index];
280 }
281 
282 /* Theoretical max between source and sink */
283 static int intel_dp_max_common_rate(struct intel_dp *intel_dp)
284 {
285 	return intel_dp_common_rate(intel_dp, intel_dp->num_common_rates - 1);
286 }
287 
288 /* Theoretical max between source and sink */
289 static int intel_dp_max_common_lane_count(struct intel_dp *intel_dp)
290 {
291 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
292 	int source_max = dig_port->max_lanes;
293 	int sink_max = intel_dp->max_sink_lane_count;
294 	int fia_max = intel_tc_port_fia_max_lane_count(dig_port);
295 	int lttpr_max = drm_dp_lttpr_max_lane_count(intel_dp->lttpr_common_caps);
296 
297 	if (lttpr_max)
298 		sink_max = min(sink_max, lttpr_max);
299 
300 	return min3(source_max, sink_max, fia_max);
301 }
302 
303 int intel_dp_max_lane_count(struct intel_dp *intel_dp)
304 {
305 	switch (intel_dp->max_link_lane_count) {
306 	case 1:
307 	case 2:
308 	case 4:
309 		return intel_dp->max_link_lane_count;
310 	default:
311 		MISSING_CASE(intel_dp->max_link_lane_count);
312 		return 1;
313 	}
314 }
315 
316 /*
317  * The required data bandwidth for a mode with given pixel clock and bpp. This
318  * is the required net bandwidth independent of the data bandwidth efficiency.
319  */
320 int
321 intel_dp_link_required(int pixel_clock, int bpp)
322 {
323 	/* pixel_clock is in kHz, divide bpp by 8 for bit to Byte conversion */
324 	return DIV_ROUND_UP(pixel_clock * bpp, 8);
325 }
326 
327 /*
328  * Given a link rate and lanes, get the data bandwidth.
329  *
330  * Data bandwidth is the actual payload rate, which depends on the data
331  * bandwidth efficiency and the link rate.
332  *
333  * For 8b/10b channel encoding, SST and non-FEC, the data bandwidth efficiency
334  * is 80%. For example, for a 1.62 Gbps link, 1.62*10^9 bps * 0.80 * (1/8) =
335  * 162000 kBps. With 8-bit symbols, we have 162000 kHz symbol clock. Just by
336  * coincidence, the port clock in kHz matches the data bandwidth in kBps, and
337  * they equal the link bit rate in Gbps multiplied by 100000. (Note that this no
338  * longer holds for data bandwidth as soon as FEC or MST is taken into account!)
339  *
340  * For 128b/132b channel encoding, the data bandwidth efficiency is 96.71%. For
341  * example, for a 10 Gbps link, 10*10^9 bps * 0.9671 * (1/8) = 1208875
342  * kBps. With 32-bit symbols, we have 312500 kHz symbol clock. The value 1000000
343  * does not match the symbol clock, the port clock (not even if you think in
344  * terms of a byte clock), nor the data bandwidth. It only matches the link bit
345  * rate in units of 10000 bps.
346  */
347 int
348 intel_dp_max_data_rate(int max_link_rate, int max_lanes)
349 {
350 	if (max_link_rate >= 1000000) {
351 		/*
352 		 * UHBR rates always use 128b/132b channel encoding, and have
353 		 * 97.71% data bandwidth efficiency. Consider max_link_rate the
354 		 * link bit rate in units of 10000 bps.
355 		 */
356 		int max_link_rate_kbps = max_link_rate * 10;
357 
358 		max_link_rate_kbps = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(max_link_rate_kbps, 9671), 10000);
359 		max_link_rate = max_link_rate_kbps / 8;
360 	}
361 
362 	/*
363 	 * Lower than UHBR rates always use 8b/10b channel encoding, and have
364 	 * 80% data bandwidth efficiency for SST non-FEC. However, this turns
365 	 * out to be a nop by coincidence, and can be skipped:
366 	 *
367 	 *	int max_link_rate_kbps = max_link_rate * 10;
368 	 *	max_link_rate_kbps = DIV_ROUND_CLOSEST_ULL(max_link_rate_kbps * 8, 10);
369 	 *	max_link_rate = max_link_rate_kbps / 8;
370 	 */
371 
372 	return max_link_rate * max_lanes;
373 }
374 
375 bool intel_dp_can_bigjoiner(struct intel_dp *intel_dp)
376 {
377 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
378 	struct intel_encoder *encoder = &intel_dig_port->base;
379 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
380 
381 	return DISPLAY_VER(dev_priv) >= 12 ||
382 		(DISPLAY_VER(dev_priv) == 11 &&
383 		 encoder->port != PORT_A);
384 }
385 
386 static int dg2_max_source_rate(struct intel_dp *intel_dp)
387 {
388 	return intel_dp_is_edp(intel_dp) ? 810000 : 1350000;
389 }
390 
391 static int icl_max_source_rate(struct intel_dp *intel_dp)
392 {
393 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
394 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
395 	enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
396 
397 	if (intel_phy_is_combo(dev_priv, phy) && !intel_dp_is_edp(intel_dp))
398 		return 540000;
399 
400 	return 810000;
401 }
402 
403 static int ehl_max_source_rate(struct intel_dp *intel_dp)
404 {
405 	if (intel_dp_is_edp(intel_dp))
406 		return 540000;
407 
408 	return 810000;
409 }
410 
411 static void
412 intel_dp_set_source_rates(struct intel_dp *intel_dp)
413 {
414 	/* The values must be in increasing order */
415 	static const int icl_rates[] = {
416 		162000, 216000, 270000, 324000, 432000, 540000, 648000, 810000,
417 		1000000, 1350000,
418 	};
419 	static const int bxt_rates[] = {
420 		162000, 216000, 243000, 270000, 324000, 432000, 540000
421 	};
422 	static const int skl_rates[] = {
423 		162000, 216000, 270000, 324000, 432000, 540000
424 	};
425 	static const int hsw_rates[] = {
426 		162000, 270000, 540000
427 	};
428 	static const int g4x_rates[] = {
429 		162000, 270000
430 	};
431 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
432 	struct intel_encoder *encoder = &dig_port->base;
433 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
434 	const int *source_rates;
435 	int size, max_rate = 0, vbt_max_rate;
436 
437 	/* This should only be done once */
438 	drm_WARN_ON(&dev_priv->drm,
439 		    intel_dp->source_rates || intel_dp->num_source_rates);
440 
441 	if (DISPLAY_VER(dev_priv) >= 11) {
442 		source_rates = icl_rates;
443 		size = ARRAY_SIZE(icl_rates);
444 		if (IS_DG2(dev_priv))
445 			max_rate = dg2_max_source_rate(intel_dp);
446 		else if (IS_ALDERLAKE_P(dev_priv) || IS_ALDERLAKE_S(dev_priv) ||
447 			 IS_DG1(dev_priv) || IS_ROCKETLAKE(dev_priv))
448 			max_rate = 810000;
449 		else if (IS_JSL_EHL(dev_priv))
450 			max_rate = ehl_max_source_rate(intel_dp);
451 		else
452 			max_rate = icl_max_source_rate(intel_dp);
453 	} else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
454 		source_rates = bxt_rates;
455 		size = ARRAY_SIZE(bxt_rates);
456 	} else if (DISPLAY_VER(dev_priv) == 9) {
457 		source_rates = skl_rates;
458 		size = ARRAY_SIZE(skl_rates);
459 	} else if ((IS_HASWELL(dev_priv) && !IS_HSW_ULX(dev_priv)) ||
460 		   IS_BROADWELL(dev_priv)) {
461 		source_rates = hsw_rates;
462 		size = ARRAY_SIZE(hsw_rates);
463 	} else {
464 		source_rates = g4x_rates;
465 		size = ARRAY_SIZE(g4x_rates);
466 	}
467 
468 	vbt_max_rate = intel_bios_dp_max_link_rate(encoder);
469 	if (max_rate && vbt_max_rate)
470 		max_rate = min(max_rate, vbt_max_rate);
471 	else if (vbt_max_rate)
472 		max_rate = vbt_max_rate;
473 
474 	if (max_rate)
475 		size = intel_dp_rate_limit_len(source_rates, size, max_rate);
476 
477 	intel_dp->source_rates = source_rates;
478 	intel_dp->num_source_rates = size;
479 }
480 
481 static int intersect_rates(const int *source_rates, int source_len,
482 			   const int *sink_rates, int sink_len,
483 			   int *common_rates)
484 {
485 	int i = 0, j = 0, k = 0;
486 
487 	while (i < source_len && j < sink_len) {
488 		if (source_rates[i] == sink_rates[j]) {
489 			if (WARN_ON(k >= DP_MAX_SUPPORTED_RATES))
490 				return k;
491 			common_rates[k] = source_rates[i];
492 			++k;
493 			++i;
494 			++j;
495 		} else if (source_rates[i] < sink_rates[j]) {
496 			++i;
497 		} else {
498 			++j;
499 		}
500 	}
501 	return k;
502 }
503 
504 /* return index of rate in rates array, or -1 if not found */
505 static int intel_dp_rate_index(const int *rates, int len, int rate)
506 {
507 	int i;
508 
509 	for (i = 0; i < len; i++)
510 		if (rate == rates[i])
511 			return i;
512 
513 	return -1;
514 }
515 
516 static void intel_dp_set_common_rates(struct intel_dp *intel_dp)
517 {
518 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
519 
520 	drm_WARN_ON(&i915->drm,
521 		    !intel_dp->num_source_rates || !intel_dp->num_sink_rates);
522 
523 	intel_dp->num_common_rates = intersect_rates(intel_dp->source_rates,
524 						     intel_dp->num_source_rates,
525 						     intel_dp->sink_rates,
526 						     intel_dp->num_sink_rates,
527 						     intel_dp->common_rates);
528 
529 	/* Paranoia, there should always be something in common. */
530 	if (drm_WARN_ON(&i915->drm, intel_dp->num_common_rates == 0)) {
531 		intel_dp->common_rates[0] = 162000;
532 		intel_dp->num_common_rates = 1;
533 	}
534 }
535 
536 static bool intel_dp_link_params_valid(struct intel_dp *intel_dp, int link_rate,
537 				       u8 lane_count)
538 {
539 	/*
540 	 * FIXME: we need to synchronize the current link parameters with
541 	 * hardware readout. Currently fast link training doesn't work on
542 	 * boot-up.
543 	 */
544 	if (link_rate == 0 ||
545 	    link_rate > intel_dp->max_link_rate)
546 		return false;
547 
548 	if (lane_count == 0 ||
549 	    lane_count > intel_dp_max_lane_count(intel_dp))
550 		return false;
551 
552 	return true;
553 }
554 
555 static bool intel_dp_can_link_train_fallback_for_edp(struct intel_dp *intel_dp,
556 						     int link_rate,
557 						     u8 lane_count)
558 {
559 	/* FIXME figure out what we actually want here */
560 	const struct drm_display_mode *fixed_mode =
561 		intel_panel_preferred_fixed_mode(intel_dp->attached_connector);
562 	int mode_rate, max_rate;
563 
564 	mode_rate = intel_dp_link_required(fixed_mode->clock, 18);
565 	max_rate = intel_dp_max_data_rate(link_rate, lane_count);
566 	if (mode_rate > max_rate)
567 		return false;
568 
569 	return true;
570 }
571 
572 int intel_dp_get_link_train_fallback_values(struct intel_dp *intel_dp,
573 					    int link_rate, u8 lane_count)
574 {
575 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
576 	int index;
577 
578 	/*
579 	 * TODO: Enable fallback on MST links once MST link compute can handle
580 	 * the fallback params.
581 	 */
582 	if (intel_dp->is_mst) {
583 		drm_err(&i915->drm, "Link Training Unsuccessful\n");
584 		return -1;
585 	}
586 
587 	if (intel_dp_is_edp(intel_dp) && !intel_dp->use_max_params) {
588 		drm_dbg_kms(&i915->drm,
589 			    "Retrying Link training for eDP with max parameters\n");
590 		intel_dp->use_max_params = true;
591 		return 0;
592 	}
593 
594 	index = intel_dp_rate_index(intel_dp->common_rates,
595 				    intel_dp->num_common_rates,
596 				    link_rate);
597 	if (index > 0) {
598 		if (intel_dp_is_edp(intel_dp) &&
599 		    !intel_dp_can_link_train_fallback_for_edp(intel_dp,
600 							      intel_dp_common_rate(intel_dp, index - 1),
601 							      lane_count)) {
602 			drm_dbg_kms(&i915->drm,
603 				    "Retrying Link training for eDP with same parameters\n");
604 			return 0;
605 		}
606 		intel_dp->max_link_rate = intel_dp_common_rate(intel_dp, index - 1);
607 		intel_dp->max_link_lane_count = lane_count;
608 	} else if (lane_count > 1) {
609 		if (intel_dp_is_edp(intel_dp) &&
610 		    !intel_dp_can_link_train_fallback_for_edp(intel_dp,
611 							      intel_dp_max_common_rate(intel_dp),
612 							      lane_count >> 1)) {
613 			drm_dbg_kms(&i915->drm,
614 				    "Retrying Link training for eDP with same parameters\n");
615 			return 0;
616 		}
617 		intel_dp->max_link_rate = intel_dp_max_common_rate(intel_dp);
618 		intel_dp->max_link_lane_count = lane_count >> 1;
619 	} else {
620 		drm_err(&i915->drm, "Link Training Unsuccessful\n");
621 		return -1;
622 	}
623 
624 	return 0;
625 }
626 
627 u32 intel_dp_mode_to_fec_clock(u32 mode_clock)
628 {
629 	return div_u64(mul_u32_u32(mode_clock, 1000000U),
630 		       DP_DSC_FEC_OVERHEAD_FACTOR);
631 }
632 
633 static int
634 small_joiner_ram_size_bits(struct drm_i915_private *i915)
635 {
636 	if (DISPLAY_VER(i915) >= 13)
637 		return 17280 * 8;
638 	else if (DISPLAY_VER(i915) >= 11)
639 		return 7680 * 8;
640 	else
641 		return 6144 * 8;
642 }
643 
644 static u16 intel_dp_dsc_get_output_bpp(struct drm_i915_private *i915,
645 				       u32 link_clock, u32 lane_count,
646 				       u32 mode_clock, u32 mode_hdisplay,
647 				       bool bigjoiner,
648 				       u32 pipe_bpp)
649 {
650 	u32 bits_per_pixel, max_bpp_small_joiner_ram;
651 	int i;
652 
653 	/*
654 	 * Available Link Bandwidth(Kbits/sec) = (NumberOfLanes)*
655 	 * (LinkSymbolClock)* 8 * (TimeSlotsPerMTP)
656 	 * for SST -> TimeSlotsPerMTP is 1,
657 	 * for MST -> TimeSlotsPerMTP has to be calculated
658 	 */
659 	bits_per_pixel = (link_clock * lane_count * 8) /
660 			 intel_dp_mode_to_fec_clock(mode_clock);
661 	drm_dbg_kms(&i915->drm, "Max link bpp: %u\n", bits_per_pixel);
662 
663 	/* Small Joiner Check: output bpp <= joiner RAM (bits) / Horiz. width */
664 	max_bpp_small_joiner_ram = small_joiner_ram_size_bits(i915) /
665 		mode_hdisplay;
666 
667 	if (bigjoiner)
668 		max_bpp_small_joiner_ram *= 2;
669 
670 	drm_dbg_kms(&i915->drm, "Max small joiner bpp: %u\n",
671 		    max_bpp_small_joiner_ram);
672 
673 	/*
674 	 * Greatest allowed DSC BPP = MIN (output BPP from available Link BW
675 	 * check, output bpp from small joiner RAM check)
676 	 */
677 	bits_per_pixel = min(bits_per_pixel, max_bpp_small_joiner_ram);
678 
679 	if (bigjoiner) {
680 		u32 max_bpp_bigjoiner =
681 			i915->max_cdclk_freq * 48 /
682 			intel_dp_mode_to_fec_clock(mode_clock);
683 
684 		drm_dbg_kms(&i915->drm, "Max big joiner bpp: %u\n", max_bpp_bigjoiner);
685 		bits_per_pixel = min(bits_per_pixel, max_bpp_bigjoiner);
686 	}
687 
688 	/* Error out if the max bpp is less than smallest allowed valid bpp */
689 	if (bits_per_pixel < valid_dsc_bpp[0]) {
690 		drm_dbg_kms(&i915->drm, "Unsupported BPP %u, min %u\n",
691 			    bits_per_pixel, valid_dsc_bpp[0]);
692 		return 0;
693 	}
694 
695 	/* From XE_LPD onwards we support from bpc upto uncompressed bpp-1 BPPs */
696 	if (DISPLAY_VER(i915) >= 13) {
697 		bits_per_pixel = min(bits_per_pixel, pipe_bpp - 1);
698 	} else {
699 		/* Find the nearest match in the array of known BPPs from VESA */
700 		for (i = 0; i < ARRAY_SIZE(valid_dsc_bpp) - 1; i++) {
701 			if (bits_per_pixel < valid_dsc_bpp[i + 1])
702 				break;
703 		}
704 		bits_per_pixel = valid_dsc_bpp[i];
705 	}
706 
707 	/*
708 	 * Compressed BPP in U6.4 format so multiply by 16, for Gen 11,
709 	 * fractional part is 0
710 	 */
711 	return bits_per_pixel << 4;
712 }
713 
714 static u8 intel_dp_dsc_get_slice_count(struct intel_dp *intel_dp,
715 				       int mode_clock, int mode_hdisplay,
716 				       bool bigjoiner)
717 {
718 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
719 	u8 min_slice_count, i;
720 	int max_slice_width;
721 
722 	if (mode_clock <= DP_DSC_PEAK_PIXEL_RATE)
723 		min_slice_count = DIV_ROUND_UP(mode_clock,
724 					       DP_DSC_MAX_ENC_THROUGHPUT_0);
725 	else
726 		min_slice_count = DIV_ROUND_UP(mode_clock,
727 					       DP_DSC_MAX_ENC_THROUGHPUT_1);
728 
729 	max_slice_width = drm_dp_dsc_sink_max_slice_width(intel_dp->dsc_dpcd);
730 	if (max_slice_width < DP_DSC_MIN_SLICE_WIDTH_VALUE) {
731 		drm_dbg_kms(&i915->drm,
732 			    "Unsupported slice width %d by DP DSC Sink device\n",
733 			    max_slice_width);
734 		return 0;
735 	}
736 	/* Also take into account max slice width */
737 	min_slice_count = max_t(u8, min_slice_count,
738 				DIV_ROUND_UP(mode_hdisplay,
739 					     max_slice_width));
740 
741 	/* Find the closest match to the valid slice count values */
742 	for (i = 0; i < ARRAY_SIZE(valid_dsc_slicecount); i++) {
743 		u8 test_slice_count = valid_dsc_slicecount[i] << bigjoiner;
744 
745 		if (test_slice_count >
746 		    drm_dp_dsc_sink_max_slice_count(intel_dp->dsc_dpcd, false))
747 			break;
748 
749 		/* big joiner needs small joiner to be enabled */
750 		if (bigjoiner && test_slice_count < 4)
751 			continue;
752 
753 		if (min_slice_count <= test_slice_count)
754 			return test_slice_count;
755 	}
756 
757 	drm_dbg_kms(&i915->drm, "Unsupported Slice Count %d\n",
758 		    min_slice_count);
759 	return 0;
760 }
761 
762 static enum intel_output_format
763 intel_dp_output_format(struct intel_connector *connector,
764 		       bool ycbcr_420_output)
765 {
766 	struct intel_dp *intel_dp = intel_attached_dp(connector);
767 
768 	if (!connector->base.ycbcr_420_allowed || !ycbcr_420_output)
769 		return INTEL_OUTPUT_FORMAT_RGB;
770 
771 	if (intel_dp->dfp.rgb_to_ycbcr &&
772 	    intel_dp->dfp.ycbcr_444_to_420)
773 		return INTEL_OUTPUT_FORMAT_RGB;
774 
775 	if (intel_dp->dfp.ycbcr_444_to_420)
776 		return INTEL_OUTPUT_FORMAT_YCBCR444;
777 	else
778 		return INTEL_OUTPUT_FORMAT_YCBCR420;
779 }
780 
781 int intel_dp_min_bpp(enum intel_output_format output_format)
782 {
783 	if (output_format == INTEL_OUTPUT_FORMAT_RGB)
784 		return 6 * 3;
785 	else
786 		return 8 * 3;
787 }
788 
789 static int intel_dp_output_bpp(enum intel_output_format output_format, int bpp)
790 {
791 	/*
792 	 * bpp value was assumed to RGB format. And YCbCr 4:2:0 output
793 	 * format of the number of bytes per pixel will be half the number
794 	 * of bytes of RGB pixel.
795 	 */
796 	if (output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
797 		bpp /= 2;
798 
799 	return bpp;
800 }
801 
802 static int
803 intel_dp_mode_min_output_bpp(struct intel_connector *connector,
804 			     const struct drm_display_mode *mode)
805 {
806 	const struct drm_display_info *info = &connector->base.display_info;
807 	enum intel_output_format output_format =
808 		intel_dp_output_format(connector, drm_mode_is_420_only(info, mode));
809 
810 	return intel_dp_output_bpp(output_format, intel_dp_min_bpp(output_format));
811 }
812 
813 static bool intel_dp_hdisplay_bad(struct drm_i915_private *dev_priv,
814 				  int hdisplay)
815 {
816 	/*
817 	 * Older platforms don't like hdisplay==4096 with DP.
818 	 *
819 	 * On ILK/SNB/IVB the pipe seems to be somewhat running (scanline
820 	 * and frame counter increment), but we don't get vblank interrupts,
821 	 * and the pipe underruns immediately. The link also doesn't seem
822 	 * to get trained properly.
823 	 *
824 	 * On CHV the vblank interrupts don't seem to disappear but
825 	 * otherwise the symptoms are similar.
826 	 *
827 	 * TODO: confirm the behaviour on HSW+
828 	 */
829 	return hdisplay == 4096 && !HAS_DDI(dev_priv);
830 }
831 
832 static int intel_dp_max_tmds_clock(struct intel_dp *intel_dp)
833 {
834 	struct intel_connector *connector = intel_dp->attached_connector;
835 	const struct drm_display_info *info = &connector->base.display_info;
836 	int max_tmds_clock = intel_dp->dfp.max_tmds_clock;
837 
838 	/* Only consider the sink's max TMDS clock if we know this is a HDMI DFP */
839 	if (max_tmds_clock && info->max_tmds_clock)
840 		max_tmds_clock = min(max_tmds_clock, info->max_tmds_clock);
841 
842 	return max_tmds_clock;
843 }
844 
845 static enum drm_mode_status
846 intel_dp_tmds_clock_valid(struct intel_dp *intel_dp,
847 			  int clock, int bpc, bool ycbcr420_output,
848 			  bool respect_downstream_limits)
849 {
850 	int tmds_clock, min_tmds_clock, max_tmds_clock;
851 
852 	if (!respect_downstream_limits)
853 		return MODE_OK;
854 
855 	tmds_clock = intel_hdmi_tmds_clock(clock, bpc, ycbcr420_output);
856 
857 	min_tmds_clock = intel_dp->dfp.min_tmds_clock;
858 	max_tmds_clock = intel_dp_max_tmds_clock(intel_dp);
859 
860 	if (min_tmds_clock && tmds_clock < min_tmds_clock)
861 		return MODE_CLOCK_LOW;
862 
863 	if (max_tmds_clock && tmds_clock > max_tmds_clock)
864 		return MODE_CLOCK_HIGH;
865 
866 	return MODE_OK;
867 }
868 
869 static enum drm_mode_status
870 intel_dp_mode_valid_downstream(struct intel_connector *connector,
871 			       const struct drm_display_mode *mode,
872 			       int target_clock)
873 {
874 	struct intel_dp *intel_dp = intel_attached_dp(connector);
875 	const struct drm_display_info *info = &connector->base.display_info;
876 	enum drm_mode_status status;
877 	bool ycbcr_420_only;
878 
879 	/* If PCON supports FRL MODE, check FRL bandwidth constraints */
880 	if (intel_dp->dfp.pcon_max_frl_bw) {
881 		int target_bw;
882 		int max_frl_bw;
883 		int bpp = intel_dp_mode_min_output_bpp(connector, mode);
884 
885 		target_bw = bpp * target_clock;
886 
887 		max_frl_bw = intel_dp->dfp.pcon_max_frl_bw;
888 
889 		/* converting bw from Gbps to Kbps*/
890 		max_frl_bw = max_frl_bw * 1000000;
891 
892 		if (target_bw > max_frl_bw)
893 			return MODE_CLOCK_HIGH;
894 
895 		return MODE_OK;
896 	}
897 
898 	if (intel_dp->dfp.max_dotclock &&
899 	    target_clock > intel_dp->dfp.max_dotclock)
900 		return MODE_CLOCK_HIGH;
901 
902 	ycbcr_420_only = drm_mode_is_420_only(info, mode);
903 
904 	/* Assume 8bpc for the DP++/HDMI/DVI TMDS clock check */
905 	status = intel_dp_tmds_clock_valid(intel_dp, target_clock,
906 					   8, ycbcr_420_only, true);
907 
908 	if (status != MODE_OK) {
909 		if (ycbcr_420_only ||
910 		    !connector->base.ycbcr_420_allowed ||
911 		    !drm_mode_is_420_also(info, mode))
912 			return status;
913 
914 		status = intel_dp_tmds_clock_valid(intel_dp, target_clock,
915 						   8, true, true);
916 		if (status != MODE_OK)
917 			return status;
918 	}
919 
920 	return MODE_OK;
921 }
922 
923 static bool intel_dp_need_bigjoiner(struct intel_dp *intel_dp,
924 				    int hdisplay, int clock)
925 {
926 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
927 
928 	if (!intel_dp_can_bigjoiner(intel_dp))
929 		return false;
930 
931 	return clock > i915->max_dotclk_freq || hdisplay > 5120;
932 }
933 
934 static enum drm_mode_status
935 intel_dp_mode_valid(struct drm_connector *_connector,
936 		    struct drm_display_mode *mode)
937 {
938 	struct intel_connector *connector = to_intel_connector(_connector);
939 	struct intel_dp *intel_dp = intel_attached_dp(connector);
940 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
941 	const struct drm_display_mode *fixed_mode;
942 	int target_clock = mode->clock;
943 	int max_rate, mode_rate, max_lanes, max_link_clock;
944 	int max_dotclk = dev_priv->max_dotclk_freq;
945 	u16 dsc_max_output_bpp = 0;
946 	u8 dsc_slice_count = 0;
947 	enum drm_mode_status status;
948 	bool dsc = false, bigjoiner = false;
949 
950 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
951 		return MODE_NO_DBLESCAN;
952 
953 	if (mode->flags & DRM_MODE_FLAG_DBLCLK)
954 		return MODE_H_ILLEGAL;
955 
956 	fixed_mode = intel_panel_fixed_mode(connector, mode);
957 	if (intel_dp_is_edp(intel_dp) && fixed_mode) {
958 		status = intel_panel_mode_valid(connector, mode);
959 		if (status != MODE_OK)
960 			return status;
961 
962 		target_clock = fixed_mode->clock;
963 	}
964 
965 	if (mode->clock < 10000)
966 		return MODE_CLOCK_LOW;
967 
968 	if (intel_dp_need_bigjoiner(intel_dp, mode->hdisplay, target_clock)) {
969 		bigjoiner = true;
970 		max_dotclk *= 2;
971 	}
972 	if (target_clock > max_dotclk)
973 		return MODE_CLOCK_HIGH;
974 
975 	max_link_clock = intel_dp_max_link_rate(intel_dp);
976 	max_lanes = intel_dp_max_lane_count(intel_dp);
977 
978 	max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
979 	mode_rate = intel_dp_link_required(target_clock,
980 					   intel_dp_mode_min_output_bpp(connector, mode));
981 
982 	if (intel_dp_hdisplay_bad(dev_priv, mode->hdisplay))
983 		return MODE_H_ILLEGAL;
984 
985 	/*
986 	 * Output bpp is stored in 6.4 format so right shift by 4 to get the
987 	 * integer value since we support only integer values of bpp.
988 	 */
989 	if (DISPLAY_VER(dev_priv) >= 10 &&
990 	    drm_dp_sink_supports_dsc(intel_dp->dsc_dpcd)) {
991 		/*
992 		 * TBD pass the connector BPC,
993 		 * for now U8_MAX so that max BPC on that platform would be picked
994 		 */
995 		int pipe_bpp = intel_dp_dsc_compute_bpp(intel_dp, U8_MAX);
996 
997 		if (intel_dp_is_edp(intel_dp)) {
998 			dsc_max_output_bpp =
999 				drm_edp_dsc_sink_output_bpp(intel_dp->dsc_dpcd) >> 4;
1000 			dsc_slice_count =
1001 				drm_dp_dsc_sink_max_slice_count(intel_dp->dsc_dpcd,
1002 								true);
1003 		} else if (drm_dp_sink_supports_fec(intel_dp->fec_capable)) {
1004 			dsc_max_output_bpp =
1005 				intel_dp_dsc_get_output_bpp(dev_priv,
1006 							    max_link_clock,
1007 							    max_lanes,
1008 							    target_clock,
1009 							    mode->hdisplay,
1010 							    bigjoiner,
1011 							    pipe_bpp) >> 4;
1012 			dsc_slice_count =
1013 				intel_dp_dsc_get_slice_count(intel_dp,
1014 							     target_clock,
1015 							     mode->hdisplay,
1016 							     bigjoiner);
1017 		}
1018 
1019 		dsc = dsc_max_output_bpp && dsc_slice_count;
1020 	}
1021 
1022 	/*
1023 	 * Big joiner configuration needs DSC for TGL which is not true for
1024 	 * XE_LPD where uncompressed joiner is supported.
1025 	 */
1026 	if (DISPLAY_VER(dev_priv) < 13 && bigjoiner && !dsc)
1027 		return MODE_CLOCK_HIGH;
1028 
1029 	if (mode_rate > max_rate && !dsc)
1030 		return MODE_CLOCK_HIGH;
1031 
1032 	status = intel_dp_mode_valid_downstream(connector, mode, target_clock);
1033 	if (status != MODE_OK)
1034 		return status;
1035 
1036 	return intel_mode_valid_max_plane_size(dev_priv, mode, bigjoiner);
1037 }
1038 
1039 bool intel_dp_source_supports_tps3(struct drm_i915_private *i915)
1040 {
1041 	return DISPLAY_VER(i915) >= 9 || IS_BROADWELL(i915) || IS_HASWELL(i915);
1042 }
1043 
1044 bool intel_dp_source_supports_tps4(struct drm_i915_private *i915)
1045 {
1046 	return DISPLAY_VER(i915) >= 10;
1047 }
1048 
1049 static void snprintf_int_array(char *str, size_t len,
1050 			       const int *array, int nelem)
1051 {
1052 	int i;
1053 
1054 	str[0] = '\0';
1055 
1056 	for (i = 0; i < nelem; i++) {
1057 		int r = snprintf(str, len, "%s%d", i ? ", " : "", array[i]);
1058 		if (r >= len)
1059 			return;
1060 		str += r;
1061 		len -= r;
1062 	}
1063 }
1064 
1065 static void intel_dp_print_rates(struct intel_dp *intel_dp)
1066 {
1067 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1068 	char str[128]; /* FIXME: too big for stack? */
1069 
1070 	if (!drm_debug_enabled(DRM_UT_KMS))
1071 		return;
1072 
1073 	snprintf_int_array(str, sizeof(str),
1074 			   intel_dp->source_rates, intel_dp->num_source_rates);
1075 	drm_dbg_kms(&i915->drm, "source rates: %s\n", str);
1076 
1077 	snprintf_int_array(str, sizeof(str),
1078 			   intel_dp->sink_rates, intel_dp->num_sink_rates);
1079 	drm_dbg_kms(&i915->drm, "sink rates: %s\n", str);
1080 
1081 	snprintf_int_array(str, sizeof(str),
1082 			   intel_dp->common_rates, intel_dp->num_common_rates);
1083 	drm_dbg_kms(&i915->drm, "common rates: %s\n", str);
1084 }
1085 
1086 int
1087 intel_dp_max_link_rate(struct intel_dp *intel_dp)
1088 {
1089 	int len;
1090 
1091 	len = intel_dp_common_len_rate_limit(intel_dp, intel_dp->max_link_rate);
1092 
1093 	return intel_dp_common_rate(intel_dp, len - 1);
1094 }
1095 
1096 int intel_dp_rate_select(struct intel_dp *intel_dp, int rate)
1097 {
1098 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1099 	int i = intel_dp_rate_index(intel_dp->sink_rates,
1100 				    intel_dp->num_sink_rates, rate);
1101 
1102 	if (drm_WARN_ON(&i915->drm, i < 0))
1103 		i = 0;
1104 
1105 	return i;
1106 }
1107 
1108 void intel_dp_compute_rate(struct intel_dp *intel_dp, int port_clock,
1109 			   u8 *link_bw, u8 *rate_select)
1110 {
1111 	/* eDP 1.4 rate select method. */
1112 	if (intel_dp->use_rate_select) {
1113 		*link_bw = 0;
1114 		*rate_select =
1115 			intel_dp_rate_select(intel_dp, port_clock);
1116 	} else {
1117 		*link_bw = drm_dp_link_rate_to_bw_code(port_clock);
1118 		*rate_select = 0;
1119 	}
1120 }
1121 
1122 static bool intel_dp_source_supports_fec(struct intel_dp *intel_dp,
1123 					 const struct intel_crtc_state *pipe_config)
1124 {
1125 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1126 
1127 	/* On TGL, FEC is supported on all Pipes */
1128 	if (DISPLAY_VER(dev_priv) >= 12)
1129 		return true;
1130 
1131 	if (DISPLAY_VER(dev_priv) == 11 && pipe_config->cpu_transcoder != TRANSCODER_A)
1132 		return true;
1133 
1134 	return false;
1135 }
1136 
1137 static bool intel_dp_supports_fec(struct intel_dp *intel_dp,
1138 				  const struct intel_crtc_state *pipe_config)
1139 {
1140 	return intel_dp_source_supports_fec(intel_dp, pipe_config) &&
1141 		drm_dp_sink_supports_fec(intel_dp->fec_capable);
1142 }
1143 
1144 static bool intel_dp_supports_dsc(struct intel_dp *intel_dp,
1145 				  const struct intel_crtc_state *crtc_state)
1146 {
1147 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP) && !crtc_state->fec_enable)
1148 		return false;
1149 
1150 	return intel_dsc_source_support(crtc_state) &&
1151 		drm_dp_sink_supports_dsc(intel_dp->dsc_dpcd);
1152 }
1153 
1154 static bool intel_dp_is_ycbcr420(struct intel_dp *intel_dp,
1155 				 const struct intel_crtc_state *crtc_state)
1156 {
1157 	return crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
1158 		(crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444 &&
1159 		 intel_dp->dfp.ycbcr_444_to_420);
1160 }
1161 
1162 static int intel_dp_hdmi_compute_bpc(struct intel_dp *intel_dp,
1163 				     const struct intel_crtc_state *crtc_state,
1164 				     int bpc, bool respect_downstream_limits)
1165 {
1166 	bool ycbcr420_output = intel_dp_is_ycbcr420(intel_dp, crtc_state);
1167 	int clock = crtc_state->hw.adjusted_mode.crtc_clock;
1168 
1169 	/*
1170 	 * Current bpc could already be below 8bpc due to
1171 	 * FDI bandwidth constraints or other limits.
1172 	 * HDMI minimum is 8bpc however.
1173 	 */
1174 	bpc = max(bpc, 8);
1175 
1176 	/*
1177 	 * We will never exceed downstream TMDS clock limits while
1178 	 * attempting deep color. If the user insists on forcing an
1179 	 * out of spec mode they will have to be satisfied with 8bpc.
1180 	 */
1181 	if (!respect_downstream_limits)
1182 		bpc = 8;
1183 
1184 	for (; bpc >= 8; bpc -= 2) {
1185 		if (intel_hdmi_bpc_possible(crtc_state, bpc,
1186 					    intel_dp->has_hdmi_sink, ycbcr420_output) &&
1187 		    intel_dp_tmds_clock_valid(intel_dp, clock, bpc, ycbcr420_output,
1188 					      respect_downstream_limits) == MODE_OK)
1189 			return bpc;
1190 	}
1191 
1192 	return -EINVAL;
1193 }
1194 
1195 static int intel_dp_max_bpp(struct intel_dp *intel_dp,
1196 			    const struct intel_crtc_state *crtc_state,
1197 			    bool respect_downstream_limits)
1198 {
1199 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1200 	struct intel_connector *intel_connector = intel_dp->attached_connector;
1201 	int bpp, bpc;
1202 
1203 	bpc = crtc_state->pipe_bpp / 3;
1204 
1205 	if (intel_dp->dfp.max_bpc)
1206 		bpc = min_t(int, bpc, intel_dp->dfp.max_bpc);
1207 
1208 	if (intel_dp->dfp.min_tmds_clock) {
1209 		int max_hdmi_bpc;
1210 
1211 		max_hdmi_bpc = intel_dp_hdmi_compute_bpc(intel_dp, crtc_state, bpc,
1212 							 respect_downstream_limits);
1213 		if (max_hdmi_bpc < 0)
1214 			return 0;
1215 
1216 		bpc = min(bpc, max_hdmi_bpc);
1217 	}
1218 
1219 	bpp = bpc * 3;
1220 	if (intel_dp_is_edp(intel_dp)) {
1221 		/* Get bpp from vbt only for panels that dont have bpp in edid */
1222 		if (intel_connector->base.display_info.bpc == 0 &&
1223 		    dev_priv->vbt.edp.bpp && dev_priv->vbt.edp.bpp < bpp) {
1224 			drm_dbg_kms(&dev_priv->drm,
1225 				    "clamping bpp for eDP panel to BIOS-provided %i\n",
1226 				    dev_priv->vbt.edp.bpp);
1227 			bpp = dev_priv->vbt.edp.bpp;
1228 		}
1229 	}
1230 
1231 	return bpp;
1232 }
1233 
1234 /* Adjust link config limits based on compliance test requests. */
1235 void
1236 intel_dp_adjust_compliance_config(struct intel_dp *intel_dp,
1237 				  struct intel_crtc_state *pipe_config,
1238 				  struct link_config_limits *limits)
1239 {
1240 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1241 
1242 	/* For DP Compliance we override the computed bpp for the pipe */
1243 	if (intel_dp->compliance.test_data.bpc != 0) {
1244 		int bpp = 3 * intel_dp->compliance.test_data.bpc;
1245 
1246 		limits->min_bpp = limits->max_bpp = bpp;
1247 		pipe_config->dither_force_disable = bpp == 6 * 3;
1248 
1249 		drm_dbg_kms(&i915->drm, "Setting pipe_bpp to %d\n", bpp);
1250 	}
1251 
1252 	/* Use values requested by Compliance Test Request */
1253 	if (intel_dp->compliance.test_type == DP_TEST_LINK_TRAINING) {
1254 		int index;
1255 
1256 		/* Validate the compliance test data since max values
1257 		 * might have changed due to link train fallback.
1258 		 */
1259 		if (intel_dp_link_params_valid(intel_dp, intel_dp->compliance.test_link_rate,
1260 					       intel_dp->compliance.test_lane_count)) {
1261 			index = intel_dp_rate_index(intel_dp->common_rates,
1262 						    intel_dp->num_common_rates,
1263 						    intel_dp->compliance.test_link_rate);
1264 			if (index >= 0)
1265 				limits->min_rate = limits->max_rate =
1266 					intel_dp->compliance.test_link_rate;
1267 			limits->min_lane_count = limits->max_lane_count =
1268 				intel_dp->compliance.test_lane_count;
1269 		}
1270 	}
1271 }
1272 
1273 /* Optimize link config in order: max bpp, min clock, min lanes */
1274 static int
1275 intel_dp_compute_link_config_wide(struct intel_dp *intel_dp,
1276 				  struct intel_crtc_state *pipe_config,
1277 				  const struct link_config_limits *limits)
1278 {
1279 	struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
1280 	int bpp, i, lane_count;
1281 	int mode_rate, link_rate, link_avail;
1282 
1283 	for (bpp = limits->max_bpp; bpp >= limits->min_bpp; bpp -= 2 * 3) {
1284 		int output_bpp = intel_dp_output_bpp(pipe_config->output_format, bpp);
1285 
1286 		mode_rate = intel_dp_link_required(adjusted_mode->crtc_clock,
1287 						   output_bpp);
1288 
1289 		for (i = 0; i < intel_dp->num_common_rates; i++) {
1290 			link_rate = intel_dp_common_rate(intel_dp, i);
1291 			if (link_rate < limits->min_rate ||
1292 			    link_rate > limits->max_rate)
1293 				continue;
1294 
1295 			for (lane_count = limits->min_lane_count;
1296 			     lane_count <= limits->max_lane_count;
1297 			     lane_count <<= 1) {
1298 				link_avail = intel_dp_max_data_rate(link_rate,
1299 								    lane_count);
1300 
1301 				if (mode_rate <= link_avail) {
1302 					pipe_config->lane_count = lane_count;
1303 					pipe_config->pipe_bpp = bpp;
1304 					pipe_config->port_clock = link_rate;
1305 
1306 					return 0;
1307 				}
1308 			}
1309 		}
1310 	}
1311 
1312 	return -EINVAL;
1313 }
1314 
1315 static int intel_dp_dsc_compute_bpp(struct intel_dp *intel_dp, u8 max_req_bpc)
1316 {
1317 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1318 	int i, num_bpc;
1319 	u8 dsc_bpc[3] = {0};
1320 	u8 dsc_max_bpc;
1321 
1322 	/* Max DSC Input BPC for ICL is 10 and for TGL+ is 12 */
1323 	if (DISPLAY_VER(i915) >= 12)
1324 		dsc_max_bpc = min_t(u8, 12, max_req_bpc);
1325 	else
1326 		dsc_max_bpc = min_t(u8, 10, max_req_bpc);
1327 
1328 	num_bpc = drm_dp_dsc_sink_supported_input_bpcs(intel_dp->dsc_dpcd,
1329 						       dsc_bpc);
1330 	for (i = 0; i < num_bpc; i++) {
1331 		if (dsc_max_bpc >= dsc_bpc[i])
1332 			return dsc_bpc[i] * 3;
1333 	}
1334 
1335 	return 0;
1336 }
1337 
1338 #define DSC_SUPPORTED_VERSION_MIN		1
1339 
1340 static int intel_dp_dsc_compute_params(struct intel_encoder *encoder,
1341 				       struct intel_crtc_state *crtc_state)
1342 {
1343 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1344 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1345 	struct drm_dsc_config *vdsc_cfg = &crtc_state->dsc.config;
1346 	u8 line_buf_depth;
1347 	int ret;
1348 
1349 	/*
1350 	 * RC_MODEL_SIZE is currently a constant across all configurations.
1351 	 *
1352 	 * FIXME: Look into using sink defined DPCD DP_DSC_RC_BUF_BLK_SIZE and
1353 	 * DP_DSC_RC_BUF_SIZE for this.
1354 	 */
1355 	vdsc_cfg->rc_model_size = DSC_RC_MODEL_SIZE_CONST;
1356 
1357 	/*
1358 	 * Slice Height of 8 works for all currently available panels. So start
1359 	 * with that if pic_height is an integral multiple of 8. Eventually add
1360 	 * logic to try multiple slice heights.
1361 	 */
1362 	if (vdsc_cfg->pic_height % 8 == 0)
1363 		vdsc_cfg->slice_height = 8;
1364 	else if (vdsc_cfg->pic_height % 4 == 0)
1365 		vdsc_cfg->slice_height = 4;
1366 	else
1367 		vdsc_cfg->slice_height = 2;
1368 
1369 	ret = intel_dsc_compute_params(crtc_state);
1370 	if (ret)
1371 		return ret;
1372 
1373 	vdsc_cfg->dsc_version_major =
1374 		(intel_dp->dsc_dpcd[DP_DSC_REV - DP_DSC_SUPPORT] &
1375 		 DP_DSC_MAJOR_MASK) >> DP_DSC_MAJOR_SHIFT;
1376 	vdsc_cfg->dsc_version_minor =
1377 		min(DSC_SUPPORTED_VERSION_MIN,
1378 		    (intel_dp->dsc_dpcd[DP_DSC_REV - DP_DSC_SUPPORT] &
1379 		     DP_DSC_MINOR_MASK) >> DP_DSC_MINOR_SHIFT);
1380 
1381 	vdsc_cfg->convert_rgb = intel_dp->dsc_dpcd[DP_DSC_DEC_COLOR_FORMAT_CAP - DP_DSC_SUPPORT] &
1382 		DP_DSC_RGB;
1383 
1384 	line_buf_depth = drm_dp_dsc_sink_line_buf_depth(intel_dp->dsc_dpcd);
1385 	if (!line_buf_depth) {
1386 		drm_dbg_kms(&i915->drm,
1387 			    "DSC Sink Line Buffer Depth invalid\n");
1388 		return -EINVAL;
1389 	}
1390 
1391 	if (vdsc_cfg->dsc_version_minor == 2)
1392 		vdsc_cfg->line_buf_depth = (line_buf_depth == DSC_1_2_MAX_LINEBUF_DEPTH_BITS) ?
1393 			DSC_1_2_MAX_LINEBUF_DEPTH_VAL : line_buf_depth;
1394 	else
1395 		vdsc_cfg->line_buf_depth = (line_buf_depth > DSC_1_1_MAX_LINEBUF_DEPTH_BITS) ?
1396 			DSC_1_1_MAX_LINEBUF_DEPTH_BITS : line_buf_depth;
1397 
1398 	vdsc_cfg->block_pred_enable =
1399 		intel_dp->dsc_dpcd[DP_DSC_BLK_PREDICTION_SUPPORT - DP_DSC_SUPPORT] &
1400 		DP_DSC_BLK_PREDICTION_IS_SUPPORTED;
1401 
1402 	return drm_dsc_compute_rc_parameters(vdsc_cfg);
1403 }
1404 
1405 static int intel_dp_dsc_compute_config(struct intel_dp *intel_dp,
1406 				       struct intel_crtc_state *pipe_config,
1407 				       struct drm_connector_state *conn_state,
1408 				       struct link_config_limits *limits)
1409 {
1410 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1411 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
1412 	const struct drm_display_mode *adjusted_mode =
1413 		&pipe_config->hw.adjusted_mode;
1414 	int pipe_bpp;
1415 	int ret;
1416 
1417 	pipe_config->fec_enable = !intel_dp_is_edp(intel_dp) &&
1418 		intel_dp_supports_fec(intel_dp, pipe_config);
1419 
1420 	if (!intel_dp_supports_dsc(intel_dp, pipe_config))
1421 		return -EINVAL;
1422 
1423 	pipe_bpp = intel_dp_dsc_compute_bpp(intel_dp, conn_state->max_requested_bpc);
1424 
1425 	/* Min Input BPC for ICL+ is 8 */
1426 	if (pipe_bpp < 8 * 3) {
1427 		drm_dbg_kms(&dev_priv->drm,
1428 			    "No DSC support for less than 8bpc\n");
1429 		return -EINVAL;
1430 	}
1431 
1432 	/*
1433 	 * For now enable DSC for max bpp, max link rate, max lane count.
1434 	 * Optimize this later for the minimum possible link rate/lane count
1435 	 * with DSC enabled for the requested mode.
1436 	 */
1437 	pipe_config->pipe_bpp = pipe_bpp;
1438 	pipe_config->port_clock = limits->max_rate;
1439 	pipe_config->lane_count = limits->max_lane_count;
1440 
1441 	if (intel_dp_is_edp(intel_dp)) {
1442 		pipe_config->dsc.compressed_bpp =
1443 			min_t(u16, drm_edp_dsc_sink_output_bpp(intel_dp->dsc_dpcd) >> 4,
1444 			      pipe_config->pipe_bpp);
1445 		pipe_config->dsc.slice_count =
1446 			drm_dp_dsc_sink_max_slice_count(intel_dp->dsc_dpcd,
1447 							true);
1448 	} else {
1449 		u16 dsc_max_output_bpp;
1450 		u8 dsc_dp_slice_count;
1451 
1452 		dsc_max_output_bpp =
1453 			intel_dp_dsc_get_output_bpp(dev_priv,
1454 						    pipe_config->port_clock,
1455 						    pipe_config->lane_count,
1456 						    adjusted_mode->crtc_clock,
1457 						    adjusted_mode->crtc_hdisplay,
1458 						    pipe_config->bigjoiner_pipes,
1459 						    pipe_bpp);
1460 		dsc_dp_slice_count =
1461 			intel_dp_dsc_get_slice_count(intel_dp,
1462 						     adjusted_mode->crtc_clock,
1463 						     adjusted_mode->crtc_hdisplay,
1464 						     pipe_config->bigjoiner_pipes);
1465 		if (!dsc_max_output_bpp || !dsc_dp_slice_count) {
1466 			drm_dbg_kms(&dev_priv->drm,
1467 				    "Compressed BPP/Slice Count not supported\n");
1468 			return -EINVAL;
1469 		}
1470 		pipe_config->dsc.compressed_bpp = min_t(u16,
1471 							       dsc_max_output_bpp >> 4,
1472 							       pipe_config->pipe_bpp);
1473 		pipe_config->dsc.slice_count = dsc_dp_slice_count;
1474 	}
1475 
1476 	/* As of today we support DSC for only RGB */
1477 	if (intel_dp->force_dsc_bpp) {
1478 		if (intel_dp->force_dsc_bpp >= 8 &&
1479 		    intel_dp->force_dsc_bpp < pipe_bpp) {
1480 			drm_dbg_kms(&dev_priv->drm,
1481 				    "DSC BPP forced to %d",
1482 				    intel_dp->force_dsc_bpp);
1483 			pipe_config->dsc.compressed_bpp =
1484 						intel_dp->force_dsc_bpp;
1485 		} else {
1486 			drm_dbg_kms(&dev_priv->drm,
1487 				    "Invalid DSC BPP %d",
1488 				    intel_dp->force_dsc_bpp);
1489 		}
1490 	}
1491 
1492 	/*
1493 	 * VDSC engine operates at 1 Pixel per clock, so if peak pixel rate
1494 	 * is greater than the maximum Cdclock and if slice count is even
1495 	 * then we need to use 2 VDSC instances.
1496 	 */
1497 	if (adjusted_mode->crtc_clock > dev_priv->max_cdclk_freq ||
1498 	    pipe_config->bigjoiner_pipes) {
1499 		if (pipe_config->dsc.slice_count < 2) {
1500 			drm_dbg_kms(&dev_priv->drm,
1501 				    "Cannot split stream to use 2 VDSC instances\n");
1502 			return -EINVAL;
1503 		}
1504 
1505 		pipe_config->dsc.dsc_split = true;
1506 	}
1507 
1508 	ret = intel_dp_dsc_compute_params(&dig_port->base, pipe_config);
1509 	if (ret < 0) {
1510 		drm_dbg_kms(&dev_priv->drm,
1511 			    "Cannot compute valid DSC parameters for Input Bpp = %d "
1512 			    "Compressed BPP = %d\n",
1513 			    pipe_config->pipe_bpp,
1514 			    pipe_config->dsc.compressed_bpp);
1515 		return ret;
1516 	}
1517 
1518 	pipe_config->dsc.compression_enable = true;
1519 	drm_dbg_kms(&dev_priv->drm, "DP DSC computed with Input Bpp = %d "
1520 		    "Compressed Bpp = %d Slice Count = %d\n",
1521 		    pipe_config->pipe_bpp,
1522 		    pipe_config->dsc.compressed_bpp,
1523 		    pipe_config->dsc.slice_count);
1524 
1525 	return 0;
1526 }
1527 
1528 static int
1529 intel_dp_compute_link_config(struct intel_encoder *encoder,
1530 			     struct intel_crtc_state *pipe_config,
1531 			     struct drm_connector_state *conn_state,
1532 			     bool respect_downstream_limits)
1533 {
1534 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1535 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
1536 	const struct drm_display_mode *adjusted_mode =
1537 		&pipe_config->hw.adjusted_mode;
1538 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1539 	struct link_config_limits limits;
1540 	bool joiner_needs_dsc = false;
1541 	int ret;
1542 
1543 	limits.min_rate = intel_dp_common_rate(intel_dp, 0);
1544 	limits.max_rate = intel_dp_max_link_rate(intel_dp);
1545 
1546 	limits.min_lane_count = 1;
1547 	limits.max_lane_count = intel_dp_max_lane_count(intel_dp);
1548 
1549 	limits.min_bpp = intel_dp_min_bpp(pipe_config->output_format);
1550 	limits.max_bpp = intel_dp_max_bpp(intel_dp, pipe_config, respect_downstream_limits);
1551 
1552 	if (intel_dp->use_max_params) {
1553 		/*
1554 		 * Use the maximum clock and number of lanes the eDP panel
1555 		 * advertizes being capable of in case the initial fast
1556 		 * optimal params failed us. The panels are generally
1557 		 * designed to support only a single clock and lane
1558 		 * configuration, and typically on older panels these
1559 		 * values correspond to the native resolution of the panel.
1560 		 */
1561 		limits.min_lane_count = limits.max_lane_count;
1562 		limits.min_rate = limits.max_rate;
1563 	}
1564 
1565 	intel_dp_adjust_compliance_config(intel_dp, pipe_config, &limits);
1566 
1567 	drm_dbg_kms(&i915->drm, "DP link computation with max lane count %i "
1568 		    "max rate %d max bpp %d pixel clock %iKHz\n",
1569 		    limits.max_lane_count, limits.max_rate,
1570 		    limits.max_bpp, adjusted_mode->crtc_clock);
1571 
1572 	if (intel_dp_need_bigjoiner(intel_dp, adjusted_mode->crtc_hdisplay,
1573 				    adjusted_mode->crtc_clock))
1574 		pipe_config->bigjoiner_pipes = GENMASK(crtc->pipe + 1, crtc->pipe);
1575 
1576 	/*
1577 	 * Pipe joiner needs compression up to display 12 due to bandwidth
1578 	 * limitation. DG2 onwards pipe joiner can be enabled without
1579 	 * compression.
1580 	 */
1581 	joiner_needs_dsc = DISPLAY_VER(i915) < 13 && pipe_config->bigjoiner_pipes;
1582 
1583 	/*
1584 	 * Optimize for slow and wide for everything, because there are some
1585 	 * eDP 1.3 and 1.4 panels don't work well with fast and narrow.
1586 	 */
1587 	ret = intel_dp_compute_link_config_wide(intel_dp, pipe_config, &limits);
1588 
1589 	if (ret || joiner_needs_dsc || intel_dp->force_dsc_en) {
1590 		drm_dbg_kms(&i915->drm, "Try DSC (fallback=%s, joiner=%s, force=%s)\n",
1591 			    str_yes_no(ret), str_yes_no(joiner_needs_dsc),
1592 			    str_yes_no(intel_dp->force_dsc_en));
1593 		ret = intel_dp_dsc_compute_config(intel_dp, pipe_config,
1594 						  conn_state, &limits);
1595 		if (ret < 0)
1596 			return ret;
1597 	}
1598 
1599 	if (pipe_config->dsc.compression_enable) {
1600 		drm_dbg_kms(&i915->drm,
1601 			    "DP lane count %d clock %d Input bpp %d Compressed bpp %d\n",
1602 			    pipe_config->lane_count, pipe_config->port_clock,
1603 			    pipe_config->pipe_bpp,
1604 			    pipe_config->dsc.compressed_bpp);
1605 
1606 		drm_dbg_kms(&i915->drm,
1607 			    "DP link rate required %i available %i\n",
1608 			    intel_dp_link_required(adjusted_mode->crtc_clock,
1609 						   pipe_config->dsc.compressed_bpp),
1610 			    intel_dp_max_data_rate(pipe_config->port_clock,
1611 						   pipe_config->lane_count));
1612 	} else {
1613 		drm_dbg_kms(&i915->drm, "DP lane count %d clock %d bpp %d\n",
1614 			    pipe_config->lane_count, pipe_config->port_clock,
1615 			    pipe_config->pipe_bpp);
1616 
1617 		drm_dbg_kms(&i915->drm,
1618 			    "DP link rate required %i available %i\n",
1619 			    intel_dp_link_required(adjusted_mode->crtc_clock,
1620 						   pipe_config->pipe_bpp),
1621 			    intel_dp_max_data_rate(pipe_config->port_clock,
1622 						   pipe_config->lane_count));
1623 	}
1624 	return 0;
1625 }
1626 
1627 bool intel_dp_limited_color_range(const struct intel_crtc_state *crtc_state,
1628 				  const struct drm_connector_state *conn_state)
1629 {
1630 	const struct intel_digital_connector_state *intel_conn_state =
1631 		to_intel_digital_connector_state(conn_state);
1632 	const struct drm_display_mode *adjusted_mode =
1633 		&crtc_state->hw.adjusted_mode;
1634 
1635 	/*
1636 	 * Our YCbCr output is always limited range.
1637 	 * crtc_state->limited_color_range only applies to RGB,
1638 	 * and it must never be set for YCbCr or we risk setting
1639 	 * some conflicting bits in PIPECONF which will mess up
1640 	 * the colors on the monitor.
1641 	 */
1642 	if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
1643 		return false;
1644 
1645 	if (intel_conn_state->broadcast_rgb == INTEL_BROADCAST_RGB_AUTO) {
1646 		/*
1647 		 * See:
1648 		 * CEA-861-E - 5.1 Default Encoding Parameters
1649 		 * VESA DisplayPort Ver.1.2a - 5.1.1.1 Video Colorimetry
1650 		 */
1651 		return crtc_state->pipe_bpp != 18 &&
1652 			drm_default_rgb_quant_range(adjusted_mode) ==
1653 			HDMI_QUANTIZATION_RANGE_LIMITED;
1654 	} else {
1655 		return intel_conn_state->broadcast_rgb ==
1656 			INTEL_BROADCAST_RGB_LIMITED;
1657 	}
1658 }
1659 
1660 static bool intel_dp_port_has_audio(struct drm_i915_private *dev_priv,
1661 				    enum port port)
1662 {
1663 	if (IS_G4X(dev_priv))
1664 		return false;
1665 	if (DISPLAY_VER(dev_priv) < 12 && port == PORT_A)
1666 		return false;
1667 
1668 	return true;
1669 }
1670 
1671 static void intel_dp_compute_vsc_colorimetry(const struct intel_crtc_state *crtc_state,
1672 					     const struct drm_connector_state *conn_state,
1673 					     struct drm_dp_vsc_sdp *vsc)
1674 {
1675 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1676 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1677 
1678 	/*
1679 	 * Prepare VSC Header for SU as per DP 1.4 spec, Table 2-118
1680 	 * VSC SDP supporting 3D stereo, PSR2, and Pixel Encoding/
1681 	 * Colorimetry Format indication.
1682 	 */
1683 	vsc->revision = 0x5;
1684 	vsc->length = 0x13;
1685 
1686 	/* DP 1.4a spec, Table 2-120 */
1687 	switch (crtc_state->output_format) {
1688 	case INTEL_OUTPUT_FORMAT_YCBCR444:
1689 		vsc->pixelformat = DP_PIXELFORMAT_YUV444;
1690 		break;
1691 	case INTEL_OUTPUT_FORMAT_YCBCR420:
1692 		vsc->pixelformat = DP_PIXELFORMAT_YUV420;
1693 		break;
1694 	case INTEL_OUTPUT_FORMAT_RGB:
1695 	default:
1696 		vsc->pixelformat = DP_PIXELFORMAT_RGB;
1697 	}
1698 
1699 	switch (conn_state->colorspace) {
1700 	case DRM_MODE_COLORIMETRY_BT709_YCC:
1701 		vsc->colorimetry = DP_COLORIMETRY_BT709_YCC;
1702 		break;
1703 	case DRM_MODE_COLORIMETRY_XVYCC_601:
1704 		vsc->colorimetry = DP_COLORIMETRY_XVYCC_601;
1705 		break;
1706 	case DRM_MODE_COLORIMETRY_XVYCC_709:
1707 		vsc->colorimetry = DP_COLORIMETRY_XVYCC_709;
1708 		break;
1709 	case DRM_MODE_COLORIMETRY_SYCC_601:
1710 		vsc->colorimetry = DP_COLORIMETRY_SYCC_601;
1711 		break;
1712 	case DRM_MODE_COLORIMETRY_OPYCC_601:
1713 		vsc->colorimetry = DP_COLORIMETRY_OPYCC_601;
1714 		break;
1715 	case DRM_MODE_COLORIMETRY_BT2020_CYCC:
1716 		vsc->colorimetry = DP_COLORIMETRY_BT2020_CYCC;
1717 		break;
1718 	case DRM_MODE_COLORIMETRY_BT2020_RGB:
1719 		vsc->colorimetry = DP_COLORIMETRY_BT2020_RGB;
1720 		break;
1721 	case DRM_MODE_COLORIMETRY_BT2020_YCC:
1722 		vsc->colorimetry = DP_COLORIMETRY_BT2020_YCC;
1723 		break;
1724 	case DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65:
1725 	case DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER:
1726 		vsc->colorimetry = DP_COLORIMETRY_DCI_P3_RGB;
1727 		break;
1728 	default:
1729 		/*
1730 		 * RGB->YCBCR color conversion uses the BT.709
1731 		 * color space.
1732 		 */
1733 		if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
1734 			vsc->colorimetry = DP_COLORIMETRY_BT709_YCC;
1735 		else
1736 			vsc->colorimetry = DP_COLORIMETRY_DEFAULT;
1737 		break;
1738 	}
1739 
1740 	vsc->bpc = crtc_state->pipe_bpp / 3;
1741 
1742 	/* only RGB pixelformat supports 6 bpc */
1743 	drm_WARN_ON(&dev_priv->drm,
1744 		    vsc->bpc == 6 && vsc->pixelformat != DP_PIXELFORMAT_RGB);
1745 
1746 	/* all YCbCr are always limited range */
1747 	vsc->dynamic_range = DP_DYNAMIC_RANGE_CTA;
1748 	vsc->content_type = DP_CONTENT_TYPE_NOT_DEFINED;
1749 }
1750 
1751 static void intel_dp_compute_vsc_sdp(struct intel_dp *intel_dp,
1752 				     struct intel_crtc_state *crtc_state,
1753 				     const struct drm_connector_state *conn_state)
1754 {
1755 	struct drm_dp_vsc_sdp *vsc = &crtc_state->infoframes.vsc;
1756 
1757 	/* When a crtc state has PSR, VSC SDP will be handled by PSR routine */
1758 	if (crtc_state->has_psr)
1759 		return;
1760 
1761 	if (!intel_dp_needs_vsc_sdp(crtc_state, conn_state))
1762 		return;
1763 
1764 	crtc_state->infoframes.enable |= intel_hdmi_infoframe_enable(DP_SDP_VSC);
1765 	vsc->sdp_type = DP_SDP_VSC;
1766 	intel_dp_compute_vsc_colorimetry(crtc_state, conn_state,
1767 					 &crtc_state->infoframes.vsc);
1768 }
1769 
1770 void intel_dp_compute_psr_vsc_sdp(struct intel_dp *intel_dp,
1771 				  const struct intel_crtc_state *crtc_state,
1772 				  const struct drm_connector_state *conn_state,
1773 				  struct drm_dp_vsc_sdp *vsc)
1774 {
1775 	vsc->sdp_type = DP_SDP_VSC;
1776 
1777 	if (crtc_state->has_psr2) {
1778 		if (intel_dp->psr.colorimetry_support &&
1779 		    intel_dp_needs_vsc_sdp(crtc_state, conn_state)) {
1780 			/* [PSR2, +Colorimetry] */
1781 			intel_dp_compute_vsc_colorimetry(crtc_state, conn_state,
1782 							 vsc);
1783 		} else {
1784 			/*
1785 			 * [PSR2, -Colorimetry]
1786 			 * Prepare VSC Header for SU as per eDP 1.4 spec, Table 6-11
1787 			 * 3D stereo + PSR/PSR2 + Y-coordinate.
1788 			 */
1789 			vsc->revision = 0x4;
1790 			vsc->length = 0xe;
1791 		}
1792 	} else {
1793 		/*
1794 		 * [PSR1]
1795 		 * Prepare VSC Header for SU as per DP 1.4 spec, Table 2-118
1796 		 * VSC SDP supporting 3D stereo + PSR (applies to eDP v1.3 or
1797 		 * higher).
1798 		 */
1799 		vsc->revision = 0x2;
1800 		vsc->length = 0x8;
1801 	}
1802 }
1803 
1804 static void
1805 intel_dp_compute_hdr_metadata_infoframe_sdp(struct intel_dp *intel_dp,
1806 					    struct intel_crtc_state *crtc_state,
1807 					    const struct drm_connector_state *conn_state)
1808 {
1809 	int ret;
1810 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1811 	struct hdmi_drm_infoframe *drm_infoframe = &crtc_state->infoframes.drm.drm;
1812 
1813 	if (!conn_state->hdr_output_metadata)
1814 		return;
1815 
1816 	ret = drm_hdmi_infoframe_set_hdr_metadata(drm_infoframe, conn_state);
1817 
1818 	if (ret) {
1819 		drm_dbg_kms(&dev_priv->drm, "couldn't set HDR metadata in infoframe\n");
1820 		return;
1821 	}
1822 
1823 	crtc_state->infoframes.enable |=
1824 		intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GAMUT_METADATA);
1825 }
1826 
1827 static bool cpu_transcoder_has_drrs(struct drm_i915_private *i915,
1828 				    enum transcoder cpu_transcoder)
1829 {
1830 	/* M1/N1 is double buffered */
1831 	if (DISPLAY_VER(i915) >= 9 || IS_BROADWELL(i915))
1832 		return true;
1833 
1834 	return intel_cpu_transcoder_has_m2_n2(i915, cpu_transcoder);
1835 }
1836 
1837 static bool can_enable_drrs(struct intel_connector *connector,
1838 			    const struct intel_crtc_state *pipe_config,
1839 			    const struct drm_display_mode *downclock_mode)
1840 {
1841 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
1842 
1843 	if (pipe_config->vrr.enable)
1844 		return false;
1845 
1846 	/*
1847 	 * DRRS and PSR can't be enable together, so giving preference to PSR
1848 	 * as it allows more power-savings by complete shutting down display,
1849 	 * so to guarantee this, intel_drrs_compute_config() must be called
1850 	 * after intel_psr_compute_config().
1851 	 */
1852 	if (pipe_config->has_psr)
1853 		return false;
1854 
1855 	/* FIXME missing FDI M2/N2 etc. */
1856 	if (pipe_config->has_pch_encoder)
1857 		return false;
1858 
1859 	if (!cpu_transcoder_has_drrs(i915, pipe_config->cpu_transcoder))
1860 		return false;
1861 
1862 	return downclock_mode &&
1863 		intel_panel_drrs_type(connector) == DRRS_TYPE_SEAMLESS;
1864 }
1865 
1866 static void
1867 intel_dp_drrs_compute_config(struct intel_connector *connector,
1868 			     struct intel_crtc_state *pipe_config,
1869 			     int output_bpp, bool constant_n)
1870 {
1871 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
1872 	const struct drm_display_mode *downclock_mode =
1873 		intel_panel_downclock_mode(connector, &pipe_config->hw.adjusted_mode);
1874 	int pixel_clock;
1875 
1876 	if (!can_enable_drrs(connector, pipe_config, downclock_mode)) {
1877 		if (intel_cpu_transcoder_has_m2_n2(i915, pipe_config->cpu_transcoder))
1878 			intel_zero_m_n(&pipe_config->dp_m2_n2);
1879 		return;
1880 	}
1881 
1882 	if (IS_IRONLAKE(i915) || IS_SANDYBRIDGE(i915) || IS_IVYBRIDGE(i915))
1883 		pipe_config->msa_timing_delay = i915->vbt.edp.drrs_msa_timing_delay;
1884 
1885 	pipe_config->has_drrs = true;
1886 
1887 	pixel_clock = downclock_mode->clock;
1888 	if (pipe_config->splitter.enable)
1889 		pixel_clock /= pipe_config->splitter.link_count;
1890 
1891 	intel_link_compute_m_n(output_bpp, pipe_config->lane_count, pixel_clock,
1892 			       pipe_config->port_clock, &pipe_config->dp_m2_n2,
1893 			       constant_n, pipe_config->fec_enable);
1894 
1895 	/* FIXME: abstract this better */
1896 	if (pipe_config->splitter.enable)
1897 		pipe_config->dp_m2_n2.data_m *= pipe_config->splitter.link_count;
1898 }
1899 
1900 static bool intel_dp_has_audio(struct intel_encoder *encoder,
1901 			       const struct intel_crtc_state *crtc_state,
1902 			       const struct drm_connector_state *conn_state)
1903 {
1904 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1905 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1906 	const struct intel_digital_connector_state *intel_conn_state =
1907 		to_intel_digital_connector_state(conn_state);
1908 
1909 	if (!intel_dp_port_has_audio(i915, encoder->port))
1910 		return false;
1911 
1912 	if (intel_conn_state->force_audio == HDMI_AUDIO_AUTO)
1913 		return intel_dp->has_audio;
1914 	else
1915 		return intel_conn_state->force_audio == HDMI_AUDIO_ON;
1916 }
1917 
1918 static int
1919 intel_dp_compute_output_format(struct intel_encoder *encoder,
1920 			       struct intel_crtc_state *crtc_state,
1921 			       struct drm_connector_state *conn_state,
1922 			       bool respect_downstream_limits)
1923 {
1924 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1925 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1926 	struct intel_connector *connector = intel_dp->attached_connector;
1927 	const struct drm_display_info *info = &connector->base.display_info;
1928 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
1929 	bool ycbcr_420_only;
1930 	int ret;
1931 
1932 	ycbcr_420_only = drm_mode_is_420_only(info, adjusted_mode);
1933 
1934 	crtc_state->output_format = intel_dp_output_format(connector, ycbcr_420_only);
1935 
1936 	if (ycbcr_420_only && !intel_dp_is_ycbcr420(intel_dp, crtc_state)) {
1937 		drm_dbg_kms(&i915->drm,
1938 			    "YCbCr 4:2:0 mode but YCbCr 4:2:0 output not possible. Falling back to RGB.\n");
1939 		crtc_state->output_format = INTEL_OUTPUT_FORMAT_RGB;
1940 	}
1941 
1942 	ret = intel_dp_compute_link_config(encoder, crtc_state, conn_state,
1943 					   respect_downstream_limits);
1944 	if (ret) {
1945 		if (intel_dp_is_ycbcr420(intel_dp, crtc_state) ||
1946 		    !connector->base.ycbcr_420_allowed ||
1947 		    !drm_mode_is_420_also(info, adjusted_mode))
1948 			return ret;
1949 
1950 		crtc_state->output_format = intel_dp_output_format(connector, true);
1951 		ret = intel_dp_compute_link_config(encoder, crtc_state, conn_state,
1952 						   respect_downstream_limits);
1953 	}
1954 
1955 	return ret;
1956 }
1957 
1958 int
1959 intel_dp_compute_config(struct intel_encoder *encoder,
1960 			struct intel_crtc_state *pipe_config,
1961 			struct drm_connector_state *conn_state)
1962 {
1963 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1964 	struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
1965 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1966 	const struct drm_display_mode *fixed_mode;
1967 	struct intel_connector *connector = intel_dp->attached_connector;
1968 	bool constant_n = drm_dp_has_quirk(&intel_dp->desc, DP_DPCD_QUIRK_CONSTANT_N);
1969 	int ret = 0, output_bpp;
1970 
1971 	if (HAS_PCH_SPLIT(dev_priv) && !HAS_DDI(dev_priv) && encoder->port != PORT_A)
1972 		pipe_config->has_pch_encoder = true;
1973 
1974 	pipe_config->has_audio = intel_dp_has_audio(encoder, pipe_config, conn_state);
1975 
1976 	fixed_mode = intel_panel_fixed_mode(connector, adjusted_mode);
1977 	if (intel_dp_is_edp(intel_dp) && fixed_mode) {
1978 		ret = intel_panel_compute_config(connector, adjusted_mode);
1979 		if (ret)
1980 			return ret;
1981 	}
1982 
1983 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
1984 		return -EINVAL;
1985 
1986 	if (HAS_GMCH(dev_priv) &&
1987 	    adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
1988 		return -EINVAL;
1989 
1990 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
1991 		return -EINVAL;
1992 
1993 	if (intel_dp_hdisplay_bad(dev_priv, adjusted_mode->crtc_hdisplay))
1994 		return -EINVAL;
1995 
1996 	/*
1997 	 * Try to respect downstream TMDS clock limits first, if
1998 	 * that fails assume the user might know something we don't.
1999 	 */
2000 	ret = intel_dp_compute_output_format(encoder, pipe_config, conn_state, true);
2001 	if (ret)
2002 		ret = intel_dp_compute_output_format(encoder, pipe_config, conn_state, false);
2003 	if (ret)
2004 		return ret;
2005 
2006 	if ((intel_dp_is_edp(intel_dp) && fixed_mode) ||
2007 	    pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420) {
2008 		ret = intel_panel_fitting(pipe_config, conn_state);
2009 		if (ret)
2010 			return ret;
2011 	}
2012 
2013 	pipe_config->limited_color_range =
2014 		intel_dp_limited_color_range(pipe_config, conn_state);
2015 
2016 	if (pipe_config->dsc.compression_enable)
2017 		output_bpp = pipe_config->dsc.compressed_bpp;
2018 	else
2019 		output_bpp = intel_dp_output_bpp(pipe_config->output_format,
2020 						 pipe_config->pipe_bpp);
2021 
2022 	if (intel_dp->mso_link_count) {
2023 		int n = intel_dp->mso_link_count;
2024 		int overlap = intel_dp->mso_pixel_overlap;
2025 
2026 		pipe_config->splitter.enable = true;
2027 		pipe_config->splitter.link_count = n;
2028 		pipe_config->splitter.pixel_overlap = overlap;
2029 
2030 		drm_dbg_kms(&dev_priv->drm, "MSO link count %d, pixel overlap %d\n",
2031 			    n, overlap);
2032 
2033 		adjusted_mode->crtc_hdisplay = adjusted_mode->crtc_hdisplay / n + overlap;
2034 		adjusted_mode->crtc_hblank_start = adjusted_mode->crtc_hblank_start / n + overlap;
2035 		adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_hblank_end / n + overlap;
2036 		adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hsync_start / n + overlap;
2037 		adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_end / n + overlap;
2038 		adjusted_mode->crtc_htotal = adjusted_mode->crtc_htotal / n + overlap;
2039 		adjusted_mode->crtc_clock /= n;
2040 	}
2041 
2042 	intel_link_compute_m_n(output_bpp,
2043 			       pipe_config->lane_count,
2044 			       adjusted_mode->crtc_clock,
2045 			       pipe_config->port_clock,
2046 			       &pipe_config->dp_m_n,
2047 			       constant_n, pipe_config->fec_enable);
2048 
2049 	/* FIXME: abstract this better */
2050 	if (pipe_config->splitter.enable)
2051 		pipe_config->dp_m_n.data_m *= pipe_config->splitter.link_count;
2052 
2053 	if (!HAS_DDI(dev_priv))
2054 		g4x_dp_set_clock(encoder, pipe_config);
2055 
2056 	intel_vrr_compute_config(pipe_config, conn_state);
2057 	intel_psr_compute_config(intel_dp, pipe_config, conn_state);
2058 	intel_dp_drrs_compute_config(connector, pipe_config,
2059 				     output_bpp, constant_n);
2060 	intel_dp_compute_vsc_sdp(intel_dp, pipe_config, conn_state);
2061 	intel_dp_compute_hdr_metadata_infoframe_sdp(intel_dp, pipe_config, conn_state);
2062 
2063 	return 0;
2064 }
2065 
2066 void intel_dp_set_link_params(struct intel_dp *intel_dp,
2067 			      int link_rate, int lane_count)
2068 {
2069 	memset(intel_dp->train_set, 0, sizeof(intel_dp->train_set));
2070 	intel_dp->link_trained = false;
2071 	intel_dp->link_rate = link_rate;
2072 	intel_dp->lane_count = lane_count;
2073 }
2074 
2075 static void intel_dp_reset_max_link_params(struct intel_dp *intel_dp)
2076 {
2077 	intel_dp->max_link_lane_count = intel_dp_max_common_lane_count(intel_dp);
2078 	intel_dp->max_link_rate = intel_dp_max_common_rate(intel_dp);
2079 }
2080 
2081 /* Enable backlight PWM and backlight PP control. */
2082 void intel_edp_backlight_on(const struct intel_crtc_state *crtc_state,
2083 			    const struct drm_connector_state *conn_state)
2084 {
2085 	struct intel_dp *intel_dp = enc_to_intel_dp(to_intel_encoder(conn_state->best_encoder));
2086 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2087 
2088 	if (!intel_dp_is_edp(intel_dp))
2089 		return;
2090 
2091 	drm_dbg_kms(&i915->drm, "\n");
2092 
2093 	intel_backlight_enable(crtc_state, conn_state);
2094 	intel_pps_backlight_on(intel_dp);
2095 }
2096 
2097 /* Disable backlight PP control and backlight PWM. */
2098 void intel_edp_backlight_off(const struct drm_connector_state *old_conn_state)
2099 {
2100 	struct intel_dp *intel_dp = enc_to_intel_dp(to_intel_encoder(old_conn_state->best_encoder));
2101 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2102 
2103 	if (!intel_dp_is_edp(intel_dp))
2104 		return;
2105 
2106 	drm_dbg_kms(&i915->drm, "\n");
2107 
2108 	intel_pps_backlight_off(intel_dp);
2109 	intel_backlight_disable(old_conn_state);
2110 }
2111 
2112 static bool downstream_hpd_needs_d0(struct intel_dp *intel_dp)
2113 {
2114 	/*
2115 	 * DPCD 1.2+ should support BRANCH_DEVICE_CTRL, and thus
2116 	 * be capable of signalling downstream hpd with a long pulse.
2117 	 * Whether or not that means D3 is safe to use is not clear,
2118 	 * but let's assume so until proven otherwise.
2119 	 *
2120 	 * FIXME should really check all downstream ports...
2121 	 */
2122 	return intel_dp->dpcd[DP_DPCD_REV] == 0x11 &&
2123 		drm_dp_is_branch(intel_dp->dpcd) &&
2124 		intel_dp->downstream_ports[0] & DP_DS_PORT_HPD;
2125 }
2126 
2127 void intel_dp_sink_set_decompression_state(struct intel_dp *intel_dp,
2128 					   const struct intel_crtc_state *crtc_state,
2129 					   bool enable)
2130 {
2131 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2132 	int ret;
2133 
2134 	if (!crtc_state->dsc.compression_enable)
2135 		return;
2136 
2137 	ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_DSC_ENABLE,
2138 				 enable ? DP_DECOMPRESSION_EN : 0);
2139 	if (ret < 0)
2140 		drm_dbg_kms(&i915->drm,
2141 			    "Failed to %s sink decompression state\n",
2142 			    str_enable_disable(enable));
2143 }
2144 
2145 static void
2146 intel_edp_init_source_oui(struct intel_dp *intel_dp, bool careful)
2147 {
2148 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2149 	u8 oui[] = { 0x00, 0xaa, 0x01 };
2150 	u8 buf[3] = { 0 };
2151 
2152 	/*
2153 	 * During driver init, we want to be careful and avoid changing the source OUI if it's
2154 	 * already set to what we want, so as to avoid clearing any state by accident
2155 	 */
2156 	if (careful) {
2157 		if (drm_dp_dpcd_read(&intel_dp->aux, DP_SOURCE_OUI, buf, sizeof(buf)) < 0)
2158 			drm_err(&i915->drm, "Failed to read source OUI\n");
2159 
2160 		if (memcmp(oui, buf, sizeof(oui)) == 0)
2161 			return;
2162 	}
2163 
2164 	if (drm_dp_dpcd_write(&intel_dp->aux, DP_SOURCE_OUI, oui, sizeof(oui)) < 0)
2165 		drm_err(&i915->drm, "Failed to write source OUI\n");
2166 
2167 	intel_dp->last_oui_write = jiffies;
2168 }
2169 
2170 void intel_dp_wait_source_oui(struct intel_dp *intel_dp)
2171 {
2172 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2173 
2174 	drm_dbg_kms(&i915->drm, "Performing OUI wait\n");
2175 	wait_remaining_ms_from_jiffies(intel_dp->last_oui_write, 30);
2176 }
2177 
2178 /* If the device supports it, try to set the power state appropriately */
2179 void intel_dp_set_power(struct intel_dp *intel_dp, u8 mode)
2180 {
2181 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
2182 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
2183 	int ret, i;
2184 
2185 	/* Should have a valid DPCD by this point */
2186 	if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
2187 		return;
2188 
2189 	if (mode != DP_SET_POWER_D0) {
2190 		if (downstream_hpd_needs_d0(intel_dp))
2191 			return;
2192 
2193 		ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER, mode);
2194 	} else {
2195 		struct intel_lspcon *lspcon = dp_to_lspcon(intel_dp);
2196 
2197 		lspcon_resume(dp_to_dig_port(intel_dp));
2198 
2199 		/* Write the source OUI as early as possible */
2200 		if (intel_dp_is_edp(intel_dp))
2201 			intel_edp_init_source_oui(intel_dp, false);
2202 
2203 		/*
2204 		 * When turning on, we need to retry for 1ms to give the sink
2205 		 * time to wake up.
2206 		 */
2207 		for (i = 0; i < 3; i++) {
2208 			ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER, mode);
2209 			if (ret == 1)
2210 				break;
2211 			msleep(1);
2212 		}
2213 
2214 		if (ret == 1 && lspcon->active)
2215 			lspcon_wait_pcon_mode(lspcon);
2216 	}
2217 
2218 	if (ret != 1)
2219 		drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] Set power to %s failed\n",
2220 			    encoder->base.base.id, encoder->base.name,
2221 			    mode == DP_SET_POWER_D0 ? "D0" : "D3");
2222 }
2223 
2224 static bool
2225 intel_dp_get_dpcd(struct intel_dp *intel_dp);
2226 
2227 /**
2228  * intel_dp_sync_state - sync the encoder state during init/resume
2229  * @encoder: intel encoder to sync
2230  * @crtc_state: state for the CRTC connected to the encoder
2231  *
2232  * Sync any state stored in the encoder wrt. HW state during driver init
2233  * and system resume.
2234  */
2235 void intel_dp_sync_state(struct intel_encoder *encoder,
2236 			 const struct intel_crtc_state *crtc_state)
2237 {
2238 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2239 
2240 	if (!crtc_state)
2241 		return;
2242 
2243 	/*
2244 	 * Don't clobber DPCD if it's been already read out during output
2245 	 * setup (eDP) or detect.
2246 	 */
2247 	if (intel_dp->dpcd[DP_DPCD_REV] == 0)
2248 		intel_dp_get_dpcd(intel_dp);
2249 
2250 	intel_dp_reset_max_link_params(intel_dp);
2251 }
2252 
2253 bool intel_dp_initial_fastset_check(struct intel_encoder *encoder,
2254 				    struct intel_crtc_state *crtc_state)
2255 {
2256 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
2257 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2258 
2259 	/*
2260 	 * If BIOS has set an unsupported or non-standard link rate for some
2261 	 * reason force an encoder recompute and full modeset.
2262 	 */
2263 	if (intel_dp_rate_index(intel_dp->source_rates, intel_dp->num_source_rates,
2264 				crtc_state->port_clock) < 0) {
2265 		drm_dbg_kms(&i915->drm, "Forcing full modeset due to unsupported link rate\n");
2266 		crtc_state->uapi.connectors_changed = true;
2267 		return false;
2268 	}
2269 
2270 	/*
2271 	 * FIXME hack to force full modeset when DSC is being used.
2272 	 *
2273 	 * As long as we do not have full state readout and config comparison
2274 	 * of crtc_state->dsc, we have no way to ensure reliable fastset.
2275 	 * Remove once we have readout for DSC.
2276 	 */
2277 	if (crtc_state->dsc.compression_enable) {
2278 		drm_dbg_kms(&i915->drm, "Forcing full modeset due to DSC being enabled\n");
2279 		crtc_state->uapi.mode_changed = true;
2280 		return false;
2281 	}
2282 
2283 	if (CAN_PSR(intel_dp)) {
2284 		drm_dbg_kms(&i915->drm, "Forcing full modeset to compute PSR state\n");
2285 		crtc_state->uapi.mode_changed = true;
2286 		return false;
2287 	}
2288 
2289 	return true;
2290 }
2291 
2292 static void intel_dp_get_pcon_dsc_cap(struct intel_dp *intel_dp)
2293 {
2294 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2295 
2296 	/* Clear the cached register set to avoid using stale values */
2297 
2298 	memset(intel_dp->pcon_dsc_dpcd, 0, sizeof(intel_dp->pcon_dsc_dpcd));
2299 
2300 	if (drm_dp_dpcd_read(&intel_dp->aux, DP_PCON_DSC_ENCODER,
2301 			     intel_dp->pcon_dsc_dpcd,
2302 			     sizeof(intel_dp->pcon_dsc_dpcd)) < 0)
2303 		drm_err(&i915->drm, "Failed to read DPCD register 0x%x\n",
2304 			DP_PCON_DSC_ENCODER);
2305 
2306 	drm_dbg_kms(&i915->drm, "PCON ENCODER DSC DPCD: %*ph\n",
2307 		    (int)sizeof(intel_dp->pcon_dsc_dpcd), intel_dp->pcon_dsc_dpcd);
2308 }
2309 
2310 static int intel_dp_pcon_get_frl_mask(u8 frl_bw_mask)
2311 {
2312 	int bw_gbps[] = {9, 18, 24, 32, 40, 48};
2313 	int i;
2314 
2315 	for (i = ARRAY_SIZE(bw_gbps) - 1; i >= 0; i--) {
2316 		if (frl_bw_mask & (1 << i))
2317 			return bw_gbps[i];
2318 	}
2319 	return 0;
2320 }
2321 
2322 static int intel_dp_pcon_set_frl_mask(int max_frl)
2323 {
2324 	switch (max_frl) {
2325 	case 48:
2326 		return DP_PCON_FRL_BW_MASK_48GBPS;
2327 	case 40:
2328 		return DP_PCON_FRL_BW_MASK_40GBPS;
2329 	case 32:
2330 		return DP_PCON_FRL_BW_MASK_32GBPS;
2331 	case 24:
2332 		return DP_PCON_FRL_BW_MASK_24GBPS;
2333 	case 18:
2334 		return DP_PCON_FRL_BW_MASK_18GBPS;
2335 	case 9:
2336 		return DP_PCON_FRL_BW_MASK_9GBPS;
2337 	}
2338 
2339 	return 0;
2340 }
2341 
2342 static int intel_dp_hdmi_sink_max_frl(struct intel_dp *intel_dp)
2343 {
2344 	struct intel_connector *intel_connector = intel_dp->attached_connector;
2345 	struct drm_connector *connector = &intel_connector->base;
2346 	int max_frl_rate;
2347 	int max_lanes, rate_per_lane;
2348 	int max_dsc_lanes, dsc_rate_per_lane;
2349 
2350 	max_lanes = connector->display_info.hdmi.max_lanes;
2351 	rate_per_lane = connector->display_info.hdmi.max_frl_rate_per_lane;
2352 	max_frl_rate = max_lanes * rate_per_lane;
2353 
2354 	if (connector->display_info.hdmi.dsc_cap.v_1p2) {
2355 		max_dsc_lanes = connector->display_info.hdmi.dsc_cap.max_lanes;
2356 		dsc_rate_per_lane = connector->display_info.hdmi.dsc_cap.max_frl_rate_per_lane;
2357 		if (max_dsc_lanes && dsc_rate_per_lane)
2358 			max_frl_rate = min(max_frl_rate, max_dsc_lanes * dsc_rate_per_lane);
2359 	}
2360 
2361 	return max_frl_rate;
2362 }
2363 
2364 static bool
2365 intel_dp_pcon_is_frl_trained(struct intel_dp *intel_dp,
2366 			     u8 max_frl_bw_mask, u8 *frl_trained_mask)
2367 {
2368 	if (drm_dp_pcon_hdmi_link_active(&intel_dp->aux) &&
2369 	    drm_dp_pcon_hdmi_link_mode(&intel_dp->aux, frl_trained_mask) == DP_PCON_HDMI_MODE_FRL &&
2370 	    *frl_trained_mask >= max_frl_bw_mask)
2371 		return true;
2372 
2373 	return false;
2374 }
2375 
2376 static int intel_dp_pcon_start_frl_training(struct intel_dp *intel_dp)
2377 {
2378 #define TIMEOUT_FRL_READY_MS 500
2379 #define TIMEOUT_HDMI_LINK_ACTIVE_MS 1000
2380 
2381 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2382 	int max_frl_bw, max_pcon_frl_bw, max_edid_frl_bw, ret;
2383 	u8 max_frl_bw_mask = 0, frl_trained_mask;
2384 	bool is_active;
2385 
2386 	max_pcon_frl_bw = intel_dp->dfp.pcon_max_frl_bw;
2387 	drm_dbg(&i915->drm, "PCON max rate = %d Gbps\n", max_pcon_frl_bw);
2388 
2389 	max_edid_frl_bw = intel_dp_hdmi_sink_max_frl(intel_dp);
2390 	drm_dbg(&i915->drm, "Sink max rate from EDID = %d Gbps\n", max_edid_frl_bw);
2391 
2392 	max_frl_bw = min(max_edid_frl_bw, max_pcon_frl_bw);
2393 
2394 	if (max_frl_bw <= 0)
2395 		return -EINVAL;
2396 
2397 	max_frl_bw_mask = intel_dp_pcon_set_frl_mask(max_frl_bw);
2398 	drm_dbg(&i915->drm, "MAX_FRL_BW_MASK = %u\n", max_frl_bw_mask);
2399 
2400 	if (intel_dp_pcon_is_frl_trained(intel_dp, max_frl_bw_mask, &frl_trained_mask))
2401 		goto frl_trained;
2402 
2403 	ret = drm_dp_pcon_frl_prepare(&intel_dp->aux, false);
2404 	if (ret < 0)
2405 		return ret;
2406 	/* Wait for PCON to be FRL Ready */
2407 	wait_for(is_active = drm_dp_pcon_is_frl_ready(&intel_dp->aux) == true, TIMEOUT_FRL_READY_MS);
2408 
2409 	if (!is_active)
2410 		return -ETIMEDOUT;
2411 
2412 	ret = drm_dp_pcon_frl_configure_1(&intel_dp->aux, max_frl_bw,
2413 					  DP_PCON_ENABLE_SEQUENTIAL_LINK);
2414 	if (ret < 0)
2415 		return ret;
2416 	ret = drm_dp_pcon_frl_configure_2(&intel_dp->aux, max_frl_bw_mask,
2417 					  DP_PCON_FRL_LINK_TRAIN_NORMAL);
2418 	if (ret < 0)
2419 		return ret;
2420 	ret = drm_dp_pcon_frl_enable(&intel_dp->aux);
2421 	if (ret < 0)
2422 		return ret;
2423 	/*
2424 	 * Wait for FRL to be completed
2425 	 * Check if the HDMI Link is up and active.
2426 	 */
2427 	wait_for(is_active =
2428 		 intel_dp_pcon_is_frl_trained(intel_dp, max_frl_bw_mask, &frl_trained_mask),
2429 		 TIMEOUT_HDMI_LINK_ACTIVE_MS);
2430 
2431 	if (!is_active)
2432 		return -ETIMEDOUT;
2433 
2434 frl_trained:
2435 	drm_dbg(&i915->drm, "FRL_TRAINED_MASK = %u\n", frl_trained_mask);
2436 	intel_dp->frl.trained_rate_gbps = intel_dp_pcon_get_frl_mask(frl_trained_mask);
2437 	intel_dp->frl.is_trained = true;
2438 	drm_dbg(&i915->drm, "FRL trained with : %d Gbps\n", intel_dp->frl.trained_rate_gbps);
2439 
2440 	return 0;
2441 }
2442 
2443 static bool intel_dp_is_hdmi_2_1_sink(struct intel_dp *intel_dp)
2444 {
2445 	if (drm_dp_is_branch(intel_dp->dpcd) &&
2446 	    intel_dp->has_hdmi_sink &&
2447 	    intel_dp_hdmi_sink_max_frl(intel_dp) > 0)
2448 		return true;
2449 
2450 	return false;
2451 }
2452 
2453 static
2454 int intel_dp_pcon_set_tmds_mode(struct intel_dp *intel_dp)
2455 {
2456 	int ret;
2457 	u8 buf = 0;
2458 
2459 	/* Set PCON source control mode */
2460 	buf |= DP_PCON_ENABLE_SOURCE_CTL_MODE;
2461 
2462 	ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2463 	if (ret < 0)
2464 		return ret;
2465 
2466 	/* Set HDMI LINK ENABLE */
2467 	buf |= DP_PCON_ENABLE_HDMI_LINK;
2468 	ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2469 	if (ret < 0)
2470 		return ret;
2471 
2472 	return 0;
2473 }
2474 
2475 void intel_dp_check_frl_training(struct intel_dp *intel_dp)
2476 {
2477 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
2478 
2479 	/*
2480 	 * Always go for FRL training if:
2481 	 * -PCON supports SRC_CTL_MODE (VESA DP2.0-HDMI2.1 PCON Spec Draft-1 Sec-7)
2482 	 * -sink is HDMI2.1
2483 	 */
2484 	if (!(intel_dp->downstream_ports[2] & DP_PCON_SOURCE_CTL_MODE) ||
2485 	    !intel_dp_is_hdmi_2_1_sink(intel_dp) ||
2486 	    intel_dp->frl.is_trained)
2487 		return;
2488 
2489 	if (intel_dp_pcon_start_frl_training(intel_dp) < 0) {
2490 		int ret, mode;
2491 
2492 		drm_dbg(&dev_priv->drm, "Couldn't set FRL mode, continuing with TMDS mode\n");
2493 		ret = intel_dp_pcon_set_tmds_mode(intel_dp);
2494 		mode = drm_dp_pcon_hdmi_link_mode(&intel_dp->aux, NULL);
2495 
2496 		if (ret < 0 || mode != DP_PCON_HDMI_MODE_TMDS)
2497 			drm_dbg(&dev_priv->drm, "Issue with PCON, cannot set TMDS mode\n");
2498 	} else {
2499 		drm_dbg(&dev_priv->drm, "FRL training Completed\n");
2500 	}
2501 }
2502 
2503 static int
2504 intel_dp_pcon_dsc_enc_slice_height(const struct intel_crtc_state *crtc_state)
2505 {
2506 	int vactive = crtc_state->hw.adjusted_mode.vdisplay;
2507 
2508 	return intel_hdmi_dsc_get_slice_height(vactive);
2509 }
2510 
2511 static int
2512 intel_dp_pcon_dsc_enc_slices(struct intel_dp *intel_dp,
2513 			     const struct intel_crtc_state *crtc_state)
2514 {
2515 	struct intel_connector *intel_connector = intel_dp->attached_connector;
2516 	struct drm_connector *connector = &intel_connector->base;
2517 	int hdmi_throughput = connector->display_info.hdmi.dsc_cap.clk_per_slice;
2518 	int hdmi_max_slices = connector->display_info.hdmi.dsc_cap.max_slices;
2519 	int pcon_max_slices = drm_dp_pcon_dsc_max_slices(intel_dp->pcon_dsc_dpcd);
2520 	int pcon_max_slice_width = drm_dp_pcon_dsc_max_slice_width(intel_dp->pcon_dsc_dpcd);
2521 
2522 	return intel_hdmi_dsc_get_num_slices(crtc_state, pcon_max_slices,
2523 					     pcon_max_slice_width,
2524 					     hdmi_max_slices, hdmi_throughput);
2525 }
2526 
2527 static int
2528 intel_dp_pcon_dsc_enc_bpp(struct intel_dp *intel_dp,
2529 			  const struct intel_crtc_state *crtc_state,
2530 			  int num_slices, int slice_width)
2531 {
2532 	struct intel_connector *intel_connector = intel_dp->attached_connector;
2533 	struct drm_connector *connector = &intel_connector->base;
2534 	int output_format = crtc_state->output_format;
2535 	bool hdmi_all_bpp = connector->display_info.hdmi.dsc_cap.all_bpp;
2536 	int pcon_fractional_bpp = drm_dp_pcon_dsc_bpp_incr(intel_dp->pcon_dsc_dpcd);
2537 	int hdmi_max_chunk_bytes =
2538 		connector->display_info.hdmi.dsc_cap.total_chunk_kbytes * 1024;
2539 
2540 	return intel_hdmi_dsc_get_bpp(pcon_fractional_bpp, slice_width,
2541 				      num_slices, output_format, hdmi_all_bpp,
2542 				      hdmi_max_chunk_bytes);
2543 }
2544 
2545 void
2546 intel_dp_pcon_dsc_configure(struct intel_dp *intel_dp,
2547 			    const struct intel_crtc_state *crtc_state)
2548 {
2549 	u8 pps_param[6];
2550 	int slice_height;
2551 	int slice_width;
2552 	int num_slices;
2553 	int bits_per_pixel;
2554 	int ret;
2555 	struct intel_connector *intel_connector = intel_dp->attached_connector;
2556 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2557 	struct drm_connector *connector;
2558 	bool hdmi_is_dsc_1_2;
2559 
2560 	if (!intel_dp_is_hdmi_2_1_sink(intel_dp))
2561 		return;
2562 
2563 	if (!intel_connector)
2564 		return;
2565 	connector = &intel_connector->base;
2566 	hdmi_is_dsc_1_2 = connector->display_info.hdmi.dsc_cap.v_1p2;
2567 
2568 	if (!drm_dp_pcon_enc_is_dsc_1_2(intel_dp->pcon_dsc_dpcd) ||
2569 	    !hdmi_is_dsc_1_2)
2570 		return;
2571 
2572 	slice_height = intel_dp_pcon_dsc_enc_slice_height(crtc_state);
2573 	if (!slice_height)
2574 		return;
2575 
2576 	num_slices = intel_dp_pcon_dsc_enc_slices(intel_dp, crtc_state);
2577 	if (!num_slices)
2578 		return;
2579 
2580 	slice_width = DIV_ROUND_UP(crtc_state->hw.adjusted_mode.hdisplay,
2581 				   num_slices);
2582 
2583 	bits_per_pixel = intel_dp_pcon_dsc_enc_bpp(intel_dp, crtc_state,
2584 						   num_slices, slice_width);
2585 	if (!bits_per_pixel)
2586 		return;
2587 
2588 	pps_param[0] = slice_height & 0xFF;
2589 	pps_param[1] = slice_height >> 8;
2590 	pps_param[2] = slice_width & 0xFF;
2591 	pps_param[3] = slice_width >> 8;
2592 	pps_param[4] = bits_per_pixel & 0xFF;
2593 	pps_param[5] = (bits_per_pixel >> 8) & 0x3;
2594 
2595 	ret = drm_dp_pcon_pps_override_param(&intel_dp->aux, pps_param);
2596 	if (ret < 0)
2597 		drm_dbg_kms(&i915->drm, "Failed to set pcon DSC\n");
2598 }
2599 
2600 void intel_dp_configure_protocol_converter(struct intel_dp *intel_dp,
2601 					   const struct intel_crtc_state *crtc_state)
2602 {
2603 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2604 	u8 tmp;
2605 
2606 	if (intel_dp->dpcd[DP_DPCD_REV] < 0x13)
2607 		return;
2608 
2609 	if (!drm_dp_is_branch(intel_dp->dpcd))
2610 		return;
2611 
2612 	tmp = intel_dp->has_hdmi_sink ?
2613 		DP_HDMI_DVI_OUTPUT_CONFIG : 0;
2614 
2615 	if (drm_dp_dpcd_writeb(&intel_dp->aux,
2616 			       DP_PROTOCOL_CONVERTER_CONTROL_0, tmp) != 1)
2617 		drm_dbg_kms(&i915->drm, "Failed to %s protocol converter HDMI mode\n",
2618 			    str_enable_disable(intel_dp->has_hdmi_sink));
2619 
2620 	tmp = crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444 &&
2621 		intel_dp->dfp.ycbcr_444_to_420 ? DP_CONVERSION_TO_YCBCR420_ENABLE : 0;
2622 
2623 	if (drm_dp_dpcd_writeb(&intel_dp->aux,
2624 			       DP_PROTOCOL_CONVERTER_CONTROL_1, tmp) != 1)
2625 		drm_dbg_kms(&i915->drm,
2626 			    "Failed to %s protocol converter YCbCr 4:2:0 conversion mode\n",
2627 			    str_enable_disable(intel_dp->dfp.ycbcr_444_to_420));
2628 
2629 	tmp = intel_dp->dfp.rgb_to_ycbcr ?
2630 		DP_CONVERSION_BT709_RGB_YCBCR_ENABLE : 0;
2631 
2632 	if (drm_dp_pcon_convert_rgb_to_ycbcr(&intel_dp->aux, tmp) < 0)
2633 		drm_dbg_kms(&i915->drm,
2634 			   "Failed to %s protocol converter RGB->YCbCr conversion mode\n",
2635 			   str_enable_disable(tmp));
2636 }
2637 
2638 
2639 bool intel_dp_get_colorimetry_status(struct intel_dp *intel_dp)
2640 {
2641 	u8 dprx = 0;
2642 
2643 	if (drm_dp_dpcd_readb(&intel_dp->aux, DP_DPRX_FEATURE_ENUMERATION_LIST,
2644 			      &dprx) != 1)
2645 		return false;
2646 	return dprx & DP_VSC_SDP_EXT_FOR_COLORIMETRY_SUPPORTED;
2647 }
2648 
2649 static void intel_dp_get_dsc_sink_cap(struct intel_dp *intel_dp)
2650 {
2651 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2652 
2653 	/*
2654 	 * Clear the cached register set to avoid using stale values
2655 	 * for the sinks that do not support DSC.
2656 	 */
2657 	memset(intel_dp->dsc_dpcd, 0, sizeof(intel_dp->dsc_dpcd));
2658 
2659 	/* Clear fec_capable to avoid using stale values */
2660 	intel_dp->fec_capable = 0;
2661 
2662 	/* Cache the DSC DPCD if eDP or DP rev >= 1.4 */
2663 	if (intel_dp->dpcd[DP_DPCD_REV] >= 0x14 ||
2664 	    intel_dp->edp_dpcd[0] >= DP_EDP_14) {
2665 		if (drm_dp_dpcd_read(&intel_dp->aux, DP_DSC_SUPPORT,
2666 				     intel_dp->dsc_dpcd,
2667 				     sizeof(intel_dp->dsc_dpcd)) < 0)
2668 			drm_err(&i915->drm,
2669 				"Failed to read DPCD register 0x%x\n",
2670 				DP_DSC_SUPPORT);
2671 
2672 		drm_dbg_kms(&i915->drm, "DSC DPCD: %*ph\n",
2673 			    (int)sizeof(intel_dp->dsc_dpcd),
2674 			    intel_dp->dsc_dpcd);
2675 
2676 		/* FEC is supported only on DP 1.4 */
2677 		if (!intel_dp_is_edp(intel_dp) &&
2678 		    drm_dp_dpcd_readb(&intel_dp->aux, DP_FEC_CAPABILITY,
2679 				      &intel_dp->fec_capable) < 0)
2680 			drm_err(&i915->drm,
2681 				"Failed to read FEC DPCD register\n");
2682 
2683 		drm_dbg_kms(&i915->drm, "FEC CAPABILITY: %x\n",
2684 			    intel_dp->fec_capable);
2685 	}
2686 }
2687 
2688 static void intel_edp_mso_mode_fixup(struct intel_connector *connector,
2689 				     struct drm_display_mode *mode)
2690 {
2691 	struct intel_dp *intel_dp = intel_attached_dp(connector);
2692 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
2693 	int n = intel_dp->mso_link_count;
2694 	int overlap = intel_dp->mso_pixel_overlap;
2695 
2696 	if (!mode || !n)
2697 		return;
2698 
2699 	mode->hdisplay = (mode->hdisplay - overlap) * n;
2700 	mode->hsync_start = (mode->hsync_start - overlap) * n;
2701 	mode->hsync_end = (mode->hsync_end - overlap) * n;
2702 	mode->htotal = (mode->htotal - overlap) * n;
2703 	mode->clock *= n;
2704 
2705 	drm_mode_set_name(mode);
2706 
2707 	drm_dbg_kms(&i915->drm,
2708 		    "[CONNECTOR:%d:%s] using generated MSO mode: " DRM_MODE_FMT "\n",
2709 		    connector->base.base.id, connector->base.name,
2710 		    DRM_MODE_ARG(mode));
2711 }
2712 
2713 static void intel_edp_mso_init(struct intel_dp *intel_dp)
2714 {
2715 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2716 	struct intel_connector *connector = intel_dp->attached_connector;
2717 	struct drm_display_info *info = &connector->base.display_info;
2718 	u8 mso;
2719 
2720 	if (intel_dp->edp_dpcd[0] < DP_EDP_14)
2721 		return;
2722 
2723 	if (drm_dp_dpcd_readb(&intel_dp->aux, DP_EDP_MSO_LINK_CAPABILITIES, &mso) != 1) {
2724 		drm_err(&i915->drm, "Failed to read MSO cap\n");
2725 		return;
2726 	}
2727 
2728 	/* Valid configurations are SST or MSO 2x1, 2x2, 4x1 */
2729 	mso &= DP_EDP_MSO_NUMBER_OF_LINKS_MASK;
2730 	if (mso % 2 || mso > drm_dp_max_lane_count(intel_dp->dpcd)) {
2731 		drm_err(&i915->drm, "Invalid MSO link count cap %u\n", mso);
2732 		mso = 0;
2733 	}
2734 
2735 	if (mso) {
2736 		drm_dbg_kms(&i915->drm, "Sink MSO %ux%u configuration, pixel overlap %u\n",
2737 			    mso, drm_dp_max_lane_count(intel_dp->dpcd) / mso,
2738 			    info->mso_pixel_overlap);
2739 		if (!HAS_MSO(i915)) {
2740 			drm_err(&i915->drm, "No source MSO support, disabling\n");
2741 			mso = 0;
2742 		}
2743 	}
2744 
2745 	intel_dp->mso_link_count = mso;
2746 	intel_dp->mso_pixel_overlap = mso ? info->mso_pixel_overlap : 0;
2747 }
2748 
2749 static bool
2750 intel_edp_init_dpcd(struct intel_dp *intel_dp)
2751 {
2752 	struct drm_i915_private *dev_priv =
2753 		to_i915(dp_to_dig_port(intel_dp)->base.base.dev);
2754 
2755 	/* this function is meant to be called only once */
2756 	drm_WARN_ON(&dev_priv->drm, intel_dp->dpcd[DP_DPCD_REV] != 0);
2757 
2758 	if (drm_dp_read_dpcd_caps(&intel_dp->aux, intel_dp->dpcd) != 0)
2759 		return false;
2760 
2761 	drm_dp_read_desc(&intel_dp->aux, &intel_dp->desc,
2762 			 drm_dp_is_branch(intel_dp->dpcd));
2763 
2764 	/*
2765 	 * Read the eDP display control registers.
2766 	 *
2767 	 * Do this independent of DP_DPCD_DISPLAY_CONTROL_CAPABLE bit in
2768 	 * DP_EDP_CONFIGURATION_CAP, because some buggy displays do not have it
2769 	 * set, but require eDP 1.4+ detection (e.g. for supported link rates
2770 	 * method). The display control registers should read zero if they're
2771 	 * not supported anyway.
2772 	 */
2773 	if (drm_dp_dpcd_read(&intel_dp->aux, DP_EDP_DPCD_REV,
2774 			     intel_dp->edp_dpcd, sizeof(intel_dp->edp_dpcd)) ==
2775 			     sizeof(intel_dp->edp_dpcd)) {
2776 		drm_dbg_kms(&dev_priv->drm, "eDP DPCD: %*ph\n",
2777 			    (int)sizeof(intel_dp->edp_dpcd),
2778 			    intel_dp->edp_dpcd);
2779 
2780 		intel_dp->use_max_params = intel_dp->edp_dpcd[0] < DP_EDP_14;
2781 	}
2782 
2783 	/*
2784 	 * This has to be called after intel_dp->edp_dpcd is filled, PSR checks
2785 	 * for SET_POWER_CAPABLE bit in intel_dp->edp_dpcd[1]
2786 	 */
2787 	intel_psr_init_dpcd(intel_dp);
2788 
2789 	/* Clear the default sink rates */
2790 	intel_dp->num_sink_rates = 0;
2791 
2792 	/* Read the eDP 1.4+ supported link rates. */
2793 	if (intel_dp->edp_dpcd[0] >= DP_EDP_14) {
2794 		__le16 sink_rates[DP_MAX_SUPPORTED_RATES];
2795 		int i;
2796 
2797 		drm_dp_dpcd_read(&intel_dp->aux, DP_SUPPORTED_LINK_RATES,
2798 				sink_rates, sizeof(sink_rates));
2799 
2800 		for (i = 0; i < ARRAY_SIZE(sink_rates); i++) {
2801 			int val = le16_to_cpu(sink_rates[i]);
2802 
2803 			if (val == 0)
2804 				break;
2805 
2806 			/* Value read multiplied by 200kHz gives the per-lane
2807 			 * link rate in kHz. The source rates are, however,
2808 			 * stored in terms of LS_Clk kHz. The full conversion
2809 			 * back to symbols is
2810 			 * (val * 200kHz)*(8/10 ch. encoding)*(1/8 bit to Byte)
2811 			 */
2812 			intel_dp->sink_rates[i] = (val * 200) / 10;
2813 		}
2814 		intel_dp->num_sink_rates = i;
2815 	}
2816 
2817 	/*
2818 	 * Use DP_LINK_RATE_SET if DP_SUPPORTED_LINK_RATES are available,
2819 	 * default to DP_MAX_LINK_RATE and DP_LINK_BW_SET otherwise.
2820 	 */
2821 	if (intel_dp->num_sink_rates)
2822 		intel_dp->use_rate_select = true;
2823 	else
2824 		intel_dp_set_sink_rates(intel_dp);
2825 	intel_dp_set_max_sink_lane_count(intel_dp);
2826 
2827 	intel_dp_set_common_rates(intel_dp);
2828 	intel_dp_reset_max_link_params(intel_dp);
2829 
2830 	/* Read the eDP DSC DPCD registers */
2831 	if (DISPLAY_VER(dev_priv) >= 10)
2832 		intel_dp_get_dsc_sink_cap(intel_dp);
2833 
2834 	/*
2835 	 * If needed, program our source OUI so we can make various Intel-specific AUX services
2836 	 * available (such as HDR backlight controls)
2837 	 */
2838 	intel_edp_init_source_oui(intel_dp, true);
2839 
2840 	return true;
2841 }
2842 
2843 static bool
2844 intel_dp_has_sink_count(struct intel_dp *intel_dp)
2845 {
2846 	if (!intel_dp->attached_connector)
2847 		return false;
2848 
2849 	return drm_dp_read_sink_count_cap(&intel_dp->attached_connector->base,
2850 					  intel_dp->dpcd,
2851 					  &intel_dp->desc);
2852 }
2853 
2854 static bool
2855 intel_dp_get_dpcd(struct intel_dp *intel_dp)
2856 {
2857 	int ret;
2858 
2859 	if (intel_dp_init_lttpr_and_dprx_caps(intel_dp) < 0)
2860 		return false;
2861 
2862 	/*
2863 	 * Don't clobber cached eDP rates. Also skip re-reading
2864 	 * the OUI/ID since we know it won't change.
2865 	 */
2866 	if (!intel_dp_is_edp(intel_dp)) {
2867 		drm_dp_read_desc(&intel_dp->aux, &intel_dp->desc,
2868 				 drm_dp_is_branch(intel_dp->dpcd));
2869 
2870 		intel_dp_set_sink_rates(intel_dp);
2871 		intel_dp_set_max_sink_lane_count(intel_dp);
2872 		intel_dp_set_common_rates(intel_dp);
2873 	}
2874 
2875 	if (intel_dp_has_sink_count(intel_dp)) {
2876 		ret = drm_dp_read_sink_count(&intel_dp->aux);
2877 		if (ret < 0)
2878 			return false;
2879 
2880 		/*
2881 		 * Sink count can change between short pulse hpd hence
2882 		 * a member variable in intel_dp will track any changes
2883 		 * between short pulse interrupts.
2884 		 */
2885 		intel_dp->sink_count = ret;
2886 
2887 		/*
2888 		 * SINK_COUNT == 0 and DOWNSTREAM_PORT_PRESENT == 1 implies that
2889 		 * a dongle is present but no display. Unless we require to know
2890 		 * if a dongle is present or not, we don't need to update
2891 		 * downstream port information. So, an early return here saves
2892 		 * time from performing other operations which are not required.
2893 		 */
2894 		if (!intel_dp->sink_count)
2895 			return false;
2896 	}
2897 
2898 	return drm_dp_read_downstream_info(&intel_dp->aux, intel_dp->dpcd,
2899 					   intel_dp->downstream_ports) == 0;
2900 }
2901 
2902 static bool
2903 intel_dp_can_mst(struct intel_dp *intel_dp)
2904 {
2905 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2906 
2907 	return i915->params.enable_dp_mst &&
2908 		intel_dp_mst_source_support(intel_dp) &&
2909 		drm_dp_read_mst_cap(&intel_dp->aux, intel_dp->dpcd);
2910 }
2911 
2912 static void
2913 intel_dp_configure_mst(struct intel_dp *intel_dp)
2914 {
2915 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2916 	struct intel_encoder *encoder =
2917 		&dp_to_dig_port(intel_dp)->base;
2918 	bool sink_can_mst = drm_dp_read_mst_cap(&intel_dp->aux, intel_dp->dpcd);
2919 
2920 	drm_dbg_kms(&i915->drm,
2921 		    "[ENCODER:%d:%s] MST support: port: %s, sink: %s, modparam: %s\n",
2922 		    encoder->base.base.id, encoder->base.name,
2923 		    str_yes_no(intel_dp_mst_source_support(intel_dp)),
2924 		    str_yes_no(sink_can_mst),
2925 		    str_yes_no(i915->params.enable_dp_mst));
2926 
2927 	if (!intel_dp_mst_source_support(intel_dp))
2928 		return;
2929 
2930 	intel_dp->is_mst = sink_can_mst &&
2931 		i915->params.enable_dp_mst;
2932 
2933 	drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr,
2934 					intel_dp->is_mst);
2935 }
2936 
2937 static bool
2938 intel_dp_get_sink_irq_esi(struct intel_dp *intel_dp, u8 *esi)
2939 {
2940 	return drm_dp_dpcd_read(&intel_dp->aux, DP_SINK_COUNT_ESI, esi, 4) == 4;
2941 }
2942 
2943 static bool intel_dp_ack_sink_irq_esi(struct intel_dp *intel_dp, u8 esi[4])
2944 {
2945 	int retry;
2946 
2947 	for (retry = 0; retry < 3; retry++) {
2948 		if (drm_dp_dpcd_write(&intel_dp->aux, DP_SINK_COUNT_ESI + 1,
2949 				      &esi[1], 3) == 3)
2950 			return true;
2951 	}
2952 
2953 	return false;
2954 }
2955 
2956 bool
2957 intel_dp_needs_vsc_sdp(const struct intel_crtc_state *crtc_state,
2958 		       const struct drm_connector_state *conn_state)
2959 {
2960 	/*
2961 	 * As per DP 1.4a spec section 2.2.4.3 [MSA Field for Indication
2962 	 * of Color Encoding Format and Content Color Gamut], in order to
2963 	 * sending YCBCR 420 or HDR BT.2020 signals we should use DP VSC SDP.
2964 	 */
2965 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
2966 		return true;
2967 
2968 	switch (conn_state->colorspace) {
2969 	case DRM_MODE_COLORIMETRY_SYCC_601:
2970 	case DRM_MODE_COLORIMETRY_OPYCC_601:
2971 	case DRM_MODE_COLORIMETRY_BT2020_YCC:
2972 	case DRM_MODE_COLORIMETRY_BT2020_RGB:
2973 	case DRM_MODE_COLORIMETRY_BT2020_CYCC:
2974 		return true;
2975 	default:
2976 		break;
2977 	}
2978 
2979 	return false;
2980 }
2981 
2982 static ssize_t intel_dp_vsc_sdp_pack(const struct drm_dp_vsc_sdp *vsc,
2983 				     struct dp_sdp *sdp, size_t size)
2984 {
2985 	size_t length = sizeof(struct dp_sdp);
2986 
2987 	if (size < length)
2988 		return -ENOSPC;
2989 
2990 	memset(sdp, 0, size);
2991 
2992 	/*
2993 	 * Prepare VSC Header for SU as per DP 1.4a spec, Table 2-119
2994 	 * VSC SDP Header Bytes
2995 	 */
2996 	sdp->sdp_header.HB0 = 0; /* Secondary-Data Packet ID = 0 */
2997 	sdp->sdp_header.HB1 = vsc->sdp_type; /* Secondary-data Packet Type */
2998 	sdp->sdp_header.HB2 = vsc->revision; /* Revision Number */
2999 	sdp->sdp_header.HB3 = vsc->length; /* Number of Valid Data Bytes */
3000 
3001 	/*
3002 	 * Only revision 0x5 supports Pixel Encoding/Colorimetry Format as
3003 	 * per DP 1.4a spec.
3004 	 */
3005 	if (vsc->revision != 0x5)
3006 		goto out;
3007 
3008 	/* VSC SDP Payload for DB16 through DB18 */
3009 	/* Pixel Encoding and Colorimetry Formats  */
3010 	sdp->db[16] = (vsc->pixelformat & 0xf) << 4; /* DB16[7:4] */
3011 	sdp->db[16] |= vsc->colorimetry & 0xf; /* DB16[3:0] */
3012 
3013 	switch (vsc->bpc) {
3014 	case 6:
3015 		/* 6bpc: 0x0 */
3016 		break;
3017 	case 8:
3018 		sdp->db[17] = 0x1; /* DB17[3:0] */
3019 		break;
3020 	case 10:
3021 		sdp->db[17] = 0x2;
3022 		break;
3023 	case 12:
3024 		sdp->db[17] = 0x3;
3025 		break;
3026 	case 16:
3027 		sdp->db[17] = 0x4;
3028 		break;
3029 	default:
3030 		MISSING_CASE(vsc->bpc);
3031 		break;
3032 	}
3033 	/* Dynamic Range and Component Bit Depth */
3034 	if (vsc->dynamic_range == DP_DYNAMIC_RANGE_CTA)
3035 		sdp->db[17] |= 0x80;  /* DB17[7] */
3036 
3037 	/* Content Type */
3038 	sdp->db[18] = vsc->content_type & 0x7;
3039 
3040 out:
3041 	return length;
3042 }
3043 
3044 static ssize_t
3045 intel_dp_hdr_metadata_infoframe_sdp_pack(struct drm_i915_private *i915,
3046 					 const struct hdmi_drm_infoframe *drm_infoframe,
3047 					 struct dp_sdp *sdp,
3048 					 size_t size)
3049 {
3050 	size_t length = sizeof(struct dp_sdp);
3051 	const int infoframe_size = HDMI_INFOFRAME_HEADER_SIZE + HDMI_DRM_INFOFRAME_SIZE;
3052 	unsigned char buf[HDMI_INFOFRAME_HEADER_SIZE + HDMI_DRM_INFOFRAME_SIZE];
3053 	ssize_t len;
3054 
3055 	if (size < length)
3056 		return -ENOSPC;
3057 
3058 	memset(sdp, 0, size);
3059 
3060 	len = hdmi_drm_infoframe_pack_only(drm_infoframe, buf, sizeof(buf));
3061 	if (len < 0) {
3062 		drm_dbg_kms(&i915->drm, "buffer size is smaller than hdr metadata infoframe\n");
3063 		return -ENOSPC;
3064 	}
3065 
3066 	if (len != infoframe_size) {
3067 		drm_dbg_kms(&i915->drm, "wrong static hdr metadata size\n");
3068 		return -ENOSPC;
3069 	}
3070 
3071 	/*
3072 	 * Set up the infoframe sdp packet for HDR static metadata.
3073 	 * Prepare VSC Header for SU as per DP 1.4a spec,
3074 	 * Table 2-100 and Table 2-101
3075 	 */
3076 
3077 	/* Secondary-Data Packet ID, 00h for non-Audio INFOFRAME */
3078 	sdp->sdp_header.HB0 = 0;
3079 	/*
3080 	 * Packet Type 80h + Non-audio INFOFRAME Type value
3081 	 * HDMI_INFOFRAME_TYPE_DRM: 0x87
3082 	 * - 80h + Non-audio INFOFRAME Type value
3083 	 * - InfoFrame Type: 0x07
3084 	 *    [CTA-861-G Table-42 Dynamic Range and Mastering InfoFrame]
3085 	 */
3086 	sdp->sdp_header.HB1 = drm_infoframe->type;
3087 	/*
3088 	 * Least Significant Eight Bits of (Data Byte Count – 1)
3089 	 * infoframe_size - 1
3090 	 */
3091 	sdp->sdp_header.HB2 = 0x1D;
3092 	/* INFOFRAME SDP Version Number */
3093 	sdp->sdp_header.HB3 = (0x13 << 2);
3094 	/* CTA Header Byte 2 (INFOFRAME Version Number) */
3095 	sdp->db[0] = drm_infoframe->version;
3096 	/* CTA Header Byte 3 (Length of INFOFRAME): HDMI_DRM_INFOFRAME_SIZE */
3097 	sdp->db[1] = drm_infoframe->length;
3098 	/*
3099 	 * Copy HDMI_DRM_INFOFRAME_SIZE size from a buffer after
3100 	 * HDMI_INFOFRAME_HEADER_SIZE
3101 	 */
3102 	BUILD_BUG_ON(sizeof(sdp->db) < HDMI_DRM_INFOFRAME_SIZE + 2);
3103 	memcpy(&sdp->db[2], &buf[HDMI_INFOFRAME_HEADER_SIZE],
3104 	       HDMI_DRM_INFOFRAME_SIZE);
3105 
3106 	/*
3107 	 * Size of DP infoframe sdp packet for HDR static metadata consists of
3108 	 * - DP SDP Header(struct dp_sdp_header): 4 bytes
3109 	 * - Two Data Blocks: 2 bytes
3110 	 *    CTA Header Byte2 (INFOFRAME Version Number)
3111 	 *    CTA Header Byte3 (Length of INFOFRAME)
3112 	 * - HDMI_DRM_INFOFRAME_SIZE: 26 bytes
3113 	 *
3114 	 * Prior to GEN11's GMP register size is identical to DP HDR static metadata
3115 	 * infoframe size. But GEN11+ has larger than that size, write_infoframe
3116 	 * will pad rest of the size.
3117 	 */
3118 	return sizeof(struct dp_sdp_header) + 2 + HDMI_DRM_INFOFRAME_SIZE;
3119 }
3120 
3121 static void intel_write_dp_sdp(struct intel_encoder *encoder,
3122 			       const struct intel_crtc_state *crtc_state,
3123 			       unsigned int type)
3124 {
3125 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3126 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3127 	struct dp_sdp sdp = {};
3128 	ssize_t len;
3129 
3130 	if ((crtc_state->infoframes.enable &
3131 	     intel_hdmi_infoframe_enable(type)) == 0)
3132 		return;
3133 
3134 	switch (type) {
3135 	case DP_SDP_VSC:
3136 		len = intel_dp_vsc_sdp_pack(&crtc_state->infoframes.vsc, &sdp,
3137 					    sizeof(sdp));
3138 		break;
3139 	case HDMI_PACKET_TYPE_GAMUT_METADATA:
3140 		len = intel_dp_hdr_metadata_infoframe_sdp_pack(dev_priv,
3141 							       &crtc_state->infoframes.drm.drm,
3142 							       &sdp, sizeof(sdp));
3143 		break;
3144 	default:
3145 		MISSING_CASE(type);
3146 		return;
3147 	}
3148 
3149 	if (drm_WARN_ON(&dev_priv->drm, len < 0))
3150 		return;
3151 
3152 	dig_port->write_infoframe(encoder, crtc_state, type, &sdp, len);
3153 }
3154 
3155 void intel_write_dp_vsc_sdp(struct intel_encoder *encoder,
3156 			    const struct intel_crtc_state *crtc_state,
3157 			    const struct drm_dp_vsc_sdp *vsc)
3158 {
3159 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3160 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3161 	struct dp_sdp sdp = {};
3162 	ssize_t len;
3163 
3164 	len = intel_dp_vsc_sdp_pack(vsc, &sdp, sizeof(sdp));
3165 
3166 	if (drm_WARN_ON(&dev_priv->drm, len < 0))
3167 		return;
3168 
3169 	dig_port->write_infoframe(encoder, crtc_state, DP_SDP_VSC,
3170 					&sdp, len);
3171 }
3172 
3173 void intel_dp_set_infoframes(struct intel_encoder *encoder,
3174 			     bool enable,
3175 			     const struct intel_crtc_state *crtc_state,
3176 			     const struct drm_connector_state *conn_state)
3177 {
3178 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3179 	i915_reg_t reg = HSW_TVIDEO_DIP_CTL(crtc_state->cpu_transcoder);
3180 	u32 dip_enable = VIDEO_DIP_ENABLE_AVI_HSW | VIDEO_DIP_ENABLE_GCP_HSW |
3181 			 VIDEO_DIP_ENABLE_VS_HSW | VIDEO_DIP_ENABLE_GMP_HSW |
3182 			 VIDEO_DIP_ENABLE_SPD_HSW | VIDEO_DIP_ENABLE_DRM_GLK;
3183 	u32 val = intel_de_read(dev_priv, reg) & ~dip_enable;
3184 
3185 	/* TODO: Add DSC case (DIP_ENABLE_PPS) */
3186 	/* When PSR is enabled, this routine doesn't disable VSC DIP */
3187 	if (!crtc_state->has_psr)
3188 		val &= ~VIDEO_DIP_ENABLE_VSC_HSW;
3189 
3190 	intel_de_write(dev_priv, reg, val);
3191 	intel_de_posting_read(dev_priv, reg);
3192 
3193 	if (!enable)
3194 		return;
3195 
3196 	/* When PSR is enabled, VSC SDP is handled by PSR routine */
3197 	if (!crtc_state->has_psr)
3198 		intel_write_dp_sdp(encoder, crtc_state, DP_SDP_VSC);
3199 
3200 	intel_write_dp_sdp(encoder, crtc_state, HDMI_PACKET_TYPE_GAMUT_METADATA);
3201 }
3202 
3203 static int intel_dp_vsc_sdp_unpack(struct drm_dp_vsc_sdp *vsc,
3204 				   const void *buffer, size_t size)
3205 {
3206 	const struct dp_sdp *sdp = buffer;
3207 
3208 	if (size < sizeof(struct dp_sdp))
3209 		return -EINVAL;
3210 
3211 	memset(vsc, 0, sizeof(*vsc));
3212 
3213 	if (sdp->sdp_header.HB0 != 0)
3214 		return -EINVAL;
3215 
3216 	if (sdp->sdp_header.HB1 != DP_SDP_VSC)
3217 		return -EINVAL;
3218 
3219 	vsc->sdp_type = sdp->sdp_header.HB1;
3220 	vsc->revision = sdp->sdp_header.HB2;
3221 	vsc->length = sdp->sdp_header.HB3;
3222 
3223 	if ((sdp->sdp_header.HB2 == 0x2 && sdp->sdp_header.HB3 == 0x8) ||
3224 	    (sdp->sdp_header.HB2 == 0x4 && sdp->sdp_header.HB3 == 0xe)) {
3225 		/*
3226 		 * - HB2 = 0x2, HB3 = 0x8
3227 		 *   VSC SDP supporting 3D stereo + PSR
3228 		 * - HB2 = 0x4, HB3 = 0xe
3229 		 *   VSC SDP supporting 3D stereo + PSR2 with Y-coordinate of
3230 		 *   first scan line of the SU region (applies to eDP v1.4b
3231 		 *   and higher).
3232 		 */
3233 		return 0;
3234 	} else if (sdp->sdp_header.HB2 == 0x5 && sdp->sdp_header.HB3 == 0x13) {
3235 		/*
3236 		 * - HB2 = 0x5, HB3 = 0x13
3237 		 *   VSC SDP supporting 3D stereo + PSR2 + Pixel Encoding/Colorimetry
3238 		 *   Format.
3239 		 */
3240 		vsc->pixelformat = (sdp->db[16] >> 4) & 0xf;
3241 		vsc->colorimetry = sdp->db[16] & 0xf;
3242 		vsc->dynamic_range = (sdp->db[17] >> 7) & 0x1;
3243 
3244 		switch (sdp->db[17] & 0x7) {
3245 		case 0x0:
3246 			vsc->bpc = 6;
3247 			break;
3248 		case 0x1:
3249 			vsc->bpc = 8;
3250 			break;
3251 		case 0x2:
3252 			vsc->bpc = 10;
3253 			break;
3254 		case 0x3:
3255 			vsc->bpc = 12;
3256 			break;
3257 		case 0x4:
3258 			vsc->bpc = 16;
3259 			break;
3260 		default:
3261 			MISSING_CASE(sdp->db[17] & 0x7);
3262 			return -EINVAL;
3263 		}
3264 
3265 		vsc->content_type = sdp->db[18] & 0x7;
3266 	} else {
3267 		return -EINVAL;
3268 	}
3269 
3270 	return 0;
3271 }
3272 
3273 static int
3274 intel_dp_hdr_metadata_infoframe_sdp_unpack(struct hdmi_drm_infoframe *drm_infoframe,
3275 					   const void *buffer, size_t size)
3276 {
3277 	int ret;
3278 
3279 	const struct dp_sdp *sdp = buffer;
3280 
3281 	if (size < sizeof(struct dp_sdp))
3282 		return -EINVAL;
3283 
3284 	if (sdp->sdp_header.HB0 != 0)
3285 		return -EINVAL;
3286 
3287 	if (sdp->sdp_header.HB1 != HDMI_INFOFRAME_TYPE_DRM)
3288 		return -EINVAL;
3289 
3290 	/*
3291 	 * Least Significant Eight Bits of (Data Byte Count – 1)
3292 	 * 1Dh (i.e., Data Byte Count = 30 bytes).
3293 	 */
3294 	if (sdp->sdp_header.HB2 != 0x1D)
3295 		return -EINVAL;
3296 
3297 	/* Most Significant Two Bits of (Data Byte Count – 1), Clear to 00b. */
3298 	if ((sdp->sdp_header.HB3 & 0x3) != 0)
3299 		return -EINVAL;
3300 
3301 	/* INFOFRAME SDP Version Number */
3302 	if (((sdp->sdp_header.HB3 >> 2) & 0x3f) != 0x13)
3303 		return -EINVAL;
3304 
3305 	/* CTA Header Byte 2 (INFOFRAME Version Number) */
3306 	if (sdp->db[0] != 1)
3307 		return -EINVAL;
3308 
3309 	/* CTA Header Byte 3 (Length of INFOFRAME): HDMI_DRM_INFOFRAME_SIZE */
3310 	if (sdp->db[1] != HDMI_DRM_INFOFRAME_SIZE)
3311 		return -EINVAL;
3312 
3313 	ret = hdmi_drm_infoframe_unpack_only(drm_infoframe, &sdp->db[2],
3314 					     HDMI_DRM_INFOFRAME_SIZE);
3315 
3316 	return ret;
3317 }
3318 
3319 static void intel_read_dp_vsc_sdp(struct intel_encoder *encoder,
3320 				  struct intel_crtc_state *crtc_state,
3321 				  struct drm_dp_vsc_sdp *vsc)
3322 {
3323 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3324 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3325 	unsigned int type = DP_SDP_VSC;
3326 	struct dp_sdp sdp = {};
3327 	int ret;
3328 
3329 	/* When PSR is enabled, VSC SDP is handled by PSR routine */
3330 	if (crtc_state->has_psr)
3331 		return;
3332 
3333 	if ((crtc_state->infoframes.enable &
3334 	     intel_hdmi_infoframe_enable(type)) == 0)
3335 		return;
3336 
3337 	dig_port->read_infoframe(encoder, crtc_state, type, &sdp, sizeof(sdp));
3338 
3339 	ret = intel_dp_vsc_sdp_unpack(vsc, &sdp, sizeof(sdp));
3340 
3341 	if (ret)
3342 		drm_dbg_kms(&dev_priv->drm, "Failed to unpack DP VSC SDP\n");
3343 }
3344 
3345 static void intel_read_dp_hdr_metadata_infoframe_sdp(struct intel_encoder *encoder,
3346 						     struct intel_crtc_state *crtc_state,
3347 						     struct hdmi_drm_infoframe *drm_infoframe)
3348 {
3349 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3350 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3351 	unsigned int type = HDMI_PACKET_TYPE_GAMUT_METADATA;
3352 	struct dp_sdp sdp = {};
3353 	int ret;
3354 
3355 	if ((crtc_state->infoframes.enable &
3356 	    intel_hdmi_infoframe_enable(type)) == 0)
3357 		return;
3358 
3359 	dig_port->read_infoframe(encoder, crtc_state, type, &sdp,
3360 				 sizeof(sdp));
3361 
3362 	ret = intel_dp_hdr_metadata_infoframe_sdp_unpack(drm_infoframe, &sdp,
3363 							 sizeof(sdp));
3364 
3365 	if (ret)
3366 		drm_dbg_kms(&dev_priv->drm,
3367 			    "Failed to unpack DP HDR Metadata Infoframe SDP\n");
3368 }
3369 
3370 void intel_read_dp_sdp(struct intel_encoder *encoder,
3371 		       struct intel_crtc_state *crtc_state,
3372 		       unsigned int type)
3373 {
3374 	switch (type) {
3375 	case DP_SDP_VSC:
3376 		intel_read_dp_vsc_sdp(encoder, crtc_state,
3377 				      &crtc_state->infoframes.vsc);
3378 		break;
3379 	case HDMI_PACKET_TYPE_GAMUT_METADATA:
3380 		intel_read_dp_hdr_metadata_infoframe_sdp(encoder, crtc_state,
3381 							 &crtc_state->infoframes.drm.drm);
3382 		break;
3383 	default:
3384 		MISSING_CASE(type);
3385 		break;
3386 	}
3387 }
3388 
3389 static u8 intel_dp_autotest_link_training(struct intel_dp *intel_dp)
3390 {
3391 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3392 	int status = 0;
3393 	int test_link_rate;
3394 	u8 test_lane_count, test_link_bw;
3395 	/* (DP CTS 1.2)
3396 	 * 4.3.1.11
3397 	 */
3398 	/* Read the TEST_LANE_COUNT and TEST_LINK_RTAE fields (DP CTS 3.1.4) */
3399 	status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_LANE_COUNT,
3400 				   &test_lane_count);
3401 
3402 	if (status <= 0) {
3403 		drm_dbg_kms(&i915->drm, "Lane count read failed\n");
3404 		return DP_TEST_NAK;
3405 	}
3406 	test_lane_count &= DP_MAX_LANE_COUNT_MASK;
3407 
3408 	status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_LINK_RATE,
3409 				   &test_link_bw);
3410 	if (status <= 0) {
3411 		drm_dbg_kms(&i915->drm, "Link Rate read failed\n");
3412 		return DP_TEST_NAK;
3413 	}
3414 	test_link_rate = drm_dp_bw_code_to_link_rate(test_link_bw);
3415 
3416 	/* Validate the requested link rate and lane count */
3417 	if (!intel_dp_link_params_valid(intel_dp, test_link_rate,
3418 					test_lane_count))
3419 		return DP_TEST_NAK;
3420 
3421 	intel_dp->compliance.test_lane_count = test_lane_count;
3422 	intel_dp->compliance.test_link_rate = test_link_rate;
3423 
3424 	return DP_TEST_ACK;
3425 }
3426 
3427 static u8 intel_dp_autotest_video_pattern(struct intel_dp *intel_dp)
3428 {
3429 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3430 	u8 test_pattern;
3431 	u8 test_misc;
3432 	__be16 h_width, v_height;
3433 	int status = 0;
3434 
3435 	/* Read the TEST_PATTERN (DP CTS 3.1.5) */
3436 	status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_PATTERN,
3437 				   &test_pattern);
3438 	if (status <= 0) {
3439 		drm_dbg_kms(&i915->drm, "Test pattern read failed\n");
3440 		return DP_TEST_NAK;
3441 	}
3442 	if (test_pattern != DP_COLOR_RAMP)
3443 		return DP_TEST_NAK;
3444 
3445 	status = drm_dp_dpcd_read(&intel_dp->aux, DP_TEST_H_WIDTH_HI,
3446 				  &h_width, 2);
3447 	if (status <= 0) {
3448 		drm_dbg_kms(&i915->drm, "H Width read failed\n");
3449 		return DP_TEST_NAK;
3450 	}
3451 
3452 	status = drm_dp_dpcd_read(&intel_dp->aux, DP_TEST_V_HEIGHT_HI,
3453 				  &v_height, 2);
3454 	if (status <= 0) {
3455 		drm_dbg_kms(&i915->drm, "V Height read failed\n");
3456 		return DP_TEST_NAK;
3457 	}
3458 
3459 	status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_MISC0,
3460 				   &test_misc);
3461 	if (status <= 0) {
3462 		drm_dbg_kms(&i915->drm, "TEST MISC read failed\n");
3463 		return DP_TEST_NAK;
3464 	}
3465 	if ((test_misc & DP_TEST_COLOR_FORMAT_MASK) != DP_COLOR_FORMAT_RGB)
3466 		return DP_TEST_NAK;
3467 	if (test_misc & DP_TEST_DYNAMIC_RANGE_CEA)
3468 		return DP_TEST_NAK;
3469 	switch (test_misc & DP_TEST_BIT_DEPTH_MASK) {
3470 	case DP_TEST_BIT_DEPTH_6:
3471 		intel_dp->compliance.test_data.bpc = 6;
3472 		break;
3473 	case DP_TEST_BIT_DEPTH_8:
3474 		intel_dp->compliance.test_data.bpc = 8;
3475 		break;
3476 	default:
3477 		return DP_TEST_NAK;
3478 	}
3479 
3480 	intel_dp->compliance.test_data.video_pattern = test_pattern;
3481 	intel_dp->compliance.test_data.hdisplay = be16_to_cpu(h_width);
3482 	intel_dp->compliance.test_data.vdisplay = be16_to_cpu(v_height);
3483 	/* Set test active flag here so userspace doesn't interrupt things */
3484 	intel_dp->compliance.test_active = true;
3485 
3486 	return DP_TEST_ACK;
3487 }
3488 
3489 static u8 intel_dp_autotest_edid(struct intel_dp *intel_dp)
3490 {
3491 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3492 	u8 test_result = DP_TEST_ACK;
3493 	struct intel_connector *intel_connector = intel_dp->attached_connector;
3494 	struct drm_connector *connector = &intel_connector->base;
3495 
3496 	if (intel_connector->detect_edid == NULL ||
3497 	    connector->edid_corrupt ||
3498 	    intel_dp->aux.i2c_defer_count > 6) {
3499 		/* Check EDID read for NACKs, DEFERs and corruption
3500 		 * (DP CTS 1.2 Core r1.1)
3501 		 *    4.2.2.4 : Failed EDID read, I2C_NAK
3502 		 *    4.2.2.5 : Failed EDID read, I2C_DEFER
3503 		 *    4.2.2.6 : EDID corruption detected
3504 		 * Use failsafe mode for all cases
3505 		 */
3506 		if (intel_dp->aux.i2c_nack_count > 0 ||
3507 			intel_dp->aux.i2c_defer_count > 0)
3508 			drm_dbg_kms(&i915->drm,
3509 				    "EDID read had %d NACKs, %d DEFERs\n",
3510 				    intel_dp->aux.i2c_nack_count,
3511 				    intel_dp->aux.i2c_defer_count);
3512 		intel_dp->compliance.test_data.edid = INTEL_DP_RESOLUTION_FAILSAFE;
3513 	} else {
3514 		struct edid *block = intel_connector->detect_edid;
3515 
3516 		/* We have to write the checksum
3517 		 * of the last block read
3518 		 */
3519 		block += intel_connector->detect_edid->extensions;
3520 
3521 		if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_EDID_CHECKSUM,
3522 				       block->checksum) <= 0)
3523 			drm_dbg_kms(&i915->drm,
3524 				    "Failed to write EDID checksum\n");
3525 
3526 		test_result = DP_TEST_ACK | DP_TEST_EDID_CHECKSUM_WRITE;
3527 		intel_dp->compliance.test_data.edid = INTEL_DP_RESOLUTION_PREFERRED;
3528 	}
3529 
3530 	/* Set test active flag here so userspace doesn't interrupt things */
3531 	intel_dp->compliance.test_active = true;
3532 
3533 	return test_result;
3534 }
3535 
3536 static void intel_dp_phy_pattern_update(struct intel_dp *intel_dp,
3537 					const struct intel_crtc_state *crtc_state)
3538 {
3539 	struct drm_i915_private *dev_priv =
3540 			to_i915(dp_to_dig_port(intel_dp)->base.base.dev);
3541 	struct drm_dp_phy_test_params *data =
3542 			&intel_dp->compliance.test_data.phytest;
3543 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3544 	enum pipe pipe = crtc->pipe;
3545 	u32 pattern_val;
3546 
3547 	switch (data->phy_pattern) {
3548 	case DP_PHY_TEST_PATTERN_NONE:
3549 		drm_dbg_kms(&dev_priv->drm, "Disable Phy Test Pattern\n");
3550 		intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe), 0x0);
3551 		break;
3552 	case DP_PHY_TEST_PATTERN_D10_2:
3553 		drm_dbg_kms(&dev_priv->drm, "Set D10.2 Phy Test Pattern\n");
3554 		intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe),
3555 			       DDI_DP_COMP_CTL_ENABLE | DDI_DP_COMP_CTL_D10_2);
3556 		break;
3557 	case DP_PHY_TEST_PATTERN_ERROR_COUNT:
3558 		drm_dbg_kms(&dev_priv->drm, "Set Error Count Phy Test Pattern\n");
3559 		intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe),
3560 			       DDI_DP_COMP_CTL_ENABLE |
3561 			       DDI_DP_COMP_CTL_SCRAMBLED_0);
3562 		break;
3563 	case DP_PHY_TEST_PATTERN_PRBS7:
3564 		drm_dbg_kms(&dev_priv->drm, "Set PRBS7 Phy Test Pattern\n");
3565 		intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe),
3566 			       DDI_DP_COMP_CTL_ENABLE | DDI_DP_COMP_CTL_PRBS7);
3567 		break;
3568 	case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
3569 		/*
3570 		 * FIXME: Ideally pattern should come from DPCD 0x250. As
3571 		 * current firmware of DPR-100 could not set it, so hardcoding
3572 		 * now for complaince test.
3573 		 */
3574 		drm_dbg_kms(&dev_priv->drm,
3575 			    "Set 80Bit Custom Phy Test Pattern 0x3e0f83e0 0x0f83e0f8 0x0000f83e\n");
3576 		pattern_val = 0x3e0f83e0;
3577 		intel_de_write(dev_priv, DDI_DP_COMP_PAT(pipe, 0), pattern_val);
3578 		pattern_val = 0x0f83e0f8;
3579 		intel_de_write(dev_priv, DDI_DP_COMP_PAT(pipe, 1), pattern_val);
3580 		pattern_val = 0x0000f83e;
3581 		intel_de_write(dev_priv, DDI_DP_COMP_PAT(pipe, 2), pattern_val);
3582 		intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe),
3583 			       DDI_DP_COMP_CTL_ENABLE |
3584 			       DDI_DP_COMP_CTL_CUSTOM80);
3585 		break;
3586 	case DP_PHY_TEST_PATTERN_CP2520:
3587 		/*
3588 		 * FIXME: Ideally pattern should come from DPCD 0x24A. As
3589 		 * current firmware of DPR-100 could not set it, so hardcoding
3590 		 * now for complaince test.
3591 		 */
3592 		drm_dbg_kms(&dev_priv->drm, "Set HBR2 compliance Phy Test Pattern\n");
3593 		pattern_val = 0xFB;
3594 		intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe),
3595 			       DDI_DP_COMP_CTL_ENABLE | DDI_DP_COMP_CTL_HBR2 |
3596 			       pattern_val);
3597 		break;
3598 	default:
3599 		WARN(1, "Invalid Phy Test Pattern\n");
3600 	}
3601 }
3602 
3603 static void
3604 intel_dp_autotest_phy_ddi_disable(struct intel_dp *intel_dp,
3605 				  const struct intel_crtc_state *crtc_state)
3606 {
3607 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
3608 	struct drm_device *dev = dig_port->base.base.dev;
3609 	struct drm_i915_private *dev_priv = to_i915(dev);
3610 	struct intel_crtc *crtc = to_intel_crtc(dig_port->base.base.crtc);
3611 	enum pipe pipe = crtc->pipe;
3612 	u32 trans_ddi_func_ctl_value, trans_conf_value, dp_tp_ctl_value;
3613 
3614 	trans_ddi_func_ctl_value = intel_de_read(dev_priv,
3615 						 TRANS_DDI_FUNC_CTL(pipe));
3616 	trans_conf_value = intel_de_read(dev_priv, PIPECONF(pipe));
3617 	dp_tp_ctl_value = intel_de_read(dev_priv, TGL_DP_TP_CTL(pipe));
3618 
3619 	trans_ddi_func_ctl_value &= ~(TRANS_DDI_FUNC_ENABLE |
3620 				      TGL_TRANS_DDI_PORT_MASK);
3621 	trans_conf_value &= ~PIPECONF_ENABLE;
3622 	dp_tp_ctl_value &= ~DP_TP_CTL_ENABLE;
3623 
3624 	intel_de_write(dev_priv, PIPECONF(pipe), trans_conf_value);
3625 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(pipe),
3626 		       trans_ddi_func_ctl_value);
3627 	intel_de_write(dev_priv, TGL_DP_TP_CTL(pipe), dp_tp_ctl_value);
3628 }
3629 
3630 static void
3631 intel_dp_autotest_phy_ddi_enable(struct intel_dp *intel_dp,
3632 				 const struct intel_crtc_state *crtc_state)
3633 {
3634 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
3635 	struct drm_device *dev = dig_port->base.base.dev;
3636 	struct drm_i915_private *dev_priv = to_i915(dev);
3637 	enum port port = dig_port->base.port;
3638 	struct intel_crtc *crtc = to_intel_crtc(dig_port->base.base.crtc);
3639 	enum pipe pipe = crtc->pipe;
3640 	u32 trans_ddi_func_ctl_value, trans_conf_value, dp_tp_ctl_value;
3641 
3642 	trans_ddi_func_ctl_value = intel_de_read(dev_priv,
3643 						 TRANS_DDI_FUNC_CTL(pipe));
3644 	trans_conf_value = intel_de_read(dev_priv, PIPECONF(pipe));
3645 	dp_tp_ctl_value = intel_de_read(dev_priv, TGL_DP_TP_CTL(pipe));
3646 
3647 	trans_ddi_func_ctl_value |= TRANS_DDI_FUNC_ENABLE |
3648 				    TGL_TRANS_DDI_SELECT_PORT(port);
3649 	trans_conf_value |= PIPECONF_ENABLE;
3650 	dp_tp_ctl_value |= DP_TP_CTL_ENABLE;
3651 
3652 	intel_de_write(dev_priv, PIPECONF(pipe), trans_conf_value);
3653 	intel_de_write(dev_priv, TGL_DP_TP_CTL(pipe), dp_tp_ctl_value);
3654 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(pipe),
3655 		       trans_ddi_func_ctl_value);
3656 }
3657 
3658 static void intel_dp_process_phy_request(struct intel_dp *intel_dp,
3659 					 const struct intel_crtc_state *crtc_state)
3660 {
3661 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3662 	struct drm_dp_phy_test_params *data =
3663 		&intel_dp->compliance.test_data.phytest;
3664 	u8 link_status[DP_LINK_STATUS_SIZE];
3665 
3666 	if (drm_dp_dpcd_read_phy_link_status(&intel_dp->aux, DP_PHY_DPRX,
3667 					     link_status) < 0) {
3668 		drm_dbg_kms(&i915->drm, "failed to get link status\n");
3669 		return;
3670 	}
3671 
3672 	/* retrieve vswing & pre-emphasis setting */
3673 	intel_dp_get_adjust_train(intel_dp, crtc_state, DP_PHY_DPRX,
3674 				  link_status);
3675 
3676 	intel_dp_autotest_phy_ddi_disable(intel_dp, crtc_state);
3677 
3678 	intel_dp_set_signal_levels(intel_dp, crtc_state, DP_PHY_DPRX);
3679 
3680 	intel_dp_phy_pattern_update(intel_dp, crtc_state);
3681 
3682 	intel_dp_autotest_phy_ddi_enable(intel_dp, crtc_state);
3683 
3684 	drm_dp_dpcd_write(&intel_dp->aux, DP_TRAINING_LANE0_SET,
3685 			  intel_dp->train_set, crtc_state->lane_count);
3686 
3687 	drm_dp_set_phy_test_pattern(&intel_dp->aux, data,
3688 				    link_status[DP_DPCD_REV]);
3689 }
3690 
3691 static u8 intel_dp_autotest_phy_pattern(struct intel_dp *intel_dp)
3692 {
3693 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3694 	struct drm_dp_phy_test_params *data =
3695 		&intel_dp->compliance.test_data.phytest;
3696 
3697 	if (drm_dp_get_phy_test_pattern(&intel_dp->aux, data)) {
3698 		drm_dbg_kms(&i915->drm, "DP Phy Test pattern AUX read failure\n");
3699 		return DP_TEST_NAK;
3700 	}
3701 
3702 	/* Set test active flag here so userspace doesn't interrupt things */
3703 	intel_dp->compliance.test_active = true;
3704 
3705 	return DP_TEST_ACK;
3706 }
3707 
3708 static void intel_dp_handle_test_request(struct intel_dp *intel_dp)
3709 {
3710 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3711 	u8 response = DP_TEST_NAK;
3712 	u8 request = 0;
3713 	int status;
3714 
3715 	status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_REQUEST, &request);
3716 	if (status <= 0) {
3717 		drm_dbg_kms(&i915->drm,
3718 			    "Could not read test request from sink\n");
3719 		goto update_status;
3720 	}
3721 
3722 	switch (request) {
3723 	case DP_TEST_LINK_TRAINING:
3724 		drm_dbg_kms(&i915->drm, "LINK_TRAINING test requested\n");
3725 		response = intel_dp_autotest_link_training(intel_dp);
3726 		break;
3727 	case DP_TEST_LINK_VIDEO_PATTERN:
3728 		drm_dbg_kms(&i915->drm, "TEST_PATTERN test requested\n");
3729 		response = intel_dp_autotest_video_pattern(intel_dp);
3730 		break;
3731 	case DP_TEST_LINK_EDID_READ:
3732 		drm_dbg_kms(&i915->drm, "EDID test requested\n");
3733 		response = intel_dp_autotest_edid(intel_dp);
3734 		break;
3735 	case DP_TEST_LINK_PHY_TEST_PATTERN:
3736 		drm_dbg_kms(&i915->drm, "PHY_PATTERN test requested\n");
3737 		response = intel_dp_autotest_phy_pattern(intel_dp);
3738 		break;
3739 	default:
3740 		drm_dbg_kms(&i915->drm, "Invalid test request '%02x'\n",
3741 			    request);
3742 		break;
3743 	}
3744 
3745 	if (response & DP_TEST_ACK)
3746 		intel_dp->compliance.test_type = request;
3747 
3748 update_status:
3749 	status = drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_RESPONSE, response);
3750 	if (status <= 0)
3751 		drm_dbg_kms(&i915->drm,
3752 			    "Could not write test response to sink\n");
3753 }
3754 
3755 static bool intel_dp_link_ok(struct intel_dp *intel_dp,
3756 			     u8 link_status[DP_LINK_STATUS_SIZE])
3757 {
3758 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3759 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3760 	bool uhbr = intel_dp->link_rate >= 1000000;
3761 	bool ok;
3762 
3763 	if (uhbr)
3764 		ok = drm_dp_128b132b_lane_channel_eq_done(link_status,
3765 							  intel_dp->lane_count);
3766 	else
3767 		ok = drm_dp_channel_eq_ok(link_status, intel_dp->lane_count);
3768 
3769 	if (ok)
3770 		return true;
3771 
3772 	intel_dp_dump_link_status(intel_dp, DP_PHY_DPRX, link_status);
3773 	drm_dbg_kms(&i915->drm,
3774 		    "[ENCODER:%d:%s] %s link not ok, retraining\n",
3775 		    encoder->base.base.id, encoder->base.name,
3776 		    uhbr ? "128b/132b" : "8b/10b");
3777 
3778 	return false;
3779 }
3780 
3781 static void
3782 intel_dp_mst_hpd_irq(struct intel_dp *intel_dp, u8 *esi, u8 *ack)
3783 {
3784 	bool handled = false;
3785 
3786 	drm_dp_mst_hpd_irq(&intel_dp->mst_mgr, esi, &handled);
3787 	if (handled)
3788 		ack[1] |= esi[1] & (DP_DOWN_REP_MSG_RDY | DP_UP_REQ_MSG_RDY);
3789 
3790 	if (esi[1] & DP_CP_IRQ) {
3791 		intel_hdcp_handle_cp_irq(intel_dp->attached_connector);
3792 		ack[1] |= DP_CP_IRQ;
3793 	}
3794 }
3795 
3796 static bool intel_dp_mst_link_status(struct intel_dp *intel_dp)
3797 {
3798 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3799 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3800 	u8 link_status[DP_LINK_STATUS_SIZE] = {};
3801 	const size_t esi_link_status_size = DP_LINK_STATUS_SIZE - 2;
3802 
3803 	if (drm_dp_dpcd_read(&intel_dp->aux, DP_LANE0_1_STATUS_ESI, link_status,
3804 			     esi_link_status_size) != esi_link_status_size) {
3805 		drm_err(&i915->drm,
3806 			"[ENCODER:%d:%s] Failed to read link status\n",
3807 			encoder->base.base.id, encoder->base.name);
3808 		return false;
3809 	}
3810 
3811 	return intel_dp_link_ok(intel_dp, link_status);
3812 }
3813 
3814 /**
3815  * intel_dp_check_mst_status - service any pending MST interrupts, check link status
3816  * @intel_dp: Intel DP struct
3817  *
3818  * Read any pending MST interrupts, call MST core to handle these and ack the
3819  * interrupts. Check if the main and AUX link state is ok.
3820  *
3821  * Returns:
3822  * - %true if pending interrupts were serviced (or no interrupts were
3823  *   pending) w/o detecting an error condition.
3824  * - %false if an error condition - like AUX failure or a loss of link - is
3825  *   detected, which needs servicing from the hotplug work.
3826  */
3827 static bool
3828 intel_dp_check_mst_status(struct intel_dp *intel_dp)
3829 {
3830 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3831 	bool link_ok = true;
3832 
3833 	drm_WARN_ON_ONCE(&i915->drm, intel_dp->active_mst_links < 0);
3834 
3835 	for (;;) {
3836 		u8 esi[4] = {};
3837 		u8 ack[4] = {};
3838 
3839 		if (!intel_dp_get_sink_irq_esi(intel_dp, esi)) {
3840 			drm_dbg_kms(&i915->drm,
3841 				    "failed to get ESI - device may have failed\n");
3842 			link_ok = false;
3843 
3844 			break;
3845 		}
3846 
3847 		drm_dbg_kms(&i915->drm, "DPRX ESI: %4ph\n", esi);
3848 
3849 		if (intel_dp->active_mst_links > 0 && link_ok &&
3850 		    esi[3] & LINK_STATUS_CHANGED) {
3851 			if (!intel_dp_mst_link_status(intel_dp))
3852 				link_ok = false;
3853 			ack[3] |= LINK_STATUS_CHANGED;
3854 		}
3855 
3856 		intel_dp_mst_hpd_irq(intel_dp, esi, ack);
3857 
3858 		if (!memchr_inv(ack, 0, sizeof(ack)))
3859 			break;
3860 
3861 		if (!intel_dp_ack_sink_irq_esi(intel_dp, ack))
3862 			drm_dbg_kms(&i915->drm, "Failed to ack ESI\n");
3863 	}
3864 
3865 	return link_ok;
3866 }
3867 
3868 static void
3869 intel_dp_handle_hdmi_link_status_change(struct intel_dp *intel_dp)
3870 {
3871 	bool is_active;
3872 	u8 buf = 0;
3873 
3874 	is_active = drm_dp_pcon_hdmi_link_active(&intel_dp->aux);
3875 	if (intel_dp->frl.is_trained && !is_active) {
3876 		if (drm_dp_dpcd_readb(&intel_dp->aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf) < 0)
3877 			return;
3878 
3879 		buf &=  ~DP_PCON_ENABLE_HDMI_LINK;
3880 		if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_PCON_HDMI_LINK_CONFIG_1, buf) < 0)
3881 			return;
3882 
3883 		drm_dp_pcon_hdmi_frl_link_error_count(&intel_dp->aux, &intel_dp->attached_connector->base);
3884 
3885 		/* Restart FRL training or fall back to TMDS mode */
3886 		intel_dp_check_frl_training(intel_dp);
3887 	}
3888 }
3889 
3890 static bool
3891 intel_dp_needs_link_retrain(struct intel_dp *intel_dp)
3892 {
3893 	u8 link_status[DP_LINK_STATUS_SIZE];
3894 
3895 	if (!intel_dp->link_trained)
3896 		return false;
3897 
3898 	/*
3899 	 * While PSR source HW is enabled, it will control main-link sending
3900 	 * frames, enabling and disabling it so trying to do a retrain will fail
3901 	 * as the link would or not be on or it could mix training patterns
3902 	 * and frame data at the same time causing retrain to fail.
3903 	 * Also when exiting PSR, HW will retrain the link anyways fixing
3904 	 * any link status error.
3905 	 */
3906 	if (intel_psr_enabled(intel_dp))
3907 		return false;
3908 
3909 	if (drm_dp_dpcd_read_phy_link_status(&intel_dp->aux, DP_PHY_DPRX,
3910 					     link_status) < 0)
3911 		return false;
3912 
3913 	/*
3914 	 * Validate the cached values of intel_dp->link_rate and
3915 	 * intel_dp->lane_count before attempting to retrain.
3916 	 *
3917 	 * FIXME would be nice to user the crtc state here, but since
3918 	 * we need to call this from the short HPD handler that seems
3919 	 * a bit hard.
3920 	 */
3921 	if (!intel_dp_link_params_valid(intel_dp, intel_dp->link_rate,
3922 					intel_dp->lane_count))
3923 		return false;
3924 
3925 	/* Retrain if link not ok */
3926 	return !intel_dp_link_ok(intel_dp, link_status);
3927 }
3928 
3929 static bool intel_dp_has_connector(struct intel_dp *intel_dp,
3930 				   const struct drm_connector_state *conn_state)
3931 {
3932 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3933 	struct intel_encoder *encoder;
3934 	enum pipe pipe;
3935 
3936 	if (!conn_state->best_encoder)
3937 		return false;
3938 
3939 	/* SST */
3940 	encoder = &dp_to_dig_port(intel_dp)->base;
3941 	if (conn_state->best_encoder == &encoder->base)
3942 		return true;
3943 
3944 	/* MST */
3945 	for_each_pipe(i915, pipe) {
3946 		encoder = &intel_dp->mst_encoders[pipe]->base;
3947 		if (conn_state->best_encoder == &encoder->base)
3948 			return true;
3949 	}
3950 
3951 	return false;
3952 }
3953 
3954 static int intel_dp_prep_link_retrain(struct intel_dp *intel_dp,
3955 				      struct drm_modeset_acquire_ctx *ctx,
3956 				      u8 *pipe_mask)
3957 {
3958 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3959 	struct drm_connector_list_iter conn_iter;
3960 	struct intel_connector *connector;
3961 	int ret = 0;
3962 
3963 	*pipe_mask = 0;
3964 
3965 	if (!intel_dp_needs_link_retrain(intel_dp))
3966 		return 0;
3967 
3968 	drm_connector_list_iter_begin(&i915->drm, &conn_iter);
3969 	for_each_intel_connector_iter(connector, &conn_iter) {
3970 		struct drm_connector_state *conn_state =
3971 			connector->base.state;
3972 		struct intel_crtc_state *crtc_state;
3973 		struct intel_crtc *crtc;
3974 
3975 		if (!intel_dp_has_connector(intel_dp, conn_state))
3976 			continue;
3977 
3978 		crtc = to_intel_crtc(conn_state->crtc);
3979 		if (!crtc)
3980 			continue;
3981 
3982 		ret = drm_modeset_lock(&crtc->base.mutex, ctx);
3983 		if (ret)
3984 			break;
3985 
3986 		crtc_state = to_intel_crtc_state(crtc->base.state);
3987 
3988 		drm_WARN_ON(&i915->drm, !intel_crtc_has_dp_encoder(crtc_state));
3989 
3990 		if (!crtc_state->hw.active)
3991 			continue;
3992 
3993 		if (conn_state->commit &&
3994 		    !try_wait_for_completion(&conn_state->commit->hw_done))
3995 			continue;
3996 
3997 		*pipe_mask |= BIT(crtc->pipe);
3998 	}
3999 	drm_connector_list_iter_end(&conn_iter);
4000 
4001 	if (!intel_dp_needs_link_retrain(intel_dp))
4002 		*pipe_mask = 0;
4003 
4004 	return ret;
4005 }
4006 
4007 static bool intel_dp_is_connected(struct intel_dp *intel_dp)
4008 {
4009 	struct intel_connector *connector = intel_dp->attached_connector;
4010 
4011 	return connector->base.status == connector_status_connected ||
4012 		intel_dp->is_mst;
4013 }
4014 
4015 int intel_dp_retrain_link(struct intel_encoder *encoder,
4016 			  struct drm_modeset_acquire_ctx *ctx)
4017 {
4018 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4019 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4020 	struct intel_crtc *crtc;
4021 	u8 pipe_mask;
4022 	int ret;
4023 
4024 	if (!intel_dp_is_connected(intel_dp))
4025 		return 0;
4026 
4027 	ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
4028 			       ctx);
4029 	if (ret)
4030 		return ret;
4031 
4032 	ret = intel_dp_prep_link_retrain(intel_dp, ctx, &pipe_mask);
4033 	if (ret)
4034 		return ret;
4035 
4036 	if (pipe_mask == 0)
4037 		return 0;
4038 
4039 	drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s] retraining link\n",
4040 		    encoder->base.base.id, encoder->base.name);
4041 
4042 	for_each_intel_crtc_in_pipe_mask(&dev_priv->drm, crtc, pipe_mask) {
4043 		const struct intel_crtc_state *crtc_state =
4044 			to_intel_crtc_state(crtc->base.state);
4045 
4046 		/* Suppress underruns caused by re-training */
4047 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
4048 		if (crtc_state->has_pch_encoder)
4049 			intel_set_pch_fifo_underrun_reporting(dev_priv,
4050 							      intel_crtc_pch_transcoder(crtc), false);
4051 	}
4052 
4053 	for_each_intel_crtc_in_pipe_mask(&dev_priv->drm, crtc, pipe_mask) {
4054 		const struct intel_crtc_state *crtc_state =
4055 			to_intel_crtc_state(crtc->base.state);
4056 
4057 		/* retrain on the MST master transcoder */
4058 		if (DISPLAY_VER(dev_priv) >= 12 &&
4059 		    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST) &&
4060 		    !intel_dp_mst_is_master_trans(crtc_state))
4061 			continue;
4062 
4063 		intel_dp_check_frl_training(intel_dp);
4064 		intel_dp_pcon_dsc_configure(intel_dp, crtc_state);
4065 		intel_dp_start_link_train(intel_dp, crtc_state);
4066 		intel_dp_stop_link_train(intel_dp, crtc_state);
4067 		break;
4068 	}
4069 
4070 	for_each_intel_crtc_in_pipe_mask(&dev_priv->drm, crtc, pipe_mask) {
4071 		const struct intel_crtc_state *crtc_state =
4072 			to_intel_crtc_state(crtc->base.state);
4073 
4074 		/* Keep underrun reporting disabled until things are stable */
4075 		intel_crtc_wait_for_next_vblank(crtc);
4076 
4077 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
4078 		if (crtc_state->has_pch_encoder)
4079 			intel_set_pch_fifo_underrun_reporting(dev_priv,
4080 							      intel_crtc_pch_transcoder(crtc), true);
4081 	}
4082 
4083 	return 0;
4084 }
4085 
4086 static int intel_dp_prep_phy_test(struct intel_dp *intel_dp,
4087 				  struct drm_modeset_acquire_ctx *ctx,
4088 				  u8 *pipe_mask)
4089 {
4090 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4091 	struct drm_connector_list_iter conn_iter;
4092 	struct intel_connector *connector;
4093 	int ret = 0;
4094 
4095 	*pipe_mask = 0;
4096 
4097 	drm_connector_list_iter_begin(&i915->drm, &conn_iter);
4098 	for_each_intel_connector_iter(connector, &conn_iter) {
4099 		struct drm_connector_state *conn_state =
4100 			connector->base.state;
4101 		struct intel_crtc_state *crtc_state;
4102 		struct intel_crtc *crtc;
4103 
4104 		if (!intel_dp_has_connector(intel_dp, conn_state))
4105 			continue;
4106 
4107 		crtc = to_intel_crtc(conn_state->crtc);
4108 		if (!crtc)
4109 			continue;
4110 
4111 		ret = drm_modeset_lock(&crtc->base.mutex, ctx);
4112 		if (ret)
4113 			break;
4114 
4115 		crtc_state = to_intel_crtc_state(crtc->base.state);
4116 
4117 		drm_WARN_ON(&i915->drm, !intel_crtc_has_dp_encoder(crtc_state));
4118 
4119 		if (!crtc_state->hw.active)
4120 			continue;
4121 
4122 		if (conn_state->commit &&
4123 		    !try_wait_for_completion(&conn_state->commit->hw_done))
4124 			continue;
4125 
4126 		*pipe_mask |= BIT(crtc->pipe);
4127 	}
4128 	drm_connector_list_iter_end(&conn_iter);
4129 
4130 	return ret;
4131 }
4132 
4133 static int intel_dp_do_phy_test(struct intel_encoder *encoder,
4134 				struct drm_modeset_acquire_ctx *ctx)
4135 {
4136 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4137 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4138 	struct intel_crtc *crtc;
4139 	u8 pipe_mask;
4140 	int ret;
4141 
4142 	ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
4143 			       ctx);
4144 	if (ret)
4145 		return ret;
4146 
4147 	ret = intel_dp_prep_phy_test(intel_dp, ctx, &pipe_mask);
4148 	if (ret)
4149 		return ret;
4150 
4151 	if (pipe_mask == 0)
4152 		return 0;
4153 
4154 	drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s] PHY test\n",
4155 		    encoder->base.base.id, encoder->base.name);
4156 
4157 	for_each_intel_crtc_in_pipe_mask(&dev_priv->drm, crtc, pipe_mask) {
4158 		const struct intel_crtc_state *crtc_state =
4159 			to_intel_crtc_state(crtc->base.state);
4160 
4161 		/* test on the MST master transcoder */
4162 		if (DISPLAY_VER(dev_priv) >= 12 &&
4163 		    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST) &&
4164 		    !intel_dp_mst_is_master_trans(crtc_state))
4165 			continue;
4166 
4167 		intel_dp_process_phy_request(intel_dp, crtc_state);
4168 		break;
4169 	}
4170 
4171 	return 0;
4172 }
4173 
4174 void intel_dp_phy_test(struct intel_encoder *encoder)
4175 {
4176 	struct drm_modeset_acquire_ctx ctx;
4177 	int ret;
4178 
4179 	drm_modeset_acquire_init(&ctx, 0);
4180 
4181 	for (;;) {
4182 		ret = intel_dp_do_phy_test(encoder, &ctx);
4183 
4184 		if (ret == -EDEADLK) {
4185 			drm_modeset_backoff(&ctx);
4186 			continue;
4187 		}
4188 
4189 		break;
4190 	}
4191 
4192 	drm_modeset_drop_locks(&ctx);
4193 	drm_modeset_acquire_fini(&ctx);
4194 	drm_WARN(encoder->base.dev, ret,
4195 		 "Acquiring modeset locks failed with %i\n", ret);
4196 }
4197 
4198 static void intel_dp_check_device_service_irq(struct intel_dp *intel_dp)
4199 {
4200 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4201 	u8 val;
4202 
4203 	if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
4204 		return;
4205 
4206 	if (drm_dp_dpcd_readb(&intel_dp->aux,
4207 			      DP_DEVICE_SERVICE_IRQ_VECTOR, &val) != 1 || !val)
4208 		return;
4209 
4210 	drm_dp_dpcd_writeb(&intel_dp->aux, DP_DEVICE_SERVICE_IRQ_VECTOR, val);
4211 
4212 	if (val & DP_AUTOMATED_TEST_REQUEST)
4213 		intel_dp_handle_test_request(intel_dp);
4214 
4215 	if (val & DP_CP_IRQ)
4216 		intel_hdcp_handle_cp_irq(intel_dp->attached_connector);
4217 
4218 	if (val & DP_SINK_SPECIFIC_IRQ)
4219 		drm_dbg_kms(&i915->drm, "Sink specific irq unhandled\n");
4220 }
4221 
4222 static void intel_dp_check_link_service_irq(struct intel_dp *intel_dp)
4223 {
4224 	u8 val;
4225 
4226 	if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
4227 		return;
4228 
4229 	if (drm_dp_dpcd_readb(&intel_dp->aux,
4230 			      DP_LINK_SERVICE_IRQ_VECTOR_ESI0, &val) != 1 || !val)
4231 		return;
4232 
4233 	if (drm_dp_dpcd_writeb(&intel_dp->aux,
4234 			       DP_LINK_SERVICE_IRQ_VECTOR_ESI0, val) != 1)
4235 		return;
4236 
4237 	if (val & HDMI_LINK_STATUS_CHANGED)
4238 		intel_dp_handle_hdmi_link_status_change(intel_dp);
4239 }
4240 
4241 /*
4242  * According to DP spec
4243  * 5.1.2:
4244  *  1. Read DPCD
4245  *  2. Configure link according to Receiver Capabilities
4246  *  3. Use Link Training from 2.5.3.3 and 3.5.1.3
4247  *  4. Check link status on receipt of hot-plug interrupt
4248  *
4249  * intel_dp_short_pulse -  handles short pulse interrupts
4250  * when full detection is not required.
4251  * Returns %true if short pulse is handled and full detection
4252  * is NOT required and %false otherwise.
4253  */
4254 static bool
4255 intel_dp_short_pulse(struct intel_dp *intel_dp)
4256 {
4257 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
4258 	u8 old_sink_count = intel_dp->sink_count;
4259 	bool ret;
4260 
4261 	/*
4262 	 * Clearing compliance test variables to allow capturing
4263 	 * of values for next automated test request.
4264 	 */
4265 	memset(&intel_dp->compliance, 0, sizeof(intel_dp->compliance));
4266 
4267 	/*
4268 	 * Now read the DPCD to see if it's actually running
4269 	 * If the current value of sink count doesn't match with
4270 	 * the value that was stored earlier or dpcd read failed
4271 	 * we need to do full detection
4272 	 */
4273 	ret = intel_dp_get_dpcd(intel_dp);
4274 
4275 	if ((old_sink_count != intel_dp->sink_count) || !ret) {
4276 		/* No need to proceed if we are going to do full detect */
4277 		return false;
4278 	}
4279 
4280 	intel_dp_check_device_service_irq(intel_dp);
4281 	intel_dp_check_link_service_irq(intel_dp);
4282 
4283 	/* Handle CEC interrupts, if any */
4284 	drm_dp_cec_irq(&intel_dp->aux);
4285 
4286 	/* defer to the hotplug work for link retraining if needed */
4287 	if (intel_dp_needs_link_retrain(intel_dp))
4288 		return false;
4289 
4290 	intel_psr_short_pulse(intel_dp);
4291 
4292 	switch (intel_dp->compliance.test_type) {
4293 	case DP_TEST_LINK_TRAINING:
4294 		drm_dbg_kms(&dev_priv->drm,
4295 			    "Link Training Compliance Test requested\n");
4296 		/* Send a Hotplug Uevent to userspace to start modeset */
4297 		drm_kms_helper_hotplug_event(&dev_priv->drm);
4298 		break;
4299 	case DP_TEST_LINK_PHY_TEST_PATTERN:
4300 		drm_dbg_kms(&dev_priv->drm,
4301 			    "PHY test pattern Compliance Test requested\n");
4302 		/*
4303 		 * Schedule long hpd to do the test
4304 		 *
4305 		 * FIXME get rid of the ad-hoc phy test modeset code
4306 		 * and properly incorporate it into the normal modeset.
4307 		 */
4308 		return false;
4309 	}
4310 
4311 	return true;
4312 }
4313 
4314 /* XXX this is probably wrong for multiple downstream ports */
4315 static enum drm_connector_status
4316 intel_dp_detect_dpcd(struct intel_dp *intel_dp)
4317 {
4318 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4319 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4320 	u8 *dpcd = intel_dp->dpcd;
4321 	u8 type;
4322 
4323 	if (drm_WARN_ON(&i915->drm, intel_dp_is_edp(intel_dp)))
4324 		return connector_status_connected;
4325 
4326 	lspcon_resume(dig_port);
4327 
4328 	if (!intel_dp_get_dpcd(intel_dp))
4329 		return connector_status_disconnected;
4330 
4331 	/* if there's no downstream port, we're done */
4332 	if (!drm_dp_is_branch(dpcd))
4333 		return connector_status_connected;
4334 
4335 	/* If we're HPD-aware, SINK_COUNT changes dynamically */
4336 	if (intel_dp_has_sink_count(intel_dp) &&
4337 	    intel_dp->downstream_ports[0] & DP_DS_PORT_HPD) {
4338 		return intel_dp->sink_count ?
4339 		connector_status_connected : connector_status_disconnected;
4340 	}
4341 
4342 	if (intel_dp_can_mst(intel_dp))
4343 		return connector_status_connected;
4344 
4345 	/* If no HPD, poke DDC gently */
4346 	if (drm_probe_ddc(&intel_dp->aux.ddc))
4347 		return connector_status_connected;
4348 
4349 	/* Well we tried, say unknown for unreliable port types */
4350 	if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11) {
4351 		type = intel_dp->downstream_ports[0] & DP_DS_PORT_TYPE_MASK;
4352 		if (type == DP_DS_PORT_TYPE_VGA ||
4353 		    type == DP_DS_PORT_TYPE_NON_EDID)
4354 			return connector_status_unknown;
4355 	} else {
4356 		type = intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
4357 			DP_DWN_STRM_PORT_TYPE_MASK;
4358 		if (type == DP_DWN_STRM_PORT_TYPE_ANALOG ||
4359 		    type == DP_DWN_STRM_PORT_TYPE_OTHER)
4360 			return connector_status_unknown;
4361 	}
4362 
4363 	/* Anything else is out of spec, warn and ignore */
4364 	drm_dbg_kms(&i915->drm, "Broken DP branch device, ignoring\n");
4365 	return connector_status_disconnected;
4366 }
4367 
4368 static enum drm_connector_status
4369 edp_detect(struct intel_dp *intel_dp)
4370 {
4371 	return connector_status_connected;
4372 }
4373 
4374 /*
4375  * intel_digital_port_connected - is the specified port connected?
4376  * @encoder: intel_encoder
4377  *
4378  * In cases where there's a connector physically connected but it can't be used
4379  * by our hardware we also return false, since the rest of the driver should
4380  * pretty much treat the port as disconnected. This is relevant for type-C
4381  * (starting on ICL) where there's ownership involved.
4382  *
4383  * Return %true if port is connected, %false otherwise.
4384  */
4385 bool intel_digital_port_connected(struct intel_encoder *encoder)
4386 {
4387 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4388 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
4389 	bool is_connected = false;
4390 	intel_wakeref_t wakeref;
4391 
4392 	with_intel_display_power(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref)
4393 		is_connected = dig_port->connected(encoder);
4394 
4395 	return is_connected;
4396 }
4397 
4398 static struct edid *
4399 intel_dp_get_edid(struct intel_dp *intel_dp)
4400 {
4401 	struct intel_connector *intel_connector = intel_dp->attached_connector;
4402 
4403 	/* use cached edid if we have one */
4404 	if (intel_connector->edid) {
4405 		/* invalid edid */
4406 		if (IS_ERR(intel_connector->edid))
4407 			return NULL;
4408 
4409 		return drm_edid_duplicate(intel_connector->edid);
4410 	} else
4411 		return drm_get_edid(&intel_connector->base,
4412 				    &intel_dp->aux.ddc);
4413 }
4414 
4415 static void
4416 intel_dp_update_dfp(struct intel_dp *intel_dp,
4417 		    const struct edid *edid)
4418 {
4419 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4420 	struct intel_connector *connector = intel_dp->attached_connector;
4421 
4422 	intel_dp->dfp.max_bpc =
4423 		drm_dp_downstream_max_bpc(intel_dp->dpcd,
4424 					  intel_dp->downstream_ports, edid);
4425 
4426 	intel_dp->dfp.max_dotclock =
4427 		drm_dp_downstream_max_dotclock(intel_dp->dpcd,
4428 					       intel_dp->downstream_ports);
4429 
4430 	intel_dp->dfp.min_tmds_clock =
4431 		drm_dp_downstream_min_tmds_clock(intel_dp->dpcd,
4432 						 intel_dp->downstream_ports,
4433 						 edid);
4434 	intel_dp->dfp.max_tmds_clock =
4435 		drm_dp_downstream_max_tmds_clock(intel_dp->dpcd,
4436 						 intel_dp->downstream_ports,
4437 						 edid);
4438 
4439 	intel_dp->dfp.pcon_max_frl_bw =
4440 		drm_dp_get_pcon_max_frl_bw(intel_dp->dpcd,
4441 					   intel_dp->downstream_ports);
4442 
4443 	drm_dbg_kms(&i915->drm,
4444 		    "[CONNECTOR:%d:%s] DFP max bpc %d, max dotclock %d, TMDS clock %d-%d, PCON Max FRL BW %dGbps\n",
4445 		    connector->base.base.id, connector->base.name,
4446 		    intel_dp->dfp.max_bpc,
4447 		    intel_dp->dfp.max_dotclock,
4448 		    intel_dp->dfp.min_tmds_clock,
4449 		    intel_dp->dfp.max_tmds_clock,
4450 		    intel_dp->dfp.pcon_max_frl_bw);
4451 
4452 	intel_dp_get_pcon_dsc_cap(intel_dp);
4453 }
4454 
4455 static void
4456 intel_dp_update_420(struct intel_dp *intel_dp)
4457 {
4458 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4459 	struct intel_connector *connector = intel_dp->attached_connector;
4460 	bool is_branch, ycbcr_420_passthrough, ycbcr_444_to_420, rgb_to_ycbcr;
4461 
4462 	/* No YCbCr output support on gmch platforms */
4463 	if (HAS_GMCH(i915))
4464 		return;
4465 
4466 	/*
4467 	 * ILK doesn't seem capable of DP YCbCr output. The
4468 	 * displayed image is severly corrupted. SNB+ is fine.
4469 	 */
4470 	if (IS_IRONLAKE(i915))
4471 		return;
4472 
4473 	is_branch = drm_dp_is_branch(intel_dp->dpcd);
4474 	ycbcr_420_passthrough =
4475 		drm_dp_downstream_420_passthrough(intel_dp->dpcd,
4476 						  intel_dp->downstream_ports);
4477 	/* on-board LSPCON always assumed to support 4:4:4->4:2:0 conversion */
4478 	ycbcr_444_to_420 =
4479 		dp_to_dig_port(intel_dp)->lspcon.active ||
4480 		drm_dp_downstream_444_to_420_conversion(intel_dp->dpcd,
4481 							intel_dp->downstream_ports);
4482 	rgb_to_ycbcr = drm_dp_downstream_rgb_to_ycbcr_conversion(intel_dp->dpcd,
4483 								 intel_dp->downstream_ports,
4484 								 DP_DS_HDMI_BT709_RGB_YCBCR_CONV);
4485 
4486 	if (DISPLAY_VER(i915) >= 11) {
4487 		/* Let PCON convert from RGB->YCbCr if possible */
4488 		if (is_branch && rgb_to_ycbcr && ycbcr_444_to_420) {
4489 			intel_dp->dfp.rgb_to_ycbcr = true;
4490 			intel_dp->dfp.ycbcr_444_to_420 = true;
4491 			connector->base.ycbcr_420_allowed = true;
4492 		} else {
4493 		/* Prefer 4:2:0 passthrough over 4:4:4->4:2:0 conversion */
4494 			intel_dp->dfp.ycbcr_444_to_420 =
4495 				ycbcr_444_to_420 && !ycbcr_420_passthrough;
4496 
4497 			connector->base.ycbcr_420_allowed =
4498 				!is_branch || ycbcr_444_to_420 || ycbcr_420_passthrough;
4499 		}
4500 	} else {
4501 		/* 4:4:4->4:2:0 conversion is the only way */
4502 		intel_dp->dfp.ycbcr_444_to_420 = ycbcr_444_to_420;
4503 
4504 		connector->base.ycbcr_420_allowed = ycbcr_444_to_420;
4505 	}
4506 
4507 	drm_dbg_kms(&i915->drm,
4508 		    "[CONNECTOR:%d:%s] RGB->YcbCr conversion? %s, YCbCr 4:2:0 allowed? %s, YCbCr 4:4:4->4:2:0 conversion? %s\n",
4509 		    connector->base.base.id, connector->base.name,
4510 		    str_yes_no(intel_dp->dfp.rgb_to_ycbcr),
4511 		    str_yes_no(connector->base.ycbcr_420_allowed),
4512 		    str_yes_no(intel_dp->dfp.ycbcr_444_to_420));
4513 }
4514 
4515 static void
4516 intel_dp_set_edid(struct intel_dp *intel_dp)
4517 {
4518 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4519 	struct intel_connector *connector = intel_dp->attached_connector;
4520 	struct edid *edid;
4521 	bool vrr_capable;
4522 
4523 	intel_dp_unset_edid(intel_dp);
4524 	edid = intel_dp_get_edid(intel_dp);
4525 	connector->detect_edid = edid;
4526 
4527 	vrr_capable = intel_vrr_is_capable(&connector->base);
4528 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s] VRR capable: %s\n",
4529 		    connector->base.base.id, connector->base.name, str_yes_no(vrr_capable));
4530 	drm_connector_set_vrr_capable_property(&connector->base, vrr_capable);
4531 
4532 	intel_dp_update_dfp(intel_dp, edid);
4533 	intel_dp_update_420(intel_dp);
4534 
4535 	if (edid && edid->input & DRM_EDID_INPUT_DIGITAL) {
4536 		intel_dp->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
4537 		intel_dp->has_audio = drm_detect_monitor_audio(edid);
4538 	}
4539 
4540 	drm_dp_cec_set_edid(&intel_dp->aux, edid);
4541 }
4542 
4543 static void
4544 intel_dp_unset_edid(struct intel_dp *intel_dp)
4545 {
4546 	struct intel_connector *connector = intel_dp->attached_connector;
4547 
4548 	drm_dp_cec_unset_edid(&intel_dp->aux);
4549 	kfree(connector->detect_edid);
4550 	connector->detect_edid = NULL;
4551 
4552 	intel_dp->has_hdmi_sink = false;
4553 	intel_dp->has_audio = false;
4554 
4555 	intel_dp->dfp.max_bpc = 0;
4556 	intel_dp->dfp.max_dotclock = 0;
4557 	intel_dp->dfp.min_tmds_clock = 0;
4558 	intel_dp->dfp.max_tmds_clock = 0;
4559 
4560 	intel_dp->dfp.pcon_max_frl_bw = 0;
4561 
4562 	intel_dp->dfp.ycbcr_444_to_420 = false;
4563 	connector->base.ycbcr_420_allowed = false;
4564 
4565 	drm_connector_set_vrr_capable_property(&connector->base,
4566 					       false);
4567 }
4568 
4569 static int
4570 intel_dp_detect(struct drm_connector *connector,
4571 		struct drm_modeset_acquire_ctx *ctx,
4572 		bool force)
4573 {
4574 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
4575 	struct intel_dp *intel_dp = intel_attached_dp(to_intel_connector(connector));
4576 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4577 	struct intel_encoder *encoder = &dig_port->base;
4578 	enum drm_connector_status status;
4579 
4580 	drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s]\n",
4581 		    connector->base.id, connector->name);
4582 	drm_WARN_ON(&dev_priv->drm,
4583 		    !drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex));
4584 
4585 	if (!INTEL_DISPLAY_ENABLED(dev_priv))
4586 		return connector_status_disconnected;
4587 
4588 	/* Can't disconnect eDP */
4589 	if (intel_dp_is_edp(intel_dp))
4590 		status = edp_detect(intel_dp);
4591 	else if (intel_digital_port_connected(encoder))
4592 		status = intel_dp_detect_dpcd(intel_dp);
4593 	else
4594 		status = connector_status_disconnected;
4595 
4596 	if (status == connector_status_disconnected) {
4597 		memset(&intel_dp->compliance, 0, sizeof(intel_dp->compliance));
4598 		memset(intel_dp->dsc_dpcd, 0, sizeof(intel_dp->dsc_dpcd));
4599 
4600 		if (intel_dp->is_mst) {
4601 			drm_dbg_kms(&dev_priv->drm,
4602 				    "MST device may have disappeared %d vs %d\n",
4603 				    intel_dp->is_mst,
4604 				    intel_dp->mst_mgr.mst_state);
4605 			intel_dp->is_mst = false;
4606 			drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr,
4607 							intel_dp->is_mst);
4608 		}
4609 
4610 		goto out;
4611 	}
4612 
4613 	/* Read DP Sink DSC Cap DPCD regs for DP v1.4 */
4614 	if (DISPLAY_VER(dev_priv) >= 11)
4615 		intel_dp_get_dsc_sink_cap(intel_dp);
4616 
4617 	intel_dp_configure_mst(intel_dp);
4618 
4619 	/*
4620 	 * TODO: Reset link params when switching to MST mode, until MST
4621 	 * supports link training fallback params.
4622 	 */
4623 	if (intel_dp->reset_link_params || intel_dp->is_mst) {
4624 		intel_dp_reset_max_link_params(intel_dp);
4625 		intel_dp->reset_link_params = false;
4626 	}
4627 
4628 	intel_dp_print_rates(intel_dp);
4629 
4630 	if (intel_dp->is_mst) {
4631 		/*
4632 		 * If we are in MST mode then this connector
4633 		 * won't appear connected or have anything
4634 		 * with EDID on it
4635 		 */
4636 		status = connector_status_disconnected;
4637 		goto out;
4638 	}
4639 
4640 	/*
4641 	 * Some external monitors do not signal loss of link synchronization
4642 	 * with an IRQ_HPD, so force a link status check.
4643 	 */
4644 	if (!intel_dp_is_edp(intel_dp)) {
4645 		int ret;
4646 
4647 		ret = intel_dp_retrain_link(encoder, ctx);
4648 		if (ret)
4649 			return ret;
4650 	}
4651 
4652 	/*
4653 	 * Clearing NACK and defer counts to get their exact values
4654 	 * while reading EDID which are required by Compliance tests
4655 	 * 4.2.2.4 and 4.2.2.5
4656 	 */
4657 	intel_dp->aux.i2c_nack_count = 0;
4658 	intel_dp->aux.i2c_defer_count = 0;
4659 
4660 	intel_dp_set_edid(intel_dp);
4661 	if (intel_dp_is_edp(intel_dp) ||
4662 	    to_intel_connector(connector)->detect_edid)
4663 		status = connector_status_connected;
4664 
4665 	intel_dp_check_device_service_irq(intel_dp);
4666 
4667 out:
4668 	if (status != connector_status_connected && !intel_dp->is_mst)
4669 		intel_dp_unset_edid(intel_dp);
4670 
4671 	/*
4672 	 * Make sure the refs for power wells enabled during detect are
4673 	 * dropped to avoid a new detect cycle triggered by HPD polling.
4674 	 */
4675 	intel_display_power_flush_work(dev_priv);
4676 
4677 	if (!intel_dp_is_edp(intel_dp))
4678 		drm_dp_set_subconnector_property(connector,
4679 						 status,
4680 						 intel_dp->dpcd,
4681 						 intel_dp->downstream_ports);
4682 	return status;
4683 }
4684 
4685 static void
4686 intel_dp_force(struct drm_connector *connector)
4687 {
4688 	struct intel_dp *intel_dp = intel_attached_dp(to_intel_connector(connector));
4689 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4690 	struct intel_encoder *intel_encoder = &dig_port->base;
4691 	struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
4692 	enum intel_display_power_domain aux_domain =
4693 		intel_aux_power_domain(dig_port);
4694 	intel_wakeref_t wakeref;
4695 
4696 	drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s]\n",
4697 		    connector->base.id, connector->name);
4698 	intel_dp_unset_edid(intel_dp);
4699 
4700 	if (connector->status != connector_status_connected)
4701 		return;
4702 
4703 	wakeref = intel_display_power_get(dev_priv, aux_domain);
4704 
4705 	intel_dp_set_edid(intel_dp);
4706 
4707 	intel_display_power_put(dev_priv, aux_domain, wakeref);
4708 }
4709 
4710 static int intel_dp_get_modes(struct drm_connector *connector)
4711 {
4712 	struct intel_connector *intel_connector = to_intel_connector(connector);
4713 	struct edid *edid;
4714 	int num_modes = 0;
4715 
4716 	edid = intel_connector->detect_edid;
4717 	if (edid)
4718 		num_modes = intel_connector_update_modes(connector, edid);
4719 
4720 	/* Also add fixed mode, which may or may not be present in EDID */
4721 	if (intel_dp_is_edp(intel_attached_dp(intel_connector)))
4722 		num_modes += intel_panel_get_modes(intel_connector);
4723 
4724 	if (num_modes)
4725 		return num_modes;
4726 
4727 	if (!edid) {
4728 		struct intel_dp *intel_dp = intel_attached_dp(intel_connector);
4729 		struct drm_display_mode *mode;
4730 
4731 		mode = drm_dp_downstream_mode(connector->dev,
4732 					      intel_dp->dpcd,
4733 					      intel_dp->downstream_ports);
4734 		if (mode) {
4735 			drm_mode_probed_add(connector, mode);
4736 			num_modes++;
4737 		}
4738 	}
4739 
4740 	return num_modes;
4741 }
4742 
4743 static int
4744 intel_dp_connector_register(struct drm_connector *connector)
4745 {
4746 	struct drm_i915_private *i915 = to_i915(connector->dev);
4747 	struct intel_dp *intel_dp = intel_attached_dp(to_intel_connector(connector));
4748 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4749 	struct intel_lspcon *lspcon = &dig_port->lspcon;
4750 	int ret;
4751 
4752 	ret = intel_connector_register(connector);
4753 	if (ret)
4754 		return ret;
4755 
4756 	drm_dbg_kms(&i915->drm, "registering %s bus for %s\n",
4757 		    intel_dp->aux.name, connector->kdev->kobj.name);
4758 
4759 	intel_dp->aux.dev = connector->kdev;
4760 	ret = drm_dp_aux_register(&intel_dp->aux);
4761 	if (!ret)
4762 		drm_dp_cec_register_connector(&intel_dp->aux, connector);
4763 
4764 	if (!intel_bios_is_lspcon_present(i915, dig_port->base.port))
4765 		return ret;
4766 
4767 	/*
4768 	 * ToDo: Clean this up to handle lspcon init and resume more
4769 	 * efficiently and streamlined.
4770 	 */
4771 	if (lspcon_init(dig_port)) {
4772 		lspcon_detect_hdr_capability(lspcon);
4773 		if (lspcon->hdr_supported)
4774 			drm_connector_attach_hdr_output_metadata_property(connector);
4775 	}
4776 
4777 	return ret;
4778 }
4779 
4780 static void
4781 intel_dp_connector_unregister(struct drm_connector *connector)
4782 {
4783 	struct intel_dp *intel_dp = intel_attached_dp(to_intel_connector(connector));
4784 
4785 	drm_dp_cec_unregister_connector(&intel_dp->aux);
4786 	drm_dp_aux_unregister(&intel_dp->aux);
4787 	intel_connector_unregister(connector);
4788 }
4789 
4790 void intel_dp_encoder_flush_work(struct drm_encoder *encoder)
4791 {
4792 	struct intel_digital_port *dig_port = enc_to_dig_port(to_intel_encoder(encoder));
4793 	struct intel_dp *intel_dp = &dig_port->dp;
4794 
4795 	intel_dp_mst_encoder_cleanup(dig_port);
4796 
4797 	intel_pps_vdd_off_sync(intel_dp);
4798 
4799 	intel_dp_aux_fini(intel_dp);
4800 }
4801 
4802 void intel_dp_encoder_suspend(struct intel_encoder *intel_encoder)
4803 {
4804 	struct intel_dp *intel_dp = enc_to_intel_dp(intel_encoder);
4805 
4806 	intel_pps_vdd_off_sync(intel_dp);
4807 }
4808 
4809 void intel_dp_encoder_shutdown(struct intel_encoder *intel_encoder)
4810 {
4811 	struct intel_dp *intel_dp = enc_to_intel_dp(intel_encoder);
4812 
4813 	intel_pps_wait_power_cycle(intel_dp);
4814 }
4815 
4816 static int intel_modeset_tile_group(struct intel_atomic_state *state,
4817 				    int tile_group_id)
4818 {
4819 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
4820 	struct drm_connector_list_iter conn_iter;
4821 	struct drm_connector *connector;
4822 	int ret = 0;
4823 
4824 	drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
4825 	drm_for_each_connector_iter(connector, &conn_iter) {
4826 		struct drm_connector_state *conn_state;
4827 		struct intel_crtc_state *crtc_state;
4828 		struct intel_crtc *crtc;
4829 
4830 		if (!connector->has_tile ||
4831 		    connector->tile_group->id != tile_group_id)
4832 			continue;
4833 
4834 		conn_state = drm_atomic_get_connector_state(&state->base,
4835 							    connector);
4836 		if (IS_ERR(conn_state)) {
4837 			ret = PTR_ERR(conn_state);
4838 			break;
4839 		}
4840 
4841 		crtc = to_intel_crtc(conn_state->crtc);
4842 
4843 		if (!crtc)
4844 			continue;
4845 
4846 		crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
4847 		crtc_state->uapi.mode_changed = true;
4848 
4849 		ret = drm_atomic_add_affected_planes(&state->base, &crtc->base);
4850 		if (ret)
4851 			break;
4852 	}
4853 	drm_connector_list_iter_end(&conn_iter);
4854 
4855 	return ret;
4856 }
4857 
4858 static int intel_modeset_affected_transcoders(struct intel_atomic_state *state, u8 transcoders)
4859 {
4860 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
4861 	struct intel_crtc *crtc;
4862 
4863 	if (transcoders == 0)
4864 		return 0;
4865 
4866 	for_each_intel_crtc(&dev_priv->drm, crtc) {
4867 		struct intel_crtc_state *crtc_state;
4868 		int ret;
4869 
4870 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
4871 		if (IS_ERR(crtc_state))
4872 			return PTR_ERR(crtc_state);
4873 
4874 		if (!crtc_state->hw.enable)
4875 			continue;
4876 
4877 		if (!(transcoders & BIT(crtc_state->cpu_transcoder)))
4878 			continue;
4879 
4880 		crtc_state->uapi.mode_changed = true;
4881 
4882 		ret = drm_atomic_add_affected_connectors(&state->base, &crtc->base);
4883 		if (ret)
4884 			return ret;
4885 
4886 		ret = drm_atomic_add_affected_planes(&state->base, &crtc->base);
4887 		if (ret)
4888 			return ret;
4889 
4890 		transcoders &= ~BIT(crtc_state->cpu_transcoder);
4891 	}
4892 
4893 	drm_WARN_ON(&dev_priv->drm, transcoders != 0);
4894 
4895 	return 0;
4896 }
4897 
4898 static int intel_modeset_synced_crtcs(struct intel_atomic_state *state,
4899 				      struct drm_connector *connector)
4900 {
4901 	const struct drm_connector_state *old_conn_state =
4902 		drm_atomic_get_old_connector_state(&state->base, connector);
4903 	const struct intel_crtc_state *old_crtc_state;
4904 	struct intel_crtc *crtc;
4905 	u8 transcoders;
4906 
4907 	crtc = to_intel_crtc(old_conn_state->crtc);
4908 	if (!crtc)
4909 		return 0;
4910 
4911 	old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc);
4912 
4913 	if (!old_crtc_state->hw.active)
4914 		return 0;
4915 
4916 	transcoders = old_crtc_state->sync_mode_slaves_mask;
4917 	if (old_crtc_state->master_transcoder != INVALID_TRANSCODER)
4918 		transcoders |= BIT(old_crtc_state->master_transcoder);
4919 
4920 	return intel_modeset_affected_transcoders(state,
4921 						  transcoders);
4922 }
4923 
4924 static int intel_dp_connector_atomic_check(struct drm_connector *conn,
4925 					   struct drm_atomic_state *_state)
4926 {
4927 	struct drm_i915_private *dev_priv = to_i915(conn->dev);
4928 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
4929 	int ret;
4930 
4931 	ret = intel_digital_connector_atomic_check(conn, &state->base);
4932 	if (ret)
4933 		return ret;
4934 
4935 	/*
4936 	 * We don't enable port sync on BDW due to missing w/as and
4937 	 * due to not having adjusted the modeset sequence appropriately.
4938 	 */
4939 	if (DISPLAY_VER(dev_priv) < 9)
4940 		return 0;
4941 
4942 	if (!intel_connector_needs_modeset(state, conn))
4943 		return 0;
4944 
4945 	if (conn->has_tile) {
4946 		ret = intel_modeset_tile_group(state, conn->tile_group->id);
4947 		if (ret)
4948 			return ret;
4949 	}
4950 
4951 	return intel_modeset_synced_crtcs(state, conn);
4952 }
4953 
4954 static void intel_dp_oob_hotplug_event(struct drm_connector *connector)
4955 {
4956 	struct intel_encoder *encoder = intel_attached_encoder(to_intel_connector(connector));
4957 	struct drm_i915_private *i915 = to_i915(connector->dev);
4958 
4959 	spin_lock_irq(&i915->irq_lock);
4960 	i915->hotplug.event_bits |= BIT(encoder->hpd_pin);
4961 	spin_unlock_irq(&i915->irq_lock);
4962 	queue_delayed_work(system_wq, &i915->hotplug.hotplug_work, 0);
4963 }
4964 
4965 static const struct drm_connector_funcs intel_dp_connector_funcs = {
4966 	.force = intel_dp_force,
4967 	.fill_modes = drm_helper_probe_single_connector_modes,
4968 	.atomic_get_property = intel_digital_connector_atomic_get_property,
4969 	.atomic_set_property = intel_digital_connector_atomic_set_property,
4970 	.late_register = intel_dp_connector_register,
4971 	.early_unregister = intel_dp_connector_unregister,
4972 	.destroy = intel_connector_destroy,
4973 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
4974 	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
4975 	.oob_hotplug_event = intel_dp_oob_hotplug_event,
4976 };
4977 
4978 static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
4979 	.detect_ctx = intel_dp_detect,
4980 	.get_modes = intel_dp_get_modes,
4981 	.mode_valid = intel_dp_mode_valid,
4982 	.atomic_check = intel_dp_connector_atomic_check,
4983 };
4984 
4985 enum irqreturn
4986 intel_dp_hpd_pulse(struct intel_digital_port *dig_port, bool long_hpd)
4987 {
4988 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
4989 	struct intel_dp *intel_dp = &dig_port->dp;
4990 
4991 	if (dig_port->base.type == INTEL_OUTPUT_EDP &&
4992 	    (long_hpd || !intel_pps_have_panel_power_or_vdd(intel_dp))) {
4993 		/*
4994 		 * vdd off can generate a long/short pulse on eDP which
4995 		 * would require vdd on to handle it, and thus we
4996 		 * would end up in an endless cycle of
4997 		 * "vdd off -> long/short hpd -> vdd on -> detect -> vdd off -> ..."
4998 		 */
4999 		drm_dbg_kms(&i915->drm,
5000 			    "ignoring %s hpd on eDP [ENCODER:%d:%s]\n",
5001 			    long_hpd ? "long" : "short",
5002 			    dig_port->base.base.base.id,
5003 			    dig_port->base.base.name);
5004 		return IRQ_HANDLED;
5005 	}
5006 
5007 	drm_dbg_kms(&i915->drm, "got hpd irq on [ENCODER:%d:%s] - %s\n",
5008 		    dig_port->base.base.base.id,
5009 		    dig_port->base.base.name,
5010 		    long_hpd ? "long" : "short");
5011 
5012 	if (long_hpd) {
5013 		intel_dp->reset_link_params = true;
5014 		return IRQ_NONE;
5015 	}
5016 
5017 	if (intel_dp->is_mst) {
5018 		if (!intel_dp_check_mst_status(intel_dp))
5019 			return IRQ_NONE;
5020 	} else if (!intel_dp_short_pulse(intel_dp)) {
5021 		return IRQ_NONE;
5022 	}
5023 
5024 	return IRQ_HANDLED;
5025 }
5026 
5027 /* check the VBT to see whether the eDP is on another port */
5028 bool intel_dp_is_port_edp(struct drm_i915_private *dev_priv, enum port port)
5029 {
5030 	/*
5031 	 * eDP not supported on g4x. so bail out early just
5032 	 * for a bit extra safety in case the VBT is bonkers.
5033 	 */
5034 	if (DISPLAY_VER(dev_priv) < 5)
5035 		return false;
5036 
5037 	if (DISPLAY_VER(dev_priv) < 9 && port == PORT_A)
5038 		return true;
5039 
5040 	return intel_bios_is_port_edp(dev_priv, port);
5041 }
5042 
5043 static bool
5044 has_gamut_metadata_dip(struct drm_i915_private *i915, enum port port)
5045 {
5046 	if (intel_bios_is_lspcon_present(i915, port))
5047 		return false;
5048 
5049 	if (DISPLAY_VER(i915) >= 11)
5050 		return true;
5051 
5052 	if (port == PORT_A)
5053 		return false;
5054 
5055 	if (IS_HASWELL(i915) || IS_BROADWELL(i915) ||
5056 	    DISPLAY_VER(i915) >= 9)
5057 		return true;
5058 
5059 	return false;
5060 }
5061 
5062 static void
5063 intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
5064 {
5065 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
5066 	enum port port = dp_to_dig_port(intel_dp)->base.port;
5067 
5068 	if (!intel_dp_is_edp(intel_dp))
5069 		drm_connector_attach_dp_subconnector_property(connector);
5070 
5071 	if (!IS_G4X(dev_priv) && port != PORT_A)
5072 		intel_attach_force_audio_property(connector);
5073 
5074 	intel_attach_broadcast_rgb_property(connector);
5075 	if (HAS_GMCH(dev_priv))
5076 		drm_connector_attach_max_bpc_property(connector, 6, 10);
5077 	else if (DISPLAY_VER(dev_priv) >= 5)
5078 		drm_connector_attach_max_bpc_property(connector, 6, 12);
5079 
5080 	/* Register HDMI colorspace for case of lspcon */
5081 	if (intel_bios_is_lspcon_present(dev_priv, port)) {
5082 		drm_connector_attach_content_type_property(connector);
5083 		intel_attach_hdmi_colorspace_property(connector);
5084 	} else {
5085 		intel_attach_dp_colorspace_property(connector);
5086 	}
5087 
5088 	if (has_gamut_metadata_dip(dev_priv, port))
5089 		drm_connector_attach_hdr_output_metadata_property(connector);
5090 
5091 	if (intel_dp_is_edp(intel_dp)) {
5092 		u32 allowed_scalers;
5093 
5094 		allowed_scalers = BIT(DRM_MODE_SCALE_ASPECT) | BIT(DRM_MODE_SCALE_FULLSCREEN);
5095 		if (!HAS_GMCH(dev_priv))
5096 			allowed_scalers |= BIT(DRM_MODE_SCALE_CENTER);
5097 
5098 		drm_connector_attach_scaling_mode_property(connector, allowed_scalers);
5099 
5100 		connector->state->scaling_mode = DRM_MODE_SCALE_ASPECT;
5101 
5102 	}
5103 
5104 	if (HAS_VRR(dev_priv))
5105 		drm_connector_attach_vrr_capable_property(connector);
5106 }
5107 
5108 static void
5109 intel_edp_add_properties(struct intel_dp *intel_dp)
5110 {
5111 	struct intel_connector *connector = intel_dp->attached_connector;
5112 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
5113 	const struct drm_display_mode *fixed_mode =
5114 		intel_panel_preferred_fixed_mode(connector);
5115 
5116 	if (!fixed_mode)
5117 		return;
5118 
5119 	drm_connector_set_panel_orientation_with_quirk(&connector->base,
5120 						       i915->vbt.orientation,
5121 						       fixed_mode->hdisplay,
5122 						       fixed_mode->vdisplay);
5123 }
5124 
5125 static bool intel_edp_init_connector(struct intel_dp *intel_dp,
5126 				     struct intel_connector *intel_connector)
5127 {
5128 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
5129 	struct drm_device *dev = &dev_priv->drm;
5130 	struct drm_connector *connector = &intel_connector->base;
5131 	struct drm_display_mode *fixed_mode;
5132 	bool has_dpcd;
5133 	enum pipe pipe = INVALID_PIPE;
5134 	struct edid *edid;
5135 
5136 	if (!intel_dp_is_edp(intel_dp))
5137 		return true;
5138 
5139 	/*
5140 	 * On IBX/CPT we may get here with LVDS already registered. Since the
5141 	 * driver uses the only internal power sequencer available for both
5142 	 * eDP and LVDS bail out early in this case to prevent interfering
5143 	 * with an already powered-on LVDS power sequencer.
5144 	 */
5145 	if (intel_get_lvds_encoder(dev_priv)) {
5146 		drm_WARN_ON(dev,
5147 			    !(HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)));
5148 		drm_info(&dev_priv->drm,
5149 			 "LVDS was detected, not registering eDP\n");
5150 
5151 		return false;
5152 	}
5153 
5154 	intel_pps_init(intel_dp);
5155 
5156 	/* Cache DPCD and EDID for edp. */
5157 	has_dpcd = intel_edp_init_dpcd(intel_dp);
5158 
5159 	if (!has_dpcd) {
5160 		/* if this fails, presume the device is a ghost */
5161 		drm_info(&dev_priv->drm,
5162 			 "failed to retrieve link info, disabling eDP\n");
5163 		goto out_vdd_off;
5164 	}
5165 
5166 	mutex_lock(&dev->mode_config.mutex);
5167 	edid = drm_get_edid(connector, &intel_dp->aux.ddc);
5168 	if (!edid) {
5169 		/* Fallback to EDID from ACPI OpRegion, if any */
5170 		edid = intel_opregion_get_edid(intel_connector);
5171 		if (edid)
5172 			drm_dbg_kms(&dev_priv->drm,
5173 				    "[CONNECTOR:%d:%s] Using OpRegion EDID\n",
5174 				    connector->base.id, connector->name);
5175 	}
5176 	if (edid) {
5177 		if (drm_add_edid_modes(connector, edid)) {
5178 			drm_connector_update_edid_property(connector, edid);
5179 		} else {
5180 			kfree(edid);
5181 			edid = ERR_PTR(-EINVAL);
5182 		}
5183 	} else {
5184 		edid = ERR_PTR(-ENOENT);
5185 	}
5186 	intel_connector->edid = edid;
5187 
5188 	intel_panel_add_edid_fixed_modes(intel_connector,
5189 					 dev_priv->vbt.drrs_type != DRRS_TYPE_NONE);
5190 
5191 	/* MSO requires information from the EDID */
5192 	intel_edp_mso_init(intel_dp);
5193 
5194 	/* multiply the mode clock and horizontal timings for MSO */
5195 	list_for_each_entry(fixed_mode, &intel_connector->panel.fixed_modes, head)
5196 		intel_edp_mso_mode_fixup(intel_connector, fixed_mode);
5197 
5198 	/* fallback to VBT if available for eDP */
5199 	if (!intel_panel_preferred_fixed_mode(intel_connector))
5200 		intel_panel_add_vbt_lfp_fixed_mode(intel_connector);
5201 
5202 	mutex_unlock(&dev->mode_config.mutex);
5203 
5204 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
5205 		/*
5206 		 * Figure out the current pipe for the initial backlight setup.
5207 		 * If the current pipe isn't valid, try the PPS pipe, and if that
5208 		 * fails just assume pipe A.
5209 		 */
5210 		pipe = vlv_active_pipe(intel_dp);
5211 
5212 		if (pipe != PIPE_A && pipe != PIPE_B)
5213 			pipe = intel_dp->pps.pps_pipe;
5214 
5215 		if (pipe != PIPE_A && pipe != PIPE_B)
5216 			pipe = PIPE_A;
5217 
5218 		drm_dbg_kms(&dev_priv->drm,
5219 			    "using pipe %c for initial backlight setup\n",
5220 			    pipe_name(pipe));
5221 	}
5222 
5223 	intel_panel_init(intel_connector);
5224 
5225 	if (!(dev_priv->quirks & QUIRK_NO_PPS_BACKLIGHT_POWER_HOOK))
5226 		intel_connector->panel.backlight.power = intel_pps_backlight_power;
5227 	intel_backlight_setup(intel_connector, pipe);
5228 
5229 	intel_edp_add_properties(intel_dp);
5230 
5231 	return true;
5232 
5233 out_vdd_off:
5234 	intel_pps_vdd_off_sync(intel_dp);
5235 
5236 	return false;
5237 }
5238 
5239 static void intel_dp_modeset_retry_work_fn(struct work_struct *work)
5240 {
5241 	struct intel_connector *intel_connector;
5242 	struct drm_connector *connector;
5243 
5244 	intel_connector = container_of(work, typeof(*intel_connector),
5245 				       modeset_retry_work);
5246 	connector = &intel_connector->base;
5247 	drm_dbg_kms(connector->dev, "[CONNECTOR:%d:%s]\n", connector->base.id,
5248 		    connector->name);
5249 
5250 	/* Grab the locks before changing connector property*/
5251 	mutex_lock(&connector->dev->mode_config.mutex);
5252 	/* Set connector link status to BAD and send a Uevent to notify
5253 	 * userspace to do a modeset.
5254 	 */
5255 	drm_connector_set_link_status_property(connector,
5256 					       DRM_MODE_LINK_STATUS_BAD);
5257 	mutex_unlock(&connector->dev->mode_config.mutex);
5258 	/* Send Hotplug uevent so userspace can reprobe */
5259 	drm_kms_helper_connector_hotplug_event(connector);
5260 }
5261 
5262 bool
5263 intel_dp_init_connector(struct intel_digital_port *dig_port,
5264 			struct intel_connector *intel_connector)
5265 {
5266 	struct drm_connector *connector = &intel_connector->base;
5267 	struct intel_dp *intel_dp = &dig_port->dp;
5268 	struct intel_encoder *intel_encoder = &dig_port->base;
5269 	struct drm_device *dev = intel_encoder->base.dev;
5270 	struct drm_i915_private *dev_priv = to_i915(dev);
5271 	enum port port = intel_encoder->port;
5272 	enum phy phy = intel_port_to_phy(dev_priv, port);
5273 	int type;
5274 
5275 	/* Initialize the work for modeset in case of link train failure */
5276 	INIT_WORK(&intel_connector->modeset_retry_work,
5277 		  intel_dp_modeset_retry_work_fn);
5278 
5279 	if (drm_WARN(dev, dig_port->max_lanes < 1,
5280 		     "Not enough lanes (%d) for DP on [ENCODER:%d:%s]\n",
5281 		     dig_port->max_lanes, intel_encoder->base.base.id,
5282 		     intel_encoder->base.name))
5283 		return false;
5284 
5285 	intel_dp->reset_link_params = true;
5286 	intel_dp->pps.pps_pipe = INVALID_PIPE;
5287 	intel_dp->pps.active_pipe = INVALID_PIPE;
5288 
5289 	/* Preserve the current hw state. */
5290 	intel_dp->DP = intel_de_read(dev_priv, intel_dp->output_reg);
5291 	intel_dp->attached_connector = intel_connector;
5292 
5293 	if (intel_dp_is_port_edp(dev_priv, port)) {
5294 		/*
5295 		 * Currently we don't support eDP on TypeC ports, although in
5296 		 * theory it could work on TypeC legacy ports.
5297 		 */
5298 		drm_WARN_ON(dev, intel_phy_is_tc(dev_priv, phy));
5299 		type = DRM_MODE_CONNECTOR_eDP;
5300 		intel_encoder->type = INTEL_OUTPUT_EDP;
5301 
5302 		/* eDP only on port B and/or C on vlv/chv */
5303 		if (drm_WARN_ON(dev, (IS_VALLEYVIEW(dev_priv) ||
5304 				      IS_CHERRYVIEW(dev_priv)) &&
5305 				port != PORT_B && port != PORT_C))
5306 			return false;
5307 	} else {
5308 		type = DRM_MODE_CONNECTOR_DisplayPort;
5309 	}
5310 
5311 	intel_dp_set_source_rates(intel_dp);
5312 	intel_dp_set_default_sink_rates(intel_dp);
5313 	intel_dp_set_default_max_sink_lane_count(intel_dp);
5314 	intel_dp_set_common_rates(intel_dp);
5315 	intel_dp_reset_max_link_params(intel_dp);
5316 
5317 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
5318 		intel_dp->pps.active_pipe = vlv_active_pipe(intel_dp);
5319 
5320 	drm_dbg_kms(&dev_priv->drm,
5321 		    "Adding %s connector on [ENCODER:%d:%s]\n",
5322 		    type == DRM_MODE_CONNECTOR_eDP ? "eDP" : "DP",
5323 		    intel_encoder->base.base.id, intel_encoder->base.name);
5324 
5325 	drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
5326 	drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
5327 
5328 	if (!HAS_GMCH(dev_priv))
5329 		connector->interlace_allowed = true;
5330 	connector->doublescan_allowed = 0;
5331 
5332 	intel_connector->polled = DRM_CONNECTOR_POLL_HPD;
5333 
5334 	intel_dp_aux_init(intel_dp);
5335 
5336 	intel_connector_attach_encoder(intel_connector, intel_encoder);
5337 
5338 	if (HAS_DDI(dev_priv))
5339 		intel_connector->get_hw_state = intel_ddi_connector_get_hw_state;
5340 	else
5341 		intel_connector->get_hw_state = intel_connector_get_hw_state;
5342 
5343 	/* init MST on ports that can support it */
5344 	intel_dp_mst_encoder_init(dig_port,
5345 				  intel_connector->base.base.id);
5346 
5347 	if (!intel_edp_init_connector(intel_dp, intel_connector)) {
5348 		intel_dp_aux_fini(intel_dp);
5349 		intel_dp_mst_encoder_cleanup(dig_port);
5350 		goto fail;
5351 	}
5352 
5353 	intel_dp_add_properties(intel_dp, connector);
5354 
5355 	if (is_hdcp_supported(dev_priv, port) && !intel_dp_is_edp(intel_dp)) {
5356 		int ret = intel_dp_hdcp_init(dig_port, intel_connector);
5357 		if (ret)
5358 			drm_dbg_kms(&dev_priv->drm,
5359 				    "HDCP init failed, skipping.\n");
5360 	}
5361 
5362 	/* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
5363 	 * 0xd.  Failure to do so will result in spurious interrupts being
5364 	 * generated on the port when a cable is not attached.
5365 	 */
5366 	if (IS_G45(dev_priv)) {
5367 		u32 temp = intel_de_read(dev_priv, PEG_BAND_GAP_DATA);
5368 		intel_de_write(dev_priv, PEG_BAND_GAP_DATA,
5369 			       (temp & ~0xf) | 0xd);
5370 	}
5371 
5372 	intel_dp->frl.is_trained = false;
5373 	intel_dp->frl.trained_rate_gbps = 0;
5374 
5375 	intel_psr_init(intel_dp);
5376 
5377 	return true;
5378 
5379 fail:
5380 	drm_connector_cleanup(connector);
5381 
5382 	return false;
5383 }
5384 
5385 void intel_dp_mst_suspend(struct drm_i915_private *dev_priv)
5386 {
5387 	struct intel_encoder *encoder;
5388 
5389 	if (!HAS_DISPLAY(dev_priv))
5390 		return;
5391 
5392 	for_each_intel_encoder(&dev_priv->drm, encoder) {
5393 		struct intel_dp *intel_dp;
5394 
5395 		if (encoder->type != INTEL_OUTPUT_DDI)
5396 			continue;
5397 
5398 		intel_dp = enc_to_intel_dp(encoder);
5399 
5400 		if (!intel_dp_mst_source_support(intel_dp))
5401 			continue;
5402 
5403 		if (intel_dp->is_mst)
5404 			drm_dp_mst_topology_mgr_suspend(&intel_dp->mst_mgr);
5405 	}
5406 }
5407 
5408 void intel_dp_mst_resume(struct drm_i915_private *dev_priv)
5409 {
5410 	struct intel_encoder *encoder;
5411 
5412 	if (!HAS_DISPLAY(dev_priv))
5413 		return;
5414 
5415 	for_each_intel_encoder(&dev_priv->drm, encoder) {
5416 		struct intel_dp *intel_dp;
5417 		int ret;
5418 
5419 		if (encoder->type != INTEL_OUTPUT_DDI)
5420 			continue;
5421 
5422 		intel_dp = enc_to_intel_dp(encoder);
5423 
5424 		if (!intel_dp_mst_source_support(intel_dp))
5425 			continue;
5426 
5427 		ret = drm_dp_mst_topology_mgr_resume(&intel_dp->mst_mgr,
5428 						     true);
5429 		if (ret) {
5430 			intel_dp->is_mst = false;
5431 			drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr,
5432 							false);
5433 		}
5434 	}
5435 }
5436