1 /*
2  * drm gem framebuffer helper functions
3  *
4  * Copyright (C) 2017 Noralf Trønnes
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11 
12 #include <linux/dma-buf.h>
13 #include <linux/dma-fence.h>
14 #include <linux/reservation.h>
15 #include <linux/slab.h>
16 
17 #include <drm/drmP.h>
18 #include <drm/drm_atomic.h>
19 #include <drm/drm_fb_helper.h>
20 #include <drm/drm_fourcc.h>
21 #include <drm/drm_framebuffer.h>
22 #include <drm/drm_gem.h>
23 #include <drm/drm_gem_framebuffer_helper.h>
24 #include <drm/drm_modeset_helper.h>
25 
26 /**
27  * DOC: overview
28  *
29  * This library provides helpers for drivers that don't subclass
30  * &drm_framebuffer and use &drm_gem_object for their backing storage.
31  *
32  * Drivers without additional needs to validate framebuffers can simply use
33  * drm_gem_fb_create() and everything is wired up automatically. Other drivers
34  * can use all parts independently.
35  */
36 
37 /**
38  * drm_gem_fb_get_obj() - Get GEM object backing the framebuffer
39  * @fb: Framebuffer
40  * @plane: Plane index
41  *
42  * No additional reference is taken beyond the one that the &drm_frambuffer
43  * already holds.
44  *
45  * Returns:
46  * Pointer to &drm_gem_object for the given framebuffer and plane index or NULL
47  * if it does not exist.
48  */
49 struct drm_gem_object *drm_gem_fb_get_obj(struct drm_framebuffer *fb,
50 					  unsigned int plane)
51 {
52 	if (plane >= 4)
53 		return NULL;
54 
55 	return fb->obj[plane];
56 }
57 EXPORT_SYMBOL_GPL(drm_gem_fb_get_obj);
58 
59 static struct drm_framebuffer *
60 drm_gem_fb_alloc(struct drm_device *dev,
61 		 const struct drm_mode_fb_cmd2 *mode_cmd,
62 		 struct drm_gem_object **obj, unsigned int num_planes,
63 		 const struct drm_framebuffer_funcs *funcs)
64 {
65 	struct drm_framebuffer *fb;
66 	int ret, i;
67 
68 	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
69 	if (!fb)
70 		return ERR_PTR(-ENOMEM);
71 
72 	drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
73 
74 	for (i = 0; i < num_planes; i++)
75 		fb->obj[i] = obj[i];
76 
77 	ret = drm_framebuffer_init(dev, fb, funcs);
78 	if (ret) {
79 		DRM_DEV_ERROR(dev->dev, "Failed to init framebuffer: %d\n",
80 			      ret);
81 		kfree(fb);
82 		return ERR_PTR(ret);
83 	}
84 
85 	return fb;
86 }
87 
88 /**
89  * drm_gem_fb_destroy - Free GEM backed framebuffer
90  * @fb: Framebuffer
91  *
92  * Frees a GEM backed framebuffer with its backing buffer(s) and the structure
93  * itself. Drivers can use this as their &drm_framebuffer_funcs->destroy
94  * callback.
95  */
96 void drm_gem_fb_destroy(struct drm_framebuffer *fb)
97 {
98 	int i;
99 
100 	for (i = 0; i < 4; i++)
101 		drm_gem_object_put_unlocked(fb->obj[i]);
102 
103 	drm_framebuffer_cleanup(fb);
104 	kfree(fb);
105 }
106 EXPORT_SYMBOL(drm_gem_fb_destroy);
107 
108 /**
109  * drm_gem_fb_create_handle - Create handle for GEM backed framebuffer
110  * @fb: Framebuffer
111  * @file: DRM file to register the handle for
112  * @handle: Pointer to return the created handle
113  *
114  * This function creates a handle for the GEM object backing the framebuffer.
115  * Drivers can use this as their &drm_framebuffer_funcs->create_handle
116  * callback. The GETFB IOCTL calls into this callback.
117  *
118  * Returns:
119  * 0 on success or a negative error code on failure.
120  */
121 int drm_gem_fb_create_handle(struct drm_framebuffer *fb, struct drm_file *file,
122 			     unsigned int *handle)
123 {
124 	return drm_gem_handle_create(file, fb->obj[0], handle);
125 }
126 EXPORT_SYMBOL(drm_gem_fb_create_handle);
127 
128 /**
129  * drm_gem_fb_create_with_funcs() - Helper function for the
130  *                                  &drm_mode_config_funcs.fb_create
131  *                                  callback
132  * @dev: DRM device
133  * @file: DRM file that holds the GEM handle(s) backing the framebuffer
134  * @mode_cmd: Metadata from the userspace framebuffer creation request
135  * @funcs: vtable to be used for the new framebuffer object
136  *
137  * This can be used to set &drm_framebuffer_funcs for drivers that need the
138  * &drm_framebuffer_funcs.dirty callback. Use drm_gem_fb_create() if you don't
139  * need to change &drm_framebuffer_funcs.
140  * The function does buffer size validation.
141  *
142  * Returns:
143  * Pointer to a &drm_framebuffer on success or an error pointer on failure.
144  */
145 struct drm_framebuffer *
146 drm_gem_fb_create_with_funcs(struct drm_device *dev, struct drm_file *file,
147 			     const struct drm_mode_fb_cmd2 *mode_cmd,
148 			     const struct drm_framebuffer_funcs *funcs)
149 {
150 	const struct drm_format_info *info;
151 	struct drm_gem_object *objs[4];
152 	struct drm_framebuffer *fb;
153 	int ret, i;
154 
155 	info = drm_get_format_info(dev, mode_cmd);
156 	if (!info)
157 		return ERR_PTR(-EINVAL);
158 
159 	for (i = 0; i < info->num_planes; i++) {
160 		unsigned int width = mode_cmd->width / (i ? info->hsub : 1);
161 		unsigned int height = mode_cmd->height / (i ? info->vsub : 1);
162 		unsigned int min_size;
163 
164 		objs[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
165 		if (!objs[i]) {
166 			DRM_DEBUG_KMS("Failed to lookup GEM object\n");
167 			ret = -ENOENT;
168 			goto err_gem_object_put;
169 		}
170 
171 		min_size = (height - 1) * mode_cmd->pitches[i]
172 			 + width * info->cpp[i]
173 			 + mode_cmd->offsets[i];
174 
175 		if (objs[i]->size < min_size) {
176 			drm_gem_object_put_unlocked(objs[i]);
177 			ret = -EINVAL;
178 			goto err_gem_object_put;
179 		}
180 	}
181 
182 	fb = drm_gem_fb_alloc(dev, mode_cmd, objs, i, funcs);
183 	if (IS_ERR(fb)) {
184 		ret = PTR_ERR(fb);
185 		goto err_gem_object_put;
186 	}
187 
188 	return fb;
189 
190 err_gem_object_put:
191 	for (i--; i >= 0; i--)
192 		drm_gem_object_put_unlocked(objs[i]);
193 
194 	return ERR_PTR(ret);
195 }
196 EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_funcs);
197 
198 static const struct drm_framebuffer_funcs drm_gem_fb_funcs = {
199 	.destroy	= drm_gem_fb_destroy,
200 	.create_handle	= drm_gem_fb_create_handle,
201 };
202 
203 /**
204  * drm_gem_fb_create() - Helper function for the
205  *                       &drm_mode_config_funcs.fb_create callback
206  * @dev: DRM device
207  * @file: DRM file that holds the GEM handle(s) backing the framebuffer
208  * @mode_cmd: Metadata from the userspace framebuffer creation request
209  *
210  * This function creates a new framebuffer object described by
211  * &drm_mode_fb_cmd2. This description includes handles for the buffer(s)
212  * backing the framebuffer.
213  *
214  * If your hardware has special alignment or pitch requirements these should be
215  * checked before calling this function. The function does buffer size
216  * validation. Use drm_gem_fb_create_with_funcs() if you need to set
217  * &drm_framebuffer_funcs.dirty.
218  *
219  * Drivers can use this as their &drm_mode_config_funcs.fb_create callback.
220  * The ADDFB2 IOCTL calls into this callback.
221  *
222  * Returns:
223  * Pointer to a &drm_framebuffer on success or an error pointer on failure.
224  */
225 struct drm_framebuffer *
226 drm_gem_fb_create(struct drm_device *dev, struct drm_file *file,
227 		  const struct drm_mode_fb_cmd2 *mode_cmd)
228 {
229 	return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
230 					    &drm_gem_fb_funcs);
231 }
232 EXPORT_SYMBOL_GPL(drm_gem_fb_create);
233 
234 /**
235  * drm_gem_fb_prepare_fb() - Prepare a GEM backed framebuffer
236  * @plane: Plane
237  * @state: Plane state the fence will be attached to
238  *
239  * This function prepares a GEM backed framebuffer for scanout by checking if
240  * the plane framebuffer has a DMA-BUF attached. If it does, it extracts the
241  * exclusive fence and attaches it to the plane state for the atomic helper to
242  * wait on. This function can be used as the &drm_plane_helper_funcs.prepare_fb
243  * callback.
244  *
245  * There is no need for &drm_plane_helper_funcs.cleanup_fb hook for simple
246  * gem based framebuffer drivers which have their buffers always pinned in
247  * memory.
248  */
249 int drm_gem_fb_prepare_fb(struct drm_plane *plane,
250 			  struct drm_plane_state *state)
251 {
252 	struct dma_buf *dma_buf;
253 	struct dma_fence *fence;
254 
255 	if (plane->state->fb == state->fb || !state->fb)
256 		return 0;
257 
258 	dma_buf = drm_gem_fb_get_obj(state->fb, 0)->dma_buf;
259 	if (dma_buf) {
260 		fence = reservation_object_get_excl_rcu(dma_buf->resv);
261 		drm_atomic_set_fence_for_plane(state, fence);
262 	}
263 
264 	return 0;
265 }
266 EXPORT_SYMBOL_GPL(drm_gem_fb_prepare_fb);
267 
268 /**
269  * drm_gem_fbdev_fb_create - Create a GEM backed &drm_framebuffer for fbdev
270  *                           emulation
271  * @dev: DRM device
272  * @sizes: fbdev size description
273  * @pitch_align: Optional pitch alignment
274  * @obj: GEM object backing the framebuffer
275  * @funcs: vtable to be used for the new framebuffer object
276  *
277  * This function creates a framebuffer from a &drm_fb_helper_surface_size
278  * description for use in the &drm_fb_helper_funcs.fb_probe callback.
279  *
280  * Returns:
281  * Pointer to a &drm_framebuffer on success or an error pointer on failure.
282  */
283 struct drm_framebuffer *
284 drm_gem_fbdev_fb_create(struct drm_device *dev,
285 			struct drm_fb_helper_surface_size *sizes,
286 			unsigned int pitch_align, struct drm_gem_object *obj,
287 			const struct drm_framebuffer_funcs *funcs)
288 {
289 	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
290 
291 	mode_cmd.width = sizes->surface_width;
292 	mode_cmd.height = sizes->surface_height;
293 	mode_cmd.pitches[0] = sizes->surface_width *
294 			      DIV_ROUND_UP(sizes->surface_bpp, 8);
295 	if (pitch_align)
296 		mode_cmd.pitches[0] = roundup(mode_cmd.pitches[0],
297 					      pitch_align);
298 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
299 							sizes->surface_depth);
300 	if (obj->size < mode_cmd.pitches[0] * mode_cmd.height)
301 		return ERR_PTR(-EINVAL);
302 
303 	return drm_gem_fb_alloc(dev, &mode_cmd, &obj, 1, funcs);
304 }
305 EXPORT_SYMBOL(drm_gem_fbdev_fb_create);
306