xref: /netbsd-src/sys/external/bsd/drm2/radeon/radeon_module.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: radeon_module.c,v 1.3 2014/11/12 03:14:00 christos 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: radeon_module.c,v 1.3 2014/11/12 03:14:00 christos 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 #include <drm/drm_sysctl.h>
44 
45 #include "radeon_drv.h"
46 
47 MODULE(MODULE_CLASS_DRIVER, radeon, "drmkms,drmkms_pci"); /* XXX drmkms_i2c, drmkms_ttm */
48 
49 #ifdef _MODULE
50 #include "ioconf.c"
51 #endif
52 
53 /* XXX Kludge to get these from radeon_drv.c.  */
54 extern struct drm_driver *const radeon_drm_driver;
55 extern int radeon_max_kms_ioctl;
56 
57 struct drm_sysctl_def radeon_def = DRM_SYSCTL_INIT();
58 
59 static int
60 radeon_init(void)
61 {
62 	extern int drm_guarantee_initialized(void);
63 	int error;
64 
65 	error = drm_guarantee_initialized();
66 	if (error)
67 		return error;
68 
69 	radeon_drm_driver->num_ioctls = radeon_max_kms_ioctl;
70 	radeon_drm_driver->driver_features |= DRIVER_MODESET;
71 
72 	error = drm_pci_init(radeon_drm_driver, NULL);
73 	if (error) {
74 		aprint_error("radeon: failed to init pci: %d\n",
75 		    error);
76 		return error;
77 	}
78 	drm_sysctl_init(&radeon_def);
79 
80 	return 0;
81 }
82 
83 int	radeon_guarantee_initialized(void); /* XXX */
84 int
85 radeon_guarantee_initialized(void)
86 {
87 #ifdef _MODULE
88 	return 0;
89 #else
90 	static ONCE_DECL(radeon_init_once);
91 
92 	return RUN_ONCE(&radeon_init_once, &radeon_init);
93 #endif
94 }
95 
96 static void
97 radeon_fini(void)
98 {
99 	drm_sysctl_fini(&radeon_def);
100 	drm_pci_exit(radeon_drm_driver, NULL);
101 }
102 
103 static int
104 radeon_modcmd(modcmd_t cmd, void *arg __unused)
105 {
106 	int error;
107 
108 	switch (cmd) {
109 	case MODULE_CMD_INIT:
110 		/* XXX Kludge it up...  Must happen before attachment.  */
111 #ifdef _MODULE
112 		error = radeon_init();
113 #else
114 		error = radeon_guarantee_initialized();
115 #endif
116 		if (error) {
117 			aprint_error("radeon: failed to initialize: %d\n",
118 			    error);
119 			return error;
120 		}
121 #ifdef _MODULE
122 		error = config_init_component(cfdriver_ioconf_radeon,
123 		    cfattach_ioconf_radeon, cfdata_ioconf_radeon);
124 		if (error) {
125 			aprint_error("radeon: failed to init component"
126 			    ": %d\n", error);
127 			radeon_fini();
128 			return error;
129 		}
130 #endif
131 		return 0;
132 
133 	case MODULE_CMD_FINI:
134 #ifdef _MODULE
135 		error = config_fini_component(cfdriver_ioconf_radeon,
136 		    cfattach_ioconf_radeon, cfdata_ioconf_radeon);
137 		if (error) {
138 			aprint_error("radeon: failed to fini component"
139 			    ": %d\n", error);
140 			return error;
141 		}
142 #endif
143 		radeon_fini();
144 		return 0;
145 
146 	default:
147 		return ENOTTY;
148 	}
149 }
150