]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/gpu/drm/udl/udl_fb.c
drm/udl: fix display corruption of the last line
[mirror_ubuntu-bionic-kernel.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 #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
28 static int fb_defio = 0; /* 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 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
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 int 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 = dev->dev_private;
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 bpp = fb->base.format->cpp[0];
94
95 if (!fb->active_16)
96 return 0;
97
98 if (!fb->obj->vmapping) {
99 ret = udl_gem_vmap(fb->obj);
100 if (ret == -ENOMEM) {
101 DRM_ERROR("failed to vmap fb\n");
102 return 0;
103 }
104 if (!fb->obj->vmapping) {
105 DRM_ERROR("failed to vmapping\n");
106 return 0;
107 }
108 }
109
110 aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
111 width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
112 x = aligned_x;
113
114 if ((width <= 0) ||
115 (x + width > fb->base.width) ||
116 (y + height > fb->base.height))
117 return -EINVAL;
118
119 start_cycles = get_cycles();
120
121 urb = udl_get_urb(dev);
122 if (!urb)
123 return 0;
124 cmd = urb->transfer_buffer;
125
126 for (i = y; i < y + height ; i++) {
127 const int line_offset = fb->base.pitches[0] * i;
128 const int byte_offset = line_offset + (x * bpp);
129 const int dev_byte_offset = (fb->base.width * bpp * i) + (x * bpp);
130 if (udl_render_hline(dev, bpp, &urb,
131 (char *) fb->obj->vmapping,
132 &cmd, byte_offset, dev_byte_offset,
133 width * bpp,
134 &bytes_identical, &bytes_sent))
135 goto error;
136 }
137
138 if (cmd > (char *) urb->transfer_buffer) {
139 /* Send partial buffer remaining before exiting */
140 int len;
141 if (cmd < (char *) urb->transfer_buffer + urb->transfer_buffer_length)
142 *cmd++ = 0xAF;
143 len = cmd - (char *) urb->transfer_buffer;
144 ret = udl_submit_urb(dev, urb, len);
145 bytes_sent += len;
146 } else
147 udl_urb_completion(urb);
148
149 error:
150 atomic_add(bytes_sent, &udl->bytes_sent);
151 atomic_add(bytes_identical, &udl->bytes_identical);
152 atomic_add(width*height*bpp, &udl->bytes_rendered);
153 end_cycles = get_cycles();
154 atomic_add(((unsigned int) ((end_cycles - start_cycles)
155 >> 10)), /* Kcycles */
156 &udl->cpu_kcycles_used);
157
158 return 0;
159 }
160
161 static int udl_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
162 {
163 unsigned long start = vma->vm_start;
164 unsigned long size = vma->vm_end - vma->vm_start;
165 unsigned long offset;
166 unsigned long page, pos;
167
168 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
169 return -EINVAL;
170
171 offset = vma->vm_pgoff << PAGE_SHIFT;
172
173 if (offset > info->fix.smem_len || size > info->fix.smem_len - offset)
174 return -EINVAL;
175
176 pos = (unsigned long)info->fix.smem_start + offset;
177
178 pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
179 pos, size);
180
181 /* We don't want the framebuffer to be mapped encrypted */
182 vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
183
184 while (size > 0) {
185 page = vmalloc_to_pfn((void *)pos);
186 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
187 return -EAGAIN;
188
189 start += PAGE_SIZE;
190 pos += PAGE_SIZE;
191 if (size > PAGE_SIZE)
192 size -= PAGE_SIZE;
193 else
194 size = 0;
195 }
196
197 /* VM_IO | VM_DONTEXPAND | VM_DONTDUMP are set by remap_pfn_range() */
198 return 0;
199 }
200
201 /*
202 * It's common for several clients to have framebuffer open simultaneously.
203 * e.g. both fbcon and X. Makes things interesting.
204 * Assumes caller is holding info->lock (for open and release at least)
205 */
206 static int udl_fb_open(struct fb_info *info, int user)
207 {
208 struct udl_fbdev *ufbdev = info->par;
209 struct drm_device *dev = ufbdev->ufb.base.dev;
210 struct udl_device *udl = dev->dev_private;
211
212 /* If the USB device is gone, we don't accept new opens */
213 if (drm_dev_is_unplugged(udl->ddev))
214 return -ENODEV;
215
216 ufbdev->fb_count++;
217
218 #ifdef CONFIG_DRM_FBDEV_EMULATION
219 if (fb_defio && (info->fbdefio == NULL)) {
220 /* enable defio at last moment if not disabled by client */
221
222 struct fb_deferred_io *fbdefio;
223
224 fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
225
226 if (fbdefio) {
227 fbdefio->delay = DL_DEFIO_WRITE_DELAY;
228 fbdefio->deferred_io = drm_fb_helper_deferred_io;
229 }
230
231 info->fbdefio = fbdefio;
232 fb_deferred_io_init(info);
233 }
234 #endif
235
236 pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
237 info->node, user, info, ufbdev->fb_count);
238
239 return 0;
240 }
241
242
243 /*
244 * Assumes caller is holding info->lock mutex (for open and release at least)
245 */
246 static int udl_fb_release(struct fb_info *info, int user)
247 {
248 struct udl_fbdev *ufbdev = info->par;
249
250 ufbdev->fb_count--;
251
252 #ifdef CONFIG_DRM_FBDEV_EMULATION
253 if ((ufbdev->fb_count == 0) && (info->fbdefio)) {
254 fb_deferred_io_cleanup(info);
255 kfree(info->fbdefio);
256 info->fbdefio = NULL;
257 info->fbops->fb_mmap = udl_fb_mmap;
258 }
259 #endif
260
261 pr_warn("released /dev/fb%d user=%d count=%d\n",
262 info->node, user, ufbdev->fb_count);
263
264 return 0;
265 }
266
267 static struct fb_ops udlfb_ops = {
268 .owner = THIS_MODULE,
269 DRM_FB_HELPER_DEFAULT_OPS,
270 .fb_fillrect = drm_fb_helper_sys_fillrect,
271 .fb_copyarea = drm_fb_helper_sys_copyarea,
272 .fb_imageblit = drm_fb_helper_sys_imageblit,
273 .fb_mmap = udl_fb_mmap,
274 .fb_open = udl_fb_open,
275 .fb_release = udl_fb_release,
276 };
277
278 static int udl_user_framebuffer_dirty(struct drm_framebuffer *fb,
279 struct drm_file *file,
280 unsigned flags, unsigned color,
281 struct drm_clip_rect *clips,
282 unsigned num_clips)
283 {
284 struct udl_framebuffer *ufb = to_udl_fb(fb);
285 int i;
286 int ret = 0;
287
288 drm_modeset_lock_all(fb->dev);
289
290 if (!ufb->active_16)
291 goto unlock;
292
293 if (ufb->obj->base.import_attach) {
294 ret = dma_buf_begin_cpu_access(ufb->obj->base.import_attach->dmabuf,
295 DMA_FROM_DEVICE);
296 if (ret)
297 goto unlock;
298 }
299
300 for (i = 0; i < num_clips; i++) {
301 ret = udl_handle_damage(ufb, clips[i].x1, clips[i].y1,
302 clips[i].x2 - clips[i].x1,
303 clips[i].y2 - clips[i].y1);
304 if (ret)
305 break;
306 }
307
308 if (ufb->obj->base.import_attach) {
309 ret = dma_buf_end_cpu_access(ufb->obj->base.import_attach->dmabuf,
310 DMA_FROM_DEVICE);
311 }
312
313 unlock:
314 drm_modeset_unlock_all(fb->dev);
315
316 return ret;
317 }
318
319 static void udl_user_framebuffer_destroy(struct drm_framebuffer *fb)
320 {
321 struct udl_framebuffer *ufb = to_udl_fb(fb);
322
323 if (ufb->obj)
324 drm_gem_object_put_unlocked(&ufb->obj->base);
325
326 drm_framebuffer_cleanup(fb);
327 kfree(ufb);
328 }
329
330 static const struct drm_framebuffer_funcs udlfb_funcs = {
331 .destroy = udl_user_framebuffer_destroy,
332 .dirty = udl_user_framebuffer_dirty,
333 };
334
335
336 static int
337 udl_framebuffer_init(struct drm_device *dev,
338 struct udl_framebuffer *ufb,
339 const struct drm_mode_fb_cmd2 *mode_cmd,
340 struct udl_gem_object *obj)
341 {
342 int ret;
343
344 ufb->obj = obj;
345 drm_helper_mode_fill_fb_struct(dev, &ufb->base, mode_cmd);
346 ret = drm_framebuffer_init(dev, &ufb->base, &udlfb_funcs);
347 return ret;
348 }
349
350
351 static int udlfb_create(struct drm_fb_helper *helper,
352 struct drm_fb_helper_surface_size *sizes)
353 {
354 struct udl_fbdev *ufbdev =
355 container_of(helper, struct udl_fbdev, helper);
356 struct drm_device *dev = ufbdev->helper.dev;
357 struct fb_info *info;
358 struct drm_framebuffer *fb;
359 struct drm_mode_fb_cmd2 mode_cmd;
360 struct udl_gem_object *obj;
361 uint32_t size;
362 int ret = 0;
363
364 if (sizes->surface_bpp == 24)
365 sizes->surface_bpp = 32;
366
367 mode_cmd.width = sizes->surface_width;
368 mode_cmd.height = sizes->surface_height;
369 mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
370
371 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
372 sizes->surface_depth);
373
374 size = mode_cmd.pitches[0] * mode_cmd.height;
375 size = ALIGN(size, PAGE_SIZE);
376
377 obj = udl_gem_alloc_object(dev, size);
378 if (!obj)
379 goto out;
380
381 ret = udl_gem_vmap(obj);
382 if (ret) {
383 DRM_ERROR("failed to vmap fb\n");
384 goto out_gfree;
385 }
386
387 info = drm_fb_helper_alloc_fbi(helper);
388 if (IS_ERR(info)) {
389 ret = PTR_ERR(info);
390 goto out_gfree;
391 }
392 info->par = ufbdev;
393
394 ret = udl_framebuffer_init(dev, &ufbdev->ufb, &mode_cmd, obj);
395 if (ret)
396 goto out_gfree;
397
398 fb = &ufbdev->ufb.base;
399
400 ufbdev->helper.fb = fb;
401
402 strcpy(info->fix.id, "udldrmfb");
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_fix(info, fb->pitches[0], fb->format->depth);
410 drm_fb_helper_fill_var(info, &ufbdev->helper, sizes->fb_width, sizes->fb_height);
411
412 DRM_DEBUG_KMS("allocated %dx%d vmal %p\n",
413 fb->width, fb->height,
414 ufbdev->ufb.obj->vmapping);
415
416 return ret;
417 out_gfree:
418 drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
419 out:
420 return ret;
421 }
422
423 static const struct drm_fb_helper_funcs udl_fb_helper_funcs = {
424 .fb_probe = udlfb_create,
425 };
426
427 static void udl_fbdev_destroy(struct drm_device *dev,
428 struct udl_fbdev *ufbdev)
429 {
430 drm_fb_helper_unregister_fbi(&ufbdev->helper);
431 drm_fb_helper_fini(&ufbdev->helper);
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 int udl_fbdev_init(struct drm_device *dev)
438 {
439 struct udl_device *udl = dev->dev_private;
440 int bpp_sel = fb_bpp;
441 struct udl_fbdev *ufbdev;
442 int ret;
443
444 ufbdev = kzalloc(sizeof(struct udl_fbdev), GFP_KERNEL);
445 if (!ufbdev)
446 return -ENOMEM;
447
448 udl->fbdev = ufbdev;
449
450 drm_fb_helper_prepare(dev, &ufbdev->helper, &udl_fb_helper_funcs);
451
452 ret = drm_fb_helper_init(dev, &ufbdev->helper, 1);
453 if (ret)
454 goto free;
455
456 ret = drm_fb_helper_single_add_all_connectors(&ufbdev->helper);
457 if (ret)
458 goto fini;
459
460 /* disable all the possible outputs/crtcs before entering KMS mode */
461 drm_helper_disable_unused_functions(dev);
462
463 ret = drm_fb_helper_initial_config(&ufbdev->helper, bpp_sel);
464 if (ret)
465 goto fini;
466
467 return 0;
468
469 fini:
470 drm_fb_helper_fini(&ufbdev->helper);
471 free:
472 kfree(ufbdev);
473 return ret;
474 }
475
476 void udl_fbdev_cleanup(struct drm_device *dev)
477 {
478 struct udl_device *udl = dev->dev_private;
479 if (!udl->fbdev)
480 return;
481
482 udl_fbdev_destroy(dev, udl->fbdev);
483 kfree(udl->fbdev);
484 udl->fbdev = NULL;
485 }
486
487 void udl_fbdev_unplug(struct drm_device *dev)
488 {
489 struct udl_device *udl = dev->dev_private;
490 struct udl_fbdev *ufbdev;
491 if (!udl->fbdev)
492 return;
493
494 ufbdev = udl->fbdev;
495 drm_fb_helper_unlink_fbi(&ufbdev->helper);
496 }
497
498 struct drm_framebuffer *
499 udl_fb_user_fb_create(struct drm_device *dev,
500 struct drm_file *file,
501 const struct drm_mode_fb_cmd2 *mode_cmd)
502 {
503 struct drm_gem_object *obj;
504 struct udl_framebuffer *ufb;
505 int ret;
506 uint32_t size;
507
508 obj = drm_gem_object_lookup(file, mode_cmd->handles[0]);
509 if (obj == NULL)
510 return ERR_PTR(-ENOENT);
511
512 size = mode_cmd->pitches[0] * mode_cmd->height;
513 size = ALIGN(size, PAGE_SIZE);
514
515 if (size > obj->size) {
516 DRM_ERROR("object size not sufficient for fb %d %zu %d %d\n", size, obj->size, mode_cmd->pitches[0], mode_cmd->height);
517 return ERR_PTR(-ENOMEM);
518 }
519
520 ufb = kzalloc(sizeof(*ufb), GFP_KERNEL);
521 if (ufb == NULL)
522 return ERR_PTR(-ENOMEM);
523
524 ret = udl_framebuffer_init(dev, ufb, mode_cmd, to_udl_bo(obj));
525 if (ret) {
526 kfree(ufb);
527 return ERR_PTR(-EINVAL);
528 }
529 return &ufb->base;
530 }