Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: MIT
0002 /*
0003  * Copyright © 2022 Intel Corporation
0004  *
0005  * Read out the current hardware modeset state, and sanitize it to the current
0006  * state.
0007  */
0008 
0009 #include <drm/drm_atomic_uapi.h>
0010 #include <drm/drm_atomic_state_helper.h>
0011 
0012 #include "i915_drv.h"
0013 #include "intel_atomic.h"
0014 #include "intel_bw.h"
0015 #include "intel_color.h"
0016 #include "intel_crtc.h"
0017 #include "intel_crtc_state_dump.h"
0018 #include "intel_ddi.h"
0019 #include "intel_de.h"
0020 #include "intel_display.h"
0021 #include "intel_display_power.h"
0022 #include "intel_display_types.h"
0023 #include "intel_modeset_setup.h"
0024 #include "intel_pch_display.h"
0025 #include "intel_pm.h"
0026 
0027 static void intel_crtc_disable_noatomic(struct intel_crtc *crtc,
0028                     struct drm_modeset_acquire_ctx *ctx)
0029 {
0030     struct intel_encoder *encoder;
0031     struct drm_i915_private *i915 = to_i915(crtc->base.dev);
0032     struct intel_bw_state *bw_state =
0033         to_intel_bw_state(i915->bw_obj.state);
0034     struct intel_cdclk_state *cdclk_state =
0035         to_intel_cdclk_state(i915->cdclk.obj.state);
0036     struct intel_dbuf_state *dbuf_state =
0037         to_intel_dbuf_state(i915->dbuf.obj.state);
0038     struct intel_crtc_state *crtc_state =
0039         to_intel_crtc_state(crtc->base.state);
0040     struct intel_plane *plane;
0041     struct drm_atomic_state *state;
0042     struct intel_crtc_state *temp_crtc_state;
0043     enum pipe pipe = crtc->pipe;
0044     int ret;
0045 
0046     if (!crtc_state->hw.active)
0047         return;
0048 
0049     for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) {
0050         const struct intel_plane_state *plane_state =
0051             to_intel_plane_state(plane->base.state);
0052 
0053         if (plane_state->uapi.visible)
0054             intel_plane_disable_noatomic(crtc, plane);
0055     }
0056 
0057     state = drm_atomic_state_alloc(&i915->drm);
0058     if (!state) {
0059         drm_dbg_kms(&i915->drm,
0060                 "failed to disable [CRTC:%d:%s], out of memory",
0061                 crtc->base.base.id, crtc->base.name);
0062         return;
0063     }
0064 
0065     state->acquire_ctx = ctx;
0066 
0067     /* Everything's already locked, -EDEADLK can't happen. */
0068     temp_crtc_state = intel_atomic_get_crtc_state(state, crtc);
0069     ret = drm_atomic_add_affected_connectors(state, &crtc->base);
0070 
0071     drm_WARN_ON(&i915->drm, IS_ERR(temp_crtc_state) || ret);
0072 
0073     i915->display->crtc_disable(to_intel_atomic_state(state), crtc);
0074 
0075     drm_atomic_state_put(state);
0076 
0077     drm_dbg_kms(&i915->drm,
0078             "[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
0079             crtc->base.base.id, crtc->base.name);
0080 
0081     crtc->active = false;
0082     crtc->base.enabled = false;
0083 
0084     drm_WARN_ON(&i915->drm,
0085             drm_atomic_set_mode_for_crtc(&crtc_state->uapi, NULL) < 0);
0086     crtc_state->uapi.active = false;
0087     crtc_state->uapi.connector_mask = 0;
0088     crtc_state->uapi.encoder_mask = 0;
0089     intel_crtc_free_hw_state(crtc_state);
0090     memset(&crtc_state->hw, 0, sizeof(crtc_state->hw));
0091 
0092     for_each_encoder_on_crtc(&i915->drm, &crtc->base, encoder)
0093         encoder->base.crtc = NULL;
0094 
0095     intel_fbc_disable(crtc);
0096     intel_update_watermarks(i915);
0097     intel_disable_shared_dpll(crtc_state);
0098 
0099     intel_display_power_put_all_in_set(i915, &crtc->enabled_power_domains);
0100 
0101     cdclk_state->min_cdclk[pipe] = 0;
0102     cdclk_state->min_voltage_level[pipe] = 0;
0103     cdclk_state->active_pipes &= ~BIT(pipe);
0104 
0105     dbuf_state->active_pipes &= ~BIT(pipe);
0106 
0107     bw_state->data_rate[pipe] = 0;
0108     bw_state->num_active_planes[pipe] = 0;
0109 }
0110 
0111 static void intel_modeset_update_connector_atomic_state(struct drm_i915_private *i915)
0112 {
0113     struct intel_connector *connector;
0114     struct drm_connector_list_iter conn_iter;
0115 
0116     drm_connector_list_iter_begin(&i915->drm, &conn_iter);
0117     for_each_intel_connector_iter(connector, &conn_iter) {
0118         struct drm_connector_state *conn_state = connector->base.state;
0119         struct intel_encoder *encoder =
0120             to_intel_encoder(connector->base.encoder);
0121 
0122         if (conn_state->crtc)
0123             drm_connector_put(&connector->base);
0124 
0125         if (encoder) {
0126             struct intel_crtc *crtc =
0127                 to_intel_crtc(encoder->base.crtc);
0128             const struct intel_crtc_state *crtc_state =
0129                 to_intel_crtc_state(crtc->base.state);
0130 
0131             conn_state->best_encoder = &encoder->base;
0132             conn_state->crtc = &crtc->base;
0133             conn_state->max_bpc = (crtc_state->pipe_bpp ?: 24) / 3;
0134 
0135             drm_connector_get(&connector->base);
0136         } else {
0137             conn_state->best_encoder = NULL;
0138             conn_state->crtc = NULL;
0139         }
0140     }
0141     drm_connector_list_iter_end(&conn_iter);
0142 }
0143 
0144 static void intel_crtc_copy_hw_to_uapi_state(struct intel_crtc_state *crtc_state)
0145 {
0146     if (intel_crtc_is_bigjoiner_slave(crtc_state))
0147         return;
0148 
0149     crtc_state->uapi.enable = crtc_state->hw.enable;
0150     crtc_state->uapi.active = crtc_state->hw.active;
0151     drm_WARN_ON(crtc_state->uapi.crtc->dev,
0152             drm_atomic_set_mode_for_crtc(&crtc_state->uapi, &crtc_state->hw.mode) < 0);
0153 
0154     crtc_state->uapi.adjusted_mode = crtc_state->hw.adjusted_mode;
0155     crtc_state->uapi.scaling_filter = crtc_state->hw.scaling_filter;
0156 
0157     drm_property_replace_blob(&crtc_state->uapi.degamma_lut,
0158                   crtc_state->hw.degamma_lut);
0159     drm_property_replace_blob(&crtc_state->uapi.gamma_lut,
0160                   crtc_state->hw.gamma_lut);
0161     drm_property_replace_blob(&crtc_state->uapi.ctm,
0162                   crtc_state->hw.ctm);
0163 }
0164 
0165 static void
0166 intel_sanitize_plane_mapping(struct drm_i915_private *i915)
0167 {
0168     struct intel_crtc *crtc;
0169 
0170     if (DISPLAY_VER(i915) >= 4)
0171         return;
0172 
0173     for_each_intel_crtc(&i915->drm, crtc) {
0174         struct intel_plane *plane =
0175             to_intel_plane(crtc->base.primary);
0176         struct intel_crtc *plane_crtc;
0177         enum pipe pipe;
0178 
0179         if (!plane->get_hw_state(plane, &pipe))
0180             continue;
0181 
0182         if (pipe == crtc->pipe)
0183             continue;
0184 
0185         drm_dbg_kms(&i915->drm,
0186                 "[PLANE:%d:%s] attached to the wrong pipe, disabling plane\n",
0187                 plane->base.base.id, plane->base.name);
0188 
0189         plane_crtc = intel_crtc_for_pipe(i915, pipe);
0190         intel_plane_disable_noatomic(plane_crtc, plane);
0191     }
0192 }
0193 
0194 static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
0195 {
0196     struct drm_device *dev = crtc->base.dev;
0197     struct intel_encoder *encoder;
0198 
0199     for_each_encoder_on_crtc(dev, &crtc->base, encoder)
0200         return true;
0201 
0202     return false;
0203 }
0204 
0205 static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
0206 {
0207     struct drm_device *dev = encoder->base.dev;
0208     struct intel_connector *connector;
0209 
0210     for_each_connector_on_encoder(dev, &encoder->base, connector)
0211         return connector;
0212 
0213     return NULL;
0214 }
0215 
0216 static void intel_sanitize_fifo_underrun_reporting(const struct intel_crtc_state *crtc_state)
0217 {
0218     struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
0219     struct drm_i915_private *i915 = to_i915(crtc->base.dev);
0220 
0221     if (!crtc_state->hw.active && !HAS_GMCH(i915))
0222         return;
0223 
0224     /*
0225      * We start out with underrun reporting disabled to avoid races.
0226      * For correct bookkeeping mark this on active crtcs.
0227      *
0228      * Also on gmch platforms we dont have any hardware bits to
0229      * disable the underrun reporting. Which means we need to start
0230      * out with underrun reporting disabled also on inactive pipes,
0231      * since otherwise we'll complain about the garbage we read when
0232      * e.g. coming up after runtime pm.
0233      *
0234      * No protection against concurrent access is required - at
0235      * worst a fifo underrun happens which also sets this to false.
0236      */
0237     crtc->cpu_fifo_underrun_disabled = true;
0238 
0239     /*
0240      * We track the PCH trancoder underrun reporting state
0241      * within the crtc. With crtc for pipe A housing the underrun
0242      * reporting state for PCH transcoder A, crtc for pipe B housing
0243      * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
0244      * and marking underrun reporting as disabled for the non-existing
0245      * PCH transcoders B and C would prevent enabling the south
0246      * error interrupt (see cpt_can_enable_serr_int()).
0247      */
0248     if (intel_has_pch_trancoder(i915, crtc->pipe))
0249         crtc->pch_fifo_underrun_disabled = true;
0250 }
0251 
0252 static void intel_sanitize_crtc(struct intel_crtc *crtc,
0253                 struct drm_modeset_acquire_ctx *ctx)
0254 {
0255     struct drm_i915_private *i915 = to_i915(crtc->base.dev);
0256     struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
0257 
0258     if (crtc_state->hw.active) {
0259         struct intel_plane *plane;
0260 
0261         /* Disable everything but the primary plane */
0262         for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) {
0263             const struct intel_plane_state *plane_state =
0264                 to_intel_plane_state(plane->base.state);
0265 
0266             if (plane_state->uapi.visible &&
0267                 plane->base.type != DRM_PLANE_TYPE_PRIMARY)
0268                 intel_plane_disable_noatomic(crtc, plane);
0269         }
0270 
0271         /* Disable any background color/etc. set by the BIOS */
0272         intel_color_commit_noarm(crtc_state);
0273         intel_color_commit_arm(crtc_state);
0274     }
0275 
0276     /*
0277      * Adjust the state of the output pipe according to whether we have
0278      * active connectors/encoders.
0279      */
0280     if (crtc_state->hw.active && !intel_crtc_has_encoders(crtc) &&
0281         !intel_crtc_is_bigjoiner_slave(crtc_state))
0282         intel_crtc_disable_noatomic(crtc, ctx);
0283 }
0284 
0285 static bool has_bogus_dpll_config(const struct intel_crtc_state *crtc_state)
0286 {
0287     struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
0288 
0289     /*
0290      * Some SNB BIOSen (eg. ASUS K53SV) are known to misprogram
0291      * the hardware when a high res displays plugged in. DPLL P
0292      * divider is zero, and the pipe timings are bonkers. We'll
0293      * try to disable everything in that case.
0294      *
0295      * FIXME would be nice to be able to sanitize this state
0296      * without several WARNs, but for now let's take the easy
0297      * road.
0298      */
0299     return IS_SANDYBRIDGE(i915) &&
0300         crtc_state->hw.active &&
0301         crtc_state->shared_dpll &&
0302         crtc_state->port_clock == 0;
0303 }
0304 
0305 static void intel_sanitize_encoder(struct intel_encoder *encoder)
0306 {
0307     struct drm_i915_private *i915 = to_i915(encoder->base.dev);
0308     struct intel_connector *connector;
0309     struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
0310     struct intel_crtc_state *crtc_state = crtc ?
0311         to_intel_crtc_state(crtc->base.state) : NULL;
0312 
0313     /*
0314      * We need to check both for a crtc link (meaning that the encoder is
0315      * active and trying to read from a pipe) and the pipe itself being
0316      * active.
0317      */
0318     bool has_active_crtc = crtc_state &&
0319         crtc_state->hw.active;
0320 
0321     if (crtc_state && has_bogus_dpll_config(crtc_state)) {
0322         drm_dbg_kms(&i915->drm,
0323                 "BIOS has misprogrammed the hardware. Disabling pipe %c\n",
0324                 pipe_name(crtc->pipe));
0325         has_active_crtc = false;
0326     }
0327 
0328     connector = intel_encoder_find_connector(encoder);
0329     if (connector && !has_active_crtc) {
0330         drm_dbg_kms(&i915->drm,
0331                 "[ENCODER:%d:%s] has active connectors but no active pipe!\n",
0332                 encoder->base.base.id,
0333                 encoder->base.name);
0334 
0335         /*
0336          * Connector is active, but has no active pipe. This is fallout
0337          * from our resume register restoring. Disable the encoder
0338          * manually again.
0339          */
0340         if (crtc_state) {
0341             struct drm_encoder *best_encoder;
0342 
0343             drm_dbg_kms(&i915->drm,
0344                     "[ENCODER:%d:%s] manually disabled\n",
0345                     encoder->base.base.id,
0346                     encoder->base.name);
0347 
0348             /* avoid oopsing in case the hooks consult best_encoder */
0349             best_encoder = connector->base.state->best_encoder;
0350             connector->base.state->best_encoder = &encoder->base;
0351 
0352             /* FIXME NULL atomic state passed! */
0353             if (encoder->disable)
0354                 encoder->disable(NULL, encoder, crtc_state,
0355                          connector->base.state);
0356             if (encoder->post_disable)
0357                 encoder->post_disable(NULL, encoder, crtc_state,
0358                               connector->base.state);
0359 
0360             connector->base.state->best_encoder = best_encoder;
0361         }
0362         encoder->base.crtc = NULL;
0363 
0364         /*
0365          * Inconsistent output/port/pipe state happens presumably due to
0366          * a bug in one of the get_hw_state functions. Or someplace else
0367          * in our code, like the register restore mess on resume. Clamp
0368          * things to off as a safer default.
0369          */
0370         connector->base.dpms = DRM_MODE_DPMS_OFF;
0371         connector->base.encoder = NULL;
0372     }
0373 
0374     /* notify opregion of the sanitized encoder state */
0375     intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
0376 
0377     if (HAS_DDI(i915))
0378         intel_ddi_sanitize_encoder_pll_mapping(encoder);
0379 }
0380 
0381 /* FIXME read out full plane state for all planes */
0382 static void readout_plane_state(struct drm_i915_private *i915)
0383 {
0384     struct intel_plane *plane;
0385     struct intel_crtc *crtc;
0386 
0387     for_each_intel_plane(&i915->drm, plane) {
0388         struct intel_plane_state *plane_state =
0389             to_intel_plane_state(plane->base.state);
0390         struct intel_crtc_state *crtc_state;
0391         enum pipe pipe = PIPE_A;
0392         bool visible;
0393 
0394         visible = plane->get_hw_state(plane, &pipe);
0395 
0396         crtc = intel_crtc_for_pipe(i915, pipe);
0397         crtc_state = to_intel_crtc_state(crtc->base.state);
0398 
0399         intel_set_plane_visible(crtc_state, plane_state, visible);
0400 
0401         drm_dbg_kms(&i915->drm,
0402                 "[PLANE:%d:%s] hw state readout: %s, pipe %c\n",
0403                 plane->base.base.id, plane->base.name,
0404                 str_enabled_disabled(visible), pipe_name(pipe));
0405     }
0406 
0407     for_each_intel_crtc(&i915->drm, crtc) {
0408         struct intel_crtc_state *crtc_state =
0409             to_intel_crtc_state(crtc->base.state);
0410 
0411         intel_plane_fixup_bitmasks(crtc_state);
0412     }
0413 }
0414 
0415 static void intel_modeset_readout_hw_state(struct drm_i915_private *i915)
0416 {
0417     struct intel_cdclk_state *cdclk_state =
0418         to_intel_cdclk_state(i915->cdclk.obj.state);
0419     struct intel_dbuf_state *dbuf_state =
0420         to_intel_dbuf_state(i915->dbuf.obj.state);
0421     enum pipe pipe;
0422     struct intel_crtc *crtc;
0423     struct intel_encoder *encoder;
0424     struct intel_connector *connector;
0425     struct drm_connector_list_iter conn_iter;
0426     u8 active_pipes = 0;
0427 
0428     for_each_intel_crtc(&i915->drm, crtc) {
0429         struct intel_crtc_state *crtc_state =
0430             to_intel_crtc_state(crtc->base.state);
0431 
0432         __drm_atomic_helper_crtc_destroy_state(&crtc_state->uapi);
0433         intel_crtc_free_hw_state(crtc_state);
0434         intel_crtc_state_reset(crtc_state, crtc);
0435 
0436         intel_crtc_get_pipe_config(crtc_state);
0437 
0438         crtc_state->hw.enable = crtc_state->hw.active;
0439 
0440         crtc->base.enabled = crtc_state->hw.enable;
0441         crtc->active = crtc_state->hw.active;
0442 
0443         if (crtc_state->hw.active)
0444             active_pipes |= BIT(crtc->pipe);
0445 
0446         drm_dbg_kms(&i915->drm,
0447                 "[CRTC:%d:%s] hw state readout: %s\n",
0448                 crtc->base.base.id, crtc->base.name,
0449                 str_enabled_disabled(crtc_state->hw.active));
0450     }
0451 
0452     cdclk_state->active_pipes = active_pipes;
0453     dbuf_state->active_pipes = active_pipes;
0454 
0455     readout_plane_state(i915);
0456 
0457     for_each_intel_encoder(&i915->drm, encoder) {
0458         struct intel_crtc_state *crtc_state = NULL;
0459 
0460         pipe = 0;
0461 
0462         if (encoder->get_hw_state(encoder, &pipe)) {
0463             crtc = intel_crtc_for_pipe(i915, pipe);
0464             crtc_state = to_intel_crtc_state(crtc->base.state);
0465 
0466             encoder->base.crtc = &crtc->base;
0467             intel_encoder_get_config(encoder, crtc_state);
0468 
0469             /* read out to slave crtc as well for bigjoiner */
0470             if (crtc_state->bigjoiner_pipes) {
0471                 struct intel_crtc *slave_crtc;
0472 
0473                 /* encoder should read be linked to bigjoiner master */
0474                 WARN_ON(intel_crtc_is_bigjoiner_slave(crtc_state));
0475 
0476                 for_each_intel_crtc_in_pipe_mask(&i915->drm, slave_crtc,
0477                                  intel_crtc_bigjoiner_slave_pipes(crtc_state)) {
0478                     struct intel_crtc_state *slave_crtc_state;
0479 
0480                     slave_crtc_state = to_intel_crtc_state(slave_crtc->base.state);
0481                     intel_encoder_get_config(encoder, slave_crtc_state);
0482                 }
0483             }
0484         } else {
0485             encoder->base.crtc = NULL;
0486         }
0487 
0488         if (encoder->sync_state)
0489             encoder->sync_state(encoder, crtc_state);
0490 
0491         drm_dbg_kms(&i915->drm,
0492                 "[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
0493                 encoder->base.base.id, encoder->base.name,
0494                 str_enabled_disabled(encoder->base.crtc),
0495                 pipe_name(pipe));
0496     }
0497 
0498     intel_dpll_readout_hw_state(i915);
0499 
0500     drm_connector_list_iter_begin(&i915->drm, &conn_iter);
0501     for_each_intel_connector_iter(connector, &conn_iter) {
0502         if (connector->get_hw_state(connector)) {
0503             struct intel_crtc_state *crtc_state;
0504             struct intel_crtc *crtc;
0505 
0506             connector->base.dpms = DRM_MODE_DPMS_ON;
0507 
0508             encoder = intel_attached_encoder(connector);
0509             connector->base.encoder = &encoder->base;
0510 
0511             crtc = to_intel_crtc(encoder->base.crtc);
0512             crtc_state = crtc ? to_intel_crtc_state(crtc->base.state) : NULL;
0513 
0514             if (crtc_state && crtc_state->hw.active) {
0515                 /*
0516                  * This has to be done during hardware readout
0517                  * because anything calling .crtc_disable may
0518                  * rely on the connector_mask being accurate.
0519                  */
0520                 crtc_state->uapi.connector_mask |=
0521                     drm_connector_mask(&connector->base);
0522                 crtc_state->uapi.encoder_mask |=
0523                     drm_encoder_mask(&encoder->base);
0524             }
0525         } else {
0526             connector->base.dpms = DRM_MODE_DPMS_OFF;
0527             connector->base.encoder = NULL;
0528         }
0529         drm_dbg_kms(&i915->drm,
0530                 "[CONNECTOR:%d:%s] hw state readout: %s\n",
0531                 connector->base.base.id, connector->base.name,
0532                 str_enabled_disabled(connector->base.encoder));
0533     }
0534     drm_connector_list_iter_end(&conn_iter);
0535 
0536     for_each_intel_crtc(&i915->drm, crtc) {
0537         struct intel_bw_state *bw_state =
0538             to_intel_bw_state(i915->bw_obj.state);
0539         struct intel_crtc_state *crtc_state =
0540             to_intel_crtc_state(crtc->base.state);
0541         struct intel_plane *plane;
0542         int min_cdclk = 0;
0543 
0544         if (crtc_state->hw.active) {
0545             /*
0546              * The initial mode needs to be set in order to keep
0547              * the atomic core happy. It wants a valid mode if the
0548              * crtc's enabled, so we do the above call.
0549              *
0550              * But we don't set all the derived state fully, hence
0551              * set a flag to indicate that a full recalculation is
0552              * needed on the next commit.
0553              */
0554             crtc_state->inherited = true;
0555 
0556             intel_crtc_update_active_timings(crtc_state);
0557 
0558             intel_crtc_copy_hw_to_uapi_state(crtc_state);
0559         }
0560 
0561         for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) {
0562             const struct intel_plane_state *plane_state =
0563                 to_intel_plane_state(plane->base.state);
0564 
0565             /*
0566              * FIXME don't have the fb yet, so can't
0567              * use intel_plane_data_rate() :(
0568              */
0569             if (plane_state->uapi.visible)
0570                 crtc_state->data_rate[plane->id] =
0571                     4 * crtc_state->pixel_rate;
0572             /*
0573              * FIXME don't have the fb yet, so can't
0574              * use plane->min_cdclk() :(
0575              */
0576             if (plane_state->uapi.visible && plane->min_cdclk) {
0577                 if (crtc_state->double_wide || DISPLAY_VER(i915) >= 10)
0578                     crtc_state->min_cdclk[plane->id] =
0579                         DIV_ROUND_UP(crtc_state->pixel_rate, 2);
0580                 else
0581                     crtc_state->min_cdclk[plane->id] =
0582                         crtc_state->pixel_rate;
0583             }
0584             drm_dbg_kms(&i915->drm,
0585                     "[PLANE:%d:%s] min_cdclk %d kHz\n",
0586                     plane->base.base.id, plane->base.name,
0587                     crtc_state->min_cdclk[plane->id]);
0588         }
0589 
0590         if (crtc_state->hw.active) {
0591             min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
0592             if (drm_WARN_ON(&i915->drm, min_cdclk < 0))
0593                 min_cdclk = 0;
0594         }
0595 
0596         cdclk_state->min_cdclk[crtc->pipe] = min_cdclk;
0597         cdclk_state->min_voltage_level[crtc->pipe] =
0598             crtc_state->min_voltage_level;
0599 
0600         intel_bw_crtc_update(bw_state, crtc_state);
0601     }
0602 }
0603 
0604 static void
0605 get_encoder_power_domains(struct drm_i915_private *i915)
0606 {
0607     struct intel_encoder *encoder;
0608 
0609     for_each_intel_encoder(&i915->drm, encoder) {
0610         struct intel_crtc_state *crtc_state;
0611 
0612         if (!encoder->get_power_domains)
0613             continue;
0614 
0615         /*
0616          * MST-primary and inactive encoders don't have a crtc state
0617          * and neither of these require any power domain references.
0618          */
0619         if (!encoder->base.crtc)
0620             continue;
0621 
0622         crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
0623         encoder->get_power_domains(encoder, crtc_state);
0624     }
0625 }
0626 
0627 static void intel_early_display_was(struct drm_i915_private *i915)
0628 {
0629     /*
0630      * Display WA #1185 WaDisableDARBFClkGating:glk,icl,ehl,tgl
0631      * Also known as Wa_14010480278.
0632      */
0633     if (IS_DISPLAY_VER(i915, 10, 12))
0634         intel_de_write(i915, GEN9_CLKGATE_DIS_0,
0635                    intel_de_read(i915, GEN9_CLKGATE_DIS_0) | DARBF_GATING_DIS);
0636 
0637     if (IS_HASWELL(i915)) {
0638         /*
0639          * WaRsPkgCStateDisplayPMReq:hsw
0640          * System hang if this isn't done before disabling all planes!
0641          */
0642         intel_de_write(i915, CHICKEN_PAR1_1,
0643                    intel_de_read(i915, CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
0644     }
0645 
0646     if (IS_KABYLAKE(i915) || IS_COFFEELAKE(i915) || IS_COMETLAKE(i915)) {
0647         /* Display WA #1142:kbl,cfl,cml */
0648         intel_de_rmw(i915, CHICKEN_PAR1_1,
0649                  KBL_ARB_FILL_SPARE_22, KBL_ARB_FILL_SPARE_22);
0650         intel_de_rmw(i915, CHICKEN_MISC_2,
0651                  KBL_ARB_FILL_SPARE_13 | KBL_ARB_FILL_SPARE_14,
0652                  KBL_ARB_FILL_SPARE_14);
0653     }
0654 }
0655 
0656 void intel_modeset_setup_hw_state(struct drm_i915_private *i915,
0657                   struct drm_modeset_acquire_ctx *ctx)
0658 {
0659     struct intel_encoder *encoder;
0660     struct intel_crtc *crtc;
0661     intel_wakeref_t wakeref;
0662 
0663     wakeref = intel_display_power_get(i915, POWER_DOMAIN_INIT);
0664 
0665     intel_early_display_was(i915);
0666     intel_modeset_readout_hw_state(i915);
0667 
0668     /* HW state is read out, now we need to sanitize this mess. */
0669     get_encoder_power_domains(i915);
0670 
0671     intel_pch_sanitize(i915);
0672 
0673     /*
0674      * intel_sanitize_plane_mapping() may need to do vblank
0675      * waits, so we need vblank interrupts restored beforehand.
0676      */
0677     for_each_intel_crtc(&i915->drm, crtc) {
0678         struct intel_crtc_state *crtc_state =
0679             to_intel_crtc_state(crtc->base.state);
0680 
0681         intel_sanitize_fifo_underrun_reporting(crtc_state);
0682 
0683         drm_crtc_vblank_reset(&crtc->base);
0684 
0685         if (crtc_state->hw.active)
0686             intel_crtc_vblank_on(crtc_state);
0687     }
0688 
0689     intel_fbc_sanitize(i915);
0690 
0691     intel_sanitize_plane_mapping(i915);
0692 
0693     for_each_intel_encoder(&i915->drm, encoder)
0694         intel_sanitize_encoder(encoder);
0695 
0696     for_each_intel_crtc(&i915->drm, crtc) {
0697         struct intel_crtc_state *crtc_state =
0698             to_intel_crtc_state(crtc->base.state);
0699 
0700         intel_sanitize_crtc(crtc, ctx);
0701         intel_crtc_state_dump(crtc_state, NULL, "setup_hw_state");
0702     }
0703 
0704     intel_modeset_update_connector_atomic_state(i915);
0705 
0706     intel_dpll_sanitize_state(i915);
0707 
0708     if (IS_G4X(i915)) {
0709         g4x_wm_get_hw_state(i915);
0710         g4x_wm_sanitize(i915);
0711     } else if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915)) {
0712         vlv_wm_get_hw_state(i915);
0713         vlv_wm_sanitize(i915);
0714     } else if (DISPLAY_VER(i915) >= 9) {
0715         skl_wm_get_hw_state(i915);
0716         skl_wm_sanitize(i915);
0717     } else if (HAS_PCH_SPLIT(i915)) {
0718         ilk_wm_get_hw_state(i915);
0719     }
0720 
0721     for_each_intel_crtc(&i915->drm, crtc) {
0722         struct intel_crtc_state *crtc_state =
0723             to_intel_crtc_state(crtc->base.state);
0724         struct intel_power_domain_mask put_domains;
0725 
0726         intel_modeset_get_crtc_power_domains(crtc_state, &put_domains);
0727         if (drm_WARN_ON(&i915->drm, !bitmap_empty(put_domains.bits, POWER_DOMAIN_NUM)))
0728             intel_modeset_put_crtc_power_domains(crtc, &put_domains);
0729     }
0730 
0731     intel_display_power_put(i915, POWER_DOMAIN_INIT, wakeref);
0732 
0733     intel_power_domains_sanitize_state(i915);
0734 }