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 bool drm_fbdev_emulation = true;
43 module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
44 MODULE_PARM_DESC(fbdev_emulation,
45 "Enable legacy fbdev emulation [default=true]");
47 static LIST_HEAD(kernel_fb_helper_list);
52 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
53 * mode setting driver. They can be used mostly independently from the crtc
54 * helper functions used by many drivers to implement the kernel mode setting
57 * Initialization is done as a four-step process with drm_fb_helper_prepare(),
58 * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
59 * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
60 * default behaviour can override the third step with their own code.
61 * Teardown is done with drm_fb_helper_fini().
63 * At runtime drivers should restore the fbdev console by calling
64 * drm_fb_helper_restore_fbdev_mode_unlocked() from their ->lastclose callback.
65 * They should also notify the fb helper code from updates to the output
66 * configuration by calling drm_fb_helper_hotplug_event(). For easier
67 * integration with the output polling code in drm_crtc_helper.c the modeset
68 * code provides a ->output_poll_changed callback.
70 * All other functions exported by the fb helper library can be used to
71 * implement the fbdev driver interface by the driver.
73 * It is possible, though perhaps somewhat tricky, to implement race-free
74 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
75 * helper must be called first to initialize the minimum required to make
76 * hotplug detection work. Drivers also need to make sure to properly set up
77 * the dev->mode_config.funcs member. After calling drm_kms_helper_poll_init()
78 * it is safe to enable interrupts and start processing hotplug events. At the
79 * same time, drivers should initialize all modeset objects such as CRTCs,
80 * encoders and connectors. To finish up the fbdev helper initialization, the
81 * drm_fb_helper_init() function is called. To probe for all attached displays
82 * and set up an initial configuration using the detected hardware, drivers
83 * should call drm_fb_helper_single_add_all_connectors() followed by
84 * drm_fb_helper_initial_config().
88 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
90 * @fb_helper: fbdev initialized with drm_fb_helper_init
92 * This functions adds all the available connectors for use with the given
93 * fb_helper. This is a separate step to allow drivers to freely assign
94 * connectors to the fbdev, e.g. if some are reserved for special purposes or
95 * not adequate to be used for the fbcon.
97 * This function is protected against concurrent connector hotadds/removals
98 * using drm_fb_helper_add_one_connector() and
99 * drm_fb_helper_remove_one_connector().
101 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
103 struct drm_device *dev = fb_helper->dev;
104 struct drm_connector *connector;
107 if (!drm_fbdev_emulation)
110 mutex_lock(&dev->mode_config.mutex);
111 drm_for_each_connector(connector, dev) {
112 struct drm_fb_helper_connector *fb_helper_connector;
114 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
115 if (!fb_helper_connector)
118 fb_helper_connector->connector = connector;
119 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
121 mutex_unlock(&dev->mode_config.mutex);
124 for (i = 0; i < fb_helper->connector_count; i++) {
125 kfree(fb_helper->connector_info[i]);
126 fb_helper->connector_info[i] = NULL;
128 fb_helper->connector_count = 0;
129 mutex_unlock(&dev->mode_config.mutex);
133 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
135 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector)
137 struct drm_fb_helper_connector **temp;
138 struct drm_fb_helper_connector *fb_helper_connector;
140 if (!drm_fbdev_emulation)
143 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
144 if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) {
145 temp = krealloc(fb_helper->connector_info, sizeof(struct drm_fb_helper_connector *) * (fb_helper->connector_count + 1), GFP_KERNEL);
149 fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1;
150 fb_helper->connector_info = temp;
154 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
155 if (!fb_helper_connector)
158 fb_helper_connector->connector = connector;
159 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
162 EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
164 static void remove_from_modeset(struct drm_mode_set *set,
165 struct drm_connector *connector)
169 for (i = 0; i < set->num_connectors; i++) {
170 if (set->connectors[i] == connector)
174 if (i == set->num_connectors)
177 for (j = i + 1; j < set->num_connectors; j++) {
178 set->connectors[j - 1] = set->connectors[j];
180 set->num_connectors--;
183 * TODO maybe need to makes sure we set it back to !=NULL somewhere?
185 if (set->num_connectors == 0) {
187 drm_mode_destroy(connector->dev, set->mode);
192 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
193 struct drm_connector *connector)
195 struct drm_fb_helper_connector *fb_helper_connector;
198 if (!drm_fbdev_emulation)
201 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
203 for (i = 0; i < fb_helper->connector_count; i++) {
204 if (fb_helper->connector_info[i]->connector == connector)
208 if (i == fb_helper->connector_count)
210 fb_helper_connector = fb_helper->connector_info[i];
212 for (j = i + 1; j < fb_helper->connector_count; j++) {
213 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
215 fb_helper->connector_count--;
216 kfree(fb_helper_connector);
218 /* also cleanup dangling references to the connector: */
219 for (i = 0; i < fb_helper->crtc_count; i++)
220 remove_from_modeset(&fb_helper->crtc_info[i].mode_set, connector);
224 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
226 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
228 uint16_t *r_base, *g_base, *b_base;
231 if (helper->funcs->gamma_get == NULL)
234 r_base = crtc->gamma_store;
235 g_base = r_base + crtc->gamma_size;
236 b_base = g_base + crtc->gamma_size;
238 for (i = 0; i < crtc->gamma_size; i++)
239 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
242 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
244 uint16_t *r_base, *g_base, *b_base;
246 if (crtc->funcs->gamma_set == NULL)
249 r_base = crtc->gamma_store;
250 g_base = r_base + crtc->gamma_size;
251 b_base = g_base + crtc->gamma_size;
253 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
257 * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
258 * @info: fbdev registered by the helper
260 int drm_fb_helper_debug_enter(struct fb_info *info)
262 struct drm_fb_helper *helper = info->par;
263 const struct drm_crtc_helper_funcs *funcs;
266 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
267 for (i = 0; i < helper->crtc_count; i++) {
268 struct drm_mode_set *mode_set =
269 &helper->crtc_info[i].mode_set;
271 if (!mode_set->crtc->enabled)
274 funcs = mode_set->crtc->helper_private;
275 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
276 funcs->mode_set_base_atomic(mode_set->crtc,
280 ENTER_ATOMIC_MODE_SET);
286 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
288 /* Find the real fb for a given fb helper CRTC */
289 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
291 struct drm_device *dev = crtc->dev;
294 drm_for_each_crtc(c, dev) {
295 if (crtc->base.id == c->base.id)
296 return c->primary->fb;
303 * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
304 * @info: fbdev registered by the helper
306 int drm_fb_helper_debug_leave(struct fb_info *info)
308 struct drm_fb_helper *helper = info->par;
309 struct drm_crtc *crtc;
310 const struct drm_crtc_helper_funcs *funcs;
311 struct drm_framebuffer *fb;
314 for (i = 0; i < helper->crtc_count; i++) {
315 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
316 crtc = mode_set->crtc;
317 funcs = crtc->helper_private;
318 fb = drm_mode_config_fb(crtc);
324 DRM_ERROR("no fb to restore??\n");
328 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
329 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
330 crtc->y, LEAVE_ATOMIC_MODE_SET);
335 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
337 static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
339 struct drm_device *dev = fb_helper->dev;
340 struct drm_plane *plane;
343 drm_warn_on_modeset_not_all_locked(dev);
345 drm_for_each_plane(plane, dev) {
346 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
347 drm_plane_force_disable(plane);
349 if (dev->mode_config.rotation_property) {
350 drm_mode_plane_set_obj_prop(plane,
351 dev->mode_config.rotation_property,
356 for (i = 0; i < fb_helper->crtc_count; i++) {
357 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
358 struct drm_crtc *crtc = mode_set->crtc;
361 if (crtc->funcs->cursor_set) {
362 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
367 ret = drm_mode_set_config_internal(mode_set);
376 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
377 * @fb_helper: fbcon to restore
379 * This should be called from driver's drm ->lastclose callback
380 * when implementing an fbcon on top of kms using this helper. This ensures that
381 * the user isn't greeted with a black screen when e.g. X dies.
384 * Zero if everything went ok, negative error code otherwise.
386 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
388 struct drm_device *dev = fb_helper->dev;
392 if (!drm_fbdev_emulation)
395 drm_modeset_lock_all(dev);
396 ret = restore_fbdev_mode(fb_helper);
398 do_delayed = fb_helper->delayed_hotplug;
400 fb_helper->delayed_hotplug = false;
401 drm_modeset_unlock_all(dev);
404 drm_fb_helper_hotplug_event(fb_helper);
407 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
409 static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
411 struct drm_device *dev = fb_helper->dev;
412 struct drm_crtc *crtc;
413 int bound = 0, crtcs_bound = 0;
415 /* Sometimes user space wants everything disabled, so don't steal the
416 * display if there's a master. */
417 if (dev->primary->master)
420 drm_for_each_crtc(crtc, dev) {
421 if (crtc->primary->fb)
423 if (crtc->primary->fb == fb_helper->fb)
427 if (bound < crtcs_bound)
433 #ifdef CONFIG_MAGIC_SYSRQ
435 * restore fbcon display for all kms driver's using this helper, used for sysrq
436 * and panic handling.
438 static bool drm_fb_helper_force_kernel_mode(void)
440 bool ret, error = false;
441 struct drm_fb_helper *helper;
443 if (list_empty(&kernel_fb_helper_list))
446 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
447 struct drm_device *dev = helper->dev;
449 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
452 drm_modeset_lock_all(dev);
453 ret = restore_fbdev_mode(helper);
456 drm_modeset_unlock_all(dev);
461 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
464 ret = drm_fb_helper_force_kernel_mode();
466 DRM_ERROR("Failed to restore crtc configuration\n");
468 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
470 static void drm_fb_helper_sysrq(int dummy1)
472 schedule_work(&drm_fb_helper_restore_work);
475 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
476 .handler = drm_fb_helper_sysrq,
477 .help_msg = "force-fb(V)",
478 .action_msg = "Restore framebuffer console",
481 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
484 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
486 struct drm_fb_helper *fb_helper = info->par;
487 struct drm_device *dev = fb_helper->dev;
488 struct drm_crtc *crtc;
489 struct drm_connector *connector;
493 * For each CRTC in this fb, turn the connectors on/off.
495 drm_modeset_lock_all(dev);
496 if (!drm_fb_helper_is_bound(fb_helper)) {
497 drm_modeset_unlock_all(dev);
501 for (i = 0; i < fb_helper->crtc_count; i++) {
502 crtc = fb_helper->crtc_info[i].mode_set.crtc;
507 /* Walk the connectors & encoders on this fb turning them on/off */
508 for (j = 0; j < fb_helper->connector_count; j++) {
509 connector = fb_helper->connector_info[j]->connector;
510 connector->funcs->dpms(connector, dpms_mode);
511 drm_object_property_set_value(&connector->base,
512 dev->mode_config.dpms_property, dpms_mode);
515 drm_modeset_unlock_all(dev);
519 * drm_fb_helper_blank - implementation for ->fb_blank
520 * @blank: desired blanking state
521 * @info: fbdev registered by the helper
523 int drm_fb_helper_blank(int blank, struct fb_info *info)
525 if (oops_in_progress)
529 /* Display: On; HSync: On, VSync: On */
530 case FB_BLANK_UNBLANK:
531 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
533 /* Display: Off; HSync: On, VSync: On */
534 case FB_BLANK_NORMAL:
535 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
537 /* Display: Off; HSync: Off, VSync: On */
538 case FB_BLANK_HSYNC_SUSPEND:
539 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
541 /* Display: Off; HSync: On, VSync: Off */
542 case FB_BLANK_VSYNC_SUSPEND:
543 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
545 /* Display: Off; HSync: Off, VSync: Off */
546 case FB_BLANK_POWERDOWN:
547 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
552 EXPORT_SYMBOL(drm_fb_helper_blank);
554 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
558 for (i = 0; i < helper->connector_count; i++)
559 kfree(helper->connector_info[i]);
560 kfree(helper->connector_info);
561 for (i = 0; i < helper->crtc_count; i++) {
562 kfree(helper->crtc_info[i].mode_set.connectors);
563 if (helper->crtc_info[i].mode_set.mode)
564 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
566 kfree(helper->crtc_info);
570 * drm_fb_helper_prepare - setup a drm_fb_helper structure
572 * @helper: driver-allocated fbdev helper structure to set up
573 * @funcs: pointer to structure of functions associate with this helper
575 * Sets up the bare minimum to make the framebuffer helper usable. This is
576 * useful to implement race-free initialization of the polling helpers.
578 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
579 const struct drm_fb_helper_funcs *funcs)
581 INIT_LIST_HEAD(&helper->kernel_fb_list);
582 helper->funcs = funcs;
585 EXPORT_SYMBOL(drm_fb_helper_prepare);
588 * drm_fb_helper_init - initialize a drm_fb_helper structure
590 * @fb_helper: driver-allocated fbdev helper structure to initialize
591 * @crtc_count: maximum number of crtcs to support in this fbdev emulation
592 * @max_conn_count: max connector count
594 * This allocates the structures for the fbdev helper with the given limits.
595 * Note that this won't yet touch the hardware (through the driver interfaces)
596 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
597 * to allow driver writes more control over the exact init sequence.
599 * Drivers must call drm_fb_helper_prepare() before calling this function.
602 * Zero if everything went ok, nonzero otherwise.
604 int drm_fb_helper_init(struct drm_device *dev,
605 struct drm_fb_helper *fb_helper,
606 int crtc_count, int max_conn_count)
608 struct drm_crtc *crtc;
611 if (!drm_fbdev_emulation)
617 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
618 if (!fb_helper->crtc_info)
621 fb_helper->crtc_count = crtc_count;
622 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
623 if (!fb_helper->connector_info) {
624 kfree(fb_helper->crtc_info);
627 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
628 fb_helper->connector_count = 0;
630 for (i = 0; i < crtc_count; i++) {
631 fb_helper->crtc_info[i].mode_set.connectors =
632 kcalloc(max_conn_count,
633 sizeof(struct drm_connector *),
636 if (!fb_helper->crtc_info[i].mode_set.connectors)
638 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
642 drm_for_each_crtc(crtc, dev) {
643 fb_helper->crtc_info[i].mode_set.crtc = crtc;
649 drm_fb_helper_crtc_free(fb_helper);
652 EXPORT_SYMBOL(drm_fb_helper_init);
655 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
656 * @fb_helper: driver-allocated fbdev helper
658 * A helper to alloc fb_info and the members cmap and apertures. Called
659 * by the driver within the fb_probe fb_helper callback function.
662 * fb_info pointer if things went okay, pointer containing error code
665 struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
667 struct device *dev = fb_helper->dev->dev;
668 struct fb_info *info;
671 info = framebuffer_alloc(0, dev);
673 return ERR_PTR(-ENOMEM);
675 ret = fb_alloc_cmap(&info->cmap, 256, 0);
679 info->apertures = alloc_apertures(1);
680 if (!info->apertures) {
685 fb_helper->fbdev = info;
690 fb_dealloc_cmap(&info->cmap);
692 framebuffer_release(info);
695 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
698 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
699 * @fb_helper: driver-allocated fbdev helper
701 * A wrapper around unregister_framebuffer, to release the fb_info
704 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
706 if (fb_helper && fb_helper->fbdev)
707 unregister_framebuffer(fb_helper->fbdev);
709 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
712 * drm_fb_helper_release_fbi - dealloc fb_info and its members
713 * @fb_helper: driver-allocated fbdev helper
715 * A helper to free memory taken by fb_info and the members cmap and
718 void drm_fb_helper_release_fbi(struct drm_fb_helper *fb_helper)
721 struct fb_info *info = fb_helper->fbdev;
725 fb_dealloc_cmap(&info->cmap);
726 framebuffer_release(info);
729 fb_helper->fbdev = NULL;
732 EXPORT_SYMBOL(drm_fb_helper_release_fbi);
734 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
736 if (!drm_fbdev_emulation)
739 if (!list_empty(&fb_helper->kernel_fb_list)) {
740 list_del(&fb_helper->kernel_fb_list);
741 if (list_empty(&kernel_fb_helper_list)) {
742 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
746 drm_fb_helper_crtc_free(fb_helper);
749 EXPORT_SYMBOL(drm_fb_helper_fini);
752 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
753 * @fb_helper: driver-allocated fbdev helper
755 * A wrapper around unlink_framebuffer implemented by fbdev core
757 void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
759 if (fb_helper && fb_helper->fbdev)
760 unlink_framebuffer(fb_helper->fbdev);
762 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
765 * drm_fb_helper_sys_read - wrapper around fb_sys_read
766 * @info: fb_info struct pointer
767 * @buf: userspace buffer to read from framebuffer memory
768 * @count: number of bytes to read from framebuffer memory
769 * @ppos: read offset within framebuffer memory
771 * A wrapper around fb_sys_read implemented by fbdev core
773 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
774 size_t count, loff_t *ppos)
776 return fb_sys_read(info, buf, count, ppos);
778 EXPORT_SYMBOL(drm_fb_helper_sys_read);
781 * drm_fb_helper_sys_write - wrapper around fb_sys_write
782 * @info: fb_info struct pointer
783 * @buf: userspace buffer to write to framebuffer memory
784 * @count: number of bytes to write to framebuffer memory
785 * @ppos: write offset within framebuffer memory
787 * A wrapper around fb_sys_write implemented by fbdev core
789 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
790 size_t count, loff_t *ppos)
792 return fb_sys_write(info, buf, count, ppos);
794 EXPORT_SYMBOL(drm_fb_helper_sys_write);
797 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
798 * @info: fbdev registered by the helper
799 * @rect: info about rectangle to fill
801 * A wrapper around sys_fillrect implemented by fbdev core
803 void drm_fb_helper_sys_fillrect(struct fb_info *info,
804 const struct fb_fillrect *rect)
806 sys_fillrect(info, rect);
808 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
811 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
812 * @info: fbdev registered by the helper
813 * @area: info about area to copy
815 * A wrapper around sys_copyarea implemented by fbdev core
817 void drm_fb_helper_sys_copyarea(struct fb_info *info,
818 const struct fb_copyarea *area)
820 sys_copyarea(info, area);
822 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
825 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
826 * @info: fbdev registered by the helper
827 * @image: info about image to blit
829 * A wrapper around sys_imageblit implemented by fbdev core
831 void drm_fb_helper_sys_imageblit(struct fb_info *info,
832 const struct fb_image *image)
834 sys_imageblit(info, image);
836 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
839 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
840 * @info: fbdev registered by the helper
841 * @rect: info about rectangle to fill
843 * A wrapper around cfb_imageblit implemented by fbdev core
845 void drm_fb_helper_cfb_fillrect(struct fb_info *info,
846 const struct fb_fillrect *rect)
848 cfb_fillrect(info, rect);
850 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
853 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
854 * @info: fbdev registered by the helper
855 * @area: info about area to copy
857 * A wrapper around cfb_copyarea implemented by fbdev core
859 void drm_fb_helper_cfb_copyarea(struct fb_info *info,
860 const struct fb_copyarea *area)
862 cfb_copyarea(info, area);
864 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
867 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
868 * @info: fbdev registered by the helper
869 * @image: info about image to blit
871 * A wrapper around cfb_imageblit implemented by fbdev core
873 void drm_fb_helper_cfb_imageblit(struct fb_info *info,
874 const struct fb_image *image)
876 cfb_imageblit(info, image);
878 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
881 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
882 * @fb_helper: driver-allocated fbdev helper
883 * @state: desired state, zero to resume, non-zero to suspend
885 * A wrapper around fb_set_suspend implemented by fbdev core
887 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, int state)
889 if (fb_helper && fb_helper->fbdev)
890 fb_set_suspend(fb_helper->fbdev, state);
892 EXPORT_SYMBOL(drm_fb_helper_set_suspend);
894 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
895 u16 blue, u16 regno, struct fb_info *info)
897 struct drm_fb_helper *fb_helper = info->par;
898 struct drm_framebuffer *fb = fb_helper->fb;
901 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
904 /* place color in psuedopalette */
907 palette = (u32 *)info->pseudo_palette;
908 red >>= (16 - info->var.red.length);
909 green >>= (16 - info->var.green.length);
910 blue >>= (16 - info->var.blue.length);
911 value = (red << info->var.red.offset) |
912 (green << info->var.green.offset) |
913 (blue << info->var.blue.offset);
914 if (info->var.transp.length > 0) {
915 u32 mask = (1 << info->var.transp.length) - 1;
916 mask <<= info->var.transp.offset;
919 palette[regno] = value;
924 * The driver really shouldn't advertise pseudo/directcolor
925 * visuals if it can't deal with the palette.
927 if (WARN_ON(!fb_helper->funcs->gamma_set ||
928 !fb_helper->funcs->gamma_get))
933 if (fb->bits_per_pixel == 16) {
936 if (fb->depth == 16 && regno > 63)
938 if (fb->depth == 15 && regno > 31)
941 if (fb->depth == 16) {
945 for (i = 0; i < 8; i++)
946 fb_helper->funcs->gamma_set(crtc, red,
947 green, blue, pindex + i);
950 fb_helper->funcs->gamma_get(crtc, &r,
954 for (i = 0; i < 4; i++)
955 fb_helper->funcs->gamma_set(crtc, r,
962 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
967 * drm_fb_helper_setcmap - implementation for ->fb_setcmap
969 * @info: fbdev registered by the helper
971 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
973 struct drm_fb_helper *fb_helper = info->par;
974 struct drm_device *dev = fb_helper->dev;
975 const struct drm_crtc_helper_funcs *crtc_funcs;
976 u16 *red, *green, *blue, *transp;
977 struct drm_crtc *crtc;
981 if (oops_in_progress)
984 drm_modeset_lock_all(dev);
985 if (!drm_fb_helper_is_bound(fb_helper)) {
986 drm_modeset_unlock_all(dev);
990 for (i = 0; i < fb_helper->crtc_count; i++) {
991 crtc = fb_helper->crtc_info[i].mode_set.crtc;
992 crtc_funcs = crtc->helper_private;
997 transp = cmap->transp;
1000 for (j = 0; j < cmap->len; j++) {
1001 u16 hred, hgreen, hblue, htransp = 0xffff;
1008 htransp = *transp++;
1010 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
1014 if (crtc_funcs->load_lut)
1015 crtc_funcs->load_lut(crtc);
1018 drm_modeset_unlock_all(dev);
1021 EXPORT_SYMBOL(drm_fb_helper_setcmap);
1024 * drm_fb_helper_check_var - implementation for ->fb_check_var
1025 * @var: screeninfo to check
1026 * @info: fbdev registered by the helper
1028 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1029 struct fb_info *info)
1031 struct drm_fb_helper *fb_helper = info->par;
1032 struct drm_framebuffer *fb = fb_helper->fb;
1035 if (var->pixclock != 0 || in_dbg_master())
1038 /* Need to resize the fb object !!! */
1039 if (var->bits_per_pixel > fb->bits_per_pixel ||
1040 var->xres > fb->width || var->yres > fb->height ||
1041 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1042 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
1043 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1044 var->xres, var->yres, var->bits_per_pixel,
1045 var->xres_virtual, var->yres_virtual,
1046 fb->width, fb->height, fb->bits_per_pixel);
1050 switch (var->bits_per_pixel) {
1052 depth = (var->green.length == 6) ? 16 : 15;
1055 depth = (var->transp.length > 0) ? 32 : 24;
1058 depth = var->bits_per_pixel;
1064 var->red.offset = 0;
1065 var->green.offset = 0;
1066 var->blue.offset = 0;
1067 var->red.length = 8;
1068 var->green.length = 8;
1069 var->blue.length = 8;
1070 var->transp.length = 0;
1071 var->transp.offset = 0;
1074 var->red.offset = 10;
1075 var->green.offset = 5;
1076 var->blue.offset = 0;
1077 var->red.length = 5;
1078 var->green.length = 5;
1079 var->blue.length = 5;
1080 var->transp.length = 1;
1081 var->transp.offset = 15;
1084 var->red.offset = 11;
1085 var->green.offset = 5;
1086 var->blue.offset = 0;
1087 var->red.length = 5;
1088 var->green.length = 6;
1089 var->blue.length = 5;
1090 var->transp.length = 0;
1091 var->transp.offset = 0;
1094 var->red.offset = 16;
1095 var->green.offset = 8;
1096 var->blue.offset = 0;
1097 var->red.length = 8;
1098 var->green.length = 8;
1099 var->blue.length = 8;
1100 var->transp.length = 0;
1101 var->transp.offset = 0;
1104 var->red.offset = 16;
1105 var->green.offset = 8;
1106 var->blue.offset = 0;
1107 var->red.length = 8;
1108 var->green.length = 8;
1109 var->blue.length = 8;
1110 var->transp.length = 8;
1111 var->transp.offset = 24;
1118 EXPORT_SYMBOL(drm_fb_helper_check_var);
1121 * drm_fb_helper_set_par - implementation for ->fb_set_par
1122 * @info: fbdev registered by the helper
1124 * This will let fbcon do the mode init and is called at initialization time by
1125 * the fbdev core when registering the driver, and later on through the hotplug
1128 int drm_fb_helper_set_par(struct fb_info *info)
1130 struct drm_fb_helper *fb_helper = info->par;
1131 struct fb_var_screeninfo *var = &info->var;
1133 if (oops_in_progress)
1136 if (var->pixclock != 0) {
1137 DRM_ERROR("PIXEL CLOCK SET\n");
1141 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
1145 EXPORT_SYMBOL(drm_fb_helper_set_par);
1148 * drm_fb_helper_pan_display - implementation for ->fb_pan_display
1149 * @var: updated screen information
1150 * @info: fbdev registered by the helper
1152 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1153 struct fb_info *info)
1155 struct drm_fb_helper *fb_helper = info->par;
1156 struct drm_device *dev = fb_helper->dev;
1157 struct drm_mode_set *modeset;
1161 if (oops_in_progress)
1164 drm_modeset_lock_all(dev);
1165 if (!drm_fb_helper_is_bound(fb_helper)) {
1166 drm_modeset_unlock_all(dev);
1170 for (i = 0; i < fb_helper->crtc_count; i++) {
1171 modeset = &fb_helper->crtc_info[i].mode_set;
1173 modeset->x = var->xoffset;
1174 modeset->y = var->yoffset;
1176 if (modeset->num_connectors) {
1177 ret = drm_mode_set_config_internal(modeset);
1179 info->var.xoffset = var->xoffset;
1180 info->var.yoffset = var->yoffset;
1184 drm_modeset_unlock_all(dev);
1187 EXPORT_SYMBOL(drm_fb_helper_pan_display);
1190 * Allocates the backing storage and sets up the fbdev info structure through
1191 * the ->fb_probe callback and then registers the fbdev and sets up the panic
1194 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1200 struct fb_info *info;
1201 struct drm_fb_helper_surface_size sizes;
1204 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1205 sizes.surface_depth = 24;
1206 sizes.surface_bpp = 32;
1207 sizes.fb_width = (unsigned)-1;
1208 sizes.fb_height = (unsigned)-1;
1210 /* if driver picks 8 or 16 by default use that
1211 for both depth/bpp */
1212 if (preferred_bpp != sizes.surface_bpp)
1213 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
1215 /* first up get a count of crtcs now in use and new min/maxes width/heights */
1216 for (i = 0; i < fb_helper->connector_count; i++) {
1217 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
1218 struct drm_cmdline_mode *cmdline_mode;
1220 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1222 if (cmdline_mode->bpp_specified) {
1223 switch (cmdline_mode->bpp) {
1225 sizes.surface_depth = sizes.surface_bpp = 8;
1228 sizes.surface_depth = 15;
1229 sizes.surface_bpp = 16;
1232 sizes.surface_depth = sizes.surface_bpp = 16;
1235 sizes.surface_depth = sizes.surface_bpp = 24;
1238 sizes.surface_depth = 24;
1239 sizes.surface_bpp = 32;
1247 for (i = 0; i < fb_helper->crtc_count; i++) {
1248 struct drm_display_mode *desired_mode;
1249 struct drm_mode_set *mode_set;
1251 /* in case of tile group, are we the last tile vert or horiz?
1252 * If no tile group you are always the last one both vertically
1255 bool lastv = true, lasth = true;
1257 desired_mode = fb_helper->crtc_info[i].desired_mode;
1258 mode_set = &fb_helper->crtc_info[i].mode_set;
1265 x = fb_helper->crtc_info[i].x;
1266 y = fb_helper->crtc_info[i].y;
1268 if (gamma_size == 0)
1269 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1271 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1272 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
1274 for (j = 0; j < mode_set->num_connectors; j++) {
1275 struct drm_connector *connector = mode_set->connectors[j];
1276 if (connector->has_tile) {
1277 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1278 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1279 /* cloning to multiple tiles is just crazy-talk, so: */
1285 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1287 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
1290 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
1291 /* hmm everyone went away - assume VGA cable just fell out
1292 and will come back later. */
1293 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
1294 sizes.fb_width = sizes.surface_width = 1024;
1295 sizes.fb_height = sizes.surface_height = 768;
1298 /* push down into drivers */
1299 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1303 info = fb_helper->fbdev;
1306 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1307 * events, but at init time drm_setup_crtcs needs to be called before
1308 * the fb is allocated (since we need to figure out the desired size of
1309 * the fb before we can allocate it ...). Hence we need to fix things up
1312 for (i = 0; i < fb_helper->crtc_count; i++)
1313 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1314 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1317 info->var.pixclock = 0;
1318 if (register_framebuffer(info) < 0)
1321 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
1322 info->node, info->fix.id);
1324 if (list_empty(&kernel_fb_helper_list)) {
1325 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1328 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1334 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1335 * @info: fbdev registered by the helper
1336 * @pitch: desired pitch
1337 * @depth: desired depth
1339 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1340 * fbdev emulations. Drivers which support acceleration methods which impose
1341 * additional constraints need to set up their own limits.
1343 * Drivers should call this (or their equivalent setup code) from their
1344 * ->fb_probe callback.
1346 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1349 info->fix.type = FB_TYPE_PACKED_PIXELS;
1350 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1351 FB_VISUAL_TRUECOLOR;
1352 info->fix.mmio_start = 0;
1353 info->fix.mmio_len = 0;
1354 info->fix.type_aux = 0;
1355 info->fix.xpanstep = 1; /* doing it in hw */
1356 info->fix.ypanstep = 1; /* doing it in hw */
1357 info->fix.ywrapstep = 0;
1358 info->fix.accel = FB_ACCEL_NONE;
1360 info->fix.line_length = pitch;
1363 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1366 * drm_fb_helper_fill_var - initalizes variable fbdev information
1367 * @info: fbdev instance to set up
1368 * @fb_helper: fb helper instance to use as template
1369 * @fb_width: desired fb width
1370 * @fb_height: desired fb height
1372 * Sets up the variable fbdev metainformation from the given fb helper instance
1373 * and the drm framebuffer allocated in fb_helper->fb.
1375 * Drivers should call this (or their equivalent setup code) from their
1376 * ->fb_probe callback after having allocated the fbdev backing
1377 * storage framebuffer.
1379 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1380 uint32_t fb_width, uint32_t fb_height)
1382 struct drm_framebuffer *fb = fb_helper->fb;
1383 info->pseudo_palette = fb_helper->pseudo_palette;
1384 info->var.xres_virtual = fb->width;
1385 info->var.yres_virtual = fb->height;
1386 info->var.bits_per_pixel = fb->bits_per_pixel;
1387 info->var.accel_flags = FB_ACCELF_TEXT;
1388 info->var.xoffset = 0;
1389 info->var.yoffset = 0;
1390 info->var.activate = FB_ACTIVATE_NOW;
1391 info->var.height = -1;
1392 info->var.width = -1;
1394 switch (fb->depth) {
1396 info->var.red.offset = 0;
1397 info->var.green.offset = 0;
1398 info->var.blue.offset = 0;
1399 info->var.red.length = 8; /* 8bit DAC */
1400 info->var.green.length = 8;
1401 info->var.blue.length = 8;
1402 info->var.transp.offset = 0;
1403 info->var.transp.length = 0;
1406 info->var.red.offset = 10;
1407 info->var.green.offset = 5;
1408 info->var.blue.offset = 0;
1409 info->var.red.length = 5;
1410 info->var.green.length = 5;
1411 info->var.blue.length = 5;
1412 info->var.transp.offset = 15;
1413 info->var.transp.length = 1;
1416 info->var.red.offset = 11;
1417 info->var.green.offset = 5;
1418 info->var.blue.offset = 0;
1419 info->var.red.length = 5;
1420 info->var.green.length = 6;
1421 info->var.blue.length = 5;
1422 info->var.transp.offset = 0;
1425 info->var.red.offset = 16;
1426 info->var.green.offset = 8;
1427 info->var.blue.offset = 0;
1428 info->var.red.length = 8;
1429 info->var.green.length = 8;
1430 info->var.blue.length = 8;
1431 info->var.transp.offset = 0;
1432 info->var.transp.length = 0;
1435 info->var.red.offset = 16;
1436 info->var.green.offset = 8;
1437 info->var.blue.offset = 0;
1438 info->var.red.length = 8;
1439 info->var.green.length = 8;
1440 info->var.blue.length = 8;
1441 info->var.transp.offset = 24;
1442 info->var.transp.length = 8;
1448 info->var.xres = fb_width;
1449 info->var.yres = fb_height;
1451 EXPORT_SYMBOL(drm_fb_helper_fill_var);
1453 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1457 struct drm_connector *connector;
1461 for (i = 0; i < fb_helper->connector_count; i++) {
1462 connector = fb_helper->connector_info[i]->connector;
1463 count += connector->funcs->fill_modes(connector, maxX, maxY);
1469 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1471 struct drm_display_mode *mode;
1473 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1474 if (mode->hdisplay > width ||
1475 mode->vdisplay > height)
1477 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1482 EXPORT_SYMBOL(drm_has_preferred_mode);
1484 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1486 return fb_connector->connector->cmdline_mode.specified;
1489 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1490 int width, int height)
1492 struct drm_cmdline_mode *cmdline_mode;
1493 struct drm_display_mode *mode;
1494 bool prefer_non_interlace;
1496 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1497 if (cmdline_mode->specified == false)
1500 /* attempt to find a matching mode in the list of modes
1501 * we have gotten so far, if not add a CVT mode that conforms
1503 if (cmdline_mode->rb || cmdline_mode->margins)
1506 prefer_non_interlace = !cmdline_mode->interlace;
1508 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1509 /* check width/height */
1510 if (mode->hdisplay != cmdline_mode->xres ||
1511 mode->vdisplay != cmdline_mode->yres)
1514 if (cmdline_mode->refresh_specified) {
1515 if (mode->vrefresh != cmdline_mode->refresh)
1519 if (cmdline_mode->interlace) {
1520 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1522 } else if (prefer_non_interlace) {
1523 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1529 if (prefer_non_interlace) {
1530 prefer_non_interlace = false;
1535 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1537 list_add(&mode->head, &fb_helper_conn->connector->modes);
1540 EXPORT_SYMBOL(drm_pick_cmdline_mode);
1542 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1547 enable = connector->status == connector_status_connected;
1549 enable = connector->status != connector_status_disconnected;
1554 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1557 bool any_enabled = false;
1558 struct drm_connector *connector;
1561 for (i = 0; i < fb_helper->connector_count; i++) {
1562 connector = fb_helper->connector_info[i]->connector;
1563 enabled[i] = drm_connector_enabled(connector, true);
1564 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1565 enabled[i] ? "yes" : "no");
1566 any_enabled |= enabled[i];
1572 for (i = 0; i < fb_helper->connector_count; i++) {
1573 connector = fb_helper->connector_info[i]->connector;
1574 enabled[i] = drm_connector_enabled(connector, false);
1578 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1579 struct drm_display_mode **modes,
1580 struct drm_fb_offset *offsets,
1581 bool *enabled, int width, int height)
1584 bool can_clone = false;
1585 struct drm_fb_helper_connector *fb_helper_conn;
1586 struct drm_display_mode *dmt_mode, *mode;
1588 /* only contemplate cloning in the single crtc case */
1589 if (fb_helper->crtc_count > 1)
1593 for (i = 0; i < fb_helper->connector_count; i++) {
1598 /* only contemplate cloning if more than one connector is enabled */
1602 /* check the command line or if nothing common pick 1024x768 */
1604 for (i = 0; i < fb_helper->connector_count; i++) {
1607 fb_helper_conn = fb_helper->connector_info[i];
1608 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1613 for (j = 0; j < i; j++) {
1616 if (!drm_mode_equal(modes[j], modes[i]))
1622 DRM_DEBUG_KMS("can clone using command line\n");
1626 /* try and find a 1024x768 mode on each connector */
1628 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1630 for (i = 0; i < fb_helper->connector_count; i++) {
1635 fb_helper_conn = fb_helper->connector_info[i];
1636 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1637 if (drm_mode_equal(mode, dmt_mode))
1645 DRM_DEBUG_KMS("can clone using 1024x768\n");
1648 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1652 static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
1653 struct drm_display_mode **modes,
1654 struct drm_fb_offset *offsets,
1656 int h_idx, int v_idx)
1658 struct drm_fb_helper_connector *fb_helper_conn;
1660 int hoffset = 0, voffset = 0;
1662 for (i = 0; i < fb_helper->connector_count; i++) {
1663 fb_helper_conn = fb_helper->connector_info[i];
1664 if (!fb_helper_conn->connector->has_tile)
1667 if (!modes[i] && (h_idx || v_idx)) {
1668 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
1669 fb_helper_conn->connector->base.id);
1672 if (fb_helper_conn->connector->tile_h_loc < h_idx)
1673 hoffset += modes[i]->hdisplay;
1675 if (fb_helper_conn->connector->tile_v_loc < v_idx)
1676 voffset += modes[i]->vdisplay;
1678 offsets[idx].x = hoffset;
1679 offsets[idx].y = voffset;
1680 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
1684 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1685 struct drm_display_mode **modes,
1686 struct drm_fb_offset *offsets,
1687 bool *enabled, int width, int height)
1689 struct drm_fb_helper_connector *fb_helper_conn;
1691 uint64_t conn_configured = 0, mask;
1693 mask = (1 << fb_helper->connector_count) - 1;
1695 for (i = 0; i < fb_helper->connector_count; i++) {
1696 fb_helper_conn = fb_helper->connector_info[i];
1698 if (conn_configured & (1 << i))
1701 if (enabled[i] == false) {
1702 conn_configured |= (1 << i);
1706 /* first pass over all the untiled connectors */
1707 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
1710 if (tile_pass == 1) {
1711 if (fb_helper_conn->connector->tile_h_loc != 0 ||
1712 fb_helper_conn->connector->tile_v_loc != 0)
1716 if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 &&
1717 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
1718 /* if this tile_pass doesn't cover any of the tiles - keep going */
1721 /* find the tile offsets for this pass - need
1722 to find all tiles left and above */
1723 drm_get_tile_offsets(fb_helper, modes, offsets,
1724 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
1726 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1727 fb_helper_conn->connector->base.id);
1729 /* got for command line mode first */
1730 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1732 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
1733 fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0);
1734 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1736 /* No preferred modes, pick one off the list */
1737 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1738 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1741 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1743 conn_configured |= (1 << i);
1746 if ((conn_configured & mask) != mask) {
1753 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1754 struct drm_fb_helper_crtc **best_crtcs,
1755 struct drm_display_mode **modes,
1756 int n, int width, int height)
1759 struct drm_device *dev = fb_helper->dev;
1760 struct drm_connector *connector;
1761 const struct drm_connector_helper_funcs *connector_funcs;
1762 struct drm_encoder *encoder;
1763 int my_score, best_score, score;
1764 struct drm_fb_helper_crtc **crtcs, *crtc;
1765 struct drm_fb_helper_connector *fb_helper_conn;
1767 if (n == fb_helper->connector_count)
1770 fb_helper_conn = fb_helper->connector_info[n];
1771 connector = fb_helper_conn->connector;
1773 best_crtcs[n] = NULL;
1774 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1775 if (modes[n] == NULL)
1778 crtcs = kzalloc(dev->mode_config.num_connector *
1779 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1784 if (connector->status == connector_status_connected)
1786 if (drm_has_cmdline_mode(fb_helper_conn))
1788 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1791 connector_funcs = connector->helper_private;
1792 encoder = connector_funcs->best_encoder(connector);
1796 /* select a crtc for this connector and then attempt to configure
1797 remaining connectors */
1798 for (c = 0; c < fb_helper->crtc_count; c++) {
1799 crtc = &fb_helper->crtc_info[c];
1801 if ((encoder->possible_crtcs & (1 << c)) == 0)
1804 for (o = 0; o < n; o++)
1805 if (best_crtcs[o] == crtc)
1809 /* ignore cloning unless only a single crtc */
1810 if (fb_helper->crtc_count > 1)
1813 if (!drm_mode_equal(modes[o], modes[n]))
1818 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1819 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1821 if (score > best_score) {
1823 memcpy(best_crtcs, crtcs,
1824 dev->mode_config.num_connector *
1825 sizeof(struct drm_fb_helper_crtc *));
1833 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1835 struct drm_device *dev = fb_helper->dev;
1836 struct drm_fb_helper_crtc **crtcs;
1837 struct drm_display_mode **modes;
1838 struct drm_fb_offset *offsets;
1839 struct drm_mode_set *modeset;
1844 DRM_DEBUG_KMS("\n");
1846 width = dev->mode_config.max_width;
1847 height = dev->mode_config.max_height;
1849 crtcs = kcalloc(dev->mode_config.num_connector,
1850 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1851 modes = kcalloc(dev->mode_config.num_connector,
1852 sizeof(struct drm_display_mode *), GFP_KERNEL);
1853 offsets = kcalloc(dev->mode_config.num_connector,
1854 sizeof(struct drm_fb_offset), GFP_KERNEL);
1855 enabled = kcalloc(dev->mode_config.num_connector,
1856 sizeof(bool), GFP_KERNEL);
1857 if (!crtcs || !modes || !enabled || !offsets) {
1858 DRM_ERROR("Memory allocation failed\n");
1863 drm_enable_connectors(fb_helper, enabled);
1865 if (!(fb_helper->funcs->initial_config &&
1866 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
1868 enabled, width, height))) {
1869 memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
1870 memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
1871 memset(offsets, 0, dev->mode_config.num_connector*sizeof(offsets[0]));
1873 if (!drm_target_cloned(fb_helper, modes, offsets,
1874 enabled, width, height) &&
1875 !drm_target_preferred(fb_helper, modes, offsets,
1876 enabled, width, height))
1877 DRM_ERROR("Unable to find initial modes\n");
1879 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1882 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1885 /* need to set the modesets up here for use later */
1886 /* fill out the connector<->crtc mappings into the modesets */
1887 for (i = 0; i < fb_helper->crtc_count; i++) {
1888 modeset = &fb_helper->crtc_info[i].mode_set;
1889 modeset->num_connectors = 0;
1893 for (i = 0; i < fb_helper->connector_count; i++) {
1894 struct drm_display_mode *mode = modes[i];
1895 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1896 struct drm_fb_offset *offset = &offsets[i];
1897 modeset = &fb_crtc->mode_set;
1899 if (mode && fb_crtc) {
1900 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
1901 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
1902 fb_crtc->desired_mode = mode;
1903 fb_crtc->x = offset->x;
1904 fb_crtc->y = offset->y;
1906 drm_mode_destroy(dev, modeset->mode);
1907 modeset->mode = drm_mode_duplicate(dev,
1908 fb_crtc->desired_mode);
1909 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1910 modeset->fb = fb_helper->fb;
1911 modeset->x = offset->x;
1912 modeset->y = offset->y;
1916 /* Clear out any old modes if there are no more connected outputs. */
1917 for (i = 0; i < fb_helper->crtc_count; i++) {
1918 modeset = &fb_helper->crtc_info[i].mode_set;
1919 if (modeset->num_connectors == 0) {
1920 BUG_ON(modeset->fb);
1922 drm_mode_destroy(dev, modeset->mode);
1923 modeset->mode = NULL;
1934 * drm_fb_helper_initial_config - setup a sane initial connector configuration
1935 * @fb_helper: fb_helper device struct
1936 * @bpp_sel: bpp value to use for the framebuffer configuration
1938 * Scans the CRTCs and connectors and tries to put together an initial setup.
1939 * At the moment, this is a cloned configuration across all heads with
1940 * a new framebuffer object as the backing store.
1942 * Note that this also registers the fbdev and so allows userspace to call into
1943 * the driver through the fbdev interfaces.
1945 * This function will call down into the ->fb_probe callback to let
1946 * the driver allocate and initialize the fbdev info structure and the drm
1947 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1948 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1949 * values for the fbdev info structure.
1952 * Zero if everything went ok, nonzero otherwise.
1954 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1956 struct drm_device *dev = fb_helper->dev;
1959 if (!drm_fbdev_emulation)
1962 mutex_lock(&dev->mode_config.mutex);
1963 count = drm_fb_helper_probe_connector_modes(fb_helper,
1964 dev->mode_config.max_width,
1965 dev->mode_config.max_height);
1966 mutex_unlock(&dev->mode_config.mutex);
1968 * we shouldn't end up with no modes here.
1971 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
1973 drm_setup_crtcs(fb_helper);
1975 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1977 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1980 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1981 * probing all the outputs attached to the fb
1982 * @fb_helper: the drm_fb_helper
1984 * Scan the connectors attached to the fb_helper and try to put together a
1985 * setup after *notification of a change in output configuration.
1987 * Called at runtime, takes the mode config locks to be able to check/change the
1988 * modeset configuration. Must be run from process context (which usually means
1989 * either the output polling work or a work item launched from the driver's
1990 * hotplug interrupt).
1992 * Note that drivers may call this even before calling
1993 * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
1994 * for a race-free fbcon setup and will make sure that the fbdev emulation will
1995 * not miss any hotplug events.
1998 * 0 on success and a non-zero error code otherwise.
2000 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
2002 struct drm_device *dev = fb_helper->dev;
2003 u32 max_width, max_height;
2005 if (!drm_fbdev_emulation)
2008 mutex_lock(&fb_helper->dev->mode_config.mutex);
2009 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
2010 fb_helper->delayed_hotplug = true;
2011 mutex_unlock(&fb_helper->dev->mode_config.mutex);
2014 DRM_DEBUG_KMS("\n");
2016 max_width = fb_helper->fb->width;
2017 max_height = fb_helper->fb->height;
2019 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
2020 mutex_unlock(&fb_helper->dev->mode_config.mutex);
2022 drm_modeset_lock_all(dev);
2023 drm_setup_crtcs(fb_helper);
2024 drm_modeset_unlock_all(dev);
2025 drm_fb_helper_set_par(fb_helper->fbdev);
2029 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
2031 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
2032 * but the module doesn't depend on any fb console symbols. At least
2033 * attempt to load fbcon to avoid leaving the system without a usable console.
2035 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
2036 static int __init drm_fb_helper_modinit(void)
2038 const char *name = "fbcon";
2039 struct module *fbcon;
2041 mutex_lock(&module_mutex);
2042 fbcon = find_module(name);
2043 mutex_unlock(&module_mutex);
2046 request_module_nowait(name);
2050 module_init(drm_fb_helper_modinit);