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