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