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