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