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