1 // SPDX-License-Identifier: MIT
3 #include <linux/moduleparam.h>
4 #include <linux/vmalloc.h>
6 #include <drm/drm_crtc_helper.h>
7 #include <drm/drm_drv.h>
8 #include <drm/drm_fb_helper.h>
9 #include <drm/drm_framebuffer.h>
10 #include <drm/drm_print.h>
12 #include <drm/drm_fbdev_generic.h>
14 static bool drm_fbdev_use_shadow_fb(struct drm_fb_helper *fb_helper)
16 struct drm_device *dev = fb_helper->dev;
17 struct drm_framebuffer *fb = fb_helper->fb;
19 return dev->mode_config.prefer_shadow_fbdev ||
20 dev->mode_config.prefer_shadow ||
24 /* @user: 1=userspace, 0=fbcon */
25 static int drm_fbdev_fb_open(struct fb_info *info, int user)
27 struct drm_fb_helper *fb_helper = info->par;
29 /* No need to take a ref for fbcon because it unbinds on unregister */
30 if (user && !try_module_get(fb_helper->dev->driver->fops->owner))
36 static int drm_fbdev_fb_release(struct fb_info *info, int user)
38 struct drm_fb_helper *fb_helper = info->par;
41 module_put(fb_helper->dev->driver->fops->owner);
46 static void drm_fbdev_cleanup(struct drm_fb_helper *fb_helper)
48 struct fb_info *fbi = fb_helper->info;
56 fb_deferred_io_cleanup(fbi);
57 if (drm_fbdev_use_shadow_fb(fb_helper))
58 shadow = fbi->screen_buffer;
61 drm_fb_helper_fini(fb_helper);
65 else if (fb_helper->buffer)
66 drm_client_buffer_vunmap(fb_helper->buffer);
68 drm_client_framebuffer_delete(fb_helper->buffer);
71 static void drm_fbdev_release(struct drm_fb_helper *fb_helper)
73 drm_fbdev_cleanup(fb_helper);
74 drm_client_release(&fb_helper->client);
79 * fb_ops.fb_destroy is called by the last put_fb_info() call at the end of
80 * unregister_framebuffer() or fb_release().
82 static void drm_fbdev_fb_destroy(struct fb_info *info)
84 drm_fbdev_release(info->par);
87 static int drm_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
89 struct drm_fb_helper *fb_helper = info->par;
91 if (drm_fbdev_use_shadow_fb(fb_helper))
92 return fb_deferred_io_mmap(info, vma);
93 else if (fb_helper->dev->driver->gem_prime_mmap)
94 return fb_helper->dev->driver->gem_prime_mmap(fb_helper->buffer->gem, vma);
99 static bool drm_fbdev_use_iomem(struct fb_info *info)
101 struct drm_fb_helper *fb_helper = info->par;
102 struct drm_client_buffer *buffer = fb_helper->buffer;
104 return !drm_fbdev_use_shadow_fb(fb_helper) && buffer->map.is_iomem;
107 static ssize_t drm_fbdev_fb_read(struct fb_info *info, char __user *buf,
108 size_t count, loff_t *ppos)
112 if (drm_fbdev_use_iomem(info))
113 ret = drm_fb_helper_cfb_read(info, buf, count, ppos);
115 ret = drm_fb_helper_sys_read(info, buf, count, ppos);
120 static ssize_t drm_fbdev_fb_write(struct fb_info *info, const char __user *buf,
121 size_t count, loff_t *ppos)
125 if (drm_fbdev_use_iomem(info))
126 ret = drm_fb_helper_cfb_write(info, buf, count, ppos);
128 ret = drm_fb_helper_sys_write(info, buf, count, ppos);
133 static void drm_fbdev_fb_fillrect(struct fb_info *info,
134 const struct fb_fillrect *rect)
136 if (drm_fbdev_use_iomem(info))
137 drm_fb_helper_cfb_fillrect(info, rect);
139 drm_fb_helper_sys_fillrect(info, rect);
142 static void drm_fbdev_fb_copyarea(struct fb_info *info,
143 const struct fb_copyarea *area)
145 if (drm_fbdev_use_iomem(info))
146 drm_fb_helper_cfb_copyarea(info, area);
148 drm_fb_helper_sys_copyarea(info, area);
151 static void drm_fbdev_fb_imageblit(struct fb_info *info,
152 const struct fb_image *image)
154 if (drm_fbdev_use_iomem(info))
155 drm_fb_helper_cfb_imageblit(info, image);
157 drm_fb_helper_sys_imageblit(info, image);
160 static const struct fb_ops drm_fbdev_fb_ops = {
161 .owner = THIS_MODULE,
162 DRM_FB_HELPER_DEFAULT_OPS,
163 .fb_open = drm_fbdev_fb_open,
164 .fb_release = drm_fbdev_fb_release,
165 .fb_destroy = drm_fbdev_fb_destroy,
166 .fb_mmap = drm_fbdev_fb_mmap,
167 .fb_read = drm_fbdev_fb_read,
168 .fb_write = drm_fbdev_fb_write,
169 .fb_fillrect = drm_fbdev_fb_fillrect,
170 .fb_copyarea = drm_fbdev_fb_copyarea,
171 .fb_imageblit = drm_fbdev_fb_imageblit,
174 static struct fb_deferred_io drm_fbdev_defio = {
176 .deferred_io = drm_fb_helper_deferred_io,
180 * This function uses the client API to create a framebuffer backed by a dumb buffer.
182 static int drm_fbdev_fb_probe(struct drm_fb_helper *fb_helper,
183 struct drm_fb_helper_surface_size *sizes)
185 struct drm_client_dev *client = &fb_helper->client;
186 struct drm_device *dev = fb_helper->dev;
187 struct drm_client_buffer *buffer;
188 struct drm_framebuffer *fb;
191 struct iosys_map map;
194 drm_dbg_kms(dev, "surface width(%d), height(%d) and bpp(%d)\n",
195 sizes->surface_width, sizes->surface_height,
198 format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
199 buffer = drm_client_framebuffer_create(client, sizes->surface_width,
200 sizes->surface_height, format);
202 return PTR_ERR(buffer);
204 fb_helper->buffer = buffer;
205 fb_helper->fb = buffer->fb;
208 fbi = drm_fb_helper_alloc_info(fb_helper);
212 fbi->fbops = &drm_fbdev_fb_ops;
213 fbi->screen_size = sizes->surface_height * fb->pitches[0];
214 fbi->fix.smem_len = fbi->screen_size;
215 fbi->flags = FBINFO_DEFAULT;
217 drm_fb_helper_fill_info(fbi, fb_helper, sizes);
219 if (drm_fbdev_use_shadow_fb(fb_helper)) {
220 fbi->screen_buffer = vzalloc(fbi->screen_size);
221 if (!fbi->screen_buffer)
223 fbi->flags |= FBINFO_VIRTFB | FBINFO_READS_FAST;
225 fbi->fbdefio = &drm_fbdev_defio;
226 fb_deferred_io_init(fbi);
228 /* buffer is mapped for HW framebuffer */
229 ret = drm_client_buffer_vmap(fb_helper->buffer, &map);
233 fbi->screen_base = map.vaddr_iomem;
235 fbi->screen_buffer = map.vaddr;
236 fbi->flags |= FBINFO_VIRTFB;
240 * Shamelessly leak the physical address to user-space. As
241 * page_to_phys() is undefined for I/O memory, warn in this
244 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
245 if (fb_helper->hint_leak_smem_start && fbi->fix.smem_start == 0 &&
246 !drm_WARN_ON_ONCE(dev, map.is_iomem))
247 fbi->fix.smem_start =
248 page_to_phys(virt_to_page(fbi->screen_buffer));
255 static void drm_fbdev_damage_blit_real(struct drm_fb_helper *fb_helper,
256 struct drm_clip_rect *clip,
257 struct iosys_map *dst)
259 struct drm_framebuffer *fb = fb_helper->fb;
260 size_t offset = clip->y1 * fb->pitches[0];
261 size_t len = clip->x2 - clip->x1;
265 switch (drm_format_info_bpp(fb->format, 0)) {
267 offset += clip->x1 / 8;
268 len = DIV_ROUND_UP(len + clip->x1 % 8, 8);
271 offset += clip->x1 / 4;
272 len = DIV_ROUND_UP(len + clip->x1 % 4, 4);
275 offset += clip->x1 / 2;
276 len = DIV_ROUND_UP(len + clip->x1 % 2, 2);
279 offset += clip->x1 * fb->format->cpp[0];
280 len *= fb->format->cpp[0];
284 src = fb_helper->info->screen_buffer + offset;
285 iosys_map_incr(dst, offset); /* go to first pixel within clip rect */
287 for (y = clip->y1; y < clip->y2; y++) {
288 iosys_map_memcpy_to(dst, 0, src, len);
289 iosys_map_incr(dst, fb->pitches[0]);
290 src += fb->pitches[0];
294 static int drm_fbdev_damage_blit(struct drm_fb_helper *fb_helper,
295 struct drm_clip_rect *clip)
297 struct drm_client_buffer *buffer = fb_helper->buffer;
298 struct iosys_map map, dst;
302 * We have to pin the client buffer to its current location while
303 * flushing the shadow buffer. In the general case, concurrent
304 * modesetting operations could try to move the buffer and would
305 * fail. The modeset has to be serialized by acquiring the reservation
306 * object of the underlying BO here.
308 * For fbdev emulation, we only have to protect against fbdev modeset
309 * operations. Nothing else will involve the client buffer's BO. So it
310 * is sufficient to acquire struct drm_fb_helper.lock here.
312 mutex_lock(&fb_helper->lock);
314 ret = drm_client_buffer_vmap(buffer, &map);
319 drm_fbdev_damage_blit_real(fb_helper, clip, &dst);
321 drm_client_buffer_vunmap(buffer);
324 mutex_unlock(&fb_helper->lock);
329 static int drm_fbdev_fb_dirty(struct drm_fb_helper *helper, struct drm_clip_rect *clip)
331 struct drm_device *dev = helper->dev;
334 if (!drm_fbdev_use_shadow_fb(helper))
337 /* Call damage handlers only if necessary */
338 if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2))
341 if (helper->buffer) {
342 ret = drm_fbdev_damage_blit(helper, clip);
343 if (drm_WARN_ONCE(dev, ret, "Damage blitter failed: ret=%d\n", ret))
347 if (helper->fb->funcs->dirty) {
348 ret = helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1);
349 if (drm_WARN_ONCE(dev, ret, "Dirty helper failed: ret=%d\n", ret))
356 static const struct drm_fb_helper_funcs drm_fb_helper_generic_funcs = {
357 .fb_probe = drm_fbdev_fb_probe,
358 .fb_dirty = drm_fbdev_fb_dirty,
361 static void drm_fbdev_client_unregister(struct drm_client_dev *client)
363 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
366 /* drm_fbdev_fb_destroy() takes care of cleanup */
367 drm_fb_helper_unregister_info(fb_helper);
369 drm_fbdev_release(fb_helper);
372 static int drm_fbdev_client_restore(struct drm_client_dev *client)
374 drm_fb_helper_lastclose(client->dev);
379 static int drm_fbdev_client_hotplug(struct drm_client_dev *client)
381 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
382 struct drm_device *dev = client->dev;
385 /* Setup is not retried if it has failed */
386 if (!fb_helper->dev && fb_helper->funcs)
390 return drm_fb_helper_hotplug_event(dev->fb_helper);
392 if (!dev->mode_config.num_connector) {
393 drm_dbg_kms(dev, "No connectors found, will not create framebuffer!\n");
397 drm_fb_helper_prepare(dev, fb_helper, &drm_fb_helper_generic_funcs);
399 ret = drm_fb_helper_init(dev, fb_helper);
403 if (!drm_drv_uses_atomic_modeset(dev))
404 drm_helper_disable_unused_functions(dev);
406 ret = drm_fb_helper_initial_config(fb_helper, fb_helper->preferred_bpp);
413 drm_fbdev_cleanup(fb_helper);
415 fb_helper->dev = NULL;
416 fb_helper->info = NULL;
418 drm_err(dev, "fbdev: Failed to setup generic emulation (ret=%d)\n", ret);
423 static const struct drm_client_funcs drm_fbdev_client_funcs = {
424 .owner = THIS_MODULE,
425 .unregister = drm_fbdev_client_unregister,
426 .restore = drm_fbdev_client_restore,
427 .hotplug = drm_fbdev_client_hotplug,
431 * drm_fbdev_generic_setup() - Setup generic fbdev emulation
433 * @preferred_bpp: Preferred bits per pixel for the device.
434 * @dev->mode_config.preferred_depth is used if this is zero.
436 * This function sets up generic fbdev emulation for drivers that supports
437 * dumb buffers with a virtual address and that can be mmap'ed.
438 * drm_fbdev_generic_setup() shall be called after the DRM driver registered
439 * the new DRM device with drm_dev_register().
441 * Restore, hotplug events and teardown are all taken care of. Drivers that do
442 * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves.
443 * Simple drivers might use drm_mode_config_helper_suspend().
445 * Drivers that set the dirty callback on their framebuffer will get a shadow
446 * fbdev buffer that is blitted onto the real buffer. This is done in order to
447 * make deferred I/O work with all kinds of buffers. A shadow buffer can be
448 * requested explicitly by setting struct drm_mode_config.prefer_shadow or
449 * struct drm_mode_config.prefer_shadow_fbdev to true beforehand. This is
450 * required to use generic fbdev emulation with SHMEM helpers.
452 * This function is safe to call even when there are no connectors present.
453 * Setup will be retried on the next hotplug event.
455 * The fbdev is destroyed by drm_dev_unregister().
457 void drm_fbdev_generic_setup(struct drm_device *dev,
458 unsigned int preferred_bpp)
460 struct drm_fb_helper *fb_helper;
463 drm_WARN(dev, !dev->registered, "Device has not been registered.\n");
464 drm_WARN(dev, dev->fb_helper, "fb_helper is already set!\n");
466 fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
470 ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_client_funcs);
473 drm_err(dev, "Failed to register client: %d\n", ret);
478 * FIXME: This mixes up depth with bpp, which results in a glorious
479 * mess, resulting in some drivers picking wrong fbdev defaults and
480 * others wrong preferred_depth defaults.
483 preferred_bpp = dev->mode_config.preferred_depth;
486 fb_helper->preferred_bpp = preferred_bpp;
488 ret = drm_fbdev_client_hotplug(&fb_helper->client);
490 drm_dbg_kms(dev, "client hotplug ret=%d\n", ret);
492 drm_client_register(&fb_helper->client);
494 EXPORT_SYMBOL(drm_fbdev_generic_setup);