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$");
33 #include "opt_iommu.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/taskqueue.h>
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
47 #include <dev/acpica/acpivar.h>
48 #include <dev/acpica/acpi_pcivar.h>
50 #include <sys/pciio.h>
51 #include <dev/pci/pcireg.h>
52 #include <dev/pci/pcivar.h>
53 #include <dev/pci/pci_private.h>
55 #include <dev/iommu/iommu.h>
60 /* Hooks for the ACPI CA debugging infrastructure. */
61 #define _COMPONENT ACPI_BUS
62 ACPI_MODULE_NAME("PCI")
64 struct acpi_pci_devinfo {
65 struct pci_devinfo ap_dinfo;
66 ACPI_HANDLE ap_handle;
70 ACPI_SERIAL_DECL(pci_powerstate, "ACPI PCI power methods");
72 /* Be sure that ACPI and PCI power states are equivalent. */
73 CTASSERT(ACPI_STATE_D0 == PCI_POWERSTATE_D0);
74 CTASSERT(ACPI_STATE_D1 == PCI_POWERSTATE_D1);
75 CTASSERT(ACPI_STATE_D2 == PCI_POWERSTATE_D2);
76 CTASSERT(ACPI_STATE_D3 == PCI_POWERSTATE_D3);
78 static struct pci_devinfo *acpi_pci_alloc_devinfo(device_t dev);
79 static int acpi_pci_attach(device_t dev);
80 static void acpi_pci_child_deleted(device_t dev, device_t child);
81 static int acpi_pci_child_location_str_method(device_t cbdev,
82 device_t child, char *buf, size_t buflen);
83 static int acpi_pci_detach(device_t dev);
84 static int acpi_pci_probe(device_t dev);
85 static int acpi_pci_read_ivar(device_t dev, device_t child, int which,
87 static int acpi_pci_write_ivar(device_t dev, device_t child, int which,
89 static ACPI_STATUS acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level,
90 void *context, void **status);
91 static int acpi_pci_set_powerstate_method(device_t dev, device_t child,
93 static void acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child);
94 static bus_dma_tag_t acpi_pci_get_dma_tag(device_t bus, device_t child);
96 static device_method_t acpi_pci_methods[] = {
97 /* Device interface */
98 DEVMETHOD(device_probe, acpi_pci_probe),
99 DEVMETHOD(device_attach, acpi_pci_attach),
100 DEVMETHOD(device_detach, acpi_pci_detach),
103 DEVMETHOD(bus_read_ivar, acpi_pci_read_ivar),
104 DEVMETHOD(bus_write_ivar, acpi_pci_write_ivar),
105 DEVMETHOD(bus_child_deleted, acpi_pci_child_deleted),
106 DEVMETHOD(bus_child_location_str, acpi_pci_child_location_str_method),
107 DEVMETHOD(bus_get_cpus, acpi_get_cpus),
108 DEVMETHOD(bus_get_dma_tag, acpi_pci_get_dma_tag),
109 DEVMETHOD(bus_get_domain, acpi_get_domain),
112 DEVMETHOD(pci_alloc_devinfo, acpi_pci_alloc_devinfo),
113 DEVMETHOD(pci_child_added, acpi_pci_child_added),
114 DEVMETHOD(pci_set_powerstate, acpi_pci_set_powerstate_method),
119 static devclass_t pci_devclass;
121 DEFINE_CLASS_1(pci, acpi_pci_driver, acpi_pci_methods, sizeof(struct pci_softc),
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);
128 static struct pci_devinfo *
129 acpi_pci_alloc_devinfo(device_t dev)
131 struct acpi_pci_devinfo *dinfo;
133 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
134 return (&dinfo->ap_dinfo);
138 acpi_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
140 struct acpi_pci_devinfo *dinfo;
142 dinfo = device_get_ivars(child);
144 case ACPI_IVAR_HANDLE:
145 *result = (uintptr_t)dinfo->ap_handle;
147 case ACPI_IVAR_FLAGS:
148 *result = (uintptr_t)dinfo->ap_flags;
151 return (pci_read_ivar(dev, child, which, result));
155 acpi_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
157 struct acpi_pci_devinfo *dinfo;
159 dinfo = device_get_ivars(child);
161 case ACPI_IVAR_HANDLE:
162 dinfo->ap_handle = (ACPI_HANDLE)value;
164 case ACPI_IVAR_FLAGS:
165 dinfo->ap_flags = (int)value;
168 return (pci_write_ivar(dev, child, which, value));
172 acpi_pci_child_deleted(device_t dev, device_t child)
174 struct acpi_pci_devinfo *dinfo = device_get_ivars(child);
176 if (acpi_get_device(dinfo->ap_handle) == child)
177 AcpiDetachData(dinfo->ap_handle, acpi_fake_objhandler);
178 pci_child_deleted(dev, child);
182 acpi_pci_child_location_str_method(device_t cbdev, device_t child, char *buf,
185 struct acpi_pci_devinfo *dinfo = device_get_ivars(child);
189 pci_child_location_str_method(cbdev, child, buf, buflen);
191 if (dinfo->ap_handle) {
192 strlcat(buf, " handle=", buflen);
193 strlcat(buf, acpi_name(dinfo->ap_handle), buflen);
195 if (ACPI_SUCCESS(acpi_GetInteger(dinfo->ap_handle, "_PXM", &pxm))) {
196 snprintf(buf2, 32, " _PXM=%d", pxm);
197 strlcat(buf, buf2, buflen);
204 * PCI power manangement
207 acpi_pci_set_powerstate_method(device_t dev, device_t child, int state)
211 int old_state, error;
214 if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
218 * We set the state using PCI Power Management outside of setting
219 * the ACPI state. This means that when powering down a device, we
220 * first shut it down using PCI, and then using ACPI, which lets ACPI
221 * try to power down any Power Resources that are now no longer used.
222 * When powering up a device, we let ACPI set the state first so that
223 * it can enable any needed Power Resources before changing the PCI
226 ACPI_SERIAL_BEGIN(pci_powerstate);
227 old_state = pci_get_powerstate(child);
228 if (old_state < state && pci_do_power_suspend) {
229 error = pci_set_powerstate_method(dev, child, state);
233 h = acpi_get_handle(child);
234 status = acpi_pwr_switch_consumer(h, state);
235 if (ACPI_SUCCESS(status)) {
237 device_printf(dev, "set ACPI power state D%d on %s\n",
238 state, acpi_name(h));
239 } else if (status != AE_NOT_FOUND)
241 "failed to set ACPI power state D%d on %s: %s\n",
242 state, acpi_name(h), AcpiFormatException(status));
243 if (old_state > state && pci_do_power_resume)
244 error = pci_set_powerstate_method(dev, child, state);
247 ACPI_SERIAL_END(pci_powerstate);
252 acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child)
258 * Occasionally a PCI device may show up as an ACPI device
259 * with a _HID. (For example, the TabletPC TC1000 has a
260 * second PCI-ISA bridge that has a _HID for an
261 * acpi_sysresource device.) In that case, leave ACPI-CA's
262 * device data pointing at the ACPI-enumerated device.
264 child = acpi_get_device(handle);
266 KASSERT(device_get_parent(child) ==
267 devclass_get_device(devclass_find("acpi"), 0),
268 ("%s: child (%s)'s parent is not acpi0", __func__,
274 * Update ACPI-CA to use the PCI enumerated device_t for this handle.
276 status = AcpiAttachData(handle, acpi_fake_objhandler, pci_child);
277 if (ACPI_FAILURE(status))
278 printf("WARNING: Unable to attach object data to %s - %s\n",
279 acpi_name(handle), AcpiFormatException(status));
283 acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, void *context,
286 struct acpi_pci_devinfo *dinfo;
291 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
294 if (ACPI_FAILURE(acpi_GetInteger(handle, "_ADR", &address)))
295 return_ACPI_STATUS (AE_OK);
296 slot = ACPI_ADR_PCI_SLOT(address);
297 func = ACPI_ADR_PCI_FUNC(address);
298 dinfo = device_get_ivars(child);
299 if (dinfo->ap_dinfo.cfg.func == func &&
300 dinfo->ap_dinfo.cfg.slot == slot) {
301 dinfo->ap_handle = handle;
302 acpi_pci_update_device(handle, child);
303 return_ACPI_STATUS (AE_CTRL_TERMINATE);
305 return_ACPI_STATUS (AE_OK);
309 acpi_pci_child_added(device_t dev, device_t child)
313 * PCI devices are added via the bus scan in the normal PCI
314 * bus driver. As each device is added, the
315 * acpi_pci_child_added() callback walks the ACPI namespace
316 * under the bridge driver to save ACPI handles to all the
317 * devices that appear in the ACPI namespace as immediate
318 * descendants of the bridge.
320 * XXX: Sometimes PCI devices show up in the ACPI namespace that
321 * pci_add_children() doesn't find. We currently just ignore
324 AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1,
325 acpi_pci_save_handle, NULL, child, NULL);
329 acpi_pci_probe(device_t dev)
332 if (acpi_get_handle(dev) == NULL)
334 device_set_desc(dev, "ACPI PCI bus");
335 return (BUS_PROBE_DEFAULT);
339 acpi_pci_bus_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
346 case ACPI_NOTIFY_BUS_CHECK:
352 device_printf(dev, "unknown notify %#x\n", notify);
358 acpi_pci_device_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
367 case ACPI_NOTIFY_DEVICE_CHECK:
372 case ACPI_NOTIFY_EJECT_REQUEST:
373 child = acpi_get_device(h);
375 device_printf(dev, "no device to eject for %s\n",
380 error = device_detach(child);
383 device_printf(dev, "failed to detach %s: %d\n",
384 device_get_nameunit(child), error);
387 status = acpi_SetInteger(h, "_EJ0", 1);
388 if (ACPI_FAILURE(status)) {
390 device_printf(dev, "failed to eject %s: %s\n",
391 acpi_name(h), AcpiFormatException(status));
398 device_printf(dev, "unknown notify %#x for %s\n", notify,
405 acpi_pci_install_device_notify_handler(ACPI_HANDLE handle, UINT32 level,
406 void *context, void **status)
410 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
412 if (ACPI_FAILURE(AcpiGetHandle(handle, "_EJ0", &h)))
413 return_ACPI_STATUS (AE_OK);
415 AcpiInstallNotifyHandler(handle, ACPI_SYSTEM_NOTIFY,
416 acpi_pci_device_notify_handler, context);
417 return_ACPI_STATUS (AE_OK);
421 acpi_pci_attach(device_t dev)
425 error = pci_attach(dev);
428 AcpiInstallNotifyHandler(acpi_get_handle(dev), ACPI_SYSTEM_NOTIFY,
429 acpi_pci_bus_notify_handler, dev);
430 AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1,
431 acpi_pci_install_device_notify_handler, NULL, dev, NULL);
437 acpi_pci_remove_notify_handler(ACPI_HANDLE handle, UINT32 level, void *context,
442 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
444 if (ACPI_FAILURE(AcpiGetHandle(handle, "_EJ0", &h)))
445 return_ACPI_STATUS (AE_OK);
447 AcpiRemoveNotifyHandler(handle, ACPI_SYSTEM_NOTIFY,
448 acpi_pci_device_notify_handler);
449 return_ACPI_STATUS (AE_OK);
453 acpi_pci_detach(device_t dev)
456 AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1,
457 acpi_pci_remove_notify_handler, NULL, dev, NULL);
458 AcpiRemoveNotifyHandler(acpi_get_handle(dev), ACPI_SYSTEM_NOTIFY,
459 acpi_pci_bus_notify_handler);
460 return (pci_detach(dev));
465 acpi_pci_get_dma_tag(device_t bus, device_t child)
469 if (device_get_parent(child) == bus) {
470 /* try iommu and return if it works */
471 tag = iommu_get_dma_tag(bus, child);
475 tag = pci_get_dma_tag(bus, child);
480 acpi_pci_get_dma_tag(device_t bus, device_t child)
483 return (pci_get_dma_tag(bus, child));