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_gem.h>
11 #include <drm/drm_print.h>
13 #include <drm/drm_fbdev_generic.h>
15 /* @user: 1=userspace, 0=fbcon */
16 static int drm_fbdev_generic_fb_open(struct fb_info *info, int user)
18 struct drm_fb_helper *fb_helper = info->par;
20 /* No need to take a ref for fbcon because it unbinds on unregister */
21 if (user && !try_module_get(fb_helper->dev->driver->fops->owner))
27 static int drm_fbdev_generic_fb_release(struct fb_info *info, int user)
29 struct drm_fb_helper *fb_helper = info->par;
32 module_put(fb_helper->dev->driver->fops->owner);
37 FB_GEN_DEFAULT_DEFERRED_SYSMEM_OPS(drm_fbdev_generic,
38 drm_fb_helper_damage_range,
39 drm_fb_helper_damage_area);
41 static void drm_fbdev_generic_fb_destroy(struct fb_info *info)
43 struct drm_fb_helper *fb_helper = info->par;
44 void *shadow = info->screen_buffer;
49 fb_deferred_io_cleanup(info);
50 drm_fb_helper_fini(fb_helper);
52 drm_client_framebuffer_delete(fb_helper->buffer);
54 drm_client_release(&fb_helper->client);
55 drm_fb_helper_unprepare(fb_helper);
59 static const struct fb_ops drm_fbdev_generic_fb_ops = {
61 .fb_open = drm_fbdev_generic_fb_open,
62 .fb_release = drm_fbdev_generic_fb_release,
63 FB_DEFAULT_DEFERRED_OPS(drm_fbdev_generic),
64 DRM_FB_HELPER_DEFAULT_OPS,
65 .fb_destroy = drm_fbdev_generic_fb_destroy,
69 * This function uses the client API to create a framebuffer backed by a dumb buffer.
71 static int drm_fbdev_generic_helper_fb_probe(struct drm_fb_helper *fb_helper,
72 struct drm_fb_helper_surface_size *sizes)
74 struct drm_client_dev *client = &fb_helper->client;
75 struct drm_device *dev = fb_helper->dev;
76 struct drm_client_buffer *buffer;
83 drm_dbg_kms(dev, "surface width(%d), height(%d) and bpp(%d)\n",
84 sizes->surface_width, sizes->surface_height,
87 format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
88 buffer = drm_client_framebuffer_create(client, sizes->surface_width,
89 sizes->surface_height, format);
91 return PTR_ERR(buffer);
93 fb_helper->buffer = buffer;
94 fb_helper->fb = buffer->fb;
96 screen_size = buffer->gem->size;
97 screen_buffer = vzalloc(screen_size);
100 goto err_drm_client_framebuffer_delete;
103 info = drm_fb_helper_alloc_info(fb_helper);
109 drm_fb_helper_fill_info(info, fb_helper, sizes);
111 info->fbops = &drm_fbdev_generic_fb_ops;
114 info->flags |= FBINFO_VIRTFB | FBINFO_READS_FAST;
115 info->screen_buffer = screen_buffer;
116 info->fix.smem_start = page_to_phys(vmalloc_to_page(info->screen_buffer));
117 info->fix.smem_len = screen_size;
120 fb_helper->fbdefio.delay = HZ / 20;
121 fb_helper->fbdefio.deferred_io = drm_fb_helper_deferred_io;
123 info->fbdefio = &fb_helper->fbdefio;
124 ret = fb_deferred_io_init(info);
126 goto err_drm_fb_helper_release_info;
130 err_drm_fb_helper_release_info:
131 drm_fb_helper_release_info(fb_helper);
133 vfree(screen_buffer);
134 err_drm_client_framebuffer_delete:
135 fb_helper->fb = NULL;
136 fb_helper->buffer = NULL;
137 drm_client_framebuffer_delete(buffer);
141 static void drm_fbdev_generic_damage_blit_real(struct drm_fb_helper *fb_helper,
142 struct drm_clip_rect *clip,
143 struct iosys_map *dst)
145 struct drm_framebuffer *fb = fb_helper->fb;
146 size_t offset = clip->y1 * fb->pitches[0];
147 size_t len = clip->x2 - clip->x1;
151 switch (drm_format_info_bpp(fb->format, 0)) {
153 offset += clip->x1 / 8;
154 len = DIV_ROUND_UP(len + clip->x1 % 8, 8);
157 offset += clip->x1 / 4;
158 len = DIV_ROUND_UP(len + clip->x1 % 4, 4);
161 offset += clip->x1 / 2;
162 len = DIV_ROUND_UP(len + clip->x1 % 2, 2);
165 offset += clip->x1 * fb->format->cpp[0];
166 len *= fb->format->cpp[0];
170 src = fb_helper->info->screen_buffer + offset;
171 iosys_map_incr(dst, offset); /* go to first pixel within clip rect */
173 for (y = clip->y1; y < clip->y2; y++) {
174 iosys_map_memcpy_to(dst, 0, src, len);
175 iosys_map_incr(dst, fb->pitches[0]);
176 src += fb->pitches[0];
180 static int drm_fbdev_generic_damage_blit(struct drm_fb_helper *fb_helper,
181 struct drm_clip_rect *clip)
183 struct drm_client_buffer *buffer = fb_helper->buffer;
184 struct iosys_map map, dst;
188 * We have to pin the client buffer to its current location while
189 * flushing the shadow buffer. In the general case, concurrent
190 * modesetting operations could try to move the buffer and would
191 * fail. The modeset has to be serialized by acquiring the reservation
192 * object of the underlying BO here.
194 * For fbdev emulation, we only have to protect against fbdev modeset
195 * operations. Nothing else will involve the client buffer's BO. So it
196 * is sufficient to acquire struct drm_fb_helper.lock here.
198 mutex_lock(&fb_helper->lock);
200 ret = drm_client_buffer_vmap(buffer, &map);
205 drm_fbdev_generic_damage_blit_real(fb_helper, clip, &dst);
207 drm_client_buffer_vunmap(buffer);
210 mutex_unlock(&fb_helper->lock);
215 static int drm_fbdev_generic_helper_fb_dirty(struct drm_fb_helper *helper,
216 struct drm_clip_rect *clip)
218 struct drm_device *dev = helper->dev;
221 /* Call damage handlers only if necessary */
222 if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2))
225 ret = drm_fbdev_generic_damage_blit(helper, clip);
226 if (drm_WARN_ONCE(dev, ret, "Damage blitter failed: ret=%d\n", ret))
229 if (helper->fb->funcs->dirty) {
230 ret = helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1);
231 if (drm_WARN_ONCE(dev, ret, "Dirty helper failed: ret=%d\n", ret))
238 static const struct drm_fb_helper_funcs drm_fbdev_generic_helper_funcs = {
239 .fb_probe = drm_fbdev_generic_helper_fb_probe,
240 .fb_dirty = drm_fbdev_generic_helper_fb_dirty,
243 static void drm_fbdev_generic_client_unregister(struct drm_client_dev *client)
245 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
247 if (fb_helper->info) {
248 drm_fb_helper_unregister_info(fb_helper);
250 drm_client_release(&fb_helper->client);
251 drm_fb_helper_unprepare(fb_helper);
256 static int drm_fbdev_generic_client_restore(struct drm_client_dev *client)
258 drm_fb_helper_lastclose(client->dev);
263 static int drm_fbdev_generic_client_hotplug(struct drm_client_dev *client)
265 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
266 struct drm_device *dev = client->dev;
270 return drm_fb_helper_hotplug_event(dev->fb_helper);
272 ret = drm_fb_helper_init(dev, fb_helper);
276 if (!drm_drv_uses_atomic_modeset(dev))
277 drm_helper_disable_unused_functions(dev);
279 ret = drm_fb_helper_initial_config(fb_helper);
281 goto err_drm_fb_helper_fini;
285 err_drm_fb_helper_fini:
286 drm_fb_helper_fini(fb_helper);
288 drm_err(dev, "fbdev: Failed to setup generic emulation (ret=%d)\n", ret);
292 static const struct drm_client_funcs drm_fbdev_generic_client_funcs = {
293 .owner = THIS_MODULE,
294 .unregister = drm_fbdev_generic_client_unregister,
295 .restore = drm_fbdev_generic_client_restore,
296 .hotplug = drm_fbdev_generic_client_hotplug,
300 * drm_fbdev_generic_setup() - Setup generic fbdev emulation
302 * @preferred_bpp: Preferred bits per pixel for the device.
304 * This function sets up generic fbdev emulation for drivers that supports
305 * dumb buffers with a virtual address and that can be mmap'ed.
306 * drm_fbdev_generic_setup() shall be called after the DRM driver registered
307 * the new DRM device with drm_dev_register().
309 * Restore, hotplug events and teardown are all taken care of. Drivers that do
310 * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves.
311 * Simple drivers might use drm_mode_config_helper_suspend().
313 * In order to provide fixed mmap-able memory ranges, generic fbdev emulation
314 * uses a shadow buffer in system memory. The implementation blits the shadow
315 * fbdev buffer onto the real buffer in regular intervals.
317 * This function is safe to call even when there are no connectors present.
318 * Setup will be retried on the next hotplug event.
320 * The fbdev is destroyed by drm_dev_unregister().
322 void drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp)
324 struct drm_fb_helper *fb_helper;
327 drm_WARN(dev, !dev->registered, "Device has not been registered.\n");
328 drm_WARN(dev, dev->fb_helper, "fb_helper is already set!\n");
330 fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
333 drm_fb_helper_prepare(dev, fb_helper, preferred_bpp, &drm_fbdev_generic_helper_funcs);
335 ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_generic_client_funcs);
337 drm_err(dev, "Failed to register client: %d\n", ret);
338 goto err_drm_client_init;
341 drm_client_register(&fb_helper->client);
346 drm_fb_helper_unprepare(fb_helper);
350 EXPORT_SYMBOL(drm_fbdev_generic_setup);