udl_fb.c 16 KB

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