linux/drivers/gpu/drm/udl/udl_fb.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2012 Red Hat
   3 *
   4 * based in parts on udlfb.c:
   5 * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
   6 * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
   7 * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
   8 *
   9 * This file is subject to the terms and conditions of the GNU General Public
  10 * License v2. See the file COPYING in the main directory of this archive for
  11 * more details.
  12 */
  13#include <linux/module.h>
  14#include <linux/slab.h>
  15#include <linux/fb.h>
  16#include <linux/dma-buf.h>
  17#include <linux/mem_encrypt.h>
  18
  19#include <drm/drmP.h>
  20#include <drm/drm_crtc.h>
  21#include <drm/drm_crtc_helper.h>
  22#include "udl_drv.h"
  23
  24#include <drm/drm_fb_helper.h>
  25
  26#define DL_DEFIO_WRITE_DELAY    (HZ/20) /* fb_deferred_io.delay in jiffies */
  27
  28static int fb_defio = 0;  /* Optionally enable experimental fb_defio mmap support */
  29static int fb_bpp = 16;
  30
  31module_param(fb_bpp, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
  32module_param(fb_defio, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
  33
  34struct udl_fbdev {
  35        struct drm_fb_helper helper;
  36        struct udl_framebuffer ufb;
  37        int fb_count;
  38};
  39
  40#define DL_ALIGN_UP(x, a) ALIGN(x, a)
  41#define DL_ALIGN_DOWN(x, a) ALIGN_DOWN(x, a)
  42
  43/** Read the red component (0..255) of a 32 bpp colour. */
  44#define DLO_RGB_GETRED(col) (uint8_t)((col) & 0xFF)
  45
  46/** Read the green component (0..255) of a 32 bpp colour. */
  47#define DLO_RGB_GETGRN(col) (uint8_t)(((col) >> 8) & 0xFF)
  48
  49/** Read the blue component (0..255) of a 32 bpp colour. */
  50#define DLO_RGB_GETBLU(col) (uint8_t)(((col) >> 16) & 0xFF)
  51
  52/** Return red/green component of a 16 bpp colour number. */
  53#define DLO_RG16(red, grn) (uint8_t)((((red) & 0xF8) | ((grn) >> 5)) & 0xFF)
  54
  55/** Return green/blue component of a 16 bpp colour number. */
  56#define DLO_GB16(grn, blu) (uint8_t)(((((grn) & 0x1C) << 3) | ((blu) >> 3)) & 0xFF)
  57
  58/** Return 8 bpp colour number from red, green and blue components. */
  59#define DLO_RGB8(red, grn, blu) ((((red) << 5) | (((grn) & 3) << 3) | ((blu) & 7)) & 0xFF)
  60
  61#if 0
  62static uint8_t rgb8(uint32_t col)
  63{
  64        uint8_t red = DLO_RGB_GETRED(col);
  65        uint8_t grn = DLO_RGB_GETGRN(col);
  66        uint8_t blu = DLO_RGB_GETBLU(col);
  67
  68        return DLO_RGB8(red, grn, blu);
  69}
  70
  71static uint16_t rgb16(uint32_t col)
  72{
  73        uint8_t red = DLO_RGB_GETRED(col);
  74        uint8_t grn = DLO_RGB_GETGRN(col);
  75        uint8_t blu = DLO_RGB_GETBLU(col);
  76
  77        return (DLO_RG16(red, grn) << 8) + DLO_GB16(grn, blu);
  78}
  79#endif
  80
  81int udl_handle_damage(struct udl_framebuffer *fb, int x, int y,
  82                      int width, int height)
  83{
  84        struct drm_device *dev = fb->base.dev;
  85        struct udl_device *udl = dev->dev_private;
  86        int i, ret;
  87        char *cmd;
  88        cycles_t start_cycles, end_cycles;
  89        int bytes_sent = 0;
  90        int bytes_identical = 0;
  91        struct urb *urb;
  92        int aligned_x;
  93        int log_bpp;
  94
  95        BUG_ON(!is_power_of_2(fb->base.format->cpp[0]));
  96        log_bpp = __ffs(fb->base.format->cpp[0]);
  97
  98        if (!fb->active_16)
  99                return 0;
 100
 101        if (!fb->obj->vmapping) {
 102                ret = udl_gem_vmap(fb->obj);
 103                if (ret == -ENOMEM) {
 104                        DRM_ERROR("failed to vmap fb\n");
 105                        return 0;
 106                }
 107                if (!fb->obj->vmapping) {
 108                        DRM_ERROR("failed to vmapping\n");
 109                        return 0;
 110                }
 111        }
 112
 113        aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
 114        width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
 115        x = aligned_x;
 116
 117        if ((width <= 0) ||
 118            (x + width > fb->base.width) ||
 119            (y + height > fb->base.height))
 120                return -EINVAL;
 121
 122        start_cycles = get_cycles();
 123
 124        urb = udl_get_urb(dev);
 125        if (!urb)
 126                return 0;
 127        cmd = urb->transfer_buffer;
 128
 129        for (i = y; i < y + height ; i++) {
 130                const int line_offset = fb->base.pitches[0] * i;
 131                const int byte_offset = line_offset + (x << log_bpp);
 132                const int dev_byte_offset = (fb->base.width * i + x) << log_bpp;
 133                if (udl_render_hline(dev, log_bpp, &urb,
 134                                     (char *) fb->obj->vmapping,
 135                                     &cmd, byte_offset, dev_byte_offset,
 136                                     width << log_bpp,
 137                                     &bytes_identical, &bytes_sent))
 138                        goto error;
 139        }
 140
 141        if (cmd > (char *) urb->transfer_buffer) {
 142                /* Send partial buffer remaining before exiting */
 143                int len;
 144                if (cmd < (char *) urb->transfer_buffer + urb->transfer_buffer_length)
 145                        *cmd++ = 0xAF;
 146                len = cmd - (char *) urb->transfer_buffer;
 147                ret = udl_submit_urb(dev, urb, len);
 148                bytes_sent += len;
 149        } else
 150                udl_urb_completion(urb);
 151
 152error:
 153        atomic_add(bytes_sent, &udl->bytes_sent);
 154        atomic_add(bytes_identical, &udl->bytes_identical);
 155        atomic_add((width * height) << log_bpp, &udl->bytes_rendered);
 156        end_cycles = get_cycles();
 157        atomic_add(((unsigned int) ((end_cycles - start_cycles)
 158                    >> 10)), /* Kcycles */
 159                   &udl->cpu_kcycles_used);
 160
 161        return 0;
 162}
 163
 164static int udl_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
 165{
 166        unsigned long start = vma->vm_start;
 167        unsigned long size = vma->vm_end - vma->vm_start;
 168        unsigned long offset;
 169        unsigned long page, pos;
 170
 171        if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
 172                return -EINVAL;
 173
 174        offset = vma->vm_pgoff << PAGE_SHIFT;
 175
 176        if (offset > info->fix.smem_len || size > info->fix.smem_len - offset)
 177                return -EINVAL;
 178
 179        pos = (unsigned long)info->fix.smem_start + offset;
 180
 181        pr_debug("mmap() framebuffer addr:%lu size:%lu\n",
 182                  pos, size);
 183
 184        /* We don't want the framebuffer to be mapped encrypted */
 185        vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
 186
 187        while (size > 0) {
 188                page = vmalloc_to_pfn((void *)pos);
 189                if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
 190                        return -EAGAIN;
 191
 192                start += PAGE_SIZE;
 193                pos += PAGE_SIZE;
 194                if (size > PAGE_SIZE)
 195                        size -= PAGE_SIZE;
 196                else
 197                        size = 0;
 198        }
 199
 200        /* VM_IO | VM_DONTEXPAND | VM_DONTDUMP are set by remap_pfn_range() */
 201        return 0;
 202}
 203
 204/*
 205 * It's common for several clients to have framebuffer open simultaneously.
 206 * e.g. both fbcon and X. Makes things interesting.
 207 * Assumes caller is holding info->lock (for open and release at least)
 208 */
 209static int udl_fb_open(struct fb_info *info, int user)
 210{
 211        struct udl_fbdev *ufbdev = info->par;
 212        struct drm_device *dev = ufbdev->ufb.base.dev;
 213        struct udl_device *udl = dev->dev_private;
 214
 215        /* If the USB device is gone, we don't accept new opens */
 216        if (drm_dev_is_unplugged(udl->ddev))
 217                return -ENODEV;
 218
 219        ufbdev->fb_count++;
 220
 221#ifdef CONFIG_DRM_FBDEV_EMULATION
 222        if (fb_defio && (info->fbdefio == NULL)) {
 223                /* enable defio at last moment if not disabled by client */
 224
 225                struct fb_deferred_io *fbdefio;
 226
 227                fbdefio = kzalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
 228
 229                if (fbdefio) {
 230                        fbdefio->delay = DL_DEFIO_WRITE_DELAY;
 231                        fbdefio->deferred_io = drm_fb_helper_deferred_io;
 232                }
 233
 234                info->fbdefio = fbdefio;
 235                fb_deferred_io_init(info);
 236        }
 237#endif
 238
 239        pr_debug("open /dev/fb%d user=%d fb_info=%p count=%d\n",
 240                  info->node, user, info, ufbdev->fb_count);
 241
 242        return 0;
 243}
 244
 245
 246/*
 247 * Assumes caller is holding info->lock mutex (for open and release at least)
 248 */
 249static int udl_fb_release(struct fb_info *info, int user)
 250{
 251        struct udl_fbdev *ufbdev = info->par;
 252
 253        ufbdev->fb_count--;
 254
 255#ifdef CONFIG_DRM_FBDEV_EMULATION
 256        if ((ufbdev->fb_count == 0) && (info->fbdefio)) {
 257                fb_deferred_io_cleanup(info);
 258                kfree(info->fbdefio);
 259                info->fbdefio = NULL;
 260                info->fbops->fb_mmap = udl_fb_mmap;
 261        }
 262#endif
 263
 264        pr_debug("released /dev/fb%d user=%d count=%d\n",
 265                info->node, user, ufbdev->fb_count);
 266
 267        return 0;
 268}
 269
 270static struct fb_ops udlfb_ops = {
 271        .owner = THIS_MODULE,
 272        DRM_FB_HELPER_DEFAULT_OPS,
 273        .fb_fillrect = drm_fb_helper_sys_fillrect,
 274        .fb_copyarea = drm_fb_helper_sys_copyarea,
 275        .fb_imageblit = drm_fb_helper_sys_imageblit,
 276        .fb_mmap = udl_fb_mmap,
 277        .fb_open = udl_fb_open,
 278        .fb_release = udl_fb_release,
 279};
 280
 281static int udl_user_framebuffer_dirty(struct drm_framebuffer *fb,
 282                                      struct drm_file *file,
 283                                      unsigned flags, unsigned color,
 284                                      struct drm_clip_rect *clips,
 285                                      unsigned num_clips)
 286{
 287        struct udl_framebuffer *ufb = to_udl_fb(fb);
 288        int i;
 289        int ret = 0;
 290
 291        drm_modeset_lock_all(fb->dev);
 292
 293        if (!ufb->active_16)
 294                goto unlock;
 295
 296        if (ufb->obj->base.import_attach) {
 297                ret = dma_buf_begin_cpu_access(ufb->obj->base.import_attach->dmabuf,
 298                                               DMA_FROM_DEVICE);
 299                if (ret)
 300                        goto unlock;
 301        }
 302
 303        for (i = 0; i < num_clips; i++) {
 304                ret = udl_handle_damage(ufb, clips[i].x1, clips[i].y1,
 305                                  clips[i].x2 - clips[i].x1,
 306                                  clips[i].y2 - clips[i].y1);
 307                if (ret)
 308                        break;
 309        }
 310
 311        if (ufb->obj->base.import_attach) {
 312                ret = dma_buf_end_cpu_access(ufb->obj->base.import_attach->dmabuf,
 313                                             DMA_FROM_DEVICE);
 314        }
 315
 316 unlock:
 317        drm_modeset_unlock_all(fb->dev);
 318
 319        return ret;
 320}
 321
 322static void udl_user_framebuffer_destroy(struct drm_framebuffer *fb)
 323{
 324        struct udl_framebuffer *ufb = to_udl_fb(fb);
 325
 326        if (ufb->obj)
 327                drm_gem_object_put_unlocked(&ufb->obj->base);
 328
 329        drm_framebuffer_cleanup(fb);
 330        kfree(ufb);
 331}
 332
 333static const struct drm_framebuffer_funcs udlfb_funcs = {
 334        .destroy = udl_user_framebuffer_destroy,
 335        .dirty = udl_user_framebuffer_dirty,
 336};
 337
 338
 339static int
 340udl_framebuffer_init(struct drm_device *dev,
 341                     struct udl_framebuffer *ufb,
 342                     const struct drm_mode_fb_cmd2 *mode_cmd,
 343                     struct udl_gem_object *obj)
 344{
 345        int ret;
 346
 347        ufb->obj = obj;
 348        drm_helper_mode_fill_fb_struct(dev, &ufb->base, mode_cmd);
 349        ret = drm_framebuffer_init(dev, &ufb->base, &udlfb_funcs);
 350        return ret;
 351}
 352
 353
 354static int udlfb_create(struct drm_fb_helper *helper,
 355                        struct drm_fb_helper_surface_size *sizes)
 356{
 357        struct udl_fbdev *ufbdev =
 358                container_of(helper, struct udl_fbdev, helper);
 359        struct drm_device *dev = ufbdev->helper.dev;
 360        struct fb_info *info;
 361        struct drm_framebuffer *fb;
 362        struct drm_mode_fb_cmd2 mode_cmd;
 363        struct udl_gem_object *obj;
 364        uint32_t size;
 365        int ret = 0;
 366
 367        if (sizes->surface_bpp == 24)
 368                sizes->surface_bpp = 32;
 369
 370        mode_cmd.width = sizes->surface_width;
 371        mode_cmd.height = sizes->surface_height;
 372        mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
 373
 374        mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
 375                                                          sizes->surface_depth);
 376
 377        size = mode_cmd.pitches[0] * mode_cmd.height;
 378        size = ALIGN(size, PAGE_SIZE);
 379
 380        obj = udl_gem_alloc_object(dev, size);
 381        if (!obj)
 382                goto out;
 383
 384        ret = udl_gem_vmap(obj);
 385        if (ret) {
 386                DRM_ERROR("failed to vmap fb\n");
 387                goto out_gfree;
 388        }
 389
 390        info = drm_fb_helper_alloc_fbi(helper);
 391        if (IS_ERR(info)) {
 392                ret = PTR_ERR(info);
 393                goto out_gfree;
 394        }
 395        info->par = ufbdev;
 396
 397        ret = udl_framebuffer_init(dev, &ufbdev->ufb, &mode_cmd, obj);
 398        if (ret)
 399                goto out_gfree;
 400
 401        fb = &ufbdev->ufb.base;
 402
 403        ufbdev->helper.fb = fb;
 404
 405        strcpy(info->fix.id, "udldrmfb");
 406
 407        info->screen_base = ufbdev->ufb.obj->vmapping;
 408        info->fix.smem_len = size;
 409        info->fix.smem_start = (unsigned long)ufbdev->ufb.obj->vmapping;
 410
 411        info->fbops = &udlfb_ops;
 412        drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
 413        drm_fb_helper_fill_var(info, &ufbdev->helper, sizes->fb_width, sizes->fb_height);
 414
 415        DRM_DEBUG_KMS("allocated %dx%d vmal %p\n",
 416                      fb->width, fb->height,
 417                      ufbdev->ufb.obj->vmapping);
 418
 419        return ret;
 420out_gfree:
 421        drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
 422out:
 423        return ret;
 424}
 425
 426static const struct drm_fb_helper_funcs udl_fb_helper_funcs = {
 427        .fb_probe = udlfb_create,
 428};
 429
 430static void udl_fbdev_destroy(struct drm_device *dev,
 431                              struct udl_fbdev *ufbdev)
 432{
 433        drm_fb_helper_unregister_fbi(&ufbdev->helper);
 434        drm_fb_helper_fini(&ufbdev->helper);
 435        if (ufbdev->ufb.obj) {
 436                drm_framebuffer_unregister_private(&ufbdev->ufb.base);
 437                drm_framebuffer_cleanup(&ufbdev->ufb.base);
 438                drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
 439        }
 440}
 441
 442int udl_fbdev_init(struct drm_device *dev)
 443{
 444        struct udl_device *udl = dev->dev_private;
 445        int bpp_sel = fb_bpp;
 446        struct udl_fbdev *ufbdev;
 447        int ret;
 448
 449        ufbdev = kzalloc(sizeof(struct udl_fbdev), GFP_KERNEL);
 450        if (!ufbdev)
 451                return -ENOMEM;
 452
 453        udl->fbdev = ufbdev;
 454
 455        drm_fb_helper_prepare(dev, &ufbdev->helper, &udl_fb_helper_funcs);
 456
 457        ret = drm_fb_helper_init(dev, &ufbdev->helper, 1);
 458        if (ret)
 459                goto free;
 460
 461        ret = drm_fb_helper_single_add_all_connectors(&ufbdev->helper);
 462        if (ret)
 463                goto fini;
 464
 465        /* disable all the possible outputs/crtcs before entering KMS mode */
 466        drm_helper_disable_unused_functions(dev);
 467
 468        ret = drm_fb_helper_initial_config(&ufbdev->helper, bpp_sel);
 469        if (ret)
 470                goto fini;
 471
 472        return 0;
 473
 474fini:
 475        drm_fb_helper_fini(&ufbdev->helper);
 476free:
 477        kfree(ufbdev);
 478        return ret;
 479}
 480
 481void udl_fbdev_cleanup(struct drm_device *dev)
 482{
 483        struct udl_device *udl = dev->dev_private;
 484        if (!udl->fbdev)
 485                return;
 486
 487        udl_fbdev_destroy(dev, udl->fbdev);
 488        kfree(udl->fbdev);
 489        udl->fbdev = NULL;
 490}
 491
 492void udl_fbdev_unplug(struct drm_device *dev)
 493{
 494        struct udl_device *udl = dev->dev_private;
 495        struct udl_fbdev *ufbdev;
 496        if (!udl->fbdev)
 497                return;
 498
 499        ufbdev = udl->fbdev;
 500        drm_fb_helper_unlink_fbi(&ufbdev->helper);
 501}
 502
 503struct drm_framebuffer *
 504udl_fb_user_fb_create(struct drm_device *dev,
 505                   struct drm_file *file,
 506                   const struct drm_mode_fb_cmd2 *mode_cmd)
 507{
 508        struct drm_gem_object *obj;
 509        struct udl_framebuffer *ufb;
 510        int ret;
 511        uint32_t size;
 512
 513        obj = drm_gem_object_lookup(file, mode_cmd->handles[0]);
 514        if (obj == NULL)
 515                return ERR_PTR(-ENOENT);
 516
 517        size = mode_cmd->pitches[0] * mode_cmd->height;
 518        size = ALIGN(size, PAGE_SIZE);
 519
 520        if (size > obj->size) {
 521                DRM_ERROR("object size not sufficient for fb %d %zu %d %d\n", size, obj->size, mode_cmd->pitches[0], mode_cmd->height);
 522                return ERR_PTR(-ENOMEM);
 523        }
 524
 525        ufb = kzalloc(sizeof(*ufb), GFP_KERNEL);
 526        if (ufb == NULL)
 527                return ERR_PTR(-ENOMEM);
 528
 529        ret = udl_framebuffer_init(dev, ufb, mode_cmd, to_udl_bo(obj));
 530        if (ret) {
 531                kfree(ufb);
 532                return ERR_PTR(-EINVAL);
 533        }
 534        return &ufb->base;
 535}
 536