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