2 * Copyright (c) 2005 Eric Anholt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * Based on reading the Linux 2.6.8.1 driver by Dave Jones.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
39 #include <sys/mutex.h>
42 #include <dev/agp/agppriv.h>
43 #include <dev/agp/agpreg.h>
44 #include <dev/pci/pcivar.h>
45 #include <dev/pci/pcireg.h>
48 #include <vm/vm_extern.h>
49 #include <vm/vm_kern.h>
50 #include <vm/vm_object.h>
52 #include <machine/bus.h>
53 #include <machine/resource.h>
56 MALLOC_DECLARE(M_AGP);
58 #define READ4(off) bus_space_read_4(sc->bst, sc->bsh, off)
59 #define WRITE4(off,v) bus_space_write_4(sc->bst, sc->bsh, off, v)
61 struct agp_ati_softc {
63 struct resource *regs; /* memory mapped control registers */
64 bus_space_tag_t bst; /* bus_space tag */
65 bus_space_handle_t bsh; /* bus_space handle */
66 u_int32_t initial_aperture; /* aperture size at startup */
71 u_int32_t *ag_virtual; /* virtual address of gatt */
72 u_int32_t *ag_vdir; /* virtual address of page dir */
73 vm_offset_t ag_pdir; /* physical address of page dir */
78 agp_ati_match(device_t dev)
80 if (pci_get_class(dev) != PCIC_BRIDGE ||
81 pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
84 if (agp_find_caps(dev) == 0)
87 switch (pci_get_devid(dev)) {
89 return ("ATI RS100 AGP bridge");
91 return ("ATI RS200 AGP bridge");
93 return ("ATI RS200M AGP bridge");
95 return ("ATI RS250 AGP bridge");
97 return ("ATI RS300_100 AGP bridge");
99 return ("ATI RS300_133 AGP bridge");
101 return ("ATI RS300_166 AGP bridge");
103 return ("ATI RS300_200 AGP bridge");
110 agp_ati_probe(device_t dev)
114 desc = agp_ati_match(dev);
116 device_set_desc(dev, desc);
124 agp_ati_alloc_gatt(device_t dev)
126 struct agp_ati_softc *sc = device_get_softc(dev);
127 u_int32_t apsize = AGP_GET_APERTURE(dev);
128 u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
129 u_int32_t apbase_offset;
132 /* Alloc the GATT -- pointers to pages of AGP memory */
133 sc->ag_entries = entries;
134 sc->ag_virtual = (void *)kmem_alloc_attr(kernel_arena,
135 entries * sizeof(u_int32_t), M_NOWAIT | M_ZERO, 0, ~0,
136 VM_MEMATTR_WRITE_COMBINING);
137 if (sc->ag_virtual == NULL) {
139 device_printf(dev, "GATT allocation failed\n");
143 /* Alloc the page directory -- pointers to each page of the GATT */
144 sc->ag_vdir = (void *)kmem_alloc_attr(kernel_arena, AGP_PAGE_SIZE,
145 M_NOWAIT | M_ZERO, 0, ~0, VM_MEMATTR_WRITE_COMBINING);
146 if (sc->ag_vdir == NULL) {
148 device_printf(dev, "pagedir allocation failed\n");
149 kmem_free(kernel_arena, (vm_offset_t)sc->ag_virtual,
150 entries * sizeof(u_int32_t));
153 sc->ag_pdir = vtophys((vm_offset_t)sc->ag_vdir);
155 apbase_offset = pci_read_config(dev, AGP_APBASE, 4) >> 22;
156 /* Fill in the pagedir's pointers to GATT pages */
157 for (i = 0; i < sc->ag_entries / 1024; i++) {
161 va = ((vm_offset_t)sc->ag_virtual) + i * AGP_PAGE_SIZE;
163 sc->ag_vdir[apbase_offset + i] = pa | 1;
171 agp_ati_attach(device_t dev)
173 struct agp_ati_softc *sc = device_get_softc(dev);
176 u_int32_t apsize_reg, agpmode_reg;
178 error = agp_generic_attach(dev);
182 switch (pci_get_devid(dev)) {
183 case 0xcab01002: /* ATI RS100 AGP bridge */
184 case 0xcab21002: /* ATI RS200 AGP bridge */
185 case 0xcbb21002: /* ATI RS200M AGP bridge */
186 case 0xcab31002: /* ATI RS250 AGP bridge */
188 apsize_reg = ATI_RS100_APSIZE;
189 agpmode_reg = ATI_RS100_IG_AGPMODE;
191 case 0x58301002: /* ATI RS300_100 AGP bridge */
192 case 0x58311002: /* ATI RS300_133 AGP bridge */
193 case 0x58321002: /* ATI RS300_166 AGP bridge */
194 case 0x58331002: /* ATI RS300_200 AGP bridge */
196 apsize_reg = ATI_RS300_APSIZE;
197 agpmode_reg = ATI_RS300_IG_AGPMODE;
200 /* Unknown chipset */
204 rid = ATI_GART_MMADDR;
205 sc->regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
207 agp_generic_detach(dev);
211 sc->bst = rman_get_bustag(sc->regs);
212 sc->bsh = rman_get_bushandle(sc->regs);
214 sc->initial_aperture = AGP_GET_APERTURE(dev);
217 if (agp_ati_alloc_gatt(dev) == 0)
221 * Probably contigmalloc failure. Try reducing the
222 * aperture so that the gatt size reduces.
224 if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2))
228 temp = pci_read_config(dev, apsize_reg, 4);
229 pci_write_config(dev, apsize_reg, temp | 1, 4);
231 pci_write_config(dev, agpmode_reg, 0x20000, 4);
233 WRITE4(ATI_GART_FEATURE_ID, 0x00060000);
235 temp = pci_read_config(dev, 4, 4); /* XXX: Magic reg# */
236 pci_write_config(dev, 4, temp | (1 << 14), 4);
238 WRITE4(ATI_GART_BASE, sc->ag_pdir);
246 agp_ati_detach(device_t dev)
248 struct agp_ati_softc *sc = device_get_softc(dev);
249 u_int32_t apsize_reg, temp;
254 apsize_reg = ATI_RS300_APSIZE;
256 apsize_reg = ATI_RS100_APSIZE;
258 /* Clear the GATT base */
259 WRITE4(ATI_GART_BASE, 0);
261 /* Put the aperture back the way it started. */
262 AGP_SET_APERTURE(dev, sc->initial_aperture);
264 temp = pci_read_config(dev, apsize_reg, 4);
265 pci_write_config(dev, apsize_reg, temp & ~1, 4);
267 kmem_free(kernel_arena, (vm_offset_t)sc->ag_vdir, AGP_PAGE_SIZE);
268 kmem_free(kernel_arena, (vm_offset_t)sc->ag_virtual,
269 sc->ag_entries * sizeof(u_int32_t));
271 bus_release_resource(dev, SYS_RES_MEMORY, ATI_GART_MMADDR, sc->regs);
278 agp_ati_get_aperture(device_t dev)
280 struct agp_ati_softc *sc = device_get_softc(dev);
284 size_value = pci_read_config(dev, ATI_RS300_APSIZE, 4);
286 size_value = pci_read_config(dev, ATI_RS100_APSIZE, 4);
288 size_value = (size_value & 0x0000000e) >> 1;
289 size_value = (32 * 1024 * 1024) << size_value;
295 agp_ati_set_aperture(device_t dev, u_int32_t aperture)
297 struct agp_ati_softc *sc = device_get_softc(dev);
299 u_int32_t apsize_reg;
302 apsize_reg = ATI_RS300_APSIZE;
304 apsize_reg = ATI_RS100_APSIZE;
306 size_value = pci_read_config(dev, apsize_reg, 4);
308 size_value &= ~0x0000000e;
309 size_value |= (ffs(aperture / (32 * 1024 * 1024)) - 1) << 1;
311 pci_write_config(dev, apsize_reg, size_value, 4);
317 agp_ati_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
319 struct agp_ati_softc *sc = device_get_softc(dev);
321 if (offset >= (sc->ag_entries << AGP_PAGE_SHIFT))
324 sc->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical | 1;
330 agp_ati_unbind_page(device_t dev, vm_offset_t offset)
332 struct agp_ati_softc *sc = device_get_softc(dev);
334 if (offset >= (sc->ag_entries << AGP_PAGE_SHIFT))
337 sc->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
342 agp_ati_flush_tlb(device_t dev)
344 struct agp_ati_softc *sc = device_get_softc(dev);
346 /* Set the cache invalidate bit and wait for the chipset to clear */
347 WRITE4(ATI_GART_CACHE_CNTRL, 1);
348 (void)READ4(ATI_GART_CACHE_CNTRL);
351 static device_method_t agp_ati_methods[] = {
352 /* Device interface */
353 DEVMETHOD(device_probe, agp_ati_probe),
354 DEVMETHOD(device_attach, agp_ati_attach),
355 DEVMETHOD(device_detach, agp_ati_detach),
356 DEVMETHOD(device_shutdown, bus_generic_shutdown),
357 DEVMETHOD(device_suspend, bus_generic_suspend),
358 DEVMETHOD(device_resume, bus_generic_resume),
361 DEVMETHOD(agp_get_aperture, agp_ati_get_aperture),
362 DEVMETHOD(agp_set_aperture, agp_ati_set_aperture),
363 DEVMETHOD(agp_bind_page, agp_ati_bind_page),
364 DEVMETHOD(agp_unbind_page, agp_ati_unbind_page),
365 DEVMETHOD(agp_flush_tlb, agp_ati_flush_tlb),
366 DEVMETHOD(agp_enable, agp_generic_enable),
367 DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory),
368 DEVMETHOD(agp_free_memory, agp_generic_free_memory),
369 DEVMETHOD(agp_bind_memory, agp_generic_bind_memory),
370 DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory),
375 static driver_t agp_ati_driver = {
378 sizeof(struct agp_ati_softc),
381 static devclass_t agp_devclass;
383 DRIVER_MODULE(agp_ati, hostb, agp_ati_driver, agp_devclass, 0, 0);
384 MODULE_DEPEND(agp_ati, agp, 1, 1, 1);
385 MODULE_DEPEND(agp_ati, pci, 1, 1, 1);