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 if (helper->crtc_info[i].mode_set.mode)
560 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
562 drm_free(helper->crtc_info, DRM_MEM_KMS);
565 int drm_fb_helper_init(struct drm_device *dev,
566 struct drm_fb_helper *fb_helper,
567 int crtc_count, int max_conn_count)
569 struct drm_crtc *crtc;
572 fb_helper->dev = dev;
574 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
576 fb_helper->crtc_info = kmalloc(crtc_count *
577 sizeof(struct drm_fb_helper_crtc), DRM_MEM_KMS, M_WAITOK | M_ZERO);
579 fb_helper->crtc_count = crtc_count;
580 fb_helper->connector_info = kmalloc(dev->mode_config.num_connector *
581 sizeof(struct drm_fb_helper_connector *), DRM_MEM_KMS,
583 fb_helper->connector_count = 0;
585 for (i = 0; i < crtc_count; i++) {
586 fb_helper->crtc_info[i].mode_set.connectors =
587 kmalloc(max_conn_count * sizeof(struct drm_connector *),
588 DRM_MEM_KMS, M_WAITOK | M_ZERO);
590 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
594 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
595 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
596 fb_helper->crtc_info[i].mode_set.crtc = crtc;
599 fb_helper->conn_limit = max_conn_count;
603 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
605 if (!list_empty(&fb_helper->kernel_fb_list)) {
606 list_del(&fb_helper->kernel_fb_list);
607 if (list_empty(&kernel_fb_helper_list)) {
609 printk(KERN_INFO "drm: unregistered panic notifier\n");
610 atomic_notifier_chain_unregister(&panic_notifier_list,
612 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
617 drm_fb_helper_crtc_free(fb_helper);
622 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
623 u16 blue, u16 regno, struct fb_info *info)
625 struct drm_fb_helper *fb_helper = info->par;
626 struct drm_framebuffer *fb = fb_helper->fb;
629 if (info->fix.visual == FB_VISUAL_trueCOLOR) {
632 /* place color in psuedopalette */
635 palette = (u32 *)info->pseudo_palette;
636 red >>= (16 - info->var.red.length);
637 green >>= (16 - info->var.green.length);
638 blue >>= (16 - info->var.blue.length);
639 value = (red << info->var.red.offset) |
640 (green << info->var.green.offset) |
641 (blue << info->var.blue.offset);
642 if (info->var.transp.length > 0) {
643 u32 mask = (1 << info->var.transp.length) - 1;
644 mask <<= info->var.transp.offset;
647 palette[regno] = value;
653 if (fb->bits_per_pixel == 16) {
656 if (fb->depth == 16 && regno > 63)
658 if (fb->depth == 15 && regno > 31)
661 if (fb->depth == 16) {
665 for (i = 0; i < 8; i++)
666 fb_helper->funcs->gamma_set(crtc, red,
667 green, blue, pindex + i);
670 fb_helper->funcs->gamma_get(crtc, &r,
674 for (i = 0; i < 4; i++)
675 fb_helper->funcs->gamma_set(crtc, r,
682 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
688 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
690 struct drm_fb_helper *fb_helper = info->par;
691 struct drm_crtc_helper_funcs *crtc_funcs;
692 u16 *red, *green, *blue, *transp;
693 struct drm_crtc *crtc;
697 for (i = 0; i < fb_helper->crtc_count; i++) {
698 crtc = fb_helper->crtc_info[i].mode_set.crtc;
699 crtc_funcs = crtc->helper_private;
704 transp = cmap->transp;
707 for (j = 0; j < cmap->len; j++) {
708 u16 hred, hgreen, hblue, htransp = 0xffff;
717 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
721 crtc_funcs->load_lut(crtc);
728 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
729 struct fb_info *info)
731 struct drm_fb_helper *fb_helper = info->par;
732 struct drm_framebuffer *fb = fb_helper->fb;
735 if (var->pixclock != 0 || in_dbg_master())
738 /* Need to resize the fb object !!! */
739 if (var->bits_per_pixel > fb->bits_per_pixel ||
740 var->xres > fb->width || var->yres > fb->height ||
741 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
742 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
743 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
744 var->xres, var->yres, var->bits_per_pixel,
745 var->xres_virtual, var->yres_virtual,
746 fb->width, fb->height, fb->bits_per_pixel);
750 switch (var->bits_per_pixel) {
752 depth = (var->green.length == 6) ? 16 : 15;
755 depth = (var->transp.length > 0) ? 32 : 24;
758 depth = var->bits_per_pixel;
765 var->green.offset = 0;
766 var->blue.offset = 0;
768 var->green.length = 8;
769 var->blue.length = 8;
770 var->transp.length = 0;
771 var->transp.offset = 0;
774 var->red.offset = 10;
775 var->green.offset = 5;
776 var->blue.offset = 0;
778 var->green.length = 5;
779 var->blue.length = 5;
780 var->transp.length = 1;
781 var->transp.offset = 15;
784 var->red.offset = 11;
785 var->green.offset = 5;
786 var->blue.offset = 0;
788 var->green.length = 6;
789 var->blue.length = 5;
790 var->transp.length = 0;
791 var->transp.offset = 0;
794 var->red.offset = 16;
795 var->green.offset = 8;
796 var->blue.offset = 0;
798 var->green.length = 8;
799 var->blue.length = 8;
800 var->transp.length = 0;
801 var->transp.offset = 0;
804 var->red.offset = 16;
805 var->green.offset = 8;
806 var->blue.offset = 0;
808 var->green.length = 8;
809 var->blue.length = 8;
810 var->transp.length = 8;
811 var->transp.offset = 24;
821 /* this will let fbcon do the mode init */
822 int drm_fb_helper_set_par(struct fb_info *info)
824 struct drm_fb_helper *fb_helper = info->par;
825 struct drm_device *dev = fb_helper->dev;
826 struct fb_var_screeninfo *var = &info->var;
827 struct drm_crtc *crtc;
831 if (var->pixclock != 0) {
832 DRM_ERROR("PIXEL CLOCK SET\n");
836 mutex_lock(&dev->mode_config.mutex);
837 for (i = 0; i < fb_helper->crtc_count; i++) {
838 crtc = fb_helper->crtc_info[i].mode_set.crtc;
839 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
841 mutex_unlock(&dev->mode_config.mutex);
845 mutex_unlock(&dev->mode_config.mutex);
847 if (fb_helper->delayed_hotplug) {
848 fb_helper->delayed_hotplug = false;
849 drm_fb_helper_hotplug_event(fb_helper);
856 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
857 struct fb_info *info)
859 struct drm_fb_helper *fb_helper = info->par;
860 struct drm_device *dev = fb_helper->dev;
861 struct drm_mode_set *modeset;
862 struct drm_crtc *crtc;
866 mutex_lock(&dev->mode_config.mutex);
867 for (i = 0; i < fb_helper->crtc_count; i++) {
868 crtc = fb_helper->crtc_info[i].mode_set.crtc;
870 modeset = &fb_helper->crtc_info[i].mode_set;
872 modeset->x = var->xoffset;
873 modeset->y = var->yoffset;
875 if (modeset->num_connectors) {
876 ret = crtc->funcs->set_config(modeset);
878 info->var.xoffset = var->xoffset;
879 info->var.yoffset = var->yoffset;
883 mutex_unlock(&dev->mode_config.mutex);
888 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
895 struct fb_info *info;
897 struct drm_fb_helper_surface_size sizes;
900 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
901 sizes.surface_depth = 24;
902 sizes.surface_bpp = 32;
903 sizes.fb_width = (unsigned)-1;
904 sizes.fb_height = (unsigned)-1;
906 /* if driver picks 8 or 16 by default use that
907 for both depth/bpp */
908 if (preferred_bpp != sizes.surface_bpp) {
909 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
911 /* first up get a count of crtcs now in use and new min/maxes width/heights */
912 for (i = 0; i < fb_helper->connector_count; i++) {
913 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
914 struct drm_fb_helper_cmdline_mode *cmdline_mode;
916 cmdline_mode = &fb_helper_conn->cmdline_mode;
918 if (cmdline_mode->bpp_specified) {
919 switch (cmdline_mode->bpp) {
921 sizes.surface_depth = sizes.surface_bpp = 8;
924 sizes.surface_depth = 15;
925 sizes.surface_bpp = 16;
928 sizes.surface_depth = sizes.surface_bpp = 16;
931 sizes.surface_depth = sizes.surface_bpp = 24;
934 sizes.surface_depth = 24;
935 sizes.surface_bpp = 32;
943 for (i = 0; i < fb_helper->crtc_count; i++) {
944 struct drm_display_mode *desired_mode;
945 desired_mode = fb_helper->crtc_info[i].desired_mode;
949 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
950 if (desired_mode->hdisplay < sizes.fb_width)
951 sizes.fb_width = desired_mode->hdisplay;
952 if (desired_mode->vdisplay < sizes.fb_height)
953 sizes.fb_height = desired_mode->vdisplay;
954 if (desired_mode->hdisplay > sizes.surface_width)
955 sizes.surface_width = desired_mode->hdisplay;
956 if (desired_mode->vdisplay > sizes.surface_height)
957 sizes.surface_height = desired_mode->vdisplay;
962 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
963 /* hmm everyone went away - assume VGA cable just fell out
964 and will come back later. */
965 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
966 sizes.fb_width = sizes.surface_width = 1024;
967 sizes.fb_height = sizes.surface_height = 768;
970 /* push down into drivers */
971 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
976 info = fb_helper->fbdev;
979 /* set the fb pointer */
980 for (i = 0; i < fb_helper->crtc_count; i++) {
981 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
986 info->var.pixclock = 0;
987 if (register_framebuffer(info) < 0) {
991 printf("fb%d: %s frame buffer device\n", info->node,
995 drm_fb_helper_set_par(info);
998 /* Switch back to kernel console on panic */
999 /* multi card linked list maybe */
1000 if (list_empty(&kernel_fb_helper_list)) {
1001 printf("drm: registered panic notifier\n");
1002 atomic_notifier_chain_register(&panic_notifier_list,
1006 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1013 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1016 info->fix.type = FB_TYPE_PACKED_PIXELS;
1017 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1018 FB_VISUAL_trueCOLOR;
1019 info->fix.mmio_start = 0;
1020 info->fix.mmio_len = 0;
1021 info->fix.type_aux = 0;
1022 info->fix.xpanstep = 1; /* doing it in hw */
1023 info->fix.ypanstep = 1; /* doing it in hw */
1024 info->fix.ywrapstep = 0;
1025 info->fix.accel = FB_ACCEL_NONE;
1026 info->fix.type_aux = 0;
1028 info->fix.line_length = pitch;
1032 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1033 uint32_t fb_width, uint32_t fb_height)
1035 struct drm_framebuffer *fb = fb_helper->fb;
1036 info->pseudo_palette = fb_helper->pseudo_palette;
1037 info->var.xres_virtual = fb->width;
1038 info->var.yres_virtual = fb->height;
1039 info->var.bits_per_pixel = fb->bits_per_pixel;
1040 info->var.accel_flags = FB_ACCELF_TEXT;
1041 info->var.xoffset = 0;
1042 info->var.yoffset = 0;
1043 info->var.activate = FB_ACTIVATE_NOW;
1044 info->var.height = -1;
1045 info->var.width = -1;
1047 switch (fb->depth) {
1049 info->var.red.offset = 0;
1050 info->var.green.offset = 0;
1051 info->var.blue.offset = 0;
1052 info->var.red.length = 8; /* 8bit DAC */
1053 info->var.green.length = 8;
1054 info->var.blue.length = 8;
1055 info->var.transp.offset = 0;
1056 info->var.transp.length = 0;
1059 info->var.red.offset = 10;
1060 info->var.green.offset = 5;
1061 info->var.blue.offset = 0;
1062 info->var.red.length = 5;
1063 info->var.green.length = 5;
1064 info->var.blue.length = 5;
1065 info->var.transp.offset = 15;
1066 info->var.transp.length = 1;
1069 info->var.red.offset = 11;
1070 info->var.green.offset = 5;
1071 info->var.blue.offset = 0;
1072 info->var.red.length = 5;
1073 info->var.green.length = 6;
1074 info->var.blue.length = 5;
1075 info->var.transp.offset = 0;
1078 info->var.red.offset = 16;
1079 info->var.green.offset = 8;
1080 info->var.blue.offset = 0;
1081 info->var.red.length = 8;
1082 info->var.green.length = 8;
1083 info->var.blue.length = 8;
1084 info->var.transp.offset = 0;
1085 info->var.transp.length = 0;
1088 info->var.red.offset = 16;
1089 info->var.green.offset = 8;
1090 info->var.blue.offset = 0;
1091 info->var.red.length = 8;
1092 info->var.green.length = 8;
1093 info->var.blue.length = 8;
1094 info->var.transp.offset = 24;
1095 info->var.transp.length = 8;
1101 info->var.xres = fb_width;
1102 info->var.yres = fb_height;
1106 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1110 struct drm_connector *connector;
1114 for (i = 0; i < fb_helper->connector_count; i++) {
1115 connector = fb_helper->connector_info[i]->connector;
1116 count += connector->funcs->fill_modes(connector, maxX, maxY);
1122 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1124 struct drm_display_mode *mode;
1126 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1127 if (drm_mode_width(mode) > width ||
1128 drm_mode_height(mode) > height)
1130 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1136 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1138 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1139 cmdline_mode = &fb_connector->cmdline_mode;
1140 return cmdline_mode->specified;
1143 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1144 int width, int height)
1146 struct drm_cmdline_mode *cmdline_mode;
1147 struct drm_display_mode *mode = NULL;
1149 cmdline_mode = &fb_helper_conn->cmdline_mode1;
1150 if (cmdline_mode->specified == false &&
1151 !drm_fetch_cmdline_mode_from_kenv(fb_helper_conn->connector,
1155 /* attempt to find a matching mode in the list of modes
1156 * we have gotten so far, if not add a CVT mode that conforms
1158 if (cmdline_mode->rb || cmdline_mode->margins)
1161 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1162 /* check width/height */
1163 if (mode->hdisplay != cmdline_mode->xres ||
1164 mode->vdisplay != cmdline_mode->yres)
1167 if (cmdline_mode->refresh_specified) {
1168 if (mode->vrefresh != cmdline_mode->refresh)
1172 if (cmdline_mode->interlace) {
1173 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1180 if (cmdline_mode->cvt)
1181 mode = drm_cvt_mode(fb_helper_conn->connector->dev,
1182 cmdline_mode->xres, cmdline_mode->yres,
1183 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1184 cmdline_mode->rb, cmdline_mode->interlace,
1185 cmdline_mode->margins);
1187 mode = drm_gtf_mode(fb_helper_conn->connector->dev,
1188 cmdline_mode->xres, cmdline_mode->yres,
1189 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1190 cmdline_mode->interlace,
1191 cmdline_mode->margins);
1192 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1193 list_add(&mode->head, &fb_helper_conn->connector->modes);
1197 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1202 enable = connector->status == connector_status_connected;
1204 enable = connector->status != connector_status_disconnected;
1209 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1212 bool any_enabled = false;
1213 struct drm_connector *connector;
1216 for (i = 0; i < fb_helper->connector_count; i++) {
1217 connector = fb_helper->connector_info[i]->connector;
1218 enabled[i] = drm_connector_enabled(connector, true);
1219 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1220 enabled[i] ? "yes" : "no");
1221 any_enabled |= enabled[i];
1227 for (i = 0; i < fb_helper->connector_count; i++) {
1228 connector = fb_helper->connector_info[i]->connector;
1229 enabled[i] = drm_connector_enabled(connector, false);
1233 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1234 struct drm_display_mode **modes,
1235 bool *enabled, int width, int height)
1238 bool can_clone = false;
1239 struct drm_fb_helper_connector *fb_helper_conn;
1240 struct drm_display_mode *dmt_mode, *mode;
1242 /* only contemplate cloning in the single crtc case */
1243 if (fb_helper->crtc_count > 1)
1247 for (i = 0; i < fb_helper->connector_count; i++) {
1252 /* only contemplate cloning if more than one connector is enabled */
1256 /* check the command line or if nothing common pick 1024x768 */
1258 for (i = 0; i < fb_helper->connector_count; i++) {
1261 fb_helper_conn = fb_helper->connector_info[i];
1262 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1267 for (j = 0; j < i; j++) {
1270 if (!drm_mode_equal(modes[j], modes[i]))
1276 DRM_DEBUG_KMS("can clone using command line\n");
1280 /* try and find a 1024x768 mode on each connector */
1282 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1284 for (i = 0; i < fb_helper->connector_count; i++) {
1289 fb_helper_conn = fb_helper->connector_info[i];
1290 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1291 if (drm_mode_equal(mode, dmt_mode))
1299 DRM_DEBUG_KMS("can clone using 1024x768\n");
1302 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1306 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1307 struct drm_display_mode **modes,
1308 bool *enabled, int width, int height)
1310 struct drm_fb_helper_connector *fb_helper_conn;
1313 for (i = 0; i < fb_helper->connector_count; i++) {
1314 fb_helper_conn = fb_helper->connector_info[i];
1316 if (enabled[i] == false)
1319 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1320 fb_helper_conn->connector->base.id);
1322 /* got for command line mode first */
1323 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1325 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1326 fb_helper_conn->connector->base.id);
1327 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1329 /* No preferred modes, pick one off the list */
1330 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1331 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1334 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1340 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1341 struct drm_fb_helper_crtc **best_crtcs,
1342 struct drm_display_mode **modes,
1343 int n, int width, int height)
1346 struct drm_device *dev = fb_helper->dev;
1347 struct drm_connector *connector;
1348 struct drm_connector_helper_funcs *connector_funcs;
1349 struct drm_encoder *encoder;
1350 struct drm_fb_helper_crtc *best_crtc;
1351 int my_score, best_score, score;
1352 struct drm_fb_helper_crtc **crtcs, *crtc;
1353 struct drm_fb_helper_connector *fb_helper_conn;
1355 if (n == fb_helper->connector_count)
1358 fb_helper_conn = fb_helper->connector_info[n];
1359 connector = fb_helper_conn->connector;
1361 best_crtcs[n] = NULL;
1363 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1364 if (modes[n] == NULL)
1367 crtcs = kmalloc(dev->mode_config.num_connector *
1368 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1372 if (connector->status == connector_status_connected)
1374 if (drm_has_cmdline_mode(fb_helper_conn))
1376 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1379 connector_funcs = connector->helper_private;
1380 encoder = connector_funcs->best_encoder(connector);
1384 /* select a crtc for this connector and then attempt to configure
1385 remaining connectors */
1386 for (c = 0; c < fb_helper->crtc_count; c++) {
1387 crtc = &fb_helper->crtc_info[c];
1389 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1393 for (o = 0; o < n; o++)
1394 if (best_crtcs[o] == crtc)
1398 /* ignore cloning unless only a single crtc */
1399 if (fb_helper->crtc_count > 1)
1402 if (!drm_mode_equal(modes[o], modes[n]))
1407 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1408 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1410 if (score > best_score) {
1413 memcpy(best_crtcs, crtcs,
1414 dev->mode_config.num_connector *
1415 sizeof(struct drm_fb_helper_crtc *));
1419 drm_free(crtcs, DRM_MEM_KMS);
1423 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1425 struct drm_device *dev = fb_helper->dev;
1426 struct drm_fb_helper_crtc **crtcs;
1427 struct drm_display_mode **modes;
1428 struct drm_encoder *encoder;
1429 struct drm_mode_set *modeset;
1434 DRM_DEBUG_KMS("\n");
1436 width = dev->mode_config.max_width;
1437 height = dev->mode_config.max_height;
1439 /* clean out all the encoder/crtc combos */
1440 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1441 encoder->crtc = NULL;
1444 crtcs = kmalloc(dev->mode_config.num_connector *
1445 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1447 modes = kmalloc(dev->mode_config.num_connector *
1448 sizeof(struct drm_display_mode *), DRM_MEM_KMS,
1450 enabled = kmalloc(dev->mode_config.num_connector *
1451 sizeof(bool), DRM_MEM_KMS, M_WAITOK | M_ZERO);
1453 drm_enable_connectors(fb_helper, enabled);
1455 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1457 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1459 DRM_ERROR("Unable to find initial modes\n");
1462 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1464 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1466 /* need to set the modesets up here for use later */
1467 /* fill out the connector<->crtc mappings into the modesets */
1468 for (i = 0; i < fb_helper->crtc_count; i++) {
1469 modeset = &fb_helper->crtc_info[i].mode_set;
1470 modeset->num_connectors = 0;
1473 for (i = 0; i < fb_helper->connector_count; i++) {
1474 struct drm_display_mode *mode = modes[i];
1475 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1476 modeset = &fb_crtc->mode_set;
1478 if (mode && fb_crtc) {
1479 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1480 mode->name, fb_crtc->mode_set.crtc->base.id);
1481 fb_crtc->desired_mode = mode;
1483 drm_mode_destroy(dev, modeset->mode);
1484 modeset->mode = drm_mode_duplicate(dev,
1485 fb_crtc->desired_mode);
1486 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1490 drm_free(crtcs, DRM_MEM_KMS);
1491 drm_free(modes, DRM_MEM_KMS);
1492 drm_free(enabled, DRM_MEM_KMS);
1496 * drm_helper_initial_config - setup a sane initial connector configuration
1500 * Called at init time, must take mode config lock.
1502 * Scan the CRTCs and connectors and try to put together an initial setup.
1503 * At the moment, this is a cloned configuration across all heads with
1504 * a new framebuffer object as the backing store.
1507 * Zero if everything went ok, nonzero otherwise.
1509 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1511 struct drm_device *dev = fb_helper->dev;
1514 /* disable all the possible outputs/crtcs before entering KMS mode */
1515 drm_helper_disable_unused_functions(fb_helper->dev);
1517 drm_fb_helper_parse_command_line(fb_helper);
1519 count = drm_fb_helper_probe_connector_modes(fb_helper,
1520 dev->mode_config.max_width,
1521 dev->mode_config.max_height);
1523 * we shouldn't end up with no modes here.
1526 kprintf("No connectors reported connected with modes\n");
1528 drm_setup_crtcs(fb_helper);
1530 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1533 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1535 struct drm_device *dev = fb_helper->dev;
1537 u32 max_width, max_height, bpp_sel;
1538 bool bound = false, crtcs_bound = false;
1539 struct drm_crtc *crtc;
1544 lockmgr(&dev->mode_config.lock, LK_EXCLUSIVE);
1545 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1548 if (crtc->fb == fb_helper->fb)
1552 if (!bound && crtcs_bound) {
1553 fb_helper->delayed_hotplug = true;
1554 lockmgr(&dev->mode_config.lock, LK_RELEASE);
1557 DRM_DEBUG_KMS("\n");
1559 max_width = fb_helper->fb->width;
1560 max_height = fb_helper->fb->height;
1561 bpp_sel = fb_helper->fb->bits_per_pixel;
1563 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1565 drm_setup_crtcs(fb_helper);
1566 lockmgr(&dev->mode_config.lock, LK_RELEASE);
1568 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);