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 	if (plane >= ARRAY_SIZE(fb->obj))
57 		return NULL;
58 
59 	return fb->obj[plane];
60 }
61 EXPORT_SYMBOL_GPL(drm_gem_fb_get_obj);
62 
63 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)64 drm_gem_fb_init(struct drm_device *dev,
65 		 struct drm_framebuffer *fb,
66 		 const struct drm_mode_fb_cmd2 *mode_cmd,
67 		 struct drm_gem_object **obj, unsigned int num_planes,
68 		 const struct drm_framebuffer_funcs *funcs)
69 {
70 	unsigned int i;
71 	int ret;
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  */
drm_gem_fb_destroy(struct drm_framebuffer * fb)93 void drm_gem_fb_destroy(struct drm_framebuffer *fb)
94 {
95 	size_t i;
96 
97 	for (i = 0; i < ARRAY_SIZE(fb->obj); 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  */
drm_gem_fb_create_handle(struct drm_framebuffer * fb,struct drm_file * file,unsigned int * handle)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  */
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)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[DRM_FORMAT_MAX_PLANES];
155 	unsigned int i;
156 	int ret;
157 
158 	info = drm_get_format_info(dev, mode_cmd);
159 	if (!info) {
160 		drm_dbg_kms(dev, "Failed to get FB format info\n");
161 		return -EINVAL;
162 	}
163 
164 	for (i = 0; i < info->num_planes; i++) {
165 		unsigned int width = mode_cmd->width / (i ? info->hsub : 1);
166 		unsigned int height = mode_cmd->height / (i ? info->vsub : 1);
167 		unsigned int min_size;
168 
169 		objs[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
170 		if (!objs[i]) {
171 			drm_dbg_kms(dev, "Failed to lookup GEM object\n");
172 			ret = -ENOENT;
173 			goto err_gem_object_put;
174 		}
175 
176 		min_size = (height - 1) * mode_cmd->pitches[i]
177 			 + drm_format_info_min_pitch(info, i, width)
178 			 + mode_cmd->offsets[i];
179 
180 		if (objs[i]->size < min_size) {
181 			drm_dbg_kms(dev,
182 				    "GEM object size (%zu) smaller than minimum size (%u) for plane %d\n",
183 				    objs[i]->size, min_size, i);
184 			drm_gem_object_put(objs[i]);
185 			ret = -EINVAL;
186 			goto err_gem_object_put;
187 		}
188 	}
189 
190 	ret = drm_gem_fb_init(dev, fb, mode_cmd, objs, i, funcs);
191 	if (ret)
192 		goto err_gem_object_put;
193 
194 	return 0;
195 
196 err_gem_object_put:
197 	while (i > 0) {
198 		--i;
199 		drm_gem_object_put(objs[i]);
200 	}
201 	return ret;
202 }
203 EXPORT_SYMBOL_GPL(drm_gem_fb_init_with_funcs);
204 
205 /**
206  * drm_gem_fb_create_with_funcs() - Helper function for the
207  *                                  &drm_mode_config_funcs.fb_create
208  *                                  callback
209  * @dev: DRM device
210  * @file: DRM file that holds the GEM handle(s) backing the framebuffer
211  * @mode_cmd: Metadata from the userspace framebuffer creation request
212  * @funcs: vtable to be used for the new framebuffer object
213  *
214  * This function can be used to set &drm_framebuffer_funcs for drivers that need
215  * custom framebuffer callbacks. Use drm_gem_fb_create() if you don't need to
216  * change &drm_framebuffer_funcs. The function does buffer size validation.
217  *
218  * Returns:
219  * Pointer to a &drm_framebuffer on success or an error pointer on failure.
220  */
221 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)222 drm_gem_fb_create_with_funcs(struct drm_device *dev, struct drm_file *file,
223 			     const struct drm_mode_fb_cmd2 *mode_cmd,
224 			     const struct drm_framebuffer_funcs *funcs)
225 {
226 	struct drm_framebuffer *fb;
227 	int ret;
228 
229 	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
230 	if (!fb)
231 		return ERR_PTR(-ENOMEM);
232 
233 	ret = drm_gem_fb_init_with_funcs(dev, fb, file, mode_cmd, funcs);
234 	if (ret) {
235 		kfree(fb);
236 		return ERR_PTR(ret);
237 	}
238 
239 	return fb;
240 }
241 EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_funcs);
242 
243 static const struct drm_framebuffer_funcs drm_gem_fb_funcs = {
244 	.destroy	= drm_gem_fb_destroy,
245 	.create_handle	= drm_gem_fb_create_handle,
246 };
247 
248 /**
249  * drm_gem_fb_create() - Helper function for the
250  *                       &drm_mode_config_funcs.fb_create callback
251  * @dev: DRM device
252  * @file: DRM file that holds the GEM handle(s) backing the framebuffer
253  * @mode_cmd: Metadata from the userspace framebuffer creation request
254  *
255  * This function creates a new framebuffer object described by
256  * &drm_mode_fb_cmd2. This description includes handles for the buffer(s)
257  * backing the framebuffer.
258  *
259  * If your hardware has special alignment or pitch requirements these should be
260  * checked before calling this function. The function does buffer size
261  * validation. Use drm_gem_fb_create_with_dirty() if you need framebuffer
262  * flushing.
263  *
264  * Drivers can use this as their &drm_mode_config_funcs.fb_create callback.
265  * The ADDFB2 IOCTL calls into this callback.
266  *
267  * Returns:
268  * Pointer to a &drm_framebuffer on success or an error pointer on failure.
269  */
270 struct drm_framebuffer *
drm_gem_fb_create(struct drm_device * dev,struct drm_file * file,const struct drm_mode_fb_cmd2 * mode_cmd)271 drm_gem_fb_create(struct drm_device *dev, struct drm_file *file,
272 		  const struct drm_mode_fb_cmd2 *mode_cmd)
273 {
274 	return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
275 					    &drm_gem_fb_funcs);
276 }
277 EXPORT_SYMBOL_GPL(drm_gem_fb_create);
278 
279 static const struct drm_framebuffer_funcs drm_gem_fb_funcs_dirtyfb = {
280 	.destroy	= drm_gem_fb_destroy,
281 	.create_handle	= drm_gem_fb_create_handle,
282 	.dirty		= drm_atomic_helper_dirtyfb,
283 };
284 
285 /**
286  * drm_gem_fb_create_with_dirty() - Helper function for the
287  *                       &drm_mode_config_funcs.fb_create callback
288  * @dev: DRM device
289  * @file: DRM file that holds the GEM handle(s) backing the framebuffer
290  * @mode_cmd: Metadata from the userspace framebuffer creation request
291  *
292  * This function creates a new framebuffer object described by
293  * &drm_mode_fb_cmd2. This description includes handles for the buffer(s)
294  * backing the framebuffer. drm_atomic_helper_dirtyfb() is used for the dirty
295  * callback giving framebuffer flushing through the atomic machinery. Use
296  * drm_gem_fb_create() if you don't need the dirty callback.
297  * The function does buffer size validation.
298  *
299  * Drivers should also call drm_plane_enable_fb_damage_clips() on all planes
300  * to enable userspace to use damage clips also with the ATOMIC IOCTL.
301  *
302  * Drivers can use this as their &drm_mode_config_funcs.fb_create callback.
303  * The ADDFB2 IOCTL calls into this callback.
304  *
305  * Returns:
306  * Pointer to a &drm_framebuffer on success or an error pointer on failure.
307  */
308 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)309 drm_gem_fb_create_with_dirty(struct drm_device *dev, struct drm_file *file,
310 			     const struct drm_mode_fb_cmd2 *mode_cmd)
311 {
312 	return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
313 					    &drm_gem_fb_funcs_dirtyfb);
314 }
315 EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_dirty);
316 
317 /**
318  * drm_gem_fb_vmap - maps all framebuffer BOs into kernel address space
319  * @fb: the framebuffer
320  * @map: returns the mapping's address for each BO
321  * @data: returns the data address for each BO, can be NULL
322  *
323  * This function maps all buffer objects of the given framebuffer into
324  * kernel address space and stores them in struct iosys_map. If the
325  * mapping operation fails for one of the BOs, the function unmaps the
326  * already established mappings automatically.
327  *
328  * Callers that want to access a BO's stored data should pass @data.
329  * The argument returns the addresses of the data stored in each BO. This
330  * is different from @map if the framebuffer's offsets field is non-zero.
331  *
332  * See drm_gem_fb_vunmap() for unmapping.
333  *
334  * Returns:
335  * 0 on success, or a negative errno code otherwise.
336  */
drm_gem_fb_vmap(struct drm_framebuffer * fb,struct iosys_map map[static DRM_FORMAT_MAX_PLANES],struct iosys_map data[DRM_FORMAT_MAX_PLANES])337 int drm_gem_fb_vmap(struct drm_framebuffer *fb,
338 		    struct iosys_map map[static DRM_FORMAT_MAX_PLANES],
339 		    struct iosys_map data[DRM_FORMAT_MAX_PLANES])
340 {
341 	struct drm_gem_object *obj;
342 	unsigned int i;
343 	int ret;
344 
345 	for (i = 0; i < DRM_FORMAT_MAX_PLANES; ++i) {
346 		obj = drm_gem_fb_get_obj(fb, i);
347 		if (!obj) {
348 			iosys_map_clear(&map[i]);
349 			continue;
350 		}
351 		ret = drm_gem_vmap(obj, &map[i]);
352 		if (ret)
353 			goto err_drm_gem_vunmap;
354 	}
355 
356 	if (data) {
357 		for (i = 0; i < DRM_FORMAT_MAX_PLANES; ++i) {
358 			memcpy(&data[i], &map[i], sizeof(data[i]));
359 			if (iosys_map_is_null(&data[i]))
360 				continue;
361 			iosys_map_incr(&data[i], fb->offsets[i]);
362 		}
363 	}
364 
365 	return 0;
366 
367 err_drm_gem_vunmap:
368 	while (i) {
369 		--i;
370 		obj = drm_gem_fb_get_obj(fb, i);
371 		if (!obj)
372 			continue;
373 		drm_gem_vunmap(obj, &map[i]);
374 	}
375 	return ret;
376 }
377 EXPORT_SYMBOL(drm_gem_fb_vmap);
378 
379 /**
380  * drm_gem_fb_vunmap - unmaps framebuffer BOs from kernel address space
381  * @fb: the framebuffer
382  * @map: mapping addresses as returned by drm_gem_fb_vmap()
383  *
384  * This function unmaps all buffer objects of the given framebuffer.
385  *
386  * See drm_gem_fb_vmap() for more information.
387  */
drm_gem_fb_vunmap(struct drm_framebuffer * fb,struct iosys_map map[static DRM_FORMAT_MAX_PLANES])388 void drm_gem_fb_vunmap(struct drm_framebuffer *fb,
389 		       struct iosys_map map[static DRM_FORMAT_MAX_PLANES])
390 {
391 	unsigned int i = DRM_FORMAT_MAX_PLANES;
392 	struct drm_gem_object *obj;
393 
394 	while (i) {
395 		--i;
396 		obj = drm_gem_fb_get_obj(fb, i);
397 		if (!obj)
398 			continue;
399 		if (iosys_map_is_null(&map[i]))
400 			continue;
401 		drm_gem_vunmap(obj, &map[i]);
402 	}
403 }
404 EXPORT_SYMBOL(drm_gem_fb_vunmap);
405 
406 /**
407  * drm_gem_fb_begin_cpu_access - prepares GEM buffer objects for CPU access
408  * @fb: the framebuffer
409  * @dir: access mode
410  *
411  * Prepares a framebuffer's GEM buffer objects for CPU access. This function
412  * must be called before accessing the BO data within the kernel. For imported
413  * BOs, the function calls dma_buf_begin_cpu_access().
414  *
415  * See drm_gem_fb_end_cpu_access() for signalling the end of CPU access.
416  *
417  * Returns:
418  * 0 on success, or a negative errno code otherwise.
419  */
drm_gem_fb_begin_cpu_access(struct drm_framebuffer * fb,enum dma_data_direction dir)420 int drm_gem_fb_begin_cpu_access(struct drm_framebuffer *fb, enum dma_data_direction dir)
421 {
422 	struct dma_buf_attachment *import_attach;
423 	struct drm_gem_object *obj;
424 	size_t i;
425 	int ret, ret2;
426 
427 	for (i = 0; i < ARRAY_SIZE(fb->obj); ++i) {
428 		obj = drm_gem_fb_get_obj(fb, i);
429 		if (!obj)
430 			continue;
431 		import_attach = obj->import_attach;
432 		if (!import_attach)
433 			continue;
434 		ret = dma_buf_begin_cpu_access(import_attach->dmabuf, dir);
435 		if (ret)
436 			goto err_dma_buf_end_cpu_access;
437 	}
438 
439 	return 0;
440 
441 err_dma_buf_end_cpu_access:
442 	while (i) {
443 		--i;
444 		obj = drm_gem_fb_get_obj(fb, i);
445 		if (!obj)
446 			continue;
447 		import_attach = obj->import_attach;
448 		if (!import_attach)
449 			continue;
450 		ret2 = dma_buf_end_cpu_access(import_attach->dmabuf, dir);
451 		if (ret2) {
452 			drm_err(fb->dev,
453 				"dma_buf_end_cpu_access() failed during error handling: %d\n",
454 				ret2);
455 		}
456 	}
457 
458 	return ret;
459 }
460 EXPORT_SYMBOL(drm_gem_fb_begin_cpu_access);
461 
462 /**
463  * drm_gem_fb_end_cpu_access - signals end of CPU access to GEM buffer objects
464  * @fb: the framebuffer
465  * @dir: access mode
466  *
467  * Signals the end of CPU access to the given framebuffer's GEM buffer objects. This
468  * function must be paired with a corresponding call to drm_gem_fb_begin_cpu_access().
469  * For imported BOs, the function calls dma_buf_end_cpu_access().
470  *
471  * See also drm_gem_fb_begin_cpu_access().
472  */
drm_gem_fb_end_cpu_access(struct drm_framebuffer * fb,enum dma_data_direction dir)473 void drm_gem_fb_end_cpu_access(struct drm_framebuffer *fb, enum dma_data_direction dir)
474 {
475 	size_t i = ARRAY_SIZE(fb->obj);
476 	struct dma_buf_attachment *import_attach;
477 	struct drm_gem_object *obj;
478 	int ret;
479 
480 	while (i) {
481 		--i;
482 		obj = drm_gem_fb_get_obj(fb, i);
483 		if (!obj)
484 			continue;
485 		import_attach = obj->import_attach;
486 		if (!import_attach)
487 			continue;
488 		ret = dma_buf_end_cpu_access(import_attach->dmabuf, dir);
489 		if (ret)
490 			drm_err(fb->dev, "dma_buf_end_cpu_access() failed: %d\n", ret);
491 	}
492 }
493 EXPORT_SYMBOL(drm_gem_fb_end_cpu_access);
494 
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 	/* use whatever a driver has set */
503 	if (info->cpp[0])
504 		return info->cpp[0] * 8;
505 
506 	/* guess otherwise */
507 	switch (info->format) {
508 	case DRM_FORMAT_YUV420_8BIT:
509 		return 12;
510 	case DRM_FORMAT_YUV420_10BIT:
511 		return 15;
512 	case DRM_FORMAT_VUY101010:
513 		return 30;
514 	default:
515 		break;
516 	}
517 
518 	/* all attempts failed */
519 	return 0;
520 }
521 
drm_gem_afbc_min_size(struct drm_device * dev,const struct drm_mode_fb_cmd2 * mode_cmd,struct drm_afbc_framebuffer * afbc_fb)522 static int drm_gem_afbc_min_size(struct drm_device *dev,
523 				 const struct drm_mode_fb_cmd2 *mode_cmd,
524 				 struct drm_afbc_framebuffer *afbc_fb)
525 {
526 	__u32 n_blocks, w_alignment, h_alignment, hdr_alignment;
527 	/* remove bpp when all users properly encode cpp in drm_format_info */
528 	__u32 bpp;
529 
530 	switch (mode_cmd->modifier[0] & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) {
531 	case AFBC_FORMAT_MOD_BLOCK_SIZE_16x16:
532 		afbc_fb->block_width = 16;
533 		afbc_fb->block_height = 16;
534 		break;
535 	case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8:
536 		afbc_fb->block_width = 32;
537 		afbc_fb->block_height = 8;
538 		break;
539 	/* no user exists yet - fall through */
540 	case AFBC_FORMAT_MOD_BLOCK_SIZE_64x4:
541 	case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8_64x4:
542 	default:
543 		drm_dbg_kms(dev, "Invalid AFBC_FORMAT_MOD_BLOCK_SIZE: %lld.\n",
544 			    mode_cmd->modifier[0]
545 			    & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK);
546 		return -EINVAL;
547 	}
548 
549 	/* tiled header afbc */
550 	w_alignment = afbc_fb->block_width;
551 	h_alignment = afbc_fb->block_height;
552 	hdr_alignment = AFBC_HDR_ALIGN;
553 	if (mode_cmd->modifier[0] & AFBC_FORMAT_MOD_TILED) {
554 		w_alignment *= AFBC_TH_LAYOUT_ALIGNMENT;
555 		h_alignment *= AFBC_TH_LAYOUT_ALIGNMENT;
556 		hdr_alignment = AFBC_TH_BODY_START_ALIGNMENT;
557 	}
558 
559 	afbc_fb->aligned_width = ALIGN(mode_cmd->width, w_alignment);
560 	afbc_fb->aligned_height = ALIGN(mode_cmd->height, h_alignment);
561 	afbc_fb->offset = mode_cmd->offsets[0];
562 
563 	bpp = drm_gem_afbc_get_bpp(dev, mode_cmd);
564 	if (!bpp) {
565 		drm_dbg_kms(dev, "Invalid AFBC bpp value: %d\n", bpp);
566 		return -EINVAL;
567 	}
568 
569 	n_blocks = (afbc_fb->aligned_width * afbc_fb->aligned_height)
570 		   / AFBC_SUPERBLOCK_PIXELS;
571 	afbc_fb->afbc_size = ALIGN(n_blocks * AFBC_HEADER_SIZE, hdr_alignment);
572 	afbc_fb->afbc_size += n_blocks * ALIGN(bpp * AFBC_SUPERBLOCK_PIXELS / 8,
573 					       AFBC_SUPERBLOCK_ALIGNMENT);
574 
575 	return 0;
576 }
577 
578 /**
579  * drm_gem_fb_afbc_init() - Helper function for drivers using afbc to
580  *			    fill and validate all the afbc-specific
581  *			    struct drm_afbc_framebuffer members
582  *
583  * @dev: DRM device
584  * @afbc_fb: afbc-specific framebuffer
585  * @mode_cmd: Metadata from the userspace framebuffer creation request
586  * @afbc_fb: afbc framebuffer
587  *
588  * This function can be used by drivers which support afbc to complete
589  * the preparation of struct drm_afbc_framebuffer. It must be called after
590  * allocating the said struct and calling drm_gem_fb_init_with_funcs().
591  * It is caller's responsibility to put afbc_fb->base.obj objects in case
592  * the call is unsuccessful.
593  *
594  * Returns:
595  * Zero on success or a negative error value on failure.
596  */
drm_gem_fb_afbc_init(struct drm_device * dev,const struct drm_mode_fb_cmd2 * mode_cmd,struct drm_afbc_framebuffer * afbc_fb)597 int drm_gem_fb_afbc_init(struct drm_device *dev,
598 			 const struct drm_mode_fb_cmd2 *mode_cmd,
599 			 struct drm_afbc_framebuffer *afbc_fb)
600 {
601 	const struct drm_format_info *info;
602 	struct drm_gem_object **objs;
603 	int ret;
604 
605 	objs = afbc_fb->base.obj;
606 	info = drm_get_format_info(dev, mode_cmd);
607 	if (!info)
608 		return -EINVAL;
609 
610 	ret = drm_gem_afbc_min_size(dev, mode_cmd, afbc_fb);
611 	if (ret < 0)
612 		return ret;
613 
614 	if (objs[0]->size < afbc_fb->afbc_size)
615 		return -EINVAL;
616 
617 	return 0;
618 }
619 EXPORT_SYMBOL_GPL(drm_gem_fb_afbc_init);
620