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