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 #include <linux/kernel.h>
31 #include <linux/sysrq.h>
32 #include <linux/slab.h>
34 #include <linux/module.h>
37 #include "drm_fb_helper.h"
38 #include "drm_crtc_helper.h"
40 MODULE_AUTHOR("David Airlie, Jesse Barnes");
41 MODULE_DESCRIPTION("DRM KMS helper");
42 MODULE_LICENSE("GPL and additional rights");
44 static LIST_HEAD(kernel_fb_helper_list);
46 /* simple single crtc case helper function */
47 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
49 struct drm_device *dev = fb_helper->dev;
50 struct drm_connector *connector;
53 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
54 struct drm_fb_helper_connector *fb_helper_connector;
56 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
57 if (!fb_helper_connector)
60 fb_helper_connector->connector = connector;
61 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
65 for (i = 0; i < fb_helper->connector_count; i++) {
66 kfree(fb_helper->connector_info[i]);
67 fb_helper->connector_info[i] = NULL;
69 fb_helper->connector_count = 0;
72 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
74 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
76 struct drm_fb_helper_connector *fb_helper_conn;
79 for (i = 0; i < fb_helper->connector_count; i++) {
80 struct drm_cmdline_mode *mode;
81 struct drm_connector *connector;
84 fb_helper_conn = fb_helper->connector_info[i];
85 connector = fb_helper_conn->connector;
86 mode = &fb_helper_conn->cmdline_mode;
88 /* do something on return - turn off connector maybe */
89 if (fb_get_options(drm_get_connector_name(connector), &option))
92 if (drm_mode_parse_command_line_for_connector(option,
97 switch (mode->force) {
98 case DRM_FORCE_OFF: s = "OFF"; break;
99 case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
101 case DRM_FORCE_ON: s = "ON"; break;
104 DRM_INFO("forcing %s connector %s\n",
105 drm_get_connector_name(connector), s);
106 connector->force = mode->force;
109 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
110 drm_get_connector_name(connector),
111 mode->xres, mode->yres,
112 mode->refresh_specified ? mode->refresh : 60,
113 mode->rb ? " reduced blanking" : "",
114 mode->margins ? " with margins" : "",
115 mode->interlace ? " interlaced" : "");
122 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
124 uint16_t *r_base, *g_base, *b_base;
127 r_base = crtc->gamma_store;
128 g_base = r_base + crtc->gamma_size;
129 b_base = g_base + crtc->gamma_size;
131 for (i = 0; i < crtc->gamma_size; i++)
132 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
135 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
137 uint16_t *r_base, *g_base, *b_base;
139 r_base = crtc->gamma_store;
140 g_base = r_base + crtc->gamma_size;
141 b_base = g_base + crtc->gamma_size;
143 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
146 int drm_fb_helper_debug_enter(struct fb_info *info)
148 struct drm_fb_helper *helper = info->par;
149 struct drm_crtc_helper_funcs *funcs;
152 if (list_empty(&kernel_fb_helper_list))
155 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
156 for (i = 0; i < helper->crtc_count; i++) {
157 struct drm_mode_set *mode_set =
158 &helper->crtc_info[i].mode_set;
160 if (!mode_set->crtc->enabled)
163 funcs = mode_set->crtc->helper_private;
164 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
165 funcs->mode_set_base_atomic(mode_set->crtc,
169 ENTER_ATOMIC_MODE_SET);
175 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
177 /* Find the real fb for a given fb helper CRTC */
178 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
180 struct drm_device *dev = crtc->dev;
183 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
184 if (crtc->base.id == c->base.id)
191 int drm_fb_helper_debug_leave(struct fb_info *info)
193 struct drm_fb_helper *helper = info->par;
194 struct drm_crtc *crtc;
195 struct drm_crtc_helper_funcs *funcs;
196 struct drm_framebuffer *fb;
199 for (i = 0; i < helper->crtc_count; i++) {
200 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
201 crtc = mode_set->crtc;
202 funcs = crtc->helper_private;
203 fb = drm_mode_config_fb(crtc);
209 DRM_ERROR("no fb to restore??\n");
213 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
214 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
215 crtc->y, LEAVE_ATOMIC_MODE_SET);
220 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
222 bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
226 for (i = 0; i < fb_helper->crtc_count; i++) {
227 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
228 ret = drm_crtc_helper_set_config(mode_set);
234 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode);
236 bool drm_fb_helper_force_kernel_mode(void)
238 bool ret, error = false;
239 struct drm_fb_helper *helper;
241 if (list_empty(&kernel_fb_helper_list))
244 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
245 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
248 ret = drm_fb_helper_restore_fbdev_mode(helper);
255 int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
258 printk(KERN_ERR "panic occurred, switching back to text console\n");
259 return drm_fb_helper_force_kernel_mode();
261 EXPORT_SYMBOL(drm_fb_helper_panic);
263 static struct notifier_block paniced = {
264 .notifier_call = drm_fb_helper_panic,
268 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
270 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
272 void drm_fb_helper_restore(void)
275 ret = drm_fb_helper_force_kernel_mode();
277 DRM_ERROR("Failed to restore crtc configuration\n");
279 EXPORT_SYMBOL(drm_fb_helper_restore);
281 #ifdef CONFIG_MAGIC_SYSRQ
282 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
284 drm_fb_helper_restore();
286 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
288 static void drm_fb_helper_sysrq(int dummy1)
290 schedule_work(&drm_fb_helper_restore_work);
293 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
294 .handler = drm_fb_helper_sysrq,
295 .help_msg = "force-fb(V)",
296 .action_msg = "Restore framebuffer console",
299 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
302 static void drm_fb_helper_on(struct fb_info *info)
304 struct drm_fb_helper *fb_helper = info->par;
305 struct drm_device *dev = fb_helper->dev;
306 struct drm_crtc *crtc;
307 struct drm_crtc_helper_funcs *crtc_funcs;
308 struct drm_connector *connector;
309 struct drm_encoder *encoder;
313 * For each CRTC in this fb, turn the crtc on then,
314 * find all associated encoders and turn them on.
316 mutex_lock(&dev->mode_config.mutex);
317 for (i = 0; i < fb_helper->crtc_count; i++) {
318 crtc = fb_helper->crtc_info[i].mode_set.crtc;
319 crtc_funcs = crtc->helper_private;
324 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
326 /* Walk the connectors & encoders on this fb turning them on */
327 for (j = 0; j < fb_helper->connector_count; j++) {
328 connector = fb_helper->connector_info[j]->connector;
329 connector->dpms = DRM_MODE_DPMS_ON;
330 drm_connector_property_set_value(connector,
331 dev->mode_config.dpms_property,
334 /* Found a CRTC on this fb, now find encoders */
335 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
336 if (encoder->crtc == crtc) {
337 struct drm_encoder_helper_funcs *encoder_funcs;
339 encoder_funcs = encoder->helper_private;
340 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
344 mutex_unlock(&dev->mode_config.mutex);
347 static void drm_fb_helper_off(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_crtc_helper_funcs *crtc_funcs;
353 struct drm_connector *connector;
354 struct drm_encoder *encoder;
358 * For each CRTC in this fb, find all associated encoders
359 * and turn them off, then turn off the CRTC.
361 mutex_lock(&dev->mode_config.mutex);
362 for (i = 0; i < fb_helper->crtc_count; i++) {
363 crtc = fb_helper->crtc_info[i].mode_set.crtc;
364 crtc_funcs = crtc->helper_private;
369 /* Walk the connectors on this fb and mark them off */
370 for (j = 0; j < fb_helper->connector_count; j++) {
371 connector = fb_helper->connector_info[j]->connector;
372 connector->dpms = dpms_mode;
373 drm_connector_property_set_value(connector,
374 dev->mode_config.dpms_property,
377 /* Found a CRTC on this fb, now find encoders */
378 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
379 if (encoder->crtc == crtc) {
380 struct drm_encoder_helper_funcs *encoder_funcs;
382 encoder_funcs = encoder->helper_private;
383 encoder_funcs->dpms(encoder, dpms_mode);
386 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
388 mutex_unlock(&dev->mode_config.mutex);
391 int drm_fb_helper_blank(int blank, struct fb_info *info)
394 /* Display: On; HSync: On, VSync: On */
395 case FB_BLANK_UNBLANK:
396 drm_fb_helper_on(info);
398 /* Display: Off; HSync: On, VSync: On */
399 case FB_BLANK_NORMAL:
400 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
402 /* Display: Off; HSync: Off, VSync: On */
403 case FB_BLANK_HSYNC_SUSPEND:
404 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
406 /* Display: Off; HSync: On, VSync: Off */
407 case FB_BLANK_VSYNC_SUSPEND:
408 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
410 /* Display: Off; HSync: Off, VSync: Off */
411 case FB_BLANK_POWERDOWN:
412 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
417 EXPORT_SYMBOL(drm_fb_helper_blank);
419 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
423 for (i = 0; i < helper->connector_count; i++)
424 kfree(helper->connector_info[i]);
425 kfree(helper->connector_info);
426 for (i = 0; i < helper->crtc_count; i++)
427 kfree(helper->crtc_info[i].mode_set.connectors);
428 kfree(helper->crtc_info);
431 int drm_fb_helper_init(struct drm_device *dev,
432 struct drm_fb_helper *fb_helper,
433 int crtc_count, int max_conn_count)
435 struct drm_crtc *crtc;
439 fb_helper->dev = dev;
441 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
443 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
444 if (!fb_helper->crtc_info)
447 fb_helper->crtc_count = crtc_count;
448 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
449 if (!fb_helper->connector_info) {
450 kfree(fb_helper->crtc_info);
453 fb_helper->connector_count = 0;
455 for (i = 0; i < crtc_count; i++) {
456 fb_helper->crtc_info[i].mode_set.connectors =
457 kcalloc(max_conn_count,
458 sizeof(struct drm_connector *),
461 if (!fb_helper->crtc_info[i].mode_set.connectors) {
465 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
469 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
470 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
471 fb_helper->crtc_info[i].mode_set.crtc = crtc;
474 fb_helper->conn_limit = max_conn_count;
477 drm_fb_helper_crtc_free(fb_helper);
480 EXPORT_SYMBOL(drm_fb_helper_init);
482 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
484 if (!list_empty(&fb_helper->kernel_fb_list)) {
485 list_del(&fb_helper->kernel_fb_list);
486 if (list_empty(&kernel_fb_helper_list)) {
487 printk(KERN_INFO "drm: unregistered panic notifier\n");
488 atomic_notifier_chain_unregister(&panic_notifier_list,
490 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
494 drm_fb_helper_crtc_free(fb_helper);
497 EXPORT_SYMBOL(drm_fb_helper_fini);
499 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
500 u16 blue, u16 regno, struct fb_info *info)
502 struct drm_fb_helper *fb_helper = info->par;
503 struct drm_framebuffer *fb = fb_helper->fb;
506 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
509 /* place color in psuedopalette */
512 palette = (u32 *)info->pseudo_palette;
513 red >>= (16 - info->var.red.length);
514 green >>= (16 - info->var.green.length);
515 blue >>= (16 - info->var.blue.length);
516 value = (red << info->var.red.offset) |
517 (green << info->var.green.offset) |
518 (blue << info->var.blue.offset);
519 if (info->var.transp.length > 0) {
520 u32 mask = (1 << info->var.transp.length) - 1;
521 mask <<= info->var.transp.offset;
524 palette[regno] = value;
530 if (fb->bits_per_pixel == 16) {
533 if (fb->depth == 16 && regno > 63)
535 if (fb->depth == 15 && regno > 31)
538 if (fb->depth == 16) {
542 for (i = 0; i < 8; i++)
543 fb_helper->funcs->gamma_set(crtc, red,
544 green, blue, pindex + i);
547 fb_helper->funcs->gamma_get(crtc, &r,
551 for (i = 0; i < 4; i++)
552 fb_helper->funcs->gamma_set(crtc, r,
559 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
563 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
565 struct drm_fb_helper *fb_helper = info->par;
566 struct drm_crtc_helper_funcs *crtc_funcs;
567 u16 *red, *green, *blue, *transp;
568 struct drm_crtc *crtc;
572 for (i = 0; i < fb_helper->crtc_count; i++) {
573 crtc = fb_helper->crtc_info[i].mode_set.crtc;
574 crtc_funcs = crtc->helper_private;
579 transp = cmap->transp;
582 for (j = 0; j < cmap->len; j++) {
583 u16 hred, hgreen, hblue, htransp = 0xffff;
592 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
596 crtc_funcs->load_lut(crtc);
600 EXPORT_SYMBOL(drm_fb_helper_setcmap);
602 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
603 struct fb_info *info)
605 struct drm_fb_helper *fb_helper = info->par;
606 struct drm_framebuffer *fb = fb_helper->fb;
609 if (var->pixclock != 0 || in_dbg_master())
612 /* Need to resize the fb object !!! */
613 if (var->bits_per_pixel > fb->bits_per_pixel ||
614 var->xres > fb->width || var->yres > fb->height ||
615 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
616 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
617 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
618 var->xres, var->yres, var->bits_per_pixel,
619 var->xres_virtual, var->yres_virtual,
620 fb->width, fb->height, fb->bits_per_pixel);
624 switch (var->bits_per_pixel) {
626 depth = (var->green.length == 6) ? 16 : 15;
629 depth = (var->transp.length > 0) ? 32 : 24;
632 depth = var->bits_per_pixel;
639 var->green.offset = 0;
640 var->blue.offset = 0;
642 var->green.length = 8;
643 var->blue.length = 8;
644 var->transp.length = 0;
645 var->transp.offset = 0;
648 var->red.offset = 10;
649 var->green.offset = 5;
650 var->blue.offset = 0;
652 var->green.length = 5;
653 var->blue.length = 5;
654 var->transp.length = 1;
655 var->transp.offset = 15;
658 var->red.offset = 11;
659 var->green.offset = 5;
660 var->blue.offset = 0;
662 var->green.length = 6;
663 var->blue.length = 5;
664 var->transp.length = 0;
665 var->transp.offset = 0;
668 var->red.offset = 16;
669 var->green.offset = 8;
670 var->blue.offset = 0;
672 var->green.length = 8;
673 var->blue.length = 8;
674 var->transp.length = 0;
675 var->transp.offset = 0;
678 var->red.offset = 16;
679 var->green.offset = 8;
680 var->blue.offset = 0;
682 var->green.length = 8;
683 var->blue.length = 8;
684 var->transp.length = 8;
685 var->transp.offset = 24;
692 EXPORT_SYMBOL(drm_fb_helper_check_var);
694 /* this will let fbcon do the mode init */
695 int drm_fb_helper_set_par(struct fb_info *info)
697 struct drm_fb_helper *fb_helper = info->par;
698 struct drm_device *dev = fb_helper->dev;
699 struct fb_var_screeninfo *var = &info->var;
700 struct drm_crtc *crtc;
704 if (var->pixclock != 0) {
705 DRM_ERROR("PIXEL CLOCK SET\n");
709 mutex_lock(&dev->mode_config.mutex);
710 for (i = 0; i < fb_helper->crtc_count; i++) {
711 crtc = fb_helper->crtc_info[i].mode_set.crtc;
712 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
714 mutex_unlock(&dev->mode_config.mutex);
718 mutex_unlock(&dev->mode_config.mutex);
720 if (fb_helper->delayed_hotplug) {
721 fb_helper->delayed_hotplug = false;
722 drm_fb_helper_hotplug_event(fb_helper);
726 EXPORT_SYMBOL(drm_fb_helper_set_par);
728 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
729 struct fb_info *info)
731 struct drm_fb_helper *fb_helper = info->par;
732 struct drm_device *dev = fb_helper->dev;
733 struct drm_mode_set *modeset;
734 struct drm_crtc *crtc;
738 mutex_lock(&dev->mode_config.mutex);
739 for (i = 0; i < fb_helper->crtc_count; i++) {
740 crtc = fb_helper->crtc_info[i].mode_set.crtc;
742 modeset = &fb_helper->crtc_info[i].mode_set;
744 modeset->x = var->xoffset;
745 modeset->y = var->yoffset;
747 if (modeset->num_connectors) {
748 ret = crtc->funcs->set_config(modeset);
750 info->var.xoffset = var->xoffset;
751 info->var.yoffset = var->yoffset;
755 mutex_unlock(&dev->mode_config.mutex);
758 EXPORT_SYMBOL(drm_fb_helper_pan_display);
760 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
766 struct fb_info *info;
767 struct drm_fb_helper_surface_size sizes;
770 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
771 sizes.surface_depth = 24;
772 sizes.surface_bpp = 32;
773 sizes.fb_width = (unsigned)-1;
774 sizes.fb_height = (unsigned)-1;
776 /* if driver picks 8 or 16 by default use that
777 for both depth/bpp */
778 if (preferred_bpp != sizes.surface_bpp) {
779 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
781 /* first up get a count of crtcs now in use and new min/maxes width/heights */
782 for (i = 0; i < fb_helper->connector_count; i++) {
783 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
784 struct drm_cmdline_mode *cmdline_mode;
786 cmdline_mode = &fb_helper_conn->cmdline_mode;
788 if (cmdline_mode->bpp_specified) {
789 switch (cmdline_mode->bpp) {
791 sizes.surface_depth = sizes.surface_bpp = 8;
794 sizes.surface_depth = 15;
795 sizes.surface_bpp = 16;
798 sizes.surface_depth = sizes.surface_bpp = 16;
801 sizes.surface_depth = sizes.surface_bpp = 24;
804 sizes.surface_depth = 24;
805 sizes.surface_bpp = 32;
813 for (i = 0; i < fb_helper->crtc_count; i++) {
814 struct drm_display_mode *desired_mode;
815 desired_mode = fb_helper->crtc_info[i].desired_mode;
819 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
820 if (desired_mode->hdisplay < sizes.fb_width)
821 sizes.fb_width = desired_mode->hdisplay;
822 if (desired_mode->vdisplay < sizes.fb_height)
823 sizes.fb_height = desired_mode->vdisplay;
824 if (desired_mode->hdisplay > sizes.surface_width)
825 sizes.surface_width = desired_mode->hdisplay;
826 if (desired_mode->vdisplay > sizes.surface_height)
827 sizes.surface_height = desired_mode->vdisplay;
832 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
833 /* hmm everyone went away - assume VGA cable just fell out
834 and will come back later. */
835 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
836 sizes.fb_width = sizes.surface_width = 1024;
837 sizes.fb_height = sizes.surface_height = 768;
840 /* push down into drivers */
841 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
845 info = fb_helper->fbdev;
847 /* set the fb pointer */
848 for (i = 0; i < fb_helper->crtc_count; i++) {
849 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
853 info->var.pixclock = 0;
854 if (register_framebuffer(info) < 0) {
858 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
862 drm_fb_helper_set_par(info);
865 /* Switch back to kernel console on panic */
866 /* multi card linked list maybe */
867 if (list_empty(&kernel_fb_helper_list)) {
868 printk(KERN_INFO "drm: registered panic notifier\n");
869 atomic_notifier_chain_register(&panic_notifier_list,
871 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
874 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
878 EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
880 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
883 info->fix.type = FB_TYPE_PACKED_PIXELS;
884 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
886 info->fix.mmio_start = 0;
887 info->fix.mmio_len = 0;
888 info->fix.type_aux = 0;
889 info->fix.xpanstep = 1; /* doing it in hw */
890 info->fix.ypanstep = 1; /* doing it in hw */
891 info->fix.ywrapstep = 0;
892 info->fix.accel = FB_ACCEL_NONE;
893 info->fix.type_aux = 0;
895 info->fix.line_length = pitch;
898 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
900 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
901 uint32_t fb_width, uint32_t fb_height)
903 struct drm_framebuffer *fb = fb_helper->fb;
904 info->pseudo_palette = fb_helper->pseudo_palette;
905 info->var.xres_virtual = fb->width;
906 info->var.yres_virtual = fb->height;
907 info->var.bits_per_pixel = fb->bits_per_pixel;
908 info->var.accel_flags = FB_ACCELF_TEXT;
909 info->var.xoffset = 0;
910 info->var.yoffset = 0;
911 info->var.activate = FB_ACTIVATE_NOW;
912 info->var.height = -1;
913 info->var.width = -1;
917 info->var.red.offset = 0;
918 info->var.green.offset = 0;
919 info->var.blue.offset = 0;
920 info->var.red.length = 8; /* 8bit DAC */
921 info->var.green.length = 8;
922 info->var.blue.length = 8;
923 info->var.transp.offset = 0;
924 info->var.transp.length = 0;
927 info->var.red.offset = 10;
928 info->var.green.offset = 5;
929 info->var.blue.offset = 0;
930 info->var.red.length = 5;
931 info->var.green.length = 5;
932 info->var.blue.length = 5;
933 info->var.transp.offset = 15;
934 info->var.transp.length = 1;
937 info->var.red.offset = 11;
938 info->var.green.offset = 5;
939 info->var.blue.offset = 0;
940 info->var.red.length = 5;
941 info->var.green.length = 6;
942 info->var.blue.length = 5;
943 info->var.transp.offset = 0;
946 info->var.red.offset = 16;
947 info->var.green.offset = 8;
948 info->var.blue.offset = 0;
949 info->var.red.length = 8;
950 info->var.green.length = 8;
951 info->var.blue.length = 8;
952 info->var.transp.offset = 0;
953 info->var.transp.length = 0;
956 info->var.red.offset = 16;
957 info->var.green.offset = 8;
958 info->var.blue.offset = 0;
959 info->var.red.length = 8;
960 info->var.green.length = 8;
961 info->var.blue.length = 8;
962 info->var.transp.offset = 24;
963 info->var.transp.length = 8;
969 info->var.xres = fb_width;
970 info->var.yres = fb_height;
972 EXPORT_SYMBOL(drm_fb_helper_fill_var);
974 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
978 struct drm_connector *connector;
982 for (i = 0; i < fb_helper->connector_count; i++) {
983 connector = fb_helper->connector_info[i]->connector;
984 count += connector->funcs->fill_modes(connector, maxX, maxY);
990 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
992 struct drm_display_mode *mode;
994 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
995 if (drm_mode_width(mode) > width ||
996 drm_mode_height(mode) > height)
998 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1004 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1006 struct drm_cmdline_mode *cmdline_mode;
1007 cmdline_mode = &fb_connector->cmdline_mode;
1008 return cmdline_mode->specified;
1011 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1012 int width, int height)
1014 struct drm_cmdline_mode *cmdline_mode;
1015 struct drm_display_mode *mode = NULL;
1017 cmdline_mode = &fb_helper_conn->cmdline_mode;
1018 if (cmdline_mode->specified == false)
1021 /* attempt to find a matching mode in the list of modes
1022 * we have gotten so far, if not add a CVT mode that conforms
1024 if (cmdline_mode->rb || cmdline_mode->margins)
1027 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1028 /* check width/height */
1029 if (mode->hdisplay != cmdline_mode->xres ||
1030 mode->vdisplay != cmdline_mode->yres)
1033 if (cmdline_mode->refresh_specified) {
1034 if (mode->vrefresh != cmdline_mode->refresh)
1038 if (cmdline_mode->interlace) {
1039 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1046 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1048 list_add(&mode->head, &fb_helper_conn->connector->modes);
1052 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1057 enable = connector->status == connector_status_connected;
1059 enable = connector->status != connector_status_disconnected;
1064 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1067 bool any_enabled = false;
1068 struct drm_connector *connector;
1071 for (i = 0; i < fb_helper->connector_count; i++) {
1072 connector = fb_helper->connector_info[i]->connector;
1073 enabled[i] = drm_connector_enabled(connector, true);
1074 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1075 enabled[i] ? "yes" : "no");
1076 any_enabled |= enabled[i];
1082 for (i = 0; i < fb_helper->connector_count; i++) {
1083 connector = fb_helper->connector_info[i]->connector;
1084 enabled[i] = drm_connector_enabled(connector, false);
1088 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1089 struct drm_display_mode **modes,
1090 bool *enabled, int width, int height)
1093 bool can_clone = false;
1094 struct drm_fb_helper_connector *fb_helper_conn;
1095 struct drm_display_mode *dmt_mode, *mode;
1097 /* only contemplate cloning in the single crtc case */
1098 if (fb_helper->crtc_count > 1)
1102 for (i = 0; i < fb_helper->connector_count; i++) {
1107 /* only contemplate cloning if more than one connector is enabled */
1111 /* check the command line or if nothing common pick 1024x768 */
1113 for (i = 0; i < fb_helper->connector_count; i++) {
1116 fb_helper_conn = fb_helper->connector_info[i];
1117 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1122 for (j = 0; j < i; j++) {
1125 if (!drm_mode_equal(modes[j], modes[i]))
1131 DRM_DEBUG_KMS("can clone using command line\n");
1135 /* try and find a 1024x768 mode on each connector */
1137 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1139 for (i = 0; i < fb_helper->connector_count; i++) {
1144 fb_helper_conn = fb_helper->connector_info[i];
1145 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1146 if (drm_mode_equal(mode, dmt_mode))
1154 DRM_DEBUG_KMS("can clone using 1024x768\n");
1157 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1161 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1162 struct drm_display_mode **modes,
1163 bool *enabled, int width, int height)
1165 struct drm_fb_helper_connector *fb_helper_conn;
1168 for (i = 0; i < fb_helper->connector_count; i++) {
1169 fb_helper_conn = fb_helper->connector_info[i];
1171 if (enabled[i] == false)
1174 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1175 fb_helper_conn->connector->base.id);
1177 /* got for command line mode first */
1178 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1180 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1181 fb_helper_conn->connector->base.id);
1182 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1184 /* No preferred modes, pick one off the list */
1185 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1186 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1189 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1195 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1196 struct drm_fb_helper_crtc **best_crtcs,
1197 struct drm_display_mode **modes,
1198 int n, int width, int height)
1201 struct drm_device *dev = fb_helper->dev;
1202 struct drm_connector *connector;
1203 struct drm_connector_helper_funcs *connector_funcs;
1204 struct drm_encoder *encoder;
1205 struct drm_fb_helper_crtc *best_crtc;
1206 int my_score, best_score, score;
1207 struct drm_fb_helper_crtc **crtcs, *crtc;
1208 struct drm_fb_helper_connector *fb_helper_conn;
1210 if (n == fb_helper->connector_count)
1213 fb_helper_conn = fb_helper->connector_info[n];
1214 connector = fb_helper_conn->connector;
1216 best_crtcs[n] = NULL;
1218 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1219 if (modes[n] == NULL)
1222 crtcs = kzalloc(dev->mode_config.num_connector *
1223 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1228 if (connector->status == connector_status_connected)
1230 if (drm_has_cmdline_mode(fb_helper_conn))
1232 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1235 connector_funcs = connector->helper_private;
1236 encoder = connector_funcs->best_encoder(connector);
1240 /* select a crtc for this connector and then attempt to configure
1241 remaining connectors */
1242 for (c = 0; c < fb_helper->crtc_count; c++) {
1243 crtc = &fb_helper->crtc_info[c];
1245 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1249 for (o = 0; o < n; o++)
1250 if (best_crtcs[o] == crtc)
1254 /* ignore cloning unless only a single crtc */
1255 if (fb_helper->crtc_count > 1)
1258 if (!drm_mode_equal(modes[o], modes[n]))
1263 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1264 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1266 if (score > best_score) {
1269 memcpy(best_crtcs, crtcs,
1270 dev->mode_config.num_connector *
1271 sizeof(struct drm_fb_helper_crtc *));
1279 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1281 struct drm_device *dev = fb_helper->dev;
1282 struct drm_fb_helper_crtc **crtcs;
1283 struct drm_display_mode **modes;
1284 struct drm_encoder *encoder;
1285 struct drm_mode_set *modeset;
1290 DRM_DEBUG_KMS("\n");
1292 width = dev->mode_config.max_width;
1293 height = dev->mode_config.max_height;
1295 /* clean out all the encoder/crtc combos */
1296 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1297 encoder->crtc = NULL;
1300 crtcs = kcalloc(dev->mode_config.num_connector,
1301 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1302 modes = kcalloc(dev->mode_config.num_connector,
1303 sizeof(struct drm_display_mode *), GFP_KERNEL);
1304 enabled = kcalloc(dev->mode_config.num_connector,
1305 sizeof(bool), GFP_KERNEL);
1307 drm_enable_connectors(fb_helper, enabled);
1309 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1311 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1313 DRM_ERROR("Unable to find initial modes\n");
1316 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1318 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1320 /* need to set the modesets up here for use later */
1321 /* fill out the connector<->crtc mappings into the modesets */
1322 for (i = 0; i < fb_helper->crtc_count; i++) {
1323 modeset = &fb_helper->crtc_info[i].mode_set;
1324 modeset->num_connectors = 0;
1327 for (i = 0; i < fb_helper->connector_count; i++) {
1328 struct drm_display_mode *mode = modes[i];
1329 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1330 modeset = &fb_crtc->mode_set;
1332 if (mode && fb_crtc) {
1333 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1334 mode->name, fb_crtc->mode_set.crtc->base.id);
1335 fb_crtc->desired_mode = mode;
1337 drm_mode_destroy(dev, modeset->mode);
1338 modeset->mode = drm_mode_duplicate(dev,
1339 fb_crtc->desired_mode);
1340 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1350 * drm_helper_initial_config - setup a sane initial connector configuration
1354 * Called at init time, must take mode config lock.
1356 * Scan the CRTCs and connectors and try to put together an initial setup.
1357 * At the moment, this is a cloned configuration across all heads with
1358 * a new framebuffer object as the backing store.
1361 * Zero if everything went ok, nonzero otherwise.
1363 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1365 struct drm_device *dev = fb_helper->dev;
1368 /* disable all the possible outputs/crtcs before entering KMS mode */
1369 drm_helper_disable_unused_functions(fb_helper->dev);
1371 drm_fb_helper_parse_command_line(fb_helper);
1373 count = drm_fb_helper_probe_connector_modes(fb_helper,
1374 dev->mode_config.max_width,
1375 dev->mode_config.max_height);
1377 * we shouldn't end up with no modes here.
1380 printk(KERN_INFO "No connectors reported connected with modes\n");
1382 drm_setup_crtcs(fb_helper);
1384 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1386 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1389 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1390 * probing all the outputs attached to the fb.
1391 * @fb_helper: the drm_fb_helper
1394 * Called at runtime, must take mode config lock.
1396 * Scan the connectors attached to the fb_helper and try to put together a
1397 * setup after *notification of a change in output configuration.
1400 * 0 on success and a non-zero error code otherwise.
1402 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1404 struct drm_device *dev = fb_helper->dev;
1406 u32 max_width, max_height, bpp_sel;
1407 bool bound = false, crtcs_bound = false;
1408 struct drm_crtc *crtc;
1413 mutex_lock(&dev->mode_config.mutex);
1414 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1417 if (crtc->fb == fb_helper->fb)
1421 if (!bound && crtcs_bound) {
1422 fb_helper->delayed_hotplug = true;
1423 mutex_unlock(&dev->mode_config.mutex);
1426 DRM_DEBUG_KMS("\n");
1428 max_width = fb_helper->fb->width;
1429 max_height = fb_helper->fb->height;
1430 bpp_sel = fb_helper->fb->bits_per_pixel;
1432 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1434 drm_setup_crtcs(fb_helper);
1435 mutex_unlock(&dev->mode_config.mutex);
1437 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1439 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
1441 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
1442 * but the module doesn't depend on any fb console symbols. At least
1443 * attempt to load fbcon to avoid leaving the system without a usable console.
1445 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
1446 static int __init drm_fb_helper_modinit(void)
1448 const char *name = "fbcon";
1449 struct module *fbcon;
1451 mutex_lock(&module_mutex);
1452 fbcon = find_module(name);
1453 mutex_unlock(&module_mutex);
1456 request_module_nowait(name);
1460 module_init(drm_fb_helper_modinit);