950-0067-fbdev-add-FBIOCOPYAREA-ioctl.patch 9.3 KB

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