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>
31 #include <linux/kernel.h>
33 #include <linux/module.h>
35 #include <drm/drm_crtc.h>
36 #include <drm/drm_fb_helper.h>
37 #include <drm/drm_crtc_helper.h>
39 static LINUX_LIST_HEAD(kernel_fb_helper_list);
44 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
45 * mode setting driver. They can be used mostly independently from the crtc
46 * helper functions used by many drivers to implement the kernel mode setting
49 * Initialization is done as a four-step process with drm_fb_helper_prepare(),
50 * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
51 * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
52 * default behaviour can override the third step with their own code.
53 * Teardown is done with drm_fb_helper_fini().
55 * At runtime drivers should restore the fbdev console by calling
56 * drm_fb_helper_restore_fbdev_mode() from their ->lastclose callback. They
57 * should also notify the fb helper code from updates to the output
58 * configuration by calling drm_fb_helper_hotplug_event(). For easier
59 * integration with the output polling code in drm_crtc_helper.c the modeset
60 * code provides a ->output_poll_changed callback.
62 * All other functions exported by the fb helper library can be used to
63 * implement the fbdev driver interface by the driver.
65 * It is possible, though perhaps somewhat tricky, to implement race-free
66 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
67 * helper must be called first to initialize the minimum required to make
68 * hotplug detection work. Drivers also need to make sure to properly set up
69 * the dev->mode_config.funcs member. After calling drm_kms_helper_poll_init()
70 * it is safe to enable interrupts and start processing hotplug events. At the
71 * same time, drivers should initialize all modeset objects such as CRTCs,
72 * encoders and connectors. To finish up the fbdev helper initialization, the
73 * drm_fb_helper_init() function is called. To probe for all attached displays
74 * and set up an initial configuration using the detected hardware, drivers
75 * should call drm_fb_helper_single_add_all_connectors() followed by
76 * drm_fb_helper_initial_config().
80 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
82 * @fb_helper: fbdev initialized with drm_fb_helper_init
84 * This functions adds all the available connectors for use with the given
85 * fb_helper. This is a separate step to allow drivers to freely assign
86 * connectors to the fbdev, e.g. if some are reserved for special purposes or
87 * not adequate to be used for the fbcon.
89 * Since this is part of the initial setup before the fbdev is published, no
90 * locking is required.
92 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
94 struct drm_device *dev = fb_helper->dev;
95 struct drm_connector *connector;
98 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
99 struct drm_fb_helper_connector *fb_helper_connector;
101 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
102 if (!fb_helper_connector)
105 fb_helper_connector->connector = connector;
106 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
110 for (i = 0; i < fb_helper->connector_count; i++) {
111 kfree(fb_helper->connector_info[i]);
112 fb_helper->connector_info[i] = NULL;
114 fb_helper->connector_count = 0;
117 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
119 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector)
121 struct drm_fb_helper_connector **temp;
122 struct drm_fb_helper_connector *fb_helper_connector;
124 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
125 if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) {
126 temp = krealloc(fb_helper->connector_info, sizeof(struct drm_fb_helper_connector *) * (fb_helper->connector_count + 1), M_DRM, M_WAITOK);
130 fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1;
131 fb_helper->connector_info = temp;
135 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
136 if (!fb_helper_connector)
139 fb_helper_connector->connector = connector;
140 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
143 EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
145 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
146 struct drm_connector *connector)
148 struct drm_fb_helper_connector *fb_helper_connector;
151 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
153 for (i = 0; i < fb_helper->connector_count; i++) {
154 if (fb_helper->connector_info[i]->connector == connector)
158 if (i == fb_helper->connector_count)
160 fb_helper_connector = fb_helper->connector_info[i];
162 for (j = i + 1; j < fb_helper->connector_count; j++) {
163 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
165 fb_helper->connector_count--;
166 kfree(fb_helper_connector);
169 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
172 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
174 uint16_t *r_base, *g_base, *b_base;
177 if (helper->funcs->gamma_get == NULL)
180 r_base = crtc->gamma_store;
181 g_base = r_base + crtc->gamma_size;
182 b_base = g_base + crtc->gamma_size;
184 for (i = 0; i < crtc->gamma_size; i++)
185 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
188 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
190 uint16_t *r_base, *g_base, *b_base;
192 if (crtc->funcs->gamma_set == NULL)
195 r_base = crtc->gamma_store;
196 g_base = r_base + crtc->gamma_size;
197 b_base = g_base + crtc->gamma_size;
199 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
203 * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
204 * @info: fbdev registered by the helper
206 int drm_fb_helper_debug_enter(struct fb_info *info)
208 struct drm_fb_helper *helper = info->par;
209 struct drm_crtc_helper_funcs *funcs;
212 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
213 for (i = 0; i < helper->crtc_count; i++) {
214 struct drm_mode_set *mode_set =
215 &helper->crtc_info[i].mode_set;
217 if (!mode_set->crtc->enabled)
220 funcs = mode_set->crtc->helper_private;
221 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
222 funcs->mode_set_base_atomic(mode_set->crtc,
226 ENTER_ATOMIC_MODE_SET);
232 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
234 /* Find the real fb for a given fb helper CRTC */
235 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
237 struct drm_device *dev = crtc->dev;
240 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
241 if (crtc->base.id == c->base.id)
242 return c->primary->fb;
249 * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
250 * @info: fbdev registered by the helper
252 int drm_fb_helper_debug_leave(struct fb_info *info)
254 struct drm_fb_helper *helper = info->par;
255 struct drm_crtc *crtc;
256 struct drm_crtc_helper_funcs *funcs;
257 struct drm_framebuffer *fb;
260 for (i = 0; i < helper->crtc_count; i++) {
261 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
262 crtc = mode_set->crtc;
263 funcs = crtc->helper_private;
264 fb = drm_mode_config_fb(crtc);
270 DRM_ERROR("no fb to restore??\n");
274 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
275 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
276 crtc->y, LEAVE_ATOMIC_MODE_SET);
281 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
284 static bool restore_fbdev_mode(struct drm_fb_helper *fb_helper)
286 struct drm_device *dev = fb_helper->dev;
287 struct drm_plane *plane;
291 drm_warn_on_modeset_not_all_locked(dev);
293 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
294 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
295 drm_plane_force_disable(plane);
297 if (dev->mode_config.rotation_property) {
298 drm_mode_plane_set_obj_prop(plane,
299 dev->mode_config.rotation_property,
304 for (i = 0; i < fb_helper->crtc_count; i++) {
305 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
306 struct drm_crtc *crtc = mode_set->crtc;
309 if (crtc->funcs->cursor_set) {
310 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
315 ret = drm_mode_set_config_internal(mode_set);
322 * drm_fb_helper_restore_fbdev_mode - restore fbdev configuration
323 * @fb_helper: fbcon to restore
325 * This should be called from driver's drm ->lastclose callback
326 * when implementing an fbcon on top of kms using this helper. This ensures that
327 * the user isn't greeted with a black screen when e.g. X dies.
329 * Use this variant if you need to bypass locking (panic), or already
330 * hold all modeset locks. Otherwise use drm_fb_helper_restore_fbdev_mode_unlocked()
332 static bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
334 return restore_fbdev_mode(fb_helper);
338 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
339 * @fb_helper: fbcon to restore
341 * This should be called from driver's drm ->lastclose callback
342 * when implementing an fbcon on top of kms using this helper. This ensures that
343 * the user isn't greeted with a black screen when e.g. X dies.
345 bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
347 struct drm_device *dev = fb_helper->dev;
349 drm_modeset_lock_all(dev);
350 ret = restore_fbdev_mode(fb_helper);
351 drm_modeset_unlock_all(dev);
354 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
358 * restore fbcon display for all kms driver's using this helper, used for sysrq
359 * and panic handling.
361 static bool drm_fb_helper_force_kernel_mode(void)
363 bool ret, error = false;
364 struct drm_fb_helper *helper;
366 if (list_empty(&kernel_fb_helper_list))
369 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
370 struct drm_device *dev = helper->dev;
372 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
375 /* NOTE: we use lockless flag below to avoid grabbing other
376 * modeset locks. So just trylock the underlying mutex
379 if (!mutex_trylock(&dev->mode_config.mutex)) {
384 ret = drm_fb_helper_restore_fbdev_mode(helper);
388 mutex_unlock(&dev->mode_config.mutex);
393 static int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
397 * It's a waste of time and effort to switch back to text console
398 * if the kernel should reboot before panic messages can be seen.
400 if (panic_timeout < 0)
403 pr_err("panic occurred, switching back to text console\n");
404 return drm_fb_helper_force_kernel_mode();
407 static struct notifier_block paniced = {
408 .notifier_call = drm_fb_helper_panic,
412 static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
414 struct drm_device *dev = fb_helper->dev;
415 struct drm_crtc *crtc;
416 int bound = 0, crtcs_bound = 0;
419 /* Sometimes user space wants everything disabled, so don't steal the
420 * display if there's a master. */
421 if (dev->primary->master)
425 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
426 if (crtc->primary->fb)
428 if (crtc->primary->fb == fb_helper->fb)
432 if (bound < crtcs_bound)
439 #ifdef CONFIG_MAGIC_SYSRQ
440 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
443 ret = drm_fb_helper_force_kernel_mode();
445 DRM_ERROR("Failed to restore crtc configuration\n");
447 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
449 static void drm_fb_helper_sysrq(int dummy1)
451 schedule_work(&drm_fb_helper_restore_work);
454 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
455 .handler = drm_fb_helper_sysrq,
456 .help_msg = "force-fb(V)",
457 .action_msg = "Restore framebuffer console",
460 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
463 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
465 struct drm_fb_helper *fb_helper = info->par;
466 struct drm_device *dev = fb_helper->dev;
467 struct drm_crtc *crtc;
468 struct drm_connector *connector;
472 * fbdev->blank can be called from irq context in case of a panic.
473 * Since we already have our own special panic handler which will
474 * restore the fbdev console mode completely, just bail out early.
476 if (oops_in_progress)
480 * For each CRTC in this fb, turn the connectors on/off.
482 drm_modeset_lock_all(dev);
483 if (!drm_fb_helper_is_bound(fb_helper)) {
484 drm_modeset_unlock_all(dev);
488 for (i = 0; i < fb_helper->crtc_count; i++) {
489 crtc = fb_helper->crtc_info[i].mode_set.crtc;
494 /* Walk the connectors & encoders on this fb turning them on/off */
495 for (j = 0; j < fb_helper->connector_count; j++) {
496 connector = fb_helper->connector_info[j]->connector;
497 connector->funcs->dpms(connector, dpms_mode);
498 drm_object_property_set_value(&connector->base,
499 dev->mode_config.dpms_property, dpms_mode);
502 drm_modeset_unlock_all(dev);
506 * drm_fb_helper_blank - implementation for ->fb_blank
507 * @blank: desired blanking state
508 * @info: fbdev registered by the helper
510 int drm_fb_helper_blank(int blank, struct fb_info *info)
513 /* Display: On; HSync: On, VSync: On */
514 case FB_BLANK_UNBLANK:
515 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
517 /* Display: Off; HSync: On, VSync: On */
518 case FB_BLANK_NORMAL:
519 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
521 /* Display: Off; HSync: Off, VSync: On */
522 case FB_BLANK_HSYNC_SUSPEND:
523 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
525 /* Display: Off; HSync: On, VSync: Off */
526 case FB_BLANK_VSYNC_SUSPEND:
527 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
529 /* Display: Off; HSync: Off, VSync: Off */
530 case FB_BLANK_POWERDOWN:
531 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
536 EXPORT_SYMBOL(drm_fb_helper_blank);
539 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
543 for (i = 0; i < helper->connector_count; i++)
544 kfree(helper->connector_info[i]);
545 kfree(helper->connector_info);
546 for (i = 0; i < helper->crtc_count; i++) {
547 kfree(helper->crtc_info[i].mode_set.connectors);
548 if (helper->crtc_info[i].mode_set.mode)
549 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
551 kfree(helper->crtc_info);
555 * drm_fb_helper_prepare - setup a drm_fb_helper structure
557 * @helper: driver-allocated fbdev helper structure to set up
558 * @funcs: pointer to structure of functions associate with this helper
560 * Sets up the bare minimum to make the framebuffer helper usable. This is
561 * useful to implement race-free initialization of the polling helpers.
563 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
564 const struct drm_fb_helper_funcs *funcs)
566 INIT_LIST_HEAD(&helper->kernel_fb_list);
567 helper->funcs = funcs;
570 EXPORT_SYMBOL(drm_fb_helper_prepare);
573 * drm_fb_helper_init - initialize a drm_fb_helper structure
575 * @fb_helper: driver-allocated fbdev helper structure to initialize
576 * @crtc_count: maximum number of crtcs to support in this fbdev emulation
577 * @max_conn_count: max connector count
579 * This allocates the structures for the fbdev helper with the given limits.
580 * Note that this won't yet touch the hardware (through the driver interfaces)
581 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
582 * to allow driver writes more control over the exact init sequence.
584 * Drivers must call drm_fb_helper_prepare() before calling this function.
587 * Zero if everything went ok, nonzero otherwise.
589 int drm_fb_helper_init(struct drm_device *dev,
590 struct drm_fb_helper *fb_helper,
591 int crtc_count, int max_conn_count)
593 struct drm_crtc *crtc;
599 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
600 if (!fb_helper->crtc_info)
603 fb_helper->crtc_count = crtc_count;
604 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
605 if (!fb_helper->connector_info) {
606 kfree(fb_helper->crtc_info);
609 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
610 fb_helper->connector_count = 0;
612 for (i = 0; i < crtc_count; i++) {
613 fb_helper->crtc_info[i].mode_set.connectors =
614 kcalloc(max_conn_count,
615 sizeof(struct drm_connector *),
618 if (!fb_helper->crtc_info[i].mode_set.connectors)
620 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
624 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
625 fb_helper->crtc_info[i].mode_set.crtc = crtc;
631 drm_fb_helper_crtc_free(fb_helper);
634 EXPORT_SYMBOL(drm_fb_helper_init);
636 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
638 if (!list_empty(&fb_helper->kernel_fb_list)) {
639 list_del(&fb_helper->kernel_fb_list);
640 if (list_empty(&kernel_fb_helper_list)) {
642 pr_info("drm: unregistered panic notifier\n");
643 atomic_notifier_chain_unregister(&panic_notifier_list,
645 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
650 drm_fb_helper_crtc_free(fb_helper);
653 EXPORT_SYMBOL(drm_fb_helper_fini);
656 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
657 u16 blue, u16 regno, struct fb_info *info)
659 struct drm_fb_helper *fb_helper = info->par;
660 struct drm_framebuffer *fb = fb_helper->fb;
663 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
666 /* place color in psuedopalette */
669 palette = (u32 *)info->pseudo_palette;
670 red >>= (16 - info->var.red.length);
671 green >>= (16 - info->var.green.length);
672 blue >>= (16 - info->var.blue.length);
673 value = (red << info->var.red.offset) |
674 (green << info->var.green.offset) |
675 (blue << info->var.blue.offset);
676 if (info->var.transp.length > 0) {
677 u32 mask = (1 << info->var.transp.length) - 1;
678 mask <<= info->var.transp.offset;
681 palette[regno] = value;
686 * The driver really shouldn't advertise pseudo/directcolor
687 * visuals if it can't deal with the palette.
689 if (WARN_ON(!fb_helper->funcs->gamma_set ||
690 !fb_helper->funcs->gamma_get))
695 if (fb->bits_per_pixel == 16) {
698 if (fb->depth == 16 && regno > 63)
700 if (fb->depth == 15 && regno > 31)
703 if (fb->depth == 16) {
707 for (i = 0; i < 8; i++)
708 fb_helper->funcs->gamma_set(crtc, red,
709 green, blue, pindex + i);
712 fb_helper->funcs->gamma_get(crtc, &r,
716 for (i = 0; i < 4; i++)
717 fb_helper->funcs->gamma_set(crtc, r,
724 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
729 * drm_fb_helper_setcmap - implementation for ->fb_setcmap
731 * @info: fbdev registered by the helper
733 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
735 struct drm_fb_helper *fb_helper = info->par;
736 struct drm_device *dev = fb_helper->dev;
737 struct drm_crtc_helper_funcs *crtc_funcs;
738 u16 *red, *green, *blue, *transp;
739 struct drm_crtc *crtc;
743 drm_modeset_lock_all(dev);
744 if (!drm_fb_helper_is_bound(fb_helper)) {
745 drm_modeset_unlock_all(dev);
749 for (i = 0; i < fb_helper->crtc_count; i++) {
750 crtc = fb_helper->crtc_info[i].mode_set.crtc;
751 crtc_funcs = crtc->helper_private;
756 transp = cmap->transp;
759 for (j = 0; j < cmap->len; j++) {
760 u16 hred, hgreen, hblue, htransp = 0xffff;
769 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
773 if (crtc_funcs->load_lut)
774 crtc_funcs->load_lut(crtc);
777 drm_modeset_unlock_all(dev);
780 EXPORT_SYMBOL(drm_fb_helper_setcmap);
783 * drm_fb_helper_check_var - implementation for ->fb_check_var
784 * @var: screeninfo to check
785 * @info: fbdev registered by the helper
787 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
788 struct fb_info *info)
790 struct drm_fb_helper *fb_helper = info->par;
791 struct drm_framebuffer *fb = fb_helper->fb;
794 if (var->pixclock != 0 || in_dbg_master())
797 /* Need to resize the fb object !!! */
798 if (var->bits_per_pixel > fb->bits_per_pixel ||
799 var->xres > fb->width || var->yres > fb->height ||
800 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
801 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
802 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
803 var->xres, var->yres, var->bits_per_pixel,
804 var->xres_virtual, var->yres_virtual,
805 fb->width, fb->height, fb->bits_per_pixel);
809 switch (var->bits_per_pixel) {
811 depth = (var->green.length == 6) ? 16 : 15;
814 depth = (var->transp.length > 0) ? 32 : 24;
817 depth = var->bits_per_pixel;
824 var->green.offset = 0;
825 var->blue.offset = 0;
827 var->green.length = 8;
828 var->blue.length = 8;
829 var->transp.length = 0;
830 var->transp.offset = 0;
833 var->red.offset = 10;
834 var->green.offset = 5;
835 var->blue.offset = 0;
837 var->green.length = 5;
838 var->blue.length = 5;
839 var->transp.length = 1;
840 var->transp.offset = 15;
843 var->red.offset = 11;
844 var->green.offset = 5;
845 var->blue.offset = 0;
847 var->green.length = 6;
848 var->blue.length = 5;
849 var->transp.length = 0;
850 var->transp.offset = 0;
853 var->red.offset = 16;
854 var->green.offset = 8;
855 var->blue.offset = 0;
857 var->green.length = 8;
858 var->blue.length = 8;
859 var->transp.length = 0;
860 var->transp.offset = 0;
863 var->red.offset = 16;
864 var->green.offset = 8;
865 var->blue.offset = 0;
867 var->green.length = 8;
868 var->blue.length = 8;
869 var->transp.length = 8;
870 var->transp.offset = 24;
877 EXPORT_SYMBOL(drm_fb_helper_check_var);
880 * drm_fb_helper_set_par - implementation for ->fb_set_par
881 * @info: fbdev registered by the helper
883 * This will let fbcon do the mode init and is called at initialization time by
884 * the fbdev core when registering the driver, and later on through the hotplug
887 int drm_fb_helper_set_par(struct fb_info *info)
889 struct drm_fb_helper *fb_helper = info->par;
890 struct fb_var_screeninfo *var = &info->var;
892 if (var->pixclock != 0) {
893 DRM_ERROR("PIXEL CLOCK SET\n");
897 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
899 if (fb_helper->delayed_hotplug) {
900 fb_helper->delayed_hotplug = false;
901 drm_fb_helper_hotplug_event(fb_helper);
905 EXPORT_SYMBOL(drm_fb_helper_set_par);
908 * drm_fb_helper_pan_display - implementation for ->fb_pan_display
909 * @var: updated screen information
910 * @info: fbdev registered by the helper
912 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
913 struct fb_info *info)
915 struct drm_fb_helper *fb_helper = info->par;
916 struct drm_device *dev = fb_helper->dev;
917 struct drm_mode_set *modeset;
921 drm_modeset_lock_all(dev);
922 if (!drm_fb_helper_is_bound(fb_helper)) {
923 drm_modeset_unlock_all(dev);
927 for (i = 0; i < fb_helper->crtc_count; i++) {
928 modeset = &fb_helper->crtc_info[i].mode_set;
930 modeset->x = var->xoffset;
931 modeset->y = var->yoffset;
933 if (modeset->num_connectors) {
934 ret = drm_mode_set_config_internal(modeset);
936 info->var.xoffset = var->xoffset;
937 info->var.yoffset = var->yoffset;
941 drm_modeset_unlock_all(dev);
944 EXPORT_SYMBOL(drm_fb_helper_pan_display);
949 sc_restore_fbdev_mode(void *cookie)
951 struct drm_fb_helper *fb_helper = cookie;
952 struct drm_device *dev = fb_helper->dev;
957 drm_modeset_lock_all(dev);
958 drm_fb_helper_restore_fbdev_mode(fb_helper);
959 drm_modeset_unlock_all(dev);
964 * Allocates the backing storage and sets up the fbdev info structure through
965 * the ->fb_probe callback and then registers the fbdev and sets up the panic
968 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
974 struct fb_info *info;
975 struct drm_fb_helper_surface_size sizes;
981 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
982 sizes.surface_depth = 24;
983 sizes.surface_bpp = 32;
984 sizes.fb_width = (unsigned)-1;
985 sizes.fb_height = (unsigned)-1;
987 /* if driver picks 8 or 16 by default use that
988 for both depth/bpp */
989 if (preferred_bpp != sizes.surface_bpp)
990 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
992 /* first up get a count of crtcs now in use and new min/maxes width/heights */
993 for (i = 0; i < fb_helper->connector_count; i++) {
994 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
995 struct drm_cmdline_mode *cmdline_mode;
997 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
999 if (cmdline_mode->bpp_specified) {
1000 switch (cmdline_mode->bpp) {
1002 sizes.surface_depth = sizes.surface_bpp = 8;
1005 sizes.surface_depth = 15;
1006 sizes.surface_bpp = 16;
1009 sizes.surface_depth = sizes.surface_bpp = 16;
1012 sizes.surface_depth = sizes.surface_bpp = 24;
1015 sizes.surface_depth = 24;
1016 sizes.surface_bpp = 32;
1024 for (i = 0; i < fb_helper->crtc_count; i++) {
1025 struct drm_display_mode *desired_mode;
1026 desired_mode = fb_helper->crtc_info[i].desired_mode;
1029 if (gamma_size == 0)
1030 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1031 if (desired_mode->hdisplay < sizes.fb_width)
1032 sizes.fb_width = desired_mode->hdisplay;
1033 if (desired_mode->vdisplay < sizes.fb_height)
1034 sizes.fb_height = desired_mode->vdisplay;
1035 if (desired_mode->hdisplay > sizes.surface_width)
1036 sizes.surface_width = desired_mode->hdisplay;
1037 if (desired_mode->vdisplay > sizes.surface_height)
1038 sizes.surface_height = desired_mode->vdisplay;
1043 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
1044 /* hmm everyone went away - assume VGA cable just fell out
1045 and will come back later. */
1046 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
1047 sizes.fb_width = sizes.surface_width = 1024;
1048 sizes.fb_height = sizes.surface_height = 768;
1051 /* push down into drivers */
1052 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1056 info = fb_helper->fbdev;
1059 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1060 * events, but at init time drm_setup_crtcs needs to be called before
1061 * the fb is allocated (since we need to figure out the desired size of
1062 * the fb before we can allocate it ...). Hence we need to fix things up
1065 for (i = 0; i < fb_helper->crtc_count; i++)
1066 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1067 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1069 #ifdef __DragonFly__
1070 TUNABLE_INT_FETCH("kern.kms_console", &kms_console);
1072 info->cookie = fb_helper;
1073 info->restore = (void *)&sc_restore_fbdev_mode;
1074 if (register_framebuffer(info) < 0)
1080 info->var.pixclock = 0;
1081 if (register_framebuffer(info) < 0)
1084 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
1085 info->node, info->fix.id);
1087 /* Switch back to kernel console on panic */
1088 /* multi card linked list maybe */
1089 if (list_empty(&kernel_fb_helper_list)) {
1090 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
1091 atomic_notifier_chain_register(&panic_notifier_list,
1093 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1096 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1104 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1105 * @info: fbdev registered by the helper
1106 * @pitch: desired pitch
1107 * @depth: desired depth
1109 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1110 * fbdev emulations. Drivers which support acceleration methods which impose
1111 * additional constraints need to set up their own limits.
1113 * Drivers should call this (or their equivalent setup code) from their
1114 * ->fb_probe callback.
1116 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1119 info->fix.type = FB_TYPE_PACKED_PIXELS;
1120 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1121 FB_VISUAL_TRUECOLOR;
1122 info->fix.mmio_start = 0;
1123 info->fix.mmio_len = 0;
1124 info->fix.type_aux = 0;
1125 info->fix.xpanstep = 1; /* doing it in hw */
1126 info->fix.ypanstep = 1; /* doing it in hw */
1127 info->fix.ywrapstep = 0;
1128 info->fix.accel = FB_ACCEL_NONE;
1130 info->fix.line_length = pitch;
1133 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1136 * drm_fb_helper_fill_var - initalizes variable fbdev information
1137 * @info: fbdev instance to set up
1138 * @fb_helper: fb helper instance to use as template
1139 * @fb_width: desired fb width
1140 * @fb_height: desired fb height
1142 * Sets up the variable fbdev metainformation from the given fb helper instance
1143 * and the drm framebuffer allocated in fb_helper->fb.
1145 * Drivers should call this (or their equivalent setup code) from their
1146 * ->fb_probe callback after having allocated the fbdev backing
1147 * storage framebuffer.
1149 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1150 uint32_t fb_width, uint32_t fb_height)
1152 struct drm_framebuffer *fb = fb_helper->fb;
1153 info->pseudo_palette = fb_helper->pseudo_palette;
1154 info->var.xres_virtual = fb->width;
1155 info->var.yres_virtual = fb->height;
1156 info->var.bits_per_pixel = fb->bits_per_pixel;
1157 info->var.accel_flags = FB_ACCELF_TEXT;
1158 info->var.xoffset = 0;
1159 info->var.yoffset = 0;
1160 info->var.activate = FB_ACTIVATE_NOW;
1161 info->var.height = -1;
1162 info->var.width = -1;
1164 switch (fb->depth) {
1166 info->var.red.offset = 0;
1167 info->var.green.offset = 0;
1168 info->var.blue.offset = 0;
1169 info->var.red.length = 8; /* 8bit DAC */
1170 info->var.green.length = 8;
1171 info->var.blue.length = 8;
1172 info->var.transp.offset = 0;
1173 info->var.transp.length = 0;
1176 info->var.red.offset = 10;
1177 info->var.green.offset = 5;
1178 info->var.blue.offset = 0;
1179 info->var.red.length = 5;
1180 info->var.green.length = 5;
1181 info->var.blue.length = 5;
1182 info->var.transp.offset = 15;
1183 info->var.transp.length = 1;
1186 info->var.red.offset = 11;
1187 info->var.green.offset = 5;
1188 info->var.blue.offset = 0;
1189 info->var.red.length = 5;
1190 info->var.green.length = 6;
1191 info->var.blue.length = 5;
1192 info->var.transp.offset = 0;
1195 info->var.red.offset = 16;
1196 info->var.green.offset = 8;
1197 info->var.blue.offset = 0;
1198 info->var.red.length = 8;
1199 info->var.green.length = 8;
1200 info->var.blue.length = 8;
1201 info->var.transp.offset = 0;
1202 info->var.transp.length = 0;
1205 info->var.red.offset = 16;
1206 info->var.green.offset = 8;
1207 info->var.blue.offset = 0;
1208 info->var.red.length = 8;
1209 info->var.green.length = 8;
1210 info->var.blue.length = 8;
1211 info->var.transp.offset = 24;
1212 info->var.transp.length = 8;
1218 info->var.xres = fb_width;
1219 info->var.yres = fb_height;
1221 EXPORT_SYMBOL(drm_fb_helper_fill_var);
1224 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1228 struct drm_connector *connector;
1232 for (i = 0; i < fb_helper->connector_count; i++) {
1233 connector = fb_helper->connector_info[i]->connector;
1234 count += connector->funcs->fill_modes(connector, maxX, maxY);
1240 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1242 struct drm_display_mode *mode;
1244 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1245 if (mode->hdisplay > width ||
1246 mode->vdisplay > height)
1248 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1253 EXPORT_SYMBOL(drm_has_preferred_mode);
1255 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1257 return fb_connector->connector->cmdline_mode.specified;
1260 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1261 int width, int height)
1263 struct drm_cmdline_mode *cmdline_mode;
1264 struct drm_display_mode *mode = NULL;
1265 bool prefer_non_interlace;
1267 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1268 if (cmdline_mode->specified == false)
1271 /* attempt to find a matching mode in the list of modes
1272 * we have gotten so far, if not add a CVT mode that conforms
1274 if (cmdline_mode->rb || cmdline_mode->margins)
1277 prefer_non_interlace = !cmdline_mode->interlace;
1279 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1280 /* check width/height */
1281 if (mode->hdisplay != cmdline_mode->xres ||
1282 mode->vdisplay != cmdline_mode->yres)
1285 if (cmdline_mode->refresh_specified) {
1286 if (mode->vrefresh != cmdline_mode->refresh)
1290 if (cmdline_mode->interlace) {
1291 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1293 } else if (prefer_non_interlace) {
1294 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1300 if (prefer_non_interlace) {
1301 prefer_non_interlace = false;
1306 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1308 list_add(&mode->head, &fb_helper_conn->connector->modes);
1311 EXPORT_SYMBOL(drm_pick_cmdline_mode);
1313 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1318 enable = connector->status == connector_status_connected;
1320 enable = connector->status != connector_status_disconnected;
1325 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1328 bool any_enabled = false;
1329 struct drm_connector *connector;
1332 for (i = 0; i < fb_helper->connector_count; i++) {
1333 connector = fb_helper->connector_info[i]->connector;
1334 enabled[i] = drm_connector_enabled(connector, true);
1335 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1336 enabled[i] ? "yes" : "no");
1337 any_enabled |= enabled[i];
1343 for (i = 0; i < fb_helper->connector_count; i++) {
1344 connector = fb_helper->connector_info[i]->connector;
1345 enabled[i] = drm_connector_enabled(connector, false);
1349 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1350 struct drm_display_mode **modes,
1351 bool *enabled, int width, int height)
1354 bool can_clone = false;
1355 struct drm_fb_helper_connector *fb_helper_conn;
1356 struct drm_display_mode *dmt_mode, *mode;
1358 /* only contemplate cloning in the single crtc case */
1359 if (fb_helper->crtc_count > 1)
1363 for (i = 0; i < fb_helper->connector_count; i++) {
1368 /* only contemplate cloning if more than one connector is enabled */
1372 /* check the command line or if nothing common pick 1024x768 */
1374 for (i = 0; i < fb_helper->connector_count; i++) {
1377 fb_helper_conn = fb_helper->connector_info[i];
1378 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1383 for (j = 0; j < i; j++) {
1386 if (!drm_mode_equal(modes[j], modes[i]))
1392 DRM_DEBUG_KMS("can clone using command line\n");
1396 /* try and find a 1024x768 mode on each connector */
1398 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1400 for (i = 0; i < fb_helper->connector_count; i++) {
1405 fb_helper_conn = fb_helper->connector_info[i];
1406 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1407 if (drm_mode_equal(mode, dmt_mode))
1415 DRM_DEBUG_KMS("can clone using 1024x768\n");
1418 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1422 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1423 struct drm_display_mode **modes,
1424 bool *enabled, int width, int height)
1426 struct drm_fb_helper_connector *fb_helper_conn;
1429 for (i = 0; i < fb_helper->connector_count; i++) {
1430 fb_helper_conn = fb_helper->connector_info[i];
1432 if (enabled[i] == false)
1435 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1436 fb_helper_conn->connector->base.id);
1438 /* got for command line mode first */
1439 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1441 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1442 fb_helper_conn->connector->base.id);
1443 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1445 /* No preferred modes, pick one off the list */
1446 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1447 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1450 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1456 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1457 struct drm_fb_helper_crtc **best_crtcs,
1458 struct drm_display_mode **modes,
1459 int n, int width, int height)
1462 struct drm_device *dev = fb_helper->dev;
1463 struct drm_connector *connector;
1464 struct drm_connector_helper_funcs *connector_funcs;
1465 struct drm_encoder *encoder;
1466 int my_score, best_score, score;
1467 struct drm_fb_helper_crtc **crtcs, *crtc;
1468 struct drm_fb_helper_connector *fb_helper_conn;
1470 if (n == fb_helper->connector_count)
1473 fb_helper_conn = fb_helper->connector_info[n];
1474 connector = fb_helper_conn->connector;
1476 best_crtcs[n] = NULL;
1477 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1478 if (modes[n] == NULL)
1481 crtcs = kzalloc(dev->mode_config.num_connector *
1482 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1487 if (connector->status == connector_status_connected)
1489 if (drm_has_cmdline_mode(fb_helper_conn))
1491 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1494 connector_funcs = connector->helper_private;
1495 encoder = connector_funcs->best_encoder(connector);
1499 /* select a crtc for this connector and then attempt to configure
1500 remaining connectors */
1501 for (c = 0; c < fb_helper->crtc_count; c++) {
1502 crtc = &fb_helper->crtc_info[c];
1504 if ((encoder->possible_crtcs & (1 << c)) == 0)
1507 for (o = 0; o < n; o++)
1508 if (best_crtcs[o] == crtc)
1512 /* ignore cloning unless only a single crtc */
1513 if (fb_helper->crtc_count > 1)
1516 if (!drm_mode_equal(modes[o], modes[n]))
1521 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1522 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1524 if (score > best_score) {
1526 memcpy(best_crtcs, crtcs,
1527 dev->mode_config.num_connector *
1528 sizeof(struct drm_fb_helper_crtc *));
1536 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1538 struct drm_device *dev = fb_helper->dev;
1539 struct drm_fb_helper_crtc **crtcs;
1540 struct drm_display_mode **modes;
1541 struct drm_mode_set *modeset;
1546 DRM_DEBUG_KMS("\n");
1548 width = dev->mode_config.max_width;
1549 height = dev->mode_config.max_height;
1551 crtcs = kcalloc(dev->mode_config.num_connector,
1552 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1553 modes = kcalloc(dev->mode_config.num_connector,
1554 sizeof(struct drm_display_mode *), GFP_KERNEL);
1555 enabled = kcalloc(dev->mode_config.num_connector,
1556 sizeof(bool), GFP_KERNEL);
1557 if (!crtcs || !modes || !enabled) {
1558 DRM_ERROR("Memory allocation failed\n");
1563 drm_enable_connectors(fb_helper, enabled);
1565 if (!(fb_helper->funcs->initial_config &&
1566 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
1567 enabled, width, height))) {
1568 memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
1569 memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
1571 if (!drm_target_cloned(fb_helper,
1572 modes, enabled, width, height) &&
1573 !drm_target_preferred(fb_helper,
1574 modes, enabled, width, height))
1575 DRM_ERROR("Unable to find initial modes\n");
1577 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1580 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1583 /* need to set the modesets up here for use later */
1584 /* fill out the connector<->crtc mappings into the modesets */
1585 for (i = 0; i < fb_helper->crtc_count; i++) {
1586 modeset = &fb_helper->crtc_info[i].mode_set;
1587 modeset->num_connectors = 0;
1591 for (i = 0; i < fb_helper->connector_count; i++) {
1592 struct drm_display_mode *mode = modes[i];
1593 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1594 modeset = &fb_crtc->mode_set;
1596 if (mode && fb_crtc) {
1597 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1598 mode->name, fb_crtc->mode_set.crtc->base.id);
1599 fb_crtc->desired_mode = mode;
1601 drm_mode_destroy(dev, modeset->mode);
1602 modeset->mode = drm_mode_duplicate(dev,
1603 fb_crtc->desired_mode);
1604 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1605 modeset->fb = fb_helper->fb;
1609 /* Clear out any old modes if there are no more connected outputs. */
1610 for (i = 0; i < fb_helper->crtc_count; i++) {
1611 modeset = &fb_helper->crtc_info[i].mode_set;
1612 if (modeset->num_connectors == 0) {
1613 BUG_ON(modeset->fb);
1614 BUG_ON(modeset->num_connectors);
1616 drm_mode_destroy(dev, modeset->mode);
1617 modeset->mode = NULL;
1627 * drm_fb_helper_initial_config - setup a sane initial connector configuration
1628 * @fb_helper: fb_helper device struct
1629 * @bpp_sel: bpp value to use for the framebuffer configuration
1631 * Scans the CRTCs and connectors and tries to put together an initial setup.
1632 * At the moment, this is a cloned configuration across all heads with
1633 * a new framebuffer object as the backing store.
1635 * Note that this also registers the fbdev and so allows userspace to call into
1636 * the driver through the fbdev interfaces.
1638 * This function will call down into the ->fb_probe callback to let
1639 * the driver allocate and initialize the fbdev info structure and the drm
1640 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1641 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1642 * values for the fbdev info structure.
1645 * Zero if everything went ok, nonzero otherwise.
1647 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1649 struct drm_device *dev = fb_helper->dev;
1652 mutex_lock(&dev->mode_config.mutex);
1653 count = drm_fb_helper_probe_connector_modes(fb_helper,
1654 dev->mode_config.max_width,
1655 dev->mode_config.max_height);
1656 mutex_unlock(&dev->mode_config.mutex);
1658 * we shouldn't end up with no modes here.
1661 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
1663 drm_setup_crtcs(fb_helper);
1665 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1667 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1670 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1671 * probing all the outputs attached to the fb
1672 * @fb_helper: the drm_fb_helper
1674 * Scan the connectors attached to the fb_helper and try to put together a
1675 * setup after *notification of a change in output configuration.
1677 * Called at runtime, takes the mode config locks to be able to check/change the
1678 * modeset configuration. Must be run from process context (which usually means
1679 * either the output polling work or a work item launched from the driver's
1680 * hotplug interrupt).
1682 * Note that drivers may call this even before calling
1683 * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
1684 * for a race-free fbcon setup and will make sure that the fbdev emulation will
1685 * not miss any hotplug events.
1688 * 0 on success and a non-zero error code otherwise.
1690 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1692 struct drm_device *dev = fb_helper->dev;
1693 u32 max_width, max_height;
1695 mutex_lock(&fb_helper->dev->mode_config.mutex);
1696 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
1697 fb_helper->delayed_hotplug = true;
1698 mutex_unlock(&fb_helper->dev->mode_config.mutex);
1701 DRM_DEBUG_KMS("\n");
1703 max_width = fb_helper->fb->width;
1704 max_height = fb_helper->fb->height;
1706 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
1707 mutex_unlock(&fb_helper->dev->mode_config.mutex);
1709 drm_modeset_lock_all(dev);
1710 drm_setup_crtcs(fb_helper);
1711 drm_modeset_unlock_all(dev);
1713 drm_fb_helper_set_par(fb_helper->fbdev);
1718 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
1720 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
1721 * but the module doesn't depend on any fb console symbols. At least
1722 * attempt to load fbcon to avoid leaving the system without a usable console.
1724 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
1725 static int __init drm_fb_helper_modinit(void)
1727 const char *name = "fbcon";
1728 struct module *fbcon;
1730 mutex_lock(&module_mutex);
1731 fbcon = find_module(name);
1732 mutex_unlock(&module_mutex);
1735 request_module_nowait(name);
1739 module_init(drm_fb_helper_modinit);