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