1 // SPDX-License-Identifier: MIT
2
3 #include <linux/export.h>
4 #include <linux/moduleparam.h>
5 #include <linux/vmalloc.h>
6
7 #include <drm/drm_crtc_helper.h>
8 #include <drm/drm_drv.h>
9 #include <drm/drm_fb_helper.h>
10 #include <drm/drm_framebuffer.h>
11 #include <drm/drm_gem.h>
12 #include <drm/drm_print.h>
13
14 #include <drm/drm_fbdev_ttm.h>
15
16 /* @user: 1=userspace, 0=fbcon */
drm_fbdev_ttm_fb_open(struct fb_info * info,int user)17 static int drm_fbdev_ttm_fb_open(struct fb_info *info, int user)
18 {
19 struct drm_fb_helper *fb_helper = info->par;
20
21 /* No need to take a ref for fbcon because it unbinds on unregister */
22 if (user && !try_module_get(fb_helper->dev->driver->fops->owner))
23 return -ENODEV;
24
25 return 0;
26 }
27
drm_fbdev_ttm_fb_release(struct fb_info * info,int user)28 static int drm_fbdev_ttm_fb_release(struct fb_info *info, int user)
29 {
30 struct drm_fb_helper *fb_helper = info->par;
31
32 if (user)
33 module_put(fb_helper->dev->driver->fops->owner);
34
35 return 0;
36 }
37
38 FB_GEN_DEFAULT_DEFERRED_SYSMEM_OPS(drm_fbdev_ttm,
39 drm_fb_helper_damage_range,
40 drm_fb_helper_damage_area);
41
drm_fbdev_ttm_fb_destroy(struct fb_info * info)42 static void drm_fbdev_ttm_fb_destroy(struct fb_info *info)
43 {
44 struct drm_fb_helper *fb_helper = info->par;
45 void *shadow = info->screen_buffer;
46
47 if (!fb_helper->dev)
48 return;
49
50 fb_deferred_io_cleanup(info);
51 drm_fb_helper_fini(fb_helper);
52 vfree(shadow);
53 drm_client_framebuffer_delete(fb_helper->buffer);
54
55 drm_client_release(&fb_helper->client);
56 drm_fb_helper_unprepare(fb_helper);
57 kfree(fb_helper);
58 }
59
60 static const struct fb_ops drm_fbdev_ttm_fb_ops = {
61 .owner = THIS_MODULE,
62 .fb_open = drm_fbdev_ttm_fb_open,
63 .fb_release = drm_fbdev_ttm_fb_release,
64 FB_DEFAULT_DEFERRED_OPS(drm_fbdev_ttm),
65 DRM_FB_HELPER_DEFAULT_OPS,
66 .fb_destroy = drm_fbdev_ttm_fb_destroy,
67 };
68
drm_fbdev_ttm_damage_blit_real(struct drm_fb_helper * fb_helper,struct drm_clip_rect * clip,struct iosys_map * dst)69 static void drm_fbdev_ttm_damage_blit_real(struct drm_fb_helper *fb_helper,
70 struct drm_clip_rect *clip,
71 struct iosys_map *dst)
72 {
73 struct drm_framebuffer *fb = fb_helper->fb;
74 size_t offset = clip->y1 * fb->pitches[0];
75 size_t len = clip->x2 - clip->x1;
76 unsigned int y;
77 void *src;
78
79 switch (drm_format_info_bpp(fb->format, 0)) {
80 case 1:
81 offset += clip->x1 / 8;
82 len = DIV_ROUND_UP(len + clip->x1 % 8, 8);
83 break;
84 case 2:
85 offset += clip->x1 / 4;
86 len = DIV_ROUND_UP(len + clip->x1 % 4, 4);
87 break;
88 case 4:
89 offset += clip->x1 / 2;
90 len = DIV_ROUND_UP(len + clip->x1 % 2, 2);
91 break;
92 default:
93 offset += clip->x1 * fb->format->cpp[0];
94 len *= fb->format->cpp[0];
95 break;
96 }
97
98 src = fb_helper->info->screen_buffer + offset;
99 iosys_map_incr(dst, offset); /* go to first pixel within clip rect */
100
101 for (y = clip->y1; y < clip->y2; y++) {
102 iosys_map_memcpy_to(dst, 0, src, len);
103 iosys_map_incr(dst, fb->pitches[0]);
104 src += fb->pitches[0];
105 }
106 }
107
drm_fbdev_ttm_damage_blit(struct drm_fb_helper * fb_helper,struct drm_clip_rect * clip)108 static int drm_fbdev_ttm_damage_blit(struct drm_fb_helper *fb_helper,
109 struct drm_clip_rect *clip)
110 {
111 struct drm_client_buffer *buffer = fb_helper->buffer;
112 struct iosys_map map, dst;
113 int ret;
114
115 /*
116 * We have to pin the client buffer to its current location while
117 * flushing the shadow buffer. In the general case, concurrent
118 * modesetting operations could try to move the buffer and would
119 * fail. The modeset has to be serialized by acquiring the reservation
120 * object of the underlying BO here.
121 *
122 * For fbdev emulation, we only have to protect against fbdev modeset
123 * operations. Nothing else will involve the client buffer's BO. So it
124 * is sufficient to acquire struct drm_fb_helper.lock here.
125 */
126 mutex_lock(&fb_helper->lock);
127
128 ret = drm_client_buffer_vmap_local(buffer, &map);
129 if (ret)
130 goto out;
131
132 dst = map;
133 drm_fbdev_ttm_damage_blit_real(fb_helper, clip, &dst);
134
135 drm_client_buffer_vunmap_local(buffer);
136
137 out:
138 mutex_unlock(&fb_helper->lock);
139
140 return ret;
141 }
142
drm_fbdev_ttm_helper_fb_dirty(struct drm_fb_helper * helper,struct drm_clip_rect * clip)143 static int drm_fbdev_ttm_helper_fb_dirty(struct drm_fb_helper *helper,
144 struct drm_clip_rect *clip)
145 {
146 struct drm_device *dev = helper->dev;
147 int ret;
148
149 /* Call damage handlers only if necessary */
150 if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2))
151 return 0;
152
153 ret = drm_fbdev_ttm_damage_blit(helper, clip);
154 if (drm_WARN_ONCE(dev, ret, "Damage blitter failed: ret=%d\n", ret))
155 return ret;
156
157 if (helper->fb->funcs->dirty) {
158 ret = helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1);
159 if (drm_WARN_ONCE(dev, ret, "Dirty helper failed: ret=%d\n", ret))
160 return ret;
161 }
162
163 return 0;
164 }
165
166 static const struct drm_fb_helper_funcs drm_fbdev_ttm_helper_funcs = {
167 .fb_dirty = drm_fbdev_ttm_helper_fb_dirty,
168 };
169
170 /*
171 * struct drm_driver
172 */
173
drm_fbdev_ttm_driver_fbdev_probe(struct drm_fb_helper * fb_helper,struct drm_fb_helper_surface_size * sizes)174 int drm_fbdev_ttm_driver_fbdev_probe(struct drm_fb_helper *fb_helper,
175 struct drm_fb_helper_surface_size *sizes)
176 {
177 struct drm_client_dev *client = &fb_helper->client;
178 struct drm_device *dev = fb_helper->dev;
179 struct drm_client_buffer *buffer;
180 struct fb_info *info;
181 size_t screen_size;
182 void *screen_buffer;
183 u32 format;
184 int ret;
185
186 drm_dbg_kms(dev, "surface width(%d), height(%d) and bpp(%d)\n",
187 sizes->surface_width, sizes->surface_height,
188 sizes->surface_bpp);
189
190 format = drm_driver_legacy_fb_format(dev, sizes->surface_bpp,
191 sizes->surface_depth);
192 buffer = drm_client_framebuffer_create(client, sizes->surface_width,
193 sizes->surface_height, format);
194 if (IS_ERR(buffer))
195 return PTR_ERR(buffer);
196
197 fb_helper->funcs = &drm_fbdev_ttm_helper_funcs;
198 fb_helper->buffer = buffer;
199 fb_helper->fb = buffer->fb;
200
201 screen_size = buffer->gem->size;
202 screen_buffer = vzalloc(screen_size);
203 if (!screen_buffer) {
204 ret = -ENOMEM;
205 goto err_drm_client_framebuffer_delete;
206 }
207
208 info = drm_fb_helper_alloc_info(fb_helper);
209 if (IS_ERR(info)) {
210 ret = PTR_ERR(info);
211 goto err_vfree;
212 }
213
214 drm_fb_helper_fill_info(info, fb_helper, sizes);
215
216 info->fbops = &drm_fbdev_ttm_fb_ops;
217
218 /* screen */
219 info->flags |= FBINFO_VIRTFB | FBINFO_READS_FAST;
220 info->screen_buffer = screen_buffer;
221 info->fix.smem_len = screen_size;
222
223 /* deferred I/O */
224 fb_helper->fbdefio.delay = HZ / 20;
225 fb_helper->fbdefio.deferred_io = drm_fb_helper_deferred_io;
226
227 info->fbdefio = &fb_helper->fbdefio;
228 ret = fb_deferred_io_init(info);
229 if (ret)
230 goto err_drm_fb_helper_release_info;
231
232 return 0;
233
234 err_drm_fb_helper_release_info:
235 drm_fb_helper_release_info(fb_helper);
236 err_vfree:
237 vfree(screen_buffer);
238 err_drm_client_framebuffer_delete:
239 fb_helper->fb = NULL;
240 fb_helper->buffer = NULL;
241 drm_client_framebuffer_delete(buffer);
242 return ret;
243 }
244 EXPORT_SYMBOL(drm_fbdev_ttm_driver_fbdev_probe);
245