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
56 * Initialization is done as a three-step process with drm_fb_helper_init(),
57 * drm_fb_helper_single_add_all_connectors() and drm_fb_helper_initial_config().
58 * Drivers with fancier requirements than the default beheviour can override the
59 * second step with their own code. Teardown is done with drm_fb_helper_fini().
61 * At runtime drivers should restore the fbdev console by calling
62 * drm_fb_helper_restore_fbdev_mode() from their ->lastclose callback. They
63 * should also notify the fb helper code from updates to the output
64 * configuration by calling drm_fb_helper_hotplug_event(). For easier
65 * integration with the output polling code in drm_crtc_helper.c the modeset
66 * code proves a ->output_poll_changed callback.
68 * All other functions exported by the fb helper library can be used to
69 * implement the fbdev driver interface by the driver.
73 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
75 * @fb_helper: fbdev initialized with drm_fb_helper_init
77 * This functions adds all the available connectors for use with the given
78 * fb_helper. This is a separate step to allow drivers to freely assign
79 * connectors to the fbdev, e.g. if some are reserved for special purposes or
80 * not adequate to be used for the fbcon.
82 * Since this is part of the initial setup before the fbdev is published, no
83 * locking is required.
85 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
87 struct drm_device *dev = fb_helper->dev;
88 struct drm_connector *connector;
91 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
92 struct drm_fb_helper_connector *fb_helper_connector;
94 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
95 if (!fb_helper_connector)
98 fb_helper_connector->connector = connector;
99 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
103 for (i = 0; i < fb_helper->connector_count; i++) {
104 kfree(fb_helper->connector_info[i]);
105 fb_helper->connector_info[i] = NULL;
107 fb_helper->connector_count = 0;
110 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
112 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
114 struct drm_fb_helper_connector *fb_helper_conn;
117 for (i = 0; i < fb_helper->connector_count; i++) {
118 struct drm_cmdline_mode *mode;
119 struct drm_connector *connector;
122 fb_helper_conn = fb_helper->connector_info[i];
123 connector = fb_helper_conn->connector;
124 mode = &fb_helper_conn->cmdline_mode;
126 /* do something on return - turn off connector maybe */
127 if (fb_get_options(drm_get_connector_name(connector), &option))
130 if (drm_mode_parse_command_line_for_connector(option,
135 switch (mode->force) {
139 case DRM_FORCE_ON_DIGITAL:
148 DRM_INFO("forcing %s connector %s\n",
149 drm_get_connector_name(connector), s);
150 connector->force = mode->force;
153 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
154 drm_get_connector_name(connector),
155 mode->xres, mode->yres,
156 mode->refresh_specified ? mode->refresh : 60,
157 mode->rb ? " reduced blanking" : "",
158 mode->margins ? " with margins" : "",
159 mode->interlace ? " interlaced" : "");
166 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
168 uint16_t *r_base, *g_base, *b_base;
171 r_base = crtc->gamma_store;
172 g_base = r_base + crtc->gamma_size;
173 b_base = g_base + crtc->gamma_size;
175 for (i = 0; i < crtc->gamma_size; i++)
176 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
179 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
181 uint16_t *r_base, *g_base, *b_base;
183 if (crtc->funcs->gamma_set == NULL)
186 r_base = crtc->gamma_store;
187 g_base = r_base + crtc->gamma_size;
188 b_base = g_base + crtc->gamma_size;
190 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
194 * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
195 * @info: fbdev registered by the helper
197 int drm_fb_helper_debug_enter(struct fb_info *info)
199 struct drm_fb_helper *helper = info->par;
200 struct drm_crtc_helper_funcs *funcs;
203 if (list_empty(&kernel_fb_helper_list))
206 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
207 for (i = 0; i < helper->crtc_count; i++) {
208 struct drm_mode_set *mode_set =
209 &helper->crtc_info[i].mode_set;
211 if (!mode_set->crtc->enabled)
214 funcs = mode_set->crtc->helper_private;
215 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
216 funcs->mode_set_base_atomic(mode_set->crtc,
220 ENTER_ATOMIC_MODE_SET);
226 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
228 /* Find the real fb for a given fb helper CRTC */
229 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
231 struct drm_device *dev = crtc->dev;
234 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
235 if (crtc->base.id == c->base.id)
243 * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
244 * @info: fbdev registered by the helper
246 int drm_fb_helper_debug_leave(struct fb_info *info)
248 struct drm_fb_helper *helper = info->par;
249 struct drm_crtc *crtc;
250 struct drm_crtc_helper_funcs *funcs;
251 struct drm_framebuffer *fb;
254 for (i = 0; i < helper->crtc_count; i++) {
255 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
256 crtc = mode_set->crtc;
257 funcs = crtc->helper_private;
258 fb = drm_mode_config_fb(crtc);
264 DRM_ERROR("no fb to restore??\n");
268 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
269 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
270 crtc->y, LEAVE_ATOMIC_MODE_SET);
275 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
278 * drm_fb_helper_restore_fbdev_mode - restore fbdev configuration
279 * @fb_helper: fbcon to restore
281 * This should be called from driver's drm ->lastclose callback
282 * when implementing an fbcon on top of kms using this helper. This ensures that
283 * the user isn't greeted with a black screen when e.g. X dies.
285 bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
290 drm_warn_on_modeset_not_all_locked(fb_helper->dev);
292 for (i = 0; i < fb_helper->crtc_count; i++) {
293 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
294 ret = drm_mode_set_config_internal(mode_set);
300 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode);
303 * restore fbcon display for all kms driver's using this helper, used for sysrq
304 * and panic handling.
306 static bool drm_fb_helper_force_kernel_mode(void)
308 bool ret, error = false;
309 struct drm_fb_helper *helper;
311 if (list_empty(&kernel_fb_helper_list))
314 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
315 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
318 ret = drm_fb_helper_restore_fbdev_mode(helper);
325 static int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
329 * It's a waste of time and effort to switch back to text console
330 * if the kernel should reboot before panic messages can be seen.
332 if (panic_timeout < 0)
335 pr_err("panic occurred, switching back to text console\n");
336 return drm_fb_helper_force_kernel_mode();
339 static struct notifier_block paniced = {
340 .notifier_call = drm_fb_helper_panic,
343 static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
345 struct drm_device *dev = fb_helper->dev;
346 struct drm_crtc *crtc;
347 int bound = 0, crtcs_bound = 0;
349 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
352 if (crtc->fb == fb_helper->fb)
356 if (bound < crtcs_bound)
361 #ifdef CONFIG_MAGIC_SYSRQ
362 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
365 ret = drm_fb_helper_force_kernel_mode();
367 DRM_ERROR("Failed to restore crtc configuration\n");
369 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
371 static void drm_fb_helper_sysrq(int dummy1)
373 schedule_work(&drm_fb_helper_restore_work);
376 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
377 .handler = drm_fb_helper_sysrq,
378 .help_msg = "force-fb(V)",
379 .action_msg = "Restore framebuffer console",
382 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
385 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
387 struct drm_fb_helper *fb_helper = info->par;
388 struct drm_device *dev = fb_helper->dev;
389 struct drm_crtc *crtc;
390 struct drm_connector *connector;
394 * fbdev->blank can be called from irq context in case of a panic.
395 * Since we already have our own special panic handler which will
396 * restore the fbdev console mode completely, just bail out early.
398 if (oops_in_progress)
402 * fbdev->blank can be called from irq context in case of a panic.
403 * Since we already have our own special panic handler which will
404 * restore the fbdev console mode completely, just bail out early.
406 if (oops_in_progress)
410 * For each CRTC in this fb, turn the connectors on/off.
412 drm_modeset_lock_all(dev);
413 if (!drm_fb_helper_is_bound(fb_helper)) {
414 drm_modeset_unlock_all(dev);
418 for (i = 0; i < fb_helper->crtc_count; i++) {
419 crtc = fb_helper->crtc_info[i].mode_set.crtc;
424 /* Walk the connectors & encoders on this fb turning them on/off */
425 for (j = 0; j < fb_helper->connector_count; j++) {
426 connector = fb_helper->connector_info[j]->connector;
427 connector->funcs->dpms(connector, dpms_mode);
428 drm_object_property_set_value(&connector->base,
429 dev->mode_config.dpms_property, dpms_mode);
432 drm_modeset_unlock_all(dev);
436 * drm_fb_helper_blank - implementation for ->fb_blank
437 * @blank: desired blanking state
438 * @info: fbdev registered by the helper
440 int drm_fb_helper_blank(int blank, struct fb_info *info)
443 /* Display: On; HSync: On, VSync: On */
444 case FB_BLANK_UNBLANK:
445 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
447 /* Display: Off; HSync: On, VSync: On */
448 case FB_BLANK_NORMAL:
449 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
451 /* Display: Off; HSync: Off, VSync: On */
452 case FB_BLANK_HSYNC_SUSPEND:
453 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
455 /* Display: Off; HSync: On, VSync: Off */
456 case FB_BLANK_VSYNC_SUSPEND:
457 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
459 /* Display: Off; HSync: Off, VSync: Off */
460 case FB_BLANK_POWERDOWN:
461 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
466 EXPORT_SYMBOL(drm_fb_helper_blank);
468 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
472 for (i = 0; i < helper->connector_count; i++)
473 kfree(helper->connector_info[i]);
474 kfree(helper->connector_info);
475 for (i = 0; i < helper->crtc_count; i++) {
476 kfree(helper->crtc_info[i].mode_set.connectors);
477 if (helper->crtc_info[i].mode_set.mode)
478 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
480 kfree(helper->crtc_info);
484 * drm_fb_helper_init - initialize a drm_fb_helper structure
486 * @fb_helper: driver-allocated fbdev helper structure to initialize
487 * @crtc_count: maximum number of crtcs to support in this fbdev emulation
488 * @max_conn_count: max connector count
490 * This allocates the structures for the fbdev helper with the given limits.
491 * Note that this won't yet touch the hardware (through the driver interfaces)
492 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
493 * to allow driver writes more control over the exact init sequence.
495 * Drivers must set fb_helper->funcs before calling
496 * drm_fb_helper_initial_config().
499 * Zero if everything went ok, nonzero otherwise.
501 int drm_fb_helper_init(struct drm_device *dev,
502 struct drm_fb_helper *fb_helper,
503 int crtc_count, int max_conn_count)
505 struct drm_crtc *crtc;
508 fb_helper->dev = dev;
510 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
512 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
513 if (!fb_helper->crtc_info)
516 fb_helper->crtc_count = crtc_count;
517 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
518 if (!fb_helper->connector_info) {
519 kfree(fb_helper->crtc_info);
522 fb_helper->connector_count = 0;
524 for (i = 0; i < crtc_count; i++) {
525 fb_helper->crtc_info[i].mode_set.connectors =
526 kcalloc(max_conn_count,
527 sizeof(struct drm_connector *),
530 if (!fb_helper->crtc_info[i].mode_set.connectors)
532 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
536 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
537 fb_helper->crtc_info[i].mode_set.crtc = crtc;
543 drm_fb_helper_crtc_free(fb_helper);
546 EXPORT_SYMBOL(drm_fb_helper_init);
548 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
550 if (!list_empty(&fb_helper->kernel_fb_list)) {
551 list_del(&fb_helper->kernel_fb_list);
552 if (list_empty(&kernel_fb_helper_list)) {
553 pr_info("drm: unregistered panic notifier\n");
554 atomic_notifier_chain_unregister(&panic_notifier_list,
556 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
560 drm_fb_helper_crtc_free(fb_helper);
563 EXPORT_SYMBOL(drm_fb_helper_fini);
565 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
566 u16 blue, u16 regno, struct fb_info *info)
568 struct drm_fb_helper *fb_helper = info->par;
569 struct drm_framebuffer *fb = fb_helper->fb;
572 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
575 /* place color in psuedopalette */
578 palette = (u32 *)info->pseudo_palette;
579 red >>= (16 - info->var.red.length);
580 green >>= (16 - info->var.green.length);
581 blue >>= (16 - info->var.blue.length);
582 value = (red << info->var.red.offset) |
583 (green << info->var.green.offset) |
584 (blue << info->var.blue.offset);
585 if (info->var.transp.length > 0) {
586 u32 mask = (1 << info->var.transp.length) - 1;
587 mask <<= info->var.transp.offset;
590 palette[regno] = value;
596 if (fb->bits_per_pixel == 16) {
599 if (fb->depth == 16 && regno > 63)
601 if (fb->depth == 15 && regno > 31)
604 if (fb->depth == 16) {
608 for (i = 0; i < 8; i++)
609 fb_helper->funcs->gamma_set(crtc, red,
610 green, blue, pindex + i);
613 fb_helper->funcs->gamma_get(crtc, &r,
617 for (i = 0; i < 4; i++)
618 fb_helper->funcs->gamma_set(crtc, r,
625 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
630 * drm_fb_helper_setcmap - implementation for ->fb_setcmap
632 * @info: fbdev registered by the helper
634 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
636 struct drm_fb_helper *fb_helper = info->par;
637 struct drm_crtc_helper_funcs *crtc_funcs;
638 u16 *red, *green, *blue, *transp;
639 struct drm_crtc *crtc;
643 for (i = 0; i < fb_helper->crtc_count; i++) {
644 crtc = fb_helper->crtc_info[i].mode_set.crtc;
645 crtc_funcs = crtc->helper_private;
650 transp = cmap->transp;
653 for (j = 0; j < cmap->len; j++) {
654 u16 hred, hgreen, hblue, htransp = 0xffff;
663 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
667 crtc_funcs->load_lut(crtc);
671 EXPORT_SYMBOL(drm_fb_helper_setcmap);
674 * drm_fb_helper_check_var - implementation for ->fb_check_var
675 * @var: screeninfo to check
676 * @info: fbdev registered by the helper
678 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
679 struct fb_info *info)
681 struct drm_fb_helper *fb_helper = info->par;
682 struct drm_framebuffer *fb = fb_helper->fb;
685 if (var->pixclock != 0 || in_dbg_master())
688 /* Need to resize the fb object !!! */
689 if (var->bits_per_pixel > fb->bits_per_pixel ||
690 var->xres > fb->width || var->yres > fb->height ||
691 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
692 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
693 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
694 var->xres, var->yres, var->bits_per_pixel,
695 var->xres_virtual, var->yres_virtual,
696 fb->width, fb->height, fb->bits_per_pixel);
700 switch (var->bits_per_pixel) {
702 depth = (var->green.length == 6) ? 16 : 15;
705 depth = (var->transp.length > 0) ? 32 : 24;
708 depth = var->bits_per_pixel;
715 var->green.offset = 0;
716 var->blue.offset = 0;
718 var->green.length = 8;
719 var->blue.length = 8;
720 var->transp.length = 0;
721 var->transp.offset = 0;
724 var->red.offset = 10;
725 var->green.offset = 5;
726 var->blue.offset = 0;
728 var->green.length = 5;
729 var->blue.length = 5;
730 var->transp.length = 1;
731 var->transp.offset = 15;
734 var->red.offset = 11;
735 var->green.offset = 5;
736 var->blue.offset = 0;
738 var->green.length = 6;
739 var->blue.length = 5;
740 var->transp.length = 0;
741 var->transp.offset = 0;
744 var->red.offset = 16;
745 var->green.offset = 8;
746 var->blue.offset = 0;
748 var->green.length = 8;
749 var->blue.length = 8;
750 var->transp.length = 0;
751 var->transp.offset = 0;
754 var->red.offset = 16;
755 var->green.offset = 8;
756 var->blue.offset = 0;
758 var->green.length = 8;
759 var->blue.length = 8;
760 var->transp.length = 8;
761 var->transp.offset = 24;
768 EXPORT_SYMBOL(drm_fb_helper_check_var);
771 * drm_fb_helper_set_par - implementation for ->fb_set_par
772 * @info: fbdev registered by the helper
774 * This will let fbcon do the mode init and is called at initialization time by
775 * the fbdev core when registering the driver, and later on through the hotplug
778 int drm_fb_helper_set_par(struct fb_info *info)
780 struct drm_fb_helper *fb_helper = info->par;
781 struct drm_device *dev = fb_helper->dev;
782 struct fb_var_screeninfo *var = &info->var;
786 if (var->pixclock != 0) {
787 DRM_ERROR("PIXEL CLOCK SET\n");
791 drm_modeset_lock_all(dev);
792 for (i = 0; i < fb_helper->crtc_count; i++) {
793 ret = drm_mode_set_config_internal(&fb_helper->crtc_info[i].mode_set);
795 drm_modeset_unlock_all(dev);
799 drm_modeset_unlock_all(dev);
801 if (fb_helper->delayed_hotplug) {
802 fb_helper->delayed_hotplug = false;
803 drm_fb_helper_hotplug_event(fb_helper);
807 EXPORT_SYMBOL(drm_fb_helper_set_par);
810 * drm_fb_helper_pan_display - implementation for ->fb_pan_display
811 * @var: updated screen information
812 * @info: fbdev registered by the helper
814 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
815 struct fb_info *info)
817 struct drm_fb_helper *fb_helper = info->par;
818 struct drm_device *dev = fb_helper->dev;
819 struct drm_mode_set *modeset;
820 struct drm_crtc *crtc;
824 drm_modeset_lock_all(dev);
825 if (!drm_fb_helper_is_bound(fb_helper)) {
826 drm_modeset_unlock_all(dev);
830 for (i = 0; i < fb_helper->crtc_count; i++) {
831 crtc = fb_helper->crtc_info[i].mode_set.crtc;
833 modeset = &fb_helper->crtc_info[i].mode_set;
835 modeset->x = var->xoffset;
836 modeset->y = var->yoffset;
838 if (modeset->num_connectors) {
839 ret = drm_mode_set_config_internal(modeset);
841 info->var.xoffset = var->xoffset;
842 info->var.yoffset = var->yoffset;
846 drm_modeset_unlock_all(dev);
849 EXPORT_SYMBOL(drm_fb_helper_pan_display);
852 * Allocates the backing storage and sets up the fbdev info structure through
853 * the ->fb_probe callback and then registers the fbdev and sets up the panic
856 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
862 struct fb_info *info;
863 struct drm_fb_helper_surface_size sizes;
866 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
867 sizes.surface_depth = 24;
868 sizes.surface_bpp = 32;
869 sizes.fb_width = (unsigned)-1;
870 sizes.fb_height = (unsigned)-1;
872 /* if driver picks 8 or 16 by default use that
873 for both depth/bpp */
874 if (preferred_bpp != sizes.surface_bpp)
875 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
877 /* first up get a count of crtcs now in use and new min/maxes width/heights */
878 for (i = 0; i < fb_helper->connector_count; i++) {
879 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
880 struct drm_cmdline_mode *cmdline_mode;
882 cmdline_mode = &fb_helper_conn->cmdline_mode;
884 if (cmdline_mode->bpp_specified) {
885 switch (cmdline_mode->bpp) {
887 sizes.surface_depth = sizes.surface_bpp = 8;
890 sizes.surface_depth = 15;
891 sizes.surface_bpp = 16;
894 sizes.surface_depth = sizes.surface_bpp = 16;
897 sizes.surface_depth = sizes.surface_bpp = 24;
900 sizes.surface_depth = 24;
901 sizes.surface_bpp = 32;
909 for (i = 0; i < fb_helper->crtc_count; i++) {
910 struct drm_display_mode *desired_mode;
911 desired_mode = fb_helper->crtc_info[i].desired_mode;
915 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
916 if (desired_mode->hdisplay < sizes.fb_width)
917 sizes.fb_width = desired_mode->hdisplay;
918 if (desired_mode->vdisplay < sizes.fb_height)
919 sizes.fb_height = desired_mode->vdisplay;
920 if (desired_mode->hdisplay > sizes.surface_width)
921 sizes.surface_width = desired_mode->hdisplay;
922 if (desired_mode->vdisplay > sizes.surface_height)
923 sizes.surface_height = desired_mode->vdisplay;
928 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
929 /* hmm everyone went away - assume VGA cable just fell out
930 and will come back later. */
931 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
932 sizes.fb_width = sizes.surface_width = 1024;
933 sizes.fb_height = sizes.surface_height = 768;
936 /* push down into drivers */
937 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
941 info = fb_helper->fbdev;
944 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
945 * events, but at init time drm_setup_crtcs needs to be called before
946 * the fb is allocated (since we need to figure out the desired size of
947 * the fb before we can allocate it ...). Hence we need to fix things up
950 for (i = 0; i < fb_helper->crtc_count; i++)
951 if (fb_helper->crtc_info[i].mode_set.num_connectors)
952 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
955 info->var.pixclock = 0;
956 if (register_framebuffer(info) < 0)
959 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
960 info->node, info->fix.id);
962 /* Switch back to kernel console on panic */
963 /* multi card linked list maybe */
964 if (list_empty(&kernel_fb_helper_list)) {
965 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
966 atomic_notifier_chain_register(&panic_notifier_list,
968 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
971 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
977 * drm_fb_helper_fill_fix - initializes fixed fbdev information
978 * @info: fbdev registered by the helper
979 * @pitch: desired pitch
980 * @depth: desired depth
982 * Helper to fill in the fixed fbdev information useful for a non-accelerated
983 * fbdev emulations. Drivers which support acceleration methods which impose
984 * additional constraints need to set up their own limits.
986 * Drivers should call this (or their equivalent setup code) from their
987 * ->fb_probe callback.
989 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
992 info->fix.type = FB_TYPE_PACKED_PIXELS;
993 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
995 info->fix.mmio_start = 0;
996 info->fix.mmio_len = 0;
997 info->fix.type_aux = 0;
998 info->fix.xpanstep = 1; /* doing it in hw */
999 info->fix.ypanstep = 1; /* doing it in hw */
1000 info->fix.ywrapstep = 0;
1001 info->fix.accel = FB_ACCEL_NONE;
1002 info->fix.type_aux = 0;
1004 info->fix.line_length = pitch;
1007 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1010 * drm_fb_helper_fill_var - initalizes variable fbdev information
1011 * @info: fbdev instance to set up
1012 * @fb_helper: fb helper instance to use as template
1013 * @fb_width: desired fb width
1014 * @fb_height: desired fb height
1016 * Sets up the variable fbdev metainformation from the given fb helper instance
1017 * and the drm framebuffer allocated in fb_helper->fb.
1019 * Drivers should call this (or their equivalent setup code) from their
1020 * ->fb_probe callback after having allocated the fbdev backing
1021 * storage framebuffer.
1023 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1024 uint32_t fb_width, uint32_t fb_height)
1026 struct drm_framebuffer *fb = fb_helper->fb;
1027 info->pseudo_palette = fb_helper->pseudo_palette;
1028 info->var.xres_virtual = fb->width;
1029 info->var.yres_virtual = fb->height;
1030 info->var.bits_per_pixel = fb->bits_per_pixel;
1031 info->var.accel_flags = FB_ACCELF_TEXT;
1032 info->var.xoffset = 0;
1033 info->var.yoffset = 0;
1034 info->var.activate = FB_ACTIVATE_NOW;
1035 info->var.height = -1;
1036 info->var.width = -1;
1038 switch (fb->depth) {
1040 info->var.red.offset = 0;
1041 info->var.green.offset = 0;
1042 info->var.blue.offset = 0;
1043 info->var.red.length = 8; /* 8bit DAC */
1044 info->var.green.length = 8;
1045 info->var.blue.length = 8;
1046 info->var.transp.offset = 0;
1047 info->var.transp.length = 0;
1050 info->var.red.offset = 10;
1051 info->var.green.offset = 5;
1052 info->var.blue.offset = 0;
1053 info->var.red.length = 5;
1054 info->var.green.length = 5;
1055 info->var.blue.length = 5;
1056 info->var.transp.offset = 15;
1057 info->var.transp.length = 1;
1060 info->var.red.offset = 11;
1061 info->var.green.offset = 5;
1062 info->var.blue.offset = 0;
1063 info->var.red.length = 5;
1064 info->var.green.length = 6;
1065 info->var.blue.length = 5;
1066 info->var.transp.offset = 0;
1069 info->var.red.offset = 16;
1070 info->var.green.offset = 8;
1071 info->var.blue.offset = 0;
1072 info->var.red.length = 8;
1073 info->var.green.length = 8;
1074 info->var.blue.length = 8;
1075 info->var.transp.offset = 0;
1076 info->var.transp.length = 0;
1079 info->var.red.offset = 16;
1080 info->var.green.offset = 8;
1081 info->var.blue.offset = 0;
1082 info->var.red.length = 8;
1083 info->var.green.length = 8;
1084 info->var.blue.length = 8;
1085 info->var.transp.offset = 24;
1086 info->var.transp.length = 8;
1092 info->var.xres = fb_width;
1093 info->var.yres = fb_height;
1095 EXPORT_SYMBOL(drm_fb_helper_fill_var);
1097 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1101 struct drm_connector *connector;
1105 for (i = 0; i < fb_helper->connector_count; i++) {
1106 connector = fb_helper->connector_info[i]->connector;
1107 count += connector->funcs->fill_modes(connector, maxX, maxY);
1113 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1115 struct drm_display_mode *mode;
1117 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1118 if (drm_mode_width(mode) > width ||
1119 drm_mode_height(mode) > height)
1121 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1127 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1129 struct drm_cmdline_mode *cmdline_mode;
1130 cmdline_mode = &fb_connector->cmdline_mode;
1131 return cmdline_mode->specified;
1134 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1135 int width, int height)
1137 struct drm_cmdline_mode *cmdline_mode;
1138 struct drm_display_mode *mode = NULL;
1140 cmdline_mode = &fb_helper_conn->cmdline_mode;
1141 if (cmdline_mode->specified == false)
1144 /* attempt to find a matching mode in the list of modes
1145 * we have gotten so far, if not add a CVT mode that conforms
1147 if (cmdline_mode->rb || cmdline_mode->margins)
1150 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1151 /* check width/height */
1152 if (mode->hdisplay != cmdline_mode->xres ||
1153 mode->vdisplay != cmdline_mode->yres)
1156 if (cmdline_mode->refresh_specified) {
1157 if (mode->vrefresh != cmdline_mode->refresh)
1161 if (cmdline_mode->interlace) {
1162 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1169 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1171 list_add(&mode->head, &fb_helper_conn->connector->modes);
1175 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1180 enable = connector->status == connector_status_connected;
1182 enable = connector->status != connector_status_disconnected;
1187 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1190 bool any_enabled = false;
1191 struct drm_connector *connector;
1194 for (i = 0; i < fb_helper->connector_count; i++) {
1195 connector = fb_helper->connector_info[i]->connector;
1196 enabled[i] = drm_connector_enabled(connector, true);
1197 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1198 enabled[i] ? "yes" : "no");
1199 any_enabled |= enabled[i];
1205 for (i = 0; i < fb_helper->connector_count; i++) {
1206 connector = fb_helper->connector_info[i]->connector;
1207 enabled[i] = drm_connector_enabled(connector, false);
1211 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1212 struct drm_display_mode **modes,
1213 bool *enabled, int width, int height)
1216 bool can_clone = false;
1217 struct drm_fb_helper_connector *fb_helper_conn;
1218 struct drm_display_mode *dmt_mode, *mode;
1220 /* only contemplate cloning in the single crtc case */
1221 if (fb_helper->crtc_count > 1)
1225 for (i = 0; i < fb_helper->connector_count; i++) {
1230 /* only contemplate cloning if more than one connector is enabled */
1234 /* check the command line or if nothing common pick 1024x768 */
1236 for (i = 0; i < fb_helper->connector_count; i++) {
1239 fb_helper_conn = fb_helper->connector_info[i];
1240 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1245 for (j = 0; j < i; j++) {
1248 if (!drm_mode_equal(modes[j], modes[i]))
1254 DRM_DEBUG_KMS("can clone using command line\n");
1258 /* try and find a 1024x768 mode on each connector */
1260 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1262 for (i = 0; i < fb_helper->connector_count; i++) {
1267 fb_helper_conn = fb_helper->connector_info[i];
1268 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1269 if (drm_mode_equal(mode, dmt_mode))
1277 DRM_DEBUG_KMS("can clone using 1024x768\n");
1280 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1284 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1285 struct drm_display_mode **modes,
1286 bool *enabled, int width, int height)
1288 struct drm_fb_helper_connector *fb_helper_conn;
1291 for (i = 0; i < fb_helper->connector_count; i++) {
1292 fb_helper_conn = fb_helper->connector_info[i];
1294 if (enabled[i] == false)
1297 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1298 fb_helper_conn->connector->base.id);
1300 /* got for command line mode first */
1301 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1303 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1304 fb_helper_conn->connector->base.id);
1305 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1307 /* No preferred modes, pick one off the list */
1308 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1309 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1312 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1318 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1319 struct drm_fb_helper_crtc **best_crtcs,
1320 struct drm_display_mode **modes,
1321 int n, int width, int height)
1324 struct drm_device *dev = fb_helper->dev;
1325 struct drm_connector *connector;
1326 struct drm_connector_helper_funcs *connector_funcs;
1327 struct drm_encoder *encoder;
1328 struct drm_fb_helper_crtc *best_crtc;
1329 int my_score, best_score, score;
1330 struct drm_fb_helper_crtc **crtcs, *crtc;
1331 struct drm_fb_helper_connector *fb_helper_conn;
1333 if (n == fb_helper->connector_count)
1336 fb_helper_conn = fb_helper->connector_info[n];
1337 connector = fb_helper_conn->connector;
1339 best_crtcs[n] = NULL;
1341 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1342 if (modes[n] == NULL)
1345 crtcs = kzalloc(dev->mode_config.num_connector *
1346 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1351 if (connector->status == connector_status_connected)
1353 if (drm_has_cmdline_mode(fb_helper_conn))
1355 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1358 connector_funcs = connector->helper_private;
1359 encoder = connector_funcs->best_encoder(connector);
1363 /* select a crtc for this connector and then attempt to configure
1364 remaining connectors */
1365 for (c = 0; c < fb_helper->crtc_count; c++) {
1366 crtc = &fb_helper->crtc_info[c];
1368 if ((encoder->possible_crtcs & (1 << c)) == 0)
1371 for (o = 0; o < n; o++)
1372 if (best_crtcs[o] == crtc)
1376 /* ignore cloning unless only a single crtc */
1377 if (fb_helper->crtc_count > 1)
1380 if (!drm_mode_equal(modes[o], modes[n]))
1385 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1386 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1388 if (score > best_score) {
1391 memcpy(best_crtcs, crtcs,
1392 dev->mode_config.num_connector *
1393 sizeof(struct drm_fb_helper_crtc *));
1401 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1403 struct drm_device *dev = fb_helper->dev;
1404 struct drm_fb_helper_crtc **crtcs;
1405 struct drm_display_mode **modes;
1406 struct drm_mode_set *modeset;
1411 DRM_DEBUG_KMS("\n");
1413 width = dev->mode_config.max_width;
1414 height = dev->mode_config.max_height;
1416 crtcs = kcalloc(dev->mode_config.num_connector,
1417 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1418 modes = kcalloc(dev->mode_config.num_connector,
1419 sizeof(struct drm_display_mode *), GFP_KERNEL);
1420 enabled = kcalloc(dev->mode_config.num_connector,
1421 sizeof(bool), GFP_KERNEL);
1422 if (!crtcs || !modes || !enabled) {
1423 DRM_ERROR("Memory allocation failed\n");
1428 drm_enable_connectors(fb_helper, enabled);
1430 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1432 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1434 DRM_ERROR("Unable to find initial modes\n");
1437 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1439 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1441 /* need to set the modesets up here for use later */
1442 /* fill out the connector<->crtc mappings into the modesets */
1443 for (i = 0; i < fb_helper->crtc_count; i++) {
1444 modeset = &fb_helper->crtc_info[i].mode_set;
1445 modeset->num_connectors = 0;
1449 for (i = 0; i < fb_helper->connector_count; i++) {
1450 struct drm_display_mode *mode = modes[i];
1451 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1452 modeset = &fb_crtc->mode_set;
1454 if (mode && fb_crtc) {
1455 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1456 mode->name, fb_crtc->mode_set.crtc->base.id);
1457 fb_crtc->desired_mode = mode;
1459 drm_mode_destroy(dev, modeset->mode);
1460 modeset->mode = drm_mode_duplicate(dev,
1461 fb_crtc->desired_mode);
1462 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1463 modeset->fb = fb_helper->fb;
1467 /* Clear out any old modes if there are no more connected outputs. */
1468 for (i = 0; i < fb_helper->crtc_count; i++) {
1469 modeset = &fb_helper->crtc_info[i].mode_set;
1470 if (modeset->num_connectors == 0) {
1471 BUG_ON(modeset->fb);
1472 BUG_ON(modeset->num_connectors);
1474 drm_mode_destroy(dev, modeset->mode);
1475 modeset->mode = NULL;
1485 * drm_fb_helper_initial_config - setup a sane initial connector configuration
1486 * @fb_helper: fb_helper device struct
1487 * @bpp_sel: bpp value to use for the framebuffer configuration
1489 * Scans the CRTCs and connectors and tries to put together an initial setup.
1490 * At the moment, this is a cloned configuration across all heads with
1491 * a new framebuffer object as the backing store.
1493 * Note that this also registers the fbdev and so allows userspace to call into
1494 * the driver through the fbdev interfaces.
1496 * This function will call down into the ->fb_probe callback to let
1497 * the driver allocate and initialize the fbdev info structure and the drm
1498 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1499 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1500 * values for the fbdev info structure.
1503 * Zero if everything went ok, nonzero otherwise.
1505 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1507 struct drm_device *dev = fb_helper->dev;
1510 drm_fb_helper_parse_command_line(fb_helper);
1512 count = drm_fb_helper_probe_connector_modes(fb_helper,
1513 dev->mode_config.max_width,
1514 dev->mode_config.max_height);
1516 * we shouldn't end up with no modes here.
1519 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
1521 drm_setup_crtcs(fb_helper);
1523 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1525 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1528 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1529 * probing all the outputs attached to the fb
1530 * @fb_helper: the drm_fb_helper
1532 * Scan the connectors attached to the fb_helper and try to put together a
1533 * setup after *notification of a change in output configuration.
1535 * Called at runtime, takes the mode config locks to be able to check/change the
1536 * modeset configuration. Must be run from process context (which usually means
1537 * either the output polling work or a work item launched from the driver's
1538 * hotplug interrupt).
1540 * Note that the driver must ensure that this is only called _after_ the fb has
1541 * been fully set up, i.e. after the call to drm_fb_helper_initial_config.
1544 * 0 on success and a non-zero error code otherwise.
1546 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1548 struct drm_device *dev = fb_helper->dev;
1550 u32 max_width, max_height, bpp_sel;
1555 drm_modeset_lock_all(dev);
1556 if (!drm_fb_helper_is_bound(fb_helper)) {
1557 fb_helper->delayed_hotplug = true;
1558 drm_modeset_unlock_all(dev);
1561 DRM_DEBUG_KMS("\n");
1563 max_width = fb_helper->fb->width;
1564 max_height = fb_helper->fb->height;
1565 bpp_sel = fb_helper->fb->bits_per_pixel;
1567 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1569 drm_setup_crtcs(fb_helper);
1570 drm_modeset_unlock_all(dev);
1572 drm_fb_helper_set_par(fb_helper->fbdev);
1576 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
1578 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
1579 * but the module doesn't depend on any fb console symbols. At least
1580 * attempt to load fbcon to avoid leaving the system without a usable console.
1582 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
1583 static int __init drm_fb_helper_modinit(void)
1585 const char *name = "fbcon";
1586 struct module *fbcon;
1588 mutex_lock(&module_mutex);
1589 fbcon = find_module(name);
1590 mutex_unlock(&module_mutex);
1593 request_module_nowait(name);
1597 module_init(drm_fb_helper_modinit);