1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Red Hat
5 * based in parts on udlfb.c:
10 #include <linux/module.h>
11 #include <linux/slab.h>
13 #include <linux/dma-buf.h>
14 #include <linux/mem_encrypt.h>
17 #include <drm/drm_crtc.h>
18 #include <drm/drm_crtc_helper.h>
21 #include <drm/drm_fb_helper.h>
23 #define DL_DEFIO_WRITE_DELAY (HZ/20) /* fb_deferred_io.delay in jiffies */
25 static int fb_defio = 0; /* Optionally enable experimental fb_defio mmap support */
26 static int fb_bpp = 16;
28 module_param(fb_bpp, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
29 module_param(fb_defio, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
32 struct drm_fb_helper helper; /* must be first */
33 struct udl_framebuffer ufb;
37 #define DL_ALIGN_UP(x, a) ALIGN(x, a)
38 #define DL_ALIGN_DOWN(x, a) ALIGN_DOWN(x, a)
40 /** Read the red component (0..255) of a 32 bpp colour. */
41 #define DLO_RGB_GETRED(col) (uint8_t)((col) & 0xFF)
43 /** Read the green component (0..255) of a 32 bpp colour. */
44 #define DLO_RGB_GETGRN(col) (uint8_t)(((col) >> 8) & 0xFF)
46 /** Read the blue component (0..255) of a 32 bpp colour. */
47 #define DLO_RGB_GETBLU(col) (uint8_t)(((col) >> 16) & 0xFF)
49 /** Return red/green component of a 16 bpp colour number. */
50 #define DLO_RG16(red, grn) (uint8_t)((((red) & 0xF8) | ((grn) >> 5)) & 0xFF)
52 /** Return green/blue component of a 16 bpp colour number. */
53 #define DLO_GB16(grn, blu) (uint8_t)(((((grn) & 0x1C) << 3) | ((blu) >> 3)) & 0xFF)
55 /** Return 8 bpp colour number from red, green and blue components. */
56 #define DLO_RGB8(red, grn, blu) ((((red) << 5) | (((grn) & 3) << 3) | ((blu) & 7)) & 0xFF)
59 static uint8_t rgb8(uint32_t col)
61 uint8_t red = DLO_RGB_GETRED(col);
62 uint8_t grn = DLO_RGB_GETGRN(col);
63 uint8_t blu = DLO_RGB_GETBLU(col);
65 return DLO_RGB8(red, grn, blu);
68 static uint16_t rgb16(uint32_t col)
70 uint8_t red = DLO_RGB_GETRED(col);
71 uint8_t grn = DLO_RGB_GETGRN(col);
72 uint8_t blu = DLO_RGB_GETBLU(col);
74 return (DLO_RG16(red, grn) << 8) + DLO_GB16(grn, blu);
78 int udl_handle_damage(struct udl_framebuffer *fb, int x, int y,
79 int width, int height)
81 struct drm_device *dev = fb->base.dev;
82 struct udl_device *udl = to_udl(dev);
85 cycles_t start_cycles, end_cycles;
87 int bytes_identical = 0;
92 BUG_ON(!is_power_of_2(fb->base.format->cpp[0]));
93 log_bpp = __ffs(fb->base.format->cpp[0]);
98 if (!fb->obj->vmapping) {
99 ret = udl_gem_vmap(fb->obj);
100 if (ret == -ENOMEM) {
101 DRM_ERROR("failed to vmap fb\n");
104 if (!fb->obj->vmapping) {
105 DRM_ERROR("failed to vmapping\n");
110 aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
111 width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
115 (x + width > fb->base.width) ||
116 (y + height > fb->base.height))
119 start_cycles = get_cycles();
121 urb = udl_get_urb(dev);
124 cmd = urb->transfer_buffer;
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 << log_bpp);
129 const int dev_byte_offset = (fb->base.width * i + x) << log_bpp;
130 if (udl_render_hline(dev, log_bpp, &urb,
131 (char *) fb->obj->vmapping,
132 &cmd, byte_offset, dev_byte_offset,
134 &bytes_identical, &bytes_sent))
138 if (cmd > (char *) urb->transfer_buffer) {
139 /* Send partial buffer remaining before exiting */
141 if (cmd < (char *) urb->transfer_buffer + urb->transfer_buffer_length)
143 len = cmd - (char *) urb->transfer_buffer;
144 ret = udl_submit_urb(dev, urb, len);
147 udl_urb_completion(urb);
150 atomic_add(bytes_sent, &udl->bytes_sent);
151 atomic_add(bytes_identical, &udl->bytes_identical);
152 atomic_add((width * height) << log_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);
161 static int udl_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
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;
168 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
171 offset = vma->vm_pgoff << PAGE_SHIFT;
173 if (offset > info->fix.smem_len || size > info->fix.smem_len - offset)
176 pos = (unsigned long)info->fix.smem_start + offset;
178 pr_debug("mmap() framebuffer addr:%lu size:%lu\n",
181 /* We don't want the framebuffer to be mapped encrypted */
182 vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
185 page = vmalloc_to_pfn((void *)pos);
186 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
191 if (size > PAGE_SIZE)
197 /* VM_IO | VM_DONTEXPAND | VM_DONTDUMP are set by remap_pfn_range() */
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)
206 static int udl_fb_open(struct fb_info *info, int user)
208 struct udl_fbdev *ufbdev = info->par;
209 struct drm_device *dev = ufbdev->ufb.base.dev;
210 struct udl_device *udl = to_udl(dev);
212 /* If the USB device is gone, we don't accept new opens */
213 if (drm_dev_is_unplugged(&udl->drm))
218 #ifdef CONFIG_DRM_FBDEV_EMULATION
219 if (fb_defio && (info->fbdefio == NULL)) {
220 /* enable defio at last moment if not disabled by client */
222 struct fb_deferred_io *fbdefio;
224 fbdefio = kzalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
227 fbdefio->delay = DL_DEFIO_WRITE_DELAY;
228 fbdefio->deferred_io = drm_fb_helper_deferred_io;
231 info->fbdefio = fbdefio;
232 fb_deferred_io_init(info);
236 pr_debug("open /dev/fb%d user=%d fb_info=%p count=%d\n",
237 info->node, user, info, ufbdev->fb_count);
244 * Assumes caller is holding info->lock mutex (for open and release at least)
246 static int udl_fb_release(struct fb_info *info, int user)
248 struct udl_fbdev *ufbdev = info->par;
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;
261 pr_debug("released /dev/fb%d user=%d count=%d\n",
262 info->node, user, ufbdev->fb_count);
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,
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,
284 struct udl_framebuffer *ufb = to_udl_fb(fb);
288 drm_modeset_lock_all(fb->dev);
293 if (ufb->obj->base.import_attach) {
294 ret = dma_buf_begin_cpu_access(ufb->obj->base.import_attach->dmabuf,
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);
308 if (ufb->obj->base.import_attach) {
309 ret = dma_buf_end_cpu_access(ufb->obj->base.import_attach->dmabuf,
314 drm_modeset_unlock_all(fb->dev);
319 static void udl_user_framebuffer_destroy(struct drm_framebuffer *fb)
321 struct udl_framebuffer *ufb = to_udl_fb(fb);
324 drm_gem_object_put_unlocked(&ufb->obj->base);
326 drm_framebuffer_cleanup(fb);
330 static const struct drm_framebuffer_funcs udlfb_funcs = {
331 .destroy = udl_user_framebuffer_destroy,
332 .dirty = udl_user_framebuffer_dirty,
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)
345 drm_helper_mode_fill_fb_struct(dev, &ufb->base, mode_cmd);
346 ret = drm_framebuffer_init(dev, &ufb->base, &udlfb_funcs);
351 static int udlfb_create(struct drm_fb_helper *helper,
352 struct drm_fb_helper_surface_size *sizes)
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;
364 if (sizes->surface_bpp == 24)
365 sizes->surface_bpp = 32;
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);
371 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
372 sizes->surface_depth);
374 size = mode_cmd.pitches[0] * mode_cmd.height;
375 size = ALIGN(size, PAGE_SIZE);
377 obj = udl_gem_alloc_object(dev, size);
381 ret = udl_gem_vmap(obj);
383 DRM_ERROR("failed to vmap fb\n");
387 info = drm_fb_helper_alloc_fbi(helper);
393 ret = udl_framebuffer_init(dev, &ufbdev->ufb, &mode_cmd, obj);
397 fb = &ufbdev->ufb.base;
399 ufbdev->helper.fb = fb;
401 info->screen_base = ufbdev->ufb.obj->vmapping;
402 info->fix.smem_len = size;
403 info->fix.smem_start = (unsigned long)ufbdev->ufb.obj->vmapping;
405 info->fbops = &udlfb_ops;
406 drm_fb_helper_fill_info(info, &ufbdev->helper, sizes);
408 DRM_DEBUG_KMS("allocated %dx%d vmal %p\n",
409 fb->width, fb->height,
410 ufbdev->ufb.obj->vmapping);
414 drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
419 static const struct drm_fb_helper_funcs udl_fb_helper_funcs = {
420 .fb_probe = udlfb_create,
423 static void udl_fbdev_destroy(struct drm_device *dev,
424 struct udl_fbdev *ufbdev)
426 drm_fb_helper_unregister_fbi(&ufbdev->helper);
427 drm_fb_helper_fini(&ufbdev->helper);
428 if (ufbdev->ufb.obj) {
429 drm_framebuffer_unregister_private(&ufbdev->ufb.base);
430 drm_framebuffer_cleanup(&ufbdev->ufb.base);
431 drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
435 int udl_fbdev_init(struct drm_device *dev)
437 struct udl_device *udl = to_udl(dev);
438 int bpp_sel = fb_bpp;
439 struct udl_fbdev *ufbdev;
442 ufbdev = kzalloc(sizeof(struct udl_fbdev), GFP_KERNEL);
448 drm_fb_helper_prepare(dev, &ufbdev->helper, &udl_fb_helper_funcs);
450 ret = drm_fb_helper_init(dev, &ufbdev->helper, 1);
454 ret = drm_fb_helper_single_add_all_connectors(&ufbdev->helper);
458 /* disable all the possible outputs/crtcs before entering KMS mode */
459 drm_helper_disable_unused_functions(dev);
461 ret = drm_fb_helper_initial_config(&ufbdev->helper, bpp_sel);
468 drm_fb_helper_fini(&ufbdev->helper);
474 void udl_fbdev_cleanup(struct drm_device *dev)
476 struct udl_device *udl = to_udl(dev);
480 udl_fbdev_destroy(dev, udl->fbdev);
485 void udl_fbdev_unplug(struct drm_device *dev)
487 struct udl_device *udl = to_udl(dev);
488 struct udl_fbdev *ufbdev;
493 drm_fb_helper_unlink_fbi(&ufbdev->helper);
496 struct drm_framebuffer *
497 udl_fb_user_fb_create(struct drm_device *dev,
498 struct drm_file *file,
499 const struct drm_mode_fb_cmd2 *mode_cmd)
501 struct drm_gem_object *obj;
502 struct udl_framebuffer *ufb;
506 obj = drm_gem_object_lookup(file, mode_cmd->handles[0]);
508 return ERR_PTR(-ENOENT);
510 size = mode_cmd->pitches[0] * mode_cmd->height;
511 size = ALIGN(size, PAGE_SIZE);
513 if (size > obj->size) {
514 DRM_ERROR("object size not sufficient for fb %d %zu %d %d\n", size, obj->size, mode_cmd->pitches[0], mode_cmd->height);
515 return ERR_PTR(-ENOMEM);
518 ufb = kzalloc(sizeof(*ufb), GFP_KERNEL);
520 return ERR_PTR(-ENOMEM);
522 ret = udl_framebuffer_init(dev, ufb, mode_cmd, to_udl_bo(obj));
525 return ERR_PTR(-EINVAL);