xref: /openbmc/linux/drivers/gpu/drm/drm_fbdev_dma.c (revision 37dd6b9f)
1 // SPDX-License-Identifier: MIT
2 
3 #include <linux/fb.h>
4 
5 #include <drm/drm_crtc_helper.h>
6 #include <drm/drm_drv.h>
7 #include <drm/drm_fb_helper.h>
8 #include <drm/drm_framebuffer.h>
9 #include <drm/drm_gem_dma_helper.h>
10 
11 #include <drm/drm_fbdev_dma.h>
12 
13 /*
14  * struct fb_ops
15  */
16 
17 static int drm_fbdev_dma_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 
28 static int drm_fbdev_dma_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 static void drm_fbdev_dma_fb_destroy(struct fb_info *info)
39 {
40 	struct drm_fb_helper *fb_helper = info->par;
41 
42 	if (!fb_helper->dev)
43 		return;
44 
45 	drm_fb_helper_fini(fb_helper);
46 
47 	drm_client_buffer_vunmap(fb_helper->buffer);
48 	drm_client_framebuffer_delete(fb_helper->buffer);
49 	drm_client_release(&fb_helper->client);
50 	drm_fb_helper_unprepare(fb_helper);
51 	kfree(fb_helper);
52 }
53 
54 static int drm_fbdev_dma_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
55 {
56 	struct drm_fb_helper *fb_helper = info->par;
57 	struct drm_device *dev = fb_helper->dev;
58 
59 	if (drm_WARN_ON_ONCE(dev, !fb_helper->dev->driver->gem_prime_mmap))
60 		return -ENODEV;
61 
62 	return fb_helper->dev->driver->gem_prime_mmap(fb_helper->buffer->gem, vma);
63 }
64 
65 static const struct fb_ops drm_fbdev_dma_fb_ops = {
66 	.owner = THIS_MODULE,
67 	.fb_open = drm_fbdev_dma_fb_open,
68 	.fb_release = drm_fbdev_dma_fb_release,
69 	__FB_DEFAULT_SYS_OPS_RDWR,
70 	DRM_FB_HELPER_DEFAULT_OPS,
71 	__FB_DEFAULT_SYS_OPS_DRAW,
72 	.fb_mmap = drm_fbdev_dma_fb_mmap,
73 	.fb_destroy = drm_fbdev_dma_fb_destroy,
74 };
75 
76 /*
77  * struct drm_fb_helper
78  */
79 
80 static int drm_fbdev_dma_helper_fb_probe(struct drm_fb_helper *fb_helper,
81 					 struct drm_fb_helper_surface_size *sizes)
82 {
83 	struct drm_client_dev *client = &fb_helper->client;
84 	struct drm_device *dev = fb_helper->dev;
85 	struct drm_client_buffer *buffer;
86 	struct drm_gem_dma_object *dma_obj;
87 	struct drm_framebuffer *fb;
88 	struct fb_info *info;
89 	u32 format;
90 	struct iosys_map map;
91 	int ret;
92 
93 	drm_dbg_kms(dev, "surface width(%d), height(%d) and bpp(%d)\n",
94 		    sizes->surface_width, sizes->surface_height,
95 		    sizes->surface_bpp);
96 
97 	format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
98 	buffer = drm_client_framebuffer_create(client, sizes->surface_width,
99 					       sizes->surface_height, format);
100 	if (IS_ERR(buffer))
101 		return PTR_ERR(buffer);
102 	dma_obj = to_drm_gem_dma_obj(buffer->gem);
103 
104 	fb = buffer->fb;
105 	if (drm_WARN_ON(dev, fb->funcs->dirty)) {
106 		ret = -ENODEV; /* damage handling not supported; use generic emulation */
107 		goto err_drm_client_buffer_delete;
108 	}
109 
110 	ret = drm_client_buffer_vmap(buffer, &map);
111 	if (ret) {
112 		goto err_drm_client_buffer_delete;
113 	} else if (drm_WARN_ON(dev, map.is_iomem)) {
114 		ret = -ENODEV; /* I/O memory not supported; use generic emulation */
115 		goto err_drm_client_buffer_delete;
116 	}
117 
118 	fb_helper->buffer = buffer;
119 	fb_helper->fb = buffer->fb;
120 
121 	info = drm_fb_helper_alloc_info(fb_helper);
122 	if (IS_ERR(info)) {
123 		ret = PTR_ERR(info);
124 		goto err_drm_client_buffer_vunmap;
125 	}
126 
127 	drm_fb_helper_fill_info(info, fb_helper, sizes);
128 
129 	info->fbops = &drm_fbdev_dma_fb_ops;
130 	info->flags = FBINFO_DEFAULT;
131 
132 	/* screen */
133 	info->flags |= FBINFO_VIRTFB; /* system memory */
134 	if (dma_obj->map_noncoherent)
135 		info->flags |= FBINFO_READS_FAST; /* signal caching */
136 	info->screen_size = sizes->surface_height * fb->pitches[0];
137 	info->screen_buffer = map.vaddr;
138 	info->fix.smem_start = page_to_phys(virt_to_page(info->screen_buffer));
139 	info->fix.smem_len = info->screen_size;
140 
141 	return 0;
142 
143 err_drm_client_buffer_vunmap:
144 	fb_helper->fb = NULL;
145 	fb_helper->buffer = NULL;
146 	drm_client_buffer_vunmap(buffer);
147 err_drm_client_buffer_delete:
148 	drm_client_framebuffer_delete(buffer);
149 	return ret;
150 }
151 
152 static const struct drm_fb_helper_funcs drm_fbdev_dma_helper_funcs = {
153 	.fb_probe = drm_fbdev_dma_helper_fb_probe,
154 };
155 
156 /*
157  * struct drm_client_funcs
158  */
159 
160 static void drm_fbdev_dma_client_unregister(struct drm_client_dev *client)
161 {
162 	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
163 
164 	if (fb_helper->info) {
165 		drm_fb_helper_unregister_info(fb_helper);
166 	} else {
167 		drm_client_release(&fb_helper->client);
168 		drm_fb_helper_unprepare(fb_helper);
169 		kfree(fb_helper);
170 	}
171 }
172 
173 static int drm_fbdev_dma_client_restore(struct drm_client_dev *client)
174 {
175 	drm_fb_helper_lastclose(client->dev);
176 
177 	return 0;
178 }
179 
180 static int drm_fbdev_dma_client_hotplug(struct drm_client_dev *client)
181 {
182 	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
183 	struct drm_device *dev = client->dev;
184 	int ret;
185 
186 	if (dev->fb_helper)
187 		return drm_fb_helper_hotplug_event(dev->fb_helper);
188 
189 	ret = drm_fb_helper_init(dev, fb_helper);
190 	if (ret)
191 		goto err_drm_err;
192 
193 	if (!drm_drv_uses_atomic_modeset(dev))
194 		drm_helper_disable_unused_functions(dev);
195 
196 	ret = drm_fb_helper_initial_config(fb_helper);
197 	if (ret)
198 		goto err_drm_fb_helper_fini;
199 
200 	return 0;
201 
202 err_drm_fb_helper_fini:
203 	drm_fb_helper_fini(fb_helper);
204 err_drm_err:
205 	drm_err(dev, "fbdev-dma: Failed to setup generic emulation (ret=%d)\n", ret);
206 	return ret;
207 }
208 
209 static const struct drm_client_funcs drm_fbdev_dma_client_funcs = {
210 	.owner		= THIS_MODULE,
211 	.unregister	= drm_fbdev_dma_client_unregister,
212 	.restore	= drm_fbdev_dma_client_restore,
213 	.hotplug	= drm_fbdev_dma_client_hotplug,
214 };
215 
216 /**
217  * drm_fbdev_dma_setup() - Setup fbdev emulation for GEM DMA helpers
218  * @dev: DRM device
219  * @preferred_bpp: Preferred bits per pixel for the device.
220  *                 32 is used if this is zero.
221  *
222  * This function sets up fbdev emulation for GEM DMA drivers that support
223  * dumb buffers with a virtual address and that can be mmap'ed.
224  * drm_fbdev_dma_setup() shall be called after the DRM driver registered
225  * the new DRM device with drm_dev_register().
226  *
227  * Restore, hotplug events and teardown are all taken care of. Drivers that do
228  * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves.
229  * Simple drivers might use drm_mode_config_helper_suspend().
230  *
231  * This function is safe to call even when there are no connectors present.
232  * Setup will be retried on the next hotplug event.
233  *
234  * The fbdev is destroyed by drm_dev_unregister().
235  */
236 void drm_fbdev_dma_setup(struct drm_device *dev, unsigned int preferred_bpp)
237 {
238 	struct drm_fb_helper *fb_helper;
239 	int ret;
240 
241 	drm_WARN(dev, !dev->registered, "Device has not been registered.\n");
242 	drm_WARN(dev, dev->fb_helper, "fb_helper is already set!\n");
243 
244 	fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
245 	if (!fb_helper)
246 		return;
247 	drm_fb_helper_prepare(dev, fb_helper, preferred_bpp, &drm_fbdev_dma_helper_funcs);
248 
249 	ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_dma_client_funcs);
250 	if (ret) {
251 		drm_err(dev, "Failed to register client: %d\n", ret);
252 		goto err_drm_client_init;
253 	}
254 
255 	drm_client_register(&fb_helper->client);
256 
257 	return;
258 
259 err_drm_client_init:
260 	drm_fb_helper_unprepare(fb_helper);
261 	kfree(fb_helper);
262 }
263 EXPORT_SYMBOL(drm_fbdev_dma_setup);
264