udl_fb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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 "drmP.h"
  17. #include "drm.h"
  18. #include "drm_crtc.h"
  19. #include "drm_crtc_helper.h"
  20. #include "udl_drv.h"
  21. #include "drm_fb_helper.h"
  22. #define DL_DEFIO_WRITE_DELAY 5 /* fb_deferred_io.delay in jiffies */
  23. static int fb_defio = 1; /* 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. struct list_head fbdev_list;
  31. int fb_count;
  32. };
  33. #define DL_ALIGN_UP(x, a) ALIGN(x, a)
  34. #define DL_ALIGN_DOWN(x, a) ALIGN(x-(a-1), 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. /*
  64. * NOTE: fb_defio.c is holding info->fbdefio.mutex
  65. * Touching ANY framebuffer memory that triggers a page fault
  66. * in fb_defio will cause a deadlock, when it also tries to
  67. * grab the same mutex.
  68. */
  69. static void udlfb_dpy_deferred_io(struct fb_info *info,
  70. struct list_head *pagelist)
  71. {
  72. struct page *cur;
  73. struct fb_deferred_io *fbdefio = info->fbdefio;
  74. struct udl_fbdev *ufbdev = info->par;
  75. struct drm_device *dev = ufbdev->ufb.base.dev;
  76. struct udl_device *udl = dev->dev_private;
  77. struct urb *urb;
  78. char *cmd;
  79. cycles_t start_cycles, end_cycles;
  80. int bytes_sent = 0;
  81. int bytes_identical = 0;
  82. int bytes_rendered = 0;
  83. if (!fb_defio)
  84. return;
  85. start_cycles = get_cycles();
  86. urb = udl_get_urb(dev);
  87. if (!urb)
  88. return;
  89. cmd = urb->transfer_buffer;
  90. /* walk the written page list and render each to device */
  91. list_for_each_entry(cur, &fbdefio->pagelist, lru) {
  92. if (udl_render_hline(dev, (ufbdev->ufb.base.bits_per_pixel / 8),
  93. &urb, (char *) info->fix.smem_start,
  94. &cmd, cur->index << PAGE_SHIFT,
  95. PAGE_SIZE, &bytes_identical, &bytes_sent))
  96. goto error;
  97. bytes_rendered += PAGE_SIZE;
  98. }
  99. if (cmd > (char *) urb->transfer_buffer) {
  100. /* Send partial buffer remaining before exiting */
  101. int len = cmd - (char *) urb->transfer_buffer;
  102. udl_submit_urb(dev, urb, len);
  103. bytes_sent += len;
  104. } else
  105. udl_urb_completion(urb);
  106. error:
  107. atomic_add(bytes_sent, &udl->bytes_sent);
  108. atomic_add(bytes_identical, &udl->bytes_identical);
  109. atomic_add(bytes_rendered, &udl->bytes_rendered);
  110. end_cycles = get_cycles();
  111. atomic_add(((unsigned int) ((end_cycles - start_cycles)
  112. >> 10)), /* Kcycles */
  113. &udl->cpu_kcycles_used);
  114. }
  115. int udl_handle_damage(struct udl_framebuffer *fb, int x, int y,
  116. int width, int height)
  117. {
  118. struct drm_device *dev = fb->base.dev;
  119. struct udl_device *udl = dev->dev_private;
  120. int i, ret;
  121. char *cmd;
  122. cycles_t start_cycles, end_cycles;
  123. int bytes_sent = 0;
  124. int bytes_identical = 0;
  125. struct urb *urb;
  126. int aligned_x;
  127. int bpp = (fb->base.bits_per_pixel / 8);
  128. if (!fb->active_16)
  129. return 0;
  130. if (!fb->obj->vmapping)
  131. udl_gem_vmap(fb->obj);
  132. start_cycles = get_cycles();
  133. aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
  134. width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
  135. x = aligned_x;
  136. if ((width <= 0) ||
  137. (x + width > fb->base.width) ||
  138. (y + height > fb->base.height))
  139. return -EINVAL;
  140. urb = udl_get_urb(dev);
  141. if (!urb)
  142. return 0;
  143. cmd = urb->transfer_buffer;
  144. for (i = y; i < y + height ; i++) {
  145. const int line_offset = fb->base.pitches[0] * i;
  146. const int byte_offset = line_offset + (x * bpp);
  147. if (udl_render_hline(dev, bpp, &urb,
  148. (char *) fb->obj->vmapping,
  149. &cmd, byte_offset, width * bpp,
  150. &bytes_identical, &bytes_sent))
  151. goto error;
  152. }
  153. if (cmd > (char *) urb->transfer_buffer) {
  154. /* Send partial buffer remaining before exiting */
  155. int len = cmd - (char *) urb->transfer_buffer;
  156. ret = udl_submit_urb(dev, urb, len);
  157. bytes_sent += len;
  158. } else
  159. udl_urb_completion(urb);
  160. error:
  161. atomic_add(bytes_sent, &udl->bytes_sent);
  162. atomic_add(bytes_identical, &udl->bytes_identical);
  163. atomic_add(width*height*bpp, &udl->bytes_rendered);
  164. end_cycles = get_cycles();
  165. atomic_add(((unsigned int) ((end_cycles - start_cycles)
  166. >> 10)), /* Kcycles */
  167. &udl->cpu_kcycles_used);
  168. return 0;
  169. }
  170. static int udl_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
  171. {
  172. unsigned long start = vma->vm_start;
  173. unsigned long size = vma->vm_end - vma->vm_start;
  174. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  175. unsigned long page, pos;
  176. if (offset + size > info->fix.smem_len)
  177. return -EINVAL;
  178. pos = (unsigned long)info->fix.smem_start + offset;
  179. pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
  180. pos, size);
  181. while (size > 0) {
  182. page = vmalloc_to_pfn((void *)pos);
  183. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
  184. return -EAGAIN;
  185. start += PAGE_SIZE;
  186. pos += PAGE_SIZE;
  187. if (size > PAGE_SIZE)
  188. size -= PAGE_SIZE;
  189. else
  190. size = 0;
  191. }
  192. vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
  193. return 0;
  194. }
  195. static void udl_fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  196. {
  197. struct udl_fbdev *ufbdev = info->par;
  198. sys_fillrect(info, rect);
  199. udl_handle_damage(&ufbdev->ufb, rect->dx, rect->dy, rect->width,
  200. rect->height);
  201. }
  202. static void udl_fb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
  203. {
  204. struct udl_fbdev *ufbdev = info->par;
  205. sys_copyarea(info, region);
  206. udl_handle_damage(&ufbdev->ufb, region->dx, region->dy, region->width,
  207. region->height);
  208. }
  209. static void udl_fb_imageblit(struct fb_info *info, const struct fb_image *image)
  210. {
  211. struct udl_fbdev *ufbdev = info->par;
  212. sys_imageblit(info, image);
  213. udl_handle_damage(&ufbdev->ufb, image->dx, image->dy, image->width,
  214. image->height);
  215. }
  216. /*
  217. * It's common for several clients to have framebuffer open simultaneously.
  218. * e.g. both fbcon and X. Makes things interesting.
  219. * Assumes caller is holding info->lock (for open and release at least)
  220. */
  221. static int udl_fb_open(struct fb_info *info, int user)
  222. {
  223. struct udl_fbdev *ufbdev = info->par;
  224. struct drm_device *dev = ufbdev->ufb.base.dev;
  225. struct udl_device *udl = dev->dev_private;
  226. /* If the USB device is gone, we don't accept new opens */
  227. if (drm_device_is_unplugged(udl->ddev))
  228. return -ENODEV;
  229. ufbdev->fb_count++;
  230. if (fb_defio && (info->fbdefio == NULL)) {
  231. /* enable defio at last moment if not disabled by client */
  232. struct fb_deferred_io *fbdefio;
  233. fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
  234. if (fbdefio) {
  235. fbdefio->delay = DL_DEFIO_WRITE_DELAY;
  236. fbdefio->deferred_io = udlfb_dpy_deferred_io;
  237. }
  238. info->fbdefio = fbdefio;
  239. fb_deferred_io_init(info);
  240. }
  241. pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
  242. info->node, user, info, ufbdev->fb_count);
  243. return 0;
  244. }
  245. /*
  246. * Assumes caller is holding info->lock mutex (for open and release at least)
  247. */
  248. static int udl_fb_release(struct fb_info *info, int user)
  249. {
  250. struct udl_fbdev *ufbdev = info->par;
  251. ufbdev->fb_count--;
  252. if ((ufbdev->fb_count == 0) && (info->fbdefio)) {
  253. fb_deferred_io_cleanup(info);
  254. kfree(info->fbdefio);
  255. info->fbdefio = NULL;
  256. info->fbops->fb_mmap = udl_fb_mmap;
  257. }
  258. pr_warn("released /dev/fb%d user=%d count=%d\n",
  259. info->node, user, ufbdev->fb_count);
  260. return 0;
  261. }
  262. static struct fb_ops udlfb_ops = {
  263. .owner = THIS_MODULE,
  264. .fb_check_var = drm_fb_helper_check_var,
  265. .fb_set_par = drm_fb_helper_set_par,
  266. .fb_fillrect = udl_fb_fillrect,
  267. .fb_copyarea = udl_fb_copyarea,
  268. .fb_imageblit = udl_fb_imageblit,
  269. .fb_pan_display = drm_fb_helper_pan_display,
  270. .fb_blank = drm_fb_helper_blank,
  271. .fb_setcmap = drm_fb_helper_setcmap,
  272. .fb_debug_enter = drm_fb_helper_debug_enter,
  273. .fb_debug_leave = drm_fb_helper_debug_leave,
  274. .fb_mmap = udl_fb_mmap,
  275. .fb_open = udl_fb_open,
  276. .fb_release = udl_fb_release,
  277. };
  278. void udl_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  279. u16 blue, int regno)
  280. {
  281. }
  282. void udl_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  283. u16 *blue, int regno)
  284. {
  285. *red = 0;
  286. *green = 0;
  287. *blue = 0;
  288. }
  289. static int udl_user_framebuffer_dirty(struct drm_framebuffer *fb,
  290. struct drm_file *file,
  291. unsigned flags, unsigned color,
  292. struct drm_clip_rect *clips,
  293. unsigned num_clips)
  294. {
  295. struct udl_framebuffer *ufb = to_udl_fb(fb);
  296. int i;
  297. if (!ufb->active_16)
  298. return 0;
  299. for (i = 0; i < num_clips; i++) {
  300. udl_handle_damage(ufb, clips[i].x1, clips[i].y1,
  301. clips[i].x2 - clips[i].x1,
  302. clips[i].y2 - clips[i].y1);
  303. }
  304. return 0;
  305. }
  306. static void udl_user_framebuffer_destroy(struct drm_framebuffer *fb)
  307. {
  308. struct udl_framebuffer *ufb = to_udl_fb(fb);
  309. if (ufb->obj)
  310. drm_gem_object_unreference_unlocked(&ufb->obj->base);
  311. drm_framebuffer_cleanup(fb);
  312. kfree(ufb);
  313. }
  314. static const struct drm_framebuffer_funcs udlfb_funcs = {
  315. .destroy = udl_user_framebuffer_destroy,
  316. .dirty = udl_user_framebuffer_dirty,
  317. .create_handle = NULL,
  318. };
  319. static int
  320. udl_framebuffer_init(struct drm_device *dev,
  321. struct udl_framebuffer *ufb,
  322. struct drm_mode_fb_cmd2 *mode_cmd,
  323. struct udl_gem_object *obj)
  324. {
  325. int ret;
  326. ufb->obj = obj;
  327. ret = drm_framebuffer_init(dev, &ufb->base, &udlfb_funcs);
  328. drm_helper_mode_fill_fb_struct(&ufb->base, mode_cmd);
  329. return ret;
  330. }
  331. static int udlfb_create(struct udl_fbdev *ufbdev,
  332. struct drm_fb_helper_surface_size *sizes)
  333. {
  334. struct drm_device *dev = ufbdev->helper.dev;
  335. struct fb_info *info;
  336. struct device *device = &dev->usbdev->dev;
  337. struct drm_framebuffer *fb;
  338. struct drm_mode_fb_cmd2 mode_cmd;
  339. struct udl_gem_object *obj;
  340. uint32_t size;
  341. int ret = 0;
  342. if (sizes->surface_bpp == 24)
  343. sizes->surface_bpp = 32;
  344. mode_cmd.width = sizes->surface_width;
  345. mode_cmd.height = sizes->surface_height;
  346. mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
  347. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  348. sizes->surface_depth);
  349. size = mode_cmd.pitches[0] * mode_cmd.height;
  350. size = ALIGN(size, PAGE_SIZE);
  351. obj = udl_gem_alloc_object(dev, size);
  352. if (!obj)
  353. goto out;
  354. ret = udl_gem_vmap(obj);
  355. if (ret) {
  356. DRM_ERROR("failed to vmap fb\n");
  357. goto out_gfree;
  358. }
  359. info = framebuffer_alloc(0, device);
  360. if (!info) {
  361. ret = -ENOMEM;
  362. goto out_gfree;
  363. }
  364. info->par = ufbdev;
  365. ret = udl_framebuffer_init(dev, &ufbdev->ufb, &mode_cmd, obj);
  366. if (ret)
  367. goto out_gfree;
  368. fb = &ufbdev->ufb.base;
  369. ufbdev->helper.fb = fb;
  370. ufbdev->helper.fbdev = info;
  371. strcpy(info->fix.id, "udldrmfb");
  372. info->screen_base = ufbdev->ufb.obj->vmapping;
  373. info->fix.smem_len = size;
  374. info->fix.smem_start = (unsigned long)ufbdev->ufb.obj->vmapping;
  375. info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  376. info->fbops = &udlfb_ops;
  377. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  378. drm_fb_helper_fill_var(info, &ufbdev->helper, sizes->fb_width, sizes->fb_height);
  379. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  380. if (ret) {
  381. ret = -ENOMEM;
  382. goto out_gfree;
  383. }
  384. DRM_DEBUG_KMS("allocated %dx%d vmal %p\n",
  385. fb->width, fb->height,
  386. ufbdev->ufb.obj->vmapping);
  387. return ret;
  388. out_gfree:
  389. drm_gem_object_unreference(&ufbdev->ufb.obj->base);
  390. out:
  391. return ret;
  392. }
  393. static int udl_fb_find_or_create_single(struct drm_fb_helper *helper,
  394. struct drm_fb_helper_surface_size *sizes)
  395. {
  396. struct udl_fbdev *ufbdev = (struct udl_fbdev *)helper;
  397. int new_fb = 0;
  398. int ret;
  399. if (!helper->fb) {
  400. ret = udlfb_create(ufbdev, sizes);
  401. if (ret)
  402. return ret;
  403. new_fb = 1;
  404. }
  405. return new_fb;
  406. }
  407. static struct drm_fb_helper_funcs udl_fb_helper_funcs = {
  408. .gamma_set = udl_crtc_fb_gamma_set,
  409. .gamma_get = udl_crtc_fb_gamma_get,
  410. .fb_probe = udl_fb_find_or_create_single,
  411. };
  412. static void udl_fbdev_destroy(struct drm_device *dev,
  413. struct udl_fbdev *ufbdev)
  414. {
  415. struct fb_info *info;
  416. if (ufbdev->helper.fbdev) {
  417. info = ufbdev->helper.fbdev;
  418. unregister_framebuffer(info);
  419. if (info->cmap.len)
  420. fb_dealloc_cmap(&info->cmap);
  421. framebuffer_release(info);
  422. }
  423. drm_fb_helper_fini(&ufbdev->helper);
  424. drm_framebuffer_cleanup(&ufbdev->ufb.base);
  425. drm_gem_object_unreference_unlocked(&ufbdev->ufb.obj->base);
  426. }
  427. int udl_fbdev_init(struct drm_device *dev)
  428. {
  429. struct udl_device *udl = dev->dev_private;
  430. int bpp_sel = fb_bpp;
  431. struct udl_fbdev *ufbdev;
  432. int ret;
  433. ufbdev = kzalloc(sizeof(struct udl_fbdev), GFP_KERNEL);
  434. if (!ufbdev)
  435. return -ENOMEM;
  436. udl->fbdev = ufbdev;
  437. ufbdev->helper.funcs = &udl_fb_helper_funcs;
  438. ret = drm_fb_helper_init(dev, &ufbdev->helper,
  439. 1, 1);
  440. if (ret) {
  441. kfree(ufbdev);
  442. return ret;
  443. }
  444. drm_fb_helper_single_add_all_connectors(&ufbdev->helper);
  445. drm_fb_helper_initial_config(&ufbdev->helper, bpp_sel);
  446. return 0;
  447. }
  448. void udl_fbdev_cleanup(struct drm_device *dev)
  449. {
  450. struct udl_device *udl = dev->dev_private;
  451. if (!udl->fbdev)
  452. return;
  453. udl_fbdev_destroy(dev, udl->fbdev);
  454. kfree(udl->fbdev);
  455. udl->fbdev = NULL;
  456. }
  457. void udl_fbdev_unplug(struct drm_device *dev)
  458. {
  459. struct udl_device *udl = dev->dev_private;
  460. struct udl_fbdev *ufbdev;
  461. if (!udl->fbdev)
  462. return;
  463. ufbdev = udl->fbdev;
  464. if (ufbdev->helper.fbdev) {
  465. struct fb_info *info;
  466. info = ufbdev->helper.fbdev;
  467. unlink_framebuffer(info);
  468. }
  469. }
  470. struct drm_framebuffer *
  471. udl_fb_user_fb_create(struct drm_device *dev,
  472. struct drm_file *file,
  473. struct drm_mode_fb_cmd2 *mode_cmd)
  474. {
  475. struct drm_gem_object *obj;
  476. struct udl_framebuffer *ufb;
  477. int ret;
  478. obj = drm_gem_object_lookup(dev, file, mode_cmd->handles[0]);
  479. if (obj == NULL)
  480. return ERR_PTR(-ENOENT);
  481. ufb = kzalloc(sizeof(*ufb), GFP_KERNEL);
  482. if (ufb == NULL)
  483. return ERR_PTR(-ENOMEM);
  484. ret = udl_framebuffer_init(dev, ufb, mode_cmd, to_udl_bo(obj));
  485. if (ret) {
  486. kfree(ufb);
  487. return ERR_PTR(-EINVAL);
  488. }
  489. return &ufb->base;
  490. }