blob: b986caa6643098ae75efbea72fea8f1a73cc16b3 [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;
Al Virob36180a2017-05-24 19:27:36 -0400197 struct drm_map map;
Dave Airlie9a186642005-06-23 21:29:18 +1000198 int err;
Dave Airlie9a186642005-06-23 21:29:18 +1000199
200 if (copy_from_user(&m32, argp, sizeof(m32)))
201 return -EFAULT;
202
Al Virob36180a2017-05-24 19:27:36 -0400203 map.offset = m32.offset;
204 map.size = m32.size;
205 map.type = m32.type;
206 map.flags = m32.flags;
Dave Airlie9a186642005-06-23 21:29:18 +1000207
Al Virob36180a2017-05-24 19:27:36 -0400208 err = drm_ioctl_kernel(file, drm_legacy_addmap_ioctl, &map,
209 DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
Dave Airlie9a186642005-06-23 21:29:18 +1000210 if (err)
211 return err;
212
Al Virob36180a2017-05-24 19:27:36 -0400213 m32.offset = map.offset;
214 m32.mtrr = map.mtrr;
215 m32.handle = ptr_to_compat(map.handle);
216 if (map.handle != compat_ptr(m32.handle))
Joe Perches499447d2017-02-28 04:55:53 -0800217 pr_err_ratelimited("compat_drm_addmap truncated handle %p for type %d offset %x\n",
Al Virob36180a2017-05-24 19:27:36 -0400218 map.handle, m32.type, m32.offset);
Dave Airlie9a186642005-06-23 21:29:18 +1000219
220 if (copy_to_user(argp, &m32, sizeof(m32)))
221 return -EFAULT;
222
223 return 0;
224}
225
226static int compat_drm_rmmap(struct file *file, unsigned int cmd,
227 unsigned long arg)
228{
229 drm_map32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000230 struct drm_map __user *map;
Dave Airlie9a186642005-06-23 21:29:18 +1000231 u32 handle;
232
233 if (get_user(handle, &argp->handle))
234 return -EFAULT;
235
236 map = compat_alloc_user_space(sizeof(*map));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300237 if (!map)
Dave Airlie9a186642005-06-23 21:29:18 +1000238 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000239 if (__put_user((void *)(unsigned long)handle, &map->handle))
Dave Airlie9a186642005-06-23 21:29:18 +1000240 return -EFAULT;
241
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000242 return drm_ioctl(file, DRM_IOCTL_RM_MAP, (unsigned long)map);
Dave Airlie9a186642005-06-23 21:29:18 +1000243}
244
245typedef struct drm_client32 {
Daniel Vetter26409812017-04-04 11:52:57 +0200246 int idx; /* Which client desired? */
247 int auth; /* Is client authenticated? */
248 u32 pid; /* Process ID */
249 u32 uid; /* User ID */
250 u32 magic; /* Magic */
251 u32 iocs; /* Ioctl count */
Dave Airlie9a186642005-06-23 21:29:18 +1000252} drm_client32_t;
253
254static int compat_drm_getclient(struct file *file, unsigned int cmd,
255 unsigned long arg)
256{
257 drm_client32_t c32;
258 drm_client32_t __user *argp = (void __user *)arg;
Al Viro9e926622017-05-24 19:10:32 -0400259 struct drm_client client;
260 int err;
Dave Airlie9a186642005-06-23 21:29:18 +1000261
Al Viro9e926622017-05-24 19:10:32 -0400262 if (copy_from_user(&c32, argp, sizeof(c32)))
Dave Airlie9a186642005-06-23 21:29:18 +1000263 return -EFAULT;
264
Al Viro9e926622017-05-24 19:10:32 -0400265 client.idx = c32.idx;
Dave Airlie9a186642005-06-23 21:29:18 +1000266
Al Viro9e926622017-05-24 19:10:32 -0400267 err = drm_ioctl_kernel(file, drm_getclient, &client, DRM_UNLOCKED);
Dave Airlie9a186642005-06-23 21:29:18 +1000268 if (err)
269 return err;
270
Al Viro9e926622017-05-24 19:10:32 -0400271 c32.idx = client.idx;
272 c32.auth = client.auth;
273 c32.pid = client.pid;
274 c32.uid = client.uid;
275 c32.magic = client.magic;
276 c32.iocs = client.iocs;
Dave Airlie9a186642005-06-23 21:29:18 +1000277
278 if (copy_to_user(argp, &c32, sizeof(c32)))
279 return -EFAULT;
280 return 0;
281}
282
283typedef struct drm_stats32 {
284 u32 count;
285 struct {
286 u32 value;
Dave Airlieff4135a2007-07-16 13:53:57 +1000287 enum drm_stat_type type;
Dave Airlie9a186642005-06-23 21:29:18 +1000288 } data[15];
289} drm_stats32_t;
290
291static int compat_drm_getstats(struct file *file, unsigned int cmd,
292 unsigned long arg)
293{
Dave Airlie9a186642005-06-23 21:29:18 +1000294 drm_stats32_t __user *argp = (void __user *)arg;
Al Viro8547ee92017-05-24 19:18:06 -0400295 int err;
Dave Airlie9a186642005-06-23 21:29:18 +1000296
Al Viro8547ee92017-05-24 19:18:06 -0400297 err = drm_ioctl_kernel(file, drm_noop, NULL, DRM_UNLOCKED);
Dave Airlie9a186642005-06-23 21:29:18 +1000298 if (err)
299 return err;
300
Al Viro8547ee92017-05-24 19:18:06 -0400301 if (clear_user(argp, sizeof(drm_stats32_t)))
Dave Airlie9a186642005-06-23 21:29:18 +1000302 return -EFAULT;
303 return 0;
304}
305
306typedef struct drm_buf_desc32 {
Daniel Vetter26409812017-04-04 11:52:57 +0200307 int count; /* Number of buffers of this size */
308 int size; /* Size in bytes */
309 int low_mark; /* Low water mark */
310 int high_mark; /* High water mark */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000311 int flags;
Daniel Vetter26409812017-04-04 11:52:57 +0200312 u32 agp_start; /* Start address in the AGP aperture */
Dave Airlie9a186642005-06-23 21:29:18 +1000313} drm_buf_desc32_t;
314
315static int compat_drm_addbufs(struct file *file, unsigned int cmd,
316 unsigned long arg)
317{
318 drm_buf_desc32_t __user *argp = (void __user *)arg;
Al Viroff22ff92017-05-24 13:42:27 -0400319 drm_buf_desc32_t desc32;
320 struct drm_buf_desc desc;
Dave Airlie9a186642005-06-23 21:29:18 +1000321 int err;
Dave Airlie9a186642005-06-23 21:29:18 +1000322
Al Viroff22ff92017-05-24 13:42:27 -0400323 if (copy_from_user(&desc32, argp, sizeof(drm_buf_desc32_t)))
Dave Airlie9a186642005-06-23 21:29:18 +1000324 return -EFAULT;
325
Al Viroff22ff92017-05-24 13:42:27 -0400326 desc = (struct drm_buf_desc){
327 desc32.count, desc32.size, desc32.low_mark, desc32.high_mark,
328 desc32.flags, desc32.agp_start
329 };
Dave Airlie9a186642005-06-23 21:29:18 +1000330
Al Viroff22ff92017-05-24 13:42:27 -0400331 err = drm_ioctl_kernel(file, drm_legacy_addbufs, &desc,
332 DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
Dave Airlie9a186642005-06-23 21:29:18 +1000333 if (err)
334 return err;
335
Al Viroff22ff92017-05-24 13:42:27 -0400336 desc32 = (drm_buf_desc32_t){
337 desc.count, desc.size, desc.low_mark, desc.high_mark,
338 desc.flags, desc.agp_start
339 };
340 if (copy_to_user(argp, &desc32, sizeof(drm_buf_desc32_t)))
Dave Airlie9a186642005-06-23 21:29:18 +1000341 return -EFAULT;
342
343 return 0;
344}
345
346static int compat_drm_markbufs(struct file *file, unsigned int cmd,
347 unsigned long arg)
348{
349 drm_buf_desc32_t b32;
350 drm_buf_desc32_t __user *argp = (void __user *)arg;
Al Virodbae7402017-05-24 19:30:28 -0400351 struct drm_buf_desc __user buf;
Dave Airlie9a186642005-06-23 21:29:18 +1000352
353 if (copy_from_user(&b32, argp, sizeof(b32)))
354 return -EFAULT;
355
Al Virodbae7402017-05-24 19:30:28 -0400356 buf.size = b32.size;
357 buf.low_mark = b32.low_mark;
358 buf.high_mark = b32.high_mark;
Dave Airlie9a186642005-06-23 21:29:18 +1000359
Al Virodbae7402017-05-24 19:30:28 -0400360 return drm_ioctl_kernel(file, drm_legacy_markbufs, &buf,
361 DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
Dave Airlie9a186642005-06-23 21:29:18 +1000362}
363
364typedef struct drm_buf_info32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000365 int count; /**< Entries in list */
366 u32 list;
Dave Airlie9a186642005-06-23 21:29:18 +1000367} drm_buf_info32_t;
368
Al Viro5c7640a2017-05-24 17:54:09 -0400369static int copy_one_buf32(void *data, int count, struct drm_buf_entry *from)
370{
371 drm_buf_info32_t *request = data;
372 drm_buf_desc32_t __user *to = compat_ptr(request->list);
373 drm_buf_desc32_t v = {.count = from->buf_count,
374 .size = from->buf_size,
375 .low_mark = from->low_mark,
376 .high_mark = from->high_mark};
377 return copy_to_user(to + count, &v, offsetof(drm_buf_desc32_t, flags));
378}
379
380static int drm_legacy_infobufs32(struct drm_device *dev, void *data,
381 struct drm_file *file_priv)
382{
383 drm_buf_info32_t *request = data;
384 return __drm_legacy_infobufs(dev, data, &request->count, copy_one_buf32);
385}
386
Dave Airlie9a186642005-06-23 21:29:18 +1000387static int compat_drm_infobufs(struct file *file, unsigned int cmd,
388 unsigned long arg)
389{
390 drm_buf_info32_t req32;
391 drm_buf_info32_t __user *argp = (void __user *)arg;
Al Viro5c7640a2017-05-24 17:54:09 -0400392 int err;
Dave Airlie9a186642005-06-23 21:29:18 +1000393
394 if (copy_from_user(&req32, argp, sizeof(req32)))
395 return -EFAULT;
396
Al Viro5c7640a2017-05-24 17:54:09 -0400397 if (req32.count < 0)
398 req32.count = 0;
Dave Airlie9a186642005-06-23 21:29:18 +1000399
Al Viro5c7640a2017-05-24 17:54:09 -0400400 err = drm_ioctl_kernel(file, drm_legacy_infobufs32, &req32, DRM_AUTH);
Dave Airlie9a186642005-06-23 21:29:18 +1000401 if (err)
402 return err;
403
Al Viro5c7640a2017-05-24 17:54:09 -0400404 if (put_user(req32.count, &argp->count))
Dave Airlie9a186642005-06-23 21:29:18 +1000405 return -EFAULT;
406
407 return 0;
408}
409
410typedef struct drm_buf_pub32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000411 int idx; /**< Index into the master buffer list */
412 int total; /**< Buffer size */
413 int used; /**< Amount of buffer in use (for DMA) */
414 u32 address; /**< Address of buffer */
Dave Airlie9a186642005-06-23 21:29:18 +1000415} drm_buf_pub32_t;
416
417typedef struct drm_buf_map32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000418 int count; /**< Length of the buffer list */
419 u32 virtual; /**< Mmap'd area in user-virtual */
420 u32 list; /**< Buffer information */
Dave Airlie9a186642005-06-23 21:29:18 +1000421} drm_buf_map32_t;
422
423static int compat_drm_mapbufs(struct file *file, unsigned int cmd,
424 unsigned long arg)
425{
426 drm_buf_map32_t __user *argp = (void __user *)arg;
427 drm_buf_map32_t req32;
428 drm_buf_pub32_t __user *list32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000429 struct drm_buf_map __user *request;
430 struct drm_buf_pub __user *list;
Dave Airlie9a186642005-06-23 21:29:18 +1000431 int i, err;
432 int count, actual;
433 size_t nbytes;
434 void __user *addr;
435
436 if (copy_from_user(&req32, argp, sizeof(req32)))
437 return -EFAULT;
438 count = req32.count;
439 list32 = (void __user *)(unsigned long)req32.list;
440
441 if (count < 0)
442 return -EINVAL;
Dave Airlieff4135a2007-07-16 13:53:57 +1000443 nbytes = sizeof(*request) + count * sizeof(struct drm_buf_pub);
Dave Airlie9a186642005-06-23 21:29:18 +1000444 request = compat_alloc_user_space(nbytes);
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300445 if (!request)
Dave Airlie9a186642005-06-23 21:29:18 +1000446 return -EFAULT;
Dave Airlieff4135a2007-07-16 13:53:57 +1000447 list = (struct drm_buf_pub *) (request + 1);
Dave Airlie9a186642005-06-23 21:29:18 +1000448
449 if (__put_user(count, &request->count)
450 || __put_user(list, &request->list))
451 return -EFAULT;
452
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000453 err = drm_ioctl(file, DRM_IOCTL_MAP_BUFS, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000454 if (err)
455 return err;
456
457 if (__get_user(actual, &request->count))
458 return -EFAULT;
459 if (count >= actual)
460 for (i = 0; i < actual; ++i)
461 if (__copy_in_user(&list32[i], &list[i],
Dave Airlieff4135a2007-07-16 13:53:57 +1000462 offsetof(struct drm_buf_pub, address))
Dave Airlie9a186642005-06-23 21:29:18 +1000463 || __get_user(addr, &list[i].address)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000464 || __put_user((unsigned long)addr,
Dave Airlie9a186642005-06-23 21:29:18 +1000465 &list32[i].address))
466 return -EFAULT;
467
468 if (__put_user(actual, &argp->count)
469 || __get_user(addr, &request->virtual)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000470 || __put_user((unsigned long)addr, &argp->virtual))
Dave Airlie9a186642005-06-23 21:29:18 +1000471 return -EFAULT;
472
473 return 0;
474}
475
476typedef struct drm_buf_free32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000477 int count;
478 u32 list;
Dave Airlie9a186642005-06-23 21:29:18 +1000479} drm_buf_free32_t;
480
481static int compat_drm_freebufs(struct file *file, unsigned int cmd,
482 unsigned long arg)
483{
484 drm_buf_free32_t req32;
Al Viro8c9e4cf2017-05-25 12:44:05 -0400485 struct drm_buf_free request;
Dave Airlie9a186642005-06-23 21:29:18 +1000486 drm_buf_free32_t __user *argp = (void __user *)arg;
487
488 if (copy_from_user(&req32, argp, sizeof(req32)))
489 return -EFAULT;
490
Al Viro8c9e4cf2017-05-25 12:44:05 -0400491 request.count = req32.count;
492 request.list = compat_ptr(req32.list);
493 return drm_ioctl_kernel(file, drm_legacy_freebufs, &request, DRM_AUTH);
Dave Airlie9a186642005-06-23 21:29:18 +1000494}
495
496typedef struct drm_ctx_priv_map32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000497 unsigned int ctx_id; /**< Context requesting private mapping */
498 u32 handle; /**< Handle of map */
Dave Airlie9a186642005-06-23 21:29:18 +1000499} drm_ctx_priv_map32_t;
500
501static int compat_drm_setsareactx(struct file *file, unsigned int cmd,
502 unsigned long arg)
503{
504 drm_ctx_priv_map32_t req32;
Al Viro7900c602017-05-25 12:47:00 -0400505 struct drm_ctx_priv_map request;
Dave Airlie9a186642005-06-23 21:29:18 +1000506 drm_ctx_priv_map32_t __user *argp = (void __user *)arg;
507
508 if (copy_from_user(&req32, argp, sizeof(req32)))
509 return -EFAULT;
510
Al Viro7900c602017-05-25 12:47:00 -0400511 request.ctx_id = req32.ctx_id;
512 request.handle = compat_ptr(req32.handle);
513 return drm_ioctl_kernel(file, drm_legacy_setsareactx, &request,
514 DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
Dave Airlie9a186642005-06-23 21:29:18 +1000515}
516
517static int compat_drm_getsareactx(struct file *file, unsigned int cmd,
518 unsigned long arg)
519{
Al Virod7631e32017-05-25 12:50:51 -0400520 struct drm_ctx_priv_map req;
521 drm_ctx_priv_map32_t req32;
Dave Airlie9a186642005-06-23 21:29:18 +1000522 drm_ctx_priv_map32_t __user *argp = (void __user *)arg;
523 int err;
Dave Airlie9a186642005-06-23 21:29:18 +1000524
Al Virod7631e32017-05-25 12:50:51 -0400525 if (copy_from_user(&req32, argp, sizeof(req32)))
Dave Airlie9a186642005-06-23 21:29:18 +1000526 return -EFAULT;
527
Al Virod7631e32017-05-25 12:50:51 -0400528 req.ctx_id = req32.ctx_id;
529 err = drm_ioctl_kernel(file, drm_legacy_getsareactx, &req, DRM_AUTH);
Dave Airlie9a186642005-06-23 21:29:18 +1000530 if (err)
531 return err;
532
Al Virod7631e32017-05-25 12:50:51 -0400533 req32.handle = ptr_to_compat(req.handle);
534 if (copy_to_user(argp, &req32, sizeof(req32)))
Dave Airlie9a186642005-06-23 21:29:18 +1000535 return -EFAULT;
536
537 return 0;
538}
539
540typedef struct drm_ctx_res32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000541 int count;
542 u32 contexts;
Dave Airlie9a186642005-06-23 21:29:18 +1000543} drm_ctx_res32_t;
544
545static int compat_drm_resctx(struct file *file, unsigned int cmd,
546 unsigned long arg)
547{
548 drm_ctx_res32_t __user *argp = (void __user *)arg;
549 drm_ctx_res32_t res32;
Al Viro9e075682017-05-25 12:53:59 -0400550 struct drm_ctx_res res;
Dave Airlie9a186642005-06-23 21:29:18 +1000551 int err;
552
553 if (copy_from_user(&res32, argp, sizeof(res32)))
554 return -EFAULT;
555
Al Viro9e075682017-05-25 12:53:59 -0400556 res.count = res32.count;
557 res.contexts = compat_ptr(res32.contexts);
558 err = drm_ioctl_kernel(file, drm_legacy_resctx, &res, DRM_AUTH);
Dave Airlie9a186642005-06-23 21:29:18 +1000559 if (err)
560 return err;
561
Al Viro9e075682017-05-25 12:53:59 -0400562 res32.count = res.count;
563 if (copy_to_user(argp, &res32, sizeof(res32)))
Dave Airlie9a186642005-06-23 21:29:18 +1000564 return -EFAULT;
565
566 return 0;
567}
568
569typedef struct drm_dma32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000570 int context; /**< Context handle */
571 int send_count; /**< Number of buffers to send */
572 u32 send_indices; /**< List of handles to buffers */
573 u32 send_sizes; /**< Lengths of data to send */
Dave Airlieff4135a2007-07-16 13:53:57 +1000574 enum drm_dma_flags flags; /**< Flags */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000575 int request_count; /**< Number of buffers requested */
576 int request_size; /**< Desired size for buffers */
577 u32 request_indices; /**< Buffer information */
578 u32 request_sizes;
579 int granted_count; /**< Number of buffers granted */
Dave Airlie9a186642005-06-23 21:29:18 +1000580} drm_dma32_t;
581
582static int compat_drm_dma(struct file *file, unsigned int cmd,
583 unsigned long arg)
584{
585 drm_dma32_t d32;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000586 drm_dma32_t __user *argp = (void __user *)arg;
Al Viroc6f08962017-05-25 12:58:47 -0400587 struct drm_dma d;
Dave Airlie9a186642005-06-23 21:29:18 +1000588 int err;
589
590 if (copy_from_user(&d32, argp, sizeof(d32)))
591 return -EFAULT;
592
Al Viroc6f08962017-05-25 12:58:47 -0400593 d.context = d32.context;
594 d.send_count = d32.send_count;
595 d.send_indices = compat_ptr(d32.send_indices);
596 d.send_sizes = compat_ptr(d32.send_sizes);
597 d.flags = d32.flags;
598 d.request_count = d32.request_count;
599 d.request_indices = compat_ptr(d32.request_indices);
600 d.request_sizes = compat_ptr(d32.request_sizes);
601 err = drm_ioctl_kernel(file, drm_legacy_dma_ioctl, &d, DRM_AUTH);
Dave Airlie9a186642005-06-23 21:29:18 +1000602 if (err)
603 return err;
604
Al Viroc6f08962017-05-25 12:58:47 -0400605 if (put_user(d.request_size, &argp->request_size)
606 || put_user(d.granted_count, &argp->granted_count))
Dave Airlie9a186642005-06-23 21:29:18 +1000607 return -EFAULT;
608
609 return 0;
610}
611
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200612#if IS_ENABLED(CONFIG_AGP)
Dave Airlie9a186642005-06-23 21:29:18 +1000613typedef struct drm_agp_mode32 {
614 u32 mode; /**< AGP mode */
615} drm_agp_mode32_t;
616
617static int compat_drm_agp_enable(struct file *file, unsigned int cmd,
618 unsigned long arg)
619{
620 drm_agp_mode32_t __user *argp = (void __user *)arg;
Al Viroe92673e2017-05-25 13:17:17 -0400621 struct drm_agp_mode mode;
Dave Airlie9a186642005-06-23 21:29:18 +1000622
Al Viroe92673e2017-05-25 13:17:17 -0400623 if (get_user(mode.mode, &argp->mode))
Dave Airlie9a186642005-06-23 21:29:18 +1000624 return -EFAULT;
625
Al Viroe92673e2017-05-25 13:17:17 -0400626 return drm_ioctl_kernel(file, drm_agp_enable_ioctl, &mode,
627 DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
Dave Airlie9a186642005-06-23 21:29:18 +1000628}
629
630typedef struct drm_agp_info32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000631 int agp_version_major;
632 int agp_version_minor;
633 u32 mode;
634 u32 aperture_base; /* physical address */
635 u32 aperture_size; /* bytes */
636 u32 memory_allowed; /* bytes */
637 u32 memory_used;
Dave Airlie9a186642005-06-23 21:29:18 +1000638
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000639 /* PCI information */
Dave Airlie9a186642005-06-23 21:29:18 +1000640 unsigned short id_vendor;
641 unsigned short id_device;
642} drm_agp_info32_t;
643
644static int compat_drm_agp_info(struct file *file, unsigned int cmd,
645 unsigned long arg)
646{
647 drm_agp_info32_t __user *argp = (void __user *)arg;
648 drm_agp_info32_t i32;
Al Viroe92673e2017-05-25 13:17:17 -0400649 struct drm_agp_info info;
Dave Airlie9a186642005-06-23 21:29:18 +1000650 int err;
651
Al Viroe92673e2017-05-25 13:17:17 -0400652 err = drm_ioctl_kernel(file, drm_agp_info_ioctl, &info, DRM_AUTH);
Dave Airlie9a186642005-06-23 21:29:18 +1000653 if (err)
654 return err;
655
Al Viroe92673e2017-05-25 13:17:17 -0400656 i32.agp_version_major = info.agp_version_major;
657 i32.agp_version_minor = info.agp_version_minor;
658 i32.mode = info.mode;
659 i32.aperture_base = info.aperture_base;
660 i32.aperture_size = info.aperture_size;
661 i32.memory_allowed = info.memory_allowed;
662 i32.memory_used = info.memory_used;
663 i32.id_vendor = info.id_vendor;
664 i32.id_device = info.id_device;
Dave Airlie9a186642005-06-23 21:29:18 +1000665 if (copy_to_user(argp, &i32, sizeof(i32)))
666 return -EFAULT;
667
668 return 0;
669}
670
671typedef struct drm_agp_buffer32 {
672 u32 size; /**< In bytes -- will round to page boundary */
673 u32 handle; /**< Used for binding / unbinding */
674 u32 type; /**< Type of memory to allocate */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000675 u32 physical; /**< Physical used by i810 */
Dave Airlie9a186642005-06-23 21:29:18 +1000676} drm_agp_buffer32_t;
677
678static int compat_drm_agp_alloc(struct file *file, unsigned int cmd,
679 unsigned long arg)
680{
681 drm_agp_buffer32_t __user *argp = (void __user *)arg;
682 drm_agp_buffer32_t req32;
Al Viroe92673e2017-05-25 13:17:17 -0400683 struct drm_agp_buffer request;
Dave Airlie9a186642005-06-23 21:29:18 +1000684 int err;
685
686 if (copy_from_user(&req32, argp, sizeof(req32)))
687 return -EFAULT;
688
Al Viroe92673e2017-05-25 13:17:17 -0400689 request.size = req32.size;
690 request.type = req32.type;
691 err = drm_ioctl_kernel(file, drm_agp_alloc_ioctl, &request,
692 DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
Dave Airlie9a186642005-06-23 21:29:18 +1000693 if (err)
694 return err;
695
Al Viroe92673e2017-05-25 13:17:17 -0400696 req32.handle = request.handle;
697 req32.physical = request.physical;
698 if (copy_to_user(argp, &req32, sizeof(req32))) {
699 drm_ioctl_kernel(file, drm_agp_free_ioctl, &request,
700 DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
Dave Airlie9a186642005-06-23 21:29:18 +1000701 return -EFAULT;
702 }
703
704 return 0;
705}
706
707static int compat_drm_agp_free(struct file *file, unsigned int cmd,
708 unsigned long arg)
709{
710 drm_agp_buffer32_t __user *argp = (void __user *)arg;
Al Viroe92673e2017-05-25 13:17:17 -0400711 struct drm_agp_buffer request;
Dave Airlie9a186642005-06-23 21:29:18 +1000712
Al Viroe92673e2017-05-25 13:17:17 -0400713 if (get_user(request.handle, &argp->handle))
Dave Airlie9a186642005-06-23 21:29:18 +1000714 return -EFAULT;
715
Al Viroe92673e2017-05-25 13:17:17 -0400716 return drm_ioctl_kernel(file, drm_agp_free_ioctl, &request,
717 DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
Dave Airlie9a186642005-06-23 21:29:18 +1000718}
719
720typedef struct drm_agp_binding32 {
721 u32 handle; /**< From drm_agp_buffer */
722 u32 offset; /**< In bytes -- will round to page boundary */
723} drm_agp_binding32_t;
724
725static int compat_drm_agp_bind(struct file *file, unsigned int cmd,
726 unsigned long arg)
727{
728 drm_agp_binding32_t __user *argp = (void __user *)arg;
729 drm_agp_binding32_t req32;
Al Viroe92673e2017-05-25 13:17:17 -0400730 struct drm_agp_binding request;
Dave Airlie9a186642005-06-23 21:29:18 +1000731
732 if (copy_from_user(&req32, argp, sizeof(req32)))
733 return -EFAULT;
734
Al Viroe92673e2017-05-25 13:17:17 -0400735 request.handle = req32.handle;
736 request.offset = req32.offset;
737 return drm_ioctl_kernel(file, drm_agp_bind_ioctl, &request,
738 DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
Dave Airlie9a186642005-06-23 21:29:18 +1000739}
740
741static int compat_drm_agp_unbind(struct file *file, unsigned int cmd,
742 unsigned long arg)
743{
744 drm_agp_binding32_t __user *argp = (void __user *)arg;
Al Viroe92673e2017-05-25 13:17:17 -0400745 struct drm_agp_binding request;
Dave Airlie9a186642005-06-23 21:29:18 +1000746
Al Viroe92673e2017-05-25 13:17:17 -0400747 if (get_user(request.handle, &argp->handle))
Dave Airlie9a186642005-06-23 21:29:18 +1000748 return -EFAULT;
749
Al Viroe92673e2017-05-25 13:17:17 -0400750 return drm_ioctl_kernel(file, drm_agp_unbind_ioctl, &request,
751 DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
Dave Airlie9a186642005-06-23 21:29:18 +1000752}
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200753#endif /* CONFIG_AGP */
Dave Airlie9a186642005-06-23 21:29:18 +1000754
755typedef struct drm_scatter_gather32 {
756 u32 size; /**< In bytes -- will round to page boundary */
757 u32 handle; /**< Used for mapping / unmapping */
758} drm_scatter_gather32_t;
759
760static int compat_drm_sg_alloc(struct file *file, unsigned int cmd,
761 unsigned long arg)
762{
763 drm_scatter_gather32_t __user *argp = (void __user *)arg;
Al Viro314ed732017-05-25 13:24:59 -0400764 struct drm_scatter_gather request;
Dave Airlie9a186642005-06-23 21:29:18 +1000765 int err;
Dave Airlie9a186642005-06-23 21:29:18 +1000766
Al Viro314ed732017-05-25 13:24:59 -0400767 if (get_user(request.size, &argp->size))
Dave Airlie9a186642005-06-23 21:29:18 +1000768 return -EFAULT;
769
Al Viro314ed732017-05-25 13:24:59 -0400770 err = drm_ioctl_kernel(file, drm_legacy_sg_alloc, &request,
771 DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
Dave Airlie9a186642005-06-23 21:29:18 +1000772 if (err)
773 return err;
774
775 /* XXX not sure about the handle conversion here... */
Al Viro314ed732017-05-25 13:24:59 -0400776 if (put_user(request.handle >> PAGE_SHIFT, &argp->handle))
Dave Airlie9a186642005-06-23 21:29:18 +1000777 return -EFAULT;
778
779 return 0;
780}
781
782static int compat_drm_sg_free(struct file *file, unsigned int cmd,
783 unsigned long arg)
784{
785 drm_scatter_gather32_t __user *argp = (void __user *)arg;
Al Viro314ed732017-05-25 13:24:59 -0400786 struct drm_scatter_gather request;
Dave Airlie9a186642005-06-23 21:29:18 +1000787 unsigned long x;
788
Al Viro314ed732017-05-25 13:24:59 -0400789 if (get_user(x, &argp->handle))
Dave Airlie9a186642005-06-23 21:29:18 +1000790 return -EFAULT;
Al Viro314ed732017-05-25 13:24:59 -0400791 request.handle = x << PAGE_SHIFT;
792 return drm_ioctl_kernel(file, drm_legacy_sg_free, &request,
793 DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
Dave Airlie9a186642005-06-23 21:29:18 +1000794}
795
David Miller09e40d62009-02-18 01:35:21 -0800796#if defined(CONFIG_X86) || defined(CONFIG_IA64)
Eric Anholt49568872008-10-21 11:38:50 -0700797typedef struct drm_update_draw32 {
798 drm_drawable_t handle;
799 unsigned int type;
800 unsigned int num;
801 /* 64-bit version has a 32-bit pad here */
802 u64 data; /**< Pointer */
803} __attribute__((packed)) drm_update_draw32_t;
804
805static int compat_drm_update_draw(struct file *file, unsigned int cmd,
806 unsigned long arg)
807{
808 drm_update_draw32_t update32;
809 struct drm_update_draw __user *request;
810 int err;
811
812 if (copy_from_user(&update32, (void __user *)arg, sizeof(update32)))
813 return -EFAULT;
814
815 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300816 if (!request ||
Eric Anholt49568872008-10-21 11:38:50 -0700817 __put_user(update32.handle, &request->handle) ||
818 __put_user(update32.type, &request->type) ||
819 __put_user(update32.num, &request->num) ||
820 __put_user(update32.data, &request->data))
821 return -EFAULT;
822
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000823 err = drm_ioctl(file, DRM_IOCTL_UPDATE_DRAW, (unsigned long)request);
Eric Anholt49568872008-10-21 11:38:50 -0700824 return err;
825}
David Miller09e40d62009-02-18 01:35:21 -0800826#endif
Eric Anholt49568872008-10-21 11:38:50 -0700827
Dave Airlie9a186642005-06-23 21:29:18 +1000828struct drm_wait_vblank_request32 {
Dave Airlieff4135a2007-07-16 13:53:57 +1000829 enum drm_vblank_seq_type type;
Dave Airlie9a186642005-06-23 21:29:18 +1000830 unsigned int sequence;
831 u32 signal;
832};
833
834struct drm_wait_vblank_reply32 {
Dave Airlieff4135a2007-07-16 13:53:57 +1000835 enum drm_vblank_seq_type type;
Dave Airlie9a186642005-06-23 21:29:18 +1000836 unsigned int sequence;
837 s32 tval_sec;
838 s32 tval_usec;
839};
840
841typedef union drm_wait_vblank32 {
842 struct drm_wait_vblank_request32 request;
843 struct drm_wait_vblank_reply32 reply;
844} drm_wait_vblank32_t;
845
846static int compat_drm_wait_vblank(struct file *file, unsigned int cmd,
847 unsigned long arg)
848{
849 drm_wait_vblank32_t __user *argp = (void __user *)arg;
850 drm_wait_vblank32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000851 union drm_wait_vblank __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000852 int err;
853
854 if (copy_from_user(&req32, argp, sizeof(req32)))
855 return -EFAULT;
856
857 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300858 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000859 || __put_user(req32.request.type, &request->request.type)
860 || __put_user(req32.request.sequence, &request->request.sequence)
861 || __put_user(req32.request.signal, &request->request.signal))
862 return -EFAULT;
863
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000864 err = drm_ioctl(file, DRM_IOCTL_WAIT_VBLANK, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000865 if (err)
866 return err;
867
868 if (__get_user(req32.reply.type, &request->reply.type)
869 || __get_user(req32.reply.sequence, &request->reply.sequence)
870 || __get_user(req32.reply.tval_sec, &request->reply.tval_sec)
871 || __get_user(req32.reply.tval_usec, &request->reply.tval_usec))
872 return -EFAULT;
873
874 if (copy_to_user(argp, &req32, sizeof(req32)))
875 return -EFAULT;
876
877 return 0;
878}
879
Kristian H. Kristensena9885882016-09-13 14:20:45 -0700880#if defined(CONFIG_X86) || defined(CONFIG_IA64)
Tvrtko Ursulinc631d5f2015-07-14 11:13:08 +0100881typedef struct drm_mode_fb_cmd232 {
882 u32 fb_id;
883 u32 width;
884 u32 height;
885 u32 pixel_format;
886 u32 flags;
887 u32 handles[4];
888 u32 pitches[4];
889 u32 offsets[4];
890 u64 modifier[4];
891} __attribute__((packed)) drm_mode_fb_cmd232_t;
892
893static int compat_drm_mode_addfb2(struct file *file, unsigned int cmd,
894 unsigned long arg)
895{
896 struct drm_mode_fb_cmd232 __user *argp = (void __user *)arg;
897 struct drm_mode_fb_cmd232 req32;
898 struct drm_mode_fb_cmd2 __user *req64;
899 int i;
900 int err;
901
902 if (copy_from_user(&req32, argp, sizeof(req32)))
903 return -EFAULT;
904
905 req64 = compat_alloc_user_space(sizeof(*req64));
906
907 if (!access_ok(VERIFY_WRITE, req64, sizeof(*req64))
908 || __put_user(req32.width, &req64->width)
909 || __put_user(req32.height, &req64->height)
910 || __put_user(req32.pixel_format, &req64->pixel_format)
911 || __put_user(req32.flags, &req64->flags))
912 return -EFAULT;
913
914 for (i = 0; i < 4; i++) {
915 if (__put_user(req32.handles[i], &req64->handles[i]))
916 return -EFAULT;
917 if (__put_user(req32.pitches[i], &req64->pitches[i]))
918 return -EFAULT;
919 if (__put_user(req32.offsets[i], &req64->offsets[i]))
920 return -EFAULT;
921 if (__put_user(req32.modifier[i], &req64->modifier[i]))
922 return -EFAULT;
923 }
924
925 err = drm_ioctl(file, DRM_IOCTL_MODE_ADDFB2, (unsigned long)req64);
926 if (err)
927 return err;
928
929 if (__get_user(req32.fb_id, &req64->fb_id))
930 return -EFAULT;
931
932 if (copy_to_user(argp, &req32, sizeof(req32)))
933 return -EFAULT;
934
935 return 0;
936}
Kristian H. Kristensena9885882016-09-13 14:20:45 -0700937#endif
Tvrtko Ursulinc631d5f2015-07-14 11:13:08 +0100938
Al Viro9f43e542017-05-24 13:59:22 -0400939static struct {
940 drm_ioctl_compat_t *fn;
941 char *name;
942} drm_compat_ioctls[] = {
Al Viroff22ff92017-05-24 13:42:27 -0400943#define DRM_IOCTL32_DEF(n, f) [DRM_IOCTL_NR(n##32)] = {.fn = f, .name = #n}
Al Viro012c6742017-05-24 14:11:03 -0400944 DRM_IOCTL32_DEF(DRM_IOCTL_VERSION, compat_drm_version),
Al Viro17e3dad2017-05-24 14:15:20 -0400945 DRM_IOCTL32_DEF(DRM_IOCTL_GET_UNIQUE, compat_drm_getunique),
Al Viro8c4fe492017-05-24 14:26:39 -0400946 DRM_IOCTL32_DEF(DRM_IOCTL_GET_MAP, compat_drm_getmap),
Al Viro9e926622017-05-24 19:10:32 -0400947 DRM_IOCTL32_DEF(DRM_IOCTL_GET_CLIENT, compat_drm_getclient),
Al Viro8547ee92017-05-24 19:18:06 -0400948 DRM_IOCTL32_DEF(DRM_IOCTL_GET_STATS, compat_drm_getstats),
Al Viro56c275c2017-05-24 14:20:21 -0400949 DRM_IOCTL32_DEF(DRM_IOCTL_SET_UNIQUE, compat_drm_setunique),
Al Virob36180a2017-05-24 19:27:36 -0400950 DRM_IOCTL32_DEF(DRM_IOCTL_ADD_MAP, compat_drm_addmap),
Al Viroff22ff92017-05-24 13:42:27 -0400951 DRM_IOCTL32_DEF(DRM_IOCTL_ADD_BUFS, compat_drm_addbufs),
Al Virodbae7402017-05-24 19:30:28 -0400952 DRM_IOCTL32_DEF(DRM_IOCTL_MARK_BUFS, compat_drm_markbufs),
Al Viro5c7640a2017-05-24 17:54:09 -0400953 DRM_IOCTL32_DEF(DRM_IOCTL_INFO_BUFS, compat_drm_infobufs),
Al Viro9f43e542017-05-24 13:59:22 -0400954 [DRM_IOCTL_NR(DRM_IOCTL_MAP_BUFS32)].fn = compat_drm_mapbufs,
Al Viro8c9e4cf2017-05-25 12:44:05 -0400955 DRM_IOCTL32_DEF(DRM_IOCTL_FREE_BUFS, compat_drm_freebufs),
Al Viro9f43e542017-05-24 13:59:22 -0400956 [DRM_IOCTL_NR(DRM_IOCTL_RM_MAP32)].fn = compat_drm_rmmap,
Al Viro7900c602017-05-25 12:47:00 -0400957 DRM_IOCTL32_DEF(DRM_IOCTL_SET_SAREA_CTX, compat_drm_setsareactx),
Al Virod7631e32017-05-25 12:50:51 -0400958 DRM_IOCTL32_DEF(DRM_IOCTL_GET_SAREA_CTX, compat_drm_getsareactx),
Al Viro9e075682017-05-25 12:53:59 -0400959 DRM_IOCTL32_DEF(DRM_IOCTL_RES_CTX, compat_drm_resctx),
Al Viroc6f08962017-05-25 12:58:47 -0400960 DRM_IOCTL32_DEF(DRM_IOCTL_DMA, compat_drm_dma),
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200961#if IS_ENABLED(CONFIG_AGP)
Al Viroe92673e2017-05-25 13:17:17 -0400962 DRM_IOCTL32_DEF(DRM_IOCTL_AGP_ENABLE, compat_drm_agp_enable),
963 DRM_IOCTL32_DEF(DRM_IOCTL_AGP_INFO, compat_drm_agp_info),
964 DRM_IOCTL32_DEF(DRM_IOCTL_AGP_ALLOC, compat_drm_agp_alloc),
965 DRM_IOCTL32_DEF(DRM_IOCTL_AGP_FREE, compat_drm_agp_free),
966 DRM_IOCTL32_DEF(DRM_IOCTL_AGP_BIND, compat_drm_agp_bind),
967 DRM_IOCTL32_DEF(DRM_IOCTL_AGP_UNBIND, compat_drm_agp_unbind),
Dave Airlie9a186642005-06-23 21:29:18 +1000968#endif
Al Viro314ed732017-05-25 13:24:59 -0400969 DRM_IOCTL32_DEF(DRM_IOCTL_SG_ALLOC, compat_drm_sg_alloc),
970 DRM_IOCTL32_DEF(DRM_IOCTL_SG_FREE, compat_drm_sg_free),
David Miller09e40d62009-02-18 01:35:21 -0800971#if defined(CONFIG_X86) || defined(CONFIG_IA64)
Al Viro9f43e542017-05-24 13:59:22 -0400972 [DRM_IOCTL_NR(DRM_IOCTL_UPDATE_DRAW32)].fn = compat_drm_update_draw,
David Miller09e40d62009-02-18 01:35:21 -0800973#endif
Al Viro9f43e542017-05-24 13:59:22 -0400974 [DRM_IOCTL_NR(DRM_IOCTL_WAIT_VBLANK32)].fn = compat_drm_wait_vblank,
Kristian H. Kristensena9885882016-09-13 14:20:45 -0700975#if defined(CONFIG_X86) || defined(CONFIG_IA64)
Al Viro9f43e542017-05-24 13:59:22 -0400976 [DRM_IOCTL_NR(DRM_IOCTL_MODE_ADDFB232)].fn = compat_drm_mode_addfb2,
Kristian H. Kristensena9885882016-09-13 14:20:45 -0700977#endif
Dave Airlie9a186642005-06-23 21:29:18 +1000978};
979
980/**
Daniel Vetter26409812017-04-04 11:52:57 +0200981 * drm_compat_ioctl - 32bit IOCTL compatibility handler for DRM drivers
982 * @filp: file this ioctl is called on
983 * @cmd: ioctl cmd number
984 * @arg: user argument
Dave Airlie9a186642005-06-23 21:29:18 +1000985 *
Daniel Vetter26409812017-04-04 11:52:57 +0200986 * Compatibility handler for 32 bit userspace running on 64 kernels. All actual
987 * IOCTL handling is forwarded to drm_ioctl(), while marshalling structures as
988 * appropriate. Note that this only handles DRM core IOCTLs, if the driver has
989 * botched IOCTL itself, it must handle those by wrapping this function.
990 *
991 * Returns:
992 * Zero on success, negative error code on failure.
Dave Airlie9a186642005-06-23 21:29:18 +1000993 */
994long drm_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
995{
996 unsigned int nr = DRM_IOCTL_NR(cmd);
997 drm_ioctl_compat_t *fn;
998 int ret;
999
Ian Romanick7ffa05e2007-11-22 17:02:08 +10001000 /* Assume that ioctls without an explicit compat routine will just
1001 * work. This may not always be a good assumption, but it's better
1002 * than always failing.
1003 */
Dave Airlie3d774612006-08-07 20:07:43 +10001004 if (nr >= ARRAY_SIZE(drm_compat_ioctls))
Arnd Bergmanned8b6702009-12-16 22:17:09 +00001005 return drm_ioctl(filp, cmd, arg);
Dave Airlie9a186642005-06-23 21:29:18 +10001006
Al Viro9f43e542017-05-24 13:59:22 -04001007 fn = drm_compat_ioctls[nr].fn;
1008 if (!fn)
1009 return drm_ioctl(filp, cmd, arg);
Dave Airlie9a186642005-06-23 21:29:18 +10001010
Al Viro9f43e542017-05-24 13:59:22 -04001011 if (drm_compat_ioctls[nr].name) {
1012 struct drm_file *file_priv = filp->private_data;
1013 DRM_DEBUG("pid=%d, dev=0x%lx, auth=%d, %s\n",
1014 task_pid_nr(current),
1015 (long)old_encode_dev(file_priv->minor->kdev->devt),
1016 file_priv->authenticated,
1017 drm_compat_ioctls[nr].name);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001018 ret = (*fn) (filp, cmd, arg);
Al Viro9f43e542017-05-24 13:59:22 -04001019 if (ret)
1020 DRM_DEBUG("ret = %d\n", ret);
1021 } else {
1022 ret = (*fn) (filp, cmd, arg);
1023 }
Dave Airlie9a186642005-06-23 21:29:18 +10001024 return ret;
1025}
Dave Airlie9a186642005-06-23 21:29:18 +10001026EXPORT_SYMBOL(drm_compat_ioctl);