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