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