blob: d7c14274c8b2530b814fd583f52d0b4e75f01f35 [file] [log] [blame]
Daniel Vetter26409812017-04-04 11:52:57 +02001/*
Dave Airlie9a186642005-06-23 21:29:18 +10002 * \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>
Al Viroff22ff92017-05-24 13:42:27 -040035#include "drm_legacy.h"
Al Viro012c6742017-05-24 14:11:03 -040036#include "drm_internal.h"
Dave Airlie9a186642005-06-23 21:29:18 +100037
38#define DRM_IOCTL_VERSION32 DRM_IOWR(0x00, drm_version32_t)
39#define DRM_IOCTL_GET_UNIQUE32 DRM_IOWR(0x01, drm_unique32_t)
40#define DRM_IOCTL_GET_MAP32 DRM_IOWR(0x04, drm_map32_t)
41#define DRM_IOCTL_GET_CLIENT32 DRM_IOWR(0x05, drm_client32_t)
42#define DRM_IOCTL_GET_STATS32 DRM_IOR( 0x06, drm_stats32_t)
43
44#define DRM_IOCTL_SET_UNIQUE32 DRM_IOW( 0x10, drm_unique32_t)
45#define DRM_IOCTL_ADD_MAP32 DRM_IOWR(0x15, drm_map32_t)
46#define DRM_IOCTL_ADD_BUFS32 DRM_IOWR(0x16, drm_buf_desc32_t)
47#define DRM_IOCTL_MARK_BUFS32 DRM_IOW( 0x17, drm_buf_desc32_t)
48#define DRM_IOCTL_INFO_BUFS32 DRM_IOWR(0x18, drm_buf_info32_t)
49#define DRM_IOCTL_MAP_BUFS32 DRM_IOWR(0x19, drm_buf_map32_t)
50#define DRM_IOCTL_FREE_BUFS32 DRM_IOW( 0x1a, drm_buf_free32_t)
51
52#define DRM_IOCTL_RM_MAP32 DRM_IOW( 0x1b, drm_map32_t)
53
54#define DRM_IOCTL_SET_SAREA_CTX32 DRM_IOW( 0x1c, drm_ctx_priv_map32_t)
55#define DRM_IOCTL_GET_SAREA_CTX32 DRM_IOWR(0x1d, drm_ctx_priv_map32_t)
56
57#define DRM_IOCTL_RES_CTX32 DRM_IOWR(0x26, drm_ctx_res32_t)
58#define DRM_IOCTL_DMA32 DRM_IOWR(0x29, drm_dma32_t)
59
60#define DRM_IOCTL_AGP_ENABLE32 DRM_IOW( 0x32, drm_agp_mode32_t)
61#define DRM_IOCTL_AGP_INFO32 DRM_IOR( 0x33, drm_agp_info32_t)
62#define DRM_IOCTL_AGP_ALLOC32 DRM_IOWR(0x34, drm_agp_buffer32_t)
63#define DRM_IOCTL_AGP_FREE32 DRM_IOW( 0x35, drm_agp_buffer32_t)
64#define DRM_IOCTL_AGP_BIND32 DRM_IOW( 0x36, drm_agp_binding32_t)
65#define DRM_IOCTL_AGP_UNBIND32 DRM_IOW( 0x37, drm_agp_binding32_t)
66
67#define DRM_IOCTL_SG_ALLOC32 DRM_IOW( 0x38, drm_scatter_gather32_t)
68#define DRM_IOCTL_SG_FREE32 DRM_IOW( 0x39, drm_scatter_gather32_t)
69
Eric Anholt49568872008-10-21 11:38:50 -070070#define DRM_IOCTL_UPDATE_DRAW32 DRM_IOW( 0x3f, drm_update_draw32_t)
71
Dave Airlie9a186642005-06-23 21:29:18 +100072#define DRM_IOCTL_WAIT_VBLANK32 DRM_IOWR(0x3a, drm_wait_vblank32_t)
73
Tvrtko Ursulinc631d5f2015-07-14 11:13:08 +010074#define DRM_IOCTL_MODE_ADDFB232 DRM_IOWR(0xb8, drm_mode_fb_cmd232_t)
75
Dave Airlie9a186642005-06-23 21:29:18 +100076typedef struct drm_version_32 {
Daniel Vetter26409812017-04-04 11:52:57 +020077 int version_major; /* Major version */
78 int version_minor; /* Minor version */
79 int version_patchlevel; /* Patch level */
80 u32 name_len; /* Length of name buffer */
81 u32 name; /* Name of driver */
82 u32 date_len; /* Length of date buffer */
83 u32 date; /* User-space buffer to hold date */
84 u32 desc_len; /* Length of desc buffer */
85 u32 desc; /* User-space buffer to hold desc */
Dave Airlie9a186642005-06-23 21:29:18 +100086} drm_version32_t;
87
88static int compat_drm_version(struct file *file, unsigned int cmd,
89 unsigned long arg)
90{
91 drm_version32_t v32;
Al Viro012c6742017-05-24 14:11:03 -040092 struct drm_version v;
Dave Airlie9a186642005-06-23 21:29:18 +100093 int err;
94
Dave Airlieb5e89ed2005-09-25 14:28:13 +100095 if (copy_from_user(&v32, (void __user *)arg, sizeof(v32)))
Dave Airlie9a186642005-06-23 21:29:18 +100096 return -EFAULT;
97
Al Viro012c6742017-05-24 14:11:03 -040098 v = (struct drm_version) {
99 .name_len = v32.name_len,
100 .name = compat_ptr(v32.name),
101 .date_len = v32.date_len,
102 .date = compat_ptr(v32.date),
103 .desc_len = v32.desc_len,
104 .desc = compat_ptr(v32.desc),
105 };
106 err = drm_ioctl_kernel(file, drm_version, &v,
107 DRM_UNLOCKED|DRM_RENDER_ALLOW|DRM_CONTROL_ALLOW);
Dave Airlie9a186642005-06-23 21:29:18 +1000108 if (err)
109 return err;
110
Al Viro012c6742017-05-24 14:11:03 -0400111 v32.version_major = v.version_major;
112 v32.version_minor = v.version_minor;
113 v32.version_patchlevel = v.version_patchlevel;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000114 if (copy_to_user((void __user *)arg, &v32, sizeof(v32)))
Dave Airlie9a186642005-06-23 21:29:18 +1000115 return -EFAULT;
116 return 0;
117}
118
119typedef struct drm_unique32 {
Daniel Vetter26409812017-04-04 11:52:57 +0200120 u32 unique_len; /* Length of unique */
121 u32 unique; /* Unique name for driver instantiation */
Dave Airlie9a186642005-06-23 21:29:18 +1000122} drm_unique32_t;
123
124static int compat_drm_getunique(struct file *file, unsigned int cmd,
125 unsigned long arg)
126{
127 drm_unique32_t uq32;
Al Viro17e3dad2017-05-24 14:15:20 -0400128 struct drm_unique uq;
Dave Airlie9a186642005-06-23 21:29:18 +1000129 int err;
130
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000131 if (copy_from_user(&uq32, (void __user *)arg, sizeof(uq32)))
Dave Airlie9a186642005-06-23 21:29:18 +1000132 return -EFAULT;
Al Viro17e3dad2017-05-24 14:15:20 -0400133 uq = (struct drm_unique){
134 .unique_len = uq32.unique_len,
135 .unique = compat_ptr(uq32.unique),
136 };
Dave Airlie9a186642005-06-23 21:29:18 +1000137
Al Viro17e3dad2017-05-24 14:15:20 -0400138 err = drm_ioctl_kernel(file, drm_getunique, &uq, DRM_UNLOCKED);
Dave Airlie9a186642005-06-23 21:29:18 +1000139 if (err)
140 return err;
141
Al Viro17e3dad2017-05-24 14:15:20 -0400142 uq32.unique_len = uq.unique_len;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000143 if (copy_to_user((void __user *)arg, &uq32, sizeof(uq32)))
Dave Airlie9a186642005-06-23 21:29:18 +1000144 return -EFAULT;
145 return 0;
146}
147
148static int compat_drm_setunique(struct file *file, unsigned int cmd,
149 unsigned long arg)
150{
Al Viro56c275c2017-05-24 14:20:21 -0400151 /* it's dead */
152 return -EINVAL;
Dave Airlie9a186642005-06-23 21:29:18 +1000153}
154
155typedef struct drm_map32 {
Daniel Vetter26409812017-04-04 11:52:57 +0200156 u32 offset; /* Requested physical address (0 for SAREA) */
157 u32 size; /* Requested physical size (bytes) */
158 enum drm_map_type type; /* Type of memory to map */
159 enum drm_map_flags flags; /* Flags */
160 u32 handle; /* User-space: "Handle" to pass to mmap() */
161 int mtrr; /* MTRR slot used */
Dave Airlie9a186642005-06-23 21:29:18 +1000162} drm_map32_t;
163
164static int compat_drm_getmap(struct file *file, unsigned int cmd,
165 unsigned long arg)
166{
167 drm_map32_t __user *argp = (void __user *)arg;
168 drm_map32_t m32;
Al Viro8c4fe492017-05-24 14:26:39 -0400169 struct drm_map map;
170 int err;
Dave Airlie9a186642005-06-23 21:29:18 +1000171
Al Viro8c4fe492017-05-24 14:26:39 -0400172 if (copy_from_user(&m32, argp, sizeof(m32)))
Dave Airlie9a186642005-06-23 21:29:18 +1000173 return -EFAULT;
174
Al Viro8c4fe492017-05-24 14:26:39 -0400175 map.offset = m32.offset;
176 err = drm_ioctl_kernel(file, drm_legacy_getmap_ioctl, &map, DRM_UNLOCKED);
Dave Airlie9a186642005-06-23 21:29:18 +1000177 if (err)
178 return err;
179
Al Viro8c4fe492017-05-24 14:26:39 -0400180 m32.offset = map.offset;
181 m32.size = map.size;
182 m32.type = map.type;
183 m32.flags = map.flags;
184 m32.handle = ptr_to_compat(map.handle);
185 m32.mtrr = map.mtrr;
Dave Airlie9a186642005-06-23 21:29:18 +1000186 if (copy_to_user(argp, &m32, sizeof(m32)))
187 return -EFAULT;
188 return 0;
189
190}
191
192static int compat_drm_addmap(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 err;
199 void *handle;
200
201 if (copy_from_user(&m32, argp, sizeof(m32)))
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(m32.offset, &map->offset)
208 || __put_user(m32.size, &map->size)
209 || __put_user(m32.type, &map->type)
210 || __put_user(m32.flags, &map->flags))
211 return -EFAULT;
212
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000213 err = drm_ioctl(file, DRM_IOCTL_ADD_MAP, (unsigned long)map);
Dave Airlie9a186642005-06-23 21:29:18 +1000214 if (err)
215 return err;
216
217 if (__get_user(m32.offset, &map->offset)
218 || __get_user(m32.mtrr, &map->mtrr)
219 || __get_user(handle, &map->handle))
220 return -EFAULT;
221
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000222 m32.handle = (unsigned long)handle;
Christian Dietrichcafe8d82011-06-04 15:36:43 +0000223 if (m32.handle != (unsigned long)handle)
Joe Perches499447d2017-02-28 04:55:53 -0800224 pr_err_ratelimited("compat_drm_addmap truncated handle %p for type %d offset %x\n",
Christian Dietrichcafe8d82011-06-04 15:36:43 +0000225 handle, m32.type, m32.offset);
Dave Airlie9a186642005-06-23 21:29:18 +1000226
227 if (copy_to_user(argp, &m32, sizeof(m32)))
228 return -EFAULT;
229
230 return 0;
231}
232
233static int compat_drm_rmmap(struct file *file, unsigned int cmd,
234 unsigned long arg)
235{
236 drm_map32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000237 struct drm_map __user *map;
Dave Airlie9a186642005-06-23 21:29:18 +1000238 u32 handle;
239
240 if (get_user(handle, &argp->handle))
241 return -EFAULT;
242
243 map = compat_alloc_user_space(sizeof(*map));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300244 if (!map)
Dave Airlie9a186642005-06-23 21:29:18 +1000245 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000246 if (__put_user((void *)(unsigned long)handle, &map->handle))
Dave Airlie9a186642005-06-23 21:29:18 +1000247 return -EFAULT;
248
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000249 return drm_ioctl(file, DRM_IOCTL_RM_MAP, (unsigned long)map);
Dave Airlie9a186642005-06-23 21:29:18 +1000250}
251
252typedef struct drm_client32 {
Daniel Vetter26409812017-04-04 11:52:57 +0200253 int idx; /* Which client desired? */
254 int auth; /* Is client authenticated? */
255 u32 pid; /* Process ID */
256 u32 uid; /* User ID */
257 u32 magic; /* Magic */
258 u32 iocs; /* Ioctl count */
Dave Airlie9a186642005-06-23 21:29:18 +1000259} drm_client32_t;
260
261static int compat_drm_getclient(struct file *file, unsigned int cmd,
262 unsigned long arg)
263{
264 drm_client32_t c32;
265 drm_client32_t __user *argp = (void __user *)arg;
Al Viro9e926622017-05-24 19:10:32 -0400266 struct drm_client client;
267 int err;
Dave Airlie9a186642005-06-23 21:29:18 +1000268
Al Viro9e926622017-05-24 19:10:32 -0400269 if (copy_from_user(&c32, argp, sizeof(c32)))
Dave Airlie9a186642005-06-23 21:29:18 +1000270 return -EFAULT;
271
Al Viro9e926622017-05-24 19:10:32 -0400272 client.idx = c32.idx;
Dave Airlie9a186642005-06-23 21:29:18 +1000273
Al Viro9e926622017-05-24 19:10:32 -0400274 err = drm_ioctl_kernel(file, drm_getclient, &client, DRM_UNLOCKED);
Dave Airlie9a186642005-06-23 21:29:18 +1000275 if (err)
276 return err;
277
Al Viro9e926622017-05-24 19:10:32 -0400278 c32.idx = client.idx;
279 c32.auth = client.auth;
280 c32.pid = client.pid;
281 c32.uid = client.uid;
282 c32.magic = client.magic;
283 c32.iocs = client.iocs;
Dave Airlie9a186642005-06-23 21:29:18 +1000284
285 if (copy_to_user(argp, &c32, sizeof(c32)))
286 return -EFAULT;
287 return 0;
288}
289
290typedef struct drm_stats32 {
291 u32 count;
292 struct {
293 u32 value;
Dave Airlieff4135a2007-07-16 13:53:57 +1000294 enum drm_stat_type type;
Dave Airlie9a186642005-06-23 21:29:18 +1000295 } data[15];
296} drm_stats32_t;
297
298static int compat_drm_getstats(struct file *file, unsigned int cmd,
299 unsigned long arg)
300{
Dave Airlie9a186642005-06-23 21:29:18 +1000301 drm_stats32_t __user *argp = (void __user *)arg;
Al Viro8547ee92017-05-24 19:18:06 -0400302 int err;
Dave Airlie9a186642005-06-23 21:29:18 +1000303
Al Viro8547ee92017-05-24 19:18:06 -0400304 err = drm_ioctl_kernel(file, drm_noop, NULL, DRM_UNLOCKED);
Dave Airlie9a186642005-06-23 21:29:18 +1000305 if (err)
306 return err;
307
Al Viro8547ee92017-05-24 19:18:06 -0400308 if (clear_user(argp, sizeof(drm_stats32_t)))
Dave Airlie9a186642005-06-23 21:29:18 +1000309 return -EFAULT;
310 return 0;
311}
312
313typedef struct drm_buf_desc32 {
Daniel Vetter26409812017-04-04 11:52:57 +0200314 int count; /* Number of buffers of this size */
315 int size; /* Size in bytes */
316 int low_mark; /* Low water mark */
317 int high_mark; /* High water mark */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000318 int flags;
Daniel Vetter26409812017-04-04 11:52:57 +0200319 u32 agp_start; /* Start address in the AGP aperture */
Dave Airlie9a186642005-06-23 21:29:18 +1000320} drm_buf_desc32_t;
321
322static int compat_drm_addbufs(struct file *file, unsigned int cmd,
323 unsigned long arg)
324{
325 drm_buf_desc32_t __user *argp = (void __user *)arg;
Al Viroff22ff92017-05-24 13:42:27 -0400326 drm_buf_desc32_t desc32;
327 struct drm_buf_desc desc;
Dave Airlie9a186642005-06-23 21:29:18 +1000328 int err;
Dave Airlie9a186642005-06-23 21:29:18 +1000329
Al Viroff22ff92017-05-24 13:42:27 -0400330 if (copy_from_user(&desc32, argp, sizeof(drm_buf_desc32_t)))
Dave Airlie9a186642005-06-23 21:29:18 +1000331 return -EFAULT;
332
Al Viroff22ff92017-05-24 13:42:27 -0400333 desc = (struct drm_buf_desc){
334 desc32.count, desc32.size, desc32.low_mark, desc32.high_mark,
335 desc32.flags, desc32.agp_start
336 };
Dave Airlie9a186642005-06-23 21:29:18 +1000337
Al Viroff22ff92017-05-24 13:42:27 -0400338 err = drm_ioctl_kernel(file, drm_legacy_addbufs, &desc,
339 DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
Dave Airlie9a186642005-06-23 21:29:18 +1000340 if (err)
341 return err;
342
Al Viroff22ff92017-05-24 13:42:27 -0400343 desc32 = (drm_buf_desc32_t){
344 desc.count, desc.size, desc.low_mark, desc.high_mark,
345 desc.flags, desc.agp_start
346 };
347 if (copy_to_user(argp, &desc32, sizeof(drm_buf_desc32_t)))
Dave Airlie9a186642005-06-23 21:29:18 +1000348 return -EFAULT;
349
350 return 0;
351}
352
353static int compat_drm_markbufs(struct file *file, unsigned int cmd,
354 unsigned long arg)
355{
356 drm_buf_desc32_t b32;
357 drm_buf_desc32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000358 struct drm_buf_desc __user *buf;
Dave Airlie9a186642005-06-23 21:29:18 +1000359
360 if (copy_from_user(&b32, argp, sizeof(b32)))
361 return -EFAULT;
362
363 buf = compat_alloc_user_space(sizeof(*buf));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300364 if (!buf)
Dave Airlie9a186642005-06-23 21:29:18 +1000365 return -EFAULT;
366
367 if (__put_user(b32.size, &buf->size)
368 || __put_user(b32.low_mark, &buf->low_mark)
369 || __put_user(b32.high_mark, &buf->high_mark))
370 return -EFAULT;
371
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000372 return drm_ioctl(file, DRM_IOCTL_MARK_BUFS, (unsigned long)buf);
Dave Airlie9a186642005-06-23 21:29:18 +1000373}
374
375typedef struct drm_buf_info32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000376 int count; /**< Entries in list */
377 u32 list;
Dave Airlie9a186642005-06-23 21:29:18 +1000378} drm_buf_info32_t;
379
Al Viro5c7640a2017-05-24 17:54:09 -0400380static int copy_one_buf32(void *data, int count, struct drm_buf_entry *from)
381{
382 drm_buf_info32_t *request = data;
383 drm_buf_desc32_t __user *to = compat_ptr(request->list);
384 drm_buf_desc32_t v = {.count = from->buf_count,
385 .size = from->buf_size,
386 .low_mark = from->low_mark,
387 .high_mark = from->high_mark};
388 return copy_to_user(to + count, &v, offsetof(drm_buf_desc32_t, flags));
389}
390
391static int drm_legacy_infobufs32(struct drm_device *dev, void *data,
392 struct drm_file *file_priv)
393{
394 drm_buf_info32_t *request = data;
395 return __drm_legacy_infobufs(dev, data, &request->count, copy_one_buf32);
396}
397
Dave Airlie9a186642005-06-23 21:29:18 +1000398static int compat_drm_infobufs(struct file *file, unsigned int cmd,
399 unsigned long arg)
400{
401 drm_buf_info32_t req32;
402 drm_buf_info32_t __user *argp = (void __user *)arg;
Al Viro5c7640a2017-05-24 17:54:09 -0400403 int err;
Dave Airlie9a186642005-06-23 21:29:18 +1000404
405 if (copy_from_user(&req32, argp, sizeof(req32)))
406 return -EFAULT;
407
Al Viro5c7640a2017-05-24 17:54:09 -0400408 if (req32.count < 0)
409 req32.count = 0;
Dave Airlie9a186642005-06-23 21:29:18 +1000410
Al Viro5c7640a2017-05-24 17:54:09 -0400411 err = drm_ioctl_kernel(file, drm_legacy_infobufs32, &req32, DRM_AUTH);
Dave Airlie9a186642005-06-23 21:29:18 +1000412 if (err)
413 return err;
414
Al Viro5c7640a2017-05-24 17:54:09 -0400415 if (put_user(req32.count, &argp->count))
Dave Airlie9a186642005-06-23 21:29:18 +1000416 return -EFAULT;
417
418 return 0;
419}
420
421typedef struct drm_buf_pub32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000422 int idx; /**< Index into the master buffer list */
423 int total; /**< Buffer size */
424 int used; /**< Amount of buffer in use (for DMA) */
425 u32 address; /**< Address of buffer */
Dave Airlie9a186642005-06-23 21:29:18 +1000426} drm_buf_pub32_t;
427
428typedef struct drm_buf_map32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000429 int count; /**< Length of the buffer list */
430 u32 virtual; /**< Mmap'd area in user-virtual */
431 u32 list; /**< Buffer information */
Dave Airlie9a186642005-06-23 21:29:18 +1000432} drm_buf_map32_t;
433
434static int compat_drm_mapbufs(struct file *file, unsigned int cmd,
435 unsigned long arg)
436{
437 drm_buf_map32_t __user *argp = (void __user *)arg;
438 drm_buf_map32_t req32;
439 drm_buf_pub32_t __user *list32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000440 struct drm_buf_map __user *request;
441 struct drm_buf_pub __user *list;
Dave Airlie9a186642005-06-23 21:29:18 +1000442 int i, err;
443 int count, actual;
444 size_t nbytes;
445 void __user *addr;
446
447 if (copy_from_user(&req32, argp, sizeof(req32)))
448 return -EFAULT;
449 count = req32.count;
450 list32 = (void __user *)(unsigned long)req32.list;
451
452 if (count < 0)
453 return -EINVAL;
Dave Airlieff4135a2007-07-16 13:53:57 +1000454 nbytes = sizeof(*request) + count * sizeof(struct drm_buf_pub);
Dave Airlie9a186642005-06-23 21:29:18 +1000455 request = compat_alloc_user_space(nbytes);
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300456 if (!request)
Dave Airlie9a186642005-06-23 21:29:18 +1000457 return -EFAULT;
Dave Airlieff4135a2007-07-16 13:53:57 +1000458 list = (struct drm_buf_pub *) (request + 1);
Dave Airlie9a186642005-06-23 21:29:18 +1000459
460 if (__put_user(count, &request->count)
461 || __put_user(list, &request->list))
462 return -EFAULT;
463
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000464 err = drm_ioctl(file, DRM_IOCTL_MAP_BUFS, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000465 if (err)
466 return err;
467
468 if (__get_user(actual, &request->count))
469 return -EFAULT;
470 if (count >= actual)
471 for (i = 0; i < actual; ++i)
472 if (__copy_in_user(&list32[i], &list[i],
Dave Airlieff4135a2007-07-16 13:53:57 +1000473 offsetof(struct drm_buf_pub, address))
Dave Airlie9a186642005-06-23 21:29:18 +1000474 || __get_user(addr, &list[i].address)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000475 || __put_user((unsigned long)addr,
Dave Airlie9a186642005-06-23 21:29:18 +1000476 &list32[i].address))
477 return -EFAULT;
478
479 if (__put_user(actual, &argp->count)
480 || __get_user(addr, &request->virtual)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000481 || __put_user((unsigned long)addr, &argp->virtual))
Dave Airlie9a186642005-06-23 21:29:18 +1000482 return -EFAULT;
483
484 return 0;
485}
486
487typedef struct drm_buf_free32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000488 int count;
489 u32 list;
Dave Airlie9a186642005-06-23 21:29:18 +1000490} drm_buf_free32_t;
491
492static int compat_drm_freebufs(struct file *file, unsigned int cmd,
493 unsigned long arg)
494{
495 drm_buf_free32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000496 struct drm_buf_free __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000497 drm_buf_free32_t __user *argp = (void __user *)arg;
498
499 if (copy_from_user(&req32, argp, sizeof(req32)))
500 return -EFAULT;
501
502 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300503 if (!request)
Dave Airlie9a186642005-06-23 21:29:18 +1000504 return -EFAULT;
505 if (__put_user(req32.count, &request->count)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000506 || __put_user((int __user *)(unsigned long)req32.list,
Dave Airlie9a186642005-06-23 21:29:18 +1000507 &request->list))
508 return -EFAULT;
509
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000510 return drm_ioctl(file, DRM_IOCTL_FREE_BUFS, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000511}
512
513typedef struct drm_ctx_priv_map32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000514 unsigned int ctx_id; /**< Context requesting private mapping */
515 u32 handle; /**< Handle of map */
Dave Airlie9a186642005-06-23 21:29:18 +1000516} drm_ctx_priv_map32_t;
517
518static int compat_drm_setsareactx(struct file *file, unsigned int cmd,
519 unsigned long arg)
520{
521 drm_ctx_priv_map32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000522 struct drm_ctx_priv_map __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000523 drm_ctx_priv_map32_t __user *argp = (void __user *)arg;
524
525 if (copy_from_user(&req32, argp, sizeof(req32)))
526 return -EFAULT;
527
528 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300529 if (!request)
Dave Airlie9a186642005-06-23 21:29:18 +1000530 return -EFAULT;
531 if (__put_user(req32.ctx_id, &request->ctx_id)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000532 || __put_user((void *)(unsigned long)req32.handle,
Dave Airlie9a186642005-06-23 21:29:18 +1000533 &request->handle))
534 return -EFAULT;
535
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000536 return drm_ioctl(file, DRM_IOCTL_SET_SAREA_CTX, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000537}
538
539static int compat_drm_getsareactx(struct file *file, unsigned int cmd,
540 unsigned long arg)
541{
Dave Airlieff4135a2007-07-16 13:53:57 +1000542 struct drm_ctx_priv_map __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000543 drm_ctx_priv_map32_t __user *argp = (void __user *)arg;
544 int err;
545 unsigned int ctx_id;
546 void *handle;
547
548 if (!access_ok(VERIFY_WRITE, argp, sizeof(*argp))
549 || __get_user(ctx_id, &argp->ctx_id))
550 return -EFAULT;
551
552 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300553 if (!request)
Dave Airlie9a186642005-06-23 21:29:18 +1000554 return -EFAULT;
555 if (__put_user(ctx_id, &request->ctx_id))
556 return -EFAULT;
557
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000558 err = drm_ioctl(file, DRM_IOCTL_GET_SAREA_CTX, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000559 if (err)
560 return err;
561
562 if (__get_user(handle, &request->handle)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000563 || __put_user((unsigned long)handle, &argp->handle))
Dave Airlie9a186642005-06-23 21:29:18 +1000564 return -EFAULT;
565
566 return 0;
567}
568
569typedef struct drm_ctx_res32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000570 int count;
571 u32 contexts;
Dave Airlie9a186642005-06-23 21:29:18 +1000572} drm_ctx_res32_t;
573
574static int compat_drm_resctx(struct file *file, unsigned int cmd,
575 unsigned long arg)
576{
577 drm_ctx_res32_t __user *argp = (void __user *)arg;
578 drm_ctx_res32_t res32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000579 struct drm_ctx_res __user *res;
Dave Airlie9a186642005-06-23 21:29:18 +1000580 int err;
581
582 if (copy_from_user(&res32, argp, sizeof(res32)))
583 return -EFAULT;
584
585 res = compat_alloc_user_space(sizeof(*res));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300586 if (!res)
Dave Airlie9a186642005-06-23 21:29:18 +1000587 return -EFAULT;
588 if (__put_user(res32.count, &res->count)
Dave Airlieff4135a2007-07-16 13:53:57 +1000589 || __put_user((struct drm_ctx __user *) (unsigned long)res32.contexts,
Dave Airlie9a186642005-06-23 21:29:18 +1000590 &res->contexts))
591 return -EFAULT;
592
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000593 err = drm_ioctl(file, DRM_IOCTL_RES_CTX, (unsigned long)res);
Dave Airlie9a186642005-06-23 21:29:18 +1000594 if (err)
595 return err;
596
597 if (__get_user(res32.count, &res->count)
598 || __put_user(res32.count, &argp->count))
599 return -EFAULT;
600
601 return 0;
602}
603
604typedef struct drm_dma32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000605 int context; /**< Context handle */
606 int send_count; /**< Number of buffers to send */
607 u32 send_indices; /**< List of handles to buffers */
608 u32 send_sizes; /**< Lengths of data to send */
Dave Airlieff4135a2007-07-16 13:53:57 +1000609 enum drm_dma_flags flags; /**< Flags */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000610 int request_count; /**< Number of buffers requested */
611 int request_size; /**< Desired size for buffers */
612 u32 request_indices; /**< Buffer information */
613 u32 request_sizes;
614 int granted_count; /**< Number of buffers granted */
Dave Airlie9a186642005-06-23 21:29:18 +1000615} drm_dma32_t;
616
617static int compat_drm_dma(struct file *file, unsigned int cmd,
618 unsigned long arg)
619{
620 drm_dma32_t d32;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000621 drm_dma32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000622 struct drm_dma __user *d;
Dave Airlie9a186642005-06-23 21:29:18 +1000623 int err;
624
625 if (copy_from_user(&d32, argp, sizeof(d32)))
626 return -EFAULT;
627
628 d = compat_alloc_user_space(sizeof(*d));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300629 if (!d)
Dave Airlie9a186642005-06-23 21:29:18 +1000630 return -EFAULT;
631
632 if (__put_user(d32.context, &d->context)
633 || __put_user(d32.send_count, &d->send_count)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000634 || __put_user((int __user *)(unsigned long)d32.send_indices,
Dave Airlie9a186642005-06-23 21:29:18 +1000635 &d->send_indices)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000636 || __put_user((int __user *)(unsigned long)d32.send_sizes,
Dave Airlie9a186642005-06-23 21:29:18 +1000637 &d->send_sizes)
638 || __put_user(d32.flags, &d->flags)
639 || __put_user(d32.request_count, &d->request_count)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000640 || __put_user((int __user *)(unsigned long)d32.request_indices,
Dave Airlie9a186642005-06-23 21:29:18 +1000641 &d->request_indices)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000642 || __put_user((int __user *)(unsigned long)d32.request_sizes,
Dave Airlie9a186642005-06-23 21:29:18 +1000643 &d->request_sizes))
644 return -EFAULT;
645
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000646 err = drm_ioctl(file, DRM_IOCTL_DMA, (unsigned long)d);
Dave Airlie9a186642005-06-23 21:29:18 +1000647 if (err)
648 return err;
649
650 if (__get_user(d32.request_size, &d->request_size)
651 || __get_user(d32.granted_count, &d->granted_count)
652 || __put_user(d32.request_size, &argp->request_size)
653 || __put_user(d32.granted_count, &argp->granted_count))
654 return -EFAULT;
655
656 return 0;
657}
658
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200659#if IS_ENABLED(CONFIG_AGP)
Dave Airlie9a186642005-06-23 21:29:18 +1000660typedef struct drm_agp_mode32 {
661 u32 mode; /**< AGP mode */
662} drm_agp_mode32_t;
663
664static int compat_drm_agp_enable(struct file *file, unsigned int cmd,
665 unsigned long arg)
666{
667 drm_agp_mode32_t __user *argp = (void __user *)arg;
668 drm_agp_mode32_t m32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000669 struct drm_agp_mode __user *mode;
Dave Airlie9a186642005-06-23 21:29:18 +1000670
671 if (get_user(m32.mode, &argp->mode))
672 return -EFAULT;
673
674 mode = compat_alloc_user_space(sizeof(*mode));
675 if (put_user(m32.mode, &mode->mode))
676 return -EFAULT;
677
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000678 return drm_ioctl(file, DRM_IOCTL_AGP_ENABLE, (unsigned long)mode);
Dave Airlie9a186642005-06-23 21:29:18 +1000679}
680
681typedef struct drm_agp_info32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000682 int agp_version_major;
683 int agp_version_minor;
684 u32 mode;
685 u32 aperture_base; /* physical address */
686 u32 aperture_size; /* bytes */
687 u32 memory_allowed; /* bytes */
688 u32 memory_used;
Dave Airlie9a186642005-06-23 21:29:18 +1000689
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000690 /* PCI information */
Dave Airlie9a186642005-06-23 21:29:18 +1000691 unsigned short id_vendor;
692 unsigned short id_device;
693} drm_agp_info32_t;
694
695static int compat_drm_agp_info(struct file *file, unsigned int cmd,
696 unsigned long arg)
697{
698 drm_agp_info32_t __user *argp = (void __user *)arg;
699 drm_agp_info32_t i32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000700 struct drm_agp_info __user *info;
Dave Airlie9a186642005-06-23 21:29:18 +1000701 int err;
702
703 info = compat_alloc_user_space(sizeof(*info));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300704 if (!info)
Dave Airlie9a186642005-06-23 21:29:18 +1000705 return -EFAULT;
706
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000707 err = drm_ioctl(file, DRM_IOCTL_AGP_INFO, (unsigned long)info);
Dave Airlie9a186642005-06-23 21:29:18 +1000708 if (err)
709 return err;
710
711 if (__get_user(i32.agp_version_major, &info->agp_version_major)
712 || __get_user(i32.agp_version_minor, &info->agp_version_minor)
713 || __get_user(i32.mode, &info->mode)
714 || __get_user(i32.aperture_base, &info->aperture_base)
715 || __get_user(i32.aperture_size, &info->aperture_size)
716 || __get_user(i32.memory_allowed, &info->memory_allowed)
717 || __get_user(i32.memory_used, &info->memory_used)
718 || __get_user(i32.id_vendor, &info->id_vendor)
719 || __get_user(i32.id_device, &info->id_device))
720 return -EFAULT;
721
722 if (copy_to_user(argp, &i32, sizeof(i32)))
723 return -EFAULT;
724
725 return 0;
726}
727
728typedef struct drm_agp_buffer32 {
729 u32 size; /**< In bytes -- will round to page boundary */
730 u32 handle; /**< Used for binding / unbinding */
731 u32 type; /**< Type of memory to allocate */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000732 u32 physical; /**< Physical used by i810 */
Dave Airlie9a186642005-06-23 21:29:18 +1000733} drm_agp_buffer32_t;
734
735static int compat_drm_agp_alloc(struct file *file, unsigned int cmd,
736 unsigned long arg)
737{
738 drm_agp_buffer32_t __user *argp = (void __user *)arg;
739 drm_agp_buffer32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000740 struct drm_agp_buffer __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000741 int err;
742
743 if (copy_from_user(&req32, argp, sizeof(req32)))
744 return -EFAULT;
745
746 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300747 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000748 || __put_user(req32.size, &request->size)
749 || __put_user(req32.type, &request->type))
750 return -EFAULT;
751
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000752 err = drm_ioctl(file, DRM_IOCTL_AGP_ALLOC, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000753 if (err)
754 return err;
755
756 if (__get_user(req32.handle, &request->handle)
757 || __get_user(req32.physical, &request->physical)
758 || copy_to_user(argp, &req32, sizeof(req32))) {
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000759 drm_ioctl(file, DRM_IOCTL_AGP_FREE, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000760 return -EFAULT;
761 }
762
763 return 0;
764}
765
766static int compat_drm_agp_free(struct file *file, unsigned int cmd,
767 unsigned long arg)
768{
769 drm_agp_buffer32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000770 struct drm_agp_buffer __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000771 u32 handle;
772
773 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300774 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000775 || get_user(handle, &argp->handle)
776 || __put_user(handle, &request->handle))
777 return -EFAULT;
778
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000779 return drm_ioctl(file, DRM_IOCTL_AGP_FREE, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000780}
781
782typedef struct drm_agp_binding32 {
783 u32 handle; /**< From drm_agp_buffer */
784 u32 offset; /**< In bytes -- will round to page boundary */
785} drm_agp_binding32_t;
786
787static int compat_drm_agp_bind(struct file *file, unsigned int cmd,
788 unsigned long arg)
789{
790 drm_agp_binding32_t __user *argp = (void __user *)arg;
791 drm_agp_binding32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000792 struct drm_agp_binding __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000793
794 if (copy_from_user(&req32, argp, sizeof(req32)))
795 return -EFAULT;
796
797 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300798 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000799 || __put_user(req32.handle, &request->handle)
800 || __put_user(req32.offset, &request->offset))
801 return -EFAULT;
802
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000803 return drm_ioctl(file, DRM_IOCTL_AGP_BIND, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000804}
805
806static int compat_drm_agp_unbind(struct file *file, unsigned int cmd,
807 unsigned long arg)
808{
809 drm_agp_binding32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000810 struct drm_agp_binding __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000811 u32 handle;
812
813 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300814 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000815 || get_user(handle, &argp->handle)
816 || __put_user(handle, &request->handle))
817 return -EFAULT;
818
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000819 return drm_ioctl(file, DRM_IOCTL_AGP_UNBIND, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000820}
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200821#endif /* CONFIG_AGP */
Dave Airlie9a186642005-06-23 21:29:18 +1000822
823typedef struct drm_scatter_gather32 {
824 u32 size; /**< In bytes -- will round to page boundary */
825 u32 handle; /**< Used for mapping / unmapping */
826} drm_scatter_gather32_t;
827
828static int compat_drm_sg_alloc(struct file *file, unsigned int cmd,
829 unsigned long arg)
830{
831 drm_scatter_gather32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000832 struct drm_scatter_gather __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000833 int err;
834 unsigned long x;
835
836 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300837 if (!request || !access_ok(VERIFY_WRITE, argp, sizeof(*argp))
Dave Airlie9a186642005-06-23 21:29:18 +1000838 || __get_user(x, &argp->size)
839 || __put_user(x, &request->size))
840 return -EFAULT;
841
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000842 err = drm_ioctl(file, DRM_IOCTL_SG_ALLOC, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000843 if (err)
844 return err;
845
846 /* XXX not sure about the handle conversion here... */
847 if (__get_user(x, &request->handle)
848 || __put_user(x >> PAGE_SHIFT, &argp->handle))
849 return -EFAULT;
850
851 return 0;
852}
853
854static int compat_drm_sg_free(struct file *file, unsigned int cmd,
855 unsigned long arg)
856{
857 drm_scatter_gather32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000858 struct drm_scatter_gather __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000859 unsigned long x;
860
861 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300862 if (!request || !access_ok(VERIFY_WRITE, argp, sizeof(*argp))
Dave Airlie9a186642005-06-23 21:29:18 +1000863 || __get_user(x, &argp->handle)
864 || __put_user(x << PAGE_SHIFT, &request->handle))
865 return -EFAULT;
866
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000867 return drm_ioctl(file, DRM_IOCTL_SG_FREE, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000868}
869
David Miller09e40d62009-02-18 01:35:21 -0800870#if defined(CONFIG_X86) || defined(CONFIG_IA64)
Eric Anholt49568872008-10-21 11:38:50 -0700871typedef struct drm_update_draw32 {
872 drm_drawable_t handle;
873 unsigned int type;
874 unsigned int num;
875 /* 64-bit version has a 32-bit pad here */
876 u64 data; /**< Pointer */
877} __attribute__((packed)) drm_update_draw32_t;
878
879static int compat_drm_update_draw(struct file *file, unsigned int cmd,
880 unsigned long arg)
881{
882 drm_update_draw32_t update32;
883 struct drm_update_draw __user *request;
884 int err;
885
886 if (copy_from_user(&update32, (void __user *)arg, sizeof(update32)))
887 return -EFAULT;
888
889 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300890 if (!request ||
Eric Anholt49568872008-10-21 11:38:50 -0700891 __put_user(update32.handle, &request->handle) ||
892 __put_user(update32.type, &request->type) ||
893 __put_user(update32.num, &request->num) ||
894 __put_user(update32.data, &request->data))
895 return -EFAULT;
896
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000897 err = drm_ioctl(file, DRM_IOCTL_UPDATE_DRAW, (unsigned long)request);
Eric Anholt49568872008-10-21 11:38:50 -0700898 return err;
899}
David Miller09e40d62009-02-18 01:35:21 -0800900#endif
Eric Anholt49568872008-10-21 11:38:50 -0700901
Dave Airlie9a186642005-06-23 21:29:18 +1000902struct drm_wait_vblank_request32 {
Dave Airlieff4135a2007-07-16 13:53:57 +1000903 enum drm_vblank_seq_type type;
Dave Airlie9a186642005-06-23 21:29:18 +1000904 unsigned int sequence;
905 u32 signal;
906};
907
908struct drm_wait_vblank_reply32 {
Dave Airlieff4135a2007-07-16 13:53:57 +1000909 enum drm_vblank_seq_type type;
Dave Airlie9a186642005-06-23 21:29:18 +1000910 unsigned int sequence;
911 s32 tval_sec;
912 s32 tval_usec;
913};
914
915typedef union drm_wait_vblank32 {
916 struct drm_wait_vblank_request32 request;
917 struct drm_wait_vblank_reply32 reply;
918} drm_wait_vblank32_t;
919
920static int compat_drm_wait_vblank(struct file *file, unsigned int cmd,
921 unsigned long arg)
922{
923 drm_wait_vblank32_t __user *argp = (void __user *)arg;
924 drm_wait_vblank32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000925 union drm_wait_vblank __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000926 int err;
927
928 if (copy_from_user(&req32, argp, sizeof(req32)))
929 return -EFAULT;
930
931 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300932 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000933 || __put_user(req32.request.type, &request->request.type)
934 || __put_user(req32.request.sequence, &request->request.sequence)
935 || __put_user(req32.request.signal, &request->request.signal))
936 return -EFAULT;
937
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000938 err = drm_ioctl(file, DRM_IOCTL_WAIT_VBLANK, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000939 if (err)
940 return err;
941
942 if (__get_user(req32.reply.type, &request->reply.type)
943 || __get_user(req32.reply.sequence, &request->reply.sequence)
944 || __get_user(req32.reply.tval_sec, &request->reply.tval_sec)
945 || __get_user(req32.reply.tval_usec, &request->reply.tval_usec))
946 return -EFAULT;
947
948 if (copy_to_user(argp, &req32, sizeof(req32)))
949 return -EFAULT;
950
951 return 0;
952}
953
Kristian H. Kristensena9885882016-09-13 14:20:45 -0700954#if defined(CONFIG_X86) || defined(CONFIG_IA64)
Tvrtko Ursulinc631d5f2015-07-14 11:13:08 +0100955typedef struct drm_mode_fb_cmd232 {
956 u32 fb_id;
957 u32 width;
958 u32 height;
959 u32 pixel_format;
960 u32 flags;
961 u32 handles[4];
962 u32 pitches[4];
963 u32 offsets[4];
964 u64 modifier[4];
965} __attribute__((packed)) drm_mode_fb_cmd232_t;
966
967static int compat_drm_mode_addfb2(struct file *file, unsigned int cmd,
968 unsigned long arg)
969{
970 struct drm_mode_fb_cmd232 __user *argp = (void __user *)arg;
971 struct drm_mode_fb_cmd232 req32;
972 struct drm_mode_fb_cmd2 __user *req64;
973 int i;
974 int err;
975
976 if (copy_from_user(&req32, argp, sizeof(req32)))
977 return -EFAULT;
978
979 req64 = compat_alloc_user_space(sizeof(*req64));
980
981 if (!access_ok(VERIFY_WRITE, req64, sizeof(*req64))
982 || __put_user(req32.width, &req64->width)
983 || __put_user(req32.height, &req64->height)
984 || __put_user(req32.pixel_format, &req64->pixel_format)
985 || __put_user(req32.flags, &req64->flags))
986 return -EFAULT;
987
988 for (i = 0; i < 4; i++) {
989 if (__put_user(req32.handles[i], &req64->handles[i]))
990 return -EFAULT;
991 if (__put_user(req32.pitches[i], &req64->pitches[i]))
992 return -EFAULT;
993 if (__put_user(req32.offsets[i], &req64->offsets[i]))
994 return -EFAULT;
995 if (__put_user(req32.modifier[i], &req64->modifier[i]))
996 return -EFAULT;
997 }
998
999 err = drm_ioctl(file, DRM_IOCTL_MODE_ADDFB2, (unsigned long)req64);
1000 if (err)
1001 return err;
1002
1003 if (__get_user(req32.fb_id, &req64->fb_id))
1004 return -EFAULT;
1005
1006 if (copy_to_user(argp, &req32, sizeof(req32)))
1007 return -EFAULT;
1008
1009 return 0;
1010}
Kristian H. Kristensena9885882016-09-13 14:20:45 -07001011#endif
Tvrtko Ursulinc631d5f2015-07-14 11:13:08 +01001012
Al Viro9f43e542017-05-24 13:59:22 -04001013static struct {
1014 drm_ioctl_compat_t *fn;
1015 char *name;
1016} drm_compat_ioctls[] = {
Al Viroff22ff92017-05-24 13:42:27 -04001017#define DRM_IOCTL32_DEF(n, f) [DRM_IOCTL_NR(n##32)] = {.fn = f, .name = #n}
Al Viro012c6742017-05-24 14:11:03 -04001018 DRM_IOCTL32_DEF(DRM_IOCTL_VERSION, compat_drm_version),
Al Viro17e3dad2017-05-24 14:15:20 -04001019 DRM_IOCTL32_DEF(DRM_IOCTL_GET_UNIQUE, compat_drm_getunique),
Al Viro8c4fe492017-05-24 14:26:39 -04001020 DRM_IOCTL32_DEF(DRM_IOCTL_GET_MAP, compat_drm_getmap),
Al Viro9e926622017-05-24 19:10:32 -04001021 DRM_IOCTL32_DEF(DRM_IOCTL_GET_CLIENT, compat_drm_getclient),
Al Viro8547ee92017-05-24 19:18:06 -04001022 DRM_IOCTL32_DEF(DRM_IOCTL_GET_STATS, compat_drm_getstats),
Al Viro56c275c2017-05-24 14:20:21 -04001023 DRM_IOCTL32_DEF(DRM_IOCTL_SET_UNIQUE, compat_drm_setunique),
Al Viro9f43e542017-05-24 13:59:22 -04001024 [DRM_IOCTL_NR(DRM_IOCTL_ADD_MAP32)].fn = compat_drm_addmap,
Al Viroff22ff92017-05-24 13:42:27 -04001025 DRM_IOCTL32_DEF(DRM_IOCTL_ADD_BUFS, compat_drm_addbufs),
Al Viro9f43e542017-05-24 13:59:22 -04001026 [DRM_IOCTL_NR(DRM_IOCTL_MARK_BUFS32)].fn = compat_drm_markbufs,
Al Viro5c7640a2017-05-24 17:54:09 -04001027 DRM_IOCTL32_DEF(DRM_IOCTL_INFO_BUFS, compat_drm_infobufs),
Al Viro9f43e542017-05-24 13:59:22 -04001028 [DRM_IOCTL_NR(DRM_IOCTL_MAP_BUFS32)].fn = compat_drm_mapbufs,
1029 [DRM_IOCTL_NR(DRM_IOCTL_FREE_BUFS32)].fn = compat_drm_freebufs,
1030 [DRM_IOCTL_NR(DRM_IOCTL_RM_MAP32)].fn = compat_drm_rmmap,
1031 [DRM_IOCTL_NR(DRM_IOCTL_SET_SAREA_CTX32)].fn = compat_drm_setsareactx,
1032 [DRM_IOCTL_NR(DRM_IOCTL_GET_SAREA_CTX32)].fn = compat_drm_getsareactx,
1033 [DRM_IOCTL_NR(DRM_IOCTL_RES_CTX32)].fn = compat_drm_resctx,
1034 [DRM_IOCTL_NR(DRM_IOCTL_DMA32)].fn = compat_drm_dma,
Daniel Vettera7fb8a22015-09-09 16:45:52 +02001035#if IS_ENABLED(CONFIG_AGP)
Al Viro9f43e542017-05-24 13:59:22 -04001036 [DRM_IOCTL_NR(DRM_IOCTL_AGP_ENABLE32)].fn = compat_drm_agp_enable,
1037 [DRM_IOCTL_NR(DRM_IOCTL_AGP_INFO32)].fn = compat_drm_agp_info,
1038 [DRM_IOCTL_NR(DRM_IOCTL_AGP_ALLOC32)].fn = compat_drm_agp_alloc,
1039 [DRM_IOCTL_NR(DRM_IOCTL_AGP_FREE32)].fn = compat_drm_agp_free,
1040 [DRM_IOCTL_NR(DRM_IOCTL_AGP_BIND32)].fn = compat_drm_agp_bind,
1041 [DRM_IOCTL_NR(DRM_IOCTL_AGP_UNBIND32)].fn = compat_drm_agp_unbind,
Dave Airlie9a186642005-06-23 21:29:18 +10001042#endif
Al Viro9f43e542017-05-24 13:59:22 -04001043 [DRM_IOCTL_NR(DRM_IOCTL_SG_ALLOC32)].fn = compat_drm_sg_alloc,
1044 [DRM_IOCTL_NR(DRM_IOCTL_SG_FREE32)].fn = compat_drm_sg_free,
David Miller09e40d62009-02-18 01:35:21 -08001045#if defined(CONFIG_X86) || defined(CONFIG_IA64)
Al Viro9f43e542017-05-24 13:59:22 -04001046 [DRM_IOCTL_NR(DRM_IOCTL_UPDATE_DRAW32)].fn = compat_drm_update_draw,
David Miller09e40d62009-02-18 01:35:21 -08001047#endif
Al Viro9f43e542017-05-24 13:59:22 -04001048 [DRM_IOCTL_NR(DRM_IOCTL_WAIT_VBLANK32)].fn = compat_drm_wait_vblank,
Kristian H. Kristensena9885882016-09-13 14:20:45 -07001049#if defined(CONFIG_X86) || defined(CONFIG_IA64)
Al Viro9f43e542017-05-24 13:59:22 -04001050 [DRM_IOCTL_NR(DRM_IOCTL_MODE_ADDFB232)].fn = compat_drm_mode_addfb2,
Kristian H. Kristensena9885882016-09-13 14:20:45 -07001051#endif
Dave Airlie9a186642005-06-23 21:29:18 +10001052};
1053
1054/**
Daniel Vetter26409812017-04-04 11:52:57 +02001055 * drm_compat_ioctl - 32bit IOCTL compatibility handler for DRM drivers
1056 * @filp: file this ioctl is called on
1057 * @cmd: ioctl cmd number
1058 * @arg: user argument
Dave Airlie9a186642005-06-23 21:29:18 +10001059 *
Daniel Vetter26409812017-04-04 11:52:57 +02001060 * Compatibility handler for 32 bit userspace running on 64 kernels. All actual
1061 * IOCTL handling is forwarded to drm_ioctl(), while marshalling structures as
1062 * appropriate. Note that this only handles DRM core IOCTLs, if the driver has
1063 * botched IOCTL itself, it must handle those by wrapping this function.
1064 *
1065 * Returns:
1066 * Zero on success, negative error code on failure.
Dave Airlie9a186642005-06-23 21:29:18 +10001067 */
1068long drm_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1069{
1070 unsigned int nr = DRM_IOCTL_NR(cmd);
1071 drm_ioctl_compat_t *fn;
1072 int ret;
1073
Ian Romanick7ffa05e2007-11-22 17:02:08 +10001074 /* Assume that ioctls without an explicit compat routine will just
1075 * work. This may not always be a good assumption, but it's better
1076 * than always failing.
1077 */
Dave Airlie3d774612006-08-07 20:07:43 +10001078 if (nr >= ARRAY_SIZE(drm_compat_ioctls))
Arnd Bergmanned8b6702009-12-16 22:17:09 +00001079 return drm_ioctl(filp, cmd, arg);
Dave Airlie9a186642005-06-23 21:29:18 +10001080
Al Viro9f43e542017-05-24 13:59:22 -04001081 fn = drm_compat_ioctls[nr].fn;
1082 if (!fn)
1083 return drm_ioctl(filp, cmd, arg);
Dave Airlie9a186642005-06-23 21:29:18 +10001084
Al Viro9f43e542017-05-24 13:59:22 -04001085 if (drm_compat_ioctls[nr].name) {
1086 struct drm_file *file_priv = filp->private_data;
1087 DRM_DEBUG("pid=%d, dev=0x%lx, auth=%d, %s\n",
1088 task_pid_nr(current),
1089 (long)old_encode_dev(file_priv->minor->kdev->devt),
1090 file_priv->authenticated,
1091 drm_compat_ioctls[nr].name);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001092 ret = (*fn) (filp, cmd, arg);
Al Viro9f43e542017-05-24 13:59:22 -04001093 if (ret)
1094 DRM_DEBUG("ret = %d\n", ret);
1095 } else {
1096 ret = (*fn) (filp, cmd, arg);
1097 }
Dave Airlie9a186642005-06-23 21:29:18 +10001098 return ret;
1099}
Dave Airlie9a186642005-06-23 21:29:18 +10001100EXPORT_SYMBOL(drm_compat_ioctl);