brcm2708: add linux 4.19 support
[oweals/openwrt.git] / target / linux / brcm2708 / patches-4.19 / 950-0059-fbdev-add-FBIOCOPYAREA-ioctl.patch
1 From cdec439b6dd76c5e1ccbe49636882067971abd0d 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 059/703] 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 | 119 ++++++++++++++++++++++++++++++-
17  drivers/video/fbdev/core/fbmem.c |  36 ++++++++++
18  include/uapi/linux/fb.h          |  12 ++++
19  3 files changed, 166 insertions(+), 1 deletion(-)
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 <linux/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 @@ -95,6 +97,7 @@ struct bcm2708_fb {
35         wait_queue_head_t dma_waitq;
36         struct bcm2708_fb_stats stats;
37         unsigned long fb_bus_address;
38 +       struct { u32 base, length; } gpu;
39  };
40  
41  #define to_bcm2708(info)       container_of(info, struct bcm2708_fb, fb)
42 @@ -439,7 +442,118 @@ static int bcm2708_fb_pan_display(struct
43         return result;
44  }
45  
46 -static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
47 +static void dma_memcpy(struct bcm2708_fb *fb, dma_addr_t dst, dma_addr_t src,
48 +                      int size)
49 +{
50 +       int burst_size = (fb->dma_chan == 0) ? 8 : 2;
51 +       struct bcm2708_dma_cb *cb = fb->cb_base;
52 +
53 +       cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
54 +                  BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
55 +                  BCM2708_DMA_D_INC;
56 +       cb->dst = dst;
57 +       cb->src = src;
58 +       cb->length = size;
59 +       cb->stride = 0;
60 +       cb->pad[0] = 0;
61 +       cb->pad[1] = 0;
62 +       cb->next = 0;
63 +
64 +       if (size < dma_busy_wait_threshold) {
65 +               bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
66 +               bcm_dma_wait_idle(fb->dma_chan_base);
67 +       } else {
68 +               void __iomem *dma_chan = fb->dma_chan_base;
69 +
70 +               cb->info |= BCM2708_DMA_INT_EN;
71 +               bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
72 +               while (bcm_dma_is_busy(dma_chan)) {
73 +                       wait_event_interruptible(
74 +                               fb->dma_waitq,
75 +                               !bcm_dma_is_busy(dma_chan));
76 +               }
77 +               fb->stats.dma_irqs++;
78 +       }
79 +       fb->stats.dma_copies++;
80 +}
81 +
82 +/* address with no aliases */
83 +#define INTALIAS_NORMAL(x) ((x)&~0xc0000000)
84 +/* cache coherent but non-allocating in L1 and L2 */
85 +#define INTALIAS_L1L2_NONALLOCATING(x) (((x)&~0xc0000000)|0x80000000)
86 +
87 +static long vc_mem_copy(struct bcm2708_fb *fb, unsigned long arg)
88 +{
89 +       struct fb_dmacopy ioparam;
90 +       size_t size = PAGE_SIZE;
91 +       u32 *buf = NULL;
92 +       dma_addr_t bus_addr;
93 +       long rc = 0;
94 +       size_t offset;
95 +
96 +       /* restrict this to root user */
97 +       if (!uid_eq(current_euid(), GLOBAL_ROOT_UID)) {
98 +               rc = -EFAULT;
99 +               goto out;
100 +       }
101 +
102 +       /* Get the parameter data.
103 +        */
104 +       if (copy_from_user
105 +           (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
106 +               pr_err("[%s]: failed to copy-from-user\n",
107 +                               __func__);
108 +               rc = -EFAULT;
109 +               goto out;
110 +       }
111 +
112 +       if (fb->gpu.base == 0 || fb->gpu.length == 0) {
113 +               pr_err("[%s]: Unable to determine gpu memory (%x,%x)\n",
114 +                       __func__, fb->gpu.base, fb->gpu.length);
115 +               return -EFAULT;
116 +       }
117 +
118 +       if (INTALIAS_NORMAL(ioparam.src) < fb->gpu.base ||
119 +               INTALIAS_NORMAL(ioparam.src) >= fb->gpu.base + fb->gpu.length) {
120 +               pr_err("[%s]: Invalid memory access %x (%x-%x)", __func__,
121 +                       INTALIAS_NORMAL(ioparam.src), fb->gpu.base,
122 +                       fb->gpu.base + fb->gpu.length);
123 +               return -EFAULT;
124 +       }
125 +
126 +       buf = dma_alloc_coherent(fb->fb.device, PAGE_ALIGN(size), &bus_addr,
127 +                                GFP_ATOMIC);
128 +       if (!buf) {
129 +               pr_err("[%s]: failed to dma_alloc_coherent(%d)\n",
130 +                               __func__, size);
131 +               rc = -ENOMEM;
132 +               goto out;
133 +       }
134 +
135 +       for (offset = 0; offset < ioparam.length; offset += size) {
136 +               size_t remaining = ioparam.length - offset;
137 +               size_t s = min(size, remaining);
138 +               unsigned char *p = (unsigned char *)ioparam.src + offset;
139 +               unsigned char *q = (unsigned char *)ioparam.dst + offset;
140 +
141 +               dma_memcpy(fb, bus_addr,
142 +                          INTALIAS_L1L2_NONALLOCATING((dma_addr_t)p), size);
143 +               if (copy_to_user(q, buf, s) != 0) {
144 +                       pr_err("[%s]: failed to copy-to-user\n",
145 +                                       __func__);
146 +                       rc = -EFAULT;
147 +                       goto out;
148 +               }
149 +       }
150 +out:
151 +       if (buf)
152 +               dma_free_coherent(fb->fb.device, PAGE_ALIGN(size), buf,
153 +                                 bus_addr);
154 +       return rc;
155 +}
156 +
157 +static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd,
158 +                        unsigned long arg)
159  {
160         struct bcm2708_fb *fb = to_bcm2708(info);
161         u32 dummy = 0;
162 @@ -451,6 +565,9 @@ static int bcm2708_ioctl(struct fb_info
163                                             RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC,
164                                             &dummy, sizeof(dummy));
165                 break;
166 +       case FBIODMACOPY:
167 +               ret = vc_mem_copy(fb, arg);
168 +               break;
169         default:
170                 dev_dbg(info->device, "Unknown ioctl 0x%x\n", cmd);
171                 return -ENOTTY;
172 --- a/drivers/video/fbdev/core/fbmem.c
173 +++ b/drivers/video/fbdev/core/fbmem.c
174 @@ -1081,6 +1081,31 @@ fb_blank(struct fb_info *info, int blank
175  }
176  EXPORT_SYMBOL(fb_blank);
177  
178 +static int fb_copyarea_user(struct fb_info *info,
179 +                           struct fb_copyarea *copy)
180 +{
181 +       int ret = 0;
182 +       if (!lock_fb_info(info))
183 +               return -ENODEV;
184 +       if (copy->dx >= info->var.xres ||
185 +           copy->sx >= info->var.xres ||
186 +           copy->width > info->var.xres ||
187 +           copy->dy >= info->var.yres ||
188 +           copy->sy >= info->var.yres ||
189 +           copy->height > info->var.yres ||
190 +           copy->dx + copy->width > info->var.xres ||
191 +           copy->sx + copy->width > info->var.xres ||
192 +           copy->dy + copy->height > info->var.yres ||
193 +           copy->sy + copy->height > info->var.yres) {
194 +               ret = -EINVAL;
195 +               goto out;
196 +       }
197 +       info->fbops->fb_copyarea(info, copy);
198 +out:
199 +       unlock_fb_info(info);
200 +       return ret;
201 +}
202 +
203  static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
204                         unsigned long arg)
205  {
206 @@ -1091,6 +1116,7 @@ static long do_fb_ioctl(struct fb_info *
207         struct fb_cmap cmap_from;
208         struct fb_cmap_user cmap;
209         struct fb_event event;
210 +       struct fb_copyarea copy;
211         void __user *argp = (void __user *)arg;
212         long ret = 0;
213  
214 @@ -1208,6 +1234,15 @@ static long do_fb_ioctl(struct fb_info *
215                 unlock_fb_info(info);
216                 console_unlock();
217                 break;
218 +       case FBIOCOPYAREA:
219 +               if (info->flags & FBINFO_HWACCEL_COPYAREA) {
220 +                       /* only provide this ioctl if it is accelerated */
221 +                       if (copy_from_user(&copy, argp, sizeof(copy)))
222 +                               return -EFAULT;
223 +                       ret = fb_copyarea_user(info, &copy);
224 +                       break;
225 +               }
226 +               /* fall through */
227         default:
228                 if (!lock_fb_info(info))
229                         return -ENODEV;
230 @@ -1353,6 +1388,7 @@ static long fb_compat_ioctl(struct file
231         case FBIOPAN_DISPLAY:
232         case FBIOGET_CON2FBMAP:
233         case FBIOPUT_CON2FBMAP:
234 +       case FBIOCOPYAREA:
235                 arg = (unsigned long) compat_ptr(arg);
236                 /* fall through */
237         case FBIOBLANK:
238 --- a/include/uapi/linux/fb.h
239 +++ b/include/uapi/linux/fb.h
240 @@ -35,6 +35,12 @@
241  #define FBIOPUT_MODEINFO        0x4617
242  #define FBIOGET_DISPINFO        0x4618
243  #define FBIO_WAITFORVSYNC      _IOW('F', 0x20, __u32)
244 +/*
245 + * HACK: use 'z' in order not to clash with any other ioctl numbers which might
246 + * be concurrently added to the mainline kernel
247 + */
248 +#define FBIOCOPYAREA           _IOW('z', 0x21, struct fb_copyarea)
249 +#define FBIODMACOPY            _IOW('z', 0x22, struct fb_dmacopy)
250  
251  #define FB_TYPE_PACKED_PIXELS          0       /* Packed Pixels        */
252  #define FB_TYPE_PLANES                 1       /* Non interleaved planes */
253 @@ -347,6 +353,12 @@ struct fb_copyarea {
254         __u32 sy;
255  };
256  
257 +struct fb_dmacopy {
258 +       void *dst;
259 +       __u32 src;
260 +       __u32 length;
261 +};
262 +
263  struct fb_fillrect {
264         __u32 dx;       /* screen-relative */
265         __u32 dy;