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