linux/drivers/gpu/drm/qxl/qxl_fb.c
<<
>>
Prefs
   1/*
   2 * Copyright © 2013 Red Hat
   3 *
   4 * Permission is hereby granted, free of charge, to any person obtaining a
   5 * copy of this software and associated documentation files (the "Software"),
   6 * to deal in the Software without restriction, including without limitation
   7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
   8 * and/or sell copies of the Software, and to permit persons to whom the
   9 * Software is furnished to do so, subject to the following conditions:
  10 *
  11 * The above copyright notice and this permission notice (including the next
  12 * paragraph) shall be included in all copies or substantial portions of the
  13 * Software.
  14 *
  15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21 * DEALINGS IN THE SOFTWARE.
  22 *
  23 * Authors:
  24 *     David Airlie
  25 */
  26#include <linux/module.h>
  27#include <linux/fb.h>
  28
  29#include "drmP.h"
  30#include "drm/drm.h"
  31#include "drm/drm_crtc.h"
  32#include "drm/drm_crtc_helper.h"
  33#include "qxl_drv.h"
  34
  35#include "qxl_object.h"
  36#include "drm_fb_helper.h"
  37
  38#define QXL_DIRTY_DELAY (HZ / 30)
  39
  40struct qxl_fbdev {
  41        struct drm_fb_helper helper;
  42        struct qxl_framebuffer  qfb;
  43        struct qxl_device       *qdev;
  44
  45        spinlock_t delayed_ops_lock;
  46        struct list_head delayed_ops;
  47        void *shadow;
  48        int size;
  49};
  50
  51static void qxl_fb_image_init(struct qxl_fb_image *qxl_fb_image,
  52                              struct qxl_device *qdev, struct fb_info *info,
  53                              const struct fb_image *image)
  54{
  55        qxl_fb_image->qdev = qdev;
  56        if (info) {
  57                qxl_fb_image->visual = info->fix.visual;
  58                if (qxl_fb_image->visual == FB_VISUAL_TRUECOLOR ||
  59                    qxl_fb_image->visual == FB_VISUAL_DIRECTCOLOR)
  60                        memcpy(&qxl_fb_image->pseudo_palette,
  61                               info->pseudo_palette,
  62                               sizeof(qxl_fb_image->pseudo_palette));
  63        } else {
  64                 /* fallback */
  65                if (image->depth == 1)
  66                        qxl_fb_image->visual = FB_VISUAL_MONO10;
  67                else
  68                        qxl_fb_image->visual = FB_VISUAL_DIRECTCOLOR;
  69        }
  70        if (image) {
  71                memcpy(&qxl_fb_image->fb_image, image,
  72                       sizeof(qxl_fb_image->fb_image));
  73        }
  74}
  75
  76static struct fb_deferred_io qxl_defio = {
  77        .delay          = QXL_DIRTY_DELAY,
  78        .deferred_io    = drm_fb_helper_deferred_io,
  79};
  80
  81static struct fb_ops qxlfb_ops = {
  82        .owner = THIS_MODULE,
  83        .fb_check_var = drm_fb_helper_check_var,
  84        .fb_set_par = drm_fb_helper_set_par, /* TODO: copy vmwgfx */
  85        .fb_fillrect = drm_fb_helper_sys_fillrect,
  86        .fb_copyarea = drm_fb_helper_sys_copyarea,
  87        .fb_imageblit = drm_fb_helper_sys_imageblit,
  88        .fb_pan_display = drm_fb_helper_pan_display,
  89        .fb_blank = drm_fb_helper_blank,
  90        .fb_setcmap = drm_fb_helper_setcmap,
  91        .fb_debug_enter = drm_fb_helper_debug_enter,
  92        .fb_debug_leave = drm_fb_helper_debug_leave,
  93};
  94
  95static void qxlfb_destroy_pinned_object(struct drm_gem_object *gobj)
  96{
  97        struct qxl_bo *qbo = gem_to_qxl_bo(gobj);
  98        int ret;
  99
 100        ret = qxl_bo_reserve(qbo, false);
 101        if (likely(ret == 0)) {
 102                qxl_bo_kunmap(qbo);
 103                qxl_bo_unpin(qbo);
 104                qxl_bo_unreserve(qbo);
 105        }
 106        drm_gem_object_unreference_unlocked(gobj);
 107}
 108
 109int qxl_get_handle_for_primary_fb(struct qxl_device *qdev,
 110                                  struct drm_file *file_priv,
 111                                  uint32_t *handle)
 112{
 113        int r;
 114        struct drm_gem_object *gobj = qdev->fbdev_qfb->obj;
 115
 116        BUG_ON(!gobj);
 117        /* drm_get_handle_create adds a reference - good */
 118        r = drm_gem_handle_create(file_priv, gobj, handle);
 119        if (r)
 120                return r;
 121        return 0;
 122}
 123
 124static int qxlfb_create_pinned_object(struct qxl_fbdev *qfbdev,
 125                                      const struct drm_mode_fb_cmd2 *mode_cmd,
 126                                      struct drm_gem_object **gobj_p)
 127{
 128        struct qxl_device *qdev = qfbdev->qdev;
 129        struct drm_gem_object *gobj = NULL;
 130        struct qxl_bo *qbo = NULL;
 131        int ret;
 132        int aligned_size, size;
 133        int height = mode_cmd->height;
 134        int bpp;
 135        int depth;
 136
 137        drm_fb_get_bpp_depth(mode_cmd->pixel_format, &bpp, &depth);
 138
 139        size = mode_cmd->pitches[0] * height;
 140        aligned_size = ALIGN(size, PAGE_SIZE);
 141        /* TODO: unallocate and reallocate surface0 for real. Hack to just
 142         * have a large enough surface0 for 1024x768 Xorg 32bpp mode */
 143        ret = qxl_gem_object_create(qdev, aligned_size, 0,
 144                                    QXL_GEM_DOMAIN_SURFACE,
 145                                    false, /* is discardable */
 146                                    false, /* is kernel (false means device) */
 147                                    NULL,
 148                                    &gobj);
 149        if (ret) {
 150                pr_err("failed to allocate framebuffer (%d)\n",
 151                       aligned_size);
 152                return -ENOMEM;
 153        }
 154        qbo = gem_to_qxl_bo(gobj);
 155
 156        qbo->surf.width = mode_cmd->width;
 157        qbo->surf.height = mode_cmd->height;
 158        qbo->surf.stride = mode_cmd->pitches[0];
 159        qbo->surf.format = SPICE_SURFACE_FMT_32_xRGB;
 160        ret = qxl_bo_reserve(qbo, false);
 161        if (unlikely(ret != 0))
 162                goto out_unref;
 163        ret = qxl_bo_pin(qbo, QXL_GEM_DOMAIN_SURFACE, NULL);
 164        if (ret) {
 165                qxl_bo_unreserve(qbo);
 166                goto out_unref;
 167        }
 168        ret = qxl_bo_kmap(qbo, NULL);
 169        qxl_bo_unreserve(qbo); /* unreserve, will be mmaped */
 170        if (ret)
 171                goto out_unref;
 172
 173        *gobj_p = gobj;
 174        return 0;
 175out_unref:
 176        qxlfb_destroy_pinned_object(gobj);
 177        *gobj_p = NULL;
 178        return ret;
 179}
 180
 181/*
 182 * FIXME
 183 * It should not be necessary to have a special dirty() callback for fbdev.
 184 */
 185static int qxlfb_framebuffer_dirty(struct drm_framebuffer *fb,
 186                                   struct drm_file *file_priv,
 187                                   unsigned flags, unsigned color,
 188                                   struct drm_clip_rect *clips,
 189                                   unsigned num_clips)
 190{
 191        struct qxl_device *qdev = fb->dev->dev_private;
 192        struct fb_info *info = qdev->fbdev_info;
 193        struct qxl_fbdev *qfbdev = info->par;
 194        struct qxl_fb_image qxl_fb_image;
 195        struct fb_image *image = &qxl_fb_image.fb_image;
 196
 197        /* TODO: hard coding 32 bpp */
 198        int stride = qfbdev->qfb.base.pitches[0];
 199
 200        /*
 201         * we are using a shadow draw buffer, at qdev->surface0_shadow
 202         */
 203        qxl_io_log(qdev, "dirty x[%d, %d], y[%d, %d]", clips->x1, clips->x2,
 204                   clips->y1, clips->y2);
 205        image->dx = clips->x1;
 206        image->dy = clips->y1;
 207        image->width = clips->x2 - clips->x1;
 208        image->height = clips->y2 - clips->y1;
 209        image->fg_color = 0xffffffff; /* unused, just to avoid uninitialized
 210                                         warnings */
 211        image->bg_color = 0;
 212        image->depth = 32;           /* TODO: take from somewhere? */
 213        image->cmap.start = 0;
 214        image->cmap.len = 0;
 215        image->cmap.red = NULL;
 216        image->cmap.green = NULL;
 217        image->cmap.blue = NULL;
 218        image->cmap.transp = NULL;
 219        image->data = qfbdev->shadow + (clips->x1 * 4) + (stride * clips->y1);
 220
 221        qxl_fb_image_init(&qxl_fb_image, qdev, info, NULL);
 222        qxl_draw_opaque_fb(&qxl_fb_image, stride);
 223
 224        return 0;
 225}
 226
 227static const struct drm_framebuffer_funcs qxlfb_fb_funcs = {
 228        .destroy = qxl_user_framebuffer_destroy,
 229        .dirty = qxlfb_framebuffer_dirty,
 230};
 231
 232static int qxlfb_create(struct qxl_fbdev *qfbdev,
 233                        struct drm_fb_helper_surface_size *sizes)
 234{
 235        struct qxl_device *qdev = qfbdev->qdev;
 236        struct fb_info *info;
 237        struct drm_framebuffer *fb = NULL;
 238        struct drm_mode_fb_cmd2 mode_cmd;
 239        struct drm_gem_object *gobj = NULL;
 240        struct qxl_bo *qbo = NULL;
 241        int ret;
 242        int size;
 243        int bpp = sizes->surface_bpp;
 244        int depth = sizes->surface_depth;
 245        void *shadow;
 246
 247        mode_cmd.width = sizes->surface_width;
 248        mode_cmd.height = sizes->surface_height;
 249
 250        mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 1) / 8), 64);
 251        mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
 252
 253        ret = qxlfb_create_pinned_object(qfbdev, &mode_cmd, &gobj);
 254        if (ret < 0)
 255                return ret;
 256
 257        qbo = gem_to_qxl_bo(gobj);
 258        QXL_INFO(qdev, "%s: %dx%d %d\n", __func__, mode_cmd.width,
 259                 mode_cmd.height, mode_cmd.pitches[0]);
 260
 261        shadow = vmalloc(mode_cmd.pitches[0] * mode_cmd.height);
 262        /* TODO: what's the usual response to memory allocation errors? */
 263        BUG_ON(!shadow);
 264        QXL_INFO(qdev,
 265        "surface0 at gpu offset %lld, mmap_offset %lld (virt %p, shadow %p)\n",
 266                 qxl_bo_gpu_offset(qbo),
 267                 qxl_bo_mmap_offset(qbo),
 268                 qbo->kptr,
 269                 shadow);
 270        size = mode_cmd.pitches[0] * mode_cmd.height;
 271
 272        info = drm_fb_helper_alloc_fbi(&qfbdev->helper);
 273        if (IS_ERR(info)) {
 274                ret = PTR_ERR(info);
 275                goto out_unref;
 276        }
 277
 278        info->par = qfbdev;
 279
 280        qxl_framebuffer_init(qdev->ddev, &qfbdev->qfb, &mode_cmd, gobj,
 281                             &qxlfb_fb_funcs);
 282
 283        fb = &qfbdev->qfb.base;
 284
 285        /* setup helper with fb data */
 286        qfbdev->helper.fb = fb;
 287
 288        qfbdev->shadow = shadow;
 289        strcpy(info->fix.id, "qxldrmfb");
 290
 291        drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
 292
 293        info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
 294        info->fbops = &qxlfb_ops;
 295
 296        /*
 297         * TODO: using gobj->size in various places in this function. Not sure
 298         * what the difference between the different sizes is.
 299         */
 300        info->fix.smem_start = qdev->vram_base; /* TODO - correct? */
 301        info->fix.smem_len = gobj->size;
 302        info->screen_base = qfbdev->shadow;
 303        info->screen_size = gobj->size;
 304
 305        drm_fb_helper_fill_var(info, &qfbdev->helper, sizes->fb_width,
 306                               sizes->fb_height);
 307
 308        /* setup aperture base/size for vesafb takeover */
 309        info->apertures->ranges[0].base = qdev->ddev->mode_config.fb_base;
 310        info->apertures->ranges[0].size = qdev->vram_size;
 311
 312        info->fix.mmio_start = 0;
 313        info->fix.mmio_len = 0;
 314
 315        if (info->screen_base == NULL) {
 316                ret = -ENOSPC;
 317                goto out_destroy_fbi;
 318        }
 319
 320        info->fbdefio = &qxl_defio;
 321        fb_deferred_io_init(info);
 322
 323        qdev->fbdev_info = info;
 324        qdev->fbdev_qfb = &qfbdev->qfb;
 325        DRM_INFO("fb mappable at 0x%lX, size %lu\n",  info->fix.smem_start, (unsigned long)info->screen_size);
 326        DRM_INFO("fb: depth %d, pitch %d, width %d, height %d\n", fb->depth, fb->pitches[0], fb->width, fb->height);
 327        return 0;
 328
 329out_destroy_fbi:
 330        drm_fb_helper_release_fbi(&qfbdev->helper);
 331out_unref:
 332        if (qbo) {
 333                ret = qxl_bo_reserve(qbo, false);
 334                if (likely(ret == 0)) {
 335                        qxl_bo_kunmap(qbo);
 336                        qxl_bo_unpin(qbo);
 337                        qxl_bo_unreserve(qbo);
 338                }
 339        }
 340        if (fb && ret) {
 341                drm_gem_object_unreference_unlocked(gobj);
 342                drm_framebuffer_cleanup(fb);
 343                kfree(fb);
 344        }
 345        drm_gem_object_unreference_unlocked(gobj);
 346        return ret;
 347}
 348
 349static int qxl_fb_find_or_create_single(
 350                struct drm_fb_helper *helper,
 351                struct drm_fb_helper_surface_size *sizes)
 352{
 353        struct qxl_fbdev *qfbdev =
 354                container_of(helper, struct qxl_fbdev, helper);
 355        int new_fb = 0;
 356        int ret;
 357
 358        if (!helper->fb) {
 359                ret = qxlfb_create(qfbdev, sizes);
 360                if (ret)
 361                        return ret;
 362                new_fb = 1;
 363        }
 364        return new_fb;
 365}
 366
 367static int qxl_fbdev_destroy(struct drm_device *dev, struct qxl_fbdev *qfbdev)
 368{
 369        struct qxl_framebuffer *qfb = &qfbdev->qfb;
 370
 371        drm_fb_helper_unregister_fbi(&qfbdev->helper);
 372        drm_fb_helper_release_fbi(&qfbdev->helper);
 373
 374        if (qfb->obj) {
 375                qxlfb_destroy_pinned_object(qfb->obj);
 376                qfb->obj = NULL;
 377        }
 378        drm_fb_helper_fini(&qfbdev->helper);
 379        vfree(qfbdev->shadow);
 380        drm_framebuffer_cleanup(&qfb->base);
 381
 382        return 0;
 383}
 384
 385static const struct drm_fb_helper_funcs qxl_fb_helper_funcs = {
 386        .fb_probe = qxl_fb_find_or_create_single,
 387};
 388
 389int qxl_fbdev_init(struct qxl_device *qdev)
 390{
 391        struct qxl_fbdev *qfbdev;
 392        int bpp_sel = 32; /* TODO: parameter from somewhere? */
 393        int ret;
 394
 395        qfbdev = kzalloc(sizeof(struct qxl_fbdev), GFP_KERNEL);
 396        if (!qfbdev)
 397                return -ENOMEM;
 398
 399        qfbdev->qdev = qdev;
 400        qdev->mode_info.qfbdev = qfbdev;
 401        spin_lock_init(&qfbdev->delayed_ops_lock);
 402        INIT_LIST_HEAD(&qfbdev->delayed_ops);
 403
 404        drm_fb_helper_prepare(qdev->ddev, &qfbdev->helper,
 405                              &qxl_fb_helper_funcs);
 406
 407        ret = drm_fb_helper_init(qdev->ddev, &qfbdev->helper,
 408                                 qxl_num_crtc /* num_crtc - QXL supports just 1 */,
 409                                 QXLFB_CONN_LIMIT);
 410        if (ret)
 411                goto free;
 412
 413        ret = drm_fb_helper_single_add_all_connectors(&qfbdev->helper);
 414        if (ret)
 415                goto fini;
 416
 417        ret = drm_fb_helper_initial_config(&qfbdev->helper, bpp_sel);
 418        if (ret)
 419                goto fini;
 420
 421        return 0;
 422
 423fini:
 424        drm_fb_helper_fini(&qfbdev->helper);
 425free:
 426        kfree(qfbdev);
 427        return ret;
 428}
 429
 430void qxl_fbdev_fini(struct qxl_device *qdev)
 431{
 432        if (!qdev->mode_info.qfbdev)
 433                return;
 434
 435        qxl_fbdev_destroy(qdev->ddev, qdev->mode_info.qfbdev);
 436        kfree(qdev->mode_info.qfbdev);
 437        qdev->mode_info.qfbdev = NULL;
 438}
 439
 440void qxl_fbdev_set_suspend(struct qxl_device *qdev, int state)
 441{
 442        drm_fb_helper_set_suspend(&qdev->mode_info.qfbdev->helper, state);
 443}
 444
 445bool qxl_fbdev_qobj_is_fb(struct qxl_device *qdev, struct qxl_bo *qobj)
 446{
 447        if (qobj == gem_to_qxl_bo(qdev->mode_info.qfbdev->qfb.obj))
 448                return true;
 449        return false;
 450}
 451