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