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 static 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 lockdep_assert_held(&fb_helper
->lock
);
123 lockdep_assert_held(&fb_helper
->dev
->mode_config
.mutex
);
125 count
= fb_helper
->connector_count
+ 1;
127 if (count
> fb_helper
->connector_info_alloc_count
) {
128 size_t size
= count
* sizeof(fb_conn
);
130 temp
= krealloc(fb_helper
->connector_info
, size
, GFP_KERNEL
);
134 fb_helper
->connector_info_alloc_count
= count
;
135 fb_helper
->connector_info
= temp
;
138 fb_conn
= kzalloc(sizeof(*fb_conn
), GFP_KERNEL
);
142 drm_connector_get(connector
);
143 fb_conn
->connector
= connector
;
144 fb_helper
->connector_info
[fb_helper
->connector_count
++] = fb_conn
;
149 int drm_fb_helper_add_one_connector(struct drm_fb_helper
*fb_helper
,
150 struct drm_connector
*connector
)
154 mutex_lock(&fb_helper
->lock
);
155 mutex_lock(&fb_helper
->dev
->mode_config
.mutex
);
157 err
= __drm_fb_helper_add_one_connector(fb_helper
, connector
);
159 mutex_unlock(&fb_helper
->dev
->mode_config
.mutex
);
160 mutex_unlock(&fb_helper
->lock
);
164 EXPORT_SYMBOL(drm_fb_helper_add_one_connector
);
167 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
169 * @fb_helper: fbdev initialized with drm_fb_helper_init
171 * This functions adds all the available connectors for use with the given
172 * fb_helper. This is a separate step to allow drivers to freely assign
173 * connectors to the fbdev, e.g. if some are reserved for special purposes or
174 * not adequate to be used for the fbcon.
176 * This function is protected against concurrent connector hotadds/removals
177 * using drm_fb_helper_add_one_connector() and
178 * drm_fb_helper_remove_one_connector().
180 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper
*fb_helper
)
182 struct drm_device
*dev
= fb_helper
->dev
;
183 struct drm_connector
*connector
;
184 struct drm_connector_list_iter conn_iter
;
187 if (!drm_fbdev_emulation
)
190 mutex_lock(&fb_helper
->lock
);
191 mutex_lock(&dev
->mode_config
.mutex
);
192 drm_connector_list_iter_begin(dev
, &conn_iter
);
193 drm_for_each_connector_iter(connector
, &conn_iter
) {
194 ret
= __drm_fb_helper_add_one_connector(fb_helper
, connector
);
201 drm_fb_helper_for_each_connector(fb_helper
, i
) {
202 struct drm_fb_helper_connector
*fb_helper_connector
=
203 fb_helper
->connector_info
[i
];
205 drm_connector_put(fb_helper_connector
->connector
);
207 kfree(fb_helper_connector
);
208 fb_helper
->connector_info
[i
] = NULL
;
210 fb_helper
->connector_count
= 0;
212 drm_connector_list_iter_end(&conn_iter
);
213 mutex_unlock(&dev
->mode_config
.mutex
);
214 mutex_unlock(&fb_helper
->lock
);
218 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors
);
220 static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper
*fb_helper
,
221 struct drm_connector
*connector
)
223 struct drm_fb_helper_connector
*fb_helper_connector
;
226 if (!drm_fbdev_emulation
)
229 lockdep_assert_held(&fb_helper
->lock
);
231 drm_fb_helper_for_each_connector(fb_helper
, i
) {
232 if (fb_helper
->connector_info
[i
]->connector
== connector
)
236 if (i
== fb_helper
->connector_count
)
238 fb_helper_connector
= fb_helper
->connector_info
[i
];
239 drm_connector_put(fb_helper_connector
->connector
);
241 for (j
= i
+ 1; j
< fb_helper
->connector_count
; j
++)
242 fb_helper
->connector_info
[j
- 1] = fb_helper
->connector_info
[j
];
244 fb_helper
->connector_count
--;
245 kfree(fb_helper_connector
);
250 int drm_fb_helper_remove_one_connector(struct drm_fb_helper
*fb_helper
,
251 struct drm_connector
*connector
)
255 mutex_lock(&fb_helper
->lock
);
256 mutex_lock(&fb_helper
->dev
->mode_config
.mutex
);
258 err
= __drm_fb_helper_remove_one_connector(fb_helper
, connector
);
260 mutex_unlock(&fb_helper
->dev
->mode_config
.mutex
);
261 mutex_unlock(&fb_helper
->lock
);
265 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector
);
267 static void drm_fb_helper_save_lut_atomic(struct drm_crtc
*crtc
, struct drm_fb_helper
*helper
)
269 uint16_t *r_base
, *g_base
, *b_base
;
272 if (helper
->funcs
->gamma_get
== NULL
)
275 r_base
= crtc
->gamma_store
;
276 g_base
= r_base
+ crtc
->gamma_size
;
277 b_base
= g_base
+ crtc
->gamma_size
;
279 for (i
= 0; i
< crtc
->gamma_size
; i
++)
280 helper
->funcs
->gamma_get(crtc
, &r_base
[i
], &g_base
[i
], &b_base
[i
], i
);
283 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc
*crtc
)
285 uint16_t *r_base
, *g_base
, *b_base
;
287 if (crtc
->funcs
->gamma_set
== NULL
)
290 r_base
= crtc
->gamma_store
;
291 g_base
= r_base
+ crtc
->gamma_size
;
292 b_base
= g_base
+ crtc
->gamma_size
;
294 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
,
295 crtc
->gamma_size
, NULL
);
299 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
300 * @info: fbdev registered by the helper
302 int drm_fb_helper_debug_enter(struct fb_info
*info
)
304 struct drm_fb_helper
*helper
= info
->par
;
305 const struct drm_crtc_helper_funcs
*funcs
;
308 list_for_each_entry(helper
, &kernel_fb_helper_list
, kernel_fb_list
) {
309 for (i
= 0; i
< helper
->crtc_count
; i
++) {
310 struct drm_mode_set
*mode_set
=
311 &helper
->crtc_info
[i
].mode_set
;
313 if (!mode_set
->crtc
->enabled
)
316 funcs
= mode_set
->crtc
->helper_private
;
317 if (funcs
->mode_set_base_atomic
== NULL
)
320 if (drm_drv_uses_atomic_modeset(mode_set
->crtc
->dev
))
323 drm_fb_helper_save_lut_atomic(mode_set
->crtc
, helper
);
324 funcs
->mode_set_base_atomic(mode_set
->crtc
,
328 ENTER_ATOMIC_MODE_SET
);
334 EXPORT_SYMBOL(drm_fb_helper_debug_enter
);
337 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
338 * @info: fbdev registered by the helper
340 int drm_fb_helper_debug_leave(struct fb_info
*info
)
342 struct drm_fb_helper
*helper
= info
->par
;
343 struct drm_crtc
*crtc
;
344 const struct drm_crtc_helper_funcs
*funcs
;
345 struct drm_framebuffer
*fb
;
348 for (i
= 0; i
< helper
->crtc_count
; i
++) {
349 struct drm_mode_set
*mode_set
= &helper
->crtc_info
[i
].mode_set
;
351 crtc
= mode_set
->crtc
;
352 if (drm_drv_uses_atomic_modeset(crtc
->dev
))
355 funcs
= crtc
->helper_private
;
356 fb
= crtc
->primary
->fb
;
362 DRM_ERROR("no fb to restore??\n");
366 if (funcs
->mode_set_base_atomic
== NULL
)
369 drm_fb_helper_restore_lut_atomic(mode_set
->crtc
);
370 funcs
->mode_set_base_atomic(mode_set
->crtc
, fb
, crtc
->x
,
371 crtc
->y
, LEAVE_ATOMIC_MODE_SET
);
376 EXPORT_SYMBOL(drm_fb_helper_debug_leave
);
378 static int restore_fbdev_mode_atomic(struct drm_fb_helper
*fb_helper
)
380 struct drm_device
*dev
= fb_helper
->dev
;
381 struct drm_plane
*plane
;
382 struct drm_atomic_state
*state
;
384 unsigned int plane_mask
;
386 state
= drm_atomic_state_alloc(dev
);
390 state
->acquire_ctx
= dev
->mode_config
.acquire_ctx
;
393 drm_for_each_plane(plane
, dev
) {
394 struct drm_plane_state
*plane_state
;
396 plane_state
= drm_atomic_get_plane_state(state
, plane
);
397 if (IS_ERR(plane_state
)) {
398 ret
= PTR_ERR(plane_state
);
402 plane_state
->rotation
= DRM_MODE_ROTATE_0
;
404 plane
->old_fb
= plane
->fb
;
405 plane_mask
|= 1 << drm_plane_index(plane
);
407 /* disable non-primary: */
408 if (plane
->type
== DRM_PLANE_TYPE_PRIMARY
)
411 ret
= __drm_atomic_helper_disable_plane(plane
, plane_state
);
416 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
417 struct drm_mode_set
*mode_set
= &fb_helper
->crtc_info
[i
].mode_set
;
419 ret
= __drm_atomic_helper_set_config(mode_set
, state
);
424 ret
= drm_atomic_commit(state
);
427 drm_atomic_clean_old_fb(dev
, plane_mask
, ret
);
432 drm_atomic_state_put(state
);
436 drm_atomic_state_clear(state
);
437 drm_atomic_legacy_backoff(state
);
442 static int restore_fbdev_mode_legacy(struct drm_fb_helper
*fb_helper
)
444 struct drm_device
*dev
= fb_helper
->dev
;
445 struct drm_plane
*plane
;
448 drm_for_each_plane(plane
, dev
) {
449 if (plane
->type
!= DRM_PLANE_TYPE_PRIMARY
)
450 drm_plane_force_disable(plane
);
452 if (plane
->rotation_property
)
453 drm_mode_plane_set_obj_prop(plane
,
454 plane
->rotation_property
,
458 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
459 struct drm_mode_set
*mode_set
= &fb_helper
->crtc_info
[i
].mode_set
;
460 struct drm_crtc
*crtc
= mode_set
->crtc
;
463 if (crtc
->funcs
->cursor_set2
) {
464 ret
= crtc
->funcs
->cursor_set2(crtc
, NULL
, 0, 0, 0, 0, 0);
467 } else if (crtc
->funcs
->cursor_set
) {
468 ret
= crtc
->funcs
->cursor_set(crtc
, NULL
, 0, 0, 0);
473 ret
= drm_mode_set_config_internal(mode_set
);
481 static int restore_fbdev_mode(struct drm_fb_helper
*fb_helper
)
483 struct drm_device
*dev
= fb_helper
->dev
;
485 drm_warn_on_modeset_not_all_locked(dev
);
487 if (drm_drv_uses_atomic_modeset(dev
))
488 return restore_fbdev_mode_atomic(fb_helper
);
490 return restore_fbdev_mode_legacy(fb_helper
);
494 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
495 * @fb_helper: fbcon to restore
497 * This should be called from driver's drm &drm_driver.lastclose callback
498 * when implementing an fbcon on top of kms using this helper. This ensures that
499 * the user isn't greeted with a black screen when e.g. X dies.
502 * Zero if everything went ok, negative error code otherwise.
504 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper
*fb_helper
)
506 struct drm_device
*dev
= fb_helper
->dev
;
510 if (!drm_fbdev_emulation
)
513 mutex_lock(&fb_helper
->lock
);
514 drm_modeset_lock_all(dev
);
516 ret
= restore_fbdev_mode(fb_helper
);
518 do_delayed
= fb_helper
->delayed_hotplug
;
520 fb_helper
->delayed_hotplug
= false;
522 drm_modeset_unlock_all(dev
);
523 mutex_unlock(&fb_helper
->lock
);
526 drm_fb_helper_hotplug_event(fb_helper
);
530 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked
);
532 static bool drm_fb_helper_is_bound(struct drm_fb_helper
*fb_helper
)
534 struct drm_device
*dev
= fb_helper
->dev
;
535 struct drm_crtc
*crtc
;
536 int bound
= 0, crtcs_bound
= 0;
539 * Sometimes user space wants everything disabled, so don't steal the
540 * display if there's a master.
542 if (READ_ONCE(dev
->master
))
545 drm_for_each_crtc(crtc
, dev
) {
546 drm_modeset_lock(&crtc
->mutex
, NULL
);
547 if (crtc
->primary
->fb
)
549 if (crtc
->primary
->fb
== fb_helper
->fb
)
551 drm_modeset_unlock(&crtc
->mutex
);
554 if (bound
< crtcs_bound
)
560 #ifdef CONFIG_MAGIC_SYSRQ
562 * restore fbcon display for all kms driver's using this helper, used for sysrq
563 * and panic handling.
565 static bool drm_fb_helper_force_kernel_mode(void)
567 bool ret
, error
= false;
568 struct drm_fb_helper
*helper
;
570 if (list_empty(&kernel_fb_helper_list
))
573 list_for_each_entry(helper
, &kernel_fb_helper_list
, kernel_fb_list
) {
574 struct drm_device
*dev
= helper
->dev
;
576 if (dev
->switch_power_state
== DRM_SWITCH_POWER_OFF
)
579 mutex_lock(&helper
->lock
);
580 drm_modeset_lock_all(dev
);
581 ret
= restore_fbdev_mode(helper
);
584 drm_modeset_unlock_all(dev
);
585 mutex_unlock(&helper
->lock
);
590 static void drm_fb_helper_restore_work_fn(struct work_struct
*ignored
)
594 ret
= drm_fb_helper_force_kernel_mode();
596 DRM_ERROR("Failed to restore crtc configuration\n");
598 static DECLARE_WORK(drm_fb_helper_restore_work
, drm_fb_helper_restore_work_fn
);
600 static void drm_fb_helper_sysrq(int dummy1
)
602 schedule_work(&drm_fb_helper_restore_work
);
605 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op
= {
606 .handler
= drm_fb_helper_sysrq
,
607 .help_msg
= "force-fb(V)",
608 .action_msg
= "Restore framebuffer console",
611 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op
= { };
614 static void drm_fb_helper_dpms(struct fb_info
*info
, int dpms_mode
)
616 struct drm_fb_helper
*fb_helper
= info
->par
;
617 struct drm_device
*dev
= fb_helper
->dev
;
618 struct drm_crtc
*crtc
;
619 struct drm_connector
*connector
;
623 * For each CRTC in this fb, turn the connectors on/off.
625 mutex_lock(&fb_helper
->lock
);
626 if (!drm_fb_helper_is_bound(fb_helper
)) {
627 mutex_unlock(&fb_helper
->lock
);
631 drm_modeset_lock_all(dev
);
632 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
633 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
638 /* Walk the connectors & encoders on this fb turning them on/off */
639 drm_fb_helper_for_each_connector(fb_helper
, j
) {
640 connector
= fb_helper
->connector_info
[j
]->connector
;
641 connector
->funcs
->dpms(connector
, dpms_mode
);
642 drm_object_property_set_value(&connector
->base
,
643 dev
->mode_config
.dpms_property
, dpms_mode
);
646 drm_modeset_unlock_all(dev
);
647 mutex_unlock(&fb_helper
->lock
);
651 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
652 * @blank: desired blanking state
653 * @info: fbdev registered by the helper
655 int drm_fb_helper_blank(int blank
, struct fb_info
*info
)
657 if (oops_in_progress
)
661 /* Display: On; HSync: On, VSync: On */
662 case FB_BLANK_UNBLANK
:
663 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_ON
);
665 /* Display: Off; HSync: On, VSync: On */
666 case FB_BLANK_NORMAL
:
667 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_STANDBY
);
669 /* Display: Off; HSync: Off, VSync: On */
670 case FB_BLANK_HSYNC_SUSPEND
:
671 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_STANDBY
);
673 /* Display: Off; HSync: On, VSync: Off */
674 case FB_BLANK_VSYNC_SUSPEND
:
675 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_SUSPEND
);
677 /* Display: Off; HSync: Off, VSync: Off */
678 case FB_BLANK_POWERDOWN
:
679 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_OFF
);
684 EXPORT_SYMBOL(drm_fb_helper_blank
);
686 static void drm_fb_helper_modeset_release(struct drm_fb_helper
*helper
,
687 struct drm_mode_set
*modeset
)
691 for (i
= 0; i
< modeset
->num_connectors
; i
++) {
692 drm_connector_put(modeset
->connectors
[i
]);
693 modeset
->connectors
[i
] = NULL
;
695 modeset
->num_connectors
= 0;
697 drm_mode_destroy(helper
->dev
, modeset
->mode
);
698 modeset
->mode
= NULL
;
700 /* FIXME should hold a ref? */
704 static void drm_fb_helper_crtc_free(struct drm_fb_helper
*helper
)
708 for (i
= 0; i
< helper
->connector_count
; i
++) {
709 drm_connector_put(helper
->connector_info
[i
]->connector
);
710 kfree(helper
->connector_info
[i
]);
712 kfree(helper
->connector_info
);
714 for (i
= 0; i
< helper
->crtc_count
; i
++) {
715 struct drm_mode_set
*modeset
= &helper
->crtc_info
[i
].mode_set
;
717 drm_fb_helper_modeset_release(helper
, modeset
);
718 kfree(modeset
->connectors
);
720 kfree(helper
->crtc_info
);
723 static void drm_fb_helper_resume_worker(struct work_struct
*work
)
725 struct drm_fb_helper
*helper
= container_of(work
, struct drm_fb_helper
,
729 fb_set_suspend(helper
->fbdev
, 0);
733 static void drm_fb_helper_dirty_work(struct work_struct
*work
)
735 struct drm_fb_helper
*helper
= container_of(work
, struct drm_fb_helper
,
737 struct drm_clip_rect
*clip
= &helper
->dirty_clip
;
738 struct drm_clip_rect clip_copy
;
741 spin_lock_irqsave(&helper
->dirty_lock
, flags
);
743 clip
->x1
= clip
->y1
= ~0;
744 clip
->x2
= clip
->y2
= 0;
745 spin_unlock_irqrestore(&helper
->dirty_lock
, flags
);
747 /* call dirty callback only when it has been really touched */
748 if (clip_copy
.x1
< clip_copy
.x2
&& clip_copy
.y1
< clip_copy
.y2
)
749 helper
->fb
->funcs
->dirty(helper
->fb
, NULL
, 0, 0, &clip_copy
, 1);
753 * drm_fb_helper_prepare - setup a drm_fb_helper structure
755 * @helper: driver-allocated fbdev helper structure to set up
756 * @funcs: pointer to structure of functions associate with this helper
758 * Sets up the bare minimum to make the framebuffer helper usable. This is
759 * useful to implement race-free initialization of the polling helpers.
761 void drm_fb_helper_prepare(struct drm_device
*dev
, struct drm_fb_helper
*helper
,
762 const struct drm_fb_helper_funcs
*funcs
)
764 INIT_LIST_HEAD(&helper
->kernel_fb_list
);
765 spin_lock_init(&helper
->dirty_lock
);
766 INIT_WORK(&helper
->resume_work
, drm_fb_helper_resume_worker
);
767 INIT_WORK(&helper
->dirty_work
, drm_fb_helper_dirty_work
);
768 helper
->dirty_clip
.x1
= helper
->dirty_clip
.y1
= ~0;
769 mutex_init(&helper
->lock
);
770 helper
->funcs
= funcs
;
773 EXPORT_SYMBOL(drm_fb_helper_prepare
);
776 * drm_fb_helper_init - initialize a &struct drm_fb_helper
778 * @fb_helper: driver-allocated fbdev helper structure to initialize
779 * @max_conn_count: max connector count
781 * This allocates the structures for the fbdev helper with the given limits.
782 * Note that this won't yet touch the hardware (through the driver interfaces)
783 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
784 * to allow driver writes more control over the exact init sequence.
786 * Drivers must call drm_fb_helper_prepare() before calling this function.
789 * Zero if everything went ok, nonzero otherwise.
791 int drm_fb_helper_init(struct drm_device
*dev
,
792 struct drm_fb_helper
*fb_helper
,
795 struct drm_crtc
*crtc
;
796 struct drm_mode_config
*config
= &dev
->mode_config
;
799 if (!drm_fbdev_emulation
)
805 fb_helper
->crtc_info
= kcalloc(config
->num_crtc
, sizeof(struct drm_fb_helper_crtc
), GFP_KERNEL
);
806 if (!fb_helper
->crtc_info
)
809 fb_helper
->crtc_count
= config
->num_crtc
;
810 fb_helper
->connector_info
= kcalloc(dev
->mode_config
.num_connector
, sizeof(struct drm_fb_helper_connector
*), GFP_KERNEL
);
811 if (!fb_helper
->connector_info
) {
812 kfree(fb_helper
->crtc_info
);
815 fb_helper
->connector_info_alloc_count
= dev
->mode_config
.num_connector
;
816 fb_helper
->connector_count
= 0;
818 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
819 fb_helper
->crtc_info
[i
].mode_set
.connectors
=
820 kcalloc(max_conn_count
,
821 sizeof(struct drm_connector
*),
824 if (!fb_helper
->crtc_info
[i
].mode_set
.connectors
)
826 fb_helper
->crtc_info
[i
].mode_set
.num_connectors
= 0;
830 drm_for_each_crtc(crtc
, dev
) {
831 fb_helper
->crtc_info
[i
].mode_set
.crtc
= crtc
;
837 drm_fb_helper_crtc_free(fb_helper
);
840 EXPORT_SYMBOL(drm_fb_helper_init
);
843 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
844 * @fb_helper: driver-allocated fbdev helper
846 * A helper to alloc fb_info and the members cmap and apertures. Called
847 * by the driver within the fb_probe fb_helper callback function. Drivers do not
848 * need to release the allocated fb_info structure themselves, this is
849 * automatically done when calling drm_fb_helper_fini().
852 * fb_info pointer if things went okay, pointer containing error code
855 struct fb_info
*drm_fb_helper_alloc_fbi(struct drm_fb_helper
*fb_helper
)
857 struct device
*dev
= fb_helper
->dev
->dev
;
858 struct fb_info
*info
;
861 info
= framebuffer_alloc(0, dev
);
863 return ERR_PTR(-ENOMEM
);
865 ret
= fb_alloc_cmap(&info
->cmap
, 256, 0);
869 info
->apertures
= alloc_apertures(1);
870 if (!info
->apertures
) {
875 fb_helper
->fbdev
= info
;
880 fb_dealloc_cmap(&info
->cmap
);
882 framebuffer_release(info
);
885 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi
);
888 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
889 * @fb_helper: driver-allocated fbdev helper
891 * A wrapper around unregister_framebuffer, to release the fb_info
892 * framebuffer device. This must be called before releasing all resources for
893 * @fb_helper by calling drm_fb_helper_fini().
895 void drm_fb_helper_unregister_fbi(struct drm_fb_helper
*fb_helper
)
897 if (fb_helper
&& fb_helper
->fbdev
)
898 unregister_framebuffer(fb_helper
->fbdev
);
900 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi
);
903 * drm_fb_helper_fini - finialize a &struct drm_fb_helper
904 * @fb_helper: driver-allocated fbdev helper
906 * This cleans up all remaining resources associated with @fb_helper. Must be
907 * called after drm_fb_helper_unlink_fbi() was called.
909 void drm_fb_helper_fini(struct drm_fb_helper
*fb_helper
)
911 struct fb_info
*info
;
913 if (!drm_fbdev_emulation
|| !fb_helper
)
916 info
= fb_helper
->fbdev
;
919 fb_dealloc_cmap(&info
->cmap
);
920 framebuffer_release(info
);
922 fb_helper
->fbdev
= NULL
;
924 cancel_work_sync(&fb_helper
->resume_work
);
925 cancel_work_sync(&fb_helper
->dirty_work
);
927 mutex_lock(&kernel_fb_helper_lock
);
928 if (!list_empty(&fb_helper
->kernel_fb_list
)) {
929 list_del(&fb_helper
->kernel_fb_list
);
930 if (list_empty(&kernel_fb_helper_list
))
931 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op
);
933 mutex_unlock(&kernel_fb_helper_lock
);
935 mutex_destroy(&fb_helper
->lock
);
936 drm_fb_helper_crtc_free(fb_helper
);
939 EXPORT_SYMBOL(drm_fb_helper_fini
);
942 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
943 * @fb_helper: driver-allocated fbdev helper
945 * A wrapper around unlink_framebuffer implemented by fbdev core
947 void drm_fb_helper_unlink_fbi(struct drm_fb_helper
*fb_helper
)
949 if (fb_helper
&& fb_helper
->fbdev
)
950 unlink_framebuffer(fb_helper
->fbdev
);
952 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi
);
954 static void drm_fb_helper_dirty(struct fb_info
*info
, u32 x
, u32 y
,
955 u32 width
, u32 height
)
957 struct drm_fb_helper
*helper
= info
->par
;
958 struct drm_clip_rect
*clip
= &helper
->dirty_clip
;
961 if (!helper
->fb
->funcs
->dirty
)
964 spin_lock_irqsave(&helper
->dirty_lock
, flags
);
965 clip
->x1
= min_t(u32
, clip
->x1
, x
);
966 clip
->y1
= min_t(u32
, clip
->y1
, y
);
967 clip
->x2
= max_t(u32
, clip
->x2
, x
+ width
);
968 clip
->y2
= max_t(u32
, clip
->y2
, y
+ height
);
969 spin_unlock_irqrestore(&helper
->dirty_lock
, flags
);
971 schedule_work(&helper
->dirty_work
);
975 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
976 * @info: fb_info struct pointer
977 * @pagelist: list of dirty mmap framebuffer pages
979 * This function is used as the &fb_deferred_io.deferred_io
980 * callback function for flushing the fbdev mmap writes.
982 void drm_fb_helper_deferred_io(struct fb_info
*info
,
983 struct list_head
*pagelist
)
985 unsigned long start
, end
, min
, max
;
991 list_for_each_entry(page
, pagelist
, lru
) {
992 start
= page
->index
<< PAGE_SHIFT
;
993 end
= start
+ PAGE_SIZE
- 1;
994 min
= min(min
, start
);
999 y1
= min
/ info
->fix
.line_length
;
1000 y2
= min_t(u32
, DIV_ROUND_UP(max
, info
->fix
.line_length
),
1002 drm_fb_helper_dirty(info
, 0, y1
, info
->var
.xres
, y2
- y1
);
1005 EXPORT_SYMBOL(drm_fb_helper_deferred_io
);
1008 * drm_fb_helper_sys_read - wrapper around fb_sys_read
1009 * @info: fb_info struct pointer
1010 * @buf: userspace buffer to read from framebuffer memory
1011 * @count: number of bytes to read from framebuffer memory
1012 * @ppos: read offset within framebuffer memory
1014 * A wrapper around fb_sys_read implemented by fbdev core
1016 ssize_t
drm_fb_helper_sys_read(struct fb_info
*info
, char __user
*buf
,
1017 size_t count
, loff_t
*ppos
)
1019 return fb_sys_read(info
, buf
, count
, ppos
);
1021 EXPORT_SYMBOL(drm_fb_helper_sys_read
);
1024 * drm_fb_helper_sys_write - wrapper around fb_sys_write
1025 * @info: fb_info struct pointer
1026 * @buf: userspace buffer to write to framebuffer memory
1027 * @count: number of bytes to write to framebuffer memory
1028 * @ppos: write offset within framebuffer memory
1030 * A wrapper around fb_sys_write implemented by fbdev core
1032 ssize_t
drm_fb_helper_sys_write(struct fb_info
*info
, const char __user
*buf
,
1033 size_t count
, loff_t
*ppos
)
1037 ret
= fb_sys_write(info
, buf
, count
, ppos
);
1039 drm_fb_helper_dirty(info
, 0, 0, info
->var
.xres
,
1044 EXPORT_SYMBOL(drm_fb_helper_sys_write
);
1047 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
1048 * @info: fbdev registered by the helper
1049 * @rect: info about rectangle to fill
1051 * A wrapper around sys_fillrect implemented by fbdev core
1053 void drm_fb_helper_sys_fillrect(struct fb_info
*info
,
1054 const struct fb_fillrect
*rect
)
1056 sys_fillrect(info
, rect
);
1057 drm_fb_helper_dirty(info
, rect
->dx
, rect
->dy
,
1058 rect
->width
, rect
->height
);
1060 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect
);
1063 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1064 * @info: fbdev registered by the helper
1065 * @area: info about area to copy
1067 * A wrapper around sys_copyarea implemented by fbdev core
1069 void drm_fb_helper_sys_copyarea(struct fb_info
*info
,
1070 const struct fb_copyarea
*area
)
1072 sys_copyarea(info
, area
);
1073 drm_fb_helper_dirty(info
, area
->dx
, area
->dy
,
1074 area
->width
, area
->height
);
1076 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea
);
1079 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1080 * @info: fbdev registered by the helper
1081 * @image: info about image to blit
1083 * A wrapper around sys_imageblit implemented by fbdev core
1085 void drm_fb_helper_sys_imageblit(struct fb_info
*info
,
1086 const struct fb_image
*image
)
1088 sys_imageblit(info
, image
);
1089 drm_fb_helper_dirty(info
, image
->dx
, image
->dy
,
1090 image
->width
, image
->height
);
1092 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit
);
1095 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1096 * @info: fbdev registered by the helper
1097 * @rect: info about rectangle to fill
1099 * A wrapper around cfb_imageblit implemented by fbdev core
1101 void drm_fb_helper_cfb_fillrect(struct fb_info
*info
,
1102 const struct fb_fillrect
*rect
)
1104 cfb_fillrect(info
, rect
);
1105 drm_fb_helper_dirty(info
, rect
->dx
, rect
->dy
,
1106 rect
->width
, rect
->height
);
1108 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect
);
1111 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1112 * @info: fbdev registered by the helper
1113 * @area: info about area to copy
1115 * A wrapper around cfb_copyarea implemented by fbdev core
1117 void drm_fb_helper_cfb_copyarea(struct fb_info
*info
,
1118 const struct fb_copyarea
*area
)
1120 cfb_copyarea(info
, area
);
1121 drm_fb_helper_dirty(info
, area
->dx
, area
->dy
,
1122 area
->width
, area
->height
);
1124 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea
);
1127 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1128 * @info: fbdev registered by the helper
1129 * @image: info about image to blit
1131 * A wrapper around cfb_imageblit implemented by fbdev core
1133 void drm_fb_helper_cfb_imageblit(struct fb_info
*info
,
1134 const struct fb_image
*image
)
1136 cfb_imageblit(info
, image
);
1137 drm_fb_helper_dirty(info
, image
->dx
, image
->dy
,
1138 image
->width
, image
->height
);
1140 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit
);
1143 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
1144 * @fb_helper: driver-allocated fbdev helper
1145 * @suspend: whether to suspend or resume
1147 * A wrapper around fb_set_suspend implemented by fbdev core.
1148 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
1151 void drm_fb_helper_set_suspend(struct drm_fb_helper
*fb_helper
, bool suspend
)
1153 if (fb_helper
&& fb_helper
->fbdev
)
1154 fb_set_suspend(fb_helper
->fbdev
, suspend
);
1156 EXPORT_SYMBOL(drm_fb_helper_set_suspend
);
1159 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
1160 * takes the console lock
1161 * @fb_helper: driver-allocated fbdev helper
1162 * @suspend: whether to suspend or resume
1164 * A wrapper around fb_set_suspend() that takes the console lock. If the lock
1165 * isn't available on resume, a worker is tasked with waiting for the lock
1166 * to become available. The console lock can be pretty contented on resume
1167 * due to all the printk activity.
1169 * This function can be called multiple times with the same state since
1170 * &fb_info.state is checked to see if fbdev is running or not before locking.
1172 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
1174 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper
*fb_helper
,
1177 if (!fb_helper
|| !fb_helper
->fbdev
)
1180 /* make sure there's no pending/ongoing resume */
1181 flush_work(&fb_helper
->resume_work
);
1184 if (fb_helper
->fbdev
->state
!= FBINFO_STATE_RUNNING
)
1190 if (fb_helper
->fbdev
->state
== FBINFO_STATE_RUNNING
)
1193 if (!console_trylock()) {
1194 schedule_work(&fb_helper
->resume_work
);
1199 fb_set_suspend(fb_helper
->fbdev
, suspend
);
1202 EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked
);
1204 static int setcolreg(struct drm_crtc
*crtc
, u16 red
, u16 green
,
1205 u16 blue
, u16 regno
, struct fb_info
*info
)
1207 struct drm_fb_helper
*fb_helper
= info
->par
;
1208 struct drm_framebuffer
*fb
= fb_helper
->fb
;
1210 if (info
->fix
.visual
== FB_VISUAL_TRUECOLOR
) {
1213 /* place color in psuedopalette */
1216 palette
= (u32
*)info
->pseudo_palette
;
1217 red
>>= (16 - info
->var
.red
.length
);
1218 green
>>= (16 - info
->var
.green
.length
);
1219 blue
>>= (16 - info
->var
.blue
.length
);
1220 value
= (red
<< info
->var
.red
.offset
) |
1221 (green
<< info
->var
.green
.offset
) |
1222 (blue
<< info
->var
.blue
.offset
);
1223 if (info
->var
.transp
.length
> 0) {
1224 u32 mask
= (1 << info
->var
.transp
.length
) - 1;
1226 mask
<<= info
->var
.transp
.offset
;
1229 palette
[regno
] = value
;
1234 * The driver really shouldn't advertise pseudo/directcolor
1235 * visuals if it can't deal with the palette.
1237 if (WARN_ON(!fb_helper
->funcs
->gamma_set
||
1238 !fb_helper
->funcs
->gamma_get
))
1241 WARN_ON(fb
->format
->cpp
[0] != 1);
1243 fb_helper
->funcs
->gamma_set(crtc
, red
, green
, blue
, regno
);
1249 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
1250 * @cmap: cmap to set
1251 * @info: fbdev registered by the helper
1253 int drm_fb_helper_setcmap(struct fb_cmap
*cmap
, struct fb_info
*info
)
1255 struct drm_fb_helper
*fb_helper
= info
->par
;
1256 struct drm_device
*dev
= fb_helper
->dev
;
1257 const struct drm_crtc_helper_funcs
*crtc_funcs
;
1258 u16
*red
, *green
, *blue
, *transp
;
1259 struct drm_crtc
*crtc
;
1263 if (oops_in_progress
)
1266 mutex_lock(&fb_helper
->lock
);
1267 if (!drm_fb_helper_is_bound(fb_helper
)) {
1268 mutex_unlock(&fb_helper
->lock
);
1272 drm_modeset_lock_all(dev
);
1273 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1274 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
1275 crtc_funcs
= crtc
->helper_private
;
1278 green
= cmap
->green
;
1280 transp
= cmap
->transp
;
1281 start
= cmap
->start
;
1283 for (j
= 0; j
< cmap
->len
; j
++) {
1284 u16 hred
, hgreen
, hblue
, htransp
= 0xffff;
1291 htransp
= *transp
++;
1293 rc
= setcolreg(crtc
, hred
, hgreen
, hblue
, start
++, info
);
1297 if (crtc_funcs
->load_lut
)
1298 crtc_funcs
->load_lut(crtc
);
1301 drm_modeset_unlock_all(dev
);
1302 mutex_unlock(&fb_helper
->lock
);
1305 EXPORT_SYMBOL(drm_fb_helper_setcmap
);
1308 * drm_fb_helper_ioctl - legacy ioctl implementation
1309 * @info: fbdev registered by the helper
1310 * @cmd: ioctl command
1311 * @arg: ioctl argument
1313 * A helper to implement the standard fbdev ioctl. Only
1314 * FBIO_WAITFORVSYNC is implemented for now.
1316 int drm_fb_helper_ioctl(struct fb_info
*info
, unsigned int cmd
,
1319 struct drm_fb_helper
*fb_helper
= info
->par
;
1320 struct drm_device
*dev
= fb_helper
->dev
;
1321 struct drm_mode_set
*mode_set
;
1322 struct drm_crtc
*crtc
;
1325 mutex_lock(&fb_helper
->lock
);
1326 if (!drm_fb_helper_is_bound(fb_helper
)) {
1331 mutex_lock(&dev
->mode_config
.mutex
);
1333 case FBIO_WAITFORVSYNC
:
1335 * Only consider the first CRTC.
1337 * This ioctl is supposed to take the CRTC number as
1338 * an argument, but in fbdev times, what that number
1339 * was supposed to be was quite unclear, different
1340 * drivers were passing that argument differently
1341 * (some by reference, some by value), and most of the
1342 * userspace applications were just hardcoding 0 as an
1345 * The first CRTC should be the integrated panel on
1346 * most drivers, so this is the best choice we can
1347 * make. If we're not smart enough here, one should
1348 * just consider switch the userspace to KMS.
1350 mode_set
= &fb_helper
->crtc_info
[0].mode_set
;
1351 crtc
= mode_set
->crtc
;
1354 * Only wait for a vblank event if the CRTC is
1355 * enabled, otherwise just don't do anythintg,
1356 * not even report an error.
1358 ret
= drm_crtc_vblank_get(crtc
);
1360 drm_crtc_wait_one_vblank(crtc
);
1361 drm_crtc_vblank_put(crtc
);
1371 mutex_unlock(&dev
->mode_config
.mutex
);
1372 mutex_unlock(&fb_helper
->lock
);
1375 EXPORT_SYMBOL(drm_fb_helper_ioctl
);
1378 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
1379 * @var: screeninfo to check
1380 * @info: fbdev registered by the helper
1382 int drm_fb_helper_check_var(struct fb_var_screeninfo
*var
,
1383 struct fb_info
*info
)
1385 struct drm_fb_helper
*fb_helper
= info
->par
;
1386 struct drm_framebuffer
*fb
= fb_helper
->fb
;
1389 if (var
->pixclock
!= 0 || in_dbg_master())
1393 * Changes struct fb_var_screeninfo are currently not pushed back
1394 * to KMS, hence fail if different settings are requested.
1396 if (var
->bits_per_pixel
!= fb
->format
->cpp
[0] * 8 ||
1397 var
->xres
> fb
->width
|| var
->yres
> fb
->height
||
1398 var
->xres_virtual
> fb
->width
|| var
->yres_virtual
> fb
->height
) {
1399 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
1400 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1401 var
->xres
, var
->yres
, var
->bits_per_pixel
,
1402 var
->xres_virtual
, var
->yres_virtual
,
1403 fb
->width
, fb
->height
, fb
->format
->cpp
[0] * 8);
1407 switch (var
->bits_per_pixel
) {
1409 depth
= (var
->green
.length
== 6) ? 16 : 15;
1412 depth
= (var
->transp
.length
> 0) ? 32 : 24;
1415 depth
= var
->bits_per_pixel
;
1421 var
->red
.offset
= 0;
1422 var
->green
.offset
= 0;
1423 var
->blue
.offset
= 0;
1424 var
->red
.length
= 8;
1425 var
->green
.length
= 8;
1426 var
->blue
.length
= 8;
1427 var
->transp
.length
= 0;
1428 var
->transp
.offset
= 0;
1431 var
->red
.offset
= 10;
1432 var
->green
.offset
= 5;
1433 var
->blue
.offset
= 0;
1434 var
->red
.length
= 5;
1435 var
->green
.length
= 5;
1436 var
->blue
.length
= 5;
1437 var
->transp
.length
= 1;
1438 var
->transp
.offset
= 15;
1441 var
->red
.offset
= 11;
1442 var
->green
.offset
= 5;
1443 var
->blue
.offset
= 0;
1444 var
->red
.length
= 5;
1445 var
->green
.length
= 6;
1446 var
->blue
.length
= 5;
1447 var
->transp
.length
= 0;
1448 var
->transp
.offset
= 0;
1451 var
->red
.offset
= 16;
1452 var
->green
.offset
= 8;
1453 var
->blue
.offset
= 0;
1454 var
->red
.length
= 8;
1455 var
->green
.length
= 8;
1456 var
->blue
.length
= 8;
1457 var
->transp
.length
= 0;
1458 var
->transp
.offset
= 0;
1461 var
->red
.offset
= 16;
1462 var
->green
.offset
= 8;
1463 var
->blue
.offset
= 0;
1464 var
->red
.length
= 8;
1465 var
->green
.length
= 8;
1466 var
->blue
.length
= 8;
1467 var
->transp
.length
= 8;
1468 var
->transp
.offset
= 24;
1475 EXPORT_SYMBOL(drm_fb_helper_check_var
);
1478 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
1479 * @info: fbdev registered by the helper
1481 * This will let fbcon do the mode init and is called at initialization time by
1482 * the fbdev core when registering the driver, and later on through the hotplug
1485 int drm_fb_helper_set_par(struct fb_info
*info
)
1487 struct drm_fb_helper
*fb_helper
= info
->par
;
1488 struct fb_var_screeninfo
*var
= &info
->var
;
1490 if (oops_in_progress
)
1493 if (var
->pixclock
!= 0) {
1494 DRM_ERROR("PIXEL CLOCK SET\n");
1498 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper
);
1502 EXPORT_SYMBOL(drm_fb_helper_set_par
);
1504 static int pan_display_atomic(struct fb_var_screeninfo
*var
,
1505 struct fb_info
*info
)
1507 struct drm_fb_helper
*fb_helper
= info
->par
;
1508 struct drm_device
*dev
= fb_helper
->dev
;
1509 struct drm_atomic_state
*state
;
1510 struct drm_plane
*plane
;
1512 unsigned int plane_mask
;
1514 state
= drm_atomic_state_alloc(dev
);
1518 state
->acquire_ctx
= dev
->mode_config
.acquire_ctx
;
1521 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1522 struct drm_mode_set
*mode_set
;
1524 mode_set
= &fb_helper
->crtc_info
[i
].mode_set
;
1526 mode_set
->x
= var
->xoffset
;
1527 mode_set
->y
= var
->yoffset
;
1529 ret
= __drm_atomic_helper_set_config(mode_set
, state
);
1533 plane
= mode_set
->crtc
->primary
;
1534 plane_mask
|= (1 << drm_plane_index(plane
));
1535 plane
->old_fb
= plane
->fb
;
1538 ret
= drm_atomic_commit(state
);
1542 info
->var
.xoffset
= var
->xoffset
;
1543 info
->var
.yoffset
= var
->yoffset
;
1546 drm_atomic_clean_old_fb(dev
, plane_mask
, ret
);
1548 if (ret
== -EDEADLK
)
1551 drm_atomic_state_put(state
);
1555 drm_atomic_state_clear(state
);
1556 drm_atomic_legacy_backoff(state
);
1561 static int pan_display_legacy(struct fb_var_screeninfo
*var
,
1562 struct fb_info
*info
)
1564 struct drm_fb_helper
*fb_helper
= info
->par
;
1565 struct drm_mode_set
*modeset
;
1569 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1570 modeset
= &fb_helper
->crtc_info
[i
].mode_set
;
1572 modeset
->x
= var
->xoffset
;
1573 modeset
->y
= var
->yoffset
;
1575 if (modeset
->num_connectors
) {
1576 ret
= drm_mode_set_config_internal(modeset
);
1578 info
->var
.xoffset
= var
->xoffset
;
1579 info
->var
.yoffset
= var
->yoffset
;
1588 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1589 * @var: updated screen information
1590 * @info: fbdev registered by the helper
1592 int drm_fb_helper_pan_display(struct fb_var_screeninfo
*var
,
1593 struct fb_info
*info
)
1595 struct drm_fb_helper
*fb_helper
= info
->par
;
1596 struct drm_device
*dev
= fb_helper
->dev
;
1599 if (oops_in_progress
)
1602 mutex_lock(&fb_helper
->lock
);
1603 if (!drm_fb_helper_is_bound(fb_helper
)) {
1604 mutex_unlock(&fb_helper
->lock
);
1608 drm_modeset_lock_all(dev
);
1609 if (drm_drv_uses_atomic_modeset(dev
))
1610 ret
= pan_display_atomic(var
, info
);
1612 ret
= pan_display_legacy(var
, info
);
1613 drm_modeset_unlock_all(dev
);
1614 mutex_unlock(&fb_helper
->lock
);
1618 EXPORT_SYMBOL(drm_fb_helper_pan_display
);
1621 * Allocates the backing storage and sets up the fbdev info structure through
1622 * the ->fb_probe callback and then registers the fbdev and sets up the panic
1625 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper
*fb_helper
,
1631 struct drm_fb_helper_surface_size sizes
;
1634 memset(&sizes
, 0, sizeof(struct drm_fb_helper_surface_size
));
1635 sizes
.surface_depth
= 24;
1636 sizes
.surface_bpp
= 32;
1637 sizes
.fb_width
= (u32
)-1;
1638 sizes
.fb_height
= (u32
)-1;
1640 /* if driver picks 8 or 16 by default use that for both depth/bpp */
1641 if (preferred_bpp
!= sizes
.surface_bpp
)
1642 sizes
.surface_depth
= sizes
.surface_bpp
= preferred_bpp
;
1644 /* first up get a count of crtcs now in use and new min/maxes width/heights */
1645 drm_fb_helper_for_each_connector(fb_helper
, i
) {
1646 struct drm_fb_helper_connector
*fb_helper_conn
= fb_helper
->connector_info
[i
];
1647 struct drm_cmdline_mode
*cmdline_mode
;
1649 cmdline_mode
= &fb_helper_conn
->connector
->cmdline_mode
;
1651 if (cmdline_mode
->bpp_specified
) {
1652 switch (cmdline_mode
->bpp
) {
1654 sizes
.surface_depth
= sizes
.surface_bpp
= 8;
1657 sizes
.surface_depth
= 15;
1658 sizes
.surface_bpp
= 16;
1661 sizes
.surface_depth
= sizes
.surface_bpp
= 16;
1664 sizes
.surface_depth
= sizes
.surface_bpp
= 24;
1667 sizes
.surface_depth
= 24;
1668 sizes
.surface_bpp
= 32;
1676 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1677 struct drm_display_mode
*desired_mode
;
1678 struct drm_mode_set
*mode_set
;
1680 /* in case of tile group, are we the last tile vert or horiz?
1681 * If no tile group you are always the last one both vertically
1684 bool lastv
= true, lasth
= true;
1686 desired_mode
= fb_helper
->crtc_info
[i
].desired_mode
;
1687 mode_set
= &fb_helper
->crtc_info
[i
].mode_set
;
1694 x
= fb_helper
->crtc_info
[i
].x
;
1695 y
= fb_helper
->crtc_info
[i
].y
;
1697 if (gamma_size
== 0)
1698 gamma_size
= fb_helper
->crtc_info
[i
].mode_set
.crtc
->gamma_size
;
1700 sizes
.surface_width
= max_t(u32
, desired_mode
->hdisplay
+ x
, sizes
.surface_width
);
1701 sizes
.surface_height
= max_t(u32
, desired_mode
->vdisplay
+ y
, sizes
.surface_height
);
1703 for (j
= 0; j
< mode_set
->num_connectors
; j
++) {
1704 struct drm_connector
*connector
= mode_set
->connectors
[j
];
1706 if (connector
->has_tile
) {
1707 lasth
= (connector
->tile_h_loc
== (connector
->num_h_tile
- 1));
1708 lastv
= (connector
->tile_v_loc
== (connector
->num_v_tile
- 1));
1709 /* cloning to multiple tiles is just crazy-talk, so: */
1715 sizes
.fb_width
= min_t(u32
, desired_mode
->hdisplay
+ x
, sizes
.fb_width
);
1717 sizes
.fb_height
= min_t(u32
, desired_mode
->vdisplay
+ y
, sizes
.fb_height
);
1720 if (crtc_count
== 0 || sizes
.fb_width
== -1 || sizes
.fb_height
== -1) {
1722 * hmm everyone went away - assume VGA cable just fell out
1723 * and will come back later.
1725 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
1726 sizes
.fb_width
= sizes
.surface_width
= 1024;
1727 sizes
.fb_height
= sizes
.surface_height
= 768;
1730 /* Handle our overallocation */
1731 sizes
.surface_height
*= drm_fbdev_overalloc
;
1732 sizes
.surface_height
/= 100;
1734 /* push down into drivers */
1735 ret
= (*fb_helper
->funcs
->fb_probe
)(fb_helper
, &sizes
);
1740 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1741 * events, but at init time drm_setup_crtcs needs to be called before
1742 * the fb is allocated (since we need to figure out the desired size of
1743 * the fb before we can allocate it ...). Hence we need to fix things up
1746 for (i
= 0; i
< fb_helper
->crtc_count
; i
++)
1747 if (fb_helper
->crtc_info
[i
].mode_set
.num_connectors
)
1748 fb_helper
->crtc_info
[i
].mode_set
.fb
= fb_helper
->fb
;
1754 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1755 * @info: fbdev registered by the helper
1756 * @pitch: desired pitch
1757 * @depth: desired depth
1759 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1760 * fbdev emulations. Drivers which support acceleration methods which impose
1761 * additional constraints need to set up their own limits.
1763 * Drivers should call this (or their equivalent setup code) from their
1764 * &drm_fb_helper_funcs.fb_probe callback.
1766 void drm_fb_helper_fill_fix(struct fb_info
*info
, uint32_t pitch
,
1769 info
->fix
.type
= FB_TYPE_PACKED_PIXELS
;
1770 info
->fix
.visual
= depth
== 8 ? FB_VISUAL_PSEUDOCOLOR
:
1771 FB_VISUAL_TRUECOLOR
;
1772 info
->fix
.mmio_start
= 0;
1773 info
->fix
.mmio_len
= 0;
1774 info
->fix
.type_aux
= 0;
1775 info
->fix
.xpanstep
= 1; /* doing it in hw */
1776 info
->fix
.ypanstep
= 1; /* doing it in hw */
1777 info
->fix
.ywrapstep
= 0;
1778 info
->fix
.accel
= FB_ACCEL_NONE
;
1780 info
->fix
.line_length
= pitch
;
1782 EXPORT_SYMBOL(drm_fb_helper_fill_fix
);
1785 * drm_fb_helper_fill_var - initalizes variable fbdev information
1786 * @info: fbdev instance to set up
1787 * @fb_helper: fb helper instance to use as template
1788 * @fb_width: desired fb width
1789 * @fb_height: desired fb height
1791 * Sets up the variable fbdev metainformation from the given fb helper instance
1792 * and the drm framebuffer allocated in &drm_fb_helper.fb.
1794 * Drivers should call this (or their equivalent setup code) from their
1795 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
1796 * backing storage framebuffer.
1798 void drm_fb_helper_fill_var(struct fb_info
*info
, struct drm_fb_helper
*fb_helper
,
1799 uint32_t fb_width
, uint32_t fb_height
)
1801 struct drm_framebuffer
*fb
= fb_helper
->fb
;
1803 info
->pseudo_palette
= fb_helper
->pseudo_palette
;
1804 info
->var
.xres_virtual
= fb
->width
;
1805 info
->var
.yres_virtual
= fb
->height
;
1806 info
->var
.bits_per_pixel
= fb
->format
->cpp
[0] * 8;
1807 info
->var
.accel_flags
= FB_ACCELF_TEXT
;
1808 info
->var
.xoffset
= 0;
1809 info
->var
.yoffset
= 0;
1810 info
->var
.activate
= FB_ACTIVATE_NOW
;
1811 info
->var
.height
= -1;
1812 info
->var
.width
= -1;
1814 switch (fb
->format
->depth
) {
1816 info
->var
.red
.offset
= 0;
1817 info
->var
.green
.offset
= 0;
1818 info
->var
.blue
.offset
= 0;
1819 info
->var
.red
.length
= 8; /* 8bit DAC */
1820 info
->var
.green
.length
= 8;
1821 info
->var
.blue
.length
= 8;
1822 info
->var
.transp
.offset
= 0;
1823 info
->var
.transp
.length
= 0;
1826 info
->var
.red
.offset
= 10;
1827 info
->var
.green
.offset
= 5;
1828 info
->var
.blue
.offset
= 0;
1829 info
->var
.red
.length
= 5;
1830 info
->var
.green
.length
= 5;
1831 info
->var
.blue
.length
= 5;
1832 info
->var
.transp
.offset
= 15;
1833 info
->var
.transp
.length
= 1;
1836 info
->var
.red
.offset
= 11;
1837 info
->var
.green
.offset
= 5;
1838 info
->var
.blue
.offset
= 0;
1839 info
->var
.red
.length
= 5;
1840 info
->var
.green
.length
= 6;
1841 info
->var
.blue
.length
= 5;
1842 info
->var
.transp
.offset
= 0;
1845 info
->var
.red
.offset
= 16;
1846 info
->var
.green
.offset
= 8;
1847 info
->var
.blue
.offset
= 0;
1848 info
->var
.red
.length
= 8;
1849 info
->var
.green
.length
= 8;
1850 info
->var
.blue
.length
= 8;
1851 info
->var
.transp
.offset
= 0;
1852 info
->var
.transp
.length
= 0;
1855 info
->var
.red
.offset
= 16;
1856 info
->var
.green
.offset
= 8;
1857 info
->var
.blue
.offset
= 0;
1858 info
->var
.red
.length
= 8;
1859 info
->var
.green
.length
= 8;
1860 info
->var
.blue
.length
= 8;
1861 info
->var
.transp
.offset
= 24;
1862 info
->var
.transp
.length
= 8;
1868 info
->var
.xres
= fb_width
;
1869 info
->var
.yres
= fb_height
;
1871 EXPORT_SYMBOL(drm_fb_helper_fill_var
);
1873 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper
*fb_helper
,
1877 struct drm_connector
*connector
;
1881 drm_fb_helper_for_each_connector(fb_helper
, i
) {
1882 connector
= fb_helper
->connector_info
[i
]->connector
;
1883 count
+= connector
->funcs
->fill_modes(connector
, maxX
, maxY
);
1889 struct drm_display_mode
*drm_has_preferred_mode(struct drm_fb_helper_connector
*fb_connector
, int width
, int height
)
1891 struct drm_display_mode
*mode
;
1893 list_for_each_entry(mode
, &fb_connector
->connector
->modes
, head
) {
1894 if (mode
->hdisplay
> width
||
1895 mode
->vdisplay
> height
)
1897 if (mode
->type
& DRM_MODE_TYPE_PREFERRED
)
1902 EXPORT_SYMBOL(drm_has_preferred_mode
);
1904 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector
*fb_connector
)
1906 return fb_connector
->connector
->cmdline_mode
.specified
;
1909 struct drm_display_mode
*drm_pick_cmdline_mode(struct drm_fb_helper_connector
*fb_helper_conn
)
1911 struct drm_cmdline_mode
*cmdline_mode
;
1912 struct drm_display_mode
*mode
;
1913 bool prefer_non_interlace
;
1915 cmdline_mode
= &fb_helper_conn
->connector
->cmdline_mode
;
1916 if (cmdline_mode
->specified
== false)
1919 /* attempt to find a matching mode in the list of modes
1920 * we have gotten so far, if not add a CVT mode that conforms
1922 if (cmdline_mode
->rb
|| cmdline_mode
->margins
)
1925 prefer_non_interlace
= !cmdline_mode
->interlace
;
1927 list_for_each_entry(mode
, &fb_helper_conn
->connector
->modes
, head
) {
1928 /* check width/height */
1929 if (mode
->hdisplay
!= cmdline_mode
->xres
||
1930 mode
->vdisplay
!= cmdline_mode
->yres
)
1933 if (cmdline_mode
->refresh_specified
) {
1934 if (mode
->vrefresh
!= cmdline_mode
->refresh
)
1938 if (cmdline_mode
->interlace
) {
1939 if (!(mode
->flags
& DRM_MODE_FLAG_INTERLACE
))
1941 } else if (prefer_non_interlace
) {
1942 if (mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
1948 if (prefer_non_interlace
) {
1949 prefer_non_interlace
= false;
1954 mode
= drm_mode_create_from_cmdline_mode(fb_helper_conn
->connector
->dev
,
1956 list_add(&mode
->head
, &fb_helper_conn
->connector
->modes
);
1959 EXPORT_SYMBOL(drm_pick_cmdline_mode
);
1961 static bool drm_connector_enabled(struct drm_connector
*connector
, bool strict
)
1966 enable
= connector
->status
== connector_status_connected
;
1968 enable
= connector
->status
!= connector_status_disconnected
;
1973 static void drm_enable_connectors(struct drm_fb_helper
*fb_helper
,
1976 bool any_enabled
= false;
1977 struct drm_connector
*connector
;
1980 drm_fb_helper_for_each_connector(fb_helper
, i
) {
1981 connector
= fb_helper
->connector_info
[i
]->connector
;
1982 enabled
[i
] = drm_connector_enabled(connector
, true);
1983 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector
->base
.id
,
1984 enabled
[i
] ? "yes" : "no");
1985 any_enabled
|= enabled
[i
];
1991 drm_fb_helper_for_each_connector(fb_helper
, i
) {
1992 connector
= fb_helper
->connector_info
[i
]->connector
;
1993 enabled
[i
] = drm_connector_enabled(connector
, false);
1997 static bool drm_target_cloned(struct drm_fb_helper
*fb_helper
,
1998 struct drm_display_mode
**modes
,
1999 struct drm_fb_offset
*offsets
,
2000 bool *enabled
, int width
, int height
)
2003 bool can_clone
= false;
2004 struct drm_fb_helper_connector
*fb_helper_conn
;
2005 struct drm_display_mode
*dmt_mode
, *mode
;
2007 /* only contemplate cloning in the single crtc case */
2008 if (fb_helper
->crtc_count
> 1)
2012 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2017 /* only contemplate cloning if more than one connector is enabled */
2021 /* check the command line or if nothing common pick 1024x768 */
2023 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2026 fb_helper_conn
= fb_helper
->connector_info
[i
];
2027 modes
[i
] = drm_pick_cmdline_mode(fb_helper_conn
);
2032 for (j
= 0; j
< i
; j
++) {
2035 if (!drm_mode_equal(modes
[j
], modes
[i
]))
2041 DRM_DEBUG_KMS("can clone using command line\n");
2045 /* try and find a 1024x768 mode on each connector */
2047 dmt_mode
= drm_mode_find_dmt(fb_helper
->dev
, 1024, 768, 60, false);
2049 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2053 fb_helper_conn
= fb_helper
->connector_info
[i
];
2054 list_for_each_entry(mode
, &fb_helper_conn
->connector
->modes
, head
) {
2055 if (drm_mode_equal(mode
, dmt_mode
))
2063 DRM_DEBUG_KMS("can clone using 1024x768\n");
2066 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
2070 static int drm_get_tile_offsets(struct drm_fb_helper
*fb_helper
,
2071 struct drm_display_mode
**modes
,
2072 struct drm_fb_offset
*offsets
,
2074 int h_idx
, int v_idx
)
2076 struct drm_fb_helper_connector
*fb_helper_conn
;
2078 int hoffset
= 0, voffset
= 0;
2080 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2081 fb_helper_conn
= fb_helper
->connector_info
[i
];
2082 if (!fb_helper_conn
->connector
->has_tile
)
2085 if (!modes
[i
] && (h_idx
|| v_idx
)) {
2086 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i
,
2087 fb_helper_conn
->connector
->base
.id
);
2090 if (fb_helper_conn
->connector
->tile_h_loc
< h_idx
)
2091 hoffset
+= modes
[i
]->hdisplay
;
2093 if (fb_helper_conn
->connector
->tile_v_loc
< v_idx
)
2094 voffset
+= modes
[i
]->vdisplay
;
2096 offsets
[idx
].x
= hoffset
;
2097 offsets
[idx
].y
= voffset
;
2098 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset
, voffset
, h_idx
, v_idx
);
2102 static bool drm_target_preferred(struct drm_fb_helper
*fb_helper
,
2103 struct drm_display_mode
**modes
,
2104 struct drm_fb_offset
*offsets
,
2105 bool *enabled
, int width
, int height
)
2107 struct drm_fb_helper_connector
*fb_helper_conn
;
2108 const u64 mask
= BIT_ULL(fb_helper
->connector_count
) - 1;
2109 u64 conn_configured
= 0;
2114 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2115 fb_helper_conn
= fb_helper
->connector_info
[i
];
2117 if (conn_configured
& BIT_ULL(i
))
2120 if (enabled
[i
] == false) {
2121 conn_configured
|= BIT_ULL(i
);
2125 /* first pass over all the untiled connectors */
2126 if (tile_pass
== 0 && fb_helper_conn
->connector
->has_tile
)
2129 if (tile_pass
== 1) {
2130 if (fb_helper_conn
->connector
->tile_h_loc
!= 0 ||
2131 fb_helper_conn
->connector
->tile_v_loc
!= 0)
2135 if (fb_helper_conn
->connector
->tile_h_loc
!= tile_pass
- 1 &&
2136 fb_helper_conn
->connector
->tile_v_loc
!= tile_pass
- 1)
2137 /* if this tile_pass doesn't cover any of the tiles - keep going */
2141 * find the tile offsets for this pass - need to find
2142 * all tiles left and above
2144 drm_get_tile_offsets(fb_helper
, modes
, offsets
,
2145 i
, fb_helper_conn
->connector
->tile_h_loc
, fb_helper_conn
->connector
->tile_v_loc
);
2147 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
2148 fb_helper_conn
->connector
->base
.id
);
2150 /* got for command line mode first */
2151 modes
[i
] = drm_pick_cmdline_mode(fb_helper_conn
);
2153 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
2154 fb_helper_conn
->connector
->base
.id
, fb_helper_conn
->connector
->tile_group
? fb_helper_conn
->connector
->tile_group
->id
: 0);
2155 modes
[i
] = drm_has_preferred_mode(fb_helper_conn
, width
, height
);
2157 /* No preferred modes, pick one off the list */
2158 if (!modes
[i
] && !list_empty(&fb_helper_conn
->connector
->modes
)) {
2159 list_for_each_entry(modes
[i
], &fb_helper_conn
->connector
->modes
, head
)
2162 DRM_DEBUG_KMS("found mode %s\n", modes
[i
] ? modes
[i
]->name
:
2164 conn_configured
|= BIT_ULL(i
);
2167 if ((conn_configured
& mask
) != mask
) {
2174 static int drm_pick_crtcs(struct drm_fb_helper
*fb_helper
,
2175 struct drm_fb_helper_crtc
**best_crtcs
,
2176 struct drm_display_mode
**modes
,
2177 int n
, int width
, int height
)
2180 struct drm_connector
*connector
;
2181 const struct drm_connector_helper_funcs
*connector_funcs
;
2182 struct drm_encoder
*encoder
;
2183 int my_score
, best_score
, score
;
2184 struct drm_fb_helper_crtc
**crtcs
, *crtc
;
2185 struct drm_fb_helper_connector
*fb_helper_conn
;
2187 if (n
== fb_helper
->connector_count
)
2190 fb_helper_conn
= fb_helper
->connector_info
[n
];
2191 connector
= fb_helper_conn
->connector
;
2193 best_crtcs
[n
] = NULL
;
2194 best_score
= drm_pick_crtcs(fb_helper
, best_crtcs
, modes
, n
+1, width
, height
);
2195 if (modes
[n
] == NULL
)
2198 crtcs
= kzalloc(fb_helper
->connector_count
*
2199 sizeof(struct drm_fb_helper_crtc
*), GFP_KERNEL
);
2204 if (connector
->status
== connector_status_connected
)
2206 if (drm_has_cmdline_mode(fb_helper_conn
))
2208 if (drm_has_preferred_mode(fb_helper_conn
, width
, height
))
2211 connector_funcs
= connector
->helper_private
;
2214 * If the DRM device implements atomic hooks and ->best_encoder() is
2215 * NULL we fallback to the default drm_atomic_helper_best_encoder()
2218 if (drm_drv_uses_atomic_modeset(fb_helper
->dev
) &&
2219 !connector_funcs
->best_encoder
)
2220 encoder
= drm_atomic_helper_best_encoder(connector
);
2222 encoder
= connector_funcs
->best_encoder(connector
);
2228 * select a crtc for this connector and then attempt to configure
2229 * remaining connectors
2231 for (c
= 0; c
< fb_helper
->crtc_count
; c
++) {
2232 crtc
= &fb_helper
->crtc_info
[c
];
2234 if ((encoder
->possible_crtcs
& (1 << c
)) == 0)
2237 for (o
= 0; o
< n
; o
++)
2238 if (best_crtcs
[o
] == crtc
)
2242 /* ignore cloning unless only a single crtc */
2243 if (fb_helper
->crtc_count
> 1)
2246 if (!drm_mode_equal(modes
[o
], modes
[n
]))
2251 memcpy(crtcs
, best_crtcs
, n
* sizeof(struct drm_fb_helper_crtc
*));
2252 score
= my_score
+ drm_pick_crtcs(fb_helper
, crtcs
, modes
, n
+ 1,
2254 if (score
> best_score
) {
2256 memcpy(best_crtcs
, crtcs
,
2257 fb_helper
->connector_count
*
2258 sizeof(struct drm_fb_helper_crtc
*));
2266 static void drm_setup_crtcs(struct drm_fb_helper
*fb_helper
,
2267 u32 width
, u32 height
)
2269 struct drm_device
*dev
= fb_helper
->dev
;
2270 struct drm_fb_helper_crtc
**crtcs
;
2271 struct drm_display_mode
**modes
;
2272 struct drm_fb_offset
*offsets
;
2276 DRM_DEBUG_KMS("\n");
2277 if (drm_fb_helper_probe_connector_modes(fb_helper
, width
, height
) == 0)
2278 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
2280 /* prevent concurrent modification of connector_count by hotplug */
2281 lockdep_assert_held(&fb_helper
->lock
);
2282 lockdep_assert_held(&fb_helper
->dev
->mode_config
.mutex
);
2284 crtcs
= kcalloc(fb_helper
->connector_count
,
2285 sizeof(struct drm_fb_helper_crtc
*), GFP_KERNEL
);
2286 modes
= kcalloc(fb_helper
->connector_count
,
2287 sizeof(struct drm_display_mode
*), GFP_KERNEL
);
2288 offsets
= kcalloc(fb_helper
->connector_count
,
2289 sizeof(struct drm_fb_offset
), GFP_KERNEL
);
2290 enabled
= kcalloc(fb_helper
->connector_count
,
2291 sizeof(bool), GFP_KERNEL
);
2292 if (!crtcs
|| !modes
|| !enabled
|| !offsets
) {
2293 DRM_ERROR("Memory allocation failed\n");
2297 drm_enable_connectors(fb_helper
, enabled
);
2299 if (!(fb_helper
->funcs
->initial_config
&&
2300 fb_helper
->funcs
->initial_config(fb_helper
, crtcs
, modes
,
2302 enabled
, width
, height
))) {
2303 memset(modes
, 0, fb_helper
->connector_count
*sizeof(modes
[0]));
2304 memset(crtcs
, 0, fb_helper
->connector_count
*sizeof(crtcs
[0]));
2305 memset(offsets
, 0, fb_helper
->connector_count
*sizeof(offsets
[0]));
2307 if (!drm_target_cloned(fb_helper
, modes
, offsets
,
2308 enabled
, width
, height
) &&
2309 !drm_target_preferred(fb_helper
, modes
, offsets
,
2310 enabled
, width
, height
))
2311 DRM_ERROR("Unable to find initial modes\n");
2313 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2316 drm_pick_crtcs(fb_helper
, crtcs
, modes
, 0, width
, height
);
2319 /* need to set the modesets up here for use later */
2320 /* fill out the connector<->crtc mappings into the modesets */
2321 for (i
= 0; i
< fb_helper
->crtc_count
; i
++)
2322 drm_fb_helper_modeset_release(fb_helper
,
2323 &fb_helper
->crtc_info
[i
].mode_set
);
2325 drm_fb_helper_for_each_connector(fb_helper
, i
) {
2326 struct drm_display_mode
*mode
= modes
[i
];
2327 struct drm_fb_helper_crtc
*fb_crtc
= crtcs
[i
];
2328 struct drm_fb_offset
*offset
= &offsets
[i
];
2329 struct drm_mode_set
*modeset
= &fb_crtc
->mode_set
;
2331 if (mode
&& fb_crtc
) {
2332 struct drm_connector
*connector
=
2333 fb_helper
->connector_info
[i
]->connector
;
2335 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2336 mode
->name
, fb_crtc
->mode_set
.crtc
->base
.id
, offset
->x
, offset
->y
);
2338 fb_crtc
->desired_mode
= mode
;
2339 fb_crtc
->x
= offset
->x
;
2340 fb_crtc
->y
= offset
->y
;
2341 modeset
->mode
= drm_mode_duplicate(dev
,
2342 fb_crtc
->desired_mode
);
2343 drm_connector_get(connector
);
2344 modeset
->connectors
[modeset
->num_connectors
++] = connector
;
2345 modeset
->fb
= fb_helper
->fb
;
2346 modeset
->x
= offset
->x
;
2347 modeset
->y
= offset
->y
;
2358 * drm_fb_helper_initial_config - setup a sane initial connector configuration
2359 * @fb_helper: fb_helper device struct
2360 * @bpp_sel: bpp value to use for the framebuffer configuration
2362 * Scans the CRTCs and connectors and tries to put together an initial setup.
2363 * At the moment, this is a cloned configuration across all heads with
2364 * a new framebuffer object as the backing store.
2366 * Note that this also registers the fbdev and so allows userspace to call into
2367 * the driver through the fbdev interfaces.
2369 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
2370 * to let the driver allocate and initialize the fbdev info structure and the
2371 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
2372 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
2373 * values for the fbdev info structure.
2377 * When you have fbcon support built-in or already loaded, this function will do
2378 * a full modeset to setup the fbdev console. Due to locking misdesign in the
2379 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2380 * console_lock. Until console_unlock is called no dmesg lines will be sent out
2381 * to consoles, not even serial console. This means when your driver crashes,
2382 * you will see absolutely nothing else but a system stuck in this function,
2383 * with no further output. Any kind of printk() you place within your own driver
2384 * or in the drm core modeset code will also never show up.
2386 * Standard debug practice is to run the fbcon setup without taking the
2387 * console_lock as a hack, to be able to see backtraces and crashes on the
2388 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2391 * The other option is to just disable fbdev emulation since very likely the
2392 * first modeset from userspace will crash in the same way, and is even easier
2393 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
2394 * kernel cmdline option.
2397 * Zero if everything went ok, nonzero otherwise.
2399 int drm_fb_helper_initial_config(struct drm_fb_helper
*fb_helper
, int bpp_sel
)
2401 struct drm_device
*dev
= fb_helper
->dev
;
2402 struct fb_info
*info
;
2405 if (!drm_fbdev_emulation
)
2408 mutex_lock(&fb_helper
->lock
);
2409 mutex_lock(&dev
->mode_config
.mutex
);
2410 drm_setup_crtcs(fb_helper
,
2411 dev
->mode_config
.max_width
,
2412 dev
->mode_config
.max_height
);
2413 ret
= drm_fb_helper_single_fb_probe(fb_helper
, bpp_sel
);
2414 mutex_unlock(&dev
->mode_config
.mutex
);
2415 mutex_unlock(&fb_helper
->lock
);
2419 info
= fb_helper
->fbdev
;
2420 info
->var
.pixclock
= 0;
2421 ret
= register_framebuffer(info
);
2425 dev_info(dev
->dev
, "fb%d: %s frame buffer device\n",
2426 info
->node
, info
->fix
.id
);
2428 mutex_lock(&kernel_fb_helper_lock
);
2429 if (list_empty(&kernel_fb_helper_list
))
2430 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op
);
2432 list_add(&fb_helper
->kernel_fb_list
, &kernel_fb_helper_list
);
2433 mutex_unlock(&kernel_fb_helper_lock
);
2437 EXPORT_SYMBOL(drm_fb_helper_initial_config
);
2440 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
2441 * probing all the outputs attached to the fb
2442 * @fb_helper: the drm_fb_helper
2444 * Scan the connectors attached to the fb_helper and try to put together a
2445 * setup after notification of a change in output configuration.
2447 * Called at runtime, takes the mode config locks to be able to check/change the
2448 * modeset configuration. Must be run from process context (which usually means
2449 * either the output polling work or a work item launched from the driver's
2450 * hotplug interrupt).
2452 * Note that drivers may call this even before calling
2453 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
2454 * for a race-free fbcon setup and will make sure that the fbdev emulation will
2455 * not miss any hotplug events.
2458 * 0 on success and a non-zero error code otherwise.
2460 int drm_fb_helper_hotplug_event(struct drm_fb_helper
*fb_helper
)
2462 struct drm_device
*dev
= fb_helper
->dev
;
2465 if (!drm_fbdev_emulation
)
2468 mutex_lock(&fb_helper
->lock
);
2469 if (!fb_helper
->fb
|| !drm_fb_helper_is_bound(fb_helper
)) {
2470 fb_helper
->delayed_hotplug
= true;
2471 mutex_unlock(&fb_helper
->lock
);
2475 DRM_DEBUG_KMS("\n");
2477 mutex_lock(&dev
->mode_config
.mutex
);
2478 drm_setup_crtcs(fb_helper
, fb_helper
->fb
->width
, fb_helper
->fb
->height
);
2480 mutex_unlock(&dev
->mode_config
.mutex
);
2481 mutex_unlock(&fb_helper
->lock
);
2483 drm_fb_helper_set_par(fb_helper
->fbdev
);
2487 EXPORT_SYMBOL(drm_fb_helper_hotplug_event
);
2489 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
2490 * but the module doesn't depend on any fb console symbols. At least
2491 * attempt to load fbcon to avoid leaving the system without a usable console.
2493 int __init
drm_fb_helper_modinit(void)
2495 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
2496 const char name
[] = "fbcon";
2497 struct module
*fbcon
;
2499 mutex_lock(&module_mutex
);
2500 fbcon
= find_module(name
);
2501 mutex_unlock(&module_mutex
);
2504 request_module_nowait(name
);
2508 EXPORT_SYMBOL(drm_fb_helper_modinit
);