udl_fb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. int udl_handle_damage(struct udl_framebuffer *fb, int x, int y,
  63. int width, int height)
  64. {
  65. struct drm_device *dev = fb->base.dev;
  66. struct udl_device *udl = dev->dev_private;
  67. int i, ret;
  68. char *cmd;
  69. cycles_t start_cycles, end_cycles;
  70. int bytes_sent = 0;
  71. int bytes_identical = 0;
  72. struct urb *urb;
  73. int aligned_x;
  74. int bpp = (fb->base.bits_per_pixel / 8);
  75. if (!fb->active_16)
  76. return 0;
  77. if (!fb->obj->vmapping) {
  78. ret = udl_gem_vmap(fb->obj);
  79. if (ret == -ENOMEM) {
  80. DRM_ERROR("failed to vmap fb\n");
  81. return 0;
  82. }
  83. if (!fb->obj->vmapping) {
  84. DRM_ERROR("failed to vmapping\n");
  85. return 0;
  86. }
  87. }
  88. aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
  89. width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
  90. x = aligned_x;
  91. if ((width <= 0) ||
  92. (x + width > fb->base.width) ||
  93. (y + height > fb->base.height))
  94. return -EINVAL;
  95. start_cycles = get_cycles();
  96. urb = udl_get_urb(dev);
  97. if (!urb)
  98. return 0;
  99. cmd = urb->transfer_buffer;
  100. for (i = y; i < height ; i++) {
  101. const int line_offset = fb->base.pitches[0] * i;
  102. const int byte_offset = line_offset + (x * bpp);
  103. const int dev_byte_offset = (fb->base.width * bpp * i) + (x * bpp);
  104. if (udl_render_hline(dev, bpp, &urb,
  105. (char *) fb->obj->vmapping,
  106. &cmd, byte_offset, dev_byte_offset,
  107. width * bpp,
  108. &bytes_identical, &bytes_sent))
  109. goto error;
  110. }
  111. if (cmd > (char *) urb->transfer_buffer) {
  112. /* Send partial buffer remaining before exiting */
  113. int len = cmd - (char *) urb->transfer_buffer;
  114. ret = udl_submit_urb(dev, urb, len);
  115. bytes_sent += len;
  116. } else
  117. udl_urb_completion(urb);
  118. error:
  119. atomic_add(bytes_sent, &udl->bytes_sent);
  120. atomic_add(bytes_identical, &udl->bytes_identical);
  121. atomic_add(width*height*bpp, &udl->bytes_rendered);
  122. end_cycles = get_cycles();
  123. atomic_add(((unsigned int) ((end_cycles - start_cycles)
  124. >> 10)), /* Kcycles */
  125. &udl->cpu_kcycles_used);
  126. return 0;
  127. }
  128. static int udl_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
  129. {
  130. unsigned long start = vma->vm_start;
  131. unsigned long size = vma->vm_end - vma->vm_start;
  132. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  133. unsigned long page, pos;
  134. if (offset + size > info->fix.smem_len)
  135. return -EINVAL;
  136. pos = (unsigned long)info->fix.smem_start + offset;
  137. pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
  138. pos, size);
  139. while (size > 0) {
  140. page = vmalloc_to_pfn((void *)pos);
  141. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
  142. return -EAGAIN;
  143. start += PAGE_SIZE;
  144. pos += PAGE_SIZE;
  145. if (size > PAGE_SIZE)
  146. size -= PAGE_SIZE;
  147. else
  148. size = 0;
  149. }
  150. /* VM_IO | VM_DONTEXPAND | VM_DONTDUMP are set by remap_pfn_range() */
  151. return 0;
  152. }
  153. /*
  154. * It's common for several clients to have framebuffer open simultaneously.
  155. * e.g. both fbcon and X. Makes things interesting.
  156. * Assumes caller is holding info->lock (for open and release at least)
  157. */
  158. static int udl_fb_open(struct fb_info *info, int user)
  159. {
  160. struct udl_fbdev *ufbdev = info->par;
  161. struct drm_device *dev = ufbdev->ufb.base.dev;
  162. struct udl_device *udl = dev->dev_private;
  163. /* If the USB device is gone, we don't accept new opens */
  164. if (drm_device_is_unplugged(udl->ddev))
  165. return -ENODEV;
  166. ufbdev->fb_count++;
  167. if (fb_defio && (info->fbdefio == NULL)) {
  168. /* enable defio at last moment if not disabled by client */
  169. struct fb_deferred_io *fbdefio;
  170. fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
  171. if (fbdefio) {
  172. fbdefio->delay = DL_DEFIO_WRITE_DELAY;
  173. fbdefio->deferred_io = drm_fb_helper_deferred_io;
  174. }
  175. info->fbdefio = fbdefio;
  176. fb_deferred_io_init(info);
  177. }
  178. pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
  179. info->node, user, info, ufbdev->fb_count);
  180. return 0;
  181. }
  182. /*
  183. * Assumes caller is holding info->lock mutex (for open and release at least)
  184. */
  185. static int udl_fb_release(struct fb_info *info, int user)
  186. {
  187. struct udl_fbdev *ufbdev = info->par;
  188. ufbdev->fb_count--;
  189. if ((ufbdev->fb_count == 0) && (info->fbdefio)) {
  190. fb_deferred_io_cleanup(info);
  191. kfree(info->fbdefio);
  192. info->fbdefio = NULL;
  193. info->fbops->fb_mmap = udl_fb_mmap;
  194. }
  195. pr_warn("released /dev/fb%d user=%d count=%d\n",
  196. info->node, user, ufbdev->fb_count);
  197. return 0;
  198. }
  199. static struct fb_ops udlfb_ops = {
  200. .owner = THIS_MODULE,
  201. .fb_check_var = drm_fb_helper_check_var,
  202. .fb_set_par = drm_fb_helper_set_par,
  203. .fb_fillrect = drm_fb_helper_sys_fillrect,
  204. .fb_copyarea = drm_fb_helper_sys_copyarea,
  205. .fb_imageblit = drm_fb_helper_sys_imageblit,
  206. .fb_pan_display = drm_fb_helper_pan_display,
  207. .fb_blank = drm_fb_helper_blank,
  208. .fb_setcmap = drm_fb_helper_setcmap,
  209. .fb_debug_enter = drm_fb_helper_debug_enter,
  210. .fb_debug_leave = drm_fb_helper_debug_leave,
  211. .fb_mmap = udl_fb_mmap,
  212. .fb_open = udl_fb_open,
  213. .fb_release = udl_fb_release,
  214. };
  215. static int udl_user_framebuffer_dirty(struct drm_framebuffer *fb,
  216. struct drm_file *file,
  217. unsigned flags, unsigned color,
  218. struct drm_clip_rect *clips,
  219. unsigned num_clips)
  220. {
  221. struct udl_framebuffer *ufb = to_udl_fb(fb);
  222. int i;
  223. int ret = 0;
  224. drm_modeset_lock_all(fb->dev);
  225. if (!ufb->active_16)
  226. goto unlock;
  227. if (ufb->obj->base.import_attach) {
  228. ret = dma_buf_begin_cpu_access(ufb->obj->base.import_attach->dmabuf,
  229. DMA_FROM_DEVICE);
  230. if (ret)
  231. goto unlock;
  232. }
  233. for (i = 0; i < num_clips; i++) {
  234. ret = udl_handle_damage(ufb, clips[i].x1, clips[i].y1,
  235. clips[i].x2 - clips[i].x1,
  236. clips[i].y2 - clips[i].y1);
  237. if (ret)
  238. break;
  239. }
  240. if (ufb->obj->base.import_attach) {
  241. ret = dma_buf_end_cpu_access(ufb->obj->base.import_attach->dmabuf,
  242. DMA_FROM_DEVICE);
  243. }
  244. unlock:
  245. drm_modeset_unlock_all(fb->dev);
  246. return ret;
  247. }
  248. static void udl_user_framebuffer_destroy(struct drm_framebuffer *fb)
  249. {
  250. struct udl_framebuffer *ufb = to_udl_fb(fb);
  251. if (ufb->obj)
  252. drm_gem_object_unreference_unlocked(&ufb->obj->base);
  253. drm_framebuffer_cleanup(fb);
  254. kfree(ufb);
  255. }
  256. static const struct drm_framebuffer_funcs udlfb_funcs = {
  257. .destroy = udl_user_framebuffer_destroy,
  258. .dirty = udl_user_framebuffer_dirty,
  259. };
  260. static int
  261. udl_framebuffer_init(struct drm_device *dev,
  262. struct udl_framebuffer *ufb,
  263. const struct drm_mode_fb_cmd2 *mode_cmd,
  264. struct udl_gem_object *obj)
  265. {
  266. int ret;
  267. ufb->obj = obj;
  268. drm_helper_mode_fill_fb_struct(&ufb->base, mode_cmd);
  269. ret = drm_framebuffer_init(dev, &ufb->base, &udlfb_funcs);
  270. return ret;
  271. }
  272. static int udlfb_create(struct drm_fb_helper *helper,
  273. struct drm_fb_helper_surface_size *sizes)
  274. {
  275. struct udl_fbdev *ufbdev =
  276. container_of(helper, struct udl_fbdev, helper);
  277. struct drm_device *dev = ufbdev->helper.dev;
  278. struct fb_info *info;
  279. struct drm_framebuffer *fb;
  280. struct drm_mode_fb_cmd2 mode_cmd;
  281. struct udl_gem_object *obj;
  282. uint32_t size;
  283. int ret = 0;
  284. if (sizes->surface_bpp == 24)
  285. sizes->surface_bpp = 32;
  286. mode_cmd.width = sizes->surface_width;
  287. mode_cmd.height = sizes->surface_height;
  288. mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
  289. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  290. sizes->surface_depth);
  291. size = mode_cmd.pitches[0] * mode_cmd.height;
  292. size = ALIGN(size, PAGE_SIZE);
  293. obj = udl_gem_alloc_object(dev, size);
  294. if (!obj)
  295. goto out;
  296. ret = udl_gem_vmap(obj);
  297. if (ret) {
  298. DRM_ERROR("failed to vmap fb\n");
  299. goto out_gfree;
  300. }
  301. info = drm_fb_helper_alloc_fbi(helper);
  302. if (IS_ERR(info)) {
  303. ret = PTR_ERR(info);
  304. goto out_gfree;
  305. }
  306. info->par = ufbdev;
  307. ret = udl_framebuffer_init(dev, &ufbdev->ufb, &mode_cmd, obj);
  308. if (ret)
  309. goto out_destroy_fbi;
  310. fb = &ufbdev->ufb.base;
  311. ufbdev->helper.fb = fb;
  312. strcpy(info->fix.id, "udldrmfb");
  313. info->screen_base = ufbdev->ufb.obj->vmapping;
  314. info->fix.smem_len = size;
  315. info->fix.smem_start = (unsigned long)ufbdev->ufb.obj->vmapping;
  316. info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  317. info->fbops = &udlfb_ops;
  318. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  319. drm_fb_helper_fill_var(info, &ufbdev->helper, sizes->fb_width, sizes->fb_height);
  320. DRM_DEBUG_KMS("allocated %dx%d vmal %p\n",
  321. fb->width, fb->height,
  322. ufbdev->ufb.obj->vmapping);
  323. return ret;
  324. out_destroy_fbi:
  325. drm_fb_helper_release_fbi(helper);
  326. out_gfree:
  327. drm_gem_object_unreference_unlocked(&ufbdev->ufb.obj->base);
  328. out:
  329. return ret;
  330. }
  331. static const struct drm_fb_helper_funcs udl_fb_helper_funcs = {
  332. .fb_probe = udlfb_create,
  333. };
  334. static void udl_fbdev_destroy(struct drm_device *dev,
  335. struct udl_fbdev *ufbdev)
  336. {
  337. drm_fb_helper_unregister_fbi(&ufbdev->helper);
  338. drm_fb_helper_release_fbi(&ufbdev->helper);
  339. drm_fb_helper_fini(&ufbdev->helper);
  340. drm_framebuffer_unregister_private(&ufbdev->ufb.base);
  341. drm_framebuffer_cleanup(&ufbdev->ufb.base);
  342. drm_gem_object_unreference_unlocked(&ufbdev->ufb.obj->base);
  343. }
  344. int udl_fbdev_init(struct drm_device *dev)
  345. {
  346. struct udl_device *udl = dev->dev_private;
  347. int bpp_sel = fb_bpp;
  348. struct udl_fbdev *ufbdev;
  349. int ret;
  350. ufbdev = kzalloc(sizeof(struct udl_fbdev), GFP_KERNEL);
  351. if (!ufbdev)
  352. return -ENOMEM;
  353. udl->fbdev = ufbdev;
  354. drm_fb_helper_prepare(dev, &ufbdev->helper, &udl_fb_helper_funcs);
  355. ret = drm_fb_helper_init(dev, &ufbdev->helper,
  356. 1, 1);
  357. if (ret)
  358. goto free;
  359. ret = drm_fb_helper_single_add_all_connectors(&ufbdev->helper);
  360. if (ret)
  361. goto fini;
  362. /* disable all the possible outputs/crtcs before entering KMS mode */
  363. drm_helper_disable_unused_functions(dev);
  364. ret = drm_fb_helper_initial_config(&ufbdev->helper, bpp_sel);
  365. if (ret)
  366. goto fini;
  367. return 0;
  368. fini:
  369. drm_fb_helper_fini(&ufbdev->helper);
  370. free:
  371. kfree(ufbdev);
  372. return ret;
  373. }
  374. void udl_fbdev_cleanup(struct drm_device *dev)
  375. {
  376. struct udl_device *udl = dev->dev_private;
  377. if (!udl->fbdev)
  378. return;
  379. udl_fbdev_destroy(dev, udl->fbdev);
  380. kfree(udl->fbdev);
  381. udl->fbdev = NULL;
  382. }
  383. void udl_fbdev_unplug(struct drm_device *dev)
  384. {
  385. struct udl_device *udl = dev->dev_private;
  386. struct udl_fbdev *ufbdev;
  387. if (!udl->fbdev)
  388. return;
  389. ufbdev = udl->fbdev;
  390. drm_fb_helper_unlink_fbi(&ufbdev->helper);
  391. }
  392. struct drm_framebuffer *
  393. udl_fb_user_fb_create(struct drm_device *dev,
  394. struct drm_file *file,
  395. const struct drm_mode_fb_cmd2 *mode_cmd)
  396. {
  397. struct drm_gem_object *obj;
  398. struct udl_framebuffer *ufb;
  399. int ret;
  400. uint32_t size;
  401. obj = drm_gem_object_lookup(file, mode_cmd->handles[0]);
  402. if (obj == NULL)
  403. return ERR_PTR(-ENOENT);
  404. size = mode_cmd->pitches[0] * mode_cmd->height;
  405. size = ALIGN(size, PAGE_SIZE);
  406. if (size > obj->size) {
  407. DRM_ERROR("object size not sufficient for fb %d %zu %d %d\n", size, obj->size, mode_cmd->pitches[0], mode_cmd->height);
  408. return ERR_PTR(-ENOMEM);
  409. }
  410. ufb = kzalloc(sizeof(*ufb), GFP_KERNEL);
  411. if (ufb == NULL)
  412. return ERR_PTR(-ENOMEM);
  413. ret = udl_framebuffer_init(dev, ufb, mode_cmd, to_udl_bo(obj));
  414. if (ret) {
  415. kfree(ufb);
  416. return ERR_PTR(-EINVAL);
  417. }
  418. return &ufb->base;
  419. }