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>
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <dev/drm2/drmP.h>
35 #include <dev/drm2/drm_crtc.h>
36 #include <dev/drm2/drm_fb_helper.h>
37 #include <dev/drm2/drm_crtc_helper.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
44 struct drm_fb_helper *fb_helper;
45 struct task fb_mode_task;
48 static fb_enter_t vt_kms_postswitch;
49 static void vt_restore_fbdev_mode(void *, int);
51 /* Call restore out of vt(9) locks. */
53 vt_restore_fbdev_mode(void *arg, int pending)
55 struct drm_fb_helper *fb_helper;
56 struct vt_kms_softc *sc;
58 sc = (struct vt_kms_softc *)arg;
59 fb_helper = sc->fb_helper;
60 sx_xlock(&fb_helper->dev->mode_config.mutex);
61 drm_fb_helper_restore_fbdev_mode(fb_helper);
62 sx_xunlock(&fb_helper->dev->mode_config.mutex);
66 vt_kms_postswitch(void *arg)
68 struct vt_kms_softc *sc;
70 sc = (struct vt_kms_softc *)arg;
72 if (!kdb_active && panicstr == NULL)
73 taskqueue_enqueue_fast(taskqueue_thread, &sc->fb_mode_task);
75 drm_fb_helper_restore_fbdev_mode(sc->fb_helper);
80 static DRM_LIST_HEAD(kernel_fb_helper_list);
82 /* simple single crtc case helper function */
83 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
85 struct drm_device *dev = fb_helper->dev;
86 struct drm_connector *connector;
88 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
89 struct drm_fb_helper_connector *fb_helper_connector;
91 fb_helper_connector = malloc(
92 sizeof(struct drm_fb_helper_connector), DRM_MEM_KMS,
95 fb_helper_connector->connector = connector;
96 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
102 fb_get_options(const char *connector_name, char **option)
107 * A user may use loader tunables to set a specific mode for the
108 * console. Tunables are read in the following order:
109 * 1. kern.vt.fb.modes.$connector_name
110 * 2. kern.vt.fb.default_mode
112 * Example of a mode specific to the LVDS connector:
113 * kern.vt.fb.modes.LVDS="1024x768"
115 * Example of a mode applied to all connectors not having a
116 * connector-specific mode:
117 * kern.vt.fb.default_mode="640x480"
119 snprintf(tunable, sizeof(tunable), "kern.vt.fb.modes.%s",
121 DRM_INFO("Connector %s: get mode from tunables:\n", connector_name);
122 DRM_INFO(" - %s\n", tunable);
123 DRM_INFO(" - kern.vt.fb.default_mode\n");
124 *option = kern_getenv(tunable);
126 *option = kern_getenv("kern.vt.fb.default_mode");
128 return (*option != NULL ? 0 : 1);
131 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
133 struct drm_fb_helper_connector *fb_helper_conn;
136 for (i = 0; i < fb_helper->connector_count; i++) {
137 struct drm_cmdline_mode *mode;
138 struct drm_connector *connector;
141 fb_helper_conn = fb_helper->connector_info[i];
142 connector = fb_helper_conn->connector;
143 mode = &fb_helper_conn->cmdline_mode;
145 /* do something on return - turn off connector maybe */
146 if (fb_get_options(drm_get_connector_name(connector), &option))
149 if (drm_mode_parse_command_line_for_connector(option,
154 switch (mode->force) {
158 case DRM_FORCE_ON_DIGITAL:
167 DRM_INFO("forcing %s connector %s\n",
168 drm_get_connector_name(connector), s);
169 connector->force = mode->force;
172 DRM_INFO("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
173 drm_get_connector_name(connector),
174 mode->xres, mode->yres,
175 mode->refresh_specified ? mode->refresh : 60,
176 mode->rb ? " reduced blanking" : "",
177 mode->margins ? " with margins" : "",
178 mode->interlace ? " interlaced" : "");
188 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
190 uint16_t *r_base, *g_base, *b_base;
193 r_base = crtc->gamma_store;
194 g_base = r_base + crtc->gamma_size;
195 b_base = g_base + crtc->gamma_size;
197 for (i = 0; i < crtc->gamma_size; i++)
198 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
201 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
203 uint16_t *r_base, *g_base, *b_base;
205 r_base = crtc->gamma_store;
206 g_base = r_base + crtc->gamma_size;
207 b_base = g_base + crtc->gamma_size;
209 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
214 int drm_fb_helper_debug_enter(struct fb_info *info)
216 struct drm_fb_helper *helper = info->par;
217 struct drm_crtc_helper_funcs *funcs;
220 if (list_empty(&kernel_fb_helper_list))
223 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
224 for (i = 0; i < helper->crtc_count; i++) {
225 struct drm_mode_set *mode_set =
226 &helper->crtc_info[i].mode_set;
228 if (!mode_set->crtc->enabled)
231 funcs = mode_set->crtc->helper_private;
232 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
233 funcs->mode_set_base_atomic(mode_set->crtc,
237 ENTER_ATOMIC_MODE_SET);
246 /* Find the real fb for a given fb helper CRTC */
247 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
249 struct drm_device *dev = crtc->dev;
252 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
253 if (crtc->base.id == c->base.id)
262 int drm_fb_helper_debug_leave(struct fb_info *info)
264 struct drm_fb_helper *helper = info->par;
265 struct drm_crtc *crtc;
266 struct drm_crtc_helper_funcs *funcs;
267 struct drm_framebuffer *fb;
270 for (i = 0; i < helper->crtc_count; i++) {
271 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
272 crtc = mode_set->crtc;
273 funcs = crtc->helper_private;
274 fb = drm_mode_config_fb(crtc);
280 DRM_ERROR("no fb to restore??\n");
284 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
285 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
286 crtc->y, LEAVE_ATOMIC_MODE_SET);
293 bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
297 for (i = 0; i < fb_helper->crtc_count; i++) {
298 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
299 ret = drm_crtc_helper_set_config(mode_set);
307 bool drm_fb_helper_force_kernel_mode(void)
309 bool ret, error = false;
310 struct drm_fb_helper *helper;
312 if (list_empty(&kernel_fb_helper_list))
315 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
316 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
319 ret = drm_fb_helper_restore_fbdev_mode(helper);
328 int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
331 printf("panic occurred, switching back to text console\n");
332 return drm_fb_helper_force_kernel_mode();
336 static struct notifier_block paniced = {
337 .notifier_call = drm_fb_helper_panic,
341 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
343 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
345 void drm_fb_helper_restore(void)
348 ret = drm_fb_helper_force_kernel_mode();
350 DRM_ERROR("Failed to restore crtc configuration\n");
353 #ifdef CONFIG_MAGIC_SYSRQ
354 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
356 drm_fb_helper_restore();
358 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
360 static void drm_fb_helper_sysrq(int dummy1)
362 schedule_work(&drm_fb_helper_restore_work);
365 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
366 .handler = drm_fb_helper_sysrq,
367 .help_msg = "force-fb(V)",
368 .action_msg = "Restore framebuffer console",
371 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
376 static void drm_fb_helper_on(struct fb_info *info)
378 struct drm_fb_helper *fb_helper = info->par;
379 struct drm_device *dev = fb_helper->dev;
380 struct drm_crtc *crtc;
381 struct drm_crtc_helper_funcs *crtc_funcs;
382 struct drm_connector *connector;
383 struct drm_encoder *encoder;
387 * For each CRTC in this fb, turn the crtc on then,
388 * find all associated encoders and turn them on.
390 sx_xlock(&dev->mode_config.mutex);
391 for (i = 0; i < fb_helper->crtc_count; i++) {
392 crtc = fb_helper->crtc_info[i].mode_set.crtc;
393 crtc_funcs = crtc->helper_private;
398 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
400 /* Walk the connectors & encoders on this fb turning them on */
401 for (j = 0; j < fb_helper->connector_count; j++) {
402 connector = fb_helper->connector_info[j]->connector;
403 connector->dpms = DRM_MODE_DPMS_ON;
404 drm_connector_property_set_value(connector,
405 dev->mode_config.dpms_property,
408 /* Found a CRTC on this fb, now find encoders */
409 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
410 if (encoder->crtc == crtc) {
411 struct drm_encoder_helper_funcs *encoder_funcs;
413 encoder_funcs = encoder->helper_private;
414 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
418 sx_xunlock(&dev->mode_config.mutex);
423 static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
425 struct drm_fb_helper *fb_helper = info->par;
426 struct drm_device *dev = fb_helper->dev;
427 struct drm_crtc *crtc;
428 struct drm_crtc_helper_funcs *crtc_funcs;
429 struct drm_connector *connector;
430 struct drm_encoder *encoder;
434 * For each CRTC in this fb, find all associated encoders
435 * and turn them off, then turn off the CRTC.
437 sx_xlock(&dev->mode_config.mutex);
438 for (i = 0; i < fb_helper->crtc_count; i++) {
439 crtc = fb_helper->crtc_info[i].mode_set.crtc;
440 crtc_funcs = crtc->helper_private;
445 /* Walk the connectors on this fb and mark them off */
446 for (j = 0; j < fb_helper->connector_count; j++) {
447 connector = fb_helper->connector_info[j]->connector;
448 connector->dpms = dpms_mode;
449 drm_connector_property_set_value(connector,
450 dev->mode_config.dpms_property,
453 /* Found a CRTC on this fb, now find encoders */
454 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
455 if (encoder->crtc == crtc) {
456 struct drm_encoder_helper_funcs *encoder_funcs;
458 encoder_funcs = encoder->helper_private;
459 encoder_funcs->dpms(encoder, dpms_mode);
462 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
464 sx_xunlock(&dev->mode_config.mutex);
469 int drm_fb_helper_blank(int blank, struct fb_info *info)
472 /* Display: On; HSync: On, VSync: On */
473 case FB_BLANK_UNBLANK:
474 drm_fb_helper_on(info);
476 /* Display: Off; HSync: On, VSync: On */
477 case FB_BLANK_NORMAL:
478 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
480 /* Display: Off; HSync: Off, VSync: On */
481 case FB_BLANK_HSYNC_SUSPEND:
482 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
484 /* Display: Off; HSync: On, VSync: Off */
485 case FB_BLANK_VSYNC_SUSPEND:
486 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
488 /* Display: Off; HSync: Off, VSync: Off */
489 case FB_BLANK_POWERDOWN:
490 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
497 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
501 for (i = 0; i < helper->connector_count; i++)
502 free(helper->connector_info[i], DRM_MEM_KMS);
503 free(helper->connector_info, DRM_MEM_KMS);
504 for (i = 0; i < helper->crtc_count; i++) {
505 free(helper->crtc_info[i].mode_set.connectors, DRM_MEM_KMS);
506 if (helper->crtc_info[i].mode_set.mode)
507 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
509 free(helper->crtc_info, DRM_MEM_KMS);
512 int drm_fb_helper_init(struct drm_device *dev,
513 struct drm_fb_helper *fb_helper,
514 int crtc_count, int max_conn_count)
516 struct drm_crtc *crtc;
519 fb_helper->dev = dev;
521 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
523 fb_helper->crtc_info = malloc(crtc_count *
524 sizeof(struct drm_fb_helper_crtc), DRM_MEM_KMS, M_WAITOK | M_ZERO);
526 fb_helper->crtc_count = crtc_count;
527 fb_helper->connector_info = malloc(dev->mode_config.num_connector *
528 sizeof(struct drm_fb_helper_connector *), DRM_MEM_KMS,
530 fb_helper->connector_count = 0;
532 for (i = 0; i < crtc_count; i++) {
533 fb_helper->crtc_info[i].mode_set.connectors =
534 malloc(max_conn_count * sizeof(struct drm_connector *),
535 DRM_MEM_KMS, M_WAITOK | M_ZERO);
537 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
541 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
542 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
543 fb_helper->crtc_info[i].mode_set.crtc = crtc;
546 fb_helper->conn_limit = max_conn_count;
550 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
552 if (!list_empty(&fb_helper->kernel_fb_list)) {
553 list_del(&fb_helper->kernel_fb_list);
554 if (list_empty(&kernel_fb_helper_list)) {
556 printk(KERN_INFO "drm: unregistered panic notifier\n");
557 atomic_notifier_chain_unregister(&panic_notifier_list,
559 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
564 drm_fb_helper_crtc_free(fb_helper);
569 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
570 u16 blue, u16 regno, struct fb_info *info)
572 struct drm_fb_helper *fb_helper = info->par;
573 struct drm_framebuffer *fb = fb_helper->fb;
576 if (info->fix.visual == FB_VISUAL_trueCOLOR) {
579 /* place color in psuedopalette */
582 palette = (u32 *)info->pseudo_palette;
583 red >>= (16 - info->var.red.length);
584 green >>= (16 - info->var.green.length);
585 blue >>= (16 - info->var.blue.length);
586 value = (red << info->var.red.offset) |
587 (green << info->var.green.offset) |
588 (blue << info->var.blue.offset);
589 if (info->var.transp.length > 0) {
590 u32 mask = (1 << info->var.transp.length) - 1;
591 mask <<= info->var.transp.offset;
594 palette[regno] = value;
600 if (fb->bits_per_pixel == 16) {
603 if (fb->depth == 16 && regno > 63)
605 if (fb->depth == 15 && regno > 31)
608 if (fb->depth == 16) {
612 for (i = 0; i < 8; i++)
613 fb_helper->funcs->gamma_set(crtc, red,
614 green, blue, pindex + i);
617 fb_helper->funcs->gamma_get(crtc, &r,
621 for (i = 0; i < 4; i++)
622 fb_helper->funcs->gamma_set(crtc, r,
629 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
635 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
637 struct drm_fb_helper *fb_helper = info->par;
638 struct drm_crtc_helper_funcs *crtc_funcs;
639 u16 *red, *green, *blue, *transp;
640 struct drm_crtc *crtc;
644 for (i = 0; i < fb_helper->crtc_count; i++) {
645 crtc = fb_helper->crtc_info[i].mode_set.crtc;
646 crtc_funcs = crtc->helper_private;
651 transp = cmap->transp;
654 for (j = 0; j < cmap->len; j++) {
655 u16 hred, hgreen, hblue, htransp = 0xffff;
664 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
668 crtc_funcs->load_lut(crtc);
675 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
676 struct fb_info *info)
678 struct drm_fb_helper *fb_helper = info->par;
679 struct drm_framebuffer *fb = fb_helper->fb;
682 if (var->pixclock != 0 || in_dbg_master())
685 /* Need to resize the fb object !!! */
686 if (var->bits_per_pixel > fb->bits_per_pixel ||
687 var->xres > fb->width || var->yres > fb->height ||
688 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
689 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
690 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
691 var->xres, var->yres, var->bits_per_pixel,
692 var->xres_virtual, var->yres_virtual,
693 fb->width, fb->height, fb->bits_per_pixel);
697 switch (var->bits_per_pixel) {
699 depth = (var->green.length == 6) ? 16 : 15;
702 depth = (var->transp.length > 0) ? 32 : 24;
705 depth = var->bits_per_pixel;
712 var->green.offset = 0;
713 var->blue.offset = 0;
715 var->green.length = 8;
716 var->blue.length = 8;
717 var->transp.length = 0;
718 var->transp.offset = 0;
721 var->red.offset = 10;
722 var->green.offset = 5;
723 var->blue.offset = 0;
725 var->green.length = 5;
726 var->blue.length = 5;
727 var->transp.length = 1;
728 var->transp.offset = 15;
731 var->red.offset = 11;
732 var->green.offset = 5;
733 var->blue.offset = 0;
735 var->green.length = 6;
736 var->blue.length = 5;
737 var->transp.length = 0;
738 var->transp.offset = 0;
741 var->red.offset = 16;
742 var->green.offset = 8;
743 var->blue.offset = 0;
745 var->green.length = 8;
746 var->blue.length = 8;
747 var->transp.length = 0;
748 var->transp.offset = 0;
751 var->red.offset = 16;
752 var->green.offset = 8;
753 var->blue.offset = 0;
755 var->green.length = 8;
756 var->blue.length = 8;
757 var->transp.length = 8;
758 var->transp.offset = 24;
768 /* this will let fbcon do the mode init */
769 int drm_fb_helper_set_par(struct fb_info *info)
771 struct drm_fb_helper *fb_helper = info->par;
772 struct drm_device *dev = fb_helper->dev;
773 struct fb_var_screeninfo *var = &info->var;
774 struct drm_crtc *crtc;
778 if (var->pixclock != 0) {
779 DRM_ERROR("PIXEL CLOCK SET\n");
783 mutex_lock(&dev->mode_config.mutex);
784 for (i = 0; i < fb_helper->crtc_count; i++) {
785 crtc = fb_helper->crtc_info[i].mode_set.crtc;
786 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
788 mutex_unlock(&dev->mode_config.mutex);
792 mutex_unlock(&dev->mode_config.mutex);
794 if (fb_helper->delayed_hotplug) {
795 fb_helper->delayed_hotplug = false;
796 drm_fb_helper_hotplug_event(fb_helper);
803 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
804 struct fb_info *info)
806 struct drm_fb_helper *fb_helper = info->par;
807 struct drm_device *dev = fb_helper->dev;
808 struct drm_mode_set *modeset;
809 struct drm_crtc *crtc;
813 mutex_lock(&dev->mode_config.mutex);
814 for (i = 0; i < fb_helper->crtc_count; i++) {
815 crtc = fb_helper->crtc_info[i].mode_set.crtc;
817 modeset = &fb_helper->crtc_info[i].mode_set;
819 modeset->x = var->xoffset;
820 modeset->y = var->yoffset;
822 if (modeset->num_connectors) {
823 ret = crtc->funcs->set_config(modeset);
825 info->var.xoffset = var->xoffset;
826 info->var.yoffset = var->yoffset;
830 mutex_unlock(&dev->mode_config.mutex);
835 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
841 struct fb_info *info;
842 struct drm_fb_helper_surface_size sizes;
844 struct vt_kms_softc *sc;
847 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
848 sizes.surface_depth = 24;
849 sizes.surface_bpp = 32;
850 sizes.fb_width = (unsigned)-1;
851 sizes.fb_height = (unsigned)-1;
853 /* if driver picks 8 or 16 by default use that
854 for both depth/bpp */
855 if (preferred_bpp != sizes.surface_bpp) {
856 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
858 /* first up get a count of crtcs now in use and new min/maxes width/heights */
859 for (i = 0; i < fb_helper->connector_count; i++) {
860 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
861 struct drm_cmdline_mode *cmdline_mode;
863 cmdline_mode = &fb_helper_conn->cmdline_mode;
865 if (cmdline_mode->bpp_specified) {
866 switch (cmdline_mode->bpp) {
868 sizes.surface_depth = sizes.surface_bpp = 8;
871 sizes.surface_depth = 15;
872 sizes.surface_bpp = 16;
875 sizes.surface_depth = sizes.surface_bpp = 16;
878 sizes.surface_depth = sizes.surface_bpp = 24;
881 sizes.surface_depth = 24;
882 sizes.surface_bpp = 32;
890 for (i = 0; i < fb_helper->crtc_count; i++) {
891 struct drm_display_mode *desired_mode;
892 desired_mode = fb_helper->crtc_info[i].desired_mode;
896 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
897 if (desired_mode->hdisplay < sizes.fb_width)
898 sizes.fb_width = desired_mode->hdisplay;
899 if (desired_mode->vdisplay < sizes.fb_height)
900 sizes.fb_height = desired_mode->vdisplay;
901 if (desired_mode->hdisplay > sizes.surface_width)
902 sizes.surface_width = desired_mode->hdisplay;
903 if (desired_mode->vdisplay > sizes.surface_height)
904 sizes.surface_height = desired_mode->vdisplay;
909 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
910 /* hmm everyone went away - assume VGA cable just fell out
911 and will come back later. */
912 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
913 sizes.fb_width = sizes.surface_width = 1024;
914 sizes.fb_height = sizes.surface_height = 768;
917 /* push down into drivers */
918 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
922 sc = malloc(sizeof(struct vt_kms_softc), DRM_MEM_KMS,
924 sc->fb_helper = fb_helper;
925 TASK_INIT(&sc->fb_mode_task, 0, vt_restore_fbdev_mode, sc);
927 info = fb_helper->fbdev;
929 info->fb_name = device_get_nameunit(fb_helper->dev->device);
930 info->fb_depth = fb_helper->fb->bits_per_pixel;
931 info->fb_height = fb_helper->fb->height;
932 info->fb_width = fb_helper->fb->width;
933 info->fb_stride = fb_helper->fb->pitches[0];
935 info->enter = &vt_kms_postswitch;
937 /* set the fb pointer */
938 for (i = 0; i < fb_helper->crtc_count; i++) {
939 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
946 kdev = fb_helper->dev->device;
947 fbd = device_add_child(kdev, "fbd", device_get_unit(kdev));
949 ret = device_probe_and_attach(fbd);
954 DRM_ERROR("Failed to attach fbd device: %d\n", ret);
961 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
964 info->fix.type = FB_TYPE_PACKED_PIXELS;
965 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
967 info->fix.mmio_start = 0;
968 info->fix.mmio_len = 0;
969 info->fix.type_aux = 0;
970 info->fix.xpanstep = 1; /* doing it in hw */
971 info->fix.ypanstep = 1; /* doing it in hw */
972 info->fix.ywrapstep = 0;
973 info->fix.accel = FB_ACCEL_NONE;
974 info->fix.type_aux = 0;
976 info->fix.line_length = pitch;
980 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
981 uint32_t fb_width, uint32_t fb_height)
983 struct drm_framebuffer *fb = fb_helper->fb;
984 info->pseudo_palette = fb_helper->pseudo_palette;
985 info->var.xres_virtual = fb->width;
986 info->var.yres_virtual = fb->height;
987 info->var.bits_per_pixel = fb->bits_per_pixel;
988 info->var.accel_flags = FB_ACCELF_TEXT;
989 info->var.xoffset = 0;
990 info->var.yoffset = 0;
991 info->var.activate = FB_ACTIVATE_NOW;
992 info->var.height = -1;
993 info->var.width = -1;
997 info->var.red.offset = 0;
998 info->var.green.offset = 0;
999 info->var.blue.offset = 0;
1000 info->var.red.length = 8; /* 8bit DAC */
1001 info->var.green.length = 8;
1002 info->var.blue.length = 8;
1003 info->var.transp.offset = 0;
1004 info->var.transp.length = 0;
1007 info->var.red.offset = 10;
1008 info->var.green.offset = 5;
1009 info->var.blue.offset = 0;
1010 info->var.red.length = 5;
1011 info->var.green.length = 5;
1012 info->var.blue.length = 5;
1013 info->var.transp.offset = 15;
1014 info->var.transp.length = 1;
1017 info->var.red.offset = 11;
1018 info->var.green.offset = 5;
1019 info->var.blue.offset = 0;
1020 info->var.red.length = 5;
1021 info->var.green.length = 6;
1022 info->var.blue.length = 5;
1023 info->var.transp.offset = 0;
1026 info->var.red.offset = 16;
1027 info->var.green.offset = 8;
1028 info->var.blue.offset = 0;
1029 info->var.red.length = 8;
1030 info->var.green.length = 8;
1031 info->var.blue.length = 8;
1032 info->var.transp.offset = 0;
1033 info->var.transp.length = 0;
1036 info->var.red.offset = 16;
1037 info->var.green.offset = 8;
1038 info->var.blue.offset = 0;
1039 info->var.red.length = 8;
1040 info->var.green.length = 8;
1041 info->var.blue.length = 8;
1042 info->var.transp.offset = 24;
1043 info->var.transp.length = 8;
1049 info->var.xres = fb_width;
1050 info->var.yres = fb_height;
1054 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1058 struct drm_connector *connector;
1062 for (i = 0; i < fb_helper->connector_count; i++) {
1063 connector = fb_helper->connector_info[i]->connector;
1064 count += connector->funcs->fill_modes(connector, maxX, maxY);
1070 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1072 struct drm_display_mode *mode;
1074 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1075 if (drm_mode_width(mode) > width ||
1076 drm_mode_height(mode) > height)
1078 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1084 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1086 struct drm_cmdline_mode *cmdline_mode;
1087 cmdline_mode = &fb_connector->cmdline_mode;
1088 return cmdline_mode->specified;
1091 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1092 int width, int height)
1094 struct drm_cmdline_mode *cmdline_mode;
1095 struct drm_display_mode *mode = NULL;
1097 cmdline_mode = &fb_helper_conn->cmdline_mode;
1098 if (cmdline_mode->specified == false)
1101 /* attempt to find a matching mode in the list of modes
1102 * we have gotten so far, if not add a CVT mode that conforms
1104 if (cmdline_mode->rb || cmdline_mode->margins)
1107 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1108 /* check width/height */
1109 if (mode->hdisplay != cmdline_mode->xres ||
1110 mode->vdisplay != cmdline_mode->yres)
1113 if (cmdline_mode->refresh_specified) {
1114 if (mode->vrefresh != cmdline_mode->refresh)
1118 if (cmdline_mode->interlace) {
1119 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1126 if (cmdline_mode->cvt)
1127 mode = drm_cvt_mode(fb_helper_conn->connector->dev,
1128 cmdline_mode->xres, cmdline_mode->yres,
1129 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1130 cmdline_mode->rb, cmdline_mode->interlace,
1131 cmdline_mode->margins);
1133 mode = drm_gtf_mode(fb_helper_conn->connector->dev,
1134 cmdline_mode->xres, cmdline_mode->yres,
1135 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1136 cmdline_mode->interlace,
1137 cmdline_mode->margins);
1138 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1139 list_add(&mode->head, &fb_helper_conn->connector->modes);
1143 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1148 enable = connector->status == connector_status_connected;
1150 enable = connector->status != connector_status_disconnected;
1155 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1158 bool any_enabled = false;
1159 struct drm_connector *connector;
1162 for (i = 0; i < fb_helper->connector_count; i++) {
1163 connector = fb_helper->connector_info[i]->connector;
1164 enabled[i] = drm_connector_enabled(connector, true);
1165 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1166 enabled[i] ? "yes" : "no");
1167 any_enabled |= enabled[i];
1173 for (i = 0; i < fb_helper->connector_count; i++) {
1174 connector = fb_helper->connector_info[i]->connector;
1175 enabled[i] = drm_connector_enabled(connector, false);
1179 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1180 struct drm_display_mode **modes,
1181 bool *enabled, int width, int height)
1184 bool can_clone = false;
1185 struct drm_fb_helper_connector *fb_helper_conn;
1186 struct drm_display_mode *dmt_mode, *mode;
1188 /* only contemplate cloning in the single crtc case */
1189 if (fb_helper->crtc_count > 1)
1193 for (i = 0; i < fb_helper->connector_count; i++) {
1198 /* only contemplate cloning if more than one connector is enabled */
1202 /* check the command line or if nothing common pick 1024x768 */
1204 for (i = 0; i < fb_helper->connector_count; i++) {
1207 fb_helper_conn = fb_helper->connector_info[i];
1208 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1213 for (j = 0; j < i; j++) {
1216 if (!drm_mode_equal(modes[j], modes[i]))
1222 DRM_DEBUG_KMS("can clone using command line\n");
1226 /* try and find a 1024x768 mode on each connector */
1228 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1230 for (i = 0; i < fb_helper->connector_count; i++) {
1235 fb_helper_conn = fb_helper->connector_info[i];
1236 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1237 if (drm_mode_equal(mode, dmt_mode))
1245 DRM_DEBUG_KMS("can clone using 1024x768\n");
1248 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1252 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1253 struct drm_display_mode **modes,
1254 bool *enabled, int width, int height)
1256 struct drm_fb_helper_connector *fb_helper_conn;
1259 for (i = 0; i < fb_helper->connector_count; i++) {
1260 fb_helper_conn = fb_helper->connector_info[i];
1262 if (enabled[i] == false)
1265 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1266 fb_helper_conn->connector->base.id);
1268 /* got for command line mode first */
1269 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1271 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1272 fb_helper_conn->connector->base.id);
1273 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1275 /* No preferred modes, pick one off the list */
1276 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1277 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1280 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1286 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1287 struct drm_fb_helper_crtc **best_crtcs,
1288 struct drm_display_mode **modes,
1289 int n, int width, int height)
1292 struct drm_device *dev = fb_helper->dev;
1293 struct drm_connector *connector;
1294 struct drm_connector_helper_funcs *connector_funcs;
1295 struct drm_encoder *encoder;
1296 struct drm_fb_helper_crtc *best_crtc;
1297 int my_score, best_score, score;
1298 struct drm_fb_helper_crtc **crtcs, *crtc;
1299 struct drm_fb_helper_connector *fb_helper_conn;
1301 if (n == fb_helper->connector_count)
1304 fb_helper_conn = fb_helper->connector_info[n];
1305 connector = fb_helper_conn->connector;
1307 best_crtcs[n] = NULL;
1309 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1310 if (modes[n] == NULL)
1313 crtcs = malloc(dev->mode_config.num_connector *
1314 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1318 if (connector->status == connector_status_connected)
1320 if (drm_has_cmdline_mode(fb_helper_conn))
1322 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1325 connector_funcs = connector->helper_private;
1326 encoder = connector_funcs->best_encoder(connector);
1330 /* select a crtc for this connector and then attempt to configure
1331 remaining connectors */
1332 for (c = 0; c < fb_helper->crtc_count; c++) {
1333 crtc = &fb_helper->crtc_info[c];
1335 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1339 for (o = 0; o < n; o++)
1340 if (best_crtcs[o] == crtc)
1344 /* ignore cloning unless only a single crtc */
1345 if (fb_helper->crtc_count > 1)
1348 if (!drm_mode_equal(modes[o], modes[n]))
1353 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1354 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1356 if (score > best_score) {
1359 memcpy(best_crtcs, crtcs,
1360 dev->mode_config.num_connector *
1361 sizeof(struct drm_fb_helper_crtc *));
1365 free(crtcs, DRM_MEM_KMS);
1369 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1371 struct drm_device *dev = fb_helper->dev;
1372 struct drm_fb_helper_crtc **crtcs;
1373 struct drm_display_mode **modes;
1374 struct drm_encoder *encoder;
1375 struct drm_mode_set *modeset;
1380 DRM_DEBUG_KMS("\n");
1382 width = dev->mode_config.max_width;
1383 height = dev->mode_config.max_height;
1385 /* clean out all the encoder/crtc combos */
1386 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1387 encoder->crtc = NULL;
1390 crtcs = malloc(dev->mode_config.num_connector *
1391 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1393 modes = malloc(dev->mode_config.num_connector *
1394 sizeof(struct drm_display_mode *), DRM_MEM_KMS,
1396 enabled = malloc(dev->mode_config.num_connector *
1397 sizeof(bool), DRM_MEM_KMS, M_WAITOK | M_ZERO);
1399 drm_enable_connectors(fb_helper, enabled);
1401 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1403 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1405 DRM_ERROR("Unable to find initial modes\n");
1408 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1410 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1412 /* need to set the modesets up here for use later */
1413 /* fill out the connector<->crtc mappings into the modesets */
1414 for (i = 0; i < fb_helper->crtc_count; i++) {
1415 modeset = &fb_helper->crtc_info[i].mode_set;
1416 modeset->num_connectors = 0;
1419 for (i = 0; i < fb_helper->connector_count; i++) {
1420 struct drm_display_mode *mode = modes[i];
1421 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1422 modeset = &fb_crtc->mode_set;
1424 if (mode && fb_crtc) {
1425 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1426 mode->name, fb_crtc->mode_set.crtc->base.id);
1427 fb_crtc->desired_mode = mode;
1429 drm_mode_destroy(dev, modeset->mode);
1430 modeset->mode = drm_mode_duplicate(dev,
1431 fb_crtc->desired_mode);
1432 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1436 free(crtcs, DRM_MEM_KMS);
1437 free(modes, DRM_MEM_KMS);
1438 free(enabled, DRM_MEM_KMS);
1442 * drm_helper_initial_config - setup a sane initial connector configuration
1446 * Called at init time, must take mode config lock.
1448 * Scan the CRTCs and connectors and try to put together an initial setup.
1449 * At the moment, this is a cloned configuration across all heads with
1450 * a new framebuffer object as the backing store.
1453 * Zero if everything went ok, nonzero otherwise.
1455 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1457 struct drm_device *dev = fb_helper->dev;
1460 /* disable all the possible outputs/crtcs before entering KMS mode */
1461 drm_helper_disable_unused_functions(fb_helper->dev);
1463 drm_fb_helper_parse_command_line(fb_helper);
1465 count = drm_fb_helper_probe_connector_modes(fb_helper,
1466 dev->mode_config.max_width,
1467 dev->mode_config.max_height);
1469 * we shouldn't end up with no modes here.
1472 printf("No connectors reported connected with modes\n");
1474 drm_setup_crtcs(fb_helper);
1476 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1479 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1481 struct drm_device *dev = fb_helper->dev;
1483 u32 max_width, max_height, bpp_sel;
1484 bool bound = false, crtcs_bound = false;
1485 struct drm_crtc *crtc;
1490 sx_xlock(&dev->mode_config.mutex);
1491 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1494 if (crtc->fb == fb_helper->fb)
1498 if (!bound && crtcs_bound) {
1499 fb_helper->delayed_hotplug = true;
1500 sx_xunlock(&dev->mode_config.mutex);
1503 DRM_DEBUG_KMS("\n");
1505 max_width = fb_helper->fb->width;
1506 max_height = fb_helper->fb->height;
1507 bpp_sel = fb_helper->fb->bits_per_pixel;
1509 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1511 drm_setup_crtcs(fb_helper);
1512 sx_xunlock(&dev->mode_config.mutex);
1514 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);