2 * Copyright (c) 2006-2009 Red Hat Inc.
3 * Copyright (c) 2006-2008 Intel Corporation
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
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 * Likewise, bits_per_pixel should be rounded up to a supported value.
1314 var->bits_per_pixel = fb->format->cpp[0] * 8;
1317 * drm fbdev emulation doesn't support changing the pixel format at all,
1318 * so reject all pixel format changing requests.
1320 if (!drm_fb_pixel_format_equal(var, &info->var)) {
1321 DRM_DEBUG("fbdev emulation doesn't support changing the pixel format\n");
1327 EXPORT_SYMBOL(drm_fb_helper_check_var);
1330 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
1331 * @info: fbdev registered by the helper
1333 * This will let fbcon do the mode init and is called at initialization time by
1334 * the fbdev core when registering the driver, and later on through the hotplug
1337 int drm_fb_helper_set_par(struct fb_info *info)
1339 struct drm_fb_helper *fb_helper = info->par;
1340 struct fb_var_screeninfo *var = &info->var;
1342 if (oops_in_progress)
1345 if (var->pixclock != 0) {
1346 DRM_ERROR("PIXEL CLOCK SET\n");
1350 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
1354 EXPORT_SYMBOL(drm_fb_helper_set_par);
1356 static void pan_set(struct drm_fb_helper *fb_helper, int x, int y)
1358 struct drm_mode_set *mode_set;
1360 mutex_lock(&fb_helper->client.modeset_mutex);
1361 drm_client_for_each_modeset(mode_set, &fb_helper->client) {
1365 mutex_unlock(&fb_helper->client.modeset_mutex);
1368 static int pan_display_atomic(struct fb_var_screeninfo *var,
1369 struct fb_info *info)
1371 struct drm_fb_helper *fb_helper = info->par;
1374 pan_set(fb_helper, var->xoffset, var->yoffset);
1376 ret = drm_client_modeset_commit_force(&fb_helper->client);
1378 info->var.xoffset = var->xoffset;
1379 info->var.yoffset = var->yoffset;
1381 pan_set(fb_helper, info->var.xoffset, info->var.yoffset);
1386 static int pan_display_legacy(struct fb_var_screeninfo *var,
1387 struct fb_info *info)
1389 struct drm_fb_helper *fb_helper = info->par;
1390 struct drm_client_dev *client = &fb_helper->client;
1391 struct drm_mode_set *modeset;
1394 mutex_lock(&client->modeset_mutex);
1395 drm_modeset_lock_all(fb_helper->dev);
1396 drm_client_for_each_modeset(modeset, client) {
1397 modeset->x = var->xoffset;
1398 modeset->y = var->yoffset;
1400 if (modeset->num_connectors) {
1401 ret = drm_mode_set_config_internal(modeset);
1403 info->var.xoffset = var->xoffset;
1404 info->var.yoffset = var->yoffset;
1408 drm_modeset_unlock_all(fb_helper->dev);
1409 mutex_unlock(&client->modeset_mutex);
1415 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1416 * @var: updated screen information
1417 * @info: fbdev registered by the helper
1419 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1420 struct fb_info *info)
1422 struct drm_fb_helper *fb_helper = info->par;
1423 struct drm_device *dev = fb_helper->dev;
1426 if (oops_in_progress)
1429 mutex_lock(&fb_helper->lock);
1430 if (!drm_master_internal_acquire(dev)) {
1435 if (drm_drv_uses_atomic_modeset(dev))
1436 ret = pan_display_atomic(var, info);
1438 ret = pan_display_legacy(var, info);
1440 drm_master_internal_release(dev);
1442 mutex_unlock(&fb_helper->lock);
1446 EXPORT_SYMBOL(drm_fb_helper_pan_display);
1449 * Allocates the backing storage and sets up the fbdev info structure through
1450 * the ->fb_probe callback.
1452 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1455 struct drm_client_dev *client = &fb_helper->client;
1458 struct drm_connector_list_iter conn_iter;
1459 struct drm_fb_helper_surface_size sizes;
1460 struct drm_connector *connector;
1461 struct drm_mode_set *mode_set;
1464 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1465 sizes.surface_depth = 24;
1466 sizes.surface_bpp = 32;
1467 sizes.fb_width = (u32)-1;
1468 sizes.fb_height = (u32)-1;
1471 * If driver picks 8 or 16 by default use that for both depth/bpp
1474 if (preferred_bpp != sizes.surface_bpp)
1475 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
1477 drm_connector_list_iter_begin(fb_helper->dev, &conn_iter);
1478 drm_client_for_each_connector_iter(connector, &conn_iter) {
1479 struct drm_cmdline_mode *cmdline_mode;
1481 cmdline_mode = &connector->cmdline_mode;
1483 if (cmdline_mode->bpp_specified) {
1484 switch (cmdline_mode->bpp) {
1486 sizes.surface_depth = sizes.surface_bpp = 8;
1489 sizes.surface_depth = 15;
1490 sizes.surface_bpp = 16;
1493 sizes.surface_depth = sizes.surface_bpp = 16;
1496 sizes.surface_depth = sizes.surface_bpp = 24;
1499 sizes.surface_depth = 24;
1500 sizes.surface_bpp = 32;
1506 drm_connector_list_iter_end(&conn_iter);
1509 * If we run into a situation where, for example, the primary plane
1510 * supports RGBA5551 (16 bpp, depth 15) but not RGB565 (16 bpp, depth
1511 * 16) we need to scale down the depth of the sizes we request.
1513 mutex_lock(&client->modeset_mutex);
1514 drm_client_for_each_modeset(mode_set, client) {
1515 struct drm_crtc *crtc = mode_set->crtc;
1516 struct drm_plane *plane = crtc->primary;
1519 DRM_DEBUG("test CRTC %u primary plane\n", drm_crtc_index(crtc));
1521 for (j = 0; j < plane->format_count; j++) {
1522 const struct drm_format_info *fmt;
1524 fmt = drm_format_info(plane->format_types[j]);
1527 * Do not consider YUV or other complicated formats
1528 * for framebuffers. This means only legacy formats
1529 * are supported (fmt->depth is a legacy field) but
1530 * the framebuffer emulation can only deal with such
1531 * formats, specifically RGB/BGA formats.
1533 if (fmt->depth == 0)
1536 /* We found a perfect fit, great */
1537 if (fmt->depth == sizes.surface_depth) {
1538 best_depth = fmt->depth;
1542 /* Skip depths above what we're looking for */
1543 if (fmt->depth > sizes.surface_depth)
1546 /* Best depth found so far */
1547 if (fmt->depth > best_depth)
1548 best_depth = fmt->depth;
1551 if (sizes.surface_depth != best_depth && best_depth) {
1552 DRM_INFO("requested bpp %d, scaled depth down to %d",
1553 sizes.surface_bpp, best_depth);
1554 sizes.surface_depth = best_depth;
1557 /* first up get a count of crtcs now in use and new min/maxes width/heights */
1559 drm_client_for_each_modeset(mode_set, client) {
1560 struct drm_display_mode *desired_mode;
1562 /* in case of tile group, are we the last tile vert or horiz?
1563 * If no tile group you are always the last one both vertically
1566 bool lastv = true, lasth = true;
1568 desired_mode = mode_set->mode;
1578 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1579 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
1581 for (j = 0; j < mode_set->num_connectors; j++) {
1582 struct drm_connector *connector = mode_set->connectors[j];
1584 if (connector->has_tile) {
1585 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1586 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1587 /* cloning to multiple tiles is just crazy-talk, so: */
1593 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1595 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
1597 mutex_unlock(&client->modeset_mutex);
1599 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
1600 DRM_INFO("Cannot find any crtc or sizes\n");
1602 /* First time: disable all crtc's.. */
1603 if (!fb_helper->deferred_setup)
1604 drm_client_modeset_commit(client);
1608 /* Handle our overallocation */
1609 sizes.surface_height *= drm_fbdev_overalloc;
1610 sizes.surface_height /= 100;
1612 /* push down into drivers */
1613 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1617 strcpy(fb_helper->fb->comm, "[fbcon]");
1621 static void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1624 info->fix.type = FB_TYPE_PACKED_PIXELS;
1625 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1626 FB_VISUAL_TRUECOLOR;
1627 info->fix.mmio_start = 0;
1628 info->fix.mmio_len = 0;
1629 info->fix.type_aux = 0;
1630 info->fix.xpanstep = 1; /* doing it in hw */
1631 info->fix.ypanstep = 1; /* doing it in hw */
1632 info->fix.ywrapstep = 0;
1633 info->fix.accel = FB_ACCEL_NONE;
1635 info->fix.line_length = pitch;
1638 static void drm_fb_helper_fill_var(struct fb_info *info,
1639 struct drm_fb_helper *fb_helper,
1640 uint32_t fb_width, uint32_t fb_height)
1642 struct drm_framebuffer *fb = fb_helper->fb;
1644 WARN_ON((drm_format_info_block_width(fb->format, 0) > 1) ||
1645 (drm_format_info_block_height(fb->format, 0) > 1));
1646 info->pseudo_palette = fb_helper->pseudo_palette;
1647 info->var.xres_virtual = fb->width;
1648 info->var.yres_virtual = fb->height;
1649 info->var.bits_per_pixel = fb->format->cpp[0] * 8;
1650 info->var.accel_flags = FB_ACCELF_TEXT;
1651 info->var.xoffset = 0;
1652 info->var.yoffset = 0;
1653 info->var.activate = FB_ACTIVATE_NOW;
1655 drm_fb_helper_fill_pixel_fmt(&info->var, fb->format->depth);
1657 info->var.xres = fb_width;
1658 info->var.yres = fb_height;
1662 * drm_fb_helper_fill_info - initializes fbdev information
1663 * @info: fbdev instance to set up
1664 * @fb_helper: fb helper instance to use as template
1665 * @sizes: describes fbdev size and scanout surface size
1667 * Sets up the variable and fixed fbdev metainformation from the given fb helper
1668 * instance and the drm framebuffer allocated in &drm_fb_helper.fb.
1670 * Drivers should call this (or their equivalent setup code) from their
1671 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
1672 * backing storage framebuffer.
1674 void drm_fb_helper_fill_info(struct fb_info *info,
1675 struct drm_fb_helper *fb_helper,
1676 struct drm_fb_helper_surface_size *sizes)
1678 struct drm_framebuffer *fb = fb_helper->fb;
1680 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
1681 drm_fb_helper_fill_var(info, fb_helper,
1682 sizes->fb_width, sizes->fb_height);
1684 info->par = fb_helper;
1685 snprintf(info->fix.id, sizeof(info->fix.id), "%sdrmfb",
1686 fb_helper->dev->driver->name);
1689 EXPORT_SYMBOL(drm_fb_helper_fill_info);
1692 * This is a continuation of drm_setup_crtcs() that sets up anything related
1693 * to the framebuffer. During initialization, drm_setup_crtcs() is called before
1694 * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev).
1695 * So, any setup that touches those fields needs to be done here instead of in
1696 * drm_setup_crtcs().
1698 static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper)
1700 struct drm_client_dev *client = &fb_helper->client;
1701 struct drm_connector_list_iter conn_iter;
1702 struct fb_info *info = fb_helper->fbdev;
1703 unsigned int rotation, sw_rotations = 0;
1704 struct drm_connector *connector;
1705 struct drm_mode_set *modeset;
1707 mutex_lock(&client->modeset_mutex);
1708 drm_client_for_each_modeset(modeset, client) {
1709 if (!modeset->num_connectors)
1712 modeset->fb = fb_helper->fb;
1714 if (drm_client_rotation(modeset, &rotation))
1715 /* Rotating in hardware, fbcon should not rotate */
1716 sw_rotations |= DRM_MODE_ROTATE_0;
1718 sw_rotations |= rotation;
1720 mutex_unlock(&client->modeset_mutex);
1722 drm_connector_list_iter_begin(fb_helper->dev, &conn_iter);
1723 drm_client_for_each_connector_iter(connector, &conn_iter) {
1725 /* use first connected connector for the physical dimensions */
1726 if (connector->status == connector_status_connected) {
1727 info->var.width = connector->display_info.width_mm;
1728 info->var.height = connector->display_info.height_mm;
1732 drm_connector_list_iter_end(&conn_iter);
1734 switch (sw_rotations) {
1735 case DRM_MODE_ROTATE_0:
1736 info->fbcon_rotate_hint = FB_ROTATE_UR;
1738 case DRM_MODE_ROTATE_90:
1739 info->fbcon_rotate_hint = FB_ROTATE_CCW;
1741 case DRM_MODE_ROTATE_180:
1742 info->fbcon_rotate_hint = FB_ROTATE_UD;
1744 case DRM_MODE_ROTATE_270:
1745 info->fbcon_rotate_hint = FB_ROTATE_CW;
1749 * Multiple bits are set / multiple rotations requested
1750 * fbcon cannot handle separate rotation settings per
1751 * output, so fallback to unrotated.
1753 info->fbcon_rotate_hint = FB_ROTATE_UR;
1757 /* Note: Drops fb_helper->lock before returning. */
1759 __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper,
1762 struct drm_device *dev = fb_helper->dev;
1763 struct fb_info *info;
1764 unsigned int width, height;
1767 width = dev->mode_config.max_width;
1768 height = dev->mode_config.max_height;
1770 drm_client_modeset_probe(&fb_helper->client, width, height);
1771 ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1773 if (ret == -EAGAIN) {
1774 fb_helper->preferred_bpp = bpp_sel;
1775 fb_helper->deferred_setup = true;
1778 mutex_unlock(&fb_helper->lock);
1782 drm_setup_crtcs_fb(fb_helper);
1784 fb_helper->deferred_setup = false;
1786 info = fb_helper->fbdev;
1787 info->var.pixclock = 0;
1788 /* Shamelessly allow physical address leaking to userspace */
1789 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
1790 if (!drm_leak_fbdev_smem)
1792 /* don't leak any physical addresses to userspace */
1793 info->flags |= FBINFO_HIDE_SMEM_START;
1795 /* Need to drop locks to avoid recursive deadlock in
1796 * register_framebuffer. This is ok because the only thing left to do is
1797 * register the fbdev emulation instance in kernel_fb_helper_list. */
1798 mutex_unlock(&fb_helper->lock);
1800 ret = register_framebuffer(info);
1804 dev_info(dev->dev, "fb%d: %s frame buffer device\n",
1805 info->node, info->fix.id);
1807 mutex_lock(&kernel_fb_helper_lock);
1808 if (list_empty(&kernel_fb_helper_list))
1809 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1811 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1812 mutex_unlock(&kernel_fb_helper_lock);
1818 * drm_fb_helper_initial_config - setup a sane initial connector configuration
1819 * @fb_helper: fb_helper device struct
1820 * @bpp_sel: bpp value to use for the framebuffer configuration
1822 * Scans the CRTCs and connectors and tries to put together an initial setup.
1823 * At the moment, this is a cloned configuration across all heads with
1824 * a new framebuffer object as the backing store.
1826 * Note that this also registers the fbdev and so allows userspace to call into
1827 * the driver through the fbdev interfaces.
1829 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
1830 * to let the driver allocate and initialize the fbdev info structure and the
1831 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_info() is provided
1832 * as a helper to setup simple default values for the fbdev info structure.
1836 * When you have fbcon support built-in or already loaded, this function will do
1837 * a full modeset to setup the fbdev console. Due to locking misdesign in the
1838 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
1839 * console_lock. Until console_unlock is called no dmesg lines will be sent out
1840 * to consoles, not even serial console. This means when your driver crashes,
1841 * you will see absolutely nothing else but a system stuck in this function,
1842 * with no further output. Any kind of printk() you place within your own driver
1843 * or in the drm core modeset code will also never show up.
1845 * Standard debug practice is to run the fbcon setup without taking the
1846 * console_lock as a hack, to be able to see backtraces and crashes on the
1847 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
1850 * The other option is to just disable fbdev emulation since very likely the
1851 * first modeset from userspace will crash in the same way, and is even easier
1852 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
1853 * kernel cmdline option.
1856 * Zero if everything went ok, nonzero otherwise.
1858 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1862 if (!drm_fbdev_emulation)
1865 mutex_lock(&fb_helper->lock);
1866 ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel);
1870 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1873 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1874 * probing all the outputs attached to the fb
1875 * @fb_helper: driver-allocated fbdev helper, can be NULL
1877 * Scan the connectors attached to the fb_helper and try to put together a
1878 * setup after notification of a change in output configuration.
1880 * Called at runtime, takes the mode config locks to be able to check/change the
1881 * modeset configuration. Must be run from process context (which usually means
1882 * either the output polling work or a work item launched from the driver's
1883 * hotplug interrupt).
1885 * Note that drivers may call this even before calling
1886 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
1887 * for a race-free fbcon setup and will make sure that the fbdev emulation will
1888 * not miss any hotplug events.
1891 * 0 on success and a non-zero error code otherwise.
1893 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1897 if (!drm_fbdev_emulation || !fb_helper)
1900 mutex_lock(&fb_helper->lock);
1901 if (fb_helper->deferred_setup) {
1902 err = __drm_fb_helper_initial_config_and_unlock(fb_helper,
1903 fb_helper->preferred_bpp);
1907 if (!fb_helper->fb || !drm_master_internal_acquire(fb_helper->dev)) {
1908 fb_helper->delayed_hotplug = true;
1909 mutex_unlock(&fb_helper->lock);
1913 drm_master_internal_release(fb_helper->dev);
1915 DRM_DEBUG_KMS("\n");
1917 drm_client_modeset_probe(&fb_helper->client, fb_helper->fb->width, fb_helper->fb->height);
1918 drm_setup_crtcs_fb(fb_helper);
1919 mutex_unlock(&fb_helper->lock);
1921 drm_fb_helper_set_par(fb_helper->fbdev);
1925 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
1928 * drm_fb_helper_fbdev_setup() - Setup fbdev emulation
1930 * @fb_helper: fbdev helper structure to set up
1931 * @funcs: fbdev helper functions
1932 * @preferred_bpp: Preferred bits per pixel for the device.
1933 * @dev->mode_config.preferred_depth is used if this is zero.
1934 * @max_conn_count: Maximum number of connectors (not used)
1936 * This function sets up fbdev emulation and registers fbdev for access by
1937 * userspace. If all connectors are disconnected, setup is deferred to the next
1938 * time drm_fb_helper_hotplug_event() is called.
1939 * The caller must to provide a &drm_fb_helper_funcs->fb_probe callback
1942 * Use drm_fb_helper_fbdev_teardown() to destroy the fbdev.
1944 * See also: drm_fb_helper_initial_config(), drm_fbdev_generic_setup().
1947 * Zero on success or negative error code on failure.
1949 int drm_fb_helper_fbdev_setup(struct drm_device *dev,
1950 struct drm_fb_helper *fb_helper,
1951 const struct drm_fb_helper_funcs *funcs,
1952 unsigned int preferred_bpp,
1953 unsigned int max_conn_count)
1958 preferred_bpp = dev->mode_config.preferred_depth;
1962 drm_fb_helper_prepare(dev, fb_helper, funcs);
1964 ret = drm_fb_helper_init(dev, fb_helper, 0);
1966 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to initialize (ret=%d)\n", ret);
1970 if (!drm_drv_uses_atomic_modeset(dev))
1971 drm_helper_disable_unused_functions(dev);
1973 ret = drm_fb_helper_initial_config(fb_helper, preferred_bpp);
1975 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to set configuration (ret=%d)\n", ret);
1976 goto err_drm_fb_helper_fini;
1981 err_drm_fb_helper_fini:
1982 drm_fb_helper_fbdev_teardown(dev);
1986 EXPORT_SYMBOL(drm_fb_helper_fbdev_setup);
1989 * drm_fb_helper_fbdev_teardown - Tear down fbdev emulation
1992 * This function unregisters fbdev if not already done and cleans up the
1993 * associated resources including the &drm_framebuffer.
1994 * The driver is responsible for freeing the &drm_fb_helper structure which is
1995 * stored in &drm_device->fb_helper. Do note that this pointer has been cleared
1996 * when this function returns.
1998 * In order to support device removal/unplug while file handles are still open,
1999 * drm_fb_helper_unregister_fbi() should be called on device removal and
2000 * drm_fb_helper_fbdev_teardown() in the &drm_driver->release callback when
2001 * file handles are closed.
2003 void drm_fb_helper_fbdev_teardown(struct drm_device *dev)
2005 struct drm_fb_helper *fb_helper = dev->fb_helper;
2006 struct fb_ops *fbops = NULL;
2011 /* Unregister if it hasn't been done already */
2012 if (fb_helper->fbdev && fb_helper->fbdev->dev)
2013 drm_fb_helper_unregister_fbi(fb_helper);
2015 if (fb_helper->fbdev && fb_helper->fbdev->fbdefio) {
2016 fb_deferred_io_cleanup(fb_helper->fbdev);
2017 kfree(fb_helper->fbdev->fbdefio);
2018 fbops = fb_helper->fbdev->fbops;
2021 drm_fb_helper_fini(fb_helper);
2025 drm_framebuffer_remove(fb_helper->fb);
2027 EXPORT_SYMBOL(drm_fb_helper_fbdev_teardown);
2030 * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation
2033 * This function can be used as the &drm_driver->lastclose callback for drivers
2034 * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked().
2036 void drm_fb_helper_lastclose(struct drm_device *dev)
2038 drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper);
2040 EXPORT_SYMBOL(drm_fb_helper_lastclose);
2043 * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed
2044 * helper for fbdev emulation
2047 * This function can be used as the
2048 * &drm_mode_config_funcs.output_poll_changed callback for drivers that only
2049 * need to call drm_fb_helper_hotplug_event().
2051 void drm_fb_helper_output_poll_changed(struct drm_device *dev)
2053 drm_fb_helper_hotplug_event(dev->fb_helper);
2055 EXPORT_SYMBOL(drm_fb_helper_output_poll_changed);
2057 /* @user: 1=userspace, 0=fbcon */
2058 static int drm_fbdev_fb_open(struct fb_info *info, int user)
2060 struct drm_fb_helper *fb_helper = info->par;
2062 /* No need to take a ref for fbcon because it unbinds on unregister */
2063 if (user && !try_module_get(fb_helper->dev->driver->fops->owner))
2069 static int drm_fbdev_fb_release(struct fb_info *info, int user)
2071 struct drm_fb_helper *fb_helper = info->par;
2074 module_put(fb_helper->dev->driver->fops->owner);
2079 static void drm_fbdev_cleanup(struct drm_fb_helper *fb_helper)
2081 struct fb_info *fbi = fb_helper->fbdev;
2082 struct fb_ops *fbops = NULL;
2083 void *shadow = NULL;
2085 if (!fb_helper->dev)
2088 if (fbi && fbi->fbdefio) {
2089 fb_deferred_io_cleanup(fbi);
2090 shadow = fbi->screen_buffer;
2094 drm_fb_helper_fini(fb_helper);
2101 drm_client_framebuffer_delete(fb_helper->buffer);
2104 static void drm_fbdev_release(struct drm_fb_helper *fb_helper)
2106 drm_fbdev_cleanup(fb_helper);
2107 drm_client_release(&fb_helper->client);
2112 * fb_ops.fb_destroy is called by the last put_fb_info() call at the end of
2113 * unregister_framebuffer() or fb_release().
2115 static void drm_fbdev_fb_destroy(struct fb_info *info)
2117 drm_fbdev_release(info->par);
2120 static int drm_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
2122 struct drm_fb_helper *fb_helper = info->par;
2124 if (fb_helper->dev->driver->gem_prime_mmap)
2125 return fb_helper->dev->driver->gem_prime_mmap(fb_helper->buffer->gem, vma);
2130 static struct fb_ops drm_fbdev_fb_ops = {
2131 .owner = THIS_MODULE,
2132 DRM_FB_HELPER_DEFAULT_OPS,
2133 .fb_open = drm_fbdev_fb_open,
2134 .fb_release = drm_fbdev_fb_release,
2135 .fb_destroy = drm_fbdev_fb_destroy,
2136 .fb_mmap = drm_fbdev_fb_mmap,
2137 .fb_read = drm_fb_helper_sys_read,
2138 .fb_write = drm_fb_helper_sys_write,
2139 .fb_fillrect = drm_fb_helper_sys_fillrect,
2140 .fb_copyarea = drm_fb_helper_sys_copyarea,
2141 .fb_imageblit = drm_fb_helper_sys_imageblit,
2144 static struct fb_deferred_io drm_fbdev_defio = {
2146 .deferred_io = drm_fb_helper_deferred_io,
2150 * drm_fb_helper_generic_probe - Generic fbdev emulation probe helper
2151 * @fb_helper: fbdev helper structure
2152 * @sizes: describes fbdev size and scanout surface size
2154 * This function uses the client API to create a framebuffer backed by a dumb buffer.
2156 * The _sys_ versions are used for &fb_ops.fb_read, fb_write, fb_fillrect,
2157 * fb_copyarea, fb_imageblit.
2160 * Zero on success or negative error code on failure.
2162 int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper,
2163 struct drm_fb_helper_surface_size *sizes)
2165 struct drm_client_dev *client = &fb_helper->client;
2166 struct drm_client_buffer *buffer;
2167 struct drm_framebuffer *fb;
2168 struct fb_info *fbi;
2172 DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n",
2173 sizes->surface_width, sizes->surface_height,
2174 sizes->surface_bpp);
2176 format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
2177 buffer = drm_client_framebuffer_create(client, sizes->surface_width,
2178 sizes->surface_height, format);
2180 return PTR_ERR(buffer);
2182 fb_helper->buffer = buffer;
2183 fb_helper->fb = buffer->fb;
2186 fbi = drm_fb_helper_alloc_fbi(fb_helper);
2188 return PTR_ERR(fbi);
2190 fbi->fbops = &drm_fbdev_fb_ops;
2191 fbi->screen_size = fb->height * fb->pitches[0];
2192 fbi->fix.smem_len = fbi->screen_size;
2194 drm_fb_helper_fill_info(fbi, fb_helper, sizes);
2196 if (drm_fbdev_use_shadow_fb(fb_helper)) {
2197 struct fb_ops *fbops;
2201 * fb_deferred_io_cleanup() clears &fbops->fb_mmap so a per
2202 * instance version is necessary.
2204 fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
2205 shadow = vzalloc(fbi->screen_size);
2206 if (!fbops || !shadow) {
2212 *fbops = *fbi->fbops;
2214 fbi->screen_buffer = shadow;
2215 fbi->fbdefio = &drm_fbdev_defio;
2217 fb_deferred_io_init(fbi);
2219 /* buffer is mapped for HW framebuffer */
2220 vaddr = drm_client_buffer_vmap(fb_helper->buffer);
2222 return PTR_ERR(vaddr);
2224 fbi->screen_buffer = vaddr;
2225 /* Shamelessly leak the physical address to user-space */
2226 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
2227 if (drm_leak_fbdev_smem && fbi->fix.smem_start == 0)
2228 fbi->fix.smem_start =
2229 page_to_phys(virt_to_page(fbi->screen_buffer));
2235 EXPORT_SYMBOL(drm_fb_helper_generic_probe);
2237 static const struct drm_fb_helper_funcs drm_fb_helper_generic_funcs = {
2238 .fb_probe = drm_fb_helper_generic_probe,
2241 static void drm_fbdev_client_unregister(struct drm_client_dev *client)
2243 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
2245 if (fb_helper->fbdev)
2246 /* drm_fbdev_fb_destroy() takes care of cleanup */
2247 drm_fb_helper_unregister_fbi(fb_helper);
2249 drm_fbdev_release(fb_helper);
2252 static int drm_fbdev_client_restore(struct drm_client_dev *client)
2254 drm_fb_helper_lastclose(client->dev);
2259 static int drm_fbdev_client_hotplug(struct drm_client_dev *client)
2261 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
2262 struct drm_device *dev = client->dev;
2265 /* Setup is not retried if it has failed */
2266 if (!fb_helper->dev && fb_helper->funcs)
2270 return drm_fb_helper_hotplug_event(dev->fb_helper);
2272 if (!dev->mode_config.num_connector) {
2273 DRM_DEV_DEBUG(dev->dev, "No connectors found, will not create framebuffer!\n");
2277 drm_fb_helper_prepare(dev, fb_helper, &drm_fb_helper_generic_funcs);
2279 ret = drm_fb_helper_init(dev, fb_helper, 0);
2283 if (!drm_drv_uses_atomic_modeset(dev))
2284 drm_helper_disable_unused_functions(dev);
2286 ret = drm_fb_helper_initial_config(fb_helper, fb_helper->preferred_bpp);
2293 drm_fbdev_cleanup(fb_helper);
2295 fb_helper->dev = NULL;
2296 fb_helper->fbdev = NULL;
2298 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to setup generic emulation (ret=%d)\n", ret);
2303 static const struct drm_client_funcs drm_fbdev_client_funcs = {
2304 .owner = THIS_MODULE,
2305 .unregister = drm_fbdev_client_unregister,
2306 .restore = drm_fbdev_client_restore,
2307 .hotplug = drm_fbdev_client_hotplug,
2311 * drm_fbdev_generic_setup() - Setup generic fbdev emulation
2313 * @preferred_bpp: Preferred bits per pixel for the device.
2314 * @dev->mode_config.preferred_depth is used if this is zero.
2316 * This function sets up generic fbdev emulation for drivers that supports
2317 * dumb buffers with a virtual address and that can be mmap'ed. If the driver
2318 * does not support these functions, it could use drm_fb_helper_fbdev_setup().
2320 * Restore, hotplug events and teardown are all taken care of. Drivers that do
2321 * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves.
2322 * Simple drivers might use drm_mode_config_helper_suspend().
2324 * Drivers that set the dirty callback on their framebuffer will get a shadow
2325 * fbdev buffer that is blitted onto the real buffer. This is done in order to
2326 * make deferred I/O work with all kinds of buffers. A shadow buffer can be
2327 * requested explicitly by setting struct drm_mode_config.prefer_shadow or
2328 * struct drm_mode_config.prefer_shadow_fbdev to true beforehand. This is
2329 * required to use generic fbdev emulation with SHMEM helpers.
2331 * This function is safe to call even when there are no connectors present.
2332 * Setup will be retried on the next hotplug event.
2334 * The fbdev is destroyed by drm_dev_unregister().
2337 * Zero on success or negative error code on failure.
2339 int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp)
2341 struct drm_fb_helper *fb_helper;
2344 WARN(dev->fb_helper, "fb_helper is already set!\n");
2346 if (!drm_fbdev_emulation)
2349 fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
2353 ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_client_funcs);
2356 DRM_DEV_ERROR(dev->dev, "Failed to register client: %d\n", ret);
2361 preferred_bpp = dev->mode_config.preferred_depth;
2364 fb_helper->preferred_bpp = preferred_bpp;
2366 ret = drm_fbdev_client_hotplug(&fb_helper->client);
2368 DRM_DEV_DEBUG(dev->dev, "client hotplug ret=%d\n", ret);
2370 drm_client_register(&fb_helper->client);
2374 EXPORT_SYMBOL(drm_fbdev_generic_setup);
2376 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
2377 * but the module doesn't depend on any fb console symbols. At least
2378 * attempt to load fbcon to avoid leaving the system without a usable console.
2380 int __init drm_fb_helper_modinit(void)
2382 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
2383 const char name[] = "fbcon";
2384 struct module *fbcon;
2386 mutex_lock(&module_mutex);
2387 fbcon = find_module(name);
2388 mutex_unlock(&module_mutex);
2391 request_module_nowait(name);
2395 EXPORT_SYMBOL(drm_fb_helper_modinit);