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