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