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/slab.h>
9 #include <linux/module.h>
10 
11 #include <drm/drm_damage_helper.h>
12 #include <drm/drm_fb_helper.h>
13 #include <drm/drm_fourcc.h>
14 #include <drm/drm_framebuffer.h>
15 #include <drm/drm_gem.h>
16 #include <drm/drm_gem_framebuffer_helper.h>
17 #include <drm/drm_modeset_helper.h>
18 
19 #include "drm_internal.h"
20 
21 MODULE_IMPORT_NS(DMA_BUF);
22 
23 #define AFBC_HEADER_SIZE		16
24 #define AFBC_TH_LAYOUT_ALIGNMENT	8
25 #define AFBC_HDR_ALIGN			64
26 #define AFBC_SUPERBLOCK_PIXELS		256
27 #define AFBC_SUPERBLOCK_ALIGNMENT	128
28 #define AFBC_TH_BODY_START_ALIGNMENT	4096
29 
30 /**
31  * DOC: overview
32  *
33  * This library provides helpers for drivers that don't subclass
34  * &drm_framebuffer and use &drm_gem_object for their backing storage.
35  *
36  * Drivers without additional needs to validate framebuffers can simply use
37  * drm_gem_fb_create() and everything is wired up automatically. Other drivers
38  * can use all parts independently.
39  */
40 
41 /**
42  * drm_gem_fb_get_obj() - Get GEM object backing the framebuffer
43  * @fb: Framebuffer
44  * @plane: Plane index
45  *
46  * No additional reference is taken beyond the one that the &drm_frambuffer
47  * already holds.
48  *
49  * Returns:
50  * Pointer to &drm_gem_object for the given framebuffer and plane index or NULL
51  * if it does not exist.
52  */
drm_gem_fb_get_obj(struct drm_framebuffer * fb,unsigned int plane)53 struct drm_gem_object *drm_gem_fb_get_obj(struct drm_framebuffer *fb,
54 					  unsigned int plane)
55 {
56 	struct drm_device *dev = fb->dev;
57 
58 	if (drm_WARN_ON_ONCE(dev, plane >= ARRAY_SIZE(fb->obj)))
59 		return NULL;
60 	else if (drm_WARN_ON_ONCE(dev, !fb->obj[plane]))
61 		return NULL;
62 
63 	return fb->obj[plane];
64 }
65 EXPORT_SYMBOL_GPL(drm_gem_fb_get_obj);
66 
67 static int
drm_gem_fb_init(struct drm_device * dev,struct drm_framebuffer * fb,const struct drm_mode_fb_cmd2 * mode_cmd,struct drm_gem_object ** obj,unsigned int num_planes,const struct drm_framebuffer_funcs * funcs)68 drm_gem_fb_init(struct drm_device *dev,
69 		 struct drm_framebuffer *fb,
70 		 const struct drm_mode_fb_cmd2 *mode_cmd,
71 		 struct drm_gem_object **obj, unsigned int num_planes,
72 		 const struct drm_framebuffer_funcs *funcs)
73 {
74 	unsigned int i;
75 	int ret;
76 
77 	drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
78 
79 	for (i = 0; i < num_planes; i++)
80 		fb->obj[i] = obj[i];
81 
82 	ret = drm_framebuffer_init(dev, fb, funcs);
83 	if (ret)
84 		drm_err(dev, "Failed to init framebuffer: %d\n", ret);
85 
86 	return ret;
87 }
88 
89 /**
90  * drm_gem_fb_destroy - Free GEM backed framebuffer
91  * @fb: Framebuffer
92  *
93  * Frees a GEM backed framebuffer with its backing buffer(s) and the structure
94  * itself. Drivers can use this as their &drm_framebuffer_funcs->destroy
95  * callback.
96  */
drm_gem_fb_destroy(struct drm_framebuffer * fb)97 void drm_gem_fb_destroy(struct drm_framebuffer *fb)
98 {
99 	unsigned int i;
100 
101 	for (i = 0; i < fb->format->num_planes; i++)
102 		drm_gem_object_put(fb->obj[i]);
103 
104 	drm_framebuffer_cleanup(fb);
105 	kfree(fb);
106 }
107 EXPORT_SYMBOL(drm_gem_fb_destroy);
108 
109 /**
110  * drm_gem_fb_create_handle - Create handle for GEM backed framebuffer
111  * @fb: Framebuffer
112  * @file: DRM file to register the handle for
113  * @handle: Pointer to return the created handle
114  *
115  * This function creates a handle for the GEM object backing the framebuffer.
116  * Drivers can use this as their &drm_framebuffer_funcs->create_handle
117  * callback. The GETFB IOCTL calls into this callback.
118  *
119  * Returns:
120  * 0 on success or a negative error code on failure.
121  */
drm_gem_fb_create_handle(struct drm_framebuffer * fb,struct drm_file * file,unsigned int * handle)122 int drm_gem_fb_create_handle(struct drm_framebuffer *fb, struct drm_file *file,
123 			     unsigned int *handle)
124 {
125 	return drm_gem_handle_create(file, fb->obj[0], handle);
126 }
127 EXPORT_SYMBOL(drm_gem_fb_create_handle);
128 
129 /**
130  * drm_gem_fb_init_with_funcs() - Helper function for implementing
131  *				  &drm_mode_config_funcs.fb_create
132  *				  callback in cases when the driver
133  *				  allocates a subclass of
134  *				  struct drm_framebuffer
135  * @dev: DRM device
136  * @fb: framebuffer object
137  * @file: DRM file that holds the GEM handle(s) backing the framebuffer
138  * @mode_cmd: Metadata from the userspace framebuffer creation request
139  * @funcs: vtable to be used for the new framebuffer object
140  *
141  * This function can be used to set &drm_framebuffer_funcs for drivers that need
142  * custom framebuffer callbacks. Use drm_gem_fb_create() if you don't need to
143  * change &drm_framebuffer_funcs. The function does buffer size validation.
144  * The buffer size validation is for a general case, though, so users should
145  * pay attention to the checks being appropriate for them or, at least,
146  * non-conflicting.
147  *
148  * Returns:
149  * Zero or a negative error code.
150  */
drm_gem_fb_init_with_funcs(struct drm_device * dev,struct drm_framebuffer * fb,struct drm_file * file,const struct drm_mode_fb_cmd2 * mode_cmd,const struct drm_framebuffer_funcs * funcs)151 int drm_gem_fb_init_with_funcs(struct drm_device *dev,
152 			       struct drm_framebuffer *fb,
153 			       struct drm_file *file,
154 			       const struct drm_mode_fb_cmd2 *mode_cmd,
155 			       const struct drm_framebuffer_funcs *funcs)
156 {
157 	const struct drm_format_info *info;
158 	struct drm_gem_object *objs[DRM_FORMAT_MAX_PLANES];
159 	unsigned int i;
160 	int ret;
161 
162 	info = drm_get_format_info(dev, mode_cmd);
163 	if (!info) {
164 		drm_dbg_kms(dev, "Failed to get FB format info\n");
165 		return -EINVAL;
166 	}
167 
168 	for (i = 0; i < info->num_planes; i++) {
169 		unsigned int width = mode_cmd->width / (i ? info->hsub : 1);
170 		unsigned int height = mode_cmd->height / (i ? info->vsub : 1);
171 		unsigned int min_size;
172 
173 		objs[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
174 		if (!objs[i]) {
175 			drm_dbg_kms(dev, "Failed to lookup GEM object\n");
176 			ret = -ENOENT;
177 			goto err_gem_object_put;
178 		}
179 
180 		min_size = (height - 1) * mode_cmd->pitches[i]
181 			 + drm_format_info_min_pitch(info, i, width)
182 			 + mode_cmd->offsets[i];
183 
184 		if (objs[i]->size < min_size) {
185 			drm_dbg_kms(dev,
186 				    "GEM object size (%zu) smaller than minimum size (%u) for plane %d\n",
187 				    objs[i]->size, min_size, i);
188 			drm_gem_object_put(objs[i]);
189 			ret = -EINVAL;
190 			goto err_gem_object_put;
191 		}
192 	}
193 
194 	ret = drm_gem_fb_init(dev, fb, mode_cmd, objs, i, funcs);
195 	if (ret)
196 		goto err_gem_object_put;
197 
198 	return 0;
199 
200 err_gem_object_put:
201 	while (i > 0) {
202 		--i;
203 		drm_gem_object_put(objs[i]);
204 	}
205 	return ret;
206 }
207 EXPORT_SYMBOL_GPL(drm_gem_fb_init_with_funcs);
208 
209 /**
210  * drm_gem_fb_create_with_funcs() - Helper function for the
211  *                                  &drm_mode_config_funcs.fb_create
212  *                                  callback
213  * @dev: DRM device
214  * @file: DRM file that holds the GEM handle(s) backing the framebuffer
215  * @mode_cmd: Metadata from the userspace framebuffer creation request
216  * @funcs: vtable to be used for the new framebuffer object
217  *
218  * This function can be used to set &drm_framebuffer_funcs for drivers that need
219  * custom framebuffer callbacks. Use drm_gem_fb_create() if you don't need to
220  * change &drm_framebuffer_funcs. The function does buffer size validation.
221  *
222  * Returns:
223  * Pointer to a &drm_framebuffer on success or an error pointer on failure.
224  */
225 struct drm_framebuffer *
drm_gem_fb_create_with_funcs(struct drm_device * dev,struct drm_file * file,const struct drm_mode_fb_cmd2 * mode_cmd,const struct drm_framebuffer_funcs * funcs)226 drm_gem_fb_create_with_funcs(struct drm_device *dev, struct drm_file *file,
227 			     const struct drm_mode_fb_cmd2 *mode_cmd,
228 			     const struct drm_framebuffer_funcs *funcs)
229 {
230 	struct drm_framebuffer *fb;
231 	int ret;
232 
233 	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
234 	if (!fb)
235 		return ERR_PTR(-ENOMEM);
236 
237 	ret = drm_gem_fb_init_with_funcs(dev, fb, file, mode_cmd, funcs);
238 	if (ret) {
239 		kfree(fb);
240 		return ERR_PTR(ret);
241 	}
242 
243 	return fb;
244 }
245 EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_funcs);
246 
247 static const struct drm_framebuffer_funcs drm_gem_fb_funcs = {
248 	.destroy	= drm_gem_fb_destroy,
249 	.create_handle	= drm_gem_fb_create_handle,
250 };
251 
252 /**
253  * drm_gem_fb_create() - Helper function for the
254  *                       &drm_mode_config_funcs.fb_create callback
255  * @dev: DRM device
256  * @file: DRM file that holds the GEM handle(s) backing the framebuffer
257  * @mode_cmd: Metadata from the userspace framebuffer creation request
258  *
259  * This function creates a new framebuffer object described by
260  * &drm_mode_fb_cmd2. This description includes handles for the buffer(s)
261  * backing the framebuffer.
262  *
263  * If your hardware has special alignment or pitch requirements these should be
264  * checked before calling this function. The function does buffer size
265  * validation. Use drm_gem_fb_create_with_dirty() if you need framebuffer
266  * flushing.
267  *
268  * Drivers can use this as their &drm_mode_config_funcs.fb_create callback.
269  * The ADDFB2 IOCTL calls into this callback.
270  *
271  * Returns:
272  * Pointer to a &drm_framebuffer on success or an error pointer on failure.
273  */
274 struct drm_framebuffer *
drm_gem_fb_create(struct drm_device * dev,struct drm_file * file,const struct drm_mode_fb_cmd2 * mode_cmd)275 drm_gem_fb_create(struct drm_device *dev, struct drm_file *file,
276 		  const struct drm_mode_fb_cmd2 *mode_cmd)
277 {
278 	return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
279 					    &drm_gem_fb_funcs);
280 }
281 EXPORT_SYMBOL_GPL(drm_gem_fb_create);
282 
283 static const struct drm_framebuffer_funcs drm_gem_fb_funcs_dirtyfb = {
284 	.destroy	= drm_gem_fb_destroy,
285 	.create_handle	= drm_gem_fb_create_handle,
286 	.dirty		= drm_atomic_helper_dirtyfb,
287 };
288 
289 /**
290  * drm_gem_fb_create_with_dirty() - Helper function for the
291  *                       &drm_mode_config_funcs.fb_create callback
292  * @dev: DRM device
293  * @file: DRM file that holds the GEM handle(s) backing the framebuffer
294  * @mode_cmd: Metadata from the userspace framebuffer creation request
295  *
296  * This function creates a new framebuffer object described by
297  * &drm_mode_fb_cmd2. This description includes handles for the buffer(s)
298  * backing the framebuffer. drm_atomic_helper_dirtyfb() is used for the dirty
299  * callback giving framebuffer flushing through the atomic machinery. Use
300  * drm_gem_fb_create() if you don't need the dirty callback.
301  * The function does buffer size validation.
302  *
303  * Drivers should also call drm_plane_enable_fb_damage_clips() on all planes
304  * to enable userspace to use damage clips also with the ATOMIC IOCTL.
305  *
306  * Drivers can use this as their &drm_mode_config_funcs.fb_create callback.
307  * The ADDFB2 IOCTL calls into this callback.
308  *
309  * Returns:
310  * Pointer to a &drm_framebuffer on success or an error pointer on failure.
311  */
312 struct drm_framebuffer *
drm_gem_fb_create_with_dirty(struct drm_device * dev,struct drm_file * file,const struct drm_mode_fb_cmd2 * mode_cmd)313 drm_gem_fb_create_with_dirty(struct drm_device *dev, struct drm_file *file,
314 			     const struct drm_mode_fb_cmd2 *mode_cmd)
315 {
316 	return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
317 					    &drm_gem_fb_funcs_dirtyfb);
318 }
319 EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_dirty);
320 
321 /**
322  * drm_gem_fb_vmap - maps all framebuffer BOs into kernel address space
323  * @fb: the framebuffer
324  * @map: returns the mapping's address for each BO
325  * @data: returns the data address for each BO, can be NULL
326  *
327  * This function maps all buffer objects of the given framebuffer into
328  * kernel address space and stores them in struct iosys_map. If the
329  * mapping operation fails for one of the BOs, the function unmaps the
330  * already established mappings automatically.
331  *
332  * Callers that want to access a BO's stored data should pass @data.
333  * The argument returns the addresses of the data stored in each BO. This
334  * is different from @map if the framebuffer's offsets field is non-zero.
335  *
336  * Both, @map and @data, must each refer to arrays with at least
337  * fb->format->num_planes elements.
338  *
339  * See drm_gem_fb_vunmap() for unmapping.
340  *
341  * Returns:
342  * 0 on success, or a negative errno code otherwise.
343  */
drm_gem_fb_vmap(struct drm_framebuffer * fb,struct iosys_map * map,struct iosys_map * data)344 int drm_gem_fb_vmap(struct drm_framebuffer *fb, struct iosys_map *map,
345 		    struct iosys_map *data)
346 {
347 	struct drm_gem_object *obj;
348 	unsigned int i;
349 	int ret;
350 
351 	for (i = 0; i < fb->format->num_planes; ++i) {
352 		obj = drm_gem_fb_get_obj(fb, i);
353 		if (!obj) {
354 			ret = -EINVAL;
355 			goto err_drm_gem_vunmap;
356 		}
357 		ret = drm_gem_vmap(obj, &map[i]);
358 		if (ret)
359 			goto err_drm_gem_vunmap;
360 	}
361 
362 	if (data) {
363 		for (i = 0; i < fb->format->num_planes; ++i) {
364 			memcpy(&data[i], &map[i], sizeof(data[i]));
365 			if (iosys_map_is_null(&data[i]))
366 				continue;
367 			iosys_map_incr(&data[i], fb->offsets[i]);
368 		}
369 	}
370 
371 	return 0;
372 
373 err_drm_gem_vunmap:
374 	while (i) {
375 		--i;
376 		obj = drm_gem_fb_get_obj(fb, i);
377 		if (!obj)
378 			continue;
379 		drm_gem_vunmap(obj, &map[i]);
380 	}
381 	return ret;
382 }
383 EXPORT_SYMBOL(drm_gem_fb_vmap);
384 
385 /**
386  * drm_gem_fb_vunmap - unmaps framebuffer BOs from kernel address space
387  * @fb: the framebuffer
388  * @map: mapping addresses as returned by drm_gem_fb_vmap()
389  *
390  * This function unmaps all buffer objects of the given framebuffer.
391  *
392  * See drm_gem_fb_vmap() for more information.
393  */
drm_gem_fb_vunmap(struct drm_framebuffer * fb,struct iosys_map * map)394 void drm_gem_fb_vunmap(struct drm_framebuffer *fb, struct iosys_map *map)
395 {
396 	unsigned int i = fb->format->num_planes;
397 	struct drm_gem_object *obj;
398 
399 	while (i) {
400 		--i;
401 		obj = drm_gem_fb_get_obj(fb, i);
402 		if (!obj)
403 			continue;
404 		if (iosys_map_is_null(&map[i]))
405 			continue;
406 		drm_gem_vunmap(obj, &map[i]);
407 	}
408 }
409 EXPORT_SYMBOL(drm_gem_fb_vunmap);
410 
__drm_gem_fb_end_cpu_access(struct drm_framebuffer * fb,enum dma_data_direction dir,unsigned int num_planes)411 static void __drm_gem_fb_end_cpu_access(struct drm_framebuffer *fb, enum dma_data_direction dir,
412 					unsigned int num_planes)
413 {
414 	struct dma_buf_attachment *import_attach;
415 	struct drm_gem_object *obj;
416 	int ret;
417 
418 	while (num_planes) {
419 		--num_planes;
420 		obj = drm_gem_fb_get_obj(fb, num_planes);
421 		if (!obj)
422 			continue;
423 		import_attach = obj->import_attach;
424 		if (!import_attach)
425 			continue;
426 		ret = dma_buf_end_cpu_access(import_attach->dmabuf, dir);
427 		if (ret)
428 			drm_err(fb->dev, "dma_buf_end_cpu_access(%u, %d) failed: %d\n",
429 				ret, num_planes, dir);
430 	}
431 }
432 
433 /**
434  * drm_gem_fb_begin_cpu_access - prepares GEM buffer objects for CPU access
435  * @fb: the framebuffer
436  * @dir: access mode
437  *
438  * Prepares a framebuffer's GEM buffer objects for CPU access. This function
439  * must be called before accessing the BO data within the kernel. For imported
440  * BOs, the function calls dma_buf_begin_cpu_access().
441  *
442  * See drm_gem_fb_end_cpu_access() for signalling the end of CPU access.
443  *
444  * Returns:
445  * 0 on success, or a negative errno code otherwise.
446  */
drm_gem_fb_begin_cpu_access(struct drm_framebuffer * fb,enum dma_data_direction dir)447 int drm_gem_fb_begin_cpu_access(struct drm_framebuffer *fb, enum dma_data_direction dir)
448 {
449 	struct dma_buf_attachment *import_attach;
450 	struct drm_gem_object *obj;
451 	unsigned int i;
452 	int ret;
453 
454 	for (i = 0; i < fb->format->num_planes; ++i) {
455 		obj = drm_gem_fb_get_obj(fb, i);
456 		if (!obj) {
457 			ret = -EINVAL;
458 			goto err___drm_gem_fb_end_cpu_access;
459 		}
460 		import_attach = obj->import_attach;
461 		if (!import_attach)
462 			continue;
463 		ret = dma_buf_begin_cpu_access(import_attach->dmabuf, dir);
464 		if (ret)
465 			goto err___drm_gem_fb_end_cpu_access;
466 	}
467 
468 	return 0;
469 
470 err___drm_gem_fb_end_cpu_access:
471 	__drm_gem_fb_end_cpu_access(fb, dir, i);
472 	return ret;
473 }
474 EXPORT_SYMBOL(drm_gem_fb_begin_cpu_access);
475 
476 /**
477  * drm_gem_fb_end_cpu_access - signals end of CPU access to GEM buffer objects
478  * @fb: the framebuffer
479  * @dir: access mode
480  *
481  * Signals the end of CPU access to the given framebuffer's GEM buffer objects. This
482  * function must be paired with a corresponding call to drm_gem_fb_begin_cpu_access().
483  * For imported BOs, the function calls dma_buf_end_cpu_access().
484  *
485  * See also drm_gem_fb_begin_cpu_access().
486  */
drm_gem_fb_end_cpu_access(struct drm_framebuffer * fb,enum dma_data_direction dir)487 void drm_gem_fb_end_cpu_access(struct drm_framebuffer *fb, enum dma_data_direction dir)
488 {
489 	__drm_gem_fb_end_cpu_access(fb, dir, fb->format->num_planes);
490 }
491 EXPORT_SYMBOL(drm_gem_fb_end_cpu_access);
492 
493 // TODO Drop this function and replace by drm_format_info_bpp() once all
494 // DRM_FORMAT_* provide proper block info in drivers/gpu/drm/drm_fourcc.c
drm_gem_afbc_get_bpp(struct drm_device * dev,const struct drm_mode_fb_cmd2 * mode_cmd)495 static __u32 drm_gem_afbc_get_bpp(struct drm_device *dev,
496 				  const struct drm_mode_fb_cmd2 *mode_cmd)
497 {
498 	const struct drm_format_info *info;
499 
500 	info = drm_get_format_info(dev, mode_cmd);
501 
502 	switch (info->format) {
503 	case DRM_FORMAT_YUV420_8BIT:
504 		return 12;
505 	case DRM_FORMAT_YUV420_10BIT:
506 		return 15;
507 	case DRM_FORMAT_VUY101010:
508 		return 30;
509 	default:
510 		return drm_format_info_bpp(info, 0);
511 	}
512 }
513 
drm_gem_afbc_min_size(struct drm_device * dev,const struct drm_mode_fb_cmd2 * mode_cmd,struct drm_afbc_framebuffer * afbc_fb)514 static int drm_gem_afbc_min_size(struct drm_device *dev,
515 				 const struct drm_mode_fb_cmd2 *mode_cmd,
516 				 struct drm_afbc_framebuffer *afbc_fb)
517 {
518 	__u32 n_blocks, w_alignment, h_alignment, hdr_alignment;
519 	/* remove bpp when all users properly encode cpp in drm_format_info */
520 	__u32 bpp;
521 
522 	switch (mode_cmd->modifier[0] & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) {
523 	case AFBC_FORMAT_MOD_BLOCK_SIZE_16x16:
524 		afbc_fb->block_width = 16;
525 		afbc_fb->block_height = 16;
526 		break;
527 	case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8:
528 		afbc_fb->block_width = 32;
529 		afbc_fb->block_height = 8;
530 		break;
531 	/* no user exists yet - fall through */
532 	case AFBC_FORMAT_MOD_BLOCK_SIZE_64x4:
533 	case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8_64x4:
534 	default:
535 		drm_dbg_kms(dev, "Invalid AFBC_FORMAT_MOD_BLOCK_SIZE: %lld.\n",
536 			    mode_cmd->modifier[0]
537 			    & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK);
538 		return -EINVAL;
539 	}
540 
541 	/* tiled header afbc */
542 	w_alignment = afbc_fb->block_width;
543 	h_alignment = afbc_fb->block_height;
544 	hdr_alignment = AFBC_HDR_ALIGN;
545 	if (mode_cmd->modifier[0] & AFBC_FORMAT_MOD_TILED) {
546 		w_alignment *= AFBC_TH_LAYOUT_ALIGNMENT;
547 		h_alignment *= AFBC_TH_LAYOUT_ALIGNMENT;
548 		hdr_alignment = AFBC_TH_BODY_START_ALIGNMENT;
549 	}
550 
551 	afbc_fb->aligned_width = ALIGN(mode_cmd->width, w_alignment);
552 	afbc_fb->aligned_height = ALIGN(mode_cmd->height, h_alignment);
553 	afbc_fb->offset = mode_cmd->offsets[0];
554 
555 	bpp = drm_gem_afbc_get_bpp(dev, mode_cmd);
556 	if (!bpp) {
557 		drm_dbg_kms(dev, "Invalid AFBC bpp value: %d\n", bpp);
558 		return -EINVAL;
559 	}
560 
561 	n_blocks = (afbc_fb->aligned_width * afbc_fb->aligned_height)
562 		   / AFBC_SUPERBLOCK_PIXELS;
563 	afbc_fb->afbc_size = ALIGN(n_blocks * AFBC_HEADER_SIZE, hdr_alignment);
564 	afbc_fb->afbc_size += n_blocks * ALIGN(bpp * AFBC_SUPERBLOCK_PIXELS / 8,
565 					       AFBC_SUPERBLOCK_ALIGNMENT);
566 
567 	return 0;
568 }
569 
570 /**
571  * drm_gem_fb_afbc_init() - Helper function for drivers using afbc to
572  *			    fill and validate all the afbc-specific
573  *			    struct drm_afbc_framebuffer members
574  *
575  * @dev: DRM device
576  * @afbc_fb: afbc-specific framebuffer
577  * @mode_cmd: Metadata from the userspace framebuffer creation request
578  * @afbc_fb: afbc framebuffer
579  *
580  * This function can be used by drivers which support afbc to complete
581  * the preparation of struct drm_afbc_framebuffer. It must be called after
582  * allocating the said struct and calling drm_gem_fb_init_with_funcs().
583  * It is caller's responsibility to put afbc_fb->base.obj objects in case
584  * the call is unsuccessful.
585  *
586  * Returns:
587  * Zero on success or a negative error value on failure.
588  */
drm_gem_fb_afbc_init(struct drm_device * dev,const struct drm_mode_fb_cmd2 * mode_cmd,struct drm_afbc_framebuffer * afbc_fb)589 int drm_gem_fb_afbc_init(struct drm_device *dev,
590 			 const struct drm_mode_fb_cmd2 *mode_cmd,
591 			 struct drm_afbc_framebuffer *afbc_fb)
592 {
593 	const struct drm_format_info *info;
594 	struct drm_gem_object **objs;
595 	int ret;
596 
597 	objs = afbc_fb->base.obj;
598 	info = drm_get_format_info(dev, mode_cmd);
599 	if (!info)
600 		return -EINVAL;
601 
602 	ret = drm_gem_afbc_min_size(dev, mode_cmd, afbc_fb);
603 	if (ret < 0)
604 		return ret;
605 
606 	if (objs[0]->size < afbc_fb->afbc_size)
607 		return -EINVAL;
608 
609 	return 0;
610 }
611 EXPORT_SYMBOL_GPL(drm_gem_fb_afbc_init);
612