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 MODULE_AUTHOR("David Airlie, Jesse Barnes");
43 MODULE_DESCRIPTION("DRM KMS helper");
44 MODULE_LICENSE("GPL and additional rights");
46 static LIST_HEAD(kernel_fb_helper_list);
51 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
52 * mode setting driver. They can be used mostly independantely from the crtc
53 * helper functions used by many drivers to implement the kernel mode setting
57 /* simple single crtc case helper function */
58 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
60 struct drm_device *dev = fb_helper->dev;
61 struct drm_connector *connector;
64 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
65 struct drm_fb_helper_connector *fb_helper_connector;
67 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
68 if (!fb_helper_connector)
71 fb_helper_connector->connector = connector;
72 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
76 for (i = 0; i < fb_helper->connector_count; i++) {
77 kfree(fb_helper->connector_info[i]);
78 fb_helper->connector_info[i] = NULL;
80 fb_helper->connector_count = 0;
83 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
85 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
87 struct drm_fb_helper_connector *fb_helper_conn;
90 for (i = 0; i < fb_helper->connector_count; i++) {
91 struct drm_cmdline_mode *mode;
92 struct drm_connector *connector;
95 fb_helper_conn = fb_helper->connector_info[i];
96 connector = fb_helper_conn->connector;
97 mode = &fb_helper_conn->cmdline_mode;
99 /* do something on return - turn off connector maybe */
100 if (fb_get_options(drm_get_connector_name(connector), &option))
103 if (drm_mode_parse_command_line_for_connector(option,
108 switch (mode->force) {
112 case DRM_FORCE_ON_DIGITAL:
121 DRM_INFO("forcing %s connector %s\n",
122 drm_get_connector_name(connector), s);
123 connector->force = mode->force;
126 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
127 drm_get_connector_name(connector),
128 mode->xres, mode->yres,
129 mode->refresh_specified ? mode->refresh : 60,
130 mode->rb ? " reduced blanking" : "",
131 mode->margins ? " with margins" : "",
132 mode->interlace ? " interlaced" : "");
139 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
141 uint16_t *r_base, *g_base, *b_base;
144 r_base = crtc->gamma_store;
145 g_base = r_base + crtc->gamma_size;
146 b_base = g_base + crtc->gamma_size;
148 for (i = 0; i < crtc->gamma_size; i++)
149 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
152 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
154 uint16_t *r_base, *g_base, *b_base;
156 if (crtc->funcs->gamma_set == NULL)
159 r_base = crtc->gamma_store;
160 g_base = r_base + crtc->gamma_size;
161 b_base = g_base + crtc->gamma_size;
163 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
166 int drm_fb_helper_debug_enter(struct fb_info *info)
168 struct drm_fb_helper *helper = info->par;
169 struct drm_crtc_helper_funcs *funcs;
172 if (list_empty(&kernel_fb_helper_list))
175 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
176 for (i = 0; i < helper->crtc_count; i++) {
177 struct drm_mode_set *mode_set =
178 &helper->crtc_info[i].mode_set;
180 if (!mode_set->crtc->enabled)
183 funcs = mode_set->crtc->helper_private;
184 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
185 funcs->mode_set_base_atomic(mode_set->crtc,
189 ENTER_ATOMIC_MODE_SET);
195 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
197 /* Find the real fb for a given fb helper CRTC */
198 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
200 struct drm_device *dev = crtc->dev;
203 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
204 if (crtc->base.id == c->base.id)
211 int drm_fb_helper_debug_leave(struct fb_info *info)
213 struct drm_fb_helper *helper = info->par;
214 struct drm_crtc *crtc;
215 struct drm_crtc_helper_funcs *funcs;
216 struct drm_framebuffer *fb;
219 for (i = 0; i < helper->crtc_count; i++) {
220 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
221 crtc = mode_set->crtc;
222 funcs = crtc->helper_private;
223 fb = drm_mode_config_fb(crtc);
229 DRM_ERROR("no fb to restore??\n");
233 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
234 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
235 crtc->y, LEAVE_ATOMIC_MODE_SET);
240 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
242 bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
246 for (i = 0; i < fb_helper->crtc_count; i++) {
247 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
248 ret = drm_mode_set_config_internal(mode_set);
254 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode);
256 static bool drm_fb_helper_force_kernel_mode(void)
258 bool ret, error = false;
259 struct drm_fb_helper *helper;
261 if (list_empty(&kernel_fb_helper_list))
264 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
265 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
268 ret = drm_fb_helper_restore_fbdev_mode(helper);
275 int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
279 * It's a waste of time and effort to switch back to text console
280 * if the kernel should reboot before panic messages can be seen.
282 if (panic_timeout < 0)
285 pr_err("panic occurred, switching back to text console\n");
286 return drm_fb_helper_force_kernel_mode();
288 EXPORT_SYMBOL(drm_fb_helper_panic);
290 static struct notifier_block paniced = {
291 .notifier_call = drm_fb_helper_panic,
295 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
297 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
299 void drm_fb_helper_restore(void)
302 ret = drm_fb_helper_force_kernel_mode();
304 DRM_ERROR("Failed to restore crtc configuration\n");
306 EXPORT_SYMBOL(drm_fb_helper_restore);
308 static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
310 struct drm_device *dev = fb_helper->dev;
311 struct drm_crtc *crtc;
312 int bound = 0, crtcs_bound = 0;
314 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
317 if (crtc->fb == fb_helper->fb)
321 if (bound < crtcs_bound)
326 #ifdef CONFIG_MAGIC_SYSRQ
327 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
329 drm_fb_helper_restore();
331 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
333 static void drm_fb_helper_sysrq(int dummy1)
335 schedule_work(&drm_fb_helper_restore_work);
338 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
339 .handler = drm_fb_helper_sysrq,
340 .help_msg = "force-fb(V)",
341 .action_msg = "Restore framebuffer console",
344 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
347 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
349 struct drm_fb_helper *fb_helper = info->par;
350 struct drm_device *dev = fb_helper->dev;
351 struct drm_crtc *crtc;
352 struct drm_connector *connector;
356 * For each CRTC in this fb, turn the connectors on/off.
358 drm_modeset_lock_all(dev);
359 if (!drm_fb_helper_is_bound(fb_helper)) {
360 drm_modeset_unlock_all(dev);
364 for (i = 0; i < fb_helper->crtc_count; i++) {
365 crtc = fb_helper->crtc_info[i].mode_set.crtc;
370 /* Walk the connectors & encoders on this fb turning them on/off */
371 for (j = 0; j < fb_helper->connector_count; j++) {
372 connector = fb_helper->connector_info[j]->connector;
373 connector->funcs->dpms(connector, dpms_mode);
374 drm_object_property_set_value(&connector->base,
375 dev->mode_config.dpms_property, dpms_mode);
378 drm_modeset_unlock_all(dev);
381 int drm_fb_helper_blank(int blank, struct fb_info *info)
384 /* Display: On; HSync: On, VSync: On */
385 case FB_BLANK_UNBLANK:
386 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
388 /* Display: Off; HSync: On, VSync: On */
389 case FB_BLANK_NORMAL:
390 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
392 /* Display: Off; HSync: Off, VSync: On */
393 case FB_BLANK_HSYNC_SUSPEND:
394 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
396 /* Display: Off; HSync: On, VSync: Off */
397 case FB_BLANK_VSYNC_SUSPEND:
398 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
400 /* Display: Off; HSync: Off, VSync: Off */
401 case FB_BLANK_POWERDOWN:
402 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
407 EXPORT_SYMBOL(drm_fb_helper_blank);
409 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
413 for (i = 0; i < helper->connector_count; i++)
414 kfree(helper->connector_info[i]);
415 kfree(helper->connector_info);
416 for (i = 0; i < helper->crtc_count; i++) {
417 kfree(helper->crtc_info[i].mode_set.connectors);
418 if (helper->crtc_info[i].mode_set.mode)
419 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
421 kfree(helper->crtc_info);
424 int drm_fb_helper_init(struct drm_device *dev,
425 struct drm_fb_helper *fb_helper,
426 int crtc_count, int max_conn_count)
428 struct drm_crtc *crtc;
431 fb_helper->dev = dev;
433 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
435 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
436 if (!fb_helper->crtc_info)
439 fb_helper->crtc_count = crtc_count;
440 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
441 if (!fb_helper->connector_info) {
442 kfree(fb_helper->crtc_info);
445 fb_helper->connector_count = 0;
447 for (i = 0; i < crtc_count; i++) {
448 fb_helper->crtc_info[i].mode_set.connectors =
449 kcalloc(max_conn_count,
450 sizeof(struct drm_connector *),
453 if (!fb_helper->crtc_info[i].mode_set.connectors)
455 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
459 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
460 fb_helper->crtc_info[i].mode_set.crtc = crtc;
466 drm_fb_helper_crtc_free(fb_helper);
469 EXPORT_SYMBOL(drm_fb_helper_init);
471 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
473 if (!list_empty(&fb_helper->kernel_fb_list)) {
474 list_del(&fb_helper->kernel_fb_list);
475 if (list_empty(&kernel_fb_helper_list)) {
476 pr_info("drm: unregistered panic notifier\n");
477 atomic_notifier_chain_unregister(&panic_notifier_list,
479 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
483 drm_fb_helper_crtc_free(fb_helper);
486 EXPORT_SYMBOL(drm_fb_helper_fini);
488 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
489 u16 blue, u16 regno, struct fb_info *info)
491 struct drm_fb_helper *fb_helper = info->par;
492 struct drm_framebuffer *fb = fb_helper->fb;
495 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
498 /* place color in psuedopalette */
501 palette = (u32 *)info->pseudo_palette;
502 red >>= (16 - info->var.red.length);
503 green >>= (16 - info->var.green.length);
504 blue >>= (16 - info->var.blue.length);
505 value = (red << info->var.red.offset) |
506 (green << info->var.green.offset) |
507 (blue << info->var.blue.offset);
508 if (info->var.transp.length > 0) {
509 u32 mask = (1 << info->var.transp.length) - 1;
510 mask <<= info->var.transp.offset;
513 palette[regno] = value;
519 if (fb->bits_per_pixel == 16) {
522 if (fb->depth == 16 && regno > 63)
524 if (fb->depth == 15 && regno > 31)
527 if (fb->depth == 16) {
531 for (i = 0; i < 8; i++)
532 fb_helper->funcs->gamma_set(crtc, red,
533 green, blue, pindex + i);
536 fb_helper->funcs->gamma_get(crtc, &r,
540 for (i = 0; i < 4; i++)
541 fb_helper->funcs->gamma_set(crtc, r,
548 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
552 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
554 struct drm_fb_helper *fb_helper = info->par;
555 struct drm_crtc_helper_funcs *crtc_funcs;
556 u16 *red, *green, *blue, *transp;
557 struct drm_crtc *crtc;
561 for (i = 0; i < fb_helper->crtc_count; i++) {
562 crtc = fb_helper->crtc_info[i].mode_set.crtc;
563 crtc_funcs = crtc->helper_private;
568 transp = cmap->transp;
571 for (j = 0; j < cmap->len; j++) {
572 u16 hred, hgreen, hblue, htransp = 0xffff;
581 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
585 crtc_funcs->load_lut(crtc);
589 EXPORT_SYMBOL(drm_fb_helper_setcmap);
591 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
592 struct fb_info *info)
594 struct drm_fb_helper *fb_helper = info->par;
595 struct drm_framebuffer *fb = fb_helper->fb;
598 if (var->pixclock != 0 || in_dbg_master())
601 /* Need to resize the fb object !!! */
602 if (var->bits_per_pixel > fb->bits_per_pixel ||
603 var->xres > fb->width || var->yres > fb->height ||
604 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
605 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
606 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
607 var->xres, var->yres, var->bits_per_pixel,
608 var->xres_virtual, var->yres_virtual,
609 fb->width, fb->height, fb->bits_per_pixel);
613 switch (var->bits_per_pixel) {
615 depth = (var->green.length == 6) ? 16 : 15;
618 depth = (var->transp.length > 0) ? 32 : 24;
621 depth = var->bits_per_pixel;
628 var->green.offset = 0;
629 var->blue.offset = 0;
631 var->green.length = 8;
632 var->blue.length = 8;
633 var->transp.length = 0;
634 var->transp.offset = 0;
637 var->red.offset = 10;
638 var->green.offset = 5;
639 var->blue.offset = 0;
641 var->green.length = 5;
642 var->blue.length = 5;
643 var->transp.length = 1;
644 var->transp.offset = 15;
647 var->red.offset = 11;
648 var->green.offset = 5;
649 var->blue.offset = 0;
651 var->green.length = 6;
652 var->blue.length = 5;
653 var->transp.length = 0;
654 var->transp.offset = 0;
657 var->red.offset = 16;
658 var->green.offset = 8;
659 var->blue.offset = 0;
661 var->green.length = 8;
662 var->blue.length = 8;
663 var->transp.length = 0;
664 var->transp.offset = 0;
667 var->red.offset = 16;
668 var->green.offset = 8;
669 var->blue.offset = 0;
671 var->green.length = 8;
672 var->blue.length = 8;
673 var->transp.length = 8;
674 var->transp.offset = 24;
681 EXPORT_SYMBOL(drm_fb_helper_check_var);
683 /* this will let fbcon do the mode init */
684 int drm_fb_helper_set_par(struct fb_info *info)
686 struct drm_fb_helper *fb_helper = info->par;
687 struct drm_device *dev = fb_helper->dev;
688 struct fb_var_screeninfo *var = &info->var;
689 struct drm_crtc *crtc;
693 if (var->pixclock != 0) {
694 DRM_ERROR("PIXEL CLOCK SET\n");
698 drm_modeset_lock_all(dev);
699 for (i = 0; i < fb_helper->crtc_count; i++) {
700 crtc = fb_helper->crtc_info[i].mode_set.crtc;
701 ret = drm_mode_set_config_internal(&fb_helper->crtc_info[i].mode_set);
703 drm_modeset_unlock_all(dev);
707 drm_modeset_unlock_all(dev);
709 if (fb_helper->delayed_hotplug) {
710 fb_helper->delayed_hotplug = false;
711 drm_fb_helper_hotplug_event(fb_helper);
715 EXPORT_SYMBOL(drm_fb_helper_set_par);
717 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
718 struct fb_info *info)
720 struct drm_fb_helper *fb_helper = info->par;
721 struct drm_device *dev = fb_helper->dev;
722 struct drm_mode_set *modeset;
723 struct drm_crtc *crtc;
727 drm_modeset_lock_all(dev);
728 if (!drm_fb_helper_is_bound(fb_helper)) {
729 drm_modeset_unlock_all(dev);
733 for (i = 0; i < fb_helper->crtc_count; i++) {
734 crtc = fb_helper->crtc_info[i].mode_set.crtc;
736 modeset = &fb_helper->crtc_info[i].mode_set;
738 modeset->x = var->xoffset;
739 modeset->y = var->yoffset;
741 if (modeset->num_connectors) {
742 ret = drm_mode_set_config_internal(modeset);
744 info->var.xoffset = var->xoffset;
745 info->var.yoffset = var->yoffset;
749 drm_modeset_unlock_all(dev);
752 EXPORT_SYMBOL(drm_fb_helper_pan_display);
754 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
760 struct fb_info *info;
761 struct drm_fb_helper_surface_size sizes;
764 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
765 sizes.surface_depth = 24;
766 sizes.surface_bpp = 32;
767 sizes.fb_width = (unsigned)-1;
768 sizes.fb_height = (unsigned)-1;
770 /* if driver picks 8 or 16 by default use that
771 for both depth/bpp */
772 if (preferred_bpp != sizes.surface_bpp)
773 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
775 /* first up get a count of crtcs now in use and new min/maxes width/heights */
776 for (i = 0; i < fb_helper->connector_count; i++) {
777 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
778 struct drm_cmdline_mode *cmdline_mode;
780 cmdline_mode = &fb_helper_conn->cmdline_mode;
782 if (cmdline_mode->bpp_specified) {
783 switch (cmdline_mode->bpp) {
785 sizes.surface_depth = sizes.surface_bpp = 8;
788 sizes.surface_depth = 15;
789 sizes.surface_bpp = 16;
792 sizes.surface_depth = sizes.surface_bpp = 16;
795 sizes.surface_depth = sizes.surface_bpp = 24;
798 sizes.surface_depth = 24;
799 sizes.surface_bpp = 32;
807 for (i = 0; i < fb_helper->crtc_count; i++) {
808 struct drm_display_mode *desired_mode;
809 desired_mode = fb_helper->crtc_info[i].desired_mode;
813 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
814 if (desired_mode->hdisplay < sizes.fb_width)
815 sizes.fb_width = desired_mode->hdisplay;
816 if (desired_mode->vdisplay < sizes.fb_height)
817 sizes.fb_height = desired_mode->vdisplay;
818 if (desired_mode->hdisplay > sizes.surface_width)
819 sizes.surface_width = desired_mode->hdisplay;
820 if (desired_mode->vdisplay > sizes.surface_height)
821 sizes.surface_height = desired_mode->vdisplay;
826 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
827 /* hmm everyone went away - assume VGA cable just fell out
828 and will come back later. */
829 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
830 sizes.fb_width = sizes.surface_width = 1024;
831 sizes.fb_height = sizes.surface_height = 768;
834 /* push down into drivers */
835 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
839 info = fb_helper->fbdev;
841 /* set the fb pointer */
842 for (i = 0; i < fb_helper->crtc_count; i++)
843 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
846 info->var.pixclock = 0;
847 if (register_framebuffer(info) < 0)
850 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
851 info->node, info->fix.id);
854 drm_fb_helper_set_par(info);
857 /* Switch back to kernel console on panic */
858 /* multi card linked list maybe */
859 if (list_empty(&kernel_fb_helper_list)) {
860 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
861 atomic_notifier_chain_register(&panic_notifier_list,
863 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
866 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
870 EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
872 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
875 info->fix.type = FB_TYPE_PACKED_PIXELS;
876 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
878 info->fix.mmio_start = 0;
879 info->fix.mmio_len = 0;
880 info->fix.type_aux = 0;
881 info->fix.xpanstep = 1; /* doing it in hw */
882 info->fix.ypanstep = 1; /* doing it in hw */
883 info->fix.ywrapstep = 0;
884 info->fix.accel = FB_ACCEL_NONE;
885 info->fix.type_aux = 0;
887 info->fix.line_length = pitch;
890 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
892 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
893 uint32_t fb_width, uint32_t fb_height)
895 struct drm_framebuffer *fb = fb_helper->fb;
896 info->pseudo_palette = fb_helper->pseudo_palette;
897 info->var.xres_virtual = fb->width;
898 info->var.yres_virtual = fb->height;
899 info->var.bits_per_pixel = fb->bits_per_pixel;
900 info->var.accel_flags = FB_ACCELF_TEXT;
901 info->var.xoffset = 0;
902 info->var.yoffset = 0;
903 info->var.activate = FB_ACTIVATE_NOW;
904 info->var.height = -1;
905 info->var.width = -1;
909 info->var.red.offset = 0;
910 info->var.green.offset = 0;
911 info->var.blue.offset = 0;
912 info->var.red.length = 8; /* 8bit DAC */
913 info->var.green.length = 8;
914 info->var.blue.length = 8;
915 info->var.transp.offset = 0;
916 info->var.transp.length = 0;
919 info->var.red.offset = 10;
920 info->var.green.offset = 5;
921 info->var.blue.offset = 0;
922 info->var.red.length = 5;
923 info->var.green.length = 5;
924 info->var.blue.length = 5;
925 info->var.transp.offset = 15;
926 info->var.transp.length = 1;
929 info->var.red.offset = 11;
930 info->var.green.offset = 5;
931 info->var.blue.offset = 0;
932 info->var.red.length = 5;
933 info->var.green.length = 6;
934 info->var.blue.length = 5;
935 info->var.transp.offset = 0;
938 info->var.red.offset = 16;
939 info->var.green.offset = 8;
940 info->var.blue.offset = 0;
941 info->var.red.length = 8;
942 info->var.green.length = 8;
943 info->var.blue.length = 8;
944 info->var.transp.offset = 0;
945 info->var.transp.length = 0;
948 info->var.red.offset = 16;
949 info->var.green.offset = 8;
950 info->var.blue.offset = 0;
951 info->var.red.length = 8;
952 info->var.green.length = 8;
953 info->var.blue.length = 8;
954 info->var.transp.offset = 24;
955 info->var.transp.length = 8;
961 info->var.xres = fb_width;
962 info->var.yres = fb_height;
964 EXPORT_SYMBOL(drm_fb_helper_fill_var);
966 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
970 struct drm_connector *connector;
974 for (i = 0; i < fb_helper->connector_count; i++) {
975 connector = fb_helper->connector_info[i]->connector;
976 count += connector->funcs->fill_modes(connector, maxX, maxY);
982 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
984 struct drm_display_mode *mode;
986 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
987 if (drm_mode_width(mode) > width ||
988 drm_mode_height(mode) > height)
990 if (mode->type & DRM_MODE_TYPE_PREFERRED)
996 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
998 struct drm_cmdline_mode *cmdline_mode;
999 cmdline_mode = &fb_connector->cmdline_mode;
1000 return cmdline_mode->specified;
1003 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1004 int width, int height)
1006 struct drm_cmdline_mode *cmdline_mode;
1007 struct drm_display_mode *mode = NULL;
1009 cmdline_mode = &fb_helper_conn->cmdline_mode;
1010 if (cmdline_mode->specified == false)
1013 /* attempt to find a matching mode in the list of modes
1014 * we have gotten so far, if not add a CVT mode that conforms
1016 if (cmdline_mode->rb || cmdline_mode->margins)
1019 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1020 /* check width/height */
1021 if (mode->hdisplay != cmdline_mode->xres ||
1022 mode->vdisplay != cmdline_mode->yres)
1025 if (cmdline_mode->refresh_specified) {
1026 if (mode->vrefresh != cmdline_mode->refresh)
1030 if (cmdline_mode->interlace) {
1031 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1038 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1040 list_add(&mode->head, &fb_helper_conn->connector->modes);
1044 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1049 enable = connector->status == connector_status_connected;
1051 enable = connector->status != connector_status_disconnected;
1056 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1059 bool any_enabled = false;
1060 struct drm_connector *connector;
1063 for (i = 0; i < fb_helper->connector_count; i++) {
1064 connector = fb_helper->connector_info[i]->connector;
1065 enabled[i] = drm_connector_enabled(connector, true);
1066 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1067 enabled[i] ? "yes" : "no");
1068 any_enabled |= enabled[i];
1074 for (i = 0; i < fb_helper->connector_count; i++) {
1075 connector = fb_helper->connector_info[i]->connector;
1076 enabled[i] = drm_connector_enabled(connector, false);
1080 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1081 struct drm_display_mode **modes,
1082 bool *enabled, int width, int height)
1085 bool can_clone = false;
1086 struct drm_fb_helper_connector *fb_helper_conn;
1087 struct drm_display_mode *dmt_mode, *mode;
1089 /* only contemplate cloning in the single crtc case */
1090 if (fb_helper->crtc_count > 1)
1094 for (i = 0; i < fb_helper->connector_count; i++) {
1099 /* only contemplate cloning if more than one connector is enabled */
1103 /* check the command line or if nothing common pick 1024x768 */
1105 for (i = 0; i < fb_helper->connector_count; i++) {
1108 fb_helper_conn = fb_helper->connector_info[i];
1109 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1114 for (j = 0; j < i; j++) {
1117 if (!drm_mode_equal(modes[j], modes[i]))
1123 DRM_DEBUG_KMS("can clone using command line\n");
1127 /* try and find a 1024x768 mode on each connector */
1129 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1131 for (i = 0; i < fb_helper->connector_count; i++) {
1136 fb_helper_conn = fb_helper->connector_info[i];
1137 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1138 if (drm_mode_equal(mode, dmt_mode))
1146 DRM_DEBUG_KMS("can clone using 1024x768\n");
1149 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1153 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1154 struct drm_display_mode **modes,
1155 bool *enabled, int width, int height)
1157 struct drm_fb_helper_connector *fb_helper_conn;
1160 for (i = 0; i < fb_helper->connector_count; i++) {
1161 fb_helper_conn = fb_helper->connector_info[i];
1163 if (enabled[i] == false)
1166 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1167 fb_helper_conn->connector->base.id);
1169 /* got for command line mode first */
1170 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1172 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1173 fb_helper_conn->connector->base.id);
1174 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1176 /* No preferred modes, pick one off the list */
1177 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1178 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1181 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1187 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1188 struct drm_fb_helper_crtc **best_crtcs,
1189 struct drm_display_mode **modes,
1190 int n, int width, int height)
1193 struct drm_device *dev = fb_helper->dev;
1194 struct drm_connector *connector;
1195 struct drm_connector_helper_funcs *connector_funcs;
1196 struct drm_encoder *encoder;
1197 struct drm_fb_helper_crtc *best_crtc;
1198 int my_score, best_score, score;
1199 struct drm_fb_helper_crtc **crtcs, *crtc;
1200 struct drm_fb_helper_connector *fb_helper_conn;
1202 if (n == fb_helper->connector_count)
1205 fb_helper_conn = fb_helper->connector_info[n];
1206 connector = fb_helper_conn->connector;
1208 best_crtcs[n] = NULL;
1210 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1211 if (modes[n] == NULL)
1214 crtcs = kzalloc(dev->mode_config.num_connector *
1215 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1220 if (connector->status == connector_status_connected)
1222 if (drm_has_cmdline_mode(fb_helper_conn))
1224 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1227 connector_funcs = connector->helper_private;
1228 encoder = connector_funcs->best_encoder(connector);
1232 /* select a crtc for this connector and then attempt to configure
1233 remaining connectors */
1234 for (c = 0; c < fb_helper->crtc_count; c++) {
1235 crtc = &fb_helper->crtc_info[c];
1237 if ((encoder->possible_crtcs & (1 << c)) == 0)
1240 for (o = 0; o < n; o++)
1241 if (best_crtcs[o] == crtc)
1245 /* ignore cloning unless only a single crtc */
1246 if (fb_helper->crtc_count > 1)
1249 if (!drm_mode_equal(modes[o], modes[n]))
1254 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1255 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1257 if (score > best_score) {
1260 memcpy(best_crtcs, crtcs,
1261 dev->mode_config.num_connector *
1262 sizeof(struct drm_fb_helper_crtc *));
1270 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1272 struct drm_device *dev = fb_helper->dev;
1273 struct drm_fb_helper_crtc **crtcs;
1274 struct drm_display_mode **modes;
1275 struct drm_mode_set *modeset;
1280 DRM_DEBUG_KMS("\n");
1282 width = dev->mode_config.max_width;
1283 height = dev->mode_config.max_height;
1285 crtcs = kcalloc(dev->mode_config.num_connector,
1286 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1287 modes = kcalloc(dev->mode_config.num_connector,
1288 sizeof(struct drm_display_mode *), GFP_KERNEL);
1289 enabled = kcalloc(dev->mode_config.num_connector,
1290 sizeof(bool), GFP_KERNEL);
1291 if (!crtcs || !modes || !enabled) {
1292 DRM_ERROR("Memory allocation failed\n");
1297 drm_enable_connectors(fb_helper, enabled);
1299 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1301 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1303 DRM_ERROR("Unable to find initial modes\n");
1306 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1308 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1310 /* need to set the modesets up here for use later */
1311 /* fill out the connector<->crtc mappings into the modesets */
1312 for (i = 0; i < fb_helper->crtc_count; i++) {
1313 modeset = &fb_helper->crtc_info[i].mode_set;
1314 modeset->num_connectors = 0;
1317 for (i = 0; i < fb_helper->connector_count; i++) {
1318 struct drm_display_mode *mode = modes[i];
1319 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1320 modeset = &fb_crtc->mode_set;
1322 if (mode && fb_crtc) {
1323 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1324 mode->name, fb_crtc->mode_set.crtc->base.id);
1325 fb_crtc->desired_mode = mode;
1327 drm_mode_destroy(dev, modeset->mode);
1328 modeset->mode = drm_mode_duplicate(dev,
1329 fb_crtc->desired_mode);
1330 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1341 * drm_helper_initial_config - setup a sane initial connector configuration
1342 * @fb_helper: fb_helper device struct
1343 * @bpp_sel: bpp value to use for the framebuffer configuration
1346 * Called at init time by the driver to set up the @fb_helper initial
1347 * configuration, must take the mode config lock.
1349 * Scans the CRTCs and connectors and tries to put together an initial setup.
1350 * At the moment, this is a cloned configuration across all heads with
1351 * a new framebuffer object as the backing store.
1354 * Zero if everything went ok, nonzero otherwise.
1356 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1358 struct drm_device *dev = fb_helper->dev;
1361 /* disable all the possible outputs/crtcs before entering KMS mode */
1362 drm_helper_disable_unused_functions(fb_helper->dev);
1364 drm_fb_helper_parse_command_line(fb_helper);
1366 count = drm_fb_helper_probe_connector_modes(fb_helper,
1367 dev->mode_config.max_width,
1368 dev->mode_config.max_height);
1370 * we shouldn't end up with no modes here.
1373 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
1375 drm_setup_crtcs(fb_helper);
1377 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1379 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1382 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1383 * probing all the outputs attached to the fb
1384 * @fb_helper: the drm_fb_helper
1387 * Called at runtime, must take mode config lock.
1389 * Scan the connectors attached to the fb_helper and try to put together a
1390 * setup after *notification of a change in output configuration.
1393 * 0 on success and a non-zero error code otherwise.
1395 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1397 struct drm_device *dev = fb_helper->dev;
1399 u32 max_width, max_height, bpp_sel;
1404 drm_modeset_lock_all(dev);
1405 if (!drm_fb_helper_is_bound(fb_helper)) {
1406 fb_helper->delayed_hotplug = true;
1407 drm_modeset_unlock_all(dev);
1410 DRM_DEBUG_KMS("\n");
1412 max_width = fb_helper->fb->width;
1413 max_height = fb_helper->fb->height;
1414 bpp_sel = fb_helper->fb->bits_per_pixel;
1416 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1418 drm_setup_crtcs(fb_helper);
1419 drm_modeset_unlock_all(dev);
1421 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1423 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
1425 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
1426 * but the module doesn't depend on any fb console symbols. At least
1427 * attempt to load fbcon to avoid leaving the system without a usable console.
1429 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
1430 static int __init drm_fb_helper_modinit(void)
1432 const char *name = "fbcon";
1433 struct module *fbcon;
1435 mutex_lock(&module_mutex);
1436 fbcon = find_module(name);
1437 mutex_unlock(&module_mutex);
1440 request_module_nowait(name);
1444 module_init(drm_fb_helper_modinit);