xref: /netbsd-src/sys/external/bsd/drm2/radeon/radeon_module.c (revision cef8759bd76c1b621f8eab8faa6f208faabc2e15)
1 /*	$NetBSD: radeon_module.c,v 1.5 2018/08/28 03:35:08 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: radeon_module.c,v 1.5 2018/08/28 03:35:08 riastradh 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 	int error;
63 
64 	error = drm_guarantee_initialized();
65 	if (error)
66 		return error;
67 
68 	radeon_drm_driver->num_ioctls = radeon_max_kms_ioctl;
69 	radeon_drm_driver->driver_features |= DRIVER_MODESET;
70 
71 	drm_sysctl_init(&radeon_def);
72 
73 	return 0;
74 }
75 
76 int	radeon_guarantee_initialized(void); /* XXX */
77 int
78 radeon_guarantee_initialized(void)
79 {
80 #ifdef _MODULE
81 	return 0;
82 #else
83 	static ONCE_DECL(radeon_init_once);
84 
85 	return RUN_ONCE(&radeon_init_once, &radeon_init);
86 #endif
87 }
88 
89 static void
90 radeon_fini(void)
91 {
92 
93 	drm_sysctl_fini(&radeon_def);
94 }
95 
96 static int
97 radeon_modcmd(modcmd_t cmd, void *arg __unused)
98 {
99 	int error;
100 
101 	switch (cmd) {
102 	case MODULE_CMD_INIT:
103 		/* XXX Kludge it up...  Must happen before attachment.  */
104 #ifdef _MODULE
105 		error = radeon_init();
106 #else
107 		error = radeon_guarantee_initialized();
108 #endif
109 		if (error) {
110 			aprint_error("radeon: failed to initialize: %d\n",
111 			    error);
112 			return error;
113 		}
114 #ifdef _MODULE
115 		error = config_init_component(cfdriver_ioconf_radeon,
116 		    cfattach_ioconf_radeon, cfdata_ioconf_radeon);
117 		if (error) {
118 			aprint_error("radeon: failed to init component"
119 			    ": %d\n", error);
120 			radeon_fini();
121 			return error;
122 		}
123 #endif
124 		return 0;
125 
126 	case MODULE_CMD_FINI:
127 #ifdef _MODULE
128 		error = config_fini_component(cfdriver_ioconf_radeon,
129 		    cfattach_ioconf_radeon, cfdata_ioconf_radeon);
130 		if (error) {
131 			aprint_error("radeon: failed to fini component"
132 			    ": %d\n", error);
133 			return error;
134 		}
135 #endif
136 		radeon_fini();
137 		return 0;
138 
139 	default:
140 		return ENOTTY;
141 	}
142 }
143