udl_fb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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.format->cpp[0];
  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 < y + 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. #ifdef CONFIG_DRM_FBDEV_EMULATION
  168. if (fb_defio && (info->fbdefio == NULL)) {
  169. /* enable defio at last moment if not disabled by client */
  170. struct fb_deferred_io *fbdefio;
  171. fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
  172. if (fbdefio) {
  173. fbdefio->delay = DL_DEFIO_WRITE_DELAY;
  174. fbdefio->deferred_io = drm_fb_helper_deferred_io;
  175. }
  176. info->fbdefio = fbdefio;
  177. fb_deferred_io_init(info);
  178. }
  179. #endif
  180. pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
  181. info->node, user, info, ufbdev->fb_count);
  182. return 0;
  183. }
  184. /*
  185. * Assumes caller is holding info->lock mutex (for open and release at least)
  186. */
  187. static int udl_fb_release(struct fb_info *info, int user)
  188. {
  189. struct udl_fbdev *ufbdev = info->par;
  190. ufbdev->fb_count--;
  191. #ifdef CONFIG_DRM_FBDEV_EMULATION
  192. if ((ufbdev->fb_count == 0) && (info->fbdefio)) {
  193. fb_deferred_io_cleanup(info);
  194. kfree(info->fbdefio);
  195. info->fbdefio = NULL;
  196. info->fbops->fb_mmap = udl_fb_mmap;
  197. }
  198. #endif
  199. pr_warn("released /dev/fb%d user=%d count=%d\n",
  200. info->node, user, ufbdev->fb_count);
  201. return 0;
  202. }
  203. static struct fb_ops udlfb_ops = {
  204. .owner = THIS_MODULE,
  205. DRM_FB_HELPER_DEFAULT_OPS,
  206. .fb_fillrect = drm_fb_helper_sys_fillrect,
  207. .fb_copyarea = drm_fb_helper_sys_copyarea,
  208. .fb_imageblit = drm_fb_helper_sys_imageblit,
  209. .fb_mmap = udl_fb_mmap,
  210. .fb_open = udl_fb_open,
  211. .fb_release = udl_fb_release,
  212. };
  213. static int udl_user_framebuffer_dirty(struct drm_framebuffer *fb,
  214. struct drm_file *file,
  215. unsigned flags, unsigned color,
  216. struct drm_clip_rect *clips,
  217. unsigned num_clips)
  218. {
  219. struct udl_framebuffer *ufb = to_udl_fb(fb);
  220. int i;
  221. int ret = 0;
  222. drm_modeset_lock_all(fb->dev);
  223. if (!ufb->active_16)
  224. goto unlock;
  225. if (ufb->obj->base.import_attach) {
  226. ret = dma_buf_begin_cpu_access(ufb->obj->base.import_attach->dmabuf,
  227. DMA_FROM_DEVICE);
  228. if (ret)
  229. goto unlock;
  230. }
  231. for (i = 0; i < num_clips; i++) {
  232. ret = udl_handle_damage(ufb, clips[i].x1, clips[i].y1,
  233. clips[i].x2 - clips[i].x1,
  234. clips[i].y2 - clips[i].y1);
  235. if (ret)
  236. break;
  237. }
  238. if (ufb->obj->base.import_attach) {
  239. ret = dma_buf_end_cpu_access(ufb->obj->base.import_attach->dmabuf,
  240. DMA_FROM_DEVICE);
  241. }
  242. unlock:
  243. drm_modeset_unlock_all(fb->dev);
  244. return ret;
  245. }
  246. static void udl_user_framebuffer_destroy(struct drm_framebuffer *fb)
  247. {
  248. struct udl_framebuffer *ufb = to_udl_fb(fb);
  249. if (ufb->obj)
  250. drm_gem_object_unreference_unlocked(&ufb->obj->base);
  251. drm_framebuffer_cleanup(fb);
  252. kfree(ufb);
  253. }
  254. static const struct drm_framebuffer_funcs udlfb_funcs = {
  255. .destroy = udl_user_framebuffer_destroy,
  256. .dirty = udl_user_framebuffer_dirty,
  257. };
  258. static int
  259. udl_framebuffer_init(struct drm_device *dev,
  260. struct udl_framebuffer *ufb,
  261. const struct drm_mode_fb_cmd2 *mode_cmd,
  262. struct udl_gem_object *obj)
  263. {
  264. int ret;
  265. ufb->obj = obj;
  266. drm_helper_mode_fill_fb_struct(dev, &ufb->base, mode_cmd);
  267. ret = drm_framebuffer_init(dev, &ufb->base, &udlfb_funcs);
  268. return ret;
  269. }
  270. static int udlfb_create(struct drm_fb_helper *helper,
  271. struct drm_fb_helper_surface_size *sizes)
  272. {
  273. struct udl_fbdev *ufbdev =
  274. container_of(helper, struct udl_fbdev, helper);
  275. struct drm_device *dev = ufbdev->helper.dev;
  276. struct fb_info *info;
  277. struct drm_framebuffer *fb;
  278. struct drm_mode_fb_cmd2 mode_cmd;
  279. struct udl_gem_object *obj;
  280. uint32_t size;
  281. int ret = 0;
  282. if (sizes->surface_bpp == 24)
  283. sizes->surface_bpp = 32;
  284. mode_cmd.width = sizes->surface_width;
  285. mode_cmd.height = sizes->surface_height;
  286. mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
  287. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  288. sizes->surface_depth);
  289. size = mode_cmd.pitches[0] * mode_cmd.height;
  290. size = ALIGN(size, PAGE_SIZE);
  291. obj = udl_gem_alloc_object(dev, size);
  292. if (!obj)
  293. goto out;
  294. ret = udl_gem_vmap(obj);
  295. if (ret) {
  296. DRM_ERROR("failed to vmap fb\n");
  297. goto out_gfree;
  298. }
  299. info = drm_fb_helper_alloc_fbi(helper);
  300. if (IS_ERR(info)) {
  301. ret = PTR_ERR(info);
  302. goto out_gfree;
  303. }
  304. info->par = ufbdev;
  305. ret = udl_framebuffer_init(dev, &ufbdev->ufb, &mode_cmd, obj);
  306. if (ret)
  307. goto out_destroy_fbi;
  308. fb = &ufbdev->ufb.base;
  309. ufbdev->helper.fb = fb;
  310. strcpy(info->fix.id, "udldrmfb");
  311. info->screen_base = ufbdev->ufb.obj->vmapping;
  312. info->fix.smem_len = size;
  313. info->fix.smem_start = (unsigned long)ufbdev->ufb.obj->vmapping;
  314. info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  315. info->fbops = &udlfb_ops;
  316. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
  317. drm_fb_helper_fill_var(info, &ufbdev->helper, sizes->fb_width, sizes->fb_height);
  318. DRM_DEBUG_KMS("allocated %dx%d vmal %p\n",
  319. fb->width, fb->height,
  320. ufbdev->ufb.obj->vmapping);
  321. return ret;
  322. out_destroy_fbi:
  323. drm_fb_helper_release_fbi(helper);
  324. out_gfree:
  325. drm_gem_object_unreference_unlocked(&ufbdev->ufb.obj->base);
  326. out:
  327. return ret;
  328. }
  329. static const struct drm_fb_helper_funcs udl_fb_helper_funcs = {
  330. .fb_probe = udlfb_create,
  331. };
  332. static void udl_fbdev_destroy(struct drm_device *dev,
  333. struct udl_fbdev *ufbdev)
  334. {
  335. drm_fb_helper_unregister_fbi(&ufbdev->helper);
  336. drm_fb_helper_release_fbi(&ufbdev->helper);
  337. drm_fb_helper_fini(&ufbdev->helper);
  338. drm_framebuffer_unregister_private(&ufbdev->ufb.base);
  339. drm_framebuffer_cleanup(&ufbdev->ufb.base);
  340. drm_gem_object_unreference_unlocked(&ufbdev->ufb.obj->base);
  341. }
  342. int udl_fbdev_init(struct drm_device *dev)
  343. {
  344. struct udl_device *udl = dev->dev_private;
  345. int bpp_sel = fb_bpp;
  346. struct udl_fbdev *ufbdev;
  347. int ret;
  348. ufbdev = kzalloc(sizeof(struct udl_fbdev), GFP_KERNEL);
  349. if (!ufbdev)
  350. return -ENOMEM;
  351. udl->fbdev = ufbdev;
  352. drm_fb_helper_prepare(dev, &ufbdev->helper, &udl_fb_helper_funcs);
  353. ret = drm_fb_helper_init(dev, &ufbdev->helper,
  354. 1, 1);
  355. if (ret)
  356. goto free;
  357. ret = drm_fb_helper_single_add_all_connectors(&ufbdev->helper);
  358. if (ret)
  359. goto fini;
  360. /* disable all the possible outputs/crtcs before entering KMS mode */
  361. drm_helper_disable_unused_functions(dev);
  362. ret = drm_fb_helper_initial_config(&ufbdev->helper, bpp_sel);
  363. if (ret)
  364. goto fini;
  365. return 0;
  366. fini:
  367. drm_fb_helper_fini(&ufbdev->helper);
  368. free:
  369. kfree(ufbdev);
  370. return ret;
  371. }
  372. void udl_fbdev_cleanup(struct drm_device *dev)
  373. {
  374. struct udl_device *udl = dev->dev_private;
  375. if (!udl->fbdev)
  376. return;
  377. udl_fbdev_destroy(dev, udl->fbdev);
  378. kfree(udl->fbdev);
  379. udl->fbdev = NULL;
  380. }
  381. void udl_fbdev_unplug(struct drm_device *dev)
  382. {
  383. struct udl_device *udl = dev->dev_private;
  384. struct udl_fbdev *ufbdev;
  385. if (!udl->fbdev)
  386. return;
  387. ufbdev = udl->fbdev;
  388. drm_fb_helper_unlink_fbi(&ufbdev->helper);
  389. }
  390. struct drm_framebuffer *
  391. udl_fb_user_fb_create(struct drm_device *dev,
  392. struct drm_file *file,
  393. const struct drm_mode_fb_cmd2 *mode_cmd)
  394. {
  395. struct drm_gem_object *obj;
  396. struct udl_framebuffer *ufb;
  397. int ret;
  398. uint32_t size;
  399. obj = drm_gem_object_lookup(file, mode_cmd->handles[0]);
  400. if (obj == NULL)
  401. return ERR_PTR(-ENOENT);
  402. size = mode_cmd->pitches[0] * mode_cmd->height;
  403. size = ALIGN(size, PAGE_SIZE);
  404. if (size > obj->size) {
  405. DRM_ERROR("object size not sufficient for fb %d %zu %d %d\n", size, obj->size, mode_cmd->pitches[0], mode_cmd->height);
  406. return ERR_PTR(-ENOMEM);
  407. }
  408. ufb = kzalloc(sizeof(*ufb), GFP_KERNEL);
  409. if (ufb == NULL)
  410. return ERR_PTR(-ENOMEM);
  411. ret = udl_framebuffer_init(dev, ufb, mode_cmd, to_udl_bo(obj));
  412. if (ret) {
  413. kfree(ufb);
  414. return ERR_PTR(-EINVAL);
  415. }
  416. return &ufb->base;
  417. }