Subversion Repositories Kolibri OS

Rev

Rev 2160 | Rev 3051 | 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.     ENTER();
  357.  
  358.         crtc->enabled = drm_helper_crtc_in_use(crtc);
  359.         if (!crtc->enabled)
  360.                 return true;
  361.  
  362.     printf("crtc->enabled\n");
  363.  
  364.         adjusted_mode = drm_mode_duplicate(dev, mode);
  365.         if (!adjusted_mode)
  366.                 return false;
  367.  
  368.     printf("adjusted_mode\n");
  369.  
  370.         saved_hwmode = crtc->hwmode;
  371.         saved_mode = crtc->mode;
  372.         saved_x = crtc->x;
  373.         saved_y = crtc->y;
  374.  
  375.         /* Update crtc values up front so the driver can rely on them for mode
  376.          * setting.
  377.          */
  378.         crtc->mode = *mode;
  379.         crtc->x = x;
  380.         crtc->y = y;
  381.  
  382.         /* Pass our mode to the connectors and the CRTC to give them a chance to
  383.          * adjust it according to limitations or connector properties, and also
  384.          * a chance to reject the mode entirely.
  385.          */
  386.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  387.  
  388.                 if (encoder->crtc != crtc)
  389.                         continue;
  390.                 encoder_funcs = encoder->helper_private;
  391.                 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
  392.                                                       adjusted_mode))) {
  393.                         DRM_DEBUG_KMS("Encoder fixup failed\n");
  394.                         goto done;
  395.                 }
  396.         }
  397.  
  398.     printf("list_for_each_entry\n");
  399.     printf("mode_fixup %x\n", crtc_funcs->mode_fixup);
  400.  
  401.         if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
  402.                 DRM_DEBUG_KMS("CRTC fixup failed\n");
  403.                 goto done;
  404.         }
  405.         DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  406.  
  407.         /* Prepare the encoders and CRTCs before setting the mode. */
  408.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  409.  
  410.                 if (encoder->crtc != crtc)
  411.                         continue;
  412.                 encoder_funcs = encoder->helper_private;
  413.                 /* Disable the encoders as the first thing we do. */
  414.                 encoder_funcs->prepare(encoder);
  415.         }
  416.  
  417.         drm_crtc_prepare_encoders(dev);
  418.  
  419.         crtc_funcs->prepare(crtc);
  420.  
  421.         /* Set up the DPLL and any encoders state that needs to adjust or depend
  422.          * on the DPLL.
  423.          */
  424.         ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
  425.         if (!ret)
  426.             goto done;
  427.  
  428.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  429.  
  430.                 if (encoder->crtc != crtc)
  431.                         continue;
  432.  
  433.                 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
  434.                         encoder->base.id, drm_get_encoder_name(encoder),
  435.                         mode->base.id, mode->name);
  436.                 encoder_funcs = encoder->helper_private;
  437.                 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
  438.         }
  439.  
  440.         /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  441.         crtc_funcs->commit(crtc);
  442.  
  443.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  444.  
  445.                 if (encoder->crtc != crtc)
  446.                         continue;
  447.  
  448.                 encoder_funcs = encoder->helper_private;
  449.                 encoder_funcs->commit(encoder);
  450.  
  451.         }
  452.  
  453.         /* Store real post-adjustment hardware mode. */
  454.         crtc->hwmode = *adjusted_mode;
  455.  
  456.         /* Calculate and store various constants which
  457.          * are later needed by vblank and swap-completion
  458.          * timestamping. They are derived from true hwmode.
  459.          */
  460.         drm_calc_timestamping_constants(crtc);
  461.  
  462.         /* FIXME: add subpixel order */
  463. done:
  464.         drm_mode_destroy(dev, adjusted_mode);
  465.         if (!ret) {
  466.                 crtc->hwmode = saved_hwmode;
  467.                 crtc->mode = saved_mode;
  468.                 crtc->x = saved_x;
  469.                 crtc->y = saved_y;
  470.         }
  471.  
  472.     LEAVE();
  473.  
  474.         return ret;
  475. }
  476. EXPORT_SYMBOL(drm_crtc_helper_set_mode);
  477.  
  478.  
  479. static int
  480. drm_crtc_helper_disable(struct drm_crtc *crtc)
  481. {
  482.         struct drm_device *dev = crtc->dev;
  483.         struct drm_connector *connector;
  484.         struct drm_encoder *encoder;
  485.  
  486.         /* Decouple all encoders and their attached connectors from this crtc */
  487.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  488.                 if (encoder->crtc != crtc)
  489.                         continue;
  490.  
  491.                 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  492.                         if (connector->encoder != encoder)
  493.                                 continue;
  494.  
  495.                         connector->encoder = NULL;
  496.                 }
  497.         }
  498.  
  499.         drm_helper_disable_unused_functions(dev);
  500.         return 0;
  501. }
  502.  
  503. /**
  504.  * drm_crtc_helper_set_config - set a new config from userspace
  505.  * @crtc: CRTC to setup
  506.  * @crtc_info: user provided configuration
  507.  * @new_mode: new mode to set
  508.  * @connector_set: set of connectors for the new config
  509.  * @fb: new framebuffer
  510.  *
  511.  * LOCKING:
  512.  * Caller must hold mode config lock.
  513.  *
  514.  * Setup a new configuration, provided by the user in @crtc_info, and enable
  515.  * it.
  516.  *
  517.  * RETURNS:
  518.  * Zero. (FIXME)
  519.  */
  520. int drm_crtc_helper_set_config(struct drm_mode_set *set)
  521. {
  522.         struct drm_device *dev;
  523.         struct drm_crtc *save_crtcs, *new_crtc, *crtc;
  524.         struct drm_encoder *save_encoders, *new_encoder, *encoder;
  525.         struct drm_framebuffer *old_fb = NULL;
  526.         bool mode_changed = false; /* if true do a full mode set */
  527.         bool fb_changed = false; /* if true and !mode_changed just do a flip */
  528.         struct drm_connector *save_connectors, *connector;
  529.         int count = 0, ro, fail = 0;
  530.         struct drm_crtc_helper_funcs *crtc_funcs;
  531.         struct drm_mode_set save_set;
  532.         int ret;
  533.         int i;
  534.  
  535.         DRM_DEBUG_KMS("\n");
  536.  
  537.         if (!set)
  538.                 return -EINVAL;
  539.  
  540.         if (!set->crtc)
  541.                 return -EINVAL;
  542.  
  543.         if (!set->crtc->helper_private)
  544.                 return -EINVAL;
  545.  
  546.         crtc_funcs = set->crtc->helper_private;
  547.  
  548.         if (!set->mode)
  549.                 set->fb = NULL;
  550.  
  551.         if (set->fb) {
  552.                 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  553.                                 set->crtc->base.id, set->fb->base.id,
  554.                   (int)set->num_connectors, set->x, set->y);
  555.         } else {
  556.                 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  557.                 return drm_crtc_helper_disable(set->crtc);
  558.         }
  559.  
  560.         dev = set->crtc->dev;
  561.  
  562.         /* Allocate space for the backup of all (non-pointer) crtc, encoder and
  563.          * connector data. */
  564.         save_crtcs = kzalloc(dev->mode_config.num_crtc *
  565.                              sizeof(struct drm_crtc), GFP_KERNEL);
  566.         if (!save_crtcs)
  567.                 return -ENOMEM;
  568.  
  569.         save_encoders = kzalloc(dev->mode_config.num_encoder *
  570.                                 sizeof(struct drm_encoder), GFP_KERNEL);
  571.         if (!save_encoders) {
  572.                 kfree(save_crtcs);
  573.                 return -ENOMEM;
  574.         }
  575.  
  576.         save_connectors = kzalloc(dev->mode_config.num_connector *
  577.                                 sizeof(struct drm_connector), GFP_KERNEL);
  578.         if (!save_connectors) {
  579.                 kfree(save_crtcs);
  580.                 kfree(save_encoders);
  581.                 return -ENOMEM;
  582.         }
  583.  
  584.         /* Copy data. Note that driver private data is not affected.
  585.          * Should anything bad happen only the expected state is
  586.          * restored, not the drivers personal bookkeeping.
  587.          */
  588.         count = 0;
  589.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  590.                 save_crtcs[count++] = *crtc;
  591.         }
  592.  
  593.         count = 0;
  594.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  595.                 save_encoders[count++] = *encoder;
  596.         }
  597.  
  598.         count = 0;
  599.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  600.                 save_connectors[count++] = *connector;
  601.         }
  602.  
  603.         save_set.crtc = set->crtc;
  604.         save_set.mode = &set->crtc->mode;
  605.         save_set.x = set->crtc->x;
  606.         save_set.y = set->crtc->y;
  607.         save_set.fb = set->crtc->fb;
  608.  
  609.         /* We should be able to check here if the fb has the same properties
  610.          * and then just flip_or_move it */
  611.         if (set->crtc->fb != set->fb) {
  612.                 /* If we have no fb then treat it as a full mode set */
  613.                 if (set->crtc->fb == NULL) {
  614.                         DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
  615.                         mode_changed = true;
  616.                 } else if (set->fb == NULL) {
  617.                         mode_changed = true;
  618.                 } else if (set->fb->depth != set->crtc->fb->depth) {
  619.                         mode_changed = true;
  620.                 } else if (set->fb->bits_per_pixel !=
  621.                            set->crtc->fb->bits_per_pixel) {
  622.                         mode_changed = true;
  623.                 } else
  624.                         fb_changed = true;
  625.         }
  626.  
  627.         if (set->x != set->crtc->x || set->y != set->crtc->y)
  628.                 fb_changed = true;
  629.  
  630.         if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  631.                 DRM_DEBUG_KMS("modes are different, full mode set\n");
  632.                 drm_mode_debug_printmodeline(&set->crtc->mode);
  633.                 drm_mode_debug_printmodeline(set->mode);
  634.                 mode_changed = true;
  635.         }
  636.  
  637.         /* a) traverse passed in connector list and get encoders for them */
  638.         count = 0;
  639.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  640.                 struct drm_connector_helper_funcs *connector_funcs =
  641.                         connector->helper_private;
  642.                 new_encoder = connector->encoder;
  643.                 for (ro = 0; ro < set->num_connectors; ro++) {
  644.                         if (set->connectors[ro] == connector) {
  645.                                 new_encoder = connector_funcs->best_encoder(connector);
  646.                                 /* if we can't get an encoder for a connector
  647.                                    we are setting now - then fail */
  648.                                 if (new_encoder == NULL)
  649.                                         /* don't break so fail path works correct */
  650.                                         fail = 1;
  651.                                 break;
  652.                         }
  653.                 }
  654.  
  655.                 if (new_encoder != connector->encoder) {
  656.                         DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  657.                         mode_changed = true;
  658.                         /* If the encoder is reused for another connector, then
  659.                          * the appropriate crtc will be set later.
  660.                          */
  661.                         if (connector->encoder)
  662.                                 connector->encoder->crtc = NULL;
  663.                         connector->encoder = new_encoder;
  664.                 }
  665.         }
  666.  
  667.         if (fail) {
  668.                 ret = -EINVAL;
  669.                 goto fail;
  670.         }
  671.  
  672.         count = 0;
  673.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  674.                 if (!connector->encoder)
  675.                         continue;
  676.  
  677.                 if (connector->encoder->crtc == set->crtc)
  678.                         new_crtc = NULL;
  679.                 else
  680.                         new_crtc = connector->encoder->crtc;
  681.  
  682.                 for (ro = 0; ro < set->num_connectors; ro++) {
  683.                         if (set->connectors[ro] == connector)
  684.                                 new_crtc = set->crtc;
  685.                 }
  686.  
  687.                 /* Make sure the new CRTC will work with the encoder */
  688.                 if (new_crtc &&
  689.                     !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
  690.                         ret = -EINVAL;
  691.                         goto fail;
  692.                 }
  693.                 if (new_crtc != connector->encoder->crtc) {
  694.                         DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  695.                         mode_changed = true;
  696.                         connector->encoder->crtc = new_crtc;
  697.                 }
  698.                 if (new_crtc) {
  699.                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  700.                                 connector->base.id, drm_get_connector_name(connector),
  701.                                 new_crtc->base.id);
  702.                 } else {
  703.                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  704.                                 connector->base.id, drm_get_connector_name(connector));
  705.                 }
  706.         }
  707.  
  708.         /* mode_set_base is not a required function */
  709.         if (fb_changed && !crtc_funcs->mode_set_base)
  710.                 mode_changed = true;
  711.  
  712.         if (mode_changed) {
  713.                 set->crtc->enabled = drm_helper_crtc_in_use(set->crtc);
  714.                 if (set->crtc->enabled) {
  715.                         DRM_DEBUG_KMS("attempting to set mode from"
  716.                                         " userspace\n");
  717.                         drm_mode_debug_printmodeline(set->mode);
  718.                         old_fb = set->crtc->fb;
  719.                         set->crtc->fb = set->fb;
  720.                         if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
  721.                                                       set->x, set->y,
  722.                                                       old_fb)) {
  723.                                 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
  724.                                           set->crtc->base.id);
  725.                                 set->crtc->fb = old_fb;
  726.                                 ret = -EINVAL;
  727.                                 goto fail;
  728.                         }
  729.                         DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
  730.                         for (i = 0; i < set->num_connectors; i++) {
  731.                                 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
  732.                                               drm_get_connector_name(set->connectors[i]));
  733.                                 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
  734.                         }
  735.                 }
  736.                 drm_helper_disable_unused_functions(dev);
  737.         } else if (fb_changed) {
  738.                 set->crtc->x = set->x;
  739.                 set->crtc->y = set->y;
  740.  
  741.                 old_fb = set->crtc->fb;
  742.                 if (set->crtc->fb != set->fb)
  743.                         set->crtc->fb = set->fb;
  744.                 ret = crtc_funcs->mode_set_base(set->crtc,
  745.                                                 set->x, set->y, old_fb);
  746.                 if (ret != 0) {
  747.                         set->crtc->fb = old_fb;
  748.                         goto fail;
  749.         }
  750.         }
  751.  
  752.         kfree(save_connectors);
  753.         kfree(save_encoders);
  754.         kfree(save_crtcs);
  755.         return 0;
  756.  
  757. fail:
  758.         /* Restore all previous data. */
  759.         count = 0;
  760.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  761.                 *crtc = save_crtcs[count++];
  762.         }
  763.  
  764.         count = 0;
  765.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  766.                 *encoder = save_encoders[count++];
  767.         }
  768.  
  769.         count = 0;
  770.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  771.                 *connector = save_connectors[count++];
  772.         }
  773.  
  774.         /* Try to restore the config */
  775.         if (mode_changed &&
  776.             !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
  777.                                       save_set.y, save_set.fb))
  778.                 DRM_ERROR("failed to restore config after modeset failure\n");
  779.  
  780.         kfree(save_connectors);
  781.         kfree(save_encoders);
  782.         kfree(save_crtcs);
  783.         return ret;
  784. }
  785. EXPORT_SYMBOL(drm_crtc_helper_set_config);
  786.  
  787. static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
  788. {
  789.         int dpms = DRM_MODE_DPMS_OFF;
  790.         struct drm_connector *connector;
  791.         struct drm_device *dev = encoder->dev;
  792.  
  793.         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  794.                 if (connector->encoder == encoder)
  795.                         if (connector->dpms < dpms)
  796.                                 dpms = connector->dpms;
  797.         return dpms;
  798. }
  799.  
  800. static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
  801. {
  802.         int dpms = DRM_MODE_DPMS_OFF;
  803.         struct drm_connector *connector;
  804.         struct drm_device *dev = crtc->dev;
  805.  
  806.         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  807.                 if (connector->encoder && connector->encoder->crtc == crtc)
  808.                         if (connector->dpms < dpms)
  809.                                 dpms = connector->dpms;
  810.         return dpms;
  811. }
  812.  
  813. /**
  814.  * drm_helper_connector_dpms
  815.  * @connector affected connector
  816.  * @mode DPMS mode
  817.  *
  818.  * Calls the low-level connector DPMS function, then
  819.  * calls appropriate encoder and crtc DPMS functions as well
  820.  */
  821. void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
  822. {
  823.         struct drm_encoder *encoder = connector->encoder;
  824.         struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  825.         int old_dpms;
  826.  
  827.         if (mode == connector->dpms)
  828.                 return;
  829.  
  830.         old_dpms = connector->dpms;
  831.         connector->dpms = mode;
  832.  
  833.         /* from off to on, do crtc then encoder */
  834.         if (mode < old_dpms) {
  835.                 if (crtc) {
  836.                         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  837.                         if (crtc_funcs->dpms)
  838.                                 (*crtc_funcs->dpms) (crtc,
  839.                                                      drm_helper_choose_crtc_dpms(crtc));
  840.                 }
  841.                 if (encoder) {
  842.                         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  843.                         if (encoder_funcs->dpms)
  844.                                 (*encoder_funcs->dpms) (encoder,
  845.                                                         drm_helper_choose_encoder_dpms(encoder));
  846.                 }
  847.         }
  848.  
  849.         /* from on to off, do encoder then crtc */
  850.         if (mode > old_dpms) {
  851.                 if (encoder) {
  852.                         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  853.                         if (encoder_funcs->dpms)
  854.                                 (*encoder_funcs->dpms) (encoder,
  855.                                                         drm_helper_choose_encoder_dpms(encoder));
  856.                 }
  857.                 if (crtc) {
  858.                         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  859.                         if (crtc_funcs->dpms)
  860.                                 (*crtc_funcs->dpms) (crtc,
  861.                                                      drm_helper_choose_crtc_dpms(crtc));
  862.                 }
  863.         }
  864.  
  865.         return;
  866. }
  867. EXPORT_SYMBOL(drm_helper_connector_dpms);
  868.  
  869. int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
  870.                                    struct drm_mode_fb_cmd2 *mode_cmd)
  871. {
  872.         int i;
  873.  
  874.         fb->width = mode_cmd->width;
  875.         fb->height = mode_cmd->height;
  876.         for (i = 0; i < 4; i++) {
  877.                 fb->pitches[i] = mode_cmd->pitches[i];
  878.                 fb->offsets[i] = mode_cmd->offsets[i];
  879.         }
  880.         drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
  881.                                     &fb->bits_per_pixel);
  882.         fb->pixel_format = mode_cmd->pixel_format;
  883.  
  884.         return 0;
  885. }
  886. EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
  887.  
  888. int drm_helper_resume_force_mode(struct drm_device *dev)
  889. {
  890.         struct drm_crtc *crtc;
  891.         struct drm_encoder *encoder;
  892.         struct drm_encoder_helper_funcs *encoder_funcs;
  893.         struct drm_crtc_helper_funcs *crtc_funcs;
  894.         int ret;
  895.  
  896.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  897.  
  898.        if (!crtc->enabled)
  899.            continue;
  900.  
  901.                 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
  902.                                                crtc->x, crtc->y, crtc->fb);
  903.  
  904.                 if (ret == false)
  905.                         DRM_ERROR("failed to set mode on crtc %p\n", crtc);
  906.  
  907.                 /* Turn off outputs that were already powered off */
  908.                 if (drm_helper_choose_crtc_dpms(crtc)) {
  909.                         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  910.  
  911.                                 if(encoder->crtc != crtc)
  912.                                         continue;
  913.  
  914.                                 encoder_funcs = encoder->helper_private;
  915.                                 if (encoder_funcs->dpms)
  916.                                         (*encoder_funcs->dpms) (encoder,
  917.                                                                 drm_helper_choose_encoder_dpms(encoder));
  918.                         }
  919.  
  920.                                 crtc_funcs = crtc->helper_private;
  921.                                 if (crtc_funcs->dpms)
  922.                                         (*crtc_funcs->dpms) (crtc,
  923.                                                              drm_helper_choose_crtc_dpms(crtc));
  924.                         }
  925.                 }
  926.         /* disable the unused connectors while restoring the modesetting */
  927.         drm_helper_disable_unused_functions(dev);
  928.         return 0;
  929. }
  930. EXPORT_SYMBOL(drm_helper_resume_force_mode);
  931.  
  932. #if 0
  933.  
  934. #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
  935. static void output_poll_execute(struct work_struct *work)
  936. {
  937.         struct delayed_work *delayed_work = to_delayed_work(work);
  938.         struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
  939.         struct drm_connector *connector;
  940.         enum drm_connector_status old_status;
  941.         bool repoll = false, changed = false;
  942.  
  943.         if (!drm_kms_helper_poll)
  944.                 return;
  945.  
  946.         mutex_lock(&dev->mode_config.mutex);
  947.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  948.  
  949.                 /* if this is HPD or polled don't check it -
  950.                    TV out for instance */
  951.                 if (!connector->polled)
  952.                         continue;
  953.  
  954.                 else if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT))
  955.                         repoll = true;
  956.  
  957.                 old_status = connector->status;
  958.                 /* if we are connected and don't want to poll for disconnect
  959.                    skip it */
  960.                 if (old_status == connector_status_connected &&
  961.                     !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT) &&
  962.                     !(connector->polled & DRM_CONNECTOR_POLL_HPD))
  963.                         continue;
  964.  
  965.                 connector->status = connector->funcs->detect(connector, false);
  966.                 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
  967.                               connector->base.id,
  968.                               drm_get_connector_name(connector),
  969.                               old_status, connector->status);
  970.                 if (old_status != connector->status)
  971.                         changed = true;
  972.         }
  973.  
  974.         mutex_unlock(&dev->mode_config.mutex);
  975.  
  976.         if (changed) {
  977.                 /* send a uevent + call fbdev */
  978.                 drm_sysfs_hotplug_event(dev);
  979.                 if (dev->mode_config.funcs->output_poll_changed)
  980.                         dev->mode_config.funcs->output_poll_changed(dev);
  981.         }
  982.  
  983.         if (repoll)
  984.                 schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
  985. }
  986.  
  987. void drm_kms_helper_poll_disable(struct drm_device *dev)
  988. {
  989.         if (!dev->mode_config.poll_enabled)
  990.                 return;
  991.         cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
  992. }
  993. EXPORT_SYMBOL(drm_kms_helper_poll_disable);
  994.  
  995. void drm_kms_helper_poll_enable(struct drm_device *dev)
  996. {
  997.         bool poll = false;
  998.         struct drm_connector *connector;
  999.  
  1000.         if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
  1001.                 return;
  1002.  
  1003.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1004.                 if (connector->polled)
  1005.                         poll = true;
  1006.         }
  1007.  
  1008.         if (poll)
  1009.                 schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
  1010. }
  1011. EXPORT_SYMBOL(drm_kms_helper_poll_enable);
  1012.  
  1013. void drm_kms_helper_poll_init(struct drm_device *dev)
  1014. {
  1015.         INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
  1016.         dev->mode_config.poll_enabled = true;
  1017.  
  1018.         drm_kms_helper_poll_enable(dev);
  1019. }
  1020. EXPORT_SYMBOL(drm_kms_helper_poll_init);
  1021.  
  1022. void drm_kms_helper_poll_fini(struct drm_device *dev)
  1023. {
  1024.         drm_kms_helper_poll_disable(dev);
  1025. }
  1026. EXPORT_SYMBOL(drm_kms_helper_poll_fini);
  1027.  
  1028. void drm_helper_hpd_irq_event(struct drm_device *dev)
  1029. {
  1030.         if (!dev->mode_config.poll_enabled)
  1031.                 return;
  1032.  
  1033.         /* kill timer and schedule immediate execution, this doesn't block */
  1034.         cancel_delayed_work(&dev->mode_config.output_poll_work);
  1035.         if (drm_kms_helper_poll)
  1036.                 schedule_delayed_work(&dev->mode_config.output_poll_work, 0);
  1037. }
  1038. EXPORT_SYMBOL(drm_helper_hpd_irq_event);
  1039.  
  1040. #endif
  1041.