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