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/console.h>
33 #include <linux/dma-buf.h>
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/slab.h>
37 #include <linux/sysrq.h>
38 #include <linux/vmalloc.h>
40 #include <drm/drm_atomic.h>
41 #include <drm/drm_atomic_helper.h>
42 #include <drm/drm_crtc.h>
43 #include <drm/drm_crtc_helper.h>
44 #include <drm/drm_drv.h>
45 #include <drm/drm_fb_helper.h>
46 #include <drm/drm_fourcc.h>
47 #include <drm/drm_print.h>
48 #include <drm/drm_vblank.h>
50 #include "drm_crtc_helper_internal.h"
51 #include "drm_crtc_internal.h"
52 #include "drm_internal.h"
54 static bool drm_fbdev_emulation = true;
55 module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
56 MODULE_PARM_DESC(fbdev_emulation,
57 "Enable legacy fbdev emulation [default=true]");
59 static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC;
60 module_param(drm_fbdev_overalloc, int, 0444);
61 MODULE_PARM_DESC(drm_fbdev_overalloc,
62 "Overallocation of the fbdev buffer (%) [default="
63 __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]");
66 * In order to keep user-space compatibility, we want in certain use-cases
67 * to keep leaking the fbdev physical address to the user-space program
68 * handling the fbdev buffer.
69 * This is a bad habit essentially kept into closed source opengl driver
70 * that should really be moved into open-source upstream projects instead
71 * of using legacy physical addresses in user space to communicate with
72 * other out-of-tree kernel modules.
74 * This module_param *should* be removed as soon as possible and be
75 * considered as a broken and legacy behaviour from a modern fbdev device.
77 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
78 static bool drm_leak_fbdev_smem = false;
79 module_param_unsafe(drm_leak_fbdev_smem, bool, 0600);
80 MODULE_PARM_DESC(drm_leak_fbdev_smem,
81 "Allow unsafe leaking fbdev physical smem address [default=false]");
84 static LIST_HEAD(kernel_fb_helper_list);
85 static DEFINE_MUTEX(kernel_fb_helper_lock);
90 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
91 * mode setting driver. They can be used mostly independently from the crtc
92 * helper functions used by many drivers to implement the kernel mode setting
95 * Drivers that support a dumb buffer with a virtual address and mmap support,
96 * should try out the generic fbdev emulation using drm_fbdev_generic_setup().
98 * Setup fbdev emulation by calling drm_fb_helper_fbdev_setup() and tear it
99 * down by calling drm_fb_helper_fbdev_teardown().
101 * Drivers that need to handle connector hotplugging (e.g. dp mst) can't use
102 * the setup helper and will need to do the whole four-step setup process with
103 * drm_fb_helper_prepare(), drm_fb_helper_init(),
104 * drm_fb_helper_single_add_all_connectors(), enable hotplugging and
105 * drm_fb_helper_initial_config() to avoid a possible race window.
107 * At runtime drivers should restore the fbdev console by using
108 * drm_fb_helper_lastclose() as their &drm_driver.lastclose callback.
109 * They should also notify the fb helper code from updates to the output
110 * configuration by using drm_fb_helper_output_poll_changed() as their
111 * &drm_mode_config_funcs.output_poll_changed callback.
113 * For suspend/resume consider using drm_mode_config_helper_suspend() and
114 * drm_mode_config_helper_resume() which takes care of fbdev as well.
116 * All other functions exported by the fb helper library can be used to
117 * implement the fbdev driver interface by the driver.
119 * It is possible, though perhaps somewhat tricky, to implement race-free
120 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
121 * helper must be called first to initialize the minimum required to make
122 * hotplug detection work. Drivers also need to make sure to properly set up
123 * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
124 * it is safe to enable interrupts and start processing hotplug events. At the
125 * same time, drivers should initialize all modeset objects such as CRTCs,
126 * encoders and connectors. To finish up the fbdev helper initialization, the
127 * drm_fb_helper_init() function is called. To probe for all attached displays
128 * and set up an initial configuration using the detected hardware, drivers
129 * should call drm_fb_helper_single_add_all_connectors() followed by
130 * drm_fb_helper_initial_config().
132 * If &drm_framebuffer_funcs.dirty is set, the
133 * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
134 * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
135 * away. This worker then calls the dirty() function ensuring that it will
136 * always run in process context since the fb_*() function could be running in
137 * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
138 * callback it will also schedule dirty_work with the damage collected from the
139 * mmap page writes. Drivers can use drm_fb_helper_defio_init() to setup
140 * deferred I/O (coupled with drm_fb_helper_fbdev_teardown()).
143 #define drm_fb_helper_for_each_connector(fbh, i__) \
144 for (({ lockdep_assert_held(&(fbh)->lock); }), \
145 i__ = 0; i__ < (fbh)->connector_count; i__++)
147 static int __drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
148 struct drm_connector *connector)
150 struct drm_fb_helper_connector *fb_conn;
151 struct drm_fb_helper_connector **temp;
154 if (!drm_fbdev_emulation)
157 lockdep_assert_held(&fb_helper->lock);
159 count = fb_helper->connector_count + 1;
161 if (count > fb_helper->connector_info_alloc_count) {
162 size_t size = count * sizeof(fb_conn);
164 temp = krealloc(fb_helper->connector_info, size, GFP_KERNEL);
168 fb_helper->connector_info_alloc_count = count;
169 fb_helper->connector_info = temp;
172 fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL);
176 drm_connector_get(connector);
177 fb_conn->connector = connector;
178 fb_helper->connector_info[fb_helper->connector_count++] = fb_conn;
183 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
184 struct drm_connector *connector)
191 mutex_lock(&fb_helper->lock);
192 err = __drm_fb_helper_add_one_connector(fb_helper, connector);
193 mutex_unlock(&fb_helper->lock);
197 EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
200 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
202 * @fb_helper: fbdev initialized with drm_fb_helper_init, can be NULL
204 * This functions adds all the available connectors for use with the given
205 * fb_helper. This is a separate step to allow drivers to freely assign
206 * connectors to the fbdev, e.g. if some are reserved for special purposes or
207 * not adequate to be used for the fbcon.
209 * This function is protected against concurrent connector hotadds/removals
210 * using drm_fb_helper_add_one_connector() and
211 * drm_fb_helper_remove_one_connector().
213 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
215 struct drm_device *dev;
216 struct drm_connector *connector;
217 struct drm_connector_list_iter conn_iter;
220 if (!drm_fbdev_emulation || !fb_helper)
223 dev = fb_helper->dev;
225 mutex_lock(&fb_helper->lock);
226 drm_connector_list_iter_begin(dev, &conn_iter);
227 drm_for_each_connector_iter(connector, &conn_iter) {
228 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
231 ret = __drm_fb_helper_add_one_connector(fb_helper, connector);
238 drm_fb_helper_for_each_connector(fb_helper, i) {
239 struct drm_fb_helper_connector *fb_helper_connector =
240 fb_helper->connector_info[i];
242 drm_connector_put(fb_helper_connector->connector);
244 kfree(fb_helper_connector);
245 fb_helper->connector_info[i] = NULL;
247 fb_helper->connector_count = 0;
249 drm_connector_list_iter_end(&conn_iter);
250 mutex_unlock(&fb_helper->lock);
254 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
256 static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
257 struct drm_connector *connector)
259 struct drm_fb_helper_connector *fb_helper_connector;
262 if (!drm_fbdev_emulation)
265 lockdep_assert_held(&fb_helper->lock);
267 drm_fb_helper_for_each_connector(fb_helper, i) {
268 if (fb_helper->connector_info[i]->connector == connector)
272 if (i == fb_helper->connector_count)
274 fb_helper_connector = fb_helper->connector_info[i];
275 drm_connector_put(fb_helper_connector->connector);
277 for (j = i + 1; j < fb_helper->connector_count; j++)
278 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
280 fb_helper->connector_count--;
281 kfree(fb_helper_connector);
286 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
287 struct drm_connector *connector)
294 mutex_lock(&fb_helper->lock);
295 err = __drm_fb_helper_remove_one_connector(fb_helper, connector);
296 mutex_unlock(&fb_helper->lock);
300 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
302 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
304 uint16_t *r_base, *g_base, *b_base;
306 if (crtc->funcs->gamma_set == NULL)
309 r_base = crtc->gamma_store;
310 g_base = r_base + crtc->gamma_size;
311 b_base = g_base + crtc->gamma_size;
313 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
314 crtc->gamma_size, NULL);
318 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
319 * @info: fbdev registered by the helper
321 int drm_fb_helper_debug_enter(struct fb_info *info)
323 struct drm_fb_helper *helper = info->par;
324 const struct drm_crtc_helper_funcs *funcs;
327 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
328 for (i = 0; i < helper->crtc_count; i++) {
329 struct drm_mode_set *mode_set =
330 &helper->crtc_info[i].mode_set;
332 if (!mode_set->crtc->enabled)
335 funcs = mode_set->crtc->helper_private;
336 if (funcs->mode_set_base_atomic == NULL)
339 if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
342 funcs->mode_set_base_atomic(mode_set->crtc,
346 ENTER_ATOMIC_MODE_SET);
352 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
355 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
356 * @info: fbdev registered by the helper
358 int drm_fb_helper_debug_leave(struct fb_info *info)
360 struct drm_fb_helper *helper = info->par;
361 struct drm_crtc *crtc;
362 const struct drm_crtc_helper_funcs *funcs;
363 struct drm_framebuffer *fb;
366 for (i = 0; i < helper->crtc_count; i++) {
367 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
369 crtc = mode_set->crtc;
370 if (drm_drv_uses_atomic_modeset(crtc->dev))
373 funcs = crtc->helper_private;
374 fb = crtc->primary->fb;
380 DRM_ERROR("no fb to restore??\n");
384 if (funcs->mode_set_base_atomic == NULL)
387 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
388 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
389 crtc->y, LEAVE_ATOMIC_MODE_SET);
394 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
396 /* Check if the plane can hw rotate to match panel orientation */
397 static bool drm_fb_helper_panel_rotation(struct drm_mode_set *modeset,
398 unsigned int *rotation)
400 struct drm_connector *connector = modeset->connectors[0];
401 struct drm_plane *plane = modeset->crtc->primary;
405 if (!modeset->num_connectors)
408 switch (connector->display_info.panel_orientation) {
409 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
410 *rotation = DRM_MODE_ROTATE_180;
412 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
413 *rotation = DRM_MODE_ROTATE_90;
415 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
416 *rotation = DRM_MODE_ROTATE_270;
419 *rotation = DRM_MODE_ROTATE_0;
423 * TODO: support 90 / 270 degree hardware rotation,
424 * depending on the hardware this may require the framebuffer
425 * to be in a specific tiling format.
427 if (*rotation != DRM_MODE_ROTATE_180 || !plane->rotation_property)
430 for (i = 0; i < plane->rotation_property->num_values; i++)
431 valid_mask |= (1ULL << plane->rotation_property->values[i]);
433 if (!(*rotation & valid_mask))
439 static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper, bool active)
441 struct drm_device *dev = fb_helper->dev;
442 struct drm_plane_state *plane_state;
443 struct drm_plane *plane;
444 struct drm_atomic_state *state;
446 struct drm_modeset_acquire_ctx ctx;
448 drm_modeset_acquire_init(&ctx, 0);
450 state = drm_atomic_state_alloc(dev);
456 state->acquire_ctx = &ctx;
458 drm_for_each_plane(plane, dev) {
459 plane_state = drm_atomic_get_plane_state(state, plane);
460 if (IS_ERR(plane_state)) {
461 ret = PTR_ERR(plane_state);
465 plane_state->rotation = DRM_MODE_ROTATE_0;
467 /* disable non-primary: */
468 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
471 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
476 for (i = 0; i < fb_helper->crtc_count; i++) {
477 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
478 struct drm_plane *primary = mode_set->crtc->primary;
479 unsigned int rotation;
481 if (drm_fb_helper_panel_rotation(mode_set, &rotation)) {
482 /* Cannot fail as we've already gotten the plane state above */
483 plane_state = drm_atomic_get_new_plane_state(state, primary);
484 plane_state->rotation = rotation;
487 ret = __drm_atomic_helper_set_config(mode_set, state);
492 * __drm_atomic_helper_set_config() sets active when a
493 * mode is set, unconditionally clear it if we force DPMS off
496 struct drm_crtc *crtc = mode_set->crtc;
497 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
499 crtc_state->active = false;
503 ret = drm_atomic_commit(state);
509 drm_atomic_state_put(state);
511 drm_modeset_drop_locks(&ctx);
512 drm_modeset_acquire_fini(&ctx);
517 drm_atomic_state_clear(state);
518 drm_modeset_backoff(&ctx);
523 static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper)
525 struct drm_device *dev = fb_helper->dev;
526 struct drm_plane *plane;
529 drm_modeset_lock_all(fb_helper->dev);
530 drm_for_each_plane(plane, dev) {
531 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
532 drm_plane_force_disable(plane);
534 if (plane->rotation_property)
535 drm_mode_plane_set_obj_prop(plane,
536 plane->rotation_property,
540 for (i = 0; i < fb_helper->crtc_count; i++) {
541 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
542 struct drm_crtc *crtc = mode_set->crtc;
544 if (crtc->funcs->cursor_set2) {
545 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
548 } else if (crtc->funcs->cursor_set) {
549 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
554 ret = drm_mode_set_config_internal(mode_set);
559 drm_modeset_unlock_all(fb_helper->dev);
564 static int restore_fbdev_mode_force(struct drm_fb_helper *fb_helper)
566 struct drm_device *dev = fb_helper->dev;
568 if (drm_drv_uses_atomic_modeset(dev))
569 return restore_fbdev_mode_atomic(fb_helper, true);
571 return restore_fbdev_mode_legacy(fb_helper);
574 static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
576 struct drm_device *dev = fb_helper->dev;
579 if (!drm_master_internal_acquire(dev))
582 ret = restore_fbdev_mode_force(fb_helper);
584 drm_master_internal_release(dev);
590 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
591 * @fb_helper: driver-allocated fbdev helper, can be NULL
593 * This should be called from driver's drm &drm_driver.lastclose callback
594 * when implementing an fbcon on top of kms using this helper. This ensures that
595 * the user isn't greeted with a black screen when e.g. X dies.
598 * Zero if everything went ok, negative error code otherwise.
600 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
605 if (!drm_fbdev_emulation || !fb_helper)
608 if (READ_ONCE(fb_helper->deferred_setup))
611 mutex_lock(&fb_helper->lock);
614 * We should bail out here if there is a master by dropping _force.
615 * Currently these igt tests fail if we do that:
616 * - kms_fbcon_fbt@psr
617 * - kms_fbcon_fbt@psr-suspend
619 * So first these tests need to be fixed so they drop master or don't
622 ret = restore_fbdev_mode_force(fb_helper);
624 do_delayed = fb_helper->delayed_hotplug;
626 fb_helper->delayed_hotplug = false;
627 mutex_unlock(&fb_helper->lock);
630 drm_fb_helper_hotplug_event(fb_helper);
634 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
636 #ifdef CONFIG_MAGIC_SYSRQ
638 * restore fbcon display for all kms driver's using this helper, used for sysrq
639 * and panic handling.
641 static bool drm_fb_helper_force_kernel_mode(void)
643 bool ret, error = false;
644 struct drm_fb_helper *helper;
646 if (list_empty(&kernel_fb_helper_list))
649 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
650 struct drm_device *dev = helper->dev;
652 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
655 mutex_lock(&helper->lock);
656 ret = restore_fbdev_mode_force(helper);
659 mutex_unlock(&helper->lock);
664 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
668 ret = drm_fb_helper_force_kernel_mode();
670 DRM_ERROR("Failed to restore crtc configuration\n");
672 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
674 static void drm_fb_helper_sysrq(int dummy1)
676 schedule_work(&drm_fb_helper_restore_work);
679 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
680 .handler = drm_fb_helper_sysrq,
681 .help_msg = "force-fb(V)",
682 .action_msg = "Restore framebuffer console",
685 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
688 static void dpms_legacy(struct drm_fb_helper *fb_helper, int dpms_mode)
690 struct drm_device *dev = fb_helper->dev;
691 struct drm_connector *connector;
692 struct drm_mode_set *modeset;
695 drm_modeset_lock_all(dev);
696 for (i = 0; i < fb_helper->crtc_count; i++) {
697 modeset = &fb_helper->crtc_info[i].mode_set;
699 if (!modeset->crtc->enabled)
702 for (j = 0; j < modeset->num_connectors; j++) {
703 connector = modeset->connectors[j];
704 connector->funcs->dpms(connector, dpms_mode);
705 drm_object_property_set_value(&connector->base,
706 dev->mode_config.dpms_property, dpms_mode);
709 drm_modeset_unlock_all(dev);
712 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
714 struct drm_fb_helper *fb_helper = info->par;
715 struct drm_device *dev = fb_helper->dev;
718 * For each CRTC in this fb, turn the connectors on/off.
720 mutex_lock(&fb_helper->lock);
721 if (!drm_master_internal_acquire(dev))
724 if (drm_drv_uses_atomic_modeset(dev))
725 restore_fbdev_mode_atomic(fb_helper, dpms_mode == DRM_MODE_DPMS_ON);
727 dpms_legacy(fb_helper, dpms_mode);
729 drm_master_internal_release(dev);
731 mutex_unlock(&fb_helper->lock);
735 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
736 * @blank: desired blanking state
737 * @info: fbdev registered by the helper
739 int drm_fb_helper_blank(int blank, struct fb_info *info)
741 if (oops_in_progress)
745 /* Display: On; HSync: On, VSync: On */
746 case FB_BLANK_UNBLANK:
747 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
749 /* Display: Off; HSync: On, VSync: On */
750 case FB_BLANK_NORMAL:
751 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
753 /* Display: Off; HSync: Off, VSync: On */
754 case FB_BLANK_HSYNC_SUSPEND:
755 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
757 /* Display: Off; HSync: On, VSync: Off */
758 case FB_BLANK_VSYNC_SUSPEND:
759 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
761 /* Display: Off; HSync: Off, VSync: Off */
762 case FB_BLANK_POWERDOWN:
763 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
768 EXPORT_SYMBOL(drm_fb_helper_blank);
770 static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper,
771 struct drm_mode_set *modeset)
775 for (i = 0; i < modeset->num_connectors; i++) {
776 drm_connector_put(modeset->connectors[i]);
777 modeset->connectors[i] = NULL;
779 modeset->num_connectors = 0;
781 drm_mode_destroy(helper->dev, modeset->mode);
782 modeset->mode = NULL;
784 /* FIXME should hold a ref? */
788 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
792 for (i = 0; i < helper->connector_count; i++) {
793 drm_connector_put(helper->connector_info[i]->connector);
794 kfree(helper->connector_info[i]);
796 kfree(helper->connector_info);
798 for (i = 0; i < helper->crtc_count; i++) {
799 struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set;
801 drm_fb_helper_modeset_release(helper, modeset);
802 kfree(modeset->connectors);
804 kfree(helper->crtc_info);
807 static void drm_fb_helper_resume_worker(struct work_struct *work)
809 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
813 fb_set_suspend(helper->fbdev, 0);
817 static void drm_fb_helper_dirty_blit_real(struct drm_fb_helper *fb_helper,
818 struct drm_clip_rect *clip)
820 struct drm_framebuffer *fb = fb_helper->fb;
821 unsigned int cpp = fb->format->cpp[0];
822 size_t offset = clip->y1 * fb->pitches[0] + clip->x1 * cpp;
823 void *src = fb_helper->fbdev->screen_buffer + offset;
824 void *dst = fb_helper->buffer->vaddr + offset;
825 size_t len = (clip->x2 - clip->x1) * cpp;
828 for (y = clip->y1; y < clip->y2; y++) {
829 memcpy(dst, src, len);
830 src += fb->pitches[0];
831 dst += fb->pitches[0];
835 static void drm_fb_helper_dirty_work(struct work_struct *work)
837 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
839 struct drm_clip_rect *clip = &helper->dirty_clip;
840 struct drm_clip_rect clip_copy;
843 spin_lock_irqsave(&helper->dirty_lock, flags);
845 clip->x1 = clip->y1 = ~0;
846 clip->x2 = clip->y2 = 0;
847 spin_unlock_irqrestore(&helper->dirty_lock, flags);
849 /* call dirty callback only when it has been really touched */
850 if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2) {
851 /* Generic fbdev uses a shadow buffer */
853 drm_fb_helper_dirty_blit_real(helper, &clip_copy);
854 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
859 * drm_fb_helper_prepare - setup a drm_fb_helper structure
861 * @helper: driver-allocated fbdev helper structure to set up
862 * @funcs: pointer to structure of functions associate with this helper
864 * Sets up the bare minimum to make the framebuffer helper usable. This is
865 * useful to implement race-free initialization of the polling helpers.
867 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
868 const struct drm_fb_helper_funcs *funcs)
870 INIT_LIST_HEAD(&helper->kernel_fb_list);
871 spin_lock_init(&helper->dirty_lock);
872 INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
873 INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
874 helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
875 mutex_init(&helper->lock);
876 helper->funcs = funcs;
879 EXPORT_SYMBOL(drm_fb_helper_prepare);
882 * drm_fb_helper_init - initialize a &struct drm_fb_helper
884 * @fb_helper: driver-allocated fbdev helper structure to initialize
885 * @max_conn_count: max connector count
887 * This allocates the structures for the fbdev helper with the given limits.
888 * Note that this won't yet touch the hardware (through the driver interfaces)
889 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
890 * to allow driver writes more control over the exact init sequence.
892 * Drivers must call drm_fb_helper_prepare() before calling this function.
895 * Zero if everything went ok, nonzero otherwise.
897 int drm_fb_helper_init(struct drm_device *dev,
898 struct drm_fb_helper *fb_helper,
901 struct drm_crtc *crtc;
902 struct drm_mode_config *config = &dev->mode_config;
905 if (!drm_fbdev_emulation) {
906 dev->fb_helper = fb_helper;
913 fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
914 if (!fb_helper->crtc_info)
917 fb_helper->crtc_count = config->num_crtc;
918 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
919 if (!fb_helper->connector_info) {
920 kfree(fb_helper->crtc_info);
923 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
924 fb_helper->connector_count = 0;
926 for (i = 0; i < fb_helper->crtc_count; i++) {
927 fb_helper->crtc_info[i].mode_set.connectors =
928 kcalloc(max_conn_count,
929 sizeof(struct drm_connector *),
932 if (!fb_helper->crtc_info[i].mode_set.connectors)
934 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
938 drm_for_each_crtc(crtc, dev) {
939 fb_helper->crtc_info[i].mode_set.crtc = crtc;
943 dev->fb_helper = fb_helper;
947 drm_fb_helper_crtc_free(fb_helper);
950 EXPORT_SYMBOL(drm_fb_helper_init);
953 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
954 * @fb_helper: driver-allocated fbdev helper
956 * A helper to alloc fb_info and the members cmap and apertures. Called
957 * by the driver within the fb_probe fb_helper callback function. Drivers do not
958 * need to release the allocated fb_info structure themselves, this is
959 * automatically done when calling drm_fb_helper_fini().
962 * fb_info pointer if things went okay, pointer containing error code
965 struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
967 struct device *dev = fb_helper->dev->dev;
968 struct fb_info *info;
971 info = framebuffer_alloc(0, dev);
973 return ERR_PTR(-ENOMEM);
975 ret = fb_alloc_cmap(&info->cmap, 256, 0);
979 info->apertures = alloc_apertures(1);
980 if (!info->apertures) {
985 fb_helper->fbdev = info;
986 info->skip_vt_switch = true;
991 fb_dealloc_cmap(&info->cmap);
993 framebuffer_release(info);
996 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
999 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
1000 * @fb_helper: driver-allocated fbdev helper, can be NULL
1002 * A wrapper around unregister_framebuffer, to release the fb_info
1003 * framebuffer device. This must be called before releasing all resources for
1004 * @fb_helper by calling drm_fb_helper_fini().
1006 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
1008 if (fb_helper && fb_helper->fbdev)
1009 unregister_framebuffer(fb_helper->fbdev);
1011 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
1014 * drm_fb_helper_fini - finialize a &struct drm_fb_helper
1015 * @fb_helper: driver-allocated fbdev helper, can be NULL
1017 * This cleans up all remaining resources associated with @fb_helper. Must be
1018 * called after drm_fb_helper_unlink_fbi() was called.
1020 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
1022 struct fb_info *info;
1027 fb_helper->dev->fb_helper = NULL;
1029 if (!drm_fbdev_emulation)
1032 cancel_work_sync(&fb_helper->resume_work);
1033 cancel_work_sync(&fb_helper->dirty_work);
1035 info = fb_helper->fbdev;
1038 fb_dealloc_cmap(&info->cmap);
1039 framebuffer_release(info);
1041 fb_helper->fbdev = NULL;
1043 mutex_lock(&kernel_fb_helper_lock);
1044 if (!list_empty(&fb_helper->kernel_fb_list)) {
1045 list_del(&fb_helper->kernel_fb_list);
1046 if (list_empty(&kernel_fb_helper_list))
1047 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1049 mutex_unlock(&kernel_fb_helper_lock);
1051 mutex_destroy(&fb_helper->lock);
1052 drm_fb_helper_crtc_free(fb_helper);
1055 EXPORT_SYMBOL(drm_fb_helper_fini);
1058 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
1059 * @fb_helper: driver-allocated fbdev helper, can be NULL
1061 * A wrapper around unlink_framebuffer implemented by fbdev core
1063 void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
1065 if (fb_helper && fb_helper->fbdev)
1066 unlink_framebuffer(fb_helper->fbdev);
1068 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
1070 static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
1071 u32 width, u32 height)
1073 struct drm_fb_helper *helper = info->par;
1074 struct drm_clip_rect *clip = &helper->dirty_clip;
1075 unsigned long flags;
1077 if (!helper->fb->funcs->dirty)
1080 spin_lock_irqsave(&helper->dirty_lock, flags);
1081 clip->x1 = min_t(u32, clip->x1, x);
1082 clip->y1 = min_t(u32, clip->y1, y);
1083 clip->x2 = max_t(u32, clip->x2, x + width);
1084 clip->y2 = max_t(u32, clip->y2, y + height);
1085 spin_unlock_irqrestore(&helper->dirty_lock, flags);
1087 schedule_work(&helper->dirty_work);
1091 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
1092 * @info: fb_info struct pointer
1093 * @pagelist: list of dirty mmap framebuffer pages
1095 * This function is used as the &fb_deferred_io.deferred_io
1096 * callback function for flushing the fbdev mmap writes.
1098 void drm_fb_helper_deferred_io(struct fb_info *info,
1099 struct list_head *pagelist)
1101 unsigned long start, end, min, max;
1107 list_for_each_entry(page, pagelist, lru) {
1108 start = page->index << PAGE_SHIFT;
1109 end = start + PAGE_SIZE - 1;
1110 min = min(min, start);
1111 max = max(max, end);
1115 y1 = min / info->fix.line_length;
1116 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
1118 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
1121 EXPORT_SYMBOL(drm_fb_helper_deferred_io);
1124 * drm_fb_helper_defio_init - fbdev deferred I/O initialization
1125 * @fb_helper: driver-allocated fbdev helper
1127 * This function allocates &fb_deferred_io, sets callback to
1128 * drm_fb_helper_deferred_io(), delay to 50ms and calls fb_deferred_io_init().
1129 * It should be called from the &drm_fb_helper_funcs->fb_probe callback.
1130 * drm_fb_helper_fbdev_teardown() cleans up deferred I/O.
1132 * NOTE: A copy of &fb_ops is made and assigned to &info->fbops. This is done
1133 * because fb_deferred_io_cleanup() clears &fbops->fb_mmap and would thereby
1134 * affect other instances of that &fb_ops.
1137 * 0 on success or a negative error code on failure.
1139 int drm_fb_helper_defio_init(struct drm_fb_helper *fb_helper)
1141 struct fb_info *info = fb_helper->fbdev;
1142 struct fb_deferred_io *fbdefio;
1143 struct fb_ops *fbops;
1145 fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL);
1146 fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
1147 if (!fbdefio || !fbops) {
1153 info->fbdefio = fbdefio;
1154 fbdefio->delay = msecs_to_jiffies(50);
1155 fbdefio->deferred_io = drm_fb_helper_deferred_io;
1157 *fbops = *info->fbops;
1158 info->fbops = fbops;
1160 fb_deferred_io_init(info);
1164 EXPORT_SYMBOL(drm_fb_helper_defio_init);
1167 * drm_fb_helper_sys_read - wrapper around fb_sys_read
1168 * @info: fb_info struct pointer
1169 * @buf: userspace buffer to read from framebuffer memory
1170 * @count: number of bytes to read from framebuffer memory
1171 * @ppos: read offset within framebuffer memory
1173 * A wrapper around fb_sys_read implemented by fbdev core
1175 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
1176 size_t count, loff_t *ppos)
1178 return fb_sys_read(info, buf, count, ppos);
1180 EXPORT_SYMBOL(drm_fb_helper_sys_read);
1183 * drm_fb_helper_sys_write - wrapper around fb_sys_write
1184 * @info: fb_info struct pointer
1185 * @buf: userspace buffer to write to framebuffer memory
1186 * @count: number of bytes to write to framebuffer memory
1187 * @ppos: write offset within framebuffer memory
1189 * A wrapper around fb_sys_write implemented by fbdev core
1191 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
1192 size_t count, loff_t *ppos)
1196 ret = fb_sys_write(info, buf, count, ppos);
1198 drm_fb_helper_dirty(info, 0, 0, info->var.xres,
1203 EXPORT_SYMBOL(drm_fb_helper_sys_write);
1206 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
1207 * @info: fbdev registered by the helper
1208 * @rect: info about rectangle to fill
1210 * A wrapper around sys_fillrect implemented by fbdev core
1212 void drm_fb_helper_sys_fillrect(struct fb_info *info,
1213 const struct fb_fillrect *rect)
1215 sys_fillrect(info, rect);
1216 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1217 rect->width, rect->height);
1219 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
1222 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1223 * @info: fbdev registered by the helper
1224 * @area: info about area to copy
1226 * A wrapper around sys_copyarea implemented by fbdev core
1228 void drm_fb_helper_sys_copyarea(struct fb_info *info,
1229 const struct fb_copyarea *area)
1231 sys_copyarea(info, area);
1232 drm_fb_helper_dirty(info, area->dx, area->dy,
1233 area->width, area->height);
1235 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
1238 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1239 * @info: fbdev registered by the helper
1240 * @image: info about image to blit
1242 * A wrapper around sys_imageblit implemented by fbdev core
1244 void drm_fb_helper_sys_imageblit(struct fb_info *info,
1245 const struct fb_image *image)
1247 sys_imageblit(info, image);
1248 drm_fb_helper_dirty(info, image->dx, image->dy,
1249 image->width, image->height);
1251 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
1254 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1255 * @info: fbdev registered by the helper
1256 * @rect: info about rectangle to fill
1258 * A wrapper around cfb_fillrect implemented by fbdev core
1260 void drm_fb_helper_cfb_fillrect(struct fb_info *info,
1261 const struct fb_fillrect *rect)
1263 cfb_fillrect(info, rect);
1264 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1265 rect->width, rect->height);
1267 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
1270 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1271 * @info: fbdev registered by the helper
1272 * @area: info about area to copy
1274 * A wrapper around cfb_copyarea implemented by fbdev core
1276 void drm_fb_helper_cfb_copyarea(struct fb_info *info,
1277 const struct fb_copyarea *area)
1279 cfb_copyarea(info, area);
1280 drm_fb_helper_dirty(info, area->dx, area->dy,
1281 area->width, area->height);
1283 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
1286 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1287 * @info: fbdev registered by the helper
1288 * @image: info about image to blit
1290 * A wrapper around cfb_imageblit implemented by fbdev core
1292 void drm_fb_helper_cfb_imageblit(struct fb_info *info,
1293 const struct fb_image *image)
1295 cfb_imageblit(info, image);
1296 drm_fb_helper_dirty(info, image->dx, image->dy,
1297 image->width, image->height);
1299 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
1302 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
1303 * @fb_helper: driver-allocated fbdev helper, can be NULL
1304 * @suspend: whether to suspend or resume
1306 * A wrapper around fb_set_suspend implemented by fbdev core.
1307 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
1310 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
1312 if (fb_helper && fb_helper->fbdev)
1313 fb_set_suspend(fb_helper->fbdev, suspend);
1315 EXPORT_SYMBOL(drm_fb_helper_set_suspend);
1318 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
1319 * takes the console lock
1320 * @fb_helper: driver-allocated fbdev helper, can be NULL
1321 * @suspend: whether to suspend or resume
1323 * A wrapper around fb_set_suspend() that takes the console lock. If the lock
1324 * isn't available on resume, a worker is tasked with waiting for the lock
1325 * to become available. The console lock can be pretty contented on resume
1326 * due to all the printk activity.
1328 * This function can be called multiple times with the same state since
1329 * &fb_info.state is checked to see if fbdev is running or not before locking.
1331 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
1333 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
1336 if (!fb_helper || !fb_helper->fbdev)
1339 /* make sure there's no pending/ongoing resume */
1340 flush_work(&fb_helper->resume_work);
1343 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
1349 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
1352 if (!console_trylock()) {
1353 schedule_work(&fb_helper->resume_work);
1358 fb_set_suspend(fb_helper->fbdev, suspend);
1361 EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
1363 static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info)
1365 u32 *palette = (u32 *)info->pseudo_palette;
1368 if (cmap->start + cmap->len > 16)
1371 for (i = 0; i < cmap->len; ++i) {
1372 u16 red = cmap->red[i];
1373 u16 green = cmap->green[i];
1374 u16 blue = cmap->blue[i];
1377 red >>= 16 - info->var.red.length;
1378 green >>= 16 - info->var.green.length;
1379 blue >>= 16 - info->var.blue.length;
1380 value = (red << info->var.red.offset) |
1381 (green << info->var.green.offset) |
1382 (blue << info->var.blue.offset);
1383 if (info->var.transp.length > 0) {
1384 u32 mask = (1 << info->var.transp.length) - 1;
1386 mask <<= info->var.transp.offset;
1389 palette[cmap->start + i] = value;
1395 static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info)
1397 struct drm_fb_helper *fb_helper = info->par;
1398 struct drm_crtc *crtc;
1402 drm_modeset_lock_all(fb_helper->dev);
1403 for (i = 0; i < fb_helper->crtc_count; i++) {
1404 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1405 if (!crtc->funcs->gamma_set || !crtc->gamma_size)
1408 if (cmap->start + cmap->len > crtc->gamma_size)
1411 r = crtc->gamma_store;
1412 g = r + crtc->gamma_size;
1413 b = g + crtc->gamma_size;
1415 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1416 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1417 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1419 ret = crtc->funcs->gamma_set(crtc, r, g, b,
1420 crtc->gamma_size, NULL);
1424 drm_modeset_unlock_all(fb_helper->dev);
1429 static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc,
1430 struct fb_cmap *cmap)
1432 struct drm_device *dev = crtc->dev;
1433 struct drm_property_blob *gamma_lut;
1434 struct drm_color_lut *lut;
1435 int size = crtc->gamma_size;
1438 if (!size || cmap->start + cmap->len > size)
1439 return ERR_PTR(-EINVAL);
1441 gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL);
1442 if (IS_ERR(gamma_lut))
1445 lut = gamma_lut->data;
1446 if (cmap->start || cmap->len != size) {
1447 u16 *r = crtc->gamma_store;
1448 u16 *g = r + crtc->gamma_size;
1449 u16 *b = g + crtc->gamma_size;
1451 for (i = 0; i < cmap->start; i++) {
1453 lut[i].green = g[i];
1456 for (i = cmap->start + cmap->len; i < size; i++) {
1458 lut[i].green = g[i];
1463 for (i = 0; i < cmap->len; i++) {
1464 lut[cmap->start + i].red = cmap->red[i];
1465 lut[cmap->start + i].green = cmap->green[i];
1466 lut[cmap->start + i].blue = cmap->blue[i];
1472 static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info)
1474 struct drm_fb_helper *fb_helper = info->par;
1475 struct drm_device *dev = fb_helper->dev;
1476 struct drm_property_blob *gamma_lut = NULL;
1477 struct drm_modeset_acquire_ctx ctx;
1478 struct drm_crtc_state *crtc_state;
1479 struct drm_atomic_state *state;
1480 struct drm_crtc *crtc;
1485 drm_modeset_acquire_init(&ctx, 0);
1487 state = drm_atomic_state_alloc(dev);
1493 state->acquire_ctx = &ctx;
1495 for (i = 0; i < fb_helper->crtc_count; i++) {
1496 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1499 gamma_lut = setcmap_new_gamma_lut(crtc, cmap);
1500 if (IS_ERR(gamma_lut)) {
1501 ret = PTR_ERR(gamma_lut);
1506 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1507 if (IS_ERR(crtc_state)) {
1508 ret = PTR_ERR(crtc_state);
1512 replaced = drm_property_replace_blob(&crtc_state->degamma_lut,
1514 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
1515 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
1517 crtc_state->color_mgmt_changed |= replaced;
1520 ret = drm_atomic_commit(state);
1524 for (i = 0; i < fb_helper->crtc_count; i++) {
1525 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1527 r = crtc->gamma_store;
1528 g = r + crtc->gamma_size;
1529 b = g + crtc->gamma_size;
1531 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1532 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1533 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1537 if (ret == -EDEADLK)
1540 drm_property_blob_put(gamma_lut);
1541 drm_atomic_state_put(state);
1543 drm_modeset_drop_locks(&ctx);
1544 drm_modeset_acquire_fini(&ctx);
1549 drm_atomic_state_clear(state);
1550 drm_modeset_backoff(&ctx);
1555 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
1556 * @cmap: cmap to set
1557 * @info: fbdev registered by the helper
1559 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1561 struct drm_fb_helper *fb_helper = info->par;
1562 struct drm_device *dev = fb_helper->dev;
1565 if (oops_in_progress)
1568 mutex_lock(&fb_helper->lock);
1570 if (!drm_master_internal_acquire(dev)) {
1575 if (info->fix.visual == FB_VISUAL_TRUECOLOR)
1576 ret = setcmap_pseudo_palette(cmap, info);
1577 else if (drm_drv_uses_atomic_modeset(fb_helper->dev))
1578 ret = setcmap_atomic(cmap, info);
1580 ret = setcmap_legacy(cmap, info);
1582 drm_master_internal_release(dev);
1584 mutex_unlock(&fb_helper->lock);
1588 EXPORT_SYMBOL(drm_fb_helper_setcmap);
1591 * drm_fb_helper_ioctl - legacy ioctl implementation
1592 * @info: fbdev registered by the helper
1593 * @cmd: ioctl command
1594 * @arg: ioctl argument
1596 * A helper to implement the standard fbdev ioctl. Only
1597 * FBIO_WAITFORVSYNC is implemented for now.
1599 int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
1602 struct drm_fb_helper *fb_helper = info->par;
1603 struct drm_device *dev = fb_helper->dev;
1604 struct drm_mode_set *mode_set;
1605 struct drm_crtc *crtc;
1608 mutex_lock(&fb_helper->lock);
1609 if (!drm_master_internal_acquire(dev)) {
1615 case FBIO_WAITFORVSYNC:
1617 * Only consider the first CRTC.
1619 * This ioctl is supposed to take the CRTC number as
1620 * an argument, but in fbdev times, what that number
1621 * was supposed to be was quite unclear, different
1622 * drivers were passing that argument differently
1623 * (some by reference, some by value), and most of the
1624 * userspace applications were just hardcoding 0 as an
1627 * The first CRTC should be the integrated panel on
1628 * most drivers, so this is the best choice we can
1629 * make. If we're not smart enough here, one should
1630 * just consider switch the userspace to KMS.
1632 mode_set = &fb_helper->crtc_info[0].mode_set;
1633 crtc = mode_set->crtc;
1636 * Only wait for a vblank event if the CRTC is
1637 * enabled, otherwise just don't do anythintg,
1638 * not even report an error.
1640 ret = drm_crtc_vblank_get(crtc);
1642 drm_crtc_wait_one_vblank(crtc);
1643 drm_crtc_vblank_put(crtc);
1652 drm_master_internal_release(dev);
1654 mutex_unlock(&fb_helper->lock);
1657 EXPORT_SYMBOL(drm_fb_helper_ioctl);
1659 static bool drm_fb_pixel_format_equal(const struct fb_var_screeninfo *var_1,
1660 const struct fb_var_screeninfo *var_2)
1662 return var_1->bits_per_pixel == var_2->bits_per_pixel &&
1663 var_1->grayscale == var_2->grayscale &&
1664 var_1->red.offset == var_2->red.offset &&
1665 var_1->red.length == var_2->red.length &&
1666 var_1->red.msb_right == var_2->red.msb_right &&
1667 var_1->green.offset == var_2->green.offset &&
1668 var_1->green.length == var_2->green.length &&
1669 var_1->green.msb_right == var_2->green.msb_right &&
1670 var_1->blue.offset == var_2->blue.offset &&
1671 var_1->blue.length == var_2->blue.length &&
1672 var_1->blue.msb_right == var_2->blue.msb_right &&
1673 var_1->transp.offset == var_2->transp.offset &&
1674 var_1->transp.length == var_2->transp.length &&
1675 var_1->transp.msb_right == var_2->transp.msb_right;
1678 static void drm_fb_helper_fill_pixel_fmt(struct fb_var_screeninfo *var,
1683 var->red.offset = 0;
1684 var->green.offset = 0;
1685 var->blue.offset = 0;
1686 var->red.length = 8; /* 8bit DAC */
1687 var->green.length = 8;
1688 var->blue.length = 8;
1689 var->transp.offset = 0;
1690 var->transp.length = 0;
1693 var->red.offset = 10;
1694 var->green.offset = 5;
1695 var->blue.offset = 0;
1696 var->red.length = 5;
1697 var->green.length = 5;
1698 var->blue.length = 5;
1699 var->transp.offset = 15;
1700 var->transp.length = 1;
1703 var->red.offset = 11;
1704 var->green.offset = 5;
1705 var->blue.offset = 0;
1706 var->red.length = 5;
1707 var->green.length = 6;
1708 var->blue.length = 5;
1709 var->transp.offset = 0;
1712 var->red.offset = 16;
1713 var->green.offset = 8;
1714 var->blue.offset = 0;
1715 var->red.length = 8;
1716 var->green.length = 8;
1717 var->blue.length = 8;
1718 var->transp.offset = 0;
1719 var->transp.length = 0;
1722 var->red.offset = 16;
1723 var->green.offset = 8;
1724 var->blue.offset = 0;
1725 var->red.length = 8;
1726 var->green.length = 8;
1727 var->blue.length = 8;
1728 var->transp.offset = 24;
1729 var->transp.length = 8;
1737 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
1738 * @var: screeninfo to check
1739 * @info: fbdev registered by the helper
1741 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1742 struct fb_info *info)
1744 struct drm_fb_helper *fb_helper = info->par;
1745 struct drm_framebuffer *fb = fb_helper->fb;
1747 if (in_dbg_master())
1750 if (var->pixclock != 0) {
1751 DRM_DEBUG("fbdev emulation doesn't support changing the pixel clock, value of pixclock is ignored\n");
1755 if ((drm_format_info_block_width(fb->format, 0) > 1) ||
1756 (drm_format_info_block_height(fb->format, 0) > 1))
1760 * Changes struct fb_var_screeninfo are currently not pushed back
1761 * to KMS, hence fail if different settings are requested.
1763 if (var->bits_per_pixel != fb->format->cpp[0] * 8 ||
1764 var->xres > fb->width || var->yres > fb->height ||
1765 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1766 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
1767 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1768 var->xres, var->yres, var->bits_per_pixel,
1769 var->xres_virtual, var->yres_virtual,
1770 fb->width, fb->height, fb->format->cpp[0] * 8);
1775 * Workaround for SDL 1.2, which is known to be setting all pixel format
1776 * fields values to zero in some cases. We treat this situation as a
1777 * kind of "use some reasonable autodetected values".
1779 if (!var->red.offset && !var->green.offset &&
1780 !var->blue.offset && !var->transp.offset &&
1781 !var->red.length && !var->green.length &&
1782 !var->blue.length && !var->transp.length &&
1783 !var->red.msb_right && !var->green.msb_right &&
1784 !var->blue.msb_right && !var->transp.msb_right) {
1785 drm_fb_helper_fill_pixel_fmt(var, fb->format->depth);
1789 * drm fbdev emulation doesn't support changing the pixel format at all,
1790 * so reject all pixel format changing requests.
1792 if (!drm_fb_pixel_format_equal(var, &info->var)) {
1793 DRM_DEBUG("fbdev emulation doesn't support changing the pixel format\n");
1799 EXPORT_SYMBOL(drm_fb_helper_check_var);
1802 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
1803 * @info: fbdev registered by the helper
1805 * This will let fbcon do the mode init and is called at initialization time by
1806 * the fbdev core when registering the driver, and later on through the hotplug
1809 int drm_fb_helper_set_par(struct fb_info *info)
1811 struct drm_fb_helper *fb_helper = info->par;
1812 struct fb_var_screeninfo *var = &info->var;
1814 if (oops_in_progress)
1817 if (var->pixclock != 0) {
1818 DRM_ERROR("PIXEL CLOCK SET\n");
1822 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
1826 EXPORT_SYMBOL(drm_fb_helper_set_par);
1828 static void pan_set(struct drm_fb_helper *fb_helper, int x, int y)
1832 for (i = 0; i < fb_helper->crtc_count; i++) {
1833 struct drm_mode_set *mode_set;
1835 mode_set = &fb_helper->crtc_info[i].mode_set;
1842 static int pan_display_atomic(struct fb_var_screeninfo *var,
1843 struct fb_info *info)
1845 struct drm_fb_helper *fb_helper = info->par;
1848 pan_set(fb_helper, var->xoffset, var->yoffset);
1850 ret = restore_fbdev_mode_atomic(fb_helper, true);
1852 info->var.xoffset = var->xoffset;
1853 info->var.yoffset = var->yoffset;
1855 pan_set(fb_helper, info->var.xoffset, info->var.yoffset);
1860 static int pan_display_legacy(struct fb_var_screeninfo *var,
1861 struct fb_info *info)
1863 struct drm_fb_helper *fb_helper = info->par;
1864 struct drm_mode_set *modeset;
1868 drm_modeset_lock_all(fb_helper->dev);
1869 for (i = 0; i < fb_helper->crtc_count; i++) {
1870 modeset = &fb_helper->crtc_info[i].mode_set;
1872 modeset->x = var->xoffset;
1873 modeset->y = var->yoffset;
1875 if (modeset->num_connectors) {
1876 ret = drm_mode_set_config_internal(modeset);
1878 info->var.xoffset = var->xoffset;
1879 info->var.yoffset = var->yoffset;
1883 drm_modeset_unlock_all(fb_helper->dev);
1889 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1890 * @var: updated screen information
1891 * @info: fbdev registered by the helper
1893 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1894 struct fb_info *info)
1896 struct drm_fb_helper *fb_helper = info->par;
1897 struct drm_device *dev = fb_helper->dev;
1900 if (oops_in_progress)
1903 mutex_lock(&fb_helper->lock);
1904 if (!drm_master_internal_acquire(dev)) {
1909 if (drm_drv_uses_atomic_modeset(dev))
1910 ret = pan_display_atomic(var, info);
1912 ret = pan_display_legacy(var, info);
1914 drm_master_internal_release(dev);
1916 mutex_unlock(&fb_helper->lock);
1920 EXPORT_SYMBOL(drm_fb_helper_pan_display);
1923 * Allocates the backing storage and sets up the fbdev info structure through
1924 * the ->fb_probe callback.
1926 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1932 struct drm_fb_helper_surface_size sizes;
1935 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1936 sizes.surface_depth = 24;
1937 sizes.surface_bpp = 32;
1938 sizes.fb_width = (u32)-1;
1939 sizes.fb_height = (u32)-1;
1942 * If driver picks 8 or 16 by default use that for both depth/bpp
1945 if (preferred_bpp != sizes.surface_bpp)
1946 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
1948 drm_fb_helper_for_each_connector(fb_helper, i) {
1949 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
1950 struct drm_cmdline_mode *cmdline_mode;
1952 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1954 if (cmdline_mode->bpp_specified) {
1955 switch (cmdline_mode->bpp) {
1957 sizes.surface_depth = sizes.surface_bpp = 8;
1960 sizes.surface_depth = 15;
1961 sizes.surface_bpp = 16;
1964 sizes.surface_depth = sizes.surface_bpp = 16;
1967 sizes.surface_depth = sizes.surface_bpp = 24;
1970 sizes.surface_depth = 24;
1971 sizes.surface_bpp = 32;
1979 * If we run into a situation where, for example, the primary plane
1980 * supports RGBA5551 (16 bpp, depth 15) but not RGB565 (16 bpp, depth
1981 * 16) we need to scale down the depth of the sizes we request.
1983 for (i = 0; i < fb_helper->crtc_count; i++) {
1984 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
1985 struct drm_crtc *crtc = mode_set->crtc;
1986 struct drm_plane *plane = crtc->primary;
1989 DRM_DEBUG("test CRTC %d primary plane\n", i);
1991 for (j = 0; j < plane->format_count; j++) {
1992 const struct drm_format_info *fmt;
1994 fmt = drm_format_info(plane->format_types[j]);
1997 * Do not consider YUV or other complicated formats
1998 * for framebuffers. This means only legacy formats
1999 * are supported (fmt->depth is a legacy field) but
2000 * the framebuffer emulation can only deal with such
2001 * formats, specifically RGB/BGA formats.
2003 if (fmt->depth == 0)
2006 /* We found a perfect fit, great */
2007 if (fmt->depth == sizes.surface_depth) {
2008 best_depth = fmt->depth;
2012 /* Skip depths above what we're looking for */
2013 if (fmt->depth > sizes.surface_depth)
2016 /* Best depth found so far */
2017 if (fmt->depth > best_depth)
2018 best_depth = fmt->depth;
2021 if (sizes.surface_depth != best_depth && best_depth) {
2022 DRM_INFO("requested bpp %d, scaled depth down to %d",
2023 sizes.surface_bpp, best_depth);
2024 sizes.surface_depth = best_depth;
2027 /* first up get a count of crtcs now in use and new min/maxes width/heights */
2029 for (i = 0; i < fb_helper->crtc_count; i++) {
2030 struct drm_display_mode *desired_mode;
2031 struct drm_mode_set *mode_set;
2033 /* in case of tile group, are we the last tile vert or horiz?
2034 * If no tile group you are always the last one both vertically
2037 bool lastv = true, lasth = true;
2039 mode_set = &fb_helper->crtc_info[i].mode_set;
2040 desired_mode = mode_set->mode;
2050 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
2051 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
2053 for (j = 0; j < mode_set->num_connectors; j++) {
2054 struct drm_connector *connector = mode_set->connectors[j];
2056 if (connector->has_tile) {
2057 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
2058 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
2059 /* cloning to multiple tiles is just crazy-talk, so: */
2065 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
2067 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
2070 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
2071 DRM_INFO("Cannot find any crtc or sizes\n");
2073 /* First time: disable all crtc's.. */
2074 if (!fb_helper->deferred_setup)
2075 restore_fbdev_mode(fb_helper);
2079 /* Handle our overallocation */
2080 sizes.surface_height *= drm_fbdev_overalloc;
2081 sizes.surface_height /= 100;
2083 /* push down into drivers */
2084 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
2088 strcpy(fb_helper->fb->comm, "[fbcon]");
2092 static void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
2095 info->fix.type = FB_TYPE_PACKED_PIXELS;
2096 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
2097 FB_VISUAL_TRUECOLOR;
2098 info->fix.mmio_start = 0;
2099 info->fix.mmio_len = 0;
2100 info->fix.type_aux = 0;
2101 info->fix.xpanstep = 1; /* doing it in hw */
2102 info->fix.ypanstep = 1; /* doing it in hw */
2103 info->fix.ywrapstep = 0;
2104 info->fix.accel = FB_ACCEL_NONE;
2106 info->fix.line_length = pitch;
2109 static void drm_fb_helper_fill_var(struct fb_info *info,
2110 struct drm_fb_helper *fb_helper,
2111 uint32_t fb_width, uint32_t fb_height)
2113 struct drm_framebuffer *fb = fb_helper->fb;
2115 WARN_ON((drm_format_info_block_width(fb->format, 0) > 1) ||
2116 (drm_format_info_block_height(fb->format, 0) > 1));
2117 info->pseudo_palette = fb_helper->pseudo_palette;
2118 info->var.xres_virtual = fb->width;
2119 info->var.yres_virtual = fb->height;
2120 info->var.bits_per_pixel = fb->format->cpp[0] * 8;
2121 info->var.accel_flags = FB_ACCELF_TEXT;
2122 info->var.xoffset = 0;
2123 info->var.yoffset = 0;
2124 info->var.activate = FB_ACTIVATE_NOW;
2126 drm_fb_helper_fill_pixel_fmt(&info->var, fb->format->depth);
2128 info->var.xres = fb_width;
2129 info->var.yres = fb_height;
2133 * drm_fb_helper_fill_info - initializes fbdev information
2134 * @info: fbdev instance to set up
2135 * @fb_helper: fb helper instance to use as template
2136 * @sizes: describes fbdev size and scanout surface size
2138 * Sets up the variable and fixed fbdev metainformation from the given fb helper
2139 * instance and the drm framebuffer allocated in &drm_fb_helper.fb.
2141 * Drivers should call this (or their equivalent setup code) from their
2142 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
2143 * backing storage framebuffer.
2145 void drm_fb_helper_fill_info(struct fb_info *info,
2146 struct drm_fb_helper *fb_helper,
2147 struct drm_fb_helper_surface_size *sizes)
2149 struct drm_framebuffer *fb = fb_helper->fb;
2151 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
2152 drm_fb_helper_fill_var(info, fb_helper,
2153 sizes->fb_width, sizes->fb_height);
2155 info->par = fb_helper;
2156 snprintf(info->fix.id, sizeof(info->fix.id), "%sdrmfb",
2157 fb_helper->dev->driver->name);
2160 EXPORT_SYMBOL(drm_fb_helper_fill_info);
2162 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
2166 struct drm_connector *connector;
2169 drm_fb_helper_for_each_connector(fb_helper, i) {
2170 connector = fb_helper->connector_info[i]->connector;
2171 count += connector->funcs->fill_modes(connector, maxX, maxY);
2177 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
2179 struct drm_display_mode *mode;
2181 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
2182 if (mode->hdisplay > width ||
2183 mode->vdisplay > height)
2185 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2190 EXPORT_SYMBOL(drm_has_preferred_mode);
2192 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
2194 return fb_connector->connector->cmdline_mode.specified;
2197 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn)
2199 struct drm_cmdline_mode *cmdline_mode;
2200 struct drm_display_mode *mode;
2201 bool prefer_non_interlace;
2203 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
2204 if (cmdline_mode->specified == false)
2207 /* attempt to find a matching mode in the list of modes
2208 * we have gotten so far, if not add a CVT mode that conforms
2210 if (cmdline_mode->rb || cmdline_mode->margins)
2213 prefer_non_interlace = !cmdline_mode->interlace;
2215 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
2216 /* check width/height */
2217 if (mode->hdisplay != cmdline_mode->xres ||
2218 mode->vdisplay != cmdline_mode->yres)
2221 if (cmdline_mode->refresh_specified) {
2222 if (mode->vrefresh != cmdline_mode->refresh)
2226 if (cmdline_mode->interlace) {
2227 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
2229 } else if (prefer_non_interlace) {
2230 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2236 if (prefer_non_interlace) {
2237 prefer_non_interlace = false;
2242 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
2244 list_add(&mode->head, &fb_helper_conn->connector->modes);
2247 EXPORT_SYMBOL(drm_pick_cmdline_mode);
2249 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
2253 if (connector->display_info.non_desktop)
2257 enable = connector->status == connector_status_connected;
2259 enable = connector->status != connector_status_disconnected;
2264 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
2267 bool any_enabled = false;
2268 struct drm_connector *connector;
2271 drm_fb_helper_for_each_connector(fb_helper, i) {
2272 connector = fb_helper->connector_info[i]->connector;
2273 enabled[i] = drm_connector_enabled(connector, true);
2274 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
2275 connector->display_info.non_desktop ? "non desktop" : enabled[i] ? "yes" : "no");
2277 any_enabled |= enabled[i];
2283 drm_fb_helper_for_each_connector(fb_helper, i) {
2284 connector = fb_helper->connector_info[i]->connector;
2285 enabled[i] = drm_connector_enabled(connector, false);
2289 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
2290 struct drm_display_mode **modes,
2291 struct drm_fb_offset *offsets,
2292 bool *enabled, int width, int height)
2295 bool can_clone = false;
2296 struct drm_fb_helper_connector *fb_helper_conn;
2297 struct drm_display_mode *dmt_mode, *mode;
2299 /* only contemplate cloning in the single crtc case */
2300 if (fb_helper->crtc_count > 1)
2304 drm_fb_helper_for_each_connector(fb_helper, i) {
2309 /* only contemplate cloning if more than one connector is enabled */
2313 /* check the command line or if nothing common pick 1024x768 */
2315 drm_fb_helper_for_each_connector(fb_helper, i) {
2318 fb_helper_conn = fb_helper->connector_info[i];
2319 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
2324 for (j = 0; j < i; j++) {
2327 if (!drm_mode_match(modes[j], modes[i],
2328 DRM_MODE_MATCH_TIMINGS |
2329 DRM_MODE_MATCH_CLOCK |
2330 DRM_MODE_MATCH_FLAGS |
2331 DRM_MODE_MATCH_3D_FLAGS))
2337 DRM_DEBUG_KMS("can clone using command line\n");
2341 /* try and find a 1024x768 mode on each connector */
2343 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
2345 drm_fb_helper_for_each_connector(fb_helper, i) {
2349 fb_helper_conn = fb_helper->connector_info[i];
2350 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
2351 if (drm_mode_match(mode, dmt_mode,
2352 DRM_MODE_MATCH_TIMINGS |
2353 DRM_MODE_MATCH_CLOCK |
2354 DRM_MODE_MATCH_FLAGS |
2355 DRM_MODE_MATCH_3D_FLAGS))
2363 DRM_DEBUG_KMS("can clone using 1024x768\n");
2366 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
2370 static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
2371 struct drm_display_mode **modes,
2372 struct drm_fb_offset *offsets,
2374 int h_idx, int v_idx)
2376 struct drm_fb_helper_connector *fb_helper_conn;
2378 int hoffset = 0, voffset = 0;
2380 drm_fb_helper_for_each_connector(fb_helper, i) {
2381 fb_helper_conn = fb_helper->connector_info[i];
2382 if (!fb_helper_conn->connector->has_tile)
2385 if (!modes[i] && (h_idx || v_idx)) {
2386 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
2387 fb_helper_conn->connector->base.id);
2390 if (fb_helper_conn->connector->tile_h_loc < h_idx)
2391 hoffset += modes[i]->hdisplay;
2393 if (fb_helper_conn->connector->tile_v_loc < v_idx)
2394 voffset += modes[i]->vdisplay;
2396 offsets[idx].x = hoffset;
2397 offsets[idx].y = voffset;
2398 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
2402 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
2403 struct drm_display_mode **modes,
2404 struct drm_fb_offset *offsets,
2405 bool *enabled, int width, int height)
2407 struct drm_fb_helper_connector *fb_helper_conn;
2408 const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
2409 u64 conn_configured = 0;
2414 drm_fb_helper_for_each_connector(fb_helper, i) {
2415 fb_helper_conn = fb_helper->connector_info[i];
2417 if (conn_configured & BIT_ULL(i))
2420 if (enabled[i] == false) {
2421 conn_configured |= BIT_ULL(i);
2425 /* first pass over all the untiled connectors */
2426 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
2429 if (tile_pass == 1) {
2430 if (fb_helper_conn->connector->tile_h_loc != 0 ||
2431 fb_helper_conn->connector->tile_v_loc != 0)
2435 if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 &&
2436 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
2437 /* if this tile_pass doesn't cover any of the tiles - keep going */
2441 * find the tile offsets for this pass - need to find
2442 * all tiles left and above
2444 drm_get_tile_offsets(fb_helper, modes, offsets,
2445 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
2447 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
2448 fb_helper_conn->connector->base.id);
2450 /* got for command line mode first */
2451 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
2453 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
2454 fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0);
2455 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
2457 /* No preferred modes, pick one off the list */
2458 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
2459 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
2462 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
2464 conn_configured |= BIT_ULL(i);
2467 if ((conn_configured & mask) != mask) {
2474 static bool connector_has_possible_crtc(struct drm_connector *connector,
2475 struct drm_crtc *crtc)
2477 struct drm_encoder *encoder;
2480 drm_connector_for_each_possible_encoder(connector, encoder, i) {
2481 if (encoder->possible_crtcs & drm_crtc_mask(crtc))
2488 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
2489 struct drm_fb_helper_crtc **best_crtcs,
2490 struct drm_display_mode **modes,
2491 int n, int width, int height)
2494 struct drm_connector *connector;
2495 int my_score, best_score, score;
2496 struct drm_fb_helper_crtc **crtcs, *crtc;
2497 struct drm_fb_helper_connector *fb_helper_conn;
2499 if (n == fb_helper->connector_count)
2502 fb_helper_conn = fb_helper->connector_info[n];
2503 connector = fb_helper_conn->connector;
2505 best_crtcs[n] = NULL;
2506 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
2507 if (modes[n] == NULL)
2510 crtcs = kcalloc(fb_helper->connector_count,
2511 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
2516 if (connector->status == connector_status_connected)
2518 if (drm_has_cmdline_mode(fb_helper_conn))
2520 if (drm_has_preferred_mode(fb_helper_conn, width, height))
2524 * select a crtc for this connector and then attempt to configure
2525 * remaining connectors
2527 for (c = 0; c < fb_helper->crtc_count; c++) {
2528 crtc = &fb_helper->crtc_info[c];
2530 if (!connector_has_possible_crtc(connector,
2531 crtc->mode_set.crtc))
2534 for (o = 0; o < n; o++)
2535 if (best_crtcs[o] == crtc)
2539 /* ignore cloning unless only a single crtc */
2540 if (fb_helper->crtc_count > 1)
2543 if (!drm_mode_equal(modes[o], modes[n]))
2548 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
2549 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
2551 if (score > best_score) {
2553 memcpy(best_crtcs, crtcs,
2554 fb_helper->connector_count *
2555 sizeof(struct drm_fb_helper_crtc *));
2563 static struct drm_fb_helper_crtc *
2564 drm_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
2568 for (i = 0; i < fb_helper->crtc_count; i++)
2569 if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
2570 return &fb_helper->crtc_info[i];
2575 /* Try to read the BIOS display configuration and use it for the initial config */
2576 static bool drm_fb_helper_firmware_config(struct drm_fb_helper *fb_helper,
2577 struct drm_fb_helper_crtc **crtcs,
2578 struct drm_display_mode **modes,
2579 struct drm_fb_offset *offsets,
2580 bool *enabled, int width, int height)
2582 struct drm_device *dev = fb_helper->dev;
2583 unsigned int count = min(fb_helper->connector_count, BITS_PER_LONG);
2584 unsigned long conn_configured, conn_seq, mask;
2587 bool fallback = true, ret = true;
2588 int num_connectors_enabled = 0;
2589 int num_connectors_detected = 0;
2590 struct drm_modeset_acquire_ctx ctx;
2592 if (!drm_drv_uses_atomic_modeset(dev))
2595 save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
2599 drm_modeset_acquire_init(&ctx, 0);
2601 while (drm_modeset_lock_all_ctx(dev, &ctx) != 0)
2602 drm_modeset_backoff(&ctx);
2604 memcpy(save_enabled, enabled, count);
2605 mask = GENMASK(count - 1, 0);
2606 conn_configured = 0;
2608 conn_seq = conn_configured;
2609 for (i = 0; i < count; i++) {
2610 struct drm_fb_helper_connector *fb_conn;
2611 struct drm_connector *connector;
2612 struct drm_encoder *encoder;
2613 struct drm_fb_helper_crtc *new_crtc;
2615 fb_conn = fb_helper->connector_info[i];
2616 connector = fb_conn->connector;
2618 if (conn_configured & BIT(i))
2621 if (conn_seq == 0 && !connector->has_tile)
2624 if (connector->status == connector_status_connected)
2625 num_connectors_detected++;
2628 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
2630 conn_configured |= BIT(i);
2634 if (connector->force == DRM_FORCE_OFF) {
2635 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
2641 encoder = connector->state->best_encoder;
2642 if (!encoder || WARN_ON(!connector->state->crtc)) {
2643 if (connector->force > DRM_FORCE_OFF)
2646 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
2649 conn_configured |= BIT(i);
2653 num_connectors_enabled++;
2655 new_crtc = drm_fb_helper_crtc(fb_helper, connector->state->crtc);
2658 * Make sure we're not trying to drive multiple connectors
2659 * with a single CRTC, since our cloning support may not
2662 for (j = 0; j < count; j++) {
2663 if (crtcs[j] == new_crtc) {
2664 DRM_DEBUG_KMS("fallback: cloned configuration\n");
2669 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
2672 /* go for command line mode first */
2673 modes[i] = drm_pick_cmdline_mode(fb_conn);
2675 /* try for preferred next */
2677 DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
2678 connector->name, connector->has_tile);
2679 modes[i] = drm_has_preferred_mode(fb_conn, width,
2683 /* No preferred mode marked by the EDID? Are there any modes? */
2684 if (!modes[i] && !list_empty(&connector->modes)) {
2685 DRM_DEBUG_KMS("using first mode listed on connector %s\n",
2687 modes[i] = list_first_entry(&connector->modes,
2688 struct drm_display_mode,
2692 /* last resort: use current mode */
2695 * IMPORTANT: We want to use the adjusted mode (i.e.
2696 * after the panel fitter upscaling) as the initial
2697 * config, not the input mode, which is what crtc->mode
2698 * usually contains. But since our current
2699 * code puts a mode derived from the post-pfit timings
2700 * into crtc->mode this works out correctly.
2702 * This is crtc->mode and not crtc->state->mode for the
2703 * fastboot check to work correctly.
2705 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
2707 modes[i] = &connector->state->crtc->mode;
2709 crtcs[i] = new_crtc;
2711 DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
2713 connector->state->crtc->base.id,
2714 connector->state->crtc->name,
2715 modes[i]->hdisplay, modes[i]->vdisplay,
2716 modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" : "");
2719 conn_configured |= BIT(i);
2722 if ((conn_configured & mask) != mask && conn_configured != conn_seq)
2726 * If the BIOS didn't enable everything it could, fall back to have the
2727 * same user experiencing of lighting up as much as possible like the
2728 * fbdev helper library.
2730 if (num_connectors_enabled != num_connectors_detected &&
2731 num_connectors_enabled < dev->mode_config.num_crtc) {
2732 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
2733 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
2734 num_connectors_detected);
2740 DRM_DEBUG_KMS("Not using firmware configuration\n");
2741 memcpy(enabled, save_enabled, count);
2745 drm_modeset_drop_locks(&ctx);
2746 drm_modeset_acquire_fini(&ctx);
2748 kfree(save_enabled);
2752 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
2753 u32 width, u32 height)
2755 struct drm_device *dev = fb_helper->dev;
2756 struct drm_fb_helper_crtc **crtcs;
2757 struct drm_display_mode **modes;
2758 struct drm_fb_offset *offsets;
2762 DRM_DEBUG_KMS("\n");
2763 /* prevent concurrent modification of connector_count by hotplug */
2764 lockdep_assert_held(&fb_helper->lock);
2766 crtcs = kcalloc(fb_helper->connector_count,
2767 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
2768 modes = kcalloc(fb_helper->connector_count,
2769 sizeof(struct drm_display_mode *), GFP_KERNEL);
2770 offsets = kcalloc(fb_helper->connector_count,
2771 sizeof(struct drm_fb_offset), GFP_KERNEL);
2772 enabled = kcalloc(fb_helper->connector_count,
2773 sizeof(bool), GFP_KERNEL);
2774 if (!crtcs || !modes || !enabled || !offsets) {
2775 DRM_ERROR("Memory allocation failed\n");
2779 mutex_lock(&fb_helper->dev->mode_config.mutex);
2780 if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
2781 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
2782 drm_enable_connectors(fb_helper, enabled);
2784 if (!drm_fb_helper_firmware_config(fb_helper, crtcs, modes, offsets,
2785 enabled, width, height)) {
2786 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
2787 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
2788 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
2790 if (!drm_target_cloned(fb_helper, modes, offsets,
2791 enabled, width, height) &&
2792 !drm_target_preferred(fb_helper, modes, offsets,
2793 enabled, width, height))
2794 DRM_ERROR("Unable to find initial modes\n");
2796 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2799 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
2801 mutex_unlock(&fb_helper->dev->mode_config.mutex);
2803 /* need to set the modesets up here for use later */
2804 /* fill out the connector<->crtc mappings into the modesets */
2805 for (i = 0; i < fb_helper->crtc_count; i++)
2806 drm_fb_helper_modeset_release(fb_helper,
2807 &fb_helper->crtc_info[i].mode_set);
2809 drm_fb_helper_for_each_connector(fb_helper, i) {
2810 struct drm_display_mode *mode = modes[i];
2811 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
2812 struct drm_fb_offset *offset = &offsets[i];
2814 if (mode && fb_crtc) {
2815 struct drm_mode_set *modeset = &fb_crtc->mode_set;
2816 struct drm_connector *connector =
2817 fb_helper->connector_info[i]->connector;
2819 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2820 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
2822 modeset->mode = drm_mode_duplicate(dev, mode);
2823 drm_connector_get(connector);
2824 modeset->connectors[modeset->num_connectors++] = connector;
2825 modeset->x = offset->x;
2826 modeset->y = offset->y;
2837 * This is a continuation of drm_setup_crtcs() that sets up anything related
2838 * to the framebuffer. During initialization, drm_setup_crtcs() is called before
2839 * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev).
2840 * So, any setup that touches those fields needs to be done here instead of in
2841 * drm_setup_crtcs().
2843 static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper)
2845 struct fb_info *info = fb_helper->fbdev;
2846 unsigned int rotation, sw_rotations = 0;
2849 for (i = 0; i < fb_helper->crtc_count; i++) {
2850 struct drm_mode_set *modeset = &fb_helper->crtc_info[i].mode_set;
2852 if (!modeset->num_connectors)
2855 modeset->fb = fb_helper->fb;
2857 if (drm_fb_helper_panel_rotation(modeset, &rotation))
2858 /* Rotating in hardware, fbcon should not rotate */
2859 sw_rotations |= DRM_MODE_ROTATE_0;
2861 sw_rotations |= rotation;
2864 mutex_lock(&fb_helper->dev->mode_config.mutex);
2865 drm_fb_helper_for_each_connector(fb_helper, i) {
2866 struct drm_connector *connector =
2867 fb_helper->connector_info[i]->connector;
2869 /* use first connected connector for the physical dimensions */
2870 if (connector->status == connector_status_connected) {
2871 info->var.width = connector->display_info.width_mm;
2872 info->var.height = connector->display_info.height_mm;
2876 mutex_unlock(&fb_helper->dev->mode_config.mutex);
2878 switch (sw_rotations) {
2879 case DRM_MODE_ROTATE_0:
2880 info->fbcon_rotate_hint = FB_ROTATE_UR;
2882 case DRM_MODE_ROTATE_90:
2883 info->fbcon_rotate_hint = FB_ROTATE_CCW;
2885 case DRM_MODE_ROTATE_180:
2886 info->fbcon_rotate_hint = FB_ROTATE_UD;
2888 case DRM_MODE_ROTATE_270:
2889 info->fbcon_rotate_hint = FB_ROTATE_CW;
2893 * Multiple bits are set / multiple rotations requested
2894 * fbcon cannot handle separate rotation settings per
2895 * output, so fallback to unrotated.
2897 info->fbcon_rotate_hint = FB_ROTATE_UR;
2901 /* Note: Drops fb_helper->lock before returning. */
2903 __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper,
2906 struct drm_device *dev = fb_helper->dev;
2907 struct fb_info *info;
2908 unsigned int width, height;
2911 width = dev->mode_config.max_width;
2912 height = dev->mode_config.max_height;
2914 drm_setup_crtcs(fb_helper, width, height);
2915 ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
2917 if (ret == -EAGAIN) {
2918 fb_helper->preferred_bpp = bpp_sel;
2919 fb_helper->deferred_setup = true;
2922 mutex_unlock(&fb_helper->lock);
2926 drm_setup_crtcs_fb(fb_helper);
2928 fb_helper->deferred_setup = false;
2930 info = fb_helper->fbdev;
2931 info->var.pixclock = 0;
2932 /* Shamelessly allow physical address leaking to userspace */
2933 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
2934 if (!drm_leak_fbdev_smem)
2936 /* don't leak any physical addresses to userspace */
2937 info->flags |= FBINFO_HIDE_SMEM_START;
2939 /* Need to drop locks to avoid recursive deadlock in
2940 * register_framebuffer. This is ok because the only thing left to do is
2941 * register the fbdev emulation instance in kernel_fb_helper_list. */
2942 mutex_unlock(&fb_helper->lock);
2944 ret = register_framebuffer(info);
2948 dev_info(dev->dev, "fb%d: %s frame buffer device\n",
2949 info->node, info->fix.id);
2951 mutex_lock(&kernel_fb_helper_lock);
2952 if (list_empty(&kernel_fb_helper_list))
2953 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
2955 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
2956 mutex_unlock(&kernel_fb_helper_lock);
2962 * drm_fb_helper_initial_config - setup a sane initial connector configuration
2963 * @fb_helper: fb_helper device struct
2964 * @bpp_sel: bpp value to use for the framebuffer configuration
2966 * Scans the CRTCs and connectors and tries to put together an initial setup.
2967 * At the moment, this is a cloned configuration across all heads with
2968 * a new framebuffer object as the backing store.
2970 * Note that this also registers the fbdev and so allows userspace to call into
2971 * the driver through the fbdev interfaces.
2973 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
2974 * to let the driver allocate and initialize the fbdev info structure and the
2975 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_info() is provided
2976 * as a helper to setup simple default values for the fbdev info structure.
2980 * When you have fbcon support built-in or already loaded, this function will do
2981 * a full modeset to setup the fbdev console. Due to locking misdesign in the
2982 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2983 * console_lock. Until console_unlock is called no dmesg lines will be sent out
2984 * to consoles, not even serial console. This means when your driver crashes,
2985 * you will see absolutely nothing else but a system stuck in this function,
2986 * with no further output. Any kind of printk() you place within your own driver
2987 * or in the drm core modeset code will also never show up.
2989 * Standard debug practice is to run the fbcon setup without taking the
2990 * console_lock as a hack, to be able to see backtraces and crashes on the
2991 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2994 * The other option is to just disable fbdev emulation since very likely the
2995 * first modeset from userspace will crash in the same way, and is even easier
2996 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
2997 * kernel cmdline option.
3000 * Zero if everything went ok, nonzero otherwise.
3002 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
3006 if (!drm_fbdev_emulation)
3009 mutex_lock(&fb_helper->lock);
3010 ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel);
3014 EXPORT_SYMBOL(drm_fb_helper_initial_config);
3017 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
3018 * probing all the outputs attached to the fb
3019 * @fb_helper: driver-allocated fbdev helper, can be NULL
3021 * Scan the connectors attached to the fb_helper and try to put together a
3022 * setup after notification of a change in output configuration.
3024 * Called at runtime, takes the mode config locks to be able to check/change the
3025 * modeset configuration. Must be run from process context (which usually means
3026 * either the output polling work or a work item launched from the driver's
3027 * hotplug interrupt).
3029 * Note that drivers may call this even before calling
3030 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
3031 * for a race-free fbcon setup and will make sure that the fbdev emulation will
3032 * not miss any hotplug events.
3035 * 0 on success and a non-zero error code otherwise.
3037 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
3041 if (!drm_fbdev_emulation || !fb_helper)
3044 mutex_lock(&fb_helper->lock);
3045 if (fb_helper->deferred_setup) {
3046 err = __drm_fb_helper_initial_config_and_unlock(fb_helper,
3047 fb_helper->preferred_bpp);
3051 if (!fb_helper->fb || !drm_master_internal_acquire(fb_helper->dev)) {
3052 fb_helper->delayed_hotplug = true;
3053 mutex_unlock(&fb_helper->lock);
3057 drm_master_internal_release(fb_helper->dev);
3059 DRM_DEBUG_KMS("\n");
3061 drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
3062 drm_setup_crtcs_fb(fb_helper);
3063 mutex_unlock(&fb_helper->lock);
3065 drm_fb_helper_set_par(fb_helper->fbdev);
3069 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
3072 * drm_fb_helper_fbdev_setup() - Setup fbdev emulation
3074 * @fb_helper: fbdev helper structure to set up
3075 * @funcs: fbdev helper functions
3076 * @preferred_bpp: Preferred bits per pixel for the device.
3077 * @dev->mode_config.preferred_depth is used if this is zero.
3078 * @max_conn_count: Maximum number of connectors.
3079 * @dev->mode_config.num_connector is used if this is zero.
3081 * This function sets up fbdev emulation and registers fbdev for access by
3082 * userspace. If all connectors are disconnected, setup is deferred to the next
3083 * time drm_fb_helper_hotplug_event() is called.
3084 * The caller must to provide a &drm_fb_helper_funcs->fb_probe callback
3087 * Use drm_fb_helper_fbdev_teardown() to destroy the fbdev.
3089 * See also: drm_fb_helper_initial_config(), drm_fbdev_generic_setup().
3092 * Zero on success or negative error code on failure.
3094 int drm_fb_helper_fbdev_setup(struct drm_device *dev,
3095 struct drm_fb_helper *fb_helper,
3096 const struct drm_fb_helper_funcs *funcs,
3097 unsigned int preferred_bpp,
3098 unsigned int max_conn_count)
3103 preferred_bpp = dev->mode_config.preferred_depth;
3107 if (!max_conn_count)
3108 max_conn_count = dev->mode_config.num_connector;
3109 if (!max_conn_count) {
3110 DRM_DEV_ERROR(dev->dev, "fbdev: No connectors\n");
3114 drm_fb_helper_prepare(dev, fb_helper, funcs);
3116 ret = drm_fb_helper_init(dev, fb_helper, max_conn_count);
3118 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to initialize (ret=%d)\n", ret);
3122 ret = drm_fb_helper_single_add_all_connectors(fb_helper);
3124 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to add connectors (ret=%d)\n", ret);
3125 goto err_drm_fb_helper_fini;
3128 if (!drm_drv_uses_atomic_modeset(dev))
3129 drm_helper_disable_unused_functions(dev);
3131 ret = drm_fb_helper_initial_config(fb_helper, preferred_bpp);
3133 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to set configuration (ret=%d)\n", ret);
3134 goto err_drm_fb_helper_fini;
3139 err_drm_fb_helper_fini:
3140 drm_fb_helper_fbdev_teardown(dev);
3144 EXPORT_SYMBOL(drm_fb_helper_fbdev_setup);
3147 * drm_fb_helper_fbdev_teardown - Tear down fbdev emulation
3150 * This function unregisters fbdev if not already done and cleans up the
3151 * associated resources including the &drm_framebuffer.
3152 * The driver is responsible for freeing the &drm_fb_helper structure which is
3153 * stored in &drm_device->fb_helper. Do note that this pointer has been cleared
3154 * when this function returns.
3156 * In order to support device removal/unplug while file handles are still open,
3157 * drm_fb_helper_unregister_fbi() should be called on device removal and
3158 * drm_fb_helper_fbdev_teardown() in the &drm_driver->release callback when
3159 * file handles are closed.
3161 void drm_fb_helper_fbdev_teardown(struct drm_device *dev)
3163 struct drm_fb_helper *fb_helper = dev->fb_helper;
3164 struct fb_ops *fbops = NULL;
3169 /* Unregister if it hasn't been done already */
3170 if (fb_helper->fbdev && fb_helper->fbdev->dev)
3171 drm_fb_helper_unregister_fbi(fb_helper);
3173 if (fb_helper->fbdev && fb_helper->fbdev->fbdefio) {
3174 fb_deferred_io_cleanup(fb_helper->fbdev);
3175 kfree(fb_helper->fbdev->fbdefio);
3176 fbops = fb_helper->fbdev->fbops;
3179 drm_fb_helper_fini(fb_helper);
3183 drm_framebuffer_remove(fb_helper->fb);
3185 EXPORT_SYMBOL(drm_fb_helper_fbdev_teardown);
3188 * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation
3191 * This function can be used as the &drm_driver->lastclose callback for drivers
3192 * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked().
3194 void drm_fb_helper_lastclose(struct drm_device *dev)
3196 drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper);
3198 EXPORT_SYMBOL(drm_fb_helper_lastclose);
3201 * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed
3202 * helper for fbdev emulation
3205 * This function can be used as the
3206 * &drm_mode_config_funcs.output_poll_changed callback for drivers that only
3207 * need to call drm_fb_helper_hotplug_event().
3209 void drm_fb_helper_output_poll_changed(struct drm_device *dev)
3211 drm_fb_helper_hotplug_event(dev->fb_helper);
3213 EXPORT_SYMBOL(drm_fb_helper_output_poll_changed);
3215 /* @user: 1=userspace, 0=fbcon */
3216 static int drm_fbdev_fb_open(struct fb_info *info, int user)
3218 struct drm_fb_helper *fb_helper = info->par;
3220 /* No need to take a ref for fbcon because it unbinds on unregister */
3221 if (user && !try_module_get(fb_helper->dev->driver->fops->owner))
3227 static int drm_fbdev_fb_release(struct fb_info *info, int user)
3229 struct drm_fb_helper *fb_helper = info->par;
3232 module_put(fb_helper->dev->driver->fops->owner);
3237 static void drm_fbdev_cleanup(struct drm_fb_helper *fb_helper)
3239 struct fb_info *fbi = fb_helper->fbdev;
3240 struct fb_ops *fbops = NULL;
3241 void *shadow = NULL;
3243 if (!fb_helper->dev)
3246 if (fbi && fbi->fbdefio) {
3247 fb_deferred_io_cleanup(fbi);
3248 shadow = fbi->screen_buffer;
3252 drm_fb_helper_fini(fb_helper);
3259 drm_client_framebuffer_delete(fb_helper->buffer);
3262 static void drm_fbdev_release(struct drm_fb_helper *fb_helper)
3264 drm_fbdev_cleanup(fb_helper);
3265 drm_client_release(&fb_helper->client);
3270 * fb_ops.fb_destroy is called by the last put_fb_info() call at the end of
3271 * unregister_framebuffer() or fb_release().
3273 static void drm_fbdev_fb_destroy(struct fb_info *info)
3275 drm_fbdev_release(info->par);
3278 static int drm_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
3280 struct drm_fb_helper *fb_helper = info->par;
3282 if (fb_helper->dev->driver->gem_prime_mmap)
3283 return fb_helper->dev->driver->gem_prime_mmap(fb_helper->buffer->gem, vma);
3288 static struct fb_ops drm_fbdev_fb_ops = {
3289 .owner = THIS_MODULE,
3290 DRM_FB_HELPER_DEFAULT_OPS,
3291 .fb_open = drm_fbdev_fb_open,
3292 .fb_release = drm_fbdev_fb_release,
3293 .fb_destroy = drm_fbdev_fb_destroy,
3294 .fb_mmap = drm_fbdev_fb_mmap,
3295 .fb_read = drm_fb_helper_sys_read,
3296 .fb_write = drm_fb_helper_sys_write,
3297 .fb_fillrect = drm_fb_helper_sys_fillrect,
3298 .fb_copyarea = drm_fb_helper_sys_copyarea,
3299 .fb_imageblit = drm_fb_helper_sys_imageblit,
3302 static struct fb_deferred_io drm_fbdev_defio = {
3304 .deferred_io = drm_fb_helper_deferred_io,
3308 * drm_fb_helper_generic_probe - Generic fbdev emulation probe helper
3309 * @fb_helper: fbdev helper structure
3310 * @sizes: describes fbdev size and scanout surface size
3312 * This function uses the client API to create a framebuffer backed by a dumb buffer.
3314 * The _sys_ versions are used for &fb_ops.fb_read, fb_write, fb_fillrect,
3315 * fb_copyarea, fb_imageblit.
3318 * Zero on success or negative error code on failure.
3320 int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper,
3321 struct drm_fb_helper_surface_size *sizes)
3323 struct drm_client_dev *client = &fb_helper->client;
3324 struct drm_client_buffer *buffer;
3325 struct drm_framebuffer *fb;
3326 struct fb_info *fbi;
3329 DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n",
3330 sizes->surface_width, sizes->surface_height,
3331 sizes->surface_bpp);
3333 format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
3334 buffer = drm_client_framebuffer_create(client, sizes->surface_width,
3335 sizes->surface_height, format);
3337 return PTR_ERR(buffer);
3339 fb_helper->buffer = buffer;
3340 fb_helper->fb = buffer->fb;
3343 fbi = drm_fb_helper_alloc_fbi(fb_helper);
3345 return PTR_ERR(fbi);
3347 fbi->fbops = &drm_fbdev_fb_ops;
3348 fbi->screen_size = fb->height * fb->pitches[0];
3349 fbi->fix.smem_len = fbi->screen_size;
3350 fbi->screen_buffer = buffer->vaddr;
3351 /* Shamelessly leak the physical address to user-space */
3352 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
3353 if (drm_leak_fbdev_smem && fbi->fix.smem_start == 0)
3354 fbi->fix.smem_start =
3355 page_to_phys(virt_to_page(fbi->screen_buffer));
3357 drm_fb_helper_fill_info(fbi, fb_helper, sizes);
3359 if (fb->funcs->dirty) {
3360 struct fb_ops *fbops;
3364 * fb_deferred_io_cleanup() clears &fbops->fb_mmap so a per
3365 * instance version is necessary.
3367 fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
3368 shadow = vzalloc(fbi->screen_size);
3369 if (!fbops || !shadow) {
3375 *fbops = *fbi->fbops;
3377 fbi->screen_buffer = shadow;
3378 fbi->fbdefio = &drm_fbdev_defio;
3380 fb_deferred_io_init(fbi);
3385 EXPORT_SYMBOL(drm_fb_helper_generic_probe);
3387 static const struct drm_fb_helper_funcs drm_fb_helper_generic_funcs = {
3388 .fb_probe = drm_fb_helper_generic_probe,
3391 static void drm_fbdev_client_unregister(struct drm_client_dev *client)
3393 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
3395 if (fb_helper->fbdev)
3396 /* drm_fbdev_fb_destroy() takes care of cleanup */
3397 drm_fb_helper_unregister_fbi(fb_helper);
3399 drm_fbdev_release(fb_helper);
3402 static int drm_fbdev_client_restore(struct drm_client_dev *client)
3404 drm_fb_helper_lastclose(client->dev);
3409 static int drm_fbdev_client_hotplug(struct drm_client_dev *client)
3411 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
3412 struct drm_device *dev = client->dev;
3415 /* Setup is not retried if it has failed */
3416 if (!fb_helper->dev && fb_helper->funcs)
3420 return drm_fb_helper_hotplug_event(dev->fb_helper);
3422 if (!dev->mode_config.num_connector) {
3423 DRM_DEV_DEBUG(dev->dev, "No connectors found, will not create framebuffer!\n");
3427 drm_fb_helper_prepare(dev, fb_helper, &drm_fb_helper_generic_funcs);
3429 ret = drm_fb_helper_init(dev, fb_helper, dev->mode_config.num_connector);
3433 ret = drm_fb_helper_single_add_all_connectors(fb_helper);
3437 if (!drm_drv_uses_atomic_modeset(dev))
3438 drm_helper_disable_unused_functions(dev);
3440 ret = drm_fb_helper_initial_config(fb_helper, fb_helper->preferred_bpp);
3447 drm_fbdev_cleanup(fb_helper);
3449 fb_helper->dev = NULL;
3450 fb_helper->fbdev = NULL;
3452 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to setup generic emulation (ret=%d)\n", ret);
3457 static const struct drm_client_funcs drm_fbdev_client_funcs = {
3458 .owner = THIS_MODULE,
3459 .unregister = drm_fbdev_client_unregister,
3460 .restore = drm_fbdev_client_restore,
3461 .hotplug = drm_fbdev_client_hotplug,
3465 * drm_fbdev_generic_setup() - Setup generic fbdev emulation
3467 * @preferred_bpp: Preferred bits per pixel for the device.
3468 * @dev->mode_config.preferred_depth is used if this is zero.
3470 * This function sets up generic fbdev emulation for drivers that supports
3471 * dumb buffers with a virtual address and that can be mmap'ed. If the driver
3472 * does not support these functions, it could use drm_fb_helper_fbdev_setup().
3474 * Restore, hotplug events and teardown are all taken care of. Drivers that do
3475 * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves.
3476 * Simple drivers might use drm_mode_config_helper_suspend().
3478 * Drivers that set the dirty callback on their framebuffer will get a shadow
3479 * fbdev buffer that is blitted onto the real buffer. This is done in order to
3480 * make deferred I/O work with all kinds of buffers.
3482 * This function is safe to call even when there are no connectors present.
3483 * Setup will be retried on the next hotplug event.
3485 * The fbdev is destroyed by drm_dev_unregister().
3488 * Zero on success or negative error code on failure.
3490 int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp)
3492 struct drm_fb_helper *fb_helper;
3495 WARN(dev->fb_helper, "fb_helper is already set!\n");
3497 if (!drm_fbdev_emulation)
3500 fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
3504 ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_client_funcs);
3507 DRM_DEV_ERROR(dev->dev, "Failed to register client: %d\n", ret);
3512 preferred_bpp = dev->mode_config.preferred_depth;
3515 fb_helper->preferred_bpp = preferred_bpp;
3517 ret = drm_fbdev_client_hotplug(&fb_helper->client);
3519 DRM_DEV_DEBUG(dev->dev, "client hotplug ret=%d\n", ret);
3521 drm_client_register(&fb_helper->client);
3525 EXPORT_SYMBOL(drm_fbdev_generic_setup);
3527 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
3528 * but the module doesn't depend on any fb console symbols. At least
3529 * attempt to load fbcon to avoid leaving the system without a usable console.
3531 int __init drm_fb_helper_modinit(void)
3533 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
3534 const char name[] = "fbcon";
3535 struct module *fbcon;
3537 mutex_lock(&module_mutex);
3538 fbcon = find_module(name);
3539 mutex_unlock(&module_mutex);
3542 request_module_nowait(name);
3546 EXPORT_SYMBOL(drm_fb_helper_modinit);