1 From c6175951b8f95d9f2c30d00ec069cafd23c6f376 Mon Sep 17 00:00:00 2001
2 From: popcornmix <popcornmix@gmail.com>
3 Date: Sat, 5 Nov 2016 14:14:43 +0000
4 Subject: [PATCH] bcm2708_fb: Add ioctl for reading gpu memory through dma
7 drivers/video/fbdev/bcm2708_fb.c | 109 +++++++++++++++++++++++++++++++++++++++
8 include/uapi/linux/fb.h | 7 +++
9 2 files changed, 116 insertions(+)
11 --- a/drivers/video/fbdev/bcm2708_fb.c
12 +++ b/drivers/video/fbdev/bcm2708_fb.c
14 #include <linux/console.h>
15 #include <linux/debugfs.h>
16 #include <asm/sizes.h>
17 +#include <asm/uaccess.h>
19 #include <linux/dma-mapping.h>
20 +#include <linux/cred.h>
21 #include <soc/bcm2835/raspberrypi-firmware.h>
23 //#define BCM2708_FB_DEBUG
24 @@ -429,6 +431,110 @@ static int bcm2708_fb_pan_display(struct
28 +static void dma_memcpy(struct bcm2708_fb *fb, dma_addr_t dst, dma_addr_t src, int size)
30 + int burst_size = (fb->dma_chan == 0) ? 8 : 2;
31 + struct bcm2708_dma_cb *cb = fb->cb_base;
33 + cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
34 + BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
35 + BCM2708_DMA_D_INC | BCM2708_DMA_TDMODE;
44 + if (size < dma_busy_wait_threshold) {
45 + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
46 + bcm_dma_wait_idle(fb->dma_chan_base);
48 + void __iomem *dma_chan = fb->dma_chan_base;
49 + cb->info |= BCM2708_DMA_INT_EN;
50 + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
51 + while (bcm_dma_is_busy(dma_chan)) {
52 + wait_event_interruptible(
54 + !bcm_dma_is_busy(dma_chan));
56 + fb->stats.dma_irqs++;
58 + fb->stats.dma_copies++;
61 +#define INTALIAS_NORMAL(x) ((x)&~0xc0000000) // address with no aliases
62 +#define INTALIAS_L1L2_NONALLOCATING(x) (((x)&~0xc0000000)|0x80000000) // cache coherent but non-allocating in L1 and L2
64 +static long vc_mem_copy(struct bcm2708_fb *fb, unsigned long arg)
66 + struct fb_dmacopy ioparam;
67 + size_t size = PAGE_SIZE;
69 + dma_addr_t bus_addr;
72 + struct { u32 base, length; } gpu = {};
74 + /* restrict this to root user */
75 + if (!uid_eq(current_euid(), GLOBAL_ROOT_UID))
81 + /* Get the parameter data.
84 + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
85 + pr_err("[%s]: failed to copy-from-user\n",
91 + rc = rpi_firmware_property(fb->fw,
92 + RPI_FIRMWARE_GET_VC_MEMORY,
94 + if (rc != 0 || gpu.base == 0 || gpu.length == 0) {
95 + pr_err("[%s]: Unable to determine gpu memory %ld,%x,%x)\n", __func__, rc, gpu.base, gpu.length);
99 + if (INTALIAS_NORMAL(ioparam.src) < gpu.base || INTALIAS_NORMAL(ioparam.src) >= gpu.base + gpu.length) {
100 + pr_err("[%s]: Invalid memory access %x (%x-%x)", __func__, INTALIAS_NORMAL(ioparam.src), gpu.base, gpu.base + gpu.length);
104 + buf = dma_alloc_coherent(NULL, PAGE_ALIGN(size), &bus_addr,
107 + pr_err("[%s]: failed to dma_alloc_coherent(%d)\n",
113 + for (offset = 0; offset < ioparam.length; offset += size) {
114 + size_t remaining = ioparam.length - offset;
115 + size_t s = min(size, remaining);
116 + unsigned char *p = (unsigned char *)ioparam.src + offset;
117 + unsigned char *q = (unsigned char *)ioparam.dst + offset;
118 + dma_memcpy(fb, (dma_addr_t)buf, INTALIAS_L1L2_NONALLOCATING((dma_addr_t)p), size);
119 + if (copy_to_user(q, buf, s) != 0) {
120 + pr_err("[%s]: failed to copy-to-user\n",
128 + dma_free_coherent(NULL, PAGE_ALIGN(size), buf, bus_addr);
132 static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
134 struct bcm2708_fb *fb = to_bcm2708(info);
135 @@ -441,6 +547,9 @@ static int bcm2708_ioctl(struct fb_info
136 RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC,
137 &dummy, sizeof(dummy));
140 + ret = vc_mem_copy(fb, arg);
143 dev_dbg(info->device, "Unknown ioctl 0x%x\n", cmd);
145 --- a/include/uapi/linux/fb.h
146 +++ b/include/uapi/linux/fb.h
148 * be concurrently added to the mainline kernel
150 #define FBIOCOPYAREA _IOW('z', 0x21, struct fb_copyarea)
151 +#define FBIODMACOPY _IOW('z', 0x22, struct fb_dmacopy)
153 #define FB_TYPE_PACKED_PIXELS 0 /* Packed Pixels */
154 #define FB_TYPE_PLANES 1 /* Non interleaved planes */
155 @@ -351,6 +352,12 @@ struct fb_copyarea {
166 __u32 dx; /* screen-relative */