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