blob: caa84f1de9c1d46b8d35dd218945ad504b084292 [file] [log] [blame]
Dave Airlie53209182010-12-15 07:14:24 +10001/*
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 Airlie32ecd242012-07-30 14:06:55 +100016#include <linux/dma-buf.h>
Dave Airlie53209182010-12-15 07:14:24 +100017
David Howells760285e2012-10-02 18:01:07 +010018#include <drm/drmP.h>
19#include <drm/drm_crtc.h>
20#include <drm/drm_crtc_helper.h>
Dave Airlie53209182010-12-15 07:14:24 +100021#include "udl_drv.h"
22
David Howells760285e2012-10-02 18:01:07 +010023#include <drm/drm_fb_helper.h>
Dave Airlie53209182010-12-15 07:14:24 +100024
25#define DL_DEFIO_WRITE_DELAY 5 /* fb_deferred_io.delay in jiffies */
26
27static int fb_defio = 1; /* Optionally enable experimental fb_defio mmap support */
28static int fb_bpp = 16;
29
30module_param(fb_bpp, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
31module_param(fb_defio, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
32
33struct udl_fbdev {
34 struct drm_fb_helper helper;
35 struct udl_framebuffer ufb;
36 struct list_head fbdev_list;
37 int fb_count;
38};
39
40#define DL_ALIGN_UP(x, a) ALIGN(x, a)
41#define DL_ALIGN_DOWN(x, a) ALIGN(x-(a-1), 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
62static 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
71static 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/*
82 * NOTE: fb_defio.c is holding info->fbdefio.mutex
83 * Touching ANY framebuffer memory that triggers a page fault
84 * in fb_defio will cause a deadlock, when it also tries to
85 * grab the same mutex.
86 */
87static void udlfb_dpy_deferred_io(struct fb_info *info,
88 struct list_head *pagelist)
89{
90 struct page *cur;
91 struct fb_deferred_io *fbdefio = info->fbdefio;
92 struct udl_fbdev *ufbdev = info->par;
93 struct drm_device *dev = ufbdev->ufb.base.dev;
94 struct udl_device *udl = dev->dev_private;
95 struct urb *urb;
96 char *cmd;
97 cycles_t start_cycles, end_cycles;
98 int bytes_sent = 0;
99 int bytes_identical = 0;
100 int bytes_rendered = 0;
101
102 if (!fb_defio)
103 return;
104
105 start_cycles = get_cycles();
106
107 urb = udl_get_urb(dev);
108 if (!urb)
109 return;
110
111 cmd = urb->transfer_buffer;
112
113 /* walk the written page list and render each to device */
114 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
115
116 if (udl_render_hline(dev, (ufbdev->ufb.base.bits_per_pixel / 8),
Dave Airlie3916e1d2012-11-01 13:47:09 +1000117 &urb, (char *) info->fix.smem_start,
118 &cmd, cur->index << PAGE_SHIFT,
119 cur->index << PAGE_SHIFT,
120 PAGE_SIZE, &bytes_identical, &bytes_sent))
Dave Airlie53209182010-12-15 07:14:24 +1000121 goto error;
122 bytes_rendered += PAGE_SIZE;
123 }
124
125 if (cmd > (char *) urb->transfer_buffer) {
126 /* Send partial buffer remaining before exiting */
127 int len = cmd - (char *) urb->transfer_buffer;
128 udl_submit_urb(dev, urb, len);
129 bytes_sent += len;
130 } else
131 udl_urb_completion(urb);
132
133error:
134 atomic_add(bytes_sent, &udl->bytes_sent);
135 atomic_add(bytes_identical, &udl->bytes_identical);
136 atomic_add(bytes_rendered, &udl->bytes_rendered);
137 end_cycles = get_cycles();
138 atomic_add(((unsigned int) ((end_cycles - start_cycles)
139 >> 10)), /* Kcycles */
140 &udl->cpu_kcycles_used);
141}
142
143int udl_handle_damage(struct udl_framebuffer *fb, int x, int y,
144 int width, int height)
145{
146 struct drm_device *dev = fb->base.dev;
147 struct udl_device *udl = dev->dev_private;
148 int i, ret;
149 char *cmd;
150 cycles_t start_cycles, end_cycles;
151 int bytes_sent = 0;
152 int bytes_identical = 0;
153 struct urb *urb;
154 int aligned_x;
155 int bpp = (fb->base.bits_per_pixel / 8);
156
157 if (!fb->active_16)
158 return 0;
159
Dave Airliee8aa1d12012-03-26 14:36:56 +0100160 if (!fb->obj->vmapping) {
161 ret = udl_gem_vmap(fb->obj);
162 if (ret == -ENOMEM) {
163 DRM_ERROR("failed to vmap fb\n");
164 return 0;
165 }
166 if (!fb->obj->vmapping) {
167 DRM_ERROR("failed to vmapping\n");
168 return 0;
169 }
170 }
Dave Airlie53209182010-12-15 07:14:24 +1000171
172 start_cycles = get_cycles();
173
174 aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
175 width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
176 x = aligned_x;
177
178 if ((width <= 0) ||
179 (x + width > fb->base.width) ||
180 (y + height > fb->base.height))
181 return -EINVAL;
182
183 urb = udl_get_urb(dev);
184 if (!urb)
185 return 0;
186 cmd = urb->transfer_buffer;
187
188 for (i = y; i < y + height ; i++) {
189 const int line_offset = fb->base.pitches[0] * i;
190 const int byte_offset = line_offset + (x * bpp);
Dave Airlie3916e1d2012-11-01 13:47:09 +1000191 const int dev_byte_offset = (fb->base.width * bpp * i) + (x * bpp);
Dave Airlie53209182010-12-15 07:14:24 +1000192 if (udl_render_hline(dev, bpp, &urb,
193 (char *) fb->obj->vmapping,
Dave Airlie3916e1d2012-11-01 13:47:09 +1000194 &cmd, byte_offset, dev_byte_offset,
195 width * bpp,
Dave Airlie53209182010-12-15 07:14:24 +1000196 &bytes_identical, &bytes_sent))
197 goto error;
198 }
199
200 if (cmd > (char *) urb->transfer_buffer) {
201 /* Send partial buffer remaining before exiting */
202 int len = cmd - (char *) urb->transfer_buffer;
203 ret = udl_submit_urb(dev, urb, len);
204 bytes_sent += len;
205 } else
206 udl_urb_completion(urb);
207
208error:
209 atomic_add(bytes_sent, &udl->bytes_sent);
210 atomic_add(bytes_identical, &udl->bytes_identical);
211 atomic_add(width*height*bpp, &udl->bytes_rendered);
212 end_cycles = get_cycles();
213 atomic_add(((unsigned int) ((end_cycles - start_cycles)
214 >> 10)), /* Kcycles */
215 &udl->cpu_kcycles_used);
216
217 return 0;
218}
219
220static int udl_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
221{
222 unsigned long start = vma->vm_start;
223 unsigned long size = vma->vm_end - vma->vm_start;
224 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
225 unsigned long page, pos;
226
227 if (offset + size > info->fix.smem_len)
228 return -EINVAL;
229
230 pos = (unsigned long)info->fix.smem_start + offset;
231
232 pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
233 pos, size);
234
235 while (size > 0) {
236 page = vmalloc_to_pfn((void *)pos);
237 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
238 return -EAGAIN;
239
240 start += PAGE_SIZE;
241 pos += PAGE_SIZE;
242 if (size > PAGE_SIZE)
243 size -= PAGE_SIZE;
244 else
245 size = 0;
246 }
247
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700248 /* VM_IO | VM_DONTEXPAND | VM_DONTDUMP are set by remap_pfn_range() */
Dave Airlie53209182010-12-15 07:14:24 +1000249 return 0;
250}
251
252static void udl_fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
253{
254 struct udl_fbdev *ufbdev = info->par;
255
256 sys_fillrect(info, rect);
257
258 udl_handle_damage(&ufbdev->ufb, rect->dx, rect->dy, rect->width,
259 rect->height);
260}
261
262static void udl_fb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
263{
264 struct udl_fbdev *ufbdev = info->par;
265
266 sys_copyarea(info, region);
267
268 udl_handle_damage(&ufbdev->ufb, region->dx, region->dy, region->width,
269 region->height);
270}
271
272static void udl_fb_imageblit(struct fb_info *info, const struct fb_image *image)
273{
274 struct udl_fbdev *ufbdev = info->par;
275
276 sys_imageblit(info, image);
277
278 udl_handle_damage(&ufbdev->ufb, image->dx, image->dy, image->width,
279 image->height);
280}
281
282/*
283 * It's common for several clients to have framebuffer open simultaneously.
284 * e.g. both fbcon and X. Makes things interesting.
285 * Assumes caller is holding info->lock (for open and release at least)
286 */
287static int udl_fb_open(struct fb_info *info, int user)
288{
289 struct udl_fbdev *ufbdev = info->par;
290 struct drm_device *dev = ufbdev->ufb.base.dev;
291 struct udl_device *udl = dev->dev_private;
292
293 /* If the USB device is gone, we don't accept new opens */
294 if (drm_device_is_unplugged(udl->ddev))
295 return -ENODEV;
296
297 ufbdev->fb_count++;
298
299 if (fb_defio && (info->fbdefio == NULL)) {
300 /* enable defio at last moment if not disabled by client */
301
302 struct fb_deferred_io *fbdefio;
303
304 fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
305
306 if (fbdefio) {
307 fbdefio->delay = DL_DEFIO_WRITE_DELAY;
308 fbdefio->deferred_io = udlfb_dpy_deferred_io;
309 }
310
311 info->fbdefio = fbdefio;
312 fb_deferred_io_init(info);
313 }
314
315 pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
316 info->node, user, info, ufbdev->fb_count);
317
318 return 0;
319}
320
321
322/*
323 * Assumes caller is holding info->lock mutex (for open and release at least)
324 */
325static int udl_fb_release(struct fb_info *info, int user)
326{
327 struct udl_fbdev *ufbdev = info->par;
328
329 ufbdev->fb_count--;
330
331 if ((ufbdev->fb_count == 0) && (info->fbdefio)) {
332 fb_deferred_io_cleanup(info);
333 kfree(info->fbdefio);
334 info->fbdefio = NULL;
335 info->fbops->fb_mmap = udl_fb_mmap;
336 }
337
338 pr_warn("released /dev/fb%d user=%d count=%d\n",
339 info->node, user, ufbdev->fb_count);
340
341 return 0;
342}
343
344static struct fb_ops udlfb_ops = {
345 .owner = THIS_MODULE,
346 .fb_check_var = drm_fb_helper_check_var,
347 .fb_set_par = drm_fb_helper_set_par,
348 .fb_fillrect = udl_fb_fillrect,
349 .fb_copyarea = udl_fb_copyarea,
350 .fb_imageblit = udl_fb_imageblit,
351 .fb_pan_display = drm_fb_helper_pan_display,
352 .fb_blank = drm_fb_helper_blank,
353 .fb_setcmap = drm_fb_helper_setcmap,
354 .fb_debug_enter = drm_fb_helper_debug_enter,
355 .fb_debug_leave = drm_fb_helper_debug_leave,
356 .fb_mmap = udl_fb_mmap,
357 .fb_open = udl_fb_open,
358 .fb_release = udl_fb_release,
359};
360
Sachin Kamat27796b42012-09-22 06:30:03 +0000361static void udl_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlie53209182010-12-15 07:14:24 +1000362 u16 blue, int regno)
363{
364}
365
Sachin Kamat27796b42012-09-22 06:30:03 +0000366static void udl_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
Dave Airlie53209182010-12-15 07:14:24 +1000367 u16 *blue, int regno)
368{
369 *red = 0;
370 *green = 0;
371 *blue = 0;
372}
373
374static int udl_user_framebuffer_dirty(struct drm_framebuffer *fb,
375 struct drm_file *file,
376 unsigned flags, unsigned color,
377 struct drm_clip_rect *clips,
378 unsigned num_clips)
379{
380 struct udl_framebuffer *ufb = to_udl_fb(fb);
381 int i;
Dave Airlie32ecd242012-07-30 14:06:55 +1000382 int ret = 0;
Dave Airlie53209182010-12-15 07:14:24 +1000383
384 if (!ufb->active_16)
385 return 0;
386
Dave Airlie32ecd242012-07-30 14:06:55 +1000387 if (ufb->obj->base.import_attach) {
388 ret = dma_buf_begin_cpu_access(ufb->obj->base.import_attach->dmabuf,
389 0, ufb->obj->base.size,
390 DMA_FROM_DEVICE);
391 if (ret)
392 return ret;
393 }
394
Dave Airlie53209182010-12-15 07:14:24 +1000395 for (i = 0; i < num_clips; i++) {
Dave Airlie32ecd242012-07-30 14:06:55 +1000396 ret = udl_handle_damage(ufb, clips[i].x1, clips[i].y1,
Dave Airlie53209182010-12-15 07:14:24 +1000397 clips[i].x2 - clips[i].x1,
398 clips[i].y2 - clips[i].y1);
Dave Airlie32ecd242012-07-30 14:06:55 +1000399 if (ret)
400 break;
Dave Airlie53209182010-12-15 07:14:24 +1000401 }
Dave Airlie32ecd242012-07-30 14:06:55 +1000402
403 if (ufb->obj->base.import_attach) {
404 dma_buf_end_cpu_access(ufb->obj->base.import_attach->dmabuf,
405 0, ufb->obj->base.size,
406 DMA_FROM_DEVICE);
407 }
408 return ret;
Dave Airlie53209182010-12-15 07:14:24 +1000409}
410
411static void udl_user_framebuffer_destroy(struct drm_framebuffer *fb)
412{
413 struct udl_framebuffer *ufb = to_udl_fb(fb);
414
415 if (ufb->obj)
416 drm_gem_object_unreference_unlocked(&ufb->obj->base);
417
418 drm_framebuffer_cleanup(fb);
419 kfree(ufb);
420}
421
422static const struct drm_framebuffer_funcs udlfb_funcs = {
423 .destroy = udl_user_framebuffer_destroy,
424 .dirty = udl_user_framebuffer_dirty,
Dave Airlie53209182010-12-15 07:14:24 +1000425};
426
427
428static int
429udl_framebuffer_init(struct drm_device *dev,
430 struct udl_framebuffer *ufb,
431 struct drm_mode_fb_cmd2 *mode_cmd,
432 struct udl_gem_object *obj)
433{
434 int ret;
435
436 ufb->obj = obj;
Dave Airlie53209182010-12-15 07:14:24 +1000437 drm_helper_mode_fill_fb_struct(&ufb->base, mode_cmd);
Daniel Vetterc7d73f62012-12-13 23:38:38 +0100438 ret = drm_framebuffer_init(dev, &ufb->base, &udlfb_funcs);
Dave Airlie53209182010-12-15 07:14:24 +1000439 return ret;
440}
441
442
443static int udlfb_create(struct udl_fbdev *ufbdev,
444 struct drm_fb_helper_surface_size *sizes)
445{
446 struct drm_device *dev = ufbdev->helper.dev;
447 struct fb_info *info;
448 struct device *device = &dev->usbdev->dev;
449 struct drm_framebuffer *fb;
450 struct drm_mode_fb_cmd2 mode_cmd;
451 struct udl_gem_object *obj;
452 uint32_t size;
453 int ret = 0;
454
455 if (sizes->surface_bpp == 24)
456 sizes->surface_bpp = 32;
457
458 mode_cmd.width = sizes->surface_width;
459 mode_cmd.height = sizes->surface_height;
460 mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
461
462 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
463 sizes->surface_depth);
464
465 size = mode_cmd.pitches[0] * mode_cmd.height;
466 size = ALIGN(size, PAGE_SIZE);
467
468 obj = udl_gem_alloc_object(dev, size);
469 if (!obj)
470 goto out;
471
472 ret = udl_gem_vmap(obj);
473 if (ret) {
474 DRM_ERROR("failed to vmap fb\n");
475 goto out_gfree;
476 }
477
478 info = framebuffer_alloc(0, device);
479 if (!info) {
480 ret = -ENOMEM;
481 goto out_gfree;
482 }
483 info->par = ufbdev;
484
485 ret = udl_framebuffer_init(dev, &ufbdev->ufb, &mode_cmd, obj);
486 if (ret)
487 goto out_gfree;
488
489 fb = &ufbdev->ufb.base;
490
491 ufbdev->helper.fb = fb;
492 ufbdev->helper.fbdev = info;
493
494 strcpy(info->fix.id, "udldrmfb");
495
496 info->screen_base = ufbdev->ufb.obj->vmapping;
497 info->fix.smem_len = size;
498 info->fix.smem_start = (unsigned long)ufbdev->ufb.obj->vmapping;
499
500 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
501 info->fbops = &udlfb_ops;
502 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
503 drm_fb_helper_fill_var(info, &ufbdev->helper, sizes->fb_width, sizes->fb_height);
504
505 ret = fb_alloc_cmap(&info->cmap, 256, 0);
506 if (ret) {
507 ret = -ENOMEM;
508 goto out_gfree;
509 }
510
511
512 DRM_DEBUG_KMS("allocated %dx%d vmal %p\n",
513 fb->width, fb->height,
514 ufbdev->ufb.obj->vmapping);
515
516 return ret;
517out_gfree:
518 drm_gem_object_unreference(&ufbdev->ufb.obj->base);
519out:
520 return ret;
521}
522
523static int udl_fb_find_or_create_single(struct drm_fb_helper *helper,
524 struct drm_fb_helper_surface_size *sizes)
525{
526 struct udl_fbdev *ufbdev = (struct udl_fbdev *)helper;
527 int new_fb = 0;
528 int ret;
529
530 if (!helper->fb) {
531 ret = udlfb_create(ufbdev, sizes);
532 if (ret)
533 return ret;
534
535 new_fb = 1;
536 }
537 return new_fb;
538}
539
540static struct drm_fb_helper_funcs udl_fb_helper_funcs = {
541 .gamma_set = udl_crtc_fb_gamma_set,
542 .gamma_get = udl_crtc_fb_gamma_get,
543 .fb_probe = udl_fb_find_or_create_single,
544};
545
546static void udl_fbdev_destroy(struct drm_device *dev,
547 struct udl_fbdev *ufbdev)
548{
549 struct fb_info *info;
550 if (ufbdev->helper.fbdev) {
551 info = ufbdev->helper.fbdev;
552 unregister_framebuffer(info);
553 if (info->cmap.len)
554 fb_dealloc_cmap(&info->cmap);
555 framebuffer_release(info);
556 }
557 drm_fb_helper_fini(&ufbdev->helper);
Daniel Vetter36206362012-12-10 20:42:17 +0100558 drm_framebuffer_unregister_private(&ufbdev->ufb.base);
Dave Airlie53209182010-12-15 07:14:24 +1000559 drm_framebuffer_cleanup(&ufbdev->ufb.base);
560 drm_gem_object_unreference_unlocked(&ufbdev->ufb.obj->base);
561}
562
563int udl_fbdev_init(struct drm_device *dev)
564{
565 struct udl_device *udl = dev->dev_private;
566 int bpp_sel = fb_bpp;
567 struct udl_fbdev *ufbdev;
568 int ret;
569
570 ufbdev = kzalloc(sizeof(struct udl_fbdev), GFP_KERNEL);
571 if (!ufbdev)
572 return -ENOMEM;
573
574 udl->fbdev = ufbdev;
575 ufbdev->helper.funcs = &udl_fb_helper_funcs;
576
577 ret = drm_fb_helper_init(dev, &ufbdev->helper,
578 1, 1);
579 if (ret) {
580 kfree(ufbdev);
581 return ret;
582
583 }
584
585 drm_fb_helper_single_add_all_connectors(&ufbdev->helper);
586 drm_fb_helper_initial_config(&ufbdev->helper, bpp_sel);
587 return 0;
588}
589
590void udl_fbdev_cleanup(struct drm_device *dev)
591{
592 struct udl_device *udl = dev->dev_private;
593 if (!udl->fbdev)
594 return;
595
596 udl_fbdev_destroy(dev, udl->fbdev);
597 kfree(udl->fbdev);
598 udl->fbdev = NULL;
599}
600
601void udl_fbdev_unplug(struct drm_device *dev)
602{
603 struct udl_device *udl = dev->dev_private;
604 struct udl_fbdev *ufbdev;
605 if (!udl->fbdev)
606 return;
607
608 ufbdev = udl->fbdev;
609 if (ufbdev->helper.fbdev) {
610 struct fb_info *info;
611 info = ufbdev->helper.fbdev;
612 unlink_framebuffer(info);
613 }
614}
615
616struct drm_framebuffer *
617udl_fb_user_fb_create(struct drm_device *dev,
618 struct drm_file *file,
619 struct drm_mode_fb_cmd2 *mode_cmd)
620{
621 struct drm_gem_object *obj;
622 struct udl_framebuffer *ufb;
623 int ret;
Dave Airlie96503f52011-12-21 11:23:44 +0000624 uint32_t size;
Dave Airlie53209182010-12-15 07:14:24 +1000625
626 obj = drm_gem_object_lookup(dev, file, mode_cmd->handles[0]);
627 if (obj == NULL)
628 return ERR_PTR(-ENOENT);
629
Dave Airlie96503f52011-12-21 11:23:44 +0000630 size = mode_cmd->pitches[0] * mode_cmd->height;
631 size = ALIGN(size, PAGE_SIZE);
632
633 if (size > obj->size) {
634 DRM_ERROR("object size not sufficient for fb %d %zu %d %d\n", size, obj->size, mode_cmd->pitches[0], mode_cmd->height);
635 return ERR_PTR(-ENOMEM);
636 }
637
Dave Airlie53209182010-12-15 07:14:24 +1000638 ufb = kzalloc(sizeof(*ufb), GFP_KERNEL);
639 if (ufb == NULL)
640 return ERR_PTR(-ENOMEM);
641
642 ret = udl_framebuffer_init(dev, ufb, mode_cmd, to_udl_bo(obj));
643 if (ret) {
644 kfree(ufb);
645 return ERR_PTR(-EINVAL);
646 }
647 return &ufb->base;
648}