159747216SDoug Rabson /*- 259747216SDoug Rabson * Copyright (c) 2000 Doug Rabson 359747216SDoug Rabson * All rights reserved. 459747216SDoug Rabson * 559747216SDoug Rabson * Redistribution and use in source and binary forms, with or without 659747216SDoug Rabson * modification, are permitted provided that the following conditions 759747216SDoug Rabson * are met: 859747216SDoug Rabson * 1. Redistributions of source code must retain the above copyright 959747216SDoug Rabson * notice, this list of conditions and the following disclaimer. 1059747216SDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright 1159747216SDoug Rabson * notice, this list of conditions and the following disclaimer in the 1259747216SDoug Rabson * documentation and/or other materials provided with the distribution. 1359747216SDoug Rabson * 1459747216SDoug Rabson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1559747216SDoug Rabson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1659747216SDoug Rabson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1759747216SDoug Rabson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1859747216SDoug Rabson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1959747216SDoug Rabson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2059747216SDoug Rabson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2159747216SDoug Rabson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2259747216SDoug Rabson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2359747216SDoug Rabson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2459747216SDoug Rabson * SUCH DAMAGE. 2559747216SDoug Rabson * 2659747216SDoug Rabson * $FreeBSD$ 2759747216SDoug Rabson */ 2859747216SDoug Rabson 2959747216SDoug Rabson #include "opt_bus.h" 3059747216SDoug Rabson #include "opt_pci.h" 3159747216SDoug Rabson 3259747216SDoug Rabson #include <sys/param.h> 3359747216SDoug Rabson #include <sys/systm.h> 3459747216SDoug Rabson #include <sys/malloc.h> 3559747216SDoug Rabson #include <sys/kernel.h> 3659747216SDoug Rabson #include <sys/bus.h> 3759747216SDoug Rabson #include <sys/lock.h> 3859747216SDoug Rabson 3959747216SDoug Rabson #include <pci/pcivar.h> 4059747216SDoug Rabson #include <pci/pcireg.h> 4159747216SDoug Rabson #include <pci/agppriv.h> 4259747216SDoug Rabson #include <pci/agpreg.h> 4359747216SDoug Rabson 4459747216SDoug Rabson #include <vm/vm.h> 4559747216SDoug Rabson #include <vm/vm_object.h> 4659747216SDoug Rabson #include <vm/pmap.h> 4759747216SDoug Rabson 4859747216SDoug Rabson struct agp_sis_softc { 4959747216SDoug Rabson struct agp_softc agp; 5059747216SDoug Rabson u_int32_t initial_aperture; /* aperture size at startup */ 5159747216SDoug Rabson struct agp_gatt *gatt; 5259747216SDoug Rabson }; 5359747216SDoug Rabson 5459747216SDoug Rabson static const char* 5559747216SDoug Rabson agp_sis_match(device_t dev) 5659747216SDoug Rabson { 5759747216SDoug Rabson if (pci_get_class(dev) != PCIC_BRIDGE 5859747216SDoug Rabson || pci_get_subclass(dev) != PCIS_BRIDGE_HOST) 5959747216SDoug Rabson return NULL; 6059747216SDoug Rabson 6159747216SDoug Rabson if (agp_find_caps(dev) == 0) 6259747216SDoug Rabson return NULL; 6359747216SDoug Rabson 6459747216SDoug Rabson switch (pci_get_devid(dev)) { 6559747216SDoug Rabson case 0x00011039: 6659747216SDoug Rabson return ("SiS 5591 host to AGP bridge"); 6759747216SDoug Rabson }; 6859747216SDoug Rabson 6959747216SDoug Rabson if (pci_get_vendor(dev) == 0x1039) 7059747216SDoug Rabson return ("SIS Generic host to PCI bridge"); 7159747216SDoug Rabson 7259747216SDoug Rabson return NULL; 7359747216SDoug Rabson } 7459747216SDoug Rabson 7559747216SDoug Rabson static int 7659747216SDoug Rabson agp_sis_probe(device_t dev) 7759747216SDoug Rabson { 7859747216SDoug Rabson const char *desc; 7959747216SDoug Rabson 8059747216SDoug Rabson desc = agp_sis_match(dev); 8159747216SDoug Rabson if (desc) { 8259747216SDoug Rabson device_verbose(dev); 8359747216SDoug Rabson device_set_desc(dev, desc); 8459747216SDoug Rabson return 0; 8559747216SDoug Rabson } 8659747216SDoug Rabson 8759747216SDoug Rabson return ENXIO; 8859747216SDoug Rabson } 8959747216SDoug Rabson 9059747216SDoug Rabson static int 9159747216SDoug Rabson agp_sis_attach(device_t dev) 9259747216SDoug Rabson { 9359747216SDoug Rabson struct agp_sis_softc *sc = device_get_softc(dev); 9459747216SDoug Rabson struct agp_gatt *gatt; 9559747216SDoug Rabson int error; 9659747216SDoug Rabson 9759747216SDoug Rabson error = agp_generic_attach(dev); 9859747216SDoug Rabson if (error) 9959747216SDoug Rabson return error; 10059747216SDoug Rabson 10159747216SDoug Rabson sc->initial_aperture = AGP_GET_APERTURE(dev); 10259747216SDoug Rabson 10359747216SDoug Rabson for (;;) { 10459747216SDoug Rabson gatt = agp_alloc_gatt(dev); 10559747216SDoug Rabson if (gatt) 10659747216SDoug Rabson break; 10759747216SDoug Rabson 10859747216SDoug Rabson /* 10959747216SDoug Rabson * Probably contigmalloc failure. Try reducing the 11059747216SDoug Rabson * aperture so that the gatt size reduces. 11159747216SDoug Rabson */ 11259747216SDoug Rabson if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2)) { 11359747216SDoug Rabson agp_generic_detach(dev); 11459747216SDoug Rabson return ENOMEM; 11559747216SDoug Rabson } 11659747216SDoug Rabson } 11759747216SDoug Rabson sc->gatt = gatt; 11859747216SDoug Rabson 11959747216SDoug Rabson /* Install the gatt. */ 12059747216SDoug Rabson pci_write_config(dev, AGP_SIS_ATTBASE, gatt->ag_physical, 4); 12159747216SDoug Rabson 12259747216SDoug Rabson /* Enable the aperture. */ 12359747216SDoug Rabson pci_write_config(dev, AGP_SIS_WINCTRL, 12459747216SDoug Rabson pci_read_config(dev, AGP_SIS_WINCTRL, 1) | 3, 1); 12559747216SDoug Rabson 12659747216SDoug Rabson /* 12759747216SDoug Rabson * Enable the TLB and make it automatically invalidate entries 12859747216SDoug Rabson * when the GATT is written. 12959747216SDoug Rabson */ 13059747216SDoug Rabson pci_write_config(dev, AGP_SIS_TLBCTRL, 0x05, 1); 13159747216SDoug Rabson 13259747216SDoug Rabson return 0; 13359747216SDoug Rabson } 13459747216SDoug Rabson 13559747216SDoug Rabson static int 13659747216SDoug Rabson agp_sis_detach(device_t dev) 13759747216SDoug Rabson { 13859747216SDoug Rabson struct agp_sis_softc *sc = device_get_softc(dev); 13959747216SDoug Rabson int error; 14059747216SDoug Rabson 14159747216SDoug Rabson error = agp_generic_detach(dev); 14259747216SDoug Rabson if (error) 14359747216SDoug Rabson return error; 14459747216SDoug Rabson 14559747216SDoug Rabson /* Disable the aperture.. */ 14659747216SDoug Rabson pci_write_config(dev, AGP_SIS_WINCTRL, 14759747216SDoug Rabson pci_read_config(dev, AGP_SIS_WINCTRL, 1) & ~3, 1); 14859747216SDoug Rabson 14959747216SDoug Rabson /* and the TLB. */ 15059747216SDoug Rabson pci_write_config(dev, AGP_SIS_TLBCTRL, 0, 1); 15159747216SDoug Rabson 15259747216SDoug Rabson /* Put the aperture back the way it started. */ 15359747216SDoug Rabson AGP_SET_APERTURE(dev, sc->initial_aperture); 15459747216SDoug Rabson 15559747216SDoug Rabson agp_free_gatt(sc->gatt); 15659747216SDoug Rabson return 0; 15759747216SDoug Rabson } 15859747216SDoug Rabson 15959747216SDoug Rabson static u_int32_t 16059747216SDoug Rabson agp_sis_get_aperture(device_t dev) 16159747216SDoug Rabson { 16259747216SDoug Rabson int gws; 16359747216SDoug Rabson 16459747216SDoug Rabson /* 16559747216SDoug Rabson * The aperture size is equal to 4M<<gws. 16659747216SDoug Rabson */ 16759747216SDoug Rabson gws = (pci_read_config(dev, AGP_SIS_WINCTRL, 1) & 0x70) >> 4; 16859747216SDoug Rabson return (4*1024*1024) << gws; 16959747216SDoug Rabson } 17059747216SDoug Rabson 17159747216SDoug Rabson static int 17259747216SDoug Rabson agp_sis_set_aperture(device_t dev, u_int32_t aperture) 17359747216SDoug Rabson { 17459747216SDoug Rabson int gws; 17559747216SDoug Rabson 17659747216SDoug Rabson /* 17759747216SDoug Rabson * Check for a power of two and make sure its within the 17859747216SDoug Rabson * programmable range. 17959747216SDoug Rabson */ 18059747216SDoug Rabson if (aperture & (aperture - 1) 18159747216SDoug Rabson || aperture < 4*1024*1024 18259747216SDoug Rabson || aperture > 256*1024*1024) 18359747216SDoug Rabson return EINVAL; 18459747216SDoug Rabson 18559747216SDoug Rabson gws = ffs(aperture / 4*1024*1024) - 1; 18659747216SDoug Rabson 18759747216SDoug Rabson pci_write_config(dev, AGP_SIS_WINCTRL, 18859747216SDoug Rabson ((pci_read_config(dev, AGP_SIS_WINCTRL, 1) & ~0x70) 18959747216SDoug Rabson | gws << 4), 1); 19059747216SDoug Rabson 19159747216SDoug Rabson return 0; 19259747216SDoug Rabson } 19359747216SDoug Rabson 19459747216SDoug Rabson static int 19559747216SDoug Rabson agp_sis_bind_page(device_t dev, int offset, vm_offset_t physical) 19659747216SDoug Rabson { 19759747216SDoug Rabson struct agp_sis_softc *sc = device_get_softc(dev); 19859747216SDoug Rabson 19959747216SDoug Rabson if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 20059747216SDoug Rabson return EINVAL; 20159747216SDoug Rabson 20259747216SDoug Rabson sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical; 20359747216SDoug Rabson return 0; 20459747216SDoug Rabson } 20559747216SDoug Rabson 20659747216SDoug Rabson static int 20759747216SDoug Rabson agp_sis_unbind_page(device_t dev, int offset) 20859747216SDoug Rabson { 20959747216SDoug Rabson struct agp_sis_softc *sc = device_get_softc(dev); 21059747216SDoug Rabson 21159747216SDoug Rabson if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 21259747216SDoug Rabson return EINVAL; 21359747216SDoug Rabson 21459747216SDoug Rabson sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0; 21559747216SDoug Rabson return 0; 21659747216SDoug Rabson } 21759747216SDoug Rabson 21859747216SDoug Rabson static void 21959747216SDoug Rabson agp_sis_flush_tlb(device_t dev) 22059747216SDoug Rabson { 22159747216SDoug Rabson pci_write_config(dev, AGP_SIS_TLBFLUSH, 0x02, 1); 22259747216SDoug Rabson } 22359747216SDoug Rabson 22459747216SDoug Rabson static device_method_t agp_sis_methods[] = { 22559747216SDoug Rabson /* Device interface */ 22659747216SDoug Rabson DEVMETHOD(device_probe, agp_sis_probe), 22759747216SDoug Rabson DEVMETHOD(device_attach, agp_sis_attach), 22859747216SDoug Rabson DEVMETHOD(device_detach, agp_sis_detach), 22959747216SDoug Rabson DEVMETHOD(device_shutdown, bus_generic_shutdown), 23059747216SDoug Rabson DEVMETHOD(device_suspend, bus_generic_suspend), 23159747216SDoug Rabson DEVMETHOD(device_resume, bus_generic_resume), 23259747216SDoug Rabson 23359747216SDoug Rabson /* AGP interface */ 23459747216SDoug Rabson DEVMETHOD(agp_get_aperture, agp_sis_get_aperture), 23559747216SDoug Rabson DEVMETHOD(agp_set_aperture, agp_sis_set_aperture), 23659747216SDoug Rabson DEVMETHOD(agp_bind_page, agp_sis_bind_page), 23759747216SDoug Rabson DEVMETHOD(agp_unbind_page, agp_sis_unbind_page), 23859747216SDoug Rabson DEVMETHOD(agp_flush_tlb, agp_sis_flush_tlb), 23959747216SDoug Rabson DEVMETHOD(agp_enable, agp_generic_enable), 24059747216SDoug Rabson DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory), 24159747216SDoug Rabson DEVMETHOD(agp_free_memory, agp_generic_free_memory), 24259747216SDoug Rabson DEVMETHOD(agp_bind_memory, agp_generic_bind_memory), 24359747216SDoug Rabson DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory), 24459747216SDoug Rabson 24559747216SDoug Rabson { 0, 0 } 24659747216SDoug Rabson }; 24759747216SDoug Rabson 24859747216SDoug Rabson static driver_t agp_sis_driver = { 24959747216SDoug Rabson "agp", 25059747216SDoug Rabson agp_sis_methods, 25159747216SDoug Rabson sizeof(struct agp_sis_softc), 25259747216SDoug Rabson }; 25359747216SDoug Rabson 25459747216SDoug Rabson static devclass_t agp_devclass; 25559747216SDoug Rabson 25659747216SDoug Rabson DRIVER_MODULE(agp_sis, pci, agp_sis_driver, agp_devclass, 0, 0); 257