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