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 * $FreeBSD: src/sys/dev/drm2/drm_fb_helper.c,v 1.1 2012/05/22 11:07:44 kib Exp $
33 #include <linux/export.h>
35 #include <drm/drm_crtc.h>
36 #include <drm/drm_fb_helper.h>
37 #include <drm/drm_crtc_helper.h>
39 static LINUX_LIST_HEAD(kernel_fb_helper_list);
41 /* simple single crtc case helper function */
42 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
44 struct drm_device *dev = fb_helper->dev;
45 struct drm_connector *connector;
47 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
48 struct drm_fb_helper_connector *fb_helper_connector;
50 fb_helper_connector = kmalloc(
51 sizeof(struct drm_fb_helper_connector), DRM_MEM_KMS,
54 fb_helper_connector->connector = connector;
55 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
60 const char *fb_mode_option;
63 fb_get_options(const char *connector_name, char **option)
69 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
71 struct drm_fb_helper_connector *fb_helper_conn;
74 for (i = 0; i < fb_helper->connector_count; i++) {
75 struct drm_cmdline_mode *mode;
76 struct drm_connector *connector;
79 fb_helper_conn = fb_helper->connector_info[i];
80 connector = fb_helper_conn->connector;
81 mode = &fb_helper_conn->cmdline_mode;
83 /* do something on return - turn off connector maybe */
84 if (fb_get_options(drm_get_connector_name(connector), &option))
87 if (drm_mode_parse_command_line_for_connector(option,
92 switch (mode->force) {
96 case DRM_FORCE_ON_DIGITAL:
105 DRM_INFO("forcing %s connector %s\n",
106 drm_get_connector_name(connector), s);
107 connector->force = mode->force;
110 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
111 drm_get_connector_name(connector),
112 mode->xres, mode->yres,
113 mode->refresh_specified ? mode->refresh : 60,
114 mode->rb ? " reduced blanking" : "",
115 mode->margins ? " with margins" : "",
116 mode->interlace ? " interlaced" : "");
124 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
126 uint16_t *r_base, *g_base, *b_base;
129 r_base = crtc->gamma_store;
130 g_base = r_base + crtc->gamma_size;
131 b_base = g_base + crtc->gamma_size;
133 for (i = 0; i < crtc->gamma_size; i++)
134 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
137 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
139 uint16_t *r_base, *g_base, *b_base;
141 r_base = crtc->gamma_store;
142 g_base = r_base + crtc->gamma_size;
143 b_base = g_base + crtc->gamma_size;
145 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
150 int drm_fb_helper_debug_enter(struct fb_info *info)
152 struct drm_fb_helper *helper = info->par;
153 struct drm_crtc_helper_funcs *funcs;
156 if (list_empty(&kernel_fb_helper_list))
159 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
160 for (i = 0; i < helper->crtc_count; i++) {
161 struct drm_mode_set *mode_set =
162 &helper->crtc_info[i].mode_set;
164 if (!mode_set->crtc->enabled)
167 funcs = mode_set->crtc->helper_private;
168 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
169 funcs->mode_set_base_atomic(mode_set->crtc,
173 ENTER_ATOMIC_MODE_SET);
182 /* Find the real fb for a given fb helper CRTC */
183 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
185 struct drm_device *dev = crtc->dev;
188 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
189 if (crtc->base.id == c->base.id)
198 int drm_fb_helper_debug_leave(struct fb_info *info)
200 struct drm_fb_helper *helper = info->par;
201 struct drm_crtc *crtc;
202 struct drm_crtc_helper_funcs *funcs;
203 struct drm_framebuffer *fb;
206 for (i = 0; i < helper->crtc_count; i++) {
207 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
208 crtc = mode_set->crtc;
209 funcs = crtc->helper_private;
210 fb = drm_mode_config_fb(crtc);
216 DRM_ERROR("no fb to restore??\n");
220 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
221 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
222 crtc->y, LEAVE_ATOMIC_MODE_SET);
229 bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
233 for (i = 0; i < fb_helper->crtc_count; i++) {
234 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
235 ret = drm_crtc_helper_set_config(mode_set);
243 bool drm_fb_helper_force_kernel_mode(void)
245 bool ret, error = false;
246 struct drm_fb_helper *helper;
248 if (list_empty(&kernel_fb_helper_list))
251 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
252 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
255 ret = drm_fb_helper_restore_fbdev_mode(helper);
264 int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
267 printf("panic occurred, switching back to text console\n");
268 return drm_fb_helper_force_kernel_mode();
272 static struct notifier_block paniced = {
273 .notifier_call = drm_fb_helper_panic,
277 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
279 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
281 void drm_fb_helper_restore(void)
284 ret = drm_fb_helper_force_kernel_mode();
286 DRM_ERROR("Failed to restore crtc configuration\n");
289 #ifdef CONFIG_MAGIC_SYSRQ
290 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
292 drm_fb_helper_restore();
294 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
296 static void drm_fb_helper_sysrq(int dummy1)
298 schedule_work(&drm_fb_helper_restore_work);
301 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
302 .handler = drm_fb_helper_sysrq,
303 .help_msg = "force-fb(V)",
304 .action_msg = "Restore framebuffer console",
307 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
312 static void drm_fb_helper_on(struct fb_info *info)
314 struct drm_fb_helper *fb_helper = info->par;
315 struct drm_device *dev = fb_helper->dev;
316 struct drm_crtc *crtc;
317 struct drm_crtc_helper_funcs *crtc_funcs;
318 struct drm_connector *connector;
319 struct drm_encoder *encoder;
323 * For each CRTC in this fb, turn the crtc on then,
324 * find all associated encoders and turn them on.
326 sx_xlock(&dev->mode_config.mutex);
327 for (i = 0; i < fb_helper->crtc_count; i++) {
328 crtc = fb_helper->crtc_info[i].mode_set.crtc;
329 crtc_funcs = crtc->helper_private;
334 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
336 /* Walk the connectors & encoders on this fb turning them on */
337 for (j = 0; j < fb_helper->connector_count; j++) {
338 connector = fb_helper->connector_info[j]->connector;
339 connector->dpms = DRM_MODE_DPMS_ON;
340 drm_connector_property_set_value(connector,
341 dev->mode_config.dpms_property,
344 /* Found a CRTC on this fb, now find encoders */
345 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
346 if (encoder->crtc == crtc) {
347 struct drm_encoder_helper_funcs *encoder_funcs;
349 encoder_funcs = encoder->helper_private;
350 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
354 sx_xunlock(&dev->mode_config.mutex);
359 static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
361 struct drm_fb_helper *fb_helper = info->par;
362 struct drm_device *dev = fb_helper->dev;
363 struct drm_crtc *crtc;
364 struct drm_crtc_helper_funcs *crtc_funcs;
365 struct drm_connector *connector;
366 struct drm_encoder *encoder;
370 * For each CRTC in this fb, find all associated encoders
371 * and turn them off, then turn off the CRTC.
373 sx_xlock(&dev->mode_config.mutex);
374 for (i = 0; i < fb_helper->crtc_count; i++) {
375 crtc = fb_helper->crtc_info[i].mode_set.crtc;
376 crtc_funcs = crtc->helper_private;
381 /* Walk the connectors on this fb and mark them off */
382 for (j = 0; j < fb_helper->connector_count; j++) {
383 connector = fb_helper->connector_info[j]->connector;
384 connector->dpms = dpms_mode;
385 drm_connector_property_set_value(connector,
386 dev->mode_config.dpms_property,
389 /* Found a CRTC on this fb, now find encoders */
390 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
391 if (encoder->crtc == crtc) {
392 struct drm_encoder_helper_funcs *encoder_funcs;
394 encoder_funcs = encoder->helper_private;
395 encoder_funcs->dpms(encoder, dpms_mode);
398 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
400 sx_xunlock(&dev->mode_config.mutex);
405 int drm_fb_helper_blank(int blank, struct fb_info *info)
408 /* Display: On; HSync: On, VSync: On */
409 case FB_BLANK_UNBLANK:
410 drm_fb_helper_on(info);
412 /* Display: Off; HSync: On, VSync: On */
413 case FB_BLANK_NORMAL:
414 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
416 /* Display: Off; HSync: Off, VSync: On */
417 case FB_BLANK_HSYNC_SUSPEND:
418 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
420 /* Display: Off; HSync: On, VSync: Off */
421 case FB_BLANK_VSYNC_SUSPEND:
422 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
424 /* Display: Off; HSync: Off, VSync: Off */
425 case FB_BLANK_POWERDOWN:
426 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
433 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
437 for (i = 0; i < helper->connector_count; i++)
438 drm_free(helper->connector_info[i], DRM_MEM_KMS);
439 drm_free(helper->connector_info, DRM_MEM_KMS);
440 for (i = 0; i < helper->crtc_count; i++) {
441 drm_free(helper->crtc_info[i].mode_set.connectors, DRM_MEM_KMS);
442 if (helper->crtc_info[i].mode_set.mode)
443 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
445 drm_free(helper->crtc_info, DRM_MEM_KMS);
448 int drm_fb_helper_init(struct drm_device *dev,
449 struct drm_fb_helper *fb_helper,
450 int crtc_count, int max_conn_count)
452 struct drm_crtc *crtc;
455 fb_helper->dev = dev;
457 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
459 fb_helper->crtc_info = kmalloc(crtc_count *
460 sizeof(struct drm_fb_helper_crtc), DRM_MEM_KMS, M_WAITOK | M_ZERO);
461 if (!fb_helper->crtc_info)
464 fb_helper->crtc_count = crtc_count;
465 fb_helper->connector_info = kmalloc(dev->mode_config.num_connector *
466 sizeof(struct drm_fb_helper_connector *), DRM_MEM_KMS,
468 if (!fb_helper->connector_info) {
469 kfree(fb_helper->crtc_info, DRM_MEM_KMS);
472 fb_helper->connector_count = 0;
474 for (i = 0; i < crtc_count; i++) {
475 fb_helper->crtc_info[i].mode_set.connectors =
476 kmalloc(max_conn_count * sizeof(struct drm_connector *),
477 DRM_MEM_KMS, M_WAITOK | M_ZERO);
479 if (!fb_helper->crtc_info[i].mode_set.connectors)
481 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
485 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
486 fb_helper->crtc_info[i].mode_set.crtc = crtc;
492 drm_fb_helper_crtc_free(fb_helper);
495 EXPORT_SYMBOL(drm_fb_helper_init);
497 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
499 if (!list_empty(&fb_helper->kernel_fb_list)) {
500 list_del(&fb_helper->kernel_fb_list);
501 if (list_empty(&kernel_fb_helper_list)) {
503 printk(KERN_INFO "drm: unregistered panic notifier\n");
504 atomic_notifier_chain_unregister(&panic_notifier_list,
506 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
511 drm_fb_helper_crtc_free(fb_helper);
516 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
517 u16 blue, u16 regno, struct fb_info *info)
519 struct drm_fb_helper *fb_helper = info->par;
520 struct drm_framebuffer *fb = fb_helper->fb;
523 if (info->fix.visual == FB_VISUAL_trueCOLOR) {
526 /* place color in psuedopalette */
529 palette = (u32 *)info->pseudo_palette;
530 red >>= (16 - info->var.red.length);
531 green >>= (16 - info->var.green.length);
532 blue >>= (16 - info->var.blue.length);
533 value = (red << info->var.red.offset) |
534 (green << info->var.green.offset) |
535 (blue << info->var.blue.offset);
536 if (info->var.transp.length > 0) {
537 u32 mask = (1 << info->var.transp.length) - 1;
538 mask <<= info->var.transp.offset;
541 palette[regno] = value;
547 if (fb->bits_per_pixel == 16) {
550 if (fb->depth == 16 && regno > 63)
552 if (fb->depth == 15 && regno > 31)
555 if (fb->depth == 16) {
559 for (i = 0; i < 8; i++)
560 fb_helper->funcs->gamma_set(crtc, red,
561 green, blue, pindex + i);
564 fb_helper->funcs->gamma_get(crtc, &r,
568 for (i = 0; i < 4; i++)
569 fb_helper->funcs->gamma_set(crtc, r,
576 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
582 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
584 struct drm_fb_helper *fb_helper = info->par;
585 struct drm_crtc_helper_funcs *crtc_funcs;
586 u16 *red, *green, *blue, *transp;
587 struct drm_crtc *crtc;
591 for (i = 0; i < fb_helper->crtc_count; i++) {
592 crtc = fb_helper->crtc_info[i].mode_set.crtc;
593 crtc_funcs = crtc->helper_private;
598 transp = cmap->transp;
601 for (j = 0; j < cmap->len; j++) {
602 u16 hred, hgreen, hblue, htransp = 0xffff;
611 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
615 crtc_funcs->load_lut(crtc);
622 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
623 struct fb_info *info)
625 struct drm_fb_helper *fb_helper = info->par;
626 struct drm_framebuffer *fb = fb_helper->fb;
629 if (var->pixclock != 0 || in_dbg_master())
632 /* Need to resize the fb object !!! */
633 if (var->bits_per_pixel > fb->bits_per_pixel ||
634 var->xres > fb->width || var->yres > fb->height ||
635 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
636 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
637 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
638 var->xres, var->yres, var->bits_per_pixel,
639 var->xres_virtual, var->yres_virtual,
640 fb->width, fb->height, fb->bits_per_pixel);
644 switch (var->bits_per_pixel) {
646 depth = (var->green.length == 6) ? 16 : 15;
649 depth = (var->transp.length > 0) ? 32 : 24;
652 depth = var->bits_per_pixel;
659 var->green.offset = 0;
660 var->blue.offset = 0;
662 var->green.length = 8;
663 var->blue.length = 8;
664 var->transp.length = 0;
665 var->transp.offset = 0;
668 var->red.offset = 10;
669 var->green.offset = 5;
670 var->blue.offset = 0;
672 var->green.length = 5;
673 var->blue.length = 5;
674 var->transp.length = 1;
675 var->transp.offset = 15;
678 var->red.offset = 11;
679 var->green.offset = 5;
680 var->blue.offset = 0;
682 var->green.length = 6;
683 var->blue.length = 5;
684 var->transp.length = 0;
685 var->transp.offset = 0;
688 var->red.offset = 16;
689 var->green.offset = 8;
690 var->blue.offset = 0;
692 var->green.length = 8;
693 var->blue.length = 8;
694 var->transp.length = 0;
695 var->transp.offset = 0;
698 var->red.offset = 16;
699 var->green.offset = 8;
700 var->blue.offset = 0;
702 var->green.length = 8;
703 var->blue.length = 8;
704 var->transp.length = 8;
705 var->transp.offset = 24;
715 /* this will let fbcon do the mode init */
716 int drm_fb_helper_set_par(struct fb_info *info)
718 struct drm_fb_helper *fb_helper = info->par;
719 struct drm_device *dev = fb_helper->dev;
720 struct fb_var_screeninfo *var = &info->var;
721 struct drm_crtc *crtc;
725 if (var->pixclock != 0) {
726 DRM_ERROR("PIXEL CLOCK SET\n");
730 mutex_lock(&dev->mode_config.mutex);
731 for (i = 0; i < fb_helper->crtc_count; i++) {
732 crtc = fb_helper->crtc_info[i].mode_set.crtc;
733 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
735 mutex_unlock(&dev->mode_config.mutex);
739 mutex_unlock(&dev->mode_config.mutex);
741 if (fb_helper->delayed_hotplug) {
742 fb_helper->delayed_hotplug = false;
743 drm_fb_helper_hotplug_event(fb_helper);
750 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
751 struct fb_info *info)
753 struct drm_fb_helper *fb_helper = info->par;
754 struct drm_device *dev = fb_helper->dev;
755 struct drm_mode_set *modeset;
756 struct drm_crtc *crtc;
760 mutex_lock(&dev->mode_config.mutex);
761 for (i = 0; i < fb_helper->crtc_count; i++) {
762 crtc = fb_helper->crtc_info[i].mode_set.crtc;
764 modeset = &fb_helper->crtc_info[i].mode_set;
766 modeset->x = var->xoffset;
767 modeset->y = var->yoffset;
769 if (modeset->num_connectors) {
770 ret = crtc->funcs->set_config(modeset);
772 info->var.xoffset = var->xoffset;
773 info->var.yoffset = var->yoffset;
777 mutex_unlock(&dev->mode_config.mutex);
782 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
789 struct fb_info *info;
791 struct drm_fb_helper_surface_size sizes;
794 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
795 sizes.surface_depth = 24;
796 sizes.surface_bpp = 32;
797 sizes.fb_width = (unsigned)-1;
798 sizes.fb_height = (unsigned)-1;
800 /* if driver picks 8 or 16 by default use that
801 for both depth/bpp */
802 if (preferred_bpp != sizes.surface_bpp)
803 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
805 /* first up get a count of crtcs now in use and new min/maxes width/heights */
806 for (i = 0; i < fb_helper->connector_count; i++) {
807 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
808 struct drm_cmdline_mode *cmdline_mode;
810 cmdline_mode = &fb_helper_conn->cmdline_mode;
812 if (cmdline_mode->bpp_specified) {
813 switch (cmdline_mode->bpp) {
815 sizes.surface_depth = sizes.surface_bpp = 8;
818 sizes.surface_depth = 15;
819 sizes.surface_bpp = 16;
822 sizes.surface_depth = sizes.surface_bpp = 16;
825 sizes.surface_depth = sizes.surface_bpp = 24;
828 sizes.surface_depth = 24;
829 sizes.surface_bpp = 32;
837 for (i = 0; i < fb_helper->crtc_count; i++) {
838 struct drm_display_mode *desired_mode;
839 desired_mode = fb_helper->crtc_info[i].desired_mode;
843 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
844 if (desired_mode->hdisplay < sizes.fb_width)
845 sizes.fb_width = desired_mode->hdisplay;
846 if (desired_mode->vdisplay < sizes.fb_height)
847 sizes.fb_height = desired_mode->vdisplay;
848 if (desired_mode->hdisplay > sizes.surface_width)
849 sizes.surface_width = desired_mode->hdisplay;
850 if (desired_mode->vdisplay > sizes.surface_height)
851 sizes.surface_height = desired_mode->vdisplay;
856 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
857 /* hmm everyone went away - assume VGA cable just fell out
858 and will come back later. */
859 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
860 sizes.fb_width = sizes.surface_width = 1024;
861 sizes.fb_height = sizes.surface_height = 768;
864 /* push down into drivers */
865 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
870 info = fb_helper->fbdev;
873 /* set the fb pointer */
874 for (i = 0; i < fb_helper->crtc_count; i++)
875 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
879 info->var.pixclock = 0;
880 if (register_framebuffer(info) < 0)
883 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
884 info->node, info->fix.id);
887 drm_fb_helper_set_par(info);
890 /* Switch back to kernel console on panic */
891 /* multi card linked list maybe */
892 if (list_empty(&kernel_fb_helper_list)) {
893 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
894 atomic_notifier_chain_register(&panic_notifier_list,
896 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
899 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
904 EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
907 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
910 info->fix.type = FB_TYPE_PACKED_PIXELS;
911 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
913 info->fix.mmio_start = 0;
914 info->fix.mmio_len = 0;
915 info->fix.type_aux = 0;
916 info->fix.xpanstep = 1; /* doing it in hw */
917 info->fix.ypanstep = 1; /* doing it in hw */
918 info->fix.ywrapstep = 0;
919 info->fix.accel = FB_ACCEL_NONE;
920 info->fix.type_aux = 0;
922 info->fix.line_length = pitch;
926 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
927 uint32_t fb_width, uint32_t fb_height)
929 struct drm_framebuffer *fb = fb_helper->fb;
930 info->pseudo_palette = fb_helper->pseudo_palette;
931 info->var.xres_virtual = fb->width;
932 info->var.yres_virtual = fb->height;
933 info->var.bits_per_pixel = fb->bits_per_pixel;
934 info->var.accel_flags = FB_ACCELF_TEXT;
935 info->var.xoffset = 0;
936 info->var.yoffset = 0;
937 info->var.activate = FB_ACTIVATE_NOW;
938 info->var.height = -1;
939 info->var.width = -1;
943 info->var.red.offset = 0;
944 info->var.green.offset = 0;
945 info->var.blue.offset = 0;
946 info->var.red.length = 8; /* 8bit DAC */
947 info->var.green.length = 8;
948 info->var.blue.length = 8;
949 info->var.transp.offset = 0;
950 info->var.transp.length = 0;
953 info->var.red.offset = 10;
954 info->var.green.offset = 5;
955 info->var.blue.offset = 0;
956 info->var.red.length = 5;
957 info->var.green.length = 5;
958 info->var.blue.length = 5;
959 info->var.transp.offset = 15;
960 info->var.transp.length = 1;
963 info->var.red.offset = 11;
964 info->var.green.offset = 5;
965 info->var.blue.offset = 0;
966 info->var.red.length = 5;
967 info->var.green.length = 6;
968 info->var.blue.length = 5;
969 info->var.transp.offset = 0;
972 info->var.red.offset = 16;
973 info->var.green.offset = 8;
974 info->var.blue.offset = 0;
975 info->var.red.length = 8;
976 info->var.green.length = 8;
977 info->var.blue.length = 8;
978 info->var.transp.offset = 0;
979 info->var.transp.length = 0;
982 info->var.red.offset = 16;
983 info->var.green.offset = 8;
984 info->var.blue.offset = 0;
985 info->var.red.length = 8;
986 info->var.green.length = 8;
987 info->var.blue.length = 8;
988 info->var.transp.offset = 24;
989 info->var.transp.length = 8;
995 info->var.xres = fb_width;
996 info->var.yres = fb_height;
1000 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1004 struct drm_connector *connector;
1008 for (i = 0; i < fb_helper->connector_count; i++) {
1009 connector = fb_helper->connector_info[i]->connector;
1010 count += connector->funcs->fill_modes(connector, maxX, maxY);
1016 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1018 struct drm_display_mode *mode;
1020 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1021 if (drm_mode_width(mode) > width ||
1022 drm_mode_height(mode) > height)
1024 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1030 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1032 struct drm_cmdline_mode *cmdline_mode;
1033 cmdline_mode = &fb_connector->cmdline_mode;
1034 return cmdline_mode->specified;
1037 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1038 int width, int height)
1040 struct drm_cmdline_mode *cmdline_mode;
1041 struct drm_display_mode *mode = NULL;
1043 cmdline_mode = &fb_helper_conn->cmdline_mode;
1044 if (cmdline_mode->specified == false &&
1045 !drm_fetch_cmdline_mode_from_kenv(fb_helper_conn->connector,
1049 /* attempt to find a matching mode in the list of modes
1050 * we have gotten so far, if not add a CVT mode that conforms
1052 if (cmdline_mode->rb || cmdline_mode->margins)
1055 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1056 /* check width/height */
1057 if (mode->hdisplay != cmdline_mode->xres ||
1058 mode->vdisplay != cmdline_mode->yres)
1061 if (cmdline_mode->refresh_specified) {
1062 if (mode->vrefresh != cmdline_mode->refresh)
1066 if (cmdline_mode->interlace) {
1067 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1074 if (cmdline_mode->cvt)
1075 mode = drm_cvt_mode(fb_helper_conn->connector->dev,
1076 cmdline_mode->xres, cmdline_mode->yres,
1077 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1078 cmdline_mode->rb, cmdline_mode->interlace,
1079 cmdline_mode->margins);
1081 mode = drm_gtf_mode(fb_helper_conn->connector->dev,
1082 cmdline_mode->xres, cmdline_mode->yres,
1083 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1084 cmdline_mode->interlace,
1085 cmdline_mode->margins);
1086 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1087 list_add(&mode->head, &fb_helper_conn->connector->modes);
1091 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1096 enable = connector->status == connector_status_connected;
1098 enable = connector->status != connector_status_disconnected;
1103 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1106 bool any_enabled = false;
1107 struct drm_connector *connector;
1110 for (i = 0; i < fb_helper->connector_count; i++) {
1111 connector = fb_helper->connector_info[i]->connector;
1112 enabled[i] = drm_connector_enabled(connector, true);
1113 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1114 enabled[i] ? "yes" : "no");
1115 any_enabled |= enabled[i];
1121 for (i = 0; i < fb_helper->connector_count; i++) {
1122 connector = fb_helper->connector_info[i]->connector;
1123 enabled[i] = drm_connector_enabled(connector, false);
1127 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1128 struct drm_display_mode **modes,
1129 bool *enabled, int width, int height)
1132 bool can_clone = false;
1133 struct drm_fb_helper_connector *fb_helper_conn;
1134 struct drm_display_mode *dmt_mode, *mode;
1136 /* only contemplate cloning in the single crtc case */
1137 if (fb_helper->crtc_count > 1)
1141 for (i = 0; i < fb_helper->connector_count; i++) {
1146 /* only contemplate cloning if more than one connector is enabled */
1150 /* check the command line or if nothing common pick 1024x768 */
1152 for (i = 0; i < fb_helper->connector_count; i++) {
1155 fb_helper_conn = fb_helper->connector_info[i];
1156 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1161 for (j = 0; j < i; j++) {
1164 if (!drm_mode_equal(modes[j], modes[i]))
1170 DRM_DEBUG_KMS("can clone using command line\n");
1174 /* try and find a 1024x768 mode on each connector */
1176 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1178 for (i = 0; i < fb_helper->connector_count; i++) {
1183 fb_helper_conn = fb_helper->connector_info[i];
1184 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1185 if (drm_mode_equal(mode, dmt_mode))
1193 DRM_DEBUG_KMS("can clone using 1024x768\n");
1196 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1200 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1201 struct drm_display_mode **modes,
1202 bool *enabled, int width, int height)
1204 struct drm_fb_helper_connector *fb_helper_conn;
1207 for (i = 0; i < fb_helper->connector_count; i++) {
1208 fb_helper_conn = fb_helper->connector_info[i];
1210 if (enabled[i] == false)
1213 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1214 fb_helper_conn->connector->base.id);
1216 /* got for command line mode first */
1217 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1219 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1220 fb_helper_conn->connector->base.id);
1221 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1223 /* No preferred modes, pick one off the list */
1224 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1225 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1228 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1234 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1235 struct drm_fb_helper_crtc **best_crtcs,
1236 struct drm_display_mode **modes,
1237 int n, int width, int height)
1240 struct drm_device *dev = fb_helper->dev;
1241 struct drm_connector *connector;
1242 struct drm_connector_helper_funcs *connector_funcs;
1243 struct drm_encoder *encoder;
1244 struct drm_fb_helper_crtc *best_crtc;
1245 int my_score, best_score, score;
1246 struct drm_fb_helper_crtc **crtcs, *crtc;
1247 struct drm_fb_helper_connector *fb_helper_conn;
1249 if (n == fb_helper->connector_count)
1252 fb_helper_conn = fb_helper->connector_info[n];
1253 connector = fb_helper_conn->connector;
1255 best_crtcs[n] = NULL;
1257 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1258 if (modes[n] == NULL)
1261 crtcs = kmalloc(dev->mode_config.num_connector *
1262 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1266 if (connector->status == connector_status_connected)
1268 if (drm_has_cmdline_mode(fb_helper_conn))
1270 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1273 connector_funcs = connector->helper_private;
1274 encoder = connector_funcs->best_encoder(connector);
1278 /* select a crtc for this connector and then attempt to configure
1279 remaining connectors */
1280 for (c = 0; c < fb_helper->crtc_count; c++) {
1281 crtc = &fb_helper->crtc_info[c];
1283 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1287 for (o = 0; o < n; o++)
1288 if (best_crtcs[o] == crtc)
1292 /* ignore cloning unless only a single crtc */
1293 if (fb_helper->crtc_count > 1)
1296 if (!drm_mode_equal(modes[o], modes[n]))
1301 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1302 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1304 if (score > best_score) {
1307 memcpy(best_crtcs, crtcs,
1308 dev->mode_config.num_connector *
1309 sizeof(struct drm_fb_helper_crtc *));
1313 drm_free(crtcs, DRM_MEM_KMS);
1317 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1319 struct drm_device *dev = fb_helper->dev;
1320 struct drm_fb_helper_crtc **crtcs;
1321 struct drm_display_mode **modes;
1322 struct drm_encoder *encoder;
1323 struct drm_mode_set *modeset;
1328 DRM_DEBUG_KMS("\n");
1330 width = dev->mode_config.max_width;
1331 height = dev->mode_config.max_height;
1333 /* clean out all the encoder/crtc combos */
1334 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1335 encoder->crtc = NULL;
1338 crtcs = kmalloc(dev->mode_config.num_connector *
1339 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1341 modes = kmalloc(dev->mode_config.num_connector *
1342 sizeof(struct drm_display_mode *), DRM_MEM_KMS,
1344 enabled = kmalloc(dev->mode_config.num_connector *
1345 sizeof(bool), DRM_MEM_KMS, M_WAITOK | M_ZERO);
1347 drm_enable_connectors(fb_helper, enabled);
1349 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1351 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1353 DRM_ERROR("Unable to find initial modes\n");
1356 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1358 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1360 /* need to set the modesets up here for use later */
1361 /* fill out the connector<->crtc mappings into the modesets */
1362 for (i = 0; i < fb_helper->crtc_count; i++) {
1363 modeset = &fb_helper->crtc_info[i].mode_set;
1364 modeset->num_connectors = 0;
1367 for (i = 0; i < fb_helper->connector_count; i++) {
1368 struct drm_display_mode *mode = modes[i];
1369 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1370 modeset = &fb_crtc->mode_set;
1372 if (mode && fb_crtc) {
1373 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1374 mode->name, fb_crtc->mode_set.crtc->base.id);
1375 fb_crtc->desired_mode = mode;
1377 drm_mode_destroy(dev, modeset->mode);
1378 modeset->mode = drm_mode_duplicate(dev,
1379 fb_crtc->desired_mode);
1380 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1384 drm_free(crtcs, DRM_MEM_KMS);
1385 drm_free(modes, DRM_MEM_KMS);
1386 drm_free(enabled, DRM_MEM_KMS);
1390 * drm_helper_initial_config - setup a sane initial connector configuration
1394 * Called at init time, must take mode config lock.
1396 * Scan the CRTCs and connectors and try to put together an initial setup.
1397 * At the moment, this is a cloned configuration across all heads with
1398 * a new framebuffer object as the backing store.
1401 * Zero if everything went ok, nonzero otherwise.
1403 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1405 struct drm_device *dev = fb_helper->dev;
1408 /* disable all the possible outputs/crtcs before entering KMS mode */
1409 drm_helper_disable_unused_functions(fb_helper->dev);
1411 drm_fb_helper_parse_command_line(fb_helper);
1413 count = drm_fb_helper_probe_connector_modes(fb_helper,
1414 dev->mode_config.max_width,
1415 dev->mode_config.max_height);
1417 * we shouldn't end up with no modes here.
1420 kprintf("No connectors reported connected with modes\n");
1422 drm_setup_crtcs(fb_helper);
1424 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1427 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1429 struct drm_device *dev = fb_helper->dev;
1431 u32 max_width, max_height, bpp_sel;
1432 bool bound = false, crtcs_bound = false;
1433 struct drm_crtc *crtc;
1438 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
1439 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1442 if (crtc->fb == fb_helper->fb)
1446 if (!bound && crtcs_bound) {
1447 fb_helper->delayed_hotplug = true;
1448 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
1451 DRM_DEBUG_KMS("\n");
1453 max_width = fb_helper->fb->width;
1454 max_height = fb_helper->fb->height;
1455 bpp_sel = fb_helper->fb->bits_per_pixel;
1457 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1459 drm_setup_crtcs(fb_helper);
1460 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
1462 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);