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 "acpi.h" 40 41 #include <dev/acpica/acpivar.h> 42 43 #include <sys/pciio.h> 44 #include <dev/pci/pcireg.h> 45 #include <dev/pci/pcivar.h> 46 #include <dev/pci/pci_private.h> 47 48 #include "pcib_if.h" 49 #include "pci_if.h" 50 51 /* 52 * Hooks for the ACPI CA debugging infrastructure 53 */ 54 #define _COMPONENT ACPI_BUS 55 ACPI_MODULE_NAME("PCI") 56 57 struct acpi_pci_devinfo { 58 struct pci_devinfo ap_dinfo; 59 ACPI_HANDLE ap_handle; 60 }; 61 62 static int acpi_pci_probe(device_t dev); 63 static int acpi_pci_attach(device_t dev); 64 static int acpi_pci_read_ivar(device_t dev, device_t child, int which, 65 uintptr_t *result); 66 static int acpi_pci_child_location_str_method(device_t cbdev, 67 device_t child, char *buf, size_t buflen); 68 69 70 static int acpi_pci_set_powerstate_method(device_t dev, device_t child, 71 int state); 72 static ACPI_STATUS acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, 73 void *context, void **status); 74 75 static device_method_t acpi_pci_methods[] = { 76 /* Device interface */ 77 DEVMETHOD(device_probe, acpi_pci_probe), 78 DEVMETHOD(device_attach, acpi_pci_attach), 79 DEVMETHOD(device_shutdown, bus_generic_shutdown), 80 DEVMETHOD(device_suspend, pci_suspend), 81 DEVMETHOD(device_resume, pci_resume), 82 83 /* Bus interface */ 84 DEVMETHOD(bus_print_child, pci_print_child), 85 DEVMETHOD(bus_probe_nomatch, pci_probe_nomatch), 86 DEVMETHOD(bus_read_ivar, acpi_pci_read_ivar), 87 DEVMETHOD(bus_write_ivar, pci_write_ivar), 88 DEVMETHOD(bus_driver_added, pci_driver_added), 89 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 90 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 91 92 DEVMETHOD(bus_get_resource_list,pci_get_resource_list), 93 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 94 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 95 DEVMETHOD(bus_delete_resource, pci_delete_resource), 96 DEVMETHOD(bus_alloc_resource, pci_alloc_resource), 97 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource), 98 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 99 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 100 DEVMETHOD(bus_child_pnpinfo_str, pci_child_pnpinfo_str_method), 101 DEVMETHOD(bus_child_location_str, acpi_pci_child_location_str_method), 102 103 /* PCI interface */ 104 DEVMETHOD(pci_read_config, pci_read_config_method), 105 DEVMETHOD(pci_write_config, pci_write_config_method), 106 DEVMETHOD(pci_enable_busmaster, pci_enable_busmaster_method), 107 DEVMETHOD(pci_disable_busmaster, pci_disable_busmaster_method), 108 DEVMETHOD(pci_enable_io, pci_enable_io_method), 109 DEVMETHOD(pci_disable_io, pci_disable_io_method), 110 DEVMETHOD(pci_get_powerstate, pci_get_powerstate_method), 111 DEVMETHOD(pci_set_powerstate, acpi_pci_set_powerstate_method), 112 DEVMETHOD(pci_assign_interrupt, pci_assign_interrupt_method), 113 114 { 0, 0 } 115 }; 116 117 static driver_t acpi_pci_driver = { 118 "pci", 119 acpi_pci_methods, 120 0, /* no softc */ 121 }; 122 123 DRIVER_MODULE(acpi_pci, pcib, acpi_pci_driver, pci_devclass, 0, 0); 124 MODULE_DEPEND(acpi_pci, acpi, 1, 1, 1); 125 MODULE_DEPEND(acpi_pci, pci, 1, 1, 1); 126 MODULE_VERSION(acpi_pci, 1); 127 128 static int 129 acpi_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 130 { 131 struct acpi_pci_devinfo *dinfo; 132 133 switch (which) { 134 case ACPI_IVAR_HANDLE: 135 dinfo = device_get_ivars(child); 136 *result = (uintptr_t)dinfo->ap_handle; 137 return (0); 138 } 139 return (pci_read_ivar(dev, child, which, result)); 140 } 141 142 static int 143 acpi_pci_child_location_str_method(device_t cbdev, device_t child, char *buf, 144 size_t buflen) 145 { 146 struct acpi_pci_devinfo *dinfo = device_get_ivars(child); 147 148 pci_child_location_str_method(cbdev, child, buf, buflen); 149 150 if (dinfo->ap_handle) { 151 strlcat(buf, " path=", buflen); 152 strlcat(buf, acpi_name(dinfo->ap_handle), buflen); 153 } 154 return (0); 155 } 156 157 /* 158 * PCI power manangement 159 */ 160 static int 161 acpi_pci_set_powerstate_method(device_t dev, device_t child, int state) 162 { 163 ACPI_HANDLE h; 164 ACPI_STATUS status; 165 int acpi_state, old_state, error; 166 167 switch (state) { 168 case PCI_POWERSTATE_D0: 169 acpi_state = ACPI_STATE_D0; 170 break; 171 case PCI_POWERSTATE_D1: 172 acpi_state = ACPI_STATE_D1; 173 break; 174 case PCI_POWERSTATE_D2: 175 acpi_state = ACPI_STATE_D2; 176 break; 177 case PCI_POWERSTATE_D3: 178 acpi_state = ACPI_STATE_D3; 179 break; 180 default: 181 return (EINVAL); 182 } 183 184 /* 185 * We set the state using PCI Power Management outside of setting 186 * the ACPI state. This means that when powering down a device, we 187 * first shut it down using PCI, and then using ACPI, which lets ACPI 188 * try to power down any Power Resources that are now no longer used. 189 * When powering up a device, we let ACPI set the state first so that 190 * it can enable any needed Power Resources before changing the PCI 191 * power state. 192 */ 193 old_state = pci_get_powerstate(child); 194 if (old_state < state) { 195 error = pci_set_powerstate_method(dev, child, state); 196 if (error) 197 return (error); 198 } 199 h = acpi_get_handle(child); 200 if (h != NULL) { 201 status = acpi_pwr_switch_consumer(h, acpi_state); 202 if (ACPI_FAILURE(status)) 203 device_printf(dev, 204 "Failed to set ACPI power state D%d on %s: %s\n", 205 acpi_state, device_get_nameunit(child), 206 AcpiFormatException(status)); 207 } 208 if (old_state > state) 209 return (pci_set_powerstate_method(dev, child, state)); 210 else 211 return (0); 212 } 213 214 static ACPI_STATUS 215 acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, void *context, 216 void **status) 217 { 218 struct acpi_pci_devinfo *dinfo; 219 device_t *devlist; 220 int devcount, i, func, slot; 221 UINT32 address; 222 223 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 224 225 if (ACPI_FAILURE(acpi_GetInteger(handle, "_ADR", &address))) 226 return_ACPI_STATUS(AE_OK); 227 slot = address >> 16; 228 func = address & 0xffff; 229 if (device_get_children((device_t)context, &devlist, &devcount) != 0) 230 return_ACPI_STATUS(AE_OK); 231 for (i = 0; i < devcount; i++) { 232 dinfo = device_get_ivars(devlist[i]); 233 if (dinfo->ap_dinfo.cfg.func == func && 234 dinfo->ap_dinfo.cfg.slot == slot) { 235 dinfo->ap_handle = handle; 236 free(devlist, M_TEMP); 237 return_ACPI_STATUS(AE_OK); 238 } 239 } 240 free(devlist, M_TEMP); 241 return_ACPI_STATUS(AE_OK); 242 } 243 244 static int 245 acpi_pci_probe(device_t dev) 246 { 247 248 if (pcib_get_bus(dev) < 0) 249 return (ENXIO); 250 251 device_set_desc(dev, "ACPI PCI bus"); 252 253 if (acpi_get_handle(dev) == NULL) 254 return (ENXIO); 255 return (0); 256 } 257 258 static int 259 acpi_pci_attach(device_t dev) 260 { 261 int busno; 262 263 /* 264 * Since there can be multiple independantly numbered PCI 265 * busses on some large alpha systems, we can't use the unit 266 * number to decide what bus we are probing. We ask the parent 267 * pcib what our bus number is. 268 */ 269 busno = pcib_get_bus(dev); 270 if (bootverbose) 271 device_printf(dev, "physical bus=%d\n", busno); 272 273 /* 274 * First, PCI devices are added as in the normal PCI bus driver. 275 * Afterwards, the ACPI namespace under the bridge driver is 276 * walked to save ACPI handles to all the devices that appear in 277 * the ACPI namespace as immediate descendants of the bridge. 278 * 279 * XXX: Sometimes PCI devices show up in the ACPI namespace that 280 * pci_add_children() doesn't find. We currently just ignore 281 * these devices. 282 */ 283 pci_add_children(dev, busno, sizeof(struct acpi_pci_devinfo)); 284 (void) AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1, 285 acpi_pci_save_handle, dev, NULL); 286 287 return (bus_generic_attach(dev)); 288 } 289