Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * Copyright (c) 2006-2009 Red Hat Inc.
  3.  * Copyright (c) 2006-2008 Intel Corporation
  4.  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  5.  *
  6.  * DRM framebuffer helper functions
  7.  *
  8.  * Permission to use, copy, modify, distribute, and sell this software and its
  9.  * documentation for any purpose is hereby granted without fee, provided that
  10.  * the above copyright notice appear in all copies and that both that copyright
  11.  * notice and this permission notice appear in supporting documentation, and
  12.  * that the name of the copyright holders not be used in advertising or
  13.  * publicity pertaining to distribution of the software without specific,
  14.  * written prior permission.  The copyright holders make no representations
  15.  * about the suitability of this software for any purpose.  It is provided "as
  16.  * is" without express or implied warranty.
  17.  *
  18.  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  19.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  20.  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  22.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  23.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  24.  * OF THIS SOFTWARE.
  25.  *
  26.  * Authors:
  27.  *      Dave Airlie <airlied@linux.ie>
  28.  *      Jesse Barnes <jesse.barnes@intel.com>
  29.  */
  30. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  31.  
  32. #include <linux/kernel.h>
  33. #include <linux/sysrq.h>
  34. #include <linux/slab.h>
  35. #include <linux/fb.h>
  36. #include <linux/module.h>
  37. #include <drm/drmP.h>
  38. #include <drm/drm_crtc.h>
  39. #include <drm/drm_fb_helper.h>
  40. #include <drm/drm_crtc_helper.h>
  41.  
  42. static LIST_HEAD(kernel_fb_helper_list);
  43.  
  44. /**
  45.  * DOC: fbdev helpers
  46.  *
  47.  * The fb helper functions are useful to provide an fbdev on top of a drm kernel
  48.  * mode setting driver. They can be used mostly independently from the crtc
  49.  * helper functions used by many drivers to implement the kernel mode setting
  50.  * interfaces.
  51.  *
  52.  * Initialization is done as a four-step process with drm_fb_helper_prepare(),
  53.  * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
  54.  * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
  55.  * default behaviour can override the third step with their own code.
  56.  * Teardown is done with drm_fb_helper_fini().
  57.  *
  58.  * At runtime drivers should restore the fbdev console by calling
  59.  * drm_fb_helper_restore_fbdev_mode() from their ->lastclose callback. They
  60.  * should also notify the fb helper code from updates to the output
  61.  * configuration by calling drm_fb_helper_hotplug_event(). For easier
  62.  * integration with the output polling code in drm_crtc_helper.c the modeset
  63.  * code provides a ->output_poll_changed callback.
  64.  *
  65.  * All other functions exported by the fb helper library can be used to
  66.  * implement the fbdev driver interface by the driver.
  67.  *
  68.  * It is possible, though perhaps somewhat tricky, to implement race-free
  69.  * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
  70.  * helper must be called first to initialize the minimum required to make
  71.  * hotplug detection work. Drivers also need to make sure to properly set up
  72.  * the dev->mode_config.funcs member. After calling drm_kms_helper_poll_init()
  73.  * it is safe to enable interrupts and start processing hotplug events. At the
  74.  * same time, drivers should initialize all modeset objects such as CRTCs,
  75.  * encoders and connectors. To finish up the fbdev helper initialization, the
  76.  * drm_fb_helper_init() function is called. To probe for all attached displays
  77.  * and set up an initial configuration using the detected hardware, drivers
  78.  * should call drm_fb_helper_single_add_all_connectors() followed by
  79.  * drm_fb_helper_initial_config().
  80.  */
  81.  
  82. /**
  83.  * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
  84.  *                                             emulation helper
  85.  * @fb_helper: fbdev initialized with drm_fb_helper_init
  86.  *
  87.  * This functions adds all the available connectors for use with the given
  88.  * fb_helper. This is a separate step to allow drivers to freely assign
  89.  * connectors to the fbdev, e.g. if some are reserved for special purposes or
  90.  * not adequate to be used for the fbcon.
  91.  *
  92.  * Since this is part of the initial setup before the fbdev is published, no
  93.  * locking is required.
  94.  */
  95. int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
  96. {
  97.         struct drm_device *dev = fb_helper->dev;
  98.         struct drm_connector *connector;
  99.         int i;
  100.  
  101.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  102.                 struct drm_fb_helper_connector *fb_helper_connector;
  103.  
  104.                 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
  105.                 if (!fb_helper_connector)
  106.                         goto fail;
  107.  
  108.                 fb_helper_connector->connector = connector;
  109.                 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
  110.         }
  111.         return 0;
  112. fail:
  113.         for (i = 0; i < fb_helper->connector_count; i++) {
  114.                 kfree(fb_helper->connector_info[i]);
  115.                 fb_helper->connector_info[i] = NULL;
  116.         }
  117.         fb_helper->connector_count = 0;
  118.     return -ENOMEM;
  119. }
  120. EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
  121.  
  122. int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector)
  123. {
  124.         struct drm_fb_helper_connector **temp;
  125.         struct drm_fb_helper_connector *fb_helper_connector;
  126.  
  127.         WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
  128.         if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) {
  129.                 temp = krealloc(fb_helper->connector_info, sizeof(struct drm_fb_helper_connector) * (fb_helper->connector_count + 1), GFP_KERNEL);
  130.                 if (!temp)
  131.                         return -ENOMEM;
  132.  
  133.                 fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1;
  134.                 fb_helper->connector_info = temp;
  135.         }
  136.  
  137.  
  138.         fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
  139.         if (!fb_helper_connector)
  140.                 return -ENOMEM;
  141.  
  142.         fb_helper_connector->connector = connector;
  143.         fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
  144.         return 0;
  145. }
  146. EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
  147.  
  148. int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
  149.                                        struct drm_connector *connector)
  150. {
  151.         struct drm_fb_helper_connector *fb_helper_connector;
  152.         int i, j;
  153.  
  154.         WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
  155.  
  156.         for (i = 0; i < fb_helper->connector_count; i++) {
  157.                 if (fb_helper->connector_info[i]->connector == connector)
  158.                         break;
  159.         }
  160.  
  161.         if (i == fb_helper->connector_count)
  162.                 return -EINVAL;
  163.         fb_helper_connector = fb_helper->connector_info[i];
  164.  
  165.         for (j = i + 1; j < fb_helper->connector_count; j++) {
  166.                 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
  167.         }
  168.         fb_helper->connector_count--;
  169.         kfree(fb_helper_connector);
  170.         return 0;
  171. }
  172. EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
  173. static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
  174. {
  175.         uint16_t *r_base, *g_base, *b_base;
  176.         int i;
  177.  
  178.         if (helper->funcs->gamma_get == NULL)
  179.                 return;
  180.  
  181.         r_base = crtc->gamma_store;
  182.         g_base = r_base + crtc->gamma_size;
  183.         b_base = g_base + crtc->gamma_size;
  184.  
  185.         for (i = 0; i < crtc->gamma_size; i++)
  186.                 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
  187. }
  188.  
  189. static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
  190. {
  191.         uint16_t *r_base, *g_base, *b_base;
  192.  
  193.         if (crtc->funcs->gamma_set == NULL)
  194.                 return;
  195.  
  196.         r_base = crtc->gamma_store;
  197.         g_base = r_base + crtc->gamma_size;
  198.         b_base = g_base + crtc->gamma_size;
  199.  
  200.         crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  201. }
  202.  
  203.  
  204. static bool restore_fbdev_mode(struct drm_fb_helper *fb_helper)
  205. {
  206.         struct drm_device *dev = fb_helper->dev;
  207.         struct drm_plane *plane;
  208.         bool error = false;
  209.         int i;
  210.  
  211.         drm_warn_on_modeset_not_all_locked(dev);
  212.  
  213.         list_for_each_entry(plane, &dev->mode_config.plane_list, head)
  214.                 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
  215.                         drm_plane_force_disable(plane);
  216.  
  217.         for (i = 0; i < fb_helper->crtc_count; i++) {
  218.                 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
  219.                 struct drm_crtc *crtc = mode_set->crtc;
  220.                 int ret;
  221.  
  222.                 if (crtc->funcs->cursor_set) {
  223.                         ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
  224.                         if (ret)
  225.                                 error = true;
  226.                 }
  227.  
  228.                 ret = drm_mode_set_config_internal(mode_set);
  229.                 if (ret)
  230.                         error = true;
  231.         }
  232.         return error;
  233. }
  234. /**
  235.  * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
  236.  * @fb_helper: fbcon to restore
  237.  *
  238.  * This should be called from driver's drm ->lastclose callback
  239.  * when implementing an fbcon on top of kms using this helper. This ensures that
  240.  * the user isn't greeted with a black screen when e.g. X dies.
  241.  */
  242. bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
  243. {
  244.         struct drm_device *dev = fb_helper->dev;
  245.         bool ret;
  246.         drm_modeset_lock_all(dev);
  247.         ret = restore_fbdev_mode(fb_helper);
  248.         drm_modeset_unlock_all(dev);
  249.         return ret;
  250. }
  251. EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
  252.  
  253. static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
  254. {
  255.         struct drm_device *dev = fb_helper->dev;
  256.         struct drm_crtc *crtc;
  257.         int bound = 0, crtcs_bound = 0;
  258.  
  259.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  260.                 if (crtc->primary->fb)
  261.                         crtcs_bound++;
  262.                 if (crtc->primary->fb == fb_helper->fb)
  263.                         bound++;
  264.         }
  265.  
  266.         if (bound < crtcs_bound)
  267.                 return false;
  268.  
  269.         return true;
  270. }
  271.  
  272. #ifdef CONFIG_MAGIC_SYSRQ
  273. static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
  274. {
  275.         bool ret;
  276.         ret = drm_fb_helper_force_kernel_mode();
  277.         if (ret == true)
  278.                 DRM_ERROR("Failed to restore crtc configuration\n");
  279. }
  280. static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
  281.  
  282. static void drm_fb_helper_sysrq(int dummy1)
  283. {
  284.         schedule_work(&drm_fb_helper_restore_work);
  285. }
  286.  
  287. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
  288.         .handler = drm_fb_helper_sysrq,
  289.         .help_msg = "force-fb(V)",
  290.         .action_msg = "Restore framebuffer console",
  291. };
  292. #else
  293.  
  294. #endif
  295.  
  296. static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
  297. {
  298.         struct drm_fb_helper *fb_helper = info->par;
  299.         struct drm_device *dev = fb_helper->dev;
  300.         struct drm_crtc *crtc;
  301.         struct drm_connector *connector;
  302.         int i, j;
  303.  
  304.         /*
  305.          * fbdev->blank can be called from irq context in case of a panic.
  306.          * Since we already have our own special panic handler which will
  307.          * restore the fbdev console mode completely, just bail out early.
  308.          */
  309.  
  310.         /*
  311.          * For each CRTC in this fb, turn the connectors on/off.
  312.          */
  313.         drm_modeset_lock_all(dev);
  314.         if (!drm_fb_helper_is_bound(fb_helper)) {
  315.                 drm_modeset_unlock_all(dev);
  316.                 return;
  317.         }
  318.  
  319.         for (i = 0; i < fb_helper->crtc_count; i++) {
  320.                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
  321.  
  322.                 if (!crtc->enabled)
  323.                                 continue;
  324.  
  325.                 /* Walk the connectors & encoders on this fb turning them on/off */
  326.                 for (j = 0; j < fb_helper->connector_count; j++) {
  327.                         connector = fb_helper->connector_info[j]->connector;
  328.                         connector->funcs->dpms(connector, dpms_mode);
  329.                         drm_object_property_set_value(&connector->base,
  330.                                 dev->mode_config.dpms_property, dpms_mode);
  331.                 }
  332.         }
  333.         drm_modeset_unlock_all(dev);
  334. }
  335.  
  336. /**
  337.  * drm_fb_helper_blank - implementation for ->fb_blank
  338.  * @blank: desired blanking state
  339.  * @info: fbdev registered by the helper
  340.  */
  341. int drm_fb_helper_blank(int blank, struct fb_info *info)
  342. {
  343.         switch (blank) {
  344.         /* Display: On; HSync: On, VSync: On */
  345.         case FB_BLANK_UNBLANK:
  346.                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
  347.                 break;
  348.         /* Display: Off; HSync: On, VSync: On */
  349.         case FB_BLANK_NORMAL:
  350.                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  351.                 break;
  352.         /* Display: Off; HSync: Off, VSync: On */
  353.         case FB_BLANK_HSYNC_SUSPEND:
  354.                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  355.                 break;
  356.         /* Display: Off; HSync: On, VSync: Off */
  357.         case FB_BLANK_VSYNC_SUSPEND:
  358.                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
  359.                 break;
  360.         /* Display: Off; HSync: Off, VSync: Off */
  361.         case FB_BLANK_POWERDOWN:
  362.                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
  363.                 break;
  364.         }
  365.         return 0;
  366. }
  367. EXPORT_SYMBOL(drm_fb_helper_blank);
  368.  
  369. static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
  370. {
  371.         int i;
  372.  
  373.         for (i = 0; i < helper->connector_count; i++)
  374.                 kfree(helper->connector_info[i]);
  375.         kfree(helper->connector_info);
  376.         for (i = 0; i < helper->crtc_count; i++) {
  377.                 kfree(helper->crtc_info[i].mode_set.connectors);
  378.                 if (helper->crtc_info[i].mode_set.mode)
  379.                         drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
  380.         }
  381.         kfree(helper->crtc_info);
  382. }
  383.  
  384. /**
  385.  * drm_fb_helper_prepare - setup a drm_fb_helper structure
  386.  * @dev: DRM device
  387.  * @helper: driver-allocated fbdev helper structure to set up
  388.  * @funcs: pointer to structure of functions associate with this helper
  389.  *
  390.  * Sets up the bare minimum to make the framebuffer helper usable. This is
  391.  * useful to implement race-free initialization of the polling helpers.
  392.  */
  393. void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
  394.                            const struct drm_fb_helper_funcs *funcs)
  395. {
  396.         INIT_LIST_HEAD(&helper->kernel_fb_list);
  397.         helper->funcs = funcs;
  398.         helper->dev = dev;
  399. }
  400. EXPORT_SYMBOL(drm_fb_helper_prepare);
  401.  
  402. /**
  403.  * drm_fb_helper_init - initialize a drm_fb_helper structure
  404.  * @dev: drm device
  405.  * @fb_helper: driver-allocated fbdev helper structure to initialize
  406.  * @crtc_count: maximum number of crtcs to support in this fbdev emulation
  407.  * @max_conn_count: max connector count
  408.  *
  409.  * This allocates the structures for the fbdev helper with the given limits.
  410.  * Note that this won't yet touch the hardware (through the driver interfaces)
  411.  * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
  412.  * to allow driver writes more control over the exact init sequence.
  413.  *
  414.  * Drivers must call drm_fb_helper_prepare() before calling this function.
  415.  *
  416.  * RETURNS:
  417.  * Zero if everything went ok, nonzero otherwise.
  418.  */
  419. int drm_fb_helper_init(struct drm_device *dev,
  420.                        struct drm_fb_helper *fb_helper,
  421.                        int crtc_count, int max_conn_count)
  422. {
  423.         struct drm_crtc *crtc;
  424.         int i;
  425.  
  426.         if (!max_conn_count)
  427.                 return -EINVAL;
  428.  
  429.         fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
  430.         if (!fb_helper->crtc_info)
  431.                 return -ENOMEM;
  432.  
  433.         fb_helper->crtc_count = crtc_count;
  434.         fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
  435.         if (!fb_helper->connector_info) {
  436.                 kfree(fb_helper->crtc_info);
  437.                 return -ENOMEM;
  438.         }
  439.         fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
  440.         fb_helper->connector_count = 0;
  441.  
  442.         for (i = 0; i < crtc_count; i++) {
  443.                 fb_helper->crtc_info[i].mode_set.connectors =
  444.                         kcalloc(max_conn_count,
  445.                                 sizeof(struct drm_connector *),
  446.                                 GFP_KERNEL);
  447.  
  448.                 if (!fb_helper->crtc_info[i].mode_set.connectors)
  449.                         goto out_free;
  450.                 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
  451.         }
  452.  
  453.         i = 0;
  454.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  455.                 fb_helper->crtc_info[i].mode_set.crtc = crtc;
  456.                 i++;
  457.         }
  458.  
  459.         return 0;
  460. out_free:
  461.         drm_fb_helper_crtc_free(fb_helper);
  462.         return -ENOMEM;
  463. }
  464. EXPORT_SYMBOL(drm_fb_helper_init);
  465.  
  466. static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
  467.                      u16 blue, u16 regno, struct fb_info *info)
  468. {
  469.         struct drm_fb_helper *fb_helper = info->par;
  470.         struct drm_framebuffer *fb = fb_helper->fb;
  471.         int pindex;
  472.  
  473.         if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
  474.                 u32 *palette;
  475.                 u32 value;
  476.                 /* place color in psuedopalette */
  477.                 if (regno > 16)
  478.                         return -EINVAL;
  479.                 palette = (u32 *)info->pseudo_palette;
  480.                 red >>= (16 - info->var.red.length);
  481.                 green >>= (16 - info->var.green.length);
  482.                 blue >>= (16 - info->var.blue.length);
  483.                 value = (red << info->var.red.offset) |
  484.                         (green << info->var.green.offset) |
  485.                         (blue << info->var.blue.offset);
  486.                 if (info->var.transp.length > 0) {
  487.                         u32 mask = (1 << info->var.transp.length) - 1;
  488.                         mask <<= info->var.transp.offset;
  489.                         value |= mask;
  490.                 }
  491.                 palette[regno] = value;
  492.                 return 0;
  493.         }
  494.  
  495.         /*
  496.          * The driver really shouldn't advertise pseudo/directcolor
  497.          * visuals if it can't deal with the palette.
  498.          */
  499.         if (WARN_ON(!fb_helper->funcs->gamma_set ||
  500.                     !fb_helper->funcs->gamma_get))
  501.                 return -EINVAL;
  502.  
  503.         pindex = regno;
  504.  
  505.         if (fb->bits_per_pixel == 16) {
  506.                 pindex = regno << 3;
  507.  
  508.                 if (fb->depth == 16 && regno > 63)
  509.                         return -EINVAL;
  510.                 if (fb->depth == 15 && regno > 31)
  511.                         return -EINVAL;
  512.  
  513.                 if (fb->depth == 16) {
  514.                         u16 r, g, b;
  515.                         int i;
  516.                         if (regno < 32) {
  517.                                 for (i = 0; i < 8; i++)
  518.                                         fb_helper->funcs->gamma_set(crtc, red,
  519.                                                 green, blue, pindex + i);
  520.                         }
  521.  
  522.                         fb_helper->funcs->gamma_get(crtc, &r,
  523.                                                     &g, &b,
  524.                                                     pindex >> 1);
  525.  
  526.                         for (i = 0; i < 4; i++)
  527.                                 fb_helper->funcs->gamma_set(crtc, r,
  528.                                                             green, b,
  529.                                                             (pindex >> 1) + i);
  530.                 }
  531.         }
  532.  
  533.         if (fb->depth != 16)
  534.                 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
  535.         return 0;
  536. }
  537.  
  538. /**
  539.  * drm_fb_helper_setcmap - implementation for ->fb_setcmap
  540.  * @cmap: cmap to set
  541.  * @info: fbdev registered by the helper
  542.  */
  543. int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
  544. {
  545.         struct drm_fb_helper *fb_helper = info->par;
  546.         struct drm_device *dev = fb_helper->dev;
  547.         struct drm_crtc_helper_funcs *crtc_funcs;
  548.         u16 *red, *green, *blue, *transp;
  549.         struct drm_crtc *crtc;
  550.         int i, j, rc = 0;
  551.         int start;
  552.  
  553.         drm_modeset_lock_all(dev);
  554.         if (!drm_fb_helper_is_bound(fb_helper)) {
  555.                 drm_modeset_unlock_all(dev);
  556.                 return -EBUSY;
  557.         }
  558.  
  559.                 for (i = 0; i < fb_helper->crtc_count; i++) {
  560.                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
  561.                 crtc_funcs = crtc->helper_private;
  562.  
  563.                 red = cmap->red;
  564.                 green = cmap->green;
  565.                 blue = cmap->blue;
  566.                 transp = cmap->transp;
  567.                 start = cmap->start;
  568.  
  569.                 for (j = 0; j < cmap->len; j++) {
  570.                         u16 hred, hgreen, hblue, htransp = 0xffff;
  571.  
  572.                         hred = *red++;
  573.                         hgreen = *green++;
  574.                         hblue = *blue++;
  575.  
  576.                         if (transp)
  577.                                 htransp = *transp++;
  578.  
  579.                         rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
  580.                         if (rc)
  581.                                 goto out;
  582.                 }
  583.                 if (crtc_funcs->load_lut)
  584.                 crtc_funcs->load_lut(crtc);
  585.         }
  586.  out:
  587.         drm_modeset_unlock_all(dev);
  588.         return rc;
  589. }
  590. EXPORT_SYMBOL(drm_fb_helper_setcmap);
  591.  
  592. /**
  593.  * drm_fb_helper_check_var - implementation for ->fb_check_var
  594.  * @var: screeninfo to check
  595.  * @info: fbdev registered by the helper
  596.  */
  597. int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
  598.                             struct fb_info *info)
  599. {
  600.         struct drm_fb_helper *fb_helper = info->par;
  601.         struct drm_framebuffer *fb = fb_helper->fb;
  602.         int depth;
  603.  
  604.         if (var->pixclock != 0 || in_dbg_master())
  605.                 return -EINVAL;
  606.  
  607.         /* Need to resize the fb object !!! */
  608.         if (var->bits_per_pixel > fb->bits_per_pixel ||
  609.             var->xres > fb->width || var->yres > fb->height ||
  610.             var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
  611.                 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
  612.                           "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
  613.                           var->xres, var->yres, var->bits_per_pixel,
  614.                           var->xres_virtual, var->yres_virtual,
  615.                           fb->width, fb->height, fb->bits_per_pixel);
  616.                 return -EINVAL;
  617.         }
  618.  
  619.         switch (var->bits_per_pixel) {
  620.         case 16:
  621.                 depth = (var->green.length == 6) ? 16 : 15;
  622.                 break;
  623.         case 32:
  624.                 depth = (var->transp.length > 0) ? 32 : 24;
  625.                 break;
  626.         default:
  627.                 depth = var->bits_per_pixel;
  628.                 break;
  629.         }
  630.  
  631.         switch (depth) {
  632.         case 8:
  633.                 var->red.offset = 0;
  634.                 var->green.offset = 0;
  635.                 var->blue.offset = 0;
  636.                 var->red.length = 8;
  637.                 var->green.length = 8;
  638.                 var->blue.length = 8;
  639.                 var->transp.length = 0;
  640.                 var->transp.offset = 0;
  641.                 break;
  642.         case 15:
  643.                 var->red.offset = 10;
  644.                 var->green.offset = 5;
  645.                 var->blue.offset = 0;
  646.                 var->red.length = 5;
  647.                 var->green.length = 5;
  648.                 var->blue.length = 5;
  649.                 var->transp.length = 1;
  650.                 var->transp.offset = 15;
  651.                 break;
  652.         case 16:
  653.                 var->red.offset = 11;
  654.                 var->green.offset = 5;
  655.                 var->blue.offset = 0;
  656.                 var->red.length = 5;
  657.                 var->green.length = 6;
  658.                 var->blue.length = 5;
  659.                 var->transp.length = 0;
  660.                 var->transp.offset = 0;
  661.                 break;
  662.         case 24:
  663.                 var->red.offset = 16;
  664.                 var->green.offset = 8;
  665.                 var->blue.offset = 0;
  666.                 var->red.length = 8;
  667.                 var->green.length = 8;
  668.                 var->blue.length = 8;
  669.                 var->transp.length = 0;
  670.                 var->transp.offset = 0;
  671.                 break;
  672.         case 32:
  673.                 var->red.offset = 16;
  674.                 var->green.offset = 8;
  675.                 var->blue.offset = 0;
  676.                 var->red.length = 8;
  677.                 var->green.length = 8;
  678.                 var->blue.length = 8;
  679.                 var->transp.length = 8;
  680.                 var->transp.offset = 24;
  681.                 break;
  682.         default:
  683.                 return -EINVAL;
  684.         }
  685.         return 0;
  686. }
  687. EXPORT_SYMBOL(drm_fb_helper_check_var);
  688.  
  689. /**
  690.  * drm_fb_helper_set_par - implementation for ->fb_set_par
  691.  * @info: fbdev registered by the helper
  692.  *
  693.  * This will let fbcon do the mode init and is called at initialization time by
  694.  * the fbdev core when registering the driver, and later on through the hotplug
  695.  * callback.
  696.  */
  697. int drm_fb_helper_set_par(struct fb_info *info)
  698. {
  699.         struct drm_fb_helper *fb_helper = info->par;
  700.         struct fb_var_screeninfo *var = &info->var;
  701.  
  702.         if (var->pixclock != 0) {
  703.                 DRM_ERROR("PIXEL CLOCK SET\n");
  704.                 return -EINVAL;
  705.         }
  706.  
  707.         drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
  708.  
  709.         if (fb_helper->delayed_hotplug) {
  710.                 fb_helper->delayed_hotplug = false;
  711.                 drm_fb_helper_hotplug_event(fb_helper);
  712.         }
  713.         return 0;
  714. }
  715. EXPORT_SYMBOL(drm_fb_helper_set_par);
  716.  
  717. /**
  718.  * drm_fb_helper_pan_display - implementation for ->fb_pan_display
  719.  * @var: updated screen information
  720.  * @info: fbdev registered by the helper
  721.  */
  722. int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
  723.                               struct fb_info *info)
  724. {
  725.         struct drm_fb_helper *fb_helper = info->par;
  726.         struct drm_device *dev = fb_helper->dev;
  727.         struct drm_mode_set *modeset;
  728.         int ret = 0;
  729.         int i;
  730.  
  731.         drm_modeset_lock_all(dev);
  732.         if (!drm_fb_helper_is_bound(fb_helper)) {
  733.                 drm_modeset_unlock_all(dev);
  734.                 return -EBUSY;
  735.         }
  736.  
  737.                 for (i = 0; i < fb_helper->crtc_count; i++) {
  738.                 modeset = &fb_helper->crtc_info[i].mode_set;
  739.  
  740.                 modeset->x = var->xoffset;
  741.                 modeset->y = var->yoffset;
  742.  
  743.                 if (modeset->num_connectors) {
  744.                         ret = drm_mode_set_config_internal(modeset);
  745.                         if (!ret) {
  746.                                 info->var.xoffset = var->xoffset;
  747.                                 info->var.yoffset = var->yoffset;
  748.                         }
  749.                 }
  750.         }
  751.         drm_modeset_unlock_all(dev);
  752.         return ret;
  753. }
  754. EXPORT_SYMBOL(drm_fb_helper_pan_display);
  755.  
  756. /*
  757.  * Allocates the backing storage and sets up the fbdev info structure through
  758.  * the ->fb_probe callback and then registers the fbdev and sets up the panic
  759.  * notifier.
  760.  */
  761. static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
  762.                                   int preferred_bpp)
  763. {
  764.         int ret = 0;
  765.         int crtc_count = 0;
  766.         int i;
  767.         struct fb_info *info;
  768.         struct drm_fb_helper_surface_size sizes;
  769.         int gamma_size = 0;
  770.  
  771.         memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
  772.         sizes.surface_depth = 24;
  773.         sizes.surface_bpp = 32;
  774.         sizes.fb_width = (unsigned)-1;
  775.         sizes.fb_height = (unsigned)-1;
  776.  
  777.         /* if driver picks 8 or 16 by default use that
  778.            for both depth/bpp */
  779.         if (preferred_bpp != sizes.surface_bpp)
  780.                 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
  781.  
  782.         /* first up get a count of crtcs now in use and new min/maxes width/heights */
  783.         for (i = 0; i < fb_helper->connector_count; i++) {
  784.                 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
  785.                 struct drm_cmdline_mode *cmdline_mode;
  786.  
  787.                 cmdline_mode = &fb_helper_conn->cmdline_mode;
  788.  
  789.                 if (cmdline_mode->bpp_specified) {
  790.                         switch (cmdline_mode->bpp) {
  791.                         case 8:
  792.                                 sizes.surface_depth = sizes.surface_bpp = 8;
  793.                                 break;
  794.                         case 15:
  795.                                 sizes.surface_depth = 15;
  796.                                 sizes.surface_bpp = 16;
  797.                                 break;
  798.                         case 16:
  799.                                 sizes.surface_depth = sizes.surface_bpp = 16;
  800.                                 break;
  801.                         case 24:
  802.                                 sizes.surface_depth = sizes.surface_bpp = 24;
  803.                                 break;
  804.                         case 32:
  805.     sizes.surface_depth = 24;
  806.     sizes.surface_bpp = 32;
  807.                                 break;
  808.                         }
  809.                         break;
  810.                 }
  811.         }
  812.  
  813.         crtc_count = 0;
  814.         for (i = 0; i < fb_helper->crtc_count; i++) {
  815.                 struct drm_display_mode *desired_mode;
  816.                 desired_mode = fb_helper->crtc_info[i].desired_mode;
  817.  
  818.                 if (desired_mode) {
  819.                         if (gamma_size == 0)
  820.                                 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
  821.                         if (desired_mode->hdisplay < sizes.fb_width)
  822.                                 sizes.fb_width = desired_mode->hdisplay;
  823.                         if (desired_mode->vdisplay < sizes.fb_height)
  824.                                 sizes.fb_height = desired_mode->vdisplay;
  825.                         if (desired_mode->hdisplay > sizes.surface_width)
  826.                                 sizes.surface_width = desired_mode->hdisplay;
  827.                         if (desired_mode->vdisplay > sizes.surface_height)
  828.                                 sizes.surface_height = desired_mode->vdisplay;
  829.                         crtc_count++;
  830.        }
  831.         }
  832.  
  833.         if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
  834.                 /* hmm everyone went away - assume VGA cable just fell out
  835.                    and will come back later. */
  836.                 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
  837.                 sizes.fb_width = sizes.surface_width = 1024;
  838.                 sizes.fb_height = sizes.surface_height = 768;
  839.         }
  840.  
  841.         /* push down into drivers */
  842.         ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
  843.         if (ret < 0)
  844.                 return ret;
  845.  
  846.         info = fb_helper->fbdev;
  847.  
  848.         /*
  849.          * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
  850.          * events, but at init time drm_setup_crtcs needs to be called before
  851.          * the fb is allocated (since we need to figure out the desired size of
  852.          * the fb before we can allocate it ...). Hence we need to fix things up
  853.          * here again.
  854.          */
  855.         for (i = 0; i < fb_helper->crtc_count; i++)
  856.                 if (fb_helper->crtc_info[i].mode_set.num_connectors)
  857.                 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
  858.  
  859.  
  860.                 info->var.pixclock = 0;
  861.  
  862.         list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
  863.  
  864.         return 0;
  865. }
  866.  
  867. /**
  868.  * drm_fb_helper_fill_fix - initializes fixed fbdev information
  869.  * @info: fbdev registered by the helper
  870.  * @pitch: desired pitch
  871.  * @depth: desired depth
  872.  *
  873.  * Helper to fill in the fixed fbdev information useful for a non-accelerated
  874.  * fbdev emulations. Drivers which support acceleration methods which impose
  875.  * additional constraints need to set up their own limits.
  876.  *
  877.  * Drivers should call this (or their equivalent setup code) from their
  878.  * ->fb_probe callback.
  879.  */
  880. void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
  881.                             uint32_t depth)
  882. {
  883.         info->fix.type = FB_TYPE_PACKED_PIXELS;
  884.         info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
  885.                 FB_VISUAL_TRUECOLOR;
  886.         info->fix.mmio_start = 0;
  887.         info->fix.mmio_len = 0;
  888.         info->fix.type_aux = 0;
  889.         info->fix.xpanstep = 1; /* doing it in hw */
  890.         info->fix.ypanstep = 1; /* doing it in hw */
  891.         info->fix.ywrapstep = 0;
  892.         info->fix.accel = FB_ACCEL_NONE;
  893.  
  894.         info->fix.line_length = pitch;
  895.         return;
  896. }
  897. EXPORT_SYMBOL(drm_fb_helper_fill_fix);
  898.  
  899. /**
  900.  * drm_fb_helper_fill_var - initalizes variable fbdev information
  901.  * @info: fbdev instance to set up
  902.  * @fb_helper: fb helper instance to use as template
  903.  * @fb_width: desired fb width
  904.  * @fb_height: desired fb height
  905.  *
  906.  * Sets up the variable fbdev metainformation from the given fb helper instance
  907.  * and the drm framebuffer allocated in fb_helper->fb.
  908.  *
  909.  * Drivers should call this (or their equivalent setup code) from their
  910.  * ->fb_probe callback after having allocated the fbdev backing
  911.  * storage framebuffer.
  912.  */
  913. void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
  914.                             uint32_t fb_width, uint32_t fb_height)
  915. {
  916.         struct drm_framebuffer *fb = fb_helper->fb;
  917.         info->pseudo_palette = fb_helper->pseudo_palette;
  918.         info->var.xres_virtual = fb->width;
  919.         info->var.yres_virtual = fb->height;
  920.         info->var.bits_per_pixel = fb->bits_per_pixel;
  921.         info->var.accel_flags = FB_ACCELF_TEXT;
  922.         info->var.xoffset = 0;
  923.         info->var.yoffset = 0;
  924.         info->var.activate = FB_ACTIVATE_NOW;
  925.         info->var.height = -1;
  926.         info->var.width = -1;
  927.  
  928.         switch (fb->depth) {
  929.         case 8:
  930.                 info->var.red.offset = 0;
  931.                 info->var.green.offset = 0;
  932.                 info->var.blue.offset = 0;
  933.                 info->var.red.length = 8; /* 8bit DAC */
  934.                 info->var.green.length = 8;
  935.                 info->var.blue.length = 8;
  936.                 info->var.transp.offset = 0;
  937.                 info->var.transp.length = 0;
  938.                 break;
  939.         case 15:
  940.                 info->var.red.offset = 10;
  941.                 info->var.green.offset = 5;
  942.                 info->var.blue.offset = 0;
  943.                 info->var.red.length = 5;
  944.                 info->var.green.length = 5;
  945.                 info->var.blue.length = 5;
  946.                 info->var.transp.offset = 15;
  947.                 info->var.transp.length = 1;
  948.                 break;
  949.         case 16:
  950.                 info->var.red.offset = 11;
  951.                 info->var.green.offset = 5;
  952.                 info->var.blue.offset = 0;
  953.                 info->var.red.length = 5;
  954.                 info->var.green.length = 6;
  955.                 info->var.blue.length = 5;
  956.                 info->var.transp.offset = 0;
  957.                 break;
  958.         case 24:
  959.                 info->var.red.offset = 16;
  960.                 info->var.green.offset = 8;
  961.                 info->var.blue.offset = 0;
  962.                 info->var.red.length = 8;
  963.                 info->var.green.length = 8;
  964.                 info->var.blue.length = 8;
  965.                 info->var.transp.offset = 0;
  966.                 info->var.transp.length = 0;
  967.                 break;
  968.         case 32:
  969.                 info->var.red.offset = 16;
  970.                 info->var.green.offset = 8;
  971.                 info->var.blue.offset = 0;
  972.                 info->var.red.length = 8;
  973.                 info->var.green.length = 8;
  974.                 info->var.blue.length = 8;
  975.                 info->var.transp.offset = 24;
  976.                 info->var.transp.length = 8;
  977.                 break;
  978.         default:
  979.                 break;
  980.         }
  981.  
  982.         info->var.xres = fb_width;
  983.         info->var.yres = fb_height;
  984. }
  985. EXPORT_SYMBOL(drm_fb_helper_fill_var);
  986.  
  987. static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
  988.                                                uint32_t maxX,
  989.                                                uint32_t maxY)
  990. {
  991.         struct drm_connector *connector;
  992.         int count = 0;
  993.         int i;
  994.  
  995.         for (i = 0; i < fb_helper->connector_count; i++) {
  996.                 connector = fb_helper->connector_info[i]->connector;
  997.                 count += connector->funcs->fill_modes(connector, maxX, maxY);
  998.         }
  999.  
  1000.         return count;
  1001. }
  1002.  
  1003. struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
  1004. {
  1005.         struct drm_display_mode *mode;
  1006.  
  1007.         list_for_each_entry(mode, &fb_connector->connector->modes, head) {
  1008.                 if (mode->hdisplay > width ||
  1009.                     mode->vdisplay > height)
  1010.                         continue;
  1011.                 if (mode->type & DRM_MODE_TYPE_PREFERRED)
  1012.                         return mode;
  1013.         }
  1014.         return NULL;
  1015. }
  1016. EXPORT_SYMBOL(drm_has_preferred_mode);
  1017.  
  1018. static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
  1019. {
  1020.         struct drm_cmdline_mode *cmdline_mode;
  1021.         cmdline_mode = &fb_connector->cmdline_mode;
  1022.         return cmdline_mode->specified;
  1023. }
  1024.  
  1025. struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
  1026.                                                       int width, int height)
  1027. {
  1028.         struct drm_cmdline_mode *cmdline_mode;
  1029.         struct drm_display_mode *mode = NULL;
  1030.         bool prefer_non_interlace;
  1031.  
  1032.         return NULL;
  1033.  
  1034.         cmdline_mode = &fb_helper_conn->cmdline_mode;
  1035.         if (cmdline_mode->specified == false)
  1036.                 return mode;
  1037.  
  1038.         /* attempt to find a matching mode in the list of modes
  1039.          *  we have gotten so far, if not add a CVT mode that conforms
  1040.          */
  1041.         if (cmdline_mode->rb || cmdline_mode->margins)
  1042.                 goto create_mode;
  1043.  
  1044.         prefer_non_interlace = !cmdline_mode->interlace;
  1045.  again:
  1046.         list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  1047.                 /* check width/height */
  1048.                 if (mode->hdisplay != cmdline_mode->xres ||
  1049.                     mode->vdisplay != cmdline_mode->yres)
  1050.                         continue;
  1051.  
  1052.                 if (cmdline_mode->refresh_specified) {
  1053.                         if (mode->vrefresh != cmdline_mode->refresh)
  1054.                                 continue;
  1055.                 }
  1056.  
  1057.                 if (cmdline_mode->interlace) {
  1058.                         if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
  1059.                                 continue;
  1060.                 } else if (prefer_non_interlace) {
  1061.                         if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1062.                                 continue;
  1063.                 }
  1064.                 return mode;
  1065.         }
  1066.  
  1067.         if (prefer_non_interlace) {
  1068.                 prefer_non_interlace = false;
  1069.                 goto again;
  1070.         }
  1071.  
  1072. create_mode:
  1073.         mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
  1074.                                                  cmdline_mode);
  1075.         list_add(&mode->head, &fb_helper_conn->connector->modes);
  1076.         return mode;
  1077. }
  1078. EXPORT_SYMBOL(drm_pick_cmdline_mode);
  1079.  
  1080. static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
  1081. {
  1082.         bool enable;
  1083.  
  1084.         if (strict)
  1085.                 enable = connector->status == connector_status_connected;
  1086.         else
  1087.                 enable = connector->status != connector_status_disconnected;
  1088.  
  1089.         return enable;
  1090. }
  1091.  
  1092. static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
  1093.                                   bool *enabled)
  1094. {
  1095.         bool any_enabled = false;
  1096.         struct drm_connector *connector;
  1097.         int i = 0;
  1098.  
  1099.         for (i = 0; i < fb_helper->connector_count; i++) {
  1100.                 connector = fb_helper->connector_info[i]->connector;
  1101.                 enabled[i] = drm_connector_enabled(connector, true);
  1102.                 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
  1103.                           enabled[i] ? "yes" : "no");
  1104.                 any_enabled |= enabled[i];
  1105.         }
  1106.  
  1107.         if (any_enabled)
  1108.                 return;
  1109.  
  1110.         for (i = 0; i < fb_helper->connector_count; i++) {
  1111.                 connector = fb_helper->connector_info[i]->connector;
  1112.                 enabled[i] = drm_connector_enabled(connector, false);
  1113.         }
  1114. }
  1115.  
  1116. static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
  1117.                               struct drm_display_mode **modes,
  1118.                               bool *enabled, int width, int height)
  1119. {
  1120.         int count, i, j;
  1121.         bool can_clone = false;
  1122.         struct drm_fb_helper_connector *fb_helper_conn;
  1123.         struct drm_display_mode *dmt_mode, *mode;
  1124.  
  1125.         /* only contemplate cloning in the single crtc case */
  1126.         if (fb_helper->crtc_count > 1)
  1127.                 return false;
  1128.  
  1129.         count = 0;
  1130.         for (i = 0; i < fb_helper->connector_count; i++) {
  1131.                 if (enabled[i])
  1132.                         count++;
  1133.         }
  1134.  
  1135.         /* only contemplate cloning if more than one connector is enabled */
  1136.         if (count <= 1)
  1137.                 return false;
  1138.  
  1139.         /* check the command line or if nothing common pick 1024x768 */
  1140.         can_clone = true;
  1141.         for (i = 0; i < fb_helper->connector_count; i++) {
  1142.                 if (!enabled[i])
  1143.                         continue;
  1144.                 fb_helper_conn = fb_helper->connector_info[i];
  1145.                 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
  1146.                 if (!modes[i]) {
  1147.                         can_clone = false;
  1148.                         break;
  1149.                 }
  1150.                 for (j = 0; j < i; j++) {
  1151.                         if (!enabled[j])
  1152.                                 continue;
  1153.                         if (!drm_mode_equal(modes[j], modes[i]))
  1154.                                 can_clone = false;
  1155.                 }
  1156.         }
  1157.  
  1158.         if (can_clone) {
  1159.                 DRM_DEBUG_KMS("can clone using command line\n");
  1160.                 return true;
  1161.         }
  1162.  
  1163.         /* try and find a 1024x768 mode on each connector */
  1164.         can_clone = true;
  1165.         dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
  1166.  
  1167.         for (i = 0; i < fb_helper->connector_count; i++) {
  1168.  
  1169.                 if (!enabled[i])
  1170.                         continue;
  1171.  
  1172.                 fb_helper_conn = fb_helper->connector_info[i];
  1173.                 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  1174.                         if (drm_mode_equal(mode, dmt_mode))
  1175.                                 modes[i] = mode;
  1176.                 }
  1177.                 if (!modes[i])
  1178.                         can_clone = false;
  1179.         }
  1180.  
  1181.         if (can_clone) {
  1182.                 DRM_DEBUG_KMS("can clone using 1024x768\n");
  1183.                 return true;
  1184.         }
  1185.         DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
  1186.         return false;
  1187. }
  1188.  
  1189. static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
  1190.                                  struct drm_display_mode **modes,
  1191.                                  bool *enabled, int width, int height)
  1192. {
  1193.         struct drm_fb_helper_connector *fb_helper_conn;
  1194.         int i;
  1195.  
  1196.         for (i = 0; i < fb_helper->connector_count; i++) {
  1197.                 fb_helper_conn = fb_helper->connector_info[i];
  1198.  
  1199.                 if (enabled[i] == false)
  1200.                         continue;
  1201.  
  1202.                 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
  1203.                               fb_helper_conn->connector->base.id);
  1204.  
  1205.                 /* got for command line mode first */
  1206.                 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
  1207.                 if (!modes[i]) {
  1208.                         DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
  1209.                                       fb_helper_conn->connector->base.id);
  1210.                         modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
  1211.                 }
  1212.                 /* No preferred modes, pick one off the list */
  1213.                 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
  1214.                         list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
  1215.                                 break;
  1216.                 }
  1217.                 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
  1218.                           "none");
  1219.         }
  1220.         return true;
  1221. }
  1222.  
  1223. static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
  1224.                           struct drm_fb_helper_crtc **best_crtcs,
  1225.                           struct drm_display_mode **modes,
  1226.                           int n, int width, int height)
  1227. {
  1228.         int c, o;
  1229.         struct drm_device *dev = fb_helper->dev;
  1230.         struct drm_connector *connector;
  1231.         struct drm_connector_helper_funcs *connector_funcs;
  1232.         struct drm_encoder *encoder;
  1233.         int my_score, best_score, score;
  1234.         struct drm_fb_helper_crtc **crtcs, *crtc;
  1235.         struct drm_fb_helper_connector *fb_helper_conn;
  1236.  
  1237.         if (n == fb_helper->connector_count)
  1238.                 return 0;
  1239.  
  1240.         fb_helper_conn = fb_helper->connector_info[n];
  1241.         connector = fb_helper_conn->connector;
  1242.  
  1243.         best_crtcs[n] = NULL;
  1244.         best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
  1245.         if (modes[n] == NULL)
  1246.                 return best_score;
  1247.  
  1248.         crtcs = kzalloc(dev->mode_config.num_connector *
  1249.                         sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1250.         if (!crtcs)
  1251.                 return best_score;
  1252.  
  1253.         my_score = 1;
  1254.         if (connector->status == connector_status_connected)
  1255.                 my_score++;
  1256.         if (drm_has_cmdline_mode(fb_helper_conn))
  1257.                 my_score++;
  1258.         if (drm_has_preferred_mode(fb_helper_conn, width, height))
  1259.                 my_score++;
  1260.  
  1261.         connector_funcs = connector->helper_private;
  1262.         encoder = connector_funcs->best_encoder(connector);
  1263.         if (!encoder)
  1264.                 goto out;
  1265.  
  1266.         /* select a crtc for this connector and then attempt to configure
  1267.            remaining connectors */
  1268.         for (c = 0; c < fb_helper->crtc_count; c++) {
  1269.                 crtc = &fb_helper->crtc_info[c];
  1270.  
  1271.                 if ((encoder->possible_crtcs & (1 << c)) == 0)
  1272.                         continue;
  1273.  
  1274.                 for (o = 0; o < n; o++)
  1275.                         if (best_crtcs[o] == crtc)
  1276.                                 break;
  1277.  
  1278.                 if (o < n) {
  1279.                         /* ignore cloning unless only a single crtc */
  1280.                         if (fb_helper->crtc_count > 1)
  1281.                                 continue;
  1282.  
  1283.                         if (!drm_mode_equal(modes[o], modes[n]))
  1284.                                 continue;
  1285.                 }
  1286.  
  1287.                 crtcs[n] = crtc;
  1288.                 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
  1289.                 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
  1290.                                                   width, height);
  1291.                 if (score > best_score) {
  1292.                         best_score = score;
  1293.                         memcpy(best_crtcs, crtcs,
  1294.                                dev->mode_config.num_connector *
  1295.                                sizeof(struct drm_fb_helper_crtc *));
  1296.                 }
  1297.         }
  1298. out:
  1299.         kfree(crtcs);
  1300.         return best_score;
  1301. }
  1302.  
  1303. static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
  1304. {
  1305.         struct drm_device *dev = fb_helper->dev;
  1306.         struct drm_fb_helper_crtc **crtcs;
  1307.         struct drm_display_mode **modes;
  1308.         struct drm_mode_set *modeset;
  1309.         bool *enabled;
  1310.         int width, height;
  1311.         int i;
  1312.  
  1313.         DRM_DEBUG_KMS("\n");
  1314.  
  1315.         width = dev->mode_config.max_width;
  1316.         height = dev->mode_config.max_height;
  1317.  
  1318.         crtcs = kcalloc(dev->mode_config.num_connector,
  1319.                         sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1320.         modes = kcalloc(dev->mode_config.num_connector,
  1321.                         sizeof(struct drm_display_mode *), GFP_KERNEL);
  1322.         enabled = kcalloc(dev->mode_config.num_connector,
  1323.                           sizeof(bool), GFP_KERNEL);
  1324.         if (!crtcs || !modes || !enabled) {
  1325.                 DRM_ERROR("Memory allocation failed\n");
  1326.                 goto out;
  1327.         }
  1328.  
  1329.  
  1330.         drm_enable_connectors(fb_helper, enabled);
  1331.  
  1332.         if (!(fb_helper->funcs->initial_config &&
  1333.               fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
  1334.                                                enabled, width, height))) {
  1335.                 memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
  1336.                 memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
  1337.  
  1338.                 if (!drm_target_cloned(fb_helper,
  1339.                                        modes, enabled, width, height) &&
  1340.                     !drm_target_preferred(fb_helper,
  1341.                                           modes, enabled, width, height))
  1342.                         DRM_ERROR("Unable to find initial modes\n");
  1343.  
  1344.                 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
  1345.                               width, height);
  1346.  
  1347.         drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
  1348.         }
  1349.  
  1350.         /* need to set the modesets up here for use later */
  1351.         /* fill out the connector<->crtc mappings into the modesets */
  1352.         for (i = 0; i < fb_helper->crtc_count; i++) {
  1353.                 modeset = &fb_helper->crtc_info[i].mode_set;
  1354.                 modeset->num_connectors = 0;
  1355.                 modeset->fb = NULL;
  1356.         }
  1357.  
  1358.         for (i = 0; i < fb_helper->connector_count; i++) {
  1359.                 struct drm_display_mode *mode = modes[i];
  1360.                 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
  1361.                 modeset = &fb_crtc->mode_set;
  1362.  
  1363.                 if (mode && fb_crtc) {
  1364.                         DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
  1365.                                       mode->name, fb_crtc->mode_set.crtc->base.id);
  1366.                         fb_crtc->desired_mode = mode;
  1367.                         if (modeset->mode)
  1368.                                 drm_mode_destroy(dev, modeset->mode);
  1369.                         modeset->mode = drm_mode_duplicate(dev,
  1370.                                                            fb_crtc->desired_mode);
  1371.                         modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
  1372.                         modeset->fb = fb_helper->fb;
  1373.                 }
  1374.         }
  1375.  
  1376.         /* Clear out any old modes if there are no more connected outputs. */
  1377.         for (i = 0; i < fb_helper->crtc_count; i++) {
  1378.                 modeset = &fb_helper->crtc_info[i].mode_set;
  1379.                 if (modeset->num_connectors == 0) {
  1380.                         BUG_ON(modeset->fb);
  1381.                         BUG_ON(modeset->num_connectors);
  1382.                         if (modeset->mode)
  1383.                                 drm_mode_destroy(dev, modeset->mode);
  1384.                         modeset->mode = NULL;
  1385.                 }
  1386.         }
  1387. out:
  1388.         kfree(crtcs);
  1389.         kfree(modes);
  1390.         kfree(enabled);
  1391. }
  1392.  
  1393. /**
  1394.  * drm_fb_helper_initial_config - setup a sane initial connector configuration
  1395.  * @fb_helper: fb_helper device struct
  1396.  * @bpp_sel: bpp value to use for the framebuffer configuration
  1397.  *
  1398.  * Scans the CRTCs and connectors and tries to put together an initial setup.
  1399.  * At the moment, this is a cloned configuration across all heads with
  1400.  * a new framebuffer object as the backing store.
  1401.  *
  1402.  * Note that this also registers the fbdev and so allows userspace to call into
  1403.  * the driver through the fbdev interfaces.
  1404.  *
  1405.  * This function will call down into the ->fb_probe callback to let
  1406.  * the driver allocate and initialize the fbdev info structure and the drm
  1407.  * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
  1408.  * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
  1409.  * values for the fbdev info structure.
  1410.  *
  1411.  * RETURNS:
  1412.  * Zero if everything went ok, nonzero otherwise.
  1413.  */
  1414. bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
  1415. {
  1416.         struct drm_device *dev = fb_helper->dev;
  1417.         int count = 0;
  1418.  
  1419. //   drm_fb_helper_parse_command_line(fb_helper);
  1420.  
  1421.         mutex_lock(&dev->mode_config.mutex);
  1422.         count = drm_fb_helper_probe_connector_modes(fb_helper,
  1423.                                                     dev->mode_config.max_width,
  1424.                                                     dev->mode_config.max_height);
  1425.         mutex_unlock(&dev->mode_config.mutex);
  1426.         /*
  1427.          * we shouldn't end up with no modes here.
  1428.          */
  1429.         if (count == 0)
  1430.                 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
  1431.  
  1432.         drm_setup_crtcs(fb_helper);
  1433.  
  1434.         return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
  1435. }
  1436. EXPORT_SYMBOL(drm_fb_helper_initial_config);
  1437.  
  1438. /**
  1439.  * drm_fb_helper_hotplug_event - respond to a hotplug notification by
  1440.  *                               probing all the outputs attached to the fb
  1441.  * @fb_helper: the drm_fb_helper
  1442.  *
  1443.  * Scan the connectors attached to the fb_helper and try to put together a
  1444.  * setup after *notification of a change in output configuration.
  1445.  *
  1446.  * Called at runtime, takes the mode config locks to be able to check/change the
  1447.  * modeset configuration. Must be run from process context (which usually means
  1448.  * either the output polling work or a work item launched from the driver's
  1449.  * hotplug interrupt).
  1450.  *
  1451.  * Note that drivers may call this even before calling
  1452.  * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
  1453.  * for a race-free fbcon setup and will make sure that the fbdev emulation will
  1454.  * not miss any hotplug events.
  1455.  *
  1456.  * RETURNS:
  1457.  * 0 on success and a non-zero error code otherwise.
  1458.  */
  1459. int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
  1460. {
  1461.         struct drm_device *dev = fb_helper->dev;
  1462.         u32 max_width, max_height;
  1463.  
  1464.         mutex_lock(&fb_helper->dev->mode_config.mutex);
  1465.         if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
  1466.                 fb_helper->delayed_hotplug = true;
  1467.                 mutex_unlock(&fb_helper->dev->mode_config.mutex);
  1468.                 return 0;
  1469.         }
  1470.         DRM_DEBUG_KMS("\n");
  1471.  
  1472.         max_width = fb_helper->fb->width;
  1473.         max_height = fb_helper->fb->height;
  1474.  
  1475.         drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
  1476.         mutex_unlock(&fb_helper->dev->mode_config.mutex);
  1477.  
  1478.         drm_modeset_lock_all(dev);
  1479.         drm_setup_crtcs(fb_helper);
  1480.         drm_modeset_unlock_all(dev);
  1481.         drm_fb_helper_set_par(fb_helper->fbdev);
  1482.  
  1483.         return 0;
  1484. }
  1485. EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
  1486.  
  1487. /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
  1488.  * but the module doesn't depend on any fb console symbols.  At least
  1489.  * attempt to load fbcon to avoid leaving the system without a usable console.
  1490.  */
  1491. #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
  1492. static int __init drm_fb_helper_modinit(void)
  1493. {
  1494.         const char *name = "fbcon";
  1495.         struct module *fbcon;
  1496.  
  1497.         mutex_lock(&module_mutex);
  1498.         fbcon = find_module(name);
  1499.         mutex_unlock(&module_mutex);
  1500.  
  1501.         if (!fbcon)
  1502.                 request_module_nowait(name);
  1503.         return 0;
  1504. }
  1505.  
  1506. module_init(drm_fb_helper_modinit);
  1507. #endif
  1508.