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 "opt_acpi.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/bus.h> 37 #include <sys/kernel.h> 38 #include <sys/malloc.h> 39 #include <sys/module.h> 40 41 #include <contrib/dev/acpica/include/acpi.h> 42 #include <contrib/dev/acpica/include/accommon.h> 43 44 #include <dev/acpica/acpivar.h> 45 #include <dev/acpica/acpi_pcivar.h> 46 47 #include <sys/pciio.h> 48 #include <dev/pci/pcireg.h> 49 #include <dev/pci/pcivar.h> 50 #include <dev/pci/pci_private.h> 51 52 #include "pcib_if.h" 53 #include "pci_if.h" 54 55 /* Hooks for the ACPI CA debugging infrastructure. */ 56 #define _COMPONENT ACPI_BUS 57 ACPI_MODULE_NAME("PCI") 58 59 struct acpi_pci_devinfo { 60 struct pci_devinfo ap_dinfo; 61 ACPI_HANDLE ap_handle; 62 int ap_flags; 63 }; 64 65 ACPI_SERIAL_DECL(pci_powerstate, "ACPI PCI power methods"); 66 67 /* Be sure that ACPI and PCI power states are equivalent. */ 68 CTASSERT(ACPI_STATE_D0 == PCI_POWERSTATE_D0); 69 CTASSERT(ACPI_STATE_D1 == PCI_POWERSTATE_D1); 70 CTASSERT(ACPI_STATE_D2 == PCI_POWERSTATE_D2); 71 CTASSERT(ACPI_STATE_D3 == PCI_POWERSTATE_D3); 72 73 static struct pci_devinfo *acpi_pci_alloc_devinfo(device_t dev); 74 static void acpi_pci_child_deleted(device_t dev, device_t child); 75 static int acpi_pci_child_location_str_method(device_t cbdev, 76 device_t child, char *buf, size_t buflen); 77 static int acpi_pci_probe(device_t dev); 78 static int acpi_pci_read_ivar(device_t dev, device_t child, int which, 79 uintptr_t *result); 80 static int acpi_pci_write_ivar(device_t dev, device_t child, int which, 81 uintptr_t value); 82 static ACPI_STATUS acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, 83 void *context, void **status); 84 static int acpi_pci_set_powerstate_method(device_t dev, device_t child, 85 int state); 86 static void acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child); 87 static bus_dma_tag_t acpi_pci_get_dma_tag(device_t bus, device_t child); 88 89 static device_method_t acpi_pci_methods[] = { 90 /* Device interface */ 91 DEVMETHOD(device_probe, acpi_pci_probe), 92 93 /* Bus interface */ 94 DEVMETHOD(bus_read_ivar, acpi_pci_read_ivar), 95 DEVMETHOD(bus_write_ivar, acpi_pci_write_ivar), 96 DEVMETHOD(bus_child_deleted, acpi_pci_child_deleted), 97 DEVMETHOD(bus_child_location_str, acpi_pci_child_location_str_method), 98 DEVMETHOD(bus_get_dma_tag, acpi_pci_get_dma_tag), 99 DEVMETHOD(bus_get_domain, acpi_get_domain), 100 101 /* PCI interface */ 102 DEVMETHOD(pci_alloc_devinfo, acpi_pci_alloc_devinfo), 103 DEVMETHOD(pci_child_added, acpi_pci_child_added), 104 DEVMETHOD(pci_set_powerstate, acpi_pci_set_powerstate_method), 105 106 DEVMETHOD_END 107 }; 108 109 static devclass_t pci_devclass; 110 111 DEFINE_CLASS_1(pci, acpi_pci_driver, acpi_pci_methods, sizeof(struct pci_softc), 112 pci_driver); 113 DRIVER_MODULE(acpi_pci, pcib, acpi_pci_driver, pci_devclass, 0, 0); 114 MODULE_DEPEND(acpi_pci, acpi, 1, 1, 1); 115 MODULE_DEPEND(acpi_pci, pci, 1, 1, 1); 116 MODULE_VERSION(acpi_pci, 1); 117 118 static struct pci_devinfo * 119 acpi_pci_alloc_devinfo(device_t dev) 120 { 121 struct acpi_pci_devinfo *dinfo; 122 123 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO); 124 return (&dinfo->ap_dinfo); 125 } 126 127 static int 128 acpi_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 129 { 130 struct acpi_pci_devinfo *dinfo; 131 132 dinfo = device_get_ivars(child); 133 switch (which) { 134 case ACPI_IVAR_HANDLE: 135 *result = (uintptr_t)dinfo->ap_handle; 136 return (0); 137 case ACPI_IVAR_FLAGS: 138 *result = (uintptr_t)dinfo->ap_flags; 139 return (0); 140 } 141 return (pci_read_ivar(dev, child, which, result)); 142 } 143 144 static int 145 acpi_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 146 { 147 struct acpi_pci_devinfo *dinfo; 148 149 dinfo = device_get_ivars(child); 150 switch (which) { 151 case ACPI_IVAR_HANDLE: 152 dinfo->ap_handle = (ACPI_HANDLE)value; 153 return (0); 154 case ACPI_IVAR_FLAGS: 155 dinfo->ap_flags = (int)value; 156 return (0); 157 } 158 return (pci_write_ivar(dev, child, which, value)); 159 } 160 161 static void 162 acpi_pci_child_deleted(device_t dev, device_t child) 163 { 164 struct acpi_pci_devinfo *dinfo = device_get_ivars(child); 165 166 if (acpi_get_device(dinfo->ap_handle) == child) 167 AcpiDetachData(dinfo->ap_handle, acpi_fake_objhandler); 168 pci_child_deleted(dev, child); 169 } 170 171 static int 172 acpi_pci_child_location_str_method(device_t cbdev, device_t child, char *buf, 173 size_t buflen) 174 { 175 struct acpi_pci_devinfo *dinfo = device_get_ivars(child); 176 int pxm; 177 char buf2[32]; 178 179 pci_child_location_str_method(cbdev, child, buf, buflen); 180 181 if (dinfo->ap_handle) { 182 strlcat(buf, " handle=", buflen); 183 strlcat(buf, acpi_name(dinfo->ap_handle), buflen); 184 185 if (ACPI_SUCCESS(acpi_GetInteger(dinfo->ap_handle, "_PXM", &pxm))) { 186 snprintf(buf2, 32, " _PXM=%d", pxm); 187 strlcat(buf, buf2, buflen); 188 } 189 } 190 return (0); 191 } 192 193 /* 194 * PCI power manangement 195 */ 196 static int 197 acpi_pci_set_powerstate_method(device_t dev, device_t child, int state) 198 { 199 ACPI_HANDLE h; 200 ACPI_STATUS status; 201 int old_state, error; 202 203 error = 0; 204 if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3) 205 return (EINVAL); 206 207 /* 208 * We set the state using PCI Power Management outside of setting 209 * the ACPI state. This means that when powering down a device, we 210 * first shut it down using PCI, and then using ACPI, which lets ACPI 211 * try to power down any Power Resources that are now no longer used. 212 * When powering up a device, we let ACPI set the state first so that 213 * it can enable any needed Power Resources before changing the PCI 214 * power state. 215 */ 216 ACPI_SERIAL_BEGIN(pci_powerstate); 217 old_state = pci_get_powerstate(child); 218 if (old_state < state && pci_do_power_suspend) { 219 error = pci_set_powerstate_method(dev, child, state); 220 if (error) 221 goto out; 222 } 223 h = acpi_get_handle(child); 224 status = acpi_pwr_switch_consumer(h, state); 225 if (ACPI_SUCCESS(status)) { 226 if (bootverbose) 227 device_printf(dev, "set ACPI power state D%d on %s\n", 228 state, acpi_name(h)); 229 } else if (status != AE_NOT_FOUND) 230 device_printf(dev, 231 "failed to set ACPI power state D%d on %s: %s\n", 232 state, acpi_name(h), AcpiFormatException(status)); 233 if (old_state > state && pci_do_power_resume) 234 error = pci_set_powerstate_method(dev, child, state); 235 236 out: 237 ACPI_SERIAL_END(pci_powerstate); 238 return (error); 239 } 240 241 static void 242 acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child) 243 { 244 ACPI_STATUS status; 245 device_t child; 246 247 /* 248 * Occasionally a PCI device may show up as an ACPI device 249 * with a _HID. (For example, the TabletPC TC1000 has a 250 * second PCI-ISA bridge that has a _HID for an 251 * acpi_sysresource device.) In that case, leave ACPI-CA's 252 * device data pointing at the ACPI-enumerated device. 253 */ 254 child = acpi_get_device(handle); 255 if (child != NULL) { 256 KASSERT(device_get_parent(child) == 257 devclass_get_device(devclass_find("acpi"), 0), 258 ("%s: child (%s)'s parent is not acpi0", __func__, 259 acpi_name(handle))); 260 return; 261 } 262 263 /* 264 * Update ACPI-CA to use the PCI enumerated device_t for this handle. 265 */ 266 status = AcpiAttachData(handle, acpi_fake_objhandler, pci_child); 267 if (ACPI_FAILURE(status)) 268 printf("WARNING: Unable to attach object data to %s - %s\n", 269 acpi_name(handle), AcpiFormatException(status)); 270 } 271 272 static ACPI_STATUS 273 acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, void *context, 274 void **status) 275 { 276 struct acpi_pci_devinfo *dinfo; 277 device_t child; 278 int func, slot; 279 UINT32 address; 280 281 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 282 283 child = context; 284 if (ACPI_FAILURE(acpi_GetInteger(handle, "_ADR", &address))) 285 return_ACPI_STATUS (AE_OK); 286 slot = ACPI_ADR_PCI_SLOT(address); 287 func = ACPI_ADR_PCI_FUNC(address); 288 dinfo = device_get_ivars(child); 289 if (dinfo->ap_dinfo.cfg.func == func && 290 dinfo->ap_dinfo.cfg.slot == slot) { 291 dinfo->ap_handle = handle; 292 acpi_pci_update_device(handle, child); 293 return_ACPI_STATUS (AE_CTRL_TERMINATE); 294 } 295 return_ACPI_STATUS (AE_OK); 296 } 297 298 void 299 acpi_pci_child_added(device_t dev, device_t child) 300 { 301 302 /* 303 * PCI devices are added via the bus scan in the normal PCI 304 * bus driver. As each device is added, the 305 * acpi_pci_child_added() callback walks the ACPI namespace 306 * under the bridge driver to save ACPI handles to all the 307 * devices that appear in the ACPI namespace as immediate 308 * descendants of the bridge. 309 * 310 * XXX: Sometimes PCI devices show up in the ACPI namespace that 311 * pci_add_children() doesn't find. We currently just ignore 312 * these devices. 313 */ 314 AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1, 315 acpi_pci_save_handle, NULL, child, NULL); 316 } 317 318 static int 319 acpi_pci_probe(device_t dev) 320 { 321 322 if (acpi_get_handle(dev) == NULL) 323 return (ENXIO); 324 device_set_desc(dev, "ACPI PCI bus"); 325 return (BUS_PROBE_DEFAULT); 326 } 327 328 #ifdef ACPI_DMAR 329 bus_dma_tag_t dmar_get_dma_tag(device_t dev, device_t child); 330 static bus_dma_tag_t 331 acpi_pci_get_dma_tag(device_t bus, device_t child) 332 { 333 bus_dma_tag_t tag; 334 335 if (device_get_parent(child) == bus) { 336 /* try dmar and return if it works */ 337 tag = dmar_get_dma_tag(bus, child); 338 } else 339 tag = NULL; 340 if (tag == NULL) 341 tag = pci_get_dma_tag(bus, child); 342 return (tag); 343 } 344 #else 345 static bus_dma_tag_t 346 acpi_pci_get_dma_tag(device_t bus, device_t child) 347 { 348 349 return (pci_get_dma_tag(bus, child)); 350 } 351 #endif 352 353