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