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