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