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>
39 #if defined(__FreeBSD__)
41 struct drm_fb_helper *fb_helper;
42 struct task fb_mode_task;
45 static fb_enter_t vt_kms_postswitch;
46 static void vt_restore_fbdev_mode(void *, int);
48 /* Call restore out of vt(9) locks. */
50 vt_restore_fbdev_mode(void *arg, int pending)
52 struct vt_kms_softc *sc;
54 sc = (struct vt_kms_softc *)arg;
55 drm_fb_helper_restore_fbdev_mode(sc->fb_helper);
59 vt_kms_postswitch(void *arg)
61 struct vt_kms_softc *sc;
63 sc = (struct vt_kms_softc *)arg;
64 taskqueue_enqueue_fast(taskqueue_thread, &sc->fb_mode_task);
70 static DRM_LIST_HEAD(kernel_fb_helper_list);
72 /* simple single crtc case helper function */
73 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
75 struct drm_device *dev = fb_helper->dev;
76 struct drm_connector *connector;
78 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
79 struct drm_fb_helper_connector *fb_helper_connector;
81 fb_helper_connector = malloc(
82 sizeof(struct drm_fb_helper_connector), DRM_MEM_KMS,
85 fb_helper_connector->connector = connector;
86 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
91 const char *fb_mode_option;
94 * drm_fb_helper_connector_parse_command_line - parse command line for connector
95 * @connector - connector to parse line for
96 * @mode_option - per connector mode option
98 * This parses the connector specific then generic command lines for
99 * modes and options to configure the connector.
101 * This uses the same parameters as the fb modedb.c, except for extra
102 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
104 * enable/enable Digital/disable bit at the end
106 static bool drm_fb_helper_connector_parse_command_line(struct drm_fb_helper_connector *fb_helper_conn,
107 const char *mode_option)
110 unsigned int namelen;
111 int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
112 unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
113 int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
115 enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
116 struct drm_fb_helper_cmdline_mode *cmdline_mode;
117 struct drm_connector *connector;
121 connector = fb_helper_conn->connector;
123 cmdline_mode = &fb_helper_conn->cmdline_mode;
125 mode_option = fb_mode_option;
128 cmdline_mode->specified = false;
133 namelen = strlen(name);
134 for (i = namelen-1; i >= 0; i--) {
138 if (!refresh_specified && !bpp_specified &&
140 refresh = strtol(&name[i+1], NULL, 10);
141 refresh_specified = 1;
149 if (!bpp_specified && !yres_specified) {
150 bpp = strtol(&name[i+1], NULL, 10);
158 if (!yres_specified) {
159 yres = strtol(&name[i+1], NULL, 10);
182 force = DRM_FORCE_ON;
185 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
186 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
187 force = DRM_FORCE_ON;
189 force = DRM_FORCE_ON_DIGITAL;
192 force = DRM_FORCE_OFF;
198 if (i < 0 && yres_specified) {
199 xres = strtol(name, NULL, 10);
204 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
205 drm_get_connector_name(connector), xres, yres,
206 (refresh) ? refresh : 60, (rb) ? " reduced blanking" :
207 "", (margins) ? " with margins" : "", (interlace) ?
213 case DRM_FORCE_OFF: s = "OFF"; break;
214 case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
216 case DRM_FORCE_ON: s = "ON"; break;
219 DRM_INFO("forcing %s connector %s\n",
220 drm_get_connector_name(connector), s);
221 connector->force = force;
225 cmdline_mode->specified = true;
226 cmdline_mode->xres = xres;
227 cmdline_mode->yres = yres;
230 if (refresh_specified) {
231 cmdline_mode->refresh_specified = true;
232 cmdline_mode->refresh = refresh;
236 cmdline_mode->bpp_specified = true;
237 cmdline_mode->bpp = bpp;
239 cmdline_mode->rb = rb ? true : false;
240 cmdline_mode->cvt = cvt ? true : false;
241 cmdline_mode->interlace = interlace ? true : false;
247 fb_get_options(const char *connector_name, char **option)
251 * TODO: store mode options pointer in ${option} for connector with
252 * name ${connector_name}
257 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
259 struct drm_fb_helper_connector *fb_helper_conn;
262 for (i = 0; i < fb_helper->connector_count; i++) {
265 fb_helper_conn = fb_helper->connector_info[i];
267 /* do something on return - turn off connector maybe */
268 if (fb_get_options(drm_get_connector_name(fb_helper_conn->connector), &option))
271 drm_fb_helper_connector_parse_command_line(fb_helper_conn, option);
277 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
279 uint16_t *r_base, *g_base, *b_base;
282 r_base = crtc->gamma_store;
283 g_base = r_base + crtc->gamma_size;
284 b_base = g_base + crtc->gamma_size;
286 for (i = 0; i < crtc->gamma_size; i++)
287 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
290 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
292 uint16_t *r_base, *g_base, *b_base;
294 r_base = crtc->gamma_store;
295 g_base = r_base + crtc->gamma_size;
296 b_base = g_base + crtc->gamma_size;
298 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
303 int drm_fb_helper_debug_enter(struct fb_info *info)
305 struct drm_fb_helper *helper = info->par;
306 struct drm_crtc_helper_funcs *funcs;
309 if (list_empty(&kernel_fb_helper_list))
312 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
313 for (i = 0; i < helper->crtc_count; i++) {
314 struct drm_mode_set *mode_set =
315 &helper->crtc_info[i].mode_set;
317 if (!mode_set->crtc->enabled)
320 funcs = mode_set->crtc->helper_private;
321 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
322 funcs->mode_set_base_atomic(mode_set->crtc,
326 ENTER_ATOMIC_MODE_SET);
335 /* Find the real fb for a given fb helper CRTC */
336 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
338 struct drm_device *dev = crtc->dev;
341 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
342 if (crtc->base.id == c->base.id)
351 int drm_fb_helper_debug_leave(struct fb_info *info)
353 struct drm_fb_helper *helper = info->par;
354 struct drm_crtc *crtc;
355 struct drm_crtc_helper_funcs *funcs;
356 struct drm_framebuffer *fb;
359 for (i = 0; i < helper->crtc_count; i++) {
360 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
361 crtc = mode_set->crtc;
362 funcs = crtc->helper_private;
363 fb = drm_mode_config_fb(crtc);
369 DRM_ERROR("no fb to restore??\n");
373 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
374 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
375 crtc->y, LEAVE_ATOMIC_MODE_SET);
382 bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
386 for (i = 0; i < fb_helper->crtc_count; i++) {
387 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
388 ret = drm_crtc_helper_set_config(mode_set);
396 bool drm_fb_helper_force_kernel_mode(void)
398 bool ret, error = false;
399 struct drm_fb_helper *helper;
401 if (list_empty(&kernel_fb_helper_list))
404 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
405 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
408 ret = drm_fb_helper_restore_fbdev_mode(helper);
417 int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
420 printf("panic occurred, switching back to text console\n");
421 return drm_fb_helper_force_kernel_mode();
425 static struct notifier_block paniced = {
426 .notifier_call = drm_fb_helper_panic,
430 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
432 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
434 void drm_fb_helper_restore(void)
437 ret = drm_fb_helper_force_kernel_mode();
439 DRM_ERROR("Failed to restore crtc configuration\n");
442 #ifdef CONFIG_MAGIC_SYSRQ
443 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
445 drm_fb_helper_restore();
447 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
449 static void drm_fb_helper_sysrq(int dummy1)
451 schedule_work(&drm_fb_helper_restore_work);
454 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
455 .handler = drm_fb_helper_sysrq,
456 .help_msg = "force-fb(V)",
457 .action_msg = "Restore framebuffer console",
460 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
465 static void drm_fb_helper_on(struct fb_info *info)
467 struct drm_fb_helper *fb_helper = info->par;
468 struct drm_device *dev = fb_helper->dev;
469 struct drm_crtc *crtc;
470 struct drm_crtc_helper_funcs *crtc_funcs;
471 struct drm_connector *connector;
472 struct drm_encoder *encoder;
476 * For each CRTC in this fb, turn the crtc on then,
477 * find all associated encoders and turn them on.
479 sx_xlock(&dev->mode_config.mutex);
480 for (i = 0; i < fb_helper->crtc_count; i++) {
481 crtc = fb_helper->crtc_info[i].mode_set.crtc;
482 crtc_funcs = crtc->helper_private;
487 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
489 /* Walk the connectors & encoders on this fb turning them on */
490 for (j = 0; j < fb_helper->connector_count; j++) {
491 connector = fb_helper->connector_info[j]->connector;
492 connector->dpms = DRM_MODE_DPMS_ON;
493 drm_connector_property_set_value(connector,
494 dev->mode_config.dpms_property,
497 /* Found a CRTC on this fb, now find encoders */
498 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
499 if (encoder->crtc == crtc) {
500 struct drm_encoder_helper_funcs *encoder_funcs;
502 encoder_funcs = encoder->helper_private;
503 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
507 sx_xunlock(&dev->mode_config.mutex);
512 static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
514 struct drm_fb_helper *fb_helper = info->par;
515 struct drm_device *dev = fb_helper->dev;
516 struct drm_crtc *crtc;
517 struct drm_crtc_helper_funcs *crtc_funcs;
518 struct drm_connector *connector;
519 struct drm_encoder *encoder;
523 * For each CRTC in this fb, find all associated encoders
524 * and turn them off, then turn off the CRTC.
526 sx_xlock(&dev->mode_config.mutex);
527 for (i = 0; i < fb_helper->crtc_count; i++) {
528 crtc = fb_helper->crtc_info[i].mode_set.crtc;
529 crtc_funcs = crtc->helper_private;
534 /* Walk the connectors on this fb and mark them off */
535 for (j = 0; j < fb_helper->connector_count; j++) {
536 connector = fb_helper->connector_info[j]->connector;
537 connector->dpms = dpms_mode;
538 drm_connector_property_set_value(connector,
539 dev->mode_config.dpms_property,
542 /* Found a CRTC on this fb, now find encoders */
543 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
544 if (encoder->crtc == crtc) {
545 struct drm_encoder_helper_funcs *encoder_funcs;
547 encoder_funcs = encoder->helper_private;
548 encoder_funcs->dpms(encoder, dpms_mode);
551 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
553 sx_xunlock(&dev->mode_config.mutex);
558 int drm_fb_helper_blank(int blank, struct fb_info *info)
561 /* Display: On; HSync: On, VSync: On */
562 case FB_BLANK_UNBLANK:
563 drm_fb_helper_on(info);
565 /* Display: Off; HSync: On, VSync: On */
566 case FB_BLANK_NORMAL:
567 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
569 /* Display: Off; HSync: Off, VSync: On */
570 case FB_BLANK_HSYNC_SUSPEND:
571 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
573 /* Display: Off; HSync: On, VSync: Off */
574 case FB_BLANK_VSYNC_SUSPEND:
575 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
577 /* Display: Off; HSync: Off, VSync: Off */
578 case FB_BLANK_POWERDOWN:
579 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
586 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
590 for (i = 0; i < helper->connector_count; i++)
591 free(helper->connector_info[i], DRM_MEM_KMS);
592 free(helper->connector_info, DRM_MEM_KMS);
593 for (i = 0; i < helper->crtc_count; i++) {
594 free(helper->crtc_info[i].mode_set.connectors, DRM_MEM_KMS);
595 if (helper->crtc_info[i].mode_set.mode)
596 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
598 free(helper->crtc_info, DRM_MEM_KMS);
601 int drm_fb_helper_init(struct drm_device *dev,
602 struct drm_fb_helper *fb_helper,
603 int crtc_count, int max_conn_count)
605 struct drm_crtc *crtc;
608 fb_helper->dev = dev;
610 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
612 fb_helper->crtc_info = malloc(crtc_count *
613 sizeof(struct drm_fb_helper_crtc), DRM_MEM_KMS, M_WAITOK | M_ZERO);
615 fb_helper->crtc_count = crtc_count;
616 fb_helper->connector_info = malloc(dev->mode_config.num_connector *
617 sizeof(struct drm_fb_helper_connector *), DRM_MEM_KMS,
619 fb_helper->connector_count = 0;
621 for (i = 0; i < crtc_count; i++) {
622 fb_helper->crtc_info[i].mode_set.connectors =
623 malloc(max_conn_count * sizeof(struct drm_connector *),
624 DRM_MEM_KMS, M_WAITOK | M_ZERO);
626 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
630 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
631 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
632 fb_helper->crtc_info[i].mode_set.crtc = crtc;
635 fb_helper->conn_limit = max_conn_count;
639 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
641 if (!list_empty(&fb_helper->kernel_fb_list)) {
642 list_del(&fb_helper->kernel_fb_list);
643 if (list_empty(&kernel_fb_helper_list)) {
645 printk(KERN_INFO "drm: unregistered panic notifier\n");
646 atomic_notifier_chain_unregister(&panic_notifier_list,
648 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
653 drm_fb_helper_crtc_free(fb_helper);
658 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
659 u16 blue, u16 regno, struct fb_info *info)
661 struct drm_fb_helper *fb_helper = info->par;
662 struct drm_framebuffer *fb = fb_helper->fb;
665 if (info->fix.visual == FB_VISUAL_trueCOLOR) {
668 /* place color in psuedopalette */
671 palette = (u32 *)info->pseudo_palette;
672 red >>= (16 - info->var.red.length);
673 green >>= (16 - info->var.green.length);
674 blue >>= (16 - info->var.blue.length);
675 value = (red << info->var.red.offset) |
676 (green << info->var.green.offset) |
677 (blue << info->var.blue.offset);
678 if (info->var.transp.length > 0) {
679 u32 mask = (1 << info->var.transp.length) - 1;
680 mask <<= info->var.transp.offset;
683 palette[regno] = value;
689 if (fb->bits_per_pixel == 16) {
692 if (fb->depth == 16 && regno > 63)
694 if (fb->depth == 15 && regno > 31)
697 if (fb->depth == 16) {
701 for (i = 0; i < 8; i++)
702 fb_helper->funcs->gamma_set(crtc, red,
703 green, blue, pindex + i);
706 fb_helper->funcs->gamma_get(crtc, &r,
710 for (i = 0; i < 4; i++)
711 fb_helper->funcs->gamma_set(crtc, r,
718 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
724 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
726 struct drm_fb_helper *fb_helper = info->par;
727 struct drm_crtc_helper_funcs *crtc_funcs;
728 u16 *red, *green, *blue, *transp;
729 struct drm_crtc *crtc;
733 for (i = 0; i < fb_helper->crtc_count; i++) {
734 crtc = fb_helper->crtc_info[i].mode_set.crtc;
735 crtc_funcs = crtc->helper_private;
740 transp = cmap->transp;
743 for (j = 0; j < cmap->len; j++) {
744 u16 hred, hgreen, hblue, htransp = 0xffff;
753 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
757 crtc_funcs->load_lut(crtc);
764 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
765 struct fb_info *info)
767 struct drm_fb_helper *fb_helper = info->par;
768 struct drm_framebuffer *fb = fb_helper->fb;
771 if (var->pixclock != 0 || in_dbg_master())
774 /* Need to resize the fb object !!! */
775 if (var->bits_per_pixel > fb->bits_per_pixel ||
776 var->xres > fb->width || var->yres > fb->height ||
777 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
778 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
779 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
780 var->xres, var->yres, var->bits_per_pixel,
781 var->xres_virtual, var->yres_virtual,
782 fb->width, fb->height, fb->bits_per_pixel);
786 switch (var->bits_per_pixel) {
788 depth = (var->green.length == 6) ? 16 : 15;
791 depth = (var->transp.length > 0) ? 32 : 24;
794 depth = var->bits_per_pixel;
801 var->green.offset = 0;
802 var->blue.offset = 0;
804 var->green.length = 8;
805 var->blue.length = 8;
806 var->transp.length = 0;
807 var->transp.offset = 0;
810 var->red.offset = 10;
811 var->green.offset = 5;
812 var->blue.offset = 0;
814 var->green.length = 5;
815 var->blue.length = 5;
816 var->transp.length = 1;
817 var->transp.offset = 15;
820 var->red.offset = 11;
821 var->green.offset = 5;
822 var->blue.offset = 0;
824 var->green.length = 6;
825 var->blue.length = 5;
826 var->transp.length = 0;
827 var->transp.offset = 0;
830 var->red.offset = 16;
831 var->green.offset = 8;
832 var->blue.offset = 0;
834 var->green.length = 8;
835 var->blue.length = 8;
836 var->transp.length = 0;
837 var->transp.offset = 0;
840 var->red.offset = 16;
841 var->green.offset = 8;
842 var->blue.offset = 0;
844 var->green.length = 8;
845 var->blue.length = 8;
846 var->transp.length = 8;
847 var->transp.offset = 24;
857 /* this will let fbcon do the mode init */
858 int drm_fb_helper_set_par(struct fb_info *info)
860 struct drm_fb_helper *fb_helper = info->par;
861 struct drm_device *dev = fb_helper->dev;
862 struct fb_var_screeninfo *var = &info->var;
863 struct drm_crtc *crtc;
867 if (var->pixclock != 0) {
868 DRM_ERROR("PIXEL CLOCK SET\n");
872 mutex_lock(&dev->mode_config.mutex);
873 for (i = 0; i < fb_helper->crtc_count; i++) {
874 crtc = fb_helper->crtc_info[i].mode_set.crtc;
875 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
877 mutex_unlock(&dev->mode_config.mutex);
881 mutex_unlock(&dev->mode_config.mutex);
883 if (fb_helper->delayed_hotplug) {
884 fb_helper->delayed_hotplug = false;
885 drm_fb_helper_hotplug_event(fb_helper);
892 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
893 struct fb_info *info)
895 struct drm_fb_helper *fb_helper = info->par;
896 struct drm_device *dev = fb_helper->dev;
897 struct drm_mode_set *modeset;
898 struct drm_crtc *crtc;
902 mutex_lock(&dev->mode_config.mutex);
903 for (i = 0; i < fb_helper->crtc_count; i++) {
904 crtc = fb_helper->crtc_info[i].mode_set.crtc;
906 modeset = &fb_helper->crtc_info[i].mode_set;
908 modeset->x = var->xoffset;
909 modeset->y = var->yoffset;
911 if (modeset->num_connectors) {
912 ret = crtc->funcs->set_config(modeset);
914 info->var.xoffset = var->xoffset;
915 info->var.yoffset = var->yoffset;
919 mutex_unlock(&dev->mode_config.mutex);
924 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
930 struct fb_info *info;
931 struct drm_fb_helper_surface_size sizes;
933 #if defined(__FreeBSD__)
934 struct vt_kms_softc *sc;
937 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
938 sizes.surface_depth = 24;
939 sizes.surface_bpp = 32;
940 sizes.fb_width = (unsigned)-1;
941 sizes.fb_height = (unsigned)-1;
943 /* if driver picks 8 or 16 by default use that
944 for both depth/bpp */
945 if (preferred_bpp != sizes.surface_bpp) {
946 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
948 /* first up get a count of crtcs now in use and new min/maxes width/heights */
949 for (i = 0; i < fb_helper->connector_count; i++) {
950 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
951 struct drm_fb_helper_cmdline_mode *cmdline_mode;
953 cmdline_mode = &fb_helper_conn->cmdline_mode;
955 if (cmdline_mode->bpp_specified) {
956 switch (cmdline_mode->bpp) {
958 sizes.surface_depth = sizes.surface_bpp = 8;
961 sizes.surface_depth = 15;
962 sizes.surface_bpp = 16;
965 sizes.surface_depth = sizes.surface_bpp = 16;
968 sizes.surface_depth = sizes.surface_bpp = 24;
971 sizes.surface_depth = 24;
972 sizes.surface_bpp = 32;
980 for (i = 0; i < fb_helper->crtc_count; i++) {
981 struct drm_display_mode *desired_mode;
982 desired_mode = fb_helper->crtc_info[i].desired_mode;
986 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
987 if (desired_mode->hdisplay < sizes.fb_width)
988 sizes.fb_width = desired_mode->hdisplay;
989 if (desired_mode->vdisplay < sizes.fb_height)
990 sizes.fb_height = desired_mode->vdisplay;
991 if (desired_mode->hdisplay > sizes.surface_width)
992 sizes.surface_width = desired_mode->hdisplay;
993 if (desired_mode->vdisplay > sizes.surface_height)
994 sizes.surface_height = desired_mode->vdisplay;
999 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
1000 /* hmm everyone went away - assume VGA cable just fell out
1001 and will come back later. */
1002 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
1003 sizes.fb_width = sizes.surface_width = 1024;
1004 sizes.fb_height = sizes.surface_height = 768;
1007 /* push down into drivers */
1008 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1012 #if defined(__FreeBSD__)
1013 sc = malloc(sizeof(struct vt_kms_softc), DRM_MEM_KMS,
1015 sc->fb_helper = fb_helper;
1016 TASK_INIT(&sc->fb_mode_task, 0, vt_restore_fbdev_mode, sc);
1018 info = fb_helper->fbdev;
1020 info->fb_name = device_get_nameunit(fb_helper->dev->device);
1021 info->fb_depth = fb_helper->fb->bits_per_pixel;
1022 info->fb_height = fb_helper->fb->height;
1023 info->fb_width = fb_helper->fb->width;
1024 info->fb_stride = fb_helper->fb->pitches[0];
1026 info->enter = &vt_kms_postswitch;
1029 /* set the fb pointer */
1030 for (i = 0; i < fb_helper->crtc_count; i++) {
1031 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1034 #if defined(__FreeBSD__)
1036 register_framebuffer(info);
1040 info->var.pixclock = 0;
1041 if (register_framebuffer(info) < 0) {
1045 printf("fb%d: %s frame buffer device\n", info->node,
1049 drm_fb_helper_set_par(info);
1052 /* Switch back to kernel console on panic */
1053 /* multi card linked list maybe */
1054 if (list_empty(&kernel_fb_helper_list)) {
1055 printf("drm: registered panic notifier\n");
1056 atomic_notifier_chain_register(&panic_notifier_list,
1060 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1066 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1069 info->fix.type = FB_TYPE_PACKED_PIXELS;
1070 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1071 FB_VISUAL_trueCOLOR;
1072 info->fix.mmio_start = 0;
1073 info->fix.mmio_len = 0;
1074 info->fix.type_aux = 0;
1075 info->fix.xpanstep = 1; /* doing it in hw */
1076 info->fix.ypanstep = 1; /* doing it in hw */
1077 info->fix.ywrapstep = 0;
1078 info->fix.accel = FB_ACCEL_NONE;
1079 info->fix.type_aux = 0;
1081 info->fix.line_length = pitch;
1085 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1086 uint32_t fb_width, uint32_t fb_height)
1088 struct drm_framebuffer *fb = fb_helper->fb;
1089 info->pseudo_palette = fb_helper->pseudo_palette;
1090 info->var.xres_virtual = fb->width;
1091 info->var.yres_virtual = fb->height;
1092 info->var.bits_per_pixel = fb->bits_per_pixel;
1093 info->var.accel_flags = FB_ACCELF_TEXT;
1094 info->var.xoffset = 0;
1095 info->var.yoffset = 0;
1096 info->var.activate = FB_ACTIVATE_NOW;
1097 info->var.height = -1;
1098 info->var.width = -1;
1100 switch (fb->depth) {
1102 info->var.red.offset = 0;
1103 info->var.green.offset = 0;
1104 info->var.blue.offset = 0;
1105 info->var.red.length = 8; /* 8bit DAC */
1106 info->var.green.length = 8;
1107 info->var.blue.length = 8;
1108 info->var.transp.offset = 0;
1109 info->var.transp.length = 0;
1112 info->var.red.offset = 10;
1113 info->var.green.offset = 5;
1114 info->var.blue.offset = 0;
1115 info->var.red.length = 5;
1116 info->var.green.length = 5;
1117 info->var.blue.length = 5;
1118 info->var.transp.offset = 15;
1119 info->var.transp.length = 1;
1122 info->var.red.offset = 11;
1123 info->var.green.offset = 5;
1124 info->var.blue.offset = 0;
1125 info->var.red.length = 5;
1126 info->var.green.length = 6;
1127 info->var.blue.length = 5;
1128 info->var.transp.offset = 0;
1131 info->var.red.offset = 16;
1132 info->var.green.offset = 8;
1133 info->var.blue.offset = 0;
1134 info->var.red.length = 8;
1135 info->var.green.length = 8;
1136 info->var.blue.length = 8;
1137 info->var.transp.offset = 0;
1138 info->var.transp.length = 0;
1141 info->var.red.offset = 16;
1142 info->var.green.offset = 8;
1143 info->var.blue.offset = 0;
1144 info->var.red.length = 8;
1145 info->var.green.length = 8;
1146 info->var.blue.length = 8;
1147 info->var.transp.offset = 24;
1148 info->var.transp.length = 8;
1154 info->var.xres = fb_width;
1155 info->var.yres = fb_height;
1159 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1163 struct drm_connector *connector;
1167 for (i = 0; i < fb_helper->connector_count; i++) {
1168 connector = fb_helper->connector_info[i]->connector;
1169 count += connector->funcs->fill_modes(connector, maxX, maxY);
1175 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1177 struct drm_display_mode *mode;
1179 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1180 if (drm_mode_width(mode) > width ||
1181 drm_mode_height(mode) > height)
1183 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1189 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1191 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1192 cmdline_mode = &fb_connector->cmdline_mode;
1193 return cmdline_mode->specified;
1196 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1197 int width, int height)
1199 struct drm_cmdline_mode *cmdline_mode;
1200 struct drm_display_mode *mode = NULL;
1202 cmdline_mode = &fb_helper_conn->cmdline_mode1;
1203 if (cmdline_mode->specified == false &&
1204 !drm_fetch_cmdline_mode_from_kenv(fb_helper_conn->connector,
1208 /* attempt to find a matching mode in the list of modes
1209 * we have gotten so far, if not add a CVT mode that conforms
1211 if (cmdline_mode->rb || cmdline_mode->margins)
1214 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1215 /* check width/height */
1216 if (mode->hdisplay != cmdline_mode->xres ||
1217 mode->vdisplay != cmdline_mode->yres)
1220 if (cmdline_mode->refresh_specified) {
1221 if (mode->vrefresh != cmdline_mode->refresh)
1225 if (cmdline_mode->interlace) {
1226 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1233 if (cmdline_mode->cvt)
1234 mode = drm_cvt_mode(fb_helper_conn->connector->dev,
1235 cmdline_mode->xres, cmdline_mode->yres,
1236 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1237 cmdline_mode->rb, cmdline_mode->interlace,
1238 cmdline_mode->margins);
1240 mode = drm_gtf_mode(fb_helper_conn->connector->dev,
1241 cmdline_mode->xres, cmdline_mode->yres,
1242 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1243 cmdline_mode->interlace,
1244 cmdline_mode->margins);
1245 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1246 list_add(&mode->head, &fb_helper_conn->connector->modes);
1250 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1255 enable = connector->status == connector_status_connected;
1257 enable = connector->status != connector_status_disconnected;
1262 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1265 bool any_enabled = false;
1266 struct drm_connector *connector;
1269 for (i = 0; i < fb_helper->connector_count; i++) {
1270 connector = fb_helper->connector_info[i]->connector;
1271 enabled[i] = drm_connector_enabled(connector, true);
1272 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1273 enabled[i] ? "yes" : "no");
1274 any_enabled |= enabled[i];
1280 for (i = 0; i < fb_helper->connector_count; i++) {
1281 connector = fb_helper->connector_info[i]->connector;
1282 enabled[i] = drm_connector_enabled(connector, false);
1286 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1287 struct drm_display_mode **modes,
1288 bool *enabled, int width, int height)
1291 bool can_clone = false;
1292 struct drm_fb_helper_connector *fb_helper_conn;
1293 struct drm_display_mode *dmt_mode, *mode;
1295 /* only contemplate cloning in the single crtc case */
1296 if (fb_helper->crtc_count > 1)
1300 for (i = 0; i < fb_helper->connector_count; i++) {
1305 /* only contemplate cloning if more than one connector is enabled */
1309 /* check the command line or if nothing common pick 1024x768 */
1311 for (i = 0; i < fb_helper->connector_count; i++) {
1314 fb_helper_conn = fb_helper->connector_info[i];
1315 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1320 for (j = 0; j < i; j++) {
1323 if (!drm_mode_equal(modes[j], modes[i]))
1329 DRM_DEBUG_KMS("can clone using command line\n");
1333 /* try and find a 1024x768 mode on each connector */
1335 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1337 for (i = 0; i < fb_helper->connector_count; i++) {
1342 fb_helper_conn = fb_helper->connector_info[i];
1343 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1344 if (drm_mode_equal(mode, dmt_mode))
1352 DRM_DEBUG_KMS("can clone using 1024x768\n");
1355 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1359 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1360 struct drm_display_mode **modes,
1361 bool *enabled, int width, int height)
1363 struct drm_fb_helper_connector *fb_helper_conn;
1366 for (i = 0; i < fb_helper->connector_count; i++) {
1367 fb_helper_conn = fb_helper->connector_info[i];
1369 if (enabled[i] == false)
1372 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1373 fb_helper_conn->connector->base.id);
1375 /* got for command line mode first */
1376 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1378 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1379 fb_helper_conn->connector->base.id);
1380 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1382 /* No preferred modes, pick one off the list */
1383 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1384 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1387 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1393 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1394 struct drm_fb_helper_crtc **best_crtcs,
1395 struct drm_display_mode **modes,
1396 int n, int width, int height)
1399 struct drm_device *dev = fb_helper->dev;
1400 struct drm_connector *connector;
1401 struct drm_connector_helper_funcs *connector_funcs;
1402 struct drm_encoder *encoder;
1403 struct drm_fb_helper_crtc *best_crtc;
1404 int my_score, best_score, score;
1405 struct drm_fb_helper_crtc **crtcs, *crtc;
1406 struct drm_fb_helper_connector *fb_helper_conn;
1408 if (n == fb_helper->connector_count)
1411 fb_helper_conn = fb_helper->connector_info[n];
1412 connector = fb_helper_conn->connector;
1414 best_crtcs[n] = NULL;
1416 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1417 if (modes[n] == NULL)
1420 crtcs = malloc(dev->mode_config.num_connector *
1421 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1425 if (connector->status == connector_status_connected)
1427 if (drm_has_cmdline_mode(fb_helper_conn))
1429 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1432 connector_funcs = connector->helper_private;
1433 encoder = connector_funcs->best_encoder(connector);
1437 /* select a crtc for this connector and then attempt to configure
1438 remaining connectors */
1439 for (c = 0; c < fb_helper->crtc_count; c++) {
1440 crtc = &fb_helper->crtc_info[c];
1442 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1446 for (o = 0; o < n; o++)
1447 if (best_crtcs[o] == crtc)
1451 /* ignore cloning unless only a single crtc */
1452 if (fb_helper->crtc_count > 1)
1455 if (!drm_mode_equal(modes[o], modes[n]))
1460 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1461 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1463 if (score > best_score) {
1466 memcpy(best_crtcs, crtcs,
1467 dev->mode_config.num_connector *
1468 sizeof(struct drm_fb_helper_crtc *));
1472 free(crtcs, DRM_MEM_KMS);
1476 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1478 struct drm_device *dev = fb_helper->dev;
1479 struct drm_fb_helper_crtc **crtcs;
1480 struct drm_display_mode **modes;
1481 struct drm_encoder *encoder;
1482 struct drm_mode_set *modeset;
1487 DRM_DEBUG_KMS("\n");
1489 width = dev->mode_config.max_width;
1490 height = dev->mode_config.max_height;
1492 /* clean out all the encoder/crtc combos */
1493 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1494 encoder->crtc = NULL;
1497 crtcs = malloc(dev->mode_config.num_connector *
1498 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1500 modes = malloc(dev->mode_config.num_connector *
1501 sizeof(struct drm_display_mode *), DRM_MEM_KMS,
1503 enabled = malloc(dev->mode_config.num_connector *
1504 sizeof(bool), DRM_MEM_KMS, M_WAITOK | M_ZERO);
1506 drm_enable_connectors(fb_helper, enabled);
1508 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1510 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1512 DRM_ERROR("Unable to find initial modes\n");
1515 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1517 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1519 /* need to set the modesets up here for use later */
1520 /* fill out the connector<->crtc mappings into the modesets */
1521 for (i = 0; i < fb_helper->crtc_count; i++) {
1522 modeset = &fb_helper->crtc_info[i].mode_set;
1523 modeset->num_connectors = 0;
1526 for (i = 0; i < fb_helper->connector_count; i++) {
1527 struct drm_display_mode *mode = modes[i];
1528 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1529 modeset = &fb_crtc->mode_set;
1531 if (mode && fb_crtc) {
1532 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1533 mode->name, fb_crtc->mode_set.crtc->base.id);
1534 fb_crtc->desired_mode = mode;
1536 drm_mode_destroy(dev, modeset->mode);
1537 modeset->mode = drm_mode_duplicate(dev,
1538 fb_crtc->desired_mode);
1539 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1543 free(crtcs, DRM_MEM_KMS);
1544 free(modes, DRM_MEM_KMS);
1545 free(enabled, DRM_MEM_KMS);
1549 * drm_helper_initial_config - setup a sane initial connector configuration
1553 * Called at init time, must take mode config lock.
1555 * Scan the CRTCs and connectors and try to put together an initial setup.
1556 * At the moment, this is a cloned configuration across all heads with
1557 * a new framebuffer object as the backing store.
1560 * Zero if everything went ok, nonzero otherwise.
1562 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1564 struct drm_device *dev = fb_helper->dev;
1567 /* disable all the possible outputs/crtcs before entering KMS mode */
1568 drm_helper_disable_unused_functions(fb_helper->dev);
1570 drm_fb_helper_parse_command_line(fb_helper);
1572 count = drm_fb_helper_probe_connector_modes(fb_helper,
1573 dev->mode_config.max_width,
1574 dev->mode_config.max_height);
1576 * we shouldn't end up with no modes here.
1579 printf("No connectors reported connected with modes\n");
1581 drm_setup_crtcs(fb_helper);
1583 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1586 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1588 struct drm_device *dev = fb_helper->dev;
1590 u32 max_width, max_height, bpp_sel;
1591 bool bound = false, crtcs_bound = false;
1592 struct drm_crtc *crtc;
1597 sx_xlock(&dev->mode_config.mutex);
1598 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1601 if (crtc->fb == fb_helper->fb)
1605 if (!bound && crtcs_bound) {
1606 fb_helper->delayed_hotplug = true;
1607 sx_xunlock(&dev->mode_config.mutex);
1610 DRM_DEBUG_KMS("\n");
1612 max_width = fb_helper->fb->width;
1613 max_height = fb_helper->fb->height;
1614 bpp_sel = fb_helper->fb->bits_per_pixel;
1616 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1618 drm_setup_crtcs(fb_helper);
1619 sx_xunlock(&dev->mode_config.mutex);
1621 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);