xref: /netbsd-src/sys/external/bsd/drm2/linux/linux_module.c (revision 4e9d6760b362573e25ae4cf0d99ab0c048b10bca)
1 /*	$NetBSD: linux_module.c,v 1.10 2021/12/19 01:17:14 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.10 2021/12/19 01:17:14 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/workqueue.h>
48 
49 MODULE(MODULE_CLASS_MISC, drmkms_linux, "i2cexec");
50 
51 static int
52 linux_init(void)
53 {
54 	int error;
55 
56 	error = linux_idr_module_init();
57 	if (error) {
58 		printf("linux: unable to initialize idr: %d\n", error);
59 		goto fail0;
60 	}
61 
62 	error = linux_kmap_init();
63 	if (error) {
64 		printf("linux: unable to initialize kmap: %d\n", error);
65 		goto fail1;
66 	}
67 
68 	error = linux_rcu_gc_init();
69 	if (error) {
70 		printf("linux: unable to initialize rcu gc: %d\n", error);
71 		goto fail2;
72 	}
73 
74 	error = linux_workqueue_init();
75 	if (error) {
76 		printf("linux: unable to initialize workqueues: %d\n", error);
77 		goto fail3;
78 	}
79 
80 	error = linux_writecomb_init();
81 	if (error) {
82 		printf("linux: unable to initialize write-combining: %d\n",
83 		    error);
84 		goto fail4;
85 	}
86 
87 	error = linux_atomic64_init();
88 	if (error) {
89 		printf("linux: unable to initialize atomic64: %d\n", error);
90 		goto fail5;
91 	}
92 
93 	error = linux_tasklets_init();
94 	if (error) {
95 		printf("linux: unable to initialize tasklets: %d\n", error);
96 		goto fail6;
97 	}
98 
99 	return 0;
100 
101 fail7: __unused
102 	linux_tasklets_fini();
103 fail6:	linux_atomic64_fini();
104 fail5:	linux_writecomb_fini();
105 fail4:	linux_workqueue_fini();
106 fail3:	linux_rcu_gc_fini();
107 fail2:	linux_kmap_fini();
108 fail1:	linux_idr_module_fini();
109 fail0:	return error;
110 }
111 
112 int	linux_guarantee_initialized(void); /* XXX */
113 int
114 linux_guarantee_initialized(void)
115 {
116 #ifdef _MODULE
117 	return 0;
118 #else
119 	static ONCE_DECL(linux_init_once);
120 
121 	return RUN_ONCE(&linux_init_once, &linux_init);
122 #endif
123 }
124 
125 static void
126 linux_fini(void)
127 {
128 
129 	linux_tasklets_fini();
130 	linux_atomic64_fini();
131 	linux_writecomb_fini();
132 	linux_workqueue_fini();
133 	linux_rcu_gc_fini();
134 	linux_kmap_fini();
135 	linux_idr_module_fini();
136 }
137 
138 static int
139 drmkms_linux_modcmd(modcmd_t cmd, void *arg __unused)
140 {
141 
142 	switch (cmd) {
143 	case MODULE_CMD_INIT:
144 #ifdef _MODULE
145 		return linux_init();
146 #else
147 		return linux_guarantee_initialized();
148 #endif
149 	case MODULE_CMD_FINI:
150 		linux_fini();
151 		return 0;
152 	default:
153 		return ENOTTY;
154 	}
155 }
156