2019-05-27 13:55:21 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2016-01-05 00:36:34 +07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 MediaTek Inc.
|
|
|
|
*/
|
|
|
|
|
2019-07-16 13:42:20 +07:00
|
|
|
#include <linux/dma-buf.h>
|
2019-08-11 15:06:32 +07:00
|
|
|
#include <linux/dma-resv.h>
|
2019-07-16 13:42:20 +07:00
|
|
|
|
2019-01-18 04:03:34 +07:00
|
|
|
#include <drm/drm_modeset_helper.h>
|
2016-01-05 00:36:34 +07:00
|
|
|
#include <drm/drm_fb_helper.h>
|
2019-07-16 13:42:20 +07:00
|
|
|
#include <drm/drm_fourcc.h>
|
2016-01-05 00:36:34 +07:00
|
|
|
#include <drm/drm_gem.h>
|
2018-05-18 20:47:04 +07:00
|
|
|
#include <drm/drm_gem_framebuffer_helper.h>
|
2016-01-05 00:36:34 +07:00
|
|
|
|
|
|
|
#include "mtk_drm_drv.h"
|
|
|
|
#include "mtk_drm_fb.h"
|
|
|
|
#include "mtk_drm_gem.h"
|
|
|
|
|
|
|
|
static const struct drm_framebuffer_funcs mtk_drm_fb_funcs = {
|
2018-05-18 20:47:04 +07:00
|
|
|
.create_handle = drm_gem_fb_create_handle,
|
|
|
|
.destroy = drm_gem_fb_destroy,
|
2016-01-05 00:36:34 +07:00
|
|
|
};
|
|
|
|
|
2018-05-18 20:47:05 +07:00
|
|
|
static struct drm_framebuffer *mtk_drm_framebuffer_init(struct drm_device *dev,
|
2016-01-05 00:36:34 +07:00
|
|
|
const struct drm_mode_fb_cmd2 *mode,
|
|
|
|
struct drm_gem_object *obj)
|
|
|
|
{
|
2019-05-16 17:31:47 +07:00
|
|
|
const struct drm_format_info *info = drm_get_format_info(dev, mode);
|
2018-05-18 20:47:05 +07:00
|
|
|
struct drm_framebuffer *fb;
|
2016-01-05 00:36:34 +07:00
|
|
|
int ret;
|
|
|
|
|
2019-05-16 17:31:47 +07:00
|
|
|
if (info->num_planes != 1)
|
2016-01-05 00:36:34 +07:00
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
2018-05-18 20:47:05 +07:00
|
|
|
fb = kzalloc(sizeof(*fb), GFP_KERNEL);
|
|
|
|
if (!fb)
|
2016-01-05 00:36:34 +07:00
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2018-05-18 20:47:05 +07:00
|
|
|
drm_helper_mode_fill_fb_struct(dev, fb, mode);
|
2016-01-05 00:36:34 +07:00
|
|
|
|
2018-05-18 20:47:05 +07:00
|
|
|
fb->obj[0] = obj;
|
2016-01-05 00:36:34 +07:00
|
|
|
|
2018-05-18 20:47:05 +07:00
|
|
|
ret = drm_framebuffer_init(dev, fb, &mtk_drm_fb_funcs);
|
2016-01-05 00:36:34 +07:00
|
|
|
if (ret) {
|
|
|
|
DRM_ERROR("failed to initialize framebuffer\n");
|
2018-05-18 20:47:05 +07:00
|
|
|
kfree(fb);
|
2016-01-05 00:36:34 +07:00
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
2018-05-18 20:47:05 +07:00
|
|
|
return fb;
|
2016-01-05 00:36:34 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
struct drm_framebuffer *mtk_drm_mode_fb_create(struct drm_device *dev,
|
|
|
|
struct drm_file *file,
|
|
|
|
const struct drm_mode_fb_cmd2 *cmd)
|
|
|
|
{
|
2019-05-16 17:31:47 +07:00
|
|
|
const struct drm_format_info *info = drm_get_format_info(dev, cmd);
|
2018-05-18 20:47:05 +07:00
|
|
|
struct drm_framebuffer *fb;
|
2016-01-05 00:36:34 +07:00
|
|
|
struct drm_gem_object *gem;
|
|
|
|
unsigned int width = cmd->width;
|
|
|
|
unsigned int height = cmd->height;
|
|
|
|
unsigned int size, bpp;
|
|
|
|
int ret;
|
|
|
|
|
2019-05-16 17:31:47 +07:00
|
|
|
if (info->num_planes != 1)
|
2016-01-05 00:36:34 +07:00
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
2016-05-18 23:07:31 +07:00
|
|
|
gem = drm_gem_object_lookup(file, cmd->handles[0]);
|
2016-01-05 00:36:34 +07:00
|
|
|
if (!gem)
|
|
|
|
return ERR_PTR(-ENOENT);
|
|
|
|
|
2019-05-16 17:31:52 +07:00
|
|
|
bpp = info->cpp[0];
|
2016-01-05 00:36:34 +07:00
|
|
|
size = (height - 1) * cmd->pitches[0] + width * bpp;
|
|
|
|
size += cmd->offsets[0];
|
|
|
|
|
|
|
|
if (gem->size < size) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto unreference;
|
|
|
|
}
|
|
|
|
|
2018-05-18 20:47:05 +07:00
|
|
|
fb = mtk_drm_framebuffer_init(dev, cmd, gem);
|
|
|
|
if (IS_ERR(fb)) {
|
|
|
|
ret = PTR_ERR(fb);
|
2016-01-05 00:36:34 +07:00
|
|
|
goto unreference;
|
|
|
|
}
|
|
|
|
|
2018-05-18 20:47:05 +07:00
|
|
|
return fb;
|
2016-01-05 00:36:34 +07:00
|
|
|
|
|
|
|
unreference:
|
2017-08-11 19:32:59 +07:00
|
|
|
drm_gem_object_put_unlocked(gem);
|
2016-01-05 00:36:34 +07:00
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|