Skip to content

Commit f703bc2

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
1 parent 9f3e8e3 commit f703bc2

3 files changed

Lines changed: 157 additions & 0 deletions

File tree

‎drivers/video/fbdev/bcm2708_fb.c‎

Lines changed: 109 additions & 0 deletions
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 <asm/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
@@ -426,6 +428,110 @@ static int bcm2708_fb_pan_display(struct fb_var_screeninfo *var, struct fb_info
426428
return result;
427429
}
428430

431+
static void dma_memcpy(struct bcm2708_fb *fb, dma_addr_t dst, dma_addr_t src, int size)
432+
{
433+
int burst_size = (fb->dma_chan == 0) ? 8 : 2;
434+
struct bcm2708_dma_cb *cb = fb->cb_base;
435+
436+
cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
437+
BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
438+
BCM2708_DMA_D_INC;
439+
cb->dst = dst;
440+
cb->src = src;
441+
cb->length = size;
442+
cb->stride = 0;
443+
cb->pad[0] = 0;
444+
cb->pad[1] = 0;
445+
cb->next = 0;
446+
447+
if (size < dma_busy_wait_threshold) {
448+
bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
449+
bcm_dma_wait_idle(fb->dma_chan_base);
450+
} else {
451+
void __iomem *dma_chan = fb->dma_chan_base;
452+
cb->info |= BCM2708_DMA_INT_EN;
453+
bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
454+
while (bcm_dma_is_busy(dma_chan)) {
455+
wait_event_interruptible(
456+
fb->dma_waitq,
457+
!bcm_dma_is_busy(dma_chan));
458+
}
459+
fb->stats.dma_irqs++;
460+
}
461+
fb->stats.dma_copies++;
462+
}
463+
464+
#define INTALIAS_NORMAL(x) ((x)&~0xc0000000) // address with no aliases
465+
#define INTALIAS_L1L2_NONALLOCATING(x) (((x)&~0xc0000000)|0x80000000) // cache coherent but non-allocating in L1 and L2
466+
467+
static long vc_mem_copy(struct bcm2708_fb *fb, unsigned long arg)
468+
{
469+
struct fb_dmacopy ioparam;
470+
size_t size = PAGE_SIZE;
471+
u32 *buf = NULL;
472+
dma_addr_t bus_addr;
473+
long rc = 0;
474+
size_t offset;
475+
struct { u32 base, length; } gpu = {};
476+
477+
/* restrict this to root user */
478+
if (!uid_eq(current_euid(), GLOBAL_ROOT_UID))
479+
{
480+
rc = -EFAULT;
481+
goto out;
482+
}
483+
484+
/* Get the parameter data.
485+
*/
486+
if (copy_from_user
487+
(&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
488+
pr_err("[%s]: failed to copy-from-user\n",
489+
__func__);
490+
rc = -EFAULT;
491+
goto out;
492+
}
493+
494+
rc = rpi_firmware_property(fb->fw,
495+
RPI_FIRMWARE_GET_VC_MEMORY,
496+
&gpu, sizeof(gpu));
497+
if (rc != 0 || gpu.base == 0 || gpu.length == 0) {
498+
pr_err("[%s]: Unable to determine gpu memory %ld,%x,%x)\n", __func__, rc, gpu.base, gpu.length);
499+
return -EFAULT;
500+
}
501+
502+
if (INTALIAS_NORMAL(ioparam.src) < gpu.base || INTALIAS_NORMAL(ioparam.src) >= gpu.base + gpu.length) {
503+
pr_err("[%s]: Invalid memory access %x (%x-%x)", __func__, INTALIAS_NORMAL(ioparam.src), gpu.base, gpu.base + gpu.length);
504+
return -EFAULT;
505+
}
506+
507+
buf = dma_alloc_coherent(fb->fb.device, PAGE_ALIGN(size), &bus_addr,
508+
GFP_ATOMIC);
509+
if (!buf) {
510+
pr_err("[%s]: failed to dma_alloc_coherent(%d)\n",
511+
__func__, size);
512+
rc = -ENOMEM;
513+
goto out;
514+
}
515+
516+
for (offset = 0; offset < ioparam.length; offset += size) {
517+
size_t remaining = ioparam.length - offset;
518+
size_t s = min(size, remaining);
519+
unsigned char *p = (unsigned char *)ioparam.src + offset;
520+
unsigned char *q = (unsigned char *)ioparam.dst + offset;
521+
dma_memcpy(fb, bus_addr, INTALIAS_L1L2_NONALLOCATING((dma_addr_t)p), size);
522+
if (copy_to_user(q, buf, s) != 0) {
523+
pr_err("[%s]: failed to copy-to-user\n",
524+
__func__);
525+
rc = -EFAULT;
526+
goto out;
527+
}
528+
}
529+
out:
530+
if (buf)
531+
dma_free_coherent(fb->fb.device, PAGE_ALIGN(size), buf, bus_addr);
532+
return rc;
533+
}
534+
429535
static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
430536
{
431537
struct bcm2708_fb *fb = to_bcm2708(info);
@@ -438,6 +544,9 @@ static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long a
438544
RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC,
439545
&dummy, sizeof(dummy));
440546
break;
547+
case FBIODMACOPY:
548+
ret = vc_mem_copy(fb, arg);
549+
break;
441550
default:
442551
dev_dbg(info->device, "Unknown ioctl 0x%x\n", cmd);
443552
return -ENOTTY;

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,31 @@ fb_blank(struct fb_info *info, int blank)
10841084
}
10851085
EXPORT_SYMBOL(fb_blank);
10861086

1087+
static int fb_copyarea_user(struct fb_info *info,
1088+
struct fb_copyarea *copy)
1089+
{
1090+
int ret = 0;
1091+
if (!lock_fb_info(info))
1092+
return -ENODEV;
1093+
if (copy->dx >= info->var.xres ||
1094+
copy->sx >= info->var.xres ||
1095+
copy->width > info->var.xres ||
1096+
copy->dy >= info->var.yres ||
1097+
copy->sy >= info->var.yres ||
1098+
copy->height > info->var.yres ||
1099+
copy->dx + copy->width > info->var.xres ||
1100+
copy->sx + copy->width > info->var.xres ||
1101+
copy->dy + copy->height > info->var.yres ||
1102+
copy->sy + copy->height > info->var.yres) {
1103+
ret = -EINVAL;
1104+
goto out;
1105+
}
1106+
info->fbops->fb_copyarea(info, copy);
1107+
out:
1108+
unlock_fb_info(info);
1109+
return ret;
1110+
}
1111+
10871112
static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
10881113
unsigned long arg)
10891114
{
@@ -1094,6 +1119,7 @@ static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
10941119
struct fb_cmap cmap_from;
10951120
struct fb_cmap_user cmap;
10961121
struct fb_event event;
1122+
struct fb_copyarea copy;
10971123
void __user *argp = (void __user *)arg;
10981124
long ret = 0;
10991125

@@ -1211,6 +1237,15 @@ static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
12111237
unlock_fb_info(info);
12121238
console_unlock();
12131239
break;
1240+
case FBIOCOPYAREA:
1241+
if (info->flags & FBINFO_HWACCEL_COPYAREA) {
1242+
/* only provide this ioctl if it is accelerated */
1243+
if (copy_from_user(&copy, argp, sizeof(copy)))
1244+
return -EFAULT;
1245+
ret = fb_copyarea_user(info, &copy);
1246+
break;
1247+
}
1248+
/* fall through */
12141249
default:
12151250
if (!lock_fb_info(info))
12161251
return -ENODEV;
@@ -1365,6 +1400,7 @@ static long fb_compat_ioctl(struct file *file, unsigned int cmd,
13651400
case FBIOPAN_DISPLAY:
13661401
case FBIOGET_CON2FBMAP:
13671402
case FBIOPUT_CON2FBMAP:
1403+
case FBIOCOPYAREA:
13681404
arg = (unsigned long) compat_ptr(arg);
13691405
case FBIOBLANK:
13701406
ret = do_fb_ioctl(info, cmd, arg);

‎include/uapi/linux/fb.h‎

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

3844
#define FB_TYPE_PACKED_PIXELS 0 /* Packed Pixels */
3945
#define FB_TYPE_PLANES 1 /* Non interleaved planes */
@@ -346,6 +352,12 @@ struct fb_copyarea {
346352
__u32 sy;
347353
};
348354

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

0 commit comments

Comments
 (0)