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