drm_module.c revision 1.12 1 1.12 riastrad /* $NetBSD: drm_module.c,v 1.12 2018/08/27 15:31:27 riastradh 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.12 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_module.c,v 1.12 2018/08/27 15:31:27 riastradh Exp $");
34 1.2 riastrad
35 1.2 riastrad #include <sys/types.h>
36 1.12 riastrad #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.12 riastrad #include <sys/mutex.h>
41 1.6 riastrad #ifndef _MODULE
42 1.6 riastrad #include <sys/once.h>
43 1.6 riastrad #endif
44 1.4 riastrad #include <sys/reboot.h>
45 1.2 riastrad #include <sys/systm.h>
46 1.2 riastrad
47 1.6 riastrad #include <linux/mutex.h>
48 1.6 riastrad
49 1.2 riastrad #include <drm/drmP.h>
50 1.10 riastrad #include <drm/drm_encoder_slave.h>
51 1.12 riastrad #include <drm/drm_internal.h>
52 1.9 christos #include <drm/drm_sysctl.h>
53 1.2 riastrad
54 1.2 riastrad /*
55 1.2 riastrad * XXX I2C stuff should be moved to a separate drmkms_i2c module.
56 1.2 riastrad */
57 1.11 pgoyette MODULE(MODULE_CLASS_DRIVER, drmkms, "drmkms_linux");
58 1.2 riastrad
59 1.6 riastrad struct mutex drm_global_mutex;
60 1.6 riastrad
61 1.9 christos struct drm_sysctl_def drm_def = DRM_SYSCTL_INIT();
62 1.9 christos
63 1.12 riastrad static struct {
64 1.12 riastrad kmutex_t lock;
65 1.12 riastrad kcondvar_t cv;
66 1.12 riastrad unsigned refcnt;
67 1.12 riastrad int (*hook)(struct drm_device *,
68 1.12 riastrad struct drm_master *, struct drm_unique *);
69 1.12 riastrad } set_unique_hook __cacheline_aligned;
70 1.12 riastrad
71 1.6 riastrad static int
72 1.6 riastrad drm_init(void)
73 1.6 riastrad {
74 1.6 riastrad extern int linux_guarantee_initialized(void);
75 1.6 riastrad int error;
76 1.6 riastrad
77 1.6 riastrad error = linux_guarantee_initialized();
78 1.6 riastrad if (error)
79 1.6 riastrad return error;
80 1.6 riastrad
81 1.6 riastrad if (ISSET(boothowto, AB_DEBUG))
82 1.6 riastrad drm_debug = ~(unsigned int)0;
83 1.6 riastrad
84 1.6 riastrad spin_lock_init(&drm_minor_lock);
85 1.6 riastrad idr_init(&drm_minors_idr);
86 1.6 riastrad linux_mutex_init(&drm_global_mutex);
87 1.6 riastrad drm_connector_ida_init();
88 1.7 riastrad drm_global_init();
89 1.9 christos drm_sysctl_init(&drm_def);
90 1.10 riastrad drm_i2c_encoders_init();
91 1.6 riastrad
92 1.6 riastrad return 0;
93 1.6 riastrad }
94 1.6 riastrad
95 1.6 riastrad int drm_guarantee_initialized(void); /* XXX */
96 1.6 riastrad int
97 1.6 riastrad drm_guarantee_initialized(void)
98 1.6 riastrad {
99 1.5 riastrad #ifdef _MODULE
100 1.6 riastrad return 0;
101 1.5 riastrad #else
102 1.6 riastrad static ONCE_DECL(drm_init_once);
103 1.6 riastrad
104 1.6 riastrad return RUN_ONCE(&drm_init_once, &drm_init);
105 1.2 riastrad #endif
106 1.6 riastrad }
107 1.6 riastrad
108 1.6 riastrad static void
109 1.6 riastrad drm_fini(void)
110 1.6 riastrad {
111 1.10 riastrad
112 1.10 riastrad drm_i2c_encoders_fini();
113 1.9 christos drm_sysctl_fini(&drm_def);
114 1.7 riastrad drm_global_release();
115 1.6 riastrad drm_connector_ida_destroy();
116 1.6 riastrad linux_mutex_destroy(&drm_global_mutex);
117 1.6 riastrad idr_destroy(&drm_minors_idr);
118 1.6 riastrad spin_lock_destroy(&drm_minor_lock);
119 1.6 riastrad }
120 1.2 riastrad
121 1.12 riastrad int
122 1.12 riastrad drm_irq_by_busid(struct drm_device *dev, void *data, struct drm_file *file)
123 1.12 riastrad {
124 1.12 riastrad
125 1.12 riastrad return -ENODEV;
126 1.12 riastrad }
127 1.12 riastrad
128 1.12 riastrad /* XXX Stupid kludge... */
129 1.12 riastrad
130 1.12 riastrad void
131 1.12 riastrad drm_pci_set_unique_hook(int (**hook)(struct drm_device *, struct drm_master *,
132 1.12 riastrad struct drm_unique *))
133 1.12 riastrad {
134 1.12 riastrad int (*old)(struct drm_device *, struct drm_master *,
135 1.12 riastrad struct drm_unique *);
136 1.12 riastrad
137 1.12 riastrad mutex_enter(&set_unique_hook.lock);
138 1.12 riastrad while (set_unique_hook.refcnt)
139 1.12 riastrad cv_wait(&set_unique_hook.cv, &set_unique_hook.lock);
140 1.12 riastrad old = set_unique_hook.hook;
141 1.12 riastrad set_unique_hook.hook = *hook;
142 1.12 riastrad *hook = old;
143 1.12 riastrad mutex_exit(&set_unique_hook.lock);
144 1.12 riastrad }
145 1.12 riastrad
146 1.12 riastrad int
147 1.12 riastrad drm_pci_set_unique(struct drm_device *dev, struct drm_master *master,
148 1.12 riastrad struct drm_unique *unique)
149 1.12 riastrad {
150 1.12 riastrad int ret;
151 1.12 riastrad
152 1.12 riastrad mutex_enter(&set_unique_hook.lock);
153 1.12 riastrad while (set_unique_hook.refcnt == UINT_MAX)
154 1.12 riastrad cv_wait(&set_unique_hook.cv, &set_unique_hook.lock);
155 1.12 riastrad set_unique_hook.refcnt++;
156 1.12 riastrad mutex_exit(&set_unique_hook.lock);
157 1.12 riastrad
158 1.12 riastrad if (set_unique_hook.hook)
159 1.12 riastrad ret = set_unique_hook.hook(dev, master, unique);
160 1.12 riastrad else
161 1.12 riastrad ret = -ENODEV;
162 1.12 riastrad
163 1.12 riastrad mutex_enter(&set_unique_hook.lock);
164 1.12 riastrad if (set_unique_hook.refcnt-- == UINT_MAX ||
165 1.12 riastrad set_unique_hook.refcnt == 0)
166 1.12 riastrad cv_broadcast(&set_unique_hook.cv);
167 1.12 riastrad mutex_exit(&set_unique_hook.lock);
168 1.12 riastrad
169 1.12 riastrad return ret;
170 1.12 riastrad }
171 1.12 riastrad
172 1.2 riastrad static int
173 1.2 riastrad drmkms_modcmd(modcmd_t cmd, void *arg __unused)
174 1.2 riastrad {
175 1.2 riastrad #ifdef _MODULE
176 1.2 riastrad devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
177 1.6 riastrad #endif
178 1.2 riastrad int error;
179 1.2 riastrad
180 1.2 riastrad switch (cmd) {
181 1.2 riastrad case MODULE_CMD_INIT:
182 1.2 riastrad #ifdef _MODULE
183 1.6 riastrad error = drm_init();
184 1.6 riastrad #else
185 1.6 riastrad error = drm_guarantee_initialized();
186 1.6 riastrad #endif
187 1.6 riastrad if (error)
188 1.6 riastrad return error;
189 1.6 riastrad #ifdef _MODULE
190 1.2 riastrad error = devsw_attach("drm", NULL, &bmajor,
191 1.2 riastrad &drm_cdevsw, &cmajor);
192 1.2 riastrad if (error) {
193 1.2 riastrad aprint_error("drmkms: unable to attach devsw: %d\n",
194 1.2 riastrad error);
195 1.6 riastrad return error;
196 1.2 riastrad }
197 1.2 riastrad #endif
198 1.2 riastrad return 0;
199 1.2 riastrad
200 1.2 riastrad case MODULE_CMD_FINI:
201 1.2 riastrad #ifdef _MODULE
202 1.2 riastrad error = devsw_detach(NULL, &drm_cdevsw);
203 1.2 riastrad if (error)
204 1.2 riastrad return error;
205 1.2 riastrad #endif
206 1.6 riastrad drm_fini();
207 1.2 riastrad return 0;
208 1.2 riastrad
209 1.2 riastrad default:
210 1.2 riastrad return ENOTTY;
211 1.2 riastrad }
212 1.2 riastrad }
213