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/kernel.h>
34 #include <linux/sysrq.h>
35 #include <linux/slab.h>
36 #include <linux/module.h>
38 #include <drm/drm_crtc.h>
39 #include <drm/drm_fb_helper.h>
40 #include <drm/drm_crtc_helper.h>
41 #include <drm/drm_atomic.h>
42 #include <drm/drm_atomic_helper.h>
44 #include "drm_crtc_helper_internal.h"
46 static bool drm_fbdev_emulation
= true;
47 module_param_named(fbdev_emulation
, drm_fbdev_emulation
, bool, 0600);
48 MODULE_PARM_DESC(fbdev_emulation
,
49 "Enable legacy fbdev emulation [default=true]");
51 static int drm_fbdev_overalloc
= CONFIG_DRM_FBDEV_OVERALLOC
;
52 module_param(drm_fbdev_overalloc
, int, 0444);
53 MODULE_PARM_DESC(drm_fbdev_overalloc
,
54 "Overallocation of the fbdev buffer (%) [default="
55 __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC
) "]");
57 static LIST_HEAD(kernel_fb_helper_list
);
58 static DEFINE_MUTEX(kernel_fb_helper_lock
);
63 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
64 * mode setting driver. They can be used mostly independently from the crtc
65 * helper functions used by many drivers to implement the kernel mode setting
68 * Initialization is done as a four-step process with drm_fb_helper_prepare(),
69 * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
70 * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
71 * default behaviour can override the third step with their own code.
72 * Teardown is done with drm_fb_helper_fini() after the fbdev device is
73 * unregisters using drm_fb_helper_unregister_fbi().
75 * At runtime drivers should restore the fbdev console by calling
76 * drm_fb_helper_restore_fbdev_mode_unlocked() from their &drm_driver.lastclose
77 * callback. They should also notify the fb helper code from updates to the
78 * output configuration by calling drm_fb_helper_hotplug_event(). For easier
79 * integration with the output polling code in drm_crtc_helper.c the modeset
80 * code provides a &drm_mode_config_funcs.output_poll_changed callback.
82 * All other functions exported by the fb helper library can be used to
83 * implement the fbdev driver interface by the driver.
85 * It is possible, though perhaps somewhat tricky, to implement race-free
86 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
87 * helper must be called first to initialize the minimum required to make
88 * hotplug detection work. Drivers also need to make sure to properly set up
89 * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
90 * it is safe to enable interrupts and start processing hotplug events. At the
91 * same time, drivers should initialize all modeset objects such as CRTCs,
92 * encoders and connectors. To finish up the fbdev helper initialization, the
93 * drm_fb_helper_init() function is called. To probe for all attached displays
94 * and set up an initial configuration using the detected hardware, drivers
95 * should call drm_fb_helper_single_add_all_connectors() followed by
96 * drm_fb_helper_initial_config().
98 * If &drm_framebuffer_funcs.dirty is set, the
99 * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
100 * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
101 * away. This worker then calls the dirty() function ensuring that it will
102 * always run in process context since the fb_*() function could be running in
103 * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
104 * callback it will also schedule dirty_work with the damage collected from the
108 #define drm_fb_helper_for_each_connector(fbh, i__) \
109 for (({ lockdep_assert_held(&(fbh)->dev->mode_config.mutex); }), \
110 i__ = 0; i__ < (fbh)->connector_count; i__++)
112 int drm_fb_helper_add_one_connector(struct drm_fb_helper
*fb_helper
,
113 struct drm_connector
*connector
)
115 struct drm_fb_helper_connector
*fb_conn
;
116 struct drm_fb_helper_connector
**temp
;
119 if (!drm_fbdev_emulation
)
122 WARN_ON(!mutex_is_locked(&fb_helper
->dev
->mode_config
.mutex
));
124 count
= fb_helper
->connector_count
+ 1;
126 if (count
> fb_helper
->connector_info_alloc_count
) {
127 size_t size
= count
* sizeof(fb_conn
);
129 temp
= krealloc(fb_helper
->connector_info
, size
, GFP_KERNEL
);
133 fb_helper
->connector_info_alloc_count
= count
;
134 fb_helper
->connector_info
= temp
;
137 fb_conn
= kzalloc(sizeof(*fb_conn
), GFP_KERNEL
);
141 drm_connector_get(connector
);
142 fb_conn
->connector
= connector
;
143 fb_helper
->connector_info
[fb_helper
->connector_count
++] = fb_conn
;
146 EXPORT_SYMBOL(drm_fb_helper_add_one_connector
);
149 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
151 * @fb_helper: fbdev initialized with drm_fb_helper_init
153 * This functions adds all the available connectors for use with the given
154 * fb_helper. This is a separate step to allow drivers to freely assign
155 * connectors to the fbdev, e.g. if some are reserved for special purposes or
156 * not adequate to be used for the fbcon.
158 * This function is protected against concurrent connector hotadds/removals
159 * using drm_fb_helper_add_one_connector() and
160 * drm_fb_helper_remove_one_connector().
162 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper
*fb_helper
)
164 struct drm_device
*dev
= fb_helper
->dev
;
165 struct drm_connector
*connector
;
166 struct drm_connector_list_iter conn_iter
;
169 if (!drm_fbdev_emulation
)
172 mutex_lock(&dev
->mode_config
.mutex
);
173 drm_connector_list_iter_begin(dev
, &conn_iter
);
174 drm_for_each_connector_iter(connector
, &conn_iter
) {
175 ret
= drm_fb_helper_add_one_connector(fb_helper
, connector
);
183 drm_fb_helper_for_each_connector(fb_helper
, i
) {
184 struct drm_fb_helper_connector
*fb_helper_connector
=
185 fb_helper
->connector_info
[i
];
187 drm_connector_put(fb_helper_connector
->connector
);
189 kfree(fb_helper_connector
);
190 fb_helper
->connector_info
[i
] = NULL
;
192 fb_helper
->connector_count
= 0;
194 drm_connector_list_iter_end(&conn_iter
);
195 mutex_unlock(&dev
->mode_config
.mutex
);
199 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors
);
201 int drm_fb_helper_remove_one_connector(struct drm_fb_helper
*fb_helper
,
202 struct drm_connector
*connector
)
204 struct drm_fb_helper_connector
*fb_helper_connector
;
207 if (!drm_fbdev_emulation
)
210 WARN_ON(!mutex_is_locked(&fb_helper
->dev
->mode_config
.mutex
));
212 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
213 if (fb_helper
->connector_info
[i
]->connector
== connector
)
217 if (i
== fb_helper
->connector_count
)
219 fb_helper_connector
= fb_helper
->connector_info
[i
];
220 drm_connector_put(fb_helper_connector
->connector
);
222 for (j
= i
+ 1; j
< fb_helper
->connector_count
; j
++)
223 fb_helper
->connector_info
[j
- 1] = fb_helper
->connector_info
[j
];
225 fb_helper
->connector_count
--;
226 kfree(fb_helper_connector
);
230 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector
);
232 static void drm_fb_helper_save_lut_atomic(struct drm_crtc
*crtc
, struct drm_fb_helper
*helper
)
234 uint16_t *r_base
, *g_base
, *b_base
;
237 if (helper
->funcs
->gamma_get
== NULL
)
240 r_base
= crtc
->gamma_store
;
241 g_base
= r_base
+ crtc
->gamma_size
;
242 b_base
= g_base
+ crtc
->gamma_size
;
244 for (i
= 0; i
< crtc
->gamma_size
; i
++)
245 helper
->funcs
->gamma_get(crtc
, &r_base
[i
], &g_base
[i
], &b_base
[i
], i
);
248 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc
*crtc
)
250 uint16_t *r_base
, *g_base
, *b_base
;
252 if (crtc
->funcs
->gamma_set
== NULL
)
255 r_base
= crtc
->gamma_store
;
256 g_base
= r_base
+ crtc
->gamma_size
;
257 b_base
= g_base
+ crtc
->gamma_size
;
259 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
,
260 crtc
->gamma_size
, NULL
);
264 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
265 * @info: fbdev registered by the helper
267 int drm_fb_helper_debug_enter(struct fb_info
*info
)
269 struct drm_fb_helper
*helper
= info
->par
;
270 const struct drm_crtc_helper_funcs
*funcs
;
273 list_for_each_entry(helper
, &kernel_fb_helper_list
, kernel_fb_list
) {
274 for (i
= 0; i
< helper
->crtc_count
; i
++) {
275 struct drm_mode_set
*mode_set
=
276 &helper
->crtc_info
[i
].mode_set
;
278 if (!mode_set
->crtc
->enabled
)
281 funcs
= mode_set
->crtc
->helper_private
;
282 if (funcs
->mode_set_base_atomic
== NULL
)
285 if (drm_drv_uses_atomic_modeset(mode_set
->crtc
->dev
))
288 drm_fb_helper_save_lut_atomic(mode_set
->crtc
, helper
);
289 funcs
->mode_set_base_atomic(mode_set
->crtc
,
293 ENTER_ATOMIC_MODE_SET
);
299 EXPORT_SYMBOL(drm_fb_helper_debug_enter
);
301 /* Find the real fb for a given fb helper CRTC */
302 static struct drm_framebuffer
*drm_mode_config_fb(struct drm_crtc
*crtc
)
304 struct drm_device
*dev
= crtc
->dev
;
307 drm_for_each_crtc(c
, dev
) {
308 if (crtc
->base
.id
== c
->base
.id
)
309 return c
->primary
->fb
;
316 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
317 * @info: fbdev registered by the helper
319 int drm_fb_helper_debug_leave(struct fb_info
*info
)
321 struct drm_fb_helper
*helper
= info
->par
;
322 struct drm_crtc
*crtc
;
323 const struct drm_crtc_helper_funcs
*funcs
;
324 struct drm_framebuffer
*fb
;
327 for (i
= 0; i
< helper
->crtc_count
; i
++) {
328 struct drm_mode_set
*mode_set
= &helper
->crtc_info
[i
].mode_set
;
330 crtc
= mode_set
->crtc
;
331 funcs
= crtc
->helper_private
;
332 fb
= drm_mode_config_fb(crtc
);
338 DRM_ERROR("no fb to restore??\n");
342 if (funcs
->mode_set_base_atomic
== NULL
)
345 if (drm_drv_uses_atomic_modeset(crtc
->dev
))
348 drm_fb_helper_restore_lut_atomic(mode_set
->crtc
);
349 funcs
->mode_set_base_atomic(mode_set
->crtc
, fb
, crtc
->x
,
350 crtc
->y
, LEAVE_ATOMIC_MODE_SET
);
355 EXPORT_SYMBOL(drm_fb_helper_debug_leave
);
357 static int restore_fbdev_mode_atomic(struct drm_fb_helper
*fb_helper
)
359 struct drm_device
*dev
= fb_helper
->dev
;
360 struct drm_plane
*plane
;
361 struct drm_atomic_state
*state
;
363 unsigned int plane_mask
;
365 state
= drm_atomic_state_alloc(dev
);
369 state
->acquire_ctx
= dev
->mode_config
.acquire_ctx
;
372 drm_for_each_plane(plane
, dev
) {
373 struct drm_plane_state
*plane_state
;
375 plane_state
= drm_atomic_get_plane_state(state
, plane
);
376 if (IS_ERR(plane_state
)) {
377 ret
= PTR_ERR(plane_state
);
381 plane_state
->rotation
= DRM_MODE_ROTATE_0
;
383 plane
->old_fb
= plane
->fb
;
384 plane_mask
|= 1 << drm_plane_index(plane
);
386 /* disable non-primary: */
387 if (plane
->type
== DRM_PLANE_TYPE_PRIMARY
)
390 ret
= __drm_atomic_helper_disable_plane(plane
, plane_state
);
395 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
396 struct drm_mode_set
*mode_set
= &fb_helper
->crtc_info
[i
].mode_set
;
398 ret
= __drm_atomic_helper_set_config(mode_set
, state
);
403 ret
= drm_atomic_commit(state
);
406 drm_atomic_clean_old_fb(dev
, plane_mask
, ret
);
411 drm_atomic_state_put(state
);
415 drm_atomic_state_clear(state
);
416 drm_atomic_legacy_backoff(state
);
421 static int restore_fbdev_mode_legacy(struct drm_fb_helper
*fb_helper
)
423 struct drm_device
*dev
= fb_helper
->dev
;
424 struct drm_plane
*plane
;
427 drm_for_each_plane(plane
, dev
) {
428 if (plane
->type
!= DRM_PLANE_TYPE_PRIMARY
)
429 drm_plane_force_disable(plane
);
431 if (plane
->rotation_property
)
432 drm_mode_plane_set_obj_prop(plane
,
433 plane
->rotation_property
,
437 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
438 struct drm_mode_set
*mode_set
= &fb_helper
->crtc_info
[i
].mode_set
;
439 struct drm_crtc
*crtc
= mode_set
->crtc
;
442 if (crtc
->funcs
->cursor_set2
) {
443 ret
= crtc
->funcs
->cursor_set2(crtc
, NULL
, 0, 0, 0, 0, 0);
446 } else if (crtc
->funcs
->cursor_set
) {
447 ret
= crtc
->funcs
->cursor_set(crtc
, NULL
, 0, 0, 0);
452 ret
= drm_mode_set_config_internal(mode_set
);
460 static int restore_fbdev_mode(struct drm_fb_helper
*fb_helper
)
462 struct drm_device
*dev
= fb_helper
->dev
;
464 drm_warn_on_modeset_not_all_locked(dev
);
466 if (drm_drv_uses_atomic_modeset(dev
))
467 return restore_fbdev_mode_atomic(fb_helper
);
469 return restore_fbdev_mode_legacy(fb_helper
);
473 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
474 * @fb_helper: fbcon to restore
476 * This should be called from driver's drm &drm_driver.lastclose callback
477 * when implementing an fbcon on top of kms using this helper. This ensures that
478 * the user isn't greeted with a black screen when e.g. X dies.
481 * Zero if everything went ok, negative error code otherwise.
483 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper
*fb_helper
)
485 struct drm_device
*dev
= fb_helper
->dev
;
489 if (!drm_fbdev_emulation
)
492 drm_modeset_lock_all(dev
);
493 ret
= restore_fbdev_mode(fb_helper
);
495 do_delayed
= fb_helper
->delayed_hotplug
;
497 fb_helper
->delayed_hotplug
= false;
498 drm_modeset_unlock_all(dev
);
501 drm_fb_helper_hotplug_event(fb_helper
);
504 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked
);
506 static bool drm_fb_helper_is_bound(struct drm_fb_helper
*fb_helper
)
508 struct drm_device
*dev
= fb_helper
->dev
;
509 struct drm_crtc
*crtc
;
510 int bound
= 0, crtcs_bound
= 0;
513 * Sometimes user space wants everything disabled, so don't steal the
514 * display if there's a master.
516 if (READ_ONCE(dev
->master
))
519 drm_for_each_crtc(crtc
, dev
) {
520 if (crtc
->primary
->fb
)
522 if (crtc
->primary
->fb
== fb_helper
->fb
)
526 if (bound
< crtcs_bound
)
532 #ifdef CONFIG_MAGIC_SYSRQ
534 * restore fbcon display for all kms driver's using this helper, used for sysrq
535 * and panic handling.
537 static bool drm_fb_helper_force_kernel_mode(void)
539 bool ret
, error
= false;
540 struct drm_fb_helper
*helper
;
542 if (list_empty(&kernel_fb_helper_list
))
545 list_for_each_entry(helper
, &kernel_fb_helper_list
, kernel_fb_list
) {
546 struct drm_device
*dev
= helper
->dev
;
548 if (dev
->switch_power_state
== DRM_SWITCH_POWER_OFF
)
551 drm_modeset_lock_all(dev
);
552 ret
= restore_fbdev_mode(helper
);
555 drm_modeset_unlock_all(dev
);
560 static void drm_fb_helper_restore_work_fn(struct work_struct
*ignored
)
564 ret
= drm_fb_helper_force_kernel_mode();
566 DRM_ERROR("Failed to restore crtc configuration\n");
568 static DECLARE_WORK(drm_fb_helper_restore_work
, drm_fb_helper_restore_work_fn
);
570 static void drm_fb_helper_sysrq(int dummy1
)
572 schedule_work(&drm_fb_helper_restore_work
);
575 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op
= {
576 .handler
= drm_fb_helper_sysrq
,
577 .help_msg
= "force-fb(V)",
578 .action_msg
= "Restore framebuffer console",
581 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op
= { };
584 static void drm_fb_helper_dpms(struct fb_info
*info
, int dpms_mode
)
586 struct drm_fb_helper
*fb_helper
= info
->par
;
587 struct drm_device
*dev
= fb_helper
->dev
;
588 struct drm_crtc
*crtc
;
589 struct drm_connector
*connector
;
593 * For each CRTC in this fb, turn the connectors on/off.
595 drm_modeset_lock_all(dev
);
596 if (!drm_fb_helper_is_bound(fb_helper
)) {
597 drm_modeset_unlock_all(dev
);
601 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
602 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
607 /* Walk the connectors & encoders on this fb turning them on/off */
608 drm_fb_helper_for_each_connector(fb_helper
, j
) {
609 connector
= fb_helper
->connector_info
[j
]->connector
;
610 connector
->funcs
->dpms(connector
, dpms_mode
);
611 drm_object_property_set_value(&connector
->base
,
612 dev
->mode_config
.dpms_property
, dpms_mode
);
615 drm_modeset_unlock_all(dev
);
619 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
620 * @blank: desired blanking state
621 * @info: fbdev registered by the helper
623 int drm_fb_helper_blank(int blank
, struct fb_info
*info
)
625 if (oops_in_progress
)
629 /* Display: On; HSync: On, VSync: On */
630 case FB_BLANK_UNBLANK
:
631 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_ON
);
633 /* Display: Off; HSync: On, VSync: On */
634 case FB_BLANK_NORMAL
:
635 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_STANDBY
);
637 /* Display: Off; HSync: Off, VSync: On */
638 case FB_BLANK_HSYNC_SUSPEND
:
639 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_STANDBY
);
641 /* Display: Off; HSync: On, VSync: Off */
642 case FB_BLANK_VSYNC_SUSPEND
:
643 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_SUSPEND
);
645 /* Display: Off; HSync: Off, VSync: Off */
646 case FB_BLANK_POWERDOWN
:
647 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_OFF
);
652 EXPORT_SYMBOL(drm_fb_helper_blank
);
654 static void drm_fb_helper_modeset_release(struct drm_fb_helper
*helper
,
655 struct drm_mode_set
*modeset
)
659 for (i
= 0; i
< modeset
->num_connectors
; i
++) {
660 drm_connector_put(modeset
->connectors
[i
]);
661 modeset
->connectors
[i
] = NULL
;
663 modeset
->num_connectors
= 0;
665 drm_mode_destroy(helper
->dev
, modeset
->mode
);
666 modeset
->mode
= NULL
;
668 /* FIXME should hold a ref? */
672 static void drm_fb_helper_crtc_free(struct drm_fb_helper
*helper
)
676 for (i
= 0; i
< helper
->connector_count
; i
++) {
677 drm_connector_put(helper
->connector_info
[i
]->connector
);
678 kfree(helper
->connector_info
[i
]);
680 kfree(helper
->connector_info
);
682 for (i
= 0; i
< helper
->crtc_count
; i
++) {
683 struct drm_mode_set
*modeset
= &helper
->crtc_info
[i
].mode_set
;
685 drm_fb_helper_modeset_release(helper
, modeset
);
686 kfree(modeset
->connectors
);
688 kfree(helper
->crtc_info
);
691 static void drm_fb_helper_resume_worker(struct work_struct
*work
)
693 struct drm_fb_helper
*helper
= container_of(work
, struct drm_fb_helper
,
697 fb_set_suspend(helper
->fbdev
, 0);
701 static void drm_fb_helper_dirty_work(struct work_struct
*work
)
703 struct drm_fb_helper
*helper
= container_of(work
, struct drm_fb_helper
,
705 struct drm_clip_rect
*clip
= &helper
->dirty_clip
;
706 struct drm_clip_rect clip_copy
;
709 spin_lock_irqsave(&helper
->dirty_lock
, flags
);
711 clip
->x1
= clip
->y1
= ~0;
712 clip
->x2
= clip
->y2
= 0;
713 spin_unlock_irqrestore(&helper
->dirty_lock
, flags
);
715 /* call dirty callback only when it has been really touched */
716 if (clip_copy
.x1
< clip_copy
.x2
&& clip_copy
.y1
< clip_copy
.y2
)
717 helper
->fb
->funcs
->dirty(helper
->fb
, NULL
, 0, 0, &clip_copy
, 1);
721 * drm_fb_helper_prepare - setup a drm_fb_helper structure
723 * @helper: driver-allocated fbdev helper structure to set up
724 * @funcs: pointer to structure of functions associate with this helper
726 * Sets up the bare minimum to make the framebuffer helper usable. This is
727 * useful to implement race-free initialization of the polling helpers.
729 void drm_fb_helper_prepare(struct drm_device
*dev
, struct drm_fb_helper
*helper
,
730 const struct drm_fb_helper_funcs
*funcs
)
732 INIT_LIST_HEAD(&helper
->kernel_fb_list
);
733 spin_lock_init(&helper
->dirty_lock
);
734 INIT_WORK(&helper
->resume_work
, drm_fb_helper_resume_worker
);
735 INIT_WORK(&helper
->dirty_work
, drm_fb_helper_dirty_work
);
736 helper
->dirty_clip
.x1
= helper
->dirty_clip
.y1
= ~0;
737 helper
->funcs
= funcs
;
740 EXPORT_SYMBOL(drm_fb_helper_prepare
);
743 * drm_fb_helper_init - initialize a &struct drm_fb_helper
745 * @fb_helper: driver-allocated fbdev helper structure to initialize
746 * @max_conn_count: max connector count
748 * This allocates the structures for the fbdev helper with the given limits.
749 * Note that this won't yet touch the hardware (through the driver interfaces)
750 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
751 * to allow driver writes more control over the exact init sequence.
753 * Drivers must call drm_fb_helper_prepare() before calling this function.
756 * Zero if everything went ok, nonzero otherwise.
758 int drm_fb_helper_init(struct drm_device
*dev
,
759 struct drm_fb_helper
*fb_helper
,
762 struct drm_crtc
*crtc
;
763 struct drm_mode_config
*config
= &dev
->mode_config
;
766 if (!drm_fbdev_emulation
)
772 fb_helper
->crtc_info
= kcalloc(config
->num_crtc
, sizeof(struct drm_fb_helper_crtc
), GFP_KERNEL
);
773 if (!fb_helper
->crtc_info
)
776 fb_helper
->crtc_count
= config
->num_crtc
;
777 fb_helper
->connector_info
= kcalloc(dev
->mode_config
.num_connector
, sizeof(struct drm_fb_helper_connector
*), GFP_KERNEL
);
778 if (!fb_helper
->connector_info
) {
779 kfree(fb_helper
->crtc_info
);
782 fb_helper
->connector_info_alloc_count
= dev
->mode_config
.num_connector
;
783 fb_helper
->connector_count
= 0;
785 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
786 fb_helper
->crtc_info
[i
].mode_set
.connectors
=
787 kcalloc(max_conn_count
,
788 sizeof(struct drm_connector
*),
791 if (!fb_helper
->crtc_info
[i
].mode_set
.connectors
)
793 fb_helper
->crtc_info
[i
].mode_set
.num_connectors
= 0;
797 drm_for_each_crtc(crtc
, dev
) {
798 fb_helper
->crtc_info
[i
].mode_set
.crtc
= crtc
;
804 drm_fb_helper_crtc_free(fb_helper
);
807 EXPORT_SYMBOL(drm_fb_helper_init
);
810 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
811 * @fb_helper: driver-allocated fbdev helper
813 * A helper to alloc fb_info and the members cmap and apertures. Called
814 * by the driver within the fb_probe fb_helper callback function. Drivers do not
815 * need to release the allocated fb_info structure themselves, this is
816 * automatically done when calling drm_fb_helper_fini().
819 * fb_info pointer if things went okay, pointer containing error code
822 struct fb_info
*drm_fb_helper_alloc_fbi(struct drm_fb_helper
*fb_helper
)
824 struct device
*dev
= fb_helper
->dev
->dev
;
825 struct fb_info
*info
;
828 info
= framebuffer_alloc(0, dev
);
830 return ERR_PTR(-ENOMEM
);
832 ret
= fb_alloc_cmap(&info
->cmap
, 256, 0);
836 info
->apertures
= alloc_apertures(1);
837 if (!info
->apertures
) {
842 fb_helper
->fbdev
= info
;
847 fb_dealloc_cmap(&info
->cmap
);
849 framebuffer_release(info
);
852 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi
);
855 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
856 * @fb_helper: driver-allocated fbdev helper
858 * A wrapper around unregister_framebuffer, to release the fb_info
859 * framebuffer device. This must be called before releasing all resources for
860 * @fb_helper by calling drm_fb_helper_fini().
862 void drm_fb_helper_unregister_fbi(struct drm_fb_helper
*fb_helper
)
864 if (fb_helper
&& fb_helper
->fbdev
)
865 unregister_framebuffer(fb_helper
->fbdev
);
867 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi
);
870 * drm_fb_helper_fini - finialize a &struct drm_fb_helper
871 * @fb_helper: driver-allocated fbdev helper
873 * This cleans up all remaining resources associated with @fb_helper. Must be
874 * called after drm_fb_helper_unlink_fbi() was called.
876 void drm_fb_helper_fini(struct drm_fb_helper
*fb_helper
)
878 struct fb_info
*info
;
880 if (!drm_fbdev_emulation
|| !fb_helper
)
883 info
= fb_helper
->fbdev
;
886 fb_dealloc_cmap(&info
->cmap
);
887 framebuffer_release(info
);
889 fb_helper
->fbdev
= NULL
;
891 cancel_work_sync(&fb_helper
->resume_work
);
892 cancel_work_sync(&fb_helper
->dirty_work
);
894 mutex_lock(&kernel_fb_helper_lock
);
895 if (!list_empty(&fb_helper
->kernel_fb_list
)) {
896 list_del(&fb_helper
->kernel_fb_list
);
897 if (list_empty(&kernel_fb_helper_list
))
898 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op
);
900 mutex_unlock(&kernel_fb_helper_lock
);
902 drm_fb_helper_crtc_free(fb_helper
);
905 EXPORT_SYMBOL(drm_fb_helper_fini
);
908 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
909 * @fb_helper: driver-allocated fbdev helper
911 * A wrapper around unlink_framebuffer implemented by fbdev core
913 void drm_fb_helper_unlink_fbi(struct drm_fb_helper
*fb_helper
)
915 if (fb_helper
&& fb_helper
->fbdev
)
916 unlink_framebuffer(fb_helper
->fbdev
);
918 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi
);
920 static void drm_fb_helper_dirty(struct fb_info
*info
, u32 x
, u32 y
,
921 u32 width
, u32 height
)
923 struct drm_fb_helper
*helper
= info
->par
;
924 struct drm_clip_rect
*clip
= &helper
->dirty_clip
;
927 if (!helper
->fb
->funcs
->dirty
)
930 spin_lock_irqsave(&helper
->dirty_lock
, flags
);
931 clip
->x1
= min_t(u32
, clip
->x1
, x
);
932 clip
->y1
= min_t(u32
, clip
->y1
, y
);
933 clip
->x2
= max_t(u32
, clip
->x2
, x
+ width
);
934 clip
->y2
= max_t(u32
, clip
->y2
, y
+ height
);
935 spin_unlock_irqrestore(&helper
->dirty_lock
, flags
);
937 schedule_work(&helper
->dirty_work
);
941 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
942 * @info: fb_info struct pointer
943 * @pagelist: list of dirty mmap framebuffer pages
945 * This function is used as the &fb_deferred_io.deferred_io
946 * callback function for flushing the fbdev mmap writes.
948 void drm_fb_helper_deferred_io(struct fb_info
*info
,
949 struct list_head
*pagelist
)
951 unsigned long start
, end
, min
, max
;
957 list_for_each_entry(page
, pagelist
, lru
) {
958 start
= page
->index
<< PAGE_SHIFT
;
959 end
= start
+ PAGE_SIZE
- 1;
960 min
= min(min
, start
);
965 y1
= min
/ info
->fix
.line_length
;
966 y2
= min_t(u32
, DIV_ROUND_UP(max
, info
->fix
.line_length
),
968 drm_fb_helper_dirty(info
, 0, y1
, info
->var
.xres
, y2
- y1
);
971 EXPORT_SYMBOL(drm_fb_helper_deferred_io
);
974 * drm_fb_helper_sys_read - wrapper around fb_sys_read
975 * @info: fb_info struct pointer
976 * @buf: userspace buffer to read from framebuffer memory
977 * @count: number of bytes to read from framebuffer memory
978 * @ppos: read offset within framebuffer memory
980 * A wrapper around fb_sys_read implemented by fbdev core
982 ssize_t
drm_fb_helper_sys_read(struct fb_info
*info
, char __user
*buf
,
983 size_t count
, loff_t
*ppos
)
985 return fb_sys_read(info
, buf
, count
, ppos
);
987 EXPORT_SYMBOL(drm_fb_helper_sys_read
);
990 * drm_fb_helper_sys_write - wrapper around fb_sys_write
991 * @info: fb_info struct pointer
992 * @buf: userspace buffer to write to framebuffer memory
993 * @count: number of bytes to write to framebuffer memory
994 * @ppos: write offset within framebuffer memory
996 * A wrapper around fb_sys_write implemented by fbdev core
998 ssize_t
drm_fb_helper_sys_write(struct fb_info
*info
, const char __user
*buf
,
999 size_t count
, loff_t
*ppos
)
1003 ret
= fb_sys_write(info
, buf
, count
, ppos
);
1005 drm_fb_helper_dirty(info
, 0, 0, info
->var
.xres
,
1010 EXPORT_SYMBOL(drm_fb_helper_sys_write
);
1013 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
1014 * @info: fbdev registered by the helper
1015 * @rect: info about rectangle to fill
1017 * A wrapper around sys_fillrect implemented by fbdev core
1019 void drm_fb_helper_sys_fillrect(struct fb_info
*info
,
1020 const struct fb_fillrect
*rect
)
1022 sys_fillrect(info
, rect
);
1023 drm_fb_helper_dirty(info
, rect
->dx
, rect
->dy
,
1024 rect
->width
, rect
->height
);
1026 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect
);
1029 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1030 * @info: fbdev registered by the helper
1031 * @area: info about area to copy
1033 * A wrapper around sys_copyarea implemented by fbdev core
1035 void drm_fb_helper_sys_copyarea(struct fb_info
*info
,
1036 const struct fb_copyarea
*area
)
1038 sys_copyarea(info
, area
);
1039 drm_fb_helper_dirty(info
, area
->dx
, area
->dy
,
1040 area
->width
, area
->height
);
1042 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea
);
1045 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1046 * @info: fbdev registered by the helper
1047 * @image: info about image to blit
1049 * A wrapper around sys_imageblit implemented by fbdev core
1051 void drm_fb_helper_sys_imageblit(struct fb_info
*info
,
1052 const struct fb_image
*image
)
1054 sys_imageblit(info
, image
);
1055 drm_fb_helper_dirty(info
, image
->dx
, image
->dy
,
1056 image
->width
, image
->height
);
1058 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit
);
1061 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1062 * @info: fbdev registered by the helper
1063 * @rect: info about rectangle to fill
1065 * A wrapper around cfb_imageblit implemented by fbdev core
1067 void drm_fb_helper_cfb_fillrect(struct fb_info
*info
,
1068 const struct fb_fillrect
*rect
)
1070 cfb_fillrect(info
, rect
);
1071 drm_fb_helper_dirty(info
, rect
->dx
, rect
->dy
,
1072 rect
->width
, rect
->height
);
1074 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect
);
1077 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1078 * @info: fbdev registered by the helper
1079 * @area: info about area to copy
1081 * A wrapper around cfb_copyarea implemented by fbdev core
1083 void drm_fb_helper_cfb_copyarea(struct fb_info
*info
,
1084 const struct fb_copyarea
*area
)
1086 cfb_copyarea(info
, area
);
1087 drm_fb_helper_dirty(info
, area
->dx
, area
->dy
,
1088 area
->width
, area
->height
);
1090 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea
);
1093 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1094 * @info: fbdev registered by the helper
1095 * @image: info about image to blit
1097 * A wrapper around cfb_imageblit implemented by fbdev core
1099 void drm_fb_helper_cfb_imageblit(struct fb_info
*info
,
1100 const struct fb_image
*image
)
1102 cfb_imageblit(info
, image
);
1103 drm_fb_helper_dirty(info
, image
->dx
, image
->dy
,
1104 image
->width
, image
->height
);
1106 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit
);
1109 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
1110 * @fb_helper: driver-allocated fbdev helper
1111 * @suspend: whether to suspend or resume
1113 * A wrapper around fb_set_suspend implemented by fbdev core.
1114 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
1117 void drm_fb_helper_set_suspend(struct drm_fb_helper
*fb_helper
, bool suspend
)
1119 if (fb_helper
&& fb_helper
->fbdev
)
1120 fb_set_suspend(fb_helper
->fbdev
, suspend
);
1122 EXPORT_SYMBOL(drm_fb_helper_set_suspend
);
1125 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
1126 * takes the console lock
1127 * @fb_helper: driver-allocated fbdev helper
1128 * @suspend: whether to suspend or resume
1130 * A wrapper around fb_set_suspend() that takes the console lock. If the lock
1131 * isn't available on resume, a worker is tasked with waiting for the lock
1132 * to become available. The console lock can be pretty contented on resume
1133 * due to all the printk activity.
1135 * This function can be called multiple times with the same state since
1136 * &fb_info.state is checked to see if fbdev is running or not before locking.
1138 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
1140 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper
*fb_helper
,
1143 if (!fb_helper
|| !fb_helper
->fbdev
)
1146 /* make sure there's no pending/ongoing resume */
1147 flush_work(&fb_helper
->resume_work
);
1150 if (fb_helper
->fbdev
->state
!= FBINFO_STATE_RUNNING
)
1156 if (fb_helper
->fbdev
->state
== FBINFO_STATE_RUNNING
)
1159 if (!console_trylock()) {
1160 schedule_work(&fb_helper
->resume_work
);
1165 fb_set_suspend(fb_helper
->fbdev
, suspend
);
1168 EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked
);
1170 static int setcolreg(struct drm_crtc
*crtc
, u16 red
, u16 green
,
1171 u16 blue
, u16 regno
, struct fb_info
*info
)
1173 struct drm_fb_helper
*fb_helper
= info
->par
;
1174 struct drm_framebuffer
*fb
= fb_helper
->fb
;
1176 if (info
->fix
.visual
== FB_VISUAL_TRUECOLOR
) {
1179 /* place color in psuedopalette */
1182 palette
= (u32
*)info
->pseudo_palette
;
1183 red
>>= (16 - info
->var
.red
.length
);
1184 green
>>= (16 - info
->var
.green
.length
);
1185 blue
>>= (16 - info
->var
.blue
.length
);
1186 value
= (red
<< info
->var
.red
.offset
) |
1187 (green
<< info
->var
.green
.offset
) |
1188 (blue
<< info
->var
.blue
.offset
);
1189 if (info
->var
.transp
.length
> 0) {
1190 u32 mask
= (1 << info
->var
.transp
.length
) - 1;
1192 mask
<<= info
->var
.transp
.offset
;
1195 palette
[regno
] = value
;
1200 * The driver really shouldn't advertise pseudo/directcolor
1201 * visuals if it can't deal with the palette.
1203 if (WARN_ON(!fb_helper
->funcs
->gamma_set
||
1204 !fb_helper
->funcs
->gamma_get
))
1207 WARN_ON(fb
->format
->cpp
[0] != 1);
1209 fb_helper
->funcs
->gamma_set(crtc
, red
, green
, blue
, regno
);
1215 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
1216 * @cmap: cmap to set
1217 * @info: fbdev registered by the helper
1219 int drm_fb_helper_setcmap(struct fb_cmap
*cmap
, struct fb_info
*info
)
1221 struct drm_fb_helper
*fb_helper
= info
->par
;
1222 struct drm_device
*dev
= fb_helper
->dev
;
1223 const struct drm_crtc_helper_funcs
*crtc_funcs
;
1224 u16
*red
, *green
, *blue
, *transp
;
1225 struct drm_crtc
*crtc
;
1229 if (oops_in_progress
)
1232 drm_modeset_lock_all(dev
);
1233 if (!drm_fb_helper_is_bound(fb_helper
)) {
1234 drm_modeset_unlock_all(dev
);
1238 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1239 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
1240 crtc_funcs
= crtc
->helper_private
;
1243 green
= cmap
->green
;
1245 transp
= cmap
->transp
;
1246 start
= cmap
->start
;
1248 for (j
= 0; j
< cmap
->len
; j
++) {
1249 u16 hred
, hgreen
, hblue
, htransp
= 0xffff;
1256 htransp
= *transp
++;
1258 rc
= setcolreg(crtc
, hred
, hgreen
, hblue
, start
++, info
);
1262 if (crtc_funcs
->load_lut
)
1263 crtc_funcs
->load_lut(crtc
);
1266 drm_modeset_unlock_all(dev
);
1269 EXPORT_SYMBOL(drm_fb_helper_setcmap
);
1272 * drm_fb_helper_ioctl - legacy ioctl implementation
1273 * @info: fbdev registered by the helper
1274 * @cmd: ioctl command
1275 * @arg: ioctl argument
1277 * A helper to implement the standard fbdev ioctl. Only
1278 * FBIO_WAITFORVSYNC is implemented for now.
1280 int drm_fb_helper_ioctl(struct fb_info
*info
, unsigned int cmd
,
1283 struct drm_fb_helper
*fb_helper
= info
->par
;
1284 struct drm_device
*dev
= fb_helper
->dev
;
1285 struct drm_mode_set
*mode_set
;
1286 struct drm_crtc
*crtc
;
1289 mutex_lock(&dev
->mode_config
.mutex
);
1290 if (!drm_fb_helper_is_bound(fb_helper
)) {
1296 case FBIO_WAITFORVSYNC
:
1298 * Only consider the first CRTC.
1300 * This ioctl is supposed to take the CRTC number as
1301 * an argument, but in fbdev times, what that number
1302 * was supposed to be was quite unclear, different
1303 * drivers were passing that argument differently
1304 * (some by reference, some by value), and most of the
1305 * userspace applications were just hardcoding 0 as an
1308 * The first CRTC should be the integrated panel on
1309 * most drivers, so this is the best choice we can
1310 * make. If we're not smart enough here, one should
1311 * just consider switch the userspace to KMS.
1313 mode_set
= &fb_helper
->crtc_info
[0].mode_set
;
1314 crtc
= mode_set
->crtc
;
1317 * Only wait for a vblank event if the CRTC is
1318 * enabled, otherwise just don't do anythintg,
1319 * not even report an error.
1321 ret
= drm_crtc_vblank_get(crtc
);
1323 drm_crtc_wait_one_vblank(crtc
);
1324 drm_crtc_vblank_put(crtc
);
1334 mutex_unlock(&dev
->mode_config
.mutex
);
1337 EXPORT_SYMBOL(drm_fb_helper_ioctl
);
1340 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
1341 * @var: screeninfo to check
1342 * @info: fbdev registered by the helper
1344 int drm_fb_helper_check_var(struct fb_var_screeninfo
*var
,
1345 struct fb_info
*info
)
1347 struct drm_fb_helper
*fb_helper
= info
->par
;
1348 struct drm_framebuffer
*fb
= fb_helper
->fb
;
1351 if (var
->pixclock
!= 0 || in_dbg_master())
1355 * Changes struct fb_var_screeninfo are currently not pushed back
1356 * to KMS, hence fail if different settings are requested.
1358 if (var
->bits_per_pixel
!= fb
->format
->cpp
[0] * 8 ||
1359 var
->xres
> fb
->width
|| var
->yres
> fb
->height
||
1360 var
->xres_virtual
> fb
->width
|| var
->yres_virtual
> fb
->height
) {
1361 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
1362 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1363 var
->xres
, var
->yres
, var
->bits_per_pixel
,
1364 var
->xres_virtual
, var
->yres_virtual
,
1365 fb
->width
, fb
->height
, fb
->format
->cpp
[0] * 8);
1369 switch (var
->bits_per_pixel
) {
1371 depth
= (var
->green
.length
== 6) ? 16 : 15;
1374 depth
= (var
->transp
.length
> 0) ? 32 : 24;
1377 depth
= var
->bits_per_pixel
;
1383 var
->red
.offset
= 0;
1384 var
->green
.offset
= 0;
1385 var
->blue
.offset
= 0;
1386 var
->red
.length
= 8;
1387 var
->green
.length
= 8;
1388 var
->blue
.length
= 8;
1389 var
->transp
.length
= 0;
1390 var
->transp
.offset
= 0;
1393 var
->red
.offset
= 10;
1394 var
->green
.offset
= 5;
1395 var
->blue
.offset
= 0;
1396 var
->red
.length
= 5;
1397 var
->green
.length
= 5;
1398 var
->blue
.length
= 5;
1399 var
->transp
.length
= 1;
1400 var
->transp
.offset
= 15;
1403 var
->red
.offset
= 11;
1404 var
->green
.offset
= 5;
1405 var
->blue
.offset
= 0;
1406 var
->red
.length
= 5;
1407 var
->green
.length
= 6;
1408 var
->blue
.length
= 5;
1409 var
->transp
.length
= 0;
1410 var
->transp
.offset
= 0;
1413 var
->red
.offset
= 16;
1414 var
->green
.offset
= 8;
1415 var
->blue
.offset
= 0;
1416 var
->red
.length
= 8;
1417 var
->green
.length
= 8;
1418 var
->blue
.length
= 8;
1419 var
->transp
.length
= 0;
1420 var
->transp
.offset
= 0;
1423 var
->red
.offset
= 16;
1424 var
->green
.offset
= 8;
1425 var
->blue
.offset
= 0;
1426 var
->red
.length
= 8;
1427 var
->green
.length
= 8;
1428 var
->blue
.length
= 8;
1429 var
->transp
.length
= 8;
1430 var
->transp
.offset
= 24;
1437 EXPORT_SYMBOL(drm_fb_helper_check_var
);
1440 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
1441 * @info: fbdev registered by the helper
1443 * This will let fbcon do the mode init and is called at initialization time by
1444 * the fbdev core when registering the driver, and later on through the hotplug
1447 int drm_fb_helper_set_par(struct fb_info
*info
)
1449 struct drm_fb_helper
*fb_helper
= info
->par
;
1450 struct fb_var_screeninfo
*var
= &info
->var
;
1452 if (oops_in_progress
)
1455 if (var
->pixclock
!= 0) {
1456 DRM_ERROR("PIXEL CLOCK SET\n");
1460 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper
);
1464 EXPORT_SYMBOL(drm_fb_helper_set_par
);
1466 static int pan_display_atomic(struct fb_var_screeninfo
*var
,
1467 struct fb_info
*info
)
1469 struct drm_fb_helper
*fb_helper
= info
->par
;
1470 struct drm_device
*dev
= fb_helper
->dev
;
1471 struct drm_atomic_state
*state
;
1472 struct drm_plane
*plane
;
1474 unsigned int plane_mask
;
1476 state
= drm_atomic_state_alloc(dev
);
1480 state
->acquire_ctx
= dev
->mode_config
.acquire_ctx
;
1483 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1484 struct drm_mode_set
*mode_set
;
1486 mode_set
= &fb_helper
->crtc_info
[i
].mode_set
;
1488 mode_set
->x
= var
->xoffset
;
1489 mode_set
->y
= var
->yoffset
;
1491 ret
= __drm_atomic_helper_set_config(mode_set
, state
);
1495 plane
= mode_set
->crtc
->primary
;
1496 plane_mask
|= (1 << drm_plane_index(plane
));
1497 plane
->old_fb
= plane
->fb
;
1500 ret
= drm_atomic_commit(state
);
1504 info
->var
.xoffset
= var
->xoffset
;
1505 info
->var
.yoffset
= var
->yoffset
;
1508 drm_atomic_clean_old_fb(dev
, plane_mask
, ret
);
1510 if (ret
== -EDEADLK
)
1513 drm_atomic_state_put(state
);
1517 drm_atomic_state_clear(state
);
1518 drm_atomic_legacy_backoff(state
);
1523 static int pan_display_legacy(struct fb_var_screeninfo
*var
,
1524 struct fb_info
*info
)
1526 struct drm_fb_helper
*fb_helper
= info
->par
;
1527 struct drm_mode_set
*modeset
;
1531 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1532 modeset
= &fb_helper
->crtc_info
[i
].mode_set
;
1534 modeset
->x
= var
->xoffset
;
1535 modeset
->y
= var
->yoffset
;
1537 if (modeset
->num_connectors
) {
1538 ret
= drm_mode_set_config_internal(modeset
);
1540 info
->var
.xoffset
= var
->xoffset
;
1541 info
->var
.yoffset
= var
->yoffset
;
1550 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1551 * @var: updated screen information
1552 * @info: fbdev registered by the helper
1554 int drm_fb_helper_pan_display(struct fb_var_screeninfo
*var
,
1555 struct fb_info
*info
)
1557 struct drm_fb_helper
*fb_helper
= info
->par
;
1558 struct drm_device
*dev
= fb_helper
->dev
;
1561 if (oops_in_progress
)
1564 drm_modeset_lock_all(dev
);
1565 if (!drm_fb_helper_is_bound(fb_helper
)) {
1566 drm_modeset_unlock_all(dev
);
1570 if (drm_drv_uses_atomic_modeset(dev
))
1571 ret
= pan_display_atomic(var
, info
);
1573 ret
= pan_display_legacy(var
, info
);
1574 drm_modeset_unlock_all(dev
);
1578 EXPORT_SYMBOL(drm_fb_helper_pan_display
);
1581 * Allocates the backing storage and sets up the fbdev info structure through
1582 * the ->fb_probe callback and then registers the fbdev and sets up the panic
1585 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper
*fb_helper
,
1591 struct drm_fb_helper_surface_size sizes
;
1594 memset(&sizes
, 0, sizeof(struct drm_fb_helper_surface_size
));
1595 sizes
.surface_depth
= 24;
1596 sizes
.surface_bpp
= 32;
1597 sizes
.fb_width
= (u32
)-1;
1598 sizes
.fb_height
= (u32
)-1;
1600 /* if driver picks 8 or 16 by default use that for both depth/bpp */
1601 if (preferred_bpp
!= sizes
.surface_bpp
)
1602 sizes
.surface_depth
= sizes
.surface_bpp
= preferred_bpp
;
1604 /* first up get a count of crtcs now in use and new min/maxes width/heights */
1605 drm_fb_helper_for_each_connector(fb_helper
, i
) {
1606 struct drm_fb_helper_connector
*fb_helper_conn
= fb_helper
->connector_info
[i
];
1607 struct drm_cmdline_mode
*cmdline_mode
;
1609 cmdline_mode
= &fb_helper_conn
->connector
->cmdline_mode
;
1611 if (cmdline_mode
->bpp_specified
) {
1612 switch (cmdline_mode
->bpp
) {
1614 sizes
.surface_depth
= sizes
.surface_bpp
= 8;
1617 sizes
.surface_depth
= 15;
1618 sizes
.surface_bpp
= 16;
1621 sizes
.surface_depth
= sizes
.surface_bpp
= 16;
1624 sizes
.surface_depth
= sizes
.surface_bpp
= 24;
1627 sizes
.surface_depth
= 24;
1628 sizes
.surface_bpp
= 32;
1636 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1637 struct drm_display_mode
*desired_mode
;
1638 struct drm_mode_set
*mode_set
;
1640 /* in case of tile group, are we the last tile vert or horiz?
1641 * If no tile group you are always the last one both vertically
1644 bool lastv
= true, lasth
= true;
1646 desired_mode
= fb_helper
->crtc_info
[i
].desired_mode
;
1647 mode_set
= &fb_helper
->crtc_info
[i
].mode_set
;
1654 x
= fb_helper
->crtc_info
[i
].x
;
1655 y
= fb_helper
->crtc_info
[i
].y
;
1657 if (gamma_size
== 0)
1658 gamma_size
= fb_helper
->crtc_info
[i
].mode_set
.crtc
->gamma_size
;
1660 sizes
.surface_width
= max_t(u32
, desired_mode
->hdisplay
+ x
, sizes
.surface_width
);
1661 sizes
.surface_height
= max_t(u32
, desired_mode
->vdisplay
+ y
, sizes
.surface_height
);
1663 for (j
= 0; j
< mode_set
->num_connectors
; j
++) {
1664 struct drm_connector
*connector
= mode_set
->connectors
[j
];
1666 if (connector
->has_tile
) {
1667 lasth
= (connector
->tile_h_loc
== (connector
->num_h_tile
- 1));
1668 lastv
= (connector
->tile_v_loc
== (connector
->num_v_tile
- 1));
1669 /* cloning to multiple tiles is just crazy-talk, so: */
1675 sizes
.fb_width
= min_t(u32
, desired_mode
->hdisplay
+ x
, sizes
.fb_width
);
1677 sizes
.fb_height
= min_t(u32
, desired_mode
->vdisplay
+ y
, sizes
.fb_height
);
1680 if (crtc_count
== 0 || sizes
.fb_width
== -1 || sizes
.fb_height
== -1) {
1682 * hmm everyone went away - assume VGA cable just fell out
1683 * and will come back later.
1685 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
1686 sizes
.fb_width
= sizes
.surface_width
= 1024;
1687 sizes
.fb_height
= sizes
.surface_height
= 768;
1690 /* Handle our overallocation */
1691 sizes
.surface_height
*= drm_fbdev_overalloc
;
1692 sizes
.surface_height
/= 100;
1694 /* push down into drivers */
1695 ret
= (*fb_helper
->funcs
->fb_probe
)(fb_helper
, &sizes
);
1700 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1701 * events, but at init time drm_setup_crtcs needs to be called before
1702 * the fb is allocated (since we need to figure out the desired size of
1703 * the fb before we can allocate it ...). Hence we need to fix things up
1706 for (i
= 0; i
< fb_helper
->crtc_count
; i
++)
1707 if (fb_helper
->crtc_info
[i
].mode_set
.num_connectors
)
1708 fb_helper
->crtc_info
[i
].mode_set
.fb
= fb_helper
->fb
;
1714 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1715 * @info: fbdev registered by the helper
1716 * @pitch: desired pitch
1717 * @depth: desired depth
1719 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1720 * fbdev emulations. Drivers which support acceleration methods which impose
1721 * additional constraints need to set up their own limits.
1723 * Drivers should call this (or their equivalent setup code) from their
1724 * &drm_fb_helper_funcs.fb_probe callback.
1726 void drm_fb_helper_fill_fix(struct fb_info
*info
, uint32_t pitch
,
1729 info
->fix
.type
= FB_TYPE_PACKED_PIXELS
;
1730 info
->fix
.visual
= depth
== 8 ? FB_VISUAL_PSEUDOCOLOR
:
1731 FB_VISUAL_TRUECOLOR
;
1732 info
->fix
.mmio_start
= 0;
1733 info
->fix
.mmio_len
= 0;
1734 info
->fix
.type_aux
= 0;
1735 info
->fix
.xpanstep
= 1; /* doing it in hw */
1736 info
->fix
.ypanstep
= 1; /* doing it in hw */
1737 info
->fix
.ywrapstep
= 0;
1738 info
->fix
.accel
= FB_ACCEL_NONE
;
1740 info
->fix
.line_length
= pitch
;
1742 EXPORT_SYMBOL(drm_fb_helper_fill_fix
);
1745 * drm_fb_helper_fill_var - initalizes variable fbdev information
1746 * @info: fbdev instance to set up
1747 * @fb_helper: fb helper instance to use as template
1748 * @fb_width: desired fb width
1749 * @fb_height: desired fb height
1751 * Sets up the variable fbdev metainformation from the given fb helper instance
1752 * and the drm framebuffer allocated in &drm_fb_helper.fb.
1754 * Drivers should call this (or their equivalent setup code) from their
1755 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
1756 * backing storage framebuffer.
1758 void drm_fb_helper_fill_var(struct fb_info
*info
, struct drm_fb_helper
*fb_helper
,
1759 uint32_t fb_width
, uint32_t fb_height
)
1761 struct drm_framebuffer
*fb
= fb_helper
->fb
;
1763 info
->pseudo_palette
= fb_helper
->pseudo_palette
;
1764 info
->var
.xres_virtual
= fb
->width
;
1765 info
->var
.yres_virtual
= fb
->height
;
1766 info
->var
.bits_per_pixel
= fb
->format
->cpp
[0] * 8;
1767 info
->var
.accel_flags
= FB_ACCELF_TEXT
;
1768 info
->var
.xoffset
= 0;
1769 info
->var
.yoffset
= 0;
1770 info
->var
.activate
= FB_ACTIVATE_NOW
;
1771 info
->var
.height
= -1;
1772 info
->var
.width
= -1;
1774 switch (fb
->format
->depth
) {
1776 info
->var
.red
.offset
= 0;
1777 info
->var
.green
.offset
= 0;
1778 info
->var
.blue
.offset
= 0;
1779 info
->var
.red
.length
= 8; /* 8bit DAC */
1780 info
->var
.green
.length
= 8;
1781 info
->var
.blue
.length
= 8;
1782 info
->var
.transp
.offset
= 0;
1783 info
->var
.transp
.length
= 0;
1786 info
->var
.red
.offset
= 10;
1787 info
->var
.green
.offset
= 5;
1788 info
->var
.blue
.offset
= 0;
1789 info
->var
.red
.length
= 5;
1790 info
->var
.green
.length
= 5;
1791 info
->var
.blue
.length
= 5;
1792 info
->var
.transp
.offset
= 15;
1793 info
->var
.transp
.length
= 1;
1796 info
->var
.red
.offset
= 11;
1797 info
->var
.green
.offset
= 5;
1798 info
->var
.blue
.offset
= 0;
1799 info
->var
.red
.length
= 5;
1800 info
->var
.green
.length
= 6;
1801 info
->var
.blue
.length
= 5;
1802 info
->var
.transp
.offset
= 0;
1805 info
->var
.red
.offset
= 16;
1806 info
->var
.green
.offset
= 8;
1807 info
->var
.blue
.offset
= 0;
1808 info
->var
.red
.length
= 8;
1809 info
->var
.green
.length
= 8;
1810 info
->var
.blue
.length
= 8;
1811 info
->var
.transp
.offset
= 0;
1812 info
->var
.transp
.length
= 0;
1815 info
->var
.red
.offset
= 16;
1816 info
->var
.green
.offset
= 8;
1817 info
->var
.blue
.offset
= 0;
1818 info
->var
.red
.length
= 8;
1819 info
->var
.green
.length
= 8;
1820 info
->var
.blue
.length
= 8;
1821 info
->var
.transp
.offset
= 24;
1822 info
->var
.transp
.length
= 8;
1828 info
->var
.xres
= fb_width
;
1829 info
->var
.yres
= fb_height
;
1831 EXPORT_SYMBOL(drm_fb_helper_fill_var
);
1833 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper
*fb_helper
,
1837 struct drm_connector
*connector
;
1841 drm_fb_helper_for_each_connector(fb_helper
, i
) {
1842 connector
= fb_helper
->connector_info
[i
]->connector
;
1843 count
+= connector
->funcs
->fill_modes(connector
, maxX
, maxY
);
1849 struct drm_display_mode
*drm_has_preferred_mode(struct drm_fb_helper_connector
*fb_connector
, int width
, int height
)
1851 struct drm_display_mode
*mode
;
1853 list_for_each_entry(mode
, &fb_connector
->connector
->modes
, head
) {
1854 if (mode
->hdisplay
> width
||
1855 mode
->vdisplay
> height
)
1857 if (mode
->type
& DRM_MODE_TYPE_PREFERRED
)
1862 EXPORT_SYMBOL(drm_has_preferred_mode
);
1864 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector
*fb_connector
)
1866 return fb_connector
->connector
->cmdline_mode
.specified
;
1869 struct drm_display_mode
*drm_pick_cmdline_mode(struct drm_fb_helper_connector
*fb_helper_conn
)
1871 struct drm_cmdline_mode
*cmdline_mode
;
1872 struct drm_display_mode
*mode
;
1873 bool prefer_non_interlace
;
1875 cmdline_mode
= &fb_helper_conn
->connector
->cmdline_mode
;
1876 if (cmdline_mode
->specified
== false)
1879 /* attempt to find a matching mode in the list of modes
1880 * we have gotten so far, if not add a CVT mode that conforms
1882 if (cmdline_mode
->rb
|| cmdline_mode
->margins
)
1885 prefer_non_interlace
= !cmdline_mode
->interlace
;
1887 list_for_each_entry(mode
, &fb_helper_conn
->connector
->modes
, head
) {
1888 /* check width/height */
1889 if (mode
->hdisplay
!= cmdline_mode
->xres
||
1890 mode
->vdisplay
!= cmdline_mode
->yres
)
1893 if (cmdline_mode
->refresh_specified
) {
1894 if (mode
->vrefresh
!= cmdline_mode
->refresh
)
1898 if (cmdline_mode
->interlace
) {
1899 if (!(mode
->flags
& DRM_MODE_FLAG_INTERLACE
))
1901 } else if (prefer_non_interlace
) {
1902 if (mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
1908 if (prefer_non_interlace
) {
1909 prefer_non_interlace
= false;
1914 mode
= drm_mode_create_from_cmdline_mode(fb_helper_conn
->connector
->dev
,
1916 list_add(&mode
->head
, &fb_helper_conn
->connector
->modes
);
1919 EXPORT_SYMBOL(drm_pick_cmdline_mode
);
1921 static bool drm_connector_enabled(struct drm_connector
*connector
, bool strict
)
1926 enable
= connector
->status
== connector_status_connected
;
1928 enable
= connector
->status
!= connector_status_disconnected
;
1933 static void drm_enable_connectors(struct drm_fb_helper
*fb_helper
,
1936 bool any_enabled
= false;
1937 struct drm_connector
*connector
;
1940 drm_fb_helper_for_each_connector(fb_helper
, i
) {
1941 connector
= fb_helper
->connector_info
[i
]->connector
;
1942 enabled
[i
] = drm_connector_enabled(connector
, true);
1943 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector
->base
.id
,
1944 enabled
[i
] ? "yes" : "no");
1945 any_enabled
|= enabled
[i
];
1951 drm_fb_helper_for_each_connector(fb_helper
, i
) {
1952 connector
= fb_helper
->connector_info
[i
]->connector
;
1953 enabled
[i
] = drm_connector_enabled(connector
, false);
1957 static bool drm_target_cloned(struct drm_fb_helper
*fb_helper
,
1958 struct drm_display_mode
**modes
,
1959 struct drm_fb_offset
*offsets
,
1960 bool *enabled
, int width
, int height
)
1963 bool can_clone
= false;
1964 struct drm_fb_helper_connector
*fb_helper_conn
;
1965 struct drm_display_mode
*dmt_mode
, *mode
;
1967 /* only contemplate cloning in the single crtc case */
1968 if (fb_helper
->crtc_count
> 1)
1972 drm_fb_helper_for_each_connector(fb_helper
, i
) {
1977 /* only contemplate cloning if more than one connector is enabled */
1981 /* check the command line or if nothing common pick 1024x768 */
1983 drm_fb_helper_for_each_connector(fb_helper
, i
) {
1986 fb_helper_conn
= fb_helper
->connector_info
[i
];
1987 modes
[i
] = drm_pick_cmdline_mode(fb_helper_conn
);
1992 for (j
= 0; j
< i
; j
++) {
1995 if (!drm_mode_equal(modes
[j
], modes
[i
]))
2001 DRM_DEBUG_KMS("can clone using command line\n");
2005 /* try and find a 1024x768 mode on each connector */
2007 dmt_mode
= drm_mode_find_dmt(fb_helper
->dev
, 1024, 768, 60, false);
2009 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2013 fb_helper_conn
= fb_helper
->connector_info
[i
];
2014 list_for_each_entry(mode
, &fb_helper_conn
->connector
->modes
, head
) {
2015 if (drm_mode_equal(mode
, dmt_mode
))
2023 DRM_DEBUG_KMS("can clone using 1024x768\n");
2026 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
2030 static int drm_get_tile_offsets(struct drm_fb_helper
*fb_helper
,
2031 struct drm_display_mode
**modes
,
2032 struct drm_fb_offset
*offsets
,
2034 int h_idx
, int v_idx
)
2036 struct drm_fb_helper_connector
*fb_helper_conn
;
2038 int hoffset
= 0, voffset
= 0;
2040 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2041 fb_helper_conn
= fb_helper
->connector_info
[i
];
2042 if (!fb_helper_conn
->connector
->has_tile
)
2045 if (!modes
[i
] && (h_idx
|| v_idx
)) {
2046 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i
,
2047 fb_helper_conn
->connector
->base
.id
);
2050 if (fb_helper_conn
->connector
->tile_h_loc
< h_idx
)
2051 hoffset
+= modes
[i
]->hdisplay
;
2053 if (fb_helper_conn
->connector
->tile_v_loc
< v_idx
)
2054 voffset
+= modes
[i
]->vdisplay
;
2056 offsets
[idx
].x
= hoffset
;
2057 offsets
[idx
].y
= voffset
;
2058 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset
, voffset
, h_idx
, v_idx
);
2062 static bool drm_target_preferred(struct drm_fb_helper
*fb_helper
,
2063 struct drm_display_mode
**modes
,
2064 struct drm_fb_offset
*offsets
,
2065 bool *enabled
, int width
, int height
)
2067 struct drm_fb_helper_connector
*fb_helper_conn
;
2068 const u64 mask
= BIT_ULL(fb_helper
->connector_count
) - 1;
2069 u64 conn_configured
= 0;
2074 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2075 fb_helper_conn
= fb_helper
->connector_info
[i
];
2077 if (conn_configured
& BIT_ULL(i
))
2080 if (enabled
[i
] == false) {
2081 conn_configured
|= BIT_ULL(i
);
2085 /* first pass over all the untiled connectors */
2086 if (tile_pass
== 0 && fb_helper_conn
->connector
->has_tile
)
2089 if (tile_pass
== 1) {
2090 if (fb_helper_conn
->connector
->tile_h_loc
!= 0 ||
2091 fb_helper_conn
->connector
->tile_v_loc
!= 0)
2095 if (fb_helper_conn
->connector
->tile_h_loc
!= tile_pass
- 1 &&
2096 fb_helper_conn
->connector
->tile_v_loc
!= tile_pass
- 1)
2097 /* if this tile_pass doesn't cover any of the tiles - keep going */
2101 * find the tile offsets for this pass - need to find
2102 * all tiles left and above
2104 drm_get_tile_offsets(fb_helper
, modes
, offsets
,
2105 i
, fb_helper_conn
->connector
->tile_h_loc
, fb_helper_conn
->connector
->tile_v_loc
);
2107 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
2108 fb_helper_conn
->connector
->base
.id
);
2110 /* got for command line mode first */
2111 modes
[i
] = drm_pick_cmdline_mode(fb_helper_conn
);
2113 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
2114 fb_helper_conn
->connector
->base
.id
, fb_helper_conn
->connector
->tile_group
? fb_helper_conn
->connector
->tile_group
->id
: 0);
2115 modes
[i
] = drm_has_preferred_mode(fb_helper_conn
, width
, height
);
2117 /* No preferred modes, pick one off the list */
2118 if (!modes
[i
] && !list_empty(&fb_helper_conn
->connector
->modes
)) {
2119 list_for_each_entry(modes
[i
], &fb_helper_conn
->connector
->modes
, head
)
2122 DRM_DEBUG_KMS("found mode %s\n", modes
[i
] ? modes
[i
]->name
:
2124 conn_configured
|= BIT_ULL(i
);
2127 if ((conn_configured
& mask
) != mask
) {
2134 static int drm_pick_crtcs(struct drm_fb_helper
*fb_helper
,
2135 struct drm_fb_helper_crtc
**best_crtcs
,
2136 struct drm_display_mode
**modes
,
2137 int n
, int width
, int height
)
2140 struct drm_connector
*connector
;
2141 const struct drm_connector_helper_funcs
*connector_funcs
;
2142 struct drm_encoder
*encoder
;
2143 int my_score
, best_score
, score
;
2144 struct drm_fb_helper_crtc
**crtcs
, *crtc
;
2145 struct drm_fb_helper_connector
*fb_helper_conn
;
2147 if (n
== fb_helper
->connector_count
)
2150 fb_helper_conn
= fb_helper
->connector_info
[n
];
2151 connector
= fb_helper_conn
->connector
;
2153 best_crtcs
[n
] = NULL
;
2154 best_score
= drm_pick_crtcs(fb_helper
, best_crtcs
, modes
, n
+1, width
, height
);
2155 if (modes
[n
] == NULL
)
2158 crtcs
= kzalloc(fb_helper
->connector_count
*
2159 sizeof(struct drm_fb_helper_crtc
*), GFP_KERNEL
);
2164 if (connector
->status
== connector_status_connected
)
2166 if (drm_has_cmdline_mode(fb_helper_conn
))
2168 if (drm_has_preferred_mode(fb_helper_conn
, width
, height
))
2171 connector_funcs
= connector
->helper_private
;
2174 * If the DRM device implements atomic hooks and ->best_encoder() is
2175 * NULL we fallback to the default drm_atomic_helper_best_encoder()
2178 if (drm_drv_uses_atomic_modeset(fb_helper
->dev
) &&
2179 !connector_funcs
->best_encoder
)
2180 encoder
= drm_atomic_helper_best_encoder(connector
);
2182 encoder
= connector_funcs
->best_encoder(connector
);
2188 * select a crtc for this connector and then attempt to configure
2189 * remaining connectors
2191 for (c
= 0; c
< fb_helper
->crtc_count
; c
++) {
2192 crtc
= &fb_helper
->crtc_info
[c
];
2194 if ((encoder
->possible_crtcs
& (1 << c
)) == 0)
2197 for (o
= 0; o
< n
; o
++)
2198 if (best_crtcs
[o
] == crtc
)
2202 /* ignore cloning unless only a single crtc */
2203 if (fb_helper
->crtc_count
> 1)
2206 if (!drm_mode_equal(modes
[o
], modes
[n
]))
2211 memcpy(crtcs
, best_crtcs
, n
* sizeof(struct drm_fb_helper_crtc
*));
2212 score
= my_score
+ drm_pick_crtcs(fb_helper
, crtcs
, modes
, n
+ 1,
2214 if (score
> best_score
) {
2216 memcpy(best_crtcs
, crtcs
,
2217 fb_helper
->connector_count
*
2218 sizeof(struct drm_fb_helper_crtc
*));
2226 static void drm_setup_crtcs(struct drm_fb_helper
*fb_helper
,
2227 u32 width
, u32 height
)
2229 struct drm_device
*dev
= fb_helper
->dev
;
2230 struct drm_fb_helper_crtc
**crtcs
;
2231 struct drm_display_mode
**modes
;
2232 struct drm_fb_offset
*offsets
;
2236 DRM_DEBUG_KMS("\n");
2237 if (drm_fb_helper_probe_connector_modes(fb_helper
, width
, height
) == 0)
2238 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
2240 /* prevent concurrent modification of connector_count by hotplug */
2241 lockdep_assert_held(&fb_helper
->dev
->mode_config
.mutex
);
2243 crtcs
= kcalloc(fb_helper
->connector_count
,
2244 sizeof(struct drm_fb_helper_crtc
*), GFP_KERNEL
);
2245 modes
= kcalloc(fb_helper
->connector_count
,
2246 sizeof(struct drm_display_mode
*), GFP_KERNEL
);
2247 offsets
= kcalloc(fb_helper
->connector_count
,
2248 sizeof(struct drm_fb_offset
), GFP_KERNEL
);
2249 enabled
= kcalloc(fb_helper
->connector_count
,
2250 sizeof(bool), GFP_KERNEL
);
2251 if (!crtcs
|| !modes
|| !enabled
|| !offsets
) {
2252 DRM_ERROR("Memory allocation failed\n");
2256 drm_enable_connectors(fb_helper
, enabled
);
2258 if (!(fb_helper
->funcs
->initial_config
&&
2259 fb_helper
->funcs
->initial_config(fb_helper
, crtcs
, modes
,
2261 enabled
, width
, height
))) {
2262 memset(modes
, 0, fb_helper
->connector_count
*sizeof(modes
[0]));
2263 memset(crtcs
, 0, fb_helper
->connector_count
*sizeof(crtcs
[0]));
2264 memset(offsets
, 0, fb_helper
->connector_count
*sizeof(offsets
[0]));
2266 if (!drm_target_cloned(fb_helper
, modes
, offsets
,
2267 enabled
, width
, height
) &&
2268 !drm_target_preferred(fb_helper
, modes
, offsets
,
2269 enabled
, width
, height
))
2270 DRM_ERROR("Unable to find initial modes\n");
2272 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2275 drm_pick_crtcs(fb_helper
, crtcs
, modes
, 0, width
, height
);
2278 /* need to set the modesets up here for use later */
2279 /* fill out the connector<->crtc mappings into the modesets */
2280 for (i
= 0; i
< fb_helper
->crtc_count
; i
++)
2281 drm_fb_helper_modeset_release(fb_helper
,
2282 &fb_helper
->crtc_info
[i
].mode_set
);
2284 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2285 struct drm_display_mode
*mode
= modes
[i
];
2286 struct drm_fb_helper_crtc
*fb_crtc
= crtcs
[i
];
2287 struct drm_fb_offset
*offset
= &offsets
[i
];
2288 struct drm_mode_set
*modeset
= &fb_crtc
->mode_set
;
2290 if (mode
&& fb_crtc
) {
2291 struct drm_connector
*connector
=
2292 fb_helper
->connector_info
[i
]->connector
;
2294 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2295 mode
->name
, fb_crtc
->mode_set
.crtc
->base
.id
, offset
->x
, offset
->y
);
2297 fb_crtc
->desired_mode
= mode
;
2298 fb_crtc
->x
= offset
->x
;
2299 fb_crtc
->y
= offset
->y
;
2300 modeset
->mode
= drm_mode_duplicate(dev
,
2301 fb_crtc
->desired_mode
);
2302 drm_connector_get(connector
);
2303 modeset
->connectors
[modeset
->num_connectors
++] = connector
;
2304 modeset
->fb
= fb_helper
->fb
;
2305 modeset
->x
= offset
->x
;
2306 modeset
->y
= offset
->y
;
2317 * drm_fb_helper_initial_config - setup a sane initial connector configuration
2318 * @fb_helper: fb_helper device struct
2319 * @bpp_sel: bpp value to use for the framebuffer configuration
2321 * Scans the CRTCs and connectors and tries to put together an initial setup.
2322 * At the moment, this is a cloned configuration across all heads with
2323 * a new framebuffer object as the backing store.
2325 * Note that this also registers the fbdev and so allows userspace to call into
2326 * the driver through the fbdev interfaces.
2328 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
2329 * to let the driver allocate and initialize the fbdev info structure and the
2330 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
2331 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
2332 * values for the fbdev info structure.
2336 * When you have fbcon support built-in or already loaded, this function will do
2337 * a full modeset to setup the fbdev console. Due to locking misdesign in the
2338 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2339 * console_lock. Until console_unlock is called no dmesg lines will be sent out
2340 * to consoles, not even serial console. This means when your driver crashes,
2341 * you will see absolutely nothing else but a system stuck in this function,
2342 * with no further output. Any kind of printk() you place within your own driver
2343 * or in the drm core modeset code will also never show up.
2345 * Standard debug practice is to run the fbcon setup without taking the
2346 * console_lock as a hack, to be able to see backtraces and crashes on the
2347 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2350 * The other option is to just disable fbdev emulation since very likely the
2351 * first modeset from userspace will crash in the same way, and is even easier
2352 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
2353 * kernel cmdline option.
2356 * Zero if everything went ok, nonzero otherwise.
2358 int drm_fb_helper_initial_config(struct drm_fb_helper
*fb_helper
, int bpp_sel
)
2360 struct drm_device
*dev
= fb_helper
->dev
;
2361 struct fb_info
*info
;
2364 if (!drm_fbdev_emulation
)
2367 mutex_lock(&dev
->mode_config
.mutex
);
2368 drm_setup_crtcs(fb_helper
,
2369 dev
->mode_config
.max_width
,
2370 dev
->mode_config
.max_height
);
2371 ret
= drm_fb_helper_single_fb_probe(fb_helper
, bpp_sel
);
2372 mutex_unlock(&dev
->mode_config
.mutex
);
2376 info
= fb_helper
->fbdev
;
2377 info
->var
.pixclock
= 0;
2378 ret
= register_framebuffer(info
);
2382 dev_info(dev
->dev
, "fb%d: %s frame buffer device\n",
2383 info
->node
, info
->fix
.id
);
2385 mutex_lock(&kernel_fb_helper_lock
);
2386 if (list_empty(&kernel_fb_helper_list
))
2387 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op
);
2389 list_add(&fb_helper
->kernel_fb_list
, &kernel_fb_helper_list
);
2390 mutex_unlock(&kernel_fb_helper_lock
);
2394 EXPORT_SYMBOL(drm_fb_helper_initial_config
);
2397 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
2398 * probing all the outputs attached to the fb
2399 * @fb_helper: the drm_fb_helper
2401 * Scan the connectors attached to the fb_helper and try to put together a
2402 * setup after notification of a change in output configuration.
2404 * Called at runtime, takes the mode config locks to be able to check/change the
2405 * modeset configuration. Must be run from process context (which usually means
2406 * either the output polling work or a work item launched from the driver's
2407 * hotplug interrupt).
2409 * Note that drivers may call this even before calling
2410 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
2411 * for a race-free fbcon setup and will make sure that the fbdev emulation will
2412 * not miss any hotplug events.
2415 * 0 on success and a non-zero error code otherwise.
2417 int drm_fb_helper_hotplug_event(struct drm_fb_helper
*fb_helper
)
2419 struct drm_device
*dev
= fb_helper
->dev
;
2421 if (!drm_fbdev_emulation
)
2424 mutex_lock(&dev
->mode_config
.mutex
);
2425 if (!fb_helper
->fb
|| !drm_fb_helper_is_bound(fb_helper
)) {
2426 fb_helper
->delayed_hotplug
= true;
2427 mutex_unlock(&dev
->mode_config
.mutex
);
2430 DRM_DEBUG_KMS("\n");
2432 drm_setup_crtcs(fb_helper
, fb_helper
->fb
->width
, fb_helper
->fb
->height
);
2434 mutex_unlock(&dev
->mode_config
.mutex
);
2436 drm_fb_helper_set_par(fb_helper
->fbdev
);
2440 EXPORT_SYMBOL(drm_fb_helper_hotplug_event
);
2442 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
2443 * but the module doesn't depend on any fb console symbols. At least
2444 * attempt to load fbcon to avoid leaving the system without a usable console.
2446 int __init
drm_fb_helper_modinit(void)
2448 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
2449 const char name
[] = "fbcon";
2450 struct module
*fbcon
;
2452 mutex_lock(&module_mutex
);
2453 fbcon
= find_module(name
);
2454 mutex_unlock(&module_mutex
);
2457 request_module_nowait(name
);
2461 EXPORT_SYMBOL(drm_fb_helper_modinit
);