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