udl_fb.c 13 KB

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