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