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