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 <dev/drm/drmP.h>
34 #include <dev/drm/drm_crtc.h>
35 #include <dev/drm/drm_fb_helper.h>
36 #include <dev/drm/drm_crtc_helper.h>
38 static DRM_LIST_HEAD(kernel_fb_helper_list);
40 /* simple single crtc case helper function */
41 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
43 struct drm_device *dev = fb_helper->dev;
44 struct drm_connector *connector;
46 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
47 struct drm_fb_helper_connector *fb_helper_connector;
49 fb_helper_connector = kmalloc(
50 sizeof(struct drm_fb_helper_connector), DRM_MEM_KMS,
53 fb_helper_connector->connector = connector;
54 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
59 const char *fb_mode_option;
62 * drm_fb_helper_connector_parse_command_line - parse command line for connector
63 * @connector - connector to parse line for
64 * @mode_option - per connector mode option
66 * This parses the connector specific then generic command lines for
67 * modes and options to configure the connector.
69 * This uses the same parameters as the fb modedb.c, except for extra
70 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
72 * enable/enable Digital/disable bit at the end
74 static bool drm_fb_helper_connector_parse_command_line(struct drm_fb_helper_connector *fb_helper_conn,
75 const char *mode_option)
79 int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
80 unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
81 int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
83 enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
84 struct drm_fb_helper_cmdline_mode *cmdline_mode;
85 struct drm_connector *connector;
89 connector = fb_helper_conn->connector;
91 cmdline_mode = &fb_helper_conn->cmdline_mode;
93 mode_option = fb_mode_option;
96 cmdline_mode->specified = false;
101 namelen = strlen(name);
102 for (i = namelen-1; i >= 0; i--) {
106 if (!refresh_specified && !bpp_specified &&
108 refresh = strtol(&name[i+1], NULL, 10);
109 refresh_specified = 1;
117 if (!bpp_specified && !yres_specified) {
118 bpp = strtol(&name[i+1], NULL, 10);
126 if (!yres_specified) {
127 yres = strtol(&name[i+1], NULL, 10);
150 force = DRM_FORCE_ON;
153 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
154 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
155 force = DRM_FORCE_ON;
157 force = DRM_FORCE_ON_DIGITAL;
160 force = DRM_FORCE_OFF;
166 if (i < 0 && yres_specified) {
167 xres = strtol(name, NULL, 10);
172 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
173 drm_get_connector_name(connector), xres, yres,
174 (refresh) ? refresh : 60, (rb) ? " reduced blanking" :
175 "", (margins) ? " with margins" : "", (interlace) ?
181 case DRM_FORCE_OFF: s = "OFF"; break;
182 case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
184 case DRM_FORCE_ON: s = "ON"; break;
187 DRM_INFO("forcing %s connector %s\n",
188 drm_get_connector_name(connector), s);
189 connector->force = force;
193 cmdline_mode->specified = true;
194 cmdline_mode->xres = xres;
195 cmdline_mode->yres = yres;
198 if (refresh_specified) {
199 cmdline_mode->refresh_specified = true;
200 cmdline_mode->refresh = refresh;
204 cmdline_mode->bpp_specified = true;
205 cmdline_mode->bpp = bpp;
207 cmdline_mode->rb = rb ? true : false;
208 cmdline_mode->cvt = cvt ? true : false;
209 cmdline_mode->interlace = interlace ? true : false;
215 fb_get_options(const char *connector_name, char **option)
221 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
223 struct drm_fb_helper_connector *fb_helper_conn;
226 for (i = 0; i < fb_helper->connector_count; i++) {
229 fb_helper_conn = fb_helper->connector_info[i];
231 /* do something on return - turn off connector maybe */
232 if (fb_get_options(drm_get_connector_name(fb_helper_conn->connector), &option))
235 drm_fb_helper_connector_parse_command_line(fb_helper_conn, option);
241 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
243 uint16_t *r_base, *g_base, *b_base;
246 r_base = crtc->gamma_store;
247 g_base = r_base + crtc->gamma_size;
248 b_base = g_base + crtc->gamma_size;
250 for (i = 0; i < crtc->gamma_size; i++)
251 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
254 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
256 uint16_t *r_base, *g_base, *b_base;
258 r_base = crtc->gamma_store;
259 g_base = r_base + crtc->gamma_size;
260 b_base = g_base + crtc->gamma_size;
262 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
267 int drm_fb_helper_debug_enter(struct fb_info *info)
269 struct drm_fb_helper *helper = info->par;
270 struct drm_crtc_helper_funcs *funcs;
273 if (list_empty(&kernel_fb_helper_list))
276 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
277 for (i = 0; i < helper->crtc_count; i++) {
278 struct drm_mode_set *mode_set =
279 &helper->crtc_info[i].mode_set;
281 if (!mode_set->crtc->enabled)
284 funcs = mode_set->crtc->helper_private;
285 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
286 funcs->mode_set_base_atomic(mode_set->crtc,
290 ENTER_ATOMIC_MODE_SET);
299 /* Find the real fb for a given fb helper CRTC */
300 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
302 struct drm_device *dev = crtc->dev;
305 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
306 if (crtc->base.id == c->base.id)
315 int drm_fb_helper_debug_leave(struct fb_info *info)
317 struct drm_fb_helper *helper = info->par;
318 struct drm_crtc *crtc;
319 struct drm_crtc_helper_funcs *funcs;
320 struct drm_framebuffer *fb;
323 for (i = 0; i < helper->crtc_count; i++) {
324 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
325 crtc = mode_set->crtc;
326 funcs = crtc->helper_private;
327 fb = drm_mode_config_fb(crtc);
333 DRM_ERROR("no fb to restore??\n");
337 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
338 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
339 crtc->y, LEAVE_ATOMIC_MODE_SET);
346 bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
350 for (i = 0; i < fb_helper->crtc_count; i++) {
351 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
352 ret = drm_crtc_helper_set_config(mode_set);
360 bool drm_fb_helper_force_kernel_mode(void)
362 bool ret, error = false;
363 struct drm_fb_helper *helper;
365 if (list_empty(&kernel_fb_helper_list))
368 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
369 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
372 ret = drm_fb_helper_restore_fbdev_mode(helper);
381 int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
384 printf("panic occurred, switching back to text console\n");
385 return drm_fb_helper_force_kernel_mode();
389 static struct notifier_block paniced = {
390 .notifier_call = drm_fb_helper_panic,
394 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
396 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
398 void drm_fb_helper_restore(void)
401 ret = drm_fb_helper_force_kernel_mode();
403 DRM_ERROR("Failed to restore crtc configuration\n");
406 #ifdef CONFIG_MAGIC_SYSRQ
407 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
409 drm_fb_helper_restore();
411 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
413 static void drm_fb_helper_sysrq(int dummy1)
415 schedule_work(&drm_fb_helper_restore_work);
418 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
419 .handler = drm_fb_helper_sysrq,
420 .help_msg = "force-fb(V)",
421 .action_msg = "Restore framebuffer console",
424 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
429 static void drm_fb_helper_on(struct fb_info *info)
431 struct drm_fb_helper *fb_helper = info->par;
432 struct drm_device *dev = fb_helper->dev;
433 struct drm_crtc *crtc;
434 struct drm_crtc_helper_funcs *crtc_funcs;
435 struct drm_connector *connector;
436 struct drm_encoder *encoder;
440 * For each CRTC in this fb, turn the crtc on then,
441 * find all associated encoders and turn them on.
443 sx_xlock(&dev->mode_config.mutex);
444 for (i = 0; i < fb_helper->crtc_count; i++) {
445 crtc = fb_helper->crtc_info[i].mode_set.crtc;
446 crtc_funcs = crtc->helper_private;
451 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
453 /* Walk the connectors & encoders on this fb turning them on */
454 for (j = 0; j < fb_helper->connector_count; j++) {
455 connector = fb_helper->connector_info[j]->connector;
456 connector->dpms = DRM_MODE_DPMS_ON;
457 drm_connector_property_set_value(connector,
458 dev->mode_config.dpms_property,
461 /* Found a CRTC on this fb, now find encoders */
462 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
463 if (encoder->crtc == crtc) {
464 struct drm_encoder_helper_funcs *encoder_funcs;
466 encoder_funcs = encoder->helper_private;
467 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
471 sx_xunlock(&dev->mode_config.mutex);
476 static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
478 struct drm_fb_helper *fb_helper = info->par;
479 struct drm_device *dev = fb_helper->dev;
480 struct drm_crtc *crtc;
481 struct drm_crtc_helper_funcs *crtc_funcs;
482 struct drm_connector *connector;
483 struct drm_encoder *encoder;
487 * For each CRTC in this fb, find all associated encoders
488 * and turn them off, then turn off the CRTC.
490 sx_xlock(&dev->mode_config.mutex);
491 for (i = 0; i < fb_helper->crtc_count; i++) {
492 crtc = fb_helper->crtc_info[i].mode_set.crtc;
493 crtc_funcs = crtc->helper_private;
498 /* Walk the connectors on this fb and mark them off */
499 for (j = 0; j < fb_helper->connector_count; j++) {
500 connector = fb_helper->connector_info[j]->connector;
501 connector->dpms = dpms_mode;
502 drm_connector_property_set_value(connector,
503 dev->mode_config.dpms_property,
506 /* Found a CRTC on this fb, now find encoders */
507 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
508 if (encoder->crtc == crtc) {
509 struct drm_encoder_helper_funcs *encoder_funcs;
511 encoder_funcs = encoder->helper_private;
512 encoder_funcs->dpms(encoder, dpms_mode);
515 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
517 sx_xunlock(&dev->mode_config.mutex);
522 int drm_fb_helper_blank(int blank, struct fb_info *info)
525 /* Display: On; HSync: On, VSync: On */
526 case FB_BLANK_UNBLANK:
527 drm_fb_helper_on(info);
529 /* Display: Off; HSync: On, VSync: On */
530 case FB_BLANK_NORMAL:
531 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
533 /* Display: Off; HSync: Off, VSync: On */
534 case FB_BLANK_HSYNC_SUSPEND:
535 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
537 /* Display: Off; HSync: On, VSync: Off */
538 case FB_BLANK_VSYNC_SUSPEND:
539 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
541 /* Display: Off; HSync: Off, VSync: Off */
542 case FB_BLANK_POWERDOWN:
543 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
550 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
554 for (i = 0; i < helper->connector_count; i++)
555 drm_free(helper->connector_info[i], DRM_MEM_KMS);
556 drm_free(helper->connector_info, DRM_MEM_KMS);
557 for (i = 0; i < helper->crtc_count; i++)
558 drm_free(helper->crtc_info[i].mode_set.connectors, DRM_MEM_KMS);
559 drm_free(helper->crtc_info, DRM_MEM_KMS);
562 int drm_fb_helper_init(struct drm_device *dev,
563 struct drm_fb_helper *fb_helper,
564 int crtc_count, int max_conn_count)
566 struct drm_crtc *crtc;
569 fb_helper->dev = dev;
571 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
573 fb_helper->crtc_info = kmalloc(crtc_count *
574 sizeof(struct drm_fb_helper_crtc), DRM_MEM_KMS, M_WAITOK | M_ZERO);
576 fb_helper->crtc_count = crtc_count;
577 fb_helper->connector_info = kmalloc(dev->mode_config.num_connector *
578 sizeof(struct drm_fb_helper_connector *), DRM_MEM_KMS,
580 fb_helper->connector_count = 0;
582 for (i = 0; i < crtc_count; i++) {
583 fb_helper->crtc_info[i].mode_set.connectors =
584 kmalloc(max_conn_count * sizeof(struct drm_connector *),
585 DRM_MEM_KMS, M_WAITOK | M_ZERO);
587 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
591 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
592 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
593 fb_helper->crtc_info[i].mode_set.crtc = crtc;
596 fb_helper->conn_limit = max_conn_count;
600 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
602 if (!list_empty(&fb_helper->kernel_fb_list)) {
603 list_del(&fb_helper->kernel_fb_list);
604 if (list_empty(&kernel_fb_helper_list)) {
606 printk(KERN_INFO "drm: unregistered panic notifier\n");
607 atomic_notifier_chain_unregister(&panic_notifier_list,
609 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
614 drm_fb_helper_crtc_free(fb_helper);
619 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
620 u16 blue, u16 regno, struct fb_info *info)
622 struct drm_fb_helper *fb_helper = info->par;
623 struct drm_framebuffer *fb = fb_helper->fb;
626 if (info->fix.visual == FB_VISUAL_trueCOLOR) {
629 /* place color in psuedopalette */
632 palette = (u32 *)info->pseudo_palette;
633 red >>= (16 - info->var.red.length);
634 green >>= (16 - info->var.green.length);
635 blue >>= (16 - info->var.blue.length);
636 value = (red << info->var.red.offset) |
637 (green << info->var.green.offset) |
638 (blue << info->var.blue.offset);
639 if (info->var.transp.length > 0) {
640 u32 mask = (1 << info->var.transp.length) - 1;
641 mask <<= info->var.transp.offset;
644 palette[regno] = value;
650 if (fb->bits_per_pixel == 16) {
653 if (fb->depth == 16 && regno > 63)
655 if (fb->depth == 15 && regno > 31)
658 if (fb->depth == 16) {
662 for (i = 0; i < 8; i++)
663 fb_helper->funcs->gamma_set(crtc, red,
664 green, blue, pindex + i);
667 fb_helper->funcs->gamma_get(crtc, &r,
671 for (i = 0; i < 4; i++)
672 fb_helper->funcs->gamma_set(crtc, r,
679 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
685 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
687 struct drm_fb_helper *fb_helper = info->par;
688 struct drm_crtc_helper_funcs *crtc_funcs;
689 u16 *red, *green, *blue, *transp;
690 struct drm_crtc *crtc;
694 for (i = 0; i < fb_helper->crtc_count; i++) {
695 crtc = fb_helper->crtc_info[i].mode_set.crtc;
696 crtc_funcs = crtc->helper_private;
701 transp = cmap->transp;
704 for (j = 0; j < cmap->len; j++) {
705 u16 hred, hgreen, hblue, htransp = 0xffff;
714 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
718 crtc_funcs->load_lut(crtc);
725 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
726 struct fb_info *info)
728 struct drm_fb_helper *fb_helper = info->par;
729 struct drm_framebuffer *fb = fb_helper->fb;
732 if (var->pixclock != 0 || in_dbg_master())
735 /* Need to resize the fb object !!! */
736 if (var->bits_per_pixel > fb->bits_per_pixel ||
737 var->xres > fb->width || var->yres > fb->height ||
738 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
739 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
740 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
741 var->xres, var->yres, var->bits_per_pixel,
742 var->xres_virtual, var->yres_virtual,
743 fb->width, fb->height, fb->bits_per_pixel);
747 switch (var->bits_per_pixel) {
749 depth = (var->green.length == 6) ? 16 : 15;
752 depth = (var->transp.length > 0) ? 32 : 24;
755 depth = var->bits_per_pixel;
762 var->green.offset = 0;
763 var->blue.offset = 0;
765 var->green.length = 8;
766 var->blue.length = 8;
767 var->transp.length = 0;
768 var->transp.offset = 0;
771 var->red.offset = 10;
772 var->green.offset = 5;
773 var->blue.offset = 0;
775 var->green.length = 5;
776 var->blue.length = 5;
777 var->transp.length = 1;
778 var->transp.offset = 15;
781 var->red.offset = 11;
782 var->green.offset = 5;
783 var->blue.offset = 0;
785 var->green.length = 6;
786 var->blue.length = 5;
787 var->transp.length = 0;
788 var->transp.offset = 0;
791 var->red.offset = 16;
792 var->green.offset = 8;
793 var->blue.offset = 0;
795 var->green.length = 8;
796 var->blue.length = 8;
797 var->transp.length = 0;
798 var->transp.offset = 0;
801 var->red.offset = 16;
802 var->green.offset = 8;
803 var->blue.offset = 0;
805 var->green.length = 8;
806 var->blue.length = 8;
807 var->transp.length = 8;
808 var->transp.offset = 24;
818 /* this will let fbcon do the mode init */
819 int drm_fb_helper_set_par(struct fb_info *info)
821 struct drm_fb_helper *fb_helper = info->par;
822 struct drm_device *dev = fb_helper->dev;
823 struct fb_var_screeninfo *var = &info->var;
824 struct drm_crtc *crtc;
828 if (var->pixclock != 0) {
829 DRM_ERROR("PIXEL CLOCK SET\n");
833 mutex_lock(&dev->mode_config.mutex);
834 for (i = 0; i < fb_helper->crtc_count; i++) {
835 crtc = fb_helper->crtc_info[i].mode_set.crtc;
836 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
838 mutex_unlock(&dev->mode_config.mutex);
842 mutex_unlock(&dev->mode_config.mutex);
844 if (fb_helper->delayed_hotplug) {
845 fb_helper->delayed_hotplug = false;
846 drm_fb_helper_hotplug_event(fb_helper);
853 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
854 struct fb_info *info)
856 struct drm_fb_helper *fb_helper = info->par;
857 struct drm_device *dev = fb_helper->dev;
858 struct drm_mode_set *modeset;
859 struct drm_crtc *crtc;
863 mutex_lock(&dev->mode_config.mutex);
864 for (i = 0; i < fb_helper->crtc_count; i++) {
865 crtc = fb_helper->crtc_info[i].mode_set.crtc;
867 modeset = &fb_helper->crtc_info[i].mode_set;
869 modeset->x = var->xoffset;
870 modeset->y = var->yoffset;
872 if (modeset->num_connectors) {
873 ret = crtc->funcs->set_config(modeset);
875 info->var.xoffset = var->xoffset;
876 info->var.yoffset = var->yoffset;
880 mutex_unlock(&dev->mode_config.mutex);
885 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
892 struct fb_info *info;
894 struct drm_fb_helper_surface_size sizes;
897 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
898 sizes.surface_depth = 24;
899 sizes.surface_bpp = 32;
900 sizes.fb_width = (unsigned)-1;
901 sizes.fb_height = (unsigned)-1;
903 /* if driver picks 8 or 16 by default use that
904 for both depth/bpp */
905 if (preferred_bpp != sizes.surface_bpp) {
906 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
908 /* first up get a count of crtcs now in use and new min/maxes width/heights */
909 for (i = 0; i < fb_helper->connector_count; i++) {
910 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
911 struct drm_fb_helper_cmdline_mode *cmdline_mode;
913 cmdline_mode = &fb_helper_conn->cmdline_mode;
915 if (cmdline_mode->bpp_specified) {
916 switch (cmdline_mode->bpp) {
918 sizes.surface_depth = sizes.surface_bpp = 8;
921 sizes.surface_depth = 15;
922 sizes.surface_bpp = 16;
925 sizes.surface_depth = sizes.surface_bpp = 16;
928 sizes.surface_depth = sizes.surface_bpp = 24;
931 sizes.surface_depth = 24;
932 sizes.surface_bpp = 32;
940 for (i = 0; i < fb_helper->crtc_count; i++) {
941 struct drm_display_mode *desired_mode;
942 desired_mode = fb_helper->crtc_info[i].desired_mode;
946 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
947 if (desired_mode->hdisplay < sizes.fb_width)
948 sizes.fb_width = desired_mode->hdisplay;
949 if (desired_mode->vdisplay < sizes.fb_height)
950 sizes.fb_height = desired_mode->vdisplay;
951 if (desired_mode->hdisplay > sizes.surface_width)
952 sizes.surface_width = desired_mode->hdisplay;
953 if (desired_mode->vdisplay > sizes.surface_height)
954 sizes.surface_height = desired_mode->vdisplay;
959 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
960 /* hmm everyone went away - assume VGA cable just fell out
961 and will come back later. */
962 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
963 sizes.fb_width = sizes.surface_width = 1024;
964 sizes.fb_height = sizes.surface_height = 768;
967 /* push down into drivers */
968 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
973 info = fb_helper->fbdev;
976 /* set the fb pointer */
977 for (i = 0; i < fb_helper->crtc_count; i++) {
978 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
983 info->var.pixclock = 0;
984 if (register_framebuffer(info) < 0) {
988 printf("fb%d: %s frame buffer device\n", info->node,
992 drm_fb_helper_set_par(info);
995 /* Switch back to kernel console on panic */
996 /* multi card linked list maybe */
997 if (list_empty(&kernel_fb_helper_list)) {
998 printf("drm: registered panic notifier\n");
999 atomic_notifier_chain_register(&panic_notifier_list,
1003 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1010 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1013 info->fix.type = FB_TYPE_PACKED_PIXELS;
1014 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1015 FB_VISUAL_trueCOLOR;
1016 info->fix.mmio_start = 0;
1017 info->fix.mmio_len = 0;
1018 info->fix.type_aux = 0;
1019 info->fix.xpanstep = 1; /* doing it in hw */
1020 info->fix.ypanstep = 1; /* doing it in hw */
1021 info->fix.ywrapstep = 0;
1022 info->fix.accel = FB_ACCEL_NONE;
1023 info->fix.type_aux = 0;
1025 info->fix.line_length = pitch;
1029 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1030 uint32_t fb_width, uint32_t fb_height)
1032 struct drm_framebuffer *fb = fb_helper->fb;
1033 info->pseudo_palette = fb_helper->pseudo_palette;
1034 info->var.xres_virtual = fb->width;
1035 info->var.yres_virtual = fb->height;
1036 info->var.bits_per_pixel = fb->bits_per_pixel;
1037 info->var.accel_flags = FB_ACCELF_TEXT;
1038 info->var.xoffset = 0;
1039 info->var.yoffset = 0;
1040 info->var.activate = FB_ACTIVATE_NOW;
1041 info->var.height = -1;
1042 info->var.width = -1;
1044 switch (fb->depth) {
1046 info->var.red.offset = 0;
1047 info->var.green.offset = 0;
1048 info->var.blue.offset = 0;
1049 info->var.red.length = 8; /* 8bit DAC */
1050 info->var.green.length = 8;
1051 info->var.blue.length = 8;
1052 info->var.transp.offset = 0;
1053 info->var.transp.length = 0;
1056 info->var.red.offset = 10;
1057 info->var.green.offset = 5;
1058 info->var.blue.offset = 0;
1059 info->var.red.length = 5;
1060 info->var.green.length = 5;
1061 info->var.blue.length = 5;
1062 info->var.transp.offset = 15;
1063 info->var.transp.length = 1;
1066 info->var.red.offset = 11;
1067 info->var.green.offset = 5;
1068 info->var.blue.offset = 0;
1069 info->var.red.length = 5;
1070 info->var.green.length = 6;
1071 info->var.blue.length = 5;
1072 info->var.transp.offset = 0;
1075 info->var.red.offset = 16;
1076 info->var.green.offset = 8;
1077 info->var.blue.offset = 0;
1078 info->var.red.length = 8;
1079 info->var.green.length = 8;
1080 info->var.blue.length = 8;
1081 info->var.transp.offset = 0;
1082 info->var.transp.length = 0;
1085 info->var.red.offset = 16;
1086 info->var.green.offset = 8;
1087 info->var.blue.offset = 0;
1088 info->var.red.length = 8;
1089 info->var.green.length = 8;
1090 info->var.blue.length = 8;
1091 info->var.transp.offset = 24;
1092 info->var.transp.length = 8;
1098 info->var.xres = fb_width;
1099 info->var.yres = fb_height;
1103 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1107 struct drm_connector *connector;
1111 for (i = 0; i < fb_helper->connector_count; i++) {
1112 connector = fb_helper->connector_info[i]->connector;
1113 count += connector->funcs->fill_modes(connector, maxX, maxY);
1119 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1121 struct drm_display_mode *mode;
1123 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1124 if (drm_mode_width(mode) > width ||
1125 drm_mode_height(mode) > height)
1127 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1133 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1135 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1136 cmdline_mode = &fb_connector->cmdline_mode;
1137 return cmdline_mode->specified;
1140 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1141 int width, int height)
1143 struct drm_cmdline_mode *cmdline_mode;
1144 struct drm_display_mode *mode = NULL;
1146 cmdline_mode = &fb_helper_conn->cmdline_mode1;
1147 if (cmdline_mode->specified == false &&
1148 !drm_fetch_cmdline_mode_from_kenv(fb_helper_conn->connector,
1152 /* attempt to find a matching mode in the list of modes
1153 * we have gotten so far, if not add a CVT mode that conforms
1155 if (cmdline_mode->rb || cmdline_mode->margins)
1158 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1159 /* check width/height */
1160 if (mode->hdisplay != cmdline_mode->xres ||
1161 mode->vdisplay != cmdline_mode->yres)
1164 if (cmdline_mode->refresh_specified) {
1165 if (mode->vrefresh != cmdline_mode->refresh)
1169 if (cmdline_mode->interlace) {
1170 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1177 if (cmdline_mode->cvt)
1178 mode = drm_cvt_mode(fb_helper_conn->connector->dev,
1179 cmdline_mode->xres, cmdline_mode->yres,
1180 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1181 cmdline_mode->rb, cmdline_mode->interlace,
1182 cmdline_mode->margins);
1184 mode = drm_gtf_mode(fb_helper_conn->connector->dev,
1185 cmdline_mode->xres, cmdline_mode->yres,
1186 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1187 cmdline_mode->interlace,
1188 cmdline_mode->margins);
1189 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1190 list_add(&mode->head, &fb_helper_conn->connector->modes);
1194 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1199 enable = connector->status == connector_status_connected;
1201 enable = connector->status != connector_status_disconnected;
1206 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1209 bool any_enabled = false;
1210 struct drm_connector *connector;
1213 for (i = 0; i < fb_helper->connector_count; i++) {
1214 connector = fb_helper->connector_info[i]->connector;
1215 enabled[i] = drm_connector_enabled(connector, true);
1216 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1217 enabled[i] ? "yes" : "no");
1218 any_enabled |= enabled[i];
1224 for (i = 0; i < fb_helper->connector_count; i++) {
1225 connector = fb_helper->connector_info[i]->connector;
1226 enabled[i] = drm_connector_enabled(connector, false);
1230 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1231 struct drm_display_mode **modes,
1232 bool *enabled, int width, int height)
1235 bool can_clone = false;
1236 struct drm_fb_helper_connector *fb_helper_conn;
1237 struct drm_display_mode *dmt_mode, *mode;
1239 /* only contemplate cloning in the single crtc case */
1240 if (fb_helper->crtc_count > 1)
1244 for (i = 0; i < fb_helper->connector_count; i++) {
1249 /* only contemplate cloning if more than one connector is enabled */
1253 /* check the command line or if nothing common pick 1024x768 */
1255 for (i = 0; i < fb_helper->connector_count; i++) {
1258 fb_helper_conn = fb_helper->connector_info[i];
1259 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1264 for (j = 0; j < i; j++) {
1267 if (!drm_mode_equal(modes[j], modes[i]))
1273 DRM_DEBUG_KMS("can clone using command line\n");
1277 /* try and find a 1024x768 mode on each connector */
1279 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1281 for (i = 0; i < fb_helper->connector_count; i++) {
1286 fb_helper_conn = fb_helper->connector_info[i];
1287 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1288 if (drm_mode_equal(mode, dmt_mode))
1296 DRM_DEBUG_KMS("can clone using 1024x768\n");
1299 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1303 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1304 struct drm_display_mode **modes,
1305 bool *enabled, int width, int height)
1307 struct drm_fb_helper_connector *fb_helper_conn;
1310 for (i = 0; i < fb_helper->connector_count; i++) {
1311 fb_helper_conn = fb_helper->connector_info[i];
1313 if (enabled[i] == false)
1316 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1317 fb_helper_conn->connector->base.id);
1319 /* got for command line mode first */
1320 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1322 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1323 fb_helper_conn->connector->base.id);
1324 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1326 /* No preferred modes, pick one off the list */
1327 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1328 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1331 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1337 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1338 struct drm_fb_helper_crtc **best_crtcs,
1339 struct drm_display_mode **modes,
1340 int n, int width, int height)
1343 struct drm_device *dev = fb_helper->dev;
1344 struct drm_connector *connector;
1345 struct drm_connector_helper_funcs *connector_funcs;
1346 struct drm_encoder *encoder;
1347 struct drm_fb_helper_crtc *best_crtc;
1348 int my_score, best_score, score;
1349 struct drm_fb_helper_crtc **crtcs, *crtc;
1350 struct drm_fb_helper_connector *fb_helper_conn;
1352 if (n == fb_helper->connector_count)
1355 fb_helper_conn = fb_helper->connector_info[n];
1356 connector = fb_helper_conn->connector;
1358 best_crtcs[n] = NULL;
1360 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1361 if (modes[n] == NULL)
1364 crtcs = kmalloc(dev->mode_config.num_connector *
1365 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1369 if (connector->status == connector_status_connected)
1371 if (drm_has_cmdline_mode(fb_helper_conn))
1373 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1376 connector_funcs = connector->helper_private;
1377 encoder = connector_funcs->best_encoder(connector);
1381 /* select a crtc for this connector and then attempt to configure
1382 remaining connectors */
1383 for (c = 0; c < fb_helper->crtc_count; c++) {
1384 crtc = &fb_helper->crtc_info[c];
1386 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1390 for (o = 0; o < n; o++)
1391 if (best_crtcs[o] == crtc)
1395 /* ignore cloning unless only a single crtc */
1396 if (fb_helper->crtc_count > 1)
1399 if (!drm_mode_equal(modes[o], modes[n]))
1404 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1405 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1407 if (score > best_score) {
1410 memcpy(best_crtcs, crtcs,
1411 dev->mode_config.num_connector *
1412 sizeof(struct drm_fb_helper_crtc *));
1416 drm_free(crtcs, DRM_MEM_KMS);
1420 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1422 struct drm_device *dev = fb_helper->dev;
1423 struct drm_fb_helper_crtc **crtcs;
1424 struct drm_display_mode **modes;
1425 struct drm_encoder *encoder;
1426 struct drm_mode_set *modeset;
1431 DRM_DEBUG_KMS("\n");
1433 width = dev->mode_config.max_width;
1434 height = dev->mode_config.max_height;
1436 /* clean out all the encoder/crtc combos */
1437 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1438 encoder->crtc = NULL;
1441 crtcs = kmalloc(dev->mode_config.num_connector *
1442 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1444 modes = kmalloc(dev->mode_config.num_connector *
1445 sizeof(struct drm_display_mode *), DRM_MEM_KMS,
1447 enabled = kmalloc(dev->mode_config.num_connector *
1448 sizeof(bool), DRM_MEM_KMS, M_WAITOK | M_ZERO);
1450 drm_enable_connectors(fb_helper, enabled);
1452 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1454 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1456 DRM_ERROR("Unable to find initial modes\n");
1459 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1461 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1463 /* need to set the modesets up here for use later */
1464 /* fill out the connector<->crtc mappings into the modesets */
1465 for (i = 0; i < fb_helper->crtc_count; i++) {
1466 modeset = &fb_helper->crtc_info[i].mode_set;
1467 modeset->num_connectors = 0;
1470 for (i = 0; i < fb_helper->connector_count; i++) {
1471 struct drm_display_mode *mode = modes[i];
1472 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1473 modeset = &fb_crtc->mode_set;
1475 if (mode && fb_crtc) {
1476 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1477 mode->name, fb_crtc->mode_set.crtc->base.id);
1478 fb_crtc->desired_mode = mode;
1480 drm_mode_destroy(dev, modeset->mode);
1481 modeset->mode = drm_mode_duplicate(dev,
1482 fb_crtc->desired_mode);
1483 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1487 drm_free(crtcs, DRM_MEM_KMS);
1488 drm_free(modes, DRM_MEM_KMS);
1489 drm_free(enabled, DRM_MEM_KMS);
1493 * drm_helper_initial_config - setup a sane initial connector configuration
1497 * Called at init time, must take mode config lock.
1499 * Scan the CRTCs and connectors and try to put together an initial setup.
1500 * At the moment, this is a cloned configuration across all heads with
1501 * a new framebuffer object as the backing store.
1504 * Zero if everything went ok, nonzero otherwise.
1506 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1508 struct drm_device *dev = fb_helper->dev;
1511 /* disable all the possible outputs/crtcs before entering KMS mode */
1512 drm_helper_disable_unused_functions(fb_helper->dev);
1514 drm_fb_helper_parse_command_line(fb_helper);
1516 count = drm_fb_helper_probe_connector_modes(fb_helper,
1517 dev->mode_config.max_width,
1518 dev->mode_config.max_height);
1520 * we shouldn't end up with no modes here.
1523 kprintf("No connectors reported connected with modes\n");
1525 drm_setup_crtcs(fb_helper);
1527 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1530 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1532 struct drm_device *dev = fb_helper->dev;
1534 u32 max_width, max_height, bpp_sel;
1535 bool bound = false, crtcs_bound = false;
1536 struct drm_crtc *crtc;
1541 lockmgr(&dev->mode_config.lock, LK_EXCLUSIVE);
1542 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1545 if (crtc->fb == fb_helper->fb)
1549 if (!bound && crtcs_bound) {
1550 fb_helper->delayed_hotplug = true;
1551 lockmgr(&dev->mode_config.lock, LK_RELEASE);
1554 DRM_DEBUG_KMS("\n");
1556 max_width = fb_helper->fb->width;
1557 max_height = fb_helper->fb->height;
1558 bpp_sel = fb_helper->fb->bits_per_pixel;
1560 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1562 drm_setup_crtcs(fb_helper);
1563 lockmgr(&dev->mode_config.lock, LK_RELEASE);
1565 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);