nouveau_module.c revision 1.1.4.1 1 /* $NetBSD: nouveau_module.c,v 1.1.4.1 2014/09/21 17:41:53 snj 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: nouveau_module.c,v 1.1.4.1 2014/09/21 17:41:53 snj Exp $");
34
35 #include <sys/types.h>
36 #include <sys/module.h>
37 #ifndef _MODULE
38 #include <sys/once.h>
39 #endif
40 #include <sys/systm.h>
41
42 #include <drm/drmP.h>
43
44 #include <core/object.h>
45 #include <engine/device.h>
46
47 MODULE(MODULE_CLASS_DRIVER, nouveau, "drmkms,drmkms_pci"); /* XXX drmkms_i2c, drmkms_ttm */
48
49 #ifdef _MODULE
50 #include "ioconf.c"
51 #endif
52
53 extern struct drm_driver *const nouveau_drm_driver; /* XXX */
54
55 static int
56 nouveau_init(void)
57 {
58 extern int drm_guarantee_initialized(void);
59 int error;
60
61 error = drm_guarantee_initialized();
62 if (error)
63 return error;
64
65 error = drm_pci_init(nouveau_drm_driver, NULL);
66 if (error) {
67 aprint_error("nouveau: failed to init pci: %d\n", error);
68 return error;
69 }
70
71 nouveau_objects_init();
72 nouveau_devices_init();
73 #if 0 /* XXX nouveau acpi */
74 nouveau_register_dsm_handler();
75 #endif
76
77 return 0;
78 }
79
80 int nouveau_guarantee_initialized(void); /* XXX */
81 int
82 nouveau_guarantee_initialized(void)
83 {
84 #ifdef _MODULE
85 return 0;
86 #else
87 static ONCE_DECL(nouveau_init_once);
88
89 return RUN_ONCE(&nouveau_init_once, &nouveau_init);
90 #endif
91 }
92
93 static void
94 nouveau_fini(void)
95 {
96
97 #if 0 /* XXX nouveau acpi */
98 nouveau_unregister_dsm_handler();
99 #endif
100 nouveau_devices_fini();
101 nouveau_objects_fini();
102 drm_pci_exit(nouveau_drm_driver, NULL);
103 }
104
105 static int
106 nouveau_modcmd(modcmd_t cmd, void *arg __unused)
107 {
108 int error;
109
110 switch (cmd) {
111 case MODULE_CMD_INIT:
112 #ifdef _MODULE
113 error = nouveau_init();
114 #else
115 error = nouveau_guarantee_initialized();
116 #endif
117 if (error) {
118 aprint_error("nouveau: failed to initialize: %d\n",
119 error);
120 return error;
121 }
122 #ifdef _MODULE
123 error = config_init_component(cfdriver_ioconf_nouveau,
124 cfattach_ioconf_nouveau, cfdata_ioconf_nouveau);
125 if (error) {
126 aprint_error("nouveau: failed to init component"
127 ": %d\n", error);
128 nouveau_fini();
129 return error;
130 }
131 #endif
132 return 0;
133
134 case MODULE_CMD_FINI:
135 #ifdef _MODULE
136 error = config_fini_component(cfdriver_ioconf_nouveau,
137 cfattach_ioconf_nouveau, cfdata_ioconf_nouveau);
138 if (error) {
139 aprint_error("nouveau: failed to fini component"
140 ": %d\n", error);
141 return error;
142 }
143 #endif
144 nouveau_fini();
145 return 0;
146
147 default:
148 return ENOTTY;
149 }
150 }
151