xref: /openbsd-src/sys/dev/pci/drm/i915/display/intel_dp.c (revision 4e1ee0786f11cc571bd0be17d38e46f635c719fc)
1 /*
2  * Copyright © 2008 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Keith Packard <keithp@keithp.com>
25  *
26  */
27 
28 #include <linux/export.h>
29 #include <linux/i2c.h>
30 #include <linux/notifier.h>
31 #include <linux/reboot.h>
32 #include <linux/slab.h>
33 #include <linux/types.h>
34 
35 #include <asm/byteorder.h>
36 
37 #include <drm/drm_atomic_helper.h>
38 #include <drm/drm_crtc.h>
39 #include <drm/drm_dp_helper.h>
40 #include <drm/drm_edid.h>
41 #include <drm/drm_probe_helper.h>
42 
43 #include "i915_debugfs.h"
44 #include "i915_drv.h"
45 #include "i915_trace.h"
46 #include "intel_atomic.h"
47 #include "intel_audio.h"
48 #include "intel_connector.h"
49 #include "intel_ddi.h"
50 #include "intel_display_types.h"
51 #include "intel_dp.h"
52 #include "intel_dp_link_training.h"
53 #include "intel_dp_mst.h"
54 #include "intel_dpio_phy.h"
55 #include "intel_fifo_underrun.h"
56 #include "intel_hdcp.h"
57 #include "intel_hdmi.h"
58 #include "intel_hotplug.h"
59 #include "intel_lspcon.h"
60 #include "intel_lvds.h"
61 #include "intel_panel.h"
62 #include "intel_psr.h"
63 #include "intel_sideband.h"
64 #include "intel_tc.h"
65 #include "intel_vdsc.h"
66 
67 #define DP_DPRX_ESI_LEN 14
68 
69 /* DP DSC throughput values used for slice count calculations KPixels/s */
70 #define DP_DSC_PEAK_PIXEL_RATE			2720000
71 #define DP_DSC_MAX_ENC_THROUGHPUT_0		340000
72 #define DP_DSC_MAX_ENC_THROUGHPUT_1		400000
73 
74 /* DP DSC FEC Overhead factor = 1/(0.972261) */
75 #define DP_DSC_FEC_OVERHEAD_FACTOR		972261
76 
77 /* Compliance test status bits  */
78 #define INTEL_DP_RESOLUTION_SHIFT_MASK	0
79 #define INTEL_DP_RESOLUTION_PREFERRED	(1 << INTEL_DP_RESOLUTION_SHIFT_MASK)
80 #define INTEL_DP_RESOLUTION_STANDARD	(2 << INTEL_DP_RESOLUTION_SHIFT_MASK)
81 #define INTEL_DP_RESOLUTION_FAILSAFE	(3 << INTEL_DP_RESOLUTION_SHIFT_MASK)
82 
83 struct dp_link_dpll {
84 	int clock;
85 	struct dpll dpll;
86 };
87 
88 static const struct dp_link_dpll g4x_dpll[] = {
89 	{ 162000,
90 		{ .p1 = 2, .p2 = 10, .n = 2, .m1 = 23, .m2 = 8 } },
91 	{ 270000,
92 		{ .p1 = 1, .p2 = 10, .n = 1, .m1 = 14, .m2 = 2 } }
93 };
94 
95 static const struct dp_link_dpll pch_dpll[] = {
96 	{ 162000,
97 		{ .p1 = 2, .p2 = 10, .n = 1, .m1 = 12, .m2 = 9 } },
98 	{ 270000,
99 		{ .p1 = 1, .p2 = 10, .n = 2, .m1 = 14, .m2 = 8 } }
100 };
101 
102 static const struct dp_link_dpll vlv_dpll[] = {
103 	{ 162000,
104 		{ .p1 = 3, .p2 = 2, .n = 5, .m1 = 3, .m2 = 81 } },
105 	{ 270000,
106 		{ .p1 = 2, .p2 = 2, .n = 1, .m1 = 2, .m2 = 27 } }
107 };
108 
109 /*
110  * CHV supports eDP 1.4 that have  more link rates.
111  * Below only provides the fixed rate but exclude variable rate.
112  */
113 static const struct dp_link_dpll chv_dpll[] = {
114 	/*
115 	 * CHV requires to program fractional division for m2.
116 	 * m2 is stored in fixed point format using formula below
117 	 * (m2_int << 22) | m2_fraction
118 	 */
119 	{ 162000,	/* m2_int = 32, m2_fraction = 1677722 */
120 		{ .p1 = 4, .p2 = 2, .n = 1, .m1 = 2, .m2 = 0x819999a } },
121 	{ 270000,	/* m2_int = 27, m2_fraction = 0 */
122 		{ .p1 = 4, .p2 = 1, .n = 1, .m1 = 2, .m2 = 0x6c00000 } },
123 };
124 
125 /* Constants for DP DSC configurations */
126 static const u8 valid_dsc_bpp[] = {6, 8, 10, 12, 15};
127 
128 /* With Single pipe configuration, HW is capable of supporting maximum
129  * of 4 slices per line.
130  */
131 static const u8 valid_dsc_slicecount[] = {1, 2, 4};
132 
133 /**
134  * intel_dp_is_edp - is the given port attached to an eDP panel (either CPU or PCH)
135  * @intel_dp: DP struct
136  *
137  * If a CPU or PCH DP output is attached to an eDP panel, this function
138  * will return true, and false otherwise.
139  */
140 bool intel_dp_is_edp(struct intel_dp *intel_dp)
141 {
142 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
143 
144 	return dig_port->base.type == INTEL_OUTPUT_EDP;
145 }
146 
147 static void intel_dp_link_down(struct intel_encoder *encoder,
148 			       const struct intel_crtc_state *old_crtc_state);
149 static bool edp_panel_vdd_on(struct intel_dp *intel_dp);
150 static void edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync);
151 static void vlv_init_panel_power_sequencer(struct intel_encoder *encoder,
152 					   const struct intel_crtc_state *crtc_state);
153 static void vlv_steal_power_sequencer(struct drm_i915_private *dev_priv,
154 				      enum pipe pipe);
155 static void intel_dp_unset_edid(struct intel_dp *intel_dp);
156 
157 /* update sink rates from dpcd */
158 static void intel_dp_set_sink_rates(struct intel_dp *intel_dp)
159 {
160 	static const int dp_rates[] = {
161 		162000, 270000, 540000, 810000
162 	};
163 	int i, max_rate;
164 
165 	if (drm_dp_has_quirk(&intel_dp->desc, 0,
166 			     DP_DPCD_QUIRK_CAN_DO_MAX_LINK_RATE_3_24_GBPS)) {
167 		/* Needed, e.g., for Apple MBP 2017, 15 inch eDP Retina panel */
168 		static const int quirk_rates[] = { 162000, 270000, 324000 };
169 
170 		memcpy(intel_dp->sink_rates, quirk_rates, sizeof(quirk_rates));
171 		intel_dp->num_sink_rates = ARRAY_SIZE(quirk_rates);
172 
173 		return;
174 	}
175 
176 	max_rate = drm_dp_bw_code_to_link_rate(intel_dp->dpcd[DP_MAX_LINK_RATE]);
177 
178 	for (i = 0; i < ARRAY_SIZE(dp_rates); i++) {
179 		if (dp_rates[i] > max_rate)
180 			break;
181 		intel_dp->sink_rates[i] = dp_rates[i];
182 	}
183 
184 	intel_dp->num_sink_rates = i;
185 }
186 
187 /* Get length of rates array potentially limited by max_rate. */
188 static int intel_dp_rate_limit_len(const int *rates, int len, int max_rate)
189 {
190 	int i;
191 
192 	/* Limit results by potentially reduced max rate */
193 	for (i = 0; i < len; i++) {
194 		if (rates[len - i - 1] <= max_rate)
195 			return len - i;
196 	}
197 
198 	return 0;
199 }
200 
201 /* Get length of common rates array potentially limited by max_rate. */
202 static int intel_dp_common_len_rate_limit(const struct intel_dp *intel_dp,
203 					  int max_rate)
204 {
205 	return intel_dp_rate_limit_len(intel_dp->common_rates,
206 				       intel_dp->num_common_rates, max_rate);
207 }
208 
209 /* Theoretical max between source and sink */
210 static int intel_dp_max_common_rate(struct intel_dp *intel_dp)
211 {
212 	return intel_dp->common_rates[intel_dp->num_common_rates - 1];
213 }
214 
215 /* Theoretical max between source and sink */
216 static int intel_dp_max_common_lane_count(struct intel_dp *intel_dp)
217 {
218 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
219 	int source_max = dig_port->max_lanes;
220 	int sink_max = drm_dp_max_lane_count(intel_dp->dpcd);
221 	int fia_max = intel_tc_port_fia_max_lane_count(dig_port);
222 
223 	return min3(source_max, sink_max, fia_max);
224 }
225 
226 int intel_dp_max_lane_count(struct intel_dp *intel_dp)
227 {
228 	return intel_dp->max_link_lane_count;
229 }
230 
231 int
232 intel_dp_link_required(int pixel_clock, int bpp)
233 {
234 	/* pixel_clock is in kHz, divide bpp by 8 for bit to Byte conversion */
235 	return DIV_ROUND_UP(pixel_clock * bpp, 8);
236 }
237 
238 int
239 intel_dp_max_data_rate(int max_link_clock, int max_lanes)
240 {
241 	/* max_link_clock is the link symbol clock (LS_Clk) in kHz and not the
242 	 * link rate that is generally expressed in Gbps. Since, 8 bits of data
243 	 * is transmitted every LS_Clk per lane, there is no need to account for
244 	 * the channel encoding that is done in the PHY layer here.
245 	 */
246 
247 	return max_link_clock * max_lanes;
248 }
249 
250 static int cnl_max_source_rate(struct intel_dp *intel_dp)
251 {
252 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
253 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
254 	enum port port = dig_port->base.port;
255 
256 	u32 voltage = intel_de_read(dev_priv, CNL_PORT_COMP_DW3) & VOLTAGE_INFO_MASK;
257 
258 	/* Low voltage SKUs are limited to max of 5.4G */
259 	if (voltage == VOLTAGE_INFO_0_85V)
260 		return 540000;
261 
262 	/* For this SKU 8.1G is supported in all ports */
263 	if (IS_CNL_WITH_PORT_F(dev_priv))
264 		return 810000;
265 
266 	/* For other SKUs, max rate on ports A and D is 5.4G */
267 	if (port == PORT_A || port == PORT_D)
268 		return 540000;
269 
270 	return 810000;
271 }
272 
273 static int icl_max_source_rate(struct intel_dp *intel_dp)
274 {
275 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
276 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
277 	enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
278 
279 	if (intel_phy_is_combo(dev_priv, phy) &&
280 	    !IS_ELKHARTLAKE(dev_priv) &&
281 	    !intel_dp_is_edp(intel_dp))
282 		return 540000;
283 
284 	return 810000;
285 }
286 
287 static void
288 intel_dp_set_source_rates(struct intel_dp *intel_dp)
289 {
290 	/* The values must be in increasing order */
291 	static const int cnl_rates[] = {
292 		162000, 216000, 270000, 324000, 432000, 540000, 648000, 810000
293 	};
294 	static const int bxt_rates[] = {
295 		162000, 216000, 243000, 270000, 324000, 432000, 540000
296 	};
297 	static const int skl_rates[] = {
298 		162000, 216000, 270000, 324000, 432000, 540000
299 	};
300 	static const int hsw_rates[] = {
301 		162000, 270000, 540000
302 	};
303 	static const int g4x_rates[] = {
304 		162000, 270000
305 	};
306 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
307 	struct intel_encoder *encoder = &dig_port->base;
308 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
309 	const int *source_rates;
310 	int size, max_rate = 0, vbt_max_rate;
311 
312 	/* This should only be done once */
313 	drm_WARN_ON(&dev_priv->drm,
314 		    intel_dp->source_rates || intel_dp->num_source_rates);
315 
316 	if (INTEL_GEN(dev_priv) >= 10) {
317 		source_rates = cnl_rates;
318 		size = ARRAY_SIZE(cnl_rates);
319 		if (IS_GEN(dev_priv, 10))
320 			max_rate = cnl_max_source_rate(intel_dp);
321 		else
322 			max_rate = icl_max_source_rate(intel_dp);
323 	} else if (IS_GEN9_LP(dev_priv)) {
324 		source_rates = bxt_rates;
325 		size = ARRAY_SIZE(bxt_rates);
326 	} else if (IS_GEN9_BC(dev_priv)) {
327 		source_rates = skl_rates;
328 		size = ARRAY_SIZE(skl_rates);
329 	} else if ((IS_HASWELL(dev_priv) && !IS_HSW_ULX(dev_priv)) ||
330 		   IS_BROADWELL(dev_priv)) {
331 		source_rates = hsw_rates;
332 		size = ARRAY_SIZE(hsw_rates);
333 	} else {
334 		source_rates = g4x_rates;
335 		size = ARRAY_SIZE(g4x_rates);
336 	}
337 
338 	vbt_max_rate = intel_bios_dp_max_link_rate(encoder);
339 	if (max_rate && vbt_max_rate)
340 		max_rate = min(max_rate, vbt_max_rate);
341 	else if (vbt_max_rate)
342 		max_rate = vbt_max_rate;
343 
344 	if (max_rate)
345 		size = intel_dp_rate_limit_len(source_rates, size, max_rate);
346 
347 	intel_dp->source_rates = source_rates;
348 	intel_dp->num_source_rates = size;
349 }
350 
351 static int intersect_rates(const int *source_rates, int source_len,
352 			   const int *sink_rates, int sink_len,
353 			   int *common_rates)
354 {
355 	int i = 0, j = 0, k = 0;
356 
357 	while (i < source_len && j < sink_len) {
358 		if (source_rates[i] == sink_rates[j]) {
359 			if (WARN_ON(k >= DP_MAX_SUPPORTED_RATES))
360 				return k;
361 			common_rates[k] = source_rates[i];
362 			++k;
363 			++i;
364 			++j;
365 		} else if (source_rates[i] < sink_rates[j]) {
366 			++i;
367 		} else {
368 			++j;
369 		}
370 	}
371 	return k;
372 }
373 
374 /* return index of rate in rates array, or -1 if not found */
375 static int intel_dp_rate_index(const int *rates, int len, int rate)
376 {
377 	int i;
378 
379 	for (i = 0; i < len; i++)
380 		if (rate == rates[i])
381 			return i;
382 
383 	return -1;
384 }
385 
386 static void intel_dp_set_common_rates(struct intel_dp *intel_dp)
387 {
388 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
389 
390 	drm_WARN_ON(&i915->drm,
391 		    !intel_dp->num_source_rates || !intel_dp->num_sink_rates);
392 
393 	intel_dp->num_common_rates = intersect_rates(intel_dp->source_rates,
394 						     intel_dp->num_source_rates,
395 						     intel_dp->sink_rates,
396 						     intel_dp->num_sink_rates,
397 						     intel_dp->common_rates);
398 
399 	/* Paranoia, there should always be something in common. */
400 	if (drm_WARN_ON(&i915->drm, intel_dp->num_common_rates == 0)) {
401 		intel_dp->common_rates[0] = 162000;
402 		intel_dp->num_common_rates = 1;
403 	}
404 }
405 
406 static bool intel_dp_link_params_valid(struct intel_dp *intel_dp, int link_rate,
407 				       u8 lane_count)
408 {
409 	/*
410 	 * FIXME: we need to synchronize the current link parameters with
411 	 * hardware readout. Currently fast link training doesn't work on
412 	 * boot-up.
413 	 */
414 	if (link_rate == 0 ||
415 	    link_rate > intel_dp->max_link_rate)
416 		return false;
417 
418 	if (lane_count == 0 ||
419 	    lane_count > intel_dp_max_lane_count(intel_dp))
420 		return false;
421 
422 	return true;
423 }
424 
425 static bool intel_dp_can_link_train_fallback_for_edp(struct intel_dp *intel_dp,
426 						     int link_rate,
427 						     u8 lane_count)
428 {
429 	const struct drm_display_mode *fixed_mode =
430 		intel_dp->attached_connector->panel.fixed_mode;
431 	int mode_rate, max_rate;
432 
433 	mode_rate = intel_dp_link_required(fixed_mode->clock, 18);
434 	max_rate = intel_dp_max_data_rate(link_rate, lane_count);
435 	if (mode_rate > max_rate)
436 		return false;
437 
438 	return true;
439 }
440 
441 int intel_dp_get_link_train_fallback_values(struct intel_dp *intel_dp,
442 					    int link_rate, u8 lane_count)
443 {
444 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
445 	int index;
446 
447 	/*
448 	 * TODO: Enable fallback on MST links once MST link compute can handle
449 	 * the fallback params.
450 	 */
451 	if (intel_dp->is_mst) {
452 		drm_err(&i915->drm, "Link Training Unsuccessful\n");
453 		return -1;
454 	}
455 
456 	index = intel_dp_rate_index(intel_dp->common_rates,
457 				    intel_dp->num_common_rates,
458 				    link_rate);
459 	if (index > 0) {
460 		if (intel_dp_is_edp(intel_dp) &&
461 		    !intel_dp_can_link_train_fallback_for_edp(intel_dp,
462 							      intel_dp->common_rates[index - 1],
463 							      lane_count)) {
464 			drm_dbg_kms(&i915->drm,
465 				    "Retrying Link training for eDP with same parameters\n");
466 			return 0;
467 		}
468 		intel_dp->max_link_rate = intel_dp->common_rates[index - 1];
469 		intel_dp->max_link_lane_count = lane_count;
470 	} else if (lane_count > 1) {
471 		if (intel_dp_is_edp(intel_dp) &&
472 		    !intel_dp_can_link_train_fallback_for_edp(intel_dp,
473 							      intel_dp_max_common_rate(intel_dp),
474 							      lane_count >> 1)) {
475 			drm_dbg_kms(&i915->drm,
476 				    "Retrying Link training for eDP with same parameters\n");
477 			return 0;
478 		}
479 		intel_dp->max_link_rate = intel_dp_max_common_rate(intel_dp);
480 		intel_dp->max_link_lane_count = lane_count >> 1;
481 	} else {
482 		drm_err(&i915->drm, "Link Training Unsuccessful\n");
483 		return -1;
484 	}
485 
486 	return 0;
487 }
488 
489 u32 intel_dp_mode_to_fec_clock(u32 mode_clock)
490 {
491 	return div_u64(mul_u32_u32(mode_clock, 1000000U),
492 		       DP_DSC_FEC_OVERHEAD_FACTOR);
493 }
494 
495 static int
496 small_joiner_ram_size_bits(struct drm_i915_private *i915)
497 {
498 	if (INTEL_GEN(i915) >= 11)
499 		return 7680 * 8;
500 	else
501 		return 6144 * 8;
502 }
503 
504 static u16 intel_dp_dsc_get_output_bpp(struct drm_i915_private *i915,
505 				       u32 link_clock, u32 lane_count,
506 				       u32 mode_clock, u32 mode_hdisplay)
507 {
508 	u32 bits_per_pixel, max_bpp_small_joiner_ram;
509 	int i;
510 
511 	/*
512 	 * Available Link Bandwidth(Kbits/sec) = (NumberOfLanes)*
513 	 * (LinkSymbolClock)* 8 * (TimeSlotsPerMTP)
514 	 * for SST -> TimeSlotsPerMTP is 1,
515 	 * for MST -> TimeSlotsPerMTP has to be calculated
516 	 */
517 	bits_per_pixel = (link_clock * lane_count * 8) /
518 			 intel_dp_mode_to_fec_clock(mode_clock);
519 	drm_dbg_kms(&i915->drm, "Max link bpp: %u\n", bits_per_pixel);
520 
521 	/* Small Joiner Check: output bpp <= joiner RAM (bits) / Horiz. width */
522 	max_bpp_small_joiner_ram = small_joiner_ram_size_bits(i915) /
523 		mode_hdisplay;
524 	drm_dbg_kms(&i915->drm, "Max small joiner bpp: %u\n",
525 		    max_bpp_small_joiner_ram);
526 
527 	/*
528 	 * Greatest allowed DSC BPP = MIN (output BPP from available Link BW
529 	 * check, output bpp from small joiner RAM check)
530 	 */
531 	bits_per_pixel = min(bits_per_pixel, max_bpp_small_joiner_ram);
532 
533 	/* Error out if the max bpp is less than smallest allowed valid bpp */
534 	if (bits_per_pixel < valid_dsc_bpp[0]) {
535 		drm_dbg_kms(&i915->drm, "Unsupported BPP %u, min %u\n",
536 			    bits_per_pixel, valid_dsc_bpp[0]);
537 		return 0;
538 	}
539 
540 	/* Find the nearest match in the array of known BPPs from VESA */
541 	for (i = 0; i < ARRAY_SIZE(valid_dsc_bpp) - 1; i++) {
542 		if (bits_per_pixel < valid_dsc_bpp[i + 1])
543 			break;
544 	}
545 	bits_per_pixel = valid_dsc_bpp[i];
546 
547 	/*
548 	 * Compressed BPP in U6.4 format so multiply by 16, for Gen 11,
549 	 * fractional part is 0
550 	 */
551 	return bits_per_pixel << 4;
552 }
553 
554 static u8 intel_dp_dsc_get_slice_count(struct intel_dp *intel_dp,
555 				       int mode_clock, int mode_hdisplay)
556 {
557 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
558 	u8 min_slice_count, i;
559 	int max_slice_width;
560 
561 	if (mode_clock <= DP_DSC_PEAK_PIXEL_RATE)
562 		min_slice_count = DIV_ROUND_UP(mode_clock,
563 					       DP_DSC_MAX_ENC_THROUGHPUT_0);
564 	else
565 		min_slice_count = DIV_ROUND_UP(mode_clock,
566 					       DP_DSC_MAX_ENC_THROUGHPUT_1);
567 
568 	max_slice_width = drm_dp_dsc_sink_max_slice_width(intel_dp->dsc_dpcd);
569 	if (max_slice_width < DP_DSC_MIN_SLICE_WIDTH_VALUE) {
570 		drm_dbg_kms(&i915->drm,
571 			    "Unsupported slice width %d by DP DSC Sink device\n",
572 			    max_slice_width);
573 		return 0;
574 	}
575 	/* Also take into account max slice width */
576 	min_slice_count = max_t(u8, min_slice_count,
577 				DIV_ROUND_UP(mode_hdisplay,
578 					     max_slice_width));
579 
580 	/* Find the closest match to the valid slice count values */
581 	for (i = 0; i < ARRAY_SIZE(valid_dsc_slicecount); i++) {
582 		if (valid_dsc_slicecount[i] >
583 		    drm_dp_dsc_sink_max_slice_count(intel_dp->dsc_dpcd,
584 						    false))
585 			break;
586 		if (min_slice_count  <= valid_dsc_slicecount[i])
587 			return valid_dsc_slicecount[i];
588 	}
589 
590 	drm_dbg_kms(&i915->drm, "Unsupported Slice Count %d\n",
591 		    min_slice_count);
592 	return 0;
593 }
594 
595 static bool intel_dp_hdisplay_bad(struct drm_i915_private *dev_priv,
596 				  int hdisplay)
597 {
598 	/*
599 	 * Older platforms don't like hdisplay==4096 with DP.
600 	 *
601 	 * On ILK/SNB/IVB the pipe seems to be somewhat running (scanline
602 	 * and frame counter increment), but we don't get vblank interrupts,
603 	 * and the pipe underruns immediately. The link also doesn't seem
604 	 * to get trained properly.
605 	 *
606 	 * On CHV the vblank interrupts don't seem to disappear but
607 	 * otherwise the symptoms are similar.
608 	 *
609 	 * TODO: confirm the behaviour on HSW+
610 	 */
611 	return hdisplay == 4096 && !HAS_DDI(dev_priv);
612 }
613 
614 static enum drm_mode_status
615 intel_dp_mode_valid_downstream(struct intel_connector *connector,
616 			       const struct drm_display_mode *mode,
617 			       int target_clock)
618 {
619 	struct intel_dp *intel_dp = intel_attached_dp(connector);
620 	const struct drm_display_info *info = &connector->base.display_info;
621 	int tmds_clock;
622 
623 	if (intel_dp->dfp.max_dotclock &&
624 	    target_clock > intel_dp->dfp.max_dotclock)
625 		return MODE_CLOCK_HIGH;
626 
627 	/* Assume 8bpc for the DP++/HDMI/DVI TMDS clock check */
628 	tmds_clock = target_clock;
629 	if (drm_mode_is_420_only(info, mode))
630 		tmds_clock /= 2;
631 
632 	if (intel_dp->dfp.min_tmds_clock &&
633 	    tmds_clock < intel_dp->dfp.min_tmds_clock)
634 		return MODE_CLOCK_LOW;
635 	if (intel_dp->dfp.max_tmds_clock &&
636 	    tmds_clock > intel_dp->dfp.max_tmds_clock)
637 		return MODE_CLOCK_HIGH;
638 
639 	return MODE_OK;
640 }
641 
642 static enum drm_mode_status
643 intel_dp_mode_valid(struct drm_connector *connector,
644 		    struct drm_display_mode *mode)
645 {
646 	struct intel_dp *intel_dp = intel_attached_dp(to_intel_connector(connector));
647 	struct intel_connector *intel_connector = to_intel_connector(connector);
648 	struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
649 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
650 	int target_clock = mode->clock;
651 	int max_rate, mode_rate, max_lanes, max_link_clock;
652 	int max_dotclk = dev_priv->max_dotclk_freq;
653 	u16 dsc_max_output_bpp = 0;
654 	u8 dsc_slice_count = 0;
655 	enum drm_mode_status status;
656 
657 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
658 		return MODE_NO_DBLESCAN;
659 
660 	if (intel_dp_is_edp(intel_dp) && fixed_mode) {
661 		if (mode->hdisplay > fixed_mode->hdisplay)
662 			return MODE_PANEL;
663 
664 		if (mode->vdisplay > fixed_mode->vdisplay)
665 			return MODE_PANEL;
666 
667 		target_clock = fixed_mode->clock;
668 	}
669 
670 	max_link_clock = intel_dp_max_link_rate(intel_dp);
671 	max_lanes = intel_dp_max_lane_count(intel_dp);
672 
673 	max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
674 	mode_rate = intel_dp_link_required(target_clock, 18);
675 
676 	if (intel_dp_hdisplay_bad(dev_priv, mode->hdisplay))
677 		return MODE_H_ILLEGAL;
678 
679 	/*
680 	 * Output bpp is stored in 6.4 format so right shift by 4 to get the
681 	 * integer value since we support only integer values of bpp.
682 	 */
683 	if ((INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) &&
684 	    drm_dp_sink_supports_dsc(intel_dp->dsc_dpcd)) {
685 		if (intel_dp_is_edp(intel_dp)) {
686 			dsc_max_output_bpp =
687 				drm_edp_dsc_sink_output_bpp(intel_dp->dsc_dpcd) >> 4;
688 			dsc_slice_count =
689 				drm_dp_dsc_sink_max_slice_count(intel_dp->dsc_dpcd,
690 								true);
691 		} else if (drm_dp_sink_supports_fec(intel_dp->fec_capable)) {
692 			dsc_max_output_bpp =
693 				intel_dp_dsc_get_output_bpp(dev_priv,
694 							    max_link_clock,
695 							    max_lanes,
696 							    target_clock,
697 							    mode->hdisplay) >> 4;
698 			dsc_slice_count =
699 				intel_dp_dsc_get_slice_count(intel_dp,
700 							     target_clock,
701 							     mode->hdisplay);
702 		}
703 	}
704 
705 	if ((mode_rate > max_rate && !(dsc_max_output_bpp && dsc_slice_count)) ||
706 	    target_clock > max_dotclk)
707 		return MODE_CLOCK_HIGH;
708 
709 	if (mode->clock < 10000)
710 		return MODE_CLOCK_LOW;
711 
712 	if (mode->flags & DRM_MODE_FLAG_DBLCLK)
713 		return MODE_H_ILLEGAL;
714 
715 	status = intel_dp_mode_valid_downstream(intel_connector,
716 						mode, target_clock);
717 	if (status != MODE_OK)
718 		return status;
719 
720 	return intel_mode_valid_max_plane_size(dev_priv, mode);
721 }
722 
723 u32 intel_dp_pack_aux(const u8 *src, int src_bytes)
724 {
725 	int i;
726 	u32 v = 0;
727 
728 	if (src_bytes > 4)
729 		src_bytes = 4;
730 	for (i = 0; i < src_bytes; i++)
731 		v |= ((u32)src[i]) << ((3 - i) * 8);
732 	return v;
733 }
734 
735 static void intel_dp_unpack_aux(u32 src, u8 *dst, int dst_bytes)
736 {
737 	int i;
738 	if (dst_bytes > 4)
739 		dst_bytes = 4;
740 	for (i = 0; i < dst_bytes; i++)
741 		dst[i] = src >> ((3-i) * 8);
742 }
743 
744 static void
745 intel_dp_init_panel_power_sequencer(struct intel_dp *intel_dp);
746 static void
747 intel_dp_init_panel_power_sequencer_registers(struct intel_dp *intel_dp,
748 					      bool force_disable_vdd);
749 static void
750 intel_dp_pps_init(struct intel_dp *intel_dp);
751 
752 static intel_wakeref_t
753 pps_lock(struct intel_dp *intel_dp)
754 {
755 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
756 	intel_wakeref_t wakeref;
757 
758 	/*
759 	 * See intel_power_sequencer_reset() why we need
760 	 * a power domain reference here.
761 	 */
762 	wakeref = intel_display_power_get(dev_priv,
763 					  intel_aux_power_domain(dp_to_dig_port(intel_dp)));
764 
765 	mutex_lock(&dev_priv->pps_mutex);
766 
767 	return wakeref;
768 }
769 
770 static intel_wakeref_t
771 pps_unlock(struct intel_dp *intel_dp, intel_wakeref_t wakeref)
772 {
773 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
774 
775 	mutex_unlock(&dev_priv->pps_mutex);
776 	intel_display_power_put(dev_priv,
777 				intel_aux_power_domain(dp_to_dig_port(intel_dp)),
778 				wakeref);
779 	return 0;
780 }
781 
782 #define with_pps_lock(dp, wf) \
783 	for ((wf) = pps_lock(dp); (wf); (wf) = pps_unlock((dp), (wf)))
784 
785 static void
786 vlv_power_sequencer_kick(struct intel_dp *intel_dp)
787 {
788 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
789 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
790 	enum pipe pipe = intel_dp->pps_pipe;
791 	bool pll_enabled, release_cl_override = false;
792 	enum dpio_phy phy = DPIO_PHY(pipe);
793 	enum dpio_channel ch = vlv_pipe_to_channel(pipe);
794 	u32 DP;
795 
796 	if (drm_WARN(&dev_priv->drm,
797 		     intel_de_read(dev_priv, intel_dp->output_reg) & DP_PORT_EN,
798 		     "skipping pipe %c power sequencer kick due to [ENCODER:%d:%s] being active\n",
799 		     pipe_name(pipe), dig_port->base.base.base.id,
800 		     dig_port->base.base.name))
801 		return;
802 
803 	drm_dbg_kms(&dev_priv->drm,
804 		    "kicking pipe %c power sequencer for [ENCODER:%d:%s]\n",
805 		    pipe_name(pipe), dig_port->base.base.base.id,
806 		    dig_port->base.base.name);
807 
808 	/* Preserve the BIOS-computed detected bit. This is
809 	 * supposed to be read-only.
810 	 */
811 	DP = intel_de_read(dev_priv, intel_dp->output_reg) & DP_DETECTED;
812 	DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
813 	DP |= DP_PORT_WIDTH(1);
814 	DP |= DP_LINK_TRAIN_PAT_1;
815 
816 	if (IS_CHERRYVIEW(dev_priv))
817 		DP |= DP_PIPE_SEL_CHV(pipe);
818 	else
819 		DP |= DP_PIPE_SEL(pipe);
820 
821 	pll_enabled = intel_de_read(dev_priv, DPLL(pipe)) & DPLL_VCO_ENABLE;
822 
823 	/*
824 	 * The DPLL for the pipe must be enabled for this to work.
825 	 * So enable temporarily it if it's not already enabled.
826 	 */
827 	if (!pll_enabled) {
828 		release_cl_override = IS_CHERRYVIEW(dev_priv) &&
829 			!chv_phy_powergate_ch(dev_priv, phy, ch, true);
830 
831 		if (vlv_force_pll_on(dev_priv, pipe, IS_CHERRYVIEW(dev_priv) ?
832 				     &chv_dpll[0].dpll : &vlv_dpll[0].dpll)) {
833 			drm_err(&dev_priv->drm,
834 				"Failed to force on pll for pipe %c!\n",
835 				pipe_name(pipe));
836 			return;
837 		}
838 	}
839 
840 	/*
841 	 * Similar magic as in intel_dp_enable_port().
842 	 * We _must_ do this port enable + disable trick
843 	 * to make this power sequencer lock onto the port.
844 	 * Otherwise even VDD force bit won't work.
845 	 */
846 	intel_de_write(dev_priv, intel_dp->output_reg, DP);
847 	intel_de_posting_read(dev_priv, intel_dp->output_reg);
848 
849 	intel_de_write(dev_priv, intel_dp->output_reg, DP | DP_PORT_EN);
850 	intel_de_posting_read(dev_priv, intel_dp->output_reg);
851 
852 	intel_de_write(dev_priv, intel_dp->output_reg, DP & ~DP_PORT_EN);
853 	intel_de_posting_read(dev_priv, intel_dp->output_reg);
854 
855 	if (!pll_enabled) {
856 		vlv_force_pll_off(dev_priv, pipe);
857 
858 		if (release_cl_override)
859 			chv_phy_powergate_ch(dev_priv, phy, ch, false);
860 	}
861 }
862 
863 static enum pipe vlv_find_free_pps(struct drm_i915_private *dev_priv)
864 {
865 	struct intel_encoder *encoder;
866 	unsigned int pipes = (1 << PIPE_A) | (1 << PIPE_B);
867 
868 	/*
869 	 * We don't have power sequencer currently.
870 	 * Pick one that's not used by other ports.
871 	 */
872 	for_each_intel_dp(&dev_priv->drm, encoder) {
873 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
874 
875 		if (encoder->type == INTEL_OUTPUT_EDP) {
876 			drm_WARN_ON(&dev_priv->drm,
877 				    intel_dp->active_pipe != INVALID_PIPE &&
878 				    intel_dp->active_pipe !=
879 				    intel_dp->pps_pipe);
880 
881 			if (intel_dp->pps_pipe != INVALID_PIPE)
882 				pipes &= ~(1 << intel_dp->pps_pipe);
883 		} else {
884 			drm_WARN_ON(&dev_priv->drm,
885 				    intel_dp->pps_pipe != INVALID_PIPE);
886 
887 			if (intel_dp->active_pipe != INVALID_PIPE)
888 				pipes &= ~(1 << intel_dp->active_pipe);
889 		}
890 	}
891 
892 	if (pipes == 0)
893 		return INVALID_PIPE;
894 
895 	return ffs(pipes) - 1;
896 }
897 
898 static enum pipe
899 vlv_power_sequencer_pipe(struct intel_dp *intel_dp)
900 {
901 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
902 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
903 	enum pipe pipe;
904 
905 	lockdep_assert_held(&dev_priv->pps_mutex);
906 
907 	/* We should never land here with regular DP ports */
908 	drm_WARN_ON(&dev_priv->drm, !intel_dp_is_edp(intel_dp));
909 
910 	drm_WARN_ON(&dev_priv->drm, intel_dp->active_pipe != INVALID_PIPE &&
911 		    intel_dp->active_pipe != intel_dp->pps_pipe);
912 
913 	if (intel_dp->pps_pipe != INVALID_PIPE)
914 		return intel_dp->pps_pipe;
915 
916 	pipe = vlv_find_free_pps(dev_priv);
917 
918 	/*
919 	 * Didn't find one. This should not happen since there
920 	 * are two power sequencers and up to two eDP ports.
921 	 */
922 	if (drm_WARN_ON(&dev_priv->drm, pipe == INVALID_PIPE))
923 		pipe = PIPE_A;
924 
925 	vlv_steal_power_sequencer(dev_priv, pipe);
926 	intel_dp->pps_pipe = pipe;
927 
928 	drm_dbg_kms(&dev_priv->drm,
929 		    "picked pipe %c power sequencer for [ENCODER:%d:%s]\n",
930 		    pipe_name(intel_dp->pps_pipe),
931 		    dig_port->base.base.base.id,
932 		    dig_port->base.base.name);
933 
934 	/* init power sequencer on this pipe and port */
935 	intel_dp_init_panel_power_sequencer(intel_dp);
936 	intel_dp_init_panel_power_sequencer_registers(intel_dp, true);
937 
938 	/*
939 	 * Even vdd force doesn't work until we've made
940 	 * the power sequencer lock in on the port.
941 	 */
942 	vlv_power_sequencer_kick(intel_dp);
943 
944 	return intel_dp->pps_pipe;
945 }
946 
947 static int
948 bxt_power_sequencer_idx(struct intel_dp *intel_dp)
949 {
950 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
951 	int backlight_controller = dev_priv->vbt.backlight.controller;
952 
953 	lockdep_assert_held(&dev_priv->pps_mutex);
954 
955 	/* We should never land here with regular DP ports */
956 	drm_WARN_ON(&dev_priv->drm, !intel_dp_is_edp(intel_dp));
957 
958 	if (!intel_dp->pps_reset)
959 		return backlight_controller;
960 
961 	intel_dp->pps_reset = false;
962 
963 	/*
964 	 * Only the HW needs to be reprogrammed, the SW state is fixed and
965 	 * has been setup during connector init.
966 	 */
967 	intel_dp_init_panel_power_sequencer_registers(intel_dp, false);
968 
969 	return backlight_controller;
970 }
971 
972 typedef bool (*vlv_pipe_check)(struct drm_i915_private *dev_priv,
973 			       enum pipe pipe);
974 
975 static bool vlv_pipe_has_pp_on(struct drm_i915_private *dev_priv,
976 			       enum pipe pipe)
977 {
978 	return intel_de_read(dev_priv, PP_STATUS(pipe)) & PP_ON;
979 }
980 
981 static bool vlv_pipe_has_vdd_on(struct drm_i915_private *dev_priv,
982 				enum pipe pipe)
983 {
984 	return intel_de_read(dev_priv, PP_CONTROL(pipe)) & EDP_FORCE_VDD;
985 }
986 
987 static bool vlv_pipe_any(struct drm_i915_private *dev_priv,
988 			 enum pipe pipe)
989 {
990 	return true;
991 }
992 
993 static enum pipe
994 vlv_initial_pps_pipe(struct drm_i915_private *dev_priv,
995 		     enum port port,
996 		     vlv_pipe_check pipe_check)
997 {
998 	enum pipe pipe;
999 
1000 	for (pipe = PIPE_A; pipe <= PIPE_B; pipe++) {
1001 		u32 port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(pipe)) &
1002 			PANEL_PORT_SELECT_MASK;
1003 
1004 		if (port_sel != PANEL_PORT_SELECT_VLV(port))
1005 			continue;
1006 
1007 		if (!pipe_check(dev_priv, pipe))
1008 			continue;
1009 
1010 		return pipe;
1011 	}
1012 
1013 	return INVALID_PIPE;
1014 }
1015 
1016 static void
1017 vlv_initial_power_sequencer_setup(struct intel_dp *intel_dp)
1018 {
1019 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1020 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1021 	enum port port = dig_port->base.port;
1022 
1023 	lockdep_assert_held(&dev_priv->pps_mutex);
1024 
1025 	/* try to find a pipe with this port selected */
1026 	/* first pick one where the panel is on */
1027 	intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
1028 						  vlv_pipe_has_pp_on);
1029 	/* didn't find one? pick one where vdd is on */
1030 	if (intel_dp->pps_pipe == INVALID_PIPE)
1031 		intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
1032 							  vlv_pipe_has_vdd_on);
1033 	/* didn't find one? pick one with just the correct port */
1034 	if (intel_dp->pps_pipe == INVALID_PIPE)
1035 		intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
1036 							  vlv_pipe_any);
1037 
1038 	/* didn't find one? just let vlv_power_sequencer_pipe() pick one when needed */
1039 	if (intel_dp->pps_pipe == INVALID_PIPE) {
1040 		drm_dbg_kms(&dev_priv->drm,
1041 			    "no initial power sequencer for [ENCODER:%d:%s]\n",
1042 			    dig_port->base.base.base.id,
1043 			    dig_port->base.base.name);
1044 		return;
1045 	}
1046 
1047 	drm_dbg_kms(&dev_priv->drm,
1048 		    "initial power sequencer for [ENCODER:%d:%s]: pipe %c\n",
1049 		    dig_port->base.base.base.id,
1050 		    dig_port->base.base.name,
1051 		    pipe_name(intel_dp->pps_pipe));
1052 
1053 	intel_dp_init_panel_power_sequencer(intel_dp);
1054 	intel_dp_init_panel_power_sequencer_registers(intel_dp, false);
1055 }
1056 
1057 void intel_power_sequencer_reset(struct drm_i915_private *dev_priv)
1058 {
1059 	struct intel_encoder *encoder;
1060 
1061 	if (drm_WARN_ON(&dev_priv->drm,
1062 			!(IS_VALLEYVIEW(dev_priv) ||
1063 			  IS_CHERRYVIEW(dev_priv) ||
1064 			  IS_GEN9_LP(dev_priv))))
1065 		return;
1066 
1067 	/*
1068 	 * We can't grab pps_mutex here due to deadlock with power_domain
1069 	 * mutex when power_domain functions are called while holding pps_mutex.
1070 	 * That also means that in order to use pps_pipe the code needs to
1071 	 * hold both a power domain reference and pps_mutex, and the power domain
1072 	 * reference get/put must be done while _not_ holding pps_mutex.
1073 	 * pps_{lock,unlock}() do these steps in the correct order, so one
1074 	 * should use them always.
1075 	 */
1076 
1077 	for_each_intel_dp(&dev_priv->drm, encoder) {
1078 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1079 
1080 		drm_WARN_ON(&dev_priv->drm,
1081 			    intel_dp->active_pipe != INVALID_PIPE);
1082 
1083 		if (encoder->type != INTEL_OUTPUT_EDP)
1084 			continue;
1085 
1086 		if (IS_GEN9_LP(dev_priv))
1087 			intel_dp->pps_reset = true;
1088 		else
1089 			intel_dp->pps_pipe = INVALID_PIPE;
1090 	}
1091 }
1092 
1093 struct pps_registers {
1094 	i915_reg_t pp_ctrl;
1095 	i915_reg_t pp_stat;
1096 	i915_reg_t pp_on;
1097 	i915_reg_t pp_off;
1098 	i915_reg_t pp_div;
1099 };
1100 
1101 static void intel_pps_get_registers(struct intel_dp *intel_dp,
1102 				    struct pps_registers *regs)
1103 {
1104 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1105 	int pps_idx = 0;
1106 
1107 	memset(regs, 0, sizeof(*regs));
1108 
1109 	if (IS_GEN9_LP(dev_priv))
1110 		pps_idx = bxt_power_sequencer_idx(intel_dp);
1111 	else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
1112 		pps_idx = vlv_power_sequencer_pipe(intel_dp);
1113 
1114 	regs->pp_ctrl = PP_CONTROL(pps_idx);
1115 	regs->pp_stat = PP_STATUS(pps_idx);
1116 	regs->pp_on = PP_ON_DELAYS(pps_idx);
1117 	regs->pp_off = PP_OFF_DELAYS(pps_idx);
1118 
1119 	/* Cycle delay moved from PP_DIVISOR to PP_CONTROL */
1120 	if (IS_GEN9_LP(dev_priv) || INTEL_PCH_TYPE(dev_priv) >= PCH_CNP)
1121 		regs->pp_div = INVALID_MMIO_REG;
1122 	else
1123 		regs->pp_div = PP_DIVISOR(pps_idx);
1124 }
1125 
1126 static i915_reg_t
1127 _pp_ctrl_reg(struct intel_dp *intel_dp)
1128 {
1129 	struct pps_registers regs;
1130 
1131 	intel_pps_get_registers(intel_dp, &regs);
1132 
1133 	return regs.pp_ctrl;
1134 }
1135 
1136 static i915_reg_t
1137 _pp_stat_reg(struct intel_dp *intel_dp)
1138 {
1139 	struct pps_registers regs;
1140 
1141 	intel_pps_get_registers(intel_dp, &regs);
1142 
1143 	return regs.pp_stat;
1144 }
1145 
1146 /* Reboot notifier handler to shutdown panel power to guarantee T12 timing
1147    This function only applicable when panel PM state is not to be tracked */
1148 static int edp_notify_handler(struct notifier_block *this, unsigned long code,
1149 			      void *unused)
1150 {
1151 	struct intel_dp *intel_dp = container_of(this, typeof(* intel_dp),
1152 						 edp_notifier);
1153 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1154 	intel_wakeref_t wakeref;
1155 
1156 	if (!intel_dp_is_edp(intel_dp) || code != SYS_RESTART)
1157 		return 0;
1158 
1159 	with_pps_lock(intel_dp, wakeref) {
1160 		if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
1161 			enum pipe pipe = vlv_power_sequencer_pipe(intel_dp);
1162 			i915_reg_t pp_ctrl_reg, pp_div_reg;
1163 			u32 pp_div;
1164 
1165 			pp_ctrl_reg = PP_CONTROL(pipe);
1166 			pp_div_reg  = PP_DIVISOR(pipe);
1167 			pp_div = intel_de_read(dev_priv, pp_div_reg);
1168 			pp_div &= PP_REFERENCE_DIVIDER_MASK;
1169 
1170 			/* 0x1F write to PP_DIV_REG sets max cycle delay */
1171 			intel_de_write(dev_priv, pp_div_reg, pp_div | 0x1F);
1172 			intel_de_write(dev_priv, pp_ctrl_reg,
1173 				       PANEL_UNLOCK_REGS);
1174 			drm_msleep(intel_dp->panel_power_cycle_delay);
1175 		}
1176 	}
1177 
1178 	return 0;
1179 }
1180 
1181 static bool edp_have_panel_power(struct intel_dp *intel_dp)
1182 {
1183 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1184 
1185 	lockdep_assert_held(&dev_priv->pps_mutex);
1186 
1187 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
1188 	    intel_dp->pps_pipe == INVALID_PIPE)
1189 		return false;
1190 
1191 	return (intel_de_read(dev_priv, _pp_stat_reg(intel_dp)) & PP_ON) != 0;
1192 }
1193 
1194 static bool edp_have_panel_vdd(struct intel_dp *intel_dp)
1195 {
1196 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1197 
1198 	lockdep_assert_held(&dev_priv->pps_mutex);
1199 
1200 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
1201 	    intel_dp->pps_pipe == INVALID_PIPE)
1202 		return false;
1203 
1204 	return intel_de_read(dev_priv, _pp_ctrl_reg(intel_dp)) & EDP_FORCE_VDD;
1205 }
1206 
1207 static void
1208 intel_dp_check_edp(struct intel_dp *intel_dp)
1209 {
1210 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1211 
1212 	if (!intel_dp_is_edp(intel_dp))
1213 		return;
1214 
1215 	if (!edp_have_panel_power(intel_dp) && !edp_have_panel_vdd(intel_dp)) {
1216 		drm_WARN(&dev_priv->drm, 1,
1217 			 "eDP powered off while attempting aux channel communication.\n");
1218 		drm_dbg_kms(&dev_priv->drm, "Status 0x%08x Control 0x%08x\n",
1219 			    intel_de_read(dev_priv, _pp_stat_reg(intel_dp)),
1220 			    intel_de_read(dev_priv, _pp_ctrl_reg(intel_dp)));
1221 	}
1222 }
1223 
1224 static u32
1225 intel_dp_aux_wait_done(struct intel_dp *intel_dp)
1226 {
1227 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1228 	i915_reg_t ch_ctl = intel_dp->aux_ch_ctl_reg(intel_dp);
1229 	const unsigned int timeout_ms = 10;
1230 	u32 status;
1231 	bool done;
1232 
1233 #define C (((status = intel_uncore_read_notrace(&i915->uncore, ch_ctl)) & DP_AUX_CH_CTL_SEND_BUSY) == 0)
1234 	done = wait_event_timeout(i915->gmbus_wait_queue, C,
1235 				  msecs_to_jiffies_timeout(timeout_ms));
1236 
1237 	/* just trace the final value */
1238 	trace_i915_reg_rw(false, ch_ctl, status, sizeof(status), true);
1239 
1240 	if (!done)
1241 		drm_err(&i915->drm,
1242 			"%s: did not complete or timeout within %ums (status 0x%08x)\n",
1243 			intel_dp->aux.name, timeout_ms, status);
1244 #undef C
1245 
1246 	return status;
1247 }
1248 
1249 static u32 g4x_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
1250 {
1251 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1252 
1253 	if (index)
1254 		return 0;
1255 
1256 	/*
1257 	 * The clock divider is based off the hrawclk, and would like to run at
1258 	 * 2MHz.  So, take the hrawclk value and divide by 2000 and use that
1259 	 */
1260 	return DIV_ROUND_CLOSEST(RUNTIME_INFO(dev_priv)->rawclk_freq, 2000);
1261 }
1262 
1263 static u32 ilk_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
1264 {
1265 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1266 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1267 	u32 freq;
1268 
1269 	if (index)
1270 		return 0;
1271 
1272 	/*
1273 	 * The clock divider is based off the cdclk or PCH rawclk, and would
1274 	 * like to run at 2MHz.  So, take the cdclk or PCH rawclk value and
1275 	 * divide by 2000 and use that
1276 	 */
1277 	if (dig_port->aux_ch == AUX_CH_A)
1278 		freq = dev_priv->cdclk.hw.cdclk;
1279 	else
1280 		freq = RUNTIME_INFO(dev_priv)->rawclk_freq;
1281 	return DIV_ROUND_CLOSEST(freq, 2000);
1282 }
1283 
1284 static u32 hsw_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
1285 {
1286 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1287 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1288 
1289 	if (dig_port->aux_ch != AUX_CH_A && HAS_PCH_LPT_H(dev_priv)) {
1290 		/* Workaround for non-ULT HSW */
1291 		switch (index) {
1292 		case 0: return 63;
1293 		case 1: return 72;
1294 		default: return 0;
1295 		}
1296 	}
1297 
1298 	return ilk_get_aux_clock_divider(intel_dp, index);
1299 }
1300 
1301 static u32 skl_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
1302 {
1303 	/*
1304 	 * SKL doesn't need us to program the AUX clock divider (Hardware will
1305 	 * derive the clock from CDCLK automatically). We still implement the
1306 	 * get_aux_clock_divider vfunc to plug-in into the existing code.
1307 	 */
1308 	return index ? 0 : 1;
1309 }
1310 
1311 static u32 g4x_get_aux_send_ctl(struct intel_dp *intel_dp,
1312 				int send_bytes,
1313 				u32 aux_clock_divider)
1314 {
1315 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1316 	struct drm_i915_private *dev_priv =
1317 			to_i915(dig_port->base.base.dev);
1318 	u32 precharge, timeout;
1319 
1320 	if (IS_GEN(dev_priv, 6))
1321 		precharge = 3;
1322 	else
1323 		precharge = 5;
1324 
1325 	if (IS_BROADWELL(dev_priv))
1326 		timeout = DP_AUX_CH_CTL_TIME_OUT_600us;
1327 	else
1328 		timeout = DP_AUX_CH_CTL_TIME_OUT_400us;
1329 
1330 	return DP_AUX_CH_CTL_SEND_BUSY |
1331 	       DP_AUX_CH_CTL_DONE |
1332 	       DP_AUX_CH_CTL_INTERRUPT |
1333 	       DP_AUX_CH_CTL_TIME_OUT_ERROR |
1334 	       timeout |
1335 	       DP_AUX_CH_CTL_RECEIVE_ERROR |
1336 	       (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
1337 	       (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
1338 	       (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT);
1339 }
1340 
1341 static u32 skl_get_aux_send_ctl(struct intel_dp *intel_dp,
1342 				int send_bytes,
1343 				u32 unused)
1344 {
1345 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1346 	struct drm_i915_private *i915 =
1347 			to_i915(dig_port->base.base.dev);
1348 	enum phy phy = intel_port_to_phy(i915, dig_port->base.port);
1349 	u32 ret;
1350 
1351 	ret = DP_AUX_CH_CTL_SEND_BUSY |
1352 	      DP_AUX_CH_CTL_DONE |
1353 	      DP_AUX_CH_CTL_INTERRUPT |
1354 	      DP_AUX_CH_CTL_TIME_OUT_ERROR |
1355 	      DP_AUX_CH_CTL_TIME_OUT_MAX |
1356 	      DP_AUX_CH_CTL_RECEIVE_ERROR |
1357 	      (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
1358 	      DP_AUX_CH_CTL_FW_SYNC_PULSE_SKL(32) |
1359 	      DP_AUX_CH_CTL_SYNC_PULSE_SKL(32);
1360 
1361 	if (intel_phy_is_tc(i915, phy) &&
1362 	    dig_port->tc_mode == TC_PORT_TBT_ALT)
1363 		ret |= DP_AUX_CH_CTL_TBT_IO;
1364 
1365 	return ret;
1366 }
1367 
1368 static int
1369 intel_dp_aux_xfer(struct intel_dp *intel_dp,
1370 		  const u8 *send, int send_bytes,
1371 		  u8 *recv, int recv_size,
1372 		  u32 aux_send_ctl_flags)
1373 {
1374 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1375 	struct drm_i915_private *i915 =
1376 			to_i915(dig_port->base.base.dev);
1377 	struct intel_uncore *uncore = &i915->uncore;
1378 	enum phy phy = intel_port_to_phy(i915, dig_port->base.port);
1379 	bool is_tc_port = intel_phy_is_tc(i915, phy);
1380 	i915_reg_t ch_ctl, ch_data[5];
1381 	u32 aux_clock_divider;
1382 	enum intel_display_power_domain aux_domain;
1383 	intel_wakeref_t aux_wakeref;
1384 	intel_wakeref_t pps_wakeref;
1385 	int i, ret, recv_bytes;
1386 	int try, clock = 0;
1387 	u32 status;
1388 	bool vdd;
1389 
1390 	ch_ctl = intel_dp->aux_ch_ctl_reg(intel_dp);
1391 	for (i = 0; i < ARRAY_SIZE(ch_data); i++)
1392 		ch_data[i] = intel_dp->aux_ch_data_reg(intel_dp, i);
1393 
1394 	if (is_tc_port)
1395 		intel_tc_port_lock(dig_port);
1396 
1397 	aux_domain = intel_aux_power_domain(dig_port);
1398 
1399 	aux_wakeref = intel_display_power_get(i915, aux_domain);
1400 	pps_wakeref = pps_lock(intel_dp);
1401 
1402 	/*
1403 	 * We will be called with VDD already enabled for dpcd/edid/oui reads.
1404 	 * In such cases we want to leave VDD enabled and it's up to upper layers
1405 	 * to turn it off. But for eg. i2c-dev access we need to turn it on/off
1406 	 * ourselves.
1407 	 */
1408 	vdd = edp_panel_vdd_on(intel_dp);
1409 
1410 	/* dp aux is extremely sensitive to irq latency, hence request the
1411 	 * lowest possible wakeup latency and so prevent the cpu from going into
1412 	 * deep sleep states.
1413 	 */
1414 	cpu_latency_qos_update_request(&intel_dp->pm_qos, 0);
1415 
1416 	intel_dp_check_edp(intel_dp);
1417 
1418 	/* Try to wait for any previous AUX channel activity */
1419 	for (try = 0; try < 3; try++) {
1420 		status = intel_uncore_read_notrace(uncore, ch_ctl);
1421 		if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
1422 			break;
1423 		drm_msleep(1);
1424 	}
1425 	/* just trace the final value */
1426 	trace_i915_reg_rw(false, ch_ctl, status, sizeof(status), true);
1427 
1428 	if (try == 3) {
1429 		const u32 status = intel_uncore_read(uncore, ch_ctl);
1430 
1431 		if (status != intel_dp->aux_busy_last_status) {
1432 			drm_WARN(&i915->drm, 1,
1433 				 "%s: not started (status 0x%08x)\n",
1434 				 intel_dp->aux.name, status);
1435 			intel_dp->aux_busy_last_status = status;
1436 		}
1437 
1438 		ret = -EBUSY;
1439 		goto out;
1440 	}
1441 
1442 	/* Only 5 data registers! */
1443 	if (drm_WARN_ON(&i915->drm, send_bytes > 20 || recv_size > 20)) {
1444 		ret = -E2BIG;
1445 		goto out;
1446 	}
1447 
1448 	while ((aux_clock_divider = intel_dp->get_aux_clock_divider(intel_dp, clock++))) {
1449 		u32 send_ctl = intel_dp->get_aux_send_ctl(intel_dp,
1450 							  send_bytes,
1451 							  aux_clock_divider);
1452 
1453 		send_ctl |= aux_send_ctl_flags;
1454 
1455 		/* Must try at least 3 times according to DP spec */
1456 		for (try = 0; try < 5; try++) {
1457 			/* Load the send data into the aux channel data registers */
1458 			for (i = 0; i < send_bytes; i += 4)
1459 				intel_uncore_write(uncore,
1460 						   ch_data[i >> 2],
1461 						   intel_dp_pack_aux(send + i,
1462 								     send_bytes - i));
1463 
1464 			/* Send the command and wait for it to complete */
1465 			intel_uncore_write(uncore, ch_ctl, send_ctl);
1466 
1467 			status = intel_dp_aux_wait_done(intel_dp);
1468 
1469 			/* Clear done status and any errors */
1470 			intel_uncore_write(uncore,
1471 					   ch_ctl,
1472 					   status |
1473 					   DP_AUX_CH_CTL_DONE |
1474 					   DP_AUX_CH_CTL_TIME_OUT_ERROR |
1475 					   DP_AUX_CH_CTL_RECEIVE_ERROR);
1476 
1477 			/* DP CTS 1.2 Core Rev 1.1, 4.2.1.1 & 4.2.1.2
1478 			 *   400us delay required for errors and timeouts
1479 			 *   Timeout errors from the HW already meet this
1480 			 *   requirement so skip to next iteration
1481 			 */
1482 			if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR)
1483 				continue;
1484 
1485 			if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
1486 				usleep_range(400, 500);
1487 				continue;
1488 			}
1489 			if (status & DP_AUX_CH_CTL_DONE)
1490 				goto done;
1491 		}
1492 	}
1493 
1494 	if ((status & DP_AUX_CH_CTL_DONE) == 0) {
1495 		drm_err(&i915->drm, "%s: not done (status 0x%08x)\n",
1496 			intel_dp->aux.name, status);
1497 		ret = -EBUSY;
1498 		goto out;
1499 	}
1500 
1501 done:
1502 	/* Check for timeout or receive error.
1503 	 * Timeouts occur when the sink is not connected
1504 	 */
1505 	if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
1506 		drm_err(&i915->drm, "%s: receive error (status 0x%08x)\n",
1507 			intel_dp->aux.name, status);
1508 		ret = -EIO;
1509 		goto out;
1510 	}
1511 
1512 	/* Timeouts occur when the device isn't connected, so they're
1513 	 * "normal" -- don't fill the kernel log with these */
1514 	if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
1515 		drm_dbg_kms(&i915->drm, "%s: timeout (status 0x%08x)\n",
1516 			    intel_dp->aux.name, status);
1517 		ret = -ETIMEDOUT;
1518 		goto out;
1519 	}
1520 
1521 	/* Unload any bytes sent back from the other side */
1522 	recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
1523 		      DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
1524 
1525 	/*
1526 	 * By BSpec: "Message sizes of 0 or >20 are not allowed."
1527 	 * We have no idea of what happened so we return -EBUSY so
1528 	 * drm layer takes care for the necessary retries.
1529 	 */
1530 	if (recv_bytes == 0 || recv_bytes > 20) {
1531 		drm_dbg_kms(&i915->drm,
1532 			    "%s: Forbidden recv_bytes = %d on aux transaction\n",
1533 			    intel_dp->aux.name, recv_bytes);
1534 		ret = -EBUSY;
1535 		goto out;
1536 	}
1537 
1538 	if (recv_bytes > recv_size)
1539 		recv_bytes = recv_size;
1540 
1541 	for (i = 0; i < recv_bytes; i += 4)
1542 		intel_dp_unpack_aux(intel_uncore_read(uncore, ch_data[i >> 2]),
1543 				    recv + i, recv_bytes - i);
1544 
1545 	ret = recv_bytes;
1546 out:
1547 	cpu_latency_qos_update_request(&intel_dp->pm_qos, PM_QOS_DEFAULT_VALUE);
1548 
1549 	if (vdd)
1550 		edp_panel_vdd_off(intel_dp, false);
1551 
1552 	pps_unlock(intel_dp, pps_wakeref);
1553 	intel_display_power_put_async(i915, aux_domain, aux_wakeref);
1554 
1555 	if (is_tc_port)
1556 		intel_tc_port_unlock(dig_port);
1557 
1558 	return ret;
1559 }
1560 
1561 #define BARE_ADDRESS_SIZE	3
1562 #define HEADER_SIZE		(BARE_ADDRESS_SIZE + 1)
1563 
1564 static void
1565 intel_dp_aux_header(u8 txbuf[HEADER_SIZE],
1566 		    const struct drm_dp_aux_msg *msg)
1567 {
1568 	txbuf[0] = (msg->request << 4) | ((msg->address >> 16) & 0xf);
1569 	txbuf[1] = (msg->address >> 8) & 0xff;
1570 	txbuf[2] = msg->address & 0xff;
1571 	txbuf[3] = msg->size - 1;
1572 }
1573 
1574 static u32 intel_dp_aux_xfer_flags(const struct drm_dp_aux_msg *msg)
1575 {
1576 	/*
1577 	 * If we're trying to send the HDCP Aksv, we need to set a the Aksv
1578 	 * select bit to inform the hardware to send the Aksv after our header
1579 	 * since we can't access that data from software.
1580 	 */
1581 	if ((msg->request & ~DP_AUX_I2C_MOT) == DP_AUX_NATIVE_WRITE &&
1582 	    msg->address == DP_AUX_HDCP_AKSV)
1583 		return DP_AUX_CH_CTL_AUX_AKSV_SELECT;
1584 
1585 	return 0;
1586 }
1587 
1588 static ssize_t
1589 intel_dp_aux_transfer(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
1590 {
1591 	struct intel_dp *intel_dp = container_of(aux, struct intel_dp, aux);
1592 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1593 	u8 txbuf[20], rxbuf[20];
1594 	size_t txsize, rxsize;
1595 	u32 flags = intel_dp_aux_xfer_flags(msg);
1596 	int ret;
1597 
1598 	intel_dp_aux_header(txbuf, msg);
1599 
1600 	switch (msg->request & ~DP_AUX_I2C_MOT) {
1601 	case DP_AUX_NATIVE_WRITE:
1602 	case DP_AUX_I2C_WRITE:
1603 	case DP_AUX_I2C_WRITE_STATUS_UPDATE:
1604 		txsize = msg->size ? HEADER_SIZE + msg->size : BARE_ADDRESS_SIZE;
1605 		rxsize = 2; /* 0 or 1 data bytes */
1606 
1607 		if (drm_WARN_ON(&i915->drm, txsize > 20))
1608 			return -E2BIG;
1609 
1610 		drm_WARN_ON(&i915->drm, !msg->buffer != !msg->size);
1611 
1612 		if (msg->buffer)
1613 			memcpy(txbuf + HEADER_SIZE, msg->buffer, msg->size);
1614 
1615 		ret = intel_dp_aux_xfer(intel_dp, txbuf, txsize,
1616 					rxbuf, rxsize, flags);
1617 		if (ret > 0) {
1618 			msg->reply = rxbuf[0] >> 4;
1619 
1620 			if (ret > 1) {
1621 				/* Number of bytes written in a short write. */
1622 				ret = clamp_t(int, rxbuf[1], 0, msg->size);
1623 			} else {
1624 				/* Return payload size. */
1625 				ret = msg->size;
1626 			}
1627 		}
1628 		break;
1629 
1630 	case DP_AUX_NATIVE_READ:
1631 	case DP_AUX_I2C_READ:
1632 		txsize = msg->size ? HEADER_SIZE : BARE_ADDRESS_SIZE;
1633 		rxsize = msg->size + 1;
1634 
1635 		if (drm_WARN_ON(&i915->drm, rxsize > 20))
1636 			return -E2BIG;
1637 
1638 		ret = intel_dp_aux_xfer(intel_dp, txbuf, txsize,
1639 					rxbuf, rxsize, flags);
1640 		if (ret > 0) {
1641 			msg->reply = rxbuf[0] >> 4;
1642 			/*
1643 			 * Assume happy day, and copy the data. The caller is
1644 			 * expected to check msg->reply before touching it.
1645 			 *
1646 			 * Return payload size.
1647 			 */
1648 			ret--;
1649 			memcpy(msg->buffer, rxbuf + 1, ret);
1650 		}
1651 		break;
1652 
1653 	default:
1654 		ret = -EINVAL;
1655 		break;
1656 	}
1657 
1658 	return ret;
1659 }
1660 
1661 
1662 static i915_reg_t g4x_aux_ctl_reg(struct intel_dp *intel_dp)
1663 {
1664 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1665 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1666 	enum aux_ch aux_ch = dig_port->aux_ch;
1667 
1668 	switch (aux_ch) {
1669 	case AUX_CH_B:
1670 	case AUX_CH_C:
1671 	case AUX_CH_D:
1672 		return DP_AUX_CH_CTL(aux_ch);
1673 	default:
1674 		MISSING_CASE(aux_ch);
1675 		return DP_AUX_CH_CTL(AUX_CH_B);
1676 	}
1677 }
1678 
1679 static i915_reg_t g4x_aux_data_reg(struct intel_dp *intel_dp, int index)
1680 {
1681 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1682 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1683 	enum aux_ch aux_ch = dig_port->aux_ch;
1684 
1685 	switch (aux_ch) {
1686 	case AUX_CH_B:
1687 	case AUX_CH_C:
1688 	case AUX_CH_D:
1689 		return DP_AUX_CH_DATA(aux_ch, index);
1690 	default:
1691 		MISSING_CASE(aux_ch);
1692 		return DP_AUX_CH_DATA(AUX_CH_B, index);
1693 	}
1694 }
1695 
1696 static i915_reg_t ilk_aux_ctl_reg(struct intel_dp *intel_dp)
1697 {
1698 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1699 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1700 	enum aux_ch aux_ch = dig_port->aux_ch;
1701 
1702 	switch (aux_ch) {
1703 	case AUX_CH_A:
1704 		return DP_AUX_CH_CTL(aux_ch);
1705 	case AUX_CH_B:
1706 	case AUX_CH_C:
1707 	case AUX_CH_D:
1708 		return PCH_DP_AUX_CH_CTL(aux_ch);
1709 	default:
1710 		MISSING_CASE(aux_ch);
1711 		return DP_AUX_CH_CTL(AUX_CH_A);
1712 	}
1713 }
1714 
1715 static i915_reg_t ilk_aux_data_reg(struct intel_dp *intel_dp, int index)
1716 {
1717 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1718 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1719 	enum aux_ch aux_ch = dig_port->aux_ch;
1720 
1721 	switch (aux_ch) {
1722 	case AUX_CH_A:
1723 		return DP_AUX_CH_DATA(aux_ch, index);
1724 	case AUX_CH_B:
1725 	case AUX_CH_C:
1726 	case AUX_CH_D:
1727 		return PCH_DP_AUX_CH_DATA(aux_ch, index);
1728 	default:
1729 		MISSING_CASE(aux_ch);
1730 		return DP_AUX_CH_DATA(AUX_CH_A, index);
1731 	}
1732 }
1733 
1734 static i915_reg_t skl_aux_ctl_reg(struct intel_dp *intel_dp)
1735 {
1736 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1737 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1738 	enum aux_ch aux_ch = dig_port->aux_ch;
1739 
1740 	switch (aux_ch) {
1741 	case AUX_CH_A:
1742 	case AUX_CH_B:
1743 	case AUX_CH_C:
1744 	case AUX_CH_D:
1745 	case AUX_CH_E:
1746 	case AUX_CH_F:
1747 	case AUX_CH_G:
1748 		return DP_AUX_CH_CTL(aux_ch);
1749 	default:
1750 		MISSING_CASE(aux_ch);
1751 		return DP_AUX_CH_CTL(AUX_CH_A);
1752 	}
1753 }
1754 
1755 static i915_reg_t skl_aux_data_reg(struct intel_dp *intel_dp, int index)
1756 {
1757 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1758 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1759 	enum aux_ch aux_ch = dig_port->aux_ch;
1760 
1761 	switch (aux_ch) {
1762 	case AUX_CH_A:
1763 	case AUX_CH_B:
1764 	case AUX_CH_C:
1765 	case AUX_CH_D:
1766 	case AUX_CH_E:
1767 	case AUX_CH_F:
1768 	case AUX_CH_G:
1769 		return DP_AUX_CH_DATA(aux_ch, index);
1770 	default:
1771 		MISSING_CASE(aux_ch);
1772 		return DP_AUX_CH_DATA(AUX_CH_A, index);
1773 	}
1774 }
1775 
1776 static void
1777 intel_dp_aux_fini(struct intel_dp *intel_dp)
1778 {
1779 	if (cpu_latency_qos_request_active(&intel_dp->pm_qos))
1780 		cpu_latency_qos_remove_request(&intel_dp->pm_qos);
1781 
1782 	kfree(intel_dp->aux.name);
1783 }
1784 
1785 static void
1786 intel_dp_aux_init(struct intel_dp *intel_dp)
1787 {
1788 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1789 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1790 	struct intel_encoder *encoder = &dig_port->base;
1791 
1792 	if (INTEL_GEN(dev_priv) >= 9) {
1793 		intel_dp->aux_ch_ctl_reg = skl_aux_ctl_reg;
1794 		intel_dp->aux_ch_data_reg = skl_aux_data_reg;
1795 	} else if (HAS_PCH_SPLIT(dev_priv)) {
1796 		intel_dp->aux_ch_ctl_reg = ilk_aux_ctl_reg;
1797 		intel_dp->aux_ch_data_reg = ilk_aux_data_reg;
1798 	} else {
1799 		intel_dp->aux_ch_ctl_reg = g4x_aux_ctl_reg;
1800 		intel_dp->aux_ch_data_reg = g4x_aux_data_reg;
1801 	}
1802 
1803 	if (INTEL_GEN(dev_priv) >= 9)
1804 		intel_dp->get_aux_clock_divider = skl_get_aux_clock_divider;
1805 	else if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
1806 		intel_dp->get_aux_clock_divider = hsw_get_aux_clock_divider;
1807 	else if (HAS_PCH_SPLIT(dev_priv))
1808 		intel_dp->get_aux_clock_divider = ilk_get_aux_clock_divider;
1809 	else
1810 		intel_dp->get_aux_clock_divider = g4x_get_aux_clock_divider;
1811 
1812 	if (INTEL_GEN(dev_priv) >= 9)
1813 		intel_dp->get_aux_send_ctl = skl_get_aux_send_ctl;
1814 	else
1815 		intel_dp->get_aux_send_ctl = g4x_get_aux_send_ctl;
1816 
1817 	drm_dp_aux_init(&intel_dp->aux);
1818 
1819 	/* Failure to allocate our preferred name is not critical */
1820 	intel_dp->aux.name = kasprintf(GFP_KERNEL, "AUX %c/port %c",
1821 				       aux_ch_name(dig_port->aux_ch),
1822 				       port_name(encoder->port));
1823 	intel_dp->aux.transfer = intel_dp_aux_transfer;
1824 	cpu_latency_qos_add_request(&intel_dp->pm_qos, PM_QOS_DEFAULT_VALUE);
1825 }
1826 
1827 bool intel_dp_source_supports_hbr2(struct intel_dp *intel_dp)
1828 {
1829 	int max_rate = intel_dp->source_rates[intel_dp->num_source_rates - 1];
1830 
1831 	return max_rate >= 540000;
1832 }
1833 
1834 bool intel_dp_source_supports_hbr3(struct intel_dp *intel_dp)
1835 {
1836 	int max_rate = intel_dp->source_rates[intel_dp->num_source_rates - 1];
1837 
1838 	return max_rate >= 810000;
1839 }
1840 
1841 static void
1842 intel_dp_set_clock(struct intel_encoder *encoder,
1843 		   struct intel_crtc_state *pipe_config)
1844 {
1845 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1846 	const struct dp_link_dpll *divisor = NULL;
1847 	int i, count = 0;
1848 
1849 	if (IS_G4X(dev_priv)) {
1850 		divisor = g4x_dpll;
1851 		count = ARRAY_SIZE(g4x_dpll);
1852 	} else if (HAS_PCH_SPLIT(dev_priv)) {
1853 		divisor = pch_dpll;
1854 		count = ARRAY_SIZE(pch_dpll);
1855 	} else if (IS_CHERRYVIEW(dev_priv)) {
1856 		divisor = chv_dpll;
1857 		count = ARRAY_SIZE(chv_dpll);
1858 	} else if (IS_VALLEYVIEW(dev_priv)) {
1859 		divisor = vlv_dpll;
1860 		count = ARRAY_SIZE(vlv_dpll);
1861 	}
1862 
1863 	if (divisor && count) {
1864 		for (i = 0; i < count; i++) {
1865 			if (pipe_config->port_clock == divisor[i].clock) {
1866 				pipe_config->dpll = divisor[i].dpll;
1867 				pipe_config->clock_set = true;
1868 				break;
1869 			}
1870 		}
1871 	}
1872 }
1873 
1874 static void snprintf_int_array(char *str, size_t len,
1875 			       const int *array, int nelem)
1876 {
1877 	int i;
1878 
1879 	str[0] = '\0';
1880 
1881 	for (i = 0; i < nelem; i++) {
1882 		int r = snprintf(str, len, "%s%d", i ? ", " : "", array[i]);
1883 		if (r >= len)
1884 			return;
1885 		str += r;
1886 		len -= r;
1887 	}
1888 }
1889 
1890 static void intel_dp_print_rates(struct intel_dp *intel_dp)
1891 {
1892 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1893 	char str[128]; /* FIXME: too big for stack? */
1894 
1895 	if (!drm_debug_enabled(DRM_UT_KMS))
1896 		return;
1897 
1898 	snprintf_int_array(str, sizeof(str),
1899 			   intel_dp->source_rates, intel_dp->num_source_rates);
1900 	drm_dbg_kms(&i915->drm, "source rates: %s\n", str);
1901 
1902 	snprintf_int_array(str, sizeof(str),
1903 			   intel_dp->sink_rates, intel_dp->num_sink_rates);
1904 	drm_dbg_kms(&i915->drm, "sink rates: %s\n", str);
1905 
1906 	snprintf_int_array(str, sizeof(str),
1907 			   intel_dp->common_rates, intel_dp->num_common_rates);
1908 	drm_dbg_kms(&i915->drm, "common rates: %s\n", str);
1909 }
1910 
1911 int
1912 intel_dp_max_link_rate(struct intel_dp *intel_dp)
1913 {
1914 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1915 	int len;
1916 
1917 	len = intel_dp_common_len_rate_limit(intel_dp, intel_dp->max_link_rate);
1918 	if (drm_WARN_ON(&i915->drm, len <= 0))
1919 		return 162000;
1920 
1921 	return intel_dp->common_rates[len - 1];
1922 }
1923 
1924 int intel_dp_rate_select(struct intel_dp *intel_dp, int rate)
1925 {
1926 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1927 	int i = intel_dp_rate_index(intel_dp->sink_rates,
1928 				    intel_dp->num_sink_rates, rate);
1929 
1930 	if (drm_WARN_ON(&i915->drm, i < 0))
1931 		i = 0;
1932 
1933 	return i;
1934 }
1935 
1936 void intel_dp_compute_rate(struct intel_dp *intel_dp, int port_clock,
1937 			   u8 *link_bw, u8 *rate_select)
1938 {
1939 	/* eDP 1.4 rate select method. */
1940 	if (intel_dp->use_rate_select) {
1941 		*link_bw = 0;
1942 		*rate_select =
1943 			intel_dp_rate_select(intel_dp, port_clock);
1944 	} else {
1945 		*link_bw = drm_dp_link_rate_to_bw_code(port_clock);
1946 		*rate_select = 0;
1947 	}
1948 }
1949 
1950 static bool intel_dp_source_supports_fec(struct intel_dp *intel_dp,
1951 					 const struct intel_crtc_state *pipe_config)
1952 {
1953 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1954 
1955 	/* On TGL, FEC is supported on all Pipes */
1956 	if (INTEL_GEN(dev_priv) >= 12)
1957 		return true;
1958 
1959 	if (IS_GEN(dev_priv, 11) && pipe_config->cpu_transcoder != TRANSCODER_A)
1960 		return true;
1961 
1962 	return false;
1963 }
1964 
1965 static bool intel_dp_supports_fec(struct intel_dp *intel_dp,
1966 				  const struct intel_crtc_state *pipe_config)
1967 {
1968 	return intel_dp_source_supports_fec(intel_dp, pipe_config) &&
1969 		drm_dp_sink_supports_fec(intel_dp->fec_capable);
1970 }
1971 
1972 static bool intel_dp_supports_dsc(struct intel_dp *intel_dp,
1973 				  const struct intel_crtc_state *crtc_state)
1974 {
1975 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
1976 
1977 	if (!intel_dp_is_edp(intel_dp) && !crtc_state->fec_enable)
1978 		return false;
1979 
1980 	return intel_dsc_source_support(encoder, crtc_state) &&
1981 		drm_dp_sink_supports_dsc(intel_dp->dsc_dpcd);
1982 }
1983 
1984 static bool intel_dp_hdmi_ycbcr420(struct intel_dp *intel_dp,
1985 				   const struct intel_crtc_state *crtc_state)
1986 {
1987 	return crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
1988 		(crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444 &&
1989 		 intel_dp->dfp.ycbcr_444_to_420);
1990 }
1991 
1992 static int intel_dp_hdmi_tmds_clock(struct intel_dp *intel_dp,
1993 				    const struct intel_crtc_state *crtc_state, int bpc)
1994 {
1995 	int clock = crtc_state->hw.adjusted_mode.crtc_clock * bpc / 8;
1996 
1997 	if (intel_dp_hdmi_ycbcr420(intel_dp, crtc_state))
1998 		clock /= 2;
1999 
2000 	return clock;
2001 }
2002 
2003 static bool intel_dp_hdmi_tmds_clock_valid(struct intel_dp *intel_dp,
2004 					   const struct intel_crtc_state *crtc_state, int bpc)
2005 {
2006 	int tmds_clock = intel_dp_hdmi_tmds_clock(intel_dp, crtc_state, bpc);
2007 
2008 	if (intel_dp->dfp.min_tmds_clock &&
2009 	    tmds_clock < intel_dp->dfp.min_tmds_clock)
2010 		return false;
2011 
2012 	if (intel_dp->dfp.max_tmds_clock &&
2013 	    tmds_clock > intel_dp->dfp.max_tmds_clock)
2014 		return false;
2015 
2016 	return true;
2017 }
2018 
2019 static bool intel_dp_hdmi_deep_color_possible(struct intel_dp *intel_dp,
2020 					      const struct intel_crtc_state *crtc_state,
2021 					      int bpc)
2022 {
2023 
2024 	return intel_hdmi_deep_color_possible(crtc_state, bpc,
2025 					      intel_dp->has_hdmi_sink,
2026 					      intel_dp_hdmi_ycbcr420(intel_dp, crtc_state)) &&
2027 		intel_dp_hdmi_tmds_clock_valid(intel_dp, crtc_state, bpc);
2028 }
2029 
2030 static int intel_dp_max_bpp(struct intel_dp *intel_dp,
2031 			    const struct intel_crtc_state *crtc_state)
2032 {
2033 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
2034 	struct intel_connector *intel_connector = intel_dp->attached_connector;
2035 	int bpp, bpc;
2036 
2037 	bpc = crtc_state->pipe_bpp / 3;
2038 
2039 	if (intel_dp->dfp.max_bpc)
2040 		bpc = min_t(int, bpc, intel_dp->dfp.max_bpc);
2041 
2042 	if (intel_dp->dfp.min_tmds_clock) {
2043 		for (; bpc >= 10; bpc -= 2) {
2044 			if (intel_dp_hdmi_deep_color_possible(intel_dp, crtc_state, bpc))
2045 				break;
2046 		}
2047 	}
2048 
2049 	bpp = bpc * 3;
2050 	if (intel_dp_is_edp(intel_dp)) {
2051 		/* Get bpp from vbt only for panels that dont have bpp in edid */
2052 		if (intel_connector->base.display_info.bpc == 0 &&
2053 		    dev_priv->vbt.edp.bpp && dev_priv->vbt.edp.bpp < bpp) {
2054 			drm_dbg_kms(&dev_priv->drm,
2055 				    "clamping bpp for eDP panel to BIOS-provided %i\n",
2056 				    dev_priv->vbt.edp.bpp);
2057 			bpp = dev_priv->vbt.edp.bpp;
2058 		}
2059 	}
2060 
2061 	return bpp;
2062 }
2063 
2064 /* Adjust link config limits based on compliance test requests. */
2065 void
2066 intel_dp_adjust_compliance_config(struct intel_dp *intel_dp,
2067 				  struct intel_crtc_state *pipe_config,
2068 				  struct link_config_limits *limits)
2069 {
2070 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2071 
2072 	/* For DP Compliance we override the computed bpp for the pipe */
2073 	if (intel_dp->compliance.test_data.bpc != 0) {
2074 		int bpp = 3 * intel_dp->compliance.test_data.bpc;
2075 
2076 		limits->min_bpp = limits->max_bpp = bpp;
2077 		pipe_config->dither_force_disable = bpp == 6 * 3;
2078 
2079 		drm_dbg_kms(&i915->drm, "Setting pipe_bpp to %d\n", bpp);
2080 	}
2081 
2082 	/* Use values requested by Compliance Test Request */
2083 	if (intel_dp->compliance.test_type == DP_TEST_LINK_TRAINING) {
2084 		int index;
2085 
2086 		/* Validate the compliance test data since max values
2087 		 * might have changed due to link train fallback.
2088 		 */
2089 		if (intel_dp_link_params_valid(intel_dp, intel_dp->compliance.test_link_rate,
2090 					       intel_dp->compliance.test_lane_count)) {
2091 			index = intel_dp_rate_index(intel_dp->common_rates,
2092 						    intel_dp->num_common_rates,
2093 						    intel_dp->compliance.test_link_rate);
2094 			if (index >= 0)
2095 				limits->min_clock = limits->max_clock = index;
2096 			limits->min_lane_count = limits->max_lane_count =
2097 				intel_dp->compliance.test_lane_count;
2098 		}
2099 	}
2100 }
2101 
2102 static int intel_dp_output_bpp(const struct intel_crtc_state *crtc_state, int bpp)
2103 {
2104 	/*
2105 	 * bpp value was assumed to RGB format. And YCbCr 4:2:0 output
2106 	 * format of the number of bytes per pixel will be half the number
2107 	 * of bytes of RGB pixel.
2108 	 */
2109 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
2110 		bpp /= 2;
2111 
2112 	return bpp;
2113 }
2114 
2115 /* Optimize link config in order: max bpp, min clock, min lanes */
2116 static int
2117 intel_dp_compute_link_config_wide(struct intel_dp *intel_dp,
2118 				  struct intel_crtc_state *pipe_config,
2119 				  const struct link_config_limits *limits)
2120 {
2121 	struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
2122 	int bpp, clock, lane_count;
2123 	int mode_rate, link_clock, link_avail;
2124 
2125 	for (bpp = limits->max_bpp; bpp >= limits->min_bpp; bpp -= 2 * 3) {
2126 		int output_bpp = intel_dp_output_bpp(pipe_config, bpp);
2127 
2128 		mode_rate = intel_dp_link_required(adjusted_mode->crtc_clock,
2129 						   output_bpp);
2130 
2131 		for (clock = limits->min_clock; clock <= limits->max_clock; clock++) {
2132 			for (lane_count = limits->min_lane_count;
2133 			     lane_count <= limits->max_lane_count;
2134 			     lane_count <<= 1) {
2135 				link_clock = intel_dp->common_rates[clock];
2136 				link_avail = intel_dp_max_data_rate(link_clock,
2137 								    lane_count);
2138 
2139 				if (mode_rate <= link_avail) {
2140 					pipe_config->lane_count = lane_count;
2141 					pipe_config->pipe_bpp = bpp;
2142 					pipe_config->port_clock = link_clock;
2143 
2144 					return 0;
2145 				}
2146 			}
2147 		}
2148 	}
2149 
2150 	return -EINVAL;
2151 }
2152 
2153 static int intel_dp_dsc_compute_bpp(struct intel_dp *intel_dp, u8 dsc_max_bpc)
2154 {
2155 	int i, num_bpc;
2156 	u8 dsc_bpc[3] = {0};
2157 
2158 	num_bpc = drm_dp_dsc_sink_supported_input_bpcs(intel_dp->dsc_dpcd,
2159 						       dsc_bpc);
2160 	for (i = 0; i < num_bpc; i++) {
2161 		if (dsc_max_bpc >= dsc_bpc[i])
2162 			return dsc_bpc[i] * 3;
2163 	}
2164 
2165 	return 0;
2166 }
2167 
2168 #define DSC_SUPPORTED_VERSION_MIN		1
2169 
2170 static int intel_dp_dsc_compute_params(struct intel_encoder *encoder,
2171 				       struct intel_crtc_state *crtc_state)
2172 {
2173 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
2174 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2175 	struct drm_dsc_config *vdsc_cfg = &crtc_state->dsc.config;
2176 	u8 line_buf_depth;
2177 	int ret;
2178 
2179 	ret = intel_dsc_compute_params(encoder, crtc_state);
2180 	if (ret)
2181 		return ret;
2182 
2183 	/*
2184 	 * Slice Height of 8 works for all currently available panels. So start
2185 	 * with that if pic_height is an integral multiple of 8. Eventually add
2186 	 * logic to try multiple slice heights.
2187 	 */
2188 	if (vdsc_cfg->pic_height % 8 == 0)
2189 		vdsc_cfg->slice_height = 8;
2190 	else if (vdsc_cfg->pic_height % 4 == 0)
2191 		vdsc_cfg->slice_height = 4;
2192 	else
2193 		vdsc_cfg->slice_height = 2;
2194 
2195 	vdsc_cfg->dsc_version_major =
2196 		(intel_dp->dsc_dpcd[DP_DSC_REV - DP_DSC_SUPPORT] &
2197 		 DP_DSC_MAJOR_MASK) >> DP_DSC_MAJOR_SHIFT;
2198 	vdsc_cfg->dsc_version_minor =
2199 		min(DSC_SUPPORTED_VERSION_MIN,
2200 		    (intel_dp->dsc_dpcd[DP_DSC_REV - DP_DSC_SUPPORT] &
2201 		     DP_DSC_MINOR_MASK) >> DP_DSC_MINOR_SHIFT);
2202 
2203 	vdsc_cfg->convert_rgb = intel_dp->dsc_dpcd[DP_DSC_DEC_COLOR_FORMAT_CAP - DP_DSC_SUPPORT] &
2204 		DP_DSC_RGB;
2205 
2206 	line_buf_depth = drm_dp_dsc_sink_line_buf_depth(intel_dp->dsc_dpcd);
2207 	if (!line_buf_depth) {
2208 		drm_dbg_kms(&i915->drm,
2209 			    "DSC Sink Line Buffer Depth invalid\n");
2210 		return -EINVAL;
2211 	}
2212 
2213 	if (vdsc_cfg->dsc_version_minor == 2)
2214 		vdsc_cfg->line_buf_depth = (line_buf_depth == DSC_1_2_MAX_LINEBUF_DEPTH_BITS) ?
2215 			DSC_1_2_MAX_LINEBUF_DEPTH_VAL : line_buf_depth;
2216 	else
2217 		vdsc_cfg->line_buf_depth = (line_buf_depth > DSC_1_1_MAX_LINEBUF_DEPTH_BITS) ?
2218 			DSC_1_1_MAX_LINEBUF_DEPTH_BITS : line_buf_depth;
2219 
2220 	vdsc_cfg->block_pred_enable =
2221 		intel_dp->dsc_dpcd[DP_DSC_BLK_PREDICTION_SUPPORT - DP_DSC_SUPPORT] &
2222 		DP_DSC_BLK_PREDICTION_IS_SUPPORTED;
2223 
2224 	return drm_dsc_compute_rc_parameters(vdsc_cfg);
2225 }
2226 
2227 static int intel_dp_dsc_compute_config(struct intel_dp *intel_dp,
2228 				       struct intel_crtc_state *pipe_config,
2229 				       struct drm_connector_state *conn_state,
2230 				       struct link_config_limits *limits)
2231 {
2232 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
2233 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
2234 	const struct drm_display_mode *adjusted_mode =
2235 		&pipe_config->hw.adjusted_mode;
2236 	u8 dsc_max_bpc;
2237 	int pipe_bpp;
2238 	int ret;
2239 
2240 	pipe_config->fec_enable = !intel_dp_is_edp(intel_dp) &&
2241 		intel_dp_supports_fec(intel_dp, pipe_config);
2242 
2243 	if (!intel_dp_supports_dsc(intel_dp, pipe_config))
2244 		return -EINVAL;
2245 
2246 	/* Max DSC Input BPC for ICL is 10 and for TGL+ is 12 */
2247 	if (INTEL_GEN(dev_priv) >= 12)
2248 		dsc_max_bpc = min_t(u8, 12, conn_state->max_requested_bpc);
2249 	else
2250 		dsc_max_bpc = min_t(u8, 10,
2251 				    conn_state->max_requested_bpc);
2252 
2253 	pipe_bpp = intel_dp_dsc_compute_bpp(intel_dp, dsc_max_bpc);
2254 
2255 	/* Min Input BPC for ICL+ is 8 */
2256 	if (pipe_bpp < 8 * 3) {
2257 		drm_dbg_kms(&dev_priv->drm,
2258 			    "No DSC support for less than 8bpc\n");
2259 		return -EINVAL;
2260 	}
2261 
2262 	/*
2263 	 * For now enable DSC for max bpp, max link rate, max lane count.
2264 	 * Optimize this later for the minimum possible link rate/lane count
2265 	 * with DSC enabled for the requested mode.
2266 	 */
2267 	pipe_config->pipe_bpp = pipe_bpp;
2268 	pipe_config->port_clock = intel_dp->common_rates[limits->max_clock];
2269 	pipe_config->lane_count = limits->max_lane_count;
2270 
2271 	if (intel_dp_is_edp(intel_dp)) {
2272 		pipe_config->dsc.compressed_bpp =
2273 			min_t(u16, drm_edp_dsc_sink_output_bpp(intel_dp->dsc_dpcd) >> 4,
2274 			      pipe_config->pipe_bpp);
2275 		pipe_config->dsc.slice_count =
2276 			drm_dp_dsc_sink_max_slice_count(intel_dp->dsc_dpcd,
2277 							true);
2278 	} else {
2279 		u16 dsc_max_output_bpp;
2280 		u8 dsc_dp_slice_count;
2281 
2282 		dsc_max_output_bpp =
2283 			intel_dp_dsc_get_output_bpp(dev_priv,
2284 						    pipe_config->port_clock,
2285 						    pipe_config->lane_count,
2286 						    adjusted_mode->crtc_clock,
2287 						    adjusted_mode->crtc_hdisplay);
2288 		dsc_dp_slice_count =
2289 			intel_dp_dsc_get_slice_count(intel_dp,
2290 						     adjusted_mode->crtc_clock,
2291 						     adjusted_mode->crtc_hdisplay);
2292 		if (!dsc_max_output_bpp || !dsc_dp_slice_count) {
2293 			drm_dbg_kms(&dev_priv->drm,
2294 				    "Compressed BPP/Slice Count not supported\n");
2295 			return -EINVAL;
2296 		}
2297 		pipe_config->dsc.compressed_bpp = min_t(u16,
2298 							       dsc_max_output_bpp >> 4,
2299 							       pipe_config->pipe_bpp);
2300 		pipe_config->dsc.slice_count = dsc_dp_slice_count;
2301 	}
2302 	/*
2303 	 * VDSC engine operates at 1 Pixel per clock, so if peak pixel rate
2304 	 * is greater than the maximum Cdclock and if slice count is even
2305 	 * then we need to use 2 VDSC instances.
2306 	 */
2307 	if (adjusted_mode->crtc_clock > dev_priv->max_cdclk_freq) {
2308 		if (pipe_config->dsc.slice_count > 1) {
2309 			pipe_config->dsc.dsc_split = true;
2310 		} else {
2311 			drm_dbg_kms(&dev_priv->drm,
2312 				    "Cannot split stream to use 2 VDSC instances\n");
2313 			return -EINVAL;
2314 		}
2315 	}
2316 
2317 	ret = intel_dp_dsc_compute_params(&dig_port->base, pipe_config);
2318 	if (ret < 0) {
2319 		drm_dbg_kms(&dev_priv->drm,
2320 			    "Cannot compute valid DSC parameters for Input Bpp = %d "
2321 			    "Compressed BPP = %d\n",
2322 			    pipe_config->pipe_bpp,
2323 			    pipe_config->dsc.compressed_bpp);
2324 		return ret;
2325 	}
2326 
2327 	pipe_config->dsc.compression_enable = true;
2328 	drm_dbg_kms(&dev_priv->drm, "DP DSC computed with Input Bpp = %d "
2329 		    "Compressed Bpp = %d Slice Count = %d\n",
2330 		    pipe_config->pipe_bpp,
2331 		    pipe_config->dsc.compressed_bpp,
2332 		    pipe_config->dsc.slice_count);
2333 
2334 	return 0;
2335 }
2336 
2337 int intel_dp_min_bpp(const struct intel_crtc_state *crtc_state)
2338 {
2339 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_RGB)
2340 		return 6 * 3;
2341 	else
2342 		return 8 * 3;
2343 }
2344 
2345 static int
2346 intel_dp_compute_link_config(struct intel_encoder *encoder,
2347 			     struct intel_crtc_state *pipe_config,
2348 			     struct drm_connector_state *conn_state)
2349 {
2350 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
2351 	const struct drm_display_mode *adjusted_mode =
2352 		&pipe_config->hw.adjusted_mode;
2353 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2354 	struct link_config_limits limits;
2355 	int common_len;
2356 	int ret;
2357 
2358 	common_len = intel_dp_common_len_rate_limit(intel_dp,
2359 						    intel_dp->max_link_rate);
2360 
2361 	/* No common link rates between source and sink */
2362 	drm_WARN_ON(encoder->base.dev, common_len <= 0);
2363 
2364 	limits.min_clock = 0;
2365 	limits.max_clock = common_len - 1;
2366 
2367 	limits.min_lane_count = 1;
2368 	limits.max_lane_count = intel_dp_max_lane_count(intel_dp);
2369 
2370 	limits.min_bpp = intel_dp_min_bpp(pipe_config);
2371 	limits.max_bpp = intel_dp_max_bpp(intel_dp, pipe_config);
2372 
2373 	if (intel_dp_is_edp(intel_dp)) {
2374 		/*
2375 		 * Use the maximum clock and number of lanes the eDP panel
2376 		 * advertizes being capable of. The panels are generally
2377 		 * designed to support only a single clock and lane
2378 		 * configuration, and typically these values correspond to the
2379 		 * native resolution of the panel.
2380 		 */
2381 		limits.min_lane_count = limits.max_lane_count;
2382 		limits.min_clock = limits.max_clock;
2383 	}
2384 
2385 	intel_dp_adjust_compliance_config(intel_dp, pipe_config, &limits);
2386 
2387 	drm_dbg_kms(&i915->drm, "DP link computation with max lane count %i "
2388 		    "max rate %d max bpp %d pixel clock %iKHz\n",
2389 		    limits.max_lane_count,
2390 		    intel_dp->common_rates[limits.max_clock],
2391 		    limits.max_bpp, adjusted_mode->crtc_clock);
2392 
2393 	/*
2394 	 * Optimize for slow and wide. This is the place to add alternative
2395 	 * optimization policy.
2396 	 */
2397 	ret = intel_dp_compute_link_config_wide(intel_dp, pipe_config, &limits);
2398 
2399 	/* enable compression if the mode doesn't fit available BW */
2400 	drm_dbg_kms(&i915->drm, "Force DSC en = %d\n", intel_dp->force_dsc_en);
2401 	if (ret || intel_dp->force_dsc_en) {
2402 		ret = intel_dp_dsc_compute_config(intel_dp, pipe_config,
2403 						  conn_state, &limits);
2404 		if (ret < 0)
2405 			return ret;
2406 	}
2407 
2408 	if (pipe_config->dsc.compression_enable) {
2409 		drm_dbg_kms(&i915->drm,
2410 			    "DP lane count %d clock %d Input bpp %d Compressed bpp %d\n",
2411 			    pipe_config->lane_count, pipe_config->port_clock,
2412 			    pipe_config->pipe_bpp,
2413 			    pipe_config->dsc.compressed_bpp);
2414 
2415 		drm_dbg_kms(&i915->drm,
2416 			    "DP link rate required %i available %i\n",
2417 			    intel_dp_link_required(adjusted_mode->crtc_clock,
2418 						   pipe_config->dsc.compressed_bpp),
2419 			    intel_dp_max_data_rate(pipe_config->port_clock,
2420 						   pipe_config->lane_count));
2421 	} else {
2422 		drm_dbg_kms(&i915->drm, "DP lane count %d clock %d bpp %d\n",
2423 			    pipe_config->lane_count, pipe_config->port_clock,
2424 			    pipe_config->pipe_bpp);
2425 
2426 		drm_dbg_kms(&i915->drm,
2427 			    "DP link rate required %i available %i\n",
2428 			    intel_dp_link_required(adjusted_mode->crtc_clock,
2429 						   pipe_config->pipe_bpp),
2430 			    intel_dp_max_data_rate(pipe_config->port_clock,
2431 						   pipe_config->lane_count));
2432 	}
2433 	return 0;
2434 }
2435 
2436 static int
2437 intel_dp_ycbcr420_config(struct intel_dp *intel_dp,
2438 			 struct intel_crtc_state *crtc_state,
2439 			 const struct drm_connector_state *conn_state)
2440 {
2441 	struct drm_connector *connector = conn_state->connector;
2442 	const struct drm_display_info *info = &connector->display_info;
2443 	const struct drm_display_mode *adjusted_mode =
2444 		&crtc_state->hw.adjusted_mode;
2445 
2446 	if (!connector->ycbcr_420_allowed)
2447 		return 0;
2448 
2449 	if (!drm_mode_is_420_only(info, adjusted_mode))
2450 		return 0;
2451 
2452 	if (intel_dp->dfp.ycbcr_444_to_420) {
2453 		crtc_state->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
2454 		return 0;
2455 	}
2456 
2457 	crtc_state->output_format = INTEL_OUTPUT_FORMAT_YCBCR420;
2458 
2459 	return intel_pch_panel_fitting(crtc_state, conn_state);
2460 }
2461 
2462 bool intel_dp_limited_color_range(const struct intel_crtc_state *crtc_state,
2463 				  const struct drm_connector_state *conn_state)
2464 {
2465 	const struct intel_digital_connector_state *intel_conn_state =
2466 		to_intel_digital_connector_state(conn_state);
2467 	const struct drm_display_mode *adjusted_mode =
2468 		&crtc_state->hw.adjusted_mode;
2469 
2470 	/*
2471 	 * Our YCbCr output is always limited range.
2472 	 * crtc_state->limited_color_range only applies to RGB,
2473 	 * and it must never be set for YCbCr or we risk setting
2474 	 * some conflicting bits in PIPECONF which will mess up
2475 	 * the colors on the monitor.
2476 	 */
2477 	if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
2478 		return false;
2479 
2480 	if (intel_conn_state->broadcast_rgb == INTEL_BROADCAST_RGB_AUTO) {
2481 		/*
2482 		 * See:
2483 		 * CEA-861-E - 5.1 Default Encoding Parameters
2484 		 * VESA DisplayPort Ver.1.2a - 5.1.1.1 Video Colorimetry
2485 		 */
2486 		return crtc_state->pipe_bpp != 18 &&
2487 			drm_default_rgb_quant_range(adjusted_mode) ==
2488 			HDMI_QUANTIZATION_RANGE_LIMITED;
2489 	} else {
2490 		return intel_conn_state->broadcast_rgb ==
2491 			INTEL_BROADCAST_RGB_LIMITED;
2492 	}
2493 }
2494 
2495 static bool intel_dp_port_has_audio(struct drm_i915_private *dev_priv,
2496 				    enum port port)
2497 {
2498 	if (IS_G4X(dev_priv))
2499 		return false;
2500 	if (INTEL_GEN(dev_priv) < 12 && port == PORT_A)
2501 		return false;
2502 
2503 	return true;
2504 }
2505 
2506 static void intel_dp_compute_vsc_colorimetry(const struct intel_crtc_state *crtc_state,
2507 					     const struct drm_connector_state *conn_state,
2508 					     struct drm_dp_vsc_sdp *vsc)
2509 {
2510 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2511 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2512 
2513 	/*
2514 	 * Prepare VSC Header for SU as per DP 1.4 spec, Table 2-118
2515 	 * VSC SDP supporting 3D stereo, PSR2, and Pixel Encoding/
2516 	 * Colorimetry Format indication.
2517 	 */
2518 	vsc->revision = 0x5;
2519 	vsc->length = 0x13;
2520 
2521 	/* DP 1.4a spec, Table 2-120 */
2522 	switch (crtc_state->output_format) {
2523 	case INTEL_OUTPUT_FORMAT_YCBCR444:
2524 		vsc->pixelformat = DP_PIXELFORMAT_YUV444;
2525 		break;
2526 	case INTEL_OUTPUT_FORMAT_YCBCR420:
2527 		vsc->pixelformat = DP_PIXELFORMAT_YUV420;
2528 		break;
2529 	case INTEL_OUTPUT_FORMAT_RGB:
2530 	default:
2531 		vsc->pixelformat = DP_PIXELFORMAT_RGB;
2532 	}
2533 
2534 	switch (conn_state->colorspace) {
2535 	case DRM_MODE_COLORIMETRY_BT709_YCC:
2536 		vsc->colorimetry = DP_COLORIMETRY_BT709_YCC;
2537 		break;
2538 	case DRM_MODE_COLORIMETRY_XVYCC_601:
2539 		vsc->colorimetry = DP_COLORIMETRY_XVYCC_601;
2540 		break;
2541 	case DRM_MODE_COLORIMETRY_XVYCC_709:
2542 		vsc->colorimetry = DP_COLORIMETRY_XVYCC_709;
2543 		break;
2544 	case DRM_MODE_COLORIMETRY_SYCC_601:
2545 		vsc->colorimetry = DP_COLORIMETRY_SYCC_601;
2546 		break;
2547 	case DRM_MODE_COLORIMETRY_OPYCC_601:
2548 		vsc->colorimetry = DP_COLORIMETRY_OPYCC_601;
2549 		break;
2550 	case DRM_MODE_COLORIMETRY_BT2020_CYCC:
2551 		vsc->colorimetry = DP_COLORIMETRY_BT2020_CYCC;
2552 		break;
2553 	case DRM_MODE_COLORIMETRY_BT2020_RGB:
2554 		vsc->colorimetry = DP_COLORIMETRY_BT2020_RGB;
2555 		break;
2556 	case DRM_MODE_COLORIMETRY_BT2020_YCC:
2557 		vsc->colorimetry = DP_COLORIMETRY_BT2020_YCC;
2558 		break;
2559 	case DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65:
2560 	case DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER:
2561 		vsc->colorimetry = DP_COLORIMETRY_DCI_P3_RGB;
2562 		break;
2563 	default:
2564 		/*
2565 		 * RGB->YCBCR color conversion uses the BT.709
2566 		 * color space.
2567 		 */
2568 		if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
2569 			vsc->colorimetry = DP_COLORIMETRY_BT709_YCC;
2570 		else
2571 			vsc->colorimetry = DP_COLORIMETRY_DEFAULT;
2572 		break;
2573 	}
2574 
2575 	vsc->bpc = crtc_state->pipe_bpp / 3;
2576 
2577 	/* only RGB pixelformat supports 6 bpc */
2578 	drm_WARN_ON(&dev_priv->drm,
2579 		    vsc->bpc == 6 && vsc->pixelformat != DP_PIXELFORMAT_RGB);
2580 
2581 	/* all YCbCr are always limited range */
2582 	vsc->dynamic_range = DP_DYNAMIC_RANGE_CTA;
2583 	vsc->content_type = DP_CONTENT_TYPE_NOT_DEFINED;
2584 }
2585 
2586 static void intel_dp_compute_vsc_sdp(struct intel_dp *intel_dp,
2587 				     struct intel_crtc_state *crtc_state,
2588 				     const struct drm_connector_state *conn_state)
2589 {
2590 	struct drm_dp_vsc_sdp *vsc = &crtc_state->infoframes.vsc;
2591 
2592 	/* When a crtc state has PSR, VSC SDP will be handled by PSR routine */
2593 	if (crtc_state->has_psr)
2594 		return;
2595 
2596 	if (!intel_dp_needs_vsc_sdp(crtc_state, conn_state))
2597 		return;
2598 
2599 	crtc_state->infoframes.enable |= intel_hdmi_infoframe_enable(DP_SDP_VSC);
2600 	vsc->sdp_type = DP_SDP_VSC;
2601 	intel_dp_compute_vsc_colorimetry(crtc_state, conn_state,
2602 					 &crtc_state->infoframes.vsc);
2603 }
2604 
2605 void intel_dp_compute_psr_vsc_sdp(struct intel_dp *intel_dp,
2606 				  const struct intel_crtc_state *crtc_state,
2607 				  const struct drm_connector_state *conn_state,
2608 				  struct drm_dp_vsc_sdp *vsc)
2609 {
2610 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
2611 
2612 	vsc->sdp_type = DP_SDP_VSC;
2613 
2614 	if (dev_priv->psr.psr2_enabled) {
2615 		if (dev_priv->psr.colorimetry_support &&
2616 		    intel_dp_needs_vsc_sdp(crtc_state, conn_state)) {
2617 			/* [PSR2, +Colorimetry] */
2618 			intel_dp_compute_vsc_colorimetry(crtc_state, conn_state,
2619 							 vsc);
2620 		} else {
2621 			/*
2622 			 * [PSR2, -Colorimetry]
2623 			 * Prepare VSC Header for SU as per eDP 1.4 spec, Table 6-11
2624 			 * 3D stereo + PSR/PSR2 + Y-coordinate.
2625 			 */
2626 			vsc->revision = 0x4;
2627 			vsc->length = 0xe;
2628 		}
2629 	} else {
2630 		/*
2631 		 * [PSR1]
2632 		 * Prepare VSC Header for SU as per DP 1.4 spec, Table 2-118
2633 		 * VSC SDP supporting 3D stereo + PSR (applies to eDP v1.3 or
2634 		 * higher).
2635 		 */
2636 		vsc->revision = 0x2;
2637 		vsc->length = 0x8;
2638 	}
2639 }
2640 
2641 static void
2642 intel_dp_compute_hdr_metadata_infoframe_sdp(struct intel_dp *intel_dp,
2643 					    struct intel_crtc_state *crtc_state,
2644 					    const struct drm_connector_state *conn_state)
2645 {
2646 	int ret;
2647 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
2648 	struct hdmi_drm_infoframe *drm_infoframe = &crtc_state->infoframes.drm.drm;
2649 
2650 	if (!conn_state->hdr_output_metadata)
2651 		return;
2652 
2653 	ret = drm_hdmi_infoframe_set_hdr_metadata(drm_infoframe, conn_state);
2654 
2655 	if (ret) {
2656 		drm_dbg_kms(&dev_priv->drm, "couldn't set HDR metadata in infoframe\n");
2657 		return;
2658 	}
2659 
2660 	crtc_state->infoframes.enable |=
2661 		intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GAMUT_METADATA);
2662 }
2663 
2664 static void
2665 intel_dp_drrs_compute_config(struct intel_dp *intel_dp,
2666 			     struct intel_crtc_state *pipe_config,
2667 			     int output_bpp, bool constant_n)
2668 {
2669 	struct intel_connector *intel_connector = intel_dp->attached_connector;
2670 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
2671 
2672 	/*
2673 	 * DRRS and PSR can't be enable together, so giving preference to PSR
2674 	 * as it allows more power-savings by complete shutting down display,
2675 	 * so to guarantee this, intel_dp_drrs_compute_config() must be called
2676 	 * after intel_psr_compute_config().
2677 	 */
2678 	if (pipe_config->has_psr)
2679 		return;
2680 
2681 	if (!intel_connector->panel.downclock_mode ||
2682 	    dev_priv->drrs.type != SEAMLESS_DRRS_SUPPORT)
2683 		return;
2684 
2685 	pipe_config->has_drrs = true;
2686 	intel_link_compute_m_n(output_bpp, pipe_config->lane_count,
2687 			       intel_connector->panel.downclock_mode->clock,
2688 			       pipe_config->port_clock, &pipe_config->dp_m2_n2,
2689 			       constant_n, pipe_config->fec_enable);
2690 }
2691 
2692 int
2693 intel_dp_compute_config(struct intel_encoder *encoder,
2694 			struct intel_crtc_state *pipe_config,
2695 			struct drm_connector_state *conn_state)
2696 {
2697 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2698 	struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
2699 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2700 	struct intel_lspcon *lspcon = enc_to_intel_lspcon(encoder);
2701 	enum port port = encoder->port;
2702 	struct intel_connector *intel_connector = intel_dp->attached_connector;
2703 	struct intel_digital_connector_state *intel_conn_state =
2704 		to_intel_digital_connector_state(conn_state);
2705 	bool constant_n = drm_dp_has_quirk(&intel_dp->desc, 0,
2706 					   DP_DPCD_QUIRK_CONSTANT_N);
2707 	int ret = 0, output_bpp;
2708 
2709 	if (HAS_PCH_SPLIT(dev_priv) && !HAS_DDI(dev_priv) && port != PORT_A)
2710 		pipe_config->has_pch_encoder = true;
2711 
2712 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
2713 
2714 	if (lspcon->active)
2715 		lspcon_ycbcr420_config(&intel_connector->base, pipe_config);
2716 	else
2717 		ret = intel_dp_ycbcr420_config(intel_dp, pipe_config,
2718 					       conn_state);
2719 	if (ret)
2720 		return ret;
2721 
2722 	if (!intel_dp_port_has_audio(dev_priv, port))
2723 		pipe_config->has_audio = false;
2724 	else if (intel_conn_state->force_audio == HDMI_AUDIO_AUTO)
2725 		pipe_config->has_audio = intel_dp->has_audio;
2726 	else
2727 		pipe_config->has_audio = intel_conn_state->force_audio == HDMI_AUDIO_ON;
2728 
2729 	if (intel_dp_is_edp(intel_dp) && intel_connector->panel.fixed_mode) {
2730 		intel_fixed_panel_mode(intel_connector->panel.fixed_mode,
2731 				       adjusted_mode);
2732 
2733 		if (HAS_GMCH(dev_priv))
2734 			ret = intel_gmch_panel_fitting(pipe_config, conn_state);
2735 		else
2736 			ret = intel_pch_panel_fitting(pipe_config, conn_state);
2737 		if (ret)
2738 			return ret;
2739 	}
2740 
2741 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
2742 		return -EINVAL;
2743 
2744 	if (HAS_GMCH(dev_priv) &&
2745 	    adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
2746 		return -EINVAL;
2747 
2748 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
2749 		return -EINVAL;
2750 
2751 	if (intel_dp_hdisplay_bad(dev_priv, adjusted_mode->crtc_hdisplay))
2752 		return -EINVAL;
2753 
2754 	ret = intel_dp_compute_link_config(encoder, pipe_config, conn_state);
2755 	if (ret < 0)
2756 		return ret;
2757 
2758 	pipe_config->limited_color_range =
2759 		intel_dp_limited_color_range(pipe_config, conn_state);
2760 
2761 	if (pipe_config->dsc.compression_enable)
2762 		output_bpp = pipe_config->dsc.compressed_bpp;
2763 	else
2764 		output_bpp = intel_dp_output_bpp(pipe_config, pipe_config->pipe_bpp);
2765 
2766 	intel_link_compute_m_n(output_bpp,
2767 			       pipe_config->lane_count,
2768 			       adjusted_mode->crtc_clock,
2769 			       pipe_config->port_clock,
2770 			       &pipe_config->dp_m_n,
2771 			       constant_n, pipe_config->fec_enable);
2772 
2773 	if (!HAS_DDI(dev_priv))
2774 		intel_dp_set_clock(encoder, pipe_config);
2775 
2776 	intel_psr_compute_config(intel_dp, pipe_config);
2777 	intel_dp_drrs_compute_config(intel_dp, pipe_config, output_bpp,
2778 				     constant_n);
2779 	intel_dp_compute_vsc_sdp(intel_dp, pipe_config, conn_state);
2780 	intel_dp_compute_hdr_metadata_infoframe_sdp(intel_dp, pipe_config, conn_state);
2781 
2782 	return 0;
2783 }
2784 
2785 void intel_dp_set_link_params(struct intel_dp *intel_dp,
2786 			      int link_rate, u8 lane_count,
2787 			      bool link_mst)
2788 {
2789 	intel_dp->link_trained = false;
2790 	intel_dp->link_rate = link_rate;
2791 	intel_dp->lane_count = lane_count;
2792 	intel_dp->link_mst = link_mst;
2793 }
2794 
2795 static void intel_dp_prepare(struct intel_encoder *encoder,
2796 			     const struct intel_crtc_state *pipe_config)
2797 {
2798 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2799 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2800 	enum port port = encoder->port;
2801 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
2802 	const struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
2803 
2804 	intel_dp_set_link_params(intel_dp, pipe_config->port_clock,
2805 				 pipe_config->lane_count,
2806 				 intel_crtc_has_type(pipe_config,
2807 						     INTEL_OUTPUT_DP_MST));
2808 
2809 	/*
2810 	 * There are four kinds of DP registers:
2811 	 *
2812 	 * 	IBX PCH
2813 	 * 	SNB CPU
2814 	 *	IVB CPU
2815 	 * 	CPT PCH
2816 	 *
2817 	 * IBX PCH and CPU are the same for almost everything,
2818 	 * except that the CPU DP PLL is configured in this
2819 	 * register
2820 	 *
2821 	 * CPT PCH is quite different, having many bits moved
2822 	 * to the TRANS_DP_CTL register instead. That
2823 	 * configuration happens (oddly) in ilk_pch_enable
2824 	 */
2825 
2826 	/* Preserve the BIOS-computed detected bit. This is
2827 	 * supposed to be read-only.
2828 	 */
2829 	intel_dp->DP = intel_de_read(dev_priv, intel_dp->output_reg) & DP_DETECTED;
2830 
2831 	/* Handle DP bits in common between all three register formats */
2832 	intel_dp->DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
2833 	intel_dp->DP |= DP_PORT_WIDTH(pipe_config->lane_count);
2834 
2835 	/* Split out the IBX/CPU vs CPT settings */
2836 
2837 	if (IS_IVYBRIDGE(dev_priv) && port == PORT_A) {
2838 		if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
2839 			intel_dp->DP |= DP_SYNC_HS_HIGH;
2840 		if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
2841 			intel_dp->DP |= DP_SYNC_VS_HIGH;
2842 		intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
2843 
2844 		if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
2845 			intel_dp->DP |= DP_ENHANCED_FRAMING;
2846 
2847 		intel_dp->DP |= DP_PIPE_SEL_IVB(crtc->pipe);
2848 	} else if (HAS_PCH_CPT(dev_priv) && port != PORT_A) {
2849 		u32 trans_dp;
2850 
2851 		intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
2852 
2853 		trans_dp = intel_de_read(dev_priv, TRANS_DP_CTL(crtc->pipe));
2854 		if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
2855 			trans_dp |= TRANS_DP_ENH_FRAMING;
2856 		else
2857 			trans_dp &= ~TRANS_DP_ENH_FRAMING;
2858 		intel_de_write(dev_priv, TRANS_DP_CTL(crtc->pipe), trans_dp);
2859 	} else {
2860 		if (IS_G4X(dev_priv) && pipe_config->limited_color_range)
2861 			intel_dp->DP |= DP_COLOR_RANGE_16_235;
2862 
2863 		if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
2864 			intel_dp->DP |= DP_SYNC_HS_HIGH;
2865 		if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
2866 			intel_dp->DP |= DP_SYNC_VS_HIGH;
2867 		intel_dp->DP |= DP_LINK_TRAIN_OFF;
2868 
2869 		if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
2870 			intel_dp->DP |= DP_ENHANCED_FRAMING;
2871 
2872 		if (IS_CHERRYVIEW(dev_priv))
2873 			intel_dp->DP |= DP_PIPE_SEL_CHV(crtc->pipe);
2874 		else
2875 			intel_dp->DP |= DP_PIPE_SEL(crtc->pipe);
2876 	}
2877 }
2878 
2879 #define IDLE_ON_MASK		(PP_ON | PP_SEQUENCE_MASK | 0                     | PP_SEQUENCE_STATE_MASK)
2880 #define IDLE_ON_VALUE   	(PP_ON | PP_SEQUENCE_NONE | 0                     | PP_SEQUENCE_STATE_ON_IDLE)
2881 
2882 #define IDLE_OFF_MASK		(PP_ON | PP_SEQUENCE_MASK | 0                     | 0)
2883 #define IDLE_OFF_VALUE		(0     | PP_SEQUENCE_NONE | 0                     | 0)
2884 
2885 #define IDLE_CYCLE_MASK		(PP_ON | PP_SEQUENCE_MASK | PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK)
2886 #define IDLE_CYCLE_VALUE	(0     | PP_SEQUENCE_NONE | 0                     | PP_SEQUENCE_STATE_OFF_IDLE)
2887 
2888 static void intel_pps_verify_state(struct intel_dp *intel_dp);
2889 
2890 static void wait_panel_status(struct intel_dp *intel_dp,
2891 				       u32 mask,
2892 				       u32 value)
2893 {
2894 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
2895 	i915_reg_t pp_stat_reg, pp_ctrl_reg;
2896 
2897 	lockdep_assert_held(&dev_priv->pps_mutex);
2898 
2899 	intel_pps_verify_state(intel_dp);
2900 
2901 	pp_stat_reg = _pp_stat_reg(intel_dp);
2902 	pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
2903 
2904 	drm_dbg_kms(&dev_priv->drm,
2905 		    "mask %08x value %08x status %08x control %08x\n",
2906 		    mask, value,
2907 		    intel_de_read(dev_priv, pp_stat_reg),
2908 		    intel_de_read(dev_priv, pp_ctrl_reg));
2909 
2910 	if (intel_de_wait_for_register(dev_priv, pp_stat_reg,
2911 				       mask, value, 5000))
2912 		drm_err(&dev_priv->drm,
2913 			"Panel status timeout: status %08x control %08x\n",
2914 			intel_de_read(dev_priv, pp_stat_reg),
2915 			intel_de_read(dev_priv, pp_ctrl_reg));
2916 
2917 	drm_dbg_kms(&dev_priv->drm, "Wait complete\n");
2918 }
2919 
2920 static void wait_panel_on(struct intel_dp *intel_dp)
2921 {
2922 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2923 
2924 	drm_dbg_kms(&i915->drm, "Wait for panel power on\n");
2925 	wait_panel_status(intel_dp, IDLE_ON_MASK, IDLE_ON_VALUE);
2926 }
2927 
2928 static void wait_panel_off(struct intel_dp *intel_dp)
2929 {
2930 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2931 
2932 	drm_dbg_kms(&i915->drm, "Wait for panel power off time\n");
2933 	wait_panel_status(intel_dp, IDLE_OFF_MASK, IDLE_OFF_VALUE);
2934 }
2935 
2936 static void wait_panel_power_cycle(struct intel_dp *intel_dp)
2937 {
2938 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2939 	ktime_t panel_power_on_time;
2940 	s64 panel_power_off_duration;
2941 
2942 	drm_dbg_kms(&i915->drm, "Wait for panel power cycle\n");
2943 
2944 	/* take the difference of currrent time and panel power off time
2945 	 * and then make panel wait for t11_t12 if needed. */
2946 	panel_power_on_time = ktime_get_boottime();
2947 	panel_power_off_duration = ktime_ms_delta(panel_power_on_time, intel_dp->panel_power_off_time);
2948 
2949 	/* When we disable the VDD override bit last we have to do the manual
2950 	 * wait. */
2951 	if (panel_power_off_duration < (s64)intel_dp->panel_power_cycle_delay)
2952 		wait_remaining_ms_from_jiffies(jiffies,
2953 				       intel_dp->panel_power_cycle_delay - panel_power_off_duration);
2954 
2955 	wait_panel_status(intel_dp, IDLE_CYCLE_MASK, IDLE_CYCLE_VALUE);
2956 }
2957 
2958 static void wait_backlight_on(struct intel_dp *intel_dp)
2959 {
2960 	wait_remaining_ms_from_jiffies(intel_dp->last_power_on,
2961 				       intel_dp->backlight_on_delay);
2962 }
2963 
2964 static void edp_wait_backlight_off(struct intel_dp *intel_dp)
2965 {
2966 	wait_remaining_ms_from_jiffies(intel_dp->last_backlight_off,
2967 				       intel_dp->backlight_off_delay);
2968 }
2969 
2970 /* Read the current pp_control value, unlocking the register if it
2971  * is locked
2972  */
2973 
2974 static  u32 ilk_get_pp_control(struct intel_dp *intel_dp)
2975 {
2976 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
2977 	u32 control;
2978 
2979 	lockdep_assert_held(&dev_priv->pps_mutex);
2980 
2981 	control = intel_de_read(dev_priv, _pp_ctrl_reg(intel_dp));
2982 	if (drm_WARN_ON(&dev_priv->drm, !HAS_DDI(dev_priv) &&
2983 			(control & PANEL_UNLOCK_MASK) != PANEL_UNLOCK_REGS)) {
2984 		control &= ~PANEL_UNLOCK_MASK;
2985 		control |= PANEL_UNLOCK_REGS;
2986 	}
2987 	return control;
2988 }
2989 
2990 /*
2991  * Must be paired with edp_panel_vdd_off().
2992  * Must hold pps_mutex around the whole on/off sequence.
2993  * Can be nested with intel_edp_panel_vdd_{on,off}() calls.
2994  */
2995 static bool edp_panel_vdd_on(struct intel_dp *intel_dp)
2996 {
2997 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
2998 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
2999 	u32 pp;
3000 	i915_reg_t pp_stat_reg, pp_ctrl_reg;
3001 	bool need_to_disable = !intel_dp->want_panel_vdd;
3002 
3003 	lockdep_assert_held(&dev_priv->pps_mutex);
3004 
3005 	if (!intel_dp_is_edp(intel_dp))
3006 		return false;
3007 
3008 	cancel_delayed_work(&intel_dp->panel_vdd_work);
3009 	intel_dp->want_panel_vdd = true;
3010 
3011 	if (edp_have_panel_vdd(intel_dp))
3012 		return need_to_disable;
3013 
3014 	intel_display_power_get(dev_priv,
3015 				intel_aux_power_domain(dig_port));
3016 
3017 	drm_dbg_kms(&dev_priv->drm, "Turning [ENCODER:%d:%s] VDD on\n",
3018 		    dig_port->base.base.base.id,
3019 		    dig_port->base.base.name);
3020 
3021 	if (!edp_have_panel_power(intel_dp))
3022 		wait_panel_power_cycle(intel_dp);
3023 
3024 	pp = ilk_get_pp_control(intel_dp);
3025 	pp |= EDP_FORCE_VDD;
3026 
3027 	pp_stat_reg = _pp_stat_reg(intel_dp);
3028 	pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
3029 
3030 	intel_de_write(dev_priv, pp_ctrl_reg, pp);
3031 	intel_de_posting_read(dev_priv, pp_ctrl_reg);
3032 	drm_dbg_kms(&dev_priv->drm, "PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
3033 		    intel_de_read(dev_priv, pp_stat_reg),
3034 		    intel_de_read(dev_priv, pp_ctrl_reg));
3035 	/*
3036 	 * If the panel wasn't on, delay before accessing aux channel
3037 	 */
3038 	if (!edp_have_panel_power(intel_dp)) {
3039 		drm_dbg_kms(&dev_priv->drm,
3040 			    "[ENCODER:%d:%s] panel power wasn't enabled\n",
3041 			    dig_port->base.base.base.id,
3042 			    dig_port->base.base.name);
3043 		drm_msleep(intel_dp->panel_power_up_delay);
3044 	}
3045 
3046 	return need_to_disable;
3047 }
3048 
3049 /*
3050  * Must be paired with intel_edp_panel_vdd_off() or
3051  * intel_edp_panel_off().
3052  * Nested calls to these functions are not allowed since
3053  * we drop the lock. Caller must use some higher level
3054  * locking to prevent nested calls from other threads.
3055  */
3056 void intel_edp_panel_vdd_on(struct intel_dp *intel_dp)
3057 {
3058 	intel_wakeref_t wakeref;
3059 	bool vdd;
3060 
3061 	if (!intel_dp_is_edp(intel_dp))
3062 		return;
3063 
3064 	vdd = false;
3065 	with_pps_lock(intel_dp, wakeref)
3066 		vdd = edp_panel_vdd_on(intel_dp);
3067 	I915_STATE_WARN(!vdd, "[ENCODER:%d:%s] VDD already requested on\n",
3068 			dp_to_dig_port(intel_dp)->base.base.base.id,
3069 			dp_to_dig_port(intel_dp)->base.base.name);
3070 }
3071 
3072 static void edp_panel_vdd_off_sync(struct intel_dp *intel_dp)
3073 {
3074 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
3075 	struct intel_digital_port *dig_port =
3076 		dp_to_dig_port(intel_dp);
3077 	u32 pp;
3078 	i915_reg_t pp_stat_reg, pp_ctrl_reg;
3079 
3080 	lockdep_assert_held(&dev_priv->pps_mutex);
3081 
3082 	drm_WARN_ON(&dev_priv->drm, intel_dp->want_panel_vdd);
3083 
3084 	if (!edp_have_panel_vdd(intel_dp))
3085 		return;
3086 
3087 	drm_dbg_kms(&dev_priv->drm, "Turning [ENCODER:%d:%s] VDD off\n",
3088 		    dig_port->base.base.base.id,
3089 		    dig_port->base.base.name);
3090 
3091 	pp = ilk_get_pp_control(intel_dp);
3092 	pp &= ~EDP_FORCE_VDD;
3093 
3094 	pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
3095 	pp_stat_reg = _pp_stat_reg(intel_dp);
3096 
3097 	intel_de_write(dev_priv, pp_ctrl_reg, pp);
3098 	intel_de_posting_read(dev_priv, pp_ctrl_reg);
3099 
3100 	/* Make sure sequencer is idle before allowing subsequent activity */
3101 	drm_dbg_kms(&dev_priv->drm, "PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
3102 		    intel_de_read(dev_priv, pp_stat_reg),
3103 		    intel_de_read(dev_priv, pp_ctrl_reg));
3104 
3105 	if ((pp & PANEL_POWER_ON) == 0)
3106 		intel_dp->panel_power_off_time = ktime_get_boottime();
3107 
3108 	intel_display_power_put_unchecked(dev_priv,
3109 					  intel_aux_power_domain(dig_port));
3110 }
3111 
3112 static void edp_panel_vdd_work(struct work_struct *__work)
3113 {
3114 	struct intel_dp *intel_dp =
3115 		container_of(to_delayed_work(__work),
3116 			     struct intel_dp, panel_vdd_work);
3117 	intel_wakeref_t wakeref;
3118 
3119 	with_pps_lock(intel_dp, wakeref) {
3120 		if (!intel_dp->want_panel_vdd)
3121 			edp_panel_vdd_off_sync(intel_dp);
3122 	}
3123 }
3124 
3125 static void edp_panel_vdd_schedule_off(struct intel_dp *intel_dp)
3126 {
3127 	unsigned long delay;
3128 
3129 	/*
3130 	 * Queue the timer to fire a long time from now (relative to the power
3131 	 * down delay) to keep the panel power up across a sequence of
3132 	 * operations.
3133 	 */
3134 	delay = msecs_to_jiffies(intel_dp->panel_power_cycle_delay * 5);
3135 	schedule_delayed_work(&intel_dp->panel_vdd_work, delay);
3136 }
3137 
3138 /*
3139  * Must be paired with edp_panel_vdd_on().
3140  * Must hold pps_mutex around the whole on/off sequence.
3141  * Can be nested with intel_edp_panel_vdd_{on,off}() calls.
3142  */
3143 static void edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync)
3144 {
3145 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
3146 
3147 	lockdep_assert_held(&dev_priv->pps_mutex);
3148 
3149 	if (!intel_dp_is_edp(intel_dp))
3150 		return;
3151 
3152 	I915_STATE_WARN(!intel_dp->want_panel_vdd, "[ENCODER:%d:%s] VDD not forced on",
3153 			dp_to_dig_port(intel_dp)->base.base.base.id,
3154 			dp_to_dig_port(intel_dp)->base.base.name);
3155 
3156 	intel_dp->want_panel_vdd = false;
3157 
3158 	if (sync)
3159 		edp_panel_vdd_off_sync(intel_dp);
3160 	else
3161 		edp_panel_vdd_schedule_off(intel_dp);
3162 }
3163 
3164 static void edp_panel_on(struct intel_dp *intel_dp)
3165 {
3166 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
3167 	u32 pp;
3168 	i915_reg_t pp_ctrl_reg;
3169 
3170 	lockdep_assert_held(&dev_priv->pps_mutex);
3171 
3172 	if (!intel_dp_is_edp(intel_dp))
3173 		return;
3174 
3175 	drm_dbg_kms(&dev_priv->drm, "Turn [ENCODER:%d:%s] panel power on\n",
3176 		    dp_to_dig_port(intel_dp)->base.base.base.id,
3177 		    dp_to_dig_port(intel_dp)->base.base.name);
3178 
3179 	if (drm_WARN(&dev_priv->drm, edp_have_panel_power(intel_dp),
3180 		     "[ENCODER:%d:%s] panel power already on\n",
3181 		     dp_to_dig_port(intel_dp)->base.base.base.id,
3182 		     dp_to_dig_port(intel_dp)->base.base.name))
3183 		return;
3184 
3185 	wait_panel_power_cycle(intel_dp);
3186 
3187 	pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
3188 	pp = ilk_get_pp_control(intel_dp);
3189 	if (IS_GEN(dev_priv, 5)) {
3190 		/* ILK workaround: disable reset around power sequence */
3191 		pp &= ~PANEL_POWER_RESET;
3192 		intel_de_write(dev_priv, pp_ctrl_reg, pp);
3193 		intel_de_posting_read(dev_priv, pp_ctrl_reg);
3194 	}
3195 
3196 	pp |= PANEL_POWER_ON;
3197 	if (!IS_GEN(dev_priv, 5))
3198 		pp |= PANEL_POWER_RESET;
3199 
3200 	intel_de_write(dev_priv, pp_ctrl_reg, pp);
3201 	intel_de_posting_read(dev_priv, pp_ctrl_reg);
3202 
3203 	wait_panel_on(intel_dp);
3204 	intel_dp->last_power_on = jiffies;
3205 
3206 	if (IS_GEN(dev_priv, 5)) {
3207 		pp |= PANEL_POWER_RESET; /* restore panel reset bit */
3208 		intel_de_write(dev_priv, pp_ctrl_reg, pp);
3209 		intel_de_posting_read(dev_priv, pp_ctrl_reg);
3210 	}
3211 }
3212 
3213 void intel_edp_panel_on(struct intel_dp *intel_dp)
3214 {
3215 	intel_wakeref_t wakeref;
3216 
3217 	if (!intel_dp_is_edp(intel_dp))
3218 		return;
3219 
3220 	with_pps_lock(intel_dp, wakeref)
3221 		edp_panel_on(intel_dp);
3222 }
3223 
3224 
3225 static void edp_panel_off(struct intel_dp *intel_dp)
3226 {
3227 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
3228 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
3229 	u32 pp;
3230 	i915_reg_t pp_ctrl_reg;
3231 
3232 	lockdep_assert_held(&dev_priv->pps_mutex);
3233 
3234 	if (!intel_dp_is_edp(intel_dp))
3235 		return;
3236 
3237 	drm_dbg_kms(&dev_priv->drm, "Turn [ENCODER:%d:%s] panel power off\n",
3238 		    dig_port->base.base.base.id, dig_port->base.base.name);
3239 
3240 	drm_WARN(&dev_priv->drm, !intel_dp->want_panel_vdd,
3241 		 "Need [ENCODER:%d:%s] VDD to turn off panel\n",
3242 		 dig_port->base.base.base.id, dig_port->base.base.name);
3243 
3244 	pp = ilk_get_pp_control(intel_dp);
3245 	/* We need to switch off panel power _and_ force vdd, for otherwise some
3246 	 * panels get very unhappy and cease to work. */
3247 	pp &= ~(PANEL_POWER_ON | PANEL_POWER_RESET | EDP_FORCE_VDD |
3248 		EDP_BLC_ENABLE);
3249 
3250 	pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
3251 
3252 	intel_dp->want_panel_vdd = false;
3253 
3254 	intel_de_write(dev_priv, pp_ctrl_reg, pp);
3255 	intel_de_posting_read(dev_priv, pp_ctrl_reg);
3256 
3257 	wait_panel_off(intel_dp);
3258 	intel_dp->panel_power_off_time = ktime_get_boottime();
3259 
3260 	/* We got a reference when we enabled the VDD. */
3261 	intel_display_power_put_unchecked(dev_priv, intel_aux_power_domain(dig_port));
3262 }
3263 
3264 void intel_edp_panel_off(struct intel_dp *intel_dp)
3265 {
3266 	intel_wakeref_t wakeref;
3267 
3268 	if (!intel_dp_is_edp(intel_dp))
3269 		return;
3270 
3271 	with_pps_lock(intel_dp, wakeref)
3272 		edp_panel_off(intel_dp);
3273 }
3274 
3275 /* Enable backlight in the panel power control. */
3276 static void _intel_edp_backlight_on(struct intel_dp *intel_dp)
3277 {
3278 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
3279 	intel_wakeref_t wakeref;
3280 
3281 	/*
3282 	 * If we enable the backlight right away following a panel power
3283 	 * on, we may see slight flicker as the panel syncs with the eDP
3284 	 * link.  So delay a bit to make sure the image is solid before
3285 	 * allowing it to appear.
3286 	 */
3287 	wait_backlight_on(intel_dp);
3288 
3289 	with_pps_lock(intel_dp, wakeref) {
3290 		i915_reg_t pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
3291 		u32 pp;
3292 
3293 		pp = ilk_get_pp_control(intel_dp);
3294 		pp |= EDP_BLC_ENABLE;
3295 
3296 		intel_de_write(dev_priv, pp_ctrl_reg, pp);
3297 		intel_de_posting_read(dev_priv, pp_ctrl_reg);
3298 	}
3299 }
3300 
3301 /* Enable backlight PWM and backlight PP control. */
3302 void intel_edp_backlight_on(const struct intel_crtc_state *crtc_state,
3303 			    const struct drm_connector_state *conn_state)
3304 {
3305 	struct intel_dp *intel_dp = enc_to_intel_dp(to_intel_encoder(conn_state->best_encoder));
3306 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3307 
3308 	if (!intel_dp_is_edp(intel_dp))
3309 		return;
3310 
3311 	drm_dbg_kms(&i915->drm, "\n");
3312 
3313 	intel_panel_enable_backlight(crtc_state, conn_state);
3314 	_intel_edp_backlight_on(intel_dp);
3315 }
3316 
3317 /* Disable backlight in the panel power control. */
3318 static void _intel_edp_backlight_off(struct intel_dp *intel_dp)
3319 {
3320 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
3321 	intel_wakeref_t wakeref;
3322 
3323 	if (!intel_dp_is_edp(intel_dp))
3324 		return;
3325 
3326 	with_pps_lock(intel_dp, wakeref) {
3327 		i915_reg_t pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
3328 		u32 pp;
3329 
3330 		pp = ilk_get_pp_control(intel_dp);
3331 		pp &= ~EDP_BLC_ENABLE;
3332 
3333 		intel_de_write(dev_priv, pp_ctrl_reg, pp);
3334 		intel_de_posting_read(dev_priv, pp_ctrl_reg);
3335 	}
3336 
3337 	intel_dp->last_backlight_off = jiffies;
3338 	edp_wait_backlight_off(intel_dp);
3339 }
3340 
3341 /* Disable backlight PP control and backlight PWM. */
3342 void intel_edp_backlight_off(const struct drm_connector_state *old_conn_state)
3343 {
3344 	struct intel_dp *intel_dp = enc_to_intel_dp(to_intel_encoder(old_conn_state->best_encoder));
3345 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3346 
3347 	if (!intel_dp_is_edp(intel_dp))
3348 		return;
3349 
3350 	drm_dbg_kms(&i915->drm, "\n");
3351 
3352 	_intel_edp_backlight_off(intel_dp);
3353 	intel_panel_disable_backlight(old_conn_state);
3354 }
3355 
3356 /*
3357  * Hook for controlling the panel power control backlight through the bl_power
3358  * sysfs attribute. Take care to handle multiple calls.
3359  */
3360 static void intel_edp_backlight_power(struct intel_connector *connector,
3361 				      bool enable)
3362 {
3363 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
3364 	struct intel_dp *intel_dp = intel_attached_dp(connector);
3365 	intel_wakeref_t wakeref;
3366 	bool is_enabled;
3367 
3368 	is_enabled = false;
3369 	with_pps_lock(intel_dp, wakeref)
3370 		is_enabled = ilk_get_pp_control(intel_dp) & EDP_BLC_ENABLE;
3371 	if (is_enabled == enable)
3372 		return;
3373 
3374 	drm_dbg_kms(&i915->drm, "panel power control backlight %s\n",
3375 		    enable ? "enable" : "disable");
3376 
3377 	if (enable)
3378 		_intel_edp_backlight_on(intel_dp);
3379 	else
3380 		_intel_edp_backlight_off(intel_dp);
3381 }
3382 
3383 static void assert_dp_port(struct intel_dp *intel_dp, bool state)
3384 {
3385 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
3386 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
3387 	bool cur_state = intel_de_read(dev_priv, intel_dp->output_reg) & DP_PORT_EN;
3388 
3389 	I915_STATE_WARN(cur_state != state,
3390 			"[ENCODER:%d:%s] state assertion failure (expected %s, current %s)\n",
3391 			dig_port->base.base.base.id, dig_port->base.base.name,
3392 			onoff(state), onoff(cur_state));
3393 }
3394 #define assert_dp_port_disabled(d) assert_dp_port((d), false)
3395 
3396 static void assert_edp_pll(struct drm_i915_private *dev_priv, bool state)
3397 {
3398 	bool cur_state = intel_de_read(dev_priv, DP_A) & DP_PLL_ENABLE;
3399 
3400 	I915_STATE_WARN(cur_state != state,
3401 			"eDP PLL state assertion failure (expected %s, current %s)\n",
3402 			onoff(state), onoff(cur_state));
3403 }
3404 #define assert_edp_pll_enabled(d) assert_edp_pll((d), true)
3405 #define assert_edp_pll_disabled(d) assert_edp_pll((d), false)
3406 
3407 static void ilk_edp_pll_on(struct intel_dp *intel_dp,
3408 			   const struct intel_crtc_state *pipe_config)
3409 {
3410 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
3411 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3412 
3413 	assert_pipe_disabled(dev_priv, pipe_config->cpu_transcoder);
3414 	assert_dp_port_disabled(intel_dp);
3415 	assert_edp_pll_disabled(dev_priv);
3416 
3417 	drm_dbg_kms(&dev_priv->drm, "enabling eDP PLL for clock %d\n",
3418 		    pipe_config->port_clock);
3419 
3420 	intel_dp->DP &= ~DP_PLL_FREQ_MASK;
3421 
3422 	if (pipe_config->port_clock == 162000)
3423 		intel_dp->DP |= DP_PLL_FREQ_162MHZ;
3424 	else
3425 		intel_dp->DP |= DP_PLL_FREQ_270MHZ;
3426 
3427 	intel_de_write(dev_priv, DP_A, intel_dp->DP);
3428 	intel_de_posting_read(dev_priv, DP_A);
3429 	udelay(500);
3430 
3431 	/*
3432 	 * [DevILK] Work around required when enabling DP PLL
3433 	 * while a pipe is enabled going to FDI:
3434 	 * 1. Wait for the start of vertical blank on the enabled pipe going to FDI
3435 	 * 2. Program DP PLL enable
3436 	 */
3437 	if (IS_GEN(dev_priv, 5))
3438 		intel_wait_for_vblank_if_active(dev_priv, !crtc->pipe);
3439 
3440 	intel_dp->DP |= DP_PLL_ENABLE;
3441 
3442 	intel_de_write(dev_priv, DP_A, intel_dp->DP);
3443 	intel_de_posting_read(dev_priv, DP_A);
3444 	udelay(200);
3445 }
3446 
3447 static void ilk_edp_pll_off(struct intel_dp *intel_dp,
3448 			    const struct intel_crtc_state *old_crtc_state)
3449 {
3450 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
3451 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3452 
3453 	assert_pipe_disabled(dev_priv, old_crtc_state->cpu_transcoder);
3454 	assert_dp_port_disabled(intel_dp);
3455 	assert_edp_pll_enabled(dev_priv);
3456 
3457 	drm_dbg_kms(&dev_priv->drm, "disabling eDP PLL\n");
3458 
3459 	intel_dp->DP &= ~DP_PLL_ENABLE;
3460 
3461 	intel_de_write(dev_priv, DP_A, intel_dp->DP);
3462 	intel_de_posting_read(dev_priv, DP_A);
3463 	udelay(200);
3464 }
3465 
3466 static bool downstream_hpd_needs_d0(struct intel_dp *intel_dp)
3467 {
3468 	/*
3469 	 * DPCD 1.2+ should support BRANCH_DEVICE_CTRL, and thus
3470 	 * be capable of signalling downstream hpd with a long pulse.
3471 	 * Whether or not that means D3 is safe to use is not clear,
3472 	 * but let's assume so until proven otherwise.
3473 	 *
3474 	 * FIXME should really check all downstream ports...
3475 	 */
3476 	return intel_dp->dpcd[DP_DPCD_REV] == 0x11 &&
3477 		drm_dp_is_branch(intel_dp->dpcd) &&
3478 		intel_dp->downstream_ports[0] & DP_DS_PORT_HPD;
3479 }
3480 
3481 void intel_dp_sink_set_decompression_state(struct intel_dp *intel_dp,
3482 					   const struct intel_crtc_state *crtc_state,
3483 					   bool enable)
3484 {
3485 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3486 	int ret;
3487 
3488 	if (!crtc_state->dsc.compression_enable)
3489 		return;
3490 
3491 	ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_DSC_ENABLE,
3492 				 enable ? DP_DECOMPRESSION_EN : 0);
3493 	if (ret < 0)
3494 		drm_dbg_kms(&i915->drm,
3495 			    "Failed to %s sink decompression state\n",
3496 			    enable ? "enable" : "disable");
3497 }
3498 
3499 /* If the device supports it, try to set the power state appropriately */
3500 void intel_dp_set_power(struct intel_dp *intel_dp, u8 mode)
3501 {
3502 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3503 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3504 	int ret, i;
3505 
3506 	/* Should have a valid DPCD by this point */
3507 	if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
3508 		return;
3509 
3510 	if (mode != DP_SET_POWER_D0) {
3511 		if (downstream_hpd_needs_d0(intel_dp))
3512 			return;
3513 
3514 		ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER, mode);
3515 	} else {
3516 		struct intel_lspcon *lspcon = dp_to_lspcon(intel_dp);
3517 
3518 		/*
3519 		 * When turning on, we need to retry for 1ms to give the sink
3520 		 * time to wake up.
3521 		 */
3522 		for (i = 0; i < 3; i++) {
3523 			ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER, mode);
3524 			if (ret == 1)
3525 				break;
3526 			drm_msleep(1);
3527 		}
3528 
3529 		if (ret == 1 && lspcon->active)
3530 			lspcon_wait_pcon_mode(lspcon);
3531 	}
3532 
3533 	if (ret != 1)
3534 		drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] Set power to %s failed\n",
3535 			    encoder->base.base.id, encoder->base.name,
3536 			    mode == DP_SET_POWER_D0 ? "D0" : "D3");
3537 }
3538 
3539 static bool cpt_dp_port_selected(struct drm_i915_private *dev_priv,
3540 				 enum port port, enum pipe *pipe)
3541 {
3542 	enum pipe p;
3543 
3544 	for_each_pipe(dev_priv, p) {
3545 		u32 val = intel_de_read(dev_priv, TRANS_DP_CTL(p));
3546 
3547 		if ((val & TRANS_DP_PORT_SEL_MASK) == TRANS_DP_PORT_SEL(port)) {
3548 			*pipe = p;
3549 			return true;
3550 		}
3551 	}
3552 
3553 	drm_dbg_kms(&dev_priv->drm, "No pipe for DP port %c found\n",
3554 		    port_name(port));
3555 
3556 	/* must initialize pipe to something for the asserts */
3557 	*pipe = PIPE_A;
3558 
3559 	return false;
3560 }
3561 
3562 bool intel_dp_port_enabled(struct drm_i915_private *dev_priv,
3563 			   i915_reg_t dp_reg, enum port port,
3564 			   enum pipe *pipe)
3565 {
3566 	bool ret;
3567 	u32 val;
3568 
3569 	val = intel_de_read(dev_priv, dp_reg);
3570 
3571 	ret = val & DP_PORT_EN;
3572 
3573 	/* asserts want to know the pipe even if the port is disabled */
3574 	if (IS_IVYBRIDGE(dev_priv) && port == PORT_A)
3575 		*pipe = (val & DP_PIPE_SEL_MASK_IVB) >> DP_PIPE_SEL_SHIFT_IVB;
3576 	else if (HAS_PCH_CPT(dev_priv) && port != PORT_A)
3577 		ret &= cpt_dp_port_selected(dev_priv, port, pipe);
3578 	else if (IS_CHERRYVIEW(dev_priv))
3579 		*pipe = (val & DP_PIPE_SEL_MASK_CHV) >> DP_PIPE_SEL_SHIFT_CHV;
3580 	else
3581 		*pipe = (val & DP_PIPE_SEL_MASK) >> DP_PIPE_SEL_SHIFT;
3582 
3583 	return ret;
3584 }
3585 
3586 static bool intel_dp_get_hw_state(struct intel_encoder *encoder,
3587 				  enum pipe *pipe)
3588 {
3589 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3590 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3591 	intel_wakeref_t wakeref;
3592 	bool ret;
3593 
3594 	wakeref = intel_display_power_get_if_enabled(dev_priv,
3595 						     encoder->power_domain);
3596 	if (!wakeref)
3597 		return false;
3598 
3599 	ret = intel_dp_port_enabled(dev_priv, intel_dp->output_reg,
3600 				    encoder->port, pipe);
3601 
3602 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
3603 
3604 	return ret;
3605 }
3606 
3607 static void intel_dp_get_config(struct intel_encoder *encoder,
3608 				struct intel_crtc_state *pipe_config)
3609 {
3610 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3611 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3612 	u32 tmp, flags = 0;
3613 	enum port port = encoder->port;
3614 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
3615 
3616 	if (encoder->type == INTEL_OUTPUT_EDP)
3617 		pipe_config->output_types |= BIT(INTEL_OUTPUT_EDP);
3618 	else
3619 		pipe_config->output_types |= BIT(INTEL_OUTPUT_DP);
3620 
3621 	tmp = intel_de_read(dev_priv, intel_dp->output_reg);
3622 
3623 	pipe_config->has_audio = tmp & DP_AUDIO_OUTPUT_ENABLE && port != PORT_A;
3624 
3625 	if (HAS_PCH_CPT(dev_priv) && port != PORT_A) {
3626 		u32 trans_dp = intel_de_read(dev_priv,
3627 					     TRANS_DP_CTL(crtc->pipe));
3628 
3629 		if (trans_dp & TRANS_DP_HSYNC_ACTIVE_HIGH)
3630 			flags |= DRM_MODE_FLAG_PHSYNC;
3631 		else
3632 			flags |= DRM_MODE_FLAG_NHSYNC;
3633 
3634 		if (trans_dp & TRANS_DP_VSYNC_ACTIVE_HIGH)
3635 			flags |= DRM_MODE_FLAG_PVSYNC;
3636 		else
3637 			flags |= DRM_MODE_FLAG_NVSYNC;
3638 	} else {
3639 		if (tmp & DP_SYNC_HS_HIGH)
3640 			flags |= DRM_MODE_FLAG_PHSYNC;
3641 		else
3642 			flags |= DRM_MODE_FLAG_NHSYNC;
3643 
3644 		if (tmp & DP_SYNC_VS_HIGH)
3645 			flags |= DRM_MODE_FLAG_PVSYNC;
3646 		else
3647 			flags |= DRM_MODE_FLAG_NVSYNC;
3648 	}
3649 
3650 	pipe_config->hw.adjusted_mode.flags |= flags;
3651 
3652 	if (IS_G4X(dev_priv) && tmp & DP_COLOR_RANGE_16_235)
3653 		pipe_config->limited_color_range = true;
3654 
3655 	pipe_config->lane_count =
3656 		((tmp & DP_PORT_WIDTH_MASK) >> DP_PORT_WIDTH_SHIFT) + 1;
3657 
3658 	intel_dp_get_m_n(crtc, pipe_config);
3659 
3660 	if (port == PORT_A) {
3661 		if ((intel_de_read(dev_priv, DP_A) & DP_PLL_FREQ_MASK) == DP_PLL_FREQ_162MHZ)
3662 			pipe_config->port_clock = 162000;
3663 		else
3664 			pipe_config->port_clock = 270000;
3665 	}
3666 
3667 	pipe_config->hw.adjusted_mode.crtc_clock =
3668 		intel_dotclock_calculate(pipe_config->port_clock,
3669 					 &pipe_config->dp_m_n);
3670 
3671 	if (intel_dp_is_edp(intel_dp) && dev_priv->vbt.edp.bpp &&
3672 	    pipe_config->pipe_bpp > dev_priv->vbt.edp.bpp) {
3673 		/*
3674 		 * This is a big fat ugly hack.
3675 		 *
3676 		 * Some machines in UEFI boot mode provide us a VBT that has 18
3677 		 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
3678 		 * unknown we fail to light up. Yet the same BIOS boots up with
3679 		 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
3680 		 * max, not what it tells us to use.
3681 		 *
3682 		 * Note: This will still be broken if the eDP panel is not lit
3683 		 * up by the BIOS, and thus we can't get the mode at module
3684 		 * load.
3685 		 */
3686 		drm_dbg_kms(&dev_priv->drm,
3687 			    "pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
3688 			    pipe_config->pipe_bpp, dev_priv->vbt.edp.bpp);
3689 		dev_priv->vbt.edp.bpp = pipe_config->pipe_bpp;
3690 	}
3691 }
3692 
3693 static void intel_disable_dp(struct intel_atomic_state *state,
3694 			     struct intel_encoder *encoder,
3695 			     const struct intel_crtc_state *old_crtc_state,
3696 			     const struct drm_connector_state *old_conn_state)
3697 {
3698 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3699 
3700 	intel_dp->link_trained = false;
3701 
3702 	if (old_crtc_state->has_audio)
3703 		intel_audio_codec_disable(encoder,
3704 					  old_crtc_state, old_conn_state);
3705 
3706 	/* Make sure the panel is off before trying to change the mode. But also
3707 	 * ensure that we have vdd while we switch off the panel. */
3708 	intel_edp_panel_vdd_on(intel_dp);
3709 	intel_edp_backlight_off(old_conn_state);
3710 	intel_dp_set_power(intel_dp, DP_SET_POWER_D3);
3711 	intel_edp_panel_off(intel_dp);
3712 }
3713 
3714 static void g4x_disable_dp(struct intel_atomic_state *state,
3715 			   struct intel_encoder *encoder,
3716 			   const struct intel_crtc_state *old_crtc_state,
3717 			   const struct drm_connector_state *old_conn_state)
3718 {
3719 	intel_disable_dp(state, encoder, old_crtc_state, old_conn_state);
3720 }
3721 
3722 static void vlv_disable_dp(struct intel_atomic_state *state,
3723 			   struct intel_encoder *encoder,
3724 			   const struct intel_crtc_state *old_crtc_state,
3725 			   const struct drm_connector_state *old_conn_state)
3726 {
3727 	intel_disable_dp(state, encoder, old_crtc_state, old_conn_state);
3728 }
3729 
3730 static void g4x_post_disable_dp(struct intel_atomic_state *state,
3731 				struct intel_encoder *encoder,
3732 				const struct intel_crtc_state *old_crtc_state,
3733 				const struct drm_connector_state *old_conn_state)
3734 {
3735 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3736 	enum port port = encoder->port;
3737 
3738 	/*
3739 	 * Bspec does not list a specific disable sequence for g4x DP.
3740 	 * Follow the ilk+ sequence (disable pipe before the port) for
3741 	 * g4x DP as it does not suffer from underruns like the normal
3742 	 * g4x modeset sequence (disable pipe after the port).
3743 	 */
3744 	intel_dp_link_down(encoder, old_crtc_state);
3745 
3746 	/* Only ilk+ has port A */
3747 	if (port == PORT_A)
3748 		ilk_edp_pll_off(intel_dp, old_crtc_state);
3749 }
3750 
3751 static void vlv_post_disable_dp(struct intel_atomic_state *state,
3752 				struct intel_encoder *encoder,
3753 				const struct intel_crtc_state *old_crtc_state,
3754 				const struct drm_connector_state *old_conn_state)
3755 {
3756 	intel_dp_link_down(encoder, old_crtc_state);
3757 }
3758 
3759 static void chv_post_disable_dp(struct intel_atomic_state *state,
3760 				struct intel_encoder *encoder,
3761 				const struct intel_crtc_state *old_crtc_state,
3762 				const struct drm_connector_state *old_conn_state)
3763 {
3764 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3765 
3766 	intel_dp_link_down(encoder, old_crtc_state);
3767 
3768 	vlv_dpio_get(dev_priv);
3769 
3770 	/* Assert data lane reset */
3771 	chv_data_lane_soft_reset(encoder, old_crtc_state, true);
3772 
3773 	vlv_dpio_put(dev_priv);
3774 }
3775 
3776 static void
3777 cpt_set_link_train(struct intel_dp *intel_dp,
3778 		   u8 dp_train_pat)
3779 {
3780 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
3781 	u32 *DP = &intel_dp->DP;
3782 
3783 	*DP &= ~DP_LINK_TRAIN_MASK_CPT;
3784 
3785 	switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
3786 	case DP_TRAINING_PATTERN_DISABLE:
3787 		*DP |= DP_LINK_TRAIN_OFF_CPT;
3788 		break;
3789 	case DP_TRAINING_PATTERN_1:
3790 		*DP |= DP_LINK_TRAIN_PAT_1_CPT;
3791 		break;
3792 	case DP_TRAINING_PATTERN_2:
3793 		*DP |= DP_LINK_TRAIN_PAT_2_CPT;
3794 		break;
3795 	case DP_TRAINING_PATTERN_3:
3796 		drm_dbg_kms(&dev_priv->drm,
3797 			    "TPS3 not supported, using TPS2 instead\n");
3798 		*DP |= DP_LINK_TRAIN_PAT_2_CPT;
3799 		break;
3800 	}
3801 
3802 	intel_de_write(dev_priv, intel_dp->output_reg, intel_dp->DP);
3803 	intel_de_posting_read(dev_priv, intel_dp->output_reg);
3804 }
3805 
3806 static void
3807 g4x_set_link_train(struct intel_dp *intel_dp,
3808 		   u8 dp_train_pat)
3809 {
3810 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
3811 	u32 *DP = &intel_dp->DP;
3812 
3813 	*DP &= ~DP_LINK_TRAIN_MASK;
3814 
3815 	switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
3816 	case DP_TRAINING_PATTERN_DISABLE:
3817 		*DP |= DP_LINK_TRAIN_OFF;
3818 		break;
3819 	case DP_TRAINING_PATTERN_1:
3820 		*DP |= DP_LINK_TRAIN_PAT_1;
3821 		break;
3822 	case DP_TRAINING_PATTERN_2:
3823 		*DP |= DP_LINK_TRAIN_PAT_2;
3824 		break;
3825 	case DP_TRAINING_PATTERN_3:
3826 		drm_dbg_kms(&dev_priv->drm,
3827 			    "TPS3 not supported, using TPS2 instead\n");
3828 		*DP |= DP_LINK_TRAIN_PAT_2;
3829 		break;
3830 	}
3831 
3832 	intel_de_write(dev_priv, intel_dp->output_reg, intel_dp->DP);
3833 	intel_de_posting_read(dev_priv, intel_dp->output_reg);
3834 }
3835 
3836 static void intel_dp_enable_port(struct intel_dp *intel_dp,
3837 				 const struct intel_crtc_state *old_crtc_state)
3838 {
3839 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
3840 
3841 	/* enable with pattern 1 (as per spec) */
3842 
3843 	intel_dp_program_link_training_pattern(intel_dp, DP_TRAINING_PATTERN_1);
3844 
3845 	/*
3846 	 * Magic for VLV/CHV. We _must_ first set up the register
3847 	 * without actually enabling the port, and then do another
3848 	 * write to enable the port. Otherwise link training will
3849 	 * fail when the power sequencer is freshly used for this port.
3850 	 */
3851 	intel_dp->DP |= DP_PORT_EN;
3852 	if (old_crtc_state->has_audio)
3853 		intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
3854 
3855 	intel_de_write(dev_priv, intel_dp->output_reg, intel_dp->DP);
3856 	intel_de_posting_read(dev_priv, intel_dp->output_reg);
3857 }
3858 
3859 void intel_dp_configure_protocol_converter(struct intel_dp *intel_dp,
3860 					   const struct intel_crtc_state *crtc_state)
3861 {
3862 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3863 	u8 tmp;
3864 
3865 	if (intel_dp->dpcd[DP_DPCD_REV] < 0x13)
3866 		return;
3867 
3868 	if (!drm_dp_is_branch(intel_dp->dpcd))
3869 		return;
3870 
3871 	tmp = intel_dp->has_hdmi_sink ?
3872 		DP_HDMI_DVI_OUTPUT_CONFIG : 0;
3873 
3874 	if (drm_dp_dpcd_writeb(&intel_dp->aux,
3875 			       DP_PROTOCOL_CONVERTER_CONTROL_0, tmp) != 1)
3876 		drm_dbg_kms(&i915->drm, "Failed to set protocol converter HDMI mode to %s\n",
3877 			    enableddisabled(intel_dp->has_hdmi_sink));
3878 
3879 	tmp = crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444 &&
3880 		intel_dp->dfp.ycbcr_444_to_420 ? DP_CONVERSION_TO_YCBCR420_ENABLE : 0;
3881 
3882 	if (drm_dp_dpcd_writeb(&intel_dp->aux,
3883 			       DP_PROTOCOL_CONVERTER_CONTROL_1, tmp) != 1)
3884 		drm_dbg_kms(&i915->drm,
3885 			    "Failed to set protocol converter YCbCr 4:2:0 conversion mode to %s\n",
3886 			    enableddisabled(intel_dp->dfp.ycbcr_444_to_420));
3887 
3888 	tmp = 0;
3889 
3890 	if (drm_dp_dpcd_writeb(&intel_dp->aux,
3891 			       DP_PROTOCOL_CONVERTER_CONTROL_2, tmp) <= 0)
3892 		drm_dbg_kms(&i915->drm,
3893 			    "Failed to set protocol converter YCbCr 4:2:2 conversion mode to %s\n",
3894 			    enableddisabled(false));
3895 }
3896 
3897 static void intel_enable_dp(struct intel_atomic_state *state,
3898 			    struct intel_encoder *encoder,
3899 			    const struct intel_crtc_state *pipe_config,
3900 			    const struct drm_connector_state *conn_state)
3901 {
3902 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3903 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3904 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
3905 	u32 dp_reg = intel_de_read(dev_priv, intel_dp->output_reg);
3906 	enum pipe pipe = crtc->pipe;
3907 	intel_wakeref_t wakeref;
3908 
3909 	if (drm_WARN_ON(&dev_priv->drm, dp_reg & DP_PORT_EN))
3910 		return;
3911 
3912 	with_pps_lock(intel_dp, wakeref) {
3913 		if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
3914 			vlv_init_panel_power_sequencer(encoder, pipe_config);
3915 
3916 		intel_dp_enable_port(intel_dp, pipe_config);
3917 
3918 		edp_panel_vdd_on(intel_dp);
3919 		edp_panel_on(intel_dp);
3920 		edp_panel_vdd_off(intel_dp, true);
3921 	}
3922 
3923 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3924 		unsigned int lane_mask = 0x0;
3925 
3926 		if (IS_CHERRYVIEW(dev_priv))
3927 			lane_mask = intel_dp_unused_lane_mask(pipe_config->lane_count);
3928 
3929 		vlv_wait_port_ready(dev_priv, dp_to_dig_port(intel_dp),
3930 				    lane_mask);
3931 	}
3932 
3933 	intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
3934 	intel_dp_configure_protocol_converter(intel_dp, pipe_config);
3935 	intel_dp_start_link_train(intel_dp);
3936 	intel_dp_stop_link_train(intel_dp);
3937 
3938 	if (pipe_config->has_audio) {
3939 		drm_dbg(&dev_priv->drm, "Enabling DP audio on pipe %c\n",
3940 			pipe_name(pipe));
3941 		intel_audio_codec_enable(encoder, pipe_config, conn_state);
3942 	}
3943 }
3944 
3945 static void g4x_enable_dp(struct intel_atomic_state *state,
3946 			  struct intel_encoder *encoder,
3947 			  const struct intel_crtc_state *pipe_config,
3948 			  const struct drm_connector_state *conn_state)
3949 {
3950 	intel_enable_dp(state, encoder, pipe_config, conn_state);
3951 	intel_edp_backlight_on(pipe_config, conn_state);
3952 }
3953 
3954 static void vlv_enable_dp(struct intel_atomic_state *state,
3955 			  struct intel_encoder *encoder,
3956 			  const struct intel_crtc_state *pipe_config,
3957 			  const struct drm_connector_state *conn_state)
3958 {
3959 	intel_edp_backlight_on(pipe_config, conn_state);
3960 }
3961 
3962 static void g4x_pre_enable_dp(struct intel_atomic_state *state,
3963 			      struct intel_encoder *encoder,
3964 			      const struct intel_crtc_state *pipe_config,
3965 			      const struct drm_connector_state *conn_state)
3966 {
3967 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3968 	enum port port = encoder->port;
3969 
3970 	intel_dp_prepare(encoder, pipe_config);
3971 
3972 	/* Only ilk+ has port A */
3973 	if (port == PORT_A)
3974 		ilk_edp_pll_on(intel_dp, pipe_config);
3975 }
3976 
3977 static void vlv_detach_power_sequencer(struct intel_dp *intel_dp)
3978 {
3979 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
3980 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
3981 	enum pipe pipe = intel_dp->pps_pipe;
3982 	i915_reg_t pp_on_reg = PP_ON_DELAYS(pipe);
3983 
3984 	drm_WARN_ON(&dev_priv->drm, intel_dp->active_pipe != INVALID_PIPE);
3985 
3986 	if (drm_WARN_ON(&dev_priv->drm, pipe != PIPE_A && pipe != PIPE_B))
3987 		return;
3988 
3989 	edp_panel_vdd_off_sync(intel_dp);
3990 
3991 	/*
3992 	 * VLV seems to get confused when multiple power sequencers
3993 	 * have the same port selected (even if only one has power/vdd
3994 	 * enabled). The failure manifests as vlv_wait_port_ready() failing
3995 	 * CHV on the other hand doesn't seem to mind having the same port
3996 	 * selected in multiple power sequencers, but let's clear the
3997 	 * port select always when logically disconnecting a power sequencer
3998 	 * from a port.
3999 	 */
4000 	drm_dbg_kms(&dev_priv->drm,
4001 		    "detaching pipe %c power sequencer from [ENCODER:%d:%s]\n",
4002 		    pipe_name(pipe), dig_port->base.base.base.id,
4003 		    dig_port->base.base.name);
4004 	intel_de_write(dev_priv, pp_on_reg, 0);
4005 	intel_de_posting_read(dev_priv, pp_on_reg);
4006 
4007 	intel_dp->pps_pipe = INVALID_PIPE;
4008 }
4009 
4010 static void vlv_steal_power_sequencer(struct drm_i915_private *dev_priv,
4011 				      enum pipe pipe)
4012 {
4013 	struct intel_encoder *encoder;
4014 
4015 	lockdep_assert_held(&dev_priv->pps_mutex);
4016 
4017 	for_each_intel_dp(&dev_priv->drm, encoder) {
4018 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4019 
4020 		drm_WARN(&dev_priv->drm, intel_dp->active_pipe == pipe,
4021 			 "stealing pipe %c power sequencer from active [ENCODER:%d:%s]\n",
4022 			 pipe_name(pipe), encoder->base.base.id,
4023 			 encoder->base.name);
4024 
4025 		if (intel_dp->pps_pipe != pipe)
4026 			continue;
4027 
4028 		drm_dbg_kms(&dev_priv->drm,
4029 			    "stealing pipe %c power sequencer from [ENCODER:%d:%s]\n",
4030 			    pipe_name(pipe), encoder->base.base.id,
4031 			    encoder->base.name);
4032 
4033 		/* make sure vdd is off before we steal it */
4034 		vlv_detach_power_sequencer(intel_dp);
4035 	}
4036 }
4037 
4038 static void vlv_init_panel_power_sequencer(struct intel_encoder *encoder,
4039 					   const struct intel_crtc_state *crtc_state)
4040 {
4041 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4042 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4043 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4044 
4045 	lockdep_assert_held(&dev_priv->pps_mutex);
4046 
4047 	drm_WARN_ON(&dev_priv->drm, intel_dp->active_pipe != INVALID_PIPE);
4048 
4049 	if (intel_dp->pps_pipe != INVALID_PIPE &&
4050 	    intel_dp->pps_pipe != crtc->pipe) {
4051 		/*
4052 		 * If another power sequencer was being used on this
4053 		 * port previously make sure to turn off vdd there while
4054 		 * we still have control of it.
4055 		 */
4056 		vlv_detach_power_sequencer(intel_dp);
4057 	}
4058 
4059 	/*
4060 	 * We may be stealing the power
4061 	 * sequencer from another port.
4062 	 */
4063 	vlv_steal_power_sequencer(dev_priv, crtc->pipe);
4064 
4065 	intel_dp->active_pipe = crtc->pipe;
4066 
4067 	if (!intel_dp_is_edp(intel_dp))
4068 		return;
4069 
4070 	/* now it's all ours */
4071 	intel_dp->pps_pipe = crtc->pipe;
4072 
4073 	drm_dbg_kms(&dev_priv->drm,
4074 		    "initializing pipe %c power sequencer for [ENCODER:%d:%s]\n",
4075 		    pipe_name(intel_dp->pps_pipe), encoder->base.base.id,
4076 		    encoder->base.name);
4077 
4078 	/* init power sequencer on this pipe and port */
4079 	intel_dp_init_panel_power_sequencer(intel_dp);
4080 	intel_dp_init_panel_power_sequencer_registers(intel_dp, true);
4081 }
4082 
4083 static void vlv_pre_enable_dp(struct intel_atomic_state *state,
4084 			      struct intel_encoder *encoder,
4085 			      const struct intel_crtc_state *pipe_config,
4086 			      const struct drm_connector_state *conn_state)
4087 {
4088 	vlv_phy_pre_encoder_enable(encoder, pipe_config);
4089 
4090 	intel_enable_dp(state, encoder, pipe_config, conn_state);
4091 }
4092 
4093 static void vlv_dp_pre_pll_enable(struct intel_atomic_state *state,
4094 				  struct intel_encoder *encoder,
4095 				  const struct intel_crtc_state *pipe_config,
4096 				  const struct drm_connector_state *conn_state)
4097 {
4098 	intel_dp_prepare(encoder, pipe_config);
4099 
4100 	vlv_phy_pre_pll_enable(encoder, pipe_config);
4101 }
4102 
4103 static void chv_pre_enable_dp(struct intel_atomic_state *state,
4104 			      struct intel_encoder *encoder,
4105 			      const struct intel_crtc_state *pipe_config,
4106 			      const struct drm_connector_state *conn_state)
4107 {
4108 	chv_phy_pre_encoder_enable(encoder, pipe_config);
4109 
4110 	intel_enable_dp(state, encoder, pipe_config, conn_state);
4111 
4112 	/* Second common lane will stay alive on its own now */
4113 	chv_phy_release_cl2_override(encoder);
4114 }
4115 
4116 static void chv_dp_pre_pll_enable(struct intel_atomic_state *state,
4117 				  struct intel_encoder *encoder,
4118 				  const struct intel_crtc_state *pipe_config,
4119 				  const struct drm_connector_state *conn_state)
4120 {
4121 	intel_dp_prepare(encoder, pipe_config);
4122 
4123 	chv_phy_pre_pll_enable(encoder, pipe_config);
4124 }
4125 
4126 static void chv_dp_post_pll_disable(struct intel_atomic_state *state,
4127 				    struct intel_encoder *encoder,
4128 				    const struct intel_crtc_state *old_crtc_state,
4129 				    const struct drm_connector_state *old_conn_state)
4130 {
4131 	chv_phy_post_pll_disable(encoder, old_crtc_state);
4132 }
4133 
4134 /*
4135  * Fetch AUX CH registers 0x202 - 0x207 which contain
4136  * link status information
4137  */
4138 bool
4139 intel_dp_get_link_status(struct intel_dp *intel_dp, u8 *link_status)
4140 {
4141 	return drm_dp_dpcd_read(&intel_dp->aux, DP_LANE0_1_STATUS, link_status,
4142 				DP_LINK_STATUS_SIZE) == DP_LINK_STATUS_SIZE;
4143 }
4144 
4145 static u8 intel_dp_voltage_max_2(struct intel_dp *intel_dp)
4146 {
4147 	return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
4148 }
4149 
4150 static u8 intel_dp_voltage_max_3(struct intel_dp *intel_dp)
4151 {
4152 	return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
4153 }
4154 
4155 static u8 intel_dp_pre_empemph_max_2(struct intel_dp *intel_dp)
4156 {
4157 	return DP_TRAIN_PRE_EMPH_LEVEL_2;
4158 }
4159 
4160 static u8 intel_dp_pre_empemph_max_3(struct intel_dp *intel_dp)
4161 {
4162 	return DP_TRAIN_PRE_EMPH_LEVEL_3;
4163 }
4164 
4165 static void vlv_set_signal_levels(struct intel_dp *intel_dp)
4166 {
4167 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
4168 	unsigned long demph_reg_value, preemph_reg_value,
4169 		uniqtranscale_reg_value;
4170 	u8 train_set = intel_dp->train_set[0];
4171 
4172 	switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
4173 	case DP_TRAIN_PRE_EMPH_LEVEL_0:
4174 		preemph_reg_value = 0x0004000;
4175 		switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
4176 		case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
4177 			demph_reg_value = 0x2B405555;
4178 			uniqtranscale_reg_value = 0x552AB83A;
4179 			break;
4180 		case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
4181 			demph_reg_value = 0x2B404040;
4182 			uniqtranscale_reg_value = 0x5548B83A;
4183 			break;
4184 		case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
4185 			demph_reg_value = 0x2B245555;
4186 			uniqtranscale_reg_value = 0x5560B83A;
4187 			break;
4188 		case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
4189 			demph_reg_value = 0x2B405555;
4190 			uniqtranscale_reg_value = 0x5598DA3A;
4191 			break;
4192 		default:
4193 			return;
4194 		}
4195 		break;
4196 	case DP_TRAIN_PRE_EMPH_LEVEL_1:
4197 		preemph_reg_value = 0x0002000;
4198 		switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
4199 		case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
4200 			demph_reg_value = 0x2B404040;
4201 			uniqtranscale_reg_value = 0x5552B83A;
4202 			break;
4203 		case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
4204 			demph_reg_value = 0x2B404848;
4205 			uniqtranscale_reg_value = 0x5580B83A;
4206 			break;
4207 		case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
4208 			demph_reg_value = 0x2B404040;
4209 			uniqtranscale_reg_value = 0x55ADDA3A;
4210 			break;
4211 		default:
4212 			return;
4213 		}
4214 		break;
4215 	case DP_TRAIN_PRE_EMPH_LEVEL_2:
4216 		preemph_reg_value = 0x0000000;
4217 		switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
4218 		case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
4219 			demph_reg_value = 0x2B305555;
4220 			uniqtranscale_reg_value = 0x5570B83A;
4221 			break;
4222 		case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
4223 			demph_reg_value = 0x2B2B4040;
4224 			uniqtranscale_reg_value = 0x55ADDA3A;
4225 			break;
4226 		default:
4227 			return;
4228 		}
4229 		break;
4230 	case DP_TRAIN_PRE_EMPH_LEVEL_3:
4231 		preemph_reg_value = 0x0006000;
4232 		switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
4233 		case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
4234 			demph_reg_value = 0x1B405555;
4235 			uniqtranscale_reg_value = 0x55ADDA3A;
4236 			break;
4237 		default:
4238 			return;
4239 		}
4240 		break;
4241 	default:
4242 		return;
4243 	}
4244 
4245 	vlv_set_phy_signal_level(encoder, demph_reg_value, preemph_reg_value,
4246 				 uniqtranscale_reg_value, 0);
4247 }
4248 
4249 static void chv_set_signal_levels(struct intel_dp *intel_dp)
4250 {
4251 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
4252 	u32 deemph_reg_value, margin_reg_value;
4253 	bool uniq_trans_scale = false;
4254 	u8 train_set = intel_dp->train_set[0];
4255 
4256 	switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
4257 	case DP_TRAIN_PRE_EMPH_LEVEL_0:
4258 		switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
4259 		case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
4260 			deemph_reg_value = 128;
4261 			margin_reg_value = 52;
4262 			break;
4263 		case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
4264 			deemph_reg_value = 128;
4265 			margin_reg_value = 77;
4266 			break;
4267 		case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
4268 			deemph_reg_value = 128;
4269 			margin_reg_value = 102;
4270 			break;
4271 		case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
4272 			deemph_reg_value = 128;
4273 			margin_reg_value = 154;
4274 			uniq_trans_scale = true;
4275 			break;
4276 		default:
4277 			return;
4278 		}
4279 		break;
4280 	case DP_TRAIN_PRE_EMPH_LEVEL_1:
4281 		switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
4282 		case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
4283 			deemph_reg_value = 85;
4284 			margin_reg_value = 78;
4285 			break;
4286 		case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
4287 			deemph_reg_value = 85;
4288 			margin_reg_value = 116;
4289 			break;
4290 		case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
4291 			deemph_reg_value = 85;
4292 			margin_reg_value = 154;
4293 			break;
4294 		default:
4295 			return;
4296 		}
4297 		break;
4298 	case DP_TRAIN_PRE_EMPH_LEVEL_2:
4299 		switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
4300 		case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
4301 			deemph_reg_value = 64;
4302 			margin_reg_value = 104;
4303 			break;
4304 		case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
4305 			deemph_reg_value = 64;
4306 			margin_reg_value = 154;
4307 			break;
4308 		default:
4309 			return;
4310 		}
4311 		break;
4312 	case DP_TRAIN_PRE_EMPH_LEVEL_3:
4313 		switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
4314 		case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
4315 			deemph_reg_value = 43;
4316 			margin_reg_value = 154;
4317 			break;
4318 		default:
4319 			return;
4320 		}
4321 		break;
4322 	default:
4323 		return;
4324 	}
4325 
4326 	chv_set_phy_signal_level(encoder, deemph_reg_value,
4327 				 margin_reg_value, uniq_trans_scale);
4328 }
4329 
4330 static u32 g4x_signal_levels(u8 train_set)
4331 {
4332 	u32 signal_levels = 0;
4333 
4334 	switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
4335 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
4336 	default:
4337 		signal_levels |= DP_VOLTAGE_0_4;
4338 		break;
4339 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
4340 		signal_levels |= DP_VOLTAGE_0_6;
4341 		break;
4342 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
4343 		signal_levels |= DP_VOLTAGE_0_8;
4344 		break;
4345 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
4346 		signal_levels |= DP_VOLTAGE_1_2;
4347 		break;
4348 	}
4349 	switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
4350 	case DP_TRAIN_PRE_EMPH_LEVEL_0:
4351 	default:
4352 		signal_levels |= DP_PRE_EMPHASIS_0;
4353 		break;
4354 	case DP_TRAIN_PRE_EMPH_LEVEL_1:
4355 		signal_levels |= DP_PRE_EMPHASIS_3_5;
4356 		break;
4357 	case DP_TRAIN_PRE_EMPH_LEVEL_2:
4358 		signal_levels |= DP_PRE_EMPHASIS_6;
4359 		break;
4360 	case DP_TRAIN_PRE_EMPH_LEVEL_3:
4361 		signal_levels |= DP_PRE_EMPHASIS_9_5;
4362 		break;
4363 	}
4364 	return signal_levels;
4365 }
4366 
4367 static void
4368 g4x_set_signal_levels(struct intel_dp *intel_dp)
4369 {
4370 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
4371 	u8 train_set = intel_dp->train_set[0];
4372 	u32 signal_levels;
4373 
4374 	signal_levels = g4x_signal_levels(train_set);
4375 
4376 	drm_dbg_kms(&dev_priv->drm, "Using signal levels %08x\n",
4377 		    signal_levels);
4378 
4379 	intel_dp->DP &= ~(DP_VOLTAGE_MASK | DP_PRE_EMPHASIS_MASK);
4380 	intel_dp->DP |= signal_levels;
4381 
4382 	intel_de_write(dev_priv, intel_dp->output_reg, intel_dp->DP);
4383 	intel_de_posting_read(dev_priv, intel_dp->output_reg);
4384 }
4385 
4386 /* SNB CPU eDP voltage swing and pre-emphasis control */
4387 static u32 snb_cpu_edp_signal_levels(u8 train_set)
4388 {
4389 	u8 signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
4390 					DP_TRAIN_PRE_EMPHASIS_MASK);
4391 
4392 	switch (signal_levels) {
4393 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0:
4394 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0:
4395 		return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
4396 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1:
4397 		return EDP_LINK_TRAIN_400MV_3_5DB_SNB_B;
4398 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2:
4399 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_2:
4400 		return EDP_LINK_TRAIN_400_600MV_6DB_SNB_B;
4401 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1:
4402 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1:
4403 		return EDP_LINK_TRAIN_600_800MV_3_5DB_SNB_B;
4404 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0:
4405 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_3 | DP_TRAIN_PRE_EMPH_LEVEL_0:
4406 		return EDP_LINK_TRAIN_800_1200MV_0DB_SNB_B;
4407 	default:
4408 		DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
4409 			      "0x%x\n", signal_levels);
4410 		return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
4411 	}
4412 }
4413 
4414 static void
4415 snb_cpu_edp_set_signal_levels(struct intel_dp *intel_dp)
4416 {
4417 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
4418 	u8 train_set = intel_dp->train_set[0];
4419 	u32 signal_levels;
4420 
4421 	signal_levels = snb_cpu_edp_signal_levels(train_set);
4422 
4423 	drm_dbg_kms(&dev_priv->drm, "Using signal levels %08x\n",
4424 		    signal_levels);
4425 
4426 	intel_dp->DP &= ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB;
4427 	intel_dp->DP |= signal_levels;
4428 
4429 	intel_de_write(dev_priv, intel_dp->output_reg, intel_dp->DP);
4430 	intel_de_posting_read(dev_priv, intel_dp->output_reg);
4431 }
4432 
4433 /* IVB CPU eDP voltage swing and pre-emphasis control */
4434 static u32 ivb_cpu_edp_signal_levels(u8 train_set)
4435 {
4436 	u8 signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
4437 					DP_TRAIN_PRE_EMPHASIS_MASK);
4438 
4439 	switch (signal_levels) {
4440 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0:
4441 		return EDP_LINK_TRAIN_400MV_0DB_IVB;
4442 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1:
4443 		return EDP_LINK_TRAIN_400MV_3_5DB_IVB;
4444 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2:
4445 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_2:
4446 		return EDP_LINK_TRAIN_400MV_6DB_IVB;
4447 
4448 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0:
4449 		return EDP_LINK_TRAIN_600MV_0DB_IVB;
4450 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1:
4451 		return EDP_LINK_TRAIN_600MV_3_5DB_IVB;
4452 
4453 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0:
4454 		return EDP_LINK_TRAIN_800MV_0DB_IVB;
4455 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1:
4456 		return EDP_LINK_TRAIN_800MV_3_5DB_IVB;
4457 
4458 	default:
4459 		DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
4460 			      "0x%x\n", signal_levels);
4461 		return EDP_LINK_TRAIN_500MV_0DB_IVB;
4462 	}
4463 }
4464 
4465 static void
4466 ivb_cpu_edp_set_signal_levels(struct intel_dp *intel_dp)
4467 {
4468 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
4469 	u8 train_set = intel_dp->train_set[0];
4470 	u32 signal_levels;
4471 
4472 	signal_levels = ivb_cpu_edp_signal_levels(train_set);
4473 
4474 	drm_dbg_kms(&dev_priv->drm, "Using signal levels %08x\n",
4475 		    signal_levels);
4476 
4477 	intel_dp->DP &= ~EDP_LINK_TRAIN_VOL_EMP_MASK_IVB;
4478 	intel_dp->DP |= signal_levels;
4479 
4480 	intel_de_write(dev_priv, intel_dp->output_reg, intel_dp->DP);
4481 	intel_de_posting_read(dev_priv, intel_dp->output_reg);
4482 }
4483 
4484 void intel_dp_set_signal_levels(struct intel_dp *intel_dp)
4485 {
4486 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
4487 	u8 train_set = intel_dp->train_set[0];
4488 
4489 	drm_dbg_kms(&dev_priv->drm, "Using vswing level %d%s\n",
4490 		    train_set & DP_TRAIN_VOLTAGE_SWING_MASK,
4491 		    train_set & DP_TRAIN_MAX_SWING_REACHED ? " (max)" : "");
4492 	drm_dbg_kms(&dev_priv->drm, "Using pre-emphasis level %d%s\n",
4493 		    (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) >>
4494 		    DP_TRAIN_PRE_EMPHASIS_SHIFT,
4495 		    train_set & DP_TRAIN_MAX_PRE_EMPHASIS_REACHED ?
4496 		    " (max)" : "");
4497 
4498 	intel_dp->set_signal_levels(intel_dp);
4499 }
4500 
4501 void
4502 intel_dp_program_link_training_pattern(struct intel_dp *intel_dp,
4503 				       u8 dp_train_pat)
4504 {
4505 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
4506 	u8 train_pat_mask = drm_dp_training_pattern_mask(intel_dp->dpcd);
4507 
4508 	if (dp_train_pat & train_pat_mask)
4509 		drm_dbg_kms(&dev_priv->drm,
4510 			    "Using DP training pattern TPS%d\n",
4511 			    dp_train_pat & train_pat_mask);
4512 
4513 	intel_dp->set_link_train(intel_dp, dp_train_pat);
4514 }
4515 
4516 void intel_dp_set_idle_link_train(struct intel_dp *intel_dp)
4517 {
4518 	if (intel_dp->set_idle_link_train)
4519 		intel_dp->set_idle_link_train(intel_dp);
4520 }
4521 
4522 static void
4523 intel_dp_link_down(struct intel_encoder *encoder,
4524 		   const struct intel_crtc_state *old_crtc_state)
4525 {
4526 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4527 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4528 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
4529 	enum port port = encoder->port;
4530 	u32 DP = intel_dp->DP;
4531 
4532 	if (drm_WARN_ON(&dev_priv->drm,
4533 			(intel_de_read(dev_priv, intel_dp->output_reg) &
4534 			 DP_PORT_EN) == 0))
4535 		return;
4536 
4537 	drm_dbg_kms(&dev_priv->drm, "\n");
4538 
4539 	if ((IS_IVYBRIDGE(dev_priv) && port == PORT_A) ||
4540 	    (HAS_PCH_CPT(dev_priv) && port != PORT_A)) {
4541 		DP &= ~DP_LINK_TRAIN_MASK_CPT;
4542 		DP |= DP_LINK_TRAIN_PAT_IDLE_CPT;
4543 	} else {
4544 		DP &= ~DP_LINK_TRAIN_MASK;
4545 		DP |= DP_LINK_TRAIN_PAT_IDLE;
4546 	}
4547 	intel_de_write(dev_priv, intel_dp->output_reg, DP);
4548 	intel_de_posting_read(dev_priv, intel_dp->output_reg);
4549 
4550 	DP &= ~(DP_PORT_EN | DP_AUDIO_OUTPUT_ENABLE);
4551 	intel_de_write(dev_priv, intel_dp->output_reg, DP);
4552 	intel_de_posting_read(dev_priv, intel_dp->output_reg);
4553 
4554 	/*
4555 	 * HW workaround for IBX, we need to move the port
4556 	 * to transcoder A after disabling it to allow the
4557 	 * matching HDMI port to be enabled on transcoder A.
4558 	 */
4559 	if (HAS_PCH_IBX(dev_priv) && crtc->pipe == PIPE_B && port != PORT_A) {
4560 		/*
4561 		 * We get CPU/PCH FIFO underruns on the other pipe when
4562 		 * doing the workaround. Sweep them under the rug.
4563 		 */
4564 		intel_set_cpu_fifo_underrun_reporting(dev_priv, PIPE_A, false);
4565 		intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
4566 
4567 		/* always enable with pattern 1 (as per spec) */
4568 		DP &= ~(DP_PIPE_SEL_MASK | DP_LINK_TRAIN_MASK);
4569 		DP |= DP_PORT_EN | DP_PIPE_SEL(PIPE_A) |
4570 			DP_LINK_TRAIN_PAT_1;
4571 		intel_de_write(dev_priv, intel_dp->output_reg, DP);
4572 		intel_de_posting_read(dev_priv, intel_dp->output_reg);
4573 
4574 		DP &= ~DP_PORT_EN;
4575 		intel_de_write(dev_priv, intel_dp->output_reg, DP);
4576 		intel_de_posting_read(dev_priv, intel_dp->output_reg);
4577 
4578 		intel_wait_for_vblank_if_active(dev_priv, PIPE_A);
4579 		intel_set_cpu_fifo_underrun_reporting(dev_priv, PIPE_A, true);
4580 		intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
4581 	}
4582 
4583 	drm_msleep(intel_dp->panel_power_down_delay);
4584 
4585 	intel_dp->DP = DP;
4586 
4587 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
4588 		intel_wakeref_t wakeref;
4589 
4590 		with_pps_lock(intel_dp, wakeref)
4591 			intel_dp->active_pipe = INVALID_PIPE;
4592 	}
4593 }
4594 
4595 bool intel_dp_get_colorimetry_status(struct intel_dp *intel_dp)
4596 {
4597 	u8 dprx = 0;
4598 
4599 	if (drm_dp_dpcd_readb(&intel_dp->aux, DP_DPRX_FEATURE_ENUMERATION_LIST,
4600 			      &dprx) != 1)
4601 		return false;
4602 	return dprx & DP_VSC_SDP_EXT_FOR_COLORIMETRY_SUPPORTED;
4603 }
4604 
4605 static void intel_dp_get_dsc_sink_cap(struct intel_dp *intel_dp)
4606 {
4607 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4608 
4609 	/*
4610 	 * Clear the cached register set to avoid using stale values
4611 	 * for the sinks that do not support DSC.
4612 	 */
4613 	memset(intel_dp->dsc_dpcd, 0, sizeof(intel_dp->dsc_dpcd));
4614 
4615 	/* Clear fec_capable to avoid using stale values */
4616 	intel_dp->fec_capable = 0;
4617 
4618 	/* Cache the DSC DPCD if eDP or DP rev >= 1.4 */
4619 	if (intel_dp->dpcd[DP_DPCD_REV] >= 0x14 ||
4620 	    intel_dp->edp_dpcd[0] >= DP_EDP_14) {
4621 		if (drm_dp_dpcd_read(&intel_dp->aux, DP_DSC_SUPPORT,
4622 				     intel_dp->dsc_dpcd,
4623 				     sizeof(intel_dp->dsc_dpcd)) < 0)
4624 			drm_err(&i915->drm,
4625 				"Failed to read DPCD register 0x%x\n",
4626 				DP_DSC_SUPPORT);
4627 
4628 		drm_dbg_kms(&i915->drm, "DSC DPCD: %*ph\n",
4629 			    (int)sizeof(intel_dp->dsc_dpcd),
4630 			    intel_dp->dsc_dpcd);
4631 
4632 		/* FEC is supported only on DP 1.4 */
4633 		if (!intel_dp_is_edp(intel_dp) &&
4634 		    drm_dp_dpcd_readb(&intel_dp->aux, DP_FEC_CAPABILITY,
4635 				      &intel_dp->fec_capable) < 0)
4636 			drm_err(&i915->drm,
4637 				"Failed to read FEC DPCD register\n");
4638 
4639 		drm_dbg_kms(&i915->drm, "FEC CAPABILITY: %x\n",
4640 			    intel_dp->fec_capable);
4641 	}
4642 }
4643 
4644 static bool
4645 intel_edp_init_dpcd(struct intel_dp *intel_dp)
4646 {
4647 	struct drm_i915_private *dev_priv =
4648 		to_i915(dp_to_dig_port(intel_dp)->base.base.dev);
4649 
4650 	/* this function is meant to be called only once */
4651 	drm_WARN_ON(&dev_priv->drm, intel_dp->dpcd[DP_DPCD_REV] != 0);
4652 
4653 	if (drm_dp_read_dpcd_caps(&intel_dp->aux, intel_dp->dpcd) != 0)
4654 		return false;
4655 
4656 	drm_dp_read_desc(&intel_dp->aux, &intel_dp->desc,
4657 			 drm_dp_is_branch(intel_dp->dpcd));
4658 
4659 	/*
4660 	 * Read the eDP display control registers.
4661 	 *
4662 	 * Do this independent of DP_DPCD_DISPLAY_CONTROL_CAPABLE bit in
4663 	 * DP_EDP_CONFIGURATION_CAP, because some buggy displays do not have it
4664 	 * set, but require eDP 1.4+ detection (e.g. for supported link rates
4665 	 * method). The display control registers should read zero if they're
4666 	 * not supported anyway.
4667 	 */
4668 	if (drm_dp_dpcd_read(&intel_dp->aux, DP_EDP_DPCD_REV,
4669 			     intel_dp->edp_dpcd, sizeof(intel_dp->edp_dpcd)) ==
4670 			     sizeof(intel_dp->edp_dpcd))
4671 		drm_dbg_kms(&dev_priv->drm, "eDP DPCD: %*ph\n",
4672 			    (int)sizeof(intel_dp->edp_dpcd),
4673 			    intel_dp->edp_dpcd);
4674 
4675 	/*
4676 	 * This has to be called after intel_dp->edp_dpcd is filled, PSR checks
4677 	 * for SET_POWER_CAPABLE bit in intel_dp->edp_dpcd[1]
4678 	 */
4679 	intel_psr_init_dpcd(intel_dp);
4680 
4681 	/* Read the eDP 1.4+ supported link rates. */
4682 	if (intel_dp->edp_dpcd[0] >= DP_EDP_14) {
4683 		__le16 sink_rates[DP_MAX_SUPPORTED_RATES];
4684 		int i;
4685 
4686 		drm_dp_dpcd_read(&intel_dp->aux, DP_SUPPORTED_LINK_RATES,
4687 				sink_rates, sizeof(sink_rates));
4688 
4689 		for (i = 0; i < ARRAY_SIZE(sink_rates); i++) {
4690 			int val = le16_to_cpu(sink_rates[i]);
4691 
4692 			if (val == 0)
4693 				break;
4694 
4695 			/* Value read multiplied by 200kHz gives the per-lane
4696 			 * link rate in kHz. The source rates are, however,
4697 			 * stored in terms of LS_Clk kHz. The full conversion
4698 			 * back to symbols is
4699 			 * (val * 200kHz)*(8/10 ch. encoding)*(1/8 bit to Byte)
4700 			 */
4701 			intel_dp->sink_rates[i] = (val * 200) / 10;
4702 		}
4703 		intel_dp->num_sink_rates = i;
4704 	}
4705 
4706 	/*
4707 	 * Use DP_LINK_RATE_SET if DP_SUPPORTED_LINK_RATES are available,
4708 	 * default to DP_MAX_LINK_RATE and DP_LINK_BW_SET otherwise.
4709 	 */
4710 	if (intel_dp->num_sink_rates)
4711 		intel_dp->use_rate_select = true;
4712 	else
4713 		intel_dp_set_sink_rates(intel_dp);
4714 
4715 	intel_dp_set_common_rates(intel_dp);
4716 
4717 	/* Read the eDP DSC DPCD registers */
4718 	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
4719 		intel_dp_get_dsc_sink_cap(intel_dp);
4720 
4721 	return true;
4722 }
4723 
4724 static bool
4725 intel_dp_has_sink_count(struct intel_dp *intel_dp)
4726 {
4727 	if (!intel_dp->attached_connector)
4728 		return false;
4729 
4730 	return drm_dp_read_sink_count_cap(&intel_dp->attached_connector->base,
4731 					  intel_dp->dpcd,
4732 					  &intel_dp->desc);
4733 }
4734 
4735 static bool
4736 intel_dp_get_dpcd(struct intel_dp *intel_dp)
4737 {
4738 	int ret;
4739 
4740 	if (drm_dp_read_dpcd_caps(&intel_dp->aux, intel_dp->dpcd))
4741 		return false;
4742 
4743 	/*
4744 	 * Don't clobber cached eDP rates. Also skip re-reading
4745 	 * the OUI/ID since we know it won't change.
4746 	 */
4747 	if (!intel_dp_is_edp(intel_dp)) {
4748 		drm_dp_read_desc(&intel_dp->aux, &intel_dp->desc,
4749 				 drm_dp_is_branch(intel_dp->dpcd));
4750 
4751 		intel_dp_set_sink_rates(intel_dp);
4752 		intel_dp_set_common_rates(intel_dp);
4753 	}
4754 
4755 	if (intel_dp_has_sink_count(intel_dp)) {
4756 		ret = drm_dp_read_sink_count(&intel_dp->aux);
4757 		if (ret < 0)
4758 			return false;
4759 
4760 		/*
4761 		 * Sink count can change between short pulse hpd hence
4762 		 * a member variable in intel_dp will track any changes
4763 		 * between short pulse interrupts.
4764 		 */
4765 		intel_dp->sink_count = ret;
4766 
4767 		/*
4768 		 * SINK_COUNT == 0 and DOWNSTREAM_PORT_PRESENT == 1 implies that
4769 		 * a dongle is present but no display. Unless we require to know
4770 		 * if a dongle is present or not, we don't need to update
4771 		 * downstream port information. So, an early return here saves
4772 		 * time from performing other operations which are not required.
4773 		 */
4774 		if (!intel_dp->sink_count)
4775 			return false;
4776 	}
4777 
4778 	return drm_dp_read_downstream_info(&intel_dp->aux, intel_dp->dpcd,
4779 					   intel_dp->downstream_ports) == 0;
4780 }
4781 
4782 static bool
4783 intel_dp_can_mst(struct intel_dp *intel_dp)
4784 {
4785 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4786 
4787 	return i915->params.enable_dp_mst &&
4788 		intel_dp->can_mst &&
4789 		drm_dp_read_mst_cap(&intel_dp->aux, intel_dp->dpcd);
4790 }
4791 
4792 static void
4793 intel_dp_configure_mst(struct intel_dp *intel_dp)
4794 {
4795 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4796 	struct intel_encoder *encoder =
4797 		&dp_to_dig_port(intel_dp)->base;
4798 	bool sink_can_mst = drm_dp_read_mst_cap(&intel_dp->aux, intel_dp->dpcd);
4799 
4800 	drm_dbg_kms(&i915->drm,
4801 		    "[ENCODER:%d:%s] MST support: port: %s, sink: %s, modparam: %s\n",
4802 		    encoder->base.base.id, encoder->base.name,
4803 		    yesno(intel_dp->can_mst), yesno(sink_can_mst),
4804 		    yesno(i915->params.enable_dp_mst));
4805 
4806 	if (!intel_dp->can_mst)
4807 		return;
4808 
4809 	intel_dp->is_mst = sink_can_mst &&
4810 		i915->params.enable_dp_mst;
4811 
4812 	drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr,
4813 					intel_dp->is_mst);
4814 }
4815 
4816 static bool
4817 intel_dp_get_sink_irq_esi(struct intel_dp *intel_dp, u8 *sink_irq_vector)
4818 {
4819 	return drm_dp_dpcd_read(&intel_dp->aux, DP_SINK_COUNT_ESI,
4820 				sink_irq_vector, DP_DPRX_ESI_LEN) ==
4821 		DP_DPRX_ESI_LEN;
4822 }
4823 
4824 bool
4825 intel_dp_needs_vsc_sdp(const struct intel_crtc_state *crtc_state,
4826 		       const struct drm_connector_state *conn_state)
4827 {
4828 	/*
4829 	 * As per DP 1.4a spec section 2.2.4.3 [MSA Field for Indication
4830 	 * of Color Encoding Format and Content Color Gamut], in order to
4831 	 * sending YCBCR 420 or HDR BT.2020 signals we should use DP VSC SDP.
4832 	 */
4833 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
4834 		return true;
4835 
4836 	switch (conn_state->colorspace) {
4837 	case DRM_MODE_COLORIMETRY_SYCC_601:
4838 	case DRM_MODE_COLORIMETRY_OPYCC_601:
4839 	case DRM_MODE_COLORIMETRY_BT2020_YCC:
4840 	case DRM_MODE_COLORIMETRY_BT2020_RGB:
4841 	case DRM_MODE_COLORIMETRY_BT2020_CYCC:
4842 		return true;
4843 	default:
4844 		break;
4845 	}
4846 
4847 	return false;
4848 }
4849 
4850 static ssize_t intel_dp_vsc_sdp_pack(const struct drm_dp_vsc_sdp *vsc,
4851 				     struct dp_sdp *sdp, size_t size)
4852 {
4853 	size_t length = sizeof(struct dp_sdp);
4854 
4855 	if (size < length)
4856 		return -ENOSPC;
4857 
4858 	memset(sdp, 0, size);
4859 
4860 	/*
4861 	 * Prepare VSC Header for SU as per DP 1.4a spec, Table 2-119
4862 	 * VSC SDP Header Bytes
4863 	 */
4864 	sdp->sdp_header.HB0 = 0; /* Secondary-Data Packet ID = 0 */
4865 	sdp->sdp_header.HB1 = vsc->sdp_type; /* Secondary-data Packet Type */
4866 	sdp->sdp_header.HB2 = vsc->revision; /* Revision Number */
4867 	sdp->sdp_header.HB3 = vsc->length; /* Number of Valid Data Bytes */
4868 
4869 	/*
4870 	 * Only revision 0x5 supports Pixel Encoding/Colorimetry Format as
4871 	 * per DP 1.4a spec.
4872 	 */
4873 	if (vsc->revision != 0x5)
4874 		goto out;
4875 
4876 	/* VSC SDP Payload for DB16 through DB18 */
4877 	/* Pixel Encoding and Colorimetry Formats  */
4878 	sdp->db[16] = (vsc->pixelformat & 0xf) << 4; /* DB16[7:4] */
4879 	sdp->db[16] |= vsc->colorimetry & 0xf; /* DB16[3:0] */
4880 
4881 	switch (vsc->bpc) {
4882 	case 6:
4883 		/* 6bpc: 0x0 */
4884 		break;
4885 	case 8:
4886 		sdp->db[17] = 0x1; /* DB17[3:0] */
4887 		break;
4888 	case 10:
4889 		sdp->db[17] = 0x2;
4890 		break;
4891 	case 12:
4892 		sdp->db[17] = 0x3;
4893 		break;
4894 	case 16:
4895 		sdp->db[17] = 0x4;
4896 		break;
4897 	default:
4898 		MISSING_CASE(vsc->bpc);
4899 		break;
4900 	}
4901 	/* Dynamic Range and Component Bit Depth */
4902 	if (vsc->dynamic_range == DP_DYNAMIC_RANGE_CTA)
4903 		sdp->db[17] |= 0x80;  /* DB17[7] */
4904 
4905 	/* Content Type */
4906 	sdp->db[18] = vsc->content_type & 0x7;
4907 
4908 out:
4909 	return length;
4910 }
4911 
4912 static ssize_t
4913 intel_dp_hdr_metadata_infoframe_sdp_pack(const struct hdmi_drm_infoframe *drm_infoframe,
4914 					 struct dp_sdp *sdp,
4915 					 size_t size)
4916 {
4917 	size_t length = sizeof(struct dp_sdp);
4918 	const int infoframe_size = HDMI_INFOFRAME_HEADER_SIZE + HDMI_DRM_INFOFRAME_SIZE;
4919 	unsigned char buf[HDMI_INFOFRAME_HEADER_SIZE + HDMI_DRM_INFOFRAME_SIZE];
4920 	ssize_t len;
4921 
4922 	if (size < length)
4923 		return -ENOSPC;
4924 
4925 	memset(sdp, 0, size);
4926 
4927 	len = hdmi_drm_infoframe_pack_only(drm_infoframe, buf, sizeof(buf));
4928 	if (len < 0) {
4929 		DRM_DEBUG_KMS("buffer size is smaller than hdr metadata infoframe\n");
4930 		return -ENOSPC;
4931 	}
4932 
4933 	if (len != infoframe_size) {
4934 		DRM_DEBUG_KMS("wrong static hdr metadata size\n");
4935 		return -ENOSPC;
4936 	}
4937 
4938 	/*
4939 	 * Set up the infoframe sdp packet for HDR static metadata.
4940 	 * Prepare VSC Header for SU as per DP 1.4a spec,
4941 	 * Table 2-100 and Table 2-101
4942 	 */
4943 
4944 	/* Secondary-Data Packet ID, 00h for non-Audio INFOFRAME */
4945 	sdp->sdp_header.HB0 = 0;
4946 	/*
4947 	 * Packet Type 80h + Non-audio INFOFRAME Type value
4948 	 * HDMI_INFOFRAME_TYPE_DRM: 0x87
4949 	 * - 80h + Non-audio INFOFRAME Type value
4950 	 * - InfoFrame Type: 0x07
4951 	 *    [CTA-861-G Table-42 Dynamic Range and Mastering InfoFrame]
4952 	 */
4953 	sdp->sdp_header.HB1 = drm_infoframe->type;
4954 	/*
4955 	 * Least Significant Eight Bits of (Data Byte Count – 1)
4956 	 * infoframe_size - 1
4957 	 */
4958 	sdp->sdp_header.HB2 = 0x1D;
4959 	/* INFOFRAME SDP Version Number */
4960 	sdp->sdp_header.HB3 = (0x13 << 2);
4961 	/* CTA Header Byte 2 (INFOFRAME Version Number) */
4962 	sdp->db[0] = drm_infoframe->version;
4963 	/* CTA Header Byte 3 (Length of INFOFRAME): HDMI_DRM_INFOFRAME_SIZE */
4964 	sdp->db[1] = drm_infoframe->length;
4965 	/*
4966 	 * Copy HDMI_DRM_INFOFRAME_SIZE size from a buffer after
4967 	 * HDMI_INFOFRAME_HEADER_SIZE
4968 	 */
4969 	BUILD_BUG_ON(sizeof(sdp->db) < HDMI_DRM_INFOFRAME_SIZE + 2);
4970 	memcpy(&sdp->db[2], &buf[HDMI_INFOFRAME_HEADER_SIZE],
4971 	       HDMI_DRM_INFOFRAME_SIZE);
4972 
4973 	/*
4974 	 * Size of DP infoframe sdp packet for HDR static metadata consists of
4975 	 * - DP SDP Header(struct dp_sdp_header): 4 bytes
4976 	 * - Two Data Blocks: 2 bytes
4977 	 *    CTA Header Byte2 (INFOFRAME Version Number)
4978 	 *    CTA Header Byte3 (Length of INFOFRAME)
4979 	 * - HDMI_DRM_INFOFRAME_SIZE: 26 bytes
4980 	 *
4981 	 * Prior to GEN11's GMP register size is identical to DP HDR static metadata
4982 	 * infoframe size. But GEN11+ has larger than that size, write_infoframe
4983 	 * will pad rest of the size.
4984 	 */
4985 	return sizeof(struct dp_sdp_header) + 2 + HDMI_DRM_INFOFRAME_SIZE;
4986 }
4987 
4988 static void intel_write_dp_sdp(struct intel_encoder *encoder,
4989 			       const struct intel_crtc_state *crtc_state,
4990 			       unsigned int type)
4991 {
4992 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
4993 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4994 	struct dp_sdp sdp = {};
4995 	ssize_t len;
4996 
4997 	if ((crtc_state->infoframes.enable &
4998 	     intel_hdmi_infoframe_enable(type)) == 0)
4999 		return;
5000 
5001 	switch (type) {
5002 	case DP_SDP_VSC:
5003 		len = intel_dp_vsc_sdp_pack(&crtc_state->infoframes.vsc, &sdp,
5004 					    sizeof(sdp));
5005 		break;
5006 	case HDMI_PACKET_TYPE_GAMUT_METADATA:
5007 		len = intel_dp_hdr_metadata_infoframe_sdp_pack(&crtc_state->infoframes.drm.drm,
5008 							       &sdp, sizeof(sdp));
5009 		break;
5010 	default:
5011 		MISSING_CASE(type);
5012 		return;
5013 	}
5014 
5015 	if (drm_WARN_ON(&dev_priv->drm, len < 0))
5016 		return;
5017 
5018 	dig_port->write_infoframe(encoder, crtc_state, type, &sdp, len);
5019 }
5020 
5021 void intel_write_dp_vsc_sdp(struct intel_encoder *encoder,
5022 			    const struct intel_crtc_state *crtc_state,
5023 			    struct drm_dp_vsc_sdp *vsc)
5024 {
5025 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
5026 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
5027 	struct dp_sdp sdp = {};
5028 	ssize_t len;
5029 
5030 	len = intel_dp_vsc_sdp_pack(vsc, &sdp, sizeof(sdp));
5031 
5032 	if (drm_WARN_ON(&dev_priv->drm, len < 0))
5033 		return;
5034 
5035 	dig_port->write_infoframe(encoder, crtc_state, DP_SDP_VSC,
5036 					&sdp, len);
5037 }
5038 
5039 void intel_dp_set_infoframes(struct intel_encoder *encoder,
5040 			     bool enable,
5041 			     const struct intel_crtc_state *crtc_state,
5042 			     const struct drm_connector_state *conn_state)
5043 {
5044 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
5045 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
5046 	i915_reg_t reg = HSW_TVIDEO_DIP_CTL(crtc_state->cpu_transcoder);
5047 	u32 dip_enable = VIDEO_DIP_ENABLE_AVI_HSW | VIDEO_DIP_ENABLE_GCP_HSW |
5048 			 VIDEO_DIP_ENABLE_VS_HSW | VIDEO_DIP_ENABLE_GMP_HSW |
5049 			 VIDEO_DIP_ENABLE_SPD_HSW | VIDEO_DIP_ENABLE_DRM_GLK;
5050 	u32 val = intel_de_read(dev_priv, reg);
5051 
5052 	/* TODO: Add DSC case (DIP_ENABLE_PPS) */
5053 	/* When PSR is enabled, this routine doesn't disable VSC DIP */
5054 	if (intel_psr_enabled(intel_dp))
5055 		val &= ~dip_enable;
5056 	else
5057 		val &= ~(dip_enable | VIDEO_DIP_ENABLE_VSC_HSW);
5058 
5059 	if (!enable) {
5060 		intel_de_write(dev_priv, reg, val);
5061 		intel_de_posting_read(dev_priv, reg);
5062 		return;
5063 	}
5064 
5065 	intel_de_write(dev_priv, reg, val);
5066 	intel_de_posting_read(dev_priv, reg);
5067 
5068 	/* When PSR is enabled, VSC SDP is handled by PSR routine */
5069 	if (!intel_psr_enabled(intel_dp))
5070 		intel_write_dp_sdp(encoder, crtc_state, DP_SDP_VSC);
5071 
5072 	intel_write_dp_sdp(encoder, crtc_state, HDMI_PACKET_TYPE_GAMUT_METADATA);
5073 }
5074 
5075 static int intel_dp_vsc_sdp_unpack(struct drm_dp_vsc_sdp *vsc,
5076 				   const void *buffer, size_t size)
5077 {
5078 	const struct dp_sdp *sdp = buffer;
5079 
5080 	if (size < sizeof(struct dp_sdp))
5081 		return -EINVAL;
5082 
5083 	memset(vsc, 0, sizeof(*vsc));
5084 
5085 	if (sdp->sdp_header.HB0 != 0)
5086 		return -EINVAL;
5087 
5088 	if (sdp->sdp_header.HB1 != DP_SDP_VSC)
5089 		return -EINVAL;
5090 
5091 	vsc->sdp_type = sdp->sdp_header.HB1;
5092 	vsc->revision = sdp->sdp_header.HB2;
5093 	vsc->length = sdp->sdp_header.HB3;
5094 
5095 	if ((sdp->sdp_header.HB2 == 0x2 && sdp->sdp_header.HB3 == 0x8) ||
5096 	    (sdp->sdp_header.HB2 == 0x4 && sdp->sdp_header.HB3 == 0xe)) {
5097 		/*
5098 		 * - HB2 = 0x2, HB3 = 0x8
5099 		 *   VSC SDP supporting 3D stereo + PSR
5100 		 * - HB2 = 0x4, HB3 = 0xe
5101 		 *   VSC SDP supporting 3D stereo + PSR2 with Y-coordinate of
5102 		 *   first scan line of the SU region (applies to eDP v1.4b
5103 		 *   and higher).
5104 		 */
5105 		return 0;
5106 	} else if (sdp->sdp_header.HB2 == 0x5 && sdp->sdp_header.HB3 == 0x13) {
5107 		/*
5108 		 * - HB2 = 0x5, HB3 = 0x13
5109 		 *   VSC SDP supporting 3D stereo + PSR2 + Pixel Encoding/Colorimetry
5110 		 *   Format.
5111 		 */
5112 		vsc->pixelformat = (sdp->db[16] >> 4) & 0xf;
5113 		vsc->colorimetry = sdp->db[16] & 0xf;
5114 		vsc->dynamic_range = (sdp->db[17] >> 7) & 0x1;
5115 
5116 		switch (sdp->db[17] & 0x7) {
5117 		case 0x0:
5118 			vsc->bpc = 6;
5119 			break;
5120 		case 0x1:
5121 			vsc->bpc = 8;
5122 			break;
5123 		case 0x2:
5124 			vsc->bpc = 10;
5125 			break;
5126 		case 0x3:
5127 			vsc->bpc = 12;
5128 			break;
5129 		case 0x4:
5130 			vsc->bpc = 16;
5131 			break;
5132 		default:
5133 			MISSING_CASE(sdp->db[17] & 0x7);
5134 			return -EINVAL;
5135 		}
5136 
5137 		vsc->content_type = sdp->db[18] & 0x7;
5138 	} else {
5139 		return -EINVAL;
5140 	}
5141 
5142 	return 0;
5143 }
5144 
5145 static int
5146 intel_dp_hdr_metadata_infoframe_sdp_unpack(struct hdmi_drm_infoframe *drm_infoframe,
5147 					   const void *buffer, size_t size)
5148 {
5149 	int ret;
5150 
5151 	const struct dp_sdp *sdp = buffer;
5152 
5153 	if (size < sizeof(struct dp_sdp))
5154 		return -EINVAL;
5155 
5156 	if (sdp->sdp_header.HB0 != 0)
5157 		return -EINVAL;
5158 
5159 	if (sdp->sdp_header.HB1 != HDMI_INFOFRAME_TYPE_DRM)
5160 		return -EINVAL;
5161 
5162 	/*
5163 	 * Least Significant Eight Bits of (Data Byte Count – 1)
5164 	 * 1Dh (i.e., Data Byte Count = 30 bytes).
5165 	 */
5166 	if (sdp->sdp_header.HB2 != 0x1D)
5167 		return -EINVAL;
5168 
5169 	/* Most Significant Two Bits of (Data Byte Count – 1), Clear to 00b. */
5170 	if ((sdp->sdp_header.HB3 & 0x3) != 0)
5171 		return -EINVAL;
5172 
5173 	/* INFOFRAME SDP Version Number */
5174 	if (((sdp->sdp_header.HB3 >> 2) & 0x3f) != 0x13)
5175 		return -EINVAL;
5176 
5177 	/* CTA Header Byte 2 (INFOFRAME Version Number) */
5178 	if (sdp->db[0] != 1)
5179 		return -EINVAL;
5180 
5181 	/* CTA Header Byte 3 (Length of INFOFRAME): HDMI_DRM_INFOFRAME_SIZE */
5182 	if (sdp->db[1] != HDMI_DRM_INFOFRAME_SIZE)
5183 		return -EINVAL;
5184 
5185 	ret = hdmi_drm_infoframe_unpack_only(drm_infoframe, &sdp->db[2],
5186 					     HDMI_DRM_INFOFRAME_SIZE);
5187 
5188 	return ret;
5189 }
5190 
5191 static void intel_read_dp_vsc_sdp(struct intel_encoder *encoder,
5192 				  struct intel_crtc_state *crtc_state,
5193 				  struct drm_dp_vsc_sdp *vsc)
5194 {
5195 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
5196 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
5197 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
5198 	unsigned int type = DP_SDP_VSC;
5199 	struct dp_sdp sdp = {};
5200 	int ret;
5201 
5202 	/* When PSR is enabled, VSC SDP is handled by PSR routine */
5203 	if (intel_psr_enabled(intel_dp))
5204 		return;
5205 
5206 	if ((crtc_state->infoframes.enable &
5207 	     intel_hdmi_infoframe_enable(type)) == 0)
5208 		return;
5209 
5210 	dig_port->read_infoframe(encoder, crtc_state, type, &sdp, sizeof(sdp));
5211 
5212 	ret = intel_dp_vsc_sdp_unpack(vsc, &sdp, sizeof(sdp));
5213 
5214 	if (ret)
5215 		drm_dbg_kms(&dev_priv->drm, "Failed to unpack DP VSC SDP\n");
5216 }
5217 
5218 static void intel_read_dp_hdr_metadata_infoframe_sdp(struct intel_encoder *encoder,
5219 						     struct intel_crtc_state *crtc_state,
5220 						     struct hdmi_drm_infoframe *drm_infoframe)
5221 {
5222 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
5223 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
5224 	unsigned int type = HDMI_PACKET_TYPE_GAMUT_METADATA;
5225 	struct dp_sdp sdp = {};
5226 	int ret;
5227 
5228 	if ((crtc_state->infoframes.enable &
5229 	    intel_hdmi_infoframe_enable(type)) == 0)
5230 		return;
5231 
5232 	dig_port->read_infoframe(encoder, crtc_state, type, &sdp,
5233 				 sizeof(sdp));
5234 
5235 	ret = intel_dp_hdr_metadata_infoframe_sdp_unpack(drm_infoframe, &sdp,
5236 							 sizeof(sdp));
5237 
5238 	if (ret)
5239 		drm_dbg_kms(&dev_priv->drm,
5240 			    "Failed to unpack DP HDR Metadata Infoframe SDP\n");
5241 }
5242 
5243 void intel_read_dp_sdp(struct intel_encoder *encoder,
5244 		       struct intel_crtc_state *crtc_state,
5245 		       unsigned int type)
5246 {
5247 	if (encoder->type != INTEL_OUTPUT_DDI)
5248 		return;
5249 
5250 	switch (type) {
5251 	case DP_SDP_VSC:
5252 		intel_read_dp_vsc_sdp(encoder, crtc_state,
5253 				      &crtc_state->infoframes.vsc);
5254 		break;
5255 	case HDMI_PACKET_TYPE_GAMUT_METADATA:
5256 		intel_read_dp_hdr_metadata_infoframe_sdp(encoder, crtc_state,
5257 							 &crtc_state->infoframes.drm.drm);
5258 		break;
5259 	default:
5260 		MISSING_CASE(type);
5261 		break;
5262 	}
5263 }
5264 
5265 static u8 intel_dp_autotest_link_training(struct intel_dp *intel_dp)
5266 {
5267 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
5268 	int status = 0;
5269 	int test_link_rate;
5270 	u8 test_lane_count, test_link_bw;
5271 	/* (DP CTS 1.2)
5272 	 * 4.3.1.11
5273 	 */
5274 	/* Read the TEST_LANE_COUNT and TEST_LINK_RTAE fields (DP CTS 3.1.4) */
5275 	status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_LANE_COUNT,
5276 				   &test_lane_count);
5277 
5278 	if (status <= 0) {
5279 		drm_dbg_kms(&i915->drm, "Lane count read failed\n");
5280 		return DP_TEST_NAK;
5281 	}
5282 	test_lane_count &= DP_MAX_LANE_COUNT_MASK;
5283 
5284 	status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_LINK_RATE,
5285 				   &test_link_bw);
5286 	if (status <= 0) {
5287 		drm_dbg_kms(&i915->drm, "Link Rate read failed\n");
5288 		return DP_TEST_NAK;
5289 	}
5290 	test_link_rate = drm_dp_bw_code_to_link_rate(test_link_bw);
5291 
5292 	/* Validate the requested link rate and lane count */
5293 	if (!intel_dp_link_params_valid(intel_dp, test_link_rate,
5294 					test_lane_count))
5295 		return DP_TEST_NAK;
5296 
5297 	intel_dp->compliance.test_lane_count = test_lane_count;
5298 	intel_dp->compliance.test_link_rate = test_link_rate;
5299 
5300 	return DP_TEST_ACK;
5301 }
5302 
5303 static u8 intel_dp_autotest_video_pattern(struct intel_dp *intel_dp)
5304 {
5305 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
5306 	u8 test_pattern;
5307 	u8 test_misc;
5308 	__be16 h_width, v_height;
5309 	int status = 0;
5310 
5311 	/* Read the TEST_PATTERN (DP CTS 3.1.5) */
5312 	status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_PATTERN,
5313 				   &test_pattern);
5314 	if (status <= 0) {
5315 		drm_dbg_kms(&i915->drm, "Test pattern read failed\n");
5316 		return DP_TEST_NAK;
5317 	}
5318 	if (test_pattern != DP_COLOR_RAMP)
5319 		return DP_TEST_NAK;
5320 
5321 	status = drm_dp_dpcd_read(&intel_dp->aux, DP_TEST_H_WIDTH_HI,
5322 				  &h_width, 2);
5323 	if (status <= 0) {
5324 		drm_dbg_kms(&i915->drm, "H Width read failed\n");
5325 		return DP_TEST_NAK;
5326 	}
5327 
5328 	status = drm_dp_dpcd_read(&intel_dp->aux, DP_TEST_V_HEIGHT_HI,
5329 				  &v_height, 2);
5330 	if (status <= 0) {
5331 		drm_dbg_kms(&i915->drm, "V Height read failed\n");
5332 		return DP_TEST_NAK;
5333 	}
5334 
5335 	status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_MISC0,
5336 				   &test_misc);
5337 	if (status <= 0) {
5338 		drm_dbg_kms(&i915->drm, "TEST MISC read failed\n");
5339 		return DP_TEST_NAK;
5340 	}
5341 	if ((test_misc & DP_TEST_COLOR_FORMAT_MASK) != DP_COLOR_FORMAT_RGB)
5342 		return DP_TEST_NAK;
5343 	if (test_misc & DP_TEST_DYNAMIC_RANGE_CEA)
5344 		return DP_TEST_NAK;
5345 	switch (test_misc & DP_TEST_BIT_DEPTH_MASK) {
5346 	case DP_TEST_BIT_DEPTH_6:
5347 		intel_dp->compliance.test_data.bpc = 6;
5348 		break;
5349 	case DP_TEST_BIT_DEPTH_8:
5350 		intel_dp->compliance.test_data.bpc = 8;
5351 		break;
5352 	default:
5353 		return DP_TEST_NAK;
5354 	}
5355 
5356 	intel_dp->compliance.test_data.video_pattern = test_pattern;
5357 	intel_dp->compliance.test_data.hdisplay = be16_to_cpu(h_width);
5358 	intel_dp->compliance.test_data.vdisplay = be16_to_cpu(v_height);
5359 	/* Set test active flag here so userspace doesn't interrupt things */
5360 	intel_dp->compliance.test_active = true;
5361 
5362 	return DP_TEST_ACK;
5363 }
5364 
5365 static u8 intel_dp_autotest_edid(struct intel_dp *intel_dp)
5366 {
5367 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
5368 	u8 test_result = DP_TEST_ACK;
5369 	struct intel_connector *intel_connector = intel_dp->attached_connector;
5370 	struct drm_connector *connector = &intel_connector->base;
5371 
5372 	if (intel_connector->detect_edid == NULL ||
5373 	    connector->edid_corrupt ||
5374 	    intel_dp->aux.i2c_defer_count > 6) {
5375 		/* Check EDID read for NACKs, DEFERs and corruption
5376 		 * (DP CTS 1.2 Core r1.1)
5377 		 *    4.2.2.4 : Failed EDID read, I2C_NAK
5378 		 *    4.2.2.5 : Failed EDID read, I2C_DEFER
5379 		 *    4.2.2.6 : EDID corruption detected
5380 		 * Use failsafe mode for all cases
5381 		 */
5382 		if (intel_dp->aux.i2c_nack_count > 0 ||
5383 			intel_dp->aux.i2c_defer_count > 0)
5384 			drm_dbg_kms(&i915->drm,
5385 				    "EDID read had %d NACKs, %d DEFERs\n",
5386 				    intel_dp->aux.i2c_nack_count,
5387 				    intel_dp->aux.i2c_defer_count);
5388 		intel_dp->compliance.test_data.edid = INTEL_DP_RESOLUTION_FAILSAFE;
5389 	} else {
5390 		struct edid *block = intel_connector->detect_edid;
5391 
5392 		/* We have to write the checksum
5393 		 * of the last block read
5394 		 */
5395 		block += intel_connector->detect_edid->extensions;
5396 
5397 		if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_EDID_CHECKSUM,
5398 				       block->checksum) <= 0)
5399 			drm_dbg_kms(&i915->drm,
5400 				    "Failed to write EDID checksum\n");
5401 
5402 		test_result = DP_TEST_ACK | DP_TEST_EDID_CHECKSUM_WRITE;
5403 		intel_dp->compliance.test_data.edid = INTEL_DP_RESOLUTION_PREFERRED;
5404 	}
5405 
5406 	/* Set test active flag here so userspace doesn't interrupt things */
5407 	intel_dp->compliance.test_active = true;
5408 
5409 	return test_result;
5410 }
5411 
5412 static u8 intel_dp_prepare_phytest(struct intel_dp *intel_dp)
5413 {
5414 	struct drm_dp_phy_test_params *data =
5415 		&intel_dp->compliance.test_data.phytest;
5416 
5417 	if (drm_dp_get_phy_test_pattern(&intel_dp->aux, data)) {
5418 		DRM_DEBUG_KMS("DP Phy Test pattern AUX read failure\n");
5419 		return DP_TEST_NAK;
5420 	}
5421 
5422 	/*
5423 	 * link_mst is set to false to avoid executing mst related code
5424 	 * during compliance testing.
5425 	 */
5426 	intel_dp->link_mst = false;
5427 
5428 	return DP_TEST_ACK;
5429 }
5430 
5431 static void intel_dp_phy_pattern_update(struct intel_dp *intel_dp)
5432 {
5433 	struct drm_i915_private *dev_priv =
5434 			to_i915(dp_to_dig_port(intel_dp)->base.base.dev);
5435 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
5436 	struct drm_dp_phy_test_params *data =
5437 			&intel_dp->compliance.test_data.phytest;
5438 	struct intel_crtc *crtc = to_intel_crtc(dig_port->base.base.crtc);
5439 	enum pipe pipe = crtc->pipe;
5440 	u32 pattern_val;
5441 
5442 	switch (data->phy_pattern) {
5443 	case DP_PHY_TEST_PATTERN_NONE:
5444 		DRM_DEBUG_KMS("Disable Phy Test Pattern\n");
5445 		intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe), 0x0);
5446 		break;
5447 	case DP_PHY_TEST_PATTERN_D10_2:
5448 		DRM_DEBUG_KMS("Set D10.2 Phy Test Pattern\n");
5449 		intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe),
5450 			       DDI_DP_COMP_CTL_ENABLE | DDI_DP_COMP_CTL_D10_2);
5451 		break;
5452 	case DP_PHY_TEST_PATTERN_ERROR_COUNT:
5453 		DRM_DEBUG_KMS("Set Error Count Phy Test Pattern\n");
5454 		intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe),
5455 			       DDI_DP_COMP_CTL_ENABLE |
5456 			       DDI_DP_COMP_CTL_SCRAMBLED_0);
5457 		break;
5458 	case DP_PHY_TEST_PATTERN_PRBS7:
5459 		DRM_DEBUG_KMS("Set PRBS7 Phy Test Pattern\n");
5460 		intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe),
5461 			       DDI_DP_COMP_CTL_ENABLE | DDI_DP_COMP_CTL_PRBS7);
5462 		break;
5463 	case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
5464 		/*
5465 		 * FIXME: Ideally pattern should come from DPCD 0x250. As
5466 		 * current firmware of DPR-100 could not set it, so hardcoding
5467 		 * now for complaince test.
5468 		 */
5469 		DRM_DEBUG_KMS("Set 80Bit Custom Phy Test Pattern 0x3e0f83e0 0x0f83e0f8 0x0000f83e\n");
5470 		pattern_val = 0x3e0f83e0;
5471 		intel_de_write(dev_priv, DDI_DP_COMP_PAT(pipe, 0), pattern_val);
5472 		pattern_val = 0x0f83e0f8;
5473 		intel_de_write(dev_priv, DDI_DP_COMP_PAT(pipe, 1), pattern_val);
5474 		pattern_val = 0x0000f83e;
5475 		intel_de_write(dev_priv, DDI_DP_COMP_PAT(pipe, 2), pattern_val);
5476 		intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe),
5477 			       DDI_DP_COMP_CTL_ENABLE |
5478 			       DDI_DP_COMP_CTL_CUSTOM80);
5479 		break;
5480 	case DP_PHY_TEST_PATTERN_CP2520:
5481 		/*
5482 		 * FIXME: Ideally pattern should come from DPCD 0x24A. As
5483 		 * current firmware of DPR-100 could not set it, so hardcoding
5484 		 * now for complaince test.
5485 		 */
5486 		DRM_DEBUG_KMS("Set HBR2 compliance Phy Test Pattern\n");
5487 		pattern_val = 0xFB;
5488 		intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe),
5489 			       DDI_DP_COMP_CTL_ENABLE | DDI_DP_COMP_CTL_HBR2 |
5490 			       pattern_val);
5491 		break;
5492 	default:
5493 		WARN(1, "Invalid Phy Test Pattern\n");
5494 	}
5495 }
5496 
5497 static void
5498 intel_dp_autotest_phy_ddi_disable(struct intel_dp *intel_dp)
5499 {
5500 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
5501 	struct drm_device *dev = dig_port->base.base.dev;
5502 	struct drm_i915_private *dev_priv = to_i915(dev);
5503 	struct intel_crtc *crtc = to_intel_crtc(dig_port->base.base.crtc);
5504 	enum pipe pipe = crtc->pipe;
5505 	u32 trans_ddi_func_ctl_value, trans_conf_value, dp_tp_ctl_value;
5506 
5507 	trans_ddi_func_ctl_value = intel_de_read(dev_priv,
5508 						 TRANS_DDI_FUNC_CTL(pipe));
5509 	trans_conf_value = intel_de_read(dev_priv, PIPECONF(pipe));
5510 	dp_tp_ctl_value = intel_de_read(dev_priv, TGL_DP_TP_CTL(pipe));
5511 
5512 	trans_ddi_func_ctl_value &= ~(TRANS_DDI_FUNC_ENABLE |
5513 				      TGL_TRANS_DDI_PORT_MASK);
5514 	trans_conf_value &= ~PIPECONF_ENABLE;
5515 	dp_tp_ctl_value &= ~DP_TP_CTL_ENABLE;
5516 
5517 	intel_de_write(dev_priv, PIPECONF(pipe), trans_conf_value);
5518 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(pipe),
5519 		       trans_ddi_func_ctl_value);
5520 	intel_de_write(dev_priv, TGL_DP_TP_CTL(pipe), dp_tp_ctl_value);
5521 }
5522 
5523 static void
5524 intel_dp_autotest_phy_ddi_enable(struct intel_dp *intel_dp, uint8_t lane_cnt)
5525 {
5526 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
5527 	struct drm_device *dev = dig_port->base.base.dev;
5528 	struct drm_i915_private *dev_priv = to_i915(dev);
5529 	enum port port = dig_port->base.port;
5530 	struct intel_crtc *crtc = to_intel_crtc(dig_port->base.base.crtc);
5531 	enum pipe pipe = crtc->pipe;
5532 	u32 trans_ddi_func_ctl_value, trans_conf_value, dp_tp_ctl_value;
5533 
5534 	trans_ddi_func_ctl_value = intel_de_read(dev_priv,
5535 						 TRANS_DDI_FUNC_CTL(pipe));
5536 	trans_conf_value = intel_de_read(dev_priv, PIPECONF(pipe));
5537 	dp_tp_ctl_value = intel_de_read(dev_priv, TGL_DP_TP_CTL(pipe));
5538 
5539 	trans_ddi_func_ctl_value |= TRANS_DDI_FUNC_ENABLE |
5540 				    TGL_TRANS_DDI_SELECT_PORT(port);
5541 	trans_conf_value |= PIPECONF_ENABLE;
5542 	dp_tp_ctl_value |= DP_TP_CTL_ENABLE;
5543 
5544 	intel_de_write(dev_priv, PIPECONF(pipe), trans_conf_value);
5545 	intel_de_write(dev_priv, TGL_DP_TP_CTL(pipe), dp_tp_ctl_value);
5546 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(pipe),
5547 		       trans_ddi_func_ctl_value);
5548 }
5549 
5550 void intel_dp_process_phy_request(struct intel_dp *intel_dp)
5551 {
5552 	struct drm_dp_phy_test_params *data =
5553 		&intel_dp->compliance.test_data.phytest;
5554 	u8 link_status[DP_LINK_STATUS_SIZE];
5555 
5556 	if (!intel_dp_get_link_status(intel_dp, link_status)) {
5557 		DRM_DEBUG_KMS("failed to get link status\n");
5558 		return;
5559 	}
5560 
5561 	/* retrieve vswing & pre-emphasis setting */
5562 	intel_dp_get_adjust_train(intel_dp, link_status);
5563 
5564 	intel_dp_autotest_phy_ddi_disable(intel_dp);
5565 
5566 	intel_dp_set_signal_levels(intel_dp);
5567 
5568 	intel_dp_phy_pattern_update(intel_dp);
5569 
5570 	intel_dp_autotest_phy_ddi_enable(intel_dp, data->num_lanes);
5571 
5572 	drm_dp_set_phy_test_pattern(&intel_dp->aux, data,
5573 				    link_status[DP_DPCD_REV]);
5574 }
5575 
5576 static u8 intel_dp_autotest_phy_pattern(struct intel_dp *intel_dp)
5577 {
5578 	u8 test_result;
5579 
5580 	test_result = intel_dp_prepare_phytest(intel_dp);
5581 	if (test_result != DP_TEST_ACK)
5582 		DRM_ERROR("Phy test preparation failed\n");
5583 
5584 	intel_dp_process_phy_request(intel_dp);
5585 
5586 	return test_result;
5587 }
5588 
5589 static void intel_dp_handle_test_request(struct intel_dp *intel_dp)
5590 {
5591 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
5592 	u8 response = DP_TEST_NAK;
5593 	u8 request = 0;
5594 	int status;
5595 
5596 	status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_REQUEST, &request);
5597 	if (status <= 0) {
5598 		drm_dbg_kms(&i915->drm,
5599 			    "Could not read test request from sink\n");
5600 		goto update_status;
5601 	}
5602 
5603 	switch (request) {
5604 	case DP_TEST_LINK_TRAINING:
5605 		drm_dbg_kms(&i915->drm, "LINK_TRAINING test requested\n");
5606 		response = intel_dp_autotest_link_training(intel_dp);
5607 		break;
5608 	case DP_TEST_LINK_VIDEO_PATTERN:
5609 		drm_dbg_kms(&i915->drm, "TEST_PATTERN test requested\n");
5610 		response = intel_dp_autotest_video_pattern(intel_dp);
5611 		break;
5612 	case DP_TEST_LINK_EDID_READ:
5613 		drm_dbg_kms(&i915->drm, "EDID test requested\n");
5614 		response = intel_dp_autotest_edid(intel_dp);
5615 		break;
5616 	case DP_TEST_LINK_PHY_TEST_PATTERN:
5617 		drm_dbg_kms(&i915->drm, "PHY_PATTERN test requested\n");
5618 		response = intel_dp_autotest_phy_pattern(intel_dp);
5619 		break;
5620 	default:
5621 		drm_dbg_kms(&i915->drm, "Invalid test request '%02x'\n",
5622 			    request);
5623 		break;
5624 	}
5625 
5626 	if (response & DP_TEST_ACK)
5627 		intel_dp->compliance.test_type = request;
5628 
5629 update_status:
5630 	status = drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_RESPONSE, response);
5631 	if (status <= 0)
5632 		drm_dbg_kms(&i915->drm,
5633 			    "Could not write test response to sink\n");
5634 }
5635 
5636 /**
5637  * intel_dp_check_mst_status - service any pending MST interrupts, check link status
5638  * @intel_dp: Intel DP struct
5639  *
5640  * Read any pending MST interrupts, call MST core to handle these and ack the
5641  * interrupts. Check if the main and AUX link state is ok.
5642  *
5643  * Returns:
5644  * - %true if pending interrupts were serviced (or no interrupts were
5645  *   pending) w/o detecting an error condition.
5646  * - %false if an error condition - like AUX failure or a loss of link - is
5647  *   detected, which needs servicing from the hotplug work.
5648  */
5649 static bool
5650 intel_dp_check_mst_status(struct intel_dp *intel_dp)
5651 {
5652 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
5653 	bool link_ok = true;
5654 
5655 	drm_WARN_ON_ONCE(&i915->drm, intel_dp->active_mst_links < 0);
5656 
5657 	for (;;) {
5658 		/*
5659 		 * The +2 is because DP_DPRX_ESI_LEN is 14, but we then
5660 		 * pass in "esi+10" to drm_dp_channel_eq_ok(), which
5661 		 * takes a 6-byte array. So we actually need 16 bytes
5662 		 * here.
5663 		 *
5664 		 * Somebody who knows what the limits actually are
5665 		 * should check this, but for now this is at least
5666 		 * harmless and avoids a valid compiler warning about
5667 		 * using more of the array than we have allocated.
5668 		 */
5669 		u8 esi[DP_DPRX_ESI_LEN+2] = {};
5670 		bool handled;
5671 		int retry;
5672 
5673 		if (!intel_dp_get_sink_irq_esi(intel_dp, esi)) {
5674 			drm_dbg_kms(&i915->drm,
5675 				    "failed to get ESI - device may have failed\n");
5676 			link_ok = false;
5677 
5678 			break;
5679 		}
5680 
5681 		/* check link status - esi[10] = 0x200c */
5682 		if (intel_dp->active_mst_links > 0 && link_ok &&
5683 		    !drm_dp_channel_eq_ok(&esi[10], intel_dp->lane_count)) {
5684 			drm_dbg_kms(&i915->drm,
5685 				    "channel EQ not ok, retraining\n");
5686 			link_ok = false;
5687 		}
5688 
5689 		drm_dbg_kms(&i915->drm, "got esi %3ph\n", esi);
5690 
5691 		drm_dp_mst_hpd_irq(&intel_dp->mst_mgr, esi, &handled);
5692 		if (!handled)
5693 			break;
5694 
5695 		for (retry = 0; retry < 3; retry++) {
5696 			int wret;
5697 
5698 			wret = drm_dp_dpcd_write(&intel_dp->aux,
5699 						 DP_SINK_COUNT_ESI+1,
5700 						 &esi[1], 3);
5701 			if (wret == 3)
5702 				break;
5703 		}
5704 	}
5705 
5706 	return link_ok;
5707 }
5708 
5709 static bool
5710 intel_dp_needs_link_retrain(struct intel_dp *intel_dp)
5711 {
5712 	u8 link_status[DP_LINK_STATUS_SIZE];
5713 
5714 	if (!intel_dp->link_trained)
5715 		return false;
5716 
5717 	/*
5718 	 * While PSR source HW is enabled, it will control main-link sending
5719 	 * frames, enabling and disabling it so trying to do a retrain will fail
5720 	 * as the link would or not be on or it could mix training patterns
5721 	 * and frame data at the same time causing retrain to fail.
5722 	 * Also when exiting PSR, HW will retrain the link anyways fixing
5723 	 * any link status error.
5724 	 */
5725 	if (intel_psr_enabled(intel_dp))
5726 		return false;
5727 
5728 	if (!intel_dp_get_link_status(intel_dp, link_status))
5729 		return false;
5730 
5731 	/*
5732 	 * Validate the cached values of intel_dp->link_rate and
5733 	 * intel_dp->lane_count before attempting to retrain.
5734 	 */
5735 	if (!intel_dp_link_params_valid(intel_dp, intel_dp->link_rate,
5736 					intel_dp->lane_count))
5737 		return false;
5738 
5739 	/* Retrain if Channel EQ or CR not ok */
5740 	return !drm_dp_channel_eq_ok(link_status, intel_dp->lane_count);
5741 }
5742 
5743 static bool intel_dp_has_connector(struct intel_dp *intel_dp,
5744 				   const struct drm_connector_state *conn_state)
5745 {
5746 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
5747 	struct intel_encoder *encoder;
5748 	enum pipe pipe;
5749 
5750 	if (!conn_state->best_encoder)
5751 		return false;
5752 
5753 	/* SST */
5754 	encoder = &dp_to_dig_port(intel_dp)->base;
5755 	if (conn_state->best_encoder == &encoder->base)
5756 		return true;
5757 
5758 	/* MST */
5759 	for_each_pipe(i915, pipe) {
5760 		encoder = &intel_dp->mst_encoders[pipe]->base;
5761 		if (conn_state->best_encoder == &encoder->base)
5762 			return true;
5763 	}
5764 
5765 	return false;
5766 }
5767 
5768 static int intel_dp_prep_link_retrain(struct intel_dp *intel_dp,
5769 				      struct drm_modeset_acquire_ctx *ctx,
5770 				      u32 *crtc_mask)
5771 {
5772 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
5773 	struct drm_connector_list_iter conn_iter;
5774 	struct intel_connector *connector;
5775 	int ret = 0;
5776 
5777 	*crtc_mask = 0;
5778 
5779 	if (!intel_dp_needs_link_retrain(intel_dp))
5780 		return 0;
5781 
5782 	drm_connector_list_iter_begin(&i915->drm, &conn_iter);
5783 	for_each_intel_connector_iter(connector, &conn_iter) {
5784 		struct drm_connector_state *conn_state =
5785 			connector->base.state;
5786 		struct intel_crtc_state *crtc_state;
5787 		struct intel_crtc *crtc;
5788 
5789 		if (!intel_dp_has_connector(intel_dp, conn_state))
5790 			continue;
5791 
5792 		crtc = to_intel_crtc(conn_state->crtc);
5793 		if (!crtc)
5794 			continue;
5795 
5796 		ret = drm_modeset_lock(&crtc->base.mutex, ctx);
5797 		if (ret)
5798 			break;
5799 
5800 		crtc_state = to_intel_crtc_state(crtc->base.state);
5801 
5802 		drm_WARN_ON(&i915->drm, !intel_crtc_has_dp_encoder(crtc_state));
5803 
5804 		if (!crtc_state->hw.active)
5805 			continue;
5806 
5807 		if (conn_state->commit &&
5808 		    !try_wait_for_completion(&conn_state->commit->hw_done))
5809 			continue;
5810 
5811 		*crtc_mask |= drm_crtc_mask(&crtc->base);
5812 	}
5813 	drm_connector_list_iter_end(&conn_iter);
5814 
5815 	if (!intel_dp_needs_link_retrain(intel_dp))
5816 		*crtc_mask = 0;
5817 
5818 	return ret;
5819 }
5820 
5821 static bool intel_dp_is_connected(struct intel_dp *intel_dp)
5822 {
5823 	struct intel_connector *connector = intel_dp->attached_connector;
5824 
5825 	return connector->base.status == connector_status_connected ||
5826 		intel_dp->is_mst;
5827 }
5828 
5829 int intel_dp_retrain_link(struct intel_encoder *encoder,
5830 			  struct drm_modeset_acquire_ctx *ctx)
5831 {
5832 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
5833 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
5834 	struct intel_crtc *crtc;
5835 	u32 crtc_mask;
5836 	int ret;
5837 
5838 	if (!intel_dp_is_connected(intel_dp))
5839 		return 0;
5840 
5841 	ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
5842 			       ctx);
5843 	if (ret)
5844 		return ret;
5845 
5846 	ret = intel_dp_prep_link_retrain(intel_dp, ctx, &crtc_mask);
5847 	if (ret)
5848 		return ret;
5849 
5850 	if (crtc_mask == 0)
5851 		return 0;
5852 
5853 	drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s] retraining link\n",
5854 		    encoder->base.base.id, encoder->base.name);
5855 
5856 	for_each_intel_crtc_mask(&dev_priv->drm, crtc, crtc_mask) {
5857 		const struct intel_crtc_state *crtc_state =
5858 			to_intel_crtc_state(crtc->base.state);
5859 
5860 		/* Suppress underruns caused by re-training */
5861 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
5862 		if (crtc_state->has_pch_encoder)
5863 			intel_set_pch_fifo_underrun_reporting(dev_priv,
5864 							      intel_crtc_pch_transcoder(crtc), false);
5865 	}
5866 
5867 	intel_dp_start_link_train(intel_dp);
5868 	intel_dp_stop_link_train(intel_dp);
5869 
5870 	for_each_intel_crtc_mask(&dev_priv->drm, crtc, crtc_mask) {
5871 		const struct intel_crtc_state *crtc_state =
5872 			to_intel_crtc_state(crtc->base.state);
5873 
5874 		/* Keep underrun reporting disabled until things are stable */
5875 		intel_wait_for_vblank(dev_priv, crtc->pipe);
5876 
5877 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
5878 		if (crtc_state->has_pch_encoder)
5879 			intel_set_pch_fifo_underrun_reporting(dev_priv,
5880 							      intel_crtc_pch_transcoder(crtc), true);
5881 	}
5882 
5883 	return 0;
5884 }
5885 
5886 /*
5887  * If display is now connected check links status,
5888  * there has been known issues of link loss triggering
5889  * long pulse.
5890  *
5891  * Some sinks (eg. ASUS PB287Q) seem to perform some
5892  * weird HPD ping pong during modesets. So we can apparently
5893  * end up with HPD going low during a modeset, and then
5894  * going back up soon after. And once that happens we must
5895  * retrain the link to get a picture. That's in case no
5896  * userspace component reacted to intermittent HPD dip.
5897  */
5898 static enum intel_hotplug_state
5899 intel_dp_hotplug(struct intel_encoder *encoder,
5900 		 struct intel_connector *connector)
5901 {
5902 	struct drm_modeset_acquire_ctx ctx;
5903 	enum intel_hotplug_state state;
5904 	int ret;
5905 
5906 	state = intel_encoder_hotplug(encoder, connector);
5907 
5908 	drm_modeset_acquire_init(&ctx, 0);
5909 
5910 	for (;;) {
5911 		ret = intel_dp_retrain_link(encoder, &ctx);
5912 
5913 		if (ret == -EDEADLK) {
5914 			drm_modeset_backoff(&ctx);
5915 			continue;
5916 		}
5917 
5918 		break;
5919 	}
5920 
5921 	drm_modeset_drop_locks(&ctx);
5922 	drm_modeset_acquire_fini(&ctx);
5923 	drm_WARN(encoder->base.dev, ret,
5924 		 "Acquiring modeset locks failed with %i\n", ret);
5925 
5926 	/*
5927 	 * Keeping it consistent with intel_ddi_hotplug() and
5928 	 * intel_hdmi_hotplug().
5929 	 */
5930 	if (state == INTEL_HOTPLUG_UNCHANGED && !connector->hotplug_retries)
5931 		state = INTEL_HOTPLUG_RETRY;
5932 
5933 	return state;
5934 }
5935 
5936 static void intel_dp_check_service_irq(struct intel_dp *intel_dp)
5937 {
5938 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
5939 	u8 val;
5940 
5941 	if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
5942 		return;
5943 
5944 	if (drm_dp_dpcd_readb(&intel_dp->aux,
5945 			      DP_DEVICE_SERVICE_IRQ_VECTOR, &val) != 1 || !val)
5946 		return;
5947 
5948 	drm_dp_dpcd_writeb(&intel_dp->aux, DP_DEVICE_SERVICE_IRQ_VECTOR, val);
5949 
5950 	if (val & DP_AUTOMATED_TEST_REQUEST)
5951 		intel_dp_handle_test_request(intel_dp);
5952 
5953 	if (val & DP_CP_IRQ)
5954 		intel_hdcp_handle_cp_irq(intel_dp->attached_connector);
5955 
5956 	if (val & DP_SINK_SPECIFIC_IRQ)
5957 		drm_dbg_kms(&i915->drm, "Sink specific irq unhandled\n");
5958 }
5959 
5960 /*
5961  * According to DP spec
5962  * 5.1.2:
5963  *  1. Read DPCD
5964  *  2. Configure link according to Receiver Capabilities
5965  *  3. Use Link Training from 2.5.3.3 and 3.5.1.3
5966  *  4. Check link status on receipt of hot-plug interrupt
5967  *
5968  * intel_dp_short_pulse -  handles short pulse interrupts
5969  * when full detection is not required.
5970  * Returns %true if short pulse is handled and full detection
5971  * is NOT required and %false otherwise.
5972  */
5973 static bool
5974 intel_dp_short_pulse(struct intel_dp *intel_dp)
5975 {
5976 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
5977 	u8 old_sink_count = intel_dp->sink_count;
5978 	bool ret;
5979 
5980 	/*
5981 	 * Clearing compliance test variables to allow capturing
5982 	 * of values for next automated test request.
5983 	 */
5984 	memset(&intel_dp->compliance, 0, sizeof(intel_dp->compliance));
5985 
5986 	/*
5987 	 * Now read the DPCD to see if it's actually running
5988 	 * If the current value of sink count doesn't match with
5989 	 * the value that was stored earlier or dpcd read failed
5990 	 * we need to do full detection
5991 	 */
5992 	ret = intel_dp_get_dpcd(intel_dp);
5993 
5994 	if ((old_sink_count != intel_dp->sink_count) || !ret) {
5995 		/* No need to proceed if we are going to do full detect */
5996 		return false;
5997 	}
5998 
5999 	intel_dp_check_service_irq(intel_dp);
6000 
6001 	/* Handle CEC interrupts, if any */
6002 	drm_dp_cec_irq(&intel_dp->aux);
6003 
6004 	/* defer to the hotplug work for link retraining if needed */
6005 	if (intel_dp_needs_link_retrain(intel_dp))
6006 		return false;
6007 
6008 	intel_psr_short_pulse(intel_dp);
6009 
6010 	if (intel_dp->compliance.test_type == DP_TEST_LINK_TRAINING) {
6011 		drm_dbg_kms(&dev_priv->drm,
6012 			    "Link Training Compliance Test requested\n");
6013 		/* Send a Hotplug Uevent to userspace to start modeset */
6014 		drm_kms_helper_hotplug_event(&dev_priv->drm);
6015 	}
6016 
6017 	return true;
6018 }
6019 
6020 /* XXX this is probably wrong for multiple downstream ports */
6021 static enum drm_connector_status
6022 intel_dp_detect_dpcd(struct intel_dp *intel_dp)
6023 {
6024 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
6025 	struct intel_lspcon *lspcon = dp_to_lspcon(intel_dp);
6026 	u8 *dpcd = intel_dp->dpcd;
6027 	u8 type;
6028 
6029 	if (drm_WARN_ON(&i915->drm, intel_dp_is_edp(intel_dp)))
6030 		return connector_status_connected;
6031 
6032 	if (lspcon->active)
6033 		lspcon_resume(lspcon);
6034 
6035 	if (!intel_dp_get_dpcd(intel_dp))
6036 		return connector_status_disconnected;
6037 
6038 	/* if there's no downstream port, we're done */
6039 	if (!drm_dp_is_branch(dpcd))
6040 		return connector_status_connected;
6041 
6042 	/* If we're HPD-aware, SINK_COUNT changes dynamically */
6043 	if (intel_dp_has_sink_count(intel_dp) &&
6044 	    intel_dp->downstream_ports[0] & DP_DS_PORT_HPD) {
6045 		return intel_dp->sink_count ?
6046 		connector_status_connected : connector_status_disconnected;
6047 	}
6048 
6049 	if (intel_dp_can_mst(intel_dp))
6050 		return connector_status_connected;
6051 
6052 	/* If no HPD, poke DDC gently */
6053 	if (drm_probe_ddc(&intel_dp->aux.ddc))
6054 		return connector_status_connected;
6055 
6056 	/* Well we tried, say unknown for unreliable port types */
6057 	if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11) {
6058 		type = intel_dp->downstream_ports[0] & DP_DS_PORT_TYPE_MASK;
6059 		if (type == DP_DS_PORT_TYPE_VGA ||
6060 		    type == DP_DS_PORT_TYPE_NON_EDID)
6061 			return connector_status_unknown;
6062 	} else {
6063 		type = intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
6064 			DP_DWN_STRM_PORT_TYPE_MASK;
6065 		if (type == DP_DWN_STRM_PORT_TYPE_ANALOG ||
6066 		    type == DP_DWN_STRM_PORT_TYPE_OTHER)
6067 			return connector_status_unknown;
6068 	}
6069 
6070 	/* Anything else is out of spec, warn and ignore */
6071 	drm_dbg_kms(&i915->drm, "Broken DP branch device, ignoring\n");
6072 	return connector_status_disconnected;
6073 }
6074 
6075 static enum drm_connector_status
6076 edp_detect(struct intel_dp *intel_dp)
6077 {
6078 	return connector_status_connected;
6079 }
6080 
6081 static bool ibx_digital_port_connected(struct intel_encoder *encoder)
6082 {
6083 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
6084 	u32 bit = dev_priv->hotplug.pch_hpd[encoder->hpd_pin];
6085 
6086 	return intel_de_read(dev_priv, SDEISR) & bit;
6087 }
6088 
6089 static bool g4x_digital_port_connected(struct intel_encoder *encoder)
6090 {
6091 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
6092 	u32 bit;
6093 
6094 	switch (encoder->hpd_pin) {
6095 	case HPD_PORT_B:
6096 		bit = PORTB_HOTPLUG_LIVE_STATUS_G4X;
6097 		break;
6098 	case HPD_PORT_C:
6099 		bit = PORTC_HOTPLUG_LIVE_STATUS_G4X;
6100 		break;
6101 	case HPD_PORT_D:
6102 		bit = PORTD_HOTPLUG_LIVE_STATUS_G4X;
6103 		break;
6104 	default:
6105 		MISSING_CASE(encoder->hpd_pin);
6106 		return false;
6107 	}
6108 
6109 	return intel_de_read(dev_priv, PORT_HOTPLUG_STAT) & bit;
6110 }
6111 
6112 static bool gm45_digital_port_connected(struct intel_encoder *encoder)
6113 {
6114 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
6115 	u32 bit;
6116 
6117 	switch (encoder->hpd_pin) {
6118 	case HPD_PORT_B:
6119 		bit = PORTB_HOTPLUG_LIVE_STATUS_GM45;
6120 		break;
6121 	case HPD_PORT_C:
6122 		bit = PORTC_HOTPLUG_LIVE_STATUS_GM45;
6123 		break;
6124 	case HPD_PORT_D:
6125 		bit = PORTD_HOTPLUG_LIVE_STATUS_GM45;
6126 		break;
6127 	default:
6128 		MISSING_CASE(encoder->hpd_pin);
6129 		return false;
6130 	}
6131 
6132 	return intel_de_read(dev_priv, PORT_HOTPLUG_STAT) & bit;
6133 }
6134 
6135 static bool ilk_digital_port_connected(struct intel_encoder *encoder)
6136 {
6137 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
6138 	u32 bit = dev_priv->hotplug.hpd[encoder->hpd_pin];
6139 
6140 	return intel_de_read(dev_priv, DEISR) & bit;
6141 }
6142 
6143 /*
6144  * intel_digital_port_connected - is the specified port connected?
6145  * @encoder: intel_encoder
6146  *
6147  * In cases where there's a connector physically connected but it can't be used
6148  * by our hardware we also return false, since the rest of the driver should
6149  * pretty much treat the port as disconnected. This is relevant for type-C
6150  * (starting on ICL) where there's ownership involved.
6151  *
6152  * Return %true if port is connected, %false otherwise.
6153  */
6154 bool intel_digital_port_connected(struct intel_encoder *encoder)
6155 {
6156 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
6157 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
6158 	bool is_connected = false;
6159 	intel_wakeref_t wakeref;
6160 
6161 	with_intel_display_power(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref)
6162 		is_connected = dig_port->connected(encoder);
6163 
6164 	return is_connected;
6165 }
6166 
6167 static struct edid *
6168 intel_dp_get_edid(struct intel_dp *intel_dp)
6169 {
6170 	struct intel_connector *intel_connector = intel_dp->attached_connector;
6171 
6172 	/* use cached edid if we have one */
6173 	if (intel_connector->edid) {
6174 		/* invalid edid */
6175 		if (IS_ERR(intel_connector->edid))
6176 			return NULL;
6177 
6178 		return drm_edid_duplicate(intel_connector->edid);
6179 	} else
6180 		return drm_get_edid(&intel_connector->base,
6181 				    &intel_dp->aux.ddc);
6182 }
6183 
6184 static void
6185 intel_dp_update_dfp(struct intel_dp *intel_dp,
6186 		    const struct edid *edid)
6187 {
6188 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
6189 	struct intel_connector *connector = intel_dp->attached_connector;
6190 
6191 	intel_dp->dfp.max_bpc =
6192 		drm_dp_downstream_max_bpc(intel_dp->dpcd,
6193 					  intel_dp->downstream_ports, edid);
6194 
6195 	intel_dp->dfp.max_dotclock =
6196 		drm_dp_downstream_max_dotclock(intel_dp->dpcd,
6197 					       intel_dp->downstream_ports);
6198 
6199 	intel_dp->dfp.min_tmds_clock =
6200 		drm_dp_downstream_min_tmds_clock(intel_dp->dpcd,
6201 						 intel_dp->downstream_ports,
6202 						 edid);
6203 	intel_dp->dfp.max_tmds_clock =
6204 		drm_dp_downstream_max_tmds_clock(intel_dp->dpcd,
6205 						 intel_dp->downstream_ports,
6206 						 edid);
6207 
6208 	drm_dbg_kms(&i915->drm,
6209 		    "[CONNECTOR:%d:%s] DFP max bpc %d, max dotclock %d, TMDS clock %d-%d\n",
6210 		    connector->base.base.id, connector->base.name,
6211 		    intel_dp->dfp.max_bpc,
6212 		    intel_dp->dfp.max_dotclock,
6213 		    intel_dp->dfp.min_tmds_clock,
6214 		    intel_dp->dfp.max_tmds_clock);
6215 }
6216 
6217 static void
6218 intel_dp_update_420(struct intel_dp *intel_dp)
6219 {
6220 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
6221 	struct intel_connector *connector = intel_dp->attached_connector;
6222 	bool is_branch, ycbcr_420_passthrough, ycbcr_444_to_420;
6223 
6224 	/* No YCbCr output support on gmch platforms */
6225 	if (HAS_GMCH(i915))
6226 		return;
6227 
6228 	/*
6229 	 * ILK doesn't seem capable of DP YCbCr output. The
6230 	 * displayed image is severly corrupted. SNB+ is fine.
6231 	 */
6232 	if (IS_GEN(i915, 5))
6233 		return;
6234 
6235 	is_branch = drm_dp_is_branch(intel_dp->dpcd);
6236 	ycbcr_420_passthrough =
6237 		drm_dp_downstream_420_passthrough(intel_dp->dpcd,
6238 						  intel_dp->downstream_ports);
6239 	ycbcr_444_to_420 =
6240 		drm_dp_downstream_444_to_420_conversion(intel_dp->dpcd,
6241 							intel_dp->downstream_ports);
6242 
6243 	if (INTEL_GEN(i915) >= 11) {
6244 		/* Prefer 4:2:0 passthrough over 4:4:4->4:2:0 conversion */
6245 		intel_dp->dfp.ycbcr_444_to_420 =
6246 			ycbcr_444_to_420 && !ycbcr_420_passthrough;
6247 
6248 		connector->base.ycbcr_420_allowed =
6249 			!is_branch || ycbcr_444_to_420 || ycbcr_420_passthrough;
6250 	} else {
6251 		/* 4:4:4->4:2:0 conversion is the only way */
6252 		intel_dp->dfp.ycbcr_444_to_420 = ycbcr_444_to_420;
6253 
6254 		connector->base.ycbcr_420_allowed = ycbcr_444_to_420;
6255 	}
6256 
6257 	drm_dbg_kms(&i915->drm,
6258 		    "[CONNECTOR:%d:%s] YCbCr 4:2:0 allowed? %s, YCbCr 4:4:4->4:2:0 conversion? %s\n",
6259 		    connector->base.base.id, connector->base.name,
6260 		    yesno(connector->base.ycbcr_420_allowed),
6261 		    yesno(intel_dp->dfp.ycbcr_444_to_420));
6262 }
6263 
6264 static void
6265 intel_dp_set_edid(struct intel_dp *intel_dp)
6266 {
6267 	struct intel_connector *connector = intel_dp->attached_connector;
6268 	struct edid *edid;
6269 
6270 	intel_dp_unset_edid(intel_dp);
6271 	edid = intel_dp_get_edid(intel_dp);
6272 	connector->detect_edid = edid;
6273 
6274 	intel_dp_update_dfp(intel_dp, edid);
6275 	intel_dp_update_420(intel_dp);
6276 
6277 	if (edid && edid->input & DRM_EDID_INPUT_DIGITAL) {
6278 		intel_dp->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
6279 		intel_dp->has_audio = drm_detect_monitor_audio(edid);
6280 	}
6281 
6282 	drm_dp_cec_set_edid(&intel_dp->aux, edid);
6283 	intel_dp->edid_quirks = drm_dp_get_edid_quirks(edid);
6284 }
6285 
6286 static void
6287 intel_dp_unset_edid(struct intel_dp *intel_dp)
6288 {
6289 	struct intel_connector *connector = intel_dp->attached_connector;
6290 
6291 	drm_dp_cec_unset_edid(&intel_dp->aux);
6292 	kfree(connector->detect_edid);
6293 	connector->detect_edid = NULL;
6294 
6295 	intel_dp->has_hdmi_sink = false;
6296 	intel_dp->has_audio = false;
6297 	intel_dp->edid_quirks = 0;
6298 
6299 	intel_dp->dfp.max_bpc = 0;
6300 	intel_dp->dfp.max_dotclock = 0;
6301 	intel_dp->dfp.min_tmds_clock = 0;
6302 	intel_dp->dfp.max_tmds_clock = 0;
6303 
6304 	intel_dp->dfp.ycbcr_444_to_420 = false;
6305 	connector->base.ycbcr_420_allowed = false;
6306 }
6307 
6308 static int
6309 intel_dp_detect(struct drm_connector *connector,
6310 		struct drm_modeset_acquire_ctx *ctx,
6311 		bool force)
6312 {
6313 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
6314 	struct intel_dp *intel_dp = intel_attached_dp(to_intel_connector(connector));
6315 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
6316 	struct intel_encoder *encoder = &dig_port->base;
6317 	enum drm_connector_status status;
6318 
6319 	drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s]\n",
6320 		    connector->base.id, connector->name);
6321 	drm_WARN_ON(&dev_priv->drm,
6322 		    !drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex));
6323 
6324 	if (!INTEL_DISPLAY_ENABLED(dev_priv))
6325 		return connector_status_disconnected;
6326 
6327 	/* Can't disconnect eDP */
6328 	if (intel_dp_is_edp(intel_dp))
6329 		status = edp_detect(intel_dp);
6330 	else if (intel_digital_port_connected(encoder))
6331 		status = intel_dp_detect_dpcd(intel_dp);
6332 	else
6333 		status = connector_status_disconnected;
6334 
6335 	if (status == connector_status_disconnected) {
6336 		memset(&intel_dp->compliance, 0, sizeof(intel_dp->compliance));
6337 		memset(intel_dp->dsc_dpcd, 0, sizeof(intel_dp->dsc_dpcd));
6338 
6339 		if (intel_dp->is_mst) {
6340 			drm_dbg_kms(&dev_priv->drm,
6341 				    "MST device may have disappeared %d vs %d\n",
6342 				    intel_dp->is_mst,
6343 				    intel_dp->mst_mgr.mst_state);
6344 			intel_dp->is_mst = false;
6345 			drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr,
6346 							intel_dp->is_mst);
6347 		}
6348 
6349 		goto out;
6350 	}
6351 
6352 	/* Read DP Sink DSC Cap DPCD regs for DP v1.4 */
6353 	if (INTEL_GEN(dev_priv) >= 11)
6354 		intel_dp_get_dsc_sink_cap(intel_dp);
6355 
6356 	intel_dp_configure_mst(intel_dp);
6357 
6358 	/*
6359 	 * TODO: Reset link params when switching to MST mode, until MST
6360 	 * supports link training fallback params.
6361 	 */
6362 	if (intel_dp->reset_link_params || intel_dp->is_mst) {
6363 		/* Initial max link lane count */
6364 		intel_dp->max_link_lane_count = intel_dp_max_common_lane_count(intel_dp);
6365 
6366 		/* Initial max link rate */
6367 		intel_dp->max_link_rate = intel_dp_max_common_rate(intel_dp);
6368 
6369 		intel_dp->reset_link_params = false;
6370 	}
6371 
6372 	intel_dp_print_rates(intel_dp);
6373 
6374 	if (intel_dp->is_mst) {
6375 		/*
6376 		 * If we are in MST mode then this connector
6377 		 * won't appear connected or have anything
6378 		 * with EDID on it
6379 		 */
6380 		status = connector_status_disconnected;
6381 		goto out;
6382 	}
6383 
6384 	/*
6385 	 * Some external monitors do not signal loss of link synchronization
6386 	 * with an IRQ_HPD, so force a link status check.
6387 	 */
6388 	if (!intel_dp_is_edp(intel_dp)) {
6389 		int ret;
6390 
6391 		ret = intel_dp_retrain_link(encoder, ctx);
6392 		if (ret)
6393 			return ret;
6394 	}
6395 
6396 	/*
6397 	 * Clearing NACK and defer counts to get their exact values
6398 	 * while reading EDID which are required by Compliance tests
6399 	 * 4.2.2.4 and 4.2.2.5
6400 	 */
6401 	intel_dp->aux.i2c_nack_count = 0;
6402 	intel_dp->aux.i2c_defer_count = 0;
6403 
6404 	intel_dp_set_edid(intel_dp);
6405 	if (intel_dp_is_edp(intel_dp) ||
6406 	    to_intel_connector(connector)->detect_edid)
6407 		status = connector_status_connected;
6408 
6409 	intel_dp_check_service_irq(intel_dp);
6410 
6411 out:
6412 	if (status != connector_status_connected && !intel_dp->is_mst)
6413 		intel_dp_unset_edid(intel_dp);
6414 
6415 	/*
6416 	 * Make sure the refs for power wells enabled during detect are
6417 	 * dropped to avoid a new detect cycle triggered by HPD polling.
6418 	 */
6419 	intel_display_power_flush_work(dev_priv);
6420 
6421 	if (!intel_dp_is_edp(intel_dp))
6422 		drm_dp_set_subconnector_property(connector,
6423 						 status,
6424 						 intel_dp->dpcd,
6425 						 intel_dp->downstream_ports);
6426 	return status;
6427 }
6428 
6429 static void
6430 intel_dp_force(struct drm_connector *connector)
6431 {
6432 	struct intel_dp *intel_dp = intel_attached_dp(to_intel_connector(connector));
6433 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
6434 	struct intel_encoder *intel_encoder = &dig_port->base;
6435 	struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
6436 	enum intel_display_power_domain aux_domain =
6437 		intel_aux_power_domain(dig_port);
6438 	intel_wakeref_t wakeref;
6439 
6440 	drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s]\n",
6441 		    connector->base.id, connector->name);
6442 	intel_dp_unset_edid(intel_dp);
6443 
6444 	if (connector->status != connector_status_connected)
6445 		return;
6446 
6447 	wakeref = intel_display_power_get(dev_priv, aux_domain);
6448 
6449 	intel_dp_set_edid(intel_dp);
6450 
6451 	intel_display_power_put(dev_priv, aux_domain, wakeref);
6452 }
6453 
6454 static int intel_dp_get_modes(struct drm_connector *connector)
6455 {
6456 	struct intel_connector *intel_connector = to_intel_connector(connector);
6457 	struct edid *edid;
6458 
6459 	edid = intel_connector->detect_edid;
6460 	if (edid) {
6461 		int ret = intel_connector_update_modes(connector, edid);
6462 		if (ret)
6463 			return ret;
6464 	}
6465 
6466 	/* if eDP has no EDID, fall back to fixed mode */
6467 	if (intel_dp_is_edp(intel_attached_dp(intel_connector)) &&
6468 	    intel_connector->panel.fixed_mode) {
6469 		struct drm_display_mode *mode;
6470 
6471 		mode = drm_mode_duplicate(connector->dev,
6472 					  intel_connector->panel.fixed_mode);
6473 		if (mode) {
6474 			drm_mode_probed_add(connector, mode);
6475 			return 1;
6476 		}
6477 	}
6478 
6479 	if (!edid) {
6480 		struct intel_dp *intel_dp = intel_attached_dp(intel_connector);
6481 		struct drm_display_mode *mode;
6482 
6483 		mode = drm_dp_downstream_mode(connector->dev,
6484 					      intel_dp->dpcd,
6485 					      intel_dp->downstream_ports);
6486 		if (mode) {
6487 			drm_mode_probed_add(connector, mode);
6488 			return 1;
6489 		}
6490 	}
6491 
6492 	return 0;
6493 }
6494 
6495 static int
6496 intel_dp_connector_register(struct drm_connector *connector)
6497 {
6498 	struct drm_i915_private *i915 = to_i915(connector->dev);
6499 	struct intel_dp *intel_dp = intel_attached_dp(to_intel_connector(connector));
6500 	int ret;
6501 
6502 	ret = intel_connector_register(connector);
6503 	if (ret)
6504 		return ret;
6505 
6506 #ifdef notyet
6507 	drm_dbg_kms(&i915->drm, "registering %s bus for %s\n",
6508 		    intel_dp->aux.name, connector->kdev->kobj.name);
6509 #endif
6510 
6511 	intel_dp->aux.dev = connector->kdev;
6512 	ret = drm_dp_aux_register(&intel_dp->aux);
6513 	if (!ret)
6514 		drm_dp_cec_register_connector(&intel_dp->aux, connector);
6515 	return ret;
6516 }
6517 
6518 static void
6519 intel_dp_connector_unregister(struct drm_connector *connector)
6520 {
6521 	struct intel_dp *intel_dp = intel_attached_dp(to_intel_connector(connector));
6522 
6523 	drm_dp_cec_unregister_connector(&intel_dp->aux);
6524 	drm_dp_aux_unregister(&intel_dp->aux);
6525 	intel_connector_unregister(connector);
6526 }
6527 
6528 void intel_dp_encoder_flush_work(struct drm_encoder *encoder)
6529 {
6530 	struct intel_digital_port *dig_port = enc_to_dig_port(to_intel_encoder(encoder));
6531 	struct intel_dp *intel_dp = &dig_port->dp;
6532 
6533 	intel_dp_mst_encoder_cleanup(dig_port);
6534 	if (intel_dp_is_edp(intel_dp)) {
6535 		intel_wakeref_t wakeref;
6536 
6537 		cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
6538 		/*
6539 		 * vdd might still be enabled do to the delayed vdd off.
6540 		 * Make sure vdd is actually turned off here.
6541 		 */
6542 		with_pps_lock(intel_dp, wakeref)
6543 			edp_panel_vdd_off_sync(intel_dp);
6544 
6545 		if (intel_dp->edp_notifier.notifier_call) {
6546 			unregister_reboot_notifier(&intel_dp->edp_notifier);
6547 			intel_dp->edp_notifier.notifier_call = NULL;
6548 		}
6549 	}
6550 
6551 	intel_dp_aux_fini(intel_dp);
6552 }
6553 
6554 static void intel_dp_encoder_destroy(struct drm_encoder *encoder)
6555 {
6556 	intel_dp_encoder_flush_work(encoder);
6557 
6558 	drm_encoder_cleanup(encoder);
6559 	kfree(enc_to_dig_port(to_intel_encoder(encoder)));
6560 }
6561 
6562 void intel_dp_encoder_suspend(struct intel_encoder *intel_encoder)
6563 {
6564 	struct intel_dp *intel_dp = enc_to_intel_dp(intel_encoder);
6565 	intel_wakeref_t wakeref;
6566 
6567 	if (!intel_dp_is_edp(intel_dp))
6568 		return;
6569 
6570 	/*
6571 	 * vdd might still be enabled do to the delayed vdd off.
6572 	 * Make sure vdd is actually turned off here.
6573 	 */
6574 	cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
6575 	with_pps_lock(intel_dp, wakeref)
6576 		edp_panel_vdd_off_sync(intel_dp);
6577 }
6578 
6579 static void intel_edp_panel_vdd_sanitize(struct intel_dp *intel_dp)
6580 {
6581 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
6582 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
6583 
6584 	lockdep_assert_held(&dev_priv->pps_mutex);
6585 
6586 	if (!edp_have_panel_vdd(intel_dp))
6587 		return;
6588 
6589 	/*
6590 	 * The VDD bit needs a power domain reference, so if the bit is
6591 	 * already enabled when we boot or resume, grab this reference and
6592 	 * schedule a vdd off, so we don't hold on to the reference
6593 	 * indefinitely.
6594 	 */
6595 	drm_dbg_kms(&dev_priv->drm,
6596 		    "VDD left on by BIOS, adjusting state tracking\n");
6597 	intel_display_power_get(dev_priv, intel_aux_power_domain(dig_port));
6598 
6599 	edp_panel_vdd_schedule_off(intel_dp);
6600 }
6601 
6602 static enum pipe vlv_active_pipe(struct intel_dp *intel_dp)
6603 {
6604 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
6605 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
6606 	enum pipe pipe;
6607 
6608 	if (intel_dp_port_enabled(dev_priv, intel_dp->output_reg,
6609 				  encoder->port, &pipe))
6610 		return pipe;
6611 
6612 	return INVALID_PIPE;
6613 }
6614 
6615 void intel_dp_encoder_reset(struct drm_encoder *encoder)
6616 {
6617 	struct drm_i915_private *dev_priv = to_i915(encoder->dev);
6618 	struct intel_dp *intel_dp = enc_to_intel_dp(to_intel_encoder(encoder));
6619 	struct intel_lspcon *lspcon = dp_to_lspcon(intel_dp);
6620 	intel_wakeref_t wakeref;
6621 
6622 	if (!HAS_DDI(dev_priv))
6623 		intel_dp->DP = intel_de_read(dev_priv, intel_dp->output_reg);
6624 
6625 	if (lspcon->active)
6626 		lspcon_resume(lspcon);
6627 
6628 	intel_dp->reset_link_params = true;
6629 
6630 	if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
6631 	    !intel_dp_is_edp(intel_dp))
6632 		return;
6633 
6634 	with_pps_lock(intel_dp, wakeref) {
6635 		if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
6636 			intel_dp->active_pipe = vlv_active_pipe(intel_dp);
6637 
6638 		if (intel_dp_is_edp(intel_dp)) {
6639 			/*
6640 			 * Reinit the power sequencer, in case BIOS did
6641 			 * something nasty with it.
6642 			 */
6643 			intel_dp_pps_init(intel_dp);
6644 			intel_edp_panel_vdd_sanitize(intel_dp);
6645 		}
6646 	}
6647 }
6648 
6649 static int intel_modeset_tile_group(struct intel_atomic_state *state,
6650 				    int tile_group_id)
6651 {
6652 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6653 	struct drm_connector_list_iter conn_iter;
6654 	struct drm_connector *connector;
6655 	int ret = 0;
6656 
6657 	drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
6658 	drm_for_each_connector_iter(connector, &conn_iter) {
6659 		struct drm_connector_state *conn_state;
6660 		struct intel_crtc_state *crtc_state;
6661 		struct intel_crtc *crtc;
6662 
6663 		if (!connector->has_tile ||
6664 		    connector->tile_group->id != tile_group_id)
6665 			continue;
6666 
6667 		conn_state = drm_atomic_get_connector_state(&state->base,
6668 							    connector);
6669 		if (IS_ERR(conn_state)) {
6670 			ret = PTR_ERR(conn_state);
6671 			break;
6672 		}
6673 
6674 		crtc = to_intel_crtc(conn_state->crtc);
6675 
6676 		if (!crtc)
6677 			continue;
6678 
6679 		crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
6680 		crtc_state->uapi.mode_changed = true;
6681 
6682 		ret = drm_atomic_add_affected_planes(&state->base, &crtc->base);
6683 		if (ret)
6684 			break;
6685 	}
6686 	drm_connector_list_iter_end(&conn_iter);
6687 
6688 	return ret;
6689 }
6690 
6691 static int intel_modeset_affected_transcoders(struct intel_atomic_state *state, u8 transcoders)
6692 {
6693 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6694 	struct intel_crtc *crtc;
6695 
6696 	if (transcoders == 0)
6697 		return 0;
6698 
6699 	for_each_intel_crtc(&dev_priv->drm, crtc) {
6700 		struct intel_crtc_state *crtc_state;
6701 		int ret;
6702 
6703 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
6704 		if (IS_ERR(crtc_state))
6705 			return PTR_ERR(crtc_state);
6706 
6707 		if (!crtc_state->hw.enable)
6708 			continue;
6709 
6710 		if (!(transcoders & BIT(crtc_state->cpu_transcoder)))
6711 			continue;
6712 
6713 		crtc_state->uapi.mode_changed = true;
6714 
6715 		ret = drm_atomic_add_affected_connectors(&state->base, &crtc->base);
6716 		if (ret)
6717 			return ret;
6718 
6719 		ret = drm_atomic_add_affected_planes(&state->base, &crtc->base);
6720 		if (ret)
6721 			return ret;
6722 
6723 		transcoders &= ~BIT(crtc_state->cpu_transcoder);
6724 	}
6725 
6726 	drm_WARN_ON(&dev_priv->drm, transcoders != 0);
6727 
6728 	return 0;
6729 }
6730 
6731 static int intel_modeset_synced_crtcs(struct intel_atomic_state *state,
6732 				      struct drm_connector *connector)
6733 {
6734 	const struct drm_connector_state *old_conn_state =
6735 		drm_atomic_get_old_connector_state(&state->base, connector);
6736 	const struct intel_crtc_state *old_crtc_state;
6737 	struct intel_crtc *crtc;
6738 	u8 transcoders;
6739 
6740 	crtc = to_intel_crtc(old_conn_state->crtc);
6741 	if (!crtc)
6742 		return 0;
6743 
6744 	old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc);
6745 
6746 	if (!old_crtc_state->hw.active)
6747 		return 0;
6748 
6749 	transcoders = old_crtc_state->sync_mode_slaves_mask;
6750 	if (old_crtc_state->master_transcoder != INVALID_TRANSCODER)
6751 		transcoders |= BIT(old_crtc_state->master_transcoder);
6752 
6753 	return intel_modeset_affected_transcoders(state,
6754 						  transcoders);
6755 }
6756 
6757 static int intel_dp_connector_atomic_check(struct drm_connector *conn,
6758 					   struct drm_atomic_state *_state)
6759 {
6760 	struct drm_i915_private *dev_priv = to_i915(conn->dev);
6761 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
6762 	int ret;
6763 
6764 	ret = intel_digital_connector_atomic_check(conn, &state->base);
6765 	if (ret)
6766 		return ret;
6767 
6768 	/*
6769 	 * We don't enable port sync on BDW due to missing w/as and
6770 	 * due to not having adjusted the modeset sequence appropriately.
6771 	 */
6772 	if (INTEL_GEN(dev_priv) < 9)
6773 		return 0;
6774 
6775 	if (!intel_connector_needs_modeset(state, conn))
6776 		return 0;
6777 
6778 	if (conn->has_tile) {
6779 		ret = intel_modeset_tile_group(state, conn->tile_group->id);
6780 		if (ret)
6781 			return ret;
6782 	}
6783 
6784 	return intel_modeset_synced_crtcs(state, conn);
6785 }
6786 
6787 static const struct drm_connector_funcs intel_dp_connector_funcs = {
6788 	.force = intel_dp_force,
6789 	.fill_modes = drm_helper_probe_single_connector_modes,
6790 	.atomic_get_property = intel_digital_connector_atomic_get_property,
6791 	.atomic_set_property = intel_digital_connector_atomic_set_property,
6792 	.late_register = intel_dp_connector_register,
6793 	.early_unregister = intel_dp_connector_unregister,
6794 	.destroy = intel_connector_destroy,
6795 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
6796 	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
6797 };
6798 
6799 static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
6800 	.detect_ctx = intel_dp_detect,
6801 	.get_modes = intel_dp_get_modes,
6802 	.mode_valid = intel_dp_mode_valid,
6803 	.atomic_check = intel_dp_connector_atomic_check,
6804 };
6805 
6806 static const struct drm_encoder_funcs intel_dp_enc_funcs = {
6807 	.reset = intel_dp_encoder_reset,
6808 	.destroy = intel_dp_encoder_destroy,
6809 };
6810 
6811 static bool intel_edp_have_power(struct intel_dp *intel_dp)
6812 {
6813 	intel_wakeref_t wakeref;
6814 	bool have_power = false;
6815 
6816 	with_pps_lock(intel_dp, wakeref) {
6817 		have_power = edp_have_panel_power(intel_dp) &&
6818 						  edp_have_panel_vdd(intel_dp);
6819 	}
6820 
6821 	return have_power;
6822 }
6823 
6824 enum irqreturn
6825 intel_dp_hpd_pulse(struct intel_digital_port *dig_port, bool long_hpd)
6826 {
6827 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
6828 	struct intel_dp *intel_dp = &dig_port->dp;
6829 
6830 	if (dig_port->base.type == INTEL_OUTPUT_EDP &&
6831 	    (long_hpd || !intel_edp_have_power(intel_dp))) {
6832 		/*
6833 		 * vdd off can generate a long/short pulse on eDP which
6834 		 * would require vdd on to handle it, and thus we
6835 		 * would end up in an endless cycle of
6836 		 * "vdd off -> long/short hpd -> vdd on -> detect -> vdd off -> ..."
6837 		 */
6838 		drm_dbg_kms(&i915->drm,
6839 			    "ignoring %s hpd on eDP [ENCODER:%d:%s]\n",
6840 			    long_hpd ? "long" : "short",
6841 			    dig_port->base.base.base.id,
6842 			    dig_port->base.base.name);
6843 		return IRQ_HANDLED;
6844 	}
6845 
6846 	drm_dbg_kms(&i915->drm, "got hpd irq on [ENCODER:%d:%s] - %s\n",
6847 		    dig_port->base.base.base.id,
6848 		    dig_port->base.base.name,
6849 		    long_hpd ? "long" : "short");
6850 
6851 	if (long_hpd) {
6852 		intel_dp->reset_link_params = true;
6853 		return IRQ_NONE;
6854 	}
6855 
6856 	if (intel_dp->is_mst) {
6857 		if (!intel_dp_check_mst_status(intel_dp))
6858 			return IRQ_NONE;
6859 	} else if (!intel_dp_short_pulse(intel_dp)) {
6860 		return IRQ_NONE;
6861 	}
6862 
6863 	return IRQ_HANDLED;
6864 }
6865 
6866 /* check the VBT to see whether the eDP is on another port */
6867 bool intel_dp_is_port_edp(struct drm_i915_private *dev_priv, enum port port)
6868 {
6869 	/*
6870 	 * eDP not supported on g4x. so bail out early just
6871 	 * for a bit extra safety in case the VBT is bonkers.
6872 	 */
6873 	if (INTEL_GEN(dev_priv) < 5)
6874 		return false;
6875 
6876 	if (INTEL_GEN(dev_priv) < 9 && port == PORT_A)
6877 		return true;
6878 
6879 	return intel_bios_is_port_edp(dev_priv, port);
6880 }
6881 
6882 static void
6883 intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
6884 {
6885 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
6886 	enum port port = dp_to_dig_port(intel_dp)->base.port;
6887 
6888 	if (!intel_dp_is_edp(intel_dp))
6889 		drm_connector_attach_dp_subconnector_property(connector);
6890 
6891 	if (!IS_G4X(dev_priv) && port != PORT_A)
6892 		intel_attach_force_audio_property(connector);
6893 
6894 	intel_attach_broadcast_rgb_property(connector);
6895 	if (HAS_GMCH(dev_priv))
6896 		drm_connector_attach_max_bpc_property(connector, 6, 10);
6897 	else if (INTEL_GEN(dev_priv) >= 5)
6898 		drm_connector_attach_max_bpc_property(connector, 6, 12);
6899 
6900 	intel_attach_colorspace_property(connector);
6901 
6902 	if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 11)
6903 		drm_object_attach_property(&connector->base,
6904 					   connector->dev->mode_config.hdr_output_metadata_property,
6905 					   0);
6906 
6907 	if (intel_dp_is_edp(intel_dp)) {
6908 		u32 allowed_scalers;
6909 
6910 		allowed_scalers = BIT(DRM_MODE_SCALE_ASPECT) | BIT(DRM_MODE_SCALE_FULLSCREEN);
6911 		if (!HAS_GMCH(dev_priv))
6912 			allowed_scalers |= BIT(DRM_MODE_SCALE_CENTER);
6913 
6914 		drm_connector_attach_scaling_mode_property(connector, allowed_scalers);
6915 
6916 		connector->state->scaling_mode = DRM_MODE_SCALE_ASPECT;
6917 
6918 	}
6919 }
6920 
6921 static void intel_dp_init_panel_power_timestamps(struct intel_dp *intel_dp)
6922 {
6923 	intel_dp->panel_power_off_time = ktime_get_boottime();
6924 	intel_dp->last_power_on = jiffies;
6925 	intel_dp->last_backlight_off = jiffies;
6926 }
6927 
6928 static void
6929 intel_pps_readout_hw_state(struct intel_dp *intel_dp, struct edp_power_seq *seq)
6930 {
6931 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
6932 	u32 pp_on, pp_off, pp_ctl;
6933 	struct pps_registers regs;
6934 
6935 	intel_pps_get_registers(intel_dp, &regs);
6936 
6937 	pp_ctl = ilk_get_pp_control(intel_dp);
6938 
6939 	/* Ensure PPS is unlocked */
6940 	if (!HAS_DDI(dev_priv))
6941 		intel_de_write(dev_priv, regs.pp_ctrl, pp_ctl);
6942 
6943 	pp_on = intel_de_read(dev_priv, regs.pp_on);
6944 	pp_off = intel_de_read(dev_priv, regs.pp_off);
6945 
6946 	/* Pull timing values out of registers */
6947 	seq->t1_t3 = REG_FIELD_GET(PANEL_POWER_UP_DELAY_MASK, pp_on);
6948 	seq->t8 = REG_FIELD_GET(PANEL_LIGHT_ON_DELAY_MASK, pp_on);
6949 	seq->t9 = REG_FIELD_GET(PANEL_LIGHT_OFF_DELAY_MASK, pp_off);
6950 	seq->t10 = REG_FIELD_GET(PANEL_POWER_DOWN_DELAY_MASK, pp_off);
6951 
6952 	if (i915_mmio_reg_valid(regs.pp_div)) {
6953 		u32 pp_div;
6954 
6955 		pp_div = intel_de_read(dev_priv, regs.pp_div);
6956 
6957 		seq->t11_t12 = REG_FIELD_GET(PANEL_POWER_CYCLE_DELAY_MASK, pp_div) * 1000;
6958 	} else {
6959 		seq->t11_t12 = REG_FIELD_GET(BXT_POWER_CYCLE_DELAY_MASK, pp_ctl) * 1000;
6960 	}
6961 }
6962 
6963 static void
6964 intel_pps_dump_state(const char *state_name, const struct edp_power_seq *seq)
6965 {
6966 	DRM_DEBUG_KMS("%s t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
6967 		      state_name,
6968 		      seq->t1_t3, seq->t8, seq->t9, seq->t10, seq->t11_t12);
6969 }
6970 
6971 static void
6972 intel_pps_verify_state(struct intel_dp *intel_dp)
6973 {
6974 	struct edp_power_seq hw;
6975 	struct edp_power_seq *sw = &intel_dp->pps_delays;
6976 
6977 	intel_pps_readout_hw_state(intel_dp, &hw);
6978 
6979 	if (hw.t1_t3 != sw->t1_t3 || hw.t8 != sw->t8 || hw.t9 != sw->t9 ||
6980 	    hw.t10 != sw->t10 || hw.t11_t12 != sw->t11_t12) {
6981 		DRM_ERROR("PPS state mismatch\n");
6982 		intel_pps_dump_state("sw", sw);
6983 		intel_pps_dump_state("hw", &hw);
6984 	}
6985 }
6986 
6987 static void
6988 intel_dp_init_panel_power_sequencer(struct intel_dp *intel_dp)
6989 {
6990 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
6991 	struct edp_power_seq cur, vbt, spec,
6992 		*final = &intel_dp->pps_delays;
6993 
6994 	lockdep_assert_held(&dev_priv->pps_mutex);
6995 
6996 	/* already initialized? */
6997 	if (final->t11_t12 != 0)
6998 		return;
6999 
7000 	intel_pps_readout_hw_state(intel_dp, &cur);
7001 
7002 	intel_pps_dump_state("cur", &cur);
7003 
7004 	vbt = dev_priv->vbt.edp.pps;
7005 	/* On Toshiba Satellite P50-C-18C system the VBT T12 delay
7006 	 * of 500ms appears to be too short. Ocassionally the panel
7007 	 * just fails to power back on. Increasing the delay to 800ms
7008 	 * seems sufficient to avoid this problem.
7009 	 */
7010 	if (dev_priv->quirks & QUIRK_INCREASE_T12_DELAY) {
7011 		vbt.t11_t12 = max_t(u16, vbt.t11_t12, 1300 * 10);
7012 		drm_dbg_kms(&dev_priv->drm,
7013 			    "Increasing T12 panel delay as per the quirk to %d\n",
7014 			    vbt.t11_t12);
7015 	}
7016 	/* T11_T12 delay is special and actually in units of 100ms, but zero
7017 	 * based in the hw (so we need to add 100 ms). But the sw vbt
7018 	 * table multiplies it with 1000 to make it in units of 100usec,
7019 	 * too. */
7020 	vbt.t11_t12 += 100 * 10;
7021 
7022 	/* Upper limits from eDP 1.3 spec. Note that we use the clunky units of
7023 	 * our hw here, which are all in 100usec. */
7024 	spec.t1_t3 = 210 * 10;
7025 	spec.t8 = 50 * 10; /* no limit for t8, use t7 instead */
7026 	spec.t9 = 50 * 10; /* no limit for t9, make it symmetric with t8 */
7027 	spec.t10 = 500 * 10;
7028 	/* This one is special and actually in units of 100ms, but zero
7029 	 * based in the hw (so we need to add 100 ms). But the sw vbt
7030 	 * table multiplies it with 1000 to make it in units of 100usec,
7031 	 * too. */
7032 	spec.t11_t12 = (510 + 100) * 10;
7033 
7034 	intel_pps_dump_state("vbt", &vbt);
7035 
7036 	/* Use the max of the register settings and vbt. If both are
7037 	 * unset, fall back to the spec limits. */
7038 #define assign_final(field)	final->field = (max(cur.field, vbt.field) == 0 ? \
7039 				       spec.field : \
7040 				       max(cur.field, vbt.field))
7041 	assign_final(t1_t3);
7042 	assign_final(t8);
7043 	assign_final(t9);
7044 	assign_final(t10);
7045 	assign_final(t11_t12);
7046 #undef assign_final
7047 
7048 #define get_delay(field)	(DIV_ROUND_UP(final->field, 10))
7049 	intel_dp->panel_power_up_delay = get_delay(t1_t3);
7050 	intel_dp->backlight_on_delay = get_delay(t8);
7051 	intel_dp->backlight_off_delay = get_delay(t9);
7052 	intel_dp->panel_power_down_delay = get_delay(t10);
7053 	intel_dp->panel_power_cycle_delay = get_delay(t11_t12);
7054 #undef get_delay
7055 
7056 	drm_dbg_kms(&dev_priv->drm,
7057 		    "panel power up delay %d, power down delay %d, power cycle delay %d\n",
7058 		    intel_dp->panel_power_up_delay,
7059 		    intel_dp->panel_power_down_delay,
7060 		    intel_dp->panel_power_cycle_delay);
7061 
7062 	drm_dbg_kms(&dev_priv->drm, "backlight on delay %d, off delay %d\n",
7063 		    intel_dp->backlight_on_delay,
7064 		    intel_dp->backlight_off_delay);
7065 
7066 	/*
7067 	 * We override the HW backlight delays to 1 because we do manual waits
7068 	 * on them. For T8, even BSpec recommends doing it. For T9, if we
7069 	 * don't do this, we'll end up waiting for the backlight off delay
7070 	 * twice: once when we do the manual sleep, and once when we disable
7071 	 * the panel and wait for the PP_STATUS bit to become zero.
7072 	 */
7073 	final->t8 = 1;
7074 	final->t9 = 1;
7075 
7076 	/*
7077 	 * HW has only a 100msec granularity for t11_t12 so round it up
7078 	 * accordingly.
7079 	 */
7080 	final->t11_t12 = roundup(final->t11_t12, 100 * 10);
7081 }
7082 
7083 static void
7084 intel_dp_init_panel_power_sequencer_registers(struct intel_dp *intel_dp,
7085 					      bool force_disable_vdd)
7086 {
7087 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
7088 	u32 pp_on, pp_off, port_sel = 0;
7089 	int div = RUNTIME_INFO(dev_priv)->rawclk_freq / 1000;
7090 	struct pps_registers regs;
7091 	enum port port = dp_to_dig_port(intel_dp)->base.port;
7092 	const struct edp_power_seq *seq = &intel_dp->pps_delays;
7093 
7094 	lockdep_assert_held(&dev_priv->pps_mutex);
7095 
7096 	intel_pps_get_registers(intel_dp, &regs);
7097 
7098 	/*
7099 	 * On some VLV machines the BIOS can leave the VDD
7100 	 * enabled even on power sequencers which aren't
7101 	 * hooked up to any port. This would mess up the
7102 	 * power domain tracking the first time we pick
7103 	 * one of these power sequencers for use since
7104 	 * edp_panel_vdd_on() would notice that the VDD was
7105 	 * already on and therefore wouldn't grab the power
7106 	 * domain reference. Disable VDD first to avoid this.
7107 	 * This also avoids spuriously turning the VDD on as
7108 	 * soon as the new power sequencer gets initialized.
7109 	 */
7110 	if (force_disable_vdd) {
7111 		u32 pp = ilk_get_pp_control(intel_dp);
7112 
7113 		drm_WARN(&dev_priv->drm, pp & PANEL_POWER_ON,
7114 			 "Panel power already on\n");
7115 
7116 		if (pp & EDP_FORCE_VDD)
7117 			drm_dbg_kms(&dev_priv->drm,
7118 				    "VDD already on, disabling first\n");
7119 
7120 		pp &= ~EDP_FORCE_VDD;
7121 
7122 		intel_de_write(dev_priv, regs.pp_ctrl, pp);
7123 	}
7124 
7125 	pp_on = REG_FIELD_PREP(PANEL_POWER_UP_DELAY_MASK, seq->t1_t3) |
7126 		REG_FIELD_PREP(PANEL_LIGHT_ON_DELAY_MASK, seq->t8);
7127 	pp_off = REG_FIELD_PREP(PANEL_LIGHT_OFF_DELAY_MASK, seq->t9) |
7128 		REG_FIELD_PREP(PANEL_POWER_DOWN_DELAY_MASK, seq->t10);
7129 
7130 	/* Haswell doesn't have any port selection bits for the panel
7131 	 * power sequencer any more. */
7132 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
7133 		port_sel = PANEL_PORT_SELECT_VLV(port);
7134 	} else if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
7135 		switch (port) {
7136 		case PORT_A:
7137 			port_sel = PANEL_PORT_SELECT_DPA;
7138 			break;
7139 		case PORT_C:
7140 			port_sel = PANEL_PORT_SELECT_DPC;
7141 			break;
7142 		case PORT_D:
7143 			port_sel = PANEL_PORT_SELECT_DPD;
7144 			break;
7145 		default:
7146 			MISSING_CASE(port);
7147 			break;
7148 		}
7149 	}
7150 
7151 	pp_on |= port_sel;
7152 
7153 	intel_de_write(dev_priv, regs.pp_on, pp_on);
7154 	intel_de_write(dev_priv, regs.pp_off, pp_off);
7155 
7156 	/*
7157 	 * Compute the divisor for the pp clock, simply match the Bspec formula.
7158 	 */
7159 	if (i915_mmio_reg_valid(regs.pp_div)) {
7160 		intel_de_write(dev_priv, regs.pp_div,
7161 			       REG_FIELD_PREP(PP_REFERENCE_DIVIDER_MASK, (100 * div) / 2 - 1) | REG_FIELD_PREP(PANEL_POWER_CYCLE_DELAY_MASK, DIV_ROUND_UP(seq->t11_t12, 1000)));
7162 	} else {
7163 		u32 pp_ctl;
7164 
7165 		pp_ctl = intel_de_read(dev_priv, regs.pp_ctrl);
7166 		pp_ctl &= ~BXT_POWER_CYCLE_DELAY_MASK;
7167 		pp_ctl |= REG_FIELD_PREP(BXT_POWER_CYCLE_DELAY_MASK, DIV_ROUND_UP(seq->t11_t12, 1000));
7168 		intel_de_write(dev_priv, regs.pp_ctrl, pp_ctl);
7169 	}
7170 
7171 	drm_dbg_kms(&dev_priv->drm,
7172 		    "panel power sequencer register settings: PP_ON %#x, PP_OFF %#x, PP_DIV %#x\n",
7173 		    intel_de_read(dev_priv, regs.pp_on),
7174 		    intel_de_read(dev_priv, regs.pp_off),
7175 		    i915_mmio_reg_valid(regs.pp_div) ?
7176 		    intel_de_read(dev_priv, regs.pp_div) :
7177 		    (intel_de_read(dev_priv, regs.pp_ctrl) & BXT_POWER_CYCLE_DELAY_MASK));
7178 }
7179 
7180 static void intel_dp_pps_init(struct intel_dp *intel_dp)
7181 {
7182 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
7183 
7184 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
7185 		vlv_initial_power_sequencer_setup(intel_dp);
7186 	} else {
7187 		intel_dp_init_panel_power_sequencer(intel_dp);
7188 		intel_dp_init_panel_power_sequencer_registers(intel_dp, false);
7189 	}
7190 }
7191 
7192 /**
7193  * intel_dp_set_drrs_state - program registers for RR switch to take effect
7194  * @dev_priv: i915 device
7195  * @crtc_state: a pointer to the active intel_crtc_state
7196  * @refresh_rate: RR to be programmed
7197  *
7198  * This function gets called when refresh rate (RR) has to be changed from
7199  * one frequency to another. Switches can be between high and low RR
7200  * supported by the panel or to any other RR based on media playback (in
7201  * this case, RR value needs to be passed from user space).
7202  *
7203  * The caller of this function needs to take a lock on dev_priv->drrs.
7204  */
7205 static void intel_dp_set_drrs_state(struct drm_i915_private *dev_priv,
7206 				    const struct intel_crtc_state *crtc_state,
7207 				    int refresh_rate)
7208 {
7209 	struct intel_dp *intel_dp = dev_priv->drrs.dp;
7210 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->uapi.crtc);
7211 	enum drrs_refresh_rate_type index = DRRS_HIGH_RR;
7212 
7213 	if (refresh_rate <= 0) {
7214 		drm_dbg_kms(&dev_priv->drm,
7215 			    "Refresh rate should be positive non-zero.\n");
7216 		return;
7217 	}
7218 
7219 	if (intel_dp == NULL) {
7220 		drm_dbg_kms(&dev_priv->drm, "DRRS not supported.\n");
7221 		return;
7222 	}
7223 
7224 	if (!intel_crtc) {
7225 		drm_dbg_kms(&dev_priv->drm,
7226 			    "DRRS: intel_crtc not initialized\n");
7227 		return;
7228 	}
7229 
7230 	if (dev_priv->drrs.type < SEAMLESS_DRRS_SUPPORT) {
7231 		drm_dbg_kms(&dev_priv->drm, "Only Seamless DRRS supported.\n");
7232 		return;
7233 	}
7234 
7235 	if (drm_mode_vrefresh(intel_dp->attached_connector->panel.downclock_mode) ==
7236 			refresh_rate)
7237 		index = DRRS_LOW_RR;
7238 
7239 	if (index == dev_priv->drrs.refresh_rate_type) {
7240 		drm_dbg_kms(&dev_priv->drm,
7241 			    "DRRS requested for previously set RR...ignoring\n");
7242 		return;
7243 	}
7244 
7245 	if (!crtc_state->hw.active) {
7246 		drm_dbg_kms(&dev_priv->drm,
7247 			    "eDP encoder disabled. CRTC not Active\n");
7248 		return;
7249 	}
7250 
7251 	if (INTEL_GEN(dev_priv) >= 8 && !IS_CHERRYVIEW(dev_priv)) {
7252 		switch (index) {
7253 		case DRRS_HIGH_RR:
7254 			intel_dp_set_m_n(crtc_state, M1_N1);
7255 			break;
7256 		case DRRS_LOW_RR:
7257 			intel_dp_set_m_n(crtc_state, M2_N2);
7258 			break;
7259 		case DRRS_MAX_RR:
7260 		default:
7261 			drm_err(&dev_priv->drm,
7262 				"Unsupported refreshrate type\n");
7263 		}
7264 	} else if (INTEL_GEN(dev_priv) > 6) {
7265 		i915_reg_t reg = PIPECONF(crtc_state->cpu_transcoder);
7266 		u32 val;
7267 
7268 		val = intel_de_read(dev_priv, reg);
7269 		if (index > DRRS_HIGH_RR) {
7270 			if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
7271 				val |= PIPECONF_EDP_RR_MODE_SWITCH_VLV;
7272 			else
7273 				val |= PIPECONF_EDP_RR_MODE_SWITCH;
7274 		} else {
7275 			if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
7276 				val &= ~PIPECONF_EDP_RR_MODE_SWITCH_VLV;
7277 			else
7278 				val &= ~PIPECONF_EDP_RR_MODE_SWITCH;
7279 		}
7280 		intel_de_write(dev_priv, reg, val);
7281 	}
7282 
7283 	dev_priv->drrs.refresh_rate_type = index;
7284 
7285 	drm_dbg_kms(&dev_priv->drm, "eDP Refresh Rate set to : %dHz\n",
7286 		    refresh_rate);
7287 }
7288 
7289 static void
7290 intel_edp_drrs_enable_locked(struct intel_dp *intel_dp)
7291 {
7292 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
7293 
7294 	dev_priv->drrs.busy_frontbuffer_bits = 0;
7295 	dev_priv->drrs.dp = intel_dp;
7296 }
7297 
7298 /**
7299  * intel_edp_drrs_enable - init drrs struct if supported
7300  * @intel_dp: DP struct
7301  * @crtc_state: A pointer to the active crtc state.
7302  *
7303  * Initializes frontbuffer_bits and drrs.dp
7304  */
7305 void intel_edp_drrs_enable(struct intel_dp *intel_dp,
7306 			   const struct intel_crtc_state *crtc_state)
7307 {
7308 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
7309 
7310 	if (!crtc_state->has_drrs)
7311 		return;
7312 
7313 	drm_dbg_kms(&dev_priv->drm, "Enabling DRRS\n");
7314 
7315 	mutex_lock(&dev_priv->drrs.mutex);
7316 
7317 	if (dev_priv->drrs.dp) {
7318 		drm_warn(&dev_priv->drm, "DRRS already enabled\n");
7319 		goto unlock;
7320 	}
7321 
7322 	intel_edp_drrs_enable_locked(intel_dp);
7323 
7324 unlock:
7325 	mutex_unlock(&dev_priv->drrs.mutex);
7326 }
7327 
7328 static void
7329 intel_edp_drrs_disable_locked(struct intel_dp *intel_dp,
7330 			      const struct intel_crtc_state *crtc_state)
7331 {
7332 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
7333 
7334 	if (dev_priv->drrs.refresh_rate_type == DRRS_LOW_RR) {
7335 		int refresh;
7336 
7337 		refresh = drm_mode_vrefresh(intel_dp->attached_connector->panel.fixed_mode);
7338 		intel_dp_set_drrs_state(dev_priv, crtc_state, refresh);
7339 	}
7340 
7341 	dev_priv->drrs.dp = NULL;
7342 }
7343 
7344 /**
7345  * intel_edp_drrs_disable - Disable DRRS
7346  * @intel_dp: DP struct
7347  * @old_crtc_state: Pointer to old crtc_state.
7348  *
7349  */
7350 void intel_edp_drrs_disable(struct intel_dp *intel_dp,
7351 			    const struct intel_crtc_state *old_crtc_state)
7352 {
7353 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
7354 
7355 	if (!old_crtc_state->has_drrs)
7356 		return;
7357 
7358 	mutex_lock(&dev_priv->drrs.mutex);
7359 	if (!dev_priv->drrs.dp) {
7360 		mutex_unlock(&dev_priv->drrs.mutex);
7361 		return;
7362 	}
7363 
7364 	intel_edp_drrs_disable_locked(intel_dp, old_crtc_state);
7365 	mutex_unlock(&dev_priv->drrs.mutex);
7366 
7367 	cancel_delayed_work_sync(&dev_priv->drrs.work);
7368 }
7369 
7370 /**
7371  * intel_edp_drrs_update - Update DRRS state
7372  * @intel_dp: Intel DP
7373  * @crtc_state: new CRTC state
7374  *
7375  * This function will update DRRS states, disabling or enabling DRRS when
7376  * executing fastsets. For full modeset, intel_edp_drrs_disable() and
7377  * intel_edp_drrs_enable() should be called instead.
7378  */
7379 void
7380 intel_edp_drrs_update(struct intel_dp *intel_dp,
7381 		      const struct intel_crtc_state *crtc_state)
7382 {
7383 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
7384 
7385 	if (dev_priv->drrs.type != SEAMLESS_DRRS_SUPPORT)
7386 		return;
7387 
7388 	mutex_lock(&dev_priv->drrs.mutex);
7389 
7390 	/* New state matches current one? */
7391 	if (crtc_state->has_drrs == !!dev_priv->drrs.dp)
7392 		goto unlock;
7393 
7394 	if (crtc_state->has_drrs)
7395 		intel_edp_drrs_enable_locked(intel_dp);
7396 	else
7397 		intel_edp_drrs_disable_locked(intel_dp, crtc_state);
7398 
7399 unlock:
7400 	mutex_unlock(&dev_priv->drrs.mutex);
7401 }
7402 
7403 static void intel_edp_drrs_downclock_work(struct work_struct *work)
7404 {
7405 	struct drm_i915_private *dev_priv =
7406 		container_of(work, typeof(*dev_priv), drrs.work.work);
7407 	struct intel_dp *intel_dp;
7408 
7409 	mutex_lock(&dev_priv->drrs.mutex);
7410 
7411 	intel_dp = dev_priv->drrs.dp;
7412 
7413 	if (!intel_dp)
7414 		goto unlock;
7415 
7416 	/*
7417 	 * The delayed work can race with an invalidate hence we need to
7418 	 * recheck.
7419 	 */
7420 
7421 	if (dev_priv->drrs.busy_frontbuffer_bits)
7422 		goto unlock;
7423 
7424 	if (dev_priv->drrs.refresh_rate_type != DRRS_LOW_RR) {
7425 		struct drm_crtc *crtc = dp_to_dig_port(intel_dp)->base.base.crtc;
7426 
7427 		intel_dp_set_drrs_state(dev_priv, to_intel_crtc(crtc)->config,
7428 			drm_mode_vrefresh(intel_dp->attached_connector->panel.downclock_mode));
7429 	}
7430 
7431 unlock:
7432 	mutex_unlock(&dev_priv->drrs.mutex);
7433 }
7434 
7435 /**
7436  * intel_edp_drrs_invalidate - Disable Idleness DRRS
7437  * @dev_priv: i915 device
7438  * @frontbuffer_bits: frontbuffer plane tracking bits
7439  *
7440  * This function gets called everytime rendering on the given planes start.
7441  * Hence DRRS needs to be Upclocked, i.e. (LOW_RR -> HIGH_RR).
7442  *
7443  * Dirty frontbuffers relevant to DRRS are tracked in busy_frontbuffer_bits.
7444  */
7445 void intel_edp_drrs_invalidate(struct drm_i915_private *dev_priv,
7446 			       unsigned int frontbuffer_bits)
7447 {
7448 	struct intel_dp *intel_dp;
7449 	struct drm_crtc *crtc;
7450 	enum pipe pipe;
7451 
7452 	if (dev_priv->drrs.type == DRRS_NOT_SUPPORTED)
7453 		return;
7454 
7455 	cancel_delayed_work(&dev_priv->drrs.work);
7456 
7457 	mutex_lock(&dev_priv->drrs.mutex);
7458 
7459 	intel_dp = dev_priv->drrs.dp;
7460 	if (!intel_dp) {
7461 		mutex_unlock(&dev_priv->drrs.mutex);
7462 		return;
7463 	}
7464 
7465 	crtc = dp_to_dig_port(intel_dp)->base.base.crtc;
7466 	pipe = to_intel_crtc(crtc)->pipe;
7467 
7468 	frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(pipe);
7469 	dev_priv->drrs.busy_frontbuffer_bits |= frontbuffer_bits;
7470 
7471 	/* invalidate means busy screen hence upclock */
7472 	if (frontbuffer_bits && dev_priv->drrs.refresh_rate_type == DRRS_LOW_RR)
7473 		intel_dp_set_drrs_state(dev_priv, to_intel_crtc(crtc)->config,
7474 					drm_mode_vrefresh(intel_dp->attached_connector->panel.fixed_mode));
7475 
7476 	mutex_unlock(&dev_priv->drrs.mutex);
7477 }
7478 
7479 /**
7480  * intel_edp_drrs_flush - Restart Idleness DRRS
7481  * @dev_priv: i915 device
7482  * @frontbuffer_bits: frontbuffer plane tracking bits
7483  *
7484  * This function gets called every time rendering on the given planes has
7485  * completed or flip on a crtc is completed. So DRRS should be upclocked
7486  * (LOW_RR -> HIGH_RR). And also Idleness detection should be started again,
7487  * if no other planes are dirty.
7488  *
7489  * Dirty frontbuffers relevant to DRRS are tracked in busy_frontbuffer_bits.
7490  */
7491 void intel_edp_drrs_flush(struct drm_i915_private *dev_priv,
7492 			  unsigned int frontbuffer_bits)
7493 {
7494 	struct intel_dp *intel_dp;
7495 	struct drm_crtc *crtc;
7496 	enum pipe pipe;
7497 
7498 	if (dev_priv->drrs.type == DRRS_NOT_SUPPORTED)
7499 		return;
7500 
7501 	cancel_delayed_work(&dev_priv->drrs.work);
7502 
7503 	mutex_lock(&dev_priv->drrs.mutex);
7504 
7505 	intel_dp = dev_priv->drrs.dp;
7506 	if (!intel_dp) {
7507 		mutex_unlock(&dev_priv->drrs.mutex);
7508 		return;
7509 	}
7510 
7511 	crtc = dp_to_dig_port(intel_dp)->base.base.crtc;
7512 	pipe = to_intel_crtc(crtc)->pipe;
7513 
7514 	frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(pipe);
7515 	dev_priv->drrs.busy_frontbuffer_bits &= ~frontbuffer_bits;
7516 
7517 	/* flush means busy screen hence upclock */
7518 	if (frontbuffer_bits && dev_priv->drrs.refresh_rate_type == DRRS_LOW_RR)
7519 		intel_dp_set_drrs_state(dev_priv, to_intel_crtc(crtc)->config,
7520 					drm_mode_vrefresh(intel_dp->attached_connector->panel.fixed_mode));
7521 
7522 	/*
7523 	 * flush also means no more activity hence schedule downclock, if all
7524 	 * other fbs are quiescent too
7525 	 */
7526 	if (!dev_priv->drrs.busy_frontbuffer_bits)
7527 		schedule_delayed_work(&dev_priv->drrs.work,
7528 				msecs_to_jiffies(1000));
7529 	mutex_unlock(&dev_priv->drrs.mutex);
7530 }
7531 
7532 /**
7533  * DOC: Display Refresh Rate Switching (DRRS)
7534  *
7535  * Display Refresh Rate Switching (DRRS) is a power conservation feature
7536  * which enables swtching between low and high refresh rates,
7537  * dynamically, based on the usage scenario. This feature is applicable
7538  * for internal panels.
7539  *
7540  * Indication that the panel supports DRRS is given by the panel EDID, which
7541  * would list multiple refresh rates for one resolution.
7542  *
7543  * DRRS is of 2 types - static and seamless.
7544  * Static DRRS involves changing refresh rate (RR) by doing a full modeset
7545  * (may appear as a blink on screen) and is used in dock-undock scenario.
7546  * Seamless DRRS involves changing RR without any visual effect to the user
7547  * and can be used during normal system usage. This is done by programming
7548  * certain registers.
7549  *
7550  * Support for static/seamless DRRS may be indicated in the VBT based on
7551  * inputs from the panel spec.
7552  *
7553  * DRRS saves power by switching to low RR based on usage scenarios.
7554  *
7555  * The implementation is based on frontbuffer tracking implementation.  When
7556  * there is a disturbance on the screen triggered by user activity or a periodic
7557  * system activity, DRRS is disabled (RR is changed to high RR).  When there is
7558  * no movement on screen, after a timeout of 1 second, a switch to low RR is
7559  * made.
7560  *
7561  * For integration with frontbuffer tracking code, intel_edp_drrs_invalidate()
7562  * and intel_edp_drrs_flush() are called.
7563  *
7564  * DRRS can be further extended to support other internal panels and also
7565  * the scenario of video playback wherein RR is set based on the rate
7566  * requested by userspace.
7567  */
7568 
7569 /**
7570  * intel_dp_drrs_init - Init basic DRRS work and mutex.
7571  * @connector: eDP connector
7572  * @fixed_mode: preferred mode of panel
7573  *
7574  * This function is  called only once at driver load to initialize basic
7575  * DRRS stuff.
7576  *
7577  * Returns:
7578  * Downclock mode if panel supports it, else return NULL.
7579  * DRRS support is determined by the presence of downclock mode (apart
7580  * from VBT setting).
7581  */
7582 static struct drm_display_mode *
7583 intel_dp_drrs_init(struct intel_connector *connector,
7584 		   struct drm_display_mode *fixed_mode)
7585 {
7586 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
7587 	struct drm_display_mode *downclock_mode = NULL;
7588 
7589 	INIT_DELAYED_WORK(&dev_priv->drrs.work, intel_edp_drrs_downclock_work);
7590 	rw_init(&dev_priv->drrs.mutex, "drrs");
7591 
7592 	if (INTEL_GEN(dev_priv) <= 6) {
7593 		drm_dbg_kms(&dev_priv->drm,
7594 			    "DRRS supported for Gen7 and above\n");
7595 		return NULL;
7596 	}
7597 
7598 	if (dev_priv->vbt.drrs_type != SEAMLESS_DRRS_SUPPORT) {
7599 		drm_dbg_kms(&dev_priv->drm, "VBT doesn't support DRRS\n");
7600 		return NULL;
7601 	}
7602 
7603 	downclock_mode = intel_panel_edid_downclock_mode(connector, fixed_mode);
7604 	if (!downclock_mode) {
7605 		drm_dbg_kms(&dev_priv->drm,
7606 			    "Downclock mode is not found. DRRS not supported\n");
7607 		return NULL;
7608 	}
7609 
7610 	dev_priv->drrs.type = dev_priv->vbt.drrs_type;
7611 
7612 	dev_priv->drrs.refresh_rate_type = DRRS_HIGH_RR;
7613 	drm_dbg_kms(&dev_priv->drm,
7614 		    "seamless DRRS supported for eDP panel.\n");
7615 	return downclock_mode;
7616 }
7617 
7618 static bool intel_edp_init_connector(struct intel_dp *intel_dp,
7619 				     struct intel_connector *intel_connector)
7620 {
7621 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
7622 	struct drm_device *dev = &dev_priv->drm;
7623 	struct drm_connector *connector = &intel_connector->base;
7624 	struct drm_display_mode *fixed_mode = NULL;
7625 	struct drm_display_mode *downclock_mode = NULL;
7626 	bool has_dpcd;
7627 	enum pipe pipe = INVALID_PIPE;
7628 	intel_wakeref_t wakeref;
7629 	struct edid *edid;
7630 
7631 	if (!intel_dp_is_edp(intel_dp))
7632 		return true;
7633 
7634 	INIT_DELAYED_WORK(&intel_dp->panel_vdd_work, edp_panel_vdd_work);
7635 
7636 	/*
7637 	 * On IBX/CPT we may get here with LVDS already registered. Since the
7638 	 * driver uses the only internal power sequencer available for both
7639 	 * eDP and LVDS bail out early in this case to prevent interfering
7640 	 * with an already powered-on LVDS power sequencer.
7641 	 */
7642 	if (intel_get_lvds_encoder(dev_priv)) {
7643 		drm_WARN_ON(dev,
7644 			    !(HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)));
7645 		drm_info(&dev_priv->drm,
7646 			 "LVDS was detected, not registering eDP\n");
7647 
7648 		return false;
7649 	}
7650 
7651 	with_pps_lock(intel_dp, wakeref) {
7652 		intel_dp_init_panel_power_timestamps(intel_dp);
7653 		intel_dp_pps_init(intel_dp);
7654 		intel_edp_panel_vdd_sanitize(intel_dp);
7655 	}
7656 
7657 	/* Cache DPCD and EDID for edp. */
7658 	has_dpcd = intel_edp_init_dpcd(intel_dp);
7659 
7660 	if (!has_dpcd) {
7661 		/* if this fails, presume the device is a ghost */
7662 		drm_info(&dev_priv->drm,
7663 			 "failed to retrieve link info, disabling eDP\n");
7664 		goto out_vdd_off;
7665 	}
7666 
7667 	mutex_lock(&dev->mode_config.mutex);
7668 	edid = drm_get_edid(connector, &intel_dp->aux.ddc);
7669 	if (edid) {
7670 		if (drm_add_edid_modes(connector, edid)) {
7671 			drm_connector_update_edid_property(connector, edid);
7672 			intel_dp->edid_quirks = drm_dp_get_edid_quirks(edid);
7673 		} else {
7674 			kfree(edid);
7675 			edid = ERR_PTR(-EINVAL);
7676 		}
7677 	} else {
7678 		edid = ERR_PTR(-ENOENT);
7679 	}
7680 	intel_connector->edid = edid;
7681 
7682 	fixed_mode = intel_panel_edid_fixed_mode(intel_connector);
7683 	if (fixed_mode)
7684 		downclock_mode = intel_dp_drrs_init(intel_connector, fixed_mode);
7685 
7686 	/* fallback to VBT if available for eDP */
7687 	if (!fixed_mode)
7688 		fixed_mode = intel_panel_vbt_fixed_mode(intel_connector);
7689 	mutex_unlock(&dev->mode_config.mutex);
7690 
7691 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
7692 		intel_dp->edp_notifier.notifier_call = edp_notify_handler;
7693 		register_reboot_notifier(&intel_dp->edp_notifier);
7694 
7695 		/*
7696 		 * Figure out the current pipe for the initial backlight setup.
7697 		 * If the current pipe isn't valid, try the PPS pipe, and if that
7698 		 * fails just assume pipe A.
7699 		 */
7700 		pipe = vlv_active_pipe(intel_dp);
7701 
7702 		if (pipe != PIPE_A && pipe != PIPE_B)
7703 			pipe = intel_dp->pps_pipe;
7704 
7705 		if (pipe != PIPE_A && pipe != PIPE_B)
7706 			pipe = PIPE_A;
7707 
7708 		drm_dbg_kms(&dev_priv->drm,
7709 			    "using pipe %c for initial backlight setup\n",
7710 			    pipe_name(pipe));
7711 	}
7712 
7713 	intel_panel_init(&intel_connector->panel, fixed_mode, downclock_mode);
7714 	intel_connector->panel.backlight.power = intel_edp_backlight_power;
7715 	intel_panel_setup_backlight(connector, pipe);
7716 
7717 	if (fixed_mode) {
7718 		drm_connector_set_panel_orientation_with_quirk(connector,
7719 				dev_priv->vbt.orientation,
7720 				fixed_mode->hdisplay, fixed_mode->vdisplay);
7721 	}
7722 
7723 	return true;
7724 
7725 out_vdd_off:
7726 	cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
7727 	/*
7728 	 * vdd might still be enabled do to the delayed vdd off.
7729 	 * Make sure vdd is actually turned off here.
7730 	 */
7731 	with_pps_lock(intel_dp, wakeref)
7732 		edp_panel_vdd_off_sync(intel_dp);
7733 
7734 	return false;
7735 }
7736 
7737 static void intel_dp_modeset_retry_work_fn(struct work_struct *work)
7738 {
7739 	struct intel_connector *intel_connector;
7740 	struct drm_connector *connector;
7741 
7742 	intel_connector = container_of(work, typeof(*intel_connector),
7743 				       modeset_retry_work);
7744 	connector = &intel_connector->base;
7745 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
7746 		      connector->name);
7747 
7748 	/* Grab the locks before changing connector property*/
7749 	mutex_lock(&connector->dev->mode_config.mutex);
7750 	/* Set connector link status to BAD and send a Uevent to notify
7751 	 * userspace to do a modeset.
7752 	 */
7753 	drm_connector_set_link_status_property(connector,
7754 					       DRM_MODE_LINK_STATUS_BAD);
7755 	mutex_unlock(&connector->dev->mode_config.mutex);
7756 	/* Send Hotplug uevent so userspace can reprobe */
7757 	drm_kms_helper_hotplug_event(connector->dev);
7758 }
7759 
7760 bool
7761 intel_dp_init_connector(struct intel_digital_port *dig_port,
7762 			struct intel_connector *intel_connector)
7763 {
7764 	struct drm_connector *connector = &intel_connector->base;
7765 	struct intel_dp *intel_dp = &dig_port->dp;
7766 	struct intel_encoder *intel_encoder = &dig_port->base;
7767 	struct drm_device *dev = intel_encoder->base.dev;
7768 	struct drm_i915_private *dev_priv = to_i915(dev);
7769 	enum port port = intel_encoder->port;
7770 	enum phy phy = intel_port_to_phy(dev_priv, port);
7771 	int type;
7772 
7773 	/* Initialize the work for modeset in case of link train failure */
7774 	INIT_WORK(&intel_connector->modeset_retry_work,
7775 		  intel_dp_modeset_retry_work_fn);
7776 
7777 	if (drm_WARN(dev, dig_port->max_lanes < 1,
7778 		     "Not enough lanes (%d) for DP on [ENCODER:%d:%s]\n",
7779 		     dig_port->max_lanes, intel_encoder->base.base.id,
7780 		     intel_encoder->base.name))
7781 		return false;
7782 
7783 	intel_dp_set_source_rates(intel_dp);
7784 
7785 	intel_dp->reset_link_params = true;
7786 	intel_dp->pps_pipe = INVALID_PIPE;
7787 	intel_dp->active_pipe = INVALID_PIPE;
7788 
7789 	/* Preserve the current hw state. */
7790 	intel_dp->DP = intel_de_read(dev_priv, intel_dp->output_reg);
7791 	intel_dp->attached_connector = intel_connector;
7792 
7793 	if (intel_dp_is_port_edp(dev_priv, port)) {
7794 		/*
7795 		 * Currently we don't support eDP on TypeC ports, although in
7796 		 * theory it could work on TypeC legacy ports.
7797 		 */
7798 		drm_WARN_ON(dev, intel_phy_is_tc(dev_priv, phy));
7799 		type = DRM_MODE_CONNECTOR_eDP;
7800 	} else {
7801 		type = DRM_MODE_CONNECTOR_DisplayPort;
7802 	}
7803 
7804 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
7805 		intel_dp->active_pipe = vlv_active_pipe(intel_dp);
7806 
7807 	/*
7808 	 * For eDP we always set the encoder type to INTEL_OUTPUT_EDP, but
7809 	 * for DP the encoder type can be set by the caller to
7810 	 * INTEL_OUTPUT_UNKNOWN for DDI, so don't rewrite it.
7811 	 */
7812 	if (type == DRM_MODE_CONNECTOR_eDP)
7813 		intel_encoder->type = INTEL_OUTPUT_EDP;
7814 
7815 	/* eDP only on port B and/or C on vlv/chv */
7816 	if (drm_WARN_ON(dev, (IS_VALLEYVIEW(dev_priv) ||
7817 			      IS_CHERRYVIEW(dev_priv)) &&
7818 			intel_dp_is_edp(intel_dp) &&
7819 			port != PORT_B && port != PORT_C))
7820 		return false;
7821 
7822 	drm_dbg_kms(&dev_priv->drm,
7823 		    "Adding %s connector on [ENCODER:%d:%s]\n",
7824 		    type == DRM_MODE_CONNECTOR_eDP ? "eDP" : "DP",
7825 		    intel_encoder->base.base.id, intel_encoder->base.name);
7826 
7827 	drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
7828 	drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
7829 
7830 	if (!HAS_GMCH(dev_priv))
7831 		connector->interlace_allowed = true;
7832 	connector->doublescan_allowed = 0;
7833 
7834 	intel_connector->polled = DRM_CONNECTOR_POLL_HPD;
7835 
7836 	intel_dp_aux_init(intel_dp);
7837 
7838 	intel_connector_attach_encoder(intel_connector, intel_encoder);
7839 
7840 	if (HAS_DDI(dev_priv))
7841 		intel_connector->get_hw_state = intel_ddi_connector_get_hw_state;
7842 	else
7843 		intel_connector->get_hw_state = intel_connector_get_hw_state;
7844 
7845 	/* init MST on ports that can support it */
7846 	intel_dp_mst_encoder_init(dig_port,
7847 				  intel_connector->base.base.id);
7848 
7849 	if (!intel_edp_init_connector(intel_dp, intel_connector)) {
7850 		intel_dp_aux_fini(intel_dp);
7851 		intel_dp_mst_encoder_cleanup(dig_port);
7852 		goto fail;
7853 	}
7854 
7855 	intel_dp_add_properties(intel_dp, connector);
7856 
7857 	if (is_hdcp_supported(dev_priv, port) && !intel_dp_is_edp(intel_dp)) {
7858 		int ret = intel_dp_init_hdcp(dig_port, intel_connector);
7859 		if (ret)
7860 			drm_dbg_kms(&dev_priv->drm,
7861 				    "HDCP init failed, skipping.\n");
7862 	}
7863 
7864 	/* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
7865 	 * 0xd.  Failure to do so will result in spurious interrupts being
7866 	 * generated on the port when a cable is not attached.
7867 	 */
7868 	if (IS_G45(dev_priv)) {
7869 		u32 temp = intel_de_read(dev_priv, PEG_BAND_GAP_DATA);
7870 		intel_de_write(dev_priv, PEG_BAND_GAP_DATA,
7871 			       (temp & ~0xf) | 0xd);
7872 	}
7873 
7874 	return true;
7875 
7876 fail:
7877 	drm_connector_cleanup(connector);
7878 
7879 	return false;
7880 }
7881 
7882 bool intel_dp_init(struct drm_i915_private *dev_priv,
7883 		   i915_reg_t output_reg,
7884 		   enum port port)
7885 {
7886 	struct intel_digital_port *dig_port;
7887 	struct intel_encoder *intel_encoder;
7888 	struct drm_encoder *encoder;
7889 	struct intel_connector *intel_connector;
7890 
7891 	dig_port = kzalloc(sizeof(*dig_port), GFP_KERNEL);
7892 	if (!dig_port)
7893 		return false;
7894 
7895 	intel_connector = intel_connector_alloc();
7896 	if (!intel_connector)
7897 		goto err_connector_alloc;
7898 
7899 	intel_encoder = &dig_port->base;
7900 	encoder = &intel_encoder->base;
7901 
7902 	rw_init(&dig_port->hdcp_mutex, "dphdcp");
7903 
7904 	if (drm_encoder_init(&dev_priv->drm, &intel_encoder->base,
7905 			     &intel_dp_enc_funcs, DRM_MODE_ENCODER_TMDS,
7906 			     "DP %c", port_name(port)))
7907 		goto err_encoder_init;
7908 
7909 	intel_encoder->hotplug = intel_dp_hotplug;
7910 	intel_encoder->compute_config = intel_dp_compute_config;
7911 	intel_encoder->get_hw_state = intel_dp_get_hw_state;
7912 	intel_encoder->get_config = intel_dp_get_config;
7913 	intel_encoder->update_pipe = intel_panel_update_backlight;
7914 	intel_encoder->suspend = intel_dp_encoder_suspend;
7915 	if (IS_CHERRYVIEW(dev_priv)) {
7916 		intel_encoder->pre_pll_enable = chv_dp_pre_pll_enable;
7917 		intel_encoder->pre_enable = chv_pre_enable_dp;
7918 		intel_encoder->enable = vlv_enable_dp;
7919 		intel_encoder->disable = vlv_disable_dp;
7920 		intel_encoder->post_disable = chv_post_disable_dp;
7921 		intel_encoder->post_pll_disable = chv_dp_post_pll_disable;
7922 	} else if (IS_VALLEYVIEW(dev_priv)) {
7923 		intel_encoder->pre_pll_enable = vlv_dp_pre_pll_enable;
7924 		intel_encoder->pre_enable = vlv_pre_enable_dp;
7925 		intel_encoder->enable = vlv_enable_dp;
7926 		intel_encoder->disable = vlv_disable_dp;
7927 		intel_encoder->post_disable = vlv_post_disable_dp;
7928 	} else {
7929 		intel_encoder->pre_enable = g4x_pre_enable_dp;
7930 		intel_encoder->enable = g4x_enable_dp;
7931 		intel_encoder->disable = g4x_disable_dp;
7932 		intel_encoder->post_disable = g4x_post_disable_dp;
7933 	}
7934 
7935 	if ((IS_IVYBRIDGE(dev_priv) && port == PORT_A) ||
7936 	    (HAS_PCH_CPT(dev_priv) && port != PORT_A))
7937 		dig_port->dp.set_link_train = cpt_set_link_train;
7938 	else
7939 		dig_port->dp.set_link_train = g4x_set_link_train;
7940 
7941 	if (IS_CHERRYVIEW(dev_priv))
7942 		dig_port->dp.set_signal_levels = chv_set_signal_levels;
7943 	else if (IS_VALLEYVIEW(dev_priv))
7944 		dig_port->dp.set_signal_levels = vlv_set_signal_levels;
7945 	else if (IS_IVYBRIDGE(dev_priv) && port == PORT_A)
7946 		dig_port->dp.set_signal_levels = ivb_cpu_edp_set_signal_levels;
7947 	else if (IS_GEN(dev_priv, 6) && port == PORT_A)
7948 		dig_port->dp.set_signal_levels = snb_cpu_edp_set_signal_levels;
7949 	else
7950 		dig_port->dp.set_signal_levels = g4x_set_signal_levels;
7951 
7952 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv) ||
7953 	    (HAS_PCH_SPLIT(dev_priv) && port != PORT_A)) {
7954 		dig_port->dp.preemph_max = intel_dp_pre_empemph_max_3;
7955 		dig_port->dp.voltage_max = intel_dp_voltage_max_3;
7956 	} else {
7957 		dig_port->dp.preemph_max = intel_dp_pre_empemph_max_2;
7958 		dig_port->dp.voltage_max = intel_dp_voltage_max_2;
7959 	}
7960 
7961 	dig_port->dp.output_reg = output_reg;
7962 	dig_port->max_lanes = 4;
7963 	dig_port->dp.regs.dp_tp_ctl = DP_TP_CTL(port);
7964 	dig_port->dp.regs.dp_tp_status = DP_TP_STATUS(port);
7965 
7966 	intel_encoder->type = INTEL_OUTPUT_DP;
7967 	intel_encoder->power_domain = intel_port_to_power_domain(port);
7968 	if (IS_CHERRYVIEW(dev_priv)) {
7969 		if (port == PORT_D)
7970 			intel_encoder->pipe_mask = BIT(PIPE_C);
7971 		else
7972 			intel_encoder->pipe_mask = BIT(PIPE_A) | BIT(PIPE_B);
7973 	} else {
7974 		intel_encoder->pipe_mask = ~0;
7975 	}
7976 	intel_encoder->cloneable = 0;
7977 	intel_encoder->port = port;
7978 	intel_encoder->hpd_pin = intel_hpd_pin_default(dev_priv, port);
7979 
7980 	dig_port->hpd_pulse = intel_dp_hpd_pulse;
7981 
7982 	if (HAS_GMCH(dev_priv)) {
7983 		if (IS_GM45(dev_priv))
7984 			dig_port->connected = gm45_digital_port_connected;
7985 		else
7986 			dig_port->connected = g4x_digital_port_connected;
7987 	} else {
7988 		if (port == PORT_A)
7989 			dig_port->connected = ilk_digital_port_connected;
7990 		else
7991 			dig_port->connected = ibx_digital_port_connected;
7992 	}
7993 
7994 	if (port != PORT_A)
7995 		intel_infoframe_init(dig_port);
7996 
7997 	dig_port->aux_ch = intel_bios_port_aux_ch(dev_priv, port);
7998 	if (!intel_dp_init_connector(dig_port, intel_connector))
7999 		goto err_init_connector;
8000 
8001 	return true;
8002 
8003 err_init_connector:
8004 	drm_encoder_cleanup(encoder);
8005 err_encoder_init:
8006 	kfree(intel_connector);
8007 err_connector_alloc:
8008 	kfree(dig_port);
8009 	return false;
8010 }
8011 
8012 void intel_dp_mst_suspend(struct drm_i915_private *dev_priv)
8013 {
8014 	struct intel_encoder *encoder;
8015 
8016 	for_each_intel_encoder(&dev_priv->drm, encoder) {
8017 		struct intel_dp *intel_dp;
8018 
8019 		if (encoder->type != INTEL_OUTPUT_DDI)
8020 			continue;
8021 
8022 		intel_dp = enc_to_intel_dp(encoder);
8023 
8024 		if (!intel_dp->can_mst)
8025 			continue;
8026 
8027 		if (intel_dp->is_mst)
8028 			drm_dp_mst_topology_mgr_suspend(&intel_dp->mst_mgr);
8029 	}
8030 }
8031 
8032 void intel_dp_mst_resume(struct drm_i915_private *dev_priv)
8033 {
8034 	struct intel_encoder *encoder;
8035 
8036 	for_each_intel_encoder(&dev_priv->drm, encoder) {
8037 		struct intel_dp *intel_dp;
8038 		int ret;
8039 
8040 		if (encoder->type != INTEL_OUTPUT_DDI)
8041 			continue;
8042 
8043 		intel_dp = enc_to_intel_dp(encoder);
8044 
8045 		if (!intel_dp->can_mst)
8046 			continue;
8047 
8048 		ret = drm_dp_mst_topology_mgr_resume(&intel_dp->mst_mgr,
8049 						     true);
8050 		if (ret) {
8051 			intel_dp->is_mst = false;
8052 			drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr,
8053 							false);
8054 		}
8055 	}
8056 }
8057