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