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