2012-07-02 21:37:47 +07:00
|
|
|
/*
|
|
|
|
* drm kms/fb cma (contiguous memory allocator) helper functions
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Analog Device Inc.
|
|
|
|
* Author: Lars-Peter Clausen <lars@metafoo.de>
|
|
|
|
*
|
|
|
|
* Based on udl_fbdev.c
|
|
|
|
* Copyright (C) 2012 Red Hat
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <drm/drmP.h>
|
2018-07-03 23:03:50 +07:00
|
|
|
#include <drm/drm_client.h>
|
2012-07-02 21:37:47 +07:00
|
|
|
#include <drm/drm_fb_helper.h>
|
2017-08-13 20:31:45 +07:00
|
|
|
#include <drm/drm_framebuffer.h>
|
2012-07-02 21:37:47 +07:00
|
|
|
#include <drm/drm_gem_cma_helper.h>
|
2017-08-13 20:31:45 +07:00
|
|
|
#include <drm/drm_gem_framebuffer_helper.h>
|
2012-07-02 21:37:47 +07:00
|
|
|
#include <drm/drm_fb_cma_helper.h>
|
2017-11-15 21:19:41 +07:00
|
|
|
#include <drm/drm_print.h>
|
2012-07-02 21:37:47 +07:00
|
|
|
#include <linux/module.h>
|
|
|
|
|
|
|
|
struct drm_fbdev_cma {
|
|
|
|
struct drm_fb_helper fb_helper;
|
|
|
|
};
|
|
|
|
|
2016-04-28 22:18:35 +07:00
|
|
|
/**
|
|
|
|
* DOC: framebuffer cma helper functions
|
|
|
|
*
|
|
|
|
* Provides helper functions for creating a cma (contiguous memory allocator)
|
|
|
|
* backed framebuffer.
|
|
|
|
*
|
2017-09-24 19:26:25 +07:00
|
|
|
* drm_gem_fb_create() is used in the &drm_mode_config_funcs.fb_create
|
2016-05-11 23:09:18 +07:00
|
|
|
* callback function to create a cma backed framebuffer.
|
2016-04-28 22:18:35 +07:00
|
|
|
*
|
|
|
|
* An fbdev framebuffer backed by cma is also available by calling
|
2017-11-15 21:19:41 +07:00
|
|
|
* drm_fb_cma_fbdev_init(). drm_fb_cma_fbdev_fini() tears it down.
|
2016-04-28 22:18:35 +07:00
|
|
|
*/
|
|
|
|
|
2012-07-02 21:37:47 +07:00
|
|
|
static inline struct drm_fbdev_cma *to_fbdev_cma(struct drm_fb_helper *helper)
|
|
|
|
{
|
|
|
|
return container_of(helper, struct drm_fbdev_cma, fb_helper);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer
|
|
|
|
* @fb: The framebuffer
|
|
|
|
* @plane: Which plane
|
|
|
|
*
|
|
|
|
* Return the CMA GEM object for given framebuffer.
|
|
|
|
*
|
|
|
|
* This function will usually be called from the CRTC callback functions.
|
|
|
|
*/
|
|
|
|
struct drm_gem_cma_object *drm_fb_cma_get_gem_obj(struct drm_framebuffer *fb,
|
2016-06-01 04:11:12 +07:00
|
|
|
unsigned int plane)
|
2012-07-02 21:37:47 +07:00
|
|
|
{
|
2017-08-13 20:31:45 +07:00
|
|
|
struct drm_gem_object *gem;
|
2012-07-02 21:37:47 +07:00
|
|
|
|
2017-08-13 20:31:45 +07:00
|
|
|
gem = drm_gem_fb_get_obj(fb, plane);
|
|
|
|
if (!gem)
|
2012-07-02 21:37:47 +07:00
|
|
|
return NULL;
|
|
|
|
|
2017-08-13 20:31:45 +07:00
|
|
|
return to_drm_gem_cma_obj(gem);
|
2012-07-02 21:37:47 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj);
|
|
|
|
|
2017-04-14 17:13:32 +07:00
|
|
|
/**
|
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-02 00:02:05 +07:00
|
|
|
* drm_fb_cma_get_gem_addr() - Get physical address for framebuffer, for pixel
|
|
|
|
* formats where values are grouped in blocks this will get you the beginning of
|
|
|
|
* the block
|
2017-04-14 17:13:32 +07:00
|
|
|
* @fb: The framebuffer
|
|
|
|
* @state: Which state of drm plane
|
|
|
|
* @plane: Which plane
|
|
|
|
* Return the CMA GEM address for given framebuffer.
|
|
|
|
*
|
|
|
|
* This function will usually be called from the PLANE callback functions.
|
|
|
|
*/
|
|
|
|
dma_addr_t drm_fb_cma_get_gem_addr(struct drm_framebuffer *fb,
|
|
|
|
struct drm_plane_state *state,
|
|
|
|
unsigned int plane)
|
|
|
|
{
|
2017-08-13 20:31:45 +07:00
|
|
|
struct drm_gem_cma_object *obj;
|
2017-04-14 17:13:32 +07:00
|
|
|
dma_addr_t paddr;
|
2018-08-17 23:54:00 +07:00
|
|
|
u8 h_div = 1, v_div = 1;
|
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-02 00:02:05 +07:00
|
|
|
u32 block_w = drm_format_info_block_width(fb->format, plane);
|
|
|
|
u32 block_h = drm_format_info_block_height(fb->format, plane);
|
|
|
|
u32 block_size = fb->format->char_per_block[plane];
|
|
|
|
u32 sample_x;
|
|
|
|
u32 sample_y;
|
|
|
|
u32 block_start_y;
|
|
|
|
u32 num_hblocks;
|
2017-04-14 17:13:32 +07:00
|
|
|
|
2017-08-13 20:31:45 +07:00
|
|
|
obj = drm_fb_cma_get_gem_obj(fb, plane);
|
|
|
|
if (!obj)
|
2017-04-14 17:13:32 +07:00
|
|
|
return 0;
|
|
|
|
|
2017-08-13 20:31:45 +07:00
|
|
|
paddr = obj->paddr + fb->offsets[plane];
|
2018-08-17 23:54:00 +07:00
|
|
|
|
|
|
|
if (plane > 0) {
|
|
|
|
h_div = fb->format->hsub;
|
|
|
|
v_div = fb->format->vsub;
|
|
|
|
}
|
|
|
|
|
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-02 00:02:05 +07:00
|
|
|
sample_x = (state->src_x >> 16) / h_div;
|
|
|
|
sample_y = (state->src_y >> 16) / v_div;
|
|
|
|
block_start_y = (sample_y / block_h) * block_h;
|
|
|
|
num_hblocks = sample_x / block_w;
|
|
|
|
|
|
|
|
paddr += fb->pitches[plane] * block_start_y;
|
|
|
|
paddr += block_size * num_hblocks;
|
2017-04-14 17:13:32 +07:00
|
|
|
|
|
|
|
return paddr;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_addr);
|
|
|
|
|
2017-11-15 21:19:41 +07:00
|
|
|
/**
|
|
|
|
* drm_fb_cma_fbdev_init() - Allocate and initialize fbdev emulation
|
|
|
|
* @dev: DRM device
|
|
|
|
* @preferred_bpp: Preferred bits per pixel for the device.
|
|
|
|
* @dev->mode_config.preferred_depth is used if this is zero.
|
|
|
|
* @max_conn_count: Maximum number of connectors.
|
|
|
|
* @dev->mode_config.num_connector is used if this is zero.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* Zero on success or negative error code on failure.
|
|
|
|
*/
|
|
|
|
int drm_fb_cma_fbdev_init(struct drm_device *dev, unsigned int preferred_bpp,
|
|
|
|
unsigned int max_conn_count)
|
|
|
|
{
|
2018-07-03 23:03:50 +07:00
|
|
|
struct drm_fbdev_cma *fbdev_cma;
|
|
|
|
|
|
|
|
/* dev->fb_helper will indirectly point to fbdev_cma after this call */
|
|
|
|
fbdev_cma = drm_fbdev_cma_init(dev, preferred_bpp, max_conn_count);
|
2018-10-10 20:02:43 +07:00
|
|
|
return PTR_ERR_OR_ZERO(fbdev_cma);
|
2017-11-15 21:19:41 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(drm_fb_cma_fbdev_init);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* drm_fb_cma_fbdev_fini() - Teardown fbdev emulation
|
|
|
|
* @dev: DRM device
|
|
|
|
*/
|
|
|
|
void drm_fb_cma_fbdev_fini(struct drm_device *dev)
|
|
|
|
{
|
2018-07-03 23:03:50 +07:00
|
|
|
if (dev->fb_helper)
|
|
|
|
drm_fbdev_cma_fini(to_fbdev_cma(dev->fb_helper));
|
2017-11-15 21:19:41 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(drm_fb_cma_fbdev_fini);
|
|
|
|
|
2018-07-03 23:03:50 +07:00
|
|
|
static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = {
|
|
|
|
.fb_probe = drm_fb_helper_generic_probe,
|
|
|
|
};
|
|
|
|
|
2012-07-02 21:37:47 +07:00
|
|
|
/**
|
2018-07-03 23:03:50 +07:00
|
|
|
* drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct
|
2012-07-02 21:37:47 +07:00
|
|
|
* @dev: DRM device
|
|
|
|
* @preferred_bpp: Preferred bits per pixel for the device
|
|
|
|
* @max_conn_count: Maximum number of connectors
|
|
|
|
*
|
|
|
|
* Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
|
|
|
|
*/
|
2018-07-03 23:03:50 +07:00
|
|
|
struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev,
|
|
|
|
unsigned int preferred_bpp, unsigned int max_conn_count)
|
2012-07-02 21:37:47 +07:00
|
|
|
{
|
|
|
|
struct drm_fbdev_cma *fbdev_cma;
|
2018-07-03 23:03:50 +07:00
|
|
|
struct drm_fb_helper *fb_helper;
|
2012-07-02 21:37:47 +07:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL);
|
2018-07-03 23:03:50 +07:00
|
|
|
if (!fbdev_cma)
|
2012-07-02 21:37:47 +07:00
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2018-07-03 23:03:50 +07:00
|
|
|
fb_helper = &fbdev_cma->fb_helper;
|
2014-06-27 22:19:24 +07:00
|
|
|
|
2018-10-02 02:45:36 +07:00
|
|
|
ret = drm_client_init(dev, &fb_helper->client, "fbdev", NULL);
|
2018-07-03 23:03:50 +07:00
|
|
|
if (ret)
|
2012-07-02 21:37:47 +07:00
|
|
|
goto err_free;
|
|
|
|
|
2018-07-03 23:03:50 +07:00
|
|
|
ret = drm_fb_helper_fbdev_setup(dev, fb_helper, &drm_fb_cma_helper_funcs,
|
|
|
|
preferred_bpp, max_conn_count);
|
|
|
|
if (ret)
|
|
|
|
goto err_client_put;
|
2012-07-02 21:37:47 +07:00
|
|
|
|
2018-10-02 02:45:36 +07:00
|
|
|
drm_client_add(&fb_helper->client);
|
|
|
|
|
2012-07-02 21:37:47 +07:00
|
|
|
return fbdev_cma;
|
|
|
|
|
2018-07-03 23:03:50 +07:00
|
|
|
err_client_put:
|
|
|
|
drm_client_release(&fb_helper->client);
|
2012-07-02 21:37:47 +07:00
|
|
|
err_free:
|
|
|
|
kfree(fbdev_cma);
|
|
|
|
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(drm_fbdev_cma_init);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* drm_fbdev_cma_fini() - Free drm_fbdev_cma struct
|
|
|
|
* @fbdev_cma: The drm_fbdev_cma struct
|
|
|
|
*/
|
|
|
|
void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma)
|
|
|
|
{
|
2015-07-22 16:28:20 +07:00
|
|
|
drm_fb_helper_unregister_fbi(&fbdev_cma->fb_helper);
|
2018-07-03 23:03:50 +07:00
|
|
|
/* All resources have now been freed by drm_fbdev_fb_destroy() */
|
2012-07-02 21:37:47 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode
|
|
|
|
* @fbdev_cma: The drm_fbdev_cma struct, may be NULL
|
|
|
|
*
|
2016-12-30 03:48:34 +07:00
|
|
|
* This function is usually called from the &drm_driver.lastclose callback.
|
2012-07-02 21:37:47 +07:00
|
|
|
*/
|
|
|
|
void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma)
|
|
|
|
{
|
2014-05-30 23:29:48 +07:00
|
|
|
if (fbdev_cma)
|
|
|
|
drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma->fb_helper);
|
2012-07-02 21:37:47 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* drm_fbdev_cma_hotplug_event() - Poll for hotpulug events
|
|
|
|
* @fbdev_cma: The drm_fbdev_cma struct, may be NULL
|
|
|
|
*
|
2016-12-30 03:48:34 +07:00
|
|
|
* This function is usually called from the &drm_mode_config.output_poll_changed
|
2012-07-02 21:37:47 +07:00
|
|
|
* callback.
|
|
|
|
*/
|
|
|
|
void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma)
|
|
|
|
{
|
|
|
|
if (fbdev_cma)
|
|
|
|
drm_fb_helper_hotplug_event(&fbdev_cma->fb_helper);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event);
|