2 * Copyright (c) 2006-2009 Red Hat Inc.
3 * Copyright (c) 2006-2008 Intel Corporation
4 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
6 * DRM framebuffer helper functions
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
27 * Dave Airlie <airlied@linux.ie>
28 * Jesse Barnes <jesse.barnes@intel.com>
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #include <linux/console.h>
33 #include <linux/dma-buf.h>
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/slab.h>
37 #include <linux/sysrq.h>
38 #include <linux/vmalloc.h>
40 #include <drm/drm_atomic.h>
41 #include <drm/drm_crtc.h>
42 #include <drm/drm_crtc_helper.h>
43 #include <drm/drm_drv.h>
44 #include <drm/drm_fb_helper.h>
45 #include <drm/drm_fourcc.h>
46 #include <drm/drm_print.h>
47 #include <drm/drm_vblank.h>
49 #include "drm_crtc_helper_internal.h"
50 #include "drm_internal.h"
52 static bool drm_fbdev_emulation
= true;
53 module_param_named(fbdev_emulation
, drm_fbdev_emulation
, bool, 0600);
54 MODULE_PARM_DESC(fbdev_emulation
,
55 "Enable legacy fbdev emulation [default=true]");
57 static int drm_fbdev_overalloc
= CONFIG_DRM_FBDEV_OVERALLOC
;
58 module_param(drm_fbdev_overalloc
, int, 0444);
59 MODULE_PARM_DESC(drm_fbdev_overalloc
,
60 "Overallocation of the fbdev buffer (%) [default="
61 __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC
) "]");
64 * In order to keep user-space compatibility, we want in certain use-cases
65 * to keep leaking the fbdev physical address to the user-space program
66 * handling the fbdev buffer.
67 * This is a bad habit essentially kept into closed source opengl driver
68 * that should really be moved into open-source upstream projects instead
69 * of using legacy physical addresses in user space to communicate with
70 * other out-of-tree kernel modules.
72 * This module_param *should* be removed as soon as possible and be
73 * considered as a broken and legacy behaviour from a modern fbdev device.
75 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
76 static bool drm_leak_fbdev_smem
= false;
77 module_param_unsafe(drm_leak_fbdev_smem
, bool, 0600);
78 MODULE_PARM_DESC(drm_leak_fbdev_smem
,
79 "Allow unsafe leaking fbdev physical smem address [default=false]");
82 static LIST_HEAD(kernel_fb_helper_list
);
83 static DEFINE_MUTEX(kernel_fb_helper_lock
);
88 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
89 * mode setting driver. They can be used mostly independently from the crtc
90 * helper functions used by many drivers to implement the kernel mode setting
93 * Drivers that support a dumb buffer with a virtual address and mmap support,
94 * should try out the generic fbdev emulation using drm_fbdev_generic_setup().
95 * It will automatically set up deferred I/O if the driver requires a shadow
98 * For other drivers, setup fbdev emulation by calling
99 * drm_fb_helper_fbdev_setup() and tear it down by calling
100 * drm_fb_helper_fbdev_teardown().
102 * At runtime drivers should restore the fbdev console by using
103 * drm_fb_helper_lastclose() as their &drm_driver.lastclose callback.
104 * They should also notify the fb helper code from updates to the output
105 * configuration by using drm_fb_helper_output_poll_changed() as their
106 * &drm_mode_config_funcs.output_poll_changed callback.
108 * For suspend/resume consider using drm_mode_config_helper_suspend() and
109 * drm_mode_config_helper_resume() which takes care of fbdev as well.
111 * All other functions exported by the fb helper library can be used to
112 * implement the fbdev driver interface by the driver.
114 * It is possible, though perhaps somewhat tricky, to implement race-free
115 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
116 * helper must be called first to initialize the minimum required to make
117 * hotplug detection work. Drivers also need to make sure to properly set up
118 * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
119 * it is safe to enable interrupts and start processing hotplug events. At the
120 * same time, drivers should initialize all modeset objects such as CRTCs,
121 * encoders and connectors. To finish up the fbdev helper initialization, the
122 * drm_fb_helper_init() function is called. To probe for all attached displays
123 * and set up an initial configuration using the detected hardware, drivers
124 * should call drm_fb_helper_initial_config().
126 * If &drm_framebuffer_funcs.dirty is set, the
127 * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
128 * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
129 * away. This worker then calls the dirty() function ensuring that it will
130 * always run in process context since the fb_*() function could be running in
131 * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
132 * callback it will also schedule dirty_work with the damage collected from the
135 * Deferred I/O is not compatible with SHMEM. Such drivers should request an
136 * fbdev shadow buffer and call drm_fbdev_generic_setup() instead.
139 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc
*crtc
)
141 uint16_t *r_base
, *g_base
, *b_base
;
143 if (crtc
->funcs
->gamma_set
== NULL
)
146 r_base
= crtc
->gamma_store
;
147 g_base
= r_base
+ crtc
->gamma_size
;
148 b_base
= g_base
+ crtc
->gamma_size
;
150 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
,
151 crtc
->gamma_size
, NULL
);
155 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
156 * @info: fbdev registered by the helper
158 int drm_fb_helper_debug_enter(struct fb_info
*info
)
160 struct drm_fb_helper
*helper
= info
->par
;
161 const struct drm_crtc_helper_funcs
*funcs
;
162 struct drm_mode_set
*mode_set
;
164 list_for_each_entry(helper
, &kernel_fb_helper_list
, kernel_fb_list
) {
165 mutex_lock(&helper
->client
.modeset_mutex
);
166 drm_client_for_each_modeset(mode_set
, &helper
->client
) {
167 if (!mode_set
->crtc
->enabled
)
170 funcs
= mode_set
->crtc
->helper_private
;
171 if (funcs
->mode_set_base_atomic
== NULL
)
174 if (drm_drv_uses_atomic_modeset(mode_set
->crtc
->dev
))
177 funcs
->mode_set_base_atomic(mode_set
->crtc
,
181 ENTER_ATOMIC_MODE_SET
);
183 mutex_unlock(&helper
->client
.modeset_mutex
);
188 EXPORT_SYMBOL(drm_fb_helper_debug_enter
);
191 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
192 * @info: fbdev registered by the helper
194 int drm_fb_helper_debug_leave(struct fb_info
*info
)
196 struct drm_fb_helper
*helper
= info
->par
;
197 struct drm_client_dev
*client
= &helper
->client
;
198 struct drm_crtc
*crtc
;
199 const struct drm_crtc_helper_funcs
*funcs
;
200 struct drm_mode_set
*mode_set
;
201 struct drm_framebuffer
*fb
;
203 mutex_lock(&client
->modeset_mutex
);
204 drm_client_for_each_modeset(mode_set
, client
) {
205 crtc
= mode_set
->crtc
;
206 if (drm_drv_uses_atomic_modeset(crtc
->dev
))
209 funcs
= crtc
->helper_private
;
210 fb
= crtc
->primary
->fb
;
216 DRM_ERROR("no fb to restore??\n");
220 if (funcs
->mode_set_base_atomic
== NULL
)
223 drm_fb_helper_restore_lut_atomic(mode_set
->crtc
);
224 funcs
->mode_set_base_atomic(mode_set
->crtc
, fb
, crtc
->x
,
225 crtc
->y
, LEAVE_ATOMIC_MODE_SET
);
227 mutex_unlock(&client
->modeset_mutex
);
231 EXPORT_SYMBOL(drm_fb_helper_debug_leave
);
234 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
235 * @fb_helper: driver-allocated fbdev helper, can be NULL
237 * This should be called from driver's drm &drm_driver.lastclose callback
238 * when implementing an fbcon on top of kms using this helper. This ensures that
239 * the user isn't greeted with a black screen when e.g. X dies.
242 * Zero if everything went ok, negative error code otherwise.
244 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper
*fb_helper
)
249 if (!drm_fbdev_emulation
|| !fb_helper
)
252 if (READ_ONCE(fb_helper
->deferred_setup
))
255 mutex_lock(&fb_helper
->lock
);
258 * We should bail out here if there is a master by dropping _force.
259 * Currently these igt tests fail if we do that:
260 * - kms_fbcon_fbt@psr
261 * - kms_fbcon_fbt@psr-suspend
263 * So first these tests need to be fixed so they drop master or don't
266 ret
= drm_client_modeset_commit_force(&fb_helper
->client
);
268 do_delayed
= fb_helper
->delayed_hotplug
;
270 fb_helper
->delayed_hotplug
= false;
271 mutex_unlock(&fb_helper
->lock
);
274 drm_fb_helper_hotplug_event(fb_helper
);
278 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked
);
280 #ifdef CONFIG_MAGIC_SYSRQ
282 * restore fbcon display for all kms driver's using this helper, used for sysrq
283 * and panic handling.
285 static bool drm_fb_helper_force_kernel_mode(void)
287 bool ret
, error
= false;
288 struct drm_fb_helper
*helper
;
290 if (list_empty(&kernel_fb_helper_list
))
293 list_for_each_entry(helper
, &kernel_fb_helper_list
, kernel_fb_list
) {
294 struct drm_device
*dev
= helper
->dev
;
296 if (dev
->switch_power_state
== DRM_SWITCH_POWER_OFF
)
299 mutex_lock(&helper
->lock
);
300 ret
= drm_client_modeset_commit_force(&helper
->client
);
303 mutex_unlock(&helper
->lock
);
308 static void drm_fb_helper_restore_work_fn(struct work_struct
*ignored
)
312 ret
= drm_fb_helper_force_kernel_mode();
314 DRM_ERROR("Failed to restore crtc configuration\n");
316 static DECLARE_WORK(drm_fb_helper_restore_work
, drm_fb_helper_restore_work_fn
);
318 static void drm_fb_helper_sysrq(int dummy1
)
320 schedule_work(&drm_fb_helper_restore_work
);
323 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op
= {
324 .handler
= drm_fb_helper_sysrq
,
325 .help_msg
= "force-fb(V)",
326 .action_msg
= "Restore framebuffer console",
329 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op
= { };
332 static void drm_fb_helper_dpms(struct fb_info
*info
, int dpms_mode
)
334 struct drm_fb_helper
*fb_helper
= info
->par
;
336 mutex_lock(&fb_helper
->lock
);
337 drm_client_modeset_dpms(&fb_helper
->client
, dpms_mode
);
338 mutex_unlock(&fb_helper
->lock
);
342 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
343 * @blank: desired blanking state
344 * @info: fbdev registered by the helper
346 int drm_fb_helper_blank(int blank
, struct fb_info
*info
)
348 if (oops_in_progress
)
352 /* Display: On; HSync: On, VSync: On */
353 case FB_BLANK_UNBLANK
:
354 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_ON
);
356 /* Display: Off; HSync: On, VSync: On */
357 case FB_BLANK_NORMAL
:
358 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_STANDBY
);
360 /* Display: Off; HSync: Off, VSync: On */
361 case FB_BLANK_HSYNC_SUSPEND
:
362 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_STANDBY
);
364 /* Display: Off; HSync: On, VSync: Off */
365 case FB_BLANK_VSYNC_SUSPEND
:
366 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_SUSPEND
);
368 /* Display: Off; HSync: Off, VSync: Off */
369 case FB_BLANK_POWERDOWN
:
370 drm_fb_helper_dpms(info
, DRM_MODE_DPMS_OFF
);
375 EXPORT_SYMBOL(drm_fb_helper_blank
);
377 static void drm_fb_helper_resume_worker(struct work_struct
*work
)
379 struct drm_fb_helper
*helper
= container_of(work
, struct drm_fb_helper
,
383 fb_set_suspend(helper
->fbdev
, 0);
387 static void drm_fb_helper_dirty_blit_real(struct drm_fb_helper
*fb_helper
,
388 struct drm_clip_rect
*clip
)
390 struct drm_framebuffer
*fb
= fb_helper
->fb
;
391 unsigned int cpp
= fb
->format
->cpp
[0];
392 size_t offset
= clip
->y1
* fb
->pitches
[0] + clip
->x1
* cpp
;
393 void *src
= fb_helper
->fbdev
->screen_buffer
+ offset
;
394 void *dst
= fb_helper
->buffer
->vaddr
+ offset
;
395 size_t len
= (clip
->x2
- clip
->x1
) * cpp
;
398 for (y
= clip
->y1
; y
< clip
->y2
; y
++) {
399 memcpy(dst
, src
, len
);
400 src
+= fb
->pitches
[0];
401 dst
+= fb
->pitches
[0];
405 static void drm_fb_helper_dirty_work(struct work_struct
*work
)
407 struct drm_fb_helper
*helper
= container_of(work
, struct drm_fb_helper
,
409 struct drm_clip_rect
*clip
= &helper
->dirty_clip
;
410 struct drm_clip_rect clip_copy
;
414 spin_lock_irqsave(&helper
->dirty_lock
, flags
);
416 clip
->x1
= clip
->y1
= ~0;
417 clip
->x2
= clip
->y2
= 0;
418 spin_unlock_irqrestore(&helper
->dirty_lock
, flags
);
420 /* call dirty callback only when it has been really touched */
421 if (clip_copy
.x1
< clip_copy
.x2
&& clip_copy
.y1
< clip_copy
.y2
) {
423 /* Generic fbdev uses a shadow buffer */
424 if (helper
->buffer
) {
425 vaddr
= drm_client_buffer_vmap(helper
->buffer
);
428 drm_fb_helper_dirty_blit_real(helper
, &clip_copy
);
430 if (helper
->fb
->funcs
->dirty
)
431 helper
->fb
->funcs
->dirty(helper
->fb
, NULL
, 0, 0,
435 drm_client_buffer_vunmap(helper
->buffer
);
440 * drm_fb_helper_prepare - setup a drm_fb_helper structure
442 * @helper: driver-allocated fbdev helper structure to set up
443 * @funcs: pointer to structure of functions associate with this helper
445 * Sets up the bare minimum to make the framebuffer helper usable. This is
446 * useful to implement race-free initialization of the polling helpers.
448 void drm_fb_helper_prepare(struct drm_device
*dev
, struct drm_fb_helper
*helper
,
449 const struct drm_fb_helper_funcs
*funcs
)
451 INIT_LIST_HEAD(&helper
->kernel_fb_list
);
452 spin_lock_init(&helper
->dirty_lock
);
453 INIT_WORK(&helper
->resume_work
, drm_fb_helper_resume_worker
);
454 INIT_WORK(&helper
->dirty_work
, drm_fb_helper_dirty_work
);
455 helper
->dirty_clip
.x1
= helper
->dirty_clip
.y1
= ~0;
456 mutex_init(&helper
->lock
);
457 helper
->funcs
= funcs
;
460 EXPORT_SYMBOL(drm_fb_helper_prepare
);
463 * drm_fb_helper_init - initialize a &struct drm_fb_helper
465 * @fb_helper: driver-allocated fbdev helper structure to initialize
466 * @max_conn_count: max connector count (not used)
468 * This allocates the structures for the fbdev helper with the given limits.
469 * Note that this won't yet touch the hardware (through the driver interfaces)
470 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
471 * to allow driver writes more control over the exact init sequence.
473 * Drivers must call drm_fb_helper_prepare() before calling this function.
476 * Zero if everything went ok, nonzero otherwise.
478 int drm_fb_helper_init(struct drm_device
*dev
,
479 struct drm_fb_helper
*fb_helper
,
484 if (!drm_fbdev_emulation
) {
485 dev
->fb_helper
= fb_helper
;
490 * If this is not the generic fbdev client, initialize a drm_client
491 * without callbacks so we can use the modesets.
493 if (!fb_helper
->client
.funcs
) {
494 ret
= drm_client_init(dev
, &fb_helper
->client
, "drm_fb_helper", NULL
);
499 dev
->fb_helper
= fb_helper
;
503 EXPORT_SYMBOL(drm_fb_helper_init
);
506 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
507 * @fb_helper: driver-allocated fbdev helper
509 * A helper to alloc fb_info and the members cmap and apertures. Called
510 * by the driver within the fb_probe fb_helper callback function. Drivers do not
511 * need to release the allocated fb_info structure themselves, this is
512 * automatically done when calling drm_fb_helper_fini().
515 * fb_info pointer if things went okay, pointer containing error code
518 struct fb_info
*drm_fb_helper_alloc_fbi(struct drm_fb_helper
*fb_helper
)
520 struct device
*dev
= fb_helper
->dev
->dev
;
521 struct fb_info
*info
;
524 info
= framebuffer_alloc(0, dev
);
526 return ERR_PTR(-ENOMEM
);
528 ret
= fb_alloc_cmap(&info
->cmap
, 256, 0);
532 info
->apertures
= alloc_apertures(1);
533 if (!info
->apertures
) {
538 fb_helper
->fbdev
= info
;
539 info
->skip_vt_switch
= true;
544 fb_dealloc_cmap(&info
->cmap
);
546 framebuffer_release(info
);
549 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi
);
552 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
553 * @fb_helper: driver-allocated fbdev helper, can be NULL
555 * A wrapper around unregister_framebuffer, to release the fb_info
556 * framebuffer device. This must be called before releasing all resources for
557 * @fb_helper by calling drm_fb_helper_fini().
559 void drm_fb_helper_unregister_fbi(struct drm_fb_helper
*fb_helper
)
561 if (fb_helper
&& fb_helper
->fbdev
)
562 unregister_framebuffer(fb_helper
->fbdev
);
564 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi
);
567 * drm_fb_helper_fini - finialize a &struct drm_fb_helper
568 * @fb_helper: driver-allocated fbdev helper, can be NULL
570 * This cleans up all remaining resources associated with @fb_helper. Must be
571 * called after drm_fb_helper_unlink_fbi() was called.
573 void drm_fb_helper_fini(struct drm_fb_helper
*fb_helper
)
575 struct fb_info
*info
;
580 fb_helper
->dev
->fb_helper
= NULL
;
582 if (!drm_fbdev_emulation
)
585 cancel_work_sync(&fb_helper
->resume_work
);
586 cancel_work_sync(&fb_helper
->dirty_work
);
588 info
= fb_helper
->fbdev
;
591 fb_dealloc_cmap(&info
->cmap
);
592 framebuffer_release(info
);
594 fb_helper
->fbdev
= NULL
;
596 mutex_lock(&kernel_fb_helper_lock
);
597 if (!list_empty(&fb_helper
->kernel_fb_list
)) {
598 list_del(&fb_helper
->kernel_fb_list
);
599 if (list_empty(&kernel_fb_helper_list
))
600 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op
);
602 mutex_unlock(&kernel_fb_helper_lock
);
604 mutex_destroy(&fb_helper
->lock
);
606 if (!fb_helper
->client
.funcs
)
607 drm_client_release(&fb_helper
->client
);
609 EXPORT_SYMBOL(drm_fb_helper_fini
);
612 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
613 * @fb_helper: driver-allocated fbdev helper, can be NULL
615 * A wrapper around unlink_framebuffer implemented by fbdev core
617 void drm_fb_helper_unlink_fbi(struct drm_fb_helper
*fb_helper
)
619 if (fb_helper
&& fb_helper
->fbdev
)
620 unlink_framebuffer(fb_helper
->fbdev
);
622 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi
);
624 static bool drm_fbdev_use_shadow_fb(struct drm_fb_helper
*fb_helper
)
626 struct drm_device
*dev
= fb_helper
->dev
;
627 struct drm_framebuffer
*fb
= fb_helper
->fb
;
629 return dev
->mode_config
.prefer_shadow_fbdev
||
630 dev
->mode_config
.prefer_shadow
||
634 static void drm_fb_helper_dirty(struct fb_info
*info
, u32 x
, u32 y
,
635 u32 width
, u32 height
)
637 struct drm_fb_helper
*helper
= info
->par
;
638 struct drm_clip_rect
*clip
= &helper
->dirty_clip
;
641 if (!drm_fbdev_use_shadow_fb(helper
))
644 spin_lock_irqsave(&helper
->dirty_lock
, flags
);
645 clip
->x1
= min_t(u32
, clip
->x1
, x
);
646 clip
->y1
= min_t(u32
, clip
->y1
, y
);
647 clip
->x2
= max_t(u32
, clip
->x2
, x
+ width
);
648 clip
->y2
= max_t(u32
, clip
->y2
, y
+ height
);
649 spin_unlock_irqrestore(&helper
->dirty_lock
, flags
);
651 schedule_work(&helper
->dirty_work
);
655 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
656 * @info: fb_info struct pointer
657 * @pagelist: list of dirty mmap framebuffer pages
659 * This function is used as the &fb_deferred_io.deferred_io
660 * callback function for flushing the fbdev mmap writes.
662 void drm_fb_helper_deferred_io(struct fb_info
*info
,
663 struct list_head
*pagelist
)
665 unsigned long start
, end
, min
, max
;
671 list_for_each_entry(page
, pagelist
, lru
) {
672 start
= page
->index
<< PAGE_SHIFT
;
673 end
= start
+ PAGE_SIZE
- 1;
674 min
= min(min
, start
);
679 y1
= min
/ info
->fix
.line_length
;
680 y2
= min_t(u32
, DIV_ROUND_UP(max
, info
->fix
.line_length
),
682 drm_fb_helper_dirty(info
, 0, y1
, info
->var
.xres
, y2
- y1
);
685 EXPORT_SYMBOL(drm_fb_helper_deferred_io
);
688 * drm_fb_helper_sys_read - wrapper around fb_sys_read
689 * @info: fb_info struct pointer
690 * @buf: userspace buffer to read from framebuffer memory
691 * @count: number of bytes to read from framebuffer memory
692 * @ppos: read offset within framebuffer memory
694 * A wrapper around fb_sys_read implemented by fbdev core
696 ssize_t
drm_fb_helper_sys_read(struct fb_info
*info
, char __user
*buf
,
697 size_t count
, loff_t
*ppos
)
699 return fb_sys_read(info
, buf
, count
, ppos
);
701 EXPORT_SYMBOL(drm_fb_helper_sys_read
);
704 * drm_fb_helper_sys_write - wrapper around fb_sys_write
705 * @info: fb_info struct pointer
706 * @buf: userspace buffer to write to framebuffer memory
707 * @count: number of bytes to write to framebuffer memory
708 * @ppos: write offset within framebuffer memory
710 * A wrapper around fb_sys_write implemented by fbdev core
712 ssize_t
drm_fb_helper_sys_write(struct fb_info
*info
, const char __user
*buf
,
713 size_t count
, loff_t
*ppos
)
717 ret
= fb_sys_write(info
, buf
, count
, ppos
);
719 drm_fb_helper_dirty(info
, 0, 0, info
->var
.xres
,
724 EXPORT_SYMBOL(drm_fb_helper_sys_write
);
727 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
728 * @info: fbdev registered by the helper
729 * @rect: info about rectangle to fill
731 * A wrapper around sys_fillrect implemented by fbdev core
733 void drm_fb_helper_sys_fillrect(struct fb_info
*info
,
734 const struct fb_fillrect
*rect
)
736 sys_fillrect(info
, rect
);
737 drm_fb_helper_dirty(info
, rect
->dx
, rect
->dy
,
738 rect
->width
, rect
->height
);
740 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect
);
743 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
744 * @info: fbdev registered by the helper
745 * @area: info about area to copy
747 * A wrapper around sys_copyarea implemented by fbdev core
749 void drm_fb_helper_sys_copyarea(struct fb_info
*info
,
750 const struct fb_copyarea
*area
)
752 sys_copyarea(info
, area
);
753 drm_fb_helper_dirty(info
, area
->dx
, area
->dy
,
754 area
->width
, area
->height
);
756 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea
);
759 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
760 * @info: fbdev registered by the helper
761 * @image: info about image to blit
763 * A wrapper around sys_imageblit implemented by fbdev core
765 void drm_fb_helper_sys_imageblit(struct fb_info
*info
,
766 const struct fb_image
*image
)
768 sys_imageblit(info
, image
);
769 drm_fb_helper_dirty(info
, image
->dx
, image
->dy
,
770 image
->width
, image
->height
);
772 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit
);
775 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
776 * @info: fbdev registered by the helper
777 * @rect: info about rectangle to fill
779 * A wrapper around cfb_fillrect implemented by fbdev core
781 void drm_fb_helper_cfb_fillrect(struct fb_info
*info
,
782 const struct fb_fillrect
*rect
)
784 cfb_fillrect(info
, rect
);
785 drm_fb_helper_dirty(info
, rect
->dx
, rect
->dy
,
786 rect
->width
, rect
->height
);
788 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect
);
791 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
792 * @info: fbdev registered by the helper
793 * @area: info about area to copy
795 * A wrapper around cfb_copyarea implemented by fbdev core
797 void drm_fb_helper_cfb_copyarea(struct fb_info
*info
,
798 const struct fb_copyarea
*area
)
800 cfb_copyarea(info
, area
);
801 drm_fb_helper_dirty(info
, area
->dx
, area
->dy
,
802 area
->width
, area
->height
);
804 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea
);
807 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
808 * @info: fbdev registered by the helper
809 * @image: info about image to blit
811 * A wrapper around cfb_imageblit implemented by fbdev core
813 void drm_fb_helper_cfb_imageblit(struct fb_info
*info
,
814 const struct fb_image
*image
)
816 cfb_imageblit(info
, image
);
817 drm_fb_helper_dirty(info
, image
->dx
, image
->dy
,
818 image
->width
, image
->height
);
820 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit
);
823 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
824 * @fb_helper: driver-allocated fbdev helper, can be NULL
825 * @suspend: whether to suspend or resume
827 * A wrapper around fb_set_suspend implemented by fbdev core.
828 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
831 void drm_fb_helper_set_suspend(struct drm_fb_helper
*fb_helper
, bool suspend
)
833 if (fb_helper
&& fb_helper
->fbdev
)
834 fb_set_suspend(fb_helper
->fbdev
, suspend
);
836 EXPORT_SYMBOL(drm_fb_helper_set_suspend
);
839 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
840 * takes the console lock
841 * @fb_helper: driver-allocated fbdev helper, can be NULL
842 * @suspend: whether to suspend or resume
844 * A wrapper around fb_set_suspend() that takes the console lock. If the lock
845 * isn't available on resume, a worker is tasked with waiting for the lock
846 * to become available. The console lock can be pretty contented on resume
847 * due to all the printk activity.
849 * This function can be called multiple times with the same state since
850 * &fb_info.state is checked to see if fbdev is running or not before locking.
852 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
854 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper
*fb_helper
,
857 if (!fb_helper
|| !fb_helper
->fbdev
)
860 /* make sure there's no pending/ongoing resume */
861 flush_work(&fb_helper
->resume_work
);
864 if (fb_helper
->fbdev
->state
!= FBINFO_STATE_RUNNING
)
870 if (fb_helper
->fbdev
->state
== FBINFO_STATE_RUNNING
)
873 if (!console_trylock()) {
874 schedule_work(&fb_helper
->resume_work
);
879 fb_set_suspend(fb_helper
->fbdev
, suspend
);
882 EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked
);
884 static int setcmap_pseudo_palette(struct fb_cmap
*cmap
, struct fb_info
*info
)
886 u32
*palette
= (u32
*)info
->pseudo_palette
;
889 if (cmap
->start
+ cmap
->len
> 16)
892 for (i
= 0; i
< cmap
->len
; ++i
) {
893 u16 red
= cmap
->red
[i
];
894 u16 green
= cmap
->green
[i
];
895 u16 blue
= cmap
->blue
[i
];
898 red
>>= 16 - info
->var
.red
.length
;
899 green
>>= 16 - info
->var
.green
.length
;
900 blue
>>= 16 - info
->var
.blue
.length
;
901 value
= (red
<< info
->var
.red
.offset
) |
902 (green
<< info
->var
.green
.offset
) |
903 (blue
<< info
->var
.blue
.offset
);
904 if (info
->var
.transp
.length
> 0) {
905 u32 mask
= (1 << info
->var
.transp
.length
) - 1;
907 mask
<<= info
->var
.transp
.offset
;
910 palette
[cmap
->start
+ i
] = value
;
916 static int setcmap_legacy(struct fb_cmap
*cmap
, struct fb_info
*info
)
918 struct drm_fb_helper
*fb_helper
= info
->par
;
919 struct drm_mode_set
*modeset
;
920 struct drm_crtc
*crtc
;
924 drm_modeset_lock_all(fb_helper
->dev
);
925 drm_client_for_each_modeset(modeset
, &fb_helper
->client
) {
926 crtc
= modeset
->crtc
;
927 if (!crtc
->funcs
->gamma_set
|| !crtc
->gamma_size
)
930 if (cmap
->start
+ cmap
->len
> crtc
->gamma_size
)
933 r
= crtc
->gamma_store
;
934 g
= r
+ crtc
->gamma_size
;
935 b
= g
+ crtc
->gamma_size
;
937 memcpy(r
+ cmap
->start
, cmap
->red
, cmap
->len
* sizeof(*r
));
938 memcpy(g
+ cmap
->start
, cmap
->green
, cmap
->len
* sizeof(*g
));
939 memcpy(b
+ cmap
->start
, cmap
->blue
, cmap
->len
* sizeof(*b
));
941 ret
= crtc
->funcs
->gamma_set(crtc
, r
, g
, b
,
942 crtc
->gamma_size
, NULL
);
946 drm_modeset_unlock_all(fb_helper
->dev
);
951 static struct drm_property_blob
*setcmap_new_gamma_lut(struct drm_crtc
*crtc
,
952 struct fb_cmap
*cmap
)
954 struct drm_device
*dev
= crtc
->dev
;
955 struct drm_property_blob
*gamma_lut
;
956 struct drm_color_lut
*lut
;
957 int size
= crtc
->gamma_size
;
960 if (!size
|| cmap
->start
+ cmap
->len
> size
)
961 return ERR_PTR(-EINVAL
);
963 gamma_lut
= drm_property_create_blob(dev
, sizeof(*lut
) * size
, NULL
);
964 if (IS_ERR(gamma_lut
))
967 lut
= gamma_lut
->data
;
968 if (cmap
->start
|| cmap
->len
!= size
) {
969 u16
*r
= crtc
->gamma_store
;
970 u16
*g
= r
+ crtc
->gamma_size
;
971 u16
*b
= g
+ crtc
->gamma_size
;
973 for (i
= 0; i
< cmap
->start
; i
++) {
978 for (i
= cmap
->start
+ cmap
->len
; i
< size
; i
++) {
985 for (i
= 0; i
< cmap
->len
; i
++) {
986 lut
[cmap
->start
+ i
].red
= cmap
->red
[i
];
987 lut
[cmap
->start
+ i
].green
= cmap
->green
[i
];
988 lut
[cmap
->start
+ i
].blue
= cmap
->blue
[i
];
994 static int setcmap_atomic(struct fb_cmap
*cmap
, struct fb_info
*info
)
996 struct drm_fb_helper
*fb_helper
= info
->par
;
997 struct drm_device
*dev
= fb_helper
->dev
;
998 struct drm_property_blob
*gamma_lut
= NULL
;
999 struct drm_modeset_acquire_ctx ctx
;
1000 struct drm_crtc_state
*crtc_state
;
1001 struct drm_atomic_state
*state
;
1002 struct drm_mode_set
*modeset
;
1003 struct drm_crtc
*crtc
;
1008 drm_modeset_acquire_init(&ctx
, 0);
1010 state
= drm_atomic_state_alloc(dev
);
1016 state
->acquire_ctx
= &ctx
;
1018 drm_client_for_each_modeset(modeset
, &fb_helper
->client
) {
1019 crtc
= modeset
->crtc
;
1022 gamma_lut
= setcmap_new_gamma_lut(crtc
, cmap
);
1023 if (IS_ERR(gamma_lut
)) {
1024 ret
= PTR_ERR(gamma_lut
);
1029 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
1030 if (IS_ERR(crtc_state
)) {
1031 ret
= PTR_ERR(crtc_state
);
1035 replaced
= drm_property_replace_blob(&crtc_state
->degamma_lut
,
1037 replaced
|= drm_property_replace_blob(&crtc_state
->ctm
, NULL
);
1038 replaced
|= drm_property_replace_blob(&crtc_state
->gamma_lut
,
1040 crtc_state
->color_mgmt_changed
|= replaced
;
1043 ret
= drm_atomic_commit(state
);
1047 drm_client_for_each_modeset(modeset
, &fb_helper
->client
) {
1048 crtc
= modeset
->crtc
;
1050 r
= crtc
->gamma_store
;
1051 g
= r
+ crtc
->gamma_size
;
1052 b
= g
+ crtc
->gamma_size
;
1054 memcpy(r
+ cmap
->start
, cmap
->red
, cmap
->len
* sizeof(*r
));
1055 memcpy(g
+ cmap
->start
, cmap
->green
, cmap
->len
* sizeof(*g
));
1056 memcpy(b
+ cmap
->start
, cmap
->blue
, cmap
->len
* sizeof(*b
));
1060 if (ret
== -EDEADLK
)
1063 drm_property_blob_put(gamma_lut
);
1064 drm_atomic_state_put(state
);
1066 drm_modeset_drop_locks(&ctx
);
1067 drm_modeset_acquire_fini(&ctx
);
1072 drm_atomic_state_clear(state
);
1073 drm_modeset_backoff(&ctx
);
1078 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
1079 * @cmap: cmap to set
1080 * @info: fbdev registered by the helper
1082 int drm_fb_helper_setcmap(struct fb_cmap
*cmap
, struct fb_info
*info
)
1084 struct drm_fb_helper
*fb_helper
= info
->par
;
1085 struct drm_device
*dev
= fb_helper
->dev
;
1088 if (oops_in_progress
)
1091 mutex_lock(&fb_helper
->lock
);
1093 if (!drm_master_internal_acquire(dev
)) {
1098 mutex_lock(&fb_helper
->client
.modeset_mutex
);
1099 if (info
->fix
.visual
== FB_VISUAL_TRUECOLOR
)
1100 ret
= setcmap_pseudo_palette(cmap
, info
);
1101 else if (drm_drv_uses_atomic_modeset(fb_helper
->dev
))
1102 ret
= setcmap_atomic(cmap
, info
);
1104 ret
= setcmap_legacy(cmap
, info
);
1105 mutex_unlock(&fb_helper
->client
.modeset_mutex
);
1107 drm_master_internal_release(dev
);
1109 mutex_unlock(&fb_helper
->lock
);
1113 EXPORT_SYMBOL(drm_fb_helper_setcmap
);
1116 * drm_fb_helper_ioctl - legacy ioctl implementation
1117 * @info: fbdev registered by the helper
1118 * @cmd: ioctl command
1119 * @arg: ioctl argument
1121 * A helper to implement the standard fbdev ioctl. Only
1122 * FBIO_WAITFORVSYNC is implemented for now.
1124 int drm_fb_helper_ioctl(struct fb_info
*info
, unsigned int cmd
,
1127 struct drm_fb_helper
*fb_helper
= info
->par
;
1128 struct drm_device
*dev
= fb_helper
->dev
;
1129 struct drm_crtc
*crtc
;
1132 mutex_lock(&fb_helper
->lock
);
1133 if (!drm_master_internal_acquire(dev
)) {
1139 case FBIO_WAITFORVSYNC
:
1141 * Only consider the first CRTC.
1143 * This ioctl is supposed to take the CRTC number as
1144 * an argument, but in fbdev times, what that number
1145 * was supposed to be was quite unclear, different
1146 * drivers were passing that argument differently
1147 * (some by reference, some by value), and most of the
1148 * userspace applications were just hardcoding 0 as an
1151 * The first CRTC should be the integrated panel on
1152 * most drivers, so this is the best choice we can
1153 * make. If we're not smart enough here, one should
1154 * just consider switch the userspace to KMS.
1156 crtc
= fb_helper
->client
.modesets
[0].crtc
;
1159 * Only wait for a vblank event if the CRTC is
1160 * enabled, otherwise just don't do anythintg,
1161 * not even report an error.
1163 ret
= drm_crtc_vblank_get(crtc
);
1165 drm_crtc_wait_one_vblank(crtc
);
1166 drm_crtc_vblank_put(crtc
);
1175 drm_master_internal_release(dev
);
1177 mutex_unlock(&fb_helper
->lock
);
1180 EXPORT_SYMBOL(drm_fb_helper_ioctl
);
1182 static bool drm_fb_pixel_format_equal(const struct fb_var_screeninfo
*var_1
,
1183 const struct fb_var_screeninfo
*var_2
)
1185 return var_1
->bits_per_pixel
== var_2
->bits_per_pixel
&&
1186 var_1
->grayscale
== var_2
->grayscale
&&
1187 var_1
->red
.offset
== var_2
->red
.offset
&&
1188 var_1
->red
.length
== var_2
->red
.length
&&
1189 var_1
->red
.msb_right
== var_2
->red
.msb_right
&&
1190 var_1
->green
.offset
== var_2
->green
.offset
&&
1191 var_1
->green
.length
== var_2
->green
.length
&&
1192 var_1
->green
.msb_right
== var_2
->green
.msb_right
&&
1193 var_1
->blue
.offset
== var_2
->blue
.offset
&&
1194 var_1
->blue
.length
== var_2
->blue
.length
&&
1195 var_1
->blue
.msb_right
== var_2
->blue
.msb_right
&&
1196 var_1
->transp
.offset
== var_2
->transp
.offset
&&
1197 var_1
->transp
.length
== var_2
->transp
.length
&&
1198 var_1
->transp
.msb_right
== var_2
->transp
.msb_right
;
1201 static void drm_fb_helper_fill_pixel_fmt(struct fb_var_screeninfo
*var
,
1206 var
->red
.offset
= 0;
1207 var
->green
.offset
= 0;
1208 var
->blue
.offset
= 0;
1209 var
->red
.length
= 8; /* 8bit DAC */
1210 var
->green
.length
= 8;
1211 var
->blue
.length
= 8;
1212 var
->transp
.offset
= 0;
1213 var
->transp
.length
= 0;
1216 var
->red
.offset
= 10;
1217 var
->green
.offset
= 5;
1218 var
->blue
.offset
= 0;
1219 var
->red
.length
= 5;
1220 var
->green
.length
= 5;
1221 var
->blue
.length
= 5;
1222 var
->transp
.offset
= 15;
1223 var
->transp
.length
= 1;
1226 var
->red
.offset
= 11;
1227 var
->green
.offset
= 5;
1228 var
->blue
.offset
= 0;
1229 var
->red
.length
= 5;
1230 var
->green
.length
= 6;
1231 var
->blue
.length
= 5;
1232 var
->transp
.offset
= 0;
1235 var
->red
.offset
= 16;
1236 var
->green
.offset
= 8;
1237 var
->blue
.offset
= 0;
1238 var
->red
.length
= 8;
1239 var
->green
.length
= 8;
1240 var
->blue
.length
= 8;
1241 var
->transp
.offset
= 0;
1242 var
->transp
.length
= 0;
1245 var
->red
.offset
= 16;
1246 var
->green
.offset
= 8;
1247 var
->blue
.offset
= 0;
1248 var
->red
.length
= 8;
1249 var
->green
.length
= 8;
1250 var
->blue
.length
= 8;
1251 var
->transp
.offset
= 24;
1252 var
->transp
.length
= 8;
1260 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
1261 * @var: screeninfo to check
1262 * @info: fbdev registered by the helper
1264 int drm_fb_helper_check_var(struct fb_var_screeninfo
*var
,
1265 struct fb_info
*info
)
1267 struct drm_fb_helper
*fb_helper
= info
->par
;
1268 struct drm_framebuffer
*fb
= fb_helper
->fb
;
1270 if (in_dbg_master())
1273 if (var
->pixclock
!= 0) {
1274 DRM_DEBUG("fbdev emulation doesn't support changing the pixel clock, value of pixclock is ignored\n");
1278 if ((drm_format_info_block_width(fb
->format
, 0) > 1) ||
1279 (drm_format_info_block_height(fb
->format
, 0) > 1))
1283 * Changes struct fb_var_screeninfo are currently not pushed back
1284 * to KMS, hence fail if different settings are requested.
1286 if (var
->bits_per_pixel
!= fb
->format
->cpp
[0] * 8 ||
1287 var
->xres
> fb
->width
|| var
->yres
> fb
->height
||
1288 var
->xres_virtual
> fb
->width
|| var
->yres_virtual
> fb
->height
) {
1289 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
1290 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1291 var
->xres
, var
->yres
, var
->bits_per_pixel
,
1292 var
->xres_virtual
, var
->yres_virtual
,
1293 fb
->width
, fb
->height
, fb
->format
->cpp
[0] * 8);
1298 * Workaround for SDL 1.2, which is known to be setting all pixel format
1299 * fields values to zero in some cases. We treat this situation as a
1300 * kind of "use some reasonable autodetected values".
1302 if (!var
->red
.offset
&& !var
->green
.offset
&&
1303 !var
->blue
.offset
&& !var
->transp
.offset
&&
1304 !var
->red
.length
&& !var
->green
.length
&&
1305 !var
->blue
.length
&& !var
->transp
.length
&&
1306 !var
->red
.msb_right
&& !var
->green
.msb_right
&&
1307 !var
->blue
.msb_right
&& !var
->transp
.msb_right
) {
1308 drm_fb_helper_fill_pixel_fmt(var
, fb
->format
->depth
);
1312 * drm fbdev emulation doesn't support changing the pixel format at all,
1313 * so reject all pixel format changing requests.
1315 if (!drm_fb_pixel_format_equal(var
, &info
->var
)) {
1316 DRM_DEBUG("fbdev emulation doesn't support changing the pixel format\n");
1322 EXPORT_SYMBOL(drm_fb_helper_check_var
);
1325 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
1326 * @info: fbdev registered by the helper
1328 * This will let fbcon do the mode init and is called at initialization time by
1329 * the fbdev core when registering the driver, and later on through the hotplug
1332 int drm_fb_helper_set_par(struct fb_info
*info
)
1334 struct drm_fb_helper
*fb_helper
= info
->par
;
1335 struct fb_var_screeninfo
*var
= &info
->var
;
1337 if (oops_in_progress
)
1340 if (var
->pixclock
!= 0) {
1341 DRM_ERROR("PIXEL CLOCK SET\n");
1345 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper
);
1349 EXPORT_SYMBOL(drm_fb_helper_set_par
);
1351 static void pan_set(struct drm_fb_helper
*fb_helper
, int x
, int y
)
1353 struct drm_mode_set
*mode_set
;
1355 mutex_lock(&fb_helper
->client
.modeset_mutex
);
1356 drm_client_for_each_modeset(mode_set
, &fb_helper
->client
) {
1360 mutex_unlock(&fb_helper
->client
.modeset_mutex
);
1363 static int pan_display_atomic(struct fb_var_screeninfo
*var
,
1364 struct fb_info
*info
)
1366 struct drm_fb_helper
*fb_helper
= info
->par
;
1369 pan_set(fb_helper
, var
->xoffset
, var
->yoffset
);
1371 ret
= drm_client_modeset_commit_force(&fb_helper
->client
);
1373 info
->var
.xoffset
= var
->xoffset
;
1374 info
->var
.yoffset
= var
->yoffset
;
1376 pan_set(fb_helper
, info
->var
.xoffset
, info
->var
.yoffset
);
1381 static int pan_display_legacy(struct fb_var_screeninfo
*var
,
1382 struct fb_info
*info
)
1384 struct drm_fb_helper
*fb_helper
= info
->par
;
1385 struct drm_client_dev
*client
= &fb_helper
->client
;
1386 struct drm_mode_set
*modeset
;
1389 mutex_lock(&client
->modeset_mutex
);
1390 drm_modeset_lock_all(fb_helper
->dev
);
1391 drm_client_for_each_modeset(modeset
, client
) {
1392 modeset
->x
= var
->xoffset
;
1393 modeset
->y
= var
->yoffset
;
1395 if (modeset
->num_connectors
) {
1396 ret
= drm_mode_set_config_internal(modeset
);
1398 info
->var
.xoffset
= var
->xoffset
;
1399 info
->var
.yoffset
= var
->yoffset
;
1403 drm_modeset_unlock_all(fb_helper
->dev
);
1404 mutex_unlock(&client
->modeset_mutex
);
1410 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1411 * @var: updated screen information
1412 * @info: fbdev registered by the helper
1414 int drm_fb_helper_pan_display(struct fb_var_screeninfo
*var
,
1415 struct fb_info
*info
)
1417 struct drm_fb_helper
*fb_helper
= info
->par
;
1418 struct drm_device
*dev
= fb_helper
->dev
;
1421 if (oops_in_progress
)
1424 mutex_lock(&fb_helper
->lock
);
1425 if (!drm_master_internal_acquire(dev
)) {
1430 if (drm_drv_uses_atomic_modeset(dev
))
1431 ret
= pan_display_atomic(var
, info
);
1433 ret
= pan_display_legacy(var
, info
);
1435 drm_master_internal_release(dev
);
1437 mutex_unlock(&fb_helper
->lock
);
1441 EXPORT_SYMBOL(drm_fb_helper_pan_display
);
1444 * Allocates the backing storage and sets up the fbdev info structure through
1445 * the ->fb_probe callback.
1447 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper
*fb_helper
,
1450 struct drm_client_dev
*client
= &fb_helper
->client
;
1453 struct drm_connector_list_iter conn_iter
;
1454 struct drm_fb_helper_surface_size sizes
;
1455 struct drm_connector
*connector
;
1456 struct drm_mode_set
*mode_set
;
1459 memset(&sizes
, 0, sizeof(struct drm_fb_helper_surface_size
));
1460 sizes
.surface_depth
= 24;
1461 sizes
.surface_bpp
= 32;
1462 sizes
.fb_width
= (u32
)-1;
1463 sizes
.fb_height
= (u32
)-1;
1466 * If driver picks 8 or 16 by default use that for both depth/bpp
1469 if (preferred_bpp
!= sizes
.surface_bpp
)
1470 sizes
.surface_depth
= sizes
.surface_bpp
= preferred_bpp
;
1472 drm_connector_list_iter_begin(fb_helper
->dev
, &conn_iter
);
1473 drm_client_for_each_connector_iter(connector
, &conn_iter
) {
1474 struct drm_cmdline_mode
*cmdline_mode
;
1476 cmdline_mode
= &connector
->cmdline_mode
;
1478 if (cmdline_mode
->bpp_specified
) {
1479 switch (cmdline_mode
->bpp
) {
1481 sizes
.surface_depth
= sizes
.surface_bpp
= 8;
1484 sizes
.surface_depth
= 15;
1485 sizes
.surface_bpp
= 16;
1488 sizes
.surface_depth
= sizes
.surface_bpp
= 16;
1491 sizes
.surface_depth
= sizes
.surface_bpp
= 24;
1494 sizes
.surface_depth
= 24;
1495 sizes
.surface_bpp
= 32;
1501 drm_connector_list_iter_end(&conn_iter
);
1504 * If we run into a situation where, for example, the primary plane
1505 * supports RGBA5551 (16 bpp, depth 15) but not RGB565 (16 bpp, depth
1506 * 16) we need to scale down the depth of the sizes we request.
1508 mutex_lock(&client
->modeset_mutex
);
1509 drm_client_for_each_modeset(mode_set
, client
) {
1510 struct drm_crtc
*crtc
= mode_set
->crtc
;
1511 struct drm_plane
*plane
= crtc
->primary
;
1514 DRM_DEBUG("test CRTC %u primary plane\n", drm_crtc_index(crtc
));
1516 for (j
= 0; j
< plane
->format_count
; j
++) {
1517 const struct drm_format_info
*fmt
;
1519 fmt
= drm_format_info(plane
->format_types
[j
]);
1522 * Do not consider YUV or other complicated formats
1523 * for framebuffers. This means only legacy formats
1524 * are supported (fmt->depth is a legacy field) but
1525 * the framebuffer emulation can only deal with such
1526 * formats, specifically RGB/BGA formats.
1528 if (fmt
->depth
== 0)
1531 /* We found a perfect fit, great */
1532 if (fmt
->depth
== sizes
.surface_depth
) {
1533 best_depth
= fmt
->depth
;
1537 /* Skip depths above what we're looking for */
1538 if (fmt
->depth
> sizes
.surface_depth
)
1541 /* Best depth found so far */
1542 if (fmt
->depth
> best_depth
)
1543 best_depth
= fmt
->depth
;
1546 if (sizes
.surface_depth
!= best_depth
&& best_depth
) {
1547 DRM_INFO("requested bpp %d, scaled depth down to %d",
1548 sizes
.surface_bpp
, best_depth
);
1549 sizes
.surface_depth
= best_depth
;
1552 /* first up get a count of crtcs now in use and new min/maxes width/heights */
1554 drm_client_for_each_modeset(mode_set
, client
) {
1555 struct drm_display_mode
*desired_mode
;
1557 /* in case of tile group, are we the last tile vert or horiz?
1558 * If no tile group you are always the last one both vertically
1561 bool lastv
= true, lasth
= true;
1563 desired_mode
= mode_set
->mode
;
1573 sizes
.surface_width
= max_t(u32
, desired_mode
->hdisplay
+ x
, sizes
.surface_width
);
1574 sizes
.surface_height
= max_t(u32
, desired_mode
->vdisplay
+ y
, sizes
.surface_height
);
1576 for (j
= 0; j
< mode_set
->num_connectors
; j
++) {
1577 struct drm_connector
*connector
= mode_set
->connectors
[j
];
1579 if (connector
->has_tile
) {
1580 lasth
= (connector
->tile_h_loc
== (connector
->num_h_tile
- 1));
1581 lastv
= (connector
->tile_v_loc
== (connector
->num_v_tile
- 1));
1582 /* cloning to multiple tiles is just crazy-talk, so: */
1588 sizes
.fb_width
= min_t(u32
, desired_mode
->hdisplay
+ x
, sizes
.fb_width
);
1590 sizes
.fb_height
= min_t(u32
, desired_mode
->vdisplay
+ y
, sizes
.fb_height
);
1592 mutex_unlock(&client
->modeset_mutex
);
1594 if (crtc_count
== 0 || sizes
.fb_width
== -1 || sizes
.fb_height
== -1) {
1595 DRM_INFO("Cannot find any crtc or sizes\n");
1597 /* First time: disable all crtc's.. */
1598 if (!fb_helper
->deferred_setup
)
1599 drm_client_modeset_commit(client
);
1603 /* Handle our overallocation */
1604 sizes
.surface_height
*= drm_fbdev_overalloc
;
1605 sizes
.surface_height
/= 100;
1607 /* push down into drivers */
1608 ret
= (*fb_helper
->funcs
->fb_probe
)(fb_helper
, &sizes
);
1612 strcpy(fb_helper
->fb
->comm
, "[fbcon]");
1616 static void drm_fb_helper_fill_fix(struct fb_info
*info
, uint32_t pitch
,
1619 info
->fix
.type
= FB_TYPE_PACKED_PIXELS
;
1620 info
->fix
.visual
= depth
== 8 ? FB_VISUAL_PSEUDOCOLOR
:
1621 FB_VISUAL_TRUECOLOR
;
1622 info
->fix
.mmio_start
= 0;
1623 info
->fix
.mmio_len
= 0;
1624 info
->fix
.type_aux
= 0;
1625 info
->fix
.xpanstep
= 1; /* doing it in hw */
1626 info
->fix
.ypanstep
= 1; /* doing it in hw */
1627 info
->fix
.ywrapstep
= 0;
1628 info
->fix
.accel
= FB_ACCEL_NONE
;
1630 info
->fix
.line_length
= pitch
;
1633 static void drm_fb_helper_fill_var(struct fb_info
*info
,
1634 struct drm_fb_helper
*fb_helper
,
1635 uint32_t fb_width
, uint32_t fb_height
)
1637 struct drm_framebuffer
*fb
= fb_helper
->fb
;
1639 WARN_ON((drm_format_info_block_width(fb
->format
, 0) > 1) ||
1640 (drm_format_info_block_height(fb
->format
, 0) > 1));
1641 info
->pseudo_palette
= fb_helper
->pseudo_palette
;
1642 info
->var
.xres_virtual
= fb
->width
;
1643 info
->var
.yres_virtual
= fb
->height
;
1644 info
->var
.bits_per_pixel
= fb
->format
->cpp
[0] * 8;
1645 info
->var
.accel_flags
= FB_ACCELF_TEXT
;
1646 info
->var
.xoffset
= 0;
1647 info
->var
.yoffset
= 0;
1648 info
->var
.activate
= FB_ACTIVATE_NOW
;
1650 drm_fb_helper_fill_pixel_fmt(&info
->var
, fb
->format
->depth
);
1652 info
->var
.xres
= fb_width
;
1653 info
->var
.yres
= fb_height
;
1657 * drm_fb_helper_fill_info - initializes fbdev information
1658 * @info: fbdev instance to set up
1659 * @fb_helper: fb helper instance to use as template
1660 * @sizes: describes fbdev size and scanout surface size
1662 * Sets up the variable and fixed fbdev metainformation from the given fb helper
1663 * instance and the drm framebuffer allocated in &drm_fb_helper.fb.
1665 * Drivers should call this (or their equivalent setup code) from their
1666 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
1667 * backing storage framebuffer.
1669 void drm_fb_helper_fill_info(struct fb_info
*info
,
1670 struct drm_fb_helper
*fb_helper
,
1671 struct drm_fb_helper_surface_size
*sizes
)
1673 struct drm_framebuffer
*fb
= fb_helper
->fb
;
1675 drm_fb_helper_fill_fix(info
, fb
->pitches
[0], fb
->format
->depth
);
1676 drm_fb_helper_fill_var(info
, fb_helper
,
1677 sizes
->fb_width
, sizes
->fb_height
);
1679 info
->par
= fb_helper
;
1680 snprintf(info
->fix
.id
, sizeof(info
->fix
.id
), "%sdrmfb",
1681 fb_helper
->dev
->driver
->name
);
1684 EXPORT_SYMBOL(drm_fb_helper_fill_info
);
1687 * This is a continuation of drm_setup_crtcs() that sets up anything related
1688 * to the framebuffer. During initialization, drm_setup_crtcs() is called before
1689 * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev).
1690 * So, any setup that touches those fields needs to be done here instead of in
1691 * drm_setup_crtcs().
1693 static void drm_setup_crtcs_fb(struct drm_fb_helper
*fb_helper
)
1695 struct drm_client_dev
*client
= &fb_helper
->client
;
1696 struct drm_connector_list_iter conn_iter
;
1697 struct fb_info
*info
= fb_helper
->fbdev
;
1698 unsigned int rotation
, sw_rotations
= 0;
1699 struct drm_connector
*connector
;
1700 struct drm_mode_set
*modeset
;
1702 mutex_lock(&client
->modeset_mutex
);
1703 drm_client_for_each_modeset(modeset
, client
) {
1704 if (!modeset
->num_connectors
)
1707 modeset
->fb
= fb_helper
->fb
;
1709 if (drm_client_rotation(modeset
, &rotation
))
1710 /* Rotating in hardware, fbcon should not rotate */
1711 sw_rotations
|= DRM_MODE_ROTATE_0
;
1713 sw_rotations
|= rotation
;
1715 mutex_unlock(&client
->modeset_mutex
);
1717 drm_connector_list_iter_begin(fb_helper
->dev
, &conn_iter
);
1718 drm_client_for_each_connector_iter(connector
, &conn_iter
) {
1720 /* use first connected connector for the physical dimensions */
1721 if (connector
->status
== connector_status_connected
) {
1722 info
->var
.width
= connector
->display_info
.width_mm
;
1723 info
->var
.height
= connector
->display_info
.height_mm
;
1727 drm_connector_list_iter_end(&conn_iter
);
1729 switch (sw_rotations
) {
1730 case DRM_MODE_ROTATE_0
:
1731 info
->fbcon_rotate_hint
= FB_ROTATE_UR
;
1733 case DRM_MODE_ROTATE_90
:
1734 info
->fbcon_rotate_hint
= FB_ROTATE_CCW
;
1736 case DRM_MODE_ROTATE_180
:
1737 info
->fbcon_rotate_hint
= FB_ROTATE_UD
;
1739 case DRM_MODE_ROTATE_270
:
1740 info
->fbcon_rotate_hint
= FB_ROTATE_CW
;
1744 * Multiple bits are set / multiple rotations requested
1745 * fbcon cannot handle separate rotation settings per
1746 * output, so fallback to unrotated.
1748 info
->fbcon_rotate_hint
= FB_ROTATE_UR
;
1752 /* Note: Drops fb_helper->lock before returning. */
1754 __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper
*fb_helper
,
1757 struct drm_device
*dev
= fb_helper
->dev
;
1758 struct fb_info
*info
;
1759 unsigned int width
, height
;
1762 width
= dev
->mode_config
.max_width
;
1763 height
= dev
->mode_config
.max_height
;
1765 drm_client_modeset_probe(&fb_helper
->client
, width
, height
);
1766 ret
= drm_fb_helper_single_fb_probe(fb_helper
, bpp_sel
);
1768 if (ret
== -EAGAIN
) {
1769 fb_helper
->preferred_bpp
= bpp_sel
;
1770 fb_helper
->deferred_setup
= true;
1773 mutex_unlock(&fb_helper
->lock
);
1777 drm_setup_crtcs_fb(fb_helper
);
1779 fb_helper
->deferred_setup
= false;
1781 info
= fb_helper
->fbdev
;
1782 info
->var
.pixclock
= 0;
1783 /* Shamelessly allow physical address leaking to userspace */
1784 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
1785 if (!drm_leak_fbdev_smem
)
1787 /* don't leak any physical addresses to userspace */
1788 info
->flags
|= FBINFO_HIDE_SMEM_START
;
1790 /* Need to drop locks to avoid recursive deadlock in
1791 * register_framebuffer. This is ok because the only thing left to do is
1792 * register the fbdev emulation instance in kernel_fb_helper_list. */
1793 mutex_unlock(&fb_helper
->lock
);
1795 ret
= register_framebuffer(info
);
1799 dev_info(dev
->dev
, "fb%d: %s frame buffer device\n",
1800 info
->node
, info
->fix
.id
);
1802 mutex_lock(&kernel_fb_helper_lock
);
1803 if (list_empty(&kernel_fb_helper_list
))
1804 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op
);
1806 list_add(&fb_helper
->kernel_fb_list
, &kernel_fb_helper_list
);
1807 mutex_unlock(&kernel_fb_helper_lock
);
1813 * drm_fb_helper_initial_config - setup a sane initial connector configuration
1814 * @fb_helper: fb_helper device struct
1815 * @bpp_sel: bpp value to use for the framebuffer configuration
1817 * Scans the CRTCs and connectors and tries to put together an initial setup.
1818 * At the moment, this is a cloned configuration across all heads with
1819 * a new framebuffer object as the backing store.
1821 * Note that this also registers the fbdev and so allows userspace to call into
1822 * the driver through the fbdev interfaces.
1824 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
1825 * to let the driver allocate and initialize the fbdev info structure and the
1826 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_info() is provided
1827 * as a helper to setup simple default values for the fbdev info structure.
1831 * When you have fbcon support built-in or already loaded, this function will do
1832 * a full modeset to setup the fbdev console. Due to locking misdesign in the
1833 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
1834 * console_lock. Until console_unlock is called no dmesg lines will be sent out
1835 * to consoles, not even serial console. This means when your driver crashes,
1836 * you will see absolutely nothing else but a system stuck in this function,
1837 * with no further output. Any kind of printk() you place within your own driver
1838 * or in the drm core modeset code will also never show up.
1840 * Standard debug practice is to run the fbcon setup without taking the
1841 * console_lock as a hack, to be able to see backtraces and crashes on the
1842 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
1845 * The other option is to just disable fbdev emulation since very likely the
1846 * first modeset from userspace will crash in the same way, and is even easier
1847 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
1848 * kernel cmdline option.
1851 * Zero if everything went ok, nonzero otherwise.
1853 int drm_fb_helper_initial_config(struct drm_fb_helper
*fb_helper
, int bpp_sel
)
1857 if (!drm_fbdev_emulation
)
1860 mutex_lock(&fb_helper
->lock
);
1861 ret
= __drm_fb_helper_initial_config_and_unlock(fb_helper
, bpp_sel
);
1865 EXPORT_SYMBOL(drm_fb_helper_initial_config
);
1868 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1869 * probing all the outputs attached to the fb
1870 * @fb_helper: driver-allocated fbdev helper, can be NULL
1872 * Scan the connectors attached to the fb_helper and try to put together a
1873 * setup after notification of a change in output configuration.
1875 * Called at runtime, takes the mode config locks to be able to check/change the
1876 * modeset configuration. Must be run from process context (which usually means
1877 * either the output polling work or a work item launched from the driver's
1878 * hotplug interrupt).
1880 * Note that drivers may call this even before calling
1881 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
1882 * for a race-free fbcon setup and will make sure that the fbdev emulation will
1883 * not miss any hotplug events.
1886 * 0 on success and a non-zero error code otherwise.
1888 int drm_fb_helper_hotplug_event(struct drm_fb_helper
*fb_helper
)
1892 if (!drm_fbdev_emulation
|| !fb_helper
)
1895 mutex_lock(&fb_helper
->lock
);
1896 if (fb_helper
->deferred_setup
) {
1897 err
= __drm_fb_helper_initial_config_and_unlock(fb_helper
,
1898 fb_helper
->preferred_bpp
);
1902 if (!fb_helper
->fb
|| !drm_master_internal_acquire(fb_helper
->dev
)) {
1903 fb_helper
->delayed_hotplug
= true;
1904 mutex_unlock(&fb_helper
->lock
);
1908 drm_master_internal_release(fb_helper
->dev
);
1910 DRM_DEBUG_KMS("\n");
1912 drm_client_modeset_probe(&fb_helper
->client
, fb_helper
->fb
->width
, fb_helper
->fb
->height
);
1913 drm_setup_crtcs_fb(fb_helper
);
1914 mutex_unlock(&fb_helper
->lock
);
1916 drm_fb_helper_set_par(fb_helper
->fbdev
);
1920 EXPORT_SYMBOL(drm_fb_helper_hotplug_event
);
1923 * drm_fb_helper_fbdev_setup() - Setup fbdev emulation
1925 * @fb_helper: fbdev helper structure to set up
1926 * @funcs: fbdev helper functions
1927 * @preferred_bpp: Preferred bits per pixel for the device.
1928 * @dev->mode_config.preferred_depth is used if this is zero.
1929 * @max_conn_count: Maximum number of connectors (not used)
1931 * This function sets up fbdev emulation and registers fbdev for access by
1932 * userspace. If all connectors are disconnected, setup is deferred to the next
1933 * time drm_fb_helper_hotplug_event() is called.
1934 * The caller must to provide a &drm_fb_helper_funcs->fb_probe callback
1937 * Use drm_fb_helper_fbdev_teardown() to destroy the fbdev.
1939 * See also: drm_fb_helper_initial_config(), drm_fbdev_generic_setup().
1942 * Zero on success or negative error code on failure.
1944 int drm_fb_helper_fbdev_setup(struct drm_device
*dev
,
1945 struct drm_fb_helper
*fb_helper
,
1946 const struct drm_fb_helper_funcs
*funcs
,
1947 unsigned int preferred_bpp
,
1948 unsigned int max_conn_count
)
1953 preferred_bpp
= dev
->mode_config
.preferred_depth
;
1957 drm_fb_helper_prepare(dev
, fb_helper
, funcs
);
1959 ret
= drm_fb_helper_init(dev
, fb_helper
, 0);
1961 DRM_DEV_ERROR(dev
->dev
, "fbdev: Failed to initialize (ret=%d)\n", ret
);
1965 if (!drm_drv_uses_atomic_modeset(dev
))
1966 drm_helper_disable_unused_functions(dev
);
1968 ret
= drm_fb_helper_initial_config(fb_helper
, preferred_bpp
);
1970 DRM_DEV_ERROR(dev
->dev
, "fbdev: Failed to set configuration (ret=%d)\n", ret
);
1971 goto err_drm_fb_helper_fini
;
1976 err_drm_fb_helper_fini
:
1977 drm_fb_helper_fbdev_teardown(dev
);
1981 EXPORT_SYMBOL(drm_fb_helper_fbdev_setup
);
1984 * drm_fb_helper_fbdev_teardown - Tear down fbdev emulation
1987 * This function unregisters fbdev if not already done and cleans up the
1988 * associated resources including the &drm_framebuffer.
1989 * The driver is responsible for freeing the &drm_fb_helper structure which is
1990 * stored in &drm_device->fb_helper. Do note that this pointer has been cleared
1991 * when this function returns.
1993 * In order to support device removal/unplug while file handles are still open,
1994 * drm_fb_helper_unregister_fbi() should be called on device removal and
1995 * drm_fb_helper_fbdev_teardown() in the &drm_driver->release callback when
1996 * file handles are closed.
1998 void drm_fb_helper_fbdev_teardown(struct drm_device
*dev
)
2000 struct drm_fb_helper
*fb_helper
= dev
->fb_helper
;
2001 struct fb_ops
*fbops
= NULL
;
2006 /* Unregister if it hasn't been done already */
2007 if (fb_helper
->fbdev
&& fb_helper
->fbdev
->dev
)
2008 drm_fb_helper_unregister_fbi(fb_helper
);
2010 if (fb_helper
->fbdev
&& fb_helper
->fbdev
->fbdefio
) {
2011 fb_deferred_io_cleanup(fb_helper
->fbdev
);
2012 kfree(fb_helper
->fbdev
->fbdefio
);
2013 fbops
= fb_helper
->fbdev
->fbops
;
2016 drm_fb_helper_fini(fb_helper
);
2020 drm_framebuffer_remove(fb_helper
->fb
);
2022 EXPORT_SYMBOL(drm_fb_helper_fbdev_teardown
);
2025 * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation
2028 * This function can be used as the &drm_driver->lastclose callback for drivers
2029 * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked().
2031 void drm_fb_helper_lastclose(struct drm_device
*dev
)
2033 drm_fb_helper_restore_fbdev_mode_unlocked(dev
->fb_helper
);
2035 EXPORT_SYMBOL(drm_fb_helper_lastclose
);
2038 * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed
2039 * helper for fbdev emulation
2042 * This function can be used as the
2043 * &drm_mode_config_funcs.output_poll_changed callback for drivers that only
2044 * need to call drm_fb_helper_hotplug_event().
2046 void drm_fb_helper_output_poll_changed(struct drm_device
*dev
)
2048 drm_fb_helper_hotplug_event(dev
->fb_helper
);
2050 EXPORT_SYMBOL(drm_fb_helper_output_poll_changed
);
2052 /* @user: 1=userspace, 0=fbcon */
2053 static int drm_fbdev_fb_open(struct fb_info
*info
, int user
)
2055 struct drm_fb_helper
*fb_helper
= info
->par
;
2057 /* No need to take a ref for fbcon because it unbinds on unregister */
2058 if (user
&& !try_module_get(fb_helper
->dev
->driver
->fops
->owner
))
2064 static int drm_fbdev_fb_release(struct fb_info
*info
, int user
)
2066 struct drm_fb_helper
*fb_helper
= info
->par
;
2069 module_put(fb_helper
->dev
->driver
->fops
->owner
);
2074 static void drm_fbdev_cleanup(struct drm_fb_helper
*fb_helper
)
2076 struct fb_info
*fbi
= fb_helper
->fbdev
;
2077 struct fb_ops
*fbops
= NULL
;
2078 void *shadow
= NULL
;
2080 if (!fb_helper
->dev
)
2083 if (fbi
&& fbi
->fbdefio
) {
2084 fb_deferred_io_cleanup(fbi
);
2085 shadow
= fbi
->screen_buffer
;
2089 drm_fb_helper_fini(fb_helper
);
2096 drm_client_framebuffer_delete(fb_helper
->buffer
);
2099 static void drm_fbdev_release(struct drm_fb_helper
*fb_helper
)
2101 drm_fbdev_cleanup(fb_helper
);
2102 drm_client_release(&fb_helper
->client
);
2107 * fb_ops.fb_destroy is called by the last put_fb_info() call at the end of
2108 * unregister_framebuffer() or fb_release().
2110 static void drm_fbdev_fb_destroy(struct fb_info
*info
)
2112 drm_fbdev_release(info
->par
);
2115 static int drm_fbdev_fb_mmap(struct fb_info
*info
, struct vm_area_struct
*vma
)
2117 struct drm_fb_helper
*fb_helper
= info
->par
;
2119 if (fb_helper
->dev
->driver
->gem_prime_mmap
)
2120 return fb_helper
->dev
->driver
->gem_prime_mmap(fb_helper
->buffer
->gem
, vma
);
2125 static struct fb_ops drm_fbdev_fb_ops
= {
2126 .owner
= THIS_MODULE
,
2127 DRM_FB_HELPER_DEFAULT_OPS
,
2128 .fb_open
= drm_fbdev_fb_open
,
2129 .fb_release
= drm_fbdev_fb_release
,
2130 .fb_destroy
= drm_fbdev_fb_destroy
,
2131 .fb_mmap
= drm_fbdev_fb_mmap
,
2132 .fb_read
= drm_fb_helper_sys_read
,
2133 .fb_write
= drm_fb_helper_sys_write
,
2134 .fb_fillrect
= drm_fb_helper_sys_fillrect
,
2135 .fb_copyarea
= drm_fb_helper_sys_copyarea
,
2136 .fb_imageblit
= drm_fb_helper_sys_imageblit
,
2139 static struct fb_deferred_io drm_fbdev_defio
= {
2141 .deferred_io
= drm_fb_helper_deferred_io
,
2145 * drm_fb_helper_generic_probe - Generic fbdev emulation probe helper
2146 * @fb_helper: fbdev helper structure
2147 * @sizes: describes fbdev size and scanout surface size
2149 * This function uses the client API to create a framebuffer backed by a dumb buffer.
2151 * The _sys_ versions are used for &fb_ops.fb_read, fb_write, fb_fillrect,
2152 * fb_copyarea, fb_imageblit.
2155 * Zero on success or negative error code on failure.
2157 int drm_fb_helper_generic_probe(struct drm_fb_helper
*fb_helper
,
2158 struct drm_fb_helper_surface_size
*sizes
)
2160 struct drm_client_dev
*client
= &fb_helper
->client
;
2161 struct drm_client_buffer
*buffer
;
2162 struct drm_framebuffer
*fb
;
2163 struct fb_info
*fbi
;
2167 DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n",
2168 sizes
->surface_width
, sizes
->surface_height
,
2169 sizes
->surface_bpp
);
2171 format
= drm_mode_legacy_fb_format(sizes
->surface_bpp
, sizes
->surface_depth
);
2172 buffer
= drm_client_framebuffer_create(client
, sizes
->surface_width
,
2173 sizes
->surface_height
, format
);
2175 return PTR_ERR(buffer
);
2177 fb_helper
->buffer
= buffer
;
2178 fb_helper
->fb
= buffer
->fb
;
2181 fbi
= drm_fb_helper_alloc_fbi(fb_helper
);
2183 return PTR_ERR(fbi
);
2185 fbi
->fbops
= &drm_fbdev_fb_ops
;
2186 fbi
->screen_size
= fb
->height
* fb
->pitches
[0];
2187 fbi
->fix
.smem_len
= fbi
->screen_size
;
2189 drm_fb_helper_fill_info(fbi
, fb_helper
, sizes
);
2191 if (drm_fbdev_use_shadow_fb(fb_helper
)) {
2192 struct fb_ops
*fbops
;
2196 * fb_deferred_io_cleanup() clears &fbops->fb_mmap so a per
2197 * instance version is necessary.
2199 fbops
= kzalloc(sizeof(*fbops
), GFP_KERNEL
);
2200 shadow
= vzalloc(fbi
->screen_size
);
2201 if (!fbops
|| !shadow
) {
2207 *fbops
= *fbi
->fbops
;
2209 fbi
->screen_buffer
= shadow
;
2210 fbi
->fbdefio
= &drm_fbdev_defio
;
2212 fb_deferred_io_init(fbi
);
2214 /* buffer is mapped for HW framebuffer */
2215 vaddr
= drm_client_buffer_vmap(fb_helper
->buffer
);
2217 return PTR_ERR(vaddr
);
2219 fbi
->screen_buffer
= vaddr
;
2220 /* Shamelessly leak the physical address to user-space */
2221 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
2222 if (drm_leak_fbdev_smem
&& fbi
->fix
.smem_start
== 0)
2223 fbi
->fix
.smem_start
=
2224 page_to_phys(virt_to_page(fbi
->screen_buffer
));
2230 EXPORT_SYMBOL(drm_fb_helper_generic_probe
);
2232 static const struct drm_fb_helper_funcs drm_fb_helper_generic_funcs
= {
2233 .fb_probe
= drm_fb_helper_generic_probe
,
2236 static void drm_fbdev_client_unregister(struct drm_client_dev
*client
)
2238 struct drm_fb_helper
*fb_helper
= drm_fb_helper_from_client(client
);
2240 if (fb_helper
->fbdev
)
2241 /* drm_fbdev_fb_destroy() takes care of cleanup */
2242 drm_fb_helper_unregister_fbi(fb_helper
);
2244 drm_fbdev_release(fb_helper
);
2247 static int drm_fbdev_client_restore(struct drm_client_dev
*client
)
2249 drm_fb_helper_lastclose(client
->dev
);
2254 static int drm_fbdev_client_hotplug(struct drm_client_dev
*client
)
2256 struct drm_fb_helper
*fb_helper
= drm_fb_helper_from_client(client
);
2257 struct drm_device
*dev
= client
->dev
;
2260 /* Setup is not retried if it has failed */
2261 if (!fb_helper
->dev
&& fb_helper
->funcs
)
2265 return drm_fb_helper_hotplug_event(dev
->fb_helper
);
2267 if (!dev
->mode_config
.num_connector
) {
2268 DRM_DEV_DEBUG(dev
->dev
, "No connectors found, will not create framebuffer!\n");
2272 drm_fb_helper_prepare(dev
, fb_helper
, &drm_fb_helper_generic_funcs
);
2274 ret
= drm_fb_helper_init(dev
, fb_helper
, 0);
2278 if (!drm_drv_uses_atomic_modeset(dev
))
2279 drm_helper_disable_unused_functions(dev
);
2281 ret
= drm_fb_helper_initial_config(fb_helper
, fb_helper
->preferred_bpp
);
2288 drm_fbdev_cleanup(fb_helper
);
2290 fb_helper
->dev
= NULL
;
2291 fb_helper
->fbdev
= NULL
;
2293 DRM_DEV_ERROR(dev
->dev
, "fbdev: Failed to setup generic emulation (ret=%d)\n", ret
);
2298 static const struct drm_client_funcs drm_fbdev_client_funcs
= {
2299 .owner
= THIS_MODULE
,
2300 .unregister
= drm_fbdev_client_unregister
,
2301 .restore
= drm_fbdev_client_restore
,
2302 .hotplug
= drm_fbdev_client_hotplug
,
2306 * drm_fbdev_generic_setup() - Setup generic fbdev emulation
2308 * @preferred_bpp: Preferred bits per pixel for the device.
2309 * @dev->mode_config.preferred_depth is used if this is zero.
2311 * This function sets up generic fbdev emulation for drivers that supports
2312 * dumb buffers with a virtual address and that can be mmap'ed. If the driver
2313 * does not support these functions, it could use drm_fb_helper_fbdev_setup().
2315 * Restore, hotplug events and teardown are all taken care of. Drivers that do
2316 * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves.
2317 * Simple drivers might use drm_mode_config_helper_suspend().
2319 * Drivers that set the dirty callback on their framebuffer will get a shadow
2320 * fbdev buffer that is blitted onto the real buffer. This is done in order to
2321 * make deferred I/O work with all kinds of buffers. A shadow buffer can be
2322 * requested explicitly by setting struct drm_mode_config.prefer_shadow or
2323 * struct drm_mode_config.prefer_shadow_fbdev to true beforehand. This is
2324 * required to use generic fbdev emulation with SHMEM helpers.
2326 * This function is safe to call even when there are no connectors present.
2327 * Setup will be retried on the next hotplug event.
2329 * The fbdev is destroyed by drm_dev_unregister().
2332 * Zero on success or negative error code on failure.
2334 int drm_fbdev_generic_setup(struct drm_device
*dev
, unsigned int preferred_bpp
)
2336 struct drm_fb_helper
*fb_helper
;
2339 WARN(dev
->fb_helper
, "fb_helper is already set!\n");
2341 if (!drm_fbdev_emulation
)
2344 fb_helper
= kzalloc(sizeof(*fb_helper
), GFP_KERNEL
);
2348 ret
= drm_client_init(dev
, &fb_helper
->client
, "fbdev", &drm_fbdev_client_funcs
);
2351 DRM_DEV_ERROR(dev
->dev
, "Failed to register client: %d\n", ret
);
2356 preferred_bpp
= dev
->mode_config
.preferred_depth
;
2359 fb_helper
->preferred_bpp
= preferred_bpp
;
2361 ret
= drm_fbdev_client_hotplug(&fb_helper
->client
);
2363 DRM_DEV_DEBUG(dev
->dev
, "client hotplug ret=%d\n", ret
);
2365 drm_client_register(&fb_helper
->client
);
2369 EXPORT_SYMBOL(drm_fbdev_generic_setup
);
2371 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
2372 * but the module doesn't depend on any fb console symbols. At least
2373 * attempt to load fbcon to avoid leaving the system without a usable console.
2375 int __init
drm_fb_helper_modinit(void)
2377 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
2378 const char name
[] = "fbcon";
2379 struct module
*fbcon
;
2381 mutex_lock(&module_mutex
);
2382 fbcon
= find_module(name
);
2383 mutex_unlock(&module_mutex
);
2386 request_module_nowait(name
);
2390 EXPORT_SYMBOL(drm_fb_helper_modinit
);