950-0056-fbdev-add-FBIOCOPYAREA-ioctl.patch 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. From d56622a9c76ab913da66bb22c3158b10f9fb0543 Mon Sep 17 00:00:00 2001
  2. From: Siarhei Siamashka <[email protected]>
  3. Date: Mon, 17 Jun 2013 13:32:11 +0300
  4. Subject: [PATCH 056/454] fbdev: add FBIOCOPYAREA ioctl
  5. Based on the patch authored by Ali Gholami Rudi at
  6. https://lkml.org/lkml/2009/7/13/153
  7. Provide an ioctl for userspace applications, but only if this operation
  8. is hardware accelerated (otherwide it does not make any sense).
  9. Signed-off-by: Siarhei Siamashka <[email protected]>
  10. bcm2708_fb: Add ioctl for reading gpu memory through dma
  11. ---
  12. drivers/video/fbdev/bcm2708_fb.c | 111 +++++++++++++++++++++++++++++++
  13. drivers/video/fbdev/core/fbmem.c | 36 ++++++++++
  14. include/uapi/linux/fb.h | 12 ++++
  15. 3 files changed, 159 insertions(+)
  16. --- a/drivers/video/fbdev/bcm2708_fb.c
  17. +++ b/drivers/video/fbdev/bcm2708_fb.c
  18. @@ -31,8 +31,10 @@
  19. #include <linux/console.h>
  20. #include <linux/debugfs.h>
  21. #include <asm/sizes.h>
  22. +#include <linux/uaccess.h>
  23. #include <linux/io.h>
  24. #include <linux/dma-mapping.h>
  25. +#include <linux/cred.h>
  26. #include <soc/bcm2835/raspberrypi-firmware.h>
  27. //#define BCM2708_FB_DEBUG
  28. @@ -94,6 +96,7 @@ struct bcm2708_fb {
  29. wait_queue_head_t dma_waitq;
  30. struct bcm2708_fb_stats stats;
  31. unsigned long fb_bus_address;
  32. + struct { u32 base, length; } gpu;
  33. };
  34. #define to_bcm2708(info) container_of(info, struct bcm2708_fb, fb)
  35. @@ -426,6 +429,106 @@ static int bcm2708_fb_pan_display(struct
  36. return result;
  37. }
  38. +static void dma_memcpy(struct bcm2708_fb *fb, dma_addr_t dst, dma_addr_t src, int size)
  39. +{
  40. + int burst_size = (fb->dma_chan == 0) ? 8 : 2;
  41. + struct bcm2708_dma_cb *cb = fb->cb_base;
  42. +
  43. + cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
  44. + BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
  45. + BCM2708_DMA_D_INC;
  46. + cb->dst = dst;
  47. + cb->src = src;
  48. + cb->length = size;
  49. + cb->stride = 0;
  50. + cb->pad[0] = 0;
  51. + cb->pad[1] = 0;
  52. + cb->next = 0;
  53. +
  54. + if (size < dma_busy_wait_threshold) {
  55. + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
  56. + bcm_dma_wait_idle(fb->dma_chan_base);
  57. + } else {
  58. + void __iomem *dma_chan = fb->dma_chan_base;
  59. + cb->info |= BCM2708_DMA_INT_EN;
  60. + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
  61. + while (bcm_dma_is_busy(dma_chan)) {
  62. + wait_event_interruptible(
  63. + fb->dma_waitq,
  64. + !bcm_dma_is_busy(dma_chan));
  65. + }
  66. + fb->stats.dma_irqs++;
  67. + }
  68. + fb->stats.dma_copies++;
  69. +}
  70. +
  71. +#define INTALIAS_NORMAL(x) ((x)&~0xc0000000) // address with no aliases
  72. +#define INTALIAS_L1L2_NONALLOCATING(x) (((x)&~0xc0000000)|0x80000000) // cache coherent but non-allocating in L1 and L2
  73. +
  74. +static long vc_mem_copy(struct bcm2708_fb *fb, unsigned long arg)
  75. +{
  76. + struct fb_dmacopy ioparam;
  77. + size_t size = PAGE_SIZE;
  78. + u32 *buf = NULL;
  79. + dma_addr_t bus_addr;
  80. + long rc = 0;
  81. + size_t offset;
  82. +
  83. + /* restrict this to root user */
  84. + if (!uid_eq(current_euid(), GLOBAL_ROOT_UID))
  85. + {
  86. + rc = -EFAULT;
  87. + goto out;
  88. + }
  89. +
  90. + /* Get the parameter data.
  91. + */
  92. + if (copy_from_user
  93. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  94. + pr_err("[%s]: failed to copy-from-user\n",
  95. + __func__);
  96. + rc = -EFAULT;
  97. + goto out;
  98. + }
  99. +
  100. + if (fb->gpu.base == 0 || fb->gpu.length == 0) {
  101. + pr_err("[%s]: Unable to determine gpu memory (%x,%x)\n", __func__, fb->gpu.base, fb->gpu.length);
  102. + return -EFAULT;
  103. + }
  104. +
  105. + if (INTALIAS_NORMAL(ioparam.src) < fb->gpu.base || INTALIAS_NORMAL(ioparam.src) >= fb->gpu.base + fb->gpu.length) {
  106. + pr_err("[%s]: Invalid memory access %x (%x-%x)", __func__, INTALIAS_NORMAL(ioparam.src), fb->gpu.base, fb->gpu.base + fb->gpu.length);
  107. + return -EFAULT;
  108. + }
  109. +
  110. + buf = dma_alloc_coherent(fb->fb.device, PAGE_ALIGN(size), &bus_addr,
  111. + GFP_ATOMIC);
  112. + if (!buf) {
  113. + pr_err("[%s]: failed to dma_alloc_coherent(%d)\n",
  114. + __func__, size);
  115. + rc = -ENOMEM;
  116. + goto out;
  117. + }
  118. +
  119. + for (offset = 0; offset < ioparam.length; offset += size) {
  120. + size_t remaining = ioparam.length - offset;
  121. + size_t s = min(size, remaining);
  122. + unsigned char *p = (unsigned char *)ioparam.src + offset;
  123. + unsigned char *q = (unsigned char *)ioparam.dst + offset;
  124. + dma_memcpy(fb, bus_addr, INTALIAS_L1L2_NONALLOCATING((dma_addr_t)p), size);
  125. + if (copy_to_user(q, buf, s) != 0) {
  126. + pr_err("[%s]: failed to copy-to-user\n",
  127. + __func__);
  128. + rc = -EFAULT;
  129. + goto out;
  130. + }
  131. + }
  132. +out:
  133. + if (buf)
  134. + dma_free_coherent(fb->fb.device, PAGE_ALIGN(size), buf, bus_addr);
  135. + return rc;
  136. +}
  137. +
  138. static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
  139. {
  140. struct bcm2708_fb *fb = to_bcm2708(info);
  141. @@ -438,6 +541,9 @@ static int bcm2708_ioctl(struct fb_info
  142. RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC,
  143. &dummy, sizeof(dummy));
  144. break;
  145. + case FBIODMACOPY:
  146. + ret = vc_mem_copy(fb, arg);
  147. + break;
  148. default:
  149. dev_dbg(info->device, "Unknown ioctl 0x%x\n", cmd);
  150. return -ENOTTY;
  151. @@ -760,6 +866,11 @@ static int bcm2708_fb_probe(struct platf
  152. fb->dev = dev;
  153. fb->fb.device = &dev->dev;
  154. + // failure here isn't fatal, but we'll fail in vc_mem_copy if fb->gpu is not valid
  155. + rpi_firmware_property(fb->fw,
  156. + RPI_FIRMWARE_GET_VC_MEMORY,
  157. + &fb->gpu, sizeof(fb->gpu));
  158. +
  159. ret = bcm2708_fb_register(fb);
  160. if (ret == 0) {
  161. platform_set_drvdata(dev, fb);
  162. --- a/drivers/video/fbdev/core/fbmem.c
  163. +++ b/drivers/video/fbdev/core/fbmem.c
  164. @@ -1093,6 +1093,31 @@ fb_blank(struct fb_info *info, int blank
  165. }
  166. EXPORT_SYMBOL(fb_blank);
  167. +static int fb_copyarea_user(struct fb_info *info,
  168. + struct fb_copyarea *copy)
  169. +{
  170. + int ret = 0;
  171. + if (!lock_fb_info(info))
  172. + return -ENODEV;
  173. + if (copy->dx >= info->var.xres ||
  174. + copy->sx >= info->var.xres ||
  175. + copy->width > info->var.xres ||
  176. + copy->dy >= info->var.yres ||
  177. + copy->sy >= info->var.yres ||
  178. + copy->height > info->var.yres ||
  179. + copy->dx + copy->width > info->var.xres ||
  180. + copy->sx + copy->width > info->var.xres ||
  181. + copy->dy + copy->height > info->var.yres ||
  182. + copy->sy + copy->height > info->var.yres) {
  183. + ret = -EINVAL;
  184. + goto out;
  185. + }
  186. + info->fbops->fb_copyarea(info, copy);
  187. +out:
  188. + unlock_fb_info(info);
  189. + return ret;
  190. +}
  191. +
  192. static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
  193. unsigned long arg)
  194. {
  195. @@ -1103,6 +1128,7 @@ static long do_fb_ioctl(struct fb_info *
  196. struct fb_cmap cmap_from;
  197. struct fb_cmap_user cmap;
  198. struct fb_event event;
  199. + struct fb_copyarea copy;
  200. void __user *argp = (void __user *)arg;
  201. long ret = 0;
  202. @@ -1220,6 +1246,15 @@ static long do_fb_ioctl(struct fb_info *
  203. unlock_fb_info(info);
  204. console_unlock();
  205. break;
  206. + case FBIOCOPYAREA:
  207. + if (info->flags & FBINFO_HWACCEL_COPYAREA) {
  208. + /* only provide this ioctl if it is accelerated */
  209. + if (copy_from_user(&copy, argp, sizeof(copy)))
  210. + return -EFAULT;
  211. + ret = fb_copyarea_user(info, &copy);
  212. + break;
  213. + }
  214. + /* fall through */
  215. default:
  216. if (!lock_fb_info(info))
  217. return -ENODEV;
  218. @@ -1365,6 +1400,7 @@ static long fb_compat_ioctl(struct file
  219. case FBIOPAN_DISPLAY:
  220. case FBIOGET_CON2FBMAP:
  221. case FBIOPUT_CON2FBMAP:
  222. + case FBIOCOPYAREA:
  223. arg = (unsigned long) compat_ptr(arg);
  224. case FBIOBLANK:
  225. ret = do_fb_ioctl(info, cmd, arg);
  226. --- a/include/uapi/linux/fb.h
  227. +++ b/include/uapi/linux/fb.h
  228. @@ -35,6 +35,12 @@
  229. #define FBIOPUT_MODEINFO 0x4617
  230. #define FBIOGET_DISPINFO 0x4618
  231. #define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32)
  232. +/*
  233. + * HACK: use 'z' in order not to clash with any other ioctl numbers which might
  234. + * be concurrently added to the mainline kernel
  235. + */
  236. +#define FBIOCOPYAREA _IOW('z', 0x21, struct fb_copyarea)
  237. +#define FBIODMACOPY _IOW('z', 0x22, struct fb_dmacopy)
  238. #define FB_TYPE_PACKED_PIXELS 0 /* Packed Pixels */
  239. #define FB_TYPE_PLANES 1 /* Non interleaved planes */
  240. @@ -347,6 +353,12 @@ struct fb_copyarea {
  241. __u32 sy;
  242. };
  243. +struct fb_dmacopy {
  244. + void *dst;
  245. + __u32 src;
  246. + __u32 length;
  247. +};
  248. +
  249. struct fb_fillrect {
  250. __u32 dx; /* screen-relative */
  251. __u32 dy;