xref: /freebsd/sys/dev/acpica/acpi_pci.c (revision 4f29da19bd44f0e99f021510460a81bf754c21d2)
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$");
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 <contrib/dev/acpica/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 
87 	/* Bus interface */
88 	DEVMETHOD(bus_read_ivar,	acpi_pci_read_ivar),
89 	DEVMETHOD(bus_write_ivar,	acpi_pci_write_ivar),
90 	DEVMETHOD(bus_child_location_str, acpi_pci_child_location_str_method),
91 
92 	/* PCI interface */
93 	DEVMETHOD(pci_set_powerstate,	acpi_pci_set_powerstate_method),
94 
95 	{ 0, 0 }
96 };
97 
98 static devclass_t pci_devclass;
99 
100 DEFINE_CLASS_1(pci, acpi_pci_driver, acpi_pci_methods, 0, pci_driver);
101 DRIVER_MODULE(acpi_pci, pcib, acpi_pci_driver, pci_devclass, 0, 0);
102 MODULE_DEPEND(acpi_pci, acpi, 1, 1, 1);
103 MODULE_DEPEND(acpi_pci, pci, 1, 1, 1);
104 MODULE_VERSION(acpi_pci, 1);
105 
106 static int
107 acpi_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
108 {
109     struct acpi_pci_devinfo *dinfo;
110 
111     dinfo = device_get_ivars(child);
112     switch (which) {
113     case ACPI_IVAR_HANDLE:
114 	*result = (uintptr_t)dinfo->ap_handle;
115 	return (0);
116     case ACPI_IVAR_FLAGS:
117 	*result = (uintptr_t)dinfo->ap_flags;
118 	return (0);
119     }
120     return (pci_read_ivar(dev, child, which, result));
121 }
122 
123 static int
124 acpi_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
125 {
126     struct acpi_pci_devinfo *dinfo;
127 
128     dinfo = device_get_ivars(child);
129     switch (which) {
130     case ACPI_IVAR_HANDLE:
131 	dinfo->ap_handle = (ACPI_HANDLE)value;
132 	return (0);
133     case ACPI_IVAR_FLAGS:
134 	dinfo->ap_flags = (int)value;
135 	return (0);
136     }
137     return (pci_write_ivar(dev, child, which, value));
138 }
139 
140 static int
141 acpi_pci_child_location_str_method(device_t cbdev, device_t child, char *buf,
142     size_t buflen)
143 {
144     struct acpi_pci_devinfo *dinfo = device_get_ivars(child);
145 
146     pci_child_location_str_method(cbdev, child, buf, buflen);
147 
148     if (dinfo->ap_handle) {
149 	strlcat(buf, " handle=", buflen);
150 	strlcat(buf, acpi_name(dinfo->ap_handle), buflen);
151     }
152     return (0);
153 }
154 
155 /*
156  * PCI power manangement
157  */
158 static int
159 acpi_pci_set_powerstate_method(device_t dev, device_t child, int state)
160 {
161 	ACPI_HANDLE h;
162 	ACPI_STATUS status;
163 	int old_state, error;
164 
165 	error = 0;
166 	if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
167 		return (EINVAL);
168 
169 	/*
170 	 * We set the state using PCI Power Management outside of setting
171 	 * the ACPI state.  This means that when powering down a device, we
172 	 * first shut it down using PCI, and then using ACPI, which lets ACPI
173 	 * try to power down any Power Resources that are now no longer used.
174 	 * When powering up a device, we let ACPI set the state first so that
175 	 * it can enable any needed Power Resources before changing the PCI
176 	 * power state.
177 	 */
178 	ACPI_SERIAL_BEGIN(pci_powerstate);
179 	old_state = pci_get_powerstate(child);
180 	if (old_state < state) {
181 		error = pci_set_powerstate_method(dev, child, state);
182 		if (error)
183 			goto out;
184 	}
185 	h = acpi_get_handle(child);
186 	status = acpi_pwr_switch_consumer(h, state);
187 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
188 		device_printf(dev,
189 		    "Failed to set ACPI power state D%d on %s: %s\n",
190 		    state, acpi_name(h), AcpiFormatException(status));
191 	if (old_state > state)
192 		error = pci_set_powerstate_method(dev, child, state);
193 
194 out:
195 	ACPI_SERIAL_END(pci_powerstate);
196 	return (error);
197 }
198 
199 static void
200 acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child)
201 {
202 	ACPI_STATUS status;
203 	device_t child;
204 
205 	/*
206 	 * Lookup and remove the unused device that acpi0 creates when it walks
207 	 * the namespace creating devices.
208 	 */
209 	child = acpi_get_device(handle);
210 	if (child != NULL) {
211 		KASSERT(!device_is_alive(child), ("%s: deleting alive child %s",
212 		    __func__, device_get_nameunit(child)));
213 		KASSERT(device_get_parent(child) ==
214 		    devclass_get_device(devclass_find("acpi"), 0),
215 		    ("%s: child (%s)'s parent is not acpi0", __func__,
216 		    acpi_name(handle)));
217 		device_delete_child(device_get_parent(child), child);
218 	}
219 
220 	/*
221 	 * Update ACPI-CA to use the PCI enumerated device_t for this handle.
222 	 */
223 	status = AcpiDetachData(handle, acpi_fake_objhandler);
224 	if (ACPI_FAILURE(status))
225 		printf("WARNING: Unable to detach object data from %s - %s\n",
226 		    acpi_name(handle), AcpiFormatException(status));
227 	status = AcpiAttachData(handle, acpi_fake_objhandler, pci_child);
228 	if (ACPI_FAILURE(status))
229 		printf("WARNING: Unable to attach object data to %s - %s\n",
230 		    acpi_name(handle), AcpiFormatException(status));
231 }
232 
233 static ACPI_STATUS
234 acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, void *context,
235     void **status)
236 {
237 	struct acpi_pci_devinfo *dinfo;
238 	device_t *devlist;
239 	int devcount, i, func, slot;
240 	UINT32 address;
241 
242 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
243 
244 	if (ACPI_FAILURE(acpi_GetInteger(handle, "_ADR", &address)))
245 		return_ACPI_STATUS (AE_OK);
246 	slot = ACPI_ADR_PCI_SLOT(address);
247 	func = ACPI_ADR_PCI_FUNC(address);
248 	if (device_get_children((device_t)context, &devlist, &devcount) != 0)
249 		return_ACPI_STATUS (AE_OK);
250 	for (i = 0; i < devcount; i++) {
251 		dinfo = device_get_ivars(devlist[i]);
252 		if (dinfo->ap_dinfo.cfg.func == func &&
253 		    dinfo->ap_dinfo.cfg.slot == slot) {
254 			dinfo->ap_handle = handle;
255 			acpi_pci_update_device(handle, devlist[i]);
256 			break;
257 		}
258 	}
259 	free(devlist, M_TEMP);
260 	return_ACPI_STATUS (AE_OK);
261 }
262 
263 static int
264 acpi_pci_probe(device_t dev)
265 {
266 
267 	if (pcib_get_bus(dev) < 0)
268 		return (ENXIO);
269 	if (acpi_get_handle(dev) == NULL)
270 		return (ENXIO);
271 	device_set_desc(dev, "ACPI PCI bus");
272 	return (0);
273 }
274 
275 static int
276 acpi_pci_attach(device_t dev)
277 {
278 	int busno;
279 
280 	/*
281 	 * Since there can be multiple independantly numbered PCI
282 	 * busses on some large alpha systems, we can't use the unit
283 	 * number to decide what bus we are probing. We ask the parent
284 	 * pcib what our bus number is.
285 	 */
286 	busno = pcib_get_bus(dev);
287 	if (bootverbose)
288 		device_printf(dev, "physical bus=%d\n", busno);
289 
290 	/*
291 	 * First, PCI devices are added as in the normal PCI bus driver.
292 	 * Afterwards, the ACPI namespace under the bridge driver is
293 	 * walked to save ACPI handles to all the devices that appear in
294 	 * the ACPI namespace as immediate descendants of the bridge.
295 	 *
296 	 * XXX: Sometimes PCI devices show up in the ACPI namespace that
297 	 * pci_add_children() doesn't find.  We currently just ignore
298 	 * these devices.
299 	 */
300 	pci_add_children(dev, busno, sizeof(struct acpi_pci_devinfo));
301 	AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1,
302 	    acpi_pci_save_handle, dev, NULL);
303 
304 	return (bus_generic_attach(dev));
305 }
306