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