1 From 32fe1d00f572b4f41260e0ddcf88b15091ec2564 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 | 170 ++++++++++++++++++++++++++++++-
25 drivers/video/fbdev/core/fbmem.c | 35 +++++++
26 include/uapi/linux/fb.h | 12 +++
27 3 files changed, 213 insertions(+), 4 deletions(-)
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 @@ -184,9 +186,6 @@ static int bcm2708_fb_debugfs_init(struc
44 fb->debugfs_subdir = debugfs_create_dir(buf, fb->debugfs_dir);
46 - debugfs_create_regset32("stats", 0444, fb->debugfs_dir,
49 if (!fb->debugfs_subdir) {
50 dev_warn(fb->fb.dev, "%s: could not create debugfs entry %u\n",
51 __func__, fb->display_settings.display_num);
52 @@ -603,7 +602,110 @@ static int bcm2708_fb_pan_display(struct
56 -static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
57 +static void dma_memcpy(struct bcm2708_fb *fb, dma_addr_t dst, dma_addr_t src,
60 + struct bcm2708_fb_dev *fbdev = fb->fbdev;
61 + struct bcm2708_dma_cb *cb = fbdev->cb_base;
62 + int burst_size = (fbdev->dma_chan == 0) ? 8 : 2;
64 + cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
65 + BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
75 + // Not sure what to do if this gets a signal whilst waiting
76 + if (mutex_lock_interruptible(&fbdev->dma_mutex))
79 + if (size < dma_busy_wait_threshold) {
80 + bcm_dma_start(fbdev->dma_chan_base, fbdev->cb_handle);
81 + bcm_dma_wait_idle(fbdev->dma_chan_base);
83 + void __iomem *local_dma_chan = fbdev->dma_chan_base;
85 + cb->info |= BCM2708_DMA_INT_EN;
86 + bcm_dma_start(fbdev->dma_chan_base, fbdev->cb_handle);
87 + while (bcm_dma_is_busy(local_dma_chan)) {
88 + wait_event_interruptible(fbdev->dma_waitq,
89 + !bcm_dma_is_busy(local_dma_chan));
91 + fbdev->dma_stats.dma_irqs++;
93 + fbdev->dma_stats.dma_copies++;
95 + mutex_unlock(&fbdev->dma_mutex);
98 +/* address with no aliases */
99 +#define INTALIAS_NORMAL(x) ((x) & ~0xc0000000)
100 +/* cache coherent but non-allocating in L1 and L2 */
101 +#define INTALIAS_L1L2_NONALLOCATING(x) (((x) & ~0xc0000000) | 0x80000000)
103 +static long vc_mem_copy(struct bcm2708_fb *fb, struct fb_dmacopy *ioparam)
105 + size_t size = PAGE_SIZE;
107 + dma_addr_t bus_addr;
111 + /* restrict this to root user */
112 + if (!uid_eq(current_euid(), GLOBAL_ROOT_UID)) {
117 + if (!fb->gpu.base || !fb->gpu.length) {
118 + pr_err("[%s]: Unable to determine gpu memory (%x,%x)\n",
119 + __func__, fb->gpu.base, fb->gpu.length);
123 + if (INTALIAS_NORMAL(ioparam->src) < fb->gpu.base ||
124 + INTALIAS_NORMAL(ioparam->src) >= fb->gpu.base + fb->gpu.length) {
125 + pr_err("[%s]: Invalid memory access %x (%x-%x)", __func__,
126 + INTALIAS_NORMAL(ioparam->src), fb->gpu.base,
127 + fb->gpu.base + fb->gpu.length);
131 + buf = dma_alloc_coherent(fb->fb.device, PAGE_ALIGN(size), &bus_addr,
134 + pr_err("[%s]: failed to dma_alloc_coherent(%zd)\n", __func__,
140 + for (offset = 0; offset < ioparam->length; offset += size) {
141 + size_t remaining = ioparam->length - offset;
142 + size_t s = min(size, remaining);
143 + u8 *p = (u8 *)((uintptr_t)ioparam->src + offset);
144 + u8 *q = (u8 *)ioparam->dst + offset;
146 + dma_memcpy(fb, bus_addr,
147 + INTALIAS_L1L2_NONALLOCATING((dma_addr_t)p), size);
148 + if (copy_to_user(q, buf, s) != 0) {
149 + pr_err("[%s]: failed to copy-to-user\n", __func__);
156 + dma_free_coherent(fb->fb.device, PAGE_ALIGN(size), buf,
161 static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd,
164 @@ -619,6 +721,21 @@ static int bcm2708_ioctl(struct fb_info
165 RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC,
166 &dummy, sizeof(dummy));
171 + struct fb_dmacopy ioparam;
172 + /* Get the parameter data.
175 + (&ioparam, (void *)arg, sizeof(ioparam))) {
176 + pr_err("[%s]: failed to copy-from-user\n", __func__);
180 + ret = vc_mem_copy(fb, &ioparam);
184 dev_dbg(info->device, "Unknown ioctl 0x%x\n", cmd);
186 @@ -629,6 +746,48 @@ static int bcm2708_ioctl(struct fb_info
191 +#ifdef CONFIG_COMPAT
192 +struct fb_dmacopy32 {
198 +#define FBIODMACOPY32 _IOW('z', 0x22, struct fb_dmacopy32)
200 +static int bcm2708_compat_ioctl(struct fb_info *info, unsigned int cmd,
203 + struct bcm2708_fb *fb = to_bcm2708(info);
207 + case FBIODMACOPY32:
209 + struct fb_dmacopy32 param32;
210 + struct fb_dmacopy param;
211 + /* Get the parameter data.
213 + if (copy_from_user(¶m32, (void *)arg, sizeof(param32))) {
214 + pr_err("[%s]: failed to copy-from-user\n", __func__);
218 + param.dst = compat_ptr(param32.dst);
219 + param.src = param32.src;
220 + param.length = param32.length;
221 + ret = vc_mem_copy(fb, ¶m);
225 + ret = bcm2708_ioctl(info, cmd, arg);
232 static void bcm2708_fb_fillrect(struct fb_info *info,
233 const struct fb_fillrect *rect)
235 @@ -821,6 +980,9 @@ static struct fb_ops bcm2708_fb_ops = {
236 .fb_imageblit = bcm2708_fb_imageblit,
237 .fb_pan_display = bcm2708_fb_pan_display,
238 .fb_ioctl = bcm2708_ioctl,
239 +#ifdef CONFIG_COMPAT
240 + .fb_compat_ioctl = bcm2708_compat_ioctl,
244 static int bcm2708_fb_register(struct bcm2708_fb *fb)
245 --- a/drivers/video/fbdev/core/fbmem.c
246 +++ b/drivers/video/fbdev/core/fbmem.c
247 @@ -1097,6 +1097,30 @@ fb_blank(struct fb_info *info, int blank
249 EXPORT_SYMBOL(fb_blank);
251 +static int fb_copyarea_user(struct fb_info *info,
252 + struct fb_copyarea *copy)
255 + lock_fb_info(info);
256 + if (copy->dx >= info->var.xres ||
257 + copy->sx >= info->var.xres ||
258 + copy->width > info->var.xres ||
259 + copy->dy >= info->var.yres ||
260 + copy->sy >= info->var.yres ||
261 + copy->height > info->var.yres ||
262 + copy->dx + copy->width > info->var.xres ||
263 + copy->sx + copy->width > info->var.xres ||
264 + copy->dy + copy->height > info->var.yres ||
265 + copy->sy + copy->height > info->var.yres) {
269 + info->fbops->fb_copyarea(info, copy);
271 + unlock_fb_info(info);
275 static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
278 @@ -1105,6 +1129,7 @@ static long do_fb_ioctl(struct fb_info *
279 struct fb_fix_screeninfo fix;
280 struct fb_cmap cmap_from;
281 struct fb_cmap_user cmap;
282 + struct fb_copyarea copy;
283 void __user *argp = (void __user *)arg;
286 @@ -1182,6 +1207,15 @@ static long do_fb_ioctl(struct fb_info *
287 unlock_fb_info(info);
291 + if (info->flags & FBINFO_HWACCEL_COPYAREA) {
292 + /* only provide this ioctl if it is accelerated */
293 + if (copy_from_user(©, argp, sizeof(copy)))
295 + ret = fb_copyarea_user(info, ©);
302 @@ -1321,6 +1355,7 @@ static long fb_compat_ioctl(struct file
303 case FBIOPAN_DISPLAY:
304 case FBIOGET_CON2FBMAP:
305 case FBIOPUT_CON2FBMAP:
307 arg = (unsigned long) compat_ptr(arg);
310 --- a/include/uapi/linux/fb.h
311 +++ b/include/uapi/linux/fb.h
313 #define FBIOPUT_MODEINFO 0x4617
314 #define FBIOGET_DISPINFO 0x4618
315 #define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32)
317 + * HACK: use 'z' in order not to clash with any other ioctl numbers which might
318 + * be concurrently added to the mainline kernel
320 +#define FBIOCOPYAREA _IOW('z', 0x21, struct fb_copyarea)
321 +#define FBIODMACOPY _IOW('z', 0x22, struct fb_dmacopy)
323 #define FB_TYPE_PACKED_PIXELS 0 /* Packed Pixels */
324 #define FB_TYPE_PLANES 1 /* Non interleaved planes */
325 @@ -348,6 +354,12 @@ struct fb_copyarea {
336 __u32 dx; /* screen-relative */