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 goto err_debugfs; 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 &drm_driver.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 * It is recommended that drivers embed &struct drm_device into their own device 392 * structure, which is supported through drm_dev_init(). 393 */ 394 395 /** 396 * drm_put_dev - Unregister and release a DRM device 397 * @dev: DRM device 398 * 399 * Called at module unload time or when a PCI device is unplugged. 400 * 401 * Cleans up all DRM device, calling drm_lastclose(). 402 * 403 * Note: Use of this function is deprecated. It will eventually go away 404 * completely. Please use drm_dev_unregister() and drm_dev_unref() explicitly 405 * instead to make sure that the device isn't userspace accessible any more 406 * while teardown is in progress, ensuring that userspace can't access an 407 * inconsistent state. 408 */ 409 void drm_put_dev(struct drm_device *dev) 410 { 411 DRM_DEBUG("\n"); 412 413 if (!dev) { 414 DRM_ERROR("cleanup called no dev\n"); 415 return; 416 } 417 418 drm_dev_unregister(dev); 419 drm_dev_unref(dev); 420 } 421 EXPORT_SYMBOL(drm_put_dev); 422 423 void drm_unplug_dev(struct drm_device *dev) 424 { 425 /* for a USB device */ 426 if (drm_core_check_feature(dev, DRIVER_MODESET)) 427 drm_modeset_unregister_all(dev); 428 429 drm_minor_unregister(dev, DRM_MINOR_PRIMARY); 430 drm_minor_unregister(dev, DRM_MINOR_RENDER); 431 drm_minor_unregister(dev, DRM_MINOR_CONTROL); 432 433 mutex_lock(&drm_global_mutex); 434 435 drm_device_set_unplugged(dev); 436 437 if (dev->open_count == 0) { 438 drm_put_dev(dev); 439 } 440 mutex_unlock(&drm_global_mutex); 441 } 442 EXPORT_SYMBOL(drm_unplug_dev); 443 444 /* 445 * DRM internal mount 446 * We want to be able to allocate our own "struct address_space" to control 447 * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow 448 * stand-alone address_space objects, so we need an underlying inode. As there 449 * is no way to allocate an independent inode easily, we need a fake internal 450 * VFS mount-point. 451 * 452 * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free() 453 * frees it again. You are allowed to use iget() and iput() to get references to 454 * the inode. But each drm_fs_inode_new() call must be paired with exactly one 455 * drm_fs_inode_free() call (which does not have to be the last iput()). 456 * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it 457 * between multiple inode-users. You could, technically, call 458 * iget() + drm_fs_inode_free() directly after alloc and sometime later do an 459 * iput(), but this way you'd end up with a new vfsmount for each inode. 460 */ 461 462 static int drm_fs_cnt; 463 static struct vfsmount *drm_fs_mnt; 464 465 static const struct dentry_operations drm_fs_dops = { 466 .d_dname = simple_dname, 467 }; 468 469 static const struct super_operations drm_fs_sops = { 470 .statfs = simple_statfs, 471 }; 472 473 static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags, 474 const char *dev_name, void *data) 475 { 476 return mount_pseudo(fs_type, 477 "drm:", 478 &drm_fs_sops, 479 &drm_fs_dops, 480 0x010203ff); 481 } 482 483 static struct file_system_type drm_fs_type = { 484 .name = "drm", 485 .owner = THIS_MODULE, 486 .mount = drm_fs_mount, 487 .kill_sb = kill_anon_super, 488 }; 489 490 static struct inode *drm_fs_inode_new(void) 491 { 492 struct inode *inode; 493 int r; 494 495 r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt); 496 if (r < 0) { 497 DRM_ERROR("Cannot mount pseudo fs: %d\n", r); 498 return ERR_PTR(r); 499 } 500 501 inode = alloc_anon_inode(drm_fs_mnt->mnt_sb); 502 if (IS_ERR(inode)) 503 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt); 504 505 return inode; 506 } 507 508 static void drm_fs_inode_free(struct inode *inode) 509 { 510 if (inode) { 511 iput(inode); 512 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt); 513 } 514 } 515 #endif 516 517 /** 518 * drm_dev_init - Initialise new DRM device 519 * @dev: DRM device 520 * @driver: DRM driver 521 * @parent: Parent device object 522 * 523 * Initialize a new DRM device. No device registration is done. 524 * Call drm_dev_register() to advertice the device to user space and register it 525 * with other core subsystems. This should be done last in the device 526 * initialization sequence to make sure userspace can't access an inconsistent 527 * state. 528 * 529 * The initial ref-count of the object is 1. Use drm_dev_ref() and 530 * drm_dev_unref() to take and drop further ref-counts. 531 * 532 * Note that for purely virtual devices @parent can be NULL. 533 * 534 * Drivers that do not want to allocate their own device struct 535 * embedding &struct drm_device can call drm_dev_alloc() instead. For drivers 536 * that do embed &struct drm_device it must be placed first in the overall 537 * structure, and the overall structure must be allocated using kmalloc(): The 538 * drm core's release function unconditionally calls kfree() on the @dev pointer 539 * when the final reference is released. To override this behaviour, and so 540 * allow embedding of the drm_device inside the driver's device struct at an 541 * arbitrary offset, you must supply a &drm_driver.release callback and control 542 * the finalization explicitly. 543 * 544 * RETURNS: 545 * 0 on success, or error code on failure. 546 */ 547 int drm_dev_init(struct drm_device *dev, 548 struct drm_driver *driver, 549 struct device *parent) 550 { 551 int ret; 552 #ifdef __DragonFly__ 553 struct drm_softc *softc = device_get_softc(parent->bsddev); 554 555 softc->drm_driver_data = dev; 556 #endif 557 558 kref_init(&dev->ref); 559 dev->dev = parent; 560 dev->driver = driver; 561 562 INIT_LIST_HEAD(&dev->filelist); 563 INIT_LIST_HEAD(&dev->ctxlist); 564 INIT_LIST_HEAD(&dev->vmalist); 565 INIT_LIST_HEAD(&dev->maplist); 566 INIT_LIST_HEAD(&dev->vblank_event_list); 567 568 lockinit(&dev->buf_lock, "drmdbl", 0, 0); 569 lockinit(&dev->event_lock, "drmev", 0, 0); 570 lockinit(&dev->struct_mutex, "drmslk", 0, LK_CANRECURSE); 571 lockinit(&dev->filelist_mutex, "drmflm", 0, LK_CANRECURSE); 572 lockinit(&dev->ctxlist_mutex, "drmclm", 0, LK_CANRECURSE); 573 lockinit(&dev->master_mutex, "drmmm", 0, LK_CANRECURSE); 574 575 #ifndef __DragonFly__ 576 dev->anon_inode = drm_fs_inode_new(); 577 if (IS_ERR(dev->anon_inode)) { 578 ret = PTR_ERR(dev->anon_inode); 579 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret); 580 goto err_free; 581 } 582 #else 583 dev->anon_inode = NULL; 584 dev->pci_domain = pci_get_domain(dev->dev->bsddev); 585 dev->pci_bus = pci_get_bus(dev->dev->bsddev); 586 dev->pci_slot = pci_get_slot(dev->dev->bsddev); 587 dev->pci_func = pci_get_function(dev->dev->bsddev); 588 lwkt_serialize_init(&dev->irq_lock); 589 drm_sysctl_init(dev); 590 #endif 591 592 if (drm_core_check_feature(dev, DRIVER_RENDER)) { 593 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER); 594 if (ret) 595 goto err_minors; 596 } 597 598 ret = drm_minor_alloc(dev, DRM_MINOR_PRIMARY); 599 if (ret) 600 goto err_minors; 601 602 ret = drm_ht_create(&dev->map_hash, 12); 603 if (ret) 604 goto err_minors; 605 606 drm_legacy_ctxbitmap_init(dev); 607 608 if (drm_core_check_feature(dev, DRIVER_GEM)) { 609 ret = drm_gem_init(dev); 610 if (ret) { 611 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n"); 612 goto err_ctxbitmap; 613 } 614 } 615 616 /* Use the parent device name as DRM device unique identifier, but fall 617 * back to the driver name for virtual devices like vgem. */ 618 #if 0 619 ret = drm_dev_set_unique(dev, parent ? dev_name(parent) : driver->name); 620 if (ret) 621 goto err_setunique; 622 #endif 623 624 return 0; 625 626 #if 0 627 err_setunique: 628 if (drm_core_check_feature(dev, DRIVER_GEM)) 629 drm_gem_destroy(dev); 630 #endif 631 err_ctxbitmap: 632 drm_legacy_ctxbitmap_cleanup(dev); 633 drm_ht_remove(&dev->map_hash); 634 err_minors: 635 drm_minor_free(dev, DRM_MINOR_PRIMARY); 636 drm_minor_free(dev, DRM_MINOR_RENDER); 637 drm_minor_free(dev, DRM_MINOR_CONTROL); 638 #ifndef __DragonFly__ 639 drm_fs_inode_free(dev->anon_inode); 640 err_free: 641 #endif 642 mutex_destroy(&dev->master_mutex); 643 mutex_destroy(&dev->ctxlist_mutex); 644 mutex_destroy(&dev->filelist_mutex); 645 mutex_destroy(&dev->struct_mutex); 646 #ifdef __DragonFly__ 647 drm_sysctl_cleanup(dev); 648 #endif 649 return ret; 650 } 651 EXPORT_SYMBOL(drm_dev_init); 652 653 /** 654 * drm_dev_fini - Finalize a dead DRM device 655 * @dev: DRM device 656 * 657 * Finalize a dead DRM device. This is the converse to drm_dev_init() and 658 * frees up all data allocated by it. All driver private data should be 659 * finalized first. Note that this function does not free the @dev, that is 660 * left to the caller. 661 * 662 * The ref-count of @dev must be zero, and drm_dev_fini() should only be called 663 * from a &drm_driver.release callback. 664 */ 665 void drm_dev_fini(struct drm_device *dev) 666 { 667 drm_vblank_cleanup(dev); 668 669 if (drm_core_check_feature(dev, DRIVER_GEM)) 670 drm_gem_destroy(dev); 671 672 drm_legacy_ctxbitmap_cleanup(dev); 673 drm_ht_remove(&dev->map_hash); 674 #if 0 675 drm_fs_inode_free(dev->anon_inode); 676 #endif 677 678 drm_minor_free(dev, DRM_MINOR_PRIMARY); 679 drm_minor_free(dev, DRM_MINOR_RENDER); 680 drm_minor_free(dev, DRM_MINOR_CONTROL); 681 682 mutex_destroy(&dev->master_mutex); 683 mutex_destroy(&dev->ctxlist_mutex); 684 mutex_destroy(&dev->filelist_mutex); 685 mutex_destroy(&dev->struct_mutex); 686 kfree(dev->unique); 687 } 688 EXPORT_SYMBOL(drm_dev_fini); 689 690 /** 691 * drm_dev_alloc - Allocate new DRM device 692 * @driver: DRM driver to allocate device for 693 * @parent: Parent device object 694 * 695 * Allocate and initialize a new DRM device. No device registration is done. 696 * Call drm_dev_register() to advertice the device to user space and register it 697 * with other core subsystems. This should be done last in the device 698 * initialization sequence to make sure userspace can't access an inconsistent 699 * state. 700 * 701 * The initial ref-count of the object is 1. Use drm_dev_ref() and 702 * drm_dev_unref() to take and drop further ref-counts. 703 * 704 * Note that for purely virtual devices @parent can be NULL. 705 * 706 * Drivers that wish to subclass or embed &struct drm_device into their 707 * own struct should look at using drm_dev_init() instead. 708 * 709 * RETURNS: 710 * Pointer to new DRM device, or ERR_PTR on failure. 711 */ 712 struct drm_device *drm_dev_alloc(struct drm_driver *driver, 713 struct device *parent) 714 { 715 struct drm_device *dev; 716 int ret; 717 718 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 719 if (!dev) 720 return ERR_PTR(-ENOMEM); 721 722 ret = drm_dev_init(dev, driver, parent); 723 if (ret) { 724 kfree(dev); 725 return ERR_PTR(ret); 726 } 727 728 return dev; 729 } 730 EXPORT_SYMBOL(drm_dev_alloc); 731 732 #if 0 733 static void drm_dev_release(struct kref *ref) 734 { 735 struct drm_device *dev = container_of(ref, struct drm_device, ref); 736 737 if (dev->driver->release) { 738 dev->driver->release(dev); 739 } else { 740 drm_dev_fini(dev); 741 kfree(dev); 742 } 743 } 744 #endif 745 746 /** 747 * drm_dev_ref - Take reference of a DRM device 748 * @dev: device to take reference of or NULL 749 * 750 * This increases the ref-count of @dev by one. You *must* already own a 751 * reference when calling this. Use drm_dev_unref() to drop this reference 752 * again. 753 * 754 * This function never fails. However, this function does not provide *any* 755 * guarantee whether the device is alive or running. It only provides a 756 * reference to the object and the memory associated with it. 757 */ 758 void drm_dev_ref(struct drm_device *dev) 759 { 760 if (dev) 761 kref_get(&dev->ref); 762 } 763 EXPORT_SYMBOL(drm_dev_ref); 764 765 /** 766 * drm_dev_unref - Drop reference of a DRM device 767 * @dev: device to drop reference of or NULL 768 * 769 * This decreases the ref-count of @dev by one. The device is destroyed if the 770 * ref-count drops to zero. 771 */ 772 void drm_dev_unref(struct drm_device *dev) 773 { 774 #if 0 775 if (dev) 776 kref_put(&dev->ref, drm_dev_release); 777 #endif 778 } 779 EXPORT_SYMBOL(drm_dev_unref); 780 781 static int create_compat_control_link(struct drm_device *dev) 782 { 783 struct drm_minor *minor; 784 char *name; 785 int ret; 786 787 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 788 return 0; 789 790 minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY); 791 if (!minor) 792 return 0; 793 794 /* 795 * Some existing userspace out there uses the existing of the controlD* 796 * sysfs files to figure out whether it's a modeset driver. It only does 797 * readdir, hence a symlink is sufficient (and the least confusing 798 * option). Otherwise controlD* is entirely unused. 799 * 800 * Old controlD chardev have been allocated in the range 801 * 64-127. 802 */ 803 name = kasprintf(GFP_KERNEL, "controlD%d", minor->index + 64); 804 if (!name) 805 return -ENOMEM; 806 807 #ifndef __DragonFly__ /* DragonFly's libdrm does not need this */ 808 ret = sysfs_create_link(minor->kdev->kobj.parent, 809 &minor->kdev->kobj, 810 name); 811 #else 812 ret = 0; 813 #endif 814 815 kfree(name); 816 817 return ret; 818 } 819 820 static void remove_compat_control_link(struct drm_device *dev) 821 { 822 struct drm_minor *minor; 823 char *name; 824 825 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 826 return; 827 828 minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY); 829 if (!minor) 830 return; 831 832 name = kasprintf(GFP_KERNEL, "controlD%d", minor->index); 833 if (!name) 834 return; 835 836 #ifndef __DragonFly__ 837 sysfs_remove_link(minor->kdev->kobj.parent, name); 838 #endif 839 840 kfree(name); 841 } 842 843 /** 844 * drm_dev_register - Register DRM device 845 * @dev: Device to register 846 * @flags: Flags passed to the driver's .load() function 847 * 848 * Register the DRM device @dev with the system, advertise device to user-space 849 * and start normal device operation. @dev must be allocated via drm_dev_alloc() 850 * previously. 851 * 852 * Never call this twice on any device! 853 * 854 * NOTE: To ensure backward compatibility with existing drivers method this 855 * function calls the &drm_driver.load method after registering the device 856 * nodes, creating race conditions. Usage of the &drm_driver.load methods is 857 * therefore deprecated, drivers must perform all initialization before calling 858 * drm_dev_register(). 859 * 860 * RETURNS: 861 * 0 on success, negative error code on failure. 862 */ 863 int drm_dev_register(struct drm_device *dev, unsigned long flags) 864 { 865 struct drm_driver *driver = dev->driver; 866 int ret; 867 868 mutex_lock(&drm_global_mutex); 869 870 ret = drm_minor_register(dev, DRM_MINOR_CONTROL); 871 if (ret) 872 goto err_minors; 873 874 ret = drm_minor_register(dev, DRM_MINOR_RENDER); 875 if (ret) 876 goto err_minors; 877 878 ret = drm_minor_register(dev, DRM_MINOR_PRIMARY); 879 if (ret) 880 goto err_minors; 881 882 ret = create_compat_control_link(dev); 883 if (ret) 884 goto err_minors; 885 886 dev->registered = true; 887 888 if (dev->driver->load) { 889 ret = dev->driver->load(dev, flags); 890 if (ret) 891 goto err_minors; 892 } 893 894 if (drm_core_check_feature(dev, DRIVER_MODESET)) 895 drm_modeset_register_all(dev); 896 897 #ifdef __DragonFly__ 898 ret = drm_create_cdevs(dev->dev->bsddev); 899 if (ret) 900 goto err_minors; 901 #endif 902 903 ret = 0; 904 905 DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n", 906 driver->name, driver->major, driver->minor, 907 driver->patchlevel, driver->date, 908 dev->dev ? dev_name(dev->dev) : "virtual device", 909 dev->primary->index); 910 911 goto out_unlock; 912 913 err_minors: 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 out_unlock: 919 mutex_unlock(&drm_global_mutex); 920 return ret; 921 } 922 EXPORT_SYMBOL(drm_dev_register); 923 924 /** 925 * drm_dev_unregister - Unregister DRM device 926 * @dev: Device to unregister 927 * 928 * Unregister the DRM device from the system. This does the reverse of 929 * drm_dev_register() but does not deallocate the device. The caller must call 930 * drm_dev_unref() to drop their final reference. 931 * 932 * This should be called first in the device teardown code to make sure 933 * userspace can't access the device instance any more. 934 */ 935 void drm_dev_unregister(struct drm_device *dev) 936 { 937 struct drm_map_list *r_list, *list_temp; 938 939 drm_lastclose(dev); 940 941 dev->registered = false; 942 943 if (drm_core_check_feature(dev, DRIVER_MODESET)) 944 drm_modeset_unregister_all(dev); 945 946 if (dev->driver->unload) 947 dev->driver->unload(dev); 948 949 #if 0 950 if (dev->agp) 951 drm_pci_agp_destroy(dev); 952 #endif 953 954 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) 955 drm_legacy_rmmap(dev, r_list->map); 956 957 remove_compat_control_link(dev); 958 drm_minor_unregister(dev, DRM_MINOR_PRIMARY); 959 drm_minor_unregister(dev, DRM_MINOR_RENDER); 960 drm_minor_unregister(dev, DRM_MINOR_CONTROL); 961 } 962 EXPORT_SYMBOL(drm_dev_unregister); 963 964 #if 0 965 /** 966 * drm_dev_set_unique - Set the unique name of a DRM device 967 * @dev: device of which to set the unique name 968 * @name: unique name 969 * 970 * Sets the unique name of a DRM device using the specified string. Drivers 971 * can use this at driver probe time if the unique name of the devices they 972 * drive is static. 973 * 974 * Return: 0 on success or a negative error code on failure. 975 */ 976 int drm_dev_set_unique(struct drm_device *dev, const char *name) 977 { 978 kfree(dev->unique); 979 dev->unique = kstrdup(name, GFP_KERNEL); 980 981 return dev->unique ? 0 : -ENOMEM; 982 } 983 EXPORT_SYMBOL(drm_dev_set_unique); 984 985 /* 986 * DRM Core 987 * The DRM core module initializes all global DRM objects and makes them 988 * available to drivers. Once setup, drivers can probe their respective 989 * devices. 990 * Currently, core management includes: 991 * - The "DRM-Global" key/value database 992 * - Global ID management for connectors 993 * - DRM major number allocation 994 * - DRM minor management 995 * - DRM sysfs class 996 * - DRM debugfs root 997 * 998 * Furthermore, the DRM core provides dynamic char-dev lookups. For each 999 * interface registered on a DRM device, you can request minor numbers from DRM 1000 * core. DRM core takes care of major-number management and char-dev 1001 * registration. A stub ->open() callback forwards any open() requests to the 1002 * registered minor. 1003 */ 1004 1005 static int drm_stub_open(struct inode *inode, struct file *filp) 1006 { 1007 const struct file_operations *new_fops; 1008 struct drm_minor *minor; 1009 int err; 1010 1011 DRM_DEBUG("\n"); 1012 1013 mutex_lock(&drm_global_mutex); 1014 minor = drm_minor_acquire(iminor(inode)); 1015 if (IS_ERR(minor)) { 1016 err = PTR_ERR(minor); 1017 goto out_unlock; 1018 } 1019 1020 new_fops = fops_get(minor->dev->driver->fops); 1021 if (!new_fops) { 1022 err = -ENODEV; 1023 goto out_release; 1024 } 1025 1026 replace_fops(filp, new_fops); 1027 if (filp->f_op->open) 1028 err = filp->f_op->open(inode, filp); 1029 else 1030 err = 0; 1031 1032 out_release: 1033 drm_minor_release(minor); 1034 out_unlock: 1035 mutex_unlock(&drm_global_mutex); 1036 return err; 1037 } 1038 1039 static const struct file_operations drm_stub_fops = { 1040 .owner = THIS_MODULE, 1041 .open = drm_stub_open, 1042 .llseek = noop_llseek, 1043 }; 1044 #endif 1045 1046 static void drm_core_exit(void) 1047 { 1048 #if 0 1049 unregister_chrdev(DRM_MAJOR, "drm"); 1050 debugfs_remove(drm_debugfs_root); 1051 drm_sysfs_destroy(); 1052 #endif 1053 idr_destroy(&drm_minors_idr); 1054 drm_connector_ida_destroy(); 1055 drm_global_release(); 1056 } 1057 1058 static int __init drm_core_init(void) 1059 { 1060 #if 0 1061 int ret; 1062 #endif 1063 1064 drm_global_init(); 1065 drm_connector_ida_init(); 1066 idr_init(&drm_minors_idr); 1067 1068 #if 0 1069 ret = drm_sysfs_init(); 1070 if (ret < 0) { 1071 DRM_ERROR("Cannot create DRM class: %d\n", ret); 1072 goto error; 1073 } 1074 1075 drm_debugfs_root = debugfs_create_dir("dri", NULL); 1076 if (!drm_debugfs_root) { 1077 ret = -ENOMEM; 1078 DRM_ERROR("Cannot create debugfs-root: %d\n", ret); 1079 goto error; 1080 } 1081 1082 ret = register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops); 1083 if (ret < 0) 1084 goto error; 1085 #endif 1086 1087 DRM_DEBUG("Initialized\n"); 1088 return 0; 1089 1090 #if 0 1091 error: 1092 drm_core_exit(); 1093 return ret; 1094 #endif 1095 } 1096 1097 module_init(drm_core_init); 1098 module_exit(drm_core_exit); 1099 1100 #include <sys/devfs.h> 1101 1102 #include <linux/export.h> 1103 #include <linux/dmi.h> 1104 #include <drm/drmP.h> 1105 1106 static int 1107 drm_modevent(module_t mod, int type, void *data) 1108 { 1109 1110 switch (type) { 1111 case MOD_LOAD: 1112 TUNABLE_INT_FETCH("drm.debug", &drm_debug); 1113 linux_task_drop_callback = linux_task_drop; 1114 linux_proc_drop_callback = linux_proc_drop; 1115 break; 1116 case MOD_UNLOAD: 1117 linux_task_drop_callback = NULL; 1118 linux_proc_drop_callback = NULL; 1119 break; 1120 } 1121 return (0); 1122 } 1123 1124 static moduledata_t drm_mod = { 1125 "drm", 1126 drm_modevent, 1127 0 1128 }; 1129 DECLARE_MODULE(drm, drm_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 1130 MODULE_VERSION(drm, 1); 1131 MODULE_DEPEND(drm, agp, 1, 1, 1); 1132 MODULE_DEPEND(drm, pci, 1, 1, 1); 1133 MODULE_DEPEND(drm, iicbus, 1, 1, 1); 1134 1135 static struct dev_ops drm_cdevsw = { 1136 { "drm", 0, D_TRACKCLOSE | D_MPSAFE }, 1137 .d_open = drm_open, 1138 .d_close = drm_close, 1139 .d_read = drm_read, 1140 .d_ioctl = drm_ioctl, 1141 .d_kqfilter = drm_kqfilter, 1142 .d_mmap = drm_mmap, 1143 .d_mmap_single = drm_mmap_single, 1144 }; 1145 1146 SYSCTL_NODE(_hw, OID_AUTO, drm, CTLFLAG_RW, NULL, "DRM device"); 1147 SYSCTL_INT(_hw_drm, OID_AUTO, debug, CTLFLAG_RW, &drm_debug, 0, 1148 "DRM debugging"); 1149 int drm_vma_debug; 1150 SYSCTL_INT(_hw_drm, OID_AUTO, vma_debug, CTLFLAG_RW, &drm_vma_debug, 0, 1151 "DRM debugging"); 1152 1153 int 1154 drm_create_cdevs(device_t kdev) 1155 { 1156 struct drm_device *dev; 1157 int error, unit; 1158 #ifdef __DragonFly__ 1159 struct drm_softc *softc = device_get_softc(kdev); 1160 1161 dev = softc->drm_driver_data; 1162 #endif 1163 unit = device_get_unit(kdev); 1164 1165 dev->devnode = make_dev(&drm_cdevsw, unit, DRM_DEV_UID, DRM_DEV_GID, 1166 DRM_DEV_MODE, "dri/card%d", unit); 1167 error = 0; 1168 if (error == 0) 1169 dev->devnode->si_drv1 = dev; 1170 return (error); 1171 } 1172 1173 #ifndef DRM_DEV_NAME 1174 #define DRM_DEV_NAME "drm" 1175 #endif 1176 1177 devclass_t drm_devclass; 1178 1179 /* 1180 * Stub is needed for devfs 1181 */ 1182 int drm_close(struct dev_close_args *ap) 1183 { 1184 return 0; 1185 } 1186 1187 /* XXX: this is supposed to be drm_release() */ 1188 void drm_cdevpriv_dtor(void *cd) 1189 { 1190 struct drm_file *file_priv = cd; 1191 struct drm_device *dev = file_priv->dev; 1192 1193 DRM_DEBUG("open_count = %d\n", dev->open_count); 1194 1195 DRM_LOCK(dev); 1196 1197 if (dev->driver->preclose != NULL) 1198 dev->driver->preclose(dev, file_priv); 1199 1200 /* ======================================================== 1201 * Begin inline drm_release 1202 */ 1203 1204 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n", 1205 DRM_CURRENTPID, (long)dev->dev, dev->open_count); 1206 1207 if (dev->driver->driver_features & DRIVER_GEM) 1208 drm_gem_release(dev, file_priv); 1209 1210 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) 1211 drm_legacy_reclaim_buffers(dev, file_priv); 1212 1213 funsetown(&dev->buf_sigio); 1214 1215 if (dev->driver->postclose != NULL) 1216 dev->driver->postclose(dev, file_priv); 1217 list_del(&file_priv->lhead); 1218 1219 1220 /* ======================================================== 1221 * End inline drm_release 1222 */ 1223 1224 device_unbusy(dev->dev->bsddev); 1225 if (--dev->open_count == 0) { 1226 drm_lastclose(dev); 1227 } 1228 1229 DRM_UNLOCK(dev); 1230 } 1231 1232 int 1233 drm_add_busid_modesetting(struct drm_device *dev, struct sysctl_ctx_list *ctx, 1234 struct sysctl_oid *top) 1235 { 1236 struct sysctl_oid *oid; 1237 1238 ksnprintf(dev->busid_str, sizeof(dev->busid_str), 1239 "pci:%04x:%02x:%02x.%d", dev->pci_domain, dev->pci_bus, 1240 dev->pci_slot, dev->pci_func); 1241 oid = SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(top), OID_AUTO, "busid", 1242 CTLFLAG_RD, dev->busid_str, 0, NULL); 1243 if (oid == NULL) 1244 return (ENOMEM); 1245 dev->modesetting = (dev->driver->driver_features & DRIVER_MODESET) != 0; 1246 oid = SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(top), OID_AUTO, 1247 "modesetting", CTLFLAG_RD, &dev->modesetting, 0, NULL); 1248 if (oid == NULL) 1249 return (ENOMEM); 1250 1251 return (0); 1252 } 1253 1254 int 1255 drm_mmap_single(struct dev_mmap_single_args *ap) 1256 { 1257 struct drm_device *dev; 1258 struct cdev *kdev = ap->a_head.a_dev; 1259 vm_ooffset_t *offset = ap->a_offset; 1260 vm_size_t size = ap->a_size; 1261 struct vm_object **obj_res = ap->a_object; 1262 int nprot = ap->a_nprot; 1263 1264 dev = drm_get_device_from_kdev(kdev); 1265 if (dev->drm_ttm_bdev != NULL) { 1266 return (ttm_bo_mmap_single(dev, offset, size, obj_res, nprot)); 1267 } else if ((dev->driver->driver_features & DRIVER_GEM) != 0) { 1268 return (drm_gem_mmap_single(dev, offset, size, obj_res, nprot)); 1269 } else { 1270 return (ENODEV); 1271 } 1272 } 1273 1274 #include <linux/dmi.h> 1275 1276 /* 1277 * Check if dmi_system_id structure matches system DMI data 1278 */ 1279 static bool 1280 dmi_found(const struct dmi_system_id *dsi) 1281 { 1282 int i, slot; 1283 bool found = false; 1284 char *sys_vendor, *board_vendor, *product_name, *board_name; 1285 1286 sys_vendor = kgetenv("smbios.system.maker"); 1287 board_vendor = kgetenv("smbios.planar.maker"); 1288 product_name = kgetenv("smbios.system.product"); 1289 board_name = kgetenv("smbios.planar.product"); 1290 1291 for (i = 0; i < NELEM(dsi->matches); i++) { 1292 slot = dsi->matches[i].slot; 1293 switch (slot) { 1294 case DMI_NONE: 1295 break; 1296 case DMI_SYS_VENDOR: 1297 if (sys_vendor != NULL && 1298 !strcmp(sys_vendor, dsi->matches[i].substr)) 1299 break; 1300 else 1301 goto done; 1302 case DMI_BOARD_VENDOR: 1303 if (board_vendor != NULL && 1304 !strcmp(board_vendor, dsi->matches[i].substr)) 1305 break; 1306 else 1307 goto done; 1308 case DMI_PRODUCT_NAME: 1309 if (product_name != NULL && 1310 !strcmp(product_name, dsi->matches[i].substr)) 1311 break; 1312 else 1313 goto done; 1314 case DMI_BOARD_NAME: 1315 if (board_name != NULL && 1316 !strcmp(board_name, dsi->matches[i].substr)) 1317 break; 1318 else 1319 goto done; 1320 default: 1321 goto done; 1322 } 1323 } 1324 found = true; 1325 1326 done: 1327 if (sys_vendor != NULL) 1328 kfreeenv(sys_vendor); 1329 if (board_vendor != NULL) 1330 kfreeenv(board_vendor); 1331 if (product_name != NULL) 1332 kfreeenv(product_name); 1333 if (board_name != NULL) 1334 kfreeenv(board_name); 1335 1336 return found; 1337 } 1338 1339 int dmi_check_system(const struct dmi_system_id *sysid) 1340 { 1341 const struct dmi_system_id *dsi; 1342 int num = 0; 1343 1344 for (dsi = sysid; dsi->matches[0].slot != 0 ; dsi++) { 1345 if (dmi_found(dsi)) { 1346 num++; 1347 if (dsi->callback && dsi->callback(dsi)) 1348 break; 1349 } 1350 } 1351 return (num); 1352 } 1353