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/dma-resv.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 #define AFBC_HEADER_SIZE		16
25 #define AFBC_TH_LAYOUT_ALIGNMENT	8
26 #define AFBC_HDR_ALIGN			64
27 #define AFBC_SUPERBLOCK_PIXELS		256
28 #define AFBC_SUPERBLOCK_ALIGNMENT	128
29 #define AFBC_TH_BODY_START_ALIGNMENT	4096
30 
31 /**
32  * DOC: overview
33  *
34  * This library provides helpers for drivers that don't subclass
35  * &drm_framebuffer and use &drm_gem_object for their backing storage.
36  *
37  * Drivers without additional needs to validate framebuffers can simply use
38  * drm_gem_fb_create() and everything is wired up automatically. Other drivers
39  * can use all parts independently.
40  */
41 
42 /**
43  * drm_gem_fb_get_obj() - Get GEM object backing the framebuffer
44  * @fb: Framebuffer
45  * @plane: Plane index
46  *
47  * No additional reference is taken beyond the one that the &drm_frambuffer
48  * already holds.
49  *
50  * Returns:
51  * Pointer to &drm_gem_object for the given framebuffer and plane index or NULL
52  * if it does not exist.
53  */
54 struct drm_gem_object *drm_gem_fb_get_obj(struct drm_framebuffer *fb,
55 					  unsigned int plane)
56 {
57 	if (plane >= 4)
58 		return NULL;
59 
60 	return fb->obj[plane];
61 }
62 EXPORT_SYMBOL_GPL(drm_gem_fb_get_obj);
63 
64 static int
65 drm_gem_fb_init(struct drm_device *dev,
66 		 struct drm_framebuffer *fb,
67 		 const struct drm_mode_fb_cmd2 *mode_cmd,
68 		 struct drm_gem_object **obj, unsigned int num_planes,
69 		 const struct drm_framebuffer_funcs *funcs)
70 {
71 	int ret, i;
72 
73 	drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
74 
75 	for (i = 0; i < num_planes; i++)
76 		fb->obj[i] = obj[i];
77 
78 	ret = drm_framebuffer_init(dev, fb, funcs);
79 	if (ret)
80 		drm_err(dev, "Failed to init framebuffer: %d\n", ret);
81 
82 	return ret;
83 }
84 
85 /**
86  * drm_gem_fb_destroy - Free GEM backed framebuffer
87  * @fb: Framebuffer
88  *
89  * Frees a GEM backed framebuffer with its backing buffer(s) and the structure
90  * itself. Drivers can use this as their &drm_framebuffer_funcs->destroy
91  * callback.
92  */
93 void drm_gem_fb_destroy(struct drm_framebuffer *fb)
94 {
95 	int i;
96 
97 	for (i = 0; i < 4; i++)
98 		drm_gem_object_put(fb->obj[i]);
99 
100 	drm_framebuffer_cleanup(fb);
101 	kfree(fb);
102 }
103 EXPORT_SYMBOL(drm_gem_fb_destroy);
104 
105 /**
106  * drm_gem_fb_create_handle - Create handle for GEM backed framebuffer
107  * @fb: Framebuffer
108  * @file: DRM file to register the handle for
109  * @handle: Pointer to return the created handle
110  *
111  * This function creates a handle for the GEM object backing the framebuffer.
112  * Drivers can use this as their &drm_framebuffer_funcs->create_handle
113  * callback. The GETFB IOCTL calls into this callback.
114  *
115  * Returns:
116  * 0 on success or a negative error code on failure.
117  */
118 int drm_gem_fb_create_handle(struct drm_framebuffer *fb, struct drm_file *file,
119 			     unsigned int *handle)
120 {
121 	return drm_gem_handle_create(file, fb->obj[0], handle);
122 }
123 EXPORT_SYMBOL(drm_gem_fb_create_handle);
124 
125 /**
126  * drm_gem_fb_init_with_funcs() - Helper function for implementing
127  *				  &drm_mode_config_funcs.fb_create
128  *				  callback in cases when the driver
129  *				  allocates a subclass of
130  *				  struct drm_framebuffer
131  * @dev: DRM device
132  * @fb: framebuffer object
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 function can be used to set &drm_framebuffer_funcs for drivers that need
138  * custom framebuffer callbacks. Use drm_gem_fb_create() if you don't need to
139  * change &drm_framebuffer_funcs. The function does buffer size validation.
140  * The buffer size validation is for a general case, though, so users should
141  * pay attention to the checks being appropriate for them or, at least,
142  * non-conflicting.
143  *
144  * Returns:
145  * Zero or a negative error code.
146  */
147 int drm_gem_fb_init_with_funcs(struct drm_device *dev,
148 			       struct drm_framebuffer *fb,
149 			       struct drm_file *file,
150 			       const struct drm_mode_fb_cmd2 *mode_cmd,
151 			       const struct drm_framebuffer_funcs *funcs)
152 {
153 	const struct drm_format_info *info;
154 	struct drm_gem_object *objs[4];
155 	int ret, i;
156 
157 	info = drm_get_format_info(dev, mode_cmd);
158 	if (!info)
159 		return -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_dbg_kms(dev, "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 			 + drm_format_info_min_pitch(info, i, width)
175 			 + mode_cmd->offsets[i];
176 
177 		if (objs[i]->size < min_size) {
178 			drm_gem_object_put(objs[i]);
179 			ret = -EINVAL;
180 			goto err_gem_object_put;
181 		}
182 	}
183 
184 	ret = drm_gem_fb_init(dev, fb, mode_cmd, objs, i, funcs);
185 	if (ret)
186 		goto err_gem_object_put;
187 
188 	return 0;
189 
190 err_gem_object_put:
191 	for (i--; i >= 0; i--)
192 		drm_gem_object_put(objs[i]);
193 
194 	return ret;
195 }
196 EXPORT_SYMBOL_GPL(drm_gem_fb_init_with_funcs);
197 
198 /**
199  * drm_gem_fb_create_with_funcs() - Helper function for the
200  *                                  &drm_mode_config_funcs.fb_create
201  *                                  callback
202  * @dev: DRM device
203  * @file: DRM file that holds the GEM handle(s) backing the framebuffer
204  * @mode_cmd: Metadata from the userspace framebuffer creation request
205  * @funcs: vtable to be used for the new framebuffer object
206  *
207  * This function can be used to set &drm_framebuffer_funcs for drivers that need
208  * custom framebuffer callbacks. Use drm_gem_fb_create() if you don't need to
209  * change &drm_framebuffer_funcs. The function does buffer size validation.
210  *
211  * Returns:
212  * Pointer to a &drm_framebuffer on success or an error pointer on failure.
213  */
214 struct drm_framebuffer *
215 drm_gem_fb_create_with_funcs(struct drm_device *dev, struct drm_file *file,
216 			     const struct drm_mode_fb_cmd2 *mode_cmd,
217 			     const struct drm_framebuffer_funcs *funcs)
218 {
219 	struct drm_framebuffer *fb;
220 	int ret;
221 
222 	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
223 	if (!fb)
224 		return ERR_PTR(-ENOMEM);
225 
226 	ret = drm_gem_fb_init_with_funcs(dev, fb, file, mode_cmd, funcs);
227 	if (ret) {
228 		kfree(fb);
229 		return ERR_PTR(ret);
230 	}
231 
232 	return fb;
233 }
234 EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_funcs);
235 
236 static const struct drm_framebuffer_funcs drm_gem_fb_funcs = {
237 	.destroy	= drm_gem_fb_destroy,
238 	.create_handle	= drm_gem_fb_create_handle,
239 };
240 
241 /**
242  * drm_gem_fb_create() - Helper function for the
243  *                       &drm_mode_config_funcs.fb_create callback
244  * @dev: DRM device
245  * @file: DRM file that holds the GEM handle(s) backing the framebuffer
246  * @mode_cmd: Metadata from the userspace framebuffer creation request
247  *
248  * This function creates a new framebuffer object described by
249  * &drm_mode_fb_cmd2. This description includes handles for the buffer(s)
250  * backing the framebuffer.
251  *
252  * If your hardware has special alignment or pitch requirements these should be
253  * checked before calling this function. The function does buffer size
254  * validation. Use drm_gem_fb_create_with_dirty() if you need framebuffer
255  * flushing.
256  *
257  * Drivers can use this as their &drm_mode_config_funcs.fb_create callback.
258  * The ADDFB2 IOCTL calls into this callback.
259  *
260  * Returns:
261  * Pointer to a &drm_framebuffer on success or an error pointer on failure.
262  */
263 struct drm_framebuffer *
264 drm_gem_fb_create(struct drm_device *dev, struct drm_file *file,
265 		  const struct drm_mode_fb_cmd2 *mode_cmd)
266 {
267 	return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
268 					    &drm_gem_fb_funcs);
269 }
270 EXPORT_SYMBOL_GPL(drm_gem_fb_create);
271 
272 static const struct drm_framebuffer_funcs drm_gem_fb_funcs_dirtyfb = {
273 	.destroy	= drm_gem_fb_destroy,
274 	.create_handle	= drm_gem_fb_create_handle,
275 	.dirty		= drm_atomic_helper_dirtyfb,
276 };
277 
278 /**
279  * drm_gem_fb_create_with_dirty() - Helper function for the
280  *                       &drm_mode_config_funcs.fb_create callback
281  * @dev: DRM device
282  * @file: DRM file that holds the GEM handle(s) backing the framebuffer
283  * @mode_cmd: Metadata from the userspace framebuffer creation request
284  *
285  * This function creates a new framebuffer object described by
286  * &drm_mode_fb_cmd2. This description includes handles for the buffer(s)
287  * backing the framebuffer. drm_atomic_helper_dirtyfb() is used for the dirty
288  * callback giving framebuffer flushing through the atomic machinery. Use
289  * drm_gem_fb_create() if you don't need the dirty callback.
290  * The function does buffer size validation.
291  *
292  * Drivers should also call drm_plane_enable_fb_damage_clips() on all planes
293  * to enable userspace to use damage clips also with the ATOMIC IOCTL.
294  *
295  * Drivers can use this as their &drm_mode_config_funcs.fb_create callback.
296  * The ADDFB2 IOCTL calls into this callback.
297  *
298  * Returns:
299  * Pointer to a &drm_framebuffer on success or an error pointer on failure.
300  */
301 struct drm_framebuffer *
302 drm_gem_fb_create_with_dirty(struct drm_device *dev, struct drm_file *file,
303 			     const struct drm_mode_fb_cmd2 *mode_cmd)
304 {
305 	return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
306 					    &drm_gem_fb_funcs_dirtyfb);
307 }
308 EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_dirty);
309 
310 static __u32 drm_gem_afbc_get_bpp(struct drm_device *dev,
311 				  const struct drm_mode_fb_cmd2 *mode_cmd)
312 {
313 	const struct drm_format_info *info;
314 
315 	info = drm_get_format_info(dev, mode_cmd);
316 
317 	/* use whatever a driver has set */
318 	if (info->cpp[0])
319 		return info->cpp[0] * 8;
320 
321 	/* guess otherwise */
322 	switch (info->format) {
323 	case DRM_FORMAT_YUV420_8BIT:
324 		return 12;
325 	case DRM_FORMAT_YUV420_10BIT:
326 		return 15;
327 	case DRM_FORMAT_VUY101010:
328 		return 30;
329 	default:
330 		break;
331 	}
332 
333 	/* all attempts failed */
334 	return 0;
335 }
336 
337 static int drm_gem_afbc_min_size(struct drm_device *dev,
338 				 const struct drm_mode_fb_cmd2 *mode_cmd,
339 				 struct drm_afbc_framebuffer *afbc_fb)
340 {
341 	__u32 n_blocks, w_alignment, h_alignment, hdr_alignment;
342 	/* remove bpp when all users properly encode cpp in drm_format_info */
343 	__u32 bpp;
344 
345 	switch (mode_cmd->modifier[0] & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) {
346 	case AFBC_FORMAT_MOD_BLOCK_SIZE_16x16:
347 		afbc_fb->block_width = 16;
348 		afbc_fb->block_height = 16;
349 		break;
350 	case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8:
351 		afbc_fb->block_width = 32;
352 		afbc_fb->block_height = 8;
353 		break;
354 	/* no user exists yet - fall through */
355 	case AFBC_FORMAT_MOD_BLOCK_SIZE_64x4:
356 	case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8_64x4:
357 	default:
358 		drm_dbg_kms(dev, "Invalid AFBC_FORMAT_MOD_BLOCK_SIZE: %lld.\n",
359 			    mode_cmd->modifier[0]
360 			    & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK);
361 		return -EINVAL;
362 	}
363 
364 	/* tiled header afbc */
365 	w_alignment = afbc_fb->block_width;
366 	h_alignment = afbc_fb->block_height;
367 	hdr_alignment = AFBC_HDR_ALIGN;
368 	if (mode_cmd->modifier[0] & AFBC_FORMAT_MOD_TILED) {
369 		w_alignment *= AFBC_TH_LAYOUT_ALIGNMENT;
370 		h_alignment *= AFBC_TH_LAYOUT_ALIGNMENT;
371 		hdr_alignment = AFBC_TH_BODY_START_ALIGNMENT;
372 	}
373 
374 	afbc_fb->aligned_width = ALIGN(mode_cmd->width, w_alignment);
375 	afbc_fb->aligned_height = ALIGN(mode_cmd->height, h_alignment);
376 	afbc_fb->offset = mode_cmd->offsets[0];
377 
378 	bpp = drm_gem_afbc_get_bpp(dev, mode_cmd);
379 	if (!bpp) {
380 		drm_dbg_kms(dev, "Invalid AFBC bpp value: %d\n", bpp);
381 		return -EINVAL;
382 	}
383 
384 	n_blocks = (afbc_fb->aligned_width * afbc_fb->aligned_height)
385 		   / AFBC_SUPERBLOCK_PIXELS;
386 	afbc_fb->afbc_size = ALIGN(n_blocks * AFBC_HEADER_SIZE, hdr_alignment);
387 	afbc_fb->afbc_size += n_blocks * ALIGN(bpp * AFBC_SUPERBLOCK_PIXELS / 8,
388 					       AFBC_SUPERBLOCK_ALIGNMENT);
389 
390 	return 0;
391 }
392 
393 /**
394  * drm_gem_fb_afbc_init() - Helper function for drivers using afbc to
395  *			    fill and validate all the afbc-specific
396  *			    struct drm_afbc_framebuffer members
397  *
398  * @dev: DRM device
399  * @afbc_fb: afbc-specific framebuffer
400  * @mode_cmd: Metadata from the userspace framebuffer creation request
401  * @afbc_fb: afbc framebuffer
402  *
403  * This function can be used by drivers which support afbc to complete
404  * the preparation of struct drm_afbc_framebuffer. It must be called after
405  * allocating the said struct and calling drm_gem_fb_init_with_funcs().
406  * It is caller's responsibility to put afbc_fb->base.obj objects in case
407  * the call is unsuccessful.
408  *
409  * Returns:
410  * Zero on success or a negative error value on failure.
411  */
412 int drm_gem_fb_afbc_init(struct drm_device *dev,
413 			 const struct drm_mode_fb_cmd2 *mode_cmd,
414 			 struct drm_afbc_framebuffer *afbc_fb)
415 {
416 	const struct drm_format_info *info;
417 	struct drm_gem_object **objs;
418 	int ret;
419 
420 	objs = afbc_fb->base.obj;
421 	info = drm_get_format_info(dev, mode_cmd);
422 	if (!info)
423 		return -EINVAL;
424 
425 	ret = drm_gem_afbc_min_size(dev, mode_cmd, afbc_fb);
426 	if (ret < 0)
427 		return ret;
428 
429 	if (objs[0]->size < afbc_fb->afbc_size)
430 		return -EINVAL;
431 
432 	return 0;
433 }
434 EXPORT_SYMBOL_GPL(drm_gem_fb_afbc_init);
435 
436 /**
437  * drm_gem_fb_prepare_fb() - Prepare a GEM backed framebuffer
438  * @plane: Plane
439  * @state: Plane state the fence will be attached to
440  *
441  * This function extracts the exclusive fence from &drm_gem_object.resv and
442  * attaches it to plane state for the atomic helper to wait on. This is
443  * necessary to correctly implement implicit synchronization for any buffers
444  * shared as a struct &dma_buf. This function can be used as the
445  * &drm_plane_helper_funcs.prepare_fb callback.
446  *
447  * There is no need for &drm_plane_helper_funcs.cleanup_fb hook for simple
448  * gem based framebuffer drivers which have their buffers always pinned in
449  * memory.
450  *
451  * See drm_atomic_set_fence_for_plane() for a discussion of implicit and
452  * explicit fencing in atomic modeset updates.
453  */
454 int drm_gem_fb_prepare_fb(struct drm_plane *plane,
455 			  struct drm_plane_state *state)
456 {
457 	struct drm_gem_object *obj;
458 	struct dma_fence *fence;
459 
460 	if (!state->fb)
461 		return 0;
462 
463 	obj = drm_gem_fb_get_obj(state->fb, 0);
464 	fence = dma_resv_get_excl_rcu(obj->resv);
465 	drm_atomic_set_fence_for_plane(state, fence);
466 
467 	return 0;
468 }
469 EXPORT_SYMBOL_GPL(drm_gem_fb_prepare_fb);
470 
471 /**
472  * drm_gem_fb_simple_display_pipe_prepare_fb - prepare_fb helper for
473  *     &drm_simple_display_pipe
474  * @pipe: Simple display pipe
475  * @plane_state: Plane state
476  *
477  * This function uses drm_gem_fb_prepare_fb() to extract the exclusive fence
478  * from &drm_gem_object.resv and attaches it to plane state for the atomic
479  * helper to wait on. This is necessary to correctly implement implicit
480  * synchronization for any buffers shared as a struct &dma_buf. Drivers can use
481  * this as their &drm_simple_display_pipe_funcs.prepare_fb callback.
482  *
483  * See drm_atomic_set_fence_for_plane() for a discussion of implicit and
484  * explicit fencing in atomic modeset updates.
485  */
486 int drm_gem_fb_simple_display_pipe_prepare_fb(struct drm_simple_display_pipe *pipe,
487 					      struct drm_plane_state *plane_state)
488 {
489 	return drm_gem_fb_prepare_fb(&pipe->plane, plane_state);
490 }
491 EXPORT_SYMBOL(drm_gem_fb_simple_display_pipe_prepare_fb);
492