Thomas Gleixner | 12eb90f | 2019-05-29 07:12:36 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Red Hat |
| 4 | * |
| 5 | * based in parts on udlfb.c: |
| 6 | * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it> |
| 7 | * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com> |
| 8 | * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com> |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 9 | */ |
Sam Ravnborg | a9dcf38 | 2019-07-16 08:42:09 +0200 | [diff] [blame] | 10 | |
| 11 | #include <linux/moduleparam.h> |
Dave Airlie | 32ecd24 | 2012-07-30 14:06:55 +1000 | [diff] [blame] | 12 | #include <linux/dma-buf.h> |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 13 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 14 | #include <drm/drm_crtc_helper.h> |
Sam Ravnborg | a9dcf38 | 2019-07-16 08:42:09 +0200 | [diff] [blame] | 15 | #include <drm/drm_drv.h> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 16 | #include <drm/drm_fb_helper.h> |
Sam Ravnborg | a9dcf38 | 2019-07-16 08:42:09 +0200 | [diff] [blame] | 17 | #include <drm/drm_fourcc.h> |
| 18 | #include <drm/drm_modeset_helper.h> |
| 19 | |
| 20 | #include "udl_drv.h" |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 21 | |
Dave Airlie | 677d23b | 2013-02-07 12:30:25 +1000 | [diff] [blame] | 22 | #define DL_DEFIO_WRITE_DELAY (HZ/20) /* fb_deferred_io.delay in jiffies */ |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 23 | |
Dave Airlie | 677d23b | 2013-02-07 12:30:25 +1000 | [diff] [blame] | 24 | static int fb_defio = 0; /* Optionally enable experimental fb_defio mmap support */ |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 25 | static int fb_bpp = 16; |
| 26 | |
| 27 | module_param(fb_bpp, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP); |
| 28 | module_param(fb_defio, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP); |
| 29 | |
| 30 | struct udl_fbdev { |
Daniel Vetter | 5a3887b | 2019-03-26 14:20:06 +0100 | [diff] [blame] | 31 | struct drm_fb_helper helper; /* must be first */ |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 32 | struct udl_framebuffer ufb; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 33 | int fb_count; |
| 34 | }; |
| 35 | |
| 36 | #define DL_ALIGN_UP(x, a) ALIGN(x, a) |
Krzysztof Kozlowski | ed067d4 | 2017-04-11 20:08:34 +0200 | [diff] [blame] | 37 | #define DL_ALIGN_DOWN(x, a) ALIGN_DOWN(x, a) |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 38 | |
| 39 | /** Read the red component (0..255) of a 32 bpp colour. */ |
| 40 | #define DLO_RGB_GETRED(col) (uint8_t)((col) & 0xFF) |
| 41 | |
| 42 | /** Read the green component (0..255) of a 32 bpp colour. */ |
| 43 | #define DLO_RGB_GETGRN(col) (uint8_t)(((col) >> 8) & 0xFF) |
| 44 | |
| 45 | /** Read the blue component (0..255) of a 32 bpp colour. */ |
| 46 | #define DLO_RGB_GETBLU(col) (uint8_t)(((col) >> 16) & 0xFF) |
| 47 | |
| 48 | /** Return red/green component of a 16 bpp colour number. */ |
| 49 | #define DLO_RG16(red, grn) (uint8_t)((((red) & 0xF8) | ((grn) >> 5)) & 0xFF) |
| 50 | |
| 51 | /** Return green/blue component of a 16 bpp colour number. */ |
| 52 | #define DLO_GB16(grn, blu) (uint8_t)(((((grn) & 0x1C) << 3) | ((blu) >> 3)) & 0xFF) |
| 53 | |
| 54 | /** Return 8 bpp colour number from red, green and blue components. */ |
| 55 | #define DLO_RGB8(red, grn, blu) ((((red) << 5) | (((grn) & 3) << 3) | ((blu) & 7)) & 0xFF) |
| 56 | |
| 57 | #if 0 |
| 58 | static uint8_t rgb8(uint32_t col) |
| 59 | { |
| 60 | uint8_t red = DLO_RGB_GETRED(col); |
| 61 | uint8_t grn = DLO_RGB_GETGRN(col); |
| 62 | uint8_t blu = DLO_RGB_GETBLU(col); |
| 63 | |
| 64 | return DLO_RGB8(red, grn, blu); |
| 65 | } |
| 66 | |
| 67 | static uint16_t rgb16(uint32_t col) |
| 68 | { |
| 69 | uint8_t red = DLO_RGB_GETRED(col); |
| 70 | uint8_t grn = DLO_RGB_GETGRN(col); |
| 71 | uint8_t blu = DLO_RGB_GETBLU(col); |
| 72 | |
| 73 | return (DLO_RG16(red, grn) << 8) + DLO_GB16(grn, blu); |
| 74 | } |
| 75 | #endif |
| 76 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 77 | int udl_handle_damage(struct udl_framebuffer *fb, int x, int y, |
| 78 | int width, int height) |
| 79 | { |
| 80 | struct drm_device *dev = fb->base.dev; |
Dave Airlie | fd96e0d | 2019-04-05 13:17:14 +1000 | [diff] [blame] | 81 | struct udl_device *udl = to_udl(dev); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 82 | int i, ret; |
| 83 | char *cmd; |
| 84 | cycles_t start_cycles, end_cycles; |
| 85 | int bytes_sent = 0; |
| 86 | int bytes_identical = 0; |
| 87 | struct urb *urb; |
| 88 | int aligned_x; |
Mikulas Patocka | 91ba11f | 2018-06-03 16:41:00 +0200 | [diff] [blame] | 89 | int log_bpp; |
| 90 | |
| 91 | BUG_ON(!is_power_of_2(fb->base.format->cpp[0])); |
| 92 | log_bpp = __ffs(fb->base.format->cpp[0]); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 93 | |
| 94 | if (!fb->active_16) |
| 95 | return 0; |
| 96 | |
Dave Airlie | e8aa1d1 | 2012-03-26 14:36:56 +0100 | [diff] [blame] | 97 | if (!fb->obj->vmapping) { |
| 98 | ret = udl_gem_vmap(fb->obj); |
| 99 | if (ret == -ENOMEM) { |
| 100 | DRM_ERROR("failed to vmap fb\n"); |
| 101 | return 0; |
| 102 | } |
| 103 | if (!fb->obj->vmapping) { |
| 104 | DRM_ERROR("failed to vmapping\n"); |
| 105 | return 0; |
| 106 | } |
| 107 | } |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 108 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 109 | aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long)); |
| 110 | width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long)); |
| 111 | x = aligned_x; |
| 112 | |
| 113 | if ((width <= 0) || |
| 114 | (x + width > fb->base.width) || |
| 115 | (y + height > fb->base.height)) |
| 116 | return -EINVAL; |
| 117 | |
Dave Airlie | bcb39af | 2013-02-07 11:19:15 +1000 | [diff] [blame] | 118 | start_cycles = get_cycles(); |
| 119 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 120 | urb = udl_get_urb(dev); |
| 121 | if (!urb) |
| 122 | return 0; |
| 123 | cmd = urb->transfer_buffer; |
| 124 | |
David Herrmann | 90fd68d | 2016-09-23 12:36:02 +0200 | [diff] [blame] | 125 | for (i = y; i < y + height ; i++) { |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 126 | const int line_offset = fb->base.pitches[0] * i; |
Mikulas Patocka | 91ba11f | 2018-06-03 16:41:00 +0200 | [diff] [blame] | 127 | const int byte_offset = line_offset + (x << log_bpp); |
| 128 | const int dev_byte_offset = (fb->base.width * i + x) << log_bpp; |
| 129 | if (udl_render_hline(dev, log_bpp, &urb, |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 130 | (char *) fb->obj->vmapping, |
Dave Airlie | 3916e1d | 2012-11-01 13:47:09 +1000 | [diff] [blame] | 131 | &cmd, byte_offset, dev_byte_offset, |
Mikulas Patocka | 91ba11f | 2018-06-03 16:41:00 +0200 | [diff] [blame] | 132 | width << log_bpp, |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 133 | &bytes_identical, &bytes_sent)) |
| 134 | goto error; |
| 135 | } |
| 136 | |
| 137 | if (cmd > (char *) urb->transfer_buffer) { |
| 138 | /* Send partial buffer remaining before exiting */ |
Mikulas Patocka | 99ec9e7 | 2018-06-03 16:40:54 +0200 | [diff] [blame] | 139 | int len; |
| 140 | if (cmd < (char *) urb->transfer_buffer + urb->transfer_buffer_length) |
| 141 | *cmd++ = 0xAF; |
| 142 | len = cmd - (char *) urb->transfer_buffer; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 143 | ret = udl_submit_urb(dev, urb, len); |
| 144 | bytes_sent += len; |
| 145 | } else |
| 146 | udl_urb_completion(urb); |
| 147 | |
| 148 | error: |
| 149 | atomic_add(bytes_sent, &udl->bytes_sent); |
| 150 | atomic_add(bytes_identical, &udl->bytes_identical); |
Mikulas Patocka | 91ba11f | 2018-06-03 16:41:00 +0200 | [diff] [blame] | 151 | atomic_add((width * height) << log_bpp, &udl->bytes_rendered); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 152 | end_cycles = get_cycles(); |
| 153 | atomic_add(((unsigned int) ((end_cycles - start_cycles) |
| 154 | >> 10)), /* Kcycles */ |
| 155 | &udl->cpu_kcycles_used); |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static int udl_fb_mmap(struct fb_info *info, struct vm_area_struct *vma) |
| 161 | { |
| 162 | unsigned long start = vma->vm_start; |
| 163 | unsigned long size = vma->vm_end - vma->vm_start; |
Greg Kroah-Hartman | 3b82a4d | 2018-03-21 16:45:53 +0100 | [diff] [blame] | 164 | unsigned long offset; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 165 | unsigned long page, pos; |
| 166 | |
Greg Kroah-Hartman | 3b82a4d | 2018-03-21 16:45:53 +0100 | [diff] [blame] | 167 | if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT)) |
| 168 | return -EINVAL; |
| 169 | |
| 170 | offset = vma->vm_pgoff << PAGE_SHIFT; |
| 171 | |
| 172 | if (offset > info->fix.smem_len || size > info->fix.smem_len - offset) |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 173 | return -EINVAL; |
| 174 | |
| 175 | pos = (unsigned long)info->fix.smem_start + offset; |
| 176 | |
Mikulas Patocka | 9099120 | 2018-06-03 16:41:03 +0200 | [diff] [blame] | 177 | pr_debug("mmap() framebuffer addr:%lu size:%lu\n", |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 178 | pos, size); |
| 179 | |
Tom Lendacky | 95cf926 | 2017-07-17 16:10:26 -0500 | [diff] [blame] | 180 | /* We don't want the framebuffer to be mapped encrypted */ |
| 181 | vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot); |
| 182 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 183 | while (size > 0) { |
| 184 | page = vmalloc_to_pfn((void *)pos); |
| 185 | if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) |
| 186 | return -EAGAIN; |
| 187 | |
| 188 | start += PAGE_SIZE; |
| 189 | pos += PAGE_SIZE; |
| 190 | if (size > PAGE_SIZE) |
| 191 | size -= PAGE_SIZE; |
| 192 | else |
| 193 | size = 0; |
| 194 | } |
| 195 | |
Konstantin Khlebnikov | 314e51b | 2012-10-08 16:29:02 -0700 | [diff] [blame] | 196 | /* VM_IO | VM_DONTEXPAND | VM_DONTDUMP are set by remap_pfn_range() */ |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 197 | return 0; |
| 198 | } |
| 199 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 200 | /* |
| 201 | * It's common for several clients to have framebuffer open simultaneously. |
| 202 | * e.g. both fbcon and X. Makes things interesting. |
| 203 | * Assumes caller is holding info->lock (for open and release at least) |
| 204 | */ |
| 205 | static int udl_fb_open(struct fb_info *info, int user) |
| 206 | { |
| 207 | struct udl_fbdev *ufbdev = info->par; |
| 208 | struct drm_device *dev = ufbdev->ufb.base.dev; |
Dave Airlie | fd96e0d | 2019-04-05 13:17:14 +1000 | [diff] [blame] | 209 | struct udl_device *udl = to_udl(dev); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 210 | |
| 211 | /* If the USB device is gone, we don't accept new opens */ |
Dave Airlie | 6ecac85 | 2019-04-05 13:17:15 +1000 | [diff] [blame] | 212 | if (drm_dev_is_unplugged(&udl->drm)) |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 213 | return -ENODEV; |
| 214 | |
| 215 | ufbdev->fb_count++; |
| 216 | |
Daniel Vetter | 2b721f2 | 2016-08-10 18:52:38 +0200 | [diff] [blame] | 217 | #ifdef CONFIG_DRM_FBDEV_EMULATION |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 218 | if (fb_defio && (info->fbdefio == NULL)) { |
| 219 | /* enable defio at last moment if not disabled by client */ |
| 220 | |
| 221 | struct fb_deferred_io *fbdefio; |
| 222 | |
Mikulas Patocka | 09a00ab | 2018-06-03 16:40:57 +0200 | [diff] [blame] | 223 | fbdefio = kzalloc(sizeof(struct fb_deferred_io), GFP_KERNEL); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 224 | |
| 225 | if (fbdefio) { |
| 226 | fbdefio->delay = DL_DEFIO_WRITE_DELAY; |
Noralf Trønnes | e375882 | 2016-04-28 17:18:37 +0200 | [diff] [blame] | 227 | fbdefio->deferred_io = drm_fb_helper_deferred_io; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | info->fbdefio = fbdefio; |
| 231 | fb_deferred_io_init(info); |
| 232 | } |
Daniel Vetter | 2b721f2 | 2016-08-10 18:52:38 +0200 | [diff] [blame] | 233 | #endif |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 234 | |
Mikulas Patocka | 9099120 | 2018-06-03 16:41:03 +0200 | [diff] [blame] | 235 | pr_debug("open /dev/fb%d user=%d fb_info=%p count=%d\n", |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 236 | info->node, user, info, ufbdev->fb_count); |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | /* |
| 243 | * Assumes caller is holding info->lock mutex (for open and release at least) |
| 244 | */ |
| 245 | static int udl_fb_release(struct fb_info *info, int user) |
| 246 | { |
| 247 | struct udl_fbdev *ufbdev = info->par; |
| 248 | |
| 249 | ufbdev->fb_count--; |
| 250 | |
Daniel Vetter | 2b721f2 | 2016-08-10 18:52:38 +0200 | [diff] [blame] | 251 | #ifdef CONFIG_DRM_FBDEV_EMULATION |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 252 | if ((ufbdev->fb_count == 0) && (info->fbdefio)) { |
| 253 | fb_deferred_io_cleanup(info); |
| 254 | kfree(info->fbdefio); |
| 255 | info->fbdefio = NULL; |
| 256 | info->fbops->fb_mmap = udl_fb_mmap; |
| 257 | } |
Daniel Vetter | 2b721f2 | 2016-08-10 18:52:38 +0200 | [diff] [blame] | 258 | #endif |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 259 | |
Mikulas Patocka | 9099120 | 2018-06-03 16:41:03 +0200 | [diff] [blame] | 260 | pr_debug("released /dev/fb%d user=%d count=%d\n", |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 261 | info->node, user, ufbdev->fb_count); |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | static struct fb_ops udlfb_ops = { |
| 267 | .owner = THIS_MODULE, |
Stefan Christ | 812162f | 2016-11-14 00:03:23 +0100 | [diff] [blame] | 268 | DRM_FB_HELPER_DEFAULT_OPS, |
Noralf Trønnes | e375882 | 2016-04-28 17:18:37 +0200 | [diff] [blame] | 269 | .fb_fillrect = drm_fb_helper_sys_fillrect, |
| 270 | .fb_copyarea = drm_fb_helper_sys_copyarea, |
| 271 | .fb_imageblit = drm_fb_helper_sys_imageblit, |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 272 | .fb_mmap = udl_fb_mmap, |
| 273 | .fb_open = udl_fb_open, |
| 274 | .fb_release = udl_fb_release, |
| 275 | }; |
| 276 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 277 | static int udl_user_framebuffer_dirty(struct drm_framebuffer *fb, |
| 278 | struct drm_file *file, |
| 279 | unsigned flags, unsigned color, |
| 280 | struct drm_clip_rect *clips, |
| 281 | unsigned num_clips) |
| 282 | { |
| 283 | struct udl_framebuffer *ufb = to_udl_fb(fb); |
| 284 | int i; |
Dave Airlie | 32ecd24 | 2012-07-30 14:06:55 +1000 | [diff] [blame] | 285 | int ret = 0; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 286 | |
Ville Syrjälä | 73e9efd | 2013-12-04 14:13:58 +0200 | [diff] [blame] | 287 | drm_modeset_lock_all(fb->dev); |
| 288 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 289 | if (!ufb->active_16) |
Ville Syrjälä | 73e9efd | 2013-12-04 14:13:58 +0200 | [diff] [blame] | 290 | goto unlock; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 291 | |
Dave Airlie | 32ecd24 | 2012-07-30 14:06:55 +1000 | [diff] [blame] | 292 | if (ufb->obj->base.import_attach) { |
| 293 | ret = dma_buf_begin_cpu_access(ufb->obj->base.import_attach->dmabuf, |
Dave Airlie | 32ecd24 | 2012-07-30 14:06:55 +1000 | [diff] [blame] | 294 | DMA_FROM_DEVICE); |
| 295 | if (ret) |
Ville Syrjälä | 73e9efd | 2013-12-04 14:13:58 +0200 | [diff] [blame] | 296 | goto unlock; |
Dave Airlie | 32ecd24 | 2012-07-30 14:06:55 +1000 | [diff] [blame] | 297 | } |
| 298 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 299 | for (i = 0; i < num_clips; i++) { |
Dave Airlie | 32ecd24 | 2012-07-30 14:06:55 +1000 | [diff] [blame] | 300 | ret = udl_handle_damage(ufb, clips[i].x1, clips[i].y1, |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 301 | clips[i].x2 - clips[i].x1, |
| 302 | clips[i].y2 - clips[i].y1); |
Dave Airlie | 32ecd24 | 2012-07-30 14:06:55 +1000 | [diff] [blame] | 303 | if (ret) |
David Herrmann | 06c99161 | 2014-01-20 19:52:29 +0100 | [diff] [blame] | 304 | break; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 305 | } |
Dave Airlie | 32ecd24 | 2012-07-30 14:06:55 +1000 | [diff] [blame] | 306 | |
| 307 | if (ufb->obj->base.import_attach) { |
Chris Wilson | 18b862d | 2016-03-18 20:02:39 +0000 | [diff] [blame] | 308 | ret = dma_buf_end_cpu_access(ufb->obj->base.import_attach->dmabuf, |
| 309 | DMA_FROM_DEVICE); |
Dave Airlie | 32ecd24 | 2012-07-30 14:06:55 +1000 | [diff] [blame] | 310 | } |
Ville Syrjälä | 73e9efd | 2013-12-04 14:13:58 +0200 | [diff] [blame] | 311 | |
| 312 | unlock: |
| 313 | drm_modeset_unlock_all(fb->dev); |
| 314 | |
Dave Airlie | 32ecd24 | 2012-07-30 14:06:55 +1000 | [diff] [blame] | 315 | return ret; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static void udl_user_framebuffer_destroy(struct drm_framebuffer *fb) |
| 319 | { |
| 320 | struct udl_framebuffer *ufb = to_udl_fb(fb); |
| 321 | |
| 322 | if (ufb->obj) |
Cihangir Akturk | 823ee8b | 2017-08-11 15:33:09 +0300 | [diff] [blame] | 323 | drm_gem_object_put_unlocked(&ufb->obj->base); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 324 | |
| 325 | drm_framebuffer_cleanup(fb); |
| 326 | kfree(ufb); |
| 327 | } |
| 328 | |
| 329 | static const struct drm_framebuffer_funcs udlfb_funcs = { |
| 330 | .destroy = udl_user_framebuffer_destroy, |
| 331 | .dirty = udl_user_framebuffer_dirty, |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 332 | }; |
| 333 | |
| 334 | |
| 335 | static int |
| 336 | udl_framebuffer_init(struct drm_device *dev, |
| 337 | struct udl_framebuffer *ufb, |
Ville Syrjälä | 1eb8345 | 2015-11-11 19:11:29 +0200 | [diff] [blame] | 338 | const struct drm_mode_fb_cmd2 *mode_cmd, |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 339 | struct udl_gem_object *obj) |
| 340 | { |
| 341 | int ret; |
| 342 | |
| 343 | ufb->obj = obj; |
Ville Syrjälä | a3f913c | 2016-12-14 22:48:59 +0200 | [diff] [blame] | 344 | drm_helper_mode_fill_fb_struct(dev, &ufb->base, mode_cmd); |
Daniel Vetter | c7d73f6 | 2012-12-13 23:38:38 +0100 | [diff] [blame] | 345 | ret = drm_framebuffer_init(dev, &ufb->base, &udlfb_funcs); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | |
Daniel Vetter | cd5428a | 2013-01-21 23:42:49 +0100 | [diff] [blame] | 350 | static int udlfb_create(struct drm_fb_helper *helper, |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 351 | struct drm_fb_helper_surface_size *sizes) |
| 352 | { |
Fabian Frederick | ee0d68a | 2014-09-14 18:40:22 +0200 | [diff] [blame] | 353 | struct udl_fbdev *ufbdev = |
| 354 | container_of(helper, struct udl_fbdev, helper); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 355 | struct drm_device *dev = ufbdev->helper.dev; |
| 356 | struct fb_info *info; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 357 | struct drm_framebuffer *fb; |
| 358 | struct drm_mode_fb_cmd2 mode_cmd; |
| 359 | struct udl_gem_object *obj; |
| 360 | uint32_t size; |
| 361 | int ret = 0; |
| 362 | |
| 363 | if (sizes->surface_bpp == 24) |
| 364 | sizes->surface_bpp = 32; |
| 365 | |
| 366 | mode_cmd.width = sizes->surface_width; |
| 367 | mode_cmd.height = sizes->surface_height; |
| 368 | mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8); |
| 369 | |
| 370 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, |
| 371 | sizes->surface_depth); |
| 372 | |
| 373 | size = mode_cmd.pitches[0] * mode_cmd.height; |
| 374 | size = ALIGN(size, PAGE_SIZE); |
| 375 | |
| 376 | obj = udl_gem_alloc_object(dev, size); |
| 377 | if (!obj) |
| 378 | goto out; |
| 379 | |
| 380 | ret = udl_gem_vmap(obj); |
| 381 | if (ret) { |
| 382 | DRM_ERROR("failed to vmap fb\n"); |
| 383 | goto out_gfree; |
| 384 | } |
| 385 | |
Archit Taneja | 41ba2f3 | 2015-07-22 14:58:16 +0530 | [diff] [blame] | 386 | info = drm_fb_helper_alloc_fbi(helper); |
| 387 | if (IS_ERR(info)) { |
| 388 | ret = PTR_ERR(info); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 389 | goto out_gfree; |
| 390 | } |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 391 | |
| 392 | ret = udl_framebuffer_init(dev, &ufbdev->ufb, &mode_cmd, obj); |
| 393 | if (ret) |
Daniel Vetter | da7bdda | 2017-02-07 17:16:03 +0100 | [diff] [blame] | 394 | goto out_gfree; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 395 | |
| 396 | fb = &ufbdev->ufb.base; |
| 397 | |
| 398 | ufbdev->helper.fb = fb; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 399 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 400 | info->screen_base = ufbdev->ufb.obj->vmapping; |
| 401 | info->fix.smem_len = size; |
| 402 | info->fix.smem_start = (unsigned long)ufbdev->ufb.obj->vmapping; |
| 403 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 404 | info->fbops = &udlfb_ops; |
Daniel Vetter | 5a3887b | 2019-03-26 14:20:06 +0100 | [diff] [blame] | 405 | drm_fb_helper_fill_info(info, &ufbdev->helper, sizes); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 406 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 407 | DRM_DEBUG_KMS("allocated %dx%d vmal %p\n", |
| 408 | fb->width, fb->height, |
| 409 | ufbdev->ufb.obj->vmapping); |
| 410 | |
| 411 | return ret; |
| 412 | out_gfree: |
Cihangir Akturk | 823ee8b | 2017-08-11 15:33:09 +0300 | [diff] [blame] | 413 | drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 414 | out: |
| 415 | return ret; |
| 416 | } |
| 417 | |
Thierry Reding | 3a49387 | 2014-06-27 17:19:23 +0200 | [diff] [blame] | 418 | static const struct drm_fb_helper_funcs udl_fb_helper_funcs = { |
Daniel Vetter | cd5428a | 2013-01-21 23:42:49 +0100 | [diff] [blame] | 419 | .fb_probe = udlfb_create, |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 420 | }; |
| 421 | |
| 422 | static void udl_fbdev_destroy(struct drm_device *dev, |
| 423 | struct udl_fbdev *ufbdev) |
| 424 | { |
Archit Taneja | 41ba2f3 | 2015-07-22 14:58:16 +0530 | [diff] [blame] | 425 | drm_fb_helper_unregister_fbi(&ufbdev->helper); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 426 | drm_fb_helper_fini(&ufbdev->helper); |
Emil Lundmark | fcb74da | 2018-05-28 16:27:11 +0200 | [diff] [blame] | 427 | if (ufbdev->ufb.obj) { |
| 428 | drm_framebuffer_unregister_private(&ufbdev->ufb.base); |
| 429 | drm_framebuffer_cleanup(&ufbdev->ufb.base); |
| 430 | drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base); |
| 431 | } |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | int udl_fbdev_init(struct drm_device *dev) |
| 435 | { |
Dave Airlie | fd96e0d | 2019-04-05 13:17:14 +1000 | [diff] [blame] | 436 | struct udl_device *udl = to_udl(dev); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 437 | int bpp_sel = fb_bpp; |
| 438 | struct udl_fbdev *ufbdev; |
| 439 | int ret; |
| 440 | |
| 441 | ufbdev = kzalloc(sizeof(struct udl_fbdev), GFP_KERNEL); |
| 442 | if (!ufbdev) |
| 443 | return -ENOMEM; |
| 444 | |
| 445 | udl->fbdev = ufbdev; |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 446 | |
| 447 | drm_fb_helper_prepare(dev, &ufbdev->helper, &udl_fb_helper_funcs); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 448 | |
Gabriel Krisman Bertazi | e4563f6 | 2017-02-02 14:26:40 -0200 | [diff] [blame] | 449 | ret = drm_fb_helper_init(dev, &ufbdev->helper, 1); |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 450 | if (ret) |
| 451 | goto free; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 452 | |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 453 | ret = drm_fb_helper_single_add_all_connectors(&ufbdev->helper); |
| 454 | if (ret) |
| 455 | goto fini; |
Daniel Vetter | 76a39db | 2013-01-20 23:12:54 +0100 | [diff] [blame] | 456 | |
| 457 | /* disable all the possible outputs/crtcs before entering KMS mode */ |
| 458 | drm_helper_disable_unused_functions(dev); |
| 459 | |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 460 | ret = drm_fb_helper_initial_config(&ufbdev->helper, bpp_sel); |
| 461 | if (ret) |
| 462 | goto fini; |
| 463 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 464 | return 0; |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 465 | |
| 466 | fini: |
| 467 | drm_fb_helper_fini(&ufbdev->helper); |
| 468 | free: |
| 469 | kfree(ufbdev); |
| 470 | return ret; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | void udl_fbdev_cleanup(struct drm_device *dev) |
| 474 | { |
Dave Airlie | fd96e0d | 2019-04-05 13:17:14 +1000 | [diff] [blame] | 475 | struct udl_device *udl = to_udl(dev); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 476 | if (!udl->fbdev) |
| 477 | return; |
| 478 | |
| 479 | udl_fbdev_destroy(dev, udl->fbdev); |
| 480 | kfree(udl->fbdev); |
| 481 | udl->fbdev = NULL; |
| 482 | } |
| 483 | |
| 484 | void udl_fbdev_unplug(struct drm_device *dev) |
| 485 | { |
Dave Airlie | fd96e0d | 2019-04-05 13:17:14 +1000 | [diff] [blame] | 486 | struct udl_device *udl = to_udl(dev); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 487 | struct udl_fbdev *ufbdev; |
| 488 | if (!udl->fbdev) |
| 489 | return; |
| 490 | |
| 491 | ufbdev = udl->fbdev; |
Archit Taneja | 41ba2f3 | 2015-07-22 14:58:16 +0530 | [diff] [blame] | 492 | drm_fb_helper_unlink_fbi(&ufbdev->helper); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | struct drm_framebuffer * |
| 496 | udl_fb_user_fb_create(struct drm_device *dev, |
| 497 | struct drm_file *file, |
Ville Syrjälä | 1eb8345 | 2015-11-11 19:11:29 +0200 | [diff] [blame] | 498 | const struct drm_mode_fb_cmd2 *mode_cmd) |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 499 | { |
| 500 | struct drm_gem_object *obj; |
| 501 | struct udl_framebuffer *ufb; |
| 502 | int ret; |
Dave Airlie | 96503f5 | 2011-12-21 11:23:44 +0000 | [diff] [blame] | 503 | uint32_t size; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 504 | |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 505 | obj = drm_gem_object_lookup(file, mode_cmd->handles[0]); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 506 | if (obj == NULL) |
| 507 | return ERR_PTR(-ENOENT); |
| 508 | |
Dave Airlie | 96503f5 | 2011-12-21 11:23:44 +0000 | [diff] [blame] | 509 | size = mode_cmd->pitches[0] * mode_cmd->height; |
| 510 | size = ALIGN(size, PAGE_SIZE); |
| 511 | |
| 512 | if (size > obj->size) { |
| 513 | DRM_ERROR("object size not sufficient for fb %d %zu %d %d\n", size, obj->size, mode_cmd->pitches[0], mode_cmd->height); |
| 514 | return ERR_PTR(-ENOMEM); |
| 515 | } |
| 516 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 517 | ufb = kzalloc(sizeof(*ufb), GFP_KERNEL); |
| 518 | if (ufb == NULL) |
| 519 | return ERR_PTR(-ENOMEM); |
| 520 | |
| 521 | ret = udl_framebuffer_init(dev, ufb, mode_cmd, to_udl_bo(obj)); |
| 522 | if (ret) { |
| 523 | kfree(ufb); |
| 524 | return ERR_PTR(-EINVAL); |
| 525 | } |
| 526 | return &ufb->base; |
| 527 | } |