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