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 * 8 * Author Rickard E. (Rik) Faith <faith@valinux.com> 9 * Author Gareth Hughes <gareth@valinux.com> 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 * 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 31 #include <sys/devfs.h> 32 33 #include <drm/drmP.h> 34 #include <drm/drm_core.h> 35 #include "drm_legacy.h" 36 #include "drm_internal.h" 37 #include "drm_crtc_internal.h" 38 39 #include <linux/export.h> 40 41 static int drm_version(struct drm_device *dev, void *data, 42 struct drm_file *file_priv); 43 44 /* 45 * Get the bus id. 46 * 47 * \param inode device inode. 48 * \param file_priv DRM file private. 49 * \param cmd command. 50 * \param arg user argument, pointing to a drm_unique structure. 51 * \return zero on success or a negative number on failure. 52 * 53 * Copies the bus id from drm_device::unique into user space. 54 */ 55 static int drm_getunique(struct drm_device *dev, void *data, 56 struct drm_file *file_priv) 57 { 58 struct drm_unique *u = data; 59 60 if (u->unique_len >= dev->unique_len) { 61 if (copy_to_user(u->unique, dev->unique, dev->unique_len)) 62 return -EFAULT; 63 } 64 u->unique_len = dev->unique_len; 65 66 return 0; 67 } 68 69 /* 70 * Set the bus id. 71 * 72 * \param inode device inode. 73 * \param file_priv DRM file private. 74 * \param cmd command. 75 * \param arg user argument, pointing to a drm_unique structure. 76 * \return zero on success or a negative number on failure. 77 * 78 * Copies the bus id from userspace into drm_device::unique, and verifies that 79 * it matches the device this DRM is attached to (EINVAL otherwise). Deprecated 80 * in interface version 1.1 and will return EBUSY when setversion has requested 81 * version 1.1 or greater. 82 */ 83 static int drm_setunique(struct drm_device *dev, void *data, 84 struct drm_file *file_priv) 85 { 86 struct drm_unique *u = data; 87 int domain, bus, slot, func, ret; 88 char *busid; 89 90 /* Check and copy in the submitted Bus ID */ 91 if (!u->unique_len || u->unique_len > 1024) 92 return -EINVAL; 93 94 busid = kmalloc(u->unique_len + 1, M_DRM, M_WAITOK); 95 if (busid == NULL) 96 return -ENOMEM; 97 98 if (copy_from_user(busid, u->unique, u->unique_len)) { 99 kfree(busid); 100 return -EFAULT; 101 } 102 busid[u->unique_len] = '\0'; 103 104 /* Return error if the busid submitted doesn't match the device's actual 105 * busid. 106 */ 107 ret = ksscanf(busid, "PCI:%d:%d:%d", &bus, &slot, &func); 108 if (ret != 3) { 109 kfree(busid); 110 return -EINVAL; 111 } 112 domain = bus >> 8; 113 bus &= 0xff; 114 115 if ((domain != dev->pci_domain) || 116 (bus != dev->pci_bus) || 117 (slot != dev->pci_slot) || 118 (func != dev->pci_func)) { 119 kfree(busid); 120 return -EINVAL; 121 } 122 123 /* Actually set the device's busid now. */ 124 DRM_LOCK(dev); 125 if (dev->unique_len || dev->unique) { 126 DRM_UNLOCK(dev); 127 return -EBUSY; 128 } 129 130 dev->unique_len = u->unique_len; 131 dev->unique = busid; 132 DRM_UNLOCK(dev); 133 134 return 0; 135 } 136 137 static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv) 138 { 139 140 DRM_LOCK(dev); 141 142 dev->unique_len = 20; 143 dev->unique = kmalloc(dev->unique_len + 1, M_DRM, M_WAITOK | M_NULLOK); 144 if (dev->unique == NULL) { 145 DRM_UNLOCK(dev); 146 return -ENOMEM; 147 } 148 149 ksnprintf(dev->unique, dev->unique_len, "pci:%04x:%02x:%02x.%1x", 150 dev->pci_domain, dev->pci_bus, dev->pci_slot, dev->pci_func); 151 152 DRM_UNLOCK(dev); 153 154 return 0; 155 } 156 157 /* 158 * Get a mapping information. 159 * 160 * \param inode device inode. 161 * \param file_priv DRM file private. 162 * \param cmd command. 163 * \param arg user argument, pointing to a drm_map structure. 164 * 165 * \return zero on success or a negative number on failure. 166 * 167 * Searches for the mapping with the specified offset and copies its information 168 * into userspace 169 */ 170 static int drm_getmap(struct drm_device *dev, void *data, 171 struct drm_file *file_priv) 172 { 173 struct drm_map *map = data; 174 struct drm_map_list *r_list = NULL; 175 struct list_head *list; 176 int idx; 177 int i; 178 179 idx = map->offset; 180 if (idx < 0) { 181 return -EINVAL; 182 } 183 184 i = 0; 185 DRM_LOCK(dev); 186 list_for_each(list, &dev->maplist) { 187 if (i == idx) { 188 r_list = list_entry(list, struct drm_map_list, head); 189 break; 190 } 191 i++; 192 } 193 if (!r_list || !r_list->map) { 194 DRM_UNLOCK(dev); 195 return -EINVAL; 196 } 197 198 map->offset = r_list->map->offset; 199 map->size = r_list->map->size; 200 map->type = r_list->map->type; 201 map->flags = r_list->map->flags; 202 map->handle = r_list->map->handle; 203 map->mtrr = r_list->map->mtrr; 204 DRM_UNLOCK(dev); 205 206 return 0; 207 } 208 209 /* 210 * Get client information. 211 * 212 * \param inode device inode. 213 * \param file_priv DRM file private. 214 * \param cmd command. 215 * \param arg user argument, pointing to a drm_client structure. 216 * 217 * \return zero on success or a negative number on failure. 218 * 219 * Searches for the client with the specified index and copies its information 220 * into userspace 221 */ 222 static int drm_getclient(struct drm_device *dev, void *data, 223 struct drm_file *file_priv) 224 { 225 struct drm_client *client = data; 226 struct drm_file *pt; 227 int idx; 228 int i = 0; 229 230 idx = client->idx; 231 DRM_LOCK(dev); 232 list_for_each_entry(pt, &dev->filelist, lhead) { 233 if (i++ >= idx) { 234 client->auth = pt->authenticated; 235 client->pid = pt->pid; 236 client->uid = pt->uid; 237 client->magic = pt->magic; 238 client->iocs = pt->ioctl_count; 239 DRM_UNLOCK(dev); 240 241 return 0; 242 } 243 } 244 DRM_UNLOCK(dev); 245 246 return -EINVAL; 247 } 248 249 /* 250 * Get statistics information. 251 * 252 * \param inode device inode. 253 * \param file_priv DRM file private. 254 * \param cmd command. 255 * \param arg user argument, pointing to a drm_stats structure. 256 * 257 * \return zero on success or a negative number on failure. 258 */ 259 static int drm_getstats(struct drm_device *dev, void *data, 260 struct drm_file *file_priv) 261 { 262 struct drm_stats *stats = data; 263 int i; 264 265 memset(stats, 0, sizeof(struct drm_stats)); 266 267 DRM_LOCK(dev); 268 269 for (i = 0; i < dev->counters; i++) { 270 if (dev->types[i] == _DRM_STAT_LOCK) 271 stats->data[i].value = 272 (dev->lock.hw_lock ? dev->lock.hw_lock->lock : 0); 273 else 274 stats->data[i].value = atomic_read(&dev->counts[i]); 275 stats->data[i].type = dev->types[i]; 276 } 277 278 stats->count = dev->counters; 279 280 DRM_UNLOCK(dev); 281 282 return 0; 283 } 284 285 /* 286 * Get device/driver capabilities 287 */ 288 static int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv) 289 { 290 struct drm_get_cap *req = data; 291 292 req->value = 0; 293 switch (req->capability) { 294 case DRM_CAP_DUMB_BUFFER: 295 if (dev->driver->dumb_create) 296 req->value = 1; 297 break; 298 case DRM_CAP_VBLANK_HIGH_CRTC: 299 req->value = 1; 300 break; 301 case DRM_CAP_DUMB_PREFERRED_DEPTH: 302 req->value = dev->mode_config.preferred_depth; 303 break; 304 case DRM_CAP_DUMB_PREFER_SHADOW: 305 req->value = dev->mode_config.prefer_shadow; 306 break; 307 case DRM_CAP_TIMESTAMP_MONOTONIC: 308 req->value = drm_timestamp_monotonic; 309 break; 310 case DRM_CAP_ASYNC_PAGE_FLIP: 311 req->value = dev->mode_config.async_page_flip; 312 break; 313 case DRM_CAP_CURSOR_WIDTH: 314 if (dev->mode_config.cursor_width) 315 req->value = dev->mode_config.cursor_width; 316 else 317 req->value = 64; 318 break; 319 case DRM_CAP_CURSOR_HEIGHT: 320 if (dev->mode_config.cursor_height) 321 req->value = dev->mode_config.cursor_height; 322 else 323 req->value = 64; 324 break; 325 default: 326 return -EINVAL; 327 } 328 return 0; 329 } 330 331 /* 332 * Set device/driver capabilities 333 */ 334 static int 335 drm_setclientcap(struct drm_device *dev, void *data, struct drm_file *file_priv) 336 { 337 struct drm_set_client_cap *req = data; 338 339 switch (req->capability) { 340 case DRM_CLIENT_CAP_STEREO_3D: 341 if (req->value > 1) 342 return -EINVAL; 343 file_priv->stereo_allowed = req->value; 344 break; 345 case DRM_CLIENT_CAP_UNIVERSAL_PLANES: 346 if (req->value > 1) 347 return -EINVAL; 348 file_priv->universal_planes = req->value; 349 break; 350 case DRM_CLIENT_CAP_ATOMIC: 351 if (!drm_core_check_feature(dev, DRIVER_ATOMIC)) 352 return -EINVAL; 353 if (req->value > 1) 354 return -EINVAL; 355 file_priv->atomic = req->value; 356 file_priv->universal_planes = req->value; 357 break; 358 default: 359 return -EINVAL; 360 } 361 362 return 0; 363 } 364 365 /* 366 * Setversion ioctl. 367 * 368 * \param inode device inode. 369 * \param file_priv DRM file private. 370 * \param cmd command. 371 * \param arg user argument, pointing to a drm_lock structure. 372 * \return zero on success or negative number on failure. 373 * 374 * Sets the requested interface version 375 */ 376 static int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv) 377 { 378 struct drm_set_version *sv = data; 379 struct drm_set_version ver; 380 int if_version, retcode = 0; 381 382 /* Save the incoming data, and set the response before continuing 383 * any further. 384 */ 385 ver = *sv; 386 sv->drm_di_major = DRM_IF_MAJOR; 387 sv->drm_di_minor = DRM_IF_MINOR; 388 sv->drm_dd_major = dev->driver->major; 389 sv->drm_dd_minor = dev->driver->minor; 390 391 if (ver.drm_di_major != -1) { 392 if (ver.drm_di_major != DRM_IF_MAJOR || 393 ver.drm_di_minor < 0 || ver.drm_di_minor > DRM_IF_MINOR) { 394 return -EINVAL; 395 } 396 if_version = DRM_IF_VERSION(ver.drm_di_major, 397 ver.drm_dd_minor); 398 dev->if_version = DRM_MAX(if_version, dev->if_version); 399 if (ver.drm_di_minor >= 1) { 400 /* 401 * Version 1.1 includes tying of DRM to specific device 402 * Version 1.4 has proper PCI domain support 403 */ 404 retcode = drm_set_busid(dev, file_priv); 405 if (retcode) 406 return retcode; 407 } 408 } 409 410 if (ver.drm_dd_major != -1) { 411 if (ver.drm_dd_major != dev->driver->major || 412 ver.drm_dd_minor < 0 || 413 ver.drm_dd_minor > dev->driver->minor) 414 { 415 return -EINVAL; 416 } 417 } 418 419 return 0; 420 } 421 422 /** 423 * drm_noop - DRM no-op ioctl implemntation 424 * @dev: DRM device for the ioctl 425 * @data: data pointer for the ioctl 426 * @file_priv: DRM file for the ioctl call 427 * 428 * This no-op implementation for drm ioctls is useful for deprecated 429 * functionality where we can't return a failure code because existing userspace 430 * checks the result of the ioctl, but doesn't care about the action. 431 * 432 * Always returns successfully with 0. 433 */ 434 int drm_noop(struct drm_device *dev, void *data, 435 struct drm_file *file_priv) 436 { 437 DRM_DEBUG("\n"); 438 return 0; 439 } 440 EXPORT_SYMBOL(drm_noop); 441 442 /** 443 * drm_invalid_op - DRM invalid ioctl implemntation 444 * @dev: DRM device for the ioctl 445 * @data: data pointer for the ioctl 446 * @file_priv: DRM file for the ioctl call 447 * 448 * This no-op implementation for drm ioctls is useful for deprecated 449 * functionality where we really don't want to allow userspace to call the ioctl 450 * any more. This is the case for old ums interfaces for drivers that 451 * transitioned to kms gradually and so kept the old legacy tables around. This 452 * only applies to radeon and i915 kms drivers, other drivers shouldn't need to 453 * use this function. 454 * 455 * Always fails with a return value of -EINVAL. 456 */ 457 int drm_invalid_op(struct drm_device *dev, void *data, 458 struct drm_file *file_priv) 459 { 460 return -EINVAL; 461 } 462 EXPORT_SYMBOL(drm_invalid_op); 463 464 /* 465 * Copy and IOCTL return string to user space 466 */ 467 static int drm_copy_field(char __user *buf, size_t *buf_len, const char *value) 468 { 469 int len; 470 471 /* don't overflow userbuf */ 472 len = strlen(value); 473 if (len > *buf_len) 474 len = *buf_len; 475 476 /* let userspace know exact length of driver value (which could be 477 * larger than the userspace-supplied buffer) */ 478 *buf_len = strlen(value); 479 480 /* finally, try filling in the userbuf */ 481 if (len && buf) 482 if (copy_to_user(buf, value, len)) 483 return -EFAULT; 484 return 0; 485 } 486 487 /* 488 * Get version information 489 * 490 * \param inode device inode. 491 * \param filp file pointer. 492 * \param cmd command. 493 * \param arg user argument, pointing to a drm_version structure. 494 * \return zero on success or negative number on failure. 495 * 496 * Fills in the version information in \p arg. 497 */ 498 static int drm_version(struct drm_device *dev, void *data, 499 struct drm_file *file_priv) 500 { 501 struct drm_version *version = data; 502 int err; 503 504 version->version_major = dev->driver->major; 505 version->version_minor = dev->driver->minor; 506 version->version_patchlevel = dev->driver->patchlevel; 507 err = drm_copy_field(version->name, &version->name_len, 508 dev->driver->name); 509 if (!err) 510 err = drm_copy_field(version->date, &version->date_len, 511 dev->driver->date); 512 if (!err) 513 err = drm_copy_field(version->desc, &version->desc_len, 514 dev->driver->desc); 515 516 return err; 517 } 518 519 /* 520 * drm_ioctl_permit - Check ioctl permissions against caller 521 * 522 * @flags: ioctl permission flags. 523 * @file_priv: Pointer to struct drm_file identifying the caller. 524 * 525 * Checks whether the caller is allowed to run an ioctl with the 526 * indicated permissions. If so, returns zero. Otherwise returns an 527 * error code suitable for ioctl return. 528 */ 529 #if 0 530 int drm_ioctl_permit(u32 flags, struct drm_file *file_priv) 531 { 532 /* ROOT_ONLY is only for CAP_SYS_ADMIN */ 533 if (unlikely((flags & DRM_ROOT_ONLY) && !capable(CAP_SYS_ADMIN))) 534 return -EACCES; 535 536 /* AUTH is only for authenticated or render client */ 537 if (unlikely((flags & DRM_AUTH) && !drm_is_render_client(file_priv) && 538 !file_priv->authenticated)) 539 return -EACCES; 540 541 /* MASTER is only for master or control clients */ 542 if (unlikely((flags & DRM_MASTER) && !file_priv->is_master && 543 !drm_is_control_client(file_priv))) 544 return -EACCES; 545 546 /* Control clients must be explicitly allowed */ 547 if (unlikely(!(flags & DRM_CONTROL_ALLOW) && 548 drm_is_control_client(file_priv))) 549 return -EACCES; 550 551 /* Render clients must be explicitly allowed */ 552 if (unlikely(!(flags & DRM_RENDER_ALLOW) && 553 drm_is_render_client(file_priv))) 554 return -EACCES; 555 556 return 0; 557 } 558 EXPORT_SYMBOL(drm_ioctl_permit); 559 #endif 560 561 #define DRM_IOCTL_DEF(ioctl, _func, _flags) \ 562 [DRM_IOCTL_NR(ioctl)] = { \ 563 .cmd = ioctl, \ 564 .func = _func, \ 565 .flags = _flags, \ 566 .cmd_drv = 0, \ 567 .name = #ioctl \ 568 } 569 570 /* Ioctl table */ 571 static const struct drm_ioctl_desc drm_ioctls[] = { 572 DRM_IOCTL_DEF(DRM_IOCTL_VERSION, drm_version, 573 DRM_UNLOCKED|DRM_RENDER_ALLOW|DRM_CONTROL_ALLOW), 574 DRM_IOCTL_DEF(DRM_IOCTL_GET_UNIQUE, drm_getunique, 0), 575 DRM_IOCTL_DEF(DRM_IOCTL_GET_MAGIC, drm_getmagic, 0), 576 DRM_IOCTL_DEF(DRM_IOCTL_IRQ_BUSID, drm_irq_by_busid, DRM_MASTER|DRM_ROOT_ONLY), 577 DRM_IOCTL_DEF(DRM_IOCTL_GET_MAP, drm_getmap, DRM_UNLOCKED), 578 DRM_IOCTL_DEF(DRM_IOCTL_GET_CLIENT, drm_getclient, DRM_UNLOCKED), 579 DRM_IOCTL_DEF(DRM_IOCTL_GET_STATS, drm_getstats, DRM_UNLOCKED), 580 DRM_IOCTL_DEF(DRM_IOCTL_GET_CAP, drm_getcap, DRM_UNLOCKED|DRM_RENDER_ALLOW), 581 DRM_IOCTL_DEF(DRM_IOCTL_SET_CLIENT_CAP, drm_setclientcap, 0), 582 DRM_IOCTL_DEF(DRM_IOCTL_SET_VERSION, drm_setversion, DRM_MASTER), 583 584 DRM_IOCTL_DEF(DRM_IOCTL_SET_UNIQUE, drm_setunique, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 585 DRM_IOCTL_DEF(DRM_IOCTL_BLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 586 DRM_IOCTL_DEF(DRM_IOCTL_UNBLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 587 DRM_IOCTL_DEF(DRM_IOCTL_AUTH_MAGIC, drm_authmagic, DRM_AUTH|DRM_MASTER), 588 589 DRM_IOCTL_DEF(DRM_IOCTL_ADD_MAP, drm_legacy_addmap_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 590 DRM_IOCTL_DEF(DRM_IOCTL_RM_MAP, drm_legacy_rmmap_ioctl, DRM_AUTH), 591 592 DRM_IOCTL_DEF(DRM_IOCTL_SET_SAREA_CTX, drm_legacy_setsareactx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 593 DRM_IOCTL_DEF(DRM_IOCTL_GET_SAREA_CTX, drm_legacy_getsareactx, DRM_AUTH), 594 595 DRM_IOCTL_DEF(DRM_IOCTL_SET_MASTER, drm_setmaster_ioctl, DRM_ROOT_ONLY), 596 DRM_IOCTL_DEF(DRM_IOCTL_DROP_MASTER, drm_dropmaster_ioctl, DRM_ROOT_ONLY), 597 598 DRM_IOCTL_DEF(DRM_IOCTL_ADD_CTX, drm_legacy_addctx, DRM_AUTH|DRM_ROOT_ONLY), 599 DRM_IOCTL_DEF(DRM_IOCTL_RM_CTX, drm_legacy_rmctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 600 DRM_IOCTL_DEF(DRM_IOCTL_MOD_CTX, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 601 DRM_IOCTL_DEF(DRM_IOCTL_GET_CTX, drm_legacy_getctx, DRM_AUTH), 602 DRM_IOCTL_DEF(DRM_IOCTL_SWITCH_CTX, drm_legacy_switchctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 603 DRM_IOCTL_DEF(DRM_IOCTL_NEW_CTX, drm_legacy_newctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 604 DRM_IOCTL_DEF(DRM_IOCTL_RES_CTX, drm_legacy_resctx, DRM_AUTH), 605 606 DRM_IOCTL_DEF(DRM_IOCTL_ADD_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 607 DRM_IOCTL_DEF(DRM_IOCTL_RM_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 608 609 DRM_IOCTL_DEF(DRM_IOCTL_LOCK, drm_legacy_lock, DRM_AUTH), 610 DRM_IOCTL_DEF(DRM_IOCTL_UNLOCK, drm_legacy_unlock, DRM_AUTH), 611 612 DRM_IOCTL_DEF(DRM_IOCTL_FINISH, drm_noop, DRM_AUTH), 613 614 DRM_IOCTL_DEF(DRM_IOCTL_ADD_BUFS, drm_legacy_addbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 615 DRM_IOCTL_DEF(DRM_IOCTL_MARK_BUFS, drm_legacy_markbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 616 DRM_IOCTL_DEF(DRM_IOCTL_INFO_BUFS, drm_legacy_infobufs, DRM_AUTH), 617 DRM_IOCTL_DEF(DRM_IOCTL_MAP_BUFS, drm_legacy_mapbufs, DRM_AUTH), 618 DRM_IOCTL_DEF(DRM_IOCTL_FREE_BUFS, drm_legacy_freebufs, DRM_AUTH), 619 DRM_IOCTL_DEF(DRM_IOCTL_DMA, drm_legacy_dma_ioctl, DRM_AUTH), 620 621 DRM_IOCTL_DEF(DRM_IOCTL_CONTROL, drm_control, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 622 623 #if IS_ENABLED(CONFIG_AGP) 624 DRM_IOCTL_DEF(DRM_IOCTL_AGP_ACQUIRE, drm_agp_acquire_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 625 DRM_IOCTL_DEF(DRM_IOCTL_AGP_RELEASE, drm_agp_release_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 626 DRM_IOCTL_DEF(DRM_IOCTL_AGP_ENABLE, drm_agp_enable_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 627 DRM_IOCTL_DEF(DRM_IOCTL_AGP_INFO, drm_agp_info_ioctl, DRM_AUTH), 628 DRM_IOCTL_DEF(DRM_IOCTL_AGP_ALLOC, drm_agp_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 629 DRM_IOCTL_DEF(DRM_IOCTL_AGP_FREE, drm_agp_free_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 630 DRM_IOCTL_DEF(DRM_IOCTL_AGP_BIND, drm_agp_bind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 631 DRM_IOCTL_DEF(DRM_IOCTL_AGP_UNBIND, drm_agp_unbind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 632 #endif 633 634 DRM_IOCTL_DEF(DRM_IOCTL_SG_ALLOC, drm_legacy_sg_alloc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 635 DRM_IOCTL_DEF(DRM_IOCTL_SG_FREE, drm_legacy_sg_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 636 637 DRM_IOCTL_DEF(DRM_IOCTL_WAIT_VBLANK, drm_wait_vblank, DRM_UNLOCKED), 638 639 DRM_IOCTL_DEF(DRM_IOCTL_MODESET_CTL, drm_modeset_ctl, 0), 640 641 DRM_IOCTL_DEF(DRM_IOCTL_UPDATE_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 642 643 DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW), 644 DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH|DRM_UNLOCKED), 645 DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH|DRM_UNLOCKED), 646 647 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETRESOURCES, drm_mode_getresources, DRM_CONTROL_ALLOW|DRM_UNLOCKED), 648 649 #if 0 650 DRM_IOCTL_DEF(DRM_IOCTL_PRIME_HANDLE_TO_FD, drm_prime_handle_to_fd_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW), 651 DRM_IOCTL_DEF(DRM_IOCTL_PRIME_FD_TO_HANDLE, drm_prime_fd_to_handle_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW), 652 #endif 653 654 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, DRM_CONTROL_ALLOW|DRM_UNLOCKED), 655 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, DRM_CONTROL_ALLOW|DRM_UNLOCKED), 656 DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 657 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANE, drm_mode_getplane, DRM_CONTROL_ALLOW|DRM_UNLOCKED), 658 DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPLANE, drm_mode_setplane, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 659 DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR, drm_mode_cursor_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 660 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETGAMMA, drm_mode_gamma_get_ioctl, DRM_UNLOCKED), 661 DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETGAMMA, drm_mode_gamma_set_ioctl, DRM_MASTER|DRM_UNLOCKED), 662 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETENCODER, drm_mode_getencoder, DRM_CONTROL_ALLOW|DRM_UNLOCKED), 663 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCONNECTOR, drm_mode_getconnector, DRM_CONTROL_ALLOW|DRM_UNLOCKED), 664 DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATTACHMODE, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 665 DRM_IOCTL_DEF(DRM_IOCTL_MODE_DETACHMODE, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 666 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPERTY, drm_mode_getproperty_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED), 667 DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPROPERTY, drm_mode_connector_property_set_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 668 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPBLOB, drm_mode_getblob_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED), 669 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETFB, drm_mode_getfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED), 670 DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB, drm_mode_addfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED), 671 DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB2, drm_mode_addfb2, DRM_CONTROL_ALLOW|DRM_UNLOCKED), 672 DRM_IOCTL_DEF(DRM_IOCTL_MODE_RMFB, drm_mode_rmfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED), 673 DRM_IOCTL_DEF(DRM_IOCTL_MODE_PAGE_FLIP, drm_mode_page_flip_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 674 DRM_IOCTL_DEF(DRM_IOCTL_MODE_DIRTYFB, drm_mode_dirtyfb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 675 DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_DUMB, drm_mode_create_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED), 676 DRM_IOCTL_DEF(DRM_IOCTL_MODE_MAP_DUMB, drm_mode_mmap_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED), 677 DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROY_DUMB, drm_mode_destroy_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED), 678 DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_GETPROPERTIES, drm_mode_obj_get_properties_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED), 679 DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_SETPROPERTY, drm_mode_obj_set_property_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 680 DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR2, drm_mode_cursor2_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 681 DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATOMIC, drm_mode_atomic_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 682 DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATEPROPBLOB, drm_mode_createblob_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED), 683 DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROYPROPBLOB, drm_mode_destroyblob_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED), 684 }; 685 686 #define DRM_CORE_IOCTL_COUNT ARRAY_SIZE( drm_ioctls ) 687 688 /** 689 * drm_ioctl - ioctl callback implementation for DRM drivers 690 * @filp: file this ioctl is called on 691 * @cmd: ioctl cmd number 692 * @arg: user argument 693 * 694 * Looks up the ioctl function in the ::ioctls table, checking for root 695 * previleges if so required, and dispatches to the respective function. 696 * 697 * Returns: 698 * Zero on success, negative error code on failure. 699 */ 700 int drm_ioctl(struct dev_ioctl_args *ap) 701 { 702 struct cdev *kdev = ap->a_head.a_dev; 703 struct drm_device *dev; 704 const struct drm_ioctl_desc *ioctl = NULL; 705 u_long cmd = ap->a_cmd; 706 unsigned int nr = DRM_IOCTL_NR(cmd); 707 int retcode = 0; 708 caddr_t data = ap->a_data; 709 int (*func)(struct drm_device *dev, void *data, struct drm_file *file_priv); 710 struct drm_file *file_priv; 711 bool is_driver_ioctl; 712 713 dev = drm_get_device_from_kdev(kdev); 714 715 retcode = devfs_get_cdevpriv(ap->a_fp, (void **)&file_priv); 716 if (retcode !=0) { 717 DRM_ERROR("can't find authenticator\n"); 718 return EINVAL; 719 } 720 721 atomic_inc(&dev->counts[_DRM_STAT_IOCTLS]); 722 723 if (drm_device_is_unplugged(dev)) 724 return ENODEV; 725 726 is_driver_ioctl = nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END; 727 728 if (IOCGROUP(cmd) != DRM_IOCTL_BASE) { 729 DRM_DEBUG_FIOCTL("Bad ioctl group 0x%x\n", (int)IOCGROUP(cmd)); 730 return EINVAL; 731 } 732 733 ioctl = &drm_ioctls[nr]; 734 /* It's not a core DRM ioctl, try driver-specific. */ 735 if (ioctl->func == NULL && nr >= DRM_COMMAND_BASE) { 736 /* The array entries begin at DRM_COMMAND_BASE ioctl nr */ 737 nr -= DRM_COMMAND_BASE; 738 if (nr >= dev->driver->num_ioctls) { 739 return EINVAL; 740 } 741 ioctl = &dev->driver->ioctls[nr]; 742 is_driver_ioctl = 1; 743 } 744 745 DRM_DEBUG_IOCTL("pid=%d, dev=0x%lx, auth=%d, %s\n", 746 DRM_CURRENTPID, (long)dev->dev, 747 file_priv->authenticated, ioctl->name); 748 749 /* Do not trust userspace, use our own definition */ 750 func = ioctl->func; 751 752 if (unlikely(!func)) { 753 DRM_DEBUG("no function\n"); 754 retcode = -EINVAL; 755 goto err_i1; 756 } 757 758 if (((ioctl->flags & DRM_ROOT_ONLY) && !capable(CAP_SYS_ADMIN)) || 759 ((ioctl->flags & DRM_AUTH) && !file_priv->authenticated) || 760 ((ioctl->flags & DRM_MASTER) && !file_priv->master)) 761 return EACCES; 762 763 /* Enforce sane locking for kms driver ioctls. Core ioctls are 764 * too messy still. */ 765 if (is_driver_ioctl) { 766 if ((ioctl->flags & DRM_UNLOCKED) == 0) 767 mutex_lock(&drm_global_mutex); 768 /* shared code returns -errno */ 769 retcode = -func(dev, data, file_priv); 770 if (retcode == ERESTARTSYS) 771 retcode = EINTR; 772 if ((ioctl->flags & DRM_UNLOCKED) == 0) 773 mutex_unlock(&drm_global_mutex); 774 } else { 775 retcode = -func(dev, data, file_priv); 776 if (retcode == ERESTARTSYS) 777 retcode = EINTR; 778 } 779 780 err_i1: 781 if (!ioctl) 782 DRM_DEBUG_FIOCTL("invalid ioctl: pid=%d, dev=0x%lx, auth=%d, cmd=0x%02lx, nr=0x%02x\n", 783 DRM_CURRENTPID, 784 (long)dev->dev, 785 file_priv->authenticated, cmd, nr); 786 787 if (retcode) 788 DRM_DEBUG_FIOCTL("ret = %d\n", retcode); 789 return retcode; 790 } 791 EXPORT_SYMBOL(drm_ioctl); 792 793 /** 794 * drm_ioctl_flags - Check for core ioctl and return ioctl permission flags 795 * @nr: ioctl number 796 * @flags: where to return the ioctl permission flags 797 * 798 * This ioctl is only used by the vmwgfx driver to augment the access checks 799 * done by the drm core and insofar a pretty decent layering violation. This 800 * shouldn't be used by any drivers. 801 * 802 * Returns: 803 * True if the @nr corresponds to a DRM core ioctl numer, false otherwise. 804 */ 805 bool drm_ioctl_flags(unsigned int nr, unsigned int *flags) 806 { 807 if ((nr >= DRM_COMMAND_END && nr < DRM_CORE_IOCTL_COUNT) || 808 (nr < DRM_COMMAND_BASE)) { 809 *flags = drm_ioctls[nr].flags; 810 return true; 811 } 812 813 return false; 814 } 815 EXPORT_SYMBOL(drm_ioctl_flags); 816