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