xref: /netbsd-src/sys/external/bsd/drm2/linux/linux_module.c (revision 7319b3e1b48287a2b1a004c4d6f85c3c87e70a92)
1 /*	$NetBSD: linux_module.c,v 1.11 2021/12/19 01:22:15 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Taylor R. Campbell.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: linux_module.c,v 1.11 2021/12/19 01:22:15 riastradh Exp $");
34 
35 #include <sys/module.h>
36 #ifndef _MODULE
37 #include <sys/once.h>
38 #endif
39 
40 #include <linux/atomic.h>
41 #include <linux/highmem.h>
42 #include <linux/idr.h>
43 #include <linux/io.h>
44 #include <linux/mutex.h>
45 #include <linux/rcupdate.h>
46 #include <linux/tasklet.h>
47 #include <linux/wait_bit.h>
48 #include <linux/workqueue.h>
49 
50 MODULE(MODULE_CLASS_MISC, drmkms_linux, "i2cexec");
51 
52 static int
53 linux_init(void)
54 {
55 	int error;
56 
57 	error = linux_idr_module_init();
58 	if (error) {
59 		printf("linux: unable to initialize idr: %d\n", error);
60 		goto fail0;
61 	}
62 
63 	error = linux_kmap_init();
64 	if (error) {
65 		printf("linux: unable to initialize kmap: %d\n", error);
66 		goto fail1;
67 	}
68 
69 	error = linux_rcu_gc_init();
70 	if (error) {
71 		printf("linux: unable to initialize rcu gc: %d\n", error);
72 		goto fail2;
73 	}
74 
75 	error = linux_workqueue_init();
76 	if (error) {
77 		printf("linux: unable to initialize workqueues: %d\n", error);
78 		goto fail3;
79 	}
80 
81 	error = linux_writecomb_init();
82 	if (error) {
83 		printf("linux: unable to initialize write-combining: %d\n",
84 		    error);
85 		goto fail4;
86 	}
87 
88 	error = linux_atomic64_init();
89 	if (error) {
90 		printf("linux: unable to initialize atomic64: %d\n", error);
91 		goto fail5;
92 	}
93 
94 	error = linux_tasklets_init();
95 	if (error) {
96 		printf("linux: unable to initialize tasklets: %d\n", error);
97 		goto fail6;
98 	}
99 
100 	error = linux_wait_bit_init();
101 	if (error) {
102 		printf("linux: unable to initialize wait_bit: %d\n", error);
103 		goto fail7;
104 	}
105 
106 	return 0;
107 
108 fail8: __unused
109 	linux_wait_bit_fini();
110 fail7:	linux_tasklets_fini();
111 fail6:	linux_atomic64_fini();
112 fail5:	linux_writecomb_fini();
113 fail4:	linux_workqueue_fini();
114 fail3:	linux_rcu_gc_fini();
115 fail2:	linux_kmap_fini();
116 fail1:	linux_idr_module_fini();
117 fail0:	return error;
118 }
119 
120 int	linux_guarantee_initialized(void); /* XXX */
121 int
122 linux_guarantee_initialized(void)
123 {
124 #ifdef _MODULE
125 	return 0;
126 #else
127 	static ONCE_DECL(linux_init_once);
128 
129 	return RUN_ONCE(&linux_init_once, &linux_init);
130 #endif
131 }
132 
133 static void
134 linux_fini(void)
135 {
136 
137 	linux_wait_bit_fini();
138 	linux_tasklets_fini();
139 	linux_atomic64_fini();
140 	linux_writecomb_fini();
141 	linux_workqueue_fini();
142 	linux_rcu_gc_fini();
143 	linux_kmap_fini();
144 	linux_idr_module_fini();
145 }
146 
147 static int
148 drmkms_linux_modcmd(modcmd_t cmd, void *arg __unused)
149 {
150 
151 	switch (cmd) {
152 	case MODULE_CMD_INIT:
153 #ifdef _MODULE
154 		return linux_init();
155 #else
156 		return linux_guarantee_initialized();
157 #endif
158 	case MODULE_CMD_FINI:
159 		linux_fini();
160 		return 0;
161 	default:
162 		return ENOTTY;
163 	}
164 }
165