xref: /linux/drivers/gpu/drm/drm_ioc32.c (revision ebf68996de0ab250c5d520eb2291ab65643e9a1e)
1 /*
2  * \file drm_ioc32.c
3  *
4  * 32-bit ioctl compatibility routines for the DRM.
5  *
6  * \author Paul Mackerras <paulus@samba.org>
7  *
8  * Copyright (C) Paul Mackerras 2005.
9  * All Rights Reserved.
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice (including the next
19  * paragraph) shall be included in all copies or substantial portions of the
20  * Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
25  * THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28  * IN THE SOFTWARE.
29  */
30 #include <linux/compat.h>
31 #include <linux/ratelimit.h>
32 #include <linux/export.h>
33 
34 #include <drm/drm_agpsupport.h>
35 #include <drm/drm_file.h>
36 #include <drm/drm_print.h>
37 
38 #include "drm_crtc_internal.h"
39 #include "drm_internal.h"
40 #include "drm_legacy.h"
41 
42 #define DRM_IOCTL_VERSION32		DRM_IOWR(0x00, drm_version32_t)
43 #define DRM_IOCTL_GET_UNIQUE32		DRM_IOWR(0x01, drm_unique32_t)
44 #define DRM_IOCTL_GET_MAP32		DRM_IOWR(0x04, drm_map32_t)
45 #define DRM_IOCTL_GET_CLIENT32		DRM_IOWR(0x05, drm_client32_t)
46 #define DRM_IOCTL_GET_STATS32		DRM_IOR( 0x06, drm_stats32_t)
47 
48 #define DRM_IOCTL_SET_UNIQUE32		DRM_IOW( 0x10, drm_unique32_t)
49 #define DRM_IOCTL_ADD_MAP32		DRM_IOWR(0x15, drm_map32_t)
50 #define DRM_IOCTL_ADD_BUFS32		DRM_IOWR(0x16, drm_buf_desc32_t)
51 #define DRM_IOCTL_MARK_BUFS32		DRM_IOW( 0x17, drm_buf_desc32_t)
52 #define DRM_IOCTL_INFO_BUFS32		DRM_IOWR(0x18, drm_buf_info32_t)
53 #define DRM_IOCTL_MAP_BUFS32		DRM_IOWR(0x19, drm_buf_map32_t)
54 #define DRM_IOCTL_FREE_BUFS32		DRM_IOW( 0x1a, drm_buf_free32_t)
55 
56 #define DRM_IOCTL_RM_MAP32		DRM_IOW( 0x1b, drm_map32_t)
57 
58 #define DRM_IOCTL_SET_SAREA_CTX32	DRM_IOW( 0x1c, drm_ctx_priv_map32_t)
59 #define DRM_IOCTL_GET_SAREA_CTX32	DRM_IOWR(0x1d, drm_ctx_priv_map32_t)
60 
61 #define DRM_IOCTL_RES_CTX32		DRM_IOWR(0x26, drm_ctx_res32_t)
62 #define DRM_IOCTL_DMA32			DRM_IOWR(0x29, drm_dma32_t)
63 
64 #define DRM_IOCTL_AGP_ENABLE32		DRM_IOW( 0x32, drm_agp_mode32_t)
65 #define DRM_IOCTL_AGP_INFO32		DRM_IOR( 0x33, drm_agp_info32_t)
66 #define DRM_IOCTL_AGP_ALLOC32		DRM_IOWR(0x34, drm_agp_buffer32_t)
67 #define DRM_IOCTL_AGP_FREE32		DRM_IOW( 0x35, drm_agp_buffer32_t)
68 #define DRM_IOCTL_AGP_BIND32		DRM_IOW( 0x36, drm_agp_binding32_t)
69 #define DRM_IOCTL_AGP_UNBIND32		DRM_IOW( 0x37, drm_agp_binding32_t)
70 
71 #define DRM_IOCTL_SG_ALLOC32		DRM_IOW( 0x38, drm_scatter_gather32_t)
72 #define DRM_IOCTL_SG_FREE32		DRM_IOW( 0x39, drm_scatter_gather32_t)
73 
74 #define DRM_IOCTL_UPDATE_DRAW32		DRM_IOW( 0x3f, drm_update_draw32_t)
75 
76 #define DRM_IOCTL_WAIT_VBLANK32		DRM_IOWR(0x3a, drm_wait_vblank32_t)
77 
78 #define DRM_IOCTL_MODE_ADDFB232		DRM_IOWR(0xb8, drm_mode_fb_cmd232_t)
79 
80 typedef struct drm_version_32 {
81 	int version_major;	  /* Major version */
82 	int version_minor;	  /* Minor version */
83 	int version_patchlevel;	   /* Patch level */
84 	u32 name_len;		  /* Length of name buffer */
85 	u32 name;		  /* Name of driver */
86 	u32 date_len;		  /* Length of date buffer */
87 	u32 date;		  /* User-space buffer to hold date */
88 	u32 desc_len;		  /* Length of desc buffer */
89 	u32 desc;		  /* User-space buffer to hold desc */
90 } drm_version32_t;
91 
92 static int compat_drm_version(struct file *file, unsigned int cmd,
93 			      unsigned long arg)
94 {
95 	drm_version32_t v32;
96 	struct drm_version v;
97 	int err;
98 
99 	if (copy_from_user(&v32, (void __user *)arg, sizeof(v32)))
100 		return -EFAULT;
101 
102 	v = (struct drm_version) {
103 		.name_len = v32.name_len,
104 		.name = compat_ptr(v32.name),
105 		.date_len = v32.date_len,
106 		.date = compat_ptr(v32.date),
107 		.desc_len = v32.desc_len,
108 		.desc = compat_ptr(v32.desc),
109 	};
110 	err = drm_ioctl_kernel(file, drm_version, &v,
111 			       DRM_UNLOCKED|DRM_RENDER_ALLOW);
112 	if (err)
113 		return err;
114 
115 	v32.version_major = v.version_major;
116 	v32.version_minor = v.version_minor;
117 	v32.version_patchlevel = v.version_patchlevel;
118 	v32.name_len = v.name_len;
119 	v32.date_len = v.date_len;
120 	v32.desc_len = v.desc_len;
121 	if (copy_to_user((void __user *)arg, &v32, sizeof(v32)))
122 		return -EFAULT;
123 	return 0;
124 }
125 
126 typedef struct drm_unique32 {
127 	u32 unique_len;	/* Length of unique */
128 	u32 unique;	/* Unique name for driver instantiation */
129 } drm_unique32_t;
130 
131 static int compat_drm_getunique(struct file *file, unsigned int cmd,
132 				unsigned long arg)
133 {
134 	drm_unique32_t uq32;
135 	struct drm_unique uq;
136 	int err;
137 
138 	if (copy_from_user(&uq32, (void __user *)arg, sizeof(uq32)))
139 		return -EFAULT;
140 	uq = (struct drm_unique){
141 		.unique_len = uq32.unique_len,
142 		.unique = compat_ptr(uq32.unique),
143 	};
144 
145 	err = drm_ioctl_kernel(file, drm_getunique, &uq, DRM_UNLOCKED);
146 	if (err)
147 		return err;
148 
149 	uq32.unique_len = uq.unique_len;
150 	if (copy_to_user((void __user *)arg, &uq32, sizeof(uq32)))
151 		return -EFAULT;
152 	return 0;
153 }
154 
155 static int compat_drm_setunique(struct file *file, unsigned int cmd,
156 				unsigned long arg)
157 {
158 	/* it's dead */
159 	return -EINVAL;
160 }
161 
162 #if IS_ENABLED(CONFIG_DRM_LEGACY)
163 typedef struct drm_map32 {
164 	u32 offset;		/* Requested physical address (0 for SAREA) */
165 	u32 size;		/* Requested physical size (bytes) */
166 	enum drm_map_type type;	/* Type of memory to map */
167 	enum drm_map_flags flags;	/* Flags */
168 	u32 handle;		/* User-space: "Handle" to pass to mmap() */
169 	int mtrr;		/* MTRR slot used */
170 } drm_map32_t;
171 
172 static int compat_drm_getmap(struct file *file, unsigned int cmd,
173 			     unsigned long arg)
174 {
175 	drm_map32_t __user *argp = (void __user *)arg;
176 	drm_map32_t m32;
177 	struct drm_map map;
178 	int err;
179 
180 	if (copy_from_user(&m32, argp, sizeof(m32)))
181 		return -EFAULT;
182 
183 	map.offset = m32.offset;
184 	err = drm_ioctl_kernel(file, drm_legacy_getmap_ioctl, &map, DRM_UNLOCKED);
185 	if (err)
186 		return err;
187 
188 	m32.offset = map.offset;
189 	m32.size = map.size;
190 	m32.type = map.type;
191 	m32.flags = map.flags;
192 	m32.handle = ptr_to_compat((void __user *)map.handle);
193 	m32.mtrr = map.mtrr;
194 	if (copy_to_user(argp, &m32, sizeof(m32)))
195 		return -EFAULT;
196 	return 0;
197 
198 }
199 
200 static int compat_drm_addmap(struct file *file, unsigned int cmd,
201 			     unsigned long arg)
202 {
203 	drm_map32_t __user *argp = (void __user *)arg;
204 	drm_map32_t m32;
205 	struct drm_map map;
206 	int err;
207 
208 	if (copy_from_user(&m32, argp, sizeof(m32)))
209 		return -EFAULT;
210 
211 	map.offset = m32.offset;
212 	map.size = m32.size;
213 	map.type = m32.type;
214 	map.flags = m32.flags;
215 
216 	err = drm_ioctl_kernel(file, drm_legacy_addmap_ioctl, &map,
217 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
218 	if (err)
219 		return err;
220 
221 	m32.offset = map.offset;
222 	m32.mtrr = map.mtrr;
223 	m32.handle = ptr_to_compat((void __user *)map.handle);
224 	if (map.handle != compat_ptr(m32.handle))
225 		pr_err_ratelimited("compat_drm_addmap truncated handle %p for type %d offset %x\n",
226 				   map.handle, m32.type, m32.offset);
227 
228 	if (copy_to_user(argp, &m32, sizeof(m32)))
229 		return -EFAULT;
230 
231 	return 0;
232 }
233 
234 static int compat_drm_rmmap(struct file *file, unsigned int cmd,
235 			    unsigned long arg)
236 {
237 	drm_map32_t __user *argp = (void __user *)arg;
238 	struct drm_map map;
239 	u32 handle;
240 
241 	if (get_user(handle, &argp->handle))
242 		return -EFAULT;
243 	map.handle = compat_ptr(handle);
244 	return drm_ioctl_kernel(file, drm_legacy_rmmap_ioctl, &map, DRM_AUTH);
245 }
246 #endif
247 
248 typedef struct drm_client32 {
249 	int idx;	/* Which client desired? */
250 	int auth;	/* Is client authenticated? */
251 	u32 pid;	/* Process ID */
252 	u32 uid;	/* User ID */
253 	u32 magic;	/* Magic */
254 	u32 iocs;	/* Ioctl count */
255 } drm_client32_t;
256 
257 static int compat_drm_getclient(struct file *file, unsigned int cmd,
258 				unsigned long arg)
259 {
260 	drm_client32_t c32;
261 	drm_client32_t __user *argp = (void __user *)arg;
262 	struct drm_client client;
263 	int err;
264 
265 	if (copy_from_user(&c32, argp, sizeof(c32)))
266 		return -EFAULT;
267 
268 	client.idx = c32.idx;
269 
270 	err = drm_ioctl_kernel(file, drm_getclient, &client, DRM_UNLOCKED);
271 	if (err)
272 		return err;
273 
274 	c32.idx = client.idx;
275 	c32.auth = client.auth;
276 	c32.pid = client.pid;
277 	c32.uid = client.uid;
278 	c32.magic = client.magic;
279 	c32.iocs = client.iocs;
280 
281 	if (copy_to_user(argp, &c32, sizeof(c32)))
282 		return -EFAULT;
283 	return 0;
284 }
285 
286 typedef struct drm_stats32 {
287 	u32 count;
288 	struct {
289 		u32 value;
290 		enum drm_stat_type type;
291 	} data[15];
292 } drm_stats32_t;
293 
294 static int compat_drm_getstats(struct file *file, unsigned int cmd,
295 			       unsigned long arg)
296 {
297 	drm_stats32_t __user *argp = (void __user *)arg;
298 	int err;
299 
300 	err = drm_ioctl_kernel(file, drm_noop, NULL, DRM_UNLOCKED);
301 	if (err)
302 		return err;
303 
304 	if (clear_user(argp, sizeof(drm_stats32_t)))
305 		return -EFAULT;
306 	return 0;
307 }
308 
309 #if IS_ENABLED(CONFIG_DRM_LEGACY)
310 typedef struct drm_buf_desc32 {
311 	int count;		 /* Number of buffers of this size */
312 	int size;		 /* Size in bytes */
313 	int low_mark;		 /* Low water mark */
314 	int high_mark;		 /* High water mark */
315 	int flags;
316 	u32 agp_start;		 /* Start address in the AGP aperture */
317 } drm_buf_desc32_t;
318 
319 static int compat_drm_addbufs(struct file *file, unsigned int cmd,
320 			      unsigned long arg)
321 {
322 	drm_buf_desc32_t __user *argp = (void __user *)arg;
323 	drm_buf_desc32_t desc32;
324 	struct drm_buf_desc desc;
325 	int err;
326 
327 	if (copy_from_user(&desc32, argp, sizeof(drm_buf_desc32_t)))
328 		return -EFAULT;
329 
330 	desc = (struct drm_buf_desc){
331 		desc32.count, desc32.size, desc32.low_mark, desc32.high_mark,
332 		desc32.flags, desc32.agp_start
333 	};
334 
335 	err = drm_ioctl_kernel(file, drm_legacy_addbufs, &desc,
336 				   DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
337 	if (err)
338 		return err;
339 
340 	desc32 = (drm_buf_desc32_t){
341 		desc.count, desc.size, desc.low_mark, desc.high_mark,
342 		desc.flags, desc.agp_start
343 	};
344 	if (copy_to_user(argp, &desc32, sizeof(drm_buf_desc32_t)))
345 		return -EFAULT;
346 
347 	return 0;
348 }
349 
350 static int compat_drm_markbufs(struct file *file, unsigned int cmd,
351 			       unsigned long arg)
352 {
353 	drm_buf_desc32_t b32;
354 	drm_buf_desc32_t __user *argp = (void __user *)arg;
355 	struct drm_buf_desc buf;
356 
357 	if (copy_from_user(&b32, argp, sizeof(b32)))
358 		return -EFAULT;
359 
360 	buf.size = b32.size;
361 	buf.low_mark = b32.low_mark;
362 	buf.high_mark = b32.high_mark;
363 
364 	return drm_ioctl_kernel(file, drm_legacy_markbufs, &buf,
365 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
366 }
367 
368 typedef struct drm_buf_info32 {
369 	int count;		/**< Entries in list */
370 	u32 list;
371 } drm_buf_info32_t;
372 
373 static int copy_one_buf32(void *data, int count, struct drm_buf_entry *from)
374 {
375 	drm_buf_info32_t *request = data;
376 	drm_buf_desc32_t __user *to = compat_ptr(request->list);
377 	drm_buf_desc32_t v = {.count = from->buf_count,
378 			      .size = from->buf_size,
379 			      .low_mark = from->low_mark,
380 			      .high_mark = from->high_mark};
381 	return copy_to_user(to + count, &v, offsetof(drm_buf_desc32_t, flags));
382 }
383 
384 static int drm_legacy_infobufs32(struct drm_device *dev, void *data,
385 			struct drm_file *file_priv)
386 {
387 	drm_buf_info32_t *request = data;
388 	return __drm_legacy_infobufs(dev, data, &request->count, copy_one_buf32);
389 }
390 
391 static int compat_drm_infobufs(struct file *file, unsigned int cmd,
392 			       unsigned long arg)
393 {
394 	drm_buf_info32_t req32;
395 	drm_buf_info32_t __user *argp = (void __user *)arg;
396 	int err;
397 
398 	if (copy_from_user(&req32, argp, sizeof(req32)))
399 		return -EFAULT;
400 
401 	if (req32.count < 0)
402 		req32.count = 0;
403 
404 	err = drm_ioctl_kernel(file, drm_legacy_infobufs32, &req32, DRM_AUTH);
405 	if (err)
406 		return err;
407 
408 	if (put_user(req32.count, &argp->count))
409 		return -EFAULT;
410 
411 	return 0;
412 }
413 
414 typedef struct drm_buf_pub32 {
415 	int idx;		/**< Index into the master buffer list */
416 	int total;		/**< Buffer size */
417 	int used;		/**< Amount of buffer in use (for DMA) */
418 	u32 address;		/**< Address of buffer */
419 } drm_buf_pub32_t;
420 
421 typedef struct drm_buf_map32 {
422 	int count;		/**< Length of the buffer list */
423 	u32 virtual;		/**< Mmap'd area in user-virtual */
424 	u32 list;		/**< Buffer information */
425 } drm_buf_map32_t;
426 
427 static int map_one_buf32(void *data, int idx, unsigned long virtual,
428 			struct drm_buf *buf)
429 {
430 	drm_buf_map32_t *request = data;
431 	drm_buf_pub32_t __user *to = compat_ptr(request->list) + idx;
432 	drm_buf_pub32_t v;
433 
434 	v.idx = buf->idx;
435 	v.total = buf->total;
436 	v.used = 0;
437 	v.address = virtual + buf->offset;
438 	if (copy_to_user(to, &v, sizeof(v)))
439 		return -EFAULT;
440 	return 0;
441 }
442 
443 static int drm_legacy_mapbufs32(struct drm_device *dev, void *data,
444 		       struct drm_file *file_priv)
445 {
446 	drm_buf_map32_t *request = data;
447 	void __user *v;
448 	int err = __drm_legacy_mapbufs(dev, data, &request->count,
449 				    &v, map_one_buf32,
450 				    file_priv);
451 	request->virtual = ptr_to_compat(v);
452 	return err;
453 }
454 
455 static int compat_drm_mapbufs(struct file *file, unsigned int cmd,
456 			      unsigned long arg)
457 {
458 	drm_buf_map32_t __user *argp = (void __user *)arg;
459 	drm_buf_map32_t req32;
460 	int err;
461 
462 	if (copy_from_user(&req32, argp, sizeof(req32)))
463 		return -EFAULT;
464 	if (req32.count < 0)
465 		return -EINVAL;
466 
467 	err = drm_ioctl_kernel(file, drm_legacy_mapbufs32, &req32, DRM_AUTH);
468 	if (err)
469 		return err;
470 
471 	if (put_user(req32.count, &argp->count)
472 	    || put_user(req32.virtual, &argp->virtual))
473 		return -EFAULT;
474 
475 	return 0;
476 }
477 
478 typedef struct drm_buf_free32 {
479 	int count;
480 	u32 list;
481 } drm_buf_free32_t;
482 
483 static int compat_drm_freebufs(struct file *file, unsigned int cmd,
484 			       unsigned long arg)
485 {
486 	drm_buf_free32_t req32;
487 	struct drm_buf_free request;
488 	drm_buf_free32_t __user *argp = (void __user *)arg;
489 
490 	if (copy_from_user(&req32, argp, sizeof(req32)))
491 		return -EFAULT;
492 
493 	request.count = req32.count;
494 	request.list = compat_ptr(req32.list);
495 	return drm_ioctl_kernel(file, drm_legacy_freebufs, &request, DRM_AUTH);
496 }
497 
498 typedef struct drm_ctx_priv_map32 {
499 	unsigned int ctx_id;	 /**< Context requesting private mapping */
500 	u32 handle;		/**< Handle of map */
501 } drm_ctx_priv_map32_t;
502 
503 static int compat_drm_setsareactx(struct file *file, unsigned int cmd,
504 				  unsigned long arg)
505 {
506 	drm_ctx_priv_map32_t req32;
507 	struct drm_ctx_priv_map request;
508 	drm_ctx_priv_map32_t __user *argp = (void __user *)arg;
509 
510 	if (copy_from_user(&req32, argp, sizeof(req32)))
511 		return -EFAULT;
512 
513 	request.ctx_id = req32.ctx_id;
514 	request.handle = compat_ptr(req32.handle);
515 	return drm_ioctl_kernel(file, drm_legacy_setsareactx, &request,
516 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
517 }
518 
519 static int compat_drm_getsareactx(struct file *file, unsigned int cmd,
520 				  unsigned long arg)
521 {
522 	struct drm_ctx_priv_map req;
523 	drm_ctx_priv_map32_t req32;
524 	drm_ctx_priv_map32_t __user *argp = (void __user *)arg;
525 	int err;
526 
527 	if (copy_from_user(&req32, argp, sizeof(req32)))
528 		return -EFAULT;
529 
530 	req.ctx_id = req32.ctx_id;
531 	err = drm_ioctl_kernel(file, drm_legacy_getsareactx, &req, DRM_AUTH);
532 	if (err)
533 		return err;
534 
535 	req32.handle = ptr_to_compat((void __user *)req.handle);
536 	if (copy_to_user(argp, &req32, sizeof(req32)))
537 		return -EFAULT;
538 
539 	return 0;
540 }
541 
542 typedef struct drm_ctx_res32 {
543 	int count;
544 	u32 contexts;
545 } drm_ctx_res32_t;
546 
547 static int compat_drm_resctx(struct file *file, unsigned int cmd,
548 			     unsigned long arg)
549 {
550 	drm_ctx_res32_t __user *argp = (void __user *)arg;
551 	drm_ctx_res32_t res32;
552 	struct drm_ctx_res res;
553 	int err;
554 
555 	if (copy_from_user(&res32, argp, sizeof(res32)))
556 		return -EFAULT;
557 
558 	res.count = res32.count;
559 	res.contexts = compat_ptr(res32.contexts);
560 	err = drm_ioctl_kernel(file, drm_legacy_resctx, &res, DRM_AUTH);
561 	if (err)
562 		return err;
563 
564 	res32.count = res.count;
565 	if (copy_to_user(argp, &res32, sizeof(res32)))
566 		return -EFAULT;
567 
568 	return 0;
569 }
570 
571 typedef struct drm_dma32 {
572 	int context;		  /**< Context handle */
573 	int send_count;		  /**< Number of buffers to send */
574 	u32 send_indices;	  /**< List of handles to buffers */
575 	u32 send_sizes;		  /**< Lengths of data to send */
576 	enum drm_dma_flags flags;		  /**< Flags */
577 	int request_count;	  /**< Number of buffers requested */
578 	int request_size;	  /**< Desired size for buffers */
579 	u32 request_indices;	  /**< Buffer information */
580 	u32 request_sizes;
581 	int granted_count;	  /**< Number of buffers granted */
582 } drm_dma32_t;
583 
584 static int compat_drm_dma(struct file *file, unsigned int cmd,
585 			  unsigned long arg)
586 {
587 	drm_dma32_t d32;
588 	drm_dma32_t __user *argp = (void __user *)arg;
589 	struct drm_dma d;
590 	int err;
591 
592 	if (copy_from_user(&d32, argp, sizeof(d32)))
593 		return -EFAULT;
594 
595 	d.context = d32.context;
596 	d.send_count = d32.send_count;
597 	d.send_indices = compat_ptr(d32.send_indices);
598 	d.send_sizes = compat_ptr(d32.send_sizes);
599 	d.flags = d32.flags;
600 	d.request_count = d32.request_count;
601 	d.request_indices = compat_ptr(d32.request_indices);
602 	d.request_sizes = compat_ptr(d32.request_sizes);
603 	err = drm_ioctl_kernel(file, drm_legacy_dma_ioctl, &d, DRM_AUTH);
604 	if (err)
605 		return err;
606 
607 	if (put_user(d.request_size, &argp->request_size)
608 	    || put_user(d.granted_count, &argp->granted_count))
609 		return -EFAULT;
610 
611 	return 0;
612 }
613 #endif
614 
615 #if IS_ENABLED(CONFIG_AGP)
616 typedef struct drm_agp_mode32 {
617 	u32 mode;	/**< AGP mode */
618 } drm_agp_mode32_t;
619 
620 static int compat_drm_agp_enable(struct file *file, unsigned int cmd,
621 				 unsigned long arg)
622 {
623 	drm_agp_mode32_t __user *argp = (void __user *)arg;
624 	struct drm_agp_mode mode;
625 
626 	if (get_user(mode.mode, &argp->mode))
627 		return -EFAULT;
628 
629 	return drm_ioctl_kernel(file,  drm_agp_enable_ioctl, &mode,
630 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
631 }
632 
633 typedef struct drm_agp_info32 {
634 	int agp_version_major;
635 	int agp_version_minor;
636 	u32 mode;
637 	u32 aperture_base;	/* physical address */
638 	u32 aperture_size;	/* bytes */
639 	u32 memory_allowed;	/* bytes */
640 	u32 memory_used;
641 
642 	/* PCI information */
643 	unsigned short id_vendor;
644 	unsigned short id_device;
645 } drm_agp_info32_t;
646 
647 static int compat_drm_agp_info(struct file *file, unsigned int cmd,
648 			       unsigned long arg)
649 {
650 	drm_agp_info32_t __user *argp = (void __user *)arg;
651 	drm_agp_info32_t i32;
652 	struct drm_agp_info info;
653 	int err;
654 
655 	err = drm_ioctl_kernel(file, drm_agp_info_ioctl, &info, DRM_AUTH);
656 	if (err)
657 		return err;
658 
659 	i32.agp_version_major = info.agp_version_major;
660 	i32.agp_version_minor = info.agp_version_minor;
661 	i32.mode = info.mode;
662 	i32.aperture_base = info.aperture_base;
663 	i32.aperture_size = info.aperture_size;
664 	i32.memory_allowed = info.memory_allowed;
665 	i32.memory_used = info.memory_used;
666 	i32.id_vendor = info.id_vendor;
667 	i32.id_device = info.id_device;
668 	if (copy_to_user(argp, &i32, sizeof(i32)))
669 		return -EFAULT;
670 
671 	return 0;
672 }
673 
674 typedef struct drm_agp_buffer32 {
675 	u32 size;	/**< In bytes -- will round to page boundary */
676 	u32 handle;	/**< Used for binding / unbinding */
677 	u32 type;	/**< Type of memory to allocate */
678 	u32 physical;	/**< Physical used by i810 */
679 } drm_agp_buffer32_t;
680 
681 static int compat_drm_agp_alloc(struct file *file, unsigned int cmd,
682 				unsigned long arg)
683 {
684 	drm_agp_buffer32_t __user *argp = (void __user *)arg;
685 	drm_agp_buffer32_t req32;
686 	struct drm_agp_buffer request;
687 	int err;
688 
689 	if (copy_from_user(&req32, argp, sizeof(req32)))
690 		return -EFAULT;
691 
692 	request.size = req32.size;
693 	request.type = req32.type;
694 	err = drm_ioctl_kernel(file, drm_agp_alloc_ioctl, &request,
695 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
696 	if (err)
697 		return err;
698 
699 	req32.handle = request.handle;
700 	req32.physical = request.physical;
701 	if (copy_to_user(argp, &req32, sizeof(req32))) {
702 		drm_ioctl_kernel(file, drm_agp_free_ioctl, &request,
703 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
704 		return -EFAULT;
705 	}
706 
707 	return 0;
708 }
709 
710 static int compat_drm_agp_free(struct file *file, unsigned int cmd,
711 			       unsigned long arg)
712 {
713 	drm_agp_buffer32_t __user *argp = (void __user *)arg;
714 	struct drm_agp_buffer request;
715 
716 	if (get_user(request.handle, &argp->handle))
717 		return -EFAULT;
718 
719 	return drm_ioctl_kernel(file, drm_agp_free_ioctl, &request,
720 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
721 }
722 
723 typedef struct drm_agp_binding32 {
724 	u32 handle;	/**< From drm_agp_buffer */
725 	u32 offset;	/**< In bytes -- will round to page boundary */
726 } drm_agp_binding32_t;
727 
728 static int compat_drm_agp_bind(struct file *file, unsigned int cmd,
729 			       unsigned long arg)
730 {
731 	drm_agp_binding32_t __user *argp = (void __user *)arg;
732 	drm_agp_binding32_t req32;
733 	struct drm_agp_binding request;
734 
735 	if (copy_from_user(&req32, argp, sizeof(req32)))
736 		return -EFAULT;
737 
738 	request.handle = req32.handle;
739 	request.offset = req32.offset;
740 	return drm_ioctl_kernel(file, drm_agp_bind_ioctl, &request,
741 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
742 }
743 
744 static int compat_drm_agp_unbind(struct file *file, unsigned int cmd,
745 				 unsigned long arg)
746 {
747 	drm_agp_binding32_t __user *argp = (void __user *)arg;
748 	struct drm_agp_binding request;
749 
750 	if (get_user(request.handle, &argp->handle))
751 		return -EFAULT;
752 
753 	return drm_ioctl_kernel(file, drm_agp_unbind_ioctl, &request,
754 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
755 }
756 #endif /* CONFIG_AGP */
757 
758 #if IS_ENABLED(CONFIG_DRM_LEGACY)
759 typedef struct drm_scatter_gather32 {
760 	u32 size;	/**< In bytes -- will round to page boundary */
761 	u32 handle;	/**< Used for mapping / unmapping */
762 } drm_scatter_gather32_t;
763 
764 static int compat_drm_sg_alloc(struct file *file, unsigned int cmd,
765 			       unsigned long arg)
766 {
767 	drm_scatter_gather32_t __user *argp = (void __user *)arg;
768 	struct drm_scatter_gather request;
769 	int err;
770 
771 	if (get_user(request.size, &argp->size))
772 		return -EFAULT;
773 
774 	err = drm_ioctl_kernel(file, drm_legacy_sg_alloc, &request,
775 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
776 	if (err)
777 		return err;
778 
779 	/* XXX not sure about the handle conversion here... */
780 	if (put_user(request.handle >> PAGE_SHIFT, &argp->handle))
781 		return -EFAULT;
782 
783 	return 0;
784 }
785 
786 static int compat_drm_sg_free(struct file *file, unsigned int cmd,
787 			      unsigned long arg)
788 {
789 	drm_scatter_gather32_t __user *argp = (void __user *)arg;
790 	struct drm_scatter_gather request;
791 	unsigned long x;
792 
793 	if (get_user(x, &argp->handle))
794 		return -EFAULT;
795 	request.handle = x << PAGE_SHIFT;
796 	return drm_ioctl_kernel(file, drm_legacy_sg_free, &request,
797 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
798 }
799 #endif
800 #if defined(CONFIG_X86)
801 typedef struct drm_update_draw32 {
802 	drm_drawable_t handle;
803 	unsigned int type;
804 	unsigned int num;
805 	/* 64-bit version has a 32-bit pad here */
806 	u64 data;	/**< Pointer */
807 } __attribute__((packed)) drm_update_draw32_t;
808 
809 static int compat_drm_update_draw(struct file *file, unsigned int cmd,
810 				  unsigned long arg)
811 {
812 	drm_update_draw32_t update32;
813 	if (copy_from_user(&update32, (void __user *)arg, sizeof(update32)))
814 		return -EFAULT;
815 
816 	return drm_ioctl_kernel(file, drm_noop, NULL,
817 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
818 }
819 #endif
820 
821 struct drm_wait_vblank_request32 {
822 	enum drm_vblank_seq_type type;
823 	unsigned int sequence;
824 	u32 signal;
825 };
826 
827 struct drm_wait_vblank_reply32 {
828 	enum drm_vblank_seq_type type;
829 	unsigned int sequence;
830 	s32 tval_sec;
831 	s32 tval_usec;
832 };
833 
834 typedef union drm_wait_vblank32 {
835 	struct drm_wait_vblank_request32 request;
836 	struct drm_wait_vblank_reply32 reply;
837 } drm_wait_vblank32_t;
838 
839 static int compat_drm_wait_vblank(struct file *file, unsigned int cmd,
840 				  unsigned long arg)
841 {
842 	drm_wait_vblank32_t __user *argp = (void __user *)arg;
843 	drm_wait_vblank32_t req32;
844 	union drm_wait_vblank req;
845 	int err;
846 
847 	if (copy_from_user(&req32, argp, sizeof(req32)))
848 		return -EFAULT;
849 
850 	req.request.type = req32.request.type;
851 	req.request.sequence = req32.request.sequence;
852 	req.request.signal = req32.request.signal;
853 	err = drm_ioctl_kernel(file, drm_wait_vblank_ioctl, &req, DRM_UNLOCKED);
854 	if (err)
855 		return err;
856 
857 	req32.reply.type = req.reply.type;
858 	req32.reply.sequence = req.reply.sequence;
859 	req32.reply.tval_sec = req.reply.tval_sec;
860 	req32.reply.tval_usec = req.reply.tval_usec;
861 	if (copy_to_user(argp, &req32, sizeof(req32)))
862 		return -EFAULT;
863 
864 	return 0;
865 }
866 
867 #if defined(CONFIG_X86)
868 typedef struct drm_mode_fb_cmd232 {
869 	u32 fb_id;
870 	u32 width;
871 	u32 height;
872 	u32 pixel_format;
873 	u32 flags;
874 	u32 handles[4];
875 	u32 pitches[4];
876 	u32 offsets[4];
877 	u64 modifier[4];
878 } __attribute__((packed)) drm_mode_fb_cmd232_t;
879 
880 static int compat_drm_mode_addfb2(struct file *file, unsigned int cmd,
881 				  unsigned long arg)
882 {
883 	struct drm_mode_fb_cmd232 __user *argp = (void __user *)arg;
884 	struct drm_mode_fb_cmd2 req64;
885 	int err;
886 
887 	if (copy_from_user(&req64, argp,
888 			   offsetof(drm_mode_fb_cmd232_t, modifier)))
889 		return -EFAULT;
890 
891 	if (copy_from_user(&req64.modifier, &argp->modifier,
892 			   sizeof(req64.modifier)))
893 		return -EFAULT;
894 
895 	err = drm_ioctl_kernel(file, drm_mode_addfb2, &req64,
896 			       DRM_UNLOCKED);
897 	if (err)
898 		return err;
899 
900 	if (put_user(req64.fb_id, &argp->fb_id))
901 		return -EFAULT;
902 
903 	return 0;
904 }
905 #endif
906 
907 static struct {
908 	drm_ioctl_compat_t *fn;
909 	char *name;
910 } drm_compat_ioctls[] = {
911 #define DRM_IOCTL32_DEF(n, f) [DRM_IOCTL_NR(n##32)] = {.fn = f, .name = #n}
912 	DRM_IOCTL32_DEF(DRM_IOCTL_VERSION, compat_drm_version),
913 	DRM_IOCTL32_DEF(DRM_IOCTL_GET_UNIQUE, compat_drm_getunique),
914 #if IS_ENABLED(CONFIG_DRM_LEGACY)
915 	DRM_IOCTL32_DEF(DRM_IOCTL_GET_MAP, compat_drm_getmap),
916 #endif
917 	DRM_IOCTL32_DEF(DRM_IOCTL_GET_CLIENT, compat_drm_getclient),
918 	DRM_IOCTL32_DEF(DRM_IOCTL_GET_STATS, compat_drm_getstats),
919 	DRM_IOCTL32_DEF(DRM_IOCTL_SET_UNIQUE, compat_drm_setunique),
920 #if IS_ENABLED(CONFIG_DRM_LEGACY)
921 	DRM_IOCTL32_DEF(DRM_IOCTL_ADD_MAP, compat_drm_addmap),
922 	DRM_IOCTL32_DEF(DRM_IOCTL_ADD_BUFS, compat_drm_addbufs),
923 	DRM_IOCTL32_DEF(DRM_IOCTL_MARK_BUFS, compat_drm_markbufs),
924 	DRM_IOCTL32_DEF(DRM_IOCTL_INFO_BUFS, compat_drm_infobufs),
925 	DRM_IOCTL32_DEF(DRM_IOCTL_MAP_BUFS, compat_drm_mapbufs),
926 	DRM_IOCTL32_DEF(DRM_IOCTL_FREE_BUFS, compat_drm_freebufs),
927 	DRM_IOCTL32_DEF(DRM_IOCTL_RM_MAP, compat_drm_rmmap),
928 	DRM_IOCTL32_DEF(DRM_IOCTL_SET_SAREA_CTX, compat_drm_setsareactx),
929 	DRM_IOCTL32_DEF(DRM_IOCTL_GET_SAREA_CTX, compat_drm_getsareactx),
930 	DRM_IOCTL32_DEF(DRM_IOCTL_RES_CTX, compat_drm_resctx),
931 	DRM_IOCTL32_DEF(DRM_IOCTL_DMA, compat_drm_dma),
932 #endif
933 #if IS_ENABLED(CONFIG_AGP)
934 	DRM_IOCTL32_DEF(DRM_IOCTL_AGP_ENABLE, compat_drm_agp_enable),
935 	DRM_IOCTL32_DEF(DRM_IOCTL_AGP_INFO, compat_drm_agp_info),
936 	DRM_IOCTL32_DEF(DRM_IOCTL_AGP_ALLOC, compat_drm_agp_alloc),
937 	DRM_IOCTL32_DEF(DRM_IOCTL_AGP_FREE, compat_drm_agp_free),
938 	DRM_IOCTL32_DEF(DRM_IOCTL_AGP_BIND, compat_drm_agp_bind),
939 	DRM_IOCTL32_DEF(DRM_IOCTL_AGP_UNBIND, compat_drm_agp_unbind),
940 #endif
941 #if IS_ENABLED(CONFIG_DRM_LEGACY)
942 	DRM_IOCTL32_DEF(DRM_IOCTL_SG_ALLOC, compat_drm_sg_alloc),
943 	DRM_IOCTL32_DEF(DRM_IOCTL_SG_FREE, compat_drm_sg_free),
944 #endif
945 #if defined(CONFIG_X86) || defined(CONFIG_IA64)
946 	DRM_IOCTL32_DEF(DRM_IOCTL_UPDATE_DRAW, compat_drm_update_draw),
947 #endif
948 	DRM_IOCTL32_DEF(DRM_IOCTL_WAIT_VBLANK, compat_drm_wait_vblank),
949 #if defined(CONFIG_X86) || defined(CONFIG_IA64)
950 	DRM_IOCTL32_DEF(DRM_IOCTL_MODE_ADDFB2, compat_drm_mode_addfb2),
951 #endif
952 };
953 
954 /**
955  * drm_compat_ioctl - 32bit IOCTL compatibility handler for DRM drivers
956  * @filp: file this ioctl is called on
957  * @cmd: ioctl cmd number
958  * @arg: user argument
959  *
960  * Compatibility handler for 32 bit userspace running on 64 kernels. All actual
961  * IOCTL handling is forwarded to drm_ioctl(), while marshalling structures as
962  * appropriate. Note that this only handles DRM core IOCTLs, if the driver has
963  * botched IOCTL itself, it must handle those by wrapping this function.
964  *
965  * Returns:
966  * Zero on success, negative error code on failure.
967  */
968 long drm_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
969 {
970 	unsigned int nr = DRM_IOCTL_NR(cmd);
971 	struct drm_file *file_priv = filp->private_data;
972 	drm_ioctl_compat_t *fn;
973 	int ret;
974 
975 	/* Assume that ioctls without an explicit compat routine will just
976 	 * work.  This may not always be a good assumption, but it's better
977 	 * than always failing.
978 	 */
979 	if (nr >= ARRAY_SIZE(drm_compat_ioctls))
980 		return drm_ioctl(filp, cmd, arg);
981 
982 	fn = drm_compat_ioctls[nr].fn;
983 	if (!fn)
984 		return drm_ioctl(filp, cmd, arg);
985 
986 	DRM_DEBUG("pid=%d, dev=0x%lx, auth=%d, %s\n",
987 		  task_pid_nr(current),
988 		  (long)old_encode_dev(file_priv->minor->kdev->devt),
989 		  file_priv->authenticated,
990 		  drm_compat_ioctls[nr].name);
991 	ret = (*fn)(filp, cmd, arg);
992 	if (ret)
993 		DRM_DEBUG("ret = %d\n", ret);
994 	return ret;
995 }
996 EXPORT_SYMBOL(drm_compat_ioctl);
997