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