2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3 * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
4 * Copyright (c) 2000, BSDi
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
41 #include <contrib/dev/acpica/include/acpi.h>
42 #include <contrib/dev/acpica/include/accommon.h>
44 #include <dev/acpica/acpivar.h>
45 #include <dev/acpica/acpi_pcivar.h>
47 #include <sys/pciio.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pci_private.h>
55 /* Hooks for the ACPI CA debugging infrastructure. */
56 #define _COMPONENT ACPI_BUS
57 ACPI_MODULE_NAME("PCI")
59 struct acpi_pci_devinfo {
60 struct pci_devinfo ap_dinfo;
61 ACPI_HANDLE ap_handle;
65 ACPI_SERIAL_DECL(pci_powerstate, "ACPI PCI power methods");
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);
73 static struct pci_devinfo *acpi_pci_alloc_devinfo(device_t dev);
74 static int acpi_pci_attach(device_t dev);
75 static void acpi_pci_child_deleted(device_t dev, device_t child);
76 static int acpi_pci_child_location_str_method(device_t cbdev,
77 device_t child, char *buf, size_t buflen);
78 static int acpi_pci_detach(device_t dev);
79 static int acpi_pci_probe(device_t dev);
80 static int acpi_pci_read_ivar(device_t dev, device_t child, int which,
82 static int acpi_pci_write_ivar(device_t dev, device_t child, int which,
84 static ACPI_STATUS acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level,
85 void *context, void **status);
86 static int acpi_pci_set_powerstate_method(device_t dev, device_t child,
88 static void acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child);
89 static bus_dma_tag_t acpi_pci_get_dma_tag(device_t bus, device_t child);
91 static device_method_t acpi_pci_methods[] = {
92 /* Device interface */
93 DEVMETHOD(device_probe, acpi_pci_probe),
94 DEVMETHOD(device_attach, acpi_pci_attach),
95 DEVMETHOD(device_detach, acpi_pci_detach),
98 DEVMETHOD(bus_read_ivar, acpi_pci_read_ivar),
99 DEVMETHOD(bus_write_ivar, acpi_pci_write_ivar),
100 DEVMETHOD(bus_child_deleted, acpi_pci_child_deleted),
101 DEVMETHOD(bus_child_location_str, acpi_pci_child_location_str_method),
102 DEVMETHOD(bus_get_cpus, acpi_get_cpus),
103 DEVMETHOD(bus_get_dma_tag, acpi_pci_get_dma_tag),
104 DEVMETHOD(bus_get_domain, acpi_get_domain),
107 DEVMETHOD(pci_alloc_devinfo, acpi_pci_alloc_devinfo),
108 DEVMETHOD(pci_child_added, acpi_pci_child_added),
109 DEVMETHOD(pci_set_powerstate, acpi_pci_set_powerstate_method),
114 static devclass_t pci_devclass;
116 DEFINE_CLASS_1(pci, acpi_pci_driver, acpi_pci_methods, sizeof(struct pci_softc),
118 DRIVER_MODULE(acpi_pci, pcib, acpi_pci_driver, pci_devclass, 0, 0);
119 MODULE_DEPEND(acpi_pci, acpi, 1, 1, 1);
120 MODULE_DEPEND(acpi_pci, pci, 1, 1, 1);
121 MODULE_VERSION(acpi_pci, 1);
123 static struct pci_devinfo *
124 acpi_pci_alloc_devinfo(device_t dev)
126 struct acpi_pci_devinfo *dinfo;
128 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
129 return (&dinfo->ap_dinfo);
133 acpi_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
135 struct acpi_pci_devinfo *dinfo;
137 dinfo = device_get_ivars(child);
139 case ACPI_IVAR_HANDLE:
140 *result = (uintptr_t)dinfo->ap_handle;
142 case ACPI_IVAR_FLAGS:
143 *result = (uintptr_t)dinfo->ap_flags;
146 return (pci_read_ivar(dev, child, which, result));
150 acpi_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
152 struct acpi_pci_devinfo *dinfo;
154 dinfo = device_get_ivars(child);
156 case ACPI_IVAR_HANDLE:
157 dinfo->ap_handle = (ACPI_HANDLE)value;
159 case ACPI_IVAR_FLAGS:
160 dinfo->ap_flags = (int)value;
163 return (pci_write_ivar(dev, child, which, value));
167 acpi_pci_child_deleted(device_t dev, device_t child)
169 struct acpi_pci_devinfo *dinfo = device_get_ivars(child);
171 if (acpi_get_device(dinfo->ap_handle) == child)
172 AcpiDetachData(dinfo->ap_handle, acpi_fake_objhandler);
173 pci_child_deleted(dev, child);
177 acpi_pci_child_location_str_method(device_t cbdev, device_t child, char *buf,
180 struct acpi_pci_devinfo *dinfo = device_get_ivars(child);
184 pci_child_location_str_method(cbdev, child, buf, buflen);
186 if (dinfo->ap_handle) {
187 strlcat(buf, " handle=", buflen);
188 strlcat(buf, acpi_name(dinfo->ap_handle), buflen);
190 if (ACPI_SUCCESS(acpi_GetInteger(dinfo->ap_handle, "_PXM", &pxm))) {
191 snprintf(buf2, 32, " _PXM=%d", pxm);
192 strlcat(buf, buf2, buflen);
199 * PCI power manangement
202 acpi_pci_set_powerstate_method(device_t dev, device_t child, int state)
206 int old_state, error;
209 if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
213 * We set the state using PCI Power Management outside of setting
214 * the ACPI state. This means that when powering down a device, we
215 * first shut it down using PCI, and then using ACPI, which lets ACPI
216 * try to power down any Power Resources that are now no longer used.
217 * When powering up a device, we let ACPI set the state first so that
218 * it can enable any needed Power Resources before changing the PCI
221 ACPI_SERIAL_BEGIN(pci_powerstate);
222 old_state = pci_get_powerstate(child);
223 if (old_state < state && pci_do_power_suspend) {
224 error = pci_set_powerstate_method(dev, child, state);
228 h = acpi_get_handle(child);
229 status = acpi_pwr_switch_consumer(h, state);
230 if (ACPI_SUCCESS(status)) {
232 device_printf(dev, "set ACPI power state D%d on %s\n",
233 state, acpi_name(h));
234 } else if (status != AE_NOT_FOUND)
236 "failed to set ACPI power state D%d on %s: %s\n",
237 state, acpi_name(h), AcpiFormatException(status));
238 if (old_state > state && pci_do_power_resume)
239 error = pci_set_powerstate_method(dev, child, state);
242 ACPI_SERIAL_END(pci_powerstate);
247 acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child)
253 * Occasionally a PCI device may show up as an ACPI device
254 * with a _HID. (For example, the TabletPC TC1000 has a
255 * second PCI-ISA bridge that has a _HID for an
256 * acpi_sysresource device.) In that case, leave ACPI-CA's
257 * device data pointing at the ACPI-enumerated device.
259 child = acpi_get_device(handle);
261 KASSERT(device_get_parent(child) ==
262 devclass_get_device(devclass_find("acpi"), 0),
263 ("%s: child (%s)'s parent is not acpi0", __func__,
269 * Update ACPI-CA to use the PCI enumerated device_t for this handle.
271 status = AcpiAttachData(handle, acpi_fake_objhandler, pci_child);
272 if (ACPI_FAILURE(status))
273 printf("WARNING: Unable to attach object data to %s - %s\n",
274 acpi_name(handle), AcpiFormatException(status));
278 acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, void *context,
281 struct acpi_pci_devinfo *dinfo;
286 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
289 if (ACPI_FAILURE(acpi_GetInteger(handle, "_ADR", &address)))
290 return_ACPI_STATUS (AE_OK);
291 slot = ACPI_ADR_PCI_SLOT(address);
292 func = ACPI_ADR_PCI_FUNC(address);
293 dinfo = device_get_ivars(child);
294 if (dinfo->ap_dinfo.cfg.func == func &&
295 dinfo->ap_dinfo.cfg.slot == slot) {
296 dinfo->ap_handle = handle;
297 acpi_pci_update_device(handle, child);
298 return_ACPI_STATUS (AE_CTRL_TERMINATE);
300 return_ACPI_STATUS (AE_OK);
304 acpi_pci_child_added(device_t dev, device_t child)
308 * PCI devices are added via the bus scan in the normal PCI
309 * bus driver. As each device is added, the
310 * acpi_pci_child_added() callback walks the ACPI namespace
311 * under the bridge driver to save ACPI handles to all the
312 * devices that appear in the ACPI namespace as immediate
313 * descendants of the bridge.
315 * XXX: Sometimes PCI devices show up in the ACPI namespace that
316 * pci_add_children() doesn't find. We currently just ignore
319 AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1,
320 acpi_pci_save_handle, NULL, child, NULL);
324 acpi_pci_probe(device_t dev)
327 if (acpi_get_handle(dev) == NULL)
329 device_set_desc(dev, "ACPI PCI bus");
330 return (BUS_PROBE_DEFAULT);
334 acpi_pci_bus_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
341 case ACPI_NOTIFY_BUS_CHECK:
347 device_printf(dev, "unknown notify %#x\n", notify);
353 acpi_pci_device_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
362 case ACPI_NOTIFY_DEVICE_CHECK:
367 case ACPI_NOTIFY_EJECT_REQUEST:
368 child = acpi_get_device(h);
370 device_printf(dev, "no device to eject for %s\n",
375 error = device_detach(child);
378 device_printf(dev, "failed to detach %s: %d\n",
379 device_get_nameunit(child), error);
382 status = acpi_SetInteger(h, "_EJ0", 1);
383 if (ACPI_FAILURE(status)) {
385 device_printf(dev, "failed to eject %s: %s\n",
386 acpi_name(h), AcpiFormatException(status));
393 device_printf(dev, "unknown notify %#x for %s\n", notify,
400 acpi_pci_install_device_notify_handler(ACPI_HANDLE handle, UINT32 level,
401 void *context, void **status)
405 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
407 if (ACPI_FAILURE(AcpiGetHandle(handle, "_EJ0", &h)))
408 return_ACPI_STATUS (AE_OK);
410 AcpiInstallNotifyHandler(handle, ACPI_SYSTEM_NOTIFY,
411 acpi_pci_device_notify_handler, context);
412 return_ACPI_STATUS (AE_OK);
416 acpi_pci_attach(device_t dev)
420 error = pci_attach(dev);
423 AcpiInstallNotifyHandler(acpi_get_handle(dev), ACPI_SYSTEM_NOTIFY,
424 acpi_pci_bus_notify_handler, dev);
425 AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1,
426 acpi_pci_install_device_notify_handler, NULL, dev, NULL);
432 acpi_pci_remove_notify_handler(ACPI_HANDLE handle, UINT32 level, void *context,
437 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
439 if (ACPI_FAILURE(AcpiGetHandle(handle, "_EJ0", &h)))
440 return_ACPI_STATUS (AE_OK);
442 AcpiRemoveNotifyHandler(handle, ACPI_SYSTEM_NOTIFY,
443 acpi_pci_device_notify_handler);
444 return_ACPI_STATUS (AE_OK);
448 acpi_pci_detach(device_t dev)
451 AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1,
452 acpi_pci_remove_notify_handler, NULL, dev, NULL);
453 AcpiRemoveNotifyHandler(acpi_get_handle(dev), ACPI_SYSTEM_NOTIFY,
454 acpi_pci_bus_notify_handler);
455 return (pci_detach(dev));
459 bus_dma_tag_t dmar_get_dma_tag(device_t dev, device_t child);
461 acpi_pci_get_dma_tag(device_t bus, device_t child)
465 if (device_get_parent(child) == bus) {
466 /* try dmar and return if it works */
467 tag = dmar_get_dma_tag(bus, child);
471 tag = pci_get_dma_tag(bus, child);
476 acpi_pci_get_dma_tag(device_t bus, device_t child)
479 return (pci_get_dma_tag(bus, child));