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 drm_fb_helper *fb_helper;
53 struct vt_kms_softc *sc;
55 sc = (struct vt_kms_softc *)arg;
56 fb_helper = sc->fb_helper;
57 sx_xlock(&fb_helper->dev->mode_config.mutex);
58 drm_fb_helper_restore_fbdev_mode(fb_helper);
59 sx_xunlock(&fb_helper->dev->mode_config.mutex);
63 vt_kms_postswitch(void *arg)
65 struct vt_kms_softc *sc;
67 sc = (struct vt_kms_softc *)arg;
68 taskqueue_enqueue_fast(taskqueue_thread, &sc->fb_mode_task);
74 static DRM_LIST_HEAD(kernel_fb_helper_list);
76 /* simple single crtc case helper function */
77 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
79 struct drm_device *dev = fb_helper->dev;
80 struct drm_connector *connector;
82 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
83 struct drm_fb_helper_connector *fb_helper_connector;
85 fb_helper_connector = malloc(
86 sizeof(struct drm_fb_helper_connector), DRM_MEM_KMS,
89 fb_helper_connector->connector = connector;
90 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
95 const char *fb_mode_option;
98 * drm_fb_helper_connector_parse_command_line - parse command line for connector
99 * @connector - connector to parse line for
100 * @mode_option - per connector mode option
102 * This parses the connector specific then generic command lines for
103 * modes and options to configure the connector.
105 * This uses the same parameters as the fb modedb.c, except for extra
106 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
108 * enable/enable Digital/disable bit at the end
110 static bool drm_fb_helper_connector_parse_command_line(struct drm_fb_helper_connector *fb_helper_conn,
111 const char *mode_option)
114 unsigned int namelen;
115 int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
116 unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
117 int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
119 enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
120 struct drm_fb_helper_cmdline_mode *cmdline_mode;
121 struct drm_connector *connector;
125 connector = fb_helper_conn->connector;
127 cmdline_mode = &fb_helper_conn->cmdline_mode;
129 mode_option = fb_mode_option;
132 cmdline_mode->specified = false;
137 namelen = strlen(name);
138 for (i = namelen-1; i >= 0; i--) {
142 if (!refresh_specified && !bpp_specified &&
144 refresh = strtol(&name[i+1], NULL, 10);
145 refresh_specified = 1;
153 if (!bpp_specified && !yres_specified) {
154 bpp = strtol(&name[i+1], NULL, 10);
162 if (!yres_specified) {
163 yres = strtol(&name[i+1], NULL, 10);
186 force = DRM_FORCE_ON;
189 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
190 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
191 force = DRM_FORCE_ON;
193 force = DRM_FORCE_ON_DIGITAL;
196 force = DRM_FORCE_OFF;
202 if (i < 0 && yres_specified) {
203 xres = strtol(name, NULL, 10);
208 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
209 drm_get_connector_name(connector), xres, yres,
210 (refresh) ? refresh : 60, (rb) ? " reduced blanking" :
211 "", (margins) ? " with margins" : "", (interlace) ?
217 case DRM_FORCE_OFF: s = "OFF"; break;
218 case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
220 case DRM_FORCE_ON: s = "ON"; break;
223 DRM_INFO("forcing %s connector %s\n",
224 drm_get_connector_name(connector), s);
225 connector->force = force;
229 cmdline_mode->specified = true;
230 cmdline_mode->xres = xres;
231 cmdline_mode->yres = yres;
234 if (refresh_specified) {
235 cmdline_mode->refresh_specified = true;
236 cmdline_mode->refresh = refresh;
240 cmdline_mode->bpp_specified = true;
241 cmdline_mode->bpp = bpp;
243 cmdline_mode->rb = rb ? true : false;
244 cmdline_mode->cvt = cvt ? true : false;
245 cmdline_mode->interlace = interlace ? true : false;
251 fb_get_options(const char *connector_name, char **option)
255 * TODO: store mode options pointer in ${option} for connector with
256 * name ${connector_name}
261 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
263 struct drm_fb_helper_connector *fb_helper_conn;
266 for (i = 0; i < fb_helper->connector_count; i++) {
269 fb_helper_conn = fb_helper->connector_info[i];
271 /* do something on return - turn off connector maybe */
272 if (fb_get_options(drm_get_connector_name(fb_helper_conn->connector), &option))
275 drm_fb_helper_connector_parse_command_line(fb_helper_conn, option);
281 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
283 uint16_t *r_base, *g_base, *b_base;
286 r_base = crtc->gamma_store;
287 g_base = r_base + crtc->gamma_size;
288 b_base = g_base + crtc->gamma_size;
290 for (i = 0; i < crtc->gamma_size; i++)
291 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
294 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
296 uint16_t *r_base, *g_base, *b_base;
298 r_base = crtc->gamma_store;
299 g_base = r_base + crtc->gamma_size;
300 b_base = g_base + crtc->gamma_size;
302 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
307 int drm_fb_helper_debug_enter(struct fb_info *info)
309 struct drm_fb_helper *helper = info->par;
310 struct drm_crtc_helper_funcs *funcs;
313 if (list_empty(&kernel_fb_helper_list))
316 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
317 for (i = 0; i < helper->crtc_count; i++) {
318 struct drm_mode_set *mode_set =
319 &helper->crtc_info[i].mode_set;
321 if (!mode_set->crtc->enabled)
324 funcs = mode_set->crtc->helper_private;
325 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
326 funcs->mode_set_base_atomic(mode_set->crtc,
330 ENTER_ATOMIC_MODE_SET);
339 /* Find the real fb for a given fb helper CRTC */
340 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
342 struct drm_device *dev = crtc->dev;
345 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
346 if (crtc->base.id == c->base.id)
355 int drm_fb_helper_debug_leave(struct fb_info *info)
357 struct drm_fb_helper *helper = info->par;
358 struct drm_crtc *crtc;
359 struct drm_crtc_helper_funcs *funcs;
360 struct drm_framebuffer *fb;
363 for (i = 0; i < helper->crtc_count; i++) {
364 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
365 crtc = mode_set->crtc;
366 funcs = crtc->helper_private;
367 fb = drm_mode_config_fb(crtc);
373 DRM_ERROR("no fb to restore??\n");
377 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
378 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
379 crtc->y, LEAVE_ATOMIC_MODE_SET);
386 bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
390 for (i = 0; i < fb_helper->crtc_count; i++) {
391 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
392 ret = drm_crtc_helper_set_config(mode_set);
400 bool drm_fb_helper_force_kernel_mode(void)
402 bool ret, error = false;
403 struct drm_fb_helper *helper;
405 if (list_empty(&kernel_fb_helper_list))
408 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
409 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
412 ret = drm_fb_helper_restore_fbdev_mode(helper);
421 int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
424 printf("panic occurred, switching back to text console\n");
425 return drm_fb_helper_force_kernel_mode();
429 static struct notifier_block paniced = {
430 .notifier_call = drm_fb_helper_panic,
434 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
436 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
438 void drm_fb_helper_restore(void)
441 ret = drm_fb_helper_force_kernel_mode();
443 DRM_ERROR("Failed to restore crtc configuration\n");
446 #ifdef CONFIG_MAGIC_SYSRQ
447 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
449 drm_fb_helper_restore();
451 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
453 static void drm_fb_helper_sysrq(int dummy1)
455 schedule_work(&drm_fb_helper_restore_work);
458 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
459 .handler = drm_fb_helper_sysrq,
460 .help_msg = "force-fb(V)",
461 .action_msg = "Restore framebuffer console",
464 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
469 static void drm_fb_helper_on(struct fb_info *info)
471 struct drm_fb_helper *fb_helper = info->par;
472 struct drm_device *dev = fb_helper->dev;
473 struct drm_crtc *crtc;
474 struct drm_crtc_helper_funcs *crtc_funcs;
475 struct drm_connector *connector;
476 struct drm_encoder *encoder;
480 * For each CRTC in this fb, turn the crtc on then,
481 * find all associated encoders and turn them on.
483 sx_xlock(&dev->mode_config.mutex);
484 for (i = 0; i < fb_helper->crtc_count; i++) {
485 crtc = fb_helper->crtc_info[i].mode_set.crtc;
486 crtc_funcs = crtc->helper_private;
491 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
493 /* Walk the connectors & encoders on this fb turning them on */
494 for (j = 0; j < fb_helper->connector_count; j++) {
495 connector = fb_helper->connector_info[j]->connector;
496 connector->dpms = DRM_MODE_DPMS_ON;
497 drm_connector_property_set_value(connector,
498 dev->mode_config.dpms_property,
501 /* Found a CRTC on this fb, now find encoders */
502 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
503 if (encoder->crtc == crtc) {
504 struct drm_encoder_helper_funcs *encoder_funcs;
506 encoder_funcs = encoder->helper_private;
507 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
511 sx_xunlock(&dev->mode_config.mutex);
516 static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
518 struct drm_fb_helper *fb_helper = info->par;
519 struct drm_device *dev = fb_helper->dev;
520 struct drm_crtc *crtc;
521 struct drm_crtc_helper_funcs *crtc_funcs;
522 struct drm_connector *connector;
523 struct drm_encoder *encoder;
527 * For each CRTC in this fb, find all associated encoders
528 * and turn them off, then turn off the CRTC.
530 sx_xlock(&dev->mode_config.mutex);
531 for (i = 0; i < fb_helper->crtc_count; i++) {
532 crtc = fb_helper->crtc_info[i].mode_set.crtc;
533 crtc_funcs = crtc->helper_private;
538 /* Walk the connectors on this fb and mark them off */
539 for (j = 0; j < fb_helper->connector_count; j++) {
540 connector = fb_helper->connector_info[j]->connector;
541 connector->dpms = dpms_mode;
542 drm_connector_property_set_value(connector,
543 dev->mode_config.dpms_property,
546 /* Found a CRTC on this fb, now find encoders */
547 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
548 if (encoder->crtc == crtc) {
549 struct drm_encoder_helper_funcs *encoder_funcs;
551 encoder_funcs = encoder->helper_private;
552 encoder_funcs->dpms(encoder, dpms_mode);
555 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
557 sx_xunlock(&dev->mode_config.mutex);
562 int drm_fb_helper_blank(int blank, struct fb_info *info)
565 /* Display: On; HSync: On, VSync: On */
566 case FB_BLANK_UNBLANK:
567 drm_fb_helper_on(info);
569 /* Display: Off; HSync: On, VSync: On */
570 case FB_BLANK_NORMAL:
571 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
573 /* Display: Off; HSync: Off, VSync: On */
574 case FB_BLANK_HSYNC_SUSPEND:
575 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
577 /* Display: Off; HSync: On, VSync: Off */
578 case FB_BLANK_VSYNC_SUSPEND:
579 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
581 /* Display: Off; HSync: Off, VSync: Off */
582 case FB_BLANK_POWERDOWN:
583 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
590 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
594 for (i = 0; i < helper->connector_count; i++)
595 free(helper->connector_info[i], DRM_MEM_KMS);
596 free(helper->connector_info, DRM_MEM_KMS);
597 for (i = 0; i < helper->crtc_count; i++) {
598 free(helper->crtc_info[i].mode_set.connectors, DRM_MEM_KMS);
599 if (helper->crtc_info[i].mode_set.mode)
600 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
602 free(helper->crtc_info, DRM_MEM_KMS);
605 int drm_fb_helper_init(struct drm_device *dev,
606 struct drm_fb_helper *fb_helper,
607 int crtc_count, int max_conn_count)
609 struct drm_crtc *crtc;
612 fb_helper->dev = dev;
614 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
616 fb_helper->crtc_info = malloc(crtc_count *
617 sizeof(struct drm_fb_helper_crtc), DRM_MEM_KMS, M_WAITOK | M_ZERO);
619 fb_helper->crtc_count = crtc_count;
620 fb_helper->connector_info = malloc(dev->mode_config.num_connector *
621 sizeof(struct drm_fb_helper_connector *), DRM_MEM_KMS,
623 fb_helper->connector_count = 0;
625 for (i = 0; i < crtc_count; i++) {
626 fb_helper->crtc_info[i].mode_set.connectors =
627 malloc(max_conn_count * sizeof(struct drm_connector *),
628 DRM_MEM_KMS, M_WAITOK | M_ZERO);
630 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
634 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
635 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
636 fb_helper->crtc_info[i].mode_set.crtc = crtc;
639 fb_helper->conn_limit = max_conn_count;
643 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
645 if (!list_empty(&fb_helper->kernel_fb_list)) {
646 list_del(&fb_helper->kernel_fb_list);
647 if (list_empty(&kernel_fb_helper_list)) {
649 printk(KERN_INFO "drm: unregistered panic notifier\n");
650 atomic_notifier_chain_unregister(&panic_notifier_list,
652 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
657 drm_fb_helper_crtc_free(fb_helper);
662 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
663 u16 blue, u16 regno, struct fb_info *info)
665 struct drm_fb_helper *fb_helper = info->par;
666 struct drm_framebuffer *fb = fb_helper->fb;
669 if (info->fix.visual == FB_VISUAL_trueCOLOR) {
672 /* place color in psuedopalette */
675 palette = (u32 *)info->pseudo_palette;
676 red >>= (16 - info->var.red.length);
677 green >>= (16 - info->var.green.length);
678 blue >>= (16 - info->var.blue.length);
679 value = (red << info->var.red.offset) |
680 (green << info->var.green.offset) |
681 (blue << info->var.blue.offset);
682 if (info->var.transp.length > 0) {
683 u32 mask = (1 << info->var.transp.length) - 1;
684 mask <<= info->var.transp.offset;
687 palette[regno] = value;
693 if (fb->bits_per_pixel == 16) {
696 if (fb->depth == 16 && regno > 63)
698 if (fb->depth == 15 && regno > 31)
701 if (fb->depth == 16) {
705 for (i = 0; i < 8; i++)
706 fb_helper->funcs->gamma_set(crtc, red,
707 green, blue, pindex + i);
710 fb_helper->funcs->gamma_get(crtc, &r,
714 for (i = 0; i < 4; i++)
715 fb_helper->funcs->gamma_set(crtc, r,
722 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
728 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
730 struct drm_fb_helper *fb_helper = info->par;
731 struct drm_crtc_helper_funcs *crtc_funcs;
732 u16 *red, *green, *blue, *transp;
733 struct drm_crtc *crtc;
737 for (i = 0; i < fb_helper->crtc_count; i++) {
738 crtc = fb_helper->crtc_info[i].mode_set.crtc;
739 crtc_funcs = crtc->helper_private;
744 transp = cmap->transp;
747 for (j = 0; j < cmap->len; j++) {
748 u16 hred, hgreen, hblue, htransp = 0xffff;
757 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
761 crtc_funcs->load_lut(crtc);
768 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
769 struct fb_info *info)
771 struct drm_fb_helper *fb_helper = info->par;
772 struct drm_framebuffer *fb = fb_helper->fb;
775 if (var->pixclock != 0 || in_dbg_master())
778 /* Need to resize the fb object !!! */
779 if (var->bits_per_pixel > fb->bits_per_pixel ||
780 var->xres > fb->width || var->yres > fb->height ||
781 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
782 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
783 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
784 var->xres, var->yres, var->bits_per_pixel,
785 var->xres_virtual, var->yres_virtual,
786 fb->width, fb->height, fb->bits_per_pixel);
790 switch (var->bits_per_pixel) {
792 depth = (var->green.length == 6) ? 16 : 15;
795 depth = (var->transp.length > 0) ? 32 : 24;
798 depth = var->bits_per_pixel;
805 var->green.offset = 0;
806 var->blue.offset = 0;
808 var->green.length = 8;
809 var->blue.length = 8;
810 var->transp.length = 0;
811 var->transp.offset = 0;
814 var->red.offset = 10;
815 var->green.offset = 5;
816 var->blue.offset = 0;
818 var->green.length = 5;
819 var->blue.length = 5;
820 var->transp.length = 1;
821 var->transp.offset = 15;
824 var->red.offset = 11;
825 var->green.offset = 5;
826 var->blue.offset = 0;
828 var->green.length = 6;
829 var->blue.length = 5;
830 var->transp.length = 0;
831 var->transp.offset = 0;
834 var->red.offset = 16;
835 var->green.offset = 8;
836 var->blue.offset = 0;
838 var->green.length = 8;
839 var->blue.length = 8;
840 var->transp.length = 0;
841 var->transp.offset = 0;
844 var->red.offset = 16;
845 var->green.offset = 8;
846 var->blue.offset = 0;
848 var->green.length = 8;
849 var->blue.length = 8;
850 var->transp.length = 8;
851 var->transp.offset = 24;
861 /* this will let fbcon do the mode init */
862 int drm_fb_helper_set_par(struct fb_info *info)
864 struct drm_fb_helper *fb_helper = info->par;
865 struct drm_device *dev = fb_helper->dev;
866 struct fb_var_screeninfo *var = &info->var;
867 struct drm_crtc *crtc;
871 if (var->pixclock != 0) {
872 DRM_ERROR("PIXEL CLOCK SET\n");
876 mutex_lock(&dev->mode_config.mutex);
877 for (i = 0; i < fb_helper->crtc_count; i++) {
878 crtc = fb_helper->crtc_info[i].mode_set.crtc;
879 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
881 mutex_unlock(&dev->mode_config.mutex);
885 mutex_unlock(&dev->mode_config.mutex);
887 if (fb_helper->delayed_hotplug) {
888 fb_helper->delayed_hotplug = false;
889 drm_fb_helper_hotplug_event(fb_helper);
896 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
897 struct fb_info *info)
899 struct drm_fb_helper *fb_helper = info->par;
900 struct drm_device *dev = fb_helper->dev;
901 struct drm_mode_set *modeset;
902 struct drm_crtc *crtc;
906 mutex_lock(&dev->mode_config.mutex);
907 for (i = 0; i < fb_helper->crtc_count; i++) {
908 crtc = fb_helper->crtc_info[i].mode_set.crtc;
910 modeset = &fb_helper->crtc_info[i].mode_set;
912 modeset->x = var->xoffset;
913 modeset->y = var->yoffset;
915 if (modeset->num_connectors) {
916 ret = crtc->funcs->set_config(modeset);
918 info->var.xoffset = var->xoffset;
919 info->var.yoffset = var->yoffset;
923 mutex_unlock(&dev->mode_config.mutex);
928 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
934 struct fb_info *info;
935 struct drm_fb_helper_surface_size sizes;
937 #if defined(__FreeBSD__)
938 struct vt_kms_softc *sc;
942 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
943 sizes.surface_depth = 24;
944 sizes.surface_bpp = 32;
945 sizes.fb_width = (unsigned)-1;
946 sizes.fb_height = (unsigned)-1;
948 /* if driver picks 8 or 16 by default use that
949 for both depth/bpp */
950 if (preferred_bpp != sizes.surface_bpp) {
951 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
953 /* first up get a count of crtcs now in use and new min/maxes width/heights */
954 for (i = 0; i < fb_helper->connector_count; i++) {
955 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
956 struct drm_fb_helper_cmdline_mode *cmdline_mode;
958 cmdline_mode = &fb_helper_conn->cmdline_mode;
960 if (cmdline_mode->bpp_specified) {
961 switch (cmdline_mode->bpp) {
963 sizes.surface_depth = sizes.surface_bpp = 8;
966 sizes.surface_depth = 15;
967 sizes.surface_bpp = 16;
970 sizes.surface_depth = sizes.surface_bpp = 16;
973 sizes.surface_depth = sizes.surface_bpp = 24;
976 sizes.surface_depth = 24;
977 sizes.surface_bpp = 32;
985 for (i = 0; i < fb_helper->crtc_count; i++) {
986 struct drm_display_mode *desired_mode;
987 desired_mode = fb_helper->crtc_info[i].desired_mode;
991 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
992 if (desired_mode->hdisplay < sizes.fb_width)
993 sizes.fb_width = desired_mode->hdisplay;
994 if (desired_mode->vdisplay < sizes.fb_height)
995 sizes.fb_height = desired_mode->vdisplay;
996 if (desired_mode->hdisplay > sizes.surface_width)
997 sizes.surface_width = desired_mode->hdisplay;
998 if (desired_mode->vdisplay > sizes.surface_height)
999 sizes.surface_height = desired_mode->vdisplay;
1004 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
1005 /* hmm everyone went away - assume VGA cable just fell out
1006 and will come back later. */
1007 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
1008 sizes.fb_width = sizes.surface_width = 1024;
1009 sizes.fb_height = sizes.surface_height = 768;
1012 /* push down into drivers */
1013 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1017 #if defined(__FreeBSD__)
1018 sc = malloc(sizeof(struct vt_kms_softc), DRM_MEM_KMS,
1020 sc->fb_helper = fb_helper;
1021 TASK_INIT(&sc->fb_mode_task, 0, vt_restore_fbdev_mode, sc);
1023 info = fb_helper->fbdev;
1025 info->fb_name = device_get_nameunit(fb_helper->dev->device);
1026 info->fb_depth = fb_helper->fb->bits_per_pixel;
1027 info->fb_height = fb_helper->fb->height;
1028 info->fb_width = fb_helper->fb->width;
1029 info->fb_stride = fb_helper->fb->pitches[0];
1031 info->enter = &vt_kms_postswitch;
1034 /* set the fb pointer */
1035 for (i = 0; i < fb_helper->crtc_count; i++) {
1036 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1039 #if defined(__FreeBSD__)
1044 kdev = fb_helper->dev->device;
1045 fbd = device_add_child(kdev, "fbd", device_get_unit(kdev));
1046 ret = device_probe_and_attach(fbd);
1048 DRM_ERROR("Failed to attach fbd device: %d\n", ret);
1052 info->var.pixclock = 0;
1053 if (register_framebuffer(info) < 0) {
1057 printf("fb%d: %s frame buffer device\n", info->node,
1061 drm_fb_helper_set_par(info);
1064 /* Switch back to kernel console on panic */
1065 /* multi card linked list maybe */
1066 if (list_empty(&kernel_fb_helper_list)) {
1067 printf("drm: registered panic notifier\n");
1068 atomic_notifier_chain_register(&panic_notifier_list,
1072 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1078 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1081 info->fix.type = FB_TYPE_PACKED_PIXELS;
1082 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1083 FB_VISUAL_trueCOLOR;
1084 info->fix.mmio_start = 0;
1085 info->fix.mmio_len = 0;
1086 info->fix.type_aux = 0;
1087 info->fix.xpanstep = 1; /* doing it in hw */
1088 info->fix.ypanstep = 1; /* doing it in hw */
1089 info->fix.ywrapstep = 0;
1090 info->fix.accel = FB_ACCEL_NONE;
1091 info->fix.type_aux = 0;
1093 info->fix.line_length = pitch;
1097 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1098 uint32_t fb_width, uint32_t fb_height)
1100 struct drm_framebuffer *fb = fb_helper->fb;
1101 info->pseudo_palette = fb_helper->pseudo_palette;
1102 info->var.xres_virtual = fb->width;
1103 info->var.yres_virtual = fb->height;
1104 info->var.bits_per_pixel = fb->bits_per_pixel;
1105 info->var.accel_flags = FB_ACCELF_TEXT;
1106 info->var.xoffset = 0;
1107 info->var.yoffset = 0;
1108 info->var.activate = FB_ACTIVATE_NOW;
1109 info->var.height = -1;
1110 info->var.width = -1;
1112 switch (fb->depth) {
1114 info->var.red.offset = 0;
1115 info->var.green.offset = 0;
1116 info->var.blue.offset = 0;
1117 info->var.red.length = 8; /* 8bit DAC */
1118 info->var.green.length = 8;
1119 info->var.blue.length = 8;
1120 info->var.transp.offset = 0;
1121 info->var.transp.length = 0;
1124 info->var.red.offset = 10;
1125 info->var.green.offset = 5;
1126 info->var.blue.offset = 0;
1127 info->var.red.length = 5;
1128 info->var.green.length = 5;
1129 info->var.blue.length = 5;
1130 info->var.transp.offset = 15;
1131 info->var.transp.length = 1;
1134 info->var.red.offset = 11;
1135 info->var.green.offset = 5;
1136 info->var.blue.offset = 0;
1137 info->var.red.length = 5;
1138 info->var.green.length = 6;
1139 info->var.blue.length = 5;
1140 info->var.transp.offset = 0;
1143 info->var.red.offset = 16;
1144 info->var.green.offset = 8;
1145 info->var.blue.offset = 0;
1146 info->var.red.length = 8;
1147 info->var.green.length = 8;
1148 info->var.blue.length = 8;
1149 info->var.transp.offset = 0;
1150 info->var.transp.length = 0;
1153 info->var.red.offset = 16;
1154 info->var.green.offset = 8;
1155 info->var.blue.offset = 0;
1156 info->var.red.length = 8;
1157 info->var.green.length = 8;
1158 info->var.blue.length = 8;
1159 info->var.transp.offset = 24;
1160 info->var.transp.length = 8;
1166 info->var.xres = fb_width;
1167 info->var.yres = fb_height;
1171 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1175 struct drm_connector *connector;
1179 for (i = 0; i < fb_helper->connector_count; i++) {
1180 connector = fb_helper->connector_info[i]->connector;
1181 count += connector->funcs->fill_modes(connector, maxX, maxY);
1187 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1189 struct drm_display_mode *mode;
1191 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1192 if (drm_mode_width(mode) > width ||
1193 drm_mode_height(mode) > height)
1195 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1201 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1203 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1204 cmdline_mode = &fb_connector->cmdline_mode;
1205 return cmdline_mode->specified;
1208 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1209 int width, int height)
1211 struct drm_cmdline_mode *cmdline_mode;
1212 struct drm_display_mode *mode = NULL;
1214 cmdline_mode = &fb_helper_conn->cmdline_mode1;
1215 if (cmdline_mode->specified == false &&
1216 !drm_fetch_cmdline_mode_from_kenv(fb_helper_conn->connector,
1220 /* attempt to find a matching mode in the list of modes
1221 * we have gotten so far, if not add a CVT mode that conforms
1223 if (cmdline_mode->rb || cmdline_mode->margins)
1226 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1227 /* check width/height */
1228 if (mode->hdisplay != cmdline_mode->xres ||
1229 mode->vdisplay != cmdline_mode->yres)
1232 if (cmdline_mode->refresh_specified) {
1233 if (mode->vrefresh != cmdline_mode->refresh)
1237 if (cmdline_mode->interlace) {
1238 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1245 if (cmdline_mode->cvt)
1246 mode = drm_cvt_mode(fb_helper_conn->connector->dev,
1247 cmdline_mode->xres, cmdline_mode->yres,
1248 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1249 cmdline_mode->rb, cmdline_mode->interlace,
1250 cmdline_mode->margins);
1252 mode = drm_gtf_mode(fb_helper_conn->connector->dev,
1253 cmdline_mode->xres, cmdline_mode->yres,
1254 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1255 cmdline_mode->interlace,
1256 cmdline_mode->margins);
1257 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1258 list_add(&mode->head, &fb_helper_conn->connector->modes);
1262 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1267 enable = connector->status == connector_status_connected;
1269 enable = connector->status != connector_status_disconnected;
1274 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1277 bool any_enabled = false;
1278 struct drm_connector *connector;
1281 for (i = 0; i < fb_helper->connector_count; i++) {
1282 connector = fb_helper->connector_info[i]->connector;
1283 enabled[i] = drm_connector_enabled(connector, true);
1284 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1285 enabled[i] ? "yes" : "no");
1286 any_enabled |= enabled[i];
1292 for (i = 0; i < fb_helper->connector_count; i++) {
1293 connector = fb_helper->connector_info[i]->connector;
1294 enabled[i] = drm_connector_enabled(connector, false);
1298 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1299 struct drm_display_mode **modes,
1300 bool *enabled, int width, int height)
1303 bool can_clone = false;
1304 struct drm_fb_helper_connector *fb_helper_conn;
1305 struct drm_display_mode *dmt_mode, *mode;
1307 /* only contemplate cloning in the single crtc case */
1308 if (fb_helper->crtc_count > 1)
1312 for (i = 0; i < fb_helper->connector_count; i++) {
1317 /* only contemplate cloning if more than one connector is enabled */
1321 /* check the command line or if nothing common pick 1024x768 */
1323 for (i = 0; i < fb_helper->connector_count; i++) {
1326 fb_helper_conn = fb_helper->connector_info[i];
1327 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1332 for (j = 0; j < i; j++) {
1335 if (!drm_mode_equal(modes[j], modes[i]))
1341 DRM_DEBUG_KMS("can clone using command line\n");
1345 /* try and find a 1024x768 mode on each connector */
1347 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1349 for (i = 0; i < fb_helper->connector_count; i++) {
1354 fb_helper_conn = fb_helper->connector_info[i];
1355 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1356 if (drm_mode_equal(mode, dmt_mode))
1364 DRM_DEBUG_KMS("can clone using 1024x768\n");
1367 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1371 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1372 struct drm_display_mode **modes,
1373 bool *enabled, int width, int height)
1375 struct drm_fb_helper_connector *fb_helper_conn;
1378 for (i = 0; i < fb_helper->connector_count; i++) {
1379 fb_helper_conn = fb_helper->connector_info[i];
1381 if (enabled[i] == false)
1384 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1385 fb_helper_conn->connector->base.id);
1387 /* got for command line mode first */
1388 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1390 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1391 fb_helper_conn->connector->base.id);
1392 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1394 /* No preferred modes, pick one off the list */
1395 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1396 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1399 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1405 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1406 struct drm_fb_helper_crtc **best_crtcs,
1407 struct drm_display_mode **modes,
1408 int n, int width, int height)
1411 struct drm_device *dev = fb_helper->dev;
1412 struct drm_connector *connector;
1413 struct drm_connector_helper_funcs *connector_funcs;
1414 struct drm_encoder *encoder;
1415 struct drm_fb_helper_crtc *best_crtc;
1416 int my_score, best_score, score;
1417 struct drm_fb_helper_crtc **crtcs, *crtc;
1418 struct drm_fb_helper_connector *fb_helper_conn;
1420 if (n == fb_helper->connector_count)
1423 fb_helper_conn = fb_helper->connector_info[n];
1424 connector = fb_helper_conn->connector;
1426 best_crtcs[n] = NULL;
1428 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1429 if (modes[n] == NULL)
1432 crtcs = malloc(dev->mode_config.num_connector *
1433 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1437 if (connector->status == connector_status_connected)
1439 if (drm_has_cmdline_mode(fb_helper_conn))
1441 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1444 connector_funcs = connector->helper_private;
1445 encoder = connector_funcs->best_encoder(connector);
1449 /* select a crtc for this connector and then attempt to configure
1450 remaining connectors */
1451 for (c = 0; c < fb_helper->crtc_count; c++) {
1452 crtc = &fb_helper->crtc_info[c];
1454 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1458 for (o = 0; o < n; o++)
1459 if (best_crtcs[o] == crtc)
1463 /* ignore cloning unless only a single crtc */
1464 if (fb_helper->crtc_count > 1)
1467 if (!drm_mode_equal(modes[o], modes[n]))
1472 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1473 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1475 if (score > best_score) {
1478 memcpy(best_crtcs, crtcs,
1479 dev->mode_config.num_connector *
1480 sizeof(struct drm_fb_helper_crtc *));
1484 free(crtcs, DRM_MEM_KMS);
1488 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1490 struct drm_device *dev = fb_helper->dev;
1491 struct drm_fb_helper_crtc **crtcs;
1492 struct drm_display_mode **modes;
1493 struct drm_encoder *encoder;
1494 struct drm_mode_set *modeset;
1499 DRM_DEBUG_KMS("\n");
1501 width = dev->mode_config.max_width;
1502 height = dev->mode_config.max_height;
1504 /* clean out all the encoder/crtc combos */
1505 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1506 encoder->crtc = NULL;
1509 crtcs = malloc(dev->mode_config.num_connector *
1510 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1512 modes = malloc(dev->mode_config.num_connector *
1513 sizeof(struct drm_display_mode *), DRM_MEM_KMS,
1515 enabled = malloc(dev->mode_config.num_connector *
1516 sizeof(bool), DRM_MEM_KMS, M_WAITOK | M_ZERO);
1518 drm_enable_connectors(fb_helper, enabled);
1520 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1522 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1524 DRM_ERROR("Unable to find initial modes\n");
1527 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1529 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1531 /* need to set the modesets up here for use later */
1532 /* fill out the connector<->crtc mappings into the modesets */
1533 for (i = 0; i < fb_helper->crtc_count; i++) {
1534 modeset = &fb_helper->crtc_info[i].mode_set;
1535 modeset->num_connectors = 0;
1538 for (i = 0; i < fb_helper->connector_count; i++) {
1539 struct drm_display_mode *mode = modes[i];
1540 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1541 modeset = &fb_crtc->mode_set;
1543 if (mode && fb_crtc) {
1544 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1545 mode->name, fb_crtc->mode_set.crtc->base.id);
1546 fb_crtc->desired_mode = mode;
1548 drm_mode_destroy(dev, modeset->mode);
1549 modeset->mode = drm_mode_duplicate(dev,
1550 fb_crtc->desired_mode);
1551 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1555 free(crtcs, DRM_MEM_KMS);
1556 free(modes, DRM_MEM_KMS);
1557 free(enabled, DRM_MEM_KMS);
1561 * drm_helper_initial_config - setup a sane initial connector configuration
1565 * Called at init time, must take mode config lock.
1567 * Scan the CRTCs and connectors and try to put together an initial setup.
1568 * At the moment, this is a cloned configuration across all heads with
1569 * a new framebuffer object as the backing store.
1572 * Zero if everything went ok, nonzero otherwise.
1574 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1576 struct drm_device *dev = fb_helper->dev;
1579 /* disable all the possible outputs/crtcs before entering KMS mode */
1580 drm_helper_disable_unused_functions(fb_helper->dev);
1582 drm_fb_helper_parse_command_line(fb_helper);
1584 count = drm_fb_helper_probe_connector_modes(fb_helper,
1585 dev->mode_config.max_width,
1586 dev->mode_config.max_height);
1588 * we shouldn't end up with no modes here.
1591 printf("No connectors reported connected with modes\n");
1593 drm_setup_crtcs(fb_helper);
1595 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1598 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1600 struct drm_device *dev = fb_helper->dev;
1602 u32 max_width, max_height, bpp_sel;
1603 bool bound = false, crtcs_bound = false;
1604 struct drm_crtc *crtc;
1609 sx_xlock(&dev->mode_config.mutex);
1610 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1613 if (crtc->fb == fb_helper->fb)
1617 if (!bound && crtcs_bound) {
1618 fb_helper->delayed_hotplug = true;
1619 sx_xunlock(&dev->mode_config.mutex);
1622 DRM_DEBUG_KMS("\n");
1624 max_width = fb_helper->fb->width;
1625 max_height = fb_helper->fb->height;
1626 bpp_sel = fb_helper->fb->bits_per_pixel;
1628 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1630 drm_setup_crtcs(fb_helper);
1631 sx_xunlock(&dev->mode_config.mutex);
1633 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);