linux/drivers/gpu/drm/i915/display/intel_dp.c
<<
>>
Prefs
   1/*
   2 * Copyright © 2008 Intel Corporation
   3 *
   4 * Permission is hereby granted, free of charge, to any person obtaining a
   5 * copy of this software and associated documentation files (the "Software"),
   6 * to deal in the Software without restriction, including without limitation
   7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
   8 * and/or sell copies of the Software, and to permit persons to whom the
   9 * Software is furnished to do so, subject to the following conditions:
  10 *
  11 * The above copyright notice and this permission notice (including the next
  12 * paragraph) shall be included in all copies or substantial portions of the
  13 * Software.
  14 *
  15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21 * IN THE SOFTWARE.
  22 *
  23 * Authors:
  24 *    Keith Packard <keithp@keithp.com>
  25 *
  26 */
  27
  28#include <linux/export.h>
  29#include <linux/i2c.h>
  30#include <linux/notifier.h>
  31#include <linux/slab.h>
  32#include <linux/timekeeping.h>
  33#include <linux/types.h>
  34
  35#include <asm/byteorder.h>
  36
  37#include <drm/drm_atomic_helper.h>
  38#include <drm/drm_crtc.h>
  39#include <drm/drm_dp_helper.h>
  40#include <drm/drm_edid.h>
  41#include <drm/drm_probe_helper.h>
  42
  43#include "g4x_dp.h"
  44#include "i915_debugfs.h"
  45#include "i915_drv.h"
  46#include "intel_atomic.h"
  47#include "intel_audio.h"
  48#include "intel_backlight.h"
  49#include "intel_connector.h"
  50#include "intel_crtc.h"
  51#include "intel_ddi.h"
  52#include "intel_de.h"
  53#include "intel_display_types.h"
  54#include "intel_dp.h"
  55#include "intel_dp_aux.h"
  56#include "intel_dp_hdcp.h"
  57#include "intel_dp_link_training.h"
  58#include "intel_dp_mst.h"
  59#include "intel_dpio_phy.h"
  60#include "intel_dpll.h"
  61#include "intel_drrs.h"
  62#include "intel_fifo_underrun.h"
  63#include "intel_hdcp.h"
  64#include "intel_hdmi.h"
  65#include "intel_hotplug.h"
  66#include "intel_lspcon.h"
  67#include "intel_lvds.h"
  68#include "intel_panel.h"
  69#include "intel_pps.h"
  70#include "intel_psr.h"
  71#include "intel_tc.h"
  72#include "intel_vdsc.h"
  73#include "intel_vrr.h"
  74
  75#define DP_DPRX_ESI_LEN 14
  76
  77/* DP DSC throughput values used for slice count calculations KPixels/s */
  78#define DP_DSC_PEAK_PIXEL_RATE                  2720000
  79#define DP_DSC_MAX_ENC_THROUGHPUT_0             340000
  80#define DP_DSC_MAX_ENC_THROUGHPUT_1             400000
  81
  82/* DP DSC FEC Overhead factor = 1/(0.972261) */
  83#define DP_DSC_FEC_OVERHEAD_FACTOR              972261
  84
  85/* Compliance test status bits  */
  86#define INTEL_DP_RESOLUTION_SHIFT_MASK  0
  87#define INTEL_DP_RESOLUTION_PREFERRED   (1 << INTEL_DP_RESOLUTION_SHIFT_MASK)
  88#define INTEL_DP_RESOLUTION_STANDARD    (2 << INTEL_DP_RESOLUTION_SHIFT_MASK)
  89#define INTEL_DP_RESOLUTION_FAILSAFE    (3 << INTEL_DP_RESOLUTION_SHIFT_MASK)
  90
  91
  92/* Constants for DP DSC configurations */
  93static const u8 valid_dsc_bpp[] = {6, 8, 10, 12, 15};
  94
  95/* With Single pipe configuration, HW is capable of supporting maximum
  96 * of 4 slices per line.
  97 */
  98static const u8 valid_dsc_slicecount[] = {1, 2, 4};
  99
 100/**
 101 * intel_dp_is_edp - is the given port attached to an eDP panel (either CPU or PCH)
 102 * @intel_dp: DP struct
 103 *
 104 * If a CPU or PCH DP output is attached to an eDP panel, this function
 105 * will return true, and false otherwise.
 106 *
 107 * This function is not safe to use prior to encoder type being set.
 108 */
 109bool intel_dp_is_edp(struct intel_dp *intel_dp)
 110{
 111        struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
 112
 113        return dig_port->base.type == INTEL_OUTPUT_EDP;
 114}
 115
 116static void intel_dp_unset_edid(struct intel_dp *intel_dp);
 117static int intel_dp_dsc_compute_bpp(struct intel_dp *intel_dp, u8 dsc_max_bpc);
 118
 119/* Is link rate UHBR and thus 128b/132b? */
 120bool intel_dp_is_uhbr(const struct intel_crtc_state *crtc_state)
 121{
 122        return crtc_state->port_clock >= 1000000;
 123}
 124
 125static void intel_dp_set_default_sink_rates(struct intel_dp *intel_dp)
 126{
 127        intel_dp->sink_rates[0] = 162000;
 128        intel_dp->num_sink_rates = 1;
 129}
 130
 131/* update sink rates from dpcd */
 132static void intel_dp_set_dpcd_sink_rates(struct intel_dp *intel_dp)
 133{
 134        static const int dp_rates[] = {
 135                162000, 270000, 540000, 810000
 136        };
 137        int i, max_rate;
 138        int max_lttpr_rate;
 139
 140        if (drm_dp_has_quirk(&intel_dp->desc, DP_DPCD_QUIRK_CAN_DO_MAX_LINK_RATE_3_24_GBPS)) {
 141                /* Needed, e.g., for Apple MBP 2017, 15 inch eDP Retina panel */
 142                static const int quirk_rates[] = { 162000, 270000, 324000 };
 143
 144                memcpy(intel_dp->sink_rates, quirk_rates, sizeof(quirk_rates));
 145                intel_dp->num_sink_rates = ARRAY_SIZE(quirk_rates);
 146
 147                return;
 148        }
 149
 150        /*
 151         * Sink rates for 8b/10b.
 152         */
 153        max_rate = drm_dp_bw_code_to_link_rate(intel_dp->dpcd[DP_MAX_LINK_RATE]);
 154        max_lttpr_rate = drm_dp_lttpr_max_link_rate(intel_dp->lttpr_common_caps);
 155        if (max_lttpr_rate)
 156                max_rate = min(max_rate, max_lttpr_rate);
 157
 158        for (i = 0; i < ARRAY_SIZE(dp_rates); i++) {
 159                if (dp_rates[i] > max_rate)
 160                        break;
 161                intel_dp->sink_rates[i] = dp_rates[i];
 162        }
 163
 164        /*
 165         * Sink rates for 128b/132b. If set, sink should support all 8b/10b
 166         * rates and 10 Gbps.
 167         */
 168        if (intel_dp->dpcd[DP_MAIN_LINK_CHANNEL_CODING] & DP_CAP_ANSI_128B132B) {
 169                u8 uhbr_rates = 0;
 170
 171                BUILD_BUG_ON(ARRAY_SIZE(intel_dp->sink_rates) < ARRAY_SIZE(dp_rates) + 3);
 172
 173                drm_dp_dpcd_readb(&intel_dp->aux,
 174                                  DP_128B132B_SUPPORTED_LINK_RATES, &uhbr_rates);
 175
 176                if (drm_dp_lttpr_count(intel_dp->lttpr_common_caps)) {
 177                        /* We have a repeater */
 178                        if (intel_dp->lttpr_common_caps[0] >= 0x20 &&
 179                            intel_dp->lttpr_common_caps[DP_MAIN_LINK_CHANNEL_CODING_PHY_REPEATER -
 180                                                        DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV] &
 181                            DP_PHY_REPEATER_128B132B_SUPPORTED) {
 182                                /* Repeater supports 128b/132b, valid UHBR rates */
 183                                uhbr_rates &= intel_dp->lttpr_common_caps[DP_PHY_REPEATER_128B132B_RATES -
 184                                                                          DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
 185                        } else {
 186                                /* Does not support 128b/132b */
 187                                uhbr_rates = 0;
 188                        }
 189                }
 190
 191                if (uhbr_rates & DP_UHBR10)
 192                        intel_dp->sink_rates[i++] = 1000000;
 193                if (uhbr_rates & DP_UHBR13_5)
 194                        intel_dp->sink_rates[i++] = 1350000;
 195                if (uhbr_rates & DP_UHBR20)
 196                        intel_dp->sink_rates[i++] = 2000000;
 197        }
 198
 199        intel_dp->num_sink_rates = i;
 200}
 201
 202static void intel_dp_set_sink_rates(struct intel_dp *intel_dp)
 203{
 204        struct intel_connector *connector = intel_dp->attached_connector;
 205        struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
 206        struct intel_encoder *encoder = &intel_dig_port->base;
 207
 208        intel_dp_set_dpcd_sink_rates(intel_dp);
 209
 210        if (intel_dp->num_sink_rates)
 211                return;
 212
 213        drm_err(&dp_to_i915(intel_dp)->drm,
 214                "[CONNECTOR:%d:%s][ENCODER:%d:%s] Invalid DPCD with no link rates, using defaults\n",
 215                connector->base.base.id, connector->base.name,
 216                encoder->base.base.id, encoder->base.name);
 217
 218        intel_dp_set_default_sink_rates(intel_dp);
 219}
 220
 221static void intel_dp_set_default_max_sink_lane_count(struct intel_dp *intel_dp)
 222{
 223        intel_dp->max_sink_lane_count = 1;
 224}
 225
 226static void intel_dp_set_max_sink_lane_count(struct intel_dp *intel_dp)
 227{
 228        struct intel_connector *connector = intel_dp->attached_connector;
 229        struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
 230        struct intel_encoder *encoder = &intel_dig_port->base;
 231
 232        intel_dp->max_sink_lane_count = drm_dp_max_lane_count(intel_dp->dpcd);
 233
 234        switch (intel_dp->max_sink_lane_count) {
 235        case 1:
 236        case 2:
 237        case 4:
 238                return;
 239        }
 240
 241        drm_err(&dp_to_i915(intel_dp)->drm,
 242                "[CONNECTOR:%d:%s][ENCODER:%d:%s] Invalid DPCD max lane count (%d), using default\n",
 243                connector->base.base.id, connector->base.name,
 244                encoder->base.base.id, encoder->base.name,
 245                intel_dp->max_sink_lane_count);
 246
 247        intel_dp_set_default_max_sink_lane_count(intel_dp);
 248}
 249
 250/* Get length of rates array potentially limited by max_rate. */
 251static int intel_dp_rate_limit_len(const int *rates, int len, int max_rate)
 252{
 253        int i;
 254
 255        /* Limit results by potentially reduced max rate */
 256        for (i = 0; i < len; i++) {
 257                if (rates[len - i - 1] <= max_rate)
 258                        return len - i;
 259        }
 260
 261        return 0;
 262}
 263
 264/* Get length of common rates array potentially limited by max_rate. */
 265static int intel_dp_common_len_rate_limit(const struct intel_dp *intel_dp,
 266                                          int max_rate)
 267{
 268        return intel_dp_rate_limit_len(intel_dp->common_rates,
 269                                       intel_dp->num_common_rates, max_rate);
 270}
 271
 272static int intel_dp_common_rate(struct intel_dp *intel_dp, int index)
 273{
 274        if (drm_WARN_ON(&dp_to_i915(intel_dp)->drm,
 275                        index < 0 || index >= intel_dp->num_common_rates))
 276                return 162000;
 277
 278        return intel_dp->common_rates[index];
 279}
 280
 281/* Theoretical max between source and sink */
 282static int intel_dp_max_common_rate(struct intel_dp *intel_dp)
 283{
 284        return intel_dp_common_rate(intel_dp, intel_dp->num_common_rates - 1);
 285}
 286
 287/* Theoretical max between source and sink */
 288static int intel_dp_max_common_lane_count(struct intel_dp *intel_dp)
 289{
 290        struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
 291        int source_max = dig_port->max_lanes;
 292        int sink_max = intel_dp->max_sink_lane_count;
 293        int fia_max = intel_tc_port_fia_max_lane_count(dig_port);
 294        int lttpr_max = drm_dp_lttpr_max_lane_count(intel_dp->lttpr_common_caps);
 295
 296        if (lttpr_max)
 297                sink_max = min(sink_max, lttpr_max);
 298
 299        return min3(source_max, sink_max, fia_max);
 300}
 301
 302int intel_dp_max_lane_count(struct intel_dp *intel_dp)
 303{
 304        switch (intel_dp->max_link_lane_count) {
 305        case 1:
 306        case 2:
 307        case 4:
 308                return intel_dp->max_link_lane_count;
 309        default:
 310                MISSING_CASE(intel_dp->max_link_lane_count);
 311                return 1;
 312        }
 313}
 314
 315/*
 316 * The required data bandwidth for a mode with given pixel clock and bpp. This
 317 * is the required net bandwidth independent of the data bandwidth efficiency.
 318 */
 319int
 320intel_dp_link_required(int pixel_clock, int bpp)
 321{
 322        /* pixel_clock is in kHz, divide bpp by 8 for bit to Byte conversion */
 323        return DIV_ROUND_UP(pixel_clock * bpp, 8);
 324}
 325
 326/*
 327 * Given a link rate and lanes, get the data bandwidth.
 328 *
 329 * Data bandwidth is the actual payload rate, which depends on the data
 330 * bandwidth efficiency and the link rate.
 331 *
 332 * For 8b/10b channel encoding, SST and non-FEC, the data bandwidth efficiency
 333 * is 80%. For example, for a 1.62 Gbps link, 1.62*10^9 bps * 0.80 * (1/8) =
 334 * 162000 kBps. With 8-bit symbols, we have 162000 kHz symbol clock. Just by
 335 * coincidence, the port clock in kHz matches the data bandwidth in kBps, and
 336 * they equal the link bit rate in Gbps multiplied by 100000. (Note that this no
 337 * longer holds for data bandwidth as soon as FEC or MST is taken into account!)
 338 *
 339 * For 128b/132b channel encoding, the data bandwidth efficiency is 96.71%. For
 340 * example, for a 10 Gbps link, 10*10^9 bps * 0.9671 * (1/8) = 1208875
 341 * kBps. With 32-bit symbols, we have 312500 kHz symbol clock. The value 1000000
 342 * does not match the symbol clock, the port clock (not even if you think in
 343 * terms of a byte clock), nor the data bandwidth. It only matches the link bit
 344 * rate in units of 10000 bps.
 345 */
 346int
 347intel_dp_max_data_rate(int max_link_rate, int max_lanes)
 348{
 349        if (max_link_rate >= 1000000) {
 350                /*
 351                 * UHBR rates always use 128b/132b channel encoding, and have
 352                 * 97.71% data bandwidth efficiency. Consider max_link_rate the
 353                 * link bit rate in units of 10000 bps.
 354                 */
 355                int max_link_rate_kbps = max_link_rate * 10;
 356
 357                max_link_rate_kbps = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(max_link_rate_kbps, 9671), 10000);
 358                max_link_rate = max_link_rate_kbps / 8;
 359        }
 360
 361        /*
 362         * Lower than UHBR rates always use 8b/10b channel encoding, and have
 363         * 80% data bandwidth efficiency for SST non-FEC. However, this turns
 364         * out to be a nop by coincidence, and can be skipped:
 365         *
 366         *      int max_link_rate_kbps = max_link_rate * 10;
 367         *      max_link_rate_kbps = DIV_ROUND_CLOSEST_ULL(max_link_rate_kbps * 8, 10);
 368         *      max_link_rate = max_link_rate_kbps / 8;
 369         */
 370
 371        return max_link_rate * max_lanes;
 372}
 373
 374bool intel_dp_can_bigjoiner(struct intel_dp *intel_dp)
 375{
 376        struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
 377        struct intel_encoder *encoder = &intel_dig_port->base;
 378        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 379
 380        return DISPLAY_VER(dev_priv) >= 12 ||
 381                (DISPLAY_VER(dev_priv) == 11 &&
 382                 encoder->port != PORT_A);
 383}
 384
 385static int dg2_max_source_rate(struct intel_dp *intel_dp)
 386{
 387        return intel_dp_is_edp(intel_dp) ? 810000 : 1350000;
 388}
 389
 390static bool is_low_voltage_sku(struct drm_i915_private *i915, enum phy phy)
 391{
 392        u32 voltage;
 393
 394        voltage = intel_de_read(i915, ICL_PORT_COMP_DW3(phy)) & VOLTAGE_INFO_MASK;
 395
 396        return voltage == VOLTAGE_INFO_0_85V;
 397}
 398
 399static int icl_max_source_rate(struct intel_dp *intel_dp)
 400{
 401        struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
 402        struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
 403        enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
 404
 405        if (intel_phy_is_combo(dev_priv, phy) &&
 406            (is_low_voltage_sku(dev_priv, phy) || !intel_dp_is_edp(intel_dp)))
 407                return 540000;
 408
 409        return 810000;
 410}
 411
 412static int ehl_max_source_rate(struct intel_dp *intel_dp)
 413{
 414        struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
 415        struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
 416        enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
 417
 418        if (intel_dp_is_edp(intel_dp) || is_low_voltage_sku(dev_priv, phy))
 419                return 540000;
 420
 421        return 810000;
 422}
 423
 424static int dg1_max_source_rate(struct intel_dp *intel_dp)
 425{
 426        struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
 427        struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
 428        enum phy phy = intel_port_to_phy(i915, dig_port->base.port);
 429
 430        if (intel_phy_is_combo(i915, phy) && is_low_voltage_sku(i915, phy))
 431                return 540000;
 432
 433        return 810000;
 434}
 435
 436static void
 437intel_dp_set_source_rates(struct intel_dp *intel_dp)
 438{
 439        /* The values must be in increasing order */
 440        static const int icl_rates[] = {
 441                162000, 216000, 270000, 324000, 432000, 540000, 648000, 810000,
 442                1000000, 1350000,
 443        };
 444        static const int bxt_rates[] = {
 445                162000, 216000, 243000, 270000, 324000, 432000, 540000
 446        };
 447        static const int skl_rates[] = {
 448                162000, 216000, 270000, 324000, 432000, 540000
 449        };
 450        static const int hsw_rates[] = {
 451                162000, 270000, 540000
 452        };
 453        static const int g4x_rates[] = {
 454                162000, 270000
 455        };
 456        struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
 457        struct intel_encoder *encoder = &dig_port->base;
 458        struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
 459        const int *source_rates;
 460        int size, max_rate = 0, vbt_max_rate;
 461
 462        /* This should only be done once */
 463        drm_WARN_ON(&dev_priv->drm,
 464                    intel_dp->source_rates || intel_dp->num_source_rates);
 465
 466        if (DISPLAY_VER(dev_priv) >= 11) {
 467                source_rates = icl_rates;
 468                size = ARRAY_SIZE(icl_rates);
 469                if (IS_DG2(dev_priv))
 470                        max_rate = dg2_max_source_rate(intel_dp);
 471                else if (IS_ALDERLAKE_P(dev_priv) || IS_ALDERLAKE_S(dev_priv) ||
 472                         IS_DG1(dev_priv) || IS_ROCKETLAKE(dev_priv))
 473                        max_rate = dg1_max_source_rate(intel_dp);
 474                else if (IS_JSL_EHL(dev_priv))
 475                        max_rate = ehl_max_source_rate(intel_dp);
 476                else
 477                        max_rate = icl_max_source_rate(intel_dp);
 478        } else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
 479                source_rates = bxt_rates;
 480                size = ARRAY_SIZE(bxt_rates);
 481        } else if (DISPLAY_VER(dev_priv) == 9) {
 482                source_rates = skl_rates;
 483                size = ARRAY_SIZE(skl_rates);
 484        } else if ((IS_HASWELL(dev_priv) && !IS_HSW_ULX(dev_priv)) ||
 485                   IS_BROADWELL(dev_priv)) {
 486                source_rates = hsw_rates;
 487                size = ARRAY_SIZE(hsw_rates);
 488        } else {
 489                source_rates = g4x_rates;
 490                size = ARRAY_SIZE(g4x_rates);
 491        }
 492
 493        vbt_max_rate = intel_bios_dp_max_link_rate(encoder);
 494        if (max_rate && vbt_max_rate)
 495                max_rate = min(max_rate, vbt_max_rate);
 496        else if (vbt_max_rate)
 497                max_rate = vbt_max_rate;
 498
 499        if (max_rate)
 500                size = intel_dp_rate_limit_len(source_rates, size, max_rate);
 501
 502        intel_dp->source_rates = source_rates;
 503        intel_dp->num_source_rates = size;
 504}
 505
 506static int intersect_rates(const int *source_rates, int source_len,
 507                           const int *sink_rates, int sink_len,
 508                           int *common_rates)
 509{
 510        int i = 0, j = 0, k = 0;
 511
 512        while (i < source_len && j < sink_len) {
 513                if (source_rates[i] == sink_rates[j]) {
 514                        if (WARN_ON(k >= DP_MAX_SUPPORTED_RATES))
 515                                return k;
 516                        common_rates[k] = source_rates[i];
 517                        ++k;
 518                        ++i;
 519                        ++j;
 520                } else if (source_rates[i] < sink_rates[j]) {
 521                        ++i;
 522                } else {
 523                        ++j;
 524                }
 525        }
 526        return k;
 527}
 528
 529/* return index of rate in rates array, or -1 if not found */
 530static int intel_dp_rate_index(const int *rates, int len, int rate)
 531{
 532        int i;
 533
 534        for (i = 0; i < len; i++)
 535                if (rate == rates[i])
 536                        return i;
 537
 538        return -1;
 539}
 540
 541static void intel_dp_set_common_rates(struct intel_dp *intel_dp)
 542{
 543        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
 544
 545        drm_WARN_ON(&i915->drm,
 546                    !intel_dp->num_source_rates || !intel_dp->num_sink_rates);
 547
 548        intel_dp->num_common_rates = intersect_rates(intel_dp->source_rates,
 549                                                     intel_dp->num_source_rates,
 550                                                     intel_dp->sink_rates,
 551                                                     intel_dp->num_sink_rates,
 552                                                     intel_dp->common_rates);
 553
 554        /* Paranoia, there should always be something in common. */
 555        if (drm_WARN_ON(&i915->drm, intel_dp->num_common_rates == 0)) {
 556                intel_dp->common_rates[0] = 162000;
 557                intel_dp->num_common_rates = 1;
 558        }
 559}
 560
 561static bool intel_dp_link_params_valid(struct intel_dp *intel_dp, int link_rate,
 562                                       u8 lane_count)
 563{
 564        /*
 565         * FIXME: we need to synchronize the current link parameters with
 566         * hardware readout. Currently fast link training doesn't work on
 567         * boot-up.
 568         */
 569        if (link_rate == 0 ||
 570            link_rate > intel_dp->max_link_rate)
 571                return false;
 572
 573        if (lane_count == 0 ||
 574            lane_count > intel_dp_max_lane_count(intel_dp))
 575                return false;
 576
 577        return true;
 578}
 579
 580static bool intel_dp_can_link_train_fallback_for_edp(struct intel_dp *intel_dp,
 581                                                     int link_rate,
 582                                                     u8 lane_count)
 583{
 584        const struct drm_display_mode *fixed_mode =
 585                intel_dp->attached_connector->panel.fixed_mode;
 586        int mode_rate, max_rate;
 587
 588        mode_rate = intel_dp_link_required(fixed_mode->clock, 18);
 589        max_rate = intel_dp_max_data_rate(link_rate, lane_count);
 590        if (mode_rate > max_rate)
 591                return false;
 592
 593        return true;
 594}
 595
 596int intel_dp_get_link_train_fallback_values(struct intel_dp *intel_dp,
 597                                            int link_rate, u8 lane_count)
 598{
 599        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
 600        int index;
 601
 602        /*
 603         * TODO: Enable fallback on MST links once MST link compute can handle
 604         * the fallback params.
 605         */
 606        if (intel_dp->is_mst) {
 607                drm_err(&i915->drm, "Link Training Unsuccessful\n");
 608                return -1;
 609        }
 610
 611        if (intel_dp_is_edp(intel_dp) && !intel_dp->use_max_params) {
 612                drm_dbg_kms(&i915->drm,
 613                            "Retrying Link training for eDP with max parameters\n");
 614                intel_dp->use_max_params = true;
 615                return 0;
 616        }
 617
 618        index = intel_dp_rate_index(intel_dp->common_rates,
 619                                    intel_dp->num_common_rates,
 620                                    link_rate);
 621        if (index > 0) {
 622                if (intel_dp_is_edp(intel_dp) &&
 623                    !intel_dp_can_link_train_fallback_for_edp(intel_dp,
 624                                                              intel_dp_common_rate(intel_dp, index - 1),
 625                                                              lane_count)) {
 626                        drm_dbg_kms(&i915->drm,
 627                                    "Retrying Link training for eDP with same parameters\n");
 628                        return 0;
 629                }
 630                intel_dp->max_link_rate = intel_dp_common_rate(intel_dp, index - 1);
 631                intel_dp->max_link_lane_count = lane_count;
 632        } else if (lane_count > 1) {
 633                if (intel_dp_is_edp(intel_dp) &&
 634                    !intel_dp_can_link_train_fallback_for_edp(intel_dp,
 635                                                              intel_dp_max_common_rate(intel_dp),
 636                                                              lane_count >> 1)) {
 637                        drm_dbg_kms(&i915->drm,
 638                                    "Retrying Link training for eDP with same parameters\n");
 639                        return 0;
 640                }
 641                intel_dp->max_link_rate = intel_dp_max_common_rate(intel_dp);
 642                intel_dp->max_link_lane_count = lane_count >> 1;
 643        } else {
 644                drm_err(&i915->drm, "Link Training Unsuccessful\n");
 645                return -1;
 646        }
 647
 648        return 0;
 649}
 650
 651u32 intel_dp_mode_to_fec_clock(u32 mode_clock)
 652{
 653        return div_u64(mul_u32_u32(mode_clock, 1000000U),
 654                       DP_DSC_FEC_OVERHEAD_FACTOR);
 655}
 656
 657static int
 658small_joiner_ram_size_bits(struct drm_i915_private *i915)
 659{
 660        if (DISPLAY_VER(i915) >= 13)
 661                return 17280 * 8;
 662        else if (DISPLAY_VER(i915) >= 11)
 663                return 7680 * 8;
 664        else
 665                return 6144 * 8;
 666}
 667
 668static u16 intel_dp_dsc_get_output_bpp(struct drm_i915_private *i915,
 669                                       u32 link_clock, u32 lane_count,
 670                                       u32 mode_clock, u32 mode_hdisplay,
 671                                       bool bigjoiner,
 672                                       u32 pipe_bpp)
 673{
 674        u32 bits_per_pixel, max_bpp_small_joiner_ram;
 675        int i;
 676
 677        /*
 678         * Available Link Bandwidth(Kbits/sec) = (NumberOfLanes)*
 679         * (LinkSymbolClock)* 8 * (TimeSlotsPerMTP)
 680         * for SST -> TimeSlotsPerMTP is 1,
 681         * for MST -> TimeSlotsPerMTP has to be calculated
 682         */
 683        bits_per_pixel = (link_clock * lane_count * 8) /
 684                         intel_dp_mode_to_fec_clock(mode_clock);
 685        drm_dbg_kms(&i915->drm, "Max link bpp: %u\n", bits_per_pixel);
 686
 687        /* Small Joiner Check: output bpp <= joiner RAM (bits) / Horiz. width */
 688        max_bpp_small_joiner_ram = small_joiner_ram_size_bits(i915) /
 689                mode_hdisplay;
 690
 691        if (bigjoiner)
 692                max_bpp_small_joiner_ram *= 2;
 693
 694        drm_dbg_kms(&i915->drm, "Max small joiner bpp: %u\n",
 695                    max_bpp_small_joiner_ram);
 696
 697        /*
 698         * Greatest allowed DSC BPP = MIN (output BPP from available Link BW
 699         * check, output bpp from small joiner RAM check)
 700         */
 701        bits_per_pixel = min(bits_per_pixel, max_bpp_small_joiner_ram);
 702
 703        if (bigjoiner) {
 704                u32 max_bpp_bigjoiner =
 705                        i915->max_cdclk_freq * 48 /
 706                        intel_dp_mode_to_fec_clock(mode_clock);
 707
 708                DRM_DEBUG_KMS("Max big joiner bpp: %u\n", max_bpp_bigjoiner);
 709                bits_per_pixel = min(bits_per_pixel, max_bpp_bigjoiner);
 710        }
 711
 712        /* Error out if the max bpp is less than smallest allowed valid bpp */
 713        if (bits_per_pixel < valid_dsc_bpp[0]) {
 714                drm_dbg_kms(&i915->drm, "Unsupported BPP %u, min %u\n",
 715                            bits_per_pixel, valid_dsc_bpp[0]);
 716                return 0;
 717        }
 718
 719        /* From XE_LPD onwards we support from bpc upto uncompressed bpp-1 BPPs */
 720        if (DISPLAY_VER(i915) >= 13) {
 721                bits_per_pixel = min(bits_per_pixel, pipe_bpp - 1);
 722        } else {
 723                /* Find the nearest match in the array of known BPPs from VESA */
 724                for (i = 0; i < ARRAY_SIZE(valid_dsc_bpp) - 1; i++) {
 725                        if (bits_per_pixel < valid_dsc_bpp[i + 1])
 726                                break;
 727                }
 728                bits_per_pixel = valid_dsc_bpp[i];
 729        }
 730
 731        /*
 732         * Compressed BPP in U6.4 format so multiply by 16, for Gen 11,
 733         * fractional part is 0
 734         */
 735        return bits_per_pixel << 4;
 736}
 737
 738static u8 intel_dp_dsc_get_slice_count(struct intel_dp *intel_dp,
 739                                       int mode_clock, int mode_hdisplay,
 740                                       bool bigjoiner)
 741{
 742        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
 743        u8 min_slice_count, i;
 744        int max_slice_width;
 745
 746        if (mode_clock <= DP_DSC_PEAK_PIXEL_RATE)
 747                min_slice_count = DIV_ROUND_UP(mode_clock,
 748                                               DP_DSC_MAX_ENC_THROUGHPUT_0);
 749        else
 750                min_slice_count = DIV_ROUND_UP(mode_clock,
 751                                               DP_DSC_MAX_ENC_THROUGHPUT_1);
 752
 753        max_slice_width = drm_dp_dsc_sink_max_slice_width(intel_dp->dsc_dpcd);
 754        if (max_slice_width < DP_DSC_MIN_SLICE_WIDTH_VALUE) {
 755                drm_dbg_kms(&i915->drm,
 756                            "Unsupported slice width %d by DP DSC Sink device\n",
 757                            max_slice_width);
 758                return 0;
 759        }
 760        /* Also take into account max slice width */
 761        min_slice_count = max_t(u8, min_slice_count,
 762                                DIV_ROUND_UP(mode_hdisplay,
 763                                             max_slice_width));
 764
 765        /* Find the closest match to the valid slice count values */
 766        for (i = 0; i < ARRAY_SIZE(valid_dsc_slicecount); i++) {
 767                u8 test_slice_count = valid_dsc_slicecount[i] << bigjoiner;
 768
 769                if (test_slice_count >
 770                    drm_dp_dsc_sink_max_slice_count(intel_dp->dsc_dpcd, false))
 771                        break;
 772
 773                /* big joiner needs small joiner to be enabled */
 774                if (bigjoiner && test_slice_count < 4)
 775                        continue;
 776
 777                if (min_slice_count <= test_slice_count)
 778                        return test_slice_count;
 779        }
 780
 781        drm_dbg_kms(&i915->drm, "Unsupported Slice Count %d\n",
 782                    min_slice_count);
 783        return 0;
 784}
 785
 786static enum intel_output_format
 787intel_dp_output_format(struct drm_connector *connector,
 788                       const struct drm_display_mode *mode)
 789{
 790        struct intel_dp *intel_dp = intel_attached_dp(to_intel_connector(connector));
 791        const struct drm_display_info *info = &connector->display_info;
 792
 793        if (!connector->ycbcr_420_allowed ||
 794            !drm_mode_is_420_only(info, mode))
 795                return INTEL_OUTPUT_FORMAT_RGB;
 796
 797        if (intel_dp->dfp.rgb_to_ycbcr &&
 798            intel_dp->dfp.ycbcr_444_to_420)
 799                return INTEL_OUTPUT_FORMAT_RGB;
 800
 801        if (intel_dp->dfp.ycbcr_444_to_420)
 802                return INTEL_OUTPUT_FORMAT_YCBCR444;
 803        else
 804                return INTEL_OUTPUT_FORMAT_YCBCR420;
 805}
 806
 807int intel_dp_min_bpp(enum intel_output_format output_format)
 808{
 809        if (output_format == INTEL_OUTPUT_FORMAT_RGB)
 810                return 6 * 3;
 811        else
 812                return 8 * 3;
 813}
 814
 815static int intel_dp_output_bpp(enum intel_output_format output_format, int bpp)
 816{
 817        /*
 818         * bpp value was assumed to RGB format. And YCbCr 4:2:0 output
 819         * format of the number of bytes per pixel will be half the number
 820         * of bytes of RGB pixel.
 821         */
 822        if (output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
 823                bpp /= 2;
 824
 825        return bpp;
 826}
 827
 828static int
 829intel_dp_mode_min_output_bpp(struct drm_connector *connector,
 830                             const struct drm_display_mode *mode)
 831{
 832        enum intel_output_format output_format =
 833                intel_dp_output_format(connector, mode);
 834
 835        return intel_dp_output_bpp(output_format, intel_dp_min_bpp(output_format));
 836}
 837
 838static bool intel_dp_hdisplay_bad(struct drm_i915_private *dev_priv,
 839                                  int hdisplay)
 840{
 841        /*
 842         * Older platforms don't like hdisplay==4096 with DP.
 843         *
 844         * On ILK/SNB/IVB the pipe seems to be somewhat running (scanline
 845         * and frame counter increment), but we don't get vblank interrupts,
 846         * and the pipe underruns immediately. The link also doesn't seem
 847         * to get trained properly.
 848         *
 849         * On CHV the vblank interrupts don't seem to disappear but
 850         * otherwise the symptoms are similar.
 851         *
 852         * TODO: confirm the behaviour on HSW+
 853         */
 854        return hdisplay == 4096 && !HAS_DDI(dev_priv);
 855}
 856
 857static enum drm_mode_status
 858intel_dp_mode_valid_downstream(struct intel_connector *connector,
 859                               const struct drm_display_mode *mode,
 860                               int target_clock)
 861{
 862        struct intel_dp *intel_dp = intel_attached_dp(connector);
 863        const struct drm_display_info *info = &connector->base.display_info;
 864        int tmds_clock;
 865
 866        /* If PCON supports FRL MODE, check FRL bandwidth constraints */
 867        if (intel_dp->dfp.pcon_max_frl_bw) {
 868                int target_bw;
 869                int max_frl_bw;
 870                int bpp = intel_dp_mode_min_output_bpp(&connector->base, mode);
 871
 872                target_bw = bpp * target_clock;
 873
 874                max_frl_bw = intel_dp->dfp.pcon_max_frl_bw;
 875
 876                /* converting bw from Gbps to Kbps*/
 877                max_frl_bw = max_frl_bw * 1000000;
 878
 879                if (target_bw > max_frl_bw)
 880                        return MODE_CLOCK_HIGH;
 881
 882                return MODE_OK;
 883        }
 884
 885        if (intel_dp->dfp.max_dotclock &&
 886            target_clock > intel_dp->dfp.max_dotclock)
 887                return MODE_CLOCK_HIGH;
 888
 889        /* Assume 8bpc for the DP++/HDMI/DVI TMDS clock check */
 890        tmds_clock = target_clock;
 891        if (drm_mode_is_420_only(info, mode))
 892                tmds_clock /= 2;
 893
 894        if (intel_dp->dfp.min_tmds_clock &&
 895            tmds_clock < intel_dp->dfp.min_tmds_clock)
 896                return MODE_CLOCK_LOW;
 897        if (intel_dp->dfp.max_tmds_clock &&
 898            tmds_clock > intel_dp->dfp.max_tmds_clock)
 899                return MODE_CLOCK_HIGH;
 900
 901        return MODE_OK;
 902}
 903
 904static bool intel_dp_need_bigjoiner(struct intel_dp *intel_dp,
 905                                    int hdisplay, int clock)
 906{
 907        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
 908
 909        if (!intel_dp_can_bigjoiner(intel_dp))
 910                return false;
 911
 912        return clock > i915->max_dotclk_freq || hdisplay > 5120;
 913}
 914
 915static enum drm_mode_status
 916intel_dp_mode_valid(struct drm_connector *connector,
 917                    struct drm_display_mode *mode)
 918{
 919        struct intel_dp *intel_dp = intel_attached_dp(to_intel_connector(connector));
 920        struct intel_connector *intel_connector = to_intel_connector(connector);
 921        struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
 922        struct drm_i915_private *dev_priv = to_i915(connector->dev);
 923        int target_clock = mode->clock;
 924        int max_rate, mode_rate, max_lanes, max_link_clock;
 925        int max_dotclk = dev_priv->max_dotclk_freq;
 926        u16 dsc_max_output_bpp = 0;
 927        u8 dsc_slice_count = 0;
 928        enum drm_mode_status status;
 929        bool dsc = false, bigjoiner = false;
 930
 931        if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
 932                return MODE_NO_DBLESCAN;
 933
 934        if (mode->flags & DRM_MODE_FLAG_DBLCLK)
 935                return MODE_H_ILLEGAL;
 936
 937        if (intel_dp_is_edp(intel_dp) && fixed_mode) {
 938                status = intel_panel_mode_valid(intel_connector, mode);
 939                if (status != MODE_OK)
 940                        return status;
 941
 942                target_clock = fixed_mode->clock;
 943        }
 944
 945        if (mode->clock < 10000)
 946                return MODE_CLOCK_LOW;
 947
 948        if (intel_dp_need_bigjoiner(intel_dp, mode->hdisplay, target_clock)) {
 949                bigjoiner = true;
 950                max_dotclk *= 2;
 951        }
 952        if (target_clock > max_dotclk)
 953                return MODE_CLOCK_HIGH;
 954
 955        max_link_clock = intel_dp_max_link_rate(intel_dp);
 956        max_lanes = intel_dp_max_lane_count(intel_dp);
 957
 958        max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
 959        mode_rate = intel_dp_link_required(target_clock,
 960                                           intel_dp_mode_min_output_bpp(connector, mode));
 961
 962        if (intel_dp_hdisplay_bad(dev_priv, mode->hdisplay))
 963                return MODE_H_ILLEGAL;
 964
 965        /*
 966         * Output bpp is stored in 6.4 format so right shift by 4 to get the
 967         * integer value since we support only integer values of bpp.
 968         */
 969        if (DISPLAY_VER(dev_priv) >= 10 &&
 970            drm_dp_sink_supports_dsc(intel_dp->dsc_dpcd)) {
 971                /*
 972                 * TBD pass the connector BPC,
 973                 * for now U8_MAX so that max BPC on that platform would be picked
 974                 */
 975                int pipe_bpp = intel_dp_dsc_compute_bpp(intel_dp, U8_MAX);
 976
 977                if (intel_dp_is_edp(intel_dp)) {
 978                        dsc_max_output_bpp =
 979                                drm_edp_dsc_sink_output_bpp(intel_dp->dsc_dpcd) >> 4;
 980                        dsc_slice_count =
 981                                drm_dp_dsc_sink_max_slice_count(intel_dp->dsc_dpcd,
 982                                                                true);
 983                } else if (drm_dp_sink_supports_fec(intel_dp->fec_capable)) {
 984                        dsc_max_output_bpp =
 985                                intel_dp_dsc_get_output_bpp(dev_priv,
 986                                                            max_link_clock,
 987                                                            max_lanes,
 988                                                            target_clock,
 989                                                            mode->hdisplay,
 990                                                            bigjoiner,
 991                                                            pipe_bpp) >> 4;
 992                        dsc_slice_count =
 993                                intel_dp_dsc_get_slice_count(intel_dp,
 994                                                             target_clock,
 995                                                             mode->hdisplay,
 996                                                             bigjoiner);
 997                }
 998
 999                dsc = dsc_max_output_bpp && dsc_slice_count;
1000        }
1001
1002        /*
1003         * Big joiner configuration needs DSC for TGL which is not true for
1004         * XE_LPD where uncompressed joiner is supported.
1005         */
1006        if (DISPLAY_VER(dev_priv) < 13 && bigjoiner && !dsc)
1007                return MODE_CLOCK_HIGH;
1008
1009        if (mode_rate > max_rate && !dsc)
1010                return MODE_CLOCK_HIGH;
1011
1012        status = intel_dp_mode_valid_downstream(intel_connector,
1013                                                mode, target_clock);
1014        if (status != MODE_OK)
1015                return status;
1016
1017        return intel_mode_valid_max_plane_size(dev_priv, mode, bigjoiner);
1018}
1019
1020bool intel_dp_source_supports_tps3(struct drm_i915_private *i915)
1021{
1022        return DISPLAY_VER(i915) >= 9 || IS_BROADWELL(i915) || IS_HASWELL(i915);
1023}
1024
1025bool intel_dp_source_supports_tps4(struct drm_i915_private *i915)
1026{
1027        return DISPLAY_VER(i915) >= 10;
1028}
1029
1030static void snprintf_int_array(char *str, size_t len,
1031                               const int *array, int nelem)
1032{
1033        int i;
1034
1035        str[0] = '\0';
1036
1037        for (i = 0; i < nelem; i++) {
1038                int r = snprintf(str, len, "%s%d", i ? ", " : "", array[i]);
1039                if (r >= len)
1040                        return;
1041                str += r;
1042                len -= r;
1043        }
1044}
1045
1046static void intel_dp_print_rates(struct intel_dp *intel_dp)
1047{
1048        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1049        char str[128]; /* FIXME: too big for stack? */
1050
1051        if (!drm_debug_enabled(DRM_UT_KMS))
1052                return;
1053
1054        snprintf_int_array(str, sizeof(str),
1055                           intel_dp->source_rates, intel_dp->num_source_rates);
1056        drm_dbg_kms(&i915->drm, "source rates: %s\n", str);
1057
1058        snprintf_int_array(str, sizeof(str),
1059                           intel_dp->sink_rates, intel_dp->num_sink_rates);
1060        drm_dbg_kms(&i915->drm, "sink rates: %s\n", str);
1061
1062        snprintf_int_array(str, sizeof(str),
1063                           intel_dp->common_rates, intel_dp->num_common_rates);
1064        drm_dbg_kms(&i915->drm, "common rates: %s\n", str);
1065}
1066
1067int
1068intel_dp_max_link_rate(struct intel_dp *intel_dp)
1069{
1070        int len;
1071
1072        len = intel_dp_common_len_rate_limit(intel_dp, intel_dp->max_link_rate);
1073
1074        return intel_dp_common_rate(intel_dp, len - 1);
1075}
1076
1077int intel_dp_rate_select(struct intel_dp *intel_dp, int rate)
1078{
1079        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1080        int i = intel_dp_rate_index(intel_dp->sink_rates,
1081                                    intel_dp->num_sink_rates, rate);
1082
1083        if (drm_WARN_ON(&i915->drm, i < 0))
1084                i = 0;
1085
1086        return i;
1087}
1088
1089void intel_dp_compute_rate(struct intel_dp *intel_dp, int port_clock,
1090                           u8 *link_bw, u8 *rate_select)
1091{
1092        /* eDP 1.4 rate select method. */
1093        if (intel_dp->use_rate_select) {
1094                *link_bw = 0;
1095                *rate_select =
1096                        intel_dp_rate_select(intel_dp, port_clock);
1097        } else {
1098                *link_bw = drm_dp_link_rate_to_bw_code(port_clock);
1099                *rate_select = 0;
1100        }
1101}
1102
1103static bool intel_dp_source_supports_fec(struct intel_dp *intel_dp,
1104                                         const struct intel_crtc_state *pipe_config)
1105{
1106        struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1107
1108        /* On TGL, FEC is supported on all Pipes */
1109        if (DISPLAY_VER(dev_priv) >= 12)
1110                return true;
1111
1112        if (DISPLAY_VER(dev_priv) == 11 && pipe_config->cpu_transcoder != TRANSCODER_A)
1113                return true;
1114
1115        return false;
1116}
1117
1118static bool intel_dp_supports_fec(struct intel_dp *intel_dp,
1119                                  const struct intel_crtc_state *pipe_config)
1120{
1121        return intel_dp_source_supports_fec(intel_dp, pipe_config) &&
1122                drm_dp_sink_supports_fec(intel_dp->fec_capable);
1123}
1124
1125static bool intel_dp_supports_dsc(struct intel_dp *intel_dp,
1126                                  const struct intel_crtc_state *crtc_state)
1127{
1128        if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP) && !crtc_state->fec_enable)
1129                return false;
1130
1131        return intel_dsc_source_support(crtc_state) &&
1132                drm_dp_sink_supports_dsc(intel_dp->dsc_dpcd);
1133}
1134
1135static bool intel_dp_hdmi_ycbcr420(struct intel_dp *intel_dp,
1136                                   const struct intel_crtc_state *crtc_state)
1137{
1138        return crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
1139                (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444 &&
1140                 intel_dp->dfp.ycbcr_444_to_420);
1141}
1142
1143static int intel_dp_hdmi_tmds_clock(struct intel_dp *intel_dp,
1144                                    const struct intel_crtc_state *crtc_state, int bpc)
1145{
1146        int clock = crtc_state->hw.adjusted_mode.crtc_clock * bpc / 8;
1147
1148        if (intel_dp_hdmi_ycbcr420(intel_dp, crtc_state))
1149                clock /= 2;
1150
1151        return clock;
1152}
1153
1154static bool intel_dp_hdmi_tmds_clock_valid(struct intel_dp *intel_dp,
1155                                           const struct intel_crtc_state *crtc_state, int bpc)
1156{
1157        int tmds_clock = intel_dp_hdmi_tmds_clock(intel_dp, crtc_state, bpc);
1158
1159        if (intel_dp->dfp.min_tmds_clock &&
1160            tmds_clock < intel_dp->dfp.min_tmds_clock)
1161                return false;
1162
1163        if (intel_dp->dfp.max_tmds_clock &&
1164            tmds_clock > intel_dp->dfp.max_tmds_clock)
1165                return false;
1166
1167        return true;
1168}
1169
1170static bool intel_dp_hdmi_deep_color_possible(struct intel_dp *intel_dp,
1171                                              const struct intel_crtc_state *crtc_state,
1172                                              int bpc)
1173{
1174
1175        return intel_hdmi_deep_color_possible(crtc_state, bpc,
1176                                              intel_dp->has_hdmi_sink,
1177                                              intel_dp_hdmi_ycbcr420(intel_dp, crtc_state)) &&
1178                intel_dp_hdmi_tmds_clock_valid(intel_dp, crtc_state, bpc);
1179}
1180
1181static int intel_dp_max_bpp(struct intel_dp *intel_dp,
1182                            const struct intel_crtc_state *crtc_state)
1183{
1184        struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1185        struct intel_connector *intel_connector = intel_dp->attached_connector;
1186        int bpp, bpc;
1187
1188        bpc = crtc_state->pipe_bpp / 3;
1189
1190        if (intel_dp->dfp.max_bpc)
1191                bpc = min_t(int, bpc, intel_dp->dfp.max_bpc);
1192
1193        if (intel_dp->dfp.min_tmds_clock) {
1194                for (; bpc >= 10; bpc -= 2) {
1195                        if (intel_dp_hdmi_deep_color_possible(intel_dp, crtc_state, bpc))
1196                                break;
1197                }
1198        }
1199
1200        bpp = bpc * 3;
1201        if (intel_dp_is_edp(intel_dp)) {
1202                /* Get bpp from vbt only for panels that dont have bpp in edid */
1203                if (intel_connector->base.display_info.bpc == 0 &&
1204                    dev_priv->vbt.edp.bpp && dev_priv->vbt.edp.bpp < bpp) {
1205                        drm_dbg_kms(&dev_priv->drm,
1206                                    "clamping bpp for eDP panel to BIOS-provided %i\n",
1207                                    dev_priv->vbt.edp.bpp);
1208                        bpp = dev_priv->vbt.edp.bpp;
1209                }
1210        }
1211
1212        return bpp;
1213}
1214
1215/* Adjust link config limits based on compliance test requests. */
1216void
1217intel_dp_adjust_compliance_config(struct intel_dp *intel_dp,
1218                                  struct intel_crtc_state *pipe_config,
1219                                  struct link_config_limits *limits)
1220{
1221        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1222
1223        /* For DP Compliance we override the computed bpp for the pipe */
1224        if (intel_dp->compliance.test_data.bpc != 0) {
1225                int bpp = 3 * intel_dp->compliance.test_data.bpc;
1226
1227                limits->min_bpp = limits->max_bpp = bpp;
1228                pipe_config->dither_force_disable = bpp == 6 * 3;
1229
1230                drm_dbg_kms(&i915->drm, "Setting pipe_bpp to %d\n", bpp);
1231        }
1232
1233        /* Use values requested by Compliance Test Request */
1234        if (intel_dp->compliance.test_type == DP_TEST_LINK_TRAINING) {
1235                int index;
1236
1237                /* Validate the compliance test data since max values
1238                 * might have changed due to link train fallback.
1239                 */
1240                if (intel_dp_link_params_valid(intel_dp, intel_dp->compliance.test_link_rate,
1241                                               intel_dp->compliance.test_lane_count)) {
1242                        index = intel_dp_rate_index(intel_dp->common_rates,
1243                                                    intel_dp->num_common_rates,
1244                                                    intel_dp->compliance.test_link_rate);
1245                        if (index >= 0)
1246                                limits->min_rate = limits->max_rate =
1247                                        intel_dp->compliance.test_link_rate;
1248                        limits->min_lane_count = limits->max_lane_count =
1249                                intel_dp->compliance.test_lane_count;
1250                }
1251        }
1252}
1253
1254/* Optimize link config in order: max bpp, min clock, min lanes */
1255static int
1256intel_dp_compute_link_config_wide(struct intel_dp *intel_dp,
1257                                  struct intel_crtc_state *pipe_config,
1258                                  const struct link_config_limits *limits)
1259{
1260        struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
1261        int bpp, i, lane_count;
1262        int mode_rate, link_rate, link_avail;
1263
1264        for (bpp = limits->max_bpp; bpp >= limits->min_bpp; bpp -= 2 * 3) {
1265                int output_bpp = intel_dp_output_bpp(pipe_config->output_format, bpp);
1266
1267                mode_rate = intel_dp_link_required(adjusted_mode->crtc_clock,
1268                                                   output_bpp);
1269
1270                for (i = 0; i < intel_dp->num_common_rates; i++) {
1271                        link_rate = intel_dp_common_rate(intel_dp, i);
1272                        if (link_rate < limits->min_rate ||
1273                            link_rate > limits->max_rate)
1274                                continue;
1275
1276                        for (lane_count = limits->min_lane_count;
1277                             lane_count <= limits->max_lane_count;
1278                             lane_count <<= 1) {
1279                                link_avail = intel_dp_max_data_rate(link_rate,
1280                                                                    lane_count);
1281
1282                                if (mode_rate <= link_avail) {
1283                                        pipe_config->lane_count = lane_count;
1284                                        pipe_config->pipe_bpp = bpp;
1285                                        pipe_config->port_clock = link_rate;
1286
1287                                        return 0;
1288                                }
1289                        }
1290                }
1291        }
1292
1293        return -EINVAL;
1294}
1295
1296static int intel_dp_dsc_compute_bpp(struct intel_dp *intel_dp, u8 max_req_bpc)
1297{
1298        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1299        int i, num_bpc;
1300        u8 dsc_bpc[3] = {0};
1301        u8 dsc_max_bpc;
1302
1303        /* Max DSC Input BPC for ICL is 10 and for TGL+ is 12 */
1304        if (DISPLAY_VER(i915) >= 12)
1305                dsc_max_bpc = min_t(u8, 12, max_req_bpc);
1306        else
1307                dsc_max_bpc = min_t(u8, 10, max_req_bpc);
1308
1309        num_bpc = drm_dp_dsc_sink_supported_input_bpcs(intel_dp->dsc_dpcd,
1310                                                       dsc_bpc);
1311        for (i = 0; i < num_bpc; i++) {
1312                if (dsc_max_bpc >= dsc_bpc[i])
1313                        return dsc_bpc[i] * 3;
1314        }
1315
1316        return 0;
1317}
1318
1319#define DSC_SUPPORTED_VERSION_MIN               1
1320
1321static int intel_dp_dsc_compute_params(struct intel_encoder *encoder,
1322                                       struct intel_crtc_state *crtc_state)
1323{
1324        struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1325        struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1326        struct drm_dsc_config *vdsc_cfg = &crtc_state->dsc.config;
1327        u8 line_buf_depth;
1328        int ret;
1329
1330        /*
1331         * RC_MODEL_SIZE is currently a constant across all configurations.
1332         *
1333         * FIXME: Look into using sink defined DPCD DP_DSC_RC_BUF_BLK_SIZE and
1334         * DP_DSC_RC_BUF_SIZE for this.
1335         */
1336        vdsc_cfg->rc_model_size = DSC_RC_MODEL_SIZE_CONST;
1337
1338        /*
1339         * Slice Height of 8 works for all currently available panels. So start
1340         * with that if pic_height is an integral multiple of 8. Eventually add
1341         * logic to try multiple slice heights.
1342         */
1343        if (vdsc_cfg->pic_height % 8 == 0)
1344                vdsc_cfg->slice_height = 8;
1345        else if (vdsc_cfg->pic_height % 4 == 0)
1346                vdsc_cfg->slice_height = 4;
1347        else
1348                vdsc_cfg->slice_height = 2;
1349
1350        ret = intel_dsc_compute_params(crtc_state);
1351        if (ret)
1352                return ret;
1353
1354        vdsc_cfg->dsc_version_major =
1355                (intel_dp->dsc_dpcd[DP_DSC_REV - DP_DSC_SUPPORT] &
1356                 DP_DSC_MAJOR_MASK) >> DP_DSC_MAJOR_SHIFT;
1357        vdsc_cfg->dsc_version_minor =
1358                min(DSC_SUPPORTED_VERSION_MIN,
1359                    (intel_dp->dsc_dpcd[DP_DSC_REV - DP_DSC_SUPPORT] &
1360                     DP_DSC_MINOR_MASK) >> DP_DSC_MINOR_SHIFT);
1361
1362        vdsc_cfg->convert_rgb = intel_dp->dsc_dpcd[DP_DSC_DEC_COLOR_FORMAT_CAP - DP_DSC_SUPPORT] &
1363                DP_DSC_RGB;
1364
1365        line_buf_depth = drm_dp_dsc_sink_line_buf_depth(intel_dp->dsc_dpcd);
1366        if (!line_buf_depth) {
1367                drm_dbg_kms(&i915->drm,
1368                            "DSC Sink Line Buffer Depth invalid\n");
1369                return -EINVAL;
1370        }
1371
1372        if (vdsc_cfg->dsc_version_minor == 2)
1373                vdsc_cfg->line_buf_depth = (line_buf_depth == DSC_1_2_MAX_LINEBUF_DEPTH_BITS) ?
1374                        DSC_1_2_MAX_LINEBUF_DEPTH_VAL : line_buf_depth;
1375        else
1376                vdsc_cfg->line_buf_depth = (line_buf_depth > DSC_1_1_MAX_LINEBUF_DEPTH_BITS) ?
1377                        DSC_1_1_MAX_LINEBUF_DEPTH_BITS : line_buf_depth;
1378
1379        vdsc_cfg->block_pred_enable =
1380                intel_dp->dsc_dpcd[DP_DSC_BLK_PREDICTION_SUPPORT - DP_DSC_SUPPORT] &
1381                DP_DSC_BLK_PREDICTION_IS_SUPPORTED;
1382
1383        return drm_dsc_compute_rc_parameters(vdsc_cfg);
1384}
1385
1386static int intel_dp_dsc_compute_config(struct intel_dp *intel_dp,
1387                                       struct intel_crtc_state *pipe_config,
1388                                       struct drm_connector_state *conn_state,
1389                                       struct link_config_limits *limits)
1390{
1391        struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1392        struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
1393        const struct drm_display_mode *adjusted_mode =
1394                &pipe_config->hw.adjusted_mode;
1395        int pipe_bpp;
1396        int ret;
1397
1398        pipe_config->fec_enable = !intel_dp_is_edp(intel_dp) &&
1399                intel_dp_supports_fec(intel_dp, pipe_config);
1400
1401        if (!intel_dp_supports_dsc(intel_dp, pipe_config))
1402                return -EINVAL;
1403
1404        pipe_bpp = intel_dp_dsc_compute_bpp(intel_dp, conn_state->max_requested_bpc);
1405
1406        /* Min Input BPC for ICL+ is 8 */
1407        if (pipe_bpp < 8 * 3) {
1408                drm_dbg_kms(&dev_priv->drm,
1409                            "No DSC support for less than 8bpc\n");
1410                return -EINVAL;
1411        }
1412
1413        /*
1414         * For now enable DSC for max bpp, max link rate, max lane count.
1415         * Optimize this later for the minimum possible link rate/lane count
1416         * with DSC enabled for the requested mode.
1417         */
1418        pipe_config->pipe_bpp = pipe_bpp;
1419        pipe_config->port_clock = limits->max_rate;
1420        pipe_config->lane_count = limits->max_lane_count;
1421
1422        if (intel_dp_is_edp(intel_dp)) {
1423                pipe_config->dsc.compressed_bpp =
1424                        min_t(u16, drm_edp_dsc_sink_output_bpp(intel_dp->dsc_dpcd) >> 4,
1425                              pipe_config->pipe_bpp);
1426                pipe_config->dsc.slice_count =
1427                        drm_dp_dsc_sink_max_slice_count(intel_dp->dsc_dpcd,
1428                                                        true);
1429        } else {
1430                u16 dsc_max_output_bpp;
1431                u8 dsc_dp_slice_count;
1432
1433                dsc_max_output_bpp =
1434                        intel_dp_dsc_get_output_bpp(dev_priv,
1435                                                    pipe_config->port_clock,
1436                                                    pipe_config->lane_count,
1437                                                    adjusted_mode->crtc_clock,
1438                                                    adjusted_mode->crtc_hdisplay,
1439                                                    pipe_config->bigjoiner,
1440                                                    pipe_bpp);
1441                dsc_dp_slice_count =
1442                        intel_dp_dsc_get_slice_count(intel_dp,
1443                                                     adjusted_mode->crtc_clock,
1444                                                     adjusted_mode->crtc_hdisplay,
1445                                                     pipe_config->bigjoiner);
1446                if (!dsc_max_output_bpp || !dsc_dp_slice_count) {
1447                        drm_dbg_kms(&dev_priv->drm,
1448                                    "Compressed BPP/Slice Count not supported\n");
1449                        return -EINVAL;
1450                }
1451                pipe_config->dsc.compressed_bpp = min_t(u16,
1452                                                               dsc_max_output_bpp >> 4,
1453                                                               pipe_config->pipe_bpp);
1454                pipe_config->dsc.slice_count = dsc_dp_slice_count;
1455        }
1456
1457        /* As of today we support DSC for only RGB */
1458        if (intel_dp->force_dsc_bpp) {
1459                if (intel_dp->force_dsc_bpp >= 8 &&
1460                    intel_dp->force_dsc_bpp < pipe_bpp) {
1461                        drm_dbg_kms(&dev_priv->drm,
1462                                    "DSC BPP forced to %d",
1463                                    intel_dp->force_dsc_bpp);
1464                        pipe_config->dsc.compressed_bpp =
1465                                                intel_dp->force_dsc_bpp;
1466                } else {
1467                        drm_dbg_kms(&dev_priv->drm,
1468                                    "Invalid DSC BPP %d",
1469                                    intel_dp->force_dsc_bpp);
1470                }
1471        }
1472
1473        /*
1474         * VDSC engine operates at 1 Pixel per clock, so if peak pixel rate
1475         * is greater than the maximum Cdclock and if slice count is even
1476         * then we need to use 2 VDSC instances.
1477         */
1478        if (adjusted_mode->crtc_clock > dev_priv->max_cdclk_freq ||
1479            pipe_config->bigjoiner) {
1480                if (pipe_config->dsc.slice_count < 2) {
1481                        drm_dbg_kms(&dev_priv->drm,
1482                                    "Cannot split stream to use 2 VDSC instances\n");
1483                        return -EINVAL;
1484                }
1485
1486                pipe_config->dsc.dsc_split = true;
1487        }
1488
1489        ret = intel_dp_dsc_compute_params(&dig_port->base, pipe_config);
1490        if (ret < 0) {
1491                drm_dbg_kms(&dev_priv->drm,
1492                            "Cannot compute valid DSC parameters for Input Bpp = %d "
1493                            "Compressed BPP = %d\n",
1494                            pipe_config->pipe_bpp,
1495                            pipe_config->dsc.compressed_bpp);
1496                return ret;
1497        }
1498
1499        pipe_config->dsc.compression_enable = true;
1500        drm_dbg_kms(&dev_priv->drm, "DP DSC computed with Input Bpp = %d "
1501                    "Compressed Bpp = %d Slice Count = %d\n",
1502                    pipe_config->pipe_bpp,
1503                    pipe_config->dsc.compressed_bpp,
1504                    pipe_config->dsc.slice_count);
1505
1506        return 0;
1507}
1508
1509static int
1510intel_dp_compute_link_config(struct intel_encoder *encoder,
1511                             struct intel_crtc_state *pipe_config,
1512                             struct drm_connector_state *conn_state)
1513{
1514        struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1515        const struct drm_display_mode *adjusted_mode =
1516                &pipe_config->hw.adjusted_mode;
1517        struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1518        struct link_config_limits limits;
1519        int ret;
1520
1521        limits.min_rate = intel_dp_common_rate(intel_dp, 0);
1522        limits.max_rate = intel_dp_max_link_rate(intel_dp);
1523
1524        limits.min_lane_count = 1;
1525        limits.max_lane_count = intel_dp_max_lane_count(intel_dp);
1526
1527        limits.min_bpp = intel_dp_min_bpp(pipe_config->output_format);
1528        limits.max_bpp = intel_dp_max_bpp(intel_dp, pipe_config);
1529
1530        if (intel_dp->use_max_params) {
1531                /*
1532                 * Use the maximum clock and number of lanes the eDP panel
1533                 * advertizes being capable of in case the initial fast
1534                 * optimal params failed us. The panels are generally
1535                 * designed to support only a single clock and lane
1536                 * configuration, and typically on older panels these
1537                 * values correspond to the native resolution of the panel.
1538                 */
1539                limits.min_lane_count = limits.max_lane_count;
1540                limits.min_rate = limits.max_rate;
1541        }
1542
1543        intel_dp_adjust_compliance_config(intel_dp, pipe_config, &limits);
1544
1545        drm_dbg_kms(&i915->drm, "DP link computation with max lane count %i "
1546                    "max rate %d max bpp %d pixel clock %iKHz\n",
1547                    limits.max_lane_count, limits.max_rate,
1548                    limits.max_bpp, adjusted_mode->crtc_clock);
1549
1550        if (intel_dp_need_bigjoiner(intel_dp, adjusted_mode->crtc_hdisplay,
1551                                    adjusted_mode->crtc_clock))
1552                pipe_config->bigjoiner = true;
1553
1554        /*
1555         * Optimize for slow and wide for everything, because there are some
1556         * eDP 1.3 and 1.4 panels don't work well with fast and narrow.
1557         */
1558        ret = intel_dp_compute_link_config_wide(intel_dp, pipe_config, &limits);
1559
1560        /*
1561         * Pipe joiner needs compression upto display12 due to BW limitation. DG2
1562         * onwards pipe joiner can be enabled without compression.
1563         */
1564        drm_dbg_kms(&i915->drm, "Force DSC en = %d\n", intel_dp->force_dsc_en);
1565        if (ret || intel_dp->force_dsc_en || (DISPLAY_VER(i915) < 13 &&
1566                                              pipe_config->bigjoiner)) {
1567                ret = intel_dp_dsc_compute_config(intel_dp, pipe_config,
1568                                                  conn_state, &limits);
1569                if (ret < 0)
1570                        return ret;
1571        }
1572
1573        if (pipe_config->dsc.compression_enable) {
1574                drm_dbg_kms(&i915->drm,
1575                            "DP lane count %d clock %d Input bpp %d Compressed bpp %d\n",
1576                            pipe_config->lane_count, pipe_config->port_clock,
1577                            pipe_config->pipe_bpp,
1578                            pipe_config->dsc.compressed_bpp);
1579
1580                drm_dbg_kms(&i915->drm,
1581                            "DP link rate required %i available %i\n",
1582                            intel_dp_link_required(adjusted_mode->crtc_clock,
1583                                                   pipe_config->dsc.compressed_bpp),
1584                            intel_dp_max_data_rate(pipe_config->port_clock,
1585                                                   pipe_config->lane_count));
1586        } else {
1587                drm_dbg_kms(&i915->drm, "DP lane count %d clock %d bpp %d\n",
1588                            pipe_config->lane_count, pipe_config->port_clock,
1589                            pipe_config->pipe_bpp);
1590
1591                drm_dbg_kms(&i915->drm,
1592                            "DP link rate required %i available %i\n",
1593                            intel_dp_link_required(adjusted_mode->crtc_clock,
1594                                                   pipe_config->pipe_bpp),
1595                            intel_dp_max_data_rate(pipe_config->port_clock,
1596                                                   pipe_config->lane_count));
1597        }
1598        return 0;
1599}
1600
1601bool intel_dp_limited_color_range(const struct intel_crtc_state *crtc_state,
1602                                  const struct drm_connector_state *conn_state)
1603{
1604        const struct intel_digital_connector_state *intel_conn_state =
1605                to_intel_digital_connector_state(conn_state);
1606        const struct drm_display_mode *adjusted_mode =
1607                &crtc_state->hw.adjusted_mode;
1608
1609        /*
1610         * Our YCbCr output is always limited range.
1611         * crtc_state->limited_color_range only applies to RGB,
1612         * and it must never be set for YCbCr or we risk setting
1613         * some conflicting bits in PIPECONF which will mess up
1614         * the colors on the monitor.
1615         */
1616        if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
1617                return false;
1618
1619        if (intel_conn_state->broadcast_rgb == INTEL_BROADCAST_RGB_AUTO) {
1620                /*
1621                 * See:
1622                 * CEA-861-E - 5.1 Default Encoding Parameters
1623                 * VESA DisplayPort Ver.1.2a - 5.1.1.1 Video Colorimetry
1624                 */
1625                return crtc_state->pipe_bpp != 18 &&
1626                        drm_default_rgb_quant_range(adjusted_mode) ==
1627                        HDMI_QUANTIZATION_RANGE_LIMITED;
1628        } else {
1629                return intel_conn_state->broadcast_rgb ==
1630                        INTEL_BROADCAST_RGB_LIMITED;
1631        }
1632}
1633
1634static bool intel_dp_port_has_audio(struct drm_i915_private *dev_priv,
1635                                    enum port port)
1636{
1637        if (IS_G4X(dev_priv))
1638                return false;
1639        if (DISPLAY_VER(dev_priv) < 12 && port == PORT_A)
1640                return false;
1641
1642        return true;
1643}
1644
1645static void intel_dp_compute_vsc_colorimetry(const struct intel_crtc_state *crtc_state,
1646                                             const struct drm_connector_state *conn_state,
1647                                             struct drm_dp_vsc_sdp *vsc)
1648{
1649        struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1650        struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1651
1652        /*
1653         * Prepare VSC Header for SU as per DP 1.4 spec, Table 2-118
1654         * VSC SDP supporting 3D stereo, PSR2, and Pixel Encoding/
1655         * Colorimetry Format indication.
1656         */
1657        vsc->revision = 0x5;
1658        vsc->length = 0x13;
1659
1660        /* DP 1.4a spec, Table 2-120 */
1661        switch (crtc_state->output_format) {
1662        case INTEL_OUTPUT_FORMAT_YCBCR444:
1663                vsc->pixelformat = DP_PIXELFORMAT_YUV444;
1664                break;
1665        case INTEL_OUTPUT_FORMAT_YCBCR420:
1666                vsc->pixelformat = DP_PIXELFORMAT_YUV420;
1667                break;
1668        case INTEL_OUTPUT_FORMAT_RGB:
1669        default:
1670                vsc->pixelformat = DP_PIXELFORMAT_RGB;
1671        }
1672
1673        switch (conn_state->colorspace) {
1674        case DRM_MODE_COLORIMETRY_BT709_YCC:
1675                vsc->colorimetry = DP_COLORIMETRY_BT709_YCC;
1676                break;
1677        case DRM_MODE_COLORIMETRY_XVYCC_601:
1678                vsc->colorimetry = DP_COLORIMETRY_XVYCC_601;
1679                break;
1680        case DRM_MODE_COLORIMETRY_XVYCC_709:
1681                vsc->colorimetry = DP_COLORIMETRY_XVYCC_709;
1682                break;
1683        case DRM_MODE_COLORIMETRY_SYCC_601:
1684                vsc->colorimetry = DP_COLORIMETRY_SYCC_601;
1685                break;
1686        case DRM_MODE_COLORIMETRY_OPYCC_601:
1687                vsc->colorimetry = DP_COLORIMETRY_OPYCC_601;
1688                break;
1689        case DRM_MODE_COLORIMETRY_BT2020_CYCC:
1690                vsc->colorimetry = DP_COLORIMETRY_BT2020_CYCC;
1691                break;
1692        case DRM_MODE_COLORIMETRY_BT2020_RGB:
1693                vsc->colorimetry = DP_COLORIMETRY_BT2020_RGB;
1694                break;
1695        case DRM_MODE_COLORIMETRY_BT2020_YCC:
1696                vsc->colorimetry = DP_COLORIMETRY_BT2020_YCC;
1697                break;
1698        case DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65:
1699        case DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER:
1700                vsc->colorimetry = DP_COLORIMETRY_DCI_P3_RGB;
1701                break;
1702        default:
1703                /*
1704                 * RGB->YCBCR color conversion uses the BT.709
1705                 * color space.
1706                 */
1707                if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
1708                        vsc->colorimetry = DP_COLORIMETRY_BT709_YCC;
1709                else
1710                        vsc->colorimetry = DP_COLORIMETRY_DEFAULT;
1711                break;
1712        }
1713
1714        vsc->bpc = crtc_state->pipe_bpp / 3;
1715
1716        /* only RGB pixelformat supports 6 bpc */
1717        drm_WARN_ON(&dev_priv->drm,
1718                    vsc->bpc == 6 && vsc->pixelformat != DP_PIXELFORMAT_RGB);
1719
1720        /* all YCbCr are always limited range */
1721        vsc->dynamic_range = DP_DYNAMIC_RANGE_CTA;
1722        vsc->content_type = DP_CONTENT_TYPE_NOT_DEFINED;
1723}
1724
1725static void intel_dp_compute_vsc_sdp(struct intel_dp *intel_dp,
1726                                     struct intel_crtc_state *crtc_state,
1727                                     const struct drm_connector_state *conn_state)
1728{
1729        struct drm_dp_vsc_sdp *vsc = &crtc_state->infoframes.vsc;
1730
1731        /* When a crtc state has PSR, VSC SDP will be handled by PSR routine */
1732        if (crtc_state->has_psr)
1733                return;
1734
1735        if (!intel_dp_needs_vsc_sdp(crtc_state, conn_state))
1736                return;
1737
1738        crtc_state->infoframes.enable |= intel_hdmi_infoframe_enable(DP_SDP_VSC);
1739        vsc->sdp_type = DP_SDP_VSC;
1740        intel_dp_compute_vsc_colorimetry(crtc_state, conn_state,
1741                                         &crtc_state->infoframes.vsc);
1742}
1743
1744void intel_dp_compute_psr_vsc_sdp(struct intel_dp *intel_dp,
1745                                  const struct intel_crtc_state *crtc_state,
1746                                  const struct drm_connector_state *conn_state,
1747                                  struct drm_dp_vsc_sdp *vsc)
1748{
1749        vsc->sdp_type = DP_SDP_VSC;
1750
1751        if (crtc_state->has_psr2) {
1752                if (intel_dp->psr.colorimetry_support &&
1753                    intel_dp_needs_vsc_sdp(crtc_state, conn_state)) {
1754                        /* [PSR2, +Colorimetry] */
1755                        intel_dp_compute_vsc_colorimetry(crtc_state, conn_state,
1756                                                         vsc);
1757                } else {
1758                        /*
1759                         * [PSR2, -Colorimetry]
1760                         * Prepare VSC Header for SU as per eDP 1.4 spec, Table 6-11
1761                         * 3D stereo + PSR/PSR2 + Y-coordinate.
1762                         */
1763                        vsc->revision = 0x4;
1764                        vsc->length = 0xe;
1765                }
1766        } else {
1767                /*
1768                 * [PSR1]
1769                 * Prepare VSC Header for SU as per DP 1.4 spec, Table 2-118
1770                 * VSC SDP supporting 3D stereo + PSR (applies to eDP v1.3 or
1771                 * higher).
1772                 */
1773                vsc->revision = 0x2;
1774                vsc->length = 0x8;
1775        }
1776}
1777
1778static void
1779intel_dp_compute_hdr_metadata_infoframe_sdp(struct intel_dp *intel_dp,
1780                                            struct intel_crtc_state *crtc_state,
1781                                            const struct drm_connector_state *conn_state)
1782{
1783        int ret;
1784        struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1785        struct hdmi_drm_infoframe *drm_infoframe = &crtc_state->infoframes.drm.drm;
1786
1787        if (!conn_state->hdr_output_metadata)
1788                return;
1789
1790        ret = drm_hdmi_infoframe_set_hdr_metadata(drm_infoframe, conn_state);
1791
1792        if (ret) {
1793                drm_dbg_kms(&dev_priv->drm, "couldn't set HDR metadata in infoframe\n");
1794                return;
1795        }
1796
1797        crtc_state->infoframes.enable |=
1798                intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GAMUT_METADATA);
1799}
1800
1801int
1802intel_dp_compute_config(struct intel_encoder *encoder,
1803                        struct intel_crtc_state *pipe_config,
1804                        struct drm_connector_state *conn_state)
1805{
1806        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1807        struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
1808        struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1809        enum port port = encoder->port;
1810        struct intel_connector *intel_connector = intel_dp->attached_connector;
1811        struct intel_digital_connector_state *intel_conn_state =
1812                to_intel_digital_connector_state(conn_state);
1813        bool constant_n = drm_dp_has_quirk(&intel_dp->desc, DP_DPCD_QUIRK_CONSTANT_N);
1814        int ret = 0, output_bpp;
1815
1816        if (HAS_PCH_SPLIT(dev_priv) && !HAS_DDI(dev_priv) && port != PORT_A)
1817                pipe_config->has_pch_encoder = true;
1818
1819        pipe_config->output_format = intel_dp_output_format(&intel_connector->base,
1820                                                            adjusted_mode);
1821
1822        if (pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420) {
1823                ret = intel_panel_fitting(pipe_config, conn_state);
1824                if (ret)
1825                        return ret;
1826        }
1827
1828        if (!intel_dp_port_has_audio(dev_priv, port))
1829                pipe_config->has_audio = false;
1830        else if (intel_conn_state->force_audio == HDMI_AUDIO_AUTO)
1831                pipe_config->has_audio = intel_dp->has_audio;
1832        else
1833                pipe_config->has_audio = intel_conn_state->force_audio == HDMI_AUDIO_ON;
1834
1835        if (intel_dp_is_edp(intel_dp) && intel_connector->panel.fixed_mode) {
1836                ret = intel_panel_compute_config(intel_connector, adjusted_mode);
1837                if (ret)
1838                        return ret;
1839
1840                ret = intel_panel_fitting(pipe_config, conn_state);
1841                if (ret)
1842                        return ret;
1843        }
1844
1845        if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
1846                return -EINVAL;
1847
1848        if (HAS_GMCH(dev_priv) &&
1849            adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
1850                return -EINVAL;
1851
1852        if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
1853                return -EINVAL;
1854
1855        if (intel_dp_hdisplay_bad(dev_priv, adjusted_mode->crtc_hdisplay))
1856                return -EINVAL;
1857
1858        ret = intel_dp_compute_link_config(encoder, pipe_config, conn_state);
1859        if (ret < 0)
1860                return ret;
1861
1862        pipe_config->limited_color_range =
1863                intel_dp_limited_color_range(pipe_config, conn_state);
1864
1865        if (pipe_config->dsc.compression_enable)
1866                output_bpp = pipe_config->dsc.compressed_bpp;
1867        else
1868                output_bpp = intel_dp_output_bpp(pipe_config->output_format,
1869                                                 pipe_config->pipe_bpp);
1870
1871        if (intel_dp->mso_link_count) {
1872                int n = intel_dp->mso_link_count;
1873                int overlap = intel_dp->mso_pixel_overlap;
1874
1875                pipe_config->splitter.enable = true;
1876                pipe_config->splitter.link_count = n;
1877                pipe_config->splitter.pixel_overlap = overlap;
1878
1879                drm_dbg_kms(&dev_priv->drm, "MSO link count %d, pixel overlap %d\n",
1880                            n, overlap);
1881
1882                adjusted_mode->crtc_hdisplay = adjusted_mode->crtc_hdisplay / n + overlap;
1883                adjusted_mode->crtc_hblank_start = adjusted_mode->crtc_hblank_start / n + overlap;
1884                adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_hblank_end / n + overlap;
1885                adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hsync_start / n + overlap;
1886                adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_end / n + overlap;
1887                adjusted_mode->crtc_htotal = adjusted_mode->crtc_htotal / n + overlap;
1888                adjusted_mode->crtc_clock /= n;
1889        }
1890
1891        intel_link_compute_m_n(output_bpp,
1892                               pipe_config->lane_count,
1893                               adjusted_mode->crtc_clock,
1894                               pipe_config->port_clock,
1895                               &pipe_config->dp_m_n,
1896                               constant_n, pipe_config->fec_enable);
1897
1898        /* FIXME: abstract this better */
1899        if (pipe_config->splitter.enable)
1900                pipe_config->dp_m_n.gmch_m *= pipe_config->splitter.link_count;
1901
1902        if (!HAS_DDI(dev_priv))
1903                g4x_dp_set_clock(encoder, pipe_config);
1904
1905        intel_vrr_compute_config(pipe_config, conn_state);
1906        intel_psr_compute_config(intel_dp, pipe_config, conn_state);
1907        intel_drrs_compute_config(intel_dp, pipe_config, output_bpp,
1908                                  constant_n);
1909        intel_dp_compute_vsc_sdp(intel_dp, pipe_config, conn_state);
1910        intel_dp_compute_hdr_metadata_infoframe_sdp(intel_dp, pipe_config, conn_state);
1911
1912        return 0;
1913}
1914
1915void intel_dp_set_link_params(struct intel_dp *intel_dp,
1916                              int link_rate, int lane_count)
1917{
1918        memset(intel_dp->train_set, 0, sizeof(intel_dp->train_set));
1919        intel_dp->link_trained = false;
1920        intel_dp->link_rate = link_rate;
1921        intel_dp->lane_count = lane_count;
1922}
1923
1924static void intel_dp_reset_max_link_params(struct intel_dp *intel_dp)
1925{
1926        intel_dp->max_link_lane_count = intel_dp_max_common_lane_count(intel_dp);
1927        intel_dp->max_link_rate = intel_dp_max_common_rate(intel_dp);
1928}
1929
1930/* Enable backlight PWM and backlight PP control. */
1931void intel_edp_backlight_on(const struct intel_crtc_state *crtc_state,
1932                            const struct drm_connector_state *conn_state)
1933{
1934        struct intel_dp *intel_dp = enc_to_intel_dp(to_intel_encoder(conn_state->best_encoder));
1935        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1936
1937        if (!intel_dp_is_edp(intel_dp))
1938                return;
1939
1940        drm_dbg_kms(&i915->drm, "\n");
1941
1942        intel_backlight_enable(crtc_state, conn_state);
1943        intel_pps_backlight_on(intel_dp);
1944}
1945
1946/* Disable backlight PP control and backlight PWM. */
1947void intel_edp_backlight_off(const struct drm_connector_state *old_conn_state)
1948{
1949        struct intel_dp *intel_dp = enc_to_intel_dp(to_intel_encoder(old_conn_state->best_encoder));
1950        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1951
1952        if (!intel_dp_is_edp(intel_dp))
1953                return;
1954
1955        drm_dbg_kms(&i915->drm, "\n");
1956
1957        intel_pps_backlight_off(intel_dp);
1958        intel_backlight_disable(old_conn_state);
1959}
1960
1961static bool downstream_hpd_needs_d0(struct intel_dp *intel_dp)
1962{
1963        /*
1964         * DPCD 1.2+ should support BRANCH_DEVICE_CTRL, and thus
1965         * be capable of signalling downstream hpd with a long pulse.
1966         * Whether or not that means D3 is safe to use is not clear,
1967         * but let's assume so until proven otherwise.
1968         *
1969         * FIXME should really check all downstream ports...
1970         */
1971        return intel_dp->dpcd[DP_DPCD_REV] == 0x11 &&
1972                drm_dp_is_branch(intel_dp->dpcd) &&
1973                intel_dp->downstream_ports[0] & DP_DS_PORT_HPD;
1974}
1975
1976void intel_dp_sink_set_decompression_state(struct intel_dp *intel_dp,
1977                                           const struct intel_crtc_state *crtc_state,
1978                                           bool enable)
1979{
1980        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1981        int ret;
1982
1983        if (!crtc_state->dsc.compression_enable)
1984                return;
1985
1986        ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_DSC_ENABLE,
1987                                 enable ? DP_DECOMPRESSION_EN : 0);
1988        if (ret < 0)
1989                drm_dbg_kms(&i915->drm,
1990                            "Failed to %s sink decompression state\n",
1991                            enabledisable(enable));
1992}
1993
1994static void
1995intel_edp_init_source_oui(struct intel_dp *intel_dp, bool careful)
1996{
1997        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1998        u8 oui[] = { 0x00, 0xaa, 0x01 };
1999        u8 buf[3] = { 0 };
2000
2001        /*
2002         * During driver init, we want to be careful and avoid changing the source OUI if it's
2003         * already set to what we want, so as to avoid clearing any state by accident
2004         */
2005        if (careful) {
2006                if (drm_dp_dpcd_read(&intel_dp->aux, DP_SOURCE_OUI, buf, sizeof(buf)) < 0)
2007                        drm_err(&i915->drm, "Failed to read source OUI\n");
2008
2009                if (memcmp(oui, buf, sizeof(oui)) == 0)
2010                        return;
2011        }
2012
2013        if (drm_dp_dpcd_write(&intel_dp->aux, DP_SOURCE_OUI, oui, sizeof(oui)) < 0)
2014                drm_err(&i915->drm, "Failed to write source OUI\n");
2015
2016        intel_dp->last_oui_write = jiffies;
2017}
2018
2019void intel_dp_wait_source_oui(struct intel_dp *intel_dp)
2020{
2021        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2022
2023        drm_dbg_kms(&i915->drm, "Performing OUI wait\n");
2024        wait_remaining_ms_from_jiffies(intel_dp->last_oui_write, 30);
2025}
2026
2027/* If the device supports it, try to set the power state appropriately */
2028void intel_dp_set_power(struct intel_dp *intel_dp, u8 mode)
2029{
2030        struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
2031        struct drm_i915_private *i915 = to_i915(encoder->base.dev);
2032        int ret, i;
2033
2034        /* Should have a valid DPCD by this point */
2035        if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
2036                return;
2037
2038        if (mode != DP_SET_POWER_D0) {
2039                if (downstream_hpd_needs_d0(intel_dp))
2040                        return;
2041
2042                ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER, mode);
2043        } else {
2044                struct intel_lspcon *lspcon = dp_to_lspcon(intel_dp);
2045
2046                lspcon_resume(dp_to_dig_port(intel_dp));
2047
2048                /* Write the source OUI as early as possible */
2049                if (intel_dp_is_edp(intel_dp))
2050                        intel_edp_init_source_oui(intel_dp, false);
2051
2052                /*
2053                 * When turning on, we need to retry for 1ms to give the sink
2054                 * time to wake up.
2055                 */
2056                for (i = 0; i < 3; i++) {
2057                        ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER, mode);
2058                        if (ret == 1)
2059                                break;
2060                        msleep(1);
2061                }
2062
2063                if (ret == 1 && lspcon->active)
2064                        lspcon_wait_pcon_mode(lspcon);
2065        }
2066
2067        if (ret != 1)
2068                drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] Set power to %s failed\n",
2069                            encoder->base.base.id, encoder->base.name,
2070                            mode == DP_SET_POWER_D0 ? "D0" : "D3");
2071}
2072
2073static bool
2074intel_dp_get_dpcd(struct intel_dp *intel_dp);
2075
2076/**
2077 * intel_dp_sync_state - sync the encoder state during init/resume
2078 * @encoder: intel encoder to sync
2079 * @crtc_state: state for the CRTC connected to the encoder
2080 *
2081 * Sync any state stored in the encoder wrt. HW state during driver init
2082 * and system resume.
2083 */
2084void intel_dp_sync_state(struct intel_encoder *encoder,
2085                         const struct intel_crtc_state *crtc_state)
2086{
2087        struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2088
2089        if (!crtc_state)
2090                return;
2091
2092        /*
2093         * Don't clobber DPCD if it's been already read out during output
2094         * setup (eDP) or detect.
2095         */
2096        if (intel_dp->dpcd[DP_DPCD_REV] == 0)
2097                intel_dp_get_dpcd(intel_dp);
2098
2099        intel_dp_reset_max_link_params(intel_dp);
2100}
2101
2102bool intel_dp_initial_fastset_check(struct intel_encoder *encoder,
2103                                    struct intel_crtc_state *crtc_state)
2104{
2105        struct drm_i915_private *i915 = to_i915(encoder->base.dev);
2106        struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2107
2108        /*
2109         * If BIOS has set an unsupported or non-standard link rate for some
2110         * reason force an encoder recompute and full modeset.
2111         */
2112        if (intel_dp_rate_index(intel_dp->source_rates, intel_dp->num_source_rates,
2113                                crtc_state->port_clock) < 0) {
2114                drm_dbg_kms(&i915->drm, "Forcing full modeset due to unsupported link rate\n");
2115                crtc_state->uapi.connectors_changed = true;
2116                return false;
2117        }
2118
2119        /*
2120         * FIXME hack to force full modeset when DSC is being used.
2121         *
2122         * As long as we do not have full state readout and config comparison
2123         * of crtc_state->dsc, we have no way to ensure reliable fastset.
2124         * Remove once we have readout for DSC.
2125         */
2126        if (crtc_state->dsc.compression_enable) {
2127                drm_dbg_kms(&i915->drm, "Forcing full modeset due to DSC being enabled\n");
2128                crtc_state->uapi.mode_changed = true;
2129                return false;
2130        }
2131
2132        if (CAN_PSR(intel_dp)) {
2133                drm_dbg_kms(&i915->drm, "Forcing full modeset to compute PSR state\n");
2134                crtc_state->uapi.mode_changed = true;
2135                return false;
2136        }
2137
2138        return true;
2139}
2140
2141static void intel_dp_get_pcon_dsc_cap(struct intel_dp *intel_dp)
2142{
2143        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2144
2145        /* Clear the cached register set to avoid using stale values */
2146
2147        memset(intel_dp->pcon_dsc_dpcd, 0, sizeof(intel_dp->pcon_dsc_dpcd));
2148
2149        if (drm_dp_dpcd_read(&intel_dp->aux, DP_PCON_DSC_ENCODER,
2150                             intel_dp->pcon_dsc_dpcd,
2151                             sizeof(intel_dp->pcon_dsc_dpcd)) < 0)
2152                drm_err(&i915->drm, "Failed to read DPCD register 0x%x\n",
2153                        DP_PCON_DSC_ENCODER);
2154
2155        drm_dbg_kms(&i915->drm, "PCON ENCODER DSC DPCD: %*ph\n",
2156                    (int)sizeof(intel_dp->pcon_dsc_dpcd), intel_dp->pcon_dsc_dpcd);
2157}
2158
2159static int intel_dp_pcon_get_frl_mask(u8 frl_bw_mask)
2160{
2161        int bw_gbps[] = {9, 18, 24, 32, 40, 48};
2162        int i;
2163
2164        for (i = ARRAY_SIZE(bw_gbps) - 1; i >= 0; i--) {
2165                if (frl_bw_mask & (1 << i))
2166                        return bw_gbps[i];
2167        }
2168        return 0;
2169}
2170
2171static int intel_dp_pcon_set_frl_mask(int max_frl)
2172{
2173        switch (max_frl) {
2174        case 48:
2175                return DP_PCON_FRL_BW_MASK_48GBPS;
2176        case 40:
2177                return DP_PCON_FRL_BW_MASK_40GBPS;
2178        case 32:
2179                return DP_PCON_FRL_BW_MASK_32GBPS;
2180        case 24:
2181                return DP_PCON_FRL_BW_MASK_24GBPS;
2182        case 18:
2183                return DP_PCON_FRL_BW_MASK_18GBPS;
2184        case 9:
2185                return DP_PCON_FRL_BW_MASK_9GBPS;
2186        }
2187
2188        return 0;
2189}
2190
2191static int intel_dp_hdmi_sink_max_frl(struct intel_dp *intel_dp)
2192{
2193        struct intel_connector *intel_connector = intel_dp->attached_connector;
2194        struct drm_connector *connector = &intel_connector->base;
2195        int max_frl_rate;
2196        int max_lanes, rate_per_lane;
2197        int max_dsc_lanes, dsc_rate_per_lane;
2198
2199        max_lanes = connector->display_info.hdmi.max_lanes;
2200        rate_per_lane = connector->display_info.hdmi.max_frl_rate_per_lane;
2201        max_frl_rate = max_lanes * rate_per_lane;
2202
2203        if (connector->display_info.hdmi.dsc_cap.v_1p2) {
2204                max_dsc_lanes = connector->display_info.hdmi.dsc_cap.max_lanes;
2205                dsc_rate_per_lane = connector->display_info.hdmi.dsc_cap.max_frl_rate_per_lane;
2206                if (max_dsc_lanes && dsc_rate_per_lane)
2207                        max_frl_rate = min(max_frl_rate, max_dsc_lanes * dsc_rate_per_lane);
2208        }
2209
2210        return max_frl_rate;
2211}
2212
2213static bool
2214intel_dp_pcon_is_frl_trained(struct intel_dp *intel_dp,
2215                             u8 max_frl_bw_mask, u8 *frl_trained_mask)
2216{
2217        if (drm_dp_pcon_hdmi_link_active(&intel_dp->aux) &&
2218            drm_dp_pcon_hdmi_link_mode(&intel_dp->aux, frl_trained_mask) == DP_PCON_HDMI_MODE_FRL &&
2219            *frl_trained_mask >= max_frl_bw_mask)
2220                return true;
2221
2222        return false;
2223}
2224
2225static int intel_dp_pcon_start_frl_training(struct intel_dp *intel_dp)
2226{
2227#define TIMEOUT_FRL_READY_MS 500
2228#define TIMEOUT_HDMI_LINK_ACTIVE_MS 1000
2229
2230        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2231        int max_frl_bw, max_pcon_frl_bw, max_edid_frl_bw, ret;
2232        u8 max_frl_bw_mask = 0, frl_trained_mask;
2233        bool is_active;
2234
2235        max_pcon_frl_bw = intel_dp->dfp.pcon_max_frl_bw;
2236        drm_dbg(&i915->drm, "PCON max rate = %d Gbps\n", max_pcon_frl_bw);
2237
2238        max_edid_frl_bw = intel_dp_hdmi_sink_max_frl(intel_dp);
2239        drm_dbg(&i915->drm, "Sink max rate from EDID = %d Gbps\n", max_edid_frl_bw);
2240
2241        max_frl_bw = min(max_edid_frl_bw, max_pcon_frl_bw);
2242
2243        if (max_frl_bw <= 0)
2244                return -EINVAL;
2245
2246        max_frl_bw_mask = intel_dp_pcon_set_frl_mask(max_frl_bw);
2247        drm_dbg(&i915->drm, "MAX_FRL_BW_MASK = %u\n", max_frl_bw_mask);
2248
2249        if (intel_dp_pcon_is_frl_trained(intel_dp, max_frl_bw_mask, &frl_trained_mask))
2250                goto frl_trained;
2251
2252        ret = drm_dp_pcon_frl_prepare(&intel_dp->aux, false);
2253        if (ret < 0)
2254                return ret;
2255        /* Wait for PCON to be FRL Ready */
2256        wait_for(is_active = drm_dp_pcon_is_frl_ready(&intel_dp->aux) == true, TIMEOUT_FRL_READY_MS);
2257
2258        if (!is_active)
2259                return -ETIMEDOUT;
2260
2261        ret = drm_dp_pcon_frl_configure_1(&intel_dp->aux, max_frl_bw,
2262                                          DP_PCON_ENABLE_SEQUENTIAL_LINK);
2263        if (ret < 0)
2264                return ret;
2265        ret = drm_dp_pcon_frl_configure_2(&intel_dp->aux, max_frl_bw_mask,
2266                                          DP_PCON_FRL_LINK_TRAIN_NORMAL);
2267        if (ret < 0)
2268                return ret;
2269        ret = drm_dp_pcon_frl_enable(&intel_dp->aux);
2270        if (ret < 0)
2271                return ret;
2272        /*
2273         * Wait for FRL to be completed
2274         * Check if the HDMI Link is up and active.
2275         */
2276        wait_for(is_active =
2277                 intel_dp_pcon_is_frl_trained(intel_dp, max_frl_bw_mask, &frl_trained_mask),
2278                 TIMEOUT_HDMI_LINK_ACTIVE_MS);
2279
2280        if (!is_active)
2281                return -ETIMEDOUT;
2282
2283frl_trained:
2284        drm_dbg(&i915->drm, "FRL_TRAINED_MASK = %u\n", frl_trained_mask);
2285        intel_dp->frl.trained_rate_gbps = intel_dp_pcon_get_frl_mask(frl_trained_mask);
2286        intel_dp->frl.is_trained = true;
2287        drm_dbg(&i915->drm, "FRL trained with : %d Gbps\n", intel_dp->frl.trained_rate_gbps);
2288
2289        return 0;
2290}
2291
2292static bool intel_dp_is_hdmi_2_1_sink(struct intel_dp *intel_dp)
2293{
2294        if (drm_dp_is_branch(intel_dp->dpcd) &&
2295            intel_dp->has_hdmi_sink &&
2296            intel_dp_hdmi_sink_max_frl(intel_dp) > 0)
2297                return true;
2298
2299        return false;
2300}
2301
2302static
2303int intel_dp_pcon_set_tmds_mode(struct intel_dp *intel_dp)
2304{
2305        int ret;
2306        u8 buf = 0;
2307
2308        /* Set PCON source control mode */
2309        buf |= DP_PCON_ENABLE_SOURCE_CTL_MODE;
2310
2311        ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2312        if (ret < 0)
2313                return ret;
2314
2315        /* Set HDMI LINK ENABLE */
2316        buf |= DP_PCON_ENABLE_HDMI_LINK;
2317        ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2318        if (ret < 0)
2319                return ret;
2320
2321        return 0;
2322}
2323
2324void intel_dp_check_frl_training(struct intel_dp *intel_dp)
2325{
2326        struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
2327
2328        /*
2329         * Always go for FRL training if:
2330         * -PCON supports SRC_CTL_MODE (VESA DP2.0-HDMI2.1 PCON Spec Draft-1 Sec-7)
2331         * -sink is HDMI2.1
2332         */
2333        if (!(intel_dp->downstream_ports[2] & DP_PCON_SOURCE_CTL_MODE) ||
2334            !intel_dp_is_hdmi_2_1_sink(intel_dp) ||
2335            intel_dp->frl.is_trained)
2336                return;
2337
2338        if (intel_dp_pcon_start_frl_training(intel_dp) < 0) {
2339                int ret, mode;
2340
2341                drm_dbg(&dev_priv->drm, "Couldn't set FRL mode, continuing with TMDS mode\n");
2342                ret = intel_dp_pcon_set_tmds_mode(intel_dp);
2343                mode = drm_dp_pcon_hdmi_link_mode(&intel_dp->aux, NULL);
2344
2345                if (ret < 0 || mode != DP_PCON_HDMI_MODE_TMDS)
2346                        drm_dbg(&dev_priv->drm, "Issue with PCON, cannot set TMDS mode\n");
2347        } else {
2348                drm_dbg(&dev_priv->drm, "FRL training Completed\n");
2349        }
2350}
2351
2352static int
2353intel_dp_pcon_dsc_enc_slice_height(const struct intel_crtc_state *crtc_state)
2354{
2355        int vactive = crtc_state->hw.adjusted_mode.vdisplay;
2356
2357        return intel_hdmi_dsc_get_slice_height(vactive);
2358}
2359
2360static int
2361intel_dp_pcon_dsc_enc_slices(struct intel_dp *intel_dp,
2362                             const struct intel_crtc_state *crtc_state)
2363{
2364        struct intel_connector *intel_connector = intel_dp->attached_connector;
2365        struct drm_connector *connector = &intel_connector->base;
2366        int hdmi_throughput = connector->display_info.hdmi.dsc_cap.clk_per_slice;
2367        int hdmi_max_slices = connector->display_info.hdmi.dsc_cap.max_slices;
2368        int pcon_max_slices = drm_dp_pcon_dsc_max_slices(intel_dp->pcon_dsc_dpcd);
2369        int pcon_max_slice_width = drm_dp_pcon_dsc_max_slice_width(intel_dp->pcon_dsc_dpcd);
2370
2371        return intel_hdmi_dsc_get_num_slices(crtc_state, pcon_max_slices,
2372                                             pcon_max_slice_width,
2373                                             hdmi_max_slices, hdmi_throughput);
2374}
2375
2376static int
2377intel_dp_pcon_dsc_enc_bpp(struct intel_dp *intel_dp,
2378                          const struct intel_crtc_state *crtc_state,
2379                          int num_slices, int slice_width)
2380{
2381        struct intel_connector *intel_connector = intel_dp->attached_connector;
2382        struct drm_connector *connector = &intel_connector->base;
2383        int output_format = crtc_state->output_format;
2384        bool hdmi_all_bpp = connector->display_info.hdmi.dsc_cap.all_bpp;
2385        int pcon_fractional_bpp = drm_dp_pcon_dsc_bpp_incr(intel_dp->pcon_dsc_dpcd);
2386        int hdmi_max_chunk_bytes =
2387                connector->display_info.hdmi.dsc_cap.total_chunk_kbytes * 1024;
2388
2389        return intel_hdmi_dsc_get_bpp(pcon_fractional_bpp, slice_width,
2390                                      num_slices, output_format, hdmi_all_bpp,
2391                                      hdmi_max_chunk_bytes);
2392}
2393
2394void
2395intel_dp_pcon_dsc_configure(struct intel_dp *intel_dp,
2396                            const struct intel_crtc_state *crtc_state)
2397{
2398        u8 pps_param[6];
2399        int slice_height;
2400        int slice_width;
2401        int num_slices;
2402        int bits_per_pixel;
2403        int ret;
2404        struct intel_connector *intel_connector = intel_dp->attached_connector;
2405        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2406        struct drm_connector *connector;
2407        bool hdmi_is_dsc_1_2;
2408
2409        if (!intel_dp_is_hdmi_2_1_sink(intel_dp))
2410                return;
2411
2412        if (!intel_connector)
2413                return;
2414        connector = &intel_connector->base;
2415        hdmi_is_dsc_1_2 = connector->display_info.hdmi.dsc_cap.v_1p2;
2416
2417        if (!drm_dp_pcon_enc_is_dsc_1_2(intel_dp->pcon_dsc_dpcd) ||
2418            !hdmi_is_dsc_1_2)
2419                return;
2420
2421        slice_height = intel_dp_pcon_dsc_enc_slice_height(crtc_state);
2422        if (!slice_height)
2423                return;
2424
2425        num_slices = intel_dp_pcon_dsc_enc_slices(intel_dp, crtc_state);
2426        if (!num_slices)
2427                return;
2428
2429        slice_width = DIV_ROUND_UP(crtc_state->hw.adjusted_mode.hdisplay,
2430                                   num_slices);
2431
2432        bits_per_pixel = intel_dp_pcon_dsc_enc_bpp(intel_dp, crtc_state,
2433                                                   num_slices, slice_width);
2434        if (!bits_per_pixel)
2435                return;
2436
2437        pps_param[0] = slice_height & 0xFF;
2438        pps_param[1] = slice_height >> 8;
2439        pps_param[2] = slice_width & 0xFF;
2440        pps_param[3] = slice_width >> 8;
2441        pps_param[4] = bits_per_pixel & 0xFF;
2442        pps_param[5] = (bits_per_pixel >> 8) & 0x3;
2443
2444        ret = drm_dp_pcon_pps_override_param(&intel_dp->aux, pps_param);
2445        if (ret < 0)
2446                drm_dbg_kms(&i915->drm, "Failed to set pcon DSC\n");
2447}
2448
2449void intel_dp_configure_protocol_converter(struct intel_dp *intel_dp,
2450                                           const struct intel_crtc_state *crtc_state)
2451{
2452        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2453        u8 tmp;
2454
2455        if (intel_dp->dpcd[DP_DPCD_REV] < 0x13)
2456                return;
2457
2458        if (!drm_dp_is_branch(intel_dp->dpcd))
2459                return;
2460
2461        tmp = intel_dp->has_hdmi_sink ?
2462                DP_HDMI_DVI_OUTPUT_CONFIG : 0;
2463
2464        if (drm_dp_dpcd_writeb(&intel_dp->aux,
2465                               DP_PROTOCOL_CONVERTER_CONTROL_0, tmp) != 1)
2466                drm_dbg_kms(&i915->drm, "Failed to %s protocol converter HDMI mode\n",
2467                            enabledisable(intel_dp->has_hdmi_sink));
2468
2469        tmp = crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444 &&
2470                intel_dp->dfp.ycbcr_444_to_420 ? DP_CONVERSION_TO_YCBCR420_ENABLE : 0;
2471
2472        if (drm_dp_dpcd_writeb(&intel_dp->aux,
2473                               DP_PROTOCOL_CONVERTER_CONTROL_1, tmp) != 1)
2474                drm_dbg_kms(&i915->drm,
2475                            "Failed to %s protocol converter YCbCr 4:2:0 conversion mode\n",
2476                            enabledisable(intel_dp->dfp.ycbcr_444_to_420));
2477
2478        tmp = 0;
2479        if (intel_dp->dfp.rgb_to_ycbcr) {
2480                bool bt2020, bt709;
2481
2482                /*
2483                 * FIXME: Currently if userspace selects BT2020 or BT709, but PCON supports only
2484                 * RGB->YCbCr for BT601 colorspace, we go ahead with BT601, as default.
2485                 *
2486                 */
2487                tmp = DP_CONVERSION_BT601_RGB_YCBCR_ENABLE;
2488
2489                bt2020 = drm_dp_downstream_rgb_to_ycbcr_conversion(intel_dp->dpcd,
2490                                                                   intel_dp->downstream_ports,
2491                                                                   DP_DS_HDMI_BT2020_RGB_YCBCR_CONV);
2492                bt709 = drm_dp_downstream_rgb_to_ycbcr_conversion(intel_dp->dpcd,
2493                                                                  intel_dp->downstream_ports,
2494                                                                  DP_DS_HDMI_BT709_RGB_YCBCR_CONV);
2495                switch (crtc_state->infoframes.vsc.colorimetry) {
2496                case DP_COLORIMETRY_BT2020_RGB:
2497                case DP_COLORIMETRY_BT2020_YCC:
2498                        if (bt2020)
2499                                tmp = DP_CONVERSION_BT2020_RGB_YCBCR_ENABLE;
2500                        break;
2501                case DP_COLORIMETRY_BT709_YCC:
2502                case DP_COLORIMETRY_XVYCC_709:
2503                        if (bt709)
2504                                tmp = DP_CONVERSION_BT709_RGB_YCBCR_ENABLE;
2505                        break;
2506                default:
2507                        break;
2508                }
2509        }
2510
2511        if (drm_dp_pcon_convert_rgb_to_ycbcr(&intel_dp->aux, tmp) < 0)
2512                drm_dbg_kms(&i915->drm,
2513                           "Failed to %s protocol converter RGB->YCbCr conversion mode\n",
2514                           enabledisable(tmp));
2515}
2516
2517
2518bool intel_dp_get_colorimetry_status(struct intel_dp *intel_dp)
2519{
2520        u8 dprx = 0;
2521
2522        if (drm_dp_dpcd_readb(&intel_dp->aux, DP_DPRX_FEATURE_ENUMERATION_LIST,
2523                              &dprx) != 1)
2524                return false;
2525        return dprx & DP_VSC_SDP_EXT_FOR_COLORIMETRY_SUPPORTED;
2526}
2527
2528static void intel_dp_get_dsc_sink_cap(struct intel_dp *intel_dp)
2529{
2530        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2531
2532        /*
2533         * Clear the cached register set to avoid using stale values
2534         * for the sinks that do not support DSC.
2535         */
2536        memset(intel_dp->dsc_dpcd, 0, sizeof(intel_dp->dsc_dpcd));
2537
2538        /* Clear fec_capable to avoid using stale values */
2539        intel_dp->fec_capable = 0;
2540
2541        /* Cache the DSC DPCD if eDP or DP rev >= 1.4 */
2542        if (intel_dp->dpcd[DP_DPCD_REV] >= 0x14 ||
2543            intel_dp->edp_dpcd[0] >= DP_EDP_14) {
2544                if (drm_dp_dpcd_read(&intel_dp->aux, DP_DSC_SUPPORT,
2545                                     intel_dp->dsc_dpcd,
2546                                     sizeof(intel_dp->dsc_dpcd)) < 0)
2547                        drm_err(&i915->drm,
2548                                "Failed to read DPCD register 0x%x\n",
2549                                DP_DSC_SUPPORT);
2550
2551                drm_dbg_kms(&i915->drm, "DSC DPCD: %*ph\n",
2552                            (int)sizeof(intel_dp->dsc_dpcd),
2553                            intel_dp->dsc_dpcd);
2554
2555                /* FEC is supported only on DP 1.4 */
2556                if (!intel_dp_is_edp(intel_dp) &&
2557                    drm_dp_dpcd_readb(&intel_dp->aux, DP_FEC_CAPABILITY,
2558                                      &intel_dp->fec_capable) < 0)
2559                        drm_err(&i915->drm,
2560                                "Failed to read FEC DPCD register\n");
2561
2562                drm_dbg_kms(&i915->drm, "FEC CAPABILITY: %x\n",
2563                            intel_dp->fec_capable);
2564        }
2565}
2566
2567static void intel_edp_mso_mode_fixup(struct intel_connector *connector,
2568                                     struct drm_display_mode *mode)
2569{
2570        struct intel_dp *intel_dp = intel_attached_dp(connector);
2571        struct drm_i915_private *i915 = to_i915(connector->base.dev);
2572        int n = intel_dp->mso_link_count;
2573        int overlap = intel_dp->mso_pixel_overlap;
2574
2575        if (!mode || !n)
2576                return;
2577
2578        mode->hdisplay = (mode->hdisplay - overlap) * n;
2579        mode->hsync_start = (mode->hsync_start - overlap) * n;
2580        mode->hsync_end = (mode->hsync_end - overlap) * n;
2581        mode->htotal = (mode->htotal - overlap) * n;
2582        mode->clock *= n;
2583
2584        drm_mode_set_name(mode);
2585
2586        drm_dbg_kms(&i915->drm,
2587                    "[CONNECTOR:%d:%s] using generated MSO mode: ",
2588                    connector->base.base.id, connector->base.name);
2589        drm_mode_debug_printmodeline(mode);
2590}
2591
2592static void intel_edp_mso_init(struct intel_dp *intel_dp)
2593{
2594        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2595        struct intel_connector *connector = intel_dp->attached_connector;
2596        struct drm_display_info *info = &connector->base.display_info;
2597        u8 mso;
2598
2599        if (intel_dp->edp_dpcd[0] < DP_EDP_14)
2600                return;
2601
2602        if (drm_dp_dpcd_readb(&intel_dp->aux, DP_EDP_MSO_LINK_CAPABILITIES, &mso) != 1) {
2603                drm_err(&i915->drm, "Failed to read MSO cap\n");
2604                return;
2605        }
2606
2607        /* Valid configurations are SST or MSO 2x1, 2x2, 4x1 */
2608        mso &= DP_EDP_MSO_NUMBER_OF_LINKS_MASK;
2609        if (mso % 2 || mso > drm_dp_max_lane_count(intel_dp->dpcd)) {
2610                drm_err(&i915->drm, "Invalid MSO link count cap %u\n", mso);
2611                mso = 0;
2612        }
2613
2614        if (mso) {
2615                drm_dbg_kms(&i915->drm, "Sink MSO %ux%u configuration, pixel overlap %u\n",
2616                            mso, drm_dp_max_lane_count(intel_dp->dpcd) / mso,
2617                            info->mso_pixel_overlap);
2618                if (!HAS_MSO(i915)) {
2619                        drm_err(&i915->drm, "No source MSO support, disabling\n");
2620                        mso = 0;
2621                }
2622        }
2623
2624        intel_dp->mso_link_count = mso;
2625        intel_dp->mso_pixel_overlap = mso ? info->mso_pixel_overlap : 0;
2626}
2627
2628static bool
2629intel_edp_init_dpcd(struct intel_dp *intel_dp)
2630{
2631        struct drm_i915_private *dev_priv =
2632                to_i915(dp_to_dig_port(intel_dp)->base.base.dev);
2633
2634        /* this function is meant to be called only once */
2635        drm_WARN_ON(&dev_priv->drm, intel_dp->dpcd[DP_DPCD_REV] != 0);
2636
2637        if (drm_dp_read_dpcd_caps(&intel_dp->aux, intel_dp->dpcd) != 0)
2638                return false;
2639
2640        drm_dp_read_desc(&intel_dp->aux, &intel_dp->desc,
2641                         drm_dp_is_branch(intel_dp->dpcd));
2642
2643        /*
2644         * Read the eDP display control registers.
2645         *
2646         * Do this independent of DP_DPCD_DISPLAY_CONTROL_CAPABLE bit in
2647         * DP_EDP_CONFIGURATION_CAP, because some buggy displays do not have it
2648         * set, but require eDP 1.4+ detection (e.g. for supported link rates
2649         * method). The display control registers should read zero if they're
2650         * not supported anyway.
2651         */
2652        if (drm_dp_dpcd_read(&intel_dp->aux, DP_EDP_DPCD_REV,
2653                             intel_dp->edp_dpcd, sizeof(intel_dp->edp_dpcd)) ==
2654                             sizeof(intel_dp->edp_dpcd)) {
2655                drm_dbg_kms(&dev_priv->drm, "eDP DPCD: %*ph\n",
2656                            (int)sizeof(intel_dp->edp_dpcd),
2657                            intel_dp->edp_dpcd);
2658
2659                intel_dp->use_max_params = intel_dp->edp_dpcd[0] < DP_EDP_14;
2660        }
2661
2662        /*
2663         * This has to be called after intel_dp->edp_dpcd is filled, PSR checks
2664         * for SET_POWER_CAPABLE bit in intel_dp->edp_dpcd[1]
2665         */
2666        intel_psr_init_dpcd(intel_dp);
2667
2668        /* Clear the default sink rates */
2669        intel_dp->num_sink_rates = 0;
2670
2671        /* Read the eDP 1.4+ supported link rates. */
2672        if (intel_dp->edp_dpcd[0] >= DP_EDP_14) {
2673                __le16 sink_rates[DP_MAX_SUPPORTED_RATES];
2674                int i;
2675
2676                drm_dp_dpcd_read(&intel_dp->aux, DP_SUPPORTED_LINK_RATES,
2677                                sink_rates, sizeof(sink_rates));
2678
2679                for (i = 0; i < ARRAY_SIZE(sink_rates); i++) {
2680                        int val = le16_to_cpu(sink_rates[i]);
2681
2682                        if (val == 0)
2683                                break;
2684
2685                        /* Value read multiplied by 200kHz gives the per-lane
2686                         * link rate in kHz. The source rates are, however,
2687                         * stored in terms of LS_Clk kHz. The full conversion
2688                         * back to symbols is
2689                         * (val * 200kHz)*(8/10 ch. encoding)*(1/8 bit to Byte)
2690                         */
2691                        intel_dp->sink_rates[i] = (val * 200) / 10;
2692                }
2693                intel_dp->num_sink_rates = i;
2694        }
2695
2696        /*
2697         * Use DP_LINK_RATE_SET if DP_SUPPORTED_LINK_RATES are available,
2698         * default to DP_MAX_LINK_RATE and DP_LINK_BW_SET otherwise.
2699         */
2700        if (intel_dp->num_sink_rates)
2701                intel_dp->use_rate_select = true;
2702        else
2703                intel_dp_set_sink_rates(intel_dp);
2704        intel_dp_set_max_sink_lane_count(intel_dp);
2705
2706        intel_dp_set_common_rates(intel_dp);
2707        intel_dp_reset_max_link_params(intel_dp);
2708
2709        /* Read the eDP DSC DPCD registers */
2710        if (DISPLAY_VER(dev_priv) >= 10)
2711                intel_dp_get_dsc_sink_cap(intel_dp);
2712
2713        /*
2714         * If needed, program our source OUI so we can make various Intel-specific AUX services
2715         * available (such as HDR backlight controls)
2716         */
2717        intel_edp_init_source_oui(intel_dp, true);
2718
2719        return true;
2720}
2721
2722static bool
2723intel_dp_has_sink_count(struct intel_dp *intel_dp)
2724{
2725        if (!intel_dp->attached_connector)
2726                return false;
2727
2728        return drm_dp_read_sink_count_cap(&intel_dp->attached_connector->base,
2729                                          intel_dp->dpcd,
2730                                          &intel_dp->desc);
2731}
2732
2733static bool
2734intel_dp_get_dpcd(struct intel_dp *intel_dp)
2735{
2736        int ret;
2737
2738        if (intel_dp_init_lttpr_and_dprx_caps(intel_dp) < 0)
2739                return false;
2740
2741        /*
2742         * Don't clobber cached eDP rates. Also skip re-reading
2743         * the OUI/ID since we know it won't change.
2744         */
2745        if (!intel_dp_is_edp(intel_dp)) {
2746                drm_dp_read_desc(&intel_dp->aux, &intel_dp->desc,
2747                                 drm_dp_is_branch(intel_dp->dpcd));
2748
2749                intel_dp_set_sink_rates(intel_dp);
2750                intel_dp_set_max_sink_lane_count(intel_dp);
2751                intel_dp_set_common_rates(intel_dp);
2752        }
2753
2754        if (intel_dp_has_sink_count(intel_dp)) {
2755                ret = drm_dp_read_sink_count(&intel_dp->aux);
2756                if (ret < 0)
2757                        return false;
2758
2759                /*
2760                 * Sink count can change between short pulse hpd hence
2761                 * a member variable in intel_dp will track any changes
2762                 * between short pulse interrupts.
2763                 */
2764                intel_dp->sink_count = ret;
2765
2766                /*
2767                 * SINK_COUNT == 0 and DOWNSTREAM_PORT_PRESENT == 1 implies that
2768                 * a dongle is present but no display. Unless we require to know
2769                 * if a dongle is present or not, we don't need to update
2770                 * downstream port information. So, an early return here saves
2771                 * time from performing other operations which are not required.
2772                 */
2773                if (!intel_dp->sink_count)
2774                        return false;
2775        }
2776
2777        return drm_dp_read_downstream_info(&intel_dp->aux, intel_dp->dpcd,
2778                                           intel_dp->downstream_ports) == 0;
2779}
2780
2781static bool
2782intel_dp_can_mst(struct intel_dp *intel_dp)
2783{
2784        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2785
2786        return i915->params.enable_dp_mst &&
2787                intel_dp_mst_source_support(intel_dp) &&
2788                drm_dp_read_mst_cap(&intel_dp->aux, intel_dp->dpcd);
2789}
2790
2791static void
2792intel_dp_configure_mst(struct intel_dp *intel_dp)
2793{
2794        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2795        struct intel_encoder *encoder =
2796                &dp_to_dig_port(intel_dp)->base;
2797        bool sink_can_mst = drm_dp_read_mst_cap(&intel_dp->aux, intel_dp->dpcd);
2798
2799        drm_dbg_kms(&i915->drm,
2800                    "[ENCODER:%d:%s] MST support: port: %s, sink: %s, modparam: %s\n",
2801                    encoder->base.base.id, encoder->base.name,
2802                    yesno(intel_dp_mst_source_support(intel_dp)), yesno(sink_can_mst),
2803                    yesno(i915->params.enable_dp_mst));
2804
2805        if (!intel_dp_mst_source_support(intel_dp))
2806                return;
2807
2808        intel_dp->is_mst = sink_can_mst &&
2809                i915->params.enable_dp_mst;
2810
2811        drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr,
2812                                        intel_dp->is_mst);
2813}
2814
2815static bool
2816intel_dp_get_sink_irq_esi(struct intel_dp *intel_dp, u8 *sink_irq_vector)
2817{
2818        return drm_dp_dpcd_read(&intel_dp->aux, DP_SINK_COUNT_ESI,
2819                                sink_irq_vector, DP_DPRX_ESI_LEN) ==
2820                DP_DPRX_ESI_LEN;
2821}
2822
2823bool
2824intel_dp_needs_vsc_sdp(const struct intel_crtc_state *crtc_state,
2825                       const struct drm_connector_state *conn_state)
2826{
2827        /*
2828         * As per DP 1.4a spec section 2.2.4.3 [MSA Field for Indication
2829         * of Color Encoding Format and Content Color Gamut], in order to
2830         * sending YCBCR 420 or HDR BT.2020 signals we should use DP VSC SDP.
2831         */
2832        if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
2833                return true;
2834
2835        switch (conn_state->colorspace) {
2836        case DRM_MODE_COLORIMETRY_SYCC_601:
2837        case DRM_MODE_COLORIMETRY_OPYCC_601:
2838        case DRM_MODE_COLORIMETRY_BT2020_YCC:
2839        case DRM_MODE_COLORIMETRY_BT2020_RGB:
2840        case DRM_MODE_COLORIMETRY_BT2020_CYCC:
2841                return true;
2842        default:
2843                break;
2844        }
2845
2846        return false;
2847}
2848
2849static ssize_t intel_dp_vsc_sdp_pack(const struct drm_dp_vsc_sdp *vsc,
2850                                     struct dp_sdp *sdp, size_t size)
2851{
2852        size_t length = sizeof(struct dp_sdp);
2853
2854        if (size < length)
2855                return -ENOSPC;
2856
2857        memset(sdp, 0, size);
2858
2859        /*
2860         * Prepare VSC Header for SU as per DP 1.4a spec, Table 2-119
2861         * VSC SDP Header Bytes
2862         */
2863        sdp->sdp_header.HB0 = 0; /* Secondary-Data Packet ID = 0 */
2864        sdp->sdp_header.HB1 = vsc->sdp_type; /* Secondary-data Packet Type */
2865        sdp->sdp_header.HB2 = vsc->revision; /* Revision Number */
2866        sdp->sdp_header.HB3 = vsc->length; /* Number of Valid Data Bytes */
2867
2868        /*
2869         * Only revision 0x5 supports Pixel Encoding/Colorimetry Format as
2870         * per DP 1.4a spec.
2871         */
2872        if (vsc->revision != 0x5)
2873                goto out;
2874
2875        /* VSC SDP Payload for DB16 through DB18 */
2876        /* Pixel Encoding and Colorimetry Formats  */
2877        sdp->db[16] = (vsc->pixelformat & 0xf) << 4; /* DB16[7:4] */
2878        sdp->db[16] |= vsc->colorimetry & 0xf; /* DB16[3:0] */
2879
2880        switch (vsc->bpc) {
2881        case 6:
2882                /* 6bpc: 0x0 */
2883                break;
2884        case 8:
2885                sdp->db[17] = 0x1; /* DB17[3:0] */
2886                break;
2887        case 10:
2888                sdp->db[17] = 0x2;
2889                break;
2890        case 12:
2891                sdp->db[17] = 0x3;
2892                break;
2893        case 16:
2894                sdp->db[17] = 0x4;
2895                break;
2896        default:
2897                MISSING_CASE(vsc->bpc);
2898                break;
2899        }
2900        /* Dynamic Range and Component Bit Depth */
2901        if (vsc->dynamic_range == DP_DYNAMIC_RANGE_CTA)
2902                sdp->db[17] |= 0x80;  /* DB17[7] */
2903
2904        /* Content Type */
2905        sdp->db[18] = vsc->content_type & 0x7;
2906
2907out:
2908        return length;
2909}
2910
2911static ssize_t
2912intel_dp_hdr_metadata_infoframe_sdp_pack(const struct hdmi_drm_infoframe *drm_infoframe,
2913                                         struct dp_sdp *sdp,
2914                                         size_t size)
2915{
2916        size_t length = sizeof(struct dp_sdp);
2917        const int infoframe_size = HDMI_INFOFRAME_HEADER_SIZE + HDMI_DRM_INFOFRAME_SIZE;
2918        unsigned char buf[HDMI_INFOFRAME_HEADER_SIZE + HDMI_DRM_INFOFRAME_SIZE];
2919        ssize_t len;
2920
2921        if (size < length)
2922                return -ENOSPC;
2923
2924        memset(sdp, 0, size);
2925
2926        len = hdmi_drm_infoframe_pack_only(drm_infoframe, buf, sizeof(buf));
2927        if (len < 0) {
2928                DRM_DEBUG_KMS("buffer size is smaller than hdr metadata infoframe\n");
2929                return -ENOSPC;
2930        }
2931
2932        if (len != infoframe_size) {
2933                DRM_DEBUG_KMS("wrong static hdr metadata size\n");
2934                return -ENOSPC;
2935        }
2936
2937        /*
2938         * Set up the infoframe sdp packet for HDR static metadata.
2939         * Prepare VSC Header for SU as per DP 1.4a spec,
2940         * Table 2-100 and Table 2-101
2941         */
2942
2943        /* Secondary-Data Packet ID, 00h for non-Audio INFOFRAME */
2944        sdp->sdp_header.HB0 = 0;
2945        /*
2946         * Packet Type 80h + Non-audio INFOFRAME Type value
2947         * HDMI_INFOFRAME_TYPE_DRM: 0x87
2948         * - 80h + Non-audio INFOFRAME Type value
2949         * - InfoFrame Type: 0x07
2950         *    [CTA-861-G Table-42 Dynamic Range and Mastering InfoFrame]
2951         */
2952        sdp->sdp_header.HB1 = drm_infoframe->type;
2953        /*
2954         * Least Significant Eight Bits of (Data Byte Count – 1)
2955         * infoframe_size - 1
2956         */
2957        sdp->sdp_header.HB2 = 0x1D;
2958        /* INFOFRAME SDP Version Number */
2959        sdp->sdp_header.HB3 = (0x13 << 2);
2960        /* CTA Header Byte 2 (INFOFRAME Version Number) */
2961        sdp->db[0] = drm_infoframe->version;
2962        /* CTA Header Byte 3 (Length of INFOFRAME): HDMI_DRM_INFOFRAME_SIZE */
2963        sdp->db[1] = drm_infoframe->length;
2964        /*
2965         * Copy HDMI_DRM_INFOFRAME_SIZE size from a buffer after
2966         * HDMI_INFOFRAME_HEADER_SIZE
2967         */
2968        BUILD_BUG_ON(sizeof(sdp->db) < HDMI_DRM_INFOFRAME_SIZE + 2);
2969        memcpy(&sdp->db[2], &buf[HDMI_INFOFRAME_HEADER_SIZE],
2970               HDMI_DRM_INFOFRAME_SIZE);
2971
2972        /*
2973         * Size of DP infoframe sdp packet for HDR static metadata consists of
2974         * - DP SDP Header(struct dp_sdp_header): 4 bytes
2975         * - Two Data Blocks: 2 bytes
2976         *    CTA Header Byte2 (INFOFRAME Version Number)
2977         *    CTA Header Byte3 (Length of INFOFRAME)
2978         * - HDMI_DRM_INFOFRAME_SIZE: 26 bytes
2979         *
2980         * Prior to GEN11's GMP register size is identical to DP HDR static metadata
2981         * infoframe size. But GEN11+ has larger than that size, write_infoframe
2982         * will pad rest of the size.
2983         */
2984        return sizeof(struct dp_sdp_header) + 2 + HDMI_DRM_INFOFRAME_SIZE;
2985}
2986
2987static void intel_write_dp_sdp(struct intel_encoder *encoder,
2988                               const struct intel_crtc_state *crtc_state,
2989                               unsigned int type)
2990{
2991        struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2992        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2993        struct dp_sdp sdp = {};
2994        ssize_t len;
2995
2996        if ((crtc_state->infoframes.enable &
2997             intel_hdmi_infoframe_enable(type)) == 0)
2998                return;
2999
3000        switch (type) {
3001        case DP_SDP_VSC:
3002                len = intel_dp_vsc_sdp_pack(&crtc_state->infoframes.vsc, &sdp,
3003                                            sizeof(sdp));
3004                break;
3005        case HDMI_PACKET_TYPE_GAMUT_METADATA:
3006                len = intel_dp_hdr_metadata_infoframe_sdp_pack(&crtc_state->infoframes.drm.drm,
3007                                                               &sdp, sizeof(sdp));
3008                break;
3009        default:
3010                MISSING_CASE(type);
3011                return;
3012        }
3013
3014        if (drm_WARN_ON(&dev_priv->drm, len < 0))
3015                return;
3016
3017        dig_port->write_infoframe(encoder, crtc_state, type, &sdp, len);
3018}
3019
3020void intel_write_dp_vsc_sdp(struct intel_encoder *encoder,
3021                            const struct intel_crtc_state *crtc_state,
3022                            const struct drm_dp_vsc_sdp *vsc)
3023{
3024        struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3025        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3026        struct dp_sdp sdp = {};
3027        ssize_t len;
3028
3029        len = intel_dp_vsc_sdp_pack(vsc, &sdp, sizeof(sdp));
3030
3031        if (drm_WARN_ON(&dev_priv->drm, len < 0))
3032                return;
3033
3034        dig_port->write_infoframe(encoder, crtc_state, DP_SDP_VSC,
3035                                        &sdp, len);
3036}
3037
3038void intel_dp_set_infoframes(struct intel_encoder *encoder,
3039                             bool enable,
3040                             const struct intel_crtc_state *crtc_state,
3041                             const struct drm_connector_state *conn_state)
3042{
3043        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3044        i915_reg_t reg = HSW_TVIDEO_DIP_CTL(crtc_state->cpu_transcoder);
3045        u32 dip_enable = VIDEO_DIP_ENABLE_AVI_HSW | VIDEO_DIP_ENABLE_GCP_HSW |
3046                         VIDEO_DIP_ENABLE_VS_HSW | VIDEO_DIP_ENABLE_GMP_HSW |
3047                         VIDEO_DIP_ENABLE_SPD_HSW | VIDEO_DIP_ENABLE_DRM_GLK;
3048        u32 val = intel_de_read(dev_priv, reg) & ~dip_enable;
3049
3050        /* TODO: Add DSC case (DIP_ENABLE_PPS) */
3051        /* When PSR is enabled, this routine doesn't disable VSC DIP */
3052        if (!crtc_state->has_psr)
3053                val &= ~VIDEO_DIP_ENABLE_VSC_HSW;
3054
3055        intel_de_write(dev_priv, reg, val);
3056        intel_de_posting_read(dev_priv, reg);
3057
3058        if (!enable)
3059                return;
3060
3061        /* When PSR is enabled, VSC SDP is handled by PSR routine */
3062        if (!crtc_state->has_psr)
3063                intel_write_dp_sdp(encoder, crtc_state, DP_SDP_VSC);
3064
3065        intel_write_dp_sdp(encoder, crtc_state, HDMI_PACKET_TYPE_GAMUT_METADATA);
3066}
3067
3068static int intel_dp_vsc_sdp_unpack(struct drm_dp_vsc_sdp *vsc,
3069                                   const void *buffer, size_t size)
3070{
3071        const struct dp_sdp *sdp = buffer;
3072
3073        if (size < sizeof(struct dp_sdp))
3074                return -EINVAL;
3075
3076        memset(vsc, 0, sizeof(*vsc));
3077
3078        if (sdp->sdp_header.HB0 != 0)
3079                return -EINVAL;
3080
3081        if (sdp->sdp_header.HB1 != DP_SDP_VSC)
3082                return -EINVAL;
3083
3084        vsc->sdp_type = sdp->sdp_header.HB1;
3085        vsc->revision = sdp->sdp_header.HB2;
3086        vsc->length = sdp->sdp_header.HB3;
3087
3088        if ((sdp->sdp_header.HB2 == 0x2 && sdp->sdp_header.HB3 == 0x8) ||
3089            (sdp->sdp_header.HB2 == 0x4 && sdp->sdp_header.HB3 == 0xe)) {
3090                /*
3091                 * - HB2 = 0x2, HB3 = 0x8
3092                 *   VSC SDP supporting 3D stereo + PSR
3093                 * - HB2 = 0x4, HB3 = 0xe
3094                 *   VSC SDP supporting 3D stereo + PSR2 with Y-coordinate of
3095                 *   first scan line of the SU region (applies to eDP v1.4b
3096                 *   and higher).
3097                 */
3098                return 0;
3099        } else if (sdp->sdp_header.HB2 == 0x5 && sdp->sdp_header.HB3 == 0x13) {
3100                /*
3101                 * - HB2 = 0x5, HB3 = 0x13
3102                 *   VSC SDP supporting 3D stereo + PSR2 + Pixel Encoding/Colorimetry
3103                 *   Format.
3104                 */
3105                vsc->pixelformat = (sdp->db[16] >> 4) & 0xf;
3106                vsc->colorimetry = sdp->db[16] & 0xf;
3107                vsc->dynamic_range = (sdp->db[17] >> 7) & 0x1;
3108
3109                switch (sdp->db[17] & 0x7) {
3110                case 0x0:
3111                        vsc->bpc = 6;
3112                        break;
3113                case 0x1:
3114                        vsc->bpc = 8;
3115                        break;
3116                case 0x2:
3117                        vsc->bpc = 10;
3118                        break;
3119                case 0x3:
3120                        vsc->bpc = 12;
3121                        break;
3122                case 0x4:
3123                        vsc->bpc = 16;
3124                        break;
3125                default:
3126                        MISSING_CASE(sdp->db[17] & 0x7);
3127                        return -EINVAL;
3128                }
3129
3130                vsc->content_type = sdp->db[18] & 0x7;
3131        } else {
3132                return -EINVAL;
3133        }
3134
3135        return 0;
3136}
3137
3138static int
3139intel_dp_hdr_metadata_infoframe_sdp_unpack(struct hdmi_drm_infoframe *drm_infoframe,
3140                                           const void *buffer, size_t size)
3141{
3142        int ret;
3143
3144        const struct dp_sdp *sdp = buffer;
3145
3146        if (size < sizeof(struct dp_sdp))
3147                return -EINVAL;
3148
3149        if (sdp->sdp_header.HB0 != 0)
3150                return -EINVAL;
3151
3152        if (sdp->sdp_header.HB1 != HDMI_INFOFRAME_TYPE_DRM)
3153                return -EINVAL;
3154
3155        /*
3156         * Least Significant Eight Bits of (Data Byte Count – 1)
3157         * 1Dh (i.e., Data Byte Count = 30 bytes).
3158         */
3159        if (sdp->sdp_header.HB2 != 0x1D)
3160                return -EINVAL;
3161
3162        /* Most Significant Two Bits of (Data Byte Count – 1), Clear to 00b. */
3163        if ((sdp->sdp_header.HB3 & 0x3) != 0)
3164                return -EINVAL;
3165
3166        /* INFOFRAME SDP Version Number */
3167        if (((sdp->sdp_header.HB3 >> 2) & 0x3f) != 0x13)
3168                return -EINVAL;
3169
3170        /* CTA Header Byte 2 (INFOFRAME Version Number) */
3171        if (sdp->db[0] != 1)
3172                return -EINVAL;
3173
3174        /* CTA Header Byte 3 (Length of INFOFRAME): HDMI_DRM_INFOFRAME_SIZE */
3175        if (sdp->db[1] != HDMI_DRM_INFOFRAME_SIZE)
3176                return -EINVAL;
3177
3178        ret = hdmi_drm_infoframe_unpack_only(drm_infoframe, &sdp->db[2],
3179                                             HDMI_DRM_INFOFRAME_SIZE);
3180
3181        return ret;
3182}
3183
3184static void intel_read_dp_vsc_sdp(struct intel_encoder *encoder,
3185                                  struct intel_crtc_state *crtc_state,
3186                                  struct drm_dp_vsc_sdp *vsc)
3187{
3188        struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3189        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3190        unsigned int type = DP_SDP_VSC;
3191        struct dp_sdp sdp = {};
3192        int ret;
3193
3194        /* When PSR is enabled, VSC SDP is handled by PSR routine */
3195        if (crtc_state->has_psr)
3196                return;
3197
3198        if ((crtc_state->infoframes.enable &
3199             intel_hdmi_infoframe_enable(type)) == 0)
3200                return;
3201
3202        dig_port->read_infoframe(encoder, crtc_state, type, &sdp, sizeof(sdp));
3203
3204        ret = intel_dp_vsc_sdp_unpack(vsc, &sdp, sizeof(sdp));
3205
3206        if (ret)
3207                drm_dbg_kms(&dev_priv->drm, "Failed to unpack DP VSC SDP\n");
3208}
3209
3210static void intel_read_dp_hdr_metadata_infoframe_sdp(struct intel_encoder *encoder,
3211                                                     struct intel_crtc_state *crtc_state,
3212                                                     struct hdmi_drm_infoframe *drm_infoframe)
3213{
3214        struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3215        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3216        unsigned int type = HDMI_PACKET_TYPE_GAMUT_METADATA;
3217        struct dp_sdp sdp = {};
3218        int ret;
3219
3220        if ((crtc_state->infoframes.enable &
3221            intel_hdmi_infoframe_enable(type)) == 0)
3222                return;
3223
3224        dig_port->read_infoframe(encoder, crtc_state, type, &sdp,
3225                                 sizeof(sdp));
3226
3227        ret = intel_dp_hdr_metadata_infoframe_sdp_unpack(drm_infoframe, &sdp,
3228                                                         sizeof(sdp));
3229
3230        if (ret)
3231                drm_dbg_kms(&dev_priv->drm,
3232                            "Failed to unpack DP HDR Metadata Infoframe SDP\n");
3233}
3234
3235void intel_read_dp_sdp(struct intel_encoder *encoder,
3236                       struct intel_crtc_state *crtc_state,
3237                       unsigned int type)
3238{
3239        switch (type) {
3240        case DP_SDP_VSC:
3241                intel_read_dp_vsc_sdp(encoder, crtc_state,
3242                                      &crtc_state->infoframes.vsc);
3243                break;
3244        case HDMI_PACKET_TYPE_GAMUT_METADATA:
3245                intel_read_dp_hdr_metadata_infoframe_sdp(encoder, crtc_state,
3246                                                         &crtc_state->infoframes.drm.drm);
3247                break;
3248        default:
3249                MISSING_CASE(type);
3250                break;
3251        }
3252}
3253
3254static u8 intel_dp_autotest_link_training(struct intel_dp *intel_dp)
3255{
3256        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3257        int status = 0;
3258        int test_link_rate;
3259        u8 test_lane_count, test_link_bw;
3260        /* (DP CTS 1.2)
3261         * 4.3.1.11
3262         */
3263        /* Read the TEST_LANE_COUNT and TEST_LINK_RTAE fields (DP CTS 3.1.4) */
3264        status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_LANE_COUNT,
3265                                   &test_lane_count);
3266
3267        if (status <= 0) {
3268                drm_dbg_kms(&i915->drm, "Lane count read failed\n");
3269                return DP_TEST_NAK;
3270        }
3271        test_lane_count &= DP_MAX_LANE_COUNT_MASK;
3272
3273        status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_LINK_RATE,
3274                                   &test_link_bw);
3275        if (status <= 0) {
3276                drm_dbg_kms(&i915->drm, "Link Rate read failed\n");
3277                return DP_TEST_NAK;
3278        }
3279        test_link_rate = drm_dp_bw_code_to_link_rate(test_link_bw);
3280
3281        /* Validate the requested link rate and lane count */
3282        if (!intel_dp_link_params_valid(intel_dp, test_link_rate,
3283                                        test_lane_count))
3284                return DP_TEST_NAK;
3285
3286        intel_dp->compliance.test_lane_count = test_lane_count;
3287        intel_dp->compliance.test_link_rate = test_link_rate;
3288
3289        return DP_TEST_ACK;
3290}
3291
3292static u8 intel_dp_autotest_video_pattern(struct intel_dp *intel_dp)
3293{
3294        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3295        u8 test_pattern;
3296        u8 test_misc;
3297        __be16 h_width, v_height;
3298        int status = 0;
3299
3300        /* Read the TEST_PATTERN (DP CTS 3.1.5) */
3301        status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_PATTERN,
3302                                   &test_pattern);
3303        if (status <= 0) {
3304                drm_dbg_kms(&i915->drm, "Test pattern read failed\n");
3305                return DP_TEST_NAK;
3306        }
3307        if (test_pattern != DP_COLOR_RAMP)
3308                return DP_TEST_NAK;
3309
3310        status = drm_dp_dpcd_read(&intel_dp->aux, DP_TEST_H_WIDTH_HI,
3311                                  &h_width, 2);
3312        if (status <= 0) {
3313                drm_dbg_kms(&i915->drm, "H Width read failed\n");
3314                return DP_TEST_NAK;
3315        }
3316
3317        status = drm_dp_dpcd_read(&intel_dp->aux, DP_TEST_V_HEIGHT_HI,
3318                                  &v_height, 2);
3319        if (status <= 0) {
3320                drm_dbg_kms(&i915->drm, "V Height read failed\n");
3321                return DP_TEST_NAK;
3322        }
3323
3324        status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_MISC0,
3325                                   &test_misc);
3326        if (status <= 0) {
3327                drm_dbg_kms(&i915->drm, "TEST MISC read failed\n");
3328                return DP_TEST_NAK;
3329        }
3330        if ((test_misc & DP_TEST_COLOR_FORMAT_MASK) != DP_COLOR_FORMAT_RGB)
3331                return DP_TEST_NAK;
3332        if (test_misc & DP_TEST_DYNAMIC_RANGE_CEA)
3333                return DP_TEST_NAK;
3334        switch (test_misc & DP_TEST_BIT_DEPTH_MASK) {
3335        case DP_TEST_BIT_DEPTH_6:
3336                intel_dp->compliance.test_data.bpc = 6;
3337                break;
3338        case DP_TEST_BIT_DEPTH_8:
3339                intel_dp->compliance.test_data.bpc = 8;
3340                break;
3341        default:
3342                return DP_TEST_NAK;
3343        }
3344
3345        intel_dp->compliance.test_data.video_pattern = test_pattern;
3346        intel_dp->compliance.test_data.hdisplay = be16_to_cpu(h_width);
3347        intel_dp->compliance.test_data.vdisplay = be16_to_cpu(v_height);
3348        /* Set test active flag here so userspace doesn't interrupt things */
3349        intel_dp->compliance.test_active = true;
3350
3351        return DP_TEST_ACK;
3352}
3353
3354static u8 intel_dp_autotest_edid(struct intel_dp *intel_dp)
3355{
3356        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3357        u8 test_result = DP_TEST_ACK;
3358        struct intel_connector *intel_connector = intel_dp->attached_connector;
3359        struct drm_connector *connector = &intel_connector->base;
3360
3361        if (intel_connector->detect_edid == NULL ||
3362            connector->edid_corrupt ||
3363            intel_dp->aux.i2c_defer_count > 6) {
3364                /* Check EDID read for NACKs, DEFERs and corruption
3365                 * (DP CTS 1.2 Core r1.1)
3366                 *    4.2.2.4 : Failed EDID read, I2C_NAK
3367                 *    4.2.2.5 : Failed EDID read, I2C_DEFER
3368                 *    4.2.2.6 : EDID corruption detected
3369                 * Use failsafe mode for all cases
3370                 */
3371                if (intel_dp->aux.i2c_nack_count > 0 ||
3372                        intel_dp->aux.i2c_defer_count > 0)
3373                        drm_dbg_kms(&i915->drm,
3374                                    "EDID read had %d NACKs, %d DEFERs\n",
3375                                    intel_dp->aux.i2c_nack_count,
3376                                    intel_dp->aux.i2c_defer_count);
3377                intel_dp->compliance.test_data.edid = INTEL_DP_RESOLUTION_FAILSAFE;
3378        } else {
3379                struct edid *block = intel_connector->detect_edid;
3380
3381                /* We have to write the checksum
3382                 * of the last block read
3383                 */
3384                block += intel_connector->detect_edid->extensions;
3385
3386                if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_EDID_CHECKSUM,
3387                                       block->checksum) <= 0)
3388                        drm_dbg_kms(&i915->drm,
3389                                    "Failed to write EDID checksum\n");
3390
3391                test_result = DP_TEST_ACK | DP_TEST_EDID_CHECKSUM_WRITE;
3392                intel_dp->compliance.test_data.edid = INTEL_DP_RESOLUTION_PREFERRED;
3393        }
3394
3395        /* Set test active flag here so userspace doesn't interrupt things */
3396        intel_dp->compliance.test_active = true;
3397
3398        return test_result;
3399}
3400
3401static void intel_dp_phy_pattern_update(struct intel_dp *intel_dp,
3402                                        const struct intel_crtc_state *crtc_state)
3403{
3404        struct drm_i915_private *dev_priv =
3405                        to_i915(dp_to_dig_port(intel_dp)->base.base.dev);
3406        struct drm_dp_phy_test_params *data =
3407                        &intel_dp->compliance.test_data.phytest;
3408        struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3409        enum pipe pipe = crtc->pipe;
3410        u32 pattern_val;
3411
3412        switch (data->phy_pattern) {
3413        case DP_PHY_TEST_PATTERN_NONE:
3414                DRM_DEBUG_KMS("Disable Phy Test Pattern\n");
3415                intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe), 0x0);
3416                break;
3417        case DP_PHY_TEST_PATTERN_D10_2:
3418                DRM_DEBUG_KMS("Set D10.2 Phy Test Pattern\n");
3419                intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe),
3420                               DDI_DP_COMP_CTL_ENABLE | DDI_DP_COMP_CTL_D10_2);
3421                break;
3422        case DP_PHY_TEST_PATTERN_ERROR_COUNT:
3423                DRM_DEBUG_KMS("Set Error Count Phy Test Pattern\n");
3424                intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe),
3425                               DDI_DP_COMP_CTL_ENABLE |
3426                               DDI_DP_COMP_CTL_SCRAMBLED_0);
3427                break;
3428        case DP_PHY_TEST_PATTERN_PRBS7:
3429                DRM_DEBUG_KMS("Set PRBS7 Phy Test Pattern\n");
3430                intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe),
3431                               DDI_DP_COMP_CTL_ENABLE | DDI_DP_COMP_CTL_PRBS7);
3432                break;
3433        case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
3434                /*
3435                 * FIXME: Ideally pattern should come from DPCD 0x250. As
3436                 * current firmware of DPR-100 could not set it, so hardcoding
3437                 * now for complaince test.
3438                 */
3439                DRM_DEBUG_KMS("Set 80Bit Custom Phy Test Pattern 0x3e0f83e0 0x0f83e0f8 0x0000f83e\n");
3440                pattern_val = 0x3e0f83e0;
3441                intel_de_write(dev_priv, DDI_DP_COMP_PAT(pipe, 0), pattern_val);
3442                pattern_val = 0x0f83e0f8;
3443                intel_de_write(dev_priv, DDI_DP_COMP_PAT(pipe, 1), pattern_val);
3444                pattern_val = 0x0000f83e;
3445                intel_de_write(dev_priv, DDI_DP_COMP_PAT(pipe, 2), pattern_val);
3446                intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe),
3447                               DDI_DP_COMP_CTL_ENABLE |
3448                               DDI_DP_COMP_CTL_CUSTOM80);
3449                break;
3450        case DP_PHY_TEST_PATTERN_CP2520:
3451                /*
3452                 * FIXME: Ideally pattern should come from DPCD 0x24A. As
3453                 * current firmware of DPR-100 could not set it, so hardcoding
3454                 * now for complaince test.
3455                 */
3456                DRM_DEBUG_KMS("Set HBR2 compliance Phy Test Pattern\n");
3457                pattern_val = 0xFB;
3458                intel_de_write(dev_priv, DDI_DP_COMP_CTL(pipe),
3459                               DDI_DP_COMP_CTL_ENABLE | DDI_DP_COMP_CTL_HBR2 |
3460                               pattern_val);
3461                break;
3462        default:
3463                WARN(1, "Invalid Phy Test Pattern\n");
3464        }
3465}
3466
3467static void
3468intel_dp_autotest_phy_ddi_disable(struct intel_dp *intel_dp,
3469                                  const struct intel_crtc_state *crtc_state)
3470{
3471        struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
3472        struct drm_device *dev = dig_port->base.base.dev;
3473        struct drm_i915_private *dev_priv = to_i915(dev);
3474        struct intel_crtc *crtc = to_intel_crtc(dig_port->base.base.crtc);
3475        enum pipe pipe = crtc->pipe;
3476        u32 trans_ddi_func_ctl_value, trans_conf_value, dp_tp_ctl_value;
3477
3478        trans_ddi_func_ctl_value = intel_de_read(dev_priv,
3479                                                 TRANS_DDI_FUNC_CTL(pipe));
3480        trans_conf_value = intel_de_read(dev_priv, PIPECONF(pipe));
3481        dp_tp_ctl_value = intel_de_read(dev_priv, TGL_DP_TP_CTL(pipe));
3482
3483        trans_ddi_func_ctl_value &= ~(TRANS_DDI_FUNC_ENABLE |
3484                                      TGL_TRANS_DDI_PORT_MASK);
3485        trans_conf_value &= ~PIPECONF_ENABLE;
3486        dp_tp_ctl_value &= ~DP_TP_CTL_ENABLE;
3487
3488        intel_de_write(dev_priv, PIPECONF(pipe), trans_conf_value);
3489        intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(pipe),
3490                       trans_ddi_func_ctl_value);
3491        intel_de_write(dev_priv, TGL_DP_TP_CTL(pipe), dp_tp_ctl_value);
3492}
3493
3494static void
3495intel_dp_autotest_phy_ddi_enable(struct intel_dp *intel_dp,
3496                                 const struct intel_crtc_state *crtc_state)
3497{
3498        struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
3499        struct drm_device *dev = dig_port->base.base.dev;
3500        struct drm_i915_private *dev_priv = to_i915(dev);
3501        enum port port = dig_port->base.port;
3502        struct intel_crtc *crtc = to_intel_crtc(dig_port->base.base.crtc);
3503        enum pipe pipe = crtc->pipe;
3504        u32 trans_ddi_func_ctl_value, trans_conf_value, dp_tp_ctl_value;
3505
3506        trans_ddi_func_ctl_value = intel_de_read(dev_priv,
3507                                                 TRANS_DDI_FUNC_CTL(pipe));
3508        trans_conf_value = intel_de_read(dev_priv, PIPECONF(pipe));
3509        dp_tp_ctl_value = intel_de_read(dev_priv, TGL_DP_TP_CTL(pipe));
3510
3511        trans_ddi_func_ctl_value |= TRANS_DDI_FUNC_ENABLE |
3512                                    TGL_TRANS_DDI_SELECT_PORT(port);
3513        trans_conf_value |= PIPECONF_ENABLE;
3514        dp_tp_ctl_value |= DP_TP_CTL_ENABLE;
3515
3516        intel_de_write(dev_priv, PIPECONF(pipe), trans_conf_value);
3517        intel_de_write(dev_priv, TGL_DP_TP_CTL(pipe), dp_tp_ctl_value);
3518        intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(pipe),
3519                       trans_ddi_func_ctl_value);
3520}
3521
3522static void intel_dp_process_phy_request(struct intel_dp *intel_dp,
3523                                         const struct intel_crtc_state *crtc_state)
3524{
3525        struct drm_dp_phy_test_params *data =
3526                &intel_dp->compliance.test_data.phytest;
3527        u8 link_status[DP_LINK_STATUS_SIZE];
3528
3529        if (drm_dp_dpcd_read_phy_link_status(&intel_dp->aux, DP_PHY_DPRX,
3530                                             link_status) < 0) {
3531                DRM_DEBUG_KMS("failed to get link status\n");
3532                return;
3533        }
3534
3535        /* retrieve vswing & pre-emphasis setting */
3536        intel_dp_get_adjust_train(intel_dp, crtc_state, DP_PHY_DPRX,
3537                                  link_status);
3538
3539        intel_dp_autotest_phy_ddi_disable(intel_dp, crtc_state);
3540
3541        intel_dp_set_signal_levels(intel_dp, crtc_state, DP_PHY_DPRX);
3542
3543        intel_dp_phy_pattern_update(intel_dp, crtc_state);
3544
3545        intel_dp_autotest_phy_ddi_enable(intel_dp, crtc_state);
3546
3547        drm_dp_dpcd_write(&intel_dp->aux, DP_TRAINING_LANE0_SET,
3548                          intel_dp->train_set, crtc_state->lane_count);
3549
3550        drm_dp_set_phy_test_pattern(&intel_dp->aux, data,
3551                                    link_status[DP_DPCD_REV]);
3552}
3553
3554static u8 intel_dp_autotest_phy_pattern(struct intel_dp *intel_dp)
3555{
3556        struct drm_dp_phy_test_params *data =
3557                &intel_dp->compliance.test_data.phytest;
3558
3559        if (drm_dp_get_phy_test_pattern(&intel_dp->aux, data)) {
3560                DRM_DEBUG_KMS("DP Phy Test pattern AUX read failure\n");
3561                return DP_TEST_NAK;
3562        }
3563
3564        /* Set test active flag here so userspace doesn't interrupt things */
3565        intel_dp->compliance.test_active = true;
3566
3567        return DP_TEST_ACK;
3568}
3569
3570static void intel_dp_handle_test_request(struct intel_dp *intel_dp)
3571{
3572        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3573        u8 response = DP_TEST_NAK;
3574        u8 request = 0;
3575        int status;
3576
3577        status = drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_REQUEST, &request);
3578        if (status <= 0) {
3579                drm_dbg_kms(&i915->drm,
3580                            "Could not read test request from sink\n");
3581                goto update_status;
3582        }
3583
3584        switch (request) {
3585        case DP_TEST_LINK_TRAINING:
3586                drm_dbg_kms(&i915->drm, "LINK_TRAINING test requested\n");
3587                response = intel_dp_autotest_link_training(intel_dp);
3588                break;
3589        case DP_TEST_LINK_VIDEO_PATTERN:
3590                drm_dbg_kms(&i915->drm, "TEST_PATTERN test requested\n");
3591                response = intel_dp_autotest_video_pattern(intel_dp);
3592                break;
3593        case DP_TEST_LINK_EDID_READ:
3594                drm_dbg_kms(&i915->drm, "EDID test requested\n");
3595                response = intel_dp_autotest_edid(intel_dp);
3596                break;
3597        case DP_TEST_LINK_PHY_TEST_PATTERN:
3598                drm_dbg_kms(&i915->drm, "PHY_PATTERN test requested\n");
3599                response = intel_dp_autotest_phy_pattern(intel_dp);
3600                break;
3601        default:
3602                drm_dbg_kms(&i915->drm, "Invalid test request '%02x'\n",
3603                            request);
3604                break;
3605        }
3606
3607        if (response & DP_TEST_ACK)
3608                intel_dp->compliance.test_type = request;
3609
3610update_status:
3611        status = drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_RESPONSE, response);
3612        if (status <= 0)
3613                drm_dbg_kms(&i915->drm,
3614                            "Could not write test response to sink\n");
3615}
3616
3617static void
3618intel_dp_mst_hpd_irq(struct intel_dp *intel_dp, u8 *esi, bool *handled)
3619{
3620                drm_dp_mst_hpd_irq(&intel_dp->mst_mgr, esi, handled);
3621
3622                if (esi[1] & DP_CP_IRQ) {
3623                        intel_hdcp_handle_cp_irq(intel_dp->attached_connector);
3624                        *handled = true;
3625                }
3626}
3627
3628/**
3629 * intel_dp_check_mst_status - service any pending MST interrupts, check link status
3630 * @intel_dp: Intel DP struct
3631 *
3632 * Read any pending MST interrupts, call MST core to handle these and ack the
3633 * interrupts. Check if the main and AUX link state is ok.
3634 *
3635 * Returns:
3636 * - %true if pending interrupts were serviced (or no interrupts were
3637 *   pending) w/o detecting an error condition.
3638 * - %false if an error condition - like AUX failure or a loss of link - is
3639 *   detected, which needs servicing from the hotplug work.
3640 */
3641static bool
3642intel_dp_check_mst_status(struct intel_dp *intel_dp)
3643{
3644        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3645        bool link_ok = true;
3646
3647        drm_WARN_ON_ONCE(&i915->drm, intel_dp->active_mst_links < 0);
3648
3649        for (;;) {
3650                /*
3651                 * The +2 is because DP_DPRX_ESI_LEN is 14, but we then
3652                 * pass in "esi+10" to drm_dp_channel_eq_ok(), which
3653                 * takes a 6-byte array. So we actually need 16 bytes
3654                 * here.
3655                 *
3656                 * Somebody who knows what the limits actually are
3657                 * should check this, but for now this is at least
3658                 * harmless and avoids a valid compiler warning about
3659                 * using more of the array than we have allocated.
3660                 */
3661                u8 esi[DP_DPRX_ESI_LEN+2] = {};
3662                bool handled;
3663                int retry;
3664
3665                if (!intel_dp_get_sink_irq_esi(intel_dp, esi)) {
3666                        drm_dbg_kms(&i915->drm,
3667                                    "failed to get ESI - device may have failed\n");
3668                        link_ok = false;
3669
3670                        break;
3671                }
3672
3673                /* check link status - esi[10] = 0x200c */
3674                if (intel_dp->active_mst_links > 0 && link_ok &&
3675                    !drm_dp_channel_eq_ok(&esi[10], intel_dp->lane_count)) {
3676                        drm_dbg_kms(&i915->drm,
3677                                    "channel EQ not ok, retraining\n");
3678                        link_ok = false;
3679                }
3680
3681                drm_dbg_kms(&i915->drm, "got esi %3ph\n", esi);
3682
3683                intel_dp_mst_hpd_irq(intel_dp, esi, &handled);
3684
3685                if (!handled)
3686                        break;
3687
3688                for (retry = 0; retry < 3; retry++) {
3689                        int wret;
3690
3691                        wret = drm_dp_dpcd_write(&intel_dp->aux,
3692                                                 DP_SINK_COUNT_ESI+1,
3693                                                 &esi[1], 3);
3694                        if (wret == 3)
3695                                break;
3696                }
3697        }
3698
3699        return link_ok;
3700}
3701
3702static void
3703intel_dp_handle_hdmi_link_status_change(struct intel_dp *intel_dp)
3704{
3705        bool is_active;
3706        u8 buf = 0;
3707
3708        is_active = drm_dp_pcon_hdmi_link_active(&intel_dp->aux);
3709        if (intel_dp->frl.is_trained && !is_active) {
3710                if (drm_dp_dpcd_readb(&intel_dp->aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf) < 0)
3711                        return;
3712
3713                buf &=  ~DP_PCON_ENABLE_HDMI_LINK;
3714                if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_PCON_HDMI_LINK_CONFIG_1, buf) < 0)
3715                        return;
3716
3717                drm_dp_pcon_hdmi_frl_link_error_count(&intel_dp->aux, &intel_dp->attached_connector->base);
3718
3719                /* Restart FRL training or fall back to TMDS mode */
3720                intel_dp_check_frl_training(intel_dp);
3721        }
3722}
3723
3724static bool
3725intel_dp_needs_link_retrain(struct intel_dp *intel_dp)
3726{
3727        u8 link_status[DP_LINK_STATUS_SIZE];
3728
3729        if (!intel_dp->link_trained)
3730                return false;
3731
3732        /*
3733         * While PSR source HW is enabled, it will control main-link sending
3734         * frames, enabling and disabling it so trying to do a retrain will fail
3735         * as the link would or not be on or it could mix training patterns
3736         * and frame data at the same time causing retrain to fail.
3737         * Also when exiting PSR, HW will retrain the link anyways fixing
3738         * any link status error.
3739         */
3740        if (intel_psr_enabled(intel_dp))
3741                return false;
3742
3743        if (drm_dp_dpcd_read_phy_link_status(&intel_dp->aux, DP_PHY_DPRX,
3744                                             link_status) < 0)
3745                return false;
3746
3747        /*
3748         * Validate the cached values of intel_dp->link_rate and
3749         * intel_dp->lane_count before attempting to retrain.
3750         *
3751         * FIXME would be nice to user the crtc state here, but since
3752         * we need to call this from the short HPD handler that seems
3753         * a bit hard.
3754         */
3755        if (!intel_dp_link_params_valid(intel_dp, intel_dp->link_rate,
3756                                        intel_dp->lane_count))
3757                return false;
3758
3759        /* Retrain if Channel EQ or CR not ok */
3760        return !drm_dp_channel_eq_ok(link_status, intel_dp->lane_count);
3761}
3762
3763static bool intel_dp_has_connector(struct intel_dp *intel_dp,
3764                                   const struct drm_connector_state *conn_state)
3765{
3766        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3767        struct intel_encoder *encoder;
3768        enum pipe pipe;
3769
3770        if (!conn_state->best_encoder)
3771                return false;
3772
3773        /* SST */
3774        encoder = &dp_to_dig_port(intel_dp)->base;
3775        if (conn_state->best_encoder == &encoder->base)
3776                return true;
3777
3778        /* MST */
3779        for_each_pipe(i915, pipe) {
3780                encoder = &intel_dp->mst_encoders[pipe]->base;
3781                if (conn_state->best_encoder == &encoder->base)
3782                        return true;
3783        }
3784
3785        return false;
3786}
3787
3788static int intel_dp_prep_link_retrain(struct intel_dp *intel_dp,
3789                                      struct drm_modeset_acquire_ctx *ctx,
3790                                      u32 *crtc_mask)
3791{
3792        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3793        struct drm_connector_list_iter conn_iter;
3794        struct intel_connector *connector;
3795        int ret = 0;
3796
3797        *crtc_mask = 0;
3798
3799        if (!intel_dp_needs_link_retrain(intel_dp))
3800                return 0;
3801
3802        drm_connector_list_iter_begin(&i915->drm, &conn_iter);
3803        for_each_intel_connector_iter(connector, &conn_iter) {
3804                struct drm_connector_state *conn_state =
3805                        connector->base.state;
3806                struct intel_crtc_state *crtc_state;
3807                struct intel_crtc *crtc;
3808
3809                if (!intel_dp_has_connector(intel_dp, conn_state))
3810                        continue;
3811
3812                crtc = to_intel_crtc(conn_state->crtc);
3813                if (!crtc)
3814                        continue;
3815
3816                ret = drm_modeset_lock(&crtc->base.mutex, ctx);
3817                if (ret)
3818                        break;
3819
3820                crtc_state = to_intel_crtc_state(crtc->base.state);
3821
3822                drm_WARN_ON(&i915->drm, !intel_crtc_has_dp_encoder(crtc_state));
3823
3824                if (!crtc_state->hw.active)
3825                        continue;
3826
3827                if (conn_state->commit &&
3828                    !try_wait_for_completion(&conn_state->commit->hw_done))
3829                        continue;
3830
3831                *crtc_mask |= drm_crtc_mask(&crtc->base);
3832        }
3833        drm_connector_list_iter_end(&conn_iter);
3834
3835        if (!intel_dp_needs_link_retrain(intel_dp))
3836                *crtc_mask = 0;
3837
3838        return ret;
3839}
3840
3841static bool intel_dp_is_connected(struct intel_dp *intel_dp)
3842{
3843        struct intel_connector *connector = intel_dp->attached_connector;
3844
3845        return connector->base.status == connector_status_connected ||
3846                intel_dp->is_mst;
3847}
3848
3849int intel_dp_retrain_link(struct intel_encoder *encoder,
3850                          struct drm_modeset_acquire_ctx *ctx)
3851{
3852        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3853        struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3854        struct intel_crtc *crtc;
3855        u32 crtc_mask;
3856        int ret;
3857
3858        if (!intel_dp_is_connected(intel_dp))
3859                return 0;
3860
3861        ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
3862                               ctx);
3863        if (ret)
3864                return ret;
3865
3866        ret = intel_dp_prep_link_retrain(intel_dp, ctx, &crtc_mask);
3867        if (ret)
3868                return ret;
3869
3870        if (crtc_mask == 0)
3871                return 0;
3872
3873        drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s] retraining link\n",
3874                    encoder->base.base.id, encoder->base.name);
3875
3876        for_each_intel_crtc_mask(&dev_priv->drm, crtc, crtc_mask) {
3877                const struct intel_crtc_state *crtc_state =
3878                        to_intel_crtc_state(crtc->base.state);
3879
3880                /* Suppress underruns caused by re-training */
3881                intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
3882                if (crtc_state->has_pch_encoder)
3883                        intel_set_pch_fifo_underrun_reporting(dev_priv,
3884                                                              intel_crtc_pch_transcoder(crtc), false);
3885        }
3886
3887        for_each_intel_crtc_mask(&dev_priv->drm, crtc, crtc_mask) {
3888                const struct intel_crtc_state *crtc_state =
3889                        to_intel_crtc_state(crtc->base.state);
3890
3891                /* retrain on the MST master transcoder */
3892                if (DISPLAY_VER(dev_priv) >= 12 &&
3893                    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST) &&
3894                    !intel_dp_mst_is_master_trans(crtc_state))
3895                        continue;
3896
3897                intel_dp_check_frl_training(intel_dp);
3898                intel_dp_pcon_dsc_configure(intel_dp, crtc_state);
3899                intel_dp_start_link_train(intel_dp, crtc_state);
3900                intel_dp_stop_link_train(intel_dp, crtc_state);
3901                break;
3902        }
3903
3904        for_each_intel_crtc_mask(&dev_priv->drm, crtc, crtc_mask) {
3905                const struct intel_crtc_state *crtc_state =
3906                        to_intel_crtc_state(crtc->base.state);
3907
3908                /* Keep underrun reporting disabled until things are stable */
3909                intel_crtc_wait_for_next_vblank(crtc);
3910
3911                intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
3912                if (crtc_state->has_pch_encoder)
3913                        intel_set_pch_fifo_underrun_reporting(dev_priv,
3914                                                              intel_crtc_pch_transcoder(crtc), true);
3915        }
3916
3917        return 0;
3918}
3919
3920static int intel_dp_prep_phy_test(struct intel_dp *intel_dp,
3921                                  struct drm_modeset_acquire_ctx *ctx,
3922                                  u32 *crtc_mask)
3923{
3924        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
3925        struct drm_connector_list_iter conn_iter;
3926        struct intel_connector *connector;
3927        int ret = 0;
3928
3929        *crtc_mask = 0;
3930
3931        drm_connector_list_iter_begin(&i915->drm, &conn_iter);
3932        for_each_intel_connector_iter(connector, &conn_iter) {
3933                struct drm_connector_state *conn_state =
3934                        connector->base.state;
3935                struct intel_crtc_state *crtc_state;
3936                struct intel_crtc *crtc;
3937
3938                if (!intel_dp_has_connector(intel_dp, conn_state))
3939                        continue;
3940
3941                crtc = to_intel_crtc(conn_state->crtc);
3942                if (!crtc)
3943                        continue;
3944
3945                ret = drm_modeset_lock(&crtc->base.mutex, ctx);
3946                if (ret)
3947                        break;
3948
3949                crtc_state = to_intel_crtc_state(crtc->base.state);
3950
3951                drm_WARN_ON(&i915->drm, !intel_crtc_has_dp_encoder(crtc_state));
3952
3953                if (!crtc_state->hw.active)
3954                        continue;
3955
3956                if (conn_state->commit &&
3957                    !try_wait_for_completion(&conn_state->commit->hw_done))
3958                        continue;
3959
3960                *crtc_mask |= drm_crtc_mask(&crtc->base);
3961        }
3962        drm_connector_list_iter_end(&conn_iter);
3963
3964        return ret;
3965}
3966
3967static int intel_dp_do_phy_test(struct intel_encoder *encoder,
3968                                struct drm_modeset_acquire_ctx *ctx)
3969{
3970        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3971        struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3972        struct intel_crtc *crtc;
3973        u32 crtc_mask;
3974        int ret;
3975
3976        ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
3977                               ctx);
3978        if (ret)
3979                return ret;
3980
3981        ret = intel_dp_prep_phy_test(intel_dp, ctx, &crtc_mask);
3982        if (ret)
3983                return ret;
3984
3985        if (crtc_mask == 0)
3986                return 0;
3987
3988        drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s] PHY test\n",
3989                    encoder->base.base.id, encoder->base.name);
3990
3991        for_each_intel_crtc_mask(&dev_priv->drm, crtc, crtc_mask) {
3992                const struct intel_crtc_state *crtc_state =
3993                        to_intel_crtc_state(crtc->base.state);
3994
3995                /* test on the MST master transcoder */
3996                if (DISPLAY_VER(dev_priv) >= 12 &&
3997                    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST) &&
3998                    !intel_dp_mst_is_master_trans(crtc_state))
3999                        continue;
4000
4001                intel_dp_process_phy_request(intel_dp, crtc_state);
4002                break;
4003        }
4004
4005        return 0;
4006}
4007
4008void intel_dp_phy_test(struct intel_encoder *encoder)
4009{
4010        struct drm_modeset_acquire_ctx ctx;
4011        int ret;
4012
4013        drm_modeset_acquire_init(&ctx, 0);
4014
4015        for (;;) {
4016                ret = intel_dp_do_phy_test(encoder, &ctx);
4017
4018                if (ret == -EDEADLK) {
4019                        drm_modeset_backoff(&ctx);
4020                        continue;
4021                }
4022
4023                break;
4024        }
4025
4026        drm_modeset_drop_locks(&ctx);
4027        drm_modeset_acquire_fini(&ctx);
4028        drm_WARN(encoder->base.dev, ret,
4029                 "Acquiring modeset locks failed with %i\n", ret);
4030}
4031
4032static void intel_dp_check_device_service_irq(struct intel_dp *intel_dp)
4033{
4034        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4035        u8 val;
4036
4037        if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
4038                return;
4039
4040        if (drm_dp_dpcd_readb(&intel_dp->aux,
4041                              DP_DEVICE_SERVICE_IRQ_VECTOR, &val) != 1 || !val)
4042                return;
4043
4044        drm_dp_dpcd_writeb(&intel_dp->aux, DP_DEVICE_SERVICE_IRQ_VECTOR, val);
4045
4046        if (val & DP_AUTOMATED_TEST_REQUEST)
4047                intel_dp_handle_test_request(intel_dp);
4048
4049        if (val & DP_CP_IRQ)
4050                intel_hdcp_handle_cp_irq(intel_dp->attached_connector);
4051
4052        if (val & DP_SINK_SPECIFIC_IRQ)
4053                drm_dbg_kms(&i915->drm, "Sink specific irq unhandled\n");
4054}
4055
4056static void intel_dp_check_link_service_irq(struct intel_dp *intel_dp)
4057{
4058        u8 val;
4059
4060        if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
4061                return;
4062
4063        if (drm_dp_dpcd_readb(&intel_dp->aux,
4064                              DP_LINK_SERVICE_IRQ_VECTOR_ESI0, &val) != 1 || !val)
4065                return;
4066
4067        if (drm_dp_dpcd_writeb(&intel_dp->aux,
4068                               DP_LINK_SERVICE_IRQ_VECTOR_ESI0, val) != 1)
4069                return;
4070
4071        if (val & HDMI_LINK_STATUS_CHANGED)
4072                intel_dp_handle_hdmi_link_status_change(intel_dp);
4073}
4074
4075/*
4076 * According to DP spec
4077 * 5.1.2:
4078 *  1. Read DPCD
4079 *  2. Configure link according to Receiver Capabilities
4080 *  3. Use Link Training from 2.5.3.3 and 3.5.1.3
4081 *  4. Check link status on receipt of hot-plug interrupt
4082 *
4083 * intel_dp_short_pulse -  handles short pulse interrupts
4084 * when full detection is not required.
4085 * Returns %true if short pulse is handled and full detection
4086 * is NOT required and %false otherwise.
4087 */
4088static bool
4089intel_dp_short_pulse(struct intel_dp *intel_dp)
4090{
4091        struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
4092        u8 old_sink_count = intel_dp->sink_count;
4093        bool ret;
4094
4095        /*
4096         * Clearing compliance test variables to allow capturing
4097         * of values for next automated test request.
4098         */
4099        memset(&intel_dp->compliance, 0, sizeof(intel_dp->compliance));
4100
4101        /*
4102         * Now read the DPCD to see if it's actually running
4103         * If the current value of sink count doesn't match with
4104         * the value that was stored earlier or dpcd read failed
4105         * we need to do full detection
4106         */
4107        ret = intel_dp_get_dpcd(intel_dp);
4108
4109        if ((old_sink_count != intel_dp->sink_count) || !ret) {
4110                /* No need to proceed if we are going to do full detect */
4111                return false;
4112        }
4113
4114        intel_dp_check_device_service_irq(intel_dp);
4115        intel_dp_check_link_service_irq(intel_dp);
4116
4117        /* Handle CEC interrupts, if any */
4118        drm_dp_cec_irq(&intel_dp->aux);
4119
4120        /* defer to the hotplug work for link retraining if needed */
4121        if (intel_dp_needs_link_retrain(intel_dp))
4122                return false;
4123
4124        intel_psr_short_pulse(intel_dp);
4125
4126        switch (intel_dp->compliance.test_type) {
4127        case DP_TEST_LINK_TRAINING:
4128                drm_dbg_kms(&dev_priv->drm,
4129                            "Link Training Compliance Test requested\n");
4130                /* Send a Hotplug Uevent to userspace to start modeset */
4131                drm_kms_helper_hotplug_event(&dev_priv->drm);
4132                break;
4133        case DP_TEST_LINK_PHY_TEST_PATTERN:
4134                drm_dbg_kms(&dev_priv->drm,
4135                            "PHY test pattern Compliance Test requested\n");
4136                /*
4137                 * Schedule long hpd to do the test
4138                 *
4139                 * FIXME get rid of the ad-hoc phy test modeset code
4140                 * and properly incorporate it into the normal modeset.
4141                 */
4142                return false;
4143        }
4144
4145        return true;
4146}
4147
4148/* XXX this is probably wrong for multiple downstream ports */
4149static enum drm_connector_status
4150intel_dp_detect_dpcd(struct intel_dp *intel_dp)
4151{
4152        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4153        struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4154        u8 *dpcd = intel_dp->dpcd;
4155        u8 type;
4156
4157        if (drm_WARN_ON(&i915->drm, intel_dp_is_edp(intel_dp)))
4158                return connector_status_connected;
4159
4160        lspcon_resume(dig_port);
4161
4162        if (!intel_dp_get_dpcd(intel_dp))
4163                return connector_status_disconnected;
4164
4165        /* if there's no downstream port, we're done */
4166        if (!drm_dp_is_branch(dpcd))
4167                return connector_status_connected;
4168
4169        /* If we're HPD-aware, SINK_COUNT changes dynamically */
4170        if (intel_dp_has_sink_count(intel_dp) &&
4171            intel_dp->downstream_ports[0] & DP_DS_PORT_HPD) {
4172                return intel_dp->sink_count ?
4173                connector_status_connected : connector_status_disconnected;
4174        }
4175
4176        if (intel_dp_can_mst(intel_dp))
4177                return connector_status_connected;
4178
4179        /* If no HPD, poke DDC gently */
4180        if (drm_probe_ddc(&intel_dp->aux.ddc))
4181                return connector_status_connected;
4182
4183        /* Well we tried, say unknown for unreliable port types */
4184        if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11) {
4185                type = intel_dp->downstream_ports[0] & DP_DS_PORT_TYPE_MASK;
4186                if (type == DP_DS_PORT_TYPE_VGA ||
4187                    type == DP_DS_PORT_TYPE_NON_EDID)
4188                        return connector_status_unknown;
4189        } else {
4190                type = intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
4191                        DP_DWN_STRM_PORT_TYPE_MASK;
4192                if (type == DP_DWN_STRM_PORT_TYPE_ANALOG ||
4193                    type == DP_DWN_STRM_PORT_TYPE_OTHER)
4194                        return connector_status_unknown;
4195        }
4196
4197        /* Anything else is out of spec, warn and ignore */
4198        drm_dbg_kms(&i915->drm, "Broken DP branch device, ignoring\n");
4199        return connector_status_disconnected;
4200}
4201
4202static enum drm_connector_status
4203edp_detect(struct intel_dp *intel_dp)
4204{
4205        return connector_status_connected;
4206}
4207
4208/*
4209 * intel_digital_port_connected - is the specified port connected?
4210 * @encoder: intel_encoder
4211 *
4212 * In cases where there's a connector physically connected but it can't be used
4213 * by our hardware we also return false, since the rest of the driver should
4214 * pretty much treat the port as disconnected. This is relevant for type-C
4215 * (starting on ICL) where there's ownership involved.
4216 *
4217 * Return %true if port is connected, %false otherwise.
4218 */
4219bool intel_digital_port_connected(struct intel_encoder *encoder)
4220{
4221        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4222        struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
4223        bool is_connected = false;
4224        intel_wakeref_t wakeref;
4225
4226        with_intel_display_power(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref)
4227                is_connected = dig_port->connected(encoder);
4228
4229        return is_connected;
4230}
4231
4232static struct edid *
4233intel_dp_get_edid(struct intel_dp *intel_dp)
4234{
4235        struct intel_connector *intel_connector = intel_dp->attached_connector;
4236
4237        /* use cached edid if we have one */
4238        if (intel_connector->edid) {
4239                /* invalid edid */
4240                if (IS_ERR(intel_connector->edid))
4241                        return NULL;
4242
4243                return drm_edid_duplicate(intel_connector->edid);
4244        } else
4245                return drm_get_edid(&intel_connector->base,
4246                                    &intel_dp->aux.ddc);
4247}
4248
4249static void
4250intel_dp_update_dfp(struct intel_dp *intel_dp,
4251                    const struct edid *edid)
4252{
4253        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4254        struct intel_connector *connector = intel_dp->attached_connector;
4255
4256        intel_dp->dfp.max_bpc =
4257                drm_dp_downstream_max_bpc(intel_dp->dpcd,
4258                                          intel_dp->downstream_ports, edid);
4259
4260        intel_dp->dfp.max_dotclock =
4261                drm_dp_downstream_max_dotclock(intel_dp->dpcd,
4262                                               intel_dp->downstream_ports);
4263
4264        intel_dp->dfp.min_tmds_clock =
4265                drm_dp_downstream_min_tmds_clock(intel_dp->dpcd,
4266                                                 intel_dp->downstream_ports,
4267                                                 edid);
4268        intel_dp->dfp.max_tmds_clock =
4269                drm_dp_downstream_max_tmds_clock(intel_dp->dpcd,
4270                                                 intel_dp->downstream_ports,
4271                                                 edid);
4272
4273        intel_dp->dfp.pcon_max_frl_bw =
4274                drm_dp_get_pcon_max_frl_bw(intel_dp->dpcd,
4275                                           intel_dp->downstream_ports);
4276
4277        drm_dbg_kms(&i915->drm,
4278                    "[CONNECTOR:%d:%s] DFP max bpc %d, max dotclock %d, TMDS clock %d-%d, PCON Max FRL BW %dGbps\n",
4279                    connector->base.base.id, connector->base.name,
4280                    intel_dp->dfp.max_bpc,
4281                    intel_dp->dfp.max_dotclock,
4282                    intel_dp->dfp.min_tmds_clock,
4283                    intel_dp->dfp.max_tmds_clock,
4284                    intel_dp->dfp.pcon_max_frl_bw);
4285
4286        intel_dp_get_pcon_dsc_cap(intel_dp);
4287}
4288
4289static void
4290intel_dp_update_420(struct intel_dp *intel_dp)
4291{
4292        struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4293        struct intel_connector *connector = intel_dp->attached_connector;
4294        bool is_branch, ycbcr_420_passthrough, ycbcr_444_to_420, rgb_to_ycbcr;
4295
4296        /* No YCbCr output support on gmch platforms */
4297        if (HAS_GMCH(i915))
4298                return;
4299
4300        /*
4301         * ILK doesn't seem capable of DP YCbCr output. The
4302         * displayed image is severly corrupted. SNB+ is fine.
4303         */
4304        if (IS_IRONLAKE(i915))
4305                return;
4306
4307        is_branch = drm_dp_is_branch(intel_dp->dpcd);
4308        ycbcr_420_passthrough =
4309                drm_dp_downstream_420_passthrough(intel_dp->dpcd,
4310                                                  intel_dp->downstream_ports);
4311        /* on-board LSPCON always assumed to support 4:4:4->4:2:0 conversion */
4312        ycbcr_444_to_420 =
4313                dp_to_dig_port(intel_dp)->lspcon.active ||
4314                drm_dp_downstream_444_to_420_conversion(intel_dp->dpcd,
4315                                                        intel_dp->downstream_ports);
4316        rgb_to_ycbcr = drm_dp_downstream_rgb_to_ycbcr_conversion(intel_dp->dpcd,
4317                                                                 intel_dp->downstream_ports,
4318                                                                 DP_DS_HDMI_BT601_RGB_YCBCR_CONV |
4319                                                                 DP_DS_HDMI_BT709_RGB_YCBCR_CONV |
4320                                                                 DP_DS_HDMI_BT2020_RGB_YCBCR_CONV);
4321
4322        if (DISPLAY_VER(i915) >= 11) {
4323                /* Let PCON convert from RGB->YCbCr if possible */
4324                if (is_branch && rgb_to_ycbcr && ycbcr_444_to_420) {
4325                        intel_dp->dfp.rgb_to_ycbcr = true;
4326                        intel_dp->dfp.ycbcr_444_to_420 = true;
4327                        connector->base.ycbcr_420_allowed = true;
4328                } else {
4329                /* Prefer 4:2:0 passthrough over 4:4:4->4:2:0 conversion */
4330                        intel_dp->dfp.ycbcr_444_to_420 =
4331                                ycbcr_444_to_420 && !ycbcr_420_passthrough;
4332
4333                        connector->base.ycbcr_420_allowed =
4334                                !is_branch || ycbcr_444_to_420 || ycbcr_420_passthrough;
4335                }
4336        } else {
4337                /* 4:4:4->4:2:0 conversion is the only way */
4338                intel_dp->dfp.ycbcr_444_to_420 = ycbcr_444_to_420;
4339
4340                connector->base.ycbcr_420_allowed = ycbcr_444_to_420;
4341        }
4342
4343        drm_dbg_kms(&i915->drm,
4344                    "[CONNECTOR:%d:%s] RGB->YcbCr conversion? %s, YCbCr 4:2:0 allowed? %s, YCbCr 4:4:4->4:2:0 conversion? %s\n",
4345                    connector->base.base.id, connector->base.name,
4346                    yesno(intel_dp->dfp.rgb_to_ycbcr),
4347                    yesno(connector->base.ycbcr_420_allowed),
4348                    yesno(intel_dp->dfp.ycbcr_444_to_420));
4349}
4350
4351static void
4352intel_dp_set_edid(struct intel_dp *intel_dp)
4353{
4354        struct intel_connector *connector = intel_dp->attached_connector;
4355        struct edid *edid;
4356
4357        intel_dp_unset_edid(intel_dp);
4358        edid = intel_dp_get_edid(intel_dp);
4359        connector->detect_edid = edid;
4360
4361        intel_dp_update_dfp(intel_dp, edid);
4362        intel_dp_update_420(intel_dp);
4363
4364        if (edid && edid->input & DRM_EDID_INPUT_DIGITAL) {
4365                intel_dp->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
4366                intel_dp->has_audio = drm_detect_monitor_audio(edid);
4367        }
4368
4369        drm_dp_cec_set_edid(&intel_dp->aux, edid);
4370}
4371
4372static void
4373intel_dp_unset_edid(struct intel_dp *intel_dp)
4374{
4375        struct intel_connector *connector = intel_dp->attached_connector;
4376
4377        drm_dp_cec_unset_edid(&intel_dp->aux);
4378        kfree(connector->detect_edid);
4379        connector->detect_edid = NULL;
4380
4381        intel_dp->has_hdmi_sink = false;
4382        intel_dp->has_audio = false;
4383
4384        intel_dp->dfp.max_bpc = 0;
4385        intel_dp->dfp.max_dotclock = 0;
4386        intel_dp->dfp.min_tmds_clock = 0;
4387        intel_dp->dfp.max_tmds_clock = 0;
4388
4389        intel_dp->dfp.pcon_max_frl_bw = 0;
4390
4391        intel_dp->dfp.ycbcr_444_to_420 = false;
4392        connector->base.ycbcr_420_allowed = false;
4393}
4394
4395static int
4396intel_dp_detect(struct drm_connector *connector,
4397                struct drm_modeset_acquire_ctx *ctx,
4398                bool force)
4399{
4400        struct drm_i915_private *dev_priv = to_i915(connector->dev);
4401        struct intel_dp *intel_dp = intel_attached_dp(to_intel_connector(connector));
4402        struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4403        struct intel_encoder *encoder = &dig_port->base;
4404        enum drm_connector_status status;
4405
4406        drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s]\n",
4407                    connector->base.id, connector->name);
4408        drm_WARN_ON(&dev_priv->drm,
4409                    !drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex));
4410
4411        if (!INTEL_DISPLAY_ENABLED(dev_priv))
4412                return connector_status_disconnected;
4413
4414        /* Can't disconnect eDP */
4415        if (intel_dp_is_edp(intel_dp))
4416                status = edp_detect(intel_dp);
4417        else if (intel_digital_port_connected(encoder))
4418                status = intel_dp_detect_dpcd(intel_dp);
4419        else
4420                status = connector_status_disconnected;
4421
4422        if (status == connector_status_disconnected) {
4423                memset(&intel_dp->compliance, 0, sizeof(intel_dp->compliance));
4424                memset(intel_dp->dsc_dpcd, 0, sizeof(intel_dp->dsc_dpcd));
4425
4426                if (intel_dp->is_mst) {
4427                        drm_dbg_kms(&dev_priv->drm,
4428                                    "MST device may have disappeared %d vs %d\n",
4429                                    intel_dp->is_mst,
4430                                    intel_dp->mst_mgr.mst_state);
4431                        intel_dp->is_mst = false;
4432                        drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr,
4433                                                        intel_dp->is_mst);
4434                }
4435
4436                goto out;
4437        }
4438
4439        /* Read DP Sink DSC Cap DPCD regs for DP v1.4 */
4440        if (DISPLAY_VER(dev_priv) >= 11)
4441                intel_dp_get_dsc_sink_cap(intel_dp);
4442
4443        intel_dp_configure_mst(intel_dp);
4444
4445        /*
4446         * TODO: Reset link params when switching to MST mode, until MST
4447         * supports link training fallback params.
4448         */
4449        if (intel_dp->reset_link_params || intel_dp->is_mst) {
4450                intel_dp_reset_max_link_params(intel_dp);
4451                intel_dp->reset_link_params = false;
4452        }
4453
4454        intel_dp_print_rates(intel_dp);
4455
4456        if (intel_dp->is_mst) {
4457                /*
4458                 * If we are in MST mode then this connector
4459                 * won't appear connected or have anything
4460                 * with EDID on it
4461                 */
4462                status = connector_status_disconnected;
4463                goto out;
4464        }
4465
4466        /*
4467         * Some external monitors do not signal loss of link synchronization
4468         * with an IRQ_HPD, so force a link status check.
4469         */
4470        if (!intel_dp_is_edp(intel_dp)) {
4471                int ret;
4472
4473                ret = intel_dp_retrain_link(encoder, ctx);
4474                if (ret)
4475                        return ret;
4476        }
4477
4478        /*
4479         * Clearing NACK and defer counts to get their exact values
4480         * while reading EDID which are required by Compliance tests
4481         * 4.2.2.4 and 4.2.2.5
4482         */
4483        intel_dp->aux.i2c_nack_count = 0;
4484        intel_dp->aux.i2c_defer_count = 0;
4485
4486        intel_dp_set_edid(intel_dp);
4487        if (intel_dp_is_edp(intel_dp) ||
4488            to_intel_connector(connector)->detect_edid)
4489                status = connector_status_connected;
4490
4491        intel_dp_check_device_service_irq(intel_dp);
4492
4493out:
4494        if (status != connector_status_connected && !intel_dp->is_mst)
4495                intel_dp_unset_edid(intel_dp);
4496
4497        /*
4498         * Make sure the refs for power wells enabled during detect are
4499         * dropped to avoid a new detect cycle triggered by HPD polling.
4500         */
4501        intel_display_power_flush_work(dev_priv);
4502
4503        if (!intel_dp_is_edp(intel_dp))
4504                drm_dp_set_subconnector_property(connector,
4505                                                 status,
4506                                                 intel_dp->dpcd,
4507                                                 intel_dp->downstream_ports);
4508        return status;
4509}
4510
4511static void
4512intel_dp_force(struct drm_connector *connector)
4513{
4514        struct intel_dp *intel_dp = intel_attached_dp(to_intel_connector(connector));
4515        struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4516        struct intel_encoder *intel_encoder = &dig_port->base;
4517        struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
4518        enum intel_display_power_domain aux_domain =
4519                intel_aux_power_domain(dig_port);
4520        intel_wakeref_t wakeref;
4521
4522        drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s]\n",
4523                    connector->base.id, connector->name);
4524        intel_dp_unset_edid(intel_dp);
4525
4526        if (connector->status != connector_status_connected)
4527                return;
4528
4529        wakeref = intel_display_power_get(dev_priv, aux_domain);
4530
4531        intel_dp_set_edid(intel_dp);
4532
4533        intel_display_power_put(dev_priv, aux_domain, wakeref);
4534}
4535
4536static int intel_dp_get_modes(struct drm_connector *connector)
4537{
4538        struct intel_connector *intel_connector = to_intel_connector(connector);
4539        struct edid *edid;
4540        int num_modes = 0;
4541
4542        edid = intel_connector->detect_edid;
4543        if (edid) {
4544                num_modes = intel_connector_update_modes(connector, edid);
4545
4546                if (intel_vrr_is_capable(connector))
4547                        drm_connector_set_vrr_capable_property(connector,
4548                                                               true);
4549        }
4550
4551        /* Also add fixed mode, which may or may not be present in EDID */
4552        if (intel_dp_is_edp(intel_attached_dp(intel_connector)) &&
4553            intel_connector->panel.fixed_mode) {
4554                struct drm_display_mode *mode;
4555
4556                mode = drm_mode_duplicate(connector->dev,
4557                                          intel_connector->panel.fixed_mode);
4558                if (mode) {
4559                        drm_mode_probed_add(connector, mode);
4560                        num_modes++;
4561                }
4562        }
4563
4564        if (num_modes)
4565                return num_modes;
4566
4567        if (!edid) {
4568                struct intel_dp *intel_dp = intel_attached_dp(intel_connector);
4569                struct drm_display_mode *mode;
4570
4571                mode = drm_dp_downstream_mode(connector->dev,
4572                                              intel_dp->dpcd,
4573                                              intel_dp->downstream_ports);
4574                if (mode) {
4575                        drm_mode_probed_add(connector, mode);
4576                        num_modes++;
4577                }
4578        }
4579
4580        return num_modes;
4581}
4582
4583static int
4584intel_dp_connector_register(struct drm_connector *connector)
4585{
4586        struct drm_i915_private *i915 = to_i915(connector->dev);
4587        struct intel_dp *intel_dp = intel_attached_dp(to_intel_connector(connector));
4588        struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4589        struct intel_lspcon *lspcon = &dig_port->lspcon;
4590        int ret;
4591
4592        ret = intel_connector_register(connector);
4593        if (ret)
4594                return ret;
4595
4596        drm_dbg_kms(&i915->drm, "registering %s bus for %s\n",
4597                    intel_dp->aux.name, connector->kdev->kobj.name);
4598
4599        intel_dp->aux.dev = connector->kdev;
4600        ret = drm_dp_aux_register(&intel_dp->aux);
4601        if (!ret)
4602                drm_dp_cec_register_connector(&intel_dp->aux, connector);
4603
4604        if (!intel_bios_is_lspcon_present(i915, dig_port->base.port))
4605                return ret;
4606
4607        /*
4608         * ToDo: Clean this up to handle lspcon init and resume more
4609         * efficiently and streamlined.
4610         */
4611        if (lspcon_init(dig_port)) {
4612                lspcon_detect_hdr_capability(lspcon);
4613                if (lspcon->hdr_supported)
4614                        drm_object_attach_property(&connector->base,
4615                                                   connector->dev->mode_config.hdr_output_metadata_property,
4616                                                   0);
4617        }
4618
4619        return ret;
4620}
4621
4622static void
4623intel_dp_connector_unregister(struct drm_connector *connector)
4624{
4625        struct intel_dp *intel_dp = intel_attached_dp(to_intel_connector(connector));
4626
4627        drm_dp_cec_unregister_connector(&intel_dp->aux);
4628        drm_dp_aux_unregister(&intel_dp->aux);
4629        intel_connector_unregister(connector);
4630}
4631
4632void intel_dp_encoder_flush_work(struct drm_encoder *encoder)
4633{
4634        struct intel_digital_port *dig_port = enc_to_dig_port(to_intel_encoder(encoder));
4635        struct intel_dp *intel_dp = &dig_port->dp;
4636
4637        intel_dp_mst_encoder_cleanup(dig_port);
4638
4639        intel_pps_vdd_off_sync(intel_dp);
4640
4641        intel_dp_aux_fini(intel_dp);
4642}
4643
4644void intel_dp_encoder_suspend(struct intel_encoder *intel_encoder)
4645{
4646        struct intel_dp *intel_dp = enc_to_intel_dp(intel_encoder);
4647
4648        intel_pps_vdd_off_sync(intel_dp);
4649}
4650
4651void intel_dp_encoder_shutdown(struct intel_encoder *intel_encoder)
4652{
4653        struct intel_dp *intel_dp = enc_to_intel_dp(intel_encoder);
4654
4655        intel_pps_wait_power_cycle(intel_dp);
4656}
4657
4658static int intel_modeset_tile_group(struct intel_atomic_state *state,
4659                                    int tile_group_id)
4660{
4661        struct drm_i915_private *dev_priv = to_i915(state->base.dev);
4662        struct drm_connector_list_iter conn_iter;
4663        struct drm_connector *connector;
4664        int ret = 0;
4665
4666        drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
4667        drm_for_each_connector_iter(connector, &conn_iter) {
4668                struct drm_connector_state *conn_state;
4669                struct intel_crtc_state *crtc_state;
4670                struct intel_crtc *crtc;
4671
4672                if (!connector->has_tile ||
4673                    connector->tile_group->id != tile_group_id)
4674                        continue;
4675
4676                conn_state = drm_atomic_get_connector_state(&state->base,
4677                                                            connector);
4678                if (IS_ERR(conn_state)) {
4679                        ret = PTR_ERR(conn_state);
4680                        break;
4681                }
4682
4683                crtc = to_intel_crtc(conn_state->crtc);
4684
4685                if (!crtc)
4686                        continue;
4687
4688                crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
4689                crtc_state->uapi.mode_changed = true;
4690
4691                ret = drm_atomic_add_affected_planes(&state->base, &crtc->base);
4692                if (ret)
4693                        break;
4694        }
4695        drm_connector_list_iter_end(&conn_iter);
4696
4697        return ret;
4698}
4699
4700static int intel_modeset_affected_transcoders(struct intel_atomic_state *state, u8 transcoders)
4701{
4702        struct drm_i915_private *dev_priv = to_i915(state->base.dev);
4703        struct intel_crtc *crtc;
4704
4705        if (transcoders == 0)
4706                return 0;
4707
4708        for_each_intel_crtc(&dev_priv->drm, crtc) {
4709                struct intel_crtc_state *crtc_state;
4710                int ret;
4711
4712                crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
4713                if (IS_ERR(crtc_state))
4714                        return PTR_ERR(crtc_state);
4715
4716                if (!crtc_state->hw.enable)
4717                        continue;
4718
4719                if (!(transcoders & BIT(crtc_state->cpu_transcoder)))
4720                        continue;
4721
4722                crtc_state->uapi.mode_changed = true;
4723
4724                ret = drm_atomic_add_affected_connectors(&state->base, &crtc->base);
4725                if (ret)
4726                        return ret;
4727
4728                ret = drm_atomic_add_affected_planes(&state->base, &crtc->base);
4729                if (ret)
4730                        return ret;
4731
4732                transcoders &= ~BIT(crtc_state->cpu_transcoder);
4733        }
4734
4735        drm_WARN_ON(&dev_priv->drm, transcoders != 0);
4736
4737        return 0;
4738}
4739
4740static int intel_modeset_synced_crtcs(struct intel_atomic_state *state,
4741                                      struct drm_connector *connector)
4742{
4743        const struct drm_connector_state *old_conn_state =
4744                drm_atomic_get_old_connector_state(&state->base, connector);
4745        const struct intel_crtc_state *old_crtc_state;
4746        struct intel_crtc *crtc;
4747        u8 transcoders;
4748
4749        crtc = to_intel_crtc(old_conn_state->crtc);
4750        if (!crtc)
4751                return 0;
4752
4753        old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc);
4754
4755        if (!old_crtc_state->hw.active)
4756                return 0;
4757
4758        transcoders = old_crtc_state->sync_mode_slaves_mask;
4759        if (old_crtc_state->master_transcoder != INVALID_TRANSCODER)
4760                transcoders |= BIT(old_crtc_state->master_transcoder);
4761
4762        return intel_modeset_affected_transcoders(state,
4763                                                  transcoders);
4764}
4765
4766static int intel_dp_connector_atomic_check(struct drm_connector *conn,
4767                                           struct drm_atomic_state *_state)
4768{
4769        struct drm_i915_private *dev_priv = to_i915(conn->dev);
4770        struct intel_atomic_state *state = to_intel_atomic_state(_state);
4771        int ret;
4772
4773        ret = intel_digital_connector_atomic_check(conn, &state->base);
4774        if (ret)
4775                return ret;
4776
4777        /*
4778         * We don't enable port sync on BDW due to missing w/as and
4779         * due to not having adjusted the modeset sequence appropriately.
4780         */
4781        if (DISPLAY_VER(dev_priv) < 9)
4782                return 0;
4783
4784        if (!intel_connector_needs_modeset(state, conn))
4785                return 0;
4786
4787        if (conn->has_tile) {
4788                ret = intel_modeset_tile_group(state, conn->tile_group->id);
4789                if (ret)
4790                        return ret;
4791        }
4792
4793        return intel_modeset_synced_crtcs(state, conn);
4794}
4795
4796static void intel_dp_oob_hotplug_event(struct drm_connector *connector)
4797{
4798        struct intel_encoder *encoder = intel_attached_encoder(to_intel_connector(connector));
4799        struct drm_i915_private *i915 = to_i915(connector->dev);
4800
4801        spin_lock_irq(&i915->irq_lock);
4802        i915->hotplug.event_bits |= BIT(encoder->hpd_pin);
4803        spin_unlock_irq(&i915->irq_lock);
4804        queue_delayed_work(system_wq, &i915->hotplug.hotplug_work, 0);
4805}
4806
4807static const struct drm_connector_funcs intel_dp_connector_funcs = {
4808        .force = intel_dp_force,
4809        .fill_modes = drm_helper_probe_single_connector_modes,
4810        .atomic_get_property = intel_digital_connector_atomic_get_property,
4811        .atomic_set_property = intel_digital_connector_atomic_set_property,
4812        .late_register = intel_dp_connector_register,
4813        .early_unregister = intel_dp_connector_unregister,
4814        .destroy = intel_connector_destroy,
4815        .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
4816        .atomic_duplicate_state = intel_digital_connector_duplicate_state,
4817        .oob_hotplug_event = intel_dp_oob_hotplug_event,
4818};
4819
4820static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
4821        .detect_ctx = intel_dp_detect,
4822        .get_modes = intel_dp_get_modes,
4823        .mode_valid = intel_dp_mode_valid,
4824        .atomic_check = intel_dp_connector_atomic_check,
4825};
4826
4827enum irqreturn
4828intel_dp_hpd_pulse(struct intel_digital_port *dig_port, bool long_hpd)
4829{
4830        struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
4831        struct intel_dp *intel_dp = &dig_port->dp;
4832
4833        if (dig_port->base.type == INTEL_OUTPUT_EDP &&
4834            (long_hpd || !intel_pps_have_power(intel_dp))) {
4835                /*
4836                 * vdd off can generate a long/short pulse on eDP which
4837                 * would require vdd on to handle it, and thus we
4838                 * would end up in an endless cycle of
4839                 * "vdd off -> long/short hpd -> vdd on -> detect -> vdd off -> ..."
4840                 */
4841                drm_dbg_kms(&i915->drm,
4842                            "ignoring %s hpd on eDP [ENCODER:%d:%s]\n",
4843                            long_hpd ? "long" : "short",
4844                            dig_port->base.base.base.id,
4845                            dig_port->base.base.name);
4846                return IRQ_HANDLED;
4847        }
4848
4849        drm_dbg_kms(&i915->drm, "got hpd irq on [ENCODER:%d:%s] - %s\n",
4850                    dig_port->base.base.base.id,
4851                    dig_port->base.base.name,
4852                    long_hpd ? "long" : "short");
4853
4854        if (long_hpd) {
4855                intel_dp->reset_link_params = true;
4856                return IRQ_NONE;
4857        }
4858
4859        if (intel_dp->is_mst) {
4860                if (!intel_dp_check_mst_status(intel_dp))
4861                        return IRQ_NONE;
4862        } else if (!intel_dp_short_pulse(intel_dp)) {
4863                return IRQ_NONE;
4864        }
4865
4866        return IRQ_HANDLED;
4867}
4868
4869/* check the VBT to see whether the eDP is on another port */
4870bool intel_dp_is_port_edp(struct drm_i915_private *dev_priv, enum port port)
4871{
4872        /*
4873         * eDP not supported on g4x. so bail out early just
4874         * for a bit extra safety in case the VBT is bonkers.
4875         */
4876        if (DISPLAY_VER(dev_priv) < 5)
4877                return false;
4878
4879        if (DISPLAY_VER(dev_priv) < 9 && port == PORT_A)
4880                return true;
4881
4882        return intel_bios_is_port_edp(dev_priv, port);
4883}
4884
4885static void
4886intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
4887{
4888        struct drm_i915_private *dev_priv = to_i915(connector->dev);
4889        enum port port = dp_to_dig_port(intel_dp)->base.port;
4890
4891        if (!intel_dp_is_edp(intel_dp))
4892                drm_connector_attach_dp_subconnector_property(connector);
4893
4894        if (!IS_G4X(dev_priv) && port != PORT_A)
4895                intel_attach_force_audio_property(connector);
4896
4897        intel_attach_broadcast_rgb_property(connector);
4898        if (HAS_GMCH(dev_priv))
4899                drm_connector_attach_max_bpc_property(connector, 6, 10);
4900        else if (DISPLAY_VER(dev_priv) >= 5)
4901                drm_connector_attach_max_bpc_property(connector, 6, 12);
4902
4903        /* Register HDMI colorspace for case of lspcon */
4904        if (intel_bios_is_lspcon_present(dev_priv, port)) {
4905                drm_connector_attach_content_type_property(connector);
4906                intel_attach_hdmi_colorspace_property(connector);
4907        } else {
4908                intel_attach_dp_colorspace_property(connector);
4909        }
4910
4911        if (IS_GEMINILAKE(dev_priv) || DISPLAY_VER(dev_priv) >= 11)
4912                drm_object_attach_property(&connector->base,
4913                                           connector->dev->mode_config.hdr_output_metadata_property,
4914                                           0);
4915
4916        if (intel_dp_is_edp(intel_dp)) {
4917                u32 allowed_scalers;
4918
4919                allowed_scalers = BIT(DRM_MODE_SCALE_ASPECT) | BIT(DRM_MODE_SCALE_FULLSCREEN);
4920                if (!HAS_GMCH(dev_priv))
4921                        allowed_scalers |= BIT(DRM_MODE_SCALE_CENTER);
4922
4923                drm_connector_attach_scaling_mode_property(connector, allowed_scalers);
4924
4925                connector->state->scaling_mode = DRM_MODE_SCALE_ASPECT;
4926
4927        }
4928
4929        if (HAS_VRR(dev_priv))
4930                drm_connector_attach_vrr_capable_property(connector);
4931}
4932
4933static bool intel_edp_init_connector(struct intel_dp *intel_dp,
4934                                     struct intel_connector *intel_connector)
4935{
4936        struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
4937        struct drm_device *dev = &dev_priv->drm;
4938        struct drm_connector *connector = &intel_connector->base;
4939        struct drm_display_mode *fixed_mode = NULL;
4940        struct drm_display_mode *downclock_mode = NULL;
4941        bool has_dpcd;
4942        enum pipe pipe = INVALID_PIPE;
4943        struct edid *edid;
4944
4945        if (!intel_dp_is_edp(intel_dp))
4946                return true;
4947
4948        /*
4949         * On IBX/CPT we may get here with LVDS already registered. Since the
4950         * driver uses the only internal power sequencer available for both
4951         * eDP and LVDS bail out early in this case to prevent interfering
4952         * with an already powered-on LVDS power sequencer.
4953         */
4954        if (intel_get_lvds_encoder(dev_priv)) {
4955                drm_WARN_ON(dev,
4956                            !(HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)));
4957                drm_info(&dev_priv->drm,
4958                         "LVDS was detected, not registering eDP\n");
4959
4960                return false;
4961        }
4962
4963        intel_pps_init(intel_dp);
4964
4965        /* Cache DPCD and EDID for edp. */
4966        has_dpcd = intel_edp_init_dpcd(intel_dp);
4967
4968        if (!has_dpcd) {
4969                /* if this fails, presume the device is a ghost */
4970                drm_info(&dev_priv->drm,
4971                         "failed to retrieve link info, disabling eDP\n");
4972                goto out_vdd_off;
4973        }
4974
4975        mutex_lock(&dev->mode_config.mutex);
4976        edid = drm_get_edid(connector, &intel_dp->aux.ddc);
4977        if (edid) {
4978                if (drm_add_edid_modes(connector, edid)) {
4979                        drm_connector_update_edid_property(connector, edid);
4980                } else {
4981                        kfree(edid);
4982                        edid = ERR_PTR(-EINVAL);
4983                }
4984        } else {
4985                edid = ERR_PTR(-ENOENT);
4986        }
4987        intel_connector->edid = edid;
4988
4989        fixed_mode = intel_panel_edid_fixed_mode(intel_connector);
4990        if (fixed_mode)
4991                downclock_mode = intel_drrs_init(intel_connector, fixed_mode);
4992
4993        /* MSO requires information from the EDID */
4994        intel_edp_mso_init(intel_dp);
4995
4996        /* multiply the mode clock and horizontal timings for MSO */
4997        intel_edp_mso_mode_fixup(intel_connector, fixed_mode);
4998        intel_edp_mso_mode_fixup(intel_connector, downclock_mode);
4999
5000        /* fallback to VBT if available for eDP */
5001        if (!fixed_mode)
5002                fixed_mode = intel_panel_vbt_fixed_mode(intel_connector);
5003        mutex_unlock(&dev->mode_config.mutex);
5004
5005        if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
5006                /*
5007                 * Figure out the current pipe for the initial backlight setup.
5008                 * If the current pipe isn't valid, try the PPS pipe, and if that
5009                 * fails just assume pipe A.
5010                 */
5011                pipe = vlv_active_pipe(intel_dp);
5012
5013                if (pipe != PIPE_A && pipe != PIPE_B)
5014                        pipe = intel_dp->pps.pps_pipe;
5015
5016                if (pipe != PIPE_A && pipe != PIPE_B)
5017                        pipe = PIPE_A;
5018
5019                drm_dbg_kms(&dev_priv->drm,
5020                            "using pipe %c for initial backlight setup\n",
5021                            pipe_name(pipe));
5022        }
5023
5024        intel_panel_init(&intel_connector->panel, fixed_mode, downclock_mode);
5025        if (!(dev_priv->quirks & QUIRK_NO_PPS_BACKLIGHT_POWER_HOOK))
5026                intel_connector->panel.backlight.power = intel_pps_backlight_power;
5027        intel_backlight_setup(intel_connector, pipe);
5028
5029        if (fixed_mode) {
5030                drm_connector_set_panel_orientation_with_quirk(connector,
5031                                dev_priv->vbt.orientation,
5032                                fixed_mode->hdisplay, fixed_mode->vdisplay);
5033        }
5034
5035        return true;
5036
5037out_vdd_off:
5038        intel_pps_vdd_off_sync(intel_dp);
5039
5040        return false;
5041}
5042
5043static void intel_dp_modeset_retry_work_fn(struct work_struct *work)
5044{
5045        struct intel_connector *intel_connector;
5046        struct drm_connector *connector;
5047
5048        intel_connector = container_of(work, typeof(*intel_connector),
5049                                       modeset_retry_work);
5050        connector = &intel_connector->base;
5051        DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
5052                      connector->name);
5053
5054        /* Grab the locks before changing connector property*/
5055        mutex_lock(&connector->dev->mode_config.mutex);
5056        /* Set connector link status to BAD and send a Uevent to notify
5057         * userspace to do a modeset.
5058         */
5059        drm_connector_set_link_status_property(connector,
5060                                               DRM_MODE_LINK_STATUS_BAD);
5061        mutex_unlock(&connector->dev->mode_config.mutex);
5062        /* Send Hotplug uevent so userspace can reprobe */
5063        drm_kms_helper_connector_hotplug_event(connector);
5064}
5065
5066bool
5067intel_dp_init_connector(struct intel_digital_port *dig_port,
5068                        struct intel_connector *intel_connector)
5069{
5070        struct drm_connector *connector = &intel_connector->base;
5071        struct intel_dp *intel_dp = &dig_port->dp;
5072        struct intel_encoder *intel_encoder = &dig_port->base;
5073        struct drm_device *dev = intel_encoder->base.dev;
5074        struct drm_i915_private *dev_priv = to_i915(dev);
5075        enum port port = intel_encoder->port;
5076        enum phy phy = intel_port_to_phy(dev_priv, port);
5077        int type;
5078
5079        /* Initialize the work for modeset in case of link train failure */
5080        INIT_WORK(&intel_connector->modeset_retry_work,
5081                  intel_dp_modeset_retry_work_fn);
5082
5083        if (drm_WARN(dev, dig_port->max_lanes < 1,
5084                     "Not enough lanes (%d) for DP on [ENCODER:%d:%s]\n",
5085                     dig_port->max_lanes, intel_encoder->base.base.id,
5086                     intel_encoder->base.name))
5087                return false;
5088
5089        intel_dp->reset_link_params = true;
5090        intel_dp->pps.pps_pipe = INVALID_PIPE;
5091        intel_dp->pps.active_pipe = INVALID_PIPE;
5092
5093        /* Preserve the current hw state. */
5094        intel_dp->DP = intel_de_read(dev_priv, intel_dp->output_reg);
5095        intel_dp->attached_connector = intel_connector;
5096
5097        if (intel_dp_is_port_edp(dev_priv, port)) {
5098                /*
5099                 * Currently we don't support eDP on TypeC ports, although in
5100                 * theory it could work on TypeC legacy ports.
5101                 */
5102                drm_WARN_ON(dev, intel_phy_is_tc(dev_priv, phy));
5103                type = DRM_MODE_CONNECTOR_eDP;
5104                intel_encoder->type = INTEL_OUTPUT_EDP;
5105
5106                /* eDP only on port B and/or C on vlv/chv */
5107                if (drm_WARN_ON(dev, (IS_VALLEYVIEW(dev_priv) ||
5108                                      IS_CHERRYVIEW(dev_priv)) &&
5109                                port != PORT_B && port != PORT_C))
5110                        return false;
5111        } else {
5112                type = DRM_MODE_CONNECTOR_DisplayPort;
5113        }
5114
5115        intel_dp_set_source_rates(intel_dp);
5116        intel_dp_set_default_sink_rates(intel_dp);
5117        intel_dp_set_default_max_sink_lane_count(intel_dp);
5118        intel_dp_set_common_rates(intel_dp);
5119        intel_dp_reset_max_link_params(intel_dp);
5120
5121        if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
5122                intel_dp->pps.active_pipe = vlv_active_pipe(intel_dp);
5123
5124        drm_dbg_kms(&dev_priv->drm,
5125                    "Adding %s connector on [ENCODER:%d:%s]\n",
5126                    type == DRM_MODE_CONNECTOR_eDP ? "eDP" : "DP",
5127                    intel_encoder->base.base.id, intel_encoder->base.name);
5128
5129        drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
5130        drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
5131
5132        if (!HAS_GMCH(dev_priv))
5133                connector->interlace_allowed = true;
5134        connector->doublescan_allowed = 0;
5135
5136        intel_connector->polled = DRM_CONNECTOR_POLL_HPD;
5137
5138        intel_dp_aux_init(intel_dp);
5139
5140        intel_connector_attach_encoder(intel_connector, intel_encoder);
5141
5142        if (HAS_DDI(dev_priv))
5143                intel_connector->get_hw_state = intel_ddi_connector_get_hw_state;
5144        else
5145                intel_connector->get_hw_state = intel_connector_get_hw_state;
5146
5147        /* init MST on ports that can support it */
5148        intel_dp_mst_encoder_init(dig_port,
5149                                  intel_connector->base.base.id);
5150
5151        if (!intel_edp_init_connector(intel_dp, intel_connector)) {
5152                intel_dp_aux_fini(intel_dp);
5153                intel_dp_mst_encoder_cleanup(dig_port);
5154                goto fail;
5155        }
5156
5157        intel_dp_add_properties(intel_dp, connector);
5158
5159        if (is_hdcp_supported(dev_priv, port) && !intel_dp_is_edp(intel_dp)) {
5160                int ret = intel_dp_hdcp_init(dig_port, intel_connector);
5161                if (ret)
5162                        drm_dbg_kms(&dev_priv->drm,
5163                                    "HDCP init failed, skipping.\n");
5164        }
5165
5166        /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
5167         * 0xd.  Failure to do so will result in spurious interrupts being
5168         * generated on the port when a cable is not attached.
5169         */
5170        if (IS_G45(dev_priv)) {
5171                u32 temp = intel_de_read(dev_priv, PEG_BAND_GAP_DATA);
5172                intel_de_write(dev_priv, PEG_BAND_GAP_DATA,
5173                               (temp & ~0xf) | 0xd);
5174        }
5175
5176        intel_dp->frl.is_trained = false;
5177        intel_dp->frl.trained_rate_gbps = 0;
5178
5179        intel_psr_init(intel_dp);
5180
5181        return true;
5182
5183fail:
5184        drm_connector_cleanup(connector);
5185
5186        return false;
5187}
5188
5189void intel_dp_mst_suspend(struct drm_i915_private *dev_priv)
5190{
5191        struct intel_encoder *encoder;
5192
5193        if (!HAS_DISPLAY(dev_priv))
5194                return;
5195
5196        for_each_intel_encoder(&dev_priv->drm, encoder) {
5197                struct intel_dp *intel_dp;
5198
5199                if (encoder->type != INTEL_OUTPUT_DDI)
5200                        continue;
5201
5202                intel_dp = enc_to_intel_dp(encoder);
5203
5204                if (!intel_dp_mst_source_support(intel_dp))
5205                        continue;
5206
5207                if (intel_dp->is_mst)
5208                        drm_dp_mst_topology_mgr_suspend(&intel_dp->mst_mgr);
5209        }
5210}
5211
5212void intel_dp_mst_resume(struct drm_i915_private *dev_priv)
5213{
5214        struct intel_encoder *encoder;
5215
5216        if (!HAS_DISPLAY(dev_priv))
5217                return;
5218
5219        for_each_intel_encoder(&dev_priv->drm, encoder) {
5220                struct intel_dp *intel_dp;
5221                int ret;
5222
5223                if (encoder->type != INTEL_OUTPUT_DDI)
5224                        continue;
5225
5226                intel_dp = enc_to_intel_dp(encoder);
5227
5228                if (!intel_dp_mst_source_support(intel_dp))
5229                        continue;
5230
5231                ret = drm_dp_mst_topology_mgr_resume(&intel_dp->mst_mgr,
5232                                                     true);
5233                if (ret) {
5234                        intel_dp->is_mst = false;
5235                        drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr,
5236                                                        false);
5237                }
5238        }
5239}
5240