acpi_pcib_acpi.c revision 223424
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: head/sys/dev/acpica/acpi_pcib_acpi.c 223424 2011-06-22 16:15:15Z jhb $"); 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/rman.h> 38#include <sys/sysctl.h> 39 40#include <contrib/dev/acpica/include/acpi.h> 41#include <contrib/dev/acpica/include/accommon.h> 42 43#include <dev/acpica/acpivar.h> 44 45#include <machine/pci_cfgreg.h> 46#include <dev/pci/pcivar.h> 47#include <dev/pci/pcib_private.h> 48#include "pcib_if.h" 49 50#include <dev/acpica/acpi_pcibvar.h> 51 52/* Hooks for the ACPI CA debugging infrastructure. */ 53#define _COMPONENT ACPI_BUS 54ACPI_MODULE_NAME("PCI_ACPI") 55 56struct acpi_hpcib_softc { 57 device_t ap_dev; 58 ACPI_HANDLE ap_handle; 59 int ap_flags; 60 61 int ap_segment; /* analagous to Alpha 'hose' */ 62 int ap_bus; /* bios-assigned bus number */ 63 64 ACPI_BUFFER ap_prt; /* interrupt routing table */ 65}; 66 67static int acpi_pcib_acpi_probe(device_t bus); 68static int acpi_pcib_acpi_attach(device_t bus); 69static int acpi_pcib_read_ivar(device_t dev, device_t child, 70 int which, uintptr_t *result); 71static int acpi_pcib_write_ivar(device_t dev, device_t child, 72 int which, uintptr_t value); 73static uint32_t acpi_pcib_read_config(device_t dev, u_int bus, 74 u_int slot, u_int func, u_int reg, int bytes); 75static void acpi_pcib_write_config(device_t dev, u_int bus, 76 u_int slot, u_int func, u_int reg, uint32_t data, 77 int bytes); 78static int acpi_pcib_acpi_route_interrupt(device_t pcib, 79 device_t dev, int pin); 80static int acpi_pcib_alloc_msi(device_t pcib, device_t dev, 81 int count, int maxcount, int *irqs); 82static int acpi_pcib_map_msi(device_t pcib, device_t dev, 83 int irq, uint64_t *addr, uint32_t *data); 84static int acpi_pcib_alloc_msix(device_t pcib, device_t dev, 85 int *irq); 86static struct resource *acpi_pcib_acpi_alloc_resource(device_t dev, 87 device_t child, int type, int *rid, 88 u_long start, u_long end, u_long count, 89 u_int flags); 90 91static device_method_t acpi_pcib_acpi_methods[] = { 92 /* Device interface */ 93 DEVMETHOD(device_probe, acpi_pcib_acpi_probe), 94 DEVMETHOD(device_attach, acpi_pcib_acpi_attach), 95 DEVMETHOD(device_shutdown, bus_generic_shutdown), 96 DEVMETHOD(device_suspend, bus_generic_suspend), 97 DEVMETHOD(device_resume, bus_generic_resume), 98 99 /* Bus interface */ 100 DEVMETHOD(bus_print_child, bus_generic_print_child), 101 DEVMETHOD(bus_read_ivar, acpi_pcib_read_ivar), 102 DEVMETHOD(bus_write_ivar, acpi_pcib_write_ivar), 103 DEVMETHOD(bus_alloc_resource, acpi_pcib_acpi_alloc_resource), 104 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource), 105 DEVMETHOD(bus_release_resource, bus_generic_release_resource), 106 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 107 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 108 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 109 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 110 111 /* pcib interface */ 112 DEVMETHOD(pcib_maxslots, pcib_maxslots), 113 DEVMETHOD(pcib_read_config, acpi_pcib_read_config), 114 DEVMETHOD(pcib_write_config, acpi_pcib_write_config), 115 DEVMETHOD(pcib_route_interrupt, acpi_pcib_acpi_route_interrupt), 116 DEVMETHOD(pcib_alloc_msi, acpi_pcib_alloc_msi), 117 DEVMETHOD(pcib_release_msi, pcib_release_msi), 118 DEVMETHOD(pcib_alloc_msix, acpi_pcib_alloc_msix), 119 DEVMETHOD(pcib_release_msix, pcib_release_msix), 120 DEVMETHOD(pcib_map_msi, acpi_pcib_map_msi), 121 DEVMETHOD(pcib_power_for_sleep, acpi_pcib_power_for_sleep), 122 123 {0, 0} 124}; 125 126static devclass_t pcib_devclass; 127 128DEFINE_CLASS_0(pcib, acpi_pcib_acpi_driver, acpi_pcib_acpi_methods, 129 sizeof(struct acpi_hpcib_softc)); 130DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, pcib_devclass, 0, 0); 131MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1); 132 133static int 134acpi_pcib_acpi_probe(device_t dev) 135{ 136 ACPI_DEVICE_INFO *devinfo; 137 ACPI_HANDLE h; 138 int root; 139 140 if (acpi_disabled("pcib") || (h = acpi_get_handle(dev)) == NULL || 141 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 142 return (ENXIO); 143 root = (devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0; 144 AcpiOsFree(devinfo); 145 if (!root || pci_cfgregopen() == 0) 146 return (ENXIO); 147 148 device_set_desc(dev, "ACPI Host-PCI bridge"); 149 return (0); 150} 151 152static int 153acpi_pcib_acpi_attach(device_t dev) 154{ 155 struct acpi_hpcib_softc *sc; 156 ACPI_STATUS status; 157 static int bus0_seen = 0; 158 u_int addr, slot, func, busok; 159 uint8_t busno; 160 161 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 162 163 sc = device_get_softc(dev); 164 sc->ap_dev = dev; 165 sc->ap_handle = acpi_get_handle(dev); 166 167 /* 168 * Get our segment number by evaluating _SEG 169 * It's OK for this to not exist. 170 */ 171 status = acpi_GetInteger(sc->ap_handle, "_SEG", &sc->ap_segment); 172 if (ACPI_FAILURE(status)) { 173 if (status != AE_NOT_FOUND) { 174 device_printf(dev, "could not evaluate _SEG - %s\n", 175 AcpiFormatException(status)); 176 return_VALUE (ENXIO); 177 } 178 /* If it's not found, assume 0. */ 179 sc->ap_segment = 0; 180 } 181 182 /* 183 * Get our base bus number by evaluating _BBN. 184 * If this doesn't work, we assume we're bus number 0. 185 * 186 * XXX note that it may also not exist in the case where we are 187 * meant to use a private configuration space mechanism for this bus, 188 * so we should dig out our resources and check to see if we have 189 * anything like that. How do we do this? 190 * XXX If we have the requisite information, and if we don't think the 191 * default PCI configuration space handlers can deal with this bus, 192 * we should attach our own handler. 193 * XXX invoke _REG on this for the PCI config space address space? 194 * XXX It seems many BIOS's with multiple Host-PCI bridges do not set 195 * _BBN correctly. They set _BBN to zero for all bridges. Thus, 196 * if _BBN is zero and PCI bus 0 already exists, we try to read our 197 * bus number from the configuration registers at address _ADR. 198 * We only do this for domain/segment 0 in the hopes that this is 199 * only needed for old single-domain machines. 200 */ 201 status = acpi_GetInteger(sc->ap_handle, "_BBN", &sc->ap_bus); 202 if (ACPI_FAILURE(status)) { 203 if (status != AE_NOT_FOUND) { 204 device_printf(dev, "could not evaluate _BBN - %s\n", 205 AcpiFormatException(status)); 206 return_VALUE (ENXIO); 207 } else { 208 /* If it's not found, assume 0. */ 209 sc->ap_bus = 0; 210 } 211 } 212 213 /* 214 * If this is segment 0, the bus is zero, and PCI bus 0 already 215 * exists, read the bus number via PCI config space. 216 */ 217 busok = 1; 218 if (sc->ap_segment == 0 && sc->ap_bus == 0 && bus0_seen) { 219 busok = 0; 220 status = acpi_GetInteger(sc->ap_handle, "_ADR", &addr); 221 if (ACPI_FAILURE(status)) { 222 if (status != AE_NOT_FOUND) { 223 device_printf(dev, "could not evaluate _ADR - %s\n", 224 AcpiFormatException(status)); 225 return_VALUE (ENXIO); 226 } else 227 device_printf(dev, "couldn't find _ADR\n"); 228 } else { 229 /* XXX: We assume bus 0. */ 230 slot = ACPI_ADR_PCI_SLOT(addr); 231 func = ACPI_ADR_PCI_FUNC(addr); 232 if (bootverbose) 233 device_printf(dev, "reading config registers from 0:%d:%d\n", 234 slot, func); 235 if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0) 236 device_printf(dev, "couldn't read bus number from cfg space\n"); 237 else { 238 sc->ap_bus = busno; 239 busok = 1; 240 } 241 } 242 } 243 244 /* 245 * If nothing else worked, hope that ACPI at least lays out the 246 * host-PCI bridges in order and that as a result our unit number 247 * is actually our bus number. There are several reasons this 248 * might not be true. 249 */ 250 if (busok == 0) { 251 sc->ap_bus = device_get_unit(dev); 252 device_printf(dev, "trying bus number %d\n", sc->ap_bus); 253 } 254 255 /* If this is bus 0 on segment 0, note that it has been seen already. */ 256 if (sc->ap_segment == 0 && sc->ap_bus == 0) 257 bus0_seen = 1; 258 259 return (acpi_pcib_attach(dev, &sc->ap_prt, sc->ap_bus)); 260} 261 262/* 263 * Support for standard PCI bridge ivars. 264 */ 265static int 266acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 267{ 268 struct acpi_hpcib_softc *sc = device_get_softc(dev); 269 270 switch (which) { 271 case PCIB_IVAR_DOMAIN: 272 *result = sc->ap_segment; 273 return (0); 274 case PCIB_IVAR_BUS: 275 *result = sc->ap_bus; 276 return (0); 277 case ACPI_IVAR_HANDLE: 278 *result = (uintptr_t)sc->ap_handle; 279 return (0); 280 case ACPI_IVAR_FLAGS: 281 *result = (uintptr_t)sc->ap_flags; 282 return (0); 283 } 284 return (ENOENT); 285} 286 287static int 288acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 289{ 290 struct acpi_hpcib_softc *sc = device_get_softc(dev); 291 292 switch (which) { 293 case PCIB_IVAR_DOMAIN: 294 return (EINVAL); 295 case PCIB_IVAR_BUS: 296 sc->ap_bus = value; 297 return (0); 298 case ACPI_IVAR_HANDLE: 299 sc->ap_handle = (ACPI_HANDLE)value; 300 return (0); 301 case ACPI_IVAR_FLAGS: 302 sc->ap_flags = (int)value; 303 return (0); 304 } 305 return (ENOENT); 306} 307 308static uint32_t 309acpi_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func, 310 u_int reg, int bytes) 311{ 312 return (pci_cfgregread(bus, slot, func, reg, bytes)); 313} 314 315static void 316acpi_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func, 317 u_int reg, uint32_t data, int bytes) 318{ 319 pci_cfgregwrite(bus, slot, func, reg, data, bytes); 320} 321 322static int 323acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin) 324{ 325 struct acpi_hpcib_softc *sc = device_get_softc(pcib); 326 327 return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt)); 328} 329 330static int 331acpi_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, 332 int *irqs) 333{ 334 device_t bus; 335 336 bus = device_get_parent(pcib); 337 return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount, 338 irqs)); 339} 340 341static int 342acpi_pcib_alloc_msix(device_t pcib, device_t dev, int *irq) 343{ 344 device_t bus; 345 346 bus = device_get_parent(pcib); 347 return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq)); 348} 349 350static int 351acpi_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, 352 uint32_t *data) 353{ 354 device_t bus; 355 356 bus = device_get_parent(pcib); 357 return (PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data)); 358} 359 360struct resource * 361acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid, 362 u_long start, u_long end, u_long count, u_int flags) 363{ 364 365#if defined(__i386__) || defined(__amd64__) 366 start = hostb_alloc_start(type, start, end, count); 367#endif 368 return (bus_generic_alloc_resource(dev, child, type, rid, start, end, 369 count, flags)); 370} 371