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