Merge tag 'kvmarm-fixes-6.8-2' of git://git.kernel.org/pub/scm/linux/kernel/git/kvmar...
[linux-2.6-block.git] / drivers / gpu / drm / drm_ioc32.c
CommitLineData
2640981f 1/*
9a186645
DA
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>
cafe8d84 31#include <linux/ratelimit.h>
2d1a8a48 32#include <linux/export.h>
9a186645 33
9f4db449 34#include <drm/drm_device.h>
0500c04e
SR
35#include <drm/drm_file.h>
36#include <drm/drm_print.h>
37
d6c56613 38#include "drm_crtc_internal.h"
0500c04e 39#include "drm_internal.h"
9a186645
DA
40
41#define DRM_IOCTL_VERSION32 DRM_IOWR(0x00, drm_version32_t)
42#define DRM_IOCTL_GET_UNIQUE32 DRM_IOWR(0x01, drm_unique32_t)
43#define DRM_IOCTL_GET_MAP32 DRM_IOWR(0x04, drm_map32_t)
44#define DRM_IOCTL_GET_CLIENT32 DRM_IOWR(0x05, drm_client32_t)
45#define DRM_IOCTL_GET_STATS32 DRM_IOR( 0x06, drm_stats32_t)
46
47#define DRM_IOCTL_SET_UNIQUE32 DRM_IOW( 0x10, drm_unique32_t)
48#define DRM_IOCTL_ADD_MAP32 DRM_IOWR(0x15, drm_map32_t)
49#define DRM_IOCTL_ADD_BUFS32 DRM_IOWR(0x16, drm_buf_desc32_t)
50#define DRM_IOCTL_MARK_BUFS32 DRM_IOW( 0x17, drm_buf_desc32_t)
51#define DRM_IOCTL_INFO_BUFS32 DRM_IOWR(0x18, drm_buf_info32_t)
52#define DRM_IOCTL_MAP_BUFS32 DRM_IOWR(0x19, drm_buf_map32_t)
53#define DRM_IOCTL_FREE_BUFS32 DRM_IOW( 0x1a, drm_buf_free32_t)
54
55#define DRM_IOCTL_RM_MAP32 DRM_IOW( 0x1b, drm_map32_t)
56
57#define DRM_IOCTL_SET_SAREA_CTX32 DRM_IOW( 0x1c, drm_ctx_priv_map32_t)
58#define DRM_IOCTL_GET_SAREA_CTX32 DRM_IOWR(0x1d, drm_ctx_priv_map32_t)
59
60#define DRM_IOCTL_RES_CTX32 DRM_IOWR(0x26, drm_ctx_res32_t)
61#define DRM_IOCTL_DMA32 DRM_IOWR(0x29, drm_dma32_t)
62
63#define DRM_IOCTL_AGP_ENABLE32 DRM_IOW( 0x32, drm_agp_mode32_t)
64#define DRM_IOCTL_AGP_INFO32 DRM_IOR( 0x33, drm_agp_info32_t)
65#define DRM_IOCTL_AGP_ALLOC32 DRM_IOWR(0x34, drm_agp_buffer32_t)
66#define DRM_IOCTL_AGP_FREE32 DRM_IOW( 0x35, drm_agp_buffer32_t)
67#define DRM_IOCTL_AGP_BIND32 DRM_IOW( 0x36, drm_agp_binding32_t)
68#define DRM_IOCTL_AGP_UNBIND32 DRM_IOW( 0x37, drm_agp_binding32_t)
69
70#define DRM_IOCTL_SG_ALLOC32 DRM_IOW( 0x38, drm_scatter_gather32_t)
71#define DRM_IOCTL_SG_FREE32 DRM_IOW( 0x39, drm_scatter_gather32_t)
72
49568873
EA
73#define DRM_IOCTL_UPDATE_DRAW32 DRM_IOW( 0x3f, drm_update_draw32_t)
74
9a186645
DA
75#define DRM_IOCTL_WAIT_VBLANK32 DRM_IOWR(0x3a, drm_wait_vblank32_t)
76
c631d5f9
TU
77#define DRM_IOCTL_MODE_ADDFB232 DRM_IOWR(0xb8, drm_mode_fb_cmd232_t)
78
9a186645 79typedef struct drm_version_32 {
2640981f
DV
80 int version_major; /* Major version */
81 int version_minor; /* Minor version */
82 int version_patchlevel; /* Patch level */
83 u32 name_len; /* Length of name buffer */
84 u32 name; /* Name of driver */
85 u32 date_len; /* Length of date buffer */
86 u32 date; /* User-space buffer to hold date */
87 u32 desc_len; /* Length of desc buffer */
88 u32 desc; /* User-space buffer to hold desc */
9a186645
DA
89} drm_version32_t;
90
91static int compat_drm_version(struct file *file, unsigned int cmd,
92 unsigned long arg)
93{
94 drm_version32_t v32;
012c6741 95 struct drm_version v;
9a186645
DA
96 int err;
97
b5e89ed5 98 if (copy_from_user(&v32, (void __user *)arg, sizeof(v32)))
9a186645
DA
99 return -EFAULT;
100
e926c474
DV
101 memset(&v, 0, sizeof(v));
102
012c6741
AV
103 v = (struct drm_version) {
104 .name_len = v32.name_len,
105 .name = compat_ptr(v32.name),
106 .date_len = v32.date_len,
107 .date = compat_ptr(v32.date),
108 .desc_len = v32.desc_len,
109 .desc = compat_ptr(v32.desc),
110 };
111 err = drm_ioctl_kernel(file, drm_version, &v,
75426367 112 DRM_RENDER_ALLOW);
9a186645
DA
113 if (err)
114 return err;
115
012c6741
AV
116 v32.version_major = v.version_major;
117 v32.version_minor = v.version_minor;
118 v32.version_patchlevel = v.version_patchlevel;
266530b3
JC
119 v32.name_len = v.name_len;
120 v32.date_len = v.date_len;
121 v32.desc_len = v.desc_len;
b5e89ed5 122 if (copy_to_user((void __user *)arg, &v32, sizeof(v32)))
9a186645
DA
123 return -EFAULT;
124 return 0;
125}
126
127typedef struct drm_unique32 {
2640981f
DV
128 u32 unique_len; /* Length of unique */
129 u32 unique; /* Unique name for driver instantiation */
9a186645
DA
130} drm_unique32_t;
131
132static int compat_drm_getunique(struct file *file, unsigned int cmd,
133 unsigned long arg)
134{
135 drm_unique32_t uq32;
17e3dade 136 struct drm_unique uq;
9a186645
DA
137 int err;
138
b5e89ed5 139 if (copy_from_user(&uq32, (void __user *)arg, sizeof(uq32)))
9a186645 140 return -EFAULT;
e926c474
DV
141
142 memset(&uq, 0, sizeof(uq));
143
17e3dade
AV
144 uq = (struct drm_unique){
145 .unique_len = uq32.unique_len,
146 .unique = compat_ptr(uq32.unique),
147 };
9a186645 148
75426367 149 err = drm_ioctl_kernel(file, drm_getunique, &uq, 0);
9a186645
DA
150 if (err)
151 return err;
152
17e3dade 153 uq32.unique_len = uq.unique_len;
b5e89ed5 154 if (copy_to_user((void __user *)arg, &uq32, sizeof(uq32)))
9a186645
DA
155 return -EFAULT;
156 return 0;
157}
158
159static int compat_drm_setunique(struct file *file, unsigned int cmd,
160 unsigned long arg)
161{
56c275cc
AV
162 /* it's dead */
163 return -EINVAL;
9a186645
DA
164}
165
9a186645 166typedef struct drm_client32 {
2640981f
DV
167 int idx; /* Which client desired? */
168 int auth; /* Is client authenticated? */
169 u32 pid; /* Process ID */
170 u32 uid; /* User ID */
171 u32 magic; /* Magic */
172 u32 iocs; /* Ioctl count */
9a186645
DA
173} drm_client32_t;
174
175static int compat_drm_getclient(struct file *file, unsigned int cmd,
176 unsigned long arg)
177{
178 drm_client32_t c32;
179 drm_client32_t __user *argp = (void __user *)arg;
9e92662d
AV
180 struct drm_client client;
181 int err;
9a186645 182
9e92662d 183 if (copy_from_user(&c32, argp, sizeof(c32)))
9a186645
DA
184 return -EFAULT;
185
e926c474
DV
186 memset(&client, 0, sizeof(client));
187
9e92662d 188 client.idx = c32.idx;
9a186645 189
75426367 190 err = drm_ioctl_kernel(file, drm_getclient, &client, 0);
9a186645
DA
191 if (err)
192 return err;
193
9e92662d
AV
194 c32.idx = client.idx;
195 c32.auth = client.auth;
196 c32.pid = client.pid;
197 c32.uid = client.uid;
198 c32.magic = client.magic;
199 c32.iocs = client.iocs;
9a186645
DA
200
201 if (copy_to_user(argp, &c32, sizeof(c32)))
202 return -EFAULT;
203 return 0;
204}
205
206typedef struct drm_stats32 {
207 u32 count;
208 struct {
209 u32 value;
ff4135ae 210 enum drm_stat_type type;
9a186645
DA
211 } data[15];
212} drm_stats32_t;
213
214static int compat_drm_getstats(struct file *file, unsigned int cmd,
215 unsigned long arg)
216{
9a186645 217 drm_stats32_t __user *argp = (void __user *)arg;
9a186645 218
2da9a1a2 219 /* getstats is defunct, just clear */
8547ee9d 220 if (clear_user(argp, sizeof(drm_stats32_t)))
9a186645
DA
221 return -EFAULT;
222 return 0;
223}
224
9cc73ce2 225#if defined(CONFIG_X86)
49568873
EA
226typedef struct drm_update_draw32 {
227 drm_drawable_t handle;
228 unsigned int type;
229 unsigned int num;
230 /* 64-bit version has a 32-bit pad here */
231 u64 data; /**< Pointer */
232} __attribute__((packed)) drm_update_draw32_t;
233
234static int compat_drm_update_draw(struct file *file, unsigned int cmd,
235 unsigned long arg)
236{
2da9a1a2
DV
237 /* update_draw is defunct */
238 return 0;
49568873 239}
09e40d65 240#endif
49568873 241
9a186645 242struct drm_wait_vblank_request32 {
ff4135ae 243 enum drm_vblank_seq_type type;
9a186645
DA
244 unsigned int sequence;
245 u32 signal;
246};
247
248struct drm_wait_vblank_reply32 {
ff4135ae 249 enum drm_vblank_seq_type type;
9a186645
DA
250 unsigned int sequence;
251 s32 tval_sec;
252 s32 tval_usec;
253};
254
255typedef union drm_wait_vblank32 {
256 struct drm_wait_vblank_request32 request;
257 struct drm_wait_vblank_reply32 reply;
258} drm_wait_vblank32_t;
259
260static int compat_drm_wait_vblank(struct file *file, unsigned int cmd,
261 unsigned long arg)
262{
263 drm_wait_vblank32_t __user *argp = (void __user *)arg;
264 drm_wait_vblank32_t req32;
d5288c88 265 union drm_wait_vblank req;
9a186645
DA
266 int err;
267
268 if (copy_from_user(&req32, argp, sizeof(req32)))
269 return -EFAULT;
270
e926c474
DV
271 memset(&req, 0, sizeof(req));
272
d5288c88
AV
273 req.request.type = req32.request.type;
274 req.request.sequence = req32.request.sequence;
275 req.request.signal = req32.request.signal;
2798ffcc 276 err = drm_ioctl_kernel(file, drm_wait_vblank_ioctl, &req, 0);
9a186645 277
d5288c88
AV
278 req32.reply.type = req.reply.type;
279 req32.reply.sequence = req.reply.sequence;
280 req32.reply.tval_sec = req.reply.tval_sec;
281 req32.reply.tval_usec = req.reply.tval_usec;
9a186645
DA
282 if (copy_to_user(argp, &req32, sizeof(req32)))
283 return -EFAULT;
284
fa0b1ef5 285 return err;
9a186645
DA
286}
287
9cc73ce2 288#if defined(CONFIG_X86)
c631d5f9
TU
289typedef struct drm_mode_fb_cmd232 {
290 u32 fb_id;
291 u32 width;
292 u32 height;
293 u32 pixel_format;
294 u32 flags;
295 u32 handles[4];
296 u32 pitches[4];
297 u32 offsets[4];
298 u64 modifier[4];
299} __attribute__((packed)) drm_mode_fb_cmd232_t;
300
301static int compat_drm_mode_addfb2(struct file *file, unsigned int cmd,
302 unsigned long arg)
303{
304 struct drm_mode_fb_cmd232 __user *argp = (void __user *)arg;
b87b786b 305 struct drm_mode_fb_cmd2 req64;
c631d5f9
TU
306 int err;
307
e926c474
DV
308 memset(&req64, 0, sizeof(req64));
309
d6c56613
AV
310 if (copy_from_user(&req64, argp,
311 offsetof(drm_mode_fb_cmd232_t, modifier)))
c631d5f9
TU
312 return -EFAULT;
313
d6c56613
AV
314 if (copy_from_user(&req64.modifier, &argp->modifier,
315 sizeof(req64.modifier)))
c631d5f9
TU
316 return -EFAULT;
317
75426367 318 err = drm_ioctl_kernel(file, drm_mode_addfb2, &req64, 0);
c631d5f9
TU
319 if (err)
320 return err;
321
d6c56613 322 if (put_user(req64.fb_id, &argp->fb_id))
c631d5f9
TU
323 return -EFAULT;
324
325 return 0;
326}
a988588b 327#endif
c631d5f9 328
9f43e542
AV
329static struct {
330 drm_ioctl_compat_t *fn;
331 char *name;
332} drm_compat_ioctls[] = {
ff22ff9e 333#define DRM_IOCTL32_DEF(n, f) [DRM_IOCTL_NR(n##32)] = {.fn = f, .name = #n}
012c6741 334 DRM_IOCTL32_DEF(DRM_IOCTL_VERSION, compat_drm_version),
17e3dade 335 DRM_IOCTL32_DEF(DRM_IOCTL_GET_UNIQUE, compat_drm_getunique),
9e92662d 336 DRM_IOCTL32_DEF(DRM_IOCTL_GET_CLIENT, compat_drm_getclient),
8547ee9d 337 DRM_IOCTL32_DEF(DRM_IOCTL_GET_STATS, compat_drm_getstats),
56c275cc 338 DRM_IOCTL32_DEF(DRM_IOCTL_SET_UNIQUE, compat_drm_setunique),
cf8e8658 339#if defined(CONFIG_X86)
1a36bf1d 340 DRM_IOCTL32_DEF(DRM_IOCTL_UPDATE_DRAW, compat_drm_update_draw),
09e40d65 341#endif
d5288c88 342 DRM_IOCTL32_DEF(DRM_IOCTL_WAIT_VBLANK, compat_drm_wait_vblank),
cf8e8658 343#if defined(CONFIG_X86)
d6c56613 344 DRM_IOCTL32_DEF(DRM_IOCTL_MODE_ADDFB2, compat_drm_mode_addfb2),
a988588b 345#endif
9a186645
DA
346};
347
348/**
2640981f
DV
349 * drm_compat_ioctl - 32bit IOCTL compatibility handler for DRM drivers
350 * @filp: file this ioctl is called on
351 * @cmd: ioctl cmd number
352 * @arg: user argument
353 *
354 * Compatibility handler for 32 bit userspace running on 64 kernels. All actual
355 * IOCTL handling is forwarded to drm_ioctl(), while marshalling structures as
356 * appropriate. Note that this only handles DRM core IOCTLs, if the driver has
357 * botched IOCTL itself, it must handle those by wrapping this function.
9a186645 358 *
2640981f
DV
359 * Returns:
360 * Zero on success, negative error code on failure.
9a186645
DA
361 */
362long drm_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
363{
364 unsigned int nr = DRM_IOCTL_NR(cmd);
88e3cb07 365 struct drm_file *file_priv = filp->private_data;
723dad97 366 struct drm_device *dev = file_priv->minor->dev;
9a186645
DA
367 drm_ioctl_compat_t *fn;
368 int ret;
369
7ffa05e0
IR
370 /* Assume that ioctls without an explicit compat routine will just
371 * work. This may not always be a good assumption, but it's better
372 * than always failing.
373 */
3d77461e 374 if (nr >= ARRAY_SIZE(drm_compat_ioctls))
ed8b6704 375 return drm_ioctl(filp, cmd, arg);
9a186645 376
9f43e542
AV
377 fn = drm_compat_ioctls[nr].fn;
378 if (!fn)
379 return drm_ioctl(filp, cmd, arg);
9a186645 380
723dad97
TU
381 drm_dbg_core(dev, "comm=\"%s\", pid=%d, dev=0x%lx, auth=%d, %s\n",
382 current->comm, task_pid_nr(current),
383 (long)old_encode_dev(file_priv->minor->kdev->devt),
384 file_priv->authenticated,
385 drm_compat_ioctls[nr].name);
88e3cb07
AV
386 ret = (*fn)(filp, cmd, arg);
387 if (ret)
723dad97 388 drm_dbg_core(dev, "ret = %d\n", ret);
9a186645
DA
389 return ret;
390}
391EXPORT_SYMBOL(drm_compat_ioctl);