Subversion Repositories Kolibri OS

Rev

Rev 3051 | Rev 3243 | 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/export.h>
  33. #include <linux/moduleparam.h>
  34.  
  35. #include <drm/drmP.h>
  36. #include <drm/drm_crtc.h>
  37. #include <drm/drm_fourcc.h>
  38. #include <drm/drm_crtc_helper.h>
  39. #include <drm/drm_fb_helper.h>
  40. #include <drm/drm_edid.h>
  41.  
  42. /**
  43.  * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
  44.  *                                              connector list
  45.  * @dev: drm device to operate on
  46.  *
  47.  * Some userspace presumes that the first connected connector is the main
  48.  * display, where it's supposed to display e.g. the login screen. For
  49.  * laptops, this should be the main panel. Use this function to sort all
  50.  * (eDP/LVDS) panels to the front of the connector list, instead of
  51.  * painstakingly trying to initialize them in the right order.
  52.  */
  53. void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
  54. {
  55.         struct drm_connector *connector, *tmp;
  56.         struct list_head panel_list;
  57.  
  58.         INIT_LIST_HEAD(&panel_list);
  59.  
  60.         list_for_each_entry_safe(connector, tmp,
  61.                                  &dev->mode_config.connector_list, head) {
  62.                 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
  63.                     connector->connector_type == DRM_MODE_CONNECTOR_eDP)
  64.                         list_move_tail(&connector->head, &panel_list);
  65.         }
  66.  
  67.         list_splice(&panel_list, &dev->mode_config.connector_list);
  68. }
  69. EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
  70.  
  71. static bool drm_kms_helper_poll = true;
  72.  
  73. static void drm_mode_validate_flag(struct drm_connector *connector,
  74.                                    int flags)
  75. {
  76.         struct drm_display_mode *mode;
  77.  
  78.         if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
  79.                 return;
  80.  
  81.         list_for_each_entry(mode, &connector->modes, head) {
  82.                 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
  83.                                 !(flags & DRM_MODE_FLAG_INTERLACE))
  84.                         mode->status = MODE_NO_INTERLACE;
  85.                 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
  86.                                 !(flags & DRM_MODE_FLAG_DBLSCAN))
  87.                         mode->status = MODE_NO_DBLESCAN;
  88.         }
  89.  
  90.         return;
  91. }
  92.  
  93. /**
  94.  * drm_helper_probe_single_connector_modes - get complete set of display modes
  95.  * @connector: connector to probe
  96.  * @maxX: max width for modes
  97.  * @maxY: max height for modes
  98.  *
  99.  * LOCKING:
  100.  * Caller must hold mode config lock.
  101.  *
  102.  * Based on the helper callbacks implemented by @connector try to detect all
  103.  * valid modes.  Modes will first be added to the connector's probed_modes list,
  104.  * then culled (based on validity and the @maxX, @maxY parameters) and put into
  105.  * the normal modes list.
  106.  *
  107.  * Intended to be use as a generic implementation of the ->probe() @connector
  108.  * callback for drivers that use the crtc helpers for output mode filtering and
  109.  * detection.
  110.  *
  111.  * RETURNS:
  112.  * Number of modes found on @connector.
  113.  */
  114. int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
  115.                                             uint32_t maxX, uint32_t maxY)
  116. {
  117.         struct drm_device *dev = connector->dev;
  118.         struct drm_display_mode *mode;
  119.         struct drm_connector_helper_funcs *connector_funcs =
  120.                 connector->helper_private;
  121.         int count = 0;
  122.         int mode_flags = 0;
  123.  
  124.         DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
  125.                         drm_get_connector_name(connector));
  126.         /* set all modes to the unverified state */
  127.         list_for_each_entry(mode, &connector->modes, head)
  128.                 mode->status = MODE_UNVERIFIED;
  129.  
  130.         if (connector->force) {
  131.                 if (connector->force == DRM_FORCE_ON)
  132.                         connector->status = connector_status_connected;
  133.                 else
  134.                         connector->status = connector_status_disconnected;
  135.                 if (connector->funcs->force)
  136.                         connector->funcs->force(connector);
  137.         } else {
  138.                 connector->status = connector->funcs->detect(connector, true);
  139. //              drm_kms_helper_poll_enable(dev);
  140.         }
  141.  
  142.         if (connector->status == connector_status_disconnected) {
  143.                 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
  144.                         connector->base.id, drm_get_connector_name(connector));
  145.                 drm_mode_connector_update_edid_property(connector, NULL);
  146.                 goto prune;
  147.         }
  148.  
  149. #ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE
  150.         count = drm_load_edid_firmware(connector);
  151.         if (count == 0)
  152. #endif
  153.         count = (*connector_funcs->get_modes)(connector);
  154.  
  155.         if (count == 0 && connector->status == connector_status_connected)
  156.                 count = drm_add_modes_noedid(connector, 1024, 768);
  157.         if (count == 0)
  158.                 goto prune;
  159.  
  160.         drm_mode_connector_list_update(connector);
  161.  
  162.         if (maxX && maxY)
  163.                 drm_mode_validate_size(dev, &connector->modes, maxX,
  164.                                        maxY, 0);
  165.  
  166.         if (connector->interlace_allowed)
  167.                 mode_flags |= DRM_MODE_FLAG_INTERLACE;
  168.         if (connector->doublescan_allowed)
  169.                 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
  170.         drm_mode_validate_flag(connector, mode_flags);
  171.  
  172.         list_for_each_entry(mode, &connector->modes, head) {
  173.                 if (mode->status == MODE_OK)
  174.                         mode->status = connector_funcs->mode_valid(connector,
  175.                                                                    mode);
  176.         }
  177.  
  178. prune:
  179.         drm_mode_prune_invalid(dev, &connector->modes, true);
  180.  
  181.         if (list_empty(&connector->modes))
  182.                 return 0;
  183.  
  184.         drm_mode_sort(&connector->modes);
  185.  
  186.         DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
  187.                                 drm_get_connector_name(connector));
  188.         list_for_each_entry(mode, &connector->modes, head) {
  189.                 mode->vrefresh = drm_mode_vrefresh(mode);
  190.  
  191.                 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  192.                 drm_mode_debug_printmodeline(mode);
  193.         }
  194.  
  195.         return count;
  196. }
  197. EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
  198.  
  199. /**
  200.  * drm_helper_encoder_in_use - check if a given encoder is in use
  201.  * @encoder: encoder to check
  202.  *
  203.  * LOCKING:
  204.  * Caller must hold mode config lock.
  205.  *
  206.  * Walk @encoders's DRM device's mode_config and see if it's in use.
  207.  *
  208.  * RETURNS:
  209.  * True if @encoder is part of the mode_config, false otherwise.
  210.  */
  211. bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
  212. {
  213.         struct drm_connector *connector;
  214.         struct drm_device *dev = encoder->dev;
  215.         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  216.                 if (connector->encoder == encoder)
  217.                         return true;
  218.         return false;
  219. }
  220. EXPORT_SYMBOL(drm_helper_encoder_in_use);
  221.  
  222. /**
  223.  * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
  224.  * @crtc: CRTC to check
  225.  *
  226.  * LOCKING:
  227.  * Caller must hold mode config lock.
  228.  *
  229.  * Walk @crtc's DRM device's mode_config and see if it's in use.
  230.  *
  231.  * RETURNS:
  232.  * True if @crtc is part of the mode_config, false otherwise.
  233.  */
  234. bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
  235. {
  236.         struct drm_encoder *encoder;
  237.         struct drm_device *dev = crtc->dev;
  238.         /* FIXME: Locking around list access? */
  239.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  240.                 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
  241.                         return true;
  242.         return false;
  243. }
  244. EXPORT_SYMBOL(drm_helper_crtc_in_use);
  245.  
  246. static void
  247. drm_encoder_disable(struct drm_encoder *encoder)
  248. {
  249.         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  250.  
  251.         if (encoder_funcs->disable)
  252.                 (*encoder_funcs->disable)(encoder);
  253.         else
  254.                 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
  255. }
  256.  
  257. /**
  258.  * drm_helper_disable_unused_functions - disable unused objects
  259.  * @dev: DRM device
  260.  *
  261.  * LOCKING:
  262.  * Caller must hold mode config lock.
  263.  *
  264.  * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
  265.  * by calling its dpms function, which should power it off.
  266.  */
  267. void drm_helper_disable_unused_functions(struct drm_device *dev)
  268. {
  269.         struct drm_encoder *encoder;
  270.         struct drm_connector *connector;
  271.         struct drm_crtc *crtc;
  272.  
  273.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  274.                 if (!connector->encoder)
  275.                         continue;
  276.                 if (connector->status == connector_status_disconnected)
  277.                         connector->encoder = NULL;
  278.         }
  279.  
  280.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  281.                 if (!drm_helper_encoder_in_use(encoder)) {
  282.                         drm_encoder_disable(encoder);
  283.                         /* disconnector encoder from any connector */
  284.                         encoder->crtc = NULL;
  285.                 }
  286.         }
  287.  
  288.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  289.                 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  290.                 crtc->enabled = drm_helper_crtc_in_use(crtc);
  291.                 if (!crtc->enabled) {
  292.                         if (crtc_funcs->disable)
  293.                                 (*crtc_funcs->disable)(crtc);
  294.                         else
  295.                                 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
  296.                         crtc->fb = NULL;
  297.                 }
  298.         }
  299.  
  300. }
  301. EXPORT_SYMBOL(drm_helper_disable_unused_functions);
  302.  
  303. /**
  304.  * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
  305.  * @encoder: encoder to test
  306.  * @crtc: crtc to test
  307.  *
  308.  * Return false if @encoder can't be driven by @crtc, true otherwise.
  309.  */
  310. static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
  311.                                 struct drm_crtc *crtc)
  312. {
  313.         struct drm_device *dev;
  314.         struct drm_crtc *tmp;
  315.         int crtc_mask = 1;
  316.  
  317.         WARN(!crtc, "checking null crtc?\n");
  318.  
  319.         dev = crtc->dev;
  320.  
  321.         list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
  322.                 if (tmp == crtc)
  323.                         break;
  324.                 crtc_mask <<= 1;
  325.         }
  326.  
  327.         if (encoder->possible_crtcs & crtc_mask)
  328.                 return true;
  329.         return false;
  330. }
  331.  
  332. /*
  333.  * Check the CRTC we're going to map each output to vs. its current
  334.  * CRTC.  If they don't match, we have to disable the output and the CRTC
  335.  * since the driver will have to re-route things.
  336.  */
  337. static void
  338. drm_crtc_prepare_encoders(struct drm_device *dev)
  339. {
  340.         struct drm_encoder_helper_funcs *encoder_funcs;
  341.         struct drm_encoder *encoder;
  342.  
  343.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  344.                 encoder_funcs = encoder->helper_private;
  345.                 /* Disable unused encoders */
  346.                 if (encoder->crtc == NULL)
  347.                         drm_encoder_disable(encoder);
  348.                 /* Disable encoders whose CRTC is about to change */
  349.                 if (encoder_funcs->get_crtc &&
  350.                     encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
  351.                         drm_encoder_disable(encoder);
  352.         }
  353. }
  354.  
  355. /**
  356.  * drm_crtc_helper_set_mode - internal helper to set a mode
  357.  * @crtc: CRTC to program
  358.  * @mode: mode to use
  359.  * @x: horizontal offset into the surface
  360.  * @y: vertical offset into the surface
  361.  * @old_fb: old framebuffer, for cleanup
  362.  *
  363.  * LOCKING:
  364.  * Caller must hold mode config lock.
  365.  *
  366.  * Try to set @mode on @crtc.  Give @crtc and its associated connectors a chance
  367.  * to fixup or reject the mode prior to trying to set it. This is an internal
  368.  * helper that drivers could e.g. use to update properties that require the
  369.  * entire output pipe to be disabled and re-enabled in a new configuration. For
  370.  * example for changing whether audio is enabled on a hdmi link or for changing
  371.  * panel fitter or dither attributes. It is also called by the
  372.  * drm_crtc_helper_set_config() helper function to drive the mode setting
  373.  * sequence.
  374.  *
  375.  * RETURNS:
  376.  * True if the mode was set successfully, or false otherwise.
  377.  */
  378. bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
  379.                               struct drm_display_mode *mode,
  380.                               int x, int y,
  381.                               struct drm_framebuffer *old_fb)
  382. {
  383.         struct drm_device *dev = crtc->dev;
  384.         struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
  385.         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  386.         struct drm_encoder_helper_funcs *encoder_funcs;
  387.         int saved_x, saved_y;
  388.         struct drm_encoder *encoder;
  389.         bool ret = true;
  390.  
  391.         crtc->enabled = drm_helper_crtc_in_use(crtc);
  392.         if (!crtc->enabled)
  393.                 return true;
  394.  
  395.         adjusted_mode = drm_mode_duplicate(dev, mode);
  396.         if (!adjusted_mode)
  397.                 return false;
  398.  
  399.         saved_hwmode = crtc->hwmode;
  400.         saved_mode = crtc->mode;
  401.         saved_x = crtc->x;
  402.         saved_y = crtc->y;
  403.  
  404.         /* Update crtc values up front so the driver can rely on them for mode
  405.          * setting.
  406.          */
  407.         crtc->mode = *mode;
  408.         crtc->x = x;
  409.         crtc->y = y;
  410.  
  411.         /* Pass our mode to the connectors and the CRTC to give them a chance to
  412.          * adjust it according to limitations or connector properties, and also
  413.          * a chance to reject the mode entirely.
  414.          */
  415.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  416.  
  417.                 if (encoder->crtc != crtc)
  418.                         continue;
  419.                 encoder_funcs = encoder->helper_private;
  420.                 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
  421.                                                       adjusted_mode))) {
  422.                         DRM_DEBUG_KMS("Encoder fixup failed\n");
  423.                         goto done;
  424.                 }
  425.         }
  426.  
  427.         if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
  428.                 DRM_DEBUG_KMS("CRTC fixup failed\n");
  429.                 goto done;
  430.         }
  431.         DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  432.  
  433.         /* Prepare the encoders and CRTCs before setting the mode. */
  434.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  435.  
  436.                 if (encoder->crtc != crtc)
  437.                         continue;
  438.                 encoder_funcs = encoder->helper_private;
  439.                 /* Disable the encoders as the first thing we do. */
  440.                 encoder_funcs->prepare(encoder);
  441.         }
  442.  
  443.         drm_crtc_prepare_encoders(dev);
  444.  
  445.         crtc_funcs->prepare(crtc);
  446.  
  447.         /* Set up the DPLL and any encoders state that needs to adjust or depend
  448.          * on the DPLL.
  449.          */
  450.         ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
  451.         if (!ret)
  452.             goto done;
  453.  
  454.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  455.  
  456.                 if (encoder->crtc != crtc)
  457.                         continue;
  458.  
  459.                 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
  460.                         encoder->base.id, drm_get_encoder_name(encoder),
  461.                         mode->base.id, mode->name);
  462.                 encoder_funcs = encoder->helper_private;
  463.                 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
  464.         }
  465.  
  466.         /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  467.         crtc_funcs->commit(crtc);
  468.  
  469.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  470.  
  471.                 if (encoder->crtc != crtc)
  472.                         continue;
  473.  
  474.                 encoder_funcs = encoder->helper_private;
  475.                 encoder_funcs->commit(encoder);
  476.  
  477.         }
  478.  
  479.         /* Store real post-adjustment hardware mode. */
  480.         crtc->hwmode = *adjusted_mode;
  481.  
  482.         /* Calculate and store various constants which
  483.          * are later needed by vblank and swap-completion
  484.          * timestamping. They are derived from true hwmode.
  485.          */
  486.         drm_calc_timestamping_constants(crtc);
  487.  
  488.         /* FIXME: add subpixel order */
  489. done:
  490.         drm_mode_destroy(dev, adjusted_mode);
  491.         if (!ret) {
  492.                 crtc->hwmode = saved_hwmode;
  493.                 crtc->mode = saved_mode;
  494.                 crtc->x = saved_x;
  495.                 crtc->y = saved_y;
  496.         }
  497.  
  498.         return ret;
  499. }
  500. EXPORT_SYMBOL(drm_crtc_helper_set_mode);
  501.  
  502.  
  503. static int
  504. drm_crtc_helper_disable(struct drm_crtc *crtc)
  505. {
  506.         struct drm_device *dev = crtc->dev;
  507.         struct drm_connector *connector;
  508.         struct drm_encoder *encoder;
  509.  
  510.         /* Decouple all encoders and their attached connectors from this crtc */
  511.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  512.                 if (encoder->crtc != crtc)
  513.                         continue;
  514.  
  515.                 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  516.                         if (connector->encoder != encoder)
  517.                                 continue;
  518.  
  519.                         connector->encoder = NULL;
  520.                 }
  521.         }
  522.  
  523.         drm_helper_disable_unused_functions(dev);
  524.         return 0;
  525. }
  526.  
  527. /**
  528.  * drm_crtc_helper_set_config - set a new config from userspace
  529.  * @set: mode set configuration
  530.  *
  531.  * LOCKING:
  532.  * Caller must hold mode config lock.
  533.  *
  534.  * Setup a new configuration, provided by the upper layers (either an ioctl call
  535.  * from userspace or internally e.g. from the fbdev suppport code) in @set, and
  536.  * enable it. This is the main helper functions for drivers that implement
  537.  * kernel mode setting with the crtc helper functions and the assorted
  538.  * ->prepare(), ->modeset() and ->commit() helper callbacks.
  539.  *
  540.  * RETURNS:
  541.  * Returns 0 on success, -ERRNO on failure.
  542.  */
  543. int drm_crtc_helper_set_config(struct drm_mode_set *set)
  544. {
  545.         struct drm_device *dev;
  546.         struct drm_crtc *save_crtcs, *new_crtc, *crtc;
  547.         struct drm_encoder *save_encoders, *new_encoder, *encoder;
  548.         struct drm_framebuffer *old_fb = NULL;
  549.         bool mode_changed = false; /* if true do a full mode set */
  550.         bool fb_changed = false; /* if true and !mode_changed just do a flip */
  551.         struct drm_connector *save_connectors, *connector;
  552.         int count = 0, ro, fail = 0;
  553.         struct drm_crtc_helper_funcs *crtc_funcs;
  554.         struct drm_mode_set save_set;
  555.         int ret;
  556.         int i;
  557.  
  558.         DRM_DEBUG_KMS("\n");
  559.  
  560.         if (!set)
  561.                 return -EINVAL;
  562.  
  563.         if (!set->crtc)
  564.                 return -EINVAL;
  565.  
  566.         if (!set->crtc->helper_private)
  567.                 return -EINVAL;
  568.  
  569.         crtc_funcs = set->crtc->helper_private;
  570.  
  571.         if (!set->mode)
  572.                 set->fb = NULL;
  573.  
  574.         if (set->fb) {
  575.                 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  576.                                 set->crtc->base.id, set->fb->base.id,
  577.                   (int)set->num_connectors, set->x, set->y);
  578.         } else {
  579.                 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  580.                 return drm_crtc_helper_disable(set->crtc);
  581.         }
  582.  
  583.         dev = set->crtc->dev;
  584.  
  585.         /* Allocate space for the backup of all (non-pointer) crtc, encoder and
  586.          * connector data. */
  587.         save_crtcs = kzalloc(dev->mode_config.num_crtc *
  588.                              sizeof(struct drm_crtc), GFP_KERNEL);
  589.         if (!save_crtcs)
  590.                 return -ENOMEM;
  591.  
  592.         save_encoders = kzalloc(dev->mode_config.num_encoder *
  593.                                 sizeof(struct drm_encoder), GFP_KERNEL);
  594.         if (!save_encoders) {
  595.                 kfree(save_crtcs);
  596.                 return -ENOMEM;
  597.         }
  598.  
  599.         save_connectors = kzalloc(dev->mode_config.num_connector *
  600.                                 sizeof(struct drm_connector), GFP_KERNEL);
  601.         if (!save_connectors) {
  602.                 kfree(save_crtcs);
  603.                 kfree(save_encoders);
  604.                 return -ENOMEM;
  605.         }
  606.  
  607.         /* Copy data. Note that driver private data is not affected.
  608.          * Should anything bad happen only the expected state is
  609.          * restored, not the drivers personal bookkeeping.
  610.          */
  611.         count = 0;
  612.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  613.                 save_crtcs[count++] = *crtc;
  614.         }
  615.  
  616.         count = 0;
  617.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  618.                 save_encoders[count++] = *encoder;
  619.         }
  620.  
  621.         count = 0;
  622.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  623.                 save_connectors[count++] = *connector;
  624.         }
  625.  
  626.         save_set.crtc = set->crtc;
  627.         save_set.mode = &set->crtc->mode;
  628.         save_set.x = set->crtc->x;
  629.         save_set.y = set->crtc->y;
  630.         save_set.fb = set->crtc->fb;
  631.  
  632.         /* We should be able to check here if the fb has the same properties
  633.          * and then just flip_or_move it */
  634.         if (set->crtc->fb != set->fb) {
  635.                 /* If we have no fb then treat it as a full mode set */
  636.                 if (set->crtc->fb == NULL) {
  637.                         DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
  638.                         mode_changed = true;
  639.                 } else if (set->fb == NULL) {
  640.                         mode_changed = true;
  641.                 } else if (set->fb->depth != set->crtc->fb->depth) {
  642.                         mode_changed = true;
  643.                 } else if (set->fb->bits_per_pixel !=
  644.                            set->crtc->fb->bits_per_pixel) {
  645.                         mode_changed = true;
  646.                 } else
  647.                         fb_changed = true;
  648.         }
  649.  
  650.         if (set->x != set->crtc->x || set->y != set->crtc->y)
  651.                 fb_changed = true;
  652.  
  653.         if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  654.                 DRM_DEBUG_KMS("modes are different, full mode set\n");
  655.                 drm_mode_debug_printmodeline(&set->crtc->mode);
  656.                 drm_mode_debug_printmodeline(set->mode);
  657.                 mode_changed = true;
  658.         }
  659.  
  660.         /* a) traverse passed in connector list and get encoders for them */
  661.         count = 0;
  662.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  663.                 struct drm_connector_helper_funcs *connector_funcs =
  664.                         connector->helper_private;
  665.                 new_encoder = connector->encoder;
  666.                 for (ro = 0; ro < set->num_connectors; ro++) {
  667.                         if (set->connectors[ro] == connector) {
  668.                                 new_encoder = connector_funcs->best_encoder(connector);
  669.                                 /* if we can't get an encoder for a connector
  670.                                    we are setting now - then fail */
  671.                                 if (new_encoder == NULL)
  672.                                         /* don't break so fail path works correct */
  673.                                         fail = 1;
  674.                                 break;
  675.                         }
  676.                 }
  677.  
  678.                 if (new_encoder != connector->encoder) {
  679.                         DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  680.                         mode_changed = true;
  681.                         /* If the encoder is reused for another connector, then
  682.                          * the appropriate crtc will be set later.
  683.                          */
  684.                         if (connector->encoder)
  685.                                 connector->encoder->crtc = NULL;
  686.                         connector->encoder = new_encoder;
  687.                 }
  688.         }
  689.  
  690.         if (fail) {
  691.                 ret = -EINVAL;
  692.                 goto fail;
  693.         }
  694.  
  695.         count = 0;
  696.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  697.                 if (!connector->encoder)
  698.                         continue;
  699.  
  700.                 if (connector->encoder->crtc == set->crtc)
  701.                         new_crtc = NULL;
  702.                 else
  703.                         new_crtc = connector->encoder->crtc;
  704.  
  705.                 for (ro = 0; ro < set->num_connectors; ro++) {
  706.                         if (set->connectors[ro] == connector)
  707.                                 new_crtc = set->crtc;
  708.                 }
  709.  
  710.                 /* Make sure the new CRTC will work with the encoder */
  711.                 if (new_crtc &&
  712.                     !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
  713.                         ret = -EINVAL;
  714.                         goto fail;
  715.                 }
  716.                 if (new_crtc != connector->encoder->crtc) {
  717.                         DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  718.                         mode_changed = true;
  719.                         connector->encoder->crtc = new_crtc;
  720.                 }
  721.                 if (new_crtc) {
  722.                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  723.                                 connector->base.id, drm_get_connector_name(connector),
  724.                                 new_crtc->base.id);
  725.                 } else {
  726.                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  727.                                 connector->base.id, drm_get_connector_name(connector));
  728.                 }
  729.         }
  730.  
  731.         /* mode_set_base is not a required function */
  732.         if (fb_changed && !crtc_funcs->mode_set_base)
  733.                 mode_changed = true;
  734.  
  735.         if (mode_changed) {
  736.                 set->crtc->enabled = drm_helper_crtc_in_use(set->crtc);
  737.                 if (set->crtc->enabled) {
  738.                         DRM_DEBUG_KMS("attempting to set mode from"
  739.                                         " userspace\n");
  740.                         drm_mode_debug_printmodeline(set->mode);
  741.                         old_fb = set->crtc->fb;
  742.                         set->crtc->fb = set->fb;
  743.                         if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  744.                                                       set->x, set->y,
  745.                                                       old_fb)) {
  746.                                 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
  747.                                           set->crtc->base.id);
  748.                                 set->crtc->fb = old_fb;
  749.                                 ret = -EINVAL;
  750.                                 goto fail;
  751.                         }
  752.                         DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
  753.                         for (i = 0; i < set->num_connectors; i++) {
  754.                                 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
  755.                                               drm_get_connector_name(set->connectors[i]));
  756.                                 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
  757.                         }
  758.                 }
  759.                 drm_helper_disable_unused_functions(dev);
  760.         } else if (fb_changed) {
  761.                 set->crtc->x = set->x;
  762.                 set->crtc->y = set->y;
  763.  
  764.                 old_fb = set->crtc->fb;
  765.                 if (set->crtc->fb != set->fb)
  766.                         set->crtc->fb = set->fb;
  767.                 ret = crtc_funcs->mode_set_base(set->crtc,
  768.                                                 set->x, set->y, old_fb);
  769.                 if (ret != 0) {
  770.                         set->crtc->fb = old_fb;
  771.                         goto fail;
  772.         }
  773.         }
  774.  
  775.         kfree(save_connectors);
  776.         kfree(save_encoders);
  777.         kfree(save_crtcs);
  778.         return 0;
  779.  
  780. fail:
  781.         /* Restore all previous data. */
  782.         count = 0;
  783.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  784.                 *crtc = save_crtcs[count++];
  785.         }
  786.  
  787.         count = 0;
  788.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  789.                 *encoder = save_encoders[count++];
  790.         }
  791.  
  792.         count = 0;
  793.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  794.                 *connector = save_connectors[count++];
  795.         }
  796.  
  797.         /* Try to restore the config */
  798.         if (mode_changed &&
  799.             !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
  800.                                       save_set.y, save_set.fb))
  801.                 DRM_ERROR("failed to restore config after modeset failure\n");
  802.  
  803.         kfree(save_connectors);
  804.         kfree(save_encoders);
  805.         kfree(save_crtcs);
  806.         return ret;
  807. }
  808. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  809.  
  810. static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
  811. {
  812.         int dpms = DRM_MODE_DPMS_OFF;
  813.         struct drm_connector *connector;
  814.         struct drm_device *dev = encoder->dev;
  815.  
  816.         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  817.                 if (connector->encoder == encoder)
  818.                         if (connector->dpms < dpms)
  819.                                 dpms = connector->dpms;
  820.         return dpms;
  821. }
  822.  
  823. static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
  824. {
  825.         int dpms = DRM_MODE_DPMS_OFF;
  826.         struct drm_connector *connector;
  827.         struct drm_device *dev = crtc->dev;
  828.  
  829.         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  830.                 if (connector->encoder && connector->encoder->crtc == crtc)
  831.                         if (connector->dpms < dpms)
  832.                                 dpms = connector->dpms;
  833.         return dpms;
  834. }
  835.  
  836. /**
  837.  * drm_helper_connector_dpms() - connector dpms helper implementation
  838.  * @connector: affected connector
  839.  * @mode: DPMS mode
  840.  *
  841.  * This is the main helper function provided by the crtc helper framework for
  842.  * implementing the DPMS connector attribute. It computes the new desired DPMS
  843.  * state for all encoders and crtcs in the output mesh and calls the ->dpms()
  844.  * callback provided by the driver appropriately.
  845.  */
  846. void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
  847. {
  848.         struct drm_encoder *encoder = connector->encoder;
  849.         struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  850.         int old_dpms;
  851.  
  852.         if (mode == connector->dpms)
  853.                 return;
  854.  
  855.         old_dpms = connector->dpms;
  856.         connector->dpms = mode;
  857.  
  858.         /* from off to on, do crtc then encoder */
  859.         if (mode < old_dpms) {
  860.                 if (crtc) {
  861.                         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  862.                         if (crtc_funcs->dpms)
  863.                                 (*crtc_funcs->dpms) (crtc,
  864.                                                      drm_helper_choose_crtc_dpms(crtc));
  865.                 }
  866.                 if (encoder) {
  867.                         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  868.                         if (encoder_funcs->dpms)
  869.                                 (*encoder_funcs->dpms) (encoder,
  870.                                                         drm_helper_choose_encoder_dpms(encoder));
  871.                 }
  872.         }
  873.  
  874.         /* from on to off, do encoder then crtc */
  875.         if (mode > old_dpms) {
  876.                 if (encoder) {
  877.                         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  878.                         if (encoder_funcs->dpms)
  879.                                 (*encoder_funcs->dpms) (encoder,
  880.                                                         drm_helper_choose_encoder_dpms(encoder));
  881.                 }
  882.                 if (crtc) {
  883.                         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  884.                         if (crtc_funcs->dpms)
  885.                                 (*crtc_funcs->dpms) (crtc,
  886.                                                      drm_helper_choose_crtc_dpms(crtc));
  887.                 }
  888.         }
  889.  
  890.         return;
  891. }
  892. EXPORT_SYMBOL(drm_helper_connector_dpms);
  893.  
  894. int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
  895.                                    struct drm_mode_fb_cmd2 *mode_cmd)
  896. {
  897.         int i;
  898.  
  899.         fb->width = mode_cmd->width;
  900.         fb->height = mode_cmd->height;
  901.         for (i = 0; i < 4; i++) {
  902.                 fb->pitches[i] = mode_cmd->pitches[i];
  903.                 fb->offsets[i] = mode_cmd->offsets[i];
  904.         }
  905.         drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
  906.                                     &fb->bits_per_pixel);
  907.         fb->pixel_format = mode_cmd->pixel_format;
  908.  
  909.         return 0;
  910. }
  911. EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
  912.  
  913. int drm_helper_resume_force_mode(struct drm_device *dev)
  914. {
  915.         struct drm_crtc *crtc;
  916.         struct drm_encoder *encoder;
  917.         struct drm_encoder_helper_funcs *encoder_funcs;
  918.         struct drm_crtc_helper_funcs *crtc_funcs;
  919.         int ret;
  920.  
  921.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  922.  
  923.        if (!crtc->enabled)
  924.            continue;
  925.  
  926.                 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
  927.                                                crtc->x, crtc->y, crtc->fb);
  928.  
  929.                 if (ret == false)
  930.                         DRM_ERROR("failed to set mode on crtc %p\n", crtc);
  931.  
  932.                 /* Turn off outputs that were already powered off */
  933.                 if (drm_helper_choose_crtc_dpms(crtc)) {
  934.                         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  935.  
  936.                                 if(encoder->crtc != crtc)
  937.                                         continue;
  938.  
  939.                                 encoder_funcs = encoder->helper_private;
  940.                                 if (encoder_funcs->dpms)
  941.                                         (*encoder_funcs->dpms) (encoder,
  942.                                                                 drm_helper_choose_encoder_dpms(encoder));
  943.                         }
  944.  
  945.                                 crtc_funcs = crtc->helper_private;
  946.                                 if (crtc_funcs->dpms)
  947.                                         (*crtc_funcs->dpms) (crtc,
  948.                                                              drm_helper_choose_crtc_dpms(crtc));
  949.                         }
  950.                 }
  951.         /* disable the unused connectors while restoring the modesetting */
  952.         drm_helper_disable_unused_functions(dev);
  953.         return 0;
  954. }
  955. EXPORT_SYMBOL(drm_helper_resume_force_mode);
  956.  
  957. #if 0
  958.  
  959. #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
  960. static void output_poll_execute(struct work_struct *work)
  961. {
  962.         struct delayed_work *delayed_work = to_delayed_work(work);
  963.         struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
  964.         struct drm_connector *connector;
  965.         enum drm_connector_status old_status;
  966.         bool repoll = false, changed = false;
  967.  
  968.         if (!drm_kms_helper_poll)
  969.                 return;
  970.  
  971.         mutex_lock(&dev->mode_config.mutex);
  972.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  973.  
  974.                 /* Ignore forced connectors. */
  975.                 if (connector->force)
  976.                         continue;
  977.  
  978.                 /* Ignore HPD capable connectors and connectors where we don't
  979.                  * want any hotplug detection at all for polling. */
  980.                 if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
  981.                         continue;
  982.  
  983.                         repoll = true;
  984.  
  985.                 old_status = connector->status;
  986.                 /* if we are connected and don't want to poll for disconnect
  987.                    skip it */
  988.                 if (old_status == connector_status_connected &&
  989.                     !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
  990.                         continue;
  991.  
  992.                 connector->status = connector->funcs->detect(connector, false);
  993.                 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
  994.                               connector->base.id,
  995.                               drm_get_connector_name(connector),
  996.                               old_status, connector->status);
  997.                 if (old_status != connector->status)
  998.                         changed = true;
  999.         }
  1000.  
  1001.         mutex_unlock(&dev->mode_config.mutex);
  1002.  
  1003.         if (changed)
  1004.                 drm_kms_helper_hotplug_event(dev);
  1005.  
  1006.         if (repoll)
  1007.                 schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
  1008. }
  1009.  
  1010. void drm_kms_helper_poll_disable(struct drm_device *dev)
  1011. {
  1012.         if (!dev->mode_config.poll_enabled)
  1013.                 return;
  1014.         cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
  1015. }
  1016. EXPORT_SYMBOL(drm_kms_helper_poll_disable);
  1017.  
  1018. void drm_kms_helper_poll_enable(struct drm_device *dev)
  1019. {
  1020.         bool poll = false;
  1021.         struct drm_connector *connector;
  1022.  
  1023.         if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
  1024.                 return;
  1025.  
  1026.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1027.                 if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
  1028.                                          DRM_CONNECTOR_POLL_DISCONNECT))
  1029.                         poll = true;
  1030.         }
  1031.  
  1032.         if (poll)
  1033.                 schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
  1034. }
  1035. EXPORT_SYMBOL(drm_kms_helper_poll_enable);
  1036.  
  1037. void drm_kms_helper_poll_init(struct drm_device *dev)
  1038. {
  1039.         INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
  1040.         dev->mode_config.poll_enabled = true;
  1041.  
  1042.         drm_kms_helper_poll_enable(dev);
  1043. }
  1044. EXPORT_SYMBOL(drm_kms_helper_poll_init);
  1045.  
  1046. void drm_kms_helper_poll_fini(struct drm_device *dev)
  1047. {
  1048.         drm_kms_helper_poll_disable(dev);
  1049. }
  1050. EXPORT_SYMBOL(drm_kms_helper_poll_fini);
  1051.  
  1052. void drm_helper_hpd_irq_event(struct drm_device *dev)
  1053. {
  1054.         struct drm_connector *connector;
  1055.         enum drm_connector_status old_status;
  1056.         bool changed = false;
  1057.  
  1058.         if (!dev->mode_config.poll_enabled)
  1059.                 return;
  1060.  
  1061.         mutex_lock(&dev->mode_config.mutex);
  1062.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1063.  
  1064.                 /* Only handle HPD capable connectors. */
  1065.                 if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
  1066.                         continue;
  1067.  
  1068.                 old_status = connector->status;
  1069.  
  1070.                 connector->status = connector->funcs->detect(connector, false);
  1071.                 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
  1072.                               connector->base.id,
  1073.                               drm_get_connector_name(connector),
  1074.                               old_status, connector->status);
  1075.                 if (old_status != connector->status)
  1076.                         changed = true;
  1077.         }
  1078.  
  1079.         mutex_unlock(&dev->mode_config.mutex);
  1080.  
  1081.         if (changed)
  1082.                 drm_kms_helper_hotplug_event(dev);
  1083. }
  1084. EXPORT_SYMBOL(drm_helper_hpd_irq_event);
  1085.  
  1086. #endif
  1087.