1 /* $NetBSD: agp_via.c,v 1.4 2002/01/12 16:17:05 tsutsui Exp $ */ 2 3 /*- 4 * Copyright (c) 2000 Doug Rabson 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD: src/sys/pci/agp_via.c,v 1.3 2001/07/05 21:28:47 jhb Exp $ 29 */ 30 31 #include <sys/cdefs.h> 32 __KERNEL_RCSID(0, "$NetBSD: agp_via.c,v 1.4 2002/01/12 16:17:05 tsutsui Exp $"); 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/malloc.h> 37 #include <sys/kernel.h> 38 #include <sys/lock.h> 39 #include <sys/proc.h> 40 #include <sys/conf.h> 41 #include <sys/device.h> 42 #include <sys/agpio.h> 43 44 #include <uvm/uvm_extern.h> 45 46 #include <dev/pci/pcivar.h> 47 #include <dev/pci/pcireg.h> 48 #include <dev/pci/agpvar.h> 49 #include <dev/pci/agpreg.h> 50 51 #include <machine/bus.h> 52 53 static u_int32_t agp_via_get_aperture(struct agp_softc *); 54 static int agp_via_set_aperture(struct agp_softc *, u_int32_t); 55 static int agp_via_bind_page(struct agp_softc *, off_t, bus_addr_t); 56 static int agp_via_unbind_page(struct agp_softc *, off_t); 57 static void agp_via_flush_tlb(struct agp_softc *); 58 59 struct agp_methods agp_via_methods = { 60 agp_via_get_aperture, 61 agp_via_set_aperture, 62 agp_via_bind_page, 63 agp_via_unbind_page, 64 agp_via_flush_tlb, 65 agp_generic_enable, 66 agp_generic_alloc_memory, 67 agp_generic_free_memory, 68 agp_generic_bind_memory, 69 agp_generic_unbind_memory, 70 }; 71 72 struct agp_via_softc { 73 u_int32_t initial_aperture; /* aperture size at startup */ 74 struct agp_gatt *gatt; 75 }; 76 77 int 78 agp_via_attach(struct device *parent, struct device *self, void *aux) 79 { 80 struct pci_attach_args *pa = aux; 81 struct agp_softc *sc = (void *)self; 82 struct agp_via_softc *asc; 83 struct agp_gatt *gatt; 84 85 asc = malloc(sizeof *asc, M_AGP, M_NOWAIT|M_ZERO); 86 if (asc == NULL) { 87 printf(": can't allocate chipset-specific softc\n"); 88 return ENOMEM; 89 } 90 sc->as_chipc = asc; 91 sc->as_methods = &agp_via_methods; 92 pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff, 93 NULL); 94 95 if (agp_map_aperture(pa, sc) != 0) { 96 printf(": can't map aperture\n"); 97 free(asc, M_AGP); 98 return ENXIO; 99 } 100 101 asc->initial_aperture = AGP_GET_APERTURE(sc); 102 103 for (;;) { 104 gatt = agp_alloc_gatt(sc); 105 if (gatt) 106 break; 107 108 /* 109 * Probably contigmalloc failure. Try reducing the 110 * aperture so that the gatt size reduces. 111 */ 112 if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) { 113 agp_generic_detach(sc); 114 printf(": can't set aperture size\n"); 115 return ENOMEM; 116 } 117 } 118 asc->gatt = gatt; 119 120 /* Install the gatt. */ 121 pci_conf_write(pa->pa_pc, pa->pa_tag, AGP_VIA_ATTBASE, 122 gatt->ag_physical | 3); 123 124 /* Enable the aperture. */ 125 pci_conf_write(pa->pa_pc, pa->pa_tag, AGP_VIA_GARTCTRL, 0x0000000f); 126 127 return 0; 128 } 129 130 #if 0 131 static int 132 agp_via_detach(struct agp_softc *sc) 133 { 134 struct agp_via_softc *asc = sc->as_chipc; 135 int error; 136 137 error = agp_generic_detach(sc); 138 if (error) 139 return error; 140 141 pci_conf_write(sc->as_pc, sc->as_tag, AGP_VIA_GARTCTRL, 0); 142 pci_conf_write(sc->as_pc, sc->as_tag, AGP_VIA_ATTBASE, 0); 143 AGP_SET_APERTURE(sc, asc->initial_aperture); 144 agp_free_gatt(sc, asc->gatt); 145 146 return 0; 147 } 148 #endif 149 150 static u_int32_t 151 agp_via_get_aperture(struct agp_softc *sc) 152 { 153 u_int32_t apsize; 154 155 apsize = pci_conf_read(sc->as_pc, sc->as_tag, AGP_VIA_APSIZE) & 0x1f; 156 157 /* 158 * The size is determined by the number of low bits of 159 * register APBASE which are forced to zero. The low 20 bits 160 * are always forced to zero and each zero bit in the apsize 161 * field just read forces the corresponding bit in the 27:20 162 * to be zero. We calculate the aperture size accordingly. 163 */ 164 return (((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1; 165 } 166 167 static int 168 agp_via_set_aperture(struct agp_softc *sc, u_int32_t aperture) 169 { 170 u_int32_t apsize; 171 pcireg_t reg; 172 173 /* 174 * Reverse the magic from get_aperture. 175 */ 176 apsize = ((aperture - 1) >> 20) ^ 0xff; 177 178 /* 179 * Double check for sanity. 180 */ 181 if ((((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1 != aperture) 182 return EINVAL; 183 184 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_VIA_APSIZE); 185 reg &= ~0xff; 186 reg |= apsize; 187 pci_conf_write(sc->as_pc, sc->as_tag, AGP_VIA_APSIZE, reg); 188 189 return 0; 190 } 191 192 static int 193 agp_via_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical) 194 { 195 struct agp_via_softc *asc = sc->as_chipc; 196 197 if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT)) 198 return EINVAL; 199 200 asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical; 201 return 0; 202 } 203 204 static int 205 agp_via_unbind_page(struct agp_softc *sc, off_t offset) 206 { 207 struct agp_via_softc *asc = sc->as_chipc; 208 209 if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT)) 210 return EINVAL; 211 212 asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0; 213 return 0; 214 } 215 216 static void 217 agp_via_flush_tlb(struct agp_softc *sc) 218 { 219 pci_conf_write(sc->as_pc, sc->as_tag, AGP_VIA_GARTCTRL, 0x8f); 220 pci_conf_write(sc->as_pc, sc->as_tag, AGP_VIA_GARTCTRL, 0x0f); 221 } 222