1 From 764b96cc27c293fb37a8b9031ddb25290974e3a2 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] 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 video: bcm2708_fb: Add compat_ioctl support.
18 When using a 64 bit kernel with 32 bit userspace we need
19 compat ioctl handling for FBIODMACOPY as one of the
20 parameters is a pointer.
22 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
24 drivers/video/fbdev/bcm2708_fb.c | 167 ++++++++++++++++++++++++++++++-
25 drivers/video/fbdev/core/fbmem.c | 35 +++++++
26 include/uapi/linux/fb.h | 12 +++
27 3 files changed, 213 insertions(+), 1 deletion(-)
29 --- a/drivers/video/fbdev/bcm2708_fb.c
30 +++ b/drivers/video/fbdev/bcm2708_fb.c
32 #include <linux/printk.h>
33 #include <linux/console.h>
34 #include <linux/debugfs.h>
35 +#include <linux/uaccess.h>
37 #include <linux/dma-mapping.h>
38 +#include <linux/cred.h>
39 #include <soc/bcm2835/raspberrypi-firmware.h>
40 #include <linux/mutex.h>
42 @@ -613,7 +615,110 @@ 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 + struct bcm2708_fb_dev *fbdev = fb->fbdev;
51 + struct bcm2708_dma_cb *cb = fbdev->cb_base;
52 + int burst_size = (fbdev->dma_chan == 0) ? 8 : 2;
54 + cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
55 + BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
65 + // Not sure what to do if this gets a signal whilst waiting
66 + if (mutex_lock_interruptible(&fbdev->dma_mutex))
69 + if (size < dma_busy_wait_threshold) {
70 + bcm_dma_start(fbdev->dma_chan_base, fbdev->cb_handle);
71 + bcm_dma_wait_idle(fbdev->dma_chan_base);
73 + void __iomem *local_dma_chan = fbdev->dma_chan_base;
75 + cb->info |= BCM2708_DMA_INT_EN;
76 + bcm_dma_start(fbdev->dma_chan_base, fbdev->cb_handle);
77 + while (bcm_dma_is_busy(local_dma_chan)) {
78 + wait_event_interruptible(fbdev->dma_waitq,
79 + !bcm_dma_is_busy(local_dma_chan));
81 + fbdev->dma_stats.dma_irqs++;
83 + fbdev->dma_stats.dma_copies++;
85 + mutex_unlock(&fbdev->dma_mutex);
88 +/* address with no aliases */
89 +#define INTALIAS_NORMAL(x) ((x) & ~0xc0000000)
90 +/* cache coherent but non-allocating in L1 and L2 */
91 +#define INTALIAS_L1L2_NONALLOCATING(x) (((x) & ~0xc0000000) | 0x80000000)
93 +static long vc_mem_copy(struct bcm2708_fb *fb, struct fb_dmacopy *ioparam)
95 + size_t size = PAGE_SIZE;
97 + dma_addr_t bus_addr;
101 + /* restrict this to root user */
102 + if (!uid_eq(current_euid(), GLOBAL_ROOT_UID)) {
107 + if (!fb->gpu.base || !fb->gpu.length) {
108 + pr_err("[%s]: Unable to determine gpu memory (%x,%x)\n",
109 + __func__, fb->gpu.base, fb->gpu.length);
113 + if (INTALIAS_NORMAL(ioparam->src) < fb->gpu.base ||
114 + INTALIAS_NORMAL(ioparam->src) >= fb->gpu.base + fb->gpu.length) {
115 + pr_err("[%s]: Invalid memory access %x (%x-%x)", __func__,
116 + INTALIAS_NORMAL(ioparam->src), fb->gpu.base,
117 + fb->gpu.base + fb->gpu.length);
121 + buf = dma_alloc_coherent(fb->fb.device, PAGE_ALIGN(size), &bus_addr,
124 + pr_err("[%s]: failed to dma_alloc_coherent(%zd)\n", __func__,
130 + for (offset = 0; offset < ioparam->length; offset += size) {
131 + size_t remaining = ioparam->length - offset;
132 + size_t s = min(size, remaining);
133 + u8 *p = (u8 *)((uintptr_t)ioparam->src + offset);
134 + u8 *q = (u8 *)ioparam->dst + offset;
136 + dma_memcpy(fb, bus_addr,
137 + INTALIAS_L1L2_NONALLOCATING((dma_addr_t)p), size);
138 + if (copy_to_user(q, buf, s) != 0) {
139 + pr_err("[%s]: failed to copy-to-user\n", __func__);
146 + dma_free_coherent(fb->fb.device, PAGE_ALIGN(size), buf,
151 static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd,
154 @@ -629,6 +734,21 @@ static int bcm2708_ioctl(struct fb_info
155 RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC,
156 &dummy, sizeof(dummy));
161 + struct fb_dmacopy ioparam;
162 + /* Get the parameter data.
165 + (&ioparam, (void *)arg, sizeof(ioparam))) {
166 + pr_err("[%s]: failed to copy-from-user\n", __func__);
170 + ret = vc_mem_copy(fb, &ioparam);
174 dev_dbg(info->device, "Unknown ioctl 0x%x\n", cmd);
176 @@ -639,6 +759,48 @@ static int bcm2708_ioctl(struct fb_info
181 +#ifdef CONFIG_COMPAT
182 +struct fb_dmacopy32 {
188 +#define FBIODMACOPY32 _IOW('z', 0x22, struct fb_dmacopy32)
190 +static int bcm2708_compat_ioctl(struct fb_info *info, unsigned int cmd,
193 + struct bcm2708_fb *fb = to_bcm2708(info);
197 + case FBIODMACOPY32:
199 + struct fb_dmacopy32 param32;
200 + struct fb_dmacopy param;
201 + /* Get the parameter data.
203 + if (copy_from_user(¶m32, (void *)arg, sizeof(param32))) {
204 + pr_err("[%s]: failed to copy-from-user\n", __func__);
208 + param.dst = compat_ptr(param32.dst);
209 + param.src = param32.src;
210 + param.length = param32.length;
211 + ret = vc_mem_copy(fb, ¶m);
215 + ret = bcm2708_ioctl(info, cmd, arg);
222 static void bcm2708_fb_fillrect(struct fb_info *info,
223 const struct fb_fillrect *rect)
225 @@ -831,6 +993,9 @@ static struct fb_ops bcm2708_fb_ops = {
226 .fb_imageblit = bcm2708_fb_imageblit,
227 .fb_pan_display = bcm2708_fb_pan_display,
228 .fb_ioctl = bcm2708_ioctl,
229 +#ifdef CONFIG_COMPAT
230 + .fb_compat_ioctl = bcm2708_compat_ioctl,
234 static int bcm2708_fb_register(struct bcm2708_fb *fb)
235 --- a/drivers/video/fbdev/core/fbmem.c
236 +++ b/drivers/video/fbdev/core/fbmem.c
237 @@ -1074,6 +1074,30 @@ fb_blank(struct fb_info *info, int blank
239 EXPORT_SYMBOL(fb_blank);
241 +static int fb_copyarea_user(struct fb_info *info,
242 + struct fb_copyarea *copy)
245 + lock_fb_info(info);
246 + if (copy->dx >= info->var.xres ||
247 + copy->sx >= info->var.xres ||
248 + copy->width > info->var.xres ||
249 + copy->dy >= info->var.yres ||
250 + copy->sy >= info->var.yres ||
251 + copy->height > info->var.yres ||
252 + copy->dx + copy->width > info->var.xres ||
253 + copy->sx + copy->width > info->var.xres ||
254 + copy->dy + copy->height > info->var.yres ||
255 + copy->sy + copy->height > info->var.yres) {
259 + info->fbops->fb_copyarea(info, copy);
261 + unlock_fb_info(info);
265 static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
268 @@ -1082,6 +1106,7 @@ static long do_fb_ioctl(struct fb_info *
269 struct fb_fix_screeninfo fix;
270 struct fb_cmap cmap_from;
271 struct fb_cmap_user cmap;
272 + struct fb_copyarea copy;
273 void __user *argp = (void __user *)arg;
276 @@ -1157,6 +1182,15 @@ static long do_fb_ioctl(struct fb_info *
277 unlock_fb_info(info);
281 + if (info->flags & FBINFO_HWACCEL_COPYAREA) {
282 + /* only provide this ioctl if it is accelerated */
283 + if (copy_from_user(©, argp, sizeof(copy)))
285 + ret = fb_copyarea_user(info, ©);
292 @@ -1302,6 +1336,7 @@ static long fb_compat_ioctl(struct file
293 case FBIOPAN_DISPLAY:
294 case FBIOGET_CON2FBMAP:
295 case FBIOPUT_CON2FBMAP:
297 arg = (unsigned long) compat_ptr(arg);
300 --- a/include/uapi/linux/fb.h
301 +++ b/include/uapi/linux/fb.h
303 #define FBIOPUT_MODEINFO 0x4617
304 #define FBIOGET_DISPINFO 0x4618
305 #define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32)
307 + * HACK: use 'z' in order not to clash with any other ioctl numbers which might
308 + * be concurrently added to the mainline kernel
310 +#define FBIOCOPYAREA _IOW('z', 0x21, struct fb_copyarea)
311 +#define FBIODMACOPY _IOW('z', 0x22, struct fb_dmacopy)
313 #define FB_TYPE_PACKED_PIXELS 0 /* Packed Pixels */
314 #define FB_TYPE_PLANES 1 /* Non interleaved planes */
315 @@ -347,6 +353,12 @@ struct fb_copyarea {
326 __u32 dx; /* screen-relative */