drm_agp_hook.c revision 1.3 1 1.3 mrg /* $NetBSD: drm_agp_hook.c,v 1.3 2018/08/30 22:39:54 mrg Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.1 riastrad #include <sys/cdefs.h>
33 1.3 mrg __KERNEL_RCSID(0, "$NetBSD: drm_agp_hook.c,v 1.3 2018/08/30 22:39:54 mrg Exp $");
34 1.1 riastrad
35 1.1 riastrad #include <sys/types.h>
36 1.1 riastrad #include <sys/condvar.h>
37 1.1 riastrad #include <sys/errno.h>
38 1.1 riastrad #include <sys/mutex.h>
39 1.1 riastrad #include <sys/once.h>
40 1.1 riastrad
41 1.1 riastrad #include <drm/drmP.h>
42 1.1 riastrad #include <drm/drm_internal.h>
43 1.1 riastrad
44 1.1 riastrad static struct {
45 1.1 riastrad kmutex_t lock;
46 1.1 riastrad kcondvar_t cv;
47 1.1 riastrad unsigned refcnt; /* at most one per device */
48 1.1 riastrad const struct drm_agp_hooks *hooks;
49 1.1 riastrad } agp_hooks __cacheline_aligned;
50 1.1 riastrad
51 1.1 riastrad void
52 1.1 riastrad drm_agp_hooks_init(void)
53 1.1 riastrad {
54 1.1 riastrad
55 1.1 riastrad mutex_init(&agp_hooks.lock, MUTEX_DEFAULT, IPL_NONE);
56 1.1 riastrad cv_init(&agp_hooks.cv, "agphooks");
57 1.1 riastrad agp_hooks.refcnt = 0;
58 1.1 riastrad agp_hooks.hooks = NULL;
59 1.1 riastrad }
60 1.1 riastrad
61 1.1 riastrad void
62 1.1 riastrad drm_agp_hooks_fini(void)
63 1.1 riastrad {
64 1.1 riastrad
65 1.1 riastrad KASSERT(agp_hooks.hooks == NULL);
66 1.1 riastrad KASSERT(agp_hooks.refcnt == 0);
67 1.1 riastrad cv_destroy(&agp_hooks.cv);
68 1.1 riastrad mutex_destroy(&agp_hooks.lock);
69 1.1 riastrad }
70 1.1 riastrad
71 1.1 riastrad int
72 1.1 riastrad drm_agp_register(const struct drm_agp_hooks *hooks)
73 1.1 riastrad {
74 1.1 riastrad int error = 0;
75 1.1 riastrad
76 1.1 riastrad mutex_enter(&agp_hooks.lock);
77 1.1 riastrad if (agp_hooks.refcnt) {
78 1.1 riastrad KASSERT(agp_hooks.hooks);
79 1.1 riastrad error = EBUSY;
80 1.1 riastrad } else {
81 1.1 riastrad agp_hooks.refcnt++;
82 1.1 riastrad agp_hooks.hooks = hooks;
83 1.1 riastrad }
84 1.1 riastrad mutex_exit(&agp_hooks.lock);
85 1.1 riastrad
86 1.1 riastrad return error;
87 1.1 riastrad }
88 1.1 riastrad
89 1.1 riastrad int
90 1.1 riastrad drm_agp_deregister(const struct drm_agp_hooks *hooks)
91 1.1 riastrad {
92 1.2 tnn int error = 0;
93 1.1 riastrad
94 1.1 riastrad mutex_enter(&agp_hooks.lock);
95 1.1 riastrad KASSERT(agp_hooks.hooks == hooks);
96 1.1 riastrad if (agp_hooks.refcnt > 1) {
97 1.1 riastrad error = EBUSY;
98 1.1 riastrad } else {
99 1.1 riastrad agp_hooks.refcnt = 0;
100 1.1 riastrad agp_hooks.hooks = NULL;
101 1.1 riastrad }
102 1.1 riastrad mutex_exit(&agp_hooks.lock);
103 1.1 riastrad
104 1.1 riastrad return error;
105 1.1 riastrad }
106 1.1 riastrad
107 1.1 riastrad static const struct drm_agp_hooks *
108 1.1 riastrad drm_agp_hooks_acquire(void)
109 1.1 riastrad {
110 1.1 riastrad const struct drm_agp_hooks *hooks;
111 1.1 riastrad
112 1.1 riastrad mutex_enter(&agp_hooks.lock);
113 1.1 riastrad if (agp_hooks.refcnt == 0) {
114 1.1 riastrad hooks = NULL;
115 1.1 riastrad } else {
116 1.1 riastrad KASSERT(agp_hooks.refcnt < UINT_MAX);
117 1.1 riastrad agp_hooks.refcnt++;
118 1.1 riastrad hooks = agp_hooks.hooks;
119 1.1 riastrad }
120 1.1 riastrad mutex_exit(&agp_hooks.lock);
121 1.1 riastrad
122 1.1 riastrad return hooks;
123 1.1 riastrad }
124 1.1 riastrad
125 1.1 riastrad static void
126 1.1 riastrad drm_agp_hooks_release(const struct drm_agp_hooks *hooks)
127 1.1 riastrad {
128 1.1 riastrad
129 1.1 riastrad mutex_enter(&agp_hooks.lock);
130 1.1 riastrad KASSERT(agp_hooks.hooks == hooks);
131 1.1 riastrad KASSERT(agp_hooks.refcnt);
132 1.1 riastrad if (--agp_hooks.refcnt == 0)
133 1.1 riastrad cv_broadcast(&agp_hooks.cv);
134 1.1 riastrad mutex_exit(&agp_hooks.lock);
135 1.1 riastrad }
136 1.1 riastrad
137 1.1 riastrad struct drm_agp_head *
138 1.1 riastrad drm_agp_init(struct drm_device *dev)
139 1.1 riastrad {
140 1.1 riastrad const struct drm_agp_hooks *hooks;
141 1.1 riastrad struct drm_agp_head *agp;
142 1.1 riastrad
143 1.1 riastrad if ((hooks = drm_agp_hooks_acquire()) == NULL)
144 1.1 riastrad return NULL;
145 1.1 riastrad agp = hooks->agph_init(dev);
146 1.1 riastrad if (agp == NULL)
147 1.1 riastrad drm_agp_hooks_release(hooks);
148 1.3 mrg else
149 1.3 mrg agp->hooks = hooks;
150 1.1 riastrad
151 1.1 riastrad return agp;
152 1.1 riastrad }
153 1.1 riastrad
154 1.1 riastrad void
155 1.1 riastrad drm_agp_fini(struct drm_device *dev)
156 1.1 riastrad {
157 1.1 riastrad
158 1.1 riastrad if (dev->agp == NULL)
159 1.1 riastrad return;
160 1.1 riastrad dev->agp->hooks->agph_clear(dev);
161 1.1 riastrad drm_agp_hooks_release(dev->agp->hooks);
162 1.1 riastrad kfree(dev->agp);
163 1.1 riastrad dev->agp = NULL;
164 1.1 riastrad }
165 1.1 riastrad
166 1.1 riastrad void
167 1.1 riastrad drm_agp_clear(struct drm_device *dev)
168 1.1 riastrad {
169 1.1 riastrad
170 1.1 riastrad if (dev->agp == NULL)
171 1.1 riastrad return;
172 1.1 riastrad dev->agp->hooks->agph_clear(dev);
173 1.1 riastrad }
174 1.1 riastrad
175 1.1 riastrad int
176 1.1 riastrad drm_agp_acquire(struct drm_device *dev)
177 1.1 riastrad {
178 1.1 riastrad
179 1.1 riastrad if (dev->agp == NULL)
180 1.1 riastrad return -ENODEV;
181 1.1 riastrad return dev->agp->hooks->agph_acquire(dev);
182 1.1 riastrad }
183 1.1 riastrad
184 1.1 riastrad int
185 1.1 riastrad drm_agp_release(struct drm_device *dev)
186 1.1 riastrad {
187 1.1 riastrad
188 1.1 riastrad if (dev->agp == NULL)
189 1.1 riastrad return -EINVAL;
190 1.1 riastrad return dev->agp->hooks->agph_release(dev);
191 1.1 riastrad }
192 1.1 riastrad
193 1.1 riastrad int
194 1.1 riastrad drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode)
195 1.1 riastrad {
196 1.1 riastrad
197 1.1 riastrad if (dev->agp == NULL)
198 1.1 riastrad return -EINVAL;
199 1.1 riastrad return dev->agp->hooks->agph_enable(dev, mode);
200 1.1 riastrad }
201 1.1 riastrad
202 1.1 riastrad int
203 1.1 riastrad drm_agp_info(struct drm_device *dev, struct drm_agp_info *info)
204 1.1 riastrad {
205 1.1 riastrad
206 1.1 riastrad if (dev->agp == NULL)
207 1.1 riastrad return -EINVAL;
208 1.1 riastrad return dev->agp->hooks->agph_info(dev, info);
209 1.1 riastrad }
210 1.1 riastrad
211 1.1 riastrad int
212 1.1 riastrad drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
213 1.1 riastrad {
214 1.1 riastrad
215 1.1 riastrad if (dev->agp == NULL)
216 1.1 riastrad return -EINVAL;
217 1.1 riastrad return dev->agp->hooks->agph_alloc(dev, request);
218 1.1 riastrad }
219 1.1 riastrad
220 1.1 riastrad int
221 1.1 riastrad drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
222 1.1 riastrad {
223 1.1 riastrad
224 1.1 riastrad if (dev->agp == NULL)
225 1.1 riastrad return -EINVAL;
226 1.1 riastrad return dev->agp->hooks->agph_free(dev, request);
227 1.1 riastrad }
228 1.1 riastrad
229 1.1 riastrad int
230 1.1 riastrad drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
231 1.1 riastrad {
232 1.1 riastrad
233 1.1 riastrad if (dev->agp == NULL)
234 1.1 riastrad return -EINVAL;
235 1.1 riastrad return dev->agp->hooks->agph_bind(dev, request);
236 1.1 riastrad }
237 1.1 riastrad
238 1.1 riastrad int
239 1.1 riastrad drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
240 1.1 riastrad {
241 1.1 riastrad
242 1.1 riastrad if (dev->agp == NULL)
243 1.1 riastrad return -EINVAL;
244 1.1 riastrad return dev->agp->hooks->agph_unbind(dev, request);
245 1.1 riastrad }
246 1.1 riastrad
247 1.1 riastrad #define DEFINE_AGP_HOOK_IOCTL(NAME, FIELD) \
248 1.1 riastrad int \
249 1.1 riastrad NAME(struct drm_device *dev, void *data, struct drm_file *file) \
250 1.1 riastrad { \
251 1.1 riastrad \
252 1.1 riastrad if (dev->agp == NULL) \
253 1.1 riastrad return -ENODEV; \
254 1.1 riastrad return dev->agp->hooks->FIELD(dev, data, file); \
255 1.1 riastrad }
256 1.1 riastrad
257 1.1 riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_acquire_ioctl, agph_acquire_ioctl)
258 1.1 riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_release_ioctl, agph_release_ioctl)
259 1.1 riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_enable_ioctl, agph_enable_ioctl)
260 1.1 riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_info_ioctl, agph_info_ioctl)
261 1.1 riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_alloc_ioctl, agph_alloc_ioctl)
262 1.1 riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_free_ioctl, agph_free_ioctl)
263 1.1 riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_bind_ioctl, agph_bind_ioctl)
264 1.1 riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_unbind_ioctl, agph_unbind_ioctl)
265 1.1 riastrad
266 1.1 riastrad void __pci_iomem *
267 1.1 riastrad drm_agp_borrow(struct drm_device *dev, unsigned bar, bus_size_t size)
268 1.1 riastrad {
269 1.1 riastrad const struct drm_agp_hooks *hooks;
270 1.1 riastrad void __pci_iomem *iomem;
271 1.1 riastrad
272 1.1 riastrad if ((hooks = drm_agp_hooks_acquire()) == NULL)
273 1.1 riastrad return NULL;
274 1.1 riastrad iomem = hooks->agph_borrow(dev, bar, size);
275 1.1 riastrad drm_agp_hooks_release(hooks);
276 1.1 riastrad
277 1.1 riastrad return iomem;
278 1.1 riastrad }
279 1.1 riastrad
280 1.1 riastrad void
281 1.1 riastrad drm_agp_flush(void)
282 1.1 riastrad {
283 1.1 riastrad const struct drm_agp_hooks *hooks;
284 1.1 riastrad
285 1.1 riastrad if ((hooks = drm_agp_hooks_acquire()) == NULL)
286 1.1 riastrad return;
287 1.1 riastrad hooks->agph_flush();
288 1.1 riastrad drm_agp_hooks_release(hooks);
289 1.1 riastrad }
290