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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36 #include <dev/drm2/drmP.h>
37 #include <dev/drm2/drm_crtc.h>
38 #include <dev/drm2/drm_fb_helper.h>
39 #include <dev/drm2/drm_crtc_helper.h>
41 MODULE_AUTHOR("David Airlie, Jesse Barnes");
42 MODULE_DESCRIPTION("DRM KMS helper");
43 MODULE_LICENSE("GPL and additional rights");
45 static DRM_LIST_HEAD(kernel_fb_helper_list);
48 #include <sys/param.h>
49 #include <sys/systm.h>
52 struct drm_fb_helper *fb_helper;
53 struct task fb_mode_task;
56 /* Call restore out of vt(9) locks. */
58 vt_restore_fbdev_mode(void *arg, int pending)
60 struct drm_fb_helper *fb_helper;
61 struct vt_kms_softc *sc;
63 sc = (struct vt_kms_softc *)arg;
64 fb_helper = sc->fb_helper;
65 sx_xlock(&fb_helper->dev->mode_config.mutex);
66 drm_fb_helper_restore_fbdev_mode(fb_helper);
67 sx_xunlock(&fb_helper->dev->mode_config.mutex);
71 vt_kms_postswitch(void *arg)
73 struct vt_kms_softc *sc;
75 sc = (struct vt_kms_softc *)arg;
77 if (!kdb_active && panicstr == NULL)
78 taskqueue_enqueue(taskqueue_thread, &sc->fb_mode_task);
80 drm_fb_helper_restore_fbdev_mode(sc->fb_helper);
89 struct vt_kms_softc *sc;
91 info = malloc(sizeof(*info), DRM_MEM_KMS, M_WAITOK | M_ZERO);
93 sc = malloc(sizeof(*sc), DRM_MEM_KMS, M_WAITOK | M_ZERO);
94 TASK_INIT(&sc->fb_mode_task, 0, vt_restore_fbdev_mode, sc);
97 info->enter = &vt_kms_postswitch;
103 framebuffer_release(struct fb_info *info)
106 free(info->fb_priv, DRM_MEM_KMS);
107 free(info, DRM_MEM_KMS);
111 fb_get_options(const char *connector_name, char **option)
116 * A user may use loader tunables to set a specific mode for the
117 * console. Tunables are read in the following order:
118 * 1. kern.vt.fb.modes.$connector_name
119 * 2. kern.vt.fb.default_mode
121 * Example of a mode specific to the LVDS connector:
122 * kern.vt.fb.modes.LVDS="1024x768"
124 * Example of a mode applied to all connectors not having a
125 * connector-specific mode:
126 * kern.vt.fb.default_mode="640x480"
128 snprintf(tunable, sizeof(tunable), "kern.vt.fb.modes.%s",
130 DRM_INFO("Connector %s: get mode from tunables:\n", connector_name);
131 DRM_INFO(" - %s\n", tunable);
132 DRM_INFO(" - kern.vt.fb.default_mode\n");
133 *option = kern_getenv(tunable);
135 *option = kern_getenv("kern.vt.fb.default_mode");
137 return (*option != NULL ? 0 : -ENOENT);
143 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
144 * mode setting driver. They can be used mostly independantely from the crtc
145 * helper functions used by many drivers to implement the kernel mode setting
149 /* simple single crtc case helper function */
150 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
152 struct drm_device *dev = fb_helper->dev;
153 struct drm_connector *connector;
156 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
157 struct drm_fb_helper_connector *fb_helper_connector;
159 fb_helper_connector = malloc(sizeof(struct drm_fb_helper_connector),
160 DRM_MEM_KMS, M_NOWAIT | M_ZERO);
161 if (!fb_helper_connector)
164 fb_helper_connector->connector = connector;
165 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
169 for (i = 0; i < fb_helper->connector_count; i++) {
170 free(fb_helper->connector_info[i], DRM_MEM_KMS);
171 fb_helper->connector_info[i] = NULL;
173 fb_helper->connector_count = 0;
176 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
178 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
180 struct drm_fb_helper_connector *fb_helper_conn;
183 for (i = 0; i < fb_helper->connector_count; i++) {
184 struct drm_cmdline_mode *mode;
185 struct drm_connector *connector;
188 fb_helper_conn = fb_helper->connector_info[i];
189 connector = fb_helper_conn->connector;
190 mode = &fb_helper_conn->cmdline_mode;
192 /* do something on return - turn off connector maybe */
193 if (fb_get_options(drm_get_connector_name(connector), &option))
196 if (drm_mode_parse_command_line_for_connector(option,
201 switch (mode->force) {
205 case DRM_FORCE_ON_DIGITAL:
214 DRM_INFO("forcing %s connector %s\n",
215 drm_get_connector_name(connector), s);
216 connector->force = mode->force;
219 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
220 drm_get_connector_name(connector),
221 mode->xres, mode->yres,
222 mode->refresh_specified ? mode->refresh : 60,
223 mode->rb ? " reduced blanking" : "",
224 mode->margins ? " with margins" : "",
225 mode->interlace ? " interlaced" : "");
233 #if 0 && defined(FREEBSD_NOTYET)
234 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
236 uint16_t *r_base, *g_base, *b_base;
239 r_base = crtc->gamma_store;
240 g_base = r_base + crtc->gamma_size;
241 b_base = g_base + crtc->gamma_size;
243 for (i = 0; i < crtc->gamma_size; i++)
244 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
247 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
249 uint16_t *r_base, *g_base, *b_base;
251 if (crtc->funcs->gamma_set == NULL)
254 r_base = crtc->gamma_store;
255 g_base = r_base + crtc->gamma_size;
256 b_base = g_base + crtc->gamma_size;
258 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
261 int drm_fb_helper_debug_enter(struct fb_info *info)
263 struct drm_fb_helper *helper = info->par;
264 struct drm_crtc_helper_funcs *funcs;
267 if (list_empty(&kernel_fb_helper_list))
270 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
271 for (i = 0; i < helper->crtc_count; i++) {
272 struct drm_mode_set *mode_set =
273 &helper->crtc_info[i].mode_set;
275 if (!mode_set->crtc->enabled)
278 funcs = mode_set->crtc->helper_private;
279 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
280 funcs->mode_set_base_atomic(mode_set->crtc,
284 ENTER_ATOMIC_MODE_SET);
290 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
292 /* Find the real fb for a given fb helper CRTC */
293 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
295 struct drm_device *dev = crtc->dev;
298 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
299 if (crtc->base.id == c->base.id)
306 int drm_fb_helper_debug_leave(struct fb_info *info)
308 struct drm_fb_helper *helper = info->par;
309 struct drm_crtc *crtc;
310 struct drm_crtc_helper_funcs *funcs;
311 struct drm_framebuffer *fb;
314 for (i = 0; i < helper->crtc_count; i++) {
315 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
316 crtc = mode_set->crtc;
317 funcs = crtc->helper_private;
318 fb = drm_mode_config_fb(crtc);
324 DRM_ERROR("no fb to restore??\n");
328 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
329 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
330 crtc->y, LEAVE_ATOMIC_MODE_SET);
335 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
336 #endif /* FREEBSD_NOTYET */
338 bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
343 for (i = 0; i < fb_helper->crtc_count; i++) {
344 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
345 ret = mode_set->crtc->funcs->set_config(mode_set);
351 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode);
353 static bool drm_fb_helper_force_kernel_mode(void)
355 bool ret, error = false;
356 struct drm_fb_helper *helper;
358 if (list_empty(&kernel_fb_helper_list))
361 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
362 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
365 ret = drm_fb_helper_restore_fbdev_mode(helper);
372 #if 0 && defined(FREEBSD_NOTYET)
373 int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
377 * It's a waste of time and effort to switch back to text console
378 * if the kernel should reboot before panic messages can be seen.
380 if (panic_timeout < 0)
383 pr_err("panic occurred, switching back to text console\n");
384 return drm_fb_helper_force_kernel_mode();
386 EXPORT_SYMBOL(drm_fb_helper_panic);
388 static struct notifier_block paniced = {
389 .notifier_call = drm_fb_helper_panic,
391 #endif /* FREEBSD_NOTYET */
394 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
396 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
398 void drm_fb_helper_restore(void)
401 ret = drm_fb_helper_force_kernel_mode();
403 DRM_ERROR("Failed to restore crtc configuration\n");
405 EXPORT_SYMBOL(drm_fb_helper_restore);
408 #ifdef CONFIG_MAGIC_SYSRQ
409 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
411 drm_fb_helper_restore();
413 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
415 static void drm_fb_helper_sysrq(int dummy1)
417 schedule_work(&drm_fb_helper_restore_work);
420 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
421 .handler = drm_fb_helper_sysrq,
422 .help_msg = "force-fb(V)",
423 .action_msg = "Restore framebuffer console",
426 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
430 #if 0 && defined(FREEBSD_NOTYET)
431 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
433 struct drm_fb_helper *fb_helper = info->par;
434 struct drm_device *dev = fb_helper->dev;
435 struct drm_crtc *crtc;
436 struct drm_connector *connector;
440 * For each CRTC in this fb, turn the connectors on/off.
442 sx_xlock(&dev->mode_config.mutex);
443 for (i = 0; i < fb_helper->crtc_count; i++) {
444 crtc = fb_helper->crtc_info[i].mode_set.crtc;
449 /* Walk the connectors & encoders on this fb turning them on/off */
450 for (j = 0; j < fb_helper->connector_count; j++) {
451 connector = fb_helper->connector_info[j]->connector;
452 connector->funcs->dpms(connector, dpms_mode);
453 drm_object_property_set_value(&connector->base,
454 dev->mode_config.dpms_property, dpms_mode);
457 sx_xunlock(&dev->mode_config.mutex);
460 int drm_fb_helper_blank(int blank, struct fb_info *info)
463 /* Display: On; HSync: On, VSync: On */
464 case FB_BLANK_UNBLANK:
465 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
467 /* Display: Off; HSync: On, VSync: On */
468 case FB_BLANK_NORMAL:
469 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
471 /* Display: Off; HSync: Off, VSync: On */
472 case FB_BLANK_HSYNC_SUSPEND:
473 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
475 /* Display: Off; HSync: On, VSync: Off */
476 case FB_BLANK_VSYNC_SUSPEND:
477 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
479 /* Display: Off; HSync: Off, VSync: Off */
480 case FB_BLANK_POWERDOWN:
481 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
486 EXPORT_SYMBOL(drm_fb_helper_blank);
487 #endif /* FREEBSD_NOTYET */
489 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
493 for (i = 0; i < helper->connector_count; i++)
494 free(helper->connector_info[i], DRM_MEM_KMS);
495 free(helper->connector_info, DRM_MEM_KMS);
496 for (i = 0; i < helper->crtc_count; i++) {
497 free(helper->crtc_info[i].mode_set.connectors, DRM_MEM_KMS);
498 if (helper->crtc_info[i].mode_set.mode)
499 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
501 free(helper->crtc_info, DRM_MEM_KMS);
504 int drm_fb_helper_init(struct drm_device *dev,
505 struct drm_fb_helper *fb_helper,
506 int crtc_count, int max_conn_count)
508 struct drm_crtc *crtc;
511 fb_helper->dev = dev;
513 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
515 fb_helper->crtc_info = malloc(crtc_count * sizeof(struct drm_fb_helper_crtc),
516 DRM_MEM_KMS, M_NOWAIT | M_ZERO);
517 if (!fb_helper->crtc_info)
520 fb_helper->crtc_count = crtc_count;
521 fb_helper->connector_info = malloc(dev->mode_config.num_connector * sizeof(struct drm_fb_helper_connector *),
522 DRM_MEM_KMS, M_NOWAIT | M_ZERO);
523 if (!fb_helper->connector_info) {
524 free(fb_helper->crtc_info, DRM_MEM_KMS);
527 fb_helper->connector_count = 0;
529 for (i = 0; i < crtc_count; i++) {
530 fb_helper->crtc_info[i].mode_set.connectors =
531 malloc(max_conn_count *
532 sizeof(struct drm_connector *),
533 DRM_MEM_KMS, M_NOWAIT | M_ZERO);
535 if (!fb_helper->crtc_info[i].mode_set.connectors)
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].mode_set.crtc = crtc;
548 drm_fb_helper_crtc_free(fb_helper);
551 EXPORT_SYMBOL(drm_fb_helper_init);
553 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
555 if (!list_empty(&fb_helper->kernel_fb_list)) {
556 list_del(&fb_helper->kernel_fb_list);
557 #if 0 && defined(FREEBSD_NOTYET)
558 if (list_empty(&kernel_fb_helper_list)) {
559 pr_info("drm: unregistered panic notifier\n");
560 atomic_notifier_chain_unregister(&panic_notifier_list,
562 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
564 #endif /* FREEBSD_NOTYET */
567 drm_fb_helper_crtc_free(fb_helper);
570 EXPORT_SYMBOL(drm_fb_helper_fini);
572 #if 0 && defined(FREEBSD_NOTYET)
573 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
574 u16 blue, u16 regno, struct fb_info *info)
576 struct drm_fb_helper *fb_helper = info->par;
577 struct drm_framebuffer *fb = fb_helper->fb;
580 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
583 /* place color in pseudopalette */
586 palette = (u32 *)info->pseudo_palette;
587 red >>= (16 - info->var.red.length);
588 green >>= (16 - info->var.green.length);
589 blue >>= (16 - info->var.blue.length);
590 value = (red << info->var.red.offset) |
591 (green << info->var.green.offset) |
592 (blue << info->var.blue.offset);
593 if (info->var.transp.length > 0) {
594 u32 mask = (1 << info->var.transp.length) - 1;
595 mask <<= info->var.transp.offset;
598 palette[regno] = value;
604 if (fb->bits_per_pixel == 16) {
607 if (fb->depth == 16 && regno > 63)
609 if (fb->depth == 15 && regno > 31)
612 if (fb->depth == 16) {
616 for (i = 0; i < 8; i++)
617 fb_helper->funcs->gamma_set(crtc, red,
618 green, blue, pindex + i);
621 fb_helper->funcs->gamma_get(crtc, &r,
625 for (i = 0; i < 4; i++)
626 fb_helper->funcs->gamma_set(crtc, r,
633 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
637 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
639 struct drm_fb_helper *fb_helper = info->par;
640 struct drm_crtc_helper_funcs *crtc_funcs;
641 u16 *red, *green, *blue, *transp;
642 struct drm_crtc *crtc;
646 for (i = 0; i < fb_helper->crtc_count; i++) {
647 crtc = fb_helper->crtc_info[i].mode_set.crtc;
648 crtc_funcs = crtc->helper_private;
653 transp = cmap->transp;
656 for (j = 0; j < cmap->len; j++) {
657 u16 hred, hgreen, hblue, htransp = 0xffff;
666 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
670 crtc_funcs->load_lut(crtc);
674 EXPORT_SYMBOL(drm_fb_helper_setcmap);
676 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
677 struct fb_info *info)
679 struct drm_fb_helper *fb_helper = info->par;
680 struct drm_framebuffer *fb = fb_helper->fb;
683 if (var->pixclock != 0 || in_dbg_master())
686 /* Need to resize the fb object !!! */
687 if (var->bits_per_pixel > fb->bits_per_pixel ||
688 var->xres > fb->width || var->yres > fb->height ||
689 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
690 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
691 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
692 var->xres, var->yres, var->bits_per_pixel,
693 var->xres_virtual, var->yres_virtual,
694 fb->width, fb->height, fb->bits_per_pixel);
698 switch (var->bits_per_pixel) {
700 depth = (var->green.length == 6) ? 16 : 15;
703 depth = (var->transp.length > 0) ? 32 : 24;
706 depth = var->bits_per_pixel;
713 var->green.offset = 0;
714 var->blue.offset = 0;
716 var->green.length = 8;
717 var->blue.length = 8;
718 var->transp.length = 0;
719 var->transp.offset = 0;
722 var->red.offset = 10;
723 var->green.offset = 5;
724 var->blue.offset = 0;
726 var->green.length = 5;
727 var->blue.length = 5;
728 var->transp.length = 1;
729 var->transp.offset = 15;
732 var->red.offset = 11;
733 var->green.offset = 5;
734 var->blue.offset = 0;
736 var->green.length = 6;
737 var->blue.length = 5;
738 var->transp.length = 0;
739 var->transp.offset = 0;
742 var->red.offset = 16;
743 var->green.offset = 8;
744 var->blue.offset = 0;
746 var->green.length = 8;
747 var->blue.length = 8;
748 var->transp.length = 0;
749 var->transp.offset = 0;
752 var->red.offset = 16;
753 var->green.offset = 8;
754 var->blue.offset = 0;
756 var->green.length = 8;
757 var->blue.length = 8;
758 var->transp.length = 8;
759 var->transp.offset = 24;
766 EXPORT_SYMBOL(drm_fb_helper_check_var);
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 sx_xlock(&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 sx_xunlock(&dev->mode_config.mutex);
792 sx_xunlock(&dev->mode_config.mutex);
794 if (fb_helper->delayed_hotplug) {
795 fb_helper->delayed_hotplug = false;
796 drm_fb_helper_hotplug_event(fb_helper);
800 EXPORT_SYMBOL(drm_fb_helper_set_par);
802 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
803 struct fb_info *info)
805 struct drm_fb_helper *fb_helper = info->par;
806 struct drm_device *dev = fb_helper->dev;
807 struct drm_mode_set *modeset;
808 struct drm_crtc *crtc;
812 sx_xlock(&dev->mode_config.mutex);
813 for (i = 0; i < fb_helper->crtc_count; i++) {
814 crtc = fb_helper->crtc_info[i].mode_set.crtc;
816 modeset = &fb_helper->crtc_info[i].mode_set;
818 modeset->x = var->xoffset;
819 modeset->y = var->yoffset;
821 if (modeset->num_connectors) {
822 ret = crtc->funcs->set_config(modeset);
824 info->var.xoffset = var->xoffset;
825 info->var.yoffset = var->yoffset;
829 sx_xunlock(&dev->mode_config.mutex);
832 EXPORT_SYMBOL(drm_fb_helper_pan_display);
833 #endif /* FREEBSD_NOTYET */
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 #if defined(__FreeBSD__)
845 struct drm_crtc *crtc;
846 struct drm_device *dev;
851 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
852 sizes.surface_depth = 24;
853 sizes.surface_bpp = 32;
854 sizes.fb_width = (unsigned)-1;
855 sizes.fb_height = (unsigned)-1;
857 /* if driver picks 8 or 16 by default use that
858 for both depth/bpp */
859 if (preferred_bpp != sizes.surface_bpp)
860 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
862 /* first up get a count of crtcs now in use and new min/maxes width/heights */
863 for (i = 0; i < fb_helper->connector_count; i++) {
864 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
865 struct drm_cmdline_mode *cmdline_mode;
867 cmdline_mode = &fb_helper_conn->cmdline_mode;
869 if (cmdline_mode->bpp_specified) {
870 switch (cmdline_mode->bpp) {
872 sizes.surface_depth = sizes.surface_bpp = 8;
875 sizes.surface_depth = 15;
876 sizes.surface_bpp = 16;
879 sizes.surface_depth = sizes.surface_bpp = 16;
882 sizes.surface_depth = sizes.surface_bpp = 24;
885 sizes.surface_depth = 24;
886 sizes.surface_bpp = 32;
894 for (i = 0; i < fb_helper->crtc_count; i++) {
895 struct drm_display_mode *desired_mode;
896 desired_mode = fb_helper->crtc_info[i].desired_mode;
900 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
901 if (desired_mode->hdisplay < sizes.fb_width)
902 sizes.fb_width = desired_mode->hdisplay;
903 if (desired_mode->vdisplay < sizes.fb_height)
904 sizes.fb_height = desired_mode->vdisplay;
905 if (desired_mode->hdisplay > sizes.surface_width)
906 sizes.surface_width = desired_mode->hdisplay;
907 if (desired_mode->vdisplay > sizes.surface_height)
908 sizes.surface_height = desired_mode->vdisplay;
913 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
914 /* hmm everyone went away - assume VGA cable just fell out
915 and will come back later. */
916 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
917 sizes.fb_width = sizes.surface_width = 1024;
918 sizes.fb_height = sizes.surface_height = 768;
921 /* push down into drivers */
922 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
926 info = fb_helper->fbdev;
928 kdev = fb_helper->dev->dev;
929 info->fb_video_dev = device_get_parent(kdev);
931 /* set the fb pointer */
932 for (i = 0; i < fb_helper->crtc_count; i++)
933 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
935 #if defined(__FreeBSD__)
939 info->fb_fbd_dev = device_add_child(kdev, "fbd",
940 device_get_unit(kdev));
941 if (info->fb_fbd_dev != NULL)
942 ret = device_probe_and_attach(info->fb_fbd_dev);
947 DRM_ERROR("Failed to attach fbd device: %d\n", ret);
950 /* Modified version of drm_fb_helper_set_par() */
951 dev = fb_helper->dev;
952 sx_xlock(&dev->mode_config.mutex);
953 for (i = 0; i < fb_helper->crtc_count; i++) {
954 crtc = fb_helper->crtc_info[i].mode_set.crtc;
955 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
957 sx_xunlock(&dev->mode_config.mutex);
961 sx_xunlock(&dev->mode_config.mutex);
963 if (fb_helper->delayed_hotplug) {
964 fb_helper->delayed_hotplug = false;
965 drm_fb_helper_hotplug_event(fb_helper);
970 info->var.pixclock = 0;
971 if (register_framebuffer(info) < 0)
974 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
975 info->node, info->fix.id);
978 drm_fb_helper_set_par(info);
982 #if 0 && defined(FREEBSD_NOTYET)
983 /* Switch back to kernel console on panic */
984 /* multi card linked list maybe */
985 if (list_empty(&kernel_fb_helper_list)) {
986 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
987 atomic_notifier_chain_register(&panic_notifier_list,
989 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
991 #endif /* FREEBSD_NOTYET */
993 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
997 EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
999 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1002 info->fb_stride = pitch;
1006 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1008 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1009 uint32_t fb_width, uint32_t fb_height)
1011 struct drm_framebuffer *fb = fb_helper->fb;
1012 struct vt_kms_softc *sc;
1014 info->fb_name = device_get_nameunit(fb_helper->dev->dev);
1015 info->fb_width = fb->width;
1016 info->fb_height = fb->height;
1017 info->fb_depth = fb->bits_per_pixel;
1019 sc = (struct vt_kms_softc *)info->fb_priv;
1020 sc->fb_helper = fb_helper;
1022 EXPORT_SYMBOL(drm_fb_helper_fill_var);
1024 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1028 struct drm_connector *connector;
1032 for (i = 0; i < fb_helper->connector_count; i++) {
1033 connector = fb_helper->connector_info[i]->connector;
1034 count += connector->funcs->fill_modes(connector, maxX, maxY);
1040 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1042 struct drm_display_mode *mode;
1044 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1045 if (drm_mode_width(mode) > width ||
1046 drm_mode_height(mode) > height)
1048 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1054 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1056 struct drm_cmdline_mode *cmdline_mode;
1057 cmdline_mode = &fb_connector->cmdline_mode;
1058 return cmdline_mode->specified;
1061 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1062 int width, int height)
1064 struct drm_cmdline_mode *cmdline_mode;
1065 struct drm_display_mode *mode = NULL;
1067 cmdline_mode = &fb_helper_conn->cmdline_mode;
1068 if (cmdline_mode->specified == false)
1071 /* attempt to find a matching mode in the list of modes
1072 * we have gotten so far, if not add a CVT mode that conforms
1074 if (cmdline_mode->rb || cmdline_mode->margins)
1077 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1078 /* check width/height */
1079 if (mode->hdisplay != cmdline_mode->xres ||
1080 mode->vdisplay != cmdline_mode->yres)
1083 if (cmdline_mode->refresh_specified) {
1084 if (mode->vrefresh != cmdline_mode->refresh)
1088 if (cmdline_mode->interlace) {
1089 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1096 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1098 list_add(&mode->head, &fb_helper_conn->connector->modes);
1102 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1107 enable = connector->status == connector_status_connected;
1109 enable = connector->status != connector_status_disconnected;
1114 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1117 bool any_enabled = false;
1118 struct drm_connector *connector;
1121 for (i = 0; i < fb_helper->connector_count; i++) {
1122 connector = fb_helper->connector_info[i]->connector;
1123 enabled[i] = drm_connector_enabled(connector, true);
1124 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1125 enabled[i] ? "yes" : "no");
1126 any_enabled |= enabled[i];
1132 for (i = 0; i < fb_helper->connector_count; i++) {
1133 connector = fb_helper->connector_info[i]->connector;
1134 enabled[i] = drm_connector_enabled(connector, false);
1138 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1139 struct drm_display_mode **modes,
1140 bool *enabled, int width, int height)
1143 bool can_clone = false;
1144 struct drm_fb_helper_connector *fb_helper_conn;
1145 struct drm_display_mode *dmt_mode, *mode;
1147 /* only contemplate cloning in the single crtc case */
1148 if (fb_helper->crtc_count > 1)
1152 for (i = 0; i < fb_helper->connector_count; i++) {
1157 /* only contemplate cloning if more than one connector is enabled */
1161 /* check the command line or if nothing common pick 1024x768 */
1163 for (i = 0; i < fb_helper->connector_count; i++) {
1166 fb_helper_conn = fb_helper->connector_info[i];
1167 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1172 for (j = 0; j < i; j++) {
1175 if (!drm_mode_equal(modes[j], modes[i]))
1181 DRM_DEBUG_KMS("can clone using command line\n");
1185 /* try and find a 1024x768 mode on each connector */
1187 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1189 for (i = 0; i < fb_helper->connector_count; i++) {
1194 fb_helper_conn = fb_helper->connector_info[i];
1195 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1196 if (drm_mode_equal(mode, dmt_mode))
1204 DRM_DEBUG_KMS("can clone using 1024x768\n");
1207 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1211 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1212 struct drm_display_mode **modes,
1213 bool *enabled, int width, int height)
1215 struct drm_fb_helper_connector *fb_helper_conn;
1218 for (i = 0; i < fb_helper->connector_count; i++) {
1219 fb_helper_conn = fb_helper->connector_info[i];
1221 if (enabled[i] == false)
1224 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1225 fb_helper_conn->connector->base.id);
1227 /* got for command line mode first */
1228 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1230 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1231 fb_helper_conn->connector->base.id);
1232 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1234 /* No preferred modes, pick one off the list */
1235 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1236 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1239 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1245 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1246 struct drm_fb_helper_crtc **best_crtcs,
1247 struct drm_display_mode **modes,
1248 int n, int width, int height)
1251 struct drm_device *dev = fb_helper->dev;
1252 struct drm_connector *connector;
1253 struct drm_connector_helper_funcs *connector_funcs;
1254 struct drm_encoder *encoder;
1255 struct drm_fb_helper_crtc *best_crtc;
1256 int my_score, best_score, score;
1257 struct drm_fb_helper_crtc **crtcs, *crtc;
1258 struct drm_fb_helper_connector *fb_helper_conn;
1260 if (n == fb_helper->connector_count)
1263 fb_helper_conn = fb_helper->connector_info[n];
1264 connector = fb_helper_conn->connector;
1266 best_crtcs[n] = NULL;
1268 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1269 if (modes[n] == NULL)
1272 crtcs = malloc(dev->mode_config.num_connector *
1273 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS, M_NOWAIT | M_ZERO);
1278 if (connector->status == connector_status_connected)
1280 if (drm_has_cmdline_mode(fb_helper_conn))
1282 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1285 connector_funcs = connector->helper_private;
1286 encoder = connector_funcs->best_encoder(connector);
1290 /* select a crtc for this connector and then attempt to configure
1291 remaining connectors */
1292 for (c = 0; c < fb_helper->crtc_count; c++) {
1293 crtc = &fb_helper->crtc_info[c];
1295 if ((encoder->possible_crtcs & (1 << c)) == 0)
1298 for (o = 0; o < n; o++)
1299 if (best_crtcs[o] == crtc)
1303 /* ignore cloning unless only a single crtc */
1304 if (fb_helper->crtc_count > 1)
1307 if (!drm_mode_equal(modes[o], modes[n]))
1312 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1313 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1315 if (score > best_score) {
1318 memcpy(best_crtcs, crtcs,
1319 dev->mode_config.num_connector *
1320 sizeof(struct drm_fb_helper_crtc *));
1324 free(crtcs, DRM_MEM_KMS);
1328 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1330 struct drm_device *dev = fb_helper->dev;
1331 struct drm_fb_helper_crtc **crtcs;
1332 struct drm_display_mode **modes;
1333 struct drm_mode_set *modeset;
1338 DRM_DEBUG_KMS("\n");
1340 width = dev->mode_config.max_width;
1341 height = dev->mode_config.max_height;
1343 crtcs = malloc(dev->mode_config.num_connector *
1344 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS, M_NOWAIT | M_ZERO);
1345 modes = malloc(dev->mode_config.num_connector *
1346 sizeof(struct drm_display_mode *), DRM_MEM_KMS, M_NOWAIT | M_ZERO);
1347 enabled = malloc(dev->mode_config.num_connector *
1348 sizeof(bool), DRM_MEM_KMS, M_NOWAIT | M_ZERO);
1349 if (!crtcs || !modes || !enabled) {
1350 DRM_ERROR("Memory allocation failed\n");
1355 drm_enable_connectors(fb_helper, enabled);
1357 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1359 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1361 DRM_ERROR("Unable to find initial modes\n");
1364 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1366 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1368 /* need to set the modesets up here for use later */
1369 /* fill out the connector<->crtc mappings into the modesets */
1370 for (i = 0; i < fb_helper->crtc_count; i++) {
1371 modeset = &fb_helper->crtc_info[i].mode_set;
1372 modeset->num_connectors = 0;
1375 for (i = 0; i < fb_helper->connector_count; i++) {
1376 struct drm_display_mode *mode = modes[i];
1377 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1378 modeset = &fb_crtc->mode_set;
1380 if (mode && fb_crtc) {
1381 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1382 mode->name, fb_crtc->mode_set.crtc->base.id);
1383 fb_crtc->desired_mode = mode;
1385 drm_mode_destroy(dev, modeset->mode);
1386 modeset->mode = drm_mode_duplicate(dev,
1387 fb_crtc->desired_mode);
1388 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1393 free(crtcs, DRM_MEM_KMS);
1394 free(modes, DRM_MEM_KMS);
1395 free(enabled, DRM_MEM_KMS);
1399 * drm_helper_initial_config - setup a sane initial connector configuration
1400 * @fb_helper: fb_helper device struct
1401 * @bpp_sel: bpp value to use for the framebuffer configuration
1404 * Called at init time by the driver to set up the @fb_helper initial
1405 * configuration, must take the mode config lock.
1407 * Scans the CRTCs and connectors and tries to put together an initial setup.
1408 * At the moment, this is a cloned configuration across all heads with
1409 * a new framebuffer object as the backing store.
1412 * Zero if everything went ok, nonzero otherwise.
1414 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1416 struct drm_device *dev = fb_helper->dev;
1419 /* disable all the possible outputs/crtcs before entering KMS mode */
1420 drm_helper_disable_unused_functions(fb_helper->dev);
1422 drm_fb_helper_parse_command_line(fb_helper);
1424 count = drm_fb_helper_probe_connector_modes(fb_helper,
1425 dev->mode_config.max_width,
1426 dev->mode_config.max_height);
1428 * we shouldn't end up with no modes here.
1431 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
1433 drm_setup_crtcs(fb_helper);
1435 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1437 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1440 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1441 * probing all the outputs attached to the fb
1442 * @fb_helper: the drm_fb_helper
1445 * Called at runtime, must take mode config lock.
1447 * Scan the connectors attached to the fb_helper and try to put together a
1448 * setup after *notification of a change in output configuration.
1451 * 0 on success and a non-zero error code otherwise.
1453 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1455 struct drm_device *dev = fb_helper->dev;
1457 u32 max_width, max_height, bpp_sel;
1458 int bound = 0, crtcs_bound = 0;
1459 struct drm_crtc *crtc;
1464 sx_xlock(&dev->mode_config.mutex);
1465 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1468 if (crtc->fb == fb_helper->fb)
1472 if (bound < crtcs_bound) {
1473 fb_helper->delayed_hotplug = true;
1474 sx_xunlock(&dev->mode_config.mutex);
1477 DRM_DEBUG_KMS("\n");
1479 max_width = fb_helper->fb->width;
1480 max_height = fb_helper->fb->height;
1481 bpp_sel = fb_helper->fb->bits_per_pixel;
1483 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1485 drm_setup_crtcs(fb_helper);
1486 sx_xunlock(&dev->mode_config.mutex);
1488 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1490 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);