Subversion Repositories Kolibri OS

Rev

Rev 6088 | Rev 6937 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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