Merge remote-tracking branch 'ftrace/for-next'
[deliverable/linux.git] / drivers / gpu / drm / drm_ioctl.c
CommitLineData
1da177e4
LT
1/*
2 * Created: Fri Jan 8 09:01:26 1999 by faith@valinux.com
3 *
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * All Rights Reserved.
7 *
ec8f112d
DH
8 * Author Rickard E. (Rik) Faith <faith@valinux.com>
9 * Author Gareth Hughes <gareth@valinux.com>
10 *
1da177e4
LT
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 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
760285e7
DH
31#include <drm/drmP.h>
32#include <drm/drm_core.h>
3b96a0b1 33#include <drm/drm_auth.h>
e7b96070 34#include "drm_legacy.h"
18882995 35#include "drm_internal.h"
a97df1cc 36#include "drm_crtc_internal.h"
1da177e4 37
760285e7
DH
38#include <linux/pci.h>
39#include <linux/export.h>
1da177e4 40
a3257256
DV
41/**
42 * DOC: getunique and setversion story
43 *
44 * BEWARE THE DRAGONS! MIND THE TRAPDOORS!
45 *
46 * In an attempt to warn anyone else who's trying to figure out what's going
47 * on here, I'll try to summarize the story. First things first, let's clear up
48 * the names, because the kernel internals, libdrm and the ioctls are all named
49 * differently:
50 *
51 * - GET_UNIQUE ioctl, implemented by drm_getunique is wrapped up in libdrm
52 * through the drmGetBusid function.
53 * - The libdrm drmSetBusid function is backed by the SET_UNIQUE ioctl. All
54 * that code is nerved in the kernel with drm_invalid_op().
55 * - The internal set_busid kernel functions and driver callbacks are
56 * exclusively use by the SET_VERSION ioctl, because only drm 1.0 (which is
57 * nerved) allowed userspace to set the busid through the above ioctl.
58 * - Other ioctls and functions involved are named consistently.
59 *
60 * For anyone wondering what's the difference between drm 1.1 and 1.4: Correctly
61 * handling pci domains in the busid on ppc. Doing this correctly was only
62 * implemented in libdrm in 2010, hence can't be nerved yet. No one knows what's
63 * special with drm 1.2 and 1.3.
64 *
65 * Now the actual horror story of how device lookup in drm works. At large,
66 * there's 2 different ways, either by busid, or by device driver name.
67 *
68 * Opening by busid is fairly simple:
69 *
70 * 1. First call SET_VERSION to make sure pci domains are handled properly. As a
71 * side-effect this fills out the unique name in the master structure.
72 * 2. Call GET_UNIQUE to read out the unique name from the master structure,
73 * which matches the busid thanks to step 1. If it doesn't, proceed to try
74 * the next device node.
75 *
76 * Opening by name is slightly different:
77 *
78 * 1. Directly call VERSION to get the version and to match against the driver
79 * name returned by that ioctl. Note that SET_VERSION is not called, which
80 * means the the unique name for the master node just opening is _not_ filled
81 * out. This despite that with current drm device nodes are always bound to
82 * one device, and can't be runtime assigned like with drm 1.0.
83 * 2. Match driver name. If it mismatches, proceed to the next device node.
84 * 3. Call GET_UNIQUE, and check whether the unique name has length zero (by
85 * checking that the first byte in the string is 0). If that's not the case
86 * libdrm skips and proceeds to the next device node. Probably this is just
87 * copypasta from drm 1.0 times where a set unique name meant that the driver
88 * was in use already, but that's just conjecture.
89 *
90 * Long story short: To keep the open by name logic working, GET_UNIQUE must
91 * _not_ return a unique string when SET_VERSION hasn't been called yet,
92 * otherwise libdrm breaks. Even when that unique string can't ever change, and
93 * is totally irrelevant for actually opening the device because runtime
94 * assignable device instances were only support in drm 1.0, which is long dead.
95 * But the libdrm code in drmOpenByName somehow survived, hence this can't be
96 * broken.
97 */
98
ec8f112d
DH
99static int drm_version(struct drm_device *dev, void *data,
100 struct drm_file *file_priv);
101
0aaf20cf 102/*
1da177e4 103 * Get the bus id.
b5e89ed5 104 *
1da177e4 105 * \param inode device inode.
6c340eac 106 * \param file_priv DRM file private.
1da177e4
LT
107 * \param cmd command.
108 * \param arg user argument, pointing to a drm_unique structure.
109 * \return zero on success or a negative number on failure.
110 *
111 * Copies the bus id from drm_device::unique into user space.
112 */
78238757 113static int drm_getunique(struct drm_device *dev, void *data,
c153f45f 114 struct drm_file *file_priv)
1da177e4 115{
c153f45f 116 struct drm_unique *u = data;
7c1c2871 117 struct drm_master *master = file_priv->master;
1da177e4 118
7c1c2871
DA
119 if (u->unique_len >= master->unique_len) {
120 if (copy_to_user(u->unique, master->unique, master->unique_len))
1da177e4
LT
121 return -EFAULT;
122 }
7c1c2871 123 u->unique_len = master->unique_len;
c153f45f 124
1da177e4
LT
125 return 0;
126}
127
3fb688fd
CW
128static void
129drm_unset_busid(struct drm_device *dev,
130 struct drm_master *master)
131{
3fb688fd
CW
132 kfree(master->unique);
133 master->unique = NULL;
134 master->unique_len = 0;
3fb688fd
CW
135}
136
7c1c2871 137static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)
1da177e4 138{
7c1c2871 139 struct drm_master *master = file_priv->master;
8410ea3b 140 int ret;
3fb688fd
CW
141
142 if (master->unique != NULL)
143 drm_unset_busid(dev, master);
fe34765b 144
915b4d11
DH
145 if (dev->driver->set_busid) {
146 ret = dev->driver->set_busid(dev, master);
147 if (ret) {
148 drm_unset_busid(dev, master);
149 return ret;
150 }
ca8e2ad7 151 } else {
5079c464 152 WARN_ON(!dev->unique);
ca8e2ad7
TR
153 master->unique = kstrdup(dev->unique, GFP_KERNEL);
154 if (master->unique)
155 master->unique_len = strlen(dev->unique);
156 }
157
1da177e4
LT
158 return 0;
159}
160
0aaf20cf 161/*
1da177e4
LT
162 * Get client information.
163 *
164 * \param inode device inode.
6c340eac 165 * \param file_priv DRM file private.
1da177e4
LT
166 * \param cmd command.
167 * \param arg user argument, pointing to a drm_client structure.
b5e89ed5 168 *
1da177e4
LT
169 * \return zero on success or a negative number on failure.
170 *
171 * Searches for the client with the specified index and copies its information
172 * into userspace
173 */
78238757 174static int drm_getclient(struct drm_device *dev, void *data,
c153f45f 175 struct drm_file *file_priv)
1da177e4 176{
c153f45f 177 struct drm_client *client = data;
09b4ea47 178
719524df
DV
179 /*
180 * Hollowed-out getclient ioctl to keep some dead old drm tests/tools
181 * not breaking completely. Userspace tools stop enumerating one they
182 * get -EINVAL, hence this is the return value we need to hand back for
183 * no clients tracked.
184 *
185 * Unfortunately some clients (*cough* libva *cough*) use this in a fun
186 * attempt to figure out whether they're authenticated or not. Since
187 * that's the only thing they care about, give it to the directly
188 * instead of walking one giant list.
189 */
190 if (client->idx == 0) {
191 client->auth = file_priv->authenticated;
a8c79818
DH
192 client->pid = task_pid_vnr(current);
193 client->uid = overflowuid;
719524df
DV
194 client->magic = 0;
195 client->iocs = 0;
196
197 return 0;
198 } else {
199 return -EINVAL;
bd1b331f 200 }
1da177e4
LT
201}
202
0aaf20cf 203/*
b5e89ed5
DA
204 * Get statistics information.
205 *
1da177e4 206 * \param inode device inode.
6c340eac 207 * \param file_priv DRM file private.
1da177e4
LT
208 * \param cmd command.
209 * \param arg user argument, pointing to a drm_stats structure.
b5e89ed5 210 *
1da177e4
LT
211 * \return zero on success or a negative number on failure.
212 */
78238757 213static int drm_getstats(struct drm_device *dev, void *data,
c153f45f 214 struct drm_file *file_priv)
1da177e4 215{
c153f45f 216 struct drm_stats *stats = data;
1da177e4 217
d79cdc83 218 /* Clear stats to prevent userspace from eating its stack garbage. */
246a3d18 219 memset(stats, 0, sizeof(*stats));
b5e89ed5 220
1da177e4
LT
221 return 0;
222}
223
0aaf20cf 224/*
9f35421e
BS
225 * Get device/driver capabilities
226 */
78238757 227static int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
9f35421e
BS
228{
229 struct drm_get_cap *req = data;
f837297a 230 struct drm_crtc *crtc;
9f35421e
BS
231
232 req->value = 0;
e73f88af
DA
233 switch (req->capability) {
234 case DRM_CAP_DUMB_BUFFER:
235 if (dev->driver->dumb_create)
236 req->value = 1;
237 break;
51eab416 238 case DRM_CAP_VBLANK_HIGH_CRTC:
19b01b5f
IH
239 req->value = 1;
240 break;
019d96cb
DA
241 case DRM_CAP_DUMB_PREFERRED_DEPTH:
242 req->value = dev->mode_config.preferred_depth;
243 break;
244 case DRM_CAP_DUMB_PREFER_SHADOW:
245 req->value = dev->mode_config.prefer_shadow;
246 break;
4271a409
DA
247 case DRM_CAP_PRIME:
248 req->value |= dev->driver->prime_fd_to_handle ? DRM_PRIME_CAP_IMPORT : 0;
249 req->value |= dev->driver->prime_handle_to_fd ? DRM_PRIME_CAP_EXPORT : 0;
250 break;
c61eef72
ID
251 case DRM_CAP_TIMESTAMP_MONOTONIC:
252 req->value = drm_timestamp_monotonic;
253 break;
62f2104f
KP
254 case DRM_CAP_ASYNC_PAGE_FLIP:
255 req->value = dev->mode_config.async_page_flip;
256 break;
f837297a
MD
257 case DRM_CAP_PAGE_FLIP_TARGET:
258 req->value = 1;
259 drm_for_each_crtc(crtc, dev) {
260 if (!crtc->funcs->page_flip_target)
261 req->value = 0;
262 }
263 break;
8716ed4e
AD
264 case DRM_CAP_CURSOR_WIDTH:
265 if (dev->mode_config.cursor_width)
266 req->value = dev->mode_config.cursor_width;
267 else
268 req->value = 64;
269 break;
270 case DRM_CAP_CURSOR_HEIGHT:
271 if (dev->mode_config.cursor_height)
272 req->value = dev->mode_config.cursor_height;
273 else
274 req->value = 64;
275 break;
e3eb3250
RC
276 case DRM_CAP_ADDFB2_MODIFIERS:
277 req->value = dev->mode_config.allow_fb_modifiers;
278 break;
e73f88af
DA
279 default:
280 return -EINVAL;
281 }
9f35421e
BS
282 return 0;
283}
284
0aaf20cf 285/*
1c0814fe
DL
286 * Set device/driver capabilities
287 */
78238757 288static int
1c0814fe
DL
289drm_setclientcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
290{
61d8e328
DL
291 struct drm_set_client_cap *req = data;
292
293 switch (req->capability) {
294 case DRM_CLIENT_CAP_STEREO_3D:
295 if (req->value > 1)
296 return -EINVAL;
297 file_priv->stereo_allowed = req->value;
298 break;
681e7ec7 299 case DRM_CLIENT_CAP_UNIVERSAL_PLANES:
681e7ec7
MR
300 if (req->value > 1)
301 return -EINVAL;
302 file_priv->universal_planes = req->value;
303 break;
88a48e29 304 case DRM_CLIENT_CAP_ATOMIC:
88a48e29
RC
305 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
306 return -EINVAL;
307 if (req->value > 1)
308 return -EINVAL;
309 file_priv->atomic = req->value;
179f158c 310 file_priv->universal_planes = req->value;
88a48e29 311 break;
61d8e328
DL
312 default:
313 return -EINVAL;
314 }
315
316 return 0;
1c0814fe
DL
317}
318
0aaf20cf 319/*
1da177e4
LT
320 * Setversion ioctl.
321 *
322 * \param inode device inode.
6c340eac 323 * \param file_priv DRM file private.
1da177e4
LT
324 * \param cmd command.
325 * \param arg user argument, pointing to a drm_lock structure.
326 * \return zero on success or negative number on failure.
327 *
328 * Sets the requested interface version
329 */
78238757 330static int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
1da177e4 331{
c153f45f
EA
332 struct drm_set_version *sv = data;
333 int if_version, retcode = 0;
334
335 if (sv->drm_di_major != -1) {
336 if (sv->drm_di_major != DRM_IF_MAJOR ||
337 sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
338 retcode = -EINVAL;
339 goto done;
340 }
341 if_version = DRM_IF_VERSION(sv->drm_di_major,
342 sv->drm_di_minor);
3d77461e 343 dev->if_version = max(if_version, dev->if_version);
c153f45f 344 if (sv->drm_di_minor >= 1) {
1da177e4
LT
345 /*
346 * Version 1.1 includes tying of DRM to specific device
c17c2f89 347 * Version 1.4 has proper PCI domain support
1da177e4 348 */
3fb688fd
CW
349 retcode = drm_set_busid(dev, file_priv);
350 if (retcode)
351 goto done;
1da177e4
LT
352 }
353 }
354
c153f45f
EA
355 if (sv->drm_dd_major != -1) {
356 if (sv->drm_dd_major != dev->driver->major ||
357 sv->drm_dd_minor < 0 || sv->drm_dd_minor >
358 dev->driver->minor) {
359 retcode = -EINVAL;
360 goto done;
361 }
1da177e4 362 }
c153f45f
EA
363
364done:
365 sv->drm_di_major = DRM_IF_MAJOR;
366 sv->drm_di_minor = DRM_IF_MINOR;
367 sv->drm_dd_major = dev->driver->major;
368 sv->drm_dd_minor = dev->driver->minor;
369
370 return retcode;
1da177e4
LT
371}
372
0aaf20cf
DV
373/**
374 * drm_noop - DRM no-op ioctl implemntation
375 * @dev: DRM device for the ioctl
376 * @data: data pointer for the ioctl
377 * @file_priv: DRM file for the ioctl call
378 *
379 * This no-op implementation for drm ioctls is useful for deprecated
380 * functionality where we can't return a failure code because existing userspace
381 * checks the result of the ioctl, but doesn't care about the action.
382 *
383 * Always returns successfully with 0.
384 */
c153f45f
EA
385int drm_noop(struct drm_device *dev, void *data,
386 struct drm_file *file_priv)
1da177e4
LT
387{
388 DRM_DEBUG("\n");
389 return 0;
390}
b2c606fe 391EXPORT_SYMBOL(drm_noop);
ec8f112d 392
0aaf20cf
DV
393/**
394 * drm_invalid_op - DRM invalid ioctl implemntation
395 * @dev: DRM device for the ioctl
396 * @data: data pointer for the ioctl
397 * @file_priv: DRM file for the ioctl call
398 *
399 * This no-op implementation for drm ioctls is useful for deprecated
400 * functionality where we really don't want to allow userspace to call the ioctl
401 * any more. This is the case for old ums interfaces for drivers that
402 * transitioned to kms gradually and so kept the old legacy tables around. This
403 * only applies to radeon and i915 kms drivers, other drivers shouldn't need to
404 * use this function.
405 *
406 * Always fails with a return value of -EINVAL.
407 */
4b63539b
DV
408int drm_invalid_op(struct drm_device *dev, void *data,
409 struct drm_file *file_priv)
410{
411 return -EINVAL;
412}
413EXPORT_SYMBOL(drm_invalid_op);
414
0aaf20cf 415/*
ec8f112d
DH
416 * Copy and IOCTL return string to user space
417 */
418static int drm_copy_field(char __user *buf, size_t *buf_len, const char *value)
419{
420 int len;
421
422 /* don't overflow userbuf */
423 len = strlen(value);
424 if (len > *buf_len)
425 len = *buf_len;
426
427 /* let userspace know exact length of driver value (which could be
428 * larger than the userspace-supplied buffer) */
429 *buf_len = strlen(value);
430
431 /* finally, try filling in the userbuf */
432 if (len && buf)
433 if (copy_to_user(buf, value, len))
434 return -EFAULT;
435 return 0;
436}
437
0aaf20cf 438/*
ec8f112d
DH
439 * Get version information
440 *
441 * \param inode device inode.
442 * \param filp file pointer.
443 * \param cmd command.
444 * \param arg user argument, pointing to a drm_version structure.
445 * \return zero on success or negative number on failure.
446 *
447 * Fills in the version information in \p arg.
448 */
449static int drm_version(struct drm_device *dev, void *data,
450 struct drm_file *file_priv)
451{
452 struct drm_version *version = data;
453 int err;
454
455 version->version_major = dev->driver->major;
456 version->version_minor = dev->driver->minor;
457 version->version_patchlevel = dev->driver->patchlevel;
458 err = drm_copy_field(version->name, &version->name_len,
459 dev->driver->name);
460 if (!err)
461 err = drm_copy_field(version->date, &version->date_len,
462 dev->driver->date);
463 if (!err)
464 err = drm_copy_field(version->desc, &version->desc_len,
465 dev->driver->desc);
466
467 return err;
468}
469
0aaf20cf 470/*
ec8f112d
DH
471 * drm_ioctl_permit - Check ioctl permissions against caller
472 *
473 * @flags: ioctl permission flags.
474 * @file_priv: Pointer to struct drm_file identifying the caller.
475 *
476 * Checks whether the caller is allowed to run an ioctl with the
477 * indicated permissions. If so, returns zero. Otherwise returns an
478 * error code suitable for ioctl return.
479 */
5101020c 480int drm_ioctl_permit(u32 flags, struct drm_file *file_priv)
ec8f112d
DH
481{
482 /* ROOT_ONLY is only for CAP_SYS_ADMIN */
483 if (unlikely((flags & DRM_ROOT_ONLY) && !capable(CAP_SYS_ADMIN)))
484 return -EACCES;
485
486 /* AUTH is only for authenticated or render client */
487 if (unlikely((flags & DRM_AUTH) && !drm_is_render_client(file_priv) &&
488 !file_priv->authenticated))
489 return -EACCES;
490
491 /* MASTER is only for master or control clients */
b3ac9f25
DV
492 if (unlikely((flags & DRM_MASTER) &&
493 !drm_is_current_master(file_priv) &&
ec8f112d
DH
494 !drm_is_control_client(file_priv)))
495 return -EACCES;
496
497 /* Control clients must be explicitly allowed */
498 if (unlikely(!(flags & DRM_CONTROL_ALLOW) &&
499 drm_is_control_client(file_priv)))
500 return -EACCES;
501
502 /* Render clients must be explicitly allowed */
503 if (unlikely(!(flags & DRM_RENDER_ALLOW) &&
504 drm_is_render_client(file_priv)))
505 return -EACCES;
506
507 return 0;
508}
5101020c 509EXPORT_SYMBOL(drm_ioctl_permit);
ec8f112d 510
066626d5
EV
511#define DRM_IOCTL_DEF(ioctl, _func, _flags) \
512 [DRM_IOCTL_NR(ioctl)] = { \
513 .cmd = ioctl, \
514 .func = _func, \
515 .flags = _flags, \
516 .name = #ioctl \
517 }
78238757 518
0aaf20cf 519/* Ioctl table */
78238757 520static const struct drm_ioctl_desc drm_ioctls[] = {
30c64664
TH
521 DRM_IOCTL_DEF(DRM_IOCTL_VERSION, drm_version,
522 DRM_UNLOCKED|DRM_RENDER_ALLOW|DRM_CONTROL_ALLOW),
78238757 523 DRM_IOCTL_DEF(DRM_IOCTL_GET_UNIQUE, drm_getunique, 0),
94e86f5d 524 DRM_IOCTL_DEF(DRM_IOCTL_GET_MAGIC, drm_getmagic, DRM_UNLOCKED),
78238757 525 DRM_IOCTL_DEF(DRM_IOCTL_IRQ_BUSID, drm_irq_by_busid, DRM_MASTER|DRM_ROOT_ONLY),
ec1f52ef 526 DRM_IOCTL_DEF(DRM_IOCTL_GET_MAP, drm_legacy_getmap_ioctl, DRM_UNLOCKED),
78238757
DV
527 DRM_IOCTL_DEF(DRM_IOCTL_GET_CLIENT, drm_getclient, DRM_UNLOCKED),
528 DRM_IOCTL_DEF(DRM_IOCTL_GET_STATS, drm_getstats, DRM_UNLOCKED),
529 DRM_IOCTL_DEF(DRM_IOCTL_GET_CAP, drm_getcap, DRM_UNLOCKED|DRM_RENDER_ALLOW),
530 DRM_IOCTL_DEF(DRM_IOCTL_SET_CLIENT_CAP, drm_setclientcap, 0),
531 DRM_IOCTL_DEF(DRM_IOCTL_SET_VERSION, drm_setversion, DRM_MASTER),
532
46bfdf9a 533 DRM_IOCTL_DEF(DRM_IOCTL_SET_UNIQUE, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
78238757
DV
534 DRM_IOCTL_DEF(DRM_IOCTL_BLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
535 DRM_IOCTL_DEF(DRM_IOCTL_UNBLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
94e86f5d 536 DRM_IOCTL_DEF(DRM_IOCTL_AUTH_MAGIC, drm_authmagic, DRM_AUTH|DRM_UNLOCKED|DRM_MASTER),
78238757
DV
537
538 DRM_IOCTL_DEF(DRM_IOCTL_ADD_MAP, drm_legacy_addmap_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
539 DRM_IOCTL_DEF(DRM_IOCTL_RM_MAP, drm_legacy_rmmap_ioctl, DRM_AUTH),
540
541 DRM_IOCTL_DEF(DRM_IOCTL_SET_SAREA_CTX, drm_legacy_setsareactx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
542 DRM_IOCTL_DEF(DRM_IOCTL_GET_SAREA_CTX, drm_legacy_getsareactx, DRM_AUTH),
543
ce833ba9
DV
544 DRM_IOCTL_DEF(DRM_IOCTL_SET_MASTER, drm_setmaster_ioctl, DRM_UNLOCKED|DRM_ROOT_ONLY),
545 DRM_IOCTL_DEF(DRM_IOCTL_DROP_MASTER, drm_dropmaster_ioctl, DRM_UNLOCKED|DRM_ROOT_ONLY),
78238757
DV
546
547 DRM_IOCTL_DEF(DRM_IOCTL_ADD_CTX, drm_legacy_addctx, DRM_AUTH|DRM_ROOT_ONLY),
548 DRM_IOCTL_DEF(DRM_IOCTL_RM_CTX, drm_legacy_rmctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
549 DRM_IOCTL_DEF(DRM_IOCTL_MOD_CTX, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
550 DRM_IOCTL_DEF(DRM_IOCTL_GET_CTX, drm_legacy_getctx, DRM_AUTH),
551 DRM_IOCTL_DEF(DRM_IOCTL_SWITCH_CTX, drm_legacy_switchctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
552 DRM_IOCTL_DEF(DRM_IOCTL_NEW_CTX, drm_legacy_newctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
553 DRM_IOCTL_DEF(DRM_IOCTL_RES_CTX, drm_legacy_resctx, DRM_AUTH),
554
555 DRM_IOCTL_DEF(DRM_IOCTL_ADD_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
556 DRM_IOCTL_DEF(DRM_IOCTL_RM_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
557
558 DRM_IOCTL_DEF(DRM_IOCTL_LOCK, drm_legacy_lock, DRM_AUTH),
559 DRM_IOCTL_DEF(DRM_IOCTL_UNLOCK, drm_legacy_unlock, DRM_AUTH),
560
561 DRM_IOCTL_DEF(DRM_IOCTL_FINISH, drm_noop, DRM_AUTH),
562
563 DRM_IOCTL_DEF(DRM_IOCTL_ADD_BUFS, drm_legacy_addbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
564 DRM_IOCTL_DEF(DRM_IOCTL_MARK_BUFS, drm_legacy_markbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
565 DRM_IOCTL_DEF(DRM_IOCTL_INFO_BUFS, drm_legacy_infobufs, DRM_AUTH),
566 DRM_IOCTL_DEF(DRM_IOCTL_MAP_BUFS, drm_legacy_mapbufs, DRM_AUTH),
567 DRM_IOCTL_DEF(DRM_IOCTL_FREE_BUFS, drm_legacy_freebufs, DRM_AUTH),
568 DRM_IOCTL_DEF(DRM_IOCTL_DMA, drm_legacy_dma_ioctl, DRM_AUTH),
569
570 DRM_IOCTL_DEF(DRM_IOCTL_CONTROL, drm_control, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
571
a7fb8a23 572#if IS_ENABLED(CONFIG_AGP)
78238757
DV
573 DRM_IOCTL_DEF(DRM_IOCTL_AGP_ACQUIRE, drm_agp_acquire_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
574 DRM_IOCTL_DEF(DRM_IOCTL_AGP_RELEASE, drm_agp_release_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
575 DRM_IOCTL_DEF(DRM_IOCTL_AGP_ENABLE, drm_agp_enable_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
576 DRM_IOCTL_DEF(DRM_IOCTL_AGP_INFO, drm_agp_info_ioctl, DRM_AUTH),
577 DRM_IOCTL_DEF(DRM_IOCTL_AGP_ALLOC, drm_agp_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
578 DRM_IOCTL_DEF(DRM_IOCTL_AGP_FREE, drm_agp_free_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
579 DRM_IOCTL_DEF(DRM_IOCTL_AGP_BIND, drm_agp_bind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
580 DRM_IOCTL_DEF(DRM_IOCTL_AGP_UNBIND, drm_agp_unbind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
581#endif
582
583 DRM_IOCTL_DEF(DRM_IOCTL_SG_ALLOC, drm_legacy_sg_alloc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
584 DRM_IOCTL_DEF(DRM_IOCTL_SG_FREE, drm_legacy_sg_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
585
586 DRM_IOCTL_DEF(DRM_IOCTL_WAIT_VBLANK, drm_wait_vblank, DRM_UNLOCKED),
587
588 DRM_IOCTL_DEF(DRM_IOCTL_MODESET_CTL, drm_modeset_ctl, 0),
589
590 DRM_IOCTL_DEF(DRM_IOCTL_UPDATE_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
591
592 DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
593 DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH|DRM_UNLOCKED),
594 DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH|DRM_UNLOCKED),
595
596 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETRESOURCES, drm_mode_getresources, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
597
598 DRM_IOCTL_DEF(DRM_IOCTL_PRIME_HANDLE_TO_FD, drm_prime_handle_to_fd_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
599 DRM_IOCTL_DEF(DRM_IOCTL_PRIME_FD_TO_HANDLE, drm_prime_fd_to_handle_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
600
601 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
602 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
603 DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
604 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANE, drm_mode_getplane, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
605 DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPLANE, drm_mode_setplane, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
606 DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR, drm_mode_cursor_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
607 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETGAMMA, drm_mode_gamma_get_ioctl, DRM_UNLOCKED),
608 DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETGAMMA, drm_mode_gamma_set_ioctl, DRM_MASTER|DRM_UNLOCKED),
609 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETENCODER, drm_mode_getencoder, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
610 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCONNECTOR, drm_mode_getconnector, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
611 DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATTACHMODE, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
612 DRM_IOCTL_DEF(DRM_IOCTL_MODE_DETACHMODE, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
613 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPERTY, drm_mode_getproperty_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
614 DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPROPERTY, drm_mode_connector_property_set_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
615 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPBLOB, drm_mode_getblob_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
616 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETFB, drm_mode_getfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
617 DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB, drm_mode_addfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
618 DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB2, drm_mode_addfb2, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
619 DRM_IOCTL_DEF(DRM_IOCTL_MODE_RMFB, drm_mode_rmfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
620 DRM_IOCTL_DEF(DRM_IOCTL_MODE_PAGE_FLIP, drm_mode_page_flip_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
621 DRM_IOCTL_DEF(DRM_IOCTL_MODE_DIRTYFB, drm_mode_dirtyfb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
622 DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_DUMB, drm_mode_create_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
623 DRM_IOCTL_DEF(DRM_IOCTL_MODE_MAP_DUMB, drm_mode_mmap_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
624 DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROY_DUMB, drm_mode_destroy_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
625 DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_GETPROPERTIES, drm_mode_obj_get_properties_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
626 DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_SETPROPERTY, drm_mode_obj_set_property_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
627 DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR2, drm_mode_cursor2_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
d34f20d6 628 DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATOMIC, drm_mode_atomic_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
e2f5d2ea
DS
629 DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATEPROPBLOB, drm_mode_createblob_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
630 DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROYPROPBLOB, drm_mode_destroyblob_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
78238757
DV
631};
632
633#define DRM_CORE_IOCTL_COUNT ARRAY_SIZE( drm_ioctls )
634
ec8f112d 635/**
0aaf20cf
DV
636 * drm_ioctl - ioctl callback implementation for DRM drivers
637 * @filp: file this ioctl is called on
638 * @cmd: ioctl cmd number
639 * @arg: user argument
ec8f112d
DH
640 *
641 * Looks up the ioctl function in the ::ioctls table, checking for root
642 * previleges if so required, and dispatches to the respective function.
0aaf20cf
DV
643 *
644 * Returns:
645 * Zero on success, negative error code on failure.
ec8f112d
DH
646 */
647long drm_ioctl(struct file *filp,
648 unsigned int cmd, unsigned long arg)
649{
650 struct drm_file *file_priv = filp->private_data;
651 struct drm_device *dev;
652 const struct drm_ioctl_desc *ioctl = NULL;
653 drm_ioctl_t *func;
654 unsigned int nr = DRM_IOCTL_NR(cmd);
655 int retcode = -EINVAL;
656 char stack_kdata[128];
657 char *kdata = NULL;
01d3434a 658 unsigned int in_size, out_size, drv_size, ksize;
ea487835 659 bool is_driver_ioctl;
ec8f112d
DH
660
661 dev = file_priv->minor->dev;
662
663 if (drm_device_is_unplugged(dev))
664 return -ENODEV;
665
ea487835
DV
666 is_driver_ioctl = nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END;
667
668 if (is_driver_ioctl) {
83be0038
VS
669 /* driver ioctl */
670 if (nr - DRM_COMMAND_BASE >= dev->driver->num_ioctls)
671 goto err_i1;
ec8f112d 672 ioctl = &dev->driver->ioctls[nr - DRM_COMMAND_BASE];
83be0038
VS
673 } else {
674 /* core ioctl */
675 if (nr >= DRM_CORE_IOCTL_COUNT)
676 goto err_i1;
ec8f112d 677 ioctl = &drm_ioctls[nr];
83be0038 678 }
ec8f112d 679
83be0038 680 drv_size = _IOC_SIZE(ioctl->cmd);
01d3434a
CW
681 out_size = in_size = _IOC_SIZE(cmd);
682 if ((cmd & ioctl->cmd & IOC_IN) == 0)
683 in_size = 0;
684 if ((cmd & ioctl->cmd & IOC_OUT) == 0)
685 out_size = 0;
686 ksize = max(max(in_size, out_size), drv_size);
ec8f112d
DH
687
688 DRM_DEBUG("pid=%d, dev=0x%lx, auth=%d, %s\n",
689 task_pid_nr(current),
690 (long)old_encode_dev(file_priv->minor->kdev->devt),
691 file_priv->authenticated, ioctl->name);
692
693 /* Do not trust userspace, use our own definition */
694 func = ioctl->func;
695
696 if (unlikely(!func)) {
697 DRM_DEBUG("no function\n");
698 retcode = -EINVAL;
699 goto err_i1;
700 }
701
702 retcode = drm_ioctl_permit(ioctl->flags, file_priv);
703 if (unlikely(retcode))
704 goto err_i1;
705
01d3434a
CW
706 if (ksize <= sizeof(stack_kdata)) {
707 kdata = stack_kdata;
708 } else {
709 kdata = kmalloc(ksize, GFP_KERNEL);
710 if (!kdata) {
711 retcode = -ENOMEM;
712 goto err_i1;
ec8f112d 713 }
ec8f112d
DH
714 }
715
01d3434a
CW
716 if (copy_from_user(kdata, (void __user *)arg, in_size) != 0) {
717 retcode = -EFAULT;
718 goto err_i1;
ec8f112d
DH
719 }
720
01d3434a
CW
721 if (ksize > in_size)
722 memset(kdata + in_size, 0, ksize - in_size);
723
fa538645 724 /* Enforce sane locking for modern driver ioctls. Core ioctls are
ea487835 725 * too messy still. */
fa538645 726 if ((!drm_core_check_feature(dev, DRIVER_LEGACY) && is_driver_ioctl) ||
ea487835 727 (ioctl->flags & DRM_UNLOCKED))
ec8f112d
DH
728 retcode = func(dev, kdata, file_priv);
729 else {
730 mutex_lock(&drm_global_mutex);
731 retcode = func(dev, kdata, file_priv);
732 mutex_unlock(&drm_global_mutex);
733 }
734
01d3434a
CW
735 if (copy_to_user((void __user *)arg, kdata, out_size) != 0)
736 retcode = -EFAULT;
ec8f112d
DH
737
738 err_i1:
739 if (!ioctl)
740 DRM_DEBUG("invalid ioctl: pid=%d, dev=0x%lx, auth=%d, cmd=0x%02x, nr=0x%02x\n",
741 task_pid_nr(current),
742 (long)old_encode_dev(file_priv->minor->kdev->devt),
743 file_priv->authenticated, cmd, nr);
744
745 if (kdata != stack_kdata)
746 kfree(kdata);
747 if (retcode)
748 DRM_DEBUG("ret = %d\n", retcode);
749 return retcode;
750}
751EXPORT_SYMBOL(drm_ioctl);
752
753/**
754 * drm_ioctl_flags - Check for core ioctl and return ioctl permission flags
0aaf20cf
DV
755 * @nr: ioctl number
756 * @flags: where to return the ioctl permission flags
757 *
758 * This ioctl is only used by the vmwgfx driver to augment the access checks
759 * done by the drm core and insofar a pretty decent layering violation. This
760 * shouldn't be used by any drivers.
ec8f112d 761 *
0aaf20cf 762 * Returns:
2ae99588 763 * True if the @nr corresponds to a DRM core ioctl number, false otherwise.
ec8f112d
DH
764 */
765bool drm_ioctl_flags(unsigned int nr, unsigned int *flags)
766{
7ef5f82b
VS
767 if (nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END)
768 return false;
769
770 if (nr >= DRM_CORE_IOCTL_COUNT)
771 return false;
ec8f112d 772
7ef5f82b
VS
773 *flags = drm_ioctls[nr].flags;
774 return true;
ec8f112d
DH
775}
776EXPORT_SYMBOL(drm_ioctl_flags);
This page took 1.297848 seconds and 5 git commands to generate.