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