1 /* $OpenBSD: agp_apple.c,v 1.9 2024/05/24 06:02:53 jsg Exp $ */ 2 3 /* 4 * Copyright (c) 2012 Martin Pieuchot <mpi@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/param.h> 20 #include <sys/systm.h> 21 #include <sys/device.h> 22 23 #include <dev/pci/pcivar.h> 24 #include <dev/pci/pcireg.h> 25 #include <dev/pci/pcidevs.h> 26 #include <dev/pci/agpvar.h> 27 #include <dev/pci/agpreg.h> 28 29 #include <machine/bus.h> 30 31 #define M (1024 * 1024) 32 33 struct agp_apple_softc { 34 struct device dev; 35 struct agp_softc *agpdev; 36 struct agp_gatt *gatt; 37 pci_chipset_tag_t asc_pc; 38 pcitag_t asc_tag; 39 bus_addr_t asc_apaddr; 40 bus_size_t asc_apsize; 41 int asc_flags; 42 #define AGP_APPLE_ISU3 (1 << 0) 43 }; 44 45 int agp_apple_match(struct device *, void *, void *); 46 void agp_apple_attach(struct device *, struct device *, void *); 47 bus_size_t agp_apple_get_aperture(void *); 48 int agp_apple_set_aperture(void *sc, bus_size_t); 49 void agp_apple_bind_page(void *, bus_addr_t, paddr_t, int); 50 void agp_apple_unbind_page(void *, bus_addr_t); 51 void agp_apple_flush_tlb(void *); 52 int agp_apple_enable(void *, uint32_t); 53 54 const struct cfattach appleagp_ca = { 55 sizeof(struct agp_apple_softc), agp_apple_match, agp_apple_attach, 56 }; 57 58 struct cfdriver appleagp_cd = { 59 NULL, "appleagp", DV_DULL 60 }; 61 62 const struct agp_methods agp_apple_methods = { 63 agp_apple_bind_page, 64 agp_apple_unbind_page, 65 agp_apple_flush_tlb, 66 agp_apple_enable, 67 }; 68 69 int 70 agp_apple_match(struct device *parent, void *match, void *aux) 71 { 72 struct agp_attach_args *aa = aux; 73 struct pci_attach_args *pa = aa->aa_pa; 74 75 if (agpbus_probe(aa) != 1 || PCI_VENDOR(pa->pa_id) != PCI_VENDOR_APPLE) 76 return (0); 77 78 switch (PCI_PRODUCT(pa->pa_id)) { 79 case PCI_PRODUCT_APPLE_UNINORTH_AGP: 80 case PCI_PRODUCT_APPLE_PANGEA_AGP: 81 case PCI_PRODUCT_APPLE_UNINORTH2_AGP: 82 case PCI_PRODUCT_APPLE_UNINORTH_AGP3: 83 case PCI_PRODUCT_APPLE_INTREPID2_AGP: 84 /* XXX until KMS works with these bridges */ 85 return (0); 86 case PCI_PRODUCT_APPLE_U3_AGP: 87 case PCI_PRODUCT_APPLE_U3L_AGP: 88 case PCI_PRODUCT_APPLE_K2_AGP: 89 return (1); 90 } 91 92 return (0); 93 } 94 95 void 96 agp_apple_attach(struct device *parent, struct device *self, void *aux) 97 { 98 struct agp_apple_softc *asc = (struct agp_apple_softc *)self; 99 struct agp_attach_args *aa = aux; 100 struct pci_attach_args *pa = aa->aa_pa; 101 struct agp_gatt *gatt; 102 103 asc->asc_tag = pa->pa_tag; 104 asc->asc_pc = pa->pa_pc; 105 106 switch (PCI_PRODUCT(pa->pa_id)) { 107 case PCI_PRODUCT_APPLE_U3_AGP: 108 case PCI_PRODUCT_APPLE_U3L_AGP: 109 case PCI_PRODUCT_APPLE_K2_AGP: 110 asc->asc_flags |= AGP_APPLE_ISU3; 111 break; 112 default: 113 break; 114 } 115 116 /* 117 * XXX It looks like UniNorth GART only accepts an aperture 118 * base address of 0x00 certainly because it does not perform 119 * any physical-to-physical address translation. 120 */ 121 asc->asc_apaddr = 0x00; 122 pci_conf_write(asc->asc_pc, asc->asc_tag, AGP_APPLE_APBASE, 123 asc->asc_apaddr); 124 125 /* 126 * There's no way to read the aperture size but all UniNorth 127 * chips seem to support an aperture of 256M (and 512M for U3). 128 */ 129 asc->asc_apsize = 256 * M; 130 for (;;) { 131 gatt = agp_alloc_gatt(pa->pa_dmat, asc->asc_apsize); 132 if (gatt != NULL) 133 break; 134 135 asc->asc_apsize /= 2; 136 } 137 asc->gatt = gatt; 138 139 if (agp_apple_set_aperture(asc, asc->asc_apsize)) { 140 printf("failed to set aperture\n"); 141 return; 142 } 143 144 agp_apple_flush_tlb(asc); 145 146 asc->agpdev = (struct agp_softc *)agp_attach_bus(pa, &agp_apple_methods, 147 asc->asc_apaddr, asc->asc_apsize, &asc->dev); 148 } 149 150 bus_size_t 151 agp_apple_get_aperture(void *dev) 152 { 153 struct agp_apple_softc *asc = dev; 154 155 return (asc->asc_apsize); 156 } 157 158 int 159 agp_apple_set_aperture(void *dev, bus_size_t aperture) 160 { 161 struct agp_apple_softc *asc = dev; 162 163 if (aperture % (4 * M) || aperture < (4 * M) || aperture > (256 * M)) 164 return (EINVAL); 165 166 pci_conf_write(asc->asc_pc, asc->asc_tag, AGP_APPLE_ATTBASE, 167 (asc->gatt->ag_physical & 0xfffff000) | (aperture >> 22)); 168 169 return (0); 170 } 171 172 #define flushd(p) __asm volatile("dcbst 0,%0; sync" ::"r"(p) : "memory") 173 174 void 175 agp_apple_bind_page(void *v, bus_addr_t off, paddr_t pa, int flags) 176 { 177 struct agp_apple_softc *asc = v; 178 uint32_t entry; 179 180 if (off >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT)) 181 return; 182 183 if (asc->asc_flags & AGP_APPLE_ISU3) 184 entry = (pa >> PAGE_SHIFT | 0x80000000); 185 else 186 entry = htole32(pa | 0x01); 187 188 flushdcache((void *)pa, PAGE_SIZE); 189 190 asc->gatt->ag_virtual[off >> AGP_PAGE_SHIFT] = entry; 191 flushd(&asc->gatt->ag_virtual[off >> AGP_PAGE_SHIFT]); 192 } 193 194 void 195 agp_apple_unbind_page(void *v, bus_size_t off) 196 { 197 struct agp_apple_softc *asc = v; 198 199 if (off >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT)) 200 return; 201 202 asc->gatt->ag_virtual[off >> AGP_PAGE_SHIFT] = 0; 203 204 flushd(&asc->gatt->ag_virtual[off >> AGP_PAGE_SHIFT]); 205 } 206 207 #undef flushd 208 209 void 210 agp_apple_flush_tlb(void *v) 211 { 212 struct agp_apple_softc *asc = v; 213 214 if (asc->asc_flags & AGP_APPLE_ISU3) { 215 pci_conf_write(asc->asc_pc, asc->asc_tag, AGP_APPLE_GARTCTRL, 216 AGP_APPLE_GART_ENABLE | AGP_APPLE_GART_PERFRD | 217 AGP_APPLE_GART_INVALIDATE); 218 pci_conf_write(asc->asc_pc, asc->asc_tag, AGP_APPLE_GARTCTRL, 219 AGP_APPLE_GART_ENABLE | AGP_APPLE_GART_PERFRD); 220 } else { 221 pci_conf_write(asc->asc_pc, asc->asc_tag, AGP_APPLE_GARTCTRL, 222 AGP_APPLE_GART_ENABLE | AGP_APPLE_GART_INVALIDATE); 223 pci_conf_write(asc->asc_pc, asc->asc_tag, AGP_APPLE_GARTCTRL, 224 AGP_APPLE_GART_ENABLE); 225 } 226 } 227 228 int 229 agp_apple_enable(void *v, uint32_t mode) 230 { 231 struct agp_apple_softc *asc = v; 232 233 if ((asc->asc_flags & AGP_APPLE_ISU3) == 0) { 234 /* 235 * GART invalidate/SBA reset? Linux and Darwin do something 236 * similar and it prevents GPU lockups with KMS. 237 */ 238 pci_conf_write(asc->asc_pc, asc->asc_tag, AGP_APPLE_GARTCTRL, 239 AGP_APPLE_GART_ENABLE | AGP_APPLE_GART_2XRESET); 240 pci_conf_write(asc->asc_pc, asc->asc_tag, 241 AGP_APPLE_GARTCTRL, AGP_APPLE_GART_ENABLE); 242 } 243 244 return (agp_generic_enable(asc->agpdev, mode)); 245 } 246