agp_i810.c revision 1.3.2.2 1 1.3.2.2 thorpej /* $NetBSD: agp_i810.c,v 1.3.2.2 2001/09/13 01:15:50 thorpej Exp $ */
2 1.3.2.2 thorpej
3 1.3.2.2 thorpej /*-
4 1.3.2.2 thorpej * Copyright (c) 2000 Doug Rabson
5 1.3.2.2 thorpej * Copyright (c) 2000 Ruslan Ermilov
6 1.3.2.2 thorpej * All rights reserved.
7 1.3.2.2 thorpej *
8 1.3.2.2 thorpej * Redistribution and use in source and binary forms, with or without
9 1.3.2.2 thorpej * modification, are permitted provided that the following conditions
10 1.3.2.2 thorpej * are met:
11 1.3.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
12 1.3.2.2 thorpej * notice, this list of conditions and the following disclaimer.
13 1.3.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
14 1.3.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
15 1.3.2.2 thorpej * documentation and/or other materials provided with the distribution.
16 1.3.2.2 thorpej *
17 1.3.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 1.3.2.2 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.3.2.2 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.3.2.2 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 1.3.2.2 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.3.2.2 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.3.2.2 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.3.2.2 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.3.2.2 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.3.2.2 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.3.2.2 thorpej * SUCH DAMAGE.
28 1.3.2.2 thorpej *
29 1.3.2.2 thorpej * $FreeBSD: src/sys/pci/agp_i810.c,v 1.4 2001/07/05 21:28:47 jhb Exp $
30 1.3.2.2 thorpej */
31 1.3.2.2 thorpej
32 1.3.2.2 thorpej #include <sys/param.h>
33 1.3.2.2 thorpej #include <sys/systm.h>
34 1.3.2.2 thorpej #include <sys/malloc.h>
35 1.3.2.2 thorpej #include <sys/kernel.h>
36 1.3.2.2 thorpej #include <sys/lock.h>
37 1.3.2.2 thorpej #include <sys/proc.h>
38 1.3.2.2 thorpej #include <sys/device.h>
39 1.3.2.2 thorpej #include <sys/conf.h>
40 1.3.2.2 thorpej
41 1.3.2.2 thorpej #include <uvm/uvm_extern.h>
42 1.3.2.2 thorpej
43 1.3.2.2 thorpej #include <dev/pci/pcivar.h>
44 1.3.2.2 thorpej #include <dev/pci/pcireg.h>
45 1.3.2.2 thorpej #include <dev/pci/pcidevs.h>
46 1.3.2.2 thorpej #include <dev/pci/agpvar.h>
47 1.3.2.2 thorpej #include <dev/pci/agpreg.h>
48 1.3.2.2 thorpej
49 1.3.2.2 thorpej #include <sys/agpio.h>
50 1.3.2.2 thorpej
51 1.3.2.2 thorpej #include <machine/bus.h>
52 1.3.2.2 thorpej
53 1.3.2.2 thorpej #define READ1(off) bus_space_read_1(isc->bst, isc->bsh, off)
54 1.3.2.2 thorpej #define WRITE4(off,v) bus_space_write_4(isc->bst, isc->bsh, off, v)
55 1.3.2.2 thorpej
56 1.3.2.2 thorpej struct agp_i810_softc {
57 1.3.2.2 thorpej u_int32_t initial_aperture; /* aperture size at startup */
58 1.3.2.2 thorpej struct agp_gatt *gatt;
59 1.3.2.2 thorpej u_int32_t dcache_size;
60 1.3.2.2 thorpej bus_space_tag_t bst; /* bus_space tag */
61 1.3.2.2 thorpej bus_space_handle_t bsh; /* bus_space handle */
62 1.3.2.2 thorpej struct pci_attach_args vga_pa;
63 1.3.2.2 thorpej };
64 1.3.2.2 thorpej
65 1.3.2.2 thorpej static u_int32_t agp_i810_get_aperture(struct agp_softc *);
66 1.3.2.2 thorpej static int agp_i810_set_aperture(struct agp_softc *, u_int32_t);
67 1.3.2.2 thorpej static int agp_i810_bind_page(struct agp_softc *, off_t, bus_addr_t);
68 1.3.2.2 thorpej static int agp_i810_unbind_page(struct agp_softc *, off_t);
69 1.3.2.2 thorpej static void agp_i810_flush_tlb(struct agp_softc *);
70 1.3.2.2 thorpej static int agp_i810_enable(struct agp_softc *, u_int32_t mode);
71 1.3.2.2 thorpej static struct agp_memory *agp_i810_alloc_memory(struct agp_softc *, int,
72 1.3.2.2 thorpej vsize_t);
73 1.3.2.2 thorpej static int agp_i810_free_memory(struct agp_softc *, struct agp_memory *);
74 1.3.2.2 thorpej static int agp_i810_bind_memory(struct agp_softc *, struct agp_memory *, off_t);
75 1.3.2.2 thorpej static int agp_i810_unbind_memory(struct agp_softc *, struct agp_memory *);
76 1.3.2.2 thorpej
77 1.3.2.2 thorpej struct agp_methods agp_i810_methods = {
78 1.3.2.2 thorpej agp_i810_get_aperture,
79 1.3.2.2 thorpej agp_i810_set_aperture,
80 1.3.2.2 thorpej agp_i810_bind_page,
81 1.3.2.2 thorpej agp_i810_unbind_page,
82 1.3.2.2 thorpej agp_i810_flush_tlb,
83 1.3.2.2 thorpej agp_i810_enable,
84 1.3.2.2 thorpej agp_i810_alloc_memory,
85 1.3.2.2 thorpej agp_i810_free_memory,
86 1.3.2.2 thorpej agp_i810_bind_memory,
87 1.3.2.2 thorpej agp_i810_unbind_memory,
88 1.3.2.2 thorpej };
89 1.3.2.2 thorpej
90 1.3.2.2 thorpej static int
91 1.3.2.2 thorpej agp_i810_vgamatch(struct pci_attach_args *pa)
92 1.3.2.2 thorpej {
93 1.3.2.2 thorpej if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
94 1.3.2.2 thorpej PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
95 1.3.2.2 thorpej return 0;
96 1.3.2.2 thorpej switch (PCI_PRODUCT(pa->pa_id)) {
97 1.3.2.2 thorpej case PCI_PRODUCT_INTEL_82810_GC:
98 1.3.2.2 thorpej case PCI_PRODUCT_INTEL_82810_DC100_GC:
99 1.3.2.2 thorpej case PCI_PRODUCT_INTEL_82810E_GC:
100 1.3.2.2 thorpej case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
101 1.3.2.2 thorpej return 1;
102 1.3.2.2 thorpej };
103 1.3.2.2 thorpej
104 1.3.2.2 thorpej return 0;
105 1.3.2.2 thorpej }
106 1.3.2.2 thorpej
107 1.3.2.2 thorpej /*
108 1.3.2.2 thorpej * Find bridge device.
109 1.3.2.2 thorpej */
110 1.3.2.2 thorpej int
111 1.3.2.2 thorpej agp_i810_bridgematch(struct pci_attach_args *pa)
112 1.3.2.2 thorpej {
113 1.3.2.2 thorpej switch (PCI_PRODUCT(pa->pa_id)) {
114 1.3.2.2 thorpej case PCI_PRODUCT_INTEL_82810_MCH:
115 1.3.2.2 thorpej case PCI_PRODUCT_INTEL_82810_DC100_MCH:
116 1.3.2.2 thorpej case PCI_PRODUCT_INTEL_82810E_MCH:
117 1.3.2.2 thorpej case PCI_PRODUCT_INTEL_82815_FULL_HUB:
118 1.3.2.2 thorpej return 1;
119 1.3.2.2 thorpej }
120 1.3.2.2 thorpej
121 1.3.2.2 thorpej return 0;
122 1.3.2.2 thorpej }
123 1.3.2.2 thorpej
124 1.3.2.2 thorpej int
125 1.3.2.2 thorpej agp_i810_match(struct device *parent, struct cfdata *match, void *aux)
126 1.3.2.2 thorpej {
127 1.3.2.2 thorpej struct pci_attach_args vga_pa, *pa = aux;
128 1.3.2.2 thorpej pcireg_t ramreg;
129 1.3.2.2 thorpej
130 1.3.2.2 thorpej if (agp_i810_bridgematch(pa) == 0)
131 1.3.2.2 thorpej return 0;
132 1.3.2.2 thorpej /*
133 1.3.2.2 thorpej * XXXXfvdl
134 1.3.2.2 thorpej * this relies on the 'memory hub' and the VGA controller
135 1.3.2.2 thorpej * being on the same bus, which is bad. Fortunately, we
136 1.3.2.2 thorpej * know this to be the case with the i810.
137 1.3.2.2 thorpej *
138 1.3.2.2 thorpej * Could just have the attach fail later, leave as_chipc NULL
139 1.3.2.2 thorpej * and fail any open() call.
140 1.3.2.2 thorpej */
141 1.3.2.2 thorpej if (pci_find_device(&vga_pa, agp_i810_vgamatch) == 0)
142 1.3.2.2 thorpej return 0;
143 1.3.2.2 thorpej
144 1.3.2.2 thorpej ramreg = pci_conf_read(pa->pa_pc, pa->pa_tag, AGP_I810_SMRAM);
145 1.3.2.2 thorpej if ((ramreg & 0xff) == 0)
146 1.3.2.2 thorpej return 0;
147 1.3.2.2 thorpej
148 1.3.2.2 thorpej return 1;
149 1.3.2.2 thorpej }
150 1.3.2.2 thorpej
151 1.3.2.2 thorpej int
152 1.3.2.2 thorpej agp_i810_attach(struct device *parent, struct device *self, void *aux)
153 1.3.2.2 thorpej {
154 1.3.2.2 thorpej struct agp_softc *sc = (void *)self;
155 1.3.2.2 thorpej struct agp_i810_softc *isc;
156 1.3.2.2 thorpej struct agp_gatt *gatt;
157 1.3.2.2 thorpej int error;
158 1.3.2.2 thorpej
159 1.3.2.2 thorpej isc = malloc(sizeof *isc, M_AGP, M_NOWAIT);
160 1.3.2.2 thorpej if (isc == NULL) {
161 1.3.2.2 thorpej printf(": can't allocate chipset-specific softc\n");
162 1.3.2.2 thorpej return ENOMEM;
163 1.3.2.2 thorpej }
164 1.3.2.2 thorpej memset(isc, 0, sizeof *isc);
165 1.3.2.2 thorpej sc->as_chipc = isc;
166 1.3.2.2 thorpej sc->as_methods = &agp_i810_methods;
167 1.3.2.2 thorpej
168 1.3.2.2 thorpej if (pci_find_device(&isc->vga_pa, agp_i810_vgamatch) == 0) {
169 1.3.2.2 thorpej free(isc, M_AGP);
170 1.3.2.2 thorpej return ENOENT;
171 1.3.2.2 thorpej }
172 1.3.2.2 thorpej
173 1.3.2.2 thorpej /* XXXfvdl */
174 1.3.2.2 thorpej sc->as_dmat = isc->vga_pa.pa_dmat;
175 1.3.2.2 thorpej
176 1.3.2.2 thorpej error = agp_map_aperture(&isc->vga_pa, sc);
177 1.3.2.2 thorpej if (error != 0) {
178 1.3.2.2 thorpej free(isc, M_AGP);
179 1.3.2.2 thorpej return error;
180 1.3.2.2 thorpej }
181 1.3.2.2 thorpej
182 1.3.2.2 thorpej error = pci_mapreg_map(&isc->vga_pa, AGP_I810_MMADR,
183 1.3.2.2 thorpej PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh, NULL, NULL);
184 1.3.2.2 thorpej if (error != 0) {
185 1.3.2.2 thorpej printf(": can't map mmadr registers\n");
186 1.3.2.2 thorpej return error;
187 1.3.2.2 thorpej }
188 1.3.2.2 thorpej
189 1.3.2.2 thorpej isc->initial_aperture = AGP_GET_APERTURE(sc);
190 1.3.2.2 thorpej
191 1.3.2.2 thorpej if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
192 1.3.2.2 thorpej isc->dcache_size = 4 * 1024 * 1024;
193 1.3.2.2 thorpej else
194 1.3.2.2 thorpej isc->dcache_size = 0;
195 1.3.2.2 thorpej
196 1.3.2.2 thorpej for (;;) {
197 1.3.2.2 thorpej gatt = agp_alloc_gatt(sc);
198 1.3.2.2 thorpej if (gatt)
199 1.3.2.2 thorpej break;
200 1.3.2.2 thorpej
201 1.3.2.2 thorpej /*
202 1.3.2.2 thorpej * Probably contigmalloc failure. Try reducing the
203 1.3.2.2 thorpej * aperture so that the gatt size reduces.
204 1.3.2.2 thorpej */
205 1.3.2.2 thorpej if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
206 1.3.2.2 thorpej agp_generic_detach(sc);
207 1.3.2.2 thorpej return ENOMEM;
208 1.3.2.2 thorpej }
209 1.3.2.2 thorpej }
210 1.3.2.2 thorpej isc->gatt = gatt;
211 1.3.2.2 thorpej
212 1.3.2.2 thorpej /* Install the GATT. */
213 1.3.2.2 thorpej WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
214 1.3.2.2 thorpej
215 1.3.2.2 thorpej /*
216 1.3.2.2 thorpej * Make sure the chipset can see everything.
217 1.3.2.2 thorpej */
218 1.3.2.2 thorpej agp_flush_cache();
219 1.3.2.2 thorpej
220 1.3.2.2 thorpej return 0;
221 1.3.2.2 thorpej }
222 1.3.2.2 thorpej
223 1.3.2.2 thorpej #if 0
224 1.3.2.2 thorpej static int
225 1.3.2.2 thorpej agp_i810_detach(struct agp_softc *sc)
226 1.3.2.2 thorpej {
227 1.3.2.2 thorpej int error;
228 1.3.2.2 thorpej struct agp_i810_softc *isc = sc->as_chipc;
229 1.3.2.2 thorpej
230 1.3.2.2 thorpej error = agp_generic_detach(sc);
231 1.3.2.2 thorpej if (error)
232 1.3.2.2 thorpej return error;
233 1.3.2.2 thorpej
234 1.3.2.2 thorpej /* Clear the GATT base. */
235 1.3.2.2 thorpej WRITE4(AGP_I810_PGTBL_CTL, 0);
236 1.3.2.2 thorpej
237 1.3.2.2 thorpej /* Put the aperture back the way it started. */
238 1.3.2.2 thorpej AGP_SET_APERTURE(sc, isc->initial_aperture);
239 1.3.2.2 thorpej
240 1.3.2.2 thorpej agp_free_gatt(sc, isc->gatt);
241 1.3.2.2 thorpej
242 1.3.2.2 thorpej return 0;
243 1.3.2.2 thorpej }
244 1.3.2.2 thorpej #endif
245 1.3.2.2 thorpej
246 1.3.2.2 thorpej static u_int32_t
247 1.3.2.2 thorpej agp_i810_get_aperture(struct agp_softc *sc)
248 1.3.2.2 thorpej {
249 1.3.2.2 thorpej u_int16_t miscc;
250 1.3.2.2 thorpej
251 1.3.2.2 thorpej miscc = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM) >> 16;
252 1.3.2.2 thorpej if ((miscc & AGP_I810_MISCC_WINSIZE) == AGP_I810_MISCC_WINSIZE_32)
253 1.3.2.2 thorpej return 32 * 1024 * 1024;
254 1.3.2.2 thorpej else
255 1.3.2.2 thorpej return 64 * 1024 * 1024;
256 1.3.2.2 thorpej }
257 1.3.2.2 thorpej
258 1.3.2.2 thorpej static int
259 1.3.2.2 thorpej agp_i810_set_aperture(struct agp_softc *sc, u_int32_t aperture)
260 1.3.2.2 thorpej {
261 1.3.2.2 thorpej pcireg_t reg, miscc;
262 1.3.2.2 thorpej
263 1.3.2.2 thorpej /*
264 1.3.2.2 thorpej * Double check for sanity.
265 1.3.2.2 thorpej */
266 1.3.2.2 thorpej if (aperture != 32 * 1024 * 1024 && aperture != 64 * 1024 * 1024) {
267 1.3.2.2 thorpej printf("%s: bad aperture size %d\n", sc->as_dev.dv_xname,
268 1.3.2.2 thorpej aperture);
269 1.3.2.2 thorpej return EINVAL;
270 1.3.2.2 thorpej }
271 1.3.2.2 thorpej
272 1.3.2.2 thorpej reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
273 1.3.2.2 thorpej miscc = reg >> 16;
274 1.3.2.2 thorpej miscc &= ~AGP_I810_MISCC_WINSIZE;
275 1.3.2.2 thorpej if (aperture == 32 * 1024 * 1024)
276 1.3.2.2 thorpej miscc |= AGP_I810_MISCC_WINSIZE_32;
277 1.3.2.2 thorpej else
278 1.3.2.2 thorpej miscc |= AGP_I810_MISCC_WINSIZE_64;
279 1.3.2.2 thorpej
280 1.3.2.2 thorpej reg &= 0x0000ffff;
281 1.3.2.2 thorpej reg |= (miscc << 16);
282 1.3.2.2 thorpej pci_conf_write(sc->as_pc, sc->as_tag, AGP_I810_SMRAM, miscc);
283 1.3.2.2 thorpej
284 1.3.2.2 thorpej return 0;
285 1.3.2.2 thorpej }
286 1.3.2.2 thorpej
287 1.3.2.2 thorpej static int
288 1.3.2.2 thorpej agp_i810_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
289 1.3.2.2 thorpej {
290 1.3.2.2 thorpej struct agp_i810_softc *isc = sc->as_chipc;
291 1.3.2.2 thorpej
292 1.3.2.2 thorpej if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
293 1.3.2.2 thorpej return EINVAL;
294 1.3.2.2 thorpej
295 1.3.2.2 thorpej WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
296 1.3.2.2 thorpej physical | 1);
297 1.3.2.2 thorpej return 0;
298 1.3.2.2 thorpej }
299 1.3.2.2 thorpej
300 1.3.2.2 thorpej static int
301 1.3.2.2 thorpej agp_i810_unbind_page(struct agp_softc *sc, off_t offset)
302 1.3.2.2 thorpej {
303 1.3.2.2 thorpej struct agp_i810_softc *isc = sc->as_chipc;
304 1.3.2.2 thorpej
305 1.3.2.2 thorpej if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
306 1.3.2.2 thorpej return EINVAL;
307 1.3.2.2 thorpej
308 1.3.2.2 thorpej WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4, 0);
309 1.3.2.2 thorpej return 0;
310 1.3.2.2 thorpej }
311 1.3.2.2 thorpej
312 1.3.2.2 thorpej /*
313 1.3.2.2 thorpej * Writing via memory mapped registers already flushes all TLBs.
314 1.3.2.2 thorpej */
315 1.3.2.2 thorpej static void
316 1.3.2.2 thorpej agp_i810_flush_tlb(struct agp_softc *sc)
317 1.3.2.2 thorpej {
318 1.3.2.2 thorpej }
319 1.3.2.2 thorpej
320 1.3.2.2 thorpej static int
321 1.3.2.2 thorpej agp_i810_enable(struct agp_softc *sc, u_int32_t mode)
322 1.3.2.2 thorpej {
323 1.3.2.2 thorpej
324 1.3.2.2 thorpej return 0;
325 1.3.2.2 thorpej }
326 1.3.2.2 thorpej
327 1.3.2.2 thorpej static struct agp_memory *
328 1.3.2.2 thorpej agp_i810_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
329 1.3.2.2 thorpej {
330 1.3.2.2 thorpej struct agp_i810_softc *isc = sc->as_chipc;
331 1.3.2.2 thorpej struct agp_memory *mem;
332 1.3.2.2 thorpej
333 1.3.2.2 thorpej if ((size & (AGP_PAGE_SIZE - 1)) != 0)
334 1.3.2.2 thorpej return 0;
335 1.3.2.2 thorpej
336 1.3.2.2 thorpej if (sc->as_allocated + size > sc->as_maxmem)
337 1.3.2.2 thorpej return 0;
338 1.3.2.2 thorpej
339 1.3.2.2 thorpej if (type == 1) {
340 1.3.2.2 thorpej /*
341 1.3.2.2 thorpej * Mapping local DRAM into GATT.
342 1.3.2.2 thorpej */
343 1.3.2.2 thorpej if (size != isc->dcache_size)
344 1.3.2.2 thorpej return 0;
345 1.3.2.2 thorpej } else if (type == 2) {
346 1.3.2.2 thorpej /*
347 1.3.2.2 thorpej * Bogus mapping of a single page for the hardware cursor.
348 1.3.2.2 thorpej */
349 1.3.2.2 thorpej if (size != AGP_PAGE_SIZE)
350 1.3.2.2 thorpej return 0;
351 1.3.2.2 thorpej }
352 1.3.2.2 thorpej
353 1.3.2.2 thorpej mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
354 1.3.2.2 thorpej if (mem == NULL)
355 1.3.2.2 thorpej return NULL;
356 1.3.2.2 thorpej memset(mem, 0, sizeof *mem);
357 1.3.2.2 thorpej mem->am_id = sc->as_nextid++;
358 1.3.2.2 thorpej mem->am_size = size;
359 1.3.2.2 thorpej mem->am_type = type;
360 1.3.2.2 thorpej
361 1.3.2.2 thorpej if (type == 2) {
362 1.3.2.2 thorpej /*
363 1.3.2.2 thorpej * Allocate and wire down the page now so that we can
364 1.3.2.2 thorpej * get its physical address.
365 1.3.2.2 thorpej */
366 1.3.2.2 thorpej mem->am_dmaseg = malloc(sizeof *mem->am_dmaseg, M_AGP,
367 1.3.2.2 thorpej M_WAITOK);
368 1.3.2.2 thorpej if (mem->am_dmaseg == NULL) {
369 1.3.2.2 thorpej free(mem, M_AGP);
370 1.3.2.2 thorpej return NULL;
371 1.3.2.2 thorpej }
372 1.3.2.2 thorpej if (agp_alloc_dmamem(sc->as_dmat, size, 0,
373 1.3.2.2 thorpej &mem->am_dmamap, &mem->am_virtual, &mem->am_physical,
374 1.3.2.2 thorpej mem->am_dmaseg, 1, &mem->am_nseg) != 0) {
375 1.3.2.2 thorpej free(mem->am_dmaseg, M_AGP);
376 1.3.2.2 thorpej free(mem, M_AGP);
377 1.3.2.2 thorpej return NULL;
378 1.3.2.2 thorpej }
379 1.3.2.2 thorpej } else if (type != 1) {
380 1.3.2.2 thorpej if (bus_dmamap_create(sc->as_dmat, size, 1, size, 0,
381 1.3.2.2 thorpej BUS_DMA_NOWAIT, &mem->am_dmamap) != 0) {
382 1.3.2.2 thorpej free(mem, M_AGP);
383 1.3.2.2 thorpej return NULL;
384 1.3.2.2 thorpej }
385 1.3.2.2 thorpej }
386 1.3.2.2 thorpej
387 1.3.2.2 thorpej TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
388 1.3.2.2 thorpej sc->as_allocated += size;
389 1.3.2.2 thorpej
390 1.3.2.2 thorpej return mem;
391 1.3.2.2 thorpej }
392 1.3.2.2 thorpej
393 1.3.2.2 thorpej static int
394 1.3.2.2 thorpej agp_i810_free_memory(struct agp_softc *sc, struct agp_memory *mem)
395 1.3.2.2 thorpej {
396 1.3.2.2 thorpej if (mem->am_is_bound)
397 1.3.2.2 thorpej return EBUSY;
398 1.3.2.2 thorpej
399 1.3.2.2 thorpej if (mem->am_type == 2) {
400 1.3.2.2 thorpej agp_free_dmamem(sc->as_dmat, mem->am_size, mem->am_dmamap,
401 1.3.2.2 thorpej mem->am_virtual, mem->am_dmaseg, mem->am_nseg);
402 1.3.2.2 thorpej free(mem->am_dmaseg, M_AGP);
403 1.3.2.2 thorpej }
404 1.3.2.2 thorpej
405 1.3.2.2 thorpej sc->as_allocated -= mem->am_size;
406 1.3.2.2 thorpej TAILQ_REMOVE(&sc->as_memory, mem, am_link);
407 1.3.2.2 thorpej free(mem, M_AGP);
408 1.3.2.2 thorpej return 0;
409 1.3.2.2 thorpej }
410 1.3.2.2 thorpej
411 1.3.2.2 thorpej static int
412 1.3.2.2 thorpej agp_i810_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
413 1.3.2.2 thorpej off_t offset)
414 1.3.2.2 thorpej {
415 1.3.2.2 thorpej struct agp_i810_softc *isc = sc->as_chipc;
416 1.3.2.2 thorpej u_int32_t i;
417 1.3.2.2 thorpej
418 1.3.2.2 thorpej if (mem->am_type == 2)
419 1.3.2.2 thorpej return 0;
420 1.3.2.2 thorpej if (mem->am_type != 1)
421 1.3.2.2 thorpej return agp_generic_bind_memory(sc, mem, offset);
422 1.3.2.2 thorpej
423 1.3.2.2 thorpej for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
424 1.3.2.2 thorpej WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
425 1.3.2.2 thorpej i | 3);
426 1.3.2.2 thorpej }
427 1.3.2.2 thorpej
428 1.3.2.2 thorpej return 0;
429 1.3.2.2 thorpej }
430 1.3.2.2 thorpej
431 1.3.2.2 thorpej static int
432 1.3.2.2 thorpej agp_i810_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
433 1.3.2.2 thorpej {
434 1.3.2.2 thorpej struct agp_i810_softc *isc = sc->as_chipc;
435 1.3.2.2 thorpej u_int32_t i;
436 1.3.2.2 thorpej
437 1.3.2.2 thorpej if (mem->am_type == 2)
438 1.3.2.2 thorpej return 0;
439 1.3.2.2 thorpej
440 1.3.2.2 thorpej if (mem->am_type != 1)
441 1.3.2.2 thorpej return agp_generic_unbind_memory(sc, mem);
442 1.3.2.2 thorpej
443 1.3.2.2 thorpej for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
444 1.3.2.2 thorpej WRITE4(AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
445 1.3.2.2 thorpej
446 1.3.2.2 thorpej return 0;
447 1.3.2.2 thorpej }
448