xref: /dragonfly/sys/dev/drm/drm_drv.c (revision fcf17be7)
19edbd4a0SFrançois Tigeot /*
224edb884SFrançois Tigeot  * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
39edbd4a0SFrançois Tigeot  *
424edb884SFrançois Tigeot  * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
57f3c3d6fSHasso Tepper  * All Rights Reserved.
67f3c3d6fSHasso Tepper  *
724edb884SFrançois Tigeot  * Author Rickard E. (Rik) Faith <faith@valinux.com>
824edb884SFrançois Tigeot  *
97f3c3d6fSHasso Tepper  * Permission is hereby granted, free of charge, to any person obtaining a
107f3c3d6fSHasso Tepper  * copy of this software and associated documentation files (the "Software"),
117f3c3d6fSHasso Tepper  * to deal in the Software without restriction, including without limitation
127f3c3d6fSHasso Tepper  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
137f3c3d6fSHasso Tepper  * and/or sell copies of the Software, and to permit persons to whom the
147f3c3d6fSHasso Tepper  * Software is furnished to do so, subject to the following conditions:
157f3c3d6fSHasso Tepper  *
167f3c3d6fSHasso Tepper  * The above copyright notice and this permission notice (including the next
177f3c3d6fSHasso Tepper  * paragraph) shall be included in all copies or substantial portions of the
187f3c3d6fSHasso Tepper  * Software.
197f3c3d6fSHasso Tepper  *
207f3c3d6fSHasso Tepper  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
217f3c3d6fSHasso Tepper  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
227f3c3d6fSHasso Tepper  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
2324edb884SFrançois Tigeot  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
247f3c3d6fSHasso Tepper  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
2524edb884SFrançois Tigeot  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2624edb884SFrançois Tigeot  * DEALINGS IN THE SOFTWARE.
277f3c3d6fSHasso Tepper  */
287f3c3d6fSHasso Tepper 
2924edb884SFrançois Tigeot #include <linux/module.h>
3024edb884SFrançois Tigeot #include <linux/moduleparam.h>
3124edb884SFrançois Tigeot #include <drm/drmP.h>
3224edb884SFrançois Tigeot #include <drm/drm_core.h>
3324edb884SFrançois Tigeot #include "drm_legacy.h"
341b13d190SFrançois Tigeot #include "drm_internal.h"
3524edb884SFrançois Tigeot 
368621f407SFrançois Tigeot /*
378621f407SFrançois Tigeot  * drm_debug: Enable debug output.
388621f407SFrançois Tigeot  * Bitmask of DRM_UT_x. See include/drm/drmP.h for details.
398621f407SFrançois Tigeot  */
4091fff950Szrj #ifdef __DragonFly__
418621f407SFrançois Tigeot /* Provides three levels of debug: off, minimal, verbose */
4291fff950Szrj #if DRM_DEBUG_DEFAULT_ON == 1
4391fff950Szrj #define DRM_DEBUGBITS_ON (DRM_UT_CORE | DRM_UT_DRIVER | DRM_UT_KMS |	\
4491fff950Szrj 			  DRM_UT_PRIME| DRM_UT_ATOMIC | DRM_UT_FIOCTL)
4591fff950Szrj #elif DRM_DEBUG_DEFAULT_ON == 2
4691fff950Szrj #define DRM_DEBUGBITS_ON (DRM_UT_CORE | DRM_UT_DRIVER | DRM_UT_KMS |	\
4791fff950Szrj 			  DRM_UT_PRIME| DRM_UT_ATOMIC | DRM_UT_FIOCTL |	\
4891fff950Szrj 			  DRM_UT_PID  | DRM_UT_IOCTL  | DRM_UT_VBLANK)
4991fff950Szrj #else
5091fff950Szrj #define DRM_DEBUGBITS_ON (0x0)
5191fff950Szrj #endif
5291fff950Szrj unsigned int drm_debug = DRM_DEBUGBITS_ON;	/* defaults to 0 */
5391fff950Szrj #else
548621f407SFrançois Tigeot unsigned int drm_debug = 0;
5591fff950Szrj #endif /* __DragonFly__ */
5624edb884SFrançois Tigeot EXPORT_SYMBOL(drm_debug);
5724edb884SFrançois Tigeot 
5824edb884SFrançois Tigeot MODULE_AUTHOR(CORE_AUTHOR);
5924edb884SFrançois Tigeot MODULE_DESCRIPTION(CORE_DESC);
608621f407SFrançois Tigeot MODULE_PARM_DESC(debug, "Enable debug output, where each bit enables a debug category.\n"
618621f407SFrançois Tigeot "\t\tBit 0 (0x01) will enable CORE messages (drm core code)\n"
628621f407SFrançois Tigeot "\t\tBit 1 (0x02) will enable DRIVER messages (drm controller code)\n"
638621f407SFrançois Tigeot "\t\tBit 2 (0x04) will enable KMS messages (modesetting code)\n"
648621f407SFrançois Tigeot "\t\tBit 3 (0x08) will enable PRIME messages (prime code)\n"
658621f407SFrançois Tigeot "\t\tBit 4 (0x10) will enable ATOMIC messages (atomic code)\n"
668621f407SFrançois Tigeot "\t\tBit 5 (0x20) will enable VBL messages (vblank code)");
6724edb884SFrançois Tigeot module_param_named(debug, drm_debug, int, 0600);
6824edb884SFrançois Tigeot 
69*fcf17be7SFrançois Tigeot static DEFINE_MUTEX(drm_minor_lock);
7024edb884SFrançois Tigeot static struct idr drm_minors_idr;
7124edb884SFrançois Tigeot 
7224edb884SFrançois Tigeot #if 0
7324edb884SFrançois Tigeot static struct dentry *drm_debugfs_root;
7424edb884SFrançois Tigeot #endif
7524edb884SFrançois Tigeot 
7691fff950Szrj void drm_err(const char *func, const char *format, ...)
7724edb884SFrançois Tigeot {
789286b91eSSascha Wildner 	va_list args;
7924edb884SFrançois Tigeot 
8091fff950Szrj 	kprintf("error: [" DRM_NAME ":pid%d:%s] *ERROR* ", DRM_CURRENTPID, func);
8124edb884SFrançois Tigeot 
829286b91eSSascha Wildner 	va_start(args, format);
8391fff950Szrj 	kvprintf(format, args);
849286b91eSSascha Wildner 	va_end(args);
8524edb884SFrançois Tigeot }
8624edb884SFrançois Tigeot EXPORT_SYMBOL(drm_err);
8724edb884SFrançois Tigeot 
8824edb884SFrançois Tigeot void drm_ut_debug_printk(const char *function_name, const char *format, ...)
8924edb884SFrançois Tigeot {
909286b91eSSascha Wildner 	va_list args;
9124edb884SFrançois Tigeot 
9291fff950Szrj 	if (unlikely(drm_debug & DRM_UT_PID)) {
9391fff950Szrj 		kprintf("[" DRM_NAME ":pid%d:%s] ",
9491fff950Szrj 		    DRM_CURRENTPID, function_name);
9591fff950Szrj 	} else {
9691fff950Szrj 		kprintf("[" DRM_NAME ":%s] ", function_name);
9791fff950Szrj 	}
9824edb884SFrançois Tigeot 
999286b91eSSascha Wildner 	va_start(args, format);
10091fff950Szrj 	kvprintf(format, args);
1019286b91eSSascha Wildner 	va_end(args);
10224edb884SFrançois Tigeot }
10324edb884SFrançois Tigeot EXPORT_SYMBOL(drm_ut_debug_printk);
10424edb884SFrançois Tigeot 
10524edb884SFrançois Tigeot #if 0
10624edb884SFrançois Tigeot struct drm_master *drm_master_create(struct drm_minor *minor)
10724edb884SFrançois Tigeot {
10824edb884SFrançois Tigeot 	struct drm_master *master;
10924edb884SFrançois Tigeot 
11024edb884SFrançois Tigeot 	master = kzalloc(sizeof(*master), GFP_KERNEL);
11124edb884SFrançois Tigeot 	if (!master)
11224edb884SFrançois Tigeot 		return NULL;
11324edb884SFrançois Tigeot 
11424edb884SFrançois Tigeot 	kref_init(&master->refcount);
11524edb884SFrançois Tigeot 	spin_lock_init(&master->lock.spinlock);
11624edb884SFrançois Tigeot 	init_waitqueue_head(&master->lock.lock_queue);
11724edb884SFrançois Tigeot 	if (drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER)) {
11824edb884SFrançois Tigeot 		kfree(master);
11924edb884SFrançois Tigeot 		return NULL;
12024edb884SFrançois Tigeot 	}
12124edb884SFrançois Tigeot 	master->minor = minor;
12224edb884SFrançois Tigeot 
12324edb884SFrançois Tigeot 	return master;
12424edb884SFrançois Tigeot }
12524edb884SFrançois Tigeot 
12624edb884SFrançois Tigeot struct drm_master *drm_master_get(struct drm_master *master)
12724edb884SFrançois Tigeot {
12824edb884SFrançois Tigeot 	kref_get(&master->refcount);
12924edb884SFrançois Tigeot 	return master;
13024edb884SFrançois Tigeot }
13124edb884SFrançois Tigeot EXPORT_SYMBOL(drm_master_get);
13224edb884SFrançois Tigeot 
13324edb884SFrançois Tigeot static void drm_master_destroy(struct kref *kref)
13424edb884SFrançois Tigeot {
13524edb884SFrançois Tigeot 	struct drm_master *master = container_of(kref, struct drm_master, refcount);
13624edb884SFrançois Tigeot 	struct drm_device *dev = master->minor->dev;
13724edb884SFrançois Tigeot 	struct drm_map_list *r_list, *list_temp;
13824edb884SFrançois Tigeot 
13924edb884SFrançois Tigeot 	if (dev->driver->master_destroy)
14024edb884SFrançois Tigeot 		dev->driver->master_destroy(dev, master);
14124edb884SFrançois Tigeot 
1428621f407SFrançois Tigeot 	mutex_lock(&dev->struct_mutex);
14324edb884SFrançois Tigeot 	list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
14424edb884SFrançois Tigeot 		if (r_list->master == master) {
1451b13d190SFrançois Tigeot 			drm_legacy_rmmap_locked(dev, r_list->map);
14624edb884SFrançois Tigeot 			r_list = NULL;
14724edb884SFrançois Tigeot 		}
14824edb884SFrançois Tigeot 	}
14924edb884SFrançois Tigeot 
15024edb884SFrançois Tigeot 	if (master->unique) {
15124edb884SFrançois Tigeot 		kfree(master->unique);
15224edb884SFrançois Tigeot 		master->unique = NULL;
15324edb884SFrançois Tigeot 		master->unique_len = 0;
15424edb884SFrançois Tigeot 	}
15524edb884SFrançois Tigeot 
15624edb884SFrançois Tigeot 	drm_ht_remove(&master->magiclist);
15724edb884SFrançois Tigeot 
15824edb884SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
15924edb884SFrançois Tigeot 	kfree(master);
16024edb884SFrançois Tigeot }
16124edb884SFrançois Tigeot 
16224edb884SFrançois Tigeot void drm_master_put(struct drm_master **master)
16324edb884SFrançois Tigeot {
16424edb884SFrançois Tigeot 	kref_put(&(*master)->refcount, drm_master_destroy);
16524edb884SFrançois Tigeot 	*master = NULL;
16624edb884SFrançois Tigeot }
16724edb884SFrançois Tigeot EXPORT_SYMBOL(drm_master_put);
16824edb884SFrançois Tigeot #endif
16924edb884SFrançois Tigeot 
17024edb884SFrançois Tigeot int drm_setmaster_ioctl(struct drm_device *dev, void *data,
17124edb884SFrançois Tigeot 			struct drm_file *file_priv)
17224edb884SFrançois Tigeot {
17324edb884SFrançois Tigeot 	DRM_DEBUG("setmaster\n");
17424edb884SFrançois Tigeot 
17524edb884SFrançois Tigeot 	if (file_priv->master != 0)
17624edb884SFrançois Tigeot 		return (0);
17724edb884SFrançois Tigeot 
17824edb884SFrançois Tigeot 	return (-EPERM);
17924edb884SFrançois Tigeot }
18024edb884SFrançois Tigeot 
18124edb884SFrançois Tigeot int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
18224edb884SFrançois Tigeot 			 struct drm_file *file_priv)
18324edb884SFrançois Tigeot {
18424edb884SFrançois Tigeot 	DRM_DEBUG("dropmaster\n");
18524edb884SFrançois Tigeot 	if (file_priv->master != 0)
18624edb884SFrançois Tigeot 		return -EINVAL;
18724edb884SFrançois Tigeot 	return 0;
18824edb884SFrançois Tigeot }
18924edb884SFrançois Tigeot 
19024edb884SFrançois Tigeot /*
19124edb884SFrançois Tigeot  * DRM Minors
19224edb884SFrançois Tigeot  * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
19324edb884SFrançois Tigeot  * of them is represented by a drm_minor object. Depending on the capabilities
19424edb884SFrançois Tigeot  * of the device-driver, different interfaces are registered.
19524edb884SFrançois Tigeot  *
19624edb884SFrançois Tigeot  * Minors can be accessed via dev->$minor_name. This pointer is either
19724edb884SFrançois Tigeot  * NULL or a valid drm_minor pointer and stays valid as long as the device is
19824edb884SFrançois Tigeot  * valid. This means, DRM minors have the same life-time as the underlying
19924edb884SFrançois Tigeot  * device. However, this doesn't mean that the minor is active. Minors are
20024edb884SFrançois Tigeot  * registered and unregistered dynamically according to device-state.
20124edb884SFrançois Tigeot  */
20224edb884SFrançois Tigeot 
20324edb884SFrançois Tigeot static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
20424edb884SFrançois Tigeot 					     unsigned int type)
20524edb884SFrançois Tigeot {
20624edb884SFrançois Tigeot 	switch (type) {
20724edb884SFrançois Tigeot 	case DRM_MINOR_LEGACY:
20824edb884SFrançois Tigeot 		return &dev->primary;
20924edb884SFrançois Tigeot 	case DRM_MINOR_RENDER:
21024edb884SFrançois Tigeot 		return &dev->render;
21124edb884SFrançois Tigeot 	case DRM_MINOR_CONTROL:
21224edb884SFrançois Tigeot 		return &dev->control;
21324edb884SFrançois Tigeot 	default:
21424edb884SFrançois Tigeot 		return NULL;
21524edb884SFrançois Tigeot 	}
21624edb884SFrançois Tigeot }
21724edb884SFrançois Tigeot 
21824edb884SFrançois Tigeot static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
21924edb884SFrançois Tigeot {
22024edb884SFrançois Tigeot 	struct drm_minor *minor;
22124edb884SFrançois Tigeot 	unsigned long flags;
22224edb884SFrançois Tigeot 	int r;
22324edb884SFrançois Tigeot 
22424edb884SFrançois Tigeot 	minor = kzalloc(sizeof(*minor), GFP_KERNEL);
22524edb884SFrançois Tigeot 	if (!minor)
22624edb884SFrançois Tigeot 		return -ENOMEM;
22724edb884SFrançois Tigeot 
22824edb884SFrançois Tigeot 	minor->type = type;
22924edb884SFrançois Tigeot 	minor->dev = dev;
23024edb884SFrançois Tigeot 
23124edb884SFrançois Tigeot 	idr_preload(GFP_KERNEL);
23224edb884SFrançois Tigeot 	spin_lock_irqsave(&drm_minor_lock, flags);
23324edb884SFrançois Tigeot 	r = idr_alloc(&drm_minors_idr,
23424edb884SFrançois Tigeot 		      NULL,
23524edb884SFrançois Tigeot 		      64 * type,
23624edb884SFrançois Tigeot 		      64 * (type + 1),
23724edb884SFrançois Tigeot 		      GFP_NOWAIT);
23824edb884SFrançois Tigeot 	spin_unlock_irqrestore(&drm_minor_lock, flags);
23924edb884SFrançois Tigeot 	idr_preload_end();
24024edb884SFrançois Tigeot 
24124edb884SFrançois Tigeot 	if (r < 0)
24224edb884SFrançois Tigeot 		goto err_free;
24324edb884SFrançois Tigeot 
24424edb884SFrançois Tigeot 	minor->index = r;
24524edb884SFrançois Tigeot 
246*fcf17be7SFrançois Tigeot #if 0
24724edb884SFrançois Tigeot 	minor->kdev = drm_sysfs_minor_alloc(minor);
24824edb884SFrançois Tigeot 	if (IS_ERR(minor->kdev)) {
24924edb884SFrançois Tigeot 		r = PTR_ERR(minor->kdev);
25024edb884SFrançois Tigeot 		goto err_index;
25124edb884SFrançois Tigeot 	}
252*fcf17be7SFrançois Tigeot #endif
25324edb884SFrançois Tigeot 
25424edb884SFrançois Tigeot 	*drm_minor_get_slot(dev, type) = minor;
25524edb884SFrançois Tigeot 	return 0;
25624edb884SFrançois Tigeot 
257*fcf17be7SFrançois Tigeot #if 0
25824edb884SFrançois Tigeot err_index:
25924edb884SFrançois Tigeot 	spin_lock_irqsave(&drm_minor_lock, flags);
26024edb884SFrançois Tigeot 	idr_remove(&drm_minors_idr, minor->index);
26124edb884SFrançois Tigeot 	spin_unlock_irqrestore(&drm_minor_lock, flags);
262*fcf17be7SFrançois Tigeot #endif
26324edb884SFrançois Tigeot err_free:
26424edb884SFrançois Tigeot 	kfree(minor);
26524edb884SFrançois Tigeot 	return r;
26624edb884SFrançois Tigeot }
26724edb884SFrançois Tigeot 
26824edb884SFrançois Tigeot static void drm_minor_free(struct drm_device *dev, unsigned int type)
26924edb884SFrançois Tigeot {
27024edb884SFrançois Tigeot 	struct drm_minor **slot, *minor;
27124edb884SFrançois Tigeot 	unsigned long flags;
27224edb884SFrançois Tigeot 
27324edb884SFrançois Tigeot 	slot = drm_minor_get_slot(dev, type);
27424edb884SFrançois Tigeot 	minor = *slot;
27524edb884SFrançois Tigeot 	if (!minor)
27624edb884SFrançois Tigeot 		return;
27724edb884SFrançois Tigeot 
278*fcf17be7SFrançois Tigeot #if 0
27924edb884SFrançois Tigeot 	put_device(minor->kdev);
280*fcf17be7SFrançois Tigeot #endif
28124edb884SFrançois Tigeot 
28224edb884SFrançois Tigeot 	spin_lock_irqsave(&drm_minor_lock, flags);
28324edb884SFrançois Tigeot 	idr_remove(&drm_minors_idr, minor->index);
28424edb884SFrançois Tigeot 	spin_unlock_irqrestore(&drm_minor_lock, flags);
28524edb884SFrançois Tigeot 
28624edb884SFrançois Tigeot 	kfree(minor);
28724edb884SFrançois Tigeot 	*slot = NULL;
28824edb884SFrançois Tigeot }
28924edb884SFrançois Tigeot 
29024edb884SFrançois Tigeot static int drm_minor_register(struct drm_device *dev, unsigned int type)
29124edb884SFrançois Tigeot {
29224edb884SFrançois Tigeot 	struct drm_minor *minor;
29324edb884SFrançois Tigeot 	unsigned long flags;
294*fcf17be7SFrançois Tigeot #if 0
29524edb884SFrançois Tigeot 	int ret;
296*fcf17be7SFrançois Tigeot #endif
29724edb884SFrançois Tigeot 
29824edb884SFrançois Tigeot 	DRM_DEBUG("\n");
29924edb884SFrançois Tigeot 
30024edb884SFrançois Tigeot 	minor = *drm_minor_get_slot(dev, type);
30124edb884SFrançois Tigeot 	if (!minor)
30224edb884SFrançois Tigeot 		return 0;
30324edb884SFrançois Tigeot 
304*fcf17be7SFrançois Tigeot #if 0
30524edb884SFrançois Tigeot 	ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root);
30624edb884SFrançois Tigeot 	if (ret) {
30724edb884SFrançois Tigeot 		DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
30824edb884SFrançois Tigeot 		return ret;
30924edb884SFrançois Tigeot 	}
31024edb884SFrançois Tigeot 
31124edb884SFrançois Tigeot 	ret = device_add(minor->kdev);
31224edb884SFrançois Tigeot 	if (ret)
31324edb884SFrançois Tigeot 		goto err_debugfs;
314*fcf17be7SFrançois Tigeot #endif
31524edb884SFrançois Tigeot 
31624edb884SFrançois Tigeot 	/* replace NULL with @minor so lookups will succeed from now on */
31724edb884SFrançois Tigeot 	spin_lock_irqsave(&drm_minor_lock, flags);
31824edb884SFrançois Tigeot 	idr_replace(&drm_minors_idr, minor, minor->index);
31924edb884SFrançois Tigeot 	spin_unlock_irqrestore(&drm_minor_lock, flags);
32024edb884SFrançois Tigeot 
32124edb884SFrançois Tigeot 	DRM_DEBUG("new minor registered %d\n", minor->index);
32224edb884SFrançois Tigeot 	return 0;
32324edb884SFrançois Tigeot 
324*fcf17be7SFrançois Tigeot #if 0
32524edb884SFrançois Tigeot err_debugfs:
32624edb884SFrançois Tigeot 	drm_debugfs_cleanup(minor);
32724edb884SFrançois Tigeot 	return ret;
328*fcf17be7SFrançois Tigeot #endif
32924edb884SFrançois Tigeot }
33024edb884SFrançois Tigeot 
33124edb884SFrançois Tigeot static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
33224edb884SFrançois Tigeot {
33324edb884SFrançois Tigeot 	struct drm_minor *minor;
33424edb884SFrançois Tigeot 	unsigned long flags;
33524edb884SFrançois Tigeot 
33624edb884SFrançois Tigeot 	minor = *drm_minor_get_slot(dev, type);
337*fcf17be7SFrançois Tigeot #if 0
33824edb884SFrançois Tigeot 	if (!minor || !device_is_registered(minor->kdev))
339*fcf17be7SFrançois Tigeot #else
340*fcf17be7SFrançois Tigeot 	if (!minor)
341*fcf17be7SFrançois Tigeot #endif
34224edb884SFrançois Tigeot 		return;
34324edb884SFrançois Tigeot 
34424edb884SFrançois Tigeot 	/* replace @minor with NULL so lookups will fail from now on */
34524edb884SFrançois Tigeot 	spin_lock_irqsave(&drm_minor_lock, flags);
34624edb884SFrançois Tigeot 	idr_replace(&drm_minors_idr, NULL, minor->index);
34724edb884SFrançois Tigeot 	spin_unlock_irqrestore(&drm_minor_lock, flags);
34824edb884SFrançois Tigeot 
349*fcf17be7SFrançois Tigeot #if 0
35024edb884SFrançois Tigeot 	device_del(minor->kdev);
35124edb884SFrançois Tigeot 	dev_set_drvdata(minor->kdev, NULL); /* safety belt */
352*fcf17be7SFrançois Tigeot #endif
35324edb884SFrançois Tigeot 	drm_debugfs_cleanup(minor);
35424edb884SFrançois Tigeot }
35524edb884SFrançois Tigeot 
356*fcf17be7SFrançois Tigeot #if 0
35724edb884SFrançois Tigeot /**
35824edb884SFrançois Tigeot  * drm_minor_acquire - Acquire a DRM minor
35924edb884SFrançois Tigeot  * @minor_id: Minor ID of the DRM-minor
36024edb884SFrançois Tigeot  *
36124edb884SFrançois Tigeot  * Looks up the given minor-ID and returns the respective DRM-minor object. The
36224edb884SFrançois Tigeot  * refence-count of the underlying device is increased so you must release this
36324edb884SFrançois Tigeot  * object with drm_minor_release().
36424edb884SFrançois Tigeot  *
36524edb884SFrançois Tigeot  * As long as you hold this minor, it is guaranteed that the object and the
36624edb884SFrançois Tigeot  * minor->dev pointer will stay valid! However, the device may get unplugged and
36724edb884SFrançois Tigeot  * unregistered while you hold the minor.
36824edb884SFrançois Tigeot  *
36924edb884SFrançois Tigeot  * Returns:
37024edb884SFrançois Tigeot  * Pointer to minor-object with increased device-refcount, or PTR_ERR on
37124edb884SFrançois Tigeot  * failure.
37224edb884SFrançois Tigeot  */
37324edb884SFrançois Tigeot struct drm_minor *drm_minor_acquire(unsigned int minor_id)
37424edb884SFrançois Tigeot {
37524edb884SFrançois Tigeot 	struct drm_minor *minor;
37624edb884SFrançois Tigeot 	unsigned long flags;
37724edb884SFrançois Tigeot 
37824edb884SFrançois Tigeot 	spin_lock_irqsave(&drm_minor_lock, flags);
37924edb884SFrançois Tigeot 	minor = idr_find(&drm_minors_idr, minor_id);
38024edb884SFrançois Tigeot 	if (minor)
38124edb884SFrançois Tigeot 		drm_dev_ref(minor->dev);
38224edb884SFrançois Tigeot 	spin_unlock_irqrestore(&drm_minor_lock, flags);
38324edb884SFrançois Tigeot 
38424edb884SFrançois Tigeot 	if (!minor) {
38524edb884SFrançois Tigeot 		return ERR_PTR(-ENODEV);
38624edb884SFrançois Tigeot 	} else if (drm_device_is_unplugged(minor->dev)) {
38724edb884SFrançois Tigeot 		drm_dev_unref(minor->dev);
38824edb884SFrançois Tigeot 		return ERR_PTR(-ENODEV);
38924edb884SFrançois Tigeot 	}
39024edb884SFrançois Tigeot 
39124edb884SFrançois Tigeot 	return minor;
39224edb884SFrançois Tigeot }
39324edb884SFrançois Tigeot 
39424edb884SFrançois Tigeot /**
39524edb884SFrançois Tigeot  * drm_minor_release - Release DRM minor
39624edb884SFrançois Tigeot  * @minor: Pointer to DRM minor object
39724edb884SFrançois Tigeot  *
39824edb884SFrançois Tigeot  * Release a minor that was previously acquired via drm_minor_acquire().
39924edb884SFrançois Tigeot  */
40024edb884SFrançois Tigeot void drm_minor_release(struct drm_minor *minor)
40124edb884SFrançois Tigeot {
40224edb884SFrançois Tigeot 	drm_dev_unref(minor->dev);
40324edb884SFrançois Tigeot }
40424edb884SFrançois Tigeot 
40524edb884SFrançois Tigeot /**
406352ff8bdSFrançois Tigeot  * DOC: driver instance overview
407352ff8bdSFrançois Tigeot  *
408352ff8bdSFrançois Tigeot  * A device instance for a drm driver is represented by struct &drm_device. This
409352ff8bdSFrançois Tigeot  * is allocated with drm_dev_alloc(), usually from bus-specific ->probe()
410352ff8bdSFrançois Tigeot  * callbacks implemented by the driver. The driver then needs to initialize all
411352ff8bdSFrançois Tigeot  * the various subsystems for the drm device like memory management, vblank
412352ff8bdSFrançois Tigeot  * handling, modesetting support and intial output configuration plus obviously
413352ff8bdSFrançois Tigeot  * initialize all the corresponding hardware bits. An important part of this is
414352ff8bdSFrançois Tigeot  * also calling drm_dev_set_unique() to set the userspace-visible unique name of
415352ff8bdSFrançois Tigeot  * this device instance. Finally when everything is up and running and ready for
416352ff8bdSFrançois Tigeot  * userspace the device instance can be published using drm_dev_register().
417352ff8bdSFrançois Tigeot  *
418352ff8bdSFrançois Tigeot  * There is also deprecated support for initalizing device instances using
419352ff8bdSFrançois Tigeot  * bus-specific helpers and the ->load() callback. But due to
420352ff8bdSFrançois Tigeot  * backwards-compatibility needs the device instance have to be published too
421352ff8bdSFrançois Tigeot  * early, which requires unpretty global locking to make safe and is therefore
422352ff8bdSFrançois Tigeot  * only support for existing drivers not yet converted to the new scheme.
423352ff8bdSFrançois Tigeot  *
424352ff8bdSFrançois Tigeot  * When cleaning up a device instance everything needs to be done in reverse:
425352ff8bdSFrançois Tigeot  * First unpublish the device instance with drm_dev_unregister(). Then clean up
426352ff8bdSFrançois Tigeot  * any other resources allocated at device initialization and drop the driver's
427352ff8bdSFrançois Tigeot  * reference to &drm_device using drm_dev_unref().
428352ff8bdSFrançois Tigeot  *
429352ff8bdSFrançois Tigeot  * Note that the lifetime rules for &drm_device instance has still a lot of
430352ff8bdSFrançois Tigeot  * historical baggage. Hence use the reference counting provided by
431352ff8bdSFrançois Tigeot  * drm_dev_ref() and drm_dev_unref() only carefully.
432352ff8bdSFrançois Tigeot  *
433352ff8bdSFrançois Tigeot  * Also note that embedding of &drm_device is currently not (yet) supported (but
434352ff8bdSFrançois Tigeot  * it would be easy to add). Drivers can store driver-private data in the
435352ff8bdSFrançois Tigeot  * dev_priv field of &drm_device.
436352ff8bdSFrançois Tigeot  */
437352ff8bdSFrançois Tigeot 
438352ff8bdSFrançois Tigeot /**
43924edb884SFrançois Tigeot  * drm_put_dev - Unregister and release a DRM device
44024edb884SFrançois Tigeot  * @dev: DRM device
44124edb884SFrançois Tigeot  *
44224edb884SFrançois Tigeot  * Called at module unload time or when a PCI device is unplugged.
44324edb884SFrançois Tigeot  *
44424edb884SFrançois Tigeot  * Cleans up all DRM device, calling drm_lastclose().
445352ff8bdSFrançois Tigeot  *
446352ff8bdSFrançois Tigeot  * Note: Use of this function is deprecated. It will eventually go away
447352ff8bdSFrançois Tigeot  * completely.  Please use drm_dev_unregister() and drm_dev_unref() explicitly
448352ff8bdSFrançois Tigeot  * instead to make sure that the device isn't userspace accessible any more
449352ff8bdSFrançois Tigeot  * while teardown is in progress, ensuring that userspace can't access an
450352ff8bdSFrançois Tigeot  * inconsistent state.
45124edb884SFrançois Tigeot  */
45224edb884SFrançois Tigeot void drm_put_dev(struct drm_device *dev)
45324edb884SFrançois Tigeot {
45424edb884SFrançois Tigeot 	DRM_DEBUG("\n");
45524edb884SFrançois Tigeot 
45624edb884SFrançois Tigeot 	if (!dev) {
45724edb884SFrançois Tigeot 		DRM_ERROR("cleanup called no dev\n");
45824edb884SFrançois Tigeot 		return;
45924edb884SFrançois Tigeot 	}
46024edb884SFrançois Tigeot 
46124edb884SFrançois Tigeot 	drm_dev_unregister(dev);
46224edb884SFrançois Tigeot 	drm_dev_unref(dev);
46324edb884SFrançois Tigeot }
46424edb884SFrançois Tigeot EXPORT_SYMBOL(drm_put_dev);
46524edb884SFrançois Tigeot 
46624edb884SFrançois Tigeot void drm_unplug_dev(struct drm_device *dev)
46724edb884SFrançois Tigeot {
46824edb884SFrançois Tigeot 	/* for a USB device */
46924edb884SFrançois Tigeot 	drm_minor_unregister(dev, DRM_MINOR_LEGACY);
47024edb884SFrançois Tigeot 	drm_minor_unregister(dev, DRM_MINOR_RENDER);
47124edb884SFrançois Tigeot 	drm_minor_unregister(dev, DRM_MINOR_CONTROL);
47224edb884SFrançois Tigeot 
47324edb884SFrançois Tigeot 	mutex_lock(&drm_global_mutex);
47424edb884SFrançois Tigeot 
47524edb884SFrançois Tigeot 	drm_device_set_unplugged(dev);
47624edb884SFrançois Tigeot 
47724edb884SFrançois Tigeot 	if (dev->open_count == 0) {
47824edb884SFrançois Tigeot 		drm_put_dev(dev);
47924edb884SFrançois Tigeot 	}
48024edb884SFrançois Tigeot 	mutex_unlock(&drm_global_mutex);
48124edb884SFrançois Tigeot }
48224edb884SFrançois Tigeot EXPORT_SYMBOL(drm_unplug_dev);
48324edb884SFrançois Tigeot 
48424edb884SFrançois Tigeot /*
48524edb884SFrançois Tigeot  * DRM internal mount
48624edb884SFrançois Tigeot  * We want to be able to allocate our own "struct address_space" to control
48724edb884SFrançois Tigeot  * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
48824edb884SFrançois Tigeot  * stand-alone address_space objects, so we need an underlying inode. As there
48924edb884SFrançois Tigeot  * is no way to allocate an independent inode easily, we need a fake internal
49024edb884SFrançois Tigeot  * VFS mount-point.
49124edb884SFrançois Tigeot  *
49224edb884SFrançois Tigeot  * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
49324edb884SFrançois Tigeot  * frees it again. You are allowed to use iget() and iput() to get references to
49424edb884SFrançois Tigeot  * the inode. But each drm_fs_inode_new() call must be paired with exactly one
49524edb884SFrançois Tigeot  * drm_fs_inode_free() call (which does not have to be the last iput()).
49624edb884SFrançois Tigeot  * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
49724edb884SFrançois Tigeot  * between multiple inode-users. You could, technically, call
49824edb884SFrançois Tigeot  * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
49924edb884SFrançois Tigeot  * iput(), but this way you'd end up with a new vfsmount for each inode.
50024edb884SFrançois Tigeot  */
50124edb884SFrançois Tigeot 
50224edb884SFrançois Tigeot static int drm_fs_cnt;
50324edb884SFrançois Tigeot static struct vfsmount *drm_fs_mnt;
50424edb884SFrançois Tigeot 
50524edb884SFrançois Tigeot static const struct dentry_operations drm_fs_dops = {
50624edb884SFrançois Tigeot 	.d_dname	= simple_dname,
50724edb884SFrançois Tigeot };
50824edb884SFrançois Tigeot 
50924edb884SFrançois Tigeot static const struct super_operations drm_fs_sops = {
51024edb884SFrançois Tigeot 	.statfs		= simple_statfs,
51124edb884SFrançois Tigeot };
51224edb884SFrançois Tigeot 
51324edb884SFrançois Tigeot static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
51424edb884SFrançois Tigeot 				   const char *dev_name, void *data)
51524edb884SFrançois Tigeot {
51624edb884SFrançois Tigeot 	return mount_pseudo(fs_type,
51724edb884SFrançois Tigeot 			    "drm:",
51824edb884SFrançois Tigeot 			    &drm_fs_sops,
51924edb884SFrançois Tigeot 			    &drm_fs_dops,
52024edb884SFrançois Tigeot 			    0x010203ff);
52124edb884SFrançois Tigeot }
52224edb884SFrançois Tigeot 
52324edb884SFrançois Tigeot static struct file_system_type drm_fs_type = {
52424edb884SFrançois Tigeot 	.name		= "drm",
52524edb884SFrançois Tigeot 	.owner		= THIS_MODULE,
52624edb884SFrançois Tigeot 	.mount		= drm_fs_mount,
52724edb884SFrançois Tigeot 	.kill_sb	= kill_anon_super,
52824edb884SFrançois Tigeot };
52924edb884SFrançois Tigeot 
53024edb884SFrançois Tigeot static struct inode *drm_fs_inode_new(void)
53124edb884SFrançois Tigeot {
53224edb884SFrançois Tigeot 	struct inode *inode;
53324edb884SFrançois Tigeot 	int r;
53424edb884SFrançois Tigeot 
53524edb884SFrançois Tigeot 	r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
53624edb884SFrançois Tigeot 	if (r < 0) {
53724edb884SFrançois Tigeot 		DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
53824edb884SFrançois Tigeot 		return ERR_PTR(r);
53924edb884SFrançois Tigeot 	}
54024edb884SFrançois Tigeot 
54124edb884SFrançois Tigeot 	inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
54224edb884SFrançois Tigeot 	if (IS_ERR(inode))
54324edb884SFrançois Tigeot 		simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
54424edb884SFrançois Tigeot 
54524edb884SFrançois Tigeot 	return inode;
54624edb884SFrançois Tigeot }
54724edb884SFrançois Tigeot 
54824edb884SFrançois Tigeot static void drm_fs_inode_free(struct inode *inode)
54924edb884SFrançois Tigeot {
55024edb884SFrançois Tigeot 	if (inode) {
55124edb884SFrançois Tigeot 		iput(inode);
55224edb884SFrançois Tigeot 		simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
55324edb884SFrançois Tigeot 	}
55424edb884SFrançois Tigeot }
555*fcf17be7SFrançois Tigeot #endif
55624edb884SFrançois Tigeot 
55724edb884SFrançois Tigeot /**
55824edb884SFrançois Tigeot  * drm_dev_alloc - Allocate new DRM device
55924edb884SFrançois Tigeot  * @driver: DRM driver to allocate device for
56024edb884SFrançois Tigeot  * @parent: Parent device object
56124edb884SFrançois Tigeot  *
56224edb884SFrançois Tigeot  * Allocate and initialize a new DRM device. No device registration is done.
56324edb884SFrançois Tigeot  * Call drm_dev_register() to advertice the device to user space and register it
564352ff8bdSFrançois Tigeot  * with other core subsystems. This should be done last in the device
565352ff8bdSFrançois Tigeot  * initialization sequence to make sure userspace can't access an inconsistent
566352ff8bdSFrançois Tigeot  * state.
56724edb884SFrançois Tigeot  *
56824edb884SFrançois Tigeot  * The initial ref-count of the object is 1. Use drm_dev_ref() and
56924edb884SFrançois Tigeot  * drm_dev_unref() to take and drop further ref-counts.
57024edb884SFrançois Tigeot  *
5712c9916cdSFrançois Tigeot  * Note that for purely virtual devices @parent can be NULL.
5722c9916cdSFrançois Tigeot  *
57324edb884SFrançois Tigeot  * RETURNS:
57424edb884SFrançois Tigeot  * Pointer to new DRM device, or NULL if out of memory.
57524edb884SFrançois Tigeot  */
57624edb884SFrançois Tigeot struct drm_device *drm_dev_alloc(struct drm_driver *driver,
57724edb884SFrançois Tigeot 				 struct device *parent)
57824edb884SFrançois Tigeot {
57924edb884SFrançois Tigeot 	struct drm_device *dev;
58024edb884SFrançois Tigeot 	int ret;
58124edb884SFrançois Tigeot 
58224edb884SFrançois Tigeot 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
58324edb884SFrançois Tigeot 	if (!dev)
58424edb884SFrançois Tigeot 		return NULL;
58524edb884SFrançois Tigeot 
58624edb884SFrançois Tigeot 	kref_init(&dev->ref);
58724edb884SFrançois Tigeot 	dev->dev = parent;
58824edb884SFrançois Tigeot 	dev->driver = driver;
58924edb884SFrançois Tigeot 
59024edb884SFrançois Tigeot 	INIT_LIST_HEAD(&dev->filelist);
59124edb884SFrançois Tigeot 	INIT_LIST_HEAD(&dev->ctxlist);
59224edb884SFrançois Tigeot 	INIT_LIST_HEAD(&dev->vmalist);
59324edb884SFrançois Tigeot 	INIT_LIST_HEAD(&dev->maplist);
59424edb884SFrançois Tigeot 	INIT_LIST_HEAD(&dev->vblank_event_list);
59524edb884SFrançois Tigeot 
596*fcf17be7SFrançois Tigeot 	spin_init(&dev->buf_lock, "drmdbl");
597*fcf17be7SFrançois Tigeot 	lockinit(&dev->event_lock, "drmev", 0, LK_CANRECURSE);
598*fcf17be7SFrançois Tigeot 	lockinit(&dev->struct_mutex, "drmslk", 0, LK_CANRECURSE);
599*fcf17be7SFrançois Tigeot 	lockinit(&dev->filelist_mutex, "drmflm", 0, LK_CANRECURSE);
600*fcf17be7SFrançois Tigeot 	lockinit(&dev->ctxlist_mutex, "drmclm", 0, LK_CANRECURSE);
601*fcf17be7SFrançois Tigeot 	lockinit(&dev->master_mutex, "drmmm", 0, LK_CANRECURSE);
60224edb884SFrançois Tigeot 
603*fcf17be7SFrançois Tigeot #ifndef __DragonFly__
60424edb884SFrançois Tigeot 	dev->anon_inode = drm_fs_inode_new();
60524edb884SFrançois Tigeot 	if (IS_ERR(dev->anon_inode)) {
60624edb884SFrançois Tigeot 		ret = PTR_ERR(dev->anon_inode);
60724edb884SFrançois Tigeot 		DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
60824edb884SFrançois Tigeot 		goto err_free;
60924edb884SFrançois Tigeot 	}
610*fcf17be7SFrançois Tigeot #else
611*fcf17be7SFrançois Tigeot 	dev->anon_inode = NULL;
612*fcf17be7SFrançois Tigeot #endif
61324edb884SFrançois Tigeot 
61424edb884SFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
61524edb884SFrançois Tigeot 		ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL);
61624edb884SFrançois Tigeot 		if (ret)
61724edb884SFrançois Tigeot 			goto err_minors;
618352ff8bdSFrançois Tigeot 
619352ff8bdSFrançois Tigeot 		WARN_ON(driver->suspend || driver->resume);
62024edb884SFrançois Tigeot 	}
62124edb884SFrançois Tigeot 
62224edb884SFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_RENDER)) {
62324edb884SFrançois Tigeot 		ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
62424edb884SFrançois Tigeot 		if (ret)
62524edb884SFrançois Tigeot 			goto err_minors;
62624edb884SFrançois Tigeot 	}
62724edb884SFrançois Tigeot 
62824edb884SFrançois Tigeot 	ret = drm_minor_alloc(dev, DRM_MINOR_LEGACY);
62924edb884SFrançois Tigeot 	if (ret)
63024edb884SFrançois Tigeot 		goto err_minors;
63124edb884SFrançois Tigeot 
63224edb884SFrançois Tigeot 	if (drm_ht_create(&dev->map_hash, 12))
63324edb884SFrançois Tigeot 		goto err_minors;
63424edb884SFrançois Tigeot 
635a05eeebfSFrançois Tigeot 	drm_legacy_ctxbitmap_init(dev);
63624edb884SFrançois Tigeot 
6371b13d190SFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_GEM)) {
63824edb884SFrançois Tigeot 		ret = drm_gem_init(dev);
63924edb884SFrançois Tigeot 		if (ret) {
64024edb884SFrançois Tigeot 			DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
64124edb884SFrançois Tigeot 			goto err_ctxbitmap;
64224edb884SFrançois Tigeot 		}
64324edb884SFrançois Tigeot 	}
64424edb884SFrançois Tigeot 
645*fcf17be7SFrançois Tigeot #if 0
646aee94f86SFrançois Tigeot 	if (parent) {
647aee94f86SFrançois Tigeot 		ret = drm_dev_set_unique(dev, dev_name(parent));
648aee94f86SFrançois Tigeot 		if (ret)
649aee94f86SFrançois Tigeot 			goto err_setunique;
650aee94f86SFrançois Tigeot 	}
651*fcf17be7SFrançois Tigeot #endif
652aee94f86SFrançois Tigeot 
65324edb884SFrançois Tigeot 	return dev;
65424edb884SFrançois Tigeot 
655*fcf17be7SFrançois Tigeot #if 0
656aee94f86SFrançois Tigeot err_setunique:
657aee94f86SFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_GEM))
658aee94f86SFrançois Tigeot 		drm_gem_destroy(dev);
659*fcf17be7SFrançois Tigeot #endif
66024edb884SFrançois Tigeot err_ctxbitmap:
66124edb884SFrançois Tigeot 	drm_legacy_ctxbitmap_cleanup(dev);
66224edb884SFrançois Tigeot 	drm_ht_remove(&dev->map_hash);
66324edb884SFrançois Tigeot err_minors:
66424edb884SFrançois Tigeot 	drm_minor_free(dev, DRM_MINOR_LEGACY);
66524edb884SFrançois Tigeot 	drm_minor_free(dev, DRM_MINOR_RENDER);
66624edb884SFrançois Tigeot 	drm_minor_free(dev, DRM_MINOR_CONTROL);
667*fcf17be7SFrançois Tigeot #ifndef __DragonFly__
66824edb884SFrançois Tigeot 	drm_fs_inode_free(dev->anon_inode);
66924edb884SFrançois Tigeot err_free:
670*fcf17be7SFrançois Tigeot #endif
67124edb884SFrançois Tigeot 	mutex_destroy(&dev->master_mutex);
67224edb884SFrançois Tigeot 	kfree(dev);
67324edb884SFrançois Tigeot 	return NULL;
67424edb884SFrançois Tigeot }
67524edb884SFrançois Tigeot EXPORT_SYMBOL(drm_dev_alloc);
67624edb884SFrançois Tigeot 
677*fcf17be7SFrançois Tigeot #if 0
67824edb884SFrançois Tigeot static void drm_dev_release(struct kref *ref)
67924edb884SFrançois Tigeot {
68024edb884SFrançois Tigeot 	struct drm_device *dev = container_of(ref, struct drm_device, ref);
68124edb884SFrançois Tigeot 
6821b13d190SFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_GEM))
68324edb884SFrançois Tigeot 		drm_gem_destroy(dev);
68424edb884SFrançois Tigeot 
68524edb884SFrançois Tigeot 	drm_legacy_ctxbitmap_cleanup(dev);
68624edb884SFrançois Tigeot 	drm_ht_remove(&dev->map_hash);
68724edb884SFrançois Tigeot 	drm_fs_inode_free(dev->anon_inode);
68824edb884SFrançois Tigeot 
68924edb884SFrançois Tigeot 	drm_minor_free(dev, DRM_MINOR_LEGACY);
69024edb884SFrançois Tigeot 	drm_minor_free(dev, DRM_MINOR_RENDER);
69124edb884SFrançois Tigeot 	drm_minor_free(dev, DRM_MINOR_CONTROL);
69224edb884SFrançois Tigeot 
69324edb884SFrançois Tigeot 	mutex_destroy(&dev->master_mutex);
69424edb884SFrançois Tigeot 	kfree(dev->unique);
69524edb884SFrançois Tigeot 	kfree(dev);
69624edb884SFrançois Tigeot }
69724edb884SFrançois Tigeot 
69824edb884SFrançois Tigeot /**
69924edb884SFrançois Tigeot  * drm_dev_ref - Take reference of a DRM device
70024edb884SFrançois Tigeot  * @dev: device to take reference of or NULL
70124edb884SFrançois Tigeot  *
70224edb884SFrançois Tigeot  * This increases the ref-count of @dev by one. You *must* already own a
70324edb884SFrançois Tigeot  * reference when calling this. Use drm_dev_unref() to drop this reference
70424edb884SFrançois Tigeot  * again.
70524edb884SFrançois Tigeot  *
70624edb884SFrançois Tigeot  * This function never fails. However, this function does not provide *any*
70724edb884SFrançois Tigeot  * guarantee whether the device is alive or running. It only provides a
70824edb884SFrançois Tigeot  * reference to the object and the memory associated with it.
70924edb884SFrançois Tigeot  */
71024edb884SFrançois Tigeot void drm_dev_ref(struct drm_device *dev)
71124edb884SFrançois Tigeot {
71224edb884SFrançois Tigeot 	if (dev)
71324edb884SFrançois Tigeot 		kref_get(&dev->ref);
71424edb884SFrançois Tigeot }
71524edb884SFrançois Tigeot EXPORT_SYMBOL(drm_dev_ref);
71624edb884SFrançois Tigeot 
71724edb884SFrançois Tigeot /**
71824edb884SFrançois Tigeot  * drm_dev_unref - Drop reference of a DRM device
71924edb884SFrançois Tigeot  * @dev: device to drop reference of or NULL
72024edb884SFrançois Tigeot  *
72124edb884SFrançois Tigeot  * This decreases the ref-count of @dev by one. The device is destroyed if the
72224edb884SFrançois Tigeot  * ref-count drops to zero.
72324edb884SFrançois Tigeot  */
72424edb884SFrançois Tigeot void drm_dev_unref(struct drm_device *dev)
72524edb884SFrançois Tigeot {
72624edb884SFrançois Tigeot 	if (dev)
72724edb884SFrançois Tigeot 		kref_put(&dev->ref, drm_dev_release);
72824edb884SFrançois Tigeot }
72924edb884SFrançois Tigeot EXPORT_SYMBOL(drm_dev_unref);
730*fcf17be7SFrançois Tigeot #endif
73124edb884SFrançois Tigeot 
73224edb884SFrançois Tigeot /**
73324edb884SFrançois Tigeot  * drm_dev_register - Register DRM device
73424edb884SFrançois Tigeot  * @dev: Device to register
73524edb884SFrançois Tigeot  * @flags: Flags passed to the driver's .load() function
73624edb884SFrançois Tigeot  *
73724edb884SFrançois Tigeot  * Register the DRM device @dev with the system, advertise device to user-space
73824edb884SFrançois Tigeot  * and start normal device operation. @dev must be allocated via drm_dev_alloc()
7398621f407SFrançois Tigeot  * previously. Right after drm_dev_register() the driver should call
7408621f407SFrançois Tigeot  * drm_connector_register_all() to register all connectors in sysfs. This is
7418621f407SFrançois Tigeot  * a separate call for backward compatibility with drivers still using
7428621f407SFrançois Tigeot  * the deprecated ->load() callback, where connectors are registered from within
7438621f407SFrançois Tigeot  * the ->load() callback.
74424edb884SFrançois Tigeot  *
74524edb884SFrançois Tigeot  * Never call this twice on any device!
74624edb884SFrançois Tigeot  *
747352ff8bdSFrançois Tigeot  * NOTE: To ensure backward compatibility with existing drivers method this
748352ff8bdSFrançois Tigeot  * function calls the ->load() method after registering the device nodes,
749352ff8bdSFrançois Tigeot  * creating race conditions. Usage of the ->load() methods is therefore
750352ff8bdSFrançois Tigeot  * deprecated, drivers must perform all initialization before calling
751352ff8bdSFrançois Tigeot  * drm_dev_register().
752352ff8bdSFrançois Tigeot  *
75324edb884SFrançois Tigeot  * RETURNS:
75424edb884SFrançois Tigeot  * 0 on success, negative error code on failure.
75524edb884SFrançois Tigeot  */
75624edb884SFrançois Tigeot int drm_dev_register(struct drm_device *dev, unsigned long flags)
75724edb884SFrançois Tigeot {
75824edb884SFrançois Tigeot 	int ret;
75924edb884SFrançois Tigeot 
76024edb884SFrançois Tigeot 	mutex_lock(&drm_global_mutex);
76124edb884SFrançois Tigeot 
76224edb884SFrançois Tigeot 	ret = drm_minor_register(dev, DRM_MINOR_CONTROL);
76324edb884SFrançois Tigeot 	if (ret)
76424edb884SFrançois Tigeot 		goto err_minors;
76524edb884SFrançois Tigeot 
76624edb884SFrançois Tigeot 	ret = drm_minor_register(dev, DRM_MINOR_RENDER);
76724edb884SFrançois Tigeot 	if (ret)
76824edb884SFrançois Tigeot 		goto err_minors;
76924edb884SFrançois Tigeot 
77024edb884SFrançois Tigeot 	ret = drm_minor_register(dev, DRM_MINOR_LEGACY);
77124edb884SFrançois Tigeot 	if (ret)
77224edb884SFrançois Tigeot 		goto err_minors;
77324edb884SFrançois Tigeot 
77424edb884SFrançois Tigeot 	if (dev->driver->load) {
77524edb884SFrançois Tigeot 		ret = dev->driver->load(dev, flags);
77624edb884SFrançois Tigeot 		if (ret)
77724edb884SFrançois Tigeot 			goto err_minors;
77824edb884SFrançois Tigeot 	}
77924edb884SFrançois Tigeot 
78024edb884SFrançois Tigeot 	ret = 0;
78124edb884SFrançois Tigeot 	goto out_unlock;
78224edb884SFrançois Tigeot 
78324edb884SFrançois Tigeot err_minors:
78424edb884SFrançois Tigeot 	drm_minor_unregister(dev, DRM_MINOR_LEGACY);
78524edb884SFrançois Tigeot 	drm_minor_unregister(dev, DRM_MINOR_RENDER);
78624edb884SFrançois Tigeot 	drm_minor_unregister(dev, DRM_MINOR_CONTROL);
78724edb884SFrançois Tigeot out_unlock:
78824edb884SFrançois Tigeot 	mutex_unlock(&drm_global_mutex);
78924edb884SFrançois Tigeot 	return ret;
79024edb884SFrançois Tigeot }
79124edb884SFrançois Tigeot EXPORT_SYMBOL(drm_dev_register);
79224edb884SFrançois Tigeot 
793*fcf17be7SFrançois Tigeot #if 0
79424edb884SFrançois Tigeot /**
79524edb884SFrançois Tigeot  * drm_dev_unregister - Unregister DRM device
79624edb884SFrançois Tigeot  * @dev: Device to unregister
79724edb884SFrançois Tigeot  *
79824edb884SFrançois Tigeot  * Unregister the DRM device from the system. This does the reverse of
79924edb884SFrançois Tigeot  * drm_dev_register() but does not deallocate the device. The caller must call
80024edb884SFrançois Tigeot  * drm_dev_unref() to drop their final reference.
801352ff8bdSFrançois Tigeot  *
802352ff8bdSFrançois Tigeot  * This should be called first in the device teardown code to make sure
803352ff8bdSFrançois Tigeot  * userspace can't access the device instance any more.
80424edb884SFrançois Tigeot  */
80524edb884SFrançois Tigeot void drm_dev_unregister(struct drm_device *dev)
80624edb884SFrançois Tigeot {
80724edb884SFrançois Tigeot 	struct drm_map_list *r_list, *list_temp;
80824edb884SFrançois Tigeot 
80924edb884SFrançois Tigeot 	drm_lastclose(dev);
81024edb884SFrançois Tigeot 
81124edb884SFrançois Tigeot 	if (dev->driver->unload)
81224edb884SFrançois Tigeot 		dev->driver->unload(dev);
81324edb884SFrançois Tigeot 
81424edb884SFrançois Tigeot 	if (dev->agp)
81524edb884SFrançois Tigeot 		drm_pci_agp_destroy(dev);
81624edb884SFrançois Tigeot 
81724edb884SFrançois Tigeot 	drm_vblank_cleanup(dev);
81824edb884SFrançois Tigeot 
81924edb884SFrançois Tigeot 	list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
8201b13d190SFrançois Tigeot 		drm_legacy_rmmap(dev, r_list->map);
82124edb884SFrançois Tigeot 
82224edb884SFrançois Tigeot 	drm_minor_unregister(dev, DRM_MINOR_LEGACY);
82324edb884SFrançois Tigeot 	drm_minor_unregister(dev, DRM_MINOR_RENDER);
82424edb884SFrançois Tigeot 	drm_minor_unregister(dev, DRM_MINOR_CONTROL);
82524edb884SFrançois Tigeot }
82624edb884SFrançois Tigeot EXPORT_SYMBOL(drm_dev_unregister);
82724edb884SFrançois Tigeot 
82824edb884SFrançois Tigeot /**
82924edb884SFrançois Tigeot  * drm_dev_set_unique - Set the unique name of a DRM device
83024edb884SFrançois Tigeot  * @dev: device of which to set the unique name
831aee94f86SFrançois Tigeot  * @name: unique name
83224edb884SFrançois Tigeot  *
833aee94f86SFrançois Tigeot  * Sets the unique name of a DRM device using the specified string. Drivers
834aee94f86SFrançois Tigeot  * can use this at driver probe time if the unique name of the devices they
835aee94f86SFrançois Tigeot  * drive is static.
83624edb884SFrançois Tigeot  *
83724edb884SFrançois Tigeot  * Return: 0 on success or a negative error code on failure.
83824edb884SFrançois Tigeot  */
839aee94f86SFrançois Tigeot int drm_dev_set_unique(struct drm_device *dev, const char *name)
84024edb884SFrançois Tigeot {
84124edb884SFrançois Tigeot 	kfree(dev->unique);
842aee94f86SFrançois Tigeot 	dev->unique = kstrdup(name, GFP_KERNEL);
84324edb884SFrançois Tigeot 
84424edb884SFrançois Tigeot 	return dev->unique ? 0 : -ENOMEM;
84524edb884SFrançois Tigeot }
84624edb884SFrançois Tigeot EXPORT_SYMBOL(drm_dev_set_unique);
84724edb884SFrançois Tigeot #endif
84824edb884SFrançois Tigeot 
84924edb884SFrançois Tigeot /*
85024edb884SFrançois Tigeot  * DRM Core
85124edb884SFrançois Tigeot  * The DRM core module initializes all global DRM objects and makes them
85224edb884SFrançois Tigeot  * available to drivers. Once setup, drivers can probe their respective
85324edb884SFrançois Tigeot  * devices.
85424edb884SFrançois Tigeot  * Currently, core management includes:
85524edb884SFrançois Tigeot  *  - The "DRM-Global" key/value database
85624edb884SFrançois Tigeot  *  - Global ID management for connectors
85724edb884SFrançois Tigeot  *  - DRM major number allocation
85824edb884SFrançois Tigeot  *  - DRM minor management
85924edb884SFrançois Tigeot  *  - DRM sysfs class
86024edb884SFrançois Tigeot  *  - DRM debugfs root
86124edb884SFrançois Tigeot  *
86224edb884SFrançois Tigeot  * Furthermore, the DRM core provides dynamic char-dev lookups. For each
86324edb884SFrançois Tigeot  * interface registered on a DRM device, you can request minor numbers from DRM
86424edb884SFrançois Tigeot  * core. DRM core takes care of major-number management and char-dev
86524edb884SFrançois Tigeot  * registration. A stub ->open() callback forwards any open() requests to the
86624edb884SFrançois Tigeot  * registered minor.
86724edb884SFrançois Tigeot  */
86824edb884SFrançois Tigeot 
86924edb884SFrançois Tigeot #if 0
87024edb884SFrançois Tigeot static int drm_stub_open(struct inode *inode, struct file *filp)
87124edb884SFrançois Tigeot {
87224edb884SFrançois Tigeot 	const struct file_operations *new_fops;
87324edb884SFrançois Tigeot 	struct drm_minor *minor;
87424edb884SFrançois Tigeot 	int err;
87524edb884SFrançois Tigeot 
87624edb884SFrançois Tigeot 	DRM_DEBUG("\n");
87724edb884SFrançois Tigeot 
87824edb884SFrançois Tigeot 	mutex_lock(&drm_global_mutex);
87924edb884SFrançois Tigeot 	minor = drm_minor_acquire(iminor(inode));
88024edb884SFrançois Tigeot 	if (IS_ERR(minor)) {
88124edb884SFrançois Tigeot 		err = PTR_ERR(minor);
88224edb884SFrançois Tigeot 		goto out_unlock;
88324edb884SFrançois Tigeot 	}
88424edb884SFrançois Tigeot 
88524edb884SFrançois Tigeot 	new_fops = fops_get(minor->dev->driver->fops);
88624edb884SFrançois Tigeot 	if (!new_fops) {
88724edb884SFrançois Tigeot 		err = -ENODEV;
88824edb884SFrançois Tigeot 		goto out_release;
88924edb884SFrançois Tigeot 	}
89024edb884SFrançois Tigeot 
89124edb884SFrançois Tigeot 	replace_fops(filp, new_fops);
89224edb884SFrançois Tigeot 	if (filp->f_op->open)
89324edb884SFrançois Tigeot 		err = filp->f_op->open(inode, filp);
89424edb884SFrançois Tigeot 	else
89524edb884SFrançois Tigeot 		err = 0;
89624edb884SFrançois Tigeot 
89724edb884SFrançois Tigeot out_release:
89824edb884SFrançois Tigeot 	drm_minor_release(minor);
89924edb884SFrançois Tigeot out_unlock:
90024edb884SFrançois Tigeot 	mutex_unlock(&drm_global_mutex);
90124edb884SFrançois Tigeot 	return err;
90224edb884SFrançois Tigeot }
90324edb884SFrançois Tigeot 
90424edb884SFrançois Tigeot static const struct file_operations drm_stub_fops = {
90524edb884SFrançois Tigeot 	.owner = THIS_MODULE,
90624edb884SFrançois Tigeot 	.open = drm_stub_open,
90724edb884SFrançois Tigeot 	.llseek = noop_llseek,
90824edb884SFrançois Tigeot };
90924edb884SFrançois Tigeot 
91024edb884SFrançois Tigeot static int __init drm_core_init(void)
91124edb884SFrançois Tigeot {
91224edb884SFrançois Tigeot 	int ret = -ENOMEM;
91324edb884SFrançois Tigeot 
91424edb884SFrançois Tigeot 	drm_global_init();
91524edb884SFrançois Tigeot 	drm_connector_ida_init();
91624edb884SFrançois Tigeot 	idr_init(&drm_minors_idr);
91724edb884SFrançois Tigeot 
91824edb884SFrançois Tigeot 	if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops))
91924edb884SFrançois Tigeot 		goto err_p1;
92024edb884SFrançois Tigeot 
921352ff8bdSFrançois Tigeot 	ret = drm_sysfs_init();
922352ff8bdSFrançois Tigeot 	if (ret < 0) {
92324edb884SFrançois Tigeot 		printk(KERN_ERR "DRM: Error creating drm class.\n");
92424edb884SFrançois Tigeot 		goto err_p2;
92524edb884SFrançois Tigeot 	}
92624edb884SFrançois Tigeot 
92724edb884SFrançois Tigeot 	drm_debugfs_root = debugfs_create_dir("dri", NULL);
92824edb884SFrançois Tigeot 	if (!drm_debugfs_root) {
92924edb884SFrançois Tigeot 		DRM_ERROR("Cannot create /sys/kernel/debug/dri\n");
93024edb884SFrançois Tigeot 		ret = -1;
93124edb884SFrançois Tigeot 		goto err_p3;
93224edb884SFrançois Tigeot 	}
93324edb884SFrançois Tigeot 
93424edb884SFrançois Tigeot 	DRM_INFO("Initialized %s %d.%d.%d %s\n",
93524edb884SFrançois Tigeot 		 CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
93624edb884SFrançois Tigeot 	return 0;
93724edb884SFrançois Tigeot err_p3:
93824edb884SFrançois Tigeot 	drm_sysfs_destroy();
93924edb884SFrançois Tigeot err_p2:
94024edb884SFrançois Tigeot 	unregister_chrdev(DRM_MAJOR, "drm");
94124edb884SFrançois Tigeot 
94224edb884SFrançois Tigeot 	idr_destroy(&drm_minors_idr);
94324edb884SFrançois Tigeot err_p1:
94424edb884SFrançois Tigeot 	return ret;
94524edb884SFrançois Tigeot }
94624edb884SFrançois Tigeot 
94724edb884SFrançois Tigeot static void __exit drm_core_exit(void)
94824edb884SFrançois Tigeot {
94924edb884SFrançois Tigeot 	debugfs_remove(drm_debugfs_root);
95024edb884SFrançois Tigeot 	drm_sysfs_destroy();
95124edb884SFrançois Tigeot 
95224edb884SFrançois Tigeot 	unregister_chrdev(DRM_MAJOR, "drm");
95324edb884SFrançois Tigeot 
95424edb884SFrançois Tigeot 	drm_connector_ida_destroy();
95524edb884SFrançois Tigeot 	idr_destroy(&drm_minors_idr);
95624edb884SFrançois Tigeot }
95724edb884SFrançois Tigeot 
95824edb884SFrançois Tigeot module_init(drm_core_init);
95924edb884SFrançois Tigeot module_exit(drm_core_exit);
96024edb884SFrançois Tigeot #endif
96124edb884SFrançois Tigeot 
96279f713b0SFrançois Tigeot #include <sys/devfs.h>
9635718399fSFrançois Tigeot 
9649edbd4a0SFrançois Tigeot #include <linux/export.h>
965be348e9fSFrançois Tigeot #include <linux/dmi.h>
96618e26a6dSFrançois Tigeot #include <drm/drmP.h>
96718e26a6dSFrançois Tigeot #include <drm/drm_core.h>
9687f3c3d6fSHasso Tepper 
969b3705d71SHasso Tepper static int drm_load(struct drm_device *dev);
9703260c067SFrançois Tigeot drm_pci_id_list_t *drm_find_description(int vendor, int device,
9717f3c3d6fSHasso Tepper     drm_pci_id_list_t *idlist);
9727f3c3d6fSHasso Tepper 
9737f3c3d6fSHasso Tepper #define DRIVER_SOFTC(unit) \
974b3705d71SHasso Tepper 	((struct drm_device *)devclass_get_softc(drm_devclass, unit))
9757f3c3d6fSHasso Tepper 
9765718399fSFrançois Tigeot static int
9775718399fSFrançois Tigeot drm_modevent(module_t mod, int type, void *data)
9785718399fSFrançois Tigeot {
9795718399fSFrançois Tigeot 
9805718399fSFrançois Tigeot 	switch (type) {
9815718399fSFrançois Tigeot 	case MOD_LOAD:
9821d5dd71dSFrançois Tigeot 		TUNABLE_INT_FETCH("drm.debug", &drm_debug);
9835718399fSFrançois Tigeot 		break;
9845718399fSFrançois Tigeot 	}
9855718399fSFrançois Tigeot 	return (0);
9865718399fSFrançois Tigeot }
9875718399fSFrançois Tigeot 
9885718399fSFrançois Tigeot static moduledata_t drm_mod = {
9895718399fSFrançois Tigeot 	"drm",
9905718399fSFrançois Tigeot 	drm_modevent,
9915718399fSFrançois Tigeot 	0
9925718399fSFrançois Tigeot };
9935718399fSFrançois Tigeot DECLARE_MODULE(drm, drm_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
9947f3c3d6fSHasso Tepper MODULE_VERSION(drm, 1);
9957f3c3d6fSHasso Tepper MODULE_DEPEND(drm, agp, 1, 1, 1);
9967f3c3d6fSHasso Tepper MODULE_DEPEND(drm, pci, 1, 1, 1);
9975718399fSFrançois Tigeot MODULE_DEPEND(drm, iicbus, 1, 1, 1);
9987f3c3d6fSHasso Tepper 
99979f713b0SFrançois Tigeot static struct dev_ops drm_cdevsw = {
1000d268c872SFrançois Tigeot 	{ "drm", 0, D_TRACKCLOSE | D_MPSAFE },
10017f3c3d6fSHasso Tepper 	.d_open =	drm_open,
10022c845b81SSimon Schubert 	.d_close =	drm_close,
10037f3c3d6fSHasso Tepper 	.d_read =	drm_read,
10047f3c3d6fSHasso Tepper 	.d_ioctl =	drm_ioctl,
10053694ee49SSamuel J. Greear 	.d_kqfilter =	drm_kqfilter,
10065718399fSFrançois Tigeot 	.d_mmap =	drm_mmap,
10075718399fSFrançois Tigeot 	.d_mmap_single = drm_mmap_single,
10087f3c3d6fSHasso Tepper };
10097f3c3d6fSHasso Tepper 
10105718399fSFrançois Tigeot SYSCTL_NODE(_hw, OID_AUTO, drm, CTLFLAG_RW, NULL, "DRM device");
101127a0f882SMatthew Dillon SYSCTL_INT(_hw_drm, OID_AUTO, debug, CTLFLAG_RW, &drm_debug, 0,
101227a0f882SMatthew Dillon     "DRM debugging");
1013b3705d71SHasso Tepper 
1014b3705d71SHasso Tepper int drm_probe(device_t kdev, drm_pci_id_list_t *idlist)
10157f3c3d6fSHasso Tepper {
10167f3c3d6fSHasso Tepper 	drm_pci_id_list_t *id_entry;
10177f3c3d6fSHasso Tepper 	int vendor, device;
10187f3c3d6fSHasso Tepper 
101931935f45SSepherosa Ziehau 	vendor = pci_get_vendor(kdev);
102031935f45SSepherosa Ziehau 	device = pci_get_device(kdev);
1021b3705d71SHasso Tepper 
10225718399fSFrançois Tigeot 	if (pci_get_class(kdev) != PCIC_DISPLAY)
1023b3705d71SHasso Tepper 		return ENXIO;
10247f3c3d6fSHasso Tepper 
10257f3c3d6fSHasso Tepper 	id_entry = drm_find_description(vendor, device, idlist);
10267f3c3d6fSHasso Tepper 	if (id_entry != NULL) {
1027b3705d71SHasso Tepper 		if (!device_get_desc(kdev)) {
1028b3705d71SHasso Tepper 			device_set_desc(kdev, id_entry->name);
1029240f0ad6Szrj 			DRM_DEBUG("desc : %s\n", device_get_desc(kdev));
1030b3705d71SHasso Tepper 		}
10317f3c3d6fSHasso Tepper 		return 0;
10327f3c3d6fSHasso Tepper 	}
10337f3c3d6fSHasso Tepper 
10347f3c3d6fSHasso Tepper 	return ENXIO;
10357f3c3d6fSHasso Tepper }
10367f3c3d6fSHasso Tepper 
1037b3705d71SHasso Tepper int drm_attach(device_t kdev, drm_pci_id_list_t *idlist)
10387f3c3d6fSHasso Tepper {
1039b3705d71SHasso Tepper 	struct drm_device *dev;
10407f3c3d6fSHasso Tepper 	drm_pci_id_list_t *id_entry;
10414705af8fSMarkus Pfeiffer 	int unit, error;
10427f3c3d6fSHasso Tepper 
1043b3705d71SHasso Tepper 	unit = device_get_unit(kdev);
1044b3705d71SHasso Tepper 	dev = device_get_softc(kdev);
10457f3c3d6fSHasso Tepper 
1046fb572d17SFrançois Tigeot 	/* Initialize Linux struct device */
1047fb572d17SFrançois Tigeot 	dev->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1048b3705d71SHasso Tepper 
1049fb572d17SFrançois Tigeot 	if (!strcmp(device_get_name(kdev), "drmsub"))
1050fb572d17SFrançois Tigeot 		dev->dev->bsddev = device_get_parent(kdev);
1051fb572d17SFrançois Tigeot 	else
1052fb572d17SFrançois Tigeot 		dev->dev->bsddev = kdev;
1053fb572d17SFrançois Tigeot 
1054fb572d17SFrançois Tigeot 	dev->pci_domain = pci_get_domain(dev->dev->bsddev);
1055fb572d17SFrançois Tigeot 	dev->pci_bus = pci_get_bus(dev->dev->bsddev);
1056fb572d17SFrançois Tigeot 	dev->pci_slot = pci_get_slot(dev->dev->bsddev);
1057fb572d17SFrançois Tigeot 	dev->pci_func = pci_get_function(dev->dev->bsddev);
1058fb572d17SFrançois Tigeot 	drm_init_pdev(dev->dev->bsddev, &dev->pdev);
1059b3705d71SHasso Tepper 
1060ccebb33fSFrançois Tigeot 	id_entry = drm_find_description(dev->pdev->vendor,
1061ccebb33fSFrançois Tigeot 	    dev->pdev->device, idlist);
10626f486c69SFrançois Tigeot 	dev->id_entry = id_entry;
1063b3705d71SHasso Tepper 
1064fc53575bSzrj 	/* Print the contents of pdev struct. */
1065fc53575bSzrj 	drm_print_pdev(dev->pdev);
1066fc53575bSzrj 
10675718399fSFrançois Tigeot 	lockinit(&dev->dev_lock, "drmdev", 0, LK_CANRECURSE);
1068b3705d71SHasso Tepper 	lwkt_serialize_init(&dev->irq_lock);
10695718399fSFrançois Tigeot 	lockinit(&dev->event_lock, "drmev", 0, LK_CANRECURSE);
1070a2fdbec6SFrançois Tigeot 	lockinit(&dev->struct_mutex, "drmslk", 0, LK_CANRECURSE);
1071b3705d71SHasso Tepper 
10726f486c69SFrançois Tigeot 	error = drm_load(dev);
10736f486c69SFrançois Tigeot 	if (error)
10746f486c69SFrançois Tigeot 		goto error;
10757f3c3d6fSHasso Tepper 
107679f713b0SFrançois Tigeot 	error = drm_create_cdevs(kdev);
107779f713b0SFrançois Tigeot 
10786f486c69SFrançois Tigeot error:
10796f486c69SFrançois Tigeot 	return (error);
10806f486c69SFrançois Tigeot }
10816f486c69SFrançois Tigeot 
108279f713b0SFrançois Tigeot int
108379f713b0SFrançois Tigeot drm_create_cdevs(device_t kdev)
108479f713b0SFrançois Tigeot {
108579f713b0SFrançois Tigeot 	struct drm_device *dev;
108679f713b0SFrançois Tigeot 	int error, unit;
108779f713b0SFrançois Tigeot 
108879f713b0SFrançois Tigeot 	unit = device_get_unit(kdev);
108979f713b0SFrançois Tigeot 	dev = device_get_softc(kdev);
109079f713b0SFrançois Tigeot 
109179f713b0SFrançois Tigeot 	dev->devnode = make_dev(&drm_cdevsw, unit, DRM_DEV_UID, DRM_DEV_GID,
109279f713b0SFrançois Tigeot 				DRM_DEV_MODE, "dri/card%d", unit);
109379f713b0SFrançois Tigeot 	error = 0;
109479f713b0SFrançois Tigeot 	if (error == 0)
109579f713b0SFrançois Tigeot 		dev->devnode->si_drv1 = dev;
109679f713b0SFrançois Tigeot 	return (error);
109779f713b0SFrançois Tigeot }
109879f713b0SFrançois Tigeot 
10997f3c3d6fSHasso Tepper #ifndef DRM_DEV_NAME
11007f3c3d6fSHasso Tepper #define DRM_DEV_NAME "drm"
11017f3c3d6fSHasso Tepper #endif
11027f3c3d6fSHasso Tepper 
11037f3c3d6fSHasso Tepper devclass_t drm_devclass;
11047f3c3d6fSHasso Tepper 
11057f3c3d6fSHasso Tepper drm_pci_id_list_t *drm_find_description(int vendor, int device,
11067f3c3d6fSHasso Tepper     drm_pci_id_list_t *idlist)
11077f3c3d6fSHasso Tepper {
11087f3c3d6fSHasso Tepper 	int i = 0;
11097f3c3d6fSHasso Tepper 
11107f3c3d6fSHasso Tepper 	for (i = 0; idlist[i].vendor != 0; i++) {
11117f3c3d6fSHasso Tepper 		if ((idlist[i].vendor == vendor) &&
1112b3705d71SHasso Tepper 		    ((idlist[i].device == device) ||
1113b3705d71SHasso Tepper 		    (idlist[i].device == 0))) {
11147f3c3d6fSHasso Tepper 			return &idlist[i];
11157f3c3d6fSHasso Tepper 		}
11167f3c3d6fSHasso Tepper 	}
11177f3c3d6fSHasso Tepper 	return NULL;
11187f3c3d6fSHasso Tepper }
11197f3c3d6fSHasso Tepper 
1120b3705d71SHasso Tepper static int drm_load(struct drm_device *dev)
11217f3c3d6fSHasso Tepper {
11227f3c3d6fSHasso Tepper 	int i, retcode;
11237f3c3d6fSHasso Tepper 
11247f3c3d6fSHasso Tepper 	DRM_DEBUG("\n");
11257f3c3d6fSHasso Tepper 
1126f599cd46SFrançois Tigeot 	INIT_LIST_HEAD(&dev->maplist);
11277f3c3d6fSHasso Tepper 
11287f3c3d6fSHasso Tepper 	drm_sysctl_init(dev);
11291610a1a0SFrançois Tigeot 	INIT_LIST_HEAD(&dev->filelist);
11307f3c3d6fSHasso Tepper 
11317f3c3d6fSHasso Tepper 	dev->counters  = 6;
11327f3c3d6fSHasso Tepper 	dev->types[0]  = _DRM_STAT_LOCK;
11337f3c3d6fSHasso Tepper 	dev->types[1]  = _DRM_STAT_OPENS;
11347f3c3d6fSHasso Tepper 	dev->types[2]  = _DRM_STAT_CLOSES;
11357f3c3d6fSHasso Tepper 	dev->types[3]  = _DRM_STAT_IOCTLS;
11367f3c3d6fSHasso Tepper 	dev->types[4]  = _DRM_STAT_LOCKS;
11377f3c3d6fSHasso Tepper 	dev->types[5]  = _DRM_STAT_UNLOCKS;
11387f3c3d6fSHasso Tepper 
1139c6f73aabSFrançois Tigeot 	for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
11407f3c3d6fSHasso Tepper 		atomic_set(&dev->counts[i], 0);
11417f3c3d6fSHasso Tepper 
11425718399fSFrançois Tigeot 	INIT_LIST_HEAD(&dev->vblank_event_list);
11437f3c3d6fSHasso Tepper 
114453e4e524Szrj 	if (drm_core_check_feature(dev, DRIVER_USE_AGP)) {
1145e5411345Szrj 		if (drm_pci_device_is_agp(dev))
1146e5411345Szrj 			dev->agp = drm_agp_init(dev);
11477f3c3d6fSHasso Tepper 	}
11487f3c3d6fSHasso Tepper 
11495718399fSFrançois Tigeot 	if (dev->driver->driver_features & DRIVER_GEM) {
11505718399fSFrançois Tigeot 		retcode = drm_gem_init(dev);
11515718399fSFrançois Tigeot 		if (retcode != 0) {
11525718399fSFrançois Tigeot 			DRM_ERROR("Cannot initialize graphics execution "
11535718399fSFrançois Tigeot 				  "manager (GEM)\n");
11545718399fSFrançois Tigeot 			goto error1;
11555718399fSFrançois Tigeot 		}
11565718399fSFrançois Tigeot 	}
11575718399fSFrançois Tigeot 
11585718399fSFrançois Tigeot 	if (dev->driver->load != NULL) {
11595718399fSFrançois Tigeot 		DRM_LOCK(dev);
11605718399fSFrançois Tigeot 		/* Shared code returns -errno. */
11615718399fSFrançois Tigeot 		retcode = -dev->driver->load(dev,
11625718399fSFrançois Tigeot 		    dev->id_entry->driver_private);
1163fb572d17SFrançois Tigeot 		if (pci_enable_busmaster(dev->dev->bsddev))
11645718399fSFrançois Tigeot 			DRM_ERROR("Request to enable bus-master failed.\n");
11655718399fSFrançois Tigeot 		DRM_UNLOCK(dev);
11665718399fSFrançois Tigeot 		if (retcode != 0)
11676f486c69SFrançois Tigeot 			goto error1;
11685718399fSFrançois Tigeot 	}
11695718399fSFrançois Tigeot 
11707f3c3d6fSHasso Tepper 	DRM_INFO("Initialized %s %d.%d.%d %s\n",
1171b3705d71SHasso Tepper 	    dev->driver->name,
1172b3705d71SHasso Tepper 	    dev->driver->major,
1173b3705d71SHasso Tepper 	    dev->driver->minor,
1174b3705d71SHasso Tepper 	    dev->driver->patchlevel,
1175b3705d71SHasso Tepper 	    dev->driver->date);
11767f3c3d6fSHasso Tepper 
11777f3c3d6fSHasso Tepper 	return 0;
11787f3c3d6fSHasso Tepper 
11795718399fSFrançois Tigeot error1:
11806f486c69SFrançois Tigeot 	drm_gem_destroy(dev);
11817f3c3d6fSHasso Tepper 	drm_sysctl_cleanup(dev);
11825718399fSFrançois Tigeot 	DRM_LOCK(dev);
11837f3c3d6fSHasso Tepper 	drm_lastclose(dev);
11845718399fSFrançois Tigeot 	DRM_UNLOCK(dev);
11855718399fSFrançois Tigeot 	if (dev->devnode != NULL)
11867f3c3d6fSHasso Tepper 		destroy_dev(dev->devnode);
1187b3705d71SHasso Tepper 
11885718399fSFrançois Tigeot 	lockuninit(&dev->vbl_lock);
11895718399fSFrançois Tigeot 	lockuninit(&dev->dev_lock);
11905718399fSFrançois Tigeot 	lockuninit(&dev->event_lock);
1191a2fdbec6SFrançois Tigeot 	lockuninit(&dev->struct_mutex);
1192b3705d71SHasso Tepper 
11937f3c3d6fSHasso Tepper 	return retcode;
11947f3c3d6fSHasso Tepper }
11957f3c3d6fSHasso Tepper 
11963994a83eSMarkus Pfeiffer /*
11973994a83eSMarkus Pfeiffer  * Stub is needed for devfs
11983994a83eSMarkus Pfeiffer  */
11992c845b81SSimon Schubert int drm_close(struct dev_close_args *ap)
12007f3c3d6fSHasso Tepper {
12013994a83eSMarkus Pfeiffer 	return 0;
12023994a83eSMarkus Pfeiffer }
12037f3c3d6fSHasso Tepper 
1204d4d73b30SFrançois Tigeot /* XXX: this is supposed to be drm_release() */
120579f713b0SFrançois Tigeot void drm_cdevpriv_dtor(void *cd)
120679f713b0SFrançois Tigeot {
120779f713b0SFrançois Tigeot 	struct drm_file *file_priv = cd;
120879f713b0SFrançois Tigeot 	struct drm_device *dev = file_priv->dev;
120979f713b0SFrançois Tigeot 
121079f713b0SFrançois Tigeot 	DRM_DEBUG("open_count = %d\n", dev->open_count);
121179f713b0SFrançois Tigeot 
121279f713b0SFrançois Tigeot 	DRM_LOCK(dev);
121379f713b0SFrançois Tigeot 
121479f713b0SFrançois Tigeot 	if (dev->driver->preclose != NULL)
121579f713b0SFrançois Tigeot 		dev->driver->preclose(dev, file_priv);
121679f713b0SFrançois Tigeot 
121779f713b0SFrançois Tigeot 	/* ========================================================
121879f713b0SFrançois Tigeot 	 * Begin inline drm_release
121979f713b0SFrançois Tigeot 	 */
122079f713b0SFrançois Tigeot 
122179f713b0SFrançois Tigeot 	DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
122279f713b0SFrançois Tigeot 	    DRM_CURRENTPID, (long)dev->dev, dev->open_count);
122379f713b0SFrançois Tigeot 
122479f713b0SFrançois Tigeot 	if (dev->driver->driver_features & DRIVER_GEM)
122579f713b0SFrançois Tigeot 		drm_gem_release(dev, file_priv);
122679f713b0SFrançois Tigeot 
1227d4d73b30SFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
12281b13d190SFrançois Tigeot 		drm_legacy_reclaim_buffers(dev, file_priv);
122979f713b0SFrançois Tigeot 
123079f713b0SFrançois Tigeot 	funsetown(&dev->buf_sigio);
123179f713b0SFrançois Tigeot 
123279f713b0SFrançois Tigeot 	if (dev->driver->postclose != NULL)
123379f713b0SFrançois Tigeot 		dev->driver->postclose(dev, file_priv);
123479f713b0SFrançois Tigeot 	list_del(&file_priv->lhead);
123579f713b0SFrançois Tigeot 
123679f713b0SFrançois Tigeot 
123779f713b0SFrançois Tigeot 	/* ========================================================
123879f713b0SFrançois Tigeot 	 * End inline drm_release
123979f713b0SFrançois Tigeot 	 */
124079f713b0SFrançois Tigeot 
124179f713b0SFrançois Tigeot 	atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
1242fb572d17SFrançois Tigeot 	device_unbusy(dev->dev->bsddev);
124379f713b0SFrançois Tigeot 	if (--dev->open_count == 0) {
12448621f407SFrançois Tigeot 		drm_lastclose(dev);
124579f713b0SFrançois Tigeot 	}
124679f713b0SFrançois Tigeot 
124779f713b0SFrançois Tigeot 	DRM_UNLOCK(dev);
124879f713b0SFrançois Tigeot }
124979f713b0SFrançois Tigeot 
12505718399fSFrançois Tigeot int
12515718399fSFrançois Tigeot drm_add_busid_modesetting(struct drm_device *dev, struct sysctl_ctx_list *ctx,
12525718399fSFrançois Tigeot     struct sysctl_oid *top)
12535718399fSFrançois Tigeot {
12545718399fSFrançois Tigeot 	struct sysctl_oid *oid;
12555718399fSFrançois Tigeot 
12565718399fSFrançois Tigeot 	ksnprintf(dev->busid_str, sizeof(dev->busid_str),
12575718399fSFrançois Tigeot 	     "pci:%04x:%02x:%02x.%d", dev->pci_domain, dev->pci_bus,
12585718399fSFrançois Tigeot 	     dev->pci_slot, dev->pci_func);
12595718399fSFrançois Tigeot 	oid = SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(top), OID_AUTO, "busid",
12605718399fSFrançois Tigeot 	    CTLFLAG_RD, dev->busid_str, 0, NULL);
12615718399fSFrançois Tigeot 	if (oid == NULL)
12625718399fSFrançois Tigeot 		return (ENOMEM);
12635718399fSFrançois Tigeot 	dev->modesetting = (dev->driver->driver_features & DRIVER_MODESET) != 0;
12645718399fSFrançois Tigeot 	oid = SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(top), OID_AUTO,
12655718399fSFrançois Tigeot 	    "modesetting", CTLFLAG_RD, &dev->modesetting, 0, NULL);
12665718399fSFrançois Tigeot 	if (oid == NULL)
12675718399fSFrançois Tigeot 		return (ENOMEM);
12685718399fSFrançois Tigeot 
12695718399fSFrançois Tigeot 	return (0);
12705718399fSFrançois Tigeot }
12715718399fSFrançois Tigeot 
12725718399fSFrançois Tigeot int
12735718399fSFrançois Tigeot drm_mmap_single(struct dev_mmap_single_args *ap)
12745718399fSFrançois Tigeot {
127579f713b0SFrançois Tigeot 	struct drm_device *dev;
12765718399fSFrançois Tigeot 	struct cdev *kdev = ap->a_head.a_dev;
12775718399fSFrançois Tigeot 	vm_ooffset_t *offset = ap->a_offset;
12785718399fSFrançois Tigeot 	vm_size_t size = ap->a_size;
12795718399fSFrançois Tigeot 	struct vm_object **obj_res = ap->a_object;
12805718399fSFrançois Tigeot 	int nprot = ap->a_nprot;
12815718399fSFrançois Tigeot 
128279f713b0SFrançois Tigeot 	dev = drm_get_device_from_kdev(kdev);
12836f486c69SFrançois Tigeot 	if (dev->drm_ttm_bdev != NULL) {
1284a34b4168SMatthew Dillon 		return (ttm_bo_mmap_single(dev, offset, size, obj_res, nprot));
12856f486c69SFrançois Tigeot 	} else if ((dev->driver->driver_features & DRIVER_GEM) != 0) {
12866f486c69SFrançois Tigeot 		return (drm_gem_mmap_single(dev, offset, size, obj_res, nprot));
12875718399fSFrançois Tigeot 	} else {
12885718399fSFrançois Tigeot 		return (ENODEV);
12895718399fSFrançois Tigeot 	}
12905718399fSFrançois Tigeot }
12915718399fSFrançois Tigeot 
12926f486c69SFrançois Tigeot static int
12936f486c69SFrançois Tigeot drm_core_init(void *arg)
12946f486c69SFrançois Tigeot {
12956f486c69SFrançois Tigeot 
12966f486c69SFrançois Tigeot 	drm_global_init();
12976f486c69SFrançois Tigeot 
12986f486c69SFrançois Tigeot 	DRM_INFO("Initialized %s %d.%d.%d %s\n",
12996f486c69SFrançois Tigeot 		 CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
13006f486c69SFrançois Tigeot 	return 0;
13016f486c69SFrançois Tigeot }
13026f486c69SFrançois Tigeot 
13036f486c69SFrançois Tigeot static void
13046f486c69SFrançois Tigeot drm_core_exit(void *arg)
13056f486c69SFrançois Tigeot {
13066f486c69SFrançois Tigeot 
13076f486c69SFrançois Tigeot 	drm_global_release();
13086f486c69SFrançois Tigeot }
13096f486c69SFrançois Tigeot 
13106f486c69SFrançois Tigeot SYSINIT(drm_register, SI_SUB_DRIVERS, SI_ORDER_MIDDLE,
13116f486c69SFrançois Tigeot     drm_core_init, NULL);
13126f486c69SFrançois Tigeot SYSUNINIT(drm_unregister, SI_SUB_DRIVERS, SI_ORDER_MIDDLE,
13136f486c69SFrançois Tigeot     drm_core_exit, NULL);
13146f486c69SFrançois Tigeot 
131524edb884SFrançois Tigeot 
131624edb884SFrançois Tigeot #include <linux/dmi.h>
131724edb884SFrançois Tigeot 
13185718399fSFrançois Tigeot /*
13195718399fSFrançois Tigeot  * Check if dmi_system_id structure matches system DMI data
13205718399fSFrançois Tigeot  */
13215718399fSFrançois Tigeot static bool
13225718399fSFrançois Tigeot dmi_found(const struct dmi_system_id *dsi)
13235718399fSFrançois Tigeot {
13245718399fSFrançois Tigeot 	int i, slot;
13255718399fSFrançois Tigeot 	bool found = false;
13265718399fSFrançois Tigeot 	char *sys_vendor, *board_vendor, *product_name, *board_name;
13275718399fSFrançois Tigeot 
13285718399fSFrançois Tigeot 	sys_vendor = kgetenv("smbios.system.maker");
13295718399fSFrançois Tigeot 	board_vendor = kgetenv("smbios.planar.maker");
13305718399fSFrançois Tigeot 	product_name = kgetenv("smbios.system.product");
13315718399fSFrançois Tigeot 	board_name = kgetenv("smbios.planar.product");
13325718399fSFrançois Tigeot 
13335718399fSFrançois Tigeot 	for (i = 0; i < NELEM(dsi->matches); i++) {
13345718399fSFrançois Tigeot 		slot = dsi->matches[i].slot;
13355718399fSFrançois Tigeot 		switch (slot) {
13365718399fSFrançois Tigeot 		case DMI_NONE:
13375718399fSFrançois Tigeot 			break;
13385718399fSFrançois Tigeot 		case DMI_SYS_VENDOR:
13395718399fSFrançois Tigeot 			if (sys_vendor != NULL &&
13405718399fSFrançois Tigeot 			    !strcmp(sys_vendor, dsi->matches[i].substr))
13415718399fSFrançois Tigeot 				break;
13425718399fSFrançois Tigeot 			else
13435718399fSFrançois Tigeot 				goto done;
13445718399fSFrançois Tigeot 		case DMI_BOARD_VENDOR:
13455718399fSFrançois Tigeot 			if (board_vendor != NULL &&
13465718399fSFrançois Tigeot 			    !strcmp(board_vendor, dsi->matches[i].substr))
13475718399fSFrançois Tigeot 				break;
13485718399fSFrançois Tigeot 			else
13495718399fSFrançois Tigeot 				goto done;
13505718399fSFrançois Tigeot 		case DMI_PRODUCT_NAME:
13515718399fSFrançois Tigeot 			if (product_name != NULL &&
13525718399fSFrançois Tigeot 			    !strcmp(product_name, dsi->matches[i].substr))
13535718399fSFrançois Tigeot 				break;
13545718399fSFrançois Tigeot 			else
13555718399fSFrançois Tigeot 				goto done;
13565718399fSFrançois Tigeot 		case DMI_BOARD_NAME:
13575718399fSFrançois Tigeot 			if (board_name != NULL &&
13585718399fSFrançois Tigeot 			    !strcmp(board_name, dsi->matches[i].substr))
13595718399fSFrançois Tigeot 				break;
13605718399fSFrançois Tigeot 			else
13615718399fSFrançois Tigeot 				goto done;
13625718399fSFrançois Tigeot 		default:
13635718399fSFrançois Tigeot 			goto done;
13645718399fSFrançois Tigeot 		}
13655718399fSFrançois Tigeot 	}
13665718399fSFrançois Tigeot 	found = true;
13675718399fSFrançois Tigeot 
13685718399fSFrançois Tigeot done:
13695718399fSFrançois Tigeot 	if (sys_vendor != NULL)
13705718399fSFrançois Tigeot 		kfreeenv(sys_vendor);
13715718399fSFrançois Tigeot 	if (board_vendor != NULL)
13725718399fSFrançois Tigeot 		kfreeenv(board_vendor);
13735718399fSFrançois Tigeot 	if (product_name != NULL)
13745718399fSFrançois Tigeot 		kfreeenv(product_name);
13755718399fSFrançois Tigeot 	if (board_name != NULL)
13765718399fSFrançois Tigeot 		kfreeenv(board_name);
13775718399fSFrançois Tigeot 
13785718399fSFrançois Tigeot 	return found;
13795718399fSFrançois Tigeot }
13805718399fSFrançois Tigeot 
1381be348e9fSFrançois Tigeot int dmi_check_system(const struct dmi_system_id *sysid)
13825718399fSFrançois Tigeot {
13835718399fSFrançois Tigeot 	const struct dmi_system_id *dsi;
13845718399fSFrançois Tigeot 	int num = 0;
13855718399fSFrançois Tigeot 
13865718399fSFrançois Tigeot 	for (dsi = sysid; dsi->matches[0].slot != 0 ; dsi++) {
13875718399fSFrançois Tigeot 		if (dmi_found(dsi)) {
13885718399fSFrançois Tigeot 			num++;
13895718399fSFrançois Tigeot 			if (dsi->callback && dsi->callback(dsi))
13905718399fSFrançois Tigeot 				break;
13915718399fSFrançois Tigeot 		}
13925718399fSFrançois Tigeot 	}
13935718399fSFrançois Tigeot 	return (num);
13945718399fSFrançois Tigeot }
1395