1 From 65422160b21224e6d26641a4c1fa3082c0281488 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 drivers/video/fbdev/bcm2708_fb.c | 109 +++++++++++++++++++++++++++++++++++++++
17 drivers/video/fbdev/core/fbmem.c | 36 +++++++++++++
18 include/uapi/linux/fb.h | 12 +++++
19 3 files changed, 157 insertions(+)
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 <asm/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 @@ -426,6 +428,110 @@ static int bcm2708_fb_pan_display(struct
38 +static void dma_memcpy(struct bcm2708_fb *fb, dma_addr_t dst, dma_addr_t src, int size)
40 + int burst_size = (fb->dma_chan == 0) ? 8 : 2;
41 + struct bcm2708_dma_cb *cb = fb->cb_base;
43 + cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
44 + BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
54 + if (size < dma_busy_wait_threshold) {
55 + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
56 + bcm_dma_wait_idle(fb->dma_chan_base);
58 + void __iomem *dma_chan = fb->dma_chan_base;
59 + cb->info |= BCM2708_DMA_INT_EN;
60 + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
61 + while (bcm_dma_is_busy(dma_chan)) {
62 + wait_event_interruptible(
64 + !bcm_dma_is_busy(dma_chan));
66 + fb->stats.dma_irqs++;
68 + fb->stats.dma_copies++;
71 +#define INTALIAS_NORMAL(x) ((x)&~0xc0000000) // address with no aliases
72 +#define INTALIAS_L1L2_NONALLOCATING(x) (((x)&~0xc0000000)|0x80000000) // cache coherent but non-allocating in L1 and L2
74 +static long vc_mem_copy(struct bcm2708_fb *fb, unsigned long arg)
76 + struct fb_dmacopy ioparam;
77 + size_t size = PAGE_SIZE;
79 + dma_addr_t bus_addr;
82 + struct { u32 base, length; } gpu = {};
84 + /* restrict this to root user */
85 + if (!uid_eq(current_euid(), GLOBAL_ROOT_UID))
91 + /* Get the parameter data.
94 + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
95 + pr_err("[%s]: failed to copy-from-user\n",
101 + rc = rpi_firmware_property(fb->fw,
102 + RPI_FIRMWARE_GET_VC_MEMORY,
103 + &gpu, sizeof(gpu));
104 + if (rc != 0 || gpu.base == 0 || gpu.length == 0) {
105 + pr_err("[%s]: Unable to determine gpu memory %ld,%x,%x)\n", __func__, rc, gpu.base, gpu.length);
109 + if (INTALIAS_NORMAL(ioparam.src) < gpu.base || INTALIAS_NORMAL(ioparam.src) >= gpu.base + gpu.length) {
110 + pr_err("[%s]: Invalid memory access %x (%x-%x)", __func__, INTALIAS_NORMAL(ioparam.src), gpu.base, gpu.base + gpu.length);
114 + buf = dma_alloc_coherent(fb->fb.device, PAGE_ALIGN(size), &bus_addr,
117 + pr_err("[%s]: failed to dma_alloc_coherent(%d)\n",
123 + for (offset = 0; offset < ioparam.length; offset += size) {
124 + size_t remaining = ioparam.length - offset;
125 + size_t s = min(size, remaining);
126 + unsigned char *p = (unsigned char *)ioparam.src + offset;
127 + unsigned char *q = (unsigned char *)ioparam.dst + offset;
128 + dma_memcpy(fb, bus_addr, INTALIAS_L1L2_NONALLOCATING((dma_addr_t)p), size);
129 + if (copy_to_user(q, buf, s) != 0) {
130 + pr_err("[%s]: failed to copy-to-user\n",
138 + dma_free_coherent(fb->fb.device, PAGE_ALIGN(size), buf, bus_addr);
142 static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
144 struct bcm2708_fb *fb = to_bcm2708(info);
145 @@ -438,6 +544,9 @@ static int bcm2708_ioctl(struct fb_info
146 RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC,
147 &dummy, sizeof(dummy));
150 + ret = vc_mem_copy(fb, arg);
153 dev_dbg(info->device, "Unknown ioctl 0x%x\n", cmd);
155 --- a/drivers/video/fbdev/core/fbmem.c
156 +++ b/drivers/video/fbdev/core/fbmem.c
157 @@ -1084,6 +1084,31 @@ fb_blank(struct fb_info *info, int blank
159 EXPORT_SYMBOL(fb_blank);
161 +static int fb_copyarea_user(struct fb_info *info,
162 + struct fb_copyarea *copy)
165 + if (!lock_fb_info(info))
167 + if (copy->dx >= info->var.xres ||
168 + copy->sx >= info->var.xres ||
169 + copy->width > info->var.xres ||
170 + copy->dy >= info->var.yres ||
171 + copy->sy >= info->var.yres ||
172 + copy->height > info->var.yres ||
173 + copy->dx + copy->width > info->var.xres ||
174 + copy->sx + copy->width > info->var.xres ||
175 + copy->dy + copy->height > info->var.yres ||
176 + copy->sy + copy->height > info->var.yres) {
180 + info->fbops->fb_copyarea(info, copy);
182 + unlock_fb_info(info);
186 static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
189 @@ -1094,6 +1119,7 @@ static long do_fb_ioctl(struct fb_info *
190 struct fb_cmap cmap_from;
191 struct fb_cmap_user cmap;
192 struct fb_event event;
193 + struct fb_copyarea copy;
194 void __user *argp = (void __user *)arg;
197 @@ -1211,6 +1237,15 @@ static long do_fb_ioctl(struct fb_info *
198 unlock_fb_info(info);
202 + if (info->flags & FBINFO_HWACCEL_COPYAREA) {
203 + /* only provide this ioctl if it is accelerated */
204 + if (copy_from_user(©, argp, sizeof(copy)))
206 + ret = fb_copyarea_user(info, ©);
211 if (!lock_fb_info(info))
213 @@ -1365,6 +1400,7 @@ static long fb_compat_ioctl(struct file
214 case FBIOPAN_DISPLAY:
215 case FBIOGET_CON2FBMAP:
216 case FBIOPUT_CON2FBMAP:
218 arg = (unsigned long) compat_ptr(arg);
220 ret = do_fb_ioctl(info, cmd, arg);
221 --- a/include/uapi/linux/fb.h
222 +++ b/include/uapi/linux/fb.h
224 #define FBIOPUT_MODEINFO 0x4617
225 #define FBIOGET_DISPINFO 0x4618
226 #define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32)
228 + * HACK: use 'z' in order not to clash with any other ioctl numbers which might
229 + * be concurrently added to the mainline kernel
231 +#define FBIOCOPYAREA _IOW('z', 0x21, struct fb_copyarea)
232 +#define FBIODMACOPY _IOW('z', 0x22, struct fb_dmacopy)
234 #define FB_TYPE_PACKED_PIXELS 0 /* Packed Pixels */
235 #define FB_TYPE_PLANES 1 /* Non interleaved planes */
236 @@ -346,6 +352,12 @@ struct fb_copyarea {
247 __u32 dx; /* screen-relative */