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