Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * Copyright (c) 2006-2008 Intel Corporation
  3.  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  4.  *
  5.  * DRM core CRTC related functions
  6.  *
  7.  * Permission to use, copy, modify, distribute, and sell this software and its
  8.  * documentation for any purpose is hereby granted without fee, provided that
  9.  * the above copyright notice appear in all copies and that both that copyright
  10.  * notice and this permission notice appear in supporting documentation, and
  11.  * that the name of the copyright holders not be used in advertising or
  12.  * publicity pertaining to distribution of the software without specific,
  13.  * written prior permission.  The copyright holders make no representations
  14.  * about the suitability of this software for any purpose.  It is provided "as
  15.  * is" without express or implied warranty.
  16.  *
  17.  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  18.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  19.  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  20.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  21.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  22.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23.  * OF THIS SOFTWARE.
  24.  *
  25.  * Authors:
  26.  *      Keith Packard
  27.  *      Eric Anholt <eric@anholt.net>
  28.  *      Dave Airlie <airlied@linux.ie>
  29.  *      Jesse Barnes <jesse.barnes@intel.com>
  30.  */
  31.  
  32. #include <linux/kernel.h>
  33. #include <linux/export.h>
  34. #include <linux/moduleparam.h>
  35.  
  36. #include <drm/drmP.h>
  37. #include <drm/drm_atomic.h>
  38. #include <drm/drm_crtc.h>
  39. #include <drm/drm_fourcc.h>
  40. #include <drm/drm_crtc_helper.h>
  41. #include <drm/drm_fb_helper.h>
  42. #include <drm/drm_plane_helper.h>
  43. #include <drm/drm_atomic_helper.h>
  44. #include <drm/drm_edid.h>
  45.  
  46. /**
  47.  * DOC: overview
  48.  *
  49.  * The CRTC modeset helper library provides a default set_config implementation
  50.  * in drm_crtc_helper_set_config(). Plus a few other convenience functions using
  51.  * the same callbacks which drivers can use to e.g. restore the modeset
  52.  * configuration on resume with drm_helper_resume_force_mode().
  53.  *
  54.  * The driver callbacks are mostly compatible with the atomic modeset helpers,
  55.  * except for the handling of the primary plane: Atomic helpers require that the
  56.  * primary plane is implemented as a real standalone plane and not directly tied
  57.  * to the CRTC state. For easier transition this library provides functions to
  58.  * implement the old semantics required by the CRTC helpers using the new plane
  59.  * and atomic helper callbacks.
  60.  *
  61.  * Drivers are strongly urged to convert to the atomic helpers (by way of first
  62.  * converting to the plane helpers). New drivers must not use these functions
  63.  * but need to implement the atomic interface instead, potentially using the
  64.  * atomic helpers for that.
  65.  */
  66. MODULE_AUTHOR("David Airlie, Jesse Barnes");
  67. MODULE_DESCRIPTION("DRM KMS helper");
  68. MODULE_LICENSE("GPL and additional rights");
  69.  
  70. /**
  71.  * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
  72.  *                                              connector list
  73.  * @dev: drm device to operate on
  74.  *
  75.  * Some userspace presumes that the first connected connector is the main
  76.  * display, where it's supposed to display e.g. the login screen. For
  77.  * laptops, this should be the main panel. Use this function to sort all
  78.  * (eDP/LVDS) panels to the front of the connector list, instead of
  79.  * painstakingly trying to initialize them in the right order.
  80.  */
  81. void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
  82. {
  83.         struct drm_connector *connector, *tmp;
  84.         struct list_head panel_list;
  85.  
  86.         INIT_LIST_HEAD(&panel_list);
  87.  
  88.         list_for_each_entry_safe(connector, tmp,
  89.                                  &dev->mode_config.connector_list, head) {
  90.                 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
  91.                     connector->connector_type == DRM_MODE_CONNECTOR_eDP)
  92.                         list_move_tail(&connector->head, &panel_list);
  93.         }
  94.  
  95.         list_splice(&panel_list, &dev->mode_config.connector_list);
  96. }
  97. EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
  98.  
  99. /**
  100.  * drm_helper_encoder_in_use - check if a given encoder is in use
  101.  * @encoder: encoder to check
  102.  *
  103.  * Checks whether @encoder is with the current mode setting output configuration
  104.  * in use by any connector. This doesn't mean that it is actually enabled since
  105.  * the DPMS state is tracked separately.
  106.  *
  107.  * Returns:
  108.  * True if @encoder is used, false otherwise.
  109.  */
  110. bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
  111. {
  112.         struct drm_connector *connector;
  113.         struct drm_device *dev = encoder->dev;
  114.         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  115.                 if (connector->encoder == encoder)
  116.                         return true;
  117.         return false;
  118. }
  119. EXPORT_SYMBOL(drm_helper_encoder_in_use);
  120.  
  121. /**
  122.  * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
  123.  * @crtc: CRTC to check
  124.  *
  125.  * Checks whether @crtc is with the current mode setting output configuration
  126.  * in use by any connector. This doesn't mean that it is actually enabled since
  127.  * the DPMS state is tracked separately.
  128.  *
  129.  * Returns:
  130.  * True if @crtc is used, false otherwise.
  131.  */
  132. bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
  133. {
  134.         struct drm_encoder *encoder;
  135.         struct drm_device *dev = crtc->dev;
  136.         /* FIXME: Locking around list access? */
  137.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  138.                 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
  139.                         return true;
  140.         return false;
  141. }
  142. EXPORT_SYMBOL(drm_helper_crtc_in_use);
  143.  
  144. static void
  145. drm_encoder_disable(struct drm_encoder *encoder)
  146. {
  147.         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  148.  
  149.         if (encoder->bridge)
  150.                 encoder->bridge->funcs->disable(encoder->bridge);
  151.  
  152.         if (encoder_funcs->disable)
  153.                 (*encoder_funcs->disable)(encoder);
  154.         else
  155.                 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
  156.  
  157.         if (encoder->bridge)
  158.                 encoder->bridge->funcs->post_disable(encoder->bridge);
  159. }
  160.  
  161. static void __drm_helper_disable_unused_functions(struct drm_device *dev)
  162. {
  163.         struct drm_encoder *encoder;
  164.         struct drm_crtc *crtc;
  165.  
  166.         drm_warn_on_modeset_not_all_locked(dev);
  167.  
  168.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  169.                 if (!drm_helper_encoder_in_use(encoder)) {
  170.                         drm_encoder_disable(encoder);
  171.                         /* disconnect encoder from any connector */
  172.                         encoder->crtc = NULL;
  173.                 }
  174.         }
  175.  
  176.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  177.                 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  178.                 crtc->enabled = drm_helper_crtc_in_use(crtc);
  179.                 if (!crtc->enabled) {
  180.                         if (crtc_funcs->disable)
  181.                                 (*crtc_funcs->disable)(crtc);
  182.                         else
  183.                                 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
  184.                         crtc->primary->fb = NULL;
  185.                 }
  186.         }
  187. }
  188.  
  189. /**
  190.  * drm_helper_disable_unused_functions - disable unused objects
  191.  * @dev: DRM device
  192.  *
  193.  * This function walks through the entire mode setting configuration of @dev. It
  194.  * will remove any crtc links of unused encoders and encoder links of
  195.  * disconnected connectors. Then it will disable all unused encoders and crtcs
  196.  * either by calling their disable callback if available or by calling their
  197.  * dpms callback with DRM_MODE_DPMS_OFF.
  198.  */
  199. void drm_helper_disable_unused_functions(struct drm_device *dev)
  200. {
  201.         drm_modeset_lock_all(dev);
  202.         __drm_helper_disable_unused_functions(dev);
  203.         drm_modeset_unlock_all(dev);
  204. }
  205. EXPORT_SYMBOL(drm_helper_disable_unused_functions);
  206.  
  207. /*
  208.  * Check the CRTC we're going to map each output to vs. its current
  209.  * CRTC.  If they don't match, we have to disable the output and the CRTC
  210.  * since the driver will have to re-route things.
  211.  */
  212. static void
  213. drm_crtc_prepare_encoders(struct drm_device *dev)
  214. {
  215.         struct drm_encoder_helper_funcs *encoder_funcs;
  216.         struct drm_encoder *encoder;
  217.  
  218.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  219.                 encoder_funcs = encoder->helper_private;
  220.                 /* Disable unused encoders */
  221.                 if (encoder->crtc == NULL)
  222.                         drm_encoder_disable(encoder);
  223.                 /* Disable encoders whose CRTC is about to change */
  224.                 if (encoder_funcs->get_crtc &&
  225.                     encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
  226.                         drm_encoder_disable(encoder);
  227.         }
  228. }
  229.  
  230. /**
  231.  * drm_crtc_helper_set_mode - internal helper to set a mode
  232.  * @crtc: CRTC to program
  233.  * @mode: mode to use
  234.  * @x: horizontal offset into the surface
  235.  * @y: vertical offset into the surface
  236.  * @old_fb: old framebuffer, for cleanup
  237.  *
  238.  * Try to set @mode on @crtc.  Give @crtc and its associated connectors a chance
  239.  * to fixup or reject the mode prior to trying to set it. This is an internal
  240.  * helper that drivers could e.g. use to update properties that require the
  241.  * entire output pipe to be disabled and re-enabled in a new configuration. For
  242.  * example for changing whether audio is enabled on a hdmi link or for changing
  243.  * panel fitter or dither attributes. It is also called by the
  244.  * drm_crtc_helper_set_config() helper function to drive the mode setting
  245.  * sequence.
  246.  *
  247.  * Returns:
  248.  * True if the mode was set successfully, false otherwise.
  249.  */
  250. bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
  251.                               struct drm_display_mode *mode,
  252.                               int x, int y,
  253.                               struct drm_framebuffer *old_fb)
  254. {
  255.         struct drm_device *dev = crtc->dev;
  256.         struct drm_display_mode *adjusted_mode, saved_mode;
  257.         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  258.         struct drm_encoder_helper_funcs *encoder_funcs;
  259.         int saved_x, saved_y;
  260.         bool saved_enabled;
  261.         struct drm_encoder *encoder;
  262.         bool ret = true;
  263.  
  264.         drm_warn_on_modeset_not_all_locked(dev);
  265.  
  266.         saved_enabled = crtc->enabled;
  267.         crtc->enabled = drm_helper_crtc_in_use(crtc);
  268.         if (!crtc->enabled)
  269.                 return true;
  270.  
  271.         adjusted_mode = drm_mode_duplicate(dev, mode);
  272.         if (!adjusted_mode) {
  273.                 crtc->enabled = saved_enabled;
  274.                 return false;
  275.         }
  276.  
  277.         saved_mode = crtc->mode;
  278.         saved_x = crtc->x;
  279.         saved_y = crtc->y;
  280.  
  281.         /* Update crtc values up front so the driver can rely on them for mode
  282.          * setting.
  283.          */
  284.         crtc->mode = *mode;
  285.         crtc->x = x;
  286.         crtc->y = y;
  287.  
  288.         /* Pass our mode to the connectors and the CRTC to give them a chance to
  289.          * adjust it according to limitations or connector properties, and also
  290.          * a chance to reject the mode entirely.
  291.          */
  292.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  293.  
  294.                 if (encoder->crtc != crtc)
  295.                         continue;
  296.  
  297.                 if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
  298.                         ret = encoder->bridge->funcs->mode_fixup(
  299.                                         encoder->bridge, mode, adjusted_mode);
  300.                         if (!ret) {
  301.                                 DRM_DEBUG_KMS("Bridge fixup failed\n");
  302.                                 goto done;
  303.                         }
  304.                 }
  305.  
  306.                 encoder_funcs = encoder->helper_private;
  307.                 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
  308.                                                       adjusted_mode))) {
  309.                         DRM_DEBUG_KMS("Encoder fixup failed\n");
  310.                         goto done;
  311.                 }
  312.         }
  313.  
  314.         if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
  315.                 DRM_DEBUG_KMS("CRTC fixup failed\n");
  316.                 goto done;
  317.         }
  318.         DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  319.  
  320.         /* Prepare the encoders and CRTCs before setting the mode. */
  321.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  322.  
  323.                 if (encoder->crtc != crtc)
  324.                         continue;
  325.  
  326.                 if (encoder->bridge)
  327.                         encoder->bridge->funcs->disable(encoder->bridge);
  328.  
  329.                 encoder_funcs = encoder->helper_private;
  330.                 /* Disable the encoders as the first thing we do. */
  331.                 encoder_funcs->prepare(encoder);
  332.  
  333.                 if (encoder->bridge)
  334.                         encoder->bridge->funcs->post_disable(encoder->bridge);
  335.         }
  336.  
  337.         drm_crtc_prepare_encoders(dev);
  338.  
  339.         crtc_funcs->prepare(crtc);
  340.  
  341.         /* Set up the DPLL and any encoders state that needs to adjust or depend
  342.          * on the DPLL.
  343.          */
  344.         ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
  345.         if (!ret)
  346.             goto done;
  347.  
  348.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  349.  
  350.                 if (encoder->crtc != crtc)
  351.                         continue;
  352.  
  353.                 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
  354.                         encoder->base.id, encoder->name,
  355.                         mode->base.id, mode->name);
  356.                 encoder_funcs = encoder->helper_private;
  357.                 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
  358.  
  359.                 if (encoder->bridge && encoder->bridge->funcs->mode_set)
  360.                         encoder->bridge->funcs->mode_set(encoder->bridge, mode,
  361.                                         adjusted_mode);
  362.         }
  363.  
  364.         /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  365.         crtc_funcs->commit(crtc);
  366.  
  367.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  368.  
  369.                 if (encoder->crtc != crtc)
  370.                         continue;
  371.  
  372.                 if (encoder->bridge)
  373.                         encoder->bridge->funcs->pre_enable(encoder->bridge);
  374.  
  375.                 encoder_funcs = encoder->helper_private;
  376.                 encoder_funcs->commit(encoder);
  377.  
  378.                 if (encoder->bridge)
  379.                         encoder->bridge->funcs->enable(encoder->bridge);
  380.         }
  381.  
  382.         /* Store real post-adjustment hardware mode. */
  383.         crtc->hwmode = *adjusted_mode;
  384.  
  385.         /* Calculate and store various constants which
  386.          * are later needed by vblank and swap-completion
  387.          * timestamping. They are derived from true hwmode.
  388.          */
  389.         drm_calc_timestamping_constants(crtc, &crtc->hwmode);
  390.  
  391.         /* FIXME: add subpixel order */
  392. done:
  393.         drm_mode_destroy(dev, adjusted_mode);
  394.         if (!ret) {
  395.                 crtc->enabled = saved_enabled;
  396.                 crtc->mode = saved_mode;
  397.                 crtc->x = saved_x;
  398.                 crtc->y = saved_y;
  399.         }
  400.  
  401.         return ret;
  402. }
  403. EXPORT_SYMBOL(drm_crtc_helper_set_mode);
  404.  
  405. static void
  406. drm_crtc_helper_disable(struct drm_crtc *crtc)
  407. {
  408.         struct drm_device *dev = crtc->dev;
  409.         struct drm_connector *connector;
  410.         struct drm_encoder *encoder;
  411.  
  412.         /* Decouple all encoders and their attached connectors from this crtc */
  413.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  414.                 if (encoder->crtc != crtc)
  415.                         continue;
  416.  
  417.                 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  418.                         if (connector->encoder != encoder)
  419.                                 continue;
  420.  
  421.                         connector->encoder = NULL;
  422.  
  423.                         /*
  424.                          * drm_helper_disable_unused_functions() ought to be
  425.                          * doing this, but since we've decoupled the encoder
  426.                          * from the connector above, the required connection
  427.                          * between them is henceforth no longer available.
  428.                          */
  429.                         connector->dpms = DRM_MODE_DPMS_OFF;
  430.                 }
  431.         }
  432.  
  433.         __drm_helper_disable_unused_functions(dev);
  434. }
  435.  
  436. /**
  437.  * drm_crtc_helper_set_config - set a new config from userspace
  438.  * @set: mode set configuration
  439.  *
  440.  * Setup a new configuration, provided by the upper layers (either an ioctl call
  441.  * from userspace or internally e.g. from the fbdev support code) in @set, and
  442.  * enable it. This is the main helper functions for drivers that implement
  443.  * kernel mode setting with the crtc helper functions and the assorted
  444.  * ->prepare(), ->modeset() and ->commit() helper callbacks.
  445.  *
  446.  * Returns:
  447.  * Returns 0 on success, negative errno numbers on failure.
  448.  */
  449. int drm_crtc_helper_set_config(struct drm_mode_set *set)
  450. {
  451.         struct drm_device *dev;
  452.         struct drm_crtc *new_crtc;
  453.         struct drm_encoder *save_encoders, *new_encoder, *encoder;
  454.         bool mode_changed = false; /* if true do a full mode set */
  455.         bool fb_changed = false; /* if true and !mode_changed just do a flip */
  456.         struct drm_connector *save_connectors, *connector;
  457.         int count = 0, ro, fail = 0;
  458.         struct drm_crtc_helper_funcs *crtc_funcs;
  459.         struct drm_mode_set save_set;
  460.         int ret;
  461.         int i;
  462.  
  463.         DRM_DEBUG_KMS("\n");
  464.  
  465.         BUG_ON(!set);
  466.         BUG_ON(!set->crtc);
  467.         BUG_ON(!set->crtc->helper_private);
  468.  
  469.         /* Enforce sane interface api - has been abused by the fb helper. */
  470.         BUG_ON(!set->mode && set->fb);
  471.         BUG_ON(set->fb && set->num_connectors == 0);
  472.  
  473.         crtc_funcs = set->crtc->helper_private;
  474.  
  475.         if (!set->mode)
  476.                 set->fb = NULL;
  477.  
  478.         if (set->fb) {
  479.                 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  480.                                 set->crtc->base.id, set->fb->base.id,
  481.                   (int)set->num_connectors, set->x, set->y);
  482.         } else {
  483.                 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  484.                 drm_crtc_helper_disable(set->crtc);
  485.                 return 0;
  486.         }
  487.  
  488.         dev = set->crtc->dev;
  489.  
  490.         drm_warn_on_modeset_not_all_locked(dev);
  491.  
  492.         /*
  493.          * Allocate space for the backup of all (non-pointer) encoder and
  494.          * connector data.
  495.          */
  496.         save_encoders = kzalloc(dev->mode_config.num_encoder *
  497.                                 sizeof(struct drm_encoder), GFP_KERNEL);
  498.         if (!save_encoders)
  499.                 return -ENOMEM;
  500.  
  501.         save_connectors = kzalloc(dev->mode_config.num_connector *
  502.                                 sizeof(struct drm_connector), GFP_KERNEL);
  503.         if (!save_connectors) {
  504.                 kfree(save_encoders);
  505.                 return -ENOMEM;
  506.         }
  507.  
  508.         /*
  509.          * Copy data. Note that driver private data is not affected.
  510.          * Should anything bad happen only the expected state is
  511.          * restored, not the drivers personal bookkeeping.
  512.          */
  513.         count = 0;
  514.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  515.                 save_encoders[count++] = *encoder;
  516.         }
  517.  
  518.         count = 0;
  519.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  520.                 save_connectors[count++] = *connector;
  521.         }
  522.  
  523.         save_set.crtc = set->crtc;
  524.         save_set.mode = &set->crtc->mode;
  525.         save_set.x = set->crtc->x;
  526.         save_set.y = set->crtc->y;
  527.         save_set.fb = set->crtc->primary->fb;
  528.  
  529.         /* We should be able to check here if the fb has the same properties
  530.          * and then just flip_or_move it */
  531.         if (set->crtc->primary->fb != set->fb) {
  532.                 /* If we have no fb then treat it as a full mode set */
  533.                 if (set->crtc->primary->fb == NULL) {
  534.                         DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
  535.                         mode_changed = true;
  536.                 } else if (set->fb == NULL) {
  537.                         mode_changed = true;
  538.                 } else if (set->fb->pixel_format !=
  539.                            set->crtc->primary->fb->pixel_format) {
  540.                         mode_changed = true;
  541.                 } else
  542.                         fb_changed = true;
  543.         }
  544.  
  545.         if (set->x != set->crtc->x || set->y != set->crtc->y)
  546.                 fb_changed = true;
  547.  
  548.         if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  549.                 DRM_DEBUG_KMS("modes are different, full mode set\n");
  550.                 drm_mode_debug_printmodeline(&set->crtc->mode);
  551.                 drm_mode_debug_printmodeline(set->mode);
  552.                 mode_changed = true;
  553.         }
  554.  
  555.         /* a) traverse passed in connector list and get encoders for them */
  556.         count = 0;
  557.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  558.                 struct drm_connector_helper_funcs *connector_funcs =
  559.                         connector->helper_private;
  560.                 new_encoder = connector->encoder;
  561.                 for (ro = 0; ro < set->num_connectors; ro++) {
  562.                         if (set->connectors[ro] == connector) {
  563.                                 new_encoder = connector_funcs->best_encoder(connector);
  564.                                 /* if we can't get an encoder for a connector
  565.                                    we are setting now - then fail */
  566.                                 if (new_encoder == NULL)
  567.                                         /* don't break so fail path works correct */
  568.                                         fail = 1;
  569.  
  570.                                 if (connector->dpms != DRM_MODE_DPMS_ON) {
  571.                                         DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
  572.                                         mode_changed = true;
  573.                                 }
  574.  
  575.                                 break;
  576.                         }
  577.                 }
  578.  
  579.                 if (new_encoder != connector->encoder) {
  580.                         DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  581.                         mode_changed = true;
  582.                         /* If the encoder is reused for another connector, then
  583.                          * the appropriate crtc will be set later.
  584.                          */
  585.                         if (connector->encoder)
  586.                                 connector->encoder->crtc = NULL;
  587.                         connector->encoder = new_encoder;
  588.                 }
  589.         }
  590.  
  591.         if (fail) {
  592.                 ret = -EINVAL;
  593.                 goto fail;
  594.         }
  595.  
  596.         count = 0;
  597.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  598.                 if (!connector->encoder)
  599.                         continue;
  600.  
  601.                 if (connector->encoder->crtc == set->crtc)
  602.                         new_crtc = NULL;
  603.                 else
  604.                         new_crtc = connector->encoder->crtc;
  605.  
  606.                 for (ro = 0; ro < set->num_connectors; ro++) {
  607.                         if (set->connectors[ro] == connector)
  608.                                 new_crtc = set->crtc;
  609.                 }
  610.  
  611.                 /* Make sure the new CRTC will work with the encoder */
  612.                 if (new_crtc &&
  613.                     !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
  614.                         ret = -EINVAL;
  615.                         goto fail;
  616.                 }
  617.                 if (new_crtc != connector->encoder->crtc) {
  618.                         DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  619.                         mode_changed = true;
  620.                         connector->encoder->crtc = new_crtc;
  621.                 }
  622.                 if (new_crtc) {
  623.                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  624.                                 connector->base.id, connector->name,
  625.                                 new_crtc->base.id);
  626.                 } else {
  627.                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  628.                                 connector->base.id, connector->name);
  629.                 }
  630.         }
  631.  
  632.         /* mode_set_base is not a required function */
  633.         if (fb_changed && !crtc_funcs->mode_set_base)
  634.                 mode_changed = true;
  635.  
  636.         if (mode_changed) {
  637.                 if (drm_helper_crtc_in_use(set->crtc)) {
  638.                         DRM_DEBUG_KMS("attempting to set mode from"
  639.                                         " userspace\n");
  640.                         drm_mode_debug_printmodeline(set->mode);
  641.                         set->crtc->primary->fb = set->fb;
  642.                         if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  643.                                                       set->x, set->y,
  644.                                                       save_set.fb)) {
  645.                                 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
  646.                                           set->crtc->base.id);
  647.                                 set->crtc->primary->fb = save_set.fb;
  648.                                 ret = -EINVAL;
  649.                                 goto fail;
  650.                         }
  651.                         DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
  652.                         for (i = 0; i < set->num_connectors; i++) {
  653.                                 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
  654.                                               set->connectors[i]->name);
  655.                                 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
  656.                         }
  657.                 }
  658.                 __drm_helper_disable_unused_functions(dev);
  659.         } else if (fb_changed) {
  660.                 set->crtc->x = set->x;
  661.                 set->crtc->y = set->y;
  662.                 set->crtc->primary->fb = set->fb;
  663.                 ret = crtc_funcs->mode_set_base(set->crtc,
  664.                                                 set->x, set->y, save_set.fb);
  665.                 if (ret != 0) {
  666.                         set->crtc->x = save_set.x;
  667.                         set->crtc->y = save_set.y;
  668.                         set->crtc->primary->fb = save_set.fb;
  669.                         goto fail;
  670.         }
  671.         }
  672.  
  673.         kfree(save_connectors);
  674.         kfree(save_encoders);
  675.         return 0;
  676.  
  677. fail:
  678.         /* Restore all previous data. */
  679.         count = 0;
  680.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  681.                 *encoder = save_encoders[count++];
  682.         }
  683.  
  684.         count = 0;
  685.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  686.                 *connector = save_connectors[count++];
  687.         }
  688.  
  689.         /* Try to restore the config */
  690.         if (mode_changed &&
  691.             !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
  692.                                       save_set.y, save_set.fb))
  693.                 DRM_ERROR("failed to restore config after modeset failure\n");
  694.  
  695.         kfree(save_connectors);
  696.         kfree(save_encoders);
  697.         return ret;
  698. }
  699. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  700.  
  701. static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
  702. {
  703.         int dpms = DRM_MODE_DPMS_OFF;
  704.         struct drm_connector *connector;
  705.         struct drm_device *dev = encoder->dev;
  706.  
  707.         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  708.                 if (connector->encoder == encoder)
  709.                         if (connector->dpms < dpms)
  710.                                 dpms = connector->dpms;
  711.         return dpms;
  712. }
  713.  
  714. /* Helper which handles bridge ordering around encoder dpms */
  715. static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
  716. {
  717.         struct drm_bridge *bridge = encoder->bridge;
  718.         struct drm_encoder_helper_funcs *encoder_funcs;
  719.  
  720.         if (bridge) {
  721.                 if (mode == DRM_MODE_DPMS_ON)
  722.                         bridge->funcs->pre_enable(bridge);
  723.                 else
  724.                         bridge->funcs->disable(bridge);
  725.         }
  726.  
  727.         encoder_funcs = encoder->helper_private;
  728.         if (encoder_funcs->dpms)
  729.                 encoder_funcs->dpms(encoder, mode);
  730.  
  731.         if (bridge) {
  732.                 if (mode == DRM_MODE_DPMS_ON)
  733.                         bridge->funcs->enable(bridge);
  734.                 else
  735.                         bridge->funcs->post_disable(bridge);
  736.         }
  737. }
  738.  
  739. static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
  740. {
  741.         int dpms = DRM_MODE_DPMS_OFF;
  742.         struct drm_connector *connector;
  743.         struct drm_device *dev = crtc->dev;
  744.  
  745.         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  746.                 if (connector->encoder && connector->encoder->crtc == crtc)
  747.                         if (connector->dpms < dpms)
  748.                                 dpms = connector->dpms;
  749.         return dpms;
  750. }
  751.  
  752. /**
  753.  * drm_helper_connector_dpms() - connector dpms helper implementation
  754.  * @connector: affected connector
  755.  * @mode: DPMS mode
  756.  *
  757.  * This is the main helper function provided by the crtc helper framework for
  758.  * implementing the DPMS connector attribute. It computes the new desired DPMS
  759.  * state for all encoders and crtcs in the output mesh and calls the ->dpms()
  760.  * callback provided by the driver appropriately.
  761.  */
  762. void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
  763. {
  764.         struct drm_encoder *encoder = connector->encoder;
  765.         struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  766.         int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
  767.  
  768.         if (mode == connector->dpms)
  769.                 return;
  770.  
  771.         old_dpms = connector->dpms;
  772.         connector->dpms = mode;
  773.  
  774.         if (encoder)
  775.                 encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
  776.  
  777.         /* from off to on, do crtc then encoder */
  778.         if (mode < old_dpms) {
  779.                 if (crtc) {
  780.                         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  781.                         if (crtc_funcs->dpms)
  782.                                 (*crtc_funcs->dpms) (crtc,
  783.                                                      drm_helper_choose_crtc_dpms(crtc));
  784.                 }
  785.                 if (encoder)
  786.                         drm_helper_encoder_dpms(encoder, encoder_dpms);
  787.         }
  788.  
  789.         /* from on to off, do encoder then crtc */
  790.         if (mode > old_dpms) {
  791.                 if (encoder)
  792.                         drm_helper_encoder_dpms(encoder, encoder_dpms);
  793.                 if (crtc) {
  794.                         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  795.                         if (crtc_funcs->dpms)
  796.                                 (*crtc_funcs->dpms) (crtc,
  797.                                                      drm_helper_choose_crtc_dpms(crtc));
  798.                 }
  799.         }
  800.  
  801.         return;
  802. }
  803. EXPORT_SYMBOL(drm_helper_connector_dpms);
  804.  
  805. /**
  806.  * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata
  807.  * @fb: drm_framebuffer object to fill out
  808.  * @mode_cmd: metadata from the userspace fb creation request
  809.  *
  810.  * This helper can be used in a drivers fb_create callback to pre-fill the fb's
  811.  * metadata fields.
  812.  */
  813. void drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
  814.                                    struct drm_mode_fb_cmd2 *mode_cmd)
  815. {
  816.         int i;
  817.  
  818.         fb->width = mode_cmd->width;
  819.         fb->height = mode_cmd->height;
  820.         for (i = 0; i < 4; i++) {
  821.                 fb->pitches[i] = mode_cmd->pitches[i];
  822.                 fb->offsets[i] = mode_cmd->offsets[i];
  823.         }
  824.         drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
  825.                                     &fb->bits_per_pixel);
  826.         fb->pixel_format = mode_cmd->pixel_format;
  827.         fb->flags = mode_cmd->flags;
  828. }
  829. EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
  830.  
  831. /**
  832.  * drm_helper_resume_force_mode - force-restore mode setting configuration
  833.  * @dev: drm_device which should be restored
  834.  *
  835.  * Drivers which use the mode setting helpers can use this function to
  836.  * force-restore the mode setting configuration e.g. on resume or when something
  837.  * else might have trampled over the hw state (like some overzealous old BIOSen
  838.  * tended to do).
  839.  *
  840.  * This helper doesn't provide a error return value since restoring the old
  841.  * config should never fail due to resource allocation issues since the driver
  842.  * has successfully set the restored configuration already. Hence this should
  843.  * boil down to the equivalent of a few dpms on calls, which also don't provide
  844.  * an error code.
  845.  *
  846.  * Drivers where simply restoring an old configuration again might fail (e.g.
  847.  * due to slight differences in allocating shared resources when the
  848.  * configuration is restored in a different order than when userspace set it up)
  849.  * need to use their own restore logic.
  850.  */
  851. void drm_helper_resume_force_mode(struct drm_device *dev)
  852. {
  853.         struct drm_crtc *crtc;
  854.         struct drm_encoder *encoder;
  855.         struct drm_crtc_helper_funcs *crtc_funcs;
  856.         int encoder_dpms;
  857.         bool ret;
  858.  
  859.         drm_modeset_lock_all(dev);
  860.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  861.  
  862.        if (!crtc->enabled)
  863.            continue;
  864.  
  865.                 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
  866.                                                crtc->x, crtc->y, crtc->primary->fb);
  867.  
  868.                 /* Restoring the old config should never fail! */
  869.                 if (ret == false)
  870.                         DRM_ERROR("failed to set mode on crtc %p\n", crtc);
  871.  
  872.                 /* Turn off outputs that were already powered off */
  873.                 if (drm_helper_choose_crtc_dpms(crtc)) {
  874.                         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  875.  
  876.                                 if(encoder->crtc != crtc)
  877.                                         continue;
  878.  
  879.                                 encoder_dpms = drm_helper_choose_encoder_dpms(
  880.                                                         encoder);
  881.  
  882.                                 drm_helper_encoder_dpms(encoder, encoder_dpms);
  883.                         }
  884.  
  885.                                 crtc_funcs = crtc->helper_private;
  886.                                 if (crtc_funcs->dpms)
  887.                                         (*crtc_funcs->dpms) (crtc,
  888.                                                              drm_helper_choose_crtc_dpms(crtc));
  889.                         }
  890.                 }
  891.  
  892.         /* disable the unused connectors while restoring the modesetting */
  893.         __drm_helper_disable_unused_functions(dev);
  894.         drm_modeset_unlock_all(dev);
  895. }
  896. EXPORT_SYMBOL(drm_helper_resume_force_mode);
  897.  
  898. /**
  899.  * drm_helper_crtc_mode_set - mode_set implementation for atomic plane helpers
  900.  * @crtc: DRM CRTC
  901.  * @mode: DRM display mode which userspace requested
  902.  * @adjusted_mode: DRM display mode adjusted by ->mode_fixup callbacks
  903.  * @x: x offset of the CRTC scanout area on the underlying framebuffer
  904.  * @y: y offset of the CRTC scanout area on the underlying framebuffer
  905.  * @old_fb: previous framebuffer
  906.  *
  907.  * This function implements a callback useable as the ->mode_set callback
  908.  * required by the crtc helpers. Besides the atomic plane helper functions for
  909.  * the primary plane the driver must also provide the ->mode_set_nofb callback
  910.  * to set up the crtc.
  911.  *
  912.  * This is a transitional helper useful for converting drivers to the atomic
  913.  * interfaces.
  914.  */
  915. int drm_helper_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
  916.                              struct drm_display_mode *adjusted_mode, int x, int y,
  917.                              struct drm_framebuffer *old_fb)
  918. {
  919.         struct drm_crtc_state *crtc_state;
  920.         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  921.         int ret;
  922.  
  923.         if (crtc->funcs->atomic_duplicate_state)
  924.                 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
  925.         else if (crtc->state)
  926.                 crtc_state = kmemdup(crtc->state, sizeof(*crtc_state),
  927.                                      GFP_KERNEL);
  928.         else
  929.                 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  930.         if (!crtc_state)
  931.                 return -ENOMEM;
  932.  
  933.         crtc_state->enable = true;
  934.         crtc_state->planes_changed = true;
  935.         crtc_state->mode_changed = true;
  936.         drm_mode_copy(&crtc_state->mode, mode);
  937.         drm_mode_copy(&crtc_state->adjusted_mode, adjusted_mode);
  938.  
  939.         if (crtc_funcs->atomic_check) {
  940.                 ret = crtc_funcs->atomic_check(crtc, crtc_state);
  941.                 if (ret) {
  942.                         kfree(crtc_state);
  943.  
  944.                         return ret;
  945.                 }
  946.         }
  947.  
  948.         swap(crtc->state, crtc_state);
  949.  
  950.         crtc_funcs->mode_set_nofb(crtc);
  951.  
  952.         if (crtc_state) {
  953.                 if (crtc->funcs->atomic_destroy_state)
  954.                         crtc->funcs->atomic_destroy_state(crtc, crtc_state);
  955.                 else
  956.                         kfree(crtc_state);
  957.         }
  958.  
  959.         return drm_helper_crtc_mode_set_base(crtc, x, y, old_fb);
  960. }
  961. EXPORT_SYMBOL(drm_helper_crtc_mode_set);
  962.  
  963. /**
  964.  * drm_helper_crtc_mode_set_base - mode_set_base implementation for atomic plane helpers
  965.  * @crtc: DRM CRTC
  966.  * @x: x offset of the CRTC scanout area on the underlying framebuffer
  967.  * @y: y offset of the CRTC scanout area on the underlying framebuffer
  968.  * @old_fb: previous framebuffer
  969.  *
  970.  * This function implements a callback useable as the ->mode_set_base used
  971.  * required by the crtc helpers. The driver must provide the atomic plane helper
  972.  * functions for the primary plane.
  973.  *
  974.  * This is a transitional helper useful for converting drivers to the atomic
  975.  * interfaces.
  976.  */
  977. int drm_helper_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  978.                                   struct drm_framebuffer *old_fb)
  979. {
  980.         struct drm_plane_state *plane_state;
  981.         struct drm_plane *plane = crtc->primary;
  982.  
  983.         if (plane->funcs->atomic_duplicate_state)
  984.                 plane_state = plane->funcs->atomic_duplicate_state(plane);
  985.         else if (plane->state)
  986.                 plane_state = drm_atomic_helper_plane_duplicate_state(plane);
  987.         else
  988.                 plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
  989.         if (!plane_state)
  990.                 return -ENOMEM;
  991.  
  992.         plane_state->crtc = crtc;
  993.         drm_atomic_set_fb_for_plane(plane_state, crtc->primary->fb);
  994.         plane_state->crtc_x = 0;
  995.         plane_state->crtc_y = 0;
  996.         plane_state->crtc_h = crtc->mode.vdisplay;
  997.         plane_state->crtc_w = crtc->mode.hdisplay;
  998.         plane_state->src_x = x << 16;
  999.         plane_state->src_y = y << 16;
  1000.         plane_state->src_h = crtc->mode.vdisplay << 16;
  1001.         plane_state->src_w = crtc->mode.hdisplay << 16;
  1002.  
  1003.         return drm_plane_helper_commit(plane, plane_state, old_fb);
  1004. }
  1005. EXPORT_SYMBOL(drm_helper_crtc_mode_set_base);
  1006.