Skip to content

Commit 6656fbe

Browse files
ssvbpopcornmix
authored andcommitted
fbdev: add FBIOCOPYAREA ioctl
Based on the patch authored by Ali Gholami Rudi at https://lkml.org/lkml/2009/7/13/153 Provide an ioctl for userspace applications, but only if this operation is hardware accelerated (otherwide it does not make any sense). Signed-off-by: Siarhei Siamashka <siarhei.siamashka@gmail.com> bcm2708_fb: Add ioctl for reading gpu memory through dma video: bcm2708_fb: Add compat_ioctl support. When using a 64 bit kernel with 32 bit userspace we need compat ioctl handling for FBIODMACOPY as one of the parameters is a pointer. Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
1 parent 5e454b1 commit 6656fbe

3 files changed

Lines changed: 209 additions & 1 deletion

File tree

‎drivers/video/fbdev/bcm2708_fb.c‎

Lines changed: 161 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
#include <linux/console.h>
3232
#include <linux/debugfs.h>
3333
#include <asm/sizes.h>
34+
#include <linux/uaccess.h>
3435
#include <linux/io.h>
3536
#include <linux/dma-mapping.h>
37+
#include <linux/cred.h>
3638
#include <soc/bcm2835/raspberrypi-firmware.h>
3739

3840
//#define BCM2708_FB_DEBUG
@@ -99,6 +101,7 @@ struct bcm2708_fb {
99101
unsigned int image_size;
100102
dma_addr_t dma_addr;
101103
void *cpuaddr;
104+
struct { u32 base, length; } gpu;
102105
};
103106

104107
#define to_bcm2708(info) container_of(info, struct bcm2708_fb, fb)
@@ -510,7 +513,105 @@ static int bcm2708_fb_pan_display(struct fb_var_screeninfo *var,
510513
return result;
511514
}
512515

513-
static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
516+
static void dma_memcpy(struct bcm2708_fb *fb, dma_addr_t dst, dma_addr_t src,
517+
int size)
518+
{
519+
int burst_size = (fb->dma_chan == 0) ? 8 : 2;
520+
struct bcm2708_dma_cb *cb = fb->cb_base;
521+
522+
cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
523+
BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
524+
BCM2708_DMA_D_INC;
525+
cb->dst = dst;
526+
cb->src = src;
527+
cb->length = size;
528+
cb->stride = 0;
529+
cb->pad[0] = 0;
530+
cb->pad[1] = 0;
531+
cb->next = 0;
532+
533+
if (size < dma_busy_wait_threshold) {
534+
bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
535+
bcm_dma_wait_idle(fb->dma_chan_base);
536+
} else {
537+
void __iomem *dma_chan = fb->dma_chan_base;
538+
539+
cb->info |= BCM2708_DMA_INT_EN;
540+
bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
541+
while (bcm_dma_is_busy(dma_chan)) {
542+
wait_event_interruptible(fb->dma_waitq,
543+
!bcm_dma_is_busy(dma_chan));
544+
}
545+
fb->stats.dma_irqs++;
546+
}
547+
fb->stats.dma_copies++;
548+
}
549+
550+
/* address with no aliases */
551+
#define INTALIAS_NORMAL(x) ((x) & ~0xc0000000)
552+
/* cache coherent but non-allocating in L1 and L2 */
553+
#define INTALIAS_L1L2_NONALLOCATING(x) (((x) & ~0xc0000000) | 0x80000000)
554+
555+
static long vc_mem_copy(struct bcm2708_fb *fb, struct fb_dmacopy *ioparam)
556+
{
557+
size_t size = PAGE_SIZE;
558+
u32 *buf = NULL;
559+
dma_addr_t bus_addr;
560+
long rc = 0;
561+
size_t offset;
562+
563+
/* restrict this to root user */
564+
if (!uid_eq(current_euid(), GLOBAL_ROOT_UID)) {
565+
rc = -EFAULT;
566+
goto out;
567+
}
568+
569+
if (!fb->gpu.base || !fb->gpu.length) {
570+
pr_err("[%s]: Unable to determine gpu memory (%x,%x)\n",
571+
__func__, fb->gpu.base, fb->gpu.length);
572+
return -EFAULT;
573+
}
574+
575+
if (INTALIAS_NORMAL(ioparam->src) < fb->gpu.base ||
576+
INTALIAS_NORMAL(ioparam->src) >= fb->gpu.base + fb->gpu.length) {
577+
pr_err("[%s]: Invalid memory access %x (%x-%x)", __func__,
578+
INTALIAS_NORMAL(ioparam->src), fb->gpu.base,
579+
fb->gpu.base + fb->gpu.length);
580+
return -EFAULT;
581+
}
582+
583+
buf = dma_alloc_coherent(fb->fb.device, PAGE_ALIGN(size), &bus_addr,
584+
GFP_ATOMIC);
585+
if (!buf) {
586+
pr_err("[%s]: failed to dma_alloc_coherent(%zd)\n", __func__,
587+
size);
588+
rc = -ENOMEM;
589+
goto out;
590+
}
591+
592+
for (offset = 0; offset < ioparam->length; offset += size) {
593+
size_t remaining = ioparam->length - offset;
594+
size_t s = min(size, remaining);
595+
u8 *p = (u8 *)((uintptr_t)ioparam->src + offset);
596+
u8 *q = (u8 *)ioparam->dst + offset;
597+
598+
dma_memcpy(fb, bus_addr,
599+
INTALIAS_L1L2_NONALLOCATING((dma_addr_t)p), size);
600+
if (copy_to_user(q, buf, s) != 0) {
601+
pr_err("[%s]: failed to copy-to-user\n", __func__);
602+
rc = -EFAULT;
603+
goto out;
604+
}
605+
}
606+
out:
607+
if (buf)
608+
dma_free_coherent(fb->fb.device, PAGE_ALIGN(size), buf,
609+
bus_addr);
610+
return rc;
611+
}
612+
613+
static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd,
614+
unsigned long arg)
514615
{
515616
struct bcm2708_fb *fb = to_bcm2708(info);
516617
u32 dummy = 0;
@@ -522,6 +623,20 @@ static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long a
522623
RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC,
523624
&dummy, sizeof(dummy));
524625
break;
626+
case FBIODMACOPY:
627+
{
628+
struct fb_dmacopy ioparam;
629+
/* Get the parameter data.
630+
*/
631+
if (copy_from_user
632+
(&ioparam, (void *)arg, sizeof(ioparam))) {
633+
pr_err("[%s]: failed to copy-from-user\n", __func__);
634+
ret = -EFAULT;
635+
break;
636+
}
637+
ret = vc_mem_copy(fb, &ioparam);
638+
break;
639+
}
525640
default:
526641
dev_dbg(info->device, "Unknown ioctl 0x%x\n", cmd);
527642
return -ENOTTY;
@@ -532,6 +647,48 @@ static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long a
532647

533648
return ret;
534649
}
650+
651+
#ifdef CONFIG_COMPAT
652+
struct fb_dmacopy32 {
653+
compat_uptr_t dst;
654+
__u32 src;
655+
__u32 length;
656+
};
657+
658+
#define FBIODMACOPY32 _IOW('z', 0x22, struct fb_dmacopy32)
659+
660+
static int bcm2708_compat_ioctl(struct fb_info *info, unsigned int cmd,
661+
unsigned long arg)
662+
{
663+
struct bcm2708_fb *fb = to_bcm2708(info);
664+
int ret;
665+
666+
switch (cmd) {
667+
case FBIODMACOPY32:
668+
{
669+
struct fb_dmacopy32 param32;
670+
struct fb_dmacopy param;
671+
/* Get the parameter data.
672+
*/
673+
if (copy_from_user(&param32, (void *)arg, sizeof(param32))) {
674+
pr_err("[%s]: failed to copy-from-user\n", __func__);
675+
ret = -EFAULT;
676+
break;
677+
}
678+
param.dst = compat_ptr(param32.dst);
679+
param.src = param32.src;
680+
param.length = param32.length;
681+
ret = vc_mem_copy(fb, &param);
682+
break;
683+
}
684+
default:
685+
ret = bcm2708_ioctl(info, cmd, arg);
686+
break;
687+
}
688+
return ret;
689+
}
690+
#endif
691+
535692
static void bcm2708_fb_fillrect(struct fb_info *info,
536693
const struct fb_fillrect *rect)
537694
{
@@ -720,6 +877,9 @@ static struct fb_ops bcm2708_fb_ops = {
720877
.fb_imageblit = bcm2708_fb_imageblit,
721878
.fb_pan_display = bcm2708_fb_pan_display,
722879
.fb_ioctl = bcm2708_ioctl,
880+
#ifdef CONFIG_COMPAT
881+
.fb_compat_ioctl = bcm2708_compat_ioctl,
882+
#endif
723883
};
724884

725885
static int bcm2708_fb_register(struct bcm2708_fb *fb)

‎drivers/video/fbdev/core/fbmem.c‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,31 @@ fb_blank(struct fb_info *info, int blank)
11061106
}
11071107
EXPORT_SYMBOL(fb_blank);
11081108

1109+
static int fb_copyarea_user(struct fb_info *info,
1110+
struct fb_copyarea *copy)
1111+
{
1112+
int ret = 0;
1113+
if (!lock_fb_info(info))
1114+
return -ENODEV;
1115+
if (copy->dx >= info->var.xres ||
1116+
copy->sx >= info->var.xres ||
1117+
copy->width > info->var.xres ||
1118+
copy->dy >= info->var.yres ||
1119+
copy->sy >= info->var.yres ||
1120+
copy->height > info->var.yres ||
1121+
copy->dx + copy->width > info->var.xres ||
1122+
copy->sx + copy->width > info->var.xres ||
1123+
copy->dy + copy->height > info->var.yres ||
1124+
copy->sy + copy->height > info->var.yres) {
1125+
ret = -EINVAL;
1126+
goto out;
1127+
}
1128+
info->fbops->fb_copyarea(info, copy);
1129+
out:
1130+
unlock_fb_info(info);
1131+
return ret;
1132+
}
1133+
11091134
static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
11101135
unsigned long arg)
11111136
{
@@ -1116,6 +1141,7 @@ static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
11161141
struct fb_cmap cmap_from;
11171142
struct fb_cmap_user cmap;
11181143
struct fb_event event;
1144+
struct fb_copyarea copy;
11191145
void __user *argp = (void __user *)arg;
11201146
long ret = 0;
11211147

@@ -1235,6 +1261,15 @@ static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
12351261
unlock_fb_info(info);
12361262
console_unlock();
12371263
break;
1264+
case FBIOCOPYAREA:
1265+
if (info->flags & FBINFO_HWACCEL_COPYAREA) {
1266+
/* only provide this ioctl if it is accelerated */
1267+
if (copy_from_user(&copy, argp, sizeof(copy)))
1268+
return -EFAULT;
1269+
ret = fb_copyarea_user(info, &copy);
1270+
break;
1271+
}
1272+
/* fall through */
12381273
default:
12391274
if (!lock_fb_info(info))
12401275
return -ENODEV;
@@ -1382,6 +1417,7 @@ static long fb_compat_ioctl(struct file *file, unsigned int cmd,
13821417
case FBIOPAN_DISPLAY:
13831418
case FBIOGET_CON2FBMAP:
13841419
case FBIOPUT_CON2FBMAP:
1420+
case FBIOCOPYAREA:
13851421
arg = (unsigned long) compat_ptr(arg);
13861422
/* fall through */
13871423
case FBIOBLANK:

‎include/uapi/linux/fb.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
#define FBIOPUT_MODEINFO 0x4617
3636
#define FBIOGET_DISPINFO 0x4618
3737
#define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32)
38+
/*
39+
* HACK: use 'z' in order not to clash with any other ioctl numbers which might
40+
* be concurrently added to the mainline kernel
41+
*/
42+
#define FBIOCOPYAREA _IOW('z', 0x21, struct fb_copyarea)
43+
#define FBIODMACOPY _IOW('z', 0x22, struct fb_dmacopy)
3844

3945
#define FB_TYPE_PACKED_PIXELS 0 /* Packed Pixels */
4046
#define FB_TYPE_PLANES 1 /* Non interleaved planes */
@@ -347,6 +353,12 @@ struct fb_copyarea {
347353
__u32 sy;
348354
};
349355

356+
struct fb_dmacopy {
357+
void *dst;
358+
__u32 src;
359+
__u32 length;
360+
};
361+
350362
struct fb_fillrect {
351363
__u32 dx; /* screen-relative */
352364
__u32 dy;

0 commit comments

Comments
 (0)