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