1 /* $NetBSD: linux_module.c,v 1.6 2015/04/13 22:24:34 pgoyette 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.6 2015/04/13 22:24:34 pgoyette 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/reservation.h>
45 #include <linux/workqueue.h>
46
47 MODULE(MODULE_CLASS_MISC, drmkms_linux, "i2cexec");
48
49 DEFINE_WW_CLASS(reservation_ww_class __cacheline_aligned);
50
51 static int
linux_init(void)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_workqueue_init();
69 if (error) {
70 printf("linux: unable to initialize workqueues: %d\n", error);
71 goto fail2;
72 }
73
74 error = linux_writecomb_init();
75 if (error) {
76 printf("linux: unable to initialize write-combining: %d\n",
77 error);
78 goto fail3;
79 }
80
81 return 0;
82
83 fail4: __unused
84 linux_writecomb_fini();
85 fail3: linux_workqueue_fini();
86 fail2: linux_kmap_fini();
87 fail1: linux_idr_module_fini();
88 fail0: return error;
89 }
90
91 int linux_guarantee_initialized(void); /* XXX */
92 int
linux_guarantee_initialized(void)93 linux_guarantee_initialized(void)
94 {
95 #ifdef _MODULE
96 return 0;
97 #else
98 static ONCE_DECL(linux_init_once);
99
100 return RUN_ONCE(&linux_init_once, &linux_init);
101 #endif
102 }
103
104 static void
linux_fini(void)105 linux_fini(void)
106 {
107
108 linux_writecomb_fini();
109 linux_workqueue_fini();
110 linux_kmap_fini();
111 linux_idr_module_fini();
112 }
113
114 static int
drmkms_linux_modcmd(modcmd_t cmd,void * arg __unused)115 drmkms_linux_modcmd(modcmd_t cmd, void *arg __unused)
116 {
117
118 switch (cmd) {
119 case MODULE_CMD_INIT:
120 #ifdef _MODULE
121 return linux_init();
122 #else
123 return linux_guarantee_initialized();
124 #endif
125 case MODULE_CMD_FINI:
126 linux_fini();
127 return 0;
128 default:
129 return ENOTTY;
130 }
131 }
132