950-0040-fbdev-add-FBIOCOPYAREA-ioctl.patch 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. From 764b96cc27c293fb37a8b9031ddb25290974e3a2 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] 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. video: bcm2708_fb: Add compat_ioctl support.
  12. When using a 64 bit kernel with 32 bit userspace we need
  13. compat ioctl handling for FBIODMACOPY as one of the
  14. parameters is a pointer.
  15. Signed-off-by: Dave Stevenson <[email protected]>
  16. ---
  17. drivers/video/fbdev/bcm2708_fb.c | 167 ++++++++++++++++++++++++++++++-
  18. drivers/video/fbdev/core/fbmem.c | 35 +++++++
  19. include/uapi/linux/fb.h | 12 +++
  20. 3 files changed, 213 insertions(+), 1 deletion(-)
  21. --- a/drivers/video/fbdev/bcm2708_fb.c
  22. +++ b/drivers/video/fbdev/bcm2708_fb.c
  23. @@ -32,8 +32,10 @@
  24. #include <linux/printk.h>
  25. #include <linux/console.h>
  26. #include <linux/debugfs.h>
  27. +#include <linux/uaccess.h>
  28. #include <linux/io.h>
  29. #include <linux/dma-mapping.h>
  30. +#include <linux/cred.h>
  31. #include <soc/bcm2835/raspberrypi-firmware.h>
  32. #include <linux/mutex.h>
  33. @@ -613,7 +615,110 @@ static int bcm2708_fb_pan_display(struct
  34. return result;
  35. }
  36. -static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
  37. +static void dma_memcpy(struct bcm2708_fb *fb, dma_addr_t dst, dma_addr_t src,
  38. + int size)
  39. +{
  40. + struct bcm2708_fb_dev *fbdev = fb->fbdev;
  41. + struct bcm2708_dma_cb *cb = fbdev->cb_base;
  42. + int burst_size = (fbdev->dma_chan == 0) ? 8 : 2;
  43. +
  44. + cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
  45. + BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
  46. + BCM2708_DMA_D_INC;
  47. + cb->dst = dst;
  48. + cb->src = src;
  49. + cb->length = size;
  50. + cb->stride = 0;
  51. + cb->pad[0] = 0;
  52. + cb->pad[1] = 0;
  53. + cb->next = 0;
  54. +
  55. + // Not sure what to do if this gets a signal whilst waiting
  56. + if (mutex_lock_interruptible(&fbdev->dma_mutex))
  57. + return;
  58. +
  59. + if (size < dma_busy_wait_threshold) {
  60. + bcm_dma_start(fbdev->dma_chan_base, fbdev->cb_handle);
  61. + bcm_dma_wait_idle(fbdev->dma_chan_base);
  62. + } else {
  63. + void __iomem *local_dma_chan = fbdev->dma_chan_base;
  64. +
  65. + cb->info |= BCM2708_DMA_INT_EN;
  66. + bcm_dma_start(fbdev->dma_chan_base, fbdev->cb_handle);
  67. + while (bcm_dma_is_busy(local_dma_chan)) {
  68. + wait_event_interruptible(fbdev->dma_waitq,
  69. + !bcm_dma_is_busy(local_dma_chan));
  70. + }
  71. + fbdev->dma_stats.dma_irqs++;
  72. + }
  73. + fbdev->dma_stats.dma_copies++;
  74. +
  75. + mutex_unlock(&fbdev->dma_mutex);
  76. +}
  77. +
  78. +/* address with no aliases */
  79. +#define INTALIAS_NORMAL(x) ((x) & ~0xc0000000)
  80. +/* cache coherent but non-allocating in L1 and L2 */
  81. +#define INTALIAS_L1L2_NONALLOCATING(x) (((x) & ~0xc0000000) | 0x80000000)
  82. +
  83. +static long vc_mem_copy(struct bcm2708_fb *fb, struct fb_dmacopy *ioparam)
  84. +{
  85. + size_t size = PAGE_SIZE;
  86. + u32 *buf = NULL;
  87. + dma_addr_t bus_addr;
  88. + long rc = 0;
  89. + size_t offset;
  90. +
  91. + /* restrict this to root user */
  92. + if (!uid_eq(current_euid(), GLOBAL_ROOT_UID)) {
  93. + rc = -EFAULT;
  94. + goto out;
  95. + }
  96. +
  97. + if (!fb->gpu.base || !fb->gpu.length) {
  98. + pr_err("[%s]: Unable to determine gpu memory (%x,%x)\n",
  99. + __func__, fb->gpu.base, fb->gpu.length);
  100. + return -EFAULT;
  101. + }
  102. +
  103. + if (INTALIAS_NORMAL(ioparam->src) < fb->gpu.base ||
  104. + INTALIAS_NORMAL(ioparam->src) >= fb->gpu.base + fb->gpu.length) {
  105. + pr_err("[%s]: Invalid memory access %x (%x-%x)", __func__,
  106. + INTALIAS_NORMAL(ioparam->src), fb->gpu.base,
  107. + fb->gpu.base + fb->gpu.length);
  108. + return -EFAULT;
  109. + }
  110. +
  111. + buf = dma_alloc_coherent(fb->fb.device, PAGE_ALIGN(size), &bus_addr,
  112. + GFP_ATOMIC);
  113. + if (!buf) {
  114. + pr_err("[%s]: failed to dma_alloc_coherent(%zd)\n", __func__,
  115. + size);
  116. + rc = -ENOMEM;
  117. + goto out;
  118. + }
  119. +
  120. + for (offset = 0; offset < ioparam->length; offset += size) {
  121. + size_t remaining = ioparam->length - offset;
  122. + size_t s = min(size, remaining);
  123. + u8 *p = (u8 *)((uintptr_t)ioparam->src + offset);
  124. + u8 *q = (u8 *)ioparam->dst + offset;
  125. +
  126. + dma_memcpy(fb, bus_addr,
  127. + INTALIAS_L1L2_NONALLOCATING((dma_addr_t)p), size);
  128. + if (copy_to_user(q, buf, s) != 0) {
  129. + pr_err("[%s]: failed to copy-to-user\n", __func__);
  130. + rc = -EFAULT;
  131. + goto out;
  132. + }
  133. + }
  134. +out:
  135. + if (buf)
  136. + dma_free_coherent(fb->fb.device, PAGE_ALIGN(size), buf,
  137. + bus_addr);
  138. + return rc;
  139. +}
  140. +
  141. static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd,
  142. unsigned long arg)
  143. {
  144. @@ -629,6 +734,21 @@ static int bcm2708_ioctl(struct fb_info
  145. RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC,
  146. &dummy, sizeof(dummy));
  147. break;
  148. +
  149. + case FBIODMACOPY:
  150. + {
  151. + struct fb_dmacopy ioparam;
  152. + /* Get the parameter data.
  153. + */
  154. + if (copy_from_user
  155. + (&ioparam, (void *)arg, sizeof(ioparam))) {
  156. + pr_err("[%s]: failed to copy-from-user\n", __func__);
  157. + ret = -EFAULT;
  158. + break;
  159. + }
  160. + ret = vc_mem_copy(fb, &ioparam);
  161. + break;
  162. + }
  163. default:
  164. dev_dbg(info->device, "Unknown ioctl 0x%x\n", cmd);
  165. return -ENOTTY;
  166. @@ -639,6 +759,48 @@ static int bcm2708_ioctl(struct fb_info
  167. return ret;
  168. }
  169. +
  170. +#ifdef CONFIG_COMPAT
  171. +struct fb_dmacopy32 {
  172. + compat_uptr_t dst;
  173. + __u32 src;
  174. + __u32 length;
  175. +};
  176. +
  177. +#define FBIODMACOPY32 _IOW('z', 0x22, struct fb_dmacopy32)
  178. +
  179. +static int bcm2708_compat_ioctl(struct fb_info *info, unsigned int cmd,
  180. + unsigned long arg)
  181. +{
  182. + struct bcm2708_fb *fb = to_bcm2708(info);
  183. + int ret;
  184. +
  185. + switch (cmd) {
  186. + case FBIODMACOPY32:
  187. + {
  188. + struct fb_dmacopy32 param32;
  189. + struct fb_dmacopy param;
  190. + /* Get the parameter data.
  191. + */
  192. + if (copy_from_user(&param32, (void *)arg, sizeof(param32))) {
  193. + pr_err("[%s]: failed to copy-from-user\n", __func__);
  194. + ret = -EFAULT;
  195. + break;
  196. + }
  197. + param.dst = compat_ptr(param32.dst);
  198. + param.src = param32.src;
  199. + param.length = param32.length;
  200. + ret = vc_mem_copy(fb, &param);
  201. + break;
  202. + }
  203. + default:
  204. + ret = bcm2708_ioctl(info, cmd, arg);
  205. + break;
  206. + }
  207. + return ret;
  208. +}
  209. +#endif
  210. +
  211. static void bcm2708_fb_fillrect(struct fb_info *info,
  212. const struct fb_fillrect *rect)
  213. {
  214. @@ -831,6 +993,9 @@ static struct fb_ops bcm2708_fb_ops = {
  215. .fb_imageblit = bcm2708_fb_imageblit,
  216. .fb_pan_display = bcm2708_fb_pan_display,
  217. .fb_ioctl = bcm2708_ioctl,
  218. +#ifdef CONFIG_COMPAT
  219. + .fb_compat_ioctl = bcm2708_compat_ioctl,
  220. +#endif
  221. };
  222. static int bcm2708_fb_register(struct bcm2708_fb *fb)
  223. --- a/drivers/video/fbdev/core/fbmem.c
  224. +++ b/drivers/video/fbdev/core/fbmem.c
  225. @@ -1076,6 +1076,30 @@ fb_blank(struct fb_info *info, int blank
  226. }
  227. EXPORT_SYMBOL(fb_blank);
  228. +static int fb_copyarea_user(struct fb_info *info,
  229. + struct fb_copyarea *copy)
  230. +{
  231. + int ret = 0;
  232. + lock_fb_info(info);
  233. + if (copy->dx >= info->var.xres ||
  234. + copy->sx >= info->var.xres ||
  235. + copy->width > info->var.xres ||
  236. + copy->dy >= info->var.yres ||
  237. + copy->sy >= info->var.yres ||
  238. + copy->height > info->var.yres ||
  239. + copy->dx + copy->width > info->var.xres ||
  240. + copy->sx + copy->width > info->var.xres ||
  241. + copy->dy + copy->height > info->var.yres ||
  242. + copy->sy + copy->height > info->var.yres) {
  243. + ret = -EINVAL;
  244. + goto out;
  245. + }
  246. + info->fbops->fb_copyarea(info, copy);
  247. +out:
  248. + unlock_fb_info(info);
  249. + return ret;
  250. +}
  251. +
  252. static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
  253. unsigned long arg)
  254. {
  255. @@ -1084,6 +1108,7 @@ static long do_fb_ioctl(struct fb_info *
  256. struct fb_fix_screeninfo fix;
  257. struct fb_cmap cmap_from;
  258. struct fb_cmap_user cmap;
  259. + struct fb_copyarea copy;
  260. void __user *argp = (void __user *)arg;
  261. long ret = 0;
  262. @@ -1159,6 +1184,15 @@ static long do_fb_ioctl(struct fb_info *
  263. unlock_fb_info(info);
  264. console_unlock();
  265. break;
  266. + case FBIOCOPYAREA:
  267. + if (info->flags & FBINFO_HWACCEL_COPYAREA) {
  268. + /* only provide this ioctl if it is accelerated */
  269. + if (copy_from_user(&copy, argp, sizeof(copy)))
  270. + return -EFAULT;
  271. + ret = fb_copyarea_user(info, &copy);
  272. + break;
  273. + }
  274. + /* fall through */
  275. default:
  276. lock_fb_info(info);
  277. fb = info->fbops;
  278. @@ -1304,6 +1338,7 @@ static long fb_compat_ioctl(struct file
  279. case FBIOPAN_DISPLAY:
  280. case FBIOGET_CON2FBMAP:
  281. case FBIOPUT_CON2FBMAP:
  282. + case FBIOCOPYAREA:
  283. arg = (unsigned long) compat_ptr(arg);
  284. /* fall through */
  285. case FBIOBLANK:
  286. --- a/include/uapi/linux/fb.h
  287. +++ b/include/uapi/linux/fb.h
  288. @@ -35,6 +35,12 @@
  289. #define FBIOPUT_MODEINFO 0x4617
  290. #define FBIOGET_DISPINFO 0x4618
  291. #define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32)
  292. +/*
  293. + * HACK: use 'z' in order not to clash with any other ioctl numbers which might
  294. + * be concurrently added to the mainline kernel
  295. + */
  296. +#define FBIOCOPYAREA _IOW('z', 0x21, struct fb_copyarea)
  297. +#define FBIODMACOPY _IOW('z', 0x22, struct fb_dmacopy)
  298. #define FB_TYPE_PACKED_PIXELS 0 /* Packed Pixels */
  299. #define FB_TYPE_PLANES 1 /* Non interleaved planes */
  300. @@ -347,6 +353,12 @@ struct fb_copyarea {
  301. __u32 sy;
  302. };
  303. +struct fb_dmacopy {
  304. + void *dst;
  305. + __u32 src;
  306. + __u32 length;
  307. +};
  308. +
  309. struct fb_fillrect {
  310. __u32 dx; /* screen-relative */
  311. __u32 dy;