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