Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v5.2-rc1 531 lines 13 kB view raw
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; /* must be first */ 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 = to_udl(dev); 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 = to_udl(dev); 214 215 /* If the USB device is gone, we don't accept new opens */ 216 if (drm_dev_is_unplugged(&udl->drm)) 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 396 ret = udl_framebuffer_init(dev, &ufbdev->ufb, &mode_cmd, obj); 397 if (ret) 398 goto out_gfree; 399 400 fb = &ufbdev->ufb.base; 401 402 ufbdev->helper.fb = fb; 403 404 info->screen_base = ufbdev->ufb.obj->vmapping; 405 info->fix.smem_len = size; 406 info->fix.smem_start = (unsigned long)ufbdev->ufb.obj->vmapping; 407 408 info->fbops = &udlfb_ops; 409 drm_fb_helper_fill_info(info, &ufbdev->helper, sizes); 410 411 DRM_DEBUG_KMS("allocated %dx%d vmal %p\n", 412 fb->width, fb->height, 413 ufbdev->ufb.obj->vmapping); 414 415 return ret; 416out_gfree: 417 drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base); 418out: 419 return ret; 420} 421 422static const struct drm_fb_helper_funcs udl_fb_helper_funcs = { 423 .fb_probe = udlfb_create, 424}; 425 426static void udl_fbdev_destroy(struct drm_device *dev, 427 struct udl_fbdev *ufbdev) 428{ 429 drm_fb_helper_unregister_fbi(&ufbdev->helper); 430 drm_fb_helper_fini(&ufbdev->helper); 431 if (ufbdev->ufb.obj) { 432 drm_framebuffer_unregister_private(&ufbdev->ufb.base); 433 drm_framebuffer_cleanup(&ufbdev->ufb.base); 434 drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base); 435 } 436} 437 438int udl_fbdev_init(struct drm_device *dev) 439{ 440 struct udl_device *udl = to_udl(dev); 441 int bpp_sel = fb_bpp; 442 struct udl_fbdev *ufbdev; 443 int ret; 444 445 ufbdev = kzalloc(sizeof(struct udl_fbdev), GFP_KERNEL); 446 if (!ufbdev) 447 return -ENOMEM; 448 449 udl->fbdev = ufbdev; 450 451 drm_fb_helper_prepare(dev, &ufbdev->helper, &udl_fb_helper_funcs); 452 453 ret = drm_fb_helper_init(dev, &ufbdev->helper, 1); 454 if (ret) 455 goto free; 456 457 ret = drm_fb_helper_single_add_all_connectors(&ufbdev->helper); 458 if (ret) 459 goto fini; 460 461 /* disable all the possible outputs/crtcs before entering KMS mode */ 462 drm_helper_disable_unused_functions(dev); 463 464 ret = drm_fb_helper_initial_config(&ufbdev->helper, bpp_sel); 465 if (ret) 466 goto fini; 467 468 return 0; 469 470fini: 471 drm_fb_helper_fini(&ufbdev->helper); 472free: 473 kfree(ufbdev); 474 return ret; 475} 476 477void udl_fbdev_cleanup(struct drm_device *dev) 478{ 479 struct udl_device *udl = to_udl(dev); 480 if (!udl->fbdev) 481 return; 482 483 udl_fbdev_destroy(dev, udl->fbdev); 484 kfree(udl->fbdev); 485 udl->fbdev = NULL; 486} 487 488void udl_fbdev_unplug(struct drm_device *dev) 489{ 490 struct udl_device *udl = to_udl(dev); 491 struct udl_fbdev *ufbdev; 492 if (!udl->fbdev) 493 return; 494 495 ufbdev = udl->fbdev; 496 drm_fb_helper_unlink_fbi(&ufbdev->helper); 497} 498 499struct drm_framebuffer * 500udl_fb_user_fb_create(struct drm_device *dev, 501 struct drm_file *file, 502 const struct drm_mode_fb_cmd2 *mode_cmd) 503{ 504 struct drm_gem_object *obj; 505 struct udl_framebuffer *ufb; 506 int ret; 507 uint32_t size; 508 509 obj = drm_gem_object_lookup(file, mode_cmd->handles[0]); 510 if (obj == NULL) 511 return ERR_PTR(-ENOENT); 512 513 size = mode_cmd->pitches[0] * mode_cmd->height; 514 size = ALIGN(size, PAGE_SIZE); 515 516 if (size > obj->size) { 517 DRM_ERROR("object size not sufficient for fb %d %zu %d %d\n", size, obj->size, mode_cmd->pitches[0], mode_cmd->height); 518 return ERR_PTR(-ENOMEM); 519 } 520 521 ufb = kzalloc(sizeof(*ufb), GFP_KERNEL); 522 if (ufb == NULL) 523 return ERR_PTR(-ENOMEM); 524 525 ret = udl_framebuffer_init(dev, ufb, mode_cmd, to_udl_bo(obj)); 526 if (ret) { 527 kfree(ufb); 528 return ERR_PTR(-EINVAL); 529 } 530 return &ufb->base; 531}