xref: /openbmc/linux/drivers/gpu/drm/drm_ioc32.c (revision ffe66bbe)
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 	int err;
306 
307 	err = drm_ioctl_kernel(file, drm_noop, NULL, 0);
308 	if (err)
309 		return err;
310 
311 	if (clear_user(argp, sizeof(drm_stats32_t)))
312 		return -EFAULT;
313 	return 0;
314 }
315 
316 #if IS_ENABLED(CONFIG_DRM_LEGACY)
317 typedef struct drm_buf_desc32 {
318 	int count;		 /* Number of buffers of this size */
319 	int size;		 /* Size in bytes */
320 	int low_mark;		 /* Low water mark */
321 	int high_mark;		 /* High water mark */
322 	int flags;
323 	u32 agp_start;		 /* Start address in the AGP aperture */
324 } drm_buf_desc32_t;
325 
326 static int compat_drm_addbufs(struct file *file, unsigned int cmd,
327 			      unsigned long arg)
328 {
329 	drm_buf_desc32_t __user *argp = (void __user *)arg;
330 	drm_buf_desc32_t desc32;
331 	struct drm_buf_desc desc;
332 	int err;
333 
334 	if (copy_from_user(&desc32, argp, sizeof(drm_buf_desc32_t)))
335 		return -EFAULT;
336 
337 	desc = (struct drm_buf_desc){
338 		desc32.count, desc32.size, desc32.low_mark, desc32.high_mark,
339 		desc32.flags, desc32.agp_start
340 	};
341 
342 	err = drm_ioctl_kernel(file, drm_legacy_addbufs, &desc,
343 				   DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
344 	if (err)
345 		return err;
346 
347 	desc32 = (drm_buf_desc32_t){
348 		desc.count, desc.size, desc.low_mark, desc.high_mark,
349 		desc.flags, desc.agp_start
350 	};
351 	if (copy_to_user(argp, &desc32, sizeof(drm_buf_desc32_t)))
352 		return -EFAULT;
353 
354 	return 0;
355 }
356 
357 static int compat_drm_markbufs(struct file *file, unsigned int cmd,
358 			       unsigned long arg)
359 {
360 	drm_buf_desc32_t b32;
361 	drm_buf_desc32_t __user *argp = (void __user *)arg;
362 	struct drm_buf_desc buf;
363 
364 	if (copy_from_user(&b32, argp, sizeof(b32)))
365 		return -EFAULT;
366 
367 	buf.size = b32.size;
368 	buf.low_mark = b32.low_mark;
369 	buf.high_mark = b32.high_mark;
370 
371 	return drm_ioctl_kernel(file, drm_legacy_markbufs, &buf,
372 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
373 }
374 
375 typedef struct drm_buf_info32 {
376 	int count;		/**< Entries in list */
377 	u32 list;
378 } drm_buf_info32_t;
379 
380 static int copy_one_buf32(void *data, int count, struct drm_buf_entry *from)
381 {
382 	drm_buf_info32_t *request = data;
383 	drm_buf_desc32_t __user *to = compat_ptr(request->list);
384 	drm_buf_desc32_t v = {.count = from->buf_count,
385 			      .size = from->buf_size,
386 			      .low_mark = from->low_mark,
387 			      .high_mark = from->high_mark};
388 
389 	if (copy_to_user(to + count, &v, offsetof(drm_buf_desc32_t, flags)))
390 		return -EFAULT;
391 	return 0;
392 }
393 
394 static int drm_legacy_infobufs32(struct drm_device *dev, void *data,
395 			struct drm_file *file_priv)
396 {
397 	drm_buf_info32_t *request = data;
398 
399 	return __drm_legacy_infobufs(dev, data, &request->count, copy_one_buf32);
400 }
401 
402 static int compat_drm_infobufs(struct file *file, unsigned int cmd,
403 			       unsigned long arg)
404 {
405 	drm_buf_info32_t req32;
406 	drm_buf_info32_t __user *argp = (void __user *)arg;
407 	int err;
408 
409 	if (copy_from_user(&req32, argp, sizeof(req32)))
410 		return -EFAULT;
411 
412 	if (req32.count < 0)
413 		req32.count = 0;
414 
415 	err = drm_ioctl_kernel(file, drm_legacy_infobufs32, &req32, DRM_AUTH);
416 	if (err)
417 		return err;
418 
419 	if (put_user(req32.count, &argp->count))
420 		return -EFAULT;
421 
422 	return 0;
423 }
424 
425 typedef struct drm_buf_pub32 {
426 	int idx;		/**< Index into the master buffer list */
427 	int total;		/**< Buffer size */
428 	int used;		/**< Amount of buffer in use (for DMA) */
429 	u32 address;		/**< Address of buffer */
430 } drm_buf_pub32_t;
431 
432 typedef struct drm_buf_map32 {
433 	int count;		/**< Length of the buffer list */
434 	u32 virtual;		/**< Mmap'd area in user-virtual */
435 	u32 list;		/**< Buffer information */
436 } drm_buf_map32_t;
437 
438 static int map_one_buf32(void *data, int idx, unsigned long virtual,
439 			struct drm_buf *buf)
440 {
441 	drm_buf_map32_t *request = data;
442 	drm_buf_pub32_t __user *to = compat_ptr(request->list) + idx;
443 	drm_buf_pub32_t v;
444 
445 	v.idx = buf->idx;
446 	v.total = buf->total;
447 	v.used = 0;
448 	v.address = virtual + buf->offset;
449 	if (copy_to_user(to, &v, sizeof(v)))
450 		return -EFAULT;
451 	return 0;
452 }
453 
454 static int drm_legacy_mapbufs32(struct drm_device *dev, void *data,
455 		       struct drm_file *file_priv)
456 {
457 	drm_buf_map32_t *request = data;
458 	void __user *v;
459 	int err = __drm_legacy_mapbufs(dev, data, &request->count,
460 				    &v, map_one_buf32,
461 				    file_priv);
462 	request->virtual = ptr_to_compat(v);
463 	return err;
464 }
465 
466 static int compat_drm_mapbufs(struct file *file, unsigned int cmd,
467 			      unsigned long arg)
468 {
469 	drm_buf_map32_t __user *argp = (void __user *)arg;
470 	drm_buf_map32_t req32;
471 	int err;
472 
473 	if (copy_from_user(&req32, argp, sizeof(req32)))
474 		return -EFAULT;
475 	if (req32.count < 0)
476 		return -EINVAL;
477 
478 	err = drm_ioctl_kernel(file, drm_legacy_mapbufs32, &req32, DRM_AUTH);
479 	if (err)
480 		return err;
481 
482 	if (put_user(req32.count, &argp->count)
483 	    || put_user(req32.virtual, &argp->virtual))
484 		return -EFAULT;
485 
486 	return 0;
487 }
488 
489 typedef struct drm_buf_free32 {
490 	int count;
491 	u32 list;
492 } drm_buf_free32_t;
493 
494 static int compat_drm_freebufs(struct file *file, unsigned int cmd,
495 			       unsigned long arg)
496 {
497 	drm_buf_free32_t req32;
498 	struct drm_buf_free request;
499 	drm_buf_free32_t __user *argp = (void __user *)arg;
500 
501 	if (copy_from_user(&req32, argp, sizeof(req32)))
502 		return -EFAULT;
503 
504 	request.count = req32.count;
505 	request.list = compat_ptr(req32.list);
506 	return drm_ioctl_kernel(file, drm_legacy_freebufs, &request, DRM_AUTH);
507 }
508 
509 typedef struct drm_ctx_priv_map32 {
510 	unsigned int ctx_id;	 /**< Context requesting private mapping */
511 	u32 handle;		/**< Handle of map */
512 } drm_ctx_priv_map32_t;
513 
514 static int compat_drm_setsareactx(struct file *file, unsigned int cmd,
515 				  unsigned long arg)
516 {
517 	drm_ctx_priv_map32_t req32;
518 	struct drm_ctx_priv_map request;
519 	drm_ctx_priv_map32_t __user *argp = (void __user *)arg;
520 
521 	if (copy_from_user(&req32, argp, sizeof(req32)))
522 		return -EFAULT;
523 
524 	request.ctx_id = req32.ctx_id;
525 	request.handle = compat_ptr(req32.handle);
526 	return drm_ioctl_kernel(file, drm_legacy_setsareactx, &request,
527 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
528 }
529 
530 static int compat_drm_getsareactx(struct file *file, unsigned int cmd,
531 				  unsigned long arg)
532 {
533 	struct drm_ctx_priv_map req;
534 	drm_ctx_priv_map32_t req32;
535 	drm_ctx_priv_map32_t __user *argp = (void __user *)arg;
536 	int err;
537 
538 	if (copy_from_user(&req32, argp, sizeof(req32)))
539 		return -EFAULT;
540 
541 	req.ctx_id = req32.ctx_id;
542 	err = drm_ioctl_kernel(file, drm_legacy_getsareactx, &req, DRM_AUTH);
543 	if (err)
544 		return err;
545 
546 	req32.handle = ptr_to_compat((void __user *)req.handle);
547 	if (copy_to_user(argp, &req32, sizeof(req32)))
548 		return -EFAULT;
549 
550 	return 0;
551 }
552 
553 typedef struct drm_ctx_res32 {
554 	int count;
555 	u32 contexts;
556 } drm_ctx_res32_t;
557 
558 static int compat_drm_resctx(struct file *file, unsigned int cmd,
559 			     unsigned long arg)
560 {
561 	drm_ctx_res32_t __user *argp = (void __user *)arg;
562 	drm_ctx_res32_t res32;
563 	struct drm_ctx_res res;
564 	int err;
565 
566 	if (copy_from_user(&res32, argp, sizeof(res32)))
567 		return -EFAULT;
568 
569 	res.count = res32.count;
570 	res.contexts = compat_ptr(res32.contexts);
571 	err = drm_ioctl_kernel(file, drm_legacy_resctx, &res, DRM_AUTH);
572 	if (err)
573 		return err;
574 
575 	res32.count = res.count;
576 	if (copy_to_user(argp, &res32, sizeof(res32)))
577 		return -EFAULT;
578 
579 	return 0;
580 }
581 
582 typedef struct drm_dma32 {
583 	int context;		  /**< Context handle */
584 	int send_count;		  /**< Number of buffers to send */
585 	u32 send_indices;	  /**< List of handles to buffers */
586 	u32 send_sizes;		  /**< Lengths of data to send */
587 	enum drm_dma_flags flags;		  /**< Flags */
588 	int request_count;	  /**< Number of buffers requested */
589 	int request_size;	  /**< Desired size for buffers */
590 	u32 request_indices;	  /**< Buffer information */
591 	u32 request_sizes;
592 	int granted_count;	  /**< Number of buffers granted */
593 } drm_dma32_t;
594 
595 static int compat_drm_dma(struct file *file, unsigned int cmd,
596 			  unsigned long arg)
597 {
598 	drm_dma32_t d32;
599 	drm_dma32_t __user *argp = (void __user *)arg;
600 	struct drm_dma d;
601 	int err;
602 
603 	if (copy_from_user(&d32, argp, sizeof(d32)))
604 		return -EFAULT;
605 
606 	d.context = d32.context;
607 	d.send_count = d32.send_count;
608 	d.send_indices = compat_ptr(d32.send_indices);
609 	d.send_sizes = compat_ptr(d32.send_sizes);
610 	d.flags = d32.flags;
611 	d.request_count = d32.request_count;
612 	d.request_indices = compat_ptr(d32.request_indices);
613 	d.request_sizes = compat_ptr(d32.request_sizes);
614 	err = drm_ioctl_kernel(file, drm_legacy_dma_ioctl, &d, DRM_AUTH);
615 	if (err)
616 		return err;
617 
618 	if (put_user(d.request_size, &argp->request_size)
619 	    || put_user(d.granted_count, &argp->granted_count))
620 		return -EFAULT;
621 
622 	return 0;
623 }
624 #endif
625 
626 #if IS_ENABLED(CONFIG_AGP)
627 typedef struct drm_agp_mode32 {
628 	u32 mode;	/**< AGP mode */
629 } drm_agp_mode32_t;
630 
631 static int compat_drm_agp_enable(struct file *file, unsigned int cmd,
632 				 unsigned long arg)
633 {
634 	drm_agp_mode32_t __user *argp = (void __user *)arg;
635 	struct drm_agp_mode mode;
636 
637 	if (get_user(mode.mode, &argp->mode))
638 		return -EFAULT;
639 
640 	return drm_ioctl_kernel(file,  drm_agp_enable_ioctl, &mode,
641 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
642 }
643 
644 typedef struct drm_agp_info32 {
645 	int agp_version_major;
646 	int agp_version_minor;
647 	u32 mode;
648 	u32 aperture_base;	/* physical address */
649 	u32 aperture_size;	/* bytes */
650 	u32 memory_allowed;	/* bytes */
651 	u32 memory_used;
652 
653 	/* PCI information */
654 	unsigned short id_vendor;
655 	unsigned short id_device;
656 } drm_agp_info32_t;
657 
658 static int compat_drm_agp_info(struct file *file, unsigned int cmd,
659 			       unsigned long arg)
660 {
661 	drm_agp_info32_t __user *argp = (void __user *)arg;
662 	drm_agp_info32_t i32;
663 	struct drm_agp_info info;
664 	int err;
665 
666 	err = drm_ioctl_kernel(file, drm_agp_info_ioctl, &info, DRM_AUTH);
667 	if (err)
668 		return err;
669 
670 	i32.agp_version_major = info.agp_version_major;
671 	i32.agp_version_minor = info.agp_version_minor;
672 	i32.mode = info.mode;
673 	i32.aperture_base = info.aperture_base;
674 	i32.aperture_size = info.aperture_size;
675 	i32.memory_allowed = info.memory_allowed;
676 	i32.memory_used = info.memory_used;
677 	i32.id_vendor = info.id_vendor;
678 	i32.id_device = info.id_device;
679 	if (copy_to_user(argp, &i32, sizeof(i32)))
680 		return -EFAULT;
681 
682 	return 0;
683 }
684 
685 typedef struct drm_agp_buffer32 {
686 	u32 size;	/**< In bytes -- will round to page boundary */
687 	u32 handle;	/**< Used for binding / unbinding */
688 	u32 type;	/**< Type of memory to allocate */
689 	u32 physical;	/**< Physical used by i810 */
690 } drm_agp_buffer32_t;
691 
692 static int compat_drm_agp_alloc(struct file *file, unsigned int cmd,
693 				unsigned long arg)
694 {
695 	drm_agp_buffer32_t __user *argp = (void __user *)arg;
696 	drm_agp_buffer32_t req32;
697 	struct drm_agp_buffer request;
698 	int err;
699 
700 	if (copy_from_user(&req32, argp, sizeof(req32)))
701 		return -EFAULT;
702 
703 	request.size = req32.size;
704 	request.type = req32.type;
705 	err = drm_ioctl_kernel(file, drm_agp_alloc_ioctl, &request,
706 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
707 	if (err)
708 		return err;
709 
710 	req32.handle = request.handle;
711 	req32.physical = request.physical;
712 	if (copy_to_user(argp, &req32, sizeof(req32))) {
713 		drm_ioctl_kernel(file, drm_agp_free_ioctl, &request,
714 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
715 		return -EFAULT;
716 	}
717 
718 	return 0;
719 }
720 
721 static int compat_drm_agp_free(struct file *file, unsigned int cmd,
722 			       unsigned long arg)
723 {
724 	drm_agp_buffer32_t __user *argp = (void __user *)arg;
725 	struct drm_agp_buffer request;
726 
727 	if (get_user(request.handle, &argp->handle))
728 		return -EFAULT;
729 
730 	return drm_ioctl_kernel(file, drm_agp_free_ioctl, &request,
731 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
732 }
733 
734 typedef struct drm_agp_binding32 {
735 	u32 handle;	/**< From drm_agp_buffer */
736 	u32 offset;	/**< In bytes -- will round to page boundary */
737 } drm_agp_binding32_t;
738 
739 static int compat_drm_agp_bind(struct file *file, unsigned int cmd,
740 			       unsigned long arg)
741 {
742 	drm_agp_binding32_t __user *argp = (void __user *)arg;
743 	drm_agp_binding32_t req32;
744 	struct drm_agp_binding request;
745 
746 	if (copy_from_user(&req32, argp, sizeof(req32)))
747 		return -EFAULT;
748 
749 	request.handle = req32.handle;
750 	request.offset = req32.offset;
751 	return drm_ioctl_kernel(file, drm_agp_bind_ioctl, &request,
752 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
753 }
754 
755 static int compat_drm_agp_unbind(struct file *file, unsigned int cmd,
756 				 unsigned long arg)
757 {
758 	drm_agp_binding32_t __user *argp = (void __user *)arg;
759 	struct drm_agp_binding request;
760 
761 	if (get_user(request.handle, &argp->handle))
762 		return -EFAULT;
763 
764 	return drm_ioctl_kernel(file, drm_agp_unbind_ioctl, &request,
765 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
766 }
767 #endif /* CONFIG_AGP */
768 
769 #if IS_ENABLED(CONFIG_DRM_LEGACY)
770 typedef struct drm_scatter_gather32 {
771 	u32 size;	/**< In bytes -- will round to page boundary */
772 	u32 handle;	/**< Used for mapping / unmapping */
773 } drm_scatter_gather32_t;
774 
775 static int compat_drm_sg_alloc(struct file *file, unsigned int cmd,
776 			       unsigned long arg)
777 {
778 	drm_scatter_gather32_t __user *argp = (void __user *)arg;
779 	struct drm_scatter_gather request;
780 	int err;
781 
782 	if (get_user(request.size, &argp->size))
783 		return -EFAULT;
784 
785 	err = drm_ioctl_kernel(file, drm_legacy_sg_alloc, &request,
786 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
787 	if (err)
788 		return err;
789 
790 	/* XXX not sure about the handle conversion here... */
791 	if (put_user(request.handle >> PAGE_SHIFT, &argp->handle))
792 		return -EFAULT;
793 
794 	return 0;
795 }
796 
797 static int compat_drm_sg_free(struct file *file, unsigned int cmd,
798 			      unsigned long arg)
799 {
800 	drm_scatter_gather32_t __user *argp = (void __user *)arg;
801 	struct drm_scatter_gather request;
802 	unsigned long x;
803 
804 	if (get_user(x, &argp->handle))
805 		return -EFAULT;
806 	request.handle = x << PAGE_SHIFT;
807 	return drm_ioctl_kernel(file, drm_legacy_sg_free, &request,
808 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
809 }
810 #endif
811 #if defined(CONFIG_X86)
812 typedef struct drm_update_draw32 {
813 	drm_drawable_t handle;
814 	unsigned int type;
815 	unsigned int num;
816 	/* 64-bit version has a 32-bit pad here */
817 	u64 data;	/**< Pointer */
818 } __attribute__((packed)) drm_update_draw32_t;
819 
820 static int compat_drm_update_draw(struct file *file, unsigned int cmd,
821 				  unsigned long arg)
822 {
823 	drm_update_draw32_t update32;
824 
825 	if (copy_from_user(&update32, (void __user *)arg, sizeof(update32)))
826 		return -EFAULT;
827 
828 	return drm_ioctl_kernel(file, drm_noop, NULL,
829 				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
830 }
831 #endif
832 
833 struct drm_wait_vblank_request32 {
834 	enum drm_vblank_seq_type type;
835 	unsigned int sequence;
836 	u32 signal;
837 };
838 
839 struct drm_wait_vblank_reply32 {
840 	enum drm_vblank_seq_type type;
841 	unsigned int sequence;
842 	s32 tval_sec;
843 	s32 tval_usec;
844 };
845 
846 typedef union drm_wait_vblank32 {
847 	struct drm_wait_vblank_request32 request;
848 	struct drm_wait_vblank_reply32 reply;
849 } drm_wait_vblank32_t;
850 
851 static int compat_drm_wait_vblank(struct file *file, unsigned int cmd,
852 				  unsigned long arg)
853 {
854 	drm_wait_vblank32_t __user *argp = (void __user *)arg;
855 	drm_wait_vblank32_t req32;
856 	union drm_wait_vblank req;
857 	int err;
858 
859 	if (copy_from_user(&req32, argp, sizeof(req32)))
860 		return -EFAULT;
861 
862 	memset(&req, 0, sizeof(req));
863 
864 	req.request.type = req32.request.type;
865 	req.request.sequence = req32.request.sequence;
866 	req.request.signal = req32.request.signal;
867 	err = drm_ioctl_kernel(file, drm_wait_vblank_ioctl, &req, DRM_UNLOCKED);
868 	if (err)
869 		return err;
870 
871 	req32.reply.type = req.reply.type;
872 	req32.reply.sequence = req.reply.sequence;
873 	req32.reply.tval_sec = req.reply.tval_sec;
874 	req32.reply.tval_usec = req.reply.tval_usec;
875 	if (copy_to_user(argp, &req32, sizeof(req32)))
876 		return -EFAULT;
877 
878 	return 0;
879 }
880 
881 #if defined(CONFIG_X86)
882 typedef struct drm_mode_fb_cmd232 {
883 	u32 fb_id;
884 	u32 width;
885 	u32 height;
886 	u32 pixel_format;
887 	u32 flags;
888 	u32 handles[4];
889 	u32 pitches[4];
890 	u32 offsets[4];
891 	u64 modifier[4];
892 } __attribute__((packed)) drm_mode_fb_cmd232_t;
893 
894 static int compat_drm_mode_addfb2(struct file *file, unsigned int cmd,
895 				  unsigned long arg)
896 {
897 	struct drm_mode_fb_cmd232 __user *argp = (void __user *)arg;
898 	struct drm_mode_fb_cmd2 req64;
899 	int err;
900 
901 	memset(&req64, 0, sizeof(req64));
902 
903 	if (copy_from_user(&req64, argp,
904 			   offsetof(drm_mode_fb_cmd232_t, modifier)))
905 		return -EFAULT;
906 
907 	if (copy_from_user(&req64.modifier, &argp->modifier,
908 			   sizeof(req64.modifier)))
909 		return -EFAULT;
910 
911 	err = drm_ioctl_kernel(file, drm_mode_addfb2, &req64, 0);
912 	if (err)
913 		return err;
914 
915 	if (put_user(req64.fb_id, &argp->fb_id))
916 		return -EFAULT;
917 
918 	return 0;
919 }
920 #endif
921 
922 static struct {
923 	drm_ioctl_compat_t *fn;
924 	char *name;
925 } drm_compat_ioctls[] = {
926 #define DRM_IOCTL32_DEF(n, f) [DRM_IOCTL_NR(n##32)] = {.fn = f, .name = #n}
927 	DRM_IOCTL32_DEF(DRM_IOCTL_VERSION, compat_drm_version),
928 	DRM_IOCTL32_DEF(DRM_IOCTL_GET_UNIQUE, compat_drm_getunique),
929 #if IS_ENABLED(CONFIG_DRM_LEGACY)
930 	DRM_IOCTL32_DEF(DRM_IOCTL_GET_MAP, compat_drm_getmap),
931 #endif
932 	DRM_IOCTL32_DEF(DRM_IOCTL_GET_CLIENT, compat_drm_getclient),
933 	DRM_IOCTL32_DEF(DRM_IOCTL_GET_STATS, compat_drm_getstats),
934 	DRM_IOCTL32_DEF(DRM_IOCTL_SET_UNIQUE, compat_drm_setunique),
935 #if IS_ENABLED(CONFIG_DRM_LEGACY)
936 	DRM_IOCTL32_DEF(DRM_IOCTL_ADD_MAP, compat_drm_addmap),
937 	DRM_IOCTL32_DEF(DRM_IOCTL_ADD_BUFS, compat_drm_addbufs),
938 	DRM_IOCTL32_DEF(DRM_IOCTL_MARK_BUFS, compat_drm_markbufs),
939 	DRM_IOCTL32_DEF(DRM_IOCTL_INFO_BUFS, compat_drm_infobufs),
940 	DRM_IOCTL32_DEF(DRM_IOCTL_MAP_BUFS, compat_drm_mapbufs),
941 	DRM_IOCTL32_DEF(DRM_IOCTL_FREE_BUFS, compat_drm_freebufs),
942 	DRM_IOCTL32_DEF(DRM_IOCTL_RM_MAP, compat_drm_rmmap),
943 	DRM_IOCTL32_DEF(DRM_IOCTL_SET_SAREA_CTX, compat_drm_setsareactx),
944 	DRM_IOCTL32_DEF(DRM_IOCTL_GET_SAREA_CTX, compat_drm_getsareactx),
945 	DRM_IOCTL32_DEF(DRM_IOCTL_RES_CTX, compat_drm_resctx),
946 	DRM_IOCTL32_DEF(DRM_IOCTL_DMA, compat_drm_dma),
947 #endif
948 #if IS_ENABLED(CONFIG_AGP)
949 	DRM_IOCTL32_DEF(DRM_IOCTL_AGP_ENABLE, compat_drm_agp_enable),
950 	DRM_IOCTL32_DEF(DRM_IOCTL_AGP_INFO, compat_drm_agp_info),
951 	DRM_IOCTL32_DEF(DRM_IOCTL_AGP_ALLOC, compat_drm_agp_alloc),
952 	DRM_IOCTL32_DEF(DRM_IOCTL_AGP_FREE, compat_drm_agp_free),
953 	DRM_IOCTL32_DEF(DRM_IOCTL_AGP_BIND, compat_drm_agp_bind),
954 	DRM_IOCTL32_DEF(DRM_IOCTL_AGP_UNBIND, compat_drm_agp_unbind),
955 #endif
956 #if IS_ENABLED(CONFIG_DRM_LEGACY)
957 	DRM_IOCTL32_DEF(DRM_IOCTL_SG_ALLOC, compat_drm_sg_alloc),
958 	DRM_IOCTL32_DEF(DRM_IOCTL_SG_FREE, compat_drm_sg_free),
959 #endif
960 #if defined(CONFIG_X86) || defined(CONFIG_IA64)
961 	DRM_IOCTL32_DEF(DRM_IOCTL_UPDATE_DRAW, compat_drm_update_draw),
962 #endif
963 	DRM_IOCTL32_DEF(DRM_IOCTL_WAIT_VBLANK, compat_drm_wait_vblank),
964 #if defined(CONFIG_X86) || defined(CONFIG_IA64)
965 	DRM_IOCTL32_DEF(DRM_IOCTL_MODE_ADDFB2, compat_drm_mode_addfb2),
966 #endif
967 };
968 
969 /**
970  * drm_compat_ioctl - 32bit IOCTL compatibility handler for DRM drivers
971  * @filp: file this ioctl is called on
972  * @cmd: ioctl cmd number
973  * @arg: user argument
974  *
975  * Compatibility handler for 32 bit userspace running on 64 kernels. All actual
976  * IOCTL handling is forwarded to drm_ioctl(), while marshalling structures as
977  * appropriate. Note that this only handles DRM core IOCTLs, if the driver has
978  * botched IOCTL itself, it must handle those by wrapping this function.
979  *
980  * Returns:
981  * Zero on success, negative error code on failure.
982  */
983 long drm_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
984 {
985 	unsigned int nr = DRM_IOCTL_NR(cmd);
986 	struct drm_file *file_priv = filp->private_data;
987 	drm_ioctl_compat_t *fn;
988 	int ret;
989 
990 	/* Assume that ioctls without an explicit compat routine will just
991 	 * work.  This may not always be a good assumption, but it's better
992 	 * than always failing.
993 	 */
994 	if (nr >= ARRAY_SIZE(drm_compat_ioctls))
995 		return drm_ioctl(filp, cmd, arg);
996 
997 	fn = drm_compat_ioctls[nr].fn;
998 	if (!fn)
999 		return drm_ioctl(filp, cmd, arg);
1000 
1001 	DRM_DEBUG("comm=\"%s\", pid=%d, dev=0x%lx, auth=%d, %s\n",
1002 		  current->comm, task_pid_nr(current),
1003 		  (long)old_encode_dev(file_priv->minor->kdev->devt),
1004 		  file_priv->authenticated,
1005 		  drm_compat_ioctls[nr].name);
1006 	ret = (*fn)(filp, cmd, arg);
1007 	if (ret)
1008 		DRM_DEBUG("ret = %d\n", ret);
1009 	return ret;
1010 }
1011 EXPORT_SYMBOL(drm_compat_ioctl);
1012