1 // SPDX-License-Identifier: MIT
2 
3 #include <linux/moduleparam.h>
4 #include <linux/vmalloc.h>
5 
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>
12 
13 #include <drm/drm_fbdev_generic.h>
14 
15 /* @user: 1=userspace, 0=fbcon */
16 static int drm_fbdev_generic_fb_open(struct fb_info *info, int user)
17 {
18 	struct drm_fb_helper *fb_helper = info->par;
19 
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))
22 		return -ENODEV;
23 
24 	return 0;
25 }
26 
27 static int drm_fbdev_generic_fb_release(struct fb_info *info, int user)
28 {
29 	struct drm_fb_helper *fb_helper = info->par;
30 
31 	if (user)
32 		module_put(fb_helper->dev->driver->fops->owner);
33 
34 	return 0;
35 }
36 
37 static void drm_fbdev_generic_fb_destroy(struct fb_info *info)
38 {
39 	struct drm_fb_helper *fb_helper = info->par;
40 	void *shadow = info->screen_buffer;
41 
42 	if (!fb_helper->dev)
43 		return;
44 
45 	fb_deferred_io_cleanup(info);
46 	drm_fb_helper_fini(fb_helper);
47 	vfree(shadow);
48 	drm_client_framebuffer_delete(fb_helper->buffer);
49 
50 	drm_client_release(&fb_helper->client);
51 	drm_fb_helper_unprepare(fb_helper);
52 	kfree(fb_helper);
53 }
54 
55 static const struct fb_ops drm_fbdev_generic_fb_ops = {
56 	.owner		= THIS_MODULE,
57 	.fb_open	= drm_fbdev_generic_fb_open,
58 	.fb_release	= drm_fbdev_generic_fb_release,
59 	.fb_read	= drm_fb_helper_sys_read,
60 	.fb_write	= drm_fb_helper_sys_write,
61 	DRM_FB_HELPER_DEFAULT_OPS,
62 	.fb_fillrect	= drm_fb_helper_sys_fillrect,
63 	.fb_copyarea	= drm_fb_helper_sys_copyarea,
64 	.fb_imageblit	= drm_fb_helper_sys_imageblit,
65 	.fb_mmap	= fb_deferred_io_mmap,
66 	.fb_destroy	= drm_fbdev_generic_fb_destroy,
67 };
68 
69 /*
70  * This function uses the client API to create a framebuffer backed by a dumb buffer.
71  */
72 static int drm_fbdev_generic_helper_fb_probe(struct drm_fb_helper *fb_helper,
73 					     struct drm_fb_helper_surface_size *sizes)
74 {
75 	struct drm_client_dev *client = &fb_helper->client;
76 	struct drm_device *dev = fb_helper->dev;
77 	struct drm_client_buffer *buffer;
78 	struct fb_info *info;
79 	size_t screen_size;
80 	void *screen_buffer;
81 	u32 format;
82 	int ret;
83 
84 	drm_dbg_kms(dev, "surface width(%d), height(%d) and bpp(%d)\n",
85 		    sizes->surface_width, sizes->surface_height,
86 		    sizes->surface_bpp);
87 
88 	format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
89 	buffer = drm_client_framebuffer_create(client, sizes->surface_width,
90 					       sizes->surface_height, format);
91 	if (IS_ERR(buffer))
92 		return PTR_ERR(buffer);
93 
94 	fb_helper->buffer = buffer;
95 	fb_helper->fb = buffer->fb;
96 
97 	screen_size = buffer->gem->size;
98 	screen_buffer = vzalloc(screen_size);
99 	if (!screen_buffer) {
100 		ret = -ENOMEM;
101 		goto err_drm_client_framebuffer_delete;
102 	}
103 
104 	info = drm_fb_helper_alloc_info(fb_helper);
105 	if (IS_ERR(info)) {
106 		ret = PTR_ERR(info);
107 		goto err_vfree;
108 	}
109 
110 	drm_fb_helper_fill_info(info, fb_helper, sizes);
111 
112 	info->fbops = &drm_fbdev_generic_fb_ops;
113 	info->flags = FBINFO_DEFAULT;
114 
115 	/* screen */
116 	info->flags |= FBINFO_VIRTFB | FBINFO_READS_FAST;
117 	info->screen_buffer = screen_buffer;
118 	info->fix.smem_start = page_to_phys(vmalloc_to_page(info->screen_buffer));
119 	info->fix.smem_len = screen_size;
120 
121 	/* deferred I/O */
122 	fb_helper->fbdefio.delay = HZ / 20;
123 	fb_helper->fbdefio.deferred_io = drm_fb_helper_deferred_io;
124 
125 	info->fbdefio = &fb_helper->fbdefio;
126 	ret = fb_deferred_io_init(info);
127 	if (ret)
128 		goto err_drm_fb_helper_release_info;
129 
130 	return 0;
131 
132 err_drm_fb_helper_release_info:
133 	drm_fb_helper_release_info(fb_helper);
134 err_vfree:
135 	vfree(screen_buffer);
136 err_drm_client_framebuffer_delete:
137 	fb_helper->fb = NULL;
138 	fb_helper->buffer = NULL;
139 	drm_client_framebuffer_delete(buffer);
140 	return ret;
141 }
142 
143 static void drm_fbdev_generic_damage_blit_real(struct drm_fb_helper *fb_helper,
144 					       struct drm_clip_rect *clip,
145 					       struct iosys_map *dst)
146 {
147 	struct drm_framebuffer *fb = fb_helper->fb;
148 	size_t offset = clip->y1 * fb->pitches[0];
149 	size_t len = clip->x2 - clip->x1;
150 	unsigned int y;
151 	void *src;
152 
153 	switch (drm_format_info_bpp(fb->format, 0)) {
154 	case 1:
155 		offset += clip->x1 / 8;
156 		len = DIV_ROUND_UP(len + clip->x1 % 8, 8);
157 		break;
158 	case 2:
159 		offset += clip->x1 / 4;
160 		len = DIV_ROUND_UP(len + clip->x1 % 4, 4);
161 		break;
162 	case 4:
163 		offset += clip->x1 / 2;
164 		len = DIV_ROUND_UP(len + clip->x1 % 2, 2);
165 		break;
166 	default:
167 		offset += clip->x1 * fb->format->cpp[0];
168 		len *= fb->format->cpp[0];
169 		break;
170 	}
171 
172 	src = fb_helper->info->screen_buffer + offset;
173 	iosys_map_incr(dst, offset); /* go to first pixel within clip rect */
174 
175 	for (y = clip->y1; y < clip->y2; y++) {
176 		iosys_map_memcpy_to(dst, 0, src, len);
177 		iosys_map_incr(dst, fb->pitches[0]);
178 		src += fb->pitches[0];
179 	}
180 }
181 
182 static int drm_fbdev_generic_damage_blit(struct drm_fb_helper *fb_helper,
183 					 struct drm_clip_rect *clip)
184 {
185 	struct drm_client_buffer *buffer = fb_helper->buffer;
186 	struct iosys_map map, dst;
187 	int ret;
188 
189 	/*
190 	 * We have to pin the client buffer to its current location while
191 	 * flushing the shadow buffer. In the general case, concurrent
192 	 * modesetting operations could try to move the buffer and would
193 	 * fail. The modeset has to be serialized by acquiring the reservation
194 	 * object of the underlying BO here.
195 	 *
196 	 * For fbdev emulation, we only have to protect against fbdev modeset
197 	 * operations. Nothing else will involve the client buffer's BO. So it
198 	 * is sufficient to acquire struct drm_fb_helper.lock here.
199 	 */
200 	mutex_lock(&fb_helper->lock);
201 
202 	ret = drm_client_buffer_vmap(buffer, &map);
203 	if (ret)
204 		goto out;
205 
206 	dst = map;
207 	drm_fbdev_generic_damage_blit_real(fb_helper, clip, &dst);
208 
209 	drm_client_buffer_vunmap(buffer);
210 
211 out:
212 	mutex_unlock(&fb_helper->lock);
213 
214 	return ret;
215 }
216 
217 static int drm_fbdev_generic_helper_fb_dirty(struct drm_fb_helper *helper,
218 					     struct drm_clip_rect *clip)
219 {
220 	struct drm_device *dev = helper->dev;
221 	int ret;
222 
223 	/* Call damage handlers only if necessary */
224 	if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2))
225 		return 0;
226 
227 	ret = drm_fbdev_generic_damage_blit(helper, clip);
228 	if (drm_WARN_ONCE(dev, ret, "Damage blitter failed: ret=%d\n", ret))
229 		return ret;
230 
231 	if (helper->fb->funcs->dirty) {
232 		ret = helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1);
233 		if (drm_WARN_ONCE(dev, ret, "Dirty helper failed: ret=%d\n", ret))
234 			return ret;
235 	}
236 
237 	return 0;
238 }
239 
240 static const struct drm_fb_helper_funcs drm_fbdev_generic_helper_funcs = {
241 	.fb_probe = drm_fbdev_generic_helper_fb_probe,
242 	.fb_dirty = drm_fbdev_generic_helper_fb_dirty,
243 };
244 
245 static void drm_fbdev_generic_client_unregister(struct drm_client_dev *client)
246 {
247 	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
248 
249 	if (fb_helper->info) {
250 		drm_fb_helper_unregister_info(fb_helper);
251 	} else {
252 		drm_client_release(&fb_helper->client);
253 		drm_fb_helper_unprepare(fb_helper);
254 		kfree(fb_helper);
255 	}
256 }
257 
258 static int drm_fbdev_generic_client_restore(struct drm_client_dev *client)
259 {
260 	drm_fb_helper_lastclose(client->dev);
261 
262 	return 0;
263 }
264 
265 static int drm_fbdev_generic_client_hotplug(struct drm_client_dev *client)
266 {
267 	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
268 	struct drm_device *dev = client->dev;
269 	int ret;
270 
271 	if (dev->fb_helper)
272 		return drm_fb_helper_hotplug_event(dev->fb_helper);
273 
274 	ret = drm_fb_helper_init(dev, fb_helper);
275 	if (ret)
276 		goto err_drm_err;
277 
278 	if (!drm_drv_uses_atomic_modeset(dev))
279 		drm_helper_disable_unused_functions(dev);
280 
281 	ret = drm_fb_helper_initial_config(fb_helper);
282 	if (ret)
283 		goto err_drm_fb_helper_fini;
284 
285 	return 0;
286 
287 err_drm_fb_helper_fini:
288 	drm_fb_helper_fini(fb_helper);
289 err_drm_err:
290 	drm_err(dev, "fbdev: Failed to setup generic emulation (ret=%d)\n", ret);
291 	return ret;
292 }
293 
294 static const struct drm_client_funcs drm_fbdev_generic_client_funcs = {
295 	.owner		= THIS_MODULE,
296 	.unregister	= drm_fbdev_generic_client_unregister,
297 	.restore	= drm_fbdev_generic_client_restore,
298 	.hotplug	= drm_fbdev_generic_client_hotplug,
299 };
300 
301 /**
302  * drm_fbdev_generic_setup() - Setup generic fbdev emulation
303  * @dev: DRM device
304  * @preferred_bpp: Preferred bits per pixel for the device.
305  *
306  * This function sets up generic fbdev emulation for drivers that supports
307  * dumb buffers with a virtual address and that can be mmap'ed.
308  * drm_fbdev_generic_setup() shall be called after the DRM driver registered
309  * the new DRM device with drm_dev_register().
310  *
311  * Restore, hotplug events and teardown are all taken care of. Drivers that do
312  * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves.
313  * Simple drivers might use drm_mode_config_helper_suspend().
314  *
315  * In order to provide fixed mmap-able memory ranges, generic fbdev emulation
316  * uses a shadow buffer in system memory. The implementation blits the shadow
317  * fbdev buffer onto the real buffer in regular intervals.
318  *
319  * This function is safe to call even when there are no connectors present.
320  * Setup will be retried on the next hotplug event.
321  *
322  * The fbdev is destroyed by drm_dev_unregister().
323  */
324 void drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp)
325 {
326 	struct drm_fb_helper *fb_helper;
327 	int ret;
328 
329 	drm_WARN(dev, !dev->registered, "Device has not been registered.\n");
330 	drm_WARN(dev, dev->fb_helper, "fb_helper is already set!\n");
331 
332 	fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
333 	if (!fb_helper)
334 		return;
335 	drm_fb_helper_prepare(dev, fb_helper, preferred_bpp, &drm_fbdev_generic_helper_funcs);
336 
337 	ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_generic_client_funcs);
338 	if (ret) {
339 		drm_err(dev, "Failed to register client: %d\n", ret);
340 		goto err_drm_client_init;
341 	}
342 
343 	ret = drm_fbdev_generic_client_hotplug(&fb_helper->client);
344 	if (ret)
345 		drm_dbg_kms(dev, "client hotplug ret=%d\n", ret);
346 
347 	drm_client_register(&fb_helper->client);
348 
349 	return;
350 
351 err_drm_client_init:
352 	drm_fb_helper_unprepare(fb_helper);
353 	kfree(fb_helper);
354 	return;
355 }
356 EXPORT_SYMBOL(drm_fbdev_generic_setup);
357