2012-04-17 21:12:29 +08:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* Dave Airlie
|
|
|
|
*/
|
|
|
|
#include <linux/module.h>
|
2012-10-03 01:01:07 +08:00
|
|
|
#include <drm/drmP.h>
|
|
|
|
#include <drm/drm_fb_helper.h>
|
2013-01-21 06:12:54 +08:00
|
|
|
#include <drm/drm_crtc_helper.h>
|
2012-04-17 21:12:29 +08:00
|
|
|
|
|
|
|
#include "cirrus_drv.h"
|
|
|
|
|
|
|
|
static void cirrus_dirty_update(struct cirrus_fbdev *afbdev,
|
|
|
|
int x, int y, int width, int height)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct drm_gem_object *obj;
|
|
|
|
struct cirrus_bo *bo;
|
|
|
|
int src_offset, dst_offset;
|
|
|
|
int bpp = (afbdev->gfb.base.bits_per_pixel + 7)/8;
|
2013-06-27 19:38:24 +08:00
|
|
|
int ret = -EBUSY;
|
2012-04-17 21:12:29 +08:00
|
|
|
bool unmap = false;
|
2013-05-02 14:45:02 +08:00
|
|
|
bool store_for_later = false;
|
|
|
|
int x2, y2;
|
|
|
|
unsigned long flags;
|
2012-04-17 21:12:29 +08:00
|
|
|
|
|
|
|
obj = afbdev->gfb.obj;
|
|
|
|
bo = gem_to_cirrus_bo(obj);
|
|
|
|
|
2013-05-02 14:45:02 +08:00
|
|
|
/*
|
|
|
|
* try and reserve the BO, if we fail with busy
|
|
|
|
* then the BO is being moved and we should
|
|
|
|
* store up the damage until later.
|
|
|
|
*/
|
2014-02-05 12:47:45 +08:00
|
|
|
if (drm_can_sleep())
|
2013-06-27 19:38:24 +08:00
|
|
|
ret = cirrus_bo_reserve(bo, true);
|
2012-04-17 21:12:29 +08:00
|
|
|
if (ret) {
|
2013-05-02 14:45:02 +08:00
|
|
|
if (ret != -EBUSY)
|
|
|
|
return;
|
|
|
|
store_for_later = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
x2 = x + width - 1;
|
|
|
|
y2 = y + height - 1;
|
|
|
|
spin_lock_irqsave(&afbdev->dirty_lock, flags);
|
|
|
|
|
|
|
|
if (afbdev->y1 < y)
|
|
|
|
y = afbdev->y1;
|
|
|
|
if (afbdev->y2 > y2)
|
|
|
|
y2 = afbdev->y2;
|
|
|
|
if (afbdev->x1 < x)
|
|
|
|
x = afbdev->x1;
|
|
|
|
if (afbdev->x2 > x2)
|
|
|
|
x2 = afbdev->x2;
|
|
|
|
|
|
|
|
if (store_for_later) {
|
|
|
|
afbdev->x1 = x;
|
|
|
|
afbdev->x2 = x2;
|
|
|
|
afbdev->y1 = y;
|
|
|
|
afbdev->y2 = y2;
|
|
|
|
spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
|
2012-04-17 21:12:29 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-02 14:45:02 +08:00
|
|
|
afbdev->x1 = afbdev->y1 = INT_MAX;
|
|
|
|
afbdev->x2 = afbdev->y2 = 0;
|
|
|
|
spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
|
|
|
|
|
2012-04-17 21:12:29 +08:00
|
|
|
if (!bo->kmap.virtual) {
|
|
|
|
ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
|
|
|
|
if (ret) {
|
|
|
|
DRM_ERROR("failed to kmap fb updates\n");
|
|
|
|
cirrus_bo_unreserve(bo);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
unmap = true;
|
|
|
|
}
|
|
|
|
for (i = y; i < y + height; i++) {
|
|
|
|
/* assume equal stride for now */
|
|
|
|
src_offset = dst_offset = i * afbdev->gfb.base.pitches[0] + (x * bpp);
|
|
|
|
memcpy_toio(bo->kmap.virtual + src_offset, afbdev->sysram + src_offset, width * bpp);
|
|
|
|
|
|
|
|
}
|
|
|
|
if (unmap)
|
|
|
|
ttm_bo_kunmap(&bo->kmap);
|
|
|
|
|
|
|
|
cirrus_bo_unreserve(bo);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cirrus_fillrect(struct fb_info *info,
|
|
|
|
const struct fb_fillrect *rect)
|
|
|
|
{
|
|
|
|
struct cirrus_fbdev *afbdev = info->par;
|
2015-07-31 18:51:44 +08:00
|
|
|
drm_fb_helper_sys_fillrect(info, rect);
|
2012-04-17 21:12:29 +08:00
|
|
|
cirrus_dirty_update(afbdev, rect->dx, rect->dy, rect->width,
|
|
|
|
rect->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cirrus_copyarea(struct fb_info *info,
|
|
|
|
const struct fb_copyarea *area)
|
|
|
|
{
|
|
|
|
struct cirrus_fbdev *afbdev = info->par;
|
2015-07-31 18:51:44 +08:00
|
|
|
drm_fb_helper_sys_copyarea(info, area);
|
2012-04-17 21:12:29 +08:00
|
|
|
cirrus_dirty_update(afbdev, area->dx, area->dy, area->width,
|
|
|
|
area->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cirrus_imageblit(struct fb_info *info,
|
|
|
|
const struct fb_image *image)
|
|
|
|
{
|
|
|
|
struct cirrus_fbdev *afbdev = info->par;
|
2015-07-31 18:51:44 +08:00
|
|
|
drm_fb_helper_sys_imageblit(info, image);
|
2012-04-17 21:12:29 +08:00
|
|
|
cirrus_dirty_update(afbdev, image->dx, image->dy, image->width,
|
|
|
|
image->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct fb_ops cirrusfb_ops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.fb_check_var = drm_fb_helper_check_var,
|
|
|
|
.fb_set_par = drm_fb_helper_set_par,
|
|
|
|
.fb_fillrect = cirrus_fillrect,
|
|
|
|
.fb_copyarea = cirrus_copyarea,
|
|
|
|
.fb_imageblit = cirrus_imageblit,
|
|
|
|
.fb_pan_display = drm_fb_helper_pan_display,
|
|
|
|
.fb_blank = drm_fb_helper_blank,
|
|
|
|
.fb_setcmap = drm_fb_helper_setcmap,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int cirrusfb_create_object(struct cirrus_fbdev *afbdev,
|
2015-11-12 01:11:29 +08:00
|
|
|
const struct drm_mode_fb_cmd2 *mode_cmd,
|
2012-04-17 21:12:29 +08:00
|
|
|
struct drm_gem_object **gobj_p)
|
|
|
|
{
|
|
|
|
struct drm_device *dev = afbdev->helper.dev;
|
2014-10-30 02:04:24 +08:00
|
|
|
struct cirrus_device *cdev = dev->dev_private;
|
2012-04-17 21:12:29 +08:00
|
|
|
u32 bpp, depth;
|
|
|
|
u32 size;
|
|
|
|
struct drm_gem_object *gobj;
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
|
|
|
|
|
2014-10-30 02:04:24 +08:00
|
|
|
if (!cirrus_check_framebuffer(cdev, mode_cmd->width, mode_cmd->height,
|
|
|
|
bpp, mode_cmd->pitches[0]))
|
2012-04-17 21:12:29 +08:00
|
|
|
return -EINVAL;
|
2014-10-30 02:04:24 +08:00
|
|
|
|
2012-04-17 21:12:29 +08:00
|
|
|
size = mode_cmd->pitches[0] * mode_cmd->height;
|
|
|
|
ret = cirrus_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 cirrusfb_create(struct drm_fb_helper *helper,
|
2012-04-17 21:12:29 +08:00
|
|
|
struct drm_fb_helper_surface_size *sizes)
|
|
|
|
{
|
2014-09-15 00:40:14 +08:00
|
|
|
struct cirrus_fbdev *gfbdev =
|
|
|
|
container_of(helper, struct cirrus_fbdev, helper);
|
2012-04-17 21:12:29 +08:00
|
|
|
struct cirrus_device *cdev = gfbdev->helper.dev->dev_private;
|
|
|
|
struct fb_info *info;
|
|
|
|
struct drm_framebuffer *fb;
|
|
|
|
struct drm_mode_fb_cmd2 mode_cmd;
|
|
|
|
void *sysram;
|
|
|
|
struct drm_gem_object *gobj = NULL;
|
|
|
|
struct cirrus_bo *bo = NULL;
|
|
|
|
int size, ret;
|
|
|
|
|
|
|
|
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 = cirrusfb_create_object(gfbdev, &mode_cmd, &gobj);
|
|
|
|
if (ret) {
|
|
|
|
DRM_ERROR("failed to create fbcon backing object %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bo = gem_to_cirrus_bo(gobj);
|
|
|
|
|
|
|
|
sysram = vmalloc(size);
|
|
|
|
if (!sysram)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2015-07-31 18:51:44 +08:00
|
|
|
info = drm_fb_helper_alloc_fbi(helper);
|
|
|
|
if (IS_ERR(info))
|
|
|
|
return PTR_ERR(info);
|
2012-04-17 21:12:29 +08:00
|
|
|
|
|
|
|
info->par = gfbdev;
|
|
|
|
|
|
|
|
ret = cirrus_framebuffer_init(cdev->dev, &gfbdev->gfb, &mode_cmd, gobj);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
gfbdev->sysram = sysram;
|
|
|
|
gfbdev->size = size;
|
|
|
|
|
|
|
|
fb = &gfbdev->gfb.base;
|
|
|
|
if (!fb) {
|
|
|
|
DRM_INFO("fb is NULL\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setup helper */
|
|
|
|
gfbdev->helper.fb = fb;
|
|
|
|
|
|
|
|
strcpy(info->fix.id, "cirrusdrmfb");
|
|
|
|
|
|
|
|
info->flags = FBINFO_DEFAULT;
|
|
|
|
info->fbops = &cirrusfb_ops;
|
|
|
|
|
|
|
|
drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
|
|
|
|
drm_fb_helper_fill_var(info, &gfbdev->helper, sizes->fb_width,
|
|
|
|
sizes->fb_height);
|
|
|
|
|
|
|
|
/* setup aperture base/size for vesafb takeover */
|
|
|
|
info->apertures->ranges[0].base = cdev->dev->mode_config.fb_base;
|
|
|
|
info->apertures->ranges[0].size = cdev->mc.vram_size;
|
|
|
|
|
2014-01-09 17:05:07 +08:00
|
|
|
info->fix.smem_start = cdev->dev->mode_config.fb_base;
|
|
|
|
info->fix.smem_len = cdev->mc.vram_size;
|
|
|
|
|
2012-04-17 21:12:29 +08:00
|
|
|
info->screen_base = sysram;
|
|
|
|
info->screen_size = size;
|
|
|
|
|
|
|
|
info->fix.mmio_start = 0;
|
|
|
|
info->fix.mmio_len = 0;
|
|
|
|
|
|
|
|
DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
|
|
|
|
DRM_INFO("vram aper at 0x%lX\n", (unsigned long)info->fix.smem_start);
|
|
|
|
DRM_INFO("size %lu\n", (unsigned long)info->fix.smem_len);
|
|
|
|
DRM_INFO("fb depth is %d\n", fb->depth);
|
|
|
|
DRM_INFO(" pitch is %d\n", fb->pitches[0]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cirrus_fbdev_destroy(struct drm_device *dev,
|
|
|
|
struct cirrus_fbdev *gfbdev)
|
|
|
|
{
|
|
|
|
struct cirrus_framebuffer *gfb = &gfbdev->gfb;
|
|
|
|
|
2015-07-31 18:51:44 +08:00
|
|
|
drm_fb_helper_unregister_fbi(&gfbdev->helper);
|
|
|
|
drm_fb_helper_release_fbi(&gfbdev->helper);
|
2012-04-17 21:12:29 +08:00
|
|
|
|
|
|
|
if (gfb->obj) {
|
|
|
|
drm_gem_object_unreference_unlocked(gfb->obj);
|
|
|
|
gfb->obj = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
vfree(gfbdev->sysram);
|
|
|
|
drm_fb_helper_fini(&gfbdev->helper);
|
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(&gfb->base);
|
2012-04-17 21:12:29 +08:00
|
|
|
drm_framebuffer_cleanup(&gfb->base);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-27 23:19:23 +08:00
|
|
|
static const struct drm_fb_helper_funcs cirrus_fb_helper_funcs = {
|
2012-04-17 21:12:29 +08:00
|
|
|
.gamma_set = cirrus_crtc_fb_gamma_set,
|
|
|
|
.gamma_get = cirrus_crtc_fb_gamma_get,
|
2013-01-22 06:42:49 +08:00
|
|
|
.fb_probe = cirrusfb_create,
|
2012-04-17 21:12:29 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int cirrus_fbdev_init(struct cirrus_device *cdev)
|
|
|
|
{
|
|
|
|
struct cirrus_fbdev *gfbdev;
|
|
|
|
int ret;
|
|
|
|
int bpp_sel = 24;
|
|
|
|
|
|
|
|
/*bpp_sel = 8;*/
|
|
|
|
gfbdev = kzalloc(sizeof(struct cirrus_fbdev), GFP_KERNEL);
|
|
|
|
if (!gfbdev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
cdev->mode_info.gfbdev = gfbdev;
|
2013-05-02 14:45:02 +08:00
|
|
|
spin_lock_init(&gfbdev->dirty_lock);
|
2012-04-17 21:12:29 +08:00
|
|
|
|
2014-06-27 23:19:24 +08:00
|
|
|
drm_fb_helper_prepare(cdev->dev, &gfbdev->helper,
|
|
|
|
&cirrus_fb_helper_funcs);
|
|
|
|
|
2012-04-17 21:12:29 +08:00
|
|
|
ret = drm_fb_helper_init(cdev->dev, &gfbdev->helper,
|
|
|
|
cdev->num_crtc, CIRRUSFB_CONN_LIMIT);
|
2014-12-19 18:21:32 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = drm_fb_helper_single_add_all_connectors(&gfbdev->helper);
|
|
|
|
if (ret)
|
2012-04-17 21:12:29 +08:00
|
|
|
return ret;
|
2013-01-21 06:12:54 +08:00
|
|
|
|
|
|
|
/* disable all the possible outputs/crtcs before entering KMS mode */
|
|
|
|
drm_helper_disable_unused_functions(cdev->dev);
|
2012-04-17 21:12:29 +08:00
|
|
|
|
2014-12-19 18:21:32 +08:00
|
|
|
return drm_fb_helper_initial_config(&gfbdev->helper, bpp_sel);
|
2012-04-17 21:12:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cirrus_fbdev_fini(struct cirrus_device *cdev)
|
|
|
|
{
|
|
|
|
if (!cdev->mode_info.gfbdev)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cirrus_fbdev_destroy(cdev->dev, cdev->mode_info.gfbdev);
|
|
|
|
kfree(cdev->mode_info.gfbdev);
|
|
|
|
cdev->mode_info.gfbdev = NULL;
|
|
|
|
}
|