xref: /netbsd-src/sys/external/bsd/drm2/i915drm/i915_module.c (revision d90df327193d174d9a1e65991de539e06a199d6a)
1 /*	$NetBSD: i915_module.c,v 1.19 2022/07/17 14:10:43 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2013 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: i915_module.c,v 1.19 2022/07/17 14:10:43 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_sysctl.h>
44 
45 #include "i915_drv.h"
46 #include "i915_globals.h"
47 #include "gt/intel_rps.h"
48 
49 MODULE(MODULE_CLASS_DRIVER, i915drmkms, "acpivga,drmkms,drmkms_pci"); /* XXX drmkms_i2c */
50 
51 #ifdef _MODULE
52 #include "ioconf.c"
53 #endif
54 
55 struct drm_sysctl_def i915_def = DRM_SYSCTL_INIT();
56 
57 /* XXX use link sets for DEFINE_SPINLOCK */
58 extern spinlock_t i915_sw_fence_lock;
59 extern spinlock_t *const i915_schedule_lock;
60 
61 static int
i915drmkms_init(void)62 i915drmkms_init(void)
63 {
64 	int error;
65 
66 	error = drm_guarantee_initialized();
67 	if (error)
68 		return error;
69 
70 	/* XXX errno Linux->NetBSD */
71 	error = -i915_globals_init();
72 	if (error)
73 		return error;
74 
75 	drm_sysctl_init(&i915_def);
76 	spin_lock_init(&mchdev_lock);
77 	spin_lock_init(&i915_sw_fence_lock);
78 	spin_lock_init(i915_schedule_lock);
79 
80 	return 0;
81 }
82 
83 int	i915drmkms_guarantee_initialized(void); /* XXX */
84 int
i915drmkms_guarantee_initialized(void)85 i915drmkms_guarantee_initialized(void)
86 {
87 #ifdef _MODULE
88 	return 0;
89 #else
90 	static ONCE_DECL(i915drmkms_init_once);
91 
92 	return RUN_ONCE(&i915drmkms_init_once, &i915drmkms_init);
93 #endif
94 }
95 
96 static void
i915drmkms_fini(void)97 i915drmkms_fini(void)
98 {
99 
100 	spin_lock_destroy(i915_schedule_lock);
101 	spin_lock_destroy(&i915_sw_fence_lock);
102 	spin_lock_destroy(&mchdev_lock);
103 	drm_sysctl_fini(&i915_def);
104 
105 	i915_globals_exit();
106 }
107 
108 static int
i915drmkms_modcmd(modcmd_t cmd,void * arg __unused)109 i915drmkms_modcmd(modcmd_t cmd, void *arg __unused)
110 {
111 	int error;
112 
113 	switch (cmd) {
114 	case MODULE_CMD_INIT:
115 		/* XXX Kludge it up...  Must happen before attachment.  */
116 #ifdef _MODULE
117 		error = i915drmkms_init();
118 #else
119 		error = i915drmkms_guarantee_initialized();
120 #endif
121 		if (error) {
122 			aprint_error("i915drmkms: failed to initialize: %d\n",
123 			    error);
124 			return error;
125 		}
126 #ifdef _MODULE
127 		error = config_init_component(cfdriver_ioconf_i915drmkms,
128 		    cfattach_ioconf_i915drmkms, cfdata_ioconf_i915drmkms);
129 		if (error) {
130 			aprint_error("i915drmkms: failed to init component"
131 			    ": %d\n", error);
132 			i915drmkms_fini();
133 			return error;
134 		}
135 #endif
136 		return 0;
137 
138 	case MODULE_CMD_FINI:
139 #ifdef _MODULE
140 		error = config_fini_component(cfdriver_ioconf_i915drmkms,
141 		    cfattach_ioconf_i915drmkms, cfdata_ioconf_i915drmkms);
142 		if (error) {
143 			aprint_error("i915drmkms: failed to fini component"
144 			    ": %d\n", error);
145 			return error;
146 		}
147 #endif
148 		i915drmkms_fini();
149 		return 0;
150 
151 	default:
152 		return ENOTTY;
153 	}
154 }
155