drm/i915: Rename SNB/IVB CPU eDP signal level funcs
[linux-block.git] / drivers / gpu / drm / i915 / intel_dp.c
CommitLineData
a4fc5ed6
KP
1/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Keith Packard <keithp@keithp.com>
25 *
26 */
27
28#include <linux/i2c.h>
5a0e3ad6 29#include <linux/slab.h>
2d1a8a48 30#include <linux/export.h>
611032bf 31#include <linux/types.h>
01527b31
CT
32#include <linux/notifier.h>
33#include <linux/reboot.h>
611032bf 34#include <asm/byteorder.h>
760285e7 35#include <drm/drmP.h>
c6f95f27 36#include <drm/drm_atomic_helper.h>
760285e7
DH
37#include <drm/drm_crtc.h>
38#include <drm/drm_crtc_helper.h>
20f24d77 39#include <drm/drm_dp_helper.h>
760285e7 40#include <drm/drm_edid.h>
20f24d77 41#include <drm/drm_hdcp.h>
a4fc5ed6 42#include "intel_drv.h"
760285e7 43#include <drm/i915_drm.h>
a4fc5ed6 44#include "i915_drv.h"
a4fc5ed6 45
e8b2577c 46#define DP_DPRX_ESI_LEN 14
a4fc5ed6 47
559be30c
TP
48/* Compliance test status bits */
49#define INTEL_DP_RESOLUTION_SHIFT_MASK 0
50#define INTEL_DP_RESOLUTION_PREFERRED (1 << INTEL_DP_RESOLUTION_SHIFT_MASK)
51#define INTEL_DP_RESOLUTION_STANDARD (2 << INTEL_DP_RESOLUTION_SHIFT_MASK)
52#define INTEL_DP_RESOLUTION_FAILSAFE (3 << INTEL_DP_RESOLUTION_SHIFT_MASK)
53
9dd4ffdf 54struct dp_link_dpll {
840b32b7 55 int clock;
9dd4ffdf
CML
56 struct dpll dpll;
57};
58
59static const struct dp_link_dpll gen4_dpll[] = {
840b32b7 60 { 162000,
9dd4ffdf 61 { .p1 = 2, .p2 = 10, .n = 2, .m1 = 23, .m2 = 8 } },
840b32b7 62 { 270000,
9dd4ffdf
CML
63 { .p1 = 1, .p2 = 10, .n = 1, .m1 = 14, .m2 = 2 } }
64};
65
66static const struct dp_link_dpll pch_dpll[] = {
840b32b7 67 { 162000,
9dd4ffdf 68 { .p1 = 2, .p2 = 10, .n = 1, .m1 = 12, .m2 = 9 } },
840b32b7 69 { 270000,
9dd4ffdf
CML
70 { .p1 = 1, .p2 = 10, .n = 2, .m1 = 14, .m2 = 8 } }
71};
72
65ce4bf5 73static const struct dp_link_dpll vlv_dpll[] = {
840b32b7 74 { 162000,
58f6e632 75 { .p1 = 3, .p2 = 2, .n = 5, .m1 = 3, .m2 = 81 } },
840b32b7 76 { 270000,
65ce4bf5
CML
77 { .p1 = 2, .p2 = 2, .n = 1, .m1 = 2, .m2 = 27 } }
78};
79
ef9348c8
CML
80/*
81 * CHV supports eDP 1.4 that have more link rates.
82 * Below only provides the fixed rate but exclude variable rate.
83 */
84static const struct dp_link_dpll chv_dpll[] = {
85 /*
86 * CHV requires to program fractional division for m2.
87 * m2 is stored in fixed point format using formula below
88 * (m2_int << 22) | m2_fraction
89 */
840b32b7 90 { 162000, /* m2_int = 32, m2_fraction = 1677722 */
ef9348c8 91 { .p1 = 4, .p2 = 2, .n = 1, .m1 = 2, .m2 = 0x819999a } },
840b32b7 92 { 270000, /* m2_int = 27, m2_fraction = 0 */
ef9348c8 93 { .p1 = 4, .p2 = 1, .n = 1, .m1 = 2, .m2 = 0x6c00000 } },
ef9348c8 94};
637a9c63 95
cfcb0fc9 96/**
1853a9da 97 * intel_dp_is_edp - is the given port attached to an eDP panel (either CPU or PCH)
cfcb0fc9
JB
98 * @intel_dp: DP struct
99 *
100 * If a CPU or PCH DP output is attached to an eDP panel, this function
101 * will return true, and false otherwise.
102 */
1853a9da 103bool intel_dp_is_edp(struct intel_dp *intel_dp)
cfcb0fc9 104{
da63a9f2
PZ
105 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
106
107 return intel_dig_port->base.type == INTEL_OUTPUT_EDP;
cfcb0fc9
JB
108}
109
68b4d824 110static struct drm_device *intel_dp_to_dev(struct intel_dp *intel_dp)
cfcb0fc9 111{
68b4d824
ID
112 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
113
114 return intel_dig_port->base.base.dev;
cfcb0fc9
JB
115}
116
df0e9248
CW
117static struct intel_dp *intel_attached_dp(struct drm_connector *connector)
118{
fa90ecef 119 return enc_to_intel_dp(&intel_attached_encoder(connector)->base);
df0e9248
CW
120}
121
adc10304
VS
122static void intel_dp_link_down(struct intel_encoder *encoder,
123 const struct intel_crtc_state *old_crtc_state);
1e0560e0 124static bool edp_panel_vdd_on(struct intel_dp *intel_dp);
4be73780 125static void edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync);
adc10304
VS
126static void vlv_init_panel_power_sequencer(struct intel_encoder *encoder,
127 const struct intel_crtc_state *crtc_state);
46bd8383 128static void vlv_steal_power_sequencer(struct drm_i915_private *dev_priv,
a8c3344e 129 enum pipe pipe);
f21a2198 130static void intel_dp_unset_edid(struct intel_dp *intel_dp);
a4fc5ed6 131
68f357cb
JN
132/* update sink rates from dpcd */
133static void intel_dp_set_sink_rates(struct intel_dp *intel_dp)
134{
229675d5 135 static const int dp_rates[] = {
c71b53cc 136 162000, 270000, 540000, 810000
229675d5 137 };
a8a08886 138 int i, max_rate;
68f357cb 139
a8a08886 140 max_rate = drm_dp_bw_code_to_link_rate(intel_dp->dpcd[DP_MAX_LINK_RATE]);
68f357cb 141
229675d5
JN
142 for (i = 0; i < ARRAY_SIZE(dp_rates); i++) {
143 if (dp_rates[i] > max_rate)
a8a08886 144 break;
229675d5 145 intel_dp->sink_rates[i] = dp_rates[i];
a8a08886 146 }
68f357cb 147
a8a08886 148 intel_dp->num_sink_rates = i;
68f357cb
JN
149}
150
10ebb736
JN
151/* Get length of rates array potentially limited by max_rate. */
152static int intel_dp_rate_limit_len(const int *rates, int len, int max_rate)
153{
154 int i;
155
156 /* Limit results by potentially reduced max rate */
157 for (i = 0; i < len; i++) {
158 if (rates[len - i - 1] <= max_rate)
159 return len - i;
160 }
161
162 return 0;
163}
164
165/* Get length of common rates array potentially limited by max_rate. */
166static int intel_dp_common_len_rate_limit(const struct intel_dp *intel_dp,
167 int max_rate)
168{
169 return intel_dp_rate_limit_len(intel_dp->common_rates,
170 intel_dp->num_common_rates, max_rate);
171}
172
540b0b7f
JN
173/* Theoretical max between source and sink */
174static int intel_dp_max_common_rate(struct intel_dp *intel_dp)
a4fc5ed6 175{
540b0b7f 176 return intel_dp->common_rates[intel_dp->num_common_rates - 1];
a4fc5ed6
KP
177}
178
540b0b7f
JN
179/* Theoretical max between source and sink */
180static int intel_dp_max_common_lane_count(struct intel_dp *intel_dp)
eeb6324d
PZ
181{
182 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
540b0b7f
JN
183 int source_max = intel_dig_port->max_lanes;
184 int sink_max = drm_dp_max_lane_count(intel_dp->dpcd);
eeb6324d
PZ
185
186 return min(source_max, sink_max);
187}
188
3d65a735 189int intel_dp_max_lane_count(struct intel_dp *intel_dp)
540b0b7f
JN
190{
191 return intel_dp->max_link_lane_count;
192}
193
22a2c8e0 194int
c898261c 195intel_dp_link_required(int pixel_clock, int bpp)
a4fc5ed6 196{
fd81c44e
DP
197 /* pixel_clock is in kHz, divide bpp by 8 for bit to Byte conversion */
198 return DIV_ROUND_UP(pixel_clock * bpp, 8);
a4fc5ed6
KP
199}
200
22a2c8e0 201int
fe27d53e
DA
202intel_dp_max_data_rate(int max_link_clock, int max_lanes)
203{
fd81c44e
DP
204 /* max_link_clock is the link symbol clock (LS_Clk) in kHz and not the
205 * link rate that is generally expressed in Gbps. Since, 8 bits of data
206 * is transmitted every LS_Clk per lane, there is no need to account for
207 * the channel encoding that is done in the PHY layer here.
208 */
209
210 return max_link_clock * max_lanes;
fe27d53e
DA
211}
212
70ec0645
MK
213static int
214intel_dp_downstream_max_dotclock(struct intel_dp *intel_dp)
215{
216 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
217 struct intel_encoder *encoder = &intel_dig_port->base;
218 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
219 int max_dotclk = dev_priv->max_dotclk_freq;
220 int ds_max_dotclk;
221
222 int type = intel_dp->downstream_ports[0] & DP_DS_PORT_TYPE_MASK;
223
224 if (type != DP_DS_PORT_TYPE_VGA)
225 return max_dotclk;
226
227 ds_max_dotclk = drm_dp_downstream_max_clock(intel_dp->dpcd,
228 intel_dp->downstream_ports);
229
230 if (ds_max_dotclk != 0)
231 max_dotclk = min(max_dotclk, ds_max_dotclk);
232
233 return max_dotclk;
234}
235
4ba285d4 236static int cnl_max_source_rate(struct intel_dp *intel_dp)
53ddb3cd
RV
237{
238 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
239 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
240 enum port port = dig_port->base.port;
241
242 u32 voltage = I915_READ(CNL_PORT_COMP_DW3) & VOLTAGE_INFO_MASK;
243
244 /* Low voltage SKUs are limited to max of 5.4G */
245 if (voltage == VOLTAGE_INFO_0_85V)
4ba285d4 246 return 540000;
53ddb3cd
RV
247
248 /* For this SKU 8.1G is supported in all ports */
249 if (IS_CNL_WITH_PORT_F(dev_priv))
4ba285d4 250 return 810000;
53ddb3cd 251
3758d968 252 /* For other SKUs, max rate on ports A and D is 5.4G */
53ddb3cd 253 if (port == PORT_A || port == PORT_D)
4ba285d4 254 return 540000;
53ddb3cd 255
4ba285d4 256 return 810000;
53ddb3cd
RV
257}
258
55cfc580
JN
259static void
260intel_dp_set_source_rates(struct intel_dp *intel_dp)
40dba341 261{
229675d5
JN
262 /* The values must be in increasing order */
263 static const int cnl_rates[] = {
264 162000, 216000, 270000, 324000, 432000, 540000, 648000, 810000
265 };
266 static const int bxt_rates[] = {
267 162000, 216000, 243000, 270000, 324000, 432000, 540000
268 };
269 static const int skl_rates[] = {
270 162000, 216000, 270000, 324000, 432000, 540000
271 };
272 static const int hsw_rates[] = {
273 162000, 270000, 540000
274 };
275 static const int g4x_rates[] = {
276 162000, 270000
277 };
40dba341
NM
278 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
279 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
99b91bda
JN
280 const struct ddi_vbt_port_info *info =
281 &dev_priv->vbt.ddi_port_info[dig_port->base.port];
55cfc580 282 const int *source_rates;
99b91bda 283 int size, max_rate = 0, vbt_max_rate = info->dp_max_link_rate;
40dba341 284
55cfc580
JN
285 /* This should only be done once */
286 WARN_ON(intel_dp->source_rates || intel_dp->num_source_rates);
287
ba1c06a5 288 if (IS_CANNONLAKE(dev_priv)) {
d907b665 289 source_rates = cnl_rates;
4ba285d4
JN
290 size = ARRAY_SIZE(cnl_rates);
291 max_rate = cnl_max_source_rate(intel_dp);
ba1c06a5
MN
292 } else if (IS_GEN9_LP(dev_priv)) {
293 source_rates = bxt_rates;
294 size = ARRAY_SIZE(bxt_rates);
b976dc53 295 } else if (IS_GEN9_BC(dev_priv)) {
55cfc580 296 source_rates = skl_rates;
40dba341 297 size = ARRAY_SIZE(skl_rates);
fc603ca7
JN
298 } else if ((IS_HASWELL(dev_priv) && !IS_HSW_ULX(dev_priv)) ||
299 IS_BROADWELL(dev_priv)) {
229675d5
JN
300 source_rates = hsw_rates;
301 size = ARRAY_SIZE(hsw_rates);
fc603ca7 302 } else {
229675d5
JN
303 source_rates = g4x_rates;
304 size = ARRAY_SIZE(g4x_rates);
40dba341
NM
305 }
306
99b91bda
JN
307 if (max_rate && vbt_max_rate)
308 max_rate = min(max_rate, vbt_max_rate);
309 else if (vbt_max_rate)
310 max_rate = vbt_max_rate;
311
4ba285d4
JN
312 if (max_rate)
313 size = intel_dp_rate_limit_len(source_rates, size, max_rate);
314
55cfc580
JN
315 intel_dp->source_rates = source_rates;
316 intel_dp->num_source_rates = size;
40dba341
NM
317}
318
319static int intersect_rates(const int *source_rates, int source_len,
320 const int *sink_rates, int sink_len,
321 int *common_rates)
322{
323 int i = 0, j = 0, k = 0;
324
325 while (i < source_len && j < sink_len) {
326 if (source_rates[i] == sink_rates[j]) {
327 if (WARN_ON(k >= DP_MAX_SUPPORTED_RATES))
328 return k;
329 common_rates[k] = source_rates[i];
330 ++k;
331 ++i;
332 ++j;
333 } else if (source_rates[i] < sink_rates[j]) {
334 ++i;
335 } else {
336 ++j;
337 }
338 }
339 return k;
340}
341
8001b754
JN
342/* return index of rate in rates array, or -1 if not found */
343static int intel_dp_rate_index(const int *rates, int len, int rate)
344{
345 int i;
346
347 for (i = 0; i < len; i++)
348 if (rate == rates[i])
349 return i;
350
351 return -1;
352}
353
975ee5fc 354static void intel_dp_set_common_rates(struct intel_dp *intel_dp)
40dba341 355{
975ee5fc 356 WARN_ON(!intel_dp->num_source_rates || !intel_dp->num_sink_rates);
40dba341 357
975ee5fc
JN
358 intel_dp->num_common_rates = intersect_rates(intel_dp->source_rates,
359 intel_dp->num_source_rates,
360 intel_dp->sink_rates,
361 intel_dp->num_sink_rates,
362 intel_dp->common_rates);
363
364 /* Paranoia, there should always be something in common. */
365 if (WARN_ON(intel_dp->num_common_rates == 0)) {
229675d5 366 intel_dp->common_rates[0] = 162000;
975ee5fc
JN
367 intel_dp->num_common_rates = 1;
368 }
369}
370
1a92c70e
MN
371static bool intel_dp_link_params_valid(struct intel_dp *intel_dp, int link_rate,
372 uint8_t lane_count)
14c562c0
MN
373{
374 /*
375 * FIXME: we need to synchronize the current link parameters with
376 * hardware readout. Currently fast link training doesn't work on
377 * boot-up.
378 */
1a92c70e
MN
379 if (link_rate == 0 ||
380 link_rate > intel_dp->max_link_rate)
14c562c0
MN
381 return false;
382
1a92c70e
MN
383 if (lane_count == 0 ||
384 lane_count > intel_dp_max_lane_count(intel_dp))
14c562c0
MN
385 return false;
386
387 return true;
388}
389
fdb14d33
MN
390int intel_dp_get_link_train_fallback_values(struct intel_dp *intel_dp,
391 int link_rate, uint8_t lane_count)
392{
b1810a74 393 int index;
fdb14d33 394
b1810a74
JN
395 index = intel_dp_rate_index(intel_dp->common_rates,
396 intel_dp->num_common_rates,
397 link_rate);
398 if (index > 0) {
e6c0c64a
JN
399 intel_dp->max_link_rate = intel_dp->common_rates[index - 1];
400 intel_dp->max_link_lane_count = lane_count;
fdb14d33 401 } else if (lane_count > 1) {
540b0b7f 402 intel_dp->max_link_rate = intel_dp_max_common_rate(intel_dp);
e6c0c64a 403 intel_dp->max_link_lane_count = lane_count >> 1;
fdb14d33
MN
404 } else {
405 DRM_ERROR("Link Training Unsuccessful\n");
406 return -1;
407 }
408
409 return 0;
410}
411
c19de8eb 412static enum drm_mode_status
a4fc5ed6
KP
413intel_dp_mode_valid(struct drm_connector *connector,
414 struct drm_display_mode *mode)
415{
df0e9248 416 struct intel_dp *intel_dp = intel_attached_dp(connector);
dd06f90e
JN
417 struct intel_connector *intel_connector = to_intel_connector(connector);
418 struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
36008365
DV
419 int target_clock = mode->clock;
420 int max_rate, mode_rate, max_lanes, max_link_clock;
70ec0645
MK
421 int max_dotclk;
422
423 max_dotclk = intel_dp_downstream_max_dotclock(intel_dp);
a4fc5ed6 424
1853a9da 425 if (intel_dp_is_edp(intel_dp) && fixed_mode) {
dd06f90e 426 if (mode->hdisplay > fixed_mode->hdisplay)
7de56f43
ZY
427 return MODE_PANEL;
428
dd06f90e 429 if (mode->vdisplay > fixed_mode->vdisplay)
7de56f43 430 return MODE_PANEL;
03afc4a2
DV
431
432 target_clock = fixed_mode->clock;
7de56f43
ZY
433 }
434
50fec21a 435 max_link_clock = intel_dp_max_link_rate(intel_dp);
eeb6324d 436 max_lanes = intel_dp_max_lane_count(intel_dp);
36008365
DV
437
438 max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
439 mode_rate = intel_dp_link_required(target_clock, 18);
440
799487f5 441 if (mode_rate > max_rate || target_clock > max_dotclk)
c4867936 442 return MODE_CLOCK_HIGH;
a4fc5ed6
KP
443
444 if (mode->clock < 10000)
445 return MODE_CLOCK_LOW;
446
0af78a2b
DV
447 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
448 return MODE_H_ILLEGAL;
449
a4fc5ed6
KP
450 return MODE_OK;
451}
452
a4f1289e 453uint32_t intel_dp_pack_aux(const uint8_t *src, int src_bytes)
a4fc5ed6
KP
454{
455 int i;
456 uint32_t v = 0;
457
458 if (src_bytes > 4)
459 src_bytes = 4;
460 for (i = 0; i < src_bytes; i++)
461 v |= ((uint32_t) src[i]) << ((3-i) * 8);
462 return v;
463}
464
c2af70e2 465static void intel_dp_unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
a4fc5ed6
KP
466{
467 int i;
468 if (dst_bytes > 4)
469 dst_bytes = 4;
470 for (i = 0; i < dst_bytes; i++)
471 dst[i] = src >> ((3-i) * 8);
472}
473
bf13e81b 474static void
46bd8383 475intel_dp_init_panel_power_sequencer(struct intel_dp *intel_dp);
bf13e81b 476static void
46bd8383 477intel_dp_init_panel_power_sequencer_registers(struct intel_dp *intel_dp,
5d5ab2d2 478 bool force_disable_vdd);
335f752b 479static void
46bd8383 480intel_dp_pps_init(struct intel_dp *intel_dp);
bf13e81b 481
773538e8
VS
482static void pps_lock(struct intel_dp *intel_dp)
483{
2f773477 484 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
773538e8
VS
485
486 /*
40c7ae45 487 * See intel_power_sequencer_reset() why we need
773538e8
VS
488 * a power domain reference here.
489 */
5432fcaf 490 intel_display_power_get(dev_priv, intel_dp->aux_power_domain);
773538e8
VS
491
492 mutex_lock(&dev_priv->pps_mutex);
493}
494
495static void pps_unlock(struct intel_dp *intel_dp)
496{
2f773477 497 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
773538e8
VS
498
499 mutex_unlock(&dev_priv->pps_mutex);
500
5432fcaf 501 intel_display_power_put(dev_priv, intel_dp->aux_power_domain);
773538e8
VS
502}
503
961a0db0
VS
504static void
505vlv_power_sequencer_kick(struct intel_dp *intel_dp)
506{
2f773477 507 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
961a0db0 508 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
961a0db0 509 enum pipe pipe = intel_dp->pps_pipe;
0047eedc
VS
510 bool pll_enabled, release_cl_override = false;
511 enum dpio_phy phy = DPIO_PHY(pipe);
512 enum dpio_channel ch = vlv_pipe_to_channel(pipe);
961a0db0
VS
513 uint32_t DP;
514
515 if (WARN(I915_READ(intel_dp->output_reg) & DP_PORT_EN,
e7f2af78 516 "skipping pipe %c power sequencer kick due to port %c being active\n",
8f4f2797 517 pipe_name(pipe), port_name(intel_dig_port->base.port)))
961a0db0
VS
518 return;
519
520 DRM_DEBUG_KMS("kicking pipe %c power sequencer for port %c\n",
8f4f2797 521 pipe_name(pipe), port_name(intel_dig_port->base.port));
961a0db0
VS
522
523 /* Preserve the BIOS-computed detected bit. This is
524 * supposed to be read-only.
525 */
526 DP = I915_READ(intel_dp->output_reg) & DP_DETECTED;
527 DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
528 DP |= DP_PORT_WIDTH(1);
529 DP |= DP_LINK_TRAIN_PAT_1;
530
920a14b2 531 if (IS_CHERRYVIEW(dev_priv))
961a0db0
VS
532 DP |= DP_PIPE_SELECT_CHV(pipe);
533 else if (pipe == PIPE_B)
534 DP |= DP_PIPEB_SELECT;
535
d288f65f
VS
536 pll_enabled = I915_READ(DPLL(pipe)) & DPLL_VCO_ENABLE;
537
538 /*
539 * The DPLL for the pipe must be enabled for this to work.
540 * So enable temporarily it if it's not already enabled.
541 */
0047eedc 542 if (!pll_enabled) {
920a14b2 543 release_cl_override = IS_CHERRYVIEW(dev_priv) &&
0047eedc
VS
544 !chv_phy_powergate_ch(dev_priv, phy, ch, true);
545
30ad9814 546 if (vlv_force_pll_on(dev_priv, pipe, IS_CHERRYVIEW(dev_priv) ?
3f36b937
TU
547 &chv_dpll[0].dpll : &vlv_dpll[0].dpll)) {
548 DRM_ERROR("Failed to force on pll for pipe %c!\n",
549 pipe_name(pipe));
550 return;
551 }
0047eedc 552 }
d288f65f 553
961a0db0
VS
554 /*
555 * Similar magic as in intel_dp_enable_port().
556 * We _must_ do this port enable + disable trick
e7f2af78 557 * to make this power sequencer lock onto the port.
961a0db0
VS
558 * Otherwise even VDD force bit won't work.
559 */
560 I915_WRITE(intel_dp->output_reg, DP);
561 POSTING_READ(intel_dp->output_reg);
562
563 I915_WRITE(intel_dp->output_reg, DP | DP_PORT_EN);
564 POSTING_READ(intel_dp->output_reg);
565
566 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
567 POSTING_READ(intel_dp->output_reg);
d288f65f 568
0047eedc 569 if (!pll_enabled) {
30ad9814 570 vlv_force_pll_off(dev_priv, pipe);
0047eedc
VS
571
572 if (release_cl_override)
573 chv_phy_powergate_ch(dev_priv, phy, ch, false);
574 }
961a0db0
VS
575}
576
9f2bdb00
VS
577static enum pipe vlv_find_free_pps(struct drm_i915_private *dev_priv)
578{
579 struct intel_encoder *encoder;
580 unsigned int pipes = (1 << PIPE_A) | (1 << PIPE_B);
581
582 /*
583 * We don't have power sequencer currently.
584 * Pick one that's not used by other ports.
585 */
586 for_each_intel_encoder(&dev_priv->drm, encoder) {
587 struct intel_dp *intel_dp;
588
589 if (encoder->type != INTEL_OUTPUT_DP &&
590 encoder->type != INTEL_OUTPUT_EDP)
591 continue;
592
593 intel_dp = enc_to_intel_dp(&encoder->base);
594
595 if (encoder->type == INTEL_OUTPUT_EDP) {
596 WARN_ON(intel_dp->active_pipe != INVALID_PIPE &&
597 intel_dp->active_pipe != intel_dp->pps_pipe);
598
599 if (intel_dp->pps_pipe != INVALID_PIPE)
600 pipes &= ~(1 << intel_dp->pps_pipe);
601 } else {
602 WARN_ON(intel_dp->pps_pipe != INVALID_PIPE);
603
604 if (intel_dp->active_pipe != INVALID_PIPE)
605 pipes &= ~(1 << intel_dp->active_pipe);
606 }
607 }
608
609 if (pipes == 0)
610 return INVALID_PIPE;
611
612 return ffs(pipes) - 1;
613}
614
bf13e81b
JN
615static enum pipe
616vlv_power_sequencer_pipe(struct intel_dp *intel_dp)
617{
46bd8383 618 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
bf13e81b 619 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
a8c3344e 620 enum pipe pipe;
bf13e81b 621
e39b999a 622 lockdep_assert_held(&dev_priv->pps_mutex);
bf13e81b 623
a8c3344e 624 /* We should never land here with regular DP ports */
1853a9da 625 WARN_ON(!intel_dp_is_edp(intel_dp));
a8c3344e 626
9f2bdb00
VS
627 WARN_ON(intel_dp->active_pipe != INVALID_PIPE &&
628 intel_dp->active_pipe != intel_dp->pps_pipe);
629
a4a5d2f8
VS
630 if (intel_dp->pps_pipe != INVALID_PIPE)
631 return intel_dp->pps_pipe;
632
9f2bdb00 633 pipe = vlv_find_free_pps(dev_priv);
a4a5d2f8
VS
634
635 /*
636 * Didn't find one. This should not happen since there
637 * are two power sequencers and up to two eDP ports.
638 */
9f2bdb00 639 if (WARN_ON(pipe == INVALID_PIPE))
a8c3344e 640 pipe = PIPE_A;
a4a5d2f8 641
46bd8383 642 vlv_steal_power_sequencer(dev_priv, pipe);
a8c3344e 643 intel_dp->pps_pipe = pipe;
a4a5d2f8
VS
644
645 DRM_DEBUG_KMS("picked pipe %c power sequencer for port %c\n",
646 pipe_name(intel_dp->pps_pipe),
8f4f2797 647 port_name(intel_dig_port->base.port));
a4a5d2f8
VS
648
649 /* init power sequencer on this pipe and port */
46bd8383
VS
650 intel_dp_init_panel_power_sequencer(intel_dp);
651 intel_dp_init_panel_power_sequencer_registers(intel_dp, true);
a4a5d2f8 652
961a0db0
VS
653 /*
654 * Even vdd force doesn't work until we've made
655 * the power sequencer lock in on the port.
656 */
657 vlv_power_sequencer_kick(intel_dp);
a4a5d2f8
VS
658
659 return intel_dp->pps_pipe;
660}
661
78597996
ID
662static int
663bxt_power_sequencer_idx(struct intel_dp *intel_dp)
664{
46bd8383 665 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
73c0fcac 666 int backlight_controller = dev_priv->vbt.backlight.controller;
78597996
ID
667
668 lockdep_assert_held(&dev_priv->pps_mutex);
669
670 /* We should never land here with regular DP ports */
1853a9da 671 WARN_ON(!intel_dp_is_edp(intel_dp));
78597996 672
78597996 673 if (!intel_dp->pps_reset)
73c0fcac 674 return backlight_controller;
78597996
ID
675
676 intel_dp->pps_reset = false;
677
678 /*
679 * Only the HW needs to be reprogrammed, the SW state is fixed and
680 * has been setup during connector init.
681 */
46bd8383 682 intel_dp_init_panel_power_sequencer_registers(intel_dp, false);
78597996 683
73c0fcac 684 return backlight_controller;
78597996
ID
685}
686
6491ab27
VS
687typedef bool (*vlv_pipe_check)(struct drm_i915_private *dev_priv,
688 enum pipe pipe);
689
690static bool vlv_pipe_has_pp_on(struct drm_i915_private *dev_priv,
691 enum pipe pipe)
692{
44cb734c 693 return I915_READ(PP_STATUS(pipe)) & PP_ON;
6491ab27
VS
694}
695
696static bool vlv_pipe_has_vdd_on(struct drm_i915_private *dev_priv,
697 enum pipe pipe)
698{
44cb734c 699 return I915_READ(PP_CONTROL(pipe)) & EDP_FORCE_VDD;
6491ab27
VS
700}
701
702static bool vlv_pipe_any(struct drm_i915_private *dev_priv,
703 enum pipe pipe)
704{
705 return true;
706}
bf13e81b 707
a4a5d2f8 708static enum pipe
6491ab27
VS
709vlv_initial_pps_pipe(struct drm_i915_private *dev_priv,
710 enum port port,
711 vlv_pipe_check pipe_check)
a4a5d2f8
VS
712{
713 enum pipe pipe;
bf13e81b 714
bf13e81b 715 for (pipe = PIPE_A; pipe <= PIPE_B; pipe++) {
44cb734c 716 u32 port_sel = I915_READ(PP_ON_DELAYS(pipe)) &
bf13e81b 717 PANEL_PORT_SELECT_MASK;
a4a5d2f8
VS
718
719 if (port_sel != PANEL_PORT_SELECT_VLV(port))
720 continue;
721
6491ab27
VS
722 if (!pipe_check(dev_priv, pipe))
723 continue;
724
a4a5d2f8 725 return pipe;
bf13e81b
JN
726 }
727
a4a5d2f8
VS
728 return INVALID_PIPE;
729}
730
731static void
732vlv_initial_power_sequencer_setup(struct intel_dp *intel_dp)
733{
46bd8383 734 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
a4a5d2f8 735 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
8f4f2797 736 enum port port = intel_dig_port->base.port;
a4a5d2f8
VS
737
738 lockdep_assert_held(&dev_priv->pps_mutex);
739
740 /* try to find a pipe with this port selected */
6491ab27
VS
741 /* first pick one where the panel is on */
742 intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
743 vlv_pipe_has_pp_on);
744 /* didn't find one? pick one where vdd is on */
745 if (intel_dp->pps_pipe == INVALID_PIPE)
746 intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
747 vlv_pipe_has_vdd_on);
748 /* didn't find one? pick one with just the correct port */
749 if (intel_dp->pps_pipe == INVALID_PIPE)
750 intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
751 vlv_pipe_any);
a4a5d2f8
VS
752
753 /* didn't find one? just let vlv_power_sequencer_pipe() pick one when needed */
754 if (intel_dp->pps_pipe == INVALID_PIPE) {
755 DRM_DEBUG_KMS("no initial power sequencer for port %c\n",
756 port_name(port));
757 return;
bf13e81b
JN
758 }
759
a4a5d2f8
VS
760 DRM_DEBUG_KMS("initial power sequencer for port %c: pipe %c\n",
761 port_name(port), pipe_name(intel_dp->pps_pipe));
762
46bd8383
VS
763 intel_dp_init_panel_power_sequencer(intel_dp);
764 intel_dp_init_panel_power_sequencer_registers(intel_dp, false);
bf13e81b
JN
765}
766
78597996 767void intel_power_sequencer_reset(struct drm_i915_private *dev_priv)
773538e8 768{
773538e8
VS
769 struct intel_encoder *encoder;
770
920a14b2 771 if (WARN_ON(!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
cc3f90f0 772 !IS_GEN9_LP(dev_priv)))
773538e8
VS
773 return;
774
775 /*
776 * We can't grab pps_mutex here due to deadlock with power_domain
777 * mutex when power_domain functions are called while holding pps_mutex.
778 * That also means that in order to use pps_pipe the code needs to
779 * hold both a power domain reference and pps_mutex, and the power domain
780 * reference get/put must be done while _not_ holding pps_mutex.
781 * pps_{lock,unlock}() do these steps in the correct order, so one
782 * should use them always.
783 */
784
2f773477 785 for_each_intel_encoder(&dev_priv->drm, encoder) {
773538e8
VS
786 struct intel_dp *intel_dp;
787
9f2bdb00 788 if (encoder->type != INTEL_OUTPUT_DP &&
7e732cac
VS
789 encoder->type != INTEL_OUTPUT_EDP &&
790 encoder->type != INTEL_OUTPUT_DDI)
773538e8
VS
791 continue;
792
793 intel_dp = enc_to_intel_dp(&encoder->base);
9f2bdb00 794
7e732cac
VS
795 /* Skip pure DVI/HDMI DDI encoders */
796 if (!i915_mmio_reg_valid(intel_dp->output_reg))
797 continue;
798
9f2bdb00
VS
799 WARN_ON(intel_dp->active_pipe != INVALID_PIPE);
800
801 if (encoder->type != INTEL_OUTPUT_EDP)
802 continue;
803
cc3f90f0 804 if (IS_GEN9_LP(dev_priv))
78597996
ID
805 intel_dp->pps_reset = true;
806 else
807 intel_dp->pps_pipe = INVALID_PIPE;
773538e8 808 }
bf13e81b
JN
809}
810
8e8232d5
ID
811struct pps_registers {
812 i915_reg_t pp_ctrl;
813 i915_reg_t pp_stat;
814 i915_reg_t pp_on;
815 i915_reg_t pp_off;
816 i915_reg_t pp_div;
817};
818
46bd8383 819static void intel_pps_get_registers(struct intel_dp *intel_dp,
8e8232d5
ID
820 struct pps_registers *regs)
821{
46bd8383 822 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
44cb734c
ID
823 int pps_idx = 0;
824
8e8232d5
ID
825 memset(regs, 0, sizeof(*regs));
826
cc3f90f0 827 if (IS_GEN9_LP(dev_priv))
44cb734c
ID
828 pps_idx = bxt_power_sequencer_idx(intel_dp);
829 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
830 pps_idx = vlv_power_sequencer_pipe(intel_dp);
8e8232d5 831
44cb734c
ID
832 regs->pp_ctrl = PP_CONTROL(pps_idx);
833 regs->pp_stat = PP_STATUS(pps_idx);
834 regs->pp_on = PP_ON_DELAYS(pps_idx);
835 regs->pp_off = PP_OFF_DELAYS(pps_idx);
b0d6a0f2
AS
836 if (!IS_GEN9_LP(dev_priv) && !HAS_PCH_CNP(dev_priv) &&
837 !HAS_PCH_ICP(dev_priv))
44cb734c 838 regs->pp_div = PP_DIVISOR(pps_idx);
8e8232d5
ID
839}
840
f0f59a00
VS
841static i915_reg_t
842_pp_ctrl_reg(struct intel_dp *intel_dp)
bf13e81b 843{
8e8232d5 844 struct pps_registers regs;
bf13e81b 845
46bd8383 846 intel_pps_get_registers(intel_dp, &regs);
8e8232d5
ID
847
848 return regs.pp_ctrl;
bf13e81b
JN
849}
850
f0f59a00
VS
851static i915_reg_t
852_pp_stat_reg(struct intel_dp *intel_dp)
bf13e81b 853{
8e8232d5 854 struct pps_registers regs;
bf13e81b 855
46bd8383 856 intel_pps_get_registers(intel_dp, &regs);
8e8232d5
ID
857
858 return regs.pp_stat;
bf13e81b
JN
859}
860
01527b31
CT
861/* Reboot notifier handler to shutdown panel power to guarantee T12 timing
862 This function only applicable when panel PM state is not to be tracked */
863static int edp_notify_handler(struct notifier_block *this, unsigned long code,
864 void *unused)
865{
866 struct intel_dp *intel_dp = container_of(this, typeof(* intel_dp),
867 edp_notifier);
2f773477 868 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
01527b31 869
1853a9da 870 if (!intel_dp_is_edp(intel_dp) || code != SYS_RESTART)
01527b31
CT
871 return 0;
872
773538e8 873 pps_lock(intel_dp);
e39b999a 874
920a14b2 875 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
e39b999a 876 enum pipe pipe = vlv_power_sequencer_pipe(intel_dp);
f0f59a00 877 i915_reg_t pp_ctrl_reg, pp_div_reg;
649636ef 878 u32 pp_div;
e39b999a 879
44cb734c
ID
880 pp_ctrl_reg = PP_CONTROL(pipe);
881 pp_div_reg = PP_DIVISOR(pipe);
01527b31
CT
882 pp_div = I915_READ(pp_div_reg);
883 pp_div &= PP_REFERENCE_DIVIDER_MASK;
884
885 /* 0x1F write to PP_DIV_REG sets max cycle delay */
886 I915_WRITE(pp_div_reg, pp_div | 0x1F);
887 I915_WRITE(pp_ctrl_reg, PANEL_UNLOCK_REGS | PANEL_POWER_OFF);
888 msleep(intel_dp->panel_power_cycle_delay);
889 }
890
773538e8 891 pps_unlock(intel_dp);
e39b999a 892
01527b31
CT
893 return 0;
894}
895
4be73780 896static bool edp_have_panel_power(struct intel_dp *intel_dp)
ebf33b18 897{
2f773477 898 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
ebf33b18 899
e39b999a
VS
900 lockdep_assert_held(&dev_priv->pps_mutex);
901
920a14b2 902 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
9a42356b
VS
903 intel_dp->pps_pipe == INVALID_PIPE)
904 return false;
905
bf13e81b 906 return (I915_READ(_pp_stat_reg(intel_dp)) & PP_ON) != 0;
ebf33b18
KP
907}
908
4be73780 909static bool edp_have_panel_vdd(struct intel_dp *intel_dp)
ebf33b18 910{
2f773477 911 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
ebf33b18 912
e39b999a
VS
913 lockdep_assert_held(&dev_priv->pps_mutex);
914
920a14b2 915 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
9a42356b
VS
916 intel_dp->pps_pipe == INVALID_PIPE)
917 return false;
918
773538e8 919 return I915_READ(_pp_ctrl_reg(intel_dp)) & EDP_FORCE_VDD;
ebf33b18
KP
920}
921
9b984dae
KP
922static void
923intel_dp_check_edp(struct intel_dp *intel_dp)
924{
2f773477 925 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
ebf33b18 926
1853a9da 927 if (!intel_dp_is_edp(intel_dp))
9b984dae 928 return;
453c5420 929
4be73780 930 if (!edp_have_panel_power(intel_dp) && !edp_have_panel_vdd(intel_dp)) {
9b984dae
KP
931 WARN(1, "eDP powered off while attempting aux channel communication.\n");
932 DRM_DEBUG_KMS("Status 0x%08x Control 0x%08x\n",
bf13e81b
JN
933 I915_READ(_pp_stat_reg(intel_dp)),
934 I915_READ(_pp_ctrl_reg(intel_dp)));
9b984dae
KP
935 }
936}
937
9ee32fea
DV
938static uint32_t
939intel_dp_aux_wait_done(struct intel_dp *intel_dp, bool has_aux_irq)
940{
2f773477 941 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
4904fa66 942 i915_reg_t ch_ctl = intel_dp->aux_ch_ctl_reg(intel_dp);
9ee32fea
DV
943 uint32_t status;
944 bool done;
945
ef04f00d 946#define C (((status = I915_READ_NOTRACE(ch_ctl)) & DP_AUX_CH_CTL_SEND_BUSY) == 0)
9ee32fea 947 if (has_aux_irq)
b18ac466 948 done = wait_event_timeout(dev_priv->gmbus_wait_queue, C,
3598706b 949 msecs_to_jiffies_timeout(10));
9ee32fea 950 else
713a6b66 951 done = wait_for(C, 10) == 0;
9ee32fea
DV
952 if (!done)
953 DRM_ERROR("dp aux hw did not signal timeout (has irq: %i)!\n",
954 has_aux_irq);
955#undef C
956
957 return status;
958}
959
6ffb1be7 960static uint32_t g4x_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
a4fc5ed6 961{
449059a9 962 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
9ee32fea 963
a457f54b
VS
964 if (index)
965 return 0;
966
ec5b01dd
DL
967 /*
968 * The clock divider is based off the hrawclk, and would like to run at
a457f54b 969 * 2MHz. So, take the hrawclk value and divide by 2000 and use that
a4fc5ed6 970 */
a457f54b 971 return DIV_ROUND_CLOSEST(dev_priv->rawclk_freq, 2000);
ec5b01dd
DL
972}
973
974static uint32_t ilk_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
975{
449059a9 976 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
ec5b01dd
DL
977
978 if (index)
979 return 0;
980
a457f54b
VS
981 /*
982 * The clock divider is based off the cdclk or PCH rawclk, and would
983 * like to run at 2MHz. So, take the cdclk or PCH rawclk value and
984 * divide by 2000 and use that
985 */
449059a9 986 if (intel_dp->aux_ch == AUX_CH_A)
49cd97a3 987 return DIV_ROUND_CLOSEST(dev_priv->cdclk.hw.cdclk, 2000);
e7dc33f3
VS
988 else
989 return DIV_ROUND_CLOSEST(dev_priv->rawclk_freq, 2000);
ec5b01dd
DL
990}
991
992static uint32_t hsw_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
993{
449059a9 994 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
ec5b01dd 995
449059a9 996 if (intel_dp->aux_ch != AUX_CH_A && HAS_PCH_LPT_H(dev_priv)) {
2c55c336 997 /* Workaround for non-ULT HSW */
bc86625a
CW
998 switch (index) {
999 case 0: return 63;
1000 case 1: return 72;
1001 default: return 0;
1002 }
2c55c336 1003 }
a457f54b
VS
1004
1005 return ilk_get_aux_clock_divider(intel_dp, index);
b84a1cf8
RV
1006}
1007
b6b5e383
DL
1008static uint32_t skl_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
1009{
1010 /*
1011 * SKL doesn't need us to program the AUX clock divider (Hardware will
1012 * derive the clock from CDCLK automatically). We still implement the
1013 * get_aux_clock_divider vfunc to plug-in into the existing code.
1014 */
1015 return index ? 0 : 1;
1016}
1017
6ffb1be7
VS
1018static uint32_t g4x_get_aux_send_ctl(struct intel_dp *intel_dp,
1019 bool has_aux_irq,
1020 int send_bytes,
1021 uint32_t aux_clock_divider)
5ed12a19
DL
1022{
1023 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
8652744b
TU
1024 struct drm_i915_private *dev_priv =
1025 to_i915(intel_dig_port->base.base.dev);
5ed12a19
DL
1026 uint32_t precharge, timeout;
1027
8652744b 1028 if (IS_GEN6(dev_priv))
5ed12a19
DL
1029 precharge = 3;
1030 else
1031 precharge = 5;
1032
8f5f63d5 1033 if (IS_BROADWELL(dev_priv))
5ed12a19
DL
1034 timeout = DP_AUX_CH_CTL_TIME_OUT_600us;
1035 else
1036 timeout = DP_AUX_CH_CTL_TIME_OUT_400us;
1037
1038 return DP_AUX_CH_CTL_SEND_BUSY |
788d4433 1039 DP_AUX_CH_CTL_DONE |
5ed12a19 1040 (has_aux_irq ? DP_AUX_CH_CTL_INTERRUPT : 0) |
788d4433 1041 DP_AUX_CH_CTL_TIME_OUT_ERROR |
5ed12a19 1042 timeout |
788d4433 1043 DP_AUX_CH_CTL_RECEIVE_ERROR |
5ed12a19
DL
1044 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
1045 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
788d4433 1046 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT);
5ed12a19
DL
1047}
1048
b9ca5fad
DL
1049static uint32_t skl_get_aux_send_ctl(struct intel_dp *intel_dp,
1050 bool has_aux_irq,
1051 int send_bytes,
1052 uint32_t unused)
1053{
1054 return DP_AUX_CH_CTL_SEND_BUSY |
1055 DP_AUX_CH_CTL_DONE |
1056 (has_aux_irq ? DP_AUX_CH_CTL_INTERRUPT : 0) |
1057 DP_AUX_CH_CTL_TIME_OUT_ERROR |
6fa228ba 1058 DP_AUX_CH_CTL_TIME_OUT_MAX |
b9ca5fad
DL
1059 DP_AUX_CH_CTL_RECEIVE_ERROR |
1060 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
d4dcbdce 1061 DP_AUX_CH_CTL_FW_SYNC_PULSE_SKL(32) |
b9ca5fad
DL
1062 DP_AUX_CH_CTL_SYNC_PULSE_SKL(32);
1063}
1064
b84a1cf8 1065static int
f7606265
VS
1066intel_dp_aux_xfer(struct intel_dp *intel_dp,
1067 const uint8_t *send, int send_bytes,
8159c796
VS
1068 uint8_t *recv, int recv_size,
1069 u32 aux_send_ctl_flags)
b84a1cf8
RV
1070{
1071 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
0031fb96
TU
1072 struct drm_i915_private *dev_priv =
1073 to_i915(intel_dig_port->base.base.dev);
4904fa66 1074 i915_reg_t ch_ctl, ch_data[5];
bc86625a 1075 uint32_t aux_clock_divider;
b84a1cf8
RV
1076 int i, ret, recv_bytes;
1077 uint32_t status;
5ed12a19 1078 int try, clock = 0;
0031fb96 1079 bool has_aux_irq = HAS_AUX_IRQ(dev_priv);
884f19e9
JN
1080 bool vdd;
1081
4904fa66
VS
1082 ch_ctl = intel_dp->aux_ch_ctl_reg(intel_dp);
1083 for (i = 0; i < ARRAY_SIZE(ch_data); i++)
1084 ch_data[i] = intel_dp->aux_ch_data_reg(intel_dp, i);
1085
773538e8 1086 pps_lock(intel_dp);
e39b999a 1087
72c3500a
VS
1088 /*
1089 * We will be called with VDD already enabled for dpcd/edid/oui reads.
1090 * In such cases we want to leave VDD enabled and it's up to upper layers
1091 * to turn it off. But for eg. i2c-dev access we need to turn it on/off
1092 * ourselves.
1093 */
1e0560e0 1094 vdd = edp_panel_vdd_on(intel_dp);
b84a1cf8
RV
1095
1096 /* dp aux is extremely sensitive to irq latency, hence request the
1097 * lowest possible wakeup latency and so prevent the cpu from going into
1098 * deep sleep states.
1099 */
1100 pm_qos_update_request(&dev_priv->pm_qos, 0);
1101
1102 intel_dp_check_edp(intel_dp);
5eb08b69 1103
11bee43e
JB
1104 /* Try to wait for any previous AUX channel activity */
1105 for (try = 0; try < 3; try++) {
ef04f00d 1106 status = I915_READ_NOTRACE(ch_ctl);
11bee43e
JB
1107 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
1108 break;
1109 msleep(1);
1110 }
1111
1112 if (try == 3) {
02196c77
MK
1113 static u32 last_status = -1;
1114 const u32 status = I915_READ(ch_ctl);
1115
1116 if (status != last_status) {
1117 WARN(1, "dp_aux_ch not started status 0x%08x\n",
1118 status);
1119 last_status = status;
1120 }
1121
9ee32fea
DV
1122 ret = -EBUSY;
1123 goto out;
4f7f7b7e
CW
1124 }
1125
46a5ae9f
PZ
1126 /* Only 5 data registers! */
1127 if (WARN_ON(send_bytes > 20 || recv_size > 20)) {
1128 ret = -E2BIG;
1129 goto out;
1130 }
1131
ec5b01dd 1132 while ((aux_clock_divider = intel_dp->get_aux_clock_divider(intel_dp, clock++))) {
8159c796
VS
1133 u32 send_ctl = intel_dp->get_aux_send_ctl(intel_dp,
1134 has_aux_irq,
1135 send_bytes,
1136 aux_clock_divider);
1137
1138 send_ctl |= aux_send_ctl_flags;
5ed12a19 1139
bc86625a
CW
1140 /* Must try at least 3 times according to DP spec */
1141 for (try = 0; try < 5; try++) {
1142 /* Load the send data into the aux channel data registers */
1143 for (i = 0; i < send_bytes; i += 4)
4904fa66 1144 I915_WRITE(ch_data[i >> 2],
a4f1289e
RV
1145 intel_dp_pack_aux(send + i,
1146 send_bytes - i));
bc86625a
CW
1147
1148 /* Send the command and wait for it to complete */
5ed12a19 1149 I915_WRITE(ch_ctl, send_ctl);
bc86625a
CW
1150
1151 status = intel_dp_aux_wait_done(intel_dp, has_aux_irq);
1152
1153 /* Clear done status and any errors */
1154 I915_WRITE(ch_ctl,
1155 status |
1156 DP_AUX_CH_CTL_DONE |
1157 DP_AUX_CH_CTL_TIME_OUT_ERROR |
1158 DP_AUX_CH_CTL_RECEIVE_ERROR);
1159
74ebf294
TP
1160 /* DP CTS 1.2 Core Rev 1.1, 4.2.1.1 & 4.2.1.2
1161 * 400us delay required for errors and timeouts
1162 * Timeout errors from the HW already meet this
1163 * requirement so skip to next iteration
1164 */
3975f0aa
DP
1165 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR)
1166 continue;
1167
74ebf294
TP
1168 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
1169 usleep_range(400, 500);
bc86625a 1170 continue;
74ebf294 1171 }
bc86625a 1172 if (status & DP_AUX_CH_CTL_DONE)
e058c945 1173 goto done;
bc86625a 1174 }
a4fc5ed6
KP
1175 }
1176
a4fc5ed6 1177 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
1ae8c0a5 1178 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
9ee32fea
DV
1179 ret = -EBUSY;
1180 goto out;
a4fc5ed6
KP
1181 }
1182
e058c945 1183done:
a4fc5ed6
KP
1184 /* Check for timeout or receive error.
1185 * Timeouts occur when the sink is not connected
1186 */
a5b3da54 1187 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
1ae8c0a5 1188 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
9ee32fea
DV
1189 ret = -EIO;
1190 goto out;
a5b3da54 1191 }
1ae8c0a5
KP
1192
1193 /* Timeouts occur when the device isn't connected, so they're
1194 * "normal" -- don't fill the kernel log with these */
a5b3da54 1195 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
a5570fe5 1196 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
9ee32fea
DV
1197 ret = -ETIMEDOUT;
1198 goto out;
a4fc5ed6
KP
1199 }
1200
1201 /* Unload any bytes sent back from the other side */
1202 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
1203 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
14e01889
RV
1204
1205 /*
1206 * By BSpec: "Message sizes of 0 or >20 are not allowed."
1207 * We have no idea of what happened so we return -EBUSY so
1208 * drm layer takes care for the necessary retries.
1209 */
1210 if (recv_bytes == 0 || recv_bytes > 20) {
1211 DRM_DEBUG_KMS("Forbidden recv_bytes = %d on aux transaction\n",
1212 recv_bytes);
14e01889
RV
1213 ret = -EBUSY;
1214 goto out;
1215 }
1216
a4fc5ed6
KP
1217 if (recv_bytes > recv_size)
1218 recv_bytes = recv_size;
0206e353 1219
4f7f7b7e 1220 for (i = 0; i < recv_bytes; i += 4)
4904fa66 1221 intel_dp_unpack_aux(I915_READ(ch_data[i >> 2]),
a4f1289e 1222 recv + i, recv_bytes - i);
a4fc5ed6 1223
9ee32fea
DV
1224 ret = recv_bytes;
1225out:
1226 pm_qos_update_request(&dev_priv->pm_qos, PM_QOS_DEFAULT_VALUE);
1227
884f19e9
JN
1228 if (vdd)
1229 edp_panel_vdd_off(intel_dp, false);
1230
773538e8 1231 pps_unlock(intel_dp);
e39b999a 1232
9ee32fea 1233 return ret;
a4fc5ed6
KP
1234}
1235
a6c8aff0
JN
1236#define BARE_ADDRESS_SIZE 3
1237#define HEADER_SIZE (BARE_ADDRESS_SIZE + 1)
32078b72
VS
1238
1239static void
1240intel_dp_aux_header(u8 txbuf[HEADER_SIZE],
1241 const struct drm_dp_aux_msg *msg)
1242{
1243 txbuf[0] = (msg->request << 4) | ((msg->address >> 16) & 0xf);
1244 txbuf[1] = (msg->address >> 8) & 0xff;
1245 txbuf[2] = msg->address & 0xff;
1246 txbuf[3] = msg->size - 1;
1247}
1248
9d1a1031
JN
1249static ssize_t
1250intel_dp_aux_transfer(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
a4fc5ed6 1251{
9d1a1031
JN
1252 struct intel_dp *intel_dp = container_of(aux, struct intel_dp, aux);
1253 uint8_t txbuf[20], rxbuf[20];
1254 size_t txsize, rxsize;
a4fc5ed6 1255 int ret;
a4fc5ed6 1256
32078b72 1257 intel_dp_aux_header(txbuf, msg);
46a5ae9f 1258
9d1a1031
JN
1259 switch (msg->request & ~DP_AUX_I2C_MOT) {
1260 case DP_AUX_NATIVE_WRITE:
1261 case DP_AUX_I2C_WRITE:
c1e74122 1262 case DP_AUX_I2C_WRITE_STATUS_UPDATE:
a6c8aff0 1263 txsize = msg->size ? HEADER_SIZE + msg->size : BARE_ADDRESS_SIZE;
a1ddefd8 1264 rxsize = 2; /* 0 or 1 data bytes */
f51a44b9 1265
9d1a1031
JN
1266 if (WARN_ON(txsize > 20))
1267 return -E2BIG;
a4fc5ed6 1268
dd788090
VS
1269 WARN_ON(!msg->buffer != !msg->size);
1270
d81a67cc
ID
1271 if (msg->buffer)
1272 memcpy(txbuf + HEADER_SIZE, msg->buffer, msg->size);
a4fc5ed6 1273
f7606265 1274 ret = intel_dp_aux_xfer(intel_dp, txbuf, txsize,
8159c796 1275 rxbuf, rxsize, 0);
9d1a1031
JN
1276 if (ret > 0) {
1277 msg->reply = rxbuf[0] >> 4;
a4fc5ed6 1278
a1ddefd8
JN
1279 if (ret > 1) {
1280 /* Number of bytes written in a short write. */
1281 ret = clamp_t(int, rxbuf[1], 0, msg->size);
1282 } else {
1283 /* Return payload size. */
1284 ret = msg->size;
1285 }
9d1a1031
JN
1286 }
1287 break;
46a5ae9f 1288
9d1a1031
JN
1289 case DP_AUX_NATIVE_READ:
1290 case DP_AUX_I2C_READ:
a6c8aff0 1291 txsize = msg->size ? HEADER_SIZE : BARE_ADDRESS_SIZE;
9d1a1031 1292 rxsize = msg->size + 1;
a4fc5ed6 1293
9d1a1031
JN
1294 if (WARN_ON(rxsize > 20))
1295 return -E2BIG;
a4fc5ed6 1296
f7606265 1297 ret = intel_dp_aux_xfer(intel_dp, txbuf, txsize,
8159c796 1298 rxbuf, rxsize, 0);
9d1a1031
JN
1299 if (ret > 0) {
1300 msg->reply = rxbuf[0] >> 4;
1301 /*
1302 * Assume happy day, and copy the data. The caller is
1303 * expected to check msg->reply before touching it.
1304 *
1305 * Return payload size.
1306 */
1307 ret--;
1308 memcpy(msg->buffer, rxbuf + 1, ret);
a4fc5ed6 1309 }
9d1a1031
JN
1310 break;
1311
1312 default:
1313 ret = -EINVAL;
1314 break;
a4fc5ed6 1315 }
f51a44b9 1316
9d1a1031 1317 return ret;
a4fc5ed6
KP
1318}
1319
bdabdb63 1320static enum aux_ch intel_aux_ch(struct intel_dp *intel_dp)
8f7ce038 1321{
bdabdb63
VS
1322 struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
1323 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1324 enum port port = encoder->port;
8f7ce038
VS
1325 const struct ddi_vbt_port_info *info =
1326 &dev_priv->vbt.ddi_port_info[port];
bdabdb63 1327 enum aux_ch aux_ch;
8f7ce038
VS
1328
1329 if (!info->alternate_aux_channel) {
bdabdb63
VS
1330 aux_ch = (enum aux_ch) port;
1331
8f7ce038 1332 DRM_DEBUG_KMS("using AUX %c for port %c (platform default)\n",
bdabdb63
VS
1333 aux_ch_name(aux_ch), port_name(port));
1334 return aux_ch;
8f7ce038
VS
1335 }
1336
1337 switch (info->alternate_aux_channel) {
1338 case DP_AUX_A:
bdabdb63 1339 aux_ch = AUX_CH_A;
8f7ce038
VS
1340 break;
1341 case DP_AUX_B:
bdabdb63 1342 aux_ch = AUX_CH_B;
8f7ce038
VS
1343 break;
1344 case DP_AUX_C:
bdabdb63 1345 aux_ch = AUX_CH_C;
8f7ce038
VS
1346 break;
1347 case DP_AUX_D:
bdabdb63 1348 aux_ch = AUX_CH_D;
8f7ce038 1349 break;
a324fcac 1350 case DP_AUX_F:
bdabdb63 1351 aux_ch = AUX_CH_F;
a324fcac 1352 break;
8f7ce038
VS
1353 default:
1354 MISSING_CASE(info->alternate_aux_channel);
bdabdb63 1355 aux_ch = AUX_CH_A;
8f7ce038
VS
1356 break;
1357 }
1358
1359 DRM_DEBUG_KMS("using AUX %c for port %c (VBT)\n",
bdabdb63 1360 aux_ch_name(aux_ch), port_name(port));
8f7ce038 1361
bdabdb63
VS
1362 return aux_ch;
1363}
1364
1365static enum intel_display_power_domain
1366intel_aux_power_domain(struct intel_dp *intel_dp)
1367{
1368 switch (intel_dp->aux_ch) {
1369 case AUX_CH_A:
1370 return POWER_DOMAIN_AUX_A;
1371 case AUX_CH_B:
1372 return POWER_DOMAIN_AUX_B;
1373 case AUX_CH_C:
1374 return POWER_DOMAIN_AUX_C;
1375 case AUX_CH_D:
1376 return POWER_DOMAIN_AUX_D;
1377 case AUX_CH_F:
1378 return POWER_DOMAIN_AUX_F;
1379 default:
1380 MISSING_CASE(intel_dp->aux_ch);
1381 return POWER_DOMAIN_AUX_A;
1382 }
8f7ce038
VS
1383}
1384
4904fa66 1385static i915_reg_t g4x_aux_ctl_reg(struct intel_dp *intel_dp)
da00bdcf 1386{
4904fa66
VS
1387 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
1388 enum aux_ch aux_ch = intel_dp->aux_ch;
1389
bdabdb63
VS
1390 switch (aux_ch) {
1391 case AUX_CH_B:
1392 case AUX_CH_C:
1393 case AUX_CH_D:
1394 return DP_AUX_CH_CTL(aux_ch);
da00bdcf 1395 default:
bdabdb63
VS
1396 MISSING_CASE(aux_ch);
1397 return DP_AUX_CH_CTL(AUX_CH_B);
da00bdcf
VS
1398 }
1399}
1400
4904fa66 1401static i915_reg_t g4x_aux_data_reg(struct intel_dp *intel_dp, int index)
330e20ec 1402{
4904fa66
VS
1403 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
1404 enum aux_ch aux_ch = intel_dp->aux_ch;
1405
bdabdb63
VS
1406 switch (aux_ch) {
1407 case AUX_CH_B:
1408 case AUX_CH_C:
1409 case AUX_CH_D:
1410 return DP_AUX_CH_DATA(aux_ch, index);
330e20ec 1411 default:
bdabdb63
VS
1412 MISSING_CASE(aux_ch);
1413 return DP_AUX_CH_DATA(AUX_CH_B, index);
330e20ec
VS
1414 }
1415}
1416
4904fa66 1417static i915_reg_t ilk_aux_ctl_reg(struct intel_dp *intel_dp)
bdabdb63 1418{
4904fa66
VS
1419 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
1420 enum aux_ch aux_ch = intel_dp->aux_ch;
1421
bdabdb63
VS
1422 switch (aux_ch) {
1423 case AUX_CH_A:
1424 return DP_AUX_CH_CTL(aux_ch);
1425 case AUX_CH_B:
1426 case AUX_CH_C:
1427 case AUX_CH_D:
1428 return PCH_DP_AUX_CH_CTL(aux_ch);
da00bdcf 1429 default:
bdabdb63
VS
1430 MISSING_CASE(aux_ch);
1431 return DP_AUX_CH_CTL(AUX_CH_A);
da00bdcf
VS
1432 }
1433}
1434
4904fa66 1435static i915_reg_t ilk_aux_data_reg(struct intel_dp *intel_dp, int index)
bdabdb63 1436{
4904fa66
VS
1437 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
1438 enum aux_ch aux_ch = intel_dp->aux_ch;
1439
bdabdb63
VS
1440 switch (aux_ch) {
1441 case AUX_CH_A:
1442 return DP_AUX_CH_DATA(aux_ch, index);
1443 case AUX_CH_B:
1444 case AUX_CH_C:
1445 case AUX_CH_D:
1446 return PCH_DP_AUX_CH_DATA(aux_ch, index);
330e20ec 1447 default:
bdabdb63
VS
1448 MISSING_CASE(aux_ch);
1449 return DP_AUX_CH_DATA(AUX_CH_A, index);
330e20ec
VS
1450 }
1451}
1452
4904fa66 1453static i915_reg_t skl_aux_ctl_reg(struct intel_dp *intel_dp)
bdabdb63 1454{
4904fa66
VS
1455 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
1456 enum aux_ch aux_ch = intel_dp->aux_ch;
1457
bdabdb63
VS
1458 switch (aux_ch) {
1459 case AUX_CH_A:
1460 case AUX_CH_B:
1461 case AUX_CH_C:
1462 case AUX_CH_D:
1463 case AUX_CH_F:
1464 return DP_AUX_CH_CTL(aux_ch);
da00bdcf 1465 default:
bdabdb63
VS
1466 MISSING_CASE(aux_ch);
1467 return DP_AUX_CH_CTL(AUX_CH_A);
da00bdcf
VS
1468 }
1469}
1470
4904fa66 1471static i915_reg_t skl_aux_data_reg(struct intel_dp *intel_dp, int index)
bdabdb63 1472{
4904fa66
VS
1473 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
1474 enum aux_ch aux_ch = intel_dp->aux_ch;
1475
bdabdb63
VS
1476 switch (aux_ch) {
1477 case AUX_CH_A:
1478 case AUX_CH_B:
1479 case AUX_CH_C:
1480 case AUX_CH_D:
1481 case AUX_CH_F:
1482 return DP_AUX_CH_DATA(aux_ch, index);
330e20ec 1483 default:
bdabdb63
VS
1484 MISSING_CASE(aux_ch);
1485 return DP_AUX_CH_DATA(AUX_CH_A, index);
330e20ec
VS
1486 }
1487}
1488
91e939ae
VS
1489static void
1490intel_dp_aux_fini(struct intel_dp *intel_dp)
1491{
1492 kfree(intel_dp->aux.name);
1493}
1494
1495static void
1496intel_dp_aux_init(struct intel_dp *intel_dp)
330e20ec
VS
1497{
1498 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
91e939ae
VS
1499 struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
1500
1501 intel_dp->aux_ch = intel_aux_ch(intel_dp);
1502 intel_dp->aux_power_domain = intel_aux_power_domain(intel_dp);
330e20ec 1503
4904fa66
VS
1504 if (INTEL_GEN(dev_priv) >= 9) {
1505 intel_dp->aux_ch_ctl_reg = skl_aux_ctl_reg;
1506 intel_dp->aux_ch_data_reg = skl_aux_data_reg;
1507 } else if (HAS_PCH_SPLIT(dev_priv)) {
1508 intel_dp->aux_ch_ctl_reg = ilk_aux_ctl_reg;
1509 intel_dp->aux_ch_data_reg = ilk_aux_data_reg;
1510 } else {
1511 intel_dp->aux_ch_ctl_reg = g4x_aux_ctl_reg;
1512 intel_dp->aux_ch_data_reg = g4x_aux_data_reg;
1513 }
330e20ec 1514
91e939ae
VS
1515 if (INTEL_GEN(dev_priv) >= 9)
1516 intel_dp->get_aux_clock_divider = skl_get_aux_clock_divider;
1517 else if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
1518 intel_dp->get_aux_clock_divider = hsw_get_aux_clock_divider;
1519 else if (HAS_PCH_SPLIT(dev_priv))
1520 intel_dp->get_aux_clock_divider = ilk_get_aux_clock_divider;
1521 else
1522 intel_dp->get_aux_clock_divider = g4x_get_aux_clock_divider;
bdabdb63 1523
91e939ae
VS
1524 if (INTEL_GEN(dev_priv) >= 9)
1525 intel_dp->get_aux_send_ctl = skl_get_aux_send_ctl;
1526 else
1527 intel_dp->get_aux_send_ctl = g4x_get_aux_send_ctl;
ab2c0672 1528
7a418e34 1529 drm_dp_aux_init(&intel_dp->aux);
8316f337 1530
7a418e34 1531 /* Failure to allocate our preferred name is not critical */
bdabdb63
VS
1532 intel_dp->aux.name = kasprintf(GFP_KERNEL, "DPDDC-%c",
1533 port_name(encoder->port));
9d1a1031 1534 intel_dp->aux.transfer = intel_dp_aux_transfer;
a4fc5ed6
KP
1535}
1536
e588fa18 1537bool intel_dp_source_supports_hbr2(struct intel_dp *intel_dp)
ed63baaf 1538{
fc603ca7 1539 int max_rate = intel_dp->source_rates[intel_dp->num_source_rates - 1];
e588fa18 1540
fc603ca7 1541 return max_rate >= 540000;
ed63baaf
TS
1542}
1543
c6bb3538
DV
1544static void
1545intel_dp_set_clock(struct intel_encoder *encoder,
840b32b7 1546 struct intel_crtc_state *pipe_config)
c6bb3538 1547{
2f773477 1548 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
9dd4ffdf
CML
1549 const struct dp_link_dpll *divisor = NULL;
1550 int i, count = 0;
c6bb3538 1551
9beb5fea 1552 if (IS_G4X(dev_priv)) {
9dd4ffdf
CML
1553 divisor = gen4_dpll;
1554 count = ARRAY_SIZE(gen4_dpll);
6e266956 1555 } else if (HAS_PCH_SPLIT(dev_priv)) {
9dd4ffdf
CML
1556 divisor = pch_dpll;
1557 count = ARRAY_SIZE(pch_dpll);
920a14b2 1558 } else if (IS_CHERRYVIEW(dev_priv)) {
ef9348c8
CML
1559 divisor = chv_dpll;
1560 count = ARRAY_SIZE(chv_dpll);
11a914c2 1561 } else if (IS_VALLEYVIEW(dev_priv)) {
65ce4bf5
CML
1562 divisor = vlv_dpll;
1563 count = ARRAY_SIZE(vlv_dpll);
c6bb3538 1564 }
9dd4ffdf
CML
1565
1566 if (divisor && count) {
1567 for (i = 0; i < count; i++) {
840b32b7 1568 if (pipe_config->port_clock == divisor[i].clock) {
9dd4ffdf
CML
1569 pipe_config->dpll = divisor[i].dpll;
1570 pipe_config->clock_set = true;
1571 break;
1572 }
1573 }
c6bb3538
DV
1574 }
1575}
1576
0336400e
VS
1577static void snprintf_int_array(char *str, size_t len,
1578 const int *array, int nelem)
1579{
1580 int i;
1581
1582 str[0] = '\0';
1583
1584 for (i = 0; i < nelem; i++) {
b2f505be 1585 int r = snprintf(str, len, "%s%d", i ? ", " : "", array[i]);
0336400e
VS
1586 if (r >= len)
1587 return;
1588 str += r;
1589 len -= r;
1590 }
1591}
1592
1593static void intel_dp_print_rates(struct intel_dp *intel_dp)
1594{
0336400e
VS
1595 char str[128]; /* FIXME: too big for stack? */
1596
1597 if ((drm_debug & DRM_UT_KMS) == 0)
1598 return;
1599
55cfc580
JN
1600 snprintf_int_array(str, sizeof(str),
1601 intel_dp->source_rates, intel_dp->num_source_rates);
0336400e
VS
1602 DRM_DEBUG_KMS("source rates: %s\n", str);
1603
68f357cb
JN
1604 snprintf_int_array(str, sizeof(str),
1605 intel_dp->sink_rates, intel_dp->num_sink_rates);
0336400e
VS
1606 DRM_DEBUG_KMS("sink rates: %s\n", str);
1607
975ee5fc
JN
1608 snprintf_int_array(str, sizeof(str),
1609 intel_dp->common_rates, intel_dp->num_common_rates);
94ca719e 1610 DRM_DEBUG_KMS("common rates: %s\n", str);
0336400e
VS
1611}
1612
50fec21a
VS
1613int
1614intel_dp_max_link_rate(struct intel_dp *intel_dp)
1615{
50fec21a
VS
1616 int len;
1617
e6c0c64a 1618 len = intel_dp_common_len_rate_limit(intel_dp, intel_dp->max_link_rate);
50fec21a
VS
1619 if (WARN_ON(len <= 0))
1620 return 162000;
1621
975ee5fc 1622 return intel_dp->common_rates[len - 1];
50fec21a
VS
1623}
1624
ed4e9c1d
VS
1625int intel_dp_rate_select(struct intel_dp *intel_dp, int rate)
1626{
8001b754
JN
1627 int i = intel_dp_rate_index(intel_dp->sink_rates,
1628 intel_dp->num_sink_rates, rate);
b5c72b20
JN
1629
1630 if (WARN_ON(i < 0))
1631 i = 0;
1632
1633 return i;
ed4e9c1d
VS
1634}
1635
94223d04
ACO
1636void intel_dp_compute_rate(struct intel_dp *intel_dp, int port_clock,
1637 uint8_t *link_bw, uint8_t *rate_select)
04a60f9f 1638{
68f357cb
JN
1639 /* eDP 1.4 rate select method. */
1640 if (intel_dp->use_rate_select) {
04a60f9f
VS
1641 *link_bw = 0;
1642 *rate_select =
1643 intel_dp_rate_select(intel_dp, port_clock);
1644 } else {
1645 *link_bw = drm_dp_link_rate_to_bw_code(port_clock);
1646 *rate_select = 0;
1647 }
1648}
1649
7c2781e4
JN
1650struct link_config_limits {
1651 int min_clock, max_clock;
1652 int min_lane_count, max_lane_count;
1653 int min_bpp, max_bpp;
1654};
1655
f580bea9
JN
1656static int intel_dp_compute_bpp(struct intel_dp *intel_dp,
1657 struct intel_crtc_state *pipe_config)
f9bb705e 1658{
ef32659a
JN
1659 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
1660 struct intel_connector *intel_connector = intel_dp->attached_connector;
f9bb705e
MK
1661 int bpp, bpc;
1662
1663 bpp = pipe_config->pipe_bpp;
1664 bpc = drm_dp_downstream_max_bpc(intel_dp->dpcd, intel_dp->downstream_ports);
1665
1666 if (bpc > 0)
1667 bpp = min(bpp, 3*bpc);
1668
ef32659a
JN
1669 if (intel_dp_is_edp(intel_dp)) {
1670 /* Get bpp from vbt only for panels that dont have bpp in edid */
1671 if (intel_connector->base.display_info.bpc == 0 &&
1672 dev_priv->vbt.edp.bpp && dev_priv->vbt.edp.bpp < bpp) {
1673 DRM_DEBUG_KMS("clamping bpp for eDP panel to BIOS-provided %i\n",
1674 dev_priv->vbt.edp.bpp);
1675 bpp = dev_priv->vbt.edp.bpp;
1676 }
1677 }
1678
f9bb705e
MK
1679 return bpp;
1680}
1681
dc911f5b
JB
1682static bool intel_edp_compare_alt_mode(struct drm_display_mode *m1,
1683 struct drm_display_mode *m2)
1684{
1685 bool bres = false;
1686
1687 if (m1 && m2)
1688 bres = (m1->hdisplay == m2->hdisplay &&
1689 m1->hsync_start == m2->hsync_start &&
1690 m1->hsync_end == m2->hsync_end &&
1691 m1->htotal == m2->htotal &&
1692 m1->vdisplay == m2->vdisplay &&
1693 m1->vsync_start == m2->vsync_start &&
1694 m1->vsync_end == m2->vsync_end &&
1695 m1->vtotal == m2->vtotal);
1696 return bres;
1697}
1698
a4971453
JN
1699/* Adjust link config limits based on compliance test requests. */
1700static void
1701intel_dp_adjust_compliance_config(struct intel_dp *intel_dp,
1702 struct intel_crtc_state *pipe_config,
1703 struct link_config_limits *limits)
1704{
1705 /* For DP Compliance we override the computed bpp for the pipe */
1706 if (intel_dp->compliance.test_data.bpc != 0) {
1707 int bpp = 3 * intel_dp->compliance.test_data.bpc;
1708
1709 limits->min_bpp = limits->max_bpp = bpp;
1710 pipe_config->dither_force_disable = bpp == 6 * 3;
1711
1712 DRM_DEBUG_KMS("Setting pipe_bpp to %d\n", bpp);
1713 }
1714
1715 /* Use values requested by Compliance Test Request */
1716 if (intel_dp->compliance.test_type == DP_TEST_LINK_TRAINING) {
1717 int index;
1718
1719 /* Validate the compliance test data since max values
1720 * might have changed due to link train fallback.
1721 */
1722 if (intel_dp_link_params_valid(intel_dp, intel_dp->compliance.test_link_rate,
1723 intel_dp->compliance.test_lane_count)) {
1724 index = intel_dp_rate_index(intel_dp->common_rates,
1725 intel_dp->num_common_rates,
1726 intel_dp->compliance.test_link_rate);
1727 if (index >= 0)
1728 limits->min_clock = limits->max_clock = index;
1729 limits->min_lane_count = limits->max_lane_count =
1730 intel_dp->compliance.test_lane_count;
1731 }
1732 }
1733}
1734
3acd115d
JN
1735/* Optimize link config in order: max bpp, min clock, min lanes */
1736static bool
1737intel_dp_compute_link_config_wide(struct intel_dp *intel_dp,
1738 struct intel_crtc_state *pipe_config,
1739 const struct link_config_limits *limits)
1740{
1741 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
1742 int bpp, clock, lane_count;
1743 int mode_rate, link_clock, link_avail;
1744
1745 for (bpp = limits->max_bpp; bpp >= limits->min_bpp; bpp -= 2 * 3) {
1746 mode_rate = intel_dp_link_required(adjusted_mode->crtc_clock,
1747 bpp);
1748
1749 for (clock = limits->min_clock; clock <= limits->max_clock; clock++) {
1750 for (lane_count = limits->min_lane_count;
1751 lane_count <= limits->max_lane_count;
1752 lane_count <<= 1) {
1753 link_clock = intel_dp->common_rates[clock];
1754 link_avail = intel_dp_max_data_rate(link_clock,
1755 lane_count);
1756
1757 if (mode_rate <= link_avail) {
1758 pipe_config->lane_count = lane_count;
1759 pipe_config->pipe_bpp = bpp;
1760 pipe_config->port_clock = link_clock;
1761
1762 return true;
1763 }
1764 }
1765 }
1766 }
1767
1768 return false;
1769}
1770
981a63eb
JN
1771static bool
1772intel_dp_compute_link_config(struct intel_encoder *encoder,
1773 struct intel_crtc_state *pipe_config)
a4fc5ed6 1774{
2d112de7 1775 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
5bfe2ac0 1776 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
7c2781e4 1777 struct link_config_limits limits;
94ca719e 1778 int common_len;
7c2781e4 1779
975ee5fc 1780 common_len = intel_dp_common_len_rate_limit(intel_dp,
e6c0c64a 1781 intel_dp->max_link_rate);
a8f3ef61
SJ
1782
1783 /* No common link rates between source and sink */
94ca719e 1784 WARN_ON(common_len <= 0);
a8f3ef61 1785
7c2781e4
JN
1786 limits.min_clock = 0;
1787 limits.max_clock = common_len - 1;
1788
1789 limits.min_lane_count = 1;
1790 limits.max_lane_count = intel_dp_max_lane_count(intel_dp);
1791
1792 limits.min_bpp = 6 * 3;
1793 limits.max_bpp = intel_dp_compute_bpp(intel_dp, pipe_config);
a4fc5ed6 1794
1853a9da 1795 if (intel_dp_is_edp(intel_dp)) {
344c5bbc
JN
1796 /*
1797 * Use the maximum clock and number of lanes the eDP panel
1798 * advertizes being capable of. The panels are generally
1799 * designed to support only a single clock and lane
1800 * configuration, and typically these values correspond to the
1801 * native resolution of the panel.
1802 */
7c2781e4
JN
1803 limits.min_lane_count = limits.max_lane_count;
1804 limits.min_clock = limits.max_clock;
7984211e 1805 }
657445fe 1806
a4971453
JN
1807 intel_dp_adjust_compliance_config(intel_dp, pipe_config, &limits);
1808
7c2781e4
JN
1809 DRM_DEBUG_KMS("DP link computation with max lane count %i "
1810 "max rate %d max bpp %d pixel clock %iKHz\n",
1811 limits.max_lane_count,
1812 intel_dp->common_rates[limits.max_clock],
1813 limits.max_bpp, adjusted_mode->crtc_clock);
1814
3acd115d
JN
1815 /*
1816 * Optimize for slow and wide. This is the place to add alternative
1817 * optimization policy.
1818 */
1819 if (!intel_dp_compute_link_config_wide(intel_dp, pipe_config, &limits))
1820 return false;
981a63eb
JN
1821
1822 DRM_DEBUG_KMS("DP lane count %d clock %d bpp %d\n",
3acd115d
JN
1823 pipe_config->lane_count, pipe_config->port_clock,
1824 pipe_config->pipe_bpp);
1825
1826 DRM_DEBUG_KMS("DP link rate required %i available %i\n",
1827 intel_dp_link_required(adjusted_mode->crtc_clock,
1828 pipe_config->pipe_bpp),
1829 intel_dp_max_data_rate(pipe_config->port_clock,
1830 pipe_config->lane_count));
981a63eb
JN
1831
1832 return true;
1833}
1834
1835bool
1836intel_dp_compute_config(struct intel_encoder *encoder,
1837 struct intel_crtc_state *pipe_config,
1838 struct drm_connector_state *conn_state)
1839{
1840 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1841 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
1842 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
1843 enum port port = encoder->port;
1844 struct intel_crtc *intel_crtc = to_intel_crtc(pipe_config->base.crtc);
1845 struct intel_connector *intel_connector = intel_dp->attached_connector;
1846 struct intel_digital_connector_state *intel_conn_state =
1847 to_intel_digital_connector_state(conn_state);
1848 bool reduce_m_n = drm_dp_has_quirk(&intel_dp->desc,
1849 DP_DPCD_QUIRK_LIMITED_M_N);
1850
1851 if (HAS_PCH_SPLIT(dev_priv) && !HAS_DDI(dev_priv) && port != PORT_A)
1852 pipe_config->has_pch_encoder = true;
1853
1854 pipe_config->has_drrs = false;
1855 if (IS_G4X(dev_priv) || port == PORT_A)
1856 pipe_config->has_audio = false;
1857 else if (intel_conn_state->force_audio == HDMI_AUDIO_AUTO)
1858 pipe_config->has_audio = intel_dp->has_audio;
1859 else
1860 pipe_config->has_audio = intel_conn_state->force_audio == HDMI_AUDIO_ON;
1861
1862 if (intel_dp_is_edp(intel_dp) && intel_connector->panel.fixed_mode) {
1863 struct drm_display_mode *panel_mode =
1864 intel_connector->panel.alt_fixed_mode;
1865 struct drm_display_mode *req_mode = &pipe_config->base.mode;
1866
1867 if (!intel_edp_compare_alt_mode(req_mode, panel_mode))
1868 panel_mode = intel_connector->panel.fixed_mode;
1869
1870 drm_mode_debug_printmodeline(panel_mode);
1871
1872 intel_fixed_panel_mode(panel_mode, adjusted_mode);
1873
1874 if (INTEL_GEN(dev_priv) >= 9) {
1875 int ret;
1876
1877 ret = skl_update_scaler_crtc(pipe_config);
1878 if (ret)
1879 return ret;
1880 }
1881
1882 if (HAS_GMCH_DISPLAY(dev_priv))
1883 intel_gmch_panel_fitting(intel_crtc, pipe_config,
1884 conn_state->scaling_mode);
1885 else
1886 intel_pch_panel_fitting(intel_crtc, pipe_config,
1887 conn_state->scaling_mode);
1888 }
1889
1890 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
1891 adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
1892 return false;
1893
1894 if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
1895 return false;
1896
1897 if (!intel_dp_compute_link_config(encoder, pipe_config))
1898 return false;
1899
8f647a01 1900 if (intel_conn_state->broadcast_rgb == INTEL_BROADCAST_RGB_AUTO) {
55bc60db
VS
1901 /*
1902 * See:
1903 * CEA-861-E - 5.1 Default Encoding Parameters
1904 * VESA DisplayPort Ver.1.2a - 5.1.1.1 Video Colorimetry
1905 */
0f2a2a75 1906 pipe_config->limited_color_range =
981a63eb 1907 pipe_config->pipe_bpp != 18 &&
c8127cf0
VS
1908 drm_default_rgb_quant_range(adjusted_mode) ==
1909 HDMI_QUANTIZATION_RANGE_LIMITED;
0f2a2a75
VS
1910 } else {
1911 pipe_config->limited_color_range =
8f647a01 1912 intel_conn_state->broadcast_rgb == INTEL_BROADCAST_RGB_LIMITED;
55bc60db
VS
1913 }
1914
981a63eb 1915 intel_link_compute_m_n(pipe_config->pipe_bpp, pipe_config->lane_count,
241bfc38
DL
1916 adjusted_mode->crtc_clock,
1917 pipe_config->port_clock,
b31e85ed
JN
1918 &pipe_config->dp_m_n,
1919 reduce_m_n);
9d1a455b 1920
439d7ac0 1921 if (intel_connector->panel.downclock_mode != NULL &&
96178eeb 1922 dev_priv->drrs.type == SEAMLESS_DRRS_SUPPORT) {
f769cd24 1923 pipe_config->has_drrs = true;
981a63eb
JN
1924 intel_link_compute_m_n(pipe_config->pipe_bpp,
1925 pipe_config->lane_count,
1926 intel_connector->panel.downclock_mode->clock,
1927 pipe_config->port_clock,
1928 &pipe_config->dp_m2_n2,
1929 reduce_m_n);
439d7ac0
PB
1930 }
1931
4f8036a2 1932 if (!HAS_DDI(dev_priv))
840b32b7 1933 intel_dp_set_clock(encoder, pipe_config);
c6bb3538 1934
4d90f2d5
VS
1935 intel_psr_compute_config(intel_dp, pipe_config);
1936
03afc4a2 1937 return true;
a4fc5ed6
KP
1938}
1939
901c2daf 1940void intel_dp_set_link_params(struct intel_dp *intel_dp,
dfa10480
ACO
1941 int link_rate, uint8_t lane_count,
1942 bool link_mst)
901c2daf 1943{
edb2e530 1944 intel_dp->link_trained = false;
dfa10480
ACO
1945 intel_dp->link_rate = link_rate;
1946 intel_dp->lane_count = lane_count;
1947 intel_dp->link_mst = link_mst;
901c2daf
VS
1948}
1949
85cb48a1 1950static void intel_dp_prepare(struct intel_encoder *encoder,
5f88a9c6 1951 const struct intel_crtc_state *pipe_config)
a4fc5ed6 1952{
2f773477 1953 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
b934223d 1954 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
8f4f2797 1955 enum port port = encoder->port;
adc10304 1956 struct intel_crtc *crtc = to_intel_crtc(pipe_config->base.crtc);
85cb48a1 1957 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
a4fc5ed6 1958
dfa10480
ACO
1959 intel_dp_set_link_params(intel_dp, pipe_config->port_clock,
1960 pipe_config->lane_count,
1961 intel_crtc_has_type(pipe_config,
1962 INTEL_OUTPUT_DP_MST));
901c2daf 1963
417e822d 1964 /*
1a2eb460 1965 * There are four kinds of DP registers:
417e822d
KP
1966 *
1967 * IBX PCH
1a2eb460
KP
1968 * SNB CPU
1969 * IVB CPU
417e822d
KP
1970 * CPT PCH
1971 *
1972 * IBX PCH and CPU are the same for almost everything,
1973 * except that the CPU DP PLL is configured in this
1974 * register
1975 *
1976 * CPT PCH is quite different, having many bits moved
1977 * to the TRANS_DP_CTL register instead. That
1978 * configuration happens (oddly) in ironlake_pch_enable
1979 */
9c9e7927 1980
417e822d
KP
1981 /* Preserve the BIOS-computed detected bit. This is
1982 * supposed to be read-only.
1983 */
1984 intel_dp->DP = I915_READ(intel_dp->output_reg) & DP_DETECTED;
a4fc5ed6 1985
417e822d 1986 /* Handle DP bits in common between all three register formats */
417e822d 1987 intel_dp->DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
85cb48a1 1988 intel_dp->DP |= DP_PORT_WIDTH(pipe_config->lane_count);
a4fc5ed6 1989
417e822d 1990 /* Split out the IBX/CPU vs CPT settings */
32f9d658 1991
b752e995 1992 if (IS_IVYBRIDGE(dev_priv) && port == PORT_A) {
1a2eb460
KP
1993 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
1994 intel_dp->DP |= DP_SYNC_HS_HIGH;
1995 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
1996 intel_dp->DP |= DP_SYNC_VS_HIGH;
1997 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
1998
6aba5b6c 1999 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
1a2eb460
KP
2000 intel_dp->DP |= DP_ENHANCED_FRAMING;
2001
7c62a164 2002 intel_dp->DP |= crtc->pipe << 29;
6e266956 2003 } else if (HAS_PCH_CPT(dev_priv) && port != PORT_A) {
e3ef4479
VS
2004 u32 trans_dp;
2005
39e5fa88 2006 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
e3ef4479
VS
2007
2008 trans_dp = I915_READ(TRANS_DP_CTL(crtc->pipe));
2009 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
2010 trans_dp |= TRANS_DP_ENH_FRAMING;
2011 else
2012 trans_dp &= ~TRANS_DP_ENH_FRAMING;
2013 I915_WRITE(TRANS_DP_CTL(crtc->pipe), trans_dp);
39e5fa88 2014 } else {
c99f53f7 2015 if (IS_G4X(dev_priv) && pipe_config->limited_color_range)
0f2a2a75 2016 intel_dp->DP |= DP_COLOR_RANGE_16_235;
417e822d
KP
2017
2018 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
2019 intel_dp->DP |= DP_SYNC_HS_HIGH;
2020 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
2021 intel_dp->DP |= DP_SYNC_VS_HIGH;
2022 intel_dp->DP |= DP_LINK_TRAIN_OFF;
2023
6aba5b6c 2024 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
417e822d
KP
2025 intel_dp->DP |= DP_ENHANCED_FRAMING;
2026
920a14b2 2027 if (IS_CHERRYVIEW(dev_priv))
44f37d1f 2028 intel_dp->DP |= DP_PIPE_SELECT_CHV(crtc->pipe);
39e5fa88
VS
2029 else if (crtc->pipe == PIPE_B)
2030 intel_dp->DP |= DP_PIPEB_SELECT;
32f9d658 2031 }
a4fc5ed6
KP
2032}
2033
ffd6749d
PZ
2034#define IDLE_ON_MASK (PP_ON | PP_SEQUENCE_MASK | 0 | PP_SEQUENCE_STATE_MASK)
2035#define IDLE_ON_VALUE (PP_ON | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_ON_IDLE)
99ea7127 2036
1a5ef5b7
PZ
2037#define IDLE_OFF_MASK (PP_ON | PP_SEQUENCE_MASK | 0 | 0)
2038#define IDLE_OFF_VALUE (0 | PP_SEQUENCE_NONE | 0 | 0)
99ea7127 2039
ffd6749d
PZ
2040#define IDLE_CYCLE_MASK (PP_ON | PP_SEQUENCE_MASK | PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK)
2041#define IDLE_CYCLE_VALUE (0 | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_OFF_IDLE)
99ea7127 2042
46bd8383 2043static void intel_pps_verify_state(struct intel_dp *intel_dp);
de9c1b6b 2044
4be73780 2045static void wait_panel_status(struct intel_dp *intel_dp,
99ea7127
KP
2046 u32 mask,
2047 u32 value)
bd943159 2048{
2f773477 2049 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
f0f59a00 2050 i915_reg_t pp_stat_reg, pp_ctrl_reg;
453c5420 2051
e39b999a
VS
2052 lockdep_assert_held(&dev_priv->pps_mutex);
2053
46bd8383 2054 intel_pps_verify_state(intel_dp);
de9c1b6b 2055
bf13e81b
JN
2056 pp_stat_reg = _pp_stat_reg(intel_dp);
2057 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
32ce697c 2058
99ea7127 2059 DRM_DEBUG_KMS("mask %08x value %08x status %08x control %08x\n",
453c5420
JB
2060 mask, value,
2061 I915_READ(pp_stat_reg),
2062 I915_READ(pp_ctrl_reg));
32ce697c 2063
9036ff06
CW
2064 if (intel_wait_for_register(dev_priv,
2065 pp_stat_reg, mask, value,
2066 5000))
99ea7127 2067 DRM_ERROR("Panel status timeout: status %08x control %08x\n",
453c5420
JB
2068 I915_READ(pp_stat_reg),
2069 I915_READ(pp_ctrl_reg));
54c136d4
CW
2070
2071 DRM_DEBUG_KMS("Wait complete\n");
99ea7127 2072}
32ce697c 2073
4be73780 2074static void wait_panel_on(struct intel_dp *intel_dp)
99ea7127
KP
2075{
2076 DRM_DEBUG_KMS("Wait for panel power on\n");
4be73780 2077 wait_panel_status(intel_dp, IDLE_ON_MASK, IDLE_ON_VALUE);
bd943159
KP
2078}
2079
4be73780 2080static void wait_panel_off(struct intel_dp *intel_dp)
99ea7127
KP
2081{
2082 DRM_DEBUG_KMS("Wait for panel power off time\n");
4be73780 2083 wait_panel_status(intel_dp, IDLE_OFF_MASK, IDLE_OFF_VALUE);
99ea7127
KP
2084}
2085
4be73780 2086static void wait_panel_power_cycle(struct intel_dp *intel_dp)
99ea7127 2087{
d28d4731
AK
2088 ktime_t panel_power_on_time;
2089 s64 panel_power_off_duration;
2090
99ea7127 2091 DRM_DEBUG_KMS("Wait for panel power cycle\n");
dce56b3c 2092
d28d4731
AK
2093 /* take the difference of currrent time and panel power off time
2094 * and then make panel wait for t11_t12 if needed. */
2095 panel_power_on_time = ktime_get_boottime();
2096 panel_power_off_duration = ktime_ms_delta(panel_power_on_time, intel_dp->panel_power_off_time);
2097
dce56b3c
PZ
2098 /* When we disable the VDD override bit last we have to do the manual
2099 * wait. */
d28d4731
AK
2100 if (panel_power_off_duration < (s64)intel_dp->panel_power_cycle_delay)
2101 wait_remaining_ms_from_jiffies(jiffies,
2102 intel_dp->panel_power_cycle_delay - panel_power_off_duration);
dce56b3c 2103
4be73780 2104 wait_panel_status(intel_dp, IDLE_CYCLE_MASK, IDLE_CYCLE_VALUE);
99ea7127
KP
2105}
2106
4be73780 2107static void wait_backlight_on(struct intel_dp *intel_dp)
dce56b3c
PZ
2108{
2109 wait_remaining_ms_from_jiffies(intel_dp->last_power_on,
2110 intel_dp->backlight_on_delay);
2111}
2112
4be73780 2113static void edp_wait_backlight_off(struct intel_dp *intel_dp)
dce56b3c
PZ
2114{
2115 wait_remaining_ms_from_jiffies(intel_dp->last_backlight_off,
2116 intel_dp->backlight_off_delay);
2117}
99ea7127 2118
832dd3c1
KP
2119/* Read the current pp_control value, unlocking the register if it
2120 * is locked
2121 */
2122
453c5420 2123static u32 ironlake_get_pp_control(struct intel_dp *intel_dp)
832dd3c1 2124{
2f773477 2125 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
453c5420 2126 u32 control;
832dd3c1 2127
e39b999a
VS
2128 lockdep_assert_held(&dev_priv->pps_mutex);
2129
bf13e81b 2130 control = I915_READ(_pp_ctrl_reg(intel_dp));
8090ba8c
ID
2131 if (WARN_ON(!HAS_DDI(dev_priv) &&
2132 (control & PANEL_UNLOCK_MASK) != PANEL_UNLOCK_REGS)) {
b0a08bec
VK
2133 control &= ~PANEL_UNLOCK_MASK;
2134 control |= PANEL_UNLOCK_REGS;
2135 }
832dd3c1 2136 return control;
bd943159
KP
2137}
2138
951468f3
VS
2139/*
2140 * Must be paired with edp_panel_vdd_off().
2141 * Must hold pps_mutex around the whole on/off sequence.
2142 * Can be nested with intel_edp_panel_vdd_{on,off}() calls.
2143 */
1e0560e0 2144static bool edp_panel_vdd_on(struct intel_dp *intel_dp)
5d613501 2145{
2f773477 2146 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
4e6e1a54 2147 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
5d613501 2148 u32 pp;
f0f59a00 2149 i915_reg_t pp_stat_reg, pp_ctrl_reg;
adddaaf4 2150 bool need_to_disable = !intel_dp->want_panel_vdd;
5d613501 2151
e39b999a
VS
2152 lockdep_assert_held(&dev_priv->pps_mutex);
2153
1853a9da 2154 if (!intel_dp_is_edp(intel_dp))
adddaaf4 2155 return false;
bd943159 2156
2c623c11 2157 cancel_delayed_work(&intel_dp->panel_vdd_work);
bd943159 2158 intel_dp->want_panel_vdd = true;
99ea7127 2159
4be73780 2160 if (edp_have_panel_vdd(intel_dp))
adddaaf4 2161 return need_to_disable;
b0665d57 2162
5432fcaf 2163 intel_display_power_get(dev_priv, intel_dp->aux_power_domain);
e9cb81a2 2164
3936fcf4 2165 DRM_DEBUG_KMS("Turning eDP port %c VDD on\n",
8f4f2797 2166 port_name(intel_dig_port->base.port));
bd943159 2167
4be73780
DV
2168 if (!edp_have_panel_power(intel_dp))
2169 wait_panel_power_cycle(intel_dp);
99ea7127 2170
453c5420 2171 pp = ironlake_get_pp_control(intel_dp);
5d613501 2172 pp |= EDP_FORCE_VDD;
ebf33b18 2173
bf13e81b
JN
2174 pp_stat_reg = _pp_stat_reg(intel_dp);
2175 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
453c5420
JB
2176
2177 I915_WRITE(pp_ctrl_reg, pp);
2178 POSTING_READ(pp_ctrl_reg);
2179 DRM_DEBUG_KMS("PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
2180 I915_READ(pp_stat_reg), I915_READ(pp_ctrl_reg));
ebf33b18
KP
2181 /*
2182 * If the panel wasn't on, delay before accessing aux channel
2183 */
4be73780 2184 if (!edp_have_panel_power(intel_dp)) {
3936fcf4 2185 DRM_DEBUG_KMS("eDP port %c panel power wasn't enabled\n",
8f4f2797 2186 port_name(intel_dig_port->base.port));
f01eca2e 2187 msleep(intel_dp->panel_power_up_delay);
f01eca2e 2188 }
adddaaf4
JN
2189
2190 return need_to_disable;
2191}
2192
951468f3
VS
2193/*
2194 * Must be paired with intel_edp_panel_vdd_off() or
2195 * intel_edp_panel_off().
2196 * Nested calls to these functions are not allowed since
2197 * we drop the lock. Caller must use some higher level
2198 * locking to prevent nested calls from other threads.
2199 */
b80d6c78 2200void intel_edp_panel_vdd_on(struct intel_dp *intel_dp)
adddaaf4 2201{
c695b6b6 2202 bool vdd;
adddaaf4 2203
1853a9da 2204 if (!intel_dp_is_edp(intel_dp))
c695b6b6
VS
2205 return;
2206
773538e8 2207 pps_lock(intel_dp);
c695b6b6 2208 vdd = edp_panel_vdd_on(intel_dp);
773538e8 2209 pps_unlock(intel_dp);
c695b6b6 2210
e2c719b7 2211 I915_STATE_WARN(!vdd, "eDP port %c VDD already requested on\n",
8f4f2797 2212 port_name(dp_to_dig_port(intel_dp)->base.port));
5d613501
JB
2213}
2214
4be73780 2215static void edp_panel_vdd_off_sync(struct intel_dp *intel_dp)
5d613501 2216{
2f773477 2217 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
be2c9196
VS
2218 struct intel_digital_port *intel_dig_port =
2219 dp_to_dig_port(intel_dp);
5d613501 2220 u32 pp;
f0f59a00 2221 i915_reg_t pp_stat_reg, pp_ctrl_reg;
5d613501 2222
e39b999a 2223 lockdep_assert_held(&dev_priv->pps_mutex);
a0e99e68 2224
15e899a0 2225 WARN_ON(intel_dp->want_panel_vdd);
4e6e1a54 2226
15e899a0 2227 if (!edp_have_panel_vdd(intel_dp))
be2c9196 2228 return;
b0665d57 2229
3936fcf4 2230 DRM_DEBUG_KMS("Turning eDP port %c VDD off\n",
8f4f2797 2231 port_name(intel_dig_port->base.port));
bd943159 2232
be2c9196
VS
2233 pp = ironlake_get_pp_control(intel_dp);
2234 pp &= ~EDP_FORCE_VDD;
453c5420 2235
be2c9196
VS
2236 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
2237 pp_stat_reg = _pp_stat_reg(intel_dp);
99ea7127 2238
be2c9196
VS
2239 I915_WRITE(pp_ctrl_reg, pp);
2240 POSTING_READ(pp_ctrl_reg);
90791a5c 2241
be2c9196
VS
2242 /* Make sure sequencer is idle before allowing subsequent activity */
2243 DRM_DEBUG_KMS("PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
2244 I915_READ(pp_stat_reg), I915_READ(pp_ctrl_reg));
e9cb81a2 2245
5a162e22 2246 if ((pp & PANEL_POWER_ON) == 0)
d28d4731 2247 intel_dp->panel_power_off_time = ktime_get_boottime();
e9cb81a2 2248
5432fcaf 2249 intel_display_power_put(dev_priv, intel_dp->aux_power_domain);
bd943159 2250}
5d613501 2251
4be73780 2252static void edp_panel_vdd_work(struct work_struct *__work)
bd943159
KP
2253{
2254 struct intel_dp *intel_dp = container_of(to_delayed_work(__work),
2255 struct intel_dp, panel_vdd_work);
bd943159 2256
773538e8 2257 pps_lock(intel_dp);
15e899a0
VS
2258 if (!intel_dp->want_panel_vdd)
2259 edp_panel_vdd_off_sync(intel_dp);
773538e8 2260 pps_unlock(intel_dp);
bd943159
KP
2261}
2262
aba86890
ID
2263static void edp_panel_vdd_schedule_off(struct intel_dp *intel_dp)
2264{
2265 unsigned long delay;
2266
2267 /*
2268 * Queue the timer to fire a long time from now (relative to the power
2269 * down delay) to keep the panel power up across a sequence of
2270 * operations.
2271 */
2272 delay = msecs_to_jiffies(intel_dp->panel_power_cycle_delay * 5);
2273 schedule_delayed_work(&intel_dp->panel_vdd_work, delay);
2274}
2275
951468f3
VS
2276/*
2277 * Must be paired with edp_panel_vdd_on().
2278 * Must hold pps_mutex around the whole on/off sequence.
2279 * Can be nested with intel_edp_panel_vdd_{on,off}() calls.
2280 */
4be73780 2281static void edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync)
bd943159 2282{
fac5e23e 2283 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
e39b999a
VS
2284
2285 lockdep_assert_held(&dev_priv->pps_mutex);
2286
1853a9da 2287 if (!intel_dp_is_edp(intel_dp))
97af61f5 2288 return;
5d613501 2289
e2c719b7 2290 I915_STATE_WARN(!intel_dp->want_panel_vdd, "eDP port %c VDD not forced on",
8f4f2797 2291 port_name(dp_to_dig_port(intel_dp)->base.port));
f2e8b18a 2292
bd943159
KP
2293 intel_dp->want_panel_vdd = false;
2294
aba86890 2295 if (sync)
4be73780 2296 edp_panel_vdd_off_sync(intel_dp);
aba86890
ID
2297 else
2298 edp_panel_vdd_schedule_off(intel_dp);
5d613501
JB
2299}
2300
9f0fb5be 2301static void edp_panel_on(struct intel_dp *intel_dp)
9934c132 2302{
2f773477 2303 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
99ea7127 2304 u32 pp;
f0f59a00 2305 i915_reg_t pp_ctrl_reg;
9934c132 2306
9f0fb5be
VS
2307 lockdep_assert_held(&dev_priv->pps_mutex);
2308
1853a9da 2309 if (!intel_dp_is_edp(intel_dp))
bd943159 2310 return;
99ea7127 2311
3936fcf4 2312 DRM_DEBUG_KMS("Turn eDP port %c panel power on\n",
8f4f2797 2313 port_name(dp_to_dig_port(intel_dp)->base.port));
e39b999a 2314
e7a89ace
VS
2315 if (WARN(edp_have_panel_power(intel_dp),
2316 "eDP port %c panel power already on\n",
8f4f2797 2317 port_name(dp_to_dig_port(intel_dp)->base.port)))
9f0fb5be 2318 return;
9934c132 2319
4be73780 2320 wait_panel_power_cycle(intel_dp);
37c6c9b0 2321
bf13e81b 2322 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
453c5420 2323 pp = ironlake_get_pp_control(intel_dp);
5db94019 2324 if (IS_GEN5(dev_priv)) {
05ce1a49
KP
2325 /* ILK workaround: disable reset around power sequence */
2326 pp &= ~PANEL_POWER_RESET;
bf13e81b
JN
2327 I915_WRITE(pp_ctrl_reg, pp);
2328 POSTING_READ(pp_ctrl_reg);
05ce1a49 2329 }
37c6c9b0 2330
5a162e22 2331 pp |= PANEL_POWER_ON;
5db94019 2332 if (!IS_GEN5(dev_priv))
99ea7127
KP
2333 pp |= PANEL_POWER_RESET;
2334
453c5420
JB
2335 I915_WRITE(pp_ctrl_reg, pp);
2336 POSTING_READ(pp_ctrl_reg);
9934c132 2337
4be73780 2338 wait_panel_on(intel_dp);
dce56b3c 2339 intel_dp->last_power_on = jiffies;
9934c132 2340
5db94019 2341 if (IS_GEN5(dev_priv)) {
05ce1a49 2342 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
bf13e81b
JN
2343 I915_WRITE(pp_ctrl_reg, pp);
2344 POSTING_READ(pp_ctrl_reg);
05ce1a49 2345 }
9f0fb5be 2346}
e39b999a 2347
9f0fb5be
VS
2348void intel_edp_panel_on(struct intel_dp *intel_dp)
2349{
1853a9da 2350 if (!intel_dp_is_edp(intel_dp))
9f0fb5be
VS
2351 return;
2352
2353 pps_lock(intel_dp);
2354 edp_panel_on(intel_dp);
773538e8 2355 pps_unlock(intel_dp);
9934c132
JB
2356}
2357
9f0fb5be
VS
2358
2359static void edp_panel_off(struct intel_dp *intel_dp)
9934c132 2360{
2f773477 2361 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
99ea7127 2362 u32 pp;
f0f59a00 2363 i915_reg_t pp_ctrl_reg;
9934c132 2364
9f0fb5be
VS
2365 lockdep_assert_held(&dev_priv->pps_mutex);
2366
1853a9da 2367 if (!intel_dp_is_edp(intel_dp))
97af61f5 2368 return;
37c6c9b0 2369
3936fcf4 2370 DRM_DEBUG_KMS("Turn eDP port %c panel power off\n",
8f4f2797 2371 port_name(dp_to_dig_port(intel_dp)->base.port));
37c6c9b0 2372
3936fcf4 2373 WARN(!intel_dp->want_panel_vdd, "Need eDP port %c VDD to turn off panel\n",
8f4f2797 2374 port_name(dp_to_dig_port(intel_dp)->base.port));
24f3e092 2375
453c5420 2376 pp = ironlake_get_pp_control(intel_dp);
35a38556
DV
2377 /* We need to switch off panel power _and_ force vdd, for otherwise some
2378 * panels get very unhappy and cease to work. */
5a162e22 2379 pp &= ~(PANEL_POWER_ON | PANEL_POWER_RESET | EDP_FORCE_VDD |
b3064154 2380 EDP_BLC_ENABLE);
453c5420 2381
bf13e81b 2382 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
453c5420 2383
849e39f5
PZ
2384 intel_dp->want_panel_vdd = false;
2385
453c5420
JB
2386 I915_WRITE(pp_ctrl_reg, pp);
2387 POSTING_READ(pp_ctrl_reg);
9934c132 2388
4be73780 2389 wait_panel_off(intel_dp);
d7ba25bd 2390 intel_dp->panel_power_off_time = ktime_get_boottime();
849e39f5
PZ
2391
2392 /* We got a reference when we enabled the VDD. */
5432fcaf 2393 intel_display_power_put(dev_priv, intel_dp->aux_power_domain);
9f0fb5be 2394}
e39b999a 2395
9f0fb5be
VS
2396void intel_edp_panel_off(struct intel_dp *intel_dp)
2397{
1853a9da 2398 if (!intel_dp_is_edp(intel_dp))
9f0fb5be 2399 return;
e39b999a 2400
9f0fb5be
VS
2401 pps_lock(intel_dp);
2402 edp_panel_off(intel_dp);
773538e8 2403 pps_unlock(intel_dp);
9934c132
JB
2404}
2405
1250d107
JN
2406/* Enable backlight in the panel power control. */
2407static void _intel_edp_backlight_on(struct intel_dp *intel_dp)
32f9d658 2408{
2f773477 2409 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
32f9d658 2410 u32 pp;
f0f59a00 2411 i915_reg_t pp_ctrl_reg;
32f9d658 2412
01cb9ea6
JB
2413 /*
2414 * If we enable the backlight right away following a panel power
2415 * on, we may see slight flicker as the panel syncs with the eDP
2416 * link. So delay a bit to make sure the image is solid before
2417 * allowing it to appear.
2418 */
4be73780 2419 wait_backlight_on(intel_dp);
e39b999a 2420
773538e8 2421 pps_lock(intel_dp);
e39b999a 2422
453c5420 2423 pp = ironlake_get_pp_control(intel_dp);
32f9d658 2424 pp |= EDP_BLC_ENABLE;
453c5420 2425
bf13e81b 2426 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
453c5420
JB
2427
2428 I915_WRITE(pp_ctrl_reg, pp);
2429 POSTING_READ(pp_ctrl_reg);
e39b999a 2430
773538e8 2431 pps_unlock(intel_dp);
32f9d658
ZW
2432}
2433
1250d107 2434/* Enable backlight PWM and backlight PP control. */
b037d58f
ML
2435void intel_edp_backlight_on(const struct intel_crtc_state *crtc_state,
2436 const struct drm_connector_state *conn_state)
1250d107 2437{
b037d58f
ML
2438 struct intel_dp *intel_dp = enc_to_intel_dp(conn_state->best_encoder);
2439
1853a9da 2440 if (!intel_dp_is_edp(intel_dp))
1250d107
JN
2441 return;
2442
2443 DRM_DEBUG_KMS("\n");
2444
b037d58f 2445 intel_panel_enable_backlight(crtc_state, conn_state);
1250d107
JN
2446 _intel_edp_backlight_on(intel_dp);
2447}
2448
2449/* Disable backlight in the panel power control. */
2450static void _intel_edp_backlight_off(struct intel_dp *intel_dp)
32f9d658 2451{
2f773477 2452 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
32f9d658 2453 u32 pp;
f0f59a00 2454 i915_reg_t pp_ctrl_reg;
32f9d658 2455
1853a9da 2456 if (!intel_dp_is_edp(intel_dp))
f01eca2e
KP
2457 return;
2458
773538e8 2459 pps_lock(intel_dp);
e39b999a 2460
453c5420 2461 pp = ironlake_get_pp_control(intel_dp);
32f9d658 2462 pp &= ~EDP_BLC_ENABLE;
453c5420 2463
bf13e81b 2464 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
453c5420
JB
2465
2466 I915_WRITE(pp_ctrl_reg, pp);
2467 POSTING_READ(pp_ctrl_reg);
f7d2323c 2468
773538e8 2469 pps_unlock(intel_dp);
e39b999a
VS
2470
2471 intel_dp->last_backlight_off = jiffies;
f7d2323c 2472 edp_wait_backlight_off(intel_dp);
1250d107 2473}
f7d2323c 2474
1250d107 2475/* Disable backlight PP control and backlight PWM. */
b037d58f 2476void intel_edp_backlight_off(const struct drm_connector_state *old_conn_state)
1250d107 2477{
b037d58f
ML
2478 struct intel_dp *intel_dp = enc_to_intel_dp(old_conn_state->best_encoder);
2479
1853a9da 2480 if (!intel_dp_is_edp(intel_dp))
1250d107
JN
2481 return;
2482
2483 DRM_DEBUG_KMS("\n");
f7d2323c 2484
1250d107 2485 _intel_edp_backlight_off(intel_dp);
b037d58f 2486 intel_panel_disable_backlight(old_conn_state);
32f9d658 2487}
a4fc5ed6 2488
73580fb7
JN
2489/*
2490 * Hook for controlling the panel power control backlight through the bl_power
2491 * sysfs attribute. Take care to handle multiple calls.
2492 */
2493static void intel_edp_backlight_power(struct intel_connector *connector,
2494 bool enable)
2495{
2496 struct intel_dp *intel_dp = intel_attached_dp(&connector->base);
e39b999a
VS
2497 bool is_enabled;
2498
773538e8 2499 pps_lock(intel_dp);
e39b999a 2500 is_enabled = ironlake_get_pp_control(intel_dp) & EDP_BLC_ENABLE;
773538e8 2501 pps_unlock(intel_dp);
73580fb7
JN
2502
2503 if (is_enabled == enable)
2504 return;
2505
23ba9373
JN
2506 DRM_DEBUG_KMS("panel power control backlight %s\n",
2507 enable ? "enable" : "disable");
73580fb7
JN
2508
2509 if (enable)
2510 _intel_edp_backlight_on(intel_dp);
2511 else
2512 _intel_edp_backlight_off(intel_dp);
2513}
2514
64e1077a
VS
2515static void assert_dp_port(struct intel_dp *intel_dp, bool state)
2516{
2517 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
2518 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
2519 bool cur_state = I915_READ(intel_dp->output_reg) & DP_PORT_EN;
2520
2521 I915_STATE_WARN(cur_state != state,
2522 "DP port %c state assertion failure (expected %s, current %s)\n",
8f4f2797 2523 port_name(dig_port->base.port),
87ad3212 2524 onoff(state), onoff(cur_state));
64e1077a
VS
2525}
2526#define assert_dp_port_disabled(d) assert_dp_port((d), false)
2527
2528static void assert_edp_pll(struct drm_i915_private *dev_priv, bool state)
2529{
2530 bool cur_state = I915_READ(DP_A) & DP_PLL_ENABLE;
2531
2532 I915_STATE_WARN(cur_state != state,
2533 "eDP PLL state assertion failure (expected %s, current %s)\n",
87ad3212 2534 onoff(state), onoff(cur_state));
64e1077a
VS
2535}
2536#define assert_edp_pll_enabled(d) assert_edp_pll((d), true)
2537#define assert_edp_pll_disabled(d) assert_edp_pll((d), false)
2538
85cb48a1 2539static void ironlake_edp_pll_on(struct intel_dp *intel_dp,
5f88a9c6 2540 const struct intel_crtc_state *pipe_config)
d240f20f 2541{
85cb48a1 2542 struct intel_crtc *crtc = to_intel_crtc(pipe_config->base.crtc);
64e1077a 2543 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
d240f20f 2544
64e1077a
VS
2545 assert_pipe_disabled(dev_priv, crtc->pipe);
2546 assert_dp_port_disabled(intel_dp);
2547 assert_edp_pll_disabled(dev_priv);
2bd2ad64 2548
abfce949 2549 DRM_DEBUG_KMS("enabling eDP PLL for clock %d\n",
85cb48a1 2550 pipe_config->port_clock);
abfce949
VS
2551
2552 intel_dp->DP &= ~DP_PLL_FREQ_MASK;
2553
85cb48a1 2554 if (pipe_config->port_clock == 162000)
abfce949
VS
2555 intel_dp->DP |= DP_PLL_FREQ_162MHZ;
2556 else
2557 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
2558
2559 I915_WRITE(DP_A, intel_dp->DP);
2560 POSTING_READ(DP_A);
2561 udelay(500);
2562
6b23f3e8
VS
2563 /*
2564 * [DevILK] Work around required when enabling DP PLL
2565 * while a pipe is enabled going to FDI:
2566 * 1. Wait for the start of vertical blank on the enabled pipe going to FDI
2567 * 2. Program DP PLL enable
2568 */
2569 if (IS_GEN5(dev_priv))
0f0f74bc 2570 intel_wait_for_vblank_if_active(dev_priv, !crtc->pipe);
6b23f3e8 2571
0767935e 2572 intel_dp->DP |= DP_PLL_ENABLE;
6fec7662 2573
0767935e 2574 I915_WRITE(DP_A, intel_dp->DP);
298b0b39
JB
2575 POSTING_READ(DP_A);
2576 udelay(200);
d240f20f
JB
2577}
2578
adc10304
VS
2579static void ironlake_edp_pll_off(struct intel_dp *intel_dp,
2580 const struct intel_crtc_state *old_crtc_state)
d240f20f 2581{
adc10304 2582 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
64e1077a 2583 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
d240f20f 2584
64e1077a
VS
2585 assert_pipe_disabled(dev_priv, crtc->pipe);
2586 assert_dp_port_disabled(intel_dp);
2587 assert_edp_pll_enabled(dev_priv);
2bd2ad64 2588
abfce949
VS
2589 DRM_DEBUG_KMS("disabling eDP PLL\n");
2590
6fec7662 2591 intel_dp->DP &= ~DP_PLL_ENABLE;
0767935e 2592
6fec7662 2593 I915_WRITE(DP_A, intel_dp->DP);
1af5fa1b 2594 POSTING_READ(DP_A);
d240f20f
JB
2595 udelay(200);
2596}
2597
857c416e
VS
2598static bool downstream_hpd_needs_d0(struct intel_dp *intel_dp)
2599{
2600 /*
2601 * DPCD 1.2+ should support BRANCH_DEVICE_CTRL, and thus
2602 * be capable of signalling downstream hpd with a long pulse.
2603 * Whether or not that means D3 is safe to use is not clear,
2604 * but let's assume so until proven otherwise.
2605 *
2606 * FIXME should really check all downstream ports...
2607 */
2608 return intel_dp->dpcd[DP_DPCD_REV] == 0x11 &&
2609 intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT &&
2610 intel_dp->downstream_ports[0] & DP_DS_PORT_HPD;
2611}
2612
c7ad3810 2613/* If the sink supports it, try to set the power state appropriately */
c19b0669 2614void intel_dp_sink_dpms(struct intel_dp *intel_dp, int mode)
c7ad3810
JB
2615{
2616 int ret, i;
2617
2618 /* Should have a valid DPCD by this point */
2619 if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
2620 return;
2621
2622 if (mode != DRM_MODE_DPMS_ON) {
857c416e
VS
2623 if (downstream_hpd_needs_d0(intel_dp))
2624 return;
2625
9d1a1031
JN
2626 ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER,
2627 DP_SET_POWER_D3);
c7ad3810 2628 } else {
357c0ae9
ID
2629 struct intel_lspcon *lspcon = dp_to_lspcon(intel_dp);
2630
c7ad3810
JB
2631 /*
2632 * When turning on, we need to retry for 1ms to give the sink
2633 * time to wake up.
2634 */
2635 for (i = 0; i < 3; i++) {
9d1a1031
JN
2636 ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER,
2637 DP_SET_POWER_D0);
c7ad3810
JB
2638 if (ret == 1)
2639 break;
2640 msleep(1);
2641 }
357c0ae9
ID
2642
2643 if (ret == 1 && lspcon->active)
2644 lspcon_wait_pcon_mode(lspcon);
c7ad3810 2645 }
f9cac721
JN
2646
2647 if (ret != 1)
2648 DRM_DEBUG_KMS("failed to %s sink power state\n",
2649 mode == DRM_MODE_DPMS_ON ? "enable" : "disable");
c7ad3810
JB
2650}
2651
19d8fe15
DV
2652static bool intel_dp_get_hw_state(struct intel_encoder *encoder,
2653 enum pipe *pipe)
d240f20f 2654{
2f773477 2655 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
19d8fe15 2656 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
8f4f2797 2657 enum port port = encoder->port;
6d129bea 2658 u32 tmp;
6fa9a5ec 2659 bool ret;
6d129bea 2660
79f255a0
ACO
2661 if (!intel_display_power_get_if_enabled(dev_priv,
2662 encoder->power_domain))
6d129bea
ID
2663 return false;
2664
6fa9a5ec
ID
2665 ret = false;
2666
6d129bea 2667 tmp = I915_READ(intel_dp->output_reg);
19d8fe15
DV
2668
2669 if (!(tmp & DP_PORT_EN))
6fa9a5ec 2670 goto out;
19d8fe15 2671
b752e995 2672 if (IS_IVYBRIDGE(dev_priv) && port == PORT_A) {
19d8fe15 2673 *pipe = PORT_TO_PIPE_CPT(tmp);
6e266956 2674 } else if (HAS_PCH_CPT(dev_priv) && port != PORT_A) {
adc289d7 2675 enum pipe p;
19d8fe15 2676
adc289d7
VS
2677 for_each_pipe(dev_priv, p) {
2678 u32 trans_dp = I915_READ(TRANS_DP_CTL(p));
2679 if (TRANS_DP_PIPE_TO_PORT(trans_dp) == port) {
2680 *pipe = p;
6fa9a5ec
ID
2681 ret = true;
2682
2683 goto out;
19d8fe15
DV
2684 }
2685 }
19d8fe15 2686
4a0833ec 2687 DRM_DEBUG_KMS("No pipe for dp port 0x%x found\n",
f0f59a00 2688 i915_mmio_reg_offset(intel_dp->output_reg));
920a14b2 2689 } else if (IS_CHERRYVIEW(dev_priv)) {
39e5fa88
VS
2690 *pipe = DP_PORT_TO_PIPE_CHV(tmp);
2691 } else {
2692 *pipe = PORT_TO_PIPE(tmp);
4a0833ec 2693 }
d240f20f 2694
6fa9a5ec
ID
2695 ret = true;
2696
2697out:
79f255a0 2698 intel_display_power_put(dev_priv, encoder->power_domain);
6fa9a5ec
ID
2699
2700 return ret;
19d8fe15 2701}
d240f20f 2702
045ac3b5 2703static void intel_dp_get_config(struct intel_encoder *encoder,
5cec258b 2704 struct intel_crtc_state *pipe_config)
045ac3b5 2705{
2f773477 2706 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
045ac3b5 2707 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
045ac3b5 2708 u32 tmp, flags = 0;
8f4f2797 2709 enum port port = encoder->port;
adc10304 2710 struct intel_crtc *crtc = to_intel_crtc(pipe_config->base.crtc);
045ac3b5 2711
e1214b95
VS
2712 if (encoder->type == INTEL_OUTPUT_EDP)
2713 pipe_config->output_types |= BIT(INTEL_OUTPUT_EDP);
2714 else
2715 pipe_config->output_types |= BIT(INTEL_OUTPUT_DP);
045ac3b5 2716
9ed109a7 2717 tmp = I915_READ(intel_dp->output_reg);
9fcb1704
JN
2718
2719 pipe_config->has_audio = tmp & DP_AUDIO_OUTPUT_ENABLE && port != PORT_A;
9ed109a7 2720
6e266956 2721 if (HAS_PCH_CPT(dev_priv) && port != PORT_A) {
b81e34c2
VS
2722 u32 trans_dp = I915_READ(TRANS_DP_CTL(crtc->pipe));
2723
2724 if (trans_dp & TRANS_DP_HSYNC_ACTIVE_HIGH)
63000ef6
XZ
2725 flags |= DRM_MODE_FLAG_PHSYNC;
2726 else
2727 flags |= DRM_MODE_FLAG_NHSYNC;
045ac3b5 2728
b81e34c2 2729 if (trans_dp & TRANS_DP_VSYNC_ACTIVE_HIGH)
63000ef6
XZ
2730 flags |= DRM_MODE_FLAG_PVSYNC;
2731 else
2732 flags |= DRM_MODE_FLAG_NVSYNC;
2733 } else {
39e5fa88 2734 if (tmp & DP_SYNC_HS_HIGH)
63000ef6
XZ
2735 flags |= DRM_MODE_FLAG_PHSYNC;
2736 else
2737 flags |= DRM_MODE_FLAG_NHSYNC;
045ac3b5 2738
39e5fa88 2739 if (tmp & DP_SYNC_VS_HIGH)
63000ef6
XZ
2740 flags |= DRM_MODE_FLAG_PVSYNC;
2741 else
2742 flags |= DRM_MODE_FLAG_NVSYNC;
2743 }
045ac3b5 2744
2d112de7 2745 pipe_config->base.adjusted_mode.flags |= flags;
f1f644dc 2746
c99f53f7 2747 if (IS_G4X(dev_priv) && tmp & DP_COLOR_RANGE_16_235)
8c875fca
VS
2748 pipe_config->limited_color_range = true;
2749
90a6b7b0
VS
2750 pipe_config->lane_count =
2751 ((tmp & DP_PORT_WIDTH_MASK) >> DP_PORT_WIDTH_SHIFT) + 1;
2752
eb14cb74
VS
2753 intel_dp_get_m_n(crtc, pipe_config);
2754
18442d08 2755 if (port == PORT_A) {
b377e0df 2756 if ((I915_READ(DP_A) & DP_PLL_FREQ_MASK) == DP_PLL_FREQ_162MHZ)
f1f644dc
JB
2757 pipe_config->port_clock = 162000;
2758 else
2759 pipe_config->port_clock = 270000;
2760 }
18442d08 2761
e3b247da
VS
2762 pipe_config->base.adjusted_mode.crtc_clock =
2763 intel_dotclock_calculate(pipe_config->port_clock,
2764 &pipe_config->dp_m_n);
7f16e5c1 2765
1853a9da 2766 if (intel_dp_is_edp(intel_dp) && dev_priv->vbt.edp.bpp &&
6aa23e65 2767 pipe_config->pipe_bpp > dev_priv->vbt.edp.bpp) {
c6cd2ee2
JN
2768 /*
2769 * This is a big fat ugly hack.
2770 *
2771 * Some machines in UEFI boot mode provide us a VBT that has 18
2772 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
2773 * unknown we fail to light up. Yet the same BIOS boots up with
2774 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
2775 * max, not what it tells us to use.
2776 *
2777 * Note: This will still be broken if the eDP panel is not lit
2778 * up by the BIOS, and thus we can't get the mode at module
2779 * load.
2780 */
2781 DRM_DEBUG_KMS("pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
6aa23e65
JN
2782 pipe_config->pipe_bpp, dev_priv->vbt.edp.bpp);
2783 dev_priv->vbt.edp.bpp = pipe_config->pipe_bpp;
c6cd2ee2 2784 }
045ac3b5
JB
2785}
2786
fd6bbda9 2787static void intel_disable_dp(struct intel_encoder *encoder,
5f88a9c6
VS
2788 const struct intel_crtc_state *old_crtc_state,
2789 const struct drm_connector_state *old_conn_state)
d240f20f 2790{
e8cb4558 2791 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
495a5bb8 2792
edb2e530
VS
2793 intel_dp->link_trained = false;
2794
85cb48a1 2795 if (old_crtc_state->has_audio)
8ec47de2
VS
2796 intel_audio_codec_disable(encoder,
2797 old_crtc_state, old_conn_state);
6cb49835
DV
2798
2799 /* Make sure the panel is off before trying to change the mode. But also
2800 * ensure that we have vdd while we switch off the panel. */
24f3e092 2801 intel_edp_panel_vdd_on(intel_dp);
b037d58f 2802 intel_edp_backlight_off(old_conn_state);
fdbc3b1f 2803 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
4be73780 2804 intel_edp_panel_off(intel_dp);
1a8ff607
VS
2805}
2806
2807static void g4x_disable_dp(struct intel_encoder *encoder,
2808 const struct intel_crtc_state *old_crtc_state,
2809 const struct drm_connector_state *old_conn_state)
2810{
1a8ff607 2811 intel_disable_dp(encoder, old_crtc_state, old_conn_state);
3739850b 2812
08aff3fe 2813 /* disable the port before the pipe on g4x */
adc10304 2814 intel_dp_link_down(encoder, old_crtc_state);
1a8ff607
VS
2815}
2816
2817static void ilk_disable_dp(struct intel_encoder *encoder,
2818 const struct intel_crtc_state *old_crtc_state,
2819 const struct drm_connector_state *old_conn_state)
2820{
2821 intel_disable_dp(encoder, old_crtc_state, old_conn_state);
2822}
2823
2824static void vlv_disable_dp(struct intel_encoder *encoder,
2825 const struct intel_crtc_state *old_crtc_state,
2826 const struct drm_connector_state *old_conn_state)
2827{
2828 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2829
2830 intel_psr_disable(intel_dp, old_crtc_state);
2831
2832 intel_disable_dp(encoder, old_crtc_state, old_conn_state);
d240f20f
JB
2833}
2834
fd6bbda9 2835static void ilk_post_disable_dp(struct intel_encoder *encoder,
5f88a9c6
VS
2836 const struct intel_crtc_state *old_crtc_state,
2837 const struct drm_connector_state *old_conn_state)
d240f20f 2838{
2bd2ad64 2839 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
adc10304 2840 enum port port = encoder->port;
2bd2ad64 2841
adc10304 2842 intel_dp_link_down(encoder, old_crtc_state);
abfce949
VS
2843
2844 /* Only ilk+ has port A */
08aff3fe 2845 if (port == PORT_A)
adc10304 2846 ironlake_edp_pll_off(intel_dp, old_crtc_state);
49277c31
VS
2847}
2848
fd6bbda9 2849static void vlv_post_disable_dp(struct intel_encoder *encoder,
5f88a9c6
VS
2850 const struct intel_crtc_state *old_crtc_state,
2851 const struct drm_connector_state *old_conn_state)
49277c31 2852{
adc10304 2853 intel_dp_link_down(encoder, old_crtc_state);
2bd2ad64
DV
2854}
2855
fd6bbda9 2856static void chv_post_disable_dp(struct intel_encoder *encoder,
5f88a9c6
VS
2857 const struct intel_crtc_state *old_crtc_state,
2858 const struct drm_connector_state *old_conn_state)
a8f327fb 2859{
adc10304 2860 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
97fd4d5c 2861
adc10304 2862 intel_dp_link_down(encoder, old_crtc_state);
a8f327fb
VS
2863
2864 mutex_lock(&dev_priv->sb_lock);
2865
2866 /* Assert data lane reset */
2e1029c6 2867 chv_data_lane_soft_reset(encoder, old_crtc_state, true);
580d3811 2868
a580516d 2869 mutex_unlock(&dev_priv->sb_lock);
580d3811
VS
2870}
2871
7b13b58a
VS
2872static void
2873_intel_dp_set_link_train(struct intel_dp *intel_dp,
2874 uint32_t *DP,
2875 uint8_t dp_train_pat)
2876{
2f773477 2877 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
7b13b58a 2878 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
8f4f2797 2879 enum port port = intel_dig_port->base.port;
7b13b58a 2880
8b0878a0
PD
2881 if (dp_train_pat & DP_TRAINING_PATTERN_MASK)
2882 DRM_DEBUG_KMS("Using DP training pattern TPS%d\n",
2883 dp_train_pat & DP_TRAINING_PATTERN_MASK);
2884
4f8036a2 2885 if (HAS_DDI(dev_priv)) {
7b13b58a
VS
2886 uint32_t temp = I915_READ(DP_TP_CTL(port));
2887
2888 if (dp_train_pat & DP_LINK_SCRAMBLING_DISABLE)
2889 temp |= DP_TP_CTL_SCRAMBLE_DISABLE;
2890 else
2891 temp &= ~DP_TP_CTL_SCRAMBLE_DISABLE;
2892
2893 temp &= ~DP_TP_CTL_LINK_TRAIN_MASK;
2894 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
2895 case DP_TRAINING_PATTERN_DISABLE:
2896 temp |= DP_TP_CTL_LINK_TRAIN_NORMAL;
2897
2898 break;
2899 case DP_TRAINING_PATTERN_1:
2900 temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
2901 break;
2902 case DP_TRAINING_PATTERN_2:
2903 temp |= DP_TP_CTL_LINK_TRAIN_PAT2;
2904 break;
2905 case DP_TRAINING_PATTERN_3:
2906 temp |= DP_TP_CTL_LINK_TRAIN_PAT3;
2907 break;
2908 }
2909 I915_WRITE(DP_TP_CTL(port), temp);
2910
b752e995 2911 } else if ((IS_IVYBRIDGE(dev_priv) && port == PORT_A) ||
6e266956 2912 (HAS_PCH_CPT(dev_priv) && port != PORT_A)) {
7b13b58a
VS
2913 *DP &= ~DP_LINK_TRAIN_MASK_CPT;
2914
2915 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
2916 case DP_TRAINING_PATTERN_DISABLE:
2917 *DP |= DP_LINK_TRAIN_OFF_CPT;
2918 break;
2919 case DP_TRAINING_PATTERN_1:
2920 *DP |= DP_LINK_TRAIN_PAT_1_CPT;
2921 break;
2922 case DP_TRAINING_PATTERN_2:
2923 *DP |= DP_LINK_TRAIN_PAT_2_CPT;
2924 break;
2925 case DP_TRAINING_PATTERN_3:
8b0878a0 2926 DRM_DEBUG_KMS("TPS3 not supported, using TPS2 instead\n");
7b13b58a
VS
2927 *DP |= DP_LINK_TRAIN_PAT_2_CPT;
2928 break;
2929 }
2930
2931 } else {
3b358cda 2932 *DP &= ~DP_LINK_TRAIN_MASK;
7b13b58a
VS
2933
2934 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
2935 case DP_TRAINING_PATTERN_DISABLE:
2936 *DP |= DP_LINK_TRAIN_OFF;
2937 break;
2938 case DP_TRAINING_PATTERN_1:
2939 *DP |= DP_LINK_TRAIN_PAT_1;
2940 break;
2941 case DP_TRAINING_PATTERN_2:
2942 *DP |= DP_LINK_TRAIN_PAT_2;
2943 break;
2944 case DP_TRAINING_PATTERN_3:
3b358cda
VS
2945 DRM_DEBUG_KMS("TPS3 not supported, using TPS2 instead\n");
2946 *DP |= DP_LINK_TRAIN_PAT_2;
7b13b58a
VS
2947 break;
2948 }
2949 }
2950}
2951
85cb48a1 2952static void intel_dp_enable_port(struct intel_dp *intel_dp,
5f88a9c6 2953 const struct intel_crtc_state *old_crtc_state)
7b13b58a 2954{
2f773477 2955 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
7b13b58a 2956
7b13b58a 2957 /* enable with pattern 1 (as per spec) */
7b13b58a 2958
8b0878a0 2959 intel_dp_program_link_training_pattern(intel_dp, DP_TRAINING_PATTERN_1);
7b713f50
VS
2960
2961 /*
2962 * Magic for VLV/CHV. We _must_ first set up the register
2963 * without actually enabling the port, and then do another
2964 * write to enable the port. Otherwise link training will
2965 * fail when the power sequencer is freshly used for this port.
2966 */
2967 intel_dp->DP |= DP_PORT_EN;
85cb48a1 2968 if (old_crtc_state->has_audio)
6fec7662 2969 intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
7b713f50
VS
2970
2971 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
2972 POSTING_READ(intel_dp->output_reg);
580d3811
VS
2973}
2974
85cb48a1 2975static void intel_enable_dp(struct intel_encoder *encoder,
5f88a9c6
VS
2976 const struct intel_crtc_state *pipe_config,
2977 const struct drm_connector_state *conn_state)
d240f20f 2978{
2f773477 2979 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
e8cb4558 2980 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
adc10304 2981 struct intel_crtc *crtc = to_intel_crtc(pipe_config->base.crtc);
e8cb4558 2982 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
d6fbdd15 2983 enum pipe pipe = crtc->pipe;
5d613501 2984
0c33d8d7
DV
2985 if (WARN_ON(dp_reg & DP_PORT_EN))
2986 return;
5d613501 2987
093e3f13
VS
2988 pps_lock(intel_dp);
2989
920a14b2 2990 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
adc10304 2991 vlv_init_panel_power_sequencer(encoder, pipe_config);
093e3f13 2992
85cb48a1 2993 intel_dp_enable_port(intel_dp, pipe_config);
093e3f13
VS
2994
2995 edp_panel_vdd_on(intel_dp);
2996 edp_panel_on(intel_dp);
2997 edp_panel_vdd_off(intel_dp, true);
2998
2999 pps_unlock(intel_dp);
3000
920a14b2 3001 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
e0fce78f
VS
3002 unsigned int lane_mask = 0x0;
3003
920a14b2 3004 if (IS_CHERRYVIEW(dev_priv))
85cb48a1 3005 lane_mask = intel_dp_unused_lane_mask(pipe_config->lane_count);
e0fce78f 3006
9b6de0a1
VS
3007 vlv_wait_port_ready(dev_priv, dp_to_dig_port(intel_dp),
3008 lane_mask);
e0fce78f 3009 }
61234fa5 3010
f01eca2e 3011 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
33a34e4e 3012 intel_dp_start_link_train(intel_dp);
3ab9c637 3013 intel_dp_stop_link_train(intel_dp);
c1dec79a 3014
85cb48a1 3015 if (pipe_config->has_audio) {
c1dec79a 3016 DRM_DEBUG_DRIVER("Enabling DP audio on pipe %c\n",
d6fbdd15 3017 pipe_name(pipe));
bbf35e9d 3018 intel_audio_codec_enable(encoder, pipe_config, conn_state);
c1dec79a 3019 }
ab1f90f9 3020}
89b667f8 3021
fd6bbda9 3022static void g4x_enable_dp(struct intel_encoder *encoder,
5f88a9c6
VS
3023 const struct intel_crtc_state *pipe_config,
3024 const struct drm_connector_state *conn_state)
ecff4f3b 3025{
bbf35e9d 3026 intel_enable_dp(encoder, pipe_config, conn_state);
b037d58f 3027 intel_edp_backlight_on(pipe_config, conn_state);
ab1f90f9 3028}
89b667f8 3029
fd6bbda9 3030static void vlv_enable_dp(struct intel_encoder *encoder,
5f88a9c6
VS
3031 const struct intel_crtc_state *pipe_config,
3032 const struct drm_connector_state *conn_state)
ab1f90f9 3033{
828f5c6e
JN
3034 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
3035
b037d58f 3036 intel_edp_backlight_on(pipe_config, conn_state);
d2419ffc 3037 intel_psr_enable(intel_dp, pipe_config);
d240f20f
JB
3038}
3039
fd6bbda9 3040static void g4x_pre_enable_dp(struct intel_encoder *encoder,
5f88a9c6
VS
3041 const struct intel_crtc_state *pipe_config,
3042 const struct drm_connector_state *conn_state)
ab1f90f9
JN
3043{
3044 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
8f4f2797 3045 enum port port = encoder->port;
ab1f90f9 3046
85cb48a1 3047 intel_dp_prepare(encoder, pipe_config);
8ac33ed3 3048
d41f1efb 3049 /* Only ilk+ has port A */
abfce949 3050 if (port == PORT_A)
85cb48a1 3051 ironlake_edp_pll_on(intel_dp, pipe_config);
ab1f90f9
JN
3052}
3053
83b84597
VS
3054static void vlv_detach_power_sequencer(struct intel_dp *intel_dp)
3055{
3056 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
fac5e23e 3057 struct drm_i915_private *dev_priv = to_i915(intel_dig_port->base.base.dev);
83b84597 3058 enum pipe pipe = intel_dp->pps_pipe;
44cb734c 3059 i915_reg_t pp_on_reg = PP_ON_DELAYS(pipe);
83b84597 3060
9f2bdb00
VS
3061 WARN_ON(intel_dp->active_pipe != INVALID_PIPE);
3062
d158694f
VS
3063 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
3064 return;
3065
83b84597
VS
3066 edp_panel_vdd_off_sync(intel_dp);
3067
3068 /*
e7f2af78 3069 * VLV seems to get confused when multiple power sequencers
83b84597
VS
3070 * have the same port selected (even if only one has power/vdd
3071 * enabled). The failure manifests as vlv_wait_port_ready() failing
3072 * CHV on the other hand doesn't seem to mind having the same port
e7f2af78 3073 * selected in multiple power sequencers, but let's clear the
83b84597
VS
3074 * port select always when logically disconnecting a power sequencer
3075 * from a port.
3076 */
3077 DRM_DEBUG_KMS("detaching pipe %c power sequencer from port %c\n",
8f4f2797 3078 pipe_name(pipe), port_name(intel_dig_port->base.port));
83b84597
VS
3079 I915_WRITE(pp_on_reg, 0);
3080 POSTING_READ(pp_on_reg);
3081
3082 intel_dp->pps_pipe = INVALID_PIPE;
3083}
3084
46bd8383 3085static void vlv_steal_power_sequencer(struct drm_i915_private *dev_priv,
a4a5d2f8
VS
3086 enum pipe pipe)
3087{
a4a5d2f8
VS
3088 struct intel_encoder *encoder;
3089
3090 lockdep_assert_held(&dev_priv->pps_mutex);
3091
46bd8383 3092 for_each_intel_encoder(&dev_priv->drm, encoder) {
a4a5d2f8 3093 struct intel_dp *intel_dp;
773538e8 3094 enum port port;
a4a5d2f8 3095
9f2bdb00
VS
3096 if (encoder->type != INTEL_OUTPUT_DP &&
3097 encoder->type != INTEL_OUTPUT_EDP)
a4a5d2f8
VS
3098 continue;
3099
3100 intel_dp = enc_to_intel_dp(&encoder->base);
8f4f2797 3101 port = dp_to_dig_port(intel_dp)->base.port;
a4a5d2f8 3102
9f2bdb00
VS
3103 WARN(intel_dp->active_pipe == pipe,
3104 "stealing pipe %c power sequencer from active (e)DP port %c\n",
3105 pipe_name(pipe), port_name(port));
3106
a4a5d2f8
VS
3107 if (intel_dp->pps_pipe != pipe)
3108 continue;
3109
3110 DRM_DEBUG_KMS("stealing pipe %c power sequencer from port %c\n",
773538e8 3111 pipe_name(pipe), port_name(port));
a4a5d2f8
VS
3112
3113 /* make sure vdd is off before we steal it */
83b84597 3114 vlv_detach_power_sequencer(intel_dp);
a4a5d2f8
VS
3115 }
3116}
3117
adc10304
VS
3118static void vlv_init_panel_power_sequencer(struct intel_encoder *encoder,
3119 const struct intel_crtc_state *crtc_state)
a4a5d2f8 3120{
46bd8383 3121 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
adc10304 3122 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
adc10304 3123 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a4a5d2f8
VS
3124
3125 lockdep_assert_held(&dev_priv->pps_mutex);
3126
9f2bdb00 3127 WARN_ON(intel_dp->active_pipe != INVALID_PIPE);
093e3f13 3128
9f2bdb00
VS
3129 if (intel_dp->pps_pipe != INVALID_PIPE &&
3130 intel_dp->pps_pipe != crtc->pipe) {
3131 /*
3132 * If another power sequencer was being used on this
3133 * port previously make sure to turn off vdd there while
3134 * we still have control of it.
3135 */
83b84597 3136 vlv_detach_power_sequencer(intel_dp);
9f2bdb00 3137 }
a4a5d2f8
VS
3138
3139 /*
3140 * We may be stealing the power
3141 * sequencer from another port.
3142 */
46bd8383 3143 vlv_steal_power_sequencer(dev_priv, crtc->pipe);
a4a5d2f8 3144
9f2bdb00
VS
3145 intel_dp->active_pipe = crtc->pipe;
3146
1853a9da 3147 if (!intel_dp_is_edp(intel_dp))
9f2bdb00
VS
3148 return;
3149
a4a5d2f8
VS
3150 /* now it's all ours */
3151 intel_dp->pps_pipe = crtc->pipe;
3152
3153 DRM_DEBUG_KMS("initializing pipe %c power sequencer for port %c\n",
adc10304 3154 pipe_name(intel_dp->pps_pipe), port_name(encoder->port));
a4a5d2f8
VS
3155
3156 /* init power sequencer on this pipe and port */
46bd8383
VS
3157 intel_dp_init_panel_power_sequencer(intel_dp);
3158 intel_dp_init_panel_power_sequencer_registers(intel_dp, true);
a4a5d2f8
VS
3159}
3160
fd6bbda9 3161static void vlv_pre_enable_dp(struct intel_encoder *encoder,
5f88a9c6
VS
3162 const struct intel_crtc_state *pipe_config,
3163 const struct drm_connector_state *conn_state)
a4fc5ed6 3164{
2e1029c6 3165 vlv_phy_pre_encoder_enable(encoder, pipe_config);
ab1f90f9 3166
bbf35e9d 3167 intel_enable_dp(encoder, pipe_config, conn_state);
89b667f8
JB
3168}
3169
fd6bbda9 3170static void vlv_dp_pre_pll_enable(struct intel_encoder *encoder,
5f88a9c6
VS
3171 const struct intel_crtc_state *pipe_config,
3172 const struct drm_connector_state *conn_state)
89b667f8 3173{
85cb48a1 3174 intel_dp_prepare(encoder, pipe_config);
8ac33ed3 3175
2e1029c6 3176 vlv_phy_pre_pll_enable(encoder, pipe_config);
a4fc5ed6
KP
3177}
3178
fd6bbda9 3179static void chv_pre_enable_dp(struct intel_encoder *encoder,
5f88a9c6
VS
3180 const struct intel_crtc_state *pipe_config,
3181 const struct drm_connector_state *conn_state)
e4a1d846 3182{
2e1029c6 3183 chv_phy_pre_encoder_enable(encoder, pipe_config);
e4a1d846 3184
bbf35e9d 3185 intel_enable_dp(encoder, pipe_config, conn_state);
b0b33846
VS
3186
3187 /* Second common lane will stay alive on its own now */
e7d2a717 3188 chv_phy_release_cl2_override(encoder);
e4a1d846
CML
3189}
3190
fd6bbda9 3191static void chv_dp_pre_pll_enable(struct intel_encoder *encoder,
5f88a9c6
VS
3192 const struct intel_crtc_state *pipe_config,
3193 const struct drm_connector_state *conn_state)
9197c88b 3194{
85cb48a1 3195 intel_dp_prepare(encoder, pipe_config);
625695f8 3196
2e1029c6 3197 chv_phy_pre_pll_enable(encoder, pipe_config);
9197c88b
VS
3198}
3199
fd6bbda9 3200static void chv_dp_post_pll_disable(struct intel_encoder *encoder,
2e1029c6
VS
3201 const struct intel_crtc_state *old_crtc_state,
3202 const struct drm_connector_state *old_conn_state)
d6db995f 3203{
2e1029c6 3204 chv_phy_post_pll_disable(encoder, old_crtc_state);
d6db995f
VS
3205}
3206
a4fc5ed6
KP
3207/*
3208 * Fetch AUX CH registers 0x202 - 0x207 which contain
3209 * link status information
3210 */
94223d04 3211bool
93f62dad 3212intel_dp_get_link_status(struct intel_dp *intel_dp, uint8_t link_status[DP_LINK_STATUS_SIZE])
a4fc5ed6 3213{
9f085ebb
L
3214 return drm_dp_dpcd_read(&intel_dp->aux, DP_LANE0_1_STATUS, link_status,
3215 DP_LINK_STATUS_SIZE) == DP_LINK_STATUS_SIZE;
a4fc5ed6
KP
3216}
3217
1100244e 3218/* These are source-specific values. */
94223d04 3219uint8_t
1a2eb460 3220intel_dp_voltage_max(struct intel_dp *intel_dp)
a4fc5ed6 3221{
dd11bc10 3222 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
a393e964
VS
3223 struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3224 enum port port = encoder->port;
1a2eb460 3225
a393e964 3226 if (HAS_DDI(dev_priv))
ffe5111e 3227 return intel_ddi_dp_voltage_max(encoder);
a393e964 3228 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
bd60018a 3229 return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
b752e995 3230 else if (IS_IVYBRIDGE(dev_priv) && port == PORT_A)
bd60018a 3231 return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
6e266956 3232 else if (HAS_PCH_CPT(dev_priv) && port != PORT_A)
bd60018a 3233 return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
1a2eb460 3234 else
bd60018a 3235 return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
1a2eb460
KP
3236}
3237
94223d04 3238uint8_t
1a2eb460
KP
3239intel_dp_pre_emphasis_max(struct intel_dp *intel_dp, uint8_t voltage_swing)
3240{
8652744b 3241 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
4718a365
VS
3242 struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3243 enum port port = encoder->port;
1a2eb460 3244
4718a365
VS
3245 if (HAS_DDI(dev_priv)) {
3246 return intel_ddi_dp_pre_emphasis_max(encoder, voltage_swing);
8652744b 3247 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
e2fa6fba 3248 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a
SJ
3249 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
3250 return DP_TRAIN_PRE_EMPH_LEVEL_3;
3251 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
3252 return DP_TRAIN_PRE_EMPH_LEVEL_2;
3253 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
3254 return DP_TRAIN_PRE_EMPH_LEVEL_1;
3255 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
e2fa6fba 3256 default:
bd60018a 3257 return DP_TRAIN_PRE_EMPH_LEVEL_0;
e2fa6fba 3258 }
b752e995 3259 } else if (IS_IVYBRIDGE(dev_priv) && port == PORT_A) {
1a2eb460 3260 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a
SJ
3261 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
3262 return DP_TRAIN_PRE_EMPH_LEVEL_2;
3263 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
3264 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
3265 return DP_TRAIN_PRE_EMPH_LEVEL_1;
1a2eb460 3266 default:
bd60018a 3267 return DP_TRAIN_PRE_EMPH_LEVEL_0;
1a2eb460
KP
3268 }
3269 } else {
3270 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a
SJ
3271 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
3272 return DP_TRAIN_PRE_EMPH_LEVEL_2;
3273 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
3274 return DP_TRAIN_PRE_EMPH_LEVEL_2;
3275 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
3276 return DP_TRAIN_PRE_EMPH_LEVEL_1;
3277 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
1a2eb460 3278 default:
bd60018a 3279 return DP_TRAIN_PRE_EMPH_LEVEL_0;
1a2eb460 3280 }
a4fc5ed6
KP
3281 }
3282}
3283
5829975c 3284static uint32_t vlv_signal_levels(struct intel_dp *intel_dp)
e2fa6fba 3285{
53d98725 3286 struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
e2fa6fba
P
3287 unsigned long demph_reg_value, preemph_reg_value,
3288 uniqtranscale_reg_value;
3289 uint8_t train_set = intel_dp->train_set[0];
e2fa6fba
P
3290
3291 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
bd60018a 3292 case DP_TRAIN_PRE_EMPH_LEVEL_0:
e2fa6fba
P
3293 preemph_reg_value = 0x0004000;
3294 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a 3295 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
e2fa6fba
P
3296 demph_reg_value = 0x2B405555;
3297 uniqtranscale_reg_value = 0x552AB83A;
3298 break;
bd60018a 3299 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
e2fa6fba
P
3300 demph_reg_value = 0x2B404040;
3301 uniqtranscale_reg_value = 0x5548B83A;
3302 break;
bd60018a 3303 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
e2fa6fba
P
3304 demph_reg_value = 0x2B245555;
3305 uniqtranscale_reg_value = 0x5560B83A;
3306 break;
bd60018a 3307 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
e2fa6fba
P
3308 demph_reg_value = 0x2B405555;
3309 uniqtranscale_reg_value = 0x5598DA3A;
3310 break;
3311 default:
3312 return 0;
3313 }
3314 break;
bd60018a 3315 case DP_TRAIN_PRE_EMPH_LEVEL_1:
e2fa6fba
P
3316 preemph_reg_value = 0x0002000;
3317 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a 3318 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
e2fa6fba
P
3319 demph_reg_value = 0x2B404040;
3320 uniqtranscale_reg_value = 0x5552B83A;
3321 break;
bd60018a 3322 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
e2fa6fba
P
3323 demph_reg_value = 0x2B404848;
3324 uniqtranscale_reg_value = 0x5580B83A;
3325 break;
bd60018a 3326 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
e2fa6fba
P
3327 demph_reg_value = 0x2B404040;
3328 uniqtranscale_reg_value = 0x55ADDA3A;
3329 break;
3330 default:
3331 return 0;
3332 }
3333 break;
bd60018a 3334 case DP_TRAIN_PRE_EMPH_LEVEL_2:
e2fa6fba
P
3335 preemph_reg_value = 0x0000000;
3336 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a 3337 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
e2fa6fba
P
3338 demph_reg_value = 0x2B305555;
3339 uniqtranscale_reg_value = 0x5570B83A;
3340 break;
bd60018a 3341 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
e2fa6fba
P
3342 demph_reg_value = 0x2B2B4040;
3343 uniqtranscale_reg_value = 0x55ADDA3A;
3344 break;
3345 default:
3346 return 0;
3347 }
3348 break;
bd60018a 3349 case DP_TRAIN_PRE_EMPH_LEVEL_3:
e2fa6fba
P
3350 preemph_reg_value = 0x0006000;
3351 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a 3352 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
e2fa6fba
P
3353 demph_reg_value = 0x1B405555;
3354 uniqtranscale_reg_value = 0x55ADDA3A;
3355 break;
3356 default:
3357 return 0;
3358 }
3359 break;
3360 default:
3361 return 0;
3362 }
3363
53d98725
ACO
3364 vlv_set_phy_signal_level(encoder, demph_reg_value, preemph_reg_value,
3365 uniqtranscale_reg_value, 0);
e2fa6fba
P
3366
3367 return 0;
3368}
3369
5829975c 3370static uint32_t chv_signal_levels(struct intel_dp *intel_dp)
e4a1d846 3371{
b7fa22d8
ACO
3372 struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3373 u32 deemph_reg_value, margin_reg_value;
3374 bool uniq_trans_scale = false;
e4a1d846 3375 uint8_t train_set = intel_dp->train_set[0];
e4a1d846
CML
3376
3377 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
bd60018a 3378 case DP_TRAIN_PRE_EMPH_LEVEL_0:
e4a1d846 3379 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a 3380 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
e4a1d846
CML
3381 deemph_reg_value = 128;
3382 margin_reg_value = 52;
3383 break;
bd60018a 3384 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
e4a1d846
CML
3385 deemph_reg_value = 128;
3386 margin_reg_value = 77;
3387 break;
bd60018a 3388 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
e4a1d846
CML
3389 deemph_reg_value = 128;
3390 margin_reg_value = 102;
3391 break;
bd60018a 3392 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
e4a1d846
CML
3393 deemph_reg_value = 128;
3394 margin_reg_value = 154;
b7fa22d8 3395 uniq_trans_scale = true;
e4a1d846
CML
3396 break;
3397 default:
3398 return 0;
3399 }
3400 break;
bd60018a 3401 case DP_TRAIN_PRE_EMPH_LEVEL_1:
e4a1d846 3402 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a 3403 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
e4a1d846
CML
3404 deemph_reg_value = 85;
3405 margin_reg_value = 78;
3406 break;
bd60018a 3407 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
e4a1d846
CML
3408 deemph_reg_value = 85;
3409 margin_reg_value = 116;
3410 break;
bd60018a 3411 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
e4a1d846
CML
3412 deemph_reg_value = 85;
3413 margin_reg_value = 154;
3414 break;
3415 default:
3416 return 0;
3417 }
3418 break;
bd60018a 3419 case DP_TRAIN_PRE_EMPH_LEVEL_2:
e4a1d846 3420 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a 3421 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
e4a1d846
CML
3422 deemph_reg_value = 64;
3423 margin_reg_value = 104;
3424 break;
bd60018a 3425 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
e4a1d846
CML
3426 deemph_reg_value = 64;
3427 margin_reg_value = 154;
3428 break;
3429 default:
3430 return 0;
3431 }
3432 break;
bd60018a 3433 case DP_TRAIN_PRE_EMPH_LEVEL_3:
e4a1d846 3434 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a 3435 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
e4a1d846
CML
3436 deemph_reg_value = 43;
3437 margin_reg_value = 154;
3438 break;
3439 default:
3440 return 0;
3441 }
3442 break;
3443 default:
3444 return 0;
3445 }
3446
b7fa22d8
ACO
3447 chv_set_phy_signal_level(encoder, deemph_reg_value,
3448 margin_reg_value, uniq_trans_scale);
e4a1d846
CML
3449
3450 return 0;
3451}
3452
a4fc5ed6 3453static uint32_t
5829975c 3454gen4_signal_levels(uint8_t train_set)
a4fc5ed6 3455{
3cf2efb1 3456 uint32_t signal_levels = 0;
a4fc5ed6 3457
3cf2efb1 3458 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a 3459 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
a4fc5ed6
KP
3460 default:
3461 signal_levels |= DP_VOLTAGE_0_4;
3462 break;
bd60018a 3463 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
a4fc5ed6
KP
3464 signal_levels |= DP_VOLTAGE_0_6;
3465 break;
bd60018a 3466 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
a4fc5ed6
KP
3467 signal_levels |= DP_VOLTAGE_0_8;
3468 break;
bd60018a 3469 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
a4fc5ed6
KP
3470 signal_levels |= DP_VOLTAGE_1_2;
3471 break;
3472 }
3cf2efb1 3473 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
bd60018a 3474 case DP_TRAIN_PRE_EMPH_LEVEL_0:
a4fc5ed6
KP
3475 default:
3476 signal_levels |= DP_PRE_EMPHASIS_0;
3477 break;
bd60018a 3478 case DP_TRAIN_PRE_EMPH_LEVEL_1:
a4fc5ed6
KP
3479 signal_levels |= DP_PRE_EMPHASIS_3_5;
3480 break;
bd60018a 3481 case DP_TRAIN_PRE_EMPH_LEVEL_2:
a4fc5ed6
KP
3482 signal_levels |= DP_PRE_EMPHASIS_6;
3483 break;
bd60018a 3484 case DP_TRAIN_PRE_EMPH_LEVEL_3:
a4fc5ed6
KP
3485 signal_levels |= DP_PRE_EMPHASIS_9_5;
3486 break;
3487 }
3488 return signal_levels;
3489}
3490
4d82c2b5 3491/* SNB CPU eDP voltage swing and pre-emphasis control */
e3421a18 3492static uint32_t
4d82c2b5 3493snb_cpu_edp_signal_levels(uint8_t train_set)
e3421a18 3494{
3c5a62b5
YL
3495 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
3496 DP_TRAIN_PRE_EMPHASIS_MASK);
3497 switch (signal_levels) {
bd60018a
SJ
3498 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0:
3499 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0:
3c5a62b5 3500 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
bd60018a 3501 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1:
3c5a62b5 3502 return EDP_LINK_TRAIN_400MV_3_5DB_SNB_B;
bd60018a
SJ
3503 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2:
3504 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_2:
3c5a62b5 3505 return EDP_LINK_TRAIN_400_600MV_6DB_SNB_B;
bd60018a
SJ
3506 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1:
3507 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1:
3c5a62b5 3508 return EDP_LINK_TRAIN_600_800MV_3_5DB_SNB_B;
bd60018a
SJ
3509 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0:
3510 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3 | DP_TRAIN_PRE_EMPH_LEVEL_0:
3c5a62b5 3511 return EDP_LINK_TRAIN_800_1200MV_0DB_SNB_B;
e3421a18 3512 default:
3c5a62b5
YL
3513 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
3514 "0x%x\n", signal_levels);
3515 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
e3421a18
ZW
3516 }
3517}
3518
4d82c2b5 3519/* IVB CPU eDP voltage swing and pre-emphasis control */
1a2eb460 3520static uint32_t
4d82c2b5 3521ivb_cpu_edp_signal_levels(uint8_t train_set)
1a2eb460
KP
3522{
3523 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
3524 DP_TRAIN_PRE_EMPHASIS_MASK);
3525 switch (signal_levels) {
bd60018a 3526 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0:
1a2eb460 3527 return EDP_LINK_TRAIN_400MV_0DB_IVB;
bd60018a 3528 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1:
1a2eb460 3529 return EDP_LINK_TRAIN_400MV_3_5DB_IVB;
bd60018a 3530 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2:
1a2eb460
KP
3531 return EDP_LINK_TRAIN_400MV_6DB_IVB;
3532
bd60018a 3533 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0:
1a2eb460 3534 return EDP_LINK_TRAIN_600MV_0DB_IVB;
bd60018a 3535 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1:
1a2eb460
KP
3536 return EDP_LINK_TRAIN_600MV_3_5DB_IVB;
3537
bd60018a 3538 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0:
1a2eb460 3539 return EDP_LINK_TRAIN_800MV_0DB_IVB;
bd60018a 3540 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1:
1a2eb460
KP
3541 return EDP_LINK_TRAIN_800MV_3_5DB_IVB;
3542
3543 default:
3544 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
3545 "0x%x\n", signal_levels);
3546 return EDP_LINK_TRAIN_500MV_0DB_IVB;
3547 }
3548}
3549
94223d04 3550void
f4eb692e 3551intel_dp_set_signal_levels(struct intel_dp *intel_dp)
f0a3424e 3552{
2f773477 3553 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
f0a3424e 3554 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
8f4f2797 3555 enum port port = intel_dig_port->base.port;
f8896f5d 3556 uint32_t signal_levels, mask = 0;
f0a3424e
PZ
3557 uint8_t train_set = intel_dp->train_set[0];
3558
d509af6c
RV
3559 if (IS_GEN9_LP(dev_priv) || IS_CANNONLAKE(dev_priv)) {
3560 signal_levels = bxt_signal_levels(intel_dp);
3561 } else if (HAS_DDI(dev_priv)) {
f8896f5d 3562 signal_levels = ddi_signal_levels(intel_dp);
d509af6c 3563 mask = DDI_BUF_EMP_MASK;
920a14b2 3564 } else if (IS_CHERRYVIEW(dev_priv)) {
5829975c 3565 signal_levels = chv_signal_levels(intel_dp);
11a914c2 3566 } else if (IS_VALLEYVIEW(dev_priv)) {
5829975c 3567 signal_levels = vlv_signal_levels(intel_dp);
b752e995 3568 } else if (IS_IVYBRIDGE(dev_priv) && port == PORT_A) {
4d82c2b5 3569 signal_levels = ivb_cpu_edp_signal_levels(train_set);
f0a3424e 3570 mask = EDP_LINK_TRAIN_VOL_EMP_MASK_IVB;
5db94019 3571 } else if (IS_GEN6(dev_priv) && port == PORT_A) {
4d82c2b5 3572 signal_levels = snb_cpu_edp_signal_levels(train_set);
f0a3424e
PZ
3573 mask = EDP_LINK_TRAIN_VOL_EMP_MASK_SNB;
3574 } else {
5829975c 3575 signal_levels = gen4_signal_levels(train_set);
f0a3424e
PZ
3576 mask = DP_VOLTAGE_MASK | DP_PRE_EMPHASIS_MASK;
3577 }
3578
96fb9f9b
VK
3579 if (mask)
3580 DRM_DEBUG_KMS("Using signal levels %08x\n", signal_levels);
3581
3582 DRM_DEBUG_KMS("Using vswing level %d\n",
3583 train_set & DP_TRAIN_VOLTAGE_SWING_MASK);
3584 DRM_DEBUG_KMS("Using pre-emphasis level %d\n",
3585 (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) >>
3586 DP_TRAIN_PRE_EMPHASIS_SHIFT);
f0a3424e 3587
f4eb692e 3588 intel_dp->DP = (intel_dp->DP & ~mask) | signal_levels;
b905a915
ACO
3589
3590 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
3591 POSTING_READ(intel_dp->output_reg);
f0a3424e
PZ
3592}
3593
94223d04 3594void
e9c176d5
ACO
3595intel_dp_program_link_training_pattern(struct intel_dp *intel_dp,
3596 uint8_t dp_train_pat)
a4fc5ed6 3597{
174edf1f 3598 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
90a6b7b0
VS
3599 struct drm_i915_private *dev_priv =
3600 to_i915(intel_dig_port->base.base.dev);
a4fc5ed6 3601
f4eb692e 3602 _intel_dp_set_link_train(intel_dp, &intel_dp->DP, dp_train_pat);
47ea7542 3603
f4eb692e 3604 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
ea5b213a 3605 POSTING_READ(intel_dp->output_reg);
e9c176d5
ACO
3606}
3607
94223d04 3608void intel_dp_set_idle_link_train(struct intel_dp *intel_dp)
3ab9c637 3609{
2f773477 3610 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
3ab9c637 3611 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
8f4f2797 3612 enum port port = intel_dig_port->base.port;
3ab9c637
ID
3613 uint32_t val;
3614
4f8036a2 3615 if (!HAS_DDI(dev_priv))
3ab9c637
ID
3616 return;
3617
3618 val = I915_READ(DP_TP_CTL(port));
3619 val &= ~DP_TP_CTL_LINK_TRAIN_MASK;
3620 val |= DP_TP_CTL_LINK_TRAIN_IDLE;
3621 I915_WRITE(DP_TP_CTL(port), val);
3622
3623 /*
3624 * On PORT_A we can have only eDP in SST mode. There the only reason
3625 * we need to set idle transmission mode is to work around a HW issue
3626 * where we enable the pipe while not in idle link-training mode.
3627 * In this case there is requirement to wait for a minimum number of
3628 * idle patterns to be sent.
3629 */
3630 if (port == PORT_A)
3631 return;
3632
a767017f
CW
3633 if (intel_wait_for_register(dev_priv,DP_TP_STATUS(port),
3634 DP_TP_STATUS_IDLE_DONE,
3635 DP_TP_STATUS_IDLE_DONE,
3636 1))
3ab9c637
ID
3637 DRM_ERROR("Timed out waiting for DP idle patterns\n");
3638}
3639
a4fc5ed6 3640static void
adc10304
VS
3641intel_dp_link_down(struct intel_encoder *encoder,
3642 const struct intel_crtc_state *old_crtc_state)
a4fc5ed6 3643{
adc10304
VS
3644 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3645 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
3646 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
3647 enum port port = encoder->port;
ea5b213a 3648 uint32_t DP = intel_dp->DP;
a4fc5ed6 3649
4f8036a2 3650 if (WARN_ON(HAS_DDI(dev_priv)))
c19b0669
PZ
3651 return;
3652
0c33d8d7 3653 if (WARN_ON((I915_READ(intel_dp->output_reg) & DP_PORT_EN) == 0))
1b39d6f3
CW
3654 return;
3655
28c97730 3656 DRM_DEBUG_KMS("\n");
32f9d658 3657
b752e995 3658 if ((IS_IVYBRIDGE(dev_priv) && port == PORT_A) ||
6e266956 3659 (HAS_PCH_CPT(dev_priv) && port != PORT_A)) {
e3421a18 3660 DP &= ~DP_LINK_TRAIN_MASK_CPT;
1612c8bd 3661 DP |= DP_LINK_TRAIN_PAT_IDLE_CPT;
e3421a18 3662 } else {
3b358cda 3663 DP &= ~DP_LINK_TRAIN_MASK;
1612c8bd 3664 DP |= DP_LINK_TRAIN_PAT_IDLE;
e3421a18 3665 }
1612c8bd 3666 I915_WRITE(intel_dp->output_reg, DP);
fe255d00 3667 POSTING_READ(intel_dp->output_reg);
5eb08b69 3668
1612c8bd
VS
3669 DP &= ~(DP_PORT_EN | DP_AUDIO_OUTPUT_ENABLE);
3670 I915_WRITE(intel_dp->output_reg, DP);
3671 POSTING_READ(intel_dp->output_reg);
3672
3673 /*
3674 * HW workaround for IBX, we need to move the port
3675 * to transcoder A after disabling it to allow the
3676 * matching HDMI port to be enabled on transcoder A.
3677 */
6e266956 3678 if (HAS_PCH_IBX(dev_priv) && crtc->pipe == PIPE_B && port != PORT_A) {
0c241d5b
VS
3679 /*
3680 * We get CPU/PCH FIFO underruns on the other pipe when
3681 * doing the workaround. Sweep them under the rug.
3682 */
3683 intel_set_cpu_fifo_underrun_reporting(dev_priv, PIPE_A, false);
3684 intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
3685
1612c8bd
VS
3686 /* always enable with pattern 1 (as per spec) */
3687 DP &= ~(DP_PIPEB_SELECT | DP_LINK_TRAIN_MASK);
3688 DP |= DP_PORT_EN | DP_LINK_TRAIN_PAT_1;
3689 I915_WRITE(intel_dp->output_reg, DP);
3690 POSTING_READ(intel_dp->output_reg);
3691
3692 DP &= ~DP_PORT_EN;
5bddd17f 3693 I915_WRITE(intel_dp->output_reg, DP);
0ca09685 3694 POSTING_READ(intel_dp->output_reg);
0c241d5b 3695
0f0f74bc 3696 intel_wait_for_vblank_if_active(dev_priv, PIPE_A);
0c241d5b
VS
3697 intel_set_cpu_fifo_underrun_reporting(dev_priv, PIPE_A, true);
3698 intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
5bddd17f
EA
3699 }
3700
f01eca2e 3701 msleep(intel_dp->panel_power_down_delay);
6fec7662
VS
3702
3703 intel_dp->DP = DP;
9f2bdb00
VS
3704
3705 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3706 pps_lock(intel_dp);
3707 intel_dp->active_pipe = INVALID_PIPE;
3708 pps_unlock(intel_dp);
3709 }
a4fc5ed6
KP
3710}
3711
24e807e7 3712bool
fe5a66f9 3713intel_dp_read_dpcd(struct intel_dp *intel_dp)
92fd8fd1 3714{
9f085ebb
L
3715 if (drm_dp_dpcd_read(&intel_dp->aux, 0x000, intel_dp->dpcd,
3716 sizeof(intel_dp->dpcd)) < 0)
edb39244 3717 return false; /* aux transfer failed */
92fd8fd1 3718
a8e98153 3719 DRM_DEBUG_KMS("DPCD: %*ph\n", (int) sizeof(intel_dp->dpcd), intel_dp->dpcd);
577c7a50 3720
fe5a66f9
VS
3721 return intel_dp->dpcd[DP_DPCD_REV] != 0;
3722}
edb39244 3723
fe5a66f9
VS
3724static bool
3725intel_edp_init_dpcd(struct intel_dp *intel_dp)
3726{
3727 struct drm_i915_private *dev_priv =
3728 to_i915(dp_to_dig_port(intel_dp)->base.base.dev);
30d9aa42 3729
fe5a66f9
VS
3730 /* this function is meant to be called only once */
3731 WARN_ON(intel_dp->dpcd[DP_DPCD_REV] != 0);
30d9aa42 3732
fe5a66f9 3733 if (!intel_dp_read_dpcd(intel_dp))
30d9aa42
SS
3734 return false;
3735
84c36753
JN
3736 drm_dp_read_desc(&intel_dp->aux, &intel_dp->desc,
3737 drm_dp_is_branch(intel_dp->dpcd));
12a47a42 3738
fe5a66f9
VS
3739 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11)
3740 dev_priv->no_aux_handshake = intel_dp->dpcd[DP_MAX_DOWNSPREAD] &
3741 DP_NO_AUX_HANDSHAKE_LINK_TRAINING;
474d1ec4 3742
77fe36ff 3743 intel_psr_init_dpcd(intel_dp);
50003939 3744
7c838e2a
JN
3745 /*
3746 * Read the eDP display control registers.
3747 *
3748 * Do this independent of DP_DPCD_DISPLAY_CONTROL_CAPABLE bit in
3749 * DP_EDP_CONFIGURATION_CAP, because some buggy displays do not have it
3750 * set, but require eDP 1.4+ detection (e.g. for supported link rates
3751 * method). The display control registers should read zero if they're
3752 * not supported anyway.
3753 */
3754 if (drm_dp_dpcd_read(&intel_dp->aux, DP_EDP_DPCD_REV,
f7170e2e
DC
3755 intel_dp->edp_dpcd, sizeof(intel_dp->edp_dpcd)) ==
3756 sizeof(intel_dp->edp_dpcd))
e6ed2a1b 3757 DRM_DEBUG_KMS("eDP DPCD: %*ph\n", (int) sizeof(intel_dp->edp_dpcd),
fe5a66f9 3758 intel_dp->edp_dpcd);
06ea66b6 3759
e6ed2a1b
JN
3760 /* Read the eDP 1.4+ supported link rates. */
3761 if (intel_dp->edp_dpcd[0] >= DP_EDP_14) {
94ca719e 3762 __le16 sink_rates[DP_MAX_SUPPORTED_RATES];
ea2d8a42
VS
3763 int i;
3764
9f085ebb
L
3765 drm_dp_dpcd_read(&intel_dp->aux, DP_SUPPORTED_LINK_RATES,
3766 sink_rates, sizeof(sink_rates));
ea2d8a42 3767
94ca719e
VS
3768 for (i = 0; i < ARRAY_SIZE(sink_rates); i++) {
3769 int val = le16_to_cpu(sink_rates[i]);
ea2d8a42
VS
3770
3771 if (val == 0)
3772 break;
3773
fd81c44e
DP
3774 /* Value read multiplied by 200kHz gives the per-lane
3775 * link rate in kHz. The source rates are, however,
3776 * stored in terms of LS_Clk kHz. The full conversion
3777 * back to symbols is
3778 * (val * 200kHz)*(8/10 ch. encoding)*(1/8 bit to Byte)
3779 */
af77b974 3780 intel_dp->sink_rates[i] = (val * 200) / 10;
ea2d8a42 3781 }
94ca719e 3782 intel_dp->num_sink_rates = i;
fc0f8e25 3783 }
0336400e 3784
e6ed2a1b
JN
3785 /*
3786 * Use DP_LINK_RATE_SET if DP_SUPPORTED_LINK_RATES are available,
3787 * default to DP_MAX_LINK_RATE and DP_LINK_BW_SET otherwise.
3788 */
68f357cb
JN
3789 if (intel_dp->num_sink_rates)
3790 intel_dp->use_rate_select = true;
3791 else
3792 intel_dp_set_sink_rates(intel_dp);
3793
975ee5fc
JN
3794 intel_dp_set_common_rates(intel_dp);
3795
fe5a66f9
VS
3796 return true;
3797}
3798
3799
3800static bool
3801intel_dp_get_dpcd(struct intel_dp *intel_dp)
3802{
27dbefb9
JN
3803 u8 sink_count;
3804
fe5a66f9
VS
3805 if (!intel_dp_read_dpcd(intel_dp))
3806 return false;
3807
68f357cb 3808 /* Don't clobber cached eDP rates. */
1853a9da 3809 if (!intel_dp_is_edp(intel_dp)) {
68f357cb 3810 intel_dp_set_sink_rates(intel_dp);
975ee5fc
JN
3811 intel_dp_set_common_rates(intel_dp);
3812 }
68f357cb 3813
27dbefb9 3814 if (drm_dp_dpcd_readb(&intel_dp->aux, DP_SINK_COUNT, &sink_count) <= 0)
fe5a66f9
VS
3815 return false;
3816
3817 /*
3818 * Sink count can change between short pulse hpd hence
3819 * a member variable in intel_dp will track any changes
3820 * between short pulse interrupts.
3821 */
27dbefb9 3822 intel_dp->sink_count = DP_GET_SINK_COUNT(sink_count);
fe5a66f9
VS
3823
3824 /*
3825 * SINK_COUNT == 0 and DOWNSTREAM_PORT_PRESENT == 1 implies that
3826 * a dongle is present but no display. Unless we require to know
3827 * if a dongle is present or not, we don't need to update
3828 * downstream port information. So, an early return here saves
3829 * time from performing other operations which are not required.
3830 */
1853a9da 3831 if (!intel_dp_is_edp(intel_dp) && !intel_dp->sink_count)
fe5a66f9 3832 return false;
0336400e 3833
c726ad01 3834 if (!drm_dp_is_branch(intel_dp->dpcd))
edb39244
AJ
3835 return true; /* native DP sink */
3836
3837 if (intel_dp->dpcd[DP_DPCD_REV] == 0x10)
3838 return true; /* no per-port downstream info */
3839
9f085ebb
L
3840 if (drm_dp_dpcd_read(&intel_dp->aux, DP_DOWNSTREAM_PORT_0,
3841 intel_dp->downstream_ports,
3842 DP_MAX_DOWNSTREAM_PORTS) < 0)
edb39244
AJ
3843 return false; /* downstream port status fetch failed */
3844
3845 return true;
92fd8fd1
KP
3846}
3847
0e32b39c 3848static bool
c4e3170a 3849intel_dp_can_mst(struct intel_dp *intel_dp)
0e32b39c 3850{
010b9b39 3851 u8 mstm_cap;
0e32b39c 3852
4f044a88 3853 if (!i915_modparams.enable_dp_mst)
7cc96139
NS
3854 return false;
3855
0e32b39c
DA
3856 if (!intel_dp->can_mst)
3857 return false;
3858
3859 if (intel_dp->dpcd[DP_DPCD_REV] < 0x12)
3860 return false;
3861
010b9b39 3862 if (drm_dp_dpcd_readb(&intel_dp->aux, DP_MSTM_CAP, &mstm_cap) != 1)
c4e3170a 3863 return false;
0e32b39c 3864
010b9b39 3865 return mstm_cap & DP_MST_CAP;
c4e3170a
VS
3866}
3867
3868static void
3869intel_dp_configure_mst(struct intel_dp *intel_dp)
3870{
4f044a88 3871 if (!i915_modparams.enable_dp_mst)
c4e3170a
VS
3872 return;
3873
3874 if (!intel_dp->can_mst)
3875 return;
3876
3877 intel_dp->is_mst = intel_dp_can_mst(intel_dp);
3878
3879 if (intel_dp->is_mst)
3880 DRM_DEBUG_KMS("Sink is MST capable\n");
3881 else
3882 DRM_DEBUG_KMS("Sink is not MST capable\n");
3883
3884 drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr,
3885 intel_dp->is_mst);
0e32b39c
DA
3886}
3887
93313538
ML
3888static int intel_dp_sink_crc_stop(struct intel_dp *intel_dp,
3889 struct intel_crtc_state *crtc_state, bool disable_wa)
d2e216d0 3890{
082dcc7c 3891 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
0f0f74bc 3892 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
93313538 3893 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
ad9dc91b 3894 u8 buf;
e5a1cab5 3895 int ret = 0;
c6297843
RV
3896 int count = 0;
3897 int attempts = 10;
d2e216d0 3898
082dcc7c
RV
3899 if (drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_SINK, &buf) < 0) {
3900 DRM_DEBUG_KMS("Sink CRC couldn't be stopped properly\n");
e5a1cab5
RV
3901 ret = -EIO;
3902 goto out;
4373f0f2
PZ
3903 }
3904
082dcc7c 3905 if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_SINK,
e5a1cab5 3906 buf & ~DP_TEST_SINK_START) < 0) {
082dcc7c 3907 DRM_DEBUG_KMS("Sink CRC couldn't be stopped properly\n");
e5a1cab5
RV
3908 ret = -EIO;
3909 goto out;
3910 }
d2e216d0 3911
c6297843 3912 do {
0f0f74bc 3913 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
c6297843
RV
3914
3915 if (drm_dp_dpcd_readb(&intel_dp->aux,
3916 DP_TEST_SINK_MISC, &buf) < 0) {
3917 ret = -EIO;
3918 goto out;
3919 }
3920 count = buf & DP_TEST_COUNT_MASK;
3921 } while (--attempts && count);
3922
3923 if (attempts == 0) {
dc5a9037 3924 DRM_DEBUG_KMS("TIMEOUT: Sink CRC counter is not zeroed after calculation is stopped\n");
c6297843
RV
3925 ret = -ETIMEDOUT;
3926 }
3927
e5a1cab5 3928 out:
93313538 3929 if (disable_wa)
199ea381 3930 hsw_enable_ips(crtc_state);
e5a1cab5 3931 return ret;
082dcc7c
RV
3932}
3933
93313538
ML
3934static int intel_dp_sink_crc_start(struct intel_dp *intel_dp,
3935 struct intel_crtc_state *crtc_state)
082dcc7c
RV
3936{
3937 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
0f0f74bc 3938 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
93313538 3939 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
082dcc7c 3940 u8 buf;
e5a1cab5
RV
3941 int ret;
3942
082dcc7c
RV
3943 if (drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_SINK_MISC, &buf) < 0)
3944 return -EIO;
3945
3946 if (!(buf & DP_TEST_CRC_SUPPORTED))
3947 return -ENOTTY;
3948
3949 if (drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_SINK, &buf) < 0)
3950 return -EIO;
3951
6d8175da 3952 if (buf & DP_TEST_SINK_START) {
93313538 3953 ret = intel_dp_sink_crc_stop(intel_dp, crtc_state, false);
6d8175da
RV
3954 if (ret)
3955 return ret;
3956 }
3957
199ea381 3958 hsw_disable_ips(crtc_state);
1dda5f93 3959
9d1a1031 3960 if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_SINK,
082dcc7c 3961 buf | DP_TEST_SINK_START) < 0) {
199ea381 3962 hsw_enable_ips(crtc_state);
082dcc7c 3963 return -EIO;
4373f0f2
PZ
3964 }
3965
0f0f74bc 3966 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
082dcc7c
RV
3967 return 0;
3968}
3969
93313538 3970int intel_dp_sink_crc(struct intel_dp *intel_dp, struct intel_crtc_state *crtc_state, u8 *crc)
082dcc7c
RV
3971{
3972 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
0f0f74bc 3973 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
93313538 3974 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
082dcc7c 3975 u8 buf;
621d4c76 3976 int count, ret;
082dcc7c 3977 int attempts = 6;
082dcc7c 3978
93313538 3979 ret = intel_dp_sink_crc_start(intel_dp, crtc_state);
082dcc7c
RV
3980 if (ret)
3981 return ret;
3982
ad9dc91b 3983 do {
0f0f74bc 3984 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
621d4c76 3985
1dda5f93 3986 if (drm_dp_dpcd_readb(&intel_dp->aux,
4373f0f2
PZ
3987 DP_TEST_SINK_MISC, &buf) < 0) {
3988 ret = -EIO;
afe0d67e 3989 goto stop;
4373f0f2 3990 }
621d4c76 3991 count = buf & DP_TEST_COUNT_MASK;
aabc95dc 3992
7e38eeff 3993 } while (--attempts && count == 0);
ad9dc91b
RV
3994
3995 if (attempts == 0) {
7e38eeff
RV
3996 DRM_ERROR("Panel is unable to calculate any CRC after 6 vblanks\n");
3997 ret = -ETIMEDOUT;
3998 goto stop;
3999 }
4000
4001 if (drm_dp_dpcd_read(&intel_dp->aux, DP_TEST_CRC_R_CR, crc, 6) < 0) {
4002 ret = -EIO;
4003 goto stop;
ad9dc91b 4004 }
d2e216d0 4005
afe0d67e 4006stop:
93313538 4007 intel_dp_sink_crc_stop(intel_dp, crtc_state, true);
4373f0f2 4008 return ret;
d2e216d0
RV
4009}
4010
a60f0e38
JB
4011static bool
4012intel_dp_get_sink_irq(struct intel_dp *intel_dp, u8 *sink_irq_vector)
4013{
010b9b39
JN
4014 return drm_dp_dpcd_readb(&intel_dp->aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
4015 sink_irq_vector) == 1;
a60f0e38
JB
4016}
4017
0e32b39c
DA
4018static bool
4019intel_dp_get_sink_irq_esi(struct intel_dp *intel_dp, u8 *sink_irq_vector)
4020{
e8b2577c
PD
4021 return drm_dp_dpcd_read(&intel_dp->aux, DP_SINK_COUNT_ESI,
4022 sink_irq_vector, DP_DPRX_ESI_LEN) ==
4023 DP_DPRX_ESI_LEN;
0e32b39c
DA
4024}
4025
c5d5ab7a
TP
4026static uint8_t intel_dp_autotest_link_training(struct intel_dp *intel_dp)
4027{
da15f7cb 4028 int status = 0;
140ef138 4029 int test_link_rate;
da15f7cb
MN
4030 uint8_t test_lane_count, test_link_bw;
4031 /* (DP CTS 1.2)
4032 * 4.3.1.11
4033 */
4034 /* Read the TEST_LANE_COUNT and TEST_LINK_RTAE fields (DP CTS 3.1.4) */
4035 status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_LANE_COUNT,
4036 &test_lane_count);
4037
4038 if (status <= 0) {
4039 DRM_DEBUG_KMS("Lane count read failed\n");
4040 return DP_TEST_NAK;
4041 }
4042 test_lane_count &= DP_MAX_LANE_COUNT_MASK;
da15f7cb
MN
4043
4044 status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_LINK_RATE,
4045 &test_link_bw);
4046 if (status <= 0) {
4047 DRM_DEBUG_KMS("Link Rate read failed\n");
4048 return DP_TEST_NAK;
4049 }
da15f7cb 4050 test_link_rate = drm_dp_bw_code_to_link_rate(test_link_bw);
140ef138
MN
4051
4052 /* Validate the requested link rate and lane count */
4053 if (!intel_dp_link_params_valid(intel_dp, test_link_rate,
4054 test_lane_count))
da15f7cb
MN
4055 return DP_TEST_NAK;
4056
4057 intel_dp->compliance.test_lane_count = test_lane_count;
4058 intel_dp->compliance.test_link_rate = test_link_rate;
4059
4060 return DP_TEST_ACK;
c5d5ab7a
TP
4061}
4062
4063static uint8_t intel_dp_autotest_video_pattern(struct intel_dp *intel_dp)
4064{
611032bf 4065 uint8_t test_pattern;
010b9b39 4066 uint8_t test_misc;
611032bf
MN
4067 __be16 h_width, v_height;
4068 int status = 0;
4069
4070 /* Read the TEST_PATTERN (DP CTS 3.1.5) */
010b9b39
JN
4071 status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_PATTERN,
4072 &test_pattern);
611032bf
MN
4073 if (status <= 0) {
4074 DRM_DEBUG_KMS("Test pattern read failed\n");
4075 return DP_TEST_NAK;
4076 }
4077 if (test_pattern != DP_COLOR_RAMP)
4078 return DP_TEST_NAK;
4079
4080 status = drm_dp_dpcd_read(&intel_dp->aux, DP_TEST_H_WIDTH_HI,
4081 &h_width, 2);
4082 if (status <= 0) {
4083 DRM_DEBUG_KMS("H Width read failed\n");
4084 return DP_TEST_NAK;
4085 }
4086
4087 status = drm_dp_dpcd_read(&intel_dp->aux, DP_TEST_V_HEIGHT_HI,
4088 &v_height, 2);
4089 if (status <= 0) {
4090 DRM_DEBUG_KMS("V Height read failed\n");
4091 return DP_TEST_NAK;
4092 }
4093
010b9b39
JN
4094 status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_MISC0,
4095 &test_misc);
611032bf
MN
4096 if (status <= 0) {
4097 DRM_DEBUG_KMS("TEST MISC read failed\n");
4098 return DP_TEST_NAK;
4099 }
4100 if ((test_misc & DP_TEST_COLOR_FORMAT_MASK) != DP_COLOR_FORMAT_RGB)
4101 return DP_TEST_NAK;
4102 if (test_misc & DP_TEST_DYNAMIC_RANGE_CEA)
4103 return DP_TEST_NAK;
4104 switch (test_misc & DP_TEST_BIT_DEPTH_MASK) {
4105 case DP_TEST_BIT_DEPTH_6:
4106 intel_dp->compliance.test_data.bpc = 6;
4107 break;
4108 case DP_TEST_BIT_DEPTH_8:
4109 intel_dp->compliance.test_data.bpc = 8;
4110 break;
4111 default:
4112 return DP_TEST_NAK;
4113 }
4114
4115 intel_dp->compliance.test_data.video_pattern = test_pattern;
4116 intel_dp->compliance.test_data.hdisplay = be16_to_cpu(h_width);
4117 intel_dp->compliance.test_data.vdisplay = be16_to_cpu(v_height);
4118 /* Set test active flag here so userspace doesn't interrupt things */
4119 intel_dp->compliance.test_active = 1;
4120
4121 return DP_TEST_ACK;
c5d5ab7a
TP
4122}
4123
4124static uint8_t intel_dp_autotest_edid(struct intel_dp *intel_dp)
a60f0e38 4125{
b48a5ba9 4126 uint8_t test_result = DP_TEST_ACK;
559be30c
TP
4127 struct intel_connector *intel_connector = intel_dp->attached_connector;
4128 struct drm_connector *connector = &intel_connector->base;
4129
4130 if (intel_connector->detect_edid == NULL ||
ac6f2e29 4131 connector->edid_corrupt ||
559be30c
TP
4132 intel_dp->aux.i2c_defer_count > 6) {
4133 /* Check EDID read for NACKs, DEFERs and corruption
4134 * (DP CTS 1.2 Core r1.1)
4135 * 4.2.2.4 : Failed EDID read, I2C_NAK
4136 * 4.2.2.5 : Failed EDID read, I2C_DEFER
4137 * 4.2.2.6 : EDID corruption detected
4138 * Use failsafe mode for all cases
4139 */
4140 if (intel_dp->aux.i2c_nack_count > 0 ||
4141 intel_dp->aux.i2c_defer_count > 0)
4142 DRM_DEBUG_KMS("EDID read had %d NACKs, %d DEFERs\n",
4143 intel_dp->aux.i2c_nack_count,
4144 intel_dp->aux.i2c_defer_count);
c1617abc 4145 intel_dp->compliance.test_data.edid = INTEL_DP_RESOLUTION_FAILSAFE;
559be30c 4146 } else {
f79b468e
TS
4147 struct edid *block = intel_connector->detect_edid;
4148
4149 /* We have to write the checksum
4150 * of the last block read
4151 */
4152 block += intel_connector->detect_edid->extensions;
4153
010b9b39
JN
4154 if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_EDID_CHECKSUM,
4155 block->checksum) <= 0)
559be30c
TP
4156 DRM_DEBUG_KMS("Failed to write EDID checksum\n");
4157
4158 test_result = DP_TEST_ACK | DP_TEST_EDID_CHECKSUM_WRITE;
b48a5ba9 4159 intel_dp->compliance.test_data.edid = INTEL_DP_RESOLUTION_PREFERRED;
559be30c
TP
4160 }
4161
4162 /* Set test active flag here so userspace doesn't interrupt things */
c1617abc 4163 intel_dp->compliance.test_active = 1;
559be30c 4164
c5d5ab7a
TP
4165 return test_result;
4166}
4167
4168static uint8_t intel_dp_autotest_phy_pattern(struct intel_dp *intel_dp)
a60f0e38 4169{
c5d5ab7a
TP
4170 uint8_t test_result = DP_TEST_NAK;
4171 return test_result;
4172}
4173
4174static void intel_dp_handle_test_request(struct intel_dp *intel_dp)
4175{
4176 uint8_t response = DP_TEST_NAK;
5ec63bbd
JN
4177 uint8_t request = 0;
4178 int status;
c5d5ab7a 4179
5ec63bbd 4180 status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_REQUEST, &request);
c5d5ab7a
TP
4181 if (status <= 0) {
4182 DRM_DEBUG_KMS("Could not read test request from sink\n");
4183 goto update_status;
4184 }
4185
5ec63bbd 4186 switch (request) {
c5d5ab7a
TP
4187 case DP_TEST_LINK_TRAINING:
4188 DRM_DEBUG_KMS("LINK_TRAINING test requested\n");
c5d5ab7a
TP
4189 response = intel_dp_autotest_link_training(intel_dp);
4190 break;
4191 case DP_TEST_LINK_VIDEO_PATTERN:
4192 DRM_DEBUG_KMS("TEST_PATTERN test requested\n");
c5d5ab7a
TP
4193 response = intel_dp_autotest_video_pattern(intel_dp);
4194 break;
4195 case DP_TEST_LINK_EDID_READ:
4196 DRM_DEBUG_KMS("EDID test requested\n");
c5d5ab7a
TP
4197 response = intel_dp_autotest_edid(intel_dp);
4198 break;
4199 case DP_TEST_LINK_PHY_TEST_PATTERN:
4200 DRM_DEBUG_KMS("PHY_PATTERN test requested\n");
c5d5ab7a
TP
4201 response = intel_dp_autotest_phy_pattern(intel_dp);
4202 break;
4203 default:
5ec63bbd 4204 DRM_DEBUG_KMS("Invalid test request '%02x'\n", request);
c5d5ab7a
TP
4205 break;
4206 }
4207
5ec63bbd
JN
4208 if (response & DP_TEST_ACK)
4209 intel_dp->compliance.test_type = request;
4210
c5d5ab7a 4211update_status:
5ec63bbd 4212 status = drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_RESPONSE, response);
c5d5ab7a
TP
4213 if (status <= 0)
4214 DRM_DEBUG_KMS("Could not write test response to sink\n");
a60f0e38
JB
4215}
4216
0e32b39c
DA
4217static int
4218intel_dp_check_mst_status(struct intel_dp *intel_dp)
4219{
4220 bool bret;
4221
4222 if (intel_dp->is_mst) {
e8b2577c 4223 u8 esi[DP_DPRX_ESI_LEN] = { 0 };
0e32b39c
DA
4224 int ret = 0;
4225 int retry;
4226 bool handled;
4227 bret = intel_dp_get_sink_irq_esi(intel_dp, esi);
4228go_again:
4229 if (bret == true) {
4230
4231 /* check link status - esi[10] = 0x200c */
19e0b4ca 4232 if (intel_dp->active_mst_links &&
901c2daf 4233 !drm_dp_channel_eq_ok(&esi[10], intel_dp->lane_count)) {
0e32b39c
DA
4234 DRM_DEBUG_KMS("channel EQ not ok, retraining\n");
4235 intel_dp_start_link_train(intel_dp);
0e32b39c
DA
4236 intel_dp_stop_link_train(intel_dp);
4237 }
4238
6f34cc39 4239 DRM_DEBUG_KMS("got esi %3ph\n", esi);
0e32b39c
DA
4240 ret = drm_dp_mst_hpd_irq(&intel_dp->mst_mgr, esi, &handled);
4241
4242 if (handled) {
4243 for (retry = 0; retry < 3; retry++) {
4244 int wret;
4245 wret = drm_dp_dpcd_write(&intel_dp->aux,
4246 DP_SINK_COUNT_ESI+1,
4247 &esi[1], 3);
4248 if (wret == 3) {
4249 break;
4250 }
4251 }
4252
4253 bret = intel_dp_get_sink_irq_esi(intel_dp, esi);
4254 if (bret == true) {
6f34cc39 4255 DRM_DEBUG_KMS("got esi2 %3ph\n", esi);
0e32b39c
DA
4256 goto go_again;
4257 }
4258 } else
4259 ret = 0;
4260
4261 return ret;
4262 } else {
4263 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
4264 DRM_DEBUG_KMS("failed to get ESI - device may have failed\n");
4265 intel_dp->is_mst = false;
4266 drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr, intel_dp->is_mst);
4267 /* send a hotplug event */
4268 drm_kms_helper_hotplug_event(intel_dig_port->base.base.dev);
4269 }
4270 }
4271 return -EINVAL;
4272}
4273
c85d200e
VS
4274static bool
4275intel_dp_needs_link_retrain(struct intel_dp *intel_dp)
4276{
4277 u8 link_status[DP_LINK_STATUS_SIZE];
4278
edb2e530
VS
4279 if (!intel_dp->link_trained)
4280 return false;
4281
4282 if (!intel_dp_get_link_status(intel_dp, link_status))
c85d200e 4283 return false;
c85d200e
VS
4284
4285 /*
4286 * Validate the cached values of intel_dp->link_rate and
4287 * intel_dp->lane_count before attempting to retrain.
4288 */
4289 if (!intel_dp_link_params_valid(intel_dp, intel_dp->link_rate,
4290 intel_dp->lane_count))
4291 return false;
4292
4293 /* Retrain if Channel EQ or CR not ok */
4294 return !drm_dp_channel_eq_ok(link_status, intel_dp->lane_count);
4295}
4296
4297/*
4298 * If display is now connected check links status,
4299 * there has been known issues of link loss triggering
4300 * long pulse.
4301 *
4302 * Some sinks (eg. ASUS PB287Q) seem to perform some
4303 * weird HPD ping pong during modesets. So we can apparently
4304 * end up with HPD going low during a modeset, and then
4305 * going back up soon after. And once that happens we must
4306 * retrain the link to get a picture. That's in case no
4307 * userspace component reacted to intermittent HPD dip.
4308 */
4309int intel_dp_retrain_link(struct intel_encoder *encoder,
4310 struct drm_modeset_acquire_ctx *ctx)
bfd02b3c 4311{
bfd02b3c 4312 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
c85d200e
VS
4313 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
4314 struct intel_connector *connector = intel_dp->attached_connector;
4315 struct drm_connector_state *conn_state;
4316 struct intel_crtc_state *crtc_state;
4317 struct intel_crtc *crtc;
4318 int ret;
4319
4320 /* FIXME handle the MST connectors as well */
4321
4322 if (!connector || connector->base.status != connector_status_connected)
4323 return 0;
4324
4325 ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
4326 ctx);
4327 if (ret)
4328 return ret;
4329
4330 conn_state = connector->base.state;
4331
4332 crtc = to_intel_crtc(conn_state->crtc);
4333 if (!crtc)
4334 return 0;
4335
4336 ret = drm_modeset_lock(&crtc->base.mutex, ctx);
4337 if (ret)
4338 return ret;
4339
4340 crtc_state = to_intel_crtc_state(crtc->base.state);
4341
4342 WARN_ON(!intel_crtc_has_dp_encoder(crtc_state));
4343
4344 if (!crtc_state->base.active)
4345 return 0;
4346
4347 if (conn_state->commit &&
4348 !try_wait_for_completion(&conn_state->commit->hw_done))
4349 return 0;
4350
4351 if (!intel_dp_needs_link_retrain(intel_dp))
4352 return 0;
bfd02b3c
VS
4353
4354 /* Suppress underruns caused by re-training */
4355 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
4356 if (crtc->config->has_pch_encoder)
4357 intel_set_pch_fifo_underrun_reporting(dev_priv,
4358 intel_crtc_pch_transcoder(crtc), false);
4359
4360 intel_dp_start_link_train(intel_dp);
4361 intel_dp_stop_link_train(intel_dp);
4362
4363 /* Keep underrun reporting disabled until things are stable */
0f0f74bc 4364 intel_wait_for_vblank(dev_priv, crtc->pipe);
bfd02b3c
VS
4365
4366 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
4367 if (crtc->config->has_pch_encoder)
4368 intel_set_pch_fifo_underrun_reporting(dev_priv,
4369 intel_crtc_pch_transcoder(crtc), true);
c85d200e
VS
4370
4371 return 0;
bfd02b3c
VS
4372}
4373
c85d200e
VS
4374/*
4375 * If display is now connected check links status,
4376 * there has been known issues of link loss triggering
4377 * long pulse.
4378 *
4379 * Some sinks (eg. ASUS PB287Q) seem to perform some
4380 * weird HPD ping pong during modesets. So we can apparently
4381 * end up with HPD going low during a modeset, and then
4382 * going back up soon after. And once that happens we must
4383 * retrain the link to get a picture. That's in case no
4384 * userspace component reacted to intermittent HPD dip.
4385 */
4386static bool intel_dp_hotplug(struct intel_encoder *encoder,
4387 struct intel_connector *connector)
5c9114d0 4388{
c85d200e
VS
4389 struct drm_modeset_acquire_ctx ctx;
4390 bool changed;
4391 int ret;
5c9114d0 4392
c85d200e 4393 changed = intel_encoder_hotplug(encoder, connector);
5c9114d0 4394
c85d200e 4395 drm_modeset_acquire_init(&ctx, 0);
42e5e657 4396
c85d200e
VS
4397 for (;;) {
4398 ret = intel_dp_retrain_link(encoder, &ctx);
5c9114d0 4399
c85d200e
VS
4400 if (ret == -EDEADLK) {
4401 drm_modeset_backoff(&ctx);
4402 continue;
4403 }
5c9114d0 4404
c85d200e
VS
4405 break;
4406 }
d4cb3fd9 4407
c85d200e
VS
4408 drm_modeset_drop_locks(&ctx);
4409 drm_modeset_acquire_fini(&ctx);
4410 WARN(ret, "Acquiring modeset locks failed with %i\n", ret);
bfd02b3c 4411
c85d200e 4412 return changed;
5c9114d0
SS
4413}
4414
a4fc5ed6
KP
4415/*
4416 * According to DP spec
4417 * 5.1.2:
4418 * 1. Read DPCD
4419 * 2. Configure link according to Receiver Capabilities
4420 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
4421 * 4. Check link status on receipt of hot-plug interrupt
39ff747b
SS
4422 *
4423 * intel_dp_short_pulse - handles short pulse interrupts
4424 * when full detection is not required.
4425 * Returns %true if short pulse is handled and full detection
4426 * is NOT required and %false otherwise.
a4fc5ed6 4427 */
39ff747b 4428static bool
5c9114d0 4429intel_dp_short_pulse(struct intel_dp *intel_dp)
a4fc5ed6 4430{
2f773477 4431 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
65fbb4e7 4432 u8 sink_irq_vector = 0;
39ff747b
SS
4433 u8 old_sink_count = intel_dp->sink_count;
4434 bool ret;
5b215bcf 4435
4df6960e
SS
4436 /*
4437 * Clearing compliance test variables to allow capturing
4438 * of values for next automated test request.
4439 */
c1617abc 4440 memset(&intel_dp->compliance, 0, sizeof(intel_dp->compliance));
4df6960e 4441
39ff747b
SS
4442 /*
4443 * Now read the DPCD to see if it's actually running
4444 * If the current value of sink count doesn't match with
4445 * the value that was stored earlier or dpcd read failed
4446 * we need to do full detection
4447 */
4448 ret = intel_dp_get_dpcd(intel_dp);
4449
4450 if ((old_sink_count != intel_dp->sink_count) || !ret) {
4451 /* No need to proceed if we are going to do full detect */
4452 return false;
59cd09e1
JB
4453 }
4454
a60f0e38
JB
4455 /* Try to read the source of the interrupt */
4456 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
65fbb4e7
VS
4457 intel_dp_get_sink_irq(intel_dp, &sink_irq_vector) &&
4458 sink_irq_vector != 0) {
a60f0e38 4459 /* Clear interrupt source */
9d1a1031
JN
4460 drm_dp_dpcd_writeb(&intel_dp->aux,
4461 DP_DEVICE_SERVICE_IRQ_VECTOR,
4462 sink_irq_vector);
a60f0e38
JB
4463
4464 if (sink_irq_vector & DP_AUTOMATED_TEST_REQUEST)
da15f7cb 4465 intel_dp_handle_test_request(intel_dp);
a60f0e38
JB
4466 if (sink_irq_vector & (DP_CP_IRQ | DP_SINK_SPECIFIC_IRQ))
4467 DRM_DEBUG_DRIVER("CP or sink specific irq unhandled\n");
4468 }
4469
c85d200e
VS
4470 /* defer to the hotplug work for link retraining if needed */
4471 if (intel_dp_needs_link_retrain(intel_dp))
4472 return false;
42e5e657 4473
da15f7cb
MN
4474 if (intel_dp->compliance.test_type == DP_TEST_LINK_TRAINING) {
4475 DRM_DEBUG_KMS("Link Training Compliance Test requested\n");
4476 /* Send a Hotplug Uevent to userspace to start modeset */
2f773477 4477 drm_kms_helper_hotplug_event(&dev_priv->drm);
da15f7cb 4478 }
39ff747b
SS
4479
4480 return true;
a4fc5ed6 4481}
a4fc5ed6 4482
caf9ab24 4483/* XXX this is probably wrong for multiple downstream ports */
71ba9000 4484static enum drm_connector_status
26d61aad 4485intel_dp_detect_dpcd(struct intel_dp *intel_dp)
71ba9000 4486{
e393d0d6 4487 struct intel_lspcon *lspcon = dp_to_lspcon(intel_dp);
caf9ab24 4488 uint8_t *dpcd = intel_dp->dpcd;
caf9ab24
AJ
4489 uint8_t type;
4490
e393d0d6
ID
4491 if (lspcon->active)
4492 lspcon_resume(lspcon);
4493
caf9ab24
AJ
4494 if (!intel_dp_get_dpcd(intel_dp))
4495 return connector_status_disconnected;
4496
1853a9da 4497 if (intel_dp_is_edp(intel_dp))
1034ce70
SS
4498 return connector_status_connected;
4499
caf9ab24 4500 /* if there's no downstream port, we're done */
c726ad01 4501 if (!drm_dp_is_branch(dpcd))
26d61aad 4502 return connector_status_connected;
caf9ab24
AJ
4503
4504 /* If we're HPD-aware, SINK_COUNT changes dynamically */
c9ff160b
JN
4505 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
4506 intel_dp->downstream_ports[0] & DP_DS_PORT_HPD) {
9d1a1031 4507
30d9aa42
SS
4508 return intel_dp->sink_count ?
4509 connector_status_connected : connector_status_disconnected;
caf9ab24
AJ
4510 }
4511
c4e3170a
VS
4512 if (intel_dp_can_mst(intel_dp))
4513 return connector_status_connected;
4514
caf9ab24 4515 /* If no HPD, poke DDC gently */
0b99836f 4516 if (drm_probe_ddc(&intel_dp->aux.ddc))
26d61aad 4517 return connector_status_connected;
caf9ab24
AJ
4518
4519 /* Well we tried, say unknown for unreliable port types */
c9ff160b
JN
4520 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11) {
4521 type = intel_dp->downstream_ports[0] & DP_DS_PORT_TYPE_MASK;
4522 if (type == DP_DS_PORT_TYPE_VGA ||
4523 type == DP_DS_PORT_TYPE_NON_EDID)
4524 return connector_status_unknown;
4525 } else {
4526 type = intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
4527 DP_DWN_STRM_PORT_TYPE_MASK;
4528 if (type == DP_DWN_STRM_PORT_TYPE_ANALOG ||
4529 type == DP_DWN_STRM_PORT_TYPE_OTHER)
4530 return connector_status_unknown;
4531 }
caf9ab24
AJ
4532
4533 /* Anything else is out of spec, warn and ignore */
4534 DRM_DEBUG_KMS("Broken DP branch device, ignoring\n");
26d61aad 4535 return connector_status_disconnected;
71ba9000
AJ
4536}
4537
d410b56d
CW
4538static enum drm_connector_status
4539edp_detect(struct intel_dp *intel_dp)
4540{
2f773477 4541 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
d410b56d
CW
4542 enum drm_connector_status status;
4543
1650be74 4544 status = intel_panel_detect(dev_priv);
d410b56d
CW
4545 if (status == connector_status_unknown)
4546 status = connector_status_connected;
4547
4548 return status;
4549}
4550
7533eb4f 4551static bool ibx_digital_port_connected(struct intel_encoder *encoder)
5eb08b69 4552{
7533eb4f 4553 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
b93433cc 4554 u32 bit;
01cb9ea6 4555
7533eb4f
RV
4556 switch (encoder->hpd_pin) {
4557 case HPD_PORT_B:
0df53b77
JN
4558 bit = SDE_PORTB_HOTPLUG;
4559 break;
7533eb4f 4560 case HPD_PORT_C:
0df53b77
JN
4561 bit = SDE_PORTC_HOTPLUG;
4562 break;
7533eb4f 4563 case HPD_PORT_D:
0df53b77
JN
4564 bit = SDE_PORTD_HOTPLUG;
4565 break;
4566 default:
7533eb4f 4567 MISSING_CASE(encoder->hpd_pin);
0df53b77
JN
4568 return false;
4569 }
4570
4571 return I915_READ(SDEISR) & bit;
4572}
4573
7533eb4f 4574static bool cpt_digital_port_connected(struct intel_encoder *encoder)
0df53b77 4575{
7533eb4f 4576 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
0df53b77
JN
4577 u32 bit;
4578
7533eb4f
RV
4579 switch (encoder->hpd_pin) {
4580 case HPD_PORT_B:
0df53b77
JN
4581 bit = SDE_PORTB_HOTPLUG_CPT;
4582 break;
7533eb4f 4583 case HPD_PORT_C:
0df53b77
JN
4584 bit = SDE_PORTC_HOTPLUG_CPT;
4585 break;
7533eb4f 4586 case HPD_PORT_D:
0df53b77
JN
4587 bit = SDE_PORTD_HOTPLUG_CPT;
4588 break;
93e5f0b6 4589 default:
7533eb4f 4590 MISSING_CASE(encoder->hpd_pin);
93e5f0b6
VS
4591 return false;
4592 }
4593
4594 return I915_READ(SDEISR) & bit;
4595}
4596
7533eb4f 4597static bool spt_digital_port_connected(struct intel_encoder *encoder)
93e5f0b6 4598{
7533eb4f 4599 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
93e5f0b6
VS
4600 u32 bit;
4601
7533eb4f
RV
4602 switch (encoder->hpd_pin) {
4603 case HPD_PORT_A:
93e5f0b6
VS
4604 bit = SDE_PORTA_HOTPLUG_SPT;
4605 break;
7533eb4f 4606 case HPD_PORT_E:
a78695d3
JN
4607 bit = SDE_PORTE_HOTPLUG_SPT;
4608 break;
0df53b77 4609 default:
7533eb4f 4610 return cpt_digital_port_connected(encoder);
b93433cc 4611 }
1b469639 4612
b93433cc 4613 return I915_READ(SDEISR) & bit;
5eb08b69
ZW
4614}
4615
7533eb4f 4616static bool g4x_digital_port_connected(struct intel_encoder *encoder)
a4fc5ed6 4617{
7533eb4f 4618 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
9642c81c 4619 u32 bit;
5eb08b69 4620
7533eb4f
RV
4621 switch (encoder->hpd_pin) {
4622 case HPD_PORT_B:
9642c81c
JN
4623 bit = PORTB_HOTPLUG_LIVE_STATUS_G4X;
4624 break;
7533eb4f 4625 case HPD_PORT_C:
9642c81c
JN
4626 bit = PORTC_HOTPLUG_LIVE_STATUS_G4X;
4627 break;
7533eb4f 4628 case HPD_PORT_D:
9642c81c
JN
4629 bit = PORTD_HOTPLUG_LIVE_STATUS_G4X;
4630 break;
4631 default:
7533eb4f 4632 MISSING_CASE(encoder->hpd_pin);
9642c81c
JN
4633 return false;
4634 }
4635
4636 return I915_READ(PORT_HOTPLUG_STAT) & bit;
4637}
4638
7533eb4f 4639static bool gm45_digital_port_connected(struct intel_encoder *encoder)
9642c81c 4640{
7533eb4f 4641 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
9642c81c
JN
4642 u32 bit;
4643
7533eb4f
RV
4644 switch (encoder->hpd_pin) {
4645 case HPD_PORT_B:
0780cd36 4646 bit = PORTB_HOTPLUG_LIVE_STATUS_GM45;
9642c81c 4647 break;
7533eb4f 4648 case HPD_PORT_C:
0780cd36 4649 bit = PORTC_HOTPLUG_LIVE_STATUS_GM45;
9642c81c 4650 break;
7533eb4f 4651 case HPD_PORT_D:
0780cd36 4652 bit = PORTD_HOTPLUG_LIVE_STATUS_GM45;
9642c81c
JN
4653 break;
4654 default:
7533eb4f 4655 MISSING_CASE(encoder->hpd_pin);
9642c81c 4656 return false;
a4fc5ed6
KP
4657 }
4658
1d245987 4659 return I915_READ(PORT_HOTPLUG_STAT) & bit;
2a592bec
DA
4660}
4661
7533eb4f 4662static bool ilk_digital_port_connected(struct intel_encoder *encoder)
93e5f0b6 4663{
7533eb4f
RV
4664 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4665
4666 if (encoder->hpd_pin == HPD_PORT_A)
93e5f0b6
VS
4667 return I915_READ(DEISR) & DE_DP_A_HOTPLUG;
4668 else
7533eb4f 4669 return ibx_digital_port_connected(encoder);
93e5f0b6
VS
4670}
4671
7533eb4f 4672static bool snb_digital_port_connected(struct intel_encoder *encoder)
93e5f0b6 4673{
7533eb4f
RV
4674 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4675
4676 if (encoder->hpd_pin == HPD_PORT_A)
93e5f0b6
VS
4677 return I915_READ(DEISR) & DE_DP_A_HOTPLUG;
4678 else
7533eb4f 4679 return cpt_digital_port_connected(encoder);
93e5f0b6
VS
4680}
4681
7533eb4f 4682static bool ivb_digital_port_connected(struct intel_encoder *encoder)
93e5f0b6 4683{
7533eb4f
RV
4684 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4685
4686 if (encoder->hpd_pin == HPD_PORT_A)
93e5f0b6
VS
4687 return I915_READ(DEISR) & DE_DP_A_HOTPLUG_IVB;
4688 else
7533eb4f 4689 return cpt_digital_port_connected(encoder);
93e5f0b6
VS
4690}
4691
7533eb4f 4692static bool bdw_digital_port_connected(struct intel_encoder *encoder)
93e5f0b6 4693{
7533eb4f
RV
4694 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4695
4696 if (encoder->hpd_pin == HPD_PORT_A)
93e5f0b6
VS
4697 return I915_READ(GEN8_DE_PORT_ISR) & GEN8_PORT_DP_A_HOTPLUG;
4698 else
7533eb4f 4699 return cpt_digital_port_connected(encoder);
93e5f0b6
VS
4700}
4701
7533eb4f 4702static bool bxt_digital_port_connected(struct intel_encoder *encoder)
e464bfde 4703{
7533eb4f 4704 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
e464bfde
JN
4705 u32 bit;
4706
7533eb4f
RV
4707 switch (encoder->hpd_pin) {
4708 case HPD_PORT_A:
e464bfde
JN
4709 bit = BXT_DE_PORT_HP_DDIA;
4710 break;
7533eb4f 4711 case HPD_PORT_B:
e464bfde
JN
4712 bit = BXT_DE_PORT_HP_DDIB;
4713 break;
7533eb4f 4714 case HPD_PORT_C:
e464bfde
JN
4715 bit = BXT_DE_PORT_HP_DDIC;
4716 break;
4717 default:
7533eb4f 4718 MISSING_CASE(encoder->hpd_pin);
e464bfde
JN
4719 return false;
4720 }
4721
4722 return I915_READ(GEN8_DE_PORT_ISR) & bit;
4723}
4724
7e66bcf2
JN
4725/*
4726 * intel_digital_port_connected - is the specified port connected?
7533eb4f 4727 * @encoder: intel_encoder
7e66bcf2 4728 *
7533eb4f 4729 * Return %true if port is connected, %false otherwise.
7e66bcf2 4730 */
7533eb4f 4731bool intel_digital_port_connected(struct intel_encoder *encoder)
7e66bcf2 4732{
7533eb4f
RV
4733 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4734
93e5f0b6
VS
4735 if (HAS_GMCH_DISPLAY(dev_priv)) {
4736 if (IS_GM45(dev_priv))
7533eb4f 4737 return gm45_digital_port_connected(encoder);
93e5f0b6 4738 else
7533eb4f 4739 return g4x_digital_port_connected(encoder);
93e5f0b6
VS
4740 }
4741
4742 if (IS_GEN5(dev_priv))
7533eb4f 4743 return ilk_digital_port_connected(encoder);
93e5f0b6 4744 else if (IS_GEN6(dev_priv))
7533eb4f 4745 return snb_digital_port_connected(encoder);
93e5f0b6 4746 else if (IS_GEN7(dev_priv))
7533eb4f 4747 return ivb_digital_port_connected(encoder);
93e5f0b6 4748 else if (IS_GEN8(dev_priv))
7533eb4f 4749 return bdw_digital_port_connected(encoder);
cc3f90f0 4750 else if (IS_GEN9_LP(dev_priv))
7533eb4f 4751 return bxt_digital_port_connected(encoder);
7e66bcf2 4752 else
7533eb4f 4753 return spt_digital_port_connected(encoder);
7e66bcf2
JN
4754}
4755
8c241fef 4756static struct edid *
beb60608 4757intel_dp_get_edid(struct intel_dp *intel_dp)
8c241fef 4758{
beb60608 4759 struct intel_connector *intel_connector = intel_dp->attached_connector;
d6f24d0f 4760
9cd300e0
JN
4761 /* use cached edid if we have one */
4762 if (intel_connector->edid) {
9cd300e0
JN
4763 /* invalid edid */
4764 if (IS_ERR(intel_connector->edid))
d6f24d0f
JB
4765 return NULL;
4766
55e9edeb 4767 return drm_edid_duplicate(intel_connector->edid);
beb60608
CW
4768 } else
4769 return drm_get_edid(&intel_connector->base,
4770 &intel_dp->aux.ddc);
4771}
8c241fef 4772
beb60608
CW
4773static void
4774intel_dp_set_edid(struct intel_dp *intel_dp)
4775{
4776 struct intel_connector *intel_connector = intel_dp->attached_connector;
4777 struct edid *edid;
8c241fef 4778
f21a2198 4779 intel_dp_unset_edid(intel_dp);
beb60608
CW
4780 edid = intel_dp_get_edid(intel_dp);
4781 intel_connector->detect_edid = edid;
4782
e6b72c94 4783 intel_dp->has_audio = drm_detect_monitor_audio(edid);
8c241fef
KP
4784}
4785
beb60608
CW
4786static void
4787intel_dp_unset_edid(struct intel_dp *intel_dp)
8c241fef 4788{
beb60608 4789 struct intel_connector *intel_connector = intel_dp->attached_connector;
8c241fef 4790
beb60608
CW
4791 kfree(intel_connector->detect_edid);
4792 intel_connector->detect_edid = NULL;
9cd300e0 4793
beb60608
CW
4794 intel_dp->has_audio = false;
4795}
d6f24d0f 4796
6c5ed5ae 4797static int
2f773477 4798intel_dp_long_pulse(struct intel_connector *connector)
a9756bb5 4799{
2f773477
VS
4800 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
4801 struct intel_dp *intel_dp = intel_attached_dp(&connector->base);
a9756bb5 4802 enum drm_connector_status status;
65fbb4e7 4803 u8 sink_irq_vector = 0;
a9756bb5 4804
2f773477 4805 WARN_ON(!drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex));
6c5ed5ae 4806
2f773477 4807 intel_display_power_get(dev_priv, intel_dp->aux_power_domain);
a9756bb5 4808
d410b56d 4809 /* Can't disconnect eDP, but you can close the lid... */
1853a9da 4810 if (intel_dp_is_edp(intel_dp))
d410b56d 4811 status = edp_detect(intel_dp);
7533eb4f 4812 else if (intel_digital_port_connected(&dp_to_dig_port(intel_dp)->base))
c555a81d 4813 status = intel_dp_detect_dpcd(intel_dp);
a9756bb5 4814 else
c555a81d
ACO
4815 status = connector_status_disconnected;
4816
5cb651a7 4817 if (status == connector_status_disconnected) {
c1617abc 4818 memset(&intel_dp->compliance, 0, sizeof(intel_dp->compliance));
4df6960e 4819
0e505a08 4820 if (intel_dp->is_mst) {
4821 DRM_DEBUG_KMS("MST device may have disappeared %d vs %d\n",
4822 intel_dp->is_mst,
4823 intel_dp->mst_mgr.mst_state);
4824 intel_dp->is_mst = false;
4825 drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr,
4826 intel_dp->is_mst);
4827 }
4828
c8c8fb33 4829 goto out;
4df6960e 4830 }
a9756bb5 4831
d7e8ef02 4832 if (intel_dp->reset_link_params) {
540b0b7f
JN
4833 /* Initial max link lane count */
4834 intel_dp->max_link_lane_count = intel_dp_max_common_lane_count(intel_dp);
f482984a 4835
540b0b7f
JN
4836 /* Initial max link rate */
4837 intel_dp->max_link_rate = intel_dp_max_common_rate(intel_dp);
d7e8ef02
MN
4838
4839 intel_dp->reset_link_params = false;
4840 }
f482984a 4841
fe5a66f9
VS
4842 intel_dp_print_rates(intel_dp);
4843
84c36753
JN
4844 drm_dp_read_desc(&intel_dp->aux, &intel_dp->desc,
4845 drm_dp_is_branch(intel_dp->dpcd));
0e390a33 4846
c4e3170a
VS
4847 intel_dp_configure_mst(intel_dp);
4848
4849 if (intel_dp->is_mst) {
f21a2198
SS
4850 /*
4851 * If we are in MST mode then this connector
4852 * won't appear connected or have anything
4853 * with EDID on it
4854 */
0e32b39c
DA
4855 status = connector_status_disconnected;
4856 goto out;
4857 }
4858
4df6960e
SS
4859 /*
4860 * Clearing NACK and defer counts to get their exact values
4861 * while reading EDID which are required by Compliance tests
4862 * 4.2.2.4 and 4.2.2.5
4863 */
4864 intel_dp->aux.i2c_nack_count = 0;
4865 intel_dp->aux.i2c_defer_count = 0;
4866
beb60608 4867 intel_dp_set_edid(intel_dp);
2f773477 4868 if (intel_dp_is_edp(intel_dp) || connector->detect_edid)
5cb651a7 4869 status = connector_status_connected;
7d23e3c3 4870 intel_dp->detect_done = true;
c8c8fb33 4871
09b1eb13
TP
4872 /* Try to read the source of the interrupt */
4873 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
65fbb4e7
VS
4874 intel_dp_get_sink_irq(intel_dp, &sink_irq_vector) &&
4875 sink_irq_vector != 0) {
09b1eb13
TP
4876 /* Clear interrupt source */
4877 drm_dp_dpcd_writeb(&intel_dp->aux,
4878 DP_DEVICE_SERVICE_IRQ_VECTOR,
4879 sink_irq_vector);
4880
4881 if (sink_irq_vector & DP_AUTOMATED_TEST_REQUEST)
4882 intel_dp_handle_test_request(intel_dp);
4883 if (sink_irq_vector & (DP_CP_IRQ | DP_SINK_SPECIFIC_IRQ))
4884 DRM_DEBUG_DRIVER("CP or sink specific irq unhandled\n");
4885 }
4886
c8c8fb33 4887out:
5cb651a7 4888 if (status != connector_status_connected && !intel_dp->is_mst)
f21a2198 4889 intel_dp_unset_edid(intel_dp);
7d23e3c3 4890
2f773477 4891 intel_display_power_put(dev_priv, intel_dp->aux_power_domain);
5cb651a7 4892 return status;
f21a2198
SS
4893}
4894
6c5ed5ae
ML
4895static int
4896intel_dp_detect(struct drm_connector *connector,
4897 struct drm_modeset_acquire_ctx *ctx,
4898 bool force)
f21a2198
SS
4899{
4900 struct intel_dp *intel_dp = intel_attached_dp(connector);
6c5ed5ae 4901 int status = connector->status;
f21a2198
SS
4902
4903 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4904 connector->base.id, connector->name);
4905
7d23e3c3 4906 /* If full detect is not performed yet, do a full detect */
42e5e657
DV
4907 if (!intel_dp->detect_done) {
4908 struct drm_crtc *crtc;
4909 int ret;
4910
4911 crtc = connector->state->crtc;
4912 if (crtc) {
4913 ret = drm_modeset_lock(&crtc->mutex, ctx);
4914 if (ret)
4915 return ret;
4916 }
4917
5cb651a7 4918 status = intel_dp_long_pulse(intel_dp->attached_connector);
42e5e657 4919 }
7d23e3c3
SS
4920
4921 intel_dp->detect_done = false;
f21a2198 4922
5cb651a7 4923 return status;
a4fc5ed6
KP
4924}
4925
beb60608
CW
4926static void
4927intel_dp_force(struct drm_connector *connector)
a4fc5ed6 4928{
df0e9248 4929 struct intel_dp *intel_dp = intel_attached_dp(connector);
beb60608 4930 struct intel_encoder *intel_encoder = &dp_to_dig_port(intel_dp)->base;
25f78f58 4931 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
a4fc5ed6 4932
beb60608
CW
4933 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4934 connector->base.id, connector->name);
4935 intel_dp_unset_edid(intel_dp);
a4fc5ed6 4936
beb60608
CW
4937 if (connector->status != connector_status_connected)
4938 return;
671dedd2 4939
5432fcaf 4940 intel_display_power_get(dev_priv, intel_dp->aux_power_domain);
beb60608
CW
4941
4942 intel_dp_set_edid(intel_dp);
4943
5432fcaf 4944 intel_display_power_put(dev_priv, intel_dp->aux_power_domain);
beb60608
CW
4945}
4946
4947static int intel_dp_get_modes(struct drm_connector *connector)
4948{
4949 struct intel_connector *intel_connector = to_intel_connector(connector);
4950 struct edid *edid;
4951
4952 edid = intel_connector->detect_edid;
4953 if (edid) {
4954 int ret = intel_connector_update_modes(connector, edid);
4955 if (ret)
4956 return ret;
4957 }
32f9d658 4958
f8779fda 4959 /* if eDP has no EDID, fall back to fixed mode */
1853a9da 4960 if (intel_dp_is_edp(intel_attached_dp(connector)) &&
beb60608 4961 intel_connector->panel.fixed_mode) {
f8779fda 4962 struct drm_display_mode *mode;
beb60608
CW
4963
4964 mode = drm_mode_duplicate(connector->dev,
dd06f90e 4965 intel_connector->panel.fixed_mode);
f8779fda 4966 if (mode) {
32f9d658
ZW
4967 drm_mode_probed_add(connector, mode);
4968 return 1;
4969 }
4970 }
beb60608 4971
32f9d658 4972 return 0;
a4fc5ed6
KP
4973}
4974
7a418e34
CW
4975static int
4976intel_dp_connector_register(struct drm_connector *connector)
4977{
4978 struct intel_dp *intel_dp = intel_attached_dp(connector);
1ebaa0b9
CW
4979 int ret;
4980
4981 ret = intel_connector_register(connector);
4982 if (ret)
4983 return ret;
7a418e34
CW
4984
4985 i915_debugfs_connector_add(connector);
4986
4987 DRM_DEBUG_KMS("registering %s bus for %s\n",
4988 intel_dp->aux.name, connector->kdev->kobj.name);
4989
4990 intel_dp->aux.dev = connector->kdev;
4991 return drm_dp_aux_register(&intel_dp->aux);
4992}
4993
c191eca1
CW
4994static void
4995intel_dp_connector_unregister(struct drm_connector *connector)
4996{
4997 drm_dp_aux_unregister(&intel_attached_dp(connector)->aux);
4998 intel_connector_unregister(connector);
4999}
5000
a4fc5ed6 5001static void
73845adf 5002intel_dp_connector_destroy(struct drm_connector *connector)
a4fc5ed6 5003{
1d508706 5004 struct intel_connector *intel_connector = to_intel_connector(connector);
aaa6fd2a 5005
10e972d3 5006 kfree(intel_connector->detect_edid);
beb60608 5007
9cd300e0
JN
5008 if (!IS_ERR_OR_NULL(intel_connector->edid))
5009 kfree(intel_connector->edid);
5010
1853a9da
JN
5011 /*
5012 * Can't call intel_dp_is_edp() since the encoder may have been
5013 * destroyed already.
5014 */
acd8db10 5015 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
1d508706 5016 intel_panel_fini(&intel_connector->panel);
aaa6fd2a 5017
a4fc5ed6 5018 drm_connector_cleanup(connector);
55f78c43 5019 kfree(connector);
a4fc5ed6
KP
5020}
5021
00c09d70 5022void intel_dp_encoder_destroy(struct drm_encoder *encoder)
24d05927 5023{
da63a9f2
PZ
5024 struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
5025 struct intel_dp *intel_dp = &intel_dig_port->dp;
24d05927 5026
0e32b39c 5027 intel_dp_mst_encoder_cleanup(intel_dig_port);
1853a9da 5028 if (intel_dp_is_edp(intel_dp)) {
bd943159 5029 cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
951468f3
VS
5030 /*
5031 * vdd might still be enabled do to the delayed vdd off.
5032 * Make sure vdd is actually turned off here.
5033 */
773538e8 5034 pps_lock(intel_dp);
4be73780 5035 edp_panel_vdd_off_sync(intel_dp);
773538e8
VS
5036 pps_unlock(intel_dp);
5037
01527b31
CT
5038 if (intel_dp->edp_notifier.notifier_call) {
5039 unregister_reboot_notifier(&intel_dp->edp_notifier);
5040 intel_dp->edp_notifier.notifier_call = NULL;
5041 }
bd943159 5042 }
99681886
CW
5043
5044 intel_dp_aux_fini(intel_dp);
5045
c8bd0e49 5046 drm_encoder_cleanup(encoder);
da63a9f2 5047 kfree(intel_dig_port);
24d05927
DV
5048}
5049
bf93ba67 5050void intel_dp_encoder_suspend(struct intel_encoder *intel_encoder)
07f9cd0b
ID
5051{
5052 struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
5053
1853a9da 5054 if (!intel_dp_is_edp(intel_dp))
07f9cd0b
ID
5055 return;
5056
951468f3
VS
5057 /*
5058 * vdd might still be enabled do to the delayed vdd off.
5059 * Make sure vdd is actually turned off here.
5060 */
afa4e53a 5061 cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
773538e8 5062 pps_lock(intel_dp);
07f9cd0b 5063 edp_panel_vdd_off_sync(intel_dp);
773538e8 5064 pps_unlock(intel_dp);
07f9cd0b
ID
5065}
5066
20f24d77
SP
5067static
5068int intel_dp_hdcp_write_an_aksv(struct intel_digital_port *intel_dig_port,
5069 u8 *an)
5070{
5071 struct intel_dp *intel_dp = enc_to_intel_dp(&intel_dig_port->base.base);
32078b72
VS
5072 static const struct drm_dp_aux_msg msg = {
5073 .request = DP_AUX_NATIVE_WRITE,
5074 .address = DP_AUX_HDCP_AKSV,
5075 .size = DRM_HDCP_KSV_LEN,
5076 };
5077 uint8_t txbuf[HEADER_SIZE + DRM_HDCP_KSV_LEN] = {}, rxbuf[2], reply = 0;
20f24d77
SP
5078 ssize_t dpcd_ret;
5079 int ret;
5080
5081 /* Output An first, that's easy */
5082 dpcd_ret = drm_dp_dpcd_write(&intel_dig_port->dp.aux, DP_AUX_HDCP_AN,
5083 an, DRM_HDCP_AN_LEN);
5084 if (dpcd_ret != DRM_HDCP_AN_LEN) {
5085 DRM_ERROR("Failed to write An over DP/AUX (%zd)\n", dpcd_ret);
5086 return dpcd_ret >= 0 ? -EIO : dpcd_ret;
5087 }
5088
5089 /*
5090 * Since Aksv is Oh-So-Secret, we can't access it in software. So in
5091 * order to get it on the wire, we need to create the AUX header as if
5092 * we were writing the data, and then tickle the hardware to output the
5093 * data once the header is sent out.
5094 */
32078b72 5095 intel_dp_aux_header(txbuf, &msg);
20f24d77 5096
32078b72 5097 ret = intel_dp_aux_xfer(intel_dp, txbuf, HEADER_SIZE + msg.size,
8159c796
VS
5098 rxbuf, sizeof(rxbuf),
5099 DP_AUX_CH_CTL_AUX_AKSV_SELECT);
20f24d77
SP
5100 if (ret < 0) {
5101 DRM_ERROR("Write Aksv over DP/AUX failed (%d)\n", ret);
5102 return ret;
5103 } else if (ret == 0) {
5104 DRM_ERROR("Aksv write over DP/AUX was empty\n");
5105 return -EIO;
5106 }
5107
5108 reply = (rxbuf[0] >> 4) & DP_AUX_NATIVE_REPLY_MASK;
5109 return reply == DP_AUX_NATIVE_REPLY_ACK ? 0 : -EIO;
5110}
5111
5112static int intel_dp_hdcp_read_bksv(struct intel_digital_port *intel_dig_port,
5113 u8 *bksv)
5114{
5115 ssize_t ret;
5116 ret = drm_dp_dpcd_read(&intel_dig_port->dp.aux, DP_AUX_HDCP_BKSV, bksv,
5117 DRM_HDCP_KSV_LEN);
5118 if (ret != DRM_HDCP_KSV_LEN) {
5119 DRM_ERROR("Read Bksv from DP/AUX failed (%zd)\n", ret);
5120 return ret >= 0 ? -EIO : ret;
5121 }
5122 return 0;
5123}
5124
5125static int intel_dp_hdcp_read_bstatus(struct intel_digital_port *intel_dig_port,
5126 u8 *bstatus)
5127{
5128 ssize_t ret;
5129 /*
5130 * For some reason the HDMI and DP HDCP specs call this register
5131 * definition by different names. In the HDMI spec, it's called BSTATUS,
5132 * but in DP it's called BINFO.
5133 */
5134 ret = drm_dp_dpcd_read(&intel_dig_port->dp.aux, DP_AUX_HDCP_BINFO,
5135 bstatus, DRM_HDCP_BSTATUS_LEN);
5136 if (ret != DRM_HDCP_BSTATUS_LEN) {
5137 DRM_ERROR("Read bstatus from DP/AUX failed (%zd)\n", ret);
5138 return ret >= 0 ? -EIO : ret;
5139 }
5140 return 0;
5141}
5142
5143static
791a98dd
R
5144int intel_dp_hdcp_read_bcaps(struct intel_digital_port *intel_dig_port,
5145 u8 *bcaps)
20f24d77
SP
5146{
5147 ssize_t ret;
791a98dd 5148
20f24d77 5149 ret = drm_dp_dpcd_read(&intel_dig_port->dp.aux, DP_AUX_HDCP_BCAPS,
791a98dd 5150 bcaps, 1);
20f24d77
SP
5151 if (ret != 1) {
5152 DRM_ERROR("Read bcaps from DP/AUX failed (%zd)\n", ret);
5153 return ret >= 0 ? -EIO : ret;
5154 }
791a98dd
R
5155
5156 return 0;
5157}
5158
5159static
5160int intel_dp_hdcp_repeater_present(struct intel_digital_port *intel_dig_port,
5161 bool *repeater_present)
5162{
5163 ssize_t ret;
5164 u8 bcaps;
5165
5166 ret = intel_dp_hdcp_read_bcaps(intel_dig_port, &bcaps);
5167 if (ret)
5168 return ret;
5169
20f24d77
SP
5170 *repeater_present = bcaps & DP_BCAPS_REPEATER_PRESENT;
5171 return 0;
5172}
5173
5174static
5175int intel_dp_hdcp_read_ri_prime(struct intel_digital_port *intel_dig_port,
5176 u8 *ri_prime)
5177{
5178 ssize_t ret;
5179 ret = drm_dp_dpcd_read(&intel_dig_port->dp.aux, DP_AUX_HDCP_RI_PRIME,
5180 ri_prime, DRM_HDCP_RI_LEN);
5181 if (ret != DRM_HDCP_RI_LEN) {
5182 DRM_ERROR("Read Ri' from DP/AUX failed (%zd)\n", ret);
5183 return ret >= 0 ? -EIO : ret;
5184 }
5185 return 0;
5186}
5187
5188static
5189int intel_dp_hdcp_read_ksv_ready(struct intel_digital_port *intel_dig_port,
5190 bool *ksv_ready)
5191{
5192 ssize_t ret;
5193 u8 bstatus;
5194 ret = drm_dp_dpcd_read(&intel_dig_port->dp.aux, DP_AUX_HDCP_BSTATUS,
5195 &bstatus, 1);
5196 if (ret != 1) {
5197 DRM_ERROR("Read bstatus from DP/AUX failed (%zd)\n", ret);
5198 return ret >= 0 ? -EIO : ret;
5199 }
5200 *ksv_ready = bstatus & DP_BSTATUS_READY;
5201 return 0;
5202}
5203
5204static
5205int intel_dp_hdcp_read_ksv_fifo(struct intel_digital_port *intel_dig_port,
5206 int num_downstream, u8 *ksv_fifo)
5207{
5208 ssize_t ret;
5209 int i;
5210
5211 /* KSV list is read via 15 byte window (3 entries @ 5 bytes each) */
5212 for (i = 0; i < num_downstream; i += 3) {
5213 size_t len = min(num_downstream - i, 3) * DRM_HDCP_KSV_LEN;
5214 ret = drm_dp_dpcd_read(&intel_dig_port->dp.aux,
5215 DP_AUX_HDCP_KSV_FIFO,
5216 ksv_fifo + i * DRM_HDCP_KSV_LEN,
5217 len);
5218 if (ret != len) {
5219 DRM_ERROR("Read ksv[%d] from DP/AUX failed (%zd)\n", i,
5220 ret);
5221 return ret >= 0 ? -EIO : ret;
5222 }
5223 }
5224 return 0;
5225}
5226
5227static
5228int intel_dp_hdcp_read_v_prime_part(struct intel_digital_port *intel_dig_port,
5229 int i, u32 *part)
5230{
5231 ssize_t ret;
5232
5233 if (i >= DRM_HDCP_V_PRIME_NUM_PARTS)
5234 return -EINVAL;
5235
5236 ret = drm_dp_dpcd_read(&intel_dig_port->dp.aux,
5237 DP_AUX_HDCP_V_PRIME(i), part,
5238 DRM_HDCP_V_PRIME_PART_LEN);
5239 if (ret != DRM_HDCP_V_PRIME_PART_LEN) {
5240 DRM_ERROR("Read v'[%d] from DP/AUX failed (%zd)\n", i, ret);
5241 return ret >= 0 ? -EIO : ret;
5242 }
5243 return 0;
5244}
5245
5246static
5247int intel_dp_hdcp_toggle_signalling(struct intel_digital_port *intel_dig_port,
5248 bool enable)
5249{
5250 /* Not used for single stream DisplayPort setups */
5251 return 0;
5252}
5253
5254static
5255bool intel_dp_hdcp_check_link(struct intel_digital_port *intel_dig_port)
5256{
5257 ssize_t ret;
5258 u8 bstatus;
b7fc1a9b 5259
20f24d77
SP
5260 ret = drm_dp_dpcd_read(&intel_dig_port->dp.aux, DP_AUX_HDCP_BSTATUS,
5261 &bstatus, 1);
5262 if (ret != 1) {
5263 DRM_ERROR("Read bstatus from DP/AUX failed (%zd)\n", ret);
b7fc1a9b 5264 return false;
20f24d77 5265 }
b7fc1a9b 5266
20f24d77
SP
5267 return !(bstatus & (DP_BSTATUS_LINK_FAILURE | DP_BSTATUS_REAUTH_REQ));
5268}
5269
791a98dd
R
5270static
5271int intel_dp_hdcp_capable(struct intel_digital_port *intel_dig_port,
5272 bool *hdcp_capable)
5273{
5274 ssize_t ret;
5275 u8 bcaps;
5276
5277 ret = intel_dp_hdcp_read_bcaps(intel_dig_port, &bcaps);
5278 if (ret)
5279 return ret;
5280
5281 *hdcp_capable = bcaps & DP_BCAPS_HDCP_CAPABLE;
5282 return 0;
5283}
5284
20f24d77
SP
5285static const struct intel_hdcp_shim intel_dp_hdcp_shim = {
5286 .write_an_aksv = intel_dp_hdcp_write_an_aksv,
5287 .read_bksv = intel_dp_hdcp_read_bksv,
5288 .read_bstatus = intel_dp_hdcp_read_bstatus,
5289 .repeater_present = intel_dp_hdcp_repeater_present,
5290 .read_ri_prime = intel_dp_hdcp_read_ri_prime,
5291 .read_ksv_ready = intel_dp_hdcp_read_ksv_ready,
5292 .read_ksv_fifo = intel_dp_hdcp_read_ksv_fifo,
5293 .read_v_prime_part = intel_dp_hdcp_read_v_prime_part,
5294 .toggle_signalling = intel_dp_hdcp_toggle_signalling,
5295 .check_link = intel_dp_hdcp_check_link,
791a98dd 5296 .hdcp_capable = intel_dp_hdcp_capable,
20f24d77
SP
5297};
5298
49e6bc51
VS
5299static void intel_edp_panel_vdd_sanitize(struct intel_dp *intel_dp)
5300{
2f773477 5301 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
49e6bc51
VS
5302
5303 lockdep_assert_held(&dev_priv->pps_mutex);
5304
5305 if (!edp_have_panel_vdd(intel_dp))
5306 return;
5307
5308 /*
5309 * The VDD bit needs a power domain reference, so if the bit is
5310 * already enabled when we boot or resume, grab this reference and
5311 * schedule a vdd off, so we don't hold on to the reference
5312 * indefinitely.
5313 */
5314 DRM_DEBUG_KMS("VDD left on by BIOS, adjusting state tracking\n");
5432fcaf 5315 intel_display_power_get(dev_priv, intel_dp->aux_power_domain);
49e6bc51
VS
5316
5317 edp_panel_vdd_schedule_off(intel_dp);
5318}
5319
9f2bdb00
VS
5320static enum pipe vlv_active_pipe(struct intel_dp *intel_dp)
5321{
5322 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
5323
5324 if ((intel_dp->DP & DP_PORT_EN) == 0)
5325 return INVALID_PIPE;
5326
5327 if (IS_CHERRYVIEW(dev_priv))
5328 return DP_PORT_TO_PIPE_CHV(intel_dp->DP);
5329 else
5330 return PORT_TO_PIPE(intel_dp->DP);
5331}
5332
bf93ba67 5333void intel_dp_encoder_reset(struct drm_encoder *encoder)
6d93c0c4 5334{
64989ca4 5335 struct drm_i915_private *dev_priv = to_i915(encoder->dev);
dd75f6dd
ID
5336 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
5337 struct intel_lspcon *lspcon = dp_to_lspcon(intel_dp);
64989ca4
VS
5338
5339 if (!HAS_DDI(dev_priv))
5340 intel_dp->DP = I915_READ(intel_dp->output_reg);
49e6bc51 5341
dd75f6dd 5342 if (lspcon->active)
910530c0
SS
5343 lspcon_resume(lspcon);
5344
d7e8ef02
MN
5345 intel_dp->reset_link_params = true;
5346
49e6bc51
VS
5347 pps_lock(intel_dp);
5348
9f2bdb00
VS
5349 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
5350 intel_dp->active_pipe = vlv_active_pipe(intel_dp);
5351
1853a9da 5352 if (intel_dp_is_edp(intel_dp)) {
9f2bdb00 5353 /* Reinit the power sequencer, in case BIOS did something with it. */
46bd8383 5354 intel_dp_pps_init(intel_dp);
9f2bdb00
VS
5355 intel_edp_panel_vdd_sanitize(intel_dp);
5356 }
49e6bc51
VS
5357
5358 pps_unlock(intel_dp);
6d93c0c4
ID
5359}
5360
a4fc5ed6 5361static const struct drm_connector_funcs intel_dp_connector_funcs = {
beb60608 5362 .force = intel_dp_force,
a4fc5ed6 5363 .fill_modes = drm_helper_probe_single_connector_modes,
8f647a01
ML
5364 .atomic_get_property = intel_digital_connector_atomic_get_property,
5365 .atomic_set_property = intel_digital_connector_atomic_set_property,
7a418e34 5366 .late_register = intel_dp_connector_register,
c191eca1 5367 .early_unregister = intel_dp_connector_unregister,
73845adf 5368 .destroy = intel_dp_connector_destroy,
c6f95f27 5369 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
8f647a01 5370 .atomic_duplicate_state = intel_digital_connector_duplicate_state,
a4fc5ed6
KP
5371};
5372
5373static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
6c5ed5ae 5374 .detect_ctx = intel_dp_detect,
a4fc5ed6
KP
5375 .get_modes = intel_dp_get_modes,
5376 .mode_valid = intel_dp_mode_valid,
8f647a01 5377 .atomic_check = intel_digital_connector_atomic_check,
a4fc5ed6
KP
5378};
5379
a4fc5ed6 5380static const struct drm_encoder_funcs intel_dp_enc_funcs = {
6d93c0c4 5381 .reset = intel_dp_encoder_reset,
24d05927 5382 .destroy = intel_dp_encoder_destroy,
a4fc5ed6
KP
5383};
5384
b2c5c181 5385enum irqreturn
13cf5504
DA
5386intel_dp_hpd_pulse(struct intel_digital_port *intel_dig_port, bool long_hpd)
5387{
5388 struct intel_dp *intel_dp = &intel_dig_port->dp;
2f773477 5389 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
b2c5c181 5390 enum irqreturn ret = IRQ_NONE;
1c767b33 5391
7a7f84cc
VS
5392 if (long_hpd && intel_dig_port->base.type == INTEL_OUTPUT_EDP) {
5393 /*
5394 * vdd off can generate a long pulse on eDP which
5395 * would require vdd on to handle it, and thus we
5396 * would end up in an endless cycle of
5397 * "vdd off -> long hpd -> vdd on -> detect -> vdd off -> ..."
5398 */
5399 DRM_DEBUG_KMS("ignoring long hpd on eDP port %c\n",
8f4f2797 5400 port_name(intel_dig_port->base.port));
a8b3d52f 5401 return IRQ_HANDLED;
7a7f84cc
VS
5402 }
5403
26fbb774 5404 DRM_DEBUG_KMS("got hpd irq on port %c - %s\n",
8f4f2797 5405 port_name(intel_dig_port->base.port),
0e32b39c 5406 long_hpd ? "long" : "short");
13cf5504 5407
27d4efc5 5408 if (long_hpd) {
d7e8ef02 5409 intel_dp->reset_link_params = true;
27d4efc5
VS
5410 intel_dp->detect_done = false;
5411 return IRQ_NONE;
5412 }
5413
5432fcaf 5414 intel_display_power_get(dev_priv, intel_dp->aux_power_domain);
1c767b33 5415
27d4efc5
VS
5416 if (intel_dp->is_mst) {
5417 if (intel_dp_check_mst_status(intel_dp) == -EINVAL) {
5418 /*
5419 * If we were in MST mode, and device is not
5420 * there, get out of MST mode
5421 */
5422 DRM_DEBUG_KMS("MST device may have disappeared %d vs %d\n",
5423 intel_dp->is_mst, intel_dp->mst_mgr.mst_state);
5424 intel_dp->is_mst = false;
5425 drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr,
5426 intel_dp->is_mst);
5427 intel_dp->detect_done = false;
5428 goto put_power;
0e32b39c 5429 }
27d4efc5 5430 }
0e32b39c 5431
27d4efc5 5432 if (!intel_dp->is_mst) {
c85d200e 5433 bool handled;
42e5e657
DV
5434
5435 handled = intel_dp_short_pulse(intel_dp);
5436
20f24d77
SP
5437 /* Short pulse can signify loss of hdcp authentication */
5438 intel_hdcp_check_link(intel_dp->attached_connector);
5439
42e5e657 5440 if (!handled) {
27d4efc5
VS
5441 intel_dp->detect_done = false;
5442 goto put_power;
39ff747b 5443 }
0e32b39c 5444 }
b2c5c181
DV
5445
5446 ret = IRQ_HANDLED;
5447
1c767b33 5448put_power:
5432fcaf 5449 intel_display_power_put(dev_priv, intel_dp->aux_power_domain);
1c767b33
ID
5450
5451 return ret;
13cf5504
DA
5452}
5453
477ec328 5454/* check the VBT to see whether the eDP is on another port */
7b91bf7f 5455bool intel_dp_is_port_edp(struct drm_i915_private *dev_priv, enum port port)
36e83a18 5456{
53ce81a7
VS
5457 /*
5458 * eDP not supported on g4x. so bail out early just
5459 * for a bit extra safety in case the VBT is bonkers.
5460 */
dd11bc10 5461 if (INTEL_GEN(dev_priv) < 5)
53ce81a7
VS
5462 return false;
5463
a98d9c1d 5464 if (INTEL_GEN(dev_priv) < 9 && port == PORT_A)
3b32a35b
VS
5465 return true;
5466
951d9efe 5467 return intel_bios_is_port_edp(dev_priv, port);
36e83a18
ZY
5468}
5469
200819ab 5470static void
f684960e
CW
5471intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
5472{
8b45330a 5473 struct drm_i915_private *dev_priv = to_i915(connector->dev);
68ec0736
VS
5474 enum port port = dp_to_dig_port(intel_dp)->base.port;
5475
5476 if (!IS_G4X(dev_priv) && port != PORT_A)
5477 intel_attach_force_audio_property(connector);
8b45330a 5478
e953fd7b 5479 intel_attach_broadcast_rgb_property(connector);
53b41837 5480
1853a9da 5481 if (intel_dp_is_edp(intel_dp)) {
8b45330a
ML
5482 u32 allowed_scalers;
5483
5484 allowed_scalers = BIT(DRM_MODE_SCALE_ASPECT) | BIT(DRM_MODE_SCALE_FULLSCREEN);
5485 if (!HAS_GMCH_DISPLAY(dev_priv))
5486 allowed_scalers |= BIT(DRM_MODE_SCALE_CENTER);
5487
5488 drm_connector_attach_scaling_mode_property(connector, allowed_scalers);
5489
eead06df 5490 connector->state->scaling_mode = DRM_MODE_SCALE_ASPECT;
8b45330a 5491
53b41837 5492 }
f684960e
CW
5493}
5494
dada1a9f
ID
5495static void intel_dp_init_panel_power_timestamps(struct intel_dp *intel_dp)
5496{
d28d4731 5497 intel_dp->panel_power_off_time = ktime_get_boottime();
dada1a9f
ID
5498 intel_dp->last_power_on = jiffies;
5499 intel_dp->last_backlight_off = jiffies;
5500}
5501
67a54566 5502static void
46bd8383 5503intel_pps_readout_hw_state(struct intel_dp *intel_dp, struct edp_power_seq *seq)
67a54566 5504{
46bd8383 5505 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
b0a08bec 5506 u32 pp_on, pp_off, pp_div = 0, pp_ctl = 0;
8e8232d5 5507 struct pps_registers regs;
453c5420 5508
46bd8383 5509 intel_pps_get_registers(intel_dp, &regs);
67a54566
DV
5510
5511 /* Workaround: Need to write PP_CONTROL with the unlock key as
5512 * the very first thing. */
b0a08bec 5513 pp_ctl = ironlake_get_pp_control(intel_dp);
67a54566 5514
8e8232d5
ID
5515 pp_on = I915_READ(regs.pp_on);
5516 pp_off = I915_READ(regs.pp_off);
b0d6a0f2
AS
5517 if (!IS_GEN9_LP(dev_priv) && !HAS_PCH_CNP(dev_priv) &&
5518 !HAS_PCH_ICP(dev_priv)) {
8e8232d5
ID
5519 I915_WRITE(regs.pp_ctrl, pp_ctl);
5520 pp_div = I915_READ(regs.pp_div);
b0a08bec 5521 }
67a54566
DV
5522
5523 /* Pull timing values out of registers */
54648618
ID
5524 seq->t1_t3 = (pp_on & PANEL_POWER_UP_DELAY_MASK) >>
5525 PANEL_POWER_UP_DELAY_SHIFT;
67a54566 5526
54648618
ID
5527 seq->t8 = (pp_on & PANEL_LIGHT_ON_DELAY_MASK) >>
5528 PANEL_LIGHT_ON_DELAY_SHIFT;
67a54566 5529
54648618
ID
5530 seq->t9 = (pp_off & PANEL_LIGHT_OFF_DELAY_MASK) >>
5531 PANEL_LIGHT_OFF_DELAY_SHIFT;
67a54566 5532
54648618
ID
5533 seq->t10 = (pp_off & PANEL_POWER_DOWN_DELAY_MASK) >>
5534 PANEL_POWER_DOWN_DELAY_SHIFT;
67a54566 5535
b0d6a0f2
AS
5536 if (IS_GEN9_LP(dev_priv) || HAS_PCH_CNP(dev_priv) ||
5537 HAS_PCH_ICP(dev_priv)) {
12c8ca9c
MN
5538 seq->t11_t12 = ((pp_ctl & BXT_POWER_CYCLE_DELAY_MASK) >>
5539 BXT_POWER_CYCLE_DELAY_SHIFT) * 1000;
b0a08bec 5540 } else {
54648618 5541 seq->t11_t12 = ((pp_div & PANEL_POWER_CYCLE_DELAY_MASK) >>
67a54566 5542 PANEL_POWER_CYCLE_DELAY_SHIFT) * 1000;
b0a08bec 5543 }
54648618
ID
5544}
5545
de9c1b6b
ID
5546static void
5547intel_pps_dump_state(const char *state_name, const struct edp_power_seq *seq)
5548{
5549 DRM_DEBUG_KMS("%s t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
5550 state_name,
5551 seq->t1_t3, seq->t8, seq->t9, seq->t10, seq->t11_t12);
5552}
5553
5554static void
46bd8383 5555intel_pps_verify_state(struct intel_dp *intel_dp)
de9c1b6b
ID
5556{
5557 struct edp_power_seq hw;
5558 struct edp_power_seq *sw = &intel_dp->pps_delays;
5559
46bd8383 5560 intel_pps_readout_hw_state(intel_dp, &hw);
de9c1b6b
ID
5561
5562 if (hw.t1_t3 != sw->t1_t3 || hw.t8 != sw->t8 || hw.t9 != sw->t9 ||
5563 hw.t10 != sw->t10 || hw.t11_t12 != sw->t11_t12) {
5564 DRM_ERROR("PPS state mismatch\n");
5565 intel_pps_dump_state("sw", sw);
5566 intel_pps_dump_state("hw", &hw);
5567 }
5568}
5569
54648618 5570static void
46bd8383 5571intel_dp_init_panel_power_sequencer(struct intel_dp *intel_dp)
54648618 5572{
46bd8383 5573 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
54648618
ID
5574 struct edp_power_seq cur, vbt, spec,
5575 *final = &intel_dp->pps_delays;
5576
5577 lockdep_assert_held(&dev_priv->pps_mutex);
5578
5579 /* already initialized? */
5580 if (final->t11_t12 != 0)
5581 return;
5582
46bd8383 5583 intel_pps_readout_hw_state(intel_dp, &cur);
67a54566 5584
de9c1b6b 5585 intel_pps_dump_state("cur", &cur);
67a54566 5586
6aa23e65 5587 vbt = dev_priv->vbt.edp.pps;
c99a259b
MN
5588 /* On Toshiba Satellite P50-C-18C system the VBT T12 delay
5589 * of 500ms appears to be too short. Ocassionally the panel
5590 * just fails to power back on. Increasing the delay to 800ms
5591 * seems sufficient to avoid this problem.
5592 */
5593 if (dev_priv->quirks & QUIRK_INCREASE_T12_DELAY) {
7313f5a9 5594 vbt.t11_t12 = max_t(u16, vbt.t11_t12, 1300 * 10);
c99a259b
MN
5595 DRM_DEBUG_KMS("Increasing T12 panel delay as per the quirk to %d\n",
5596 vbt.t11_t12);
5597 }
770a17a5
MN
5598 /* T11_T12 delay is special and actually in units of 100ms, but zero
5599 * based in the hw (so we need to add 100 ms). But the sw vbt
5600 * table multiplies it with 1000 to make it in units of 100usec,
5601 * too. */
5602 vbt.t11_t12 += 100 * 10;
67a54566
DV
5603
5604 /* Upper limits from eDP 1.3 spec. Note that we use the clunky units of
5605 * our hw here, which are all in 100usec. */
5606 spec.t1_t3 = 210 * 10;
5607 spec.t8 = 50 * 10; /* no limit for t8, use t7 instead */
5608 spec.t9 = 50 * 10; /* no limit for t9, make it symmetric with t8 */
5609 spec.t10 = 500 * 10;
5610 /* This one is special and actually in units of 100ms, but zero
5611 * based in the hw (so we need to add 100 ms). But the sw vbt
5612 * table multiplies it with 1000 to make it in units of 100usec,
5613 * too. */
5614 spec.t11_t12 = (510 + 100) * 10;
5615
de9c1b6b 5616 intel_pps_dump_state("vbt", &vbt);
67a54566
DV
5617
5618 /* Use the max of the register settings and vbt. If both are
5619 * unset, fall back to the spec limits. */
36b5f425 5620#define assign_final(field) final->field = (max(cur.field, vbt.field) == 0 ? \
67a54566
DV
5621 spec.field : \
5622 max(cur.field, vbt.field))
5623 assign_final(t1_t3);
5624 assign_final(t8);
5625 assign_final(t9);
5626 assign_final(t10);
5627 assign_final(t11_t12);
5628#undef assign_final
5629
36b5f425 5630#define get_delay(field) (DIV_ROUND_UP(final->field, 10))
67a54566
DV
5631 intel_dp->panel_power_up_delay = get_delay(t1_t3);
5632 intel_dp->backlight_on_delay = get_delay(t8);
5633 intel_dp->backlight_off_delay = get_delay(t9);
5634 intel_dp->panel_power_down_delay = get_delay(t10);
5635 intel_dp->panel_power_cycle_delay = get_delay(t11_t12);
5636#undef get_delay
5637
f30d26e4
JN
5638 DRM_DEBUG_KMS("panel power up delay %d, power down delay %d, power cycle delay %d\n",
5639 intel_dp->panel_power_up_delay, intel_dp->panel_power_down_delay,
5640 intel_dp->panel_power_cycle_delay);
5641
5642 DRM_DEBUG_KMS("backlight on delay %d, off delay %d\n",
5643 intel_dp->backlight_on_delay, intel_dp->backlight_off_delay);
de9c1b6b
ID
5644
5645 /*
5646 * We override the HW backlight delays to 1 because we do manual waits
5647 * on them. For T8, even BSpec recommends doing it. For T9, if we
5648 * don't do this, we'll end up waiting for the backlight off delay
5649 * twice: once when we do the manual sleep, and once when we disable
5650 * the panel and wait for the PP_STATUS bit to become zero.
5651 */
5652 final->t8 = 1;
5653 final->t9 = 1;
5643205c
ID
5654
5655 /*
5656 * HW has only a 100msec granularity for t11_t12 so round it up
5657 * accordingly.
5658 */
5659 final->t11_t12 = roundup(final->t11_t12, 100 * 10);
f30d26e4
JN
5660}
5661
5662static void
46bd8383 5663intel_dp_init_panel_power_sequencer_registers(struct intel_dp *intel_dp,
5d5ab2d2 5664 bool force_disable_vdd)
f30d26e4 5665{
46bd8383 5666 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
453c5420 5667 u32 pp_on, pp_off, pp_div, port_sel = 0;
e7dc33f3 5668 int div = dev_priv->rawclk_freq / 1000;
8e8232d5 5669 struct pps_registers regs;
8f4f2797 5670 enum port port = dp_to_dig_port(intel_dp)->base.port;
36b5f425 5671 const struct edp_power_seq *seq = &intel_dp->pps_delays;
453c5420 5672
e39b999a 5673 lockdep_assert_held(&dev_priv->pps_mutex);
453c5420 5674
46bd8383 5675 intel_pps_get_registers(intel_dp, &regs);
453c5420 5676
5d5ab2d2
VS
5677 /*
5678 * On some VLV machines the BIOS can leave the VDD
e7f2af78 5679 * enabled even on power sequencers which aren't
5d5ab2d2
VS
5680 * hooked up to any port. This would mess up the
5681 * power domain tracking the first time we pick
5682 * one of these power sequencers for use since
5683 * edp_panel_vdd_on() would notice that the VDD was
5684 * already on and therefore wouldn't grab the power
5685 * domain reference. Disable VDD first to avoid this.
5686 * This also avoids spuriously turning the VDD on as
e7f2af78 5687 * soon as the new power sequencer gets initialized.
5d5ab2d2
VS
5688 */
5689 if (force_disable_vdd) {
5690 u32 pp = ironlake_get_pp_control(intel_dp);
5691
5692 WARN(pp & PANEL_POWER_ON, "Panel power already on\n");
5693
5694 if (pp & EDP_FORCE_VDD)
5695 DRM_DEBUG_KMS("VDD already on, disabling first\n");
5696
5697 pp &= ~EDP_FORCE_VDD;
5698
5699 I915_WRITE(regs.pp_ctrl, pp);
5700 }
5701
f30d26e4 5702 pp_on = (seq->t1_t3 << PANEL_POWER_UP_DELAY_SHIFT) |
de9c1b6b
ID
5703 (seq->t8 << PANEL_LIGHT_ON_DELAY_SHIFT);
5704 pp_off = (seq->t9 << PANEL_LIGHT_OFF_DELAY_SHIFT) |
f30d26e4 5705 (seq->t10 << PANEL_POWER_DOWN_DELAY_SHIFT);
67a54566
DV
5706 /* Compute the divisor for the pp clock, simply match the Bspec
5707 * formula. */
b0d6a0f2
AS
5708 if (IS_GEN9_LP(dev_priv) || HAS_PCH_CNP(dev_priv) ||
5709 HAS_PCH_ICP(dev_priv)) {
8e8232d5 5710 pp_div = I915_READ(regs.pp_ctrl);
b0a08bec 5711 pp_div &= ~BXT_POWER_CYCLE_DELAY_MASK;
12c8ca9c 5712 pp_div |= (DIV_ROUND_UP(seq->t11_t12, 1000)
b0a08bec
VK
5713 << BXT_POWER_CYCLE_DELAY_SHIFT);
5714 } else {
5715 pp_div = ((100 * div)/2 - 1) << PP_REFERENCE_DIVIDER_SHIFT;
5716 pp_div |= (DIV_ROUND_UP(seq->t11_t12, 1000)
5717 << PANEL_POWER_CYCLE_DELAY_SHIFT);
5718 }
67a54566
DV
5719
5720 /* Haswell doesn't have any port selection bits for the panel
5721 * power sequencer any more. */
920a14b2 5722 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
ad933b56 5723 port_sel = PANEL_PORT_SELECT_VLV(port);
6e266956 5724 } else if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
ad933b56 5725 if (port == PORT_A)
a24c144c 5726 port_sel = PANEL_PORT_SELECT_DPA;
67a54566 5727 else
a24c144c 5728 port_sel = PANEL_PORT_SELECT_DPD;
67a54566
DV
5729 }
5730
453c5420
JB
5731 pp_on |= port_sel;
5732
8e8232d5
ID
5733 I915_WRITE(regs.pp_on, pp_on);
5734 I915_WRITE(regs.pp_off, pp_off);
b0d6a0f2
AS
5735 if (IS_GEN9_LP(dev_priv) || HAS_PCH_CNP(dev_priv) ||
5736 HAS_PCH_ICP(dev_priv))
8e8232d5 5737 I915_WRITE(regs.pp_ctrl, pp_div);
b0a08bec 5738 else
8e8232d5 5739 I915_WRITE(regs.pp_div, pp_div);
67a54566 5740
67a54566 5741 DRM_DEBUG_KMS("panel power sequencer register settings: PP_ON %#x, PP_OFF %#x, PP_DIV %#x\n",
8e8232d5
ID
5742 I915_READ(regs.pp_on),
5743 I915_READ(regs.pp_off),
b0d6a0f2
AS
5744 (IS_GEN9_LP(dev_priv) || HAS_PCH_CNP(dev_priv) ||
5745 HAS_PCH_ICP(dev_priv)) ?
8e8232d5
ID
5746 (I915_READ(regs.pp_ctrl) & BXT_POWER_CYCLE_DELAY_MASK) :
5747 I915_READ(regs.pp_div));
f684960e
CW
5748}
5749
46bd8383 5750static void intel_dp_pps_init(struct intel_dp *intel_dp)
335f752b 5751{
46bd8383 5752 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
920a14b2
TU
5753
5754 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
335f752b
ID
5755 vlv_initial_power_sequencer_setup(intel_dp);
5756 } else {
46bd8383
VS
5757 intel_dp_init_panel_power_sequencer(intel_dp);
5758 intel_dp_init_panel_power_sequencer_registers(intel_dp, false);
335f752b
ID
5759 }
5760}
5761
b33a2815
VK
5762/**
5763 * intel_dp_set_drrs_state - program registers for RR switch to take effect
5423adf1 5764 * @dev_priv: i915 device
e896402c 5765 * @crtc_state: a pointer to the active intel_crtc_state
b33a2815
VK
5766 * @refresh_rate: RR to be programmed
5767 *
5768 * This function gets called when refresh rate (RR) has to be changed from
5769 * one frequency to another. Switches can be between high and low RR
5770 * supported by the panel or to any other RR based on media playback (in
5771 * this case, RR value needs to be passed from user space).
5772 *
5773 * The caller of this function needs to take a lock on dev_priv->drrs.
5774 */
85cb48a1 5775static void intel_dp_set_drrs_state(struct drm_i915_private *dev_priv,
5f88a9c6 5776 const struct intel_crtc_state *crtc_state,
85cb48a1 5777 int refresh_rate)
439d7ac0 5778{
439d7ac0 5779 struct intel_encoder *encoder;
96178eeb
VK
5780 struct intel_digital_port *dig_port = NULL;
5781 struct intel_dp *intel_dp = dev_priv->drrs.dp;
85cb48a1 5782 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
96178eeb 5783 enum drrs_refresh_rate_type index = DRRS_HIGH_RR;
439d7ac0
PB
5784
5785 if (refresh_rate <= 0) {
5786 DRM_DEBUG_KMS("Refresh rate should be positive non-zero.\n");
5787 return;
5788 }
5789
96178eeb
VK
5790 if (intel_dp == NULL) {
5791 DRM_DEBUG_KMS("DRRS not supported.\n");
439d7ac0
PB
5792 return;
5793 }
5794
96178eeb
VK
5795 dig_port = dp_to_dig_port(intel_dp);
5796 encoder = &dig_port->base;
439d7ac0
PB
5797
5798 if (!intel_crtc) {
5799 DRM_DEBUG_KMS("DRRS: intel_crtc not initialized\n");
5800 return;
5801 }
5802
96178eeb 5803 if (dev_priv->drrs.type < SEAMLESS_DRRS_SUPPORT) {
439d7ac0
PB
5804 DRM_DEBUG_KMS("Only Seamless DRRS supported.\n");
5805 return;
5806 }
5807
96178eeb
VK
5808 if (intel_dp->attached_connector->panel.downclock_mode->vrefresh ==
5809 refresh_rate)
439d7ac0
PB
5810 index = DRRS_LOW_RR;
5811
96178eeb 5812 if (index == dev_priv->drrs.refresh_rate_type) {
439d7ac0
PB
5813 DRM_DEBUG_KMS(
5814 "DRRS requested for previously set RR...ignoring\n");
5815 return;
5816 }
5817
85cb48a1 5818 if (!crtc_state->base.active) {
439d7ac0
PB
5819 DRM_DEBUG_KMS("eDP encoder disabled. CRTC not Active\n");
5820 return;
5821 }
5822
85cb48a1 5823 if (INTEL_GEN(dev_priv) >= 8 && !IS_CHERRYVIEW(dev_priv)) {
a4c30b1d
VK
5824 switch (index) {
5825 case DRRS_HIGH_RR:
5826 intel_dp_set_m_n(intel_crtc, M1_N1);
5827 break;
5828 case DRRS_LOW_RR:
5829 intel_dp_set_m_n(intel_crtc, M2_N2);
5830 break;
5831 case DRRS_MAX_RR:
5832 default:
5833 DRM_ERROR("Unsupported refreshrate type\n");
5834 }
85cb48a1
ML
5835 } else if (INTEL_GEN(dev_priv) > 6) {
5836 i915_reg_t reg = PIPECONF(crtc_state->cpu_transcoder);
649636ef 5837 u32 val;
a4c30b1d 5838
649636ef 5839 val = I915_READ(reg);
439d7ac0 5840 if (index > DRRS_HIGH_RR) {
85cb48a1 5841 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
6fa7aec1
VK
5842 val |= PIPECONF_EDP_RR_MODE_SWITCH_VLV;
5843 else
5844 val |= PIPECONF_EDP_RR_MODE_SWITCH;
439d7ac0 5845 } else {
85cb48a1 5846 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
6fa7aec1
VK
5847 val &= ~PIPECONF_EDP_RR_MODE_SWITCH_VLV;
5848 else
5849 val &= ~PIPECONF_EDP_RR_MODE_SWITCH;
439d7ac0
PB
5850 }
5851 I915_WRITE(reg, val);
5852 }
5853
4e9ac947
VK
5854 dev_priv->drrs.refresh_rate_type = index;
5855
5856 DRM_DEBUG_KMS("eDP Refresh Rate set to : %dHz\n", refresh_rate);
5857}
5858
b33a2815
VK
5859/**
5860 * intel_edp_drrs_enable - init drrs struct if supported
5861 * @intel_dp: DP struct
5423adf1 5862 * @crtc_state: A pointer to the active crtc state.
b33a2815
VK
5863 *
5864 * Initializes frontbuffer_bits and drrs.dp
5865 */
85cb48a1 5866void intel_edp_drrs_enable(struct intel_dp *intel_dp,
5f88a9c6 5867 const struct intel_crtc_state *crtc_state)
c395578e 5868{
2f773477 5869 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
c395578e 5870
85cb48a1 5871 if (!crtc_state->has_drrs) {
c395578e
VK
5872 DRM_DEBUG_KMS("Panel doesn't support DRRS\n");
5873 return;
5874 }
5875
da83ef85
RS
5876 if (dev_priv->psr.enabled) {
5877 DRM_DEBUG_KMS("PSR enabled. Not enabling DRRS.\n");
5878 return;
5879 }
5880
c395578e
VK
5881 mutex_lock(&dev_priv->drrs.mutex);
5882 if (WARN_ON(dev_priv->drrs.dp)) {
5883 DRM_ERROR("DRRS already enabled\n");
5884 goto unlock;
5885 }
5886
5887 dev_priv->drrs.busy_frontbuffer_bits = 0;
5888
5889 dev_priv->drrs.dp = intel_dp;
5890
5891unlock:
5892 mutex_unlock(&dev_priv->drrs.mutex);
5893}
5894
b33a2815
VK
5895/**
5896 * intel_edp_drrs_disable - Disable DRRS
5897 * @intel_dp: DP struct
5423adf1 5898 * @old_crtc_state: Pointer to old crtc_state.
b33a2815
VK
5899 *
5900 */
85cb48a1 5901void intel_edp_drrs_disable(struct intel_dp *intel_dp,
5f88a9c6 5902 const struct intel_crtc_state *old_crtc_state)
c395578e 5903{
2f773477 5904 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
c395578e 5905
85cb48a1 5906 if (!old_crtc_state->has_drrs)
c395578e
VK
5907 return;
5908
5909 mutex_lock(&dev_priv->drrs.mutex);
5910 if (!dev_priv->drrs.dp) {
5911 mutex_unlock(&dev_priv->drrs.mutex);
5912 return;
5913 }
5914
5915 if (dev_priv->drrs.refresh_rate_type == DRRS_LOW_RR)
85cb48a1
ML
5916 intel_dp_set_drrs_state(dev_priv, old_crtc_state,
5917 intel_dp->attached_connector->panel.fixed_mode->vrefresh);
c395578e
VK
5918
5919 dev_priv->drrs.dp = NULL;
5920 mutex_unlock(&dev_priv->drrs.mutex);
5921
5922 cancel_delayed_work_sync(&dev_priv->drrs.work);
5923}
5924
4e9ac947
VK
5925static void intel_edp_drrs_downclock_work(struct work_struct *work)
5926{
5927 struct drm_i915_private *dev_priv =
5928 container_of(work, typeof(*dev_priv), drrs.work.work);
5929 struct intel_dp *intel_dp;
5930
5931 mutex_lock(&dev_priv->drrs.mutex);
5932
5933 intel_dp = dev_priv->drrs.dp;
5934
5935 if (!intel_dp)
5936 goto unlock;
5937
439d7ac0 5938 /*
4e9ac947
VK
5939 * The delayed work can race with an invalidate hence we need to
5940 * recheck.
439d7ac0
PB
5941 */
5942
4e9ac947
VK
5943 if (dev_priv->drrs.busy_frontbuffer_bits)
5944 goto unlock;
439d7ac0 5945
85cb48a1
ML
5946 if (dev_priv->drrs.refresh_rate_type != DRRS_LOW_RR) {
5947 struct drm_crtc *crtc = dp_to_dig_port(intel_dp)->base.base.crtc;
5948
5949 intel_dp_set_drrs_state(dev_priv, to_intel_crtc(crtc)->config,
5950 intel_dp->attached_connector->panel.downclock_mode->vrefresh);
5951 }
439d7ac0 5952
4e9ac947 5953unlock:
4e9ac947 5954 mutex_unlock(&dev_priv->drrs.mutex);
439d7ac0
PB
5955}
5956
b33a2815 5957/**
0ddfd203 5958 * intel_edp_drrs_invalidate - Disable Idleness DRRS
5748b6a1 5959 * @dev_priv: i915 device
b33a2815
VK
5960 * @frontbuffer_bits: frontbuffer plane tracking bits
5961 *
0ddfd203
R
5962 * This function gets called everytime rendering on the given planes start.
5963 * Hence DRRS needs to be Upclocked, i.e. (LOW_RR -> HIGH_RR).
b33a2815
VK
5964 *
5965 * Dirty frontbuffers relevant to DRRS are tracked in busy_frontbuffer_bits.
5966 */
5748b6a1
CW
5967void intel_edp_drrs_invalidate(struct drm_i915_private *dev_priv,
5968 unsigned int frontbuffer_bits)
a93fad0f 5969{
a93fad0f
VK
5970 struct drm_crtc *crtc;
5971 enum pipe pipe;
5972
9da7d693 5973 if (dev_priv->drrs.type == DRRS_NOT_SUPPORTED)
a93fad0f
VK
5974 return;
5975
88f933a8 5976 cancel_delayed_work(&dev_priv->drrs.work);
3954e733 5977
a93fad0f 5978 mutex_lock(&dev_priv->drrs.mutex);
9da7d693
DV
5979 if (!dev_priv->drrs.dp) {
5980 mutex_unlock(&dev_priv->drrs.mutex);
5981 return;
5982 }
5983
a93fad0f
VK
5984 crtc = dp_to_dig_port(dev_priv->drrs.dp)->base.base.crtc;
5985 pipe = to_intel_crtc(crtc)->pipe;
5986
c1d038c6
DV
5987 frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(pipe);
5988 dev_priv->drrs.busy_frontbuffer_bits |= frontbuffer_bits;
5989
0ddfd203 5990 /* invalidate means busy screen hence upclock */
c1d038c6 5991 if (frontbuffer_bits && dev_priv->drrs.refresh_rate_type == DRRS_LOW_RR)
85cb48a1
ML
5992 intel_dp_set_drrs_state(dev_priv, to_intel_crtc(crtc)->config,
5993 dev_priv->drrs.dp->attached_connector->panel.fixed_mode->vrefresh);
a93fad0f 5994
a93fad0f
VK
5995 mutex_unlock(&dev_priv->drrs.mutex);
5996}
5997
b33a2815 5998/**
0ddfd203 5999 * intel_edp_drrs_flush - Restart Idleness DRRS
5748b6a1 6000 * @dev_priv: i915 device
b33a2815
VK
6001 * @frontbuffer_bits: frontbuffer plane tracking bits
6002 *
0ddfd203
R
6003 * This function gets called every time rendering on the given planes has
6004 * completed or flip on a crtc is completed. So DRRS should be upclocked
6005 * (LOW_RR -> HIGH_RR). And also Idleness detection should be started again,
6006 * if no other planes are dirty.
b33a2815
VK
6007 *
6008 * Dirty frontbuffers relevant to DRRS are tracked in busy_frontbuffer_bits.
6009 */
5748b6a1
CW
6010void intel_edp_drrs_flush(struct drm_i915_private *dev_priv,
6011 unsigned int frontbuffer_bits)
a93fad0f 6012{
a93fad0f
VK
6013 struct drm_crtc *crtc;
6014 enum pipe pipe;
6015
9da7d693 6016 if (dev_priv->drrs.type == DRRS_NOT_SUPPORTED)
a93fad0f
VK
6017 return;
6018
88f933a8 6019 cancel_delayed_work(&dev_priv->drrs.work);
3954e733 6020
a93fad0f 6021 mutex_lock(&dev_priv->drrs.mutex);
9da7d693
DV
6022 if (!dev_priv->drrs.dp) {
6023 mutex_unlock(&dev_priv->drrs.mutex);
6024 return;
6025 }
6026
a93fad0f
VK
6027 crtc = dp_to_dig_port(dev_priv->drrs.dp)->base.base.crtc;
6028 pipe = to_intel_crtc(crtc)->pipe;
c1d038c6
DV
6029
6030 frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(pipe);
a93fad0f
VK
6031 dev_priv->drrs.busy_frontbuffer_bits &= ~frontbuffer_bits;
6032
0ddfd203 6033 /* flush means busy screen hence upclock */
c1d038c6 6034 if (frontbuffer_bits && dev_priv->drrs.refresh_rate_type == DRRS_LOW_RR)
85cb48a1
ML
6035 intel_dp_set_drrs_state(dev_priv, to_intel_crtc(crtc)->config,
6036 dev_priv->drrs.dp->attached_connector->panel.fixed_mode->vrefresh);
0ddfd203
R
6037
6038 /*
6039 * flush also means no more activity hence schedule downclock, if all
6040 * other fbs are quiescent too
6041 */
6042 if (!dev_priv->drrs.busy_frontbuffer_bits)
a93fad0f
VK
6043 schedule_delayed_work(&dev_priv->drrs.work,
6044 msecs_to_jiffies(1000));
6045 mutex_unlock(&dev_priv->drrs.mutex);
6046}
6047
b33a2815
VK
6048/**
6049 * DOC: Display Refresh Rate Switching (DRRS)
6050 *
6051 * Display Refresh Rate Switching (DRRS) is a power conservation feature
6052 * which enables swtching between low and high refresh rates,
6053 * dynamically, based on the usage scenario. This feature is applicable
6054 * for internal panels.
6055 *
6056 * Indication that the panel supports DRRS is given by the panel EDID, which
6057 * would list multiple refresh rates for one resolution.
6058 *
6059 * DRRS is of 2 types - static and seamless.
6060 * Static DRRS involves changing refresh rate (RR) by doing a full modeset
6061 * (may appear as a blink on screen) and is used in dock-undock scenario.
6062 * Seamless DRRS involves changing RR without any visual effect to the user
6063 * and can be used during normal system usage. This is done by programming
6064 * certain registers.
6065 *
6066 * Support for static/seamless DRRS may be indicated in the VBT based on
6067 * inputs from the panel spec.
6068 *
6069 * DRRS saves power by switching to low RR based on usage scenarios.
6070 *
2e7a5701
DV
6071 * The implementation is based on frontbuffer tracking implementation. When
6072 * there is a disturbance on the screen triggered by user activity or a periodic
6073 * system activity, DRRS is disabled (RR is changed to high RR). When there is
6074 * no movement on screen, after a timeout of 1 second, a switch to low RR is
6075 * made.
6076 *
6077 * For integration with frontbuffer tracking code, intel_edp_drrs_invalidate()
6078 * and intel_edp_drrs_flush() are called.
b33a2815
VK
6079 *
6080 * DRRS can be further extended to support other internal panels and also
6081 * the scenario of video playback wherein RR is set based on the rate
6082 * requested by userspace.
6083 */
6084
6085/**
6086 * intel_dp_drrs_init - Init basic DRRS work and mutex.
2f773477 6087 * @connector: eDP connector
b33a2815
VK
6088 * @fixed_mode: preferred mode of panel
6089 *
6090 * This function is called only once at driver load to initialize basic
6091 * DRRS stuff.
6092 *
6093 * Returns:
6094 * Downclock mode if panel supports it, else return NULL.
6095 * DRRS support is determined by the presence of downclock mode (apart
6096 * from VBT setting).
6097 */
4f9db5b5 6098static struct drm_display_mode *
2f773477
VS
6099intel_dp_drrs_init(struct intel_connector *connector,
6100 struct drm_display_mode *fixed_mode)
4f9db5b5 6101{
2f773477 6102 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
4f9db5b5
PB
6103 struct drm_display_mode *downclock_mode = NULL;
6104
9da7d693
DV
6105 INIT_DELAYED_WORK(&dev_priv->drrs.work, intel_edp_drrs_downclock_work);
6106 mutex_init(&dev_priv->drrs.mutex);
6107
dd11bc10 6108 if (INTEL_GEN(dev_priv) <= 6) {
4f9db5b5
PB
6109 DRM_DEBUG_KMS("DRRS supported for Gen7 and above\n");
6110 return NULL;
6111 }
6112
6113 if (dev_priv->vbt.drrs_type != SEAMLESS_DRRS_SUPPORT) {
4079b8d1 6114 DRM_DEBUG_KMS("VBT doesn't support DRRS\n");
4f9db5b5
PB
6115 return NULL;
6116 }
6117
2f773477
VS
6118 downclock_mode = intel_find_panel_downclock(dev_priv, fixed_mode,
6119 &connector->base);
4f9db5b5
PB
6120
6121 if (!downclock_mode) {
a1d26342 6122 DRM_DEBUG_KMS("Downclock mode is not found. DRRS not supported\n");
4f9db5b5
PB
6123 return NULL;
6124 }
6125
96178eeb 6126 dev_priv->drrs.type = dev_priv->vbt.drrs_type;
4f9db5b5 6127
96178eeb 6128 dev_priv->drrs.refresh_rate_type = DRRS_HIGH_RR;
4079b8d1 6129 DRM_DEBUG_KMS("seamless DRRS supported for eDP panel.\n");
4f9db5b5
PB
6130 return downclock_mode;
6131}
6132
ed92f0b2 6133static bool intel_edp_init_connector(struct intel_dp *intel_dp,
36b5f425 6134 struct intel_connector *intel_connector)
ed92f0b2 6135{
2f773477 6136 struct drm_device *dev = intel_dp_to_dev(intel_dp);
fac5e23e 6137 struct drm_i915_private *dev_priv = to_i915(dev);
2f773477 6138 struct drm_connector *connector = &intel_connector->base;
ed92f0b2 6139 struct drm_display_mode *fixed_mode = NULL;
dc911f5b 6140 struct drm_display_mode *alt_fixed_mode = NULL;
4f9db5b5 6141 struct drm_display_mode *downclock_mode = NULL;
ed92f0b2
PZ
6142 bool has_dpcd;
6143 struct drm_display_mode *scan;
6144 struct edid *edid;
6517d273 6145 enum pipe pipe = INVALID_PIPE;
ed92f0b2 6146
1853a9da 6147 if (!intel_dp_is_edp(intel_dp))
ed92f0b2
PZ
6148 return true;
6149
97a824e1
ID
6150 /*
6151 * On IBX/CPT we may get here with LVDS already registered. Since the
6152 * driver uses the only internal power sequencer available for both
6153 * eDP and LVDS bail out early in this case to prevent interfering
6154 * with an already powered-on LVDS power sequencer.
6155 */
2f773477 6156 if (intel_get_lvds_encoder(&dev_priv->drm)) {
97a824e1
ID
6157 WARN_ON(!(HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)));
6158 DRM_INFO("LVDS was detected, not registering eDP\n");
6159
6160 return false;
6161 }
6162
49e6bc51 6163 pps_lock(intel_dp);
b4d06ede
ID
6164
6165 intel_dp_init_panel_power_timestamps(intel_dp);
46bd8383 6166 intel_dp_pps_init(intel_dp);
49e6bc51 6167 intel_edp_panel_vdd_sanitize(intel_dp);
b4d06ede 6168
49e6bc51 6169 pps_unlock(intel_dp);
63635217 6170
ed92f0b2 6171 /* Cache DPCD and EDID for edp. */
fe5a66f9 6172 has_dpcd = intel_edp_init_dpcd(intel_dp);
ed92f0b2 6173
fe5a66f9 6174 if (!has_dpcd) {
ed92f0b2
PZ
6175 /* if this fails, presume the device is a ghost */
6176 DRM_INFO("failed to retrieve link info, disabling eDP\n");
b4d06ede 6177 goto out_vdd_off;
ed92f0b2
PZ
6178 }
6179
060c8778 6180 mutex_lock(&dev->mode_config.mutex);
0b99836f 6181 edid = drm_get_edid(connector, &intel_dp->aux.ddc);
ed92f0b2
PZ
6182 if (edid) {
6183 if (drm_add_edid_modes(connector, edid)) {
6184 drm_mode_connector_update_edid_property(connector,
6185 edid);
ed92f0b2
PZ
6186 } else {
6187 kfree(edid);
6188 edid = ERR_PTR(-EINVAL);
6189 }
6190 } else {
6191 edid = ERR_PTR(-ENOENT);
6192 }
6193 intel_connector->edid = edid;
6194
dc911f5b 6195 /* prefer fixed mode from EDID if available, save an alt mode also */
ed92f0b2
PZ
6196 list_for_each_entry(scan, &connector->probed_modes, head) {
6197 if ((scan->type & DRM_MODE_TYPE_PREFERRED)) {
6198 fixed_mode = drm_mode_duplicate(dev, scan);
4f9db5b5 6199 downclock_mode = intel_dp_drrs_init(
4f9db5b5 6200 intel_connector, fixed_mode);
dc911f5b
JB
6201 } else if (!alt_fixed_mode) {
6202 alt_fixed_mode = drm_mode_duplicate(dev, scan);
ed92f0b2
PZ
6203 }
6204 }
6205
6206 /* fallback to VBT if available for eDP */
6207 if (!fixed_mode && dev_priv->vbt.lfp_lvds_vbt_mode) {
6208 fixed_mode = drm_mode_duplicate(dev,
6209 dev_priv->vbt.lfp_lvds_vbt_mode);
df457245 6210 if (fixed_mode) {
ed92f0b2 6211 fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
df457245
VS
6212 connector->display_info.width_mm = fixed_mode->width_mm;
6213 connector->display_info.height_mm = fixed_mode->height_mm;
6214 }
ed92f0b2 6215 }
060c8778 6216 mutex_unlock(&dev->mode_config.mutex);
ed92f0b2 6217
920a14b2 6218 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
01527b31
CT
6219 intel_dp->edp_notifier.notifier_call = edp_notify_handler;
6220 register_reboot_notifier(&intel_dp->edp_notifier);
6517d273
VS
6221
6222 /*
6223 * Figure out the current pipe for the initial backlight setup.
6224 * If the current pipe isn't valid, try the PPS pipe, and if that
6225 * fails just assume pipe A.
6226 */
9f2bdb00 6227 pipe = vlv_active_pipe(intel_dp);
6517d273
VS
6228
6229 if (pipe != PIPE_A && pipe != PIPE_B)
6230 pipe = intel_dp->pps_pipe;
6231
6232 if (pipe != PIPE_A && pipe != PIPE_B)
6233 pipe = PIPE_A;
6234
6235 DRM_DEBUG_KMS("using pipe %c for initial backlight setup\n",
6236 pipe_name(pipe));
01527b31
CT
6237 }
6238
dc911f5b
JB
6239 intel_panel_init(&intel_connector->panel, fixed_mode, alt_fixed_mode,
6240 downclock_mode);
5507faeb 6241 intel_connector->panel.backlight.power = intel_edp_backlight_power;
6517d273 6242 intel_panel_setup_backlight(connector, pipe);
ed92f0b2
PZ
6243
6244 return true;
b4d06ede
ID
6245
6246out_vdd_off:
6247 cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
6248 /*
6249 * vdd might still be enabled do to the delayed vdd off.
6250 * Make sure vdd is actually turned off here.
6251 */
6252 pps_lock(intel_dp);
6253 edp_panel_vdd_off_sync(intel_dp);
6254 pps_unlock(intel_dp);
6255
6256 return false;
ed92f0b2
PZ
6257}
6258
9301397a
MN
6259static void intel_dp_modeset_retry_work_fn(struct work_struct *work)
6260{
6261 struct intel_connector *intel_connector;
6262 struct drm_connector *connector;
6263
6264 intel_connector = container_of(work, typeof(*intel_connector),
6265 modeset_retry_work);
6266 connector = &intel_connector->base;
6267 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
6268 connector->name);
6269
6270 /* Grab the locks before changing connector property*/
6271 mutex_lock(&connector->dev->mode_config.mutex);
6272 /* Set connector link status to BAD and send a Uevent to notify
6273 * userspace to do a modeset.
6274 */
6275 drm_mode_connector_set_link_status_property(connector,
6276 DRM_MODE_LINK_STATUS_BAD);
6277 mutex_unlock(&connector->dev->mode_config.mutex);
6278 /* Send Hotplug uevent so userspace can reprobe */
6279 drm_kms_helper_hotplug_event(connector->dev);
6280}
6281
16c25533 6282bool
f0fec3f2
PZ
6283intel_dp_init_connector(struct intel_digital_port *intel_dig_port,
6284 struct intel_connector *intel_connector)
a4fc5ed6 6285{
f0fec3f2
PZ
6286 struct drm_connector *connector = &intel_connector->base;
6287 struct intel_dp *intel_dp = &intel_dig_port->dp;
6288 struct intel_encoder *intel_encoder = &intel_dig_port->base;
6289 struct drm_device *dev = intel_encoder->base.dev;
fac5e23e 6290 struct drm_i915_private *dev_priv = to_i915(dev);
8f4f2797 6291 enum port port = intel_encoder->port;
7a418e34 6292 int type;
a4fc5ed6 6293
9301397a
MN
6294 /* Initialize the work for modeset in case of link train failure */
6295 INIT_WORK(&intel_connector->modeset_retry_work,
6296 intel_dp_modeset_retry_work_fn);
6297
ccb1a831
VS
6298 if (WARN(intel_dig_port->max_lanes < 1,
6299 "Not enough lanes (%d) for DP on port %c\n",
6300 intel_dig_port->max_lanes, port_name(port)))
6301 return false;
6302
55cfc580
JN
6303 intel_dp_set_source_rates(intel_dp);
6304
d7e8ef02 6305 intel_dp->reset_link_params = true;
a4a5d2f8 6306 intel_dp->pps_pipe = INVALID_PIPE;
9f2bdb00 6307 intel_dp->active_pipe = INVALID_PIPE;
a4a5d2f8 6308
ec5b01dd 6309 /* intel_dp vfuncs */
4f8036a2 6310 if (HAS_DDI(dev_priv))
ad64217b
ACO
6311 intel_dp->prepare_link_retrain = intel_ddi_prepare_link_retrain;
6312
0767935e
DV
6313 /* Preserve the current hw state. */
6314 intel_dp->DP = I915_READ(intel_dp->output_reg);
dd06f90e 6315 intel_dp->attached_connector = intel_connector;
3d3dc149 6316
7b91bf7f 6317 if (intel_dp_is_port_edp(dev_priv, port))
b329530c 6318 type = DRM_MODE_CONNECTOR_eDP;
3b32a35b
VS
6319 else
6320 type = DRM_MODE_CONNECTOR_DisplayPort;
b329530c 6321
9f2bdb00
VS
6322 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
6323 intel_dp->active_pipe = vlv_active_pipe(intel_dp);
6324
f7d24902
ID
6325 /*
6326 * For eDP we always set the encoder type to INTEL_OUTPUT_EDP, but
6327 * for DP the encoder type can be set by the caller to
6328 * INTEL_OUTPUT_UNKNOWN for DDI, so don't rewrite it.
6329 */
6330 if (type == DRM_MODE_CONNECTOR_eDP)
6331 intel_encoder->type = INTEL_OUTPUT_EDP;
6332
c17ed5b5 6333 /* eDP only on port B and/or C on vlv/chv */
920a14b2 6334 if (WARN_ON((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
1853a9da
JN
6335 intel_dp_is_edp(intel_dp) &&
6336 port != PORT_B && port != PORT_C))
c17ed5b5
VS
6337 return false;
6338
e7281eab
ID
6339 DRM_DEBUG_KMS("Adding %s connector on port %c\n",
6340 type == DRM_MODE_CONNECTOR_eDP ? "eDP" : "DP",
6341 port_name(port));
6342
b329530c 6343 drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
a4fc5ed6
KP
6344 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
6345
05021389
VS
6346 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
6347 connector->interlace_allowed = true;
a4fc5ed6
KP
6348 connector->doublescan_allowed = 0;
6349
bdabdb63 6350 intel_encoder->hpd_pin = intel_hpd_pin_default(dev_priv, port);
5432fcaf 6351
b6339585 6352 intel_dp_aux_init(intel_dp);
7a418e34 6353
f0fec3f2 6354 INIT_DELAYED_WORK(&intel_dp->panel_vdd_work,
4be73780 6355 edp_panel_vdd_work);
a4fc5ed6 6356
df0e9248 6357 intel_connector_attach_encoder(intel_connector, intel_encoder);
a4fc5ed6 6358
4f8036a2 6359 if (HAS_DDI(dev_priv))
bcbc889b
PZ
6360 intel_connector->get_hw_state = intel_ddi_connector_get_hw_state;
6361 else
6362 intel_connector->get_hw_state = intel_connector_get_hw_state;
6363
0e32b39c 6364 /* init MST on ports that can support it */
1853a9da 6365 if (HAS_DP_MST(dev_priv) && !intel_dp_is_edp(intel_dp) &&
9787e835
RV
6366 (port == PORT_B || port == PORT_C ||
6367 port == PORT_D || port == PORT_F))
0c9b3715
JN
6368 intel_dp_mst_encoder_init(intel_dig_port,
6369 intel_connector->base.base.id);
0e32b39c 6370
36b5f425 6371 if (!intel_edp_init_connector(intel_dp, intel_connector)) {
a121f4e5
VS
6372 intel_dp_aux_fini(intel_dp);
6373 intel_dp_mst_encoder_cleanup(intel_dig_port);
6374 goto fail;
b2f246a8 6375 }
32f9d658 6376
f684960e 6377 intel_dp_add_properties(intel_dp, connector);
20f24d77 6378
fdddd08c 6379 if (is_hdcp_supported(dev_priv, port) && !intel_dp_is_edp(intel_dp)) {
20f24d77
SP
6380 int ret = intel_hdcp_init(intel_connector, &intel_dp_hdcp_shim);
6381 if (ret)
6382 DRM_DEBUG_KMS("HDCP init failed, skipping.\n");
6383 }
f684960e 6384
a4fc5ed6
KP
6385 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
6386 * 0xd. Failure to do so will result in spurious interrupts being
6387 * generated on the port when a cable is not attached.
6388 */
50a0bc90 6389 if (IS_G4X(dev_priv) && !IS_GM45(dev_priv)) {
a4fc5ed6
KP
6390 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
6391 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
6392 }
16c25533
PZ
6393
6394 return true;
a121f4e5
VS
6395
6396fail:
a121f4e5
VS
6397 drm_connector_cleanup(connector);
6398
6399 return false;
a4fc5ed6 6400}
f0fec3f2 6401
c39055b0 6402bool intel_dp_init(struct drm_i915_private *dev_priv,
457c52d8
CW
6403 i915_reg_t output_reg,
6404 enum port port)
f0fec3f2
PZ
6405{
6406 struct intel_digital_port *intel_dig_port;
6407 struct intel_encoder *intel_encoder;
6408 struct drm_encoder *encoder;
6409 struct intel_connector *intel_connector;
6410
b14c5679 6411 intel_dig_port = kzalloc(sizeof(*intel_dig_port), GFP_KERNEL);
f0fec3f2 6412 if (!intel_dig_port)
457c52d8 6413 return false;
f0fec3f2 6414
08d9bc92 6415 intel_connector = intel_connector_alloc();
11aee0f6
SM
6416 if (!intel_connector)
6417 goto err_connector_alloc;
f0fec3f2
PZ
6418
6419 intel_encoder = &intel_dig_port->base;
6420 encoder = &intel_encoder->base;
6421
c39055b0
ACO
6422 if (drm_encoder_init(&dev_priv->drm, &intel_encoder->base,
6423 &intel_dp_enc_funcs, DRM_MODE_ENCODER_TMDS,
6424 "DP %c", port_name(port)))
893da0c9 6425 goto err_encoder_init;
f0fec3f2 6426
c85d200e 6427 intel_encoder->hotplug = intel_dp_hotplug;
5bfe2ac0 6428 intel_encoder->compute_config = intel_dp_compute_config;
00c09d70 6429 intel_encoder->get_hw_state = intel_dp_get_hw_state;
045ac3b5 6430 intel_encoder->get_config = intel_dp_get_config;
07f9cd0b 6431 intel_encoder->suspend = intel_dp_encoder_suspend;
920a14b2 6432 if (IS_CHERRYVIEW(dev_priv)) {
9197c88b 6433 intel_encoder->pre_pll_enable = chv_dp_pre_pll_enable;
e4a1d846
CML
6434 intel_encoder->pre_enable = chv_pre_enable_dp;
6435 intel_encoder->enable = vlv_enable_dp;
1a8ff607 6436 intel_encoder->disable = vlv_disable_dp;
580d3811 6437 intel_encoder->post_disable = chv_post_disable_dp;
d6db995f 6438 intel_encoder->post_pll_disable = chv_dp_post_pll_disable;
11a914c2 6439 } else if (IS_VALLEYVIEW(dev_priv)) {
ecff4f3b 6440 intel_encoder->pre_pll_enable = vlv_dp_pre_pll_enable;
ab1f90f9
JN
6441 intel_encoder->pre_enable = vlv_pre_enable_dp;
6442 intel_encoder->enable = vlv_enable_dp;
1a8ff607 6443 intel_encoder->disable = vlv_disable_dp;
49277c31 6444 intel_encoder->post_disable = vlv_post_disable_dp;
1a8ff607
VS
6445 } else if (INTEL_GEN(dev_priv) >= 5) {
6446 intel_encoder->pre_enable = g4x_pre_enable_dp;
6447 intel_encoder->enable = g4x_enable_dp;
6448 intel_encoder->disable = ilk_disable_dp;
6449 intel_encoder->post_disable = ilk_post_disable_dp;
ab1f90f9 6450 } else {
ecff4f3b
JN
6451 intel_encoder->pre_enable = g4x_pre_enable_dp;
6452 intel_encoder->enable = g4x_enable_dp;
1a8ff607 6453 intel_encoder->disable = g4x_disable_dp;
ab1f90f9 6454 }
f0fec3f2 6455
f0fec3f2 6456 intel_dig_port->dp.output_reg = output_reg;
ccb1a831 6457 intel_dig_port->max_lanes = 4;
f0fec3f2 6458
cca0502b 6459 intel_encoder->type = INTEL_OUTPUT_DP;
79f255a0 6460 intel_encoder->power_domain = intel_port_to_power_domain(port);
920a14b2 6461 if (IS_CHERRYVIEW(dev_priv)) {
882ec384
VS
6462 if (port == PORT_D)
6463 intel_encoder->crtc_mask = 1 << 2;
6464 else
6465 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
6466 } else {
6467 intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
6468 }
bc079e8b 6469 intel_encoder->cloneable = 0;
03cdc1d4 6470 intel_encoder->port = port;
f0fec3f2 6471
13cf5504 6472 intel_dig_port->hpd_pulse = intel_dp_hpd_pulse;
5fcece80 6473 dev_priv->hotplug.irq_port[port] = intel_dig_port;
13cf5504 6474
385e4de0
VS
6475 if (port != PORT_A)
6476 intel_infoframe_init(intel_dig_port);
6477
11aee0f6
SM
6478 if (!intel_dp_init_connector(intel_dig_port, intel_connector))
6479 goto err_init_connector;
6480
457c52d8 6481 return true;
11aee0f6
SM
6482
6483err_init_connector:
6484 drm_encoder_cleanup(encoder);
893da0c9 6485err_encoder_init:
11aee0f6
SM
6486 kfree(intel_connector);
6487err_connector_alloc:
6488 kfree(intel_dig_port);
457c52d8 6489 return false;
f0fec3f2 6490}
0e32b39c
DA
6491
6492void intel_dp_mst_suspend(struct drm_device *dev)
6493{
fac5e23e 6494 struct drm_i915_private *dev_priv = to_i915(dev);
0e32b39c
DA
6495 int i;
6496
6497 /* disable MST */
6498 for (i = 0; i < I915_MAX_PORTS; i++) {
5fcece80 6499 struct intel_digital_port *intel_dig_port = dev_priv->hotplug.irq_port[i];
5aa56969
VS
6500
6501 if (!intel_dig_port || !intel_dig_port->dp.can_mst)
0e32b39c
DA
6502 continue;
6503
5aa56969
VS
6504 if (intel_dig_port->dp.is_mst)
6505 drm_dp_mst_topology_mgr_suspend(&intel_dig_port->dp.mst_mgr);
0e32b39c
DA
6506 }
6507}
6508
6509void intel_dp_mst_resume(struct drm_device *dev)
6510{
fac5e23e 6511 struct drm_i915_private *dev_priv = to_i915(dev);
0e32b39c
DA
6512 int i;
6513
6514 for (i = 0; i < I915_MAX_PORTS; i++) {
5fcece80 6515 struct intel_digital_port *intel_dig_port = dev_priv->hotplug.irq_port[i];
5aa56969 6516 int ret;
0e32b39c 6517
5aa56969
VS
6518 if (!intel_dig_port || !intel_dig_port->dp.can_mst)
6519 continue;
0e32b39c 6520
5aa56969
VS
6521 ret = drm_dp_mst_topology_mgr_resume(&intel_dig_port->dp.mst_mgr);
6522 if (ret)
6523 intel_dp_check_mst_status(&intel_dig_port->dp);
0e32b39c
DA
6524 }
6525}