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 * $FreeBSD: src/sys/dev/acpica/acpi_pcib_acpi.c,v 1.55.8.1 2009/04/15 03:14:26 kensmith Exp $ 28 */ 29 30 #include "opt_acpi.h" 31 #include <sys/param.h> 32 #include <sys/bus.h> 33 #include <sys/kernel.h> 34 #include <sys/malloc.h> 35 #include <sys/module.h> 36 #include <sys/rman.h> 37 #include <sys/sysctl.h> 38 39 #include "acpi.h" 40 #include <dev/acpica/acpivar.h> 41 42 #include <bus/pci/pci_cfgreg.h> 43 #include <bus/pci/pcivar.h> 44 #include <bus/pci/pcib_private.h> 45 #include "pcib_if.h" 46 47 #include <dev/acpica/acpi_pcibvar.h> 48 49 /* Hooks for the ACPICA 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, int cpuid); 79 static int acpi_pcib_map_msi(device_t pcib, device_t dev, 80 int irq, uint64_t *addr, uint32_t *data, int cpuid); 81 static int acpi_pcib_alloc_msix(device_t pcib, device_t dev, 82 int *irq, int cpuid); 83 static struct resource *acpi_pcib_acpi_alloc_resource(device_t dev, 84 device_t child, int type, int *rid, 85 u_long start, u_long end, u_long count, 86 u_int flags, int cpuid); 87 88 static device_method_t acpi_pcib_acpi_methods[] = { 89 /* Device interface */ 90 DEVMETHOD(device_probe, acpi_pcib_acpi_probe), 91 DEVMETHOD(device_attach, acpi_pcib_acpi_attach), 92 DEVMETHOD(device_shutdown, bus_generic_shutdown), 93 DEVMETHOD(device_suspend, bus_generic_suspend), 94 DEVMETHOD(device_resume, acpi_pcib_acpi_resume), 95 96 /* Bus interface */ 97 DEVMETHOD(bus_print_child, bus_generic_print_child), 98 DEVMETHOD(bus_read_ivar, acpi_pcib_read_ivar), 99 DEVMETHOD(bus_write_ivar, acpi_pcib_write_ivar), 100 DEVMETHOD(bus_alloc_resource, acpi_pcib_acpi_alloc_resource), 101 /* DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource), */ 102 DEVMETHOD(bus_release_resource, bus_generic_release_resource), 103 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 104 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 105 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 106 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 107 108 /* pcib interface */ 109 DEVMETHOD(pcib_maxslots, pcib_maxslots), 110 DEVMETHOD(pcib_read_config, acpi_pcib_read_config), 111 DEVMETHOD(pcib_write_config, acpi_pcib_write_config), 112 DEVMETHOD(pcib_route_interrupt, acpi_pcib_acpi_route_interrupt), 113 DEVMETHOD(pcib_alloc_msi, acpi_pcib_alloc_msi), 114 DEVMETHOD(pcib_release_msi, pcib_release_msi), 115 DEVMETHOD(pcib_alloc_msix, acpi_pcib_alloc_msix), 116 DEVMETHOD(pcib_release_msix, pcib_release_msix), 117 DEVMETHOD(pcib_map_msi, acpi_pcib_map_msi), 118 DEVMETHOD_END 119 }; 120 121 static devclass_t pcib_devclass; 122 123 DEFINE_CLASS_0(pcib, acpi_pcib_acpi_driver, acpi_pcib_acpi_methods, 124 sizeof(struct acpi_hpcib_softc)); 125 DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, pcib_devclass, NULL, NULL); 126 MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1); 127 128 static int 129 acpi_pcib_acpi_probe(device_t dev) 130 { 131 int error; 132 133 static char *pcib_ids[] = { "PNP0A03", "PNP0A08", NULL }; 134 135 if (acpi_disabled("pcib") || 136 ACPI_ID_PROBE(device_get_parent(dev), dev, pcib_ids) == NULL) 137 return (ENXIO); 138 139 error = acpi_pcib_probe(dev); 140 if (error) 141 return (error); 142 143 device_set_desc(dev, "ACPI Host-PCI bridge"); 144 return (0); 145 } 146 147 static int 148 acpi_pcib_acpi_attach(device_t dev) 149 { 150 struct acpi_hpcib_softc *sc; 151 ACPI_STATUS status; 152 static int bus0_seen = 0; 153 u_int addr, slot, func, busok; 154 uint8_t busno; 155 156 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 157 158 sc = device_get_softc(dev); 159 sc->ap_dev = dev; 160 sc->ap_handle = acpi_get_handle(dev); 161 162 /* 163 * Get our segment number by evaluating _SEG 164 * It's OK for this to not exist. 165 */ 166 status = acpi_GetInteger(sc->ap_handle, "_SEG", &sc->ap_segment); 167 if (ACPI_FAILURE(status)) { 168 if (status != AE_NOT_FOUND) { 169 device_printf(dev, "could not evaluate _SEG - %s\n", 170 AcpiFormatException(status)); 171 return_VALUE (ENXIO); 172 } 173 /* If it's not found, assume 0. */ 174 sc->ap_segment = 0; 175 } 176 177 /* 178 * Get our base bus number by evaluating _BBN. 179 * If this doesn't work, we assume we're bus number 0. 180 * 181 * XXX note that it may also not exist in the case where we are 182 * meant to use a private configuration space mechanism for this bus, 183 * so we should dig out our resources and check to see if we have 184 * anything like that. How do we do this? 185 * XXX If we have the requisite information, and if we don't think the 186 * default PCI configuration space handlers can deal with this bus, 187 * we should attach our own handler. 188 * XXX invoke _REG on this for the PCI config space address space? 189 * XXX It seems many BIOS's with multiple Host-PCI bridges do not set 190 * _BBN correctly. They set _BBN to zero for all bridges. Thus, 191 * if _BBN is zero and PCI bus 0 already exists, we try to read our 192 * bus number from the configuration registers at address _ADR. 193 * We only do this for domain/segment 0 in the hopes that this is 194 * only needed for old single-domain machines. 195 */ 196 status = acpi_GetInteger(sc->ap_handle, "_BBN", &sc->ap_bus); 197 if (ACPI_FAILURE(status)) { 198 if (status != AE_NOT_FOUND) { 199 device_printf(dev, "could not evaluate _BBN - %s\n", 200 AcpiFormatException(status)); 201 return_VALUE (ENXIO); 202 } else { 203 /* If it's not found, assume 0. */ 204 sc->ap_bus = 0; 205 } 206 } 207 208 /* 209 * If this is segment 0, the bus is zero, and PCI bus 0 already 210 * exists, read the bus number via PCI config space. 211 */ 212 busok = 1; 213 if (sc->ap_segment == 0 && sc->ap_bus == 0 && bus0_seen) { 214 busok = 0; 215 status = acpi_GetInteger(sc->ap_handle, "_ADR", &addr); 216 if (ACPI_FAILURE(status)) { 217 if (status != AE_NOT_FOUND) { 218 device_printf(dev, "could not evaluate _ADR - %s\n", 219 AcpiFormatException(status)); 220 return_VALUE (ENXIO); 221 } else 222 device_printf(dev, "couldn't find _ADR\n"); 223 } else { 224 /* XXX: We assume bus 0. */ 225 slot = ACPI_ADR_PCI_SLOT(addr); 226 func = ACPI_ADR_PCI_FUNC(addr); 227 if (bootverbose) 228 device_printf(dev, "reading config registers from 0:%d:%d\n", 229 slot, func); 230 if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0) 231 device_printf(dev, "couldn't read bus number from cfg space\n"); 232 else { 233 sc->ap_bus = busno; 234 busok = 1; 235 } 236 } 237 } 238 239 /* 240 * If nothing else worked, hope that ACPI at least lays out the 241 * host-PCI bridges in order and that as a result our unit number 242 * is actually our bus number. There are several reasons this 243 * might not be true. 244 */ 245 if (busok == 0) { 246 sc->ap_bus = device_get_unit(dev); 247 device_printf(dev, "trying bus number %d\n", sc->ap_bus); 248 } 249 250 /* If this is bus 0 on segment 0, note that it has been seen already. */ 251 if (sc->ap_segment == 0 && sc->ap_bus == 0) 252 bus0_seen = 1; 253 254 return (acpi_pcib_attach(dev, &sc->ap_prt, sc->ap_bus)); 255 } 256 257 static int 258 acpi_pcib_acpi_resume(device_t dev) 259 { 260 261 return (acpi_pcib_resume(dev)); 262 } 263 264 /* 265 * Support for standard PCI bridge ivars. 266 */ 267 static int 268 acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 269 { 270 struct acpi_hpcib_softc *sc = device_get_softc(dev); 271 272 switch (which) { 273 case PCIB_IVAR_DOMAIN: 274 *result = sc->ap_segment; 275 return (0); 276 case PCIB_IVAR_BUS: 277 *result = sc->ap_bus; 278 return (0); 279 case ACPI_IVAR_HANDLE: 280 *result = (uintptr_t)sc->ap_handle; 281 return (0); 282 case ACPI_IVAR_FLAGS: 283 *result = (uintptr_t)sc->ap_flags; 284 return (0); 285 } 286 return (ENOENT); 287 } 288 289 static int 290 acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 291 { 292 struct acpi_hpcib_softc *sc = device_get_softc(dev); 293 294 switch (which) { 295 case PCIB_IVAR_DOMAIN: 296 return (EINVAL); 297 case PCIB_IVAR_BUS: 298 sc->ap_bus = value; 299 return (0); 300 case ACPI_IVAR_HANDLE: 301 sc->ap_handle = (ACPI_HANDLE)value; 302 return (0); 303 case ACPI_IVAR_FLAGS: 304 sc->ap_flags = (int)value; 305 return (0); 306 } 307 return (ENOENT); 308 } 309 310 static uint32_t 311 acpi_pcib_read_config(device_t dev, int bus, int slot, int func, int reg, 312 int bytes) 313 { 314 return (pci_cfgregread(bus, slot, func, reg, bytes)); 315 } 316 317 static void 318 acpi_pcib_write_config(device_t dev, int bus, int slot, int func, int reg, 319 uint32_t data, int bytes) 320 { 321 pci_cfgregwrite(bus, slot, func, reg, data, bytes); 322 } 323 324 static int 325 acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin) 326 { 327 struct acpi_hpcib_softc *sc = device_get_softc(pcib); 328 329 return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt)); 330 } 331 332 static int 333 acpi_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, 334 int *irqs, int cpuid) 335 { 336 device_t bus; 337 338 bus = device_get_parent(pcib); 339 return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount, 340 irqs, cpuid)); 341 } 342 343 static int 344 acpi_pcib_alloc_msix(device_t pcib, device_t dev, int *irq, int cpuid) 345 { 346 device_t bus; 347 348 bus = device_get_parent(pcib); 349 return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq, cpuid)); 350 } 351 352 static int 353 acpi_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, 354 uint32_t *data, int cpuid) 355 { 356 device_t bus; 357 358 bus = device_get_parent(pcib); 359 return (PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data, 360 cpuid)); 361 } 362 363 static u_long acpi_host_mem_start = 0x80000000; 364 TUNABLE_ULONG("hw.acpi.host_mem_start", &acpi_host_mem_start); 365 366 static struct resource * 367 acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid, 368 u_long start, u_long end, u_long count, u_int flags, int cpuid) 369 { 370 /* 371 * If no memory preference is given, use upper 32MB slot most 372 * bioses use for their memory window. Typically other bridges 373 * before us get in the way to assert their preferences on memory. 374 * Hardcoding like this sucks, so a more MD/MI way needs to be 375 * found to do it. This is typically only used on older laptops 376 * that don't have pci busses behind pci bridge, so assuming > 32MB 377 * is likely OK. 378 * 379 * PCI-PCI bridges may allocate smaller ranges for their windows, 380 * but the heuristics here should apply to those, so we allow 381 * several different end addresses. 382 */ 383 if (type == SYS_RES_MEMORY && start == 0UL && (end == ~0UL || 384 end == 0xffffffff)) 385 start = acpi_host_mem_start; 386 if (type == SYS_RES_IOPORT && start == 0UL && (end == ~0UL || 387 end == 0xffff || end == 0xffffffff)) 388 start = 0x1000; 389 return (bus_generic_alloc_resource(dev, child, type, rid, start, end, 390 count, flags, cpuid)); 391 } 392