2 * Copyright (c) 2000 Doug Rabson
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 * $FreeBSD: src/sys/dev/agp/agp_amd.c,v 1.25 2007/11/12 21:51:36 jhb Exp $
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/kernel.h>
37 #include <bus/pci/pcivar.h>
38 #include <bus/pci/pcireg.h>
43 #include <vm/vm_object.h>
46 MALLOC_DECLARE(M_AGP);
48 #define READ2(off) bus_space_read_2(sc->bst, sc->bsh, off)
49 #define READ4(off) bus_space_read_4(sc->bst, sc->bsh, off)
50 #define WRITE2(off,v) bus_space_write_2(sc->bst, sc->bsh, off, v)
51 #define WRITE4(off,v) bus_space_write_4(sc->bst, sc->bsh, off, v)
55 u_int32_t *ag_virtual; /* virtual address of gatt */
56 vm_offset_t ag_physical;
57 u_int32_t *ag_vdir; /* virtual address of page dir */
58 vm_offset_t ag_pdir; /* physical address of page dir */
61 struct agp_amd_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 */
67 struct agp_amd_gatt *gatt;
70 static struct agp_amd_gatt *
71 agp_amd_alloc_gatt(device_t dev)
73 u_int32_t apsize = AGP_GET_APERTURE(dev);
74 u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
75 struct agp_amd_gatt *gatt;
76 int i, npages, pdir_offset;
80 "allocating GATT for aperture of size %dM\n",
81 apsize / (1024*1024));
83 gatt = kmalloc(sizeof(struct agp_amd_gatt), M_AGP, M_INTWAIT);
86 * The AMD751 uses a page directory to map a non-contiguous
87 * gatt so we don't need to use contigmalloc.
88 * Malloc individual gatt pages and map them into the page
91 gatt->ag_entries = entries;
92 gatt->ag_virtual = kmalloc(entries * sizeof(u_int32_t),
93 M_AGP, M_INTWAIT | M_ZERO);
96 * Allocate the page directory.
98 gatt->ag_vdir = kmalloc(AGP_PAGE_SIZE, M_AGP, M_INTWAIT | M_ZERO);
100 gatt->ag_pdir = vtophys((vm_offset_t) gatt->ag_vdir);
102 device_printf(dev, "gatt -> ag_pdir %#lx\n",
103 (u_long)gatt->ag_pdir);
105 * Allocate the gatt pages
107 gatt->ag_entries = entries;
109 device_printf(dev, "allocating GATT for %d AGP page entries\n",
112 gatt->ag_physical = vtophys((vm_offset_t) gatt->ag_virtual);
115 * Map the pages of the GATT into the page directory.
117 * The GATT page addresses are mapped into the directory offset by
118 * an amount dependent on the base address of the aperture. This
119 * is and offset into the page directory, not an offset added to
120 * the addresses of the gatt pages.
123 pdir_offset = pci_read_config(dev, AGP_AMD751_APBASE, 4) >> 22;
125 npages = ((entries * sizeof(u_int32_t) + AGP_PAGE_SIZE - 1)
128 for (i = 0; i < npages; i++) {
132 va = ((vm_offset_t) gatt->ag_virtual) + i * AGP_PAGE_SIZE;
134 gatt->ag_vdir[i + pdir_offset] = pa | 1;
138 * Make sure the chipset can see everything.
146 agp_amd_free_gatt(struct agp_amd_gatt *gatt)
148 kfree(gatt->ag_virtual, M_AGP);
149 kfree(gatt->ag_vdir, M_AGP);
154 agp_amd_match(device_t dev)
156 if (pci_get_class(dev) != PCIC_BRIDGE
157 || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
160 if (agp_find_caps(dev) == 0)
163 switch (pci_get_devid(dev)) {
165 return ("AMD 751 host to AGP bridge");
167 return ("AMD 761 host to AGP bridge");
169 return ("AMD 762 host to AGP bridge");
176 agp_amd_probe(device_t dev)
180 if (resource_disabled("agp", device_get_unit(dev)))
182 desc = agp_amd_match(dev);
185 device_set_desc(dev, desc);
186 return BUS_PROBE_DEFAULT;
193 agp_amd_attach(device_t dev)
195 struct agp_amd_softc *sc = device_get_softc(dev);
196 struct agp_amd_gatt *gatt;
199 error = agp_generic_attach(dev);
203 rid = AGP_AMD751_REGISTERS;
204 sc->regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
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);
215 if (sc->initial_aperture == 0) {
216 device_printf(dev, "bad initial aperture size, disabling\n");
221 gatt = agp_amd_alloc_gatt(dev);
226 * Probably contigmalloc failure. Try reducing the
227 * aperture so that the gatt size reduces.
229 if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2))
234 /* Install the gatt. */
235 WRITE4(AGP_AMD751_ATTBASE, gatt->ag_pdir);
237 /* Enable synchronisation between host and agp. */
238 pci_write_config(dev,
240 AGP_AMD751_MODECTRL_SYNEN, 1);
242 /* Set indexing mode for two-level and enable page dir cache */
243 pci_write_config(dev,
244 AGP_AMD751_MODECTRL2,
245 AGP_AMD751_MODECTRL2_GPDCE, 1);
247 /* Enable the TLB and flush */
248 WRITE2(AGP_AMD751_STATUS,
249 READ2(AGP_AMD751_STATUS) | AGP_AMD751_STATUS_GCE);
256 agp_amd_detach(device_t dev)
258 struct agp_amd_softc *sc = device_get_softc(dev);
262 /* Disable the TLB.. */
263 WRITE2(AGP_AMD751_STATUS,
264 READ2(AGP_AMD751_STATUS) & ~AGP_AMD751_STATUS_GCE);
266 /* Disable host-agp sync */
267 pci_write_config(dev, AGP_AMD751_MODECTRL, 0x00, 1);
269 /* Clear the GATT base */
270 WRITE4(AGP_AMD751_ATTBASE, 0);
272 /* Put the aperture back the way it started. */
273 AGP_SET_APERTURE(dev, sc->initial_aperture);
275 agp_amd_free_gatt(sc->gatt);
278 bus_release_resource(dev, SYS_RES_MEMORY,
279 AGP_AMD751_REGISTERS, sc->regs);
285 agp_amd_get_aperture(device_t dev)
290 * The aperture size is equal to 32M<<vas.
292 vas = (pci_read_config(dev, AGP_AMD751_APCTRL, 1) & 0x06) >> 1;
293 return (32*1024*1024) << vas;
297 agp_amd_set_aperture(device_t dev, u_int32_t aperture)
302 * Check for a power of two and make sure its within the
303 * programmable range.
305 if (aperture & (aperture - 1)
306 || aperture < 32*1024*1024
307 || aperture > 2U*1024*1024*1024)
310 vas = ffs(aperture / 32*1024*1024) - 1;
313 * While the size register is bits 1-3 of APCTRL, bit 0 must be
314 * set for the size value to be 'valid'
316 pci_write_config(dev, AGP_AMD751_APCTRL,
317 (((pci_read_config(dev, AGP_AMD751_APCTRL, 1) & ~0x06)
318 | ((vas << 1) | 1))), 1);
324 agp_amd_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
326 struct agp_amd_softc *sc = device_get_softc(dev);
328 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
331 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical | 1;
333 /* invalidate the cache */
339 agp_amd_unbind_page(device_t dev, vm_offset_t offset)
341 struct agp_amd_softc *sc = device_get_softc(dev);
343 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
346 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
351 agp_amd_flush_tlb(device_t dev)
353 struct agp_amd_softc *sc = device_get_softc(dev);
355 /* Set the cache invalidate bit and wait for the chipset to clear */
356 WRITE4(AGP_AMD751_TLBCTRL, 1);
359 } while (READ4(AGP_AMD751_TLBCTRL));
362 static device_method_t agp_amd_methods[] = {
363 /* Device interface */
364 DEVMETHOD(device_probe, agp_amd_probe),
365 DEVMETHOD(device_attach, agp_amd_attach),
366 DEVMETHOD(device_detach, agp_amd_detach),
367 DEVMETHOD(device_shutdown, bus_generic_shutdown),
368 DEVMETHOD(device_suspend, bus_generic_suspend),
369 DEVMETHOD(device_resume, bus_generic_resume),
372 DEVMETHOD(agp_get_aperture, agp_amd_get_aperture),
373 DEVMETHOD(agp_set_aperture, agp_amd_set_aperture),
374 DEVMETHOD(agp_bind_page, agp_amd_bind_page),
375 DEVMETHOD(agp_unbind_page, agp_amd_unbind_page),
376 DEVMETHOD(agp_flush_tlb, agp_amd_flush_tlb),
377 DEVMETHOD(agp_enable, agp_generic_enable),
378 DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory),
379 DEVMETHOD(agp_free_memory, agp_generic_free_memory),
380 DEVMETHOD(agp_bind_memory, agp_generic_bind_memory),
381 DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory),
386 static driver_t agp_amd_driver = {
389 sizeof(struct agp_amd_softc),
392 static devclass_t agp_devclass;
394 DRIVER_MODULE(agp_amd, pci, agp_amd_driver, agp_devclass, NULL, NULL);
395 MODULE_DEPEND(agp_amd, agp, 1, 1, 1);
396 MODULE_DEPEND(agp_amd, pci, 1, 1, 1);