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