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