xref: /linux/drivers/gpu/drm/i915/display/intel_dp.c (revision 5c9deba5578db5a3ea05be8a3e4a59ecb08ee76f)
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 u16 intel_dp_dsc_max_delta_bppx16(const struct intel_connector *connector,
2049 					 enum intel_output_format output_format)
2050 {
2051 	const u8 *dsc_dpcd = connector->dp.dsc_dpcd;
2052 	u8 max_bpp_delta_v1 = dsc_dpcd[DP_DSC_MAX_BPP_DELTA_VERSION_1 - DP_DSC_SUPPORT];
2053 	int max_bpp;
2054 
2055 	if (!(dsc_dpcd[DP_DSC_MAX_BITS_PER_PIXEL_HI - DP_DSC_SUPPORT] &
2056 	    DP_DSC_MAX_BPP_DELTA_AVAILABILITY))
2057 		return 0;
2058 
2059 	switch (output_format) {
2060 	case INTEL_OUTPUT_FORMAT_RGB:
2061 	case INTEL_OUTPUT_FORMAT_YCBCR444:
2062 		max_bpp =  max_bpp_delta_v1 & DP_DSC_RGB_YCbCr444_MAX_BPP_DELTA_MASK;
2063 		if (max_bpp >= 1 && max_bpp <= 21)
2064 			max_bpp =  max_bpp + DP_DSC_BPP_DELTA_444 - 1;
2065 		else
2066 			max_bpp = 0;
2067 		break;
2068 	case INTEL_OUTPUT_FORMAT_YCBCR420:
2069 		max_bpp = (max_bpp_delta_v1 & DP_DSC_NATIVE_YCbCr420_MAX_BPP_DELTA_MASK) >>
2070 			  DP_DSC_BPP_DELTA_SHIFT_420;
2071 		if (max_bpp >= 1 && max_bpp <= 7)
2072 			max_bpp = max_bpp + DP_DSC_BPP_DELTA_420 - 1;
2073 		break;
2074 	default:
2075 		MISSING_CASE(output_format);
2076 		return 0;
2077 	}
2078 
2079 	return max_bpp << 4;
2080 }
2081 
2082 static
2083 u16 intel_dp_dsc_max_sink_compressed_bppx16(const struct intel_connector *connector,
2084 					    enum intel_output_format output_format,
2085 					    int bpc)
2086 {
2087 	u16 max_bppx16 = intel_dp_dsc_max_delta_bppx16(connector, output_format);
2088 
2089 	if (max_bppx16)
2090 		return max_bppx16;
2091 
2092 	max_bppx16 = drm_edp_dsc_sink_output_bpp(connector->dp.dsc_dpcd);
2093 
2094 	if (max_bppx16)
2095 		return max_bppx16;
2096 	/*
2097 	 * If support not given in DPCD 67h, 68h, 6Eh, 6Fh use the Maximum Allowed bit rate
2098 	 * values as given in spec Table 2-157 DP v2.0
2099 	 */
2100 	switch (output_format) {
2101 	case INTEL_OUTPUT_FORMAT_RGB:
2102 	case INTEL_OUTPUT_FORMAT_YCBCR444:
2103 		return (3 * bpc) << 4;
2104 	case INTEL_OUTPUT_FORMAT_YCBCR420:
2105 		return (3 * (bpc / 2)) << 4;
2106 	default:
2107 		MISSING_CASE(output_format);
2108 		break;
2109 	}
2110 
2111 	return 0;
2112 }
2113 
2114 static int intel_dp_dsc_sink_min_compressed_bpp(enum intel_output_format output_format)
2115 {
2116 	/* From Mandatory bit rate range Support Table 2-157 (DP v2.0) */
2117 	switch (output_format) {
2118 	case INTEL_OUTPUT_FORMAT_RGB:
2119 	case INTEL_OUTPUT_FORMAT_YCBCR444:
2120 		return 8;
2121 	case INTEL_OUTPUT_FORMAT_YCBCR420:
2122 		return 6;
2123 	default:
2124 		MISSING_CASE(output_format);
2125 		break;
2126 	}
2127 
2128 	return 0;
2129 }
2130 
2131 static int intel_dp_dsc_sink_max_compressed_bpp(const struct intel_connector *connector,
2132 						enum intel_output_format output_format,
2133 						int bpc)
2134 {
2135 	return intel_dp_dsc_max_sink_compressed_bppx16(connector,
2136 						       output_format, bpc) >> 4;
2137 }
2138 
2139 int intel_dp_dsc_min_src_compressed_bpp(void)
2140 {
2141 	/* Min Compressed bpp supported by source is 8 */
2142 	return 8;
2143 }
2144 
2145 static int dsc_src_max_compressed_bpp(struct intel_dp *intel_dp)
2146 {
2147 	struct intel_display *display = to_intel_display(intel_dp);
2148 
2149 	/*
2150 	 * Forcing DSC and using the platform's max compressed bpp is seen to cause
2151 	 * underruns. Since DSC isn't needed in these cases, limit the
2152 	 * max compressed bpp to 18, which is a safe value across platforms with different
2153 	 * pipe bpps.
2154 	 */
2155 	if (intel_dp->force_dsc_en)
2156 		return 18;
2157 
2158 	/*
2159 	 * Max Compressed bpp for Gen 13+ is 27bpp.
2160 	 * For earlier platform is 23bpp. (Bspec:49259).
2161 	 */
2162 	if (DISPLAY_VER(display) < 13)
2163 		return 23;
2164 	else
2165 		return 27;
2166 }
2167 
2168 /*
2169  * Note: for pre-13 display you still need to check the validity of each step.
2170  */
2171 int intel_dp_dsc_bpp_step_x16(const struct intel_connector *connector)
2172 {
2173 	struct intel_display *display = to_intel_display(connector);
2174 	u8 incr = drm_dp_dsc_sink_bpp_incr(connector->dp.dsc_dpcd);
2175 
2176 	if (DISPLAY_VER(display) < 14 || !incr)
2177 		return fxp_q4_from_int(1);
2178 
2179 	if (connector->mst.dp &&
2180 	    !connector->link.force_bpp_x16 && !connector->mst.dp->force_dsc_fractional_bpp_en)
2181 		return fxp_q4_from_int(1);
2182 
2183 	/* fxp q4 */
2184 	return fxp_q4_from_int(1) / incr;
2185 }
2186 
2187 /*
2188  * Note: for bpp_x16 to be valid it must be also within the source/sink's
2189  * min..max bpp capability range.
2190  */
2191 bool intel_dp_dsc_valid_compressed_bpp(struct intel_dp *intel_dp, int bpp_x16)
2192 {
2193 	struct intel_display *display = to_intel_display(intel_dp);
2194 
2195 	if (DISPLAY_VER(display) >= 13) {
2196 		if (intel_dp->force_dsc_fractional_bpp_en && !fxp_q4_to_frac(bpp_x16))
2197 			return false;
2198 
2199 		return true;
2200 	}
2201 
2202 	if (fxp_q4_to_frac(bpp_x16))
2203 		return false;
2204 
2205 	return align_max_vesa_compressed_bpp_x16(bpp_x16) == bpp_x16;
2206 }
2207 
2208 static int align_min_compressed_bpp_x16(const struct intel_connector *connector, int min_bpp_x16)
2209 {
2210 	struct intel_display *display = to_intel_display(connector);
2211 
2212 	if (DISPLAY_VER(display) >= 13) {
2213 		int bpp_step_x16 = intel_dp_dsc_bpp_step_x16(connector);
2214 
2215 		drm_WARN_ON(display->drm, !is_power_of_2(bpp_step_x16));
2216 
2217 		return round_up(min_bpp_x16, bpp_step_x16);
2218 	} else {
2219 		return align_min_vesa_compressed_bpp_x16(min_bpp_x16);
2220 	}
2221 }
2222 
2223 static int align_max_compressed_bpp_x16(const struct intel_connector *connector,
2224 					enum intel_output_format output_format,
2225 					int pipe_bpp, int max_bpp_x16)
2226 {
2227 	struct intel_display *display = to_intel_display(connector);
2228 	int link_bpp_x16 = intel_dp_output_format_link_bpp_x16(output_format, pipe_bpp);
2229 	int bpp_step_x16 = intel_dp_dsc_bpp_step_x16(connector);
2230 
2231 	max_bpp_x16 = min(max_bpp_x16, link_bpp_x16 - bpp_step_x16);
2232 
2233 	if (DISPLAY_VER(display) >= 13) {
2234 		drm_WARN_ON(display->drm, !is_power_of_2(bpp_step_x16));
2235 
2236 		return round_down(max_bpp_x16, bpp_step_x16);
2237 	} else {
2238 		return align_max_vesa_compressed_bpp_x16(max_bpp_x16);
2239 	}
2240 }
2241 
2242 /*
2243  * Find the max compressed BPP we can find a link configuration for. The BPPs to
2244  * try depend on the source (platform) and sink.
2245  */
2246 static int dsc_compute_compressed_bpp(struct intel_dp *intel_dp,
2247 				      struct intel_crtc_state *pipe_config,
2248 				      struct drm_connector_state *conn_state,
2249 				      const struct link_config_limits *limits,
2250 				      int pipe_bpp)
2251 {
2252 	struct intel_display *display = to_intel_display(intel_dp);
2253 	const struct intel_connector *connector = to_intel_connector(conn_state->connector);
2254 	int min_bpp_x16, max_bpp_x16, bpp_step_x16;
2255 	int bpp_x16;
2256 	int ret;
2257 
2258 	min_bpp_x16 = limits->link.min_bpp_x16;
2259 	max_bpp_x16 = limits->link.max_bpp_x16;
2260 	bpp_step_x16 = intel_dp_dsc_bpp_step_x16(connector);
2261 
2262 	max_bpp_x16 = align_max_compressed_bpp_x16(connector, pipe_config->output_format,
2263 						   pipe_bpp, max_bpp_x16);
2264 	if (intel_dp_is_edp(intel_dp)) {
2265 		pipe_config->port_clock = limits->max_rate;
2266 		pipe_config->lane_count = limits->max_lane_count;
2267 
2268 		pipe_config->dsc.compressed_bpp_x16 = max_bpp_x16;
2269 
2270 		return 0;
2271 	}
2272 
2273 	for (bpp_x16 = max_bpp_x16; bpp_x16 >= min_bpp_x16; bpp_x16 -= bpp_step_x16) {
2274 		if (!intel_dp_dsc_valid_compressed_bpp(intel_dp, bpp_x16))
2275 			continue;
2276 
2277 		ret = dsc_compute_link_config(intel_dp,
2278 					      pipe_config,
2279 					      conn_state,
2280 					      limits,
2281 					      bpp_x16);
2282 		if (ret == 0) {
2283 			pipe_config->dsc.compressed_bpp_x16 = bpp_x16;
2284 			if (intel_dp->force_dsc_fractional_bpp_en &&
2285 			    fxp_q4_to_frac(bpp_x16))
2286 				drm_dbg_kms(display->drm,
2287 					    "Forcing DSC fractional bpp\n");
2288 
2289 			return 0;
2290 		}
2291 	}
2292 
2293 	return -EINVAL;
2294 }
2295 
2296 int intel_dp_dsc_min_src_input_bpc(void)
2297 {
2298 	/* Min DSC Input BPC for ICL+ is 8 */
2299 	return 8;
2300 }
2301 
2302 static
2303 bool is_dsc_pipe_bpp_sufficient(const struct link_config_limits *limits,
2304 				int pipe_bpp)
2305 {
2306 	return pipe_bpp >= limits->pipe.min_bpp &&
2307 	       pipe_bpp <= limits->pipe.max_bpp;
2308 }
2309 
2310 static
2311 int intel_dp_force_dsc_pipe_bpp(struct intel_dp *intel_dp,
2312 				const struct link_config_limits *limits)
2313 {
2314 	struct intel_display *display = to_intel_display(intel_dp);
2315 	int forced_bpp;
2316 
2317 	if (!intel_dp->force_dsc_bpc)
2318 		return 0;
2319 
2320 	forced_bpp = intel_dp->force_dsc_bpc * 3;
2321 
2322 	if (is_dsc_pipe_bpp_sufficient(limits, forced_bpp)) {
2323 		drm_dbg_kms(display->drm, "Input DSC BPC forced to %d\n",
2324 			    intel_dp->force_dsc_bpc);
2325 		return forced_bpp;
2326 	}
2327 
2328 	drm_dbg_kms(display->drm,
2329 		    "Cannot force DSC BPC:%d, due to DSC BPC limits\n",
2330 		    intel_dp->force_dsc_bpc);
2331 
2332 	return 0;
2333 }
2334 
2335 static int intel_dp_dsc_compute_pipe_bpp(struct intel_dp *intel_dp,
2336 					 struct intel_crtc_state *pipe_config,
2337 					 struct drm_connector_state *conn_state,
2338 					 const struct link_config_limits *limits)
2339 {
2340 	int forced_bpp, pipe_bpp;
2341 	int ret;
2342 
2343 	forced_bpp = intel_dp_force_dsc_pipe_bpp(intel_dp, limits);
2344 	if (forced_bpp)
2345 		pipe_bpp = forced_bpp;
2346 	else
2347 		pipe_bpp = limits->pipe.max_bpp;
2348 
2349 	ret = dsc_compute_compressed_bpp(intel_dp, pipe_config, conn_state,
2350 					 limits, pipe_bpp);
2351 	if (ret)
2352 		return -EINVAL;
2353 
2354 	pipe_config->pipe_bpp = pipe_bpp;
2355 
2356 	return 0;
2357 }
2358 
2359 /*
2360  * Return whether FEC must be enabled for 8b10b SST or MST links. On 128b132b
2361  * links FEC is always enabled implicitly by the HW, so this function returns
2362  * false for that case.
2363  */
2364 bool intel_dp_needs_8b10b_fec(const struct intel_crtc_state *crtc_state,
2365 			      bool dsc_enabled_on_crtc)
2366 {
2367 	if (intel_dp_is_uhbr(crtc_state))
2368 		return false;
2369 
2370 	/*
2371 	 * Though eDP v1.5 supports FEC with DSC, unlike DP, it is optional.
2372 	 * Since, FEC is a bandwidth overhead, continue to not enable it for
2373 	 * eDP. Until, there is a good reason to do so.
2374 	 */
2375 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP))
2376 		return false;
2377 
2378 	return dsc_enabled_on_crtc || intel_dsc_enabled_on_link(crtc_state);
2379 }
2380 
2381 void intel_dp_dsc_reset_config(struct intel_crtc_state *crtc_state)
2382 {
2383 	crtc_state->fec_enable = false;
2384 
2385 	crtc_state->dsc.compression_enable = false;
2386 	crtc_state->dsc.compressed_bpp_x16 = 0;
2387 
2388 	memset(&crtc_state->dsc.slice_config, 0, sizeof(crtc_state->dsc.slice_config));
2389 	memset(&crtc_state->dsc.config, 0, sizeof(crtc_state->dsc.config));
2390 }
2391 
2392 int intel_dp_dsc_compute_config(struct intel_dp *intel_dp,
2393 				struct intel_crtc_state *pipe_config,
2394 				struct drm_connector_state *conn_state,
2395 				const struct link_config_limits *limits,
2396 				int timeslots)
2397 {
2398 	struct intel_display *display = to_intel_display(intel_dp);
2399 	const struct intel_connector *connector =
2400 		to_intel_connector(conn_state->connector);
2401 	const struct drm_display_mode *adjusted_mode =
2402 		&pipe_config->hw.adjusted_mode;
2403 	int num_joined_pipes = intel_crtc_num_joined_pipes(pipe_config);
2404 	bool is_mst = intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DP_MST);
2405 	int ret;
2406 
2407 	/*
2408 	 * FIXME: set the FEC enabled state once pipe_config->port_clock is
2409 	 * already known, so the UHBR/non-UHBR mode can be determined.
2410 	 */
2411 	pipe_config->fec_enable = intel_dp_needs_8b10b_fec(pipe_config, true);
2412 
2413 	if (!intel_dp_dsc_supports_format(connector, pipe_config->output_format))
2414 		return -EINVAL;
2415 
2416 	/*
2417 	 * Link parameters, pipe bpp and compressed bpp have already been
2418 	 * figured out for DP MST DSC.
2419 	 */
2420 	if (!is_mst) {
2421 		ret = intel_dp_dsc_compute_pipe_bpp(intel_dp, pipe_config,
2422 						    conn_state, limits);
2423 		if (ret) {
2424 			drm_dbg_kms(display->drm,
2425 				    "No Valid pipe bpp for given mode ret = %d\n", ret);
2426 			return ret;
2427 		}
2428 	}
2429 
2430 	if (!intel_dp_dsc_get_slice_config(connector, adjusted_mode->crtc_clock,
2431 					   adjusted_mode->crtc_hdisplay, num_joined_pipes,
2432 					   &pipe_config->dsc.slice_config))
2433 		return -EINVAL;
2434 
2435 	ret = intel_dp_dsc_compute_params(connector, pipe_config);
2436 	if (ret < 0) {
2437 		drm_dbg_kms(display->drm,
2438 			    "Cannot compute valid DSC parameters for Input Bpp = %d"
2439 			    "Compressed BPP = " FXP_Q4_FMT "\n",
2440 			    pipe_config->pipe_bpp,
2441 			    FXP_Q4_ARGS(pipe_config->dsc.compressed_bpp_x16));
2442 		return ret;
2443 	}
2444 
2445 	intel_dsc_enable_on_crtc(pipe_config);
2446 
2447 	drm_dbg_kms(display->drm, "DP DSC computed with Input Bpp = %d "
2448 		    "Compressed Bpp = " FXP_Q4_FMT " Slice Count = %d\n",
2449 		    pipe_config->pipe_bpp,
2450 		    FXP_Q4_ARGS(pipe_config->dsc.compressed_bpp_x16),
2451 		    intel_dsc_line_slice_count(&pipe_config->dsc.slice_config));
2452 
2453 	return 0;
2454 }
2455 
2456 static int
2457 dsc_throughput_quirk_max_bpp_x16(const struct intel_connector *connector,
2458 				 int mode_clock)
2459 {
2460 	if (!connector->dp.dsc_throughput_quirk)
2461 		return INT_MAX;
2462 
2463 	/*
2464 	 * Synaptics Panamera branch devices have a problem decompressing a
2465 	 * stream with a compressed link-bpp higher than 12, if the pixel
2466 	 * clock is higher than ~50 % of the maximum overall throughput
2467 	 * reported by the branch device. Work around this by limiting the
2468 	 * maximum link bpp for such pixel clocks.
2469 	 *
2470 	 * TODO: Use the throughput value specific to the actual RGB/YUV
2471 	 * format of the output, after determining the pixel clock limit for
2472 	 * YUV modes. For now use the smaller of the throughput values, which
2473 	 * may result in limiting the link-bpp value already at a lower than
2474 	 * required mode clock in case of native YUV422/420 output formats.
2475 	 * The RGB/YUV444 throughput value should be always either equal or
2476 	 * smaller than the YUV422/420 value, but let's not depend on this
2477 	 * assumption.
2478 	 */
2479 	if (mode_clock <
2480 	    min(connector->dp.dsc_branch_caps.overall_throughput.rgb_yuv444,
2481 		connector->dp.dsc_branch_caps.overall_throughput.yuv422_420) / 2)
2482 		return INT_MAX;
2483 
2484 	return fxp_q4_from_int(12);
2485 }
2486 
2487 int intel_dp_compute_min_compressed_bpp_x16(struct intel_connector *connector,
2488 					    enum intel_output_format output_format)
2489 {
2490 	int dsc_src_min_bpp, dsc_sink_min_bpp, dsc_min_bpp;
2491 	int min_bpp_x16;
2492 
2493 	dsc_src_min_bpp = intel_dp_dsc_min_src_compressed_bpp();
2494 	dsc_sink_min_bpp = intel_dp_dsc_sink_min_compressed_bpp(output_format);
2495 	dsc_min_bpp = max(dsc_src_min_bpp, dsc_sink_min_bpp);
2496 
2497 	min_bpp_x16 = fxp_q4_from_int(dsc_min_bpp);
2498 
2499 	min_bpp_x16 = align_min_compressed_bpp_x16(connector, min_bpp_x16);
2500 
2501 	return min_bpp_x16;
2502 }
2503 
2504 static int compute_max_compressed_bpp_x16(struct intel_connector *connector,
2505 					  int mode_clock, int mode_hdisplay,
2506 					  int num_joined_pipes,
2507 					  enum intel_output_format output_format,
2508 					  int pipe_max_bpp, int max_link_bpp_x16)
2509 {
2510 	struct intel_display *display = to_intel_display(connector);
2511 	struct intel_dp *intel_dp = intel_attached_dp(connector);
2512 	int dsc_src_max_bpp, dsc_sink_max_bpp, dsc_max_bpp;
2513 	int throughput_max_bpp_x16;
2514 	int joiner_max_bpp;
2515 
2516 	dsc_src_max_bpp = dsc_src_max_compressed_bpp(intel_dp);
2517 	joiner_max_bpp = get_max_compressed_bpp_with_joiner(display,
2518 							    mode_clock,
2519 							    mode_hdisplay,
2520 							    num_joined_pipes);
2521 	dsc_sink_max_bpp = intel_dp_dsc_sink_max_compressed_bpp(connector,
2522 								output_format,
2523 								pipe_max_bpp / 3);
2524 	dsc_max_bpp = min(dsc_sink_max_bpp, dsc_src_max_bpp);
2525 	dsc_max_bpp = min(dsc_max_bpp, joiner_max_bpp);
2526 
2527 	max_link_bpp_x16 = min(max_link_bpp_x16, fxp_q4_from_int(dsc_max_bpp));
2528 
2529 	throughput_max_bpp_x16 = dsc_throughput_quirk_max_bpp_x16(connector,
2530 								  mode_clock);
2531 	if (throughput_max_bpp_x16 < max_link_bpp_x16) {
2532 		max_link_bpp_x16 = throughput_max_bpp_x16;
2533 
2534 		drm_dbg_kms(display->drm,
2535 			    "[CONNECTOR:%d:%s] Decreasing link max bpp to " FXP_Q4_FMT " due to DSC throughput quirk\n",
2536 			    connector->base.base.id, connector->base.name,
2537 			    FXP_Q4_ARGS(max_link_bpp_x16));
2538 	}
2539 
2540 	max_link_bpp_x16 = align_max_compressed_bpp_x16(connector, output_format,
2541 							pipe_max_bpp, max_link_bpp_x16);
2542 
2543 	return max_link_bpp_x16;
2544 }
2545 
2546 bool intel_dp_mode_valid_with_dsc(struct intel_connector *connector,
2547 				  int link_clock, int lane_count,
2548 				  int mode_clock, int mode_hdisplay,
2549 				  int num_joined_pipes,
2550 				  enum intel_output_format output_format,
2551 				  int pipe_bpp, unsigned long bw_overhead_flags)
2552 {
2553 	struct intel_dp *intel_dp = intel_attached_dp(connector);
2554 	int min_bpp_x16 = intel_dp_compute_min_compressed_bpp_x16(connector,
2555 								  output_format);
2556 	int max_bpp_x16 = compute_max_compressed_bpp_x16(connector,
2557 							 mode_clock, mode_hdisplay,
2558 							 num_joined_pipes,
2559 							 output_format,
2560 							 pipe_bpp, INT_MAX);
2561 	int dsc_slice_count = intel_dp_dsc_get_slice_count(connector,
2562 							   mode_clock,
2563 							   mode_hdisplay,
2564 							   num_joined_pipes);
2565 
2566 	if (min_bpp_x16 <= 0 || min_bpp_x16 > max_bpp_x16)
2567 		return false;
2568 
2569 	if (dsc_slice_count == 0)
2570 		return false;
2571 
2572 	return is_bw_sufficient_for_dsc_config(intel_dp,
2573 					       link_clock, lane_count,
2574 					       mode_clock, mode_hdisplay,
2575 					       dsc_slice_count, min_bpp_x16,
2576 					       bw_overhead_flags);
2577 }
2578 
2579 /*
2580  * Calculate the output link min, max bpp values in limits based on the pipe bpp
2581  * range, crtc_state and dsc mode. Return true on success.
2582  */
2583 static bool
2584 intel_dp_compute_config_link_bpp_limits(struct intel_connector *connector,
2585 					const struct intel_crtc_state *crtc_state,
2586 					bool dsc,
2587 					struct link_config_limits *limits)
2588 {
2589 	struct intel_display *display = to_intel_display(connector);
2590 	struct intel_dp *intel_dp = intel_attached_dp(connector);
2591 	const struct drm_display_mode *adjusted_mode =
2592 		&crtc_state->hw.adjusted_mode;
2593 	const struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2594 	const struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
2595 	int max_link_bpp_x16;
2596 
2597 	max_link_bpp_x16 = min(crtc_state->max_link_bpp_x16,
2598 			       fxp_q4_from_int(limits->pipe.max_bpp));
2599 
2600 	if (!dsc) {
2601 		max_link_bpp_x16 = rounddown(max_link_bpp_x16, fxp_q4_from_int(2 * 3));
2602 
2603 		if (max_link_bpp_x16 < fxp_q4_from_int(limits->pipe.min_bpp))
2604 			return false;
2605 
2606 		limits->link.min_bpp_x16 = fxp_q4_from_int(limits->pipe.min_bpp);
2607 	} else {
2608 		limits->link.min_bpp_x16 =
2609 			intel_dp_compute_min_compressed_bpp_x16(connector,
2610 								crtc_state->output_format);
2611 
2612 		max_link_bpp_x16 =
2613 			compute_max_compressed_bpp_x16(connector,
2614 						       adjusted_mode->crtc_clock,
2615 						       adjusted_mode->hdisplay,
2616 						       intel_crtc_num_joined_pipes(crtc_state),
2617 						       crtc_state->output_format,
2618 						       limits->pipe.max_bpp,
2619 						       max_link_bpp_x16);
2620 	}
2621 
2622 	limits->link.max_bpp_x16 = max_link_bpp_x16;
2623 
2624 	drm_dbg_kms(display->drm,
2625 		    "[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",
2626 		    encoder->base.base.id, encoder->base.name,
2627 		    crtc->base.base.id, crtc->base.name,
2628 		    adjusted_mode->crtc_clock,
2629 		    str_on_off(dsc),
2630 		    limits->max_lane_count,
2631 		    limits->max_rate,
2632 		    limits->pipe.max_bpp,
2633 		    FXP_Q4_ARGS(limits->link.min_bpp_x16),
2634 		    FXP_Q4_ARGS(limits->link.max_bpp_x16));
2635 
2636 	if (limits->link.min_bpp_x16 <= 0 ||
2637 	    limits->link.min_bpp_x16 > limits->link.max_bpp_x16)
2638 		return false;
2639 
2640 	return true;
2641 }
2642 
2643 static bool
2644 intel_dp_dsc_compute_pipe_bpp_limits(struct intel_connector *connector,
2645 				     struct link_config_limits *limits)
2646 {
2647 	struct intel_display *display = to_intel_display(connector);
2648 	const struct link_config_limits orig_limits = *limits;
2649 	int dsc_min_bpc = intel_dp_dsc_min_src_input_bpc();
2650 	int dsc_max_bpc = intel_dp_dsc_max_src_input_bpc(display);
2651 
2652 	limits->pipe.min_bpp = max(limits->pipe.min_bpp, dsc_min_bpc * 3);
2653 	limits->pipe.min_bpp = align_min_sink_dsc_input_bpp(connector, limits->pipe.min_bpp);
2654 
2655 	limits->pipe.max_bpp = min(limits->pipe.max_bpp, dsc_max_bpc * 3);
2656 	limits->pipe.max_bpp = align_max_sink_dsc_input_bpp(connector, limits->pipe.max_bpp);
2657 
2658 	if (limits->pipe.min_bpp <= 0 ||
2659 	    limits->pipe.min_bpp > limits->pipe.max_bpp) {
2660 		drm_dbg_kms(display->drm,
2661 			    "[CONNECTOR:%d:%s] Invalid DSC src/sink input BPP (src:%d-%d pipe:%d-%d sink-align:%d-%d)\n",
2662 			    connector->base.base.id, connector->base.name,
2663 			    dsc_min_bpc * 3, dsc_max_bpc * 3,
2664 			    orig_limits.pipe.min_bpp, orig_limits.pipe.max_bpp,
2665 			    limits->pipe.min_bpp, limits->pipe.max_bpp);
2666 
2667 		return false;
2668 	}
2669 
2670 	return true;
2671 }
2672 
2673 bool
2674 intel_dp_compute_config_limits(struct intel_dp *intel_dp,
2675 			       struct drm_connector_state *conn_state,
2676 			       struct intel_crtc_state *crtc_state,
2677 			       bool respect_downstream_limits,
2678 			       bool dsc,
2679 			       struct link_config_limits *limits)
2680 {
2681 	struct intel_display *display = to_intel_display(intel_dp);
2682 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
2683 	struct intel_connector *connector =
2684 		to_intel_connector(conn_state->connector);
2685 
2686 	limits->min_rate = intel_dp_min_link_rate(intel_dp);
2687 	limits->max_rate = intel_dp_max_link_rate(intel_dp);
2688 
2689 	limits->min_rate = min(limits->min_rate, limits->max_rate);
2690 
2691 	limits->min_lane_count = intel_dp_min_lane_count(intel_dp);
2692 	limits->max_lane_count = intel_dp_max_lane_count(intel_dp);
2693 
2694 	limits->pipe.min_bpp = intel_dp_min_bpp(crtc_state->output_format);
2695 	if (is_mst) {
2696 		/*
2697 		 * FIXME: If all the streams can't fit into the link with their
2698 		 * current pipe_bpp we should reduce pipe_bpp across the board
2699 		 * until things start to fit. Until then we limit to <= 8bpc
2700 		 * since that's what was hardcoded for all MST streams
2701 		 * previously. This hack should be removed once we have the
2702 		 * proper retry logic in place.
2703 		 */
2704 		limits->pipe.max_bpp = min(crtc_state->max_pipe_bpp, 24);
2705 	} else {
2706 		limits->pipe.max_bpp = intel_dp_max_bpp(intel_dp, crtc_state,
2707 							respect_downstream_limits);
2708 	}
2709 
2710 	if (!dsc && intel_dp_in_hdr_mode(conn_state)) {
2711 		if (intel_dp_supports_dsc(intel_dp, connector, crtc_state) &&
2712 		    limits->pipe.max_bpp >= 30)
2713 			limits->pipe.min_bpp = max(limits->pipe.min_bpp, 30);
2714 		else
2715 			drm_dbg_kms(display->drm,
2716 				    "[CONNECTOR:%d:%s] Can't force 30 bpp for HDR (pipe bpp: %d-%d DSC-support: %s)\n",
2717 				    connector->base.base.id, connector->base.name,
2718 				    limits->pipe.min_bpp, limits->pipe.max_bpp,
2719 				    str_yes_no(intel_dp_supports_dsc(intel_dp, connector,
2720 								     crtc_state)));
2721 	}
2722 
2723 	if (limits->pipe.min_bpp <= 0 ||
2724 	    limits->pipe.min_bpp > limits->pipe.max_bpp) {
2725 		drm_dbg_kms(display->drm, "[CONNECTOR:%d:%s] Invalid pipe bpp range: %d-%d\n",
2726 			    connector->base.base.id, connector->base.name,
2727 			    limits->pipe.min_bpp, limits->pipe.max_bpp);
2728 
2729 		return false;
2730 	}
2731 
2732 	if (dsc && !intel_dp_dsc_compute_pipe_bpp_limits(connector, limits))
2733 		return false;
2734 
2735 	/*
2736 	 * crtc_state->pipe_bpp is the non-DP specific baseline (platform /
2737 	 * EDID) maximum pipe BPP limited by the max-BPC connector property
2738 	 * request. Since by now pipe.max_bpp is <= the above baseline
2739 	 * maximum BPP, the only remaining reason for adjusting pipe.max_bpp
2740 	 * is the max-BPC connector property request. Adjust pipe.max_bpp to
2741 	 * this request within the current valid pipe.min_bpp .. pipe.max_bpp
2742 	 * range.
2743 	 */
2744 	limits->pipe.max_bpp = clamp(crtc_state->pipe_bpp, limits->pipe.min_bpp,
2745 				     limits->pipe.max_bpp);
2746 	if (dsc)
2747 		limits->pipe.max_bpp = align_max_sink_dsc_input_bpp(connector,
2748 								    limits->pipe.max_bpp);
2749 
2750 	if (limits->pipe.max_bpp != crtc_state->pipe_bpp)
2751 		drm_dbg_kms(display->drm,
2752 			    "[CONNECTOR:%d:%s] Adjusting requested max pipe bpp %d -> %d\n",
2753 			    connector->base.base.id, connector->base.name,
2754 			    crtc_state->pipe_bpp, limits->pipe.max_bpp);
2755 
2756 	if (is_mst || intel_dp->use_max_params) {
2757 		/*
2758 		 * For MST we always configure max link bw - the spec doesn't
2759 		 * seem to suggest we should do otherwise.
2760 		 *
2761 		 * Use the maximum clock and number of lanes the eDP panel
2762 		 * advertizes being capable of in case the initial fast
2763 		 * optimal params failed us. The panels are generally
2764 		 * designed to support only a single clock and lane
2765 		 * configuration, and typically on older panels these
2766 		 * values correspond to the native resolution of the panel.
2767 		 */
2768 		limits->min_lane_count = limits->max_lane_count;
2769 		limits->min_rate = limits->max_rate;
2770 	}
2771 
2772 	intel_dp_test_compute_config(intel_dp, crtc_state, limits);
2773 
2774 	return intel_dp_compute_config_link_bpp_limits(connector,
2775 						       crtc_state,
2776 						       dsc,
2777 						       limits);
2778 }
2779 
2780 int intel_dp_config_required_rate(const struct intel_crtc_state *crtc_state)
2781 {
2782 	const struct drm_display_mode *adjusted_mode =
2783 		&crtc_state->hw.adjusted_mode;
2784 	int link_bpp_x16 = crtc_state->dsc.compression_enable ?
2785 		crtc_state->dsc.compressed_bpp_x16 :
2786 		fxp_q4_from_int(crtc_state->pipe_bpp);
2787 
2788 	return intel_dp_link_required(crtc_state->port_clock, crtc_state->lane_count,
2789 				      adjusted_mode->crtc_clock, adjusted_mode->hdisplay,
2790 				      link_bpp_x16, 0);
2791 }
2792 
2793 bool intel_dp_joiner_needs_dsc(struct intel_display *display,
2794 			       int num_joined_pipes)
2795 {
2796 	/*
2797 	 * Pipe joiner needs compression up to display 12 due to bandwidth
2798 	 * limitation. DG2 onwards pipe joiner can be enabled without
2799 	 * compression.
2800 	 * Ultrajoiner always needs compression.
2801 	 */
2802 	return (!HAS_UNCOMPRESSED_JOINER(display) && num_joined_pipes == 2) ||
2803 		num_joined_pipes == 4;
2804 }
2805 
2806 static int
2807 intel_dp_compute_link_for_joined_pipes(struct intel_encoder *encoder,
2808 				       struct intel_crtc_state *pipe_config,
2809 				       struct drm_connector_state *conn_state,
2810 				       bool respect_downstream_limits)
2811 {
2812 	struct intel_display *display = to_intel_display(encoder);
2813 	int num_joined_pipes = intel_crtc_num_joined_pipes(pipe_config);
2814 	struct intel_connector *connector =
2815 		to_intel_connector(conn_state->connector);
2816 	const struct drm_display_mode *adjusted_mode =
2817 		&pipe_config->hw.adjusted_mode;
2818 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2819 	struct link_config_limits limits;
2820 	bool dsc_needed, joiner_needs_dsc;
2821 	int ret = 0;
2822 
2823 	intel_dp_dsc_reset_config(pipe_config);
2824 
2825 	joiner_needs_dsc = intel_dp_joiner_needs_dsc(display, num_joined_pipes);
2826 
2827 	dsc_needed = joiner_needs_dsc || intel_dp->force_dsc_en ||
2828 		     !intel_dp_compute_config_limits(intel_dp, conn_state, pipe_config,
2829 						     respect_downstream_limits,
2830 						     false,
2831 						     &limits);
2832 
2833 	if (!dsc_needed) {
2834 		/*
2835 		 * Optimize for slow and wide for everything, because there are some
2836 		 * eDP 1.3 and 1.4 panels don't work well with fast and narrow.
2837 		 */
2838 		ret = intel_dp_compute_link_config_wide(intel_dp, pipe_config,
2839 							conn_state, &limits);
2840 		if (!ret && intel_dp_is_uhbr(pipe_config))
2841 			ret = intel_dp_mtp_tu_compute_config(intel_dp,
2842 							     pipe_config,
2843 							     conn_state,
2844 							     fxp_q4_from_int(pipe_config->pipe_bpp),
2845 							     fxp_q4_from_int(pipe_config->pipe_bpp),
2846 							     0, false);
2847 
2848 		if (ret ||
2849 		    !intel_dp_dotclk_valid(display,
2850 					   adjusted_mode->crtc_clock,
2851 					   adjusted_mode->crtc_htotal,
2852 					   0,
2853 					   num_joined_pipes))
2854 			dsc_needed = true;
2855 	}
2856 
2857 	if (dsc_needed && !intel_dp_supports_dsc(intel_dp, connector, pipe_config)) {
2858 		drm_dbg_kms(display->drm, "DSC required but not available\n");
2859 		return -EINVAL;
2860 	}
2861 
2862 	if (dsc_needed) {
2863 		int dsc_slice_count;
2864 
2865 		drm_dbg_kms(display->drm,
2866 			    "Try DSC (fallback=%s, joiner=%s, force=%s)\n",
2867 			    str_yes_no(ret), str_yes_no(joiner_needs_dsc),
2868 			    str_yes_no(intel_dp->force_dsc_en));
2869 
2870 		if (!intel_dp_compute_config_limits(intel_dp, conn_state, pipe_config,
2871 						    respect_downstream_limits,
2872 						    true,
2873 						    &limits))
2874 			return -EINVAL;
2875 
2876 		ret = intel_dp_dsc_compute_config(intel_dp, pipe_config,
2877 						  conn_state, &limits, 64);
2878 		if (ret < 0)
2879 			return ret;
2880 
2881 		dsc_slice_count = intel_dsc_line_slice_count(&pipe_config->dsc.slice_config);
2882 
2883 		if (!intel_dp_dotclk_valid(display,
2884 					   adjusted_mode->crtc_clock,
2885 					   adjusted_mode->crtc_htotal,
2886 					   dsc_slice_count,
2887 					   num_joined_pipes))
2888 			return -EINVAL;
2889 	}
2890 
2891 	drm_dbg_kms(display->drm,
2892 		    "DP lane count %d clock %d bpp input %d compressed " FXP_Q4_FMT " HDR %s link rate required %d available %d\n",
2893 		    pipe_config->lane_count, pipe_config->port_clock,
2894 		    pipe_config->pipe_bpp,
2895 		    FXP_Q4_ARGS(pipe_config->dsc.compressed_bpp_x16),
2896 		    str_yes_no(intel_dp_in_hdr_mode(conn_state)),
2897 		    intel_dp_config_required_rate(pipe_config),
2898 		    intel_dp_max_link_data_rate(intel_dp,
2899 						pipe_config->port_clock,
2900 						pipe_config->lane_count));
2901 
2902 	return 0;
2903 }
2904 
2905 static int
2906 intel_dp_compute_link_config(struct intel_encoder *encoder,
2907 			     struct intel_crtc_state *crtc_state,
2908 			     struct drm_connector_state *conn_state,
2909 			     bool respect_downstream_limits)
2910 {
2911 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2912 	struct intel_connector *connector =
2913 		to_intel_connector(conn_state->connector);
2914 	const struct drm_display_mode *adjusted_mode =
2915 		&crtc_state->hw.adjusted_mode;
2916 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2917 	int num_joined_pipes;
2918 	int ret = -EINVAL;
2919 
2920 	if (crtc_state->fec_enable &&
2921 	    !intel_dp_supports_fec(intel_dp, connector, crtc_state))
2922 		return -EINVAL;
2923 
2924 	for_each_joiner_candidate(connector, adjusted_mode, num_joined_pipes) {
2925 		/*
2926 		 * NOTE:
2927 		 * The crtc_state->joiner_pipes should have been set at the end
2928 		 * only if all the conditions are met. However that would mean
2929 		 * that num_joined_pipes is passed around to all helpers and
2930 		 * make them use it instead of using crtc_state->joiner_pipes
2931 		 * directly or indirectly (via intel_crtc_num_joined_pipes()).
2932 		 *
2933 		 * For now, setting crtc_state->joiner_pipes to the candidate
2934 		 * value to avoid the above churn and resetting it to 0, in case
2935 		 * no joiner candidate is found to be suitable for the given
2936 		 * configuration.
2937 		 */
2938 		if (num_joined_pipes > 1)
2939 			crtc_state->joiner_pipes = GENMASK(crtc->pipe + num_joined_pipes - 1,
2940 							   crtc->pipe);
2941 
2942 		ret = intel_dp_compute_link_for_joined_pipes(encoder, crtc_state, conn_state,
2943 							     respect_downstream_limits);
2944 		if (ret == 0 || ret == -EDEADLK)
2945 			break;
2946 	}
2947 
2948 	if (ret < 0)
2949 		crtc_state->joiner_pipes = 0;
2950 
2951 	return ret;
2952 }
2953 
2954 bool intel_dp_limited_color_range(const struct intel_crtc_state *crtc_state,
2955 				  const struct drm_connector_state *conn_state)
2956 {
2957 	const struct intel_digital_connector_state *intel_conn_state =
2958 		to_intel_digital_connector_state(conn_state);
2959 	const struct drm_display_mode *adjusted_mode =
2960 		&crtc_state->hw.adjusted_mode;
2961 
2962 	/*
2963 	 * Our YCbCr output is always limited range.
2964 	 * crtc_state->limited_color_range only applies to RGB,
2965 	 * and it must never be set for YCbCr or we risk setting
2966 	 * some conflicting bits in TRANSCONF which will mess up
2967 	 * the colors on the monitor.
2968 	 */
2969 	if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
2970 		return false;
2971 
2972 	if (intel_conn_state->broadcast_rgb == INTEL_BROADCAST_RGB_AUTO) {
2973 		/*
2974 		 * See:
2975 		 * CEA-861-E - 5.1 Default Encoding Parameters
2976 		 * VESA DisplayPort Ver.1.2a - 5.1.1.1 Video Colorimetry
2977 		 */
2978 		return crtc_state->pipe_bpp != 18 &&
2979 			drm_default_rgb_quant_range(adjusted_mode) ==
2980 			HDMI_QUANTIZATION_RANGE_LIMITED;
2981 	} else {
2982 		return intel_conn_state->broadcast_rgb ==
2983 			INTEL_BROADCAST_RGB_LIMITED;
2984 	}
2985 }
2986 
2987 static bool intel_dp_port_has_audio(struct intel_display *display, enum port port)
2988 {
2989 	if (display->platform.g4x)
2990 		return false;
2991 	if (DISPLAY_VER(display) < 12 && port == PORT_A)
2992 		return false;
2993 
2994 	return true;
2995 }
2996 
2997 static void intel_dp_compute_vsc_colorimetry(const struct intel_crtc_state *crtc_state,
2998 					     const struct drm_connector_state *conn_state,
2999 					     struct drm_dp_vsc_sdp *vsc)
3000 {
3001 	struct intel_display *display = to_intel_display(crtc_state);
3002 
3003 	if (crtc_state->has_panel_replay) {
3004 		/*
3005 		 * Prepare VSC Header for SU as per DP 2.0 spec, Table 2-223
3006 		 * VSC SDP supporting 3D stereo, Panel Replay, and Pixel
3007 		 * Encoding/Colorimetry Format indication.
3008 		 */
3009 		vsc->revision = 0x7;
3010 	} else {
3011 		/*
3012 		 * Prepare VSC Header for SU as per DP 1.4 spec, Table 2-118
3013 		 * VSC SDP supporting 3D stereo, PSR2, and Pixel Encoding/
3014 		 * Colorimetry Format indication.
3015 		 */
3016 		vsc->revision = 0x5;
3017 	}
3018 
3019 	vsc->length = 0x13;
3020 
3021 	/* DP 1.4a spec, Table 2-120 */
3022 	switch (crtc_state->output_format) {
3023 	case INTEL_OUTPUT_FORMAT_YCBCR444:
3024 		vsc->pixelformat = DP_PIXELFORMAT_YUV444;
3025 		break;
3026 	case INTEL_OUTPUT_FORMAT_YCBCR420:
3027 		vsc->pixelformat = DP_PIXELFORMAT_YUV420;
3028 		break;
3029 	case INTEL_OUTPUT_FORMAT_RGB:
3030 	default:
3031 		vsc->pixelformat = DP_PIXELFORMAT_RGB;
3032 	}
3033 
3034 	switch (conn_state->colorspace) {
3035 	case DRM_MODE_COLORIMETRY_BT709_YCC:
3036 		vsc->colorimetry = DP_COLORIMETRY_BT709_YCC;
3037 		break;
3038 	case DRM_MODE_COLORIMETRY_XVYCC_601:
3039 		vsc->colorimetry = DP_COLORIMETRY_XVYCC_601;
3040 		break;
3041 	case DRM_MODE_COLORIMETRY_XVYCC_709:
3042 		vsc->colorimetry = DP_COLORIMETRY_XVYCC_709;
3043 		break;
3044 	case DRM_MODE_COLORIMETRY_SYCC_601:
3045 		vsc->colorimetry = DP_COLORIMETRY_SYCC_601;
3046 		break;
3047 	case DRM_MODE_COLORIMETRY_OPYCC_601:
3048 		vsc->colorimetry = DP_COLORIMETRY_OPYCC_601;
3049 		break;
3050 	case DRM_MODE_COLORIMETRY_BT2020_CYCC:
3051 		vsc->colorimetry = DP_COLORIMETRY_BT2020_CYCC;
3052 		break;
3053 	case DRM_MODE_COLORIMETRY_BT2020_RGB:
3054 		vsc->colorimetry = DP_COLORIMETRY_BT2020_RGB;
3055 		break;
3056 	case DRM_MODE_COLORIMETRY_BT2020_YCC:
3057 		vsc->colorimetry = DP_COLORIMETRY_BT2020_YCC;
3058 		break;
3059 	case DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65:
3060 	case DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER:
3061 		vsc->colorimetry = DP_COLORIMETRY_DCI_P3_RGB;
3062 		break;
3063 	default:
3064 		/*
3065 		 * RGB->YCBCR color conversion uses the BT.709
3066 		 * color space.
3067 		 */
3068 		if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
3069 			vsc->colorimetry = DP_COLORIMETRY_BT709_YCC;
3070 		else
3071 			vsc->colorimetry = DP_COLORIMETRY_DEFAULT;
3072 		break;
3073 	}
3074 
3075 	vsc->bpc = crtc_state->pipe_bpp / 3;
3076 
3077 	/* only RGB pixelformat supports 6 bpc */
3078 	drm_WARN_ON(display->drm,
3079 		    vsc->bpc == 6 && vsc->pixelformat != DP_PIXELFORMAT_RGB);
3080 
3081 	/* All YCbCr formats are always limited range. */
3082 	if (vsc->pixelformat == DP_PIXELFORMAT_RGB)
3083 		vsc->dynamic_range = crtc_state->limited_color_range ?
3084 			DP_DYNAMIC_RANGE_CTA : DP_DYNAMIC_RANGE_VESA;
3085 	else
3086 		vsc->dynamic_range = DP_DYNAMIC_RANGE_CTA;
3087 
3088 	vsc->content_type = DP_CONTENT_TYPE_NOT_DEFINED;
3089 }
3090 
3091 static bool intel_dp_needs_as_sdp(struct intel_dp *intel_dp,
3092 				  struct intel_crtc_state *crtc_state)
3093 {
3094 	if (!intel_dp->as_sdp_v2_supported)
3095 		return false;
3096 
3097 	/*
3098 	 * #TODO: Add AS SDP v1 support for PCONs (DP branch devices).
3099 	 */
3100 	if (drm_dp_is_branch(intel_dp->dpcd))
3101 		return false;
3102 
3103 	if (intel_psr_needs_alpm_aux_less(intel_dp, crtc_state) &&
3104 	    !intel_psr_pr_async_video_timing_supported(intel_dp))
3105 		return true;
3106 
3107 	return intel_vrr_possible(crtc_state);
3108 }
3109 
3110 static void intel_dp_compute_as_sdp(struct intel_dp *intel_dp,
3111 				    struct intel_crtc_state *crtc_state)
3112 {
3113 	struct drm_dp_as_sdp *as_sdp = &crtc_state->infoframes.as_sdp;
3114 	const struct drm_display_mode *adjusted_mode =
3115 		&crtc_state->hw.adjusted_mode;
3116 
3117 	/*
3118 	 * #FIXME: SDP/infoframe updates aren’t truly atomic, and with the new
3119 	 * cdclk->tc clock crossing we may transiently send a corrupted packet
3120 	 * if the update lands mid‑transmission.
3121 	 */
3122 	if (!intel_dp_needs_as_sdp(intel_dp, crtc_state))
3123 		return;
3124 
3125 	crtc_state->infoframes.enable |= intel_hdmi_infoframe_enable(DP_SDP_ADAPTIVE_SYNC);
3126 
3127 	as_sdp->sdp_type = DP_SDP_ADAPTIVE_SYNC;
3128 	as_sdp->length = 0x9;
3129 	as_sdp->duration_incr_ms = 0;
3130 	as_sdp->revision = 0x2;
3131 	as_sdp->vtotal = intel_vrr_vmin_vtotal(crtc_state);
3132 
3133 	if (crtc_state->cmrr.enable) {
3134 		as_sdp->mode = DP_AS_SDP_FAVT_TRR_REACHED;
3135 		as_sdp->target_rr = drm_mode_vrefresh(adjusted_mode);
3136 		as_sdp->target_rr_divider = true;
3137 	} else if (crtc_state->vrr.enable) {
3138 		as_sdp->mode = DP_AS_SDP_AVT_DYNAMIC_VTOTAL;
3139 	} else {
3140 		as_sdp->mode = DP_AS_SDP_AVT_FIXED_VTOTAL;
3141 	}
3142 
3143 	/*
3144 	 * For Panel Replay with Async Video Timing support, the source can
3145 	 * disable sending the AS SDP during PR Active state. In that case,
3146 	 * the sink needs the coasting vtotal value to maintain the refresh
3147 	 * rate. The HW only samples this on PR_ALPM_CTL[AS SDP Transmission
3148 	 * in Active Disable], which we never program, so providing the value
3149 	 * unconditionally when the sink advertises the capability is safe.
3150 	 *
3151 	 * #TODO:
3152 	 * If we ever advertise support for coasting at other refresh targets,
3153 	 * this logic could be revisited. For now, use the minimum refresh rate
3154 	 * as the only safe coasting value.
3155 	 */
3156 	if (intel_psr_pr_async_video_timing_supported(intel_dp))
3157 		as_sdp->coasting_vtotal = crtc_state->vrr.vmax;
3158 }
3159 
3160 static void intel_dp_compute_vsc_sdp(struct intel_dp *intel_dp,
3161 				     struct intel_crtc_state *crtc_state,
3162 				     const struct drm_connector_state *conn_state)
3163 {
3164 	struct drm_dp_vsc_sdp *vsc;
3165 
3166 	if ((!intel_dp->colorimetry_support ||
3167 	     !intel_dp_needs_vsc_sdp(crtc_state, conn_state)) &&
3168 	    !crtc_state->has_psr)
3169 		return;
3170 
3171 	vsc = &crtc_state->infoframes.vsc;
3172 
3173 	crtc_state->infoframes.enable |= intel_hdmi_infoframe_enable(DP_SDP_VSC);
3174 	vsc->sdp_type = DP_SDP_VSC;
3175 
3176 	/* Needs colorimetry */
3177 	if (intel_dp_needs_vsc_sdp(crtc_state, conn_state)) {
3178 		intel_dp_compute_vsc_colorimetry(crtc_state, conn_state,
3179 						 vsc);
3180 	} else if (crtc_state->has_panel_replay) {
3181 		/*
3182 		 * [Panel Replay without colorimetry info]
3183 		 * Prepare VSC Header for SU as per DP 2.0 spec, Table 2-223
3184 		 * VSC SDP supporting 3D stereo + Panel Replay.
3185 		 */
3186 		vsc->revision = 0x6;
3187 		vsc->length = 0x10;
3188 	} else if (crtc_state->has_sel_update) {
3189 		/*
3190 		 * [PSR2 without colorimetry]
3191 		 * Prepare VSC Header for SU as per eDP 1.4 spec, Table 6-11
3192 		 * 3D stereo + PSR/PSR2 + Y-coordinate.
3193 		 */
3194 		vsc->revision = 0x4;
3195 		vsc->length = 0xe;
3196 	} else {
3197 		/*
3198 		 * [PSR1]
3199 		 * Prepare VSC Header for SU as per DP 1.4 spec, Table 2-118
3200 		 * VSC SDP supporting 3D stereo + PSR (applies to eDP v1.3 or
3201 		 * higher).
3202 		 */
3203 		vsc->revision = 0x2;
3204 		vsc->length = 0x8;
3205 	}
3206 }
3207 
3208 bool
3209 intel_dp_in_hdr_mode(const struct drm_connector_state *conn_state)
3210 {
3211 	struct hdr_output_metadata *hdr_metadata;
3212 
3213 	if (!conn_state->hdr_output_metadata)
3214 		return false;
3215 
3216 	hdr_metadata = conn_state->hdr_output_metadata->data;
3217 
3218 	return hdr_metadata->hdmi_metadata_type1.eotf == HDMI_EOTF_SMPTE_ST2084;
3219 }
3220 
3221 static void
3222 intel_dp_compute_hdr_metadata_infoframe_sdp(struct intel_dp *intel_dp,
3223 					    struct intel_crtc_state *crtc_state,
3224 					    const struct drm_connector_state *conn_state)
3225 {
3226 	struct intel_display *display = to_intel_display(intel_dp);
3227 	int ret;
3228 	struct hdmi_drm_infoframe *drm_infoframe = &crtc_state->infoframes.drm.drm;
3229 
3230 	if (!conn_state->hdr_output_metadata)
3231 		return;
3232 
3233 	ret = drm_hdmi_infoframe_set_hdr_metadata(drm_infoframe, conn_state);
3234 
3235 	if (ret) {
3236 		drm_dbg_kms(display->drm,
3237 			    "couldn't set HDR metadata in infoframe\n");
3238 		return;
3239 	}
3240 
3241 	crtc_state->infoframes.enable |=
3242 		intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GAMUT_METADATA);
3243 }
3244 
3245 static bool can_enable_drrs(struct intel_connector *connector,
3246 			    const struct intel_crtc_state *pipe_config,
3247 			    const struct drm_display_mode *downclock_mode)
3248 {
3249 	struct intel_display *display = to_intel_display(connector);
3250 
3251 	if (pipe_config->vrr.enable)
3252 		return false;
3253 
3254 	/*
3255 	 * DRRS and PSR can't be enable together, so giving preference to PSR
3256 	 * as it allows more power-savings by complete shutting down display,
3257 	 * so to guarantee this, intel_drrs_compute_config() must be called
3258 	 * after intel_psr_compute_config().
3259 	 */
3260 	if (pipe_config->has_psr)
3261 		return false;
3262 
3263 	/* FIXME missing FDI M2/N2 etc. */
3264 	if (pipe_config->has_pch_encoder)
3265 		return false;
3266 
3267 	if (!intel_cpu_transcoder_has_drrs(display, pipe_config->cpu_transcoder))
3268 		return false;
3269 
3270 	return downclock_mode &&
3271 		intel_panel_drrs_type(connector) == DRRS_TYPE_SEAMLESS;
3272 }
3273 
3274 static void
3275 intel_dp_drrs_compute_config(struct intel_connector *connector,
3276 			     struct intel_crtc_state *pipe_config,
3277 			     int link_bpp_x16)
3278 {
3279 	struct intel_display *display = to_intel_display(connector);
3280 	const struct drm_display_mode *downclock_mode =
3281 		intel_panel_downclock_mode(connector, &pipe_config->hw.adjusted_mode);
3282 	int pixel_clock;
3283 
3284 	/*
3285 	 * FIXME all joined pipes share the same transcoder.
3286 	 * Need to account for that when updating M/N live.
3287 	 */
3288 	if (has_seamless_m_n(connector) && !pipe_config->joiner_pipes)
3289 		pipe_config->update_m_n = true;
3290 
3291 	if (!can_enable_drrs(connector, pipe_config, downclock_mode)) {
3292 		if (intel_cpu_transcoder_has_m2_n2(display, pipe_config->cpu_transcoder))
3293 			intel_zero_m_n(&pipe_config->dp_m2_n2);
3294 		return;
3295 	}
3296 
3297 	if (display->platform.ironlake || display->platform.sandybridge ||
3298 	    display->platform.ivybridge)
3299 		pipe_config->msa_timing_delay = connector->panel.vbt.edp.drrs_msa_timing_delay;
3300 
3301 	pipe_config->has_drrs = true;
3302 
3303 	pixel_clock = downclock_mode->clock;
3304 	if (pipe_config->splitter.enable)
3305 		pixel_clock /= pipe_config->splitter.link_count;
3306 
3307 	intel_link_compute_m_n(link_bpp_x16, pipe_config->lane_count, pixel_clock,
3308 			       pipe_config->port_clock,
3309 			       intel_dp_bw_fec_overhead(pipe_config->fec_enable),
3310 			       &pipe_config->dp_m2_n2);
3311 
3312 	/* FIXME: abstract this better */
3313 	if (pipe_config->splitter.enable)
3314 		pipe_config->dp_m2_n2.data_m *= pipe_config->splitter.link_count;
3315 }
3316 
3317 static bool intel_dp_has_audio(struct intel_encoder *encoder,
3318 			       const struct drm_connector_state *conn_state)
3319 {
3320 	struct intel_display *display = to_intel_display(encoder);
3321 	const struct intel_digital_connector_state *intel_conn_state =
3322 		to_intel_digital_connector_state(conn_state);
3323 	struct intel_connector *connector =
3324 		to_intel_connector(conn_state->connector);
3325 
3326 	if (!intel_dp_port_has_audio(display, encoder->port))
3327 		return false;
3328 
3329 	if (intel_conn_state->force_audio == HDMI_AUDIO_AUTO)
3330 		return connector->base.display_info.has_audio;
3331 	else
3332 		return intel_conn_state->force_audio == HDMI_AUDIO_ON;
3333 }
3334 
3335 static int
3336 intel_dp_compute_output_format(struct intel_encoder *encoder,
3337 			       struct intel_crtc_state *crtc_state,
3338 			       struct drm_connector_state *conn_state,
3339 			       bool respect_downstream_limits,
3340 			       enum intel_output_format sink_format)
3341 {
3342 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3343 	struct intel_connector *connector = intel_dp->attached_connector;
3344 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
3345 
3346 	if (intel_dp_sink_format_valid(connector, adjusted_mode,
3347 				       sink_format) != MODE_OK)
3348 		return -EINVAL;
3349 
3350 	crtc_state->sink_format = sink_format;
3351 	crtc_state->output_format = intel_dp_output_format(connector, crtc_state->sink_format);
3352 
3353 	return intel_dp_compute_link_config(encoder, crtc_state, conn_state,
3354 					    respect_downstream_limits);
3355 }
3356 
3357 static int
3358 intel_dp_compute_formats(struct intel_encoder *encoder,
3359 			 struct intel_crtc_state *crtc_state,
3360 			 struct drm_connector_state *conn_state,
3361 			 bool respect_downstream_limits)
3362 {
3363 	struct intel_display *display = to_intel_display(encoder);
3364 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3365 	struct intel_connector *connector = intel_dp->attached_connector;
3366 	const struct drm_display_info *info = &connector->base.display_info;
3367 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
3368 	int ret;
3369 
3370 	if (drm_mode_is_420_only(info, adjusted_mode)) {
3371 		ret = intel_dp_compute_output_format(encoder, crtc_state, conn_state,
3372 						     respect_downstream_limits,
3373 						     INTEL_OUTPUT_FORMAT_YCBCR420);
3374 
3375 		if (ret && !respect_downstream_limits) {
3376 			drm_dbg_kms(display->drm,
3377 				    "YCbCr 4:2:0 mode but YCbCr 4:2:0 output not possible. Falling back to RGB.\n");
3378 
3379 			ret = intel_dp_compute_output_format(encoder, crtc_state, conn_state,
3380 							     respect_downstream_limits,
3381 							     INTEL_OUTPUT_FORMAT_RGB);
3382 		}
3383 	} else {
3384 		ret = intel_dp_compute_output_format(encoder, crtc_state, conn_state,
3385 						     respect_downstream_limits,
3386 						     INTEL_OUTPUT_FORMAT_RGB);
3387 
3388 		if (ret && drm_mode_is_420_also(info, adjusted_mode))
3389 			ret = intel_dp_compute_output_format(encoder, crtc_state, conn_state,
3390 							     respect_downstream_limits,
3391 							     INTEL_OUTPUT_FORMAT_YCBCR420);
3392 	}
3393 
3394 	return ret;
3395 }
3396 
3397 void
3398 intel_dp_audio_compute_config(struct intel_encoder *encoder,
3399 			      struct intel_crtc_state *pipe_config,
3400 			      struct drm_connector_state *conn_state)
3401 {
3402 	pipe_config->has_audio =
3403 		intel_dp_has_audio(encoder, conn_state) &&
3404 		intel_audio_compute_config(encoder, pipe_config, conn_state);
3405 
3406 	pipe_config->sdp_split_enable = pipe_config->has_audio &&
3407 					intel_dp_is_uhbr(pipe_config);
3408 }
3409 
3410 void
3411 intel_dp_queue_modeset_retry_for_link(struct intel_atomic_state *state,
3412 				      struct intel_encoder *encoder,
3413 				      const struct intel_crtc_state *crtc_state)
3414 {
3415 	struct intel_connector *connector;
3416 	struct intel_digital_connector_state *conn_state;
3417 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3418 	int i;
3419 
3420 	if (intel_dp->needs_modeset_retry)
3421 		return;
3422 
3423 	intel_dp->needs_modeset_retry = true;
3424 
3425 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
3426 		intel_connector_queue_modeset_retry_work(intel_dp->attached_connector);
3427 
3428 		return;
3429 	}
3430 
3431 	for_each_new_intel_connector_in_state(state, connector, conn_state, i) {
3432 		if (!conn_state->base.crtc)
3433 			continue;
3434 
3435 		if (connector->mst.dp == intel_dp)
3436 			intel_connector_queue_modeset_retry_work(connector);
3437 	}
3438 }
3439 
3440 int intel_dp_compute_min_hblank(struct intel_crtc_state *crtc_state,
3441 				const struct drm_connector_state *conn_state)
3442 {
3443 	struct intel_display *display = to_intel_display(crtc_state);
3444 	const struct drm_display_mode *adjusted_mode =
3445 					&crtc_state->hw.adjusted_mode;
3446 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
3447 	int symbol_size = intel_dp_is_uhbr(crtc_state) ? 32 : 8;
3448 	/*
3449 	 * min symbol cycles is 3(BS,VBID, BE) for 128b/132b and
3450 	 * 5(BS, VBID, MVID, MAUD, BE) for 8b/10b
3451 	 */
3452 	int min_sym_cycles = intel_dp_is_uhbr(crtc_state) ? 3 : 5;
3453 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
3454 	int num_joined_pipes = intel_crtc_num_joined_pipes(crtc_state);
3455 	int min_hblank;
3456 	int max_lane_count = 4;
3457 	int hactive_sym_cycles, htotal_sym_cycles;
3458 	int dsc_slices = 0;
3459 	int link_bpp_x16;
3460 
3461 	if (DISPLAY_VER(display) < 30)
3462 		return 0;
3463 
3464 	/* MIN_HBLANK should be set only for 8b/10b MST or for 128b/132b SST/MST */
3465 	if (!is_mst && !intel_dp_is_uhbr(crtc_state))
3466 		return 0;
3467 
3468 	if (crtc_state->dsc.compression_enable) {
3469 		dsc_slices = intel_dp_dsc_get_slice_count(connector,
3470 							  adjusted_mode->crtc_clock,
3471 							  adjusted_mode->crtc_hdisplay,
3472 							  num_joined_pipes);
3473 		if (!dsc_slices) {
3474 			drm_dbg(display->drm, "failed to calculate dsc slice count\n");
3475 			return -EINVAL;
3476 		}
3477 	}
3478 
3479 	if (crtc_state->dsc.compression_enable)
3480 		link_bpp_x16 = crtc_state->dsc.compressed_bpp_x16;
3481 	else
3482 		link_bpp_x16 = intel_dp_output_format_link_bpp_x16(crtc_state->output_format,
3483 								   crtc_state->pipe_bpp);
3484 
3485 	/* Calculate min Hblank Link Layer Symbol Cycle Count for 8b/10b MST & 128b/132b */
3486 	hactive_sym_cycles = drm_dp_link_symbol_cycles(max_lane_count,
3487 						       adjusted_mode->hdisplay,
3488 						       dsc_slices,
3489 						       link_bpp_x16,
3490 						       symbol_size, is_mst);
3491 	htotal_sym_cycles = adjusted_mode->htotal * hactive_sym_cycles /
3492 			     adjusted_mode->hdisplay;
3493 
3494 	min_hblank = htotal_sym_cycles - hactive_sym_cycles;
3495 	/* minimum Hblank calculation: https://groups.vesa.org/wg/DP/document/20494 */
3496 	min_hblank = max(min_hblank, min_sym_cycles);
3497 
3498 	/*
3499 	 * adjust the BlankingStart/BlankingEnd framing control from
3500 	 * the calculated value
3501 	 */
3502 	min_hblank = min_hblank - 2;
3503 
3504 	/*
3505 	 * min_hblank formula is undergoing a change, to avoid underrun use the
3506 	 * recomended value in spec to compare with the calculated one and use the
3507 	 * minimum value
3508 	 */
3509 	if (intel_dp_is_uhbr(crtc_state)) {
3510 		/*
3511 		 * Note: Bspec requires a min_hblank of 2 for YCBCR420
3512 		 * with compressed bpp 6, but the minimum compressed bpp
3513 		 * supported by the driver is 8.
3514 		 */
3515 		drm_WARN_ON(display->drm,
3516 			    (crtc_state->dsc.compression_enable &&
3517 			     crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 &&
3518 			     crtc_state->dsc.compressed_bpp_x16 < fxp_q4_from_int(8)));
3519 		min_hblank = min(3, min_hblank);
3520 	} else {
3521 		min_hblank = min(10, min_hblank);
3522 	}
3523 
3524 	crtc_state->min_hblank = min_hblank;
3525 
3526 	return 0;
3527 }
3528 
3529 int
3530 intel_dp_compute_config(struct intel_atomic_state *state,
3531 			struct intel_encoder *encoder,
3532 			struct intel_crtc_state *pipe_config,
3533 			struct drm_connector_state *conn_state)
3534 {
3535 	struct intel_display *display = to_intel_display(encoder);
3536 	struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
3537 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3538 	struct intel_connector *connector = intel_dp->attached_connector;
3539 	int ret = 0, link_bpp_x16;
3540 
3541 	if (intel_dp_is_edp(intel_dp)) {
3542 		ret = intel_panel_compute_config(state, pipe_config, connector);
3543 		if (ret)
3544 			return ret;
3545 	}
3546 
3547 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
3548 		return -EINVAL;
3549 
3550 	if (!connector->base.interlace_allowed &&
3551 	    adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
3552 		return -EINVAL;
3553 
3554 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
3555 		return -EINVAL;
3556 
3557 	if (intel_dp_hdisplay_bad(display, adjusted_mode->crtc_hdisplay))
3558 		return -EINVAL;
3559 
3560 	/*
3561 	 * Try to respect downstream TMDS clock limits first, if
3562 	 * that fails assume the user might know something we don't.
3563 	 */
3564 	ret = intel_dp_compute_formats(encoder, pipe_config, conn_state, true);
3565 	if (ret)
3566 		ret = intel_dp_compute_formats(encoder, pipe_config, conn_state, false);
3567 	if (ret)
3568 		return ret;
3569 
3570 	ret = intel_pfit_compute_config(pipe_config, conn_state);
3571 	if (ret)
3572 		return ret;
3573 
3574 	pipe_config->limited_color_range =
3575 		intel_dp_limited_color_range(pipe_config, conn_state);
3576 
3577 	if (intel_dp_is_uhbr(pipe_config)) {
3578 		/* 128b/132b SST also needs this */
3579 		pipe_config->mst_master_transcoder = pipe_config->cpu_transcoder;
3580 	} else {
3581 		pipe_config->enhanced_framing =
3582 			drm_dp_enhanced_frame_cap(intel_dp->dpcd);
3583 	}
3584 
3585 	if (pipe_config->dsc.compression_enable)
3586 		link_bpp_x16 = pipe_config->dsc.compressed_bpp_x16;
3587 	else
3588 		link_bpp_x16 = intel_dp_output_format_link_bpp_x16(pipe_config->output_format,
3589 								   pipe_config->pipe_bpp);
3590 
3591 	if (intel_dp->mso_link_count) {
3592 		int n = intel_dp->mso_link_count;
3593 		int overlap = intel_dp->mso_pixel_overlap;
3594 
3595 		pipe_config->splitter.enable = true;
3596 		pipe_config->splitter.link_count = n;
3597 		pipe_config->splitter.pixel_overlap = overlap;
3598 
3599 		drm_dbg_kms(display->drm,
3600 			    "MSO link count %d, pixel overlap %d\n",
3601 			    n, overlap);
3602 
3603 		adjusted_mode->crtc_hdisplay = adjusted_mode->crtc_hdisplay / n + overlap;
3604 		adjusted_mode->crtc_hblank_start = adjusted_mode->crtc_hblank_start / n + overlap;
3605 		adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_hblank_end / n + overlap;
3606 		adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hsync_start / n + overlap;
3607 		adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_end / n + overlap;
3608 		adjusted_mode->crtc_htotal = adjusted_mode->crtc_htotal / n + overlap;
3609 		adjusted_mode->crtc_clock /= n;
3610 	}
3611 
3612 	intel_dp_audio_compute_config(encoder, pipe_config, conn_state);
3613 
3614 	if (!intel_dp_is_uhbr(pipe_config)) {
3615 		intel_link_compute_m_n(link_bpp_x16,
3616 				       pipe_config->lane_count,
3617 				       adjusted_mode->crtc_clock,
3618 				       pipe_config->port_clock,
3619 				       intel_dp_bw_fec_overhead(pipe_config->fec_enable),
3620 				       &pipe_config->dp_m_n);
3621 	}
3622 
3623 	ret = intel_dp_compute_min_hblank(pipe_config, conn_state);
3624 	if (ret)
3625 		return ret;
3626 
3627 	/* FIXME: abstract this better */
3628 	if (pipe_config->splitter.enable)
3629 		pipe_config->dp_m_n.data_m *= pipe_config->splitter.link_count;
3630 
3631 	intel_vrr_compute_config(pipe_config, conn_state);
3632 	intel_psr_compute_config(intel_dp, pipe_config, conn_state);
3633 	intel_dp_compute_as_sdp(intel_dp, pipe_config);
3634 	intel_alpm_lobf_compute_config(intel_dp, pipe_config, conn_state);
3635 	intel_dp_drrs_compute_config(connector, pipe_config, link_bpp_x16);
3636 	intel_dp_compute_vsc_sdp(intel_dp, pipe_config, conn_state);
3637 	intel_dp_compute_hdr_metadata_infoframe_sdp(intel_dp, pipe_config, conn_state);
3638 
3639 	return intel_dp_tunnel_atomic_compute_stream_bw(state, intel_dp, connector,
3640 							pipe_config);
3641 }
3642 
3643 void intel_dp_set_link_params(struct intel_dp *intel_dp,
3644 			      int link_rate, int lane_count)
3645 {
3646 	memset(intel_dp->train_set, 0, sizeof(intel_dp->train_set));
3647 	intel_dp->link.active = false;
3648 	intel_dp->needs_modeset_retry = false;
3649 	intel_dp->link_rate = link_rate;
3650 	intel_dp->lane_count = lane_count;
3651 }
3652 
3653 void intel_dp_reset_link_params(struct intel_dp *intel_dp)
3654 {
3655 	intel_dp_link_caps_reset(intel_dp->link.caps);
3656 	intel_dp->link.mst_probed_lane_count = 0;
3657 	intel_dp->link.mst_probed_rate = 0;
3658 	intel_dp_link_training_reset(intel_dp->link.training);
3659 }
3660 
3661 /* Enable backlight PWM and backlight PP control. */
3662 void intel_edp_backlight_on(const struct intel_crtc_state *crtc_state,
3663 			    const struct drm_connector_state *conn_state)
3664 {
3665 	struct intel_display *display = to_intel_display(crtc_state);
3666 	struct intel_dp *intel_dp = enc_to_intel_dp(to_intel_encoder(conn_state->best_encoder));
3667 
3668 	if (!intel_dp_is_edp(intel_dp))
3669 		return;
3670 
3671 	drm_dbg_kms(display->drm, "\n");
3672 
3673 	intel_backlight_enable(crtc_state, conn_state);
3674 	intel_pps_backlight_on(intel_dp);
3675 }
3676 
3677 /* Disable backlight PP control and backlight PWM. */
3678 void intel_edp_backlight_off(const struct drm_connector_state *old_conn_state)
3679 {
3680 	struct intel_dp *intel_dp = enc_to_intel_dp(to_intel_encoder(old_conn_state->best_encoder));
3681 	struct intel_display *display = to_intel_display(intel_dp);
3682 
3683 	if (!intel_dp_is_edp(intel_dp))
3684 		return;
3685 
3686 	drm_dbg_kms(display->drm, "\n");
3687 
3688 	intel_pps_backlight_off(intel_dp);
3689 	intel_backlight_disable(old_conn_state);
3690 }
3691 
3692 static bool downstream_hpd_needs_d0(struct intel_dp *intel_dp)
3693 {
3694 	/*
3695 	 * DPCD 1.2+ should support BRANCH_DEVICE_CTRL, and thus
3696 	 * be capable of signalling downstream hpd with a long pulse.
3697 	 * Whether or not that means D3 is safe to use is not clear,
3698 	 * but let's assume so until proven otherwise.
3699 	 *
3700 	 * FIXME should really check all downstream ports...
3701 	 */
3702 	return intel_dp->dpcd[DP_DPCD_REV] == 0x11 &&
3703 		drm_dp_is_branch(intel_dp->dpcd) &&
3704 		intel_dp->downstream_ports[0] & DP_DS_PORT_HPD;
3705 }
3706 
3707 static int
3708 write_dsc_decompression_flag(struct drm_dp_aux *aux, u8 flag, bool set)
3709 {
3710 	int err;
3711 	u8 val;
3712 
3713 	err = drm_dp_dpcd_readb(aux, DP_DSC_ENABLE, &val);
3714 	if (err < 0)
3715 		return err;
3716 
3717 	if (set)
3718 		val |= flag;
3719 	else
3720 		val &= ~flag;
3721 
3722 	return drm_dp_dpcd_writeb(aux, DP_DSC_ENABLE, val);
3723 }
3724 
3725 static void
3726 intel_dp_sink_set_dsc_decompression(struct intel_connector *connector,
3727 				    bool enable)
3728 {
3729 	struct intel_display *display = to_intel_display(connector);
3730 
3731 	if (write_dsc_decompression_flag(connector->dp.dsc_decompression_aux,
3732 					 DP_DECOMPRESSION_EN, enable) < 0)
3733 		drm_dbg_kms(display->drm,
3734 			    "Failed to %s sink decompression state\n",
3735 			    str_enable_disable(enable));
3736 }
3737 
3738 static void
3739 intel_dp_sink_set_dsc_passthrough(const struct intel_connector *connector,
3740 				  bool enable)
3741 {
3742 	struct intel_display *display = to_intel_display(connector);
3743 	struct drm_dp_aux *aux = connector->mst.port ?
3744 				 connector->mst.port->passthrough_aux : NULL;
3745 
3746 	if (!aux)
3747 		return;
3748 
3749 	if (write_dsc_decompression_flag(aux,
3750 					 DP_DSC_PASSTHROUGH_EN, enable) < 0)
3751 		drm_dbg_kms(display->drm,
3752 			    "Failed to %s sink compression passthrough state\n",
3753 			    str_enable_disable(enable));
3754 }
3755 
3756 static int intel_dp_dsc_aux_ref_count(struct intel_atomic_state *state,
3757 				      const struct intel_connector *connector,
3758 				      bool for_get_ref)
3759 {
3760 	struct intel_display *display = to_intel_display(state);
3761 	struct drm_connector *_connector_iter;
3762 	struct drm_connector_state *old_conn_state;
3763 	struct drm_connector_state *new_conn_state;
3764 	int ref_count = 0;
3765 	int i;
3766 
3767 	/*
3768 	 * On SST the decompression AUX device won't be shared, each connector
3769 	 * uses for this its own AUX targeting the sink device.
3770 	 */
3771 	if (!connector->mst.dp)
3772 		return connector->dp.dsc_decompression_enabled ? 1 : 0;
3773 
3774 	for_each_oldnew_connector_in_state(&state->base, _connector_iter,
3775 					   old_conn_state, new_conn_state, i) {
3776 		const struct intel_connector *
3777 			connector_iter = to_intel_connector(_connector_iter);
3778 
3779 		if (connector_iter->mst.dp != connector->mst.dp)
3780 			continue;
3781 
3782 		if (!connector_iter->dp.dsc_decompression_enabled)
3783 			continue;
3784 
3785 		drm_WARN_ON(display->drm,
3786 			    (for_get_ref && !new_conn_state->crtc) ||
3787 			    (!for_get_ref && !old_conn_state->crtc));
3788 
3789 		if (connector_iter->dp.dsc_decompression_aux ==
3790 		    connector->dp.dsc_decompression_aux)
3791 			ref_count++;
3792 	}
3793 
3794 	return ref_count;
3795 }
3796 
3797 static bool intel_dp_dsc_aux_get_ref(struct intel_atomic_state *state,
3798 				     struct intel_connector *connector)
3799 {
3800 	bool ret = intel_dp_dsc_aux_ref_count(state, connector, true) == 0;
3801 
3802 	connector->dp.dsc_decompression_enabled = true;
3803 
3804 	return ret;
3805 }
3806 
3807 static bool intel_dp_dsc_aux_put_ref(struct intel_atomic_state *state,
3808 				     struct intel_connector *connector)
3809 {
3810 	connector->dp.dsc_decompression_enabled = false;
3811 
3812 	return intel_dp_dsc_aux_ref_count(state, connector, false) == 0;
3813 }
3814 
3815 /**
3816  * intel_dp_sink_enable_decompression - Enable DSC decompression in sink/last branch device
3817  * @state: atomic state
3818  * @connector: connector to enable the decompression for
3819  * @new_crtc_state: new state for the CRTC driving @connector
3820  *
3821  * Enable the DSC decompression if required in the %DP_DSC_ENABLE DPCD
3822  * register of the appropriate sink/branch device. On SST this is always the
3823  * sink device, whereas on MST based on each device's DSC capabilities it's
3824  * either the last branch device (enabling decompression in it) or both the
3825  * last branch device (enabling passthrough in it) and the sink device
3826  * (enabling decompression in it).
3827  */
3828 void intel_dp_sink_enable_decompression(struct intel_atomic_state *state,
3829 					struct intel_connector *connector,
3830 					const struct intel_crtc_state *new_crtc_state)
3831 {
3832 	struct intel_display *display = to_intel_display(state);
3833 
3834 	if (!new_crtc_state->dsc.compression_enable)
3835 		return;
3836 
3837 	if (drm_WARN_ON(display->drm,
3838 			!connector->dp.dsc_decompression_aux ||
3839 			connector->dp.dsc_decompression_enabled))
3840 		return;
3841 
3842 	if (!intel_dp_dsc_aux_get_ref(state, connector))
3843 		return;
3844 
3845 	intel_dp_sink_set_dsc_passthrough(connector, true);
3846 	intel_dp_sink_set_dsc_decompression(connector, true);
3847 }
3848 
3849 /**
3850  * intel_dp_sink_disable_decompression - Disable DSC decompression in sink/last branch device
3851  * @state: atomic state
3852  * @connector: connector to disable the decompression for
3853  * @old_crtc_state: old state for the CRTC driving @connector
3854  *
3855  * Disable the DSC decompression if required in the %DP_DSC_ENABLE DPCD
3856  * register of the appropriate sink/branch device, corresponding to the
3857  * sequence in intel_dp_sink_enable_decompression().
3858  */
3859 void intel_dp_sink_disable_decompression(struct intel_atomic_state *state,
3860 					 struct intel_connector *connector,
3861 					 const struct intel_crtc_state *old_crtc_state)
3862 {
3863 	struct intel_display *display = to_intel_display(state);
3864 
3865 	if (!old_crtc_state->dsc.compression_enable)
3866 		return;
3867 
3868 	if (drm_WARN_ON(display->drm,
3869 			!connector->dp.dsc_decompression_aux ||
3870 			!connector->dp.dsc_decompression_enabled))
3871 		return;
3872 
3873 	if (!intel_dp_dsc_aux_put_ref(state, connector))
3874 		return;
3875 
3876 	intel_dp_sink_set_dsc_decompression(connector, false);
3877 	intel_dp_sink_set_dsc_passthrough(connector, false);
3878 }
3879 
3880 static void
3881 intel_dp_init_source_oui(struct intel_dp *intel_dp)
3882 {
3883 	struct intel_display *display = to_intel_display(intel_dp);
3884 	u8 oui[] = { 0x00, 0xaa, 0x01 };
3885 	u8 buf[3] = {};
3886 
3887 	if (READ_ONCE(intel_dp->oui_valid))
3888 		return;
3889 
3890 	WRITE_ONCE(intel_dp->oui_valid, true);
3891 
3892 	/*
3893 	 * During driver init, we want to be careful and avoid changing the source OUI if it's
3894 	 * already set to what we want, so as to avoid clearing any state by accident
3895 	 */
3896 	if (drm_dp_dpcd_read(&intel_dp->aux, DP_SOURCE_OUI, buf, sizeof(buf)) < 0)
3897 		drm_dbg_kms(display->drm, "Failed to read source OUI\n");
3898 
3899 	if (memcmp(oui, buf, sizeof(oui)) == 0) {
3900 		/* Assume the OUI was written now. */
3901 		intel_dp->last_oui_write = jiffies;
3902 		return;
3903 	}
3904 
3905 	if (drm_dp_dpcd_write(&intel_dp->aux, DP_SOURCE_OUI, oui, sizeof(oui)) < 0) {
3906 		drm_dbg_kms(display->drm, "Failed to write source OUI\n");
3907 		WRITE_ONCE(intel_dp->oui_valid, false);
3908 	}
3909 
3910 	intel_dp->last_oui_write = jiffies;
3911 }
3912 
3913 void intel_dp_invalidate_source_oui(struct intel_dp *intel_dp)
3914 {
3915 	WRITE_ONCE(intel_dp->oui_valid, false);
3916 }
3917 
3918 void intel_dp_wait_source_oui(struct intel_dp *intel_dp)
3919 {
3920 	struct intel_display *display = to_intel_display(intel_dp);
3921 	struct intel_connector *connector = intel_dp->attached_connector;
3922 
3923 	drm_dbg_kms(display->drm,
3924 		    "[CONNECTOR:%d:%s] Performing OUI wait (%u ms)\n",
3925 		    connector->base.base.id, connector->base.name,
3926 		    connector->panel.vbt.backlight.hdr_dpcd_refresh_timeout);
3927 
3928 	wait_remaining_ms_from_jiffies(intel_dp->last_oui_write,
3929 				       connector->panel.vbt.backlight.hdr_dpcd_refresh_timeout);
3930 }
3931 
3932 /* If the device supports it, try to set the power state appropriately */
3933 void intel_dp_set_power(struct intel_dp *intel_dp, u8 mode)
3934 {
3935 	struct intel_display *display = to_intel_display(intel_dp);
3936 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3937 	int ret, i;
3938 
3939 	/* Should have a valid DPCD by this point */
3940 	if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
3941 		return;
3942 
3943 	if (mode != DP_SET_POWER_D0) {
3944 		if (downstream_hpd_needs_d0(intel_dp))
3945 			return;
3946 
3947 		ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER, mode);
3948 	} else {
3949 		struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
3950 
3951 		intel_lspcon_resume(dig_port);
3952 
3953 		/* Write the source OUI as early as possible */
3954 		intel_dp_init_source_oui(intel_dp);
3955 
3956 		/*
3957 		 * When turning on, we need to retry for 1ms to give the sink
3958 		 * time to wake up.
3959 		 */
3960 		for (i = 0; i < 3; i++) {
3961 			ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER, mode);
3962 			if (ret == 1)
3963 				break;
3964 			msleep(1);
3965 		}
3966 
3967 		if (ret == 1 && intel_lspcon_active(dig_port))
3968 			intel_lspcon_wait_pcon_mode(dig_port);
3969 	}
3970 
3971 	if (ret != 1)
3972 		drm_dbg_kms(display->drm,
3973 			    "[ENCODER:%d:%s] Set power to %s failed\n",
3974 			    encoder->base.base.id, encoder->base.name,
3975 			    mode == DP_SET_POWER_D0 ? "D0" : "D3");
3976 }
3977 
3978 static bool
3979 intel_dp_get_dpcd(struct intel_dp *intel_dp);
3980 
3981 /**
3982  * intel_dp_sync_state - sync the encoder state during init/resume
3983  * @encoder: intel encoder to sync
3984  * @crtc_state: state for the CRTC connected to the encoder
3985  *
3986  * Sync any state stored in the encoder wrt. HW state during driver init
3987  * and system resume.
3988  */
3989 void intel_dp_sync_state(struct intel_encoder *encoder,
3990 			 const struct intel_crtc_state *crtc_state)
3991 {
3992 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3993 	bool dpcd_updated = false;
3994 
3995 	/*
3996 	 * Don't clobber DPCD if it's been already read out during output
3997 	 * setup (eDP) or detect.
3998 	 */
3999 	if (crtc_state && intel_dp->dpcd[DP_DPCD_REV] == 0) {
4000 		intel_dp_get_dpcd(intel_dp);
4001 		dpcd_updated = true;
4002 	}
4003 
4004 	intel_dp_tunnel_resume(intel_dp, crtc_state, dpcd_updated);
4005 
4006 	if (crtc_state) {
4007 		intel_dp_reset_link_params(intel_dp);
4008 		intel_dp_set_link_params(intel_dp, crtc_state->port_clock, crtc_state->lane_count);
4009 		intel_dp->link.active = true;
4010 	}
4011 }
4012 
4013 bool intel_dp_initial_fastset_check(struct intel_encoder *encoder,
4014 				    struct intel_crtc_state *crtc_state)
4015 {
4016 	struct intel_display *display = to_intel_display(encoder);
4017 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4018 	bool fastset = true;
4019 
4020 	/*
4021 	 * If BIOS has set an unsupported or non-standard link rate for some
4022 	 * reason force an encoder recompute and full modeset.
4023 	 */
4024 	if (intel_dp_rate_index(intel_dp->source_rates, intel_dp->num_source_rates,
4025 				crtc_state->port_clock) < 0) {
4026 		drm_dbg_kms(display->drm,
4027 			    "[ENCODER:%d:%s] Forcing full modeset due to unsupported link rate\n",
4028 			    encoder->base.base.id, encoder->base.name);
4029 		crtc_state->uapi.connectors_changed = true;
4030 		fastset = false;
4031 	}
4032 
4033 	/*
4034 	 * FIXME hack to force full modeset when DSC is being used.
4035 	 *
4036 	 * As long as we do not have full state readout and config comparison
4037 	 * of crtc_state->dsc, we have no way to ensure reliable fastset.
4038 	 * Remove once we have readout for DSC.
4039 	 */
4040 	if (crtc_state->dsc.compression_enable) {
4041 		drm_dbg_kms(display->drm,
4042 			    "[ENCODER:%d:%s] Forcing full modeset due to DSC being enabled\n",
4043 			    encoder->base.base.id, encoder->base.name);
4044 		crtc_state->uapi.mode_changed = true;
4045 		fastset = false;
4046 	}
4047 
4048 	if (CAN_PANEL_REPLAY(intel_dp)) {
4049 		drm_dbg_kms(display->drm,
4050 			    "[ENCODER:%d:%s] Forcing full modeset to compute panel replay state\n",
4051 			    encoder->base.base.id, encoder->base.name);
4052 		crtc_state->uapi.mode_changed = true;
4053 		fastset = false;
4054 	}
4055 
4056 	return fastset;
4057 }
4058 
4059 static void intel_dp_get_pcon_dsc_cap(struct intel_dp *intel_dp)
4060 {
4061 	struct intel_display *display = to_intel_display(intel_dp);
4062 
4063 	/* Clear the cached register set to avoid using stale values */
4064 
4065 	memset(intel_dp->pcon_dsc_dpcd, 0, sizeof(intel_dp->pcon_dsc_dpcd));
4066 
4067 	if (!drm_dp_is_branch(intel_dp->dpcd))
4068 		return;
4069 
4070 	if (drm_dp_dpcd_read(&intel_dp->aux, DP_PCON_DSC_ENCODER,
4071 			     intel_dp->pcon_dsc_dpcd,
4072 			     sizeof(intel_dp->pcon_dsc_dpcd)) < 0)
4073 		drm_err(display->drm, "Failed to read DPCD register 0x%x\n",
4074 			DP_PCON_DSC_ENCODER);
4075 
4076 	drm_dbg_kms(display->drm, "PCON ENCODER DSC DPCD: %*ph\n",
4077 		    (int)sizeof(intel_dp->pcon_dsc_dpcd), intel_dp->pcon_dsc_dpcd);
4078 }
4079 
4080 static int intel_dp_pcon_get_frl_mask(u8 frl_bw_mask)
4081 {
4082 	static const int bw_gbps[] = {9, 18, 24, 32, 40, 48};
4083 	int i;
4084 
4085 	for (i = ARRAY_SIZE(bw_gbps) - 1; i >= 0; i--) {
4086 		if (frl_bw_mask & (1 << i))
4087 			return bw_gbps[i];
4088 	}
4089 	return 0;
4090 }
4091 
4092 static int intel_dp_pcon_set_frl_mask(int max_frl)
4093 {
4094 	switch (max_frl) {
4095 	case 48:
4096 		return DP_PCON_FRL_BW_MASK_48GBPS;
4097 	case 40:
4098 		return DP_PCON_FRL_BW_MASK_40GBPS;
4099 	case 32:
4100 		return DP_PCON_FRL_BW_MASK_32GBPS;
4101 	case 24:
4102 		return DP_PCON_FRL_BW_MASK_24GBPS;
4103 	case 18:
4104 		return DP_PCON_FRL_BW_MASK_18GBPS;
4105 	case 9:
4106 		return DP_PCON_FRL_BW_MASK_9GBPS;
4107 	}
4108 
4109 	return 0;
4110 }
4111 
4112 static int intel_dp_hdmi_sink_max_frl(struct intel_dp *intel_dp)
4113 {
4114 	struct intel_connector *connector = intel_dp->attached_connector;
4115 	const struct drm_display_info *info = &connector->base.display_info;
4116 	int max_frl_rate;
4117 	int max_lanes, rate_per_lane;
4118 	int max_dsc_lanes, dsc_rate_per_lane;
4119 
4120 	max_lanes = info->hdmi.max_lanes;
4121 	rate_per_lane = info->hdmi.max_frl_rate_per_lane;
4122 	max_frl_rate = max_lanes * rate_per_lane;
4123 
4124 	if (info->hdmi.dsc_cap.v_1p2) {
4125 		max_dsc_lanes = info->hdmi.dsc_cap.max_lanes;
4126 		dsc_rate_per_lane = info->hdmi.dsc_cap.max_frl_rate_per_lane;
4127 		if (max_dsc_lanes && dsc_rate_per_lane)
4128 			max_frl_rate = min(max_frl_rate, max_dsc_lanes * dsc_rate_per_lane);
4129 	}
4130 
4131 	return max_frl_rate;
4132 }
4133 
4134 static bool
4135 intel_dp_pcon_is_frl_trained(struct intel_dp *intel_dp,
4136 			     u8 max_frl_bw_mask, u8 *frl_trained_mask)
4137 {
4138 	if (drm_dp_pcon_hdmi_link_active(&intel_dp->aux) &&
4139 	    drm_dp_pcon_hdmi_link_mode(&intel_dp->aux, frl_trained_mask) == DP_PCON_HDMI_MODE_FRL &&
4140 	    *frl_trained_mask >= max_frl_bw_mask)
4141 		return true;
4142 
4143 	return false;
4144 }
4145 
4146 static int intel_dp_pcon_start_frl_training(struct intel_dp *intel_dp)
4147 {
4148 	struct intel_display *display = to_intel_display(intel_dp);
4149 #define TIMEOUT_FRL_READY_MS 500
4150 #define TIMEOUT_HDMI_LINK_ACTIVE_MS 1000
4151 	int max_frl_bw, max_pcon_frl_bw, max_edid_frl_bw, ret;
4152 	u8 max_frl_bw_mask = 0, frl_trained_mask;
4153 	bool is_active;
4154 
4155 	max_pcon_frl_bw = intel_dp->dfp.pcon_max_frl_bw;
4156 	drm_dbg(display->drm, "PCON max rate = %d Gbps\n", max_pcon_frl_bw);
4157 
4158 	max_edid_frl_bw = intel_dp_hdmi_sink_max_frl(intel_dp);
4159 	drm_dbg(display->drm, "Sink max rate from EDID = %d Gbps\n",
4160 		max_edid_frl_bw);
4161 
4162 	max_frl_bw = min(max_edid_frl_bw, max_pcon_frl_bw);
4163 
4164 	if (max_frl_bw <= 0)
4165 		return -EINVAL;
4166 
4167 	max_frl_bw_mask = intel_dp_pcon_set_frl_mask(max_frl_bw);
4168 	drm_dbg(display->drm, "MAX_FRL_BW_MASK = %u\n", max_frl_bw_mask);
4169 
4170 	if (intel_dp_pcon_is_frl_trained(intel_dp, max_frl_bw_mask, &frl_trained_mask))
4171 		goto frl_trained;
4172 
4173 	ret = drm_dp_pcon_frl_prepare(&intel_dp->aux, false);
4174 	if (ret < 0)
4175 		return ret;
4176 	/* Wait for PCON to be FRL Ready */
4177 	ret = poll_timeout_us(is_active = drm_dp_pcon_is_frl_ready(&intel_dp->aux),
4178 			      is_active,
4179 			      1000, TIMEOUT_FRL_READY_MS * 1000, false);
4180 	if (ret)
4181 		return ret;
4182 
4183 	ret = drm_dp_pcon_frl_configure_1(&intel_dp->aux, max_frl_bw,
4184 					  DP_PCON_ENABLE_SEQUENTIAL_LINK);
4185 	if (ret < 0)
4186 		return ret;
4187 	ret = drm_dp_pcon_frl_configure_2(&intel_dp->aux, max_frl_bw_mask,
4188 					  DP_PCON_FRL_LINK_TRAIN_NORMAL);
4189 	if (ret < 0)
4190 		return ret;
4191 	ret = drm_dp_pcon_frl_enable(&intel_dp->aux);
4192 	if (ret < 0)
4193 		return ret;
4194 	/*
4195 	 * Wait for FRL to be completed
4196 	 * Check if the HDMI Link is up and active.
4197 	 */
4198 	ret = poll_timeout_us(is_active = intel_dp_pcon_is_frl_trained(intel_dp, max_frl_bw_mask, &frl_trained_mask),
4199 			      is_active,
4200 			      1000, TIMEOUT_HDMI_LINK_ACTIVE_MS * 1000, false);
4201 	if (ret)
4202 		return ret;
4203 
4204 frl_trained:
4205 	drm_dbg(display->drm, "FRL_TRAINED_MASK = %u\n", frl_trained_mask);
4206 	intel_dp->frl.trained_rate_gbps = intel_dp_pcon_get_frl_mask(frl_trained_mask);
4207 	intel_dp->frl.is_trained = true;
4208 	drm_dbg(display->drm, "FRL trained with : %d Gbps\n",
4209 		intel_dp->frl.trained_rate_gbps);
4210 
4211 	return 0;
4212 }
4213 
4214 static bool intel_dp_is_hdmi_2_1_sink(struct intel_dp *intel_dp)
4215 {
4216 	if (drm_dp_is_branch(intel_dp->dpcd) &&
4217 	    intel_dp_has_hdmi_sink(intel_dp) &&
4218 	    intel_dp_hdmi_sink_max_frl(intel_dp) > 0)
4219 		return true;
4220 
4221 	return false;
4222 }
4223 
4224 static
4225 int intel_dp_pcon_set_tmds_mode(struct intel_dp *intel_dp)
4226 {
4227 	int ret;
4228 	u8 buf = 0;
4229 
4230 	/* Set PCON source control mode */
4231 	buf |= DP_PCON_ENABLE_SOURCE_CTL_MODE;
4232 
4233 	ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
4234 	if (ret < 0)
4235 		return ret;
4236 
4237 	/* Set HDMI LINK ENABLE */
4238 	buf |= DP_PCON_ENABLE_HDMI_LINK;
4239 	ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
4240 	if (ret < 0)
4241 		return ret;
4242 
4243 	return 0;
4244 }
4245 
4246 void intel_dp_check_frl_training(struct intel_dp *intel_dp)
4247 {
4248 	struct intel_display *display = to_intel_display(intel_dp);
4249 
4250 	/*
4251 	 * Always go for FRL training if:
4252 	 * -PCON supports SRC_CTL_MODE (VESA DP2.0-HDMI2.1 PCON Spec Draft-1 Sec-7)
4253 	 * -sink is HDMI2.1
4254 	 */
4255 	if (!(intel_dp->downstream_ports[2] & DP_PCON_SOURCE_CTL_MODE) ||
4256 	    !intel_dp_is_hdmi_2_1_sink(intel_dp) ||
4257 	    intel_dp->frl.is_trained)
4258 		return;
4259 
4260 	if (intel_dp_pcon_start_frl_training(intel_dp) < 0) {
4261 		int ret, mode;
4262 
4263 		drm_dbg(display->drm,
4264 			"Couldn't set FRL mode, continuing with TMDS mode\n");
4265 		ret = intel_dp_pcon_set_tmds_mode(intel_dp);
4266 		mode = drm_dp_pcon_hdmi_link_mode(&intel_dp->aux, NULL);
4267 
4268 		if (ret < 0 || mode != DP_PCON_HDMI_MODE_TMDS)
4269 			drm_dbg(display->drm,
4270 				"Issue with PCON, cannot set TMDS mode\n");
4271 	} else {
4272 		drm_dbg(display->drm, "FRL training Completed\n");
4273 	}
4274 }
4275 
4276 static int
4277 intel_dp_pcon_dsc_enc_slice_height(const struct intel_crtc_state *crtc_state)
4278 {
4279 	int vactive = crtc_state->hw.adjusted_mode.vdisplay;
4280 
4281 	return intel_hdmi_dsc_get_slice_height(vactive);
4282 }
4283 
4284 static int
4285 intel_dp_pcon_dsc_enc_slices(struct intel_dp *intel_dp,
4286 			     const struct intel_crtc_state *crtc_state)
4287 {
4288 	struct intel_connector *connector = intel_dp->attached_connector;
4289 	const struct drm_display_info *info = &connector->base.display_info;
4290 	int hdmi_throughput = info->hdmi.dsc_cap.clk_per_slice;
4291 	int hdmi_max_slices = info->hdmi.dsc_cap.max_slices;
4292 	int pcon_max_slices = drm_dp_pcon_dsc_max_slices(intel_dp->pcon_dsc_dpcd);
4293 	int pcon_max_slice_width = drm_dp_pcon_dsc_max_slice_width(intel_dp->pcon_dsc_dpcd);
4294 
4295 	return intel_hdmi_dsc_get_num_slices(crtc_state, pcon_max_slices,
4296 					     pcon_max_slice_width,
4297 					     hdmi_max_slices, hdmi_throughput);
4298 }
4299 
4300 static int
4301 intel_dp_pcon_dsc_enc_bpp(struct intel_dp *intel_dp,
4302 			  const struct intel_crtc_state *crtc_state,
4303 			  int num_slices, int slice_width)
4304 {
4305 	struct intel_connector *connector = intel_dp->attached_connector;
4306 	const struct drm_display_info *info = &connector->base.display_info;
4307 	int output_format = crtc_state->output_format;
4308 	bool hdmi_all_bpp = info->hdmi.dsc_cap.all_bpp;
4309 	int pcon_fractional_bpp = drm_dp_pcon_dsc_bpp_incr(intel_dp->pcon_dsc_dpcd);
4310 	int hdmi_max_chunk_bytes =
4311 		info->hdmi.dsc_cap.total_chunk_kbytes * 1024;
4312 
4313 	return intel_hdmi_dsc_get_bpp(pcon_fractional_bpp, slice_width,
4314 				      num_slices, output_format, hdmi_all_bpp,
4315 				      hdmi_max_chunk_bytes);
4316 }
4317 
4318 void
4319 intel_dp_pcon_dsc_configure(struct intel_dp *intel_dp,
4320 			    const struct intel_crtc_state *crtc_state)
4321 {
4322 	struct intel_display *display = to_intel_display(intel_dp);
4323 	struct intel_connector *connector = intel_dp->attached_connector;
4324 	const struct drm_display_info *info;
4325 	u8 pps_param[6];
4326 	int slice_height;
4327 	int slice_width;
4328 	int num_slices;
4329 	int bits_per_pixel;
4330 	int ret;
4331 	bool hdmi_is_dsc_1_2;
4332 
4333 	if (!intel_dp_is_hdmi_2_1_sink(intel_dp))
4334 		return;
4335 
4336 	if (!connector)
4337 		return;
4338 
4339 	info = &connector->base.display_info;
4340 
4341 	hdmi_is_dsc_1_2 = info->hdmi.dsc_cap.v_1p2;
4342 
4343 	if (!drm_dp_pcon_enc_is_dsc_1_2(intel_dp->pcon_dsc_dpcd) ||
4344 	    !hdmi_is_dsc_1_2)
4345 		return;
4346 
4347 	slice_height = intel_dp_pcon_dsc_enc_slice_height(crtc_state);
4348 	if (!slice_height)
4349 		return;
4350 
4351 	num_slices = intel_dp_pcon_dsc_enc_slices(intel_dp, crtc_state);
4352 	if (!num_slices)
4353 		return;
4354 
4355 	slice_width = DIV_ROUND_UP(crtc_state->hw.adjusted_mode.hdisplay,
4356 				   num_slices);
4357 
4358 	bits_per_pixel = intel_dp_pcon_dsc_enc_bpp(intel_dp, crtc_state,
4359 						   num_slices, slice_width);
4360 	if (!bits_per_pixel)
4361 		return;
4362 
4363 	pps_param[0] = slice_height & 0xFF;
4364 	pps_param[1] = slice_height >> 8;
4365 	pps_param[2] = slice_width & 0xFF;
4366 	pps_param[3] = slice_width >> 8;
4367 	pps_param[4] = bits_per_pixel & 0xFF;
4368 	pps_param[5] = (bits_per_pixel >> 8) & 0x3;
4369 
4370 	ret = drm_dp_pcon_pps_override_param(&intel_dp->aux, pps_param);
4371 	if (ret < 0)
4372 		drm_dbg_kms(display->drm, "Failed to set pcon DSC\n");
4373 }
4374 
4375 void intel_dp_configure_protocol_converter(struct intel_dp *intel_dp,
4376 					   const struct intel_crtc_state *crtc_state)
4377 {
4378 	struct intel_display *display = to_intel_display(intel_dp);
4379 	bool ycbcr444_to_420 = false;
4380 	bool rgb_to_ycbcr = false;
4381 	u8 tmp;
4382 
4383 	if (intel_dp->dpcd[DP_DPCD_REV] < 0x13)
4384 		return;
4385 
4386 	if (!drm_dp_is_branch(intel_dp->dpcd))
4387 		return;
4388 
4389 	tmp = intel_dp_has_hdmi_sink(intel_dp) ? DP_HDMI_DVI_OUTPUT_CONFIG : 0;
4390 
4391 	if (drm_dp_dpcd_writeb(&intel_dp->aux,
4392 			       DP_PROTOCOL_CONVERTER_CONTROL_0, tmp) != 1)
4393 		drm_dbg_kms(display->drm,
4394 			    "Failed to %s protocol converter HDMI mode\n",
4395 			    str_enable_disable(intel_dp_has_hdmi_sink(intel_dp)));
4396 
4397 	if (crtc_state->sink_format == INTEL_OUTPUT_FORMAT_YCBCR420) {
4398 		switch (crtc_state->output_format) {
4399 		case INTEL_OUTPUT_FORMAT_YCBCR420:
4400 			break;
4401 		case INTEL_OUTPUT_FORMAT_YCBCR444:
4402 			ycbcr444_to_420 = true;
4403 			break;
4404 		case INTEL_OUTPUT_FORMAT_RGB:
4405 			rgb_to_ycbcr = true;
4406 			ycbcr444_to_420 = true;
4407 			break;
4408 		default:
4409 			MISSING_CASE(crtc_state->output_format);
4410 			break;
4411 		}
4412 	} else if (crtc_state->sink_format == INTEL_OUTPUT_FORMAT_YCBCR444) {
4413 		switch (crtc_state->output_format) {
4414 		case INTEL_OUTPUT_FORMAT_YCBCR444:
4415 			break;
4416 		case INTEL_OUTPUT_FORMAT_RGB:
4417 			rgb_to_ycbcr = true;
4418 			break;
4419 		default:
4420 			MISSING_CASE(crtc_state->output_format);
4421 			break;
4422 		}
4423 	}
4424 
4425 	tmp = ycbcr444_to_420 ? DP_CONVERSION_TO_YCBCR420_ENABLE : 0;
4426 
4427 	if (drm_dp_dpcd_writeb(&intel_dp->aux,
4428 			       DP_PROTOCOL_CONVERTER_CONTROL_1, tmp) != 1)
4429 		drm_dbg_kms(display->drm,
4430 			    "Failed to %s protocol converter YCbCr 4:2:0 conversion mode\n",
4431 			    str_enable_disable(intel_dp->dfp.ycbcr_444_to_420));
4432 
4433 	tmp = rgb_to_ycbcr ? DP_CONVERSION_BT709_RGB_YCBCR_ENABLE : 0;
4434 
4435 	if (drm_dp_pcon_convert_rgb_to_ycbcr(&intel_dp->aux, tmp) < 0)
4436 		drm_dbg_kms(display->drm,
4437 			    "Failed to %s protocol converter RGB->YCbCr conversion mode\n",
4438 			    str_enable_disable(tmp));
4439 }
4440 
4441 static bool intel_dp_get_colorimetry_status(struct intel_dp *intel_dp)
4442 {
4443 	u8 dprx = 0;
4444 
4445 	if (drm_dp_dpcd_readb(&intel_dp->aux, DP_DPRX_FEATURE_ENUMERATION_LIST,
4446 			      &dprx) != 1)
4447 		return false;
4448 	return dprx & DP_VSC_SDP_EXT_FOR_COLORIMETRY_SUPPORTED;
4449 }
4450 
4451 static int intel_dp_read_dsc_dpcd(struct drm_dp_aux *aux,
4452 				  u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE])
4453 {
4454 	int ret;
4455 
4456 	ret = drm_dp_dpcd_read_data(aux, DP_DSC_SUPPORT, dsc_dpcd,
4457 				    DP_DSC_RECEIVER_CAP_SIZE);
4458 	if (ret) {
4459 		drm_dbg_kms(aux->drm_dev,
4460 			    "Could not read DSC DPCD register 0x%x Error: %pe\n",
4461 			    DP_DSC_SUPPORT, ERR_PTR(ret));
4462 		return ret;
4463 	}
4464 
4465 	drm_dbg_kms(aux->drm_dev, "DSC DPCD: %*ph\n",
4466 		    DP_DSC_RECEIVER_CAP_SIZE,
4467 		    dsc_dpcd);
4468 	return 0;
4469 }
4470 
4471 static void init_dsc_overall_throughput_limits(struct intel_connector *connector, bool is_branch)
4472 {
4473 	u8 branch_caps[DP_DSC_BRANCH_CAP_SIZE];
4474 	int line_width;
4475 
4476 	connector->dp.dsc_branch_caps.overall_throughput.rgb_yuv444 = INT_MAX;
4477 	connector->dp.dsc_branch_caps.overall_throughput.yuv422_420 = INT_MAX;
4478 	connector->dp.dsc_branch_caps.max_line_width = INT_MAX;
4479 
4480 	if (!is_branch)
4481 		return;
4482 
4483 	if (drm_dp_dpcd_read_data(connector->dp.dsc_decompression_aux,
4484 				  DP_DSC_BRANCH_OVERALL_THROUGHPUT_0, branch_caps,
4485 				  sizeof(branch_caps)) != 0)
4486 		return;
4487 
4488 	connector->dp.dsc_branch_caps.overall_throughput.rgb_yuv444 =
4489 		drm_dp_dsc_branch_max_overall_throughput(branch_caps, true) ? : INT_MAX;
4490 
4491 	connector->dp.dsc_branch_caps.overall_throughput.yuv422_420 =
4492 		drm_dp_dsc_branch_max_overall_throughput(branch_caps, false) ? : INT_MAX;
4493 
4494 	line_width = drm_dp_dsc_branch_max_line_width(branch_caps);
4495 	connector->dp.dsc_branch_caps.max_line_width = line_width > 0 ? line_width : INT_MAX;
4496 }
4497 
4498 void intel_dp_get_dsc_sink_cap(u8 dpcd_rev,
4499 			       const struct drm_dp_desc *desc, bool is_branch,
4500 			       struct intel_connector *connector)
4501 {
4502 	struct intel_display *display = to_intel_display(connector);
4503 
4504 	/*
4505 	 * Clear the cached register set to avoid using stale values
4506 	 * for the sinks that do not support DSC.
4507 	 */
4508 	memset(connector->dp.dsc_dpcd, 0, sizeof(connector->dp.dsc_dpcd));
4509 
4510 	/* Clear fec_capable to avoid using stale values */
4511 	connector->dp.fec_capability = 0;
4512 
4513 	memset(&connector->dp.dsc_branch_caps, 0, sizeof(connector->dp.dsc_branch_caps));
4514 	connector->dp.dsc_throughput_quirk = false;
4515 
4516 	if (dpcd_rev < DP_DPCD_REV_14)
4517 		return;
4518 
4519 	if (intel_dp_read_dsc_dpcd(connector->dp.dsc_decompression_aux,
4520 				   connector->dp.dsc_dpcd) < 0)
4521 		return;
4522 
4523 	if (drm_dp_dpcd_readb(connector->dp.dsc_decompression_aux, DP_FEC_CAPABILITY,
4524 			      &connector->dp.fec_capability) < 0) {
4525 		drm_dbg_kms(display->drm, "Could not read FEC DPCD register\n");
4526 		return;
4527 	}
4528 
4529 	drm_dbg_kms(display->drm, "FEC CAPABILITY: %x\n",
4530 		    connector->dp.fec_capability);
4531 
4532 	if (!(connector->dp.dsc_dpcd[0] & DP_DSC_DECOMPRESSION_IS_SUPPORTED))
4533 		return;
4534 
4535 	init_dsc_overall_throughput_limits(connector, is_branch);
4536 
4537 	/*
4538 	 * TODO: Move the HW rev check as well to the DRM core quirk table if
4539 	 * that's required after clarifying the list of affected devices.
4540 	 */
4541 	if (drm_dp_has_quirk(desc, DP_DPCD_QUIRK_DSC_THROUGHPUT_BPP_LIMIT) &&
4542 	    desc->ident.hw_rev == 0x10)
4543 		connector->dp.dsc_throughput_quirk = true;
4544 }
4545 
4546 static void intel_edp_get_dsc_sink_cap(u8 edp_dpcd_rev, struct intel_connector *connector)
4547 {
4548 	if (edp_dpcd_rev < DP_EDP_14)
4549 		return;
4550 
4551 	if (intel_dp_read_dsc_dpcd(connector->dp.dsc_decompression_aux,
4552 				   connector->dp.dsc_dpcd) < 0)
4553 		return;
4554 
4555 	if (connector->dp.dsc_dpcd[0] & DP_DSC_DECOMPRESSION_IS_SUPPORTED)
4556 		init_dsc_overall_throughput_limits(connector, false);
4557 }
4558 
4559 static void
4560 intel_dp_detect_dsc_caps(struct intel_dp *intel_dp, struct intel_connector *connector)
4561 {
4562 	struct intel_display *display = to_intel_display(intel_dp);
4563 
4564 	/* Read DP Sink DSC Cap DPCD regs for DP v1.4 */
4565 	if (!HAS_DSC(display))
4566 		return;
4567 
4568 	if (intel_dp_is_edp(intel_dp))
4569 		intel_edp_get_dsc_sink_cap(intel_dp->edp_dpcd[0],
4570 					   connector);
4571 	else
4572 		intel_dp_get_dsc_sink_cap(intel_dp->dpcd[DP_DPCD_REV],
4573 					  &intel_dp->desc, drm_dp_is_branch(intel_dp->dpcd),
4574 					  connector);
4575 }
4576 
4577 static void intel_edp_mso_mode_fixup(struct intel_connector *connector,
4578 				     struct drm_display_mode *mode)
4579 {
4580 	struct intel_display *display = to_intel_display(connector);
4581 	struct intel_dp *intel_dp = intel_attached_dp(connector);
4582 	int n = intel_dp->mso_link_count;
4583 	int overlap = intel_dp->mso_pixel_overlap;
4584 
4585 	if (!mode || !n)
4586 		return;
4587 
4588 	mode->hdisplay = (mode->hdisplay - overlap) * n;
4589 	mode->hsync_start = (mode->hsync_start - overlap) * n;
4590 	mode->hsync_end = (mode->hsync_end - overlap) * n;
4591 	mode->htotal = (mode->htotal - overlap) * n;
4592 	mode->clock *= n;
4593 
4594 	drm_mode_set_name(mode);
4595 
4596 	drm_dbg_kms(display->drm,
4597 		    "[CONNECTOR:%d:%s] using generated MSO mode: " DRM_MODE_FMT "\n",
4598 		    connector->base.base.id, connector->base.name,
4599 		    DRM_MODE_ARG(mode));
4600 }
4601 
4602 void intel_edp_fixup_vbt_bpp(struct intel_encoder *encoder, int pipe_bpp)
4603 {
4604 	struct intel_display *display = to_intel_display(encoder);
4605 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4606 	struct intel_connector *connector = intel_dp->attached_connector;
4607 
4608 	if (connector->panel.vbt.edp.bpp && pipe_bpp > connector->panel.vbt.edp.bpp) {
4609 		/*
4610 		 * This is a big fat ugly hack.
4611 		 *
4612 		 * Some machines in UEFI boot mode provide us a VBT that has 18
4613 		 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
4614 		 * unknown we fail to light up. Yet the same BIOS boots up with
4615 		 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
4616 		 * max, not what it tells us to use.
4617 		 *
4618 		 * Note: This will still be broken if the eDP panel is not lit
4619 		 * up by the BIOS, and thus we can't get the mode at module
4620 		 * load.
4621 		 */
4622 		drm_dbg_kms(display->drm,
4623 			    "pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
4624 			    pipe_bpp, connector->panel.vbt.edp.bpp);
4625 		connector->panel.vbt.edp.bpp = pipe_bpp;
4626 	}
4627 }
4628 
4629 static void intel_edp_mso_init(struct intel_dp *intel_dp)
4630 {
4631 	struct intel_display *display = to_intel_display(intel_dp);
4632 	struct intel_connector *connector = intel_dp->attached_connector;
4633 	struct drm_display_info *info = &connector->base.display_info;
4634 	u8 mso;
4635 
4636 	if (intel_dp->edp_dpcd[0] < DP_EDP_14)
4637 		return;
4638 
4639 	if (drm_dp_dpcd_readb(&intel_dp->aux, DP_EDP_MSO_LINK_CAPABILITIES, &mso) != 1) {
4640 		drm_err(display->drm, "Failed to read MSO cap\n");
4641 		return;
4642 	}
4643 
4644 	/* Valid configurations are SST or MSO 2x1, 2x2, 4x1 */
4645 	mso &= DP_EDP_MSO_NUMBER_OF_LINKS_MASK;
4646 	if (mso % 2 || mso > drm_dp_max_lane_count(intel_dp->dpcd)) {
4647 		drm_err(display->drm, "Invalid MSO link count cap %u\n", mso);
4648 		mso = 0;
4649 	}
4650 
4651 	if (mso) {
4652 		drm_dbg_kms(display->drm,
4653 			    "Sink MSO %ux%u configuration, pixel overlap %u\n",
4654 			    mso, drm_dp_max_lane_count(intel_dp->dpcd) / mso,
4655 			    info->mso_pixel_overlap);
4656 		if (!HAS_MSO(display)) {
4657 			drm_err(display->drm,
4658 				"No source MSO support, disabling\n");
4659 			mso = 0;
4660 		}
4661 	}
4662 
4663 	intel_dp->mso_link_count = mso;
4664 	intel_dp->mso_pixel_overlap = mso ? info->mso_pixel_overlap : 0;
4665 }
4666 
4667 static void
4668 intel_edp_set_data_override_rates(struct intel_dp *intel_dp)
4669 {
4670 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
4671 	int *sink_rates = intel_dp->sink_rates;
4672 	int i, count = 0;
4673 
4674 	for (i = 0; i < intel_dp->num_sink_rates; i++) {
4675 		if (intel_bios_encoder_reject_edp_rate(encoder->devdata,
4676 						       intel_dp->sink_rates[i]))
4677 			continue;
4678 
4679 		sink_rates[count++] = intel_dp->sink_rates[i];
4680 	}
4681 	intel_dp->num_sink_rates = count;
4682 }
4683 
4684 static void
4685 intel_edp_set_sink_rates(struct intel_dp *intel_dp)
4686 {
4687 	struct intel_display *display = to_intel_display(intel_dp);
4688 
4689 	intel_dp->num_sink_rates = 0;
4690 
4691 	if (intel_dp->edp_dpcd[0] >= DP_EDP_14) {
4692 		__le16 sink_rates[DP_MAX_SUPPORTED_RATES];
4693 		int ret;
4694 		int i;
4695 
4696 		ret = drm_dp_dpcd_read_data(&intel_dp->aux,
4697 					    DP_SUPPORTED_LINK_RATES,
4698 					    sink_rates, sizeof(sink_rates));
4699 		if (ret < 0) {
4700 			drm_dbg_kms(display->drm,
4701 				    "Unable to read eDP supported link rates, using default rates\n");
4702 			memset(sink_rates, 0, sizeof(sink_rates));
4703 		}
4704 
4705 		for (i = 0; i < ARRAY_SIZE(sink_rates); i++) {
4706 			int rate;
4707 
4708 			/* Value read multiplied by 200kHz gives the per-lane
4709 			 * link rate in kHz. The source rates are, however,
4710 			 * stored in terms of LS_Clk kHz. The full conversion
4711 			 * back to symbols is
4712 			 * (val * 200kHz)*(8/10 ch. encoding)*(1/8 bit to Byte)
4713 			 */
4714 			rate = le16_to_cpu(sink_rates[i]) * 200 / 10;
4715 
4716 			if (rate == 0)
4717 				break;
4718 
4719 			/*
4720 			 * Some platforms cannot reliably drive HBR3 rates due to PHY limitations,
4721 			 * even if the sink advertises support. Reject any sink rates above HBR2 on
4722 			 * the known machines for stable output.
4723 			 */
4724 			if (rate > 540000 &&
4725 			    intel_has_quirk(display, QUIRK_EDP_LIMIT_RATE_HBR2))
4726 				break;
4727 
4728 			intel_dp->sink_rates[i] = rate;
4729 		}
4730 		intel_dp->num_sink_rates = i;
4731 	}
4732 
4733 	/*
4734 	 * Use DP_LINK_RATE_SET if DP_SUPPORTED_LINK_RATES are available,
4735 	 * default to DP_MAX_LINK_RATE and DP_LINK_BW_SET otherwise.
4736 	 */
4737 	if (intel_dp->num_sink_rates)
4738 		intel_dp->use_rate_select = true;
4739 	else
4740 		intel_dp_set_sink_rates(intel_dp);
4741 
4742 	intel_edp_set_data_override_rates(intel_dp);
4743 }
4744 
4745 static bool
4746 intel_edp_init_dpcd(struct intel_dp *intel_dp, struct intel_connector *connector)
4747 {
4748 	struct intel_display *display = to_intel_display(intel_dp);
4749 	int ret;
4750 
4751 	/* this function is meant to be called only once */
4752 	drm_WARN_ON(display->drm, intel_dp->dpcd[DP_DPCD_REV] != 0);
4753 
4754 	if (drm_dp_read_dpcd_caps(&intel_dp->aux, intel_dp->dpcd) != 0)
4755 		return false;
4756 
4757 	drm_dp_read_desc(&intel_dp->aux, &intel_dp->desc,
4758 			 drm_dp_is_branch(intel_dp->dpcd));
4759 	intel_init_dpcd_quirks(intel_dp, &intel_dp->desc.ident);
4760 
4761 	intel_dp->colorimetry_support =
4762 		intel_dp_get_colorimetry_status(intel_dp);
4763 
4764 	/*
4765 	 * Read the eDP display control registers.
4766 	 *
4767 	 * Do this independent of DP_DPCD_DISPLAY_CONTROL_CAPABLE bit in
4768 	 * DP_EDP_CONFIGURATION_CAP, because some buggy displays do not have it
4769 	 * set, but require eDP 1.4+ detection (e.g. for supported link rates
4770 	 * method). The display control registers should read zero if they're
4771 	 * not supported anyway.
4772 	 */
4773 	if (drm_dp_dpcd_read(&intel_dp->aux, DP_EDP_DPCD_REV,
4774 			     intel_dp->edp_dpcd, sizeof(intel_dp->edp_dpcd)) ==
4775 			     sizeof(intel_dp->edp_dpcd)) {
4776 		drm_dbg_kms(display->drm, "eDP DPCD: %*ph\n",
4777 			    (int)sizeof(intel_dp->edp_dpcd),
4778 			    intel_dp->edp_dpcd);
4779 
4780 		intel_dp->use_max_params = intel_dp->edp_dpcd[0] < DP_EDP_14;
4781 	}
4782 
4783 	/*
4784 	 * If needed, program our source OUI so we can make various Intel-specific AUX services
4785 	 * available (such as HDR backlight controls)
4786 	 */
4787 	intel_dp_init_source_oui(intel_dp);
4788 
4789 	/* Read the ALPM DPCD caps */
4790 	ret = drm_dp_dpcd_read_byte(&intel_dp->aux, DP_RECEIVER_ALPM_CAP,
4791 				    &intel_dp->alpm_dpcd);
4792 	if (ret < 0)
4793 		return false;
4794 
4795 	/*
4796 	 * This has to be called after intel_dp->edp_dpcd is filled, PSR checks
4797 	 * for SET_POWER_CAPABLE bit in intel_dp->edp_dpcd[1]
4798 	 */
4799 	intel_psr_init_dpcd(intel_dp, connector);
4800 
4801 	intel_edp_set_sink_rates(intel_dp);
4802 	intel_dp_set_max_sink_lane_count(intel_dp);
4803 
4804 	/* Read the eDP DSC DPCD registers */
4805 	intel_dp_detect_dsc_caps(intel_dp, connector);
4806 
4807 	return true;
4808 }
4809 
4810 static bool
4811 intel_dp_has_sink_count(struct intel_dp *intel_dp)
4812 {
4813 	if (!intel_dp->attached_connector)
4814 		return false;
4815 
4816 	return drm_dp_read_sink_count_cap(&intel_dp->attached_connector->base,
4817 					  intel_dp->dpcd,
4818 					  &intel_dp->desc);
4819 }
4820 
4821 void intel_dp_update_sink_caps(struct intel_dp *intel_dp)
4822 {
4823 	struct intel_display *display = to_intel_display(intel_dp);
4824 
4825 	intel_dp_set_sink_rates(intel_dp);
4826 	intel_dp_set_max_sink_lane_count(intel_dp);
4827 	/*
4828 	 * Handle unexpected sink cap changes, or a race between setting
4829 	 * the deferred link params flag in the HPD IRQ handler and
4830 	 * clearing the flag during connector detect.
4831 	 */
4832 	if (intel_dp_set_common_link_params(intel_dp) &&
4833 	    !intel_dp->reset_link_params) {
4834 		drm_dbg_kms(display->drm,
4835 			    "DPRX capabilities changed before long HPD or RX_CAP_CHANGED signal\n");
4836 		intel_dp->reset_link_params = true;
4837 	}
4838 }
4839 
4840 static bool
4841 intel_dp_get_dpcd(struct intel_dp *intel_dp)
4842 {
4843 	int ret;
4844 
4845 	if (intel_dp_init_lttpr_and_dprx_caps(intel_dp) < 0)
4846 		return false;
4847 
4848 	/*
4849 	 * Don't clobber cached eDP rates. Also skip re-reading
4850 	 * the OUI/ID since we know it won't change.
4851 	 */
4852 	if (!intel_dp_is_edp(intel_dp)) {
4853 		drm_dp_read_desc(&intel_dp->aux, &intel_dp->desc,
4854 				 drm_dp_is_branch(intel_dp->dpcd));
4855 
4856 		intel_init_dpcd_quirks(intel_dp, &intel_dp->desc.ident);
4857 
4858 		intel_dp->colorimetry_support =
4859 			intel_dp_get_colorimetry_status(intel_dp);
4860 
4861 		intel_dp_update_sink_caps(intel_dp);
4862 	}
4863 
4864 	if (intel_dp_has_sink_count(intel_dp)) {
4865 		ret = drm_dp_read_sink_count(&intel_dp->aux);
4866 		if (ret < 0)
4867 			return false;
4868 
4869 		/*
4870 		 * Sink count can change between short pulse hpd hence
4871 		 * a member variable in intel_dp will track any changes
4872 		 * between short pulse interrupts.
4873 		 */
4874 		intel_dp->sink_count = ret;
4875 
4876 		/*
4877 		 * SINK_COUNT == 0 and DOWNSTREAM_PORT_PRESENT == 1 implies that
4878 		 * a dongle is present but no display. Unless we require to know
4879 		 * if a dongle is present or not, we don't need to update
4880 		 * downstream port information. So, an early return here saves
4881 		 * time from performing other operations which are not required.
4882 		 */
4883 		if (!intel_dp->sink_count)
4884 			return false;
4885 	}
4886 
4887 	return drm_dp_read_downstream_info(&intel_dp->aux, intel_dp->dpcd,
4888 					   intel_dp->downstream_ports) == 0;
4889 }
4890 
4891 static const char *intel_dp_mst_mode_str(enum drm_dp_mst_mode mst_mode)
4892 {
4893 	if (mst_mode == DRM_DP_MST)
4894 		return "MST";
4895 	else if (mst_mode == DRM_DP_SST_SIDEBAND_MSG)
4896 		return "SST w/ sideband messaging";
4897 	else
4898 		return "SST";
4899 }
4900 
4901 static enum drm_dp_mst_mode
4902 intel_dp_mst_mode_choose(struct intel_dp *intel_dp,
4903 			 enum drm_dp_mst_mode sink_mst_mode)
4904 {
4905 	struct intel_display *display = to_intel_display(intel_dp);
4906 
4907 	if (!display->params.enable_dp_mst)
4908 		return DRM_DP_SST;
4909 
4910 	if (!intel_dp_mst_source_support(intel_dp))
4911 		return DRM_DP_SST;
4912 
4913 	if (sink_mst_mode == DRM_DP_SST_SIDEBAND_MSG &&
4914 	    !(intel_dp->dpcd[DP_MAIN_LINK_CHANNEL_CODING] & DP_CAP_ANSI_128B132B))
4915 		return DRM_DP_SST;
4916 
4917 	return sink_mst_mode;
4918 }
4919 
4920 static enum drm_dp_mst_mode
4921 intel_dp_mst_detect(struct intel_dp *intel_dp)
4922 {
4923 	struct intel_display *display = to_intel_display(intel_dp);
4924 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
4925 	enum drm_dp_mst_mode sink_mst_mode;
4926 	enum drm_dp_mst_mode mst_detect;
4927 
4928 	sink_mst_mode = drm_dp_read_mst_cap(&intel_dp->aux, intel_dp->dpcd);
4929 
4930 	mst_detect = intel_dp_mst_mode_choose(intel_dp, sink_mst_mode);
4931 
4932 	drm_dbg_kms(display->drm,
4933 		    "[ENCODER:%d:%s] MST support: port: %s, sink: %s, modparam: %s -> enable: %s\n",
4934 		    encoder->base.base.id, encoder->base.name,
4935 		    str_yes_no(intel_dp_mst_source_support(intel_dp)),
4936 		    intel_dp_mst_mode_str(sink_mst_mode),
4937 		    str_yes_no(display->params.enable_dp_mst),
4938 		    intel_dp_mst_mode_str(mst_detect));
4939 
4940 	return mst_detect;
4941 }
4942 
4943 static void
4944 intel_dp_mst_configure(struct intel_dp *intel_dp)
4945 {
4946 	if (!intel_dp_mst_source_support(intel_dp))
4947 		return;
4948 
4949 	intel_dp->is_mst = intel_dp->mst_detect != DRM_DP_SST;
4950 
4951 	if (intel_dp->is_mst)
4952 		intel_dp_mst_prepare_probe(intel_dp);
4953 
4954 	drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst.mgr, intel_dp->is_mst);
4955 
4956 	/* Avoid stale info on the next detect cycle. */
4957 	intel_dp->mst_detect = DRM_DP_SST;
4958 }
4959 
4960 static void
4961 intel_dp_mst_disconnect(struct intel_dp *intel_dp)
4962 {
4963 	struct intel_display *display = to_intel_display(intel_dp);
4964 
4965 	if (!intel_dp->is_mst)
4966 		return;
4967 
4968 	drm_dbg_kms(display->drm,
4969 		    "MST device may have disappeared %d vs %d\n",
4970 		    intel_dp->is_mst, intel_dp->mst.mgr.mst_state);
4971 	intel_dp->is_mst = false;
4972 	drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst.mgr, intel_dp->is_mst);
4973 }
4974 
4975 #define INTEL_DP_DEVICE_SERVICE_IRQ_MASK_SST	(DP_AUTOMATED_TEST_REQUEST | \
4976 						 DP_CP_IRQ | \
4977 						 DP_SINK_SPECIFIC_IRQ)
4978 
4979 #define INTEL_DP_DEVICE_SERVICE_IRQ_MASK_MST	(DP_CP_IRQ | \
4980 						 DP_DOWN_REP_MSG_RDY | \
4981 						 DP_UP_REQ_MSG_RDY)
4982 
4983 #define INTEL_DP_LINK_SERVICE_IRQ_MASK_SST	(RX_CAP_CHANGED | \
4984 						 LINK_STATUS_CHANGED | \
4985 						 HDMI_LINK_STATUS_CHANGED | \
4986 						 CONNECTED_OFF_ENTRY_REQUESTED | \
4987 						 DP_TUNNELING_IRQ)
4988 
4989 #define INTEL_DP_LINK_SERVICE_IRQ_MASK_MST	(RX_CAP_CHANGED | \
4990 						 LINK_STATUS_CHANGED | \
4991 						 DP_TUNNELING_IRQ)
4992 
4993 static bool
4994 intel_dp_get_sink_irq_esi(struct intel_dp *intel_dp, u8 *esi)
4995 {
4996 	struct intel_display *display = to_intel_display(intel_dp);
4997 
4998 	/*
4999 	 * Display WA for HSD #13013007775: mtl/arl/lnl
5000 	 * Read the sink count and link service IRQ registers in separate
5001 	 * transactions to prevent disconnecting the sink on a TBT link
5002 	 * inadvertently.
5003 	 */
5004 	if (IS_DISPLAY_VER(display, 14, 20) && !display->platform.battlemage) {
5005 		if (drm_dp_dpcd_read(&intel_dp->aux, DP_SINK_COUNT_ESI, esi, 3) != 3)
5006 			return false;
5007 
5008 		/* DP_SINK_COUNT_ESI + 3 == DP_LINK_SERVICE_IRQ_VECTOR_ESI0 */
5009 		return drm_dp_dpcd_readb(&intel_dp->aux, DP_LINK_SERVICE_IRQ_VECTOR_ESI0,
5010 					 &esi[3]) == 1;
5011 	}
5012 
5013 	return drm_dp_dpcd_read(&intel_dp->aux, DP_SINK_COUNT_ESI, esi, 4) == 4;
5014 }
5015 
5016 static bool intel_dp_ack_sink_irq_esi(struct intel_dp *intel_dp, u8 esi[4])
5017 {
5018 	int retry;
5019 
5020 	for (retry = 0; retry < 3; retry++) {
5021 		if (drm_dp_dpcd_write(&intel_dp->aux, DP_SINK_COUNT_ESI + 1,
5022 				      &esi[1], 3) == 3)
5023 			return true;
5024 	}
5025 
5026 	return false;
5027 }
5028 
5029 /* Return %true if reading the ESI vector succeeded, %false otherwise. */
5030 static bool intel_dp_get_sink_irq_esi_sst(struct intel_dp *intel_dp, u8 esi[4])
5031 {
5032 	memset(esi, 0, 4);
5033 
5034 	/*
5035 	 * TODO: For DP_DPCD_REV >= 0x12 read
5036 	 * DP_SINK_COUNT_ESI and DP_DEVICE_SERVICE_IRQ_VECTOR_ESI0.
5037 	 */
5038 	if (drm_dp_dpcd_read_data(&intel_dp->aux, DP_SINK_COUNT, esi, 2) != 0)
5039 		return false;
5040 
5041 	if (intel_dp->dpcd[DP_DPCD_REV] < DP_DPCD_REV_12)
5042 		return true;
5043 
5044 	/* TODO: Read DP_DEVICE_SERVICE_IRQ_VECTOR_ESI1 as well */
5045 	if (drm_dp_dpcd_read_byte(&intel_dp->aux, DP_LINK_SERVICE_IRQ_VECTOR_ESI0, &esi[3]) != 0)
5046 		return false;
5047 
5048 	return true;
5049 }
5050 
5051 /* Return %true if acking the ESI vector IRQ events succeeded, %false otherwise. */
5052 static bool intel_dp_ack_sink_irq_esi_sst(struct intel_dp *intel_dp, u8 esi[4])
5053 {
5054 	/*
5055 	 * TODO: For DP_DPCD_REV >= 0x12 write
5056 	 * DP_DEVICE_SERVICE_IRQ_VECTOR_ESI0
5057 	 */
5058 	if (drm_dp_dpcd_write_byte(&intel_dp->aux, DP_DEVICE_SERVICE_IRQ_VECTOR, esi[1]) != 0)
5059 		return false;
5060 
5061 	if (intel_dp->dpcd[DP_DPCD_REV] < DP_DPCD_REV_12)
5062 		return true;
5063 
5064 	/* TODO: Read DP_DEVICE_SERVICE_IRQ_VECTOR_ESI1 as well */
5065 	if (drm_dp_dpcd_write_byte(&intel_dp->aux, DP_LINK_SERVICE_IRQ_VECTOR_ESI0, esi[3]) != 0)
5066 		return false;
5067 
5068 	return true;
5069 }
5070 
5071 /*
5072  * Return %true if reading the ESI vector and acking the ESI IRQ events succeeded,
5073  * %false otherwise.
5074  */
5075 static bool intel_dp_get_and_ack_sink_irq_esi_sst(struct intel_dp *intel_dp, u8 esi[4])
5076 {
5077 	struct intel_display *display = to_intel_display(intel_dp);
5078 	struct intel_connector *connector = intel_dp->attached_connector;
5079 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
5080 
5081 	if (!intel_dp_get_sink_irq_esi_sst(intel_dp, esi))
5082 		return false;
5083 
5084 	drm_dbg_kms(display->drm,
5085 		    "[CONNECTOR:%d:%s][ENCODER:%d:%s] DPRX ESI: %4ph\n",
5086 		    connector->base.base.id, connector->base.name,
5087 		    encoder->base.base.id, encoder->base.name,
5088 		    esi);
5089 
5090 	esi[1] &= INTEL_DP_DEVICE_SERVICE_IRQ_MASK_SST;
5091 	esi[3] &= INTEL_DP_LINK_SERVICE_IRQ_MASK_SST;
5092 
5093 	if (mem_is_zero(&esi[1], 3))
5094 		return true;
5095 
5096 	if (!intel_dp_ack_sink_irq_esi_sst(intel_dp, esi))
5097 		return false;
5098 
5099 	return true;
5100 }
5101 
5102 bool
5103 intel_dp_needs_vsc_sdp(const struct intel_crtc_state *crtc_state,
5104 		       const struct drm_connector_state *conn_state)
5105 {
5106 	/*
5107 	 * As per DP 1.4a spec section 2.2.4.3 [MSA Field for Indication
5108 	 * of Color Encoding Format and Content Color Gamut], in order to
5109 	 * sending YCBCR 420 or HDR BT.2020 signals we should use DP VSC SDP.
5110 	 */
5111 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
5112 		return true;
5113 
5114 	switch (conn_state->colorspace) {
5115 	case DRM_MODE_COLORIMETRY_SYCC_601:
5116 	case DRM_MODE_COLORIMETRY_OPYCC_601:
5117 	case DRM_MODE_COLORIMETRY_BT2020_YCC:
5118 	case DRM_MODE_COLORIMETRY_BT2020_RGB:
5119 	case DRM_MODE_COLORIMETRY_BT2020_CYCC:
5120 		return true;
5121 	default:
5122 		break;
5123 	}
5124 
5125 	return false;
5126 }
5127 
5128 static ssize_t intel_dp_as_sdp_pack(const struct drm_dp_as_sdp *as_sdp,
5129 				    struct dp_sdp *sdp, size_t size)
5130 {
5131 	size_t length = sizeof(struct dp_sdp);
5132 
5133 	if (size < length)
5134 		return -ENOSPC;
5135 
5136 	memset(sdp, 0, size);
5137 
5138 	/* Prepare AS (Adaptive Sync) SDP Header */
5139 	sdp->sdp_header.HB0 = 0;
5140 	sdp->sdp_header.HB1 = as_sdp->sdp_type;
5141 	sdp->sdp_header.HB2 = as_sdp->revision;
5142 	sdp->sdp_header.HB3 = as_sdp->length;
5143 
5144 	/* Fill AS (Adaptive Sync) SDP Payload */
5145 	sdp->db[0] = as_sdp->mode;
5146 	sdp->db[1] = as_sdp->vtotal & 0xFF;
5147 	sdp->db[2] = (as_sdp->vtotal >> 8) & 0xFF;
5148 	sdp->db[3] = as_sdp->target_rr & 0xFF;
5149 	sdp->db[4] = (as_sdp->target_rr >> 8) & 0x3;
5150 
5151 	if (as_sdp->target_rr_divider)
5152 		sdp->db[4] |= 0x20;
5153 
5154 	sdp->db[7] = as_sdp->coasting_vtotal & 0xFF;
5155 	sdp->db[8] = (as_sdp->coasting_vtotal >> 8) & 0xFF;
5156 
5157 	return length;
5158 }
5159 
5160 static ssize_t
5161 intel_dp_hdr_metadata_infoframe_sdp_pack(struct intel_display *display,
5162 					 const struct hdmi_drm_infoframe *drm_infoframe,
5163 					 struct dp_sdp *sdp,
5164 					 size_t size)
5165 {
5166 	size_t length = sizeof(struct dp_sdp);
5167 	const int infoframe_size = HDMI_INFOFRAME_HEADER_SIZE + HDMI_DRM_INFOFRAME_SIZE;
5168 	unsigned char buf[HDMI_INFOFRAME_HEADER_SIZE + HDMI_DRM_INFOFRAME_SIZE];
5169 	ssize_t len;
5170 
5171 	if (size < length)
5172 		return -ENOSPC;
5173 
5174 	memset(sdp, 0, size);
5175 
5176 	len = hdmi_drm_infoframe_pack_only(drm_infoframe, buf, sizeof(buf));
5177 	if (len < 0) {
5178 		drm_dbg_kms(display->drm,
5179 			    "buffer size is smaller than hdr metadata infoframe\n");
5180 		return -ENOSPC;
5181 	}
5182 
5183 	if (len != infoframe_size) {
5184 		drm_dbg_kms(display->drm, "wrong static hdr metadata size\n");
5185 		return -ENOSPC;
5186 	}
5187 
5188 	/*
5189 	 * Set up the infoframe sdp packet for HDR static metadata.
5190 	 * Prepare VSC Header for SU as per DP 1.4a spec,
5191 	 * Table 2-100 and Table 2-101
5192 	 */
5193 
5194 	/* Secondary-Data Packet ID, 00h for non-Audio INFOFRAME */
5195 	sdp->sdp_header.HB0 = 0;
5196 	/*
5197 	 * Packet Type 80h + Non-audio INFOFRAME Type value
5198 	 * HDMI_INFOFRAME_TYPE_DRM: 0x87
5199 	 * - 80h + Non-audio INFOFRAME Type value
5200 	 * - InfoFrame Type: 0x07
5201 	 *    [CTA-861-G Table-42 Dynamic Range and Mastering InfoFrame]
5202 	 */
5203 	sdp->sdp_header.HB1 = drm_infoframe->type;
5204 	/*
5205 	 * Least Significant Eight Bits of (Data Byte Count – 1)
5206 	 * infoframe_size - 1
5207 	 */
5208 	sdp->sdp_header.HB2 = 0x1D;
5209 	/* INFOFRAME SDP Version Number */
5210 	sdp->sdp_header.HB3 = (0x13 << 2);
5211 	/* CTA Header Byte 2 (INFOFRAME Version Number) */
5212 	sdp->db[0] = drm_infoframe->version;
5213 	/* CTA Header Byte 3 (Length of INFOFRAME): HDMI_DRM_INFOFRAME_SIZE */
5214 	sdp->db[1] = drm_infoframe->length;
5215 	/*
5216 	 * Copy HDMI_DRM_INFOFRAME_SIZE size from a buffer after
5217 	 * HDMI_INFOFRAME_HEADER_SIZE
5218 	 */
5219 	BUILD_BUG_ON(sizeof(sdp->db) < HDMI_DRM_INFOFRAME_SIZE + 2);
5220 	memcpy(&sdp->db[2], &buf[HDMI_INFOFRAME_HEADER_SIZE],
5221 	       HDMI_DRM_INFOFRAME_SIZE);
5222 
5223 	/*
5224 	 * Size of DP infoframe sdp packet for HDR static metadata consists of
5225 	 * - DP SDP Header(struct dp_sdp_header): 4 bytes
5226 	 * - Two Data Blocks: 2 bytes
5227 	 *    CTA Header Byte2 (INFOFRAME Version Number)
5228 	 *    CTA Header Byte3 (Length of INFOFRAME)
5229 	 * - HDMI_DRM_INFOFRAME_SIZE: 26 bytes
5230 	 *
5231 	 * Prior to GEN11's GMP register size is identical to DP HDR static metadata
5232 	 * infoframe size. But GEN11+ has larger than that size, write_infoframe
5233 	 * will pad rest of the size.
5234 	 */
5235 	return sizeof(struct dp_sdp_header) + 2 + HDMI_DRM_INFOFRAME_SIZE;
5236 }
5237 
5238 static void intel_write_dp_sdp(struct intel_encoder *encoder,
5239 			       const struct intel_crtc_state *crtc_state,
5240 			       unsigned int type)
5241 {
5242 	struct intel_display *display = to_intel_display(encoder);
5243 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
5244 	struct dp_sdp sdp = {};
5245 	ssize_t len;
5246 
5247 	if ((crtc_state->infoframes.enable &
5248 	     intel_hdmi_infoframe_enable(type)) == 0)
5249 		return;
5250 
5251 	switch (type) {
5252 	case DP_SDP_VSC:
5253 		len = drm_dp_vsc_sdp_pack(&crtc_state->infoframes.vsc, &sdp);
5254 		break;
5255 	case HDMI_PACKET_TYPE_GAMUT_METADATA:
5256 		len = intel_dp_hdr_metadata_infoframe_sdp_pack(display,
5257 							       &crtc_state->infoframes.drm.drm,
5258 							       &sdp, sizeof(sdp));
5259 		break;
5260 	case DP_SDP_ADAPTIVE_SYNC:
5261 		len = intel_dp_as_sdp_pack(&crtc_state->infoframes.as_sdp, &sdp,
5262 					   sizeof(sdp));
5263 		break;
5264 	default:
5265 		MISSING_CASE(type);
5266 		return;
5267 	}
5268 
5269 	if (drm_WARN_ON(display->drm, len < 0))
5270 		return;
5271 
5272 	dig_port->write_infoframe(encoder, crtc_state, type, &sdp, len);
5273 }
5274 
5275 void intel_dp_set_infoframes(struct intel_encoder *encoder,
5276 			     bool enable,
5277 			     const struct intel_crtc_state *crtc_state,
5278 			     const struct drm_connector_state *conn_state)
5279 {
5280 	struct intel_display *display = to_intel_display(encoder);
5281 	intel_reg_t reg = HSW_TVIDEO_DIP_CTL(display, crtc_state->cpu_transcoder);
5282 	u32 dip_enable = VIDEO_DIP_ENABLE_AVI_HSW | VIDEO_DIP_ENABLE_GCP_HSW |
5283 			 VIDEO_DIP_ENABLE_VS_HSW | VIDEO_DIP_ENABLE_GMP_HSW |
5284 			 VIDEO_DIP_ENABLE_SPD_HSW | VIDEO_DIP_ENABLE_DRM_GLK;
5285 
5286 	if (HAS_AS_SDP(display))
5287 		dip_enable |= VIDEO_DIP_ENABLE_AS_ADL;
5288 
5289 	u32 val = intel_de_read(display, reg) & ~dip_enable;
5290 
5291 	/* TODO: Sanitize DSC enabling wrt. intel_dsc_dp_pps_write(). */
5292 	if (!enable && HAS_DSC(display))
5293 		val &= ~VDIP_ENABLE_PPS;
5294 
5295 	/*
5296 	 * This routine disables VSC DIP if the function is called
5297 	 * to disable SDP or if it does not have PSR
5298 	 */
5299 	if (!enable || !crtc_state->has_psr)
5300 		val &= ~VIDEO_DIP_ENABLE_VSC_HSW;
5301 
5302 	intel_de_write(display, reg, val);
5303 	intel_de_posting_read(display, reg);
5304 
5305 	if (!enable)
5306 		return;
5307 
5308 	intel_write_dp_sdp(encoder, crtc_state, DP_SDP_VSC);
5309 	intel_write_dp_sdp(encoder, crtc_state, DP_SDP_ADAPTIVE_SYNC);
5310 
5311 	intel_write_dp_sdp(encoder, crtc_state, HDMI_PACKET_TYPE_GAMUT_METADATA);
5312 }
5313 
5314 static
5315 int intel_dp_as_sdp_unpack(struct drm_dp_as_sdp *as_sdp,
5316 			   const void *buffer, size_t size)
5317 {
5318 	const struct dp_sdp *sdp = buffer;
5319 
5320 	if (size < sizeof(struct dp_sdp))
5321 		return -EINVAL;
5322 
5323 	memset(as_sdp, 0, sizeof(*as_sdp));
5324 
5325 	if (sdp->sdp_header.HB0 != 0)
5326 		return -EINVAL;
5327 
5328 	if (sdp->sdp_header.HB1 != DP_SDP_ADAPTIVE_SYNC)
5329 		return -EINVAL;
5330 
5331 	if ((sdp->sdp_header.HB3 & 0x3F) != 9)
5332 		return -EINVAL;
5333 
5334 	as_sdp->sdp_type = sdp->sdp_header.HB1;
5335 	as_sdp->length = sdp->sdp_header.HB3 & DP_AS_SDP_LENGTH_MASK;
5336 	as_sdp->revision = sdp->sdp_header.HB2;
5337 	as_sdp->mode = sdp->db[0] & DP_AS_SDP_OPERATION_MODE_MASK;
5338 	as_sdp->vtotal = (sdp->db[2] << 8) | sdp->db[1];
5339 	as_sdp->target_rr = ((sdp->db[4] & 0x3) << 8) | sdp->db[3];
5340 	as_sdp->target_rr_divider = sdp->db[4] & 0x20 ? true : false;
5341 	as_sdp->coasting_vtotal = (sdp->db[8] << 8) | sdp->db[7];
5342 
5343 	return 0;
5344 }
5345 
5346 static int intel_dp_vsc_sdp_unpack(struct drm_dp_vsc_sdp *vsc,
5347 				   const void *buffer, size_t size)
5348 {
5349 	const struct dp_sdp *sdp = buffer;
5350 
5351 	if (size < sizeof(struct dp_sdp))
5352 		return -EINVAL;
5353 
5354 	memset(vsc, 0, sizeof(*vsc));
5355 
5356 	if (sdp->sdp_header.HB0 != 0)
5357 		return -EINVAL;
5358 
5359 	if (sdp->sdp_header.HB1 != DP_SDP_VSC)
5360 		return -EINVAL;
5361 
5362 	vsc->sdp_type = sdp->sdp_header.HB1;
5363 	vsc->revision = sdp->sdp_header.HB2;
5364 	vsc->length = sdp->sdp_header.HB3;
5365 
5366 	if ((sdp->sdp_header.HB2 == 0x2 && sdp->sdp_header.HB3 == 0x8) ||
5367 	    (sdp->sdp_header.HB2 == 0x4 && sdp->sdp_header.HB3 == 0xe) ||
5368 	    (sdp->sdp_header.HB2 == 0x6 && sdp->sdp_header.HB3 == 0x10)) {
5369 		/*
5370 		 * - HB2 = 0x2, HB3 = 0x8
5371 		 *   VSC SDP supporting 3D stereo + PSR
5372 		 * - HB2 = 0x4, HB3 = 0xe
5373 		 *   VSC SDP supporting 3D stereo + PSR2 with Y-coordinate of
5374 		 *   first scan line of the SU region (applies to eDP v1.4b
5375 		 *   and higher).
5376 		 * - HB2 = 0x6, HB3 = 0x10
5377 		 *   VSC SDP supporting 3D stereo + Panel Replay.
5378 		 */
5379 		return 0;
5380 	} else if (sdp->sdp_header.HB2 == 0x5 && sdp->sdp_header.HB3 == 0x13) {
5381 		/*
5382 		 * - HB2 = 0x5, HB3 = 0x13
5383 		 *   VSC SDP supporting 3D stereo + PSR2 + Pixel Encoding/Colorimetry
5384 		 *   Format.
5385 		 */
5386 		vsc->pixelformat = (sdp->db[16] >> 4) & 0xf;
5387 		vsc->colorimetry = sdp->db[16] & 0xf;
5388 		vsc->dynamic_range = (sdp->db[17] >> 7) & 0x1;
5389 
5390 		switch (sdp->db[17] & 0x7) {
5391 		case 0x0:
5392 			vsc->bpc = 6;
5393 			break;
5394 		case 0x1:
5395 			vsc->bpc = 8;
5396 			break;
5397 		case 0x2:
5398 			vsc->bpc = 10;
5399 			break;
5400 		case 0x3:
5401 			vsc->bpc = 12;
5402 			break;
5403 		case 0x4:
5404 			vsc->bpc = 16;
5405 			break;
5406 		default:
5407 			MISSING_CASE(sdp->db[17] & 0x7);
5408 			return -EINVAL;
5409 		}
5410 
5411 		vsc->content_type = sdp->db[18] & 0x7;
5412 	} else {
5413 		return -EINVAL;
5414 	}
5415 
5416 	return 0;
5417 }
5418 
5419 static void
5420 intel_read_dp_as_sdp(struct intel_encoder *encoder,
5421 		     struct intel_crtc_state *crtc_state,
5422 		     struct drm_dp_as_sdp *as_sdp)
5423 {
5424 	struct intel_display *display = to_intel_display(encoder);
5425 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
5426 	unsigned int type = DP_SDP_ADAPTIVE_SYNC;
5427 	struct dp_sdp sdp = {};
5428 	int ret;
5429 
5430 	if ((crtc_state->infoframes.enable &
5431 	     intel_hdmi_infoframe_enable(type)) == 0)
5432 		return;
5433 
5434 	dig_port->read_infoframe(encoder, crtc_state, type, &sdp,
5435 				 sizeof(sdp));
5436 
5437 	ret = intel_dp_as_sdp_unpack(as_sdp, &sdp, sizeof(sdp));
5438 	if (ret)
5439 		drm_dbg_kms(display->drm, "Failed to unpack DP AS SDP\n");
5440 }
5441 
5442 static int
5443 intel_dp_hdr_metadata_infoframe_sdp_unpack(struct hdmi_drm_infoframe *drm_infoframe,
5444 					   const void *buffer, size_t size)
5445 {
5446 	int ret;
5447 
5448 	const struct dp_sdp *sdp = buffer;
5449 
5450 	if (size < sizeof(struct dp_sdp))
5451 		return -EINVAL;
5452 
5453 	if (sdp->sdp_header.HB0 != 0)
5454 		return -EINVAL;
5455 
5456 	if (sdp->sdp_header.HB1 != HDMI_INFOFRAME_TYPE_DRM)
5457 		return -EINVAL;
5458 
5459 	/*
5460 	 * Least Significant Eight Bits of (Data Byte Count – 1)
5461 	 * 1Dh (i.e., Data Byte Count = 30 bytes).
5462 	 */
5463 	if (sdp->sdp_header.HB2 != 0x1D)
5464 		return -EINVAL;
5465 
5466 	/* Most Significant Two Bits of (Data Byte Count – 1), Clear to 00b. */
5467 	if ((sdp->sdp_header.HB3 & 0x3) != 0)
5468 		return -EINVAL;
5469 
5470 	/* INFOFRAME SDP Version Number */
5471 	if (((sdp->sdp_header.HB3 >> 2) & 0x3f) != 0x13)
5472 		return -EINVAL;
5473 
5474 	/* CTA Header Byte 2 (INFOFRAME Version Number) */
5475 	if (sdp->db[0] != 1)
5476 		return -EINVAL;
5477 
5478 	/* CTA Header Byte 3 (Length of INFOFRAME): HDMI_DRM_INFOFRAME_SIZE */
5479 	if (sdp->db[1] != HDMI_DRM_INFOFRAME_SIZE)
5480 		return -EINVAL;
5481 
5482 	ret = hdmi_drm_infoframe_unpack_only(drm_infoframe, &sdp->db[2],
5483 					     HDMI_DRM_INFOFRAME_SIZE);
5484 
5485 	return ret;
5486 }
5487 
5488 static void intel_read_dp_vsc_sdp(struct intel_encoder *encoder,
5489 				  struct intel_crtc_state *crtc_state,
5490 				  struct drm_dp_vsc_sdp *vsc)
5491 {
5492 	struct intel_display *display = to_intel_display(encoder);
5493 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
5494 	unsigned int type = DP_SDP_VSC;
5495 	struct dp_sdp sdp = {};
5496 	int ret;
5497 
5498 	if ((crtc_state->infoframes.enable &
5499 	     intel_hdmi_infoframe_enable(type)) == 0)
5500 		return;
5501 
5502 	dig_port->read_infoframe(encoder, crtc_state, type, &sdp, sizeof(sdp));
5503 
5504 	ret = intel_dp_vsc_sdp_unpack(vsc, &sdp, sizeof(sdp));
5505 
5506 	if (ret)
5507 		drm_dbg_kms(display->drm, "Failed to unpack DP VSC SDP\n");
5508 }
5509 
5510 static void intel_read_dp_hdr_metadata_infoframe_sdp(struct intel_encoder *encoder,
5511 						     struct intel_crtc_state *crtc_state,
5512 						     struct hdmi_drm_infoframe *drm_infoframe)
5513 {
5514 	struct intel_display *display = to_intel_display(encoder);
5515 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
5516 	unsigned int type = HDMI_PACKET_TYPE_GAMUT_METADATA;
5517 	struct dp_sdp sdp = {};
5518 	int ret;
5519 
5520 	if ((crtc_state->infoframes.enable &
5521 	    intel_hdmi_infoframe_enable(type)) == 0)
5522 		return;
5523 
5524 	dig_port->read_infoframe(encoder, crtc_state, type, &sdp,
5525 				 sizeof(sdp));
5526 
5527 	ret = intel_dp_hdr_metadata_infoframe_sdp_unpack(drm_infoframe, &sdp,
5528 							 sizeof(sdp));
5529 
5530 	if (ret)
5531 		drm_dbg_kms(display->drm,
5532 			    "Failed to unpack DP HDR Metadata Infoframe SDP\n");
5533 }
5534 
5535 void intel_read_dp_sdp(struct intel_encoder *encoder,
5536 		       struct intel_crtc_state *crtc_state,
5537 		       unsigned int type)
5538 {
5539 	switch (type) {
5540 	case DP_SDP_VSC:
5541 		intel_read_dp_vsc_sdp(encoder, crtc_state,
5542 				      &crtc_state->infoframes.vsc);
5543 		break;
5544 	case HDMI_PACKET_TYPE_GAMUT_METADATA:
5545 		intel_read_dp_hdr_metadata_infoframe_sdp(encoder, crtc_state,
5546 							 &crtc_state->infoframes.drm.drm);
5547 		break;
5548 	case DP_SDP_ADAPTIVE_SYNC:
5549 		intel_read_dp_as_sdp(encoder, crtc_state,
5550 				     &crtc_state->infoframes.as_sdp);
5551 		break;
5552 	default:
5553 		MISSING_CASE(type);
5554 		break;
5555 	}
5556 }
5557 
5558 static void
5559 intel_dp_mst_hpd_irq(struct intel_dp *intel_dp, u8 *esi, u8 *ack)
5560 {
5561 	bool handled = false;
5562 
5563 	drm_dp_mst_hpd_irq_handle_event(&intel_dp->mst.mgr, esi, ack, &handled);
5564 
5565 	if (esi[1] & DP_CP_IRQ) {
5566 		intel_hdcp_handle_cp_irq(intel_dp->attached_connector);
5567 		ack[1] |= DP_CP_IRQ;
5568 	}
5569 }
5570 
5571 static bool intel_dp_handle_link_service_irq(struct intel_dp *intel_dp, u8 irq_mask);
5572 
5573 /**
5574  * intel_dp_check_mst_status - service any pending MST interrupts, check link status
5575  * @intel_dp: Intel DP struct
5576  *
5577  * Read any pending MST interrupts, call MST core to handle these and ack the
5578  * interrupts. Check if the main and AUX link state is ok.
5579  *
5580  * Returns:
5581  * - %true if pending interrupts were serviced (or no interrupts were
5582  *   pending) w/o detecting an error condition.
5583  * - %false if an error condition - like AUX failure or a loss of link - is
5584  *   detected, or another condition - like a DP tunnel BW state change - needs
5585  *   servicing from the hotplug work.
5586  */
5587 static bool
5588 intel_dp_check_mst_status(struct intel_dp *intel_dp)
5589 {
5590 	struct intel_display *display = to_intel_display(intel_dp);
5591 	bool force_retrain = intel_dp_link_training_get_force_retrain(intel_dp->link.training);
5592 	bool reprobe_needed = false;
5593 
5594 	for (;;) {
5595 		u8 esi[4] = {};
5596 		u8 ack[4] = {};
5597 		bool new_irqs;
5598 
5599 		if (!intel_dp_get_sink_irq_esi(intel_dp, esi)) {
5600 			drm_dbg_kms(display->drm,
5601 				    "failed to get ESI - device may have failed\n");
5602 			reprobe_needed = true;
5603 
5604 			break;
5605 		}
5606 
5607 		drm_dbg_kms(display->drm, "DPRX ESI: %4ph\n", esi);
5608 
5609 		ack[3] |= esi[3] & INTEL_DP_LINK_SERVICE_IRQ_MASK_MST;
5610 
5611 		intel_dp_mst_hpd_irq(intel_dp, esi, ack);
5612 
5613 		new_irqs = !mem_is_zero(ack, sizeof(ack));
5614 
5615 		drm_WARN_ON(display->drm, ack[1] & ~INTEL_DP_DEVICE_SERVICE_IRQ_MASK_MST);
5616 		drm_WARN_ON(display->drm, ack[3] & ~INTEL_DP_LINK_SERVICE_IRQ_MASK_MST);
5617 
5618 		if (new_irqs && !intel_dp_ack_sink_irq_esi(intel_dp, ack))
5619 			drm_dbg_kms(display->drm, "Failed to ack ESI\n");
5620 
5621 		if (ack[1] & (DP_DOWN_REP_MSG_RDY | DP_UP_REQ_MSG_RDY))
5622 			drm_dp_mst_hpd_irq_send_new_request(&intel_dp->mst.mgr);
5623 
5624 		if (force_retrain) {
5625 			/* Defer forced retraining to the regular link status check. */
5626 			ack[3] |= LINK_STATUS_CHANGED;
5627 			force_retrain = false;
5628 		}
5629 
5630 		if (intel_dp_handle_link_service_irq(intel_dp, ack[3]))
5631 			reprobe_needed = true;
5632 
5633 		if (!new_irqs)
5634 			break;
5635 	}
5636 
5637 	return !reprobe_needed;
5638 }
5639 
5640 static void
5641 intel_dp_handle_hdmi_link_status_change(struct intel_dp *intel_dp)
5642 {
5643 	bool is_active;
5644 	u8 buf = 0;
5645 
5646 	is_active = drm_dp_pcon_hdmi_link_active(&intel_dp->aux);
5647 	if (intel_dp->frl.is_trained && !is_active) {
5648 		if (drm_dp_dpcd_readb(&intel_dp->aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf) < 0)
5649 			return;
5650 
5651 		buf &=  ~DP_PCON_ENABLE_HDMI_LINK;
5652 		if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_PCON_HDMI_LINK_CONFIG_1, buf) < 0)
5653 			return;
5654 
5655 		drm_dp_pcon_hdmi_frl_link_error_count(&intel_dp->aux, &intel_dp->attached_connector->base);
5656 
5657 		intel_dp->frl.is_trained = false;
5658 
5659 		/* Restart FRL training or fall back to TMDS mode */
5660 		intel_dp_check_frl_training(intel_dp);
5661 	}
5662 }
5663 
5664 bool intel_dp_has_connector(struct intel_dp *intel_dp,
5665 			    const struct drm_connector_state *conn_state)
5666 {
5667 	struct intel_display *display = to_intel_display(intel_dp);
5668 	struct intel_encoder *encoder;
5669 	enum pipe pipe;
5670 
5671 	if (!conn_state->best_encoder)
5672 		return false;
5673 
5674 	/* SST */
5675 	encoder = &dp_to_dig_port(intel_dp)->base;
5676 	if (conn_state->best_encoder == &encoder->base)
5677 		return true;
5678 
5679 	/* MST */
5680 	for_each_pipe(display, pipe) {
5681 		encoder = &intel_dp->mst.stream_encoders[pipe]->base;
5682 		if (conn_state->best_encoder == &encoder->base)
5683 			return true;
5684 	}
5685 
5686 	return false;
5687 }
5688 
5689 static void wait_for_connector_hw_done(const struct drm_connector_state *conn_state)
5690 {
5691 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
5692 	struct intel_display *display = to_intel_display(connector);
5693 
5694 	drm_modeset_lock_assert_held(&display->drm->mode_config.connection_mutex);
5695 
5696 	if (!conn_state->commit)
5697 		return;
5698 
5699 	drm_WARN_ON(display->drm,
5700 		    !wait_for_completion_timeout(&conn_state->commit->hw_done,
5701 						 msecs_to_jiffies(5000)));
5702 }
5703 
5704 int intel_dp_get_active_pipes(struct intel_dp *intel_dp,
5705 			      struct drm_modeset_acquire_ctx *ctx,
5706 			      u8 *pipe_mask)
5707 {
5708 	struct intel_display *display = to_intel_display(intel_dp);
5709 	struct drm_connector_list_iter conn_iter;
5710 	struct intel_connector *connector;
5711 	int ret = 0;
5712 
5713 	*pipe_mask = 0;
5714 
5715 	drm_connector_list_iter_begin(display->drm, &conn_iter);
5716 	for_each_intel_connector_iter(connector, &conn_iter) {
5717 		struct drm_connector_state *conn_state =
5718 			connector->base.state;
5719 		struct intel_crtc_state *crtc_state;
5720 		struct intel_crtc *crtc;
5721 
5722 		if (!intel_dp_has_connector(intel_dp, conn_state))
5723 			continue;
5724 
5725 		crtc = to_intel_crtc(conn_state->crtc);
5726 		if (!crtc)
5727 			continue;
5728 
5729 		ret = drm_modeset_lock(&crtc->base.mutex, ctx);
5730 		if (ret)
5731 			break;
5732 
5733 		crtc_state = to_intel_crtc_state(crtc->base.state);
5734 
5735 		drm_WARN_ON(display->drm,
5736 			    !intel_crtc_has_dp_encoder(crtc_state));
5737 
5738 		if (!crtc_state->hw.active)
5739 			continue;
5740 
5741 		wait_for_connector_hw_done(conn_state);
5742 
5743 		*pipe_mask |= BIT(crtc->pipe);
5744 	}
5745 	drm_connector_list_iter_end(&conn_iter);
5746 
5747 	return ret;
5748 }
5749 
5750 void intel_dp_flush_connector_commits(struct intel_connector *connector)
5751 {
5752 	wait_for_connector_hw_done(connector->base.state);
5753 }
5754 
5755 static void intel_dp_handle_device_service_irq(struct intel_dp *intel_dp, u8 irq_mask)
5756 {
5757 	struct intel_display *display = to_intel_display(intel_dp);
5758 
5759 	drm_WARN_ON(display->drm, irq_mask & ~INTEL_DP_DEVICE_SERVICE_IRQ_MASK_SST);
5760 
5761 	if (irq_mask & DP_AUTOMATED_TEST_REQUEST)
5762 		intel_dp_test_request(intel_dp);
5763 
5764 	if (irq_mask & DP_CP_IRQ)
5765 		intel_hdcp_handle_cp_irq(intel_dp->attached_connector);
5766 
5767 	if (irq_mask & DP_SINK_SPECIFIC_IRQ)
5768 		drm_dbg_kms(display->drm, "Sink specific irq unhandled\n");
5769 }
5770 
5771 
5772 /*
5773  * Return %true if a full connector reprobe is required after handling a link
5774  * service IRQ event.
5775  */
5776 static bool intel_dp_handle_link_service_irq(struct intel_dp *intel_dp, u8 irq_mask)
5777 {
5778 	struct intel_display *display = to_intel_display(intel_dp);
5779 	struct intel_connector *connector = intel_dp->attached_connector;
5780 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
5781 	bool reprobe_needed = false;
5782 
5783 	drm_WARN_ON(display->drm, irq_mask & ~(INTEL_DP_LINK_SERVICE_IRQ_MASK_SST |
5784 					       INTEL_DP_LINK_SERVICE_IRQ_MASK_MST));
5785 
5786 	if (irq_mask & RX_CAP_CHANGED) {
5787 		intel_dp->reset_link_params = true;
5788 		reprobe_needed = true;
5789 	}
5790 
5791 	if (irq_mask & LINK_STATUS_CHANGED)
5792 		intel_dp_check_link_state(intel_dp);
5793 
5794 	if (irq_mask & HDMI_LINK_STATUS_CHANGED)
5795 		intel_dp_handle_hdmi_link_status_change(intel_dp);
5796 
5797 	if (irq_mask & CONNECTED_OFF_ENTRY_REQUESTED)
5798 		drm_dbg_kms(display->drm,
5799 			    "[CONNECTOR:%d:%s][ENCODER:%d:%s] Allowing connected off request\n",
5800 			    connector->base.base.id, connector->base.name,
5801 			    encoder->base.base.id, encoder->base.name);
5802 
5803 	if ((irq_mask & DP_TUNNELING_IRQ) &&
5804 	    drm_dp_tunnel_handle_irq(display->dp_tunnel_mgr,
5805 				     &intel_dp->aux))
5806 		reprobe_needed = true;
5807 
5808 	return reprobe_needed;
5809 }
5810 
5811 /*
5812  * According to DP spec
5813  * 5.1.2:
5814  *  1. Read DPCD
5815  *  2. Configure link according to Receiver Capabilities
5816  *  3. Use Link Training from 2.5.3.3 and 3.5.1.3
5817  *  4. Check link status on receipt of hot-plug interrupt
5818  *
5819  * intel_dp_short_pulse -  handles short pulse interrupts
5820  * when full detection is not required.
5821  * Returns %true if short pulse is handled and full detection
5822  * is NOT required and %false otherwise.
5823  */
5824 static bool
5825 intel_dp_short_pulse(struct intel_dp *intel_dp)
5826 {
5827 	bool reprobe_needed = false;
5828 	u8 esi[4] = {};
5829 
5830 	intel_dp_test_reset(intel_dp);
5831 
5832 	if (!intel_dp_get_and_ack_sink_irq_esi_sst(intel_dp, esi))
5833 		return false;
5834 
5835 	/*
5836 	 * If the current value of sink count doesn't match with
5837 	 * the value that was stored earlier we need to do full
5838 	 * detection.
5839 	 */
5840 	if (intel_dp_has_sink_count(intel_dp) &&
5841 	    DP_GET_SINK_COUNT(esi[0]) != intel_dp->sink_count)
5842 		/* No need to proceed if we are going to do full detect */
5843 		return false;
5844 
5845 	intel_dp_handle_device_service_irq(intel_dp, esi[1]);
5846 
5847 	/*
5848 	 * Force checking the link status for DPCD_REV < 1.2
5849 	 * TODO: let the link status check depend on LINK_STATUS_CHANGED
5850 	 * or intel_dp->link.training.force_retrain for DPCD_REV >= 1.2
5851 	 */
5852 	esi[3] |= LINK_STATUS_CHANGED;
5853 	if (intel_dp_handle_link_service_irq(intel_dp, esi[3]))
5854 		reprobe_needed = true;
5855 
5856 	/* Handle CEC interrupts, if any */
5857 	drm_dp_cec_irq(&intel_dp->aux);
5858 
5859 	if (READ_ONCE(intel_dp->downstream_port_changed)) {
5860 		WRITE_ONCE(intel_dp->downstream_port_changed, false);
5861 		reprobe_needed = true;
5862 	}
5863 
5864 	intel_psr_short_pulse(intel_dp);
5865 
5866 	if (intel_alpm_get_error(intel_dp)) {
5867 		intel_alpm_disable(intel_dp);
5868 		intel_dp->alpm.sink_alpm_error = true;
5869 	}
5870 
5871 	if (intel_dp_test_short_pulse(intel_dp))
5872 		reprobe_needed = true;
5873 
5874 	return !reprobe_needed;
5875 }
5876 
5877 /* XXX this is probably wrong for multiple downstream ports */
5878 static enum drm_connector_status
5879 intel_dp_detect_dpcd(struct intel_dp *intel_dp)
5880 {
5881 	struct intel_display *display = to_intel_display(intel_dp);
5882 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
5883 	u8 *dpcd = intel_dp->dpcd;
5884 	u8 type;
5885 
5886 	if (drm_WARN_ON(display->drm, intel_dp_is_edp(intel_dp)))
5887 		return connector_status_connected;
5888 
5889 	WRITE_ONCE(intel_dp->downstream_port_changed, false);
5890 
5891 	intel_lspcon_resume(dig_port);
5892 
5893 	if (!intel_dp_get_dpcd(intel_dp))
5894 		return connector_status_disconnected;
5895 
5896 	intel_dp->mst_detect = intel_dp_mst_detect(intel_dp);
5897 
5898 	/* if there's no downstream port, we're done */
5899 	if (!drm_dp_is_branch(dpcd))
5900 		return connector_status_connected;
5901 
5902 	/* If we're HPD-aware, SINK_COUNT changes dynamically */
5903 	if (intel_dp_has_sink_count(intel_dp) &&
5904 	    intel_dp->downstream_ports[0] & DP_DS_PORT_HPD) {
5905 		return intel_dp->sink_count ?
5906 		connector_status_connected : connector_status_disconnected;
5907 	}
5908 
5909 	if (intel_dp->mst_detect == DRM_DP_MST)
5910 		return connector_status_connected;
5911 
5912 	/* If no HPD, poke DDC gently */
5913 	if (drm_probe_ddc(&intel_dp->aux.ddc))
5914 		return connector_status_connected;
5915 
5916 	/* Well we tried, say unknown for unreliable port types */
5917 	if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11) {
5918 		type = intel_dp->downstream_ports[0] & DP_DS_PORT_TYPE_MASK;
5919 		if (type == DP_DS_PORT_TYPE_VGA ||
5920 		    type == DP_DS_PORT_TYPE_NON_EDID)
5921 			return connector_status_unknown;
5922 	} else {
5923 		type = intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
5924 			DP_DWN_STRM_PORT_TYPE_MASK;
5925 		if (type == DP_DWN_STRM_PORT_TYPE_ANALOG ||
5926 		    type == DP_DWN_STRM_PORT_TYPE_OTHER)
5927 			return connector_status_unknown;
5928 	}
5929 
5930 	/* Anything else is out of spec, warn and ignore */
5931 	drm_dbg_kms(display->drm, "Broken DP branch device, ignoring\n");
5932 	return connector_status_disconnected;
5933 }
5934 
5935 static enum drm_connector_status
5936 edp_detect(struct intel_dp *intel_dp)
5937 {
5938 	return connector_status_connected;
5939 }
5940 
5941 void intel_digital_port_lock(struct intel_encoder *encoder)
5942 {
5943 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
5944 
5945 	if (dig_port->lock)
5946 		dig_port->lock(dig_port);
5947 }
5948 
5949 void intel_digital_port_unlock(struct intel_encoder *encoder)
5950 {
5951 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
5952 
5953 	if (dig_port->unlock)
5954 		dig_port->unlock(dig_port);
5955 }
5956 
5957 /*
5958  * intel_digital_port_connected_locked - is the specified port connected?
5959  * @encoder: intel_encoder
5960  *
5961  * In cases where there's a connector physically connected but it can't be used
5962  * by our hardware we also return false, since the rest of the driver should
5963  * pretty much treat the port as disconnected. This is relevant for type-C
5964  * (starting on ICL) where there's ownership involved.
5965  *
5966  * The caller must hold the lock acquired by calling intel_digital_port_lock()
5967  * when calling this function.
5968  *
5969  * Return %true if port is connected, %false otherwise.
5970  */
5971 bool intel_digital_port_connected_locked(struct intel_encoder *encoder)
5972 {
5973 	struct intel_display *display = to_intel_display(encoder);
5974 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
5975 	bool is_glitch_free = intel_tc_port_handles_hpd_glitches(dig_port);
5976 	bool is_connected = false;
5977 
5978 	with_intel_display_power(display, POWER_DOMAIN_DISPLAY_CORE) {
5979 		poll_timeout_us(is_connected = dig_port->connected(encoder),
5980 				is_connected || is_glitch_free,
5981 				30, 4000, false);
5982 	}
5983 
5984 	return is_connected;
5985 }
5986 
5987 bool intel_digital_port_connected(struct intel_encoder *encoder)
5988 {
5989 	bool ret;
5990 
5991 	intel_digital_port_lock(encoder);
5992 	ret = intel_digital_port_connected_locked(encoder);
5993 	intel_digital_port_unlock(encoder);
5994 
5995 	return ret;
5996 }
5997 
5998 static const struct drm_edid *
5999 intel_dp_get_edid(struct intel_dp *intel_dp)
6000 {
6001 	struct intel_connector *connector = intel_dp->attached_connector;
6002 	const struct drm_edid *fixed_edid = connector->panel.fixed_edid;
6003 
6004 	/* Use panel fixed edid if we have one */
6005 	if (fixed_edid) {
6006 		/* invalid edid */
6007 		if (IS_ERR(fixed_edid))
6008 			return NULL;
6009 
6010 		return drm_edid_dup(fixed_edid);
6011 	}
6012 
6013 	return drm_edid_read_ddc(&connector->base, &intel_dp->aux.ddc);
6014 }
6015 
6016 static void
6017 intel_dp_update_dfp(struct intel_dp *intel_dp,
6018 		    const struct drm_edid *drm_edid)
6019 {
6020 	struct intel_display *display = to_intel_display(intel_dp);
6021 	struct intel_connector *connector = intel_dp->attached_connector;
6022 
6023 	intel_dp->dfp.max_bpc =
6024 		drm_dp_downstream_max_bpc(intel_dp->dpcd,
6025 					  intel_dp->downstream_ports, drm_edid);
6026 
6027 	intel_dp->dfp.max_dotclock =
6028 		drm_dp_downstream_max_dotclock(intel_dp->dpcd,
6029 					       intel_dp->downstream_ports);
6030 
6031 	intel_dp->dfp.min_tmds_clock =
6032 		drm_dp_downstream_min_tmds_clock(intel_dp->dpcd,
6033 						 intel_dp->downstream_ports,
6034 						 drm_edid);
6035 	intel_dp->dfp.max_tmds_clock =
6036 		drm_dp_downstream_max_tmds_clock(intel_dp->dpcd,
6037 						 intel_dp->downstream_ports,
6038 						 drm_edid);
6039 
6040 	intel_dp->dfp.pcon_max_frl_bw =
6041 		drm_dp_get_pcon_max_frl_bw(intel_dp->dpcd,
6042 					   intel_dp->downstream_ports);
6043 
6044 	drm_dbg_kms(display->drm,
6045 		    "[CONNECTOR:%d:%s] DFP max bpc %d, max dotclock %d, TMDS clock %d-%d, PCON Max FRL BW %dGbps\n",
6046 		    connector->base.base.id, connector->base.name,
6047 		    intel_dp->dfp.max_bpc,
6048 		    intel_dp->dfp.max_dotclock,
6049 		    intel_dp->dfp.min_tmds_clock,
6050 		    intel_dp->dfp.max_tmds_clock,
6051 		    intel_dp->dfp.pcon_max_frl_bw);
6052 
6053 	intel_dp_get_pcon_dsc_cap(intel_dp);
6054 }
6055 
6056 static bool
6057 intel_dp_can_ycbcr420(struct intel_dp *intel_dp)
6058 {
6059 	if (source_can_output(intel_dp, INTEL_OUTPUT_FORMAT_YCBCR420) &&
6060 	    (!drm_dp_is_branch(intel_dp->dpcd) || intel_dp->dfp.ycbcr420_passthrough))
6061 		return true;
6062 
6063 	if (source_can_output(intel_dp, INTEL_OUTPUT_FORMAT_RGB) &&
6064 	    dfp_can_convert_from_rgb(intel_dp, INTEL_OUTPUT_FORMAT_YCBCR420))
6065 		return true;
6066 
6067 	if (source_can_output(intel_dp, INTEL_OUTPUT_FORMAT_YCBCR444) &&
6068 	    dfp_can_convert_from_ycbcr444(intel_dp, INTEL_OUTPUT_FORMAT_YCBCR420))
6069 		return true;
6070 
6071 	return false;
6072 }
6073 
6074 static void
6075 intel_dp_update_420(struct intel_dp *intel_dp)
6076 {
6077 	struct intel_display *display = to_intel_display(intel_dp);
6078 	struct intel_connector *connector = intel_dp->attached_connector;
6079 
6080 	intel_dp->dfp.ycbcr420_passthrough =
6081 		drm_dp_downstream_420_passthrough(intel_dp->dpcd,
6082 						  intel_dp->downstream_ports);
6083 	/* on-board LSPCON always assumed to support 4:4:4->4:2:0 conversion */
6084 	intel_dp->dfp.ycbcr_444_to_420 =
6085 		intel_lspcon_active(dp_to_dig_port(intel_dp)) ||
6086 		drm_dp_downstream_444_to_420_conversion(intel_dp->dpcd,
6087 							intel_dp->downstream_ports);
6088 	intel_dp->dfp.rgb_to_ycbcr =
6089 		drm_dp_downstream_rgb_to_ycbcr_conversion(intel_dp->dpcd,
6090 							  intel_dp->downstream_ports,
6091 							  DP_DS_HDMI_BT709_RGB_YCBCR_CONV);
6092 
6093 	connector->base.ycbcr_420_allowed = intel_dp_can_ycbcr420(intel_dp);
6094 
6095 	drm_dbg_kms(display->drm,
6096 		    "[CONNECTOR:%d:%s] RGB->YcbCr conversion? %s, YCbCr 4:2:0 allowed? %s, YCbCr 4:4:4->4:2:0 conversion? %s\n",
6097 		    connector->base.base.id, connector->base.name,
6098 		    str_yes_no(intel_dp->dfp.rgb_to_ycbcr),
6099 		    str_yes_no(connector->base.ycbcr_420_allowed),
6100 		    str_yes_no(intel_dp->dfp.ycbcr_444_to_420));
6101 }
6102 
6103 static void
6104 intel_dp_set_edid(struct intel_dp *intel_dp)
6105 {
6106 	struct intel_display *display = to_intel_display(intel_dp);
6107 	struct intel_connector *connector = intel_dp->attached_connector;
6108 	const struct drm_edid *drm_edid;
6109 	bool vrr_capable;
6110 
6111 	intel_dp_unset_edid(intel_dp);
6112 	drm_edid = intel_dp_get_edid(intel_dp);
6113 	connector->detect_edid = drm_edid;
6114 
6115 	/* Below we depend on display info having been updated */
6116 	drm_edid_connector_update(&connector->base, drm_edid);
6117 
6118 	vrr_capable = intel_vrr_is_capable(connector);
6119 	drm_dbg_kms(display->drm, "[CONNECTOR:%d:%s] VRR capable: %s\n",
6120 		    connector->base.base.id, connector->base.name, str_yes_no(vrr_capable));
6121 	drm_connector_set_vrr_capable_property(&connector->base, vrr_capable);
6122 
6123 	intel_dp_update_dfp(intel_dp, drm_edid);
6124 	intel_dp_update_420(intel_dp);
6125 
6126 	drm_dp_cec_attach(&intel_dp->aux,
6127 			  connector->base.display_info.source_physical_address);
6128 }
6129 
6130 static void
6131 intel_dp_unset_edid(struct intel_dp *intel_dp)
6132 {
6133 	struct intel_connector *connector = intel_dp->attached_connector;
6134 
6135 	drm_dp_cec_unset_edid(&intel_dp->aux);
6136 	drm_edid_free(connector->detect_edid);
6137 	connector->detect_edid = NULL;
6138 
6139 	intel_dp->dfp.max_bpc = 0;
6140 	intel_dp->dfp.max_dotclock = 0;
6141 	intel_dp->dfp.min_tmds_clock = 0;
6142 	intel_dp->dfp.max_tmds_clock = 0;
6143 
6144 	intel_dp->dfp.pcon_max_frl_bw = 0;
6145 
6146 	intel_dp->dfp.ycbcr_444_to_420 = false;
6147 	connector->base.ycbcr_420_allowed = false;
6148 
6149 	drm_connector_set_vrr_capable_property(&connector->base,
6150 					       false);
6151 }
6152 
6153 static bool
6154 intel_dp_sink_supports_as_sdp_v2(struct intel_dp *intel_dp)
6155 {
6156 	u8 rx_features;
6157 
6158 	/*
6159 	 * The DP spec does not explicitly provide the AS SDP v2 capability.
6160 	 * So based on the DP v2.1 SCR, we infer it from the following bits:
6161 	 *
6162 	 * DP_AS_SDP_FAVT_PAYLOAD_FIELDS_PARSING_SUPPORTED indicates support for
6163 	 * FAVT, which is explicitly defined to use AS SDP v2.
6164 	 *
6165 	 * DP_ASYNC_VIDEO_TIMING_NOT_SUPPORTED_IN_PR indicates that the sink
6166 	 * does not support asynchronous video timing while in PR Active,
6167 	 * requiring the source to keep transmitting Adaptive-Sync SDPs. The
6168 	 * spec mandates that such sinks shall support AS SDP v2.
6169 	 *
6170 	 * #TODO: Check the Adaptive-Sync DisplayID 2.1 block once DisplayID
6171 	 * parsing is available. This may help detect AS SDP v2 support for
6172 	 * native DP 2.1 sinks that do not expose FAVT or PR-based capability
6173 	 * bits.
6174 	 *
6175 	 * In the presence of PCONs, check PCON support from DPCD and sink
6176 	 * support from Display ID.
6177 	 */
6178 
6179 	if (drm_dp_dpcd_read_byte(&intel_dp->aux,
6180 				  DP_DPRX_FEATURE_ENUMERATION_LIST_CONT_1,
6181 				  &rx_features) == 1) {
6182 		if (rx_features & DP_AS_SDP_FAVT_PAYLOAD_FIELDS_PARSING_SUPPORTED)
6183 			return true;
6184 	}
6185 
6186 	if (intel_dp->psr.sink_panel_replay_support &&
6187 	    !intel_psr_pr_async_video_timing_supported(intel_dp))
6188 		return true;
6189 
6190 	return false;
6191 }
6192 
6193 static void
6194 intel_dp_detect_sdp_caps(struct intel_dp *intel_dp)
6195 {
6196 	struct intel_display *display = to_intel_display(intel_dp);
6197 
6198 	intel_dp->as_sdp_supported = HAS_AS_SDP(display) &&
6199 		drm_dp_as_sdp_supported(&intel_dp->aux, intel_dp->dpcd);
6200 
6201 	if (!intel_dp->as_sdp_supported)
6202 		return;
6203 
6204 	/* eDP Adaptive-Sync SDP always uses AS SDP v2 */
6205 	if (intel_dp_is_edp(intel_dp))
6206 		intel_dp->as_sdp_v2_supported =  true;
6207 	else
6208 		intel_dp->as_sdp_v2_supported = intel_dp_sink_supports_as_sdp_v2(intel_dp);
6209 }
6210 
6211 static bool intel_dp_needs_dpcd_probe(struct intel_dp *intel_dp, bool force_on_external)
6212 {
6213 	struct intel_connector *connector = intel_dp->attached_connector;
6214 
6215 	if (intel_dp_is_edp(intel_dp))
6216 		return false;
6217 
6218 	if (force_on_external)
6219 		return true;
6220 
6221 	if (intel_dp->is_mst)
6222 		return false;
6223 
6224 	return drm_edid_has_quirk(&connector->base, DRM_EDID_QUIRK_DP_DPCD_PROBE);
6225 }
6226 
6227 void intel_dp_dpcd_set_probe(struct intel_dp *intel_dp, bool force_on_external)
6228 {
6229 	drm_dp_dpcd_set_probe(&intel_dp->aux,
6230 			      intel_dp_needs_dpcd_probe(intel_dp, force_on_external));
6231 }
6232 
6233 static int
6234 intel_dp_detect(struct drm_connector *_connector,
6235 		struct drm_modeset_acquire_ctx *ctx,
6236 		bool force)
6237 {
6238 	struct intel_display *display = to_intel_display(_connector->dev);
6239 	struct intel_connector *connector = to_intel_connector(_connector);
6240 	struct intel_dp *intel_dp = intel_attached_dp(connector);
6241 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
6242 	struct intel_encoder *encoder = &dig_port->base;
6243 	enum drm_connector_status status;
6244 	int ret;
6245 
6246 	drm_dbg_kms(display->drm, "[CONNECTOR:%d:%s]\n",
6247 		    connector->base.base.id, connector->base.name);
6248 	drm_WARN_ON(display->drm,
6249 		    !drm_modeset_is_locked(&display->drm->mode_config.connection_mutex));
6250 
6251 	if (!intel_display_device_enabled(display))
6252 		return connector_status_disconnected;
6253 
6254 	if (!intel_display_driver_check_access(display))
6255 		return connector->base.status;
6256 
6257 	intel_dp_flush_connector_commits(connector);
6258 
6259 	intel_pps_vdd_on(intel_dp);
6260 
6261 	/* Can't disconnect eDP */
6262 	if (intel_dp_is_edp(intel_dp))
6263 		status = edp_detect(intel_dp);
6264 	else if (intel_digital_port_connected(encoder))
6265 		status = intel_dp_detect_dpcd(intel_dp);
6266 	else
6267 		status = connector_status_disconnected;
6268 
6269 	if (status != connector_status_disconnected &&
6270 	    !intel_dp_mst_verify_dpcd_state(intel_dp))
6271 		/*
6272 		 * This requires retrying detection for instance to re-enable
6273 		 * the MST mode that got reset via a long HPD pulse. The retry
6274 		 * will happen either via the hotplug handler's retry logic,
6275 		 * ensured by setting the connector here to SST/disconnected,
6276 		 * or via a userspace connector probing in response to the
6277 		 * hotplug uevent sent when removing the MST connectors.
6278 		 */
6279 		status = connector_status_disconnected;
6280 
6281 	if (status == connector_status_disconnected) {
6282 		intel_dp_test_reset(intel_dp);
6283 		/*
6284 		 * FIXME: Resetting these caps here cause
6285 		 * state computation fail if the connector need to be
6286 		 * modeset after sink disconnect. Move resetting them
6287 		 * to where new sink is connected.
6288 		 */
6289 		memset(connector->dp.dsc_dpcd, 0, sizeof(connector->dp.dsc_dpcd));
6290 		memset(connector->dp.panel_replay_caps.dpcd, 0,
6291 		       sizeof(connector->dp.panel_replay_caps.dpcd));
6292 		intel_dp->psr.sink_panel_replay_support = false;
6293 		connector->dp.panel_replay_caps.support = false;
6294 		connector->dp.panel_replay_caps.su_support = false;
6295 		connector->dp.panel_replay_caps.dsc_support =
6296 			INTEL_DP_PANEL_REPLAY_DSC_NOT_SUPPORTED;
6297 
6298 		intel_dp_mst_disconnect(intel_dp);
6299 
6300 		intel_dp_tunnel_disconnect(intel_dp);
6301 
6302 		goto out_unset_edid;
6303 	}
6304 
6305 	intel_dp_init_source_oui(intel_dp);
6306 
6307 	ret = intel_dp_tunnel_detect(intel_dp, ctx);
6308 	if (ret == -EDEADLK) {
6309 		status = ret;
6310 
6311 		goto out_vdd_off;
6312 	}
6313 
6314 	if (ret == 1)
6315 		connector->base.epoch_counter++;
6316 
6317 	if (!intel_dp_is_edp(intel_dp))
6318 		intel_psr_init_dpcd(intel_dp, connector);
6319 
6320 	intel_dp_detect_dsc_caps(intel_dp, connector);
6321 
6322 	intel_dp_detect_sdp_caps(intel_dp);
6323 
6324 	if (intel_dp->reset_link_params) {
6325 		intel_dp_reset_link_params(intel_dp);
6326 		intel_dp->reset_link_params = false;
6327 	}
6328 
6329 	intel_dp_mst_configure(intel_dp);
6330 
6331 	intel_dp_print_rates(intel_dp);
6332 
6333 	if (intel_dp->is_mst) {
6334 		/*
6335 		 * If we are in MST mode then this connector
6336 		 * won't appear connected or have anything
6337 		 * with EDID on it
6338 		 */
6339 		status = connector_status_disconnected;
6340 		goto out_unset_edid;
6341 	}
6342 
6343 	/*
6344 	 * Some external monitors do not signal loss of link synchronization
6345 	 * with an IRQ_HPD, so force a link status check.
6346 	 *
6347 	 * TODO: this probably became redundant, so remove it: the link state
6348 	 * is rechecked/recovered now after modesets, where the loss of
6349 	 * synchronization tends to occur.
6350 	 */
6351 	if (!intel_dp_is_edp(intel_dp))
6352 		intel_dp_check_link_state(intel_dp);
6353 
6354 	/*
6355 	 * Clearing NACK and defer counts to get their exact values
6356 	 * while reading EDID which are required by Compliance tests
6357 	 * 4.2.2.4 and 4.2.2.5
6358 	 */
6359 	intel_dp->aux.i2c_nack_count = 0;
6360 	intel_dp->aux.i2c_defer_count = 0;
6361 
6362 	intel_dp_set_edid(intel_dp);
6363 	if (intel_dp_is_edp(intel_dp) || connector->detect_edid)
6364 		status = connector_status_connected;
6365 
6366 out_unset_edid:
6367 	if (status != connector_status_connected && !intel_dp->is_mst)
6368 		intel_dp_unset_edid(intel_dp);
6369 
6370 	intel_dp_dpcd_set_probe(intel_dp, false);
6371 
6372 	if (!intel_dp_is_edp(intel_dp))
6373 		drm_dp_set_subconnector_property(&connector->base,
6374 						 status,
6375 						 intel_dp->dpcd,
6376 						 intel_dp->downstream_ports);
6377 out_vdd_off:
6378 	intel_pps_vdd_off(intel_dp);
6379 
6380 	return status;
6381 }
6382 
6383 static void
6384 intel_dp_force(struct drm_connector *_connector)
6385 {
6386 	struct intel_connector *connector = to_intel_connector(_connector);
6387 	struct intel_display *display = to_intel_display(connector);
6388 	struct intel_dp *intel_dp = intel_attached_dp(connector);
6389 
6390 	drm_dbg_kms(display->drm, "[CONNECTOR:%d:%s]\n",
6391 		    connector->base.base.id, connector->base.name);
6392 
6393 	if (!intel_display_driver_check_access(display))
6394 		return;
6395 
6396 	intel_dp_unset_edid(intel_dp);
6397 
6398 	if (connector->base.status != connector_status_connected)
6399 		return;
6400 
6401 	intel_dp_set_edid(intel_dp);
6402 
6403 	intel_dp_dpcd_set_probe(intel_dp, false);
6404 }
6405 
6406 static int intel_dp_get_modes(struct drm_connector *_connector)
6407 {
6408 	struct intel_display *display = to_intel_display(_connector->dev);
6409 	struct intel_connector *connector = to_intel_connector(_connector);
6410 	struct intel_dp *intel_dp = intel_attached_dp(connector);
6411 	int num_modes;
6412 
6413 	/* drm_edid_connector_update() done in ->detect() or ->force() */
6414 	num_modes = drm_edid_connector_add_modes(&connector->base);
6415 
6416 	/* Also add fixed mode, which may or may not be present in EDID */
6417 	if (intel_dp_is_edp(intel_dp))
6418 		num_modes += intel_panel_get_modes(connector);
6419 
6420 	if (num_modes)
6421 		return num_modes;
6422 
6423 	if (!connector->detect_edid) {
6424 		struct drm_display_mode *mode;
6425 
6426 		mode = drm_dp_downstream_mode(display->drm,
6427 					      intel_dp->dpcd,
6428 					      intel_dp->downstream_ports);
6429 		if (mode) {
6430 			drm_mode_probed_add(&connector->base, mode);
6431 			num_modes++;
6432 		}
6433 	}
6434 
6435 	return num_modes;
6436 }
6437 
6438 static int
6439 intel_dp_connector_register(struct drm_connector *_connector)
6440 {
6441 	struct intel_connector *connector = to_intel_connector(_connector);
6442 	struct intel_display *display = to_intel_display(connector);
6443 	struct intel_dp *intel_dp = intel_attached_dp(connector);
6444 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
6445 	int ret;
6446 
6447 	ret = intel_connector_register(&connector->base);
6448 	if (ret)
6449 		return ret;
6450 
6451 	drm_dbg_kms(display->drm, "registering %s bus for %s\n",
6452 		    intel_dp->aux.name, connector->base.kdev->kobj.name);
6453 
6454 	intel_dp->aux.dev = connector->base.kdev;
6455 	ret = drm_dp_aux_register(&intel_dp->aux);
6456 	if (!ret)
6457 		drm_dp_cec_register_connector(&intel_dp->aux, &connector->base);
6458 
6459 	if (!intel_bios_encoder_is_lspcon(dig_port->base.devdata))
6460 		return ret;
6461 
6462 	/*
6463 	 * ToDo: Clean this up to handle lspcon init and resume more
6464 	 * efficiently and streamlined.
6465 	 */
6466 	if (intel_lspcon_init(dig_port)) {
6467 		if (intel_lspcon_detect_hdr_capability(dig_port))
6468 			drm_connector_attach_hdr_output_metadata_property(&connector->base);
6469 	}
6470 
6471 	return ret;
6472 }
6473 
6474 static void
6475 intel_dp_connector_unregister(struct drm_connector *_connector)
6476 {
6477 	struct intel_connector *connector = to_intel_connector(_connector);
6478 	struct intel_dp *intel_dp = intel_attached_dp(connector);
6479 
6480 	drm_dp_cec_unregister_connector(&intel_dp->aux);
6481 	drm_dp_aux_unregister(&intel_dp->aux);
6482 	intel_connector_unregister(&connector->base);
6483 }
6484 
6485 void intel_dp_connector_sync_state(struct intel_connector *connector,
6486 				   const struct intel_crtc_state *crtc_state)
6487 {
6488 	struct intel_display *display = to_intel_display(connector);
6489 
6490 	if (crtc_state && crtc_state->dsc.compression_enable) {
6491 		drm_WARN_ON(display->drm,
6492 			    !connector->dp.dsc_decompression_aux);
6493 		connector->dp.dsc_decompression_enabled = true;
6494 	} else {
6495 		connector->dp.dsc_decompression_enabled = false;
6496 	}
6497 }
6498 
6499 void intel_dp_encoder_flush_work(struct drm_encoder *_encoder)
6500 {
6501 	struct intel_encoder *encoder = to_intel_encoder(_encoder);
6502 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
6503 	struct intel_dp *intel_dp = &dig_port->dp;
6504 
6505 	intel_encoder_link_check_flush_work(encoder);
6506 
6507 	intel_dp_mst_encoder_cleanup(dig_port);
6508 
6509 	intel_dp_tunnel_destroy(intel_dp);
6510 
6511 	intel_pps_vdd_off_sync(intel_dp);
6512 
6513 	/*
6514 	 * Ensure power off delay is respected on module remove, so that we can
6515 	 * reduce delays at driver probe. See pps_init_timestamps().
6516 	 */
6517 	intel_pps_wait_power_cycle(intel_dp);
6518 
6519 	intel_dp_aux_fini(intel_dp);
6520 }
6521 
6522 void intel_dp_encoder_suspend(struct intel_encoder *encoder)
6523 {
6524 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
6525 
6526 	intel_pps_vdd_off_sync(intel_dp);
6527 
6528 	intel_dp_tunnel_suspend(intel_dp);
6529 }
6530 
6531 void intel_dp_encoder_shutdown(struct intel_encoder *encoder)
6532 {
6533 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
6534 
6535 	intel_pps_wait_power_cycle(intel_dp);
6536 }
6537 
6538 static int intel_modeset_tile_group(struct intel_atomic_state *state,
6539 				    int tile_group_id)
6540 {
6541 	struct intel_display *display = to_intel_display(state);
6542 	struct drm_connector_list_iter conn_iter;
6543 	struct intel_connector *connector;
6544 	int ret = 0;
6545 
6546 	drm_connector_list_iter_begin(display->drm, &conn_iter);
6547 	for_each_intel_connector_iter(connector, &conn_iter) {
6548 		struct drm_connector_state *conn_state;
6549 		struct intel_crtc_state *crtc_state;
6550 		struct intel_crtc *crtc;
6551 
6552 		if (!connector->base.has_tile ||
6553 		    connector->base.tile_group->id != tile_group_id)
6554 			continue;
6555 
6556 		conn_state = drm_atomic_get_connector_state(&state->base,
6557 							    &connector->base);
6558 		if (IS_ERR(conn_state)) {
6559 			ret = PTR_ERR(conn_state);
6560 			break;
6561 		}
6562 
6563 		crtc = to_intel_crtc(conn_state->crtc);
6564 
6565 		if (!crtc)
6566 			continue;
6567 
6568 		crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
6569 		crtc_state->uapi.mode_changed = true;
6570 
6571 		ret = drm_atomic_add_affected_planes(&state->base, &crtc->base);
6572 		if (ret)
6573 			break;
6574 	}
6575 	drm_connector_list_iter_end(&conn_iter);
6576 
6577 	return ret;
6578 }
6579 
6580 static int intel_modeset_affected_transcoders(struct intel_atomic_state *state, u8 transcoders)
6581 {
6582 	struct intel_display *display = to_intel_display(state);
6583 	struct intel_crtc *crtc;
6584 
6585 	if (transcoders == 0)
6586 		return 0;
6587 
6588 	for_each_intel_crtc(display, crtc) {
6589 		struct intel_crtc_state *crtc_state;
6590 		int ret;
6591 
6592 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
6593 		if (IS_ERR(crtc_state))
6594 			return PTR_ERR(crtc_state);
6595 
6596 		if (!crtc_state->hw.enable)
6597 			continue;
6598 
6599 		if (!(transcoders & BIT(crtc_state->cpu_transcoder)))
6600 			continue;
6601 
6602 		crtc_state->uapi.mode_changed = true;
6603 
6604 		ret = drm_atomic_add_affected_connectors(&state->base, &crtc->base);
6605 		if (ret)
6606 			return ret;
6607 
6608 		ret = drm_atomic_add_affected_planes(&state->base, &crtc->base);
6609 		if (ret)
6610 			return ret;
6611 
6612 		transcoders &= ~BIT(crtc_state->cpu_transcoder);
6613 	}
6614 
6615 	drm_WARN_ON(display->drm, transcoders != 0);
6616 
6617 	return 0;
6618 }
6619 
6620 static int intel_modeset_synced_crtcs(struct intel_atomic_state *state,
6621 				      struct drm_connector *_connector)
6622 {
6623 	struct intel_connector *connector = to_intel_connector(_connector);
6624 	const struct drm_connector_state *old_conn_state =
6625 		drm_atomic_get_old_connector_state(&state->base, &connector->base);
6626 	const struct intel_crtc_state *old_crtc_state;
6627 	struct intel_crtc *crtc;
6628 	u8 transcoders;
6629 
6630 	crtc = to_intel_crtc(old_conn_state->crtc);
6631 	if (!crtc)
6632 		return 0;
6633 
6634 	old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc);
6635 
6636 	if (!old_crtc_state->hw.active)
6637 		return 0;
6638 
6639 	transcoders = old_crtc_state->sync_mode_slaves_mask;
6640 	if (old_crtc_state->master_transcoder != INVALID_TRANSCODER)
6641 		transcoders |= BIT(old_crtc_state->master_transcoder);
6642 
6643 	return intel_modeset_affected_transcoders(state,
6644 						  transcoders);
6645 }
6646 
6647 static int intel_dp_connector_atomic_check(struct drm_connector *_connector,
6648 					   struct drm_atomic_commit *_state)
6649 {
6650 	struct intel_connector *connector = to_intel_connector(_connector);
6651 	struct intel_display *display = to_intel_display(connector);
6652 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
6653 	struct drm_connector_state *conn_state =
6654 		drm_atomic_get_new_connector_state(_state, &connector->base);
6655 	struct intel_dp *intel_dp = enc_to_intel_dp(connector->encoder);
6656 	int ret;
6657 
6658 	ret = intel_digital_connector_atomic_check(&connector->base, &state->base);
6659 	if (ret)
6660 		return ret;
6661 
6662 	if (intel_dp_mst_source_support(intel_dp)) {
6663 		ret = drm_dp_mst_root_conn_atomic_check(conn_state, &intel_dp->mst.mgr);
6664 		if (ret)
6665 			return ret;
6666 	}
6667 
6668 	if (!intel_connector_needs_modeset(state, &connector->base))
6669 		return 0;
6670 
6671 	ret = intel_dp_tunnel_atomic_check_state(state,
6672 						 intel_dp,
6673 						 connector);
6674 	if (ret)
6675 		return ret;
6676 
6677 	/*
6678 	 * We don't enable port sync on BDW due to missing w/as and
6679 	 * due to not having adjusted the modeset sequence appropriately.
6680 	 */
6681 	if (DISPLAY_VER(display) < 9)
6682 		return 0;
6683 
6684 	if (connector->base.has_tile) {
6685 		ret = intel_modeset_tile_group(state, connector->base.tile_group->id);
6686 		if (ret)
6687 			return ret;
6688 	}
6689 
6690 	return intel_modeset_synced_crtcs(state, &connector->base);
6691 }
6692 
6693 static void intel_dp_oob_hotplug_event(struct drm_connector *_connector,
6694 				       enum drm_connector_status hpd_state)
6695 {
6696 	struct intel_connector *connector = to_intel_connector(_connector);
6697 	struct intel_display *display = to_intel_display(connector);
6698 	struct intel_encoder *encoder = intel_attached_encoder(connector);
6699 	bool hpd_high = hpd_state == connector_status_connected;
6700 	unsigned int hpd_pin = encoder->hpd_pin;
6701 	bool need_work = false;
6702 
6703 	spin_lock_irq(&display->irq.lock);
6704 	if (hpd_high != test_bit(hpd_pin, &display->hotplug.oob_hotplug_last_state)) {
6705 		display->hotplug.event_bits |= BIT(hpd_pin);
6706 
6707 		__assign_bit(hpd_pin,
6708 			     &display->hotplug.oob_hotplug_last_state,
6709 			     hpd_high);
6710 		need_work = true;
6711 	}
6712 	spin_unlock_irq(&display->irq.lock);
6713 
6714 	if (need_work)
6715 		intel_hpd_schedule_detection(display);
6716 }
6717 
6718 static const struct drm_connector_funcs intel_dp_connector_funcs = {
6719 	.force = intel_dp_force,
6720 	.fill_modes = drm_helper_probe_single_connector_modes,
6721 	.atomic_get_property = intel_digital_connector_atomic_get_property,
6722 	.atomic_set_property = intel_digital_connector_atomic_set_property,
6723 	.late_register = intel_dp_connector_register,
6724 	.early_unregister = intel_dp_connector_unregister,
6725 	.destroy = intel_connector_destroy,
6726 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
6727 	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
6728 	.oob_hotplug_event = intel_dp_oob_hotplug_event,
6729 };
6730 
6731 static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
6732 	.detect_ctx = intel_dp_detect,
6733 	.get_modes = intel_dp_get_modes,
6734 	.mode_valid = intel_dp_mode_valid,
6735 	.atomic_check = intel_dp_connector_atomic_check,
6736 };
6737 
6738 enum irqreturn
6739 intel_dp_hpd_pulse(struct intel_digital_port *dig_port, bool long_hpd)
6740 {
6741 	struct intel_display *display = to_intel_display(dig_port);
6742 	struct intel_dp *intel_dp = &dig_port->dp;
6743 	u8 dpcd[DP_RECEIVER_CAP_SIZE];
6744 
6745 	if (dig_port->base.type == INTEL_OUTPUT_EDP &&
6746 	    (long_hpd ||
6747 	     intel_display_rpm_suspended(display) ||
6748 	     !intel_pps_have_panel_power_or_vdd(intel_dp))) {
6749 		/*
6750 		 * vdd off can generate a long/short pulse on eDP which
6751 		 * would require vdd on to handle it, and thus we
6752 		 * would end up in an endless cycle of
6753 		 * "vdd off -> long/short hpd -> vdd on -> detect -> vdd off -> ..."
6754 		 */
6755 		drm_dbg_kms(display->drm,
6756 			    "ignoring %s hpd on eDP [ENCODER:%d:%s]\n",
6757 			    long_hpd ? "long" : "short",
6758 			    dig_port->base.base.base.id,
6759 			    dig_port->base.base.name);
6760 		return IRQ_HANDLED;
6761 	}
6762 
6763 	drm_dbg_kms(display->drm, "got hpd irq on [ENCODER:%d:%s] - %s\n",
6764 		    dig_port->base.base.base.id,
6765 		    dig_port->base.base.name,
6766 		    long_hpd ? "long" : "short");
6767 
6768 	/*
6769 	 * TBT DP tunnels require the GFX driver to read out the DPRX caps in
6770 	 * response to long HPD pulses. The DP hotplug handler does that,
6771 	 * however the hotplug handler may be blocked by another
6772 	 * connector's/encoder's hotplug handler. Since the TBT CM may not
6773 	 * complete the DP tunnel BW request for the latter connector/encoder
6774 	 * waiting for this encoder's DPRX read, perform a dummy read here.
6775 	 */
6776 	if (long_hpd) {
6777 		intel_dp_dpcd_set_probe(intel_dp, true);
6778 
6779 		intel_dp_read_dprx_caps(intel_dp, dpcd);
6780 
6781 		intel_dp->reset_link_params = true;
6782 		intel_dp_invalidate_source_oui(intel_dp);
6783 
6784 		return IRQ_NONE;
6785 	}
6786 
6787 	if (intel_dp->is_mst) {
6788 		if (!intel_dp_check_mst_status(intel_dp))
6789 			return IRQ_NONE;
6790 	} else if (!intel_dp_short_pulse(intel_dp)) {
6791 		return IRQ_NONE;
6792 	}
6793 
6794 	return IRQ_HANDLED;
6795 }
6796 
6797 static bool _intel_dp_is_port_edp(struct intel_display *display,
6798 				  const struct intel_bios_encoder_data *devdata,
6799 				  enum port port)
6800 {
6801 	/*
6802 	 * eDP not supported on g4x. so bail out early just
6803 	 * for a bit extra safety in case the VBT is bonkers.
6804 	 */
6805 	if (DISPLAY_VER(display) < 5)
6806 		return false;
6807 
6808 	if (DISPLAY_VER(display) < 9 && port == PORT_A)
6809 		return true;
6810 
6811 	return devdata && intel_bios_encoder_supports_edp(devdata);
6812 }
6813 
6814 bool intel_dp_is_port_edp(struct intel_display *display, enum port port)
6815 {
6816 	const struct intel_bios_encoder_data *devdata =
6817 		intel_bios_encoder_data_lookup(display, port);
6818 
6819 	return _intel_dp_is_port_edp(display, devdata, port);
6820 }
6821 
6822 bool
6823 intel_dp_has_gamut_metadata_dip(struct intel_encoder *encoder)
6824 {
6825 	struct intel_display *display = to_intel_display(encoder);
6826 	enum port port = encoder->port;
6827 
6828 	if (intel_bios_encoder_is_lspcon(encoder->devdata))
6829 		return false;
6830 
6831 	if (DISPLAY_VER(display) >= 11)
6832 		return true;
6833 
6834 	if (port == PORT_A)
6835 		return false;
6836 
6837 	if (display->platform.haswell || display->platform.broadwell ||
6838 	    DISPLAY_VER(display) >= 9)
6839 		return true;
6840 
6841 	return false;
6842 }
6843 
6844 static void
6845 intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *_connector)
6846 {
6847 	struct intel_connector *connector = to_intel_connector(_connector);
6848 	struct intel_display *display = to_intel_display(intel_dp);
6849 	enum port port = dp_to_dig_port(intel_dp)->base.port;
6850 
6851 	if (!intel_dp_is_edp(intel_dp))
6852 		drm_connector_attach_dp_subconnector_property(&connector->base);
6853 
6854 	if (!display->platform.g4x && port != PORT_A)
6855 		intel_attach_force_audio_property(&connector->base);
6856 
6857 	intel_attach_broadcast_rgb_property(&connector->base);
6858 	if (HAS_GMCH(display))
6859 		drm_connector_attach_max_bpc_property(&connector->base, 6, 10);
6860 	else if (DISPLAY_VER(display) >= 5)
6861 		drm_connector_attach_max_bpc_property(&connector->base, 6, 12);
6862 
6863 	/* Register HDMI colorspace for case of lspcon */
6864 	if (intel_bios_encoder_is_lspcon(dp_to_dig_port(intel_dp)->base.devdata)) {
6865 		drm_connector_attach_content_type_property(&connector->base);
6866 		intel_attach_hdmi_colorspace_property(&connector->base);
6867 	} else {
6868 		intel_attach_dp_colorspace_property(&connector->base);
6869 	}
6870 
6871 	if (intel_dp_has_gamut_metadata_dip(&dp_to_dig_port(intel_dp)->base))
6872 		drm_connector_attach_hdr_output_metadata_property(&connector->base);
6873 
6874 	if (HAS_VRR(display))
6875 		drm_connector_attach_vrr_capable_property(&connector->base);
6876 }
6877 
6878 static void
6879 intel_edp_add_properties(struct intel_dp *intel_dp)
6880 {
6881 	struct intel_display *display = to_intel_display(intel_dp);
6882 	struct intel_connector *connector = intel_dp->attached_connector;
6883 	const struct drm_display_mode *fixed_mode =
6884 		intel_panel_preferred_fixed_mode(connector);
6885 
6886 	intel_attach_scaling_mode_property(&connector->base);
6887 
6888 	drm_connector_set_panel_orientation_with_quirk(&connector->base,
6889 						       display->vbt.orientation,
6890 						       fixed_mode->hdisplay,
6891 						       fixed_mode->vdisplay);
6892 }
6893 
6894 static void intel_edp_backlight_setup(struct intel_dp *intel_dp,
6895 				      struct intel_connector *connector)
6896 {
6897 	struct intel_display *display = to_intel_display(intel_dp);
6898 	enum pipe pipe = INVALID_PIPE;
6899 
6900 	if (display->platform.valleyview || display->platform.cherryview)
6901 		pipe = vlv_pps_backlight_initial_pipe(intel_dp);
6902 
6903 	intel_backlight_setup(connector, pipe);
6904 }
6905 
6906 static bool intel_edp_init_connector(struct intel_dp *intel_dp,
6907 				     struct intel_connector *connector)
6908 {
6909 	struct intel_display *display = to_intel_display(intel_dp);
6910 	struct drm_display_mode *fixed_mode;
6911 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
6912 	bool has_dpcd;
6913 	const struct drm_edid *drm_edid;
6914 
6915 	if (!intel_dp_is_edp(intel_dp))
6916 		return true;
6917 
6918 	/*
6919 	 * On IBX/CPT we may get here with LVDS already registered. Since the
6920 	 * driver uses the only internal power sequencer available for both
6921 	 * eDP and LVDS bail out early in this case to prevent interfering
6922 	 * with an already powered-on LVDS power sequencer.
6923 	 */
6924 	if (intel_get_lvds_encoder(display)) {
6925 		drm_WARN_ON(display->drm,
6926 			    !(HAS_PCH_IBX(display) || HAS_PCH_CPT(display)));
6927 		drm_info(display->drm,
6928 			 "LVDS was detected, not registering eDP\n");
6929 
6930 		return false;
6931 	}
6932 
6933 	intel_bios_init_panel_early(display, &connector->panel,
6934 				    encoder->devdata);
6935 
6936 	if (!intel_pps_init(intel_dp)) {
6937 		drm_info(display->drm,
6938 			 "[ENCODER:%d:%s] unusable PPS, disabling eDP\n",
6939 			 encoder->base.base.id, encoder->base.name);
6940 		/*
6941 		 * The BIOS may have still enabled VDD on the PPS even
6942 		 * though it's unusable. Make sure we turn it back off
6943 		 * and to release the power domain references/etc.
6944 		 */
6945 		goto out_vdd_off;
6946 	}
6947 
6948 	/*
6949 	 * Enable HPD sense for live status check.
6950 	 * intel_hpd_irq_setup() will turn it off again
6951 	 * if it's no longer needed later.
6952 	 *
6953 	 * The DPCD probe below will make sure VDD is on.
6954 	 */
6955 	intel_hpd_enable_detection(encoder);
6956 
6957 	intel_alpm_init(intel_dp);
6958 
6959 	/* Cache DPCD and EDID for edp. */
6960 	has_dpcd = intel_edp_init_dpcd(intel_dp, connector);
6961 
6962 	if (!has_dpcd) {
6963 		/* if this fails, presume the device is a ghost */
6964 		drm_info(display->drm,
6965 			 "[ENCODER:%d:%s] failed to retrieve link info, disabling eDP\n",
6966 			 encoder->base.base.id, encoder->base.name);
6967 		goto out_vdd_off;
6968 	}
6969 
6970 	/*
6971 	 * VBT and straps are liars. Also check HPD as that seems
6972 	 * to be the most reliable piece of information available.
6973 	 *
6974 	 * ... expect on devices that forgot to hook HPD up for eDP
6975 	 * (eg. Acer Chromebook C710), so we'll check it only if multiple
6976 	 * ports are attempting to use the same AUX CH, according to VBT.
6977 	 */
6978 	if (intel_bios_dp_has_shared_aux_ch(encoder->devdata)) {
6979 		/*
6980 		 * If this fails, presume the DPCD answer came
6981 		 * from some other port using the same AUX CH.
6982 		 *
6983 		 * FIXME maybe cleaner to check this before the
6984 		 * DPCD read? Would need sort out the VDD handling...
6985 		 */
6986 		if (!intel_digital_port_connected(encoder)) {
6987 			drm_info(display->drm,
6988 				 "[ENCODER:%d:%s] HPD is down, disabling eDP\n",
6989 				 encoder->base.base.id, encoder->base.name);
6990 			goto out_vdd_off;
6991 		}
6992 
6993 		/*
6994 		 * Unfortunately even the HPD based detection fails on
6995 		 * eg. Asus B360M-A (CFL+CNP), so as a last resort fall
6996 		 * back to checking for a VGA branch device. Only do this
6997 		 * on known affected platforms to minimize false positives.
6998 		 */
6999 		if (DISPLAY_VER(display) == 9 && drm_dp_is_branch(intel_dp->dpcd) &&
7000 		    (intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) ==
7001 		    DP_DWN_STRM_PORT_TYPE_ANALOG) {
7002 			drm_info(display->drm,
7003 				 "[ENCODER:%d:%s] VGA converter detected, disabling eDP\n",
7004 				 encoder->base.base.id, encoder->base.name);
7005 			goto out_vdd_off;
7006 		}
7007 	}
7008 
7009 	mutex_lock(&display->drm->mode_config.mutex);
7010 	drm_edid = drm_edid_read_ddc(&connector->base, connector->base.ddc);
7011 	if (!drm_edid) {
7012 		/* Fallback to EDID from ACPI OpRegion, if any */
7013 		drm_edid = intel_opregion_get_edid(connector);
7014 		if (drm_edid)
7015 			drm_dbg_kms(display->drm,
7016 				    "[CONNECTOR:%d:%s] Using OpRegion EDID\n",
7017 				    connector->base.base.id, connector->base.name);
7018 	}
7019 	if (drm_edid) {
7020 		if (drm_edid_connector_update(&connector->base, drm_edid) ||
7021 		    !drm_edid_connector_add_modes(&connector->base)) {
7022 			drm_edid_connector_update(&connector->base, NULL);
7023 			drm_edid_free(drm_edid);
7024 			drm_edid = ERR_PTR(-EINVAL);
7025 		}
7026 	} else {
7027 		drm_edid = ERR_PTR(-ENOENT);
7028 	}
7029 
7030 	intel_bios_init_panel_late(display, &connector->panel, encoder->devdata,
7031 				   IS_ERR(drm_edid) ? NULL : drm_edid);
7032 
7033 	intel_panel_add_edid_fixed_modes(connector, true);
7034 
7035 	/* MSO requires information from the EDID */
7036 	intel_edp_mso_init(intel_dp);
7037 
7038 	/* multiply the mode clock and horizontal timings for MSO */
7039 	list_for_each_entry(fixed_mode, &connector->panel.fixed_modes, head)
7040 		intel_edp_mso_mode_fixup(connector, fixed_mode);
7041 
7042 	/* fallback to VBT if available for eDP */
7043 	if (!intel_panel_preferred_fixed_mode(connector))
7044 		intel_panel_add_vbt_lfp_fixed_mode(connector);
7045 
7046 	mutex_unlock(&display->drm->mode_config.mutex);
7047 
7048 	if (!intel_panel_preferred_fixed_mode(connector)) {
7049 		drm_info(display->drm,
7050 			 "[ENCODER:%d:%s] failed to find fixed mode for the panel, disabling eDP\n",
7051 			 encoder->base.base.id, encoder->base.name);
7052 		goto out_vdd_off;
7053 	}
7054 
7055 	intel_panel_init(connector, drm_edid);
7056 
7057 	intel_edp_backlight_setup(intel_dp, connector);
7058 
7059 	intel_edp_add_properties(intel_dp);
7060 
7061 	intel_pps_init_late(intel_dp);
7062 
7063 	return true;
7064 
7065 out_vdd_off:
7066 	intel_pps_vdd_off_sync(intel_dp);
7067 	intel_bios_fini_panel(&connector->panel);
7068 
7069 	return false;
7070 }
7071 
7072 bool
7073 intel_dp_init_connector(struct intel_digital_port *dig_port,
7074 			struct intel_connector *connector)
7075 {
7076 	struct intel_display *display = to_intel_display(dig_port);
7077 	struct intel_dp *intel_dp = &dig_port->dp;
7078 	struct intel_encoder *encoder = &dig_port->base;
7079 	struct drm_device *dev = encoder->base.dev;
7080 	enum port port = encoder->port;
7081 	int type;
7082 
7083 	if (drm_WARN(dev, dig_port->max_lanes < 1,
7084 		     "Not enough lanes (%d) for DP on [ENCODER:%d:%s]\n",
7085 		     dig_port->max_lanes, encoder->base.base.id,
7086 		     encoder->base.name))
7087 		return false;
7088 
7089 	intel_dp->reset_link_params = true;
7090 
7091 	/* Preserve the current hw state. */
7092 	intel_dp->DP = intel_de_read(display, intel_dp->output_reg);
7093 	intel_dp->attached_connector = connector;
7094 
7095 	if (_intel_dp_is_port_edp(display, encoder->devdata, port)) {
7096 		/*
7097 		 * Currently we don't support eDP on TypeC ports for DISPLAY_VER < 30,
7098 		 * although in theory it could work on TypeC legacy ports.
7099 		 */
7100 		drm_WARN_ON(dev, intel_encoder_is_tc(encoder) &&
7101 			    DISPLAY_VER(display) < 30);
7102 		type = DRM_MODE_CONNECTOR_eDP;
7103 		encoder->type = INTEL_OUTPUT_EDP;
7104 
7105 		/* eDP only on port B and/or C on vlv/chv */
7106 		if (drm_WARN_ON(dev, (display->platform.valleyview ||
7107 				      display->platform.cherryview) &&
7108 				port != PORT_B && port != PORT_C))
7109 			return false;
7110 	} else {
7111 		type = DRM_MODE_CONNECTOR_DisplayPort;
7112 	}
7113 
7114 	intel_dp_set_default_sink_rates(intel_dp);
7115 	intel_dp_set_default_max_sink_lane_count(intel_dp);
7116 
7117 	if (display->platform.valleyview || display->platform.cherryview)
7118 		vlv_pps_pipe_init(intel_dp);
7119 
7120 	intel_dp_aux_init(intel_dp);
7121 	connector->dp.dsc_decompression_aux = &intel_dp->aux;
7122 
7123 	drm_dbg_kms(display->drm,
7124 		    "Adding %s connector on [ENCODER:%d:%s]\n",
7125 		    type == DRM_MODE_CONNECTOR_eDP ? "eDP" : "DP",
7126 		    encoder->base.base.id, encoder->base.name);
7127 
7128 	drm_connector_init_with_ddc(dev, &connector->base, &intel_dp_connector_funcs,
7129 				    type, &intel_dp->aux.ddc);
7130 	drm_connector_helper_add(&connector->base, &intel_dp_connector_helper_funcs);
7131 
7132 	if (!HAS_GMCH(display) && DISPLAY_VER(display) < 12)
7133 		connector->base.interlace_allowed = true;
7134 
7135 	if (type != DRM_MODE_CONNECTOR_eDP)
7136 		connector->polled = DRM_CONNECTOR_POLL_HPD;
7137 	connector->base.polled = connector->polled;
7138 
7139 	intel_connector_attach_encoder(connector, encoder);
7140 
7141 	if (HAS_DDI(display))
7142 		connector->get_hw_state = intel_ddi_connector_get_hw_state;
7143 	else
7144 		connector->get_hw_state = intel_connector_get_hw_state;
7145 	connector->sync_state = intel_dp_connector_sync_state;
7146 
7147 	if (!intel_edp_init_connector(intel_dp, connector)) {
7148 		intel_dp_aux_fini(intel_dp);
7149 		goto fail;
7150 	}
7151 
7152 	intel_dp_set_source_rates(intel_dp);
7153 	intel_dp_set_common_link_params(intel_dp);
7154 	intel_dp_reset_link_params(intel_dp);
7155 
7156 	/* init MST on ports that can support it */
7157 	intel_dp_mst_encoder_init(dig_port, connector->base.base.id);
7158 
7159 	intel_dp_add_properties(intel_dp, &connector->base);
7160 
7161 	if (is_hdcp_supported(display, port) && !intel_dp_is_edp(intel_dp)) {
7162 		int ret = intel_dp_hdcp_init(dig_port, connector);
7163 		if (ret)
7164 			drm_dbg_kms(display->drm,
7165 				    "HDCP init failed, skipping.\n");
7166 	}
7167 
7168 	intel_dp->frl.is_trained = false;
7169 	intel_dp->frl.trained_rate_gbps = 0;
7170 
7171 	intel_psr_init(intel_dp);
7172 
7173 	return true;
7174 
7175 fail:
7176 	intel_display_power_flush_work(display);
7177 	drm_connector_cleanup(&connector->base);
7178 
7179 	return false;
7180 }
7181 
7182 void intel_dp_cleanup_connector(struct intel_digital_port *dig_port,
7183 				struct intel_connector *connector)
7184 {
7185 	struct intel_display *display = to_intel_display(connector);
7186 	struct intel_dp *intel_dp = &dig_port->dp;
7187 
7188 	intel_display_power_flush_work(display);
7189 
7190 	intel_dp_mst_encoder_cleanup(dig_port);
7191 	intel_dp_aux_fini(intel_dp);
7192 	drm_connector_cleanup(&connector->base);
7193 }
7194 
7195 void intel_dp_mst_suspend(struct intel_display *display)
7196 {
7197 	struct intel_encoder *encoder;
7198 
7199 	if (!HAS_DISPLAY(display))
7200 		return;
7201 
7202 	for_each_intel_encoder(display->drm, encoder) {
7203 		struct intel_dp *intel_dp;
7204 
7205 		if (encoder->type != INTEL_OUTPUT_DDI)
7206 			continue;
7207 
7208 		intel_dp = enc_to_intel_dp(encoder);
7209 
7210 		if (!intel_dp_mst_source_support(intel_dp))
7211 			continue;
7212 
7213 		if (intel_dp->is_mst)
7214 			drm_dp_mst_topology_mgr_suspend(&intel_dp->mst.mgr);
7215 	}
7216 }
7217 
7218 void intel_dp_mst_resume(struct intel_display *display)
7219 {
7220 	struct intel_encoder *encoder;
7221 
7222 	if (!HAS_DISPLAY(display))
7223 		return;
7224 
7225 	for_each_intel_encoder(display->drm, encoder) {
7226 		struct intel_dp *intel_dp;
7227 		int ret;
7228 
7229 		if (encoder->type != INTEL_OUTPUT_DDI)
7230 			continue;
7231 
7232 		intel_dp = enc_to_intel_dp(encoder);
7233 
7234 		if (!intel_dp_mst_source_support(intel_dp))
7235 			continue;
7236 
7237 		ret = drm_dp_mst_topology_mgr_resume(&intel_dp->mst.mgr, true);
7238 		if (ret) {
7239 			intel_dp->is_mst = false;
7240 			drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst.mgr, false);
7241 		}
7242 	}
7243 }
7244 
7245 static
7246 int intel_dp_sdp_compute_config_late(struct intel_crtc_state *crtc_state)
7247 {
7248 	struct intel_display *display = to_intel_display(crtc_state);
7249 	int guardband = intel_crtc_vblank_length(crtc_state);
7250 	int min_sdp_guardband = intel_dp_sdp_min_guardband(crtc_state, false);
7251 
7252 	if (guardband < min_sdp_guardband) {
7253 		drm_dbg_kms(display->drm, "guardband %d < min sdp guardband %d\n",
7254 			    guardband, min_sdp_guardband);
7255 		return -EINVAL;
7256 	}
7257 
7258 	return 0;
7259 }
7260 
7261 int intel_dp_compute_config_late(struct intel_encoder *encoder,
7262 				 struct intel_crtc_state *crtc_state,
7263 				 struct drm_connector_state *conn_state)
7264 {
7265 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
7266 	int ret;
7267 
7268 	intel_psr_compute_config_late(intel_dp, crtc_state);
7269 
7270 	ret = intel_dp_sdp_compute_config_late(crtc_state);
7271 	if (ret)
7272 		return ret;
7273 
7274 	intel_alpm_lobf_compute_config_late(intel_dp, crtc_state);
7275 
7276 	return 0;
7277 }
7278 
7279 static
7280 int intel_dp_get_lines_for_sdp(const struct intel_crtc_state *crtc_state, u32 type)
7281 {
7282 	switch (type) {
7283 	case DP_SDP_VSC_EXT_VESA:
7284 	case DP_SDP_VSC_EXT_CEA:
7285 		return 10;
7286 	case HDMI_PACKET_TYPE_GAMUT_METADATA:
7287 		return 8;
7288 	case DP_SDP_PPS:
7289 		return 7;
7290 	case DP_SDP_ADAPTIVE_SYNC:
7291 		return crtc_state->vrr.vsync_start + 1;
7292 	default:
7293 		break;
7294 	}
7295 
7296 	return 0;
7297 }
7298 
7299 int intel_dp_sdp_min_guardband(const struct intel_crtc_state *crtc_state,
7300 			       bool assume_all_enabled)
7301 {
7302 	int sdp_guardband = 0;
7303 
7304 	if (assume_all_enabled ||
7305 	    crtc_state->infoframes.enable &
7306 	    intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GAMUT_METADATA))
7307 		sdp_guardband = max(sdp_guardband,
7308 				    intel_dp_get_lines_for_sdp(crtc_state,
7309 							       HDMI_PACKET_TYPE_GAMUT_METADATA));
7310 
7311 	if (assume_all_enabled ||
7312 	    crtc_state->dsc.compression_enable)
7313 		sdp_guardband = max(sdp_guardband,
7314 				    intel_dp_get_lines_for_sdp(crtc_state, DP_SDP_PPS));
7315 
7316 	if (crtc_state->infoframes.enable &
7317 	    intel_hdmi_infoframe_enable(DP_SDP_ADAPTIVE_SYNC))
7318 		sdp_guardband = max(sdp_guardband,
7319 				    intel_dp_get_lines_for_sdp(crtc_state, DP_SDP_ADAPTIVE_SYNC));
7320 
7321 	return sdp_guardband;
7322 }
7323 
7324 bool intel_dp_joiner_candidate_valid(struct intel_connector *connector,
7325 				     int hdisplay,
7326 				     int num_joined_pipes)
7327 {
7328 	struct intel_display *display = to_intel_display(connector);
7329 	struct intel_dp *intel_dp = intel_attached_dp(connector);
7330 
7331 	if (!intel_dp_can_join(intel_dp, num_joined_pipes))
7332 		return false;
7333 
7334 	if (hdisplay > num_joined_pipes * intel_dp_max_hdisplay_per_pipe(display))
7335 		return false;
7336 
7337 	if (connector->force_joined_pipes && connector->force_joined_pipes != num_joined_pipes)
7338 		return false;
7339 
7340 	return true;
7341 }
7342 
7343 u8 intel_dp_as_sdp_transmission_time(void)
7344 {
7345 	/*
7346 	 * DP allows AS SDP position to move during PR active in some cases, but
7347 	 * software-controlled refresh rate changes with DC6v / ALPM require the
7348 	 * AS SDP to remain at T1. Use T1 unconditionally for now.
7349 	 */
7350 
7351 	return DP_PR_AS_SDP_SETUP_TIME_T1;
7352 }
7353 
7354 int intel_dp_link_init(struct intel_dp *intel_dp)
7355 {
7356 	intel_dp->link.training = intel_dp_link_training_init(intel_dp);
7357 	if (!intel_dp->link.training)
7358 		return -ENOMEM;
7359 
7360 	intel_dp->link.caps = intel_dp_link_caps_init(intel_dp);
7361 	if (!intel_dp->link.caps) {
7362 		intel_dp_link_training_cleanup(intel_dp->link.training);
7363 
7364 		return -ENOMEM;
7365 	}
7366 
7367 	return 0;
7368 }
7369 
7370 void intel_dp_link_cleanup(struct intel_dp *intel_dp)
7371 {
7372 	intel_dp_link_caps_cleanup(intel_dp->link.caps);
7373 	intel_dp_link_training_cleanup(intel_dp->link.training);
7374 }
7375