linux/drivers/gpu/drm/cirrus/cirrus_main.c
<<
>>
Prefs
   1/*
   2 * Copyright 2012 Red Hat
   3 *
   4 * This file is subject to the terms and conditions of the GNU General
   5 * Public License version 2. See the file COPYING in the main
   6 * directory of this archive for more details.
   7 *
   8 * Authors: Matthew Garrett
   9 *          Dave Airlie
  10 */
  11#include <drm/drmP.h>
  12#include <drm/drm_crtc_helper.h>
  13#include <drm/drm_gem_framebuffer_helper.h>
  14
  15#include "cirrus_drv.h"
  16
  17static const struct drm_framebuffer_funcs cirrus_fb_funcs = {
  18        .create_handle = drm_gem_fb_create_handle,
  19        .destroy = drm_gem_fb_destroy,
  20};
  21
  22int cirrus_framebuffer_init(struct drm_device *dev,
  23                            struct drm_framebuffer *gfb,
  24                            const struct drm_mode_fb_cmd2 *mode_cmd,
  25                            struct drm_gem_object *obj)
  26{
  27        int ret;
  28
  29        drm_helper_mode_fill_fb_struct(dev, gfb, mode_cmd);
  30        gfb->obj[0] = obj;
  31        ret = drm_framebuffer_init(dev, gfb, &cirrus_fb_funcs);
  32        if (ret) {
  33                DRM_ERROR("drm_framebuffer_init failed: %d\n", ret);
  34                return ret;
  35        }
  36        return 0;
  37}
  38
  39static struct drm_framebuffer *
  40cirrus_user_framebuffer_create(struct drm_device *dev,
  41                               struct drm_file *filp,
  42                               const struct drm_mode_fb_cmd2 *mode_cmd)
  43{
  44        struct cirrus_device *cdev = dev->dev_private;
  45        struct drm_gem_object *obj;
  46        struct drm_framebuffer *fb;
  47        u32 bpp;
  48        int ret;
  49
  50        bpp = drm_format_plane_cpp(mode_cmd->pixel_format, 0) * 8;
  51
  52        if (!cirrus_check_framebuffer(cdev, mode_cmd->width, mode_cmd->height,
  53                                      bpp, mode_cmd->pitches[0]))
  54                return ERR_PTR(-EINVAL);
  55
  56        obj = drm_gem_object_lookup(filp, mode_cmd->handles[0]);
  57        if (obj == NULL)
  58                return ERR_PTR(-ENOENT);
  59
  60        fb = kzalloc(sizeof(*fb), GFP_KERNEL);
  61        if (!fb) {
  62                drm_gem_object_put_unlocked(obj);
  63                return ERR_PTR(-ENOMEM);
  64        }
  65
  66        ret = cirrus_framebuffer_init(dev, fb, mode_cmd, obj);
  67        if (ret) {
  68                drm_gem_object_put_unlocked(obj);
  69                kfree(fb);
  70                return ERR_PTR(ret);
  71        }
  72        return fb;
  73}
  74
  75static const struct drm_mode_config_funcs cirrus_mode_funcs = {
  76        .fb_create = cirrus_user_framebuffer_create,
  77};
  78
  79/* Unmap the framebuffer from the core and release the memory */
  80static void cirrus_vram_fini(struct cirrus_device *cdev)
  81{
  82        iounmap(cdev->rmmio);
  83        cdev->rmmio = NULL;
  84        if (cdev->mc.vram_base)
  85                release_mem_region(cdev->mc.vram_base, cdev->mc.vram_size);
  86}
  87
  88/* Map the framebuffer from the card and configure the core */
  89static int cirrus_vram_init(struct cirrus_device *cdev)
  90{
  91        /* BAR 0 is VRAM */
  92        cdev->mc.vram_base = pci_resource_start(cdev->dev->pdev, 0);
  93        cdev->mc.vram_size = pci_resource_len(cdev->dev->pdev, 0);
  94
  95        if (!request_mem_region(cdev->mc.vram_base, cdev->mc.vram_size,
  96                                "cirrusdrmfb_vram")) {
  97                DRM_ERROR("can't reserve VRAM\n");
  98                return -ENXIO;
  99        }
 100
 101        return 0;
 102}
 103
 104/*
 105 * Our emulated hardware has two sets of memory. One is video RAM and can
 106 * simply be used as a linear framebuffer - the other provides mmio access
 107 * to the display registers. The latter can also be accessed via IO port
 108 * access, but we map the range and use mmio to program them instead
 109 */
 110
 111int cirrus_device_init(struct cirrus_device *cdev,
 112                       struct drm_device *ddev,
 113                       struct pci_dev *pdev, uint32_t flags)
 114{
 115        int ret;
 116
 117        cdev->dev = ddev;
 118        cdev->flags = flags;
 119
 120        /* Hardcode the number of CRTCs to 1 */
 121        cdev->num_crtc = 1;
 122
 123        /* BAR 0 is the framebuffer, BAR 1 contains registers */
 124        cdev->rmmio_base = pci_resource_start(cdev->dev->pdev, 1);
 125        cdev->rmmio_size = pci_resource_len(cdev->dev->pdev, 1);
 126
 127        if (!request_mem_region(cdev->rmmio_base, cdev->rmmio_size,
 128                                "cirrusdrmfb_mmio")) {
 129                DRM_ERROR("can't reserve mmio registers\n");
 130                return -ENOMEM;
 131        }
 132
 133        cdev->rmmio = ioremap(cdev->rmmio_base, cdev->rmmio_size);
 134
 135        if (cdev->rmmio == NULL)
 136                return -ENOMEM;
 137
 138        ret = cirrus_vram_init(cdev);
 139        if (ret) {
 140                release_mem_region(cdev->rmmio_base, cdev->rmmio_size);
 141                return ret;
 142        }
 143
 144        return 0;
 145}
 146
 147void cirrus_device_fini(struct cirrus_device *cdev)
 148{
 149        release_mem_region(cdev->rmmio_base, cdev->rmmio_size);
 150        cirrus_vram_fini(cdev);
 151}
 152
 153/*
 154 * Functions here will be called by the core once it's bound the driver to
 155 * a PCI device
 156 */
 157
 158int cirrus_driver_load(struct drm_device *dev, unsigned long flags)
 159{
 160        struct cirrus_device *cdev;
 161        int r;
 162
 163        cdev = kzalloc(sizeof(struct cirrus_device), GFP_KERNEL);
 164        if (cdev == NULL)
 165                return -ENOMEM;
 166        dev->dev_private = (void *)cdev;
 167
 168        r = cirrus_device_init(cdev, dev, dev->pdev, flags);
 169        if (r) {
 170                dev_err(&dev->pdev->dev, "Fatal error during GPU init: %d\n", r);
 171                goto out;
 172        }
 173
 174        r = cirrus_mm_init(cdev);
 175        if (r) {
 176                dev_err(&dev->pdev->dev, "fatal err on mm init\n");
 177                goto out;
 178        }
 179
 180        /*
 181         * cirrus_modeset_init() is initializing/registering the emulated fbdev
 182         * and DRM internals can access/test some of the fields in
 183         * mode_config->funcs as part of the fbdev registration process.
 184         * Make sure dev->mode_config.funcs is properly set to avoid
 185         * dereferencing a NULL pointer.
 186         * FIXME: mode_config.funcs assignment should probably be done in
 187         * cirrus_modeset_init() (that's a common pattern seen in other DRM
 188         * drivers).
 189         */
 190        dev->mode_config.funcs = &cirrus_mode_funcs;
 191        r = cirrus_modeset_init(cdev);
 192        if (r) {
 193                dev_err(&dev->pdev->dev, "Fatal error during modeset init: %d\n", r);
 194                goto out;
 195        }
 196
 197        return 0;
 198out:
 199        cirrus_driver_unload(dev);
 200        return r;
 201}
 202
 203void cirrus_driver_unload(struct drm_device *dev)
 204{
 205        struct cirrus_device *cdev = dev->dev_private;
 206
 207        if (cdev == NULL)
 208                return;
 209        cirrus_modeset_fini(cdev);
 210        cirrus_mm_fini(cdev);
 211        cirrus_device_fini(cdev);
 212        kfree(cdev);
 213        dev->dev_private = NULL;
 214}
 215
 216int cirrus_gem_create(struct drm_device *dev,
 217                   u32 size, bool iskernel,
 218                   struct drm_gem_object **obj)
 219{
 220        struct cirrus_bo *cirrusbo;
 221        int ret;
 222
 223        *obj = NULL;
 224
 225        size = roundup(size, PAGE_SIZE);
 226        if (size == 0)
 227                return -EINVAL;
 228
 229        ret = cirrus_bo_create(dev, size, 0, 0, &cirrusbo);
 230        if (ret) {
 231                if (ret != -ERESTARTSYS)
 232                        DRM_ERROR("failed to allocate GEM object\n");
 233                return ret;
 234        }
 235        *obj = &cirrusbo->gem;
 236        return 0;
 237}
 238
 239int cirrus_dumb_create(struct drm_file *file,
 240                    struct drm_device *dev,
 241                    struct drm_mode_create_dumb *args)
 242{
 243        int ret;
 244        struct drm_gem_object *gobj;
 245        u32 handle;
 246
 247        args->pitch = args->width * ((args->bpp + 7) / 8);
 248        args->size = args->pitch * args->height;
 249
 250        ret = cirrus_gem_create(dev, args->size, false,
 251                             &gobj);
 252        if (ret)
 253                return ret;
 254
 255        ret = drm_gem_handle_create(file, gobj, &handle);
 256        drm_gem_object_put_unlocked(gobj);
 257        if (ret)
 258                return ret;
 259
 260        args->handle = handle;
 261        return 0;
 262}
 263
 264static void cirrus_bo_unref(struct cirrus_bo **bo)
 265{
 266        struct ttm_buffer_object *tbo;
 267
 268        if ((*bo) == NULL)
 269                return;
 270
 271        tbo = &((*bo)->bo);
 272        ttm_bo_put(tbo);
 273        *bo = NULL;
 274}
 275
 276void cirrus_gem_free_object(struct drm_gem_object *obj)
 277{
 278        struct cirrus_bo *cirrus_bo = gem_to_cirrus_bo(obj);
 279
 280        cirrus_bo_unref(&cirrus_bo);
 281}
 282
 283
 284static inline u64 cirrus_bo_mmap_offset(struct cirrus_bo *bo)
 285{
 286        return drm_vma_node_offset_addr(&bo->bo.vma_node);
 287}
 288
 289int
 290cirrus_dumb_mmap_offset(struct drm_file *file,
 291                     struct drm_device *dev,
 292                     uint32_t handle,
 293                     uint64_t *offset)
 294{
 295        struct drm_gem_object *obj;
 296        struct cirrus_bo *bo;
 297
 298        obj = drm_gem_object_lookup(file, handle);
 299        if (obj == NULL)
 300                return -ENOENT;
 301
 302        bo = gem_to_cirrus_bo(obj);
 303        *offset = cirrus_bo_mmap_offset(bo);
 304
 305        drm_gem_object_put_unlocked(obj);
 306
 307        return 0;
 308}
 309
 310bool cirrus_check_framebuffer(struct cirrus_device *cdev, int width, int height,
 311                              int bpp, int pitch)
 312{
 313        const int max_pitch = 0x1FF << 3; /* (4096 - 1) & ~111b bytes */
 314        const int max_size = cdev->mc.vram_size;
 315
 316        if (bpp > cirrus_bpp)
 317                return false;
 318        if (bpp > 32)
 319                return false;
 320
 321        if (pitch > max_pitch)
 322                return false;
 323
 324        if (pitch * height > max_size)
 325                return false;
 326
 327        return true;
 328}
 329