xref: /openbmc/linux/drivers/gpu/drm/ttm/ttm_module.c (revision 3763d635)
11297bf2eSDirk Hohndel /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2ba4e7d97SThomas Hellstrom /**************************************************************************
3ba4e7d97SThomas Hellstrom  *
4ba4e7d97SThomas Hellstrom  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
5ba4e7d97SThomas Hellstrom  * All Rights Reserved.
6ba4e7d97SThomas Hellstrom  *
7ba4e7d97SThomas Hellstrom  * Permission is hereby granted, free of charge, to any person obtaining a
8ba4e7d97SThomas Hellstrom  * copy of this software and associated documentation files (the
9ba4e7d97SThomas Hellstrom  * "Software"), to deal in the Software without restriction, including
10ba4e7d97SThomas Hellstrom  * without limitation the rights to use, copy, modify, merge, publish,
11ba4e7d97SThomas Hellstrom  * distribute, sub license, and/or sell copies of the Software, and to
12ba4e7d97SThomas Hellstrom  * permit persons to whom the Software is furnished to do so, subject to
13ba4e7d97SThomas Hellstrom  * the following conditions:
14ba4e7d97SThomas Hellstrom  *
15ba4e7d97SThomas Hellstrom  * The above copyright notice and this permission notice (including the
16ba4e7d97SThomas Hellstrom  * next paragraph) shall be included in all copies or substantial portions
17ba4e7d97SThomas Hellstrom  * of the Software.
18ba4e7d97SThomas Hellstrom  *
19ba4e7d97SThomas Hellstrom  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20ba4e7d97SThomas Hellstrom  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21ba4e7d97SThomas Hellstrom  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22ba4e7d97SThomas Hellstrom  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23ba4e7d97SThomas Hellstrom  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24ba4e7d97SThomas Hellstrom  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25ba4e7d97SThomas Hellstrom  * USE OR OTHER DEALINGS IN THE SOFTWARE.
26ba4e7d97SThomas Hellstrom  *
27ba4e7d97SThomas Hellstrom  **************************************************************************/
28ba4e7d97SThomas Hellstrom /*
29ba4e7d97SThomas Hellstrom  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
30ba4e7d97SThomas Hellstrom  * 	    Jerome Glisse
31ba4e7d97SThomas Hellstrom  */
32ba4e7d97SThomas Hellstrom #include <linux/module.h>
33e9840be8SThomas Hellstrom #include <linux/device.h>
34e9840be8SThomas Hellstrom #include <linux/sched.h>
35*3763d635SChristian König #include <linux/debugfs.h>
36760285e7SDavid Howells #include <drm/drm_sysfs.h>
37e9840be8SThomas Hellstrom 
385cf82904SChristian König #include "ttm_module.h"
395cf82904SChristian König 
40e9840be8SThomas Hellstrom static DECLARE_WAIT_QUEUE_HEAD(exit_q);
414133d018SDave Airlie static atomic_t device_released;
42*3763d635SChristian König struct dentry *ttm_debugfs_root;
43e9840be8SThomas Hellstrom 
44e9840be8SThomas Hellstrom static struct device_type ttm_drm_class_type = {
45e9840be8SThomas Hellstrom 	.name = "ttm",
46e9840be8SThomas Hellstrom 	/**
47e9840be8SThomas Hellstrom 	 * Add pm ops here.
48e9840be8SThomas Hellstrom 	 */
49e9840be8SThomas Hellstrom };
50e9840be8SThomas Hellstrom 
51e9840be8SThomas Hellstrom static void ttm_drm_class_device_release(struct device *dev)
52e9840be8SThomas Hellstrom {
53e9840be8SThomas Hellstrom 	atomic_set(&device_released, 1);
54e9840be8SThomas Hellstrom 	wake_up_all(&exit_q);
55e9840be8SThomas Hellstrom }
56e9840be8SThomas Hellstrom 
57e9840be8SThomas Hellstrom static struct device ttm_drm_class_device = {
58e9840be8SThomas Hellstrom 	.type = &ttm_drm_class_type,
59e9840be8SThomas Hellstrom 	.release = &ttm_drm_class_device_release
60e9840be8SThomas Hellstrom };
61e9840be8SThomas Hellstrom 
62e9840be8SThomas Hellstrom struct kobject *ttm_get_kobj(void)
63e9840be8SThomas Hellstrom {
64e9840be8SThomas Hellstrom 	struct kobject *kobj = &ttm_drm_class_device.kobj;
65e9840be8SThomas Hellstrom 	BUG_ON(kobj == NULL);
66e9840be8SThomas Hellstrom 	return kobj;
67e9840be8SThomas Hellstrom }
68ba4e7d97SThomas Hellstrom 
69ba4e7d97SThomas Hellstrom static int __init ttm_init(void)
70ba4e7d97SThomas Hellstrom {
71e9840be8SThomas Hellstrom 	int ret;
72e9840be8SThomas Hellstrom 
73e9840be8SThomas Hellstrom 	ret = dev_set_name(&ttm_drm_class_device, "ttm");
74e9840be8SThomas Hellstrom 	if (unlikely(ret != 0))
75e9840be8SThomas Hellstrom 		return ret;
76e9840be8SThomas Hellstrom 
77e9840be8SThomas Hellstrom 	atomic_set(&device_released, 0);
78e9840be8SThomas Hellstrom 	ret = drm_class_device_register(&ttm_drm_class_device);
79e9840be8SThomas Hellstrom 	if (unlikely(ret != 0))
80e9840be8SThomas Hellstrom 		goto out_no_dev_reg;
81e9840be8SThomas Hellstrom 
82*3763d635SChristian König 	ttm_debugfs_root = debugfs_create_dir("ttm", NULL);
83ba4e7d97SThomas Hellstrom 	return 0;
84e9840be8SThomas Hellstrom out_no_dev_reg:
85e9840be8SThomas Hellstrom 	atomic_set(&device_released, 1);
86e9840be8SThomas Hellstrom 	wake_up_all(&exit_q);
87e9840be8SThomas Hellstrom 	return ret;
88ba4e7d97SThomas Hellstrom }
89ba4e7d97SThomas Hellstrom 
90ba4e7d97SThomas Hellstrom static void __exit ttm_exit(void)
91ba4e7d97SThomas Hellstrom {
92e9840be8SThomas Hellstrom 	drm_class_device_unregister(&ttm_drm_class_device);
93e9840be8SThomas Hellstrom 
94e9840be8SThomas Hellstrom 	/**
95e9840be8SThomas Hellstrom 	 * Refuse to unload until the TTM device is released.
96e9840be8SThomas Hellstrom 	 * Not sure this is 100% needed.
97e9840be8SThomas Hellstrom 	 */
98e9840be8SThomas Hellstrom 
99e9840be8SThomas Hellstrom 	wait_event(exit_q, atomic_read(&device_released) == 1);
100*3763d635SChristian König 	debugfs_remove(ttm_debugfs_root);
101ba4e7d97SThomas Hellstrom }
102ba4e7d97SThomas Hellstrom 
103ba4e7d97SThomas Hellstrom module_init(ttm_init);
104ba4e7d97SThomas Hellstrom module_exit(ttm_exit);
105ba4e7d97SThomas Hellstrom 
106ba4e7d97SThomas Hellstrom MODULE_AUTHOR("Thomas Hellstrom, Jerome Glisse");
107ba4e7d97SThomas Hellstrom MODULE_DESCRIPTION("TTM memory manager subsystem (for DRM device)");
108ba4e7d97SThomas Hellstrom MODULE_LICENSE("GPL and additional rights");
109