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 <sys/param.h> 30 #include <sys/fcntl.h> 31 #include <sys/specdev.h> 32 #include <sys/vnode.h> 33 34 #include <machine/bus.h> 35 36 #ifdef __HAVE_ACPI 37 #include <dev/acpi/acpidev.h> 38 #include <dev/acpi/acpivar.h> 39 #include <dev/acpi/dsdt.h> 40 #endif 41 42 #include <linux/debugfs.h> 43 #include <linux/fs.h> 44 #include <linux/module.h> 45 #include <linux/moduleparam.h> 46 #include <linux/mount.h> 47 #include <linux/pseudo_fs.h> 48 #include <linux/slab.h> 49 #include <linux/srcu.h> 50 #include <linux/suspend.h> 51 52 #include <drm/drm_accel.h> 53 #include <drm/drm_cache.h> 54 #include <drm/drm_client.h> 55 #include <drm/drm_color_mgmt.h> 56 #include <drm/drm_drv.h> 57 #include <drm/drm_file.h> 58 #include <drm/drm_managed.h> 59 #include <drm/drm_mode_object.h> 60 #include <drm/drm_print.h> 61 #include <drm/drm_privacy_screen_machine.h> 62 63 #include <drm/drm_gem.h> 64 65 #include "drm_crtc_internal.h" 66 #include "drm_internal.h" 67 #include "drm_legacy.h" 68 69 MODULE_AUTHOR("Gareth Hughes, Leif Delgass, José Fonseca, Jon Smirl"); 70 MODULE_DESCRIPTION("DRM shared core routines"); 71 MODULE_LICENSE("GPL and additional rights"); 72 73 static DEFINE_SPINLOCK(drm_minor_lock); 74 static struct idr drm_minors_idr; 75 76 /* 77 * If the drm core fails to init for whatever reason, 78 * we should prevent any drivers from registering with it. 79 * It's best to check this at drm_dev_init(), as some drivers 80 * prefer to embed struct drm_device into their own device 81 * structure and call drm_dev_init() themselves. 82 */ 83 static bool drm_core_init_complete; 84 85 static struct dentry *drm_debugfs_root; 86 87 #ifdef notyet 88 DEFINE_STATIC_SRCU(drm_unplug_srcu); 89 #endif 90 91 /* 92 * Some functions are only called once on init regardless of how many times 93 * drm attaches. In linux this is handled via module_init()/module_exit() 94 */ 95 int drm_refcnt; 96 97 struct drm_softc { 98 struct device sc_dev; 99 struct drm_device *sc_drm; 100 int sc_allocated; 101 }; 102 103 struct drm_attach_args { 104 struct drm_device *drm; 105 const struct drm_driver *driver; 106 char *busid; 107 bus_dma_tag_t dmat; 108 bus_space_tag_t bst; 109 size_t busid_len; 110 int is_agp; 111 struct pci_attach_args *pa; 112 int primary; 113 }; 114 115 void drm_linux_init(void); 116 void drm_linux_exit(void); 117 int drm_linux_acpi_notify(struct aml_node *, int, void *); 118 119 int drm_dequeue_event(struct drm_device *, struct drm_file *, size_t, 120 struct drm_pending_event **); 121 122 int drmprint(void *, const char *); 123 int drmsubmatch(struct device *, void *, void *); 124 const struct pci_device_id * 125 drm_find_description(int, int, const struct pci_device_id *); 126 127 int drm_file_cmp(struct drm_file *, struct drm_file *); 128 SPLAY_PROTOTYPE(drm_file_tree, drm_file, link, drm_file_cmp); 129 130 #define DRMDEVCF_PRIMARY 0 131 #define drmdevcf_primary cf_loc[DRMDEVCF_PRIMARY] /* spec'd as primary? */ 132 #define DRMDEVCF_PRIMARY_UNK -1 133 134 /* 135 * DRM Minors 136 * A DRM device can provide several char-dev interfaces on the DRM-Major. Each 137 * of them is represented by a drm_minor object. Depending on the capabilities 138 * of the device-driver, different interfaces are registered. 139 * 140 * Minors can be accessed via dev->$minor_name. This pointer is either 141 * NULL or a valid drm_minor pointer and stays valid as long as the device is 142 * valid. This means, DRM minors have the same life-time as the underlying 143 * device. However, this doesn't mean that the minor is active. Minors are 144 * registered and unregistered dynamically according to device-state. 145 */ 146 147 static struct drm_minor **drm_minor_get_slot(struct drm_device *dev, 148 enum drm_minor_type type) 149 { 150 switch (type) { 151 case DRM_MINOR_PRIMARY: 152 return &dev->primary; 153 case DRM_MINOR_RENDER: 154 return &dev->render; 155 case DRM_MINOR_ACCEL: 156 return &dev->accel; 157 default: 158 BUG(); 159 } 160 } 161 162 static void drm_minor_alloc_release(struct drm_device *dev, void *data) 163 { 164 struct drm_minor *minor = data; 165 unsigned long flags; 166 167 WARN_ON(dev != minor->dev); 168 169 #ifdef __linux__ 170 put_device(minor->kdev); 171 #endif 172 173 if (minor->type == DRM_MINOR_ACCEL) { 174 accel_minor_remove(minor->index); 175 } else { 176 spin_lock_irqsave(&drm_minor_lock, flags); 177 idr_remove(&drm_minors_idr, minor->index); 178 spin_unlock_irqrestore(&drm_minor_lock, flags); 179 } 180 } 181 182 static int drm_minor_alloc(struct drm_device *dev, enum drm_minor_type type) 183 { 184 struct drm_minor *minor; 185 unsigned long flags; 186 int r; 187 188 minor = drmm_kzalloc(dev, sizeof(*minor), GFP_KERNEL); 189 if (!minor) 190 return -ENOMEM; 191 192 minor->type = type; 193 minor->dev = dev; 194 195 idr_preload(GFP_KERNEL); 196 if (type == DRM_MINOR_ACCEL) { 197 r = accel_minor_alloc(); 198 } else { 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 } 207 idr_preload_end(); 208 209 if (r < 0) 210 return r; 211 212 minor->index = r; 213 214 r = drmm_add_action_or_reset(dev, drm_minor_alloc_release, minor); 215 if (r) 216 return r; 217 218 #ifdef __linux__ 219 minor->kdev = drm_sysfs_minor_alloc(minor); 220 if (IS_ERR(minor->kdev)) 221 return PTR_ERR(minor->kdev); 222 #endif 223 224 *drm_minor_get_slot(dev, type) = minor; 225 return 0; 226 } 227 228 static int drm_minor_register(struct drm_device *dev, enum drm_minor_type type) 229 { 230 struct drm_minor *minor; 231 unsigned long flags; 232 #ifdef __linux__ 233 int ret; 234 #endif 235 236 DRM_DEBUG("\n"); 237 238 minor = *drm_minor_get_slot(dev, type); 239 if (!minor) 240 return 0; 241 242 #ifdef __linux__ 243 if (minor->type == DRM_MINOR_ACCEL) { 244 accel_debugfs_init(minor, minor->index); 245 } else { 246 ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root); 247 if (ret) { 248 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n"); 249 goto err_debugfs; 250 } 251 } 252 253 ret = device_add(minor->kdev); 254 if (ret) 255 goto err_debugfs; 256 #else 257 drm_debugfs_root = NULL; 258 #endif 259 260 /* replace NULL with @minor so lookups will succeed from now on */ 261 if (minor->type == DRM_MINOR_ACCEL) { 262 accel_minor_replace(minor, minor->index); 263 } else { 264 spin_lock_irqsave(&drm_minor_lock, flags); 265 idr_replace(&drm_minors_idr, minor, minor->index); 266 spin_unlock_irqrestore(&drm_minor_lock, flags); 267 } 268 269 DRM_DEBUG("new minor registered %d\n", minor->index); 270 return 0; 271 272 #ifdef __linux__ 273 err_debugfs: 274 drm_debugfs_cleanup(minor); 275 return ret; 276 #endif 277 } 278 279 static void drm_minor_unregister(struct drm_device *dev, enum drm_minor_type type) 280 { 281 struct drm_minor *minor; 282 unsigned long flags; 283 284 minor = *drm_minor_get_slot(dev, type); 285 #ifdef __linux__ 286 if (!minor || !device_is_registered(minor->kdev)) 287 #else 288 if (!minor) 289 #endif 290 return; 291 292 /* replace @minor with NULL so lookups will fail from now on */ 293 if (minor->type == DRM_MINOR_ACCEL) { 294 accel_minor_replace(NULL, minor->index); 295 } else { 296 spin_lock_irqsave(&drm_minor_lock, flags); 297 idr_replace(&drm_minors_idr, NULL, minor->index); 298 spin_unlock_irqrestore(&drm_minor_lock, flags); 299 } 300 301 #ifdef __linux__ 302 device_del(minor->kdev); 303 #endif 304 dev_set_drvdata(minor->kdev, NULL); /* safety belt */ 305 drm_debugfs_cleanup(minor); 306 } 307 308 /* 309 * Looks up the given minor-ID and returns the respective DRM-minor object. The 310 * refence-count of the underlying device is increased so you must release this 311 * object with drm_minor_release(). 312 * 313 * As long as you hold this minor, it is guaranteed that the object and the 314 * minor->dev pointer will stay valid! However, the device may get unplugged and 315 * unregistered while you hold the minor. 316 */ 317 struct drm_minor *drm_minor_acquire(unsigned int minor_id) 318 { 319 struct drm_minor *minor; 320 unsigned long flags; 321 322 spin_lock_irqsave(&drm_minor_lock, flags); 323 minor = idr_find(&drm_minors_idr, minor_id); 324 if (minor) 325 drm_dev_get(minor->dev); 326 spin_unlock_irqrestore(&drm_minor_lock, flags); 327 328 if (!minor) { 329 return ERR_PTR(-ENODEV); 330 } else if (drm_dev_is_unplugged(minor->dev)) { 331 drm_dev_put(minor->dev); 332 return ERR_PTR(-ENODEV); 333 } 334 335 return minor; 336 } 337 338 void drm_minor_release(struct drm_minor *minor) 339 { 340 drm_dev_put(minor->dev); 341 } 342 343 /** 344 * DOC: driver instance overview 345 * 346 * A device instance for a drm driver is represented by &struct drm_device. This 347 * is allocated and initialized with devm_drm_dev_alloc(), usually from 348 * bus-specific ->probe() callbacks implemented by the driver. The driver then 349 * needs to initialize all the various subsystems for the drm device like memory 350 * management, vblank handling, modesetting support and initial output 351 * configuration plus obviously initialize all the corresponding hardware bits. 352 * Finally when everything is up and running and ready for userspace the device 353 * instance can be published using drm_dev_register(). 354 * 355 * There is also deprecated support for initializing device instances using 356 * bus-specific helpers and the &drm_driver.load callback. But due to 357 * backwards-compatibility needs the device instance have to be published too 358 * early, which requires unpretty global locking to make safe and is therefore 359 * only support for existing drivers not yet converted to the new scheme. 360 * 361 * When cleaning up a device instance everything needs to be done in reverse: 362 * First unpublish the device instance with drm_dev_unregister(). Then clean up 363 * any other resources allocated at device initialization and drop the driver's 364 * reference to &drm_device using drm_dev_put(). 365 * 366 * Note that any allocation or resource which is visible to userspace must be 367 * released only when the final drm_dev_put() is called, and not when the 368 * driver is unbound from the underlying physical struct &device. Best to use 369 * &drm_device managed resources with drmm_add_action(), drmm_kmalloc() and 370 * related functions. 371 * 372 * devres managed resources like devm_kmalloc() can only be used for resources 373 * directly related to the underlying hardware device, and only used in code 374 * paths fully protected by drm_dev_enter() and drm_dev_exit(). 375 * 376 * Display driver example 377 * ~~~~~~~~~~~~~~~~~~~~~~ 378 * 379 * The following example shows a typical structure of a DRM display driver. 380 * The example focus on the probe() function and the other functions that is 381 * almost always present and serves as a demonstration of devm_drm_dev_alloc(). 382 * 383 * .. code-block:: c 384 * 385 * struct driver_device { 386 * struct drm_device drm; 387 * void *userspace_facing; 388 * struct clk *pclk; 389 * }; 390 * 391 * static const struct drm_driver driver_drm_driver = { 392 * [...] 393 * }; 394 * 395 * static int driver_probe(struct platform_device *pdev) 396 * { 397 * struct driver_device *priv; 398 * struct drm_device *drm; 399 * int ret; 400 * 401 * priv = devm_drm_dev_alloc(&pdev->dev, &driver_drm_driver, 402 * struct driver_device, drm); 403 * if (IS_ERR(priv)) 404 * return PTR_ERR(priv); 405 * drm = &priv->drm; 406 * 407 * ret = drmm_mode_config_init(drm); 408 * if (ret) 409 * return ret; 410 * 411 * priv->userspace_facing = drmm_kzalloc(..., GFP_KERNEL); 412 * if (!priv->userspace_facing) 413 * return -ENOMEM; 414 * 415 * priv->pclk = devm_clk_get(dev, "PCLK"); 416 * if (IS_ERR(priv->pclk)) 417 * return PTR_ERR(priv->pclk); 418 * 419 * // Further setup, display pipeline etc 420 * 421 * platform_set_drvdata(pdev, drm); 422 * 423 * drm_mode_config_reset(drm); 424 * 425 * ret = drm_dev_register(drm); 426 * if (ret) 427 * return ret; 428 * 429 * drm_fbdev_generic_setup(drm, 32); 430 * 431 * return 0; 432 * } 433 * 434 * // This function is called before the devm_ resources are released 435 * static int driver_remove(struct platform_device *pdev) 436 * { 437 * struct drm_device *drm = platform_get_drvdata(pdev); 438 * 439 * drm_dev_unregister(drm); 440 * drm_atomic_helper_shutdown(drm) 441 * 442 * return 0; 443 * } 444 * 445 * // This function is called on kernel restart and shutdown 446 * static void driver_shutdown(struct platform_device *pdev) 447 * { 448 * drm_atomic_helper_shutdown(platform_get_drvdata(pdev)); 449 * } 450 * 451 * static int __maybe_unused driver_pm_suspend(struct device *dev) 452 * { 453 * return drm_mode_config_helper_suspend(dev_get_drvdata(dev)); 454 * } 455 * 456 * static int __maybe_unused driver_pm_resume(struct device *dev) 457 * { 458 * drm_mode_config_helper_resume(dev_get_drvdata(dev)); 459 * 460 * return 0; 461 * } 462 * 463 * static const struct dev_pm_ops driver_pm_ops = { 464 * SET_SYSTEM_SLEEP_PM_OPS(driver_pm_suspend, driver_pm_resume) 465 * }; 466 * 467 * static struct platform_driver driver_driver = { 468 * .driver = { 469 * [...] 470 * .pm = &driver_pm_ops, 471 * }, 472 * .probe = driver_probe, 473 * .remove = driver_remove, 474 * .shutdown = driver_shutdown, 475 * }; 476 * module_platform_driver(driver_driver); 477 * 478 * Drivers that want to support device unplugging (USB, DT overlay unload) should 479 * use drm_dev_unplug() instead of drm_dev_unregister(). The driver must protect 480 * regions that is accessing device resources to prevent use after they're 481 * released. This is done using drm_dev_enter() and drm_dev_exit(). There is one 482 * shortcoming however, drm_dev_unplug() marks the drm_device as unplugged before 483 * drm_atomic_helper_shutdown() is called. This means that if the disable code 484 * paths are protected, they will not run on regular driver module unload, 485 * possibly leaving the hardware enabled. 486 */ 487 488 /** 489 * drm_put_dev - Unregister and release a DRM device 490 * @dev: DRM device 491 * 492 * Called at module unload time or when a PCI device is unplugged. 493 * 494 * Cleans up all DRM device, calling drm_lastclose(). 495 * 496 * Note: Use of this function is deprecated. It will eventually go away 497 * completely. Please use drm_dev_unregister() and drm_dev_put() explicitly 498 * instead to make sure that the device isn't userspace accessible any more 499 * while teardown is in progress, ensuring that userspace can't access an 500 * inconsistent state. 501 */ 502 void drm_put_dev(struct drm_device *dev) 503 { 504 DRM_DEBUG("\n"); 505 506 if (!dev) { 507 DRM_ERROR("cleanup called no dev\n"); 508 return; 509 } 510 511 drm_dev_unregister(dev); 512 drm_dev_put(dev); 513 } 514 EXPORT_SYMBOL(drm_put_dev); 515 516 /** 517 * drm_dev_enter - Enter device critical section 518 * @dev: DRM device 519 * @idx: Pointer to index that will be passed to the matching drm_dev_exit() 520 * 521 * This function marks and protects the beginning of a section that should not 522 * be entered after the device has been unplugged. The section end is marked 523 * with drm_dev_exit(). Calls to this function can be nested. 524 * 525 * Returns: 526 * True if it is OK to enter the section, false otherwise. 527 */ 528 bool drm_dev_enter(struct drm_device *dev, int *idx) 529 { 530 #ifdef notyet 531 *idx = srcu_read_lock(&drm_unplug_srcu); 532 533 if (dev->unplugged) { 534 srcu_read_unlock(&drm_unplug_srcu, *idx); 535 return false; 536 } 537 #endif 538 539 return true; 540 } 541 EXPORT_SYMBOL(drm_dev_enter); 542 543 /** 544 * drm_dev_exit - Exit device critical section 545 * @idx: index returned from drm_dev_enter() 546 * 547 * This function marks the end of a section that should not be entered after 548 * the device has been unplugged. 549 */ 550 void drm_dev_exit(int idx) 551 { 552 #ifdef notyet 553 srcu_read_unlock(&drm_unplug_srcu, idx); 554 #endif 555 } 556 EXPORT_SYMBOL(drm_dev_exit); 557 558 /** 559 * drm_dev_unplug - unplug a DRM device 560 * @dev: DRM device 561 * 562 * This unplugs a hotpluggable DRM device, which makes it inaccessible to 563 * userspace operations. Entry-points can use drm_dev_enter() and 564 * drm_dev_exit() to protect device resources in a race free manner. This 565 * essentially unregisters the device like drm_dev_unregister(), but can be 566 * called while there are still open users of @dev. 567 */ 568 void drm_dev_unplug(struct drm_device *dev) 569 { 570 STUB(); 571 #ifdef notyet 572 /* 573 * After synchronizing any critical read section is guaranteed to see 574 * the new value of ->unplugged, and any critical section which might 575 * still have seen the old value of ->unplugged is guaranteed to have 576 * finished. 577 */ 578 dev->unplugged = true; 579 synchronize_srcu(&drm_unplug_srcu); 580 581 drm_dev_unregister(dev); 582 583 /* Clear all CPU mappings pointing to this device */ 584 unmap_mapping_range(dev->anon_inode->i_mapping, 0, 0, 1); 585 #endif 586 } 587 EXPORT_SYMBOL(drm_dev_unplug); 588 589 #ifdef __linux__ 590 /* 591 * DRM internal mount 592 * We want to be able to allocate our own "struct address_space" to control 593 * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow 594 * stand-alone address_space objects, so we need an underlying inode. As there 595 * is no way to allocate an independent inode easily, we need a fake internal 596 * VFS mount-point. 597 * 598 * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free() 599 * frees it again. You are allowed to use iget() and iput() to get references to 600 * the inode. But each drm_fs_inode_new() call must be paired with exactly one 601 * drm_fs_inode_free() call (which does not have to be the last iput()). 602 * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it 603 * between multiple inode-users. You could, technically, call 604 * iget() + drm_fs_inode_free() directly after alloc and sometime later do an 605 * iput(), but this way you'd end up with a new vfsmount for each inode. 606 */ 607 608 static int drm_fs_cnt; 609 static struct vfsmount *drm_fs_mnt; 610 611 static int drm_fs_init_fs_context(struct fs_context *fc) 612 { 613 return init_pseudo(fc, 0x010203ff) ? 0 : -ENOMEM; 614 } 615 616 static struct file_system_type drm_fs_type = { 617 .name = "drm", 618 .owner = THIS_MODULE, 619 .init_fs_context = drm_fs_init_fs_context, 620 .kill_sb = kill_anon_super, 621 }; 622 623 static struct inode *drm_fs_inode_new(void) 624 { 625 struct inode *inode; 626 int r; 627 628 r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt); 629 if (r < 0) { 630 DRM_ERROR("Cannot mount pseudo fs: %d\n", r); 631 return ERR_PTR(r); 632 } 633 634 inode = alloc_anon_inode(drm_fs_mnt->mnt_sb); 635 if (IS_ERR(inode)) 636 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt); 637 638 return inode; 639 } 640 641 static void drm_fs_inode_free(struct inode *inode) 642 { 643 if (inode) { 644 iput(inode); 645 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt); 646 } 647 } 648 649 #endif /* __linux__ */ 650 651 /** 652 * DOC: component helper usage recommendations 653 * 654 * DRM drivers that drive hardware where a logical device consists of a pile of 655 * independent hardware blocks are recommended to use the :ref:`component helper 656 * library<component>`. For consistency and better options for code reuse the 657 * following guidelines apply: 658 * 659 * - The entire device initialization procedure should be run from the 660 * &component_master_ops.master_bind callback, starting with 661 * devm_drm_dev_alloc(), then binding all components with 662 * component_bind_all() and finishing with drm_dev_register(). 663 * 664 * - The opaque pointer passed to all components through component_bind_all() 665 * should point at &struct drm_device of the device instance, not some driver 666 * specific private structure. 667 * 668 * - The component helper fills the niche where further standardization of 669 * interfaces is not practical. When there already is, or will be, a 670 * standardized interface like &drm_bridge or &drm_panel, providing its own 671 * functions to find such components at driver load time, like 672 * drm_of_find_panel_or_bridge(), then the component helper should not be 673 * used. 674 */ 675 676 static void drm_dev_init_release(struct drm_device *dev, void *res) 677 { 678 drm_legacy_ctxbitmap_cleanup(dev); 679 drm_legacy_remove_map_hash(dev); 680 #ifdef __linux__ 681 drm_fs_inode_free(dev->anon_inode); 682 683 put_device(dev->dev); 684 #endif 685 /* Prevent use-after-free in drm_managed_release when debugging is 686 * enabled. Slightly awkward, but can't really be helped. */ 687 dev->dev = NULL; 688 mutex_destroy(&dev->master_mutex); 689 mutex_destroy(&dev->clientlist_mutex); 690 mutex_destroy(&dev->filelist_mutex); 691 mutex_destroy(&dev->struct_mutex); 692 mutex_destroy(&dev->debugfs_mutex); 693 drm_legacy_destroy_members(dev); 694 } 695 696 #ifdef notyet 697 698 static int drm_dev_init(struct drm_device *dev, 699 const struct drm_driver *driver, 700 struct device *parent) 701 { 702 struct inode *inode; 703 int ret; 704 705 if (!drm_core_init_complete) { 706 DRM_ERROR("DRM core is not initialized\n"); 707 return -ENODEV; 708 } 709 710 if (WARN_ON(!parent)) 711 return -EINVAL; 712 713 kref_init(&dev->ref); 714 dev->dev = get_device(parent); 715 dev->driver = driver; 716 717 INIT_LIST_HEAD(&dev->managed.resources); 718 spin_lock_init(&dev->managed.lock); 719 720 /* no per-device feature limits by default */ 721 dev->driver_features = ~0u; 722 723 if (drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL) && 724 (drm_core_check_feature(dev, DRIVER_RENDER) || 725 drm_core_check_feature(dev, DRIVER_MODESET))) { 726 DRM_ERROR("DRM driver can't be both a compute acceleration and graphics driver\n"); 727 return -EINVAL; 728 } 729 730 drm_legacy_init_members(dev); 731 INIT_LIST_HEAD(&dev->filelist); 732 INIT_LIST_HEAD(&dev->filelist_internal); 733 INIT_LIST_HEAD(&dev->clientlist); 734 INIT_LIST_HEAD(&dev->vblank_event_list); 735 INIT_LIST_HEAD(&dev->debugfs_list); 736 737 spin_lock_init(&dev->event_lock); 738 mutex_init(&dev->struct_mutex); 739 mutex_init(&dev->filelist_mutex); 740 mutex_init(&dev->clientlist_mutex); 741 mutex_init(&dev->master_mutex); 742 mutex_init(&dev->debugfs_mutex); 743 744 ret = drmm_add_action_or_reset(dev, drm_dev_init_release, NULL); 745 if (ret) 746 return ret; 747 748 inode = drm_fs_inode_new(); 749 if (IS_ERR(inode)) { 750 ret = PTR_ERR(inode); 751 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret); 752 goto err; 753 } 754 755 dev->anon_inode = inode; 756 757 if (drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL)) { 758 ret = drm_minor_alloc(dev, DRM_MINOR_ACCEL); 759 if (ret) 760 goto err; 761 } else { 762 if (drm_core_check_feature(dev, DRIVER_RENDER)) { 763 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER); 764 if (ret) 765 goto err; 766 } 767 768 ret = drm_minor_alloc(dev, DRM_MINOR_PRIMARY); 769 if (ret) 770 goto err; 771 } 772 773 ret = drm_legacy_create_map_hash(dev); 774 if (ret) 775 goto err; 776 777 drm_legacy_ctxbitmap_init(dev); 778 779 if (drm_core_check_feature(dev, DRIVER_GEM)) { 780 ret = drm_gem_init(dev); 781 if (ret) { 782 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n"); 783 goto err; 784 } 785 } 786 787 dev->unique = drmm_kstrdup(dev, dev_name(parent), GFP_KERNEL); 788 if (!dev->unique) { 789 ret = -ENOMEM; 790 goto err; 791 } 792 793 return 0; 794 795 err: 796 drm_managed_release(dev); 797 798 return ret; 799 } 800 801 static void devm_drm_dev_init_release(void *data) 802 { 803 drm_dev_put(data); 804 } 805 806 static int devm_drm_dev_init(struct device *parent, 807 struct drm_device *dev, 808 const struct drm_driver *driver) 809 { 810 int ret; 811 812 ret = drm_dev_init(dev, driver, parent); 813 if (ret) 814 return ret; 815 816 return devm_add_action_or_reset(parent, 817 devm_drm_dev_init_release, dev); 818 } 819 820 #endif 821 822 void *__devm_drm_dev_alloc(struct device *parent, 823 const struct drm_driver *driver, 824 size_t size, size_t offset) 825 { 826 void *container; 827 struct drm_device *drm; 828 #ifdef notyet 829 int ret; 830 #endif 831 832 container = kzalloc(size, GFP_KERNEL); 833 if (!container) 834 return ERR_PTR(-ENOMEM); 835 836 drm = container + offset; 837 #ifdef notyet 838 ret = devm_drm_dev_init(parent, drm, driver); 839 if (ret) { 840 kfree(container); 841 return ERR_PTR(ret); 842 } 843 drmm_add_final_kfree(drm, container); 844 #endif 845 846 return container; 847 } 848 EXPORT_SYMBOL(__devm_drm_dev_alloc); 849 850 #ifdef notyet 851 852 /** 853 * drm_dev_alloc - Allocate new DRM device 854 * @driver: DRM driver to allocate device for 855 * @parent: Parent device object 856 * 857 * This is the deprecated version of devm_drm_dev_alloc(), which does not support 858 * subclassing through embedding the struct &drm_device in a driver private 859 * structure, and which does not support automatic cleanup through devres. 860 * 861 * RETURNS: 862 * Pointer to new DRM device, or ERR_PTR on failure. 863 */ 864 struct drm_device *drm_dev_alloc(const struct drm_driver *driver, 865 struct device *parent) 866 { 867 struct drm_device *dev; 868 int ret; 869 870 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 871 if (!dev) 872 return ERR_PTR(-ENOMEM); 873 874 ret = drm_dev_init(dev, driver, parent); 875 if (ret) { 876 kfree(dev); 877 return ERR_PTR(ret); 878 } 879 880 drmm_add_final_kfree(dev, dev); 881 882 return dev; 883 } 884 EXPORT_SYMBOL(drm_dev_alloc); 885 886 #endif 887 888 static void drm_dev_release(struct kref *ref) 889 { 890 struct drm_device *dev = container_of(ref, struct drm_device, ref); 891 892 if (dev->driver->release) 893 dev->driver->release(dev); 894 895 drm_managed_release(dev); 896 897 kfree(dev->managed.final_kfree); 898 } 899 900 /** 901 * drm_dev_get - Take reference of a DRM device 902 * @dev: device to take reference of or NULL 903 * 904 * This increases the ref-count of @dev by one. You *must* already own a 905 * reference when calling this. Use drm_dev_put() to drop this reference 906 * again. 907 * 908 * This function never fails. However, this function does not provide *any* 909 * guarantee whether the device is alive or running. It only provides a 910 * reference to the object and the memory associated with it. 911 */ 912 void drm_dev_get(struct drm_device *dev) 913 { 914 if (dev) 915 kref_get(&dev->ref); 916 } 917 EXPORT_SYMBOL(drm_dev_get); 918 919 /** 920 * drm_dev_put - Drop reference of a DRM device 921 * @dev: device to drop reference of or NULL 922 * 923 * This decreases the ref-count of @dev by one. The device is destroyed if the 924 * ref-count drops to zero. 925 */ 926 void drm_dev_put(struct drm_device *dev) 927 { 928 if (dev) 929 kref_put(&dev->ref, drm_dev_release); 930 } 931 EXPORT_SYMBOL(drm_dev_put); 932 933 static int create_compat_control_link(struct drm_device *dev) 934 { 935 struct drm_minor *minor; 936 char *name; 937 int ret; 938 939 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 940 return 0; 941 942 minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY); 943 if (!minor) 944 return 0; 945 946 /* 947 * Some existing userspace out there uses the existing of the controlD* 948 * sysfs files to figure out whether it's a modeset driver. It only does 949 * readdir, hence a symlink is sufficient (and the least confusing 950 * option). Otherwise controlD* is entirely unused. 951 * 952 * Old controlD chardev have been allocated in the range 953 * 64-127. 954 */ 955 name = kasprintf(GFP_KERNEL, "controlD%d", minor->index + 64); 956 if (!name) 957 return -ENOMEM; 958 959 ret = sysfs_create_link(minor->kdev->kobj.parent, 960 &minor->kdev->kobj, 961 name); 962 963 kfree(name); 964 965 return ret; 966 } 967 968 static void remove_compat_control_link(struct drm_device *dev) 969 { 970 struct drm_minor *minor; 971 char *name; 972 973 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 974 return; 975 976 minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY); 977 if (!minor) 978 return; 979 980 name = kasprintf(GFP_KERNEL, "controlD%d", minor->index + 64); 981 if (!name) 982 return; 983 984 sysfs_remove_link(minor->kdev->kobj.parent, name); 985 986 kfree(name); 987 } 988 989 /** 990 * drm_dev_register - Register DRM device 991 * @dev: Device to register 992 * @flags: Flags passed to the driver's .load() function 993 * 994 * Register the DRM device @dev with the system, advertise device to user-space 995 * and start normal device operation. @dev must be initialized via drm_dev_init() 996 * previously. 997 * 998 * Never call this twice on any device! 999 * 1000 * NOTE: To ensure backward compatibility with existing drivers method this 1001 * function calls the &drm_driver.load method after registering the device 1002 * nodes, creating race conditions. Usage of the &drm_driver.load methods is 1003 * therefore deprecated, drivers must perform all initialization before calling 1004 * drm_dev_register(). 1005 * 1006 * RETURNS: 1007 * 0 on success, negative error code on failure. 1008 */ 1009 int drm_dev_register(struct drm_device *dev, unsigned long flags) 1010 { 1011 const struct drm_driver *driver = dev->driver; 1012 int ret; 1013 1014 if (!driver->load) 1015 drm_mode_config_validate(dev); 1016 1017 WARN_ON(!dev->managed.final_kfree); 1018 1019 if (drm_dev_needs_global_mutex(dev)) 1020 mutex_lock(&drm_global_mutex); 1021 1022 ret = drm_minor_register(dev, DRM_MINOR_RENDER); 1023 if (ret) 1024 goto err_minors; 1025 1026 ret = drm_minor_register(dev, DRM_MINOR_PRIMARY); 1027 if (ret) 1028 goto err_minors; 1029 1030 ret = drm_minor_register(dev, DRM_MINOR_ACCEL); 1031 if (ret) 1032 goto err_minors; 1033 1034 ret = create_compat_control_link(dev); 1035 if (ret) 1036 goto err_minors; 1037 1038 dev->registered = true; 1039 1040 if (driver->load) { 1041 ret = driver->load(dev, flags); 1042 if (ret) 1043 goto err_minors; 1044 } 1045 1046 if (drm_core_check_feature(dev, DRIVER_MODESET)) { 1047 ret = drm_modeset_register_all(dev); 1048 if (ret) 1049 goto err_unload; 1050 } 1051 1052 DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n", 1053 driver->name, driver->major, driver->minor, 1054 driver->patchlevel, driver->date, 1055 dev->dev ? dev_name(dev->dev) : "virtual device", 1056 dev->primary ? dev->primary->index : dev->accel->index); 1057 1058 goto out_unlock; 1059 1060 err_unload: 1061 if (dev->driver->unload) 1062 dev->driver->unload(dev); 1063 err_minors: 1064 remove_compat_control_link(dev); 1065 drm_minor_unregister(dev, DRM_MINOR_ACCEL); 1066 drm_minor_unregister(dev, DRM_MINOR_PRIMARY); 1067 drm_minor_unregister(dev, DRM_MINOR_RENDER); 1068 out_unlock: 1069 if (drm_dev_needs_global_mutex(dev)) 1070 mutex_unlock(&drm_global_mutex); 1071 return ret; 1072 } 1073 EXPORT_SYMBOL(drm_dev_register); 1074 1075 /** 1076 * drm_dev_unregister - Unregister DRM device 1077 * @dev: Device to unregister 1078 * 1079 * Unregister the DRM device from the system. This does the reverse of 1080 * drm_dev_register() but does not deallocate the device. The caller must call 1081 * drm_dev_put() to drop their final reference, unless it is managed with devres 1082 * (as devices allocated with devm_drm_dev_alloc() are), in which case there is 1083 * already an unwind action registered. 1084 * 1085 * A special form of unregistering for hotpluggable devices is drm_dev_unplug(), 1086 * which can be called while there are still open users of @dev. 1087 * 1088 * This should be called first in the device teardown code to make sure 1089 * userspace can't access the device instance any more. 1090 */ 1091 void drm_dev_unregister(struct drm_device *dev) 1092 { 1093 if (drm_core_check_feature(dev, DRIVER_LEGACY)) 1094 drm_lastclose(dev); 1095 1096 dev->registered = false; 1097 1098 drm_client_dev_unregister(dev); 1099 1100 if (drm_core_check_feature(dev, DRIVER_MODESET)) 1101 drm_modeset_unregister_all(dev); 1102 1103 if (dev->driver->unload) 1104 dev->driver->unload(dev); 1105 1106 drm_legacy_pci_agp_destroy(dev); 1107 drm_legacy_rmmaps(dev); 1108 1109 remove_compat_control_link(dev); 1110 drm_minor_unregister(dev, DRM_MINOR_ACCEL); 1111 drm_minor_unregister(dev, DRM_MINOR_PRIMARY); 1112 drm_minor_unregister(dev, DRM_MINOR_RENDER); 1113 } 1114 EXPORT_SYMBOL(drm_dev_unregister); 1115 1116 /* 1117 * DRM Core 1118 * The DRM core module initializes all global DRM objects and makes them 1119 * available to drivers. Once setup, drivers can probe their respective 1120 * devices. 1121 * Currently, core management includes: 1122 * - The "DRM-Global" key/value database 1123 * - Global ID management for connectors 1124 * - DRM major number allocation 1125 * - DRM minor management 1126 * - DRM sysfs class 1127 * - DRM debugfs root 1128 * 1129 * Furthermore, the DRM core provides dynamic char-dev lookups. For each 1130 * interface registered on a DRM device, you can request minor numbers from DRM 1131 * core. DRM core takes care of major-number management and char-dev 1132 * registration. A stub ->open() callback forwards any open() requests to the 1133 * registered minor. 1134 */ 1135 1136 #ifdef __linux__ 1137 static int drm_stub_open(struct inode *inode, struct file *filp) 1138 { 1139 const struct file_operations *new_fops; 1140 struct drm_minor *minor; 1141 int err; 1142 1143 DRM_DEBUG("\n"); 1144 1145 minor = drm_minor_acquire(iminor(inode)); 1146 if (IS_ERR(minor)) 1147 return PTR_ERR(minor); 1148 1149 new_fops = fops_get(minor->dev->driver->fops); 1150 if (!new_fops) { 1151 err = -ENODEV; 1152 goto out; 1153 } 1154 1155 replace_fops(filp, new_fops); 1156 if (filp->f_op->open) 1157 err = filp->f_op->open(inode, filp); 1158 else 1159 err = 0; 1160 1161 out: 1162 drm_minor_release(minor); 1163 1164 return err; 1165 } 1166 1167 static const struct file_operations drm_stub_fops = { 1168 .owner = THIS_MODULE, 1169 .open = drm_stub_open, 1170 .llseek = noop_llseek, 1171 }; 1172 #endif /* __linux__ */ 1173 1174 static void drm_core_exit(void) 1175 { 1176 drm_privacy_screen_lookup_exit(); 1177 accel_core_exit(); 1178 #ifdef __linux__ 1179 unregister_chrdev(DRM_MAJOR, "drm"); 1180 debugfs_remove(drm_debugfs_root); 1181 drm_sysfs_destroy(); 1182 #endif 1183 idr_destroy(&drm_minors_idr); 1184 drm_connector_ida_destroy(); 1185 } 1186 1187 static int __init drm_core_init(void) 1188 { 1189 #ifdef __linux__ 1190 int ret; 1191 #endif 1192 1193 drm_connector_ida_init(); 1194 idr_init(&drm_minors_idr); 1195 drm_memcpy_init_early(); 1196 1197 #ifdef __linux__ 1198 ret = drm_sysfs_init(); 1199 if (ret < 0) { 1200 DRM_ERROR("Cannot create DRM class: %d\n", ret); 1201 goto error; 1202 } 1203 1204 drm_debugfs_root = debugfs_create_dir("dri", NULL); 1205 1206 ret = register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops); 1207 if (ret < 0) 1208 goto error; 1209 1210 ret = accel_core_init(); 1211 if (ret < 0) 1212 goto error; 1213 #endif 1214 1215 drm_privacy_screen_lookup_init(); 1216 1217 drm_core_init_complete = true; 1218 1219 DRM_DEBUG("Initialized\n"); 1220 return 0; 1221 #ifdef __linux__ 1222 error: 1223 drm_core_exit(); 1224 return ret; 1225 #endif 1226 } 1227 1228 #ifdef __linux__ 1229 module_init(drm_core_init); 1230 module_exit(drm_core_exit); 1231 #endif 1232 1233 void 1234 drm_attach_platform(struct drm_driver *driver, bus_space_tag_t iot, 1235 bus_dma_tag_t dmat, struct device *dev, struct drm_device *drm) 1236 { 1237 struct drm_attach_args arg; 1238 1239 memset(&arg, 0, sizeof(arg)); 1240 arg.driver = driver; 1241 arg.bst = iot; 1242 arg.dmat = dmat; 1243 arg.drm = drm; 1244 1245 arg.busid = dev->dv_xname; 1246 arg.busid_len = strlen(dev->dv_xname) + 1; 1247 config_found_sm(dev, &arg, drmprint, drmsubmatch); 1248 } 1249 1250 struct drm_device * 1251 drm_attach_pci(const struct drm_driver *driver, struct pci_attach_args *pa, 1252 int is_agp, int primary, struct device *dev, struct drm_device *drm) 1253 { 1254 struct drm_attach_args arg; 1255 struct drm_softc *sc; 1256 1257 arg.drm = drm; 1258 arg.driver = driver; 1259 arg.dmat = pa->pa_dmat; 1260 arg.bst = pa->pa_memt; 1261 arg.is_agp = is_agp; 1262 arg.primary = primary; 1263 arg.pa = pa; 1264 1265 arg.busid_len = 20; 1266 arg.busid = malloc(arg.busid_len + 1, M_DRM, M_NOWAIT); 1267 if (arg.busid == NULL) { 1268 printf("%s: no memory for drm\n", dev->dv_xname); 1269 return (NULL); 1270 } 1271 snprintf(arg.busid, arg.busid_len, "pci:%04x:%02x:%02x.%1x", 1272 pa->pa_domain, pa->pa_bus, pa->pa_device, pa->pa_function); 1273 1274 sc = (struct drm_softc *)config_found_sm(dev, &arg, drmprint, drmsubmatch); 1275 if (sc == NULL) 1276 return NULL; 1277 1278 return sc->sc_drm; 1279 } 1280 1281 int 1282 drmprint(void *aux, const char *pnp) 1283 { 1284 if (pnp != NULL) 1285 printf("drm at %s", pnp); 1286 return (UNCONF); 1287 } 1288 1289 int 1290 drmsubmatch(struct device *parent, void *match, void *aux) 1291 { 1292 extern struct cfdriver drm_cd; 1293 struct cfdata *cf = match; 1294 1295 /* only allow drm to attach */ 1296 if (cf->cf_driver == &drm_cd) 1297 return ((*cf->cf_attach->ca_match)(parent, match, aux)); 1298 return (0); 1299 } 1300 1301 int 1302 drm_pciprobe(struct pci_attach_args *pa, const struct pci_device_id *idlist) 1303 { 1304 const struct pci_device_id *id_entry; 1305 1306 id_entry = drm_find_description(PCI_VENDOR(pa->pa_id), 1307 PCI_PRODUCT(pa->pa_id), idlist); 1308 if (id_entry != NULL) 1309 return 1; 1310 1311 return 0; 1312 } 1313 1314 int 1315 drm_probe(struct device *parent, void *match, void *aux) 1316 { 1317 struct cfdata *cf = match; 1318 struct drm_attach_args *da = aux; 1319 1320 if (cf->drmdevcf_primary != DRMDEVCF_PRIMARY_UNK) { 1321 /* 1322 * If primary-ness of device specified, either match 1323 * exactly (at high priority), or fail. 1324 */ 1325 if (cf->drmdevcf_primary != 0 && da->primary != 0) 1326 return (10); 1327 else 1328 return (0); 1329 } 1330 1331 /* If primary-ness unspecified, it wins. */ 1332 return (1); 1333 } 1334 1335 int drm_buddy_module_init(void); 1336 void drm_buddy_module_exit(void); 1337 1338 void 1339 drm_attach(struct device *parent, struct device *self, void *aux) 1340 { 1341 struct drm_softc *sc = (struct drm_softc *)self; 1342 struct drm_attach_args *da = aux; 1343 struct drm_device *dev = da->drm; 1344 int ret; 1345 1346 if (drm_refcnt == 0) { 1347 drm_linux_init(); 1348 drm_core_init(); 1349 drm_buddy_module_init(); 1350 } 1351 drm_refcnt++; 1352 1353 if (dev == NULL) { 1354 dev = malloc(sizeof(struct drm_device), M_DRM, 1355 M_WAITOK | M_ZERO); 1356 sc->sc_allocated = 1; 1357 } 1358 1359 sc->sc_drm = dev; 1360 1361 kref_init(&dev->ref); 1362 dev->dev = self; 1363 dev->dev_private = parent; 1364 dev->driver = da->driver; 1365 1366 INIT_LIST_HEAD(&dev->managed.resources); 1367 mtx_init(&dev->managed.lock, IPL_TTY); 1368 1369 /* no per-device feature limits by default */ 1370 dev->driver_features = ~0u; 1371 1372 dev->dmat = da->dmat; 1373 dev->bst = da->bst; 1374 dev->unique = da->busid; 1375 1376 if (da->pa) { 1377 struct pci_attach_args *pa = da->pa; 1378 pcireg_t subsys; 1379 1380 subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, 1381 PCI_SUBSYS_ID_REG); 1382 1383 dev->pdev = &dev->_pdev; 1384 dev->pdev->vendor = PCI_VENDOR(pa->pa_id); 1385 dev->pdev->device = PCI_PRODUCT(pa->pa_id); 1386 dev->pdev->subsystem_vendor = PCI_VENDOR(subsys); 1387 dev->pdev->subsystem_device = PCI_PRODUCT(subsys); 1388 dev->pdev->revision = PCI_REVISION(pa->pa_class); 1389 dev->pdev->class = (PCI_CLASS(pa->pa_class) << 16) | 1390 (PCI_SUBCLASS(pa->pa_class) << 8) | 1391 PCI_INTERFACE(pa->pa_class); 1392 1393 dev->pdev->devfn = PCI_DEVFN(pa->pa_device, pa->pa_function); 1394 dev->pdev->bus = &dev->pdev->_bus; 1395 dev->pdev->bus->pc = pa->pa_pc; 1396 dev->pdev->bus->number = pa->pa_bus; 1397 dev->pdev->bus->domain_nr = pa->pa_domain; 1398 dev->pdev->bus->bridgetag = pa->pa_bridgetag; 1399 1400 if (pa->pa_bridgetag != NULL) { 1401 dev->pdev->bus->self = malloc(sizeof(struct pci_dev), 1402 M_DRM, M_WAITOK | M_ZERO); 1403 dev->pdev->bus->self->pc = pa->pa_pc; 1404 dev->pdev->bus->self->tag = *pa->pa_bridgetag; 1405 } 1406 1407 dev->pdev->pc = pa->pa_pc; 1408 dev->pdev->tag = pa->pa_tag; 1409 dev->pdev->pci = (struct pci_softc *)parent->dv_parent; 1410 1411 #ifdef CONFIG_ACPI 1412 dev->pdev->dev.node = acpi_find_pci(pa->pa_pc, pa->pa_tag); 1413 aml_register_notify(dev->pdev->dev.node, NULL, 1414 drm_linux_acpi_notify, NULL, ACPIDEV_NOPOLL); 1415 #endif 1416 } 1417 1418 mtx_init(&dev->quiesce_mtx, IPL_NONE); 1419 mtx_init(&dev->event_lock, IPL_TTY); 1420 rw_init(&dev->struct_mutex, "drmdevlk"); 1421 rw_init(&dev->filelist_mutex, "drmflist"); 1422 rw_init(&dev->clientlist_mutex, "drmclist"); 1423 rw_init(&dev->master_mutex, "drmmast"); 1424 1425 ret = drmm_add_action(dev, drm_dev_init_release, NULL); 1426 if (ret) 1427 goto error; 1428 1429 SPLAY_INIT(&dev->files); 1430 INIT_LIST_HEAD(&dev->filelist_internal); 1431 INIT_LIST_HEAD(&dev->clientlist); 1432 INIT_LIST_HEAD(&dev->vblank_event_list); 1433 1434 if (drm_core_check_feature(dev, DRIVER_RENDER)) { 1435 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER); 1436 if (ret) 1437 goto error; 1438 } 1439 1440 ret = drm_minor_alloc(dev, DRM_MINOR_PRIMARY); 1441 if (ret) 1442 goto error; 1443 1444 #ifdef CONFIG_DRM_LEGACY 1445 if (drm_core_check_feature(dev, DRIVER_USE_AGP)) { 1446 #if IS_ENABLED(CONFIG_AGP) 1447 if (da->is_agp) 1448 dev->agp = drm_agp_init(); 1449 #endif 1450 if (dev->agp != NULL) { 1451 if (drm_mtrr_add(dev->agp->info.ai_aperture_base, 1452 dev->agp->info.ai_aperture_size, DRM_MTRR_WC) == 0) 1453 dev->agp->mtrr = 1; 1454 } 1455 } 1456 #endif 1457 1458 if (dev->driver->gem_size > 0) { 1459 KASSERT(dev->driver->gem_size >= sizeof(struct drm_gem_object)); 1460 /* XXX unique name */ 1461 pool_init(&dev->objpl, dev->driver->gem_size, 0, IPL_NONE, 0, 1462 "drmobjpl", NULL); 1463 } 1464 1465 if (drm_core_check_feature(dev, DRIVER_GEM)) { 1466 ret = drm_gem_init(dev); 1467 if (ret) { 1468 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n"); 1469 goto error; 1470 } 1471 } 1472 1473 drmm_add_final_kfree(dev, dev); 1474 1475 printf("\n"); 1476 return; 1477 1478 error: 1479 drm_managed_release(dev); 1480 dev->dev_private = NULL; 1481 } 1482 1483 int 1484 drm_detach(struct device *self, int flags) 1485 { 1486 struct drm_softc *sc = (struct drm_softc *)self; 1487 struct drm_device *dev = sc->sc_drm; 1488 1489 drm_refcnt--; 1490 if (drm_refcnt == 0) { 1491 drm_buddy_module_exit(); 1492 drm_core_exit(); 1493 drm_linux_exit(); 1494 } 1495 1496 drm_lastclose(dev); 1497 1498 if (drm_core_check_feature(dev, DRIVER_GEM)) { 1499 if (dev->driver->gem_size > 0) 1500 pool_destroy(&dev->objpl); 1501 } 1502 1503 #ifdef CONFIG_DRM_LEGACY 1504 if (dev->agp && dev->agp->mtrr) { 1505 int retcode; 1506 1507 retcode = drm_mtrr_del(0, dev->agp->info.ai_aperture_base, 1508 dev->agp->info.ai_aperture_size, DRM_MTRR_WC); 1509 DRM_DEBUG("mtrr_del = %d", retcode); 1510 } 1511 1512 free(dev->agp, M_DRM, 0); 1513 #endif 1514 if (dev->pdev && dev->pdev->bus) 1515 free(dev->pdev->bus->self, M_DRM, sizeof(struct pci_dev)); 1516 1517 if (sc->sc_allocated) 1518 free(dev, M_DRM, sizeof(struct drm_device)); 1519 1520 return 0; 1521 } 1522 1523 void 1524 drm_quiesce(struct drm_device *dev) 1525 { 1526 mtx_enter(&dev->quiesce_mtx); 1527 dev->quiesce = 1; 1528 while (dev->quiesce_count > 0) { 1529 msleep_nsec(&dev->quiesce_count, &dev->quiesce_mtx, 1530 PZERO, "drmqui", INFSLP); 1531 } 1532 mtx_leave(&dev->quiesce_mtx); 1533 } 1534 1535 void 1536 drm_wakeup(struct drm_device *dev) 1537 { 1538 mtx_enter(&dev->quiesce_mtx); 1539 dev->quiesce = 0; 1540 wakeup(&dev->quiesce); 1541 mtx_leave(&dev->quiesce_mtx); 1542 } 1543 1544 int 1545 drm_activate(struct device *self, int act) 1546 { 1547 struct drm_softc *sc = (struct drm_softc *)self; 1548 struct drm_device *dev = sc->sc_drm; 1549 1550 switch (act) { 1551 case DVACT_QUIESCE: 1552 #ifdef CONFIG_ACPI 1553 if (acpi_softc && acpi_softc->sc_state == ACPI_STATE_S3) 1554 pm_suspend_target_state = PM_SUSPEND_MEM; 1555 else 1556 pm_suspend_target_state = PM_SUSPEND_TO_IDLE; 1557 #else 1558 pm_suspend_target_state = PM_SUSPEND_TO_IDLE; 1559 #endif 1560 drm_quiesce(dev); 1561 break; 1562 case DVACT_WAKEUP: 1563 drm_wakeup(dev); 1564 pm_suspend_target_state = PM_SUSPEND_ON; 1565 break; 1566 } 1567 1568 return (0); 1569 } 1570 1571 const struct cfattach drm_ca = { 1572 sizeof(struct drm_softc), drm_probe, drm_attach, 1573 drm_detach, drm_activate 1574 }; 1575 1576 struct cfdriver drm_cd = { 1577 0, "drm", DV_DULL 1578 }; 1579 1580 const struct pci_device_id * 1581 drm_find_description(int vendor, int device, const struct pci_device_id *idlist) 1582 { 1583 int i = 0; 1584 1585 for (i = 0; idlist[i].vendor != 0; i++) { 1586 if ((idlist[i].vendor == vendor) && 1587 (idlist[i].device == device || 1588 idlist[i].device == PCI_ANY_ID) && 1589 (idlist[i].subvendor == PCI_ANY_ID) && 1590 (idlist[i].subdevice == PCI_ANY_ID)) 1591 return &idlist[i]; 1592 } 1593 return NULL; 1594 } 1595 1596 int 1597 drm_file_cmp(struct drm_file *f1, struct drm_file *f2) 1598 { 1599 return (f1->fminor < f2->fminor ? -1 : f1->fminor > f2->fminor); 1600 } 1601 1602 SPLAY_GENERATE(drm_file_tree, drm_file, link, drm_file_cmp); 1603 1604 struct drm_file * 1605 drm_find_file_by_minor(struct drm_device *dev, int minor) 1606 { 1607 struct drm_file key; 1608 1609 key.fminor = minor; 1610 return (SPLAY_FIND(drm_file_tree, &dev->files, &key)); 1611 } 1612 1613 struct drm_device * 1614 drm_get_device_from_kdev(dev_t kdev) 1615 { 1616 int unit = minor(kdev) & ((1 << CLONE_SHIFT) - 1); 1617 /* render */ 1618 if (unit >= 128) 1619 unit -= 128; 1620 struct drm_softc *sc; 1621 1622 if (unit < drm_cd.cd_ndevs) { 1623 sc = (struct drm_softc *)drm_cd.cd_devs[unit]; 1624 if (sc) 1625 return sc->sc_drm; 1626 } 1627 1628 return NULL; 1629 } 1630 1631 void 1632 filt_drmdetach(struct knote *kn) 1633 { 1634 struct drm_device *dev = kn->kn_hook; 1635 int s; 1636 1637 s = spltty(); 1638 klist_remove_locked(&dev->note, kn); 1639 splx(s); 1640 } 1641 1642 int 1643 filt_drmkms(struct knote *kn, long hint) 1644 { 1645 if (kn->kn_sfflags & hint) 1646 kn->kn_fflags |= hint; 1647 return (kn->kn_fflags != 0); 1648 } 1649 1650 void 1651 filt_drmreaddetach(struct knote *kn) 1652 { 1653 struct drm_file *file_priv = kn->kn_hook; 1654 int s; 1655 1656 s = spltty(); 1657 klist_remove_locked(&file_priv->rsel.si_note, kn); 1658 splx(s); 1659 } 1660 1661 int 1662 filt_drmread(struct knote *kn, long hint) 1663 { 1664 struct drm_file *file_priv = kn->kn_hook; 1665 int val = 0; 1666 1667 if ((hint & NOTE_SUBMIT) == 0) 1668 mtx_enter(&file_priv->minor->dev->event_lock); 1669 val = !list_empty(&file_priv->event_list); 1670 if ((hint & NOTE_SUBMIT) == 0) 1671 mtx_leave(&file_priv->minor->dev->event_lock); 1672 return (val); 1673 } 1674 1675 const struct filterops drm_filtops = { 1676 .f_flags = FILTEROP_ISFD, 1677 .f_attach = NULL, 1678 .f_detach = filt_drmdetach, 1679 .f_event = filt_drmkms, 1680 }; 1681 1682 const struct filterops drmread_filtops = { 1683 .f_flags = FILTEROP_ISFD, 1684 .f_attach = NULL, 1685 .f_detach = filt_drmreaddetach, 1686 .f_event = filt_drmread, 1687 }; 1688 1689 int 1690 drmkqfilter(dev_t kdev, struct knote *kn) 1691 { 1692 struct drm_device *dev = NULL; 1693 struct drm_file *file_priv = NULL; 1694 int s; 1695 1696 dev = drm_get_device_from_kdev(kdev); 1697 if (dev == NULL || dev->dev_private == NULL) 1698 return (ENXIO); 1699 1700 switch (kn->kn_filter) { 1701 case EVFILT_READ: 1702 mutex_lock(&dev->struct_mutex); 1703 file_priv = drm_find_file_by_minor(dev, minor(kdev)); 1704 mutex_unlock(&dev->struct_mutex); 1705 if (file_priv == NULL) 1706 return (ENXIO); 1707 1708 kn->kn_fop = &drmread_filtops; 1709 kn->kn_hook = file_priv; 1710 1711 s = spltty(); 1712 klist_insert_locked(&file_priv->rsel.si_note, kn); 1713 splx(s); 1714 break; 1715 case EVFILT_DEVICE: 1716 kn->kn_fop = &drm_filtops; 1717 kn->kn_hook = dev; 1718 1719 s = spltty(); 1720 klist_insert_locked(&dev->note, kn); 1721 splx(s); 1722 break; 1723 default: 1724 return (EINVAL); 1725 } 1726 1727 return (0); 1728 } 1729 1730 int 1731 drmopen(dev_t kdev, int flags, int fmt, struct proc *p) 1732 { 1733 struct drm_device *dev = NULL; 1734 struct drm_file *file_priv; 1735 struct drm_minor *dm; 1736 int ret = 0; 1737 int dminor, realminor, minor_type; 1738 int need_setup = 0; 1739 1740 dev = drm_get_device_from_kdev(kdev); 1741 if (dev == NULL || dev->dev_private == NULL) 1742 return (ENXIO); 1743 1744 DRM_DEBUG("open_count = %d\n", atomic_read(&dev->open_count)); 1745 1746 if (flags & O_EXCL) 1747 return (EBUSY); /* No exclusive opens */ 1748 1749 if (drm_dev_needs_global_mutex(dev)) 1750 mutex_lock(&drm_global_mutex); 1751 1752 if (!atomic_fetch_inc(&dev->open_count)) 1753 need_setup = 1; 1754 1755 dminor = minor(kdev); 1756 realminor = dminor & ((1 << CLONE_SHIFT) - 1); 1757 if (realminor < 64) 1758 minor_type = DRM_MINOR_PRIMARY; 1759 else if (realminor >= 128 && realminor < 192) 1760 minor_type = DRM_MINOR_RENDER; 1761 else { 1762 ret = ENXIO; 1763 goto err; 1764 } 1765 1766 dm = *drm_minor_get_slot(dev, minor_type); 1767 if (dm == NULL) { 1768 ret = ENXIO; 1769 goto err; 1770 } 1771 dm->index = minor(kdev); 1772 1773 file_priv = drm_file_alloc(dm); 1774 if (IS_ERR(file_priv)) { 1775 ret = ENOMEM; 1776 goto err; 1777 } 1778 1779 /* first opener automatically becomes master */ 1780 if (drm_is_primary_client(file_priv)) { 1781 ret = drm_master_open(file_priv); 1782 if (ret != 0) 1783 goto out_file_free; 1784 } 1785 1786 file_priv->filp = (void *)file_priv; 1787 file_priv->fminor = minor(kdev); 1788 1789 mutex_lock(&dev->filelist_mutex); 1790 SPLAY_INSERT(drm_file_tree, &dev->files, file_priv); 1791 mutex_unlock(&dev->filelist_mutex); 1792 1793 if (need_setup) { 1794 ret = drm_legacy_setup(dev); 1795 if (ret) 1796 goto out_file_free; 1797 } 1798 1799 if (drm_dev_needs_global_mutex(dev)) 1800 mutex_unlock(&drm_global_mutex); 1801 1802 return 0; 1803 1804 out_file_free: 1805 drm_file_free(file_priv); 1806 err: 1807 atomic_dec(&dev->open_count); 1808 if (drm_dev_needs_global_mutex(dev)) 1809 mutex_unlock(&drm_global_mutex); 1810 return (ret); 1811 } 1812 1813 int 1814 drmclose(dev_t kdev, int flags, int fmt, struct proc *p) 1815 { 1816 struct drm_device *dev = drm_get_device_from_kdev(kdev); 1817 struct drm_file *file_priv; 1818 int retcode = 0; 1819 1820 if (dev == NULL) 1821 return (ENXIO); 1822 1823 if (drm_dev_needs_global_mutex(dev)) 1824 mutex_lock(&drm_global_mutex); 1825 1826 DRM_DEBUG("open_count = %d\n", atomic_read(&dev->open_count)); 1827 1828 mutex_lock(&dev->filelist_mutex); 1829 file_priv = drm_find_file_by_minor(dev, minor(kdev)); 1830 if (file_priv == NULL) { 1831 DRM_ERROR("can't find authenticator\n"); 1832 retcode = EINVAL; 1833 mutex_unlock(&dev->filelist_mutex); 1834 goto done; 1835 } 1836 1837 SPLAY_REMOVE(drm_file_tree, &dev->files, file_priv); 1838 mutex_unlock(&dev->filelist_mutex); 1839 drm_file_free(file_priv); 1840 done: 1841 if (atomic_dec_and_test(&dev->open_count)) 1842 drm_lastclose(dev); 1843 1844 if (drm_dev_needs_global_mutex(dev)) 1845 mutex_unlock(&drm_global_mutex); 1846 1847 return (retcode); 1848 } 1849 1850 int 1851 drmread(dev_t kdev, struct uio *uio, int ioflag) 1852 { 1853 struct drm_device *dev = drm_get_device_from_kdev(kdev); 1854 struct drm_file *file_priv; 1855 struct drm_pending_event *ev; 1856 int error = 0; 1857 1858 if (dev == NULL) 1859 return (ENXIO); 1860 1861 mutex_lock(&dev->filelist_mutex); 1862 file_priv = drm_find_file_by_minor(dev, minor(kdev)); 1863 mutex_unlock(&dev->filelist_mutex); 1864 if (file_priv == NULL) 1865 return (ENXIO); 1866 1867 /* 1868 * The semantics are a little weird here. We will wait until we 1869 * have events to process, but as soon as we have events we will 1870 * only deliver as many as we have. 1871 * Note that events are atomic, if the read buffer will not fit in 1872 * a whole event, we won't read any of it out. 1873 */ 1874 mtx_enter(&dev->event_lock); 1875 while (error == 0 && list_empty(&file_priv->event_list)) { 1876 if (ioflag & IO_NDELAY) { 1877 mtx_leave(&dev->event_lock); 1878 return (EAGAIN); 1879 } 1880 error = msleep_nsec(&file_priv->event_wait, &dev->event_lock, 1881 PWAIT | PCATCH, "drmread", INFSLP); 1882 } 1883 if (error) { 1884 mtx_leave(&dev->event_lock); 1885 return (error); 1886 } 1887 while (drm_dequeue_event(dev, file_priv, uio->uio_resid, &ev)) { 1888 MUTEX_ASSERT_UNLOCKED(&dev->event_lock); 1889 /* XXX we always destroy the event on error. */ 1890 error = uiomove(ev->event, ev->event->length, uio); 1891 kfree(ev); 1892 if (error) 1893 break; 1894 mtx_enter(&dev->event_lock); 1895 } 1896 MUTEX_ASSERT_UNLOCKED(&dev->event_lock); 1897 1898 return (error); 1899 } 1900 1901 /* 1902 * Deqeue an event from the file priv in question. returning 1 if an 1903 * event was found. We take the resid from the read as a parameter because 1904 * we will only dequeue and event if the read buffer has space to fit the 1905 * entire thing. 1906 * 1907 * We are called locked, but we will *unlock* the queue on return so that 1908 * we may sleep to copyout the event. 1909 */ 1910 int 1911 drm_dequeue_event(struct drm_device *dev, struct drm_file *file_priv, 1912 size_t resid, struct drm_pending_event **out) 1913 { 1914 struct drm_pending_event *e = NULL; 1915 int gotone = 0; 1916 1917 MUTEX_ASSERT_LOCKED(&dev->event_lock); 1918 1919 *out = NULL; 1920 if (list_empty(&file_priv->event_list)) 1921 goto out; 1922 e = list_first_entry(&file_priv->event_list, 1923 struct drm_pending_event, link); 1924 if (e->event->length > resid) 1925 goto out; 1926 1927 file_priv->event_space += e->event->length; 1928 list_del(&e->link); 1929 *out = e; 1930 gotone = 1; 1931 1932 out: 1933 mtx_leave(&dev->event_lock); 1934 1935 return (gotone); 1936 } 1937 1938 paddr_t 1939 drmmmap(dev_t kdev, off_t offset, int prot) 1940 { 1941 return -1; 1942 } 1943 1944 struct drm_dmamem * 1945 drm_dmamem_alloc(bus_dma_tag_t dmat, bus_size_t size, bus_size_t alignment, 1946 int nsegments, bus_size_t maxsegsz, int mapflags, int loadflags) 1947 { 1948 struct drm_dmamem *mem; 1949 size_t strsize; 1950 /* 1951 * segs is the last member of the struct since we modify the size 1952 * to allow extra segments if more than one are allowed. 1953 */ 1954 strsize = sizeof(*mem) + (sizeof(bus_dma_segment_t) * (nsegments - 1)); 1955 mem = malloc(strsize, M_DRM, M_NOWAIT | M_ZERO); 1956 if (mem == NULL) 1957 return (NULL); 1958 1959 mem->size = size; 1960 1961 if (bus_dmamap_create(dmat, size, nsegments, maxsegsz, 0, 1962 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &mem->map) != 0) 1963 goto strfree; 1964 1965 if (bus_dmamem_alloc(dmat, size, alignment, 0, mem->segs, nsegments, 1966 &mem->nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO) != 0) 1967 goto destroy; 1968 1969 if (bus_dmamem_map(dmat, mem->segs, mem->nsegs, size, 1970 &mem->kva, BUS_DMA_NOWAIT | mapflags) != 0) 1971 goto free; 1972 1973 if (bus_dmamap_load(dmat, mem->map, mem->kva, size, 1974 NULL, BUS_DMA_NOWAIT | loadflags) != 0) 1975 goto unmap; 1976 1977 return (mem); 1978 1979 unmap: 1980 bus_dmamem_unmap(dmat, mem->kva, size); 1981 free: 1982 bus_dmamem_free(dmat, mem->segs, mem->nsegs); 1983 destroy: 1984 bus_dmamap_destroy(dmat, mem->map); 1985 strfree: 1986 free(mem, M_DRM, 0); 1987 1988 return (NULL); 1989 } 1990 1991 void 1992 drm_dmamem_free(bus_dma_tag_t dmat, struct drm_dmamem *mem) 1993 { 1994 if (mem == NULL) 1995 return; 1996 1997 bus_dmamap_unload(dmat, mem->map); 1998 bus_dmamem_unmap(dmat, mem->kva, mem->size); 1999 bus_dmamem_free(dmat, mem->segs, mem->nsegs); 2000 bus_dmamap_destroy(dmat, mem->map); 2001 free(mem, M_DRM, 0); 2002 } 2003 2004 struct drm_dma_handle * 2005 drm_pci_alloc(struct drm_device *dev, size_t size, size_t align) 2006 { 2007 struct drm_dma_handle *dmah; 2008 2009 dmah = malloc(sizeof(*dmah), M_DRM, M_WAITOK); 2010 dmah->mem = drm_dmamem_alloc(dev->dmat, size, align, 1, size, 2011 BUS_DMA_NOCACHE, 0); 2012 if (dmah->mem == NULL) { 2013 free(dmah, M_DRM, sizeof(*dmah)); 2014 return NULL; 2015 } 2016 dmah->busaddr = dmah->mem->segs[0].ds_addr; 2017 dmah->size = dmah->mem->size; 2018 dmah->vaddr = dmah->mem->kva; 2019 return (dmah); 2020 } 2021 2022 void 2023 drm_pci_free(struct drm_device *dev, struct drm_dma_handle *dmah) 2024 { 2025 if (dmah == NULL) 2026 return; 2027 2028 drm_dmamem_free(dev->dmat, dmah->mem); 2029 free(dmah, M_DRM, sizeof(*dmah)); 2030 } 2031 2032 /* 2033 * Compute order. Can be made faster. 2034 */ 2035 int 2036 drm_order(unsigned long size) 2037 { 2038 int order; 2039 unsigned long tmp; 2040 2041 for (order = 0, tmp = size; tmp >>= 1; ++order) 2042 ; 2043 2044 if (size & ~(1 << order)) 2045 ++order; 2046 2047 return order; 2048 } 2049 2050 int 2051 drm_getpciinfo(struct drm_device *dev, void *data, struct drm_file *file_priv) 2052 { 2053 struct drm_pciinfo *info = data; 2054 2055 if (dev->pdev == NULL) 2056 return -ENOTTY; 2057 2058 info->domain = dev->pdev->bus->domain_nr; 2059 info->bus = dev->pdev->bus->number; 2060 info->dev = PCI_SLOT(dev->pdev->devfn); 2061 info->func = PCI_FUNC(dev->pdev->devfn); 2062 info->vendor_id = dev->pdev->vendor; 2063 info->device_id = dev->pdev->device; 2064 info->subvendor_id = dev->pdev->subsystem_vendor; 2065 info->subdevice_id = dev->pdev->subsystem_device; 2066 info->revision_id = 0; 2067 2068 return 0; 2069 } 2070