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/sysrq.h>
36 #include <linux/slab.h>
37 #include <linux/module.h>
39 #include <drm/drm_crtc.h>
40 #include <drm/drm_fb_helper.h>
41 #include <drm/drm_crtc_helper.h>
42 #include <drm/drm_atomic.h>
43 #include <drm/drm_atomic_helper.h>
45 #include "drm_crtc_internal.h"
46 #include "drm_crtc_helper_internal.h"
47 #include "drm_internal.h"
49 static bool drm_fbdev_emulation
= true;
50 module_param_named(fbdev_emulation
, drm_fbdev_emulation
, bool, 0600);
51 MODULE_PARM_DESC(fbdev_emulation
,
52 "Enable legacy fbdev emulation [default=true]");
54 static int drm_fbdev_overalloc
= CONFIG_DRM_FBDEV_OVERALLOC
;
55 module_param(drm_fbdev_overalloc
, int, 0444);
56 MODULE_PARM_DESC(drm_fbdev_overalloc
,
57 "Overallocation of the fbdev buffer (%) [default="
58 __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC
) "]");
61 * In order to keep user-space compatibility, we want in certain use-cases
62 * to keep leaking the fbdev physical address to the user-space program
63 * handling the fbdev buffer.
64 * This is a bad habit essentially kept into closed source opengl driver
65 * that should really be moved into open-source upstream projects instead
66 * of using legacy physical addresses in user space to communicate with
67 * other out-of-tree kernel modules.
69 * This module_param *should* be removed as soon as possible and be
70 * considered as a broken and legacy behaviour from a modern fbdev device.
72 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
73 static bool drm_leak_fbdev_smem
= false;
74 module_param_unsafe(drm_leak_fbdev_smem
, bool, 0600);
75 MODULE_PARM_DESC(drm_leak_fbdev_smem
,
76 "Allow unsafe leaking fbdev physical smem address [default=false]");
79 static LIST_HEAD(kernel_fb_helper_list
);
80 static DEFINE_MUTEX(kernel_fb_helper_lock
);
85 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
86 * mode setting driver. They can be used mostly independently from the crtc
87 * helper functions used by many drivers to implement the kernel mode setting
90 * Drivers that support a dumb buffer with a virtual address and mmap support,
91 * should try out the generic fbdev emulation using drm_fbdev_generic_setup().
93 * Setup fbdev emulation by calling drm_fb_helper_fbdev_setup() and tear it
94 * down by calling drm_fb_helper_fbdev_teardown().
96 * Drivers that need to handle connector hotplugging (e.g. dp mst) can't use
97 * the setup helper and will need to do the whole four-step setup process with
98 * drm_fb_helper_prepare(), drm_fb_helper_init(),
99 * drm_fb_helper_single_add_all_connectors(), enable hotplugging and
100 * drm_fb_helper_initial_config() to avoid a possible race window.
102 * At runtime drivers should restore the fbdev console by using
103 * drm_fb_helper_lastclose() as their &drm_driver.lastclose callback.
104 * They should also notify the fb helper code from updates to the output
105 * configuration by using drm_fb_helper_output_poll_changed() as their
106 * &drm_mode_config_funcs.output_poll_changed callback.
108 * For suspend/resume consider using drm_mode_config_helper_suspend() and
109 * drm_mode_config_helper_resume() which takes care of fbdev as well.
111 * All other functions exported by the fb helper library can be used to
112 * implement the fbdev driver interface by the driver.
114 * It is possible, though perhaps somewhat tricky, to implement race-free
115 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
116 * helper must be called first to initialize the minimum required to make
117 * hotplug detection work. Drivers also need to make sure to properly set up
118 * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
119 * it is safe to enable interrupts and start processing hotplug events. At the
120 * same time, drivers should initialize all modeset objects such as CRTCs,
121 * encoders and connectors. To finish up the fbdev helper initialization, the
122 * drm_fb_helper_init() function is called. To probe for all attached displays
123 * and set up an initial configuration using the detected hardware, drivers
124 * should call drm_fb_helper_single_add_all_connectors() followed by
125 * drm_fb_helper_initial_config().
127 * If &drm_framebuffer_funcs.dirty is set, the
128 * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
129 * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
130 * away. This worker then calls the dirty() function ensuring that it will
131 * always run in process context since the fb_*() function could be running in
132 * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
133 * callback it will also schedule dirty_work with the damage collected from the
134 * mmap page writes. Drivers can use drm_fb_helper_defio_init() to setup
135 * deferred I/O (coupled with drm_fb_helper_fbdev_teardown()).
138 #define drm_fb_helper_for_each_connector(fbh, i__) \
139 for (({ lockdep_assert_held(&(fbh)->lock); }), \
140 i__ = 0; i__ < (fbh)->connector_count; i__++)
142 static int __drm_fb_helper_add_one_connector(struct drm_fb_helper
*fb_helper
,
143 struct drm_connector
*connector
)
145 struct drm_fb_helper_connector
*fb_conn
;
146 struct drm_fb_helper_connector
**temp
;
149 if (!drm_fbdev_emulation
)
152 lockdep_assert_held(&fb_helper
->lock
);
154 count
= fb_helper
->connector_count
+ 1;
156 if (count
> fb_helper
->connector_info_alloc_count
) {
157 size_t size
= count
* sizeof(fb_conn
);
159 temp
= krealloc(fb_helper
->connector_info
, size
, GFP_KERNEL
);
163 fb_helper
->connector_info_alloc_count
= count
;
164 fb_helper
->connector_info
= temp
;
167 fb_conn
= kzalloc(sizeof(*fb_conn
), GFP_KERNEL
);
171 drm_connector_get(connector
);
172 fb_conn
->connector
= connector
;
173 fb_helper
->connector_info
[fb_helper
->connector_count
++] = fb_conn
;
178 int drm_fb_helper_add_one_connector(struct drm_fb_helper
*fb_helper
,
179 struct drm_connector
*connector
)
186 mutex_lock(&fb_helper
->lock
);
187 err
= __drm_fb_helper_add_one_connector(fb_helper
, connector
);
188 mutex_unlock(&fb_helper
->lock
);
192 EXPORT_SYMBOL(drm_fb_helper_add_one_connector
);
195 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
197 * @fb_helper: fbdev initialized with drm_fb_helper_init, can be NULL
199 * This functions adds all the available connectors for use with the given
200 * fb_helper. This is a separate step to allow drivers to freely assign
201 * connectors to the fbdev, e.g. if some are reserved for special purposes or
202 * not adequate to be used for the fbcon.
204 * This function is protected against concurrent connector hotadds/removals
205 * using drm_fb_helper_add_one_connector() and
206 * drm_fb_helper_remove_one_connector().
208 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper
*fb_helper
)
210 struct drm_device
*dev
;
211 struct drm_connector
*connector
;
212 struct drm_connector_list_iter conn_iter
;
215 if (!drm_fbdev_emulation
|| !fb_helper
)
218 dev
= fb_helper
->dev
;
220 mutex_lock(&fb_helper
->lock
);
221 drm_connector_list_iter_begin(dev
, &conn_iter
);
222 drm_for_each_connector_iter(connector
, &conn_iter
) {
223 if (connector
->connector_type
== DRM_MODE_CONNECTOR_WRITEBACK
)
226 ret
= __drm_fb_helper_add_one_connector(fb_helper
, connector
);
233 drm_fb_helper_for_each_connector(fb_helper
, i
) {
234 struct drm_fb_helper_connector
*fb_helper_connector
=
235 fb_helper
->connector_info
[i
];
237 drm_connector_put(fb_helper_connector
->connector
);
239 kfree(fb_helper_connector
);
240 fb_helper
->connector_info
[i
] = NULL
;
242 fb_helper
->connector_count
= 0;
244 drm_connector_list_iter_end(&conn_iter
);
245 mutex_unlock(&fb_helper
->lock
);
249 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors
);
251 static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper
*fb_helper
,
252 struct drm_connector
*connector
)
254 struct drm_fb_helper_connector
*fb_helper_connector
;
257 if (!drm_fbdev_emulation
)
260 lockdep_assert_held(&fb_helper
->lock
);
262 drm_fb_helper_for_each_connector(fb_helper
, i
) {
263 if (fb_helper
->connector_info
[i
]->connector
== connector
)
267 if (i
== fb_helper
->connector_count
)
269 fb_helper_connector
= fb_helper
->connector_info
[i
];
270 drm_connector_put(fb_helper_connector
->connector
);
272 for (j
= i
+ 1; j
< fb_helper
->connector_count
; j
++)
273 fb_helper
->connector_info
[j
- 1] = fb_helper
->connector_info
[j
];
275 fb_helper
->connector_count
--;
276 kfree(fb_helper_connector
);
281 int drm_fb_helper_remove_one_connector(struct drm_fb_helper
*fb_helper
,
282 struct drm_connector
*connector
)
289 mutex_lock(&fb_helper
->lock
);
290 err
= __drm_fb_helper_remove_one_connector(fb_helper
, connector
);
291 mutex_unlock(&fb_helper
->lock
);
295 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector
);
297 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc
*crtc
)
299 uint16_t *r_base
, *g_base
, *b_base
;
301 if (crtc
->funcs
->gamma_set
== NULL
)
304 r_base
= crtc
->gamma_store
;
305 g_base
= r_base
+ crtc
->gamma_size
;
306 b_base
= g_base
+ crtc
->gamma_size
;
308 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
,
309 crtc
->gamma_size
, NULL
);
313 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
314 * @info: fbdev registered by the helper
316 int drm_fb_helper_debug_enter(struct fb_info
*info
)
318 struct drm_fb_helper
*helper
= info
->par
;
319 const struct drm_crtc_helper_funcs
*funcs
;
322 list_for_each_entry(helper
, &kernel_fb_helper_list
, kernel_fb_list
) {
323 for (i
= 0; i
< helper
->crtc_count
; i
++) {
324 struct drm_mode_set
*mode_set
=
325 &helper
->crtc_info
[i
].mode_set
;
327 if (!mode_set
->crtc
->enabled
)
330 funcs
= mode_set
->crtc
->helper_private
;
331 if (funcs
->mode_set_base_atomic
== NULL
)
334 if (drm_drv_uses_atomic_modeset(mode_set
->crtc
->dev
))
337 funcs
->mode_set_base_atomic(mode_set
->crtc
,
341 ENTER_ATOMIC_MODE_SET
);
347 EXPORT_SYMBOL(drm_fb_helper_debug_enter
);
350 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
351 * @info: fbdev registered by the helper
353 int drm_fb_helper_debug_leave(struct fb_info
*info
)
355 struct drm_fb_helper
*helper
= info
->par
;
356 struct drm_crtc
*crtc
;
357 const struct drm_crtc_helper_funcs
*funcs
;
358 struct drm_framebuffer
*fb
;
361 for (i
= 0; i
< helper
->crtc_count
; i
++) {
362 struct drm_mode_set
*mode_set
= &helper
->crtc_info
[i
].mode_set
;
364 crtc
= mode_set
->crtc
;
365 if (drm_drv_uses_atomic_modeset(crtc
->dev
))
368 funcs
= crtc
->helper_private
;
369 fb
= crtc
->primary
->fb
;
375 DRM_ERROR("no fb to restore??\n");
379 if (funcs
->mode_set_base_atomic
== NULL
)
382 drm_fb_helper_restore_lut_atomic(mode_set
->crtc
);
383 funcs
->mode_set_base_atomic(mode_set
->crtc
, fb
, crtc
->x
,
384 crtc
->y
, LEAVE_ATOMIC_MODE_SET
);
389 EXPORT_SYMBOL(drm_fb_helper_debug_leave
);
391 /* Check if the plane can hw rotate to match panel orientation */
392 static bool drm_fb_helper_panel_rotation(struct drm_mode_set
*modeset
,
393 unsigned int *rotation
)
395 struct drm_connector
*connector
= modeset
->connectors
[0];
396 struct drm_plane
*plane
= modeset
->crtc
->primary
;
400 if (!modeset
->num_connectors
)
403 switch (connector
->display_info
.panel_orientation
) {
404 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP
:
405 *rotation
= DRM_MODE_ROTATE_180
;
407 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP
:
408 *rotation
= DRM_MODE_ROTATE_90
;
410 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP
:
411 *rotation
= DRM_MODE_ROTATE_270
;
414 *rotation
= DRM_MODE_ROTATE_0
;
418 * TODO: support 90 / 270 degree hardware rotation,
419 * depending on the hardware this may require the framebuffer
420 * to be in a specific tiling format.
422 if (*rotation
!= DRM_MODE_ROTATE_180
|| !plane
->rotation_property
)
425 for (i
= 0; i
< plane
->rotation_property
->num_values
; i
++)
426 valid_mask
|= (1ULL << plane
->rotation_property
->values
[i
]);
428 if (!(*rotation
& valid_mask
))
434 static int restore_fbdev_mode_atomic(struct drm_fb_helper
*fb_helper
, bool active
)
436 struct drm_device
*dev
= fb_helper
->dev
;
437 struct drm_plane_state
*plane_state
;
438 struct drm_plane
*plane
;
439 struct drm_atomic_state
*state
;
441 struct drm_modeset_acquire_ctx ctx
;
443 drm_modeset_acquire_init(&ctx
, 0);
445 state
= drm_atomic_state_alloc(dev
);
451 state
->acquire_ctx
= &ctx
;
453 drm_for_each_plane(plane
, dev
) {
454 plane_state
= drm_atomic_get_plane_state(state
, plane
);
455 if (IS_ERR(plane_state
)) {
456 ret
= PTR_ERR(plane_state
);
460 plane_state
->rotation
= DRM_MODE_ROTATE_0
;
462 /* disable non-primary: */
463 if (plane
->type
== DRM_PLANE_TYPE_PRIMARY
)
466 ret
= __drm_atomic_helper_disable_plane(plane
, plane_state
);
471 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
472 struct drm_mode_set
*mode_set
= &fb_helper
->crtc_info
[i
].mode_set
;
473 struct drm_plane
*primary
= mode_set
->crtc
->primary
;
474 unsigned int rotation
;
476 if (drm_fb_helper_panel_rotation(mode_set
, &rotation
)) {
477 /* Cannot fail as we've already gotten the plane state above */
478 plane_state
= drm_atomic_get_new_plane_state(state
, primary
);
479 plane_state
->rotation
= rotation
;
482 ret
= __drm_atomic_helper_set_config(mode_set
, state
);
487 * __drm_atomic_helper_set_config() sets active when a
488 * mode is set, unconditionally clear it if we force DPMS off
491 struct drm_crtc
*crtc
= mode_set
->crtc
;
492 struct drm_crtc_state
*crtc_state
= drm_atomic_get_new_crtc_state(state
, crtc
);
494 crtc_state
->active
= false;
498 ret
= drm_atomic_commit(state
);
504 drm_atomic_state_put(state
);
506 drm_modeset_drop_locks(&ctx
);
507 drm_modeset_acquire_fini(&ctx
);
512 drm_atomic_state_clear(state
);
513 drm_modeset_backoff(&ctx
);
518 static int restore_fbdev_mode_legacy(struct drm_fb_helper
*fb_helper
)
520 struct drm_device
*dev
= fb_helper
->dev
;
521 struct drm_plane
*plane
;
524 drm_modeset_lock_all(fb_helper
->dev
);
525 drm_for_each_plane(plane
, dev
) {
526 if (plane
->type
!= DRM_PLANE_TYPE_PRIMARY
)
527 drm_plane_force_disable(plane
);
529 if (plane
->rotation_property
)
530 drm_mode_plane_set_obj_prop(plane
,
531 plane
->rotation_property
,
535 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
536 struct drm_mode_set
*mode_set
= &fb_helper
->crtc_info
[i
].mode_set
;
537 struct drm_crtc
*crtc
= mode_set
->crtc
;
539 if (crtc
->funcs
->cursor_set2
) {
540 ret
= crtc
->funcs
->cursor_set2(crtc
, NULL
, 0, 0, 0, 0, 0);
543 } else if (crtc
->funcs
->cursor_set
) {
544 ret
= crtc
->funcs
->cursor_set(crtc
, NULL
, 0, 0, 0);
549 ret
= drm_mode_set_config_internal(mode_set
);
554 drm_modeset_unlock_all(fb_helper
->dev
);
559 static int restore_fbdev_mode_force(struct drm_fb_helper
*fb_helper
)
561 struct drm_device
*dev
= fb_helper
->dev
;
563 if (drm_drv_uses_atomic_modeset(dev
))
564 return restore_fbdev_mode_atomic(fb_helper
, true);
566 return restore_fbdev_mode_legacy(fb_helper
);
569 static int restore_fbdev_mode(struct drm_fb_helper
*fb_helper
)
571 struct drm_device
*dev
= fb_helper
->dev
;
574 if (!drm_master_internal_acquire(dev
))
577 ret
= restore_fbdev_mode_force(fb_helper
);
579 drm_master_internal_release(dev
);
585 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
586 * @fb_helper: driver-allocated fbdev helper, can be NULL
588 * This should be called from driver's drm &drm_driver.lastclose callback
589 * when implementing an fbcon on top of kms using this helper. This ensures that
590 * the user isn't greeted with a black screen when e.g. X dies.
593 * Zero if everything went ok, negative error code otherwise.
595 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper
*fb_helper
)
600 if (!drm_fbdev_emulation
|| !fb_helper
)
603 if (READ_ONCE(fb_helper
->deferred_setup
))
606 mutex_lock(&fb_helper
->lock
);
609 * We should bail out here if there is a master by dropping _force.
610 * Currently these igt tests fail if we do that:
611 * - kms_fbcon_fbt@psr
612 * - kms_fbcon_fbt@psr-suspend
614 * So first these tests need to be fixed so they drop master or don't
617 ret
= restore_fbdev_mode_force(fb_helper
);
619 do_delayed
= fb_helper
->delayed_hotplug
;
621 fb_helper
->delayed_hotplug
= false;
622 mutex_unlock(&fb_helper
->lock
);
625 drm_fb_helper_hotplug_event(fb_helper
);
629 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked
);
631 #ifdef CONFIG_MAGIC_SYSRQ
633 * restore fbcon display for all kms driver's using this helper, used for sysrq
634 * and panic handling.
636 static bool drm_fb_helper_force_kernel_mode(void)
638 bool ret
, error
= false;
639 struct drm_fb_helper
*helper
;
641 if (list_empty(&kernel_fb_helper_list
))
644 list_for_each_entry(helper
, &kernel_fb_helper_list
, kernel_fb_list
) {
645 struct drm_device
*dev
= helper
->dev
;
647 if (dev
->switch_power_state
== DRM_SWITCH_POWER_OFF
)
650 mutex_lock(&helper
->lock
);
651 ret
= restore_fbdev_mode_force(helper
);
654 mutex_unlock(&helper
->lock
);
659 static void drm_fb_helper_restore_work_fn(struct work_struct
*ignored
)
663 ret
= drm_fb_helper_force_kernel_mode();
665 DRM_ERROR("Failed to restore crtc configuration\n");
667 static DECLARE_WORK(drm_fb_helper_restore_work
, drm_fb_helper_restore_work_fn
);
669 static void drm_fb_helper_sysrq(int dummy1
)
671 schedule_work(&drm_fb_helper_restore_work
);
674 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op
= {
675 .handler
= drm_fb_helper_sysrq
,
676 .help_msg
= "force-fb(V)",
677 .action_msg
= "Restore framebuffer console",
680 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op
= { };
683 static void dpms_legacy(struct drm_fb_helper
*fb_helper
, int dpms_mode
)
685 struct drm_device
*dev
= fb_helper
->dev
;
686 struct drm_connector
*connector
;
687 struct drm_mode_set
*modeset
;
690 drm_modeset_lock_all(dev
);
691 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
692 modeset
= &fb_helper
->crtc_info
[i
].mode_set
;
694 if (!modeset
->crtc
->enabled
)
697 for (j
= 0; j
< modeset
->num_connectors
; j
++) {
698 connector
= modeset
->connectors
[j
];
699 connector
->funcs
->dpms(connector
, dpms_mode
);
700 drm_object_property_set_value(&connector
->base
,
701 dev
->mode_config
.dpms_property
, dpms_mode
);
704 drm_modeset_unlock_all(dev
);
707 static void drm_fb_helper_dpms(struct fb_info
*info
, int dpms_mode
)
709 struct drm_fb_helper
*fb_helper
= info
->par
;
710 struct drm_device
*dev
= fb_helper
->dev
;
713 * For each CRTC in this fb, turn the connectors on/off.
715 mutex_lock(&fb_helper
->lock
);
716 if (!drm_master_internal_acquire(dev
))
719 if (drm_drv_uses_atomic_modeset(dev
))
720 restore_fbdev_mode_atomic(fb_helper
, dpms_mode
== DRM_MODE_DPMS_ON
);
722 dpms_legacy(fb_helper
, dpms_mode
);
724 drm_master_internal_release(dev
);
726 mutex_unlock(&fb_helper
->lock
);
730 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
731 * @blank: desired blanking state
732 * @info: fbdev registered by the helper
734 int drm_fb_helper_blank(int blank
, struct fb_info
*info
)
736 if (oops_in_progress
)
740 /* Display: On; HSync: On, VSync: On */
741 case FB_BLANK_UNBLANK
:
742 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_ON
);
744 /* Display: Off; HSync: On, VSync: On */
745 case FB_BLANK_NORMAL
:
746 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_STANDBY
);
748 /* Display: Off; HSync: Off, VSync: On */
749 case FB_BLANK_HSYNC_SUSPEND
:
750 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_STANDBY
);
752 /* Display: Off; HSync: On, VSync: Off */
753 case FB_BLANK_VSYNC_SUSPEND
:
754 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_SUSPEND
);
756 /* Display: Off; HSync: Off, VSync: Off */
757 case FB_BLANK_POWERDOWN
:
758 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_OFF
);
763 EXPORT_SYMBOL(drm_fb_helper_blank
);
765 static void drm_fb_helper_modeset_release(struct drm_fb_helper
*helper
,
766 struct drm_mode_set
*modeset
)
770 for (i
= 0; i
< modeset
->num_connectors
; i
++) {
771 drm_connector_put(modeset
->connectors
[i
]);
772 modeset
->connectors
[i
] = NULL
;
774 modeset
->num_connectors
= 0;
776 drm_mode_destroy(helper
->dev
, modeset
->mode
);
777 modeset
->mode
= NULL
;
779 /* FIXME should hold a ref? */
783 static void drm_fb_helper_crtc_free(struct drm_fb_helper
*helper
)
787 for (i
= 0; i
< helper
->connector_count
; i
++) {
788 drm_connector_put(helper
->connector_info
[i
]->connector
);
789 kfree(helper
->connector_info
[i
]);
791 kfree(helper
->connector_info
);
793 for (i
= 0; i
< helper
->crtc_count
; i
++) {
794 struct drm_mode_set
*modeset
= &helper
->crtc_info
[i
].mode_set
;
796 drm_fb_helper_modeset_release(helper
, modeset
);
797 kfree(modeset
->connectors
);
799 kfree(helper
->crtc_info
);
802 static void drm_fb_helper_resume_worker(struct work_struct
*work
)
804 struct drm_fb_helper
*helper
= container_of(work
, struct drm_fb_helper
,
808 fb_set_suspend(helper
->fbdev
, 0);
812 static void drm_fb_helper_dirty_blit_real(struct drm_fb_helper
*fb_helper
,
813 struct drm_clip_rect
*clip
)
815 struct drm_framebuffer
*fb
= fb_helper
->fb
;
816 unsigned int cpp
= fb
->format
->cpp
[0];
817 size_t offset
= clip
->y1
* fb
->pitches
[0] + clip
->x1
* cpp
;
818 void *src
= fb_helper
->fbdev
->screen_buffer
+ offset
;
819 void *dst
= fb_helper
->buffer
->vaddr
+ offset
;
820 size_t len
= (clip
->x2
- clip
->x1
) * cpp
;
823 for (y
= clip
->y1
; y
< clip
->y2
; y
++) {
824 memcpy(dst
, src
, len
);
825 src
+= fb
->pitches
[0];
826 dst
+= fb
->pitches
[0];
830 static void drm_fb_helper_dirty_work(struct work_struct
*work
)
832 struct drm_fb_helper
*helper
= container_of(work
, struct drm_fb_helper
,
834 struct drm_clip_rect
*clip
= &helper
->dirty_clip
;
835 struct drm_clip_rect clip_copy
;
838 spin_lock_irqsave(&helper
->dirty_lock
, flags
);
840 clip
->x1
= clip
->y1
= ~0;
841 clip
->x2
= clip
->y2
= 0;
842 spin_unlock_irqrestore(&helper
->dirty_lock
, flags
);
844 /* call dirty callback only when it has been really touched */
845 if (clip_copy
.x1
< clip_copy
.x2
&& clip_copy
.y1
< clip_copy
.y2
) {
846 /* Generic fbdev uses a shadow buffer */
848 drm_fb_helper_dirty_blit_real(helper
, &clip_copy
);
849 helper
->fb
->funcs
->dirty(helper
->fb
, NULL
, 0, 0, &clip_copy
, 1);
854 * drm_fb_helper_prepare - setup a drm_fb_helper structure
856 * @helper: driver-allocated fbdev helper structure to set up
857 * @funcs: pointer to structure of functions associate with this helper
859 * Sets up the bare minimum to make the framebuffer helper usable. This is
860 * useful to implement race-free initialization of the polling helpers.
862 void drm_fb_helper_prepare(struct drm_device
*dev
, struct drm_fb_helper
*helper
,
863 const struct drm_fb_helper_funcs
*funcs
)
865 INIT_LIST_HEAD(&helper
->kernel_fb_list
);
866 spin_lock_init(&helper
->dirty_lock
);
867 INIT_WORK(&helper
->resume_work
, drm_fb_helper_resume_worker
);
868 INIT_WORK(&helper
->dirty_work
, drm_fb_helper_dirty_work
);
869 helper
->dirty_clip
.x1
= helper
->dirty_clip
.y1
= ~0;
870 mutex_init(&helper
->lock
);
871 helper
->funcs
= funcs
;
874 EXPORT_SYMBOL(drm_fb_helper_prepare
);
877 * drm_fb_helper_init - initialize a &struct drm_fb_helper
879 * @fb_helper: driver-allocated fbdev helper structure to initialize
880 * @max_conn_count: max connector count
882 * This allocates the structures for the fbdev helper with the given limits.
883 * Note that this won't yet touch the hardware (through the driver interfaces)
884 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
885 * to allow driver writes more control over the exact init sequence.
887 * Drivers must call drm_fb_helper_prepare() before calling this function.
890 * Zero if everything went ok, nonzero otherwise.
892 int drm_fb_helper_init(struct drm_device
*dev
,
893 struct drm_fb_helper
*fb_helper
,
896 struct drm_crtc
*crtc
;
897 struct drm_mode_config
*config
= &dev
->mode_config
;
900 if (!drm_fbdev_emulation
) {
901 dev
->fb_helper
= fb_helper
;
908 fb_helper
->crtc_info
= kcalloc(config
->num_crtc
, sizeof(struct drm_fb_helper_crtc
), GFP_KERNEL
);
909 if (!fb_helper
->crtc_info
)
912 fb_helper
->crtc_count
= config
->num_crtc
;
913 fb_helper
->connector_info
= kcalloc(dev
->mode_config
.num_connector
, sizeof(struct drm_fb_helper_connector
*), GFP_KERNEL
);
914 if (!fb_helper
->connector_info
) {
915 kfree(fb_helper
->crtc_info
);
918 fb_helper
->connector_info_alloc_count
= dev
->mode_config
.num_connector
;
919 fb_helper
->connector_count
= 0;
921 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
922 fb_helper
->crtc_info
[i
].mode_set
.connectors
=
923 kcalloc(max_conn_count
,
924 sizeof(struct drm_connector
*),
927 if (!fb_helper
->crtc_info
[i
].mode_set
.connectors
)
929 fb_helper
->crtc_info
[i
].mode_set
.num_connectors
= 0;
933 drm_for_each_crtc(crtc
, dev
) {
934 fb_helper
->crtc_info
[i
].mode_set
.crtc
= crtc
;
938 dev
->fb_helper
= fb_helper
;
942 drm_fb_helper_crtc_free(fb_helper
);
945 EXPORT_SYMBOL(drm_fb_helper_init
);
948 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
949 * @fb_helper: driver-allocated fbdev helper
951 * A helper to alloc fb_info and the members cmap and apertures. Called
952 * by the driver within the fb_probe fb_helper callback function. Drivers do not
953 * need to release the allocated fb_info structure themselves, this is
954 * automatically done when calling drm_fb_helper_fini().
957 * fb_info pointer if things went okay, pointer containing error code
960 struct fb_info
*drm_fb_helper_alloc_fbi(struct drm_fb_helper
*fb_helper
)
962 struct device
*dev
= fb_helper
->dev
->dev
;
963 struct fb_info
*info
;
966 info
= framebuffer_alloc(0, dev
);
968 return ERR_PTR(-ENOMEM
);
970 ret
= fb_alloc_cmap(&info
->cmap
, 256, 0);
974 info
->apertures
= alloc_apertures(1);
975 if (!info
->apertures
) {
980 fb_helper
->fbdev
= info
;
981 info
->skip_vt_switch
= true;
986 fb_dealloc_cmap(&info
->cmap
);
988 framebuffer_release(info
);
991 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi
);
994 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
995 * @fb_helper: driver-allocated fbdev helper, can be NULL
997 * A wrapper around unregister_framebuffer, to release the fb_info
998 * framebuffer device. This must be called before releasing all resources for
999 * @fb_helper by calling drm_fb_helper_fini().
1001 void drm_fb_helper_unregister_fbi(struct drm_fb_helper
*fb_helper
)
1003 if (fb_helper
&& fb_helper
->fbdev
)
1004 unregister_framebuffer(fb_helper
->fbdev
);
1006 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi
);
1009 * drm_fb_helper_fini - finialize a &struct drm_fb_helper
1010 * @fb_helper: driver-allocated fbdev helper, can be NULL
1012 * This cleans up all remaining resources associated with @fb_helper. Must be
1013 * called after drm_fb_helper_unlink_fbi() was called.
1015 void drm_fb_helper_fini(struct drm_fb_helper
*fb_helper
)
1017 struct fb_info
*info
;
1022 fb_helper
->dev
->fb_helper
= NULL
;
1024 if (!drm_fbdev_emulation
)
1027 cancel_work_sync(&fb_helper
->resume_work
);
1028 cancel_work_sync(&fb_helper
->dirty_work
);
1030 info
= fb_helper
->fbdev
;
1033 fb_dealloc_cmap(&info
->cmap
);
1034 framebuffer_release(info
);
1036 fb_helper
->fbdev
= NULL
;
1038 mutex_lock(&kernel_fb_helper_lock
);
1039 if (!list_empty(&fb_helper
->kernel_fb_list
)) {
1040 list_del(&fb_helper
->kernel_fb_list
);
1041 if (list_empty(&kernel_fb_helper_list
))
1042 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op
);
1044 mutex_unlock(&kernel_fb_helper_lock
);
1046 mutex_destroy(&fb_helper
->lock
);
1047 drm_fb_helper_crtc_free(fb_helper
);
1050 EXPORT_SYMBOL(drm_fb_helper_fini
);
1053 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
1054 * @fb_helper: driver-allocated fbdev helper, can be NULL
1056 * A wrapper around unlink_framebuffer implemented by fbdev core
1058 void drm_fb_helper_unlink_fbi(struct drm_fb_helper
*fb_helper
)
1060 if (fb_helper
&& fb_helper
->fbdev
)
1061 unlink_framebuffer(fb_helper
->fbdev
);
1063 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi
);
1065 static void drm_fb_helper_dirty(struct fb_info
*info
, u32 x
, u32 y
,
1066 u32 width
, u32 height
)
1068 struct drm_fb_helper
*helper
= info
->par
;
1069 struct drm_clip_rect
*clip
= &helper
->dirty_clip
;
1070 unsigned long flags
;
1072 if (!helper
->fb
->funcs
->dirty
)
1075 spin_lock_irqsave(&helper
->dirty_lock
, flags
);
1076 clip
->x1
= min_t(u32
, clip
->x1
, x
);
1077 clip
->y1
= min_t(u32
, clip
->y1
, y
);
1078 clip
->x2
= max_t(u32
, clip
->x2
, x
+ width
);
1079 clip
->y2
= max_t(u32
, clip
->y2
, y
+ height
);
1080 spin_unlock_irqrestore(&helper
->dirty_lock
, flags
);
1082 schedule_work(&helper
->dirty_work
);
1086 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
1087 * @info: fb_info struct pointer
1088 * @pagelist: list of dirty mmap framebuffer pages
1090 * This function is used as the &fb_deferred_io.deferred_io
1091 * callback function for flushing the fbdev mmap writes.
1093 void drm_fb_helper_deferred_io(struct fb_info
*info
,
1094 struct list_head
*pagelist
)
1096 unsigned long start
, end
, min
, max
;
1102 list_for_each_entry(page
, pagelist
, lru
) {
1103 start
= page
->index
<< PAGE_SHIFT
;
1104 end
= start
+ PAGE_SIZE
- 1;
1105 min
= min(min
, start
);
1106 max
= max(max
, end
);
1110 y1
= min
/ info
->fix
.line_length
;
1111 y2
= min_t(u32
, DIV_ROUND_UP(max
, info
->fix
.line_length
),
1113 drm_fb_helper_dirty(info
, 0, y1
, info
->var
.xres
, y2
- y1
);
1116 EXPORT_SYMBOL(drm_fb_helper_deferred_io
);
1119 * drm_fb_helper_defio_init - fbdev deferred I/O initialization
1120 * @fb_helper: driver-allocated fbdev helper
1122 * This function allocates &fb_deferred_io, sets callback to
1123 * drm_fb_helper_deferred_io(), delay to 50ms and calls fb_deferred_io_init().
1124 * It should be called from the &drm_fb_helper_funcs->fb_probe callback.
1125 * drm_fb_helper_fbdev_teardown() cleans up deferred I/O.
1127 * NOTE: A copy of &fb_ops is made and assigned to &info->fbops. This is done
1128 * because fb_deferred_io_cleanup() clears &fbops->fb_mmap and would thereby
1129 * affect other instances of that &fb_ops.
1132 * 0 on success or a negative error code on failure.
1134 int drm_fb_helper_defio_init(struct drm_fb_helper
*fb_helper
)
1136 struct fb_info
*info
= fb_helper
->fbdev
;
1137 struct fb_deferred_io
*fbdefio
;
1138 struct fb_ops
*fbops
;
1140 fbdefio
= kzalloc(sizeof(*fbdefio
), GFP_KERNEL
);
1141 fbops
= kzalloc(sizeof(*fbops
), GFP_KERNEL
);
1142 if (!fbdefio
|| !fbops
) {
1148 info
->fbdefio
= fbdefio
;
1149 fbdefio
->delay
= msecs_to_jiffies(50);
1150 fbdefio
->deferred_io
= drm_fb_helper_deferred_io
;
1152 *fbops
= *info
->fbops
;
1153 info
->fbops
= fbops
;
1155 fb_deferred_io_init(info
);
1159 EXPORT_SYMBOL(drm_fb_helper_defio_init
);
1162 * drm_fb_helper_sys_read - wrapper around fb_sys_read
1163 * @info: fb_info struct pointer
1164 * @buf: userspace buffer to read from framebuffer memory
1165 * @count: number of bytes to read from framebuffer memory
1166 * @ppos: read offset within framebuffer memory
1168 * A wrapper around fb_sys_read implemented by fbdev core
1170 ssize_t
drm_fb_helper_sys_read(struct fb_info
*info
, char __user
*buf
,
1171 size_t count
, loff_t
*ppos
)
1173 return fb_sys_read(info
, buf
, count
, ppos
);
1175 EXPORT_SYMBOL(drm_fb_helper_sys_read
);
1178 * drm_fb_helper_sys_write - wrapper around fb_sys_write
1179 * @info: fb_info struct pointer
1180 * @buf: userspace buffer to write to framebuffer memory
1181 * @count: number of bytes to write to framebuffer memory
1182 * @ppos: write offset within framebuffer memory
1184 * A wrapper around fb_sys_write implemented by fbdev core
1186 ssize_t
drm_fb_helper_sys_write(struct fb_info
*info
, const char __user
*buf
,
1187 size_t count
, loff_t
*ppos
)
1191 ret
= fb_sys_write(info
, buf
, count
, ppos
);
1193 drm_fb_helper_dirty(info
, 0, 0, info
->var
.xres
,
1198 EXPORT_SYMBOL(drm_fb_helper_sys_write
);
1201 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
1202 * @info: fbdev registered by the helper
1203 * @rect: info about rectangle to fill
1205 * A wrapper around sys_fillrect implemented by fbdev core
1207 void drm_fb_helper_sys_fillrect(struct fb_info
*info
,
1208 const struct fb_fillrect
*rect
)
1210 sys_fillrect(info
, rect
);
1211 drm_fb_helper_dirty(info
, rect
->dx
, rect
->dy
,
1212 rect
->width
, rect
->height
);
1214 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect
);
1217 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1218 * @info: fbdev registered by the helper
1219 * @area: info about area to copy
1221 * A wrapper around sys_copyarea implemented by fbdev core
1223 void drm_fb_helper_sys_copyarea(struct fb_info
*info
,
1224 const struct fb_copyarea
*area
)
1226 sys_copyarea(info
, area
);
1227 drm_fb_helper_dirty(info
, area
->dx
, area
->dy
,
1228 area
->width
, area
->height
);
1230 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea
);
1233 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1234 * @info: fbdev registered by the helper
1235 * @image: info about image to blit
1237 * A wrapper around sys_imageblit implemented by fbdev core
1239 void drm_fb_helper_sys_imageblit(struct fb_info
*info
,
1240 const struct fb_image
*image
)
1242 sys_imageblit(info
, image
);
1243 drm_fb_helper_dirty(info
, image
->dx
, image
->dy
,
1244 image
->width
, image
->height
);
1246 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit
);
1249 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1250 * @info: fbdev registered by the helper
1251 * @rect: info about rectangle to fill
1253 * A wrapper around cfb_fillrect implemented by fbdev core
1255 void drm_fb_helper_cfb_fillrect(struct fb_info
*info
,
1256 const struct fb_fillrect
*rect
)
1258 cfb_fillrect(info
, rect
);
1259 drm_fb_helper_dirty(info
, rect
->dx
, rect
->dy
,
1260 rect
->width
, rect
->height
);
1262 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect
);
1265 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1266 * @info: fbdev registered by the helper
1267 * @area: info about area to copy
1269 * A wrapper around cfb_copyarea implemented by fbdev core
1271 void drm_fb_helper_cfb_copyarea(struct fb_info
*info
,
1272 const struct fb_copyarea
*area
)
1274 cfb_copyarea(info
, area
);
1275 drm_fb_helper_dirty(info
, area
->dx
, area
->dy
,
1276 area
->width
, area
->height
);
1278 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea
);
1281 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1282 * @info: fbdev registered by the helper
1283 * @image: info about image to blit
1285 * A wrapper around cfb_imageblit implemented by fbdev core
1287 void drm_fb_helper_cfb_imageblit(struct fb_info
*info
,
1288 const struct fb_image
*image
)
1290 cfb_imageblit(info
, image
);
1291 drm_fb_helper_dirty(info
, image
->dx
, image
->dy
,
1292 image
->width
, image
->height
);
1294 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit
);
1297 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
1298 * @fb_helper: driver-allocated fbdev helper, can be NULL
1299 * @suspend: whether to suspend or resume
1301 * A wrapper around fb_set_suspend implemented by fbdev core.
1302 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
1305 void drm_fb_helper_set_suspend(struct drm_fb_helper
*fb_helper
, bool suspend
)
1307 if (fb_helper
&& fb_helper
->fbdev
)
1308 fb_set_suspend(fb_helper
->fbdev
, suspend
);
1310 EXPORT_SYMBOL(drm_fb_helper_set_suspend
);
1313 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
1314 * takes the console lock
1315 * @fb_helper: driver-allocated fbdev helper, can be NULL
1316 * @suspend: whether to suspend or resume
1318 * A wrapper around fb_set_suspend() that takes the console lock. If the lock
1319 * isn't available on resume, a worker is tasked with waiting for the lock
1320 * to become available. The console lock can be pretty contented on resume
1321 * due to all the printk activity.
1323 * This function can be called multiple times with the same state since
1324 * &fb_info.state is checked to see if fbdev is running or not before locking.
1326 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
1328 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper
*fb_helper
,
1331 if (!fb_helper
|| !fb_helper
->fbdev
)
1334 /* make sure there's no pending/ongoing resume */
1335 flush_work(&fb_helper
->resume_work
);
1338 if (fb_helper
->fbdev
->state
!= FBINFO_STATE_RUNNING
)
1344 if (fb_helper
->fbdev
->state
== FBINFO_STATE_RUNNING
)
1347 if (!console_trylock()) {
1348 schedule_work(&fb_helper
->resume_work
);
1353 fb_set_suspend(fb_helper
->fbdev
, suspend
);
1356 EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked
);
1358 static int setcmap_pseudo_palette(struct fb_cmap
*cmap
, struct fb_info
*info
)
1360 u32
*palette
= (u32
*)info
->pseudo_palette
;
1363 if (cmap
->start
+ cmap
->len
> 16)
1366 for (i
= 0; i
< cmap
->len
; ++i
) {
1367 u16 red
= cmap
->red
[i
];
1368 u16 green
= cmap
->green
[i
];
1369 u16 blue
= cmap
->blue
[i
];
1372 red
>>= 16 - info
->var
.red
.length
;
1373 green
>>= 16 - info
->var
.green
.length
;
1374 blue
>>= 16 - info
->var
.blue
.length
;
1375 value
= (red
<< info
->var
.red
.offset
) |
1376 (green
<< info
->var
.green
.offset
) |
1377 (blue
<< info
->var
.blue
.offset
);
1378 if (info
->var
.transp
.length
> 0) {
1379 u32 mask
= (1 << info
->var
.transp
.length
) - 1;
1381 mask
<<= info
->var
.transp
.offset
;
1384 palette
[cmap
->start
+ i
] = value
;
1390 static int setcmap_legacy(struct fb_cmap
*cmap
, struct fb_info
*info
)
1392 struct drm_fb_helper
*fb_helper
= info
->par
;
1393 struct drm_crtc
*crtc
;
1397 drm_modeset_lock_all(fb_helper
->dev
);
1398 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1399 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
1400 if (!crtc
->funcs
->gamma_set
|| !crtc
->gamma_size
)
1403 if (cmap
->start
+ cmap
->len
> crtc
->gamma_size
)
1406 r
= crtc
->gamma_store
;
1407 g
= r
+ crtc
->gamma_size
;
1408 b
= g
+ crtc
->gamma_size
;
1410 memcpy(r
+ cmap
->start
, cmap
->red
, cmap
->len
* sizeof(*r
));
1411 memcpy(g
+ cmap
->start
, cmap
->green
, cmap
->len
* sizeof(*g
));
1412 memcpy(b
+ cmap
->start
, cmap
->blue
, cmap
->len
* sizeof(*b
));
1414 ret
= crtc
->funcs
->gamma_set(crtc
, r
, g
, b
,
1415 crtc
->gamma_size
, NULL
);
1419 drm_modeset_unlock_all(fb_helper
->dev
);
1424 static struct drm_property_blob
*setcmap_new_gamma_lut(struct drm_crtc
*crtc
,
1425 struct fb_cmap
*cmap
)
1427 struct drm_device
*dev
= crtc
->dev
;
1428 struct drm_property_blob
*gamma_lut
;
1429 struct drm_color_lut
*lut
;
1430 int size
= crtc
->gamma_size
;
1433 if (!size
|| cmap
->start
+ cmap
->len
> size
)
1434 return ERR_PTR(-EINVAL
);
1436 gamma_lut
= drm_property_create_blob(dev
, sizeof(*lut
) * size
, NULL
);
1437 if (IS_ERR(gamma_lut
))
1440 lut
= gamma_lut
->data
;
1441 if (cmap
->start
|| cmap
->len
!= size
) {
1442 u16
*r
= crtc
->gamma_store
;
1443 u16
*g
= r
+ crtc
->gamma_size
;
1444 u16
*b
= g
+ crtc
->gamma_size
;
1446 for (i
= 0; i
< cmap
->start
; i
++) {
1448 lut
[i
].green
= g
[i
];
1451 for (i
= cmap
->start
+ cmap
->len
; i
< size
; i
++) {
1453 lut
[i
].green
= g
[i
];
1458 for (i
= 0; i
< cmap
->len
; i
++) {
1459 lut
[cmap
->start
+ i
].red
= cmap
->red
[i
];
1460 lut
[cmap
->start
+ i
].green
= cmap
->green
[i
];
1461 lut
[cmap
->start
+ i
].blue
= cmap
->blue
[i
];
1467 static int setcmap_atomic(struct fb_cmap
*cmap
, struct fb_info
*info
)
1469 struct drm_fb_helper
*fb_helper
= info
->par
;
1470 struct drm_device
*dev
= fb_helper
->dev
;
1471 struct drm_property_blob
*gamma_lut
= NULL
;
1472 struct drm_modeset_acquire_ctx ctx
;
1473 struct drm_crtc_state
*crtc_state
;
1474 struct drm_atomic_state
*state
;
1475 struct drm_crtc
*crtc
;
1480 drm_modeset_acquire_init(&ctx
, 0);
1482 state
= drm_atomic_state_alloc(dev
);
1488 state
->acquire_ctx
= &ctx
;
1490 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1491 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
1494 gamma_lut
= setcmap_new_gamma_lut(crtc
, cmap
);
1495 if (IS_ERR(gamma_lut
)) {
1496 ret
= PTR_ERR(gamma_lut
);
1501 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
1502 if (IS_ERR(crtc_state
)) {
1503 ret
= PTR_ERR(crtc_state
);
1507 replaced
= drm_property_replace_blob(&crtc_state
->degamma_lut
,
1509 replaced
|= drm_property_replace_blob(&crtc_state
->ctm
, NULL
);
1510 replaced
|= drm_property_replace_blob(&crtc_state
->gamma_lut
,
1512 crtc_state
->color_mgmt_changed
|= replaced
;
1515 ret
= drm_atomic_commit(state
);
1519 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1520 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
1522 r
= crtc
->gamma_store
;
1523 g
= r
+ crtc
->gamma_size
;
1524 b
= g
+ crtc
->gamma_size
;
1526 memcpy(r
+ cmap
->start
, cmap
->red
, cmap
->len
* sizeof(*r
));
1527 memcpy(g
+ cmap
->start
, cmap
->green
, cmap
->len
* sizeof(*g
));
1528 memcpy(b
+ cmap
->start
, cmap
->blue
, cmap
->len
* sizeof(*b
));
1532 if (ret
== -EDEADLK
)
1535 drm_property_blob_put(gamma_lut
);
1536 drm_atomic_state_put(state
);
1538 drm_modeset_drop_locks(&ctx
);
1539 drm_modeset_acquire_fini(&ctx
);
1544 drm_atomic_state_clear(state
);
1545 drm_modeset_backoff(&ctx
);
1550 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
1551 * @cmap: cmap to set
1552 * @info: fbdev registered by the helper
1554 int drm_fb_helper_setcmap(struct fb_cmap
*cmap
, struct fb_info
*info
)
1556 struct drm_fb_helper
*fb_helper
= info
->par
;
1557 struct drm_device
*dev
= fb_helper
->dev
;
1560 if (oops_in_progress
)
1563 mutex_lock(&fb_helper
->lock
);
1565 if (!drm_master_internal_acquire(dev
)) {
1570 if (info
->fix
.visual
== FB_VISUAL_TRUECOLOR
)
1571 ret
= setcmap_pseudo_palette(cmap
, info
);
1572 else if (drm_drv_uses_atomic_modeset(fb_helper
->dev
))
1573 ret
= setcmap_atomic(cmap
, info
);
1575 ret
= setcmap_legacy(cmap
, info
);
1577 drm_master_internal_release(dev
);
1579 mutex_unlock(&fb_helper
->lock
);
1583 EXPORT_SYMBOL(drm_fb_helper_setcmap
);
1586 * drm_fb_helper_ioctl - legacy ioctl implementation
1587 * @info: fbdev registered by the helper
1588 * @cmd: ioctl command
1589 * @arg: ioctl argument
1591 * A helper to implement the standard fbdev ioctl. Only
1592 * FBIO_WAITFORVSYNC is implemented for now.
1594 int drm_fb_helper_ioctl(struct fb_info
*info
, unsigned int cmd
,
1597 struct drm_fb_helper
*fb_helper
= info
->par
;
1598 struct drm_device
*dev
= fb_helper
->dev
;
1599 struct drm_mode_set
*mode_set
;
1600 struct drm_crtc
*crtc
;
1603 mutex_lock(&fb_helper
->lock
);
1604 if (!drm_master_internal_acquire(dev
)) {
1610 case FBIO_WAITFORVSYNC
:
1612 * Only consider the first CRTC.
1614 * This ioctl is supposed to take the CRTC number as
1615 * an argument, but in fbdev times, what that number
1616 * was supposed to be was quite unclear, different
1617 * drivers were passing that argument differently
1618 * (some by reference, some by value), and most of the
1619 * userspace applications were just hardcoding 0 as an
1622 * The first CRTC should be the integrated panel on
1623 * most drivers, so this is the best choice we can
1624 * make. If we're not smart enough here, one should
1625 * just consider switch the userspace to KMS.
1627 mode_set
= &fb_helper
->crtc_info
[0].mode_set
;
1628 crtc
= mode_set
->crtc
;
1631 * Only wait for a vblank event if the CRTC is
1632 * enabled, otherwise just don't do anythintg,
1633 * not even report an error.
1635 ret
= drm_crtc_vblank_get(crtc
);
1637 drm_crtc_wait_one_vblank(crtc
);
1638 drm_crtc_vblank_put(crtc
);
1647 drm_master_internal_release(dev
);
1649 mutex_unlock(&fb_helper
->lock
);
1652 EXPORT_SYMBOL(drm_fb_helper_ioctl
);
1654 static bool drm_fb_pixel_format_equal(const struct fb_var_screeninfo
*var_1
,
1655 const struct fb_var_screeninfo
*var_2
)
1657 return var_1
->bits_per_pixel
== var_2
->bits_per_pixel
&&
1658 var_1
->grayscale
== var_2
->grayscale
&&
1659 var_1
->red
.offset
== var_2
->red
.offset
&&
1660 var_1
->red
.length
== var_2
->red
.length
&&
1661 var_1
->red
.msb_right
== var_2
->red
.msb_right
&&
1662 var_1
->green
.offset
== var_2
->green
.offset
&&
1663 var_1
->green
.length
== var_2
->green
.length
&&
1664 var_1
->green
.msb_right
== var_2
->green
.msb_right
&&
1665 var_1
->blue
.offset
== var_2
->blue
.offset
&&
1666 var_1
->blue
.length
== var_2
->blue
.length
&&
1667 var_1
->blue
.msb_right
== var_2
->blue
.msb_right
&&
1668 var_1
->transp
.offset
== var_2
->transp
.offset
&&
1669 var_1
->transp
.length
== var_2
->transp
.length
&&
1670 var_1
->transp
.msb_right
== var_2
->transp
.msb_right
;
1673 static void drm_fb_helper_fill_pixel_fmt(struct fb_var_screeninfo
*var
,
1678 var
->red
.offset
= 0;
1679 var
->green
.offset
= 0;
1680 var
->blue
.offset
= 0;
1681 var
->red
.length
= 8; /* 8bit DAC */
1682 var
->green
.length
= 8;
1683 var
->blue
.length
= 8;
1684 var
->transp
.offset
= 0;
1685 var
->transp
.length
= 0;
1688 var
->red
.offset
= 10;
1689 var
->green
.offset
= 5;
1690 var
->blue
.offset
= 0;
1691 var
->red
.length
= 5;
1692 var
->green
.length
= 5;
1693 var
->blue
.length
= 5;
1694 var
->transp
.offset
= 15;
1695 var
->transp
.length
= 1;
1698 var
->red
.offset
= 11;
1699 var
->green
.offset
= 5;
1700 var
->blue
.offset
= 0;
1701 var
->red
.length
= 5;
1702 var
->green
.length
= 6;
1703 var
->blue
.length
= 5;
1704 var
->transp
.offset
= 0;
1707 var
->red
.offset
= 16;
1708 var
->green
.offset
= 8;
1709 var
->blue
.offset
= 0;
1710 var
->red
.length
= 8;
1711 var
->green
.length
= 8;
1712 var
->blue
.length
= 8;
1713 var
->transp
.offset
= 0;
1714 var
->transp
.length
= 0;
1717 var
->red
.offset
= 16;
1718 var
->green
.offset
= 8;
1719 var
->blue
.offset
= 0;
1720 var
->red
.length
= 8;
1721 var
->green
.length
= 8;
1722 var
->blue
.length
= 8;
1723 var
->transp
.offset
= 24;
1724 var
->transp
.length
= 8;
1732 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
1733 * @var: screeninfo to check
1734 * @info: fbdev registered by the helper
1736 int drm_fb_helper_check_var(struct fb_var_screeninfo
*var
,
1737 struct fb_info
*info
)
1739 struct drm_fb_helper
*fb_helper
= info
->par
;
1740 struct drm_framebuffer
*fb
= fb_helper
->fb
;
1742 if (in_dbg_master())
1745 if (var
->pixclock
!= 0) {
1746 DRM_DEBUG("fbdev emulation doesn't support changing the pixel clock, value of pixclock is ignored\n");
1750 if ((drm_format_info_block_width(fb
->format
, 0) > 1) ||
1751 (drm_format_info_block_height(fb
->format
, 0) > 1))
1755 * Changes struct fb_var_screeninfo are currently not pushed back
1756 * to KMS, hence fail if different settings are requested.
1758 if (var
->bits_per_pixel
!= fb
->format
->cpp
[0] * 8 ||
1759 var
->xres
> fb
->width
|| var
->yres
> fb
->height
||
1760 var
->xres_virtual
> fb
->width
|| var
->yres_virtual
> fb
->height
) {
1761 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
1762 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1763 var
->xres
, var
->yres
, var
->bits_per_pixel
,
1764 var
->xres_virtual
, var
->yres_virtual
,
1765 fb
->width
, fb
->height
, fb
->format
->cpp
[0] * 8);
1770 * Workaround for SDL 1.2, which is known to be setting all pixel format
1771 * fields values to zero in some cases. We treat this situation as a
1772 * kind of "use some reasonable autodetected values".
1774 if (!var
->red
.offset
&& !var
->green
.offset
&&
1775 !var
->blue
.offset
&& !var
->transp
.offset
&&
1776 !var
->red
.length
&& !var
->green
.length
&&
1777 !var
->blue
.length
&& !var
->transp
.length
&&
1778 !var
->red
.msb_right
&& !var
->green
.msb_right
&&
1779 !var
->blue
.msb_right
&& !var
->transp
.msb_right
) {
1780 drm_fb_helper_fill_pixel_fmt(var
, fb
->format
->depth
);
1784 * drm fbdev emulation doesn't support changing the pixel format at all,
1785 * so reject all pixel format changing requests.
1787 if (!drm_fb_pixel_format_equal(var
, &info
->var
)) {
1788 DRM_DEBUG("fbdev emulation doesn't support changing the pixel format\n");
1794 EXPORT_SYMBOL(drm_fb_helper_check_var
);
1797 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
1798 * @info: fbdev registered by the helper
1800 * This will let fbcon do the mode init and is called at initialization time by
1801 * the fbdev core when registering the driver, and later on through the hotplug
1804 int drm_fb_helper_set_par(struct fb_info
*info
)
1806 struct drm_fb_helper
*fb_helper
= info
->par
;
1807 struct fb_var_screeninfo
*var
= &info
->var
;
1809 if (oops_in_progress
)
1812 if (var
->pixclock
!= 0) {
1813 DRM_ERROR("PIXEL CLOCK SET\n");
1817 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper
);
1821 EXPORT_SYMBOL(drm_fb_helper_set_par
);
1823 static void pan_set(struct drm_fb_helper
*fb_helper
, int x
, int y
)
1827 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1828 struct drm_mode_set
*mode_set
;
1830 mode_set
= &fb_helper
->crtc_info
[i
].mode_set
;
1837 static int pan_display_atomic(struct fb_var_screeninfo
*var
,
1838 struct fb_info
*info
)
1840 struct drm_fb_helper
*fb_helper
= info
->par
;
1843 pan_set(fb_helper
, var
->xoffset
, var
->yoffset
);
1845 ret
= restore_fbdev_mode_atomic(fb_helper
, true);
1847 info
->var
.xoffset
= var
->xoffset
;
1848 info
->var
.yoffset
= var
->yoffset
;
1850 pan_set(fb_helper
, info
->var
.xoffset
, info
->var
.yoffset
);
1855 static int pan_display_legacy(struct fb_var_screeninfo
*var
,
1856 struct fb_info
*info
)
1858 struct drm_fb_helper
*fb_helper
= info
->par
;
1859 struct drm_mode_set
*modeset
;
1863 drm_modeset_lock_all(fb_helper
->dev
);
1864 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1865 modeset
= &fb_helper
->crtc_info
[i
].mode_set
;
1867 modeset
->x
= var
->xoffset
;
1868 modeset
->y
= var
->yoffset
;
1870 if (modeset
->num_connectors
) {
1871 ret
= drm_mode_set_config_internal(modeset
);
1873 info
->var
.xoffset
= var
->xoffset
;
1874 info
->var
.yoffset
= var
->yoffset
;
1878 drm_modeset_unlock_all(fb_helper
->dev
);
1884 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1885 * @var: updated screen information
1886 * @info: fbdev registered by the helper
1888 int drm_fb_helper_pan_display(struct fb_var_screeninfo
*var
,
1889 struct fb_info
*info
)
1891 struct drm_fb_helper
*fb_helper
= info
->par
;
1892 struct drm_device
*dev
= fb_helper
->dev
;
1895 if (oops_in_progress
)
1898 mutex_lock(&fb_helper
->lock
);
1899 if (!drm_master_internal_acquire(dev
)) {
1904 if (drm_drv_uses_atomic_modeset(dev
))
1905 ret
= pan_display_atomic(var
, info
);
1907 ret
= pan_display_legacy(var
, info
);
1909 drm_master_internal_release(dev
);
1911 mutex_unlock(&fb_helper
->lock
);
1915 EXPORT_SYMBOL(drm_fb_helper_pan_display
);
1918 * Allocates the backing storage and sets up the fbdev info structure through
1919 * the ->fb_probe callback.
1921 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper
*fb_helper
,
1927 struct drm_fb_helper_surface_size sizes
;
1930 memset(&sizes
, 0, sizeof(struct drm_fb_helper_surface_size
));
1931 sizes
.surface_depth
= 24;
1932 sizes
.surface_bpp
= 32;
1933 sizes
.fb_width
= (u32
)-1;
1934 sizes
.fb_height
= (u32
)-1;
1937 * If driver picks 8 or 16 by default use that for both depth/bpp
1940 if (preferred_bpp
!= sizes
.surface_bpp
)
1941 sizes
.surface_depth
= sizes
.surface_bpp
= preferred_bpp
;
1943 drm_fb_helper_for_each_connector(fb_helper
, i
) {
1944 struct drm_fb_helper_connector
*fb_helper_conn
= fb_helper
->connector_info
[i
];
1945 struct drm_cmdline_mode
*cmdline_mode
;
1947 cmdline_mode
= &fb_helper_conn
->connector
->cmdline_mode
;
1949 if (cmdline_mode
->bpp_specified
) {
1950 switch (cmdline_mode
->bpp
) {
1952 sizes
.surface_depth
= sizes
.surface_bpp
= 8;
1955 sizes
.surface_depth
= 15;
1956 sizes
.surface_bpp
= 16;
1959 sizes
.surface_depth
= sizes
.surface_bpp
= 16;
1962 sizes
.surface_depth
= sizes
.surface_bpp
= 24;
1965 sizes
.surface_depth
= 24;
1966 sizes
.surface_bpp
= 32;
1974 * If we run into a situation where, for example, the primary plane
1975 * supports RGBA5551 (16 bpp, depth 15) but not RGB565 (16 bpp, depth
1976 * 16) we need to scale down the depth of the sizes we request.
1978 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1979 struct drm_mode_set
*mode_set
= &fb_helper
->crtc_info
[i
].mode_set
;
1980 struct drm_crtc
*crtc
= mode_set
->crtc
;
1981 struct drm_plane
*plane
= crtc
->primary
;
1984 DRM_DEBUG("test CRTC %d primary plane\n", i
);
1986 for (j
= 0; j
< plane
->format_count
; j
++) {
1987 const struct drm_format_info
*fmt
;
1989 fmt
= drm_format_info(plane
->format_types
[j
]);
1992 * Do not consider YUV or other complicated formats
1993 * for framebuffers. This means only legacy formats
1994 * are supported (fmt->depth is a legacy field) but
1995 * the framebuffer emulation can only deal with such
1996 * formats, specifically RGB/BGA formats.
1998 if (fmt
->depth
== 0)
2001 /* We found a perfect fit, great */
2002 if (fmt
->depth
== sizes
.surface_depth
) {
2003 best_depth
= fmt
->depth
;
2007 /* Skip depths above what we're looking for */
2008 if (fmt
->depth
> sizes
.surface_depth
)
2011 /* Best depth found so far */
2012 if (fmt
->depth
> best_depth
)
2013 best_depth
= fmt
->depth
;
2016 if (sizes
.surface_depth
!= best_depth
&& best_depth
) {
2017 DRM_INFO("requested bpp %d, scaled depth down to %d",
2018 sizes
.surface_bpp
, best_depth
);
2019 sizes
.surface_depth
= best_depth
;
2022 /* first up get a count of crtcs now in use and new min/maxes width/heights */
2024 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
2025 struct drm_display_mode
*desired_mode
;
2026 struct drm_mode_set
*mode_set
;
2028 /* in case of tile group, are we the last tile vert or horiz?
2029 * If no tile group you are always the last one both vertically
2032 bool lastv
= true, lasth
= true;
2034 mode_set
= &fb_helper
->crtc_info
[i
].mode_set
;
2035 desired_mode
= mode_set
->mode
;
2045 sizes
.surface_width
= max_t(u32
, desired_mode
->hdisplay
+ x
, sizes
.surface_width
);
2046 sizes
.surface_height
= max_t(u32
, desired_mode
->vdisplay
+ y
, sizes
.surface_height
);
2048 for (j
= 0; j
< mode_set
->num_connectors
; j
++) {
2049 struct drm_connector
*connector
= mode_set
->connectors
[j
];
2051 if (connector
->has_tile
) {
2052 lasth
= (connector
->tile_h_loc
== (connector
->num_h_tile
- 1));
2053 lastv
= (connector
->tile_v_loc
== (connector
->num_v_tile
- 1));
2054 /* cloning to multiple tiles is just crazy-talk, so: */
2060 sizes
.fb_width
= min_t(u32
, desired_mode
->hdisplay
+ x
, sizes
.fb_width
);
2062 sizes
.fb_height
= min_t(u32
, desired_mode
->vdisplay
+ y
, sizes
.fb_height
);
2065 if (crtc_count
== 0 || sizes
.fb_width
== -1 || sizes
.fb_height
== -1) {
2066 DRM_INFO("Cannot find any crtc or sizes\n");
2068 /* First time: disable all crtc's.. */
2069 if (!fb_helper
->deferred_setup
)
2070 restore_fbdev_mode(fb_helper
);
2074 /* Handle our overallocation */
2075 sizes
.surface_height
*= drm_fbdev_overalloc
;
2076 sizes
.surface_height
/= 100;
2078 /* push down into drivers */
2079 ret
= (*fb_helper
->funcs
->fb_probe
)(fb_helper
, &sizes
);
2083 strcpy(fb_helper
->fb
->comm
, "[fbcon]");
2087 static void drm_fb_helper_fill_fix(struct fb_info
*info
, uint32_t pitch
,
2090 info
->fix
.type
= FB_TYPE_PACKED_PIXELS
;
2091 info
->fix
.visual
= depth
== 8 ? FB_VISUAL_PSEUDOCOLOR
:
2092 FB_VISUAL_TRUECOLOR
;
2093 info
->fix
.mmio_start
= 0;
2094 info
->fix
.mmio_len
= 0;
2095 info
->fix
.type_aux
= 0;
2096 info
->fix
.xpanstep
= 1; /* doing it in hw */
2097 info
->fix
.ypanstep
= 1; /* doing it in hw */
2098 info
->fix
.ywrapstep
= 0;
2099 info
->fix
.accel
= FB_ACCEL_NONE
;
2101 info
->fix
.line_length
= pitch
;
2104 static void drm_fb_helper_fill_var(struct fb_info
*info
,
2105 struct drm_fb_helper
*fb_helper
,
2106 uint32_t fb_width
, uint32_t fb_height
)
2108 struct drm_framebuffer
*fb
= fb_helper
->fb
;
2110 WARN_ON((drm_format_info_block_width(fb
->format
, 0) > 1) ||
2111 (drm_format_info_block_height(fb
->format
, 0) > 1));
2112 info
->pseudo_palette
= fb_helper
->pseudo_palette
;
2113 info
->var
.xres_virtual
= fb
->width
;
2114 info
->var
.yres_virtual
= fb
->height
;
2115 info
->var
.bits_per_pixel
= fb
->format
->cpp
[0] * 8;
2116 info
->var
.accel_flags
= FB_ACCELF_TEXT
;
2117 info
->var
.xoffset
= 0;
2118 info
->var
.yoffset
= 0;
2119 info
->var
.activate
= FB_ACTIVATE_NOW
;
2121 drm_fb_helper_fill_pixel_fmt(&info
->var
, fb
->format
->depth
);
2123 info
->var
.xres
= fb_width
;
2124 info
->var
.yres
= fb_height
;
2128 * drm_fb_helper_fill_info - initializes fbdev information
2129 * @info: fbdev instance to set up
2130 * @fb_helper: fb helper instance to use as template
2131 * @sizes: describes fbdev size and scanout surface size
2133 * Sets up the variable and fixed fbdev metainformation from the given fb helper
2134 * instance and the drm framebuffer allocated in &drm_fb_helper.fb.
2136 * Drivers should call this (or their equivalent setup code) from their
2137 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
2138 * backing storage framebuffer.
2140 void drm_fb_helper_fill_info(struct fb_info
*info
,
2141 struct drm_fb_helper
*fb_helper
,
2142 struct drm_fb_helper_surface_size
*sizes
)
2144 struct drm_framebuffer
*fb
= fb_helper
->fb
;
2146 drm_fb_helper_fill_fix(info
, fb
->pitches
[0], fb
->format
->depth
);
2147 drm_fb_helper_fill_var(info
, fb_helper
,
2148 sizes
->fb_width
, sizes
->fb_height
);
2150 info
->par
= fb_helper
;
2151 snprintf(info
->fix
.id
, sizeof(info
->fix
.id
), "%sdrmfb",
2152 fb_helper
->dev
->driver
->name
);
2155 EXPORT_SYMBOL(drm_fb_helper_fill_info
);
2157 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper
*fb_helper
,
2161 struct drm_connector
*connector
;
2164 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2165 connector
= fb_helper
->connector_info
[i
]->connector
;
2166 count
+= connector
->funcs
->fill_modes(connector
, maxX
, maxY
);
2172 struct drm_display_mode
*drm_has_preferred_mode(struct drm_fb_helper_connector
*fb_connector
, int width
, int height
)
2174 struct drm_display_mode
*mode
;
2176 list_for_each_entry(mode
, &fb_connector
->connector
->modes
, head
) {
2177 if (mode
->hdisplay
> width
||
2178 mode
->vdisplay
> height
)
2180 if (mode
->type
& DRM_MODE_TYPE_PREFERRED
)
2185 EXPORT_SYMBOL(drm_has_preferred_mode
);
2187 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector
*fb_connector
)
2189 return fb_connector
->connector
->cmdline_mode
.specified
;
2192 struct drm_display_mode
*drm_pick_cmdline_mode(struct drm_fb_helper_connector
*fb_helper_conn
)
2194 struct drm_cmdline_mode
*cmdline_mode
;
2195 struct drm_display_mode
*mode
;
2196 bool prefer_non_interlace
;
2198 cmdline_mode
= &fb_helper_conn
->connector
->cmdline_mode
;
2199 if (cmdline_mode
->specified
== false)
2202 /* attempt to find a matching mode in the list of modes
2203 * we have gotten so far, if not add a CVT mode that conforms
2205 if (cmdline_mode
->rb
|| cmdline_mode
->margins
)
2208 prefer_non_interlace
= !cmdline_mode
->interlace
;
2210 list_for_each_entry(mode
, &fb_helper_conn
->connector
->modes
, head
) {
2211 /* check width/height */
2212 if (mode
->hdisplay
!= cmdline_mode
->xres
||
2213 mode
->vdisplay
!= cmdline_mode
->yres
)
2216 if (cmdline_mode
->refresh_specified
) {
2217 if (mode
->vrefresh
!= cmdline_mode
->refresh
)
2221 if (cmdline_mode
->interlace
) {
2222 if (!(mode
->flags
& DRM_MODE_FLAG_INTERLACE
))
2224 } else if (prefer_non_interlace
) {
2225 if (mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
2231 if (prefer_non_interlace
) {
2232 prefer_non_interlace
= false;
2237 mode
= drm_mode_create_from_cmdline_mode(fb_helper_conn
->connector
->dev
,
2239 list_add(&mode
->head
, &fb_helper_conn
->connector
->modes
);
2242 EXPORT_SYMBOL(drm_pick_cmdline_mode
);
2244 static bool drm_connector_enabled(struct drm_connector
*connector
, bool strict
)
2248 if (connector
->display_info
.non_desktop
)
2252 enable
= connector
->status
== connector_status_connected
;
2254 enable
= connector
->status
!= connector_status_disconnected
;
2259 static void drm_enable_connectors(struct drm_fb_helper
*fb_helper
,
2262 bool any_enabled
= false;
2263 struct drm_connector
*connector
;
2266 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2267 connector
= fb_helper
->connector_info
[i
]->connector
;
2268 enabled
[i
] = drm_connector_enabled(connector
, true);
2269 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector
->base
.id
,
2270 connector
->display_info
.non_desktop
? "non desktop" : enabled
[i
] ? "yes" : "no");
2272 any_enabled
|= enabled
[i
];
2278 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2279 connector
= fb_helper
->connector_info
[i
]->connector
;
2280 enabled
[i
] = drm_connector_enabled(connector
, false);
2284 static bool drm_target_cloned(struct drm_fb_helper
*fb_helper
,
2285 struct drm_display_mode
**modes
,
2286 struct drm_fb_offset
*offsets
,
2287 bool *enabled
, int width
, int height
)
2290 bool can_clone
= false;
2291 struct drm_fb_helper_connector
*fb_helper_conn
;
2292 struct drm_display_mode
*dmt_mode
, *mode
;
2294 /* only contemplate cloning in the single crtc case */
2295 if (fb_helper
->crtc_count
> 1)
2299 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2304 /* only contemplate cloning if more than one connector is enabled */
2308 /* check the command line or if nothing common pick 1024x768 */
2310 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2313 fb_helper_conn
= fb_helper
->connector_info
[i
];
2314 modes
[i
] = drm_pick_cmdline_mode(fb_helper_conn
);
2319 for (j
= 0; j
< i
; j
++) {
2322 if (!drm_mode_match(modes
[j
], modes
[i
],
2323 DRM_MODE_MATCH_TIMINGS
|
2324 DRM_MODE_MATCH_CLOCK
|
2325 DRM_MODE_MATCH_FLAGS
|
2326 DRM_MODE_MATCH_3D_FLAGS
))
2332 DRM_DEBUG_KMS("can clone using command line\n");
2336 /* try and find a 1024x768 mode on each connector */
2338 dmt_mode
= drm_mode_find_dmt(fb_helper
->dev
, 1024, 768, 60, false);
2340 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2344 fb_helper_conn
= fb_helper
->connector_info
[i
];
2345 list_for_each_entry(mode
, &fb_helper_conn
->connector
->modes
, head
) {
2346 if (drm_mode_match(mode
, dmt_mode
,
2347 DRM_MODE_MATCH_TIMINGS
|
2348 DRM_MODE_MATCH_CLOCK
|
2349 DRM_MODE_MATCH_FLAGS
|
2350 DRM_MODE_MATCH_3D_FLAGS
))
2358 DRM_DEBUG_KMS("can clone using 1024x768\n");
2361 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
2365 static int drm_get_tile_offsets(struct drm_fb_helper
*fb_helper
,
2366 struct drm_display_mode
**modes
,
2367 struct drm_fb_offset
*offsets
,
2369 int h_idx
, int v_idx
)
2371 struct drm_fb_helper_connector
*fb_helper_conn
;
2373 int hoffset
= 0, voffset
= 0;
2375 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2376 fb_helper_conn
= fb_helper
->connector_info
[i
];
2377 if (!fb_helper_conn
->connector
->has_tile
)
2380 if (!modes
[i
] && (h_idx
|| v_idx
)) {
2381 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i
,
2382 fb_helper_conn
->connector
->base
.id
);
2385 if (fb_helper_conn
->connector
->tile_h_loc
< h_idx
)
2386 hoffset
+= modes
[i
]->hdisplay
;
2388 if (fb_helper_conn
->connector
->tile_v_loc
< v_idx
)
2389 voffset
+= modes
[i
]->vdisplay
;
2391 offsets
[idx
].x
= hoffset
;
2392 offsets
[idx
].y
= voffset
;
2393 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset
, voffset
, h_idx
, v_idx
);
2397 static bool drm_target_preferred(struct drm_fb_helper
*fb_helper
,
2398 struct drm_display_mode
**modes
,
2399 struct drm_fb_offset
*offsets
,
2400 bool *enabled
, int width
, int height
)
2402 struct drm_fb_helper_connector
*fb_helper_conn
;
2403 const u64 mask
= BIT_ULL(fb_helper
->connector_count
) - 1;
2404 u64 conn_configured
= 0;
2409 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2410 fb_helper_conn
= fb_helper
->connector_info
[i
];
2412 if (conn_configured
& BIT_ULL(i
))
2415 if (enabled
[i
] == false) {
2416 conn_configured
|= BIT_ULL(i
);
2420 /* first pass over all the untiled connectors */
2421 if (tile_pass
== 0 && fb_helper_conn
->connector
->has_tile
)
2424 if (tile_pass
== 1) {
2425 if (fb_helper_conn
->connector
->tile_h_loc
!= 0 ||
2426 fb_helper_conn
->connector
->tile_v_loc
!= 0)
2430 if (fb_helper_conn
->connector
->tile_h_loc
!= tile_pass
- 1 &&
2431 fb_helper_conn
->connector
->tile_v_loc
!= tile_pass
- 1)
2432 /* if this tile_pass doesn't cover any of the tiles - keep going */
2436 * find the tile offsets for this pass - need to find
2437 * all tiles left and above
2439 drm_get_tile_offsets(fb_helper
, modes
, offsets
,
2440 i
, fb_helper_conn
->connector
->tile_h_loc
, fb_helper_conn
->connector
->tile_v_loc
);
2442 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
2443 fb_helper_conn
->connector
->base
.id
);
2445 /* got for command line mode first */
2446 modes
[i
] = drm_pick_cmdline_mode(fb_helper_conn
);
2448 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
2449 fb_helper_conn
->connector
->base
.id
, fb_helper_conn
->connector
->tile_group
? fb_helper_conn
->connector
->tile_group
->id
: 0);
2450 modes
[i
] = drm_has_preferred_mode(fb_helper_conn
, width
, height
);
2452 /* No preferred modes, pick one off the list */
2453 if (!modes
[i
] && !list_empty(&fb_helper_conn
->connector
->modes
)) {
2454 list_for_each_entry(modes
[i
], &fb_helper_conn
->connector
->modes
, head
)
2457 DRM_DEBUG_KMS("found mode %s\n", modes
[i
] ? modes
[i
]->name
:
2459 conn_configured
|= BIT_ULL(i
);
2462 if ((conn_configured
& mask
) != mask
) {
2469 static bool connector_has_possible_crtc(struct drm_connector
*connector
,
2470 struct drm_crtc
*crtc
)
2472 struct drm_encoder
*encoder
;
2475 drm_connector_for_each_possible_encoder(connector
, encoder
, i
) {
2476 if (encoder
->possible_crtcs
& drm_crtc_mask(crtc
))
2483 static int drm_pick_crtcs(struct drm_fb_helper
*fb_helper
,
2484 struct drm_fb_helper_crtc
**best_crtcs
,
2485 struct drm_display_mode
**modes
,
2486 int n
, int width
, int height
)
2489 struct drm_connector
*connector
;
2490 int my_score
, best_score
, score
;
2491 struct drm_fb_helper_crtc
**crtcs
, *crtc
;
2492 struct drm_fb_helper_connector
*fb_helper_conn
;
2494 if (n
== fb_helper
->connector_count
)
2497 fb_helper_conn
= fb_helper
->connector_info
[n
];
2498 connector
= fb_helper_conn
->connector
;
2500 best_crtcs
[n
] = NULL
;
2501 best_score
= drm_pick_crtcs(fb_helper
, best_crtcs
, modes
, n
+1, width
, height
);
2502 if (modes
[n
] == NULL
)
2505 crtcs
= kcalloc(fb_helper
->connector_count
,
2506 sizeof(struct drm_fb_helper_crtc
*), GFP_KERNEL
);
2511 if (connector
->status
== connector_status_connected
)
2513 if (drm_has_cmdline_mode(fb_helper_conn
))
2515 if (drm_has_preferred_mode(fb_helper_conn
, width
, height
))
2519 * select a crtc for this connector and then attempt to configure
2520 * remaining connectors
2522 for (c
= 0; c
< fb_helper
->crtc_count
; c
++) {
2523 crtc
= &fb_helper
->crtc_info
[c
];
2525 if (!connector_has_possible_crtc(connector
,
2526 crtc
->mode_set
.crtc
))
2529 for (o
= 0; o
< n
; o
++)
2530 if (best_crtcs
[o
] == crtc
)
2534 /* ignore cloning unless only a single crtc */
2535 if (fb_helper
->crtc_count
> 1)
2538 if (!drm_mode_equal(modes
[o
], modes
[n
]))
2543 memcpy(crtcs
, best_crtcs
, n
* sizeof(struct drm_fb_helper_crtc
*));
2544 score
= my_score
+ drm_pick_crtcs(fb_helper
, crtcs
, modes
, n
+ 1,
2546 if (score
> best_score
) {
2548 memcpy(best_crtcs
, crtcs
,
2549 fb_helper
->connector_count
*
2550 sizeof(struct drm_fb_helper_crtc
*));
2558 static struct drm_fb_helper_crtc
*
2559 drm_fb_helper_crtc(struct drm_fb_helper
*fb_helper
, struct drm_crtc
*crtc
)
2563 for (i
= 0; i
< fb_helper
->crtc_count
; i
++)
2564 if (fb_helper
->crtc_info
[i
].mode_set
.crtc
== crtc
)
2565 return &fb_helper
->crtc_info
[i
];
2570 /* Try to read the BIOS display configuration and use it for the initial config */
2571 static bool drm_fb_helper_firmware_config(struct drm_fb_helper
*fb_helper
,
2572 struct drm_fb_helper_crtc
**crtcs
,
2573 struct drm_display_mode
**modes
,
2574 struct drm_fb_offset
*offsets
,
2575 bool *enabled
, int width
, int height
)
2577 struct drm_device
*dev
= fb_helper
->dev
;
2578 unsigned int count
= min(fb_helper
->connector_count
, BITS_PER_LONG
);
2579 unsigned long conn_configured
, conn_seq
, mask
;
2582 bool fallback
= true, ret
= true;
2583 int num_connectors_enabled
= 0;
2584 int num_connectors_detected
= 0;
2585 struct drm_modeset_acquire_ctx ctx
;
2587 if (!drm_drv_uses_atomic_modeset(dev
))
2590 save_enabled
= kcalloc(count
, sizeof(bool), GFP_KERNEL
);
2594 drm_modeset_acquire_init(&ctx
, 0);
2596 while (drm_modeset_lock_all_ctx(dev
, &ctx
) != 0)
2597 drm_modeset_backoff(&ctx
);
2599 memcpy(save_enabled
, enabled
, count
);
2600 mask
= GENMASK(count
- 1, 0);
2601 conn_configured
= 0;
2603 conn_seq
= conn_configured
;
2604 for (i
= 0; i
< count
; i
++) {
2605 struct drm_fb_helper_connector
*fb_conn
;
2606 struct drm_connector
*connector
;
2607 struct drm_encoder
*encoder
;
2608 struct drm_fb_helper_crtc
*new_crtc
;
2610 fb_conn
= fb_helper
->connector_info
[i
];
2611 connector
= fb_conn
->connector
;
2613 if (conn_configured
& BIT(i
))
2616 if (conn_seq
== 0 && !connector
->has_tile
)
2619 if (connector
->status
== connector_status_connected
)
2620 num_connectors_detected
++;
2623 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
2625 conn_configured
|= BIT(i
);
2629 if (connector
->force
== DRM_FORCE_OFF
) {
2630 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
2636 encoder
= connector
->state
->best_encoder
;
2637 if (!encoder
|| WARN_ON(!connector
->state
->crtc
)) {
2638 if (connector
->force
> DRM_FORCE_OFF
)
2641 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
2644 conn_configured
|= BIT(i
);
2648 num_connectors_enabled
++;
2650 new_crtc
= drm_fb_helper_crtc(fb_helper
, connector
->state
->crtc
);
2653 * Make sure we're not trying to drive multiple connectors
2654 * with a single CRTC, since our cloning support may not
2657 for (j
= 0; j
< count
; j
++) {
2658 if (crtcs
[j
] == new_crtc
) {
2659 DRM_DEBUG_KMS("fallback: cloned configuration\n");
2664 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
2667 /* go for command line mode first */
2668 modes
[i
] = drm_pick_cmdline_mode(fb_conn
);
2670 /* try for preferred next */
2672 DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
2673 connector
->name
, connector
->has_tile
);
2674 modes
[i
] = drm_has_preferred_mode(fb_conn
, width
,
2678 /* No preferred mode marked by the EDID? Are there any modes? */
2679 if (!modes
[i
] && !list_empty(&connector
->modes
)) {
2680 DRM_DEBUG_KMS("using first mode listed on connector %s\n",
2682 modes
[i
] = list_first_entry(&connector
->modes
,
2683 struct drm_display_mode
,
2687 /* last resort: use current mode */
2690 * IMPORTANT: We want to use the adjusted mode (i.e.
2691 * after the panel fitter upscaling) as the initial
2692 * config, not the input mode, which is what crtc->mode
2693 * usually contains. But since our current
2694 * code puts a mode derived from the post-pfit timings
2695 * into crtc->mode this works out correctly.
2697 * This is crtc->mode and not crtc->state->mode for the
2698 * fastboot check to work correctly.
2700 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
2702 modes
[i
] = &connector
->state
->crtc
->mode
;
2704 crtcs
[i
] = new_crtc
;
2706 DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
2708 connector
->state
->crtc
->base
.id
,
2709 connector
->state
->crtc
->name
,
2710 modes
[i
]->hdisplay
, modes
[i
]->vdisplay
,
2711 modes
[i
]->flags
& DRM_MODE_FLAG_INTERLACE
? "i" : "");
2714 conn_configured
|= BIT(i
);
2717 if ((conn_configured
& mask
) != mask
&& conn_configured
!= conn_seq
)
2721 * If the BIOS didn't enable everything it could, fall back to have the
2722 * same user experiencing of lighting up as much as possible like the
2723 * fbdev helper library.
2725 if (num_connectors_enabled
!= num_connectors_detected
&&
2726 num_connectors_enabled
< dev
->mode_config
.num_crtc
) {
2727 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
2728 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled
,
2729 num_connectors_detected
);
2735 DRM_DEBUG_KMS("Not using firmware configuration\n");
2736 memcpy(enabled
, save_enabled
, count
);
2740 drm_modeset_drop_locks(&ctx
);
2741 drm_modeset_acquire_fini(&ctx
);
2743 kfree(save_enabled
);
2747 static void drm_setup_crtcs(struct drm_fb_helper
*fb_helper
,
2748 u32 width
, u32 height
)
2750 struct drm_device
*dev
= fb_helper
->dev
;
2751 struct drm_fb_helper_crtc
**crtcs
;
2752 struct drm_display_mode
**modes
;
2753 struct drm_fb_offset
*offsets
;
2757 DRM_DEBUG_KMS("\n");
2758 /* prevent concurrent modification of connector_count by hotplug */
2759 lockdep_assert_held(&fb_helper
->lock
);
2761 crtcs
= kcalloc(fb_helper
->connector_count
,
2762 sizeof(struct drm_fb_helper_crtc
*), GFP_KERNEL
);
2763 modes
= kcalloc(fb_helper
->connector_count
,
2764 sizeof(struct drm_display_mode
*), GFP_KERNEL
);
2765 offsets
= kcalloc(fb_helper
->connector_count
,
2766 sizeof(struct drm_fb_offset
), GFP_KERNEL
);
2767 enabled
= kcalloc(fb_helper
->connector_count
,
2768 sizeof(bool), GFP_KERNEL
);
2769 if (!crtcs
|| !modes
|| !enabled
|| !offsets
) {
2770 DRM_ERROR("Memory allocation failed\n");
2774 mutex_lock(&fb_helper
->dev
->mode_config
.mutex
);
2775 if (drm_fb_helper_probe_connector_modes(fb_helper
, width
, height
) == 0)
2776 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
2777 drm_enable_connectors(fb_helper
, enabled
);
2779 if (!drm_fb_helper_firmware_config(fb_helper
, crtcs
, modes
, offsets
,
2780 enabled
, width
, height
)) {
2781 memset(modes
, 0, fb_helper
->connector_count
*sizeof(modes
[0]));
2782 memset(crtcs
, 0, fb_helper
->connector_count
*sizeof(crtcs
[0]));
2783 memset(offsets
, 0, fb_helper
->connector_count
*sizeof(offsets
[0]));
2785 if (!drm_target_cloned(fb_helper
, modes
, offsets
,
2786 enabled
, width
, height
) &&
2787 !drm_target_preferred(fb_helper
, modes
, offsets
,
2788 enabled
, width
, height
))
2789 DRM_ERROR("Unable to find initial modes\n");
2791 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2794 drm_pick_crtcs(fb_helper
, crtcs
, modes
, 0, width
, height
);
2796 mutex_unlock(&fb_helper
->dev
->mode_config
.mutex
);
2798 /* need to set the modesets up here for use later */
2799 /* fill out the connector<->crtc mappings into the modesets */
2800 for (i
= 0; i
< fb_helper
->crtc_count
; i
++)
2801 drm_fb_helper_modeset_release(fb_helper
,
2802 &fb_helper
->crtc_info
[i
].mode_set
);
2804 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2805 struct drm_display_mode
*mode
= modes
[i
];
2806 struct drm_fb_helper_crtc
*fb_crtc
= crtcs
[i
];
2807 struct drm_fb_offset
*offset
= &offsets
[i
];
2809 if (mode
&& fb_crtc
) {
2810 struct drm_mode_set
*modeset
= &fb_crtc
->mode_set
;
2811 struct drm_connector
*connector
=
2812 fb_helper
->connector_info
[i
]->connector
;
2814 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2815 mode
->name
, fb_crtc
->mode_set
.crtc
->base
.id
, offset
->x
, offset
->y
);
2817 modeset
->mode
= drm_mode_duplicate(dev
, mode
);
2818 drm_connector_get(connector
);
2819 modeset
->connectors
[modeset
->num_connectors
++] = connector
;
2820 modeset
->x
= offset
->x
;
2821 modeset
->y
= offset
->y
;
2832 * This is a continuation of drm_setup_crtcs() that sets up anything related
2833 * to the framebuffer. During initialization, drm_setup_crtcs() is called before
2834 * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev).
2835 * So, any setup that touches those fields needs to be done here instead of in
2836 * drm_setup_crtcs().
2838 static void drm_setup_crtcs_fb(struct drm_fb_helper
*fb_helper
)
2840 struct fb_info
*info
= fb_helper
->fbdev
;
2841 unsigned int rotation
, sw_rotations
= 0;
2844 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
2845 struct drm_mode_set
*modeset
= &fb_helper
->crtc_info
[i
].mode_set
;
2847 if (!modeset
->num_connectors
)
2850 modeset
->fb
= fb_helper
->fb
;
2852 if (drm_fb_helper_panel_rotation(modeset
, &rotation
))
2853 /* Rotating in hardware, fbcon should not rotate */
2854 sw_rotations
|= DRM_MODE_ROTATE_0
;
2856 sw_rotations
|= rotation
;
2859 mutex_lock(&fb_helper
->dev
->mode_config
.mutex
);
2860 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2861 struct drm_connector
*connector
=
2862 fb_helper
->connector_info
[i
]->connector
;
2864 /* use first connected connector for the physical dimensions */
2865 if (connector
->status
== connector_status_connected
) {
2866 info
->var
.width
= connector
->display_info
.width_mm
;
2867 info
->var
.height
= connector
->display_info
.height_mm
;
2871 mutex_unlock(&fb_helper
->dev
->mode_config
.mutex
);
2873 switch (sw_rotations
) {
2874 case DRM_MODE_ROTATE_0
:
2875 info
->fbcon_rotate_hint
= FB_ROTATE_UR
;
2877 case DRM_MODE_ROTATE_90
:
2878 info
->fbcon_rotate_hint
= FB_ROTATE_CCW
;
2880 case DRM_MODE_ROTATE_180
:
2881 info
->fbcon_rotate_hint
= FB_ROTATE_UD
;
2883 case DRM_MODE_ROTATE_270
:
2884 info
->fbcon_rotate_hint
= FB_ROTATE_CW
;
2888 * Multiple bits are set / multiple rotations requested
2889 * fbcon cannot handle separate rotation settings per
2890 * output, so fallback to unrotated.
2892 info
->fbcon_rotate_hint
= FB_ROTATE_UR
;
2896 /* Note: Drops fb_helper->lock before returning. */
2898 __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper
*fb_helper
,
2901 struct drm_device
*dev
= fb_helper
->dev
;
2902 struct fb_info
*info
;
2903 unsigned int width
, height
;
2906 width
= dev
->mode_config
.max_width
;
2907 height
= dev
->mode_config
.max_height
;
2909 drm_setup_crtcs(fb_helper
, width
, height
);
2910 ret
= drm_fb_helper_single_fb_probe(fb_helper
, bpp_sel
);
2912 if (ret
== -EAGAIN
) {
2913 fb_helper
->preferred_bpp
= bpp_sel
;
2914 fb_helper
->deferred_setup
= true;
2917 mutex_unlock(&fb_helper
->lock
);
2921 drm_setup_crtcs_fb(fb_helper
);
2923 fb_helper
->deferred_setup
= false;
2925 info
= fb_helper
->fbdev
;
2926 info
->var
.pixclock
= 0;
2927 /* Shamelessly allow physical address leaking to userspace */
2928 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
2929 if (!drm_leak_fbdev_smem
)
2931 /* don't leak any physical addresses to userspace */
2932 info
->flags
|= FBINFO_HIDE_SMEM_START
;
2934 /* Need to drop locks to avoid recursive deadlock in
2935 * register_framebuffer. This is ok because the only thing left to do is
2936 * register the fbdev emulation instance in kernel_fb_helper_list. */
2937 mutex_unlock(&fb_helper
->lock
);
2939 ret
= register_framebuffer(info
);
2943 dev_info(dev
->dev
, "fb%d: %s frame buffer device\n",
2944 info
->node
, info
->fix
.id
);
2946 mutex_lock(&kernel_fb_helper_lock
);
2947 if (list_empty(&kernel_fb_helper_list
))
2948 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op
);
2950 list_add(&fb_helper
->kernel_fb_list
, &kernel_fb_helper_list
);
2951 mutex_unlock(&kernel_fb_helper_lock
);
2957 * drm_fb_helper_initial_config - setup a sane initial connector configuration
2958 * @fb_helper: fb_helper device struct
2959 * @bpp_sel: bpp value to use for the framebuffer configuration
2961 * Scans the CRTCs and connectors and tries to put together an initial setup.
2962 * At the moment, this is a cloned configuration across all heads with
2963 * a new framebuffer object as the backing store.
2965 * Note that this also registers the fbdev and so allows userspace to call into
2966 * the driver through the fbdev interfaces.
2968 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
2969 * to let the driver allocate and initialize the fbdev info structure and the
2970 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_info() is provided
2971 * as a helper to setup simple default values for the fbdev info structure.
2975 * When you have fbcon support built-in or already loaded, this function will do
2976 * a full modeset to setup the fbdev console. Due to locking misdesign in the
2977 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2978 * console_lock. Until console_unlock is called no dmesg lines will be sent out
2979 * to consoles, not even serial console. This means when your driver crashes,
2980 * you will see absolutely nothing else but a system stuck in this function,
2981 * with no further output. Any kind of printk() you place within your own driver
2982 * or in the drm core modeset code will also never show up.
2984 * Standard debug practice is to run the fbcon setup without taking the
2985 * console_lock as a hack, to be able to see backtraces and crashes on the
2986 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2989 * The other option is to just disable fbdev emulation since very likely the
2990 * first modeset from userspace will crash in the same way, and is even easier
2991 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
2992 * kernel cmdline option.
2995 * Zero if everything went ok, nonzero otherwise.
2997 int drm_fb_helper_initial_config(struct drm_fb_helper
*fb_helper
, int bpp_sel
)
3001 if (!drm_fbdev_emulation
)
3004 mutex_lock(&fb_helper
->lock
);
3005 ret
= __drm_fb_helper_initial_config_and_unlock(fb_helper
, bpp_sel
);
3009 EXPORT_SYMBOL(drm_fb_helper_initial_config
);
3012 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
3013 * probing all the outputs attached to the fb
3014 * @fb_helper: driver-allocated fbdev helper, can be NULL
3016 * Scan the connectors attached to the fb_helper and try to put together a
3017 * setup after notification of a change in output configuration.
3019 * Called at runtime, takes the mode config locks to be able to check/change the
3020 * modeset configuration. Must be run from process context (which usually means
3021 * either the output polling work or a work item launched from the driver's
3022 * hotplug interrupt).
3024 * Note that drivers may call this even before calling
3025 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
3026 * for a race-free fbcon setup and will make sure that the fbdev emulation will
3027 * not miss any hotplug events.
3030 * 0 on success and a non-zero error code otherwise.
3032 int drm_fb_helper_hotplug_event(struct drm_fb_helper
*fb_helper
)
3036 if (!drm_fbdev_emulation
|| !fb_helper
)
3039 mutex_lock(&fb_helper
->lock
);
3040 if (fb_helper
->deferred_setup
) {
3041 err
= __drm_fb_helper_initial_config_and_unlock(fb_helper
,
3042 fb_helper
->preferred_bpp
);
3046 if (!fb_helper
->fb
|| !drm_master_internal_acquire(fb_helper
->dev
)) {
3047 fb_helper
->delayed_hotplug
= true;
3048 mutex_unlock(&fb_helper
->lock
);
3052 drm_master_internal_release(fb_helper
->dev
);
3054 DRM_DEBUG_KMS("\n");
3056 drm_setup_crtcs(fb_helper
, fb_helper
->fb
->width
, fb_helper
->fb
->height
);
3057 drm_setup_crtcs_fb(fb_helper
);
3058 mutex_unlock(&fb_helper
->lock
);
3060 drm_fb_helper_set_par(fb_helper
->fbdev
);
3064 EXPORT_SYMBOL(drm_fb_helper_hotplug_event
);
3067 * drm_fb_helper_fbdev_setup() - Setup fbdev emulation
3069 * @fb_helper: fbdev helper structure to set up
3070 * @funcs: fbdev helper functions
3071 * @preferred_bpp: Preferred bits per pixel for the device.
3072 * @dev->mode_config.preferred_depth is used if this is zero.
3073 * @max_conn_count: Maximum number of connectors.
3074 * @dev->mode_config.num_connector is used if this is zero.
3076 * This function sets up fbdev emulation and registers fbdev for access by
3077 * userspace. If all connectors are disconnected, setup is deferred to the next
3078 * time drm_fb_helper_hotplug_event() is called.
3079 * The caller must to provide a &drm_fb_helper_funcs->fb_probe callback
3082 * Use drm_fb_helper_fbdev_teardown() to destroy the fbdev.
3084 * See also: drm_fb_helper_initial_config(), drm_fbdev_generic_setup().
3087 * Zero on success or negative error code on failure.
3089 int drm_fb_helper_fbdev_setup(struct drm_device
*dev
,
3090 struct drm_fb_helper
*fb_helper
,
3091 const struct drm_fb_helper_funcs
*funcs
,
3092 unsigned int preferred_bpp
,
3093 unsigned int max_conn_count
)
3098 preferred_bpp
= dev
->mode_config
.preferred_depth
;
3102 if (!max_conn_count
)
3103 max_conn_count
= dev
->mode_config
.num_connector
;
3104 if (!max_conn_count
) {
3105 DRM_DEV_ERROR(dev
->dev
, "fbdev: No connectors\n");
3109 drm_fb_helper_prepare(dev
, fb_helper
, funcs
);
3111 ret
= drm_fb_helper_init(dev
, fb_helper
, max_conn_count
);
3113 DRM_DEV_ERROR(dev
->dev
, "fbdev: Failed to initialize (ret=%d)\n", ret
);
3117 ret
= drm_fb_helper_single_add_all_connectors(fb_helper
);
3119 DRM_DEV_ERROR(dev
->dev
, "fbdev: Failed to add connectors (ret=%d)\n", ret
);
3120 goto err_drm_fb_helper_fini
;
3123 if (!drm_drv_uses_atomic_modeset(dev
))
3124 drm_helper_disable_unused_functions(dev
);
3126 ret
= drm_fb_helper_initial_config(fb_helper
, preferred_bpp
);
3128 DRM_DEV_ERROR(dev
->dev
, "fbdev: Failed to set configuration (ret=%d)\n", ret
);
3129 goto err_drm_fb_helper_fini
;
3134 err_drm_fb_helper_fini
:
3135 drm_fb_helper_fbdev_teardown(dev
);
3139 EXPORT_SYMBOL(drm_fb_helper_fbdev_setup
);
3142 * drm_fb_helper_fbdev_teardown - Tear down fbdev emulation
3145 * This function unregisters fbdev if not already done and cleans up the
3146 * associated resources including the &drm_framebuffer.
3147 * The driver is responsible for freeing the &drm_fb_helper structure which is
3148 * stored in &drm_device->fb_helper. Do note that this pointer has been cleared
3149 * when this function returns.
3151 * In order to support device removal/unplug while file handles are still open,
3152 * drm_fb_helper_unregister_fbi() should be called on device removal and
3153 * drm_fb_helper_fbdev_teardown() in the &drm_driver->release callback when
3154 * file handles are closed.
3156 void drm_fb_helper_fbdev_teardown(struct drm_device
*dev
)
3158 struct drm_fb_helper
*fb_helper
= dev
->fb_helper
;
3159 struct fb_ops
*fbops
= NULL
;
3164 /* Unregister if it hasn't been done already */
3165 if (fb_helper
->fbdev
&& fb_helper
->fbdev
->dev
)
3166 drm_fb_helper_unregister_fbi(fb_helper
);
3168 if (fb_helper
->fbdev
&& fb_helper
->fbdev
->fbdefio
) {
3169 fb_deferred_io_cleanup(fb_helper
->fbdev
);
3170 kfree(fb_helper
->fbdev
->fbdefio
);
3171 fbops
= fb_helper
->fbdev
->fbops
;
3174 drm_fb_helper_fini(fb_helper
);
3178 drm_framebuffer_remove(fb_helper
->fb
);
3180 EXPORT_SYMBOL(drm_fb_helper_fbdev_teardown
);
3183 * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation
3186 * This function can be used as the &drm_driver->lastclose callback for drivers
3187 * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked().
3189 void drm_fb_helper_lastclose(struct drm_device
*dev
)
3191 drm_fb_helper_restore_fbdev_mode_unlocked(dev
->fb_helper
);
3193 EXPORT_SYMBOL(drm_fb_helper_lastclose
);
3196 * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed
3197 * helper for fbdev emulation
3200 * This function can be used as the
3201 * &drm_mode_config_funcs.output_poll_changed callback for drivers that only
3202 * need to call drm_fb_helper_hotplug_event().
3204 void drm_fb_helper_output_poll_changed(struct drm_device
*dev
)
3206 drm_fb_helper_hotplug_event(dev
->fb_helper
);
3208 EXPORT_SYMBOL(drm_fb_helper_output_poll_changed
);
3210 /* @user: 1=userspace, 0=fbcon */
3211 static int drm_fbdev_fb_open(struct fb_info
*info
, int user
)
3213 struct drm_fb_helper
*fb_helper
= info
->par
;
3215 /* No need to take a ref for fbcon because it unbinds on unregister */
3216 if (user
&& !try_module_get(fb_helper
->dev
->driver
->fops
->owner
))
3222 static int drm_fbdev_fb_release(struct fb_info
*info
, int user
)
3224 struct drm_fb_helper
*fb_helper
= info
->par
;
3227 module_put(fb_helper
->dev
->driver
->fops
->owner
);
3232 static void drm_fbdev_cleanup(struct drm_fb_helper
*fb_helper
)
3234 struct fb_info
*fbi
= fb_helper
->fbdev
;
3235 struct fb_ops
*fbops
= NULL
;
3236 void *shadow
= NULL
;
3238 if (!fb_helper
->dev
)
3241 if (fbi
&& fbi
->fbdefio
) {
3242 fb_deferred_io_cleanup(fbi
);
3243 shadow
= fbi
->screen_buffer
;
3247 drm_fb_helper_fini(fb_helper
);
3254 drm_client_framebuffer_delete(fb_helper
->buffer
);
3257 static void drm_fbdev_release(struct drm_fb_helper
*fb_helper
)
3259 drm_fbdev_cleanup(fb_helper
);
3260 drm_client_release(&fb_helper
->client
);
3265 * fb_ops.fb_destroy is called by the last put_fb_info() call at the end of
3266 * unregister_framebuffer() or fb_release().
3268 static void drm_fbdev_fb_destroy(struct fb_info
*info
)
3270 drm_fbdev_release(info
->par
);
3273 static int drm_fbdev_fb_mmap(struct fb_info
*info
, struct vm_area_struct
*vma
)
3275 struct drm_fb_helper
*fb_helper
= info
->par
;
3277 if (fb_helper
->dev
->driver
->gem_prime_mmap
)
3278 return fb_helper
->dev
->driver
->gem_prime_mmap(fb_helper
->buffer
->gem
, vma
);
3283 static struct fb_ops drm_fbdev_fb_ops
= {
3284 .owner
= THIS_MODULE
,
3285 DRM_FB_HELPER_DEFAULT_OPS
,
3286 .fb_open
= drm_fbdev_fb_open
,
3287 .fb_release
= drm_fbdev_fb_release
,
3288 .fb_destroy
= drm_fbdev_fb_destroy
,
3289 .fb_mmap
= drm_fbdev_fb_mmap
,
3290 .fb_read
= drm_fb_helper_sys_read
,
3291 .fb_write
= drm_fb_helper_sys_write
,
3292 .fb_fillrect
= drm_fb_helper_sys_fillrect
,
3293 .fb_copyarea
= drm_fb_helper_sys_copyarea
,
3294 .fb_imageblit
= drm_fb_helper_sys_imageblit
,
3297 static struct fb_deferred_io drm_fbdev_defio
= {
3299 .deferred_io
= drm_fb_helper_deferred_io
,
3303 * drm_fb_helper_generic_probe - Generic fbdev emulation probe helper
3304 * @fb_helper: fbdev helper structure
3305 * @sizes: describes fbdev size and scanout surface size
3307 * This function uses the client API to create a framebuffer backed by a dumb buffer.
3309 * The _sys_ versions are used for &fb_ops.fb_read, fb_write, fb_fillrect,
3310 * fb_copyarea, fb_imageblit.
3313 * Zero on success or negative error code on failure.
3315 int drm_fb_helper_generic_probe(struct drm_fb_helper
*fb_helper
,
3316 struct drm_fb_helper_surface_size
*sizes
)
3318 struct drm_client_dev
*client
= &fb_helper
->client
;
3319 struct drm_client_buffer
*buffer
;
3320 struct drm_framebuffer
*fb
;
3321 struct fb_info
*fbi
;
3324 DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n",
3325 sizes
->surface_width
, sizes
->surface_height
,
3326 sizes
->surface_bpp
);
3328 format
= drm_mode_legacy_fb_format(sizes
->surface_bpp
, sizes
->surface_depth
);
3329 buffer
= drm_client_framebuffer_create(client
, sizes
->surface_width
,
3330 sizes
->surface_height
, format
);
3332 return PTR_ERR(buffer
);
3334 fb_helper
->buffer
= buffer
;
3335 fb_helper
->fb
= buffer
->fb
;
3338 fbi
= drm_fb_helper_alloc_fbi(fb_helper
);
3340 return PTR_ERR(fbi
);
3342 fbi
->fbops
= &drm_fbdev_fb_ops
;
3343 fbi
->screen_size
= fb
->height
* fb
->pitches
[0];
3344 fbi
->fix
.smem_len
= fbi
->screen_size
;
3345 fbi
->screen_buffer
= buffer
->vaddr
;
3346 /* Shamelessly leak the physical address to user-space */
3347 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
3348 if (drm_leak_fbdev_smem
&& fbi
->fix
.smem_start
== 0)
3349 fbi
->fix
.smem_start
=
3350 page_to_phys(virt_to_page(fbi
->screen_buffer
));
3352 drm_fb_helper_fill_info(fbi
, fb_helper
, sizes
);
3354 if (fb
->funcs
->dirty
) {
3355 struct fb_ops
*fbops
;
3359 * fb_deferred_io_cleanup() clears &fbops->fb_mmap so a per
3360 * instance version is necessary.
3362 fbops
= kzalloc(sizeof(*fbops
), GFP_KERNEL
);
3363 shadow
= vzalloc(fbi
->screen_size
);
3364 if (!fbops
|| !shadow
) {
3370 *fbops
= *fbi
->fbops
;
3372 fbi
->screen_buffer
= shadow
;
3373 fbi
->fbdefio
= &drm_fbdev_defio
;
3375 fb_deferred_io_init(fbi
);
3380 EXPORT_SYMBOL(drm_fb_helper_generic_probe
);
3382 static const struct drm_fb_helper_funcs drm_fb_helper_generic_funcs
= {
3383 .fb_probe
= drm_fb_helper_generic_probe
,
3386 static void drm_fbdev_client_unregister(struct drm_client_dev
*client
)
3388 struct drm_fb_helper
*fb_helper
= drm_fb_helper_from_client(client
);
3390 if (fb_helper
->fbdev
)
3391 /* drm_fbdev_fb_destroy() takes care of cleanup */
3392 drm_fb_helper_unregister_fbi(fb_helper
);
3394 drm_fbdev_release(fb_helper
);
3397 static int drm_fbdev_client_restore(struct drm_client_dev
*client
)
3399 drm_fb_helper_lastclose(client
->dev
);
3404 static int drm_fbdev_client_hotplug(struct drm_client_dev
*client
)
3406 struct drm_fb_helper
*fb_helper
= drm_fb_helper_from_client(client
);
3407 struct drm_device
*dev
= client
->dev
;
3410 /* Setup is not retried if it has failed */
3411 if (!fb_helper
->dev
&& fb_helper
->funcs
)
3415 return drm_fb_helper_hotplug_event(dev
->fb_helper
);
3417 if (!dev
->mode_config
.num_connector
) {
3418 DRM_DEV_DEBUG(dev
->dev
, "No connectors found, will not create framebuffer!\n");
3422 drm_fb_helper_prepare(dev
, fb_helper
, &drm_fb_helper_generic_funcs
);
3424 ret
= drm_fb_helper_init(dev
, fb_helper
, dev
->mode_config
.num_connector
);
3428 ret
= drm_fb_helper_single_add_all_connectors(fb_helper
);
3432 if (!drm_drv_uses_atomic_modeset(dev
))
3433 drm_helper_disable_unused_functions(dev
);
3435 ret
= drm_fb_helper_initial_config(fb_helper
, fb_helper
->preferred_bpp
);
3442 drm_fbdev_cleanup(fb_helper
);
3444 fb_helper
->dev
= NULL
;
3445 fb_helper
->fbdev
= NULL
;
3447 DRM_DEV_ERROR(dev
->dev
, "fbdev: Failed to setup generic emulation (ret=%d)\n", ret
);
3452 static const struct drm_client_funcs drm_fbdev_client_funcs
= {
3453 .owner
= THIS_MODULE
,
3454 .unregister
= drm_fbdev_client_unregister
,
3455 .restore
= drm_fbdev_client_restore
,
3456 .hotplug
= drm_fbdev_client_hotplug
,
3460 * drm_fbdev_generic_setup() - Setup generic fbdev emulation
3462 * @preferred_bpp: Preferred bits per pixel for the device.
3463 * @dev->mode_config.preferred_depth is used if this is zero.
3465 * This function sets up generic fbdev emulation for drivers that supports
3466 * dumb buffers with a virtual address and that can be mmap'ed. If the driver
3467 * does not support these functions, it could use drm_fb_helper_fbdev_setup().
3469 * Restore, hotplug events and teardown are all taken care of. Drivers that do
3470 * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves.
3471 * Simple drivers might use drm_mode_config_helper_suspend().
3473 * Drivers that set the dirty callback on their framebuffer will get a shadow
3474 * fbdev buffer that is blitted onto the real buffer. This is done in order to
3475 * make deferred I/O work with all kinds of buffers.
3477 * This function is safe to call even when there are no connectors present.
3478 * Setup will be retried on the next hotplug event.
3480 * The fbdev is destroyed by drm_dev_unregister().
3483 * Zero on success or negative error code on failure.
3485 int drm_fbdev_generic_setup(struct drm_device
*dev
, unsigned int preferred_bpp
)
3487 struct drm_fb_helper
*fb_helper
;
3490 WARN(dev
->fb_helper
, "fb_helper is already set!\n");
3492 if (!drm_fbdev_emulation
)
3495 fb_helper
= kzalloc(sizeof(*fb_helper
), GFP_KERNEL
);
3499 ret
= drm_client_init(dev
, &fb_helper
->client
, "fbdev", &drm_fbdev_client_funcs
);
3502 DRM_DEV_ERROR(dev
->dev
, "Failed to register client: %d\n", ret
);
3507 preferred_bpp
= dev
->mode_config
.preferred_depth
;
3510 fb_helper
->preferred_bpp
= preferred_bpp
;
3512 ret
= drm_fbdev_client_hotplug(&fb_helper
->client
);
3514 DRM_DEV_DEBUG(dev
->dev
, "client hotplug ret=%d\n", ret
);
3516 drm_client_register(&fb_helper
->client
);
3520 EXPORT_SYMBOL(drm_fbdev_generic_setup
);
3522 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
3523 * but the module doesn't depend on any fb console symbols. At least
3524 * attempt to load fbcon to avoid leaving the system without a usable console.
3526 int __init
drm_fb_helper_modinit(void)
3528 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
3529 const char name
[] = "fbcon";
3530 struct module
*fbcon
;
3532 mutex_lock(&module_mutex
);
3533 fbcon
= find_module(name
);
3534 mutex_unlock(&module_mutex
);
3537 request_module_nowait(name
);
3541 EXPORT_SYMBOL(drm_fb_helper_modinit
);