xref: /netbsd-src/sys/external/bsd/drm2/via/via_module.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: via_module.c,v 1.2 2015/09/05 23:04:01 dholland Exp $	*/
2 
3 /*-
4  * Copyright (c) 2015 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: via_module.c,v 1.2 2015/09/05 23:04:01 dholland Exp $");
34 
35 #include <sys/types.h>
36 #include <sys/module.h>
37 #include <sys/once.h>
38 
39 #include <drm/drmP.h>
40 #include <drm/via_drm.h>
41 
42 #include "via_drv.h"
43 
44 MODULE(MODULE_CLASS_DRIVER, viadrmums, "drmkms,drmkms_pci");
45 
46 #ifdef _MODULE
47 #include "ioconf.c"
48 #endif
49 
50 extern struct drm_driver *const via_drm_driver; /* XXX */
51 
52 static int
53 viadrm_init(void)
54 {
55 	extern int drm_guarantee_initialized(void);
56 	int error;
57 
58 	via_init_command_verifier(); /* idempotent, no unwind needed */
59 
60 	error = drm_guarantee_initialized();
61 	if (error)
62 		return error;
63 
64 	error = drm_pci_init(via_drm_driver, NULL);
65 	if (error) {
66 		aprint_error("viadrmkms: failed to init pci: %d\n",
67 		    error);
68 		return error;
69 	}
70 
71 	return 0;
72 }
73 
74 int	viadrm_guarantee_initialized(void); /* XXX */
75 int
76 viadrm_guarantee_initialized(void)
77 {
78 #ifdef _MODULE
79 	return 0;
80 #else
81 	static ONCE_DECL(viadrm_init_once);
82 
83 	return RUN_ONCE(&viadrm_init_once, &viadrm_init);
84 #endif
85 }
86 
87 static void
88 viadrm_fini(void)
89 {
90 
91 	drm_pci_exit(via_drm_driver, NULL);
92 }
93 
94 static int
95 viadrmums_modcmd(modcmd_t cmd, void *arg __unused)
96 {
97 	int error;
98 
99 	switch (cmd) {
100 	case MODULE_CMD_INIT:
101 #ifdef _MODULE
102 		error = viadrm_init();
103 #else
104 		error = viadrm_guarantee_initialized();
105 #endif
106 		if (error) {
107 			aprint_error("viadrmums: failed to initialize: %d\n",
108 			    error);
109 			return error;
110 		}
111 #ifdef _MODULE
112 		error = config_init_component(cfdriver_ioconf_viadrmums,
113 		    cfattach_ioconf_viadrmums, cfdata_ioconf_viadrmums);
114 		if (error) {
115 			aprint_error("viadrmums: failed to init component"
116 			    ": %d\n", error);
117 			viadrm_fini();
118 			return error;
119 		}
120 #endif
121 		return 0;
122 
123 	case MODULE_CMD_FINI:
124 #ifdef _MODULE
125 		error = config_fini_component(cfdriver_ioconf_viadrmums,
126 		    cfattach_ioconf_viadrmums, cfdata_ioconf_viadrmums);
127 		if (error) {
128 			aprint_error("viadrmums: failed to fini component"
129 			    ": %d\n", error);
130 			return error;
131 		}
132 #endif
133 		viadrm_fini();
134 		return 0;
135 
136 	default:
137 		return ENOTTY;
138 	}
139 }
140