1 /* 2 * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org 3 * 4 * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California. 5 * All Rights Reserved. 6 * 7 * Author Rickard E. (Rik) Faith <faith@valinux.com> 8 * 9 * Permission is hereby granted, free of charge, to any person obtaining a 10 * copy of this software and associated documentation files (the "Software"), 11 * to deal in the Software without restriction, including without limitation 12 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 13 * and/or sell copies of the Software, and to permit persons to whom the 14 * Software is furnished to do so, subject to the following conditions: 15 * 16 * The above copyright notice and this permission notice (including the next 17 * paragraph) shall be included in all copies or substantial portions of the 18 * Software. 19 * 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 * DEALINGS IN THE SOFTWARE. 27 */ 28 29 #include <linux/debugfs.h> 30 #include <linux/fs.h> 31 #include <linux/module.h> 32 #include <linux/moduleparam.h> 33 #include <linux/mount.h> 34 #include <linux/slab.h> 35 36 #include <drm/drm_drv.h> 37 #include <drm/drmP.h> 38 39 #include "drm_crtc_internal.h" 40 #include "drm_legacy.h" 41 #include "drm_internal.h" 42 43 /* 44 * drm_debug: Enable debug output. 45 * Bitmask of DRM_UT_x. See include/drm/drmP.h for details. 46 */ 47 #ifdef __DragonFly__ 48 /* Provides three levels of debug: off, minimal, verbose */ 49 #if DRM_DEBUG_DEFAULT_ON == 1 50 #define DRM_DEBUGBITS_ON (DRM_UT_CORE | DRM_UT_DRIVER | DRM_UT_KMS | \ 51 DRM_UT_PRIME| DRM_UT_ATOMIC | DRM_UT_FIOCTL) 52 #elif DRM_DEBUG_DEFAULT_ON == 2 53 #define DRM_DEBUGBITS_ON (DRM_UT_CORE | DRM_UT_DRIVER | DRM_UT_KMS | \ 54 DRM_UT_PRIME| DRM_UT_ATOMIC | DRM_UT_FIOCTL | \ 55 DRM_UT_PID | DRM_UT_IOCTL ) 56 #else 57 #define DRM_DEBUGBITS_ON (0x0) 58 #endif 59 unsigned int drm_debug = DRM_DEBUGBITS_ON; /* defaults to 0 */ 60 #else 61 unsigned int drm_debug = 0; 62 #endif /* __DragonFly__ */ 63 EXPORT_SYMBOL(drm_debug); 64 65 MODULE_AUTHOR("Gareth Hughes, Leif Delgass, José Fonseca, Jon Smirl"); 66 MODULE_DESCRIPTION("DRM shared core routines"); 67 MODULE_PARM_DESC(debug, "Enable debug output, where each bit enables a debug category.\n" 68 "\t\tBit 0 (0x01) will enable CORE messages (drm core code)\n" 69 "\t\tBit 1 (0x02) will enable DRIVER messages (drm controller code)\n" 70 "\t\tBit 2 (0x04) will enable KMS messages (modesetting code)\n" 71 "\t\tBit 3 (0x08) will enable PRIME messages (prime code)\n" 72 "\t\tBit 4 (0x10) will enable ATOMIC messages (atomic code)\n" 73 "\t\tBit 5 (0x20) will enable VBL messages (vblank code)\n" 74 "\t\tBit 7 (0x80) will enable LEASE messages (leasing code)"); 75 module_param_named(debug, drm_debug, int, 0600); 76 77 static DEFINE_MUTEX(drm_minor_lock); 78 static struct idr drm_minors_idr; 79 80 /* 81 * If the drm core fails to init for whatever reason, 82 * we should prevent any drivers from registering with it. 83 * It's best to check this at drm_dev_init(), as some drivers 84 * prefer to embed struct drm_device into their own device 85 * structure and call drm_dev_init() themselves. 86 */ 87 static bool drm_core_init_complete = false; 88 89 #if 0 90 static struct dentry *drm_debugfs_root; 91 #endif 92 93 void drm_err(const char *func, const char *format, ...) 94 { 95 va_list args; 96 97 kprintf("error: [" DRM_NAME ":pid%d:%s] *ERROR* ", DRM_CURRENTPID, func); 98 99 va_start(args, format); 100 kvprintf(format, args); 101 va_end(args); 102 } 103 104 void drm_ut_debug_printk(const char *function_name, const char *format, ...) 105 { 106 va_list args; 107 108 if (unlikely(drm_debug & DRM_UT_PID)) { 109 kprintf("[" DRM_NAME ":pid%d:%s] ", 110 DRM_CURRENTPID, function_name); 111 } else { 112 kprintf("[" DRM_NAME ":%s] ", function_name); 113 } 114 115 va_start(args, format); 116 kvprintf(format, args); 117 va_end(args); 118 } 119 120 #define DRM_PRINTK_FMT "[" DRM_NAME ":%s]%s %pV" 121 #define DRM_PRINTK_FMT_DFLY "[" DRM_NAME ":%s]%s " 122 123 void drm_dev_printk(const struct device *dev, const char *level, 124 unsigned int category, const char *function_name, 125 const char *prefix, const char *format, ...) 126 { 127 struct va_format vaf; 128 va_list args; 129 130 if (category != DRM_UT_NONE && !(drm_debug & category)) 131 return; 132 133 va_start(args, format); 134 vaf.fmt = format; 135 vaf.va = &args; 136 137 if (dev) 138 #if 0 139 dev_printk(level, dev, DRM_PRINTK_FMT, function_name, prefix, 140 &vaf); 141 else 142 printk("%s" DRM_PRINTK_FMT, level, function_name, prefix, &vaf); 143 #else 144 { 145 kprintf("drm_dev_printk: "); 146 dev_printk(level, dev, DRM_PRINTK_FMT_DFLY, function_name, prefix); 147 kprintf(vaf.fmt, vaf.va); 148 } else { 149 kprintf("drm_dev_printk: "); 150 printk("%s" DRM_PRINTK_FMT_DFLY, level, function_name, prefix); 151 kprintf(vaf.fmt, vaf.va); 152 } 153 #endif 154 155 va_end(args); 156 } 157 EXPORT_SYMBOL(drm_dev_printk); 158 159 void drm_printk(const char *level, unsigned int category, 160 const char *format, ...) 161 { 162 struct va_format vaf; 163 va_list args; 164 165 if (category != DRM_UT_NONE && !(drm_debug & category)) 166 return; 167 168 va_start(args, format); 169 vaf.fmt = format; 170 vaf.va = &args; 171 172 #if 0 173 printk("%s" "[" DRM_NAME ":%ps]%s %pV", 174 level, __builtin_return_address(0), 175 strcmp(level, KERN_ERR) == 0 ? " *ERROR*" : "", &vaf); 176 #else 177 printk("%s" "[" DRM_NAME ":%p]%s ", 178 level, __builtin_return_address(0), 179 strcmp(level, KERN_ERR) == 0 ? " *ERROR*" : ""); 180 kprintf(vaf.fmt, vaf.va); 181 #endif 182 183 va_end(args); 184 } 185 EXPORT_SYMBOL(drm_printk); 186 187 /* 188 * DRM Minors 189 * A DRM device can provide several char-dev interfaces on the DRM-Major. Each 190 * of them is represented by a drm_minor object. Depending on the capabilities 191 * of the device-driver, different interfaces are registered. 192 * 193 * Minors can be accessed via dev->$minor_name. This pointer is either 194 * NULL or a valid drm_minor pointer and stays valid as long as the device is 195 * valid. This means, DRM minors have the same life-time as the underlying 196 * device. However, this doesn't mean that the minor is active. Minors are 197 * registered and unregistered dynamically according to device-state. 198 */ 199 200 static struct drm_minor **drm_minor_get_slot(struct drm_device *dev, 201 unsigned int type) 202 { 203 switch (type) { 204 case DRM_MINOR_PRIMARY: 205 return &dev->primary; 206 case DRM_MINOR_RENDER: 207 return &dev->render; 208 case DRM_MINOR_CONTROL: 209 return &dev->control; 210 default: 211 return NULL; 212 } 213 } 214 215 static int drm_minor_alloc(struct drm_device *dev, unsigned int type) 216 { 217 struct drm_minor *minor; 218 unsigned long flags; 219 int r; 220 221 minor = kzalloc(sizeof(*minor), GFP_KERNEL); 222 if (!minor) 223 return -ENOMEM; 224 225 minor->type = type; 226 minor->dev = dev; 227 228 idr_preload(GFP_KERNEL); 229 spin_lock_irqsave(&drm_minor_lock, flags); 230 r = idr_alloc(&drm_minors_idr, 231 NULL, 232 64 * type, 233 64 * (type + 1), 234 GFP_NOWAIT); 235 spin_unlock_irqrestore(&drm_minor_lock, flags); 236 idr_preload_end(); 237 238 if (r < 0) 239 goto err_free; 240 241 minor->index = r; 242 243 minor->kdev = drm_sysfs_minor_alloc(minor); 244 if (IS_ERR(minor->kdev)) { 245 r = PTR_ERR(minor->kdev); 246 goto err_index; 247 } 248 249 *drm_minor_get_slot(dev, type) = minor; 250 return 0; 251 252 err_index: 253 spin_lock_irqsave(&drm_minor_lock, flags); 254 idr_remove(&drm_minors_idr, minor->index); 255 spin_unlock_irqrestore(&drm_minor_lock, flags); 256 err_free: 257 kfree(minor); 258 return r; 259 } 260 261 static void drm_minor_free(struct drm_device *dev, unsigned int type) 262 { 263 struct drm_minor **slot, *minor; 264 unsigned long flags; 265 266 slot = drm_minor_get_slot(dev, type); 267 minor = *slot; 268 if (!minor) 269 return; 270 271 #if 0 272 put_device(minor->kdev); 273 #endif 274 275 spin_lock_irqsave(&drm_minor_lock, flags); 276 idr_remove(&drm_minors_idr, minor->index); 277 spin_unlock_irqrestore(&drm_minor_lock, flags); 278 279 kfree(minor); 280 *slot = NULL; 281 } 282 283 static int drm_minor_register(struct drm_device *dev, unsigned int type) 284 { 285 struct drm_minor *minor; 286 unsigned long flags; 287 int ret; 288 289 DRM_DEBUG("\n"); 290 291 minor = *drm_minor_get_slot(dev, type); 292 if (!minor) 293 return 0; 294 295 #if 0 296 ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root); 297 if (ret) { 298 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n"); 299 goto err_debugfs; 300 } 301 302 ret = device_add(minor->kdev); 303 if (ret) 304 goto err_debugfs; 305 #endif 306 307 /* replace NULL with @minor so lookups will succeed from now on */ 308 spin_lock_irqsave(&drm_minor_lock, flags); 309 idr_replace(&drm_minors_idr, minor, minor->index); 310 spin_unlock_irqrestore(&drm_minor_lock, flags); 311 312 DRM_DEBUG("new minor registered %d\n", minor->index); 313 return 0; 314 315 #if 0 316 err_debugfs: 317 drm_debugfs_cleanup(minor); 318 #endif 319 return ret; 320 } 321 322 static void drm_minor_unregister(struct drm_device *dev, unsigned int type) 323 { 324 struct drm_minor *minor; 325 unsigned long flags; 326 327 minor = *drm_minor_get_slot(dev, type); 328 #if 0 329 if (!minor || !device_is_registered(minor->kdev)) 330 #else 331 if (!minor) 332 #endif 333 return; 334 335 /* replace @minor with NULL so lookups will fail from now on */ 336 spin_lock_irqsave(&drm_minor_lock, flags); 337 idr_replace(&drm_minors_idr, NULL, minor->index); 338 spin_unlock_irqrestore(&drm_minor_lock, flags); 339 340 #if 0 341 device_del(minor->kdev); 342 #endif 343 dev_set_drvdata(minor->kdev, NULL); /* safety belt */ 344 drm_debugfs_cleanup(minor); 345 } 346 347 /* 348 * Looks up the given minor-ID and returns the respective DRM-minor object. The 349 * refence-count of the underlying device is increased so you must release this 350 * object with drm_minor_release(). 351 * 352 * As long as you hold this minor, it is guaranteed that the object and the 353 * minor->dev pointer will stay valid! However, the device may get unplugged and 354 * unregistered while you hold the minor. 355 */ 356 struct drm_minor *drm_minor_acquire(unsigned int minor_id) 357 { 358 struct drm_minor *minor; 359 unsigned long flags; 360 361 spin_lock_irqsave(&drm_minor_lock, flags); 362 minor = idr_find(&drm_minors_idr, minor_id); 363 if (minor) 364 drm_dev_get(minor->dev); 365 spin_unlock_irqrestore(&drm_minor_lock, flags); 366 367 if (!minor) { 368 return ERR_PTR(-ENODEV); 369 } else if (drm_dev_is_unplugged(minor->dev)) { 370 drm_dev_put(minor->dev); 371 return ERR_PTR(-ENODEV); 372 } 373 374 return minor; 375 } 376 377 void drm_minor_release(struct drm_minor *minor) 378 { 379 drm_dev_put(minor->dev); 380 } 381 382 /** 383 * DOC: driver instance overview 384 * 385 * A device instance for a drm driver is represented by &struct drm_device. This 386 * is allocated with drm_dev_alloc(), usually from bus-specific ->probe() 387 * callbacks implemented by the driver. The driver then needs to initialize all 388 * the various subsystems for the drm device like memory management, vblank 389 * handling, modesetting support and intial output configuration plus obviously 390 * initialize all the corresponding hardware bits. An important part of this is 391 * also calling drm_dev_set_unique() to set the userspace-visible unique name of 392 * this device instance. Finally when everything is up and running and ready for 393 * userspace the device instance can be published using drm_dev_register(). 394 * 395 * There is also deprecated support for initalizing device instances using 396 * bus-specific helpers and the &drm_driver.load callback. But due to 397 * backwards-compatibility needs the device instance have to be published too 398 * early, which requires unpretty global locking to make safe and is therefore 399 * only support for existing drivers not yet converted to the new scheme. 400 * 401 * When cleaning up a device instance everything needs to be done in reverse: 402 * First unpublish the device instance with drm_dev_unregister(). Then clean up 403 * any other resources allocated at device initialization and drop the driver's 404 * reference to &drm_device using drm_dev_put(). 405 * 406 * Note that the lifetime rules for &drm_device instance has still a lot of 407 * historical baggage. Hence use the reference counting provided by 408 * drm_dev_get() and drm_dev_put() only carefully. 409 * 410 * It is recommended that drivers embed &struct drm_device into their own device 411 * structure, which is supported through drm_dev_init(). 412 */ 413 414 #if 0 415 /** 416 * drm_put_dev - Unregister and release a DRM device 417 * @dev: DRM device 418 * 419 * Called at module unload time or when a PCI device is unplugged. 420 * 421 * Cleans up all DRM device, calling drm_lastclose(). 422 * 423 * Note: Use of this function is deprecated. It will eventually go away 424 * completely. Please use drm_dev_unregister() and drm_dev_put() explicitly 425 * instead to make sure that the device isn't userspace accessible any more 426 * while teardown is in progress, ensuring that userspace can't access an 427 * inconsistent state. 428 */ 429 void drm_put_dev(struct drm_device *dev) 430 { 431 DRM_DEBUG("\n"); 432 433 if (!dev) { 434 DRM_ERROR("cleanup called no dev\n"); 435 return; 436 } 437 438 drm_dev_unregister(dev); 439 drm_dev_put(dev); 440 } 441 EXPORT_SYMBOL(drm_put_dev); 442 443 static void drm_device_set_unplugged(struct drm_device *dev) 444 { 445 smp_wmb(); 446 atomic_set(&dev->unplugged, 1); 447 } 448 449 /** 450 * drm_dev_unplug - unplug a DRM device 451 * @dev: DRM device 452 * 453 * This unplugs a hotpluggable DRM device, which makes it inaccessible to 454 * userspace operations. Entry-points can use drm_dev_is_unplugged(). This 455 * essentially unregisters the device like drm_dev_unregister(), but can be 456 * called while there are still open users of @dev. 457 */ 458 void drm_dev_unplug(struct drm_device *dev) 459 { 460 drm_dev_unregister(dev); 461 462 mutex_lock(&drm_global_mutex); 463 drm_device_set_unplugged(dev); 464 if (dev->open_count == 0) 465 drm_dev_put(dev); 466 mutex_unlock(&drm_global_mutex); 467 } 468 EXPORT_SYMBOL(drm_dev_unplug); 469 470 /* 471 * DRM internal mount 472 * We want to be able to allocate our own "struct address_space" to control 473 * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow 474 * stand-alone address_space objects, so we need an underlying inode. As there 475 * is no way to allocate an independent inode easily, we need a fake internal 476 * VFS mount-point. 477 * 478 * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free() 479 * frees it again. You are allowed to use iget() and iput() to get references to 480 * the inode. But each drm_fs_inode_new() call must be paired with exactly one 481 * drm_fs_inode_free() call (which does not have to be the last iput()). 482 * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it 483 * between multiple inode-users. You could, technically, call 484 * iget() + drm_fs_inode_free() directly after alloc and sometime later do an 485 * iput(), but this way you'd end up with a new vfsmount for each inode. 486 */ 487 488 static int drm_fs_cnt; 489 static struct vfsmount *drm_fs_mnt; 490 491 static const struct dentry_operations drm_fs_dops = { 492 .d_dname = simple_dname, 493 }; 494 495 static const struct super_operations drm_fs_sops = { 496 .statfs = simple_statfs, 497 }; 498 499 static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags, 500 const char *dev_name, void *data) 501 { 502 return mount_pseudo(fs_type, 503 "drm:", 504 &drm_fs_sops, 505 &drm_fs_dops, 506 0x010203ff); 507 } 508 509 static struct file_system_type drm_fs_type = { 510 .name = "drm", 511 .owner = THIS_MODULE, 512 .mount = drm_fs_mount, 513 .kill_sb = kill_anon_super, 514 }; 515 516 static struct inode *drm_fs_inode_new(void) 517 { 518 struct inode *inode; 519 int r; 520 521 r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt); 522 if (r < 0) { 523 DRM_ERROR("Cannot mount pseudo fs: %d\n", r); 524 return ERR_PTR(r); 525 } 526 527 inode = alloc_anon_inode(drm_fs_mnt->mnt_sb); 528 if (IS_ERR(inode)) 529 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt); 530 531 return inode; 532 } 533 534 static void drm_fs_inode_free(struct inode *inode) 535 { 536 if (inode) { 537 iput(inode); 538 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt); 539 } 540 } 541 #endif 542 543 /** 544 * drm_dev_init - Initialise new DRM device 545 * @dev: DRM device 546 * @driver: DRM driver 547 * @parent: Parent device object 548 * 549 * Initialize a new DRM device. No device registration is done. 550 * Call drm_dev_register() to advertice the device to user space and register it 551 * with other core subsystems. This should be done last in the device 552 * initialization sequence to make sure userspace can't access an inconsistent 553 * state. 554 * 555 * The initial ref-count of the object is 1. Use drm_dev_get() and 556 * drm_dev_put() to take and drop further ref-counts. 557 * 558 * Note that for purely virtual devices @parent can be NULL. 559 * 560 * Drivers that do not want to allocate their own device struct 561 * embedding &struct drm_device can call drm_dev_alloc() instead. For drivers 562 * that do embed &struct drm_device it must be placed first in the overall 563 * structure, and the overall structure must be allocated using kmalloc(): The 564 * drm core's release function unconditionally calls kfree() on the @dev pointer 565 * when the final reference is released. To override this behaviour, and so 566 * allow embedding of the drm_device inside the driver's device struct at an 567 * arbitrary offset, you must supply a &drm_driver.release callback and control 568 * the finalization explicitly. 569 * 570 * RETURNS: 571 * 0 on success, or error code on failure. 572 */ 573 int drm_dev_init(struct drm_device *dev, 574 struct drm_driver *driver, 575 struct device *parent) 576 { 577 int ret; 578 #ifdef __DragonFly__ 579 struct drm_softc *softc = device_get_softc(parent->bsddev); 580 581 softc->drm_driver_data = dev; 582 #endif 583 584 if (!drm_core_init_complete) { 585 DRM_ERROR("DRM core is not initialized\n"); 586 return -ENODEV; 587 } 588 589 kref_init(&dev->ref); 590 dev->dev = parent; 591 dev->driver = driver; 592 593 INIT_LIST_HEAD(&dev->filelist); 594 INIT_LIST_HEAD(&dev->ctxlist); 595 INIT_LIST_HEAD(&dev->vmalist); 596 INIT_LIST_HEAD(&dev->maplist); 597 INIT_LIST_HEAD(&dev->vblank_event_list); 598 599 lockinit(&dev->buf_lock, "drmdbl", 0, 0); 600 lockinit(&dev->event_lock, "drmev", 0, 0); 601 lockinit(&dev->struct_mutex, "drmslk", 0, LK_CANRECURSE); 602 lockinit(&dev->filelist_mutex, "drmflm", 0, LK_CANRECURSE); 603 lockinit(&dev->ctxlist_mutex, "drmclm", 0, LK_CANRECURSE); 604 lockinit(&dev->master_mutex, "drmmm", 0, LK_CANRECURSE); 605 606 #ifndef __DragonFly__ 607 dev->anon_inode = drm_fs_inode_new(); 608 if (IS_ERR(dev->anon_inode)) { 609 ret = PTR_ERR(dev->anon_inode); 610 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret); 611 goto err_free; 612 } 613 #else 614 dev->anon_inode = NULL; 615 dev->pci_domain = pci_get_domain(dev->dev->bsddev); 616 dev->pci_bus = pci_get_bus(dev->dev->bsddev); 617 dev->pci_slot = pci_get_slot(dev->dev->bsddev); 618 dev->pci_func = pci_get_function(dev->dev->bsddev); 619 drm_sysctl_init(dev); 620 #endif 621 622 if (drm_core_check_feature(dev, DRIVER_RENDER)) { 623 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER); 624 if (ret) 625 goto err_minors; 626 } 627 628 ret = drm_minor_alloc(dev, DRM_MINOR_PRIMARY); 629 if (ret) 630 goto err_minors; 631 632 ret = drm_ht_create(&dev->map_hash, 12); 633 if (ret) 634 goto err_minors; 635 636 drm_legacy_ctxbitmap_init(dev); 637 638 if (drm_core_check_feature(dev, DRIVER_GEM)) { 639 ret = drm_gem_init(dev); 640 if (ret) { 641 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n"); 642 goto err_ctxbitmap; 643 } 644 } 645 646 /* Use the parent device name as DRM device unique identifier, but fall 647 * back to the driver name for virtual devices like vgem. */ 648 #if 0 649 ret = drm_dev_set_unique(dev, parent ? dev_name(parent) : driver->name); 650 if (ret) 651 goto err_setunique; 652 #endif 653 654 return 0; 655 656 #if 0 657 err_setunique: 658 if (drm_core_check_feature(dev, DRIVER_GEM)) 659 drm_gem_destroy(dev); 660 #endif 661 err_ctxbitmap: 662 drm_legacy_ctxbitmap_cleanup(dev); 663 drm_ht_remove(&dev->map_hash); 664 err_minors: 665 drm_minor_free(dev, DRM_MINOR_PRIMARY); 666 drm_minor_free(dev, DRM_MINOR_RENDER); 667 drm_minor_free(dev, DRM_MINOR_CONTROL); 668 #ifndef __DragonFly__ 669 drm_fs_inode_free(dev->anon_inode); 670 err_free: 671 #endif 672 mutex_destroy(&dev->master_mutex); 673 mutex_destroy(&dev->ctxlist_mutex); 674 mutex_destroy(&dev->filelist_mutex); 675 mutex_destroy(&dev->struct_mutex); 676 #ifdef __DragonFly__ 677 drm_sysctl_cleanup(dev); 678 #endif 679 return ret; 680 } 681 EXPORT_SYMBOL(drm_dev_init); 682 683 /** 684 * drm_dev_fini - Finalize a dead DRM device 685 * @dev: DRM device 686 * 687 * Finalize a dead DRM device. This is the converse to drm_dev_init() and 688 * frees up all data allocated by it. All driver private data should be 689 * finalized first. Note that this function does not free the @dev, that is 690 * left to the caller. 691 * 692 * The ref-count of @dev must be zero, and drm_dev_fini() should only be called 693 * from a &drm_driver.release callback. 694 */ 695 void drm_dev_fini(struct drm_device *dev) 696 { 697 drm_vblank_cleanup(dev); 698 699 if (drm_core_check_feature(dev, DRIVER_GEM)) 700 drm_gem_destroy(dev); 701 702 drm_legacy_ctxbitmap_cleanup(dev); 703 drm_ht_remove(&dev->map_hash); 704 #if 0 705 drm_fs_inode_free(dev->anon_inode); 706 #endif 707 708 drm_minor_free(dev, DRM_MINOR_PRIMARY); 709 drm_minor_free(dev, DRM_MINOR_RENDER); 710 drm_minor_free(dev, DRM_MINOR_CONTROL); 711 712 mutex_destroy(&dev->master_mutex); 713 mutex_destroy(&dev->ctxlist_mutex); 714 mutex_destroy(&dev->filelist_mutex); 715 mutex_destroy(&dev->struct_mutex); 716 kfree(dev->unique); 717 } 718 EXPORT_SYMBOL(drm_dev_fini); 719 720 /** 721 * drm_dev_alloc - Allocate new DRM device 722 * @driver: DRM driver to allocate device for 723 * @parent: Parent device object 724 * 725 * Allocate and initialize a new DRM device. No device registration is done. 726 * Call drm_dev_register() to advertice the device to user space and register it 727 * with other core subsystems. This should be done last in the device 728 * initialization sequence to make sure userspace can't access an inconsistent 729 * state. 730 * 731 * The initial ref-count of the object is 1. Use drm_dev_get() and 732 * drm_dev_put() to take and drop further ref-counts. 733 * 734 * Note that for purely virtual devices @parent can be NULL. 735 * 736 * Drivers that wish to subclass or embed &struct drm_device into their 737 * own struct should look at using drm_dev_init() instead. 738 * 739 * RETURNS: 740 * Pointer to new DRM device, or ERR_PTR on failure. 741 */ 742 struct drm_device *drm_dev_alloc(struct drm_driver *driver, 743 struct device *parent) 744 { 745 struct drm_device *dev; 746 int ret; 747 748 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 749 if (!dev) 750 return ERR_PTR(-ENOMEM); 751 752 ret = drm_dev_init(dev, driver, parent); 753 if (ret) { 754 kfree(dev); 755 return ERR_PTR(ret); 756 } 757 758 return dev; 759 } 760 EXPORT_SYMBOL(drm_dev_alloc); 761 762 static void drm_dev_release(struct kref *ref) 763 { 764 struct drm_device *dev = container_of(ref, struct drm_device, ref); 765 766 if (dev->driver->release) { 767 dev->driver->release(dev); 768 } else { 769 drm_dev_fini(dev); 770 kfree(dev); 771 } 772 } 773 774 /** 775 * drm_dev_get - Take reference of a DRM device 776 * @dev: device to take reference of or NULL 777 * 778 * This increases the ref-count of @dev by one. You *must* already own a 779 * reference when calling this. Use drm_dev_put() to drop this reference 780 * again. 781 * 782 * This function never fails. However, this function does not provide *any* 783 * guarantee whether the device is alive or running. It only provides a 784 * reference to the object and the memory associated with it. 785 */ 786 void drm_dev_get(struct drm_device *dev) 787 { 788 if (dev) 789 kref_get(&dev->ref); 790 } 791 EXPORT_SYMBOL(drm_dev_get); 792 793 /** 794 * drm_dev_put - Drop reference of a DRM device 795 * @dev: device to drop reference of or NULL 796 * 797 * This decreases the ref-count of @dev by one. The device is destroyed if the 798 * ref-count drops to zero. 799 */ 800 void drm_dev_put(struct drm_device *dev) 801 { 802 if (dev) 803 kref_put(&dev->ref, drm_dev_release); 804 } 805 EXPORT_SYMBOL(drm_dev_put); 806 807 /** 808 * drm_dev_unref - Drop reference of a DRM device 809 * @dev: device to drop reference of or NULL 810 * 811 * This is a compatibility alias for drm_dev_put() and should not be used by new 812 * code. 813 */ 814 void drm_dev_unref(struct drm_device *dev) 815 { 816 drm_dev_put(dev); 817 } 818 EXPORT_SYMBOL(drm_dev_unref); 819 820 static int create_compat_control_link(struct drm_device *dev) 821 { 822 struct drm_minor *minor; 823 char *name; 824 int ret; 825 826 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 827 return 0; 828 829 minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY); 830 if (!minor) 831 return 0; 832 833 /* 834 * Some existing userspace out there uses the existing of the controlD* 835 * sysfs files to figure out whether it's a modeset driver. It only does 836 * readdir, hence a symlink is sufficient (and the least confusing 837 * option). Otherwise controlD* is entirely unused. 838 * 839 * Old controlD chardev have been allocated in the range 840 * 64-127. 841 */ 842 name = kasprintf(GFP_KERNEL, "controlD%d", minor->index + 64); 843 if (!name) 844 return -ENOMEM; 845 846 #ifndef __DragonFly__ /* DragonFly's libdrm does not need this */ 847 ret = sysfs_create_link(minor->kdev->kobj.parent, 848 &minor->kdev->kobj, 849 name); 850 #else 851 ret = 0; 852 #endif 853 854 kfree(name); 855 856 return ret; 857 } 858 859 static void remove_compat_control_link(struct drm_device *dev) 860 { 861 struct drm_minor *minor; 862 char *name; 863 864 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 865 return; 866 867 minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY); 868 if (!minor) 869 return; 870 871 name = kasprintf(GFP_KERNEL, "controlD%d", minor->index); 872 if (!name) 873 return; 874 875 #ifndef __DragonFly__ 876 sysfs_remove_link(minor->kdev->kobj.parent, name); 877 #endif 878 879 kfree(name); 880 } 881 882 /** 883 * drm_dev_register - Register DRM device 884 * @dev: Device to register 885 * @flags: Flags passed to the driver's .load() function 886 * 887 * Register the DRM device @dev with the system, advertise device to user-space 888 * and start normal device operation. @dev must be allocated via drm_dev_alloc() 889 * previously. 890 * 891 * Never call this twice on any device! 892 * 893 * NOTE: To ensure backward compatibility with existing drivers method this 894 * function calls the &drm_driver.load method after registering the device 895 * nodes, creating race conditions. Usage of the &drm_driver.load methods is 896 * therefore deprecated, drivers must perform all initialization before calling 897 * drm_dev_register(). 898 * 899 * RETURNS: 900 * 0 on success, negative error code on failure. 901 */ 902 int drm_dev_register(struct drm_device *dev, unsigned long flags) 903 { 904 struct drm_driver *driver = dev->driver; 905 int ret; 906 907 mutex_lock(&drm_global_mutex); 908 909 ret = drm_minor_register(dev, DRM_MINOR_CONTROL); 910 if (ret) 911 goto err_minors; 912 913 ret = drm_minor_register(dev, DRM_MINOR_RENDER); 914 if (ret) 915 goto err_minors; 916 917 ret = drm_minor_register(dev, DRM_MINOR_PRIMARY); 918 if (ret) 919 goto err_minors; 920 921 ret = create_compat_control_link(dev); 922 if (ret) 923 goto err_minors; 924 925 dev->registered = true; 926 927 if (dev->driver->load) { 928 ret = dev->driver->load(dev, flags); 929 if (ret) 930 goto err_minors; 931 } 932 933 if (drm_core_check_feature(dev, DRIVER_MODESET)) 934 drm_modeset_register_all(dev); 935 936 ret = 0; 937 938 DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n", 939 driver->name, driver->major, driver->minor, 940 driver->patchlevel, driver->date, 941 dev->dev ? dev_name(dev->dev) : "virtual device", 942 dev->primary->index); 943 944 goto out_unlock; 945 946 err_minors: 947 remove_compat_control_link(dev); 948 drm_minor_unregister(dev, DRM_MINOR_PRIMARY); 949 drm_minor_unregister(dev, DRM_MINOR_RENDER); 950 drm_minor_unregister(dev, DRM_MINOR_CONTROL); 951 out_unlock: 952 mutex_unlock(&drm_global_mutex); 953 return ret; 954 } 955 EXPORT_SYMBOL(drm_dev_register); 956 957 /** 958 * drm_dev_unregister - Unregister DRM device 959 * @dev: Device to unregister 960 * 961 * Unregister the DRM device from the system. This does the reverse of 962 * drm_dev_register() but does not deallocate the device. The caller must call 963 * drm_dev_put() to drop their final reference. 964 * 965 * A special form of unregistering for hotpluggable devices is drm_dev_unplug(), 966 * which can be called while there are still open users of @dev. 967 * 968 * This should be called first in the device teardown code to make sure 969 * userspace can't access the device instance any more. 970 */ 971 void drm_dev_unregister(struct drm_device *dev) 972 { 973 struct drm_map_list *r_list, *list_temp; 974 975 if (drm_core_check_feature(dev, DRIVER_LEGACY)) 976 drm_lastclose(dev); 977 978 dev->registered = false; 979 980 if (drm_core_check_feature(dev, DRIVER_MODESET)) 981 drm_modeset_unregister_all(dev); 982 983 if (dev->driver->unload) 984 dev->driver->unload(dev); 985 986 #if 0 987 if (dev->agp) 988 drm_pci_agp_destroy(dev); 989 #endif 990 991 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) 992 drm_legacy_rmmap(dev, r_list->map); 993 994 remove_compat_control_link(dev); 995 drm_minor_unregister(dev, DRM_MINOR_PRIMARY); 996 drm_minor_unregister(dev, DRM_MINOR_RENDER); 997 drm_minor_unregister(dev, DRM_MINOR_CONTROL); 998 } 999 EXPORT_SYMBOL(drm_dev_unregister); 1000 1001 #if 0 1002 /** 1003 * drm_dev_set_unique - Set the unique name of a DRM device 1004 * @dev: device of which to set the unique name 1005 * @name: unique name 1006 * 1007 * Sets the unique name of a DRM device using the specified string. Drivers 1008 * can use this at driver probe time if the unique name of the devices they 1009 * drive is static. 1010 * 1011 * Return: 0 on success or a negative error code on failure. 1012 */ 1013 int drm_dev_set_unique(struct drm_device *dev, const char *name) 1014 { 1015 kfree(dev->unique); 1016 dev->unique = kstrdup(name, GFP_KERNEL); 1017 1018 return dev->unique ? 0 : -ENOMEM; 1019 } 1020 EXPORT_SYMBOL(drm_dev_set_unique); 1021 1022 /* 1023 * DRM Core 1024 * The DRM core module initializes all global DRM objects and makes them 1025 * available to drivers. Once setup, drivers can probe their respective 1026 * devices. 1027 * Currently, core management includes: 1028 * - The "DRM-Global" key/value database 1029 * - Global ID management for connectors 1030 * - DRM major number allocation 1031 * - DRM minor management 1032 * - DRM sysfs class 1033 * - DRM debugfs root 1034 * 1035 * Furthermore, the DRM core provides dynamic char-dev lookups. For each 1036 * interface registered on a DRM device, you can request minor numbers from DRM 1037 * core. DRM core takes care of major-number management and char-dev 1038 * registration. A stub ->open() callback forwards any open() requests to the 1039 * registered minor. 1040 */ 1041 1042 static int drm_stub_open(struct inode *inode, struct file *filp) 1043 { 1044 const struct file_operations *new_fops; 1045 struct drm_minor *minor; 1046 int err; 1047 1048 DRM_DEBUG("\n"); 1049 1050 mutex_lock(&drm_global_mutex); 1051 minor = drm_minor_acquire(iminor(inode)); 1052 if (IS_ERR(minor)) { 1053 err = PTR_ERR(minor); 1054 goto out_unlock; 1055 } 1056 1057 new_fops = fops_get(minor->dev->driver->fops); 1058 if (!new_fops) { 1059 err = -ENODEV; 1060 goto out_release; 1061 } 1062 1063 replace_fops(filp, new_fops); 1064 if (filp->f_op->open) 1065 err = filp->f_op->open(inode, filp); 1066 else 1067 err = 0; 1068 1069 out_release: 1070 drm_minor_release(minor); 1071 out_unlock: 1072 mutex_unlock(&drm_global_mutex); 1073 return err; 1074 } 1075 1076 static const struct file_operations drm_stub_fops = { 1077 .owner = THIS_MODULE, 1078 .open = drm_stub_open, 1079 .llseek = noop_llseek, 1080 }; 1081 #endif 1082 1083 static void drm_core_exit(void) 1084 { 1085 #if 0 1086 unregister_chrdev(DRM_MAJOR, "drm"); 1087 debugfs_remove(drm_debugfs_root); 1088 drm_sysfs_destroy(); 1089 #endif 1090 idr_destroy(&drm_minors_idr); 1091 drm_connector_ida_destroy(); 1092 drm_global_release(); 1093 } 1094 1095 static int __init drm_core_init(void) 1096 { 1097 #if 0 1098 int ret; 1099 #endif 1100 1101 drm_global_init(); 1102 drm_connector_ida_init(); 1103 idr_init(&drm_minors_idr); 1104 1105 #if 0 1106 ret = drm_sysfs_init(); 1107 if (ret < 0) { 1108 DRM_ERROR("Cannot create DRM class: %d\n", ret); 1109 goto error; 1110 } 1111 1112 drm_debugfs_root = debugfs_create_dir("dri", NULL); 1113 if (!drm_debugfs_root) { 1114 ret = -ENOMEM; 1115 DRM_ERROR("Cannot create debugfs-root: %d\n", ret); 1116 goto error; 1117 } 1118 1119 ret = register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops); 1120 if (ret < 0) 1121 goto error; 1122 #endif 1123 1124 drm_core_init_complete = true; 1125 1126 DRM_DEBUG("Initialized\n"); 1127 return 0; 1128 1129 #if 0 1130 error: 1131 drm_core_exit(); 1132 return ret; 1133 #endif 1134 } 1135 1136 module_init(drm_core_init); 1137 module_exit(drm_core_exit); 1138 1139 #include <sys/devfs.h> 1140 1141 #include <linux/export.h> 1142 #include <linux/dmi.h> 1143 #include <drm/drmP.h> 1144 1145 static int 1146 drm_modevent(module_t mod, int type, void *data) 1147 { 1148 1149 switch (type) { 1150 case MOD_LOAD: 1151 TUNABLE_INT_FETCH("drm.debug", &drm_debug); 1152 linux_task_drop_callback = linux_task_drop; 1153 linux_proc_drop_callback = linux_proc_drop; 1154 break; 1155 case MOD_UNLOAD: 1156 linux_task_drop_callback = NULL; 1157 linux_proc_drop_callback = NULL; 1158 break; 1159 } 1160 return (0); 1161 } 1162 1163 static moduledata_t drm_mod = { 1164 "drm", 1165 drm_modevent, 1166 0 1167 }; 1168 DECLARE_MODULE(drm, drm_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 1169 MODULE_VERSION(drm, 1); 1170 MODULE_DEPEND(drm, agp, 1, 1, 1); 1171 MODULE_DEPEND(drm, pci, 1, 1, 1); 1172 MODULE_DEPEND(drm, iicbus, 1, 1, 1); 1173 1174 struct dev_ops drm_cdevsw = { 1175 { "drm", 0, D_TRACKCLOSE | D_MPSAFE }, 1176 .d_open = drm_open, 1177 .d_close = drm_close, 1178 .d_read = drm_read, 1179 .d_ioctl = drm_ioctl, 1180 .d_kqfilter = drm_kqfilter, 1181 .d_mmap = drm_mmap, 1182 .d_mmap_single = drm_mmap_single, 1183 }; 1184 1185 SYSCTL_NODE(_hw, OID_AUTO, drm, CTLFLAG_RW, NULL, "DRM device"); 1186 SYSCTL_INT(_hw_drm, OID_AUTO, debug, CTLFLAG_RW, &drm_debug, 0, 1187 "DRM debugging"); 1188 int drm_vma_debug; 1189 SYSCTL_INT(_hw_drm, OID_AUTO, vma_debug, CTLFLAG_RW, &drm_vma_debug, 0, 1190 "DRM debugging"); 1191 1192 #if 0 1193 int 1194 drm_create_cdevs(device_t kdev) 1195 { 1196 struct drm_device *dev; 1197 int error, unit; 1198 #ifdef __DragonFly__ 1199 struct drm_softc *softc = device_get_softc(kdev); 1200 1201 dev = softc->drm_driver_data; 1202 #endif 1203 unit = device_get_unit(kdev); 1204 1205 dev->devnode = make_dev(&drm_cdevsw, unit, DRM_DEV_UID, DRM_DEV_GID, 1206 DRM_DEV_MODE, "dri/card%d", unit); 1207 error = 0; 1208 if (error == 0) 1209 dev->devnode->si_drv1 = dev; 1210 return (error); 1211 } 1212 #endif 1213 1214 #ifndef DRM_DEV_NAME 1215 #define DRM_DEV_NAME "drm" 1216 #endif 1217 1218 devclass_t drm_devclass; 1219 1220 /* XXX: this is supposed to be drm_release() */ 1221 void drm_cdevpriv_dtor(void *cd) 1222 { 1223 struct drm_file *file_priv = cd; 1224 struct drm_device *dev = file_priv->dev; 1225 1226 DRM_DEBUG("open_count = %d\n", dev->open_count); 1227 1228 DRM_LOCK(dev); 1229 1230 if (dev->driver->preclose != NULL) 1231 dev->driver->preclose(dev, file_priv); 1232 1233 /* ======================================================== 1234 * Begin inline drm_release 1235 */ 1236 1237 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n", 1238 DRM_CURRENTPID, (long)dev->dev, dev->open_count); 1239 1240 if (dev->driver->driver_features & DRIVER_GEM) 1241 drm_gem_release(dev, file_priv); 1242 1243 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) 1244 drm_legacy_reclaim_buffers(dev, file_priv); 1245 1246 funsetown(&dev->buf_sigio); 1247 1248 if (dev->driver->postclose != NULL) 1249 dev->driver->postclose(dev, file_priv); 1250 list_del(&file_priv->lhead); 1251 1252 1253 /* ======================================================== 1254 * End inline drm_release 1255 */ 1256 1257 device_unbusy(dev->dev->bsddev); 1258 if (--dev->open_count == 0) { 1259 drm_lastclose(dev); 1260 } 1261 1262 DRM_UNLOCK(dev); 1263 } 1264 1265 int 1266 drm_add_busid_modesetting(struct drm_device *dev, struct sysctl_ctx_list *ctx, 1267 struct sysctl_oid *top) 1268 { 1269 struct sysctl_oid *oid; 1270 1271 ksnprintf(dev->busid_str, sizeof(dev->busid_str), 1272 "pci:%04x:%02x:%02x.%d", dev->pci_domain, dev->pci_bus, 1273 dev->pci_slot, dev->pci_func); 1274 oid = SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(top), OID_AUTO, "busid", 1275 CTLFLAG_RD, dev->busid_str, 0, NULL); 1276 if (oid == NULL) 1277 return (ENOMEM); 1278 1279 return (0); 1280 } 1281 1282 int 1283 drm_mmap_single(struct dev_mmap_single_args *ap) 1284 { 1285 struct drm_device *dev; 1286 struct cdev *kdev = ap->a_head.a_dev; 1287 vm_ooffset_t *offset = ap->a_offset; 1288 vm_size_t size = ap->a_size; 1289 struct vm_object **obj_res = ap->a_object; 1290 int nprot = ap->a_nprot; 1291 1292 dev = drm_get_device_from_kdev(kdev); 1293 if (dev->drm_ttm_bdev != NULL) { 1294 return (ttm_bo_mmap_single(ap->a_fp, dev, 1295 offset, size, obj_res, nprot)); 1296 } else if ((dev->driver->driver_features & DRIVER_GEM) != 0) { 1297 return (drm_gem_mmap_single(dev, offset, size, obj_res, nprot)); 1298 } else { 1299 return (ENODEV); 1300 } 1301 } 1302 1303 #include <linux/dmi.h> 1304 1305 /* 1306 * Check if dmi_system_id structure matches system DMI data 1307 */ 1308 static bool 1309 dmi_found(const struct dmi_system_id *dsi) 1310 { 1311 int i, slot; 1312 bool found = false; 1313 char *sys_vendor, *board_vendor, *product_name, *board_name; 1314 1315 sys_vendor = kgetenv("smbios.system.maker"); 1316 board_vendor = kgetenv("smbios.planar.maker"); 1317 product_name = kgetenv("smbios.system.product"); 1318 board_name = kgetenv("smbios.planar.product"); 1319 1320 for (i = 0; i < NELEM(dsi->matches); i++) { 1321 slot = dsi->matches[i].slot; 1322 switch (slot) { 1323 case DMI_NONE: 1324 break; 1325 case DMI_SYS_VENDOR: 1326 if (sys_vendor != NULL && 1327 !strcmp(sys_vendor, dsi->matches[i].substr)) 1328 break; 1329 else 1330 goto done; 1331 case DMI_BOARD_VENDOR: 1332 if (board_vendor != NULL && 1333 !strcmp(board_vendor, dsi->matches[i].substr)) 1334 break; 1335 else 1336 goto done; 1337 case DMI_PRODUCT_NAME: 1338 if (product_name != NULL && 1339 !strcmp(product_name, dsi->matches[i].substr)) 1340 break; 1341 else 1342 goto done; 1343 case DMI_BOARD_NAME: 1344 if (board_name != NULL && 1345 !strcmp(board_name, dsi->matches[i].substr)) 1346 break; 1347 else 1348 goto done; 1349 default: 1350 goto done; 1351 } 1352 } 1353 found = true; 1354 1355 done: 1356 if (sys_vendor != NULL) 1357 kfreeenv(sys_vendor); 1358 if (board_vendor != NULL) 1359 kfreeenv(board_vendor); 1360 if (product_name != NULL) 1361 kfreeenv(product_name); 1362 if (board_name != NULL) 1363 kfreeenv(board_name); 1364 1365 return found; 1366 } 1367 1368 int dmi_check_system(const struct dmi_system_id *sysid) 1369 { 1370 const struct dmi_system_id *dsi; 1371 int num = 0; 1372 1373 for (dsi = sysid; dsi->matches[0].slot != 0 ; dsi++) { 1374 if (dmi_found(dsi)) { 1375 num++; 1376 if (dsi->callback && dsi->callback(dsi)) 1377 break; 1378 } 1379 } 1380 return (num); 1381 } 1382