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