1 /* $NetBSD: agp_apple.c,v 1.2 2007/10/19 12:00:39 ad Exp $ */ 2 3 /*- 4 * Copyright (c) 2007 Michael Lorenz 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 * 3. Neither the name of The NetBSD Foundation nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: agp_apple.c,v 1.2 2007/10/19 12:00:39 ad Exp $"); 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 <sys/bus.h> 52 53 static u_int32_t agp_apple_get_aperture(struct agp_softc *); 54 static int agp_apple_set_aperture(struct agp_softc *, u_int32_t); 55 static int agp_apple_bind_page(struct agp_softc *, off_t, bus_addr_t); 56 static int agp_apple_unbind_page(struct agp_softc *, off_t); 57 static void agp_apple_flush_tlb(struct agp_softc *); 58 59 static struct agp_methods agp_apple_methods = { 60 agp_apple_get_aperture, 61 agp_apple_set_aperture, 62 agp_apple_bind_page, 63 agp_apple_unbind_page, 64 agp_apple_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_apple_softc { 73 u_int32_t initial_aperture; /* aperture size at startup */ 74 struct agp_gatt *gatt; 75 }; 76 77 int 78 agp_apple_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_apple_softc *asc; 83 struct agp_gatt *gatt; 84 85 asc = malloc(sizeof *asc, M_AGP, M_NOWAIT|M_ZERO); 86 if (asc == NULL) { 87 aprint_error(": can't allocate chipset-specific softc\n"); 88 return ENOMEM; 89 } 90 sc->as_chipc = asc; 91 sc->as_methods = &agp_apple_methods; 92 pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff, 93 NULL); 94 95 sc->as_apaddr = 0; 96 sc->as_apsize = 8 * 1024 * 1024; 97 sc->as_apflags = 0; 98 sc->as_pc = pa->pa_pc; 99 sc->as_tag = pa->pa_tag; 100 sc->as_apt = pa->pa_memt; 101 102 asc->initial_aperture = sc->as_apsize; 103 104 for (;;) { 105 gatt = agp_alloc_gatt(sc); 106 if (gatt) 107 break; 108 sc->as_apsize = sc->as_apsize >> 1; 109 if (sc->as_apsize == 0) { 110 aprint_error(": can't set aperture size\n"); 111 return ENOMEM; 112 } 113 } 114 asc->gatt = gatt; 115 116 /* Install the gatt. */ 117 aprint_error("gatt: %08x %d MB\n", gatt->ag_physical, sc->as_apsize >> 20); 118 pci_conf_write(pa->pa_pc, pa->pa_tag, APPLE_UNINORTH_GART_BASE, 119 (gatt->ag_physical & 0xfffff000) | 120 (sc->as_apsize >> 22)); 121 122 /* Enable the aperture. */ 123 pci_conf_write(pa->pa_pc, pa->pa_tag, APPLE_UNINORTH_GART_CTRL, 124 APPLE_GART_EN); 125 pci_conf_write(pa->pa_pc, pa->pa_tag, APPLE_UNINORTH_GART_CTRL, 126 APPLE_GART_EN | APPLE_GART_INV); 127 pci_conf_write(pa->pa_pc, pa->pa_tag, APPLE_UNINORTH_GART_CTRL, 128 APPLE_GART_EN); 129 return 0; 130 } 131 132 static u_int32_t 133 agp_apple_get_aperture(struct agp_softc *sc) 134 { 135 #if 0 136 u_int32_t apsize = 0; 137 138 aprint_error("%s: ", __func__); 139 apsize = pci_conf_read(sc->as_pc, sc->as_tag, 140 APPLE_UNINORTH_GART_BASE) & 0x0000ffff; 141 aprint_error("%08x\n", apsize); 142 return (apsize << 22); 143 #else 144 return sc->as_apsize; 145 #endif 146 } 147 148 static int 149 agp_apple_set_aperture(struct agp_softc *sc, u_int32_t aperture) 150 { 151 pcireg_t reg; 152 153 aprint_error("%s: %08x\n", __func__, aperture); 154 reg = pci_conf_read(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_BASE) 155 & 0xfffff000; 156 reg |= ((aperture >> 22) & 0xfff); 157 pci_conf_write(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_BASE, reg); 158 sc->as_apsize = aperture; 159 return 0; 160 } 161 162 static int 163 agp_apple_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical) 164 { 165 struct agp_apple_softc *asc = sc->as_chipc; 166 167 if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT)) 168 return EINVAL; 169 170 asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = htole32(physical); 171 return 0; 172 } 173 174 static int 175 agp_apple_unbind_page(struct agp_softc *sc, off_t offset) 176 { 177 struct agp_apple_softc *asc = sc->as_chipc; 178 179 if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT)) 180 return EINVAL; 181 182 asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0; 183 return 0; 184 } 185 186 static void 187 agp_apple_flush_tlb(struct agp_softc *sc) 188 { 189 190 pci_conf_write(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_CTRL, 191 APPLE_GART_EN); 192 pci_conf_write(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_CTRL, 193 APPLE_GART_EN | APPLE_GART_INV); 194 pci_conf_write(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_CTRL, 195 APPLE_GART_EN); 196 } 197