linux_module.c revision 1.13 1 /* $NetBSD: linux_module.c,v 1.13 2021/12/19 12:23:07 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.13 2021/12/19 12:23:07 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/irq_work.h>
45 #include <linux/kthread.h>
46 #include <linux/mutex.h>
47 #include <linux/rcupdate.h>
48 #include <linux/tasklet.h>
49 #include <linux/wait_bit.h>
50 #include <linux/workqueue.h>
51
52 MODULE(MODULE_CLASS_MISC, drmkms_linux, "i2cexec");
53
54 static int
55 linux_init(void)
56 {
57 int error;
58
59 error = linux_idr_module_init();
60 if (error) {
61 printf("linux: unable to initialize idr: %d\n", error);
62 goto fail0;
63 }
64
65 error = linux_kmap_init();
66 if (error) {
67 printf("linux: unable to initialize kmap: %d\n", error);
68 goto fail1;
69 }
70
71 error = linux_rcu_gc_init();
72 if (error) {
73 printf("linux: unable to initialize rcu gc: %d\n", error);
74 goto fail2;
75 }
76
77 error = linux_workqueue_init();
78 if (error) {
79 printf("linux: unable to initialize workqueues: %d\n", error);
80 goto fail3;
81 }
82
83 error = linux_writecomb_init();
84 if (error) {
85 printf("linux: unable to initialize write-combining: %d\n",
86 error);
87 goto fail4;
88 }
89
90 error = linux_atomic64_init();
91 if (error) {
92 printf("linux: unable to initialize atomic64: %d\n", error);
93 goto fail5;
94 }
95
96 error = linux_tasklets_init();
97 if (error) {
98 printf("linux: unable to initialize tasklets: %d\n", error);
99 goto fail6;
100 }
101
102 error = linux_wait_bit_init();
103 if (error) {
104 printf("linux: unable to initialize wait_bit: %d\n", error);
105 goto fail7;
106 }
107
108 error = linux_kthread_init();
109 if (error) {
110 printf("linux: unable to initialize kthread: %d\n", error);
111 goto fail8;
112 }
113
114 linux_irq_work_init();
115
116 return 0;
117
118 fail9: __unused
119 linux_kthread_fini();
120 fail8: linux_wait_bit_fini();
121 fail7: linux_tasklets_fini();
122 fail6: linux_atomic64_fini();
123 fail5: linux_writecomb_fini();
124 fail4: linux_workqueue_fini();
125 fail3: linux_rcu_gc_fini();
126 fail2: linux_kmap_fini();
127 fail1: linux_idr_module_fini();
128 fail0: return error;
129 }
130
131 int linux_guarantee_initialized(void); /* XXX */
132 int
133 linux_guarantee_initialized(void)
134 {
135 #ifdef _MODULE
136 return 0;
137 #else
138 static ONCE_DECL(linux_init_once);
139
140 return RUN_ONCE(&linux_init_once, &linux_init);
141 #endif
142 }
143
144 static void
145 linux_fini(void)
146 {
147
148 linux_irq_work_fini();
149 linux_kthread_fini();
150 linux_wait_bit_fini();
151 linux_tasklets_fini();
152 linux_atomic64_fini();
153 linux_writecomb_fini();
154 linux_workqueue_fini();
155 linux_rcu_gc_fini();
156 linux_kmap_fini();
157 linux_idr_module_fini();
158 }
159
160 static int
161 drmkms_linux_modcmd(modcmd_t cmd, void *arg __unused)
162 {
163
164 switch (cmd) {
165 case MODULE_CMD_INIT:
166 #ifdef _MODULE
167 return linux_init();
168 #else
169 return linux_guarantee_initialized();
170 #endif
171 case MODULE_CMD_FINI:
172 linux_fini();
173 return 0;
174 default:
175 return ENOTTY;
176 }
177 }
178