1 /*- 2 * Copyright (c) 2000 Michael Smith 3 * Copyright (c) 2000 BSDi 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include "opt_acpi.h" 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 #include <sys/kernel.h> 35 #include <sys/malloc.h> 36 #include <sys/module.h> 37 #include <sys/sysctl.h> 38 39 #include <contrib/dev/acpica/acpi.h> 40 #include <dev/acpica/acpivar.h> 41 42 #include <machine/pci_cfgreg.h> 43 #include <dev/pci/pcivar.h> 44 #include <dev/pci/pcib_private.h> 45 #include "pcib_if.h" 46 47 #include <dev/acpica/acpi_pcibvar.h> 48 49 /* Hooks for the ACPI CA debugging infrastructure. */ 50 #define _COMPONENT ACPI_BUS 51 ACPI_MODULE_NAME("PCI_ACPI") 52 53 struct acpi_hpcib_softc { 54 device_t ap_dev; 55 ACPI_HANDLE ap_handle; 56 int ap_flags; 57 58 int ap_segment; /* analagous to Alpha 'hose' */ 59 int ap_bus; /* bios-assigned bus number */ 60 61 ACPI_BUFFER ap_prt; /* interrupt routing table */ 62 }; 63 64 static int acpi_pcib_acpi_probe(device_t bus); 65 static int acpi_pcib_acpi_attach(device_t bus); 66 static int acpi_pcib_acpi_resume(device_t bus); 67 static int acpi_pcib_read_ivar(device_t dev, device_t child, 68 int which, uintptr_t *result); 69 static int acpi_pcib_write_ivar(device_t dev, device_t child, 70 int which, uintptr_t value); 71 static uint32_t acpi_pcib_read_config(device_t dev, int bus, int slot, 72 int func, int reg, int bytes); 73 static void acpi_pcib_write_config(device_t dev, int bus, int slot, 74 int func, int reg, uint32_t data, int bytes); 75 static int acpi_pcib_acpi_route_interrupt(device_t pcib, 76 device_t dev, int pin); 77 static int acpi_pcib_alloc_msi(device_t pcib, device_t dev, 78 int count, int maxcount, int *irqs); 79 static int acpi_pcib_alloc_msix(device_t pcib, device_t dev, 80 int index, int *irq); 81 static struct resource *acpi_pcib_acpi_alloc_resource(device_t dev, 82 device_t child, int type, int *rid, 83 u_long start, u_long end, u_long count, 84 u_int flags); 85 86 static device_method_t acpi_pcib_acpi_methods[] = { 87 /* Device interface */ 88 DEVMETHOD(device_probe, acpi_pcib_acpi_probe), 89 DEVMETHOD(device_attach, acpi_pcib_acpi_attach), 90 DEVMETHOD(device_shutdown, bus_generic_shutdown), 91 DEVMETHOD(device_suspend, bus_generic_suspend), 92 DEVMETHOD(device_resume, acpi_pcib_acpi_resume), 93 94 /* Bus interface */ 95 DEVMETHOD(bus_print_child, bus_generic_print_child), 96 DEVMETHOD(bus_read_ivar, acpi_pcib_read_ivar), 97 DEVMETHOD(bus_write_ivar, acpi_pcib_write_ivar), 98 DEVMETHOD(bus_alloc_resource, acpi_pcib_acpi_alloc_resource), 99 DEVMETHOD(bus_release_resource, bus_generic_release_resource), 100 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 101 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 102 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 103 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 104 105 /* pcib interface */ 106 DEVMETHOD(pcib_maxslots, pcib_maxslots), 107 DEVMETHOD(pcib_read_config, acpi_pcib_read_config), 108 DEVMETHOD(pcib_write_config, acpi_pcib_write_config), 109 DEVMETHOD(pcib_route_interrupt, acpi_pcib_acpi_route_interrupt), 110 DEVMETHOD(pcib_alloc_msi, acpi_pcib_alloc_msi), 111 DEVMETHOD(pcib_release_msi, pcib_release_msi), 112 DEVMETHOD(pcib_alloc_msix, acpi_pcib_alloc_msix), 113 DEVMETHOD(pcib_release_msix, pcib_release_msix), 114 115 {0, 0} 116 }; 117 118 static devclass_t pcib_devclass; 119 120 DEFINE_CLASS_0(pcib, acpi_pcib_acpi_driver, acpi_pcib_acpi_methods, 121 sizeof(struct acpi_hpcib_softc)); 122 DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, pcib_devclass, 0, 0); 123 MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1); 124 125 static int 126 acpi_pcib_acpi_probe(device_t dev) 127 { 128 static char *pcib_ids[] = { "PNP0A03", NULL }; 129 130 if (acpi_disabled("pcib") || 131 ACPI_ID_PROBE(device_get_parent(dev), dev, pcib_ids) == NULL) 132 return (ENXIO); 133 134 if (pci_cfgregopen() == 0) 135 return (ENXIO); 136 device_set_desc(dev, "ACPI Host-PCI bridge"); 137 return (0); 138 } 139 140 static int 141 acpi_pcib_acpi_attach(device_t dev) 142 { 143 struct acpi_hpcib_softc *sc; 144 ACPI_STATUS status; 145 u_int addr, slot, func, busok; 146 uint8_t busno; 147 148 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 149 150 sc = device_get_softc(dev); 151 sc->ap_dev = dev; 152 sc->ap_handle = acpi_get_handle(dev); 153 154 /* 155 * Get our base bus number by evaluating _BBN. 156 * If this doesn't work, we assume we're bus number 0. 157 * 158 * XXX note that it may also not exist in the case where we are 159 * meant to use a private configuration space mechanism for this bus, 160 * so we should dig out our resources and check to see if we have 161 * anything like that. How do we do this? 162 * XXX If we have the requisite information, and if we don't think the 163 * default PCI configuration space handlers can deal with this bus, 164 * we should attach our own handler. 165 * XXX invoke _REG on this for the PCI config space address space? 166 * XXX It seems many BIOS's with multiple Host-PCI bridges do not set 167 * _BBN correctly. They set _BBN to zero for all bridges. Thus, 168 * if _BBN is zero and pcib0 already exists, we try to read our 169 * bus number from the configuration registers at address _ADR. 170 */ 171 status = acpi_GetInteger(sc->ap_handle, "_BBN", &sc->ap_bus); 172 if (ACPI_FAILURE(status)) { 173 if (status != AE_NOT_FOUND) { 174 device_printf(dev, "could not evaluate _BBN - %s\n", 175 AcpiFormatException(status)); 176 return_VALUE (ENXIO); 177 } else { 178 /* If it's not found, assume 0. */ 179 sc->ap_bus = 0; 180 } 181 } 182 183 /* 184 * If the bus is zero and pcib0 already exists, read the bus number 185 * via PCI config space. 186 */ 187 busok = 1; 188 if (sc->ap_bus == 0 && devclass_get_device(pcib_devclass, 0) != dev) { 189 busok = 0; 190 status = acpi_GetInteger(sc->ap_handle, "_ADR", &addr); 191 if (ACPI_FAILURE(status)) { 192 if (status != AE_NOT_FOUND) { 193 device_printf(dev, "could not evaluate _ADR - %s\n", 194 AcpiFormatException(status)); 195 return_VALUE (ENXIO); 196 } else 197 device_printf(dev, "couldn't find _ADR\n"); 198 } else { 199 /* XXX: We assume bus 0. */ 200 slot = ACPI_ADR_PCI_SLOT(addr); 201 func = ACPI_ADR_PCI_FUNC(addr); 202 if (bootverbose) 203 device_printf(dev, "reading config registers from 0:%d:%d\n", 204 slot, func); 205 if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0) 206 device_printf(dev, "couldn't read bus number from cfg space\n"); 207 else { 208 sc->ap_bus = busno; 209 busok = 1; 210 } 211 } 212 } 213 214 /* 215 * If nothing else worked, hope that ACPI at least lays out the 216 * host-PCI bridges in order and that as a result our unit number 217 * is actually our bus number. There are several reasons this 218 * might not be true. 219 */ 220 if (busok == 0) { 221 sc->ap_bus = device_get_unit(dev); 222 device_printf(dev, "trying bus number %d\n", sc->ap_bus); 223 } 224 225 /* 226 * Get our segment number by evaluating _SEG 227 * It's OK for this to not exist. 228 */ 229 status = acpi_GetInteger(sc->ap_handle, "_SEG", &sc->ap_segment); 230 if (ACPI_FAILURE(status)) { 231 if (status != AE_NOT_FOUND) { 232 device_printf(dev, "could not evaluate _SEG - %s\n", 233 AcpiFormatException(status)); 234 return_VALUE (ENXIO); 235 } 236 /* If it's not found, assume 0. */ 237 sc->ap_segment = 0; 238 } 239 240 return (acpi_pcib_attach(dev, &sc->ap_prt, sc->ap_bus)); 241 } 242 243 static int 244 acpi_pcib_acpi_resume(device_t dev) 245 { 246 247 return (acpi_pcib_resume(dev)); 248 } 249 250 /* 251 * Support for standard PCI bridge ivars. 252 */ 253 static int 254 acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 255 { 256 struct acpi_hpcib_softc *sc = device_get_softc(dev); 257 258 switch (which) { 259 case PCIB_IVAR_BUS: 260 *result = sc->ap_bus; 261 return (0); 262 case ACPI_IVAR_HANDLE: 263 *result = (uintptr_t)sc->ap_handle; 264 return (0); 265 case ACPI_IVAR_FLAGS: 266 *result = (uintptr_t)sc->ap_flags; 267 return (0); 268 } 269 return (ENOENT); 270 } 271 272 static int 273 acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 274 { 275 struct acpi_hpcib_softc *sc = device_get_softc(dev); 276 277 switch (which) { 278 case PCIB_IVAR_BUS: 279 sc->ap_bus = value; 280 return (0); 281 case ACPI_IVAR_HANDLE: 282 sc->ap_handle = (ACPI_HANDLE)value; 283 return (0); 284 case ACPI_IVAR_FLAGS: 285 sc->ap_flags = (int)value; 286 return (0); 287 } 288 return (ENOENT); 289 } 290 291 static uint32_t 292 acpi_pcib_read_config(device_t dev, int bus, int slot, int func, int reg, 293 int bytes) 294 { 295 return (pci_cfgregread(bus, slot, func, reg, bytes)); 296 } 297 298 static void 299 acpi_pcib_write_config(device_t dev, int bus, int slot, int func, int reg, 300 uint32_t data, int bytes) 301 { 302 pci_cfgregwrite(bus, slot, func, reg, data, bytes); 303 } 304 305 static int 306 acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin) 307 { 308 struct acpi_hpcib_softc *sc = device_get_softc(pcib); 309 310 return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt)); 311 } 312 313 static int 314 acpi_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, 315 int *irqs) 316 { 317 device_t bus; 318 319 bus = device_get_parent(pcib); 320 return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount, 321 irqs)); 322 } 323 324 static int 325 acpi_pcib_alloc_msix(device_t pcib, device_t dev, int index, int *irq) 326 { 327 device_t bus; 328 329 bus = device_get_parent(pcib); 330 return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, index, irq)); 331 } 332 333 static u_long acpi_host_mem_start = 0x80000000; 334 TUNABLE_ULONG("hw.acpi.host_mem_start", &acpi_host_mem_start); 335 336 struct resource * 337 acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid, 338 u_long start, u_long end, u_long count, u_int flags) 339 { 340 /* 341 * If no memory preference is given, use upper 32MB slot most 342 * bioses use for their memory window. Typically other bridges 343 * before us get in the way to assert their preferences on memory. 344 * Hardcoding like this sucks, so a more MD/MI way needs to be 345 * found to do it. This is typically only used on older laptops 346 * that don't have pci busses behind pci bridge, so assuming > 32MB 347 * is liekly OK. 348 */ 349 if (type == SYS_RES_MEMORY && start == 0UL && end == ~0UL) 350 start = acpi_host_mem_start; 351 if (type == SYS_RES_IOPORT && start == 0UL && end == ~0UL) 352 start = 0x1000; 353 return (bus_generic_alloc_resource(dev, child, type, rid, start, end, 354 count, flags)); 355 } 356