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