1 From d56622a9c76ab913da66bb22c3158b10f9fb0543 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 056/454] 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 | 111 +++++++++++++++++++++++++++++++
17 drivers/video/fbdev/core/fbmem.c | 36 ++++++++++
18 include/uapi/linux/fb.h | 12 ++++
19 3 files changed, 159 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 <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 @@ -94,6 +96,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 @@ -426,6 +429,106 @@ static int bcm2708_fb_pan_display(struct
46 +static void dma_memcpy(struct bcm2708_fb *fb, dma_addr_t dst, dma_addr_t src, int size)
48 + int burst_size = (fb->dma_chan == 0) ? 8 : 2;
49 + struct bcm2708_dma_cb *cb = fb->cb_base;
51 + cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
52 + BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
62 + if (size < dma_busy_wait_threshold) {
63 + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
64 + bcm_dma_wait_idle(fb->dma_chan_base);
66 + void __iomem *dma_chan = fb->dma_chan_base;
67 + cb->info |= BCM2708_DMA_INT_EN;
68 + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
69 + while (bcm_dma_is_busy(dma_chan)) {
70 + wait_event_interruptible(
72 + !bcm_dma_is_busy(dma_chan));
74 + fb->stats.dma_irqs++;
76 + fb->stats.dma_copies++;
79 +#define INTALIAS_NORMAL(x) ((x)&~0xc0000000) // address with no aliases
80 +#define INTALIAS_L1L2_NONALLOCATING(x) (((x)&~0xc0000000)|0x80000000) // cache coherent but non-allocating in L1 and L2
82 +static long vc_mem_copy(struct bcm2708_fb *fb, unsigned long arg)
84 + struct fb_dmacopy ioparam;
85 + size_t size = PAGE_SIZE;
87 + dma_addr_t bus_addr;
91 + /* restrict this to root user */
92 + if (!uid_eq(current_euid(), GLOBAL_ROOT_UID))
98 + /* Get the parameter data.
101 + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
102 + pr_err("[%s]: failed to copy-from-user\n",
108 + if (fb->gpu.base == 0 || fb->gpu.length == 0) {
109 + pr_err("[%s]: Unable to determine gpu memory (%x,%x)\n", __func__, fb->gpu.base, fb->gpu.length);
113 + if (INTALIAS_NORMAL(ioparam.src) < fb->gpu.base || INTALIAS_NORMAL(ioparam.src) >= fb->gpu.base + fb->gpu.length) {
114 + pr_err("[%s]: Invalid memory access %x (%x-%x)", __func__, INTALIAS_NORMAL(ioparam.src), fb->gpu.base, fb->gpu.base + fb->gpu.length);
118 + buf = dma_alloc_coherent(fb->fb.device, PAGE_ALIGN(size), &bus_addr,
121 + pr_err("[%s]: failed to dma_alloc_coherent(%d)\n",
127 + for (offset = 0; offset < ioparam.length; offset += size) {
128 + size_t remaining = ioparam.length - offset;
129 + size_t s = min(size, remaining);
130 + unsigned char *p = (unsigned char *)ioparam.src + offset;
131 + unsigned char *q = (unsigned char *)ioparam.dst + offset;
132 + dma_memcpy(fb, bus_addr, INTALIAS_L1L2_NONALLOCATING((dma_addr_t)p), size);
133 + if (copy_to_user(q, buf, s) != 0) {
134 + pr_err("[%s]: failed to copy-to-user\n",
142 + dma_free_coherent(fb->fb.device, PAGE_ALIGN(size), buf, bus_addr);
146 static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
148 struct bcm2708_fb *fb = to_bcm2708(info);
149 @@ -438,6 +541,9 @@ static int bcm2708_ioctl(struct fb_info
150 RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC,
151 &dummy, sizeof(dummy));
154 + ret = vc_mem_copy(fb, arg);
157 dev_dbg(info->device, "Unknown ioctl 0x%x\n", cmd);
159 @@ -760,6 +866,11 @@ static int bcm2708_fb_probe(struct platf
161 fb->fb.device = &dev->dev;
163 + // failure here isn't fatal, but we'll fail in vc_mem_copy if fb->gpu is not valid
164 + rpi_firmware_property(fb->fw,
165 + RPI_FIRMWARE_GET_VC_MEMORY,
166 + &fb->gpu, sizeof(fb->gpu));
168 ret = bcm2708_fb_register(fb);
170 platform_set_drvdata(dev, fb);
171 --- a/drivers/video/fbdev/core/fbmem.c
172 +++ b/drivers/video/fbdev/core/fbmem.c
173 @@ -1086,6 +1086,31 @@ fb_blank(struct fb_info *info, int blank
175 EXPORT_SYMBOL(fb_blank);
177 +static int fb_copyarea_user(struct fb_info *info,
178 + struct fb_copyarea *copy)
181 + if (!lock_fb_info(info))
183 + if (copy->dx >= info->var.xres ||
184 + copy->sx >= info->var.xres ||
185 + copy->width > info->var.xres ||
186 + copy->dy >= info->var.yres ||
187 + copy->sy >= info->var.yres ||
188 + copy->height > info->var.yres ||
189 + copy->dx + copy->width > info->var.xres ||
190 + copy->sx + copy->width > info->var.xres ||
191 + copy->dy + copy->height > info->var.yres ||
192 + copy->sy + copy->height > info->var.yres) {
196 + info->fbops->fb_copyarea(info, copy);
198 + unlock_fb_info(info);
202 static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
205 @@ -1096,6 +1121,7 @@ static long do_fb_ioctl(struct fb_info *
206 struct fb_cmap cmap_from;
207 struct fb_cmap_user cmap;
208 struct fb_event event;
209 + struct fb_copyarea copy;
210 void __user *argp = (void __user *)arg;
213 @@ -1213,6 +1239,15 @@ static long do_fb_ioctl(struct fb_info *
214 unlock_fb_info(info);
218 + if (info->flags & FBINFO_HWACCEL_COPYAREA) {
219 + /* only provide this ioctl if it is accelerated */
220 + if (copy_from_user(©, argp, sizeof(copy)))
222 + ret = fb_copyarea_user(info, ©);
227 if (!lock_fb_info(info))
229 @@ -1358,6 +1393,7 @@ static long fb_compat_ioctl(struct file
230 case FBIOPAN_DISPLAY:
231 case FBIOGET_CON2FBMAP:
232 case FBIOPUT_CON2FBMAP:
234 arg = (unsigned long) compat_ptr(arg);
236 ret = do_fb_ioctl(info, cmd, arg);
237 --- a/include/uapi/linux/fb.h
238 +++ b/include/uapi/linux/fb.h
240 #define FBIOPUT_MODEINFO 0x4617
241 #define FBIOGET_DISPINFO 0x4618
242 #define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32)
244 + * HACK: use 'z' in order not to clash with any other ioctl numbers which might
245 + * be concurrently added to the mainline kernel
247 +#define FBIOCOPYAREA _IOW('z', 0x21, struct fb_copyarea)
248 +#define FBIODMACOPY _IOW('z', 0x22, struct fb_dmacopy)
250 #define FB_TYPE_PACKED_PIXELS 0 /* Packed Pixels */
251 #define FB_TYPE_PLANES 1 /* Non interleaved planes */
252 @@ -347,6 +353,12 @@ struct fb_copyarea {
263 __u32 dx; /* screen-relative */