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_ttm.h>
15 /* @user: 1=userspace, 0=fbcon */
16 static int drm_fbdev_ttm_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_ttm_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_ttm,
38 drm_fb_helper_damage_range,
39 drm_fb_helper_damage_area);
41 static void drm_fbdev_ttm_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_ttm_fb_ops = {
61 .fb_open = drm_fbdev_ttm_fb_open,
62 .fb_release = drm_fbdev_ttm_fb_release,
63 FB_DEFAULT_DEFERRED_OPS(drm_fbdev_ttm),
64 DRM_FB_HELPER_DEFAULT_OPS,
65 .fb_destroy = drm_fbdev_ttm_fb_destroy,
68 static void drm_fbdev_ttm_damage_blit_real(struct drm_fb_helper *fb_helper,
69 struct drm_clip_rect *clip,
70 struct iosys_map *dst)
72 struct drm_framebuffer *fb = fb_helper->fb;
73 size_t offset = clip->y1 * fb->pitches[0];
74 size_t len = clip->x2 - clip->x1;
78 switch (drm_format_info_bpp(fb->format, 0)) {
80 offset += clip->x1 / 8;
81 len = DIV_ROUND_UP(len + clip->x1 % 8, 8);
84 offset += clip->x1 / 4;
85 len = DIV_ROUND_UP(len + clip->x1 % 4, 4);
88 offset += clip->x1 / 2;
89 len = DIV_ROUND_UP(len + clip->x1 % 2, 2);
92 offset += clip->x1 * fb->format->cpp[0];
93 len *= fb->format->cpp[0];
97 src = fb_helper->info->screen_buffer + offset;
98 iosys_map_incr(dst, offset); /* go to first pixel within clip rect */
100 for (y = clip->y1; y < clip->y2; y++) {
101 iosys_map_memcpy_to(dst, 0, src, len);
102 iosys_map_incr(dst, fb->pitches[0]);
103 src += fb->pitches[0];
107 static int drm_fbdev_ttm_damage_blit(struct drm_fb_helper *fb_helper,
108 struct drm_clip_rect *clip)
110 struct drm_client_buffer *buffer = fb_helper->buffer;
111 struct iosys_map map, dst;
115 * We have to pin the client buffer to its current location while
116 * flushing the shadow buffer. In the general case, concurrent
117 * modesetting operations could try to move the buffer and would
118 * fail. The modeset has to be serialized by acquiring the reservation
119 * object of the underlying BO here.
121 * For fbdev emulation, we only have to protect against fbdev modeset
122 * operations. Nothing else will involve the client buffer's BO. So it
123 * is sufficient to acquire struct drm_fb_helper.lock here.
125 mutex_lock(&fb_helper->lock);
127 ret = drm_client_buffer_vmap_local(buffer, &map);
132 drm_fbdev_ttm_damage_blit_real(fb_helper, clip, &dst);
134 drm_client_buffer_vunmap_local(buffer);
137 mutex_unlock(&fb_helper->lock);
142 static int drm_fbdev_ttm_helper_fb_dirty(struct drm_fb_helper *helper,
143 struct drm_clip_rect *clip)
145 struct drm_device *dev = helper->dev;
148 /* Call damage handlers only if necessary */
149 if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2))
152 ret = drm_fbdev_ttm_damage_blit(helper, clip);
153 if (drm_WARN_ONCE(dev, ret, "Damage blitter failed: ret=%d\n", ret))
156 if (helper->fb->funcs->dirty) {
157 ret = helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1);
158 if (drm_WARN_ONCE(dev, ret, "Dirty helper failed: ret=%d\n", ret))
165 static const struct drm_fb_helper_funcs drm_fbdev_ttm_helper_funcs = {
166 .fb_dirty = drm_fbdev_ttm_helper_fb_dirty,
173 int drm_fbdev_ttm_driver_fbdev_probe(struct drm_fb_helper *fb_helper,
174 struct drm_fb_helper_surface_size *sizes)
176 struct drm_client_dev *client = &fb_helper->client;
177 struct drm_device *dev = fb_helper->dev;
178 struct drm_client_buffer *buffer;
179 struct fb_info *info;
185 drm_dbg_kms(dev, "surface width(%d), height(%d) and bpp(%d)\n",
186 sizes->surface_width, sizes->surface_height,
189 format = drm_driver_legacy_fb_format(dev, sizes->surface_bpp,
190 sizes->surface_depth);
191 buffer = drm_client_framebuffer_create(client, sizes->surface_width,
192 sizes->surface_height, format);
194 return PTR_ERR(buffer);
196 fb_helper->funcs = &drm_fbdev_ttm_helper_funcs;
197 fb_helper->buffer = buffer;
198 fb_helper->fb = buffer->fb;
200 screen_size = buffer->gem->size;
201 screen_buffer = vzalloc(screen_size);
202 if (!screen_buffer) {
204 goto err_drm_client_framebuffer_delete;
207 info = drm_fb_helper_alloc_info(fb_helper);
213 drm_fb_helper_fill_info(info, fb_helper, sizes);
215 info->fbops = &drm_fbdev_ttm_fb_ops;
218 info->flags |= FBINFO_VIRTFB | FBINFO_READS_FAST;
219 info->screen_buffer = screen_buffer;
220 info->fix.smem_len = screen_size;
223 fb_helper->fbdefio.delay = HZ / 20;
224 fb_helper->fbdefio.deferred_io = drm_fb_helper_deferred_io;
226 info->fbdefio = &fb_helper->fbdefio;
227 ret = fb_deferred_io_init(info);
229 goto err_drm_fb_helper_release_info;
233 err_drm_fb_helper_release_info:
234 drm_fb_helper_release_info(fb_helper);
236 vfree(screen_buffer);
237 err_drm_client_framebuffer_delete:
238 fb_helper->fb = NULL;
239 fb_helper->buffer = NULL;
240 drm_client_framebuffer_delete(buffer);
243 EXPORT_SYMBOL(drm_fbdev_ttm_driver_fbdev_probe);