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