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 do_restore_fbdev_mode(void *context, int pending)
951 struct drm_fb_helper *fb_helper = context;
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);
963 sc_restore_fbdev_mode(void *cookie)
965 struct drm_fb_helper *fb_helper = cookie;
970 taskqueue_enqueue(taskqueue_thread[0], &fb_helper->fb_mode_task);
975 * Allocates the backing storage and sets up the fbdev info structure through
976 * the ->fb_probe callback and then registers the fbdev and sets up the panic
979 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
985 struct fb_info *info;
986 struct drm_fb_helper_surface_size sizes;
992 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
993 sizes.surface_depth = 24;
994 sizes.surface_bpp = 32;
995 sizes.fb_width = (unsigned)-1;
996 sizes.fb_height = (unsigned)-1;
998 /* if driver picks 8 or 16 by default use that
999 for both depth/bpp */
1000 if (preferred_bpp != sizes.surface_bpp)
1001 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
1003 /* first up get a count of crtcs now in use and new min/maxes width/heights */
1004 for (i = 0; i < fb_helper->connector_count; i++) {
1005 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
1006 struct drm_cmdline_mode *cmdline_mode;
1008 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1010 if (cmdline_mode->bpp_specified) {
1011 switch (cmdline_mode->bpp) {
1013 sizes.surface_depth = sizes.surface_bpp = 8;
1016 sizes.surface_depth = 15;
1017 sizes.surface_bpp = 16;
1020 sizes.surface_depth = sizes.surface_bpp = 16;
1023 sizes.surface_depth = sizes.surface_bpp = 24;
1026 sizes.surface_depth = 24;
1027 sizes.surface_bpp = 32;
1035 for (i = 0; i < fb_helper->crtc_count; i++) {
1036 struct drm_display_mode *desired_mode;
1037 desired_mode = fb_helper->crtc_info[i].desired_mode;
1040 if (gamma_size == 0)
1041 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1042 if (desired_mode->hdisplay < sizes.fb_width)
1043 sizes.fb_width = desired_mode->hdisplay;
1044 if (desired_mode->vdisplay < sizes.fb_height)
1045 sizes.fb_height = desired_mode->vdisplay;
1046 if (desired_mode->hdisplay > sizes.surface_width)
1047 sizes.surface_width = desired_mode->hdisplay;
1048 if (desired_mode->vdisplay > sizes.surface_height)
1049 sizes.surface_height = desired_mode->vdisplay;
1054 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
1055 /* hmm everyone went away - assume VGA cable just fell out
1056 and will come back later. */
1057 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
1058 sizes.fb_width = sizes.surface_width = 1024;
1059 sizes.fb_height = sizes.surface_height = 768;
1062 /* push down into drivers */
1063 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1067 info = fb_helper->fbdev;
1070 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1071 * events, but at init time drm_setup_crtcs needs to be called before
1072 * the fb is allocated (since we need to figure out the desired size of
1073 * the fb before we can allocate it ...). Hence we need to fix things up
1076 for (i = 0; i < fb_helper->crtc_count; i++)
1077 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1078 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1080 #ifdef __DragonFly__
1081 TUNABLE_INT_FETCH("kern.kms_console", &kms_console);
1083 TASK_INIT(&fb_helper->fb_mode_task, 0, do_restore_fbdev_mode,
1085 info->cookie = fb_helper;
1086 info->restore = (void *)&sc_restore_fbdev_mode;
1087 if (register_framebuffer(info) < 0)
1093 info->var.pixclock = 0;
1094 if (register_framebuffer(info) < 0)
1097 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
1098 info->node, info->fix.id);
1100 /* Switch back to kernel console on panic */
1101 /* multi card linked list maybe */
1102 if (list_empty(&kernel_fb_helper_list)) {
1103 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
1104 atomic_notifier_chain_register(&panic_notifier_list,
1106 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1109 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1117 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1118 * @info: fbdev registered by the helper
1119 * @pitch: desired pitch
1120 * @depth: desired depth
1122 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1123 * fbdev emulations. Drivers which support acceleration methods which impose
1124 * additional constraints need to set up their own limits.
1126 * Drivers should call this (or their equivalent setup code) from their
1127 * ->fb_probe callback.
1129 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1132 info->fix.type = FB_TYPE_PACKED_PIXELS;
1133 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1134 FB_VISUAL_TRUECOLOR;
1135 info->fix.mmio_start = 0;
1136 info->fix.mmio_len = 0;
1137 info->fix.type_aux = 0;
1138 info->fix.xpanstep = 1; /* doing it in hw */
1139 info->fix.ypanstep = 1; /* doing it in hw */
1140 info->fix.ywrapstep = 0;
1141 info->fix.accel = FB_ACCEL_NONE;
1143 info->fix.line_length = pitch;
1146 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1149 * drm_fb_helper_fill_var - initalizes variable fbdev information
1150 * @info: fbdev instance to set up
1151 * @fb_helper: fb helper instance to use as template
1152 * @fb_width: desired fb width
1153 * @fb_height: desired fb height
1155 * Sets up the variable fbdev metainformation from the given fb helper instance
1156 * and the drm framebuffer allocated in fb_helper->fb.
1158 * Drivers should call this (or their equivalent setup code) from their
1159 * ->fb_probe callback after having allocated the fbdev backing
1160 * storage framebuffer.
1162 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1163 uint32_t fb_width, uint32_t fb_height)
1165 struct drm_framebuffer *fb = fb_helper->fb;
1166 info->pseudo_palette = fb_helper->pseudo_palette;
1167 info->var.xres_virtual = fb->width;
1168 info->var.yres_virtual = fb->height;
1169 info->var.bits_per_pixel = fb->bits_per_pixel;
1170 info->var.accel_flags = FB_ACCELF_TEXT;
1171 info->var.xoffset = 0;
1172 info->var.yoffset = 0;
1173 info->var.activate = FB_ACTIVATE_NOW;
1174 info->var.height = -1;
1175 info->var.width = -1;
1177 switch (fb->depth) {
1179 info->var.red.offset = 0;
1180 info->var.green.offset = 0;
1181 info->var.blue.offset = 0;
1182 info->var.red.length = 8; /* 8bit DAC */
1183 info->var.green.length = 8;
1184 info->var.blue.length = 8;
1185 info->var.transp.offset = 0;
1186 info->var.transp.length = 0;
1189 info->var.red.offset = 10;
1190 info->var.green.offset = 5;
1191 info->var.blue.offset = 0;
1192 info->var.red.length = 5;
1193 info->var.green.length = 5;
1194 info->var.blue.length = 5;
1195 info->var.transp.offset = 15;
1196 info->var.transp.length = 1;
1199 info->var.red.offset = 11;
1200 info->var.green.offset = 5;
1201 info->var.blue.offset = 0;
1202 info->var.red.length = 5;
1203 info->var.green.length = 6;
1204 info->var.blue.length = 5;
1205 info->var.transp.offset = 0;
1208 info->var.red.offset = 16;
1209 info->var.green.offset = 8;
1210 info->var.blue.offset = 0;
1211 info->var.red.length = 8;
1212 info->var.green.length = 8;
1213 info->var.blue.length = 8;
1214 info->var.transp.offset = 0;
1215 info->var.transp.length = 0;
1218 info->var.red.offset = 16;
1219 info->var.green.offset = 8;
1220 info->var.blue.offset = 0;
1221 info->var.red.length = 8;
1222 info->var.green.length = 8;
1223 info->var.blue.length = 8;
1224 info->var.transp.offset = 24;
1225 info->var.transp.length = 8;
1231 info->var.xres = fb_width;
1232 info->var.yres = fb_height;
1234 EXPORT_SYMBOL(drm_fb_helper_fill_var);
1237 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1241 struct drm_connector *connector;
1245 for (i = 0; i < fb_helper->connector_count; i++) {
1246 connector = fb_helper->connector_info[i]->connector;
1247 count += connector->funcs->fill_modes(connector, maxX, maxY);
1253 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1255 struct drm_display_mode *mode;
1257 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1258 if (mode->hdisplay > width ||
1259 mode->vdisplay > height)
1261 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1266 EXPORT_SYMBOL(drm_has_preferred_mode);
1268 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1270 return fb_connector->connector->cmdline_mode.specified;
1273 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1274 int width, int height)
1276 struct drm_cmdline_mode *cmdline_mode;
1277 struct drm_display_mode *mode = NULL;
1278 bool prefer_non_interlace;
1280 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1281 if (cmdline_mode->specified == false)
1284 /* attempt to find a matching mode in the list of modes
1285 * we have gotten so far, if not add a CVT mode that conforms
1287 if (cmdline_mode->rb || cmdline_mode->margins)
1290 prefer_non_interlace = !cmdline_mode->interlace;
1292 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1293 /* check width/height */
1294 if (mode->hdisplay != cmdline_mode->xres ||
1295 mode->vdisplay != cmdline_mode->yres)
1298 if (cmdline_mode->refresh_specified) {
1299 if (mode->vrefresh != cmdline_mode->refresh)
1303 if (cmdline_mode->interlace) {
1304 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1306 } else if (prefer_non_interlace) {
1307 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1313 if (prefer_non_interlace) {
1314 prefer_non_interlace = false;
1319 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1321 list_add(&mode->head, &fb_helper_conn->connector->modes);
1324 EXPORT_SYMBOL(drm_pick_cmdline_mode);
1326 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1331 enable = connector->status == connector_status_connected;
1333 enable = connector->status != connector_status_disconnected;
1338 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1341 bool any_enabled = false;
1342 struct drm_connector *connector;
1345 for (i = 0; i < fb_helper->connector_count; i++) {
1346 connector = fb_helper->connector_info[i]->connector;
1347 enabled[i] = drm_connector_enabled(connector, true);
1348 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1349 enabled[i] ? "yes" : "no");
1350 any_enabled |= enabled[i];
1356 for (i = 0; i < fb_helper->connector_count; i++) {
1357 connector = fb_helper->connector_info[i]->connector;
1358 enabled[i] = drm_connector_enabled(connector, false);
1362 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1363 struct drm_display_mode **modes,
1364 bool *enabled, int width, int height)
1367 bool can_clone = false;
1368 struct drm_fb_helper_connector *fb_helper_conn;
1369 struct drm_display_mode *dmt_mode, *mode;
1371 /* only contemplate cloning in the single crtc case */
1372 if (fb_helper->crtc_count > 1)
1376 for (i = 0; i < fb_helper->connector_count; i++) {
1381 /* only contemplate cloning if more than one connector is enabled */
1385 /* check the command line or if nothing common pick 1024x768 */
1387 for (i = 0; i < fb_helper->connector_count; i++) {
1390 fb_helper_conn = fb_helper->connector_info[i];
1391 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1396 for (j = 0; j < i; j++) {
1399 if (!drm_mode_equal(modes[j], modes[i]))
1405 DRM_DEBUG_KMS("can clone using command line\n");
1409 /* try and find a 1024x768 mode on each connector */
1411 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1413 for (i = 0; i < fb_helper->connector_count; i++) {
1418 fb_helper_conn = fb_helper->connector_info[i];
1419 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1420 if (drm_mode_equal(mode, dmt_mode))
1428 DRM_DEBUG_KMS("can clone using 1024x768\n");
1431 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1435 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1436 struct drm_display_mode **modes,
1437 bool *enabled, int width, int height)
1439 struct drm_fb_helper_connector *fb_helper_conn;
1442 for (i = 0; i < fb_helper->connector_count; i++) {
1443 fb_helper_conn = fb_helper->connector_info[i];
1445 if (enabled[i] == false)
1448 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1449 fb_helper_conn->connector->base.id);
1451 /* got for command line mode first */
1452 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1454 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1455 fb_helper_conn->connector->base.id);
1456 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1458 /* No preferred modes, pick one off the list */
1459 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1460 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1463 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1469 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1470 struct drm_fb_helper_crtc **best_crtcs,
1471 struct drm_display_mode **modes,
1472 int n, int width, int height)
1475 struct drm_device *dev = fb_helper->dev;
1476 struct drm_connector *connector;
1477 struct drm_connector_helper_funcs *connector_funcs;
1478 struct drm_encoder *encoder;
1479 int my_score, best_score, score;
1480 struct drm_fb_helper_crtc **crtcs, *crtc;
1481 struct drm_fb_helper_connector *fb_helper_conn;
1483 if (n == fb_helper->connector_count)
1486 fb_helper_conn = fb_helper->connector_info[n];
1487 connector = fb_helper_conn->connector;
1489 best_crtcs[n] = NULL;
1490 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1491 if (modes[n] == NULL)
1494 crtcs = kzalloc(dev->mode_config.num_connector *
1495 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1500 if (connector->status == connector_status_connected)
1502 if (drm_has_cmdline_mode(fb_helper_conn))
1504 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1507 connector_funcs = connector->helper_private;
1508 encoder = connector_funcs->best_encoder(connector);
1512 /* select a crtc for this connector and then attempt to configure
1513 remaining connectors */
1514 for (c = 0; c < fb_helper->crtc_count; c++) {
1515 crtc = &fb_helper->crtc_info[c];
1517 if ((encoder->possible_crtcs & (1 << c)) == 0)
1520 for (o = 0; o < n; o++)
1521 if (best_crtcs[o] == crtc)
1525 /* ignore cloning unless only a single crtc */
1526 if (fb_helper->crtc_count > 1)
1529 if (!drm_mode_equal(modes[o], modes[n]))
1534 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1535 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1537 if (score > best_score) {
1539 memcpy(best_crtcs, crtcs,
1540 dev->mode_config.num_connector *
1541 sizeof(struct drm_fb_helper_crtc *));
1549 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1551 struct drm_device *dev = fb_helper->dev;
1552 struct drm_fb_helper_crtc **crtcs;
1553 struct drm_display_mode **modes;
1554 struct drm_mode_set *modeset;
1559 DRM_DEBUG_KMS("\n");
1561 width = dev->mode_config.max_width;
1562 height = dev->mode_config.max_height;
1564 crtcs = kcalloc(dev->mode_config.num_connector,
1565 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1566 modes = kcalloc(dev->mode_config.num_connector,
1567 sizeof(struct drm_display_mode *), GFP_KERNEL);
1568 enabled = kcalloc(dev->mode_config.num_connector,
1569 sizeof(bool), GFP_KERNEL);
1570 if (!crtcs || !modes || !enabled) {
1571 DRM_ERROR("Memory allocation failed\n");
1576 drm_enable_connectors(fb_helper, enabled);
1578 if (!(fb_helper->funcs->initial_config &&
1579 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
1580 enabled, width, height))) {
1581 memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
1582 memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
1584 if (!drm_target_cloned(fb_helper,
1585 modes, enabled, width, height) &&
1586 !drm_target_preferred(fb_helper,
1587 modes, enabled, width, height))
1588 DRM_ERROR("Unable to find initial modes\n");
1590 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1593 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1596 /* need to set the modesets up here for use later */
1597 /* fill out the connector<->crtc mappings into the modesets */
1598 for (i = 0; i < fb_helper->crtc_count; i++) {
1599 modeset = &fb_helper->crtc_info[i].mode_set;
1600 modeset->num_connectors = 0;
1604 for (i = 0; i < fb_helper->connector_count; i++) {
1605 struct drm_display_mode *mode = modes[i];
1606 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1607 modeset = &fb_crtc->mode_set;
1609 if (mode && fb_crtc) {
1610 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1611 mode->name, fb_crtc->mode_set.crtc->base.id);
1612 fb_crtc->desired_mode = mode;
1614 drm_mode_destroy(dev, modeset->mode);
1615 modeset->mode = drm_mode_duplicate(dev,
1616 fb_crtc->desired_mode);
1617 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1618 modeset->fb = fb_helper->fb;
1622 /* Clear out any old modes if there are no more connected outputs. */
1623 for (i = 0; i < fb_helper->crtc_count; i++) {
1624 modeset = &fb_helper->crtc_info[i].mode_set;
1625 if (modeset->num_connectors == 0) {
1626 BUG_ON(modeset->fb);
1627 BUG_ON(modeset->num_connectors);
1629 drm_mode_destroy(dev, modeset->mode);
1630 modeset->mode = NULL;
1640 * drm_fb_helper_initial_config - setup a sane initial connector configuration
1641 * @fb_helper: fb_helper device struct
1642 * @bpp_sel: bpp value to use for the framebuffer configuration
1644 * Scans the CRTCs and connectors and tries to put together an initial setup.
1645 * At the moment, this is a cloned configuration across all heads with
1646 * a new framebuffer object as the backing store.
1648 * Note that this also registers the fbdev and so allows userspace to call into
1649 * the driver through the fbdev interfaces.
1651 * This function will call down into the ->fb_probe callback to let
1652 * the driver allocate and initialize the fbdev info structure and the drm
1653 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1654 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1655 * values for the fbdev info structure.
1658 * Zero if everything went ok, nonzero otherwise.
1660 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1662 struct drm_device *dev = fb_helper->dev;
1665 mutex_lock(&dev->mode_config.mutex);
1666 count = drm_fb_helper_probe_connector_modes(fb_helper,
1667 dev->mode_config.max_width,
1668 dev->mode_config.max_height);
1669 mutex_unlock(&dev->mode_config.mutex);
1671 * we shouldn't end up with no modes here.
1674 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
1676 drm_setup_crtcs(fb_helper);
1678 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1680 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1683 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1684 * probing all the outputs attached to the fb
1685 * @fb_helper: the drm_fb_helper
1687 * Scan the connectors attached to the fb_helper and try to put together a
1688 * setup after *notification of a change in output configuration.
1690 * Called at runtime, takes the mode config locks to be able to check/change the
1691 * modeset configuration. Must be run from process context (which usually means
1692 * either the output polling work or a work item launched from the driver's
1693 * hotplug interrupt).
1695 * Note that drivers may call this even before calling
1696 * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
1697 * for a race-free fbcon setup and will make sure that the fbdev emulation will
1698 * not miss any hotplug events.
1701 * 0 on success and a non-zero error code otherwise.
1703 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1705 struct drm_device *dev = fb_helper->dev;
1706 u32 max_width, max_height;
1708 mutex_lock(&fb_helper->dev->mode_config.mutex);
1709 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
1710 fb_helper->delayed_hotplug = true;
1711 mutex_unlock(&fb_helper->dev->mode_config.mutex);
1714 DRM_DEBUG_KMS("\n");
1716 max_width = fb_helper->fb->width;
1717 max_height = fb_helper->fb->height;
1719 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
1720 mutex_unlock(&fb_helper->dev->mode_config.mutex);
1722 drm_modeset_lock_all(dev);
1723 drm_setup_crtcs(fb_helper);
1724 drm_modeset_unlock_all(dev);
1726 drm_fb_helper_set_par(fb_helper->fbdev);
1731 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
1733 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
1734 * but the module doesn't depend on any fb console symbols. At least
1735 * attempt to load fbcon to avoid leaving the system without a usable console.
1737 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
1738 static int __init drm_fb_helper_modinit(void)
1740 const char *name = "fbcon";
1741 struct module *fbcon;
1743 mutex_lock(&module_mutex);
1744 fbcon = find_module(name);
1745 mutex_unlock(&module_mutex);
1748 request_module_nowait(name);
1752 module_init(drm_fb_helper_modinit);