udl_fb.c 13 KB

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