2012-04-17 22:01:25 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2010 Matt Turner.
|
|
|
|
* Copyright 2012 Red Hat
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU General
|
|
|
|
* Public License version 2. See the file COPYING in the main
|
|
|
|
* directory of this archive for more details.
|
|
|
|
*
|
|
|
|
* Authors: Matthew Garrett
|
|
|
|
* Matt Turner
|
|
|
|
* Dave Airlie
|
|
|
|
*/
|
|
|
|
#include <linux/module.h>
|
2012-10-03 01:01:07 +08:00
|
|
|
#include <drm/drmP.h>
|
2019-01-13 03:32:44 +08:00
|
|
|
#include <drm/drm_util.h>
|
2012-10-03 01:01:07 +08:00
|
|
|
#include <drm/drm_fb_helper.h>
|
2013-01-21 06:12:54 +08:00
|
|
|
#include <drm/drm_crtc_helper.h>
|
2012-04-17 22:01:25 +08:00
|
|
|
|
|
|
|
#include "mgag200_drv.h"
|
|
|
|
|
|
|
|
static void mga_dirty_update(struct mga_fbdev *mfbdev,
|
|
|
|
int x, int y, int width, int height)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct drm_gem_object *obj;
|
2019-05-08 16:26:24 +08:00
|
|
|
struct drm_gem_vram_object *gbo;
|
2012-04-17 22:01:25 +08:00
|
|
|
int src_offset, dst_offset;
|
2016-12-15 05:32:20 +08:00
|
|
|
int bpp = mfbdev->mfb.base.format->cpp[0];
|
2019-06-13 15:30:38 +08:00
|
|
|
int ret;
|
2019-05-08 16:26:26 +08:00
|
|
|
u8 *dst;
|
2012-04-17 22:01:25 +08:00
|
|
|
bool unmap = false;
|
2013-05-02 12:52:01 +08:00
|
|
|
bool store_for_later = false;
|
|
|
|
int x2, y2;
|
|
|
|
unsigned long flags;
|
2012-04-17 22:01:25 +08:00
|
|
|
|
|
|
|
obj = mfbdev->mfb.obj;
|
2019-05-08 16:26:24 +08:00
|
|
|
gbo = drm_gem_vram_of_gem(obj);
|
2012-04-17 22:01:25 +08:00
|
|
|
|
2019-06-13 15:30:38 +08:00
|
|
|
if (drm_can_sleep()) {
|
|
|
|
/* We pin the BO so it won't be moved during the
|
|
|
|
* update. The actual location, video RAM or system
|
|
|
|
* memory, is not important.
|
|
|
|
*/
|
|
|
|
ret = drm_gem_vram_pin(gbo, 0);
|
|
|
|
if (ret) {
|
|
|
|
if (ret != -EBUSY)
|
|
|
|
return;
|
|
|
|
store_for_later = true;
|
|
|
|
}
|
|
|
|
} else {
|
2013-05-02 12:52:01 +08:00
|
|
|
store_for_later = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
x2 = x + width - 1;
|
|
|
|
y2 = y + height - 1;
|
|
|
|
spin_lock_irqsave(&mfbdev->dirty_lock, flags);
|
|
|
|
|
|
|
|
if (mfbdev->y1 < y)
|
|
|
|
y = mfbdev->y1;
|
|
|
|
if (mfbdev->y2 > y2)
|
|
|
|
y2 = mfbdev->y2;
|
|
|
|
if (mfbdev->x1 < x)
|
|
|
|
x = mfbdev->x1;
|
|
|
|
if (mfbdev->x2 > x2)
|
|
|
|
x2 = mfbdev->x2;
|
|
|
|
|
|
|
|
if (store_for_later) {
|
|
|
|
mfbdev->x1 = x;
|
|
|
|
mfbdev->x2 = x2;
|
|
|
|
mfbdev->y1 = y;
|
|
|
|
mfbdev->y2 = y2;
|
|
|
|
spin_unlock_irqrestore(&mfbdev->dirty_lock, flags);
|
2012-04-17 22:01:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-02 12:52:01 +08:00
|
|
|
mfbdev->x1 = mfbdev->y1 = INT_MAX;
|
|
|
|
mfbdev->x2 = mfbdev->y2 = 0;
|
|
|
|
spin_unlock_irqrestore(&mfbdev->dirty_lock, flags);
|
|
|
|
|
2019-05-08 16:26:26 +08:00
|
|
|
dst = drm_gem_vram_kmap(gbo, false, NULL);
|
|
|
|
if (IS_ERR(dst)) {
|
|
|
|
DRM_ERROR("failed to kmap fb updates\n");
|
|
|
|
goto out;
|
|
|
|
} else if (!dst) {
|
|
|
|
dst = drm_gem_vram_kmap(gbo, true, NULL);
|
|
|
|
if (IS_ERR(dst)) {
|
2012-04-17 22:01:25 +08:00
|
|
|
DRM_ERROR("failed to kmap fb updates\n");
|
2019-05-08 16:26:26 +08:00
|
|
|
goto out;
|
2012-04-17 22:01:25 +08:00
|
|
|
}
|
|
|
|
unmap = true;
|
|
|
|
}
|
2019-05-08 16:26:26 +08:00
|
|
|
|
2013-05-02 12:52:01 +08:00
|
|
|
for (i = y; i <= y2; i++) {
|
2012-04-17 22:01:25 +08:00
|
|
|
/* assume equal stride for now */
|
2019-05-08 16:26:24 +08:00
|
|
|
src_offset = dst_offset =
|
|
|
|
i * mfbdev->mfb.base.pitches[0] + (x * bpp);
|
2019-05-08 16:26:26 +08:00
|
|
|
memcpy_toio(dst + dst_offset, mfbdev->sysram + src_offset,
|
|
|
|
(x2 - x + 1) * bpp);
|
2012-04-17 22:01:25 +08:00
|
|
|
}
|
2019-05-08 16:26:26 +08:00
|
|
|
|
2012-04-17 22:01:25 +08:00
|
|
|
if (unmap)
|
2019-05-08 16:26:26 +08:00
|
|
|
drm_gem_vram_kunmap(gbo);
|
2012-04-17 22:01:25 +08:00
|
|
|
|
2019-05-08 16:26:26 +08:00
|
|
|
out:
|
2019-06-13 15:30:38 +08:00
|
|
|
drm_gem_vram_unpin(gbo);
|
2012-04-17 22:01:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mga_fillrect(struct fb_info *info,
|
|
|
|
const struct fb_fillrect *rect)
|
|
|
|
{
|
|
|
|
struct mga_fbdev *mfbdev = info->par;
|
2015-07-31 18:51:53 +08:00
|
|
|
drm_fb_helper_sys_fillrect(info, rect);
|
2012-04-17 22:01:25 +08:00
|
|
|
mga_dirty_update(mfbdev, rect->dx, rect->dy, rect->width,
|
|
|
|
rect->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mga_copyarea(struct fb_info *info,
|
|
|
|
const struct fb_copyarea *area)
|
|
|
|
{
|
|
|
|
struct mga_fbdev *mfbdev = info->par;
|
2015-07-31 18:51:53 +08:00
|
|
|
drm_fb_helper_sys_copyarea(info, area);
|
2012-04-17 22:01:25 +08:00
|
|
|
mga_dirty_update(mfbdev, area->dx, area->dy, area->width,
|
|
|
|
area->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mga_imageblit(struct fb_info *info,
|
|
|
|
const struct fb_image *image)
|
|
|
|
{
|
|
|
|
struct mga_fbdev *mfbdev = info->par;
|
2015-07-31 18:51:53 +08:00
|
|
|
drm_fb_helper_sys_imageblit(info, image);
|
2012-04-17 22:01:25 +08:00
|
|
|
mga_dirty_update(mfbdev, image->dx, image->dy, image->width,
|
|
|
|
image->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct fb_ops mgag200fb_ops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.fb_check_var = drm_fb_helper_check_var,
|
|
|
|
.fb_set_par = drm_fb_helper_set_par,
|
|
|
|
.fb_fillrect = mga_fillrect,
|
|
|
|
.fb_copyarea = mga_copyarea,
|
|
|
|
.fb_imageblit = mga_imageblit,
|
|
|
|
.fb_pan_display = drm_fb_helper_pan_display,
|
|
|
|
.fb_blank = drm_fb_helper_blank,
|
|
|
|
.fb_setcmap = drm_fb_helper_setcmap,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int mgag200fb_create_object(struct mga_fbdev *afbdev,
|
2015-11-12 01:11:29 +08:00
|
|
|
const struct drm_mode_fb_cmd2 *mode_cmd,
|
2012-04-17 22:01:25 +08:00
|
|
|
struct drm_gem_object **gobj_p)
|
|
|
|
{
|
|
|
|
struct drm_device *dev = afbdev->helper.dev;
|
|
|
|
u32 size;
|
|
|
|
struct drm_gem_object *gobj;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
size = mode_cmd->pitches[0] * mode_cmd->height;
|
|
|
|
ret = mgag200_gem_create(dev, size, true, &gobj);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
*gobj_p = gobj;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-22 06:42:49 +08:00
|
|
|
static int mgag200fb_create(struct drm_fb_helper *helper,
|
2012-04-17 22:01:25 +08:00
|
|
|
struct drm_fb_helper_surface_size *sizes)
|
|
|
|
{
|
2014-09-15 00:40:15 +08:00
|
|
|
struct mga_fbdev *mfbdev =
|
|
|
|
container_of(helper, struct mga_fbdev, helper);
|
2012-04-17 22:01:25 +08:00
|
|
|
struct drm_device *dev = mfbdev->helper.dev;
|
|
|
|
struct drm_mode_fb_cmd2 mode_cmd;
|
|
|
|
struct mga_device *mdev = dev->dev_private;
|
|
|
|
struct fb_info *info;
|
|
|
|
struct drm_framebuffer *fb;
|
|
|
|
struct drm_gem_object *gobj = NULL;
|
|
|
|
int ret;
|
|
|
|
void *sysram;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
mode_cmd.width = sizes->surface_width;
|
|
|
|
mode_cmd.height = sizes->surface_height;
|
|
|
|
mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
|
|
|
|
|
|
|
|
mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
|
|
|
|
sizes->surface_depth);
|
|
|
|
size = mode_cmd.pitches[0] * mode_cmd.height;
|
|
|
|
|
|
|
|
ret = mgag200fb_create_object(mfbdev, &mode_cmd, &gobj);
|
|
|
|
if (ret) {
|
|
|
|
DRM_ERROR("failed to create fbcon backing object %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
sysram = vmalloc(size);
|
2016-08-15 23:03:51 +08:00
|
|
|
if (!sysram) {
|
|
|
|
ret = -ENOMEM;
|
2015-09-17 19:00:54 +08:00
|
|
|
goto err_sysram;
|
2016-08-15 23:03:51 +08:00
|
|
|
}
|
2012-04-17 22:01:25 +08:00
|
|
|
|
2015-07-31 18:51:53 +08:00
|
|
|
info = drm_fb_helper_alloc_fbi(helper);
|
2015-09-17 19:00:54 +08:00
|
|
|
if (IS_ERR(info)) {
|
|
|
|
ret = PTR_ERR(info);
|
|
|
|
goto err_alloc_fbi;
|
|
|
|
}
|
2012-04-17 22:01:25 +08:00
|
|
|
|
|
|
|
ret = mgag200_framebuffer_init(dev, &mfbdev->mfb, &mode_cmd, gobj);
|
|
|
|
if (ret)
|
2017-02-08 00:16:03 +08:00
|
|
|
goto err_alloc_fbi;
|
2012-04-17 22:01:25 +08:00
|
|
|
|
|
|
|
mfbdev->sysram = sysram;
|
|
|
|
mfbdev->size = size;
|
|
|
|
|
|
|
|
fb = &mfbdev->mfb.base;
|
|
|
|
|
|
|
|
/* setup helper */
|
|
|
|
mfbdev->helper.fb = fb;
|
|
|
|
|
|
|
|
info->fbops = &mgag200fb_ops;
|
|
|
|
|
|
|
|
/* setup aperture base/size for vesafb takeover */
|
|
|
|
info->apertures->ranges[0].base = mdev->dev->mode_config.fb_base;
|
|
|
|
info->apertures->ranges[0].size = mdev->mc.vram_size;
|
|
|
|
|
2019-03-26 21:19:59 +08:00
|
|
|
drm_fb_helper_fill_info(info, &mfbdev->helper, sizes);
|
2012-04-17 22:01:25 +08:00
|
|
|
|
|
|
|
info->screen_base = sysram;
|
|
|
|
info->screen_size = size;
|
|
|
|
info->pixmap.flags = FB_PIXMAP_SYSTEM;
|
|
|
|
|
|
|
|
DRM_DEBUG_KMS("allocated %dx%d\n",
|
|
|
|
fb->width, fb->height);
|
2015-09-17 19:00:54 +08:00
|
|
|
|
2012-04-17 22:01:25 +08:00
|
|
|
return 0;
|
2015-09-17 19:00:54 +08:00
|
|
|
|
|
|
|
err_alloc_fbi:
|
|
|
|
vfree(sysram);
|
|
|
|
err_sysram:
|
2017-08-11 20:33:00 +08:00
|
|
|
drm_gem_object_put_unlocked(gobj);
|
2015-09-17 19:00:54 +08:00
|
|
|
|
|
|
|
return ret;
|
2012-04-17 22:01:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int mga_fbdev_destroy(struct drm_device *dev,
|
|
|
|
struct mga_fbdev *mfbdev)
|
|
|
|
{
|
|
|
|
struct mga_framebuffer *mfb = &mfbdev->mfb;
|
|
|
|
|
2015-07-31 18:51:53 +08:00
|
|
|
drm_fb_helper_unregister_fbi(&mfbdev->helper);
|
2012-04-17 22:01:25 +08:00
|
|
|
|
|
|
|
if (mfb->obj) {
|
2017-08-11 20:33:00 +08:00
|
|
|
drm_gem_object_put_unlocked(mfb->obj);
|
2012-04-17 22:01:25 +08:00
|
|
|
mfb->obj = NULL;
|
|
|
|
}
|
|
|
|
drm_fb_helper_fini(&mfbdev->helper);
|
|
|
|
vfree(mfbdev->sysram);
|
drm: revamp framebuffer cleanup interfaces
We have two classes of framebuffer
- Created by the driver (atm only for fbdev), and the driver holds
onto the last reference count until destruction.
- Created by userspace and associated with a given fd. These
framebuffers will be reaped when their assoiciated fb is closed.
Now these two cases are set up differently, the framebuffers are on
different lists and hence destruction needs to clean up different
things. Also, for userspace framebuffers we remove them from any
current usage, whereas for internal framebuffers it is assumed that
the driver has done this already.
Long story short, we need two different ways to cleanup such drivers.
Three functions are involved in total:
- drm_framebuffer_remove: Convenience function which removes the fb
from all active usage and then drops the passed-in reference.
- drm_framebuffer_unregister_private: Will remove driver-private
framebuffers from relevant lists and drop the corresponding
references. Should be called for driver-private framebuffers before
dropping the last reference (or like for a lot of the drivers where
the fbdev is embedded someplace else, before doing the cleanup
manually).
- drm_framebuffer_cleanup: Final cleanup for both classes of fbs,
should be called by the driver's ->destroy callback once the last
reference is gone.
This patch just rolls out the new interfaces and updates all drivers
(by adding calls to drm_framebuffer_unregister_private at all the
right places)- no functional changes yet. Follow-on patches will move
drm core code around and update the lifetime management for
framebuffers, so that we are no longer required to keep framebuffers
alive by locking mode_config.mutex.
I've also updated the kerneldoc already.
vmwgfx seems to again be a bit special, at least I haven't figured out
how the fbdev support in that driver works. It smells like it's
external though.
v2: The i915 driver creates another private framebuffer in the
load-detect code. Adjust its cleanup code, too.
Reviewed-by: Rob Clark <rob@ti.com>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
2012-12-11 03:42:17 +08:00
|
|
|
drm_framebuffer_unregister_private(&mfb->base);
|
2012-04-17 22:01:25 +08:00
|
|
|
drm_framebuffer_cleanup(&mfb->base);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-27 23:19:23 +08:00
|
|
|
static const struct drm_fb_helper_funcs mga_fb_helper_funcs = {
|
2013-01-22 06:42:49 +08:00
|
|
|
.fb_probe = mgag200fb_create,
|
2012-04-17 22:01:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int mgag200_fbdev_init(struct mga_device *mdev)
|
|
|
|
{
|
|
|
|
struct mga_fbdev *mfbdev;
|
|
|
|
int ret;
|
2014-01-21 14:47:46 +08:00
|
|
|
int bpp_sel = 32;
|
|
|
|
|
|
|
|
/* prefer 16bpp on low end gpus with limited VRAM */
|
|
|
|
if (IS_G200_SE(mdev) && mdev->mc.vram_size < (2048*1024))
|
|
|
|
bpp_sel = 16;
|
2012-04-17 22:01:25 +08:00
|
|
|
|
2013-04-06 00:15:30 +08:00
|
|
|
mfbdev = devm_kzalloc(mdev->dev->dev, sizeof(struct mga_fbdev), GFP_KERNEL);
|
2012-04-17 22:01:25 +08:00
|
|
|
if (!mfbdev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
mdev->mfbdev = mfbdev;
|
2013-05-02 12:52:01 +08:00
|
|
|
spin_lock_init(&mfbdev->dirty_lock);
|
2012-04-17 22:01:25 +08:00
|
|
|
|
2014-06-27 23:19:24 +08:00
|
|
|
drm_fb_helper_prepare(mdev->dev, &mfbdev->helper, &mga_fb_helper_funcs);
|
|
|
|
|
2012-04-17 22:01:25 +08:00
|
|
|
ret = drm_fb_helper_init(mdev->dev, &mfbdev->helper,
|
drm: Rely on mode_config data for fb_helper initialization
Instead of receiving the num_crts as a parameter, we can read it
directly from the mode_config structure. I audited the drivers that
invoke this helper and I believe all of them initialize the mode_config
struct accordingly, prior to calling the fb_helper.
I used the following coccinelle hack to make this transformation, except
for the function headers and comment updates. The first and second
rules are split because I couldn't find a way to remove the unused
temporary variables at the same time I removed the parameter.
// <smpl>
@r@
expression A,B,D,E;
identifier C;
@@
(
- drm_fb_helper_init(A,B,C,D)
+ drm_fb_helper_init(A,B,D)
|
- drm_fbdev_cma_init_with_funcs(A,B,C,D,E)
+ drm_fbdev_cma_init_with_funcs(A,B,D,E)
|
- drm_fbdev_cma_init(A,B,C,D)
+ drm_fbdev_cma_init(A,B,D)
)
@@
expression A,B,C,D,E;
@@
(
- drm_fb_helper_init(A,B,C,D)
+ drm_fb_helper_init(A,B,D)
|
- drm_fbdev_cma_init_with_funcs(A,B,C,D,E)
+ drm_fbdev_cma_init_with_funcs(A,B,D,E)
|
- drm_fbdev_cma_init(A,B,C,D)
+ drm_fbdev_cma_init(A,B,D)
)
@@
identifier r.C;
type T;
expression V;
@@
- T C;
<...
when != C
- C = V;
...>
// </smpl>
Changes since v1:
- Rebased on top of the tip of drm-misc-next.
- Remove mention to sti since a proper fix got merged.
Suggested-by: Daniel Vetter <daniel.vetter@intel.com>
Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.co.uk>
Reviewed-by: Eric Anholt <eric@anholt.net>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/20170202162640.27261-1-krisman@collabora.co.uk
2017-02-03 00:26:40 +08:00
|
|
|
MGAG200FB_CONN_LIMIT);
|
2013-04-06 00:15:30 +08:00
|
|
|
if (ret)
|
2015-09-17 19:00:54 +08:00
|
|
|
goto err_fb_helper;
|
2013-04-06 00:15:30 +08:00
|
|
|
|
2014-12-19 18:21:32 +08:00
|
|
|
ret = drm_fb_helper_single_add_all_connectors(&mfbdev->helper);
|
|
|
|
if (ret)
|
2015-09-17 19:00:54 +08:00
|
|
|
goto err_fb_setup;
|
2013-01-21 06:12:54 +08:00
|
|
|
|
|
|
|
/* disable all the possible outputs/crtcs before entering KMS mode */
|
|
|
|
drm_helper_disable_unused_functions(mdev->dev);
|
|
|
|
|
2014-12-19 18:21:32 +08:00
|
|
|
ret = drm_fb_helper_initial_config(&mfbdev->helper, bpp_sel);
|
|
|
|
if (ret)
|
2015-09-17 19:00:54 +08:00
|
|
|
goto err_fb_setup;
|
2012-04-17 22:01:25 +08:00
|
|
|
|
|
|
|
return 0;
|
2014-12-19 18:21:32 +08:00
|
|
|
|
2015-09-17 19:00:54 +08:00
|
|
|
err_fb_setup:
|
2014-12-19 18:21:32 +08:00
|
|
|
drm_fb_helper_fini(&mfbdev->helper);
|
2015-09-17 19:00:54 +08:00
|
|
|
err_fb_helper:
|
|
|
|
mdev->mfbdev = NULL;
|
|
|
|
|
2014-12-19 18:21:32 +08:00
|
|
|
return ret;
|
2012-04-17 22:01:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mgag200_fbdev_fini(struct mga_device *mdev)
|
|
|
|
{
|
|
|
|
if (!mdev->mfbdev)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mga_fbdev_destroy(mdev->dev, mdev->mfbdev);
|
|
|
|
}
|