brcm2708: update linux 4.4 patches to latest version
[openwrt/openwrt.git] / target / linux / brcm2708 / patches-4.4 / 0531-bcm2708_fb-Add-ioctl-for-reading-gpu-memory-through-.patch
1 From 26a7e2d3d11c23acc6f87414da736c07a3b9cc52 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
5
6 ---
7 drivers/video/fbdev/bcm2708_fb.c | 109 +++++++++++++++++++++++++++++++++++++++
8 include/uapi/linux/fb.h | 7 +++
9 2 files changed, 116 insertions(+)
10
11 --- a/drivers/video/fbdev/bcm2708_fb.c
12 +++ b/drivers/video/fbdev/bcm2708_fb.c
13 @@ -31,8 +31,10 @@
14 #include <linux/console.h>
15 #include <linux/debugfs.h>
16 #include <asm/sizes.h>
17 +#include <asm/uaccess.h>
18 #include <linux/io.h>
19 #include <linux/dma-mapping.h>
20 +#include <linux/cred.h>
21 #include <soc/bcm2835/raspberrypi-firmware.h>
22
23 //#define BCM2708_FB_DEBUG
24 @@ -429,6 +431,110 @@ static int bcm2708_fb_pan_display(struct
25 return result;
26 }
27
28 +static void dma_memcpy(struct bcm2708_fb *fb, dma_addr_t dst, dma_addr_t src, int size)
29 +{
30 + int burst_size = (fb->dma_chan == 0) ? 8 : 2;
31 + struct bcm2708_dma_cb *cb = fb->cb_base;
32 +
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;
36 + cb->dst = dst;
37 + cb->src = src;
38 + cb->length = size;
39 + cb->stride = 0;
40 + cb->pad[0] = 0;
41 + cb->pad[1] = 0;
42 + cb->next = 0;
43 +
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);
47 + } else {
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(
53 + fb->dma_waitq,
54 + !bcm_dma_is_busy(dma_chan));
55 + }
56 + fb->stats.dma_irqs++;
57 + }
58 + fb->stats.dma_copies++;
59 +}
60 +
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
63 +
64 +static long vc_mem_copy(struct bcm2708_fb *fb, unsigned long arg)
65 +{
66 + struct fb_dmacopy ioparam;
67 + size_t size = PAGE_SIZE;
68 + u32 *buf = NULL;
69 + dma_addr_t bus_addr;
70 + long rc = 0;
71 + size_t offset;
72 + struct { u32 base, length; } gpu = {};
73 +
74 + /* restrict this to root user */
75 + if (!uid_eq(current_euid(), GLOBAL_ROOT_UID))
76 + {
77 + rc = -EFAULT;
78 + goto out;
79 + }
80 +
81 + /* Get the parameter data.
82 + */
83 + if (copy_from_user
84 + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
85 + pr_err("[%s]: failed to copy-from-user\n",
86 + __func__);
87 + rc = -EFAULT;
88 + goto out;
89 + }
90 +
91 + rc = rpi_firmware_property(fb->fw,
92 + RPI_FIRMWARE_GET_VC_MEMORY,
93 + &gpu, sizeof(gpu));
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);
96 + return -EFAULT;
97 + }
98 +
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);
101 + return -EFAULT;
102 + }
103 +
104 + buf = dma_alloc_coherent(NULL, PAGE_ALIGN(size), &bus_addr,
105 + GFP_ATOMIC);
106 + if (!buf) {
107 + pr_err("[%s]: failed to dma_alloc_coherent(%d)\n",
108 + __func__, size);
109 + rc = -ENOMEM;
110 + goto out;
111 + }
112 +
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",
121 + __func__);
122 + rc = -EFAULT;
123 + goto out;
124 + }
125 + }
126 +out:
127 + if (buf)
128 + dma_free_coherent(NULL, PAGE_ALIGN(size), buf, bus_addr);
129 + return rc;
130 +}
131 +
132 static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
133 {
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));
138 break;
139 + case FBIODMACOPY:
140 + ret = vc_mem_copy(fb, arg);
141 + break;
142 default:
143 dev_dbg(info->device, "Unknown ioctl 0x%x\n", cmd);
144 return -ENOTTY;
145 --- a/include/uapi/linux/fb.h
146 +++ b/include/uapi/linux/fb.h
147 @@ -39,6 +39,7 @@
148 * be concurrently added to the mainline kernel
149 */
150 #define FBIOCOPYAREA _IOW('z', 0x21, struct fb_copyarea)
151 +#define FBIODMACOPY _IOW('z', 0x22, struct fb_dmacopy)
152
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 {
156 __u32 sy;
157 };
158
159 +struct fb_dmacopy {
160 + dma_addr_t dst;
161 + dma_addr_t src;
162 + __u32 length;
163 +};
164 +
165 struct fb_fillrect {
166 __u32 dx; /* screen-relative */
167 __u32 dy;