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