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