1 /*- 2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org> 3 * Copyright (c) 2000, Michael Smith <msmith@freebsd.org> 4 * Copyright (c) 2000, BSDi 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 unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/bus.h> 35 #include <sys/kernel.h> 36 #include <sys/malloc.h> 37 #include <sys/module.h> 38 39 #include <contrib/dev/acpica/acpi.h> 40 #include <dev/acpica/acpivar.h> 41 42 #include <sys/pciio.h> 43 #include <dev/pci/pcireg.h> 44 #include <dev/pci/pcivar.h> 45 #include <dev/pci/pci_private.h> 46 47 #include "pcib_if.h" 48 #include "pci_if.h" 49 50 /* Hooks for the ACPI CA debugging infrastructure. */ 51 #define _COMPONENT ACPI_BUS 52 ACPI_MODULE_NAME("PCI") 53 54 struct acpi_pci_devinfo { 55 struct pci_devinfo ap_dinfo; 56 ACPI_HANDLE ap_handle; 57 int ap_flags; 58 }; 59 60 ACPI_SERIAL_DECL(pci_powerstate, "ACPI PCI power methods"); 61 62 /* Be sure that ACPI and PCI power states are equivalent. */ 63 CTASSERT(ACPI_STATE_D0 == PCI_POWERSTATE_D0); 64 CTASSERT(ACPI_STATE_D1 == PCI_POWERSTATE_D1); 65 CTASSERT(ACPI_STATE_D2 == PCI_POWERSTATE_D2); 66 CTASSERT(ACPI_STATE_D3 == PCI_POWERSTATE_D3); 67 68 static int acpi_pci_attach(device_t dev); 69 static int acpi_pci_child_location_str_method(device_t cbdev, 70 device_t child, char *buf, size_t buflen); 71 static int acpi_pci_probe(device_t dev); 72 static int acpi_pci_read_ivar(device_t dev, device_t child, int which, 73 uintptr_t *result); 74 static int acpi_pci_write_ivar(device_t dev, device_t child, int which, 75 uintptr_t value); 76 static ACPI_STATUS acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, 77 void *context, void **status); 78 static int acpi_pci_set_powerstate_method(device_t dev, device_t child, 79 int state); 80 static void acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child); 81 82 static device_method_t acpi_pci_methods[] = { 83 /* Device interface */ 84 DEVMETHOD(device_probe, acpi_pci_probe), 85 DEVMETHOD(device_attach, acpi_pci_attach), 86 DEVMETHOD(device_shutdown, bus_generic_shutdown), 87 DEVMETHOD(device_suspend, pci_suspend), 88 DEVMETHOD(device_resume, pci_resume), 89 90 /* Bus interface */ 91 DEVMETHOD(bus_print_child, pci_print_child), 92 DEVMETHOD(bus_probe_nomatch, pci_probe_nomatch), 93 DEVMETHOD(bus_read_ivar, acpi_pci_read_ivar), 94 DEVMETHOD(bus_write_ivar, acpi_pci_write_ivar), 95 DEVMETHOD(bus_driver_added, pci_driver_added), 96 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 97 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 98 99 DEVMETHOD(bus_get_resource_list,pci_get_resource_list), 100 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 101 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 102 DEVMETHOD(bus_delete_resource, pci_delete_resource), 103 DEVMETHOD(bus_alloc_resource, pci_alloc_resource), 104 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource), 105 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 106 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 107 DEVMETHOD(bus_child_pnpinfo_str, pci_child_pnpinfo_str_method), 108 DEVMETHOD(bus_child_location_str, acpi_pci_child_location_str_method), 109 110 /* PCI interface */ 111 DEVMETHOD(pci_read_config, pci_read_config_method), 112 DEVMETHOD(pci_write_config, pci_write_config_method), 113 DEVMETHOD(pci_enable_busmaster, pci_enable_busmaster_method), 114 DEVMETHOD(pci_disable_busmaster, pci_disable_busmaster_method), 115 DEVMETHOD(pci_enable_io, pci_enable_io_method), 116 DEVMETHOD(pci_disable_io, pci_disable_io_method), 117 DEVMETHOD(pci_get_powerstate, pci_get_powerstate_method), 118 DEVMETHOD(pci_set_powerstate, acpi_pci_set_powerstate_method), 119 DEVMETHOD(pci_assign_interrupt, pci_assign_interrupt_method), 120 DEVMETHOD(pci_find_extcap, pci_find_extcap_method), 121 122 { 0, 0 } 123 }; 124 125 static driver_t acpi_pci_driver = { 126 "pci", 127 acpi_pci_methods, 128 0, /* no softc */ 129 }; 130 131 DRIVER_MODULE(acpi_pci, pcib, acpi_pci_driver, pci_devclass, 0, 0); 132 MODULE_DEPEND(acpi_pci, acpi, 1, 1, 1); 133 MODULE_DEPEND(acpi_pci, pci, 1, 1, 1); 134 MODULE_VERSION(acpi_pci, 1); 135 136 static int 137 acpi_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 138 { 139 struct acpi_pci_devinfo *dinfo; 140 141 dinfo = device_get_ivars(child); 142 switch (which) { 143 case ACPI_IVAR_HANDLE: 144 *result = (uintptr_t)dinfo->ap_handle; 145 return (0); 146 case ACPI_IVAR_FLAGS: 147 *result = (uintptr_t)dinfo->ap_flags; 148 return (0); 149 } 150 return (pci_read_ivar(dev, child, which, result)); 151 } 152 153 static int 154 acpi_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 155 { 156 struct acpi_pci_devinfo *dinfo; 157 158 dinfo = device_get_ivars(child); 159 switch (which) { 160 case ACPI_IVAR_HANDLE: 161 dinfo->ap_handle = (ACPI_HANDLE)value; 162 return (0); 163 case ACPI_IVAR_FLAGS: 164 dinfo->ap_flags = (int)value; 165 return (0); 166 } 167 return (pci_write_ivar(dev, child, which, value)); 168 } 169 170 static int 171 acpi_pci_child_location_str_method(device_t cbdev, device_t child, char *buf, 172 size_t buflen) 173 { 174 struct acpi_pci_devinfo *dinfo = device_get_ivars(child); 175 176 pci_child_location_str_method(cbdev, child, buf, buflen); 177 178 if (dinfo->ap_handle) { 179 strlcat(buf, " handle=", buflen); 180 strlcat(buf, acpi_name(dinfo->ap_handle), buflen); 181 } 182 return (0); 183 } 184 185 /* 186 * PCI power manangement 187 */ 188 static int 189 acpi_pci_set_powerstate_method(device_t dev, device_t child, int state) 190 { 191 ACPI_HANDLE h; 192 ACPI_STATUS status; 193 int old_state, error; 194 195 error = 0; 196 if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3) 197 return (EINVAL); 198 199 /* 200 * We set the state using PCI Power Management outside of setting 201 * the ACPI state. This means that when powering down a device, we 202 * first shut it down using PCI, and then using ACPI, which lets ACPI 203 * try to power down any Power Resources that are now no longer used. 204 * When powering up a device, we let ACPI set the state first so that 205 * it can enable any needed Power Resources before changing the PCI 206 * power state. 207 */ 208 ACPI_SERIAL_BEGIN(pci_powerstate); 209 old_state = pci_get_powerstate(child); 210 if (old_state < state) { 211 error = pci_set_powerstate_method(dev, child, state); 212 if (error) 213 goto out; 214 } 215 h = acpi_get_handle(child); 216 status = acpi_pwr_switch_consumer(h, state); 217 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) 218 device_printf(dev, 219 "Failed to set ACPI power state D%d on %s: %s\n", 220 state, acpi_name(h), AcpiFormatException(status)); 221 if (old_state > state) 222 error = pci_set_powerstate_method(dev, child, state); 223 224 out: 225 ACPI_SERIAL_END(pci_powerstate); 226 return (error); 227 } 228 229 static void 230 acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child) 231 { 232 ACPI_STATUS status; 233 device_t child; 234 235 /* 236 * Lookup and remove the unused device that acpi0 creates when it walks 237 * the namespace creating devices. 238 */ 239 child = acpi_get_device(handle); 240 if (child != NULL) { 241 KASSERT(!device_is_alive(child), ("%s: deleting alive child %s", 242 __func__, device_get_nameunit(child))); 243 KASSERT(device_get_parent(child) == 244 devclass_get_device(devclass_find("acpi"), 0), 245 ("%s: child (%s)'s parent is not acpi0", __func__, 246 acpi_name(handle))); 247 device_delete_child(device_get_parent(child), child); 248 } 249 250 /* 251 * Update ACPI-CA to use the PCI enumerated device_t for this handle. 252 */ 253 status = AcpiDetachData(handle, acpi_fake_objhandler); 254 if (ACPI_FAILURE(status)) 255 printf("WARNING: Unable to detach object data from %s - %s\n", 256 acpi_name(handle), AcpiFormatException(status)); 257 status = AcpiAttachData(handle, acpi_fake_objhandler, pci_child); 258 if (ACPI_FAILURE(status)) 259 printf("WARNING: Unable to attach object data to %s - %s\n", 260 acpi_name(handle), AcpiFormatException(status)); 261 } 262 263 static ACPI_STATUS 264 acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, void *context, 265 void **status) 266 { 267 struct acpi_pci_devinfo *dinfo; 268 device_t *devlist; 269 int devcount, i, func, slot; 270 UINT32 address; 271 272 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 273 274 if (ACPI_FAILURE(acpi_GetInteger(handle, "_ADR", &address))) 275 return_ACPI_STATUS (AE_OK); 276 slot = ACPI_ADR_PCI_SLOT(address); 277 func = ACPI_ADR_PCI_FUNC(address); 278 if (device_get_children((device_t)context, &devlist, &devcount) != 0) 279 return_ACPI_STATUS (AE_OK); 280 for (i = 0; i < devcount; i++) { 281 dinfo = device_get_ivars(devlist[i]); 282 if (dinfo->ap_dinfo.cfg.func == func && 283 dinfo->ap_dinfo.cfg.slot == slot) { 284 dinfo->ap_handle = handle; 285 acpi_pci_update_device(handle, devlist[i]); 286 break; 287 } 288 } 289 free(devlist, M_TEMP); 290 return_ACPI_STATUS (AE_OK); 291 } 292 293 static int 294 acpi_pci_probe(device_t dev) 295 { 296 297 if (pcib_get_bus(dev) < 0) 298 return (ENXIO); 299 if (acpi_get_handle(dev) == NULL) 300 return (ENXIO); 301 device_set_desc(dev, "ACPI PCI bus"); 302 return (0); 303 } 304 305 static int 306 acpi_pci_attach(device_t dev) 307 { 308 int busno; 309 310 /* 311 * Since there can be multiple independantly numbered PCI 312 * busses on some large alpha systems, we can't use the unit 313 * number to decide what bus we are probing. We ask the parent 314 * pcib what our bus number is. 315 */ 316 busno = pcib_get_bus(dev); 317 if (bootverbose) 318 device_printf(dev, "physical bus=%d\n", busno); 319 320 /* 321 * First, PCI devices are added as in the normal PCI bus driver. 322 * Afterwards, the ACPI namespace under the bridge driver is 323 * walked to save ACPI handles to all the devices that appear in 324 * the ACPI namespace as immediate descendants of the bridge. 325 * 326 * XXX: Sometimes PCI devices show up in the ACPI namespace that 327 * pci_add_children() doesn't find. We currently just ignore 328 * these devices. 329 */ 330 pci_add_children(dev, busno, sizeof(struct acpi_pci_devinfo)); 331 AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1, 332 acpi_pci_save_handle, dev, NULL); 333 334 return (bus_generic_attach(dev)); 335 } 336