Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 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> |
Dave Airlie | 32ecd24 | 2012-07-30 14:06:55 +1000 | [diff] [blame] | 16 | #include <linux/dma-buf.h> |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 17 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 18 | #include <drm/drmP.h> |
| 19 | #include <drm/drm_crtc.h> |
| 20 | #include <drm/drm_crtc_helper.h> |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 21 | #include "udl_drv.h" |
| 22 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 23 | #include <drm/drm_fb_helper.h> |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 24 | |
Dave Airlie | 677d23b | 2013-02-07 12:30:25 +1000 | [diff] [blame] | 25 | #define DL_DEFIO_WRITE_DELAY (HZ/20) /* fb_deferred_io.delay in jiffies */ |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 26 | |
Dave Airlie | 677d23b | 2013-02-07 12:30:25 +1000 | [diff] [blame] | 27 | static int fb_defio = 0; /* Optionally enable experimental fb_defio mmap support */ |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 28 | static int fb_bpp = 16; |
| 29 | |
| 30 | module_param(fb_bpp, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP); |
| 31 | module_param(fb_defio, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP); |
| 32 | |
| 33 | struct udl_fbdev { |
| 34 | struct drm_fb_helper helper; |
| 35 | struct udl_framebuffer ufb; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 36 | int fb_count; |
| 37 | }; |
| 38 | |
| 39 | #define DL_ALIGN_UP(x, a) ALIGN(x, a) |
| 40 | #define DL_ALIGN_DOWN(x, a) ALIGN(x-(a-1), a) |
| 41 | |
| 42 | /** Read the red component (0..255) of a 32 bpp colour. */ |
| 43 | #define DLO_RGB_GETRED(col) (uint8_t)((col) & 0xFF) |
| 44 | |
| 45 | /** Read the green component (0..255) of a 32 bpp colour. */ |
| 46 | #define DLO_RGB_GETGRN(col) (uint8_t)(((col) >> 8) & 0xFF) |
| 47 | |
| 48 | /** Read the blue component (0..255) of a 32 bpp colour. */ |
| 49 | #define DLO_RGB_GETBLU(col) (uint8_t)(((col) >> 16) & 0xFF) |
| 50 | |
| 51 | /** Return red/green component of a 16 bpp colour number. */ |
| 52 | #define DLO_RG16(red, grn) (uint8_t)((((red) & 0xF8) | ((grn) >> 5)) & 0xFF) |
| 53 | |
| 54 | /** Return green/blue component of a 16 bpp colour number. */ |
| 55 | #define DLO_GB16(grn, blu) (uint8_t)(((((grn) & 0x1C) << 3) | ((blu) >> 3)) & 0xFF) |
| 56 | |
| 57 | /** Return 8 bpp colour number from red, green and blue components. */ |
| 58 | #define DLO_RGB8(red, grn, blu) ((((red) << 5) | (((grn) & 3) << 3) | ((blu) & 7)) & 0xFF) |
| 59 | |
| 60 | #if 0 |
| 61 | static uint8_t rgb8(uint32_t col) |
| 62 | { |
| 63 | uint8_t red = DLO_RGB_GETRED(col); |
| 64 | uint8_t grn = DLO_RGB_GETGRN(col); |
| 65 | uint8_t blu = DLO_RGB_GETBLU(col); |
| 66 | |
| 67 | return DLO_RGB8(red, grn, blu); |
| 68 | } |
| 69 | |
| 70 | static uint16_t rgb16(uint32_t col) |
| 71 | { |
| 72 | uint8_t red = DLO_RGB_GETRED(col); |
| 73 | uint8_t grn = DLO_RGB_GETGRN(col); |
| 74 | uint8_t blu = DLO_RGB_GETBLU(col); |
| 75 | |
| 76 | return (DLO_RG16(red, grn) << 8) + DLO_GB16(grn, blu); |
| 77 | } |
| 78 | #endif |
| 79 | |
| 80 | /* |
| 81 | * NOTE: fb_defio.c is holding info->fbdefio.mutex |
| 82 | * Touching ANY framebuffer memory that triggers a page fault |
| 83 | * in fb_defio will cause a deadlock, when it also tries to |
| 84 | * grab the same mutex. |
| 85 | */ |
| 86 | static void udlfb_dpy_deferred_io(struct fb_info *info, |
| 87 | struct list_head *pagelist) |
| 88 | { |
| 89 | struct page *cur; |
| 90 | struct fb_deferred_io *fbdefio = info->fbdefio; |
| 91 | struct udl_fbdev *ufbdev = info->par; |
| 92 | struct drm_device *dev = ufbdev->ufb.base.dev; |
| 93 | struct udl_device *udl = dev->dev_private; |
| 94 | struct urb *urb; |
| 95 | char *cmd; |
| 96 | cycles_t start_cycles, end_cycles; |
| 97 | int bytes_sent = 0; |
| 98 | int bytes_identical = 0; |
| 99 | int bytes_rendered = 0; |
| 100 | |
| 101 | if (!fb_defio) |
| 102 | return; |
| 103 | |
| 104 | start_cycles = get_cycles(); |
| 105 | |
| 106 | urb = udl_get_urb(dev); |
| 107 | if (!urb) |
| 108 | return; |
| 109 | |
| 110 | cmd = urb->transfer_buffer; |
| 111 | |
| 112 | /* walk the written page list and render each to device */ |
| 113 | list_for_each_entry(cur, &fbdefio->pagelist, lru) { |
| 114 | |
| 115 | if (udl_render_hline(dev, (ufbdev->ufb.base.bits_per_pixel / 8), |
Dave Airlie | 3916e1d | 2012-11-01 13:47:09 +1000 | [diff] [blame] | 116 | &urb, (char *) info->fix.smem_start, |
| 117 | &cmd, cur->index << PAGE_SHIFT, |
| 118 | cur->index << PAGE_SHIFT, |
| 119 | PAGE_SIZE, &bytes_identical, &bytes_sent)) |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 120 | goto error; |
| 121 | bytes_rendered += PAGE_SIZE; |
| 122 | } |
| 123 | |
| 124 | if (cmd > (char *) urb->transfer_buffer) { |
| 125 | /* Send partial buffer remaining before exiting */ |
| 126 | int len = cmd - (char *) urb->transfer_buffer; |
| 127 | udl_submit_urb(dev, urb, len); |
| 128 | bytes_sent += len; |
| 129 | } else |
| 130 | udl_urb_completion(urb); |
| 131 | |
| 132 | error: |
| 133 | atomic_add(bytes_sent, &udl->bytes_sent); |
| 134 | atomic_add(bytes_identical, &udl->bytes_identical); |
| 135 | atomic_add(bytes_rendered, &udl->bytes_rendered); |
| 136 | end_cycles = get_cycles(); |
| 137 | atomic_add(((unsigned int) ((end_cycles - start_cycles) |
| 138 | >> 10)), /* Kcycles */ |
| 139 | &udl->cpu_kcycles_used); |
| 140 | } |
| 141 | |
| 142 | int udl_handle_damage(struct udl_framebuffer *fb, int x, int y, |
| 143 | int width, int height) |
| 144 | { |
| 145 | struct drm_device *dev = fb->base.dev; |
| 146 | struct udl_device *udl = dev->dev_private; |
| 147 | int i, ret; |
| 148 | char *cmd; |
| 149 | cycles_t start_cycles, end_cycles; |
| 150 | int bytes_sent = 0; |
| 151 | int bytes_identical = 0; |
| 152 | struct urb *urb; |
| 153 | int aligned_x; |
| 154 | int bpp = (fb->base.bits_per_pixel / 8); |
Dave Airlie | bcb39af | 2013-02-07 11:19:15 +1000 | [diff] [blame] | 155 | int x2, y2; |
| 156 | bool store_for_later = false; |
| 157 | unsigned long flags; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 158 | |
| 159 | if (!fb->active_16) |
| 160 | return 0; |
| 161 | |
Dave Airlie | e8aa1d1 | 2012-03-26 14:36:56 +0100 | [diff] [blame] | 162 | if (!fb->obj->vmapping) { |
| 163 | ret = udl_gem_vmap(fb->obj); |
| 164 | if (ret == -ENOMEM) { |
| 165 | DRM_ERROR("failed to vmap fb\n"); |
| 166 | return 0; |
| 167 | } |
| 168 | if (!fb->obj->vmapping) { |
| 169 | DRM_ERROR("failed to vmapping\n"); |
| 170 | return 0; |
| 171 | } |
| 172 | } |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 173 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 174 | aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long)); |
| 175 | width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long)); |
| 176 | x = aligned_x; |
| 177 | |
| 178 | if ((width <= 0) || |
| 179 | (x + width > fb->base.width) || |
| 180 | (y + height > fb->base.height)) |
| 181 | return -EINVAL; |
| 182 | |
Dave Airlie | bcb39af | 2013-02-07 11:19:15 +1000 | [diff] [blame] | 183 | /* if we are in atomic just store the info |
| 184 | can't test inside spin lock */ |
| 185 | if (in_atomic()) |
| 186 | store_for_later = true; |
| 187 | |
| 188 | x2 = x + width - 1; |
| 189 | y2 = y + height - 1; |
| 190 | |
| 191 | spin_lock_irqsave(&fb->dirty_lock, flags); |
| 192 | |
| 193 | if (fb->y1 < y) |
| 194 | y = fb->y1; |
| 195 | if (fb->y2 > y2) |
| 196 | y2 = fb->y2; |
| 197 | if (fb->x1 < x) |
| 198 | x = fb->x1; |
| 199 | if (fb->x2 > x2) |
| 200 | x2 = fb->x2; |
| 201 | |
| 202 | if (store_for_later) { |
| 203 | fb->x1 = x; |
| 204 | fb->x2 = x2; |
| 205 | fb->y1 = y; |
| 206 | fb->y2 = y2; |
| 207 | spin_unlock_irqrestore(&fb->dirty_lock, flags); |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | fb->x1 = fb->y1 = INT_MAX; |
| 212 | fb->x2 = fb->y2 = 0; |
| 213 | |
| 214 | spin_unlock_irqrestore(&fb->dirty_lock, flags); |
| 215 | start_cycles = get_cycles(); |
| 216 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 217 | urb = udl_get_urb(dev); |
| 218 | if (!urb) |
| 219 | return 0; |
| 220 | cmd = urb->transfer_buffer; |
| 221 | |
Dave Airlie | bcb39af | 2013-02-07 11:19:15 +1000 | [diff] [blame] | 222 | for (i = y; i <= y2 ; i++) { |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 223 | const int line_offset = fb->base.pitches[0] * i; |
| 224 | const int byte_offset = line_offset + (x * bpp); |
Dave Airlie | 3916e1d | 2012-11-01 13:47:09 +1000 | [diff] [blame] | 225 | const int dev_byte_offset = (fb->base.width * bpp * i) + (x * bpp); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 226 | if (udl_render_hline(dev, bpp, &urb, |
| 227 | (char *) fb->obj->vmapping, |
Dave Airlie | 3916e1d | 2012-11-01 13:47:09 +1000 | [diff] [blame] | 228 | &cmd, byte_offset, dev_byte_offset, |
Dave Airlie | bcb39af | 2013-02-07 11:19:15 +1000 | [diff] [blame] | 229 | (x2 - x + 1) * bpp, |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 230 | &bytes_identical, &bytes_sent)) |
| 231 | goto error; |
| 232 | } |
| 233 | |
| 234 | if (cmd > (char *) urb->transfer_buffer) { |
| 235 | /* Send partial buffer remaining before exiting */ |
| 236 | int len = cmd - (char *) urb->transfer_buffer; |
| 237 | ret = udl_submit_urb(dev, urb, len); |
| 238 | bytes_sent += len; |
| 239 | } else |
| 240 | udl_urb_completion(urb); |
| 241 | |
| 242 | error: |
| 243 | atomic_add(bytes_sent, &udl->bytes_sent); |
| 244 | atomic_add(bytes_identical, &udl->bytes_identical); |
| 245 | atomic_add(width*height*bpp, &udl->bytes_rendered); |
| 246 | end_cycles = get_cycles(); |
| 247 | atomic_add(((unsigned int) ((end_cycles - start_cycles) |
| 248 | >> 10)), /* Kcycles */ |
| 249 | &udl->cpu_kcycles_used); |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static int udl_fb_mmap(struct fb_info *info, struct vm_area_struct *vma) |
| 255 | { |
| 256 | unsigned long start = vma->vm_start; |
| 257 | unsigned long size = vma->vm_end - vma->vm_start; |
| 258 | unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; |
| 259 | unsigned long page, pos; |
| 260 | |
| 261 | if (offset + size > info->fix.smem_len) |
| 262 | return -EINVAL; |
| 263 | |
| 264 | pos = (unsigned long)info->fix.smem_start + offset; |
| 265 | |
| 266 | pr_notice("mmap() framebuffer addr:%lu size:%lu\n", |
| 267 | pos, size); |
| 268 | |
| 269 | while (size > 0) { |
| 270 | page = vmalloc_to_pfn((void *)pos); |
| 271 | if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) |
| 272 | return -EAGAIN; |
| 273 | |
| 274 | start += PAGE_SIZE; |
| 275 | pos += PAGE_SIZE; |
| 276 | if (size > PAGE_SIZE) |
| 277 | size -= PAGE_SIZE; |
| 278 | else |
| 279 | size = 0; |
| 280 | } |
| 281 | |
Konstantin Khlebnikov | 314e51b | 2012-10-08 16:29:02 -0700 | [diff] [blame] | 282 | /* VM_IO | VM_DONTEXPAND | VM_DONTDUMP are set by remap_pfn_range() */ |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static void udl_fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect) |
| 287 | { |
| 288 | struct udl_fbdev *ufbdev = info->par; |
| 289 | |
Archit Taneja | 41ba2f3 | 2015-07-22 14:58:16 +0530 | [diff] [blame] | 290 | drm_fb_helper_sys_fillrect(info, rect); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 291 | |
| 292 | udl_handle_damage(&ufbdev->ufb, rect->dx, rect->dy, rect->width, |
| 293 | rect->height); |
| 294 | } |
| 295 | |
| 296 | static void udl_fb_copyarea(struct fb_info *info, const struct fb_copyarea *region) |
| 297 | { |
| 298 | struct udl_fbdev *ufbdev = info->par; |
| 299 | |
Archit Taneja | 41ba2f3 | 2015-07-22 14:58:16 +0530 | [diff] [blame] | 300 | drm_fb_helper_sys_copyarea(info, region); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 301 | |
| 302 | udl_handle_damage(&ufbdev->ufb, region->dx, region->dy, region->width, |
| 303 | region->height); |
| 304 | } |
| 305 | |
| 306 | static void udl_fb_imageblit(struct fb_info *info, const struct fb_image *image) |
| 307 | { |
| 308 | struct udl_fbdev *ufbdev = info->par; |
| 309 | |
Archit Taneja | 41ba2f3 | 2015-07-22 14:58:16 +0530 | [diff] [blame] | 310 | drm_fb_helper_sys_imageblit(info, image); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 311 | |
| 312 | udl_handle_damage(&ufbdev->ufb, image->dx, image->dy, image->width, |
| 313 | image->height); |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * It's common for several clients to have framebuffer open simultaneously. |
| 318 | * e.g. both fbcon and X. Makes things interesting. |
| 319 | * Assumes caller is holding info->lock (for open and release at least) |
| 320 | */ |
| 321 | static int udl_fb_open(struct fb_info *info, int user) |
| 322 | { |
| 323 | struct udl_fbdev *ufbdev = info->par; |
| 324 | struct drm_device *dev = ufbdev->ufb.base.dev; |
| 325 | struct udl_device *udl = dev->dev_private; |
| 326 | |
| 327 | /* If the USB device is gone, we don't accept new opens */ |
| 328 | if (drm_device_is_unplugged(udl->ddev)) |
| 329 | return -ENODEV; |
| 330 | |
| 331 | ufbdev->fb_count++; |
| 332 | |
| 333 | if (fb_defio && (info->fbdefio == NULL)) { |
| 334 | /* enable defio at last moment if not disabled by client */ |
| 335 | |
| 336 | struct fb_deferred_io *fbdefio; |
| 337 | |
| 338 | fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL); |
| 339 | |
| 340 | if (fbdefio) { |
| 341 | fbdefio->delay = DL_DEFIO_WRITE_DELAY; |
| 342 | fbdefio->deferred_io = udlfb_dpy_deferred_io; |
| 343 | } |
| 344 | |
| 345 | info->fbdefio = fbdefio; |
| 346 | fb_deferred_io_init(info); |
| 347 | } |
| 348 | |
| 349 | pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n", |
| 350 | info->node, user, info, ufbdev->fb_count); |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | |
| 356 | /* |
| 357 | * Assumes caller is holding info->lock mutex (for open and release at least) |
| 358 | */ |
| 359 | static int udl_fb_release(struct fb_info *info, int user) |
| 360 | { |
| 361 | struct udl_fbdev *ufbdev = info->par; |
| 362 | |
| 363 | ufbdev->fb_count--; |
| 364 | |
| 365 | if ((ufbdev->fb_count == 0) && (info->fbdefio)) { |
| 366 | fb_deferred_io_cleanup(info); |
| 367 | kfree(info->fbdefio); |
| 368 | info->fbdefio = NULL; |
| 369 | info->fbops->fb_mmap = udl_fb_mmap; |
| 370 | } |
| 371 | |
| 372 | pr_warn("released /dev/fb%d user=%d count=%d\n", |
| 373 | info->node, user, ufbdev->fb_count); |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | static struct fb_ops udlfb_ops = { |
| 379 | .owner = THIS_MODULE, |
| 380 | .fb_check_var = drm_fb_helper_check_var, |
| 381 | .fb_set_par = drm_fb_helper_set_par, |
| 382 | .fb_fillrect = udl_fb_fillrect, |
| 383 | .fb_copyarea = udl_fb_copyarea, |
| 384 | .fb_imageblit = udl_fb_imageblit, |
| 385 | .fb_pan_display = drm_fb_helper_pan_display, |
| 386 | .fb_blank = drm_fb_helper_blank, |
| 387 | .fb_setcmap = drm_fb_helper_setcmap, |
| 388 | .fb_debug_enter = drm_fb_helper_debug_enter, |
| 389 | .fb_debug_leave = drm_fb_helper_debug_leave, |
| 390 | .fb_mmap = udl_fb_mmap, |
| 391 | .fb_open = udl_fb_open, |
| 392 | .fb_release = udl_fb_release, |
| 393 | }; |
| 394 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 395 | static int udl_user_framebuffer_dirty(struct drm_framebuffer *fb, |
| 396 | struct drm_file *file, |
| 397 | unsigned flags, unsigned color, |
| 398 | struct drm_clip_rect *clips, |
| 399 | unsigned num_clips) |
| 400 | { |
| 401 | struct udl_framebuffer *ufb = to_udl_fb(fb); |
| 402 | int i; |
Dave Airlie | 32ecd24 | 2012-07-30 14:06:55 +1000 | [diff] [blame] | 403 | int ret = 0; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 404 | |
Ville Syrjälä | 73e9efd | 2013-12-04 14:13:58 +0200 | [diff] [blame] | 405 | drm_modeset_lock_all(fb->dev); |
| 406 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 407 | if (!ufb->active_16) |
Ville Syrjälä | 73e9efd | 2013-12-04 14:13:58 +0200 | [diff] [blame] | 408 | goto unlock; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 409 | |
Dave Airlie | 32ecd24 | 2012-07-30 14:06:55 +1000 | [diff] [blame] | 410 | if (ufb->obj->base.import_attach) { |
| 411 | ret = dma_buf_begin_cpu_access(ufb->obj->base.import_attach->dmabuf, |
Dave Airlie | 32ecd24 | 2012-07-30 14:06:55 +1000 | [diff] [blame] | 412 | DMA_FROM_DEVICE); |
| 413 | if (ret) |
Ville Syrjälä | 73e9efd | 2013-12-04 14:13:58 +0200 | [diff] [blame] | 414 | goto unlock; |
Dave Airlie | 32ecd24 | 2012-07-30 14:06:55 +1000 | [diff] [blame] | 415 | } |
| 416 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 417 | for (i = 0; i < num_clips; i++) { |
Dave Airlie | 32ecd24 | 2012-07-30 14:06:55 +1000 | [diff] [blame] | 418 | ret = udl_handle_damage(ufb, clips[i].x1, clips[i].y1, |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 419 | clips[i].x2 - clips[i].x1, |
| 420 | clips[i].y2 - clips[i].y1); |
Dave Airlie | 32ecd24 | 2012-07-30 14:06:55 +1000 | [diff] [blame] | 421 | if (ret) |
David Herrmann | 06c99161 | 2014-01-20 19:52:29 +0100 | [diff] [blame] | 422 | break; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 423 | } |
Dave Airlie | 32ecd24 | 2012-07-30 14:06:55 +1000 | [diff] [blame] | 424 | |
| 425 | if (ufb->obj->base.import_attach) { |
Chris Wilson | 18b862d | 2016-03-18 20:02:39 +0000 | [diff] [blame] | 426 | ret = dma_buf_end_cpu_access(ufb->obj->base.import_attach->dmabuf, |
| 427 | DMA_FROM_DEVICE); |
Dave Airlie | 32ecd24 | 2012-07-30 14:06:55 +1000 | [diff] [blame] | 428 | } |
Ville Syrjälä | 73e9efd | 2013-12-04 14:13:58 +0200 | [diff] [blame] | 429 | |
| 430 | unlock: |
| 431 | drm_modeset_unlock_all(fb->dev); |
| 432 | |
Dave Airlie | 32ecd24 | 2012-07-30 14:06:55 +1000 | [diff] [blame] | 433 | return ret; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | static void udl_user_framebuffer_destroy(struct drm_framebuffer *fb) |
| 437 | { |
| 438 | struct udl_framebuffer *ufb = to_udl_fb(fb); |
| 439 | |
| 440 | if (ufb->obj) |
| 441 | drm_gem_object_unreference_unlocked(&ufb->obj->base); |
| 442 | |
| 443 | drm_framebuffer_cleanup(fb); |
| 444 | kfree(ufb); |
| 445 | } |
| 446 | |
| 447 | static const struct drm_framebuffer_funcs udlfb_funcs = { |
| 448 | .destroy = udl_user_framebuffer_destroy, |
| 449 | .dirty = udl_user_framebuffer_dirty, |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 450 | }; |
| 451 | |
| 452 | |
| 453 | static int |
| 454 | udl_framebuffer_init(struct drm_device *dev, |
| 455 | struct udl_framebuffer *ufb, |
Ville Syrjälä | 1eb8345 | 2015-11-11 19:11:29 +0200 | [diff] [blame] | 456 | const struct drm_mode_fb_cmd2 *mode_cmd, |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 457 | struct udl_gem_object *obj) |
| 458 | { |
| 459 | int ret; |
| 460 | |
Dave Airlie | bcb39af | 2013-02-07 11:19:15 +1000 | [diff] [blame] | 461 | spin_lock_init(&ufb->dirty_lock); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 462 | ufb->obj = obj; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 463 | drm_helper_mode_fill_fb_struct(&ufb->base, mode_cmd); |
Daniel Vetter | c7d73f6 | 2012-12-13 23:38:38 +0100 | [diff] [blame] | 464 | ret = drm_framebuffer_init(dev, &ufb->base, &udlfb_funcs); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 465 | return ret; |
| 466 | } |
| 467 | |
| 468 | |
Daniel Vetter | cd5428a | 2013-01-21 23:42:49 +0100 | [diff] [blame] | 469 | static int udlfb_create(struct drm_fb_helper *helper, |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 470 | struct drm_fb_helper_surface_size *sizes) |
| 471 | { |
Fabian Frederick | ee0d68a | 2014-09-14 18:40:22 +0200 | [diff] [blame] | 472 | struct udl_fbdev *ufbdev = |
| 473 | container_of(helper, struct udl_fbdev, helper); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 474 | struct drm_device *dev = ufbdev->helper.dev; |
| 475 | struct fb_info *info; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 476 | struct drm_framebuffer *fb; |
| 477 | struct drm_mode_fb_cmd2 mode_cmd; |
| 478 | struct udl_gem_object *obj; |
| 479 | uint32_t size; |
| 480 | int ret = 0; |
| 481 | |
| 482 | if (sizes->surface_bpp == 24) |
| 483 | sizes->surface_bpp = 32; |
| 484 | |
| 485 | mode_cmd.width = sizes->surface_width; |
| 486 | mode_cmd.height = sizes->surface_height; |
| 487 | mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8); |
| 488 | |
| 489 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, |
| 490 | sizes->surface_depth); |
| 491 | |
| 492 | size = mode_cmd.pitches[0] * mode_cmd.height; |
| 493 | size = ALIGN(size, PAGE_SIZE); |
| 494 | |
| 495 | obj = udl_gem_alloc_object(dev, size); |
| 496 | if (!obj) |
| 497 | goto out; |
| 498 | |
| 499 | ret = udl_gem_vmap(obj); |
| 500 | if (ret) { |
| 501 | DRM_ERROR("failed to vmap fb\n"); |
| 502 | goto out_gfree; |
| 503 | } |
| 504 | |
Archit Taneja | 41ba2f3 | 2015-07-22 14:58:16 +0530 | [diff] [blame] | 505 | info = drm_fb_helper_alloc_fbi(helper); |
| 506 | if (IS_ERR(info)) { |
| 507 | ret = PTR_ERR(info); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 508 | goto out_gfree; |
| 509 | } |
| 510 | info->par = ufbdev; |
| 511 | |
| 512 | ret = udl_framebuffer_init(dev, &ufbdev->ufb, &mode_cmd, obj); |
| 513 | if (ret) |
Archit Taneja | 41ba2f3 | 2015-07-22 14:58:16 +0530 | [diff] [blame] | 514 | goto out_destroy_fbi; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 515 | |
| 516 | fb = &ufbdev->ufb.base; |
| 517 | |
| 518 | ufbdev->helper.fb = fb; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 519 | |
| 520 | strcpy(info->fix.id, "udldrmfb"); |
| 521 | |
| 522 | info->screen_base = ufbdev->ufb.obj->vmapping; |
| 523 | info->fix.smem_len = size; |
| 524 | info->fix.smem_start = (unsigned long)ufbdev->ufb.obj->vmapping; |
| 525 | |
| 526 | info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT; |
| 527 | info->fbops = &udlfb_ops; |
| 528 | drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth); |
| 529 | drm_fb_helper_fill_var(info, &ufbdev->helper, sizes->fb_width, sizes->fb_height); |
| 530 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 531 | DRM_DEBUG_KMS("allocated %dx%d vmal %p\n", |
| 532 | fb->width, fb->height, |
| 533 | ufbdev->ufb.obj->vmapping); |
| 534 | |
| 535 | return ret; |
Archit Taneja | 41ba2f3 | 2015-07-22 14:58:16 +0530 | [diff] [blame] | 536 | out_destroy_fbi: |
| 537 | drm_fb_helper_release_fbi(helper); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 538 | out_gfree: |
| 539 | drm_gem_object_unreference(&ufbdev->ufb.obj->base); |
| 540 | out: |
| 541 | return ret; |
| 542 | } |
| 543 | |
Thierry Reding | 3a49387 | 2014-06-27 17:19:23 +0200 | [diff] [blame] | 544 | static const struct drm_fb_helper_funcs udl_fb_helper_funcs = { |
Daniel Vetter | cd5428a | 2013-01-21 23:42:49 +0100 | [diff] [blame] | 545 | .fb_probe = udlfb_create, |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 546 | }; |
| 547 | |
| 548 | static void udl_fbdev_destroy(struct drm_device *dev, |
| 549 | struct udl_fbdev *ufbdev) |
| 550 | { |
Archit Taneja | 41ba2f3 | 2015-07-22 14:58:16 +0530 | [diff] [blame] | 551 | drm_fb_helper_unregister_fbi(&ufbdev->helper); |
| 552 | drm_fb_helper_release_fbi(&ufbdev->helper); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 553 | drm_fb_helper_fini(&ufbdev->helper); |
Daniel Vetter | 3620636 | 2012-12-10 20:42:17 +0100 | [diff] [blame] | 554 | drm_framebuffer_unregister_private(&ufbdev->ufb.base); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 555 | drm_framebuffer_cleanup(&ufbdev->ufb.base); |
| 556 | drm_gem_object_unreference_unlocked(&ufbdev->ufb.obj->base); |
| 557 | } |
| 558 | |
| 559 | int udl_fbdev_init(struct drm_device *dev) |
| 560 | { |
| 561 | struct udl_device *udl = dev->dev_private; |
| 562 | int bpp_sel = fb_bpp; |
| 563 | struct udl_fbdev *ufbdev; |
| 564 | int ret; |
| 565 | |
| 566 | ufbdev = kzalloc(sizeof(struct udl_fbdev), GFP_KERNEL); |
| 567 | if (!ufbdev) |
| 568 | return -ENOMEM; |
| 569 | |
| 570 | udl->fbdev = ufbdev; |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 571 | |
| 572 | drm_fb_helper_prepare(dev, &ufbdev->helper, &udl_fb_helper_funcs); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 573 | |
| 574 | ret = drm_fb_helper_init(dev, &ufbdev->helper, |
| 575 | 1, 1); |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 576 | if (ret) |
| 577 | goto free; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 578 | |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 579 | ret = drm_fb_helper_single_add_all_connectors(&ufbdev->helper); |
| 580 | if (ret) |
| 581 | goto fini; |
Daniel Vetter | 76a39db | 2013-01-20 23:12:54 +0100 | [diff] [blame] | 582 | |
| 583 | /* disable all the possible outputs/crtcs before entering KMS mode */ |
| 584 | drm_helper_disable_unused_functions(dev); |
| 585 | |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 586 | ret = drm_fb_helper_initial_config(&ufbdev->helper, bpp_sel); |
| 587 | if (ret) |
| 588 | goto fini; |
| 589 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 590 | return 0; |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 591 | |
| 592 | fini: |
| 593 | drm_fb_helper_fini(&ufbdev->helper); |
| 594 | free: |
| 595 | kfree(ufbdev); |
| 596 | return ret; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | void udl_fbdev_cleanup(struct drm_device *dev) |
| 600 | { |
| 601 | struct udl_device *udl = dev->dev_private; |
| 602 | if (!udl->fbdev) |
| 603 | return; |
| 604 | |
| 605 | udl_fbdev_destroy(dev, udl->fbdev); |
| 606 | kfree(udl->fbdev); |
| 607 | udl->fbdev = NULL; |
| 608 | } |
| 609 | |
| 610 | void udl_fbdev_unplug(struct drm_device *dev) |
| 611 | { |
| 612 | struct udl_device *udl = dev->dev_private; |
| 613 | struct udl_fbdev *ufbdev; |
| 614 | if (!udl->fbdev) |
| 615 | return; |
| 616 | |
| 617 | ufbdev = udl->fbdev; |
Archit Taneja | 41ba2f3 | 2015-07-22 14:58:16 +0530 | [diff] [blame] | 618 | drm_fb_helper_unlink_fbi(&ufbdev->helper); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | struct drm_framebuffer * |
| 622 | udl_fb_user_fb_create(struct drm_device *dev, |
| 623 | struct drm_file *file, |
Ville Syrjälä | 1eb8345 | 2015-11-11 19:11:29 +0200 | [diff] [blame] | 624 | const struct drm_mode_fb_cmd2 *mode_cmd) |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 625 | { |
| 626 | struct drm_gem_object *obj; |
| 627 | struct udl_framebuffer *ufb; |
| 628 | int ret; |
Dave Airlie | 96503f5 | 2011-12-21 11:23:44 +0000 | [diff] [blame] | 629 | uint32_t size; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 630 | |
| 631 | obj = drm_gem_object_lookup(dev, file, mode_cmd->handles[0]); |
| 632 | if (obj == NULL) |
| 633 | return ERR_PTR(-ENOENT); |
| 634 | |
Dave Airlie | 96503f5 | 2011-12-21 11:23:44 +0000 | [diff] [blame] | 635 | size = mode_cmd->pitches[0] * mode_cmd->height; |
| 636 | size = ALIGN(size, PAGE_SIZE); |
| 637 | |
| 638 | if (size > obj->size) { |
| 639 | DRM_ERROR("object size not sufficient for fb %d %zu %d %d\n", size, obj->size, mode_cmd->pitches[0], mode_cmd->height); |
| 640 | return ERR_PTR(-ENOMEM); |
| 641 | } |
| 642 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 643 | ufb = kzalloc(sizeof(*ufb), GFP_KERNEL); |
| 644 | if (ufb == NULL) |
| 645 | return ERR_PTR(-ENOMEM); |
| 646 | |
| 647 | ret = udl_framebuffer_init(dev, ufb, mode_cmd, to_udl_bo(obj)); |
| 648 | if (ret) { |
| 649 | kfree(ufb); |
| 650 | return ERR_PTR(-EINVAL); |
| 651 | } |
| 652 | return &ufb->base; |
| 653 | } |