blob: 32a489b0faff9ac87cae1fb7b899ee954b466d77 [file] [log] [blame]
Dave Airlie9a186642005-06-23 21:29:18 +10001/**
2 * \file drm_ioc32.c
3 *
4 * 32-bit ioctl compatibility routines for the DRM.
5 *
6 * \author Paul Mackerras <paulus@samba.org>
7 *
8 * Copyright (C) Paul Mackerras 2005.
9 * All Rights Reserved.
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice (including the next
19 * paragraph) shall be included in all copies or substantial portions of the
20 * Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 * IN THE SOFTWARE.
29 */
30#include <linux/compat.h>
Christian Dietrichcafe8d82011-06-04 15:36:43 +000031#include <linux/ratelimit.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040032#include <linux/export.h>
Dave Airlie9a186642005-06-23 21:29:18 +100033
David Howells760285e2012-10-02 18:01:07 +010034#include <drm/drmP.h>
35#include <drm/drm_core.h>
Dave Airlie9a186642005-06-23 21:29:18 +100036
37#define DRM_IOCTL_VERSION32 DRM_IOWR(0x00, drm_version32_t)
38#define DRM_IOCTL_GET_UNIQUE32 DRM_IOWR(0x01, drm_unique32_t)
39#define DRM_IOCTL_GET_MAP32 DRM_IOWR(0x04, drm_map32_t)
40#define DRM_IOCTL_GET_CLIENT32 DRM_IOWR(0x05, drm_client32_t)
41#define DRM_IOCTL_GET_STATS32 DRM_IOR( 0x06, drm_stats32_t)
42
43#define DRM_IOCTL_SET_UNIQUE32 DRM_IOW( 0x10, drm_unique32_t)
44#define DRM_IOCTL_ADD_MAP32 DRM_IOWR(0x15, drm_map32_t)
45#define DRM_IOCTL_ADD_BUFS32 DRM_IOWR(0x16, drm_buf_desc32_t)
46#define DRM_IOCTL_MARK_BUFS32 DRM_IOW( 0x17, drm_buf_desc32_t)
47#define DRM_IOCTL_INFO_BUFS32 DRM_IOWR(0x18, drm_buf_info32_t)
48#define DRM_IOCTL_MAP_BUFS32 DRM_IOWR(0x19, drm_buf_map32_t)
49#define DRM_IOCTL_FREE_BUFS32 DRM_IOW( 0x1a, drm_buf_free32_t)
50
51#define DRM_IOCTL_RM_MAP32 DRM_IOW( 0x1b, drm_map32_t)
52
53#define DRM_IOCTL_SET_SAREA_CTX32 DRM_IOW( 0x1c, drm_ctx_priv_map32_t)
54#define DRM_IOCTL_GET_SAREA_CTX32 DRM_IOWR(0x1d, drm_ctx_priv_map32_t)
55
56#define DRM_IOCTL_RES_CTX32 DRM_IOWR(0x26, drm_ctx_res32_t)
57#define DRM_IOCTL_DMA32 DRM_IOWR(0x29, drm_dma32_t)
58
59#define DRM_IOCTL_AGP_ENABLE32 DRM_IOW( 0x32, drm_agp_mode32_t)
60#define DRM_IOCTL_AGP_INFO32 DRM_IOR( 0x33, drm_agp_info32_t)
61#define DRM_IOCTL_AGP_ALLOC32 DRM_IOWR(0x34, drm_agp_buffer32_t)
62#define DRM_IOCTL_AGP_FREE32 DRM_IOW( 0x35, drm_agp_buffer32_t)
63#define DRM_IOCTL_AGP_BIND32 DRM_IOW( 0x36, drm_agp_binding32_t)
64#define DRM_IOCTL_AGP_UNBIND32 DRM_IOW( 0x37, drm_agp_binding32_t)
65
66#define DRM_IOCTL_SG_ALLOC32 DRM_IOW( 0x38, drm_scatter_gather32_t)
67#define DRM_IOCTL_SG_FREE32 DRM_IOW( 0x39, drm_scatter_gather32_t)
68
Eric Anholt49568872008-10-21 11:38:50 -070069#define DRM_IOCTL_UPDATE_DRAW32 DRM_IOW( 0x3f, drm_update_draw32_t)
70
Dave Airlie9a186642005-06-23 21:29:18 +100071#define DRM_IOCTL_WAIT_VBLANK32 DRM_IOWR(0x3a, drm_wait_vblank32_t)
72
Tvrtko Ursulinc631d5f2015-07-14 11:13:08 +010073#define DRM_IOCTL_MODE_ADDFB232 DRM_IOWR(0xb8, drm_mode_fb_cmd232_t)
74
Dave Airlie9a186642005-06-23 21:29:18 +100075typedef struct drm_version_32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +100076 int version_major; /**< Major version */
77 int version_minor; /**< Minor version */
78 int version_patchlevel; /**< Patch level */
79 u32 name_len; /**< Length of name buffer */
80 u32 name; /**< Name of driver */
81 u32 date_len; /**< Length of date buffer */
82 u32 date; /**< User-space buffer to hold date */
83 u32 desc_len; /**< Length of desc buffer */
84 u32 desc; /**< User-space buffer to hold desc */
Dave Airlie9a186642005-06-23 21:29:18 +100085} drm_version32_t;
86
87static int compat_drm_version(struct file *file, unsigned int cmd,
88 unsigned long arg)
89{
90 drm_version32_t v32;
Dave Airlieff4135a2007-07-16 13:53:57 +100091 struct drm_version __user *version;
Dave Airlie9a186642005-06-23 21:29:18 +100092 int err;
93
Dave Airlieb5e89ed2005-09-25 14:28:13 +100094 if (copy_from_user(&v32, (void __user *)arg, sizeof(v32)))
Dave Airlie9a186642005-06-23 21:29:18 +100095 return -EFAULT;
96
97 version = compat_alloc_user_space(sizeof(*version));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +030098 if (!version)
Dave Airlie9a186642005-06-23 21:29:18 +100099 return -EFAULT;
100 if (__put_user(v32.name_len, &version->name_len)
101 || __put_user((void __user *)(unsigned long)v32.name,
102 &version->name)
103 || __put_user(v32.date_len, &version->date_len)
104 || __put_user((void __user *)(unsigned long)v32.date,
105 &version->date)
106 || __put_user(v32.desc_len, &version->desc_len)
107 || __put_user((void __user *)(unsigned long)v32.desc,
108 &version->desc))
109 return -EFAULT;
110
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000111 err = drm_ioctl(file,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000112 DRM_IOCTL_VERSION, (unsigned long)version);
Dave Airlie9a186642005-06-23 21:29:18 +1000113 if (err)
114 return err;
115
116 if (__get_user(v32.version_major, &version->version_major)
117 || __get_user(v32.version_minor, &version->version_minor)
118 || __get_user(v32.version_patchlevel, &version->version_patchlevel)
119 || __get_user(v32.name_len, &version->name_len)
120 || __get_user(v32.date_len, &version->date_len)
121 || __get_user(v32.desc_len, &version->desc_len))
122 return -EFAULT;
123
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000124 if (copy_to_user((void __user *)arg, &v32, sizeof(v32)))
Dave Airlie9a186642005-06-23 21:29:18 +1000125 return -EFAULT;
126 return 0;
127}
128
129typedef struct drm_unique32 {
130 u32 unique_len; /**< Length of unique */
131 u32 unique; /**< Unique name for driver instantiation */
132} drm_unique32_t;
133
134static int compat_drm_getunique(struct file *file, unsigned int cmd,
135 unsigned long arg)
136{
137 drm_unique32_t uq32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000138 struct drm_unique __user *u;
Dave Airlie9a186642005-06-23 21:29:18 +1000139 int err;
140
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000141 if (copy_from_user(&uq32, (void __user *)arg, sizeof(uq32)))
Dave Airlie9a186642005-06-23 21:29:18 +1000142 return -EFAULT;
143
144 u = compat_alloc_user_space(sizeof(*u));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300145 if (!u)
Dave Airlie9a186642005-06-23 21:29:18 +1000146 return -EFAULT;
147 if (__put_user(uq32.unique_len, &u->unique_len)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000148 || __put_user((void __user *)(unsigned long)uq32.unique,
Dave Airlie9a186642005-06-23 21:29:18 +1000149 &u->unique))
150 return -EFAULT;
151
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000152 err = drm_ioctl(file, DRM_IOCTL_GET_UNIQUE, (unsigned long)u);
Dave Airlie9a186642005-06-23 21:29:18 +1000153 if (err)
154 return err;
155
156 if (__get_user(uq32.unique_len, &u->unique_len))
157 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000158 if (copy_to_user((void __user *)arg, &uq32, sizeof(uq32)))
Dave Airlie9a186642005-06-23 21:29:18 +1000159 return -EFAULT;
160 return 0;
161}
162
163static int compat_drm_setunique(struct file *file, unsigned int cmd,
164 unsigned long arg)
165{
166 drm_unique32_t uq32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000167 struct drm_unique __user *u;
Dave Airlie9a186642005-06-23 21:29:18 +1000168
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000169 if (copy_from_user(&uq32, (void __user *)arg, sizeof(uq32)))
Dave Airlie9a186642005-06-23 21:29:18 +1000170 return -EFAULT;
171
172 u = compat_alloc_user_space(sizeof(*u));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300173 if (!u)
Dave Airlie9a186642005-06-23 21:29:18 +1000174 return -EFAULT;
175 if (__put_user(uq32.unique_len, &u->unique_len)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000176 || __put_user((void __user *)(unsigned long)uq32.unique,
Dave Airlie9a186642005-06-23 21:29:18 +1000177 &u->unique))
178 return -EFAULT;
179
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000180 return drm_ioctl(file, DRM_IOCTL_SET_UNIQUE, (unsigned long)u);
Dave Airlie9a186642005-06-23 21:29:18 +1000181}
182
183typedef struct drm_map32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000184 u32 offset; /**< Requested physical address (0 for SAREA)*/
185 u32 size; /**< Requested physical size (bytes) */
Dave Airlieff4135a2007-07-16 13:53:57 +1000186 enum drm_map_type type; /**< Type of memory to map */
187 enum drm_map_flags flags; /**< Flags */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000188 u32 handle; /**< User-space: "Handle" to pass to mmap() */
189 int mtrr; /**< MTRR slot used */
Dave Airlie9a186642005-06-23 21:29:18 +1000190} drm_map32_t;
191
192static int compat_drm_getmap(struct file *file, unsigned int cmd,
193 unsigned long arg)
194{
195 drm_map32_t __user *argp = (void __user *)arg;
196 drm_map32_t m32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000197 struct drm_map __user *map;
Dave Airlie9a186642005-06-23 21:29:18 +1000198 int idx, err;
199 void *handle;
200
201 if (get_user(idx, &argp->offset))
202 return -EFAULT;
203
204 map = compat_alloc_user_space(sizeof(*map));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300205 if (!map)
Dave Airlie9a186642005-06-23 21:29:18 +1000206 return -EFAULT;
207 if (__put_user(idx, &map->offset))
208 return -EFAULT;
209
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000210 err = drm_ioctl(file, DRM_IOCTL_GET_MAP, (unsigned long)map);
Dave Airlie9a186642005-06-23 21:29:18 +1000211 if (err)
212 return err;
213
214 if (__get_user(m32.offset, &map->offset)
215 || __get_user(m32.size, &map->size)
216 || __get_user(m32.type, &map->type)
217 || __get_user(m32.flags, &map->flags)
218 || __get_user(handle, &map->handle)
219 || __get_user(m32.mtrr, &map->mtrr))
220 return -EFAULT;
221
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000222 m32.handle = (unsigned long)handle;
Dave Airlie9a186642005-06-23 21:29:18 +1000223 if (copy_to_user(argp, &m32, sizeof(m32)))
224 return -EFAULT;
225 return 0;
226
227}
228
229static int compat_drm_addmap(struct file *file, unsigned int cmd,
230 unsigned long arg)
231{
232 drm_map32_t __user *argp = (void __user *)arg;
233 drm_map32_t m32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000234 struct drm_map __user *map;
Dave Airlie9a186642005-06-23 21:29:18 +1000235 int err;
236 void *handle;
237
238 if (copy_from_user(&m32, argp, sizeof(m32)))
239 return -EFAULT;
240
241 map = compat_alloc_user_space(sizeof(*map));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300242 if (!map)
Dave Airlie9a186642005-06-23 21:29:18 +1000243 return -EFAULT;
244 if (__put_user(m32.offset, &map->offset)
245 || __put_user(m32.size, &map->size)
246 || __put_user(m32.type, &map->type)
247 || __put_user(m32.flags, &map->flags))
248 return -EFAULT;
249
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000250 err = drm_ioctl(file, DRM_IOCTL_ADD_MAP, (unsigned long)map);
Dave Airlie9a186642005-06-23 21:29:18 +1000251 if (err)
252 return err;
253
254 if (__get_user(m32.offset, &map->offset)
255 || __get_user(m32.mtrr, &map->mtrr)
256 || __get_user(handle, &map->handle))
257 return -EFAULT;
258
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000259 m32.handle = (unsigned long)handle;
Christian Dietrichcafe8d82011-06-04 15:36:43 +0000260 if (m32.handle != (unsigned long)handle)
261 printk_ratelimited(KERN_ERR "compat_drm_addmap truncated handle"
262 " %p for type %d offset %x\n",
263 handle, m32.type, m32.offset);
Dave Airlie9a186642005-06-23 21:29:18 +1000264
265 if (copy_to_user(argp, &m32, sizeof(m32)))
266 return -EFAULT;
267
268 return 0;
269}
270
271static int compat_drm_rmmap(struct file *file, unsigned int cmd,
272 unsigned long arg)
273{
274 drm_map32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000275 struct drm_map __user *map;
Dave Airlie9a186642005-06-23 21:29:18 +1000276 u32 handle;
277
278 if (get_user(handle, &argp->handle))
279 return -EFAULT;
280
281 map = compat_alloc_user_space(sizeof(*map));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300282 if (!map)
Dave Airlie9a186642005-06-23 21:29:18 +1000283 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000284 if (__put_user((void *)(unsigned long)handle, &map->handle))
Dave Airlie9a186642005-06-23 21:29:18 +1000285 return -EFAULT;
286
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000287 return drm_ioctl(file, DRM_IOCTL_RM_MAP, (unsigned long)map);
Dave Airlie9a186642005-06-23 21:29:18 +1000288}
289
290typedef struct drm_client32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000291 int idx; /**< Which client desired? */
292 int auth; /**< Is client authenticated? */
293 u32 pid; /**< Process ID */
294 u32 uid; /**< User ID */
295 u32 magic; /**< Magic */
296 u32 iocs; /**< Ioctl count */
Dave Airlie9a186642005-06-23 21:29:18 +1000297} drm_client32_t;
298
299static int compat_drm_getclient(struct file *file, unsigned int cmd,
300 unsigned long arg)
301{
302 drm_client32_t c32;
303 drm_client32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000304 struct drm_client __user *client;
Dave Airlie9a186642005-06-23 21:29:18 +1000305 int idx, err;
306
307 if (get_user(idx, &argp->idx))
308 return -EFAULT;
309
310 client = compat_alloc_user_space(sizeof(*client));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300311 if (!client)
Dave Airlie9a186642005-06-23 21:29:18 +1000312 return -EFAULT;
313 if (__put_user(idx, &client->idx))
314 return -EFAULT;
315
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000316 err = drm_ioctl(file, DRM_IOCTL_GET_CLIENT, (unsigned long)client);
Dave Airlie9a186642005-06-23 21:29:18 +1000317 if (err)
318 return err;
319
Julia Lawall08bc3d42012-01-30 12:05:55 +0100320 if (__get_user(c32.idx, &client->idx)
321 || __get_user(c32.auth, &client->auth)
Dave Airlie9a186642005-06-23 21:29:18 +1000322 || __get_user(c32.pid, &client->pid)
323 || __get_user(c32.uid, &client->uid)
324 || __get_user(c32.magic, &client->magic)
325 || __get_user(c32.iocs, &client->iocs))
326 return -EFAULT;
327
328 if (copy_to_user(argp, &c32, sizeof(c32)))
329 return -EFAULT;
330 return 0;
331}
332
333typedef struct drm_stats32 {
334 u32 count;
335 struct {
336 u32 value;
Dave Airlieff4135a2007-07-16 13:53:57 +1000337 enum drm_stat_type type;
Dave Airlie9a186642005-06-23 21:29:18 +1000338 } data[15];
339} drm_stats32_t;
340
341static int compat_drm_getstats(struct file *file, unsigned int cmd,
342 unsigned long arg)
343{
344 drm_stats32_t s32;
345 drm_stats32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000346 struct drm_stats __user *stats;
Dave Airlie9a186642005-06-23 21:29:18 +1000347 int i, err;
348
Heinrich Schuchardtec913f32016-08-21 19:56:19 +0200349 memset(&s32, 0, sizeof(drm_stats32_t));
Dave Airlie9a186642005-06-23 21:29:18 +1000350 stats = compat_alloc_user_space(sizeof(*stats));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300351 if (!stats)
Dave Airlie9a186642005-06-23 21:29:18 +1000352 return -EFAULT;
353
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000354 err = drm_ioctl(file, DRM_IOCTL_GET_STATS, (unsigned long)stats);
Dave Airlie9a186642005-06-23 21:29:18 +1000355 if (err)
356 return err;
357
358 if (__get_user(s32.count, &stats->count))
359 return -EFAULT;
360 for (i = 0; i < 15; ++i)
361 if (__get_user(s32.data[i].value, &stats->data[i].value)
362 || __get_user(s32.data[i].type, &stats->data[i].type))
363 return -EFAULT;
364
365 if (copy_to_user(argp, &s32, sizeof(s32)))
366 return -EFAULT;
367 return 0;
368}
369
370typedef struct drm_buf_desc32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000371 int count; /**< Number of buffers of this size */
372 int size; /**< Size in bytes */
373 int low_mark; /**< Low water mark */
374 int high_mark; /**< High water mark */
375 int flags;
376 u32 agp_start; /**< Start address in the AGP aperture */
Dave Airlie9a186642005-06-23 21:29:18 +1000377} drm_buf_desc32_t;
378
379static int compat_drm_addbufs(struct file *file, unsigned int cmd,
380 unsigned long arg)
381{
382 drm_buf_desc32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000383 struct drm_buf_desc __user *buf;
Dave Airlie9a186642005-06-23 21:29:18 +1000384 int err;
385 unsigned long agp_start;
386
387 buf = compat_alloc_user_space(sizeof(*buf));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300388 if (!buf || !access_ok(VERIFY_WRITE, argp, sizeof(*argp)))
Dave Airlie9a186642005-06-23 21:29:18 +1000389 return -EFAULT;
390
391 if (__copy_in_user(buf, argp, offsetof(drm_buf_desc32_t, agp_start))
392 || __get_user(agp_start, &argp->agp_start)
393 || __put_user(agp_start, &buf->agp_start))
394 return -EFAULT;
395
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000396 err = drm_ioctl(file, DRM_IOCTL_ADD_BUFS, (unsigned long)buf);
Dave Airlie9a186642005-06-23 21:29:18 +1000397 if (err)
398 return err;
399
400 if (__copy_in_user(argp, buf, offsetof(drm_buf_desc32_t, agp_start))
401 || __get_user(agp_start, &buf->agp_start)
402 || __put_user(agp_start, &argp->agp_start))
403 return -EFAULT;
404
405 return 0;
406}
407
408static int compat_drm_markbufs(struct file *file, unsigned int cmd,
409 unsigned long arg)
410{
411 drm_buf_desc32_t b32;
412 drm_buf_desc32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000413 struct drm_buf_desc __user *buf;
Dave Airlie9a186642005-06-23 21:29:18 +1000414
415 if (copy_from_user(&b32, argp, sizeof(b32)))
416 return -EFAULT;
417
418 buf = compat_alloc_user_space(sizeof(*buf));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300419 if (!buf)
Dave Airlie9a186642005-06-23 21:29:18 +1000420 return -EFAULT;
421
422 if (__put_user(b32.size, &buf->size)
423 || __put_user(b32.low_mark, &buf->low_mark)
424 || __put_user(b32.high_mark, &buf->high_mark))
425 return -EFAULT;
426
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000427 return drm_ioctl(file, DRM_IOCTL_MARK_BUFS, (unsigned long)buf);
Dave Airlie9a186642005-06-23 21:29:18 +1000428}
429
430typedef struct drm_buf_info32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000431 int count; /**< Entries in list */
432 u32 list;
Dave Airlie9a186642005-06-23 21:29:18 +1000433} drm_buf_info32_t;
434
435static int compat_drm_infobufs(struct file *file, unsigned int cmd,
436 unsigned long arg)
437{
438 drm_buf_info32_t req32;
439 drm_buf_info32_t __user *argp = (void __user *)arg;
440 drm_buf_desc32_t __user *to;
Dave Airlieff4135a2007-07-16 13:53:57 +1000441 struct drm_buf_info __user *request;
442 struct drm_buf_desc __user *list;
Dave Airlie9a186642005-06-23 21:29:18 +1000443 size_t nbytes;
444 int i, err;
445 int count, actual;
446
447 if (copy_from_user(&req32, argp, sizeof(req32)))
448 return -EFAULT;
449
450 count = req32.count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000451 to = (drm_buf_desc32_t __user *) (unsigned long)req32.list;
Dave Airlie9a186642005-06-23 21:29:18 +1000452 if (count < 0)
453 count = 0;
454 if (count > 0
455 && !access_ok(VERIFY_WRITE, to, count * sizeof(drm_buf_desc32_t)))
456 return -EFAULT;
457
Dave Airlieff4135a2007-07-16 13:53:57 +1000458 nbytes = sizeof(*request) + count * sizeof(struct drm_buf_desc);
Dave Airlie9a186642005-06-23 21:29:18 +1000459 request = compat_alloc_user_space(nbytes);
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300460 if (!request)
Dave Airlie9a186642005-06-23 21:29:18 +1000461 return -EFAULT;
Dave Airlieff4135a2007-07-16 13:53:57 +1000462 list = (struct drm_buf_desc *) (request + 1);
Dave Airlie9a186642005-06-23 21:29:18 +1000463
464 if (__put_user(count, &request->count)
465 || __put_user(list, &request->list))
466 return -EFAULT;
467
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000468 err = drm_ioctl(file, DRM_IOCTL_INFO_BUFS, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000469 if (err)
470 return err;
471
472 if (__get_user(actual, &request->count))
473 return -EFAULT;
474 if (count >= actual)
475 for (i = 0; i < actual; ++i)
476 if (__copy_in_user(&to[i], &list[i],
Dave Airlieff4135a2007-07-16 13:53:57 +1000477 offsetof(struct drm_buf_desc, flags)))
Dave Airlie9a186642005-06-23 21:29:18 +1000478 return -EFAULT;
479
480 if (__put_user(actual, &argp->count))
481 return -EFAULT;
482
483 return 0;
484}
485
486typedef struct drm_buf_pub32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000487 int idx; /**< Index into the master buffer list */
488 int total; /**< Buffer size */
489 int used; /**< Amount of buffer in use (for DMA) */
490 u32 address; /**< Address of buffer */
Dave Airlie9a186642005-06-23 21:29:18 +1000491} drm_buf_pub32_t;
492
493typedef struct drm_buf_map32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000494 int count; /**< Length of the buffer list */
495 u32 virtual; /**< Mmap'd area in user-virtual */
496 u32 list; /**< Buffer information */
Dave Airlie9a186642005-06-23 21:29:18 +1000497} drm_buf_map32_t;
498
499static int compat_drm_mapbufs(struct file *file, unsigned int cmd,
500 unsigned long arg)
501{
502 drm_buf_map32_t __user *argp = (void __user *)arg;
503 drm_buf_map32_t req32;
504 drm_buf_pub32_t __user *list32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000505 struct drm_buf_map __user *request;
506 struct drm_buf_pub __user *list;
Dave Airlie9a186642005-06-23 21:29:18 +1000507 int i, err;
508 int count, actual;
509 size_t nbytes;
510 void __user *addr;
511
512 if (copy_from_user(&req32, argp, sizeof(req32)))
513 return -EFAULT;
514 count = req32.count;
515 list32 = (void __user *)(unsigned long)req32.list;
516
517 if (count < 0)
518 return -EINVAL;
Dave Airlieff4135a2007-07-16 13:53:57 +1000519 nbytes = sizeof(*request) + count * sizeof(struct drm_buf_pub);
Dave Airlie9a186642005-06-23 21:29:18 +1000520 request = compat_alloc_user_space(nbytes);
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300521 if (!request)
Dave Airlie9a186642005-06-23 21:29:18 +1000522 return -EFAULT;
Dave Airlieff4135a2007-07-16 13:53:57 +1000523 list = (struct drm_buf_pub *) (request + 1);
Dave Airlie9a186642005-06-23 21:29:18 +1000524
525 if (__put_user(count, &request->count)
526 || __put_user(list, &request->list))
527 return -EFAULT;
528
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000529 err = drm_ioctl(file, DRM_IOCTL_MAP_BUFS, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000530 if (err)
531 return err;
532
533 if (__get_user(actual, &request->count))
534 return -EFAULT;
535 if (count >= actual)
536 for (i = 0; i < actual; ++i)
537 if (__copy_in_user(&list32[i], &list[i],
Dave Airlieff4135a2007-07-16 13:53:57 +1000538 offsetof(struct drm_buf_pub, address))
Dave Airlie9a186642005-06-23 21:29:18 +1000539 || __get_user(addr, &list[i].address)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000540 || __put_user((unsigned long)addr,
Dave Airlie9a186642005-06-23 21:29:18 +1000541 &list32[i].address))
542 return -EFAULT;
543
544 if (__put_user(actual, &argp->count)
545 || __get_user(addr, &request->virtual)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000546 || __put_user((unsigned long)addr, &argp->virtual))
Dave Airlie9a186642005-06-23 21:29:18 +1000547 return -EFAULT;
548
549 return 0;
550}
551
552typedef struct drm_buf_free32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000553 int count;
554 u32 list;
Dave Airlie9a186642005-06-23 21:29:18 +1000555} drm_buf_free32_t;
556
557static int compat_drm_freebufs(struct file *file, unsigned int cmd,
558 unsigned long arg)
559{
560 drm_buf_free32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000561 struct drm_buf_free __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000562 drm_buf_free32_t __user *argp = (void __user *)arg;
563
564 if (copy_from_user(&req32, argp, sizeof(req32)))
565 return -EFAULT;
566
567 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300568 if (!request)
Dave Airlie9a186642005-06-23 21:29:18 +1000569 return -EFAULT;
570 if (__put_user(req32.count, &request->count)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000571 || __put_user((int __user *)(unsigned long)req32.list,
Dave Airlie9a186642005-06-23 21:29:18 +1000572 &request->list))
573 return -EFAULT;
574
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000575 return drm_ioctl(file, DRM_IOCTL_FREE_BUFS, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000576}
577
578typedef struct drm_ctx_priv_map32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000579 unsigned int ctx_id; /**< Context requesting private mapping */
580 u32 handle; /**< Handle of map */
Dave Airlie9a186642005-06-23 21:29:18 +1000581} drm_ctx_priv_map32_t;
582
583static int compat_drm_setsareactx(struct file *file, unsigned int cmd,
584 unsigned long arg)
585{
586 drm_ctx_priv_map32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000587 struct drm_ctx_priv_map __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000588 drm_ctx_priv_map32_t __user *argp = (void __user *)arg;
589
590 if (copy_from_user(&req32, argp, sizeof(req32)))
591 return -EFAULT;
592
593 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300594 if (!request)
Dave Airlie9a186642005-06-23 21:29:18 +1000595 return -EFAULT;
596 if (__put_user(req32.ctx_id, &request->ctx_id)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000597 || __put_user((void *)(unsigned long)req32.handle,
Dave Airlie9a186642005-06-23 21:29:18 +1000598 &request->handle))
599 return -EFAULT;
600
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000601 return drm_ioctl(file, DRM_IOCTL_SET_SAREA_CTX, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000602}
603
604static int compat_drm_getsareactx(struct file *file, unsigned int cmd,
605 unsigned long arg)
606{
Dave Airlieff4135a2007-07-16 13:53:57 +1000607 struct drm_ctx_priv_map __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000608 drm_ctx_priv_map32_t __user *argp = (void __user *)arg;
609 int err;
610 unsigned int ctx_id;
611 void *handle;
612
613 if (!access_ok(VERIFY_WRITE, argp, sizeof(*argp))
614 || __get_user(ctx_id, &argp->ctx_id))
615 return -EFAULT;
616
617 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300618 if (!request)
Dave Airlie9a186642005-06-23 21:29:18 +1000619 return -EFAULT;
620 if (__put_user(ctx_id, &request->ctx_id))
621 return -EFAULT;
622
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000623 err = drm_ioctl(file, DRM_IOCTL_GET_SAREA_CTX, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000624 if (err)
625 return err;
626
627 if (__get_user(handle, &request->handle)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000628 || __put_user((unsigned long)handle, &argp->handle))
Dave Airlie9a186642005-06-23 21:29:18 +1000629 return -EFAULT;
630
631 return 0;
632}
633
634typedef struct drm_ctx_res32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000635 int count;
636 u32 contexts;
Dave Airlie9a186642005-06-23 21:29:18 +1000637} drm_ctx_res32_t;
638
639static int compat_drm_resctx(struct file *file, unsigned int cmd,
640 unsigned long arg)
641{
642 drm_ctx_res32_t __user *argp = (void __user *)arg;
643 drm_ctx_res32_t res32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000644 struct drm_ctx_res __user *res;
Dave Airlie9a186642005-06-23 21:29:18 +1000645 int err;
646
647 if (copy_from_user(&res32, argp, sizeof(res32)))
648 return -EFAULT;
649
650 res = compat_alloc_user_space(sizeof(*res));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300651 if (!res)
Dave Airlie9a186642005-06-23 21:29:18 +1000652 return -EFAULT;
653 if (__put_user(res32.count, &res->count)
Dave Airlieff4135a2007-07-16 13:53:57 +1000654 || __put_user((struct drm_ctx __user *) (unsigned long)res32.contexts,
Dave Airlie9a186642005-06-23 21:29:18 +1000655 &res->contexts))
656 return -EFAULT;
657
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000658 err = drm_ioctl(file, DRM_IOCTL_RES_CTX, (unsigned long)res);
Dave Airlie9a186642005-06-23 21:29:18 +1000659 if (err)
660 return err;
661
662 if (__get_user(res32.count, &res->count)
663 || __put_user(res32.count, &argp->count))
664 return -EFAULT;
665
666 return 0;
667}
668
669typedef struct drm_dma32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000670 int context; /**< Context handle */
671 int send_count; /**< Number of buffers to send */
672 u32 send_indices; /**< List of handles to buffers */
673 u32 send_sizes; /**< Lengths of data to send */
Dave Airlieff4135a2007-07-16 13:53:57 +1000674 enum drm_dma_flags flags; /**< Flags */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000675 int request_count; /**< Number of buffers requested */
676 int request_size; /**< Desired size for buffers */
677 u32 request_indices; /**< Buffer information */
678 u32 request_sizes;
679 int granted_count; /**< Number of buffers granted */
Dave Airlie9a186642005-06-23 21:29:18 +1000680} drm_dma32_t;
681
682static int compat_drm_dma(struct file *file, unsigned int cmd,
683 unsigned long arg)
684{
685 drm_dma32_t d32;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000686 drm_dma32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000687 struct drm_dma __user *d;
Dave Airlie9a186642005-06-23 21:29:18 +1000688 int err;
689
690 if (copy_from_user(&d32, argp, sizeof(d32)))
691 return -EFAULT;
692
693 d = compat_alloc_user_space(sizeof(*d));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300694 if (!d)
Dave Airlie9a186642005-06-23 21:29:18 +1000695 return -EFAULT;
696
697 if (__put_user(d32.context, &d->context)
698 || __put_user(d32.send_count, &d->send_count)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000699 || __put_user((int __user *)(unsigned long)d32.send_indices,
Dave Airlie9a186642005-06-23 21:29:18 +1000700 &d->send_indices)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000701 || __put_user((int __user *)(unsigned long)d32.send_sizes,
Dave Airlie9a186642005-06-23 21:29:18 +1000702 &d->send_sizes)
703 || __put_user(d32.flags, &d->flags)
704 || __put_user(d32.request_count, &d->request_count)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000705 || __put_user((int __user *)(unsigned long)d32.request_indices,
Dave Airlie9a186642005-06-23 21:29:18 +1000706 &d->request_indices)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000707 || __put_user((int __user *)(unsigned long)d32.request_sizes,
Dave Airlie9a186642005-06-23 21:29:18 +1000708 &d->request_sizes))
709 return -EFAULT;
710
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000711 err = drm_ioctl(file, DRM_IOCTL_DMA, (unsigned long)d);
Dave Airlie9a186642005-06-23 21:29:18 +1000712 if (err)
713 return err;
714
715 if (__get_user(d32.request_size, &d->request_size)
716 || __get_user(d32.granted_count, &d->granted_count)
717 || __put_user(d32.request_size, &argp->request_size)
718 || __put_user(d32.granted_count, &argp->granted_count))
719 return -EFAULT;
720
721 return 0;
722}
723
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200724#if IS_ENABLED(CONFIG_AGP)
Dave Airlie9a186642005-06-23 21:29:18 +1000725typedef struct drm_agp_mode32 {
726 u32 mode; /**< AGP mode */
727} drm_agp_mode32_t;
728
729static int compat_drm_agp_enable(struct file *file, unsigned int cmd,
730 unsigned long arg)
731{
732 drm_agp_mode32_t __user *argp = (void __user *)arg;
733 drm_agp_mode32_t m32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000734 struct drm_agp_mode __user *mode;
Dave Airlie9a186642005-06-23 21:29:18 +1000735
736 if (get_user(m32.mode, &argp->mode))
737 return -EFAULT;
738
739 mode = compat_alloc_user_space(sizeof(*mode));
740 if (put_user(m32.mode, &mode->mode))
741 return -EFAULT;
742
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000743 return drm_ioctl(file, DRM_IOCTL_AGP_ENABLE, (unsigned long)mode);
Dave Airlie9a186642005-06-23 21:29:18 +1000744}
745
746typedef struct drm_agp_info32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000747 int agp_version_major;
748 int agp_version_minor;
749 u32 mode;
750 u32 aperture_base; /* physical address */
751 u32 aperture_size; /* bytes */
752 u32 memory_allowed; /* bytes */
753 u32 memory_used;
Dave Airlie9a186642005-06-23 21:29:18 +1000754
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000755 /* PCI information */
Dave Airlie9a186642005-06-23 21:29:18 +1000756 unsigned short id_vendor;
757 unsigned short id_device;
758} drm_agp_info32_t;
759
760static int compat_drm_agp_info(struct file *file, unsigned int cmd,
761 unsigned long arg)
762{
763 drm_agp_info32_t __user *argp = (void __user *)arg;
764 drm_agp_info32_t i32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000765 struct drm_agp_info __user *info;
Dave Airlie9a186642005-06-23 21:29:18 +1000766 int err;
767
768 info = compat_alloc_user_space(sizeof(*info));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300769 if (!info)
Dave Airlie9a186642005-06-23 21:29:18 +1000770 return -EFAULT;
771
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000772 err = drm_ioctl(file, DRM_IOCTL_AGP_INFO, (unsigned long)info);
Dave Airlie9a186642005-06-23 21:29:18 +1000773 if (err)
774 return err;
775
776 if (__get_user(i32.agp_version_major, &info->agp_version_major)
777 || __get_user(i32.agp_version_minor, &info->agp_version_minor)
778 || __get_user(i32.mode, &info->mode)
779 || __get_user(i32.aperture_base, &info->aperture_base)
780 || __get_user(i32.aperture_size, &info->aperture_size)
781 || __get_user(i32.memory_allowed, &info->memory_allowed)
782 || __get_user(i32.memory_used, &info->memory_used)
783 || __get_user(i32.id_vendor, &info->id_vendor)
784 || __get_user(i32.id_device, &info->id_device))
785 return -EFAULT;
786
787 if (copy_to_user(argp, &i32, sizeof(i32)))
788 return -EFAULT;
789
790 return 0;
791}
792
793typedef struct drm_agp_buffer32 {
794 u32 size; /**< In bytes -- will round to page boundary */
795 u32 handle; /**< Used for binding / unbinding */
796 u32 type; /**< Type of memory to allocate */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000797 u32 physical; /**< Physical used by i810 */
Dave Airlie9a186642005-06-23 21:29:18 +1000798} drm_agp_buffer32_t;
799
800static int compat_drm_agp_alloc(struct file *file, unsigned int cmd,
801 unsigned long arg)
802{
803 drm_agp_buffer32_t __user *argp = (void __user *)arg;
804 drm_agp_buffer32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000805 struct drm_agp_buffer __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000806 int err;
807
808 if (copy_from_user(&req32, argp, sizeof(req32)))
809 return -EFAULT;
810
811 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300812 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000813 || __put_user(req32.size, &request->size)
814 || __put_user(req32.type, &request->type))
815 return -EFAULT;
816
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000817 err = drm_ioctl(file, DRM_IOCTL_AGP_ALLOC, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000818 if (err)
819 return err;
820
821 if (__get_user(req32.handle, &request->handle)
822 || __get_user(req32.physical, &request->physical)
823 || copy_to_user(argp, &req32, sizeof(req32))) {
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000824 drm_ioctl(file, DRM_IOCTL_AGP_FREE, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000825 return -EFAULT;
826 }
827
828 return 0;
829}
830
831static int compat_drm_agp_free(struct file *file, unsigned int cmd,
832 unsigned long arg)
833{
834 drm_agp_buffer32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000835 struct drm_agp_buffer __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000836 u32 handle;
837
838 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300839 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000840 || get_user(handle, &argp->handle)
841 || __put_user(handle, &request->handle))
842 return -EFAULT;
843
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000844 return drm_ioctl(file, DRM_IOCTL_AGP_FREE, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000845}
846
847typedef struct drm_agp_binding32 {
848 u32 handle; /**< From drm_agp_buffer */
849 u32 offset; /**< In bytes -- will round to page boundary */
850} drm_agp_binding32_t;
851
852static int compat_drm_agp_bind(struct file *file, unsigned int cmd,
853 unsigned long arg)
854{
855 drm_agp_binding32_t __user *argp = (void __user *)arg;
856 drm_agp_binding32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000857 struct drm_agp_binding __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000858
859 if (copy_from_user(&req32, argp, sizeof(req32)))
860 return -EFAULT;
861
862 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300863 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000864 || __put_user(req32.handle, &request->handle)
865 || __put_user(req32.offset, &request->offset))
866 return -EFAULT;
867
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000868 return drm_ioctl(file, DRM_IOCTL_AGP_BIND, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000869}
870
871static int compat_drm_agp_unbind(struct file *file, unsigned int cmd,
872 unsigned long arg)
873{
874 drm_agp_binding32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000875 struct drm_agp_binding __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000876 u32 handle;
877
878 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300879 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000880 || get_user(handle, &argp->handle)
881 || __put_user(handle, &request->handle))
882 return -EFAULT;
883
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000884 return drm_ioctl(file, DRM_IOCTL_AGP_UNBIND, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000885}
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200886#endif /* CONFIG_AGP */
Dave Airlie9a186642005-06-23 21:29:18 +1000887
888typedef struct drm_scatter_gather32 {
889 u32 size; /**< In bytes -- will round to page boundary */
890 u32 handle; /**< Used for mapping / unmapping */
891} drm_scatter_gather32_t;
892
893static int compat_drm_sg_alloc(struct file *file, unsigned int cmd,
894 unsigned long arg)
895{
896 drm_scatter_gather32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000897 struct drm_scatter_gather __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000898 int err;
899 unsigned long x;
900
901 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300902 if (!request || !access_ok(VERIFY_WRITE, argp, sizeof(*argp))
Dave Airlie9a186642005-06-23 21:29:18 +1000903 || __get_user(x, &argp->size)
904 || __put_user(x, &request->size))
905 return -EFAULT;
906
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000907 err = drm_ioctl(file, DRM_IOCTL_SG_ALLOC, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000908 if (err)
909 return err;
910
911 /* XXX not sure about the handle conversion here... */
912 if (__get_user(x, &request->handle)
913 || __put_user(x >> PAGE_SHIFT, &argp->handle))
914 return -EFAULT;
915
916 return 0;
917}
918
919static int compat_drm_sg_free(struct file *file, unsigned int cmd,
920 unsigned long arg)
921{
922 drm_scatter_gather32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000923 struct drm_scatter_gather __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000924 unsigned long x;
925
926 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300927 if (!request || !access_ok(VERIFY_WRITE, argp, sizeof(*argp))
Dave Airlie9a186642005-06-23 21:29:18 +1000928 || __get_user(x, &argp->handle)
929 || __put_user(x << PAGE_SHIFT, &request->handle))
930 return -EFAULT;
931
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000932 return drm_ioctl(file, DRM_IOCTL_SG_FREE, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000933}
934
David Miller09e40d62009-02-18 01:35:21 -0800935#if defined(CONFIG_X86) || defined(CONFIG_IA64)
Eric Anholt49568872008-10-21 11:38:50 -0700936typedef struct drm_update_draw32 {
937 drm_drawable_t handle;
938 unsigned int type;
939 unsigned int num;
940 /* 64-bit version has a 32-bit pad here */
941 u64 data; /**< Pointer */
942} __attribute__((packed)) drm_update_draw32_t;
943
944static int compat_drm_update_draw(struct file *file, unsigned int cmd,
945 unsigned long arg)
946{
947 drm_update_draw32_t update32;
948 struct drm_update_draw __user *request;
949 int err;
950
951 if (copy_from_user(&update32, (void __user *)arg, sizeof(update32)))
952 return -EFAULT;
953
954 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300955 if (!request ||
Eric Anholt49568872008-10-21 11:38:50 -0700956 __put_user(update32.handle, &request->handle) ||
957 __put_user(update32.type, &request->type) ||
958 __put_user(update32.num, &request->num) ||
959 __put_user(update32.data, &request->data))
960 return -EFAULT;
961
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000962 err = drm_ioctl(file, DRM_IOCTL_UPDATE_DRAW, (unsigned long)request);
Eric Anholt49568872008-10-21 11:38:50 -0700963 return err;
964}
David Miller09e40d62009-02-18 01:35:21 -0800965#endif
Eric Anholt49568872008-10-21 11:38:50 -0700966
Dave Airlie9a186642005-06-23 21:29:18 +1000967struct drm_wait_vblank_request32 {
Dave Airlieff4135a2007-07-16 13:53:57 +1000968 enum drm_vblank_seq_type type;
Dave Airlie9a186642005-06-23 21:29:18 +1000969 unsigned int sequence;
970 u32 signal;
971};
972
973struct drm_wait_vblank_reply32 {
Dave Airlieff4135a2007-07-16 13:53:57 +1000974 enum drm_vblank_seq_type type;
Dave Airlie9a186642005-06-23 21:29:18 +1000975 unsigned int sequence;
976 s32 tval_sec;
977 s32 tval_usec;
978};
979
980typedef union drm_wait_vblank32 {
981 struct drm_wait_vblank_request32 request;
982 struct drm_wait_vblank_reply32 reply;
983} drm_wait_vblank32_t;
984
985static int compat_drm_wait_vblank(struct file *file, unsigned int cmd,
986 unsigned long arg)
987{
988 drm_wait_vblank32_t __user *argp = (void __user *)arg;
989 drm_wait_vblank32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000990 union drm_wait_vblank __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000991 int err;
992
993 if (copy_from_user(&req32, argp, sizeof(req32)))
994 return -EFAULT;
995
996 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300997 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000998 || __put_user(req32.request.type, &request->request.type)
999 || __put_user(req32.request.sequence, &request->request.sequence)
1000 || __put_user(req32.request.signal, &request->request.signal))
1001 return -EFAULT;
1002
Arnd Bergmanned8b6702009-12-16 22:17:09 +00001003 err = drm_ioctl(file, DRM_IOCTL_WAIT_VBLANK, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +10001004 if (err)
1005 return err;
1006
1007 if (__get_user(req32.reply.type, &request->reply.type)
1008 || __get_user(req32.reply.sequence, &request->reply.sequence)
1009 || __get_user(req32.reply.tval_sec, &request->reply.tval_sec)
1010 || __get_user(req32.reply.tval_usec, &request->reply.tval_usec))
1011 return -EFAULT;
1012
1013 if (copy_to_user(argp, &req32, sizeof(req32)))
1014 return -EFAULT;
1015
1016 return 0;
1017}
1018
Tvrtko Ursulinc631d5f2015-07-14 11:13:08 +01001019typedef struct drm_mode_fb_cmd232 {
1020 u32 fb_id;
1021 u32 width;
1022 u32 height;
1023 u32 pixel_format;
1024 u32 flags;
1025 u32 handles[4];
1026 u32 pitches[4];
1027 u32 offsets[4];
1028 u64 modifier[4];
1029} __attribute__((packed)) drm_mode_fb_cmd232_t;
1030
1031static int compat_drm_mode_addfb2(struct file *file, unsigned int cmd,
1032 unsigned long arg)
1033{
1034 struct drm_mode_fb_cmd232 __user *argp = (void __user *)arg;
1035 struct drm_mode_fb_cmd232 req32;
1036 struct drm_mode_fb_cmd2 __user *req64;
1037 int i;
1038 int err;
1039
1040 if (copy_from_user(&req32, argp, sizeof(req32)))
1041 return -EFAULT;
1042
1043 req64 = compat_alloc_user_space(sizeof(*req64));
1044
1045 if (!access_ok(VERIFY_WRITE, req64, sizeof(*req64))
1046 || __put_user(req32.width, &req64->width)
1047 || __put_user(req32.height, &req64->height)
1048 || __put_user(req32.pixel_format, &req64->pixel_format)
1049 || __put_user(req32.flags, &req64->flags))
1050 return -EFAULT;
1051
1052 for (i = 0; i < 4; i++) {
1053 if (__put_user(req32.handles[i], &req64->handles[i]))
1054 return -EFAULT;
1055 if (__put_user(req32.pitches[i], &req64->pitches[i]))
1056 return -EFAULT;
1057 if (__put_user(req32.offsets[i], &req64->offsets[i]))
1058 return -EFAULT;
1059 if (__put_user(req32.modifier[i], &req64->modifier[i]))
1060 return -EFAULT;
1061 }
1062
1063 err = drm_ioctl(file, DRM_IOCTL_MODE_ADDFB2, (unsigned long)req64);
1064 if (err)
1065 return err;
1066
1067 if (__get_user(req32.fb_id, &req64->fb_id))
1068 return -EFAULT;
1069
1070 if (copy_to_user(argp, &req32, sizeof(req32)))
1071 return -EFAULT;
1072
1073 return 0;
1074}
1075
Ville Syrjälä43fc8842015-03-13 14:51:25 +02001076static drm_ioctl_compat_t *drm_compat_ioctls[] = {
Dave Airlie9a186642005-06-23 21:29:18 +10001077 [DRM_IOCTL_NR(DRM_IOCTL_VERSION32)] = compat_drm_version,
1078 [DRM_IOCTL_NR(DRM_IOCTL_GET_UNIQUE32)] = compat_drm_getunique,
1079 [DRM_IOCTL_NR(DRM_IOCTL_GET_MAP32)] = compat_drm_getmap,
1080 [DRM_IOCTL_NR(DRM_IOCTL_GET_CLIENT32)] = compat_drm_getclient,
1081 [DRM_IOCTL_NR(DRM_IOCTL_GET_STATS32)] = compat_drm_getstats,
1082 [DRM_IOCTL_NR(DRM_IOCTL_SET_UNIQUE32)] = compat_drm_setunique,
1083 [DRM_IOCTL_NR(DRM_IOCTL_ADD_MAP32)] = compat_drm_addmap,
1084 [DRM_IOCTL_NR(DRM_IOCTL_ADD_BUFS32)] = compat_drm_addbufs,
1085 [DRM_IOCTL_NR(DRM_IOCTL_MARK_BUFS32)] = compat_drm_markbufs,
1086 [DRM_IOCTL_NR(DRM_IOCTL_INFO_BUFS32)] = compat_drm_infobufs,
1087 [DRM_IOCTL_NR(DRM_IOCTL_MAP_BUFS32)] = compat_drm_mapbufs,
1088 [DRM_IOCTL_NR(DRM_IOCTL_FREE_BUFS32)] = compat_drm_freebufs,
1089 [DRM_IOCTL_NR(DRM_IOCTL_RM_MAP32)] = compat_drm_rmmap,
1090 [DRM_IOCTL_NR(DRM_IOCTL_SET_SAREA_CTX32)] = compat_drm_setsareactx,
1091 [DRM_IOCTL_NR(DRM_IOCTL_GET_SAREA_CTX32)] = compat_drm_getsareactx,
1092 [DRM_IOCTL_NR(DRM_IOCTL_RES_CTX32)] = compat_drm_resctx,
1093 [DRM_IOCTL_NR(DRM_IOCTL_DMA32)] = compat_drm_dma,
Daniel Vettera7fb8a22015-09-09 16:45:52 +02001094#if IS_ENABLED(CONFIG_AGP)
Dave Airlie9a186642005-06-23 21:29:18 +10001095 [DRM_IOCTL_NR(DRM_IOCTL_AGP_ENABLE32)] = compat_drm_agp_enable,
1096 [DRM_IOCTL_NR(DRM_IOCTL_AGP_INFO32)] = compat_drm_agp_info,
1097 [DRM_IOCTL_NR(DRM_IOCTL_AGP_ALLOC32)] = compat_drm_agp_alloc,
1098 [DRM_IOCTL_NR(DRM_IOCTL_AGP_FREE32)] = compat_drm_agp_free,
1099 [DRM_IOCTL_NR(DRM_IOCTL_AGP_BIND32)] = compat_drm_agp_bind,
1100 [DRM_IOCTL_NR(DRM_IOCTL_AGP_UNBIND32)] = compat_drm_agp_unbind,
1101#endif
1102 [DRM_IOCTL_NR(DRM_IOCTL_SG_ALLOC32)] = compat_drm_sg_alloc,
1103 [DRM_IOCTL_NR(DRM_IOCTL_SG_FREE32)] = compat_drm_sg_free,
David Miller09e40d62009-02-18 01:35:21 -08001104#if defined(CONFIG_X86) || defined(CONFIG_IA64)
Eric Anholt49568872008-10-21 11:38:50 -07001105 [DRM_IOCTL_NR(DRM_IOCTL_UPDATE_DRAW32)] = compat_drm_update_draw,
David Miller09e40d62009-02-18 01:35:21 -08001106#endif
Dave Airlie9a186642005-06-23 21:29:18 +10001107 [DRM_IOCTL_NR(DRM_IOCTL_WAIT_VBLANK32)] = compat_drm_wait_vblank,
Tvrtko Ursulinc631d5f2015-07-14 11:13:08 +01001108 [DRM_IOCTL_NR(DRM_IOCTL_MODE_ADDFB232)] = compat_drm_mode_addfb2,
Dave Airlie9a186642005-06-23 21:29:18 +10001109};
1110
1111/**
1112 * Called whenever a 32-bit process running under a 64-bit kernel
1113 * performs an ioctl on /dev/drm.
1114 *
Eric Anholt6c340ea2007-08-25 20:23:09 +10001115 * \param file_priv DRM file private.
Dave Airlie9a186642005-06-23 21:29:18 +10001116 * \param cmd command.
1117 * \param arg user argument.
1118 * \return zero on success or negative number on failure.
1119 */
1120long drm_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1121{
1122 unsigned int nr = DRM_IOCTL_NR(cmd);
1123 drm_ioctl_compat_t *fn;
1124 int ret;
1125
Ian Romanick7ffa05e2007-11-22 17:02:08 +10001126 /* Assume that ioctls without an explicit compat routine will just
1127 * work. This may not always be a good assumption, but it's better
1128 * than always failing.
1129 */
Dave Airlie3d774612006-08-07 20:07:43 +10001130 if (nr >= ARRAY_SIZE(drm_compat_ioctls))
Arnd Bergmanned8b6702009-12-16 22:17:09 +00001131 return drm_ioctl(filp, cmd, arg);
Dave Airlie9a186642005-06-23 21:29:18 +10001132
1133 fn = drm_compat_ioctls[nr];
1134
Dave Airlie9a186642005-06-23 21:29:18 +10001135 if (fn != NULL)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001136 ret = (*fn) (filp, cmd, arg);
Dave Airlie9a186642005-06-23 21:29:18 +10001137 else
Arnd Bergmanned8b6702009-12-16 22:17:09 +00001138 ret = drm_ioctl(filp, cmd, arg);
Dave Airlie9a186642005-06-23 21:29:18 +10001139
1140 return ret;
1141}
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001142
Dave Airlie9a186642005-06-23 21:29:18 +10001143EXPORT_SYMBOL(drm_compat_ioctl);