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>
30 #include <linux/kernel.h>
31 #include <linux/sysrq.h>
32 #include <linux/slab.h>
36 #include "drm_fb_helper.h"
37 #include "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 LIST_HEAD(kernel_fb_helper_list
);
45 static struct slow_work_ops output_status_change_ops
;
47 /* simple single crtc case helper function */
48 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper
*fb_helper
)
50 struct drm_device
*dev
= fb_helper
->dev
;
51 struct drm_connector
*connector
;
54 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
55 struct drm_fb_helper_connector
*fb_helper_connector
;
57 fb_helper_connector
= kzalloc(sizeof(struct drm_fb_helper_connector
), GFP_KERNEL
);
58 if (!fb_helper_connector
)
61 fb_helper_connector
->connector
= connector
;
62 fb_helper
->connector_info
[fb_helper
->connector_count
++] = fb_helper_connector
;
66 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
67 kfree(fb_helper
->connector_info
[i
]);
68 fb_helper
->connector_info
[i
] = NULL
;
70 fb_helper
->connector_count
= 0;
73 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors
);
76 * drm_fb_helper_connector_parse_command_line - parse command line for connector
77 * @connector - connector to parse line for
78 * @mode_option - per connector mode option
80 * This parses the connector specific then generic command lines for
81 * modes and options to configure the connector.
83 * This uses the same parameters as the fb modedb.c, except for extra
84 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
86 * enable/enable Digital/disable bit at the end
88 static bool drm_fb_helper_connector_parse_command_line(struct drm_fb_helper_connector
*fb_helper_conn
,
89 const char *mode_option
)
93 int res_specified
= 0, bpp_specified
= 0, refresh_specified
= 0;
94 unsigned int xres
= 0, yres
= 0, bpp
= 32, refresh
= 0;
95 int yres_specified
= 0, cvt
= 0, rb
= 0, interlace
= 0, margins
= 0;
97 enum drm_connector_force force
= DRM_FORCE_UNSPECIFIED
;
98 struct drm_fb_helper_cmdline_mode
*cmdline_mode
;
99 struct drm_connector
*connector
= fb_helper_conn
->connector
;
104 cmdline_mode
= &fb_helper_conn
->cmdline_mode
;
106 mode_option
= fb_mode_option
;
109 cmdline_mode
->specified
= false;
114 namelen
= strlen(name
);
115 for (i
= namelen
-1; i
>= 0; i
--) {
119 if (!refresh_specified
&& !bpp_specified
&&
121 refresh
= simple_strtol(&name
[i
+1], NULL
, 10);
122 refresh_specified
= 1;
130 if (!bpp_specified
&& !yres_specified
) {
131 bpp
= simple_strtol(&name
[i
+1], NULL
, 10);
139 if (!yres_specified
) {
140 yres
= simple_strtol(&name
[i
+1], NULL
, 10);
163 force
= DRM_FORCE_ON
;
166 if ((connector
->connector_type
!= DRM_MODE_CONNECTOR_DVII
) &&
167 (connector
->connector_type
!= DRM_MODE_CONNECTOR_HDMIB
))
168 force
= DRM_FORCE_ON
;
170 force
= DRM_FORCE_ON_DIGITAL
;
173 force
= DRM_FORCE_OFF
;
179 if (i
< 0 && yres_specified
) {
180 xres
= simple_strtol(name
, NULL
, 10);
185 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
186 drm_get_connector_name(connector
), xres
, yres
,
187 (refresh
) ? refresh
: 60, (rb
) ? " reduced blanking" :
188 "", (margins
) ? " with margins" : "", (interlace
) ?
194 case DRM_FORCE_OFF
: s
= "OFF"; break;
195 case DRM_FORCE_ON_DIGITAL
: s
= "ON - dig"; break;
197 case DRM_FORCE_ON
: s
= "ON"; break;
200 DRM_INFO("forcing %s connector %s\n",
201 drm_get_connector_name(connector
), s
);
202 connector
->force
= force
;
206 cmdline_mode
->specified
= true;
207 cmdline_mode
->xres
= xres
;
208 cmdline_mode
->yres
= yres
;
211 if (refresh_specified
) {
212 cmdline_mode
->refresh_specified
= true;
213 cmdline_mode
->refresh
= refresh
;
217 cmdline_mode
->bpp_specified
= true;
218 cmdline_mode
->bpp
= bpp
;
220 cmdline_mode
->rb
= rb
? true : false;
221 cmdline_mode
->cvt
= cvt
? true : false;
222 cmdline_mode
->interlace
= interlace
? true : false;
227 static int drm_fb_helper_parse_command_line(struct drm_fb_helper
*fb_helper
)
229 struct drm_fb_helper_connector
*fb_helper_conn
;
232 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
235 fb_helper_conn
= fb_helper
->connector_info
[i
];
237 /* do something on return - turn off connector maybe */
238 if (fb_get_options(drm_get_connector_name(fb_helper_conn
->connector
), &option
))
241 drm_fb_helper_connector_parse_command_line(fb_helper_conn
, option
);
246 bool drm_fb_helper_force_kernel_mode(void)
249 bool ret
, error
= false;
250 struct drm_fb_helper
*helper
;
252 if (list_empty(&kernel_fb_helper_list
))
255 list_for_each_entry(helper
, &kernel_fb_helper_list
, kernel_fb_list
) {
256 for (i
= 0; i
< helper
->crtc_count
; i
++) {
257 struct drm_mode_set
*mode_set
= &helper
->crtc_info
[i
].mode_set
;
258 ret
= drm_crtc_helper_set_config(mode_set
);
266 int drm_fb_helper_panic(struct notifier_block
*n
, unsigned long ununsed
,
269 DRM_ERROR("panic occurred, switching back to text console\n");
270 return drm_fb_helper_force_kernel_mode();
273 EXPORT_SYMBOL(drm_fb_helper_panic
);
275 static struct notifier_block paniced
= {
276 .notifier_call
= drm_fb_helper_panic
,
280 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
282 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
284 void drm_fb_helper_restore(void)
287 ret
= drm_fb_helper_force_kernel_mode();
289 DRM_ERROR("Failed to restore crtc configuration\n");
291 EXPORT_SYMBOL(drm_fb_helper_restore
);
293 #ifdef CONFIG_MAGIC_SYSRQ
294 static void drm_fb_helper_restore_work_fn(struct work_struct
*ignored
)
296 drm_fb_helper_restore();
298 static DECLARE_WORK(drm_fb_helper_restore_work
, drm_fb_helper_restore_work_fn
);
300 static void drm_fb_helper_sysrq(int dummy1
, struct tty_struct
*dummy3
)
302 schedule_work(&drm_fb_helper_restore_work
);
305 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op
= {
306 .handler
= drm_fb_helper_sysrq
,
307 .help_msg
= "force-fb(V)",
308 .action_msg
= "Restore framebuffer console",
311 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op
= { };
314 static void drm_fb_helper_on(struct fb_info
*info
)
316 struct drm_fb_helper
*fb_helper
= info
->par
;
317 struct drm_device
*dev
= fb_helper
->dev
;
318 struct drm_crtc
*crtc
;
319 struct drm_crtc_helper_funcs
*crtc_funcs
;
320 struct drm_encoder
*encoder
;
324 * For each CRTC in this fb, turn the crtc on then,
325 * find all associated encoders and turn them on.
327 mutex_lock(&dev
->mode_config
.mutex
);
328 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
329 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
330 crtc_funcs
= crtc
->helper_private
;
335 crtc_funcs
->dpms(crtc
, DRM_MODE_DPMS_ON
);
338 /* Found a CRTC on this fb, now find encoders */
339 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
340 if (encoder
->crtc
== crtc
) {
341 struct drm_encoder_helper_funcs
*encoder_funcs
;
343 encoder_funcs
= encoder
->helper_private
;
344 encoder_funcs
->dpms(encoder
, DRM_MODE_DPMS_ON
);
348 mutex_unlock(&dev
->mode_config
.mutex
);
351 static void drm_fb_helper_off(struct fb_info
*info
, int dpms_mode
)
353 struct drm_fb_helper
*fb_helper
= info
->par
;
354 struct drm_device
*dev
= fb_helper
->dev
;
355 struct drm_crtc
*crtc
;
356 struct drm_crtc_helper_funcs
*crtc_funcs
;
357 struct drm_encoder
*encoder
;
361 * For each CRTC in this fb, find all associated encoders
362 * and turn them off, then turn off the CRTC.
364 mutex_lock(&dev
->mode_config
.mutex
);
365 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
366 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
367 crtc_funcs
= crtc
->helper_private
;
372 /* Found a CRTC on this fb, now find encoders */
373 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
374 if (encoder
->crtc
== crtc
) {
375 struct drm_encoder_helper_funcs
*encoder_funcs
;
377 encoder_funcs
= encoder
->helper_private
;
378 encoder_funcs
->dpms(encoder
, dpms_mode
);
381 crtc_funcs
->dpms(crtc
, DRM_MODE_DPMS_OFF
);
383 mutex_unlock(&dev
->mode_config
.mutex
);
386 int drm_fb_helper_blank(int blank
, struct fb_info
*info
)
389 /* Display: On; HSync: On, VSync: On */
390 case FB_BLANK_UNBLANK
:
391 drm_fb_helper_on(info
);
393 /* Display: Off; HSync: On, VSync: On */
394 case FB_BLANK_NORMAL
:
395 drm_fb_helper_off(info
, DRM_MODE_DPMS_STANDBY
);
397 /* Display: Off; HSync: Off, VSync: On */
398 case FB_BLANK_HSYNC_SUSPEND
:
399 drm_fb_helper_off(info
, DRM_MODE_DPMS_STANDBY
);
401 /* Display: Off; HSync: On, VSync: Off */
402 case FB_BLANK_VSYNC_SUSPEND
:
403 drm_fb_helper_off(info
, DRM_MODE_DPMS_SUSPEND
);
405 /* Display: Off; HSync: Off, VSync: Off */
406 case FB_BLANK_POWERDOWN
:
407 drm_fb_helper_off(info
, DRM_MODE_DPMS_OFF
);
412 EXPORT_SYMBOL(drm_fb_helper_blank
);
414 static void drm_fb_helper_crtc_free(struct drm_fb_helper
*helper
)
418 for (i
= 0; i
< helper
->connector_count
; i
++)
419 kfree(helper
->connector_info
[i
]);
420 kfree(helper
->connector_info
);
421 for (i
= 0; i
< helper
->crtc_count
; i
++)
422 kfree(helper
->crtc_info
[i
].mode_set
.connectors
);
423 kfree(helper
->crtc_info
);
426 int drm_fb_helper_init(struct drm_device
*dev
,
427 struct drm_fb_helper
*fb_helper
,
428 int crtc_count
, int max_conn_count
,
431 struct drm_crtc
*crtc
;
435 fb_helper
->dev
= dev
;
436 fb_helper
->poll_enabled
= polled
;
438 slow_work_register_user(THIS_MODULE
);
439 delayed_slow_work_init(&fb_helper
->output_status_change_slow_work
,
440 &output_status_change_ops
);
442 INIT_LIST_HEAD(&fb_helper
->kernel_fb_list
);
444 fb_helper
->crtc_info
= kcalloc(crtc_count
, sizeof(struct drm_fb_helper_crtc
), GFP_KERNEL
);
445 if (!fb_helper
->crtc_info
)
448 fb_helper
->crtc_count
= crtc_count
;
449 fb_helper
->connector_info
= kcalloc(dev
->mode_config
.num_connector
, sizeof(struct drm_fb_helper_connector
*), GFP_KERNEL
);
450 if (!fb_helper
->connector_info
) {
451 kfree(fb_helper
->crtc_info
);
454 fb_helper
->connector_count
= 0;
456 for (i
= 0; i
< crtc_count
; i
++) {
457 fb_helper
->crtc_info
[i
].mode_set
.connectors
=
458 kcalloc(max_conn_count
,
459 sizeof(struct drm_connector
*),
462 if (!fb_helper
->crtc_info
[i
].mode_set
.connectors
) {
466 fb_helper
->crtc_info
[i
].mode_set
.num_connectors
= 0;
470 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
471 fb_helper
->crtc_info
[i
].crtc_id
= crtc
->base
.id
;
472 fb_helper
->crtc_info
[i
].mode_set
.crtc
= crtc
;
475 fb_helper
->conn_limit
= max_conn_count
;
478 drm_fb_helper_crtc_free(fb_helper
);
481 EXPORT_SYMBOL(drm_fb_helper_init
);
483 void drm_fb_helper_fini(struct drm_fb_helper
*fb_helper
)
485 if (!list_empty(&fb_helper
->kernel_fb_list
)) {
486 list_del(&fb_helper
->kernel_fb_list
);
487 if (list_empty(&kernel_fb_helper_list
)) {
488 printk(KERN_INFO
"drm: unregistered panic notifier\n");
489 atomic_notifier_chain_unregister(&panic_notifier_list
,
491 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op
);
495 drm_fb_helper_crtc_free(fb_helper
);
497 delayed_slow_work_cancel(&fb_helper
->output_status_change_slow_work
);
498 slow_work_unregister_user(THIS_MODULE
);
500 EXPORT_SYMBOL(drm_fb_helper_fini
);
502 static int setcolreg(struct drm_crtc
*crtc
, u16 red
, u16 green
,
503 u16 blue
, u16 regno
, struct fb_info
*info
)
505 struct drm_fb_helper
*fb_helper
= info
->par
;
506 struct drm_framebuffer
*fb
= fb_helper
->fb
;
509 if (info
->fix
.visual
== FB_VISUAL_TRUECOLOR
) {
512 /* place color in psuedopalette */
515 palette
= (u32
*)info
->pseudo_palette
;
516 red
>>= (16 - info
->var
.red
.length
);
517 green
>>= (16 - info
->var
.green
.length
);
518 blue
>>= (16 - info
->var
.blue
.length
);
519 value
= (red
<< info
->var
.red
.offset
) |
520 (green
<< info
->var
.green
.offset
) |
521 (blue
<< info
->var
.blue
.offset
);
522 palette
[regno
] = value
;
528 if (fb
->bits_per_pixel
== 16) {
531 if (fb
->depth
== 16 && regno
> 63)
533 if (fb
->depth
== 15 && regno
> 31)
536 if (fb
->depth
== 16) {
540 for (i
= 0; i
< 8; i
++)
541 fb_helper
->funcs
->gamma_set(crtc
, red
,
542 green
, blue
, pindex
+ i
);
545 fb_helper
->funcs
->gamma_get(crtc
, &r
,
549 for (i
= 0; i
< 4; i
++)
550 fb_helper
->funcs
->gamma_set(crtc
, r
,
557 fb_helper
->funcs
->gamma_set(crtc
, red
, green
, blue
, pindex
);
561 int drm_fb_helper_setcmap(struct fb_cmap
*cmap
, struct fb_info
*info
)
563 struct drm_fb_helper
*fb_helper
= info
->par
;
564 struct drm_crtc_helper_funcs
*crtc_funcs
;
565 u16
*red
, *green
, *blue
, *transp
;
566 struct drm_crtc
*crtc
;
570 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
571 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
572 crtc_funcs
= crtc
->helper_private
;
577 transp
= cmap
->transp
;
580 for (i
= 0; i
< cmap
->len
; i
++) {
581 u16 hred
, hgreen
, hblue
, htransp
= 0xffff;
590 rc
= setcolreg(crtc
, hred
, hgreen
, hblue
, start
++, info
);
594 crtc_funcs
->load_lut(crtc
);
598 EXPORT_SYMBOL(drm_fb_helper_setcmap
);
600 int drm_fb_helper_check_var(struct fb_var_screeninfo
*var
,
601 struct fb_info
*info
)
603 struct drm_fb_helper
*fb_helper
= info
->par
;
604 struct drm_framebuffer
*fb
= fb_helper
->fb
;
607 if (var
->pixclock
!= 0)
610 /* Need to resize the fb object !!! */
611 if (var
->bits_per_pixel
> fb
->bits_per_pixel
|| var
->xres
> fb
->width
|| var
->yres
> fb
->height
) {
612 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
613 "object %dx%d-%d > %dx%d-%d\n", var
->xres
, var
->yres
, var
->bits_per_pixel
,
614 fb
->width
, fb
->height
, fb
->bits_per_pixel
);
618 switch (var
->bits_per_pixel
) {
620 depth
= (var
->green
.length
== 6) ? 16 : 15;
623 depth
= (var
->transp
.length
> 0) ? 32 : 24;
626 depth
= var
->bits_per_pixel
;
633 var
->green
.offset
= 0;
634 var
->blue
.offset
= 0;
636 var
->green
.length
= 8;
637 var
->blue
.length
= 8;
638 var
->transp
.length
= 0;
639 var
->transp
.offset
= 0;
642 var
->red
.offset
= 10;
643 var
->green
.offset
= 5;
644 var
->blue
.offset
= 0;
646 var
->green
.length
= 5;
647 var
->blue
.length
= 5;
648 var
->transp
.length
= 1;
649 var
->transp
.offset
= 15;
652 var
->red
.offset
= 11;
653 var
->green
.offset
= 5;
654 var
->blue
.offset
= 0;
656 var
->green
.length
= 6;
657 var
->blue
.length
= 5;
658 var
->transp
.length
= 0;
659 var
->transp
.offset
= 0;
662 var
->red
.offset
= 16;
663 var
->green
.offset
= 8;
664 var
->blue
.offset
= 0;
666 var
->green
.length
= 8;
667 var
->blue
.length
= 8;
668 var
->transp
.length
= 0;
669 var
->transp
.offset
= 0;
672 var
->red
.offset
= 16;
673 var
->green
.offset
= 8;
674 var
->blue
.offset
= 0;
676 var
->green
.length
= 8;
677 var
->blue
.length
= 8;
678 var
->transp
.length
= 8;
679 var
->transp
.offset
= 24;
686 EXPORT_SYMBOL(drm_fb_helper_check_var
);
688 /* this will let fbcon do the mode init */
689 int drm_fb_helper_set_par(struct fb_info
*info
)
691 struct drm_fb_helper
*fb_helper
= info
->par
;
692 struct drm_device
*dev
= fb_helper
->dev
;
693 struct fb_var_screeninfo
*var
= &info
->var
;
694 struct drm_crtc
*crtc
;
698 if (var
->pixclock
!= 0) {
699 DRM_ERROR("PIXEL CLOCK SET\n");
703 mutex_lock(&dev
->mode_config
.mutex
);
704 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
705 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
706 ret
= crtc
->funcs
->set_config(&fb_helper
->crtc_info
[i
].mode_set
);
708 mutex_unlock(&dev
->mode_config
.mutex
);
712 mutex_unlock(&dev
->mode_config
.mutex
);
714 if (fb_helper
->delayed_hotplug
) {
715 fb_helper
->delayed_hotplug
= false;
716 delayed_slow_work_enqueue(&fb_helper
->output_status_change_slow_work
, 0);
720 EXPORT_SYMBOL(drm_fb_helper_set_par
);
722 int drm_fb_helper_pan_display(struct fb_var_screeninfo
*var
,
723 struct fb_info
*info
)
725 struct drm_fb_helper
*fb_helper
= info
->par
;
726 struct drm_device
*dev
= fb_helper
->dev
;
727 struct drm_mode_set
*modeset
;
728 struct drm_crtc
*crtc
;
732 mutex_lock(&dev
->mode_config
.mutex
);
733 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
734 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
736 modeset
= &fb_helper
->crtc_info
[i
].mode_set
;
738 modeset
->x
= var
->xoffset
;
739 modeset
->y
= var
->yoffset
;
741 if (modeset
->num_connectors
) {
742 ret
= crtc
->funcs
->set_config(modeset
);
744 info
->var
.xoffset
= var
->xoffset
;
745 info
->var
.yoffset
= var
->yoffset
;
749 mutex_unlock(&dev
->mode_config
.mutex
);
752 EXPORT_SYMBOL(drm_fb_helper_pan_display
);
754 int drm_fb_helper_single_fb_probe(struct drm_fb_helper
*fb_helper
,
760 struct fb_info
*info
;
761 struct drm_fb_helper_surface_size sizes
;
764 memset(&sizes
, 0, sizeof(struct drm_fb_helper_surface_size
));
765 sizes
.surface_depth
= 24;
766 sizes
.surface_bpp
= 32;
767 sizes
.fb_width
= (unsigned)-1;
768 sizes
.fb_height
= (unsigned)-1;
770 /* if driver picks 8 or 16 by default use that
771 for both depth/bpp */
772 if (preferred_bpp
!= sizes
.surface_bpp
) {
773 sizes
.surface_depth
= sizes
.surface_bpp
= preferred_bpp
;
775 /* first up get a count of crtcs now in use and new min/maxes width/heights */
776 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
777 struct drm_fb_helper_connector
*fb_helper_conn
= fb_helper
->connector_info
[i
];
778 struct drm_fb_helper_cmdline_mode
*cmdline_mode
;
780 cmdline_mode
= &fb_helper_conn
->cmdline_mode
;
782 if (cmdline_mode
->bpp_specified
) {
783 switch (cmdline_mode
->bpp
) {
785 sizes
.surface_depth
= sizes
.surface_bpp
= 8;
788 sizes
.surface_depth
= 15;
789 sizes
.surface_bpp
= 16;
792 sizes
.surface_depth
= sizes
.surface_bpp
= 16;
795 sizes
.surface_depth
= sizes
.surface_bpp
= 24;
798 sizes
.surface_depth
= 24;
799 sizes
.surface_bpp
= 32;
807 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
808 struct drm_display_mode
*desired_mode
;
809 desired_mode
= fb_helper
->crtc_info
[i
].desired_mode
;
813 gamma_size
= fb_helper
->crtc_info
[i
].mode_set
.crtc
->gamma_size
;
814 if (desired_mode
->hdisplay
< sizes
.fb_width
)
815 sizes
.fb_width
= desired_mode
->hdisplay
;
816 if (desired_mode
->vdisplay
< sizes
.fb_height
)
817 sizes
.fb_height
= desired_mode
->vdisplay
;
818 if (desired_mode
->hdisplay
> sizes
.surface_width
)
819 sizes
.surface_width
= desired_mode
->hdisplay
;
820 if (desired_mode
->vdisplay
> sizes
.surface_height
)
821 sizes
.surface_height
= desired_mode
->vdisplay
;
826 if (crtc_count
== 0 || sizes
.fb_width
== -1 || sizes
.fb_height
== -1) {
827 /* hmm everyone went away - assume VGA cable just fell out
828 and will come back later. */
829 DRM_ERROR("Cannot find any crtc or sizes - going 1024x768\n");
830 sizes
.fb_width
= sizes
.surface_width
= 1024;
831 sizes
.fb_height
= sizes
.surface_height
= 768;
834 /* push down into drivers */
835 new_fb
= (*fb_helper
->funcs
->fb_probe
)(fb_helper
, &sizes
);
839 info
= fb_helper
->fbdev
;
841 /* set the fb pointer */
842 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
843 fb_helper
->crtc_info
[i
].mode_set
.fb
= fb_helper
->fb
;
847 info
->var
.pixclock
= 0;
848 if (register_framebuffer(info
) < 0) {
852 printk(KERN_INFO
"fb%d: %s frame buffer device\n", info
->node
,
856 drm_fb_helper_set_par(info
);
859 /* Switch back to kernel console on panic */
860 /* multi card linked list maybe */
861 if (list_empty(&kernel_fb_helper_list
)) {
862 printk(KERN_INFO
"drm: registered panic notifier\n");
863 atomic_notifier_chain_register(&panic_notifier_list
,
865 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op
);
868 list_add(&fb_helper
->kernel_fb_list
, &kernel_fb_helper_list
);
872 EXPORT_SYMBOL(drm_fb_helper_single_fb_probe
);
874 void drm_fb_helper_fill_fix(struct fb_info
*info
, uint32_t pitch
,
877 info
->fix
.type
= FB_TYPE_PACKED_PIXELS
;
878 info
->fix
.visual
= depth
== 8 ? FB_VISUAL_PSEUDOCOLOR
:
880 info
->fix
.type_aux
= 0;
881 info
->fix
.xpanstep
= 1; /* doing it in hw */
882 info
->fix
.ypanstep
= 1; /* doing it in hw */
883 info
->fix
.ywrapstep
= 0;
884 info
->fix
.accel
= FB_ACCEL_NONE
;
885 info
->fix
.type_aux
= 0;
887 info
->fix
.line_length
= pitch
;
890 EXPORT_SYMBOL(drm_fb_helper_fill_fix
);
892 void drm_fb_helper_fill_var(struct fb_info
*info
, struct drm_fb_helper
*fb_helper
,
893 uint32_t fb_width
, uint32_t fb_height
)
895 struct drm_framebuffer
*fb
= fb_helper
->fb
;
896 info
->pseudo_palette
= fb_helper
->pseudo_palette
;
897 info
->var
.xres_virtual
= fb
->width
;
898 info
->var
.yres_virtual
= fb
->height
;
899 info
->var
.bits_per_pixel
= fb
->bits_per_pixel
;
900 info
->var
.xoffset
= 0;
901 info
->var
.yoffset
= 0;
902 info
->var
.activate
= FB_ACTIVATE_NOW
;
903 info
->var
.height
= -1;
904 info
->var
.width
= -1;
908 info
->var
.red
.offset
= 0;
909 info
->var
.green
.offset
= 0;
910 info
->var
.blue
.offset
= 0;
911 info
->var
.red
.length
= 8; /* 8bit DAC */
912 info
->var
.green
.length
= 8;
913 info
->var
.blue
.length
= 8;
914 info
->var
.transp
.offset
= 0;
915 info
->var
.transp
.length
= 0;
918 info
->var
.red
.offset
= 10;
919 info
->var
.green
.offset
= 5;
920 info
->var
.blue
.offset
= 0;
921 info
->var
.red
.length
= 5;
922 info
->var
.green
.length
= 5;
923 info
->var
.blue
.length
= 5;
924 info
->var
.transp
.offset
= 15;
925 info
->var
.transp
.length
= 1;
928 info
->var
.red
.offset
= 11;
929 info
->var
.green
.offset
= 5;
930 info
->var
.blue
.offset
= 0;
931 info
->var
.red
.length
= 5;
932 info
->var
.green
.length
= 6;
933 info
->var
.blue
.length
= 5;
934 info
->var
.transp
.offset
= 0;
937 info
->var
.red
.offset
= 16;
938 info
->var
.green
.offset
= 8;
939 info
->var
.blue
.offset
= 0;
940 info
->var
.red
.length
= 8;
941 info
->var
.green
.length
= 8;
942 info
->var
.blue
.length
= 8;
943 info
->var
.transp
.offset
= 0;
944 info
->var
.transp
.length
= 0;
947 info
->var
.red
.offset
= 16;
948 info
->var
.green
.offset
= 8;
949 info
->var
.blue
.offset
= 0;
950 info
->var
.red
.length
= 8;
951 info
->var
.green
.length
= 8;
952 info
->var
.blue
.length
= 8;
953 info
->var
.transp
.offset
= 24;
954 info
->var
.transp
.length
= 8;
960 info
->var
.xres
= fb_width
;
961 info
->var
.yres
= fb_height
;
963 EXPORT_SYMBOL(drm_fb_helper_fill_var
);
965 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper
*fb_helper
,
969 struct drm_connector
*connector
;
973 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
974 connector
= fb_helper
->connector_info
[i
]->connector
;
975 count
+= connector
->funcs
->fill_modes(connector
, maxX
, maxY
);
981 static struct drm_display_mode
*drm_has_preferred_mode(struct drm_fb_helper_connector
*fb_connector
, int width
, int height
)
983 struct drm_display_mode
*mode
;
985 list_for_each_entry(mode
, &fb_connector
->connector
->modes
, head
) {
986 if (drm_mode_width(mode
) > width
||
987 drm_mode_height(mode
) > height
)
989 if (mode
->type
& DRM_MODE_TYPE_PREFERRED
)
995 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector
*fb_connector
)
997 struct drm_fb_helper_cmdline_mode
*cmdline_mode
;
998 cmdline_mode
= &fb_connector
->cmdline_mode
;
999 return cmdline_mode
->specified
;
1002 static struct drm_display_mode
*drm_pick_cmdline_mode(struct drm_fb_helper_connector
*fb_helper_conn
,
1003 int width
, int height
)
1005 struct drm_fb_helper_cmdline_mode
*cmdline_mode
;
1006 struct drm_display_mode
*mode
= NULL
;
1008 cmdline_mode
= &fb_helper_conn
->cmdline_mode
;
1009 if (cmdline_mode
->specified
== false)
1012 /* attempt to find a matching mode in the list of modes
1013 * we have gotten so far, if not add a CVT mode that conforms
1015 if (cmdline_mode
->rb
|| cmdline_mode
->margins
)
1018 list_for_each_entry(mode
, &fb_helper_conn
->connector
->modes
, head
) {
1019 /* check width/height */
1020 if (mode
->hdisplay
!= cmdline_mode
->xres
||
1021 mode
->vdisplay
!= cmdline_mode
->yres
)
1024 if (cmdline_mode
->refresh_specified
) {
1025 if (mode
->vrefresh
!= cmdline_mode
->refresh
)
1029 if (cmdline_mode
->interlace
) {
1030 if (!(mode
->flags
& DRM_MODE_FLAG_INTERLACE
))
1037 mode
= drm_cvt_mode(fb_helper_conn
->connector
->dev
, cmdline_mode
->xres
,
1039 cmdline_mode
->refresh_specified
? cmdline_mode
->refresh
: 60,
1040 cmdline_mode
->rb
, cmdline_mode
->interlace
,
1041 cmdline_mode
->margins
);
1042 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
1043 list_add(&mode
->head
, &fb_helper_conn
->connector
->modes
);
1047 static bool drm_connector_enabled(struct drm_connector
*connector
, bool strict
)
1052 enable
= connector
->status
== connector_status_connected
;
1054 enable
= connector
->status
!= connector_status_disconnected
;
1059 static void drm_enable_connectors(struct drm_fb_helper
*fb_helper
,
1062 bool any_enabled
= false;
1063 struct drm_connector
*connector
;
1066 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1067 connector
= fb_helper
->connector_info
[i
]->connector
;
1068 enabled
[i
] = drm_connector_enabled(connector
, true);
1069 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector
->base
.id
,
1070 enabled
[i
] ? "yes" : "no");
1071 any_enabled
|= enabled
[i
];
1077 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1078 connector
= fb_helper
->connector_info
[i
]->connector
;
1079 enabled
[i
] = drm_connector_enabled(connector
, false);
1083 static bool drm_target_preferred(struct drm_fb_helper
*fb_helper
,
1084 struct drm_display_mode
**modes
,
1085 bool *enabled
, int width
, int height
)
1087 struct drm_fb_helper_connector
*fb_helper_conn
;
1090 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1091 fb_helper_conn
= fb_helper
->connector_info
[i
];
1093 if (enabled
[i
] == false)
1096 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1097 fb_helper_conn
->connector
->base
.id
);
1099 /* got for command line mode first */
1100 modes
[i
] = drm_pick_cmdline_mode(fb_helper_conn
, width
, height
);
1102 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1103 fb_helper_conn
->connector
->base
.id
);
1104 modes
[i
] = drm_has_preferred_mode(fb_helper_conn
, width
, height
);
1106 /* No preferred modes, pick one off the list */
1107 if (!modes
[i
] && !list_empty(&fb_helper_conn
->connector
->modes
)) {
1108 list_for_each_entry(modes
[i
], &fb_helper_conn
->connector
->modes
, head
)
1111 DRM_DEBUG_KMS("found mode %s\n", modes
[i
] ? modes
[i
]->name
:
1117 static int drm_pick_crtcs(struct drm_fb_helper
*fb_helper
,
1118 struct drm_fb_helper_crtc
**best_crtcs
,
1119 struct drm_display_mode
**modes
,
1120 int n
, int width
, int height
)
1123 struct drm_device
*dev
= fb_helper
->dev
;
1124 struct drm_connector
*connector
;
1125 struct drm_connector_helper_funcs
*connector_funcs
;
1126 struct drm_encoder
*encoder
;
1127 struct drm_fb_helper_crtc
*best_crtc
;
1128 int my_score
, best_score
, score
;
1129 struct drm_fb_helper_crtc
**crtcs
, *crtc
;
1130 struct drm_fb_helper_connector
*fb_helper_conn
;
1132 if (n
== fb_helper
->connector_count
)
1135 fb_helper_conn
= fb_helper
->connector_info
[n
];
1136 connector
= fb_helper_conn
->connector
;
1138 best_crtcs
[n
] = NULL
;
1140 best_score
= drm_pick_crtcs(fb_helper
, best_crtcs
, modes
, n
+1, width
, height
);
1141 if (modes
[n
] == NULL
)
1144 crtcs
= kzalloc(dev
->mode_config
.num_connector
*
1145 sizeof(struct drm_fb_helper_crtc
*), GFP_KERNEL
);
1150 if (connector
->status
== connector_status_connected
)
1152 if (drm_has_cmdline_mode(fb_helper_conn
))
1154 if (drm_has_preferred_mode(fb_helper_conn
, width
, height
))
1157 connector_funcs
= connector
->helper_private
;
1158 encoder
= connector_funcs
->best_encoder(connector
);
1162 /* select a crtc for this connector and then attempt to configure
1163 remaining connectors */
1164 for (c
= 0; c
< fb_helper
->crtc_count
; c
++) {
1165 crtc
= &fb_helper
->crtc_info
[c
];
1167 if ((encoder
->possible_crtcs
& (1 << c
)) == 0) {
1171 for (o
= 0; o
< n
; o
++)
1172 if (best_crtcs
[o
] == crtc
)
1176 /* ignore cloning for now */
1181 memcpy(crtcs
, best_crtcs
, n
* sizeof(struct drm_fb_helper_crtc
*));
1182 score
= my_score
+ drm_pick_crtcs(fb_helper
, crtcs
, modes
, n
+ 1,
1184 if (score
> best_score
) {
1187 memcpy(best_crtcs
, crtcs
,
1188 dev
->mode_config
.num_connector
*
1189 sizeof(struct drm_fb_helper_crtc
*));
1197 static void drm_setup_crtcs(struct drm_fb_helper
*fb_helper
)
1199 struct drm_device
*dev
= fb_helper
->dev
;
1200 struct drm_fb_helper_crtc
**crtcs
;
1201 struct drm_display_mode
**modes
;
1202 struct drm_encoder
*encoder
;
1203 struct drm_mode_set
*modeset
;
1208 DRM_DEBUG_KMS("\n");
1210 width
= dev
->mode_config
.max_width
;
1211 height
= dev
->mode_config
.max_height
;
1213 /* clean out all the encoder/crtc combos */
1214 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
1215 encoder
->crtc
= NULL
;
1218 crtcs
= kcalloc(dev
->mode_config
.num_connector
,
1219 sizeof(struct drm_fb_helper_crtc
*), GFP_KERNEL
);
1220 modes
= kcalloc(dev
->mode_config
.num_connector
,
1221 sizeof(struct drm_display_mode
*), GFP_KERNEL
);
1222 enabled
= kcalloc(dev
->mode_config
.num_connector
,
1223 sizeof(bool), GFP_KERNEL
);
1225 drm_enable_connectors(fb_helper
, enabled
);
1227 ret
= drm_target_preferred(fb_helper
, modes
, enabled
, width
, height
);
1229 DRM_ERROR("Unable to find initial modes\n");
1231 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width
, height
);
1233 drm_pick_crtcs(fb_helper
, crtcs
, modes
, 0, width
, height
);
1235 /* need to set the modesets up here for use later */
1236 /* fill out the connector<->crtc mappings into the modesets */
1237 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1238 modeset
= &fb_helper
->crtc_info
[i
].mode_set
;
1239 modeset
->num_connectors
= 0;
1242 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1243 struct drm_display_mode
*mode
= modes
[i
];
1244 struct drm_fb_helper_crtc
*fb_crtc
= crtcs
[i
];
1245 modeset
= &fb_crtc
->mode_set
;
1247 if (mode
&& fb_crtc
) {
1248 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1249 mode
->name
, fb_crtc
->mode_set
.crtc
->base
.id
);
1250 fb_crtc
->desired_mode
= mode
;
1252 drm_mode_destroy(dev
, modeset
->mode
);
1253 modeset
->mode
= drm_mode_duplicate(dev
,
1254 fb_crtc
->desired_mode
);
1255 modeset
->connectors
[modeset
->num_connectors
++] = fb_helper
->connector_info
[i
]->connector
;
1265 * drm_helper_initial_config - setup a sane initial connector configuration
1269 * Called at init time, must take mode config lock.
1271 * Scan the CRTCs and connectors and try to put together an initial setup.
1272 * At the moment, this is a cloned configuration across all heads with
1273 * a new framebuffer object as the backing store.
1276 * Zero if everything went ok, nonzero otherwise.
1278 bool drm_fb_helper_initial_config(struct drm_fb_helper
*fb_helper
, int bpp_sel
)
1280 struct drm_device
*dev
= fb_helper
->dev
;
1283 /* disable all the possible outputs/crtcs before entering KMS mode */
1284 drm_helper_disable_unused_functions(fb_helper
->dev
);
1286 drm_fb_helper_parse_command_line(fb_helper
);
1288 count
= drm_fb_helper_probe_connector_modes(fb_helper
,
1289 dev
->mode_config
.max_width
,
1290 dev
->mode_config
.max_height
);
1292 * we shouldn't end up with no modes here.
1295 if (fb_helper
->poll_enabled
) {
1296 delayed_slow_work_enqueue(&fb_helper
->output_status_change_slow_work
,
1298 printk(KERN_INFO
"No connectors reported connected with modes - started polling\n");
1300 printk(KERN_INFO
"No connectors reported connected with modes\n");
1302 drm_setup_crtcs(fb_helper
);
1304 return drm_fb_helper_single_fb_probe(fb_helper
, bpp_sel
);
1306 EXPORT_SYMBOL(drm_fb_helper_initial_config
);
1308 /* we got a hotplug irq - need to update fbcon */
1309 void drm_helper_fb_hpd_irq_event(struct drm_fb_helper
*fb_helper
)
1311 /* if we don't have the fbdev registered yet do nothing */
1312 if (!fb_helper
->fbdev
)
1315 /* schedule a slow work asap */
1316 delayed_slow_work_enqueue(&fb_helper
->output_status_change_slow_work
, 0);
1318 EXPORT_SYMBOL(drm_helper_fb_hpd_irq_event
);
1320 bool drm_helper_fb_hotplug_event(struct drm_fb_helper
*fb_helper
, bool polled
)
1324 u32 max_width
, max_height
, bpp_sel
;
1328 DRM_DEBUG_KMS("\n");
1330 max_width
= fb_helper
->fb
->width
;
1331 max_height
= fb_helper
->fb
->height
;
1332 bpp_sel
= fb_helper
->fb
->bits_per_pixel
;
1334 count
= drm_fb_helper_probe_connector_modes(fb_helper
, max_width
,
1336 if (fb_helper
->poll_enabled
&& !polled
) {
1338 delayed_slow_work_cancel(&fb_helper
->output_status_change_slow_work
);
1340 ret
= delayed_slow_work_enqueue(&fb_helper
->output_status_change_slow_work
, 5*HZ
);
1343 drm_setup_crtcs(fb_helper
);
1345 return drm_fb_helper_single_fb_probe(fb_helper
, bpp_sel
);
1347 EXPORT_SYMBOL(drm_helper_fb_hotplug_event
);
1350 * delayed work queue execution function
1351 * - check if fbdev is actually in use on the gpu
1352 * - if not set delayed flag and repoll if necessary
1353 * - check for connector status change
1354 * - repoll if 0 modes found
1355 *- call driver output status changed notifier
1357 static void output_status_change_execute(struct slow_work
*work
)
1359 struct delayed_slow_work
*delayed_work
= container_of(work
, struct delayed_slow_work
, work
);
1360 struct drm_fb_helper
*fb_helper
= container_of(delayed_work
, struct drm_fb_helper
, output_status_change_slow_work
);
1361 struct drm_connector
*connector
;
1362 enum drm_connector_status old_status
, status
;
1363 bool repoll
, changed
= false;
1366 bool bound
= false, crtcs_bound
= false;
1367 struct drm_crtc
*crtc
;
1369 repoll
= fb_helper
->poll_enabled
;
1371 /* first of all check the fbcon framebuffer is actually bound to any crtc */
1372 /* take into account that no crtc at all maybe bound */
1373 list_for_each_entry(crtc
, &fb_helper
->dev
->mode_config
.crtc_list
, head
) {
1376 if (crtc
->fb
== fb_helper
->fb
)
1380 if (bound
== false && crtcs_bound
) {
1381 fb_helper
->delayed_hotplug
= true;
1385 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1386 connector
= fb_helper
->connector_info
[i
]->connector
;
1387 old_status
= connector
->status
;
1388 status
= connector
->funcs
->detect(connector
);
1389 if (old_status
!= status
) {
1392 if (status
== connector_status_connected
&& repoll
) {
1393 DRM_DEBUG("%s is connected - stop polling\n", drm_get_connector_name(connector
));
1399 if (fb_helper
->funcs
->fb_output_status_changed
)
1400 fb_helper
->funcs
->fb_output_status_changed(fb_helper
);
1405 ret
= delayed_slow_work_enqueue(delayed_work
, 5*HZ
);
1407 DRM_ERROR("delayed enqueue failed %d\n", ret
);
1411 static struct slow_work_ops output_status_change_ops
= {
1412 .execute
= output_status_change_execute
,