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: src/sys/dev/acpica/acpi_pci.c,v 1.23.2.3 2005/01/30 00:59:23 imp Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38
39 #include "acpi.h"
40 #include <dev/acpica/acpivar.h>
41
42 #include <sys/pciio.h>
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcivar.h>
45 #include <dev/pci/pci_private.h>
46
47 #include "pcib_if.h"
48 #include "pci_if.h"
49
50 /* Hooks for the ACPI CA debugging infrastructure. */
51 #define _COMPONENT ACPI_BUS
52 ACPI_MODULE_NAME("PCI")
53
54 struct acpi_pci_devinfo {
55 struct pci_devinfo ap_dinfo;
56 ACPI_HANDLE ap_handle;
57 int ap_flags;
58 };
59
60 ACPI_SERIAL_DECL(pci_powerstate, "ACPI PCI power methods");
61
62 /* Be sure that ACPI and PCI power states are equivalent. */
63 CTASSERT(ACPI_STATE_D0 == PCI_POWERSTATE_D0);
64 CTASSERT(ACPI_STATE_D1 == PCI_POWERSTATE_D1);
65 CTASSERT(ACPI_STATE_D2 == PCI_POWERSTATE_D2);
66 CTASSERT(ACPI_STATE_D3 == PCI_POWERSTATE_D3);
67
68 static int acpi_pci_attach(device_t dev);
69 static int acpi_pci_child_location_str_method(device_t cbdev,
70 device_t child, char *buf, size_t buflen);
71 static int acpi_pci_probe(device_t dev);
72 static int acpi_pci_read_ivar(device_t dev, device_t child, int which,
73 uintptr_t *result);
74 static int acpi_pci_write_ivar(device_t dev, device_t child, int which,
75 uintptr_t value);
76 static ACPI_STATUS acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level,
77 void *context, void **status);
78 static int acpi_pci_set_powerstate_method(device_t dev, device_t child,
79 int state);
80 static void acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child);
81
82 static device_method_t acpi_pci_methods[] = {
83 /* Device interface */
84 DEVMETHOD(device_probe, acpi_pci_probe),
85 DEVMETHOD(device_attach, acpi_pci_attach),
86 DEVMETHOD(device_shutdown, bus_generic_shutdown),
87 DEVMETHOD(device_suspend, pci_suspend),
88 DEVMETHOD(device_resume, pci_resume),
89
90 /* Bus interface */
91 DEVMETHOD(bus_print_child, pci_print_child),
92 DEVMETHOD(bus_probe_nomatch, pci_probe_nomatch),
93 DEVMETHOD(bus_read_ivar, acpi_pci_read_ivar),
94 DEVMETHOD(bus_write_ivar, acpi_pci_write_ivar),
95 DEVMETHOD(bus_driver_added, pci_driver_added),
96 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
97 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
98
99 DEVMETHOD(bus_get_resource_list,pci_get_resource_list),
100 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
101 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
102 DEVMETHOD(bus_delete_resource, pci_delete_resource),
103 DEVMETHOD(bus_alloc_resource, pci_alloc_resource),
104 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
105 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
106 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
107 DEVMETHOD(bus_child_pnpinfo_str, pci_child_pnpinfo_str_method),
108 DEVMETHOD(bus_child_location_str, acpi_pci_child_location_str_method),
109
110 /* PCI interface */
111 DEVMETHOD(pci_read_config, pci_read_config_method),
112 DEVMETHOD(pci_write_config, pci_write_config_method),
113 DEVMETHOD(pci_enable_busmaster, pci_enable_busmaster_method),
114 DEVMETHOD(pci_disable_busmaster, pci_disable_busmaster_method),
115 DEVMETHOD(pci_enable_io, pci_enable_io_method),
116 DEVMETHOD(pci_disable_io, pci_disable_io_method),
117 DEVMETHOD(pci_get_powerstate, pci_get_powerstate_method),
118 DEVMETHOD(pci_set_powerstate, acpi_pci_set_powerstate_method),
119 DEVMETHOD(pci_assign_interrupt, pci_assign_interrupt_method),
120
121 { 0, 0 }
122 };
123
124 static driver_t acpi_pci_driver = {
125 "pci",
126 acpi_pci_methods,
127 0, /* no softc */
128 };
129
130 DRIVER_MODULE(acpi_pci, pcib, acpi_pci_driver, pci_devclass, 0, 0);
131 MODULE_DEPEND(acpi_pci, acpi, 1, 1, 1);
132 MODULE_DEPEND(acpi_pci, pci, 1, 1, 1);
133 MODULE_VERSION(acpi_pci, 1);
134
135 static int
136 acpi_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
137 {
138 struct acpi_pci_devinfo *dinfo;
139
140 dinfo = device_get_ivars(child);
141 switch (which) {
142 case ACPI_IVAR_HANDLE:
143 *result = (uintptr_t)dinfo->ap_handle;
144 return (0);
145 case ACPI_IVAR_FLAGS:
146 *result = (uintptr_t)dinfo->ap_flags;
147 return (0);
148 }
149 return (pci_read_ivar(dev, child, which, result));
150 }
151
152 static int
153 acpi_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
154 {
155 struct acpi_pci_devinfo *dinfo;
156
157 dinfo = device_get_ivars(child);
158 switch (which) {
159 case ACPI_IVAR_HANDLE:
160 dinfo->ap_handle = (ACPI_HANDLE)value;
161 return (0);
162 case ACPI_IVAR_FLAGS:
163 dinfo->ap_flags = (int)value;
164 return (0);
165 }
166 return (pci_write_ivar(dev, child, which, value));
167 }
168
169 static int
170 acpi_pci_child_location_str_method(device_t cbdev, device_t child, char *buf,
171 size_t buflen)
172 {
173 struct acpi_pci_devinfo *dinfo = device_get_ivars(child);
174
175 pci_child_location_str_method(cbdev, child, buf, buflen);
176
177 if (dinfo->ap_handle) {
178 strlcat(buf, " handle=", buflen);
179 strlcat(buf, acpi_name(dinfo->ap_handle), buflen);
180 }
181 return (0);
182 }
183
184 /*
185 * PCI power manangement
186 */
187 static int
188 acpi_pci_set_powerstate_method(device_t dev, device_t child, int state)
189 {
190 ACPI_HANDLE h;
191 ACPI_STATUS status;
192 int old_state, error;
193
194 error = 0;
195 if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
196 return (EINVAL);
197
198 /*
199 * We set the state using PCI Power Management outside of setting
200 * the ACPI state. This means that when powering down a device, we
201 * first shut it down using PCI, and then using ACPI, which lets ACPI
202 * try to power down any Power Resources that are now no longer used.
203 * When powering up a device, we let ACPI set the state first so that
204 * it can enable any needed Power Resources before changing the PCI
205 * power state.
206 */
207 ACPI_SERIAL_BEGIN(pci_powerstate);
208 old_state = pci_get_powerstate(child);
209 if (old_state < state) {
210 error = pci_set_powerstate_method(dev, child, state);
211 if (error)
212 goto out;
213 }
214 h = acpi_get_handle(child);
215 status = acpi_pwr_switch_consumer(h, state);
216 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
217 device_printf(dev,
218 "Failed to set ACPI power state D%d on %s: %s\n",
219 state, acpi_name(h), AcpiFormatException(status));
220 if (old_state > state)
221 error = pci_set_powerstate_method(dev, child, state);
222
223 out:
224 ACPI_SERIAL_END(pci_powerstate);
225 return (error);
226 }
227
228 static void
229 acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child)
230 {
231 ACPI_STATUS status;
232 device_t child;
233
234 /*
235 * Lookup and remove the unused device that acpi0 creates when it walks
236 * the namespace creating devices.
237 */
238 child = acpi_get_device(handle);
239 if (child != NULL) {
240 KASSERT(!device_is_alive(child), ("%s: deleting alive child %s",
241 __func__, device_get_nameunit(child)));
242 KASSERT(device_get_parent(child) ==
243 devclass_get_device(devclass_find("acpi"), 0),
244 ("%s: child (%s)'s parent is not acpi0", __func__,
245 acpi_name(handle)));
246 device_delete_child(device_get_parent(child), child);
247 }
248
249 /*
250 * Update ACPI-CA to use the PCI enumerated device_t for this handle.
251 */
252 status = AcpiDetachData(handle, acpi_fake_objhandler);
253 if (ACPI_FAILURE(status))
254 printf("WARNING: Unable to detach object data from %s - %s\n",
255 acpi_name(handle), AcpiFormatException(status));
256 status = AcpiAttachData(handle, acpi_fake_objhandler, pci_child);
257 if (ACPI_FAILURE(status))
258 printf("WARNING: Unable to attach object data to %s - %s\n",
259 acpi_name(handle), AcpiFormatException(status));
260 }
261
262 static ACPI_STATUS
263 acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, void *context,
264 void **status)
265 {
266 struct acpi_pci_devinfo *dinfo;
267 device_t *devlist;
268 int devcount, i, func, slot;
269 UINT32 address;
270
271 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
272
273 if (ACPI_FAILURE(acpi_GetInteger(handle, "_ADR", &address)))
274 return_ACPI_STATUS (AE_OK);
275 slot = ACPI_ADR_PCI_SLOT(address);
276 func = ACPI_ADR_PCI_FUNC(address);
277 if (device_get_children((device_t)context, &devlist, &devcount) != 0)
278 return_ACPI_STATUS (AE_OK);
279 for (i = 0; i < devcount; i++) {
280 dinfo = device_get_ivars(devlist[i]);
281 if (dinfo->ap_dinfo.cfg.func == func &&
282 dinfo->ap_dinfo.cfg.slot == slot) {
283 dinfo->ap_handle = handle;
284 acpi_pci_update_device(handle, devlist[i]);
285 break;
286 }
287 }
288 free(devlist, M_TEMP);
289 return_ACPI_STATUS (AE_OK);
290 }
291
292 static int
293 acpi_pci_probe(device_t dev)
294 {
295
296 if (pcib_get_bus(dev) < 0)
297 return (ENXIO);
298 if (acpi_get_handle(dev) == NULL)
299 return (ENXIO);
300 device_set_desc(dev, "ACPI PCI bus");
301 return (0);
302 }
303
304 static int
305 acpi_pci_attach(device_t dev)
306 {
307 int busno;
308
309 /*
310 * Since there can be multiple independantly numbered PCI
311 * busses on some large alpha systems, we can't use the unit
312 * number to decide what bus we are probing. We ask the parent
313 * pcib what our bus number is.
314 */
315 busno = pcib_get_bus(dev);
316 if (bootverbose)
317 device_printf(dev, "physical bus=%d\n", busno);
318
319 /*
320 * First, PCI devices are added as in the normal PCI bus driver.
321 * Afterwards, the ACPI namespace under the bridge driver is
322 * walked to save ACPI handles to all the devices that appear in
323 * the ACPI namespace as immediate descendants of the bridge.
324 *
325 * XXX: Sometimes PCI devices show up in the ACPI namespace that
326 * pci_add_children() doesn't find. We currently just ignore
327 * these devices.
328 */
329 pci_add_children(dev, busno, sizeof(struct acpi_pci_devinfo));
330 AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1,
331 acpi_pci_save_handle, dev, NULL);
332
333 return (bus_generic_attach(dev));
334 }
Cache object: b484b695fdebea7e80248099f8f04db6
|