drm_module.c revision 1.11.16.1 1 1.11.16.1 pgoyette /* $NetBSD: drm_module.c,v 1.11.16.1 2018/09/06 06:56:35 pgoyette Exp $ */
2 1.2 riastrad
3 1.2 riastrad /*-
4 1.2 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.2 riastrad * All rights reserved.
6 1.2 riastrad *
7 1.2 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.2 riastrad * by Taylor R. Campbell.
9 1.2 riastrad *
10 1.2 riastrad * Redistribution and use in source and binary forms, with or without
11 1.2 riastrad * modification, are permitted provided that the following conditions
12 1.2 riastrad * are met:
13 1.2 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.2 riastrad * notice, this list of conditions and the following disclaimer.
15 1.2 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.2 riastrad * documentation and/or other materials provided with the distribution.
18 1.2 riastrad *
19 1.2 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.2 riastrad */
31 1.2 riastrad
32 1.2 riastrad #include <sys/cdefs.h>
33 1.11.16.1 pgoyette __KERNEL_RCSID(0, "$NetBSD: drm_module.c,v 1.11.16.1 2018/09/06 06:56:35 pgoyette Exp $");
34 1.2 riastrad
35 1.2 riastrad #include <sys/types.h>
36 1.11.16.1 pgoyette #include <sys/condvar.h>
37 1.8 riastrad #include <sys/conf.h>
38 1.2 riastrad #include <sys/device.h>
39 1.2 riastrad #include <sys/module.h>
40 1.11.16.1 pgoyette #include <sys/mutex.h>
41 1.6 riastrad #include <sys/once.h>
42 1.4 riastrad #include <sys/reboot.h>
43 1.2 riastrad #include <sys/systm.h>
44 1.2 riastrad
45 1.6 riastrad #include <linux/mutex.h>
46 1.6 riastrad
47 1.2 riastrad #include <drm/drmP.h>
48 1.10 riastrad #include <drm/drm_encoder_slave.h>
49 1.11.16.1 pgoyette #include <drm/drm_internal.h>
50 1.9 christos #include <drm/drm_sysctl.h>
51 1.2 riastrad
52 1.2 riastrad /*
53 1.11.16.1 pgoyette * XXX This is stupid.
54 1.11.16.1 pgoyette *
55 1.11.16.1 pgoyette * 1. Builtin modules are broken: they don't get initialized before
56 1.11.16.1 pgoyette * autoconf matches devices, but we need the initialization to be
57 1.11.16.1 pgoyette * run in order to match and attach drmkms drivers.
58 1.11.16.1 pgoyette *
59 1.11.16.1 pgoyette * 2. The following dependencies are _not_ correct:
60 1.11.16.1 pgoyette * - drmkms can't depend on agp because not all drmkms drivers run
61 1.11.16.1 pgoyette * on platforms guaranteed to have pci, let alone agp
62 1.11.16.1 pgoyette * - drmkms_pci can't depend on agp because not all _pci_ has agp
63 1.11.16.1 pgoyette * (e.g., tegra)
64 1.11.16.1 pgoyette * - radeon (e.g.) can't depend on agp because not all radeon
65 1.11.16.1 pgoyette * devices are on platforms guaranteed to have agp
66 1.11.16.1 pgoyette *
67 1.11.16.1 pgoyette * 3. We need to register the agp hooks before we try to attach a
68 1.11.16.1 pgoyette * device.
69 1.11.16.1 pgoyette *
70 1.11.16.1 pgoyette * 4. The only mechanism we have to force this is the
71 1.11.16.1 pgoyette * mumblefrotz_guarantee_initialized kludge.
72 1.11.16.1 pgoyette *
73 1.11.16.1 pgoyette * 5. We don't know if we even _can_ call
74 1.11.16.1 pgoyette * drmkms_agp_guarantee_initialized unless we know NAGP.
75 1.11.16.1 pgoyette *
76 1.11.16.1 pgoyette * 6. We don't know NAGP unless we include "agp.h".
77 1.11.16.1 pgoyette *
78 1.11.16.1 pgoyette * 7. We can't include "agp.h" if the platform has agp.
79 1.11.16.1 pgoyette *
80 1.11.16.1 pgoyette * 8. The way we determine whether we have agp is NAGP.
81 1.11.16.1 pgoyette *
82 1.11.16.1 pgoyette * 9. @!*#&^@&*@!&^#@
83 1.11.16.1 pgoyette */
84 1.11.16.1 pgoyette #if defined(__powerpc__) || defined(__i386__) || defined(__x86_64__)
85 1.11.16.1 pgoyette #include "agp.h"
86 1.11.16.1 pgoyette #endif
87 1.11.16.1 pgoyette
88 1.11.16.1 pgoyette /*
89 1.2 riastrad * XXX I2C stuff should be moved to a separate drmkms_i2c module.
90 1.2 riastrad */
91 1.11 pgoyette MODULE(MODULE_CLASS_DRIVER, drmkms, "drmkms_linux");
92 1.2 riastrad
93 1.6 riastrad struct mutex drm_global_mutex;
94 1.6 riastrad
95 1.9 christos struct drm_sysctl_def drm_def = DRM_SYSCTL_INIT();
96 1.9 christos
97 1.6 riastrad static int
98 1.6 riastrad drm_init(void)
99 1.6 riastrad {
100 1.6 riastrad extern int linux_guarantee_initialized(void);
101 1.6 riastrad int error;
102 1.6 riastrad
103 1.6 riastrad error = linux_guarantee_initialized();
104 1.6 riastrad if (error)
105 1.6 riastrad return error;
106 1.6 riastrad
107 1.11.16.1 pgoyette drm_agp_hooks_init();
108 1.11.16.1 pgoyette #if NAGP > 0
109 1.11.16.1 pgoyette extern int drmkms_agp_guarantee_initialized(void);
110 1.11.16.1 pgoyette error = drmkms_agp_guarantee_initialized();
111 1.11.16.1 pgoyette if (error) {
112 1.11.16.1 pgoyette drm_agp_hooks_fini();
113 1.11.16.1 pgoyette return error;
114 1.11.16.1 pgoyette }
115 1.11.16.1 pgoyette #endif
116 1.11.16.1 pgoyette
117 1.6 riastrad if (ISSET(boothowto, AB_DEBUG))
118 1.6 riastrad drm_debug = ~(unsigned int)0;
119 1.6 riastrad
120 1.6 riastrad spin_lock_init(&drm_minor_lock);
121 1.6 riastrad idr_init(&drm_minors_idr);
122 1.6 riastrad linux_mutex_init(&drm_global_mutex);
123 1.6 riastrad drm_connector_ida_init();
124 1.7 riastrad drm_global_init();
125 1.9 christos drm_sysctl_init(&drm_def);
126 1.10 riastrad drm_i2c_encoders_init();
127 1.6 riastrad
128 1.6 riastrad return 0;
129 1.6 riastrad }
130 1.6 riastrad
131 1.6 riastrad int
132 1.6 riastrad drm_guarantee_initialized(void)
133 1.6 riastrad {
134 1.5 riastrad #ifdef _MODULE
135 1.6 riastrad return 0;
136 1.5 riastrad #else
137 1.6 riastrad static ONCE_DECL(drm_init_once);
138 1.6 riastrad
139 1.6 riastrad return RUN_ONCE(&drm_init_once, &drm_init);
140 1.2 riastrad #endif
141 1.6 riastrad }
142 1.6 riastrad
143 1.6 riastrad static void
144 1.6 riastrad drm_fini(void)
145 1.6 riastrad {
146 1.10 riastrad
147 1.10 riastrad drm_i2c_encoders_fini();
148 1.9 christos drm_sysctl_fini(&drm_def);
149 1.7 riastrad drm_global_release();
150 1.6 riastrad drm_connector_ida_destroy();
151 1.6 riastrad linux_mutex_destroy(&drm_global_mutex);
152 1.6 riastrad idr_destroy(&drm_minors_idr);
153 1.6 riastrad spin_lock_destroy(&drm_minor_lock);
154 1.11.16.1 pgoyette drm_agp_hooks_fini();
155 1.11.16.1 pgoyette }
156 1.11.16.1 pgoyette
157 1.11.16.1 pgoyette int
158 1.11.16.1 pgoyette drm_irq_by_busid(struct drm_device *dev, void *data, struct drm_file *file)
159 1.11.16.1 pgoyette {
160 1.11.16.1 pgoyette
161 1.11.16.1 pgoyette return -ENODEV;
162 1.6 riastrad }
163 1.2 riastrad
164 1.2 riastrad static int
165 1.2 riastrad drmkms_modcmd(modcmd_t cmd, void *arg __unused)
166 1.2 riastrad {
167 1.2 riastrad #ifdef _MODULE
168 1.2 riastrad devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
169 1.6 riastrad #endif
170 1.2 riastrad int error;
171 1.2 riastrad
172 1.2 riastrad switch (cmd) {
173 1.2 riastrad case MODULE_CMD_INIT:
174 1.2 riastrad #ifdef _MODULE
175 1.6 riastrad error = drm_init();
176 1.6 riastrad #else
177 1.6 riastrad error = drm_guarantee_initialized();
178 1.6 riastrad #endif
179 1.6 riastrad if (error)
180 1.6 riastrad return error;
181 1.6 riastrad #ifdef _MODULE
182 1.2 riastrad error = devsw_attach("drm", NULL, &bmajor,
183 1.2 riastrad &drm_cdevsw, &cmajor);
184 1.2 riastrad if (error) {
185 1.2 riastrad aprint_error("drmkms: unable to attach devsw: %d\n",
186 1.2 riastrad error);
187 1.6 riastrad return error;
188 1.2 riastrad }
189 1.2 riastrad #endif
190 1.2 riastrad return 0;
191 1.2 riastrad
192 1.2 riastrad case MODULE_CMD_FINI:
193 1.2 riastrad #ifdef _MODULE
194 1.2 riastrad error = devsw_detach(NULL, &drm_cdevsw);
195 1.2 riastrad if (error)
196 1.2 riastrad return error;
197 1.2 riastrad #endif
198 1.6 riastrad drm_fini();
199 1.2 riastrad return 0;
200 1.2 riastrad
201 1.2 riastrad default:
202 1.2 riastrad return ENOTTY;
203 1.2 riastrad }
204 1.2 riastrad }
205