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