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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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 MODULE_AUTHOR("David Airlie, Jesse Barnes");
40 MODULE_DESCRIPTION("DRM KMS helper");
41 MODULE_LICENSE("GPL and additional rights");
43 static DRM_LIST_HEAD(kernel_fb_helper_list);
46 #include <sys/param.h>
47 #include <sys/systm.h>
50 struct drm_fb_helper *fb_helper;
51 struct task fb_mode_task;
54 /* Call restore out of vt(9) locks. */
56 vt_restore_fbdev_mode(void *arg, int pending)
58 struct drm_fb_helper *fb_helper;
59 struct vt_kms_softc *sc;
61 sc = (struct vt_kms_softc *)arg;
62 fb_helper = sc->fb_helper;
63 sx_xlock(&fb_helper->dev->mode_config.mutex);
64 drm_fb_helper_restore_fbdev_mode(fb_helper);
65 sx_xunlock(&fb_helper->dev->mode_config.mutex);
69 vt_kms_postswitch(void *arg)
71 struct vt_kms_softc *sc;
73 sc = (struct vt_kms_softc *)arg;
75 if (!kdb_active && !KERNEL_PANICKED())
76 taskqueue_enqueue(taskqueue_thread, &sc->fb_mode_task);
78 drm_fb_helper_restore_fbdev_mode(sc->fb_helper);
84 framebuffer_alloc(void)
87 struct vt_kms_softc *sc;
89 info = malloc(sizeof(*info), DRM_MEM_KMS, M_WAITOK | M_ZERO);
91 sc = malloc(sizeof(*sc), DRM_MEM_KMS, M_WAITOK | M_ZERO);
92 TASK_INIT(&sc->fb_mode_task, 0, vt_restore_fbdev_mode, sc);
95 info->enter = &vt_kms_postswitch;
101 framebuffer_release(struct fb_info *info)
104 free(info->fb_priv, DRM_MEM_KMS);
105 free(info, DRM_MEM_KMS);
109 fb_get_options(const char *connector_name, char **option)
114 * A user may use loader tunables to set a specific mode for the
115 * console. Tunables are read in the following order:
116 * 1. kern.vt.fb.modes.$connector_name
117 * 2. kern.vt.fb.default_mode
119 * Example of a mode specific to the LVDS connector:
120 * kern.vt.fb.modes.LVDS="1024x768"
122 * Example of a mode applied to all connectors not having a
123 * connector-specific mode:
124 * kern.vt.fb.default_mode="640x480"
126 snprintf(tunable, sizeof(tunable), "kern.vt.fb.modes.%s",
128 DRM_INFO("Connector %s: get mode from tunables:\n", connector_name);
129 DRM_INFO(" - %s\n", tunable);
130 DRM_INFO(" - kern.vt.fb.default_mode\n");
131 *option = kern_getenv(tunable);
133 *option = kern_getenv("kern.vt.fb.default_mode");
135 return (*option != NULL ? 0 : -ENOENT);
141 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
142 * mode setting driver. They can be used mostly independantely from the crtc
143 * helper functions used by many drivers to implement the kernel mode setting
147 /* simple single crtc case helper function */
148 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
150 struct drm_device *dev = fb_helper->dev;
151 struct drm_connector *connector;
154 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
155 struct drm_fb_helper_connector *fb_helper_connector;
157 fb_helper_connector = malloc(sizeof(struct drm_fb_helper_connector),
158 DRM_MEM_KMS, M_NOWAIT | M_ZERO);
159 if (!fb_helper_connector)
162 fb_helper_connector->connector = connector;
163 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
167 for (i = 0; i < fb_helper->connector_count; i++) {
168 free(fb_helper->connector_info[i], DRM_MEM_KMS);
169 fb_helper->connector_info[i] = NULL;
171 fb_helper->connector_count = 0;
174 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
176 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
178 struct drm_fb_helper_connector *fb_helper_conn;
181 for (i = 0; i < fb_helper->connector_count; i++) {
182 struct drm_cmdline_mode *mode;
183 struct drm_connector *connector;
186 fb_helper_conn = fb_helper->connector_info[i];
187 connector = fb_helper_conn->connector;
188 mode = &fb_helper_conn->cmdline_mode;
190 /* do something on return - turn off connector maybe */
191 if (fb_get_options(drm_get_connector_name(connector), &option))
194 if (drm_mode_parse_command_line_for_connector(option,
199 switch (mode->force) {
203 case DRM_FORCE_ON_DIGITAL:
212 DRM_INFO("forcing %s connector %s\n",
213 drm_get_connector_name(connector), s);
214 connector->force = mode->force;
217 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
218 drm_get_connector_name(connector),
219 mode->xres, mode->yres,
220 mode->refresh_specified ? mode->refresh : 60,
221 mode->rb ? " reduced blanking" : "",
222 mode->margins ? " with margins" : "",
223 mode->interlace ? " interlaced" : "");
231 #if 0 && defined(FREEBSD_NOTYET)
232 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
234 uint16_t *r_base, *g_base, *b_base;
237 r_base = crtc->gamma_store;
238 g_base = r_base + crtc->gamma_size;
239 b_base = g_base + crtc->gamma_size;
241 for (i = 0; i < crtc->gamma_size; i++)
242 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
245 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
247 uint16_t *r_base, *g_base, *b_base;
249 if (crtc->funcs->gamma_set == NULL)
252 r_base = crtc->gamma_store;
253 g_base = r_base + crtc->gamma_size;
254 b_base = g_base + crtc->gamma_size;
256 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
259 int drm_fb_helper_debug_enter(struct fb_info *info)
261 struct drm_fb_helper *helper = info->par;
262 struct drm_crtc_helper_funcs *funcs;
265 if (list_empty(&kernel_fb_helper_list))
268 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
269 for (i = 0; i < helper->crtc_count; i++) {
270 struct drm_mode_set *mode_set =
271 &helper->crtc_info[i].mode_set;
273 if (!mode_set->crtc->enabled)
276 funcs = mode_set->crtc->helper_private;
277 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
278 funcs->mode_set_base_atomic(mode_set->crtc,
282 ENTER_ATOMIC_MODE_SET);
288 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
290 /* Find the real fb for a given fb helper CRTC */
291 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
293 struct drm_device *dev = crtc->dev;
296 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
297 if (crtc->base.id == c->base.id)
304 int drm_fb_helper_debug_leave(struct fb_info *info)
306 struct drm_fb_helper *helper = info->par;
307 struct drm_crtc *crtc;
308 struct drm_crtc_helper_funcs *funcs;
309 struct drm_framebuffer *fb;
312 for (i = 0; i < helper->crtc_count; i++) {
313 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
314 crtc = mode_set->crtc;
315 funcs = crtc->helper_private;
316 fb = drm_mode_config_fb(crtc);
322 DRM_ERROR("no fb to restore??\n");
326 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
327 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
328 crtc->y, LEAVE_ATOMIC_MODE_SET);
333 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
334 #endif /* FREEBSD_NOTYET */
336 bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
341 for (i = 0; i < fb_helper->crtc_count; i++) {
342 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
343 ret = mode_set->crtc->funcs->set_config(mode_set);
349 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode);
351 static bool drm_fb_helper_force_kernel_mode(void)
353 bool ret, error = false;
354 struct drm_fb_helper *helper;
356 if (list_empty(&kernel_fb_helper_list))
359 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
360 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
363 ret = drm_fb_helper_restore_fbdev_mode(helper);
370 #if 0 && defined(FREEBSD_NOTYET)
371 int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
375 * It's a waste of time and effort to switch back to text console
376 * if the kernel should reboot before panic messages can be seen.
378 if (panic_timeout < 0)
381 pr_err("panic occurred, switching back to text console\n");
382 return drm_fb_helper_force_kernel_mode();
384 EXPORT_SYMBOL(drm_fb_helper_panic);
386 static struct notifier_block paniced = {
387 .notifier_call = drm_fb_helper_panic,
389 #endif /* FREEBSD_NOTYET */
392 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
394 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
396 void drm_fb_helper_restore(void)
399 ret = drm_fb_helper_force_kernel_mode();
401 DRM_ERROR("Failed to restore crtc configuration\n");
403 EXPORT_SYMBOL(drm_fb_helper_restore);
406 #ifdef CONFIG_MAGIC_SYSRQ
407 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
409 drm_fb_helper_restore();
411 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
413 static void drm_fb_helper_sysrq(int dummy1)
415 schedule_work(&drm_fb_helper_restore_work);
418 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
419 .handler = drm_fb_helper_sysrq,
420 .help_msg = "force-fb(V)",
421 .action_msg = "Restore framebuffer console",
424 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
428 #if 0 && defined(FREEBSD_NOTYET)
429 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
431 struct drm_fb_helper *fb_helper = info->par;
432 struct drm_device *dev = fb_helper->dev;
433 struct drm_crtc *crtc;
434 struct drm_connector *connector;
438 * For each CRTC in this fb, turn the connectors on/off.
440 sx_xlock(&dev->mode_config.mutex);
441 for (i = 0; i < fb_helper->crtc_count; i++) {
442 crtc = fb_helper->crtc_info[i].mode_set.crtc;
447 /* Walk the connectors & encoders on this fb turning them on/off */
448 for (j = 0; j < fb_helper->connector_count; j++) {
449 connector = fb_helper->connector_info[j]->connector;
450 connector->funcs->dpms(connector, dpms_mode);
451 drm_object_property_set_value(&connector->base,
452 dev->mode_config.dpms_property, dpms_mode);
455 sx_xunlock(&dev->mode_config.mutex);
458 int drm_fb_helper_blank(int blank, struct fb_info *info)
461 /* Display: On; HSync: On, VSync: On */
462 case FB_BLANK_UNBLANK:
463 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
465 /* Display: Off; HSync: On, VSync: On */
466 case FB_BLANK_NORMAL:
467 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
469 /* Display: Off; HSync: Off, VSync: On */
470 case FB_BLANK_HSYNC_SUSPEND:
471 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
473 /* Display: Off; HSync: On, VSync: Off */
474 case FB_BLANK_VSYNC_SUSPEND:
475 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
477 /* Display: Off; HSync: Off, VSync: Off */
478 case FB_BLANK_POWERDOWN:
479 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
484 EXPORT_SYMBOL(drm_fb_helper_blank);
485 #endif /* FREEBSD_NOTYET */
487 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
491 for (i = 0; i < helper->connector_count; i++)
492 free(helper->connector_info[i], DRM_MEM_KMS);
493 free(helper->connector_info, DRM_MEM_KMS);
494 for (i = 0; i < helper->crtc_count; i++) {
495 free(helper->crtc_info[i].mode_set.connectors, DRM_MEM_KMS);
496 if (helper->crtc_info[i].mode_set.mode)
497 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
499 free(helper->crtc_info, DRM_MEM_KMS);
502 int drm_fb_helper_init(struct drm_device *dev,
503 struct drm_fb_helper *fb_helper,
504 int crtc_count, int max_conn_count)
506 struct drm_crtc *crtc;
509 fb_helper->dev = dev;
511 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
513 fb_helper->crtc_info = malloc(crtc_count * sizeof(struct drm_fb_helper_crtc),
514 DRM_MEM_KMS, M_NOWAIT | M_ZERO);
515 if (!fb_helper->crtc_info)
518 fb_helper->crtc_count = crtc_count;
519 fb_helper->connector_info = malloc(dev->mode_config.num_connector * sizeof(struct drm_fb_helper_connector *),
520 DRM_MEM_KMS, M_NOWAIT | M_ZERO);
521 if (!fb_helper->connector_info) {
522 free(fb_helper->crtc_info, DRM_MEM_KMS);
525 fb_helper->connector_count = 0;
527 for (i = 0; i < crtc_count; i++) {
528 fb_helper->crtc_info[i].mode_set.connectors =
529 malloc(max_conn_count *
530 sizeof(struct drm_connector *),
531 DRM_MEM_KMS, M_NOWAIT | M_ZERO);
533 if (!fb_helper->crtc_info[i].mode_set.connectors)
535 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
539 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
540 fb_helper->crtc_info[i].mode_set.crtc = crtc;
546 drm_fb_helper_crtc_free(fb_helper);
549 EXPORT_SYMBOL(drm_fb_helper_init);
551 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
553 if (!list_empty(&fb_helper->kernel_fb_list)) {
554 list_del(&fb_helper->kernel_fb_list);
555 #if 0 && defined(FREEBSD_NOTYET)
556 if (list_empty(&kernel_fb_helper_list)) {
557 pr_info("drm: unregistered panic notifier\n");
558 atomic_notifier_chain_unregister(&panic_notifier_list,
560 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
562 #endif /* FREEBSD_NOTYET */
565 drm_fb_helper_crtc_free(fb_helper);
568 EXPORT_SYMBOL(drm_fb_helper_fini);
570 #if 0 && defined(FREEBSD_NOTYET)
571 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
572 u16 blue, u16 regno, struct fb_info *info)
574 struct drm_fb_helper *fb_helper = info->par;
575 struct drm_framebuffer *fb = fb_helper->fb;
578 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
581 /* place color in pseudopalette */
584 palette = (u32 *)info->pseudo_palette;
585 red >>= (16 - info->var.red.length);
586 green >>= (16 - info->var.green.length);
587 blue >>= (16 - info->var.blue.length);
588 value = (red << info->var.red.offset) |
589 (green << info->var.green.offset) |
590 (blue << info->var.blue.offset);
591 if (info->var.transp.length > 0) {
592 u32 mask = (1 << info->var.transp.length) - 1;
593 mask <<= info->var.transp.offset;
596 palette[regno] = value;
602 if (fb->bits_per_pixel == 16) {
605 if (fb->depth == 16 && regno > 63)
607 if (fb->depth == 15 && regno > 31)
610 if (fb->depth == 16) {
614 for (i = 0; i < 8; i++)
615 fb_helper->funcs->gamma_set(crtc, red,
616 green, blue, pindex + i);
619 fb_helper->funcs->gamma_get(crtc, &r,
623 for (i = 0; i < 4; i++)
624 fb_helper->funcs->gamma_set(crtc, r,
631 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
635 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
637 struct drm_fb_helper *fb_helper = info->par;
638 struct drm_crtc_helper_funcs *crtc_funcs;
639 u16 *red, *green, *blue, *transp;
640 struct drm_crtc *crtc;
644 for (i = 0; i < fb_helper->crtc_count; i++) {
645 crtc = fb_helper->crtc_info[i].mode_set.crtc;
646 crtc_funcs = crtc->helper_private;
651 transp = cmap->transp;
654 for (j = 0; j < cmap->len; j++) {
655 u16 hred, hgreen, hblue, htransp = 0xffff;
664 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
668 crtc_funcs->load_lut(crtc);
672 EXPORT_SYMBOL(drm_fb_helper_setcmap);
674 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
675 struct fb_info *info)
677 struct drm_fb_helper *fb_helper = info->par;
678 struct drm_framebuffer *fb = fb_helper->fb;
681 if (var->pixclock != 0 || in_dbg_master())
684 /* Need to resize the fb object !!! */
685 if (var->bits_per_pixel > fb->bits_per_pixel ||
686 var->xres > fb->width || var->yres > fb->height ||
687 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
688 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
689 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
690 var->xres, var->yres, var->bits_per_pixel,
691 var->xres_virtual, var->yres_virtual,
692 fb->width, fb->height, fb->bits_per_pixel);
696 switch (var->bits_per_pixel) {
698 depth = (var->green.length == 6) ? 16 : 15;
701 depth = (var->transp.length > 0) ? 32 : 24;
704 depth = var->bits_per_pixel;
711 var->green.offset = 0;
712 var->blue.offset = 0;
714 var->green.length = 8;
715 var->blue.length = 8;
716 var->transp.length = 0;
717 var->transp.offset = 0;
720 var->red.offset = 10;
721 var->green.offset = 5;
722 var->blue.offset = 0;
724 var->green.length = 5;
725 var->blue.length = 5;
726 var->transp.length = 1;
727 var->transp.offset = 15;
730 var->red.offset = 11;
731 var->green.offset = 5;
732 var->blue.offset = 0;
734 var->green.length = 6;
735 var->blue.length = 5;
736 var->transp.length = 0;
737 var->transp.offset = 0;
740 var->red.offset = 16;
741 var->green.offset = 8;
742 var->blue.offset = 0;
744 var->green.length = 8;
745 var->blue.length = 8;
746 var->transp.length = 0;
747 var->transp.offset = 0;
750 var->red.offset = 16;
751 var->green.offset = 8;
752 var->blue.offset = 0;
754 var->green.length = 8;
755 var->blue.length = 8;
756 var->transp.length = 8;
757 var->transp.offset = 24;
764 EXPORT_SYMBOL(drm_fb_helper_check_var);
766 /* this will let fbcon do the mode init */
767 int drm_fb_helper_set_par(struct fb_info *info)
769 struct drm_fb_helper *fb_helper = info->par;
770 struct drm_device *dev = fb_helper->dev;
771 struct fb_var_screeninfo *var = &info->var;
772 struct drm_crtc *crtc;
776 if (var->pixclock != 0) {
777 DRM_ERROR("PIXEL CLOCK SET\n");
781 sx_xlock(&dev->mode_config.mutex);
782 for (i = 0; i < fb_helper->crtc_count; i++) {
783 crtc = fb_helper->crtc_info[i].mode_set.crtc;
784 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
786 sx_xunlock(&dev->mode_config.mutex);
790 sx_xunlock(&dev->mode_config.mutex);
792 if (fb_helper->delayed_hotplug) {
793 fb_helper->delayed_hotplug = false;
794 drm_fb_helper_hotplug_event(fb_helper);
798 EXPORT_SYMBOL(drm_fb_helper_set_par);
800 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
801 struct fb_info *info)
803 struct drm_fb_helper *fb_helper = info->par;
804 struct drm_device *dev = fb_helper->dev;
805 struct drm_mode_set *modeset;
806 struct drm_crtc *crtc;
810 sx_xlock(&dev->mode_config.mutex);
811 for (i = 0; i < fb_helper->crtc_count; i++) {
812 crtc = fb_helper->crtc_info[i].mode_set.crtc;
814 modeset = &fb_helper->crtc_info[i].mode_set;
816 modeset->x = var->xoffset;
817 modeset->y = var->yoffset;
819 if (modeset->num_connectors) {
820 ret = crtc->funcs->set_config(modeset);
822 info->var.xoffset = var->xoffset;
823 info->var.yoffset = var->yoffset;
827 sx_xunlock(&dev->mode_config.mutex);
830 EXPORT_SYMBOL(drm_fb_helper_pan_display);
831 #endif /* FREEBSD_NOTYET */
833 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
839 struct fb_info *info;
840 struct drm_fb_helper_surface_size sizes;
842 #if defined(__FreeBSD__)
843 struct drm_crtc *crtc;
844 struct drm_device *dev;
849 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
850 sizes.surface_depth = 24;
851 sizes.surface_bpp = 32;
852 sizes.fb_width = (unsigned)-1;
853 sizes.fb_height = (unsigned)-1;
855 /* if driver picks 8 or 16 by default use that
856 for both depth/bpp */
857 if (preferred_bpp != sizes.surface_bpp)
858 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
860 /* first up get a count of crtcs now in use and new min/maxes width/heights */
861 for (i = 0; i < fb_helper->connector_count; i++) {
862 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
863 struct drm_cmdline_mode *cmdline_mode;
865 cmdline_mode = &fb_helper_conn->cmdline_mode;
867 if (cmdline_mode->bpp_specified) {
868 switch (cmdline_mode->bpp) {
870 sizes.surface_depth = sizes.surface_bpp = 8;
873 sizes.surface_depth = 15;
874 sizes.surface_bpp = 16;
877 sizes.surface_depth = sizes.surface_bpp = 16;
880 sizes.surface_depth = sizes.surface_bpp = 24;
883 sizes.surface_depth = 24;
884 sizes.surface_bpp = 32;
892 for (i = 0; i < fb_helper->crtc_count; i++) {
893 struct drm_display_mode *desired_mode;
894 desired_mode = fb_helper->crtc_info[i].desired_mode;
898 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
899 if (desired_mode->hdisplay < sizes.fb_width)
900 sizes.fb_width = desired_mode->hdisplay;
901 if (desired_mode->vdisplay < sizes.fb_height)
902 sizes.fb_height = desired_mode->vdisplay;
903 if (desired_mode->hdisplay > sizes.surface_width)
904 sizes.surface_width = desired_mode->hdisplay;
905 if (desired_mode->vdisplay > sizes.surface_height)
906 sizes.surface_height = desired_mode->vdisplay;
911 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
912 /* hmm everyone went away - assume VGA cable just fell out
913 and will come back later. */
914 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
915 sizes.fb_width = sizes.surface_width = 1024;
916 sizes.fb_height = sizes.surface_height = 768;
919 /* push down into drivers */
920 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
924 info = fb_helper->fbdev;
926 kdev = fb_helper->dev->dev;
927 info->fb_video_dev = device_get_parent(kdev);
929 /* set the fb pointer */
930 for (i = 0; i < fb_helper->crtc_count; i++)
931 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
933 #if defined(__FreeBSD__)
937 info->fb_fbd_dev = device_add_child(kdev, "fbd",
938 device_get_unit(kdev));
939 if (info->fb_fbd_dev != NULL)
940 ret = device_probe_and_attach(info->fb_fbd_dev);
945 DRM_ERROR("Failed to attach fbd device: %d\n", ret);
948 /* Modified version of drm_fb_helper_set_par() */
949 dev = fb_helper->dev;
950 sx_xlock(&dev->mode_config.mutex);
951 for (i = 0; i < fb_helper->crtc_count; i++) {
952 crtc = fb_helper->crtc_info[i].mode_set.crtc;
953 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
955 sx_xunlock(&dev->mode_config.mutex);
959 sx_xunlock(&dev->mode_config.mutex);
961 if (fb_helper->delayed_hotplug) {
962 fb_helper->delayed_hotplug = false;
963 drm_fb_helper_hotplug_event(fb_helper);
968 info->var.pixclock = 0;
969 if (register_framebuffer(info) < 0)
972 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
973 info->node, info->fix.id);
976 drm_fb_helper_set_par(info);
980 #if 0 && defined(FREEBSD_NOTYET)
981 /* Switch back to kernel console on panic */
982 /* multi card linked list maybe */
983 if (list_empty(&kernel_fb_helper_list)) {
984 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
985 atomic_notifier_chain_register(&panic_notifier_list,
987 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
989 #endif /* FREEBSD_NOTYET */
991 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
995 EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
997 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1000 info->fb_stride = pitch;
1004 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1006 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1007 uint32_t fb_width, uint32_t fb_height)
1009 struct drm_framebuffer *fb = fb_helper->fb;
1010 struct vt_kms_softc *sc;
1012 info->fb_name = device_get_nameunit(fb_helper->dev->dev);
1013 info->fb_width = fb->width;
1014 info->fb_height = fb->height;
1015 info->fb_depth = fb->bits_per_pixel;
1017 sc = (struct vt_kms_softc *)info->fb_priv;
1018 sc->fb_helper = fb_helper;
1020 EXPORT_SYMBOL(drm_fb_helper_fill_var);
1022 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1026 struct drm_connector *connector;
1030 for (i = 0; i < fb_helper->connector_count; i++) {
1031 connector = fb_helper->connector_info[i]->connector;
1032 count += connector->funcs->fill_modes(connector, maxX, maxY);
1038 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1040 struct drm_display_mode *mode;
1042 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1043 if (drm_mode_width(mode) > width ||
1044 drm_mode_height(mode) > height)
1046 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1052 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1054 struct drm_cmdline_mode *cmdline_mode;
1055 cmdline_mode = &fb_connector->cmdline_mode;
1056 return cmdline_mode->specified;
1059 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1060 int width, int height)
1062 struct drm_cmdline_mode *cmdline_mode;
1063 struct drm_display_mode *mode = NULL;
1065 cmdline_mode = &fb_helper_conn->cmdline_mode;
1066 if (cmdline_mode->specified == false)
1069 /* attempt to find a matching mode in the list of modes
1070 * we have gotten so far, if not add a CVT mode that conforms
1072 if (cmdline_mode->rb || cmdline_mode->margins)
1075 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1076 /* check width/height */
1077 if (mode->hdisplay != cmdline_mode->xres ||
1078 mode->vdisplay != cmdline_mode->yres)
1081 if (cmdline_mode->refresh_specified) {
1082 if (mode->vrefresh != cmdline_mode->refresh)
1086 if (cmdline_mode->interlace) {
1087 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1094 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1096 list_add(&mode->head, &fb_helper_conn->connector->modes);
1100 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1105 enable = connector->status == connector_status_connected;
1107 enable = connector->status != connector_status_disconnected;
1112 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1115 bool any_enabled = false;
1116 struct drm_connector *connector;
1119 for (i = 0; i < fb_helper->connector_count; i++) {
1120 connector = fb_helper->connector_info[i]->connector;
1121 enabled[i] = drm_connector_enabled(connector, true);
1122 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1123 enabled[i] ? "yes" : "no");
1124 any_enabled |= enabled[i];
1130 for (i = 0; i < fb_helper->connector_count; i++) {
1131 connector = fb_helper->connector_info[i]->connector;
1132 enabled[i] = drm_connector_enabled(connector, false);
1136 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1137 struct drm_display_mode **modes,
1138 bool *enabled, int width, int height)
1141 bool can_clone = false;
1142 struct drm_fb_helper_connector *fb_helper_conn;
1143 struct drm_display_mode *dmt_mode, *mode;
1145 /* only contemplate cloning in the single crtc case */
1146 if (fb_helper->crtc_count > 1)
1150 for (i = 0; i < fb_helper->connector_count; i++) {
1155 /* only contemplate cloning if more than one connector is enabled */
1159 /* check the command line or if nothing common pick 1024x768 */
1161 for (i = 0; i < fb_helper->connector_count; i++) {
1164 fb_helper_conn = fb_helper->connector_info[i];
1165 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1170 for (j = 0; j < i; j++) {
1173 if (!drm_mode_equal(modes[j], modes[i]))
1179 DRM_DEBUG_KMS("can clone using command line\n");
1183 /* try and find a 1024x768 mode on each connector */
1185 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1187 for (i = 0; i < fb_helper->connector_count; i++) {
1192 fb_helper_conn = fb_helper->connector_info[i];
1193 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1194 if (drm_mode_equal(mode, dmt_mode))
1202 DRM_DEBUG_KMS("can clone using 1024x768\n");
1205 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1209 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1210 struct drm_display_mode **modes,
1211 bool *enabled, int width, int height)
1213 struct drm_fb_helper_connector *fb_helper_conn;
1216 for (i = 0; i < fb_helper->connector_count; i++) {
1217 fb_helper_conn = fb_helper->connector_info[i];
1219 if (enabled[i] == false)
1222 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1223 fb_helper_conn->connector->base.id);
1225 /* got for command line mode first */
1226 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1228 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1229 fb_helper_conn->connector->base.id);
1230 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1232 /* No preferred modes, pick one off the list */
1233 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1234 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1237 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1243 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1244 struct drm_fb_helper_crtc **best_crtcs,
1245 struct drm_display_mode **modes,
1246 int n, int width, int height)
1249 struct drm_device *dev = fb_helper->dev;
1250 struct drm_connector *connector;
1251 struct drm_connector_helper_funcs *connector_funcs;
1252 struct drm_encoder *encoder;
1253 int my_score, best_score, score;
1254 struct drm_fb_helper_crtc **crtcs, *crtc;
1255 struct drm_fb_helper_connector *fb_helper_conn;
1257 if (n == fb_helper->connector_count)
1260 fb_helper_conn = fb_helper->connector_info[n];
1261 connector = fb_helper_conn->connector;
1263 best_crtcs[n] = NULL;
1264 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1265 if (modes[n] == NULL)
1268 crtcs = malloc(dev->mode_config.num_connector *
1269 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS, M_NOWAIT | M_ZERO);
1274 if (connector->status == connector_status_connected)
1276 if (drm_has_cmdline_mode(fb_helper_conn))
1278 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1281 connector_funcs = connector->helper_private;
1282 encoder = connector_funcs->best_encoder(connector);
1286 /* select a crtc for this connector and then attempt to configure
1287 remaining connectors */
1288 for (c = 0; c < fb_helper->crtc_count; c++) {
1289 crtc = &fb_helper->crtc_info[c];
1291 if ((encoder->possible_crtcs & (1 << c)) == 0)
1294 for (o = 0; o < n; o++)
1295 if (best_crtcs[o] == crtc)
1299 /* ignore cloning unless only a single crtc */
1300 if (fb_helper->crtc_count > 1)
1303 if (!drm_mode_equal(modes[o], modes[n]))
1308 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1309 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1311 if (score > best_score) {
1313 memcpy(best_crtcs, crtcs,
1314 dev->mode_config.num_connector *
1315 sizeof(struct drm_fb_helper_crtc *));
1319 free(crtcs, DRM_MEM_KMS);
1323 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1325 struct drm_device *dev = fb_helper->dev;
1326 struct drm_fb_helper_crtc **crtcs;
1327 struct drm_display_mode **modes;
1328 struct drm_mode_set *modeset;
1333 DRM_DEBUG_KMS("\n");
1335 width = dev->mode_config.max_width;
1336 height = dev->mode_config.max_height;
1338 crtcs = malloc(dev->mode_config.num_connector *
1339 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS, M_NOWAIT | M_ZERO);
1340 modes = malloc(dev->mode_config.num_connector *
1341 sizeof(struct drm_display_mode *), DRM_MEM_KMS, M_NOWAIT | M_ZERO);
1342 enabled = malloc(dev->mode_config.num_connector *
1343 sizeof(bool), DRM_MEM_KMS, M_NOWAIT | M_ZERO);
1344 if (!crtcs || !modes || !enabled) {
1345 DRM_ERROR("Memory allocation failed\n");
1350 drm_enable_connectors(fb_helper, enabled);
1352 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1354 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1356 DRM_ERROR("Unable to find initial modes\n");
1359 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1361 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1363 /* need to set the modesets up here for use later */
1364 /* fill out the connector<->crtc mappings into the modesets */
1365 for (i = 0; i < fb_helper->crtc_count; i++) {
1366 modeset = &fb_helper->crtc_info[i].mode_set;
1367 modeset->num_connectors = 0;
1370 for (i = 0; i < fb_helper->connector_count; i++) {
1371 struct drm_display_mode *mode = modes[i];
1372 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1373 modeset = &fb_crtc->mode_set;
1375 if (mode && fb_crtc) {
1376 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1377 mode->name, fb_crtc->mode_set.crtc->base.id);
1378 fb_crtc->desired_mode = mode;
1380 drm_mode_destroy(dev, modeset->mode);
1381 modeset->mode = drm_mode_duplicate(dev,
1382 fb_crtc->desired_mode);
1383 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1388 free(crtcs, DRM_MEM_KMS);
1389 free(modes, DRM_MEM_KMS);
1390 free(enabled, DRM_MEM_KMS);
1394 * drm_helper_initial_config - setup a sane initial connector configuration
1395 * @fb_helper: fb_helper device struct
1396 * @bpp_sel: bpp value to use for the framebuffer configuration
1399 * Called at init time by the driver to set up the @fb_helper initial
1400 * configuration, must take the mode config lock.
1402 * Scans the CRTCs and connectors and tries to put together an initial setup.
1403 * At the moment, this is a cloned configuration across all heads with
1404 * a new framebuffer object as the backing store.
1407 * Zero if everything went ok, nonzero otherwise.
1409 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1411 struct drm_device *dev = fb_helper->dev;
1414 /* disable all the possible outputs/crtcs before entering KMS mode */
1415 drm_helper_disable_unused_functions(fb_helper->dev);
1417 drm_fb_helper_parse_command_line(fb_helper);
1419 count = drm_fb_helper_probe_connector_modes(fb_helper,
1420 dev->mode_config.max_width,
1421 dev->mode_config.max_height);
1423 * we shouldn't end up with no modes here.
1426 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
1428 drm_setup_crtcs(fb_helper);
1430 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1432 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1435 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1436 * probing all the outputs attached to the fb
1437 * @fb_helper: the drm_fb_helper
1440 * Called at runtime, must take mode config lock.
1442 * Scan the connectors attached to the fb_helper and try to put together a
1443 * setup after *notification of a change in output configuration.
1446 * 0 on success and a non-zero error code otherwise.
1448 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1450 struct drm_device *dev = fb_helper->dev;
1451 u32 max_width, max_height, bpp_sel;
1452 int bound = 0, crtcs_bound = 0;
1453 struct drm_crtc *crtc;
1458 sx_xlock(&dev->mode_config.mutex);
1459 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1462 if (crtc->fb == fb_helper->fb)
1466 if (bound < crtcs_bound) {
1467 fb_helper->delayed_hotplug = true;
1468 sx_xunlock(&dev->mode_config.mutex);
1471 DRM_DEBUG_KMS("\n");
1473 max_width = fb_helper->fb->width;
1474 max_height = fb_helper->fb->height;
1475 bpp_sel = fb_helper->fb->bits_per_pixel;
1477 drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1479 drm_setup_crtcs(fb_helper);
1480 sx_xunlock(&dev->mode_config.mutex);
1482 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1484 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);