1 /*	$NetBSD: radeon_module.c,v 1.8 2022/07/17 15:36:59 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.8 2022/07/17 15:36:59 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/drm_device.h>
43 #include <drm/drm_drv.h>
44 #include <drm/drm_sysctl.h>
45 
46 #include "radeon_drv.h"
47 
48 MODULE(MODULE_CLASS_DRIVER, radeon, "drmkms,drmkms_pci,drmkms_ttm"); /* XXX drmkms_i2c */
49 
50 #ifdef _MODULE
51 #include "ioconf.c"
52 #endif
53 
54 /* XXX Kludge to get these from radeon_drv.c.  */
55 extern struct drm_driver *const radeon_drm_driver;
56 extern int radeon_max_kms_ioctl;
57 
58 struct drm_sysctl_def radeon_def = DRM_SYSCTL_INIT();
59 
60 static int
radeon_init(void)61 radeon_init(void)
62 {
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 	drm_sysctl_init(&radeon_def);
73 
74 	return 0;
75 }
76 
77 int	radeon_guarantee_initialized(void); /* XXX */
78 int
radeon_guarantee_initialized(void)79 radeon_guarantee_initialized(void)
80 {
81 #ifdef _MODULE
82 	return 0;
83 #else
84 	static ONCE_DECL(radeon_init_once);
85 
86 	return RUN_ONCE(&radeon_init_once, &radeon_init);
87 #endif
88 }
89 
90 static void
radeon_fini(void)91 radeon_fini(void)
92 {
93 
94 	drm_sysctl_fini(&radeon_def);
95 }
96 
97 static int
radeon_modcmd(modcmd_t cmd,void * arg __unused)98 radeon_modcmd(modcmd_t cmd, void *arg __unused)
99 {
100 	int error;
101 
102 	switch (cmd) {
103 	case MODULE_CMD_INIT:
104 		/* XXX Kludge it up...  Must happen before attachment.  */
105 #ifdef _MODULE
106 		error = radeon_init();
107 #else
108 		error = radeon_guarantee_initialized();
109 #endif
110 		if (error) {
111 			aprint_error("radeon: failed to initialize: %d\n",
112 			    error);
113 			return error;
114 		}
115 #ifdef _MODULE
116 		error = config_init_component(cfdriver_ioconf_radeon,
117 		    cfattach_ioconf_radeon, cfdata_ioconf_radeon);
118 		if (error) {
119 			aprint_error("radeon: failed to init component"
120 			    ": %d\n", error);
121 			radeon_fini();
122 			return error;
123 		}
124 #endif
125 		return 0;
126 
127 	case MODULE_CMD_FINI:
128 #ifdef _MODULE
129 		error = config_fini_component(cfdriver_ioconf_radeon,
130 		    cfattach_ioconf_radeon, cfdata_ioconf_radeon);
131 		if (error) {
132 			aprint_error("radeon: failed to fini component"
133 			    ": %d\n", error);
134 			return error;
135 		}
136 #endif
137 		radeon_fini();
138 		return 0;
139 
140 	default:
141 		return ENOTTY;
142 	}
143 }
144