Skip to content

Commit b2bdec7

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 878a60d commit b2bdec7

3 files changed

Lines changed: 213 additions & 4 deletions

File tree

‎drivers/video/fbdev/bcm2708_fb.c‎

Lines changed: 166 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
#include <linux/printk.h>
3333
#include <linux/console.h>
3434
#include <linux/debugfs.h>
35+
#include <linux/uaccess.h>
3536
#include <linux/io.h>
3637
#include <linux/dma-mapping.h>
38+
#include <linux/cred.h>
3739
#include <soc/bcm2835/raspberrypi-firmware.h>
3840
#include <linux/mutex.h>
3941

@@ -184,9 +186,6 @@ static int bcm2708_fb_debugfs_init(struct bcm2708_fb *fb)
184186

185187
fb->debugfs_subdir = debugfs_create_dir(buf, fb->debugfs_dir);
186188

187-
debugfs_create_regset32("stats", 0444, fb->debugfs_dir,
188-
&fb->stats.regset);
189-
190189
if (!fb->debugfs_subdir) {
191190
dev_warn(fb->fb.dev, "%s: could not create debugfs entry %u\n",
192191
__func__, fb->display_settings.display_num);
@@ -603,7 +602,110 @@ static int bcm2708_fb_pan_display(struct fb_var_screeninfo *var,
603602
return result;
604603
}
605604

606-
static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
605+
static void dma_memcpy(struct bcm2708_fb *fb, dma_addr_t dst, dma_addr_t src,
606+
int size)
607+
{
608+
struct bcm2708_fb_dev *fbdev = fb->fbdev;
609+
struct bcm2708_dma_cb *cb = fbdev->cb_base;
610+
int burst_size = (fbdev->dma_chan == 0) ? 8 : 2;
611+
612+
cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
613+
BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
614+
BCM2708_DMA_D_INC;
615+
cb->dst = dst;
616+
cb->src = src;
617+
cb->length = size;
618+
cb->stride = 0;
619+
cb->pad[0] = 0;
620+
cb->pad[1] = 0;
621+
cb->next = 0;
622+
623+
// Not sure what to do if this gets a signal whilst waiting
624+
if (mutex_lock_interruptible(&fbdev->dma_mutex))
625+
return;
626+
627+
if (size < dma_busy_wait_threshold) {
628+
bcm_dma_start(fbdev->dma_chan_base, fbdev->cb_handle);
629+
bcm_dma_wait_idle(fbdev->dma_chan_base);
630+
} else {
631+
void __iomem *local_dma_chan = fbdev->dma_chan_base;
632+
633+
cb->info |= BCM2708_DMA_INT_EN;
634+
bcm_dma_start(fbdev->dma_chan_base, fbdev->cb_handle);
635+
while (bcm_dma_is_busy(local_dma_chan)) {
636+
wait_event_interruptible(fbdev->dma_waitq,
637+
!bcm_dma_is_busy(local_dma_chan));
638+
}
639+
fbdev->dma_stats.dma_irqs++;
640+
}
641+
fbdev->dma_stats.dma_copies++;
642+
643+
mutex_unlock(&fbdev->dma_mutex);
644+
}
645+
646+
/* address with no aliases */
647+
#define INTALIAS_NORMAL(x) ((x) & ~0xc0000000)
648+
/* cache coherent but non-allocating in L1 and L2 */
649+
#define INTALIAS_L1L2_NONALLOCATING(x) (((x) & ~0xc0000000) | 0x80000000)
650+
651+
static long vc_mem_copy(struct bcm2708_fb *fb, struct fb_dmacopy *ioparam)
652+
{
653+
size_t size = PAGE_SIZE;
654+
u32 *buf = NULL;
655+
dma_addr_t bus_addr;
656+
long rc = 0;
657+
size_t offset;
658+
659+
/* restrict this to root user */
660+
if (!uid_eq(current_euid(), GLOBAL_ROOT_UID)) {
661+
rc = -EFAULT;
662+
goto out;
663+
}
664+
665+
if (!fb->gpu.base || !fb->gpu.length) {
666+
pr_err("[%s]: Unable to determine gpu memory (%x,%x)\n",
667+
__func__, fb->gpu.base, fb->gpu.length);
668+
return -EFAULT;
669+
}
670+
671+
if (INTALIAS_NORMAL(ioparam->src) < fb->gpu.base ||
672+
INTALIAS_NORMAL(ioparam->src) >= fb->gpu.base + fb->gpu.length) {
673+
pr_err("[%s]: Invalid memory access %x (%x-%x)", __func__,
674+
INTALIAS_NORMAL(ioparam->src), fb->gpu.base,
675+
fb->gpu.base + fb->gpu.length);
676+
return -EFAULT;
677+
}
678+
679+
buf = dma_alloc_coherent(fb->fb.device, PAGE_ALIGN(size), &bus_addr,
680+
GFP_ATOMIC);
681+
if (!buf) {
682+
pr_err("[%s]: failed to dma_alloc_coherent(%zd)\n", __func__,
683+
size);
684+
rc = -ENOMEM;
685+
goto out;
686+
}
687+
688+
for (offset = 0; offset < ioparam->length; offset += size) {
689+
size_t remaining = ioparam->length - offset;
690+
size_t s = min(size, remaining);
691+
u8 *p = (u8 *)((uintptr_t)ioparam->src + offset);
692+
u8 *q = (u8 *)ioparam->dst + offset;
693+
694+
dma_memcpy(fb, bus_addr,
695+
INTALIAS_L1L2_NONALLOCATING((dma_addr_t)p), size);
696+
if (copy_to_user(q, buf, s) != 0) {
697+
pr_err("[%s]: failed to copy-to-user\n", __func__);
698+
rc = -EFAULT;
699+
goto out;
700+
}
701+
}
702+
out:
703+
if (buf)
704+
dma_free_coherent(fb->fb.device, PAGE_ALIGN(size), buf,
705+
bus_addr);
706+
return rc;
707+
}
708+
607709
static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd,
608710
unsigned long arg)
609711
{
@@ -619,6 +721,21 @@ static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd,
619721
RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC,
620722
&dummy, sizeof(dummy));
621723
break;
724+
725+
case FBIODMACOPY:
726+
{
727+
struct fb_dmacopy ioparam;
728+
/* Get the parameter data.
729+
*/
730+
if (copy_from_user
731+
(&ioparam, (void *)arg, sizeof(ioparam))) {
732+
pr_err("[%s]: failed to copy-from-user\n", __func__);
733+
ret = -EFAULT;
734+
break;
735+
}
736+
ret = vc_mem_copy(fb, &ioparam);
737+
break;
738+
}
622739
default:
623740
dev_dbg(info->device, "Unknown ioctl 0x%x\n", cmd);
624741
return -ENOTTY;
@@ -629,6 +746,48 @@ static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd,
629746

630747
return ret;
631748
}
749+
750+
#ifdef CONFIG_COMPAT
751+
struct fb_dmacopy32 {
752+
compat_uptr_t dst;
753+
__u32 src;
754+
__u32 length;
755+
};
756+
757+
#define FBIODMACOPY32 _IOW('z', 0x22, struct fb_dmacopy32)
758+
759+
static int bcm2708_compat_ioctl(struct fb_info *info, unsigned int cmd,
760+
unsigned long arg)
761+
{
762+
struct bcm2708_fb *fb = to_bcm2708(info);
763+
int ret;
764+
765+
switch (cmd) {
766+
case FBIODMACOPY32:
767+
{
768+
struct fb_dmacopy32 param32;
769+
struct fb_dmacopy param;
770+
/* Get the parameter data.
771+
*/
772+
if (copy_from_user(&param32, (void *)arg, sizeof(param32))) {
773+
pr_err("[%s]: failed to copy-from-user\n", __func__);
774+
ret = -EFAULT;
775+
break;
776+
}
777+
param.dst = compat_ptr(param32.dst);
778+
param.src = param32.src;
779+
param.length = param32.length;
780+
ret = vc_mem_copy(fb, &param);
781+
break;
782+
}
783+
default:
784+
ret = bcm2708_ioctl(info, cmd, arg);
785+
break;
786+
}
787+
return ret;
788+
}
789+
#endif
790+
632791
static void bcm2708_fb_fillrect(struct fb_info *info,
633792
const struct fb_fillrect *rect)
634793
{
@@ -821,6 +980,9 @@ static struct fb_ops bcm2708_fb_ops = {
821980
.fb_imageblit = bcm2708_fb_imageblit,
822981
.fb_pan_display = bcm2708_fb_pan_display,
823982
.fb_ioctl = bcm2708_ioctl,
983+
#ifdef CONFIG_COMPAT
984+
.fb_compat_ioctl = bcm2708_compat_ioctl,
985+
#endif
824986
};
825987

826988
static int bcm2708_fb_register(struct bcm2708_fb *fb)

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,30 @@ fb_blank(struct fb_info *info, int blank)
10791079
}
10801080
EXPORT_SYMBOL(fb_blank);
10811081

1082+
static int fb_copyarea_user(struct fb_info *info,
1083+
struct fb_copyarea *copy)
1084+
{
1085+
int ret = 0;
1086+
lock_fb_info(info);
1087+
if (copy->dx >= info->var.xres ||
1088+
copy->sx >= info->var.xres ||
1089+
copy->width > info->var.xres ||
1090+
copy->dy >= info->var.yres ||
1091+
copy->sy >= info->var.yres ||
1092+
copy->height > info->var.yres ||
1093+
copy->dx + copy->width > info->var.xres ||
1094+
copy->sx + copy->width > info->var.xres ||
1095+
copy->dy + copy->height > info->var.yres ||
1096+
copy->sy + copy->height > info->var.yres) {
1097+
ret = -EINVAL;
1098+
goto out;
1099+
}
1100+
info->fbops->fb_copyarea(info, copy);
1101+
out:
1102+
unlock_fb_info(info);
1103+
return ret;
1104+
}
1105+
10821106
static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
10831107
unsigned long arg)
10841108
{
@@ -1087,6 +1111,7 @@ static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
10871111
struct fb_fix_screeninfo fix;
10881112
struct fb_cmap cmap_from;
10891113
struct fb_cmap_user cmap;
1114+
struct fb_copyarea copy;
10901115
void __user *argp = (void __user *)arg;
10911116
long ret = 0;
10921117

@@ -1162,6 +1187,15 @@ static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
11621187
unlock_fb_info(info);
11631188
console_unlock();
11641189
break;
1190+
case FBIOCOPYAREA:
1191+
if (info->flags & FBINFO_HWACCEL_COPYAREA) {
1192+
/* only provide this ioctl if it is accelerated */
1193+
if (copy_from_user(&copy, argp, sizeof(copy)))
1194+
return -EFAULT;
1195+
ret = fb_copyarea_user(info, &copy);
1196+
break;
1197+
}
1198+
/* fall through */
11651199
default:
11661200
lock_fb_info(info);
11671201
fb = info->fbops;
@@ -1301,6 +1335,7 @@ static long fb_compat_ioctl(struct file *file, unsigned int cmd,
13011335
case FBIOPAN_DISPLAY:
13021336
case FBIOGET_CON2FBMAP:
13031337
case FBIOPUT_CON2FBMAP:
1338+
case FBIOCOPYAREA:
13041339
arg = (unsigned long) compat_ptr(arg);
13051340
fallthrough;
13061341
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 */
@@ -348,6 +354,12 @@ struct fb_copyarea {
348354
__u32 sy;
349355
};
350356

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

0 commit comments

Comments
 (0)