2 * Copyright (c) 2006-2009 Red Hat Inc.
3 * Copyright (c) 2006-2008 Intel Corporation
4 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
6 * DRM framebuffer helper functions
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.
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
27 * Dave Airlie <airlied@linux.ie>
28 * Jesse Barnes <jesse.barnes@intel.com>
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #include <linux/kernel.h>
33 #include <linux/sysrq.h>
34 #include <linux/slab.h>
36 #include <linux/module.h>
38 #include <drm/drm_crtc.h>
39 #include <drm/drm_fb_helper.h>
40 #include <drm/drm_crtc_helper.h>
42 static LIST_HEAD(kernel_fb_helper_list);
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
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().
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.
65 * All other functions exported by the fb helper library can be used to
66 * implement the fbdev driver interface by the driver.
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().
83 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
85 * @fb_helper: fbdev initialized with drm_fb_helper_init
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.
92 * Since this is part of the initial setup before the fbdev is published, no
93 * locking is required.
95 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
97 struct drm_device *dev = fb_helper->dev;
98 struct drm_connector *connector;
101 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
102 struct drm_fb_helper_connector *fb_helper_connector;
104 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
105 if (!fb_helper_connector)
108 fb_helper_connector->connector = connector;
109 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
113 for (i = 0; i < fb_helper->connector_count; i++) {
114 kfree(fb_helper->connector_info[i]);
115 fb_helper->connector_info[i] = NULL;
117 fb_helper->connector_count = 0;
120 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
122 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector)
124 struct drm_fb_helper_connector **temp;
125 struct drm_fb_helper_connector *fb_helper_connector;
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);
133 fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1;
134 fb_helper->connector_info = temp;
138 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
139 if (!fb_helper_connector)
142 fb_helper_connector->connector = connector;
143 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
146 EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
148 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
149 struct drm_connector *connector)
151 struct drm_fb_helper_connector *fb_helper_connector;
154 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
156 for (i = 0; i < fb_helper->connector_count; i++) {
157 if (fb_helper->connector_info[i]->connector == connector)
161 if (i == fb_helper->connector_count)
163 fb_helper_connector = fb_helper->connector_info[i];
165 for (j = i + 1; j < fb_helper->connector_count; j++) {
166 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
168 fb_helper->connector_count--;
169 kfree(fb_helper_connector);
172 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
174 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
176 struct drm_fb_helper_connector *fb_helper_conn;
179 for (i = 0; i < fb_helper->connector_count; i++) {
180 struct drm_cmdline_mode *mode;
181 struct drm_connector *connector;
184 fb_helper_conn = fb_helper->connector_info[i];
185 connector = fb_helper_conn->connector;
186 mode = &fb_helper_conn->cmdline_mode;
188 /* do something on return - turn off connector maybe */
189 if (fb_get_options(connector->name, &option))
192 if (drm_mode_parse_command_line_for_connector(option,
197 switch (mode->force) {
201 case DRM_FORCE_ON_DIGITAL:
210 DRM_INFO("forcing %s connector %s\n",
212 connector->force = mode->force;
215 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
217 mode->xres, mode->yres,
218 mode->refresh_specified ? mode->refresh : 60,
219 mode->rb ? " reduced blanking" : "",
220 mode->margins ? " with margins" : "",
221 mode->interlace ? " interlaced" : "");
228 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
230 uint16_t *r_base, *g_base, *b_base;
233 if (helper->funcs->gamma_get == NULL)
236 r_base = crtc->gamma_store;
237 g_base = r_base + crtc->gamma_size;
238 b_base = g_base + crtc->gamma_size;
240 for (i = 0; i < crtc->gamma_size; i++)
241 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
244 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
246 uint16_t *r_base, *g_base, *b_base;
248 if (crtc->funcs->gamma_set == NULL)
251 r_base = crtc->gamma_store;
252 g_base = r_base + crtc->gamma_size;
253 b_base = g_base + crtc->gamma_size;
255 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
259 * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
260 * @info: fbdev registered by the helper
262 int drm_fb_helper_debug_enter(struct fb_info *info)
264 struct drm_fb_helper *helper = info->par;
265 struct drm_crtc_helper_funcs *funcs;
268 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
269 for (i = 0; i < helper->crtc_count; i++) {
270 struct drm_mode_set *mode_set =
271 &helper->crtc_info[i].mode_set;
273 if (!mode_set->crtc->enabled)
276 funcs = mode_set->crtc->helper_private;
277 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
278 funcs->mode_set_base_atomic(mode_set->crtc,
282 ENTER_ATOMIC_MODE_SET);
288 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
290 /* Find the real fb for a given fb helper CRTC */
291 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
293 struct drm_device *dev = crtc->dev;
296 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
297 if (crtc->base.id == c->base.id)
298 return c->primary->fb;
305 * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
306 * @info: fbdev registered by the helper
308 int drm_fb_helper_debug_leave(struct fb_info *info)
310 struct drm_fb_helper *helper = info->par;
311 struct drm_crtc *crtc;
312 struct drm_crtc_helper_funcs *funcs;
313 struct drm_framebuffer *fb;
316 for (i = 0; i < helper->crtc_count; i++) {
317 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
318 crtc = mode_set->crtc;
319 funcs = crtc->helper_private;
320 fb = drm_mode_config_fb(crtc);
326 DRM_ERROR("no fb to restore??\n");
330 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
331 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
332 crtc->y, LEAVE_ATOMIC_MODE_SET);
337 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
339 static bool restore_fbdev_mode(struct drm_fb_helper *fb_helper)
341 struct drm_device *dev = fb_helper->dev;
342 struct drm_plane *plane;
346 drm_warn_on_modeset_not_all_locked(dev);
348 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
349 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
350 drm_plane_force_disable(plane);
352 for (i = 0; i < fb_helper->crtc_count; i++) {
353 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
354 struct drm_crtc *crtc = mode_set->crtc;
357 if (crtc->funcs->cursor_set) {
358 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
363 ret = drm_mode_set_config_internal(mode_set);
370 * drm_fb_helper_restore_fbdev_mode - restore fbdev configuration
371 * @fb_helper: fbcon to restore
373 * This should be called from driver's drm ->lastclose callback
374 * when implementing an fbcon on top of kms using this helper. This ensures that
375 * the user isn't greeted with a black screen when e.g. X dies.
377 * Use this variant if you need to bypass locking (panic), or already
378 * hold all modeset locks. Otherwise use drm_fb_helper_restore_fbdev_mode_unlocked()
380 static bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
382 return restore_fbdev_mode(fb_helper);
386 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
387 * @fb_helper: fbcon to restore
389 * This should be called from driver's drm ->lastclose callback
390 * when implementing an fbcon on top of kms using this helper. This ensures that
391 * the user isn't greeted with a black screen when e.g. X dies.
393 bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
395 struct drm_device *dev = fb_helper->dev;
397 drm_modeset_lock_all(dev);
398 ret = restore_fbdev_mode(fb_helper);
399 drm_modeset_unlock_all(dev);
402 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
405 * restore fbcon display for all kms driver's using this helper, used for sysrq
406 * and panic handling.
408 static bool drm_fb_helper_force_kernel_mode(void)
410 bool ret, error = false;
411 struct drm_fb_helper *helper;
413 if (list_empty(&kernel_fb_helper_list))
416 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
417 struct drm_device *dev = helper->dev;
419 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
422 /* NOTE: we use lockless flag below to avoid grabbing other
423 * modeset locks. So just trylock the underlying mutex
426 if (!mutex_trylock(&dev->mode_config.mutex)) {
431 ret = drm_fb_helper_restore_fbdev_mode(helper);
435 mutex_unlock(&dev->mode_config.mutex);
440 static int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
444 * It's a waste of time and effort to switch back to text console
445 * if the kernel should reboot before panic messages can be seen.
447 if (panic_timeout < 0)
450 pr_err("panic occurred, switching back to text console\n");
451 return drm_fb_helper_force_kernel_mode();
454 static struct notifier_block paniced = {
455 .notifier_call = drm_fb_helper_panic,
458 static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
460 struct drm_device *dev = fb_helper->dev;
461 struct drm_crtc *crtc;
462 int bound = 0, crtcs_bound = 0;
464 /* Sometimes user space wants everything disabled, so don't steal the
465 * display if there's a master. */
466 if (dev->primary->master)
469 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
470 if (crtc->primary->fb)
472 if (crtc->primary->fb == fb_helper->fb)
476 if (bound < crtcs_bound)
482 #ifdef CONFIG_MAGIC_SYSRQ
483 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
486 ret = drm_fb_helper_force_kernel_mode();
488 DRM_ERROR("Failed to restore crtc configuration\n");
490 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
492 static void drm_fb_helper_sysrq(int dummy1)
494 schedule_work(&drm_fb_helper_restore_work);
497 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
498 .handler = drm_fb_helper_sysrq,
499 .help_msg = "force-fb(V)",
500 .action_msg = "Restore framebuffer console",
503 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
506 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
508 struct drm_fb_helper *fb_helper = info->par;
509 struct drm_device *dev = fb_helper->dev;
510 struct drm_crtc *crtc;
511 struct drm_connector *connector;
515 * fbdev->blank can be called from irq context in case of a panic.
516 * Since we already have our own special panic handler which will
517 * restore the fbdev console mode completely, just bail out early.
519 if (oops_in_progress)
523 * For each CRTC in this fb, turn the connectors on/off.
525 drm_modeset_lock_all(dev);
526 if (!drm_fb_helper_is_bound(fb_helper)) {
527 drm_modeset_unlock_all(dev);
531 for (i = 0; i < fb_helper->crtc_count; i++) {
532 crtc = fb_helper->crtc_info[i].mode_set.crtc;
537 /* Walk the connectors & encoders on this fb turning them on/off */
538 for (j = 0; j < fb_helper->connector_count; j++) {
539 connector = fb_helper->connector_info[j]->connector;
540 connector->funcs->dpms(connector, dpms_mode);
541 drm_object_property_set_value(&connector->base,
542 dev->mode_config.dpms_property, dpms_mode);
545 drm_modeset_unlock_all(dev);
549 * drm_fb_helper_blank - implementation for ->fb_blank
550 * @blank: desired blanking state
551 * @info: fbdev registered by the helper
553 int drm_fb_helper_blank(int blank, struct fb_info *info)
556 /* Display: On; HSync: On, VSync: On */
557 case FB_BLANK_UNBLANK:
558 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
560 /* Display: Off; HSync: On, VSync: On */
561 case FB_BLANK_NORMAL:
562 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
564 /* Display: Off; HSync: Off, VSync: On */
565 case FB_BLANK_HSYNC_SUSPEND:
566 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
568 /* Display: Off; HSync: On, VSync: Off */
569 case FB_BLANK_VSYNC_SUSPEND:
570 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
572 /* Display: Off; HSync: Off, VSync: Off */
573 case FB_BLANK_POWERDOWN:
574 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
579 EXPORT_SYMBOL(drm_fb_helper_blank);
581 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
585 for (i = 0; i < helper->connector_count; i++)
586 kfree(helper->connector_info[i]);
587 kfree(helper->connector_info);
588 for (i = 0; i < helper->crtc_count; i++) {
589 kfree(helper->crtc_info[i].mode_set.connectors);
590 if (helper->crtc_info[i].mode_set.mode)
591 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
593 kfree(helper->crtc_info);
597 * drm_fb_helper_prepare - setup a drm_fb_helper structure
599 * @helper: driver-allocated fbdev helper structure to set up
600 * @funcs: pointer to structure of functions associate with this helper
602 * Sets up the bare minimum to make the framebuffer helper usable. This is
603 * useful to implement race-free initialization of the polling helpers.
605 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
606 const struct drm_fb_helper_funcs *funcs)
608 INIT_LIST_HEAD(&helper->kernel_fb_list);
609 helper->funcs = funcs;
612 EXPORT_SYMBOL(drm_fb_helper_prepare);
615 * drm_fb_helper_init - initialize a drm_fb_helper structure
617 * @fb_helper: driver-allocated fbdev helper structure to initialize
618 * @crtc_count: maximum number of crtcs to support in this fbdev emulation
619 * @max_conn_count: max connector count
621 * This allocates the structures for the fbdev helper with the given limits.
622 * Note that this won't yet touch the hardware (through the driver interfaces)
623 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
624 * to allow driver writes more control over the exact init sequence.
626 * Drivers must call drm_fb_helper_prepare() before calling this function.
629 * Zero if everything went ok, nonzero otherwise.
631 int drm_fb_helper_init(struct drm_device *dev,
632 struct drm_fb_helper *fb_helper,
633 int crtc_count, int max_conn_count)
635 struct drm_crtc *crtc;
641 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
642 if (!fb_helper->crtc_info)
645 fb_helper->crtc_count = crtc_count;
646 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
647 if (!fb_helper->connector_info) {
648 kfree(fb_helper->crtc_info);
651 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
652 fb_helper->connector_count = 0;
654 for (i = 0; i < crtc_count; i++) {
655 fb_helper->crtc_info[i].mode_set.connectors =
656 kcalloc(max_conn_count,
657 sizeof(struct drm_connector *),
660 if (!fb_helper->crtc_info[i].mode_set.connectors)
662 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
666 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
667 fb_helper->crtc_info[i].mode_set.crtc = crtc;
673 drm_fb_helper_crtc_free(fb_helper);
676 EXPORT_SYMBOL(drm_fb_helper_init);
678 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
680 if (!list_empty(&fb_helper->kernel_fb_list)) {
681 list_del(&fb_helper->kernel_fb_list);
682 if (list_empty(&kernel_fb_helper_list)) {
683 pr_info("drm: unregistered panic notifier\n");
684 atomic_notifier_chain_unregister(&panic_notifier_list,
686 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
690 drm_fb_helper_crtc_free(fb_helper);
693 EXPORT_SYMBOL(drm_fb_helper_fini);
695 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
696 u16 blue, u16 regno, struct fb_info *info)
698 struct drm_fb_helper *fb_helper = info->par;
699 struct drm_framebuffer *fb = fb_helper->fb;
702 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
705 /* place color in psuedopalette */
708 palette = (u32 *)info->pseudo_palette;
709 red >>= (16 - info->var.red.length);
710 green >>= (16 - info->var.green.length);
711 blue >>= (16 - info->var.blue.length);
712 value = (red << info->var.red.offset) |
713 (green << info->var.green.offset) |
714 (blue << info->var.blue.offset);
715 if (info->var.transp.length > 0) {
716 u32 mask = (1 << info->var.transp.length) - 1;
717 mask <<= info->var.transp.offset;
720 palette[regno] = value;
725 * The driver really shouldn't advertise pseudo/directcolor
726 * visuals if it can't deal with the palette.
728 if (WARN_ON(!fb_helper->funcs->gamma_set ||
729 !fb_helper->funcs->gamma_get))
734 if (fb->bits_per_pixel == 16) {
737 if (fb->depth == 16 && regno > 63)
739 if (fb->depth == 15 && regno > 31)
742 if (fb->depth == 16) {
746 for (i = 0; i < 8; i++)
747 fb_helper->funcs->gamma_set(crtc, red,
748 green, blue, pindex + i);
751 fb_helper->funcs->gamma_get(crtc, &r,
755 for (i = 0; i < 4; i++)
756 fb_helper->funcs->gamma_set(crtc, r,
763 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
768 * drm_fb_helper_setcmap - implementation for ->fb_setcmap
770 * @info: fbdev registered by the helper
772 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
774 struct drm_fb_helper *fb_helper = info->par;
775 struct drm_device *dev = fb_helper->dev;
776 struct drm_crtc_helper_funcs *crtc_funcs;
777 u16 *red, *green, *blue, *transp;
778 struct drm_crtc *crtc;
782 drm_modeset_lock_all(dev);
783 if (!drm_fb_helper_is_bound(fb_helper)) {
784 drm_modeset_unlock_all(dev);
788 for (i = 0; i < fb_helper->crtc_count; i++) {
789 crtc = fb_helper->crtc_info[i].mode_set.crtc;
790 crtc_funcs = crtc->helper_private;
795 transp = cmap->transp;
798 for (j = 0; j < cmap->len; j++) {
799 u16 hred, hgreen, hblue, htransp = 0xffff;
808 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
812 if (crtc_funcs->load_lut)
813 crtc_funcs->load_lut(crtc);
816 drm_modeset_unlock_all(dev);
819 EXPORT_SYMBOL(drm_fb_helper_setcmap);
822 * drm_fb_helper_check_var - implementation for ->fb_check_var
823 * @var: screeninfo to check
824 * @info: fbdev registered by the helper
826 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
827 struct fb_info *info)
829 struct drm_fb_helper *fb_helper = info->par;
830 struct drm_framebuffer *fb = fb_helper->fb;
833 if (var->pixclock != 0 || in_dbg_master())
836 /* Need to resize the fb object !!! */
837 if (var->bits_per_pixel > fb->bits_per_pixel ||
838 var->xres > fb->width || var->yres > fb->height ||
839 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
840 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
841 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
842 var->xres, var->yres, var->bits_per_pixel,
843 var->xres_virtual, var->yres_virtual,
844 fb->width, fb->height, fb->bits_per_pixel);
848 switch (var->bits_per_pixel) {
850 depth = (var->green.length == 6) ? 16 : 15;
853 depth = (var->transp.length > 0) ? 32 : 24;
856 depth = var->bits_per_pixel;
863 var->green.offset = 0;
864 var->blue.offset = 0;
866 var->green.length = 8;
867 var->blue.length = 8;
868 var->transp.length = 0;
869 var->transp.offset = 0;
872 var->red.offset = 10;
873 var->green.offset = 5;
874 var->blue.offset = 0;
876 var->green.length = 5;
877 var->blue.length = 5;
878 var->transp.length = 1;
879 var->transp.offset = 15;
882 var->red.offset = 11;
883 var->green.offset = 5;
884 var->blue.offset = 0;
886 var->green.length = 6;
887 var->blue.length = 5;
888 var->transp.length = 0;
889 var->transp.offset = 0;
892 var->red.offset = 16;
893 var->green.offset = 8;
894 var->blue.offset = 0;
896 var->green.length = 8;
897 var->blue.length = 8;
898 var->transp.length = 0;
899 var->transp.offset = 0;
902 var->red.offset = 16;
903 var->green.offset = 8;
904 var->blue.offset = 0;
906 var->green.length = 8;
907 var->blue.length = 8;
908 var->transp.length = 8;
909 var->transp.offset = 24;
916 EXPORT_SYMBOL(drm_fb_helper_check_var);
919 * drm_fb_helper_set_par - implementation for ->fb_set_par
920 * @info: fbdev registered by the helper
922 * This will let fbcon do the mode init and is called at initialization time by
923 * the fbdev core when registering the driver, and later on through the hotplug
926 int drm_fb_helper_set_par(struct fb_info *info)
928 struct drm_fb_helper *fb_helper = info->par;
929 struct fb_var_screeninfo *var = &info->var;
931 if (var->pixclock != 0) {
932 DRM_ERROR("PIXEL CLOCK SET\n");
936 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
938 if (fb_helper->delayed_hotplug) {
939 fb_helper->delayed_hotplug = false;
940 drm_fb_helper_hotplug_event(fb_helper);
944 EXPORT_SYMBOL(drm_fb_helper_set_par);
947 * drm_fb_helper_pan_display - implementation for ->fb_pan_display
948 * @var: updated screen information
949 * @info: fbdev registered by the helper
951 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
952 struct fb_info *info)
954 struct drm_fb_helper *fb_helper = info->par;
955 struct drm_device *dev = fb_helper->dev;
956 struct drm_mode_set *modeset;
960 drm_modeset_lock_all(dev);
961 if (!drm_fb_helper_is_bound(fb_helper)) {
962 drm_modeset_unlock_all(dev);
966 for (i = 0; i < fb_helper->crtc_count; i++) {
967 modeset = &fb_helper->crtc_info[i].mode_set;
969 modeset->x = var->xoffset;
970 modeset->y = var->yoffset;
972 if (modeset->num_connectors) {
973 ret = drm_mode_set_config_internal(modeset);
975 info->var.xoffset = var->xoffset;
976 info->var.yoffset = var->yoffset;
980 drm_modeset_unlock_all(dev);
983 EXPORT_SYMBOL(drm_fb_helper_pan_display);
986 * Allocates the backing storage and sets up the fbdev info structure through
987 * the ->fb_probe callback and then registers the fbdev and sets up the panic
990 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
996 struct fb_info *info;
997 struct drm_fb_helper_surface_size sizes;
1000 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1001 sizes.surface_depth = 24;
1002 sizes.surface_bpp = 32;
1003 sizes.fb_width = (unsigned)-1;
1004 sizes.fb_height = (unsigned)-1;
1006 /* if driver picks 8 or 16 by default use that
1007 for both depth/bpp */
1008 if (preferred_bpp != sizes.surface_bpp)
1009 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
1011 /* first up get a count of crtcs now in use and new min/maxes width/heights */
1012 for (i = 0; i < fb_helper->connector_count; i++) {
1013 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
1014 struct drm_cmdline_mode *cmdline_mode;
1016 cmdline_mode = &fb_helper_conn->cmdline_mode;
1018 if (cmdline_mode->bpp_specified) {
1019 switch (cmdline_mode->bpp) {
1021 sizes.surface_depth = sizes.surface_bpp = 8;
1024 sizes.surface_depth = 15;
1025 sizes.surface_bpp = 16;
1028 sizes.surface_depth = sizes.surface_bpp = 16;
1031 sizes.surface_depth = sizes.surface_bpp = 24;
1034 sizes.surface_depth = 24;
1035 sizes.surface_bpp = 32;
1043 for (i = 0; i < fb_helper->crtc_count; i++) {
1044 struct drm_display_mode *desired_mode;
1045 desired_mode = fb_helper->crtc_info[i].desired_mode;
1048 if (gamma_size == 0)
1049 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1050 if (desired_mode->hdisplay < sizes.fb_width)
1051 sizes.fb_width = desired_mode->hdisplay;
1052 if (desired_mode->vdisplay < sizes.fb_height)
1053 sizes.fb_height = desired_mode->vdisplay;
1054 if (desired_mode->hdisplay > sizes.surface_width)
1055 sizes.surface_width = desired_mode->hdisplay;
1056 if (desired_mode->vdisplay > sizes.surface_height)
1057 sizes.surface_height = desired_mode->vdisplay;
1062 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
1063 /* hmm everyone went away - assume VGA cable just fell out
1064 and will come back later. */
1065 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
1066 sizes.fb_width = sizes.surface_width = 1024;
1067 sizes.fb_height = sizes.surface_height = 768;
1070 /* push down into drivers */
1071 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1075 info = fb_helper->fbdev;
1078 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1079 * events, but at init time drm_setup_crtcs needs to be called before
1080 * the fb is allocated (since we need to figure out the desired size of
1081 * the fb before we can allocate it ...). Hence we need to fix things up
1084 for (i = 0; i < fb_helper->crtc_count; i++)
1085 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1086 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1089 info->var.pixclock = 0;
1090 if (register_framebuffer(info) < 0)
1093 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
1094 info->node, info->fix.id);
1096 /* Switch back to kernel console on panic */
1097 /* multi card linked list maybe */
1098 if (list_empty(&kernel_fb_helper_list)) {
1099 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
1100 atomic_notifier_chain_register(&panic_notifier_list,
1102 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1105 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1111 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1112 * @info: fbdev registered by the helper
1113 * @pitch: desired pitch
1114 * @depth: desired depth
1116 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1117 * fbdev emulations. Drivers which support acceleration methods which impose
1118 * additional constraints need to set up their own limits.
1120 * Drivers should call this (or their equivalent setup code) from their
1121 * ->fb_probe callback.
1123 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1126 info->fix.type = FB_TYPE_PACKED_PIXELS;
1127 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1128 FB_VISUAL_TRUECOLOR;
1129 info->fix.mmio_start = 0;
1130 info->fix.mmio_len = 0;
1131 info->fix.type_aux = 0;
1132 info->fix.xpanstep = 1; /* doing it in hw */
1133 info->fix.ypanstep = 1; /* doing it in hw */
1134 info->fix.ywrapstep = 0;
1135 info->fix.accel = FB_ACCEL_NONE;
1137 info->fix.line_length = pitch;
1140 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1143 * drm_fb_helper_fill_var - initalizes variable fbdev information
1144 * @info: fbdev instance to set up
1145 * @fb_helper: fb helper instance to use as template
1146 * @fb_width: desired fb width
1147 * @fb_height: desired fb height
1149 * Sets up the variable fbdev metainformation from the given fb helper instance
1150 * and the drm framebuffer allocated in fb_helper->fb.
1152 * Drivers should call this (or their equivalent setup code) from their
1153 * ->fb_probe callback after having allocated the fbdev backing
1154 * storage framebuffer.
1156 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1157 uint32_t fb_width, uint32_t fb_height)
1159 struct drm_framebuffer *fb = fb_helper->fb;
1160 info->pseudo_palette = fb_helper->pseudo_palette;
1161 info->var.xres_virtual = fb->width;
1162 info->var.yres_virtual = fb->height;
1163 info->var.bits_per_pixel = fb->bits_per_pixel;
1164 info->var.accel_flags = FB_ACCELF_TEXT;
1165 info->var.xoffset = 0;
1166 info->var.yoffset = 0;
1167 info->var.activate = FB_ACTIVATE_NOW;
1168 info->var.height = -1;
1169 info->var.width = -1;
1171 switch (fb->depth) {
1173 info->var.red.offset = 0;
1174 info->var.green.offset = 0;
1175 info->var.blue.offset = 0;
1176 info->var.red.length = 8; /* 8bit DAC */
1177 info->var.green.length = 8;
1178 info->var.blue.length = 8;
1179 info->var.transp.offset = 0;
1180 info->var.transp.length = 0;
1183 info->var.red.offset = 10;
1184 info->var.green.offset = 5;
1185 info->var.blue.offset = 0;
1186 info->var.red.length = 5;
1187 info->var.green.length = 5;
1188 info->var.blue.length = 5;
1189 info->var.transp.offset = 15;
1190 info->var.transp.length = 1;
1193 info->var.red.offset = 11;
1194 info->var.green.offset = 5;
1195 info->var.blue.offset = 0;
1196 info->var.red.length = 5;
1197 info->var.green.length = 6;
1198 info->var.blue.length = 5;
1199 info->var.transp.offset = 0;
1202 info->var.red.offset = 16;
1203 info->var.green.offset = 8;
1204 info->var.blue.offset = 0;
1205 info->var.red.length = 8;
1206 info->var.green.length = 8;
1207 info->var.blue.length = 8;
1208 info->var.transp.offset = 0;
1209 info->var.transp.length = 0;
1212 info->var.red.offset = 16;
1213 info->var.green.offset = 8;
1214 info->var.blue.offset = 0;
1215 info->var.red.length = 8;
1216 info->var.green.length = 8;
1217 info->var.blue.length = 8;
1218 info->var.transp.offset = 24;
1219 info->var.transp.length = 8;
1225 info->var.xres = fb_width;
1226 info->var.yres = fb_height;
1228 EXPORT_SYMBOL(drm_fb_helper_fill_var);
1230 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1234 struct drm_connector *connector;
1238 for (i = 0; i < fb_helper->connector_count; i++) {
1239 connector = fb_helper->connector_info[i]->connector;
1240 count += connector->funcs->fill_modes(connector, maxX, maxY);
1246 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1248 struct drm_display_mode *mode;
1250 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1251 if (mode->hdisplay > width ||
1252 mode->vdisplay > height)
1254 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1259 EXPORT_SYMBOL(drm_has_preferred_mode);
1261 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1263 struct drm_cmdline_mode *cmdline_mode;
1264 cmdline_mode = &fb_connector->cmdline_mode;
1265 return cmdline_mode->specified;
1268 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1269 int width, int height)
1271 struct drm_cmdline_mode *cmdline_mode;
1272 struct drm_display_mode *mode = NULL;
1273 bool prefer_non_interlace;
1275 cmdline_mode = &fb_helper_conn->cmdline_mode;
1276 if (cmdline_mode->specified == false)
1279 /* attempt to find a matching mode in the list of modes
1280 * we have gotten so far, if not add a CVT mode that conforms
1282 if (cmdline_mode->rb || cmdline_mode->margins)
1285 prefer_non_interlace = !cmdline_mode->interlace;
1287 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1288 /* check width/height */
1289 if (mode->hdisplay != cmdline_mode->xres ||
1290 mode->vdisplay != cmdline_mode->yres)
1293 if (cmdline_mode->refresh_specified) {
1294 if (mode->vrefresh != cmdline_mode->refresh)
1298 if (cmdline_mode->interlace) {
1299 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1301 } else if (prefer_non_interlace) {
1302 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1308 if (prefer_non_interlace) {
1309 prefer_non_interlace = false;
1314 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1316 list_add(&mode->head, &fb_helper_conn->connector->modes);
1319 EXPORT_SYMBOL(drm_pick_cmdline_mode);
1321 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1326 enable = connector->status == connector_status_connected;
1328 enable = connector->status != connector_status_disconnected;
1333 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1336 bool any_enabled = false;
1337 struct drm_connector *connector;
1340 for (i = 0; i < fb_helper->connector_count; i++) {
1341 connector = fb_helper->connector_info[i]->connector;
1342 enabled[i] = drm_connector_enabled(connector, true);
1343 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1344 enabled[i] ? "yes" : "no");
1345 any_enabled |= enabled[i];
1351 for (i = 0; i < fb_helper->connector_count; i++) {
1352 connector = fb_helper->connector_info[i]->connector;
1353 enabled[i] = drm_connector_enabled(connector, false);
1357 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1358 struct drm_display_mode **modes,
1359 bool *enabled, int width, int height)
1362 bool can_clone = false;
1363 struct drm_fb_helper_connector *fb_helper_conn;
1364 struct drm_display_mode *dmt_mode, *mode;
1366 /* only contemplate cloning in the single crtc case */
1367 if (fb_helper->crtc_count > 1)
1371 for (i = 0; i < fb_helper->connector_count; i++) {
1376 /* only contemplate cloning if more than one connector is enabled */
1380 /* check the command line or if nothing common pick 1024x768 */
1382 for (i = 0; i < fb_helper->connector_count; i++) {
1385 fb_helper_conn = fb_helper->connector_info[i];
1386 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1391 for (j = 0; j < i; j++) {
1394 if (!drm_mode_equal(modes[j], modes[i]))
1400 DRM_DEBUG_KMS("can clone using command line\n");
1404 /* try and find a 1024x768 mode on each connector */
1406 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1408 for (i = 0; i < fb_helper->connector_count; i++) {
1413 fb_helper_conn = fb_helper->connector_info[i];
1414 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1415 if (drm_mode_equal(mode, dmt_mode))
1423 DRM_DEBUG_KMS("can clone using 1024x768\n");
1426 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1430 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1431 struct drm_display_mode **modes,
1432 bool *enabled, int width, int height)
1434 struct drm_fb_helper_connector *fb_helper_conn;
1437 for (i = 0; i < fb_helper->connector_count; i++) {
1438 fb_helper_conn = fb_helper->connector_info[i];
1440 if (enabled[i] == false)
1443 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1444 fb_helper_conn->connector->base.id);
1446 /* got for command line mode first */
1447 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1449 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1450 fb_helper_conn->connector->base.id);
1451 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1453 /* No preferred modes, pick one off the list */
1454 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1455 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1458 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1464 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1465 struct drm_fb_helper_crtc **best_crtcs,
1466 struct drm_display_mode **modes,
1467 int n, int width, int height)
1470 struct drm_device *dev = fb_helper->dev;
1471 struct drm_connector *connector;
1472 struct drm_connector_helper_funcs *connector_funcs;
1473 struct drm_encoder *encoder;
1474 int my_score, best_score, score;
1475 struct drm_fb_helper_crtc **crtcs, *crtc;
1476 struct drm_fb_helper_connector *fb_helper_conn;
1478 if (n == fb_helper->connector_count)
1481 fb_helper_conn = fb_helper->connector_info[n];
1482 connector = fb_helper_conn->connector;
1484 best_crtcs[n] = NULL;
1485 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1486 if (modes[n] == NULL)
1489 crtcs = kzalloc(dev->mode_config.num_connector *
1490 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1495 if (connector->status == connector_status_connected)
1497 if (drm_has_cmdline_mode(fb_helper_conn))
1499 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1502 connector_funcs = connector->helper_private;
1503 encoder = connector_funcs->best_encoder(connector);
1507 /* select a crtc for this connector and then attempt to configure
1508 remaining connectors */
1509 for (c = 0; c < fb_helper->crtc_count; c++) {
1510 crtc = &fb_helper->crtc_info[c];
1512 if ((encoder->possible_crtcs & (1 << c)) == 0)
1515 for (o = 0; o < n; o++)
1516 if (best_crtcs[o] == crtc)
1520 /* ignore cloning unless only a single crtc */
1521 if (fb_helper->crtc_count > 1)
1524 if (!drm_mode_equal(modes[o], modes[n]))
1529 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1530 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1532 if (score > best_score) {
1534 memcpy(best_crtcs, crtcs,
1535 dev->mode_config.num_connector *
1536 sizeof(struct drm_fb_helper_crtc *));
1544 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1546 struct drm_device *dev = fb_helper->dev;
1547 struct drm_fb_helper_crtc **crtcs;
1548 struct drm_display_mode **modes;
1549 struct drm_mode_set *modeset;
1554 DRM_DEBUG_KMS("\n");
1556 width = dev->mode_config.max_width;
1557 height = dev->mode_config.max_height;
1559 crtcs = kcalloc(dev->mode_config.num_connector,
1560 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1561 modes = kcalloc(dev->mode_config.num_connector,
1562 sizeof(struct drm_display_mode *), GFP_KERNEL);
1563 enabled = kcalloc(dev->mode_config.num_connector,
1564 sizeof(bool), GFP_KERNEL);
1565 if (!crtcs || !modes || !enabled) {
1566 DRM_ERROR("Memory allocation failed\n");
1571 drm_enable_connectors(fb_helper, enabled);
1573 if (!(fb_helper->funcs->initial_config &&
1574 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
1575 enabled, width, height))) {
1576 memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
1577 memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
1579 if (!drm_target_cloned(fb_helper,
1580 modes, enabled, width, height) &&
1581 !drm_target_preferred(fb_helper,
1582 modes, enabled, width, height))
1583 DRM_ERROR("Unable to find initial modes\n");
1585 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1588 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1591 /* need to set the modesets up here for use later */
1592 /* fill out the connector<->crtc mappings into the modesets */
1593 for (i = 0; i < fb_helper->crtc_count; i++) {
1594 modeset = &fb_helper->crtc_info[i].mode_set;
1595 modeset->num_connectors = 0;
1599 for (i = 0; i < fb_helper->connector_count; i++) {
1600 struct drm_display_mode *mode = modes[i];
1601 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1602 modeset = &fb_crtc->mode_set;
1604 if (mode && fb_crtc) {
1605 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1606 mode->name, fb_crtc->mode_set.crtc->base.id);
1607 fb_crtc->desired_mode = mode;
1609 drm_mode_destroy(dev, modeset->mode);
1610 modeset->mode = drm_mode_duplicate(dev,
1611 fb_crtc->desired_mode);
1612 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1613 modeset->fb = fb_helper->fb;
1617 /* Clear out any old modes if there are no more connected outputs. */
1618 for (i = 0; i < fb_helper->crtc_count; i++) {
1619 modeset = &fb_helper->crtc_info[i].mode_set;
1620 if (modeset->num_connectors == 0) {
1621 BUG_ON(modeset->fb);
1622 BUG_ON(modeset->num_connectors);
1624 drm_mode_destroy(dev, modeset->mode);
1625 modeset->mode = NULL;
1635 * drm_fb_helper_initial_config - setup a sane initial connector configuration
1636 * @fb_helper: fb_helper device struct
1637 * @bpp_sel: bpp value to use for the framebuffer configuration
1639 * Scans the CRTCs and connectors and tries to put together an initial setup.
1640 * At the moment, this is a cloned configuration across all heads with
1641 * a new framebuffer object as the backing store.
1643 * Note that this also registers the fbdev and so allows userspace to call into
1644 * the driver through the fbdev interfaces.
1646 * This function will call down into the ->fb_probe callback to let
1647 * the driver allocate and initialize the fbdev info structure and the drm
1648 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1649 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1650 * values for the fbdev info structure.
1653 * Zero if everything went ok, nonzero otherwise.
1655 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1657 struct drm_device *dev = fb_helper->dev;
1660 drm_fb_helper_parse_command_line(fb_helper);
1662 mutex_lock(&dev->mode_config.mutex);
1663 count = drm_fb_helper_probe_connector_modes(fb_helper,
1664 dev->mode_config.max_width,
1665 dev->mode_config.max_height);
1666 mutex_unlock(&dev->mode_config.mutex);
1668 * we shouldn't end up with no modes here.
1671 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
1673 drm_setup_crtcs(fb_helper);
1675 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1677 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1680 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1681 * probing all the outputs attached to the fb
1682 * @fb_helper: the drm_fb_helper
1684 * Scan the connectors attached to the fb_helper and try to put together a
1685 * setup after *notification of a change in output configuration.
1687 * Called at runtime, takes the mode config locks to be able to check/change the
1688 * modeset configuration. Must be run from process context (which usually means
1689 * either the output polling work or a work item launched from the driver's
1690 * hotplug interrupt).
1692 * Note that drivers may call this even before calling
1693 * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
1694 * for a race-free fbcon setup and will make sure that the fbdev emulation will
1695 * not miss any hotplug events.
1698 * 0 on success and a non-zero error code otherwise.
1700 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1702 struct drm_device *dev = fb_helper->dev;
1703 u32 max_width, max_height;
1705 mutex_lock(&fb_helper->dev->mode_config.mutex);
1706 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
1707 fb_helper->delayed_hotplug = true;
1708 mutex_unlock(&fb_helper->dev->mode_config.mutex);
1711 DRM_DEBUG_KMS("\n");
1713 max_width = fb_helper->fb->width;
1714 max_height = fb_helper->fb->height;
1716 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
1717 mutex_unlock(&fb_helper->dev->mode_config.mutex);
1719 drm_modeset_lock_all(dev);
1720 drm_setup_crtcs(fb_helper);
1721 drm_modeset_unlock_all(dev);
1722 drm_fb_helper_set_par(fb_helper->fbdev);
1726 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
1728 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
1729 * but the module doesn't depend on any fb console symbols. At least
1730 * attempt to load fbcon to avoid leaving the system without a usable console.
1732 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
1733 static int __init drm_fb_helper_modinit(void)
1735 const char *name = "fbcon";
1736 struct module *fbcon;
1738 mutex_lock(&module_mutex);
1739 fbcon = find_module(name);
1740 mutex_unlock(&module_mutex);
1743 request_module_nowait(name);
1747 module_init(drm_fb_helper_modinit);