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