^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author Rickard E. (Rik) Faith <faith@valinux.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Permission is hereby granted, free of charge, to any person obtaining a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * copy of this software and associated documentation files (the "Software"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * to deal in the Software without restriction, including without limitation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * the rights to use, copy, modify, merge, publish, distribute, sublicense,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * and/or sell copies of the Software, and to permit persons to whom the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Software is furnished to do so, subject to the following conditions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * The above copyright notice and this permission notice (including the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * paragraph) shall be included in all copies or substantial portions of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Software.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * DEALINGS IN THE SOFTWARE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/pseudo_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/srcu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <drm/drm_client.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <drm/drm_color_mgmt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <drm/drm_drv.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <drm/drm_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <drm/drm_managed.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <drm/drm_mode_object.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <drm/drm_print.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include "drm_crtc_internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include "drm_internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include "drm_legacy.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) MODULE_AUTHOR("Gareth Hughes, Leif Delgass, José Fonseca, Jon Smirl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) MODULE_DESCRIPTION("DRM shared core routines");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) MODULE_LICENSE("GPL and additional rights");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static DEFINE_SPINLOCK(drm_minor_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static struct idr drm_minors_idr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * If the drm core fails to init for whatever reason,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * we should prevent any drivers from registering with it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * It's best to check this at drm_dev_init(), as some drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * prefer to embed struct drm_device into their own device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * structure and call drm_dev_init() themselves.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static bool drm_core_init_complete = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static struct dentry *drm_debugfs_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) DEFINE_STATIC_SRCU(drm_unplug_srcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * DRM Minors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * of them is represented by a drm_minor object. Depending on the capabilities
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * of the device-driver, different interfaces are registered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * Minors can be accessed via dev->$minor_name. This pointer is either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * NULL or a valid drm_minor pointer and stays valid as long as the device is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * valid. This means, DRM minors have the same life-time as the underlying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * device. However, this doesn't mean that the minor is active. Minors are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * registered and unregistered dynamically according to device-state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) unsigned int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) case DRM_MINOR_PRIMARY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return &dev->primary;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) case DRM_MINOR_RENDER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return &dev->render;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static void drm_minor_alloc_release(struct drm_device *dev, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct drm_minor *minor = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) WARN_ON(dev != minor->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) put_device(minor->kdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) spin_lock_irqsave(&drm_minor_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) idr_remove(&drm_minors_idr, minor->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) spin_unlock_irqrestore(&drm_minor_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct drm_minor *minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) minor = drmm_kzalloc(dev, sizeof(*minor), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (!minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) minor->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) minor->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) idr_preload(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) spin_lock_irqsave(&drm_minor_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) r = idr_alloc(&drm_minors_idr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 64 * type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 64 * (type + 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) GFP_NOWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) spin_unlock_irqrestore(&drm_minor_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) idr_preload_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) minor->index = r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) r = drmm_add_action_or_reset(dev, drm_minor_alloc_release, minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) minor->kdev = drm_sysfs_minor_alloc(minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (IS_ERR(minor->kdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return PTR_ERR(minor->kdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) *drm_minor_get_slot(dev, type) = minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static int drm_minor_register(struct drm_device *dev, unsigned int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct drm_minor *minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) DRM_DEBUG("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) minor = *drm_minor_get_slot(dev, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (!minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) goto err_debugfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) ret = device_add(minor->kdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) goto err_debugfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /* replace NULL with @minor so lookups will succeed from now on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) spin_lock_irqsave(&drm_minor_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) idr_replace(&drm_minors_idr, minor, minor->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) spin_unlock_irqrestore(&drm_minor_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) DRM_DEBUG("new minor registered %d\n", minor->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) err_debugfs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) drm_debugfs_cleanup(minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct drm_minor *minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) minor = *drm_minor_get_slot(dev, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (!minor || !device_is_registered(minor->kdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /* replace @minor with NULL so lookups will fail from now on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) spin_lock_irqsave(&drm_minor_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) idr_replace(&drm_minors_idr, NULL, minor->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) spin_unlock_irqrestore(&drm_minor_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) device_del(minor->kdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) dev_set_drvdata(minor->kdev, NULL); /* safety belt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) drm_debugfs_cleanup(minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * Looks up the given minor-ID and returns the respective DRM-minor object. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * refence-count of the underlying device is increased so you must release this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * object with drm_minor_release().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * As long as you hold this minor, it is guaranteed that the object and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * minor->dev pointer will stay valid! However, the device may get unplugged and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * unregistered while you hold the minor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct drm_minor *drm_minor_acquire(unsigned int minor_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct drm_minor *minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) spin_lock_irqsave(&drm_minor_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) minor = idr_find(&drm_minors_idr, minor_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) drm_dev_get(minor->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) spin_unlock_irqrestore(&drm_minor_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (!minor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) } else if (drm_dev_is_unplugged(minor->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) drm_dev_put(minor->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) void drm_minor_release(struct drm_minor *minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) drm_dev_put(minor->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * DOC: driver instance overview
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * A device instance for a drm driver is represented by &struct drm_device. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * is allocated and initialized with devm_drm_dev_alloc(), usually from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * bus-specific ->probe() callbacks implemented by the driver. The driver then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * needs to initialize all the various subsystems for the drm device like memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * management, vblank handling, modesetting support and initial output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * configuration plus obviously initialize all the corresponding hardware bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * Finally when everything is up and running and ready for userspace the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * instance can be published using drm_dev_register().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * There is also deprecated support for initalizing device instances using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * bus-specific helpers and the &drm_driver.load callback. But due to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * backwards-compatibility needs the device instance have to be published too
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * early, which requires unpretty global locking to make safe and is therefore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * only support for existing drivers not yet converted to the new scheme.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * When cleaning up a device instance everything needs to be done in reverse:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * First unpublish the device instance with drm_dev_unregister(). Then clean up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * any other resources allocated at device initialization and drop the driver's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * reference to &drm_device using drm_dev_put().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * Note that any allocation or resource which is visible to userspace must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * released only when the final drm_dev_put() is called, and not when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * driver is unbound from the underlying physical struct &device. Best to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * &drm_device managed resources with drmm_add_action(), drmm_kmalloc() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * related functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * devres managed resources like devm_kmalloc() can only be used for resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * directly related to the underlying hardware device, and only used in code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * paths fully protected by drm_dev_enter() and drm_dev_exit().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * Display driver example
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * ~~~~~~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * The following example shows a typical structure of a DRM display driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * The example focus on the probe() function and the other functions that is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * almost always present and serves as a demonstration of devm_drm_dev_alloc().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * .. code-block:: c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * struct driver_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * struct drm_device drm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * void *userspace_facing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * struct clk *pclk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * static struct drm_driver driver_drm_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * [...]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * static int driver_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * struct driver_device *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * struct drm_device *drm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * priv = devm_drm_dev_alloc(&pdev->dev, &driver_drm_driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * struct driver_device, drm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * if (IS_ERR(priv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * return PTR_ERR(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * drm = &priv->drm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * ret = drmm_mode_config_init(drm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * priv->userspace_facing = drmm_kzalloc(..., GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * if (!priv->userspace_facing)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * priv->pclk = devm_clk_get(dev, "PCLK");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * if (IS_ERR(priv->pclk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * return PTR_ERR(priv->pclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * // Further setup, display pipeline etc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * platform_set_drvdata(pdev, drm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * drm_mode_config_reset(drm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * ret = drm_dev_register(drm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * drm_fbdev_generic_setup(drm, 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * // This function is called before the devm_ resources are released
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * static int driver_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) * struct drm_device *drm = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * drm_dev_unregister(drm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * drm_atomic_helper_shutdown(drm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * // This function is called on kernel restart and shutdown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * static void driver_shutdown(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * static int __maybe_unused driver_pm_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * return drm_mode_config_helper_suspend(dev_get_drvdata(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * static int __maybe_unused driver_pm_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * drm_mode_config_helper_resume(dev_get_drvdata(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * static const struct dev_pm_ops driver_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * SET_SYSTEM_SLEEP_PM_OPS(driver_pm_suspend, driver_pm_resume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) * static struct platform_driver driver_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * [...]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * .pm = &driver_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * .probe = driver_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) * .remove = driver_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) * .shutdown = driver_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) * };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) * module_platform_driver(driver_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) * Drivers that want to support device unplugging (USB, DT overlay unload) should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) * use drm_dev_unplug() instead of drm_dev_unregister(). The driver must protect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * regions that is accessing device resources to prevent use after they're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * released. This is done using drm_dev_enter() and drm_dev_exit(). There is one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) * shortcoming however, drm_dev_unplug() marks the drm_device as unplugged before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * drm_atomic_helper_shutdown() is called. This means that if the disable code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) * paths are protected, they will not run on regular driver module unload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) * possibily leaving the hardware enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * drm_put_dev - Unregister and release a DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * @dev: DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * Called at module unload time or when a PCI device is unplugged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * Cleans up all DRM device, calling drm_lastclose().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * Note: Use of this function is deprecated. It will eventually go away
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * completely. Please use drm_dev_unregister() and drm_dev_put() explicitly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) * instead to make sure that the device isn't userspace accessible any more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * while teardown is in progress, ensuring that userspace can't access an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * inconsistent state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) void drm_put_dev(struct drm_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) DRM_DEBUG("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) DRM_ERROR("cleanup called no dev\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) drm_dev_unregister(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) drm_dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) EXPORT_SYMBOL(drm_put_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * drm_dev_enter - Enter device critical section
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * @dev: DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * @idx: Pointer to index that will be passed to the matching drm_dev_exit()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * This function marks and protects the beginning of a section that should not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * be entered after the device has been unplugged. The section end is marked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * with drm_dev_exit(). Calls to this function can be nested.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * True if it is OK to enter the section, false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) bool drm_dev_enter(struct drm_device *dev, int *idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) *idx = srcu_read_lock(&drm_unplug_srcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) if (dev->unplugged) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) srcu_read_unlock(&drm_unplug_srcu, *idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) EXPORT_SYMBOL(drm_dev_enter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) * drm_dev_exit - Exit device critical section
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) * @idx: index returned from drm_dev_enter()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) * This function marks the end of a section that should not be entered after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * the device has been unplugged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) void drm_dev_exit(int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) srcu_read_unlock(&drm_unplug_srcu, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) EXPORT_SYMBOL(drm_dev_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * drm_dev_unplug - unplug a DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * @dev: DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * This unplugs a hotpluggable DRM device, which makes it inaccessible to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * userspace operations. Entry-points can use drm_dev_enter() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * drm_dev_exit() to protect device resources in a race free manner. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * essentially unregisters the device like drm_dev_unregister(), but can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * called while there are still open users of @dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) void drm_dev_unplug(struct drm_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * After synchronizing any critical read section is guaranteed to see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * the new value of ->unplugged, and any critical section which might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * still have seen the old value of ->unplugged is guaranteed to have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * finished.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) dev->unplugged = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) synchronize_srcu(&drm_unplug_srcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) drm_dev_unregister(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) EXPORT_SYMBOL(drm_dev_unplug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) * DRM internal mount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) * We want to be able to allocate our own "struct address_space" to control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) * stand-alone address_space objects, so we need an underlying inode. As there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) * is no way to allocate an independent inode easily, we need a fake internal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * VFS mount-point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) * frees it again. You are allowed to use iget() and iput() to get references to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) * the inode. But each drm_fs_inode_new() call must be paired with exactly one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) * drm_fs_inode_free() call (which does not have to be the last iput()).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) * between multiple inode-users. You could, technically, call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) * iput(), but this way you'd end up with a new vfsmount for each inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) static int drm_fs_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) static struct vfsmount *drm_fs_mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) static int drm_fs_init_fs_context(struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) return init_pseudo(fc, 0x010203ff) ? 0 : -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) static struct file_system_type drm_fs_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) .name = "drm",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) .init_fs_context = drm_fs_init_fs_context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) .kill_sb = kill_anon_super,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) static struct inode *drm_fs_inode_new(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) return ERR_PTR(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) if (IS_ERR(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) static void drm_fs_inode_free(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) * DOC: component helper usage recommendations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * DRM drivers that drive hardware where a logical device consists of a pile of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) * independent hardware blocks are recommended to use the :ref:`component helper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) * library<component>`. For consistency and better options for code reuse the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) * following guidelines apply:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) * - The entire device initialization procedure should be run from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) * &component_master_ops.master_bind callback, starting with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) * devm_drm_dev_alloc(), then binding all components with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * component_bind_all() and finishing with drm_dev_register().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) * - The opaque pointer passed to all components through component_bind_all()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) * should point at &struct drm_device of the device instance, not some driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) * specific private structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * - The component helper fills the niche where further standardization of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) * interfaces is not practical. When there already is, or will be, a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) * standardized interface like &drm_bridge or &drm_panel, providing its own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) * functions to find such components at driver load time, like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) * drm_of_find_panel_or_bridge(), then the component helper should not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) static void drm_dev_init_release(struct drm_device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) drm_legacy_ctxbitmap_cleanup(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) drm_legacy_remove_map_hash(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) drm_fs_inode_free(dev->anon_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) put_device(dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) /* Prevent use-after-free in drm_managed_release when debugging is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) * enabled. Slightly awkward, but can't really be helped. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) dev->dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) mutex_destroy(&dev->master_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) mutex_destroy(&dev->clientlist_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) mutex_destroy(&dev->filelist_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) mutex_destroy(&dev->struct_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) drm_legacy_destroy_members(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) static int drm_dev_init(struct drm_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) struct drm_driver *driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) if (!drm_core_init_complete) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) DRM_ERROR("DRM core is not initialized\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) if (WARN_ON(!parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) kref_init(&dev->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) dev->dev = get_device(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) dev->driver = driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) INIT_LIST_HEAD(&dev->managed.resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) spin_lock_init(&dev->managed.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) /* no per-device feature limits by default */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) dev->driver_features = ~0u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) drm_legacy_init_members(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) INIT_LIST_HEAD(&dev->filelist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) INIT_LIST_HEAD(&dev->filelist_internal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) INIT_LIST_HEAD(&dev->clientlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) INIT_LIST_HEAD(&dev->vblank_event_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) spin_lock_init(&dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) mutex_init(&dev->struct_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) mutex_init(&dev->filelist_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) mutex_init(&dev->clientlist_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) mutex_init(&dev->master_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) ret = drmm_add_action(dev, drm_dev_init_release, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) inode = drm_fs_inode_new();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) ret = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) dev->anon_inode = inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) if (drm_core_check_feature(dev, DRIVER_RENDER)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) ret = drm_minor_alloc(dev, DRM_MINOR_PRIMARY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) ret = drm_legacy_create_map_hash(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) drm_legacy_ctxbitmap_init(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) if (drm_core_check_feature(dev, DRIVER_GEM)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) ret = drm_gem_init(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) ret = drm_dev_set_unique(dev, dev_name(parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) drm_managed_release(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) static void devm_drm_dev_init_release(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) drm_dev_put(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) static int devm_drm_dev_init(struct device *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) struct drm_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) struct drm_driver *driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) ret = drm_dev_init(dev, driver, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) ret = devm_add_action(parent, devm_drm_dev_init_release, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) devm_drm_dev_init_release(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) void *__devm_drm_dev_alloc(struct device *parent, struct drm_driver *driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) size_t size, size_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) void *container;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) struct drm_device *drm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) container = kzalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) if (!container)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) drm = container + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) ret = devm_drm_dev_init(parent, drm, driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) kfree(container);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) drmm_add_final_kfree(drm, container);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) return container;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) EXPORT_SYMBOL(__devm_drm_dev_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) * drm_dev_alloc - Allocate new DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) * @driver: DRM driver to allocate device for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) * @parent: Parent device object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) * This is the deprecated version of devm_drm_dev_alloc(), which does not support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) * subclassing through embedding the struct &drm_device in a driver private
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) * structure, and which does not support automatic cleanup through devres.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) * Pointer to new DRM device, or ERR_PTR on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) struct drm_device *drm_dev_alloc(struct drm_driver *driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) struct drm_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) dev = kzalloc(sizeof(*dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) ret = drm_dev_init(dev, driver, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) drmm_add_final_kfree(dev, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) EXPORT_SYMBOL(drm_dev_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) static void drm_dev_release(struct kref *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) struct drm_device *dev = container_of(ref, struct drm_device, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if (dev->driver->release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) dev->driver->release(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) drm_managed_release(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) kfree(dev->managed.final_kfree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) * drm_dev_get - Take reference of a DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) * @dev: device to take reference of or NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) * This increases the ref-count of @dev by one. You *must* already own a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) * reference when calling this. Use drm_dev_put() to drop this reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) * again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) * This function never fails. However, this function does not provide *any*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) * guarantee whether the device is alive or running. It only provides a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) * reference to the object and the memory associated with it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) void drm_dev_get(struct drm_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) if (dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) kref_get(&dev->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) EXPORT_SYMBOL(drm_dev_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) * drm_dev_put - Drop reference of a DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) * @dev: device to drop reference of or NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) * This decreases the ref-count of @dev by one. The device is destroyed if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) * ref-count drops to zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) void drm_dev_put(struct drm_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) if (dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) kref_put(&dev->ref, drm_dev_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) EXPORT_SYMBOL(drm_dev_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) static int create_compat_control_link(struct drm_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) struct drm_minor *minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) if (!drm_core_check_feature(dev, DRIVER_MODESET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) if (!minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) * Some existing userspace out there uses the existing of the controlD*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) * sysfs files to figure out whether it's a modeset driver. It only does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) * readdir, hence a symlink is sufficient (and the least confusing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) * option). Otherwise controlD* is entirely unused.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) * Old controlD chardev have been allocated in the range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) * 64-127.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) name = kasprintf(GFP_KERNEL, "controlD%d", minor->index + 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) ret = sysfs_create_link(minor->kdev->kobj.parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) &minor->kdev->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) static void remove_compat_control_link(struct drm_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) struct drm_minor *minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) if (!drm_core_check_feature(dev, DRIVER_MODESET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) if (!minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) name = kasprintf(GFP_KERNEL, "controlD%d", minor->index + 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) sysfs_remove_link(minor->kdev->kobj.parent, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) * drm_dev_register - Register DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) * @dev: Device to register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) * @flags: Flags passed to the driver's .load() function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) * Register the DRM device @dev with the system, advertise device to user-space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) * and start normal device operation. @dev must be initialized via drm_dev_init()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) * previously.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) * Never call this twice on any device!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) * NOTE: To ensure backward compatibility with existing drivers method this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) * function calls the &drm_driver.load method after registering the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) * nodes, creating race conditions. Usage of the &drm_driver.load methods is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) * therefore deprecated, drivers must perform all initialization before calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) * drm_dev_register().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) * 0 on success, negative error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) int drm_dev_register(struct drm_device *dev, unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) struct drm_driver *driver = dev->driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) if (!driver->load)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) drm_mode_config_validate(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) WARN_ON(!dev->managed.final_kfree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) if (drm_dev_needs_global_mutex(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) mutex_lock(&drm_global_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) ret = drm_minor_register(dev, DRM_MINOR_RENDER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) goto err_minors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) ret = drm_minor_register(dev, DRM_MINOR_PRIMARY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) goto err_minors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) ret = create_compat_control_link(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) goto err_minors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) dev->registered = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) if (dev->driver->load) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) ret = dev->driver->load(dev, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) goto err_minors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) if (drm_core_check_feature(dev, DRIVER_MODESET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) drm_modeset_register_all(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) driver->name, driver->major, driver->minor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) driver->patchlevel, driver->date,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) dev->dev ? dev_name(dev->dev) : "virtual device",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) dev->primary->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) err_minors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) remove_compat_control_link(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) drm_minor_unregister(dev, DRM_MINOR_RENDER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) if (drm_dev_needs_global_mutex(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) mutex_unlock(&drm_global_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) EXPORT_SYMBOL(drm_dev_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) * drm_dev_unregister - Unregister DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) * @dev: Device to unregister
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) * Unregister the DRM device from the system. This does the reverse of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) * drm_dev_register() but does not deallocate the device. The caller must call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) * drm_dev_put() to drop their final reference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) * A special form of unregistering for hotpluggable devices is drm_dev_unplug(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) * which can be called while there are still open users of @dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) * This should be called first in the device teardown code to make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) * userspace can't access the device instance any more.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) void drm_dev_unregister(struct drm_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) if (drm_core_check_feature(dev, DRIVER_LEGACY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) drm_lastclose(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) dev->registered = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) drm_client_dev_unregister(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) if (drm_core_check_feature(dev, DRIVER_MODESET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) drm_modeset_unregister_all(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) if (dev->driver->unload)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) dev->driver->unload(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) if (dev->agp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) drm_pci_agp_destroy(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) drm_legacy_rmmaps(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) remove_compat_control_link(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) drm_minor_unregister(dev, DRM_MINOR_RENDER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) EXPORT_SYMBOL(drm_dev_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) * drm_dev_set_unique - Set the unique name of a DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) * @dev: device of which to set the unique name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) * @name: unique name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) * Sets the unique name of a DRM device using the specified string. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) * already done by drm_dev_init(), drivers should only override the default
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) * unique name for backwards compatibility reasons.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) * Return: 0 on success or a negative error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) int drm_dev_set_unique(struct drm_device *dev, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) drmm_kfree(dev, dev->unique);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) dev->unique = drmm_kstrdup(dev, name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) return dev->unique ? 0 : -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) EXPORT_SYMBOL(drm_dev_set_unique);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) * DRM Core
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) * The DRM core module initializes all global DRM objects and makes them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) * available to drivers. Once setup, drivers can probe their respective
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) * devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) * Currently, core management includes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) * - The "DRM-Global" key/value database
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) * - Global ID management for connectors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) * - DRM major number allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) * - DRM minor management
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) * - DRM sysfs class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) * - DRM debugfs root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) * Furthermore, the DRM core provides dynamic char-dev lookups. For each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) * interface registered on a DRM device, you can request minor numbers from DRM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) * core. DRM core takes care of major-number management and char-dev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) * registration. A stub ->open() callback forwards any open() requests to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) * registered minor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) static int drm_stub_open(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) const struct file_operations *new_fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) struct drm_minor *minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) DRM_DEBUG("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) minor = drm_minor_acquire(iminor(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) if (IS_ERR(minor))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) return PTR_ERR(minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) new_fops = fops_get(minor->dev->driver->fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) if (!new_fops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) replace_fops(filp, new_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) if (filp->f_op->open)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) err = filp->f_op->open(inode, filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) drm_minor_release(minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) static const struct file_operations drm_stub_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) .open = drm_stub_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) .llseek = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) static void drm_core_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) unregister_chrdev(DRM_MAJOR, "drm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) debugfs_remove(drm_debugfs_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) drm_sysfs_destroy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) idr_destroy(&drm_minors_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) drm_connector_ida_destroy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) static int __init drm_core_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) drm_connector_ida_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) idr_init(&drm_minors_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) ret = drm_sysfs_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) DRM_ERROR("Cannot create DRM class: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) drm_debugfs_root = debugfs_create_dir("dri", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) ret = register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) drm_core_init_complete = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) DRM_DEBUG("Initialized\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) drm_core_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) module_init(drm_core_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) module_exit(drm_core_exit);