2 * Copyright (c) 2006-2009 Red Hat Inc.
3 * Copyright (c) 2006-2008 Intel Corporation
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
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #include <linux/console.h>
33 #include <linux/kernel.h>
34 #include <linux/sysrq.h>
35 #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>
41 #include <drm/drm_atomic.h>
42 #include <drm/drm_atomic_helper.h>
44 #include "drm_crtc_helper_internal.h"
46 static bool drm_fbdev_emulation = true;
47 module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
48 MODULE_PARM_DESC(fbdev_emulation,
49 "Enable legacy fbdev emulation [default=true]");
51 static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC;
52 module_param(drm_fbdev_overalloc, int, 0444);
53 MODULE_PARM_DESC(drm_fbdev_overalloc,
54 "Overallocation of the fbdev buffer (%) [default="
55 __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]");
57 static LIST_HEAD(kernel_fb_helper_list);
58 static DEFINE_MUTEX(kernel_fb_helper_lock);
63 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
64 * mode setting driver. They can be used mostly independently from the crtc
65 * helper functions used by many drivers to implement the kernel mode setting
68 * Initialization is done as a four-step process with drm_fb_helper_prepare(),
69 * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
70 * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
71 * default behaviour can override the third step with their own code.
72 * Teardown is done with drm_fb_helper_fini() after the fbdev device is
73 * unregisters using drm_fb_helper_unregister_fbi().
75 * At runtime drivers should restore the fbdev console by calling
76 * drm_fb_helper_restore_fbdev_mode_unlocked() from their &drm_driver.lastclose
77 * callback. They should also notify the fb helper code from updates to the
78 * output configuration by calling drm_fb_helper_hotplug_event(). For easier
79 * integration with the output polling code in drm_crtc_helper.c the modeset
80 * code provides a &drm_mode_config_funcs.output_poll_changed callback.
82 * All other functions exported by the fb helper library can be used to
83 * implement the fbdev driver interface by the driver.
85 * It is possible, though perhaps somewhat tricky, to implement race-free
86 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
87 * helper must be called first to initialize the minimum required to make
88 * hotplug detection work. Drivers also need to make sure to properly set up
89 * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
90 * it is safe to enable interrupts and start processing hotplug events. At the
91 * same time, drivers should initialize all modeset objects such as CRTCs,
92 * encoders and connectors. To finish up the fbdev helper initialization, the
93 * drm_fb_helper_init() function is called. To probe for all attached displays
94 * and set up an initial configuration using the detected hardware, drivers
95 * should call drm_fb_helper_single_add_all_connectors() followed by
96 * drm_fb_helper_initial_config().
98 * If &drm_framebuffer_funcs.dirty is set, the
99 * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
100 * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
101 * away. This worker then calls the dirty() function ensuring that it will
102 * always run in process context since the fb_*() function could be running in
103 * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
104 * callback it will also schedule dirty_work with the damage collected from the
108 #define drm_fb_helper_for_each_connector(fbh, i__) \
109 for (({ lockdep_assert_held(&(fbh)->lock); }), \
110 i__ = 0; i__ < (fbh)->connector_count; i__++)
112 static int __drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
113 struct drm_connector *connector)
115 struct drm_fb_helper_connector *fb_conn;
116 struct drm_fb_helper_connector **temp;
119 if (!drm_fbdev_emulation)
122 lockdep_assert_held(&fb_helper->lock);
124 count = fb_helper->connector_count + 1;
126 if (count > fb_helper->connector_info_alloc_count) {
127 size_t size = count * sizeof(fb_conn);
129 temp = krealloc(fb_helper->connector_info, size, GFP_KERNEL);
133 fb_helper->connector_info_alloc_count = count;
134 fb_helper->connector_info = temp;
137 fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL);
141 drm_connector_get(connector);
142 fb_conn->connector = connector;
143 fb_helper->connector_info[fb_helper->connector_count++] = fb_conn;
148 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
149 struct drm_connector *connector)
156 mutex_lock(&fb_helper->lock);
157 err = __drm_fb_helper_add_one_connector(fb_helper, connector);
158 mutex_unlock(&fb_helper->lock);
162 EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
165 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
167 * @fb_helper: fbdev initialized with drm_fb_helper_init, can be NULL
169 * This functions adds all the available connectors for use with the given
170 * fb_helper. This is a separate step to allow drivers to freely assign
171 * connectors to the fbdev, e.g. if some are reserved for special purposes or
172 * not adequate to be used for the fbcon.
174 * This function is protected against concurrent connector hotadds/removals
175 * using drm_fb_helper_add_one_connector() and
176 * drm_fb_helper_remove_one_connector().
178 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
180 struct drm_device *dev = fb_helper->dev;
181 struct drm_connector *connector;
182 struct drm_connector_list_iter conn_iter;
185 if (!drm_fbdev_emulation || !fb_helper)
188 mutex_lock(&fb_helper->lock);
189 drm_connector_list_iter_begin(dev, &conn_iter);
190 drm_for_each_connector_iter(connector, &conn_iter) {
191 ret = __drm_fb_helper_add_one_connector(fb_helper, connector);
198 drm_fb_helper_for_each_connector(fb_helper, i) {
199 struct drm_fb_helper_connector *fb_helper_connector =
200 fb_helper->connector_info[i];
202 drm_connector_put(fb_helper_connector->connector);
204 kfree(fb_helper_connector);
205 fb_helper->connector_info[i] = NULL;
207 fb_helper->connector_count = 0;
209 drm_connector_list_iter_end(&conn_iter);
210 mutex_unlock(&fb_helper->lock);
214 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
216 static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
217 struct drm_connector *connector)
219 struct drm_fb_helper_connector *fb_helper_connector;
222 if (!drm_fbdev_emulation)
225 lockdep_assert_held(&fb_helper->lock);
227 drm_fb_helper_for_each_connector(fb_helper, i) {
228 if (fb_helper->connector_info[i]->connector == connector)
232 if (i == fb_helper->connector_count)
234 fb_helper_connector = fb_helper->connector_info[i];
235 drm_connector_put(fb_helper_connector->connector);
237 for (j = i + 1; j < fb_helper->connector_count; j++)
238 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
240 fb_helper->connector_count--;
241 kfree(fb_helper_connector);
246 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
247 struct drm_connector *connector)
254 mutex_lock(&fb_helper->lock);
255 err = __drm_fb_helper_remove_one_connector(fb_helper, connector);
256 mutex_unlock(&fb_helper->lock);
260 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
262 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
264 uint16_t *r_base, *g_base, *b_base;
266 if (crtc->funcs->gamma_set == NULL)
269 r_base = crtc->gamma_store;
270 g_base = r_base + crtc->gamma_size;
271 b_base = g_base + crtc->gamma_size;
273 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
274 crtc->gamma_size, NULL);
278 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
279 * @info: fbdev registered by the helper
281 int drm_fb_helper_debug_enter(struct fb_info *info)
283 struct drm_fb_helper *helper = info->par;
284 const struct drm_crtc_helper_funcs *funcs;
287 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
288 for (i = 0; i < helper->crtc_count; i++) {
289 struct drm_mode_set *mode_set =
290 &helper->crtc_info[i].mode_set;
292 if (!mode_set->crtc->enabled)
295 funcs = mode_set->crtc->helper_private;
296 if (funcs->mode_set_base_atomic == NULL)
299 if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
302 funcs->mode_set_base_atomic(mode_set->crtc,
306 ENTER_ATOMIC_MODE_SET);
312 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
315 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
316 * @info: fbdev registered by the helper
318 int drm_fb_helper_debug_leave(struct fb_info *info)
320 struct drm_fb_helper *helper = info->par;
321 struct drm_crtc *crtc;
322 const struct drm_crtc_helper_funcs *funcs;
323 struct drm_framebuffer *fb;
326 for (i = 0; i < helper->crtc_count; i++) {
327 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
329 crtc = mode_set->crtc;
330 if (drm_drv_uses_atomic_modeset(crtc->dev))
333 funcs = crtc->helper_private;
334 fb = crtc->primary->fb;
340 DRM_ERROR("no fb to restore??\n");
344 if (funcs->mode_set_base_atomic == NULL)
347 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
348 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
349 crtc->y, LEAVE_ATOMIC_MODE_SET);
354 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
356 static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper, bool active)
358 struct drm_device *dev = fb_helper->dev;
359 struct drm_plane *plane;
360 struct drm_atomic_state *state;
362 unsigned int plane_mask;
363 struct drm_modeset_acquire_ctx ctx;
365 drm_modeset_acquire_init(&ctx, 0);
367 state = drm_atomic_state_alloc(dev);
373 state->acquire_ctx = &ctx;
376 drm_for_each_plane(plane, dev) {
377 struct drm_plane_state *plane_state;
379 plane_state = drm_atomic_get_plane_state(state, plane);
380 if (IS_ERR(plane_state)) {
381 ret = PTR_ERR(plane_state);
385 plane_state->rotation = DRM_MODE_ROTATE_0;
387 plane->old_fb = plane->fb;
388 plane_mask |= 1 << drm_plane_index(plane);
390 /* disable non-primary: */
391 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
394 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
399 for (i = 0; i < fb_helper->crtc_count; i++) {
400 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
402 ret = __drm_atomic_helper_set_config(mode_set, state);
407 * __drm_atomic_helper_set_config() sets active when a
408 * mode is set, unconditionally clear it if we force DPMS off
411 struct drm_crtc *crtc = mode_set->crtc;
412 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
414 crtc_state->active = false;
418 ret = drm_atomic_commit(state);
421 drm_atomic_clean_old_fb(dev, plane_mask, ret);
426 drm_atomic_state_put(state);
428 drm_modeset_drop_locks(&ctx);
429 drm_modeset_acquire_fini(&ctx);
434 drm_atomic_state_clear(state);
435 drm_modeset_backoff(&ctx);
440 static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper)
442 struct drm_device *dev = fb_helper->dev;
443 struct drm_plane *plane;
446 drm_modeset_lock_all(fb_helper->dev);
447 drm_for_each_plane(plane, dev) {
448 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
449 drm_plane_force_disable(plane);
451 if (plane->rotation_property)
452 drm_mode_plane_set_obj_prop(plane,
453 plane->rotation_property,
457 for (i = 0; i < fb_helper->crtc_count; i++) {
458 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
459 struct drm_crtc *crtc = mode_set->crtc;
461 if (crtc->funcs->cursor_set2) {
462 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
465 } else if (crtc->funcs->cursor_set) {
466 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
471 ret = drm_mode_set_config_internal(mode_set);
476 drm_modeset_unlock_all(fb_helper->dev);
481 static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
483 struct drm_device *dev = fb_helper->dev;
485 if (drm_drv_uses_atomic_modeset(dev))
486 return restore_fbdev_mode_atomic(fb_helper, true);
488 return restore_fbdev_mode_legacy(fb_helper);
492 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
493 * @fb_helper: driver-allocated fbdev helper, can be NULL
495 * This should be called from driver's drm &drm_driver.lastclose callback
496 * when implementing an fbcon on top of kms using this helper. This ensures that
497 * the user isn't greeted with a black screen when e.g. X dies.
500 * Zero if everything went ok, negative error code otherwise.
502 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
507 if (!drm_fbdev_emulation || !fb_helper)
510 if (READ_ONCE(fb_helper->deferred_setup))
513 mutex_lock(&fb_helper->lock);
514 ret = restore_fbdev_mode(fb_helper);
516 do_delayed = fb_helper->delayed_hotplug;
518 fb_helper->delayed_hotplug = false;
519 mutex_unlock(&fb_helper->lock);
522 drm_fb_helper_hotplug_event(fb_helper);
526 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
528 static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
530 struct drm_device *dev = fb_helper->dev;
531 struct drm_crtc *crtc;
532 int bound = 0, crtcs_bound = 0;
535 * Sometimes user space wants everything disabled, so don't steal the
536 * display if there's a master.
538 if (READ_ONCE(dev->master))
541 drm_for_each_crtc(crtc, dev) {
542 drm_modeset_lock(&crtc->mutex, NULL);
543 if (crtc->primary->fb)
545 if (crtc->primary->fb == fb_helper->fb)
547 drm_modeset_unlock(&crtc->mutex);
550 if (bound < crtcs_bound)
556 #ifdef CONFIG_MAGIC_SYSRQ
558 * restore fbcon display for all kms driver's using this helper, used for sysrq
559 * and panic handling.
561 static bool drm_fb_helper_force_kernel_mode(void)
563 bool ret, error = false;
564 struct drm_fb_helper *helper;
566 if (list_empty(&kernel_fb_helper_list))
569 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
570 struct drm_device *dev = helper->dev;
572 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
575 mutex_lock(&helper->lock);
576 ret = restore_fbdev_mode(helper);
579 mutex_unlock(&helper->lock);
584 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
588 ret = drm_fb_helper_force_kernel_mode();
590 DRM_ERROR("Failed to restore crtc configuration\n");
592 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
594 static void drm_fb_helper_sysrq(int dummy1)
596 schedule_work(&drm_fb_helper_restore_work);
599 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
600 .handler = drm_fb_helper_sysrq,
601 .help_msg = "force-fb(V)",
602 .action_msg = "Restore framebuffer console",
605 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
608 static void dpms_legacy(struct drm_fb_helper *fb_helper, int dpms_mode)
610 struct drm_device *dev = fb_helper->dev;
611 struct drm_crtc *crtc;
612 struct drm_connector *connector;
615 drm_modeset_lock_all(dev);
616 for (i = 0; i < fb_helper->crtc_count; i++) {
617 crtc = fb_helper->crtc_info[i].mode_set.crtc;
622 /* Walk the connectors & encoders on this fb turning them on/off */
623 drm_fb_helper_for_each_connector(fb_helper, j) {
624 connector = fb_helper->connector_info[j]->connector;
625 connector->funcs->dpms(connector, dpms_mode);
626 drm_object_property_set_value(&connector->base,
627 dev->mode_config.dpms_property, dpms_mode);
630 drm_modeset_unlock_all(dev);
633 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
635 struct drm_fb_helper *fb_helper = info->par;
638 * For each CRTC in this fb, turn the connectors on/off.
640 mutex_lock(&fb_helper->lock);
641 if (!drm_fb_helper_is_bound(fb_helper)) {
642 mutex_unlock(&fb_helper->lock);
646 if (drm_drv_uses_atomic_modeset(fb_helper->dev))
647 restore_fbdev_mode_atomic(fb_helper, dpms_mode == DRM_MODE_DPMS_ON);
649 dpms_legacy(fb_helper, dpms_mode);
650 mutex_unlock(&fb_helper->lock);
654 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
655 * @blank: desired blanking state
656 * @info: fbdev registered by the helper
658 int drm_fb_helper_blank(int blank, struct fb_info *info)
660 if (oops_in_progress)
664 /* Display: On; HSync: On, VSync: On */
665 case FB_BLANK_UNBLANK:
666 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
668 /* Display: Off; HSync: On, VSync: On */
669 case FB_BLANK_NORMAL:
670 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
672 /* Display: Off; HSync: Off, VSync: On */
673 case FB_BLANK_HSYNC_SUSPEND:
674 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
676 /* Display: Off; HSync: On, VSync: Off */
677 case FB_BLANK_VSYNC_SUSPEND:
678 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
680 /* Display: Off; HSync: Off, VSync: Off */
681 case FB_BLANK_POWERDOWN:
682 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
687 EXPORT_SYMBOL(drm_fb_helper_blank);
689 static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper,
690 struct drm_mode_set *modeset)
694 for (i = 0; i < modeset->num_connectors; i++) {
695 drm_connector_put(modeset->connectors[i]);
696 modeset->connectors[i] = NULL;
698 modeset->num_connectors = 0;
700 drm_mode_destroy(helper->dev, modeset->mode);
701 modeset->mode = NULL;
703 /* FIXME should hold a ref? */
707 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
711 for (i = 0; i < helper->connector_count; i++) {
712 drm_connector_put(helper->connector_info[i]->connector);
713 kfree(helper->connector_info[i]);
715 kfree(helper->connector_info);
717 for (i = 0; i < helper->crtc_count; i++) {
718 struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set;
720 drm_fb_helper_modeset_release(helper, modeset);
721 kfree(modeset->connectors);
723 kfree(helper->crtc_info);
726 static void drm_fb_helper_resume_worker(struct work_struct *work)
728 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
732 fb_set_suspend(helper->fbdev, 0);
736 static void drm_fb_helper_dirty_work(struct work_struct *work)
738 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
740 struct drm_clip_rect *clip = &helper->dirty_clip;
741 struct drm_clip_rect clip_copy;
744 spin_lock_irqsave(&helper->dirty_lock, flags);
746 clip->x1 = clip->y1 = ~0;
747 clip->x2 = clip->y2 = 0;
748 spin_unlock_irqrestore(&helper->dirty_lock, flags);
750 /* call dirty callback only when it has been really touched */
751 if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2)
752 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
756 * drm_fb_helper_prepare - setup a drm_fb_helper structure
758 * @helper: driver-allocated fbdev helper structure to set up
759 * @funcs: pointer to structure of functions associate with this helper
761 * Sets up the bare minimum to make the framebuffer helper usable. This is
762 * useful to implement race-free initialization of the polling helpers.
764 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
765 const struct drm_fb_helper_funcs *funcs)
767 INIT_LIST_HEAD(&helper->kernel_fb_list);
768 spin_lock_init(&helper->dirty_lock);
769 INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
770 INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
771 helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
772 mutex_init(&helper->lock);
773 helper->funcs = funcs;
776 EXPORT_SYMBOL(drm_fb_helper_prepare);
779 * drm_fb_helper_init - initialize a &struct drm_fb_helper
781 * @fb_helper: driver-allocated fbdev helper structure to initialize
782 * @max_conn_count: max connector count
784 * This allocates the structures for the fbdev helper with the given limits.
785 * Note that this won't yet touch the hardware (through the driver interfaces)
786 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
787 * to allow driver writes more control over the exact init sequence.
789 * Drivers must call drm_fb_helper_prepare() before calling this function.
792 * Zero if everything went ok, nonzero otherwise.
794 int drm_fb_helper_init(struct drm_device *dev,
795 struct drm_fb_helper *fb_helper,
798 struct drm_crtc *crtc;
799 struct drm_mode_config *config = &dev->mode_config;
802 if (!drm_fbdev_emulation) {
803 dev->fb_helper = fb_helper;
810 fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
811 if (!fb_helper->crtc_info)
814 fb_helper->crtc_count = config->num_crtc;
815 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
816 if (!fb_helper->connector_info) {
817 kfree(fb_helper->crtc_info);
820 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
821 fb_helper->connector_count = 0;
823 for (i = 0; i < fb_helper->crtc_count; i++) {
824 fb_helper->crtc_info[i].mode_set.connectors =
825 kcalloc(max_conn_count,
826 sizeof(struct drm_connector *),
829 if (!fb_helper->crtc_info[i].mode_set.connectors)
831 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
835 drm_for_each_crtc(crtc, dev) {
836 fb_helper->crtc_info[i].mode_set.crtc = crtc;
840 dev->fb_helper = fb_helper;
844 drm_fb_helper_crtc_free(fb_helper);
847 EXPORT_SYMBOL(drm_fb_helper_init);
850 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
851 * @fb_helper: driver-allocated fbdev helper
853 * A helper to alloc fb_info and the members cmap and apertures. Called
854 * by the driver within the fb_probe fb_helper callback function. Drivers do not
855 * need to release the allocated fb_info structure themselves, this is
856 * automatically done when calling drm_fb_helper_fini().
859 * fb_info pointer if things went okay, pointer containing error code
862 struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
864 struct device *dev = fb_helper->dev->dev;
865 struct fb_info *info;
868 info = framebuffer_alloc(0, dev);
870 return ERR_PTR(-ENOMEM);
872 ret = fb_alloc_cmap(&info->cmap, 256, 0);
876 info->apertures = alloc_apertures(1);
877 if (!info->apertures) {
882 fb_helper->fbdev = info;
887 fb_dealloc_cmap(&info->cmap);
889 framebuffer_release(info);
892 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
895 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
896 * @fb_helper: driver-allocated fbdev helper, can be NULL
898 * A wrapper around unregister_framebuffer, to release the fb_info
899 * framebuffer device. This must be called before releasing all resources for
900 * @fb_helper by calling drm_fb_helper_fini().
902 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
904 if (fb_helper && fb_helper->fbdev)
905 unregister_framebuffer(fb_helper->fbdev);
907 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
910 * drm_fb_helper_fini - finialize a &struct drm_fb_helper
911 * @fb_helper: driver-allocated fbdev helper, can be NULL
913 * This cleans up all remaining resources associated with @fb_helper. Must be
914 * called after drm_fb_helper_unlink_fbi() was called.
916 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
918 struct fb_info *info;
923 fb_helper->dev->fb_helper = NULL;
925 if (!drm_fbdev_emulation)
928 cancel_work_sync(&fb_helper->resume_work);
929 cancel_work_sync(&fb_helper->dirty_work);
931 info = fb_helper->fbdev;
934 fb_dealloc_cmap(&info->cmap);
935 framebuffer_release(info);
937 fb_helper->fbdev = NULL;
939 mutex_lock(&kernel_fb_helper_lock);
940 if (!list_empty(&fb_helper->kernel_fb_list)) {
941 list_del(&fb_helper->kernel_fb_list);
942 if (list_empty(&kernel_fb_helper_list))
943 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
945 mutex_unlock(&kernel_fb_helper_lock);
947 mutex_destroy(&fb_helper->lock);
948 drm_fb_helper_crtc_free(fb_helper);
951 EXPORT_SYMBOL(drm_fb_helper_fini);
954 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
955 * @fb_helper: driver-allocated fbdev helper, can be NULL
957 * A wrapper around unlink_framebuffer implemented by fbdev core
959 void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
961 if (fb_helper && fb_helper->fbdev)
962 unlink_framebuffer(fb_helper->fbdev);
964 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
966 static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
967 u32 width, u32 height)
969 struct drm_fb_helper *helper = info->par;
970 struct drm_clip_rect *clip = &helper->dirty_clip;
973 if (!helper->fb->funcs->dirty)
976 spin_lock_irqsave(&helper->dirty_lock, flags);
977 clip->x1 = min_t(u32, clip->x1, x);
978 clip->y1 = min_t(u32, clip->y1, y);
979 clip->x2 = max_t(u32, clip->x2, x + width);
980 clip->y2 = max_t(u32, clip->y2, y + height);
981 spin_unlock_irqrestore(&helper->dirty_lock, flags);
983 schedule_work(&helper->dirty_work);
987 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
988 * @info: fb_info struct pointer
989 * @pagelist: list of dirty mmap framebuffer pages
991 * This function is used as the &fb_deferred_io.deferred_io
992 * callback function for flushing the fbdev mmap writes.
994 void drm_fb_helper_deferred_io(struct fb_info *info,
995 struct list_head *pagelist)
997 unsigned long start, end, min, max;
1003 list_for_each_entry(page, pagelist, lru) {
1004 start = page->index << PAGE_SHIFT;
1005 end = start + PAGE_SIZE - 1;
1006 min = min(min, start);
1007 max = max(max, end);
1011 y1 = min / info->fix.line_length;
1012 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
1014 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
1017 EXPORT_SYMBOL(drm_fb_helper_deferred_io);
1020 * drm_fb_helper_sys_read - wrapper around fb_sys_read
1021 * @info: fb_info struct pointer
1022 * @buf: userspace buffer to read from framebuffer memory
1023 * @count: number of bytes to read from framebuffer memory
1024 * @ppos: read offset within framebuffer memory
1026 * A wrapper around fb_sys_read implemented by fbdev core
1028 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
1029 size_t count, loff_t *ppos)
1031 return fb_sys_read(info, buf, count, ppos);
1033 EXPORT_SYMBOL(drm_fb_helper_sys_read);
1036 * drm_fb_helper_sys_write - wrapper around fb_sys_write
1037 * @info: fb_info struct pointer
1038 * @buf: userspace buffer to write to framebuffer memory
1039 * @count: number of bytes to write to framebuffer memory
1040 * @ppos: write offset within framebuffer memory
1042 * A wrapper around fb_sys_write implemented by fbdev core
1044 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
1045 size_t count, loff_t *ppos)
1049 ret = fb_sys_write(info, buf, count, ppos);
1051 drm_fb_helper_dirty(info, 0, 0, info->var.xres,
1056 EXPORT_SYMBOL(drm_fb_helper_sys_write);
1059 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
1060 * @info: fbdev registered by the helper
1061 * @rect: info about rectangle to fill
1063 * A wrapper around sys_fillrect implemented by fbdev core
1065 void drm_fb_helper_sys_fillrect(struct fb_info *info,
1066 const struct fb_fillrect *rect)
1068 sys_fillrect(info, rect);
1069 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1070 rect->width, rect->height);
1072 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
1075 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1076 * @info: fbdev registered by the helper
1077 * @area: info about area to copy
1079 * A wrapper around sys_copyarea implemented by fbdev core
1081 void drm_fb_helper_sys_copyarea(struct fb_info *info,
1082 const struct fb_copyarea *area)
1084 sys_copyarea(info, area);
1085 drm_fb_helper_dirty(info, area->dx, area->dy,
1086 area->width, area->height);
1088 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
1091 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1092 * @info: fbdev registered by the helper
1093 * @image: info about image to blit
1095 * A wrapper around sys_imageblit implemented by fbdev core
1097 void drm_fb_helper_sys_imageblit(struct fb_info *info,
1098 const struct fb_image *image)
1100 sys_imageblit(info, image);
1101 drm_fb_helper_dirty(info, image->dx, image->dy,
1102 image->width, image->height);
1104 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
1107 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1108 * @info: fbdev registered by the helper
1109 * @rect: info about rectangle to fill
1111 * A wrapper around cfb_imageblit implemented by fbdev core
1113 void drm_fb_helper_cfb_fillrect(struct fb_info *info,
1114 const struct fb_fillrect *rect)
1116 cfb_fillrect(info, rect);
1117 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1118 rect->width, rect->height);
1120 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
1123 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1124 * @info: fbdev registered by the helper
1125 * @area: info about area to copy
1127 * A wrapper around cfb_copyarea implemented by fbdev core
1129 void drm_fb_helper_cfb_copyarea(struct fb_info *info,
1130 const struct fb_copyarea *area)
1132 cfb_copyarea(info, area);
1133 drm_fb_helper_dirty(info, area->dx, area->dy,
1134 area->width, area->height);
1136 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
1139 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1140 * @info: fbdev registered by the helper
1141 * @image: info about image to blit
1143 * A wrapper around cfb_imageblit implemented by fbdev core
1145 void drm_fb_helper_cfb_imageblit(struct fb_info *info,
1146 const struct fb_image *image)
1148 cfb_imageblit(info, image);
1149 drm_fb_helper_dirty(info, image->dx, image->dy,
1150 image->width, image->height);
1152 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
1155 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
1156 * @fb_helper: driver-allocated fbdev helper, can be NULL
1157 * @suspend: whether to suspend or resume
1159 * A wrapper around fb_set_suspend implemented by fbdev core.
1160 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
1163 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
1165 if (fb_helper && fb_helper->fbdev)
1166 fb_set_suspend(fb_helper->fbdev, suspend);
1168 EXPORT_SYMBOL(drm_fb_helper_set_suspend);
1171 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
1172 * takes the console lock
1173 * @fb_helper: driver-allocated fbdev helper, can be NULL
1174 * @suspend: whether to suspend or resume
1176 * A wrapper around fb_set_suspend() that takes the console lock. If the lock
1177 * isn't available on resume, a worker is tasked with waiting for the lock
1178 * to become available. The console lock can be pretty contented on resume
1179 * due to all the printk activity.
1181 * This function can be called multiple times with the same state since
1182 * &fb_info.state is checked to see if fbdev is running or not before locking.
1184 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
1186 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
1189 if (!fb_helper || !fb_helper->fbdev)
1192 /* make sure there's no pending/ongoing resume */
1193 flush_work(&fb_helper->resume_work);
1196 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
1202 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
1205 if (!console_trylock()) {
1206 schedule_work(&fb_helper->resume_work);
1211 fb_set_suspend(fb_helper->fbdev, suspend);
1214 EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
1216 static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info)
1218 u32 *palette = (u32 *)info->pseudo_palette;
1221 if (cmap->start + cmap->len > 16)
1224 for (i = 0; i < cmap->len; ++i) {
1225 u16 red = cmap->red[i];
1226 u16 green = cmap->green[i];
1227 u16 blue = cmap->blue[i];
1230 red >>= 16 - info->var.red.length;
1231 green >>= 16 - info->var.green.length;
1232 blue >>= 16 - info->var.blue.length;
1233 value = (red << info->var.red.offset) |
1234 (green << info->var.green.offset) |
1235 (blue << info->var.blue.offset);
1236 if (info->var.transp.length > 0) {
1237 u32 mask = (1 << info->var.transp.length) - 1;
1239 mask <<= info->var.transp.offset;
1242 palette[cmap->start + i] = value;
1248 static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info)
1250 struct drm_fb_helper *fb_helper = info->par;
1251 struct drm_crtc *crtc;
1255 drm_modeset_lock_all(fb_helper->dev);
1256 for (i = 0; i < fb_helper->crtc_count; i++) {
1257 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1258 if (!crtc->funcs->gamma_set || !crtc->gamma_size)
1261 if (cmap->start + cmap->len > crtc->gamma_size)
1264 r = crtc->gamma_store;
1265 g = r + crtc->gamma_size;
1266 b = g + crtc->gamma_size;
1268 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1269 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1270 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1272 ret = crtc->funcs->gamma_set(crtc, r, g, b,
1273 crtc->gamma_size, NULL);
1277 drm_modeset_unlock_all(fb_helper->dev);
1282 static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc,
1283 struct fb_cmap *cmap)
1285 struct drm_device *dev = crtc->dev;
1286 struct drm_property_blob *gamma_lut;
1287 struct drm_color_lut *lut;
1288 int size = crtc->gamma_size;
1291 if (!size || cmap->start + cmap->len > size)
1292 return ERR_PTR(-EINVAL);
1294 gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL);
1295 if (IS_ERR(gamma_lut))
1298 lut = (struct drm_color_lut *)gamma_lut->data;
1299 if (cmap->start || cmap->len != size) {
1300 u16 *r = crtc->gamma_store;
1301 u16 *g = r + crtc->gamma_size;
1302 u16 *b = g + crtc->gamma_size;
1304 for (i = 0; i < cmap->start; i++) {
1306 lut[i].green = g[i];
1309 for (i = cmap->start + cmap->len; i < size; i++) {
1311 lut[i].green = g[i];
1316 for (i = 0; i < cmap->len; i++) {
1317 lut[cmap->start + i].red = cmap->red[i];
1318 lut[cmap->start + i].green = cmap->green[i];
1319 lut[cmap->start + i].blue = cmap->blue[i];
1325 static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info)
1327 struct drm_fb_helper *fb_helper = info->par;
1328 struct drm_device *dev = fb_helper->dev;
1329 struct drm_property_blob *gamma_lut = NULL;
1330 struct drm_modeset_acquire_ctx ctx;
1331 struct drm_crtc_state *crtc_state;
1332 struct drm_atomic_state *state;
1333 struct drm_crtc *crtc;
1338 drm_modeset_acquire_init(&ctx, 0);
1340 state = drm_atomic_state_alloc(dev);
1346 state->acquire_ctx = &ctx;
1348 for (i = 0; i < fb_helper->crtc_count; i++) {
1349 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1352 gamma_lut = setcmap_new_gamma_lut(crtc, cmap);
1353 if (IS_ERR(gamma_lut)) {
1354 ret = PTR_ERR(gamma_lut);
1359 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1360 if (IS_ERR(crtc_state)) {
1361 ret = PTR_ERR(crtc_state);
1365 replaced = drm_property_replace_blob(&crtc_state->degamma_lut,
1367 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
1368 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
1370 crtc_state->color_mgmt_changed |= replaced;
1373 ret = drm_atomic_commit(state);
1377 for (i = 0; i < fb_helper->crtc_count; i++) {
1378 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1380 r = crtc->gamma_store;
1381 g = r + crtc->gamma_size;
1382 b = g + crtc->gamma_size;
1384 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1385 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1386 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1390 if (ret == -EDEADLK)
1393 drm_property_blob_put(gamma_lut);
1394 drm_atomic_state_put(state);
1396 drm_modeset_drop_locks(&ctx);
1397 drm_modeset_acquire_fini(&ctx);
1402 drm_atomic_state_clear(state);
1403 drm_modeset_backoff(&ctx);
1408 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
1409 * @cmap: cmap to set
1410 * @info: fbdev registered by the helper
1412 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1414 struct drm_fb_helper *fb_helper = info->par;
1417 if (oops_in_progress)
1420 mutex_lock(&fb_helper->lock);
1422 if (!drm_fb_helper_is_bound(fb_helper)) {
1427 if (info->fix.visual == FB_VISUAL_TRUECOLOR)
1428 ret = setcmap_pseudo_palette(cmap, info);
1429 else if (drm_drv_uses_atomic_modeset(fb_helper->dev))
1430 ret = setcmap_atomic(cmap, info);
1432 ret = setcmap_legacy(cmap, info);
1435 mutex_unlock(&fb_helper->lock);
1439 EXPORT_SYMBOL(drm_fb_helper_setcmap);
1442 * drm_fb_helper_ioctl - legacy ioctl implementation
1443 * @info: fbdev registered by the helper
1444 * @cmd: ioctl command
1445 * @arg: ioctl argument
1447 * A helper to implement the standard fbdev ioctl. Only
1448 * FBIO_WAITFORVSYNC is implemented for now.
1450 int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
1453 struct drm_fb_helper *fb_helper = info->par;
1454 struct drm_mode_set *mode_set;
1455 struct drm_crtc *crtc;
1458 mutex_lock(&fb_helper->lock);
1459 if (!drm_fb_helper_is_bound(fb_helper)) {
1465 case FBIO_WAITFORVSYNC:
1467 * Only consider the first CRTC.
1469 * This ioctl is supposed to take the CRTC number as
1470 * an argument, but in fbdev times, what that number
1471 * was supposed to be was quite unclear, different
1472 * drivers were passing that argument differently
1473 * (some by reference, some by value), and most of the
1474 * userspace applications were just hardcoding 0 as an
1477 * The first CRTC should be the integrated panel on
1478 * most drivers, so this is the best choice we can
1479 * make. If we're not smart enough here, one should
1480 * just consider switch the userspace to KMS.
1482 mode_set = &fb_helper->crtc_info[0].mode_set;
1483 crtc = mode_set->crtc;
1486 * Only wait for a vblank event if the CRTC is
1487 * enabled, otherwise just don't do anythintg,
1488 * not even report an error.
1490 ret = drm_crtc_vblank_get(crtc);
1492 drm_crtc_wait_one_vblank(crtc);
1493 drm_crtc_vblank_put(crtc);
1503 mutex_unlock(&fb_helper->lock);
1506 EXPORT_SYMBOL(drm_fb_helper_ioctl);
1509 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
1510 * @var: screeninfo to check
1511 * @info: fbdev registered by the helper
1513 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1514 struct fb_info *info)
1516 struct drm_fb_helper *fb_helper = info->par;
1517 struct drm_framebuffer *fb = fb_helper->fb;
1520 if (var->pixclock != 0 || in_dbg_master())
1524 * Changes struct fb_var_screeninfo are currently not pushed back
1525 * to KMS, hence fail if different settings are requested.
1527 if (var->bits_per_pixel != fb->format->cpp[0] * 8 ||
1528 var->xres > fb->width || var->yres > fb->height ||
1529 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1530 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
1531 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1532 var->xres, var->yres, var->bits_per_pixel,
1533 var->xres_virtual, var->yres_virtual,
1534 fb->width, fb->height, fb->format->cpp[0] * 8);
1538 switch (var->bits_per_pixel) {
1540 depth = (var->green.length == 6) ? 16 : 15;
1543 depth = (var->transp.length > 0) ? 32 : 24;
1546 depth = var->bits_per_pixel;
1552 var->red.offset = 0;
1553 var->green.offset = 0;
1554 var->blue.offset = 0;
1555 var->red.length = 8;
1556 var->green.length = 8;
1557 var->blue.length = 8;
1558 var->transp.length = 0;
1559 var->transp.offset = 0;
1562 var->red.offset = 10;
1563 var->green.offset = 5;
1564 var->blue.offset = 0;
1565 var->red.length = 5;
1566 var->green.length = 5;
1567 var->blue.length = 5;
1568 var->transp.length = 1;
1569 var->transp.offset = 15;
1572 var->red.offset = 11;
1573 var->green.offset = 5;
1574 var->blue.offset = 0;
1575 var->red.length = 5;
1576 var->green.length = 6;
1577 var->blue.length = 5;
1578 var->transp.length = 0;
1579 var->transp.offset = 0;
1582 var->red.offset = 16;
1583 var->green.offset = 8;
1584 var->blue.offset = 0;
1585 var->red.length = 8;
1586 var->green.length = 8;
1587 var->blue.length = 8;
1588 var->transp.length = 0;
1589 var->transp.offset = 0;
1592 var->red.offset = 16;
1593 var->green.offset = 8;
1594 var->blue.offset = 0;
1595 var->red.length = 8;
1596 var->green.length = 8;
1597 var->blue.length = 8;
1598 var->transp.length = 8;
1599 var->transp.offset = 24;
1606 EXPORT_SYMBOL(drm_fb_helper_check_var);
1609 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
1610 * @info: fbdev registered by the helper
1612 * This will let fbcon do the mode init and is called at initialization time by
1613 * the fbdev core when registering the driver, and later on through the hotplug
1616 int drm_fb_helper_set_par(struct fb_info *info)
1618 struct drm_fb_helper *fb_helper = info->par;
1619 struct fb_var_screeninfo *var = &info->var;
1621 if (oops_in_progress)
1624 if (var->pixclock != 0) {
1625 DRM_ERROR("PIXEL CLOCK SET\n");
1629 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
1633 EXPORT_SYMBOL(drm_fb_helper_set_par);
1635 static void pan_set(struct drm_fb_helper *fb_helper, int x, int y)
1639 for (i = 0; i < fb_helper->crtc_count; i++) {
1640 struct drm_mode_set *mode_set;
1642 mode_set = &fb_helper->crtc_info[i].mode_set;
1649 static int pan_display_atomic(struct fb_var_screeninfo *var,
1650 struct fb_info *info)
1652 struct drm_fb_helper *fb_helper = info->par;
1655 pan_set(fb_helper, var->xoffset, var->yoffset);
1657 ret = restore_fbdev_mode_atomic(fb_helper, true);
1659 info->var.xoffset = var->xoffset;
1660 info->var.yoffset = var->yoffset;
1662 pan_set(fb_helper, info->var.xoffset, info->var.yoffset);
1667 static int pan_display_legacy(struct fb_var_screeninfo *var,
1668 struct fb_info *info)
1670 struct drm_fb_helper *fb_helper = info->par;
1671 struct drm_mode_set *modeset;
1675 drm_modeset_lock_all(fb_helper->dev);
1676 for (i = 0; i < fb_helper->crtc_count; i++) {
1677 modeset = &fb_helper->crtc_info[i].mode_set;
1679 modeset->x = var->xoffset;
1680 modeset->y = var->yoffset;
1682 if (modeset->num_connectors) {
1683 ret = drm_mode_set_config_internal(modeset);
1685 info->var.xoffset = var->xoffset;
1686 info->var.yoffset = var->yoffset;
1690 drm_modeset_unlock_all(fb_helper->dev);
1696 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1697 * @var: updated screen information
1698 * @info: fbdev registered by the helper
1700 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1701 struct fb_info *info)
1703 struct drm_fb_helper *fb_helper = info->par;
1704 struct drm_device *dev = fb_helper->dev;
1707 if (oops_in_progress)
1710 mutex_lock(&fb_helper->lock);
1711 if (!drm_fb_helper_is_bound(fb_helper)) {
1712 mutex_unlock(&fb_helper->lock);
1716 if (drm_drv_uses_atomic_modeset(dev))
1717 ret = pan_display_atomic(var, info);
1719 ret = pan_display_legacy(var, info);
1720 mutex_unlock(&fb_helper->lock);
1724 EXPORT_SYMBOL(drm_fb_helper_pan_display);
1727 * Allocates the backing storage and sets up the fbdev info structure through
1728 * the ->fb_probe callback.
1730 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1736 struct drm_fb_helper_surface_size sizes;
1739 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1740 sizes.surface_depth = 24;
1741 sizes.surface_bpp = 32;
1742 sizes.fb_width = (u32)-1;
1743 sizes.fb_height = (u32)-1;
1745 /* if driver picks 8 or 16 by default use that for both depth/bpp */
1746 if (preferred_bpp != sizes.surface_bpp)
1747 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
1749 /* first up get a count of crtcs now in use and new min/maxes width/heights */
1750 drm_fb_helper_for_each_connector(fb_helper, i) {
1751 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
1752 struct drm_cmdline_mode *cmdline_mode;
1754 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1756 if (cmdline_mode->bpp_specified) {
1757 switch (cmdline_mode->bpp) {
1759 sizes.surface_depth = sizes.surface_bpp = 8;
1762 sizes.surface_depth = 15;
1763 sizes.surface_bpp = 16;
1766 sizes.surface_depth = sizes.surface_bpp = 16;
1769 sizes.surface_depth = sizes.surface_bpp = 24;
1772 sizes.surface_depth = 24;
1773 sizes.surface_bpp = 32;
1781 for (i = 0; i < fb_helper->crtc_count; i++) {
1782 struct drm_display_mode *desired_mode;
1783 struct drm_mode_set *mode_set;
1785 /* in case of tile group, are we the last tile vert or horiz?
1786 * If no tile group you are always the last one both vertically
1789 bool lastv = true, lasth = true;
1791 desired_mode = fb_helper->crtc_info[i].desired_mode;
1792 mode_set = &fb_helper->crtc_info[i].mode_set;
1799 x = fb_helper->crtc_info[i].x;
1800 y = fb_helper->crtc_info[i].y;
1802 if (gamma_size == 0)
1803 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1805 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1806 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
1808 for (j = 0; j < mode_set->num_connectors; j++) {
1809 struct drm_connector *connector = mode_set->connectors[j];
1811 if (connector->has_tile) {
1812 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1813 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1814 /* cloning to multiple tiles is just crazy-talk, so: */
1820 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1822 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
1825 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
1826 DRM_INFO("Cannot find any crtc or sizes\n");
1828 /* First time: disable all crtc's.. */
1829 if (!fb_helper->deferred_setup && !READ_ONCE(fb_helper->dev->master))
1830 restore_fbdev_mode(fb_helper);
1834 /* Handle our overallocation */
1835 sizes.surface_height *= drm_fbdev_overalloc;
1836 sizes.surface_height /= 100;
1838 /* push down into drivers */
1839 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1847 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1848 * @info: fbdev registered by the helper
1849 * @pitch: desired pitch
1850 * @depth: desired depth
1852 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1853 * fbdev emulations. Drivers which support acceleration methods which impose
1854 * additional constraints need to set up their own limits.
1856 * Drivers should call this (or their equivalent setup code) from their
1857 * &drm_fb_helper_funcs.fb_probe callback.
1859 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1862 info->fix.type = FB_TYPE_PACKED_PIXELS;
1863 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1864 FB_VISUAL_TRUECOLOR;
1865 info->fix.mmio_start = 0;
1866 info->fix.mmio_len = 0;
1867 info->fix.type_aux = 0;
1868 info->fix.xpanstep = 1; /* doing it in hw */
1869 info->fix.ypanstep = 1; /* doing it in hw */
1870 info->fix.ywrapstep = 0;
1871 info->fix.accel = FB_ACCEL_NONE;
1873 info->fix.line_length = pitch;
1875 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1878 * drm_fb_helper_fill_var - initalizes variable fbdev information
1879 * @info: fbdev instance to set up
1880 * @fb_helper: fb helper instance to use as template
1881 * @fb_width: desired fb width
1882 * @fb_height: desired fb height
1884 * Sets up the variable fbdev metainformation from the given fb helper instance
1885 * and the drm framebuffer allocated in &drm_fb_helper.fb.
1887 * Drivers should call this (or their equivalent setup code) from their
1888 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
1889 * backing storage framebuffer.
1891 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1892 uint32_t fb_width, uint32_t fb_height)
1894 struct drm_framebuffer *fb = fb_helper->fb;
1896 info->pseudo_palette = fb_helper->pseudo_palette;
1897 info->var.xres_virtual = fb->width;
1898 info->var.yres_virtual = fb->height;
1899 info->var.bits_per_pixel = fb->format->cpp[0] * 8;
1900 info->var.accel_flags = FB_ACCELF_TEXT;
1901 info->var.xoffset = 0;
1902 info->var.yoffset = 0;
1903 info->var.activate = FB_ACTIVATE_NOW;
1905 switch (fb->format->depth) {
1907 info->var.red.offset = 0;
1908 info->var.green.offset = 0;
1909 info->var.blue.offset = 0;
1910 info->var.red.length = 8; /* 8bit DAC */
1911 info->var.green.length = 8;
1912 info->var.blue.length = 8;
1913 info->var.transp.offset = 0;
1914 info->var.transp.length = 0;
1917 info->var.red.offset = 10;
1918 info->var.green.offset = 5;
1919 info->var.blue.offset = 0;
1920 info->var.red.length = 5;
1921 info->var.green.length = 5;
1922 info->var.blue.length = 5;
1923 info->var.transp.offset = 15;
1924 info->var.transp.length = 1;
1927 info->var.red.offset = 11;
1928 info->var.green.offset = 5;
1929 info->var.blue.offset = 0;
1930 info->var.red.length = 5;
1931 info->var.green.length = 6;
1932 info->var.blue.length = 5;
1933 info->var.transp.offset = 0;
1936 info->var.red.offset = 16;
1937 info->var.green.offset = 8;
1938 info->var.blue.offset = 0;
1939 info->var.red.length = 8;
1940 info->var.green.length = 8;
1941 info->var.blue.length = 8;
1942 info->var.transp.offset = 0;
1943 info->var.transp.length = 0;
1946 info->var.red.offset = 16;
1947 info->var.green.offset = 8;
1948 info->var.blue.offset = 0;
1949 info->var.red.length = 8;
1950 info->var.green.length = 8;
1951 info->var.blue.length = 8;
1952 info->var.transp.offset = 24;
1953 info->var.transp.length = 8;
1959 info->var.xres = fb_width;
1960 info->var.yres = fb_height;
1962 EXPORT_SYMBOL(drm_fb_helper_fill_var);
1964 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1968 struct drm_connector *connector;
1971 drm_fb_helper_for_each_connector(fb_helper, i) {
1972 connector = fb_helper->connector_info[i]->connector;
1973 count += connector->funcs->fill_modes(connector, maxX, maxY);
1979 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1981 struct drm_display_mode *mode;
1983 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1984 if (mode->hdisplay > width ||
1985 mode->vdisplay > height)
1987 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1992 EXPORT_SYMBOL(drm_has_preferred_mode);
1994 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1996 return fb_connector->connector->cmdline_mode.specified;
1999 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn)
2001 struct drm_cmdline_mode *cmdline_mode;
2002 struct drm_display_mode *mode;
2003 bool prefer_non_interlace;
2005 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
2006 if (cmdline_mode->specified == false)
2009 /* attempt to find a matching mode in the list of modes
2010 * we have gotten so far, if not add a CVT mode that conforms
2012 if (cmdline_mode->rb || cmdline_mode->margins)
2015 prefer_non_interlace = !cmdline_mode->interlace;
2017 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
2018 /* check width/height */
2019 if (mode->hdisplay != cmdline_mode->xres ||
2020 mode->vdisplay != cmdline_mode->yres)
2023 if (cmdline_mode->refresh_specified) {
2024 if (mode->vrefresh != cmdline_mode->refresh)
2028 if (cmdline_mode->interlace) {
2029 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
2031 } else if (prefer_non_interlace) {
2032 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2038 if (prefer_non_interlace) {
2039 prefer_non_interlace = false;
2044 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
2046 list_add(&mode->head, &fb_helper_conn->connector->modes);
2049 EXPORT_SYMBOL(drm_pick_cmdline_mode);
2051 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
2055 if (connector->display_info.non_desktop)
2059 enable = connector->status == connector_status_connected;
2061 enable = connector->status != connector_status_disconnected;
2066 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
2069 bool any_enabled = false;
2070 struct drm_connector *connector;
2073 drm_fb_helper_for_each_connector(fb_helper, i) {
2074 connector = fb_helper->connector_info[i]->connector;
2075 enabled[i] = drm_connector_enabled(connector, true);
2076 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
2077 connector->display_info.non_desktop ? "non desktop" : enabled[i] ? "yes" : "no");
2079 any_enabled |= enabled[i];
2085 drm_fb_helper_for_each_connector(fb_helper, i) {
2086 connector = fb_helper->connector_info[i]->connector;
2087 enabled[i] = drm_connector_enabled(connector, false);
2091 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
2092 struct drm_display_mode **modes,
2093 struct drm_fb_offset *offsets,
2094 bool *enabled, int width, int height)
2097 bool can_clone = false;
2098 struct drm_fb_helper_connector *fb_helper_conn;
2099 struct drm_display_mode *dmt_mode, *mode;
2101 /* only contemplate cloning in the single crtc case */
2102 if (fb_helper->crtc_count > 1)
2106 drm_fb_helper_for_each_connector(fb_helper, i) {
2111 /* only contemplate cloning if more than one connector is enabled */
2115 /* check the command line or if nothing common pick 1024x768 */
2117 drm_fb_helper_for_each_connector(fb_helper, i) {
2120 fb_helper_conn = fb_helper->connector_info[i];
2121 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
2126 for (j = 0; j < i; j++) {
2129 if (!drm_mode_equal(modes[j], modes[i]))
2135 DRM_DEBUG_KMS("can clone using command line\n");
2139 /* try and find a 1024x768 mode on each connector */
2141 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
2143 drm_fb_helper_for_each_connector(fb_helper, i) {
2147 fb_helper_conn = fb_helper->connector_info[i];
2148 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
2149 if (drm_mode_equal(mode, dmt_mode))
2157 DRM_DEBUG_KMS("can clone using 1024x768\n");
2160 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
2164 static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
2165 struct drm_display_mode **modes,
2166 struct drm_fb_offset *offsets,
2168 int h_idx, int v_idx)
2170 struct drm_fb_helper_connector *fb_helper_conn;
2172 int hoffset = 0, voffset = 0;
2174 drm_fb_helper_for_each_connector(fb_helper, i) {
2175 fb_helper_conn = fb_helper->connector_info[i];
2176 if (!fb_helper_conn->connector->has_tile)
2179 if (!modes[i] && (h_idx || v_idx)) {
2180 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
2181 fb_helper_conn->connector->base.id);
2184 if (fb_helper_conn->connector->tile_h_loc < h_idx)
2185 hoffset += modes[i]->hdisplay;
2187 if (fb_helper_conn->connector->tile_v_loc < v_idx)
2188 voffset += modes[i]->vdisplay;
2190 offsets[idx].x = hoffset;
2191 offsets[idx].y = voffset;
2192 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
2196 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
2197 struct drm_display_mode **modes,
2198 struct drm_fb_offset *offsets,
2199 bool *enabled, int width, int height)
2201 struct drm_fb_helper_connector *fb_helper_conn;
2202 const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
2203 u64 conn_configured = 0;
2208 drm_fb_helper_for_each_connector(fb_helper, i) {
2209 fb_helper_conn = fb_helper->connector_info[i];
2211 if (conn_configured & BIT_ULL(i))
2214 if (enabled[i] == false) {
2215 conn_configured |= BIT_ULL(i);
2219 /* first pass over all the untiled connectors */
2220 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
2223 if (tile_pass == 1) {
2224 if (fb_helper_conn->connector->tile_h_loc != 0 ||
2225 fb_helper_conn->connector->tile_v_loc != 0)
2229 if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 &&
2230 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
2231 /* if this tile_pass doesn't cover any of the tiles - keep going */
2235 * find the tile offsets for this pass - need to find
2236 * all tiles left and above
2238 drm_get_tile_offsets(fb_helper, modes, offsets,
2239 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
2241 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
2242 fb_helper_conn->connector->base.id);
2244 /* got for command line mode first */
2245 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
2247 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
2248 fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0);
2249 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
2251 /* No preferred modes, pick one off the list */
2252 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
2253 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
2256 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
2258 conn_configured |= BIT_ULL(i);
2261 if ((conn_configured & mask) != mask) {
2268 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
2269 struct drm_fb_helper_crtc **best_crtcs,
2270 struct drm_display_mode **modes,
2271 int n, int width, int height)
2274 struct drm_connector *connector;
2275 const struct drm_connector_helper_funcs *connector_funcs;
2276 struct drm_encoder *encoder;
2277 int my_score, best_score, score;
2278 struct drm_fb_helper_crtc **crtcs, *crtc;
2279 struct drm_fb_helper_connector *fb_helper_conn;
2281 if (n == fb_helper->connector_count)
2284 fb_helper_conn = fb_helper->connector_info[n];
2285 connector = fb_helper_conn->connector;
2287 best_crtcs[n] = NULL;
2288 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
2289 if (modes[n] == NULL)
2292 crtcs = kcalloc(fb_helper->connector_count,
2293 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
2298 if (connector->status == connector_status_connected)
2300 if (drm_has_cmdline_mode(fb_helper_conn))
2302 if (drm_has_preferred_mode(fb_helper_conn, width, height))
2305 connector_funcs = connector->helper_private;
2308 * If the DRM device implements atomic hooks and ->best_encoder() is
2309 * NULL we fallback to the default drm_atomic_helper_best_encoder()
2312 if (drm_drv_uses_atomic_modeset(fb_helper->dev) &&
2313 !connector_funcs->best_encoder)
2314 encoder = drm_atomic_helper_best_encoder(connector);
2316 encoder = connector_funcs->best_encoder(connector);
2322 * select a crtc for this connector and then attempt to configure
2323 * remaining connectors
2325 for (c = 0; c < fb_helper->crtc_count; c++) {
2326 crtc = &fb_helper->crtc_info[c];
2328 if ((encoder->possible_crtcs & (1 << c)) == 0)
2331 for (o = 0; o < n; o++)
2332 if (best_crtcs[o] == crtc)
2336 /* ignore cloning unless only a single crtc */
2337 if (fb_helper->crtc_count > 1)
2340 if (!drm_mode_equal(modes[o], modes[n]))
2345 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
2346 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
2348 if (score > best_score) {
2350 memcpy(best_crtcs, crtcs,
2351 fb_helper->connector_count *
2352 sizeof(struct drm_fb_helper_crtc *));
2360 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
2361 u32 width, u32 height)
2363 struct drm_device *dev = fb_helper->dev;
2364 struct drm_fb_helper_crtc **crtcs;
2365 struct drm_display_mode **modes;
2366 struct drm_fb_offset *offsets;
2370 DRM_DEBUG_KMS("\n");
2371 /* prevent concurrent modification of connector_count by hotplug */
2372 lockdep_assert_held(&fb_helper->lock);
2374 crtcs = kcalloc(fb_helper->connector_count,
2375 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
2376 modes = kcalloc(fb_helper->connector_count,
2377 sizeof(struct drm_display_mode *), GFP_KERNEL);
2378 offsets = kcalloc(fb_helper->connector_count,
2379 sizeof(struct drm_fb_offset), GFP_KERNEL);
2380 enabled = kcalloc(fb_helper->connector_count,
2381 sizeof(bool), GFP_KERNEL);
2382 if (!crtcs || !modes || !enabled || !offsets) {
2383 DRM_ERROR("Memory allocation failed\n");
2387 mutex_lock(&fb_helper->dev->mode_config.mutex);
2388 if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
2389 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
2390 drm_enable_connectors(fb_helper, enabled);
2392 if (!(fb_helper->funcs->initial_config &&
2393 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
2395 enabled, width, height))) {
2396 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
2397 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
2398 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
2400 if (!drm_target_cloned(fb_helper, modes, offsets,
2401 enabled, width, height) &&
2402 !drm_target_preferred(fb_helper, modes, offsets,
2403 enabled, width, height))
2404 DRM_ERROR("Unable to find initial modes\n");
2406 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2409 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
2411 mutex_unlock(&fb_helper->dev->mode_config.mutex);
2413 /* need to set the modesets up here for use later */
2414 /* fill out the connector<->crtc mappings into the modesets */
2415 for (i = 0; i < fb_helper->crtc_count; i++)
2416 drm_fb_helper_modeset_release(fb_helper,
2417 &fb_helper->crtc_info[i].mode_set);
2419 drm_fb_helper_for_each_connector(fb_helper, i) {
2420 struct drm_display_mode *mode = modes[i];
2421 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
2422 struct drm_fb_offset *offset = &offsets[i];
2424 if (mode && fb_crtc) {
2425 struct drm_mode_set *modeset = &fb_crtc->mode_set;
2426 struct drm_connector *connector =
2427 fb_helper->connector_info[i]->connector;
2429 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2430 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
2432 fb_crtc->desired_mode = mode;
2433 fb_crtc->x = offset->x;
2434 fb_crtc->y = offset->y;
2435 modeset->mode = drm_mode_duplicate(dev,
2436 fb_crtc->desired_mode);
2437 drm_connector_get(connector);
2438 modeset->connectors[modeset->num_connectors++] = connector;
2439 modeset->x = offset->x;
2440 modeset->y = offset->y;
2451 * This is a continuation of drm_setup_crtcs() that sets up anything related
2452 * to the framebuffer. During initialization, drm_setup_crtcs() is called before
2453 * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev).
2454 * So, any setup that touches those fields needs to be done here instead of in
2455 * drm_setup_crtcs().
2457 static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper)
2459 struct fb_info *info = fb_helper->fbdev;
2462 for (i = 0; i < fb_helper->crtc_count; i++)
2463 if (fb_helper->crtc_info[i].mode_set.num_connectors)
2464 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
2466 mutex_lock(&fb_helper->dev->mode_config.mutex);
2467 drm_fb_helper_for_each_connector(fb_helper, i) {
2468 struct drm_connector *connector =
2469 fb_helper->connector_info[i]->connector;
2471 /* use first connected connector for the physical dimensions */
2472 if (connector->status == connector_status_connected) {
2473 info->var.width = connector->display_info.width_mm;
2474 info->var.height = connector->display_info.height_mm;
2478 mutex_unlock(&fb_helper->dev->mode_config.mutex);
2481 /* Note: Drops fb_helper->lock before returning. */
2483 __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper,
2486 struct drm_device *dev = fb_helper->dev;
2487 struct fb_info *info;
2488 unsigned int width, height;
2491 width = dev->mode_config.max_width;
2492 height = dev->mode_config.max_height;
2494 drm_setup_crtcs(fb_helper, width, height);
2495 ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
2497 if (ret == -EAGAIN) {
2498 fb_helper->preferred_bpp = bpp_sel;
2499 fb_helper->deferred_setup = true;
2502 mutex_unlock(&fb_helper->lock);
2506 drm_setup_crtcs_fb(fb_helper);
2508 fb_helper->deferred_setup = false;
2510 info = fb_helper->fbdev;
2511 info->var.pixclock = 0;
2513 /* Need to drop locks to avoid recursive deadlock in
2514 * register_framebuffer. This is ok because the only thing left to do is
2515 * register the fbdev emulation instance in kernel_fb_helper_list. */
2516 mutex_unlock(&fb_helper->lock);
2518 ret = register_framebuffer(info);
2522 dev_info(dev->dev, "fb%d: %s frame buffer device\n",
2523 info->node, info->fix.id);
2525 mutex_lock(&kernel_fb_helper_lock);
2526 if (list_empty(&kernel_fb_helper_list))
2527 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
2529 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
2530 mutex_unlock(&kernel_fb_helper_lock);
2536 * drm_fb_helper_initial_config - setup a sane initial connector configuration
2537 * @fb_helper: fb_helper device struct
2538 * @bpp_sel: bpp value to use for the framebuffer configuration
2540 * Scans the CRTCs and connectors and tries to put together an initial setup.
2541 * At the moment, this is a cloned configuration across all heads with
2542 * a new framebuffer object as the backing store.
2544 * Note that this also registers the fbdev and so allows userspace to call into
2545 * the driver through the fbdev interfaces.
2547 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
2548 * to let the driver allocate and initialize the fbdev info structure and the
2549 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
2550 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
2551 * values for the fbdev info structure.
2555 * When you have fbcon support built-in or already loaded, this function will do
2556 * a full modeset to setup the fbdev console. Due to locking misdesign in the
2557 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2558 * console_lock. Until console_unlock is called no dmesg lines will be sent out
2559 * to consoles, not even serial console. This means when your driver crashes,
2560 * you will see absolutely nothing else but a system stuck in this function,
2561 * with no further output. Any kind of printk() you place within your own driver
2562 * or in the drm core modeset code will also never show up.
2564 * Standard debug practice is to run the fbcon setup without taking the
2565 * console_lock as a hack, to be able to see backtraces and crashes on the
2566 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2569 * The other option is to just disable fbdev emulation since very likely the
2570 * first modeset from userspace will crash in the same way, and is even easier
2571 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
2572 * kernel cmdline option.
2575 * Zero if everything went ok, nonzero otherwise.
2577 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
2581 if (!drm_fbdev_emulation)
2584 mutex_lock(&fb_helper->lock);
2585 ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel);
2589 EXPORT_SYMBOL(drm_fb_helper_initial_config);
2592 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
2593 * probing all the outputs attached to the fb
2594 * @fb_helper: driver-allocated fbdev helper, can be NULL
2596 * Scan the connectors attached to the fb_helper and try to put together a
2597 * setup after notification of a change in output configuration.
2599 * Called at runtime, takes the mode config locks to be able to check/change the
2600 * modeset configuration. Must be run from process context (which usually means
2601 * either the output polling work or a work item launched from the driver's
2602 * hotplug interrupt).
2604 * Note that drivers may call this even before calling
2605 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
2606 * for a race-free fbcon setup and will make sure that the fbdev emulation will
2607 * not miss any hotplug events.
2610 * 0 on success and a non-zero error code otherwise.
2612 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
2616 if (!drm_fbdev_emulation || !fb_helper)
2619 mutex_lock(&fb_helper->lock);
2620 if (fb_helper->deferred_setup) {
2621 err = __drm_fb_helper_initial_config_and_unlock(fb_helper,
2622 fb_helper->preferred_bpp);
2626 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
2627 fb_helper->delayed_hotplug = true;
2628 mutex_unlock(&fb_helper->lock);
2632 DRM_DEBUG_KMS("\n");
2634 drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
2635 drm_setup_crtcs_fb(fb_helper);
2636 mutex_unlock(&fb_helper->lock);
2638 drm_fb_helper_set_par(fb_helper->fbdev);
2642 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
2645 * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation
2648 * This function can be used as the &drm_driver->lastclose callback for drivers
2649 * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked().
2651 void drm_fb_helper_lastclose(struct drm_device *dev)
2653 drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper);
2655 EXPORT_SYMBOL(drm_fb_helper_lastclose);
2658 * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed
2659 * helper for fbdev emulation
2662 * This function can be used as the
2663 * &drm_mode_config_funcs.output_poll_changed callback for drivers that only
2664 * need to call drm_fb_helper_hotplug_event().
2666 void drm_fb_helper_output_poll_changed(struct drm_device *dev)
2668 drm_fb_helper_hotplug_event(dev->fb_helper);
2670 EXPORT_SYMBOL(drm_fb_helper_output_poll_changed);
2672 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
2673 * but the module doesn't depend on any fb console symbols. At least
2674 * attempt to load fbcon to avoid leaving the system without a usable console.
2676 int __init drm_fb_helper_modinit(void)
2678 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
2679 const char name[] = "fbcon";
2680 struct module *fbcon;
2682 mutex_lock(&module_mutex);
2683 fbcon = find_module(name);
2684 mutex_unlock(&module_mutex);
2687 request_module_nowait(name);
2691 EXPORT_SYMBOL(drm_fb_helper_modinit);