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 <linux/kernel.h>
33 #include <linux/module.h>
35 #include <drm/drm_crtc.h>
36 #include <drm/drm_fb_helper.h>
37 #include <drm/drm_crtc_helper.h>
39 static LINUX_LIST_HEAD(kernel_fb_helper_list
);
44 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
45 * mode setting driver. They can be used mostly independently from the crtc
46 * helper functions used by many drivers to implement the kernel mode setting
49 * Initialization is done as a three-step process with drm_fb_helper_init(),
50 * drm_fb_helper_single_add_all_connectors() and drm_fb_helper_initial_config().
51 * Drivers with fancier requirements than the default behaviour can override the
52 * second step with their own code. Teardown is done with drm_fb_helper_fini().
54 * At runtime drivers should restore the fbdev console by calling
55 * drm_fb_helper_restore_fbdev_mode() from their ->lastclose callback. They
56 * should also notify the fb helper code from updates to the output
57 * configuration by calling drm_fb_helper_hotplug_event(). For easier
58 * integration with the output polling code in drm_crtc_helper.c the modeset
59 * code provides a ->output_poll_changed callback.
61 * All other functions exported by the fb helper library can be used to
62 * implement the fbdev driver interface by the driver.
66 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
68 * @fb_helper: fbdev initialized with drm_fb_helper_init
70 * This functions adds all the available connectors for use with the given
71 * fb_helper. This is a separate step to allow drivers to freely assign
72 * connectors to the fbdev, e.g. if some are reserved for special purposes or
73 * not adequate to be used for the fbcon.
75 * Since this is part of the initial setup before the fbdev is published, no
76 * locking is required.
78 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper
*fb_helper
)
80 struct drm_device
*dev
= fb_helper
->dev
;
81 struct drm_connector
*connector
;
84 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
85 struct drm_fb_helper_connector
*fb_helper_connector
;
87 fb_helper_connector
= kzalloc(sizeof(struct drm_fb_helper_connector
), GFP_KERNEL
);
88 if (!fb_helper_connector
)
91 fb_helper_connector
->connector
= connector
;
92 fb_helper
->connector_info
[fb_helper
->connector_count
++] = fb_helper_connector
;
96 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
97 kfree(fb_helper
->connector_info
[i
]);
98 fb_helper
->connector_info
[i
] = NULL
;
100 fb_helper
->connector_count
= 0;
103 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors
);
105 static int drm_fb_helper_parse_command_line(struct drm_fb_helper
*fb_helper
)
107 struct drm_fb_helper_connector
*fb_helper_conn
;
110 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
111 struct drm_cmdline_mode
*mode
;
112 struct drm_connector
*connector
;
115 fb_helper_conn
= fb_helper
->connector_info
[i
];
116 connector
= fb_helper_conn
->connector
;
117 mode
= &fb_helper_conn
->cmdline_mode
;
119 /* do something on return - turn off connector maybe */
120 if (fb_get_options(connector
->name
, &option
))
123 if (drm_mode_parse_command_line_for_connector(option
,
128 switch (mode
->force
) {
132 case DRM_FORCE_ON_DIGITAL
:
141 DRM_INFO("forcing %s connector %s\n",
143 connector
->force
= mode
->force
;
146 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
148 mode
->xres
, mode
->yres
,
149 mode
->refresh_specified
? mode
->refresh
: 60,
150 mode
->rb
? " reduced blanking" : "",
151 mode
->margins
? " with margins" : "",
152 mode
->interlace
? " interlaced" : "");
160 static void drm_fb_helper_save_lut_atomic(struct drm_crtc
*crtc
, struct drm_fb_helper
*helper
)
162 uint16_t *r_base
, *g_base
, *b_base
;
165 if (helper
->funcs
->gamma_get
== NULL
)
168 r_base
= crtc
->gamma_store
;
169 g_base
= r_base
+ crtc
->gamma_size
;
170 b_base
= g_base
+ crtc
->gamma_size
;
172 for (i
= 0; i
< crtc
->gamma_size
; i
++)
173 helper
->funcs
->gamma_get(crtc
, &r_base
[i
], &g_base
[i
], &b_base
[i
], i
);
176 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc
*crtc
)
178 uint16_t *r_base
, *g_base
, *b_base
;
180 if (crtc
->funcs
->gamma_set
== NULL
)
183 r_base
= crtc
->gamma_store
;
184 g_base
= r_base
+ crtc
->gamma_size
;
185 b_base
= g_base
+ crtc
->gamma_size
;
187 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, 0, crtc
->gamma_size
);
191 * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
192 * @info: fbdev registered by the helper
194 int drm_fb_helper_debug_enter(struct fb_info
*info
)
196 struct drm_fb_helper
*helper
= info
->par
;
197 struct drm_crtc_helper_funcs
*funcs
;
200 if (list_empty(&kernel_fb_helper_list
))
203 list_for_each_entry(helper
, &kernel_fb_helper_list
, kernel_fb_list
) {
204 for (i
= 0; i
< helper
->crtc_count
; i
++) {
205 struct drm_mode_set
*mode_set
=
206 &helper
->crtc_info
[i
].mode_set
;
208 if (!mode_set
->crtc
->enabled
)
211 funcs
= mode_set
->crtc
->helper_private
;
212 drm_fb_helper_save_lut_atomic(mode_set
->crtc
, helper
);
213 funcs
->mode_set_base_atomic(mode_set
->crtc
,
217 ENTER_ATOMIC_MODE_SET
);
223 EXPORT_SYMBOL(drm_fb_helper_debug_enter
);
225 /* Find the real fb for a given fb helper CRTC */
226 static struct drm_framebuffer
*drm_mode_config_fb(struct drm_crtc
*crtc
)
228 struct drm_device
*dev
= crtc
->dev
;
231 list_for_each_entry(c
, &dev
->mode_config
.crtc_list
, head
) {
232 if (crtc
->base
.id
== c
->base
.id
)
233 return c
->primary
->fb
;
240 * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
241 * @info: fbdev registered by the helper
243 int drm_fb_helper_debug_leave(struct fb_info
*info
)
245 struct drm_fb_helper
*helper
= info
->par
;
246 struct drm_crtc
*crtc
;
247 struct drm_crtc_helper_funcs
*funcs
;
248 struct drm_framebuffer
*fb
;
251 for (i
= 0; i
< helper
->crtc_count
; i
++) {
252 struct drm_mode_set
*mode_set
= &helper
->crtc_info
[i
].mode_set
;
253 crtc
= mode_set
->crtc
;
254 funcs
= crtc
->helper_private
;
255 fb
= drm_mode_config_fb(crtc
);
261 DRM_ERROR("no fb to restore??\n");
265 drm_fb_helper_restore_lut_atomic(mode_set
->crtc
);
266 funcs
->mode_set_base_atomic(mode_set
->crtc
, fb
, crtc
->x
,
267 crtc
->y
, LEAVE_ATOMIC_MODE_SET
);
272 EXPORT_SYMBOL(drm_fb_helper_debug_leave
);
275 static bool restore_fbdev_mode(struct drm_fb_helper
*fb_helper
)
277 struct drm_device
*dev
= fb_helper
->dev
;
278 struct drm_plane
*plane
;
282 drm_warn_on_modeset_not_all_locked(dev
);
284 list_for_each_entry(plane
, &dev
->mode_config
.plane_list
, head
)
285 if (plane
->type
!= DRM_PLANE_TYPE_PRIMARY
)
286 drm_plane_force_disable(plane
);
288 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
289 struct drm_mode_set
*mode_set
= &fb_helper
->crtc_info
[i
].mode_set
;
290 struct drm_crtc
*crtc
= mode_set
->crtc
;
293 if (crtc
->funcs
->cursor_set
) {
294 ret
= crtc
->funcs
->cursor_set(crtc
, NULL
, 0, 0, 0);
299 ret
= drm_mode_set_config_internal(mode_set
);
306 * drm_fb_helper_restore_fbdev_mode - restore fbdev configuration
307 * @fb_helper: fbcon to restore
309 * This should be called from driver's drm ->lastclose callback
310 * when implementing an fbcon on top of kms using this helper. This ensures that
311 * the user isn't greeted with a black screen when e.g. X dies.
313 * Use this variant if you need to bypass locking (panic), or already
314 * hold all modeset locks. Otherwise use drm_fb_helper_restore_fbdev_mode_unlocked()
316 static bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper
*fb_helper
)
318 return restore_fbdev_mode(fb_helper
);
322 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
323 * @fb_helper: fbcon to restore
325 * This should be called from driver's drm ->lastclose callback
326 * when implementing an fbcon on top of kms using this helper. This ensures that
327 * the user isn't greeted with a black screen when e.g. X dies.
329 bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper
*fb_helper
)
331 struct drm_device
*dev
= fb_helper
->dev
;
333 drm_modeset_lock_all(dev
);
334 ret
= restore_fbdev_mode(fb_helper
);
335 drm_modeset_unlock_all(dev
);
338 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked
);
342 * restore fbcon display for all kms driver's using this helper, used for sysrq
343 * and panic handling.
345 static bool drm_fb_helper_force_kernel_mode(void)
347 bool ret
, error
= false;
348 struct drm_fb_helper
*helper
;
350 if (list_empty(&kernel_fb_helper_list
))
353 list_for_each_entry(helper
, &kernel_fb_helper_list
, kernel_fb_list
) {
354 struct drm_device
*dev
= helper
->dev
;
356 if (dev
->switch_power_state
== DRM_SWITCH_POWER_OFF
)
359 /* NOTE: we use lockless flag below to avoid grabbing other
360 * modeset locks. So just trylock the underlying mutex
363 if (!mutex_trylock(&dev
->mode_config
.mutex
)) {
368 ret
= drm_fb_helper_restore_fbdev_mode(helper
);
372 mutex_unlock(&dev
->mode_config
.mutex
);
377 static int drm_fb_helper_panic(struct notifier_block
*n
, unsigned long ununsed
,
381 * It's a waste of time and effort to switch back to text console
382 * if the kernel should reboot before panic messages can be seen.
384 if (panic_timeout
< 0)
387 pr_err("panic occurred, switching back to text console\n");
388 return drm_fb_helper_force_kernel_mode();
391 static struct notifier_block paniced
= {
392 .notifier_call
= drm_fb_helper_panic
,
396 static bool drm_fb_helper_is_bound(struct drm_fb_helper
*fb_helper
)
398 struct drm_device
*dev
= fb_helper
->dev
;
399 struct drm_crtc
*crtc
;
400 int bound
= 0, crtcs_bound
= 0;
403 /* Sometimes user space wants everything disabled, so don't steal the
404 * display if there's a master. */
405 if (dev
->primary
->master
)
409 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
410 if (crtc
->primary
->fb
)
412 if (crtc
->primary
->fb
== fb_helper
->fb
)
416 if (bound
< crtcs_bound
)
423 #ifdef CONFIG_MAGIC_SYSRQ
424 static void drm_fb_helper_restore_work_fn(struct work_struct
*ignored
)
427 ret
= drm_fb_helper_force_kernel_mode();
429 DRM_ERROR("Failed to restore crtc configuration\n");
431 static DECLARE_WORK(drm_fb_helper_restore_work
, drm_fb_helper_restore_work_fn
);
433 static void drm_fb_helper_sysrq(int dummy1
)
435 schedule_work(&drm_fb_helper_restore_work
);
438 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op
= {
439 .handler
= drm_fb_helper_sysrq
,
440 .help_msg
= "force-fb(V)",
441 .action_msg
= "Restore framebuffer console",
444 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op
= { };
447 static void drm_fb_helper_dpms(struct fb_info
*info
, int dpms_mode
)
449 struct drm_fb_helper
*fb_helper
= info
->par
;
450 struct drm_device
*dev
= fb_helper
->dev
;
451 struct drm_crtc
*crtc
;
452 struct drm_connector
*connector
;
456 * fbdev->blank can be called from irq context in case of a panic.
457 * Since we already have our own special panic handler which will
458 * restore the fbdev console mode completely, just bail out early.
460 if (oops_in_progress
)
464 * For each CRTC in this fb, turn the connectors on/off.
466 drm_modeset_lock_all(dev
);
467 if (!drm_fb_helper_is_bound(fb_helper
)) {
468 drm_modeset_unlock_all(dev
);
472 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
473 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
478 /* Walk the connectors & encoders on this fb turning them on/off */
479 for (j
= 0; j
< fb_helper
->connector_count
; j
++) {
480 connector
= fb_helper
->connector_info
[j
]->connector
;
481 connector
->funcs
->dpms(connector
, dpms_mode
);
482 drm_object_property_set_value(&connector
->base
,
483 dev
->mode_config
.dpms_property
, dpms_mode
);
486 drm_modeset_unlock_all(dev
);
490 * drm_fb_helper_blank - implementation for ->fb_blank
491 * @blank: desired blanking state
492 * @info: fbdev registered by the helper
494 int drm_fb_helper_blank(int blank
, struct fb_info
*info
)
497 /* Display: On; HSync: On, VSync: On */
498 case FB_BLANK_UNBLANK
:
499 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_ON
);
501 /* Display: Off; HSync: On, VSync: On */
502 case FB_BLANK_NORMAL
:
503 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_STANDBY
);
505 /* Display: Off; HSync: Off, VSync: On */
506 case FB_BLANK_HSYNC_SUSPEND
:
507 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_STANDBY
);
509 /* Display: Off; HSync: On, VSync: Off */
510 case FB_BLANK_VSYNC_SUSPEND
:
511 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_SUSPEND
);
513 /* Display: Off; HSync: Off, VSync: Off */
514 case FB_BLANK_POWERDOWN
:
515 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_OFF
);
520 EXPORT_SYMBOL(drm_fb_helper_blank
);
523 static void drm_fb_helper_crtc_free(struct drm_fb_helper
*helper
)
527 for (i
= 0; i
< helper
->connector_count
; i
++)
528 kfree(helper
->connector_info
[i
]);
529 kfree(helper
->connector_info
);
530 for (i
= 0; i
< helper
->crtc_count
; i
++) {
531 kfree(helper
->crtc_info
[i
].mode_set
.connectors
);
532 if (helper
->crtc_info
[i
].mode_set
.mode
)
533 drm_mode_destroy(helper
->dev
, helper
->crtc_info
[i
].mode_set
.mode
);
535 kfree(helper
->crtc_info
);
539 * drm_fb_helper_init - initialize a drm_fb_helper structure
541 * @fb_helper: driver-allocated fbdev helper structure to initialize
542 * @crtc_count: maximum number of crtcs to support in this fbdev emulation
543 * @max_conn_count: max connector count
545 * This allocates the structures for the fbdev helper with the given limits.
546 * Note that this won't yet touch the hardware (through the driver interfaces)
547 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
548 * to allow driver writes more control over the exact init sequence.
550 * Drivers must set fb_helper->funcs before calling
551 * drm_fb_helper_initial_config().
554 * Zero if everything went ok, nonzero otherwise.
556 int drm_fb_helper_init(struct drm_device
*dev
,
557 struct drm_fb_helper
*fb_helper
,
558 int crtc_count
, int max_conn_count
)
560 struct drm_crtc
*crtc
;
566 fb_helper
->dev
= dev
;
568 INIT_LIST_HEAD(&fb_helper
->kernel_fb_list
);
570 fb_helper
->crtc_info
= kcalloc(crtc_count
, sizeof(struct drm_fb_helper_crtc
), GFP_KERNEL
);
571 if (!fb_helper
->crtc_info
)
574 fb_helper
->crtc_count
= crtc_count
;
575 fb_helper
->connector_info
= kcalloc(dev
->mode_config
.num_connector
, sizeof(struct drm_fb_helper_connector
*), GFP_KERNEL
);
576 if (!fb_helper
->connector_info
) {
577 kfree(fb_helper
->crtc_info
);
580 fb_helper
->connector_count
= 0;
582 for (i
= 0; i
< crtc_count
; i
++) {
583 fb_helper
->crtc_info
[i
].mode_set
.connectors
=
584 kcalloc(max_conn_count
,
585 sizeof(struct drm_connector
*),
588 if (!fb_helper
->crtc_info
[i
].mode_set
.connectors
)
590 fb_helper
->crtc_info
[i
].mode_set
.num_connectors
= 0;
594 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
595 fb_helper
->crtc_info
[i
].mode_set
.crtc
= crtc
;
601 drm_fb_helper_crtc_free(fb_helper
);
604 EXPORT_SYMBOL(drm_fb_helper_init
);
606 void drm_fb_helper_fini(struct drm_fb_helper
*fb_helper
)
608 if (!list_empty(&fb_helper
->kernel_fb_list
)) {
609 list_del(&fb_helper
->kernel_fb_list
);
610 if (list_empty(&kernel_fb_helper_list
)) {
612 pr_info("drm: unregistered panic notifier\n");
613 atomic_notifier_chain_unregister(&panic_notifier_list
,
615 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op
);
620 drm_fb_helper_crtc_free(fb_helper
);
623 EXPORT_SYMBOL(drm_fb_helper_fini
);
626 static int setcolreg(struct drm_crtc
*crtc
, u16 red
, u16 green
,
627 u16 blue
, u16 regno
, struct fb_info
*info
)
629 struct drm_fb_helper
*fb_helper
= info
->par
;
630 struct drm_framebuffer
*fb
= fb_helper
->fb
;
633 if (info
->fix
.visual
== FB_VISUAL_TRUECOLOR
) {
636 /* place color in psuedopalette */
639 palette
= (u32
*)info
->pseudo_palette
;
640 red
>>= (16 - info
->var
.red
.length
);
641 green
>>= (16 - info
->var
.green
.length
);
642 blue
>>= (16 - info
->var
.blue
.length
);
643 value
= (red
<< info
->var
.red
.offset
) |
644 (green
<< info
->var
.green
.offset
) |
645 (blue
<< info
->var
.blue
.offset
);
646 if (info
->var
.transp
.length
> 0) {
647 u32 mask
= (1 << info
->var
.transp
.length
) - 1;
648 mask
<<= info
->var
.transp
.offset
;
651 palette
[regno
] = value
;
656 * The driver really shouldn't advertise pseudo/directcolor
657 * visuals if it can't deal with the palette.
659 if (WARN_ON(!fb_helper
->funcs
->gamma_set
||
660 !fb_helper
->funcs
->gamma_get
))
665 if (fb
->bits_per_pixel
== 16) {
668 if (fb
->depth
== 16 && regno
> 63)
670 if (fb
->depth
== 15 && regno
> 31)
673 if (fb
->depth
== 16) {
677 for (i
= 0; i
< 8; i
++)
678 fb_helper
->funcs
->gamma_set(crtc
, red
,
679 green
, blue
, pindex
+ i
);
682 fb_helper
->funcs
->gamma_get(crtc
, &r
,
686 for (i
= 0; i
< 4; i
++)
687 fb_helper
->funcs
->gamma_set(crtc
, r
,
694 fb_helper
->funcs
->gamma_set(crtc
, red
, green
, blue
, pindex
);
699 * drm_fb_helper_setcmap - implementation for ->fb_setcmap
701 * @info: fbdev registered by the helper
703 int drm_fb_helper_setcmap(struct fb_cmap
*cmap
, struct fb_info
*info
)
705 struct drm_fb_helper
*fb_helper
= info
->par
;
706 struct drm_device
*dev
= fb_helper
->dev
;
707 struct drm_crtc_helper_funcs
*crtc_funcs
;
708 u16
*red
, *green
, *blue
, *transp
;
709 struct drm_crtc
*crtc
;
713 drm_modeset_lock_all(dev
);
714 if (!drm_fb_helper_is_bound(fb_helper
)) {
715 drm_modeset_unlock_all(dev
);
719 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
720 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
721 crtc_funcs
= crtc
->helper_private
;
726 transp
= cmap
->transp
;
729 for (j
= 0; j
< cmap
->len
; j
++) {
730 u16 hred
, hgreen
, hblue
, htransp
= 0xffff;
739 rc
= setcolreg(crtc
, hred
, hgreen
, hblue
, start
++, info
);
743 if (crtc_funcs
->load_lut
)
744 crtc_funcs
->load_lut(crtc
);
747 drm_modeset_unlock_all(dev
);
750 EXPORT_SYMBOL(drm_fb_helper_setcmap
);
753 * drm_fb_helper_check_var - implementation for ->fb_check_var
754 * @var: screeninfo to check
755 * @info: fbdev registered by the helper
757 int drm_fb_helper_check_var(struct fb_var_screeninfo
*var
,
758 struct fb_info
*info
)
760 struct drm_fb_helper
*fb_helper
= info
->par
;
761 struct drm_framebuffer
*fb
= fb_helper
->fb
;
764 if (var
->pixclock
!= 0 || in_dbg_master())
767 /* Need to resize the fb object !!! */
768 if (var
->bits_per_pixel
> fb
->bits_per_pixel
||
769 var
->xres
> fb
->width
|| var
->yres
> fb
->height
||
770 var
->xres_virtual
> fb
->width
|| var
->yres_virtual
> fb
->height
) {
771 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
772 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
773 var
->xres
, var
->yres
, var
->bits_per_pixel
,
774 var
->xres_virtual
, var
->yres_virtual
,
775 fb
->width
, fb
->height
, fb
->bits_per_pixel
);
779 switch (var
->bits_per_pixel
) {
781 depth
= (var
->green
.length
== 6) ? 16 : 15;
784 depth
= (var
->transp
.length
> 0) ? 32 : 24;
787 depth
= var
->bits_per_pixel
;
794 var
->green
.offset
= 0;
795 var
->blue
.offset
= 0;
797 var
->green
.length
= 8;
798 var
->blue
.length
= 8;
799 var
->transp
.length
= 0;
800 var
->transp
.offset
= 0;
803 var
->red
.offset
= 10;
804 var
->green
.offset
= 5;
805 var
->blue
.offset
= 0;
807 var
->green
.length
= 5;
808 var
->blue
.length
= 5;
809 var
->transp
.length
= 1;
810 var
->transp
.offset
= 15;
813 var
->red
.offset
= 11;
814 var
->green
.offset
= 5;
815 var
->blue
.offset
= 0;
817 var
->green
.length
= 6;
818 var
->blue
.length
= 5;
819 var
->transp
.length
= 0;
820 var
->transp
.offset
= 0;
823 var
->red
.offset
= 16;
824 var
->green
.offset
= 8;
825 var
->blue
.offset
= 0;
827 var
->green
.length
= 8;
828 var
->blue
.length
= 8;
829 var
->transp
.length
= 0;
830 var
->transp
.offset
= 0;
833 var
->red
.offset
= 16;
834 var
->green
.offset
= 8;
835 var
->blue
.offset
= 0;
837 var
->green
.length
= 8;
838 var
->blue
.length
= 8;
839 var
->transp
.length
= 8;
840 var
->transp
.offset
= 24;
847 EXPORT_SYMBOL(drm_fb_helper_check_var
);
850 * drm_fb_helper_set_par - implementation for ->fb_set_par
851 * @info: fbdev registered by the helper
853 * This will let fbcon do the mode init and is called at initialization time by
854 * the fbdev core when registering the driver, and later on through the hotplug
857 int drm_fb_helper_set_par(struct fb_info
*info
)
859 struct drm_fb_helper
*fb_helper
= info
->par
;
860 struct fb_var_screeninfo
*var
= &info
->var
;
862 if (var
->pixclock
!= 0) {
863 DRM_ERROR("PIXEL CLOCK SET\n");
867 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper
);
869 if (fb_helper
->delayed_hotplug
) {
870 fb_helper
->delayed_hotplug
= false;
871 drm_fb_helper_hotplug_event(fb_helper
);
875 EXPORT_SYMBOL(drm_fb_helper_set_par
);
878 * drm_fb_helper_pan_display - implementation for ->fb_pan_display
879 * @var: updated screen information
880 * @info: fbdev registered by the helper
882 int drm_fb_helper_pan_display(struct fb_var_screeninfo
*var
,
883 struct fb_info
*info
)
885 struct drm_fb_helper
*fb_helper
= info
->par
;
886 struct drm_device
*dev
= fb_helper
->dev
;
887 struct drm_mode_set
*modeset
;
891 drm_modeset_lock_all(dev
);
892 if (!drm_fb_helper_is_bound(fb_helper
)) {
893 drm_modeset_unlock_all(dev
);
897 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
898 modeset
= &fb_helper
->crtc_info
[i
].mode_set
;
900 modeset
->x
= var
->xoffset
;
901 modeset
->y
= var
->yoffset
;
903 if (modeset
->num_connectors
) {
904 ret
= drm_mode_set_config_internal(modeset
);
906 info
->var
.xoffset
= var
->xoffset
;
907 info
->var
.yoffset
= var
->yoffset
;
911 drm_modeset_unlock_all(dev
);
914 EXPORT_SYMBOL(drm_fb_helper_pan_display
);
917 /* XXX: DragonFly-specific */
919 do_restore_fbdev_mode(void *context
, int pending
)
921 struct drm_fb_helper
*fb_helper
= context
;
922 struct drm_device
*dev
= fb_helper
->dev
;
927 drm_modeset_lock_all(dev
);
928 drm_fb_helper_restore_fbdev_mode(fb_helper
);
929 drm_modeset_unlock_all(dev
);
932 /* XXX: DragonFly-specific */
934 sc_restore_fbdev_mode(void *cookie
)
936 struct drm_fb_helper
*fb_helper
= cookie
;
941 taskqueue_enqueue(taskqueue_thread
[0], &fb_helper
->fb_mode_task
);
945 * Allocates the backing storage and sets up the fbdev info structure through
946 * the ->fb_probe callback and then registers the fbdev and sets up the panic
949 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper
*fb_helper
,
955 struct fb_info
*info
;
956 struct drm_fb_helper_surface_size sizes
;
958 int kms_console
= 1; /* XXX: DragonFly-specific */
960 memset(&sizes
, 0, sizeof(struct drm_fb_helper_surface_size
));
961 sizes
.surface_depth
= 24;
962 sizes
.surface_bpp
= 32;
963 sizes
.fb_width
= (unsigned)-1;
964 sizes
.fb_height
= (unsigned)-1;
966 /* if driver picks 8 or 16 by default use that
967 for both depth/bpp */
968 if (preferred_bpp
!= sizes
.surface_bpp
)
969 sizes
.surface_depth
= sizes
.surface_bpp
= preferred_bpp
;
971 /* first up get a count of crtcs now in use and new min/maxes width/heights */
972 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
973 struct drm_fb_helper_connector
*fb_helper_conn
= fb_helper
->connector_info
[i
];
974 struct drm_cmdline_mode
*cmdline_mode
;
976 cmdline_mode
= &fb_helper_conn
->cmdline_mode
;
978 if (cmdline_mode
->bpp_specified
) {
979 switch (cmdline_mode
->bpp
) {
981 sizes
.surface_depth
= sizes
.surface_bpp
= 8;
984 sizes
.surface_depth
= 15;
985 sizes
.surface_bpp
= 16;
988 sizes
.surface_depth
= sizes
.surface_bpp
= 16;
991 sizes
.surface_depth
= sizes
.surface_bpp
= 24;
994 sizes
.surface_depth
= 24;
995 sizes
.surface_bpp
= 32;
1003 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1004 struct drm_display_mode
*desired_mode
;
1005 desired_mode
= fb_helper
->crtc_info
[i
].desired_mode
;
1008 if (gamma_size
== 0)
1009 gamma_size
= fb_helper
->crtc_info
[i
].mode_set
.crtc
->gamma_size
;
1010 if (desired_mode
->hdisplay
< sizes
.fb_width
)
1011 sizes
.fb_width
= desired_mode
->hdisplay
;
1012 if (desired_mode
->vdisplay
< sizes
.fb_height
)
1013 sizes
.fb_height
= desired_mode
->vdisplay
;
1014 if (desired_mode
->hdisplay
> sizes
.surface_width
)
1015 sizes
.surface_width
= desired_mode
->hdisplay
;
1016 if (desired_mode
->vdisplay
> sizes
.surface_height
)
1017 sizes
.surface_height
= desired_mode
->vdisplay
;
1022 if (crtc_count
== 0 || sizes
.fb_width
== -1 || sizes
.fb_height
== -1) {
1023 /* hmm everyone went away - assume VGA cable just fell out
1024 and will come back later. */
1025 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
1026 sizes
.fb_width
= sizes
.surface_width
= 1024;
1027 sizes
.fb_height
= sizes
.surface_height
= 768;
1030 /* push down into drivers */
1031 ret
= (*fb_helper
->funcs
->fb_probe
)(fb_helper
, &sizes
);
1035 info
= fb_helper
->fbdev
;
1038 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1039 * events, but at init time drm_setup_crtcs needs to be called before
1040 * the fb is allocated (since we need to figure out the desired size of
1041 * the fb before we can allocate it ...). Hence we need to fix things up
1044 for (i
= 0; i
< fb_helper
->crtc_count
; i
++)
1045 if (fb_helper
->crtc_info
[i
].mode_set
.num_connectors
)
1046 fb_helper
->crtc_info
[i
].mode_set
.fb
= fb_helper
->fb
;
1049 /* XXX: DragonFly-specific */
1050 TUNABLE_INT_FETCH("kern.kms_console", &kms_console
);
1052 TASK_INIT(&fb_helper
->fb_mode_task
, 0, do_restore_fbdev_mode
,
1054 info
->cookie
= fb_helper
;
1055 info
->restore
= (void *)&sc_restore_fbdev_mode
;
1056 if (register_framebuffer(info
) < 0)
1061 info
->var
.pixclock
= 0;
1062 if (register_framebuffer(info
) < 0)
1065 dev_info(fb_helper
->dev
->dev
, "fb%d: %s frame buffer device\n",
1066 info
->node
, info
->fix
.id
);
1068 /* Switch back to kernel console on panic */
1069 /* multi card linked list maybe */
1070 if (list_empty(&kernel_fb_helper_list
)) {
1071 dev_info(fb_helper
->dev
->dev
, "registered panic notifier\n");
1072 atomic_notifier_chain_register(&panic_notifier_list
,
1074 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op
);
1077 list_add(&fb_helper
->kernel_fb_list
, &kernel_fb_helper_list
);
1085 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1086 * @info: fbdev registered by the helper
1087 * @pitch: desired pitch
1088 * @depth: desired depth
1090 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1091 * fbdev emulations. Drivers which support acceleration methods which impose
1092 * additional constraints need to set up their own limits.
1094 * Drivers should call this (or their equivalent setup code) from their
1095 * ->fb_probe callback.
1097 void drm_fb_helper_fill_fix(struct fb_info
*info
, uint32_t pitch
,
1100 info
->fix
.type
= FB_TYPE_PACKED_PIXELS
;
1101 info
->fix
.visual
= depth
== 8 ? FB_VISUAL_PSEUDOCOLOR
:
1102 FB_VISUAL_TRUECOLOR
;
1103 info
->fix
.mmio_start
= 0;
1104 info
->fix
.mmio_len
= 0;
1105 info
->fix
.type_aux
= 0;
1106 info
->fix
.xpanstep
= 1; /* doing it in hw */
1107 info
->fix
.ypanstep
= 1; /* doing it in hw */
1108 info
->fix
.ywrapstep
= 0;
1109 info
->fix
.accel
= FB_ACCEL_NONE
;
1110 info
->fix
.type_aux
= 0;
1112 info
->fix
.line_length
= pitch
;
1115 EXPORT_SYMBOL(drm_fb_helper_fill_fix
);
1118 * drm_fb_helper_fill_var - initalizes variable fbdev information
1119 * @info: fbdev instance to set up
1120 * @fb_helper: fb helper instance to use as template
1121 * @fb_width: desired fb width
1122 * @fb_height: desired fb height
1124 * Sets up the variable fbdev metainformation from the given fb helper instance
1125 * and the drm framebuffer allocated in fb_helper->fb.
1127 * Drivers should call this (or their equivalent setup code) from their
1128 * ->fb_probe callback after having allocated the fbdev backing
1129 * storage framebuffer.
1131 void drm_fb_helper_fill_var(struct fb_info
*info
, struct drm_fb_helper
*fb_helper
,
1132 uint32_t fb_width
, uint32_t fb_height
)
1134 struct drm_framebuffer
*fb
= fb_helper
->fb
;
1135 info
->pseudo_palette
= fb_helper
->pseudo_palette
;
1136 info
->var
.xres_virtual
= fb
->width
;
1137 info
->var
.yres_virtual
= fb
->height
;
1138 info
->var
.bits_per_pixel
= fb
->bits_per_pixel
;
1139 info
->var
.accel_flags
= FB_ACCELF_TEXT
;
1140 info
->var
.xoffset
= 0;
1141 info
->var
.yoffset
= 0;
1142 info
->var
.activate
= FB_ACTIVATE_NOW
;
1143 info
->var
.height
= -1;
1144 info
->var
.width
= -1;
1146 switch (fb
->depth
) {
1148 info
->var
.red
.offset
= 0;
1149 info
->var
.green
.offset
= 0;
1150 info
->var
.blue
.offset
= 0;
1151 info
->var
.red
.length
= 8; /* 8bit DAC */
1152 info
->var
.green
.length
= 8;
1153 info
->var
.blue
.length
= 8;
1154 info
->var
.transp
.offset
= 0;
1155 info
->var
.transp
.length
= 0;
1158 info
->var
.red
.offset
= 10;
1159 info
->var
.green
.offset
= 5;
1160 info
->var
.blue
.offset
= 0;
1161 info
->var
.red
.length
= 5;
1162 info
->var
.green
.length
= 5;
1163 info
->var
.blue
.length
= 5;
1164 info
->var
.transp
.offset
= 15;
1165 info
->var
.transp
.length
= 1;
1168 info
->var
.red
.offset
= 11;
1169 info
->var
.green
.offset
= 5;
1170 info
->var
.blue
.offset
= 0;
1171 info
->var
.red
.length
= 5;
1172 info
->var
.green
.length
= 6;
1173 info
->var
.blue
.length
= 5;
1174 info
->var
.transp
.offset
= 0;
1177 info
->var
.red
.offset
= 16;
1178 info
->var
.green
.offset
= 8;
1179 info
->var
.blue
.offset
= 0;
1180 info
->var
.red
.length
= 8;
1181 info
->var
.green
.length
= 8;
1182 info
->var
.blue
.length
= 8;
1183 info
->var
.transp
.offset
= 0;
1184 info
->var
.transp
.length
= 0;
1187 info
->var
.red
.offset
= 16;
1188 info
->var
.green
.offset
= 8;
1189 info
->var
.blue
.offset
= 0;
1190 info
->var
.red
.length
= 8;
1191 info
->var
.green
.length
= 8;
1192 info
->var
.blue
.length
= 8;
1193 info
->var
.transp
.offset
= 24;
1194 info
->var
.transp
.length
= 8;
1200 info
->var
.xres
= fb_width
;
1201 info
->var
.yres
= fb_height
;
1203 EXPORT_SYMBOL(drm_fb_helper_fill_var
);
1206 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper
*fb_helper
,
1210 struct drm_connector
*connector
;
1214 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1215 connector
= fb_helper
->connector_info
[i
]->connector
;
1216 count
+= connector
->funcs
->fill_modes(connector
, maxX
, maxY
);
1222 struct drm_display_mode
*drm_has_preferred_mode(struct drm_fb_helper_connector
*fb_connector
, int width
, int height
)
1224 struct drm_display_mode
*mode
;
1226 list_for_each_entry(mode
, &fb_connector
->connector
->modes
, head
) {
1227 if (mode
->hdisplay
> width
||
1228 mode
->vdisplay
> height
)
1230 if (mode
->type
& DRM_MODE_TYPE_PREFERRED
)
1235 EXPORT_SYMBOL(drm_has_preferred_mode
);
1237 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector
*fb_connector
)
1239 struct drm_cmdline_mode
*cmdline_mode
;
1240 cmdline_mode
= &fb_connector
->cmdline_mode
;
1241 return cmdline_mode
->specified
;
1244 struct drm_display_mode
*drm_pick_cmdline_mode(struct drm_fb_helper_connector
*fb_helper_conn
,
1245 int width
, int height
)
1247 struct drm_cmdline_mode
*cmdline_mode
;
1248 struct drm_display_mode
*mode
= NULL
;
1249 bool prefer_non_interlace
;
1251 cmdline_mode
= &fb_helper_conn
->cmdline_mode
;
1252 if (cmdline_mode
->specified
== false)
1255 /* attempt to find a matching mode in the list of modes
1256 * we have gotten so far, if not add a CVT mode that conforms
1258 if (cmdline_mode
->rb
|| cmdline_mode
->margins
)
1261 prefer_non_interlace
= !cmdline_mode
->interlace
;
1263 list_for_each_entry(mode
, &fb_helper_conn
->connector
->modes
, head
) {
1264 /* check width/height */
1265 if (mode
->hdisplay
!= cmdline_mode
->xres
||
1266 mode
->vdisplay
!= cmdline_mode
->yres
)
1269 if (cmdline_mode
->refresh_specified
) {
1270 if (mode
->vrefresh
!= cmdline_mode
->refresh
)
1274 if (cmdline_mode
->interlace
) {
1275 if (!(mode
->flags
& DRM_MODE_FLAG_INTERLACE
))
1277 } else if (prefer_non_interlace
) {
1278 if (mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
1284 if (prefer_non_interlace
) {
1285 prefer_non_interlace
= false;
1290 mode
= drm_mode_create_from_cmdline_mode(fb_helper_conn
->connector
->dev
,
1292 list_add(&mode
->head
, &fb_helper_conn
->connector
->modes
);
1295 EXPORT_SYMBOL(drm_pick_cmdline_mode
);
1297 static bool drm_connector_enabled(struct drm_connector
*connector
, bool strict
)
1302 enable
= connector
->status
== connector_status_connected
;
1304 enable
= connector
->status
!= connector_status_disconnected
;
1309 static void drm_enable_connectors(struct drm_fb_helper
*fb_helper
,
1312 bool any_enabled
= false;
1313 struct drm_connector
*connector
;
1316 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1317 connector
= fb_helper
->connector_info
[i
]->connector
;
1318 enabled
[i
] = drm_connector_enabled(connector
, true);
1319 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector
->base
.id
,
1320 enabled
[i
] ? "yes" : "no");
1321 any_enabled
|= enabled
[i
];
1327 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1328 connector
= fb_helper
->connector_info
[i
]->connector
;
1329 enabled
[i
] = drm_connector_enabled(connector
, false);
1333 static bool drm_target_cloned(struct drm_fb_helper
*fb_helper
,
1334 struct drm_display_mode
**modes
,
1335 bool *enabled
, int width
, int height
)
1338 bool can_clone
= false;
1339 struct drm_fb_helper_connector
*fb_helper_conn
;
1340 struct drm_display_mode
*dmt_mode
, *mode
;
1342 /* only contemplate cloning in the single crtc case */
1343 if (fb_helper
->crtc_count
> 1)
1347 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1352 /* only contemplate cloning if more than one connector is enabled */
1356 /* check the command line or if nothing common pick 1024x768 */
1358 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1361 fb_helper_conn
= fb_helper
->connector_info
[i
];
1362 modes
[i
] = drm_pick_cmdline_mode(fb_helper_conn
, width
, height
);
1367 for (j
= 0; j
< i
; j
++) {
1370 if (!drm_mode_equal(modes
[j
], modes
[i
]))
1376 DRM_DEBUG_KMS("can clone using command line\n");
1380 /* try and find a 1024x768 mode on each connector */
1382 dmt_mode
= drm_mode_find_dmt(fb_helper
->dev
, 1024, 768, 60, false);
1384 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1389 fb_helper_conn
= fb_helper
->connector_info
[i
];
1390 list_for_each_entry(mode
, &fb_helper_conn
->connector
->modes
, head
) {
1391 if (drm_mode_equal(mode
, dmt_mode
))
1399 DRM_DEBUG_KMS("can clone using 1024x768\n");
1402 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1406 static bool drm_target_preferred(struct drm_fb_helper
*fb_helper
,
1407 struct drm_display_mode
**modes
,
1408 bool *enabled
, int width
, int height
)
1410 struct drm_fb_helper_connector
*fb_helper_conn
;
1413 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1414 fb_helper_conn
= fb_helper
->connector_info
[i
];
1416 if (enabled
[i
] == false)
1419 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1420 fb_helper_conn
->connector
->base
.id
);
1422 /* got for command line mode first */
1423 modes
[i
] = drm_pick_cmdline_mode(fb_helper_conn
, width
, height
);
1425 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1426 fb_helper_conn
->connector
->base
.id
);
1427 modes
[i
] = drm_has_preferred_mode(fb_helper_conn
, width
, height
);
1429 /* No preferred modes, pick one off the list */
1430 if (!modes
[i
] && !list_empty(&fb_helper_conn
->connector
->modes
)) {
1431 list_for_each_entry(modes
[i
], &fb_helper_conn
->connector
->modes
, head
)
1434 DRM_DEBUG_KMS("found mode %s\n", modes
[i
] ? modes
[i
]->name
:
1440 static int drm_pick_crtcs(struct drm_fb_helper
*fb_helper
,
1441 struct drm_fb_helper_crtc
**best_crtcs
,
1442 struct drm_display_mode
**modes
,
1443 int n
, int width
, int height
)
1446 struct drm_device
*dev
= fb_helper
->dev
;
1447 struct drm_connector
*connector
;
1448 struct drm_connector_helper_funcs
*connector_funcs
;
1449 struct drm_encoder
*encoder
;
1450 int my_score
, best_score
, score
;
1451 struct drm_fb_helper_crtc
**crtcs
, *crtc
;
1452 struct drm_fb_helper_connector
*fb_helper_conn
;
1454 if (n
== fb_helper
->connector_count
)
1457 fb_helper_conn
= fb_helper
->connector_info
[n
];
1458 connector
= fb_helper_conn
->connector
;
1460 best_crtcs
[n
] = NULL
;
1461 best_score
= drm_pick_crtcs(fb_helper
, best_crtcs
, modes
, n
+1, width
, height
);
1462 if (modes
[n
] == NULL
)
1465 crtcs
= kzalloc(dev
->mode_config
.num_connector
*
1466 sizeof(struct drm_fb_helper_crtc
*), GFP_KERNEL
);
1471 if (connector
->status
== connector_status_connected
)
1473 if (drm_has_cmdline_mode(fb_helper_conn
))
1475 if (drm_has_preferred_mode(fb_helper_conn
, width
, height
))
1478 connector_funcs
= connector
->helper_private
;
1479 encoder
= connector_funcs
->best_encoder(connector
);
1483 /* select a crtc for this connector and then attempt to configure
1484 remaining connectors */
1485 for (c
= 0; c
< fb_helper
->crtc_count
; c
++) {
1486 crtc
= &fb_helper
->crtc_info
[c
];
1488 if ((encoder
->possible_crtcs
& (1 << c
)) == 0)
1491 for (o
= 0; o
< n
; o
++)
1492 if (best_crtcs
[o
] == crtc
)
1496 /* ignore cloning unless only a single crtc */
1497 if (fb_helper
->crtc_count
> 1)
1500 if (!drm_mode_equal(modes
[o
], modes
[n
]))
1505 memcpy(crtcs
, best_crtcs
, n
* sizeof(struct drm_fb_helper_crtc
*));
1506 score
= my_score
+ drm_pick_crtcs(fb_helper
, crtcs
, modes
, n
+ 1,
1508 if (score
> best_score
) {
1510 memcpy(best_crtcs
, crtcs
,
1511 dev
->mode_config
.num_connector
*
1512 sizeof(struct drm_fb_helper_crtc
*));
1520 static void drm_setup_crtcs(struct drm_fb_helper
*fb_helper
)
1522 struct drm_device
*dev
= fb_helper
->dev
;
1523 struct drm_fb_helper_crtc
**crtcs
;
1524 struct drm_display_mode
**modes
;
1525 struct drm_mode_set
*modeset
;
1530 DRM_DEBUG_KMS("\n");
1532 width
= dev
->mode_config
.max_width
;
1533 height
= dev
->mode_config
.max_height
;
1535 crtcs
= kcalloc(dev
->mode_config
.num_connector
,
1536 sizeof(struct drm_fb_helper_crtc
*), GFP_KERNEL
);
1537 modes
= kcalloc(dev
->mode_config
.num_connector
,
1538 sizeof(struct drm_display_mode
*), GFP_KERNEL
);
1539 enabled
= kcalloc(dev
->mode_config
.num_connector
,
1540 sizeof(bool), GFP_KERNEL
);
1541 if (!crtcs
|| !modes
|| !enabled
) {
1542 DRM_ERROR("Memory allocation failed\n");
1547 drm_enable_connectors(fb_helper
, enabled
);
1549 if (!(fb_helper
->funcs
->initial_config
&&
1550 fb_helper
->funcs
->initial_config(fb_helper
, crtcs
, modes
,
1551 enabled
, width
, height
))) {
1552 memset(modes
, 0, dev
->mode_config
.num_connector
*sizeof(modes
[0]));
1553 memset(crtcs
, 0, dev
->mode_config
.num_connector
*sizeof(crtcs
[0]));
1555 if (!drm_target_cloned(fb_helper
,
1556 modes
, enabled
, width
, height
) &&
1557 !drm_target_preferred(fb_helper
,
1558 modes
, enabled
, width
, height
))
1559 DRM_ERROR("Unable to find initial modes\n");
1561 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1564 drm_pick_crtcs(fb_helper
, crtcs
, modes
, 0, width
, height
);
1567 /* need to set the modesets up here for use later */
1568 /* fill out the connector<->crtc mappings into the modesets */
1569 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1570 modeset
= &fb_helper
->crtc_info
[i
].mode_set
;
1571 modeset
->num_connectors
= 0;
1575 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1576 struct drm_display_mode
*mode
= modes
[i
];
1577 struct drm_fb_helper_crtc
*fb_crtc
= crtcs
[i
];
1578 modeset
= &fb_crtc
->mode_set
;
1580 if (mode
&& fb_crtc
) {
1581 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1582 mode
->name
, fb_crtc
->mode_set
.crtc
->base
.id
);
1583 fb_crtc
->desired_mode
= mode
;
1585 drm_mode_destroy(dev
, modeset
->mode
);
1586 modeset
->mode
= drm_mode_duplicate(dev
,
1587 fb_crtc
->desired_mode
);
1588 modeset
->connectors
[modeset
->num_connectors
++] = fb_helper
->connector_info
[i
]->connector
;
1589 modeset
->fb
= fb_helper
->fb
;
1593 /* Clear out any old modes if there are no more connected outputs. */
1594 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1595 modeset
= &fb_helper
->crtc_info
[i
].mode_set
;
1596 if (modeset
->num_connectors
== 0) {
1597 BUG_ON(modeset
->fb
);
1598 BUG_ON(modeset
->num_connectors
);
1600 drm_mode_destroy(dev
, modeset
->mode
);
1601 modeset
->mode
= NULL
;
1611 * drm_fb_helper_initial_config - setup a sane initial connector configuration
1612 * @fb_helper: fb_helper device struct
1613 * @bpp_sel: bpp value to use for the framebuffer configuration
1615 * Scans the CRTCs and connectors and tries to put together an initial setup.
1616 * At the moment, this is a cloned configuration across all heads with
1617 * a new framebuffer object as the backing store.
1619 * Note that this also registers the fbdev and so allows userspace to call into
1620 * the driver through the fbdev interfaces.
1622 * This function will call down into the ->fb_probe callback to let
1623 * the driver allocate and initialize the fbdev info structure and the drm
1624 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1625 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1626 * values for the fbdev info structure.
1629 * Zero if everything went ok, nonzero otherwise.
1631 bool drm_fb_helper_initial_config(struct drm_fb_helper
*fb_helper
, int bpp_sel
)
1633 struct drm_device
*dev
= fb_helper
->dev
;
1636 drm_fb_helper_parse_command_line(fb_helper
);
1638 mutex_lock(&dev
->mode_config
.mutex
);
1639 count
= drm_fb_helper_probe_connector_modes(fb_helper
,
1640 dev
->mode_config
.max_width
,
1641 dev
->mode_config
.max_height
);
1642 mutex_unlock(&dev
->mode_config
.mutex
);
1644 * we shouldn't end up with no modes here.
1647 dev_info(fb_helper
->dev
->dev
, "No connectors reported connected with modes\n");
1649 drm_setup_crtcs(fb_helper
);
1651 return drm_fb_helper_single_fb_probe(fb_helper
, bpp_sel
);
1653 EXPORT_SYMBOL(drm_fb_helper_initial_config
);
1656 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1657 * probing all the outputs attached to the fb
1658 * @fb_helper: the drm_fb_helper
1660 * Scan the connectors attached to the fb_helper and try to put together a
1661 * setup after *notification of a change in output configuration.
1663 * Called at runtime, takes the mode config locks to be able to check/change the
1664 * modeset configuration. Must be run from process context (which usually means
1665 * either the output polling work or a work item launched from the driver's
1666 * hotplug interrupt).
1668 * Note that the driver must ensure that this is only called _after_ the fb has
1669 * been fully set up, i.e. after the call to drm_fb_helper_initial_config.
1672 * 0 on success and a non-zero error code otherwise.
1674 int drm_fb_helper_hotplug_event(struct drm_fb_helper
*fb_helper
)
1676 struct drm_device
*dev
= fb_helper
->dev
;
1677 u32 max_width
, max_height
;
1682 mutex_lock(&fb_helper
->dev
->mode_config
.mutex
);
1683 if (!drm_fb_helper_is_bound(fb_helper
)) {
1684 fb_helper
->delayed_hotplug
= true;
1685 mutex_unlock(&fb_helper
->dev
->mode_config
.mutex
);
1688 DRM_DEBUG_KMS("\n");
1690 max_width
= fb_helper
->fb
->width
;
1691 max_height
= fb_helper
->fb
->height
;
1693 drm_fb_helper_probe_connector_modes(fb_helper
, max_width
, max_height
);
1694 mutex_unlock(&fb_helper
->dev
->mode_config
.mutex
);
1696 drm_modeset_lock_all(dev
);
1697 drm_setup_crtcs(fb_helper
);
1698 drm_modeset_unlock_all(dev
);
1700 drm_fb_helper_set_par(fb_helper
->fbdev
);
1705 EXPORT_SYMBOL(drm_fb_helper_hotplug_event
);
1707 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
1708 * but the module doesn't depend on any fb console symbols. At least
1709 * attempt to load fbcon to avoid leaving the system without a usable console.
1711 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
1712 static int __init
drm_fb_helper_modinit(void)
1714 const char *name
= "fbcon";
1715 struct module
*fbcon
;
1717 mutex_lock(&module_mutex
);
1718 fbcon
= find_module(name
);
1719 mutex_unlock(&module_mutex
);
1722 request_module_nowait(name
);
1726 module_init(drm_fb_helper_modinit
);