udl_fb.c 13 KB

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