udl_fb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 <linux/mem_encrypt.h>
  18. #include <drm/drmP.h>
  19. #include <drm/drm_crtc.h>
  20. #include <drm/drm_crtc_helper.h>
  21. #include "udl_drv.h"
  22. #include <drm/drm_fb_helper.h>
  23. #define DL_DEFIO_WRITE_DELAY (HZ/20) /* fb_deferred_io.delay in jiffies */
  24. static int fb_defio = 0; /* Optionally enable experimental fb_defio mmap support */
  25. static int fb_bpp = 16;
  26. module_param(fb_bpp, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
  27. module_param(fb_defio, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
  28. struct udl_fbdev {
  29. struct drm_fb_helper helper;
  30. struct udl_framebuffer ufb;
  31. int fb_count;
  32. };
  33. #define DL_ALIGN_UP(x, a) ALIGN(x, a)
  34. #define DL_ALIGN_DOWN(x, a) ALIGN_DOWN(x, 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. int udl_handle_damage(struct udl_framebuffer *fb, int x, int y,
  64. int width, int height)
  65. {
  66. struct drm_device *dev = fb->base.dev;
  67. struct udl_device *udl = dev->dev_private;
  68. int i, ret;
  69. char *cmd;
  70. cycles_t start_cycles, end_cycles;
  71. int bytes_sent = 0;
  72. int bytes_identical = 0;
  73. struct urb *urb;
  74. int aligned_x;
  75. int bpp = fb->base.format->cpp[0];
  76. if (!fb->active_16)
  77. return 0;
  78. if (!fb->obj->vmapping) {
  79. ret = udl_gem_vmap(fb->obj);
  80. if (ret == -ENOMEM) {
  81. DRM_ERROR("failed to vmap fb\n");
  82. return 0;
  83. }
  84. if (!fb->obj->vmapping) {
  85. DRM_ERROR("failed to vmapping\n");
  86. return 0;
  87. }
  88. }
  89. aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
  90. width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
  91. x = aligned_x;
  92. if ((width <= 0) ||
  93. (x + width > fb->base.width) ||
  94. (y + height > fb->base.height))
  95. return -EINVAL;
  96. start_cycles = get_cycles();
  97. urb = udl_get_urb(dev);
  98. if (!urb)
  99. return 0;
  100. cmd = urb->transfer_buffer;
  101. for (i = y; i < y + height ; i++) {
  102. const int line_offset = fb->base.pitches[0] * i;
  103. const int byte_offset = line_offset + (x * bpp);
  104. const int dev_byte_offset = (fb->base.width * bpp * i) + (x * bpp);
  105. if (udl_render_hline(dev, bpp, &urb,
  106. (char *) fb->obj->vmapping,
  107. &cmd, byte_offset, dev_byte_offset,
  108. width * bpp,
  109. &bytes_identical, &bytes_sent))
  110. goto error;
  111. }
  112. if (cmd > (char *) urb->transfer_buffer) {
  113. /* Send partial buffer remaining before exiting */
  114. int len = cmd - (char *) urb->transfer_buffer;
  115. ret = udl_submit_urb(dev, urb, len);
  116. bytes_sent += len;
  117. } else
  118. udl_urb_completion(urb);
  119. error:
  120. atomic_add(bytes_sent, &udl->bytes_sent);
  121. atomic_add(bytes_identical, &udl->bytes_identical);
  122. atomic_add(width*height*bpp, &udl->bytes_rendered);
  123. end_cycles = get_cycles();
  124. atomic_add(((unsigned int) ((end_cycles - start_cycles)
  125. >> 10)), /* Kcycles */
  126. &udl->cpu_kcycles_used);
  127. return 0;
  128. }
  129. static int udl_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
  130. {
  131. unsigned long start = vma->vm_start;
  132. unsigned long size = vma->vm_end - vma->vm_start;
  133. unsigned long offset;
  134. unsigned long page, pos;
  135. if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
  136. return -EINVAL;
  137. offset = vma->vm_pgoff << PAGE_SHIFT;
  138. if (offset > info->fix.smem_len || size > info->fix.smem_len - offset)
  139. return -EINVAL;
  140. pos = (unsigned long)info->fix.smem_start + offset;
  141. pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
  142. pos, size);
  143. /* We don't want the framebuffer to be mapped encrypted */
  144. vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
  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_dev_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. DRM_FB_HELPER_DEFAULT_OPS,
  212. .fb_fillrect = drm_fb_helper_sys_fillrect,
  213. .fb_copyarea = drm_fb_helper_sys_copyarea,
  214. .fb_imageblit = drm_fb_helper_sys_imageblit,
  215. .fb_mmap = udl_fb_mmap,
  216. .fb_open = udl_fb_open,
  217. .fb_release = udl_fb_release,
  218. };
  219. static int udl_user_framebuffer_dirty(struct drm_framebuffer *fb,
  220. struct drm_file *file,
  221. unsigned flags, unsigned color,
  222. struct drm_clip_rect *clips,
  223. unsigned num_clips)
  224. {
  225. struct udl_framebuffer *ufb = to_udl_fb(fb);
  226. int i;
  227. int ret = 0;
  228. drm_modeset_lock_all(fb->dev);
  229. if (!ufb->active_16)
  230. goto unlock;
  231. if (ufb->obj->base.import_attach) {
  232. ret = dma_buf_begin_cpu_access(ufb->obj->base.import_attach->dmabuf,
  233. DMA_FROM_DEVICE);
  234. if (ret)
  235. goto unlock;
  236. }
  237. for (i = 0; i < num_clips; i++) {
  238. ret = udl_handle_damage(ufb, clips[i].x1, clips[i].y1,
  239. clips[i].x2 - clips[i].x1,
  240. clips[i].y2 - clips[i].y1);
  241. if (ret)
  242. break;
  243. }
  244. if (ufb->obj->base.import_attach) {
  245. ret = dma_buf_end_cpu_access(ufb->obj->base.import_attach->dmabuf,
  246. DMA_FROM_DEVICE);
  247. }
  248. unlock:
  249. drm_modeset_unlock_all(fb->dev);
  250. return ret;
  251. }
  252. static void udl_user_framebuffer_destroy(struct drm_framebuffer *fb)
  253. {
  254. struct udl_framebuffer *ufb = to_udl_fb(fb);
  255. if (ufb->obj)
  256. drm_gem_object_put_unlocked(&ufb->obj->base);
  257. drm_framebuffer_cleanup(fb);
  258. kfree(ufb);
  259. }
  260. static const struct drm_framebuffer_funcs udlfb_funcs = {
  261. .destroy = udl_user_framebuffer_destroy,
  262. .dirty = udl_user_framebuffer_dirty,
  263. };
  264. static int
  265. udl_framebuffer_init(struct drm_device *dev,
  266. struct udl_framebuffer *ufb,
  267. const struct drm_mode_fb_cmd2 *mode_cmd,
  268. struct udl_gem_object *obj)
  269. {
  270. int ret;
  271. ufb->obj = obj;
  272. drm_helper_mode_fill_fb_struct(dev, &ufb->base, mode_cmd);
  273. ret = drm_framebuffer_init(dev, &ufb->base, &udlfb_funcs);
  274. return ret;
  275. }
  276. static int udlfb_create(struct drm_fb_helper *helper,
  277. struct drm_fb_helper_surface_size *sizes)
  278. {
  279. struct udl_fbdev *ufbdev =
  280. container_of(helper, struct udl_fbdev, helper);
  281. struct drm_device *dev = ufbdev->helper.dev;
  282. struct fb_info *info;
  283. struct drm_framebuffer *fb;
  284. struct drm_mode_fb_cmd2 mode_cmd;
  285. struct udl_gem_object *obj;
  286. uint32_t size;
  287. int ret = 0;
  288. if (sizes->surface_bpp == 24)
  289. sizes->surface_bpp = 32;
  290. mode_cmd.width = sizes->surface_width;
  291. mode_cmd.height = sizes->surface_height;
  292. mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
  293. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  294. sizes->surface_depth);
  295. size = mode_cmd.pitches[0] * mode_cmd.height;
  296. size = ALIGN(size, PAGE_SIZE);
  297. obj = udl_gem_alloc_object(dev, size);
  298. if (!obj)
  299. goto out;
  300. ret = udl_gem_vmap(obj);
  301. if (ret) {
  302. DRM_ERROR("failed to vmap fb\n");
  303. goto out_gfree;
  304. }
  305. info = drm_fb_helper_alloc_fbi(helper);
  306. if (IS_ERR(info)) {
  307. ret = PTR_ERR(info);
  308. goto out_gfree;
  309. }
  310. info->par = ufbdev;
  311. ret = udl_framebuffer_init(dev, &ufbdev->ufb, &mode_cmd, obj);
  312. if (ret)
  313. goto out_gfree;
  314. fb = &ufbdev->ufb.base;
  315. ufbdev->helper.fb = fb;
  316. strcpy(info->fix.id, "udldrmfb");
  317. info->screen_base = ufbdev->ufb.obj->vmapping;
  318. info->fix.smem_len = size;
  319. info->fix.smem_start = (unsigned long)ufbdev->ufb.obj->vmapping;
  320. info->fbops = &udlfb_ops;
  321. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
  322. drm_fb_helper_fill_var(info, &ufbdev->helper, sizes->fb_width, sizes->fb_height);
  323. DRM_DEBUG_KMS("allocated %dx%d vmal %p\n",
  324. fb->width, fb->height,
  325. ufbdev->ufb.obj->vmapping);
  326. return ret;
  327. out_gfree:
  328. drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
  329. out:
  330. return ret;
  331. }
  332. static const struct drm_fb_helper_funcs udl_fb_helper_funcs = {
  333. .fb_probe = udlfb_create,
  334. };
  335. static void udl_fbdev_destroy(struct drm_device *dev,
  336. struct udl_fbdev *ufbdev)
  337. {
  338. drm_fb_helper_unregister_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_put_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, 1);
  356. if (ret)
  357. goto free;
  358. ret = drm_fb_helper_single_add_all_connectors(&ufbdev->helper);
  359. if (ret)
  360. goto fini;
  361. /* disable all the possible outputs/crtcs before entering KMS mode */
  362. drm_helper_disable_unused_functions(dev);
  363. ret = drm_fb_helper_initial_config(&ufbdev->helper, bpp_sel);
  364. if (ret)
  365. goto fini;
  366. return 0;
  367. fini:
  368. drm_fb_helper_fini(&ufbdev->helper);
  369. free:
  370. kfree(ufbdev);
  371. return ret;
  372. }
  373. void udl_fbdev_cleanup(struct drm_device *dev)
  374. {
  375. struct udl_device *udl = dev->dev_private;
  376. if (!udl->fbdev)
  377. return;
  378. udl_fbdev_destroy(dev, udl->fbdev);
  379. kfree(udl->fbdev);
  380. udl->fbdev = NULL;
  381. }
  382. void udl_fbdev_unplug(struct drm_device *dev)
  383. {
  384. struct udl_device *udl = dev->dev_private;
  385. struct udl_fbdev *ufbdev;
  386. if (!udl->fbdev)
  387. return;
  388. ufbdev = udl->fbdev;
  389. drm_fb_helper_unlink_fbi(&ufbdev->helper);
  390. }
  391. struct drm_framebuffer *
  392. udl_fb_user_fb_create(struct drm_device *dev,
  393. struct drm_file *file,
  394. const struct drm_mode_fb_cmd2 *mode_cmd)
  395. {
  396. struct drm_gem_object *obj;
  397. struct udl_framebuffer *ufb;
  398. int ret;
  399. uint32_t size;
  400. obj = drm_gem_object_lookup(file, mode_cmd->handles[0]);
  401. if (obj == NULL)
  402. return ERR_PTR(-ENOENT);
  403. size = mode_cmd->pitches[0] * mode_cmd->height;
  404. size = ALIGN(size, PAGE_SIZE);
  405. if (size > obj->size) {
  406. DRM_ERROR("object size not sufficient for fb %d %zu %d %d\n", size, obj->size, mode_cmd->pitches[0], mode_cmd->height);
  407. return ERR_PTR(-ENOMEM);
  408. }
  409. ufb = kzalloc(sizeof(*ufb), GFP_KERNEL);
  410. if (ufb == NULL)
  411. return ERR_PTR(-ENOMEM);
  412. ret = udl_framebuffer_init(dev, ufb, mode_cmd, to_udl_bo(obj));
  413. if (ret) {
  414. kfree(ufb);
  415. return ERR_PTR(-EINVAL);
  416. }
  417. return &ufb->base;
  418. }