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 =
2084 		intel_dp_has_audio(encoder, pipe_config, conn_state) &&
2085 		intel_audio_compute_config(encoder, pipe_config, conn_state);
2086 
2087 	fixed_mode = intel_panel_fixed_mode(connector, adjusted_mode);
2088 	if (intel_dp_is_edp(intel_dp) && fixed_mode) {
2089 		ret = intel_panel_compute_config(connector, adjusted_mode);
2090 		if (ret)
2091 			return ret;
2092 	}
2093 
2094 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
2095 		return -EINVAL;
2096 
2097 	if (!connector->base.interlace_allowed &&
2098 	    adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
2099 		return -EINVAL;
2100 
2101 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
2102 		return -EINVAL;
2103 
2104 	if (intel_dp_hdisplay_bad(dev_priv, adjusted_mode->crtc_hdisplay))
2105 		return -EINVAL;
2106 
2107 	/*
2108 	 * Try to respect downstream TMDS clock limits first, if
2109 	 * that fails assume the user might know something we don't.
2110 	 */
2111 	ret = intel_dp_compute_output_format(encoder, pipe_config, conn_state, true);
2112 	if (ret)
2113 		ret = intel_dp_compute_output_format(encoder, pipe_config, conn_state, false);
2114 	if (ret)
2115 		return ret;
2116 
2117 	if ((intel_dp_is_edp(intel_dp) && fixed_mode) ||
2118 	    pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420) {
2119 		ret = intel_panel_fitting(pipe_config, conn_state);
2120 		if (ret)
2121 			return ret;
2122 	}
2123 
2124 	pipe_config->limited_color_range =
2125 		intel_dp_limited_color_range(pipe_config, conn_state);
2126 
2127 	if (pipe_config->dsc.compression_enable)
2128 		output_bpp = pipe_config->dsc.compressed_bpp;
2129 	else
2130 		output_bpp = intel_dp_output_bpp(pipe_config->output_format,
2131 						 pipe_config->pipe_bpp);
2132 
2133 	if (intel_dp->mso_link_count) {
2134 		int n = intel_dp->mso_link_count;
2135 		int overlap = intel_dp->mso_pixel_overlap;
2136 
2137 		pipe_config->splitter.enable = true;
2138 		pipe_config->splitter.link_count = n;
2139 		pipe_config->splitter.pixel_overlap = overlap;
2140 
2141 		drm_dbg_kms(&dev_priv->drm, "MSO link count %d, pixel overlap %d\n",
2142 			    n, overlap);
2143 
2144 		adjusted_mode->crtc_hdisplay = adjusted_mode->crtc_hdisplay / n + overlap;
2145 		adjusted_mode->crtc_hblank_start = adjusted_mode->crtc_hblank_start / n + overlap;
2146 		adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_hblank_end / n + overlap;
2147 		adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hsync_start / n + overlap;
2148 		adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_end / n + overlap;
2149 		adjusted_mode->crtc_htotal = adjusted_mode->crtc_htotal / n + overlap;
2150 		adjusted_mode->crtc_clock /= n;
2151 	}
2152 
2153 	intel_dp_audio_compute_config(encoder, pipe_config, conn_state);
2154 
2155 	intel_link_compute_m_n(output_bpp,
2156 			       pipe_config->lane_count,
2157 			       adjusted_mode->crtc_clock,
2158 			       pipe_config->port_clock,
2159 			       &pipe_config->dp_m_n,
2160 			       pipe_config->fec_enable);
2161 
2162 	/* FIXME: abstract this better */
2163 	if (pipe_config->splitter.enable)
2164 		pipe_config->dp_m_n.data_m *= pipe_config->splitter.link_count;
2165 
2166 	if (!HAS_DDI(dev_priv))
2167 		g4x_dp_set_clock(encoder, pipe_config);
2168 
2169 	intel_vrr_compute_config(pipe_config, conn_state);
2170 	intel_psr_compute_config(intel_dp, pipe_config, conn_state);
2171 	intel_dp_drrs_compute_config(connector, pipe_config, output_bpp);
2172 	intel_dp_compute_vsc_sdp(intel_dp, pipe_config, conn_state);
2173 	intel_dp_compute_hdr_metadata_infoframe_sdp(intel_dp, pipe_config, conn_state);
2174 
2175 	return 0;
2176 }
2177 
2178 void intel_dp_set_link_params(struct intel_dp *intel_dp,
2179 			      int link_rate, int lane_count)
2180 {
2181 	memset(intel_dp->train_set, 0, sizeof(intel_dp->train_set));
2182 	intel_dp->link_trained = false;
2183 	intel_dp->link_rate = link_rate;
2184 	intel_dp->lane_count = lane_count;
2185 }
2186 
2187 static void intel_dp_reset_max_link_params(struct intel_dp *intel_dp)
2188 {
2189 	intel_dp->max_link_lane_count = intel_dp_max_common_lane_count(intel_dp);
2190 	intel_dp->max_link_rate = intel_dp_max_common_rate(intel_dp);
2191 }
2192 
2193 /* Enable backlight PWM and backlight PP control. */
2194 void intel_edp_backlight_on(const struct intel_crtc_state *crtc_state,
2195 			    const struct drm_connector_state *conn_state)
2196 {
2197 	struct intel_dp *intel_dp = enc_to_intel_dp(to_intel_encoder(conn_state->best_encoder));
2198 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2199 
2200 	if (!intel_dp_is_edp(intel_dp))
2201 		return;
2202 
2203 	drm_dbg_kms(&i915->drm, "\n");
2204 
2205 	intel_backlight_enable(crtc_state, conn_state);
2206 	intel_pps_backlight_on(intel_dp);
2207 }
2208 
2209 /* Disable backlight PP control and backlight PWM. */
2210 void intel_edp_backlight_off(const struct drm_connector_state *old_conn_state)
2211 {
2212 	struct intel_dp *intel_dp = enc_to_intel_dp(to_intel_encoder(old_conn_state->best_encoder));
2213 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2214 
2215 	if (!intel_dp_is_edp(intel_dp))
2216 		return;
2217 
2218 	drm_dbg_kms(&i915->drm, "\n");
2219 
2220 	intel_pps_backlight_off(intel_dp);
2221 	intel_backlight_disable(old_conn_state);
2222 }
2223 
2224 static bool downstream_hpd_needs_d0(struct intel_dp *intel_dp)
2225 {
2226 	/*
2227 	 * DPCD 1.2+ should support BRANCH_DEVICE_CTRL, and thus
2228 	 * be capable of signalling downstream hpd with a long pulse.
2229 	 * Whether or not that means D3 is safe to use is not clear,
2230 	 * but let's assume so until proven otherwise.
2231 	 *
2232 	 * FIXME should really check all downstream ports...
2233 	 */
2234 	return intel_dp->dpcd[DP_DPCD_REV] == 0x11 &&
2235 		drm_dp_is_branch(intel_dp->dpcd) &&
2236 		intel_dp->downstream_ports[0] & DP_DS_PORT_HPD;
2237 }
2238 
2239 void intel_dp_sink_set_decompression_state(struct intel_dp *intel_dp,
2240 					   const struct intel_crtc_state *crtc_state,
2241 					   bool enable)
2242 {
2243 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2244 	int ret;
2245 
2246 	if (!crtc_state->dsc.compression_enable)
2247 		return;
2248 
2249 	ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_DSC_ENABLE,
2250 				 enable ? DP_DECOMPRESSION_EN : 0);
2251 	if (ret < 0)
2252 		drm_dbg_kms(&i915->drm,
2253 			    "Failed to %s sink decompression state\n",
2254 			    str_enable_disable(enable));
2255 }
2256 
2257 static void
2258 intel_edp_init_source_oui(struct intel_dp *intel_dp, bool careful)
2259 {
2260 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2261 	u8 oui[] = { 0x00, 0xaa, 0x01 };
2262 	u8 buf[3] = { 0 };
2263 
2264 	/*
2265 	 * During driver init, we want to be careful and avoid changing the source OUI if it's
2266 	 * already set to what we want, so as to avoid clearing any state by accident
2267 	 */
2268 	if (careful) {
2269 		if (drm_dp_dpcd_read(&intel_dp->aux, DP_SOURCE_OUI, buf, sizeof(buf)) < 0)
2270 			drm_err(&i915->drm, "Failed to read source OUI\n");
2271 
2272 		if (memcmp(oui, buf, sizeof(oui)) == 0)
2273 			return;
2274 	}
2275 
2276 	if (drm_dp_dpcd_write(&intel_dp->aux, DP_SOURCE_OUI, oui, sizeof(oui)) < 0)
2277 		drm_err(&i915->drm, "Failed to write source OUI\n");
2278 
2279 	intel_dp->last_oui_write = jiffies;
2280 }
2281 
2282 void intel_dp_wait_source_oui(struct intel_dp *intel_dp)
2283 {
2284 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2285 
2286 	drm_dbg_kms(&i915->drm, "Performing OUI wait\n");
2287 	wait_remaining_ms_from_jiffies(intel_dp->last_oui_write, 30);
2288 }
2289 
2290 /* If the device supports it, try to set the power state appropriately */
2291 void intel_dp_set_power(struct intel_dp *intel_dp, u8 mode)
2292 {
2293 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
2294 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
2295 	int ret, i;
2296 
2297 	/* Should have a valid DPCD by this point */
2298 	if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
2299 		return;
2300 
2301 	if (mode != DP_SET_POWER_D0) {
2302 		if (downstream_hpd_needs_d0(intel_dp))
2303 			return;
2304 
2305 		ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER, mode);
2306 	} else {
2307 		struct intel_lspcon *lspcon = dp_to_lspcon(intel_dp);
2308 
2309 		lspcon_resume(dp_to_dig_port(intel_dp));
2310 
2311 		/* Write the source OUI as early as possible */
2312 		if (intel_dp_is_edp(intel_dp))
2313 			intel_edp_init_source_oui(intel_dp, false);
2314 
2315 		/*
2316 		 * When turning on, we need to retry for 1ms to give the sink
2317 		 * time to wake up.
2318 		 */
2319 		for (i = 0; i < 3; i++) {
2320 			ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER, mode);
2321 			if (ret == 1)
2322 				break;
2323 			msleep(1);
2324 		}
2325 
2326 		if (ret == 1 && lspcon->active)
2327 			lspcon_wait_pcon_mode(lspcon);
2328 	}
2329 
2330 	if (ret != 1)
2331 		drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] Set power to %s failed\n",
2332 			    encoder->base.base.id, encoder->base.name,
2333 			    mode == DP_SET_POWER_D0 ? "D0" : "D3");
2334 }
2335 
2336 static bool
2337 intel_dp_get_dpcd(struct intel_dp *intel_dp);
2338 
2339 /**
2340  * intel_dp_sync_state - sync the encoder state during init/resume
2341  * @encoder: intel encoder to sync
2342  * @crtc_state: state for the CRTC connected to the encoder
2343  *
2344  * Sync any state stored in the encoder wrt. HW state during driver init
2345  * and system resume.
2346  */
2347 void intel_dp_sync_state(struct intel_encoder *encoder,
2348 			 const struct intel_crtc_state *crtc_state)
2349 {
2350 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2351 
2352 	if (!crtc_state)
2353 		return;
2354 
2355 	/*
2356 	 * Don't clobber DPCD if it's been already read out during output
2357 	 * setup (eDP) or detect.
2358 	 */
2359 	if (intel_dp->dpcd[DP_DPCD_REV] == 0)
2360 		intel_dp_get_dpcd(intel_dp);
2361 
2362 	intel_dp_reset_max_link_params(intel_dp);
2363 }
2364 
2365 bool intel_dp_initial_fastset_check(struct intel_encoder *encoder,
2366 				    struct intel_crtc_state *crtc_state)
2367 {
2368 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
2369 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2370 	bool fastset = true;
2371 
2372 	/*
2373 	 * If BIOS has set an unsupported or non-standard link rate for some
2374 	 * reason force an encoder recompute and full modeset.
2375 	 */
2376 	if (intel_dp_rate_index(intel_dp->source_rates, intel_dp->num_source_rates,
2377 				crtc_state->port_clock) < 0) {
2378 		drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] Forcing full modeset due to unsupported link rate\n",
2379 			    encoder->base.base.id, encoder->base.name);
2380 		crtc_state->uapi.connectors_changed = true;
2381 		fastset = false;
2382 	}
2383 
2384 	/*
2385 	 * FIXME hack to force full modeset when DSC is being used.
2386 	 *
2387 	 * As long as we do not have full state readout and config comparison
2388 	 * of crtc_state->dsc, we have no way to ensure reliable fastset.
2389 	 * Remove once we have readout for DSC.
2390 	 */
2391 	if (crtc_state->dsc.compression_enable) {
2392 		drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] Forcing full modeset due to DSC being enabled\n",
2393 			    encoder->base.base.id, encoder->base.name);
2394 		crtc_state->uapi.mode_changed = true;
2395 		fastset = false;
2396 	}
2397 
2398 	if (CAN_PSR(intel_dp)) {
2399 		drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] Forcing full modeset to compute PSR state\n",
2400 			    encoder->base.base.id, encoder->base.name);
2401 		crtc_state->uapi.mode_changed = true;
2402 		fastset = false;
2403 	}
2404 
2405 	return fastset;
2406 }
2407 
2408 static void intel_dp_get_pcon_dsc_cap(struct intel_dp *intel_dp)
2409 {
2410 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2411 
2412 	/* Clear the cached register set to avoid using stale values */
2413 
2414 	memset(intel_dp->pcon_dsc_dpcd, 0, sizeof(intel_dp->pcon_dsc_dpcd));
2415 
2416 	if (drm_dp_dpcd_read(&intel_dp->aux, DP_PCON_DSC_ENCODER,
2417 			     intel_dp->pcon_dsc_dpcd,
2418 			     sizeof(intel_dp->pcon_dsc_dpcd)) < 0)
2419 		drm_err(&i915->drm, "Failed to read DPCD register 0x%x\n",
2420 			DP_PCON_DSC_ENCODER);
2421 
2422 	drm_dbg_kms(&i915->drm, "PCON ENCODER DSC DPCD: %*ph\n",
2423 		    (int)sizeof(intel_dp->pcon_dsc_dpcd), intel_dp->pcon_dsc_dpcd);
2424 }
2425 
2426 static int intel_dp_pcon_get_frl_mask(u8 frl_bw_mask)
2427 {
2428 	int bw_gbps[] = {9, 18, 24, 32, 40, 48};
2429 	int i;
2430 
2431 	for (i = ARRAY_SIZE(bw_gbps) - 1; i >= 0; i--) {
2432 		if (frl_bw_mask & (1 << i))
2433 			return bw_gbps[i];
2434 	}
2435 	return 0;
2436 }
2437 
2438 static int intel_dp_pcon_set_frl_mask(int max_frl)
2439 {
2440 	switch (max_frl) {
2441 	case 48:
2442 		return DP_PCON_FRL_BW_MASK_48GBPS;
2443 	case 40:
2444 		return DP_PCON_FRL_BW_MASK_40GBPS;
2445 	case 32:
2446 		return DP_PCON_FRL_BW_MASK_32GBPS;
2447 	case 24:
2448 		return DP_PCON_FRL_BW_MASK_24GBPS;
2449 	case 18:
2450 		return DP_PCON_FRL_BW_MASK_18GBPS;
2451 	case 9:
2452 		return DP_PCON_FRL_BW_MASK_9GBPS;
2453 	}
2454 
2455 	return 0;
2456 }
2457 
2458 static int intel_dp_hdmi_sink_max_frl(struct intel_dp *intel_dp)
2459 {
2460 	struct intel_connector *intel_connector = intel_dp->attached_connector;
2461 	struct drm_connector *connector = &intel_connector->base;
2462 	int max_frl_rate;
2463 	int max_lanes, rate_per_lane;
2464 	int max_dsc_lanes, dsc_rate_per_lane;
2465 
2466 	max_lanes = connector->display_info.hdmi.max_lanes;
2467 	rate_per_lane = connector->display_info.hdmi.max_frl_rate_per_lane;
2468 	max_frl_rate = max_lanes * rate_per_lane;
2469 
2470 	if (connector->display_info.hdmi.dsc_cap.v_1p2) {
2471 		max_dsc_lanes = connector->display_info.hdmi.dsc_cap.max_lanes;
2472 		dsc_rate_per_lane = connector->display_info.hdmi.dsc_cap.max_frl_rate_per_lane;
2473 		if (max_dsc_lanes && dsc_rate_per_lane)
2474 			max_frl_rate = min(max_frl_rate, max_dsc_lanes * dsc_rate_per_lane);
2475 	}
2476 
2477 	return max_frl_rate;
2478 }
2479 
2480 static bool
2481 intel_dp_pcon_is_frl_trained(struct intel_dp *intel_dp,
2482 			     u8 max_frl_bw_mask, u8 *frl_trained_mask)
2483 {
2484 	if (drm_dp_pcon_hdmi_link_active(&intel_dp->aux) &&
2485 	    drm_dp_pcon_hdmi_link_mode(&intel_dp->aux, frl_trained_mask) == DP_PCON_HDMI_MODE_FRL &&
2486 	    *frl_trained_mask >= max_frl_bw_mask)
2487 		return true;
2488 
2489 	return false;
2490 }
2491 
2492 static int intel_dp_pcon_start_frl_training(struct intel_dp *intel_dp)
2493 {
2494 #define TIMEOUT_FRL_READY_MS 500
2495 #define TIMEOUT_HDMI_LINK_ACTIVE_MS 1000
2496 
2497 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2498 	int max_frl_bw, max_pcon_frl_bw, max_edid_frl_bw, ret;
2499 	u8 max_frl_bw_mask = 0, frl_trained_mask;
2500 	bool is_active;
2501 
2502 	max_pcon_frl_bw = intel_dp->dfp.pcon_max_frl_bw;
2503 	drm_dbg(&i915->drm, "PCON max rate = %d Gbps\n", max_pcon_frl_bw);
2504 
2505 	max_edid_frl_bw = intel_dp_hdmi_sink_max_frl(intel_dp);
2506 	drm_dbg(&i915->drm, "Sink max rate from EDID = %d Gbps\n", max_edid_frl_bw);
2507 
2508 	max_frl_bw = min(max_edid_frl_bw, max_pcon_frl_bw);
2509 
2510 	if (max_frl_bw <= 0)
2511 		return -EINVAL;
2512 
2513 	max_frl_bw_mask = intel_dp_pcon_set_frl_mask(max_frl_bw);
2514 	drm_dbg(&i915->drm, "MAX_FRL_BW_MASK = %u\n", max_frl_bw_mask);
2515 
2516 	if (intel_dp_pcon_is_frl_trained(intel_dp, max_frl_bw_mask, &frl_trained_mask))
2517 		goto frl_trained;
2518 
2519 	ret = drm_dp_pcon_frl_prepare(&intel_dp->aux, false);
2520 	if (ret < 0)
2521 		return ret;
2522 	/* Wait for PCON to be FRL Ready */
2523 	wait_for(is_active = drm_dp_pcon_is_frl_ready(&intel_dp->aux) == true, TIMEOUT_FRL_READY_MS);
2524 
2525 	if (!is_active)
2526 		return -ETIMEDOUT;
2527 
2528 	ret = drm_dp_pcon_frl_configure_1(&intel_dp->aux, max_frl_bw,
2529 					  DP_PCON_ENABLE_SEQUENTIAL_LINK);
2530 	if (ret < 0)
2531 		return ret;
2532 	ret = drm_dp_pcon_frl_configure_2(&intel_dp->aux, max_frl_bw_mask,
2533 					  DP_PCON_FRL_LINK_TRAIN_NORMAL);
2534 	if (ret < 0)
2535 		return ret;
2536 	ret = drm_dp_pcon_frl_enable(&intel_dp->aux);
2537 	if (ret < 0)
2538 		return ret;
2539 	/*
2540 	 * Wait for FRL to be completed
2541 	 * Check if the HDMI Link is up and active.
2542 	 */
2543 	wait_for(is_active =
2544 		 intel_dp_pcon_is_frl_trained(intel_dp, max_frl_bw_mask, &frl_trained_mask),
2545 		 TIMEOUT_HDMI_LINK_ACTIVE_MS);
2546 
2547 	if (!is_active)
2548 		return -ETIMEDOUT;
2549 
2550 frl_trained:
2551 	drm_dbg(&i915->drm, "FRL_TRAINED_MASK = %u\n", frl_trained_mask);
2552 	intel_dp->frl.trained_rate_gbps = intel_dp_pcon_get_frl_mask(frl_trained_mask);
2553 	intel_dp->frl.is_trained = true;
2554 	drm_dbg(&i915->drm, "FRL trained with : %d Gbps\n", intel_dp->frl.trained_rate_gbps);
2555 
2556 	return 0;
2557 }
2558 
2559 static bool intel_dp_is_hdmi_2_1_sink(struct intel_dp *intel_dp)
2560 {
2561 	if (drm_dp_is_branch(intel_dp->dpcd) &&
2562 	    intel_dp->has_hdmi_sink &&
2563 	    intel_dp_hdmi_sink_max_frl(intel_dp) > 0)
2564 		return true;
2565 
2566 	return false;
2567 }
2568 
2569 static
2570 int intel_dp_pcon_set_tmds_mode(struct intel_dp *intel_dp)
2571 {
2572 	int ret;
2573 	u8 buf = 0;
2574 
2575 	/* Set PCON source control mode */
2576 	buf |= DP_PCON_ENABLE_SOURCE_CTL_MODE;
2577 
2578 	ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2579 	if (ret < 0)
2580 		return ret;
2581 
2582 	/* Set HDMI LINK ENABLE */
2583 	buf |= DP_PCON_ENABLE_HDMI_LINK;
2584 	ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2585 	if (ret < 0)
2586 		return ret;
2587 
2588 	return 0;
2589 }
2590 
2591 void intel_dp_check_frl_training(struct intel_dp *intel_dp)
2592 {
2593 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
2594 
2595 	/*
2596 	 * Always go for FRL training if:
2597 	 * -PCON supports SRC_CTL_MODE (VESA DP2.0-HDMI2.1 PCON Spec Draft-1 Sec-7)
2598 	 * -sink is HDMI2.1
2599 	 */
2600 	if (!(intel_dp->downstream_ports[2] & DP_PCON_SOURCE_CTL_MODE) ||
2601 	    !intel_dp_is_hdmi_2_1_sink(intel_dp) ||
2602 	    intel_dp->frl.is_trained)
2603 		return;
2604 
2605 	if (intel_dp_pcon_start_frl_training(intel_dp) < 0) {
2606 		int ret, mode;
2607 
2608 		drm_dbg(&dev_priv->drm, "Couldn't set FRL mode, continuing with TMDS mode\n");
2609 		ret = intel_dp_pcon_set_tmds_mode(intel_dp);
2610 		mode = drm_dp_pcon_hdmi_link_mode(&intel_dp->aux, NULL);
2611 
2612 		if (ret < 0 || mode != DP_PCON_HDMI_MODE_TMDS)
2613 			drm_dbg(&dev_priv->drm, "Issue with PCON, cannot set TMDS mode\n");
2614 	} else {
2615 		drm_dbg(&dev_priv->drm, "FRL training Completed\n");
2616 	}
2617 }
2618 
2619 static int
2620 intel_dp_pcon_dsc_enc_slice_height(const struct intel_crtc_state *crtc_state)
2621 {
2622 	int vactive = crtc_state->hw.adjusted_mode.vdisplay;
2623 
2624 	return intel_hdmi_dsc_get_slice_height(vactive);
2625 }
2626 
2627 static int
2628 intel_dp_pcon_dsc_enc_slices(struct intel_dp *intel_dp,
2629 			     const struct intel_crtc_state *crtc_state)
2630 {
2631 	struct intel_connector *intel_connector = intel_dp->attached_connector;
2632 	struct drm_connector *connector = &intel_connector->base;
2633 	int hdmi_throughput = connector->display_info.hdmi.dsc_cap.clk_per_slice;
2634 	int hdmi_max_slices = connector->display_info.hdmi.dsc_cap.max_slices;
2635 	int pcon_max_slices = drm_dp_pcon_dsc_max_slices(intel_dp->pcon_dsc_dpcd);
2636 	int pcon_max_slice_width = drm_dp_pcon_dsc_max_slice_width(intel_dp->pcon_dsc_dpcd);
2637 
2638 	return intel_hdmi_dsc_get_num_slices(crtc_state, pcon_max_slices,
2639 					     pcon_max_slice_width,
2640 					     hdmi_max_slices, hdmi_throughput);
2641 }
2642 
2643 static int
2644 intel_dp_pcon_dsc_enc_bpp(struct intel_dp *intel_dp,
2645 			  const struct intel_crtc_state *crtc_state,
2646 			  int num_slices, int slice_width)
2647 {
2648 	struct intel_connector *intel_connector = intel_dp->attached_connector;
2649 	struct drm_connector *connector = &intel_connector->base;
2650 	int output_format = crtc_state->output_format;
2651 	bool hdmi_all_bpp = connector->display_info.hdmi.dsc_cap.all_bpp;
2652 	int pcon_fractional_bpp = drm_dp_pcon_dsc_bpp_incr(intel_dp->pcon_dsc_dpcd);
2653 	int hdmi_max_chunk_bytes =
2654 		connector->display_info.hdmi.dsc_cap.total_chunk_kbytes * 1024;
2655 
2656 	return intel_hdmi_dsc_get_bpp(pcon_fractional_bpp, slice_width,
2657 				      num_slices, output_format, hdmi_all_bpp,
2658 				      hdmi_max_chunk_bytes);
2659 }
2660 
2661 void
2662 intel_dp_pcon_dsc_configure(struct intel_dp *intel_dp,
2663 			    const struct intel_crtc_state *crtc_state)
2664 {
2665 	u8 pps_param[6];
2666 	int slice_height;
2667 	int slice_width;
2668 	int num_slices;
2669 	int bits_per_pixel;
2670 	int ret;
2671 	struct intel_connector *intel_connector = intel_dp->attached_connector;
2672 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2673 	struct drm_connector *connector;
2674 	bool hdmi_is_dsc_1_2;
2675 
2676 	if (!intel_dp_is_hdmi_2_1_sink(intel_dp))
2677 		return;
2678 
2679 	if (!intel_connector)
2680 		return;
2681 	connector = &intel_connector->base;
2682 	hdmi_is_dsc_1_2 = connector->display_info.hdmi.dsc_cap.v_1p2;
2683 
2684 	if (!drm_dp_pcon_enc_is_dsc_1_2(intel_dp->pcon_dsc_dpcd) ||
2685 	    !hdmi_is_dsc_1_2)
2686 		return;
2687 
2688 	slice_height = intel_dp_pcon_dsc_enc_slice_height(crtc_state);
2689 	if (!slice_height)
2690 		return;
2691 
2692 	num_slices = intel_dp_pcon_dsc_enc_slices(intel_dp, crtc_state);
2693 	if (!num_slices)
2694 		return;
2695 
2696 	slice_width = DIV_ROUND_UP(crtc_state->hw.adjusted_mode.hdisplay,
2697 				   num_slices);
2698 
2699 	bits_per_pixel = intel_dp_pcon_dsc_enc_bpp(intel_dp, crtc_state,
2700 						   num_slices, slice_width);
2701 	if (!bits_per_pixel)
2702 		return;
2703 
2704 	pps_param[0] = slice_height & 0xFF;
2705 	pps_param[1] = slice_height >> 8;
2706 	pps_param[2] = slice_width & 0xFF;
2707 	pps_param[3] = slice_width >> 8;
2708 	pps_param[4] = bits_per_pixel & 0xFF;
2709 	pps_param[5] = (bits_per_pixel >> 8) & 0x3;
2710 
2711 	ret = drm_dp_pcon_pps_override_param(&intel_dp->aux, pps_param);
2712 	if (ret < 0)
2713 		drm_dbg_kms(&i915->drm, "Failed to set pcon DSC\n");
2714 }
2715 
2716 void intel_dp_configure_protocol_converter(struct intel_dp *intel_dp,
2717 					   const struct intel_crtc_state *crtc_state)
2718 {
2719 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2720 	u8 tmp;
2721 
2722 	if (intel_dp->dpcd[DP_DPCD_REV] < 0x13)
2723 		return;
2724 
2725 	if (!drm_dp_is_branch(intel_dp->dpcd))
2726 		return;
2727 
2728 	tmp = intel_dp->has_hdmi_sink ?
2729 		DP_HDMI_DVI_OUTPUT_CONFIG : 0;
2730 
2731 	if (drm_dp_dpcd_writeb(&intel_dp->aux,
2732 			       DP_PROTOCOL_CONVERTER_CONTROL_0, tmp) != 1)
2733 		drm_dbg_kms(&i915->drm, "Failed to %s protocol converter HDMI mode\n",
2734 			    str_enable_disable(intel_dp->has_hdmi_sink));
2735 
2736 	tmp = crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444 &&
2737 		intel_dp->dfp.ycbcr_444_to_420 ? DP_CONVERSION_TO_YCBCR420_ENABLE : 0;
2738 
2739 	if (drm_dp_dpcd_writeb(&intel_dp->aux,
2740 			       DP_PROTOCOL_CONVERTER_CONTROL_1, tmp) != 1)
2741 		drm_dbg_kms(&i915->drm,
2742 			    "Failed to %s protocol converter YCbCr 4:2:0 conversion mode\n",
2743 			    str_enable_disable(intel_dp->dfp.ycbcr_444_to_420));
2744 
2745 	tmp = intel_dp->dfp.rgb_to_ycbcr ?
2746 		DP_CONVERSION_BT709_RGB_YCBCR_ENABLE : 0;
2747 
2748 	if (drm_dp_pcon_convert_rgb_to_ycbcr(&intel_dp->aux, tmp) < 0)
2749 		drm_dbg_kms(&i915->drm,
2750 			   "Failed to %s protocol converter RGB->YCbCr conversion mode\n",
2751 			   str_enable_disable(tmp));
2752 }
2753 
2754 bool intel_dp_get_colorimetry_status(struct intel_dp *intel_dp)
2755 {
2756 	u8 dprx = 0;
2757 
2758 	if (drm_dp_dpcd_readb(&intel_dp->aux, DP_DPRX_FEATURE_ENUMERATION_LIST,
2759 			      &dprx) != 1)
2760 		return false;
2761 	return dprx & DP_VSC_SDP_EXT_FOR_COLORIMETRY_SUPPORTED;
2762 }
2763 
2764 static void intel_dp_get_dsc_sink_cap(struct intel_dp *intel_dp)
2765 {
2766 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2767 
2768 	/*
2769 	 * Clear the cached register set to avoid using stale values
2770 	 * for the sinks that do not support DSC.
2771 	 */
2772 	memset(intel_dp->dsc_dpcd, 0, sizeof(intel_dp->dsc_dpcd));
2773 
2774 	/* Clear fec_capable to avoid using stale values */
2775 	intel_dp->fec_capable = 0;
2776 
2777 	/* Cache the DSC DPCD if eDP or DP rev >= 1.4 */
2778 	if (intel_dp->dpcd[DP_DPCD_REV] >= 0x14 ||
2779 	    intel_dp->edp_dpcd[0] >= DP_EDP_14) {
2780 		if (drm_dp_dpcd_read(&intel_dp->aux, DP_DSC_SUPPORT,
2781 				     intel_dp->dsc_dpcd,
2782 				     sizeof(intel_dp->dsc_dpcd)) < 0)
2783 			drm_err(&i915->drm,
2784 				"Failed to read DPCD register 0x%x\n",
2785 				DP_DSC_SUPPORT);
2786 
2787 		drm_dbg_kms(&i915->drm, "DSC DPCD: %*ph\n",
2788 			    (int)sizeof(intel_dp->dsc_dpcd),
2789 			    intel_dp->dsc_dpcd);
2790 
2791 		/* FEC is supported only on DP 1.4 */
2792 		if (!intel_dp_is_edp(intel_dp) &&
2793 		    drm_dp_dpcd_readb(&intel_dp->aux, DP_FEC_CAPABILITY,
2794 				      &intel_dp->fec_capable) < 0)
2795 			drm_err(&i915->drm,
2796 				"Failed to read FEC DPCD register\n");
2797 
2798 		drm_dbg_kms(&i915->drm, "FEC CAPABILITY: %x\n",
2799 			    intel_dp->fec_capable);
2800 	}
2801 }
2802 
2803 static void intel_edp_mso_mode_fixup(struct intel_connector *connector,
2804 				     struct drm_display_mode *mode)
2805 {
2806 	struct intel_dp *intel_dp = intel_attached_dp(connector);
2807 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
2808 	int n = intel_dp->mso_link_count;
2809 	int overlap = intel_dp->mso_pixel_overlap;
2810 
2811 	if (!mode || !n)
2812 		return;
2813 
2814 	mode->hdisplay = (mode->hdisplay - overlap) * n;
2815 	mode->hsync_start = (mode->hsync_start - overlap) * n;
2816 	mode->hsync_end = (mode->hsync_end - overlap) * n;
2817 	mode->htotal = (mode->htotal - overlap) * n;
2818 	mode->clock *= n;
2819 
2820 	drm_mode_set_name(mode);
2821 
2822 	drm_dbg_kms(&i915->drm,
2823 		    "[CONNECTOR:%d:%s] using generated MSO mode: " DRM_MODE_FMT "\n",
2824 		    connector->base.base.id, connector->base.name,
2825 		    DRM_MODE_ARG(mode));
2826 }
2827 
2828 void intel_edp_fixup_vbt_bpp(struct intel_encoder *encoder, int pipe_bpp)
2829 {
2830 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2831 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2832 	struct intel_connector *connector = intel_dp->attached_connector;
2833 
2834 	if (connector->panel.vbt.edp.bpp && pipe_bpp > connector->panel.vbt.edp.bpp) {
2835 		/*
2836 		 * This is a big fat ugly hack.
2837 		 *
2838 		 * Some machines in UEFI boot mode provide us a VBT that has 18
2839 		 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
2840 		 * unknown we fail to light up. Yet the same BIOS boots up with
2841 		 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
2842 		 * max, not what it tells us to use.
2843 		 *
2844 		 * Note: This will still be broken if the eDP panel is not lit
2845 		 * up by the BIOS, and thus we can't get the mode at module
2846 		 * load.
2847 		 */
2848 		drm_dbg_kms(&dev_priv->drm,
2849 			    "pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
2850 			    pipe_bpp, connector->panel.vbt.edp.bpp);
2851 		connector->panel.vbt.edp.bpp = pipe_bpp;
2852 	}
2853 }
2854 
2855 static void intel_edp_mso_init(struct intel_dp *intel_dp)
2856 {
2857 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2858 	struct intel_connector *connector = intel_dp->attached_connector;
2859 	struct drm_display_info *info = &connector->base.display_info;
2860 	u8 mso;
2861 
2862 	if (intel_dp->edp_dpcd[0] < DP_EDP_14)
2863 		return;
2864 
2865 	if (drm_dp_dpcd_readb(&intel_dp->aux, DP_EDP_MSO_LINK_CAPABILITIES, &mso) != 1) {
2866 		drm_err(&i915->drm, "Failed to read MSO cap\n");
2867 		return;
2868 	}
2869 
2870 	/* Valid configurations are SST or MSO 2x1, 2x2, 4x1 */
2871 	mso &= DP_EDP_MSO_NUMBER_OF_LINKS_MASK;
2872 	if (mso % 2 || mso > drm_dp_max_lane_count(intel_dp->dpcd)) {
2873 		drm_err(&i915->drm, "Invalid MSO link count cap %u\n", mso);
2874 		mso = 0;
2875 	}
2876 
2877 	if (mso) {
2878 		drm_dbg_kms(&i915->drm, "Sink MSO %ux%u configuration, pixel overlap %u\n",
2879 			    mso, drm_dp_max_lane_count(intel_dp->dpcd) / mso,
2880 			    info->mso_pixel_overlap);
2881 		if (!HAS_MSO(i915)) {
2882 			drm_err(&i915->drm, "No source MSO support, disabling\n");
2883 			mso = 0;
2884 		}
2885 	}
2886 
2887 	intel_dp->mso_link_count = mso;
2888 	intel_dp->mso_pixel_overlap = mso ? info->mso_pixel_overlap : 0;
2889 }
2890 
2891 static bool
2892 intel_edp_init_dpcd(struct intel_dp *intel_dp)
2893 {
2894 	struct drm_i915_private *dev_priv =
2895 		to_i915(dp_to_dig_port(intel_dp)->base.base.dev);
2896 
2897 	/* this function is meant to be called only once */
2898 	drm_WARN_ON(&dev_priv->drm, intel_dp->dpcd[DP_DPCD_REV] != 0);
2899 
2900 	if (drm_dp_read_dpcd_caps(&intel_dp->aux, intel_dp->dpcd) != 0)
2901 		return false;
2902 
2903 	drm_dp_read_desc(&intel_dp->aux, &intel_dp->desc,
2904 			 drm_dp_is_branch(intel_dp->dpcd));
2905 
2906 	/*
2907 	 * Read the eDP display control registers.
2908 	 *
2909 	 * Do this independent of DP_DPCD_DISPLAY_CONTROL_CAPABLE bit in
2910 	 * DP_EDP_CONFIGURATION_CAP, because some buggy displays do not have it
2911 	 * set, but require eDP 1.4+ detection (e.g. for supported link rates
2912 	 * method). The display control registers should read zero if they're
2913 	 * not supported anyway.
2914 	 */
2915 	if (drm_dp_dpcd_read(&intel_dp->aux, DP_EDP_DPCD_REV,
2916 			     intel_dp->edp_dpcd, sizeof(intel_dp->edp_dpcd)) ==
2917 			     sizeof(intel_dp->edp_dpcd)) {
2918 		drm_dbg_kms(&dev_priv->drm, "eDP DPCD: %*ph\n",
2919 			    (int)sizeof(intel_dp->edp_dpcd),
2920 			    intel_dp->edp_dpcd);
2921 
2922 		intel_dp->use_max_params = intel_dp->edp_dpcd[0] < DP_EDP_14;
2923 	}
2924 
2925 	/*
2926 	 * This has to be called after intel_dp->edp_dpcd is filled, PSR checks
2927 	 * for SET_POWER_CAPABLE bit in intel_dp->edp_dpcd[1]
2928 	 */
2929 	intel_psr_init_dpcd(intel_dp);
2930 
2931 	/* Clear the default sink rates */
2932 	intel_dp->num_sink_rates = 0;
2933 
2934 	/* Read the eDP 1.4+ supported link rates. */
2935 	if (intel_dp->edp_dpcd[0] >= DP_EDP_14) {
2936 		__le16 sink_rates[DP_MAX_SUPPORTED_RATES];
2937 		int i;
2938 
2939 		drm_dp_dpcd_read(&intel_dp->aux, DP_SUPPORTED_LINK_RATES,
2940 				sink_rates, sizeof(sink_rates));
2941 
2942 		for (i = 0; i < ARRAY_SIZE(sink_rates); i++) {
2943 			int val = le16_to_cpu(sink_rates[i]);
2944 
2945 			if (val == 0)
2946 				break;
2947 
2948 			/* Value read multiplied by 200kHz gives the per-lane
2949 			 * link rate in kHz. The source rates are, however,
2950 			 * stored in terms of LS_Clk kHz. The full conversion
2951 			 * back to symbols is
2952 			 * (val * 200kHz)*(8/10 ch. encoding)*(1/8 bit to Byte)
2953 			 */
2954 			intel_dp->sink_rates[i] = (val * 200) / 10;
2955 		}
2956 		intel_dp->num_sink_rates = i;
2957 	}
2958 
2959 	/*
2960 	 * Use DP_LINK_RATE_SET if DP_SUPPORTED_LINK_RATES are available,
2961 	 * default to DP_MAX_LINK_RATE and DP_LINK_BW_SET otherwise.
2962 	 */
2963 	if (intel_dp->num_sink_rates)
2964 		intel_dp->use_rate_select = true;
2965 	else
2966 		intel_dp_set_sink_rates(intel_dp);
2967 	intel_dp_set_max_sink_lane_count(intel_dp);
2968 
2969 	/* Read the eDP DSC DPCD registers */
2970 	if (HAS_DSC(dev_priv))
2971 		intel_dp_get_dsc_sink_cap(intel_dp);
2972 
2973 	/*
2974 	 * If needed, program our source OUI so we can make various Intel-specific AUX services
2975 	 * available (such as HDR backlight controls)
2976 	 */
2977 	intel_edp_init_source_oui(intel_dp, true);
2978 
2979 	return true;
2980 }
2981 
2982 static bool
2983 intel_dp_has_sink_count(struct intel_dp *intel_dp)
2984 {
2985 	if (!intel_dp->attached_connector)
2986 		return false;
2987 
2988 	return drm_dp_read_sink_count_cap(&intel_dp->attached_connector->base,
2989 					  intel_dp->dpcd,
2990 					  &intel_dp->desc);
2991 }
2992 
2993 static bool
2994 intel_dp_get_dpcd(struct intel_dp *intel_dp)
2995 {
2996 	int ret;
2997 
2998 	if (intel_dp_init_lttpr_and_dprx_caps(intel_dp) < 0)
2999 		return false;
3000 
3001 	/*
3002 	 * Don't clobber cached eDP rates. Also skip re-reading
3003 	 * the OUI/ID since we know it won't change.
3004 	 */
3005 	if (!intel_dp_is_edp(intel_dp)) {
3006 		drm_dp_read_desc(&intel_dp->aux, &intel_dp->desc,
3007 				 drm_dp_is_branch(intel_dp->dpcd));
3008 
3009 		intel_dp_set_sink_rates(intel_dp);
3010 		intel_dp_set_max_sink_lane_count(intel_dp);
3011 		intel_dp_set_common_rates(intel_dp);
3012 	}
3013 
3014 	if (intel_dp_has_sink_count(intel_dp)) {
3015 		ret = drm_dp_read_sink_count(&intel_dp->aux);
3016 		if (ret < 0)
3017 			return false;
3018 
3019 		/*
3020 		 * Sink count can change between short pulse hpd hence
3021 		 * a member variable in intel_dp will track any changes
3022 		 * between short pulse interrupts.
3023 		 */
3024 		intel_dp->sink_count = ret;
3025 
3026 		/*
3027 		 * SINK_COUNT == 0 and DOWNSTREAM_PORT_PRESENT == 1 implies that
3028 		 * a dongle is present but no display. Unless we require to know
3029 		 * if a dongle is present or not, we don't need to update
3030 		 * downstream port information. So, an early return here saves
3031 		 * time from performing other operations which are not required.
3032 		 */
3033 		if (!intel_dp->sink_count)
3034 			return false;
3035 	}
3036 
3037 	return drm_dp_read_downstream_info(&intel_dp->aux, intel_dp->dpcd,
3038 					   intel_dp->downstream_ports) == 0;
3039 }
3040 
3041 static bool
3042 intel_dp_can_mst(struct intel_dp *intel_dp)
3043 {
3044 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3045 
3046 	return i915->params.enable_dp_mst &&
3047 		intel_dp_mst_source_support(intel_dp) &&
3048 		drm_dp_read_mst_cap(&intel_dp->aux, intel_dp->dpcd);
3049 }
3050 
3051 static void
3052 intel_dp_configure_mst(struct intel_dp *intel_dp)
3053 {
3054 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3055 	struct intel_encoder *encoder =
3056 		&dp_to_dig_port(intel_dp)->base;
3057 	bool sink_can_mst = drm_dp_read_mst_cap(&intel_dp->aux, intel_dp->dpcd);
3058 
3059 	drm_dbg_kms(&i915->drm,
3060 		    "[ENCODER:%d:%s] MST support: port: %s, sink: %s, modparam: %s\n",
3061 		    encoder->base.base.id, encoder->base.name,
3062 		    str_yes_no(intel_dp_mst_source_support(intel_dp)),
3063 		    str_yes_no(sink_can_mst),
3064 		    str_yes_no(i915->params.enable_dp_mst));
3065 
3066 	if (!intel_dp_mst_source_support(intel_dp))
3067 		return;
3068 
3069 	intel_dp->is_mst = sink_can_mst &&
3070 		i915->params.enable_dp_mst;
3071 
3072 	drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr,
3073 					intel_dp->is_mst);
3074 }
3075 
3076 static bool
3077 intel_dp_get_sink_irq_esi(struct intel_dp *intel_dp, u8 *esi)
3078 {
3079 	return drm_dp_dpcd_read(&intel_dp->aux, DP_SINK_COUNT_ESI, esi, 4) == 4;
3080 }
3081 
3082 static bool intel_dp_ack_sink_irq_esi(struct intel_dp *intel_dp, u8 esi[4])
3083 {
3084 	int retry;
3085 
3086 	for (retry = 0; retry < 3; retry++) {
3087 		if (drm_dp_dpcd_write(&intel_dp->aux, DP_SINK_COUNT_ESI + 1,
3088 				      &esi[1], 3) == 3)
3089 			return true;
3090 	}
3091 
3092 	return false;
3093 }
3094 
3095 bool
3096 intel_dp_needs_vsc_sdp(const struct intel_crtc_state *crtc_state,
3097 		       const struct drm_connector_state *conn_state)
3098 {
3099 	/*
3100 	 * As per DP 1.4a spec section 2.2.4.3 [MSA Field for Indication
3101 	 * of Color Encoding Format and Content Color Gamut], in order to
3102 	 * sending YCBCR 420 or HDR BT.2020 signals we should use DP VSC SDP.
3103 	 */
3104 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
3105 		return true;
3106 
3107 	switch (conn_state->colorspace) {
3108 	case DRM_MODE_COLORIMETRY_SYCC_601:
3109 	case DRM_MODE_COLORIMETRY_OPYCC_601:
3110 	case DRM_MODE_COLORIMETRY_BT2020_YCC:
3111 	case DRM_MODE_COLORIMETRY_BT2020_RGB:
3112 	case DRM_MODE_COLORIMETRY_BT2020_CYCC:
3113 		return true;
3114 	default:
3115 		break;
3116 	}
3117 
3118 	return false;
3119 }
3120 
3121 static ssize_t intel_dp_vsc_sdp_pack(const struct drm_dp_vsc_sdp *vsc,
3122 				     struct dp_sdp *sdp, size_t size)
3123 {
3124 	size_t length = sizeof(struct dp_sdp);
3125 
3126 	if (size < length)
3127 		return -ENOSPC;
3128 
3129 	memset(sdp, 0, size);
3130 
3131 	/*
3132 	 * Prepare VSC Header for SU as per DP 1.4a spec, Table 2-119
3133 	 * VSC SDP Header Bytes
3134 	 */
3135 	sdp->sdp_header.HB0 = 0; /* Secondary-Data Packet ID = 0 */
3136 	sdp->sdp_header.HB1 = vsc->sdp_type; /* Secondary-data Packet Type */
3137 	sdp->sdp_header.HB2 = vsc->revision; /* Revision Number */
3138 	sdp->sdp_header.HB3 = vsc->length; /* Number of Valid Data Bytes */
3139 
3140 	/*
3141 	 * Only revision 0x5 supports Pixel Encoding/Colorimetry Format as
3142 	 * per DP 1.4a spec.
3143 	 */
3144 	if (vsc->revision != 0x5)
3145 		goto out;
3146 
3147 	/* VSC SDP Payload for DB16 through DB18 */
3148 	/* Pixel Encoding and Colorimetry Formats  */
3149 	sdp->db[16] = (vsc->pixelformat & 0xf) << 4; /* DB16[7:4] */
3150 	sdp->db[16] |= vsc->colorimetry & 0xf; /* DB16[3:0] */
3151 
3152 	switch (vsc->bpc) {
3153 	case 6:
3154 		/* 6bpc: 0x0 */
3155 		break;
3156 	case 8:
3157 		sdp->db[17] = 0x1; /* DB17[3:0] */
3158 		break;
3159 	case 10:
3160 		sdp->db[17] = 0x2;
3161 		break;
3162 	case 12:
3163 		sdp->db[17] = 0x3;
3164 		break;
3165 	case 16:
3166 		sdp->db[17] = 0x4;
3167 		break;
3168 	default:
3169 		MISSING_CASE(vsc->bpc);
3170 		break;
3171 	}
3172 	/* Dynamic Range and Component Bit Depth */
3173 	if (vsc->dynamic_range == DP_DYNAMIC_RANGE_CTA)
3174 		sdp->db[17] |= 0x80;  /* DB17[7] */
3175 
3176 	/* Content Type */
3177 	sdp->db[18] = vsc->content_type & 0x7;
3178 
3179 out:
3180 	return length;
3181 }
3182 
3183 static ssize_t
3184 intel_dp_hdr_metadata_infoframe_sdp_pack(struct drm_i915_private *i915,
3185 					 const struct hdmi_drm_infoframe *drm_infoframe,
3186 					 struct dp_sdp *sdp,
3187 					 size_t size)
3188 {
3189 	size_t length = sizeof(struct dp_sdp);
3190 	const int infoframe_size = HDMI_INFOFRAME_HEADER_SIZE + HDMI_DRM_INFOFRAME_SIZE;
3191 	unsigned char buf[HDMI_INFOFRAME_HEADER_SIZE + HDMI_DRM_INFOFRAME_SIZE];
3192 	ssize_t len;
3193 
3194 	if (size < length)
3195 		return -ENOSPC;
3196 
3197 	memset(sdp, 0, size);
3198 
3199 	len = hdmi_drm_infoframe_pack_only(drm_infoframe, buf, sizeof(buf));
3200 	if (len < 0) {
3201 		drm_dbg_kms(&i915->drm, "buffer size is smaller than hdr metadata infoframe\n");
3202 		return -ENOSPC;
3203 	}
3204 
3205 	if (len != infoframe_size) {
3206 		drm_dbg_kms(&i915->drm, "wrong static hdr metadata size\n");
3207 		return -ENOSPC;
3208 	}
3209 
3210 	/*
3211 	 * Set up the infoframe sdp packet for HDR static metadata.
3212 	 * Prepare VSC Header for SU as per DP 1.4a spec,
3213 	 * Table 2-100 and Table 2-101
3214 	 */
3215 
3216 	/* Secondary-Data Packet ID, 00h for non-Audio INFOFRAME */
3217 	sdp->sdp_header.HB0 = 0;
3218 	/*
3219 	 * Packet Type 80h + Non-audio INFOFRAME Type value
3220 	 * HDMI_INFOFRAME_TYPE_DRM: 0x87
3221 	 * - 80h + Non-audio INFOFRAME Type value
3222 	 * - InfoFrame Type: 0x07
3223 	 *    [CTA-861-G Table-42 Dynamic Range and Mastering InfoFrame]
3224 	 */
3225 	sdp->sdp_header.HB1 = drm_infoframe->type;
3226 	/*
3227 	 * Least Significant Eight Bits of (Data Byte Count – 1)
3228 	 * infoframe_size - 1
3229 	 */
3230 	sdp->sdp_header.HB2 = 0x1D;
3231 	/* INFOFRAME SDP Version Number */
3232 	sdp->sdp_header.HB3 = (0x13 << 2);
3233 	/* CTA Header Byte 2 (INFOFRAME Version Number) */
3234 	sdp->db[0] = drm_infoframe->version;
3235 	/* CTA Header Byte 3 (Length of INFOFRAME): HDMI_DRM_INFOFRAME_SIZE */
3236 	sdp->db[1] = drm_infoframe->length;
3237 	/*
3238 	 * Copy HDMI_DRM_INFOFRAME_SIZE size from a buffer after
3239 	 * HDMI_INFOFRAME_HEADER_SIZE
3240 	 */
3241 	BUILD_BUG_ON(sizeof(sdp->db) < HDMI_DRM_INFOFRAME_SIZE + 2);
3242 	memcpy(&sdp->db[2], &buf[HDMI_INFOFRAME_HEADER_SIZE],
3243 	       HDMI_DRM_INFOFRAME_SIZE);
3244 
3245 	/*
3246 	 * Size of DP infoframe sdp packet for HDR static metadata consists of
3247 	 * - DP SDP Header(struct dp_sdp_header): 4 bytes
3248 	 * - Two Data Blocks: 2 bytes
3249 	 *    CTA Header Byte2 (INFOFRAME Version Number)
3250 	 *    CTA Header Byte3 (Length of INFOFRAME)
3251 	 * - HDMI_DRM_INFOFRAME_SIZE: 26 bytes
3252 	 *
3253 	 * Prior to GEN11's GMP register size is identical to DP HDR static metadata
3254 	 * infoframe size. But GEN11+ has larger than that size, write_infoframe
3255 	 * will pad rest of the size.
3256 	 */
3257 	return sizeof(struct dp_sdp_header) + 2 + HDMI_DRM_INFOFRAME_SIZE;
3258 }
3259 
3260 static void intel_write_dp_sdp(struct intel_encoder *encoder,
3261 			       const struct intel_crtc_state *crtc_state,
3262 			       unsigned int type)
3263 {
3264 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3265 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3266 	struct dp_sdp sdp = {};
3267 	ssize_t len;
3268 
3269 	if ((crtc_state->infoframes.enable &
3270 	     intel_hdmi_infoframe_enable(type)) == 0)
3271 		return;
3272 
3273 	switch (type) {
3274 	case DP_SDP_VSC:
3275 		len = intel_dp_vsc_sdp_pack(&crtc_state->infoframes.vsc, &sdp,
3276 					    sizeof(sdp));
3277 		break;
3278 	case HDMI_PACKET_TYPE_GAMUT_METADATA:
3279 		len = intel_dp_hdr_metadata_infoframe_sdp_pack(dev_priv,
3280 							       &crtc_state->infoframes.drm.drm,
3281 							       &sdp, sizeof(sdp));
3282 		break;
3283 	default:
3284 		MISSING_CASE(type);
3285 		return;
3286 	}
3287 
3288 	if (drm_WARN_ON(&dev_priv->drm, len < 0))
3289 		return;
3290 
3291 	dig_port->write_infoframe(encoder, crtc_state, type, &sdp, len);
3292 }
3293 
3294 void intel_write_dp_vsc_sdp(struct intel_encoder *encoder,
3295 			    const struct intel_crtc_state *crtc_state,
3296 			    const struct drm_dp_vsc_sdp *vsc)
3297 {
3298 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3299 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3300 	struct dp_sdp sdp = {};
3301 	ssize_t len;
3302 
3303 	len = intel_dp_vsc_sdp_pack(vsc, &sdp, sizeof(sdp));
3304 
3305 	if (drm_WARN_ON(&dev_priv->drm, len < 0))
3306 		return;
3307 
3308 	dig_port->write_infoframe(encoder, crtc_state, DP_SDP_VSC,
3309 					&sdp, len);
3310 }
3311 
3312 void intel_dp_set_infoframes(struct intel_encoder *encoder,
3313 			     bool enable,
3314 			     const struct intel_crtc_state *crtc_state,
3315 			     const struct drm_connector_state *conn_state)
3316 {
3317 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3318 	i915_reg_t reg = HSW_TVIDEO_DIP_CTL(crtc_state->cpu_transcoder);
3319 	u32 dip_enable = VIDEO_DIP_ENABLE_AVI_HSW | VIDEO_DIP_ENABLE_GCP_HSW |
3320 			 VIDEO_DIP_ENABLE_VS_HSW | VIDEO_DIP_ENABLE_GMP_HSW |
3321 			 VIDEO_DIP_ENABLE_SPD_HSW | VIDEO_DIP_ENABLE_DRM_GLK;
3322 	u32 val = intel_de_read(dev_priv, reg) & ~dip_enable;
3323 
3324 	/* TODO: Add DSC case (DIP_ENABLE_PPS) */
3325 	/* When PSR is enabled, this routine doesn't disable VSC DIP */
3326 	if (!crtc_state->has_psr)
3327 		val &= ~VIDEO_DIP_ENABLE_VSC_HSW;
3328 
3329 	intel_de_write(dev_priv, reg, val);
3330 	intel_de_posting_read(dev_priv, reg);
3331 
3332 	if (!enable)
3333 		return;
3334 
3335 	/* When PSR is enabled, VSC SDP is handled by PSR routine */
3336 	if (!crtc_state->has_psr)
3337 		intel_write_dp_sdp(encoder, crtc_state, DP_SDP_VSC);
3338 
3339 	intel_write_dp_sdp(encoder, crtc_state, HDMI_PACKET_TYPE_GAMUT_METADATA);
3340 }
3341 
3342 static int intel_dp_vsc_sdp_unpack(struct drm_dp_vsc_sdp *vsc,
3343 				   const void *buffer, size_t size)
3344 {
3345 	const struct dp_sdp *sdp = buffer;
3346 
3347 	if (size < sizeof(struct dp_sdp))
3348 		return -EINVAL;
3349 
3350 	memset(vsc, 0, sizeof(*vsc));
3351 
3352 	if (sdp->sdp_header.HB0 != 0)
3353 		return -EINVAL;
3354 
3355 	if (sdp->sdp_header.HB1 != DP_SDP_VSC)
3356 		return -EINVAL;
3357 
3358 	vsc->sdp_type = sdp->sdp_header.HB1;
3359 	vsc->revision = sdp->sdp_header.HB2;
3360 	vsc->length = sdp->sdp_header.HB3;
3361 
3362 	if ((sdp->sdp_header.HB2 == 0x2 && sdp->sdp_header.HB3 == 0x8) ||
3363 	    (sdp->sdp_header.HB2 == 0x4 && sdp->sdp_header.HB3 == 0xe)) {
3364 		/*
3365 		 * - HB2 = 0x2, HB3 = 0x8
3366 		 *   VSC SDP supporting 3D stereo + PSR
3367 		 * - HB2 = 0x4, HB3 = 0xe
3368 		 *   VSC SDP supporting 3D stereo + PSR2 with Y-coordinate of
3369 		 *   first scan line of the SU region (applies to eDP v1.4b
3370 		 *   and higher).
3371 		 */
3372 		return 0;
3373 	} else if (sdp->sdp_header.HB2 == 0x5 && sdp->sdp_header.HB3 == 0x13) {
3374 		/*
3375 		 * - HB2 = 0x5, HB3 = 0x13
3376 		 *   VSC SDP supporting 3D stereo + PSR2 + Pixel Encoding/Colorimetry
3377 		 *   Format.
3378 		 */
3379 		vsc->pixelformat = (sdp->db[16] >> 4) & 0xf;
3380 		vsc->colorimetry = sdp->db[16] & 0xf;
3381 		vsc->dynamic_range = (sdp->db[17] >> 7) & 0x1;
3382 
3383 		switch (sdp->db[17] & 0x7) {
3384 		case 0x0:
3385 			vsc->bpc = 6;
3386 			break;
3387 		case 0x1:
3388 			vsc->bpc = 8;
3389 			break;
3390 		case 0x2:
3391 			vsc->bpc = 10;
3392 			break;
3393 		case 0x3:
3394 			vsc->bpc = 12;
3395 			break;
3396 		case 0x4:
3397 			vsc->bpc = 16;
3398 			break;
3399 		default:
3400 			MISSING_CASE(sdp->db[17] & 0x7);
3401 			return -EINVAL;
3402 		}
3403 
3404 		vsc->content_type = sdp->db[18] & 0x7;
3405 	} else {
3406 		return -EINVAL;
3407 	}
3408 
3409 	return 0;
3410 }
3411 
3412 static int
3413 intel_dp_hdr_metadata_infoframe_sdp_unpack(struct hdmi_drm_infoframe *drm_infoframe,
3414 					   const void *buffer, size_t size)
3415 {
3416 	int ret;
3417 
3418 	const struct dp_sdp *sdp = buffer;
3419 
3420 	if (size < sizeof(struct dp_sdp))
3421 		return -EINVAL;
3422 
3423 	if (sdp->sdp_header.HB0 != 0)
3424 		return -EINVAL;
3425 
3426 	if (sdp->sdp_header.HB1 != HDMI_INFOFRAME_TYPE_DRM)
3427 		return -EINVAL;
3428 
3429 	/*
3430 	 * Least Significant Eight Bits of (Data Byte Count – 1)
3431 	 * 1Dh (i.e., Data Byte Count = 30 bytes).
3432 	 */
3433 	if (sdp->sdp_header.HB2 != 0x1D)
3434 		return -EINVAL;
3435 
3436 	/* Most Significant Two Bits of (Data Byte Count – 1), Clear to 00b. */
3437 	if ((sdp->sdp_header.HB3 & 0x3) != 0)
3438 		return -EINVAL;
3439 
3440 	/* INFOFRAME SDP Version Number */
3441 	if (((sdp->sdp_header.HB3 >> 2) & 0x3f) != 0x13)
3442 		return -EINVAL;
3443 
3444 	/* CTA Header Byte 2 (INFOFRAME Version Number) */
3445 	if (sdp->db[0] != 1)
3446 		return -EINVAL;
3447 
3448 	/* CTA Header Byte 3 (Length of INFOFRAME): HDMI_DRM_INFOFRAME_SIZE */
3449 	if (sdp->db[1] != HDMI_DRM_INFOFRAME_SIZE)
3450 		return -EINVAL;
3451 
3452 	ret = hdmi_drm_infoframe_unpack_only(drm_infoframe, &sdp->db[2],
3453 					     HDMI_DRM_INFOFRAME_SIZE);
3454 
3455 	return ret;
3456 }
3457 
3458 static void intel_read_dp_vsc_sdp(struct intel_encoder *encoder,
3459 				  struct intel_crtc_state *crtc_state,
3460 				  struct drm_dp_vsc_sdp *vsc)
3461 {
3462 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3463 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3464 	unsigned int type = DP_SDP_VSC;
3465 	struct dp_sdp sdp = {};
3466 	int ret;
3467 
3468 	/* When PSR is enabled, VSC SDP is handled by PSR routine */
3469 	if (crtc_state->has_psr)
3470 		return;
3471 
3472 	if ((crtc_state->infoframes.enable &
3473 	     intel_hdmi_infoframe_enable(type)) == 0)
3474 		return;
3475 
3476 	dig_port->read_infoframe(encoder, crtc_state, type, &sdp, sizeof(sdp));
3477 
3478 	ret = intel_dp_vsc_sdp_unpack(vsc, &sdp, sizeof(sdp));
3479 
3480 	if (ret)
3481 		drm_dbg_kms(&dev_priv->drm, "Failed to unpack DP VSC SDP\n");
3482 }
3483 
3484 static void intel_read_dp_hdr_metadata_infoframe_sdp(struct intel_encoder *encoder,
3485 						     struct intel_crtc_state *crtc_state,
3486 						     struct hdmi_drm_infoframe *drm_infoframe)
3487 {
3488 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3489 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3490 	unsigned int type = HDMI_PACKET_TYPE_GAMUT_METADATA;
3491 	struct dp_sdp sdp = {};
3492 	int ret;
3493 
3494 	if ((crtc_state->infoframes.enable &
3495 	    intel_hdmi_infoframe_enable(type)) == 0)
3496 		return;
3497 
3498 	dig_port->read_infoframe(encoder, crtc_state, type, &sdp,
3499 				 sizeof(sdp));
3500 
3501 	ret = intel_dp_hdr_metadata_infoframe_sdp_unpack(drm_infoframe, &sdp,
3502 							 sizeof(sdp));
3503 
3504 	if (ret)
3505 		drm_dbg_kms(&dev_priv->drm,
3506 			    "Failed to unpack DP HDR Metadata Infoframe SDP\n");
3507 }
3508 
3509 void intel_read_dp_sdp(struct intel_encoder *encoder,
3510 		       struct intel_crtc_state *crtc_state,
3511 		       unsigned int type)
3512 {
3513 	switch (type) {
3514 	case DP_SDP_VSC:
3515 		intel_read_dp_vsc_sdp(encoder, crtc_state,
3516 				      &crtc_state->infoframes.vsc);
3517 		break;
3518 	case HDMI_PACKET_TYPE_GAMUT_METADATA:
3519 		intel_read_dp_hdr_metadata_infoframe_sdp(encoder, crtc_state,
3520 							 &crtc_state->infoframes.drm.drm);
3521 		break;
3522 	default:
3523 		MISSING_CASE(type);
3524 		break;
3525 	}
3526 }
3527 
3528 static u8 intel_dp_autotest_link_training(struct intel_dp *intel_dp)
3529 {
3530 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3531 	int status = 0;
3532 	int test_link_rate;
3533 	u8 test_lane_count, test_link_bw;
3534 	/* (DP CTS 1.2)
3535 	 * 4.3.1.11
3536 	 */
3537 	/* Read the TEST_LANE_COUNT and TEST_LINK_RTAE fields (DP CTS 3.1.4) */
3538 	status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_LANE_COUNT,
3539 				   &test_lane_count);
3540 
3541 	if (status <= 0) {
3542 		drm_dbg_kms(&i915->drm, "Lane count read failed\n");
3543 		return DP_TEST_NAK;
3544 	}
3545 	test_lane_count &= DP_MAX_LANE_COUNT_MASK;
3546 
3547 	status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_LINK_RATE,
3548 				   &test_link_bw);
3549 	if (status <= 0) {
3550 		drm_dbg_kms(&i915->drm, "Link Rate read failed\n");
3551 		return DP_TEST_NAK;
3552 	}
3553 	test_link_rate = drm_dp_bw_code_to_link_rate(test_link_bw);
3554 
3555 	/* Validate the requested link rate and lane count */
3556 	if (!intel_dp_link_params_valid(intel_dp, test_link_rate,
3557 					test_lane_count))
3558 		return DP_TEST_NAK;
3559 
3560 	intel_dp->compliance.test_lane_count = test_lane_count;
3561 	intel_dp->compliance.test_link_rate = test_link_rate;
3562 
3563 	return DP_TEST_ACK;
3564 }
3565 
3566 static u8 intel_dp_autotest_video_pattern(struct intel_dp *intel_dp)
3567 {
3568 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3569 	u8 test_pattern;
3570 	u8 test_misc;
3571 	__be16 h_width, v_height;
3572 	int status = 0;
3573 
3574 	/* Read the TEST_PATTERN (DP CTS 3.1.5) */
3575 	status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_PATTERN,
3576 				   &test_pattern);
3577 	if (status <= 0) {
3578 		drm_dbg_kms(&i915->drm, "Test pattern read failed\n");
3579 		return DP_TEST_NAK;
3580 	}
3581 	if (test_pattern != DP_COLOR_RAMP)
3582 		return DP_TEST_NAK;
3583 
3584 	status = drm_dp_dpcd_read(&intel_dp->aux, DP_TEST_H_WIDTH_HI,
3585 				  &h_width, 2);
3586 	if (status <= 0) {
3587 		drm_dbg_kms(&i915->drm, "H Width read failed\n");
3588 		return DP_TEST_NAK;
3589 	}
3590 
3591 	status = drm_dp_dpcd_read(&intel_dp->aux, DP_TEST_V_HEIGHT_HI,
3592 				  &v_height, 2);
3593 	if (status <= 0) {
3594 		drm_dbg_kms(&i915->drm, "V Height read failed\n");
3595 		return DP_TEST_NAK;
3596 	}
3597 
3598 	status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_MISC0,
3599 				   &test_misc);
3600 	if (status <= 0) {
3601 		drm_dbg_kms(&i915->drm, "TEST MISC read failed\n");
3602 		return DP_TEST_NAK;
3603 	}
3604 	if ((test_misc & DP_TEST_COLOR_FORMAT_MASK) != DP_COLOR_FORMAT_RGB)
3605 		return DP_TEST_NAK;
3606 	if (test_misc & DP_TEST_DYNAMIC_RANGE_CEA)
3607 		return DP_TEST_NAK;
3608 	switch (test_misc & DP_TEST_BIT_DEPTH_MASK) {
3609 	case DP_TEST_BIT_DEPTH_6:
3610 		intel_dp->compliance.test_data.bpc = 6;
3611 		break;
3612 	case DP_TEST_BIT_DEPTH_8:
3613 		intel_dp->compliance.test_data.bpc = 8;
3614 		break;
3615 	default:
3616 		return DP_TEST_NAK;
3617 	}
3618 
3619 	intel_dp->compliance.test_data.video_pattern = test_pattern;
3620 	intel_dp->compliance.test_data.hdisplay = be16_to_cpu(h_width);
3621 	intel_dp->compliance.test_data.vdisplay = be16_to_cpu(v_height);
3622 	/* Set test active flag here so userspace doesn't interrupt things */
3623 	intel_dp->compliance.test_active = true;
3624 
3625 	return DP_TEST_ACK;
3626 }
3627 
3628 static u8 intel_dp_autotest_edid(struct intel_dp *intel_dp)
3629 {
3630 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3631 	u8 test_result = DP_TEST_ACK;
3632 	struct intel_connector *intel_connector = intel_dp->attached_connector;
3633 	struct drm_connector *connector = &intel_connector->base;
3634 
3635 	if (intel_connector->detect_edid == NULL ||
3636 	    connector->edid_corrupt ||
3637 	    intel_dp->aux.i2c_defer_count > 6) {
3638 		/* Check EDID read for NACKs, DEFERs and corruption
3639 		 * (DP CTS 1.2 Core r1.1)
3640 		 *    4.2.2.4 : Failed EDID read, I2C_NAK
3641 		 *    4.2.2.5 : Failed EDID read, I2C_DEFER
3642 		 *    4.2.2.6 : EDID corruption detected
3643 		 * Use failsafe mode for all cases
3644 		 */
3645 		if (intel_dp->aux.i2c_nack_count > 0 ||
3646 			intel_dp->aux.i2c_defer_count > 0)
3647 			drm_dbg_kms(&i915->drm,
3648 				    "EDID read had %d NACKs, %d DEFERs\n",
3649 				    intel_dp->aux.i2c_nack_count,
3650 				    intel_dp->aux.i2c_defer_count);
3651 		intel_dp->compliance.test_data.edid = INTEL_DP_RESOLUTION_FAILSAFE;
3652 	} else {
3653 		/* FIXME: Get rid of drm_edid_raw() */
3654 		const struct edid *block = drm_edid_raw(intel_connector->detect_edid);
3655 
3656 		/* We have to write the checksum of the last block read */
3657 		block += block->extensions;
3658 
3659 		if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_EDID_CHECKSUM,
3660 				       block->checksum) <= 0)
3661 			drm_dbg_kms(&i915->drm,
3662 				    "Failed to write EDID checksum\n");
3663 
3664 		test_result = DP_TEST_ACK | DP_TEST_EDID_CHECKSUM_WRITE;
3665 		intel_dp->compliance.test_data.edid = INTEL_DP_RESOLUTION_PREFERRED;
3666 	}
3667 
3668 	/* Set test active flag here so userspace doesn't interrupt things */
3669 	intel_dp->compliance.test_active = true;
3670 
3671 	return test_result;
3672 }
3673 
3674 static void intel_dp_phy_pattern_update(struct intel_dp *intel_dp,
3675 					const struct intel_crtc_state *crtc_state)
3676 {
3677 	struct drm_i915_private *dev_priv =
3678 			to_i915(dp_to_dig_port(intel_dp)->base.base.dev);
3679 	struct drm_dp_phy_test_params *data =
3680 			&intel_dp->compliance.test_data.phytest;
3681 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3682 	enum pipe pipe = crtc->pipe;
3683 	u32 pattern_val;
3684 
3685 	switch (data->phy_pattern) {
3686 	case DP_PHY_TEST_PATTERN_NONE:
3687 		drm_dbg_kms(&dev_priv->drm, "Disable Phy Test Pattern\n");
3688 		intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe), 0x0);
3689 		break;
3690 	case DP_PHY_TEST_PATTERN_D10_2:
3691 		drm_dbg_kms(&dev_priv->drm, "Set D10.2 Phy Test Pattern\n");
3692 		intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe),
3693 			       DDI_DP_COMP_CTL_ENABLE | DDI_DP_COMP_CTL_D10_2);
3694 		break;
3695 	case DP_PHY_TEST_PATTERN_ERROR_COUNT:
3696 		drm_dbg_kms(&dev_priv->drm, "Set Error Count Phy Test Pattern\n");
3697 		intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe),
3698 			       DDI_DP_COMP_CTL_ENABLE |
3699 			       DDI_DP_COMP_CTL_SCRAMBLED_0);
3700 		break;
3701 	case DP_PHY_TEST_PATTERN_PRBS7:
3702 		drm_dbg_kms(&dev_priv->drm, "Set PRBS7 Phy Test Pattern\n");
3703 		intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe),
3704 			       DDI_DP_COMP_CTL_ENABLE | DDI_DP_COMP_CTL_PRBS7);
3705 		break;
3706 	case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
3707 		/*
3708 		 * FIXME: Ideally pattern should come from DPCD 0x250. As
3709 		 * current firmware of DPR-100 could not set it, so hardcoding
3710 		 * now for complaince test.
3711 		 */
3712 		drm_dbg_kms(&dev_priv->drm,
3713 			    "Set 80Bit Custom Phy Test Pattern 0x3e0f83e0 0x0f83e0f8 0x0000f83e\n");
3714 		pattern_val = 0x3e0f83e0;
3715 		intel_de_write(dev_priv, DDI_DP_COMP_PAT(pipe, 0), pattern_val);
3716 		pattern_val = 0x0f83e0f8;
3717 		intel_de_write(dev_priv, DDI_DP_COMP_PAT(pipe, 1), pattern_val);
3718 		pattern_val = 0x0000f83e;
3719 		intel_de_write(dev_priv, DDI_DP_COMP_PAT(pipe, 2), pattern_val);
3720 		intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe),
3721 			       DDI_DP_COMP_CTL_ENABLE |
3722 			       DDI_DP_COMP_CTL_CUSTOM80);
3723 		break;
3724 	case DP_PHY_TEST_PATTERN_CP2520:
3725 		/*
3726 		 * FIXME: Ideally pattern should come from DPCD 0x24A. As
3727 		 * current firmware of DPR-100 could not set it, so hardcoding
3728 		 * now for complaince test.
3729 		 */
3730 		drm_dbg_kms(&dev_priv->drm, "Set HBR2 compliance Phy Test Pattern\n");
3731 		pattern_val = 0xFB;
3732 		intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe),
3733 			       DDI_DP_COMP_CTL_ENABLE | DDI_DP_COMP_CTL_HBR2 |
3734 			       pattern_val);
3735 		break;
3736 	default:
3737 		WARN(1, "Invalid Phy Test Pattern\n");
3738 	}
3739 }
3740 
3741 static void intel_dp_process_phy_request(struct intel_dp *intel_dp,
3742 					 const struct intel_crtc_state *crtc_state)
3743 {
3744 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3745 	struct drm_dp_phy_test_params *data =
3746 		&intel_dp->compliance.test_data.phytest;
3747 	u8 link_status[DP_LINK_STATUS_SIZE];
3748 
3749 	if (drm_dp_dpcd_read_phy_link_status(&intel_dp->aux, DP_PHY_DPRX,
3750 					     link_status) < 0) {
3751 		drm_dbg_kms(&i915->drm, "failed to get link status\n");
3752 		return;
3753 	}
3754 
3755 	/* retrieve vswing & pre-emphasis setting */
3756 	intel_dp_get_adjust_train(intel_dp, crtc_state, DP_PHY_DPRX,
3757 				  link_status);
3758 
3759 	intel_dp_set_signal_levels(intel_dp, crtc_state, DP_PHY_DPRX);
3760 
3761 	intel_dp_phy_pattern_update(intel_dp, crtc_state);
3762 
3763 	drm_dp_dpcd_write(&intel_dp->aux, DP_TRAINING_LANE0_SET,
3764 			  intel_dp->train_set, crtc_state->lane_count);
3765 
3766 	drm_dp_set_phy_test_pattern(&intel_dp->aux, data,
3767 				    link_status[DP_DPCD_REV]);
3768 }
3769 
3770 static u8 intel_dp_autotest_phy_pattern(struct intel_dp *intel_dp)
3771 {
3772 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3773 	struct drm_dp_phy_test_params *data =
3774 		&intel_dp->compliance.test_data.phytest;
3775 
3776 	if (drm_dp_get_phy_test_pattern(&intel_dp->aux, data)) {
3777 		drm_dbg_kms(&i915->drm, "DP Phy Test pattern AUX read failure\n");
3778 		return DP_TEST_NAK;
3779 	}
3780 
3781 	/* Set test active flag here so userspace doesn't interrupt things */
3782 	intel_dp->compliance.test_active = true;
3783 
3784 	return DP_TEST_ACK;
3785 }
3786 
3787 static void intel_dp_handle_test_request(struct intel_dp *intel_dp)
3788 {
3789 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3790 	u8 response = DP_TEST_NAK;
3791 	u8 request = 0;
3792 	int status;
3793 
3794 	status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_REQUEST, &request);
3795 	if (status <= 0) {
3796 		drm_dbg_kms(&i915->drm,
3797 			    "Could not read test request from sink\n");
3798 		goto update_status;
3799 	}
3800 
3801 	switch (request) {
3802 	case DP_TEST_LINK_TRAINING:
3803 		drm_dbg_kms(&i915->drm, "LINK_TRAINING test requested\n");
3804 		response = intel_dp_autotest_link_training(intel_dp);
3805 		break;
3806 	case DP_TEST_LINK_VIDEO_PATTERN:
3807 		drm_dbg_kms(&i915->drm, "TEST_PATTERN test requested\n");
3808 		response = intel_dp_autotest_video_pattern(intel_dp);
3809 		break;
3810 	case DP_TEST_LINK_EDID_READ:
3811 		drm_dbg_kms(&i915->drm, "EDID test requested\n");
3812 		response = intel_dp_autotest_edid(intel_dp);
3813 		break;
3814 	case DP_TEST_LINK_PHY_TEST_PATTERN:
3815 		drm_dbg_kms(&i915->drm, "PHY_PATTERN test requested\n");
3816 		response = intel_dp_autotest_phy_pattern(intel_dp);
3817 		break;
3818 	default:
3819 		drm_dbg_kms(&i915->drm, "Invalid test request '%02x'\n",
3820 			    request);
3821 		break;
3822 	}
3823 
3824 	if (response & DP_TEST_ACK)
3825 		intel_dp->compliance.test_type = request;
3826 
3827 update_status:
3828 	status = drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_RESPONSE, response);
3829 	if (status <= 0)
3830 		drm_dbg_kms(&i915->drm,
3831 			    "Could not write test response to sink\n");
3832 }
3833 
3834 static bool intel_dp_link_ok(struct intel_dp *intel_dp,
3835 			     u8 link_status[DP_LINK_STATUS_SIZE])
3836 {
3837 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3838 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3839 	bool uhbr = intel_dp->link_rate >= 1000000;
3840 	bool ok;
3841 
3842 	if (uhbr)
3843 		ok = drm_dp_128b132b_lane_channel_eq_done(link_status,
3844 							  intel_dp->lane_count);
3845 	else
3846 		ok = drm_dp_channel_eq_ok(link_status, intel_dp->lane_count);
3847 
3848 	if (ok)
3849 		return true;
3850 
3851 	intel_dp_dump_link_status(intel_dp, DP_PHY_DPRX, link_status);
3852 	drm_dbg_kms(&i915->drm,
3853 		    "[ENCODER:%d:%s] %s link not ok, retraining\n",
3854 		    encoder->base.base.id, encoder->base.name,
3855 		    uhbr ? "128b/132b" : "8b/10b");
3856 
3857 	return false;
3858 }
3859 
3860 static void
3861 intel_dp_mst_hpd_irq(struct intel_dp *intel_dp, u8 *esi, u8 *ack)
3862 {
3863 	bool handled = false;
3864 
3865 	drm_dp_mst_hpd_irq(&intel_dp->mst_mgr, esi, &handled);
3866 	if (handled)
3867 		ack[1] |= esi[1] & (DP_DOWN_REP_MSG_RDY | DP_UP_REQ_MSG_RDY);
3868 
3869 	if (esi[1] & DP_CP_IRQ) {
3870 		intel_hdcp_handle_cp_irq(intel_dp->attached_connector);
3871 		ack[1] |= DP_CP_IRQ;
3872 	}
3873 }
3874 
3875 static bool intel_dp_mst_link_status(struct intel_dp *intel_dp)
3876 {
3877 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3878 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3879 	u8 link_status[DP_LINK_STATUS_SIZE] = {};
3880 	const size_t esi_link_status_size = DP_LINK_STATUS_SIZE - 2;
3881 
3882 	if (drm_dp_dpcd_read(&intel_dp->aux, DP_LANE0_1_STATUS_ESI, link_status,
3883 			     esi_link_status_size) != esi_link_status_size) {
3884 		drm_err(&i915->drm,
3885 			"[ENCODER:%d:%s] Failed to read link status\n",
3886 			encoder->base.base.id, encoder->base.name);
3887 		return false;
3888 	}
3889 
3890 	return intel_dp_link_ok(intel_dp, link_status);
3891 }
3892 
3893 /**
3894  * intel_dp_check_mst_status - service any pending MST interrupts, check link status
3895  * @intel_dp: Intel DP struct
3896  *
3897  * Read any pending MST interrupts, call MST core to handle these and ack the
3898  * interrupts. Check if the main and AUX link state is ok.
3899  *
3900  * Returns:
3901  * - %true if pending interrupts were serviced (or no interrupts were
3902  *   pending) w/o detecting an error condition.
3903  * - %false if an error condition - like AUX failure or a loss of link - is
3904  *   detected, which needs servicing from the hotplug work.
3905  */
3906 static bool
3907 intel_dp_check_mst_status(struct intel_dp *intel_dp)
3908 {
3909 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3910 	bool link_ok = true;
3911 
3912 	drm_WARN_ON_ONCE(&i915->drm, intel_dp->active_mst_links < 0);
3913 
3914 	for (;;) {
3915 		u8 esi[4] = {};
3916 		u8 ack[4] = {};
3917 
3918 		if (!intel_dp_get_sink_irq_esi(intel_dp, esi)) {
3919 			drm_dbg_kms(&i915->drm,
3920 				    "failed to get ESI - device may have failed\n");
3921 			link_ok = false;
3922 
3923 			break;
3924 		}
3925 
3926 		drm_dbg_kms(&i915->drm, "DPRX ESI: %4ph\n", esi);
3927 
3928 		if (intel_dp->active_mst_links > 0 && link_ok &&
3929 		    esi[3] & LINK_STATUS_CHANGED) {
3930 			if (!intel_dp_mst_link_status(intel_dp))
3931 				link_ok = false;
3932 			ack[3] |= LINK_STATUS_CHANGED;
3933 		}
3934 
3935 		intel_dp_mst_hpd_irq(intel_dp, esi, ack);
3936 
3937 		if (!memchr_inv(ack, 0, sizeof(ack)))
3938 			break;
3939 
3940 		if (!intel_dp_ack_sink_irq_esi(intel_dp, ack))
3941 			drm_dbg_kms(&i915->drm, "Failed to ack ESI\n");
3942 	}
3943 
3944 	return link_ok;
3945 }
3946 
3947 static void
3948 intel_dp_handle_hdmi_link_status_change(struct intel_dp *intel_dp)
3949 {
3950 	bool is_active;
3951 	u8 buf = 0;
3952 
3953 	is_active = drm_dp_pcon_hdmi_link_active(&intel_dp->aux);
3954 	if (intel_dp->frl.is_trained && !is_active) {
3955 		if (drm_dp_dpcd_readb(&intel_dp->aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf) < 0)
3956 			return;
3957 
3958 		buf &=  ~DP_PCON_ENABLE_HDMI_LINK;
3959 		if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_PCON_HDMI_LINK_CONFIG_1, buf) < 0)
3960 			return;
3961 
3962 		drm_dp_pcon_hdmi_frl_link_error_count(&intel_dp->aux, &intel_dp->attached_connector->base);
3963 
3964 		intel_dp->frl.is_trained = false;
3965 
3966 		/* Restart FRL training or fall back to TMDS mode */
3967 		intel_dp_check_frl_training(intel_dp);
3968 	}
3969 }
3970 
3971 static bool
3972 intel_dp_needs_link_retrain(struct intel_dp *intel_dp)
3973 {
3974 	u8 link_status[DP_LINK_STATUS_SIZE];
3975 
3976 	if (!intel_dp->link_trained)
3977 		return false;
3978 
3979 	/*
3980 	 * While PSR source HW is enabled, it will control main-link sending
3981 	 * frames, enabling and disabling it so trying to do a retrain will fail
3982 	 * as the link would or not be on or it could mix training patterns
3983 	 * and frame data at the same time causing retrain to fail.
3984 	 * Also when exiting PSR, HW will retrain the link anyways fixing
3985 	 * any link status error.
3986 	 */
3987 	if (intel_psr_enabled(intel_dp))
3988 		return false;
3989 
3990 	if (drm_dp_dpcd_read_phy_link_status(&intel_dp->aux, DP_PHY_DPRX,
3991 					     link_status) < 0)
3992 		return false;
3993 
3994 	/*
3995 	 * Validate the cached values of intel_dp->link_rate and
3996 	 * intel_dp->lane_count before attempting to retrain.
3997 	 *
3998 	 * FIXME would be nice to user the crtc state here, but since
3999 	 * we need to call this from the short HPD handler that seems
4000 	 * a bit hard.
4001 	 */
4002 	if (!intel_dp_link_params_valid(intel_dp, intel_dp->link_rate,
4003 					intel_dp->lane_count))
4004 		return false;
4005 
4006 	/* Retrain if link not ok */
4007 	return !intel_dp_link_ok(intel_dp, link_status);
4008 }
4009 
4010 static bool intel_dp_has_connector(struct intel_dp *intel_dp,
4011 				   const struct drm_connector_state *conn_state)
4012 {
4013 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4014 	struct intel_encoder *encoder;
4015 	enum pipe pipe;
4016 
4017 	if (!conn_state->best_encoder)
4018 		return false;
4019 
4020 	/* SST */
4021 	encoder = &dp_to_dig_port(intel_dp)->base;
4022 	if (conn_state->best_encoder == &encoder->base)
4023 		return true;
4024 
4025 	/* MST */
4026 	for_each_pipe(i915, pipe) {
4027 		encoder = &intel_dp->mst_encoders[pipe]->base;
4028 		if (conn_state->best_encoder == &encoder->base)
4029 			return true;
4030 	}
4031 
4032 	return false;
4033 }
4034 
4035 static int intel_dp_prep_link_retrain(struct intel_dp *intel_dp,
4036 				      struct drm_modeset_acquire_ctx *ctx,
4037 				      u8 *pipe_mask)
4038 {
4039 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4040 	struct drm_connector_list_iter conn_iter;
4041 	struct intel_connector *connector;
4042 	int ret = 0;
4043 
4044 	*pipe_mask = 0;
4045 
4046 	if (!intel_dp_needs_link_retrain(intel_dp))
4047 		return 0;
4048 
4049 	drm_connector_list_iter_begin(&i915->drm, &conn_iter);
4050 	for_each_intel_connector_iter(connector, &conn_iter) {
4051 		struct drm_connector_state *conn_state =
4052 			connector->base.state;
4053 		struct intel_crtc_state *crtc_state;
4054 		struct intel_crtc *crtc;
4055 
4056 		if (!intel_dp_has_connector(intel_dp, conn_state))
4057 			continue;
4058 
4059 		crtc = to_intel_crtc(conn_state->crtc);
4060 		if (!crtc)
4061 			continue;
4062 
4063 		ret = drm_modeset_lock(&crtc->base.mutex, ctx);
4064 		if (ret)
4065 			break;
4066 
4067 		crtc_state = to_intel_crtc_state(crtc->base.state);
4068 
4069 		drm_WARN_ON(&i915->drm, !intel_crtc_has_dp_encoder(crtc_state));
4070 
4071 		if (!crtc_state->hw.active)
4072 			continue;
4073 
4074 		if (conn_state->commit &&
4075 		    !try_wait_for_completion(&conn_state->commit->hw_done))
4076 			continue;
4077 
4078 		*pipe_mask |= BIT(crtc->pipe);
4079 	}
4080 	drm_connector_list_iter_end(&conn_iter);
4081 
4082 	if (!intel_dp_needs_link_retrain(intel_dp))
4083 		*pipe_mask = 0;
4084 
4085 	return ret;
4086 }
4087 
4088 static bool intel_dp_is_connected(struct intel_dp *intel_dp)
4089 {
4090 	struct intel_connector *connector = intel_dp->attached_connector;
4091 
4092 	return connector->base.status == connector_status_connected ||
4093 		intel_dp->is_mst;
4094 }
4095 
4096 int intel_dp_retrain_link(struct intel_encoder *encoder,
4097 			  struct drm_modeset_acquire_ctx *ctx)
4098 {
4099 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4100 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4101 	struct intel_crtc *crtc;
4102 	u8 pipe_mask;
4103 	int ret;
4104 
4105 	if (!intel_dp_is_connected(intel_dp))
4106 		return 0;
4107 
4108 	ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
4109 			       ctx);
4110 	if (ret)
4111 		return ret;
4112 
4113 	ret = intel_dp_prep_link_retrain(intel_dp, ctx, &pipe_mask);
4114 	if (ret)
4115 		return ret;
4116 
4117 	if (pipe_mask == 0)
4118 		return 0;
4119 
4120 	drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s] retraining link\n",
4121 		    encoder->base.base.id, encoder->base.name);
4122 
4123 	for_each_intel_crtc_in_pipe_mask(&dev_priv->drm, crtc, pipe_mask) {
4124 		const struct intel_crtc_state *crtc_state =
4125 			to_intel_crtc_state(crtc->base.state);
4126 
4127 		/* Suppress underruns caused by re-training */
4128 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
4129 		if (crtc_state->has_pch_encoder)
4130 			intel_set_pch_fifo_underrun_reporting(dev_priv,
4131 							      intel_crtc_pch_transcoder(crtc), false);
4132 	}
4133 
4134 	for_each_intel_crtc_in_pipe_mask(&dev_priv->drm, crtc, pipe_mask) {
4135 		const struct intel_crtc_state *crtc_state =
4136 			to_intel_crtc_state(crtc->base.state);
4137 
4138 		/* retrain on the MST master transcoder */
4139 		if (DISPLAY_VER(dev_priv) >= 12 &&
4140 		    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST) &&
4141 		    !intel_dp_mst_is_master_trans(crtc_state))
4142 			continue;
4143 
4144 		intel_dp_check_frl_training(intel_dp);
4145 		intel_dp_pcon_dsc_configure(intel_dp, crtc_state);
4146 		intel_dp_start_link_train(intel_dp, crtc_state);
4147 		intel_dp_stop_link_train(intel_dp, crtc_state);
4148 		break;
4149 	}
4150 
4151 	for_each_intel_crtc_in_pipe_mask(&dev_priv->drm, crtc, pipe_mask) {
4152 		const struct intel_crtc_state *crtc_state =
4153 			to_intel_crtc_state(crtc->base.state);
4154 
4155 		/* Keep underrun reporting disabled until things are stable */
4156 		intel_crtc_wait_for_next_vblank(crtc);
4157 
4158 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
4159 		if (crtc_state->has_pch_encoder)
4160 			intel_set_pch_fifo_underrun_reporting(dev_priv,
4161 							      intel_crtc_pch_transcoder(crtc), true);
4162 	}
4163 
4164 	return 0;
4165 }
4166 
4167 static int intel_dp_prep_phy_test(struct intel_dp *intel_dp,
4168 				  struct drm_modeset_acquire_ctx *ctx,
4169 				  u8 *pipe_mask)
4170 {
4171 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4172 	struct drm_connector_list_iter conn_iter;
4173 	struct intel_connector *connector;
4174 	int ret = 0;
4175 
4176 	*pipe_mask = 0;
4177 
4178 	drm_connector_list_iter_begin(&i915->drm, &conn_iter);
4179 	for_each_intel_connector_iter(connector, &conn_iter) {
4180 		struct drm_connector_state *conn_state =
4181 			connector->base.state;
4182 		struct intel_crtc_state *crtc_state;
4183 		struct intel_crtc *crtc;
4184 
4185 		if (!intel_dp_has_connector(intel_dp, conn_state))
4186 			continue;
4187 
4188 		crtc = to_intel_crtc(conn_state->crtc);
4189 		if (!crtc)
4190 			continue;
4191 
4192 		ret = drm_modeset_lock(&crtc->base.mutex, ctx);
4193 		if (ret)
4194 			break;
4195 
4196 		crtc_state = to_intel_crtc_state(crtc->base.state);
4197 
4198 		drm_WARN_ON(&i915->drm, !intel_crtc_has_dp_encoder(crtc_state));
4199 
4200 		if (!crtc_state->hw.active)
4201 			continue;
4202 
4203 		if (conn_state->commit &&
4204 		    !try_wait_for_completion(&conn_state->commit->hw_done))
4205 			continue;
4206 
4207 		*pipe_mask |= BIT(crtc->pipe);
4208 	}
4209 	drm_connector_list_iter_end(&conn_iter);
4210 
4211 	return ret;
4212 }
4213 
4214 static int intel_dp_do_phy_test(struct intel_encoder *encoder,
4215 				struct drm_modeset_acquire_ctx *ctx)
4216 {
4217 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4218 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4219 	struct intel_crtc *crtc;
4220 	u8 pipe_mask;
4221 	int ret;
4222 
4223 	ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
4224 			       ctx);
4225 	if (ret)
4226 		return ret;
4227 
4228 	ret = intel_dp_prep_phy_test(intel_dp, ctx, &pipe_mask);
4229 	if (ret)
4230 		return ret;
4231 
4232 	if (pipe_mask == 0)
4233 		return 0;
4234 
4235 	drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s] PHY test\n",
4236 		    encoder->base.base.id, encoder->base.name);
4237 
4238 	for_each_intel_crtc_in_pipe_mask(&dev_priv->drm, crtc, pipe_mask) {
4239 		const struct intel_crtc_state *crtc_state =
4240 			to_intel_crtc_state(crtc->base.state);
4241 
4242 		/* test on the MST master transcoder */
4243 		if (DISPLAY_VER(dev_priv) >= 12 &&
4244 		    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST) &&
4245 		    !intel_dp_mst_is_master_trans(crtc_state))
4246 			continue;
4247 
4248 		intel_dp_process_phy_request(intel_dp, crtc_state);
4249 		break;
4250 	}
4251 
4252 	return 0;
4253 }
4254 
4255 void intel_dp_phy_test(struct intel_encoder *encoder)
4256 {
4257 	struct drm_modeset_acquire_ctx ctx;
4258 	int ret;
4259 
4260 	drm_modeset_acquire_init(&ctx, 0);
4261 
4262 	for (;;) {
4263 		ret = intel_dp_do_phy_test(encoder, &ctx);
4264 
4265 		if (ret == -EDEADLK) {
4266 			drm_modeset_backoff(&ctx);
4267 			continue;
4268 		}
4269 
4270 		break;
4271 	}
4272 
4273 	drm_modeset_drop_locks(&ctx);
4274 	drm_modeset_acquire_fini(&ctx);
4275 	drm_WARN(encoder->base.dev, ret,
4276 		 "Acquiring modeset locks failed with %i\n", ret);
4277 }
4278 
4279 static void intel_dp_check_device_service_irq(struct intel_dp *intel_dp)
4280 {
4281 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4282 	u8 val;
4283 
4284 	if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
4285 		return;
4286 
4287 	if (drm_dp_dpcd_readb(&intel_dp->aux,
4288 			      DP_DEVICE_SERVICE_IRQ_VECTOR, &val) != 1 || !val)
4289 		return;
4290 
4291 	drm_dp_dpcd_writeb(&intel_dp->aux, DP_DEVICE_SERVICE_IRQ_VECTOR, val);
4292 
4293 	if (val & DP_AUTOMATED_TEST_REQUEST)
4294 		intel_dp_handle_test_request(intel_dp);
4295 
4296 	if (val & DP_CP_IRQ)
4297 		intel_hdcp_handle_cp_irq(intel_dp->attached_connector);
4298 
4299 	if (val & DP_SINK_SPECIFIC_IRQ)
4300 		drm_dbg_kms(&i915->drm, "Sink specific irq unhandled\n");
4301 }
4302 
4303 static void intel_dp_check_link_service_irq(struct intel_dp *intel_dp)
4304 {
4305 	u8 val;
4306 
4307 	if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
4308 		return;
4309 
4310 	if (drm_dp_dpcd_readb(&intel_dp->aux,
4311 			      DP_LINK_SERVICE_IRQ_VECTOR_ESI0, &val) != 1 || !val)
4312 		return;
4313 
4314 	if (drm_dp_dpcd_writeb(&intel_dp->aux,
4315 			       DP_LINK_SERVICE_IRQ_VECTOR_ESI0, val) != 1)
4316 		return;
4317 
4318 	if (val & HDMI_LINK_STATUS_CHANGED)
4319 		intel_dp_handle_hdmi_link_status_change(intel_dp);
4320 }
4321 
4322 /*
4323  * According to DP spec
4324  * 5.1.2:
4325  *  1. Read DPCD
4326  *  2. Configure link according to Receiver Capabilities
4327  *  3. Use Link Training from 2.5.3.3 and 3.5.1.3
4328  *  4. Check link status on receipt of hot-plug interrupt
4329  *
4330  * intel_dp_short_pulse -  handles short pulse interrupts
4331  * when full detection is not required.
4332  * Returns %true if short pulse is handled and full detection
4333  * is NOT required and %false otherwise.
4334  */
4335 static bool
4336 intel_dp_short_pulse(struct intel_dp *intel_dp)
4337 {
4338 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
4339 	u8 old_sink_count = intel_dp->sink_count;
4340 	bool ret;
4341 
4342 	/*
4343 	 * Clearing compliance test variables to allow capturing
4344 	 * of values for next automated test request.
4345 	 */
4346 	memset(&intel_dp->compliance, 0, sizeof(intel_dp->compliance));
4347 
4348 	/*
4349 	 * Now read the DPCD to see if it's actually running
4350 	 * If the current value of sink count doesn't match with
4351 	 * the value that was stored earlier or dpcd read failed
4352 	 * we need to do full detection
4353 	 */
4354 	ret = intel_dp_get_dpcd(intel_dp);
4355 
4356 	if ((old_sink_count != intel_dp->sink_count) || !ret) {
4357 		/* No need to proceed if we are going to do full detect */
4358 		return false;
4359 	}
4360 
4361 	intel_dp_check_device_service_irq(intel_dp);
4362 	intel_dp_check_link_service_irq(intel_dp);
4363 
4364 	/* Handle CEC interrupts, if any */
4365 	drm_dp_cec_irq(&intel_dp->aux);
4366 
4367 	/* defer to the hotplug work for link retraining if needed */
4368 	if (intel_dp_needs_link_retrain(intel_dp))
4369 		return false;
4370 
4371 	intel_psr_short_pulse(intel_dp);
4372 
4373 	switch (intel_dp->compliance.test_type) {
4374 	case DP_TEST_LINK_TRAINING:
4375 		drm_dbg_kms(&dev_priv->drm,
4376 			    "Link Training Compliance Test requested\n");
4377 		/* Send a Hotplug Uevent to userspace to start modeset */
4378 		drm_kms_helper_hotplug_event(&dev_priv->drm);
4379 		break;
4380 	case DP_TEST_LINK_PHY_TEST_PATTERN:
4381 		drm_dbg_kms(&dev_priv->drm,
4382 			    "PHY test pattern Compliance Test requested\n");
4383 		/*
4384 		 * Schedule long hpd to do the test
4385 		 *
4386 		 * FIXME get rid of the ad-hoc phy test modeset code
4387 		 * and properly incorporate it into the normal modeset.
4388 		 */
4389 		return false;
4390 	}
4391 
4392 	return true;
4393 }
4394 
4395 /* XXX this is probably wrong for multiple downstream ports */
4396 static enum drm_connector_status
4397 intel_dp_detect_dpcd(struct intel_dp *intel_dp)
4398 {
4399 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4400 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4401 	u8 *dpcd = intel_dp->dpcd;
4402 	u8 type;
4403 
4404 	if (drm_WARN_ON(&i915->drm, intel_dp_is_edp(intel_dp)))
4405 		return connector_status_connected;
4406 
4407 	lspcon_resume(dig_port);
4408 
4409 	if (!intel_dp_get_dpcd(intel_dp))
4410 		return connector_status_disconnected;
4411 
4412 	/* if there's no downstream port, we're done */
4413 	if (!drm_dp_is_branch(dpcd))
4414 		return connector_status_connected;
4415 
4416 	/* If we're HPD-aware, SINK_COUNT changes dynamically */
4417 	if (intel_dp_has_sink_count(intel_dp) &&
4418 	    intel_dp->downstream_ports[0] & DP_DS_PORT_HPD) {
4419 		return intel_dp->sink_count ?
4420 		connector_status_connected : connector_status_disconnected;
4421 	}
4422 
4423 	if (intel_dp_can_mst(intel_dp))
4424 		return connector_status_connected;
4425 
4426 	/* If no HPD, poke DDC gently */
4427 	if (drm_probe_ddc(&intel_dp->aux.ddc))
4428 		return connector_status_connected;
4429 
4430 	/* Well we tried, say unknown for unreliable port types */
4431 	if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11) {
4432 		type = intel_dp->downstream_ports[0] & DP_DS_PORT_TYPE_MASK;
4433 		if (type == DP_DS_PORT_TYPE_VGA ||
4434 		    type == DP_DS_PORT_TYPE_NON_EDID)
4435 			return connector_status_unknown;
4436 	} else {
4437 		type = intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
4438 			DP_DWN_STRM_PORT_TYPE_MASK;
4439 		if (type == DP_DWN_STRM_PORT_TYPE_ANALOG ||
4440 		    type == DP_DWN_STRM_PORT_TYPE_OTHER)
4441 			return connector_status_unknown;
4442 	}
4443 
4444 	/* Anything else is out of spec, warn and ignore */
4445 	drm_dbg_kms(&i915->drm, "Broken DP branch device, ignoring\n");
4446 	return connector_status_disconnected;
4447 }
4448 
4449 static enum drm_connector_status
4450 edp_detect(struct intel_dp *intel_dp)
4451 {
4452 	return connector_status_connected;
4453 }
4454 
4455 /*
4456  * intel_digital_port_connected - is the specified port connected?
4457  * @encoder: intel_encoder
4458  *
4459  * In cases where there's a connector physically connected but it can't be used
4460  * by our hardware we also return false, since the rest of the driver should
4461  * pretty much treat the port as disconnected. This is relevant for type-C
4462  * (starting on ICL) where there's ownership involved.
4463  *
4464  * Return %true if port is connected, %false otherwise.
4465  */
4466 bool intel_digital_port_connected(struct intel_encoder *encoder)
4467 {
4468 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4469 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
4470 	bool is_connected = false;
4471 	intel_wakeref_t wakeref;
4472 
4473 	with_intel_display_power(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref)
4474 		is_connected = dig_port->connected(encoder);
4475 
4476 	return is_connected;
4477 }
4478 
4479 static const struct drm_edid *
4480 intel_dp_get_edid(struct intel_dp *intel_dp)
4481 {
4482 	struct intel_connector *connector = intel_dp->attached_connector;
4483 	const struct drm_edid *fixed_edid = connector->panel.fixed_edid;
4484 
4485 	/* Use panel fixed edid if we have one */
4486 	if (fixed_edid) {
4487 		/* invalid edid */
4488 		if (IS_ERR(fixed_edid))
4489 			return NULL;
4490 
4491 		return drm_edid_dup(fixed_edid);
4492 	}
4493 
4494 	return drm_edid_read_ddc(&connector->base, &intel_dp->aux.ddc);
4495 }
4496 
4497 static void
4498 intel_dp_update_dfp(struct intel_dp *intel_dp,
4499 		    const struct drm_edid *drm_edid)
4500 {
4501 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4502 	struct intel_connector *connector = intel_dp->attached_connector;
4503 	const struct edid *edid;
4504 
4505 	/* FIXME: Get rid of drm_edid_raw() */
4506 	edid = drm_edid_raw(drm_edid);
4507 
4508 	intel_dp->dfp.max_bpc =
4509 		drm_dp_downstream_max_bpc(intel_dp->dpcd,
4510 					  intel_dp->downstream_ports, edid);
4511 
4512 	intel_dp->dfp.max_dotclock =
4513 		drm_dp_downstream_max_dotclock(intel_dp->dpcd,
4514 					       intel_dp->downstream_ports);
4515 
4516 	intel_dp->dfp.min_tmds_clock =
4517 		drm_dp_downstream_min_tmds_clock(intel_dp->dpcd,
4518 						 intel_dp->downstream_ports,
4519 						 edid);
4520 	intel_dp->dfp.max_tmds_clock =
4521 		drm_dp_downstream_max_tmds_clock(intel_dp->dpcd,
4522 						 intel_dp->downstream_ports,
4523 						 edid);
4524 
4525 	intel_dp->dfp.pcon_max_frl_bw =
4526 		drm_dp_get_pcon_max_frl_bw(intel_dp->dpcd,
4527 					   intel_dp->downstream_ports);
4528 
4529 	drm_dbg_kms(&i915->drm,
4530 		    "[CONNECTOR:%d:%s] DFP max bpc %d, max dotclock %d, TMDS clock %d-%d, PCON Max FRL BW %dGbps\n",
4531 		    connector->base.base.id, connector->base.name,
4532 		    intel_dp->dfp.max_bpc,
4533 		    intel_dp->dfp.max_dotclock,
4534 		    intel_dp->dfp.min_tmds_clock,
4535 		    intel_dp->dfp.max_tmds_clock,
4536 		    intel_dp->dfp.pcon_max_frl_bw);
4537 
4538 	intel_dp_get_pcon_dsc_cap(intel_dp);
4539 }
4540 
4541 static void
4542 intel_dp_update_420(struct intel_dp *intel_dp)
4543 {
4544 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4545 	struct intel_connector *connector = intel_dp->attached_connector;
4546 	bool is_branch, ycbcr_420_passthrough, ycbcr_444_to_420, rgb_to_ycbcr;
4547 
4548 	/* No YCbCr output support on gmch platforms */
4549 	if (HAS_GMCH(i915))
4550 		return;
4551 
4552 	/*
4553 	 * ILK doesn't seem capable of DP YCbCr output. The
4554 	 * displayed image is severly corrupted. SNB+ is fine.
4555 	 */
4556 	if (IS_IRONLAKE(i915))
4557 		return;
4558 
4559 	is_branch = drm_dp_is_branch(intel_dp->dpcd);
4560 	ycbcr_420_passthrough =
4561 		drm_dp_downstream_420_passthrough(intel_dp->dpcd,
4562 						  intel_dp->downstream_ports);
4563 	/* on-board LSPCON always assumed to support 4:4:4->4:2:0 conversion */
4564 	ycbcr_444_to_420 =
4565 		dp_to_dig_port(intel_dp)->lspcon.active ||
4566 		drm_dp_downstream_444_to_420_conversion(intel_dp->dpcd,
4567 							intel_dp->downstream_ports);
4568 	rgb_to_ycbcr = drm_dp_downstream_rgb_to_ycbcr_conversion(intel_dp->dpcd,
4569 								 intel_dp->downstream_ports,
4570 								 DP_DS_HDMI_BT709_RGB_YCBCR_CONV);
4571 
4572 	if (DISPLAY_VER(i915) >= 11) {
4573 		/* Let PCON convert from RGB->YCbCr if possible */
4574 		if (is_branch && rgb_to_ycbcr && ycbcr_444_to_420) {
4575 			intel_dp->dfp.rgb_to_ycbcr = true;
4576 			intel_dp->dfp.ycbcr_444_to_420 = true;
4577 			connector->base.ycbcr_420_allowed = true;
4578 		} else {
4579 		/* Prefer 4:2:0 passthrough over 4:4:4->4:2:0 conversion */
4580 			intel_dp->dfp.ycbcr_444_to_420 =
4581 				ycbcr_444_to_420 && !ycbcr_420_passthrough;
4582 
4583 			connector->base.ycbcr_420_allowed =
4584 				!is_branch || ycbcr_444_to_420 || ycbcr_420_passthrough;
4585 		}
4586 	} else {
4587 		/* 4:4:4->4:2:0 conversion is the only way */
4588 		intel_dp->dfp.ycbcr_444_to_420 = ycbcr_444_to_420;
4589 
4590 		connector->base.ycbcr_420_allowed = ycbcr_444_to_420;
4591 	}
4592 
4593 	drm_dbg_kms(&i915->drm,
4594 		    "[CONNECTOR:%d:%s] RGB->YcbCr conversion? %s, YCbCr 4:2:0 allowed? %s, YCbCr 4:4:4->4:2:0 conversion? %s\n",
4595 		    connector->base.base.id, connector->base.name,
4596 		    str_yes_no(intel_dp->dfp.rgb_to_ycbcr),
4597 		    str_yes_no(connector->base.ycbcr_420_allowed),
4598 		    str_yes_no(intel_dp->dfp.ycbcr_444_to_420));
4599 }
4600 
4601 static void
4602 intel_dp_set_edid(struct intel_dp *intel_dp)
4603 {
4604 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4605 	struct intel_connector *connector = intel_dp->attached_connector;
4606 	const struct drm_edid *drm_edid;
4607 	const struct edid *edid;
4608 	bool vrr_capable;
4609 
4610 	intel_dp_unset_edid(intel_dp);
4611 	drm_edid = intel_dp_get_edid(intel_dp);
4612 	connector->detect_edid = drm_edid;
4613 
4614 	/* Below we depend on display info having been updated */
4615 	drm_edid_connector_update(&connector->base, drm_edid);
4616 
4617 	vrr_capable = intel_vrr_is_capable(connector);
4618 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s] VRR capable: %s\n",
4619 		    connector->base.base.id, connector->base.name, str_yes_no(vrr_capable));
4620 	drm_connector_set_vrr_capable_property(&connector->base, vrr_capable);
4621 
4622 	intel_dp_update_dfp(intel_dp, drm_edid);
4623 	intel_dp_update_420(intel_dp);
4624 
4625 	/* FIXME: Get rid of drm_edid_raw() */
4626 	edid = drm_edid_raw(drm_edid);
4627 	if (edid && edid->input & DRM_EDID_INPUT_DIGITAL) {
4628 		intel_dp->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
4629 		intel_dp->has_audio = drm_detect_monitor_audio(edid);
4630 	}
4631 
4632 	drm_dp_cec_set_edid(&intel_dp->aux, edid);
4633 }
4634 
4635 static void
4636 intel_dp_unset_edid(struct intel_dp *intel_dp)
4637 {
4638 	struct intel_connector *connector = intel_dp->attached_connector;
4639 
4640 	drm_dp_cec_unset_edid(&intel_dp->aux);
4641 	drm_edid_free(connector->detect_edid);
4642 	connector->detect_edid = NULL;
4643 
4644 	intel_dp->has_hdmi_sink = false;
4645 	intel_dp->has_audio = false;
4646 
4647 	intel_dp->dfp.max_bpc = 0;
4648 	intel_dp->dfp.max_dotclock = 0;
4649 	intel_dp->dfp.min_tmds_clock = 0;
4650 	intel_dp->dfp.max_tmds_clock = 0;
4651 
4652 	intel_dp->dfp.pcon_max_frl_bw = 0;
4653 
4654 	intel_dp->dfp.ycbcr_444_to_420 = false;
4655 	connector->base.ycbcr_420_allowed = false;
4656 
4657 	drm_connector_set_vrr_capable_property(&connector->base,
4658 					       false);
4659 }
4660 
4661 static int
4662 intel_dp_detect(struct drm_connector *connector,
4663 		struct drm_modeset_acquire_ctx *ctx,
4664 		bool force)
4665 {
4666 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
4667 	struct intel_dp *intel_dp = intel_attached_dp(to_intel_connector(connector));
4668 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4669 	struct intel_encoder *encoder = &dig_port->base;
4670 	enum drm_connector_status status;
4671 
4672 	drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s]\n",
4673 		    connector->base.id, connector->name);
4674 	drm_WARN_ON(&dev_priv->drm,
4675 		    !drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex));
4676 
4677 	if (!INTEL_DISPLAY_ENABLED(dev_priv))
4678 		return connector_status_disconnected;
4679 
4680 	/* Can't disconnect eDP */
4681 	if (intel_dp_is_edp(intel_dp))
4682 		status = edp_detect(intel_dp);
4683 	else if (intel_digital_port_connected(encoder))
4684 		status = intel_dp_detect_dpcd(intel_dp);
4685 	else
4686 		status = connector_status_disconnected;
4687 
4688 	if (status == connector_status_disconnected) {
4689 		memset(&intel_dp->compliance, 0, sizeof(intel_dp->compliance));
4690 		memset(intel_dp->dsc_dpcd, 0, sizeof(intel_dp->dsc_dpcd));
4691 
4692 		if (intel_dp->is_mst) {
4693 			drm_dbg_kms(&dev_priv->drm,
4694 				    "MST device may have disappeared %d vs %d\n",
4695 				    intel_dp->is_mst,
4696 				    intel_dp->mst_mgr.mst_state);
4697 			intel_dp->is_mst = false;
4698 			drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr,
4699 							intel_dp->is_mst);
4700 		}
4701 
4702 		goto out;
4703 	}
4704 
4705 	/* Read DP Sink DSC Cap DPCD regs for DP v1.4 */
4706 	if (HAS_DSC(dev_priv))
4707 		intel_dp_get_dsc_sink_cap(intel_dp);
4708 
4709 	intel_dp_configure_mst(intel_dp);
4710 
4711 	/*
4712 	 * TODO: Reset link params when switching to MST mode, until MST
4713 	 * supports link training fallback params.
4714 	 */
4715 	if (intel_dp->reset_link_params || intel_dp->is_mst) {
4716 		intel_dp_reset_max_link_params(intel_dp);
4717 		intel_dp->reset_link_params = false;
4718 	}
4719 
4720 	intel_dp_print_rates(intel_dp);
4721 
4722 	if (intel_dp->is_mst) {
4723 		/*
4724 		 * If we are in MST mode then this connector
4725 		 * won't appear connected or have anything
4726 		 * with EDID on it
4727 		 */
4728 		status = connector_status_disconnected;
4729 		goto out;
4730 	}
4731 
4732 	/*
4733 	 * Some external monitors do not signal loss of link synchronization
4734 	 * with an IRQ_HPD, so force a link status check.
4735 	 */
4736 	if (!intel_dp_is_edp(intel_dp)) {
4737 		int ret;
4738 
4739 		ret = intel_dp_retrain_link(encoder, ctx);
4740 		if (ret)
4741 			return ret;
4742 	}
4743 
4744 	/*
4745 	 * Clearing NACK and defer counts to get their exact values
4746 	 * while reading EDID which are required by Compliance tests
4747 	 * 4.2.2.4 and 4.2.2.5
4748 	 */
4749 	intel_dp->aux.i2c_nack_count = 0;
4750 	intel_dp->aux.i2c_defer_count = 0;
4751 
4752 	intel_dp_set_edid(intel_dp);
4753 	if (intel_dp_is_edp(intel_dp) ||
4754 	    to_intel_connector(connector)->detect_edid)
4755 		status = connector_status_connected;
4756 
4757 	intel_dp_check_device_service_irq(intel_dp);
4758 
4759 out:
4760 	if (status != connector_status_connected && !intel_dp->is_mst)
4761 		intel_dp_unset_edid(intel_dp);
4762 
4763 	/*
4764 	 * Make sure the refs for power wells enabled during detect are
4765 	 * dropped to avoid a new detect cycle triggered by HPD polling.
4766 	 */
4767 	intel_display_power_flush_work(dev_priv);
4768 
4769 	if (!intel_dp_is_edp(intel_dp))
4770 		drm_dp_set_subconnector_property(connector,
4771 						 status,
4772 						 intel_dp->dpcd,
4773 						 intel_dp->downstream_ports);
4774 	return status;
4775 }
4776 
4777 static void
4778 intel_dp_force(struct drm_connector *connector)
4779 {
4780 	struct intel_dp *intel_dp = intel_attached_dp(to_intel_connector(connector));
4781 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4782 	struct intel_encoder *intel_encoder = &dig_port->base;
4783 	struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
4784 	enum intel_display_power_domain aux_domain =
4785 		intel_aux_power_domain(dig_port);
4786 	intel_wakeref_t wakeref;
4787 
4788 	drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s]\n",
4789 		    connector->base.id, connector->name);
4790 	intel_dp_unset_edid(intel_dp);
4791 
4792 	if (connector->status != connector_status_connected)
4793 		return;
4794 
4795 	wakeref = intel_display_power_get(dev_priv, aux_domain);
4796 
4797 	intel_dp_set_edid(intel_dp);
4798 
4799 	intel_display_power_put(dev_priv, aux_domain, wakeref);
4800 }
4801 
4802 static int intel_dp_get_modes(struct drm_connector *connector)
4803 {
4804 	struct intel_connector *intel_connector = to_intel_connector(connector);
4805 	int num_modes;
4806 
4807 	/* drm_edid_connector_update() done in ->detect() or ->force() */
4808 	num_modes = drm_edid_connector_add_modes(connector);
4809 
4810 	/* Also add fixed mode, which may or may not be present in EDID */
4811 	if (intel_dp_is_edp(intel_attached_dp(intel_connector)))
4812 		num_modes += intel_panel_get_modes(intel_connector);
4813 
4814 	if (num_modes)
4815 		return num_modes;
4816 
4817 	if (!intel_connector->detect_edid) {
4818 		struct intel_dp *intel_dp = intel_attached_dp(intel_connector);
4819 		struct drm_display_mode *mode;
4820 
4821 		mode = drm_dp_downstream_mode(connector->dev,
4822 					      intel_dp->dpcd,
4823 					      intel_dp->downstream_ports);
4824 		if (mode) {
4825 			drm_mode_probed_add(connector, mode);
4826 			num_modes++;
4827 		}
4828 	}
4829 
4830 	return num_modes;
4831 }
4832 
4833 static int
4834 intel_dp_connector_register(struct drm_connector *connector)
4835 {
4836 	struct drm_i915_private *i915 = to_i915(connector->dev);
4837 	struct intel_dp *intel_dp = intel_attached_dp(to_intel_connector(connector));
4838 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4839 	struct intel_lspcon *lspcon = &dig_port->lspcon;
4840 	int ret;
4841 
4842 	ret = intel_connector_register(connector);
4843 	if (ret)
4844 		return ret;
4845 
4846 	drm_dbg_kms(&i915->drm, "registering %s bus for %s\n",
4847 		    intel_dp->aux.name, connector->kdev->kobj.name);
4848 
4849 	intel_dp->aux.dev = connector->kdev;
4850 	ret = drm_dp_aux_register(&intel_dp->aux);
4851 	if (!ret)
4852 		drm_dp_cec_register_connector(&intel_dp->aux, connector);
4853 
4854 	if (!intel_bios_is_lspcon_present(i915, dig_port->base.port))
4855 		return ret;
4856 
4857 	/*
4858 	 * ToDo: Clean this up to handle lspcon init and resume more
4859 	 * efficiently and streamlined.
4860 	 */
4861 	if (lspcon_init(dig_port)) {
4862 		lspcon_detect_hdr_capability(lspcon);
4863 		if (lspcon->hdr_supported)
4864 			drm_connector_attach_hdr_output_metadata_property(connector);
4865 	}
4866 
4867 	return ret;
4868 }
4869 
4870 static void
4871 intel_dp_connector_unregister(struct drm_connector *connector)
4872 {
4873 	struct intel_dp *intel_dp = intel_attached_dp(to_intel_connector(connector));
4874 
4875 	drm_dp_cec_unregister_connector(&intel_dp->aux);
4876 	drm_dp_aux_unregister(&intel_dp->aux);
4877 	intel_connector_unregister(connector);
4878 }
4879 
4880 void intel_dp_encoder_flush_work(struct drm_encoder *encoder)
4881 {
4882 	struct intel_digital_port *dig_port = enc_to_dig_port(to_intel_encoder(encoder));
4883 	struct intel_dp *intel_dp = &dig_port->dp;
4884 
4885 	intel_dp_mst_encoder_cleanup(dig_port);
4886 
4887 	intel_pps_vdd_off_sync(intel_dp);
4888 
4889 	/*
4890 	 * Ensure power off delay is respected on module remove, so that we can
4891 	 * reduce delays at driver probe. See pps_init_timestamps().
4892 	 */
4893 	intel_pps_wait_power_cycle(intel_dp);
4894 
4895 	intel_dp_aux_fini(intel_dp);
4896 }
4897 
4898 void intel_dp_encoder_suspend(struct intel_encoder *intel_encoder)
4899 {
4900 	struct intel_dp *intel_dp = enc_to_intel_dp(intel_encoder);
4901 
4902 	intel_pps_vdd_off_sync(intel_dp);
4903 }
4904 
4905 void intel_dp_encoder_shutdown(struct intel_encoder *intel_encoder)
4906 {
4907 	struct intel_dp *intel_dp = enc_to_intel_dp(intel_encoder);
4908 
4909 	intel_pps_wait_power_cycle(intel_dp);
4910 }
4911 
4912 static int intel_modeset_tile_group(struct intel_atomic_state *state,
4913 				    int tile_group_id)
4914 {
4915 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
4916 	struct drm_connector_list_iter conn_iter;
4917 	struct drm_connector *connector;
4918 	int ret = 0;
4919 
4920 	drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
4921 	drm_for_each_connector_iter(connector, &conn_iter) {
4922 		struct drm_connector_state *conn_state;
4923 		struct intel_crtc_state *crtc_state;
4924 		struct intel_crtc *crtc;
4925 
4926 		if (!connector->has_tile ||
4927 		    connector->tile_group->id != tile_group_id)
4928 			continue;
4929 
4930 		conn_state = drm_atomic_get_connector_state(&state->base,
4931 							    connector);
4932 		if (IS_ERR(conn_state)) {
4933 			ret = PTR_ERR(conn_state);
4934 			break;
4935 		}
4936 
4937 		crtc = to_intel_crtc(conn_state->crtc);
4938 
4939 		if (!crtc)
4940 			continue;
4941 
4942 		crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
4943 		crtc_state->uapi.mode_changed = true;
4944 
4945 		ret = drm_atomic_add_affected_planes(&state->base, &crtc->base);
4946 		if (ret)
4947 			break;
4948 	}
4949 	drm_connector_list_iter_end(&conn_iter);
4950 
4951 	return ret;
4952 }
4953 
4954 static int intel_modeset_affected_transcoders(struct intel_atomic_state *state, u8 transcoders)
4955 {
4956 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
4957 	struct intel_crtc *crtc;
4958 
4959 	if (transcoders == 0)
4960 		return 0;
4961 
4962 	for_each_intel_crtc(&dev_priv->drm, crtc) {
4963 		struct intel_crtc_state *crtc_state;
4964 		int ret;
4965 
4966 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
4967 		if (IS_ERR(crtc_state))
4968 			return PTR_ERR(crtc_state);
4969 
4970 		if (!crtc_state->hw.enable)
4971 			continue;
4972 
4973 		if (!(transcoders & BIT(crtc_state->cpu_transcoder)))
4974 			continue;
4975 
4976 		crtc_state->uapi.mode_changed = true;
4977 
4978 		ret = drm_atomic_add_affected_connectors(&state->base, &crtc->base);
4979 		if (ret)
4980 			return ret;
4981 
4982 		ret = drm_atomic_add_affected_planes(&state->base, &crtc->base);
4983 		if (ret)
4984 			return ret;
4985 
4986 		transcoders &= ~BIT(crtc_state->cpu_transcoder);
4987 	}
4988 
4989 	drm_WARN_ON(&dev_priv->drm, transcoders != 0);
4990 
4991 	return 0;
4992 }
4993 
4994 static int intel_modeset_synced_crtcs(struct intel_atomic_state *state,
4995 				      struct drm_connector *connector)
4996 {
4997 	const struct drm_connector_state *old_conn_state =
4998 		drm_atomic_get_old_connector_state(&state->base, connector);
4999 	const struct intel_crtc_state *old_crtc_state;
5000 	struct intel_crtc *crtc;
5001 	u8 transcoders;
5002 
5003 	crtc = to_intel_crtc(old_conn_state->crtc);
5004 	if (!crtc)
5005 		return 0;
5006 
5007 	old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc);
5008 
5009 	if (!old_crtc_state->hw.active)
5010 		return 0;
5011 
5012 	transcoders = old_crtc_state->sync_mode_slaves_mask;
5013 	if (old_crtc_state->master_transcoder != INVALID_TRANSCODER)
5014 		transcoders |= BIT(old_crtc_state->master_transcoder);
5015 
5016 	return intel_modeset_affected_transcoders(state,
5017 						  transcoders);
5018 }
5019 
5020 static int intel_dp_connector_atomic_check(struct drm_connector *conn,
5021 					   struct drm_atomic_state *_state)
5022 {
5023 	struct drm_i915_private *dev_priv = to_i915(conn->dev);
5024 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
5025 	struct drm_connector_state *conn_state = drm_atomic_get_new_connector_state(_state, conn);
5026 	struct intel_connector *intel_conn = to_intel_connector(conn);
5027 	struct intel_dp *intel_dp = enc_to_intel_dp(intel_conn->encoder);
5028 	int ret;
5029 
5030 	ret = intel_digital_connector_atomic_check(conn, &state->base);
5031 	if (ret)
5032 		return ret;
5033 
5034 	if (intel_dp_mst_source_support(intel_dp)) {
5035 		ret = drm_dp_mst_root_conn_atomic_check(conn_state, &intel_dp->mst_mgr);
5036 		if (ret)
5037 			return ret;
5038 	}
5039 
5040 	/*
5041 	 * We don't enable port sync on BDW due to missing w/as and
5042 	 * due to not having adjusted the modeset sequence appropriately.
5043 	 */
5044 	if (DISPLAY_VER(dev_priv) < 9)
5045 		return 0;
5046 
5047 	if (!intel_connector_needs_modeset(state, conn))
5048 		return 0;
5049 
5050 	if (conn->has_tile) {
5051 		ret = intel_modeset_tile_group(state, conn->tile_group->id);
5052 		if (ret)
5053 			return ret;
5054 	}
5055 
5056 	return intel_modeset_synced_crtcs(state, conn);
5057 }
5058 
5059 static void intel_dp_oob_hotplug_event(struct drm_connector *connector)
5060 {
5061 	struct intel_encoder *encoder = intel_attached_encoder(to_intel_connector(connector));
5062 	struct drm_i915_private *i915 = to_i915(connector->dev);
5063 
5064 	spin_lock_irq(&i915->irq_lock);
5065 	i915->display.hotplug.event_bits |= BIT(encoder->hpd_pin);
5066 	spin_unlock_irq(&i915->irq_lock);
5067 	queue_delayed_work(system_wq, &i915->display.hotplug.hotplug_work, 0);
5068 }
5069 
5070 static const struct drm_connector_funcs intel_dp_connector_funcs = {
5071 	.force = intel_dp_force,
5072 	.fill_modes = drm_helper_probe_single_connector_modes,
5073 	.atomic_get_property = intel_digital_connector_atomic_get_property,
5074 	.atomic_set_property = intel_digital_connector_atomic_set_property,
5075 	.late_register = intel_dp_connector_register,
5076 	.early_unregister = intel_dp_connector_unregister,
5077 	.destroy = intel_connector_destroy,
5078 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
5079 	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
5080 	.oob_hotplug_event = intel_dp_oob_hotplug_event,
5081 };
5082 
5083 static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
5084 	.detect_ctx = intel_dp_detect,
5085 	.get_modes = intel_dp_get_modes,
5086 	.mode_valid = intel_dp_mode_valid,
5087 	.atomic_check = intel_dp_connector_atomic_check,
5088 };
5089 
5090 enum irqreturn
5091 intel_dp_hpd_pulse(struct intel_digital_port *dig_port, bool long_hpd)
5092 {
5093 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
5094 	struct intel_dp *intel_dp = &dig_port->dp;
5095 
5096 	if (dig_port->base.type == INTEL_OUTPUT_EDP &&
5097 	    (long_hpd || !intel_pps_have_panel_power_or_vdd(intel_dp))) {
5098 		/*
5099 		 * vdd off can generate a long/short pulse on eDP which
5100 		 * would require vdd on to handle it, and thus we
5101 		 * would end up in an endless cycle of
5102 		 * "vdd off -> long/short hpd -> vdd on -> detect -> vdd off -> ..."
5103 		 */
5104 		drm_dbg_kms(&i915->drm,
5105 			    "ignoring %s hpd on eDP [ENCODER:%d:%s]\n",
5106 			    long_hpd ? "long" : "short",
5107 			    dig_port->base.base.base.id,
5108 			    dig_port->base.base.name);
5109 		return IRQ_HANDLED;
5110 	}
5111 
5112 	drm_dbg_kms(&i915->drm, "got hpd irq on [ENCODER:%d:%s] - %s\n",
5113 		    dig_port->base.base.base.id,
5114 		    dig_port->base.base.name,
5115 		    long_hpd ? "long" : "short");
5116 
5117 	if (long_hpd) {
5118 		intel_dp->reset_link_params = true;
5119 		return IRQ_NONE;
5120 	}
5121 
5122 	if (intel_dp->is_mst) {
5123 		if (!intel_dp_check_mst_status(intel_dp))
5124 			return IRQ_NONE;
5125 	} else if (!intel_dp_short_pulse(intel_dp)) {
5126 		return IRQ_NONE;
5127 	}
5128 
5129 	return IRQ_HANDLED;
5130 }
5131 
5132 /* check the VBT to see whether the eDP is on another port */
5133 bool intel_dp_is_port_edp(struct drm_i915_private *dev_priv, enum port port)
5134 {
5135 	/*
5136 	 * eDP not supported on g4x. so bail out early just
5137 	 * for a bit extra safety in case the VBT is bonkers.
5138 	 */
5139 	if (DISPLAY_VER(dev_priv) < 5)
5140 		return false;
5141 
5142 	if (DISPLAY_VER(dev_priv) < 9 && port == PORT_A)
5143 		return true;
5144 
5145 	return intel_bios_is_port_edp(dev_priv, port);
5146 }
5147 
5148 static bool
5149 has_gamut_metadata_dip(struct drm_i915_private *i915, enum port port)
5150 {
5151 	if (intel_bios_is_lspcon_present(i915, port))
5152 		return false;
5153 
5154 	if (DISPLAY_VER(i915) >= 11)
5155 		return true;
5156 
5157 	if (port == PORT_A)
5158 		return false;
5159 
5160 	if (IS_HASWELL(i915) || IS_BROADWELL(i915) ||
5161 	    DISPLAY_VER(i915) >= 9)
5162 		return true;
5163 
5164 	return false;
5165 }
5166 
5167 static void
5168 intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
5169 {
5170 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
5171 	enum port port = dp_to_dig_port(intel_dp)->base.port;
5172 
5173 	if (!intel_dp_is_edp(intel_dp))
5174 		drm_connector_attach_dp_subconnector_property(connector);
5175 
5176 	if (!IS_G4X(dev_priv) && port != PORT_A)
5177 		intel_attach_force_audio_property(connector);
5178 
5179 	intel_attach_broadcast_rgb_property(connector);
5180 	if (HAS_GMCH(dev_priv))
5181 		drm_connector_attach_max_bpc_property(connector, 6, 10);
5182 	else if (DISPLAY_VER(dev_priv) >= 5)
5183 		drm_connector_attach_max_bpc_property(connector, 6, 12);
5184 
5185 	/* Register HDMI colorspace for case of lspcon */
5186 	if (intel_bios_is_lspcon_present(dev_priv, port)) {
5187 		drm_connector_attach_content_type_property(connector);
5188 		intel_attach_hdmi_colorspace_property(connector);
5189 	} else {
5190 		intel_attach_dp_colorspace_property(connector);
5191 	}
5192 
5193 	if (has_gamut_metadata_dip(dev_priv, port))
5194 		drm_connector_attach_hdr_output_metadata_property(connector);
5195 
5196 	if (HAS_VRR(dev_priv))
5197 		drm_connector_attach_vrr_capable_property(connector);
5198 }
5199 
5200 static void
5201 intel_edp_add_properties(struct intel_dp *intel_dp)
5202 {
5203 	struct intel_connector *connector = intel_dp->attached_connector;
5204 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
5205 	const struct drm_display_mode *fixed_mode =
5206 		intel_panel_preferred_fixed_mode(connector);
5207 
5208 	intel_attach_scaling_mode_property(&connector->base);
5209 
5210 	drm_connector_set_panel_orientation_with_quirk(&connector->base,
5211 						       i915->display.vbt.orientation,
5212 						       fixed_mode->hdisplay,
5213 						       fixed_mode->vdisplay);
5214 }
5215 
5216 static void intel_edp_backlight_setup(struct intel_dp *intel_dp,
5217 				      struct intel_connector *connector)
5218 {
5219 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
5220 	enum pipe pipe = INVALID_PIPE;
5221 
5222 	if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915)) {
5223 		/*
5224 		 * Figure out the current pipe for the initial backlight setup.
5225 		 * If the current pipe isn't valid, try the PPS pipe, and if that
5226 		 * fails just assume pipe A.
5227 		 */
5228 		pipe = vlv_active_pipe(intel_dp);
5229 
5230 		if (pipe != PIPE_A && pipe != PIPE_B)
5231 			pipe = intel_dp->pps.pps_pipe;
5232 
5233 		if (pipe != PIPE_A && pipe != PIPE_B)
5234 			pipe = PIPE_A;
5235 
5236 		drm_dbg_kms(&i915->drm,
5237 			    "[CONNECTOR:%d:%s] using pipe %c for initial backlight setup\n",
5238 			    connector->base.base.id, connector->base.name,
5239 			    pipe_name(pipe));
5240 	}
5241 
5242 	intel_backlight_setup(connector, pipe);
5243 }
5244 
5245 static bool intel_edp_init_connector(struct intel_dp *intel_dp,
5246 				     struct intel_connector *intel_connector)
5247 {
5248 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
5249 	struct drm_connector *connector = &intel_connector->base;
5250 	struct drm_display_mode *fixed_mode;
5251 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
5252 	bool has_dpcd;
5253 	const struct drm_edid *drm_edid;
5254 
5255 	if (!intel_dp_is_edp(intel_dp))
5256 		return true;
5257 
5258 	/*
5259 	 * On IBX/CPT we may get here with LVDS already registered. Since the
5260 	 * driver uses the only internal power sequencer available for both
5261 	 * eDP and LVDS bail out early in this case to prevent interfering
5262 	 * with an already powered-on LVDS power sequencer.
5263 	 */
5264 	if (intel_get_lvds_encoder(dev_priv)) {
5265 		drm_WARN_ON(&dev_priv->drm,
5266 			    !(HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)));
5267 		drm_info(&dev_priv->drm,
5268 			 "LVDS was detected, not registering eDP\n");
5269 
5270 		return false;
5271 	}
5272 
5273 	intel_bios_init_panel_early(dev_priv, &intel_connector->panel,
5274 				    encoder->devdata);
5275 
5276 	if (!intel_pps_init(intel_dp)) {
5277 		drm_info(&dev_priv->drm,
5278 			 "[ENCODER:%d:%s] unusable PPS, disabling eDP\n",
5279 			 encoder->base.base.id, encoder->base.name);
5280 		/*
5281 		 * The BIOS may have still enabled VDD on the PPS even
5282 		 * though it's unusable. Make sure we turn it back off
5283 		 * and to release the power domain references/etc.
5284 		 */
5285 		goto out_vdd_off;
5286 	}
5287 
5288 	/* Cache DPCD and EDID for edp. */
5289 	has_dpcd = intel_edp_init_dpcd(intel_dp);
5290 
5291 	if (!has_dpcd) {
5292 		/* if this fails, presume the device is a ghost */
5293 		drm_info(&dev_priv->drm,
5294 			 "[ENCODER:%d:%s] failed to retrieve link info, disabling eDP\n",
5295 			 encoder->base.base.id, encoder->base.name);
5296 		goto out_vdd_off;
5297 	}
5298 
5299 	mutex_lock(&dev_priv->drm.mode_config.mutex);
5300 	drm_edid = drm_edid_read_ddc(connector, &intel_dp->aux.ddc);
5301 	if (!drm_edid) {
5302 		/* Fallback to EDID from ACPI OpRegion, if any */
5303 		drm_edid = intel_opregion_get_edid(intel_connector);
5304 		if (drm_edid)
5305 			drm_dbg_kms(&dev_priv->drm,
5306 				    "[CONNECTOR:%d:%s] Using OpRegion EDID\n",
5307 				    connector->base.id, connector->name);
5308 	}
5309 	if (drm_edid) {
5310 		if (drm_edid_connector_update(connector, drm_edid) ||
5311 		    !drm_edid_connector_add_modes(connector)) {
5312 			drm_edid_connector_update(connector, NULL);
5313 			drm_edid_free(drm_edid);
5314 			drm_edid = ERR_PTR(-EINVAL);
5315 		}
5316 	} else {
5317 		drm_edid = ERR_PTR(-ENOENT);
5318 	}
5319 
5320 	intel_bios_init_panel_late(dev_priv, &intel_connector->panel, encoder->devdata,
5321 				   IS_ERR(drm_edid) ? NULL : drm_edid);
5322 
5323 	intel_panel_add_edid_fixed_modes(intel_connector, true);
5324 
5325 	/* MSO requires information from the EDID */
5326 	intel_edp_mso_init(intel_dp);
5327 
5328 	/* multiply the mode clock and horizontal timings for MSO */
5329 	list_for_each_entry(fixed_mode, &intel_connector->panel.fixed_modes, head)
5330 		intel_edp_mso_mode_fixup(intel_connector, fixed_mode);
5331 
5332 	/* fallback to VBT if available for eDP */
5333 	if (!intel_panel_preferred_fixed_mode(intel_connector))
5334 		intel_panel_add_vbt_lfp_fixed_mode(intel_connector);
5335 
5336 	mutex_unlock(&dev_priv->drm.mode_config.mutex);
5337 
5338 	if (!intel_panel_preferred_fixed_mode(intel_connector)) {
5339 		drm_info(&dev_priv->drm,
5340 			 "[ENCODER:%d:%s] failed to find fixed mode for the panel, disabling eDP\n",
5341 			 encoder->base.base.id, encoder->base.name);
5342 		goto out_vdd_off;
5343 	}
5344 
5345 	intel_panel_init(intel_connector, drm_edid);
5346 
5347 	intel_edp_backlight_setup(intel_dp, intel_connector);
5348 
5349 	intel_edp_add_properties(intel_dp);
5350 
5351 	intel_pps_init_late(intel_dp);
5352 
5353 	return true;
5354 
5355 out_vdd_off:
5356 	intel_pps_vdd_off_sync(intel_dp);
5357 
5358 	return false;
5359 }
5360 
5361 static void intel_dp_modeset_retry_work_fn(struct work_struct *work)
5362 {
5363 	struct intel_connector *intel_connector;
5364 	struct drm_connector *connector;
5365 
5366 	intel_connector = container_of(work, typeof(*intel_connector),
5367 				       modeset_retry_work);
5368 	connector = &intel_connector->base;
5369 	drm_dbg_kms(connector->dev, "[CONNECTOR:%d:%s]\n", connector->base.id,
5370 		    connector->name);
5371 
5372 	/* Grab the locks before changing connector property*/
5373 	mutex_lock(&connector->dev->mode_config.mutex);
5374 	/* Set connector link status to BAD and send a Uevent to notify
5375 	 * userspace to do a modeset.
5376 	 */
5377 	drm_connector_set_link_status_property(connector,
5378 					       DRM_MODE_LINK_STATUS_BAD);
5379 	mutex_unlock(&connector->dev->mode_config.mutex);
5380 	/* Send Hotplug uevent so userspace can reprobe */
5381 	drm_kms_helper_connector_hotplug_event(connector);
5382 }
5383 
5384 bool
5385 intel_dp_init_connector(struct intel_digital_port *dig_port,
5386 			struct intel_connector *intel_connector)
5387 {
5388 	struct drm_connector *connector = &intel_connector->base;
5389 	struct intel_dp *intel_dp = &dig_port->dp;
5390 	struct intel_encoder *intel_encoder = &dig_port->base;
5391 	struct drm_device *dev = intel_encoder->base.dev;
5392 	struct drm_i915_private *dev_priv = to_i915(dev);
5393 	enum port port = intel_encoder->port;
5394 	enum phy phy = intel_port_to_phy(dev_priv, port);
5395 	int type;
5396 
5397 	/* Initialize the work for modeset in case of link train failure */
5398 	INIT_WORK(&intel_connector->modeset_retry_work,
5399 		  intel_dp_modeset_retry_work_fn);
5400 
5401 	if (drm_WARN(dev, dig_port->max_lanes < 1,
5402 		     "Not enough lanes (%d) for DP on [ENCODER:%d:%s]\n",
5403 		     dig_port->max_lanes, intel_encoder->base.base.id,
5404 		     intel_encoder->base.name))
5405 		return false;
5406 
5407 	intel_dp->reset_link_params = true;
5408 	intel_dp->pps.pps_pipe = INVALID_PIPE;
5409 	intel_dp->pps.active_pipe = INVALID_PIPE;
5410 
5411 	/* Preserve the current hw state. */
5412 	intel_dp->DP = intel_de_read(dev_priv, intel_dp->output_reg);
5413 	intel_dp->attached_connector = intel_connector;
5414 
5415 	if (intel_dp_is_port_edp(dev_priv, port)) {
5416 		/*
5417 		 * Currently we don't support eDP on TypeC ports, although in
5418 		 * theory it could work on TypeC legacy ports.
5419 		 */
5420 		drm_WARN_ON(dev, intel_phy_is_tc(dev_priv, phy));
5421 		type = DRM_MODE_CONNECTOR_eDP;
5422 		intel_encoder->type = INTEL_OUTPUT_EDP;
5423 
5424 		/* eDP only on port B and/or C on vlv/chv */
5425 		if (drm_WARN_ON(dev, (IS_VALLEYVIEW(dev_priv) ||
5426 				      IS_CHERRYVIEW(dev_priv)) &&
5427 				port != PORT_B && port != PORT_C))
5428 			return false;
5429 	} else {
5430 		type = DRM_MODE_CONNECTOR_DisplayPort;
5431 	}
5432 
5433 	intel_dp_set_default_sink_rates(intel_dp);
5434 	intel_dp_set_default_max_sink_lane_count(intel_dp);
5435 
5436 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
5437 		intel_dp->pps.active_pipe = vlv_active_pipe(intel_dp);
5438 
5439 	drm_dbg_kms(&dev_priv->drm,
5440 		    "Adding %s connector on [ENCODER:%d:%s]\n",
5441 		    type == DRM_MODE_CONNECTOR_eDP ? "eDP" : "DP",
5442 		    intel_encoder->base.base.id, intel_encoder->base.name);
5443 
5444 	drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
5445 	drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
5446 
5447 	if (!HAS_GMCH(dev_priv) && DISPLAY_VER(dev_priv) < 12)
5448 		connector->interlace_allowed = true;
5449 
5450 	intel_connector->polled = DRM_CONNECTOR_POLL_HPD;
5451 
5452 	intel_dp_aux_init(intel_dp);
5453 
5454 	intel_connector_attach_encoder(intel_connector, intel_encoder);
5455 
5456 	if (HAS_DDI(dev_priv))
5457 		intel_connector->get_hw_state = intel_ddi_connector_get_hw_state;
5458 	else
5459 		intel_connector->get_hw_state = intel_connector_get_hw_state;
5460 
5461 	if (!intel_edp_init_connector(intel_dp, intel_connector)) {
5462 		intel_dp_aux_fini(intel_dp);
5463 		goto fail;
5464 	}
5465 
5466 	intel_dp_set_source_rates(intel_dp);
5467 	intel_dp_set_common_rates(intel_dp);
5468 	intel_dp_reset_max_link_params(intel_dp);
5469 
5470 	/* init MST on ports that can support it */
5471 	intel_dp_mst_encoder_init(dig_port,
5472 				  intel_connector->base.base.id);
5473 
5474 	intel_dp_add_properties(intel_dp, connector);
5475 
5476 	if (is_hdcp_supported(dev_priv, port) && !intel_dp_is_edp(intel_dp)) {
5477 		int ret = intel_dp_hdcp_init(dig_port, intel_connector);
5478 		if (ret)
5479 			drm_dbg_kms(&dev_priv->drm,
5480 				    "HDCP init failed, skipping.\n");
5481 	}
5482 
5483 	/* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
5484 	 * 0xd.  Failure to do so will result in spurious interrupts being
5485 	 * generated on the port when a cable is not attached.
5486 	 */
5487 	if (IS_G45(dev_priv)) {
5488 		u32 temp = intel_de_read(dev_priv, PEG_BAND_GAP_DATA);
5489 		intel_de_write(dev_priv, PEG_BAND_GAP_DATA,
5490 			       (temp & ~0xf) | 0xd);
5491 	}
5492 
5493 	intel_dp->frl.is_trained = false;
5494 	intel_dp->frl.trained_rate_gbps = 0;
5495 
5496 	intel_psr_init(intel_dp);
5497 
5498 	return true;
5499 
5500 fail:
5501 	drm_connector_cleanup(connector);
5502 
5503 	return false;
5504 }
5505 
5506 void intel_dp_mst_suspend(struct drm_i915_private *dev_priv)
5507 {
5508 	struct intel_encoder *encoder;
5509 
5510 	if (!HAS_DISPLAY(dev_priv))
5511 		return;
5512 
5513 	for_each_intel_encoder(&dev_priv->drm, encoder) {
5514 		struct intel_dp *intel_dp;
5515 
5516 		if (encoder->type != INTEL_OUTPUT_DDI)
5517 			continue;
5518 
5519 		intel_dp = enc_to_intel_dp(encoder);
5520 
5521 		if (!intel_dp_mst_source_support(intel_dp))
5522 			continue;
5523 
5524 		if (intel_dp->is_mst)
5525 			drm_dp_mst_topology_mgr_suspend(&intel_dp->mst_mgr);
5526 	}
5527 }
5528 
5529 void intel_dp_mst_resume(struct drm_i915_private *dev_priv)
5530 {
5531 	struct intel_encoder *encoder;
5532 
5533 	if (!HAS_DISPLAY(dev_priv))
5534 		return;
5535 
5536 	for_each_intel_encoder(&dev_priv->drm, encoder) {
5537 		struct intel_dp *intel_dp;
5538 		int ret;
5539 
5540 		if (encoder->type != INTEL_OUTPUT_DDI)
5541 			continue;
5542 
5543 		intel_dp = enc_to_intel_dp(encoder);
5544 
5545 		if (!intel_dp_mst_source_support(intel_dp))
5546 			continue;
5547 
5548 		ret = drm_dp_mst_topology_mgr_resume(&intel_dp->mst_mgr,
5549 						     true);
5550 		if (ret) {
5551 			intel_dp->is_mst = false;
5552 			drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr,
5553 							false);
5554 		}
5555 	}
5556 }
5557