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 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,
80 static int acpi_pci_write_ivar(device_t dev, device_t child, int which,
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,
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);
89 static device_method_t acpi_pci_methods[] = {
90 /* Device interface */
91 DEVMETHOD(device_probe, acpi_pci_probe),
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_cpus, acpi_get_cpus),
99 DEVMETHOD(bus_get_dma_tag, acpi_pci_get_dma_tag),
100 DEVMETHOD(bus_get_domain, acpi_get_domain),
103 DEVMETHOD(pci_alloc_devinfo, acpi_pci_alloc_devinfo),
104 DEVMETHOD(pci_child_added, acpi_pci_child_added),
105 DEVMETHOD(pci_set_powerstate, acpi_pci_set_powerstate_method),
110 static devclass_t pci_devclass;
112 DEFINE_CLASS_1(pci, acpi_pci_driver, acpi_pci_methods, sizeof(struct pci_softc),
114 DRIVER_MODULE(acpi_pci, pcib, acpi_pci_driver, pci_devclass, 0, 0);
115 MODULE_DEPEND(acpi_pci, acpi, 1, 1, 1);
116 MODULE_DEPEND(acpi_pci, pci, 1, 1, 1);
117 MODULE_VERSION(acpi_pci, 1);
119 static struct pci_devinfo *
120 acpi_pci_alloc_devinfo(device_t dev)
122 struct acpi_pci_devinfo *dinfo;
124 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
125 return (&dinfo->ap_dinfo);
129 acpi_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
131 struct acpi_pci_devinfo *dinfo;
133 dinfo = device_get_ivars(child);
135 case ACPI_IVAR_HANDLE:
136 *result = (uintptr_t)dinfo->ap_handle;
138 case ACPI_IVAR_FLAGS:
139 *result = (uintptr_t)dinfo->ap_flags;
142 return (pci_read_ivar(dev, child, which, result));
146 acpi_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
148 struct acpi_pci_devinfo *dinfo;
150 dinfo = device_get_ivars(child);
152 case ACPI_IVAR_HANDLE:
153 dinfo->ap_handle = (ACPI_HANDLE)value;
155 case ACPI_IVAR_FLAGS:
156 dinfo->ap_flags = (int)value;
159 return (pci_write_ivar(dev, child, which, value));
163 acpi_pci_child_deleted(device_t dev, device_t child)
165 struct acpi_pci_devinfo *dinfo = device_get_ivars(child);
167 if (acpi_get_device(dinfo->ap_handle) == child)
168 AcpiDetachData(dinfo->ap_handle, acpi_fake_objhandler);
169 pci_child_deleted(dev, child);
173 acpi_pci_child_location_str_method(device_t cbdev, device_t child, char *buf,
176 struct acpi_pci_devinfo *dinfo = device_get_ivars(child);
180 pci_child_location_str_method(cbdev, child, buf, buflen);
182 if (dinfo->ap_handle) {
183 strlcat(buf, " handle=", buflen);
184 strlcat(buf, acpi_name(dinfo->ap_handle), buflen);
186 if (ACPI_SUCCESS(acpi_GetInteger(dinfo->ap_handle, "_PXM", &pxm))) {
187 snprintf(buf2, 32, " _PXM=%d", pxm);
188 strlcat(buf, buf2, buflen);
195 * PCI power manangement
198 acpi_pci_set_powerstate_method(device_t dev, device_t child, int state)
202 int old_state, error;
205 if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
209 * We set the state using PCI Power Management outside of setting
210 * the ACPI state. This means that when powering down a device, we
211 * first shut it down using PCI, and then using ACPI, which lets ACPI
212 * try to power down any Power Resources that are now no longer used.
213 * When powering up a device, we let ACPI set the state first so that
214 * it can enable any needed Power Resources before changing the PCI
217 ACPI_SERIAL_BEGIN(pci_powerstate);
218 old_state = pci_get_powerstate(child);
219 if (old_state < state && pci_do_power_suspend) {
220 error = pci_set_powerstate_method(dev, child, state);
224 h = acpi_get_handle(child);
225 status = acpi_pwr_switch_consumer(h, state);
226 if (ACPI_SUCCESS(status)) {
228 device_printf(dev, "set ACPI power state D%d on %s\n",
229 state, acpi_name(h));
230 } else if (status != AE_NOT_FOUND)
232 "failed to set ACPI power state D%d on %s: %s\n",
233 state, acpi_name(h), AcpiFormatException(status));
234 if (old_state > state && pci_do_power_resume)
235 error = pci_set_powerstate_method(dev, child, state);
238 ACPI_SERIAL_END(pci_powerstate);
243 acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child)
249 * Occasionally a PCI device may show up as an ACPI device
250 * with a _HID. (For example, the TabletPC TC1000 has a
251 * second PCI-ISA bridge that has a _HID for an
252 * acpi_sysresource device.) In that case, leave ACPI-CA's
253 * device data pointing at the ACPI-enumerated device.
255 child = acpi_get_device(handle);
257 KASSERT(device_get_parent(child) ==
258 devclass_get_device(devclass_find("acpi"), 0),
259 ("%s: child (%s)'s parent is not acpi0", __func__,
265 * Update ACPI-CA to use the PCI enumerated device_t for this handle.
267 status = AcpiAttachData(handle, acpi_fake_objhandler, pci_child);
268 if (ACPI_FAILURE(status))
269 printf("WARNING: Unable to attach object data to %s - %s\n",
270 acpi_name(handle), AcpiFormatException(status));
274 acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, void *context,
277 struct acpi_pci_devinfo *dinfo;
282 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
285 if (ACPI_FAILURE(acpi_GetInteger(handle, "_ADR", &address)))
286 return_ACPI_STATUS (AE_OK);
287 slot = ACPI_ADR_PCI_SLOT(address);
288 func = ACPI_ADR_PCI_FUNC(address);
289 dinfo = device_get_ivars(child);
290 if (dinfo->ap_dinfo.cfg.func == func &&
291 dinfo->ap_dinfo.cfg.slot == slot) {
292 dinfo->ap_handle = handle;
293 acpi_pci_update_device(handle, child);
294 return_ACPI_STATUS (AE_CTRL_TERMINATE);
296 return_ACPI_STATUS (AE_OK);
300 acpi_pci_child_added(device_t dev, device_t child)
304 * PCI devices are added via the bus scan in the normal PCI
305 * bus driver. As each device is added, the
306 * acpi_pci_child_added() callback walks the ACPI namespace
307 * under the bridge driver to save ACPI handles to all the
308 * devices that appear in the ACPI namespace as immediate
309 * descendants of the bridge.
311 * XXX: Sometimes PCI devices show up in the ACPI namespace that
312 * pci_add_children() doesn't find. We currently just ignore
315 AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1,
316 acpi_pci_save_handle, NULL, child, NULL);
320 acpi_pci_probe(device_t dev)
323 if (acpi_get_handle(dev) == NULL)
325 device_set_desc(dev, "ACPI PCI bus");
326 return (BUS_PROBE_DEFAULT);
330 bus_dma_tag_t dmar_get_dma_tag(device_t dev, device_t child);
332 acpi_pci_get_dma_tag(device_t bus, device_t child)
336 if (device_get_parent(child) == bus) {
337 /* try dmar and return if it works */
338 tag = dmar_get_dma_tag(bus, child);
342 tag = pci_get_dma_tag(bus, child);
347 acpi_pci_get_dma_tag(device_t bus, device_t child)
350 return (pci_get_dma_tag(bus, child));