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 static DRM_LIST_HEAD(kernel_fb_helper_list);
41 /* simple single crtc case helper function */
42 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
44 struct drm_device *dev = fb_helper->dev;
45 struct drm_connector *connector;
47 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
48 struct drm_fb_helper_connector *fb_helper_connector;
50 fb_helper_connector = malloc(
51 sizeof(struct drm_fb_helper_connector), DRM_MEM_KMS,
54 fb_helper_connector->connector = connector;
55 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
60 const char *fb_mode_option;
63 * drm_fb_helper_connector_parse_command_line - parse command line for connector
64 * @connector - connector to parse line for
65 * @mode_option - per connector mode option
67 * This parses the connector specific then generic command lines for
68 * modes and options to configure the connector.
70 * This uses the same parameters as the fb modedb.c, except for extra
71 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
73 * enable/enable Digital/disable bit at the end
75 static bool drm_fb_helper_connector_parse_command_line(struct drm_fb_helper_connector *fb_helper_conn,
76 const char *mode_option)
80 int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
81 unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
82 int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
84 enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
85 struct drm_fb_helper_cmdline_mode *cmdline_mode;
86 struct drm_connector *connector;
90 connector = fb_helper_conn->connector;
92 cmdline_mode = &fb_helper_conn->cmdline_mode;
94 mode_option = fb_mode_option;
97 cmdline_mode->specified = false;
102 namelen = strlen(name);
103 for (i = namelen-1; i >= 0; i--) {
107 if (!refresh_specified && !bpp_specified &&
109 refresh = strtol(&name[i+1], NULL, 10);
110 refresh_specified = 1;
118 if (!bpp_specified && !yres_specified) {
119 bpp = strtol(&name[i+1], NULL, 10);
127 if (!yres_specified) {
128 yres = strtol(&name[i+1], NULL, 10);
151 force = DRM_FORCE_ON;
154 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
155 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
156 force = DRM_FORCE_ON;
158 force = DRM_FORCE_ON_DIGITAL;
161 force = DRM_FORCE_OFF;
167 if (i < 0 && yres_specified) {
168 xres = strtol(name, NULL, 10);
173 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
174 drm_get_connector_name(connector), xres, yres,
175 (refresh) ? refresh : 60, (rb) ? " reduced blanking" :
176 "", (margins) ? " with margins" : "", (interlace) ?
182 case DRM_FORCE_OFF: s = "OFF"; break;
183 case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
185 case DRM_FORCE_ON: s = "ON"; break;
188 DRM_INFO("forcing %s connector %s\n",
189 drm_get_connector_name(connector), s);
190 connector->force = force;
194 cmdline_mode->specified = true;
195 cmdline_mode->xres = xres;
196 cmdline_mode->yres = yres;
199 if (refresh_specified) {
200 cmdline_mode->refresh_specified = true;
201 cmdline_mode->refresh = refresh;
205 cmdline_mode->bpp_specified = true;
206 cmdline_mode->bpp = bpp;
208 cmdline_mode->rb = rb ? true : false;
209 cmdline_mode->cvt = cvt ? true : false;
210 cmdline_mode->interlace = interlace ? true : false;
216 fb_get_options(const char *connector_name, char **option)
222 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
224 struct drm_fb_helper_connector *fb_helper_conn;
227 for (i = 0; i < fb_helper->connector_count; i++) {
230 fb_helper_conn = fb_helper->connector_info[i];
232 /* do something on return - turn off connector maybe */
233 if (fb_get_options(drm_get_connector_name(fb_helper_conn->connector), &option))
236 drm_fb_helper_connector_parse_command_line(fb_helper_conn, option);
242 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
244 uint16_t *r_base, *g_base, *b_base;
247 r_base = crtc->gamma_store;
248 g_base = r_base + crtc->gamma_size;
249 b_base = g_base + crtc->gamma_size;
251 for (i = 0; i < crtc->gamma_size; i++)
252 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
255 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
257 uint16_t *r_base, *g_base, *b_base;
259 r_base = crtc->gamma_store;
260 g_base = r_base + crtc->gamma_size;
261 b_base = g_base + crtc->gamma_size;
263 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
268 int drm_fb_helper_debug_enter(struct fb_info *info)
270 struct drm_fb_helper *helper = info->par;
271 struct drm_crtc_helper_funcs *funcs;
274 if (list_empty(&kernel_fb_helper_list))
277 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
278 for (i = 0; i < helper->crtc_count; i++) {
279 struct drm_mode_set *mode_set =
280 &helper->crtc_info[i].mode_set;
282 if (!mode_set->crtc->enabled)
285 funcs = mode_set->crtc->helper_private;
286 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
287 funcs->mode_set_base_atomic(mode_set->crtc,
291 ENTER_ATOMIC_MODE_SET);
300 /* Find the real fb for a given fb helper CRTC */
301 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
303 struct drm_device *dev = crtc->dev;
306 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
307 if (crtc->base.id == c->base.id)
316 int drm_fb_helper_debug_leave(struct fb_info *info)
318 struct drm_fb_helper *helper = info->par;
319 struct drm_crtc *crtc;
320 struct drm_crtc_helper_funcs *funcs;
321 struct drm_framebuffer *fb;
324 for (i = 0; i < helper->crtc_count; i++) {
325 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
326 crtc = mode_set->crtc;
327 funcs = crtc->helper_private;
328 fb = drm_mode_config_fb(crtc);
334 DRM_ERROR("no fb to restore??\n");
338 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
339 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
340 crtc->y, LEAVE_ATOMIC_MODE_SET);
347 bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
351 for (i = 0; i < fb_helper->crtc_count; i++) {
352 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
353 ret = drm_crtc_helper_set_config(mode_set);
361 bool drm_fb_helper_force_kernel_mode(void)
363 bool ret, error = false;
364 struct drm_fb_helper *helper;
366 if (list_empty(&kernel_fb_helper_list))
369 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
370 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
373 ret = drm_fb_helper_restore_fbdev_mode(helper);
382 int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
385 printf("panic occurred, switching back to text console\n");
386 return drm_fb_helper_force_kernel_mode();
390 static struct notifier_block paniced = {
391 .notifier_call = drm_fb_helper_panic,
395 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
397 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
399 void drm_fb_helper_restore(void)
402 ret = drm_fb_helper_force_kernel_mode();
404 DRM_ERROR("Failed to restore crtc configuration\n");
407 #ifdef CONFIG_MAGIC_SYSRQ
408 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
410 drm_fb_helper_restore();
412 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
414 static void drm_fb_helper_sysrq(int dummy1)
416 schedule_work(&drm_fb_helper_restore_work);
419 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
420 .handler = drm_fb_helper_sysrq,
421 .help_msg = "force-fb(V)",
422 .action_msg = "Restore framebuffer console",
425 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
430 static void drm_fb_helper_on(struct fb_info *info)
432 struct drm_fb_helper *fb_helper = info->par;
433 struct drm_device *dev = fb_helper->dev;
434 struct drm_crtc *crtc;
435 struct drm_crtc_helper_funcs *crtc_funcs;
436 struct drm_connector *connector;
437 struct drm_encoder *encoder;
441 * For each CRTC in this fb, turn the crtc on then,
442 * find all associated encoders and turn them on.
444 sx_xlock(&dev->mode_config.mutex);
445 for (i = 0; i < fb_helper->crtc_count; i++) {
446 crtc = fb_helper->crtc_info[i].mode_set.crtc;
447 crtc_funcs = crtc->helper_private;
452 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
454 /* Walk the connectors & encoders on this fb turning them on */
455 for (j = 0; j < fb_helper->connector_count; j++) {
456 connector = fb_helper->connector_info[j]->connector;
457 connector->dpms = DRM_MODE_DPMS_ON;
458 drm_connector_property_set_value(connector,
459 dev->mode_config.dpms_property,
462 /* Found a CRTC on this fb, now find encoders */
463 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
464 if (encoder->crtc == crtc) {
465 struct drm_encoder_helper_funcs *encoder_funcs;
467 encoder_funcs = encoder->helper_private;
468 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
472 sx_xunlock(&dev->mode_config.mutex);
477 static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
479 struct drm_fb_helper *fb_helper = info->par;
480 struct drm_device *dev = fb_helper->dev;
481 struct drm_crtc *crtc;
482 struct drm_crtc_helper_funcs *crtc_funcs;
483 struct drm_connector *connector;
484 struct drm_encoder *encoder;
488 * For each CRTC in this fb, find all associated encoders
489 * and turn them off, then turn off the CRTC.
491 sx_xlock(&dev->mode_config.mutex);
492 for (i = 0; i < fb_helper->crtc_count; i++) {
493 crtc = fb_helper->crtc_info[i].mode_set.crtc;
494 crtc_funcs = crtc->helper_private;
499 /* Walk the connectors on this fb and mark them off */
500 for (j = 0; j < fb_helper->connector_count; j++) {
501 connector = fb_helper->connector_info[j]->connector;
502 connector->dpms = dpms_mode;
503 drm_connector_property_set_value(connector,
504 dev->mode_config.dpms_property,
507 /* Found a CRTC on this fb, now find encoders */
508 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
509 if (encoder->crtc == crtc) {
510 struct drm_encoder_helper_funcs *encoder_funcs;
512 encoder_funcs = encoder->helper_private;
513 encoder_funcs->dpms(encoder, dpms_mode);
516 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
518 sx_xunlock(&dev->mode_config.mutex);
523 int drm_fb_helper_blank(int blank, struct fb_info *info)
526 /* Display: On; HSync: On, VSync: On */
527 case FB_BLANK_UNBLANK:
528 drm_fb_helper_on(info);
530 /* Display: Off; HSync: On, VSync: On */
531 case FB_BLANK_NORMAL:
532 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
534 /* Display: Off; HSync: Off, VSync: On */
535 case FB_BLANK_HSYNC_SUSPEND:
536 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
538 /* Display: Off; HSync: On, VSync: Off */
539 case FB_BLANK_VSYNC_SUSPEND:
540 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
542 /* Display: Off; HSync: Off, VSync: Off */
543 case FB_BLANK_POWERDOWN:
544 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
551 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
555 for (i = 0; i < helper->connector_count; i++)
556 free(helper->connector_info[i], DRM_MEM_KMS);
557 free(helper->connector_info, DRM_MEM_KMS);
558 for (i = 0; i < helper->crtc_count; i++) {
559 free(helper->crtc_info[i].mode_set.connectors, DRM_MEM_KMS);
560 if (helper->crtc_info[i].mode_set.mode)
561 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
563 free(helper->crtc_info, DRM_MEM_KMS);
566 int drm_fb_helper_init(struct drm_device *dev,
567 struct drm_fb_helper *fb_helper,
568 int crtc_count, int max_conn_count)
570 struct drm_crtc *crtc;
573 fb_helper->dev = dev;
575 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
577 fb_helper->crtc_info = malloc(crtc_count *
578 sizeof(struct drm_fb_helper_crtc), DRM_MEM_KMS, M_WAITOK | M_ZERO);
580 fb_helper->crtc_count = crtc_count;
581 fb_helper->connector_info = malloc(dev->mode_config.num_connector *
582 sizeof(struct drm_fb_helper_connector *), DRM_MEM_KMS,
584 fb_helper->connector_count = 0;
586 for (i = 0; i < crtc_count; i++) {
587 fb_helper->crtc_info[i].mode_set.connectors =
588 malloc(max_conn_count * sizeof(struct drm_connector *),
589 DRM_MEM_KMS, M_WAITOK | M_ZERO);
591 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
595 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
596 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
597 fb_helper->crtc_info[i].mode_set.crtc = crtc;
600 fb_helper->conn_limit = max_conn_count;
604 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
606 if (!list_empty(&fb_helper->kernel_fb_list)) {
607 list_del(&fb_helper->kernel_fb_list);
608 if (list_empty(&kernel_fb_helper_list)) {
610 printk(KERN_INFO "drm: unregistered panic notifier\n");
611 atomic_notifier_chain_unregister(&panic_notifier_list,
613 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
618 drm_fb_helper_crtc_free(fb_helper);
623 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
624 u16 blue, u16 regno, struct fb_info *info)
626 struct drm_fb_helper *fb_helper = info->par;
627 struct drm_framebuffer *fb = fb_helper->fb;
630 if (info->fix.visual == FB_VISUAL_trueCOLOR) {
633 /* place color in psuedopalette */
636 palette = (u32 *)info->pseudo_palette;
637 red >>= (16 - info->var.red.length);
638 green >>= (16 - info->var.green.length);
639 blue >>= (16 - info->var.blue.length);
640 value = (red << info->var.red.offset) |
641 (green << info->var.green.offset) |
642 (blue << info->var.blue.offset);
643 if (info->var.transp.length > 0) {
644 u32 mask = (1 << info->var.transp.length) - 1;
645 mask <<= info->var.transp.offset;
648 palette[regno] = value;
654 if (fb->bits_per_pixel == 16) {
657 if (fb->depth == 16 && regno > 63)
659 if (fb->depth == 15 && regno > 31)
662 if (fb->depth == 16) {
666 for (i = 0; i < 8; i++)
667 fb_helper->funcs->gamma_set(crtc, red,
668 green, blue, pindex + i);
671 fb_helper->funcs->gamma_get(crtc, &r,
675 for (i = 0; i < 4; i++)
676 fb_helper->funcs->gamma_set(crtc, r,
683 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
689 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
691 struct drm_fb_helper *fb_helper = info->par;
692 struct drm_crtc_helper_funcs *crtc_funcs;
693 u16 *red, *green, *blue, *transp;
694 struct drm_crtc *crtc;
698 for (i = 0; i < fb_helper->crtc_count; i++) {
699 crtc = fb_helper->crtc_info[i].mode_set.crtc;
700 crtc_funcs = crtc->helper_private;
705 transp = cmap->transp;
708 for (j = 0; j < cmap->len; j++) {
709 u16 hred, hgreen, hblue, htransp = 0xffff;
718 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
722 crtc_funcs->load_lut(crtc);
729 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
730 struct fb_info *info)
732 struct drm_fb_helper *fb_helper = info->par;
733 struct drm_framebuffer *fb = fb_helper->fb;
736 if (var->pixclock != 0 || in_dbg_master())
739 /* Need to resize the fb object !!! */
740 if (var->bits_per_pixel > fb->bits_per_pixel ||
741 var->xres > fb->width || var->yres > fb->height ||
742 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
743 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
744 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
745 var->xres, var->yres, var->bits_per_pixel,
746 var->xres_virtual, var->yres_virtual,
747 fb->width, fb->height, fb->bits_per_pixel);
751 switch (var->bits_per_pixel) {
753 depth = (var->green.length == 6) ? 16 : 15;
756 depth = (var->transp.length > 0) ? 32 : 24;
759 depth = var->bits_per_pixel;
766 var->green.offset = 0;
767 var->blue.offset = 0;
769 var->green.length = 8;
770 var->blue.length = 8;
771 var->transp.length = 0;
772 var->transp.offset = 0;
775 var->red.offset = 10;
776 var->green.offset = 5;
777 var->blue.offset = 0;
779 var->green.length = 5;
780 var->blue.length = 5;
781 var->transp.length = 1;
782 var->transp.offset = 15;
785 var->red.offset = 11;
786 var->green.offset = 5;
787 var->blue.offset = 0;
789 var->green.length = 6;
790 var->blue.length = 5;
791 var->transp.length = 0;
792 var->transp.offset = 0;
795 var->red.offset = 16;
796 var->green.offset = 8;
797 var->blue.offset = 0;
799 var->green.length = 8;
800 var->blue.length = 8;
801 var->transp.length = 0;
802 var->transp.offset = 0;
805 var->red.offset = 16;
806 var->green.offset = 8;
807 var->blue.offset = 0;
809 var->green.length = 8;
810 var->blue.length = 8;
811 var->transp.length = 8;
812 var->transp.offset = 24;
822 /* this will let fbcon do the mode init */
823 int drm_fb_helper_set_par(struct fb_info *info)
825 struct drm_fb_helper *fb_helper = info->par;
826 struct drm_device *dev = fb_helper->dev;
827 struct fb_var_screeninfo *var = &info->var;
828 struct drm_crtc *crtc;
832 if (var->pixclock != 0) {
833 DRM_ERROR("PIXEL CLOCK SET\n");
837 mutex_lock(&dev->mode_config.mutex);
838 for (i = 0; i < fb_helper->crtc_count; i++) {
839 crtc = fb_helper->crtc_info[i].mode_set.crtc;
840 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
842 mutex_unlock(&dev->mode_config.mutex);
846 mutex_unlock(&dev->mode_config.mutex);
848 if (fb_helper->delayed_hotplug) {
849 fb_helper->delayed_hotplug = false;
850 drm_fb_helper_hotplug_event(fb_helper);
857 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
858 struct fb_info *info)
860 struct drm_fb_helper *fb_helper = info->par;
861 struct drm_device *dev = fb_helper->dev;
862 struct drm_mode_set *modeset;
863 struct drm_crtc *crtc;
867 mutex_lock(&dev->mode_config.mutex);
868 for (i = 0; i < fb_helper->crtc_count; i++) {
869 crtc = fb_helper->crtc_info[i].mode_set.crtc;
871 modeset = &fb_helper->crtc_info[i].mode_set;
873 modeset->x = var->xoffset;
874 modeset->y = var->yoffset;
876 if (modeset->num_connectors) {
877 ret = crtc->funcs->set_config(modeset);
879 info->var.xoffset = var->xoffset;
880 info->var.yoffset = var->yoffset;
884 mutex_unlock(&dev->mode_config.mutex);
889 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
896 struct fb_info *info;
898 struct drm_fb_helper_surface_size sizes;
901 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
902 sizes.surface_depth = 24;
903 sizes.surface_bpp = 32;
904 sizes.fb_width = (unsigned)-1;
905 sizes.fb_height = (unsigned)-1;
907 /* if driver picks 8 or 16 by default use that
908 for both depth/bpp */
909 if (preferred_bpp != sizes.surface_bpp) {
910 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
912 /* first up get a count of crtcs now in use and new min/maxes width/heights */
913 for (i = 0; i < fb_helper->connector_count; i++) {
914 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
915 struct drm_fb_helper_cmdline_mode *cmdline_mode;
917 cmdline_mode = &fb_helper_conn->cmdline_mode;
919 if (cmdline_mode->bpp_specified) {
920 switch (cmdline_mode->bpp) {
922 sizes.surface_depth = sizes.surface_bpp = 8;
925 sizes.surface_depth = 15;
926 sizes.surface_bpp = 16;
929 sizes.surface_depth = sizes.surface_bpp = 16;
932 sizes.surface_depth = sizes.surface_bpp = 24;
935 sizes.surface_depth = 24;
936 sizes.surface_bpp = 32;
944 for (i = 0; i < fb_helper->crtc_count; i++) {
945 struct drm_display_mode *desired_mode;
946 desired_mode = fb_helper->crtc_info[i].desired_mode;
950 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
951 if (desired_mode->hdisplay < sizes.fb_width)
952 sizes.fb_width = desired_mode->hdisplay;
953 if (desired_mode->vdisplay < sizes.fb_height)
954 sizes.fb_height = desired_mode->vdisplay;
955 if (desired_mode->hdisplay > sizes.surface_width)
956 sizes.surface_width = desired_mode->hdisplay;
957 if (desired_mode->vdisplay > sizes.surface_height)
958 sizes.surface_height = desired_mode->vdisplay;
963 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
964 /* hmm everyone went away - assume VGA cable just fell out
965 and will come back later. */
966 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
967 sizes.fb_width = sizes.surface_width = 1024;
968 sizes.fb_height = sizes.surface_height = 768;
971 /* push down into drivers */
972 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
977 info = fb_helper->fbdev;
980 /* set the fb pointer */
981 for (i = 0; i < fb_helper->crtc_count; i++) {
982 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
987 info->var.pixclock = 0;
988 if (register_framebuffer(info) < 0) {
992 printf("fb%d: %s frame buffer device\n", info->node,
996 drm_fb_helper_set_par(info);
999 /* Switch back to kernel console on panic */
1000 /* multi card linked list maybe */
1001 if (list_empty(&kernel_fb_helper_list)) {
1002 printf("drm: registered panic notifier\n");
1003 atomic_notifier_chain_register(&panic_notifier_list,
1007 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1014 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1017 info->fix.type = FB_TYPE_PACKED_PIXELS;
1018 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1019 FB_VISUAL_trueCOLOR;
1020 info->fix.mmio_start = 0;
1021 info->fix.mmio_len = 0;
1022 info->fix.type_aux = 0;
1023 info->fix.xpanstep = 1; /* doing it in hw */
1024 info->fix.ypanstep = 1; /* doing it in hw */
1025 info->fix.ywrapstep = 0;
1026 info->fix.accel = FB_ACCEL_NONE;
1027 info->fix.type_aux = 0;
1029 info->fix.line_length = pitch;
1033 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1034 uint32_t fb_width, uint32_t fb_height)
1036 struct drm_framebuffer *fb = fb_helper->fb;
1037 info->pseudo_palette = fb_helper->pseudo_palette;
1038 info->var.xres_virtual = fb->width;
1039 info->var.yres_virtual = fb->height;
1040 info->var.bits_per_pixel = fb->bits_per_pixel;
1041 info->var.accel_flags = FB_ACCELF_TEXT;
1042 info->var.xoffset = 0;
1043 info->var.yoffset = 0;
1044 info->var.activate = FB_ACTIVATE_NOW;
1045 info->var.height = -1;
1046 info->var.width = -1;
1048 switch (fb->depth) {
1050 info->var.red.offset = 0;
1051 info->var.green.offset = 0;
1052 info->var.blue.offset = 0;
1053 info->var.red.length = 8; /* 8bit DAC */
1054 info->var.green.length = 8;
1055 info->var.blue.length = 8;
1056 info->var.transp.offset = 0;
1057 info->var.transp.length = 0;
1060 info->var.red.offset = 10;
1061 info->var.green.offset = 5;
1062 info->var.blue.offset = 0;
1063 info->var.red.length = 5;
1064 info->var.green.length = 5;
1065 info->var.blue.length = 5;
1066 info->var.transp.offset = 15;
1067 info->var.transp.length = 1;
1070 info->var.red.offset = 11;
1071 info->var.green.offset = 5;
1072 info->var.blue.offset = 0;
1073 info->var.red.length = 5;
1074 info->var.green.length = 6;
1075 info->var.blue.length = 5;
1076 info->var.transp.offset = 0;
1079 info->var.red.offset = 16;
1080 info->var.green.offset = 8;
1081 info->var.blue.offset = 0;
1082 info->var.red.length = 8;
1083 info->var.green.length = 8;
1084 info->var.blue.length = 8;
1085 info->var.transp.offset = 0;
1086 info->var.transp.length = 0;
1089 info->var.red.offset = 16;
1090 info->var.green.offset = 8;
1091 info->var.blue.offset = 0;
1092 info->var.red.length = 8;
1093 info->var.green.length = 8;
1094 info->var.blue.length = 8;
1095 info->var.transp.offset = 24;
1096 info->var.transp.length = 8;
1102 info->var.xres = fb_width;
1103 info->var.yres = fb_height;
1107 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1111 struct drm_connector *connector;
1115 for (i = 0; i < fb_helper->connector_count; i++) {
1116 connector = fb_helper->connector_info[i]->connector;
1117 count += connector->funcs->fill_modes(connector, maxX, maxY);
1123 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1125 struct drm_display_mode *mode;
1127 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1128 if (drm_mode_width(mode) > width ||
1129 drm_mode_height(mode) > height)
1131 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1137 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1139 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1140 cmdline_mode = &fb_connector->cmdline_mode;
1141 return cmdline_mode->specified;
1144 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1145 int width, int height)
1147 struct drm_cmdline_mode *cmdline_mode;
1148 struct drm_display_mode *mode = NULL;
1150 cmdline_mode = &fb_helper_conn->cmdline_mode1;
1151 if (cmdline_mode->specified == false &&
1152 !drm_fetch_cmdline_mode_from_kenv(fb_helper_conn->connector,
1156 /* attempt to find a matching mode in the list of modes
1157 * we have gotten so far, if not add a CVT mode that conforms
1159 if (cmdline_mode->rb || cmdline_mode->margins)
1162 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1163 /* check width/height */
1164 if (mode->hdisplay != cmdline_mode->xres ||
1165 mode->vdisplay != cmdline_mode->yres)
1168 if (cmdline_mode->refresh_specified) {
1169 if (mode->vrefresh != cmdline_mode->refresh)
1173 if (cmdline_mode->interlace) {
1174 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1181 if (cmdline_mode->cvt)
1182 mode = drm_cvt_mode(fb_helper_conn->connector->dev,
1183 cmdline_mode->xres, cmdline_mode->yres,
1184 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1185 cmdline_mode->rb, cmdline_mode->interlace,
1186 cmdline_mode->margins);
1188 mode = drm_gtf_mode(fb_helper_conn->connector->dev,
1189 cmdline_mode->xres, cmdline_mode->yres,
1190 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1191 cmdline_mode->interlace,
1192 cmdline_mode->margins);
1193 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1194 list_add(&mode->head, &fb_helper_conn->connector->modes);
1198 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1203 enable = connector->status == connector_status_connected;
1205 enable = connector->status != connector_status_disconnected;
1210 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1213 bool any_enabled = false;
1214 struct drm_connector *connector;
1217 for (i = 0; i < fb_helper->connector_count; i++) {
1218 connector = fb_helper->connector_info[i]->connector;
1219 enabled[i] = drm_connector_enabled(connector, true);
1220 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1221 enabled[i] ? "yes" : "no");
1222 any_enabled |= enabled[i];
1228 for (i = 0; i < fb_helper->connector_count; i++) {
1229 connector = fb_helper->connector_info[i]->connector;
1230 enabled[i] = drm_connector_enabled(connector, false);
1234 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1235 struct drm_display_mode **modes,
1236 bool *enabled, int width, int height)
1239 bool can_clone = false;
1240 struct drm_fb_helper_connector *fb_helper_conn;
1241 struct drm_display_mode *dmt_mode, *mode;
1243 /* only contemplate cloning in the single crtc case */
1244 if (fb_helper->crtc_count > 1)
1248 for (i = 0; i < fb_helper->connector_count; i++) {
1253 /* only contemplate cloning if more than one connector is enabled */
1257 /* check the command line or if nothing common pick 1024x768 */
1259 for (i = 0; i < fb_helper->connector_count; i++) {
1262 fb_helper_conn = fb_helper->connector_info[i];
1263 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1268 for (j = 0; j < i; j++) {
1271 if (!drm_mode_equal(modes[j], modes[i]))
1277 DRM_DEBUG_KMS("can clone using command line\n");
1281 /* try and find a 1024x768 mode on each connector */
1283 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1285 for (i = 0; i < fb_helper->connector_count; i++) {
1290 fb_helper_conn = fb_helper->connector_info[i];
1291 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1292 if (drm_mode_equal(mode, dmt_mode))
1300 DRM_DEBUG_KMS("can clone using 1024x768\n");
1303 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1307 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1308 struct drm_display_mode **modes,
1309 bool *enabled, int width, int height)
1311 struct drm_fb_helper_connector *fb_helper_conn;
1314 for (i = 0; i < fb_helper->connector_count; i++) {
1315 fb_helper_conn = fb_helper->connector_info[i];
1317 if (enabled[i] == false)
1320 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1321 fb_helper_conn->connector->base.id);
1323 /* got for command line mode first */
1324 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1326 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1327 fb_helper_conn->connector->base.id);
1328 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1330 /* No preferred modes, pick one off the list */
1331 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1332 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1335 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1341 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1342 struct drm_fb_helper_crtc **best_crtcs,
1343 struct drm_display_mode **modes,
1344 int n, int width, int height)
1347 struct drm_device *dev = fb_helper->dev;
1348 struct drm_connector *connector;
1349 struct drm_connector_helper_funcs *connector_funcs;
1350 struct drm_encoder *encoder;
1351 struct drm_fb_helper_crtc *best_crtc;
1352 int my_score, best_score, score;
1353 struct drm_fb_helper_crtc **crtcs, *crtc;
1354 struct drm_fb_helper_connector *fb_helper_conn;
1356 if (n == fb_helper->connector_count)
1359 fb_helper_conn = fb_helper->connector_info[n];
1360 connector = fb_helper_conn->connector;
1362 best_crtcs[n] = NULL;
1364 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1365 if (modes[n] == NULL)
1368 crtcs = malloc(dev->mode_config.num_connector *
1369 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1373 if (connector->status == connector_status_connected)
1375 if (drm_has_cmdline_mode(fb_helper_conn))
1377 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1380 connector_funcs = connector->helper_private;
1381 encoder = connector_funcs->best_encoder(connector);
1385 /* select a crtc for this connector and then attempt to configure
1386 remaining connectors */
1387 for (c = 0; c < fb_helper->crtc_count; c++) {
1388 crtc = &fb_helper->crtc_info[c];
1390 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1394 for (o = 0; o < n; o++)
1395 if (best_crtcs[o] == crtc)
1399 /* ignore cloning unless only a single crtc */
1400 if (fb_helper->crtc_count > 1)
1403 if (!drm_mode_equal(modes[o], modes[n]))
1408 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1409 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1411 if (score > best_score) {
1414 memcpy(best_crtcs, crtcs,
1415 dev->mode_config.num_connector *
1416 sizeof(struct drm_fb_helper_crtc *));
1420 free(crtcs, DRM_MEM_KMS);
1424 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1426 struct drm_device *dev = fb_helper->dev;
1427 struct drm_fb_helper_crtc **crtcs;
1428 struct drm_display_mode **modes;
1429 struct drm_encoder *encoder;
1430 struct drm_mode_set *modeset;
1435 DRM_DEBUG_KMS("\n");
1437 width = dev->mode_config.max_width;
1438 height = dev->mode_config.max_height;
1440 /* clean out all the encoder/crtc combos */
1441 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1442 encoder->crtc = NULL;
1445 crtcs = malloc(dev->mode_config.num_connector *
1446 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1448 modes = malloc(dev->mode_config.num_connector *
1449 sizeof(struct drm_display_mode *), DRM_MEM_KMS,
1451 enabled = malloc(dev->mode_config.num_connector *
1452 sizeof(bool), DRM_MEM_KMS, M_WAITOK | M_ZERO);
1454 drm_enable_connectors(fb_helper, enabled);
1456 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1458 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1460 DRM_ERROR("Unable to find initial modes\n");
1463 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1465 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1467 /* need to set the modesets up here for use later */
1468 /* fill out the connector<->crtc mappings into the modesets */
1469 for (i = 0; i < fb_helper->crtc_count; i++) {
1470 modeset = &fb_helper->crtc_info[i].mode_set;
1471 modeset->num_connectors = 0;
1474 for (i = 0; i < fb_helper->connector_count; i++) {
1475 struct drm_display_mode *mode = modes[i];
1476 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1477 modeset = &fb_crtc->mode_set;
1479 if (mode && fb_crtc) {
1480 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1481 mode->name, fb_crtc->mode_set.crtc->base.id);
1482 fb_crtc->desired_mode = mode;
1484 drm_mode_destroy(dev, modeset->mode);
1485 modeset->mode = drm_mode_duplicate(dev,
1486 fb_crtc->desired_mode);
1487 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1491 free(crtcs, DRM_MEM_KMS);
1492 free(modes, DRM_MEM_KMS);
1493 free(enabled, DRM_MEM_KMS);
1497 * drm_helper_initial_config - setup a sane initial connector configuration
1501 * Called at init time, must take mode config lock.
1503 * Scan the CRTCs and connectors and try to put together an initial setup.
1504 * At the moment, this is a cloned configuration across all heads with
1505 * a new framebuffer object as the backing store.
1508 * Zero if everything went ok, nonzero otherwise.
1510 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1512 struct drm_device *dev = fb_helper->dev;
1515 /* disable all the possible outputs/crtcs before entering KMS mode */
1516 drm_helper_disable_unused_functions(fb_helper->dev);
1518 drm_fb_helper_parse_command_line(fb_helper);
1520 count = drm_fb_helper_probe_connector_modes(fb_helper,
1521 dev->mode_config.max_width,
1522 dev->mode_config.max_height);
1524 * we shouldn't end up with no modes here.
1527 printf("No connectors reported connected with modes\n");
1529 drm_setup_crtcs(fb_helper);
1531 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1534 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1536 struct drm_device *dev = fb_helper->dev;
1538 u32 max_width, max_height, bpp_sel;
1539 bool bound = false, crtcs_bound = false;
1540 struct drm_crtc *crtc;
1545 sx_xlock(&dev->mode_config.mutex);
1546 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1549 if (crtc->fb == fb_helper->fb)
1553 if (!bound && crtcs_bound) {
1554 fb_helper->delayed_hotplug = true;
1555 sx_xunlock(&dev->mode_config.mutex);
1558 DRM_DEBUG_KMS("\n");
1560 max_width = fb_helper->fb->width;
1561 max_height = fb_helper->fb->height;
1562 bpp_sel = fb_helper->fb->bits_per_pixel;
1564 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1566 drm_setup_crtcs(fb_helper);
1567 sx_xunlock(&dev->mode_config.mutex);
1569 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);