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