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