2 * Copyright (c) 2006-2009 Red Hat Inc.
3 * Copyright (c) 2006-2008 Intel Corporation
4 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
6 * DRM framebuffer helper functions
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
27 * Dave Airlie <airlied@linux.ie>
28 * Jesse Barnes <jesse.barnes@intel.com>
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #include <linux/kernel.h>
33 #include <linux/sysrq.h>
34 #include <linux/slab.h>
36 #include <linux/module.h>
38 #include <drm/drm_crtc.h>
39 #include <drm/drm_fb_helper.h>
40 #include <drm/drm_crtc_helper.h>
42 static LIST_HEAD(kernel_fb_helper_list);
47 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
48 * mode setting driver. They can be used mostly independently from the crtc
49 * helper functions used by many drivers to implement the kernel mode setting
52 * Initialization is done as a four-step process with drm_fb_helper_prepare(),
53 * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
54 * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
55 * default behaviour can override the third step with their own code.
56 * Teardown is done with drm_fb_helper_fini().
58 * At runtime drivers should restore the fbdev console by calling
59 * drm_fb_helper_restore_fbdev_mode_unlocked() from their ->lastclose callback.
60 * They should also notify the fb helper code from updates to the output
61 * configuration by calling drm_fb_helper_hotplug_event(). For easier
62 * integration with the output polling code in drm_crtc_helper.c the modeset
63 * code provides a ->output_poll_changed callback.
65 * All other functions exported by the fb helper library can be used to
66 * implement the fbdev driver interface by the driver.
68 * It is possible, though perhaps somewhat tricky, to implement race-free
69 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
70 * helper must be called first to initialize the minimum required to make
71 * hotplug detection work. Drivers also need to make sure to properly set up
72 * the dev->mode_config.funcs member. After calling drm_kms_helper_poll_init()
73 * it is safe to enable interrupts and start processing hotplug events. At the
74 * same time, drivers should initialize all modeset objects such as CRTCs,
75 * encoders and connectors. To finish up the fbdev helper initialization, the
76 * drm_fb_helper_init() function is called. To probe for all attached displays
77 * and set up an initial configuration using the detected hardware, drivers
78 * should call drm_fb_helper_single_add_all_connectors() followed by
79 * drm_fb_helper_initial_config().
83 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
85 * @fb_helper: fbdev initialized with drm_fb_helper_init
87 * This functions adds all the available connectors for use with the given
88 * fb_helper. This is a separate step to allow drivers to freely assign
89 * connectors to the fbdev, e.g. if some are reserved for special purposes or
90 * not adequate to be used for the fbcon.
92 * This function is protected against concurrent connector hotadds/removals
93 * using drm_fb_helper_add_one_connector() and
94 * drm_fb_helper_remove_one_connector().
96 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
98 struct drm_device *dev = fb_helper->dev;
99 struct drm_connector *connector;
102 mutex_lock(&dev->mode_config.mutex);
103 drm_for_each_connector(connector, dev) {
104 struct drm_fb_helper_connector *fb_helper_connector;
106 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
107 if (!fb_helper_connector)
110 fb_helper_connector->connector = connector;
111 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
113 mutex_unlock(&dev->mode_config.mutex);
116 for (i = 0; i < fb_helper->connector_count; i++) {
117 kfree(fb_helper->connector_info[i]);
118 fb_helper->connector_info[i] = NULL;
120 fb_helper->connector_count = 0;
121 mutex_unlock(&dev->mode_config.mutex);
125 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
127 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector)
129 struct drm_fb_helper_connector **temp;
130 struct drm_fb_helper_connector *fb_helper_connector;
132 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
133 if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) {
134 temp = krealloc(fb_helper->connector_info, sizeof(struct drm_fb_helper_connector *) * (fb_helper->connector_count + 1), GFP_KERNEL);
138 fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1;
139 fb_helper->connector_info = temp;
143 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
144 if (!fb_helper_connector)
147 fb_helper_connector->connector = connector;
148 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
151 EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
153 static void remove_from_modeset(struct drm_mode_set *set,
154 struct drm_connector *connector)
158 for (i = 0; i < set->num_connectors; i++) {
159 if (set->connectors[i] == connector)
163 if (i == set->num_connectors)
166 for (j = i + 1; j < set->num_connectors; j++) {
167 set->connectors[j - 1] = set->connectors[j];
169 set->num_connectors--;
171 /* because i915 is pissy about this..
172 * TODO maybe need to makes sure we set it back to !=NULL somewhere?
174 if (set->num_connectors == 0)
178 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
179 struct drm_connector *connector)
181 struct drm_fb_helper_connector *fb_helper_connector;
184 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
186 for (i = 0; i < fb_helper->connector_count; i++) {
187 if (fb_helper->connector_info[i]->connector == connector)
191 if (i == fb_helper->connector_count)
193 fb_helper_connector = fb_helper->connector_info[i];
195 for (j = i + 1; j < fb_helper->connector_count; j++) {
196 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
198 fb_helper->connector_count--;
199 kfree(fb_helper_connector);
201 /* also cleanup dangling references to the connector: */
202 for (i = 0; i < fb_helper->crtc_count; i++)
203 remove_from_modeset(&fb_helper->crtc_info[i].mode_set, connector);
207 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
209 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
211 uint16_t *r_base, *g_base, *b_base;
214 if (helper->funcs->gamma_get == NULL)
217 r_base = crtc->gamma_store;
218 g_base = r_base + crtc->gamma_size;
219 b_base = g_base + crtc->gamma_size;
221 for (i = 0; i < crtc->gamma_size; i++)
222 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
225 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
227 uint16_t *r_base, *g_base, *b_base;
229 if (crtc->funcs->gamma_set == NULL)
232 r_base = crtc->gamma_store;
233 g_base = r_base + crtc->gamma_size;
234 b_base = g_base + crtc->gamma_size;
236 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
240 * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
241 * @info: fbdev registered by the helper
243 int drm_fb_helper_debug_enter(struct fb_info *info)
245 struct drm_fb_helper *helper = info->par;
246 const struct drm_crtc_helper_funcs *funcs;
249 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
250 for (i = 0; i < helper->crtc_count; i++) {
251 struct drm_mode_set *mode_set =
252 &helper->crtc_info[i].mode_set;
254 if (!mode_set->crtc->enabled)
257 funcs = mode_set->crtc->helper_private;
258 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
259 funcs->mode_set_base_atomic(mode_set->crtc,
263 ENTER_ATOMIC_MODE_SET);
269 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
271 /* Find the real fb for a given fb helper CRTC */
272 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
274 struct drm_device *dev = crtc->dev;
277 drm_for_each_crtc(c, dev) {
278 if (crtc->base.id == c->base.id)
279 return c->primary->fb;
286 * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
287 * @info: fbdev registered by the helper
289 int drm_fb_helper_debug_leave(struct fb_info *info)
291 struct drm_fb_helper *helper = info->par;
292 struct drm_crtc *crtc;
293 const struct drm_crtc_helper_funcs *funcs;
294 struct drm_framebuffer *fb;
297 for (i = 0; i < helper->crtc_count; i++) {
298 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
299 crtc = mode_set->crtc;
300 funcs = crtc->helper_private;
301 fb = drm_mode_config_fb(crtc);
307 DRM_ERROR("no fb to restore??\n");
311 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
312 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
313 crtc->y, LEAVE_ATOMIC_MODE_SET);
318 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
320 static bool restore_fbdev_mode(struct drm_fb_helper *fb_helper)
322 struct drm_device *dev = fb_helper->dev;
323 struct drm_plane *plane;
327 drm_warn_on_modeset_not_all_locked(dev);
329 drm_for_each_plane(plane, dev) {
330 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
331 drm_plane_force_disable(plane);
333 if (dev->mode_config.rotation_property) {
334 drm_mode_plane_set_obj_prop(plane,
335 dev->mode_config.rotation_property,
340 for (i = 0; i < fb_helper->crtc_count; i++) {
341 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
342 struct drm_crtc *crtc = mode_set->crtc;
345 if (crtc->funcs->cursor_set) {
346 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
351 ret = drm_mode_set_config_internal(mode_set);
359 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
360 * @fb_helper: fbcon to restore
362 * This should be called from driver's drm ->lastclose callback
363 * when implementing an fbcon on top of kms using this helper. This ensures that
364 * the user isn't greeted with a black screen when e.g. X dies.
366 bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
368 struct drm_device *dev = fb_helper->dev;
370 bool do_delayed = false;
372 drm_modeset_lock_all(dev);
373 ret = restore_fbdev_mode(fb_helper);
375 do_delayed = fb_helper->delayed_hotplug;
377 fb_helper->delayed_hotplug = false;
378 drm_modeset_unlock_all(dev);
381 drm_fb_helper_hotplug_event(fb_helper);
384 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
387 * restore fbcon display for all kms driver's using this helper, used for sysrq
388 * and panic handling.
390 static bool drm_fb_helper_force_kernel_mode(void)
392 bool ret, error = false;
393 struct drm_fb_helper *helper;
395 if (list_empty(&kernel_fb_helper_list))
398 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
399 struct drm_device *dev = helper->dev;
401 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
404 drm_modeset_lock_all(dev);
405 ret = restore_fbdev_mode(helper);
408 drm_modeset_unlock_all(dev);
413 static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
415 struct drm_device *dev = fb_helper->dev;
416 struct drm_crtc *crtc;
417 int bound = 0, crtcs_bound = 0;
419 /* Sometimes user space wants everything disabled, so don't steal the
420 * display if there's a master. */
421 if (dev->primary->master)
424 drm_for_each_crtc(crtc, dev) {
425 if (crtc->primary->fb)
427 if (crtc->primary->fb == fb_helper->fb)
431 if (bound < crtcs_bound)
437 #ifdef CONFIG_MAGIC_SYSRQ
438 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
441 ret = drm_fb_helper_force_kernel_mode();
443 DRM_ERROR("Failed to restore crtc configuration\n");
445 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
447 static void drm_fb_helper_sysrq(int dummy1)
449 schedule_work(&drm_fb_helper_restore_work);
452 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
453 .handler = drm_fb_helper_sysrq,
454 .help_msg = "force-fb(V)",
455 .action_msg = "Restore framebuffer console",
458 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
461 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
463 struct drm_fb_helper *fb_helper = info->par;
464 struct drm_device *dev = fb_helper->dev;
465 struct drm_crtc *crtc;
466 struct drm_connector *connector;
470 * For each CRTC in this fb, turn the connectors on/off.
472 drm_modeset_lock_all(dev);
473 if (!drm_fb_helper_is_bound(fb_helper)) {
474 drm_modeset_unlock_all(dev);
478 for (i = 0; i < fb_helper->crtc_count; i++) {
479 crtc = fb_helper->crtc_info[i].mode_set.crtc;
484 /* Walk the connectors & encoders on this fb turning them on/off */
485 for (j = 0; j < fb_helper->connector_count; j++) {
486 connector = fb_helper->connector_info[j]->connector;
487 connector->funcs->dpms(connector, dpms_mode);
488 drm_object_property_set_value(&connector->base,
489 dev->mode_config.dpms_property, dpms_mode);
492 drm_modeset_unlock_all(dev);
496 * drm_fb_helper_blank - implementation for ->fb_blank
497 * @blank: desired blanking state
498 * @info: fbdev registered by the helper
500 int drm_fb_helper_blank(int blank, struct fb_info *info)
502 if (oops_in_progress)
506 /* Display: On; HSync: On, VSync: On */
507 case FB_BLANK_UNBLANK:
508 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
510 /* Display: Off; HSync: On, VSync: On */
511 case FB_BLANK_NORMAL:
512 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
514 /* Display: Off; HSync: Off, VSync: On */
515 case FB_BLANK_HSYNC_SUSPEND:
516 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
518 /* Display: Off; HSync: On, VSync: Off */
519 case FB_BLANK_VSYNC_SUSPEND:
520 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
522 /* Display: Off; HSync: Off, VSync: Off */
523 case FB_BLANK_POWERDOWN:
524 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
529 EXPORT_SYMBOL(drm_fb_helper_blank);
531 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
535 for (i = 0; i < helper->connector_count; i++)
536 kfree(helper->connector_info[i]);
537 kfree(helper->connector_info);
538 for (i = 0; i < helper->crtc_count; i++) {
539 kfree(helper->crtc_info[i].mode_set.connectors);
540 if (helper->crtc_info[i].mode_set.mode)
541 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
543 kfree(helper->crtc_info);
547 * drm_fb_helper_prepare - setup a drm_fb_helper structure
549 * @helper: driver-allocated fbdev helper structure to set up
550 * @funcs: pointer to structure of functions associate with this helper
552 * Sets up the bare minimum to make the framebuffer helper usable. This is
553 * useful to implement race-free initialization of the polling helpers.
555 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
556 const struct drm_fb_helper_funcs *funcs)
558 INIT_LIST_HEAD(&helper->kernel_fb_list);
559 helper->funcs = funcs;
562 EXPORT_SYMBOL(drm_fb_helper_prepare);
565 * drm_fb_helper_init - initialize a drm_fb_helper structure
567 * @fb_helper: driver-allocated fbdev helper structure to initialize
568 * @crtc_count: maximum number of crtcs to support in this fbdev emulation
569 * @max_conn_count: max connector count
571 * This allocates the structures for the fbdev helper with the given limits.
572 * Note that this won't yet touch the hardware (through the driver interfaces)
573 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
574 * to allow driver writes more control over the exact init sequence.
576 * Drivers must call drm_fb_helper_prepare() before calling this function.
579 * Zero if everything went ok, nonzero otherwise.
581 int drm_fb_helper_init(struct drm_device *dev,
582 struct drm_fb_helper *fb_helper,
583 int crtc_count, int max_conn_count)
585 struct drm_crtc *crtc;
591 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
592 if (!fb_helper->crtc_info)
595 fb_helper->crtc_count = crtc_count;
596 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
597 if (!fb_helper->connector_info) {
598 kfree(fb_helper->crtc_info);
601 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
602 fb_helper->connector_count = 0;
604 for (i = 0; i < crtc_count; i++) {
605 fb_helper->crtc_info[i].mode_set.connectors =
606 kcalloc(max_conn_count,
607 sizeof(struct drm_connector *),
610 if (!fb_helper->crtc_info[i].mode_set.connectors)
612 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
616 drm_for_each_crtc(crtc, dev) {
617 fb_helper->crtc_info[i].mode_set.crtc = crtc;
623 drm_fb_helper_crtc_free(fb_helper);
626 EXPORT_SYMBOL(drm_fb_helper_init);
629 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
630 * @fb_helper: driver-allocated fbdev helper
632 * A helper to alloc fb_info and the members cmap and apertures. Called
633 * by the driver within the fb_probe fb_helper callback function.
636 * fb_info pointer if things went okay, pointer containing error code
639 struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
641 struct device *dev = fb_helper->dev->dev;
642 struct fb_info *info;
645 info = framebuffer_alloc(0, dev);
647 return ERR_PTR(-ENOMEM);
649 ret = fb_alloc_cmap(&info->cmap, 256, 0);
653 info->apertures = alloc_apertures(1);
654 if (!info->apertures) {
659 fb_helper->fbdev = info;
664 fb_dealloc_cmap(&info->cmap);
666 framebuffer_release(info);
669 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
672 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
673 * @fb_helper: driver-allocated fbdev helper
675 * A wrapper around unregister_framebuffer, to release the fb_info
678 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
680 if (fb_helper && fb_helper->fbdev)
681 unregister_framebuffer(fb_helper->fbdev);
683 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
686 * drm_fb_helper_release_fbi - dealloc fb_info and its members
687 * @fb_helper: driver-allocated fbdev helper
689 * A helper to free memory taken by fb_info and the members cmap and
692 void drm_fb_helper_release_fbi(struct drm_fb_helper *fb_helper)
695 struct fb_info *info = fb_helper->fbdev;
699 fb_dealloc_cmap(&info->cmap);
700 framebuffer_release(info);
703 fb_helper->fbdev = NULL;
706 EXPORT_SYMBOL(drm_fb_helper_release_fbi);
708 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
710 if (!list_empty(&fb_helper->kernel_fb_list)) {
711 list_del(&fb_helper->kernel_fb_list);
712 if (list_empty(&kernel_fb_helper_list)) {
713 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
717 drm_fb_helper_crtc_free(fb_helper);
720 EXPORT_SYMBOL(drm_fb_helper_fini);
723 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
724 * @fb_helper: driver-allocated fbdev helper
726 * A wrapper around unlink_framebuffer implemented by fbdev core
728 void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
730 if (fb_helper && fb_helper->fbdev)
731 unlink_framebuffer(fb_helper->fbdev);
733 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
736 * drm_fb_helper_sys_read - wrapper around fb_sys_read
737 * @info: fb_info struct pointer
738 * @buf: userspace buffer to read from framebuffer memory
739 * @count: number of bytes to read from framebuffer memory
740 * @ppos: read offset within framebuffer memory
742 * A wrapper around fb_sys_read implemented by fbdev core
744 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
745 size_t count, loff_t *ppos)
747 return fb_sys_read(info, buf, count, ppos);
749 EXPORT_SYMBOL(drm_fb_helper_sys_read);
752 * drm_fb_helper_sys_write - wrapper around fb_sys_write
753 * @info: fb_info struct pointer
754 * @buf: userspace buffer to write to framebuffer memory
755 * @count: number of bytes to write to framebuffer memory
756 * @ppos: write offset within framebuffer memory
758 * A wrapper around fb_sys_write implemented by fbdev core
760 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
761 size_t count, loff_t *ppos)
763 return fb_sys_write(info, buf, count, ppos);
765 EXPORT_SYMBOL(drm_fb_helper_sys_write);
768 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
769 * @info: fbdev registered by the helper
770 * @rect: info about rectangle to fill
772 * A wrapper around sys_fillrect implemented by fbdev core
774 void drm_fb_helper_sys_fillrect(struct fb_info *info,
775 const struct fb_fillrect *rect)
777 sys_fillrect(info, rect);
779 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
782 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
783 * @info: fbdev registered by the helper
784 * @area: info about area to copy
786 * A wrapper around sys_copyarea implemented by fbdev core
788 void drm_fb_helper_sys_copyarea(struct fb_info *info,
789 const struct fb_copyarea *area)
791 sys_copyarea(info, area);
793 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
796 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
797 * @info: fbdev registered by the helper
798 * @image: info about image to blit
800 * A wrapper around sys_imageblit implemented by fbdev core
802 void drm_fb_helper_sys_imageblit(struct fb_info *info,
803 const struct fb_image *image)
805 sys_imageblit(info, image);
807 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
810 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
811 * @info: fbdev registered by the helper
812 * @rect: info about rectangle to fill
814 * A wrapper around cfb_imageblit implemented by fbdev core
816 void drm_fb_helper_cfb_fillrect(struct fb_info *info,
817 const struct fb_fillrect *rect)
819 cfb_fillrect(info, rect);
821 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
824 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
825 * @info: fbdev registered by the helper
826 * @area: info about area to copy
828 * A wrapper around cfb_copyarea implemented by fbdev core
830 void drm_fb_helper_cfb_copyarea(struct fb_info *info,
831 const struct fb_copyarea *area)
833 cfb_copyarea(info, area);
835 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
838 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
839 * @info: fbdev registered by the helper
840 * @image: info about image to blit
842 * A wrapper around cfb_imageblit implemented by fbdev core
844 void drm_fb_helper_cfb_imageblit(struct fb_info *info,
845 const struct fb_image *image)
847 cfb_imageblit(info, image);
849 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
852 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
853 * @fb_helper: driver-allocated fbdev helper
854 * @state: desired state, zero to resume, non-zero to suspend
856 * A wrapper around fb_set_suspend implemented by fbdev core
858 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, int state)
860 if (fb_helper && fb_helper->fbdev)
861 fb_set_suspend(fb_helper->fbdev, state);
863 EXPORT_SYMBOL(drm_fb_helper_set_suspend);
865 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
866 u16 blue, u16 regno, struct fb_info *info)
868 struct drm_fb_helper *fb_helper = info->par;
869 struct drm_framebuffer *fb = fb_helper->fb;
872 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
875 /* place color in psuedopalette */
878 palette = (u32 *)info->pseudo_palette;
879 red >>= (16 - info->var.red.length);
880 green >>= (16 - info->var.green.length);
881 blue >>= (16 - info->var.blue.length);
882 value = (red << info->var.red.offset) |
883 (green << info->var.green.offset) |
884 (blue << info->var.blue.offset);
885 if (info->var.transp.length > 0) {
886 u32 mask = (1 << info->var.transp.length) - 1;
887 mask <<= info->var.transp.offset;
890 palette[regno] = value;
895 * The driver really shouldn't advertise pseudo/directcolor
896 * visuals if it can't deal with the palette.
898 if (WARN_ON(!fb_helper->funcs->gamma_set ||
899 !fb_helper->funcs->gamma_get))
904 if (fb->bits_per_pixel == 16) {
907 if (fb->depth == 16 && regno > 63)
909 if (fb->depth == 15 && regno > 31)
912 if (fb->depth == 16) {
916 for (i = 0; i < 8; i++)
917 fb_helper->funcs->gamma_set(crtc, red,
918 green, blue, pindex + i);
921 fb_helper->funcs->gamma_get(crtc, &r,
925 for (i = 0; i < 4; i++)
926 fb_helper->funcs->gamma_set(crtc, r,
933 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
938 * drm_fb_helper_setcmap - implementation for ->fb_setcmap
940 * @info: fbdev registered by the helper
942 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
944 struct drm_fb_helper *fb_helper = info->par;
945 struct drm_device *dev = fb_helper->dev;
946 const struct drm_crtc_helper_funcs *crtc_funcs;
947 u16 *red, *green, *blue, *transp;
948 struct drm_crtc *crtc;
952 if (oops_in_progress)
955 drm_modeset_lock_all(dev);
956 if (!drm_fb_helper_is_bound(fb_helper)) {
957 drm_modeset_unlock_all(dev);
961 for (i = 0; i < fb_helper->crtc_count; i++) {
962 crtc = fb_helper->crtc_info[i].mode_set.crtc;
963 crtc_funcs = crtc->helper_private;
968 transp = cmap->transp;
971 for (j = 0; j < cmap->len; j++) {
972 u16 hred, hgreen, hblue, htransp = 0xffff;
981 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
985 if (crtc_funcs->load_lut)
986 crtc_funcs->load_lut(crtc);
989 drm_modeset_unlock_all(dev);
992 EXPORT_SYMBOL(drm_fb_helper_setcmap);
995 * drm_fb_helper_check_var - implementation for ->fb_check_var
996 * @var: screeninfo to check
997 * @info: fbdev registered by the helper
999 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1000 struct fb_info *info)
1002 struct drm_fb_helper *fb_helper = info->par;
1003 struct drm_framebuffer *fb = fb_helper->fb;
1006 if (var->pixclock != 0 || in_dbg_master())
1009 /* Need to resize the fb object !!! */
1010 if (var->bits_per_pixel > fb->bits_per_pixel ||
1011 var->xres > fb->width || var->yres > fb->height ||
1012 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1013 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
1014 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1015 var->xres, var->yres, var->bits_per_pixel,
1016 var->xres_virtual, var->yres_virtual,
1017 fb->width, fb->height, fb->bits_per_pixel);
1021 switch (var->bits_per_pixel) {
1023 depth = (var->green.length == 6) ? 16 : 15;
1026 depth = (var->transp.length > 0) ? 32 : 24;
1029 depth = var->bits_per_pixel;
1035 var->red.offset = 0;
1036 var->green.offset = 0;
1037 var->blue.offset = 0;
1038 var->red.length = 8;
1039 var->green.length = 8;
1040 var->blue.length = 8;
1041 var->transp.length = 0;
1042 var->transp.offset = 0;
1045 var->red.offset = 10;
1046 var->green.offset = 5;
1047 var->blue.offset = 0;
1048 var->red.length = 5;
1049 var->green.length = 5;
1050 var->blue.length = 5;
1051 var->transp.length = 1;
1052 var->transp.offset = 15;
1055 var->red.offset = 11;
1056 var->green.offset = 5;
1057 var->blue.offset = 0;
1058 var->red.length = 5;
1059 var->green.length = 6;
1060 var->blue.length = 5;
1061 var->transp.length = 0;
1062 var->transp.offset = 0;
1065 var->red.offset = 16;
1066 var->green.offset = 8;
1067 var->blue.offset = 0;
1068 var->red.length = 8;
1069 var->green.length = 8;
1070 var->blue.length = 8;
1071 var->transp.length = 0;
1072 var->transp.offset = 0;
1075 var->red.offset = 16;
1076 var->green.offset = 8;
1077 var->blue.offset = 0;
1078 var->red.length = 8;
1079 var->green.length = 8;
1080 var->blue.length = 8;
1081 var->transp.length = 8;
1082 var->transp.offset = 24;
1089 EXPORT_SYMBOL(drm_fb_helper_check_var);
1092 * drm_fb_helper_set_par - implementation for ->fb_set_par
1093 * @info: fbdev registered by the helper
1095 * This will let fbcon do the mode init and is called at initialization time by
1096 * the fbdev core when registering the driver, and later on through the hotplug
1099 int drm_fb_helper_set_par(struct fb_info *info)
1101 struct drm_fb_helper *fb_helper = info->par;
1102 struct fb_var_screeninfo *var = &info->var;
1104 if (oops_in_progress)
1107 if (var->pixclock != 0) {
1108 DRM_ERROR("PIXEL CLOCK SET\n");
1112 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
1116 EXPORT_SYMBOL(drm_fb_helper_set_par);
1119 * drm_fb_helper_pan_display - implementation for ->fb_pan_display
1120 * @var: updated screen information
1121 * @info: fbdev registered by the helper
1123 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1124 struct fb_info *info)
1126 struct drm_fb_helper *fb_helper = info->par;
1127 struct drm_device *dev = fb_helper->dev;
1128 struct drm_mode_set *modeset;
1132 if (oops_in_progress)
1135 drm_modeset_lock_all(dev);
1136 if (!drm_fb_helper_is_bound(fb_helper)) {
1137 drm_modeset_unlock_all(dev);
1141 for (i = 0; i < fb_helper->crtc_count; i++) {
1142 modeset = &fb_helper->crtc_info[i].mode_set;
1144 modeset->x = var->xoffset;
1145 modeset->y = var->yoffset;
1147 if (modeset->num_connectors) {
1148 ret = drm_mode_set_config_internal(modeset);
1150 info->var.xoffset = var->xoffset;
1151 info->var.yoffset = var->yoffset;
1155 drm_modeset_unlock_all(dev);
1158 EXPORT_SYMBOL(drm_fb_helper_pan_display);
1161 * Allocates the backing storage and sets up the fbdev info structure through
1162 * the ->fb_probe callback and then registers the fbdev and sets up the panic
1165 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1171 struct fb_info *info;
1172 struct drm_fb_helper_surface_size sizes;
1175 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1176 sizes.surface_depth = 24;
1177 sizes.surface_bpp = 32;
1178 sizes.fb_width = (unsigned)-1;
1179 sizes.fb_height = (unsigned)-1;
1181 /* if driver picks 8 or 16 by default use that
1182 for both depth/bpp */
1183 if (preferred_bpp != sizes.surface_bpp)
1184 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
1186 /* first up get a count of crtcs now in use and new min/maxes width/heights */
1187 for (i = 0; i < fb_helper->connector_count; i++) {
1188 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
1189 struct drm_cmdline_mode *cmdline_mode;
1191 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1193 if (cmdline_mode->bpp_specified) {
1194 switch (cmdline_mode->bpp) {
1196 sizes.surface_depth = sizes.surface_bpp = 8;
1199 sizes.surface_depth = 15;
1200 sizes.surface_bpp = 16;
1203 sizes.surface_depth = sizes.surface_bpp = 16;
1206 sizes.surface_depth = sizes.surface_bpp = 24;
1209 sizes.surface_depth = 24;
1210 sizes.surface_bpp = 32;
1218 for (i = 0; i < fb_helper->crtc_count; i++) {
1219 struct drm_display_mode *desired_mode;
1220 struct drm_mode_set *mode_set;
1222 /* in case of tile group, are we the last tile vert or horiz?
1223 * If no tile group you are always the last one both vertically
1226 bool lastv = true, lasth = true;
1228 desired_mode = fb_helper->crtc_info[i].desired_mode;
1229 mode_set = &fb_helper->crtc_info[i].mode_set;
1236 x = fb_helper->crtc_info[i].x;
1237 y = fb_helper->crtc_info[i].y;
1239 if (gamma_size == 0)
1240 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1242 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1243 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
1245 for (j = 0; j < mode_set->num_connectors; j++) {
1246 struct drm_connector *connector = mode_set->connectors[j];
1247 if (connector->has_tile) {
1248 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1249 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1250 /* cloning to multiple tiles is just crazy-talk, so: */
1256 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1258 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
1261 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
1262 /* hmm everyone went away - assume VGA cable just fell out
1263 and will come back later. */
1264 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
1265 sizes.fb_width = sizes.surface_width = 1024;
1266 sizes.fb_height = sizes.surface_height = 768;
1269 /* push down into drivers */
1270 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1274 info = fb_helper->fbdev;
1277 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1278 * events, but at init time drm_setup_crtcs needs to be called before
1279 * the fb is allocated (since we need to figure out the desired size of
1280 * the fb before we can allocate it ...). Hence we need to fix things up
1283 for (i = 0; i < fb_helper->crtc_count; i++)
1284 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1285 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1288 info->var.pixclock = 0;
1289 if (register_framebuffer(info) < 0)
1292 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
1293 info->node, info->fix.id);
1295 if (list_empty(&kernel_fb_helper_list)) {
1296 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1299 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1305 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1306 * @info: fbdev registered by the helper
1307 * @pitch: desired pitch
1308 * @depth: desired depth
1310 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1311 * fbdev emulations. Drivers which support acceleration methods which impose
1312 * additional constraints need to set up their own limits.
1314 * Drivers should call this (or their equivalent setup code) from their
1315 * ->fb_probe callback.
1317 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1320 info->fix.type = FB_TYPE_PACKED_PIXELS;
1321 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1322 FB_VISUAL_TRUECOLOR;
1323 info->fix.mmio_start = 0;
1324 info->fix.mmio_len = 0;
1325 info->fix.type_aux = 0;
1326 info->fix.xpanstep = 1; /* doing it in hw */
1327 info->fix.ypanstep = 1; /* doing it in hw */
1328 info->fix.ywrapstep = 0;
1329 info->fix.accel = FB_ACCEL_NONE;
1331 info->fix.line_length = pitch;
1334 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1337 * drm_fb_helper_fill_var - initalizes variable fbdev information
1338 * @info: fbdev instance to set up
1339 * @fb_helper: fb helper instance to use as template
1340 * @fb_width: desired fb width
1341 * @fb_height: desired fb height
1343 * Sets up the variable fbdev metainformation from the given fb helper instance
1344 * and the drm framebuffer allocated in fb_helper->fb.
1346 * Drivers should call this (or their equivalent setup code) from their
1347 * ->fb_probe callback after having allocated the fbdev backing
1348 * storage framebuffer.
1350 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1351 uint32_t fb_width, uint32_t fb_height)
1353 struct drm_framebuffer *fb = fb_helper->fb;
1354 info->pseudo_palette = fb_helper->pseudo_palette;
1355 info->var.xres_virtual = fb->width;
1356 info->var.yres_virtual = fb->height;
1357 info->var.bits_per_pixel = fb->bits_per_pixel;
1358 info->var.accel_flags = FB_ACCELF_TEXT;
1359 info->var.xoffset = 0;
1360 info->var.yoffset = 0;
1361 info->var.activate = FB_ACTIVATE_NOW;
1362 info->var.height = -1;
1363 info->var.width = -1;
1365 switch (fb->depth) {
1367 info->var.red.offset = 0;
1368 info->var.green.offset = 0;
1369 info->var.blue.offset = 0;
1370 info->var.red.length = 8; /* 8bit DAC */
1371 info->var.green.length = 8;
1372 info->var.blue.length = 8;
1373 info->var.transp.offset = 0;
1374 info->var.transp.length = 0;
1377 info->var.red.offset = 10;
1378 info->var.green.offset = 5;
1379 info->var.blue.offset = 0;
1380 info->var.red.length = 5;
1381 info->var.green.length = 5;
1382 info->var.blue.length = 5;
1383 info->var.transp.offset = 15;
1384 info->var.transp.length = 1;
1387 info->var.red.offset = 11;
1388 info->var.green.offset = 5;
1389 info->var.blue.offset = 0;
1390 info->var.red.length = 5;
1391 info->var.green.length = 6;
1392 info->var.blue.length = 5;
1393 info->var.transp.offset = 0;
1396 info->var.red.offset = 16;
1397 info->var.green.offset = 8;
1398 info->var.blue.offset = 0;
1399 info->var.red.length = 8;
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 = 16;
1407 info->var.green.offset = 8;
1408 info->var.blue.offset = 0;
1409 info->var.red.length = 8;
1410 info->var.green.length = 8;
1411 info->var.blue.length = 8;
1412 info->var.transp.offset = 24;
1413 info->var.transp.length = 8;
1419 info->var.xres = fb_width;
1420 info->var.yres = fb_height;
1422 EXPORT_SYMBOL(drm_fb_helper_fill_var);
1424 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1428 struct drm_connector *connector;
1432 for (i = 0; i < fb_helper->connector_count; i++) {
1433 connector = fb_helper->connector_info[i]->connector;
1434 count += connector->funcs->fill_modes(connector, maxX, maxY);
1440 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1442 struct drm_display_mode *mode;
1444 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1445 if (mode->hdisplay > width ||
1446 mode->vdisplay > height)
1448 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1453 EXPORT_SYMBOL(drm_has_preferred_mode);
1455 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1457 return fb_connector->connector->cmdline_mode.specified;
1460 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1461 int width, int height)
1463 struct drm_cmdline_mode *cmdline_mode;
1464 struct drm_display_mode *mode;
1465 bool prefer_non_interlace;
1467 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1468 if (cmdline_mode->specified == false)
1471 /* attempt to find a matching mode in the list of modes
1472 * we have gotten so far, if not add a CVT mode that conforms
1474 if (cmdline_mode->rb || cmdline_mode->margins)
1477 prefer_non_interlace = !cmdline_mode->interlace;
1479 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1480 /* check width/height */
1481 if (mode->hdisplay != cmdline_mode->xres ||
1482 mode->vdisplay != cmdline_mode->yres)
1485 if (cmdline_mode->refresh_specified) {
1486 if (mode->vrefresh != cmdline_mode->refresh)
1490 if (cmdline_mode->interlace) {
1491 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1493 } else if (prefer_non_interlace) {
1494 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1500 if (prefer_non_interlace) {
1501 prefer_non_interlace = false;
1506 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1508 list_add(&mode->head, &fb_helper_conn->connector->modes);
1511 EXPORT_SYMBOL(drm_pick_cmdline_mode);
1513 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1518 enable = connector->status == connector_status_connected;
1520 enable = connector->status != connector_status_disconnected;
1525 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1528 bool any_enabled = false;
1529 struct drm_connector *connector;
1532 for (i = 0; i < fb_helper->connector_count; i++) {
1533 connector = fb_helper->connector_info[i]->connector;
1534 enabled[i] = drm_connector_enabled(connector, true);
1535 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1536 enabled[i] ? "yes" : "no");
1537 any_enabled |= enabled[i];
1543 for (i = 0; i < fb_helper->connector_count; i++) {
1544 connector = fb_helper->connector_info[i]->connector;
1545 enabled[i] = drm_connector_enabled(connector, false);
1549 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1550 struct drm_display_mode **modes,
1551 struct drm_fb_offset *offsets,
1552 bool *enabled, int width, int height)
1555 bool can_clone = false;
1556 struct drm_fb_helper_connector *fb_helper_conn;
1557 struct drm_display_mode *dmt_mode, *mode;
1559 /* only contemplate cloning in the single crtc case */
1560 if (fb_helper->crtc_count > 1)
1564 for (i = 0; i < fb_helper->connector_count; i++) {
1569 /* only contemplate cloning if more than one connector is enabled */
1573 /* check the command line or if nothing common pick 1024x768 */
1575 for (i = 0; i < fb_helper->connector_count; i++) {
1578 fb_helper_conn = fb_helper->connector_info[i];
1579 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1584 for (j = 0; j < i; j++) {
1587 if (!drm_mode_equal(modes[j], modes[i]))
1593 DRM_DEBUG_KMS("can clone using command line\n");
1597 /* try and find a 1024x768 mode on each connector */
1599 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1601 for (i = 0; i < fb_helper->connector_count; i++) {
1606 fb_helper_conn = fb_helper->connector_info[i];
1607 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1608 if (drm_mode_equal(mode, dmt_mode))
1616 DRM_DEBUG_KMS("can clone using 1024x768\n");
1619 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1623 static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
1624 struct drm_display_mode **modes,
1625 struct drm_fb_offset *offsets,
1627 int h_idx, int v_idx)
1629 struct drm_fb_helper_connector *fb_helper_conn;
1631 int hoffset = 0, voffset = 0;
1633 for (i = 0; i < fb_helper->connector_count; i++) {
1634 fb_helper_conn = fb_helper->connector_info[i];
1635 if (!fb_helper_conn->connector->has_tile)
1638 if (!modes[i] && (h_idx || v_idx)) {
1639 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
1640 fb_helper_conn->connector->base.id);
1643 if (fb_helper_conn->connector->tile_h_loc < h_idx)
1644 hoffset += modes[i]->hdisplay;
1646 if (fb_helper_conn->connector->tile_v_loc < v_idx)
1647 voffset += modes[i]->vdisplay;
1649 offsets[idx].x = hoffset;
1650 offsets[idx].y = voffset;
1651 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
1655 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1656 struct drm_display_mode **modes,
1657 struct drm_fb_offset *offsets,
1658 bool *enabled, int width, int height)
1660 struct drm_fb_helper_connector *fb_helper_conn;
1662 uint64_t conn_configured = 0, mask;
1664 mask = (1 << fb_helper->connector_count) - 1;
1666 for (i = 0; i < fb_helper->connector_count; i++) {
1667 fb_helper_conn = fb_helper->connector_info[i];
1669 if (conn_configured & (1 << i))
1672 if (enabled[i] == false) {
1673 conn_configured |= (1 << i);
1677 /* first pass over all the untiled connectors */
1678 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
1681 if (tile_pass == 1) {
1682 if (fb_helper_conn->connector->tile_h_loc != 0 ||
1683 fb_helper_conn->connector->tile_v_loc != 0)
1687 if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 &&
1688 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
1689 /* if this tile_pass doesn't cover any of the tiles - keep going */
1692 /* find the tile offsets for this pass - need
1693 to find all tiles left and above */
1694 drm_get_tile_offsets(fb_helper, modes, offsets,
1695 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
1697 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1698 fb_helper_conn->connector->base.id);
1700 /* got for command line mode first */
1701 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1703 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
1704 fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0);
1705 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1707 /* No preferred modes, pick one off the list */
1708 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1709 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1712 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1714 conn_configured |= (1 << i);
1717 if ((conn_configured & mask) != mask) {
1724 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1725 struct drm_fb_helper_crtc **best_crtcs,
1726 struct drm_display_mode **modes,
1727 int n, int width, int height)
1730 struct drm_device *dev = fb_helper->dev;
1731 struct drm_connector *connector;
1732 const struct drm_connector_helper_funcs *connector_funcs;
1733 struct drm_encoder *encoder;
1734 int my_score, best_score, score;
1735 struct drm_fb_helper_crtc **crtcs, *crtc;
1736 struct drm_fb_helper_connector *fb_helper_conn;
1738 if (n == fb_helper->connector_count)
1741 fb_helper_conn = fb_helper->connector_info[n];
1742 connector = fb_helper_conn->connector;
1744 best_crtcs[n] = NULL;
1745 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1746 if (modes[n] == NULL)
1749 crtcs = kzalloc(dev->mode_config.num_connector *
1750 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1755 if (connector->status == connector_status_connected)
1757 if (drm_has_cmdline_mode(fb_helper_conn))
1759 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1762 connector_funcs = connector->helper_private;
1763 encoder = connector_funcs->best_encoder(connector);
1767 /* select a crtc for this connector and then attempt to configure
1768 remaining connectors */
1769 for (c = 0; c < fb_helper->crtc_count; c++) {
1770 crtc = &fb_helper->crtc_info[c];
1772 if ((encoder->possible_crtcs & (1 << c)) == 0)
1775 for (o = 0; o < n; o++)
1776 if (best_crtcs[o] == crtc)
1780 /* ignore cloning unless only a single crtc */
1781 if (fb_helper->crtc_count > 1)
1784 if (!drm_mode_equal(modes[o], modes[n]))
1789 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1790 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1792 if (score > best_score) {
1794 memcpy(best_crtcs, crtcs,
1795 dev->mode_config.num_connector *
1796 sizeof(struct drm_fb_helper_crtc *));
1804 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1806 struct drm_device *dev = fb_helper->dev;
1807 struct drm_fb_helper_crtc **crtcs;
1808 struct drm_display_mode **modes;
1809 struct drm_fb_offset *offsets;
1810 struct drm_mode_set *modeset;
1815 DRM_DEBUG_KMS("\n");
1817 width = dev->mode_config.max_width;
1818 height = dev->mode_config.max_height;
1820 crtcs = kcalloc(dev->mode_config.num_connector,
1821 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1822 modes = kcalloc(dev->mode_config.num_connector,
1823 sizeof(struct drm_display_mode *), GFP_KERNEL);
1824 offsets = kcalloc(dev->mode_config.num_connector,
1825 sizeof(struct drm_fb_offset), GFP_KERNEL);
1826 enabled = kcalloc(dev->mode_config.num_connector,
1827 sizeof(bool), GFP_KERNEL);
1828 if (!crtcs || !modes || !enabled || !offsets) {
1829 DRM_ERROR("Memory allocation failed\n");
1834 drm_enable_connectors(fb_helper, enabled);
1836 if (!(fb_helper->funcs->initial_config &&
1837 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
1839 enabled, width, height))) {
1840 memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
1841 memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
1842 memset(offsets, 0, dev->mode_config.num_connector*sizeof(offsets[0]));
1844 if (!drm_target_cloned(fb_helper, modes, offsets,
1845 enabled, width, height) &&
1846 !drm_target_preferred(fb_helper, modes, offsets,
1847 enabled, width, height))
1848 DRM_ERROR("Unable to find initial modes\n");
1850 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1853 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1856 /* need to set the modesets up here for use later */
1857 /* fill out the connector<->crtc mappings into the modesets */
1858 for (i = 0; i < fb_helper->crtc_count; i++) {
1859 modeset = &fb_helper->crtc_info[i].mode_set;
1860 modeset->num_connectors = 0;
1864 for (i = 0; i < fb_helper->connector_count; i++) {
1865 struct drm_display_mode *mode = modes[i];
1866 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1867 struct drm_fb_offset *offset = &offsets[i];
1868 modeset = &fb_crtc->mode_set;
1870 if (mode && fb_crtc) {
1871 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
1872 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
1873 fb_crtc->desired_mode = mode;
1874 fb_crtc->x = offset->x;
1875 fb_crtc->y = offset->y;
1877 drm_mode_destroy(dev, modeset->mode);
1878 modeset->mode = drm_mode_duplicate(dev,
1879 fb_crtc->desired_mode);
1880 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1881 modeset->fb = fb_helper->fb;
1882 modeset->x = offset->x;
1883 modeset->y = offset->y;
1887 /* Clear out any old modes if there are no more connected outputs. */
1888 for (i = 0; i < fb_helper->crtc_count; i++) {
1889 modeset = &fb_helper->crtc_info[i].mode_set;
1890 if (modeset->num_connectors == 0) {
1891 BUG_ON(modeset->fb);
1893 drm_mode_destroy(dev, modeset->mode);
1894 modeset->mode = NULL;
1905 * drm_fb_helper_initial_config - setup a sane initial connector configuration
1906 * @fb_helper: fb_helper device struct
1907 * @bpp_sel: bpp value to use for the framebuffer configuration
1909 * Scans the CRTCs and connectors and tries to put together an initial setup.
1910 * At the moment, this is a cloned configuration across all heads with
1911 * a new framebuffer object as the backing store.
1913 * Note that this also registers the fbdev and so allows userspace to call into
1914 * the driver through the fbdev interfaces.
1916 * This function will call down into the ->fb_probe callback to let
1917 * the driver allocate and initialize the fbdev info structure and the drm
1918 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1919 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1920 * values for the fbdev info structure.
1923 * Zero if everything went ok, nonzero otherwise.
1925 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1927 struct drm_device *dev = fb_helper->dev;
1930 mutex_lock(&dev->mode_config.mutex);
1931 count = drm_fb_helper_probe_connector_modes(fb_helper,
1932 dev->mode_config.max_width,
1933 dev->mode_config.max_height);
1934 mutex_unlock(&dev->mode_config.mutex);
1936 * we shouldn't end up with no modes here.
1939 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
1941 drm_setup_crtcs(fb_helper);
1943 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1945 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1948 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1949 * probing all the outputs attached to the fb
1950 * @fb_helper: the drm_fb_helper
1952 * Scan the connectors attached to the fb_helper and try to put together a
1953 * setup after *notification of a change in output configuration.
1955 * Called at runtime, takes the mode config locks to be able to check/change the
1956 * modeset configuration. Must be run from process context (which usually means
1957 * either the output polling work or a work item launched from the driver's
1958 * hotplug interrupt).
1960 * Note that drivers may call this even before calling
1961 * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
1962 * for a race-free fbcon setup and will make sure that the fbdev emulation will
1963 * not miss any hotplug events.
1966 * 0 on success and a non-zero error code otherwise.
1968 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1970 struct drm_device *dev = fb_helper->dev;
1971 u32 max_width, max_height;
1973 mutex_lock(&fb_helper->dev->mode_config.mutex);
1974 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
1975 fb_helper->delayed_hotplug = true;
1976 mutex_unlock(&fb_helper->dev->mode_config.mutex);
1979 DRM_DEBUG_KMS("\n");
1981 max_width = fb_helper->fb->width;
1982 max_height = fb_helper->fb->height;
1984 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
1985 mutex_unlock(&fb_helper->dev->mode_config.mutex);
1987 drm_modeset_lock_all(dev);
1988 drm_setup_crtcs(fb_helper);
1989 drm_modeset_unlock_all(dev);
1990 drm_fb_helper_set_par(fb_helper->fbdev);
1994 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
1996 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
1997 * but the module doesn't depend on any fb console symbols. At least
1998 * attempt to load fbcon to avoid leaving the system without a usable console.
2000 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
2001 static int __init drm_fb_helper_modinit(void)
2003 const char *name = "fbcon";
2004 struct module *fbcon;
2006 mutex_lock(&module_mutex);
2007 fbcon = find_module(name);
2008 mutex_unlock(&module_mutex);
2011 request_module_nowait(name);
2015 module_init(drm_fb_helper_modinit);