2019-05-27 06:55:01 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-08-13 13:31:44 +00:00
|
|
|
/*
|
|
|
|
* drm gem framebuffer helper functions
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Noralf Trønnes
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/slab.h>
|
2021-10-10 12:46:28 +00:00
|
|
|
#include <linux/module.h>
|
2017-08-13 13:31:44 +00:00
|
|
|
|
2019-01-15 04:36:39 +00:00
|
|
|
#include <drm/drm_damage_helper.h>
|
2017-08-13 13:31:44 +00:00
|
|
|
#include <drm/drm_fb_helper.h>
|
|
|
|
#include <drm/drm_fourcc.h>
|
|
|
|
#include <drm/drm_framebuffer.h>
|
|
|
|
#include <drm/drm_gem.h>
|
|
|
|
#include <drm/drm_gem_framebuffer_helper.h>
|
|
|
|
#include <drm/drm_modeset_helper.h>
|
|
|
|
|
2021-07-30 18:35:08 +00:00
|
|
|
#include "drm_internal.h"
|
|
|
|
|
2021-10-10 12:46:28 +00:00
|
|
|
MODULE_IMPORT_NS(DMA_BUF);
|
|
|
|
|
2020-03-11 14:55:37 +00:00
|
|
|
#define AFBC_HEADER_SIZE 16
|
|
|
|
#define AFBC_TH_LAYOUT_ALIGNMENT 8
|
|
|
|
#define AFBC_HDR_ALIGN 64
|
|
|
|
#define AFBC_SUPERBLOCK_PIXELS 256
|
|
|
|
#define AFBC_SUPERBLOCK_ALIGNMENT 128
|
|
|
|
#define AFBC_TH_BODY_START_ALIGNMENT 4096
|
|
|
|
|
2017-08-13 13:31:44 +00:00
|
|
|
/**
|
|
|
|
* DOC: overview
|
|
|
|
*
|
|
|
|
* This library provides helpers for drivers that don't subclass
|
2017-09-22 15:47:44 +00:00
|
|
|
* &drm_framebuffer and use &drm_gem_object for their backing storage.
|
2017-08-13 13:31:44 +00:00
|
|
|
*
|
|
|
|
* Drivers without additional needs to validate framebuffers can simply use
|
2017-09-22 15:47:44 +00:00
|
|
|
* drm_gem_fb_create() and everything is wired up automatically. Other drivers
|
|
|
|
* can use all parts independently.
|
2017-08-13 13:31:44 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-09-22 15:47:44 +00:00
|
|
|
* drm_gem_fb_get_obj() - Get GEM object backing the framebuffer
|
|
|
|
* @fb: Framebuffer
|
|
|
|
* @plane: Plane index
|
2017-08-13 13:31:44 +00:00
|
|
|
*
|
2017-09-22 15:47:44 +00:00
|
|
|
* No additional reference is taken beyond the one that the &drm_frambuffer
|
|
|
|
* already holds.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* Pointer to &drm_gem_object for the given framebuffer and plane index or NULL
|
|
|
|
* if it does not exist.
|
2017-08-13 13:31:44 +00:00
|
|
|
*/
|
|
|
|
struct drm_gem_object *drm_gem_fb_get_obj(struct drm_framebuffer *fb,
|
|
|
|
unsigned int plane)
|
|
|
|
{
|
2021-07-30 18:35:07 +00:00
|
|
|
if (plane >= ARRAY_SIZE(fb->obj))
|
2017-08-13 13:31:44 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return fb->obj[plane];
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(drm_gem_fb_get_obj);
|
|
|
|
|
2020-03-11 14:55:36 +00:00
|
|
|
static int
|
|
|
|
drm_gem_fb_init(struct drm_device *dev,
|
|
|
|
struct drm_framebuffer *fb,
|
2017-08-13 13:31:44 +00:00
|
|
|
const struct drm_mode_fb_cmd2 *mode_cmd,
|
|
|
|
struct drm_gem_object **obj, unsigned int num_planes,
|
|
|
|
const struct drm_framebuffer_funcs *funcs)
|
|
|
|
{
|
2021-07-30 18:35:07 +00:00
|
|
|
unsigned int i;
|
|
|
|
int ret;
|
2017-08-13 13:31:44 +00:00
|
|
|
|
|
|
|
drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
|
|
|
|
|
|
|
|
for (i = 0; i < num_planes; i++)
|
|
|
|
fb->obj[i] = obj[i];
|
|
|
|
|
|
|
|
ret = drm_framebuffer_init(dev, fb, funcs);
|
2020-04-15 17:20:24 +00:00
|
|
|
if (ret)
|
2019-12-10 12:30:46 +00:00
|
|
|
drm_err(dev, "Failed to init framebuffer: %d\n", ret);
|
2017-08-13 13:31:44 +00:00
|
|
|
|
2020-03-11 14:55:36 +00:00
|
|
|
return ret;
|
2017-08-13 13:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* drm_gem_fb_destroy - Free GEM backed framebuffer
|
2017-09-22 15:47:44 +00:00
|
|
|
* @fb: Framebuffer
|
2017-08-13 13:31:44 +00:00
|
|
|
*
|
|
|
|
* Frees a GEM backed framebuffer with its backing buffer(s) and the structure
|
|
|
|
* itself. Drivers can use this as their &drm_framebuffer_funcs->destroy
|
|
|
|
* callback.
|
|
|
|
*/
|
|
|
|
void drm_gem_fb_destroy(struct drm_framebuffer *fb)
|
|
|
|
{
|
2021-07-30 18:35:07 +00:00
|
|
|
size_t i;
|
2017-08-13 13:31:44 +00:00
|
|
|
|
2021-07-30 18:35:07 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE(fb->obj); i++)
|
2020-05-15 09:50:53 +00:00
|
|
|
drm_gem_object_put(fb->obj[i]);
|
2017-08-13 13:31:44 +00:00
|
|
|
|
|
|
|
drm_framebuffer_cleanup(fb);
|
|
|
|
kfree(fb);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(drm_gem_fb_destroy);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* drm_gem_fb_create_handle - Create handle for GEM backed framebuffer
|
2017-09-22 15:47:44 +00:00
|
|
|
* @fb: Framebuffer
|
|
|
|
* @file: DRM file to register the handle for
|
|
|
|
* @handle: Pointer to return the created handle
|
2017-08-13 13:31:44 +00:00
|
|
|
*
|
2017-09-22 15:47:44 +00:00
|
|
|
* This function creates a handle for the GEM object backing the framebuffer.
|
2017-08-13 13:31:44 +00:00
|
|
|
* Drivers can use this as their &drm_framebuffer_funcs->create_handle
|
2017-09-22 15:47:44 +00:00
|
|
|
* callback. The GETFB IOCTL calls into this callback.
|
2017-08-13 13:31:44 +00:00
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* 0 on success or a negative error code on failure.
|
|
|
|
*/
|
|
|
|
int drm_gem_fb_create_handle(struct drm_framebuffer *fb, struct drm_file *file,
|
|
|
|
unsigned int *handle)
|
|
|
|
{
|
|
|
|
return drm_gem_handle_create(file, fb->obj[0], handle);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(drm_gem_fb_create_handle);
|
|
|
|
|
|
|
|
/**
|
2020-03-11 14:55:36 +00:00
|
|
|
* drm_gem_fb_init_with_funcs() - Helper function for implementing
|
|
|
|
* &drm_mode_config_funcs.fb_create
|
|
|
|
* callback in cases when the driver
|
|
|
|
* allocates a subclass of
|
|
|
|
* struct drm_framebuffer
|
2017-08-13 13:31:44 +00:00
|
|
|
* @dev: DRM device
|
2020-03-11 14:55:36 +00:00
|
|
|
* @fb: framebuffer object
|
2017-09-22 15:47:44 +00:00
|
|
|
* @file: DRM file that holds the GEM handle(s) backing the framebuffer
|
|
|
|
* @mode_cmd: Metadata from the userspace framebuffer creation request
|
2017-08-13 13:31:44 +00:00
|
|
|
* @funcs: vtable to be used for the new framebuffer object
|
|
|
|
*
|
2019-01-15 04:36:39 +00:00
|
|
|
* This function can be used to set &drm_framebuffer_funcs for drivers that need
|
|
|
|
* custom framebuffer callbacks. Use drm_gem_fb_create() if you don't need to
|
|
|
|
* change &drm_framebuffer_funcs. The function does buffer size validation.
|
2020-03-11 14:55:36 +00:00
|
|
|
* The buffer size validation is for a general case, though, so users should
|
|
|
|
* pay attention to the checks being appropriate for them or, at least,
|
|
|
|
* non-conflicting.
|
2017-09-22 15:47:44 +00:00
|
|
|
*
|
|
|
|
* Returns:
|
2020-03-11 14:55:36 +00:00
|
|
|
* Zero or a negative error code.
|
2017-08-13 13:31:44 +00:00
|
|
|
*/
|
2020-03-11 14:55:36 +00:00
|
|
|
int 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)
|
2017-08-13 13:31:44 +00:00
|
|
|
{
|
|
|
|
const struct drm_format_info *info;
|
2021-07-30 18:35:07 +00:00
|
|
|
struct drm_gem_object *objs[DRM_FORMAT_MAX_PLANES];
|
|
|
|
unsigned int i;
|
|
|
|
int ret;
|
2017-08-13 13:31:44 +00:00
|
|
|
|
|
|
|
info = drm_get_format_info(dev, mode_cmd);
|
2021-05-03 11:39:39 +00:00
|
|
|
if (!info) {
|
|
|
|
drm_dbg_kms(dev, "Failed to get FB format info\n");
|
2020-03-11 14:55:36 +00:00
|
|
|
return -EINVAL;
|
2021-05-03 11:39:39 +00:00
|
|
|
}
|
2017-08-13 13:31:44 +00:00
|
|
|
|
|
|
|
for (i = 0; i < info->num_planes; i++) {
|
|
|
|
unsigned int width = mode_cmd->width / (i ? info->hsub : 1);
|
|
|
|
unsigned int height = mode_cmd->height / (i ? info->vsub : 1);
|
|
|
|
unsigned int min_size;
|
|
|
|
|
|
|
|
objs[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
|
|
|
|
if (!objs[i]) {
|
2019-12-10 12:30:46 +00:00
|
|
|
drm_dbg_kms(dev, "Failed to lookup GEM object\n");
|
2017-08-13 13:31:44 +00:00
|
|
|
ret = -ENOENT;
|
|
|
|
goto err_gem_object_put;
|
|
|
|
}
|
|
|
|
|
|
|
|
min_size = (height - 1) * mode_cmd->pitches[i]
|
drm/fourcc: Add char_per_block, block_w and block_h in drm_format_info
For some pixel formats .cpp structure in drm_format info it's not
enough to describe the peculiarities of the pixel layout, for example
tiled formats or packed formats at bit level.
What's implemented here is to add three new members to drm_format_info
that could describe such formats:
- char_per_block[3]
- block_w[3]
- block_h[3]
char_per_block will be put in a union alongside cpp, for transparent
compatibility with the existing format descriptions.
Regarding, block_w and block_h they are intended to be used through
their equivalent getters drm_format_info_block_width /
drm_format_info_block_height, the reason of the getters is to abstract
the fact that for normal formats block_w and block_h will be unset/0,
but the methods will be returning 1.
Additionally, convenience function drm_format_info_min_pitch had been
added that computes the minimum required pitch for a given pixel
format and buffer width.
Using that the following drm core functions had been updated to
generically handle both block and non-block formats:
- drm_fb_cma_get_gem_addr: for block formats it will just return the
beginning of the block.
- framebuffer_check: Use the newly added drm_format_info_min_pitch.
- drm_gem_fb_create_with_funcs: Use the newly added
drm_format_info_min_pitch.
- In places where is not expecting to handle block formats, like fbdev
helpers I just added some warnings in case the block width/height
are greater than 1.
Changes since v3:
- Add helper function for computing the minimum required pitch.
- Improve/cleanup documentation
Changes since v8:
- Fixed build on 32bits arm architectures, with:
- return DIV_ROUND_UP((u64)buffer_width * info->char_per_block[plane],
+ return DIV_ROUND_UP_ULL((u64)buffer_width * info->char_per_block[plane],
Reviewed-by: Brian Starkey <brian.starkey@arm.com>
Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Signed-off-by: Alexandru Gheorghe <alexandru-cosmin.gheorghe@arm.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20181101170055.5433-1-alexandru-cosmin.gheorghe@arm.com
2018-11-01 17:02:05 +00:00
|
|
|
+ drm_format_info_min_pitch(info, i, width)
|
2017-08-13 13:31:44 +00:00
|
|
|
+ mode_cmd->offsets[i];
|
|
|
|
|
|
|
|
if (objs[i]->size < min_size) {
|
2021-05-03 11:39:39 +00:00
|
|
|
drm_dbg_kms(dev,
|
|
|
|
"GEM object size (%zu) smaller than minimum size (%u) for plane %d\n",
|
|
|
|
objs[i]->size, min_size, i);
|
2020-05-15 09:50:53 +00:00
|
|
|
drm_gem_object_put(objs[i]);
|
2017-08-13 13:31:44 +00:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto err_gem_object_put;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-11 14:55:36 +00:00
|
|
|
ret = drm_gem_fb_init(dev, fb, mode_cmd, objs, i, funcs);
|
|
|
|
if (ret)
|
2017-08-13 13:31:44 +00:00
|
|
|
goto err_gem_object_put;
|
|
|
|
|
2020-03-11 14:55:36 +00:00
|
|
|
return 0;
|
2017-08-13 13:31:44 +00:00
|
|
|
|
|
|
|
err_gem_object_put:
|
2021-07-30 18:35:07 +00:00
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
2020-05-15 09:50:53 +00:00
|
|
|
drm_gem_object_put(objs[i]);
|
2021-07-30 18:35:07 +00:00
|
|
|
}
|
2020-03-11 14:55:36 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(drm_gem_fb_init_with_funcs);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* drm_gem_fb_create_with_funcs() - Helper function for the
|
|
|
|
* &drm_mode_config_funcs.fb_create
|
|
|
|
* callback
|
|
|
|
* @dev: DRM device
|
|
|
|
* @file: DRM file that holds the GEM handle(s) backing the framebuffer
|
|
|
|
* @mode_cmd: Metadata from the userspace framebuffer creation request
|
|
|
|
* @funcs: vtable to be used for the new framebuffer object
|
|
|
|
*
|
|
|
|
* This function can be used to set &drm_framebuffer_funcs for drivers that need
|
|
|
|
* custom framebuffer callbacks. Use drm_gem_fb_create() if you don't need to
|
|
|
|
* change &drm_framebuffer_funcs. The function does buffer size validation.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* Pointer to a &drm_framebuffer on success or an error pointer on failure.
|
|
|
|
*/
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
struct drm_framebuffer *fb;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
fb = kzalloc(sizeof(*fb), GFP_KERNEL);
|
|
|
|
if (!fb)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
ret = drm_gem_fb_init_with_funcs(dev, fb, file, mode_cmd, funcs);
|
|
|
|
if (ret) {
|
|
|
|
kfree(fb);
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fb;
|
2017-08-13 13:31:44 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_funcs);
|
|
|
|
|
|
|
|
static const struct drm_framebuffer_funcs drm_gem_fb_funcs = {
|
|
|
|
.destroy = drm_gem_fb_destroy,
|
|
|
|
.create_handle = drm_gem_fb_create_handle,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-09-22 15:47:44 +00:00
|
|
|
* drm_gem_fb_create() - Helper function for the
|
|
|
|
* &drm_mode_config_funcs.fb_create callback
|
2017-08-13 13:31:44 +00:00
|
|
|
* @dev: DRM device
|
2017-09-22 15:47:44 +00:00
|
|
|
* @file: DRM file that holds the GEM handle(s) backing the framebuffer
|
|
|
|
* @mode_cmd: Metadata from the userspace framebuffer creation request
|
|
|
|
*
|
|
|
|
* This function creates a new framebuffer object described by
|
|
|
|
* &drm_mode_fb_cmd2. This description includes handles for the buffer(s)
|
|
|
|
* backing the framebuffer.
|
2017-08-13 13:31:44 +00:00
|
|
|
*
|
|
|
|
* If your hardware has special alignment or pitch requirements these should be
|
|
|
|
* checked before calling this function. The function does buffer size
|
2019-01-15 04:36:39 +00:00
|
|
|
* validation. Use drm_gem_fb_create_with_dirty() if you need framebuffer
|
|
|
|
* flushing.
|
2017-09-22 15:47:44 +00:00
|
|
|
*
|
|
|
|
* Drivers can use this as their &drm_mode_config_funcs.fb_create callback.
|
|
|
|
* The ADDFB2 IOCTL calls into this callback.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* Pointer to a &drm_framebuffer on success or an error pointer on failure.
|
2017-08-13 13:31:44 +00:00
|
|
|
*/
|
|
|
|
struct drm_framebuffer *
|
|
|
|
drm_gem_fb_create(struct drm_device *dev, struct drm_file *file,
|
|
|
|
const struct drm_mode_fb_cmd2 *mode_cmd)
|
|
|
|
{
|
|
|
|
return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
|
|
|
|
&drm_gem_fb_funcs);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(drm_gem_fb_create);
|
|
|
|
|
2019-01-15 04:36:39 +00:00
|
|
|
static const struct drm_framebuffer_funcs drm_gem_fb_funcs_dirtyfb = {
|
|
|
|
.destroy = drm_gem_fb_destroy,
|
|
|
|
.create_handle = drm_gem_fb_create_handle,
|
|
|
|
.dirty = drm_atomic_helper_dirtyfb,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* drm_gem_fb_create_with_dirty() - Helper function for the
|
|
|
|
* &drm_mode_config_funcs.fb_create callback
|
|
|
|
* @dev: DRM device
|
|
|
|
* @file: DRM file that holds the GEM handle(s) backing the framebuffer
|
|
|
|
* @mode_cmd: Metadata from the userspace framebuffer creation request
|
|
|
|
*
|
|
|
|
* This function creates a new framebuffer object described by
|
|
|
|
* &drm_mode_fb_cmd2. This description includes handles for the buffer(s)
|
|
|
|
* backing the framebuffer. drm_atomic_helper_dirtyfb() is used for the dirty
|
|
|
|
* callback giving framebuffer flushing through the atomic machinery. Use
|
|
|
|
* drm_gem_fb_create() if you don't need the dirty callback.
|
|
|
|
* The function does buffer size validation.
|
|
|
|
*
|
|
|
|
* Drivers should also call drm_plane_enable_fb_damage_clips() on all planes
|
|
|
|
* to enable userspace to use damage clips also with the ATOMIC IOCTL.
|
|
|
|
*
|
|
|
|
* Drivers can use this as their &drm_mode_config_funcs.fb_create callback.
|
|
|
|
* The ADDFB2 IOCTL calls into this callback.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* Pointer to a &drm_framebuffer on success or an error pointer on failure.
|
|
|
|
*/
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
|
|
|
|
&drm_gem_fb_funcs_dirtyfb);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_dirty);
|
|
|
|
|
2021-07-30 18:35:08 +00:00
|
|
|
/**
|
|
|
|
* drm_gem_fb_vmap - maps all framebuffer BOs into kernel address space
|
|
|
|
* @fb: the framebuffer
|
|
|
|
* @map: returns the mapping's address for each BO
|
2021-08-03 12:59:18 +00:00
|
|
|
* @data: returns the data address for each BO, can be NULL
|
2021-07-30 18:35:08 +00:00
|
|
|
*
|
|
|
|
* This function maps all buffer objects of the given framebuffer into
|
2022-02-04 17:05:41 +00:00
|
|
|
* kernel address space and stores them in struct iosys_map. If the
|
2021-07-30 18:35:08 +00:00
|
|
|
* mapping operation fails for one of the BOs, the function unmaps the
|
|
|
|
* already established mappings automatically.
|
|
|
|
*
|
2021-08-03 12:59:18 +00:00
|
|
|
* Callers that want to access a BO's stored data should pass @data.
|
|
|
|
* The argument returns the addresses of the data stored in each BO. This
|
|
|
|
* is different from @map if the framebuffer's offsets field is non-zero.
|
|
|
|
*
|
2021-07-30 18:35:08 +00:00
|
|
|
* See drm_gem_fb_vunmap() for unmapping.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* 0 on success, or a negative errno code otherwise.
|
|
|
|
*/
|
|
|
|
int drm_gem_fb_vmap(struct drm_framebuffer *fb,
|
2022-02-04 17:05:41 +00:00
|
|
|
struct iosys_map map[static DRM_FORMAT_MAX_PLANES],
|
|
|
|
struct iosys_map data[DRM_FORMAT_MAX_PLANES])
|
2021-07-30 18:35:08 +00:00
|
|
|
{
|
|
|
|
struct drm_gem_object *obj;
|
|
|
|
unsigned int i;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
for (i = 0; i < DRM_FORMAT_MAX_PLANES; ++i) {
|
|
|
|
obj = drm_gem_fb_get_obj(fb, i);
|
2021-07-30 18:35:09 +00:00
|
|
|
if (!obj) {
|
2022-02-04 17:05:41 +00:00
|
|
|
iosys_map_clear(&map[i]);
|
2021-07-30 18:35:08 +00:00
|
|
|
continue;
|
2021-07-30 18:35:09 +00:00
|
|
|
}
|
2021-07-30 18:35:08 +00:00
|
|
|
ret = drm_gem_vmap(obj, &map[i]);
|
|
|
|
if (ret)
|
|
|
|
goto err_drm_gem_vunmap;
|
|
|
|
}
|
|
|
|
|
2021-08-03 12:59:18 +00:00
|
|
|
if (data) {
|
|
|
|
for (i = 0; i < DRM_FORMAT_MAX_PLANES; ++i) {
|
|
|
|
memcpy(&data[i], &map[i], sizeof(data[i]));
|
2022-02-04 17:05:41 +00:00
|
|
|
if (iosys_map_is_null(&data[i]))
|
2021-08-03 12:59:18 +00:00
|
|
|
continue;
|
2022-02-04 17:05:41 +00:00
|
|
|
iosys_map_incr(&data[i], fb->offsets[i]);
|
2021-08-03 12:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-30 18:35:08 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_drm_gem_vunmap:
|
|
|
|
while (i) {
|
|
|
|
--i;
|
|
|
|
obj = drm_gem_fb_get_obj(fb, i);
|
|
|
|
if (!obj)
|
|
|
|
continue;
|
|
|
|
drm_gem_vunmap(obj, &map[i]);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(drm_gem_fb_vmap);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* drm_gem_fb_vunmap - unmaps framebuffer BOs from kernel address space
|
|
|
|
* @fb: the framebuffer
|
|
|
|
* @map: mapping addresses as returned by drm_gem_fb_vmap()
|
|
|
|
*
|
|
|
|
* This function unmaps all buffer objects of the given framebuffer.
|
|
|
|
*
|
|
|
|
* See drm_gem_fb_vmap() for more information.
|
|
|
|
*/
|
|
|
|
void drm_gem_fb_vunmap(struct drm_framebuffer *fb,
|
2022-02-04 17:05:41 +00:00
|
|
|
struct iosys_map map[static DRM_FORMAT_MAX_PLANES])
|
2021-07-30 18:35:08 +00:00
|
|
|
{
|
|
|
|
unsigned int i = DRM_FORMAT_MAX_PLANES;
|
|
|
|
struct drm_gem_object *obj;
|
|
|
|
|
|
|
|
while (i) {
|
|
|
|
--i;
|
|
|
|
obj = drm_gem_fb_get_obj(fb, i);
|
|
|
|
if (!obj)
|
|
|
|
continue;
|
2022-02-04 17:05:41 +00:00
|
|
|
if (iosys_map_is_null(&map[i]))
|
2021-07-30 18:35:09 +00:00
|
|
|
continue;
|
2021-07-30 18:35:08 +00:00
|
|
|
drm_gem_vunmap(obj, &map[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(drm_gem_fb_vunmap);
|
|
|
|
|
2022-05-17 11:33:23 +00:00
|
|
|
static void __drm_gem_fb_end_cpu_access(struct drm_framebuffer *fb, enum dma_data_direction dir,
|
|
|
|
unsigned int num_planes)
|
|
|
|
{
|
|
|
|
struct dma_buf_attachment *import_attach;
|
|
|
|
struct drm_gem_object *obj;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
while (num_planes) {
|
|
|
|
--num_planes;
|
|
|
|
obj = drm_gem_fb_get_obj(fb, num_planes);
|
|
|
|
if (!obj)
|
|
|
|
continue;
|
|
|
|
import_attach = obj->import_attach;
|
|
|
|
if (!import_attach)
|
|
|
|
continue;
|
|
|
|
ret = dma_buf_end_cpu_access(import_attach->dmabuf, dir);
|
|
|
|
if (ret)
|
|
|
|
drm_err(fb->dev, "dma_buf_end_cpu_access(%u, %d) failed: %d\n",
|
|
|
|
ret, num_planes, dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:07:55 +00:00
|
|
|
/**
|
|
|
|
* drm_gem_fb_begin_cpu_access - prepares GEM buffer objects for CPU access
|
|
|
|
* @fb: the framebuffer
|
|
|
|
* @dir: access mode
|
|
|
|
*
|
|
|
|
* Prepares a framebuffer's GEM buffer objects for CPU access. This function
|
|
|
|
* must be called before accessing the BO data within the kernel. For imported
|
|
|
|
* BOs, the function calls dma_buf_begin_cpu_access().
|
|
|
|
*
|
|
|
|
* See drm_gem_fb_end_cpu_access() for signalling the end of CPU access.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* 0 on success, or a negative errno code otherwise.
|
|
|
|
*/
|
|
|
|
int drm_gem_fb_begin_cpu_access(struct drm_framebuffer *fb, enum dma_data_direction dir)
|
|
|
|
{
|
|
|
|
struct dma_buf_attachment *import_attach;
|
|
|
|
struct drm_gem_object *obj;
|
|
|
|
size_t i;
|
2022-05-17 11:33:23 +00:00
|
|
|
int ret;
|
2021-07-16 14:07:55 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(fb->obj); ++i) {
|
|
|
|
obj = drm_gem_fb_get_obj(fb, i);
|
|
|
|
if (!obj)
|
|
|
|
continue;
|
|
|
|
import_attach = obj->import_attach;
|
|
|
|
if (!import_attach)
|
|
|
|
continue;
|
|
|
|
ret = dma_buf_begin_cpu_access(import_attach->dmabuf, dir);
|
|
|
|
if (ret)
|
2022-05-17 11:33:23 +00:00
|
|
|
goto err___drm_gem_fb_end_cpu_access;
|
2021-07-16 14:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2022-05-17 11:33:23 +00:00
|
|
|
err___drm_gem_fb_end_cpu_access:
|
|
|
|
__drm_gem_fb_end_cpu_access(fb, dir, i);
|
2021-07-16 14:07:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(drm_gem_fb_begin_cpu_access);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* drm_gem_fb_end_cpu_access - signals end of CPU access to GEM buffer objects
|
|
|
|
* @fb: the framebuffer
|
|
|
|
* @dir: access mode
|
|
|
|
*
|
|
|
|
* Signals the end of CPU access to the given framebuffer's GEM buffer objects. This
|
|
|
|
* function must be paired with a corresponding call to drm_gem_fb_begin_cpu_access().
|
|
|
|
* For imported BOs, the function calls dma_buf_end_cpu_access().
|
|
|
|
*
|
|
|
|
* See also drm_gem_fb_begin_cpu_access().
|
|
|
|
*/
|
|
|
|
void drm_gem_fb_end_cpu_access(struct drm_framebuffer *fb, enum dma_data_direction dir)
|
|
|
|
{
|
2022-05-17 11:33:23 +00:00
|
|
|
__drm_gem_fb_end_cpu_access(fb, dir, ARRAY_SIZE(fb->obj));
|
2021-07-16 14:07:55 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(drm_gem_fb_end_cpu_access);
|
|
|
|
|
2020-03-31 15:53:08 +00:00
|
|
|
static __u32 drm_gem_afbc_get_bpp(struct drm_device *dev,
|
|
|
|
const struct drm_mode_fb_cmd2 *mode_cmd)
|
|
|
|
{
|
|
|
|
const struct drm_format_info *info;
|
|
|
|
|
|
|
|
info = drm_get_format_info(dev, mode_cmd);
|
|
|
|
|
|
|
|
/* use whatever a driver has set */
|
|
|
|
if (info->cpp[0])
|
|
|
|
return info->cpp[0] * 8;
|
|
|
|
|
|
|
|
/* guess otherwise */
|
|
|
|
switch (info->format) {
|
|
|
|
case DRM_FORMAT_YUV420_8BIT:
|
|
|
|
return 12;
|
|
|
|
case DRM_FORMAT_YUV420_10BIT:
|
|
|
|
return 15;
|
|
|
|
case DRM_FORMAT_VUY101010:
|
|
|
|
return 30;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* all attempts failed */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-11 14:55:37 +00:00
|
|
|
static int drm_gem_afbc_min_size(struct drm_device *dev,
|
|
|
|
const struct drm_mode_fb_cmd2 *mode_cmd,
|
|
|
|
struct drm_afbc_framebuffer *afbc_fb)
|
|
|
|
{
|
|
|
|
__u32 n_blocks, w_alignment, h_alignment, hdr_alignment;
|
|
|
|
/* remove bpp when all users properly encode cpp in drm_format_info */
|
|
|
|
__u32 bpp;
|
|
|
|
|
|
|
|
switch (mode_cmd->modifier[0] & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) {
|
|
|
|
case AFBC_FORMAT_MOD_BLOCK_SIZE_16x16:
|
|
|
|
afbc_fb->block_width = 16;
|
|
|
|
afbc_fb->block_height = 16;
|
|
|
|
break;
|
|
|
|
case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8:
|
|
|
|
afbc_fb->block_width = 32;
|
|
|
|
afbc_fb->block_height = 8;
|
|
|
|
break;
|
|
|
|
/* no user exists yet - fall through */
|
|
|
|
case AFBC_FORMAT_MOD_BLOCK_SIZE_64x4:
|
|
|
|
case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8_64x4:
|
|
|
|
default:
|
2020-03-31 15:53:07 +00:00
|
|
|
drm_dbg_kms(dev, "Invalid AFBC_FORMAT_MOD_BLOCK_SIZE: %lld.\n",
|
|
|
|
mode_cmd->modifier[0]
|
|
|
|
& AFBC_FORMAT_MOD_BLOCK_SIZE_MASK);
|
2020-03-11 14:55:37 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tiled header afbc */
|
|
|
|
w_alignment = afbc_fb->block_width;
|
|
|
|
h_alignment = afbc_fb->block_height;
|
|
|
|
hdr_alignment = AFBC_HDR_ALIGN;
|
|
|
|
if (mode_cmd->modifier[0] & AFBC_FORMAT_MOD_TILED) {
|
|
|
|
w_alignment *= AFBC_TH_LAYOUT_ALIGNMENT;
|
|
|
|
h_alignment *= AFBC_TH_LAYOUT_ALIGNMENT;
|
|
|
|
hdr_alignment = AFBC_TH_BODY_START_ALIGNMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
afbc_fb->aligned_width = ALIGN(mode_cmd->width, w_alignment);
|
|
|
|
afbc_fb->aligned_height = ALIGN(mode_cmd->height, h_alignment);
|
|
|
|
afbc_fb->offset = mode_cmd->offsets[0];
|
|
|
|
|
2020-03-31 15:53:08 +00:00
|
|
|
bpp = drm_gem_afbc_get_bpp(dev, mode_cmd);
|
|
|
|
if (!bpp) {
|
|
|
|
drm_dbg_kms(dev, "Invalid AFBC bpp value: %d\n", bpp);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2020-03-11 14:55:37 +00:00
|
|
|
|
|
|
|
n_blocks = (afbc_fb->aligned_width * afbc_fb->aligned_height)
|
|
|
|
/ AFBC_SUPERBLOCK_PIXELS;
|
|
|
|
afbc_fb->afbc_size = ALIGN(n_blocks * AFBC_HEADER_SIZE, hdr_alignment);
|
|
|
|
afbc_fb->afbc_size += n_blocks * ALIGN(bpp * AFBC_SUPERBLOCK_PIXELS / 8,
|
|
|
|
AFBC_SUPERBLOCK_ALIGNMENT);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* drm_gem_fb_afbc_init() - Helper function for drivers using afbc to
|
|
|
|
* fill and validate all the afbc-specific
|
|
|
|
* struct drm_afbc_framebuffer members
|
|
|
|
*
|
|
|
|
* @dev: DRM device
|
|
|
|
* @afbc_fb: afbc-specific framebuffer
|
|
|
|
* @mode_cmd: Metadata from the userspace framebuffer creation request
|
|
|
|
* @afbc_fb: afbc framebuffer
|
|
|
|
*
|
|
|
|
* This function can be used by drivers which support afbc to complete
|
|
|
|
* the preparation of struct drm_afbc_framebuffer. It must be called after
|
|
|
|
* allocating the said struct and calling drm_gem_fb_init_with_funcs().
|
|
|
|
* It is caller's responsibility to put afbc_fb->base.obj objects in case
|
|
|
|
* the call is unsuccessful.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* Zero on success or a negative error value on failure.
|
|
|
|
*/
|
|
|
|
int drm_gem_fb_afbc_init(struct drm_device *dev,
|
|
|
|
const struct drm_mode_fb_cmd2 *mode_cmd,
|
|
|
|
struct drm_afbc_framebuffer *afbc_fb)
|
|
|
|
{
|
|
|
|
const struct drm_format_info *info;
|
|
|
|
struct drm_gem_object **objs;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
objs = afbc_fb->base.obj;
|
|
|
|
info = drm_get_format_info(dev, mode_cmd);
|
|
|
|
if (!info)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
ret = drm_gem_afbc_min_size(dev, mode_cmd, afbc_fb);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (objs[0]->size < afbc_fb->afbc_size)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(drm_gem_fb_afbc_init);
|