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