Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
0003  *
0004  * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
0005  * All Rights Reserved.
0006  *
0007  * Author Rickard E. (Rik) Faith <faith@valinux.com>
0008  *
0009  * Permission is hereby granted, free of charge, to any person obtaining a
0010  * copy of this software and associated documentation files (the "Software"),
0011  * to deal in the Software without restriction, including without limitation
0012  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0013  * and/or sell copies of the Software, and to permit persons to whom the
0014  * Software is furnished to do so, subject to the following conditions:
0015  *
0016  * The above copyright notice and this permission notice (including the next
0017  * paragraph) shall be included in all copies or substantial portions of the
0018  * Software.
0019  *
0020  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0021  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0022  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0023  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
0024  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0025  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
0026  * DEALINGS IN THE SOFTWARE.
0027  */
0028 
0029 #include <linux/debugfs.h>
0030 #include <linux/fs.h>
0031 #include <linux/module.h>
0032 #include <linux/moduleparam.h>
0033 #include <linux/mount.h>
0034 #include <linux/pseudo_fs.h>
0035 #include <linux/slab.h>
0036 #include <linux/srcu.h>
0037 
0038 #include <drm/drm_cache.h>
0039 #include <drm/drm_client.h>
0040 #include <drm/drm_color_mgmt.h>
0041 #include <drm/drm_drv.h>
0042 #include <drm/drm_file.h>
0043 #include <drm/drm_managed.h>
0044 #include <drm/drm_mode_object.h>
0045 #include <drm/drm_print.h>
0046 #include <drm/drm_privacy_screen_machine.h>
0047 
0048 #include "drm_crtc_internal.h"
0049 #include "drm_internal.h"
0050 #include "drm_legacy.h"
0051 
0052 MODULE_AUTHOR("Gareth Hughes, Leif Delgass, José Fonseca, Jon Smirl");
0053 MODULE_DESCRIPTION("DRM shared core routines");
0054 MODULE_LICENSE("GPL and additional rights");
0055 
0056 static DEFINE_SPINLOCK(drm_minor_lock);
0057 static struct idr drm_minors_idr;
0058 
0059 /*
0060  * If the drm core fails to init for whatever reason,
0061  * we should prevent any drivers from registering with it.
0062  * It's best to check this at drm_dev_init(), as some drivers
0063  * prefer to embed struct drm_device into their own device
0064  * structure and call drm_dev_init() themselves.
0065  */
0066 static bool drm_core_init_complete;
0067 
0068 static struct dentry *drm_debugfs_root;
0069 
0070 DEFINE_STATIC_SRCU(drm_unplug_srcu);
0071 
0072 /*
0073  * DRM Minors
0074  * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
0075  * of them is represented by a drm_minor object. Depending on the capabilities
0076  * of the device-driver, different interfaces are registered.
0077  *
0078  * Minors can be accessed via dev->$minor_name. This pointer is either
0079  * NULL or a valid drm_minor pointer and stays valid as long as the device is
0080  * valid. This means, DRM minors have the same life-time as the underlying
0081  * device. However, this doesn't mean that the minor is active. Minors are
0082  * registered and unregistered dynamically according to device-state.
0083  */
0084 
0085 static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
0086                          unsigned int type)
0087 {
0088     switch (type) {
0089     case DRM_MINOR_PRIMARY:
0090         return &dev->primary;
0091     case DRM_MINOR_RENDER:
0092         return &dev->render;
0093     default:
0094         BUG();
0095     }
0096 }
0097 
0098 static void drm_minor_alloc_release(struct drm_device *dev, void *data)
0099 {
0100     struct drm_minor *minor = data;
0101     unsigned long flags;
0102 
0103     WARN_ON(dev != minor->dev);
0104 
0105     put_device(minor->kdev);
0106 
0107     spin_lock_irqsave(&drm_minor_lock, flags);
0108     idr_remove(&drm_minors_idr, minor->index);
0109     spin_unlock_irqrestore(&drm_minor_lock, flags);
0110 }
0111 
0112 static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
0113 {
0114     struct drm_minor *minor;
0115     unsigned long flags;
0116     int r;
0117 
0118     minor = drmm_kzalloc(dev, sizeof(*minor), GFP_KERNEL);
0119     if (!minor)
0120         return -ENOMEM;
0121 
0122     minor->type = type;
0123     minor->dev = dev;
0124 
0125     idr_preload(GFP_KERNEL);
0126     spin_lock_irqsave(&drm_minor_lock, flags);
0127     r = idr_alloc(&drm_minors_idr,
0128               NULL,
0129               64 * type,
0130               64 * (type + 1),
0131               GFP_NOWAIT);
0132     spin_unlock_irqrestore(&drm_minor_lock, flags);
0133     idr_preload_end();
0134 
0135     if (r < 0)
0136         return r;
0137 
0138     minor->index = r;
0139 
0140     r = drmm_add_action_or_reset(dev, drm_minor_alloc_release, minor);
0141     if (r)
0142         return r;
0143 
0144     minor->kdev = drm_sysfs_minor_alloc(minor);
0145     if (IS_ERR(minor->kdev))
0146         return PTR_ERR(minor->kdev);
0147 
0148     *drm_minor_get_slot(dev, type) = minor;
0149     return 0;
0150 }
0151 
0152 static int drm_minor_register(struct drm_device *dev, unsigned int type)
0153 {
0154     struct drm_minor *minor;
0155     unsigned long flags;
0156     int ret;
0157 
0158     DRM_DEBUG("\n");
0159 
0160     minor = *drm_minor_get_slot(dev, type);
0161     if (!minor)
0162         return 0;
0163 
0164     ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root);
0165     if (ret) {
0166         DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
0167         goto err_debugfs;
0168     }
0169 
0170     ret = device_add(minor->kdev);
0171     if (ret)
0172         goto err_debugfs;
0173 
0174     /* replace NULL with @minor so lookups will succeed from now on */
0175     spin_lock_irqsave(&drm_minor_lock, flags);
0176     idr_replace(&drm_minors_idr, minor, minor->index);
0177     spin_unlock_irqrestore(&drm_minor_lock, flags);
0178 
0179     DRM_DEBUG("new minor registered %d\n", minor->index);
0180     return 0;
0181 
0182 err_debugfs:
0183     drm_debugfs_cleanup(minor);
0184     return ret;
0185 }
0186 
0187 static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
0188 {
0189     struct drm_minor *minor;
0190     unsigned long flags;
0191 
0192     minor = *drm_minor_get_slot(dev, type);
0193     if (!minor || !device_is_registered(minor->kdev))
0194         return;
0195 
0196     /* replace @minor with NULL so lookups will fail from now on */
0197     spin_lock_irqsave(&drm_minor_lock, flags);
0198     idr_replace(&drm_minors_idr, NULL, minor->index);
0199     spin_unlock_irqrestore(&drm_minor_lock, flags);
0200 
0201     device_del(minor->kdev);
0202     dev_set_drvdata(minor->kdev, NULL); /* safety belt */
0203     drm_debugfs_cleanup(minor);
0204 }
0205 
0206 /*
0207  * Looks up the given minor-ID and returns the respective DRM-minor object. The
0208  * refence-count of the underlying device is increased so you must release this
0209  * object with drm_minor_release().
0210  *
0211  * As long as you hold this minor, it is guaranteed that the object and the
0212  * minor->dev pointer will stay valid! However, the device may get unplugged and
0213  * unregistered while you hold the minor.
0214  */
0215 struct drm_minor *drm_minor_acquire(unsigned int minor_id)
0216 {
0217     struct drm_minor *minor;
0218     unsigned long flags;
0219 
0220     spin_lock_irqsave(&drm_minor_lock, flags);
0221     minor = idr_find(&drm_minors_idr, minor_id);
0222     if (minor)
0223         drm_dev_get(minor->dev);
0224     spin_unlock_irqrestore(&drm_minor_lock, flags);
0225 
0226     if (!minor) {
0227         return ERR_PTR(-ENODEV);
0228     } else if (drm_dev_is_unplugged(minor->dev)) {
0229         drm_dev_put(minor->dev);
0230         return ERR_PTR(-ENODEV);
0231     }
0232 
0233     return minor;
0234 }
0235 
0236 void drm_minor_release(struct drm_minor *minor)
0237 {
0238     drm_dev_put(minor->dev);
0239 }
0240 
0241 /**
0242  * DOC: driver instance overview
0243  *
0244  * A device instance for a drm driver is represented by &struct drm_device. This
0245  * is allocated and initialized with devm_drm_dev_alloc(), usually from
0246  * bus-specific ->probe() callbacks implemented by the driver. The driver then
0247  * needs to initialize all the various subsystems for the drm device like memory
0248  * management, vblank handling, modesetting support and initial output
0249  * configuration plus obviously initialize all the corresponding hardware bits.
0250  * Finally when everything is up and running and ready for userspace the device
0251  * instance can be published using drm_dev_register().
0252  *
0253  * There is also deprecated support for initializing device instances using
0254  * bus-specific helpers and the &drm_driver.load callback. But due to
0255  * backwards-compatibility needs the device instance have to be published too
0256  * early, which requires unpretty global locking to make safe and is therefore
0257  * only support for existing drivers not yet converted to the new scheme.
0258  *
0259  * When cleaning up a device instance everything needs to be done in reverse:
0260  * First unpublish the device instance with drm_dev_unregister(). Then clean up
0261  * any other resources allocated at device initialization and drop the driver's
0262  * reference to &drm_device using drm_dev_put().
0263  *
0264  * Note that any allocation or resource which is visible to userspace must be
0265  * released only when the final drm_dev_put() is called, and not when the
0266  * driver is unbound from the underlying physical struct &device. Best to use
0267  * &drm_device managed resources with drmm_add_action(), drmm_kmalloc() and
0268  * related functions.
0269  *
0270  * devres managed resources like devm_kmalloc() can only be used for resources
0271  * directly related to the underlying hardware device, and only used in code
0272  * paths fully protected by drm_dev_enter() and drm_dev_exit().
0273  *
0274  * Display driver example
0275  * ~~~~~~~~~~~~~~~~~~~~~~
0276  *
0277  * The following example shows a typical structure of a DRM display driver.
0278  * The example focus on the probe() function and the other functions that is
0279  * almost always present and serves as a demonstration of devm_drm_dev_alloc().
0280  *
0281  * .. code-block:: c
0282  *
0283  *  struct driver_device {
0284  *      struct drm_device drm;
0285  *      void *userspace_facing;
0286  *      struct clk *pclk;
0287  *  };
0288  *
0289  *  static const struct drm_driver driver_drm_driver = {
0290  *      [...]
0291  *  };
0292  *
0293  *  static int driver_probe(struct platform_device *pdev)
0294  *  {
0295  *      struct driver_device *priv;
0296  *      struct drm_device *drm;
0297  *      int ret;
0298  *
0299  *      priv = devm_drm_dev_alloc(&pdev->dev, &driver_drm_driver,
0300  *                    struct driver_device, drm);
0301  *      if (IS_ERR(priv))
0302  *          return PTR_ERR(priv);
0303  *      drm = &priv->drm;
0304  *
0305  *      ret = drmm_mode_config_init(drm);
0306  *      if (ret)
0307  *          return ret;
0308  *
0309  *      priv->userspace_facing = drmm_kzalloc(..., GFP_KERNEL);
0310  *      if (!priv->userspace_facing)
0311  *          return -ENOMEM;
0312  *
0313  *      priv->pclk = devm_clk_get(dev, "PCLK");
0314  *      if (IS_ERR(priv->pclk))
0315  *          return PTR_ERR(priv->pclk);
0316  *
0317  *      // Further setup, display pipeline etc
0318  *
0319  *      platform_set_drvdata(pdev, drm);
0320  *
0321  *      drm_mode_config_reset(drm);
0322  *
0323  *      ret = drm_dev_register(drm);
0324  *      if (ret)
0325  *          return ret;
0326  *
0327  *      drm_fbdev_generic_setup(drm, 32);
0328  *
0329  *      return 0;
0330  *  }
0331  *
0332  *  // This function is called before the devm_ resources are released
0333  *  static int driver_remove(struct platform_device *pdev)
0334  *  {
0335  *      struct drm_device *drm = platform_get_drvdata(pdev);
0336  *
0337  *      drm_dev_unregister(drm);
0338  *      drm_atomic_helper_shutdown(drm)
0339  *
0340  *      return 0;
0341  *  }
0342  *
0343  *  // This function is called on kernel restart and shutdown
0344  *  static void driver_shutdown(struct platform_device *pdev)
0345  *  {
0346  *      drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
0347  *  }
0348  *
0349  *  static int __maybe_unused driver_pm_suspend(struct device *dev)
0350  *  {
0351  *      return drm_mode_config_helper_suspend(dev_get_drvdata(dev));
0352  *  }
0353  *
0354  *  static int __maybe_unused driver_pm_resume(struct device *dev)
0355  *  {
0356  *      drm_mode_config_helper_resume(dev_get_drvdata(dev));
0357  *
0358  *      return 0;
0359  *  }
0360  *
0361  *  static const struct dev_pm_ops driver_pm_ops = {
0362  *      SET_SYSTEM_SLEEP_PM_OPS(driver_pm_suspend, driver_pm_resume)
0363  *  };
0364  *
0365  *  static struct platform_driver driver_driver = {
0366  *      .driver = {
0367  *          [...]
0368  *          .pm = &driver_pm_ops,
0369  *      },
0370  *      .probe = driver_probe,
0371  *      .remove = driver_remove,
0372  *      .shutdown = driver_shutdown,
0373  *  };
0374  *  module_platform_driver(driver_driver);
0375  *
0376  * Drivers that want to support device unplugging (USB, DT overlay unload) should
0377  * use drm_dev_unplug() instead of drm_dev_unregister(). The driver must protect
0378  * regions that is accessing device resources to prevent use after they're
0379  * released. This is done using drm_dev_enter() and drm_dev_exit(). There is one
0380  * shortcoming however, drm_dev_unplug() marks the drm_device as unplugged before
0381  * drm_atomic_helper_shutdown() is called. This means that if the disable code
0382  * paths are protected, they will not run on regular driver module unload,
0383  * possibly leaving the hardware enabled.
0384  */
0385 
0386 /**
0387  * drm_put_dev - Unregister and release a DRM device
0388  * @dev: DRM device
0389  *
0390  * Called at module unload time or when a PCI device is unplugged.
0391  *
0392  * Cleans up all DRM device, calling drm_lastclose().
0393  *
0394  * Note: Use of this function is deprecated. It will eventually go away
0395  * completely.  Please use drm_dev_unregister() and drm_dev_put() explicitly
0396  * instead to make sure that the device isn't userspace accessible any more
0397  * while teardown is in progress, ensuring that userspace can't access an
0398  * inconsistent state.
0399  */
0400 void drm_put_dev(struct drm_device *dev)
0401 {
0402     DRM_DEBUG("\n");
0403 
0404     if (!dev) {
0405         DRM_ERROR("cleanup called no dev\n");
0406         return;
0407     }
0408 
0409     drm_dev_unregister(dev);
0410     drm_dev_put(dev);
0411 }
0412 EXPORT_SYMBOL(drm_put_dev);
0413 
0414 /**
0415  * drm_dev_enter - Enter device critical section
0416  * @dev: DRM device
0417  * @idx: Pointer to index that will be passed to the matching drm_dev_exit()
0418  *
0419  * This function marks and protects the beginning of a section that should not
0420  * be entered after the device has been unplugged. The section end is marked
0421  * with drm_dev_exit(). Calls to this function can be nested.
0422  *
0423  * Returns:
0424  * True if it is OK to enter the section, false otherwise.
0425  */
0426 bool drm_dev_enter(struct drm_device *dev, int *idx)
0427 {
0428     *idx = srcu_read_lock(&drm_unplug_srcu);
0429 
0430     if (dev->unplugged) {
0431         srcu_read_unlock(&drm_unplug_srcu, *idx);
0432         return false;
0433     }
0434 
0435     return true;
0436 }
0437 EXPORT_SYMBOL(drm_dev_enter);
0438 
0439 /**
0440  * drm_dev_exit - Exit device critical section
0441  * @idx: index returned from drm_dev_enter()
0442  *
0443  * This function marks the end of a section that should not be entered after
0444  * the device has been unplugged.
0445  */
0446 void drm_dev_exit(int idx)
0447 {
0448     srcu_read_unlock(&drm_unplug_srcu, idx);
0449 }
0450 EXPORT_SYMBOL(drm_dev_exit);
0451 
0452 /**
0453  * drm_dev_unplug - unplug a DRM device
0454  * @dev: DRM device
0455  *
0456  * This unplugs a hotpluggable DRM device, which makes it inaccessible to
0457  * userspace operations. Entry-points can use drm_dev_enter() and
0458  * drm_dev_exit() to protect device resources in a race free manner. This
0459  * essentially unregisters the device like drm_dev_unregister(), but can be
0460  * called while there are still open users of @dev.
0461  */
0462 void drm_dev_unplug(struct drm_device *dev)
0463 {
0464     /*
0465      * After synchronizing any critical read section is guaranteed to see
0466      * the new value of ->unplugged, and any critical section which might
0467      * still have seen the old value of ->unplugged is guaranteed to have
0468      * finished.
0469      */
0470     dev->unplugged = true;
0471     synchronize_srcu(&drm_unplug_srcu);
0472 
0473     drm_dev_unregister(dev);
0474 
0475     /* Clear all CPU mappings pointing to this device */
0476     unmap_mapping_range(dev->anon_inode->i_mapping, 0, 0, 1);
0477 }
0478 EXPORT_SYMBOL(drm_dev_unplug);
0479 
0480 /*
0481  * DRM internal mount
0482  * We want to be able to allocate our own "struct address_space" to control
0483  * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
0484  * stand-alone address_space objects, so we need an underlying inode. As there
0485  * is no way to allocate an independent inode easily, we need a fake internal
0486  * VFS mount-point.
0487  *
0488  * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
0489  * frees it again. You are allowed to use iget() and iput() to get references to
0490  * the inode. But each drm_fs_inode_new() call must be paired with exactly one
0491  * drm_fs_inode_free() call (which does not have to be the last iput()).
0492  * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
0493  * between multiple inode-users. You could, technically, call
0494  * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
0495  * iput(), but this way you'd end up with a new vfsmount for each inode.
0496  */
0497 
0498 static int drm_fs_cnt;
0499 static struct vfsmount *drm_fs_mnt;
0500 
0501 static int drm_fs_init_fs_context(struct fs_context *fc)
0502 {
0503     return init_pseudo(fc, 0x010203ff) ? 0 : -ENOMEM;
0504 }
0505 
0506 static struct file_system_type drm_fs_type = {
0507     .name       = "drm",
0508     .owner      = THIS_MODULE,
0509     .init_fs_context = drm_fs_init_fs_context,
0510     .kill_sb    = kill_anon_super,
0511 };
0512 
0513 static struct inode *drm_fs_inode_new(void)
0514 {
0515     struct inode *inode;
0516     int r;
0517 
0518     r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
0519     if (r < 0) {
0520         DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
0521         return ERR_PTR(r);
0522     }
0523 
0524     inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
0525     if (IS_ERR(inode))
0526         simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
0527 
0528     return inode;
0529 }
0530 
0531 static void drm_fs_inode_free(struct inode *inode)
0532 {
0533     if (inode) {
0534         iput(inode);
0535         simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
0536     }
0537 }
0538 
0539 /**
0540  * DOC: component helper usage recommendations
0541  *
0542  * DRM drivers that drive hardware where a logical device consists of a pile of
0543  * independent hardware blocks are recommended to use the :ref:`component helper
0544  * library<component>`. For consistency and better options for code reuse the
0545  * following guidelines apply:
0546  *
0547  *  - The entire device initialization procedure should be run from the
0548  *    &component_master_ops.master_bind callback, starting with
0549  *    devm_drm_dev_alloc(), then binding all components with
0550  *    component_bind_all() and finishing with drm_dev_register().
0551  *
0552  *  - The opaque pointer passed to all components through component_bind_all()
0553  *    should point at &struct drm_device of the device instance, not some driver
0554  *    specific private structure.
0555  *
0556  *  - The component helper fills the niche where further standardization of
0557  *    interfaces is not practical. When there already is, or will be, a
0558  *    standardized interface like &drm_bridge or &drm_panel, providing its own
0559  *    functions to find such components at driver load time, like
0560  *    drm_of_find_panel_or_bridge(), then the component helper should not be
0561  *    used.
0562  */
0563 
0564 static void drm_dev_init_release(struct drm_device *dev, void *res)
0565 {
0566     drm_legacy_ctxbitmap_cleanup(dev);
0567     drm_legacy_remove_map_hash(dev);
0568     drm_fs_inode_free(dev->anon_inode);
0569 
0570     put_device(dev->dev);
0571     /* Prevent use-after-free in drm_managed_release when debugging is
0572      * enabled. Slightly awkward, but can't really be helped. */
0573     dev->dev = NULL;
0574     mutex_destroy(&dev->master_mutex);
0575     mutex_destroy(&dev->clientlist_mutex);
0576     mutex_destroy(&dev->filelist_mutex);
0577     mutex_destroy(&dev->struct_mutex);
0578     drm_legacy_destroy_members(dev);
0579 }
0580 
0581 static int drm_dev_init(struct drm_device *dev,
0582             const struct drm_driver *driver,
0583             struct device *parent)
0584 {
0585     struct inode *inode;
0586     int ret;
0587 
0588     if (!drm_core_init_complete) {
0589         DRM_ERROR("DRM core is not initialized\n");
0590         return -ENODEV;
0591     }
0592 
0593     if (WARN_ON(!parent))
0594         return -EINVAL;
0595 
0596     kref_init(&dev->ref);
0597     dev->dev = get_device(parent);
0598     dev->driver = driver;
0599 
0600     INIT_LIST_HEAD(&dev->managed.resources);
0601     spin_lock_init(&dev->managed.lock);
0602 
0603     /* no per-device feature limits by default */
0604     dev->driver_features = ~0u;
0605 
0606     drm_legacy_init_members(dev);
0607     INIT_LIST_HEAD(&dev->filelist);
0608     INIT_LIST_HEAD(&dev->filelist_internal);
0609     INIT_LIST_HEAD(&dev->clientlist);
0610     INIT_LIST_HEAD(&dev->vblank_event_list);
0611 
0612     spin_lock_init(&dev->event_lock);
0613     mutex_init(&dev->struct_mutex);
0614     mutex_init(&dev->filelist_mutex);
0615     mutex_init(&dev->clientlist_mutex);
0616     mutex_init(&dev->master_mutex);
0617 
0618     ret = drmm_add_action(dev, drm_dev_init_release, NULL);
0619     if (ret)
0620         return ret;
0621 
0622     inode = drm_fs_inode_new();
0623     if (IS_ERR(inode)) {
0624         ret = PTR_ERR(inode);
0625         DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
0626         goto err;
0627     }
0628 
0629     dev->anon_inode = inode;
0630 
0631     if (drm_core_check_feature(dev, DRIVER_RENDER)) {
0632         ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
0633         if (ret)
0634             goto err;
0635     }
0636 
0637     ret = drm_minor_alloc(dev, DRM_MINOR_PRIMARY);
0638     if (ret)
0639         goto err;
0640 
0641     ret = drm_legacy_create_map_hash(dev);
0642     if (ret)
0643         goto err;
0644 
0645     drm_legacy_ctxbitmap_init(dev);
0646 
0647     if (drm_core_check_feature(dev, DRIVER_GEM)) {
0648         ret = drm_gem_init(dev);
0649         if (ret) {
0650             DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
0651             goto err;
0652         }
0653     }
0654 
0655     ret = drm_dev_set_unique(dev, dev_name(parent));
0656     if (ret)
0657         goto err;
0658 
0659     return 0;
0660 
0661 err:
0662     drm_managed_release(dev);
0663 
0664     return ret;
0665 }
0666 
0667 static void devm_drm_dev_init_release(void *data)
0668 {
0669     drm_dev_put(data);
0670 }
0671 
0672 static int devm_drm_dev_init(struct device *parent,
0673                  struct drm_device *dev,
0674                  const struct drm_driver *driver)
0675 {
0676     int ret;
0677 
0678     ret = drm_dev_init(dev, driver, parent);
0679     if (ret)
0680         return ret;
0681 
0682     return devm_add_action_or_reset(parent,
0683                     devm_drm_dev_init_release, dev);
0684 }
0685 
0686 void *__devm_drm_dev_alloc(struct device *parent,
0687                const struct drm_driver *driver,
0688                size_t size, size_t offset)
0689 {
0690     void *container;
0691     struct drm_device *drm;
0692     int ret;
0693 
0694     container = kzalloc(size, GFP_KERNEL);
0695     if (!container)
0696         return ERR_PTR(-ENOMEM);
0697 
0698     drm = container + offset;
0699     ret = devm_drm_dev_init(parent, drm, driver);
0700     if (ret) {
0701         kfree(container);
0702         return ERR_PTR(ret);
0703     }
0704     drmm_add_final_kfree(drm, container);
0705 
0706     return container;
0707 }
0708 EXPORT_SYMBOL(__devm_drm_dev_alloc);
0709 
0710 /**
0711  * drm_dev_alloc - Allocate new DRM device
0712  * @driver: DRM driver to allocate device for
0713  * @parent: Parent device object
0714  *
0715  * This is the deprecated version of devm_drm_dev_alloc(), which does not support
0716  * subclassing through embedding the struct &drm_device in a driver private
0717  * structure, and which does not support automatic cleanup through devres.
0718  *
0719  * RETURNS:
0720  * Pointer to new DRM device, or ERR_PTR on failure.
0721  */
0722 struct drm_device *drm_dev_alloc(const struct drm_driver *driver,
0723                  struct device *parent)
0724 {
0725     struct drm_device *dev;
0726     int ret;
0727 
0728     dev = kzalloc(sizeof(*dev), GFP_KERNEL);
0729     if (!dev)
0730         return ERR_PTR(-ENOMEM);
0731 
0732     ret = drm_dev_init(dev, driver, parent);
0733     if (ret) {
0734         kfree(dev);
0735         return ERR_PTR(ret);
0736     }
0737 
0738     drmm_add_final_kfree(dev, dev);
0739 
0740     return dev;
0741 }
0742 EXPORT_SYMBOL(drm_dev_alloc);
0743 
0744 static void drm_dev_release(struct kref *ref)
0745 {
0746     struct drm_device *dev = container_of(ref, struct drm_device, ref);
0747 
0748     if (dev->driver->release)
0749         dev->driver->release(dev);
0750 
0751     drm_managed_release(dev);
0752 
0753     kfree(dev->managed.final_kfree);
0754 }
0755 
0756 /**
0757  * drm_dev_get - Take reference of a DRM device
0758  * @dev: device to take reference of or NULL
0759  *
0760  * This increases the ref-count of @dev by one. You *must* already own a
0761  * reference when calling this. Use drm_dev_put() to drop this reference
0762  * again.
0763  *
0764  * This function never fails. However, this function does not provide *any*
0765  * guarantee whether the device is alive or running. It only provides a
0766  * reference to the object and the memory associated with it.
0767  */
0768 void drm_dev_get(struct drm_device *dev)
0769 {
0770     if (dev)
0771         kref_get(&dev->ref);
0772 }
0773 EXPORT_SYMBOL(drm_dev_get);
0774 
0775 /**
0776  * drm_dev_put - Drop reference of a DRM device
0777  * @dev: device to drop reference of or NULL
0778  *
0779  * This decreases the ref-count of @dev by one. The device is destroyed if the
0780  * ref-count drops to zero.
0781  */
0782 void drm_dev_put(struct drm_device *dev)
0783 {
0784     if (dev)
0785         kref_put(&dev->ref, drm_dev_release);
0786 }
0787 EXPORT_SYMBOL(drm_dev_put);
0788 
0789 static int create_compat_control_link(struct drm_device *dev)
0790 {
0791     struct drm_minor *minor;
0792     char *name;
0793     int ret;
0794 
0795     if (!drm_core_check_feature(dev, DRIVER_MODESET))
0796         return 0;
0797 
0798     minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY);
0799     if (!minor)
0800         return 0;
0801 
0802     /*
0803      * Some existing userspace out there uses the existing of the controlD*
0804      * sysfs files to figure out whether it's a modeset driver. It only does
0805      * readdir, hence a symlink is sufficient (and the least confusing
0806      * option). Otherwise controlD* is entirely unused.
0807      *
0808      * Old controlD chardev have been allocated in the range
0809      * 64-127.
0810      */
0811     name = kasprintf(GFP_KERNEL, "controlD%d", minor->index + 64);
0812     if (!name)
0813         return -ENOMEM;
0814 
0815     ret = sysfs_create_link(minor->kdev->kobj.parent,
0816                 &minor->kdev->kobj,
0817                 name);
0818 
0819     kfree(name);
0820 
0821     return ret;
0822 }
0823 
0824 static void remove_compat_control_link(struct drm_device *dev)
0825 {
0826     struct drm_minor *minor;
0827     char *name;
0828 
0829     if (!drm_core_check_feature(dev, DRIVER_MODESET))
0830         return;
0831 
0832     minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY);
0833     if (!minor)
0834         return;
0835 
0836     name = kasprintf(GFP_KERNEL, "controlD%d", minor->index + 64);
0837     if (!name)
0838         return;
0839 
0840     sysfs_remove_link(minor->kdev->kobj.parent, name);
0841 
0842     kfree(name);
0843 }
0844 
0845 /**
0846  * drm_dev_register - Register DRM device
0847  * @dev: Device to register
0848  * @flags: Flags passed to the driver's .load() function
0849  *
0850  * Register the DRM device @dev with the system, advertise device to user-space
0851  * and start normal device operation. @dev must be initialized via drm_dev_init()
0852  * previously.
0853  *
0854  * Never call this twice on any device!
0855  *
0856  * NOTE: To ensure backward compatibility with existing drivers method this
0857  * function calls the &drm_driver.load method after registering the device
0858  * nodes, creating race conditions. Usage of the &drm_driver.load methods is
0859  * therefore deprecated, drivers must perform all initialization before calling
0860  * drm_dev_register().
0861  *
0862  * RETURNS:
0863  * 0 on success, negative error code on failure.
0864  */
0865 int drm_dev_register(struct drm_device *dev, unsigned long flags)
0866 {
0867     const struct drm_driver *driver = dev->driver;
0868     int ret;
0869 
0870     if (!driver->load)
0871         drm_mode_config_validate(dev);
0872 
0873     WARN_ON(!dev->managed.final_kfree);
0874 
0875     if (drm_dev_needs_global_mutex(dev))
0876         mutex_lock(&drm_global_mutex);
0877 
0878     ret = drm_minor_register(dev, DRM_MINOR_RENDER);
0879     if (ret)
0880         goto err_minors;
0881 
0882     ret = drm_minor_register(dev, DRM_MINOR_PRIMARY);
0883     if (ret)
0884         goto err_minors;
0885 
0886     ret = create_compat_control_link(dev);
0887     if (ret)
0888         goto err_minors;
0889 
0890     dev->registered = true;
0891 
0892     if (dev->driver->load) {
0893         ret = dev->driver->load(dev, flags);
0894         if (ret)
0895             goto err_minors;
0896     }
0897 
0898     if (drm_core_check_feature(dev, DRIVER_MODESET))
0899         drm_modeset_register_all(dev);
0900 
0901     DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
0902          driver->name, driver->major, driver->minor,
0903          driver->patchlevel, driver->date,
0904          dev->dev ? dev_name(dev->dev) : "virtual device",
0905          dev->primary->index);
0906 
0907     goto out_unlock;
0908 
0909 err_minors:
0910     remove_compat_control_link(dev);
0911     drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
0912     drm_minor_unregister(dev, DRM_MINOR_RENDER);
0913 out_unlock:
0914     if (drm_dev_needs_global_mutex(dev))
0915         mutex_unlock(&drm_global_mutex);
0916     return ret;
0917 }
0918 EXPORT_SYMBOL(drm_dev_register);
0919 
0920 /**
0921  * drm_dev_unregister - Unregister DRM device
0922  * @dev: Device to unregister
0923  *
0924  * Unregister the DRM device from the system. This does the reverse of
0925  * drm_dev_register() but does not deallocate the device. The caller must call
0926  * drm_dev_put() to drop their final reference.
0927  *
0928  * A special form of unregistering for hotpluggable devices is drm_dev_unplug(),
0929  * which can be called while there are still open users of @dev.
0930  *
0931  * This should be called first in the device teardown code to make sure
0932  * userspace can't access the device instance any more.
0933  */
0934 void drm_dev_unregister(struct drm_device *dev)
0935 {
0936     if (drm_core_check_feature(dev, DRIVER_LEGACY))
0937         drm_lastclose(dev);
0938 
0939     dev->registered = false;
0940 
0941     drm_client_dev_unregister(dev);
0942 
0943     if (drm_core_check_feature(dev, DRIVER_MODESET))
0944         drm_modeset_unregister_all(dev);
0945 
0946     if (dev->driver->unload)
0947         dev->driver->unload(dev);
0948 
0949     drm_legacy_pci_agp_destroy(dev);
0950     drm_legacy_rmmaps(dev);
0951 
0952     remove_compat_control_link(dev);
0953     drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
0954     drm_minor_unregister(dev, DRM_MINOR_RENDER);
0955 }
0956 EXPORT_SYMBOL(drm_dev_unregister);
0957 
0958 /**
0959  * drm_dev_set_unique - Set the unique name of a DRM device
0960  * @dev: device of which to set the unique name
0961  * @name: unique name
0962  *
0963  * Sets the unique name of a DRM device using the specified string. This is
0964  * already done by drm_dev_init(), drivers should only override the default
0965  * unique name for backwards compatibility reasons.
0966  *
0967  * Return: 0 on success or a negative error code on failure.
0968  */
0969 int drm_dev_set_unique(struct drm_device *dev, const char *name)
0970 {
0971     drmm_kfree(dev, dev->unique);
0972     dev->unique = drmm_kstrdup(dev, name, GFP_KERNEL);
0973 
0974     return dev->unique ? 0 : -ENOMEM;
0975 }
0976 EXPORT_SYMBOL(drm_dev_set_unique);
0977 
0978 /*
0979  * DRM Core
0980  * The DRM core module initializes all global DRM objects and makes them
0981  * available to drivers. Once setup, drivers can probe their respective
0982  * devices.
0983  * Currently, core management includes:
0984  *  - The "DRM-Global" key/value database
0985  *  - Global ID management for connectors
0986  *  - DRM major number allocation
0987  *  - DRM minor management
0988  *  - DRM sysfs class
0989  *  - DRM debugfs root
0990  *
0991  * Furthermore, the DRM core provides dynamic char-dev lookups. For each
0992  * interface registered on a DRM device, you can request minor numbers from DRM
0993  * core. DRM core takes care of major-number management and char-dev
0994  * registration. A stub ->open() callback forwards any open() requests to the
0995  * registered minor.
0996  */
0997 
0998 static int drm_stub_open(struct inode *inode, struct file *filp)
0999 {
1000     const struct file_operations *new_fops;
1001     struct drm_minor *minor;
1002     int err;
1003 
1004     DRM_DEBUG("\n");
1005 
1006     minor = drm_minor_acquire(iminor(inode));
1007     if (IS_ERR(minor))
1008         return PTR_ERR(minor);
1009 
1010     new_fops = fops_get(minor->dev->driver->fops);
1011     if (!new_fops) {
1012         err = -ENODEV;
1013         goto out;
1014     }
1015 
1016     replace_fops(filp, new_fops);
1017     if (filp->f_op->open)
1018         err = filp->f_op->open(inode, filp);
1019     else
1020         err = 0;
1021 
1022 out:
1023     drm_minor_release(minor);
1024 
1025     return err;
1026 }
1027 
1028 static const struct file_operations drm_stub_fops = {
1029     .owner = THIS_MODULE,
1030     .open = drm_stub_open,
1031     .llseek = noop_llseek,
1032 };
1033 
1034 static void drm_core_exit(void)
1035 {
1036     drm_privacy_screen_lookup_exit();
1037     unregister_chrdev(DRM_MAJOR, "drm");
1038     debugfs_remove(drm_debugfs_root);
1039     drm_sysfs_destroy();
1040     idr_destroy(&drm_minors_idr);
1041     drm_connector_ida_destroy();
1042 }
1043 
1044 static int __init drm_core_init(void)
1045 {
1046     int ret;
1047 
1048     drm_connector_ida_init();
1049     idr_init(&drm_minors_idr);
1050     drm_memcpy_init_early();
1051 
1052     ret = drm_sysfs_init();
1053     if (ret < 0) {
1054         DRM_ERROR("Cannot create DRM class: %d\n", ret);
1055         goto error;
1056     }
1057 
1058     drm_debugfs_root = debugfs_create_dir("dri", NULL);
1059 
1060     ret = register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops);
1061     if (ret < 0)
1062         goto error;
1063 
1064     drm_privacy_screen_lookup_init();
1065 
1066     drm_core_init_complete = true;
1067 
1068     DRM_DEBUG("Initialized\n");
1069     return 0;
1070 
1071 error:
1072     drm_core_exit();
1073     return ret;
1074 }
1075 
1076 module_init(drm_core_init);
1077 module_exit(drm_core_exit);