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