udl_fb.c 16 KB

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