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