GNU Linux-libre 4.4.284-gnu1
[releases.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 <drm/drmP.h>
19 #include <drm/drm_crtc.h>
20 #include <drm/drm_crtc_helper.h>
21 #include "udl_drv.h"
22
23 #include <drm/drm_fb_helper.h>
24
25 #define DL_DEFIO_WRITE_DELAY    (HZ/20) /* fb_deferred_io.delay in jiffies */
26
27 static int fb_defio = 0;  /* 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         int x2, y2;
157         bool store_for_later = false;
158         unsigned long flags;
159
160         if (!fb->active_16)
161                 return 0;
162
163         if (!fb->obj->vmapping) {
164                 ret = udl_gem_vmap(fb->obj);
165                 if (ret == -ENOMEM) {
166                         DRM_ERROR("failed to vmap fb\n");
167                         return 0;
168                 }
169                 if (!fb->obj->vmapping) {
170                         DRM_ERROR("failed to vmapping\n");
171                         return 0;
172                 }
173         }
174
175         aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
176         width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
177         x = aligned_x;
178
179         if ((width <= 0) ||
180             (x + width > fb->base.width) ||
181             (y + height > fb->base.height))
182                 return -EINVAL;
183
184         /* if we are in atomic just store the info
185            can't test inside spin lock */
186         if (in_atomic())
187                 store_for_later = true;
188
189         x2 = x + width - 1;
190         y2 = y + height - 1;
191
192         spin_lock_irqsave(&fb->dirty_lock, flags);
193
194         if (fb->y1 < y)
195                 y = fb->y1;
196         if (fb->y2 > y2)
197                 y2 = fb->y2;
198         if (fb->x1 < x)
199                 x = fb->x1;
200         if (fb->x2 > x2)
201                 x2 = fb->x2;
202
203         if (store_for_later) {
204                 fb->x1 = x;
205                 fb->x2 = x2;
206                 fb->y1 = y;
207                 fb->y2 = y2;
208                 spin_unlock_irqrestore(&fb->dirty_lock, flags);
209                 return 0;
210         }
211
212         fb->x1 = fb->y1 = INT_MAX;
213         fb->x2 = fb->y2 = 0;
214
215         spin_unlock_irqrestore(&fb->dirty_lock, flags);
216         start_cycles = get_cycles();
217
218         urb = udl_get_urb(dev);
219         if (!urb)
220                 return 0;
221         cmd = urb->transfer_buffer;
222
223         for (i = y; i <= y2 ; i++) {
224                 const int line_offset = fb->base.pitches[0] * i;
225                 const int byte_offset = line_offset + (x * bpp);
226                 const int dev_byte_offset = (fb->base.width * bpp * i) + (x * bpp);
227                 if (udl_render_hline(dev, bpp, &urb,
228                                      (char *) fb->obj->vmapping,
229                                      &cmd, byte_offset, dev_byte_offset,
230                                      (x2 - x + 1) * bpp,
231                                      &bytes_identical, &bytes_sent))
232                         goto error;
233         }
234
235         if (cmd > (char *) urb->transfer_buffer) {
236                 /* Send partial buffer remaining before exiting */
237                 int len = cmd - (char *) urb->transfer_buffer;
238                 ret = udl_submit_urb(dev, urb, len);
239                 bytes_sent += len;
240         } else
241                 udl_urb_completion(urb);
242
243 error:
244         atomic_add(bytes_sent, &udl->bytes_sent);
245         atomic_add(bytes_identical, &udl->bytes_identical);
246         atomic_add(width*height*bpp, &udl->bytes_rendered);
247         end_cycles = get_cycles();
248         atomic_add(((unsigned int) ((end_cycles - start_cycles)
249                     >> 10)), /* Kcycles */
250                    &udl->cpu_kcycles_used);
251
252         return 0;
253 }
254
255 static int udl_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
256 {
257         unsigned long start = vma->vm_start;
258         unsigned long size = vma->vm_end - vma->vm_start;
259         unsigned long offset;
260         unsigned long page, pos;
261
262         if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
263                 return -EINVAL;
264
265         offset = vma->vm_pgoff << PAGE_SHIFT;
266
267         if (offset > info->fix.smem_len || size > info->fix.smem_len - offset)
268                 return -EINVAL;
269
270         pos = (unsigned long)info->fix.smem_start + offset;
271
272         pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
273                   pos, size);
274
275         while (size > 0) {
276                 page = vmalloc_to_pfn((void *)pos);
277                 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
278                         return -EAGAIN;
279
280                 start += PAGE_SIZE;
281                 pos += PAGE_SIZE;
282                 if (size > PAGE_SIZE)
283                         size -= PAGE_SIZE;
284                 else
285                         size = 0;
286         }
287
288         /* VM_IO | VM_DONTEXPAND | VM_DONTDUMP are set by remap_pfn_range() */
289         return 0;
290 }
291
292 static void udl_fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
293 {
294         struct udl_fbdev *ufbdev = info->par;
295
296         drm_fb_helper_sys_fillrect(info, rect);
297
298         udl_handle_damage(&ufbdev->ufb, rect->dx, rect->dy, rect->width,
299                           rect->height);
300 }
301
302 static void udl_fb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
303 {
304         struct udl_fbdev *ufbdev = info->par;
305
306         drm_fb_helper_sys_copyarea(info, region);
307
308         udl_handle_damage(&ufbdev->ufb, region->dx, region->dy, region->width,
309                           region->height);
310 }
311
312 static void udl_fb_imageblit(struct fb_info *info, const struct fb_image *image)
313 {
314         struct udl_fbdev *ufbdev = info->par;
315
316         drm_fb_helper_sys_imageblit(info, image);
317
318         udl_handle_damage(&ufbdev->ufb, image->dx, image->dy, image->width,
319                           image->height);
320 }
321
322 /*
323  * It's common for several clients to have framebuffer open simultaneously.
324  * e.g. both fbcon and X. Makes things interesting.
325  * Assumes caller is holding info->lock (for open and release at least)
326  */
327 static int udl_fb_open(struct fb_info *info, int user)
328 {
329         struct udl_fbdev *ufbdev = info->par;
330         struct drm_device *dev = ufbdev->ufb.base.dev;
331         struct udl_device *udl = dev->dev_private;
332
333         /* If the USB device is gone, we don't accept new opens */
334         if (drm_device_is_unplugged(udl->ddev))
335                 return -ENODEV;
336
337         ufbdev->fb_count++;
338
339         if (fb_defio && (info->fbdefio == NULL)) {
340                 /* enable defio at last moment if not disabled by client */
341
342                 struct fb_deferred_io *fbdefio;
343
344                 fbdefio = kzalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
345
346                 if (fbdefio) {
347                         fbdefio->delay = DL_DEFIO_WRITE_DELAY;
348                         fbdefio->deferred_io = udlfb_dpy_deferred_io;
349                 }
350
351                 info->fbdefio = fbdefio;
352                 fb_deferred_io_init(info);
353         }
354
355         pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
356                   info->node, user, info, ufbdev->fb_count);
357
358         return 0;
359 }
360
361
362 /*
363  * Assumes caller is holding info->lock mutex (for open and release at least)
364  */
365 static int udl_fb_release(struct fb_info *info, int user)
366 {
367         struct udl_fbdev *ufbdev = info->par;
368
369         ufbdev->fb_count--;
370
371         if ((ufbdev->fb_count == 0) && (info->fbdefio)) {
372                 fb_deferred_io_cleanup(info);
373                 kfree(info->fbdefio);
374                 info->fbdefio = NULL;
375                 info->fbops->fb_mmap = udl_fb_mmap;
376         }
377
378         pr_warn("released /dev/fb%d user=%d count=%d\n",
379                 info->node, user, ufbdev->fb_count);
380
381         return 0;
382 }
383
384 static struct fb_ops udlfb_ops = {
385         .owner = THIS_MODULE,
386         .fb_check_var = drm_fb_helper_check_var,
387         .fb_set_par = drm_fb_helper_set_par,
388         .fb_fillrect = udl_fb_fillrect,
389         .fb_copyarea = udl_fb_copyarea,
390         .fb_imageblit = udl_fb_imageblit,
391         .fb_pan_display = drm_fb_helper_pan_display,
392         .fb_blank = drm_fb_helper_blank,
393         .fb_setcmap = drm_fb_helper_setcmap,
394         .fb_debug_enter = drm_fb_helper_debug_enter,
395         .fb_debug_leave = drm_fb_helper_debug_leave,
396         .fb_mmap = udl_fb_mmap,
397         .fb_open = udl_fb_open,
398         .fb_release = udl_fb_release,
399 };
400
401 static int udl_user_framebuffer_dirty(struct drm_framebuffer *fb,
402                                       struct drm_file *file,
403                                       unsigned flags, unsigned color,
404                                       struct drm_clip_rect *clips,
405                                       unsigned num_clips)
406 {
407         struct udl_framebuffer *ufb = to_udl_fb(fb);
408         int i;
409         int ret = 0;
410
411         drm_modeset_lock_all(fb->dev);
412
413         if (!ufb->active_16)
414                 goto unlock;
415
416         if (ufb->obj->base.import_attach) {
417                 ret = dma_buf_begin_cpu_access(ufb->obj->base.import_attach->dmabuf,
418                                                0, ufb->obj->base.size,
419                                                DMA_FROM_DEVICE);
420                 if (ret)
421                         goto unlock;
422         }
423
424         for (i = 0; i < num_clips; i++) {
425                 ret = udl_handle_damage(ufb, clips[i].x1, clips[i].y1,
426                                   clips[i].x2 - clips[i].x1,
427                                   clips[i].y2 - clips[i].y1);
428                 if (ret)
429                         break;
430         }
431
432         if (ufb->obj->base.import_attach) {
433                 dma_buf_end_cpu_access(ufb->obj->base.import_attach->dmabuf,
434                                        0, ufb->obj->base.size,
435                                        DMA_FROM_DEVICE);
436         }
437
438  unlock:
439         drm_modeset_unlock_all(fb->dev);
440
441         return ret;
442 }
443
444 static void udl_user_framebuffer_destroy(struct drm_framebuffer *fb)
445 {
446         struct udl_framebuffer *ufb = to_udl_fb(fb);
447
448         if (ufb->obj)
449                 drm_gem_object_unreference_unlocked(&ufb->obj->base);
450
451         drm_framebuffer_cleanup(fb);
452         kfree(ufb);
453 }
454
455 static const struct drm_framebuffer_funcs udlfb_funcs = {
456         .destroy = udl_user_framebuffer_destroy,
457         .dirty = udl_user_framebuffer_dirty,
458 };
459
460
461 static int
462 udl_framebuffer_init(struct drm_device *dev,
463                      struct udl_framebuffer *ufb,
464                      struct drm_mode_fb_cmd2 *mode_cmd,
465                      struct udl_gem_object *obj)
466 {
467         int ret;
468
469         spin_lock_init(&ufb->dirty_lock);
470         ufb->obj = obj;
471         drm_helper_mode_fill_fb_struct(&ufb->base, mode_cmd);
472         ret = drm_framebuffer_init(dev, &ufb->base, &udlfb_funcs);
473         return ret;
474 }
475
476
477 static int udlfb_create(struct drm_fb_helper *helper,
478                         struct drm_fb_helper_surface_size *sizes)
479 {
480         struct udl_fbdev *ufbdev =
481                 container_of(helper, struct udl_fbdev, helper);
482         struct drm_device *dev = ufbdev->helper.dev;
483         struct fb_info *info;
484         struct drm_framebuffer *fb;
485         struct drm_mode_fb_cmd2 mode_cmd;
486         struct udl_gem_object *obj;
487         uint32_t size;
488         int ret = 0;
489
490         if (sizes->surface_bpp == 24)
491                 sizes->surface_bpp = 32;
492
493         mode_cmd.width = sizes->surface_width;
494         mode_cmd.height = sizes->surface_height;
495         mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
496
497         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
498                                                           sizes->surface_depth);
499
500         size = mode_cmd.pitches[0] * mode_cmd.height;
501         size = ALIGN(size, PAGE_SIZE);
502
503         obj = udl_gem_alloc_object(dev, size);
504         if (!obj)
505                 goto out;
506
507         ret = udl_gem_vmap(obj);
508         if (ret) {
509                 DRM_ERROR("failed to vmap fb\n");
510                 goto out_gfree;
511         }
512
513         info = drm_fb_helper_alloc_fbi(helper);
514         if (IS_ERR(info)) {
515                 ret = PTR_ERR(info);
516                 goto out_gfree;
517         }
518         info->par = ufbdev;
519
520         ret = udl_framebuffer_init(dev, &ufbdev->ufb, &mode_cmd, obj);
521         if (ret)
522                 goto out_destroy_fbi;
523
524         fb = &ufbdev->ufb.base;
525
526         ufbdev->helper.fb = fb;
527
528         strcpy(info->fix.id, "udldrmfb");
529
530         info->screen_base = ufbdev->ufb.obj->vmapping;
531         info->fix.smem_len = size;
532         info->fix.smem_start = (unsigned long)ufbdev->ufb.obj->vmapping;
533
534         info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
535         info->fbops = &udlfb_ops;
536         drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
537         drm_fb_helper_fill_var(info, &ufbdev->helper, sizes->fb_width, sizes->fb_height);
538
539         DRM_DEBUG_KMS("allocated %dx%d vmal %p\n",
540                       fb->width, fb->height,
541                       ufbdev->ufb.obj->vmapping);
542
543         return ret;
544 out_destroy_fbi:
545         drm_fb_helper_release_fbi(helper);
546 out_gfree:
547         drm_gem_object_unreference_unlocked(&ufbdev->ufb.obj->base);
548 out:
549         return ret;
550 }
551
552 static const struct drm_fb_helper_funcs udl_fb_helper_funcs = {
553         .fb_probe = udlfb_create,
554 };
555
556 static void udl_fbdev_destroy(struct drm_device *dev,
557                               struct udl_fbdev *ufbdev)
558 {
559         drm_fb_helper_unregister_fbi(&ufbdev->helper);
560         drm_fb_helper_release_fbi(&ufbdev->helper);
561         drm_fb_helper_fini(&ufbdev->helper);
562         drm_framebuffer_unregister_private(&ufbdev->ufb.base);
563         drm_framebuffer_cleanup(&ufbdev->ufb.base);
564         drm_gem_object_unreference_unlocked(&ufbdev->ufb.obj->base);
565 }
566
567 int udl_fbdev_init(struct drm_device *dev)
568 {
569         struct udl_device *udl = dev->dev_private;
570         int bpp_sel = fb_bpp;
571         struct udl_fbdev *ufbdev;
572         int ret;
573
574         ufbdev = kzalloc(sizeof(struct udl_fbdev), GFP_KERNEL);
575         if (!ufbdev)
576                 return -ENOMEM;
577
578         udl->fbdev = ufbdev;
579
580         drm_fb_helper_prepare(dev, &ufbdev->helper, &udl_fb_helper_funcs);
581
582         ret = drm_fb_helper_init(dev, &ufbdev->helper,
583                                  1, 1);
584         if (ret)
585                 goto free;
586
587         ret = drm_fb_helper_single_add_all_connectors(&ufbdev->helper);
588         if (ret)
589                 goto fini;
590
591         /* disable all the possible outputs/crtcs before entering KMS mode */
592         drm_helper_disable_unused_functions(dev);
593
594         ret = drm_fb_helper_initial_config(&ufbdev->helper, bpp_sel);
595         if (ret)
596                 goto fini;
597
598         return 0;
599
600 fini:
601         drm_fb_helper_fini(&ufbdev->helper);
602 free:
603         kfree(ufbdev);
604         return ret;
605 }
606
607 void udl_fbdev_cleanup(struct drm_device *dev)
608 {
609         struct udl_device *udl = dev->dev_private;
610         if (!udl->fbdev)
611                 return;
612
613         udl_fbdev_destroy(dev, udl->fbdev);
614         kfree(udl->fbdev);
615         udl->fbdev = NULL;
616 }
617
618 void udl_fbdev_unplug(struct drm_device *dev)
619 {
620         struct udl_device *udl = dev->dev_private;
621         struct udl_fbdev *ufbdev;
622         if (!udl->fbdev)
623                 return;
624
625         ufbdev = udl->fbdev;
626         drm_fb_helper_unlink_fbi(&ufbdev->helper);
627 }
628
629 struct drm_framebuffer *
630 udl_fb_user_fb_create(struct drm_device *dev,
631                    struct drm_file *file,
632                    struct drm_mode_fb_cmd2 *mode_cmd)
633 {
634         struct drm_gem_object *obj;
635         struct udl_framebuffer *ufb;
636         int ret;
637         uint32_t size;
638
639         obj = drm_gem_object_lookup(dev, file, mode_cmd->handles[0]);
640         if (obj == NULL)
641                 return ERR_PTR(-ENOENT);
642
643         size = mode_cmd->pitches[0] * mode_cmd->height;
644         size = ALIGN(size, PAGE_SIZE);
645
646         if (size > obj->size) {
647                 DRM_ERROR("object size not sufficient for fb %d %zu %d %d\n", size, obj->size, mode_cmd->pitches[0], mode_cmd->height);
648                 return ERR_PTR(-ENOMEM);
649         }
650
651         ufb = kzalloc(sizeof(*ufb), GFP_KERNEL);
652         if (ufb == NULL)
653                 return ERR_PTR(-ENOMEM);
654
655         ret = udl_framebuffer_init(dev, ufb, mode_cmd, to_udl_bo(obj));
656         if (ret) {
657                 kfree(ufb);
658                 return ERR_PTR(-EINVAL);
659         }
660         return &ufb->base;
661 }