1 From 4f3938a0decb1d939978df44ac060deaa886b7c1 Mon Sep 17 00:00:00 2001
2 From: Siarhei Siamashka <siarhei.siamashka@gmail.com>
3 Date: Mon, 17 Jun 2013 13:32:11 +0300
4 Subject: [PATCH 059/725] fbdev: add FBIOCOPYAREA ioctl
6 Based on the patch authored by Ali Gholami Rudi at
7 https://lkml.org/lkml/2009/7/13/153
9 Provide an ioctl for userspace applications, but only if this operation
10 is hardware accelerated (otherwide it does not make any sense).
12 Signed-off-by: Siarhei Siamashka <siarhei.siamashka@gmail.com>
14 bcm2708_fb: Add ioctl for reading gpu memory through dma
16 drivers/video/fbdev/bcm2708_fb.c | 119 ++++++++++++++++++++++++++++++-
17 drivers/video/fbdev/core/fbmem.c | 36 ++++++++++
18 include/uapi/linux/fb.h | 12 ++++
19 3 files changed, 166 insertions(+), 1 deletion(-)
21 --- a/drivers/video/fbdev/bcm2708_fb.c
22 +++ b/drivers/video/fbdev/bcm2708_fb.c
24 #include <linux/console.h>
25 #include <linux/debugfs.h>
26 #include <asm/sizes.h>
27 +#include <linux/uaccess.h>
29 #include <linux/dma-mapping.h>
30 +#include <linux/cred.h>
31 #include <soc/bcm2835/raspberrypi-firmware.h>
33 //#define BCM2708_FB_DEBUG
34 @@ -95,6 +97,7 @@ struct bcm2708_fb {
35 wait_queue_head_t dma_waitq;
36 struct bcm2708_fb_stats stats;
37 unsigned long fb_bus_address;
38 + struct { u32 base, length; } gpu;
41 #define to_bcm2708(info) container_of(info, struct bcm2708_fb, fb)
42 @@ -439,7 +442,118 @@ static int bcm2708_fb_pan_display(struct
46 -static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
47 +static void dma_memcpy(struct bcm2708_fb *fb, dma_addr_t dst, dma_addr_t src,
50 + int burst_size = (fb->dma_chan == 0) ? 8 : 2;
51 + struct bcm2708_dma_cb *cb = fb->cb_base;
53 + cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
54 + BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
64 + if (size < dma_busy_wait_threshold) {
65 + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
66 + bcm_dma_wait_idle(fb->dma_chan_base);
68 + void __iomem *dma_chan = fb->dma_chan_base;
70 + cb->info |= BCM2708_DMA_INT_EN;
71 + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
72 + while (bcm_dma_is_busy(dma_chan)) {
73 + wait_event_interruptible(
75 + !bcm_dma_is_busy(dma_chan));
77 + fb->stats.dma_irqs++;
79 + fb->stats.dma_copies++;
82 +/* address with no aliases */
83 +#define INTALIAS_NORMAL(x) ((x)&~0xc0000000)
84 +/* cache coherent but non-allocating in L1 and L2 */
85 +#define INTALIAS_L1L2_NONALLOCATING(x) (((x)&~0xc0000000)|0x80000000)
87 +static long vc_mem_copy(struct bcm2708_fb *fb, unsigned long arg)
89 + struct fb_dmacopy ioparam;
90 + size_t size = PAGE_SIZE;
92 + dma_addr_t bus_addr;
96 + /* restrict this to root user */
97 + if (!uid_eq(current_euid(), GLOBAL_ROOT_UID)) {
102 + /* Get the parameter data.
105 + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
106 + pr_err("[%s]: failed to copy-from-user\n",
112 + if (fb->gpu.base == 0 || fb->gpu.length == 0) {
113 + pr_err("[%s]: Unable to determine gpu memory (%x,%x)\n",
114 + __func__, fb->gpu.base, fb->gpu.length);
118 + if (INTALIAS_NORMAL(ioparam.src) < fb->gpu.base ||
119 + INTALIAS_NORMAL(ioparam.src) >= fb->gpu.base + fb->gpu.length) {
120 + pr_err("[%s]: Invalid memory access %x (%x-%x)", __func__,
121 + INTALIAS_NORMAL(ioparam.src), fb->gpu.base,
122 + fb->gpu.base + fb->gpu.length);
126 + buf = dma_alloc_coherent(fb->fb.device, PAGE_ALIGN(size), &bus_addr,
129 + pr_err("[%s]: failed to dma_alloc_coherent(%d)\n",
135 + for (offset = 0; offset < ioparam.length; offset += size) {
136 + size_t remaining = ioparam.length - offset;
137 + size_t s = min(size, remaining);
138 + unsigned char *p = (unsigned char *)ioparam.src + offset;
139 + unsigned char *q = (unsigned char *)ioparam.dst + offset;
141 + dma_memcpy(fb, bus_addr,
142 + INTALIAS_L1L2_NONALLOCATING((dma_addr_t)p), size);
143 + if (copy_to_user(q, buf, s) != 0) {
144 + pr_err("[%s]: failed to copy-to-user\n",
152 + dma_free_coherent(fb->fb.device, PAGE_ALIGN(size), buf,
157 +static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd,
160 struct bcm2708_fb *fb = to_bcm2708(info);
162 @@ -451,6 +565,9 @@ static int bcm2708_ioctl(struct fb_info
163 RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC,
164 &dummy, sizeof(dummy));
167 + ret = vc_mem_copy(fb, arg);
170 dev_dbg(info->device, "Unknown ioctl 0x%x\n", cmd);
172 --- a/drivers/video/fbdev/core/fbmem.c
173 +++ b/drivers/video/fbdev/core/fbmem.c
174 @@ -1081,6 +1081,31 @@ fb_blank(struct fb_info *info, int blank
176 EXPORT_SYMBOL(fb_blank);
178 +static int fb_copyarea_user(struct fb_info *info,
179 + struct fb_copyarea *copy)
182 + if (!lock_fb_info(info))
184 + if (copy->dx >= info->var.xres ||
185 + copy->sx >= info->var.xres ||
186 + copy->width > info->var.xres ||
187 + copy->dy >= info->var.yres ||
188 + copy->sy >= info->var.yres ||
189 + copy->height > info->var.yres ||
190 + copy->dx + copy->width > info->var.xres ||
191 + copy->sx + copy->width > info->var.xres ||
192 + copy->dy + copy->height > info->var.yres ||
193 + copy->sy + copy->height > info->var.yres) {
197 + info->fbops->fb_copyarea(info, copy);
199 + unlock_fb_info(info);
203 static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
206 @@ -1091,6 +1116,7 @@ static long do_fb_ioctl(struct fb_info *
207 struct fb_cmap cmap_from;
208 struct fb_cmap_user cmap;
209 struct fb_event event;
210 + struct fb_copyarea copy;
211 void __user *argp = (void __user *)arg;
214 @@ -1208,6 +1234,15 @@ static long do_fb_ioctl(struct fb_info *
215 unlock_fb_info(info);
219 + if (info->flags & FBINFO_HWACCEL_COPYAREA) {
220 + /* only provide this ioctl if it is accelerated */
221 + if (copy_from_user(©, argp, sizeof(copy)))
223 + ret = fb_copyarea_user(info, ©);
228 if (!lock_fb_info(info))
230 @@ -1353,6 +1388,7 @@ static long fb_compat_ioctl(struct file
231 case FBIOPAN_DISPLAY:
232 case FBIOGET_CON2FBMAP:
233 case FBIOPUT_CON2FBMAP:
235 arg = (unsigned long) compat_ptr(arg);
238 --- a/include/uapi/linux/fb.h
239 +++ b/include/uapi/linux/fb.h
241 #define FBIOPUT_MODEINFO 0x4617
242 #define FBIOGET_DISPINFO 0x4618
243 #define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32)
245 + * HACK: use 'z' in order not to clash with any other ioctl numbers which might
246 + * be concurrently added to the mainline kernel
248 +#define FBIOCOPYAREA _IOW('z', 0x21, struct fb_copyarea)
249 +#define FBIODMACOPY _IOW('z', 0x22, struct fb_dmacopy)
251 #define FB_TYPE_PACKED_PIXELS 0 /* Packed Pixels */
252 #define FB_TYPE_PLANES 1 /* Non interleaved planes */
253 @@ -347,6 +353,12 @@ struct fb_copyarea {
264 __u32 dx; /* screen-relative */