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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #include <linux/kernel.h>
33 #include <linux/sysrq.h>
34 #include <linux/slab.h>
36 #include <linux/module.h>
38 #include <drm/drm_crtc.h>
39 #include <drm/drm_fb_helper.h>
40 #include <drm/drm_crtc_helper.h>
42 static LIST_HEAD(kernel_fb_helper_list
);
47 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
48 * mode setting driver. They can be used mostly independently from the crtc
49 * helper functions used by many drivers to implement the kernel mode setting
52 * Initialization is done as a four-step process with drm_fb_helper_prepare(),
53 * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
54 * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
55 * default behaviour can override the third step with their own code.
56 * Teardown is done with drm_fb_helper_fini().
58 * At runtime drivers should restore the fbdev console by calling
59 * drm_fb_helper_restore_fbdev_mode() from their ->lastclose callback. They
60 * should also notify the fb helper code from updates to the output
61 * configuration by calling drm_fb_helper_hotplug_event(). For easier
62 * integration with the output polling code in drm_crtc_helper.c the modeset
63 * code provides a ->output_poll_changed callback.
65 * All other functions exported by the fb helper library can be used to
66 * implement the fbdev driver interface by the driver.
68 * It is possible, though perhaps somewhat tricky, to implement race-free
69 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
70 * helper must be called first to initialize the minimum required to make
71 * hotplug detection work. Drivers also need to make sure to properly set up
72 * the dev->mode_config.funcs member. After calling drm_kms_helper_poll_init()
73 * it is safe to enable interrupts and start processing hotplug events. At the
74 * same time, drivers should initialize all modeset objects such as CRTCs,
75 * encoders and connectors. To finish up the fbdev helper initialization, the
76 * drm_fb_helper_init() function is called. To probe for all attached displays
77 * and set up an initial configuration using the detected hardware, drivers
78 * should call drm_fb_helper_single_add_all_connectors() followed by
79 * drm_fb_helper_initial_config().
83 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
85 * @fb_helper: fbdev initialized with drm_fb_helper_init
87 * This functions adds all the available connectors for use with the given
88 * fb_helper. This is a separate step to allow drivers to freely assign
89 * connectors to the fbdev, e.g. if some are reserved for special purposes or
90 * not adequate to be used for the fbcon.
92 * Since this is part of the initial setup before the fbdev is published, no
93 * locking is required.
95 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper
*fb_helper
)
97 struct drm_device
*dev
= fb_helper
->dev
;
98 struct drm_connector
*connector
;
101 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
102 struct drm_fb_helper_connector
*fb_helper_connector
;
104 fb_helper_connector
= kzalloc(sizeof(struct drm_fb_helper_connector
), GFP_KERNEL
);
105 if (!fb_helper_connector
)
108 fb_helper_connector
->connector
= connector
;
109 fb_helper
->connector_info
[fb_helper
->connector_count
++] = fb_helper_connector
;
113 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
114 kfree(fb_helper
->connector_info
[i
]);
115 fb_helper
->connector_info
[i
] = NULL
;
117 fb_helper
->connector_count
= 0;
120 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors
);
122 int drm_fb_helper_add_one_connector(struct drm_fb_helper
*fb_helper
, struct drm_connector
*connector
)
124 struct drm_fb_helper_connector
**temp
;
125 struct drm_fb_helper_connector
*fb_helper_connector
;
127 WARN_ON(!mutex_is_locked(&fb_helper
->dev
->mode_config
.mutex
));
128 if (fb_helper
->connector_count
+ 1 > fb_helper
->connector_info_alloc_count
) {
129 temp
= krealloc(fb_helper
->connector_info
, sizeof(struct drm_fb_helper_connector
*) * (fb_helper
->connector_count
+ 1), GFP_KERNEL
);
133 fb_helper
->connector_info_alloc_count
= fb_helper
->connector_count
+ 1;
134 fb_helper
->connector_info
= temp
;
138 fb_helper_connector
= kzalloc(sizeof(struct drm_fb_helper_connector
), GFP_KERNEL
);
139 if (!fb_helper_connector
)
142 fb_helper_connector
->connector
= connector
;
143 fb_helper
->connector_info
[fb_helper
->connector_count
++] = fb_helper_connector
;
146 EXPORT_SYMBOL(drm_fb_helper_add_one_connector
);
148 static void remove_from_modeset(struct drm_mode_set
*set
,
149 struct drm_connector
*connector
)
153 for (i
= 0; i
< set
->num_connectors
; i
++) {
154 if (set
->connectors
[i
] == connector
)
158 if (i
== set
->num_connectors
)
161 for (j
= i
+ 1; j
< set
->num_connectors
; j
++) {
162 set
->connectors
[j
- 1] = set
->connectors
[j
];
164 set
->num_connectors
--;
166 /* because i915 is pissy about this..
167 * TODO maybe need to makes sure we set it back to !=NULL somewhere?
169 if (set
->num_connectors
== 0)
173 int drm_fb_helper_remove_one_connector(struct drm_fb_helper
*fb_helper
,
174 struct drm_connector
*connector
)
176 struct drm_fb_helper_connector
*fb_helper_connector
;
179 WARN_ON(!mutex_is_locked(&fb_helper
->dev
->mode_config
.mutex
));
181 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
182 if (fb_helper
->connector_info
[i
]->connector
== connector
)
186 if (i
== fb_helper
->connector_count
)
188 fb_helper_connector
= fb_helper
->connector_info
[i
];
190 for (j
= i
+ 1; j
< fb_helper
->connector_count
; j
++) {
191 fb_helper
->connector_info
[j
- 1] = fb_helper
->connector_info
[j
];
193 fb_helper
->connector_count
--;
194 kfree(fb_helper_connector
);
196 /* also cleanup dangling references to the connector: */
197 for (i
= 0; i
< fb_helper
->crtc_count
; i
++)
198 remove_from_modeset(&fb_helper
->crtc_info
[i
].mode_set
, connector
);
202 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector
);
204 static void drm_fb_helper_save_lut_atomic(struct drm_crtc
*crtc
, struct drm_fb_helper
*helper
)
206 uint16_t *r_base
, *g_base
, *b_base
;
209 if (helper
->funcs
->gamma_get
== NULL
)
212 r_base
= crtc
->gamma_store
;
213 g_base
= r_base
+ crtc
->gamma_size
;
214 b_base
= g_base
+ crtc
->gamma_size
;
216 for (i
= 0; i
< crtc
->gamma_size
; i
++)
217 helper
->funcs
->gamma_get(crtc
, &r_base
[i
], &g_base
[i
], &b_base
[i
], i
);
220 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc
*crtc
)
222 uint16_t *r_base
, *g_base
, *b_base
;
224 if (crtc
->funcs
->gamma_set
== NULL
)
227 r_base
= crtc
->gamma_store
;
228 g_base
= r_base
+ crtc
->gamma_size
;
229 b_base
= g_base
+ crtc
->gamma_size
;
231 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, 0, crtc
->gamma_size
);
235 * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
236 * @info: fbdev registered by the helper
238 int drm_fb_helper_debug_enter(struct fb_info
*info
)
240 struct drm_fb_helper
*helper
= info
->par
;
241 const struct drm_crtc_helper_funcs
*funcs
;
244 list_for_each_entry(helper
, &kernel_fb_helper_list
, kernel_fb_list
) {
245 for (i
= 0; i
< helper
->crtc_count
; i
++) {
246 struct drm_mode_set
*mode_set
=
247 &helper
->crtc_info
[i
].mode_set
;
249 if (!mode_set
->crtc
->enabled
)
252 funcs
= mode_set
->crtc
->helper_private
;
253 drm_fb_helper_save_lut_atomic(mode_set
->crtc
, helper
);
254 funcs
->mode_set_base_atomic(mode_set
->crtc
,
258 ENTER_ATOMIC_MODE_SET
);
264 EXPORT_SYMBOL(drm_fb_helper_debug_enter
);
266 /* Find the real fb for a given fb helper CRTC */
267 static struct drm_framebuffer
*drm_mode_config_fb(struct drm_crtc
*crtc
)
269 struct drm_device
*dev
= crtc
->dev
;
272 list_for_each_entry(c
, &dev
->mode_config
.crtc_list
, head
) {
273 if (crtc
->base
.id
== c
->base
.id
)
274 return c
->primary
->fb
;
281 * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
282 * @info: fbdev registered by the helper
284 int drm_fb_helper_debug_leave(struct fb_info
*info
)
286 struct drm_fb_helper
*helper
= info
->par
;
287 struct drm_crtc
*crtc
;
288 const struct drm_crtc_helper_funcs
*funcs
;
289 struct drm_framebuffer
*fb
;
292 for (i
= 0; i
< helper
->crtc_count
; i
++) {
293 struct drm_mode_set
*mode_set
= &helper
->crtc_info
[i
].mode_set
;
294 crtc
= mode_set
->crtc
;
295 funcs
= crtc
->helper_private
;
296 fb
= drm_mode_config_fb(crtc
);
302 DRM_ERROR("no fb to restore??\n");
306 drm_fb_helper_restore_lut_atomic(mode_set
->crtc
);
307 funcs
->mode_set_base_atomic(mode_set
->crtc
, fb
, crtc
->x
,
308 crtc
->y
, LEAVE_ATOMIC_MODE_SET
);
313 EXPORT_SYMBOL(drm_fb_helper_debug_leave
);
315 static bool restore_fbdev_mode(struct drm_fb_helper
*fb_helper
)
317 struct drm_device
*dev
= fb_helper
->dev
;
318 struct drm_plane
*plane
;
322 drm_warn_on_modeset_not_all_locked(dev
);
324 list_for_each_entry(plane
, &dev
->mode_config
.plane_list
, head
) {
325 if (plane
->type
!= DRM_PLANE_TYPE_PRIMARY
)
326 drm_plane_force_disable(plane
);
328 if (dev
->mode_config
.rotation_property
) {
329 drm_mode_plane_set_obj_prop(plane
,
330 dev
->mode_config
.rotation_property
,
335 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
336 struct drm_mode_set
*mode_set
= &fb_helper
->crtc_info
[i
].mode_set
;
337 struct drm_crtc
*crtc
= mode_set
->crtc
;
340 if (crtc
->funcs
->cursor_set
) {
341 ret
= crtc
->funcs
->cursor_set(crtc
, NULL
, 0, 0, 0);
346 ret
= drm_mode_set_config_internal(mode_set
);
353 * drm_fb_helper_restore_fbdev_mode - restore fbdev configuration
354 * @fb_helper: fbcon to restore
356 * This should be called from driver's drm ->lastclose callback
357 * when implementing an fbcon on top of kms using this helper. This ensures that
358 * the user isn't greeted with a black screen when e.g. X dies.
360 * Use this variant if you need to bypass locking (panic), or already
361 * hold all modeset locks. Otherwise use drm_fb_helper_restore_fbdev_mode_unlocked()
363 static bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper
*fb_helper
)
365 return restore_fbdev_mode(fb_helper
);
369 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
370 * @fb_helper: fbcon to restore
372 * This should be called from driver's drm ->lastclose callback
373 * when implementing an fbcon on top of kms using this helper. This ensures that
374 * the user isn't greeted with a black screen when e.g. X dies.
376 bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper
*fb_helper
)
378 struct drm_device
*dev
= fb_helper
->dev
;
380 bool do_delayed
= false;
382 drm_modeset_lock_all(dev
);
383 ret
= restore_fbdev_mode(fb_helper
);
385 do_delayed
= fb_helper
->delayed_hotplug
;
387 fb_helper
->delayed_hotplug
= false;
388 drm_modeset_unlock_all(dev
);
391 drm_fb_helper_hotplug_event(fb_helper
);
394 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked
);
397 * restore fbcon display for all kms driver's using this helper, used for sysrq
398 * and panic handling.
400 static bool drm_fb_helper_force_kernel_mode(void)
402 bool ret
, error
= false;
403 struct drm_fb_helper
*helper
;
405 if (list_empty(&kernel_fb_helper_list
))
408 list_for_each_entry(helper
, &kernel_fb_helper_list
, kernel_fb_list
) {
409 struct drm_device
*dev
= helper
->dev
;
411 if (dev
->switch_power_state
== DRM_SWITCH_POWER_OFF
)
415 * NOTE: Use trylock mode to avoid deadlocks and sleeping in
418 if (__drm_modeset_lock_all(dev
, true) != 0) {
423 ret
= drm_fb_helper_restore_fbdev_mode(helper
);
427 drm_modeset_unlock_all(dev
);
432 static int drm_fb_helper_panic(struct notifier_block
*n
, unsigned long ununsed
,
436 * It's a waste of time and effort to switch back to text console
437 * if the kernel should reboot before panic messages can be seen.
439 if (panic_timeout
< 0)
442 pr_err("panic occurred, switching back to text console\n");
443 return drm_fb_helper_force_kernel_mode();
446 static struct notifier_block paniced
= {
447 .notifier_call
= drm_fb_helper_panic
,
450 static bool drm_fb_helper_is_bound(struct drm_fb_helper
*fb_helper
)
452 struct drm_device
*dev
= fb_helper
->dev
;
453 struct drm_crtc
*crtc
;
454 int bound
= 0, crtcs_bound
= 0;
456 /* Sometimes user space wants everything disabled, so don't steal the
457 * display if there's a master. */
458 if (dev
->primary
->master
)
461 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
462 if (crtc
->primary
->fb
)
464 if (crtc
->primary
->fb
== fb_helper
->fb
)
468 if (bound
< crtcs_bound
)
474 #ifdef CONFIG_MAGIC_SYSRQ
475 static void drm_fb_helper_restore_work_fn(struct work_struct
*ignored
)
478 ret
= drm_fb_helper_force_kernel_mode();
480 DRM_ERROR("Failed to restore crtc configuration\n");
482 static DECLARE_WORK(drm_fb_helper_restore_work
, drm_fb_helper_restore_work_fn
);
484 static void drm_fb_helper_sysrq(int dummy1
)
486 schedule_work(&drm_fb_helper_restore_work
);
489 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op
= {
490 .handler
= drm_fb_helper_sysrq
,
491 .help_msg
= "force-fb(V)",
492 .action_msg
= "Restore framebuffer console",
495 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op
= { };
498 static void drm_fb_helper_dpms(struct fb_info
*info
, int dpms_mode
)
500 struct drm_fb_helper
*fb_helper
= info
->par
;
501 struct drm_device
*dev
= fb_helper
->dev
;
502 struct drm_crtc
*crtc
;
503 struct drm_connector
*connector
;
507 * fbdev->blank can be called from irq context in case of a panic.
508 * Since we already have our own special panic handler which will
509 * restore the fbdev console mode completely, just bail out early.
511 if (oops_in_progress
)
515 * For each CRTC in this fb, turn the connectors on/off.
517 drm_modeset_lock_all(dev
);
518 if (!drm_fb_helper_is_bound(fb_helper
)) {
519 drm_modeset_unlock_all(dev
);
523 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
524 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
529 /* Walk the connectors & encoders on this fb turning them on/off */
530 for (j
= 0; j
< fb_helper
->connector_count
; j
++) {
531 connector
= fb_helper
->connector_info
[j
]->connector
;
532 connector
->funcs
->dpms(connector
, dpms_mode
);
533 drm_object_property_set_value(&connector
->base
,
534 dev
->mode_config
.dpms_property
, dpms_mode
);
537 drm_modeset_unlock_all(dev
);
541 * drm_fb_helper_blank - implementation for ->fb_blank
542 * @blank: desired blanking state
543 * @info: fbdev registered by the helper
545 int drm_fb_helper_blank(int blank
, struct fb_info
*info
)
548 /* Display: On; HSync: On, VSync: On */
549 case FB_BLANK_UNBLANK
:
550 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_ON
);
552 /* Display: Off; HSync: On, VSync: On */
553 case FB_BLANK_NORMAL
:
554 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_STANDBY
);
556 /* Display: Off; HSync: Off, VSync: On */
557 case FB_BLANK_HSYNC_SUSPEND
:
558 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_STANDBY
);
560 /* Display: Off; HSync: On, VSync: Off */
561 case FB_BLANK_VSYNC_SUSPEND
:
562 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_SUSPEND
);
564 /* Display: Off; HSync: Off, VSync: Off */
565 case FB_BLANK_POWERDOWN
:
566 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_OFF
);
571 EXPORT_SYMBOL(drm_fb_helper_blank
);
573 static void drm_fb_helper_crtc_free(struct drm_fb_helper
*helper
)
577 for (i
= 0; i
< helper
->connector_count
; i
++)
578 kfree(helper
->connector_info
[i
]);
579 kfree(helper
->connector_info
);
580 for (i
= 0; i
< helper
->crtc_count
; i
++) {
581 kfree(helper
->crtc_info
[i
].mode_set
.connectors
);
582 if (helper
->crtc_info
[i
].mode_set
.mode
)
583 drm_mode_destroy(helper
->dev
, helper
->crtc_info
[i
].mode_set
.mode
);
585 kfree(helper
->crtc_info
);
589 * drm_fb_helper_prepare - setup a drm_fb_helper structure
591 * @helper: driver-allocated fbdev helper structure to set up
592 * @funcs: pointer to structure of functions associate with this helper
594 * Sets up the bare minimum to make the framebuffer helper usable. This is
595 * useful to implement race-free initialization of the polling helpers.
597 void drm_fb_helper_prepare(struct drm_device
*dev
, struct drm_fb_helper
*helper
,
598 const struct drm_fb_helper_funcs
*funcs
)
600 INIT_LIST_HEAD(&helper
->kernel_fb_list
);
601 helper
->funcs
= funcs
;
604 EXPORT_SYMBOL(drm_fb_helper_prepare
);
607 * drm_fb_helper_init - initialize a drm_fb_helper structure
609 * @fb_helper: driver-allocated fbdev helper structure to initialize
610 * @crtc_count: maximum number of crtcs to support in this fbdev emulation
611 * @max_conn_count: max connector count
613 * This allocates the structures for the fbdev helper with the given limits.
614 * Note that this won't yet touch the hardware (through the driver interfaces)
615 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
616 * to allow driver writes more control over the exact init sequence.
618 * Drivers must call drm_fb_helper_prepare() before calling this function.
621 * Zero if everything went ok, nonzero otherwise.
623 int drm_fb_helper_init(struct drm_device
*dev
,
624 struct drm_fb_helper
*fb_helper
,
625 int crtc_count
, int max_conn_count
)
627 struct drm_crtc
*crtc
;
633 fb_helper
->crtc_info
= kcalloc(crtc_count
, sizeof(struct drm_fb_helper_crtc
), GFP_KERNEL
);
634 if (!fb_helper
->crtc_info
)
637 fb_helper
->crtc_count
= crtc_count
;
638 fb_helper
->connector_info
= kcalloc(dev
->mode_config
.num_connector
, sizeof(struct drm_fb_helper_connector
*), GFP_KERNEL
);
639 if (!fb_helper
->connector_info
) {
640 kfree(fb_helper
->crtc_info
);
643 fb_helper
->connector_info_alloc_count
= dev
->mode_config
.num_connector
;
644 fb_helper
->connector_count
= 0;
646 for (i
= 0; i
< crtc_count
; i
++) {
647 fb_helper
->crtc_info
[i
].mode_set
.connectors
=
648 kcalloc(max_conn_count
,
649 sizeof(struct drm_connector
*),
652 if (!fb_helper
->crtc_info
[i
].mode_set
.connectors
)
654 fb_helper
->crtc_info
[i
].mode_set
.num_connectors
= 0;
658 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
659 fb_helper
->crtc_info
[i
].mode_set
.crtc
= crtc
;
665 drm_fb_helper_crtc_free(fb_helper
);
668 EXPORT_SYMBOL(drm_fb_helper_init
);
670 void drm_fb_helper_fini(struct drm_fb_helper
*fb_helper
)
672 if (!list_empty(&fb_helper
->kernel_fb_list
)) {
673 list_del(&fb_helper
->kernel_fb_list
);
674 if (list_empty(&kernel_fb_helper_list
)) {
675 pr_info("drm: unregistered panic notifier\n");
676 atomic_notifier_chain_unregister(&panic_notifier_list
,
678 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op
);
682 drm_fb_helper_crtc_free(fb_helper
);
685 EXPORT_SYMBOL(drm_fb_helper_fini
);
687 static int setcolreg(struct drm_crtc
*crtc
, u16 red
, u16 green
,
688 u16 blue
, u16 regno
, struct fb_info
*info
)
690 struct drm_fb_helper
*fb_helper
= info
->par
;
691 struct drm_framebuffer
*fb
= fb_helper
->fb
;
694 if (info
->fix
.visual
== FB_VISUAL_TRUECOLOR
) {
697 /* place color in psuedopalette */
700 palette
= (u32
*)info
->pseudo_palette
;
701 red
>>= (16 - info
->var
.red
.length
);
702 green
>>= (16 - info
->var
.green
.length
);
703 blue
>>= (16 - info
->var
.blue
.length
);
704 value
= (red
<< info
->var
.red
.offset
) |
705 (green
<< info
->var
.green
.offset
) |
706 (blue
<< info
->var
.blue
.offset
);
707 if (info
->var
.transp
.length
> 0) {
708 u32 mask
= (1 << info
->var
.transp
.length
) - 1;
709 mask
<<= info
->var
.transp
.offset
;
712 palette
[regno
] = value
;
717 * The driver really shouldn't advertise pseudo/directcolor
718 * visuals if it can't deal with the palette.
720 if (WARN_ON(!fb_helper
->funcs
->gamma_set
||
721 !fb_helper
->funcs
->gamma_get
))
726 if (fb
->bits_per_pixel
== 16) {
729 if (fb
->depth
== 16 && regno
> 63)
731 if (fb
->depth
== 15 && regno
> 31)
734 if (fb
->depth
== 16) {
738 for (i
= 0; i
< 8; i
++)
739 fb_helper
->funcs
->gamma_set(crtc
, red
,
740 green
, blue
, pindex
+ i
);
743 fb_helper
->funcs
->gamma_get(crtc
, &r
,
747 for (i
= 0; i
< 4; i
++)
748 fb_helper
->funcs
->gamma_set(crtc
, r
,
755 fb_helper
->funcs
->gamma_set(crtc
, red
, green
, blue
, pindex
);
760 * drm_fb_helper_setcmap - implementation for ->fb_setcmap
762 * @info: fbdev registered by the helper
764 int drm_fb_helper_setcmap(struct fb_cmap
*cmap
, struct fb_info
*info
)
766 struct drm_fb_helper
*fb_helper
= info
->par
;
767 struct drm_device
*dev
= fb_helper
->dev
;
768 const struct drm_crtc_helper_funcs
*crtc_funcs
;
769 u16
*red
, *green
, *blue
, *transp
;
770 struct drm_crtc
*crtc
;
774 if (__drm_modeset_lock_all(dev
, !!oops_in_progress
)) {
777 if (!drm_fb_helper_is_bound(fb_helper
)) {
778 drm_modeset_unlock_all(dev
);
782 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
783 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
784 crtc_funcs
= crtc
->helper_private
;
789 transp
= cmap
->transp
;
792 for (j
= 0; j
< cmap
->len
; j
++) {
793 u16 hred
, hgreen
, hblue
, htransp
= 0xffff;
802 rc
= setcolreg(crtc
, hred
, hgreen
, hblue
, start
++, info
);
806 if (crtc_funcs
->load_lut
)
807 crtc_funcs
->load_lut(crtc
);
810 drm_modeset_unlock_all(dev
);
813 EXPORT_SYMBOL(drm_fb_helper_setcmap
);
816 * drm_fb_helper_check_var - implementation for ->fb_check_var
817 * @var: screeninfo to check
818 * @info: fbdev registered by the helper
820 int drm_fb_helper_check_var(struct fb_var_screeninfo
*var
,
821 struct fb_info
*info
)
823 struct drm_fb_helper
*fb_helper
= info
->par
;
824 struct drm_framebuffer
*fb
= fb_helper
->fb
;
827 if (var
->pixclock
!= 0 || in_dbg_master())
830 /* Need to resize the fb object !!! */
831 if (var
->bits_per_pixel
> fb
->bits_per_pixel
||
832 var
->xres
> fb
->width
|| var
->yres
> fb
->height
||
833 var
->xres_virtual
> fb
->width
|| var
->yres_virtual
> fb
->height
) {
834 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
835 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
836 var
->xres
, var
->yres
, var
->bits_per_pixel
,
837 var
->xres_virtual
, var
->yres_virtual
,
838 fb
->width
, fb
->height
, fb
->bits_per_pixel
);
842 switch (var
->bits_per_pixel
) {
844 depth
= (var
->green
.length
== 6) ? 16 : 15;
847 depth
= (var
->transp
.length
> 0) ? 32 : 24;
850 depth
= var
->bits_per_pixel
;
857 var
->green
.offset
= 0;
858 var
->blue
.offset
= 0;
860 var
->green
.length
= 8;
861 var
->blue
.length
= 8;
862 var
->transp
.length
= 0;
863 var
->transp
.offset
= 0;
866 var
->red
.offset
= 10;
867 var
->green
.offset
= 5;
868 var
->blue
.offset
= 0;
870 var
->green
.length
= 5;
871 var
->blue
.length
= 5;
872 var
->transp
.length
= 1;
873 var
->transp
.offset
= 15;
876 var
->red
.offset
= 11;
877 var
->green
.offset
= 5;
878 var
->blue
.offset
= 0;
880 var
->green
.length
= 6;
881 var
->blue
.length
= 5;
882 var
->transp
.length
= 0;
883 var
->transp
.offset
= 0;
886 var
->red
.offset
= 16;
887 var
->green
.offset
= 8;
888 var
->blue
.offset
= 0;
890 var
->green
.length
= 8;
891 var
->blue
.length
= 8;
892 var
->transp
.length
= 0;
893 var
->transp
.offset
= 0;
896 var
->red
.offset
= 16;
897 var
->green
.offset
= 8;
898 var
->blue
.offset
= 0;
900 var
->green
.length
= 8;
901 var
->blue
.length
= 8;
902 var
->transp
.length
= 8;
903 var
->transp
.offset
= 24;
910 EXPORT_SYMBOL(drm_fb_helper_check_var
);
913 * drm_fb_helper_set_par - implementation for ->fb_set_par
914 * @info: fbdev registered by the helper
916 * This will let fbcon do the mode init and is called at initialization time by
917 * the fbdev core when registering the driver, and later on through the hotplug
920 int drm_fb_helper_set_par(struct fb_info
*info
)
922 struct drm_fb_helper
*fb_helper
= info
->par
;
923 struct fb_var_screeninfo
*var
= &info
->var
;
925 if (var
->pixclock
!= 0) {
926 DRM_ERROR("PIXEL CLOCK SET\n");
930 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper
);
934 EXPORT_SYMBOL(drm_fb_helper_set_par
);
937 * drm_fb_helper_pan_display - implementation for ->fb_pan_display
938 * @var: updated screen information
939 * @info: fbdev registered by the helper
941 int drm_fb_helper_pan_display(struct fb_var_screeninfo
*var
,
942 struct fb_info
*info
)
944 struct drm_fb_helper
*fb_helper
= info
->par
;
945 struct drm_device
*dev
= fb_helper
->dev
;
946 struct drm_mode_set
*modeset
;
950 if (__drm_modeset_lock_all(dev
, !!oops_in_progress
)) {
953 if (!drm_fb_helper_is_bound(fb_helper
)) {
954 drm_modeset_unlock_all(dev
);
958 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
959 modeset
= &fb_helper
->crtc_info
[i
].mode_set
;
961 modeset
->x
= var
->xoffset
;
962 modeset
->y
= var
->yoffset
;
964 if (modeset
->num_connectors
) {
965 ret
= drm_mode_set_config_internal(modeset
);
967 info
->var
.xoffset
= var
->xoffset
;
968 info
->var
.yoffset
= var
->yoffset
;
972 drm_modeset_unlock_all(dev
);
975 EXPORT_SYMBOL(drm_fb_helper_pan_display
);
978 * Allocates the backing storage and sets up the fbdev info structure through
979 * the ->fb_probe callback and then registers the fbdev and sets up the panic
982 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper
*fb_helper
,
988 struct fb_info
*info
;
989 struct drm_fb_helper_surface_size sizes
;
992 memset(&sizes
, 0, sizeof(struct drm_fb_helper_surface_size
));
993 sizes
.surface_depth
= 24;
994 sizes
.surface_bpp
= 32;
995 sizes
.fb_width
= (unsigned)-1;
996 sizes
.fb_height
= (unsigned)-1;
998 /* if driver picks 8 or 16 by default use that
999 for both depth/bpp */
1000 if (preferred_bpp
!= sizes
.surface_bpp
)
1001 sizes
.surface_depth
= sizes
.surface_bpp
= preferred_bpp
;
1003 /* first up get a count of crtcs now in use and new min/maxes width/heights */
1004 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1005 struct drm_fb_helper_connector
*fb_helper_conn
= fb_helper
->connector_info
[i
];
1006 struct drm_cmdline_mode
*cmdline_mode
;
1008 cmdline_mode
= &fb_helper_conn
->connector
->cmdline_mode
;
1010 if (cmdline_mode
->bpp_specified
) {
1011 switch (cmdline_mode
->bpp
) {
1013 sizes
.surface_depth
= sizes
.surface_bpp
= 8;
1016 sizes
.surface_depth
= 15;
1017 sizes
.surface_bpp
= 16;
1020 sizes
.surface_depth
= sizes
.surface_bpp
= 16;
1023 sizes
.surface_depth
= sizes
.surface_bpp
= 24;
1026 sizes
.surface_depth
= 24;
1027 sizes
.surface_bpp
= 32;
1035 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1036 struct drm_display_mode
*desired_mode
;
1037 struct drm_mode_set
*mode_set
;
1039 /* in case of tile group, are we the last tile vert or horiz?
1040 * If no tile group you are always the last one both vertically
1043 bool lastv
= true, lasth
= true;
1045 desired_mode
= fb_helper
->crtc_info
[i
].desired_mode
;
1046 mode_set
= &fb_helper
->crtc_info
[i
].mode_set
;
1053 x
= fb_helper
->crtc_info
[i
].x
;
1054 y
= fb_helper
->crtc_info
[i
].y
;
1056 if (gamma_size
== 0)
1057 gamma_size
= fb_helper
->crtc_info
[i
].mode_set
.crtc
->gamma_size
;
1059 sizes
.surface_width
= max_t(u32
, desired_mode
->hdisplay
+ x
, sizes
.surface_width
);
1060 sizes
.surface_height
= max_t(u32
, desired_mode
->vdisplay
+ y
, sizes
.surface_height
);
1062 for (j
= 0; j
< mode_set
->num_connectors
; j
++) {
1063 struct drm_connector
*connector
= mode_set
->connectors
[j
];
1064 if (connector
->has_tile
) {
1065 lasth
= (connector
->tile_h_loc
== (connector
->num_h_tile
- 1));
1066 lastv
= (connector
->tile_v_loc
== (connector
->num_v_tile
- 1));
1067 /* cloning to multiple tiles is just crazy-talk, so: */
1073 sizes
.fb_width
= min_t(u32
, desired_mode
->hdisplay
+ x
, sizes
.fb_width
);
1075 sizes
.fb_height
= min_t(u32
, desired_mode
->vdisplay
+ y
, sizes
.fb_height
);
1078 if (crtc_count
== 0 || sizes
.fb_width
== -1 || sizes
.fb_height
== -1) {
1079 /* hmm everyone went away - assume VGA cable just fell out
1080 and will come back later. */
1081 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
1082 sizes
.fb_width
= sizes
.surface_width
= 1024;
1083 sizes
.fb_height
= sizes
.surface_height
= 768;
1086 /* push down into drivers */
1087 ret
= (*fb_helper
->funcs
->fb_probe
)(fb_helper
, &sizes
);
1091 info
= fb_helper
->fbdev
;
1094 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1095 * events, but at init time drm_setup_crtcs needs to be called before
1096 * the fb is allocated (since we need to figure out the desired size of
1097 * the fb before we can allocate it ...). Hence we need to fix things up
1100 for (i
= 0; i
< fb_helper
->crtc_count
; i
++)
1101 if (fb_helper
->crtc_info
[i
].mode_set
.num_connectors
)
1102 fb_helper
->crtc_info
[i
].mode_set
.fb
= fb_helper
->fb
;
1105 info
->var
.pixclock
= 0;
1106 if (register_framebuffer(info
) < 0)
1109 dev_info(fb_helper
->dev
->dev
, "fb%d: %s frame buffer device\n",
1110 info
->node
, info
->fix
.id
);
1112 /* Switch back to kernel console on panic */
1113 /* multi card linked list maybe */
1114 if (list_empty(&kernel_fb_helper_list
)) {
1115 dev_info(fb_helper
->dev
->dev
, "registered panic notifier\n");
1116 atomic_notifier_chain_register(&panic_notifier_list
,
1118 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op
);
1121 list_add(&fb_helper
->kernel_fb_list
, &kernel_fb_helper_list
);
1127 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1128 * @info: fbdev registered by the helper
1129 * @pitch: desired pitch
1130 * @depth: desired depth
1132 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1133 * fbdev emulations. Drivers which support acceleration methods which impose
1134 * additional constraints need to set up their own limits.
1136 * Drivers should call this (or their equivalent setup code) from their
1137 * ->fb_probe callback.
1139 void drm_fb_helper_fill_fix(struct fb_info
*info
, uint32_t pitch
,
1142 info
->fix
.type
= FB_TYPE_PACKED_PIXELS
;
1143 info
->fix
.visual
= depth
== 8 ? FB_VISUAL_PSEUDOCOLOR
:
1144 FB_VISUAL_TRUECOLOR
;
1145 info
->fix
.mmio_start
= 0;
1146 info
->fix
.mmio_len
= 0;
1147 info
->fix
.type_aux
= 0;
1148 info
->fix
.xpanstep
= 1; /* doing it in hw */
1149 info
->fix
.ypanstep
= 1; /* doing it in hw */
1150 info
->fix
.ywrapstep
= 0;
1151 info
->fix
.accel
= FB_ACCEL_NONE
;
1153 info
->fix
.line_length
= pitch
;
1156 EXPORT_SYMBOL(drm_fb_helper_fill_fix
);
1159 * drm_fb_helper_fill_var - initalizes variable fbdev information
1160 * @info: fbdev instance to set up
1161 * @fb_helper: fb helper instance to use as template
1162 * @fb_width: desired fb width
1163 * @fb_height: desired fb height
1165 * Sets up the variable fbdev metainformation from the given fb helper instance
1166 * and the drm framebuffer allocated in fb_helper->fb.
1168 * Drivers should call this (or their equivalent setup code) from their
1169 * ->fb_probe callback after having allocated the fbdev backing
1170 * storage framebuffer.
1172 void drm_fb_helper_fill_var(struct fb_info
*info
, struct drm_fb_helper
*fb_helper
,
1173 uint32_t fb_width
, uint32_t fb_height
)
1175 struct drm_framebuffer
*fb
= fb_helper
->fb
;
1176 info
->pseudo_palette
= fb_helper
->pseudo_palette
;
1177 info
->var
.xres_virtual
= fb
->width
;
1178 info
->var
.yres_virtual
= fb
->height
;
1179 info
->var
.bits_per_pixel
= fb
->bits_per_pixel
;
1180 info
->var
.accel_flags
= FB_ACCELF_TEXT
;
1181 info
->var
.xoffset
= 0;
1182 info
->var
.yoffset
= 0;
1183 info
->var
.activate
= FB_ACTIVATE_NOW
;
1184 info
->var
.height
= -1;
1185 info
->var
.width
= -1;
1187 switch (fb
->depth
) {
1189 info
->var
.red
.offset
= 0;
1190 info
->var
.green
.offset
= 0;
1191 info
->var
.blue
.offset
= 0;
1192 info
->var
.red
.length
= 8; /* 8bit DAC */
1193 info
->var
.green
.length
= 8;
1194 info
->var
.blue
.length
= 8;
1195 info
->var
.transp
.offset
= 0;
1196 info
->var
.transp
.length
= 0;
1199 info
->var
.red
.offset
= 10;
1200 info
->var
.green
.offset
= 5;
1201 info
->var
.blue
.offset
= 0;
1202 info
->var
.red
.length
= 5;
1203 info
->var
.green
.length
= 5;
1204 info
->var
.blue
.length
= 5;
1205 info
->var
.transp
.offset
= 15;
1206 info
->var
.transp
.length
= 1;
1209 info
->var
.red
.offset
= 11;
1210 info
->var
.green
.offset
= 5;
1211 info
->var
.blue
.offset
= 0;
1212 info
->var
.red
.length
= 5;
1213 info
->var
.green
.length
= 6;
1214 info
->var
.blue
.length
= 5;
1215 info
->var
.transp
.offset
= 0;
1218 info
->var
.red
.offset
= 16;
1219 info
->var
.green
.offset
= 8;
1220 info
->var
.blue
.offset
= 0;
1221 info
->var
.red
.length
= 8;
1222 info
->var
.green
.length
= 8;
1223 info
->var
.blue
.length
= 8;
1224 info
->var
.transp
.offset
= 0;
1225 info
->var
.transp
.length
= 0;
1228 info
->var
.red
.offset
= 16;
1229 info
->var
.green
.offset
= 8;
1230 info
->var
.blue
.offset
= 0;
1231 info
->var
.red
.length
= 8;
1232 info
->var
.green
.length
= 8;
1233 info
->var
.blue
.length
= 8;
1234 info
->var
.transp
.offset
= 24;
1235 info
->var
.transp
.length
= 8;
1241 info
->var
.xres
= fb_width
;
1242 info
->var
.yres
= fb_height
;
1244 EXPORT_SYMBOL(drm_fb_helper_fill_var
);
1246 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper
*fb_helper
,
1250 struct drm_connector
*connector
;
1254 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1255 connector
= fb_helper
->connector_info
[i
]->connector
;
1256 count
+= connector
->funcs
->fill_modes(connector
, maxX
, maxY
);
1262 struct drm_display_mode
*drm_has_preferred_mode(struct drm_fb_helper_connector
*fb_connector
, int width
, int height
)
1264 struct drm_display_mode
*mode
;
1266 list_for_each_entry(mode
, &fb_connector
->connector
->modes
, head
) {
1267 if (mode
->hdisplay
> width
||
1268 mode
->vdisplay
> height
)
1270 if (mode
->type
& DRM_MODE_TYPE_PREFERRED
)
1275 EXPORT_SYMBOL(drm_has_preferred_mode
);
1277 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector
*fb_connector
)
1279 return fb_connector
->connector
->cmdline_mode
.specified
;
1282 struct drm_display_mode
*drm_pick_cmdline_mode(struct drm_fb_helper_connector
*fb_helper_conn
,
1283 int width
, int height
)
1285 struct drm_cmdline_mode
*cmdline_mode
;
1286 struct drm_display_mode
*mode
;
1287 bool prefer_non_interlace
;
1289 cmdline_mode
= &fb_helper_conn
->connector
->cmdline_mode
;
1290 if (cmdline_mode
->specified
== false)
1293 /* attempt to find a matching mode in the list of modes
1294 * we have gotten so far, if not add a CVT mode that conforms
1296 if (cmdline_mode
->rb
|| cmdline_mode
->margins
)
1299 prefer_non_interlace
= !cmdline_mode
->interlace
;
1301 list_for_each_entry(mode
, &fb_helper_conn
->connector
->modes
, head
) {
1302 /* check width/height */
1303 if (mode
->hdisplay
!= cmdline_mode
->xres
||
1304 mode
->vdisplay
!= cmdline_mode
->yres
)
1307 if (cmdline_mode
->refresh_specified
) {
1308 if (mode
->vrefresh
!= cmdline_mode
->refresh
)
1312 if (cmdline_mode
->interlace
) {
1313 if (!(mode
->flags
& DRM_MODE_FLAG_INTERLACE
))
1315 } else if (prefer_non_interlace
) {
1316 if (mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
1322 if (prefer_non_interlace
) {
1323 prefer_non_interlace
= false;
1328 mode
= drm_mode_create_from_cmdline_mode(fb_helper_conn
->connector
->dev
,
1330 list_add(&mode
->head
, &fb_helper_conn
->connector
->modes
);
1333 EXPORT_SYMBOL(drm_pick_cmdline_mode
);
1335 static bool drm_connector_enabled(struct drm_connector
*connector
, bool strict
)
1340 enable
= connector
->status
== connector_status_connected
;
1342 enable
= connector
->status
!= connector_status_disconnected
;
1347 static void drm_enable_connectors(struct drm_fb_helper
*fb_helper
,
1350 bool any_enabled
= false;
1351 struct drm_connector
*connector
;
1354 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1355 connector
= fb_helper
->connector_info
[i
]->connector
;
1356 enabled
[i
] = drm_connector_enabled(connector
, true);
1357 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector
->base
.id
,
1358 enabled
[i
] ? "yes" : "no");
1359 any_enabled
|= enabled
[i
];
1365 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1366 connector
= fb_helper
->connector_info
[i
]->connector
;
1367 enabled
[i
] = drm_connector_enabled(connector
, false);
1371 static bool drm_target_cloned(struct drm_fb_helper
*fb_helper
,
1372 struct drm_display_mode
**modes
,
1373 struct drm_fb_offset
*offsets
,
1374 bool *enabled
, int width
, int height
)
1377 bool can_clone
= false;
1378 struct drm_fb_helper_connector
*fb_helper_conn
;
1379 struct drm_display_mode
*dmt_mode
, *mode
;
1381 /* only contemplate cloning in the single crtc case */
1382 if (fb_helper
->crtc_count
> 1)
1386 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1391 /* only contemplate cloning if more than one connector is enabled */
1395 /* check the command line or if nothing common pick 1024x768 */
1397 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1400 fb_helper_conn
= fb_helper
->connector_info
[i
];
1401 modes
[i
] = drm_pick_cmdline_mode(fb_helper_conn
, width
, height
);
1406 for (j
= 0; j
< i
; j
++) {
1409 if (!drm_mode_equal(modes
[j
], modes
[i
]))
1415 DRM_DEBUG_KMS("can clone using command line\n");
1419 /* try and find a 1024x768 mode on each connector */
1421 dmt_mode
= drm_mode_find_dmt(fb_helper
->dev
, 1024, 768, 60, false);
1423 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1428 fb_helper_conn
= fb_helper
->connector_info
[i
];
1429 list_for_each_entry(mode
, &fb_helper_conn
->connector
->modes
, head
) {
1430 if (drm_mode_equal(mode
, dmt_mode
))
1438 DRM_DEBUG_KMS("can clone using 1024x768\n");
1441 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1445 static int drm_get_tile_offsets(struct drm_fb_helper
*fb_helper
,
1446 struct drm_display_mode
**modes
,
1447 struct drm_fb_offset
*offsets
,
1449 int h_idx
, int v_idx
)
1451 struct drm_fb_helper_connector
*fb_helper_conn
;
1453 int hoffset
= 0, voffset
= 0;
1455 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1456 fb_helper_conn
= fb_helper
->connector_info
[i
];
1457 if (!fb_helper_conn
->connector
->has_tile
)
1460 if (!modes
[i
] && (h_idx
|| v_idx
)) {
1461 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i
,
1462 fb_helper_conn
->connector
->base
.id
);
1465 if (fb_helper_conn
->connector
->tile_h_loc
< h_idx
)
1466 hoffset
+= modes
[i
]->hdisplay
;
1468 if (fb_helper_conn
->connector
->tile_v_loc
< v_idx
)
1469 voffset
+= modes
[i
]->vdisplay
;
1471 offsets
[idx
].x
= hoffset
;
1472 offsets
[idx
].y
= voffset
;
1473 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset
, voffset
, h_idx
, v_idx
);
1477 static bool drm_target_preferred(struct drm_fb_helper
*fb_helper
,
1478 struct drm_display_mode
**modes
,
1479 struct drm_fb_offset
*offsets
,
1480 bool *enabled
, int width
, int height
)
1482 struct drm_fb_helper_connector
*fb_helper_conn
;
1484 uint64_t conn_configured
= 0, mask
;
1486 mask
= (1 << fb_helper
->connector_count
) - 1;
1488 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1489 fb_helper_conn
= fb_helper
->connector_info
[i
];
1491 if (conn_configured
& (1 << i
))
1494 if (enabled
[i
] == false) {
1495 conn_configured
|= (1 << i
);
1499 /* first pass over all the untiled connectors */
1500 if (tile_pass
== 0 && fb_helper_conn
->connector
->has_tile
)
1503 if (tile_pass
== 1) {
1504 if (fb_helper_conn
->connector
->tile_h_loc
!= 0 ||
1505 fb_helper_conn
->connector
->tile_v_loc
!= 0)
1509 if (fb_helper_conn
->connector
->tile_h_loc
!= tile_pass
-1 &&
1510 fb_helper_conn
->connector
->tile_v_loc
!= tile_pass
- 1)
1511 /* if this tile_pass doesn't cover any of the tiles - keep going */
1514 /* find the tile offsets for this pass - need
1515 to find all tiles left and above */
1516 drm_get_tile_offsets(fb_helper
, modes
, offsets
,
1517 i
, fb_helper_conn
->connector
->tile_h_loc
, fb_helper_conn
->connector
->tile_v_loc
);
1519 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1520 fb_helper_conn
->connector
->base
.id
);
1522 /* got for command line mode first */
1523 modes
[i
] = drm_pick_cmdline_mode(fb_helper_conn
, width
, height
);
1525 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
1526 fb_helper_conn
->connector
->base
.id
, fb_helper_conn
->connector
->tile_group
? fb_helper_conn
->connector
->tile_group
->id
: 0);
1527 modes
[i
] = drm_has_preferred_mode(fb_helper_conn
, width
, height
);
1529 /* No preferred modes, pick one off the list */
1530 if (!modes
[i
] && !list_empty(&fb_helper_conn
->connector
->modes
)) {
1531 list_for_each_entry(modes
[i
], &fb_helper_conn
->connector
->modes
, head
)
1534 DRM_DEBUG_KMS("found mode %s\n", modes
[i
] ? modes
[i
]->name
:
1536 conn_configured
|= (1 << i
);
1539 if ((conn_configured
& mask
) != mask
) {
1546 static int drm_pick_crtcs(struct drm_fb_helper
*fb_helper
,
1547 struct drm_fb_helper_crtc
**best_crtcs
,
1548 struct drm_display_mode
**modes
,
1549 int n
, int width
, int height
)
1552 struct drm_device
*dev
= fb_helper
->dev
;
1553 struct drm_connector
*connector
;
1554 const struct drm_connector_helper_funcs
*connector_funcs
;
1555 struct drm_encoder
*encoder
;
1556 int my_score
, best_score
, score
;
1557 struct drm_fb_helper_crtc
**crtcs
, *crtc
;
1558 struct drm_fb_helper_connector
*fb_helper_conn
;
1560 if (n
== fb_helper
->connector_count
)
1563 fb_helper_conn
= fb_helper
->connector_info
[n
];
1564 connector
= fb_helper_conn
->connector
;
1566 best_crtcs
[n
] = NULL
;
1567 best_score
= drm_pick_crtcs(fb_helper
, best_crtcs
, modes
, n
+1, width
, height
);
1568 if (modes
[n
] == NULL
)
1571 crtcs
= kzalloc(dev
->mode_config
.num_connector
*
1572 sizeof(struct drm_fb_helper_crtc
*), GFP_KERNEL
);
1577 if (connector
->status
== connector_status_connected
)
1579 if (drm_has_cmdline_mode(fb_helper_conn
))
1581 if (drm_has_preferred_mode(fb_helper_conn
, width
, height
))
1584 connector_funcs
= connector
->helper_private
;
1585 encoder
= connector_funcs
->best_encoder(connector
);
1589 /* select a crtc for this connector and then attempt to configure
1590 remaining connectors */
1591 for (c
= 0; c
< fb_helper
->crtc_count
; c
++) {
1592 crtc
= &fb_helper
->crtc_info
[c
];
1594 if ((encoder
->possible_crtcs
& (1 << c
)) == 0)
1597 for (o
= 0; o
< n
; o
++)
1598 if (best_crtcs
[o
] == crtc
)
1602 /* ignore cloning unless only a single crtc */
1603 if (fb_helper
->crtc_count
> 1)
1606 if (!drm_mode_equal(modes
[o
], modes
[n
]))
1611 memcpy(crtcs
, best_crtcs
, n
* sizeof(struct drm_fb_helper_crtc
*));
1612 score
= my_score
+ drm_pick_crtcs(fb_helper
, crtcs
, modes
, n
+ 1,
1614 if (score
> best_score
) {
1616 memcpy(best_crtcs
, crtcs
,
1617 dev
->mode_config
.num_connector
*
1618 sizeof(struct drm_fb_helper_crtc
*));
1626 static void drm_setup_crtcs(struct drm_fb_helper
*fb_helper
)
1628 struct drm_device
*dev
= fb_helper
->dev
;
1629 struct drm_fb_helper_crtc
**crtcs
;
1630 struct drm_display_mode
**modes
;
1631 struct drm_fb_offset
*offsets
;
1632 struct drm_mode_set
*modeset
;
1637 DRM_DEBUG_KMS("\n");
1639 width
= dev
->mode_config
.max_width
;
1640 height
= dev
->mode_config
.max_height
;
1642 crtcs
= kcalloc(dev
->mode_config
.num_connector
,
1643 sizeof(struct drm_fb_helper_crtc
*), GFP_KERNEL
);
1644 modes
= kcalloc(dev
->mode_config
.num_connector
,
1645 sizeof(struct drm_display_mode
*), GFP_KERNEL
);
1646 offsets
= kcalloc(dev
->mode_config
.num_connector
,
1647 sizeof(struct drm_fb_offset
), GFP_KERNEL
);
1648 enabled
= kcalloc(dev
->mode_config
.num_connector
,
1649 sizeof(bool), GFP_KERNEL
);
1650 if (!crtcs
|| !modes
|| !enabled
|| !offsets
) {
1651 DRM_ERROR("Memory allocation failed\n");
1656 drm_enable_connectors(fb_helper
, enabled
);
1658 if (!(fb_helper
->funcs
->initial_config
&&
1659 fb_helper
->funcs
->initial_config(fb_helper
, crtcs
, modes
,
1661 enabled
, width
, height
))) {
1662 memset(modes
, 0, dev
->mode_config
.num_connector
*sizeof(modes
[0]));
1663 memset(crtcs
, 0, dev
->mode_config
.num_connector
*sizeof(crtcs
[0]));
1664 memset(offsets
, 0, dev
->mode_config
.num_connector
*sizeof(offsets
[0]));
1666 if (!drm_target_cloned(fb_helper
, modes
, offsets
,
1667 enabled
, width
, height
) &&
1668 !drm_target_preferred(fb_helper
, modes
, offsets
,
1669 enabled
, width
, height
))
1670 DRM_ERROR("Unable to find initial modes\n");
1672 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1675 drm_pick_crtcs(fb_helper
, crtcs
, modes
, 0, width
, height
);
1678 /* need to set the modesets up here for use later */
1679 /* fill out the connector<->crtc mappings into the modesets */
1680 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1681 modeset
= &fb_helper
->crtc_info
[i
].mode_set
;
1682 modeset
->num_connectors
= 0;
1686 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1687 struct drm_display_mode
*mode
= modes
[i
];
1688 struct drm_fb_helper_crtc
*fb_crtc
= crtcs
[i
];
1689 struct drm_fb_offset
*offset
= &offsets
[i
];
1690 modeset
= &fb_crtc
->mode_set
;
1692 if (mode
&& fb_crtc
) {
1693 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
1694 mode
->name
, fb_crtc
->mode_set
.crtc
->base
.id
, offset
->x
, offset
->y
);
1695 fb_crtc
->desired_mode
= mode
;
1696 fb_crtc
->x
= offset
->x
;
1697 fb_crtc
->y
= offset
->y
;
1699 drm_mode_destroy(dev
, modeset
->mode
);
1700 modeset
->mode
= drm_mode_duplicate(dev
,
1701 fb_crtc
->desired_mode
);
1702 modeset
->connectors
[modeset
->num_connectors
++] = fb_helper
->connector_info
[i
]->connector
;
1703 modeset
->fb
= fb_helper
->fb
;
1704 modeset
->x
= offset
->x
;
1705 modeset
->y
= offset
->y
;
1709 /* Clear out any old modes if there are no more connected outputs. */
1710 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1711 modeset
= &fb_helper
->crtc_info
[i
].mode_set
;
1712 if (modeset
->num_connectors
== 0) {
1713 BUG_ON(modeset
->fb
);
1715 drm_mode_destroy(dev
, modeset
->mode
);
1716 modeset
->mode
= NULL
;
1727 * drm_fb_helper_initial_config - setup a sane initial connector configuration
1728 * @fb_helper: fb_helper device struct
1729 * @bpp_sel: bpp value to use for the framebuffer configuration
1731 * Scans the CRTCs and connectors and tries to put together an initial setup.
1732 * At the moment, this is a cloned configuration across all heads with
1733 * a new framebuffer object as the backing store.
1735 * Note that this also registers the fbdev and so allows userspace to call into
1736 * the driver through the fbdev interfaces.
1738 * This function will call down into the ->fb_probe callback to let
1739 * the driver allocate and initialize the fbdev info structure and the drm
1740 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1741 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1742 * values for the fbdev info structure.
1745 * Zero if everything went ok, nonzero otherwise.
1747 int drm_fb_helper_initial_config(struct drm_fb_helper
*fb_helper
, int bpp_sel
)
1749 struct drm_device
*dev
= fb_helper
->dev
;
1752 mutex_lock(&dev
->mode_config
.mutex
);
1753 count
= drm_fb_helper_probe_connector_modes(fb_helper
,
1754 dev
->mode_config
.max_width
,
1755 dev
->mode_config
.max_height
);
1756 mutex_unlock(&dev
->mode_config
.mutex
);
1758 * we shouldn't end up with no modes here.
1761 dev_info(fb_helper
->dev
->dev
, "No connectors reported connected with modes\n");
1763 drm_setup_crtcs(fb_helper
);
1765 return drm_fb_helper_single_fb_probe(fb_helper
, bpp_sel
);
1767 EXPORT_SYMBOL(drm_fb_helper_initial_config
);
1770 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1771 * probing all the outputs attached to the fb
1772 * @fb_helper: the drm_fb_helper
1774 * Scan the connectors attached to the fb_helper and try to put together a
1775 * setup after *notification of a change in output configuration.
1777 * Called at runtime, takes the mode config locks to be able to check/change the
1778 * modeset configuration. Must be run from process context (which usually means
1779 * either the output polling work or a work item launched from the driver's
1780 * hotplug interrupt).
1782 * Note that drivers may call this even before calling
1783 * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
1784 * for a race-free fbcon setup and will make sure that the fbdev emulation will
1785 * not miss any hotplug events.
1788 * 0 on success and a non-zero error code otherwise.
1790 int drm_fb_helper_hotplug_event(struct drm_fb_helper
*fb_helper
)
1792 struct drm_device
*dev
= fb_helper
->dev
;
1793 u32 max_width
, max_height
;
1795 mutex_lock(&fb_helper
->dev
->mode_config
.mutex
);
1796 if (!fb_helper
->fb
|| !drm_fb_helper_is_bound(fb_helper
)) {
1797 fb_helper
->delayed_hotplug
= true;
1798 mutex_unlock(&fb_helper
->dev
->mode_config
.mutex
);
1801 DRM_DEBUG_KMS("\n");
1803 max_width
= fb_helper
->fb
->width
;
1804 max_height
= fb_helper
->fb
->height
;
1806 drm_fb_helper_probe_connector_modes(fb_helper
, max_width
, max_height
);
1807 mutex_unlock(&fb_helper
->dev
->mode_config
.mutex
);
1809 drm_modeset_lock_all(dev
);
1810 drm_setup_crtcs(fb_helper
);
1811 drm_modeset_unlock_all(dev
);
1812 drm_fb_helper_set_par(fb_helper
->fbdev
);
1816 EXPORT_SYMBOL(drm_fb_helper_hotplug_event
);
1818 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
1819 * but the module doesn't depend on any fb console symbols. At least
1820 * attempt to load fbcon to avoid leaving the system without a usable console.
1822 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
1823 static int __init
drm_fb_helper_modinit(void)
1825 const char *name
= "fbcon";
1826 struct module
*fbcon
;
1828 mutex_lock(&module_mutex
);
1829 fbcon
= find_module(name
);
1830 mutex_unlock(&module_mutex
);
1833 request_module_nowait(name
);
1837 module_init(drm_fb_helper_modinit
);