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