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