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__++)
113 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
115 * @fb_helper: fbdev initialized with drm_fb_helper_init
117 * This functions adds all the available connectors for use with the given
118 * fb_helper. This is a separate step to allow drivers to freely assign
119 * connectors to the fbdev, e.g. if some are reserved for special purposes or
120 * not adequate to be used for the fbcon.
122 * This function is protected against concurrent connector hotadds/removals
123 * using drm_fb_helper_add_one_connector() and
124 * drm_fb_helper_remove_one_connector().
126 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper
*fb_helper
)
128 struct drm_device
*dev
= fb_helper
->dev
;
129 struct drm_connector
*connector
;
130 struct drm_connector_list_iter conn_iter
;
133 if (!drm_fbdev_emulation
)
136 mutex_lock(&dev
->mode_config
.mutex
);
137 drm_connector_list_iter_get(dev
, &conn_iter
);
138 drm_for_each_connector_iter(connector
, &conn_iter
) {
139 ret
= drm_fb_helper_add_one_connector(fb_helper
, connector
);
147 drm_fb_helper_for_each_connector(fb_helper
, i
) {
148 struct drm_fb_helper_connector
*fb_helper_connector
=
149 fb_helper
->connector_info
[i
];
151 drm_connector_put(fb_helper_connector
->connector
);
153 kfree(fb_helper_connector
);
154 fb_helper
->connector_info
[i
] = NULL
;
156 fb_helper
->connector_count
= 0;
158 drm_connector_list_iter_put(&conn_iter
);
159 mutex_unlock(&dev
->mode_config
.mutex
);
163 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors
);
165 int drm_fb_helper_add_one_connector(struct drm_fb_helper
*fb_helper
, struct drm_connector
*connector
)
167 struct drm_fb_helper_connector
**temp
;
168 struct drm_fb_helper_connector
*fb_helper_connector
;
170 if (!drm_fbdev_emulation
)
173 WARN_ON(!mutex_is_locked(&fb_helper
->dev
->mode_config
.mutex
));
174 if (fb_helper
->connector_count
+ 1 > fb_helper
->connector_info_alloc_count
) {
175 temp
= krealloc(fb_helper
->connector_info
, sizeof(struct drm_fb_helper_connector
*) * (fb_helper
->connector_count
+ 1), GFP_KERNEL
);
179 fb_helper
->connector_info_alloc_count
= fb_helper
->connector_count
+ 1;
180 fb_helper
->connector_info
= temp
;
184 fb_helper_connector
= kzalloc(sizeof(struct drm_fb_helper_connector
), GFP_KERNEL
);
185 if (!fb_helper_connector
)
188 drm_connector_get(connector
);
189 fb_helper_connector
->connector
= connector
;
190 fb_helper
->connector_info
[fb_helper
->connector_count
++] = fb_helper_connector
;
193 EXPORT_SYMBOL(drm_fb_helper_add_one_connector
);
195 int drm_fb_helper_remove_one_connector(struct drm_fb_helper
*fb_helper
,
196 struct drm_connector
*connector
)
198 struct drm_fb_helper_connector
*fb_helper_connector
;
201 if (!drm_fbdev_emulation
)
204 WARN_ON(!mutex_is_locked(&fb_helper
->dev
->mode_config
.mutex
));
206 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
207 if (fb_helper
->connector_info
[i
]->connector
== connector
)
211 if (i
== fb_helper
->connector_count
)
213 fb_helper_connector
= fb_helper
->connector_info
[i
];
214 drm_connector_put(fb_helper_connector
->connector
);
216 for (j
= i
+ 1; j
< fb_helper
->connector_count
; j
++) {
217 fb_helper
->connector_info
[j
- 1] = fb_helper
->connector_info
[j
];
219 fb_helper
->connector_count
--;
220 kfree(fb_helper_connector
);
224 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector
);
226 static void drm_fb_helper_save_lut_atomic(struct drm_crtc
*crtc
, struct drm_fb_helper
*helper
)
228 uint16_t *r_base
, *g_base
, *b_base
;
231 if (helper
->funcs
->gamma_get
== NULL
)
234 r_base
= crtc
->gamma_store
;
235 g_base
= r_base
+ crtc
->gamma_size
;
236 b_base
= g_base
+ crtc
->gamma_size
;
238 for (i
= 0; i
< crtc
->gamma_size
; i
++)
239 helper
->funcs
->gamma_get(crtc
, &r_base
[i
], &g_base
[i
], &b_base
[i
], i
);
242 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc
*crtc
)
244 uint16_t *r_base
, *g_base
, *b_base
;
246 if (crtc
->funcs
->gamma_set
== NULL
)
249 r_base
= crtc
->gamma_store
;
250 g_base
= r_base
+ crtc
->gamma_size
;
251 b_base
= g_base
+ crtc
->gamma_size
;
253 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, crtc
->gamma_size
);
257 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
258 * @info: fbdev registered by the helper
260 int drm_fb_helper_debug_enter(struct fb_info
*info
)
262 struct drm_fb_helper
*helper
= info
->par
;
263 const struct drm_crtc_helper_funcs
*funcs
;
266 list_for_each_entry(helper
, &kernel_fb_helper_list
, kernel_fb_list
) {
267 for (i
= 0; i
< helper
->crtc_count
; i
++) {
268 struct drm_mode_set
*mode_set
=
269 &helper
->crtc_info
[i
].mode_set
;
271 if (!mode_set
->crtc
->enabled
)
274 funcs
= mode_set
->crtc
->helper_private
;
275 if (funcs
->mode_set_base_atomic
== NULL
)
278 drm_fb_helper_save_lut_atomic(mode_set
->crtc
, helper
);
279 funcs
->mode_set_base_atomic(mode_set
->crtc
,
283 ENTER_ATOMIC_MODE_SET
);
289 EXPORT_SYMBOL(drm_fb_helper_debug_enter
);
291 /* Find the real fb for a given fb helper CRTC */
292 static struct drm_framebuffer
*drm_mode_config_fb(struct drm_crtc
*crtc
)
294 struct drm_device
*dev
= crtc
->dev
;
297 drm_for_each_crtc(c
, dev
) {
298 if (crtc
->base
.id
== c
->base
.id
)
299 return c
->primary
->fb
;
306 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
307 * @info: fbdev registered by the helper
309 int drm_fb_helper_debug_leave(struct fb_info
*info
)
311 struct drm_fb_helper
*helper
= info
->par
;
312 struct drm_crtc
*crtc
;
313 const struct drm_crtc_helper_funcs
*funcs
;
314 struct drm_framebuffer
*fb
;
317 for (i
= 0; i
< helper
->crtc_count
; i
++) {
318 struct drm_mode_set
*mode_set
= &helper
->crtc_info
[i
].mode_set
;
319 crtc
= mode_set
->crtc
;
320 funcs
= crtc
->helper_private
;
321 fb
= drm_mode_config_fb(crtc
);
327 DRM_ERROR("no fb to restore??\n");
331 if (funcs
->mode_set_base_atomic
== NULL
)
334 drm_fb_helper_restore_lut_atomic(mode_set
->crtc
);
335 funcs
->mode_set_base_atomic(mode_set
->crtc
, fb
, crtc
->x
,
336 crtc
->y
, LEAVE_ATOMIC_MODE_SET
);
341 EXPORT_SYMBOL(drm_fb_helper_debug_leave
);
343 static int restore_fbdev_mode_atomic(struct drm_fb_helper
*fb_helper
)
345 struct drm_device
*dev
= fb_helper
->dev
;
346 struct drm_plane
*plane
;
347 struct drm_atomic_state
*state
;
351 state
= drm_atomic_state_alloc(dev
);
355 state
->acquire_ctx
= dev
->mode_config
.acquire_ctx
;
358 drm_for_each_plane(plane
, dev
) {
359 struct drm_plane_state
*plane_state
;
361 plane_state
= drm_atomic_get_plane_state(state
, plane
);
362 if (IS_ERR(plane_state
)) {
363 ret
= PTR_ERR(plane_state
);
367 plane_state
->rotation
= DRM_ROTATE_0
;
369 plane
->old_fb
= plane
->fb
;
370 plane_mask
|= 1 << drm_plane_index(plane
);
372 /* disable non-primary: */
373 if (plane
->type
== DRM_PLANE_TYPE_PRIMARY
)
376 ret
= __drm_atomic_helper_disable_plane(plane
, plane_state
);
381 for(i
= 0; i
< fb_helper
->crtc_count
; i
++) {
382 struct drm_mode_set
*mode_set
= &fb_helper
->crtc_info
[i
].mode_set
;
384 ret
= __drm_atomic_helper_set_config(mode_set
, state
);
389 ret
= drm_atomic_commit(state
);
392 drm_atomic_clean_old_fb(dev
, plane_mask
, ret
);
397 drm_atomic_state_put(state
);
401 drm_atomic_state_clear(state
);
402 drm_atomic_legacy_backoff(state
);
407 static int restore_fbdev_mode(struct drm_fb_helper
*fb_helper
)
409 struct drm_device
*dev
= fb_helper
->dev
;
410 struct drm_plane
*plane
;
413 drm_warn_on_modeset_not_all_locked(dev
);
415 if (drm_drv_uses_atomic_modeset(dev
))
416 return restore_fbdev_mode_atomic(fb_helper
);
418 drm_for_each_plane(plane
, dev
) {
419 if (plane
->type
!= DRM_PLANE_TYPE_PRIMARY
)
420 drm_plane_force_disable(plane
);
422 if (plane
->rotation_property
)
423 drm_mode_plane_set_obj_prop(plane
,
424 plane
->rotation_property
,
428 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
429 struct drm_mode_set
*mode_set
= &fb_helper
->crtc_info
[i
].mode_set
;
430 struct drm_crtc
*crtc
= mode_set
->crtc
;
433 if (crtc
->funcs
->cursor_set2
) {
434 ret
= crtc
->funcs
->cursor_set2(crtc
, NULL
, 0, 0, 0, 0, 0);
437 } else if (crtc
->funcs
->cursor_set
) {
438 ret
= crtc
->funcs
->cursor_set(crtc
, NULL
, 0, 0, 0);
443 ret
= drm_mode_set_config_internal(mode_set
);
452 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
453 * @fb_helper: fbcon to restore
455 * This should be called from driver's drm &drm_driver.lastclose callback
456 * when implementing an fbcon on top of kms using this helper. This ensures that
457 * the user isn't greeted with a black screen when e.g. X dies.
460 * Zero if everything went ok, negative error code otherwise.
462 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper
*fb_helper
)
464 struct drm_device
*dev
= fb_helper
->dev
;
468 if (!drm_fbdev_emulation
)
471 drm_modeset_lock_all(dev
);
472 ret
= restore_fbdev_mode(fb_helper
);
474 do_delayed
= fb_helper
->delayed_hotplug
;
476 fb_helper
->delayed_hotplug
= false;
477 drm_modeset_unlock_all(dev
);
480 drm_fb_helper_hotplug_event(fb_helper
);
483 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked
);
485 static bool drm_fb_helper_is_bound(struct drm_fb_helper
*fb_helper
)
487 struct drm_device
*dev
= fb_helper
->dev
;
488 struct drm_crtc
*crtc
;
489 int bound
= 0, crtcs_bound
= 0;
491 /* Sometimes user space wants everything disabled, so don't steal the
492 * display if there's a master. */
493 if (READ_ONCE(dev
->master
))
496 drm_for_each_crtc(crtc
, dev
) {
497 if (crtc
->primary
->fb
)
499 if (crtc
->primary
->fb
== fb_helper
->fb
)
503 if (bound
< crtcs_bound
)
509 #ifdef CONFIG_MAGIC_SYSRQ
511 * restore fbcon display for all kms driver's using this helper, used for sysrq
512 * and panic handling.
514 static bool drm_fb_helper_force_kernel_mode(void)
516 bool ret
, error
= false;
517 struct drm_fb_helper
*helper
;
519 if (list_empty(&kernel_fb_helper_list
))
522 list_for_each_entry(helper
, &kernel_fb_helper_list
, kernel_fb_list
) {
523 struct drm_device
*dev
= helper
->dev
;
525 if (dev
->switch_power_state
== DRM_SWITCH_POWER_OFF
)
528 drm_modeset_lock_all(dev
);
529 ret
= restore_fbdev_mode(helper
);
532 drm_modeset_unlock_all(dev
);
537 static void drm_fb_helper_restore_work_fn(struct work_struct
*ignored
)
540 ret
= drm_fb_helper_force_kernel_mode();
542 DRM_ERROR("Failed to restore crtc configuration\n");
544 static DECLARE_WORK(drm_fb_helper_restore_work
, drm_fb_helper_restore_work_fn
);
546 static void drm_fb_helper_sysrq(int dummy1
)
548 schedule_work(&drm_fb_helper_restore_work
);
551 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op
= {
552 .handler
= drm_fb_helper_sysrq
,
553 .help_msg
= "force-fb(V)",
554 .action_msg
= "Restore framebuffer console",
557 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op
= { };
560 static void drm_fb_helper_dpms(struct fb_info
*info
, int dpms_mode
)
562 struct drm_fb_helper
*fb_helper
= info
->par
;
563 struct drm_device
*dev
= fb_helper
->dev
;
564 struct drm_crtc
*crtc
;
565 struct drm_connector
*connector
;
569 * For each CRTC in this fb, turn the connectors on/off.
571 drm_modeset_lock_all(dev
);
572 if (!drm_fb_helper_is_bound(fb_helper
)) {
573 drm_modeset_unlock_all(dev
);
577 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
578 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
583 /* Walk the connectors & encoders on this fb turning them on/off */
584 drm_fb_helper_for_each_connector(fb_helper
, j
) {
585 connector
= fb_helper
->connector_info
[j
]->connector
;
586 connector
->funcs
->dpms(connector
, dpms_mode
);
587 drm_object_property_set_value(&connector
->base
,
588 dev
->mode_config
.dpms_property
, dpms_mode
);
591 drm_modeset_unlock_all(dev
);
595 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
596 * @blank: desired blanking state
597 * @info: fbdev registered by the helper
599 int drm_fb_helper_blank(int blank
, struct fb_info
*info
)
601 if (oops_in_progress
)
605 /* Display: On; HSync: On, VSync: On */
606 case FB_BLANK_UNBLANK
:
607 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_ON
);
609 /* Display: Off; HSync: On, VSync: On */
610 case FB_BLANK_NORMAL
:
611 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_STANDBY
);
613 /* Display: Off; HSync: Off, VSync: On */
614 case FB_BLANK_HSYNC_SUSPEND
:
615 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_STANDBY
);
617 /* Display: Off; HSync: On, VSync: Off */
618 case FB_BLANK_VSYNC_SUSPEND
:
619 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_SUSPEND
);
621 /* Display: Off; HSync: Off, VSync: Off */
622 case FB_BLANK_POWERDOWN
:
623 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_OFF
);
628 EXPORT_SYMBOL(drm_fb_helper_blank
);
630 static void drm_fb_helper_modeset_release(struct drm_fb_helper
*helper
,
631 struct drm_mode_set
*modeset
)
635 for (i
= 0; i
< modeset
->num_connectors
; i
++) {
636 drm_connector_put(modeset
->connectors
[i
]);
637 modeset
->connectors
[i
] = NULL
;
639 modeset
->num_connectors
= 0;
641 drm_mode_destroy(helper
->dev
, modeset
->mode
);
642 modeset
->mode
= NULL
;
644 /* FIXME should hold a ref? */
648 static void drm_fb_helper_crtc_free(struct drm_fb_helper
*helper
)
652 for (i
= 0; i
< helper
->connector_count
; i
++) {
653 drm_connector_put(helper
->connector_info
[i
]->connector
);
654 kfree(helper
->connector_info
[i
]);
656 kfree(helper
->connector_info
);
658 for (i
= 0; i
< helper
->crtc_count
; i
++) {
659 struct drm_mode_set
*modeset
= &helper
->crtc_info
[i
].mode_set
;
661 drm_fb_helper_modeset_release(helper
, modeset
);
662 kfree(modeset
->connectors
);
664 kfree(helper
->crtc_info
);
667 static void drm_fb_helper_resume_worker(struct work_struct
*work
)
669 struct drm_fb_helper
*helper
= container_of(work
, struct drm_fb_helper
,
673 fb_set_suspend(helper
->fbdev
, 0);
677 static void drm_fb_helper_dirty_work(struct work_struct
*work
)
679 struct drm_fb_helper
*helper
= container_of(work
, struct drm_fb_helper
,
681 struct drm_clip_rect
*clip
= &helper
->dirty_clip
;
682 struct drm_clip_rect clip_copy
;
685 spin_lock_irqsave(&helper
->dirty_lock
, flags
);
687 clip
->x1
= clip
->y1
= ~0;
688 clip
->x2
= clip
->y2
= 0;
689 spin_unlock_irqrestore(&helper
->dirty_lock
, flags
);
691 /* call dirty callback only when it has been really touched */
692 if (clip_copy
.x1
< clip_copy
.x2
&& clip_copy
.y1
< clip_copy
.y2
)
693 helper
->fb
->funcs
->dirty(helper
->fb
, NULL
, 0, 0, &clip_copy
, 1);
697 * drm_fb_helper_prepare - setup a drm_fb_helper structure
699 * @helper: driver-allocated fbdev helper structure to set up
700 * @funcs: pointer to structure of functions associate with this helper
702 * Sets up the bare minimum to make the framebuffer helper usable. This is
703 * useful to implement race-free initialization of the polling helpers.
705 void drm_fb_helper_prepare(struct drm_device
*dev
, struct drm_fb_helper
*helper
,
706 const struct drm_fb_helper_funcs
*funcs
)
708 INIT_LIST_HEAD(&helper
->kernel_fb_list
);
709 spin_lock_init(&helper
->dirty_lock
);
710 INIT_WORK(&helper
->resume_work
, drm_fb_helper_resume_worker
);
711 INIT_WORK(&helper
->dirty_work
, drm_fb_helper_dirty_work
);
712 helper
->dirty_clip
.x1
= helper
->dirty_clip
.y1
= ~0;
713 helper
->funcs
= funcs
;
716 EXPORT_SYMBOL(drm_fb_helper_prepare
);
719 * drm_fb_helper_init - initialize a &struct drm_fb_helper
721 * @fb_helper: driver-allocated fbdev helper structure to initialize
722 * @max_conn_count: max connector count
724 * This allocates the structures for the fbdev helper with the given limits.
725 * Note that this won't yet touch the hardware (through the driver interfaces)
726 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
727 * to allow driver writes more control over the exact init sequence.
729 * Drivers must call drm_fb_helper_prepare() before calling this function.
732 * Zero if everything went ok, nonzero otherwise.
734 int drm_fb_helper_init(struct drm_device
*dev
,
735 struct drm_fb_helper
*fb_helper
,
738 struct drm_crtc
*crtc
;
739 struct drm_mode_config
*config
= &dev
->mode_config
;
742 if (!drm_fbdev_emulation
)
748 fb_helper
->crtc_info
= kcalloc(config
->num_crtc
, sizeof(struct drm_fb_helper_crtc
), GFP_KERNEL
);
749 if (!fb_helper
->crtc_info
)
752 fb_helper
->crtc_count
= config
->num_crtc
;
753 fb_helper
->connector_info
= kcalloc(dev
->mode_config
.num_connector
, sizeof(struct drm_fb_helper_connector
*), GFP_KERNEL
);
754 if (!fb_helper
->connector_info
) {
755 kfree(fb_helper
->crtc_info
);
758 fb_helper
->connector_info_alloc_count
= dev
->mode_config
.num_connector
;
759 fb_helper
->connector_count
= 0;
761 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
762 fb_helper
->crtc_info
[i
].mode_set
.connectors
=
763 kcalloc(max_conn_count
,
764 sizeof(struct drm_connector
*),
767 if (!fb_helper
->crtc_info
[i
].mode_set
.connectors
)
769 fb_helper
->crtc_info
[i
].mode_set
.num_connectors
= 0;
773 drm_for_each_crtc(crtc
, dev
) {
774 fb_helper
->crtc_info
[i
].mode_set
.crtc
= crtc
;
780 drm_fb_helper_crtc_free(fb_helper
);
783 EXPORT_SYMBOL(drm_fb_helper_init
);
786 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
787 * @fb_helper: driver-allocated fbdev helper
789 * A helper to alloc fb_info and the members cmap and apertures. Called
790 * by the driver within the fb_probe fb_helper callback function. Drivers do not
791 * need to release the allocated fb_info structure themselves, this is
792 * automatically done when calling drm_fb_helper_fini().
795 * fb_info pointer if things went okay, pointer containing error code
798 struct fb_info
*drm_fb_helper_alloc_fbi(struct drm_fb_helper
*fb_helper
)
800 struct device
*dev
= fb_helper
->dev
->dev
;
801 struct fb_info
*info
;
804 info
= framebuffer_alloc(0, dev
);
806 return ERR_PTR(-ENOMEM
);
808 ret
= fb_alloc_cmap(&info
->cmap
, 256, 0);
812 info
->apertures
= alloc_apertures(1);
813 if (!info
->apertures
) {
818 fb_helper
->fbdev
= info
;
823 fb_dealloc_cmap(&info
->cmap
);
825 framebuffer_release(info
);
828 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi
);
831 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
832 * @fb_helper: driver-allocated fbdev helper
834 * A wrapper around unregister_framebuffer, to release the fb_info
835 * framebuffer device. This must be called before releasing all resources for
836 * @fb_helper by calling drm_fb_helper_fini().
838 void drm_fb_helper_unregister_fbi(struct drm_fb_helper
*fb_helper
)
840 if (fb_helper
&& fb_helper
->fbdev
)
841 unregister_framebuffer(fb_helper
->fbdev
);
843 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi
);
846 * drm_fb_helper_fini - finialize a &struct drm_fb_helper
847 * @fb_helper: driver-allocated fbdev helper
849 * This cleans up all remaining resources associated with @fb_helper. Must be
850 * called after drm_fb_helper_unlink_fbi() was called.
852 void drm_fb_helper_fini(struct drm_fb_helper
*fb_helper
)
854 struct fb_info
*info
;
856 if (!drm_fbdev_emulation
|| !fb_helper
)
859 info
= fb_helper
->fbdev
;
862 fb_dealloc_cmap(&info
->cmap
);
863 framebuffer_release(info
);
865 fb_helper
->fbdev
= NULL
;
867 cancel_work_sync(&fb_helper
->resume_work
);
868 cancel_work_sync(&fb_helper
->dirty_work
);
870 mutex_lock(&kernel_fb_helper_lock
);
871 if (!list_empty(&fb_helper
->kernel_fb_list
)) {
872 list_del(&fb_helper
->kernel_fb_list
);
873 if (list_empty(&kernel_fb_helper_list
)) {
874 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op
);
877 mutex_unlock(&kernel_fb_helper_lock
);
879 drm_fb_helper_crtc_free(fb_helper
);
882 EXPORT_SYMBOL(drm_fb_helper_fini
);
885 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
886 * @fb_helper: driver-allocated fbdev helper
888 * A wrapper around unlink_framebuffer implemented by fbdev core
890 void drm_fb_helper_unlink_fbi(struct drm_fb_helper
*fb_helper
)
892 if (fb_helper
&& fb_helper
->fbdev
)
893 unlink_framebuffer(fb_helper
->fbdev
);
895 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi
);
897 static void drm_fb_helper_dirty(struct fb_info
*info
, u32 x
, u32 y
,
898 u32 width
, u32 height
)
900 struct drm_fb_helper
*helper
= info
->par
;
901 struct drm_clip_rect
*clip
= &helper
->dirty_clip
;
904 if (!helper
->fb
->funcs
->dirty
)
907 spin_lock_irqsave(&helper
->dirty_lock
, flags
);
908 clip
->x1
= min_t(u32
, clip
->x1
, x
);
909 clip
->y1
= min_t(u32
, clip
->y1
, y
);
910 clip
->x2
= max_t(u32
, clip
->x2
, x
+ width
);
911 clip
->y2
= max_t(u32
, clip
->y2
, y
+ height
);
912 spin_unlock_irqrestore(&helper
->dirty_lock
, flags
);
914 schedule_work(&helper
->dirty_work
);
918 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
919 * @info: fb_info struct pointer
920 * @pagelist: list of dirty mmap framebuffer pages
922 * This function is used as the &fb_deferred_io.deferred_io
923 * callback function for flushing the fbdev mmap writes.
925 void drm_fb_helper_deferred_io(struct fb_info
*info
,
926 struct list_head
*pagelist
)
928 unsigned long start
, end
, min
, max
;
934 list_for_each_entry(page
, pagelist
, lru
) {
935 start
= page
->index
<< PAGE_SHIFT
;
936 end
= start
+ PAGE_SIZE
- 1;
937 min
= min(min
, start
);
942 y1
= min
/ info
->fix
.line_length
;
943 y2
= min_t(u32
, DIV_ROUND_UP(max
, info
->fix
.line_length
),
945 drm_fb_helper_dirty(info
, 0, y1
, info
->var
.xres
, y2
- y1
);
948 EXPORT_SYMBOL(drm_fb_helper_deferred_io
);
951 * drm_fb_helper_sys_read - wrapper around fb_sys_read
952 * @info: fb_info struct pointer
953 * @buf: userspace buffer to read from framebuffer memory
954 * @count: number of bytes to read from framebuffer memory
955 * @ppos: read offset within framebuffer memory
957 * A wrapper around fb_sys_read implemented by fbdev core
959 ssize_t
drm_fb_helper_sys_read(struct fb_info
*info
, char __user
*buf
,
960 size_t count
, loff_t
*ppos
)
962 return fb_sys_read(info
, buf
, count
, ppos
);
964 EXPORT_SYMBOL(drm_fb_helper_sys_read
);
967 * drm_fb_helper_sys_write - wrapper around fb_sys_write
968 * @info: fb_info struct pointer
969 * @buf: userspace buffer to write to framebuffer memory
970 * @count: number of bytes to write to framebuffer memory
971 * @ppos: write offset within framebuffer memory
973 * A wrapper around fb_sys_write implemented by fbdev core
975 ssize_t
drm_fb_helper_sys_write(struct fb_info
*info
, const char __user
*buf
,
976 size_t count
, loff_t
*ppos
)
980 ret
= fb_sys_write(info
, buf
, count
, ppos
);
982 drm_fb_helper_dirty(info
, 0, 0, info
->var
.xres
,
987 EXPORT_SYMBOL(drm_fb_helper_sys_write
);
990 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
991 * @info: fbdev registered by the helper
992 * @rect: info about rectangle to fill
994 * A wrapper around sys_fillrect implemented by fbdev core
996 void drm_fb_helper_sys_fillrect(struct fb_info
*info
,
997 const struct fb_fillrect
*rect
)
999 sys_fillrect(info
, rect
);
1000 drm_fb_helper_dirty(info
, rect
->dx
, rect
->dy
,
1001 rect
->width
, rect
->height
);
1003 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect
);
1006 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1007 * @info: fbdev registered by the helper
1008 * @area: info about area to copy
1010 * A wrapper around sys_copyarea implemented by fbdev core
1012 void drm_fb_helper_sys_copyarea(struct fb_info
*info
,
1013 const struct fb_copyarea
*area
)
1015 sys_copyarea(info
, area
);
1016 drm_fb_helper_dirty(info
, area
->dx
, area
->dy
,
1017 area
->width
, area
->height
);
1019 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea
);
1022 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1023 * @info: fbdev registered by the helper
1024 * @image: info about image to blit
1026 * A wrapper around sys_imageblit implemented by fbdev core
1028 void drm_fb_helper_sys_imageblit(struct fb_info
*info
,
1029 const struct fb_image
*image
)
1031 sys_imageblit(info
, image
);
1032 drm_fb_helper_dirty(info
, image
->dx
, image
->dy
,
1033 image
->width
, image
->height
);
1035 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit
);
1038 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1039 * @info: fbdev registered by the helper
1040 * @rect: info about rectangle to fill
1042 * A wrapper around cfb_imageblit implemented by fbdev core
1044 void drm_fb_helper_cfb_fillrect(struct fb_info
*info
,
1045 const struct fb_fillrect
*rect
)
1047 cfb_fillrect(info
, rect
);
1048 drm_fb_helper_dirty(info
, rect
->dx
, rect
->dy
,
1049 rect
->width
, rect
->height
);
1051 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect
);
1054 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1055 * @info: fbdev registered by the helper
1056 * @area: info about area to copy
1058 * A wrapper around cfb_copyarea implemented by fbdev core
1060 void drm_fb_helper_cfb_copyarea(struct fb_info
*info
,
1061 const struct fb_copyarea
*area
)
1063 cfb_copyarea(info
, area
);
1064 drm_fb_helper_dirty(info
, area
->dx
, area
->dy
,
1065 area
->width
, area
->height
);
1067 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea
);
1070 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1071 * @info: fbdev registered by the helper
1072 * @image: info about image to blit
1074 * A wrapper around cfb_imageblit implemented by fbdev core
1076 void drm_fb_helper_cfb_imageblit(struct fb_info
*info
,
1077 const struct fb_image
*image
)
1079 cfb_imageblit(info
, image
);
1080 drm_fb_helper_dirty(info
, image
->dx
, image
->dy
,
1081 image
->width
, image
->height
);
1083 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit
);
1086 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
1087 * @fb_helper: driver-allocated fbdev helper
1088 * @suspend: whether to suspend or resume
1090 * A wrapper around fb_set_suspend implemented by fbdev core.
1091 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
1094 void drm_fb_helper_set_suspend(struct drm_fb_helper
*fb_helper
, bool suspend
)
1096 if (fb_helper
&& fb_helper
->fbdev
)
1097 fb_set_suspend(fb_helper
->fbdev
, suspend
);
1099 EXPORT_SYMBOL(drm_fb_helper_set_suspend
);
1102 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
1103 * takes the console lock
1104 * @fb_helper: driver-allocated fbdev helper
1105 * @suspend: whether to suspend or resume
1107 * A wrapper around fb_set_suspend() that takes the console lock. If the lock
1108 * isn't available on resume, a worker is tasked with waiting for the lock
1109 * to become available. The console lock can be pretty contented on resume
1110 * due to all the printk activity.
1112 * This function can be called multiple times with the same state since
1113 * &fb_info.state is checked to see if fbdev is running or not before locking.
1115 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
1117 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper
*fb_helper
,
1120 if (!fb_helper
|| !fb_helper
->fbdev
)
1123 /* make sure there's no pending/ongoing resume */
1124 flush_work(&fb_helper
->resume_work
);
1127 if (fb_helper
->fbdev
->state
!= FBINFO_STATE_RUNNING
)
1133 if (fb_helper
->fbdev
->state
== FBINFO_STATE_RUNNING
)
1136 if (!console_trylock()) {
1137 schedule_work(&fb_helper
->resume_work
);
1142 fb_set_suspend(fb_helper
->fbdev
, suspend
);
1145 EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked
);
1147 static int setcolreg(struct drm_crtc
*crtc
, u16 red
, u16 green
,
1148 u16 blue
, u16 regno
, struct fb_info
*info
)
1150 struct drm_fb_helper
*fb_helper
= info
->par
;
1151 struct drm_framebuffer
*fb
= fb_helper
->fb
;
1153 if (info
->fix
.visual
== FB_VISUAL_TRUECOLOR
) {
1156 /* place color in psuedopalette */
1159 palette
= (u32
*)info
->pseudo_palette
;
1160 red
>>= (16 - info
->var
.red
.length
);
1161 green
>>= (16 - info
->var
.green
.length
);
1162 blue
>>= (16 - info
->var
.blue
.length
);
1163 value
= (red
<< info
->var
.red
.offset
) |
1164 (green
<< info
->var
.green
.offset
) |
1165 (blue
<< info
->var
.blue
.offset
);
1166 if (info
->var
.transp
.length
> 0) {
1167 u32 mask
= (1 << info
->var
.transp
.length
) - 1;
1168 mask
<<= info
->var
.transp
.offset
;
1171 palette
[regno
] = value
;
1176 * The driver really shouldn't advertise pseudo/directcolor
1177 * visuals if it can't deal with the palette.
1179 if (WARN_ON(!fb_helper
->funcs
->gamma_set
||
1180 !fb_helper
->funcs
->gamma_get
))
1183 WARN_ON(fb
->format
->cpp
[0] != 1);
1185 fb_helper
->funcs
->gamma_set(crtc
, red
, green
, blue
, regno
);
1191 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
1192 * @cmap: cmap to set
1193 * @info: fbdev registered by the helper
1195 int drm_fb_helper_setcmap(struct fb_cmap
*cmap
, struct fb_info
*info
)
1197 struct drm_fb_helper
*fb_helper
= info
->par
;
1198 struct drm_device
*dev
= fb_helper
->dev
;
1199 const struct drm_crtc_helper_funcs
*crtc_funcs
;
1200 u16
*red
, *green
, *blue
, *transp
;
1201 struct drm_crtc
*crtc
;
1205 if (oops_in_progress
)
1208 drm_modeset_lock_all(dev
);
1209 if (!drm_fb_helper_is_bound(fb_helper
)) {
1210 drm_modeset_unlock_all(dev
);
1214 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1215 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
1216 crtc_funcs
= crtc
->helper_private
;
1219 green
= cmap
->green
;
1221 transp
= cmap
->transp
;
1222 start
= cmap
->start
;
1224 for (j
= 0; j
< cmap
->len
; j
++) {
1225 u16 hred
, hgreen
, hblue
, htransp
= 0xffff;
1232 htransp
= *transp
++;
1234 rc
= setcolreg(crtc
, hred
, hgreen
, hblue
, start
++, info
);
1238 if (crtc_funcs
->load_lut
)
1239 crtc_funcs
->load_lut(crtc
);
1242 drm_modeset_unlock_all(dev
);
1245 EXPORT_SYMBOL(drm_fb_helper_setcmap
);
1248 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
1249 * @var: screeninfo to check
1250 * @info: fbdev registered by the helper
1252 int drm_fb_helper_check_var(struct fb_var_screeninfo
*var
,
1253 struct fb_info
*info
)
1255 struct drm_fb_helper
*fb_helper
= info
->par
;
1256 struct drm_framebuffer
*fb
= fb_helper
->fb
;
1259 if (var
->pixclock
!= 0 || in_dbg_master())
1263 * Changes struct fb_var_screeninfo are currently not pushed back
1264 * to KMS, hence fail if different settings are requested.
1266 if (var
->bits_per_pixel
!= fb
->format
->cpp
[0] * 8 ||
1267 var
->xres
!= fb
->width
|| var
->yres
!= fb
->height
||
1268 var
->xres_virtual
!= fb
->width
|| var
->yres_virtual
!= fb
->height
) {
1269 DRM_DEBUG("fb userspace requested width/height/bpp different than current fb "
1270 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1271 var
->xres
, var
->yres
, var
->bits_per_pixel
,
1272 var
->xres_virtual
, var
->yres_virtual
,
1273 fb
->width
, fb
->height
, fb
->format
->cpp
[0] * 8);
1277 switch (var
->bits_per_pixel
) {
1279 depth
= (var
->green
.length
== 6) ? 16 : 15;
1282 depth
= (var
->transp
.length
> 0) ? 32 : 24;
1285 depth
= var
->bits_per_pixel
;
1291 var
->red
.offset
= 0;
1292 var
->green
.offset
= 0;
1293 var
->blue
.offset
= 0;
1294 var
->red
.length
= 8;
1295 var
->green
.length
= 8;
1296 var
->blue
.length
= 8;
1297 var
->transp
.length
= 0;
1298 var
->transp
.offset
= 0;
1301 var
->red
.offset
= 10;
1302 var
->green
.offset
= 5;
1303 var
->blue
.offset
= 0;
1304 var
->red
.length
= 5;
1305 var
->green
.length
= 5;
1306 var
->blue
.length
= 5;
1307 var
->transp
.length
= 1;
1308 var
->transp
.offset
= 15;
1311 var
->red
.offset
= 11;
1312 var
->green
.offset
= 5;
1313 var
->blue
.offset
= 0;
1314 var
->red
.length
= 5;
1315 var
->green
.length
= 6;
1316 var
->blue
.length
= 5;
1317 var
->transp
.length
= 0;
1318 var
->transp
.offset
= 0;
1321 var
->red
.offset
= 16;
1322 var
->green
.offset
= 8;
1323 var
->blue
.offset
= 0;
1324 var
->red
.length
= 8;
1325 var
->green
.length
= 8;
1326 var
->blue
.length
= 8;
1327 var
->transp
.length
= 0;
1328 var
->transp
.offset
= 0;
1331 var
->red
.offset
= 16;
1332 var
->green
.offset
= 8;
1333 var
->blue
.offset
= 0;
1334 var
->red
.length
= 8;
1335 var
->green
.length
= 8;
1336 var
->blue
.length
= 8;
1337 var
->transp
.length
= 8;
1338 var
->transp
.offset
= 24;
1345 EXPORT_SYMBOL(drm_fb_helper_check_var
);
1348 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
1349 * @info: fbdev registered by the helper
1351 * This will let fbcon do the mode init and is called at initialization time by
1352 * the fbdev core when registering the driver, and later on through the hotplug
1355 int drm_fb_helper_set_par(struct fb_info
*info
)
1357 struct drm_fb_helper
*fb_helper
= info
->par
;
1358 struct fb_var_screeninfo
*var
= &info
->var
;
1360 if (oops_in_progress
)
1363 if (var
->pixclock
!= 0) {
1364 DRM_ERROR("PIXEL CLOCK SET\n");
1368 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper
);
1372 EXPORT_SYMBOL(drm_fb_helper_set_par
);
1374 static int pan_display_atomic(struct fb_var_screeninfo
*var
,
1375 struct fb_info
*info
)
1377 struct drm_fb_helper
*fb_helper
= info
->par
;
1378 struct drm_device
*dev
= fb_helper
->dev
;
1379 struct drm_atomic_state
*state
;
1380 struct drm_plane
*plane
;
1382 unsigned plane_mask
;
1384 state
= drm_atomic_state_alloc(dev
);
1388 state
->acquire_ctx
= dev
->mode_config
.acquire_ctx
;
1391 for(i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1392 struct drm_mode_set
*mode_set
;
1394 mode_set
= &fb_helper
->crtc_info
[i
].mode_set
;
1396 mode_set
->x
= var
->xoffset
;
1397 mode_set
->y
= var
->yoffset
;
1399 ret
= __drm_atomic_helper_set_config(mode_set
, state
);
1403 plane
= mode_set
->crtc
->primary
;
1404 plane_mask
|= (1 << drm_plane_index(plane
));
1405 plane
->old_fb
= plane
->fb
;
1408 ret
= drm_atomic_commit(state
);
1412 info
->var
.xoffset
= var
->xoffset
;
1413 info
->var
.yoffset
= var
->yoffset
;
1416 drm_atomic_clean_old_fb(dev
, plane_mask
, ret
);
1418 if (ret
== -EDEADLK
)
1421 drm_atomic_state_put(state
);
1425 drm_atomic_state_clear(state
);
1426 drm_atomic_legacy_backoff(state
);
1432 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1433 * @var: updated screen information
1434 * @info: fbdev registered by the helper
1436 int drm_fb_helper_pan_display(struct fb_var_screeninfo
*var
,
1437 struct fb_info
*info
)
1439 struct drm_fb_helper
*fb_helper
= info
->par
;
1440 struct drm_device
*dev
= fb_helper
->dev
;
1441 struct drm_mode_set
*modeset
;
1445 if (oops_in_progress
)
1448 drm_modeset_lock_all(dev
);
1449 if (!drm_fb_helper_is_bound(fb_helper
)) {
1450 drm_modeset_unlock_all(dev
);
1454 if (drm_drv_uses_atomic_modeset(dev
)) {
1455 ret
= pan_display_atomic(var
, info
);
1459 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1460 modeset
= &fb_helper
->crtc_info
[i
].mode_set
;
1462 modeset
->x
= var
->xoffset
;
1463 modeset
->y
= var
->yoffset
;
1465 if (modeset
->num_connectors
) {
1466 ret
= drm_mode_set_config_internal(modeset
);
1468 info
->var
.xoffset
= var
->xoffset
;
1469 info
->var
.yoffset
= var
->yoffset
;
1474 drm_modeset_unlock_all(dev
);
1477 EXPORT_SYMBOL(drm_fb_helper_pan_display
);
1480 * Allocates the backing storage and sets up the fbdev info structure through
1481 * the ->fb_probe callback and then registers the fbdev and sets up the panic
1484 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper
*fb_helper
,
1490 struct drm_fb_helper_surface_size sizes
;
1493 memset(&sizes
, 0, sizeof(struct drm_fb_helper_surface_size
));
1494 sizes
.surface_depth
= 24;
1495 sizes
.surface_bpp
= 32;
1496 sizes
.fb_width
= (unsigned)-1;
1497 sizes
.fb_height
= (unsigned)-1;
1499 /* if driver picks 8 or 16 by default use that
1500 for both depth/bpp */
1501 if (preferred_bpp
!= sizes
.surface_bpp
)
1502 sizes
.surface_depth
= sizes
.surface_bpp
= preferred_bpp
;
1504 /* first up get a count of crtcs now in use and new min/maxes width/heights */
1505 drm_fb_helper_for_each_connector(fb_helper
, i
) {
1506 struct drm_fb_helper_connector
*fb_helper_conn
= fb_helper
->connector_info
[i
];
1507 struct drm_cmdline_mode
*cmdline_mode
;
1509 cmdline_mode
= &fb_helper_conn
->connector
->cmdline_mode
;
1511 if (cmdline_mode
->bpp_specified
) {
1512 switch (cmdline_mode
->bpp
) {
1514 sizes
.surface_depth
= sizes
.surface_bpp
= 8;
1517 sizes
.surface_depth
= 15;
1518 sizes
.surface_bpp
= 16;
1521 sizes
.surface_depth
= sizes
.surface_bpp
= 16;
1524 sizes
.surface_depth
= sizes
.surface_bpp
= 24;
1527 sizes
.surface_depth
= 24;
1528 sizes
.surface_bpp
= 32;
1536 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1537 struct drm_display_mode
*desired_mode
;
1538 struct drm_mode_set
*mode_set
;
1540 /* in case of tile group, are we the last tile vert or horiz?
1541 * If no tile group you are always the last one both vertically
1544 bool lastv
= true, lasth
= true;
1546 desired_mode
= fb_helper
->crtc_info
[i
].desired_mode
;
1547 mode_set
= &fb_helper
->crtc_info
[i
].mode_set
;
1554 x
= fb_helper
->crtc_info
[i
].x
;
1555 y
= fb_helper
->crtc_info
[i
].y
;
1557 if (gamma_size
== 0)
1558 gamma_size
= fb_helper
->crtc_info
[i
].mode_set
.crtc
->gamma_size
;
1560 sizes
.surface_width
= max_t(u32
, desired_mode
->hdisplay
+ x
, sizes
.surface_width
);
1561 sizes
.surface_height
= max_t(u32
, desired_mode
->vdisplay
+ y
, sizes
.surface_height
);
1563 for (j
= 0; j
< mode_set
->num_connectors
; j
++) {
1564 struct drm_connector
*connector
= mode_set
->connectors
[j
];
1565 if (connector
->has_tile
) {
1566 lasth
= (connector
->tile_h_loc
== (connector
->num_h_tile
- 1));
1567 lastv
= (connector
->tile_v_loc
== (connector
->num_v_tile
- 1));
1568 /* cloning to multiple tiles is just crazy-talk, so: */
1574 sizes
.fb_width
= min_t(u32
, desired_mode
->hdisplay
+ x
, sizes
.fb_width
);
1576 sizes
.fb_height
= min_t(u32
, desired_mode
->vdisplay
+ y
, sizes
.fb_height
);
1579 if (crtc_count
== 0 || sizes
.fb_width
== -1 || sizes
.fb_height
== -1) {
1580 /* hmm everyone went away - assume VGA cable just fell out
1581 and will come back later. */
1582 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
1583 sizes
.fb_width
= sizes
.surface_width
= 1024;
1584 sizes
.fb_height
= sizes
.surface_height
= 768;
1587 /* Handle our overallocation */
1588 sizes
.surface_height
*= drm_fbdev_overalloc
;
1589 sizes
.surface_height
/= 100;
1591 /* push down into drivers */
1592 ret
= (*fb_helper
->funcs
->fb_probe
)(fb_helper
, &sizes
);
1597 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1598 * events, but at init time drm_setup_crtcs needs to be called before
1599 * the fb is allocated (since we need to figure out the desired size of
1600 * the fb before we can allocate it ...). Hence we need to fix things up
1603 for (i
= 0; i
< fb_helper
->crtc_count
; i
++)
1604 if (fb_helper
->crtc_info
[i
].mode_set
.num_connectors
)
1605 fb_helper
->crtc_info
[i
].mode_set
.fb
= fb_helper
->fb
;
1611 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1612 * @info: fbdev registered by the helper
1613 * @pitch: desired pitch
1614 * @depth: desired depth
1616 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1617 * fbdev emulations. Drivers which support acceleration methods which impose
1618 * additional constraints need to set up their own limits.
1620 * Drivers should call this (or their equivalent setup code) from their
1621 * &drm_fb_helper_funcs.fb_probe callback.
1623 void drm_fb_helper_fill_fix(struct fb_info
*info
, uint32_t pitch
,
1626 info
->fix
.type
= FB_TYPE_PACKED_PIXELS
;
1627 info
->fix
.visual
= depth
== 8 ? FB_VISUAL_PSEUDOCOLOR
:
1628 FB_VISUAL_TRUECOLOR
;
1629 info
->fix
.mmio_start
= 0;
1630 info
->fix
.mmio_len
= 0;
1631 info
->fix
.type_aux
= 0;
1632 info
->fix
.xpanstep
= 1; /* doing it in hw */
1633 info
->fix
.ypanstep
= 1; /* doing it in hw */
1634 info
->fix
.ywrapstep
= 0;
1635 info
->fix
.accel
= FB_ACCEL_NONE
;
1637 info
->fix
.line_length
= pitch
;
1640 EXPORT_SYMBOL(drm_fb_helper_fill_fix
);
1643 * drm_fb_helper_fill_var - initalizes variable fbdev information
1644 * @info: fbdev instance to set up
1645 * @fb_helper: fb helper instance to use as template
1646 * @fb_width: desired fb width
1647 * @fb_height: desired fb height
1649 * Sets up the variable fbdev metainformation from the given fb helper instance
1650 * and the drm framebuffer allocated in &drm_fb_helper.fb.
1652 * Drivers should call this (or their equivalent setup code) from their
1653 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
1654 * backing storage framebuffer.
1656 void drm_fb_helper_fill_var(struct fb_info
*info
, struct drm_fb_helper
*fb_helper
,
1657 uint32_t fb_width
, uint32_t fb_height
)
1659 struct drm_framebuffer
*fb
= fb_helper
->fb
;
1660 info
->pseudo_palette
= fb_helper
->pseudo_palette
;
1661 info
->var
.xres_virtual
= fb
->width
;
1662 info
->var
.yres_virtual
= fb
->height
;
1663 info
->var
.bits_per_pixel
= fb
->format
->cpp
[0] * 8;
1664 info
->var
.accel_flags
= FB_ACCELF_TEXT
;
1665 info
->var
.xoffset
= 0;
1666 info
->var
.yoffset
= 0;
1667 info
->var
.activate
= FB_ACTIVATE_NOW
;
1668 info
->var
.height
= -1;
1669 info
->var
.width
= -1;
1671 switch (fb
->format
->depth
) {
1673 info
->var
.red
.offset
= 0;
1674 info
->var
.green
.offset
= 0;
1675 info
->var
.blue
.offset
= 0;
1676 info
->var
.red
.length
= 8; /* 8bit DAC */
1677 info
->var
.green
.length
= 8;
1678 info
->var
.blue
.length
= 8;
1679 info
->var
.transp
.offset
= 0;
1680 info
->var
.transp
.length
= 0;
1683 info
->var
.red
.offset
= 10;
1684 info
->var
.green
.offset
= 5;
1685 info
->var
.blue
.offset
= 0;
1686 info
->var
.red
.length
= 5;
1687 info
->var
.green
.length
= 5;
1688 info
->var
.blue
.length
= 5;
1689 info
->var
.transp
.offset
= 15;
1690 info
->var
.transp
.length
= 1;
1693 info
->var
.red
.offset
= 11;
1694 info
->var
.green
.offset
= 5;
1695 info
->var
.blue
.offset
= 0;
1696 info
->var
.red
.length
= 5;
1697 info
->var
.green
.length
= 6;
1698 info
->var
.blue
.length
= 5;
1699 info
->var
.transp
.offset
= 0;
1702 info
->var
.red
.offset
= 16;
1703 info
->var
.green
.offset
= 8;
1704 info
->var
.blue
.offset
= 0;
1705 info
->var
.red
.length
= 8;
1706 info
->var
.green
.length
= 8;
1707 info
->var
.blue
.length
= 8;
1708 info
->var
.transp
.offset
= 0;
1709 info
->var
.transp
.length
= 0;
1712 info
->var
.red
.offset
= 16;
1713 info
->var
.green
.offset
= 8;
1714 info
->var
.blue
.offset
= 0;
1715 info
->var
.red
.length
= 8;
1716 info
->var
.green
.length
= 8;
1717 info
->var
.blue
.length
= 8;
1718 info
->var
.transp
.offset
= 24;
1719 info
->var
.transp
.length
= 8;
1725 info
->var
.xres
= fb_width
;
1726 info
->var
.yres
= fb_height
;
1728 EXPORT_SYMBOL(drm_fb_helper_fill_var
);
1730 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper
*fb_helper
,
1734 struct drm_connector
*connector
;
1738 drm_fb_helper_for_each_connector(fb_helper
, i
) {
1739 connector
= fb_helper
->connector_info
[i
]->connector
;
1740 count
+= connector
->funcs
->fill_modes(connector
, maxX
, maxY
);
1746 struct drm_display_mode
*drm_has_preferred_mode(struct drm_fb_helper_connector
*fb_connector
, int width
, int height
)
1748 struct drm_display_mode
*mode
;
1750 list_for_each_entry(mode
, &fb_connector
->connector
->modes
, head
) {
1751 if (mode
->hdisplay
> width
||
1752 mode
->vdisplay
> height
)
1754 if (mode
->type
& DRM_MODE_TYPE_PREFERRED
)
1759 EXPORT_SYMBOL(drm_has_preferred_mode
);
1761 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector
*fb_connector
)
1763 return fb_connector
->connector
->cmdline_mode
.specified
;
1766 struct drm_display_mode
*drm_pick_cmdline_mode(struct drm_fb_helper_connector
*fb_helper_conn
)
1768 struct drm_cmdline_mode
*cmdline_mode
;
1769 struct drm_display_mode
*mode
;
1770 bool prefer_non_interlace
;
1772 cmdline_mode
= &fb_helper_conn
->connector
->cmdline_mode
;
1773 if (cmdline_mode
->specified
== false)
1776 /* attempt to find a matching mode in the list of modes
1777 * we have gotten so far, if not add a CVT mode that conforms
1779 if (cmdline_mode
->rb
|| cmdline_mode
->margins
)
1782 prefer_non_interlace
= !cmdline_mode
->interlace
;
1784 list_for_each_entry(mode
, &fb_helper_conn
->connector
->modes
, head
) {
1785 /* check width/height */
1786 if (mode
->hdisplay
!= cmdline_mode
->xres
||
1787 mode
->vdisplay
!= cmdline_mode
->yres
)
1790 if (cmdline_mode
->refresh_specified
) {
1791 if (mode
->vrefresh
!= cmdline_mode
->refresh
)
1795 if (cmdline_mode
->interlace
) {
1796 if (!(mode
->flags
& DRM_MODE_FLAG_INTERLACE
))
1798 } else if (prefer_non_interlace
) {
1799 if (mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
1805 if (prefer_non_interlace
) {
1806 prefer_non_interlace
= false;
1811 mode
= drm_mode_create_from_cmdline_mode(fb_helper_conn
->connector
->dev
,
1813 list_add(&mode
->head
, &fb_helper_conn
->connector
->modes
);
1816 EXPORT_SYMBOL(drm_pick_cmdline_mode
);
1818 static bool drm_connector_enabled(struct drm_connector
*connector
, bool strict
)
1823 enable
= connector
->status
== connector_status_connected
;
1825 enable
= connector
->status
!= connector_status_disconnected
;
1830 static void drm_enable_connectors(struct drm_fb_helper
*fb_helper
,
1833 bool any_enabled
= false;
1834 struct drm_connector
*connector
;
1837 drm_fb_helper_for_each_connector(fb_helper
, i
) {
1838 connector
= fb_helper
->connector_info
[i
]->connector
;
1839 enabled
[i
] = drm_connector_enabled(connector
, true);
1840 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector
->base
.id
,
1841 enabled
[i
] ? "yes" : "no");
1842 any_enabled
|= enabled
[i
];
1848 drm_fb_helper_for_each_connector(fb_helper
, i
) {
1849 connector
= fb_helper
->connector_info
[i
]->connector
;
1850 enabled
[i
] = drm_connector_enabled(connector
, false);
1854 static bool drm_target_cloned(struct drm_fb_helper
*fb_helper
,
1855 struct drm_display_mode
**modes
,
1856 struct drm_fb_offset
*offsets
,
1857 bool *enabled
, int width
, int height
)
1860 bool can_clone
= false;
1861 struct drm_fb_helper_connector
*fb_helper_conn
;
1862 struct drm_display_mode
*dmt_mode
, *mode
;
1864 /* only contemplate cloning in the single crtc case */
1865 if (fb_helper
->crtc_count
> 1)
1869 drm_fb_helper_for_each_connector(fb_helper
, i
) {
1874 /* only contemplate cloning if more than one connector is enabled */
1878 /* check the command line or if nothing common pick 1024x768 */
1880 drm_fb_helper_for_each_connector(fb_helper
, i
) {
1883 fb_helper_conn
= fb_helper
->connector_info
[i
];
1884 modes
[i
] = drm_pick_cmdline_mode(fb_helper_conn
);
1889 for (j
= 0; j
< i
; j
++) {
1892 if (!drm_mode_equal(modes
[j
], modes
[i
]))
1898 DRM_DEBUG_KMS("can clone using command line\n");
1902 /* try and find a 1024x768 mode on each connector */
1904 dmt_mode
= drm_mode_find_dmt(fb_helper
->dev
, 1024, 768, 60, false);
1906 drm_fb_helper_for_each_connector(fb_helper
, i
) {
1910 fb_helper_conn
= fb_helper
->connector_info
[i
];
1911 list_for_each_entry(mode
, &fb_helper_conn
->connector
->modes
, head
) {
1912 if (drm_mode_equal(mode
, dmt_mode
))
1920 DRM_DEBUG_KMS("can clone using 1024x768\n");
1923 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1927 static int drm_get_tile_offsets(struct drm_fb_helper
*fb_helper
,
1928 struct drm_display_mode
**modes
,
1929 struct drm_fb_offset
*offsets
,
1931 int h_idx
, int v_idx
)
1933 struct drm_fb_helper_connector
*fb_helper_conn
;
1935 int hoffset
= 0, voffset
= 0;
1937 drm_fb_helper_for_each_connector(fb_helper
, i
) {
1938 fb_helper_conn
= fb_helper
->connector_info
[i
];
1939 if (!fb_helper_conn
->connector
->has_tile
)
1942 if (!modes
[i
] && (h_idx
|| v_idx
)) {
1943 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i
,
1944 fb_helper_conn
->connector
->base
.id
);
1947 if (fb_helper_conn
->connector
->tile_h_loc
< h_idx
)
1948 hoffset
+= modes
[i
]->hdisplay
;
1950 if (fb_helper_conn
->connector
->tile_v_loc
< v_idx
)
1951 voffset
+= modes
[i
]->vdisplay
;
1953 offsets
[idx
].x
= hoffset
;
1954 offsets
[idx
].y
= voffset
;
1955 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset
, voffset
, h_idx
, v_idx
);
1959 static bool drm_target_preferred(struct drm_fb_helper
*fb_helper
,
1960 struct drm_display_mode
**modes
,
1961 struct drm_fb_offset
*offsets
,
1962 bool *enabled
, int width
, int height
)
1964 struct drm_fb_helper_connector
*fb_helper_conn
;
1965 const u64 mask
= BIT_ULL(fb_helper
->connector_count
) - 1;
1966 u64 conn_configured
= 0;
1971 drm_fb_helper_for_each_connector(fb_helper
, i
) {
1972 fb_helper_conn
= fb_helper
->connector_info
[i
];
1974 if (conn_configured
& BIT_ULL(i
))
1977 if (enabled
[i
] == false) {
1978 conn_configured
|= BIT_ULL(i
);
1982 /* first pass over all the untiled connectors */
1983 if (tile_pass
== 0 && fb_helper_conn
->connector
->has_tile
)
1986 if (tile_pass
== 1) {
1987 if (fb_helper_conn
->connector
->tile_h_loc
!= 0 ||
1988 fb_helper_conn
->connector
->tile_v_loc
!= 0)
1992 if (fb_helper_conn
->connector
->tile_h_loc
!= tile_pass
-1 &&
1993 fb_helper_conn
->connector
->tile_v_loc
!= tile_pass
- 1)
1994 /* if this tile_pass doesn't cover any of the tiles - keep going */
1997 /* find the tile offsets for this pass - need
1998 to find all tiles left and above */
1999 drm_get_tile_offsets(fb_helper
, modes
, offsets
,
2000 i
, fb_helper_conn
->connector
->tile_h_loc
, fb_helper_conn
->connector
->tile_v_loc
);
2002 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
2003 fb_helper_conn
->connector
->base
.id
);
2005 /* got for command line mode first */
2006 modes
[i
] = drm_pick_cmdline_mode(fb_helper_conn
);
2008 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
2009 fb_helper_conn
->connector
->base
.id
, fb_helper_conn
->connector
->tile_group
? fb_helper_conn
->connector
->tile_group
->id
: 0);
2010 modes
[i
] = drm_has_preferred_mode(fb_helper_conn
, width
, height
);
2012 /* No preferred modes, pick one off the list */
2013 if (!modes
[i
] && !list_empty(&fb_helper_conn
->connector
->modes
)) {
2014 list_for_each_entry(modes
[i
], &fb_helper_conn
->connector
->modes
, head
)
2017 DRM_DEBUG_KMS("found mode %s\n", modes
[i
] ? modes
[i
]->name
:
2019 conn_configured
|= BIT_ULL(i
);
2022 if ((conn_configured
& mask
) != mask
) {
2029 static int drm_pick_crtcs(struct drm_fb_helper
*fb_helper
,
2030 struct drm_fb_helper_crtc
**best_crtcs
,
2031 struct drm_display_mode
**modes
,
2032 int n
, int width
, int height
)
2035 struct drm_connector
*connector
;
2036 const struct drm_connector_helper_funcs
*connector_funcs
;
2037 struct drm_encoder
*encoder
;
2038 int my_score
, best_score
, score
;
2039 struct drm_fb_helper_crtc
**crtcs
, *crtc
;
2040 struct drm_fb_helper_connector
*fb_helper_conn
;
2042 if (n
== fb_helper
->connector_count
)
2045 fb_helper_conn
= fb_helper
->connector_info
[n
];
2046 connector
= fb_helper_conn
->connector
;
2048 best_crtcs
[n
] = NULL
;
2049 best_score
= drm_pick_crtcs(fb_helper
, best_crtcs
, modes
, n
+1, width
, height
);
2050 if (modes
[n
] == NULL
)
2053 crtcs
= kzalloc(fb_helper
->connector_count
*
2054 sizeof(struct drm_fb_helper_crtc
*), GFP_KERNEL
);
2059 if (connector
->status
== connector_status_connected
)
2061 if (drm_has_cmdline_mode(fb_helper_conn
))
2063 if (drm_has_preferred_mode(fb_helper_conn
, width
, height
))
2066 connector_funcs
= connector
->helper_private
;
2069 * If the DRM device implements atomic hooks and ->best_encoder() is
2070 * NULL we fallback to the default drm_atomic_helper_best_encoder()
2073 if (drm_drv_uses_atomic_modeset(fb_helper
->dev
) &&
2074 !connector_funcs
->best_encoder
)
2075 encoder
= drm_atomic_helper_best_encoder(connector
);
2077 encoder
= connector_funcs
->best_encoder(connector
);
2082 /* select a crtc for this connector and then attempt to configure
2083 remaining connectors */
2084 for (c
= 0; c
< fb_helper
->crtc_count
; c
++) {
2085 crtc
= &fb_helper
->crtc_info
[c
];
2087 if ((encoder
->possible_crtcs
& (1 << c
)) == 0)
2090 for (o
= 0; o
< n
; o
++)
2091 if (best_crtcs
[o
] == crtc
)
2095 /* ignore cloning unless only a single crtc */
2096 if (fb_helper
->crtc_count
> 1)
2099 if (!drm_mode_equal(modes
[o
], modes
[n
]))
2104 memcpy(crtcs
, best_crtcs
, n
* sizeof(struct drm_fb_helper_crtc
*));
2105 score
= my_score
+ drm_pick_crtcs(fb_helper
, crtcs
, modes
, n
+ 1,
2107 if (score
> best_score
) {
2109 memcpy(best_crtcs
, crtcs
,
2110 fb_helper
->connector_count
*
2111 sizeof(struct drm_fb_helper_crtc
*));
2119 static void drm_setup_crtcs(struct drm_fb_helper
*fb_helper
,
2120 u32 width
, u32 height
)
2122 struct drm_device
*dev
= fb_helper
->dev
;
2123 struct drm_fb_helper_crtc
**crtcs
;
2124 struct drm_display_mode
**modes
;
2125 struct drm_fb_offset
*offsets
;
2129 DRM_DEBUG_KMS("\n");
2130 if (drm_fb_helper_probe_connector_modes(fb_helper
, width
, height
) == 0)
2131 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
2133 /* prevent concurrent modification of connector_count by hotplug */
2134 lockdep_assert_held(&fb_helper
->dev
->mode_config
.mutex
);
2136 crtcs
= kcalloc(fb_helper
->connector_count
,
2137 sizeof(struct drm_fb_helper_crtc
*), GFP_KERNEL
);
2138 modes
= kcalloc(fb_helper
->connector_count
,
2139 sizeof(struct drm_display_mode
*), GFP_KERNEL
);
2140 offsets
= kcalloc(fb_helper
->connector_count
,
2141 sizeof(struct drm_fb_offset
), GFP_KERNEL
);
2142 enabled
= kcalloc(fb_helper
->connector_count
,
2143 sizeof(bool), GFP_KERNEL
);
2144 if (!crtcs
|| !modes
|| !enabled
|| !offsets
) {
2145 DRM_ERROR("Memory allocation failed\n");
2149 drm_enable_connectors(fb_helper
, enabled
);
2151 if (!(fb_helper
->funcs
->initial_config
&&
2152 fb_helper
->funcs
->initial_config(fb_helper
, crtcs
, modes
,
2154 enabled
, width
, height
))) {
2155 memset(modes
, 0, fb_helper
->connector_count
*sizeof(modes
[0]));
2156 memset(crtcs
, 0, fb_helper
->connector_count
*sizeof(crtcs
[0]));
2157 memset(offsets
, 0, fb_helper
->connector_count
*sizeof(offsets
[0]));
2159 if (!drm_target_cloned(fb_helper
, modes
, offsets
,
2160 enabled
, width
, height
) &&
2161 !drm_target_preferred(fb_helper
, modes
, offsets
,
2162 enabled
, width
, height
))
2163 DRM_ERROR("Unable to find initial modes\n");
2165 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2168 drm_pick_crtcs(fb_helper
, crtcs
, modes
, 0, width
, height
);
2171 /* need to set the modesets up here for use later */
2172 /* fill out the connector<->crtc mappings into the modesets */
2173 for (i
= 0; i
< fb_helper
->crtc_count
; i
++)
2174 drm_fb_helper_modeset_release(fb_helper
,
2175 &fb_helper
->crtc_info
[i
].mode_set
);
2177 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2178 struct drm_display_mode
*mode
= modes
[i
];
2179 struct drm_fb_helper_crtc
*fb_crtc
= crtcs
[i
];
2180 struct drm_fb_offset
*offset
= &offsets
[i
];
2181 struct drm_mode_set
*modeset
= &fb_crtc
->mode_set
;
2183 if (mode
&& fb_crtc
) {
2184 struct drm_connector
*connector
=
2185 fb_helper
->connector_info
[i
]->connector
;
2187 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2188 mode
->name
, fb_crtc
->mode_set
.crtc
->base
.id
, offset
->x
, offset
->y
);
2190 fb_crtc
->desired_mode
= mode
;
2191 fb_crtc
->x
= offset
->x
;
2192 fb_crtc
->y
= offset
->y
;
2193 modeset
->mode
= drm_mode_duplicate(dev
,
2194 fb_crtc
->desired_mode
);
2195 drm_connector_get(connector
);
2196 modeset
->connectors
[modeset
->num_connectors
++] = connector
;
2197 modeset
->fb
= fb_helper
->fb
;
2198 modeset
->x
= offset
->x
;
2199 modeset
->y
= offset
->y
;
2210 * drm_fb_helper_initial_config - setup a sane initial connector configuration
2211 * @fb_helper: fb_helper device struct
2212 * @bpp_sel: bpp value to use for the framebuffer configuration
2214 * Scans the CRTCs and connectors and tries to put together an initial setup.
2215 * At the moment, this is a cloned configuration across all heads with
2216 * a new framebuffer object as the backing store.
2218 * Note that this also registers the fbdev and so allows userspace to call into
2219 * the driver through the fbdev interfaces.
2221 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
2222 * to let the driver allocate and initialize the fbdev info structure and the
2223 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
2224 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
2225 * values for the fbdev info structure.
2229 * When you have fbcon support built-in or already loaded, this function will do
2230 * a full modeset to setup the fbdev console. Due to locking misdesign in the
2231 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2232 * console_lock. Until console_unlock is called no dmesg lines will be sent out
2233 * to consoles, not even serial console. This means when your driver crashes,
2234 * you will see absolutely nothing else but a system stuck in this function,
2235 * with no further output. Any kind of printk() you place within your own driver
2236 * or in the drm core modeset code will also never show up.
2238 * Standard debug practice is to run the fbcon setup without taking the
2239 * console_lock as a hack, to be able to see backtraces and crashes on the
2240 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2243 * The other option is to just disable fbdev emulation since very likely the
2244 * first modeset from userspace will crash in the same way, and is even easier
2245 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
2246 * kernel cmdline option.
2249 * Zero if everything went ok, nonzero otherwise.
2251 int drm_fb_helper_initial_config(struct drm_fb_helper
*fb_helper
, int bpp_sel
)
2253 struct drm_device
*dev
= fb_helper
->dev
;
2254 struct fb_info
*info
;
2257 if (!drm_fbdev_emulation
)
2260 mutex_lock(&dev
->mode_config
.mutex
);
2261 drm_setup_crtcs(fb_helper
,
2262 dev
->mode_config
.max_width
,
2263 dev
->mode_config
.max_height
);
2264 ret
= drm_fb_helper_single_fb_probe(fb_helper
, bpp_sel
);
2265 mutex_unlock(&dev
->mode_config
.mutex
);
2269 info
= fb_helper
->fbdev
;
2270 info
->var
.pixclock
= 0;
2271 ret
= register_framebuffer(info
);
2275 dev_info(dev
->dev
, "fb%d: %s frame buffer device\n",
2276 info
->node
, info
->fix
.id
);
2278 mutex_lock(&kernel_fb_helper_lock
);
2279 if (list_empty(&kernel_fb_helper_list
))
2280 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op
);
2282 list_add(&fb_helper
->kernel_fb_list
, &kernel_fb_helper_list
);
2283 mutex_unlock(&kernel_fb_helper_lock
);
2287 EXPORT_SYMBOL(drm_fb_helper_initial_config
);
2290 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
2291 * probing all the outputs attached to the fb
2292 * @fb_helper: the drm_fb_helper
2294 * Scan the connectors attached to the fb_helper and try to put together a
2295 * setup after notification of a change in output configuration.
2297 * Called at runtime, takes the mode config locks to be able to check/change the
2298 * modeset configuration. Must be run from process context (which usually means
2299 * either the output polling work or a work item launched from the driver's
2300 * hotplug interrupt).
2302 * Note that drivers may call this even before calling
2303 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
2304 * for a race-free fbcon setup and will make sure that the fbdev emulation will
2305 * not miss any hotplug events.
2308 * 0 on success and a non-zero error code otherwise.
2310 int drm_fb_helper_hotplug_event(struct drm_fb_helper
*fb_helper
)
2312 struct drm_device
*dev
= fb_helper
->dev
;
2314 if (!drm_fbdev_emulation
)
2317 mutex_lock(&dev
->mode_config
.mutex
);
2318 if (!fb_helper
->fb
|| !drm_fb_helper_is_bound(fb_helper
)) {
2319 fb_helper
->delayed_hotplug
= true;
2320 mutex_unlock(&dev
->mode_config
.mutex
);
2323 DRM_DEBUG_KMS("\n");
2325 drm_setup_crtcs(fb_helper
, fb_helper
->fb
->width
, fb_helper
->fb
->height
);
2327 mutex_unlock(&dev
->mode_config
.mutex
);
2329 drm_fb_helper_set_par(fb_helper
->fbdev
);
2333 EXPORT_SYMBOL(drm_fb_helper_hotplug_event
);
2335 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
2336 * but the module doesn't depend on any fb console symbols. At least
2337 * attempt to load fbcon to avoid leaving the system without a usable console.
2339 int __init
drm_fb_helper_modinit(void)
2341 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
2342 const char *name
= "fbcon";
2343 struct module
*fbcon
;
2345 mutex_lock(&module_mutex
);
2346 fbcon
= find_module(name
);
2347 mutex_unlock(&module_mutex
);
2350 request_module_nowait(name
);
2354 EXPORT_SYMBOL(drm_fb_helper_modinit
);