Subversion Repositories Kolibri OS

Rev

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