kernel: bump 4.9 to 4.9.156
[openwrt/openwrt.git] / target / linux / brcm2708 / patches-4.9 / 950-0056-fbdev-add-FBIOCOPYAREA-ioctl.patch
1 From 65422160b21224e6d26641a4c1fa3082c0281488 Mon Sep 17 00:00:00 2001
2 From: Siarhei Siamashka <siarhei.siamashka@gmail.com>
3 Date: Mon, 17 Jun 2013 13:32:11 +0300
4 Subject: [PATCH] fbdev: add FBIOCOPYAREA ioctl
5
6 Based on the patch authored by Ali Gholami Rudi at
7 https://lkml.org/lkml/2009/7/13/153
8
9 Provide an ioctl for userspace applications, but only if this operation
10 is hardware accelerated (otherwide it does not make any sense).
11
12 Signed-off-by: Siarhei Siamashka <siarhei.siamashka@gmail.com>
13
14 bcm2708_fb: Add ioctl for reading gpu memory through dma
15 ---
16 drivers/video/fbdev/bcm2708_fb.c | 109 +++++++++++++++++++++++++++++++++++++++
17 drivers/video/fbdev/core/fbmem.c | 36 +++++++++++++
18 include/uapi/linux/fb.h | 12 +++++
19 3 files changed, 157 insertions(+)
20
21 --- a/drivers/video/fbdev/bcm2708_fb.c
22 +++ b/drivers/video/fbdev/bcm2708_fb.c
23 @@ -31,8 +31,10 @@
24 #include <linux/console.h>
25 #include <linux/debugfs.h>
26 #include <asm/sizes.h>
27 +#include <asm/uaccess.h>
28 #include <linux/io.h>
29 #include <linux/dma-mapping.h>
30 +#include <linux/cred.h>
31 #include <soc/bcm2835/raspberrypi-firmware.h>
32
33 //#define BCM2708_FB_DEBUG
34 @@ -426,6 +428,110 @@ static int bcm2708_fb_pan_display(struct
35 return result;
36 }
37
38 +static void dma_memcpy(struct bcm2708_fb *fb, dma_addr_t dst, dma_addr_t src, int size)
39 +{
40 + int burst_size = (fb->dma_chan == 0) ? 8 : 2;
41 + struct bcm2708_dma_cb *cb = fb->cb_base;
42 +
43 + cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
44 + BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
45 + BCM2708_DMA_D_INC;
46 + cb->dst = dst;
47 + cb->src = src;
48 + cb->length = size;
49 + cb->stride = 0;
50 + cb->pad[0] = 0;
51 + cb->pad[1] = 0;
52 + cb->next = 0;
53 +
54 + if (size < dma_busy_wait_threshold) {
55 + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
56 + bcm_dma_wait_idle(fb->dma_chan_base);
57 + } else {
58 + void __iomem *dma_chan = fb->dma_chan_base;
59 + cb->info |= BCM2708_DMA_INT_EN;
60 + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
61 + while (bcm_dma_is_busy(dma_chan)) {
62 + wait_event_interruptible(
63 + fb->dma_waitq,
64 + !bcm_dma_is_busy(dma_chan));
65 + }
66 + fb->stats.dma_irqs++;
67 + }
68 + fb->stats.dma_copies++;
69 +}
70 +
71 +#define INTALIAS_NORMAL(x) ((x)&~0xc0000000) // address with no aliases
72 +#define INTALIAS_L1L2_NONALLOCATING(x) (((x)&~0xc0000000)|0x80000000) // cache coherent but non-allocating in L1 and L2
73 +
74 +static long vc_mem_copy(struct bcm2708_fb *fb, unsigned long arg)
75 +{
76 + struct fb_dmacopy ioparam;
77 + size_t size = PAGE_SIZE;
78 + u32 *buf = NULL;
79 + dma_addr_t bus_addr;
80 + long rc = 0;
81 + size_t offset;
82 + struct { u32 base, length; } gpu = {};
83 +
84 + /* restrict this to root user */
85 + if (!uid_eq(current_euid(), GLOBAL_ROOT_UID))
86 + {
87 + rc = -EFAULT;
88 + goto out;
89 + }
90 +
91 + /* Get the parameter data.
92 + */
93 + if (copy_from_user
94 + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
95 + pr_err("[%s]: failed to copy-from-user\n",
96 + __func__);
97 + rc = -EFAULT;
98 + goto out;
99 + }
100 +
101 + rc = rpi_firmware_property(fb->fw,
102 + RPI_FIRMWARE_GET_VC_MEMORY,
103 + &gpu, sizeof(gpu));
104 + if (rc != 0 || gpu.base == 0 || gpu.length == 0) {
105 + pr_err("[%s]: Unable to determine gpu memory %ld,%x,%x)\n", __func__, rc, gpu.base, gpu.length);
106 + return -EFAULT;
107 + }
108 +
109 + if (INTALIAS_NORMAL(ioparam.src) < gpu.base || INTALIAS_NORMAL(ioparam.src) >= gpu.base + gpu.length) {
110 + pr_err("[%s]: Invalid memory access %x (%x-%x)", __func__, INTALIAS_NORMAL(ioparam.src), gpu.base, gpu.base + gpu.length);
111 + return -EFAULT;
112 + }
113 +
114 + buf = dma_alloc_coherent(fb->fb.device, PAGE_ALIGN(size), &bus_addr,
115 + GFP_ATOMIC);
116 + if (!buf) {
117 + pr_err("[%s]: failed to dma_alloc_coherent(%d)\n",
118 + __func__, size);
119 + rc = -ENOMEM;
120 + goto out;
121 + }
122 +
123 + for (offset = 0; offset < ioparam.length; offset += size) {
124 + size_t remaining = ioparam.length - offset;
125 + size_t s = min(size, remaining);
126 + unsigned char *p = (unsigned char *)ioparam.src + offset;
127 + unsigned char *q = (unsigned char *)ioparam.dst + offset;
128 + dma_memcpy(fb, bus_addr, INTALIAS_L1L2_NONALLOCATING((dma_addr_t)p), size);
129 + if (copy_to_user(q, buf, s) != 0) {
130 + pr_err("[%s]: failed to copy-to-user\n",
131 + __func__);
132 + rc = -EFAULT;
133 + goto out;
134 + }
135 + }
136 +out:
137 + if (buf)
138 + dma_free_coherent(fb->fb.device, PAGE_ALIGN(size), buf, bus_addr);
139 + return rc;
140 +}
141 +
142 static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
143 {
144 struct bcm2708_fb *fb = to_bcm2708(info);
145 @@ -438,6 +544,9 @@ static int bcm2708_ioctl(struct fb_info
146 RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC,
147 &dummy, sizeof(dummy));
148 break;
149 + case FBIODMACOPY:
150 + ret = vc_mem_copy(fb, arg);
151 + break;
152 default:
153 dev_dbg(info->device, "Unknown ioctl 0x%x\n", cmd);
154 return -ENOTTY;
155 --- a/drivers/video/fbdev/core/fbmem.c
156 +++ b/drivers/video/fbdev/core/fbmem.c
157 @@ -1088,6 +1088,31 @@ fb_blank(struct fb_info *info, int blank
158 }
159 EXPORT_SYMBOL(fb_blank);
160
161 +static int fb_copyarea_user(struct fb_info *info,
162 + struct fb_copyarea *copy)
163 +{
164 + int ret = 0;
165 + if (!lock_fb_info(info))
166 + return -ENODEV;
167 + if (copy->dx >= info->var.xres ||
168 + copy->sx >= info->var.xres ||
169 + copy->width > info->var.xres ||
170 + copy->dy >= info->var.yres ||
171 + copy->sy >= info->var.yres ||
172 + copy->height > info->var.yres ||
173 + copy->dx + copy->width > info->var.xres ||
174 + copy->sx + copy->width > info->var.xres ||
175 + copy->dy + copy->height > info->var.yres ||
176 + copy->sy + copy->height > info->var.yres) {
177 + ret = -EINVAL;
178 + goto out;
179 + }
180 + info->fbops->fb_copyarea(info, copy);
181 +out:
182 + unlock_fb_info(info);
183 + return ret;
184 +}
185 +
186 static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
187 unsigned long arg)
188 {
189 @@ -1098,6 +1123,7 @@ static long do_fb_ioctl(struct fb_info *
190 struct fb_cmap cmap_from;
191 struct fb_cmap_user cmap;
192 struct fb_event event;
193 + struct fb_copyarea copy;
194 void __user *argp = (void __user *)arg;
195 long ret = 0;
196
197 @@ -1215,6 +1241,15 @@ static long do_fb_ioctl(struct fb_info *
198 unlock_fb_info(info);
199 console_unlock();
200 break;
201 + case FBIOCOPYAREA:
202 + if (info->flags & FBINFO_HWACCEL_COPYAREA) {
203 + /* only provide this ioctl if it is accelerated */
204 + if (copy_from_user(&copy, argp, sizeof(copy)))
205 + return -EFAULT;
206 + ret = fb_copyarea_user(info, &copy);
207 + break;
208 + }
209 + /* fall through */
210 default:
211 if (!lock_fb_info(info))
212 return -ENODEV;
213 @@ -1369,6 +1404,7 @@ static long fb_compat_ioctl(struct file
214 case FBIOPAN_DISPLAY:
215 case FBIOGET_CON2FBMAP:
216 case FBIOPUT_CON2FBMAP:
217 + case FBIOCOPYAREA:
218 arg = (unsigned long) compat_ptr(arg);
219 case FBIOBLANK:
220 ret = do_fb_ioctl(info, cmd, arg);
221 --- a/include/uapi/linux/fb.h
222 +++ b/include/uapi/linux/fb.h
223 @@ -34,6 +34,12 @@
224 #define FBIOPUT_MODEINFO 0x4617
225 #define FBIOGET_DISPINFO 0x4618
226 #define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32)
227 +/*
228 + * HACK: use 'z' in order not to clash with any other ioctl numbers which might
229 + * be concurrently added to the mainline kernel
230 + */
231 +#define FBIOCOPYAREA _IOW('z', 0x21, struct fb_copyarea)
232 +#define FBIODMACOPY _IOW('z', 0x22, struct fb_dmacopy)
233
234 #define FB_TYPE_PACKED_PIXELS 0 /* Packed Pixels */
235 #define FB_TYPE_PLANES 1 /* Non interleaved planes */
236 @@ -346,6 +352,12 @@ struct fb_copyarea {
237 __u32 sy;
238 };
239
240 +struct fb_dmacopy {
241 + void *dst;
242 + __u32 src;
243 + __u32 length;
244 +};
245 +
246 struct fb_fillrect {
247 __u32 dx; /* screen-relative */
248 __u32 dy;