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