bcm27xx: switch to 5.15
[openwrt/staging/chunkeey.git] / target / linux / bcm27xx / patches-5.10 / 950-0043-fbdev-add-FBIOCOPYAREA-ioctl.patch
1 From 6005247285065235845f233077bbeedb19a484c7 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 video: bcm2708_fb: Add compat_ioctl support.
17
18 When using a 64 bit kernel with 32 bit userspace we need
19 compat ioctl handling for FBIODMACOPY as one of the
20 parameters is a pointer.
21
22 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
23 ---
24 drivers/video/fbdev/bcm2708_fb.c | 170 ++++++++++++++++++++++++++++++-
25 drivers/video/fbdev/core/fbmem.c | 35 +++++++
26 include/uapi/linux/fb.h | 12 +++
27 3 files changed, 213 insertions(+), 4 deletions(-)
28
29 --- a/drivers/video/fbdev/bcm2708_fb.c
30 +++ b/drivers/video/fbdev/bcm2708_fb.c
31 @@ -32,8 +32,10 @@
32 #include <linux/printk.h>
33 #include <linux/console.h>
34 #include <linux/debugfs.h>
35 +#include <linux/uaccess.h>
36 #include <linux/io.h>
37 #include <linux/dma-mapping.h>
38 +#include <linux/cred.h>
39 #include <soc/bcm2835/raspberrypi-firmware.h>
40 #include <linux/mutex.h>
41
42 @@ -184,9 +186,6 @@ static int bcm2708_fb_debugfs_init(struc
43
44 fb->debugfs_subdir = debugfs_create_dir(buf, fb->debugfs_dir);
45
46 - debugfs_create_regset32("stats", 0444, fb->debugfs_dir,
47 - &fb->stats.regset);
48 -
49 if (!fb->debugfs_subdir) {
50 dev_warn(fb->fb.dev, "%s: could not create debugfs entry %u\n",
51 __func__, fb->display_settings.display_num);
52 @@ -603,7 +602,110 @@ static int bcm2708_fb_pan_display(struct
53 return result;
54 }
55
56 -static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
57 +static void dma_memcpy(struct bcm2708_fb *fb, dma_addr_t dst, dma_addr_t src,
58 + int size)
59 +{
60 + struct bcm2708_fb_dev *fbdev = fb->fbdev;
61 + struct bcm2708_dma_cb *cb = fbdev->cb_base;
62 + int burst_size = (fbdev->dma_chan == 0) ? 8 : 2;
63 +
64 + cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
65 + BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
66 + BCM2708_DMA_D_INC;
67 + cb->dst = dst;
68 + cb->src = src;
69 + cb->length = size;
70 + cb->stride = 0;
71 + cb->pad[0] = 0;
72 + cb->pad[1] = 0;
73 + cb->next = 0;
74 +
75 + // Not sure what to do if this gets a signal whilst waiting
76 + if (mutex_lock_interruptible(&fbdev->dma_mutex))
77 + return;
78 +
79 + if (size < dma_busy_wait_threshold) {
80 + bcm_dma_start(fbdev->dma_chan_base, fbdev->cb_handle);
81 + bcm_dma_wait_idle(fbdev->dma_chan_base);
82 + } else {
83 + void __iomem *local_dma_chan = fbdev->dma_chan_base;
84 +
85 + cb->info |= BCM2708_DMA_INT_EN;
86 + bcm_dma_start(fbdev->dma_chan_base, fbdev->cb_handle);
87 + while (bcm_dma_is_busy(local_dma_chan)) {
88 + wait_event_interruptible(fbdev->dma_waitq,
89 + !bcm_dma_is_busy(local_dma_chan));
90 + }
91 + fbdev->dma_stats.dma_irqs++;
92 + }
93 + fbdev->dma_stats.dma_copies++;
94 +
95 + mutex_unlock(&fbdev->dma_mutex);
96 +}
97 +
98 +/* address with no aliases */
99 +#define INTALIAS_NORMAL(x) ((x) & ~0xc0000000)
100 +/* cache coherent but non-allocating in L1 and L2 */
101 +#define INTALIAS_L1L2_NONALLOCATING(x) (((x) & ~0xc0000000) | 0x80000000)
102 +
103 +static long vc_mem_copy(struct bcm2708_fb *fb, struct fb_dmacopy *ioparam)
104 +{
105 + size_t size = PAGE_SIZE;
106 + u32 *buf = NULL;
107 + dma_addr_t bus_addr;
108 + long rc = 0;
109 + size_t offset;
110 +
111 + /* restrict this to root user */
112 + if (!uid_eq(current_euid(), GLOBAL_ROOT_UID)) {
113 + rc = -EFAULT;
114 + goto out;
115 + }
116 +
117 + if (!fb->gpu.base || !fb->gpu.length) {
118 + pr_err("[%s]: Unable to determine gpu memory (%x,%x)\n",
119 + __func__, fb->gpu.base, fb->gpu.length);
120 + return -EFAULT;
121 + }
122 +
123 + if (INTALIAS_NORMAL(ioparam->src) < fb->gpu.base ||
124 + INTALIAS_NORMAL(ioparam->src) >= fb->gpu.base + fb->gpu.length) {
125 + pr_err("[%s]: Invalid memory access %x (%x-%x)", __func__,
126 + INTALIAS_NORMAL(ioparam->src), fb->gpu.base,
127 + fb->gpu.base + fb->gpu.length);
128 + return -EFAULT;
129 + }
130 +
131 + buf = dma_alloc_coherent(fb->fb.device, PAGE_ALIGN(size), &bus_addr,
132 + GFP_ATOMIC);
133 + if (!buf) {
134 + pr_err("[%s]: failed to dma_alloc_coherent(%zd)\n", __func__,
135 + size);
136 + rc = -ENOMEM;
137 + goto out;
138 + }
139 +
140 + for (offset = 0; offset < ioparam->length; offset += size) {
141 + size_t remaining = ioparam->length - offset;
142 + size_t s = min(size, remaining);
143 + u8 *p = (u8 *)((uintptr_t)ioparam->src + offset);
144 + u8 *q = (u8 *)ioparam->dst + offset;
145 +
146 + dma_memcpy(fb, bus_addr,
147 + INTALIAS_L1L2_NONALLOCATING((dma_addr_t)p), size);
148 + if (copy_to_user(q, buf, s) != 0) {
149 + pr_err("[%s]: failed to copy-to-user\n", __func__);
150 + rc = -EFAULT;
151 + goto out;
152 + }
153 + }
154 +out:
155 + if (buf)
156 + dma_free_coherent(fb->fb.device, PAGE_ALIGN(size), buf,
157 + bus_addr);
158 + return rc;
159 +}
160 +
161 static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd,
162 unsigned long arg)
163 {
164 @@ -619,6 +721,21 @@ static int bcm2708_ioctl(struct fb_info
165 RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC,
166 &dummy, sizeof(dummy));
167 break;
168 +
169 + case FBIODMACOPY:
170 + {
171 + struct fb_dmacopy ioparam;
172 + /* Get the parameter data.
173 + */
174 + if (copy_from_user
175 + (&ioparam, (void *)arg, sizeof(ioparam))) {
176 + pr_err("[%s]: failed to copy-from-user\n", __func__);
177 + ret = -EFAULT;
178 + break;
179 + }
180 + ret = vc_mem_copy(fb, &ioparam);
181 + break;
182 + }
183 default:
184 dev_dbg(info->device, "Unknown ioctl 0x%x\n", cmd);
185 return -ENOTTY;
186 @@ -629,6 +746,48 @@ static int bcm2708_ioctl(struct fb_info
187
188 return ret;
189 }
190 +
191 +#ifdef CONFIG_COMPAT
192 +struct fb_dmacopy32 {
193 + compat_uptr_t dst;
194 + __u32 src;
195 + __u32 length;
196 +};
197 +
198 +#define FBIODMACOPY32 _IOW('z', 0x22, struct fb_dmacopy32)
199 +
200 +static int bcm2708_compat_ioctl(struct fb_info *info, unsigned int cmd,
201 + unsigned long arg)
202 +{
203 + struct bcm2708_fb *fb = to_bcm2708(info);
204 + int ret;
205 +
206 + switch (cmd) {
207 + case FBIODMACOPY32:
208 + {
209 + struct fb_dmacopy32 param32;
210 + struct fb_dmacopy param;
211 + /* Get the parameter data.
212 + */
213 + if (copy_from_user(&param32, (void *)arg, sizeof(param32))) {
214 + pr_err("[%s]: failed to copy-from-user\n", __func__);
215 + ret = -EFAULT;
216 + break;
217 + }
218 + param.dst = compat_ptr(param32.dst);
219 + param.src = param32.src;
220 + param.length = param32.length;
221 + ret = vc_mem_copy(fb, &param);
222 + break;
223 + }
224 + default:
225 + ret = bcm2708_ioctl(info, cmd, arg);
226 + break;
227 + }
228 + return ret;
229 +}
230 +#endif
231 +
232 static void bcm2708_fb_fillrect(struct fb_info *info,
233 const struct fb_fillrect *rect)
234 {
235 @@ -821,6 +980,9 @@ static struct fb_ops bcm2708_fb_ops = {
236 .fb_imageblit = bcm2708_fb_imageblit,
237 .fb_pan_display = bcm2708_fb_pan_display,
238 .fb_ioctl = bcm2708_ioctl,
239 +#ifdef CONFIG_COMPAT
240 + .fb_compat_ioctl = bcm2708_compat_ioctl,
241 +#endif
242 };
243
244 static int bcm2708_fb_register(struct bcm2708_fb *fb)
245 --- a/drivers/video/fbdev/core/fbmem.c
246 +++ b/drivers/video/fbdev/core/fbmem.c
247 @@ -1085,6 +1085,30 @@ fb_blank(struct fb_info *info, int blank
248 }
249 EXPORT_SYMBOL(fb_blank);
250
251 +static int fb_copyarea_user(struct fb_info *info,
252 + struct fb_copyarea *copy)
253 +{
254 + int ret = 0;
255 + lock_fb_info(info);
256 + if (copy->dx >= info->var.xres ||
257 + copy->sx >= info->var.xres ||
258 + copy->width > info->var.xres ||
259 + copy->dy >= info->var.yres ||
260 + copy->sy >= info->var.yres ||
261 + copy->height > info->var.yres ||
262 + copy->dx + copy->width > info->var.xres ||
263 + copy->sx + copy->width > info->var.xres ||
264 + copy->dy + copy->height > info->var.yres ||
265 + copy->sy + copy->height > info->var.yres) {
266 + ret = -EINVAL;
267 + goto out;
268 + }
269 + info->fbops->fb_copyarea(info, copy);
270 +out:
271 + unlock_fb_info(info);
272 + return ret;
273 +}
274 +
275 static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
276 unsigned long arg)
277 {
278 @@ -1093,6 +1117,7 @@ static long do_fb_ioctl(struct fb_info *
279 struct fb_fix_screeninfo fix;
280 struct fb_cmap cmap_from;
281 struct fb_cmap_user cmap;
282 + struct fb_copyarea copy;
283 void __user *argp = (void __user *)arg;
284 long ret = 0;
285
286 @@ -1168,6 +1193,15 @@ static long do_fb_ioctl(struct fb_info *
287 unlock_fb_info(info);
288 console_unlock();
289 break;
290 + case FBIOCOPYAREA:
291 + if (info->flags & FBINFO_HWACCEL_COPYAREA) {
292 + /* only provide this ioctl if it is accelerated */
293 + if (copy_from_user(&copy, argp, sizeof(copy)))
294 + return -EFAULT;
295 + ret = fb_copyarea_user(info, &copy);
296 + break;
297 + }
298 + /* fall through */
299 default:
300 lock_fb_info(info);
301 fb = info->fbops;
302 @@ -1313,6 +1347,7 @@ static long fb_compat_ioctl(struct file
303 case FBIOPAN_DISPLAY:
304 case FBIOGET_CON2FBMAP:
305 case FBIOPUT_CON2FBMAP:
306 + case FBIOCOPYAREA:
307 arg = (unsigned long) compat_ptr(arg);
308 fallthrough;
309 case FBIOBLANK:
310 --- a/include/uapi/linux/fb.h
311 +++ b/include/uapi/linux/fb.h
312 @@ -35,6 +35,12 @@
313 #define FBIOPUT_MODEINFO 0x4617
314 #define FBIOGET_DISPINFO 0x4618
315 #define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32)
316 +/*
317 + * HACK: use 'z' in order not to clash with any other ioctl numbers which might
318 + * be concurrently added to the mainline kernel
319 + */
320 +#define FBIOCOPYAREA _IOW('z', 0x21, struct fb_copyarea)
321 +#define FBIODMACOPY _IOW('z', 0x22, struct fb_dmacopy)
322
323 #define FB_TYPE_PACKED_PIXELS 0 /* Packed Pixels */
324 #define FB_TYPE_PLANES 1 /* Non interleaved planes */
325 @@ -348,6 +354,12 @@ struct fb_copyarea {
326 __u32 sy;
327 };
328
329 +struct fb_dmacopy {
330 + void *dst;
331 + __u32 src;
332 + __u32 length;
333 +};
334 +
335 struct fb_fillrect {
336 __u32 dx; /* screen-relative */
337 __u32 dy;