linux/drivers/gpu/drm/ast/ast_fb.c
<<
>>
Prefs
   1/*
   2 * Copyright 2012 Red Hat Inc.
   3 *
   4 * Permission is hereby granted, free of charge, to any person obtaining a
   5 * copy of this software and associated documentation files (the
   6 * "Software"), to deal in the Software without restriction, including
   7 * without limitation the rights to use, copy, modify, merge, publish,
   8 * distribute, sub license, and/or sell copies of the Software, and to
   9 * permit persons to whom the Software is furnished to do so, subject to
  10 * the following conditions:
  11 *
  12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  15 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  18 * USE OR OTHER DEALINGS IN THE SOFTWARE.
  19 *
  20 * The above copyright notice and this permission notice (including the
  21 * next paragraph) shall be included in all copies or substantial portions
  22 * of the Software.
  23 *
  24 */
  25/*
  26 * Authors: Dave Airlie <airlied@redhat.com>
  27 */
  28#include <linux/module.h>
  29#include <linux/kernel.h>
  30#include <linux/errno.h>
  31#include <linux/string.h>
  32#include <linux/mm.h>
  33#include <linux/tty.h>
  34#include <linux/sysrq.h>
  35#include <linux/delay.h>
  36#include <linux/init.h>
  37
  38
  39#include <drm/drmP.h>
  40#include <drm/drm_crtc.h>
  41#include <drm/drm_fb_helper.h>
  42#include <drm/drm_util.h>
  43#include <drm/drm_crtc_helper.h>
  44
  45#include "ast_drv.h"
  46
  47static void ast_dirty_update(struct ast_fbdev *afbdev,
  48                             int x, int y, int width, int height)
  49{
  50        int i;
  51        struct drm_gem_vram_object *gbo;
  52        int src_offset, dst_offset;
  53        int bpp = afbdev->afb.base.format->cpp[0];
  54        int ret;
  55        u8 *dst;
  56        bool unmap = false;
  57        bool store_for_later = false;
  58        int x2, y2;
  59        unsigned long flags;
  60
  61        gbo = drm_gem_vram_of_gem(afbdev->afb.obj);
  62
  63        if (drm_can_sleep()) {
  64                /* We pin the BO so it won't be moved during the
  65                 * update. The actual location, video RAM or system
  66                 * memory, is not important.
  67                 */
  68                ret = drm_gem_vram_pin(gbo, 0);
  69                if (ret) {
  70                        if (ret != -EBUSY)
  71                                return;
  72                        store_for_later = true;
  73                }
  74        } else {
  75                store_for_later = true;
  76        }
  77
  78        x2 = x + width - 1;
  79        y2 = y + height - 1;
  80        spin_lock_irqsave(&afbdev->dirty_lock, flags);
  81
  82        if (afbdev->y1 < y)
  83                y = afbdev->y1;
  84        if (afbdev->y2 > y2)
  85                y2 = afbdev->y2;
  86        if (afbdev->x1 < x)
  87                x = afbdev->x1;
  88        if (afbdev->x2 > x2)
  89                x2 = afbdev->x2;
  90
  91        if (store_for_later) {
  92                afbdev->x1 = x;
  93                afbdev->x2 = x2;
  94                afbdev->y1 = y;
  95                afbdev->y2 = y2;
  96                spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
  97                return;
  98        }
  99
 100        afbdev->x1 = afbdev->y1 = INT_MAX;
 101        afbdev->x2 = afbdev->y2 = 0;
 102        spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
 103
 104        dst = drm_gem_vram_kmap(gbo, false, NULL);
 105        if (IS_ERR(dst)) {
 106                DRM_ERROR("failed to kmap fb updates\n");
 107                goto out;
 108        } else if (!dst) {
 109                dst = drm_gem_vram_kmap(gbo, true, NULL);
 110                if (IS_ERR(dst)) {
 111                        DRM_ERROR("failed to kmap fb updates\n");
 112                        goto out;
 113                }
 114                unmap = true;
 115        }
 116
 117        for (i = y; i <= y2; i++) {
 118                /* assume equal stride for now */
 119                src_offset = dst_offset =
 120                        i * afbdev->afb.base.pitches[0] + (x * bpp);
 121                memcpy_toio(dst + dst_offset, afbdev->sysram + src_offset,
 122                            (x2 - x + 1) * bpp);
 123        }
 124
 125        if (unmap)
 126                drm_gem_vram_kunmap(gbo);
 127
 128out:
 129        drm_gem_vram_unpin(gbo);
 130}
 131
 132static void ast_fillrect(struct fb_info *info,
 133                         const struct fb_fillrect *rect)
 134{
 135        struct ast_fbdev *afbdev = info->par;
 136        drm_fb_helper_sys_fillrect(info, rect);
 137        ast_dirty_update(afbdev, rect->dx, rect->dy, rect->width,
 138                         rect->height);
 139}
 140
 141static void ast_copyarea(struct fb_info *info,
 142                         const struct fb_copyarea *area)
 143{
 144        struct ast_fbdev *afbdev = info->par;
 145        drm_fb_helper_sys_copyarea(info, area);
 146        ast_dirty_update(afbdev, area->dx, area->dy, area->width,
 147                         area->height);
 148}
 149
 150static void ast_imageblit(struct fb_info *info,
 151                          const struct fb_image *image)
 152{
 153        struct ast_fbdev *afbdev = info->par;
 154        drm_fb_helper_sys_imageblit(info, image);
 155        ast_dirty_update(afbdev, image->dx, image->dy, image->width,
 156                         image->height);
 157}
 158
 159static struct fb_ops astfb_ops = {
 160        .owner = THIS_MODULE,
 161        .fb_check_var = drm_fb_helper_check_var,
 162        .fb_set_par = drm_fb_helper_set_par,
 163        .fb_fillrect = ast_fillrect,
 164        .fb_copyarea = ast_copyarea,
 165        .fb_imageblit = ast_imageblit,
 166        .fb_pan_display = drm_fb_helper_pan_display,
 167        .fb_blank = drm_fb_helper_blank,
 168        .fb_setcmap = drm_fb_helper_setcmap,
 169};
 170
 171static int astfb_create_object(struct ast_fbdev *afbdev,
 172                               const struct drm_mode_fb_cmd2 *mode_cmd,
 173                               struct drm_gem_object **gobj_p)
 174{
 175        struct drm_device *dev = afbdev->helper.dev;
 176        u32 size;
 177        struct drm_gem_object *gobj;
 178        int ret = 0;
 179
 180        size = mode_cmd->pitches[0] * mode_cmd->height;
 181        ret = ast_gem_create(dev, size, true, &gobj);
 182        if (ret)
 183                return ret;
 184
 185        *gobj_p = gobj;
 186        return ret;
 187}
 188
 189static int astfb_create(struct drm_fb_helper *helper,
 190                        struct drm_fb_helper_surface_size *sizes)
 191{
 192        struct ast_fbdev *afbdev =
 193                container_of(helper, struct ast_fbdev, helper);
 194        struct drm_device *dev = afbdev->helper.dev;
 195        struct drm_mode_fb_cmd2 mode_cmd;
 196        struct drm_framebuffer *fb;
 197        struct fb_info *info;
 198        int size, ret;
 199        void *sysram;
 200        struct drm_gem_object *gobj = NULL;
 201        mode_cmd.width = sizes->surface_width;
 202        mode_cmd.height = sizes->surface_height;
 203        mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7)/8);
 204
 205        mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
 206                                                          sizes->surface_depth);
 207
 208        size = mode_cmd.pitches[0] * mode_cmd.height;
 209
 210        ret = astfb_create_object(afbdev, &mode_cmd, &gobj);
 211        if (ret) {
 212                DRM_ERROR("failed to create fbcon backing object %d\n", ret);
 213                return ret;
 214        }
 215
 216        sysram = vmalloc(size);
 217        if (!sysram)
 218                return -ENOMEM;
 219
 220        info = drm_fb_helper_alloc_fbi(helper);
 221        if (IS_ERR(info)) {
 222                ret = PTR_ERR(info);
 223                goto out;
 224        }
 225        ret = ast_framebuffer_init(dev, &afbdev->afb, &mode_cmd, gobj);
 226        if (ret)
 227                goto out;
 228
 229        afbdev->sysram = sysram;
 230        afbdev->size = size;
 231
 232        fb = &afbdev->afb.base;
 233        afbdev->helper.fb = fb;
 234
 235        info->fbops = &astfb_ops;
 236
 237        info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
 238        info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
 239
 240        drm_fb_helper_fill_info(info, &afbdev->helper, sizes);
 241
 242        info->screen_base = sysram;
 243        info->screen_size = size;
 244
 245        info->pixmap.flags = FB_PIXMAP_SYSTEM;
 246
 247        DRM_DEBUG_KMS("allocated %dx%d\n",
 248                      fb->width, fb->height);
 249
 250        return 0;
 251
 252out:
 253        vfree(sysram);
 254        return ret;
 255}
 256
 257static const struct drm_fb_helper_funcs ast_fb_helper_funcs = {
 258        .fb_probe = astfb_create,
 259};
 260
 261static void ast_fbdev_destroy(struct drm_device *dev,
 262                              struct ast_fbdev *afbdev)
 263{
 264        struct ast_framebuffer *afb = &afbdev->afb;
 265
 266        drm_helper_force_disable_all(dev);
 267        drm_fb_helper_unregister_fbi(&afbdev->helper);
 268
 269        if (afb->obj) {
 270                drm_gem_object_put_unlocked(afb->obj);
 271                afb->obj = NULL;
 272        }
 273        drm_fb_helper_fini(&afbdev->helper);
 274
 275        vfree(afbdev->sysram);
 276        drm_framebuffer_unregister_private(&afb->base);
 277        drm_framebuffer_cleanup(&afb->base);
 278}
 279
 280int ast_fbdev_init(struct drm_device *dev)
 281{
 282        struct ast_private *ast = dev->dev_private;
 283        struct ast_fbdev *afbdev;
 284        int ret;
 285
 286        afbdev = kzalloc(sizeof(struct ast_fbdev), GFP_KERNEL);
 287        if (!afbdev)
 288                return -ENOMEM;
 289
 290        ast->fbdev = afbdev;
 291        spin_lock_init(&afbdev->dirty_lock);
 292
 293        drm_fb_helper_prepare(dev, &afbdev->helper, &ast_fb_helper_funcs);
 294
 295        ret = drm_fb_helper_init(dev, &afbdev->helper, 1);
 296        if (ret)
 297                goto free;
 298
 299        ret = drm_fb_helper_single_add_all_connectors(&afbdev->helper);
 300        if (ret)
 301                goto fini;
 302
 303        /* disable all the possible outputs/crtcs before entering KMS mode */
 304        drm_helper_disable_unused_functions(dev);
 305
 306        ret = drm_fb_helper_initial_config(&afbdev->helper, 32);
 307        if (ret)
 308                goto fini;
 309
 310        return 0;
 311
 312fini:
 313        drm_fb_helper_fini(&afbdev->helper);
 314free:
 315        kfree(afbdev);
 316        return ret;
 317}
 318
 319void ast_fbdev_fini(struct drm_device *dev)
 320{
 321        struct ast_private *ast = dev->dev_private;
 322
 323        if (!ast->fbdev)
 324                return;
 325
 326        ast_fbdev_destroy(dev, ast->fbdev);
 327        kfree(ast->fbdev);
 328        ast->fbdev = NULL;
 329}
 330
 331void ast_fbdev_set_suspend(struct drm_device *dev, int state)
 332{
 333        struct ast_private *ast = dev->dev_private;
 334
 335        if (!ast->fbdev)
 336                return;
 337
 338        drm_fb_helper_set_suspend(&ast->fbdev->helper, state);
 339}
 340
 341void ast_fbdev_set_base(struct ast_private *ast, unsigned long gpu_addr)
 342{
 343        ast->fbdev->helper.fbdev->fix.smem_start =
 344                ast->fbdev->helper.fbdev->apertures->ranges[0].base + gpu_addr;
 345        ast->fbdev->helper.fbdev->fix.smem_len = ast->vram_size - gpu_addr;
 346}
 347