agp.c revision 1.10.2.5 1 1.10.2.5 nathanw /* $NetBSD: agp.c,v 1.10.2.5 2002/02/28 04:13:56 nathanw Exp $ */
2 1.10.2.2 nathanw
3 1.10.2.2 nathanw /*-
4 1.10.2.2 nathanw * Copyright (c) 2000 Doug Rabson
5 1.10.2.2 nathanw * All rights reserved.
6 1.10.2.2 nathanw *
7 1.10.2.2 nathanw * Redistribution and use in source and binary forms, with or without
8 1.10.2.2 nathanw * modification, are permitted provided that the following conditions
9 1.10.2.2 nathanw * are met:
10 1.10.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
11 1.10.2.2 nathanw * notice, this list of conditions and the following disclaimer.
12 1.10.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
13 1.10.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
14 1.10.2.2 nathanw * documentation and/or other materials provided with the distribution.
15 1.10.2.2 nathanw *
16 1.10.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.10.2.2 nathanw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.10.2.2 nathanw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.10.2.2 nathanw * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.10.2.2 nathanw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.10.2.2 nathanw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.10.2.2 nathanw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.10.2.2 nathanw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.10.2.2 nathanw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.10.2.2 nathanw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.10.2.2 nathanw * SUCH DAMAGE.
27 1.10.2.2 nathanw *
28 1.10.2.2 nathanw * $FreeBSD: src/sys/pci/agp.c,v 1.12 2001/05/19 01:28:07 alfred Exp $
29 1.10.2.2 nathanw */
30 1.10.2.2 nathanw
31 1.10.2.2 nathanw /*
32 1.10.2.2 nathanw * Copyright (c) 2001 Wasabi Systems, Inc.
33 1.10.2.2 nathanw * All rights reserved.
34 1.10.2.2 nathanw *
35 1.10.2.2 nathanw * Written by Frank van der Linden for Wasabi Systems, Inc.
36 1.10.2.2 nathanw *
37 1.10.2.2 nathanw * Redistribution and use in source and binary forms, with or without
38 1.10.2.2 nathanw * modification, are permitted provided that the following conditions
39 1.10.2.2 nathanw * are met:
40 1.10.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
41 1.10.2.2 nathanw * notice, this list of conditions and the following disclaimer.
42 1.10.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
43 1.10.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
44 1.10.2.2 nathanw * documentation and/or other materials provided with the distribution.
45 1.10.2.2 nathanw * 3. All advertising materials mentioning features or use of this software
46 1.10.2.2 nathanw * must display the following acknowledgement:
47 1.10.2.2 nathanw * This product includes software developed for the NetBSD Project by
48 1.10.2.2 nathanw * Wasabi Systems, Inc.
49 1.10.2.2 nathanw * 4. The name of Wasabi Systems, Inc. may not be used to endorse
50 1.10.2.2 nathanw * or promote products derived from this software without specific prior
51 1.10.2.2 nathanw * written permission.
52 1.10.2.2 nathanw *
53 1.10.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
54 1.10.2.2 nathanw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 1.10.2.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56 1.10.2.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
57 1.10.2.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58 1.10.2.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59 1.10.2.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60 1.10.2.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61 1.10.2.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62 1.10.2.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63 1.10.2.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
64 1.10.2.2 nathanw */
65 1.10.2.2 nathanw
66 1.10.2.2 nathanw
67 1.10.2.4 nathanw #include <sys/cdefs.h>
68 1.10.2.5 nathanw __KERNEL_RCSID(0, "$NetBSD: agp.c,v 1.10.2.5 2002/02/28 04:13:56 nathanw Exp $");
69 1.10.2.4 nathanw
70 1.10.2.2 nathanw #include <sys/param.h>
71 1.10.2.2 nathanw #include <sys/systm.h>
72 1.10.2.2 nathanw #include <sys/malloc.h>
73 1.10.2.2 nathanw #include <sys/kernel.h>
74 1.10.2.2 nathanw #include <sys/device.h>
75 1.10.2.2 nathanw #include <sys/conf.h>
76 1.10.2.2 nathanw #include <sys/ioctl.h>
77 1.10.2.2 nathanw #include <sys/fcntl.h>
78 1.10.2.2 nathanw #include <sys/agpio.h>
79 1.10.2.2 nathanw #include <sys/proc.h>
80 1.10.2.2 nathanw
81 1.10.2.2 nathanw #include <uvm/uvm_extern.h>
82 1.10.2.2 nathanw
83 1.10.2.2 nathanw #include <dev/pci/pcireg.h>
84 1.10.2.2 nathanw #include <dev/pci/pcivar.h>
85 1.10.2.2 nathanw #include <dev/pci/agpvar.h>
86 1.10.2.2 nathanw #include <dev/pci/agpreg.h>
87 1.10.2.2 nathanw #include <dev/pci/pcidevs.h>
88 1.10.2.2 nathanw
89 1.10.2.2 nathanw #include <machine/bus.h>
90 1.10.2.2 nathanw
91 1.10.2.2 nathanw /* Helper functions for implementing chipset mini drivers. */
92 1.10.2.2 nathanw /* XXXfvdl get rid of this one. */
93 1.10.2.2 nathanw
94 1.10.2.2 nathanw extern struct cfdriver agp_cd;
95 1.10.2.2 nathanw cdev_decl(agp);
96 1.10.2.2 nathanw
97 1.10.2.2 nathanw int agpmatch(struct device *, struct cfdata *, void *);
98 1.10.2.2 nathanw void agpattach(struct device *, struct device *, void *);
99 1.10.2.2 nathanw
100 1.10.2.2 nathanw struct cfattach agp_ca = {
101 1.10.2.2 nathanw sizeof(struct agp_softc), agpmatch, agpattach
102 1.10.2.2 nathanw };
103 1.10.2.2 nathanw
104 1.10.2.2 nathanw static int agp_info_user(struct agp_softc *, agp_info *);
105 1.10.2.2 nathanw static int agp_setup_user(struct agp_softc *, agp_setup *);
106 1.10.2.2 nathanw static int agp_allocate_user(struct agp_softc *, agp_allocate *);
107 1.10.2.2 nathanw static int agp_deallocate_user(struct agp_softc *, int);
108 1.10.2.2 nathanw static int agp_bind_user(struct agp_softc *, agp_bind *);
109 1.10.2.2 nathanw static int agp_unbind_user(struct agp_softc *, agp_unbind *);
110 1.10.2.2 nathanw static int agpdev_match(struct pci_attach_args *);
111 1.10.2.2 nathanw
112 1.10.2.2 nathanw #include "agp_ali.h"
113 1.10.2.2 nathanw #include "agp_amd.h"
114 1.10.2.2 nathanw #include "agp_i810.h"
115 1.10.2.2 nathanw #include "agp_intel.h"
116 1.10.2.2 nathanw #include "agp_sis.h"
117 1.10.2.2 nathanw #include "agp_via.h"
118 1.10.2.2 nathanw
119 1.10.2.2 nathanw const struct agp_product {
120 1.10.2.2 nathanw uint32_t ap_vendor;
121 1.10.2.2 nathanw uint32_t ap_product;
122 1.10.2.2 nathanw int (*ap_match)(const struct pci_attach_args *);
123 1.10.2.2 nathanw int (*ap_attach)(struct device *, struct device *, void *);
124 1.10.2.2 nathanw } agp_products[] = {
125 1.10.2.2 nathanw #if NAGP_ALI > 0
126 1.10.2.2 nathanw { PCI_VENDOR_ALI, -1,
127 1.10.2.2 nathanw NULL, agp_ali_attach },
128 1.10.2.2 nathanw #endif
129 1.10.2.2 nathanw
130 1.10.2.2 nathanw #if NAGP_AMD > 0
131 1.10.2.2 nathanw { PCI_VENDOR_AMD, -1,
132 1.10.2.2 nathanw agp_amd_match, agp_amd_attach },
133 1.10.2.2 nathanw #endif
134 1.10.2.2 nathanw
135 1.10.2.2 nathanw #if NAGP_I810 > 0
136 1.10.2.2 nathanw { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82810_MCH,
137 1.10.2.2 nathanw NULL, agp_i810_attach },
138 1.10.2.2 nathanw { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82810_DC100_MCH,
139 1.10.2.2 nathanw NULL, agp_i810_attach },
140 1.10.2.2 nathanw { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82810E_MCH,
141 1.10.2.2 nathanw NULL, agp_i810_attach },
142 1.10.2.2 nathanw { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82815_FULL_HUB,
143 1.10.2.2 nathanw NULL, agp_i810_attach },
144 1.10.2.3 nathanw { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82840_HB,
145 1.10.2.3 nathanw NULL, agp_i810_attach },
146 1.10.2.5 nathanw #if 0
147 1.10.2.5 nathanw /* XXX needs somewhat different driver */
148 1.10.2.5 nathanw { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82830MP_IO_1,
149 1.10.2.5 nathanw NULL, agp_i810_attach },
150 1.10.2.5 nathanw #endif
151 1.10.2.2 nathanw #endif
152 1.10.2.2 nathanw
153 1.10.2.2 nathanw #if NAGP_INTEL > 0
154 1.10.2.2 nathanw { PCI_VENDOR_INTEL, -1,
155 1.10.2.2 nathanw NULL, agp_intel_attach },
156 1.10.2.2 nathanw #endif
157 1.10.2.2 nathanw
158 1.10.2.2 nathanw #if NAGP_SIS > 0
159 1.10.2.2 nathanw { PCI_VENDOR_SIS, -1,
160 1.10.2.2 nathanw NULL, agp_sis_attach },
161 1.10.2.2 nathanw #endif
162 1.10.2.2 nathanw
163 1.10.2.2 nathanw #if NAGP_VIA > 0
164 1.10.2.2 nathanw { PCI_VENDOR_VIATECH, -1,
165 1.10.2.2 nathanw NULL, agp_via_attach },
166 1.10.2.2 nathanw #endif
167 1.10.2.2 nathanw
168 1.10.2.2 nathanw { 0, 0,
169 1.10.2.2 nathanw NULL, NULL },
170 1.10.2.2 nathanw };
171 1.10.2.2 nathanw
172 1.10.2.2 nathanw static const struct agp_product *
173 1.10.2.2 nathanw agp_lookup(const struct pci_attach_args *pa)
174 1.10.2.2 nathanw {
175 1.10.2.2 nathanw const struct agp_product *ap;
176 1.10.2.2 nathanw
177 1.10.2.2 nathanw /* First find the vendor. */
178 1.10.2.2 nathanw for (ap = agp_products; ap->ap_attach != NULL; ap++) {
179 1.10.2.2 nathanw if (PCI_VENDOR(pa->pa_id) == ap->ap_vendor)
180 1.10.2.2 nathanw break;
181 1.10.2.2 nathanw }
182 1.10.2.2 nathanw
183 1.10.2.2 nathanw if (ap->ap_attach == NULL)
184 1.10.2.2 nathanw return (NULL);
185 1.10.2.2 nathanw
186 1.10.2.2 nathanw /* Now find the product within the vendor's domain. */
187 1.10.2.2 nathanw for (; ap->ap_attach != NULL; ap++) {
188 1.10.2.2 nathanw if (PCI_VENDOR(pa->pa_id) != ap->ap_vendor) {
189 1.10.2.2 nathanw /* Ran out of this vendor's section of the table. */
190 1.10.2.2 nathanw return (NULL);
191 1.10.2.2 nathanw }
192 1.10.2.2 nathanw if (ap->ap_product == PCI_PRODUCT(pa->pa_id)) {
193 1.10.2.2 nathanw /* Exact match. */
194 1.10.2.2 nathanw break;
195 1.10.2.2 nathanw }
196 1.10.2.2 nathanw if (ap->ap_product == (uint32_t) -1) {
197 1.10.2.2 nathanw /* Wildcard match. */
198 1.10.2.2 nathanw break;
199 1.10.2.2 nathanw }
200 1.10.2.2 nathanw }
201 1.10.2.2 nathanw
202 1.10.2.2 nathanw if (ap->ap_attach == NULL)
203 1.10.2.2 nathanw return (NULL);
204 1.10.2.2 nathanw
205 1.10.2.2 nathanw /* Now let the product-specific driver filter the match. */
206 1.10.2.2 nathanw if (ap->ap_match != NULL && (*ap->ap_match)(pa) == 0)
207 1.10.2.2 nathanw return (NULL);
208 1.10.2.2 nathanw
209 1.10.2.2 nathanw return (ap);
210 1.10.2.2 nathanw }
211 1.10.2.2 nathanw
212 1.10.2.2 nathanw int
213 1.10.2.2 nathanw agpmatch(struct device *parent, struct cfdata *match, void *aux)
214 1.10.2.2 nathanw {
215 1.10.2.2 nathanw struct agpbus_attach_args *apa = aux;
216 1.10.2.2 nathanw struct pci_attach_args *pa = &apa->apa_pci_args;
217 1.10.2.2 nathanw
218 1.10.2.2 nathanw if (strcmp(apa->apa_busname, "agp") != 0)
219 1.10.2.2 nathanw return (0);
220 1.10.2.2 nathanw
221 1.10.2.2 nathanw if (agp_lookup(pa) == NULL)
222 1.10.2.2 nathanw return (0);
223 1.10.2.2 nathanw
224 1.10.2.2 nathanw return (1);
225 1.10.2.2 nathanw }
226 1.10.2.2 nathanw
227 1.10.2.2 nathanw static int agp_max[][2] = {
228 1.10.2.2 nathanw {0, 0},
229 1.10.2.2 nathanw {32, 4},
230 1.10.2.2 nathanw {64, 28},
231 1.10.2.2 nathanw {128, 96},
232 1.10.2.2 nathanw {256, 204},
233 1.10.2.2 nathanw {512, 440},
234 1.10.2.2 nathanw {1024, 942},
235 1.10.2.2 nathanw {2048, 1920},
236 1.10.2.2 nathanw {4096, 3932}
237 1.10.2.2 nathanw };
238 1.10.2.2 nathanw #define agp_max_size (sizeof(agp_max) / sizeof(agp_max[0]))
239 1.10.2.2 nathanw
240 1.10.2.2 nathanw void
241 1.10.2.2 nathanw agpattach(struct device *parent, struct device *self, void *aux)
242 1.10.2.2 nathanw {
243 1.10.2.2 nathanw struct agpbus_attach_args *apa = aux;
244 1.10.2.2 nathanw struct pci_attach_args *pa = &apa->apa_pci_args;
245 1.10.2.2 nathanw struct agp_softc *sc = (void *)self;
246 1.10.2.2 nathanw const struct agp_product *ap;
247 1.10.2.2 nathanw int memsize, i, ret;
248 1.10.2.2 nathanw
249 1.10.2.2 nathanw ap = agp_lookup(pa);
250 1.10.2.2 nathanw if (ap == NULL) {
251 1.10.2.2 nathanw printf("\n");
252 1.10.2.2 nathanw panic("agpattach: impossible");
253 1.10.2.2 nathanw }
254 1.10.2.2 nathanw
255 1.10.2.2 nathanw sc->as_dmat = pa->pa_dmat;
256 1.10.2.2 nathanw sc->as_pc = pa->pa_pc;
257 1.10.2.2 nathanw sc->as_tag = pa->pa_tag;
258 1.10.2.2 nathanw sc->as_id = pa->pa_id;
259 1.10.2.2 nathanw
260 1.10.2.2 nathanw /*
261 1.10.2.2 nathanw * Work out an upper bound for agp memory allocation. This
262 1.10.2.2 nathanw * uses a heurisitc table from the Linux driver.
263 1.10.2.2 nathanw */
264 1.10.2.2 nathanw memsize = ptoa(physmem) >> 20;
265 1.10.2.2 nathanw for (i = 0; i < agp_max_size; i++) {
266 1.10.2.2 nathanw if (memsize <= agp_max[i][0])
267 1.10.2.2 nathanw break;
268 1.10.2.2 nathanw }
269 1.10.2.2 nathanw if (i == agp_max_size)
270 1.10.2.2 nathanw i = agp_max_size - 1;
271 1.10.2.2 nathanw sc->as_maxmem = agp_max[i][1] << 20U;
272 1.10.2.2 nathanw
273 1.10.2.2 nathanw /*
274 1.10.2.2 nathanw * The lock is used to prevent re-entry to
275 1.10.2.2 nathanw * agp_generic_bind_memory() since that function can sleep.
276 1.10.2.2 nathanw */
277 1.10.2.2 nathanw lockinit(&sc->as_lock, PZERO|PCATCH, "agplk", 0, 0);
278 1.10.2.2 nathanw
279 1.10.2.2 nathanw TAILQ_INIT(&sc->as_memory);
280 1.10.2.2 nathanw
281 1.10.2.2 nathanw ret = (*ap->ap_attach)(parent, self, pa);
282 1.10.2.2 nathanw if (ret == 0)
283 1.10.2.2 nathanw printf(": aperture at 0x%lx, size 0x%lx\n",
284 1.10.2.2 nathanw (unsigned long)sc->as_apaddr,
285 1.10.2.2 nathanw (unsigned long)AGP_GET_APERTURE(sc));
286 1.10.2.2 nathanw else
287 1.10.2.2 nathanw sc->as_chipc = NULL;
288 1.10.2.2 nathanw }
289 1.10.2.2 nathanw int
290 1.10.2.2 nathanw agp_map_aperture(struct pci_attach_args *pa, struct agp_softc *sc)
291 1.10.2.2 nathanw {
292 1.10.2.2 nathanw /*
293 1.10.2.3 nathanw * Find and the aperture. Don't map it (yet), this would
294 1.10.2.3 nathanw * eat KVA.
295 1.10.2.2 nathanw */
296 1.10.2.3 nathanw if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, AGP_APBASE,
297 1.10.2.3 nathanw PCI_MAPREG_TYPE_MEM, &sc->as_apaddr, &sc->as_apsize,
298 1.10.2.3 nathanw &sc->as_apflags) != 0)
299 1.10.2.2 nathanw return ENXIO;
300 1.10.2.2 nathanw
301 1.10.2.3 nathanw sc->as_apt = pa->pa_memt;
302 1.10.2.3 nathanw
303 1.10.2.2 nathanw return 0;
304 1.10.2.2 nathanw }
305 1.10.2.2 nathanw
306 1.10.2.2 nathanw struct agp_gatt *
307 1.10.2.2 nathanw agp_alloc_gatt(struct agp_softc *sc)
308 1.10.2.2 nathanw {
309 1.10.2.2 nathanw u_int32_t apsize = AGP_GET_APERTURE(sc);
310 1.10.2.2 nathanw u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
311 1.10.2.2 nathanw struct agp_gatt *gatt;
312 1.10.2.2 nathanw int dummyseg;
313 1.10.2.2 nathanw
314 1.10.2.2 nathanw gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
315 1.10.2.2 nathanw if (!gatt)
316 1.10.2.2 nathanw return NULL;
317 1.10.2.2 nathanw gatt->ag_entries = entries;
318 1.10.2.2 nathanw
319 1.10.2.2 nathanw if (agp_alloc_dmamem(sc->as_dmat, entries * sizeof(u_int32_t),
320 1.10.2.2 nathanw 0, &gatt->ag_dmamap, (caddr_t *)&gatt->ag_virtual,
321 1.10.2.2 nathanw &gatt->ag_physical, &gatt->ag_dmaseg, 1, &dummyseg) != 0)
322 1.10.2.2 nathanw return NULL;
323 1.10.2.2 nathanw
324 1.10.2.2 nathanw gatt->ag_size = entries * sizeof(u_int32_t);
325 1.10.2.2 nathanw memset(gatt->ag_virtual, 0, gatt->ag_size);
326 1.10.2.2 nathanw agp_flush_cache();
327 1.10.2.2 nathanw
328 1.10.2.2 nathanw return gatt;
329 1.10.2.2 nathanw }
330 1.10.2.2 nathanw
331 1.10.2.2 nathanw void
332 1.10.2.2 nathanw agp_free_gatt(struct agp_softc *sc, struct agp_gatt *gatt)
333 1.10.2.2 nathanw {
334 1.10.2.2 nathanw agp_free_dmamem(sc->as_dmat, gatt->ag_size, gatt->ag_dmamap,
335 1.10.2.2 nathanw (caddr_t)gatt->ag_virtual, &gatt->ag_dmaseg, 1);
336 1.10.2.2 nathanw free(gatt, M_AGP);
337 1.10.2.2 nathanw }
338 1.10.2.2 nathanw
339 1.10.2.2 nathanw
340 1.10.2.2 nathanw int
341 1.10.2.2 nathanw agp_generic_detach(struct agp_softc *sc)
342 1.10.2.2 nathanw {
343 1.10.2.2 nathanw lockmgr(&sc->as_lock, LK_DRAIN, 0);
344 1.10.2.2 nathanw agp_flush_cache();
345 1.10.2.2 nathanw return 0;
346 1.10.2.2 nathanw }
347 1.10.2.2 nathanw
348 1.10.2.2 nathanw static int
349 1.10.2.2 nathanw agpdev_match(struct pci_attach_args *pa)
350 1.10.2.2 nathanw {
351 1.10.2.2 nathanw if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY &&
352 1.10.2.2 nathanw PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_DISPLAY_VGA)
353 1.10.2.2 nathanw return 1;
354 1.10.2.2 nathanw
355 1.10.2.2 nathanw return 0;
356 1.10.2.2 nathanw }
357 1.10.2.2 nathanw
358 1.10.2.2 nathanw int
359 1.10.2.2 nathanw agp_generic_enable(struct agp_softc *sc, u_int32_t mode)
360 1.10.2.2 nathanw {
361 1.10.2.2 nathanw struct pci_attach_args pa;
362 1.10.2.2 nathanw pcireg_t tstatus, mstatus;
363 1.10.2.2 nathanw pcireg_t command;
364 1.10.2.2 nathanw int rq, sba, fw, rate, capoff;
365 1.10.2.2 nathanw
366 1.10.2.2 nathanw if (pci_find_device(&pa, agpdev_match) == 0 ||
367 1.10.2.2 nathanw pci_get_capability(pa.pa_pc, pa.pa_tag, PCI_CAP_AGP,
368 1.10.2.2 nathanw &capoff, NULL) == 0) {
369 1.10.2.2 nathanw printf("%s: can't find display\n", sc->as_dev.dv_xname);
370 1.10.2.2 nathanw return ENXIO;
371 1.10.2.2 nathanw }
372 1.10.2.2 nathanw
373 1.10.2.2 nathanw tstatus = pci_conf_read(sc->as_pc, sc->as_tag,
374 1.10.2.2 nathanw sc->as_capoff + AGP_STATUS);
375 1.10.2.2 nathanw mstatus = pci_conf_read(pa.pa_pc, pa.pa_tag,
376 1.10.2.2 nathanw capoff + AGP_STATUS);
377 1.10.2.2 nathanw
378 1.10.2.2 nathanw /* Set RQ to the min of mode, tstatus and mstatus */
379 1.10.2.2 nathanw rq = AGP_MODE_GET_RQ(mode);
380 1.10.2.2 nathanw if (AGP_MODE_GET_RQ(tstatus) < rq)
381 1.10.2.2 nathanw rq = AGP_MODE_GET_RQ(tstatus);
382 1.10.2.2 nathanw if (AGP_MODE_GET_RQ(mstatus) < rq)
383 1.10.2.2 nathanw rq = AGP_MODE_GET_RQ(mstatus);
384 1.10.2.2 nathanw
385 1.10.2.2 nathanw /* Set SBA if all three can deal with SBA */
386 1.10.2.2 nathanw sba = (AGP_MODE_GET_SBA(tstatus)
387 1.10.2.2 nathanw & AGP_MODE_GET_SBA(mstatus)
388 1.10.2.2 nathanw & AGP_MODE_GET_SBA(mode));
389 1.10.2.2 nathanw
390 1.10.2.2 nathanw /* Similar for FW */
391 1.10.2.2 nathanw fw = (AGP_MODE_GET_FW(tstatus)
392 1.10.2.2 nathanw & AGP_MODE_GET_FW(mstatus)
393 1.10.2.2 nathanw & AGP_MODE_GET_FW(mode));
394 1.10.2.2 nathanw
395 1.10.2.2 nathanw /* Figure out the max rate */
396 1.10.2.2 nathanw rate = (AGP_MODE_GET_RATE(tstatus)
397 1.10.2.2 nathanw & AGP_MODE_GET_RATE(mstatus)
398 1.10.2.2 nathanw & AGP_MODE_GET_RATE(mode));
399 1.10.2.2 nathanw if (rate & AGP_MODE_RATE_4x)
400 1.10.2.2 nathanw rate = AGP_MODE_RATE_4x;
401 1.10.2.2 nathanw else if (rate & AGP_MODE_RATE_2x)
402 1.10.2.2 nathanw rate = AGP_MODE_RATE_2x;
403 1.10.2.2 nathanw else
404 1.10.2.2 nathanw rate = AGP_MODE_RATE_1x;
405 1.10.2.2 nathanw
406 1.10.2.2 nathanw /* Construct the new mode word and tell the hardware */
407 1.10.2.2 nathanw command = AGP_MODE_SET_RQ(0, rq);
408 1.10.2.2 nathanw command = AGP_MODE_SET_SBA(command, sba);
409 1.10.2.2 nathanw command = AGP_MODE_SET_FW(command, fw);
410 1.10.2.2 nathanw command = AGP_MODE_SET_RATE(command, rate);
411 1.10.2.2 nathanw command = AGP_MODE_SET_AGP(command, 1);
412 1.10.2.2 nathanw pci_conf_write(sc->as_pc, sc->as_tag,
413 1.10.2.2 nathanw sc->as_capoff + AGP_COMMAND, command);
414 1.10.2.2 nathanw pci_conf_write(pa.pa_pc, pa.pa_tag, capoff + AGP_COMMAND, command);
415 1.10.2.2 nathanw
416 1.10.2.2 nathanw return 0;
417 1.10.2.2 nathanw }
418 1.10.2.2 nathanw
419 1.10.2.2 nathanw struct agp_memory *
420 1.10.2.2 nathanw agp_generic_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
421 1.10.2.2 nathanw {
422 1.10.2.2 nathanw struct agp_memory *mem;
423 1.10.2.2 nathanw
424 1.10.2.2 nathanw if ((size & (AGP_PAGE_SIZE - 1)) != 0)
425 1.10.2.2 nathanw return 0;
426 1.10.2.2 nathanw
427 1.10.2.2 nathanw if (sc->as_allocated + size > sc->as_maxmem)
428 1.10.2.2 nathanw return 0;
429 1.10.2.2 nathanw
430 1.10.2.2 nathanw if (type != 0) {
431 1.10.2.2 nathanw printf("agp_generic_alloc_memory: unsupported type %d\n",
432 1.10.2.2 nathanw type);
433 1.10.2.2 nathanw return 0;
434 1.10.2.2 nathanw }
435 1.10.2.2 nathanw
436 1.10.2.2 nathanw mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
437 1.10.2.2 nathanw if (mem == NULL)
438 1.10.2.2 nathanw return NULL;
439 1.10.2.2 nathanw
440 1.10.2.2 nathanw if (bus_dmamap_create(sc->as_dmat, size, size / PAGE_SIZE + 1,
441 1.10.2.2 nathanw size, 0, BUS_DMA_NOWAIT, &mem->am_dmamap) != 0) {
442 1.10.2.2 nathanw free(mem, M_AGP);
443 1.10.2.2 nathanw return NULL;
444 1.10.2.2 nathanw }
445 1.10.2.2 nathanw
446 1.10.2.2 nathanw mem->am_id = sc->as_nextid++;
447 1.10.2.2 nathanw mem->am_size = size;
448 1.10.2.2 nathanw mem->am_type = 0;
449 1.10.2.2 nathanw mem->am_physical = 0;
450 1.10.2.2 nathanw mem->am_offset = 0;
451 1.10.2.2 nathanw mem->am_is_bound = 0;
452 1.10.2.2 nathanw TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
453 1.10.2.2 nathanw sc->as_allocated += size;
454 1.10.2.2 nathanw
455 1.10.2.2 nathanw return mem;
456 1.10.2.2 nathanw }
457 1.10.2.2 nathanw
458 1.10.2.2 nathanw int
459 1.10.2.2 nathanw agp_generic_free_memory(struct agp_softc *sc, struct agp_memory *mem)
460 1.10.2.2 nathanw {
461 1.10.2.2 nathanw if (mem->am_is_bound)
462 1.10.2.2 nathanw return EBUSY;
463 1.10.2.2 nathanw
464 1.10.2.2 nathanw sc->as_allocated -= mem->am_size;
465 1.10.2.2 nathanw TAILQ_REMOVE(&sc->as_memory, mem, am_link);
466 1.10.2.2 nathanw bus_dmamap_destroy(sc->as_dmat, mem->am_dmamap);
467 1.10.2.2 nathanw free(mem, M_AGP);
468 1.10.2.2 nathanw return 0;
469 1.10.2.2 nathanw }
470 1.10.2.2 nathanw
471 1.10.2.2 nathanw int
472 1.10.2.2 nathanw agp_generic_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
473 1.10.2.2 nathanw off_t offset)
474 1.10.2.2 nathanw {
475 1.10.2.2 nathanw off_t i, k;
476 1.10.2.2 nathanw bus_size_t done, j;
477 1.10.2.2 nathanw int error;
478 1.10.2.2 nathanw bus_dma_segment_t *segs, *seg;
479 1.10.2.2 nathanw bus_addr_t pa;
480 1.10.2.2 nathanw int contigpages, nseg;
481 1.10.2.2 nathanw
482 1.10.2.2 nathanw lockmgr(&sc->as_lock, LK_EXCLUSIVE, 0);
483 1.10.2.2 nathanw
484 1.10.2.2 nathanw if (mem->am_is_bound) {
485 1.10.2.2 nathanw printf("%s: memory already bound\n", sc->as_dev.dv_xname);
486 1.10.2.2 nathanw lockmgr(&sc->as_lock, LK_RELEASE, 0);
487 1.10.2.2 nathanw return EINVAL;
488 1.10.2.2 nathanw }
489 1.10.2.2 nathanw
490 1.10.2.2 nathanw if (offset < 0
491 1.10.2.2 nathanw || (offset & (AGP_PAGE_SIZE - 1)) != 0
492 1.10.2.2 nathanw || offset + mem->am_size > AGP_GET_APERTURE(sc)) {
493 1.10.2.2 nathanw printf("%s: binding memory at bad offset %#lx\n",
494 1.10.2.2 nathanw sc->as_dev.dv_xname, (unsigned long) offset);
495 1.10.2.2 nathanw lockmgr(&sc->as_lock, LK_RELEASE, 0);
496 1.10.2.2 nathanw return EINVAL;
497 1.10.2.2 nathanw }
498 1.10.2.2 nathanw
499 1.10.2.2 nathanw /*
500 1.10.2.2 nathanw * XXXfvdl
501 1.10.2.2 nathanw * The memory here needs to be directly accessable from the
502 1.10.2.2 nathanw * AGP video card, so it should be allocated using bus_dma.
503 1.10.2.2 nathanw * However, it need not be contiguous, since individual pages
504 1.10.2.2 nathanw * are translated using the GATT.
505 1.10.2.2 nathanw *
506 1.10.2.2 nathanw * Using a large chunk of contiguous memory may get in the way
507 1.10.2.2 nathanw * of other subsystems that may need one, so we try to be friendly
508 1.10.2.2 nathanw * and ask for allocation in chunks of a minimum of 8 pages
509 1.10.2.2 nathanw * of contiguous memory on average, falling back to 4, 2 and 1
510 1.10.2.2 nathanw * if really needed. Larger chunks are preferred, since allocating
511 1.10.2.2 nathanw * a bus_dma_segment per page would be overkill.
512 1.10.2.2 nathanw */
513 1.10.2.2 nathanw
514 1.10.2.2 nathanw for (contigpages = 8; contigpages > 0; contigpages >>= 1) {
515 1.10.2.2 nathanw nseg = (mem->am_size / (contigpages * PAGE_SIZE)) + 1;
516 1.10.2.2 nathanw segs = malloc(nseg * sizeof *segs, M_AGP, M_WAITOK);
517 1.10.2.2 nathanw if (segs == NULL)
518 1.10.2.2 nathanw return ENOMEM;
519 1.10.2.2 nathanw if (bus_dmamem_alloc(sc->as_dmat, mem->am_size, PAGE_SIZE, 0,
520 1.10.2.2 nathanw segs, nseg, &mem->am_nseg,
521 1.10.2.2 nathanw BUS_DMA_WAITOK) != 0) {
522 1.10.2.2 nathanw free(segs, M_AGP);
523 1.10.2.2 nathanw continue;
524 1.10.2.2 nathanw }
525 1.10.2.2 nathanw if (bus_dmamem_map(sc->as_dmat, segs, mem->am_nseg,
526 1.10.2.2 nathanw mem->am_size, &mem->am_virtual, BUS_DMA_WAITOK) != 0) {
527 1.10.2.2 nathanw bus_dmamem_free(sc->as_dmat, segs, mem->am_nseg);
528 1.10.2.2 nathanw free(segs, M_AGP);
529 1.10.2.2 nathanw continue;
530 1.10.2.2 nathanw }
531 1.10.2.2 nathanw if (bus_dmamap_load(sc->as_dmat, mem->am_dmamap,
532 1.10.2.2 nathanw mem->am_virtual, mem->am_size, NULL, BUS_DMA_WAITOK) != 0) {
533 1.10.2.2 nathanw bus_dmamem_unmap(sc->as_dmat, mem->am_virtual,
534 1.10.2.2 nathanw mem->am_size);
535 1.10.2.2 nathanw bus_dmamem_free(sc->as_dmat, segs, mem->am_nseg);
536 1.10.2.2 nathanw free(segs, M_AGP);
537 1.10.2.2 nathanw continue;
538 1.10.2.2 nathanw }
539 1.10.2.2 nathanw mem->am_dmaseg = segs;
540 1.10.2.2 nathanw break;
541 1.10.2.2 nathanw }
542 1.10.2.2 nathanw
543 1.10.2.2 nathanw if (contigpages == 0) {
544 1.10.2.2 nathanw lockmgr(&sc->as_lock, LK_RELEASE, 0);
545 1.10.2.2 nathanw return ENOMEM;
546 1.10.2.2 nathanw }
547 1.10.2.2 nathanw
548 1.10.2.2 nathanw
549 1.10.2.2 nathanw /*
550 1.10.2.2 nathanw * Bind the individual pages and flush the chipset's
551 1.10.2.2 nathanw * TLB.
552 1.10.2.2 nathanw */
553 1.10.2.2 nathanw done = 0;
554 1.10.2.2 nathanw for (i = 0; i < mem->am_dmamap->dm_nsegs; i++) {
555 1.10.2.2 nathanw seg = &mem->am_dmamap->dm_segs[i];
556 1.10.2.2 nathanw /*
557 1.10.2.2 nathanw * Install entries in the GATT, making sure that if
558 1.10.2.2 nathanw * AGP_PAGE_SIZE < PAGE_SIZE and mem->am_size is not
559 1.10.2.2 nathanw * aligned to PAGE_SIZE, we don't modify too many GATT
560 1.10.2.2 nathanw * entries.
561 1.10.2.2 nathanw */
562 1.10.2.2 nathanw for (j = 0; j < seg->ds_len && (done + j) < mem->am_size;
563 1.10.2.2 nathanw j += AGP_PAGE_SIZE) {
564 1.10.2.2 nathanw pa = seg->ds_addr + j;
565 1.10.2.2 nathanw AGP_DPF("binding offset %#lx to pa %#lx\n",
566 1.10.2.2 nathanw (unsigned long)(offset + done + j),
567 1.10.2.2 nathanw (unsigned long)pa);
568 1.10.2.2 nathanw error = AGP_BIND_PAGE(sc, offset + done + j, pa);
569 1.10.2.2 nathanw if (error) {
570 1.10.2.2 nathanw /*
571 1.10.2.2 nathanw * Bail out. Reverse all the mappings
572 1.10.2.2 nathanw * and unwire the pages.
573 1.10.2.2 nathanw */
574 1.10.2.2 nathanw for (k = 0; k < done + j; k += AGP_PAGE_SIZE)
575 1.10.2.2 nathanw AGP_UNBIND_PAGE(sc, offset + k);
576 1.10.2.2 nathanw
577 1.10.2.2 nathanw bus_dmamap_unload(sc->as_dmat, mem->am_dmamap);
578 1.10.2.2 nathanw bus_dmamem_unmap(sc->as_dmat, mem->am_virtual,
579 1.10.2.2 nathanw mem->am_size);
580 1.10.2.2 nathanw bus_dmamem_free(sc->as_dmat, mem->am_dmaseg,
581 1.10.2.2 nathanw mem->am_nseg);
582 1.10.2.2 nathanw free(mem->am_dmaseg, M_AGP);
583 1.10.2.2 nathanw lockmgr(&sc->as_lock, LK_RELEASE, 0);
584 1.10.2.2 nathanw return error;
585 1.10.2.2 nathanw }
586 1.10.2.2 nathanw }
587 1.10.2.2 nathanw done += seg->ds_len;
588 1.10.2.2 nathanw }
589 1.10.2.2 nathanw
590 1.10.2.2 nathanw /*
591 1.10.2.2 nathanw * Flush the cpu cache since we are providing a new mapping
592 1.10.2.2 nathanw * for these pages.
593 1.10.2.2 nathanw */
594 1.10.2.2 nathanw agp_flush_cache();
595 1.10.2.2 nathanw
596 1.10.2.2 nathanw /*
597 1.10.2.2 nathanw * Make sure the chipset gets the new mappings.
598 1.10.2.2 nathanw */
599 1.10.2.2 nathanw AGP_FLUSH_TLB(sc);
600 1.10.2.2 nathanw
601 1.10.2.2 nathanw mem->am_offset = offset;
602 1.10.2.2 nathanw mem->am_is_bound = 1;
603 1.10.2.2 nathanw
604 1.10.2.2 nathanw lockmgr(&sc->as_lock, LK_RELEASE, 0);
605 1.10.2.2 nathanw
606 1.10.2.2 nathanw return 0;
607 1.10.2.2 nathanw }
608 1.10.2.2 nathanw
609 1.10.2.2 nathanw int
610 1.10.2.2 nathanw agp_generic_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
611 1.10.2.2 nathanw {
612 1.10.2.2 nathanw int i;
613 1.10.2.2 nathanw
614 1.10.2.2 nathanw lockmgr(&sc->as_lock, LK_EXCLUSIVE, 0);
615 1.10.2.2 nathanw
616 1.10.2.2 nathanw if (!mem->am_is_bound) {
617 1.10.2.2 nathanw printf("%s: memory is not bound\n", sc->as_dev.dv_xname);
618 1.10.2.2 nathanw lockmgr(&sc->as_lock, LK_RELEASE, 0);
619 1.10.2.2 nathanw return EINVAL;
620 1.10.2.2 nathanw }
621 1.10.2.2 nathanw
622 1.10.2.2 nathanw
623 1.10.2.2 nathanw /*
624 1.10.2.2 nathanw * Unbind the individual pages and flush the chipset's
625 1.10.2.2 nathanw * TLB. Unwire the pages so they can be swapped.
626 1.10.2.2 nathanw */
627 1.10.2.2 nathanw for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
628 1.10.2.2 nathanw AGP_UNBIND_PAGE(sc, mem->am_offset + i);
629 1.10.2.2 nathanw
630 1.10.2.2 nathanw agp_flush_cache();
631 1.10.2.2 nathanw AGP_FLUSH_TLB(sc);
632 1.10.2.2 nathanw
633 1.10.2.2 nathanw bus_dmamap_unload(sc->as_dmat, mem->am_dmamap);
634 1.10.2.2 nathanw bus_dmamem_unmap(sc->as_dmat, mem->am_virtual, mem->am_size);
635 1.10.2.2 nathanw bus_dmamem_free(sc->as_dmat, mem->am_dmaseg, mem->am_nseg);
636 1.10.2.2 nathanw
637 1.10.2.2 nathanw free(mem->am_dmaseg, M_AGP);
638 1.10.2.2 nathanw
639 1.10.2.2 nathanw mem->am_offset = 0;
640 1.10.2.2 nathanw mem->am_is_bound = 0;
641 1.10.2.2 nathanw
642 1.10.2.2 nathanw lockmgr(&sc->as_lock, LK_RELEASE, 0);
643 1.10.2.2 nathanw
644 1.10.2.2 nathanw return 0;
645 1.10.2.2 nathanw }
646 1.10.2.2 nathanw
647 1.10.2.2 nathanw /* Helper functions for implementing user/kernel api */
648 1.10.2.2 nathanw
649 1.10.2.2 nathanw static int
650 1.10.2.2 nathanw agp_acquire_helper(struct agp_softc *sc, enum agp_acquire_state state)
651 1.10.2.2 nathanw {
652 1.10.2.2 nathanw if (sc->as_state != AGP_ACQUIRE_FREE)
653 1.10.2.2 nathanw return EBUSY;
654 1.10.2.2 nathanw sc->as_state = state;
655 1.10.2.2 nathanw
656 1.10.2.2 nathanw return 0;
657 1.10.2.2 nathanw }
658 1.10.2.2 nathanw
659 1.10.2.2 nathanw static int
660 1.10.2.2 nathanw agp_release_helper(struct agp_softc *sc, enum agp_acquire_state state)
661 1.10.2.2 nathanw {
662 1.10.2.2 nathanw struct agp_memory *mem;
663 1.10.2.2 nathanw
664 1.10.2.2 nathanw if (sc->as_state == AGP_ACQUIRE_FREE)
665 1.10.2.2 nathanw return 0;
666 1.10.2.2 nathanw
667 1.10.2.2 nathanw if (sc->as_state != state)
668 1.10.2.2 nathanw return EBUSY;
669 1.10.2.2 nathanw
670 1.10.2.2 nathanw /*
671 1.10.2.2 nathanw * Clear out the aperture and free any outstanding memory blocks.
672 1.10.2.2 nathanw */
673 1.10.2.2 nathanw TAILQ_FOREACH(mem, &sc->as_memory, am_link) {
674 1.10.2.2 nathanw if (mem->am_is_bound) {
675 1.10.2.2 nathanw printf("agp_release_helper: mem %d is bound\n",
676 1.10.2.2 nathanw mem->am_id);
677 1.10.2.2 nathanw AGP_UNBIND_MEMORY(sc, mem);
678 1.10.2.2 nathanw }
679 1.10.2.2 nathanw }
680 1.10.2.2 nathanw
681 1.10.2.2 nathanw sc->as_state = AGP_ACQUIRE_FREE;
682 1.10.2.2 nathanw return 0;
683 1.10.2.2 nathanw }
684 1.10.2.2 nathanw
685 1.10.2.2 nathanw static struct agp_memory *
686 1.10.2.2 nathanw agp_find_memory(struct agp_softc *sc, int id)
687 1.10.2.2 nathanw {
688 1.10.2.2 nathanw struct agp_memory *mem;
689 1.10.2.2 nathanw
690 1.10.2.2 nathanw AGP_DPF("searching for memory block %d\n", id);
691 1.10.2.2 nathanw TAILQ_FOREACH(mem, &sc->as_memory, am_link) {
692 1.10.2.2 nathanw AGP_DPF("considering memory block %d\n", mem->am_id);
693 1.10.2.2 nathanw if (mem->am_id == id)
694 1.10.2.2 nathanw return mem;
695 1.10.2.2 nathanw }
696 1.10.2.2 nathanw return 0;
697 1.10.2.2 nathanw }
698 1.10.2.2 nathanw
699 1.10.2.2 nathanw /* Implementation of the userland ioctl api */
700 1.10.2.2 nathanw
701 1.10.2.2 nathanw static int
702 1.10.2.2 nathanw agp_info_user(struct agp_softc *sc, agp_info *info)
703 1.10.2.2 nathanw {
704 1.10.2.2 nathanw memset(info, 0, sizeof *info);
705 1.10.2.2 nathanw info->bridge_id = sc->as_id;
706 1.10.2.2 nathanw if (sc->as_capoff != 0)
707 1.10.2.2 nathanw info->agp_mode = pci_conf_read(sc->as_pc, sc->as_tag,
708 1.10.2.2 nathanw sc->as_capoff + AGP_STATUS);
709 1.10.2.2 nathanw else
710 1.10.2.2 nathanw info->agp_mode = 0; /* i810 doesn't have real AGP */
711 1.10.2.2 nathanw info->aper_base = sc->as_apaddr;
712 1.10.2.2 nathanw info->aper_size = AGP_GET_APERTURE(sc) >> 20;
713 1.10.2.2 nathanw info->pg_total = info->pg_system = sc->as_maxmem >> AGP_PAGE_SHIFT;
714 1.10.2.2 nathanw info->pg_used = sc->as_allocated >> AGP_PAGE_SHIFT;
715 1.10.2.2 nathanw
716 1.10.2.2 nathanw return 0;
717 1.10.2.2 nathanw }
718 1.10.2.2 nathanw
719 1.10.2.2 nathanw static int
720 1.10.2.2 nathanw agp_setup_user(struct agp_softc *sc, agp_setup *setup)
721 1.10.2.2 nathanw {
722 1.10.2.2 nathanw return AGP_ENABLE(sc, setup->agp_mode);
723 1.10.2.2 nathanw }
724 1.10.2.2 nathanw
725 1.10.2.2 nathanw static int
726 1.10.2.2 nathanw agp_allocate_user(struct agp_softc *sc, agp_allocate *alloc)
727 1.10.2.2 nathanw {
728 1.10.2.2 nathanw struct agp_memory *mem;
729 1.10.2.2 nathanw
730 1.10.2.2 nathanw mem = AGP_ALLOC_MEMORY(sc,
731 1.10.2.2 nathanw alloc->type,
732 1.10.2.2 nathanw alloc->pg_count << AGP_PAGE_SHIFT);
733 1.10.2.2 nathanw if (mem) {
734 1.10.2.2 nathanw alloc->key = mem->am_id;
735 1.10.2.2 nathanw alloc->physical = mem->am_physical;
736 1.10.2.2 nathanw return 0;
737 1.10.2.2 nathanw } else {
738 1.10.2.2 nathanw return ENOMEM;
739 1.10.2.2 nathanw }
740 1.10.2.2 nathanw }
741 1.10.2.2 nathanw
742 1.10.2.2 nathanw static int
743 1.10.2.2 nathanw agp_deallocate_user(struct agp_softc *sc, int id)
744 1.10.2.2 nathanw {
745 1.10.2.2 nathanw struct agp_memory *mem = agp_find_memory(sc, id);
746 1.10.2.2 nathanw
747 1.10.2.2 nathanw if (mem) {
748 1.10.2.2 nathanw AGP_FREE_MEMORY(sc, mem);
749 1.10.2.2 nathanw return 0;
750 1.10.2.2 nathanw } else {
751 1.10.2.2 nathanw return ENOENT;
752 1.10.2.2 nathanw }
753 1.10.2.2 nathanw }
754 1.10.2.2 nathanw
755 1.10.2.2 nathanw static int
756 1.10.2.2 nathanw agp_bind_user(struct agp_softc *sc, agp_bind *bind)
757 1.10.2.2 nathanw {
758 1.10.2.2 nathanw struct agp_memory *mem = agp_find_memory(sc, bind->key);
759 1.10.2.2 nathanw
760 1.10.2.2 nathanw if (!mem)
761 1.10.2.2 nathanw return ENOENT;
762 1.10.2.2 nathanw
763 1.10.2.2 nathanw return AGP_BIND_MEMORY(sc, mem, bind->pg_start << AGP_PAGE_SHIFT);
764 1.10.2.2 nathanw }
765 1.10.2.2 nathanw
766 1.10.2.2 nathanw static int
767 1.10.2.2 nathanw agp_unbind_user(struct agp_softc *sc, agp_unbind *unbind)
768 1.10.2.2 nathanw {
769 1.10.2.2 nathanw struct agp_memory *mem = agp_find_memory(sc, unbind->key);
770 1.10.2.2 nathanw
771 1.10.2.2 nathanw if (!mem)
772 1.10.2.2 nathanw return ENOENT;
773 1.10.2.2 nathanw
774 1.10.2.2 nathanw return AGP_UNBIND_MEMORY(sc, mem);
775 1.10.2.2 nathanw }
776 1.10.2.2 nathanw
777 1.10.2.2 nathanw int
778 1.10.2.2 nathanw agpopen(dev_t dev, int oflags, int devtype, struct proc *p)
779 1.10.2.2 nathanw {
780 1.10.2.2 nathanw struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
781 1.10.2.2 nathanw
782 1.10.2.2 nathanw if (sc == NULL)
783 1.10.2.2 nathanw return ENXIO;
784 1.10.2.2 nathanw
785 1.10.2.2 nathanw if (sc->as_chipc == NULL)
786 1.10.2.2 nathanw return ENXIO;
787 1.10.2.2 nathanw
788 1.10.2.2 nathanw if (!sc->as_isopen)
789 1.10.2.2 nathanw sc->as_isopen = 1;
790 1.10.2.2 nathanw else
791 1.10.2.2 nathanw return EBUSY;
792 1.10.2.2 nathanw
793 1.10.2.2 nathanw return 0;
794 1.10.2.2 nathanw }
795 1.10.2.2 nathanw
796 1.10.2.2 nathanw int
797 1.10.2.2 nathanw agpclose(dev_t dev, int fflag, int devtype, struct proc *p)
798 1.10.2.2 nathanw {
799 1.10.2.2 nathanw struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
800 1.10.2.2 nathanw
801 1.10.2.2 nathanw /*
802 1.10.2.2 nathanw * Clear the GATT and force release on last close
803 1.10.2.2 nathanw */
804 1.10.2.2 nathanw if (sc->as_state == AGP_ACQUIRE_USER)
805 1.10.2.2 nathanw agp_release_helper(sc, AGP_ACQUIRE_USER);
806 1.10.2.2 nathanw sc->as_isopen = 0;
807 1.10.2.2 nathanw
808 1.10.2.2 nathanw return 0;
809 1.10.2.2 nathanw }
810 1.10.2.2 nathanw
811 1.10.2.2 nathanw int
812 1.10.2.2 nathanw agpioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p)
813 1.10.2.2 nathanw {
814 1.10.2.2 nathanw struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
815 1.10.2.2 nathanw
816 1.10.2.2 nathanw if (sc == NULL)
817 1.10.2.2 nathanw return ENODEV;
818 1.10.2.2 nathanw
819 1.10.2.2 nathanw if ((fflag & FWRITE) == 0 && cmd != AGPIOC_INFO)
820 1.10.2.2 nathanw return EPERM;
821 1.10.2.2 nathanw
822 1.10.2.2 nathanw switch (cmd) {
823 1.10.2.2 nathanw case AGPIOC_INFO:
824 1.10.2.2 nathanw return agp_info_user(sc, (agp_info *) data);
825 1.10.2.2 nathanw
826 1.10.2.2 nathanw case AGPIOC_ACQUIRE:
827 1.10.2.2 nathanw return agp_acquire_helper(sc, AGP_ACQUIRE_USER);
828 1.10.2.2 nathanw
829 1.10.2.2 nathanw case AGPIOC_RELEASE:
830 1.10.2.2 nathanw return agp_release_helper(sc, AGP_ACQUIRE_USER);
831 1.10.2.2 nathanw
832 1.10.2.2 nathanw case AGPIOC_SETUP:
833 1.10.2.2 nathanw return agp_setup_user(sc, (agp_setup *)data);
834 1.10.2.2 nathanw
835 1.10.2.2 nathanw case AGPIOC_ALLOCATE:
836 1.10.2.2 nathanw return agp_allocate_user(sc, (agp_allocate *)data);
837 1.10.2.2 nathanw
838 1.10.2.2 nathanw case AGPIOC_DEALLOCATE:
839 1.10.2.2 nathanw return agp_deallocate_user(sc, *(int *) data);
840 1.10.2.2 nathanw
841 1.10.2.2 nathanw case AGPIOC_BIND:
842 1.10.2.2 nathanw return agp_bind_user(sc, (agp_bind *)data);
843 1.10.2.2 nathanw
844 1.10.2.2 nathanw case AGPIOC_UNBIND:
845 1.10.2.2 nathanw return agp_unbind_user(sc, (agp_unbind *)data);
846 1.10.2.2 nathanw
847 1.10.2.2 nathanw }
848 1.10.2.2 nathanw
849 1.10.2.2 nathanw return EINVAL;
850 1.10.2.2 nathanw }
851 1.10.2.2 nathanw
852 1.10.2.2 nathanw paddr_t
853 1.10.2.2 nathanw agpmmap(dev_t dev, off_t offset, int prot)
854 1.10.2.2 nathanw {
855 1.10.2.2 nathanw struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
856 1.10.2.2 nathanw
857 1.10.2.2 nathanw if (offset > AGP_GET_APERTURE(sc))
858 1.10.2.2 nathanw return -1;
859 1.10.2.2 nathanw
860 1.10.2.2 nathanw return (bus_space_mmap(sc->as_apt, sc->as_apaddr, offset, prot,
861 1.10.2.2 nathanw BUS_SPACE_MAP_LINEAR));
862 1.10.2.2 nathanw }
863 1.10.2.2 nathanw
864 1.10.2.2 nathanw /* Implementation of the kernel api */
865 1.10.2.2 nathanw
866 1.10.2.2 nathanw void *
867 1.10.2.2 nathanw agp_find_device(int unit)
868 1.10.2.2 nathanw {
869 1.10.2.2 nathanw return device_lookup(&agp_cd, unit);
870 1.10.2.2 nathanw }
871 1.10.2.2 nathanw
872 1.10.2.2 nathanw enum agp_acquire_state
873 1.10.2.2 nathanw agp_state(void *devcookie)
874 1.10.2.2 nathanw {
875 1.10.2.2 nathanw struct agp_softc *sc = devcookie;
876 1.10.2.2 nathanw return sc->as_state;
877 1.10.2.2 nathanw }
878 1.10.2.2 nathanw
879 1.10.2.2 nathanw void
880 1.10.2.2 nathanw agp_get_info(void *devcookie, struct agp_info *info)
881 1.10.2.2 nathanw {
882 1.10.2.2 nathanw struct agp_softc *sc = devcookie;
883 1.10.2.2 nathanw
884 1.10.2.2 nathanw info->ai_mode = pci_conf_read(sc->as_pc, sc->as_tag,
885 1.10.2.2 nathanw sc->as_capoff + AGP_STATUS);
886 1.10.2.2 nathanw info->ai_aperture_base = sc->as_apaddr;
887 1.10.2.2 nathanw info->ai_aperture_size = sc->as_apsize; /* XXXfvdl inconsistent */
888 1.10.2.2 nathanw info->ai_memory_allowed = sc->as_maxmem;
889 1.10.2.2 nathanw info->ai_memory_used = sc->as_allocated;
890 1.10.2.2 nathanw }
891 1.10.2.2 nathanw
892 1.10.2.2 nathanw int
893 1.10.2.2 nathanw agp_acquire(void *dev)
894 1.10.2.2 nathanw {
895 1.10.2.2 nathanw return agp_acquire_helper(dev, AGP_ACQUIRE_KERNEL);
896 1.10.2.2 nathanw }
897 1.10.2.2 nathanw
898 1.10.2.2 nathanw int
899 1.10.2.2 nathanw agp_release(void *dev)
900 1.10.2.2 nathanw {
901 1.10.2.2 nathanw return agp_release_helper(dev, AGP_ACQUIRE_KERNEL);
902 1.10.2.2 nathanw }
903 1.10.2.2 nathanw
904 1.10.2.2 nathanw int
905 1.10.2.2 nathanw agp_enable(void *dev, u_int32_t mode)
906 1.10.2.2 nathanw {
907 1.10.2.2 nathanw struct agp_softc *sc = dev;
908 1.10.2.2 nathanw
909 1.10.2.2 nathanw return AGP_ENABLE(sc, mode);
910 1.10.2.2 nathanw }
911 1.10.2.2 nathanw
912 1.10.2.2 nathanw void *agp_alloc_memory(void *dev, int type, vsize_t bytes)
913 1.10.2.2 nathanw {
914 1.10.2.2 nathanw struct agp_softc *sc = dev;
915 1.10.2.2 nathanw
916 1.10.2.2 nathanw return (void *)AGP_ALLOC_MEMORY(sc, type, bytes);
917 1.10.2.2 nathanw }
918 1.10.2.2 nathanw
919 1.10.2.2 nathanw void agp_free_memory(void *dev, void *handle)
920 1.10.2.2 nathanw {
921 1.10.2.2 nathanw struct agp_softc *sc = dev;
922 1.10.2.2 nathanw struct agp_memory *mem = (struct agp_memory *) handle;
923 1.10.2.2 nathanw AGP_FREE_MEMORY(sc, mem);
924 1.10.2.2 nathanw }
925 1.10.2.2 nathanw
926 1.10.2.2 nathanw int agp_bind_memory(void *dev, void *handle, off_t offset)
927 1.10.2.2 nathanw {
928 1.10.2.2 nathanw struct agp_softc *sc = dev;
929 1.10.2.2 nathanw struct agp_memory *mem = (struct agp_memory *) handle;
930 1.10.2.2 nathanw
931 1.10.2.2 nathanw return AGP_BIND_MEMORY(sc, mem, offset);
932 1.10.2.2 nathanw }
933 1.10.2.2 nathanw
934 1.10.2.2 nathanw int agp_unbind_memory(void *dev, void *handle)
935 1.10.2.2 nathanw {
936 1.10.2.2 nathanw struct agp_softc *sc = dev;
937 1.10.2.2 nathanw struct agp_memory *mem = (struct agp_memory *) handle;
938 1.10.2.2 nathanw
939 1.10.2.2 nathanw return AGP_UNBIND_MEMORY(sc, mem);
940 1.10.2.2 nathanw }
941 1.10.2.2 nathanw
942 1.10.2.2 nathanw void agp_memory_info(void *dev, void *handle, struct agp_memory_info *mi)
943 1.10.2.2 nathanw {
944 1.10.2.2 nathanw struct agp_memory *mem = (struct agp_memory *) handle;
945 1.10.2.2 nathanw
946 1.10.2.2 nathanw mi->ami_size = mem->am_size;
947 1.10.2.2 nathanw mi->ami_physical = mem->am_physical;
948 1.10.2.2 nathanw mi->ami_offset = mem->am_offset;
949 1.10.2.2 nathanw mi->ami_is_bound = mem->am_is_bound;
950 1.10.2.2 nathanw }
951 1.10.2.2 nathanw
952 1.10.2.2 nathanw int
953 1.10.2.2 nathanw agp_alloc_dmamem(bus_dma_tag_t tag, size_t size, int flags,
954 1.10.2.2 nathanw bus_dmamap_t *mapp, caddr_t *vaddr, bus_addr_t *baddr,
955 1.10.2.2 nathanw bus_dma_segment_t *seg, int nseg, int *rseg)
956 1.10.2.2 nathanw
957 1.10.2.2 nathanw {
958 1.10.2.2 nathanw int error, level = 0;
959 1.10.2.2 nathanw
960 1.10.2.2 nathanw if ((error = bus_dmamem_alloc(tag, size, PAGE_SIZE, 0,
961 1.10.2.2 nathanw seg, nseg, rseg, BUS_DMA_NOWAIT)) != 0)
962 1.10.2.2 nathanw goto out;
963 1.10.2.2 nathanw level++;
964 1.10.2.2 nathanw
965 1.10.2.2 nathanw if ((error = bus_dmamem_map(tag, seg, *rseg, size, vaddr,
966 1.10.2.2 nathanw BUS_DMA_NOWAIT | flags)) != 0)
967 1.10.2.2 nathanw goto out;
968 1.10.2.2 nathanw level++;
969 1.10.2.2 nathanw
970 1.10.2.2 nathanw if ((error = bus_dmamap_create(tag, size, *rseg, size, 0,
971 1.10.2.2 nathanw BUS_DMA_NOWAIT, mapp)) != 0)
972 1.10.2.2 nathanw goto out;
973 1.10.2.2 nathanw level++;
974 1.10.2.2 nathanw
975 1.10.2.2 nathanw if ((error = bus_dmamap_load(tag, *mapp, *vaddr, size, NULL,
976 1.10.2.2 nathanw BUS_DMA_NOWAIT)) != 0)
977 1.10.2.2 nathanw goto out;
978 1.10.2.2 nathanw
979 1.10.2.2 nathanw *baddr = (*mapp)->dm_segs[0].ds_addr;
980 1.10.2.2 nathanw
981 1.10.2.2 nathanw return 0;
982 1.10.2.2 nathanw out:
983 1.10.2.2 nathanw switch (level) {
984 1.10.2.2 nathanw case 3:
985 1.10.2.2 nathanw bus_dmamap_destroy(tag, *mapp);
986 1.10.2.2 nathanw /* FALLTHROUGH */
987 1.10.2.2 nathanw case 2:
988 1.10.2.2 nathanw bus_dmamem_unmap(tag, *vaddr, size);
989 1.10.2.2 nathanw /* FALLTHROUGH */
990 1.10.2.2 nathanw case 1:
991 1.10.2.2 nathanw bus_dmamem_free(tag, seg, *rseg);
992 1.10.2.2 nathanw break;
993 1.10.2.2 nathanw default:
994 1.10.2.2 nathanw break;
995 1.10.2.2 nathanw }
996 1.10.2.2 nathanw
997 1.10.2.2 nathanw return error;
998 1.10.2.2 nathanw }
999 1.10.2.2 nathanw
1000 1.10.2.2 nathanw void
1001 1.10.2.2 nathanw agp_free_dmamem(bus_dma_tag_t tag, size_t size, bus_dmamap_t map,
1002 1.10.2.2 nathanw caddr_t vaddr, bus_dma_segment_t *seg, int nseg)
1003 1.10.2.2 nathanw {
1004 1.10.2.2 nathanw
1005 1.10.2.2 nathanw bus_dmamap_unload(tag, map);
1006 1.10.2.2 nathanw bus_dmamap_destroy(tag, map);
1007 1.10.2.2 nathanw bus_dmamem_unmap(tag, vaddr, size);
1008 1.10.2.2 nathanw bus_dmamem_free(tag, seg, nseg);
1009 1.10.2.2 nathanw }
1010