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