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