LCOV - code coverage report
Current view: top level - dev/pci/drm - drm_atomic_helper.c (source / functions) Hit Total Coverage
Test: 6.4 Lines: 0 897 0.0 %
Date: 2018-10-19 03:25:38 Functions: 0 50 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2014 Red Hat
       3             :  * Copyright (C) 2014 Intel Corp.
       4             :  *
       5             :  * Permission is hereby granted, free of charge, to any person obtaining a
       6             :  * copy of this software and associated documentation files (the "Software"),
       7             :  * to deal in the Software without restriction, including without limitation
       8             :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
       9             :  * and/or sell copies of the Software, and to permit persons to whom the
      10             :  * Software is furnished to do so, subject to the following conditions:
      11             :  *
      12             :  * The above copyright notice and this permission notice shall be included in
      13             :  * all copies or substantial portions of the 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 COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
      19             :  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
      20             :  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
      21             :  * OTHER DEALINGS IN THE SOFTWARE.
      22             :  *
      23             :  * Authors:
      24             :  * Rob Clark <robdclark@gmail.com>
      25             :  * Daniel Vetter <daniel.vetter@ffwll.ch>
      26             :  */
      27             : 
      28             : #include <dev/pci/drm/drmP.h>
      29             : #include <dev/pci/drm/drm_atomic.h>
      30             : #include <dev/pci/drm/drm_plane_helper.h>
      31             : #include <dev/pci/drm/drm_crtc_helper.h>
      32             : #include <dev/pci/drm/drm_atomic_helper.h>
      33             : #ifdef __linux__
      34             : #include <linux/fence.h>
      35             : #endif
      36             : 
      37             : /**
      38             :  * DOC: overview
      39             :  *
      40             :  * This helper library provides implementations of check and commit functions on
      41             :  * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
      42             :  * also provides convenience implementations for the atomic state handling
      43             :  * callbacks for drivers which don't need to subclass the drm core structures to
      44             :  * add their own additional internal state.
      45             :  *
      46             :  * This library also provides default implementations for the check callback in
      47             :  * drm_atomic_helper_check() and for the commit callback with
      48             :  * drm_atomic_helper_commit(). But the individual stages and callbacks are
      49             :  * exposed to allow drivers to mix and match and e.g. use the plane helpers only
      50             :  * together with a driver private modeset implementation.
      51             :  *
      52             :  * This library also provides implementations for all the legacy driver
      53             :  * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
      54             :  * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
      55             :  * various functions to implement set_property callbacks. New drivers must not
      56             :  * implement these functions themselves but must use the provided helpers.
      57             :  */
      58             : static void
      59           0 : drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
      60             :                                 struct drm_plane_state *plane_state,
      61             :                                 struct drm_plane *plane)
      62             : {
      63             :         struct drm_crtc_state *crtc_state;
      64             : 
      65           0 :         if (plane->state->crtc) {
      66           0 :                 crtc_state = state->crtc_states[drm_crtc_index(plane->state->crtc)];
      67             : 
      68           0 :                 if (WARN_ON(!crtc_state))
      69           0 :                         return;
      70             : 
      71           0 :                 crtc_state->planes_changed = true;
      72           0 :         }
      73             : 
      74           0 :         if (plane_state->crtc) {
      75             :                 crtc_state =
      76           0 :                         state->crtc_states[drm_crtc_index(plane_state->crtc)];
      77             : 
      78           0 :                 if (WARN_ON(!crtc_state))
      79           0 :                         return;
      80             : 
      81           0 :                 crtc_state->planes_changed = true;
      82           0 :         }
      83           0 : }
      84             : 
      85             : static struct drm_crtc *
      86           0 : get_current_crtc_for_encoder(struct drm_device *dev,
      87             :                              struct drm_encoder *encoder)
      88             : {
      89           0 :         struct drm_mode_config *config = &dev->mode_config;
      90             :         struct drm_connector *connector;
      91             : 
      92           0 :         WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
      93             : 
      94           0 :         drm_for_each_connector(connector, dev) {
      95           0 :                 if (connector->state->best_encoder != encoder)
      96             :                         continue;
      97             : 
      98           0 :                 return connector->state->crtc;
      99             :         }
     100             : 
     101           0 :         return NULL;
     102           0 : }
     103             : 
     104             : static int
     105           0 : steal_encoder(struct drm_atomic_state *state,
     106             :               struct drm_encoder *encoder,
     107             :               struct drm_crtc *encoder_crtc)
     108             : {
     109           0 :         struct drm_mode_config *config = &state->dev->mode_config;
     110             :         struct drm_crtc_state *crtc_state;
     111             :         struct drm_connector *connector;
     112             :         struct drm_connector_state *connector_state;
     113             : 
     114             :         /*
     115             :          * We can only steal an encoder coming from a connector, which means we
     116             :          * must already hold the connection_mutex.
     117             :          */
     118           0 :         WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
     119             : 
     120             :         DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
     121             :                          encoder->base.id, encoder->name,
     122             :                          encoder_crtc->base.id);
     123             : 
     124           0 :         crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
     125           0 :         if (IS_ERR(crtc_state))
     126           0 :                 return PTR_ERR(crtc_state);
     127             : 
     128           0 :         crtc_state->connectors_changed = true;
     129             : 
     130           0 :         list_for_each_entry(connector, &config->connector_list, head) {
     131           0 :                 if (connector->state->best_encoder != encoder)
     132             :                         continue;
     133             : 
     134             :                 DRM_DEBUG_ATOMIC("Stealing encoder from [CONNECTOR:%d:%s]\n",
     135             :                                  connector->base.id,
     136             :                                  connector->name);
     137             : 
     138           0 :                 connector_state = drm_atomic_get_connector_state(state,
     139             :                                                                  connector);
     140           0 :                 if (IS_ERR(connector_state))
     141           0 :                         return PTR_ERR(connector_state);
     142             : 
     143           0 :                 connector_state->best_encoder = NULL;
     144           0 :         }
     145             : 
     146           0 :         return 0;
     147           0 : }
     148             : 
     149             : static int
     150           0 : update_connector_routing(struct drm_atomic_state *state, int conn_idx)
     151             : {
     152             :         const struct drm_connector_helper_funcs *funcs;
     153             :         struct drm_encoder *new_encoder;
     154             :         struct drm_crtc *encoder_crtc;
     155             :         struct drm_connector *connector;
     156             :         struct drm_connector_state *connector_state;
     157             :         struct drm_crtc_state *crtc_state;
     158             :         int idx, ret;
     159             : 
     160           0 :         connector = state->connectors[conn_idx];
     161           0 :         connector_state = state->connector_states[conn_idx];
     162             : 
     163           0 :         if (!connector)
     164           0 :                 return 0;
     165             : 
     166             :         DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
     167             :                          connector->base.id,
     168             :                          connector->name);
     169             : 
     170           0 :         if (connector->state->crtc != connector_state->crtc) {
     171           0 :                 if (connector->state->crtc) {
     172           0 :                         idx = drm_crtc_index(connector->state->crtc);
     173             : 
     174           0 :                         crtc_state = state->crtc_states[idx];
     175           0 :                         crtc_state->connectors_changed = true;
     176           0 :                 }
     177             : 
     178           0 :                 if (connector_state->crtc) {
     179           0 :                         idx = drm_crtc_index(connector_state->crtc);
     180             : 
     181           0 :                         crtc_state = state->crtc_states[idx];
     182           0 :                         crtc_state->connectors_changed = true;
     183           0 :                 }
     184             :         }
     185             : 
     186           0 :         if (!connector_state->crtc) {
     187             :                 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
     188             :                                 connector->base.id,
     189             :                                 connector->name);
     190             : 
     191           0 :                 connector_state->best_encoder = NULL;
     192             : 
     193           0 :                 return 0;
     194             :         }
     195             : 
     196           0 :         funcs = connector->helper_private;
     197             : 
     198           0 :         if (funcs->atomic_best_encoder)
     199           0 :                 new_encoder = funcs->atomic_best_encoder(connector,
     200             :                                                          connector_state);
     201             :         else
     202           0 :                 new_encoder = funcs->best_encoder(connector);
     203             : 
     204           0 :         if (!new_encoder) {
     205             :                 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
     206             :                                  connector->base.id,
     207             :                                  connector->name);
     208           0 :                 return -EINVAL;
     209             :         }
     210             : 
     211           0 :         if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
     212             :                 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
     213             :                                  new_encoder->base.id,
     214             :                                  new_encoder->name,
     215             :                                  connector_state->crtc->base.id);
     216           0 :                 return -EINVAL;
     217             :         }
     218             : 
     219           0 :         if (new_encoder == connector_state->best_encoder) {
     220             :                 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
     221             :                                  connector->base.id,
     222             :                                  connector->name,
     223             :                                  new_encoder->base.id,
     224             :                                  new_encoder->name,
     225             :                                  connector_state->crtc->base.id);
     226             : 
     227           0 :                 return 0;
     228             :         }
     229             : 
     230           0 :         encoder_crtc = get_current_crtc_for_encoder(state->dev,
     231             :                                                     new_encoder);
     232             : 
     233           0 :         if (encoder_crtc) {
     234           0 :                 ret = steal_encoder(state, new_encoder, encoder_crtc);
     235           0 :                 if (ret) {
     236             :                         DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
     237             :                                          connector->base.id,
     238             :                                          connector->name);
     239           0 :                         return ret;
     240             :                 }
     241             :         }
     242             : 
     243           0 :         if (WARN_ON(!connector_state->crtc))
     244           0 :                 return -EINVAL;
     245             : 
     246           0 :         connector_state->best_encoder = new_encoder;
     247           0 :         idx = drm_crtc_index(connector_state->crtc);
     248             : 
     249           0 :         crtc_state = state->crtc_states[idx];
     250           0 :         crtc_state->connectors_changed = true;
     251             : 
     252             :         DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
     253             :                          connector->base.id,
     254             :                          connector->name,
     255             :                          new_encoder->base.id,
     256             :                          new_encoder->name,
     257             :                          connector_state->crtc->base.id);
     258             : 
     259           0 :         return 0;
     260           0 : }
     261             : 
     262             : static int
     263           0 : mode_fixup(struct drm_atomic_state *state)
     264             : {
     265             :         struct drm_crtc *crtc;
     266             :         struct drm_crtc_state *crtc_state;
     267             :         struct drm_connector *connector;
     268             :         struct drm_connector_state *conn_state;
     269             :         int i;
     270             :         int ret;
     271             : 
     272           0 :         for_each_crtc_in_state(state, crtc, crtc_state, i) {
     273           0 :                 if (!crtc_state->mode_changed &&
     274           0 :                     !crtc_state->connectors_changed)
     275             :                         continue;
     276             : 
     277           0 :                 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
     278           0 :         }
     279             : 
     280           0 :         for_each_connector_in_state(state, connector, conn_state, i) {
     281             :                 const struct drm_encoder_helper_funcs *funcs;
     282             :                 struct drm_encoder *encoder;
     283             : 
     284           0 :                 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
     285             : 
     286           0 :                 if (!conn_state->crtc || !conn_state->best_encoder)
     287           0 :                         continue;
     288             : 
     289             :                 crtc_state =
     290           0 :                         state->crtc_states[drm_crtc_index(conn_state->crtc)];
     291             : 
     292             :                 /*
     293             :                  * Each encoder has at most one connector (since we always steal
     294             :                  * it away), so we won't call ->mode_fixup twice.
     295             :                  */
     296           0 :                 encoder = conn_state->best_encoder;
     297           0 :                 funcs = encoder->helper_private;
     298           0 :                 if (!funcs)
     299           0 :                         continue;
     300             : 
     301           0 :                 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
     302           0 :                                 &crtc_state->adjusted_mode);
     303           0 :                 if (!ret) {
     304             :                         DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
     305           0 :                         return -EINVAL;
     306             :                 }
     307             : 
     308           0 :                 if (funcs->atomic_check) {
     309           0 :                         ret = funcs->atomic_check(encoder, crtc_state,
     310             :                                                   conn_state);
     311           0 :                         if (ret) {
     312             :                                 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
     313             :                                                  encoder->base.id, encoder->name);
     314           0 :                                 return ret;
     315             :                         }
     316           0 :                 } else if (funcs->mode_fixup) {
     317           0 :                         ret = funcs->mode_fixup(encoder, &crtc_state->mode,
     318             :                                                 &crtc_state->adjusted_mode);
     319           0 :                         if (!ret) {
     320             :                                 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
     321             :                                                  encoder->base.id, encoder->name);
     322           0 :                                 return -EINVAL;
     323             :                         }
     324             :                 }
     325           0 :         }
     326             : 
     327           0 :         for_each_crtc_in_state(state, crtc, crtc_state, i) {
     328             :                 const struct drm_crtc_helper_funcs *funcs;
     329             : 
     330           0 :                 if (!crtc_state->mode_changed &&
     331           0 :                     !crtc_state->connectors_changed)
     332           0 :                         continue;
     333             : 
     334           0 :                 funcs = crtc->helper_private;
     335           0 :                 if (!funcs->mode_fixup)
     336           0 :                         continue;
     337             : 
     338           0 :                 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
     339           0 :                                         &crtc_state->adjusted_mode);
     340           0 :                 if (!ret) {
     341             :                         DRM_DEBUG_ATOMIC("[CRTC:%d] fixup failed\n",
     342             :                                          crtc->base.id);
     343           0 :                         return -EINVAL;
     344             :                 }
     345           0 :         }
     346             : 
     347           0 :         return 0;
     348           0 : }
     349             : 
     350             : /**
     351             :  * drm_atomic_helper_check_modeset - validate state object for modeset changes
     352             :  * @dev: DRM device
     353             :  * @state: the driver state object
     354             :  *
     355             :  * Check the state object to see if the requested state is physically possible.
     356             :  * This does all the crtc and connector related computations for an atomic
     357             :  * update and adds any additional connectors needed for full modesets and calls
     358             :  * down into ->mode_fixup functions of the driver backend.
     359             :  *
     360             :  * crtc_state->mode_changed is set when the input mode is changed.
     361             :  * crtc_state->connectors_changed is set when a connector is added or
     362             :  * removed from the crtc.
     363             :  * crtc_state->active_changed is set when crtc_state->active changes,
     364             :  * which is used for dpms.
     365             :  *
     366             :  * IMPORTANT:
     367             :  *
     368             :  * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
     369             :  * plane update can't be done without a full modeset) _must_ call this function
     370             :  * afterwards after that change. It is permitted to call this function multiple
     371             :  * times for the same update, e.g. when the ->atomic_check functions depend upon
     372             :  * the adjusted dotclock for fifo space allocation and watermark computation.
     373             :  *
     374             :  * RETURNS
     375             :  * Zero for success or -errno
     376             :  */
     377             : int
     378           0 : drm_atomic_helper_check_modeset(struct drm_device *dev,
     379             :                                 struct drm_atomic_state *state)
     380             : {
     381             :         struct drm_crtc *crtc;
     382             :         struct drm_crtc_state *crtc_state;
     383             :         struct drm_connector *connector;
     384             :         struct drm_connector_state *connector_state;
     385             :         int i, ret;
     386             : 
     387           0 :         for_each_crtc_in_state(state, crtc, crtc_state, i) {
     388           0 :                 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
     389             :                         DRM_DEBUG_ATOMIC("[CRTC:%d] mode changed\n",
     390             :                                          crtc->base.id);
     391           0 :                         crtc_state->mode_changed = true;
     392           0 :                 }
     393             : 
     394           0 :                 if (crtc->state->enable != crtc_state->enable) {
     395             :                         DRM_DEBUG_ATOMIC("[CRTC:%d] enable changed\n",
     396             :                                          crtc->base.id);
     397             : 
     398             :                         /*
     399             :                          * For clarity this assignment is done here, but
     400             :                          * enable == 0 is only true when there are no
     401             :                          * connectors and a NULL mode.
     402             :                          *
     403             :                          * The other way around is true as well. enable != 0
     404             :                          * iff connectors are attached and a mode is set.
     405             :                          */
     406           0 :                         crtc_state->mode_changed = true;
     407           0 :                         crtc_state->connectors_changed = true;
     408           0 :                 }
     409             :         }
     410             : 
     411           0 :         for_each_connector_in_state(state, connector, connector_state, i) {
     412             :                 /*
     413             :                  * This only sets crtc->mode_changed for routing changes,
     414             :                  * drivers must set crtc->mode_changed themselves when connector
     415             :                  * properties need to be updated.
     416             :                  */
     417           0 :                 ret = update_connector_routing(state, i);
     418           0 :                 if (ret)
     419           0 :                         return ret;
     420             :         }
     421             : 
     422             :         /*
     423             :          * After all the routing has been prepared we need to add in any
     424             :          * connector which is itself unchanged, but who's crtc changes it's
     425             :          * configuration. This must be done before calling mode_fixup in case a
     426             :          * crtc only changed its mode but has the same set of connectors.
     427             :          */
     428           0 :         for_each_crtc_in_state(state, crtc, crtc_state, i) {
     429             :                 int num_connectors;
     430             : 
     431             :                 /*
     432             :                  * We must set ->active_changed after walking connectors for
     433             :                  * otherwise an update that only changes active would result in
     434             :                  * a full modeset because update_connector_routing force that.
     435             :                  */
     436           0 :                 if (crtc->state->active != crtc_state->active) {
     437             :                         DRM_DEBUG_ATOMIC("[CRTC:%d] active changed\n",
     438             :                                          crtc->base.id);
     439           0 :                         crtc_state->active_changed = true;
     440           0 :                 }
     441             : 
     442           0 :                 if (!drm_atomic_crtc_needs_modeset(crtc_state))
     443           0 :                         continue;
     444             : 
     445             :                 DRM_DEBUG_ATOMIC("[CRTC:%d] needs all connectors, enable: %c, active: %c\n",
     446             :                                  crtc->base.id,
     447             :                                  crtc_state->enable ? 'y' : 'n',
     448             :                               crtc_state->active ? 'y' : 'n');
     449             : 
     450           0 :                 ret = drm_atomic_add_affected_connectors(state, crtc);
     451           0 :                 if (ret != 0)
     452           0 :                         return ret;
     453             : 
     454           0 :                 ret = drm_atomic_add_affected_planes(state, crtc);
     455           0 :                 if (ret != 0)
     456           0 :                         return ret;
     457             : 
     458           0 :                 num_connectors = drm_atomic_connectors_for_crtc(state,
     459             :                                                                 crtc);
     460             : 
     461           0 :                 if (crtc_state->enable != !!num_connectors) {
     462             :                         DRM_DEBUG_ATOMIC("[CRTC:%d] enabled/connectors mismatch\n",
     463             :                                          crtc->base.id);
     464             : 
     465           0 :                         return -EINVAL;
     466             :                 }
     467           0 :         }
     468             : 
     469           0 :         return mode_fixup(state);
     470           0 : }
     471             : EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
     472             : 
     473             : /**
     474             :  * drm_atomic_helper_check_planes - validate state object for planes changes
     475             :  * @dev: DRM device
     476             :  * @state: the driver state object
     477             :  *
     478             :  * Check the state object to see if the requested state is physically possible.
     479             :  * This does all the plane update related checks using by calling into the
     480             :  * ->atomic_check hooks provided by the driver.
     481             :  *
     482             :  * It also sets crtc_state->planes_changed to indicate that a crtc has
     483             :  * updated planes.
     484             :  *
     485             :  * RETURNS
     486             :  * Zero for success or -errno
     487             :  */
     488             : int
     489           0 : drm_atomic_helper_check_planes(struct drm_device *dev,
     490             :                                struct drm_atomic_state *state)
     491             : {
     492             :         struct drm_crtc *crtc;
     493             :         struct drm_crtc_state *crtc_state;
     494             :         struct drm_plane *plane;
     495             :         struct drm_plane_state *plane_state;
     496             :         int i, ret = 0;
     497             : 
     498           0 :         for_each_plane_in_state(state, plane, plane_state, i) {
     499             :                 const struct drm_plane_helper_funcs *funcs;
     500             : 
     501           0 :                 funcs = plane->helper_private;
     502             : 
     503           0 :                 drm_atomic_helper_plane_changed(state, plane_state, plane);
     504             : 
     505           0 :                 if (!funcs || !funcs->atomic_check)
     506           0 :                         continue;
     507             : 
     508           0 :                 ret = funcs->atomic_check(plane, plane_state);
     509           0 :                 if (ret) {
     510             :                         DRM_DEBUG_ATOMIC("[PLANE:%d] atomic driver check failed\n",
     511             :                                          plane->base.id);
     512           0 :                         return ret;
     513             :                 }
     514           0 :         }
     515             : 
     516           0 :         for_each_crtc_in_state(state, crtc, crtc_state, i) {
     517             :                 const struct drm_crtc_helper_funcs *funcs;
     518             : 
     519           0 :                 funcs = crtc->helper_private;
     520             : 
     521           0 :                 if (!funcs || !funcs->atomic_check)
     522           0 :                         continue;
     523             : 
     524           0 :                 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
     525           0 :                 if (ret) {
     526             :                         DRM_DEBUG_ATOMIC("[CRTC:%d] atomic driver check failed\n",
     527             :                                          crtc->base.id);
     528           0 :                         return ret;
     529             :                 }
     530           0 :         }
     531             : 
     532           0 :         return ret;
     533           0 : }
     534             : EXPORT_SYMBOL(drm_atomic_helper_check_planes);
     535             : 
     536             : /**
     537             :  * drm_atomic_helper_check - validate state object
     538             :  * @dev: DRM device
     539             :  * @state: the driver state object
     540             :  *
     541             :  * Check the state object to see if the requested state is physically possible.
     542             :  * Only crtcs and planes have check callbacks, so for any additional (global)
     543             :  * checking that a driver needs it can simply wrap that around this function.
     544             :  * Drivers without such needs can directly use this as their ->atomic_check()
     545             :  * callback.
     546             :  *
     547             :  * This just wraps the two parts of the state checking for planes and modeset
     548             :  * state in the default order: First it calls drm_atomic_helper_check_modeset()
     549             :  * and then drm_atomic_helper_check_planes(). The assumption is that the
     550             :  * ->atomic_check functions depend upon an updated adjusted_mode.clock to
     551             :  * e.g. properly compute watermarks.
     552             :  *
     553             :  * RETURNS
     554             :  * Zero for success or -errno
     555             :  */
     556           0 : int drm_atomic_helper_check(struct drm_device *dev,
     557             :                             struct drm_atomic_state *state)
     558             : {
     559             :         int ret;
     560             : 
     561           0 :         ret = drm_atomic_helper_check_modeset(dev, state);
     562           0 :         if (ret)
     563           0 :                 return ret;
     564             : 
     565           0 :         ret = drm_atomic_helper_check_planes(dev, state);
     566             :         if (ret)
     567           0 :                 return ret;
     568             : 
     569             :         return ret;
     570           0 : }
     571             : EXPORT_SYMBOL(drm_atomic_helper_check);
     572             : 
     573             : static void
     574           0 : disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
     575             : {
     576             :         struct drm_connector *connector;
     577             :         struct drm_connector_state *old_conn_state;
     578             :         struct drm_crtc *crtc;
     579             :         struct drm_crtc_state *old_crtc_state;
     580             :         int i;
     581             : 
     582           0 :         for_each_connector_in_state(old_state, connector, old_conn_state, i) {
     583             :                 const struct drm_encoder_helper_funcs *funcs;
     584             :                 struct drm_encoder *encoder;
     585             :                 struct drm_crtc_state *old_crtc_state;
     586             : 
     587             :                 /* Shut down everything that's in the changeset and currently
     588             :                  * still on. So need to check the old, saved state. */
     589           0 :                 if (!old_conn_state->crtc)
     590           0 :                         continue;
     591             : 
     592           0 :                 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
     593             : 
     594           0 :                 if (!old_crtc_state->active ||
     595           0 :                     !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
     596           0 :                         continue;
     597             : 
     598           0 :                 encoder = old_conn_state->best_encoder;
     599             : 
     600             :                 /* We shouldn't get this far if we didn't previously have
     601             :                  * an encoder.. but WARN_ON() rather than explode.
     602             :                  */
     603           0 :                 if (WARN_ON(!encoder))
     604           0 :                         continue;
     605             : 
     606           0 :                 funcs = encoder->helper_private;
     607             : 
     608             :                 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
     609             :                                  encoder->base.id, encoder->name);
     610             : 
     611             :                 /*
     612             :                  * Each encoder has at most one connector (since we always steal
     613             :                  * it away), so we won't call disable hooks twice.
     614             :                  */
     615           0 :                 drm_bridge_disable(encoder->bridge);
     616             : 
     617             :                 /* Right function depends upon target state. */
     618           0 :                 if (connector->state->crtc && funcs->prepare)
     619           0 :                         funcs->prepare(encoder);
     620           0 :                 else if (funcs->disable)
     621           0 :                         funcs->disable(encoder);
     622             :                 else
     623           0 :                         funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
     624             : 
     625           0 :                 drm_bridge_post_disable(encoder->bridge);
     626           0 :         }
     627             : 
     628           0 :         for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
     629             :                 const struct drm_crtc_helper_funcs *funcs;
     630             : 
     631             :                 /* Shut down everything that needs a full modeset. */
     632           0 :                 if (!drm_atomic_crtc_needs_modeset(crtc->state))
     633           0 :                         continue;
     634             : 
     635           0 :                 if (!old_crtc_state->active)
     636           0 :                         continue;
     637             : 
     638           0 :                 funcs = crtc->helper_private;
     639             : 
     640             :                 DRM_DEBUG_ATOMIC("disabling [CRTC:%d]\n",
     641             :                                  crtc->base.id);
     642             : 
     643             : 
     644             :                 /* Right function depends upon target state. */
     645           0 :                 if (crtc->state->enable && funcs->prepare)
     646           0 :                         funcs->prepare(crtc);
     647           0 :                 else if (funcs->disable)
     648           0 :                         funcs->disable(crtc);
     649             :                 else
     650           0 :                         funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
     651           0 :         }
     652           0 : }
     653             : 
     654             : /**
     655             :  * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
     656             :  * @dev: DRM device
     657             :  * @old_state: atomic state object with old state structures
     658             :  *
     659             :  * This function updates all the various legacy modeset state pointers in
     660             :  * connectors, encoders and crtcs. It also updates the timestamping constants
     661             :  * used for precise vblank timestamps by calling
     662             :  * drm_calc_timestamping_constants().
     663             :  *
     664             :  * Drivers can use this for building their own atomic commit if they don't have
     665             :  * a pure helper-based modeset implementation.
     666             :  */
     667             : void
     668           0 : drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
     669             :                                               struct drm_atomic_state *old_state)
     670             : {
     671             :         struct drm_connector *connector;
     672             :         struct drm_connector_state *old_conn_state;
     673             :         struct drm_crtc *crtc;
     674             :         struct drm_crtc_state *old_crtc_state;
     675             :         int i;
     676             : 
     677             :         /* clear out existing links and update dpms */
     678           0 :         for_each_connector_in_state(old_state, connector, old_conn_state, i) {
     679           0 :                 if (connector->encoder) {
     680           0 :                         WARN_ON(!connector->encoder->crtc);
     681             : 
     682           0 :                         connector->encoder->crtc = NULL;
     683           0 :                         connector->encoder = NULL;
     684           0 :                 }
     685             : 
     686           0 :                 crtc = connector->state->crtc;
     687           0 :                 if ((!crtc && old_conn_state->crtc) ||
     688           0 :                     (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
     689             :                         struct drm_property *dpms_prop =
     690           0 :                                 dev->mode_config.dpms_property;
     691             :                         int mode = DRM_MODE_DPMS_OFF;
     692             : 
     693           0 :                         if (crtc && crtc->state->active)
     694           0 :                                 mode = DRM_MODE_DPMS_ON;
     695             : 
     696           0 :                         connector->dpms = mode;
     697           0 :                         drm_object_property_set_value(&connector->base,
     698           0 :                                                       dpms_prop, mode);
     699           0 :                 }
     700             :         }
     701             : 
     702             :         /* set new links */
     703           0 :         for_each_connector_in_state(old_state, connector, old_conn_state, i) {
     704           0 :                 if (!connector->state->crtc)
     705             :                         continue;
     706             : 
     707           0 :                 if (WARN_ON(!connector->state->best_encoder))
     708             :                         continue;
     709             : 
     710           0 :                 connector->encoder = connector->state->best_encoder;
     711           0 :                 connector->encoder->crtc = connector->state->crtc;
     712           0 :         }
     713             : 
     714             :         /* set legacy state in the crtc structure */
     715           0 :         for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
     716           0 :                 struct drm_plane *primary = crtc->primary;
     717             : 
     718           0 :                 crtc->mode = crtc->state->mode;
     719           0 :                 crtc->enabled = crtc->state->enable;
     720             : 
     721           0 :                 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
     722           0 :                     primary->state->crtc == crtc) {
     723           0 :                         crtc->x = primary->state->src_x >> 16;
     724           0 :                         crtc->y = primary->state->src_y >> 16;
     725           0 :                 }
     726             : 
     727           0 :                 if (crtc->state->enable)
     728           0 :                         drm_calc_timestamping_constants(crtc,
     729           0 :                                                         &crtc->state->adjusted_mode);
     730           0 :         }
     731           0 : }
     732             : EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
     733             : 
     734             : static void
     735           0 : crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
     736             : {
     737             :         struct drm_crtc *crtc;
     738             :         struct drm_crtc_state *old_crtc_state;
     739             :         struct drm_connector *connector;
     740             :         struct drm_connector_state *old_conn_state;
     741             :         int i;
     742             : 
     743           0 :         for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
     744             :                 const struct drm_crtc_helper_funcs *funcs;
     745             : 
     746           0 :                 if (!crtc->state->mode_changed)
     747           0 :                         continue;
     748             : 
     749           0 :                 funcs = crtc->helper_private;
     750             : 
     751           0 :                 if (crtc->state->enable && funcs->mode_set_nofb) {
     752             :                         DRM_DEBUG_ATOMIC("modeset on [CRTC:%d]\n",
     753             :                                          crtc->base.id);
     754             : 
     755           0 :                         funcs->mode_set_nofb(crtc);
     756           0 :                 }
     757           0 :         }
     758             : 
     759           0 :         for_each_connector_in_state(old_state, connector, old_conn_state, i) {
     760             :                 const struct drm_encoder_helper_funcs *funcs;
     761             :                 struct drm_crtc_state *new_crtc_state;
     762             :                 struct drm_encoder *encoder;
     763             :                 struct drm_display_mode *mode, *adjusted_mode;
     764             : 
     765           0 :                 if (!connector->state->best_encoder)
     766           0 :                         continue;
     767             : 
     768             :                 encoder = connector->state->best_encoder;
     769           0 :                 funcs = encoder->helper_private;
     770           0 :                 new_crtc_state = connector->state->crtc->state;
     771           0 :                 mode = &new_crtc_state->mode;
     772           0 :                 adjusted_mode = &new_crtc_state->adjusted_mode;
     773             : 
     774           0 :                 if (!new_crtc_state->mode_changed)
     775           0 :                         continue;
     776             : 
     777             :                 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
     778             :                                  encoder->base.id, encoder->name);
     779             : 
     780             :                 /*
     781             :                  * Each encoder has at most one connector (since we always steal
     782             :                  * it away), so we won't call mode_set hooks twice.
     783             :                  */
     784           0 :                 if (funcs->mode_set)
     785           0 :                         funcs->mode_set(encoder, mode, adjusted_mode);
     786             : 
     787           0 :                 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
     788           0 :         }
     789           0 : }
     790             : 
     791             : /**
     792             :  * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
     793             :  * @dev: DRM device
     794             :  * @old_state: atomic state object with old state structures
     795             :  *
     796             :  * This function shuts down all the outputs that need to be shut down and
     797             :  * prepares them (if required) with the new mode.
     798             :  *
     799             :  * For compatibility with legacy crtc helpers this should be called before
     800             :  * drm_atomic_helper_commit_planes(), which is what the default commit function
     801             :  * does. But drivers with different needs can group the modeset commits together
     802             :  * and do the plane commits at the end. This is useful for drivers doing runtime
     803             :  * PM since planes updates then only happen when the CRTC is actually enabled.
     804             :  */
     805           0 : void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
     806             :                                                struct drm_atomic_state *old_state)
     807             : {
     808           0 :         disable_outputs(dev, old_state);
     809             : 
     810           0 :         drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
     811             : 
     812           0 :         crtc_set_mode(dev, old_state);
     813           0 : }
     814             : EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
     815             : 
     816             : /**
     817             :  * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
     818             :  * @dev: DRM device
     819             :  * @old_state: atomic state object with old state structures
     820             :  *
     821             :  * This function enables all the outputs with the new configuration which had to
     822             :  * be turned off for the update.
     823             :  *
     824             :  * For compatibility with legacy crtc helpers this should be called after
     825             :  * drm_atomic_helper_commit_planes(), which is what the default commit function
     826             :  * does. But drivers with different needs can group the modeset commits together
     827             :  * and do the plane commits at the end. This is useful for drivers doing runtime
     828             :  * PM since planes updates then only happen when the CRTC is actually enabled.
     829             :  */
     830           0 : void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
     831             :                                               struct drm_atomic_state *old_state)
     832             : {
     833             :         struct drm_crtc *crtc;
     834             :         struct drm_crtc_state *old_crtc_state;
     835             :         struct drm_connector *connector;
     836             :         struct drm_connector_state *old_conn_state;
     837             :         int i;
     838             : 
     839           0 :         for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
     840             :                 const struct drm_crtc_helper_funcs *funcs;
     841             : 
     842             :                 /* Need to filter out CRTCs where only planes change. */
     843           0 :                 if (!drm_atomic_crtc_needs_modeset(crtc->state))
     844           0 :                         continue;
     845             : 
     846           0 :                 if (!crtc->state->active)
     847           0 :                         continue;
     848             : 
     849           0 :                 funcs = crtc->helper_private;
     850             : 
     851           0 :                 if (crtc->state->enable) {
     852             :                         DRM_DEBUG_ATOMIC("enabling [CRTC:%d]\n",
     853             :                                          crtc->base.id);
     854             : 
     855           0 :                         if (funcs->enable)
     856           0 :                                 funcs->enable(crtc);
     857             :                         else
     858           0 :                                 funcs->commit(crtc);
     859             :                 }
     860           0 :         }
     861             : 
     862           0 :         for_each_connector_in_state(old_state, connector, old_conn_state, i) {
     863             :                 const struct drm_encoder_helper_funcs *funcs;
     864             :                 struct drm_encoder *encoder;
     865             : 
     866           0 :                 if (!connector->state->best_encoder)
     867           0 :                         continue;
     868             : 
     869           0 :                 if (!connector->state->crtc->state->active ||
     870           0 :                     !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
     871           0 :                         continue;
     872             : 
     873           0 :                 encoder = connector->state->best_encoder;
     874           0 :                 funcs = encoder->helper_private;
     875             : 
     876             :                 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
     877             :                                  encoder->base.id, encoder->name);
     878             : 
     879             :                 /*
     880             :                  * Each encoder has at most one connector (since we always steal
     881             :                  * it away), so we won't call enable hooks twice.
     882             :                  */
     883           0 :                 drm_bridge_pre_enable(encoder->bridge);
     884             : 
     885           0 :                 if (funcs->enable)
     886           0 :                         funcs->enable(encoder);
     887             :                 else
     888           0 :                         funcs->commit(encoder);
     889             : 
     890           0 :                 drm_bridge_enable(encoder->bridge);
     891           0 :         }
     892           0 : }
     893             : EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
     894             : 
     895           0 : static void wait_for_fences(struct drm_device *dev,
     896             :                             struct drm_atomic_state *state)
     897             : {
     898             :         struct drm_plane *plane;
     899             :         struct drm_plane_state *plane_state;
     900             :         int i;
     901             : 
     902           0 :         for_each_plane_in_state(state, plane, plane_state, i) {
     903           0 :                 if (!plane->state->fence)
     904             :                         continue;
     905             : 
     906           0 :                 WARN_ON(!plane->state->fb);
     907             : 
     908           0 :                 fence_wait(plane->state->fence, false);
     909           0 :                 fence_put(plane->state->fence);
     910           0 :                 plane->state->fence = NULL;
     911           0 :         }
     912           0 : }
     913             : 
     914           0 : static bool framebuffer_changed(struct drm_device *dev,
     915             :                                 struct drm_atomic_state *old_state,
     916             :                                 struct drm_crtc *crtc)
     917             : {
     918             :         struct drm_plane *plane;
     919             :         struct drm_plane_state *old_plane_state;
     920             :         int i;
     921             : 
     922           0 :         for_each_plane_in_state(old_state, plane, old_plane_state, i) {
     923           0 :                 if (plane->state->crtc != crtc &&
     924           0 :                     old_plane_state->crtc != crtc)
     925             :                         continue;
     926             : 
     927           0 :                 if (plane->state->fb != old_plane_state->fb)
     928           0 :                         return true;
     929             :         }
     930             : 
     931           0 :         return false;
     932           0 : }
     933             : 
     934             : /**
     935             :  * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
     936             :  * @dev: DRM device
     937             :  * @old_state: atomic state object with old state structures
     938             :  *
     939             :  * Helper to, after atomic commit, wait for vblanks on all effected
     940             :  * crtcs (ie. before cleaning up old framebuffers using
     941             :  * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
     942             :  * framebuffers have actually changed to optimize for the legacy cursor and
     943             :  * plane update use-case.
     944             :  */
     945             : void
     946           0 : drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
     947             :                 struct drm_atomic_state *old_state)
     948             : {
     949             :         struct drm_crtc *crtc;
     950             :         struct drm_crtc_state *old_crtc_state;
     951             :         int i, ret;
     952             : 
     953           0 :         if (cold) {
     954           0 :                 delay(50000);
     955           0 :                 return;
     956             :         }
     957             : 
     958           0 :         for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
     959             :                 /* No one cares about the old state, so abuse it for tracking
     960             :                  * and store whether we hold a vblank reference (and should do a
     961             :                  * vblank wait) in the ->enable boolean. */
     962           0 :                 old_crtc_state->enable = false;
     963             : 
     964           0 :                 if (!crtc->state->enable)
     965             :                         continue;
     966             : 
     967             :                 /* Legacy cursor ioctls are completely unsynced, and userspace
     968             :                  * relies on that (by doing tons of cursor updates). */
     969           0 :                 if (old_state->legacy_cursor_update)
     970             :                         continue;
     971             : 
     972           0 :                 if (!framebuffer_changed(dev, old_state, crtc))
     973             :                         continue;
     974             : 
     975           0 :                 ret = drm_crtc_vblank_get(crtc);
     976           0 :                 if (ret != 0)
     977             :                         continue;
     978             : 
     979           0 :                 old_crtc_state->enable = true;
     980           0 :                 old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
     981           0 :         }
     982             : 
     983           0 :         for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
     984           0 :                 if (!old_crtc_state->enable)
     985             :                         continue;
     986             : 
     987           0 :                 ret = wait_event_timeout(dev->vblank[i].queue,
     988             :                                 old_crtc_state->last_vblank_count !=
     989             :                                         drm_crtc_vblank_count(crtc),
     990             :                                 msecs_to_jiffies(50));
     991             : 
     992           0 :                 drm_crtc_vblank_put(crtc);
     993           0 :         }
     994           0 : }
     995             : EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
     996             : 
     997             : /**
     998             :  * drm_atomic_helper_commit - commit validated state object
     999             :  * @dev: DRM device
    1000             :  * @state: the driver state object
    1001             :  * @async: asynchronous commit
    1002             :  *
    1003             :  * This function commits a with drm_atomic_helper_check() pre-validated state
    1004             :  * object. This can still fail when e.g. the framebuffer reservation fails. For
    1005             :  * now this doesn't implement asynchronous commits.
    1006             :  *
    1007             :  * Note that right now this function does not support async commits, and hence
    1008             :  * driver writers must implement their own version for now. Also note that the
    1009             :  * default ordering of how the various stages are called is to match the legacy
    1010             :  * modeset helper library closest. One peculiarity of that is that it doesn't
    1011             :  * mesh well with runtime PM at all.
    1012             :  *
    1013             :  * For drivers supporting runtime PM the recommended sequence is
    1014             :  *
    1015             :  *     drm_atomic_helper_commit_modeset_disables(dev, state);
    1016             :  *
    1017             :  *     drm_atomic_helper_commit_modeset_enables(dev, state);
    1018             :  *
    1019             :  *     drm_atomic_helper_commit_planes(dev, state, true);
    1020             :  *
    1021             :  * See the kerneldoc entries for these three functions for more details.
    1022             :  *
    1023             :  * RETURNS
    1024             :  * Zero for success or -errno.
    1025             :  */
    1026           0 : int drm_atomic_helper_commit(struct drm_device *dev,
    1027             :                              struct drm_atomic_state *state,
    1028             :                              bool async)
    1029             : {
    1030             :         int ret;
    1031             : 
    1032           0 :         if (async)
    1033           0 :                 return -EBUSY;
    1034             : 
    1035           0 :         ret = drm_atomic_helper_prepare_planes(dev, state);
    1036           0 :         if (ret)
    1037           0 :                 return ret;
    1038             : 
    1039             :         /*
    1040             :          * This is the point of no return - everything below never fails except
    1041             :          * when the hw goes bonghits. Which means we can commit the new state on
    1042             :          * the software side now.
    1043             :          */
    1044             : 
    1045           0 :         drm_atomic_helper_swap_state(dev, state);
    1046             : 
    1047             :         /*
    1048             :          * Everything below can be run asynchronously without the need to grab
    1049             :          * any modeset locks at all under one condition: It must be guaranteed
    1050             :          * that the asynchronous work has either been cancelled (if the driver
    1051             :          * supports it, which at least requires that the framebuffers get
    1052             :          * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
    1053             :          * before the new state gets committed on the software side with
    1054             :          * drm_atomic_helper_swap_state().
    1055             :          *
    1056             :          * This scheme allows new atomic state updates to be prepared and
    1057             :          * checked in parallel to the asynchronous completion of the previous
    1058             :          * update. Which is important since compositors need to figure out the
    1059             :          * composition of the next frame right after having submitted the
    1060             :          * current layout.
    1061             :          */
    1062             : 
    1063           0 :         wait_for_fences(dev, state);
    1064             : 
    1065           0 :         drm_atomic_helper_commit_modeset_disables(dev, state);
    1066             : 
    1067           0 :         drm_atomic_helper_commit_planes(dev, state, false);
    1068             : 
    1069           0 :         drm_atomic_helper_commit_modeset_enables(dev, state);
    1070             : 
    1071           0 :         drm_atomic_helper_wait_for_vblanks(dev, state);
    1072             : 
    1073           0 :         drm_atomic_helper_cleanup_planes(dev, state);
    1074             : 
    1075           0 :         drm_atomic_state_free(state);
    1076             : 
    1077           0 :         return 0;
    1078           0 : }
    1079             : EXPORT_SYMBOL(drm_atomic_helper_commit);
    1080             : 
    1081             : /**
    1082             :  * DOC: implementing async commit
    1083             :  *
    1084             :  * For now the atomic helpers don't support async commit directly. If there is
    1085             :  * real need it could be added though, using the dma-buf fence infrastructure
    1086             :  * for generic synchronization with outstanding rendering.
    1087             :  *
    1088             :  * For now drivers have to implement async commit themselves, with the following
    1089             :  * sequence being the recommended one:
    1090             :  *
    1091             :  * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
    1092             :  * which commit needs to call which can fail, so we want to run it first and
    1093             :  * synchronously.
    1094             :  *
    1095             :  * 2. Synchronize with any outstanding asynchronous commit worker threads which
    1096             :  * might be affected the new state update. This can be done by either cancelling
    1097             :  * or flushing the work items, depending upon whether the driver can deal with
    1098             :  * cancelled updates. Note that it is important to ensure that the framebuffer
    1099             :  * cleanup is still done when cancelling.
    1100             :  *
    1101             :  * For sufficient parallelism it is recommended to have a work item per crtc
    1102             :  * (for updates which don't touch global state) and a global one. Then we only
    1103             :  * need to synchronize with the crtc work items for changed crtcs and the global
    1104             :  * work item, which allows nice concurrent updates on disjoint sets of crtcs.
    1105             :  *
    1106             :  * 3. The software state is updated synchronously with
    1107             :  * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
    1108             :  * locks means concurrent callers never see inconsistent state. And doing this
    1109             :  * while it's guaranteed that no relevant async worker runs means that async
    1110             :  * workers do not need grab any locks. Actually they must not grab locks, for
    1111             :  * otherwise the work flushing will deadlock.
    1112             :  *
    1113             :  * 4. Schedule a work item to do all subsequent steps, using the split-out
    1114             :  * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
    1115             :  * then cleaning up the framebuffers after the old framebuffer is no longer
    1116             :  * being displayed.
    1117             :  */
    1118             : 
    1119             : /**
    1120             :  * drm_atomic_helper_prepare_planes - prepare plane resources before commit
    1121             :  * @dev: DRM device
    1122             :  * @state: atomic state object with new state structures
    1123             :  *
    1124             :  * This function prepares plane state, specifically framebuffers, for the new
    1125             :  * configuration. If any failure is encountered this function will call
    1126             :  * ->cleanup_fb on any already successfully prepared framebuffer.
    1127             :  *
    1128             :  * Returns:
    1129             :  * 0 on success, negative error code on failure.
    1130             :  */
    1131           0 : int drm_atomic_helper_prepare_planes(struct drm_device *dev,
    1132             :                                      struct drm_atomic_state *state)
    1133             : {
    1134           0 :         int nplanes = dev->mode_config.num_total_plane;
    1135             :         int ret, i;
    1136             : 
    1137           0 :         for (i = 0; i < nplanes; i++) {
    1138             :                 const struct drm_plane_helper_funcs *funcs;
    1139           0 :                 struct drm_plane *plane = state->planes[i];
    1140           0 :                 struct drm_plane_state *plane_state = state->plane_states[i];
    1141             : 
    1142           0 :                 if (!plane)
    1143           0 :                         continue;
    1144             : 
    1145           0 :                 funcs = plane->helper_private;
    1146             : 
    1147           0 :                 if (funcs->prepare_fb) {
    1148           0 :                         ret = funcs->prepare_fb(plane, plane_state);
    1149           0 :                         if (ret)
    1150           0 :                                 goto fail;
    1151             :                 }
    1152           0 :         }
    1153             : 
    1154           0 :         return 0;
    1155             : 
    1156             : fail:
    1157           0 :         for (i--; i >= 0; i--) {
    1158             :                 const struct drm_plane_helper_funcs *funcs;
    1159           0 :                 struct drm_plane *plane = state->planes[i];
    1160           0 :                 struct drm_plane_state *plane_state = state->plane_states[i];
    1161             : 
    1162           0 :                 if (!plane)
    1163           0 :                         continue;
    1164             : 
    1165           0 :                 funcs = plane->helper_private;
    1166             : 
    1167           0 :                 if (funcs->cleanup_fb)
    1168           0 :                         funcs->cleanup_fb(plane, plane_state);
    1169             : 
    1170           0 :         }
    1171             : 
    1172           0 :         return ret;
    1173           0 : }
    1174             : EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
    1175             : 
    1176           0 : bool plane_crtc_active(struct drm_plane_state *state)
    1177             : {
    1178           0 :         return state->crtc && state->crtc->state->active;
    1179             : }
    1180             : 
    1181             : /**
    1182             :  * drm_atomic_helper_commit_planes - commit plane state
    1183             :  * @dev: DRM device
    1184             :  * @old_state: atomic state object with old state structures
    1185             :  * @active_only: Only commit on active CRTC if set
    1186             :  *
    1187             :  * This function commits the new plane state using the plane and atomic helper
    1188             :  * functions for planes and crtcs. It assumes that the atomic state has already
    1189             :  * been pushed into the relevant object state pointers, since this step can no
    1190             :  * longer fail.
    1191             :  *
    1192             :  * It still requires the global state object @old_state to know which planes and
    1193             :  * crtcs need to be updated though.
    1194             :  *
    1195             :  * Note that this function does all plane updates across all CRTCs in one step.
    1196             :  * If the hardware can't support this approach look at
    1197             :  * drm_atomic_helper_commit_planes_on_crtc() instead.
    1198             :  *
    1199             :  * Plane parameters can be updated by applications while the associated CRTC is
    1200             :  * disabled. The DRM/KMS core will store the parameters in the plane state,
    1201             :  * which will be available to the driver when the CRTC is turned on. As a result
    1202             :  * most drivers don't need to be immediately notified of plane updates for a
    1203             :  * disabled CRTC.
    1204             :  *
    1205             :  * Unless otherwise needed, drivers are advised to set the @active_only
    1206             :  * parameters to true in order not to receive plane update notifications related
    1207             :  * to a disabled CRTC. This avoids the need to manually ignore plane updates in
    1208             :  * driver code when the driver and/or hardware can't or just don't need to deal
    1209             :  * with updates on disabled CRTCs, for example when supporting runtime PM.
    1210             :  *
    1211             :  * The drm_atomic_helper_commit() default implementation only sets @active_only
    1212             :  * to false to most closely match the behaviour of the legacy helpers. This should
    1213             :  * not be copied blindly by drivers.
    1214             :  */
    1215           0 : void drm_atomic_helper_commit_planes(struct drm_device *dev,
    1216             :                                      struct drm_atomic_state *old_state,
    1217             :                                      bool active_only)
    1218             : {
    1219             :         struct drm_crtc *crtc;
    1220             :         struct drm_crtc_state *old_crtc_state;
    1221             :         struct drm_plane *plane;
    1222             :         struct drm_plane_state *old_plane_state;
    1223             :         int i;
    1224             : 
    1225           0 :         for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
    1226             :                 const struct drm_crtc_helper_funcs *funcs;
    1227             : 
    1228           0 :                 funcs = crtc->helper_private;
    1229             : 
    1230           0 :                 if (!funcs || !funcs->atomic_begin)
    1231           0 :                         continue;
    1232             : 
    1233           0 :                 if (active_only && !crtc->state->active)
    1234           0 :                         continue;
    1235             : 
    1236           0 :                 funcs->atomic_begin(crtc, old_crtc_state);
    1237           0 :         }
    1238             : 
    1239           0 :         for_each_plane_in_state(old_state, plane, old_plane_state, i) {
    1240             :                 const struct drm_plane_helper_funcs *funcs;
    1241             :                 bool disabling;
    1242             : 
    1243           0 :                 funcs = plane->helper_private;
    1244             : 
    1245           0 :                 if (!funcs)
    1246           0 :                         continue;
    1247             : 
    1248           0 :                 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
    1249             : 
    1250           0 :                 if (active_only) {
    1251             :                         /*
    1252             :                          * Skip planes related to inactive CRTCs. If the plane
    1253             :                          * is enabled use the state of the current CRTC. If the
    1254             :                          * plane is being disabled use the state of the old
    1255             :                          * CRTC to avoid skipping planes being disabled on an
    1256             :                          * active CRTC.
    1257             :                          */
    1258           0 :                         if (!disabling && !plane_crtc_active(plane->state))
    1259           0 :                                 continue;
    1260           0 :                         if (disabling && !plane_crtc_active(old_plane_state))
    1261           0 :                                 continue;
    1262             :                 }
    1263             : 
    1264             :                 /*
    1265             :                  * Special-case disabling the plane if drivers support it.
    1266             :                  */
    1267           0 :                 if (disabling && funcs->atomic_disable)
    1268           0 :                         funcs->atomic_disable(plane, old_plane_state);
    1269           0 :                 else if (plane->state->crtc || disabling)
    1270           0 :                         funcs->atomic_update(plane, old_plane_state);
    1271           0 :         }
    1272             : 
    1273           0 :         for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
    1274             :                 const struct drm_crtc_helper_funcs *funcs;
    1275             : 
    1276           0 :                 funcs = crtc->helper_private;
    1277             : 
    1278           0 :                 if (!funcs || !funcs->atomic_flush)
    1279           0 :                         continue;
    1280             : 
    1281           0 :                 if (active_only && !crtc->state->active)
    1282           0 :                         continue;
    1283             : 
    1284           0 :                 funcs->atomic_flush(crtc, old_crtc_state);
    1285           0 :         }
    1286           0 : }
    1287             : EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
    1288             : 
    1289             : /**
    1290             :  * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
    1291             :  * @old_crtc_state: atomic state object with the old crtc state
    1292             :  *
    1293             :  * This function commits the new plane state using the plane and atomic helper
    1294             :  * functions for planes on the specific crtc. It assumes that the atomic state
    1295             :  * has already been pushed into the relevant object state pointers, since this
    1296             :  * step can no longer fail.
    1297             :  *
    1298             :  * This function is useful when plane updates should be done crtc-by-crtc
    1299             :  * instead of one global step like drm_atomic_helper_commit_planes() does.
    1300             :  *
    1301             :  * This function can only be savely used when planes are not allowed to move
    1302             :  * between different CRTCs because this function doesn't handle inter-CRTC
    1303             :  * depencies. Callers need to ensure that either no such depencies exist,
    1304             :  * resolve them through ordering of commit calls or through some other means.
    1305             :  */
    1306             : void
    1307           0 : drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
    1308             : {
    1309             :         const struct drm_crtc_helper_funcs *crtc_funcs;
    1310           0 :         struct drm_crtc *crtc = old_crtc_state->crtc;
    1311           0 :         struct drm_atomic_state *old_state = old_crtc_state->state;
    1312             :         struct drm_plane *plane;
    1313             :         unsigned plane_mask;
    1314             : 
    1315           0 :         plane_mask = old_crtc_state->plane_mask;
    1316           0 :         plane_mask |= crtc->state->plane_mask;
    1317             : 
    1318           0 :         crtc_funcs = crtc->helper_private;
    1319           0 :         if (crtc_funcs && crtc_funcs->atomic_begin)
    1320           0 :                 crtc_funcs->atomic_begin(crtc, old_crtc_state);
    1321             : 
    1322           0 :         drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
    1323             :                 struct drm_plane_state *old_plane_state =
    1324           0 :                         drm_atomic_get_existing_plane_state(old_state, plane);
    1325             :                 const struct drm_plane_helper_funcs *plane_funcs;
    1326             : 
    1327           0 :                 plane_funcs = plane->helper_private;
    1328             : 
    1329           0 :                 if (!old_plane_state || !plane_funcs)
    1330           0 :                         continue;
    1331             : 
    1332           0 :                 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
    1333             : 
    1334           0 :                 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
    1335           0 :                     plane_funcs->atomic_disable)
    1336           0 :                         plane_funcs->atomic_disable(plane, old_plane_state);
    1337           0 :                 else if (plane->state->crtc ||
    1338           0 :                          drm_atomic_plane_disabling(plane, old_plane_state))
    1339           0 :                         plane_funcs->atomic_update(plane, old_plane_state);
    1340           0 :         }
    1341             : 
    1342           0 :         if (crtc_funcs && crtc_funcs->atomic_flush)
    1343           0 :                 crtc_funcs->atomic_flush(crtc, old_crtc_state);
    1344           0 : }
    1345             : EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
    1346             : 
    1347             : /**
    1348             :  * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
    1349             :  * @dev: DRM device
    1350             :  * @old_state: atomic state object with old state structures
    1351             :  *
    1352             :  * This function cleans up plane state, specifically framebuffers, from the old
    1353             :  * configuration. Hence the old configuration must be perserved in @old_state to
    1354             :  * be able to call this function.
    1355             :  *
    1356             :  * This function must also be called on the new state when the atomic update
    1357             :  * fails at any point after calling drm_atomic_helper_prepare_planes().
    1358             :  */
    1359           0 : void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
    1360             :                                       struct drm_atomic_state *old_state)
    1361             : {
    1362             :         struct drm_plane *plane;
    1363             :         struct drm_plane_state *plane_state;
    1364             :         int i;
    1365             : 
    1366           0 :         for_each_plane_in_state(old_state, plane, plane_state, i) {
    1367             :                 const struct drm_plane_helper_funcs *funcs;
    1368             : 
    1369           0 :                 funcs = plane->helper_private;
    1370             : 
    1371           0 :                 if (funcs->cleanup_fb)
    1372           0 :                         funcs->cleanup_fb(plane, plane_state);
    1373           0 :         }
    1374           0 : }
    1375             : EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
    1376             : 
    1377             : /**
    1378             :  * drm_atomic_helper_swap_state - store atomic state into current sw state
    1379             :  * @dev: DRM device
    1380             :  * @state: atomic state
    1381             :  *
    1382             :  * This function stores the atomic state into the current state pointers in all
    1383             :  * driver objects. It should be called after all failing steps have been done
    1384             :  * and succeeded, but before the actual hardware state is committed.
    1385             :  *
    1386             :  * For cleanup and error recovery the current state for all changed objects will
    1387             :  * be swaped into @state.
    1388             :  *
    1389             :  * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
    1390             :  *
    1391             :  * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
    1392             :  *
    1393             :  * 2. Do any other steps that might fail.
    1394             :  *
    1395             :  * 3. Put the staged state into the current state pointers with this function.
    1396             :  *
    1397             :  * 4. Actually commit the hardware state.
    1398             :  *
    1399             :  * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
    1400             :  * contains the old state. Also do any other cleanup required with that state.
    1401             :  */
    1402           0 : void drm_atomic_helper_swap_state(struct drm_device *dev,
    1403             :                                   struct drm_atomic_state *state)
    1404             : {
    1405             :         int i;
    1406             : 
    1407           0 :         for (i = 0; i < dev->mode_config.num_connector; i++) {
    1408           0 :                 struct drm_connector *connector = state->connectors[i];
    1409             : 
    1410           0 :                 if (!connector)
    1411           0 :                         continue;
    1412             : 
    1413           0 :                 connector->state->state = state;
    1414           0 :                 swap(state->connector_states[i], connector->state);
    1415           0 :                 connector->state->state = NULL;
    1416           0 :         }
    1417             : 
    1418           0 :         for (i = 0; i < dev->mode_config.num_crtc; i++) {
    1419           0 :                 struct drm_crtc *crtc = state->crtcs[i];
    1420             : 
    1421           0 :                 if (!crtc)
    1422           0 :                         continue;
    1423             : 
    1424           0 :                 crtc->state->state = state;
    1425           0 :                 swap(state->crtc_states[i], crtc->state);
    1426           0 :                 crtc->state->state = NULL;
    1427           0 :         }
    1428             : 
    1429           0 :         for (i = 0; i < dev->mode_config.num_total_plane; i++) {
    1430           0 :                 struct drm_plane *plane = state->planes[i];
    1431             : 
    1432           0 :                 if (!plane)
    1433           0 :                         continue;
    1434             : 
    1435           0 :                 plane->state->state = state;
    1436           0 :                 swap(state->plane_states[i], plane->state);
    1437           0 :                 plane->state->state = NULL;
    1438           0 :         }
    1439           0 : }
    1440             : EXPORT_SYMBOL(drm_atomic_helper_swap_state);
    1441             : 
    1442             : /**
    1443             :  * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
    1444             :  * @plane: plane object to update
    1445             :  * @crtc: owning CRTC of owning plane
    1446             :  * @fb: framebuffer to flip onto plane
    1447             :  * @crtc_x: x offset of primary plane on crtc
    1448             :  * @crtc_y: y offset of primary plane on crtc
    1449             :  * @crtc_w: width of primary plane rectangle on crtc
    1450             :  * @crtc_h: height of primary plane rectangle on crtc
    1451             :  * @src_x: x offset of @fb for panning
    1452             :  * @src_y: y offset of @fb for panning
    1453             :  * @src_w: width of source rectangle in @fb
    1454             :  * @src_h: height of source rectangle in @fb
    1455             :  *
    1456             :  * Provides a default plane update handler using the atomic driver interface.
    1457             :  *
    1458             :  * RETURNS:
    1459             :  * Zero on success, error code on failure
    1460             :  */
    1461           0 : int drm_atomic_helper_update_plane(struct drm_plane *plane,
    1462             :                                    struct drm_crtc *crtc,
    1463             :                                    struct drm_framebuffer *fb,
    1464             :                                    int crtc_x, int crtc_y,
    1465             :                                    unsigned int crtc_w, unsigned int crtc_h,
    1466             :                                    uint32_t src_x, uint32_t src_y,
    1467             :                                    uint32_t src_w, uint32_t src_h)
    1468             : {
    1469             :         struct drm_atomic_state *state;
    1470             :         struct drm_plane_state *plane_state;
    1471             :         int ret = 0;
    1472             : 
    1473           0 :         state = drm_atomic_state_alloc(plane->dev);
    1474           0 :         if (!state)
    1475           0 :                 return -ENOMEM;
    1476             : 
    1477           0 :         state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
    1478             : retry:
    1479           0 :         plane_state = drm_atomic_get_plane_state(state, plane);
    1480           0 :         if (IS_ERR(plane_state)) {
    1481           0 :                 ret = PTR_ERR(plane_state);
    1482           0 :                 goto fail;
    1483             :         }
    1484             : 
    1485           0 :         ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
    1486           0 :         if (ret != 0)
    1487             :                 goto fail;
    1488           0 :         drm_atomic_set_fb_for_plane(plane_state, fb);
    1489           0 :         plane_state->crtc_x = crtc_x;
    1490           0 :         plane_state->crtc_y = crtc_y;
    1491           0 :         plane_state->crtc_h = crtc_h;
    1492           0 :         plane_state->crtc_w = crtc_w;
    1493           0 :         plane_state->src_x = src_x;
    1494           0 :         plane_state->src_y = src_y;
    1495           0 :         plane_state->src_h = src_h;
    1496           0 :         plane_state->src_w = src_w;
    1497             : 
    1498           0 :         if (plane == crtc->cursor)
    1499           0 :                 state->legacy_cursor_update = true;
    1500             : 
    1501           0 :         ret = drm_atomic_commit(state);
    1502           0 :         if (ret != 0)
    1503             :                 goto fail;
    1504             : 
    1505             :         /* Driver takes ownership of state on successful commit. */
    1506           0 :         return 0;
    1507             : fail:
    1508           0 :         if (ret == -EDEADLK)
    1509             :                 goto backoff;
    1510             : 
    1511           0 :         drm_atomic_state_free(state);
    1512             : 
    1513           0 :         return ret;
    1514             : backoff:
    1515           0 :         drm_atomic_state_clear(state);
    1516           0 :         drm_atomic_legacy_backoff(state);
    1517             : 
    1518             :         /*
    1519             :          * Someone might have exchanged the framebuffer while we dropped locks
    1520             :          * in the backoff code. We need to fix up the fb refcount tracking the
    1521             :          * core does for us.
    1522             :          */
    1523           0 :         plane->old_fb = plane->fb;
    1524             : 
    1525           0 :         goto retry;
    1526           0 : }
    1527             : EXPORT_SYMBOL(drm_atomic_helper_update_plane);
    1528             : 
    1529             : /**
    1530             :  * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
    1531             :  * @plane: plane to disable
    1532             :  *
    1533             :  * Provides a default plane disable handler using the atomic driver interface.
    1534             :  *
    1535             :  * RETURNS:
    1536             :  * Zero on success, error code on failure
    1537             :  */
    1538           0 : int drm_atomic_helper_disable_plane(struct drm_plane *plane)
    1539             : {
    1540             :         struct drm_atomic_state *state;
    1541             :         struct drm_plane_state *plane_state;
    1542             :         int ret = 0;
    1543             : 
    1544             :         /*
    1545             :          * FIXME: Without plane->crtc set we can't get at the implicit legacy
    1546             :          * acquire context. The real fix will be to wire the acquire ctx through
    1547             :          * everywhere we need it, but meanwhile prevent chaos by just skipping
    1548             :          * this noop. The critical case is the cursor ioctls which a) only grab
    1549             :          * crtc/cursor-plane locks (so we need the crtc to get at the right
    1550             :          * acquire context) and b) can try to disable the plane multiple times.
    1551             :          */
    1552           0 :         if (!plane->crtc)
    1553           0 :                 return 0;
    1554             : 
    1555           0 :         state = drm_atomic_state_alloc(plane->dev);
    1556           0 :         if (!state)
    1557           0 :                 return -ENOMEM;
    1558             : 
    1559           0 :         state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
    1560             : retry:
    1561           0 :         plane_state = drm_atomic_get_plane_state(state, plane);
    1562           0 :         if (IS_ERR(plane_state)) {
    1563           0 :                 ret = PTR_ERR(plane_state);
    1564           0 :                 goto fail;
    1565             :         }
    1566             : 
    1567           0 :         if (plane_state->crtc && (plane == plane->crtc->cursor))
    1568           0 :                 plane_state->state->legacy_cursor_update = true;
    1569             : 
    1570           0 :         ret = __drm_atomic_helper_disable_plane(plane, plane_state);
    1571           0 :         if (ret != 0)
    1572             :                 goto fail;
    1573             : 
    1574           0 :         ret = drm_atomic_commit(state);
    1575           0 :         if (ret != 0)
    1576             :                 goto fail;
    1577             : 
    1578             :         /* Driver takes ownership of state on successful commit. */
    1579           0 :         return 0;
    1580             : fail:
    1581           0 :         if (ret == -EDEADLK)
    1582             :                 goto backoff;
    1583             : 
    1584           0 :         drm_atomic_state_free(state);
    1585             : 
    1586           0 :         return ret;
    1587             : backoff:
    1588           0 :         drm_atomic_state_clear(state);
    1589           0 :         drm_atomic_legacy_backoff(state);
    1590             : 
    1591             :         /*
    1592             :          * Someone might have exchanged the framebuffer while we dropped locks
    1593             :          * in the backoff code. We need to fix up the fb refcount tracking the
    1594             :          * core does for us.
    1595             :          */
    1596           0 :         plane->old_fb = plane->fb;
    1597             : 
    1598           0 :         goto retry;
    1599           0 : }
    1600             : EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
    1601             : 
    1602             : /* just used from fb-helper and atomic-helper: */
    1603           0 : int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
    1604             :                 struct drm_plane_state *plane_state)
    1605             : {
    1606             :         int ret;
    1607             : 
    1608           0 :         ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
    1609           0 :         if (ret != 0)
    1610           0 :                 return ret;
    1611             : 
    1612           0 :         drm_atomic_set_fb_for_plane(plane_state, NULL);
    1613           0 :         plane_state->crtc_x = 0;
    1614           0 :         plane_state->crtc_y = 0;
    1615           0 :         plane_state->crtc_h = 0;
    1616           0 :         plane_state->crtc_w = 0;
    1617           0 :         plane_state->src_x = 0;
    1618           0 :         plane_state->src_y = 0;
    1619           0 :         plane_state->src_h = 0;
    1620           0 :         plane_state->src_w = 0;
    1621             : 
    1622           0 :         return 0;
    1623           0 : }
    1624             : 
    1625           0 : static int update_output_state(struct drm_atomic_state *state,
    1626             :                                struct drm_mode_set *set)
    1627             : {
    1628           0 :         struct drm_device *dev = set->crtc->dev;
    1629             :         struct drm_crtc *crtc;
    1630             :         struct drm_crtc_state *crtc_state;
    1631             :         struct drm_connector *connector;
    1632             :         struct drm_connector_state *conn_state;
    1633             :         int ret, i, j;
    1634             : 
    1635           0 :         ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
    1636           0 :                                state->acquire_ctx);
    1637           0 :         if (ret)
    1638           0 :                 return ret;
    1639             : 
    1640             :         /* First grab all affected connector/crtc states. */
    1641           0 :         for (i = 0; i < set->num_connectors; i++) {
    1642           0 :                 conn_state = drm_atomic_get_connector_state(state,
    1643           0 :                                                             set->connectors[i]);
    1644           0 :                 if (IS_ERR(conn_state))
    1645           0 :                         return PTR_ERR(conn_state);
    1646             :         }
    1647             : 
    1648           0 :         for_each_crtc_in_state(state, crtc, crtc_state, i) {
    1649           0 :                 ret = drm_atomic_add_affected_connectors(state, crtc);
    1650           0 :                 if (ret)
    1651           0 :                         return ret;
    1652             :         }
    1653             : 
    1654             :         /* Then recompute connector->crtc links and crtc enabling state. */
    1655           0 :         for_each_connector_in_state(state, connector, conn_state, i) {
    1656           0 :                 if (conn_state->crtc == set->crtc) {
    1657           0 :                         ret = drm_atomic_set_crtc_for_connector(conn_state,
    1658             :                                                                 NULL);
    1659           0 :                         if (ret)
    1660           0 :                                 return ret;
    1661             :                 }
    1662             : 
    1663           0 :                 for (j = 0; j < set->num_connectors; j++) {
    1664           0 :                         if (set->connectors[j] == connector) {
    1665           0 :                                 ret = drm_atomic_set_crtc_for_connector(conn_state,
    1666           0 :                                                                         set->crtc);
    1667           0 :                                 if (ret)
    1668           0 :                                         return ret;
    1669             :                                 break;
    1670             :                         }
    1671             :                 }
    1672             :         }
    1673             : 
    1674           0 :         for_each_crtc_in_state(state, crtc, crtc_state, i) {
    1675             :                 /* Don't update ->enable for the CRTC in the set_config request,
    1676             :                  * since a mismatch would indicate a bug in the upper layers.
    1677             :                  * The actual modeset code later on will catch any
    1678             :                  * inconsistencies here. */
    1679           0 :                 if (crtc == set->crtc)
    1680             :                         continue;
    1681             : 
    1682           0 :                 if (!drm_atomic_connectors_for_crtc(state, crtc)) {
    1683           0 :                         ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
    1684             :                                                                 NULL);
    1685           0 :                         if (ret < 0)
    1686           0 :                                 return ret;
    1687             : 
    1688           0 :                         crtc_state->active = false;
    1689           0 :                 }
    1690             :         }
    1691             : 
    1692           0 :         return 0;
    1693           0 : }
    1694             : 
    1695             : /**
    1696             :  * drm_atomic_helper_set_config - set a new config from userspace
    1697             :  * @set: mode set configuration
    1698             :  *
    1699             :  * Provides a default crtc set_config handler using the atomic driver interface.
    1700             :  *
    1701             :  * Returns:
    1702             :  * Returns 0 on success, negative errno numbers on failure.
    1703             :  */
    1704           0 : int drm_atomic_helper_set_config(struct drm_mode_set *set)
    1705             : {
    1706             :         struct drm_atomic_state *state;
    1707           0 :         struct drm_crtc *crtc = set->crtc;
    1708             :         int ret = 0;
    1709             : 
    1710           0 :         state = drm_atomic_state_alloc(crtc->dev);
    1711           0 :         if (!state)
    1712           0 :                 return -ENOMEM;
    1713             : 
    1714           0 :         state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
    1715             : retry:
    1716           0 :         ret = __drm_atomic_helper_set_config(set, state);
    1717           0 :         if (ret != 0)
    1718             :                 goto fail;
    1719             : 
    1720           0 :         ret = drm_atomic_commit(state);
    1721           0 :         if (ret != 0)
    1722             :                 goto fail;
    1723             : 
    1724             :         /* Driver takes ownership of state on successful commit. */
    1725           0 :         return 0;
    1726             : fail:
    1727           0 :         if (ret == -EDEADLK)
    1728             :                 goto backoff;
    1729             : 
    1730           0 :         drm_atomic_state_free(state);
    1731             : 
    1732           0 :         return ret;
    1733             : backoff:
    1734           0 :         drm_atomic_state_clear(state);
    1735           0 :         drm_atomic_legacy_backoff(state);
    1736             : 
    1737             :         /*
    1738             :          * Someone might have exchanged the framebuffer while we dropped locks
    1739             :          * in the backoff code. We need to fix up the fb refcount tracking the
    1740             :          * core does for us.
    1741             :          */
    1742           0 :         crtc->primary->old_fb = crtc->primary->fb;
    1743             : 
    1744           0 :         goto retry;
    1745           0 : }
    1746             : EXPORT_SYMBOL(drm_atomic_helper_set_config);
    1747             : 
    1748             : /* just used from fb-helper and atomic-helper: */
    1749           0 : int __drm_atomic_helper_set_config(struct drm_mode_set *set,
    1750             :                 struct drm_atomic_state *state)
    1751             : {
    1752             :         struct drm_crtc_state *crtc_state;
    1753             :         struct drm_plane_state *primary_state;
    1754           0 :         struct drm_crtc *crtc = set->crtc;
    1755           0 :         int hdisplay, vdisplay;
    1756             :         int ret;
    1757             : 
    1758           0 :         crtc_state = drm_atomic_get_crtc_state(state, crtc);
    1759           0 :         if (IS_ERR(crtc_state))
    1760           0 :                 return PTR_ERR(crtc_state);
    1761             : 
    1762           0 :         primary_state = drm_atomic_get_plane_state(state, crtc->primary);
    1763           0 :         if (IS_ERR(primary_state))
    1764           0 :                 return PTR_ERR(primary_state);
    1765             : 
    1766           0 :         if (!set->mode) {
    1767           0 :                 WARN_ON(set->fb);
    1768           0 :                 WARN_ON(set->num_connectors);
    1769             : 
    1770           0 :                 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
    1771           0 :                 if (ret != 0)
    1772           0 :                         return ret;
    1773             : 
    1774           0 :                 crtc_state->active = false;
    1775             : 
    1776           0 :                 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
    1777           0 :                 if (ret != 0)
    1778           0 :                         return ret;
    1779             : 
    1780           0 :                 drm_atomic_set_fb_for_plane(primary_state, NULL);
    1781             : 
    1782           0 :                 goto commit;
    1783             :         }
    1784             : 
    1785           0 :         WARN_ON(!set->fb);
    1786           0 :         WARN_ON(!set->num_connectors);
    1787             : 
    1788           0 :         ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
    1789           0 :         if (ret != 0)
    1790           0 :                 return ret;
    1791             : 
    1792           0 :         crtc_state->active = true;
    1793             : 
    1794           0 :         ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
    1795           0 :         if (ret != 0)
    1796           0 :                 return ret;
    1797             : 
    1798           0 :         drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
    1799             : 
    1800           0 :         drm_atomic_set_fb_for_plane(primary_state, set->fb);
    1801           0 :         primary_state->crtc_x = 0;
    1802           0 :         primary_state->crtc_y = 0;
    1803           0 :         primary_state->crtc_h = vdisplay;
    1804           0 :         primary_state->crtc_w = hdisplay;
    1805           0 :         primary_state->src_x = set->x << 16;
    1806           0 :         primary_state->src_y = set->y << 16;
    1807           0 :         if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
    1808           0 :                 primary_state->src_h = hdisplay << 16;
    1809           0 :                 primary_state->src_w = vdisplay << 16;
    1810           0 :         } else {
    1811           0 :                 primary_state->src_h = vdisplay << 16;
    1812           0 :                 primary_state->src_w = hdisplay << 16;
    1813             :         }
    1814             : 
    1815             : commit:
    1816           0 :         ret = update_output_state(state, set);
    1817           0 :         if (ret)
    1818           0 :                 return ret;
    1819             : 
    1820           0 :         return 0;
    1821           0 : }
    1822             : 
    1823             : /**
    1824             :  * drm_atomic_helper_crtc_set_property - helper for crtc properties
    1825             :  * @crtc: DRM crtc
    1826             :  * @property: DRM property
    1827             :  * @val: value of property
    1828             :  *
    1829             :  * Provides a default crtc set_property handler using the atomic driver
    1830             :  * interface.
    1831             :  *
    1832             :  * RETURNS:
    1833             :  * Zero on success, error code on failure
    1834             :  */
    1835             : int
    1836           0 : drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
    1837             :                                     struct drm_property *property,
    1838             :                                     uint64_t val)
    1839             : {
    1840             :         struct drm_atomic_state *state;
    1841             :         struct drm_crtc_state *crtc_state;
    1842             :         int ret = 0;
    1843             : 
    1844           0 :         state = drm_atomic_state_alloc(crtc->dev);
    1845           0 :         if (!state)
    1846           0 :                 return -ENOMEM;
    1847             : 
    1848             :         /* ->set_property is always called with all locks held. */
    1849           0 :         state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
    1850             : retry:
    1851           0 :         crtc_state = drm_atomic_get_crtc_state(state, crtc);
    1852           0 :         if (IS_ERR(crtc_state)) {
    1853           0 :                 ret = PTR_ERR(crtc_state);
    1854           0 :                 goto fail;
    1855             :         }
    1856             : 
    1857           0 :         ret = drm_atomic_crtc_set_property(crtc, crtc_state,
    1858             :                         property, val);
    1859           0 :         if (ret)
    1860             :                 goto fail;
    1861             : 
    1862           0 :         ret = drm_atomic_commit(state);
    1863           0 :         if (ret != 0)
    1864             :                 goto fail;
    1865             : 
    1866             :         /* Driver takes ownership of state on successful commit. */
    1867           0 :         return 0;
    1868             : fail:
    1869           0 :         if (ret == -EDEADLK)
    1870             :                 goto backoff;
    1871             : 
    1872           0 :         drm_atomic_state_free(state);
    1873             : 
    1874           0 :         return ret;
    1875             : backoff:
    1876           0 :         drm_atomic_state_clear(state);
    1877           0 :         drm_atomic_legacy_backoff(state);
    1878             : 
    1879           0 :         goto retry;
    1880           0 : }
    1881             : EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
    1882             : 
    1883             : /**
    1884             :  * drm_atomic_helper_plane_set_property - helper for plane properties
    1885             :  * @plane: DRM plane
    1886             :  * @property: DRM property
    1887             :  * @val: value of property
    1888             :  *
    1889             :  * Provides a default plane set_property handler using the atomic driver
    1890             :  * interface.
    1891             :  *
    1892             :  * RETURNS:
    1893             :  * Zero on success, error code on failure
    1894             :  */
    1895             : int
    1896           0 : drm_atomic_helper_plane_set_property(struct drm_plane *plane,
    1897             :                                     struct drm_property *property,
    1898             :                                     uint64_t val)
    1899             : {
    1900             :         struct drm_atomic_state *state;
    1901             :         struct drm_plane_state *plane_state;
    1902             :         int ret = 0;
    1903             : 
    1904           0 :         state = drm_atomic_state_alloc(plane->dev);
    1905           0 :         if (!state)
    1906           0 :                 return -ENOMEM;
    1907             : 
    1908             :         /* ->set_property is always called with all locks held. */
    1909           0 :         state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
    1910             : retry:
    1911           0 :         plane_state = drm_atomic_get_plane_state(state, plane);
    1912           0 :         if (IS_ERR(plane_state)) {
    1913           0 :                 ret = PTR_ERR(plane_state);
    1914           0 :                 goto fail;
    1915             :         }
    1916             : 
    1917           0 :         ret = drm_atomic_plane_set_property(plane, plane_state,
    1918             :                         property, val);
    1919           0 :         if (ret)
    1920             :                 goto fail;
    1921             : 
    1922           0 :         ret = drm_atomic_commit(state);
    1923           0 :         if (ret != 0)
    1924             :                 goto fail;
    1925             : 
    1926             :         /* Driver takes ownership of state on successful commit. */
    1927           0 :         return 0;
    1928             : fail:
    1929           0 :         if (ret == -EDEADLK)
    1930             :                 goto backoff;
    1931             : 
    1932           0 :         drm_atomic_state_free(state);
    1933             : 
    1934           0 :         return ret;
    1935             : backoff:
    1936           0 :         drm_atomic_state_clear(state);
    1937           0 :         drm_atomic_legacy_backoff(state);
    1938             : 
    1939           0 :         goto retry;
    1940           0 : }
    1941             : EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
    1942             : 
    1943             : /**
    1944             :  * drm_atomic_helper_connector_set_property - helper for connector properties
    1945             :  * @connector: DRM connector
    1946             :  * @property: DRM property
    1947             :  * @val: value of property
    1948             :  *
    1949             :  * Provides a default connector set_property handler using the atomic driver
    1950             :  * interface.
    1951             :  *
    1952             :  * RETURNS:
    1953             :  * Zero on success, error code on failure
    1954             :  */
    1955             : int
    1956           0 : drm_atomic_helper_connector_set_property(struct drm_connector *connector,
    1957             :                                     struct drm_property *property,
    1958             :                                     uint64_t val)
    1959             : {
    1960             :         struct drm_atomic_state *state;
    1961             :         struct drm_connector_state *connector_state;
    1962             :         int ret = 0;
    1963             : 
    1964           0 :         state = drm_atomic_state_alloc(connector->dev);
    1965           0 :         if (!state)
    1966           0 :                 return -ENOMEM;
    1967             : 
    1968             :         /* ->set_property is always called with all locks held. */
    1969           0 :         state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
    1970             : retry:
    1971           0 :         connector_state = drm_atomic_get_connector_state(state, connector);
    1972           0 :         if (IS_ERR(connector_state)) {
    1973           0 :                 ret = PTR_ERR(connector_state);
    1974           0 :                 goto fail;
    1975             :         }
    1976             : 
    1977           0 :         ret = drm_atomic_connector_set_property(connector, connector_state,
    1978             :                         property, val);
    1979           0 :         if (ret)
    1980             :                 goto fail;
    1981             : 
    1982           0 :         ret = drm_atomic_commit(state);
    1983           0 :         if (ret != 0)
    1984             :                 goto fail;
    1985             : 
    1986             :         /* Driver takes ownership of state on successful commit. */
    1987           0 :         return 0;
    1988             : fail:
    1989           0 :         if (ret == -EDEADLK)
    1990             :                 goto backoff;
    1991             : 
    1992           0 :         drm_atomic_state_free(state);
    1993             : 
    1994           0 :         return ret;
    1995             : backoff:
    1996           0 :         drm_atomic_state_clear(state);
    1997           0 :         drm_atomic_legacy_backoff(state);
    1998             : 
    1999           0 :         goto retry;
    2000           0 : }
    2001             : EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
    2002             : 
    2003             : /**
    2004             :  * drm_atomic_helper_page_flip - execute a legacy page flip
    2005             :  * @crtc: DRM crtc
    2006             :  * @fb: DRM framebuffer
    2007             :  * @event: optional DRM event to signal upon completion
    2008             :  * @flags: flip flags for non-vblank sync'ed updates
    2009             :  *
    2010             :  * Provides a default page flip implementation using the atomic driver interface.
    2011             :  *
    2012             :  * Note that for now so called async page flips (i.e. updates which are not
    2013             :  * synchronized to vblank) are not supported, since the atomic interfaces have
    2014             :  * no provisions for this yet.
    2015             :  *
    2016             :  * Returns:
    2017             :  * Returns 0 on success, negative errno numbers on failure.
    2018             :  */
    2019           0 : int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
    2020             :                                 struct drm_framebuffer *fb,
    2021             :                                 struct drm_pending_vblank_event *event,
    2022             :                                 uint32_t flags)
    2023             : {
    2024           0 :         struct drm_plane *plane = crtc->primary;
    2025             :         struct drm_atomic_state *state;
    2026             :         struct drm_plane_state *plane_state;
    2027             :         struct drm_crtc_state *crtc_state;
    2028             :         int ret = 0;
    2029             : 
    2030           0 :         if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
    2031           0 :                 return -EINVAL;
    2032             : 
    2033           0 :         state = drm_atomic_state_alloc(plane->dev);
    2034           0 :         if (!state)
    2035           0 :                 return -ENOMEM;
    2036             : 
    2037           0 :         state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
    2038             : retry:
    2039           0 :         crtc_state = drm_atomic_get_crtc_state(state, crtc);
    2040           0 :         if (IS_ERR(crtc_state)) {
    2041           0 :                 ret = PTR_ERR(crtc_state);
    2042           0 :                 goto fail;
    2043             :         }
    2044           0 :         crtc_state->event = event;
    2045             : 
    2046           0 :         plane_state = drm_atomic_get_plane_state(state, plane);
    2047           0 :         if (IS_ERR(plane_state)) {
    2048           0 :                 ret = PTR_ERR(plane_state);
    2049           0 :                 goto fail;
    2050             :         }
    2051             : 
    2052           0 :         ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
    2053           0 :         if (ret != 0)
    2054             :                 goto fail;
    2055           0 :         drm_atomic_set_fb_for_plane(plane_state, fb);
    2056             : 
    2057           0 :         ret = drm_atomic_async_commit(state);
    2058           0 :         if (ret != 0)
    2059             :                 goto fail;
    2060             : 
    2061             :         /* Driver takes ownership of state on successful async commit. */
    2062           0 :         return 0;
    2063             : fail:
    2064           0 :         if (ret == -EDEADLK)
    2065             :                 goto backoff;
    2066             : 
    2067           0 :         drm_atomic_state_free(state);
    2068             : 
    2069           0 :         return ret;
    2070             : backoff:
    2071           0 :         drm_atomic_state_clear(state);
    2072           0 :         drm_atomic_legacy_backoff(state);
    2073             : 
    2074             :         /*
    2075             :          * Someone might have exchanged the framebuffer while we dropped locks
    2076             :          * in the backoff code. We need to fix up the fb refcount tracking the
    2077             :          * core does for us.
    2078             :          */
    2079           0 :         plane->old_fb = plane->fb;
    2080             : 
    2081           0 :         goto retry;
    2082           0 : }
    2083             : EXPORT_SYMBOL(drm_atomic_helper_page_flip);
    2084             : 
    2085             : /**
    2086             :  * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
    2087             :  * @connector: affected connector
    2088             :  * @mode: DPMS mode
    2089             :  *
    2090             :  * This is the main helper function provided by the atomic helper framework for
    2091             :  * implementing the legacy DPMS connector interface. It computes the new desired
    2092             :  * ->active state for the corresponding CRTC (if the connector is enabled) and
    2093             :  *  updates it.
    2094             :  *
    2095             :  * Returns:
    2096             :  * Returns 0 on success, negative errno numbers on failure.
    2097             :  */
    2098           0 : int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
    2099             :                                      int mode)
    2100             : {
    2101           0 :         struct drm_mode_config *config = &connector->dev->mode_config;
    2102             :         struct drm_atomic_state *state;
    2103             :         struct drm_crtc_state *crtc_state;
    2104             :         struct drm_crtc *crtc;
    2105             :         struct drm_connector *tmp_connector;
    2106             :         int ret;
    2107             :         bool active = false;
    2108           0 :         int old_mode = connector->dpms;
    2109             : 
    2110           0 :         if (mode != DRM_MODE_DPMS_ON)
    2111           0 :                 mode = DRM_MODE_DPMS_OFF;
    2112             : 
    2113           0 :         connector->dpms = mode;
    2114           0 :         crtc = connector->state->crtc;
    2115             : 
    2116           0 :         if (!crtc)
    2117           0 :                 return 0;
    2118             : 
    2119           0 :         state = drm_atomic_state_alloc(connector->dev);
    2120           0 :         if (!state)
    2121           0 :                 return -ENOMEM;
    2122             : 
    2123           0 :         state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
    2124             : retry:
    2125           0 :         crtc_state = drm_atomic_get_crtc_state(state, crtc);
    2126           0 :         if (IS_ERR(crtc_state)) {
    2127           0 :                 ret = PTR_ERR(crtc_state);
    2128           0 :                 goto fail;
    2129             :         }
    2130             : 
    2131           0 :         WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
    2132             : 
    2133           0 :         drm_for_each_connector(tmp_connector, connector->dev) {
    2134           0 :                 if (tmp_connector->state->crtc != crtc)
    2135             :                         continue;
    2136             : 
    2137           0 :                 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
    2138             :                         active = true;
    2139           0 :                         break;
    2140             :                 }
    2141             :         }
    2142           0 :         crtc_state->active = active;
    2143             : 
    2144           0 :         ret = drm_atomic_commit(state);
    2145           0 :         if (ret != 0)
    2146             :                 goto fail;
    2147             : 
    2148             :         /* Driver takes ownership of state on successful commit. */
    2149           0 :         return 0;
    2150             : fail:
    2151           0 :         if (ret == -EDEADLK)
    2152             :                 goto backoff;
    2153             : 
    2154           0 :         connector->dpms = old_mode;
    2155           0 :         drm_atomic_state_free(state);
    2156             : 
    2157           0 :         return ret;
    2158             : backoff:
    2159           0 :         drm_atomic_state_clear(state);
    2160           0 :         drm_atomic_legacy_backoff(state);
    2161             : 
    2162           0 :         goto retry;
    2163           0 : }
    2164             : EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
    2165             : 
    2166             : /**
    2167             :  * DOC: atomic state reset and initialization
    2168             :  *
    2169             :  * Both the drm core and the atomic helpers assume that there is always the full
    2170             :  * and correct atomic software state for all connectors, CRTCs and planes
    2171             :  * available. Which is a bit a problem on driver load and also after system
    2172             :  * suspend. One way to solve this is to have a hardware state read-out
    2173             :  * infrastructure which reconstructs the full software state (e.g. the i915
    2174             :  * driver).
    2175             :  *
    2176             :  * The simpler solution is to just reset the software state to everything off,
    2177             :  * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
    2178             :  * the atomic helpers provide default reset implementations for all hooks.
    2179             :  */
    2180             : 
    2181             : /**
    2182             :  * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
    2183             :  * @crtc: drm CRTC
    2184             :  *
    2185             :  * Resets the atomic state for @crtc by freeing the state pointer (which might
    2186             :  * be NULL, e.g. at driver load time) and allocating a new empty state object.
    2187             :  */
    2188           0 : void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
    2189             : {
    2190           0 :         if (crtc->state && crtc->state->mode_blob)
    2191           0 :                 drm_property_unreference_blob(crtc->state->mode_blob);
    2192           0 :         kfree(crtc->state);
    2193           0 :         crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
    2194             : 
    2195           0 :         if (crtc->state)
    2196           0 :                 crtc->state->crtc = crtc;
    2197           0 : }
    2198             : EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
    2199             : 
    2200             : /**
    2201             :  * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
    2202             :  * @crtc: CRTC object
    2203             :  * @state: atomic CRTC state
    2204             :  *
    2205             :  * Copies atomic state from a CRTC's current state and resets inferred values.
    2206             :  * This is useful for drivers that subclass the CRTC state.
    2207             :  */
    2208           0 : void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
    2209             :                                               struct drm_crtc_state *state)
    2210             : {
    2211           0 :         memcpy(state, crtc->state, sizeof(*state));
    2212             : 
    2213           0 :         if (state->mode_blob)
    2214           0 :                 drm_property_reference_blob(state->mode_blob);
    2215           0 :         state->mode_changed = false;
    2216           0 :         state->active_changed = false;
    2217           0 :         state->planes_changed = false;
    2218           0 :         state->connectors_changed = false;
    2219           0 :         state->event = NULL;
    2220           0 : }
    2221             : EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
    2222             : 
    2223             : /**
    2224             :  * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
    2225             :  * @crtc: drm CRTC
    2226             :  *
    2227             :  * Default CRTC state duplicate hook for drivers which don't have their own
    2228             :  * subclassed CRTC state structure.
    2229             :  */
    2230             : struct drm_crtc_state *
    2231           0 : drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
    2232             : {
    2233             :         struct drm_crtc_state *state;
    2234             : 
    2235           0 :         if (WARN_ON(!crtc->state))
    2236           0 :                 return NULL;
    2237             : 
    2238           0 :         state = kmalloc(sizeof(*state), GFP_KERNEL);
    2239           0 :         if (state)
    2240           0 :                 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
    2241             : 
    2242           0 :         return state;
    2243           0 : }
    2244             : EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
    2245             : 
    2246             : /**
    2247             :  * __drm_atomic_helper_crtc_destroy_state - release CRTC state
    2248             :  * @crtc: CRTC object
    2249             :  * @state: CRTC state object to release
    2250             :  *
    2251             :  * Releases all resources stored in the CRTC state without actually freeing
    2252             :  * the memory of the CRTC state. This is useful for drivers that subclass the
    2253             :  * CRTC state.
    2254             :  */
    2255           0 : void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
    2256             :                                             struct drm_crtc_state *state)
    2257             : {
    2258           0 :         if (state->mode_blob)
    2259           0 :                 drm_property_unreference_blob(state->mode_blob);
    2260           0 : }
    2261             : EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
    2262             : 
    2263             : /**
    2264             :  * drm_atomic_helper_crtc_destroy_state - default state destroy hook
    2265             :  * @crtc: drm CRTC
    2266             :  * @state: CRTC state object to release
    2267             :  *
    2268             :  * Default CRTC state destroy hook for drivers which don't have their own
    2269             :  * subclassed CRTC state structure.
    2270             :  */
    2271           0 : void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
    2272             :                                           struct drm_crtc_state *state)
    2273             : {
    2274           0 :         __drm_atomic_helper_crtc_destroy_state(crtc, state);
    2275           0 :         kfree(state);
    2276           0 : }
    2277             : EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
    2278             : 
    2279             : /**
    2280             :  * drm_atomic_helper_plane_reset - default ->reset hook for planes
    2281             :  * @plane: drm plane
    2282             :  *
    2283             :  * Resets the atomic state for @plane by freeing the state pointer (which might
    2284             :  * be NULL, e.g. at driver load time) and allocating a new empty state object.
    2285             :  */
    2286           0 : void drm_atomic_helper_plane_reset(struct drm_plane *plane)
    2287             : {
    2288           0 :         if (plane->state && plane->state->fb)
    2289           0 :                 drm_framebuffer_unreference(plane->state->fb);
    2290             : 
    2291           0 :         kfree(plane->state);
    2292           0 :         plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
    2293             : 
    2294           0 :         if (plane->state)
    2295           0 :                 plane->state->plane = plane;
    2296           0 : }
    2297             : EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
    2298             : 
    2299             : /**
    2300             :  * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
    2301             :  * @plane: plane object
    2302             :  * @state: atomic plane state
    2303             :  *
    2304             :  * Copies atomic state from a plane's current state. This is useful for
    2305             :  * drivers that subclass the plane state.
    2306             :  */
    2307           0 : void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
    2308             :                                                struct drm_plane_state *state)
    2309             : {
    2310           0 :         memcpy(state, plane->state, sizeof(*state));
    2311             : 
    2312           0 :         if (state->fb)
    2313           0 :                 drm_framebuffer_reference(state->fb);
    2314           0 : }
    2315             : EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
    2316             : 
    2317             : /**
    2318             :  * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
    2319             :  * @plane: drm plane
    2320             :  *
    2321             :  * Default plane state duplicate hook for drivers which don't have their own
    2322             :  * subclassed plane state structure.
    2323             :  */
    2324             : struct drm_plane_state *
    2325           0 : drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
    2326             : {
    2327             :         struct drm_plane_state *state;
    2328             : 
    2329           0 :         if (WARN_ON(!plane->state))
    2330           0 :                 return NULL;
    2331             : 
    2332           0 :         state = kmalloc(sizeof(*state), GFP_KERNEL);
    2333           0 :         if (state)
    2334           0 :                 __drm_atomic_helper_plane_duplicate_state(plane, state);
    2335             : 
    2336           0 :         return state;
    2337           0 : }
    2338             : EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
    2339             : 
    2340             : /**
    2341             :  * __drm_atomic_helper_plane_destroy_state - release plane state
    2342             :  * @plane: plane object
    2343             :  * @state: plane state object to release
    2344             :  *
    2345             :  * Releases all resources stored in the plane state without actually freeing
    2346             :  * the memory of the plane state. This is useful for drivers that subclass the
    2347             :  * plane state.
    2348             :  */
    2349           0 : void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
    2350             :                                              struct drm_plane_state *state)
    2351             : {
    2352           0 :         if (state->fb)
    2353           0 :                 drm_framebuffer_unreference(state->fb);
    2354           0 : }
    2355             : EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
    2356             : 
    2357             : /**
    2358             :  * drm_atomic_helper_plane_destroy_state - default state destroy hook
    2359             :  * @plane: drm plane
    2360             :  * @state: plane state object to release
    2361             :  *
    2362             :  * Default plane state destroy hook for drivers which don't have their own
    2363             :  * subclassed plane state structure.
    2364             :  */
    2365           0 : void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
    2366             :                                            struct drm_plane_state *state)
    2367             : {
    2368           0 :         __drm_atomic_helper_plane_destroy_state(plane, state);
    2369           0 :         kfree(state);
    2370           0 : }
    2371             : EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
    2372             : 
    2373             : /**
    2374             :  * drm_atomic_helper_connector_reset - default ->reset hook for connectors
    2375             :  * @connector: drm connector
    2376             :  *
    2377             :  * Resets the atomic state for @connector by freeing the state pointer (which
    2378             :  * might be NULL, e.g. at driver load time) and allocating a new empty state
    2379             :  * object.
    2380             :  */
    2381           0 : void drm_atomic_helper_connector_reset(struct drm_connector *connector)
    2382             : {
    2383           0 :         kfree(connector->state);
    2384           0 :         connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
    2385             : 
    2386           0 :         if (connector->state)
    2387           0 :                 connector->state->connector = connector;
    2388           0 : }
    2389             : EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
    2390             : 
    2391             : /**
    2392             :  * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
    2393             :  * @connector: connector object
    2394             :  * @state: atomic connector state
    2395             :  *
    2396             :  * Copies atomic state from a connector's current state. This is useful for
    2397             :  * drivers that subclass the connector state.
    2398             :  */
    2399             : void
    2400           0 : __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
    2401             :                                             struct drm_connector_state *state)
    2402             : {
    2403           0 :         memcpy(state, connector->state, sizeof(*state));
    2404           0 : }
    2405             : EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
    2406             : 
    2407             : /**
    2408             :  * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
    2409             :  * @connector: drm connector
    2410             :  *
    2411             :  * Default connector state duplicate hook for drivers which don't have their own
    2412             :  * subclassed connector state structure.
    2413             :  */
    2414             : struct drm_connector_state *
    2415           0 : drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
    2416             : {
    2417             :         struct drm_connector_state *state;
    2418             : 
    2419           0 :         if (WARN_ON(!connector->state))
    2420           0 :                 return NULL;
    2421             : 
    2422           0 :         state = kmalloc(sizeof(*state), GFP_KERNEL);
    2423           0 :         if (state)
    2424           0 :                 __drm_atomic_helper_connector_duplicate_state(connector, state);
    2425             : 
    2426           0 :         return state;
    2427           0 : }
    2428             : EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
    2429             : 
    2430             : /**
    2431             :  * drm_atomic_helper_duplicate_state - duplicate an atomic state object
    2432             :  * @dev: DRM device
    2433             :  * @ctx: lock acquisition context
    2434             :  *
    2435             :  * Makes a copy of the current atomic state by looping over all objects and
    2436             :  * duplicating their respective states.
    2437             :  *
    2438             :  * Note that this treats atomic state as persistent between save and restore.
    2439             :  * Drivers must make sure that this is possible and won't result in confusion
    2440             :  * or erroneous behaviour.
    2441             :  *
    2442             :  * Note that if callers haven't already acquired all modeset locks this might
    2443             :  * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
    2444             :  *
    2445             :  * Returns:
    2446             :  * A pointer to the copy of the atomic state object on success or an
    2447             :  * ERR_PTR()-encoded error code on failure.
    2448             :  */
    2449             : struct drm_atomic_state *
    2450           0 : drm_atomic_helper_duplicate_state(struct drm_device *dev,
    2451             :                                   struct drm_modeset_acquire_ctx *ctx)
    2452             : {
    2453             :         struct drm_atomic_state *state;
    2454             :         struct drm_connector *conn;
    2455             :         struct drm_plane *plane;
    2456             :         struct drm_crtc *crtc;
    2457             :         int err = 0;
    2458             : 
    2459           0 :         state = drm_atomic_state_alloc(dev);
    2460           0 :         if (!state)
    2461           0 :                 return ERR_PTR(-ENOMEM);
    2462             : 
    2463           0 :         state->acquire_ctx = ctx;
    2464             : 
    2465           0 :         drm_for_each_crtc(crtc, dev) {
    2466             :                 struct drm_crtc_state *crtc_state;
    2467             : 
    2468           0 :                 crtc_state = drm_atomic_get_crtc_state(state, crtc);
    2469           0 :                 if (IS_ERR(crtc_state)) {
    2470           0 :                         err = PTR_ERR(crtc_state);
    2471           0 :                         goto free;
    2472             :                 }
    2473           0 :         }
    2474             : 
    2475           0 :         drm_for_each_plane(plane, dev) {
    2476             :                 struct drm_plane_state *plane_state;
    2477             : 
    2478           0 :                 plane_state = drm_atomic_get_plane_state(state, plane);
    2479           0 :                 if (IS_ERR(plane_state)) {
    2480           0 :                         err = PTR_ERR(plane_state);
    2481           0 :                         goto free;
    2482             :                 }
    2483           0 :         }
    2484             : 
    2485           0 :         drm_for_each_connector(conn, dev) {
    2486             :                 struct drm_connector_state *conn_state;
    2487             : 
    2488           0 :                 conn_state = drm_atomic_get_connector_state(state, conn);
    2489           0 :                 if (IS_ERR(conn_state)) {
    2490           0 :                         err = PTR_ERR(conn_state);
    2491           0 :                         goto free;
    2492             :                 }
    2493           0 :         }
    2494             : 
    2495             :         /* clear the acquire context so that it isn't accidentally reused */
    2496           0 :         state->acquire_ctx = NULL;
    2497             : 
    2498             : free:
    2499           0 :         if (err < 0) {
    2500           0 :                 drm_atomic_state_free(state);
    2501           0 :                 state = ERR_PTR(err);
    2502           0 :         }
    2503             : 
    2504           0 :         return state;
    2505           0 : }
    2506             : EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
    2507             : 
    2508             : /**
    2509             :  * __drm_atomic_helper_connector_destroy_state - release connector state
    2510             :  * @connector: connector object
    2511             :  * @state: connector state object to release
    2512             :  *
    2513             :  * Releases all resources stored in the connector state without actually
    2514             :  * freeing the memory of the connector state. This is useful for drivers that
    2515             :  * subclass the connector state.
    2516             :  */
    2517             : void
    2518           0 : __drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
    2519             :                                             struct drm_connector_state *state)
    2520             : {
    2521             :         /*
    2522             :          * This is currently a placeholder so that drivers that subclass the
    2523             :          * state will automatically do the right thing if code is ever added
    2524             :          * to this function.
    2525             :          */
    2526           0 : }
    2527             : EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
    2528             : 
    2529             : /**
    2530             :  * drm_atomic_helper_connector_destroy_state - default state destroy hook
    2531             :  * @connector: drm connector
    2532             :  * @state: connector state object to release
    2533             :  *
    2534             :  * Default connector state destroy hook for drivers which don't have their own
    2535             :  * subclassed connector state structure.
    2536             :  */
    2537           0 : void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
    2538             :                                           struct drm_connector_state *state)
    2539             : {
    2540           0 :         __drm_atomic_helper_connector_destroy_state(connector, state);
    2541           0 :         kfree(state);
    2542           0 : }
    2543             : EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);

Generated by: LCOV version 1.13